( taskId: string, agentId: AgentId, cacheSafeParams: CacheSafeParams, setAppState: TaskContext['setAppState'], )
| 44 | } |
| 45 | |
| 46 | export function startAgentSummarization( |
| 47 | taskId: string, |
| 48 | agentId: AgentId, |
| 49 | cacheSafeParams: CacheSafeParams, |
| 50 | setAppState: TaskContext['setAppState'], |
| 51 | ): { stop: () => void } { |
| 52 | // Drop forkContextMessages from the closure — runSummary rebuilds it each |
| 53 | // tick from getAgentTranscript(). Without this, the original fork messages |
| 54 | // (passed from AgentTool.tsx) are pinned for the lifetime of the timer. |
| 55 | const { forkContextMessages: _drop, ...baseParams } = cacheSafeParams |
| 56 | let summaryAbortController: AbortController | null = null |
| 57 | let timeoutId: ReturnType<typeof setTimeout> | null = null |
| 58 | let stopped = false |
| 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 |
no test coverage detected