{"version":3,"file":"branch-summarization.d.ts","sourceRoot":"","sources":["../../../src/harness/compaction/branch-summarization.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAEvC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAOnD,OAAO,KAAK,EAAE,mBAAmB,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,kBAAkB,EAAW,KAAK,MAAM,EAAgB,MAAM,aAAa,CAAC;AAErF,OAAO,EAIN,KAAK,cAAc,EAGnB,MAAM,YAAY,CAAC;AAEpB,yEAAyE;AACzE,MAAM,WAAW,oBAAoB;IACpC,wDAAwD;IACxD,SAAS,EAAE,MAAM,EAAE,CAAC;IACpB,4DAA4D;IAC5D,aAAa,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,YAAY,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,iDAAiD;AACjD,MAAM,WAAW,iBAAiB;IACjC,gDAAgD;IAChD,QAAQ,EAAE,YAAY,EAAE,CAAC;IACzB,iDAAiD;IACjD,OAAO,EAAE,cAAc,CAAC;IACxB,mDAAmD;IACnD,WAAW,EAAE,MAAM,CAAC;CACpB;AAED,iDAAiD;AACjD,MAAM,WAAW,oBAAoB;IACpC,mDAAmD;IACnD,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAC5B,0EAA0E;IAC1E,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;CAChC;AAED,+CAA+C;AAC/C,MAAM,WAAW,4BAA4B;IAC5C,oCAAoC;IACpC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,yCAAyC;IACzC,MAAM,EAAE,MAAM,CAAC;IACf,0DAA0D;IAC1D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,kDAAkD;IAClD,MAAM,EAAE,WAAW,CAAC;IACpB,yEAAyE;IACzE,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,qFAAqF;IACrF,mBAAmB,CAAC,EAAE,OAAO,CAAC;IAC9B,sEAAsE;IACtE,aAAa,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,qGAAqG;AACrG,wBAAsB,8BAA8B,CACnD,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,MAAM,GAAG,IAAI,EACxB,QAAQ,EAAE,MAAM,GACd,OAAO,CAAC,oBAAoB,CAAC,CAyB/B;AA0BD,gFAAgF;AAChF,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,gBAAgB,EAAE,EAAE,WAAW,GAAE,MAAU,GAAG,iBAAiB,CAuC5G;AAoCD,uDAAuD;AACvD,wBAAsB,qBAAqB,CAC1C,OAAO,EAAE,gBAAgB,EAAE,EAC3B,OAAO,EAAE,4BAA4B,GACnC,OAAO,CAAC,MAAM,CAAC,mBAAmB,EAAE,kBAAkB,CAAC,CAAC,CA2D1D","sourcesContent":["/**\n * Branch summarization for tree navigation.\n *\n * When navigating to a different point in the session tree, this generates\n * a summary of the branch being left so context isn't lost.\n */\n\nimport type { Model } from \"@draht/ai\";\nimport { completeSimple } from \"@draht/ai\";\nimport type { AgentMessage } from \"../../types.ts\";\nimport {\n\tconvertToLlm,\n\tcreateBranchSummaryMessage,\n\tcreateCompactionSummaryMessage,\n\tcreateCustomMessage,\n} from \"../messages.ts\";\nimport type { BranchSummaryResult, Session, SessionTreeEntry } from \"../types.ts\";\nimport { BranchSummaryError, err, ok, type Result, SessionError } from \"../types.ts\";\nimport { estimateTokens, SUMMARIZATION_SYSTEM_PROMPT } from \"./compaction.ts\";\nimport {\n\tcomputeFileLists,\n\tcreateFileOps,\n\textractFileOpsFromMessage,\n\ttype FileOperations,\n\tformatFileOperations,\n\tserializeConversation,\n} from \"./utils.ts\";\n\n/** File-operation details stored on generated branch summary entries. */\nexport interface BranchSummaryDetails {\n\t/** Files read while exploring the summarized branch. */\n\treadFiles: string[];\n\t/** Files modified while exploring the summarized branch. */\n\tmodifiedFiles: string[];\n}\n\nexport type { FileOperations } from \"./utils.ts\";\n\n/** Prepared branch content for summarization. */\nexport interface BranchPreparation {\n\t/** Messages selected for the branch summary. */\n\tmessages: AgentMessage[];\n\t/** File operations extracted from the branch. */\n\tfileOps: FileOperations;\n\t/** Estimated token count for selected messages. */\n\ttotalTokens: number;\n}\n\n/** Entries selected for branch summarization. */\nexport interface CollectEntriesResult {\n\t/** Entries to summarize in chronological order. */\n\tentries: SessionTreeEntry[];\n\t/** Deepest common ancestor between the previous leaf and target entry. */\n\tcommonAncestorId: string | null;\n}\n\n/** Options for generating a branch summary. */\nexport interface GenerateBranchSummaryOptions {\n\t/** Model used for summarization. */\n\tmodel: Model<any>;\n\t/** API key forwarded to the provider. */\n\tapiKey: string;\n\t/** Optional request headers forwarded to the provider. */\n\theaders?: Record<string, string>;\n\t/** Abort signal for the summarization request. */\n\tsignal: AbortSignal;\n\t/** Optional instructions appended to or replacing the default prompt. */\n\tcustomInstructions?: string;\n\t/** Replace the default prompt with custom instructions instead of appending them. */\n\treplaceInstructions?: boolean;\n\t/** Tokens reserved for prompt and model output. Defaults to 16384. */\n\treserveTokens?: number;\n}\n\n/** Collect entries that should be summarized before navigating to a different session tree entry. */\nexport async function collectEntriesForBranchSummary(\n\tsession: Session,\n\toldLeafId: string | null,\n\ttargetId: string,\n): Promise<CollectEntriesResult> {\n\tif (!oldLeafId) {\n\t\treturn { entries: [], commonAncestorId: null };\n\t}\n\tconst oldPath = new Set((await session.getBranch(oldLeafId)).map((e) => e.id));\n\tconst targetPath = await session.getBranch(targetId);\n\tlet commonAncestorId: string | null = null;\n\tfor (let i = targetPath.length - 1; i >= 0; i--) {\n\t\tif (oldPath.has(targetPath[i].id)) {\n\t\t\tcommonAncestorId = targetPath[i].id;\n\t\t\tbreak;\n\t\t}\n\t}\n\tconst entries: SessionTreeEntry[] = [];\n\tlet current: string | null = oldLeafId;\n\n\twhile (current && current !== commonAncestorId) {\n\t\tconst entry = await session.getEntry(current);\n\t\tif (!entry) throw new SessionError(\"invalid_session\", `Entry ${current} not found`);\n\t\tentries.push(entry as SessionTreeEntry);\n\t\tcurrent = entry.parentId;\n\t}\n\tentries.reverse();\n\n\treturn { entries, commonAncestorId };\n}\nfunction getMessageFromEntry(entry: SessionTreeEntry): AgentMessage | undefined {\n\tswitch (entry.type) {\n\t\tcase \"message\":\n\t\t\tif (entry.message.role === \"toolResult\") return undefined;\n\t\t\treturn entry.message;\n\n\t\tcase \"custom_message\":\n\t\t\treturn createCustomMessage(entry.customType, entry.content, entry.display, entry.details, entry.timestamp);\n\n\t\tcase \"branch_summary\":\n\t\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\n\t\tcase \"compaction\":\n\t\t\treturn createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);\n\t\tcase \"thinking_level_change\":\n\t\tcase \"model_change\":\n\t\tcase \"active_tools_change\":\n\t\tcase \"custom\":\n\t\tcase \"label\":\n\t\tcase \"session_info\":\n\t\tcase \"leaf\":\n\t\t\treturn undefined;\n\t}\n}\n\n/** Prepare branch entries for summarization within an optional token budget. */\nexport function prepareBranchEntries(entries: SessionTreeEntry[], tokenBudget: number = 0): BranchPreparation {\n\tconst messages: AgentMessage[] = [];\n\tconst fileOps = createFileOps();\n\tlet totalTokens = 0;\n\tfor (const entry of entries) {\n\t\tif (entry.type === \"branch_summary\" && !entry.fromHook && entry.details) {\n\t\t\tconst details = entry.details as BranchSummaryDetails;\n\t\t\tif (Array.isArray(details.readFiles)) {\n\t\t\t\tfor (const f of details.readFiles) fileOps.read.add(f);\n\t\t\t}\n\t\t\tif (Array.isArray(details.modifiedFiles)) {\n\t\t\t\tfor (const f of details.modifiedFiles) {\n\t\t\t\t\tfileOps.edited.add(f);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tconst message = getMessageFromEntry(entry);\n\t\tif (!message) continue;\n\t\textractFileOpsFromMessage(message, fileOps);\n\n\t\tconst tokens = estimateTokens(message);\n\t\tif (tokenBudget > 0 && totalTokens + tokens > tokenBudget) {\n\t\t\tif (entry.type === \"compaction\" || entry.type === \"branch_summary\") {\n\t\t\t\tif (totalTokens < tokenBudget * 0.9) {\n\t\t\t\t\tmessages.unshift(message);\n\t\t\t\t\ttotalTokens += tokens;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\tmessages.unshift(message);\n\t\ttotalTokens += tokens;\n\t}\n\n\treturn { messages, fileOps, totalTokens };\n}\n\nconst BRANCH_SUMMARY_PREAMBLE = `The user explored a different conversation branch before returning here.\nSummary of that exploration:\n\n`;\n\nconst BRANCH_SUMMARY_PROMPT = `Create a structured summary of this conversation branch for context when returning later.\n\nUse this EXACT format:\n\n## Goal\n[What was the user trying to accomplish in this branch?]\n\n## Constraints & Preferences\n- [Any constraints, preferences, or requirements mentioned]\n- [Or \"(none)\" if none were mentioned]\n\n## Progress\n### Done\n- [x] [Completed tasks/changes]\n\n### In Progress\n- [ ] [Work that was started but not finished]\n\n### Blocked\n- [Issues preventing progress, if any]\n\n## Key Decisions\n- **[Decision]**: [Brief rationale]\n\n## Next Steps\n1. [What should happen next to continue this work]\n\nKeep each section concise. Preserve exact file paths, function names, and error messages.`;\n\n/** Generate a summary for abandoned branch entries. */\nexport async function generateBranchSummary(\n\tentries: SessionTreeEntry[],\n\toptions: GenerateBranchSummaryOptions,\n): Promise<Result<BranchSummaryResult, BranchSummaryError>> {\n\tconst { model, apiKey, headers, signal, customInstructions, replaceInstructions, reserveTokens = 16384 } = options;\n\tconst contextWindow = model.contextWindow || 128000;\n\tconst tokenBudget = contextWindow - reserveTokens;\n\n\tconst { messages, fileOps } = prepareBranchEntries(entries, tokenBudget);\n\n\tif (messages.length === 0) {\n\t\treturn ok({ summary: \"No content to summarize\", readFiles: [], modifiedFiles: [] });\n\t}\n\tconst llmMessages = convertToLlm(messages);\n\tconst conversationText = serializeConversation(llmMessages);\n\tlet instructions: string;\n\tif (replaceInstructions && customInstructions) {\n\t\tinstructions = customInstructions;\n\t} else if (customInstructions) {\n\t\tinstructions = `${BRANCH_SUMMARY_PROMPT}\\n\\nAdditional focus: ${customInstructions}`;\n\t} else {\n\t\tinstructions = BRANCH_SUMMARY_PROMPT;\n\t}\n\tconst promptText = `<conversation>\\n${conversationText}\\n</conversation>\\n\\n${instructions}`;\n\n\tconst summarizationMessages = [\n\t\t{\n\t\t\trole: \"user\" as const,\n\t\t\tcontent: [{ type: \"text\" as const, text: promptText }],\n\t\t\ttimestamp: Date.now(),\n\t\t},\n\t];\n\tconst response = await completeSimple(\n\t\tmodel,\n\t\t{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },\n\t\t{ apiKey, headers, signal, maxTokens: 2048 },\n\t);\n\tif (response.stopReason === \"aborted\") {\n\t\treturn err(new BranchSummaryError(\"aborted\", response.errorMessage || \"Branch summary aborted\"));\n\t}\n\tif (response.stopReason === \"error\") {\n\t\treturn err(\n\t\t\tnew BranchSummaryError(\n\t\t\t\t\"summarization_failed\",\n\t\t\t\t`Branch summary failed: ${response.errorMessage || \"Unknown error\"}`,\n\t\t\t),\n\t\t);\n\t}\n\n\tlet summary = response.content\n\t\t.filter((c): c is { type: \"text\"; text: string } => c.type === \"text\")\n\t\t.map((c) => c.text)\n\t\t.join(\"\\n\");\n\tsummary = BRANCH_SUMMARY_PREAMBLE + summary;\n\tconst { readFiles, modifiedFiles } = computeFileLists(fileOps);\n\tsummary += formatFileOperations(readFiles, modifiedFiles);\n\n\treturn ok({\n\t\tsummary: summary || \"No summary generated\",\n\t\treadFiles,\n\t\tmodifiedFiles,\n\t});\n}\n"]}