/**
 * ContextCapsule — bounded per-node context for agent runtimes.
 *
 * OMK memory = canonical (graph-state.json, .omk/runs/).
 * Kimi memory = disposable (isolated HOME per node).
 */
export const DEFAULT_CONTEXT_BUDGET = {
    maxInputTokens: 8192,
    reservedOutputTokens: 4096,
    maxFileTokens: 4096,
    maxToolResultTokens: 2048,
    maxMemoryFacts: 10,
    compression: "lossless-ish",
};
export const CONTEXT_BUDGET_PRESETS = {
    tiny: { maxInputTokens: 4096, reservedOutputTokens: 2048, maxFileTokens: 2048, maxToolResultTokens: 1024, maxMemoryFacts: 5, compression: "aggressive" },
    small: DEFAULT_CONTEXT_BUDGET,
    normal: { maxInputTokens: 16384, reservedOutputTokens: 8192, maxFileTokens: 8192, maxToolResultTokens: 4096, maxMemoryFacts: 20, compression: "lossless-ish" },
};
export function estimateCapsuleTokens(capsule) {
    const b = breakdownCapsuleTokens(capsule);
    return b.total;
}
export function breakdownCapsuleTokens(capsule) {
    const system = Math.ceil(capsule.system.length / 4);
    const task = Math.ceil(capsule.task.length / 4);
    const goal = Math.ceil(capsule.goal.length / 4);
    let dependencies = 0;
    for (const s of capsule.dependencySummaries)
        dependencies += Math.ceil(s.length / 4);
    let files = 0;
    for (const f of capsule.relevantFiles)
        files += Math.ceil(f.content.length / 4);
    let memory = 0;
    for (const m of capsule.graphMemory)
        memory += Math.ceil((m.key.length + m.value.length) / 4);
    let evidence = 0;
    for (const e of capsule.evidenceRequirements)
        evidence += Math.ceil((e.gate.length + (e.ref?.length ?? 0)) / 4);
    let priorAttempts = 0;
    for (const a of capsule.priorAttempts) {
        priorAttempts += Math.ceil((String(a.attempt).length + a.provider.length + a.status.length + (a.failureSummary?.length ?? 0)) / 4);
    }
    const total = system + task + goal + dependencies + files + memory + evidence + priorAttempts;
    return { system, task, goal, dependencies, files, memory, evidence, priorAttempts, total };
}
