@gajae-code/agent-core 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +482 -0
  2. package/README.md +473 -0
  3. package/dist/types/agent-loop.d.ts +55 -0
  4. package/dist/types/agent.d.ts +334 -0
  5. package/dist/types/append-only-context.d.ts +113 -0
  6. package/dist/types/compaction/branch-summarization.d.ts +94 -0
  7. package/dist/types/compaction/compaction.d.ts +166 -0
  8. package/dist/types/compaction/entries.d.ts +103 -0
  9. package/dist/types/compaction/errors.d.ts +26 -0
  10. package/dist/types/compaction/index.d.ts +11 -0
  11. package/dist/types/compaction/messages.d.ts +61 -0
  12. package/dist/types/compaction/openai.d.ts +58 -0
  13. package/dist/types/compaction/pruning.d.ts +18 -0
  14. package/dist/types/compaction/utils.d.ts +32 -0
  15. package/dist/types/compaction.d.ts +1 -0
  16. package/dist/types/harmony-leak.d.ts +99 -0
  17. package/dist/types/index.d.ts +10 -0
  18. package/dist/types/proxy.d.ts +84 -0
  19. package/dist/types/run-collector.d.ts +196 -0
  20. package/dist/types/telemetry.d.ts +588 -0
  21. package/dist/types/thinking.d.ts +17 -0
  22. package/dist/types/types.d.ts +407 -0
  23. package/package.json +75 -0
  24. package/src/agent-loop.ts +1279 -0
  25. package/src/agent.ts +1399 -0
  26. package/src/append-only-context.ts +297 -0
  27. package/src/compaction/branch-summarization.ts +339 -0
  28. package/src/compaction/compaction.ts +1065 -0
  29. package/src/compaction/entries.ts +133 -0
  30. package/src/compaction/errors.ts +31 -0
  31. package/src/compaction/index.ts +12 -0
  32. package/src/compaction/messages.ts +212 -0
  33. package/src/compaction/openai.ts +552 -0
  34. package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
  35. package/src/compaction/prompts/branch-summary-context.md +5 -0
  36. package/src/compaction/prompts/branch-summary-preamble.md +2 -0
  37. package/src/compaction/prompts/branch-summary.md +30 -0
  38. package/src/compaction/prompts/compaction-short-summary.md +9 -0
  39. package/src/compaction/prompts/compaction-summary-context.md +5 -0
  40. package/src/compaction/prompts/compaction-summary.md +38 -0
  41. package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
  42. package/src/compaction/prompts/compaction-update-summary.md +45 -0
  43. package/src/compaction/prompts/file-operations.md +10 -0
  44. package/src/compaction/prompts/handoff-document.md +49 -0
  45. package/src/compaction/prompts/summarization-system.md +3 -0
  46. package/src/compaction/pruning.ts +92 -0
  47. package/src/compaction/utils.ts +185 -0
  48. package/src/compaction.ts +1 -0
  49. package/src/harmony-leak.ts +427 -0
  50. package/src/index.ts +19 -0
  51. package/src/proxy.ts +326 -0
  52. package/src/run-collector.ts +631 -0
  53. package/src/telemetry.ts +2018 -0
  54. package/src/thinking.ts +19 -0
  55. package/src/types.ts +467 -0
