()
| 59 | let previousSummary: string | null = null |
| 60 | |
| 61 | async function runSummary(): Promise<void> { |
| 62 | if (stopped) return |
| 63 | |
| 64 | logForDebugging(`[AgentSummary] Timer fired for agent ${agentId}`) |
| 65 | |
| 66 | try { |
| 67 | // Read current messages from transcript |
| 68 | const transcript = await getAgentTranscript(agentId) |
| 69 | if (!transcript || transcript.messages.length < 3) { |
| 70 | // Not enough context yet — finally block will schedule next attempt |
| 71 | logForDebugging( |
| 72 | `[AgentSummary] Skipping summary for ${taskId}: not enough messages (${transcript?.messages.length ?? 0})`, |
| 73 | ) |
| 74 | return |
| 75 | } |
| 76 | |
| 77 | // Filter to clean message state |
| 78 | const cleanMessages = filterIncompleteToolCalls(transcript.messages) |
| 79 | |
| 80 | // Build fork params with current messages |
| 81 | const forkParams: CacheSafeParams = { |
| 82 | ...baseParams, |
| 83 | forkContextMessages: cleanMessages, |
| 84 | } |
| 85 | |
| 86 | logForDebugging( |
| 87 | `[AgentSummary] Forking for summary, ${cleanMessages.length} messages in context`, |
| 88 | ) |
| 89 | |
| 90 | // Create abort controller for this summary |
| 91 | summaryAbortController = new AbortController() |
| 92 | |
| 93 | // Deny tools via callback, NOT by passing tools:[] - that busts cache |
| 94 | const canUseTool = async () => ({ |
| 95 | behavior: 'deny' as const, |
| 96 | message: 'No tools needed for summary', |
| 97 | decisionReason: { type: 'other' as const, reason: 'summary only' }, |
| 98 | }) |
| 99 | |
| 100 | // DO NOT set maxOutputTokens here. The fork piggybacks on the main |
| 101 | // thread's prompt cache by sending identical cache-key params (system, |
| 102 | // tools, model, messages prefix, thinking config). Setting maxOutputTokens |
| 103 | // would clamp budget_tokens, creating a thinking config mismatch that |
| 104 | // invalidates the cache. |
| 105 | // |
| 106 | // ContentReplacementState is cloned by default in createSubagentContext |
| 107 | // from forkParams.toolUseContext (the subagent's LIVE state captured at |
| 108 | // onCacheSafeParams time). No explicit override needed. |
| 109 | const result = await runForkedAgent({ |
| 110 | promptMessages: [ |
| 111 | createUserMessage({ content: buildSummaryPrompt(previousSummary) }), |
| 112 | ], |
| 113 | cacheSafeParams: forkParams, |
| 114 | canUseTool, |
| 115 | querySource: 'agent_summary', |
| 116 | forkLabel: 'agent_summary', |
| 117 | overrides: { abortController: summaryAbortController }, |
| 118 | skipTranscript: true, |
nothing calls this directly
no test coverage detected