"use strict";
// src/orchestrator/story/HandoffArtifact.ts
// v1.17.0 — Compact YAML artifact carried between iterations of the
// autonomous story loop. Spec from .claude/rules/agent-handoff.md:
//   - max ~500 tokens
//   - last 5 decisions, last 10 file mods, last 3 blockers
//   - story context + next_action
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports._limits = void 0;
exports.buildHandoffArtifact = buildHandoffArtifact;
exports.serializeHandoff = serializeHandoff;
exports.parseHandoff = parseHandoff;
const yaml = __importStar(require("js-yaml"));
const MAX_DECISIONS = 5;
const MAX_FILES = 10;
const MAX_BLOCKERS = 3;
/** ~4 chars/token average — 500 tokens × 4 = 2000 chars upper bound. */
const MAX_CHARS = 2000;
function buildHandoffArtifact(input) {
    return {
        handoff: {
            from_agent: input.fromAgent,
            to_agent: input.toAgent,
            story_context: {
                story_id: input.story.id,
                story_path: input.story.path,
                story_status: input.story.status,
                story_title: input.story.title,
                open_ac: input.story.openACCount,
                total_ac: input.story.totalACCount,
                branch: input.story.branch,
            },
            decisions: (input.decisions || []).slice(-MAX_DECISIONS),
            files_modified: (input.filesModified || []).slice(-MAX_FILES),
            blockers: (input.blockers || []).slice(-MAX_BLOCKERS),
            next_action: input.nextAction,
            created_at: new Date().toISOString(),
        },
    };
}
/** Trim a single string to a max char count with "…" suffix when truncated. */
function clampStr(s, max) {
    if (s.length <= max)
        return s;
    return s.slice(0, max - 1) + '…';
}
function serializeHandoff(record) {
    let yamlStr = yaml.dump(record, { lineWidth: 100, noRefs: true });
    if (yamlStr.length <= MAX_CHARS)
        return yamlStr;
    // Tier 1: slice arrays.
    const trimmed = {
        handoff: {
            ...record.handoff,
            decisions: record.handoff.decisions.slice(-3),
            files_modified: record.handoff.files_modified.slice(-5),
        },
    };
    yamlStr = yaml.dump(trimmed, { lineWidth: 100, noRefs: true });
    if (yamlStr.length <= MAX_CHARS)
        return yamlStr;
    // Tier 2: clamp each individual decision + file string.
    const clamped = {
        handoff: {
            ...trimmed.handoff,
            decisions: trimmed.handoff.decisions.map(s => clampStr(s, 200)),
            files_modified: trimmed.handoff.files_modified.map(s => clampStr(s, 120)),
            next_action: clampStr(trimmed.handoff.next_action, 240),
        },
    };
    yamlStr = yaml.dump(clamped, { lineWidth: 100, noRefs: true });
    if (yamlStr.length <= MAX_CHARS)
        return yamlStr;
    // Tier 3: hard-truncate the yaml itself (last resort).
    return yamlStr.slice(0, MAX_CHARS - 4) + '\n…\n';
}
function parseHandoff(yamlStr) {
    try {
        const parsed = yaml.load(yamlStr);
        if (!parsed?.handoff || typeof parsed.handoff.from_agent !== 'string')
            return null;
        return parsed;
    }
    catch {
        return null;
    }
}
/** Helper for tests + diagnostics. */
exports._limits = { MAX_DECISIONS, MAX_FILES, MAX_BLOCKERS, MAX_CHARS };