@@ -0,0 +1,297 @@
1
+ /**
2
+ * Append-only context mode — stabilizes the byte prefix sent to the LLM
3
+ * across turns so provider prefix caches (DeepSeek, Anthropic, etc.)
4
+ * hit at the maximum possible rate.
5
+ *
6
+ * Two mechanisms:
7
+ *
8
+ * 1. **StablePrefix** — system prompt + tool specs are computed once
9
+ * and frozen. Subsequent turns reuse the exact same byte sequence
10
+ * unless `invalidate()` is called (e.g. after MCP reconnect).
11
+ *
12
+ * 2. **AppendOnlyLog** — messages only grow; prior turns are never
13
+ * re-serialized. Combined with a stable prefix, only the user's new
14
+ * message delta is a cache miss each turn.
15
+ */
16
+
17
+ import type { Context, Message, Tool } from "@gajae-code/ai";
18
+ import { normalizeTools } from "./agent-loop";
19
+ import type { AgentContext } from "./types";
20
+
21
+ // ---------------------------------------------------------------------------
22
+ // StablePrefix (formerly ImmutablePrefix)
23
+ // ---------------------------------------------------------------------------
24
+
25
+ /** Frozen system prompt + tool spec snapshot. */
26
+ export interface StablePrefixSnapshot {
27
+ systemPrompt: string[];
28
+ tools: Tool[];
29
+ fingerprint: string;
30
+ }
31
+
32
+ /** Options threaded through `build()` so the snapshot reflects loop-time settings. */
33
+ export interface BuildOptions {
34
+ /** Inject the `_i` intent field into tool schemas (must match agent-loop's normalizeTools). */
35
+ intentTracing: boolean;
36
+ }
37
+
38
+ /**
39
+ * A frozen prefix (system prompt + tools) that produces stable byte
40
+ * sequences across `build()` calls.
41
+ *
42
+ * The first `build()` snapshots the live state. Subsequent calls reuse
43
+ * the cached copy until `invalidate()` is called or the live state's
44
+ * fingerprint changes.
45
+ */
46
+ export class StablePrefix {
47
+ #snapshot: StablePrefixSnapshot | null = null;
48
+ #version = 0;
49
+
50
+ get fingerprint(): string {
51
+ return this.#snapshot?.fingerprint ?? "<unbuilt>";
52
+ }
53
+ get version(): number {
54
+ return this.#version;
55
+ }
56
+ get built(): boolean {
57
+ return this.#snapshot !== null;
58
+ }
59
+
60
+ /**
61
+ * Build or rebuild from live context.
62
+ * Returns `true` if the prefix actually changed (cache miss imminent).
63
+ */
64
+ build(context: AgentContext, options: BuildOptions): boolean {
65
+ const snapshot = takeSnapshot(context, options);
66
+ if (this.#snapshot && this.#snapshot.fingerprint === snapshot.fingerprint) {
67
+ return false;
68
+ }
69
+ this.#snapshot = snapshot;
70
+ this.#version++;
71
+ return true;
72
+ }
73
+
74
+ /** Force rebuild on the next `build()` call. */
75
+ invalidate(): void {
76
+ this.#snapshot = null;
77
+ }
78
+
79
+ /**
80
+ * Returns the cached prefix.
81
+ * @throws if `build()` was never called.
82
+ */
83
+ toContext(): { systemPrompt: string[]; tools: Tool[] } {
84
+ const s = this.#snapshot;
85
+ if (!s) throw new Error("StablePrefix.toContext() called before build()");
86
+ return { systemPrompt: s.systemPrompt, tools: s.tools };
87
+ }
88
+ }
89
+
90
+ // ---------------------------------------------------------------------------
91
+ // AppendOnlyLog
92
+ // ---------------------------------------------------------------------------
93
+
94
+ /**
95
+ * Append-only message log at the `Message[]` (provider-level) layer.
96
+ *
97
+ * The only mutation path is `replaceTail()`, reserved for compaction.
98
+ * Every other operation is append-only.
99
+ */
100
+ export class AppendOnlyLog {
101
+ #entries: Message[] = [];
102
+
103
+ get length(): number {
104
+ return this.#entries.length;
105
+ }
106
+
107
+ append(message: any): void {
108
+ this.#entries.push(message);
109
+ }
110
+
111
+ extend(messages: any[]): void {
112
+ for (const m of messages) this.#entries.push(m);
113
+ }
114
+
115
+ /** Replace the last entry — only legal for compaction. */
116
+ replaceTail(replacement: any): void {
117
+ const idx = this.#entries.length - 1;
118
+ if (idx >= 0) this.#entries[idx] = replacement;
119
+ }
120
+
121
+ /** Returns a shallow copy of all entries. */
122
+ toMessages(): Message[] {
123
+ return this.#entries.slice();
124
+ }
125
+
126
+ /** Direct readonly access for in-place inspection. */
127
+ entries(): readonly Message[] {
128
+ return this.#entries;
129
+ }
130
+
131
+ clear(): void {
132
+ this.#entries = [];
133
+ }
134
+ }
135
+
136
+ // ---------------------------------------------------------------------------
137
+ // AppendOnlyContextManager
138
+ // ---------------------------------------------------------------------------
139
+
140
+ /**
141
+ * Manages a stable prefix + append-only log for the agent loop.
142
+ *
143
+ * Call `build(context)` each turn to get a `Context` with stable
144
+ * `systemPrompt` and `tools` and append-only messages. Call
145
+ * `syncMessages(normalizedMessages)` after `convertToLlm` each
146
+ * turn to keep the log in sync.
147
+ *
148
+ * Example:
149
+ * ```
150
+ * const mgr = new AppendOnlyContextManager();
151
+ * const ctx = mgr.build(context); // first call snapshots prefix
152
+ * mgr.syncMessages(normalized); // grow the log
153
+ * ctx = mgr.build(context); // subsequent calls use cache
154
+ * ```
155
+ */
156
+ export class AppendOnlyContextManager {
157
+ readonly prefix = new StablePrefix();
158
+ readonly log = new AppendOnlyLog();
159
+ /** How many normalized messages were synced into the log as of the last sync. */
160
+ #lastSyncCount = 0;
161
+ /** Rolling digest of synced message content — detects in-place rewrites. */
162
+ #syncedDigest = 0;
163
+
164
+ build(context: AgentContext, options: BuildOptions): Context {
165
+ this.prefix.build(context, options);
166
+ const { systemPrompt, tools } = this.prefix.toContext();
167
+ return { systemPrompt, messages: this.log.toMessages(), tools };
168
+ }
169
+
170
+ /**
171
+ * Sync normalized (provider-level) messages into the append-only log.
172
+ *
173
+ * Detects both compaction (shorter array) and in-place rewrites
174
+ * (same length, changed content via a rolling digest).
175
+ */
176
+ syncMessages(normalizedMessages: any[]): void {
177
+ // Detect in-place rewrites of already-synced messages.
178
+ if (
179
+ this.#lastSyncCount > 0 &&
180
+ this.#lastSyncCount <= normalizedMessages.length &&
181
+ this.#computeDigest(normalizedMessages.slice(0, this.#lastSyncCount)) !== this.#syncedDigest
182
+ ) {
183
+ this.log.clear();
184
+ this.#lastSyncCount = 0;
185
+ }
186
+
187
+ // Compaction — array shrunk.
188
+ if (normalizedMessages.length < this.#lastSyncCount) {
189
+ this.log.clear();
190
+ this.#lastSyncCount = 0;
191
+ }
192
+
193
+ const newMsgs = normalizedMessages.slice(this.#lastSyncCount);
194
+ for (const msg of newMsgs) {
195
+ this.log.append(msg);
196
+ }
197
+
198
+ this.#lastSyncCount = normalizedMessages.length;
199
+ this.#syncedDigest = this.#computeDigest(normalizedMessages);
200
+ }
201
+
202
+ /** Reset prefix + log for a model/provider switch while mode stays active. */
203
+ invalidateForModelChange(): void {
204
+ this.prefix.invalidate();
205
+ this.log.clear();
206
+ this.#lastSyncCount = 0;
207
+ this.#syncedDigest = 0;
208
+ }
209
+
210
+ /** Reset the sync cursor AND clear the log. */
211
+ resetSyncCursor(): void {
212
+ this.log.clear();
213
+ this.#lastSyncCount = 0;
214
+ this.#syncedDigest = 0;
215
+ }
216
+
217
+ appendMessage(message: any): void {
218
+ this.log.append(message);
219
+ }
220
+
221
+ replaceTailMessage(message: any): void {
222
+ this.log.replaceTail(message);
223
+ }
224
+
225
+ invalidate(): void {
226
+ this.prefix.invalidate();
227
+ }
228
+
229
+ reset(context: AgentContext, options: BuildOptions): void {
230
+ this.prefix.invalidate();
231
+ this.log.clear();
232
+ this.#lastSyncCount = 0;
233
+ this.#syncedDigest = 0;
234
+ this.prefix.build(context, options);
235
+ }
236
+
237
+ /**
238
+ * Deterministic digest over every field the provider may serialize — role,
239
+ * content, tool calls (both `toolCalls` and OpenAI-wire `tool_calls`),
240
+ * `tool_call_id`, `name`, `id`. Hashed with the same FNV-style rolling
241
+ * accumulator so in-place rewrites of *any* of these fields are visible.
242
+ */
243
+ #computeDigest(messages: readonly unknown[]): number {
244
+ let hash = 0;
245
+ for (let i = 0; i < messages.length; i++) {
246
+ const msg = messages[i];
247
+ if (!msg || typeof msg !== "object") continue;
248
+ const m = msg as Record<string, unknown>;
249
+ const payload = JSON.stringify({
250
+ r: m.role ?? null,
251
+ c: m.content ?? null,
252
+ tc: m.toolCalls ?? m.tool_calls ?? null,
253
+ tcid: m.tool_call_id ?? null,
254
+ n: m.name ?? null,
255
+ id: m.id ?? null,
256
+ });
257
+ for (let j = 0; j < payload.length; j++) {
258
+ hash = ((hash << 5) - hash + payload.charCodeAt(j)) | 0;
259
+ }
260
+ }
261
+ return hash >>> 0;
262
+ }
263
+ }
264
+
265
+ // ---------------------------------------------------------------------------
266
+ // Snapshot helpers
267
+ // ---------------------------------------------------------------------------
268
+
269
+ function takeSnapshot(context: AgentContext, options: BuildOptions): StablePrefixSnapshot {
270
+ const systemPrompt = [...context.systemPrompt];
271
+ const tools = normalizeTools(context.tools, options.intentTracing) ?? [];
272
+ return {
273
+ systemPrompt,
274
+ tools,
275
+ fingerprint: computeFingerprint(systemPrompt, tools, options),
276
+ };
277
+ }
278
+
279
+ function computeFingerprint(systemPrompt: string[], tools: Tool[], options: BuildOptions): string {
280
+ const payload = JSON.stringify({
281
+ s: systemPrompt,
282
+ t: tools.map(t => ({
283
+ n: t.name,
284
+ d: t.description,
285
+ p: t.parameters,
286
+ s: t.strict,
287
+ cf: t.customFormat,
288
+ cw: t.customWireName,
289
+ })),
290
+ i: options.intentTracing,
291
+ });
292
+ let hash = 0;
293
+ for (let i = 0; i < payload.length; i++) {
294
+ hash = ((hash << 5) - hash + payload.charCodeAt(i)) | 0;
295
+ }
296
+ return (hash >>> 0).toString(36);
297
+ }
@@ -0,0 +1,339 @@
1
+ /**
2
+ * Branch summarization for tree navigation.
3
+ *
4
+ * When navigating to a different point in the session tree, this generates
5
+ * a summary of the branch being left so context isn't lost.
6
+ */
7
+
8
+ import type { Model } from "@gajae-code/ai";
9
+ import { prompt } from "@gajae-code/utils";
10
+ import { type AgentTelemetry, instrumentedCompleteSimple } from "../telemetry";
11
+ import type { AgentMessage } from "../types";
12
+ import { estimateTokens } from "./compaction";
13
+ import type { ReadonlySessionManager, SessionEntry } from "./entries";
14
+ import {
15
+ type ConvertToLlm,
16
+ convertToLlm,
17
+ createBranchSummaryMessage,
18
+ createCompactionSummaryMessage,
19
+ createCustomMessage,
20
+ } from "./messages";
21
+ import branchSummaryPrompt from "./prompts/branch-summary.md" with { type: "text" };
22
+ import branchSummaryPreamble from "./prompts/branch-summary-preamble.md" with { type: "text" };
23
+ import {
24
+ computeFileLists,
25
+ createFileOps,
26
+ extractFileOpsFromMessage,
27
+ type FileOperations,
28
+ SUMMARIZATION_SYSTEM_PROMPT,
29
+ serializeConversation,
30
+ upsertFileOperations,
31
+ } from "./utils";
32
+
33
+ // ============================================================================
34
+ // Types
35
+ // ============================================================================
36
+
37
+ export interface BranchSummaryResult {
38
+ summary?: string;
39
+ readFiles?: string[];
40
+ modifiedFiles?: string[];
41
+ aborted?: boolean;
42
+ error?: string;
43
+ }
44
+
45
+ /** Details stored in BranchSummaryEntry.details for file tracking */
46
+ export interface BranchSummaryDetails {
47
+ readFiles: string[];
48
+ modifiedFiles: string[];
49
+ }
50
+
51
+ export type { FileOperations } from "./utils";
52
+
53
+ export interface BranchPreparation {
54
+ /** Messages extracted for summarization, in chronological order */
55
+ messages: AgentMessage[];
56
+ /** File operations extracted from tool calls */
57
+ fileOps: FileOperations;
58
+ /** Total estimated tokens in messages */
59
+ totalTokens: number;
60
+ }
61
+
62
+ export interface CollectEntriesResult {
63
+ /** Entries to summarize, in chronological order */
64
+ entries: SessionEntry[];
65
+ /** Common ancestor between old and new position, if any */
66
+ commonAncestorId: string | null;
67
+ }
68
+
69
+ export interface GenerateBranchSummaryOptions {
70
+ /** Model to use for summarization */
71
+ model: Model;
72
+ /** API key for the model */
73
+ apiKey: string;
74
+ /** Abort signal for cancellation */
75
+ signal: AbortSignal;
76
+ /** Optional custom instructions for summarization */
77
+ customInstructions?: string;
78
+ /** Tokens reserved for prompt + LLM response (default 16384) */
79
+ reserveTokens?: number;
80
+ /** Optional metadata forwarded to the underlying API request (e.g. user_id for session attribution). */
81
+ metadata?: Record<string, unknown>;
82
+ /** Convert app-specific messages before serializing the branch summary prompt. */
83
+ convertToLlm?: ConvertToLlm;
84
+ /**
85
+ * Optional telemetry handle. When provided, the branch summary LLM call is
86
+ * wrapped in an OTEL chat span tagged with `pi.gen_ai.oneshot.kind = "branch_summary"`.
87
+ */
88
+ telemetry?: AgentTelemetry;
89
+ }
90
+
91
+ // ============================================================================
92
+ // Entry Collection
93
+ // ============================================================================
94
+
95
+ /**
96
+ * Collect entries that should be summarized when navigating from one position to another.
97
+ *
98
+ * Walks from oldLeafId back to the common ancestor with targetId, collecting entries
99
+ * along the way. Does NOT stop at compaction boundaries - those are included and their
100
+ * summaries become context.
101
+ *
102
+ * @param session - Session manager (read-only access)
103
+ * @param oldLeafId - Current position (where we're navigating from)
104
+ * @param targetId - Target position (where we're navigating to)
105
+ * @returns Entries to summarize and the common ancestor
106
+ */
107
+ export function collectEntriesForBranchSummary(
108
+ session: ReadonlySessionManager,
109
+ oldLeafId: string | null,
110
+ targetId: string,
111
+ ): CollectEntriesResult {
112
+ // If no old position, nothing to summarize
113
+ if (!oldLeafId) {
114
+ return { entries: [], commonAncestorId: null };
115
+ }
116
+
117
+ // Find common ancestor (deepest node that's on both paths)
118
+ const oldPath = new Set(session.getBranch(oldLeafId).map(e => e.id));
119
+ const targetPath = session.getBranch(targetId);
120
+
121
+ // targetPath is root-first, so iterate backwards to find deepest common ancestor
122
+ let commonAncestorId: string | null = null;
123
+ for (let i = targetPath.length - 1; i >= 0; i--) {
124
+ if (oldPath.has(targetPath[i].id)) {
125
+ commonAncestorId = targetPath[i].id;
126
+ break;
127
+ }
128
+ }
129
+
130
+ // Collect entries from old leaf back to common ancestor
131
+ const entries: SessionEntry[] = [];
132
+ let current: string | null = oldLeafId;
133
+
134
+ while (current && current !== commonAncestorId) {
135
+ const entry = session.getEntry(current);
136
+ if (!entry) break;
137
+ entries.push(entry);
138
+ current = entry.parentId;
139
+ }
140
+
141
+ // Reverse to get chronological order
142
+ entries.reverse();
143
+
144
+ return { entries, commonAncestorId };
145
+ }
146
+
147
+ // ============================================================================
148
+ // Entry to Message Conversion
149
+ // ============================================================================
150
+
151
+ /**
152
+ * Extract AgentMessage from a session entry.
153
+ * Similar to getMessageFromEntry in compaction.ts but also handles compaction entries.
154
+ */
155
+ function getMessageFromEntry(entry: SessionEntry): AgentMessage | undefined {
156
+ switch (entry.type) {
157
+ case "message":
158
+ // Skip tool results - context is in assistant's tool call
159
+ if (entry.message.role === "toolResult") return undefined;
160
+ return entry.message;
161
+
162
+ case "custom_message":
163
+ return createCustomMessage(
164
+ entry.customType,
165
+ entry.content,
166
+ entry.display,
167
+ entry.details,
168
+ entry.timestamp,
169
+ entry.attribution,
170
+ );
171
+
172
+ case "branch_summary":
173
+ return createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);
174
+
175
+ case "compaction":
176
+ return createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp, entry.shortSummary);
177
+
178
+ // These don't contribute to conversation content
179
+ case "thinking_level_change":
180
+ case "model_change":
181
+ case "custom":
182
+ case "label":
183
+ case "service_tier_change":
184
+ case "ttsr_injection":
185
+ case "mcp_tool_selection":
186
+ case "session_init":
187
+ case "mode_change":
188
+ return undefined;
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Prepare entries for summarization with token budget.
194
+ *
195
+ * Walks entries from NEWEST to OLDEST, adding messages until we hit the token budget.
196
+ * This ensures we keep the most recent context when the branch is too long.
197
+ *
198
+ * Also collects file operations from:
199
+ * - Tool calls in assistant messages
200
+ * - Existing branch_summary entries' details (for cumulative tracking)
201
+ *
202
+ * @param entries - Entries in chronological order
203
+ * @param tokenBudget - Maximum tokens to include (0 = no limit)
204
+ */
205
+ export function prepareBranchEntries(entries: SessionEntry[], tokenBudget: number = 0): BranchPreparation {
206
+ const messages: AgentMessage[] = [];
207
+ const fileOps = createFileOps();
208
+ let totalTokens = 0;
209
+
210
+ // First pass: collect file ops from ALL entries (even if they don't fit in token budget)
211
+ // This ensures we capture cumulative file tracking from nested branch summaries
212
+ // Only extract from pi-generated summaries (fromExtension !== true), not extension-generated ones
213
+ for (const entry of entries) {
214
+ if (entry.type === "branch_summary" && !entry.fromExtension && entry.details) {
215
+ const details = entry.details as BranchSummaryDetails;
216
+ if (Array.isArray(details.readFiles)) {
217
+ for (const f of details.readFiles) fileOps.read.add(f);
218
+ }
219
+ if (Array.isArray(details.modifiedFiles)) {
220
+ // Modified files go into both edited and written for proper deduplication
221
+ for (const f of details.modifiedFiles) {
222
+ fileOps.edited.add(f);
223
+ }
224
+ }
225
+ }
226
+ }
227
+
228
+ // Second pass: walk from newest to oldest, adding messages until token budget
229
+ for (let i = entries.length - 1; i >= 0; i--) {
230
+ const entry = entries[i];
231
+ const message = getMessageFromEntry(entry);
232
+ if (!message) continue;
233
+
234
+ // Extract file ops from assistant messages (tool calls)
235
+ extractFileOpsFromMessage(message, fileOps);
236
+
237
+ const tokens = estimateTokens(message);
238
+
239
+ // Check budget before adding
240
+ if (tokenBudget > 0 && totalTokens + tokens > tokenBudget) {
241
+ // If this is a summary entry, try to fit it anyway as it's important context
242
+ if (entry.type === "compaction" || entry.type === "branch_summary") {
243
+ if (totalTokens < tokenBudget * 0.9) {
244
+ messages.unshift(message);
245
+ totalTokens += tokens;
246
+ }
247
+ }
248
+ // Stop - we've hit the budget
249
+ break;
250
+ }
251
+
252
+ messages.unshift(message);
253
+ totalTokens += tokens;
254
+ }
255
+
256
+ return { messages, fileOps, totalTokens };
257
+ }
258
+
259
+ // ============================================================================
260
+ // Summary Generation
261
+ // ============================================================================
262
+
263
+ const BRANCH_SUMMARY_PREAMBLE = prompt.render(branchSummaryPreamble);
264
+
265
+ const BRANCH_SUMMARY_PROMPT = prompt.render(branchSummaryPrompt);
266
+
267
+ /**
268
+ * Generate a summary of abandoned branch entries.
269
+ *
270
+ * @param entries - Session entries to summarize (chronological order)
271
+ * @param options - Generation options
272
+ */
273
+ export async function generateBranchSummary(
274
+ entries: SessionEntry[],
275
+ options: GenerateBranchSummaryOptions,
276
+ ): Promise<BranchSummaryResult> {
277
+ const { model, apiKey, signal, customInstructions, reserveTokens = 16384, metadata } = options;
278
+
279
+ // Token budget = context window minus reserved space for prompt + response
280
+ const contextWindow = model.contextWindow || 128000;
281
+ const tokenBudget = contextWindow - reserveTokens;
282
+
283
+ const { messages, fileOps } = prepareBranchEntries(entries, tokenBudget);
284
+
285
+ if (messages.length === 0) {
286
+ return { summary: "No content to summarize" };
287
+ }
288
+
289
+ // Transform to LLM-compatible messages, then serialize to text
290
+ // Serialization prevents the model from treating it as a conversation to continue
291
+ const llmMessages = (options.convertToLlm ?? convertToLlm)(messages);
292
+ const conversationText = serializeConversation(llmMessages);
293
+
294
+ // Build prompt
295
+ const instructions = customInstructions || BRANCH_SUMMARY_PROMPT;
296
+ const promptText = `<conversation>\n${conversationText}\n</conversation>\n\n${instructions}`;
297
+
298
+ const summarizationMessages = [
299
+ {
300
+ role: "user" as const,
301
+ content: [{ type: "text" as const, text: promptText }],
302
+ timestamp: Date.now(),
303
+ },
304
+ ];
305
+
306
+ // Call LLM for summarization
307
+ const response = await instrumentedCompleteSimple(
308
+ model,
309
+ { systemPrompt: [SUMMARIZATION_SYSTEM_PROMPT], messages: summarizationMessages },
310
+ { apiKey, signal, maxTokens: 2048, metadata },
311
+ { telemetry: options.telemetry, oneshotKind: "branch_summary" },
312
+ );
313
+
314
+ // Check if aborted or errored
315
+ if (response.stopReason === "aborted") {
316
+ return { aborted: true };
317
+ }
318
+ if (response.stopReason === "error") {
319
+ return { error: response.errorMessage || "Summarization failed" };
320
+ }
321
+
322
+ let summary = response.content
323
+ .filter((c): c is { type: "text"; text: string } => c.type === "text")
324
+ .map(c => c.text)
325
+ .join("\n");
326
+
327
+ // Prepend preamble to provide context about the branch summary
328
+ summary = BRANCH_SUMMARY_PREAMBLE + summary;
329
+
330
+ // Compute file lists and append to summary
331
+ const { readFiles, modifiedFiles } = computeFileLists(fileOps);
332
+ summary = upsertFileOperations(summary, readFiles, modifiedFiles);
333
+
334
+ return {
335
+ summary: summary || "No summary generated",
336
+ readFiles,
337
+ modifiedFiles,
338
+ };
339
+ }