@vib-rato/agent-core 0.16.0

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 (69) hide show
  1. package/CHANGELOG.md +852 -0
  2. package/README.md +493 -0
  3. package/dist/types/agent-loop.d.ts +229 -0
  4. package/dist/types/agent.d.ts +533 -0
  5. package/dist/types/append-only-context.d.ts +141 -0
  6. package/dist/types/attempt-scope.d.ts +84 -0
  7. package/dist/types/compaction/adaptive.d.ts +31 -0
  8. package/dist/types/compaction/branch-summarization.d.ts +103 -0
  9. package/dist/types/compaction/compaction.d.ts +330 -0
  10. package/dist/types/compaction/entries.d.ts +124 -0
  11. package/dist/types/compaction/errors.d.ts +26 -0
  12. package/dist/types/compaction/index.d.ts +12 -0
  13. package/dist/types/compaction/messages.d.ts +61 -0
  14. package/dist/types/compaction/openai.d.ts +65 -0
  15. package/dist/types/compaction/pruning.d.ts +130 -0
  16. package/dist/types/compaction/utils.d.ts +32 -0
  17. package/dist/types/compaction.d.ts +1 -0
  18. package/dist/types/harmony-leak.d.ts +100 -0
  19. package/dist/types/heap-eviction-retainers.test.d.ts +1 -0
  20. package/dist/types/image-placeholder-guard.d.ts +4 -0
  21. package/dist/types/index.d.ts +13 -0
  22. package/dist/types/proxy.d.ts +95 -0
  23. package/dist/types/run-collector.d.ts +223 -0
  24. package/dist/types/run-resource-ledger.d.ts +2 -0
  25. package/dist/types/telemetry.d.ts +605 -0
  26. package/dist/types/thinking.d.ts +18 -0
  27. package/dist/types/tool-dispatch-identity.d.ts +27 -0
  28. package/dist/types/types.d.ts +790 -0
  29. package/package.json +72 -0
  30. package/src/agent-loop.ts +5632 -0
  31. package/src/agent.ts +2437 -0
  32. package/src/append-only-context.ts +496 -0
  33. package/src/attempt-scope.ts +195 -0
  34. package/src/compaction/adaptive.ts +92 -0
  35. package/src/compaction/branch-summarization.ts +358 -0
  36. package/src/compaction/compaction.ts +1569 -0
  37. package/src/compaction/entries.ts +158 -0
  38. package/src/compaction/errors.ts +31 -0
  39. package/src/compaction/index.ts +13 -0
  40. package/src/compaction/messages.ts +212 -0
  41. package/src/compaction/openai.ts +580 -0
  42. package/src/compaction/prompts/auto-handoff-threshold-focus.md +1 -0
  43. package/src/compaction/prompts/branch-summary-context.md +5 -0
  44. package/src/compaction/prompts/branch-summary-preamble.md +2 -0
  45. package/src/compaction/prompts/branch-summary.md +30 -0
  46. package/src/compaction/prompts/compaction-short-summary.md +9 -0
  47. package/src/compaction/prompts/compaction-summary-context.md +5 -0
  48. package/src/compaction/prompts/compaction-summary.md +38 -0
  49. package/src/compaction/prompts/compaction-turn-prefix.md +17 -0
  50. package/src/compaction/prompts/compaction-update-summary.md +45 -0
  51. package/src/compaction/prompts/file-operations.md +10 -0
  52. package/src/compaction/prompts/handoff-document.md +56 -0
  53. package/src/compaction/prompts/summarization-system.md +3 -0
  54. package/src/compaction/pruning.ts +1026 -0
  55. package/src/compaction/utils.ts +189 -0
  56. package/src/compaction.ts +1 -0
  57. package/src/harmony-leak.ts +457 -0
  58. package/src/heap-eviction-retainers.test.ts +293 -0
  59. package/src/image-placeholder-guard.ts +20 -0
  60. package/src/index.ts +23 -0
  61. package/src/prompts/escaped-nonascii-recovery.md +3 -0
  62. package/src/prompts/repeated-tool-failure-recovery.md +1 -0
  63. package/src/proxy.ts +408 -0
  64. package/src/run-collector.ts +728 -0
  65. package/src/run-resource-ledger.ts +345 -0
  66. package/src/telemetry.ts +2161 -0
  67. package/src/thinking.ts +20 -0
  68. package/src/tool-dispatch-identity.ts +87 -0
  69. package/src/types.ts +882 -0
package/src/agent.ts ADDED
@@ -0,0 +1,2437 @@
1
+ /** Agent class that uses the agent-loop directly.
2
+ * No transport abstraction - calls streamSimple via the loop.
3
+ */
4
+ import {
5
+ type AssistantMessage,
6
+ type AssistantMessageEvent,
7
+ type CursorExecHandlers,
8
+ type CursorToolResultHandler,
9
+ type Effort,
10
+ getBundledModel,
11
+ type ImageContent,
12
+ type Message,
13
+ type Model,
14
+ type ProviderSessionState,
15
+ type ServiceTier,
16
+ type SimpleStreamOptions,
17
+ streamSimple,
18
+ type TextContent,
19
+ type ThinkingBudgets,
20
+ type ToolChoice,
21
+ type ToolResultMessage,
22
+ type UserMessage,
23
+ } from "@vib-rato/ai";
24
+ import {
25
+ CURSOR_COMPOSER_BASH_POLICY_RECOVERY_PROMPT,
26
+ isCurrentComposerBashPolicyBlockedError,
27
+ } from "@vib-rato/ai/providers/composer-discipline";
28
+ import { extractHttpStatusFromError } from "@vib-rato/utils";
29
+ import { agentLoop, agentLoopContinue, managedLocalErrorDiagnostic } from "./agent-loop";
30
+ import type { AppendOnlyContextManager } from "./append-only-context";
31
+ import type { AttemptRunHandle, AttemptScope } from "./attempt-scope";
32
+ import { createAttemptScopeAuthority } from "./attempt-scope";
33
+ import type { HarmonyAuditEvent } from "./harmony-leak";
34
+ import { assertImagePlaceholdersHavePayload } from "./image-placeholder-guard";
35
+ import { createRunResourceLedger } from "./run-resource-ledger";
36
+ import type {
37
+ AgentContext,
38
+ AgentEvent,
39
+ AgentLoopConfig,
40
+ AgentMessage,
41
+ AgentMetadataResolverContext,
42
+ AgentState,
43
+ AgentTool,
44
+ AgentToolContext,
45
+ ManagedAttemptContinuation,
46
+ ManagedAttemptContinuationOwnership,
47
+ ManagedAttemptDecision,
48
+ ManagedAttemptOutcome,
49
+ ManagedLogicalRunId,
50
+ RunCancellationDomain,
51
+ RunCancellationDomainBridge,
52
+ RunResourceLedger,
53
+ RunTerminalRequest,
54
+ StreamFn,
55
+ ToolCallContext,
56
+ } from "./types";
57
+ import { setAgentTerminalOwnerContext } from "./types";
58
+
59
+ /**
60
+ * Closed runtime allowlist of failure-classifier codes. The public diagnostic
61
+ * contract promises a STABLE runtime classifier independent of provider-specific
62
+ * detail, so an unknown or provider-supplied code maps to the nearest runtime
63
+ * class instead of being forwarded verbatim (exact-head review P1/P2).
64
+ */
65
+ const RUNTIME_FAILURE_CODES = new Set([
66
+ "agent_failed",
67
+ "aborted",
68
+ "local_snapshot_failure",
69
+ "provider_down",
70
+ "provider_unavailable",
71
+ "provider_rejected",
72
+ "provider_http_402",
73
+ "provider_http_429",
74
+ "upstream_stream_interrupted",
75
+ "argument_validation",
76
+ "execution",
77
+ "local_buffer_overflow",
78
+ "escaped_arguments_discarded",
79
+ "prompt_failed",
80
+ "prompt_deadline_exceeded",
81
+ "skill_runtime",
82
+ "io_error",
83
+ ]);
84
+
85
+ /** Provider-safe classifier subset: generic transport/validation classes a
86
+ * provider error may legitimately carry. Lifecycle classifiers ("aborted",
87
+ * "prompt_deadline_exceeded", local staging kinds) are runtime-owned and can
88
+ * never be asserted by a provider-supplied string (exact-head review P1). */
89
+ const PROVIDER_ACCEPTABLE_FAILURE_CODES = new Set([
90
+ "provider_down",
91
+ "provider_unavailable",
92
+ "upstream_stream_interrupted",
93
+ "argument_validation",
94
+ "execution",
95
+ ]);
96
+
97
+ function sanitizeAgentFailure(error: unknown, runtimeClassifiedCode?: string): { code: string; message: string } {
98
+ let code = "agent_failed";
99
+ try {
100
+ if (runtimeClassifiedCode !== undefined) {
101
+ // Runtime-authenticated classification: only the runtime itself may
102
+ // assert lifecycle classifiers; untrusted provider strings map to the
103
+ // generic failure class below.
104
+ if (RUNTIME_FAILURE_CODES.has(runtimeClassifiedCode)) code = runtimeClassifiedCode;
105
+ } else {
106
+ const candidate = error as { code?: unknown } | undefined;
107
+ if (
108
+ typeof candidate?.code === "string" &&
109
+ candidate.code.length <= 64 &&
110
+ PROVIDER_ACCEPTABLE_FAILURE_CODES.has(candidate.code)
111
+ )
112
+ code = candidate.code;
113
+ }
114
+ } catch {
115
+ // Untrusted provider errors may expose throwing accessors.
116
+ }
117
+ return { code, message: "Agent run failed." };
118
+ }
119
+
120
+ /** Guarded HTTP-status extraction for untrusted provider errors: a throwing
121
+ * getter must never escape the failure handler and suppress terminalization
122
+ * (exact-head review P1). */
123
+ function safeErrorStatus(error: unknown): number | undefined {
124
+ try {
125
+ return (
126
+ extractHttpStatusFromError({ status: (error as { errorStatus?: unknown } | undefined)?.errorStatus }) ??
127
+ extractHttpStatusFromError(error) ??
128
+ extractHttpStatusFromError((error as { transportFailure?: unknown } | undefined)?.transportFailure)
129
+ );
130
+ } catch {
131
+ return undefined;
132
+ }
133
+ }
134
+
135
+ function providerFailureCode(error: unknown): string | undefined {
136
+ const status = safeErrorStatus(error);
137
+ if (status === undefined) return undefined;
138
+ if (status === 402 || status === 429) return `provider_http_${status}`;
139
+ return "provider_rejected";
140
+ }
141
+
142
+ function assertUserImagePlaceholdersHavePayload(messages: readonly AgentMessage[]): void {
143
+ for (const message of messages) {
144
+ if (!("role" in message) || message.role !== "user") continue;
145
+ const content = message.content;
146
+ if (typeof content === "string") {
147
+ assertImagePlaceholdersHavePayload(content, undefined);
148
+ continue;
149
+ }
150
+ if (!Array.isArray(content)) continue;
151
+ const text = content
152
+ .filter(part => part.type === "text")
153
+ .map(part => part.text)
154
+ .join("\n");
155
+ assertImagePlaceholdersHavePayload(text, content);
156
+ }
157
+ }
158
+
159
+ const CURSOR_NATIVE_REPOSITORY_RECOVERY_TOOL_NAMES = new Set(["read", "grep", "search", "find", "write", "delete"]);
160
+
161
+ function isCursorComposerBashPolicyBlockedResult(message: ToolResultMessage): boolean {
162
+ return (
163
+ message.isError &&
164
+ message.toolName === "bash" &&
165
+ message.content.some(content => content.type === "text" && isCurrentComposerBashPolicyBlockedError(content.text))
166
+ );
167
+ }
168
+
169
+ function isSuccessfulCursorNativeRepositoryToolResult(message: ToolResultMessage): boolean {
170
+ return message.isError !== true && CURSOR_NATIVE_REPOSITORY_RECOVERY_TOOL_NAMES.has(message.toolName);
171
+ }
172
+
173
+ /**
174
+ * Whether persisted history ends at a point where a new model turn can resume.
175
+ * Assistant-ended histories require an in-memory queued message and are handled
176
+ * separately by `Agent.continue()`.
177
+ */
178
+ export function canContinuePersistedHistory(messages: readonly AgentMessage[]): boolean {
179
+ const lastMessage = messages.at(-1);
180
+ return lastMessage !== undefined && lastMessage.role !== "assistant";
181
+ }
182
+
183
+ /**
184
+ * Default convertToLlm: Keep only LLM-compatible messages, convert attachments.
185
+ */
186
+ function defaultConvertToLlm(messages: AgentMessage[]): Message[] {
187
+ return messages.filter((m): m is Message => m.role === "user" || m.role === "assistant" || m.role === "toolResult");
188
+ }
189
+
190
+ function refreshToolChoiceForActiveTools(
191
+ toolChoice: ToolChoice | undefined,
192
+ tools: AgentContext["tools"] = [],
193
+ ): ToolChoice | undefined {
194
+ if (!toolChoice || typeof toolChoice === "string") {
195
+ return toolChoice;
196
+ }
197
+
198
+ const toolName =
199
+ toolChoice.type === "tool"
200
+ ? toolChoice.name
201
+ : "function" in toolChoice
202
+ ? toolChoice.function.name
203
+ : toolChoice.name;
204
+
205
+ return tools.some(tool => tool.name === toolName) ? toolChoice : undefined;
206
+ }
207
+
208
+ export class ManagedCursorInvariantError extends Error {
209
+ constructor(message: string = "Managed Cursor attempt received a provider-side tool result") {
210
+ super(message);
211
+ this.name = "ManagedCursorInvariantError";
212
+ }
213
+ }
214
+
215
+ export class AgentBusyError extends Error {
216
+ constructor(
217
+ message: string = "Agent is already processing. Use steer() or followUp() to queue messages, or wait for completion.",
218
+ ) {
219
+ super(message);
220
+ this.name = "AgentBusyError";
221
+ }
222
+ }
223
+ export interface AgentOptions {
224
+ initialState?: Partial<AgentState>;
225
+
226
+ /**
227
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
228
+ * Default filters to user/assistant/toolResult and converts attachments.
229
+ */
230
+ convertToLlm?: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
231
+
232
+ /**
233
+ * Optional transform applied to context before convertToLlm.
234
+ * Use for context pruning, injecting external context, etc.
235
+ */
236
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal, scope?: AttemptScope) => Promise<AgentMessage[]>;
237
+
238
+ /**
239
+ * Steering mode: "all" = send all steering messages at once, "one-at-a-time" = one per turn
240
+ */
241
+ steeringMode?: "all" | "one-at-a-time";
242
+
243
+ /**
244
+ * Follow-up mode: "all" = send all follow-up messages at once, "one-at-a-time" = one per turn
245
+ */
246
+ followUpMode?: "all" | "one-at-a-time";
247
+
248
+ /**
249
+ * When to interrupt tool execution for steering messages.
250
+ * - "immediate": check after each tool call (default)
251
+ * - "wait": defer steering until the current turn completes
252
+ */
253
+ interruptMode?: "immediate" | "wait";
254
+ /** Cooperative pause checkpoint passed through to AgentLoopConfig.shouldPause. */
255
+ shouldPause?: AgentLoopConfig["shouldPause"];
256
+
257
+ /**
258
+ * API format for Kimi Code provider: "openai" or "anthropic" (default: "anthropic")
259
+ */
260
+ kimiApiFormat?: "openai" | "anthropic";
261
+
262
+ /** Hint that websocket transport should be preferred when supported by the provider implementation. */
263
+ preferWebsockets?: boolean;
264
+
265
+ /**
266
+ * Custom stream function (for proxy backends, etc.). Default uses streamSimple.
267
+ */
268
+ streamFn?: StreamFn;
269
+
270
+ /**
271
+ * Optional session identifier forwarded to LLM providers.
272
+ * Used by providers that support session-based caching (e.g., OpenAI code provider).
273
+ */
274
+ sessionId?: string;
275
+ /** Provider-facing cache/session affinity identifier. */
276
+ providerSessionId?: string;
277
+ /**
278
+ * Shared provider state map for session-scoped transport/session caches.
279
+ */
280
+ providerSessionState?: Map<string, ProviderSessionState>;
281
+
282
+ /**
283
+ * Resolves an API key dynamically for each LLM call.
284
+ * Useful for expiring tokens (e.g., GitHub Copilot OAuth).
285
+ */
286
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
287
+ getAuthCredentialType?: (provider: string) => "api_key" | "oauth" | undefined;
288
+
289
+ /**
290
+ * Inspect or replace provider payloads before they are sent.
291
+ */
292
+ onPayload?: SimpleStreamOptions["onPayload"];
293
+ /**
294
+ * Inspect provider response metadata after headers arrive and before streaming body consumption.
295
+ */
296
+ onResponse?: SimpleStreamOptions["onResponse"];
297
+ /**
298
+ * Inspect raw Server-Sent Events from HTTP streaming providers.
299
+ */
300
+ onSseEvent?: SimpleStreamOptions["onSseEvent"];
301
+ /**
302
+ * Inspect assistant streaming events before they are emitted to subscribers.
303
+ * Use this when abort decisions must happen before buffered events continue flowing.
304
+ */
305
+ onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
306
+ /** Called for non-content tool-choice incapability stream events. */
307
+ onToolChoiceIncapability?: AgentLoopConfig["onToolChoiceIncapability"];
308
+
309
+ /**
310
+ * Called when GPT-5 Harmony protocol leakage is detected and mitigated.
311
+ */
312
+ onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
313
+ /**
314
+ * Custom token budgets for thinking levels (token-based providers only).
315
+ */
316
+ thinkingBudgets?: ThinkingBudgets;
317
+
318
+ /**
319
+ * Sampling temperature for LLM calls. `undefined` uses provider default.
320
+ */
321
+ temperature?: number;
322
+
323
+ /** Additional sampling controls for providers that support them. */
324
+ topP?: number;
325
+ topK?: number;
326
+ minP?: number;
327
+ presencePenalty?: number;
328
+ repetitionPenalty?: number;
329
+ serviceTier?: ServiceTier;
330
+ /**
331
+ * If true, request that the underlying provider omit reasoning/thinking summaries
332
+ * from the response. The model still reasons internally; only the human-readable
333
+ * summary stream is suppressed. Useful when the UI hides thinking blocks anyway.
334
+ */
335
+ hideThinkingSummary?: boolean;
336
+
337
+ /**
338
+ * Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
339
+ * If the server's requested delay exceeds this value, the request fails immediately,
340
+ * allowing higher-level retry logic to handle it with user visibility.
341
+ * Default: 60000 (60 seconds). Set to 0 to disable the cap.
342
+ */
343
+ maxRetryDelayMs?: number;
344
+ /** Provider request retry budget. Counts retries, not the initial attempt. */
345
+ requestMaxRetries?: number;
346
+ /** Provider stream replay retry budget. Counts retries, not the initial attempt. */
347
+ streamMaxRetries?: number;
348
+ /** Explicit first-event stream watchdog override in milliseconds. Set to 0 to disable. */
349
+ streamFirstEventTimeoutMs?: number;
350
+
351
+ /**
352
+ * Provides tool execution context, resolved per tool call.
353
+ * Use for late-bound UI or session state access.
354
+ */
355
+ getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
356
+
357
+ /**
358
+ * Optional transform applied to tool call arguments before execution.
359
+ * Use for deobfuscating secrets or rewriting arguments.
360
+ */
361
+ transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
362
+
363
+ /** Enable intent tracing schema injection/stripping in the harness. */
364
+ intentTracing?: boolean;
365
+ /** Dynamic tool choice override, resolved per LLM call. */
366
+ getToolChoice?: () => ToolChoice | undefined;
367
+
368
+ /**
369
+ * Cursor exec handlers for local tool execution.
370
+ */
371
+ cursorExecHandlers?: CursorExecHandlers;
372
+
373
+ /**
374
+ * Cursor tool result callback for exec tool responses.
375
+ */
376
+ cursorOnToolResult?: CursorToolResultHandler;
377
+
378
+ /**
379
+ * Called after a tool call has been validated and is about to execute.
380
+ * See {@link AgentLoopConfig.beforeToolCall} for full semantics.
381
+ */
382
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
383
+
384
+ /**
385
+ * Called after a tool finishes executing, before `tool_execution_end` and the tool-result
386
+ * message are emitted. See {@link AgentLoopConfig.afterToolCall} for full semantics.
387
+ */
388
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
389
+ /** Invoked with the follow-up messages dequeued for the next turn (reassignable). */
390
+ onFollowUpConsumed?: AgentLoopConfig["onFollowUpConsumed"];
391
+ /** Invoked with the steering messages dequeued mid-run for the current turn (reassignable). */
392
+ onSteeringConsumed?: AgentLoopConfig["onSteeringConsumed"];
393
+
394
+ /**
395
+ * Opt-in OpenTelemetry instrumentation. Passing `{}` enables the loop's
396
+ * GenAI-semantic-convention spans using the global tracer provider. See
397
+ * {@link AgentLoopConfig.telemetry} for the full surface.
398
+ */
399
+ telemetry?: AgentLoopConfig["telemetry"];
400
+ /**
401
+ * Immutable context mode — stabilizes system prompt + tool spec bytes
402
+ * across turns so DeepSeek/Anthropic prefix caches hit at maximum rate.
403
+ */
404
+ appendOnlyContext?: AppendOnlyContextManager;
405
+ }
406
+
407
+ export interface AgentPromptOptions {
408
+ /** One-shot transient recovery instruction sent only to the provider for the next assistant request; never committed to durable history. */
409
+ transientRecoveryMessage?: UserMessage;
410
+ toolChoice?: ToolChoice;
411
+ /** Disable transport replay; fallback accounting is owned by the caller. */
412
+ fallbackManaged?: boolean;
413
+ /** Continue a cooperative maintenance checkpoint under its existing logical run and cancellation domain. */
414
+ maintenanceContinuation?: boolean;
415
+ /** Called synchronously after this invocation claims the agent run, before asynchronous provider work. */
416
+ onRunAccepted?: (handle: AttemptRunHandle, acceptance: { consumedQueuedMessages: readonly AgentMessage[] }) => void;
417
+ /** Called once immediately before every managed upstream request. */
418
+ nextFallbackAttempt?: AgentLoopConfig["nextFallbackAttempt"];
419
+ /** Called after a managed upstream request is accepted and committed. */
420
+ onManagedAttemptAccepted?: AgentLoopConfig["onManagedAttemptAccepted"];
421
+ /** Receives a discarded managed attempt without exposing assistant lifecycle events. */
422
+ onManagedAttemptOutcome?: AgentLoopConfig["onManagedAttemptOutcome"];
423
+ }
424
+
425
+ /** Buffered Cursor tool result with text position at time of call */
426
+ interface CursorToolResultEntry {
427
+ toolResult: ToolResultMessage;
428
+ textLengthAtCall: number;
429
+ }
430
+
431
+ export type AgentQueueSnapshot = {
432
+ steering: AgentMessage[];
433
+ followUp: AgentMessage[];
434
+ };
435
+
436
+ export class Agent {
437
+ #state: AgentState = {
438
+ systemPrompt: [],
439
+ model: getBundledModel("google", "gemini-2.5-flash-lite-preview-06-17"),
440
+ thinkingLevel: undefined,
441
+ tools: [],
442
+ messages: [],
443
+ isStreaming: false,
444
+ streamMessage: null,
445
+ pendingToolCalls: new Set<string>(),
446
+ error: undefined,
447
+ };
448
+ #contextRevision = 0;
449
+ #attemptAuthority = createAttemptScopeAuthority();
450
+ #runHandles = new Map<number | ManagedLogicalRunId, AttemptRunHandle>();
451
+
452
+ #listeners = new Set<(e: AgentEvent) => void>();
453
+ #abortController?: AbortController;
454
+ #convertToLlm: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
455
+ #transformContext?: (
456
+ messages: AgentMessage[],
457
+ signal?: AbortSignal,
458
+ scope?: AttemptScope,
459
+ ) => Promise<AgentMessage[]>;
460
+ #steeringQueue: AgentMessage[] = [];
461
+ #steeringWaiters = new Set<() => void>();
462
+ #followUpQueue: AgentMessage[] = [];
463
+ #followUpForceOneAtATime = new WeakSet<AgentMessage>();
464
+ #steeringMode: "all" | "one-at-a-time";
465
+ #followUpMode: "all" | "one-at-a-time";
466
+ #interruptMode: "immediate" | "wait";
467
+ #sessionId?: string;
468
+ #providerSessionId?: string;
469
+ #metadata?: Record<string, unknown>;
470
+ #metadataResolver?: (context: AgentMetadataResolverContext) => Record<string, unknown> | undefined;
471
+ #providerSessionState?: Map<string, ProviderSessionState>;
472
+ #thinkingBudgets?: ThinkingBudgets;
473
+ #temperature?: number;
474
+ #topP?: number;
475
+ #topK?: number;
476
+ #minP?: number;
477
+ #presencePenalty?: number;
478
+ #repetitionPenalty?: number;
479
+ #serviceTier?: ServiceTier;
480
+ #hideThinkingSummary?: boolean;
481
+ #maxRetryDelayMs?: number;
482
+ #requestMaxRetries?: number;
483
+ #streamMaxRetries?: number;
484
+ #streamFirstEventTimeoutMs?: number;
485
+ #getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
486
+ #cursorExecHandlers?: CursorExecHandlers;
487
+ #cursorOnToolResult?: CursorToolResultHandler;
488
+ #runningPrompt?: Promise<void>;
489
+ #resolveRunningPrompt?: () => void;
490
+ #runSequence = 0;
491
+ #activeRunId?: number;
492
+ #activeResourceRunId?: string;
493
+ #activeResourceCancellationDomain?: RunCancellationDomain;
494
+ #continuationGeneration = 0;
495
+ #kimiApiFormat?: "openai" | "anthropic";
496
+ #preferWebsockets?: boolean;
497
+ #transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
498
+ #intentTracing: boolean;
499
+ #getToolChoice?: () => ToolChoice | undefined;
500
+ #onPayload?: SimpleStreamOptions["onPayload"];
501
+ #onResponse?: SimpleStreamOptions["onResponse"];
502
+ #onSseEvent?: SimpleStreamOptions["onSseEvent"];
503
+ #onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
504
+ #onProvisionalAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
505
+ #onToolChoiceIncapability?: AgentLoopConfig["onToolChoiceIncapability"];
506
+ #onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
507
+ #onBeforeYield?: () => Promise<void> | void;
508
+ #shouldPause?: AgentLoopConfig["shouldPause"];
509
+ /** While set and returning true, steering is neither admitted nor dequeued. */
510
+ #steeringAdmissionFence?: () => boolean;
511
+ #maintainContext?: AgentLoopConfig["maintainContext"];
512
+ #telemetry?: AgentLoopConfig["telemetry"];
513
+ #appendOnlyContext?: AppendOnlyContextManager;
514
+ #mainAttemptScopeObserver?: (scope: AttemptScope) => void;
515
+
516
+ get intentTracing(): boolean {
517
+ return this.#intentTracing;
518
+ }
519
+
520
+ /** Buffered Cursor tool results with text length at time of call (for correct ordering) */
521
+ #cursorToolResultBuffer: CursorToolResultEntry[] = [];
522
+ #terminalizedLogicalRunIds = new Set<ManagedLogicalRunId>();
523
+ #managedLogicalRunOwner?: ManagedLogicalRunId;
524
+ readonly resourceLedger: RunResourceLedger = createRunResourceLedger();
525
+ bindRunCancellationDomainBridge(bridge: RunCancellationDomainBridge, agentSessionClaimKey?: object): void {
526
+ this.resourceLedger.bindCancellationDomainBridge(bridge);
527
+ if (agentSessionClaimKey) this.resourceLedger.bindAgentSessionClaimKey(agentSessionClaimKey);
528
+ }
529
+
530
+ /** Mint a side-attempt scope and its authority unregister function. */
531
+ mintSideAttemptScope(): { scope: AttemptScope; dispose: () => void } {
532
+ return this.#attemptAuthority.mintSide();
533
+ }
534
+
535
+ /** Return the Agent-owned attempt scope authority for session record injection. */
536
+ getAttemptScopeAuthority() {
537
+ return this.#attemptAuthority;
538
+ }
539
+ /**
540
+ * Observe each main-attempt scope synchronously, before any provider or
541
+ * extension-capable lifecycle work can begin.
542
+ */
543
+ setMainAttemptScopeObserver(observer: ((scope: AttemptScope) => void) | undefined): void {
544
+ this.#mainAttemptScopeObserver = observer;
545
+ }
546
+
547
+ #observeMainAttemptScope(scope: AttemptScope): void {
548
+ this.#mainAttemptScopeObserver?.(scope);
549
+ }
550
+
551
+ streamFn: StreamFn;
552
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
553
+ getAuthCredentialType?: (provider: string) => "api_key" | "oauth" | undefined;
554
+ /**
555
+ * Hook invoked after tool arguments are validated and before execution.
556
+ * Reassign at any time to swap the implementation (e.g. on extension reload).
557
+ */
558
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
559
+ /**
560
+ * Hook invoked after tool execution and before `tool_execution_end` / tool-result
561
+ * message emission. Reassign at any time to swap the implementation.
562
+ */
563
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
564
+ /** Invoked with the follow-up messages dequeued for the next turn. Reassign at any time. */
565
+ onFollowUpConsumed?: AgentLoopConfig["onFollowUpConsumed"];
566
+ /** Invoked with the steering messages dequeued mid-run for the current turn. Reassign at any time. */
567
+ onSteeringConsumed?: AgentLoopConfig["onSteeringConsumed"];
568
+
569
+ constructor(opts: AgentOptions = {}) {
570
+ this.#state = { ...this.#state, ...opts.initialState };
571
+ this.#convertToLlm = opts.convertToLlm || defaultConvertToLlm;
572
+ this.#transformContext = opts.transformContext;
573
+ this.#steeringMode = opts.steeringMode || "one-at-a-time";
574
+ this.#followUpMode = opts.followUpMode || "one-at-a-time";
575
+ this.#interruptMode = opts.interruptMode || "immediate";
576
+ this.streamFn = opts.streamFn || streamSimple;
577
+ this.#sessionId = opts.sessionId;
578
+ this.#providerSessionId = opts.providerSessionId;
579
+ this.#providerSessionState = opts.providerSessionState;
580
+ this.#thinkingBudgets = opts.thinkingBudgets;
581
+ this.#temperature = opts.temperature;
582
+ this.#topP = opts.topP;
583
+ this.#topK = opts.topK;
584
+ this.#minP = opts.minP;
585
+ this.#presencePenalty = opts.presencePenalty;
586
+ this.#repetitionPenalty = opts.repetitionPenalty;
587
+ this.#serviceTier = opts.serviceTier;
588
+ this.#hideThinkingSummary = opts.hideThinkingSummary;
589
+ this.#maxRetryDelayMs = opts.maxRetryDelayMs;
590
+ this.#requestMaxRetries = opts.requestMaxRetries;
591
+ this.#streamMaxRetries = opts.streamMaxRetries;
592
+ this.#streamFirstEventTimeoutMs = opts.streamFirstEventTimeoutMs;
593
+ this.getApiKey = opts.getApiKey;
594
+ this.getAuthCredentialType = opts.getAuthCredentialType;
595
+ this.#onPayload = opts.onPayload;
596
+ this.#onResponse = opts.onResponse;
597
+ this.#onSseEvent = opts.onSseEvent;
598
+ this.#getToolContext = opts.getToolContext;
599
+ this.#cursorExecHandlers = opts.cursorExecHandlers;
600
+ this.#cursorOnToolResult = opts.cursorOnToolResult;
601
+ this.#kimiApiFormat = opts.kimiApiFormat;
602
+ this.#preferWebsockets = opts.preferWebsockets;
603
+ this.#transformToolCallArguments = opts.transformToolCallArguments;
604
+ this.#intentTracing = opts.intentTracing === true;
605
+ this.#getToolChoice = opts.getToolChoice;
606
+ this.#onAssistantMessageEvent = opts.onAssistantMessageEvent;
607
+ this.#onToolChoiceIncapability = opts.onToolChoiceIncapability;
608
+ this.#onHarmonyLeak = opts.onHarmonyLeak;
609
+ this.#shouldPause = opts.shouldPause;
610
+ this.beforeToolCall = opts.beforeToolCall;
611
+ this.onFollowUpConsumed = opts.onFollowUpConsumed;
612
+ this.onSteeringConsumed = opts.onSteeringConsumed;
613
+ this.afterToolCall = opts.afterToolCall;
614
+ this.#telemetry = opts.telemetry;
615
+ this.#appendOnlyContext = opts.appendOnlyContext;
616
+ }
617
+
618
+ /**
619
+ * Get the current session ID used for provider caching.
620
+ */
621
+ get sessionId(): string | undefined {
622
+ return this.#sessionId;
623
+ }
624
+
625
+ /**
626
+ * Set the session ID for provider caching.
627
+ * Call this when switching sessions (new session, branch, resume).
628
+ */
629
+ set sessionId(value: string | undefined) {
630
+ this.#sessionId = value;
631
+ }
632
+
633
+ get providerSessionId(): string | undefined {
634
+ return this.#providerSessionId;
635
+ }
636
+
637
+ set providerSessionId(value: string | undefined) {
638
+ this.#providerSessionId = value;
639
+ }
640
+
641
+ /**
642
+ * Whether websocket transport is preferred when the provider implementation
643
+ * supports it. Read by maintenance one-shot calls (compaction, handoff,
644
+ * branch summary) so they forward the same transport preference as live turns.
645
+ */
646
+ get preferWebsockets(): boolean | undefined {
647
+ return this.#preferWebsockets;
648
+ }
649
+
650
+ /**
651
+ * Static metadata forwarded to every API request when no resolver is installed
652
+ * (e.g. `metadata.user_id` for Anthropic session attribution). Setting this
653
+ * clears any installed resolver.
654
+ *
655
+ * For live/provider-aware metadata (e.g. Anthropic OAuth `account_uuid` that
656
+ * must reflect the credential selected per-request), use
657
+ * {@link setMetadataResolver} and read via {@link metadataForProvider}.
658
+ */
659
+ get metadata(): Record<string, unknown> | undefined {
660
+ return this.#metadata;
661
+ }
662
+
663
+ set metadata(value: Record<string, unknown> | undefined) {
664
+ this.#metadata = value;
665
+ this.#metadataResolver = undefined;
666
+ }
667
+
668
+ /**
669
+ * Resolve request metadata for the given provider at call time. When a
670
+ * resolver is installed via {@link setMetadataResolver}, it is invoked with
671
+ * the provider string so the result can be scoped (e.g. `account_uuid` is
672
+ * only included for `"anthropic"` requests). Falls back to the static
673
+ * {@link metadata} value when no resolver is set.
674
+ */
675
+ metadataForProvider(
676
+ provider: string,
677
+ model?: Model,
678
+ transport?: AgentMetadataResolverContext["transport"],
679
+ ): Record<string, unknown> | undefined {
680
+ if (this.#metadataResolver) return this.#metadataResolver({ provider, model, transport });
681
+ return this.#metadata;
682
+ }
683
+
684
+ /**
685
+ * Install a function that resolves request metadata at call time. The
686
+ * resolver receives the target provider string and can gate provider-specific
687
+ * fields (e.g. `account_uuid` only for `"anthropic"`). Invoked per LLM
688
+ * request by `agent-loop` after `getApiKey` selects the session-sticky
689
+ * credential. Pass `undefined` to clear and revert to the static
690
+ * {@link metadata} value.
691
+ */
692
+ setMetadataResolver(
693
+ resolver: ((context: AgentMetadataResolverContext) => Record<string, unknown> | undefined) | undefined,
694
+ ): void {
695
+ this.#metadataResolver = resolver;
696
+ }
697
+
698
+ /**
699
+ * Read the active OpenTelemetry configuration. Returns `undefined` when
700
+ * instrumentation is disabled. Callers spawning child runs (e.g. subagent
701
+ * dispatch) forward this to the child's loop so its spans appear under the
702
+ * parent's active context with the subagent's own identity stamped.
703
+ */
704
+ get telemetry(): AgentLoopConfig["telemetry"] | undefined {
705
+ return this.#telemetry;
706
+ }
707
+
708
+ /**
709
+ * Replace the active OpenTelemetry configuration. Pass `undefined` to
710
+ * disable instrumentation. Applies to the *next* `agentLoop` invocation —
711
+ * in-flight loops keep the configuration they started with.
712
+ */
713
+ setTelemetry(telemetry: AgentLoopConfig["telemetry"] | undefined): void {
714
+ this.#telemetry = telemetry;
715
+ }
716
+
717
+ /**
718
+ * Get provider-scoped mutable session state store.
719
+ */
720
+ get providerSessionState(): Map<string, ProviderSessionState> | undefined {
721
+ return this.#providerSessionState;
722
+ }
723
+
724
+ /**
725
+ * Set provider-scoped mutable session state store.
726
+ */
727
+ set providerSessionState(value: Map<string, ProviderSessionState> | undefined) {
728
+ this.#providerSessionState = value;
729
+ }
730
+
731
+ /**
732
+ * Get the current thinking budgets.
733
+ */
734
+ get thinkingBudgets(): ThinkingBudgets | undefined {
735
+ return this.#thinkingBudgets;
736
+ }
737
+
738
+ /**
739
+ * Set custom thinking budgets for token-based providers.
740
+ */
741
+ set thinkingBudgets(value: ThinkingBudgets | undefined) {
742
+ this.#thinkingBudgets = value;
743
+ }
744
+
745
+ /**
746
+ * Get the current sampling temperature.
747
+ */
748
+ get temperature(): number | undefined {
749
+ return this.#temperature;
750
+ }
751
+
752
+ /**
753
+ * Set sampling temperature for LLM calls. `undefined` uses provider default.
754
+ */
755
+ set temperature(value: number | undefined) {
756
+ this.#temperature = value;
757
+ }
758
+
759
+ get topP(): number | undefined {
760
+ return this.#topP;
761
+ }
762
+
763
+ set topP(value: number | undefined) {
764
+ this.#topP = value;
765
+ }
766
+
767
+ get topK(): number | undefined {
768
+ return this.#topK;
769
+ }
770
+
771
+ set topK(value: number | undefined) {
772
+ this.#topK = value;
773
+ }
774
+
775
+ get minP(): number | undefined {
776
+ return this.#minP;
777
+ }
778
+
779
+ set minP(value: number | undefined) {
780
+ this.#minP = value;
781
+ }
782
+
783
+ get presencePenalty(): number | undefined {
784
+ return this.#presencePenalty;
785
+ }
786
+
787
+ set presencePenalty(value: number | undefined) {
788
+ this.#presencePenalty = value;
789
+ }
790
+
791
+ get repetitionPenalty(): number | undefined {
792
+ return this.#repetitionPenalty;
793
+ }
794
+
795
+ set repetitionPenalty(value: number | undefined) {
796
+ this.#repetitionPenalty = value;
797
+ }
798
+
799
+ get serviceTier(): ServiceTier | undefined {
800
+ return this.#serviceTier;
801
+ }
802
+
803
+ set serviceTier(value: ServiceTier | undefined) {
804
+ this.#serviceTier = value;
805
+ }
806
+
807
+ get hideThinkingSummary(): boolean | undefined {
808
+ return this.#hideThinkingSummary;
809
+ }
810
+
811
+ set hideThinkingSummary(value: boolean | undefined) {
812
+ this.#hideThinkingSummary = value;
813
+ }
814
+
815
+ /**
816
+ * Get the current max retry delay in milliseconds.
817
+ */
818
+ get maxRetryDelayMs(): number | undefined {
819
+ return this.#maxRetryDelayMs;
820
+ }
821
+
822
+ /**
823
+ * Set the maximum delay to wait for server-requested retries.
824
+ * Set to 0 to disable the cap.
825
+ */
826
+ set maxRetryDelayMs(value: number | undefined) {
827
+ this.#maxRetryDelayMs = value;
828
+ }
829
+
830
+ get requestMaxRetries(): number | undefined {
831
+ return this.#requestMaxRetries;
832
+ }
833
+
834
+ set requestMaxRetries(value: number | undefined) {
835
+ this.#requestMaxRetries = value;
836
+ }
837
+
838
+ get streamMaxRetries(): number | undefined {
839
+ return this.#streamMaxRetries;
840
+ }
841
+
842
+ set streamMaxRetries(value: number | undefined) {
843
+ this.#streamMaxRetries = value;
844
+ }
845
+
846
+ get streamFirstEventTimeoutMs(): number | undefined {
847
+ return this.#streamFirstEventTimeoutMs;
848
+ }
849
+
850
+ set streamFirstEventTimeoutMs(value: number | undefined) {
851
+ this.#streamFirstEventTimeoutMs = value;
852
+ }
853
+
854
+ get state(): AgentState {
855
+ return this.#state;
856
+ }
857
+
858
+ get contextRevision(): number {
859
+ return this.#contextRevision;
860
+ }
861
+
862
+ get appendOnlyContext(): AppendOnlyContextManager | undefined {
863
+ return this.#appendOnlyContext;
864
+ }
865
+
866
+ setAppendOnlyContext(manager?: AppendOnlyContextManager): void {
867
+ this.#appendOnlyContext = manager;
868
+ }
869
+
870
+ subscribe(fn: (e: AgentEvent) => void): () => void {
871
+ this.#listeners.add(fn);
872
+ return () => this.#listeners.delete(fn);
873
+ }
874
+
875
+ setProviderResponseInterceptor(fn: SimpleStreamOptions["onResponse"] | undefined): void {
876
+ this.#onResponse = fn;
877
+ }
878
+
879
+ setRawSseEventInterceptor(fn: SimpleStreamOptions["onSseEvent"] | undefined): void {
880
+ this.#onSseEvent = fn;
881
+ }
882
+
883
+ setAssistantMessageEventInterceptor(
884
+ fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined,
885
+ ): void {
886
+ this.#onAssistantMessageEvent = fn;
887
+ }
888
+
889
+ setProvisionalAssistantMessageEventInterceptor(
890
+ fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined,
891
+ ): void {
892
+ this.#onProvisionalAssistantMessageEvent = fn;
893
+ }
894
+
895
+ setOnBeforeYield(fn: (() => Promise<void> | void) | undefined): void {
896
+ this.#onBeforeYield = fn;
897
+ }
898
+
899
+ setShouldPause(fn: AgentLoopConfig["shouldPause"] | undefined): void {
900
+ this.#shouldPause = fn;
901
+ }
902
+
903
+ /** The currently installed cooperative pause checkpoint, if any. */
904
+ get shouldPause(): AgentLoopConfig["shouldPause"] | undefined {
905
+ return this.#shouldPause;
906
+ }
907
+
908
+ /**
909
+ * Fence old-turn steering admission.
910
+ *
911
+ * The loop polls steering UPSTREAM of its pause checkpoint (and again on the
912
+ * immediate-interrupt path), so a cooperative stop alone cannot prevent one
913
+ * more old-turn model call once a steering message has already been dequeued.
914
+ * While the fence returns true the poll yields no messages AND does not
915
+ * dequeue, so the queue survives intact for the next turn.
916
+ */
917
+ setSteeringAdmissionFence(fn: (() => boolean) | undefined): void {
918
+ this.#steeringAdmissionFence = fn;
919
+ }
920
+
921
+ setMaintainContext(fn: AgentLoopConfig["maintainContext"] | undefined): void {
922
+ this.#maintainContext = fn;
923
+ }
924
+
925
+ /**
926
+ * Publish an event produced OUTSIDE the agent loop (a provider that executed the tool
927
+ * itself, a host bridge, a replay).
928
+ *
929
+ * Identity is the PRODUCER's to prove: whoever dispatched the call binds the tool object
930
+ * it actually ran (see `bindDispatchedToolIdentity`) before handing the event here, and
931
+ * that binding is never touched from this side. Re-resolving `event.toolName` against the
932
+ * mutable current tool list would let a mid-run `setTools`, MCP reload, or plain name
933
+ * collision overwrite a proven object with one that never ran — and would invent an
934
+ * identity for replays and host bridges that never executed an AgentTool at all. An
935
+ * unbound external event stays unbound; unproven provenance is `custom`.
936
+ */
937
+ emitExternalEvent(event: AgentEvent) {
938
+ switch (event.type) {
939
+ case "message_start":
940
+ case "message_update":
941
+ this.#state.streamMessage = event.message;
942
+ break;
943
+ case "message_end":
944
+ this.#state.streamMessage = null;
945
+ this.appendMessage(event.message);
946
+ break;
947
+ case "tool_execution_start": {
948
+ const pending = new Set(this.#state.pendingToolCalls);
949
+ pending.add(event.toolCallId);
950
+ this.#state.pendingToolCalls = pending;
951
+ break;
952
+ }
953
+ case "tool_execution_end": {
954
+ const pending = new Set(this.#state.pendingToolCalls);
955
+ pending.delete(event.toolCallId);
956
+ this.#state.pendingToolCalls = pending;
957
+ break;
958
+ }
959
+ }
960
+
961
+ this.#emit(event);
962
+ }
963
+
964
+ createExternalEventEmitterForCurrentRun(): ((event: AgentEvent) => void) | undefined {
965
+ const runId = this.#activeRunId;
966
+ if (runId === undefined) return undefined;
967
+ return (event: AgentEvent) => {
968
+ if (this.#activeRunId !== runId) return;
969
+ this.emitExternalEvent(event);
970
+ };
971
+ }
972
+
973
+ #assertActiveRun(runId: number): void {
974
+ if (this.#activeRunId !== runId) {
975
+ throw new Error("Ignoring Cursor exec callback from an inactive agent run.");
976
+ }
977
+ }
978
+
979
+ #cursorExecHandlersForRun(runId: number): CursorExecHandlers | undefined {
980
+ const source = this.#cursorExecHandlers;
981
+ if (!source) return undefined;
982
+
983
+ const guarded: CursorExecHandlers = {};
984
+ // Bind each handler to `source`: they are methods of a CursorExecHandlers
985
+ // instance that reference private fields via `this`. Extracting them bare
986
+ // (`const read = source.read`) and calling `read(args)` would invoke them with
987
+ // `this === undefined`, throwing "undefined is not an object (this.#optionsForCall)".
988
+ const read = source.read?.bind(source);
989
+ if (read) {
990
+ guarded.read = async args => {
991
+ this.#assertActiveRun(runId);
992
+ const result = await read(args);
993
+ this.#assertActiveRun(runId);
994
+ return result;
995
+ };
996
+ }
997
+ const ls = source.ls?.bind(source);
998
+ if (ls) {
999
+ guarded.ls = async args => {
1000
+ this.#assertActiveRun(runId);
1001
+ const result = await ls(args);
1002
+ this.#assertActiveRun(runId);
1003
+ return result;
1004
+ };
1005
+ }
1006
+ const grep = source.grep?.bind(source);
1007
+ if (grep) {
1008
+ guarded.grep = async args => {
1009
+ this.#assertActiveRun(runId);
1010
+ const result = await grep(args);
1011
+ this.#assertActiveRun(runId);
1012
+ return result;
1013
+ };
1014
+ }
1015
+ const write = source.write?.bind(source);
1016
+ if (write) {
1017
+ guarded.write = async args => {
1018
+ this.#assertActiveRun(runId);
1019
+ const result = await write(args);
1020
+ this.#assertActiveRun(runId);
1021
+ return result;
1022
+ };
1023
+ }
1024
+ const deleteHandler = source.delete?.bind(source);
1025
+ if (deleteHandler) {
1026
+ guarded.delete = async args => {
1027
+ this.#assertActiveRun(runId);
1028
+ const result = await deleteHandler(args);
1029
+ this.#assertActiveRun(runId);
1030
+ return result;
1031
+ };
1032
+ }
1033
+ const shell = source.shell?.bind(source);
1034
+ if (shell) {
1035
+ guarded.shell = async args => {
1036
+ this.#assertActiveRun(runId);
1037
+ const result = await shell(args);
1038
+ this.#assertActiveRun(runId);
1039
+ return result;
1040
+ };
1041
+ }
1042
+ const shellStream = source.shellStream?.bind(source);
1043
+ if (shellStream) {
1044
+ guarded.shellStream = async (args, callbacks) => {
1045
+ this.#assertActiveRun(runId);
1046
+ const result = await shellStream(args, callbacks);
1047
+ this.#assertActiveRun(runId);
1048
+ return result;
1049
+ };
1050
+ }
1051
+ const diagnostics = source.diagnostics?.bind(source);
1052
+ if (diagnostics) {
1053
+ guarded.diagnostics = async args => {
1054
+ this.#assertActiveRun(runId);
1055
+ const result = await diagnostics(args);
1056
+ this.#assertActiveRun(runId);
1057
+ return result;
1058
+ };
1059
+ }
1060
+ const mcp = source.mcp?.bind(source);
1061
+ if (mcp) {
1062
+ guarded.mcp = async call => {
1063
+ this.#assertActiveRun(runId);
1064
+ const result = await mcp(call);
1065
+ this.#assertActiveRun(runId);
1066
+ return result;
1067
+ };
1068
+ }
1069
+ const onToolResult = source.onToolResult;
1070
+ if (onToolResult) {
1071
+ guarded.onToolResult = async message => {
1072
+ this.#assertActiveRun(runId);
1073
+ const result = await onToolResult(message);
1074
+ this.#assertActiveRun(runId);
1075
+ return result;
1076
+ };
1077
+ }
1078
+ return guarded;
1079
+ }
1080
+
1081
+ // State mutators
1082
+ setSystemPrompt(v: string[]) {
1083
+ this.#state.systemPrompt = v;
1084
+ this.#contextRevision++;
1085
+ }
1086
+
1087
+ setModel(m: Model | undefined) {
1088
+ this.#state.model = m;
1089
+ this.#contextRevision++;
1090
+ }
1091
+
1092
+ setThinkingLevel(l: Effort | undefined) {
1093
+ this.#state.thinkingLevel = l;
1094
+ }
1095
+
1096
+ setSteeringMode(mode: "all" | "one-at-a-time") {
1097
+ this.#steeringMode = mode;
1098
+ }
1099
+
1100
+ getSteeringMode(): "all" | "one-at-a-time" {
1101
+ return this.#steeringMode;
1102
+ }
1103
+
1104
+ setFollowUpMode(mode: "all" | "one-at-a-time") {
1105
+ this.#followUpMode = mode;
1106
+ }
1107
+
1108
+ getFollowUpMode(): "all" | "one-at-a-time" {
1109
+ return this.#followUpMode;
1110
+ }
1111
+
1112
+ setInterruptMode(mode: "immediate" | "wait") {
1113
+ this.#interruptMode = mode;
1114
+ }
1115
+
1116
+ getInterruptMode(): "immediate" | "wait" {
1117
+ return this.#interruptMode;
1118
+ }
1119
+
1120
+ setTools(t: AgentTool<any>[]) {
1121
+ this.#state.tools = t;
1122
+ this.#contextRevision++;
1123
+ }
1124
+
1125
+ replaceMessages(
1126
+ ms: AgentMessage[],
1127
+ options?: { historyRewrite?: { reason: string; preserveSeededPrefix?: boolean } },
1128
+ ) {
1129
+ const rewrite = options?.historyRewrite;
1130
+ if (rewrite && this.#appendOnlyContext) {
1131
+ this.#appendOnlyContext.releaseAfterHistoryRewrite({ preserveSeededPrefix: rewrite.preserveSeededPrefix });
1132
+ }
1133
+ this.#state.messages = ms.slice();
1134
+ this.#contextRevision++;
1135
+ }
1136
+
1137
+ appendMessage(m: AgentMessage) {
1138
+ // In-place push (not [...messages, m]): appending M messages over a session of
1139
+ // N is O(N+M), not O(M*N). Consumers read state.messages fresh; run() snapshots
1140
+ // via slice() at the API boundary, so no caller relies on per-append array identity.
1141
+ this.#state.messages.push(m);
1142
+ this.#contextRevision++;
1143
+ }
1144
+
1145
+ popMessage(): AgentMessage | undefined {
1146
+ const messages = this.#state.messages.slice(0, -1);
1147
+ const removed = this.#state.messages.at(-1);
1148
+ this.#state.messages = messages;
1149
+ this.#contextRevision++;
1150
+
1151
+ if (removed && this.#state.streamMessage === removed) {
1152
+ this.#state.streamMessage = null;
1153
+ }
1154
+
1155
+ return removed;
1156
+ }
1157
+
1158
+ /**
1159
+ * For callers that mutate committed messages or the system prompt in place
1160
+ * outside Agent-owned mutators.
1161
+ */
1162
+ touchContext(): void {
1163
+ this.#contextRevision++;
1164
+ }
1165
+
1166
+ /**
1167
+ * Queue a steering message to interrupt the agent mid-run.
1168
+ * Delivered after current tool execution, skips remaining tools.
1169
+ */
1170
+ steer(m: AgentMessage) {
1171
+ assertUserImagePlaceholdersHavePayload([m]);
1172
+ this.#steeringQueue.push(m);
1173
+ for (const notify of [...this.#steeringWaiters]) notify();
1174
+ }
1175
+
1176
+ /**
1177
+ * Resolves when a steering message is queued (or is already queued), or when
1178
+ * `signal` aborts. The queue is not consumed. Long observation tools use this
1179
+ * to end their wait early so a busy user message is handled at the next tool
1180
+ * boundary instead of after the full wait window.
1181
+ */
1182
+ waitForSteeringArrival(signal: AbortSignal): Promise<void> {
1183
+ if (this.#steeringQueue.length > 0 || signal.aborted) return Promise.resolve();
1184
+ const { promise, resolve } = Promise.withResolvers<void>();
1185
+ let settled = false;
1186
+ const settle = () => {
1187
+ if (settled) return;
1188
+ settled = true;
1189
+ this.#steeringWaiters.delete(settle);
1190
+ signal.removeEventListener("abort", settle);
1191
+ resolve();
1192
+ };
1193
+ this.#steeringWaiters.add(settle);
1194
+ signal.addEventListener("abort", settle, { once: true });
1195
+ if (this.#steeringQueue.length > 0 || signal.aborted) settle();
1196
+ return promise;
1197
+ }
1198
+
1199
+ /**
1200
+ * Queue a follow-up message to be processed after the agent finishes.
1201
+ * Delivered only when agent has no more tool calls or steering messages.
1202
+ *
1203
+ * `forceOneAtATime` lets UI composer queues preserve prompt-by-prompt
1204
+ * delivery even when the session-wide follow-up mode is set to `all` for
1205
+ * other integration paths.
1206
+ */
1207
+ followUp(m: AgentMessage, options?: { forceOneAtATime?: boolean }) {
1208
+ assertUserImagePlaceholdersHavePayload([m]);
1209
+ if (options?.forceOneAtATime) {
1210
+ this.#followUpForceOneAtATime.add(m);
1211
+ }
1212
+ this.#followUpQueue.push(m);
1213
+ }
1214
+
1215
+ clearSteeringQueue() {
1216
+ this.#steeringQueue = [];
1217
+ }
1218
+
1219
+ clearFollowUpQueue() {
1220
+ this.#followUpQueue = [];
1221
+ }
1222
+
1223
+ clearAllQueues() {
1224
+ this.#steeringQueue = [];
1225
+ this.#followUpQueue = [];
1226
+ }
1227
+
1228
+ hasQueuedMessages(): boolean {
1229
+ return this.#steeringQueue.length > 0 || this.#followUpQueue.length > 0;
1230
+ }
1231
+
1232
+ hasQueuedSteering(): boolean {
1233
+ return this.#steeringQueue.length > 0;
1234
+ }
1235
+
1236
+ /**
1237
+ * Snapshot the steering queue without mutating it. Used to preserve queued
1238
+ * steering across maintenance ops (compaction/handoff) that call reset().
1239
+ */
1240
+ snapshotSteering(): AgentMessage[] {
1241
+ return this.#steeringQueue.slice();
1242
+ }
1243
+
1244
+ /**
1245
+ * Restore previously snapshotted steering messages ahead of any newly
1246
+ * queued ones. No-op for an empty snapshot.
1247
+ */
1248
+ restoreSteering(messages: AgentMessage[]): void {
1249
+ if (messages.length === 0) return;
1250
+ this.#steeringQueue = [...messages, ...this.#steeringQueue];
1251
+ }
1252
+
1253
+ /** Snapshot the follow-up queue without mutating it. */
1254
+ snapshotFollowUp(): AgentMessage[] {
1255
+ return this.#followUpQueue.slice();
1256
+ }
1257
+
1258
+ /** Restore previously snapshotted follow-up messages ahead of any newly queued ones. */
1259
+ restoreFollowUp(messages: AgentMessage[]): void {
1260
+ if (messages.length === 0) return;
1261
+ this.#followUpQueue = [...messages, ...this.#followUpQueue];
1262
+ }
1263
+
1264
+ /** Snapshot both executable queues as one atomic session-level view. */
1265
+ snapshotQueues(): AgentQueueSnapshot {
1266
+ return {
1267
+ steering: this.#steeringQueue.slice(),
1268
+ followUp: this.#followUpQueue.slice(),
1269
+ };
1270
+ }
1271
+
1272
+ /** Replace both executable queues with a prior snapshot. */
1273
+ restoreQueues(snapshot: AgentQueueSnapshot): void {
1274
+ this.#steeringQueue = snapshot.steering.slice();
1275
+ this.#followUpQueue = snapshot.followUp.slice();
1276
+ }
1277
+
1278
+ #dequeueSteeringMessages(): AgentMessage[] {
1279
+ if (this.#steeringMode === "one-at-a-time") {
1280
+ if (this.#steeringQueue.length > 0) {
1281
+ const first = this.#steeringQueue[0];
1282
+ this.#steeringQueue = this.#steeringQueue.slice(1);
1283
+ return [first];
1284
+ }
1285
+ return [];
1286
+ }
1287
+ const steering = this.#steeringQueue.slice();
1288
+ this.#steeringQueue = [];
1289
+ return steering;
1290
+ }
1291
+
1292
+ #dequeueFollowUpMessages(): AgentMessage[] {
1293
+ if (this.#followUpMode === "one-at-a-time") {
1294
+ if (this.#followUpQueue.length > 0) {
1295
+ const first = this.#followUpQueue[0];
1296
+ this.#followUpQueue = this.#followUpQueue.slice(1);
1297
+ return [first];
1298
+ }
1299
+ return [];
1300
+ }
1301
+
1302
+ const first = this.#followUpQueue[0];
1303
+ if (!first) return [];
1304
+ if (this.#followUpForceOneAtATime.has(first)) {
1305
+ this.#followUpQueue = this.#followUpQueue.slice(1);
1306
+ return [first];
1307
+ }
1308
+
1309
+ const forcedIndex = this.#followUpQueue.findIndex(message => this.#followUpForceOneAtATime.has(message));
1310
+ const takeCount = forcedIndex === -1 ? this.#followUpQueue.length : forcedIndex;
1311
+ const followUp = this.#followUpQueue.slice(0, takeCount);
1312
+ this.#followUpQueue = this.#followUpQueue.slice(takeCount);
1313
+ return followUp;
1314
+ }
1315
+
1316
+ /**
1317
+ * Remove and return the last steering message from the queue (LIFO).
1318
+ * Used by dequeue keybinding.
1319
+ */
1320
+ popLastSteer(): AgentMessage | undefined {
1321
+ return this.#steeringQueue.pop();
1322
+ }
1323
+ removeSteerAt(index: number): AgentMessage | undefined {
1324
+ if (index < 0 || index >= this.#steeringQueue.length) return undefined;
1325
+ const [removed] = this.#steeringQueue.splice(index, 1);
1326
+ return removed;
1327
+ }
1328
+
1329
+ moveSteer(fromIndex: number, toIndex: number): boolean {
1330
+ return this.#moveQueuedMessage(this.#steeringQueue, fromIndex, toIndex);
1331
+ }
1332
+
1333
+ /**
1334
+ * Remove and return the last follow-up message from the queue (LIFO).
1335
+ * Used by dequeue keybinding.
1336
+ */
1337
+ popLastFollowUp(): AgentMessage | undefined {
1338
+ return this.#followUpQueue.pop();
1339
+ }
1340
+ removeFollowUpAt(index: number): AgentMessage | undefined {
1341
+ if (index < 0 || index >= this.#followUpQueue.length) return undefined;
1342
+ const [removed] = this.#followUpQueue.splice(index, 1);
1343
+ return removed;
1344
+ }
1345
+
1346
+ moveFollowUp(fromIndex: number, toIndex: number): boolean {
1347
+ return this.#moveQueuedMessage(this.#followUpQueue, fromIndex, toIndex);
1348
+ }
1349
+
1350
+ #moveQueuedMessage<T>(queue: T[], fromIndex: number, toIndex: number): boolean {
1351
+ if (fromIndex < 0 || fromIndex >= queue.length) return false;
1352
+ if (toIndex < 0 || toIndex >= queue.length) return false;
1353
+ if (fromIndex === toIndex) return true;
1354
+ const [item] = queue.splice(fromIndex, 1);
1355
+ if (item === undefined) return false;
1356
+ queue.splice(toIndex, 0, item);
1357
+ return true;
1358
+ }
1359
+
1360
+ /**
1361
+ * Remove ALL queued STEERING messages without touching the follow-up queue.
1362
+ * Used by the terminal-abort path to purge steering queued for the aborted
1363
+ * turn (the loop may exit on the abort signal without polling it); the
1364
+ * follow-up queue is preserved because it may carry owned-completion
1365
+ * resumes that must still deliver.
1366
+ */
1367
+ clearSteeringMessages(): void {
1368
+ this.#steeringQueue = [];
1369
+ }
1370
+
1371
+ /**
1372
+ * Remove queued steering/follow-up messages matching `predicate`, preserving
1373
+ * order of the rest. `scope` restricts the removal to one queue — the
1374
+ * terminal-abort steering purge must not wipe the follow-up queue, which
1375
+ * the owned-completion resume policy preserves.
1376
+ */
1377
+ removeQueuedMessages(
1378
+ predicate: (message: AgentMessage) => boolean,
1379
+ scope: "both" | "steering" | "followUp" = "both",
1380
+ ): {
1381
+ steering: number;
1382
+ followUp: number;
1383
+ total: number;
1384
+ } {
1385
+ const beforeSteering = this.#steeringQueue.length;
1386
+ const beforeFollowUp = this.#followUpQueue.length;
1387
+ if (scope !== "followUp") {
1388
+ this.#steeringQueue = this.#steeringQueue.filter(m => !predicate(m));
1389
+ }
1390
+ if (scope !== "steering") {
1391
+ this.#followUpQueue = this.#followUpQueue.filter(m => !predicate(m));
1392
+ }
1393
+ const steering = beforeSteering - this.#steeringQueue.length;
1394
+ const followUp = beforeFollowUp - this.#followUpQueue.length;
1395
+ return { steering, followUp, total: steering + followUp };
1396
+ }
1397
+
1398
+ clearMessages() {
1399
+ this.#state.messages = [];
1400
+ this.#contextRevision++;
1401
+ }
1402
+
1403
+ abort() {
1404
+ this.#abortController?.abort();
1405
+ }
1406
+
1407
+ /**
1408
+ * Force the current run out of the busy/streaming state when cooperative abort
1409
+ * did not drain. The abandoned provider/tool stream may still settle later, so
1410
+ * #runLoop guards every state mutation with a run id.
1411
+ */
1412
+ forceAbort(reason = "Force aborted", logicalRunId?: ManagedLogicalRunId | number): boolean {
1413
+ const targetLogicalRunId = logicalRunId ?? this.#managedLogicalRunOwner ?? this.#activeRunId;
1414
+ const handle = targetLogicalRunId !== undefined ? this.#runHandles.get(targetLogicalRunId) : undefined;
1415
+ const runId = this.#activeRunId;
1416
+ const managedLogicalRunId = this.#managedLogicalRunOwner;
1417
+ const activeLogicalRunId = managedLogicalRunId ?? runId;
1418
+ if (
1419
+ targetLogicalRunId !== undefined &&
1420
+ activeLogicalRunId !== undefined &&
1421
+ activeLogicalRunId !== targetLogicalRunId
1422
+ ) {
1423
+ throw new Error(`forceAbort: logicalRunId ${targetLogicalRunId} does not match the active run`);
1424
+ }
1425
+ const activeResourceDomain = this.#activeResourceCancellationDomain;
1426
+ const activeResourceRunId = this.#activeResourceRunId;
1427
+ const hadActiveRun = runId !== undefined && (this.#runningPrompt !== undefined || this.#state.isStreaming);
1428
+ if (!hadActiveRun) return false;
1429
+
1430
+ this.#abortController?.abort(reason);
1431
+ this.#continuationGeneration++;
1432
+ this.#attemptAuthority.advanceMain();
1433
+ this.#state.isStreaming = false;
1434
+ this.#state.streamMessage = null;
1435
+ this.#state.pendingToolCalls = new Set<string>();
1436
+ this.#abortController = undefined;
1437
+ this.#cursorToolResultBuffer = [];
1438
+ this.#managedLogicalRunOwner = undefined;
1439
+
1440
+ const resolve = this.#resolveRunningPrompt;
1441
+ this.#runningPrompt = undefined;
1442
+ this.#resolveRunningPrompt = undefined;
1443
+ this.#activeRunId = undefined;
1444
+ this.#activeResourceRunId = undefined;
1445
+ this.#activeResourceCancellationDomain = undefined;
1446
+ resolve?.();
1447
+ this.#finalizeRun(
1448
+ activeLogicalRunId ?? runId!,
1449
+ {
1450
+ type: "agent_end",
1451
+ messages: [],
1452
+ stopReason: "cancelled",
1453
+ scope: handle?.scope,
1454
+ },
1455
+ undefined,
1456
+ activeResourceDomain,
1457
+ );
1458
+ if (activeResourceRunId) this.resourceLedger.quarantine(activeResourceRunId);
1459
+ return true;
1460
+ }
1461
+
1462
+ waitForIdle(): Promise<void> {
1463
+ return this.#runningPrompt ?? Promise.resolve();
1464
+ }
1465
+
1466
+ /** The active per-attempt run identifier. */
1467
+ get activeRunId(): number | undefined {
1468
+ return this.#activeRunId;
1469
+ }
1470
+ /** Stable resource ownership identifier for the active prompt run. */
1471
+ get activeResourceRunId(): string | undefined {
1472
+ return this.#activeResourceRunId;
1473
+ }
1474
+
1475
+ /**
1476
+ * Stable identifier for the active managed logical run, shared by every retry
1477
+ * attempt. Pass this value to requestRunTerminal(); never retain activeRunId
1478
+ * for managed terminal completion.
1479
+ */
1480
+ get currentManagedLogicalRunId(): ManagedLogicalRunId | undefined {
1481
+ return this.#managedLogicalRunOwner;
1482
+ }
1483
+
1484
+ /**
1485
+ * Request terminal completion through the single logical-run keyed finalizer.
1486
+ *
1487
+ * For managed runs, logicalRunId must be currentManagedLogicalRunId from any
1488
+ * attempt in the retry chain. Non-managed runs use their activeRunId. Terminal
1489
+ * requests with messages emit a committed message_start/message_end lifecycle
1490
+ * for each diagnostic before agent_end. Requests without messages (such as
1491
+ * cancellation) emit only agent_end.
1492
+ */
1493
+ requestRunTerminal(logicalRunId: ManagedLogicalRunId, request: RunTerminalRequest): boolean {
1494
+ if (this.#terminalizedLogicalRunIds.has(logicalRunId)) return false;
1495
+ const handle = this.#runHandles.get(logicalRunId);
1496
+ if (!handle) throw new Error(`requestRunTerminal: unknown logicalRunId ${logicalRunId} (no attempt handle)`);
1497
+ if (this.#managedLogicalRunOwner === logicalRunId) {
1498
+ this.#managedLogicalRunOwner = undefined;
1499
+ }
1500
+ this.#finalizeRun(
1501
+ logicalRunId,
1502
+ {
1503
+ type: "agent_end",
1504
+ messages: request.messages ?? [],
1505
+ ...(request.stopReason === "cancelled" ? { stopReason: "cancelled" as const } : {}),
1506
+ scope: handle.scope,
1507
+ },
1508
+ () => {
1509
+ for (const message of request.messages ?? []) {
1510
+ this.#emit({ type: "message_start", message });
1511
+ this.appendMessage(message);
1512
+ this.#emit({ type: "message_end", message });
1513
+ }
1514
+ },
1515
+ );
1516
+ return true;
1517
+ }
1518
+
1519
+ reset() {
1520
+ this.#state.messages = [];
1521
+ this.#contextRevision++;
1522
+ this.#state.isStreaming = false;
1523
+ this.#state.streamMessage = null;
1524
+ this.#state.pendingToolCalls = new Set<string>();
1525
+ this.#managedLogicalRunOwner = undefined;
1526
+ this.#state.error = undefined;
1527
+ this.#steeringQueue = [];
1528
+ this.#followUpQueue = [];
1529
+ }
1530
+
1531
+ /** Send a prompt with an AgentMessage */
1532
+ async prompt(message: AgentMessage | AgentMessage[], options?: AgentPromptOptions): Promise<void>;
1533
+ async prompt(input: string, options?: AgentPromptOptions): Promise<void>;
1534
+ async prompt(input: string, images?: ImageContent[], options?: AgentPromptOptions): Promise<void>;
1535
+ async prompt(
1536
+ input: string | AgentMessage | AgentMessage[],
1537
+ imagesOrOptions?: ImageContent[] | AgentPromptOptions,
1538
+ options?: AgentPromptOptions,
1539
+ ) {
1540
+ if (this.#state.isStreaming) {
1541
+ throw new AgentBusyError();
1542
+ }
1543
+
1544
+ const model = this.#state.model;
1545
+ if (!model) throw new Error("No model configured");
1546
+
1547
+ let msgs: AgentMessage[];
1548
+ let promptOptions: AgentPromptOptions | undefined;
1549
+ let images: ImageContent[] | undefined;
1550
+
1551
+ if (Array.isArray(input)) {
1552
+ msgs = input;
1553
+ promptOptions = imagesOrOptions as AgentPromptOptions | undefined;
1554
+ } else if (typeof input === "string") {
1555
+ if (Array.isArray(imagesOrOptions)) {
1556
+ images = imagesOrOptions;
1557
+ promptOptions = options;
1558
+ } else {
1559
+ promptOptions = imagesOrOptions;
1560
+ }
1561
+ const content: Array<TextContent | ImageContent> = [{ type: "text", text: input }];
1562
+ if (images && images.length > 0) {
1563
+ content.push(...images);
1564
+ }
1565
+ msgs = [
1566
+ {
1567
+ role: "user",
1568
+ content,
1569
+ timestamp: Date.now(),
1570
+ },
1571
+ ];
1572
+ } else {
1573
+ msgs = [input];
1574
+ promptOptions = imagesOrOptions as AgentPromptOptions | undefined;
1575
+ }
1576
+
1577
+ assertUserImagePlaceholdersHavePayload(msgs);
1578
+ if (this.#managedLogicalRunOwner !== undefined) {
1579
+ this.requestRunTerminal(this.#managedLogicalRunOwner, { stopReason: "cancelled" });
1580
+ this.#managedLogicalRunOwner = undefined;
1581
+ }
1582
+
1583
+ await this.#runLoop(msgs, promptOptions);
1584
+ }
1585
+
1586
+ /**
1587
+ * Continue from current context (used for retries and resuming queued messages).
1588
+ */
1589
+ async continue(options?: AgentPromptOptions) {
1590
+ if (this.#state.isStreaming) {
1591
+ throw new AgentBusyError();
1592
+ }
1593
+
1594
+ const messages = this.#state.messages;
1595
+ if (messages.length === 0) {
1596
+ throw new Error("No messages to continue from");
1597
+ }
1598
+ if (messages[messages.length - 1].role === "assistant") {
1599
+ const queuedSteering = this.#dequeueSteeringMessages();
1600
+ if (queuedSteering.length > 0) {
1601
+ await this.#runLoop(queuedSteering, {
1602
+ ...options,
1603
+ skipInitialSteeringPoll: true,
1604
+ consumedQueuedMessages: queuedSteering,
1605
+ });
1606
+ return;
1607
+ }
1608
+
1609
+ const queuedFollowUp = this.#dequeueFollowUpMessages();
1610
+ if (queuedFollowUp.length > 0) {
1611
+ // Route the DIRECT-dequeue batch through the same consumption hook
1612
+ // the in-loop getFollowUpMessages path uses: denied owned-completion
1613
+ // envelopes are filtered before they reach the loop, and delivered
1614
+ // envelopes settle their registrations — the direct path otherwise
1615
+ // bypasses onFollowUpConsumed entirely (review threads P1/P2). A
1616
+ // maintenanceContinuation resumes the existing logical run (no new
1617
+ // agent_start), so its batch is in-run consumption, not an own-run
1618
+ // promotion (#4668 review P1).
1619
+ await this.onFollowUpConsumed?.(queuedFollowUp, {
1620
+ startsOwnRun: options?.maintenanceContinuation !== true,
1621
+ });
1622
+ // The hook can filter the WHOLE batch (every entry denied by a
1623
+ // scope:"owned" abort): starting an empty provider run would
1624
+ // violate the zero-final-call guarantee, so return without
1625
+ // running the loop (review thread P1).
1626
+ if (queuedFollowUp.length === 0) return;
1627
+ await this.#runLoop(queuedFollowUp, { ...options, consumedQueuedMessages: queuedFollowUp });
1628
+ return;
1629
+ }
1630
+
1631
+ throw new Error("Cannot continue from message role: assistant");
1632
+ }
1633
+
1634
+ if (!canContinuePersistedHistory(messages)) {
1635
+ throw new Error("No messages to continue from");
1636
+ }
1637
+
1638
+ await this.#runLoop(undefined, options);
1639
+ }
1640
+ /**
1641
+ * Continue by consuming queued steering/follow-up messages without replaying
1642
+ * the current non-assistant tail.
1643
+ */
1644
+ async continueQueuedMessages(options?: AgentPromptOptions): Promise<void> {
1645
+ if (this.#state.isStreaming) {
1646
+ throw new AgentBusyError();
1647
+ }
1648
+ const queuedSteering = this.#dequeueSteeringMessages();
1649
+ if (queuedSteering.length > 0) {
1650
+ await this.#runLoop(queuedSteering, {
1651
+ ...options,
1652
+ skipInitialSteeringPoll: true,
1653
+ consumedQueuedMessages: queuedSteering,
1654
+ });
1655
+ return;
1656
+ }
1657
+ const queuedFollowUp = this.#dequeueFollowUpMessages();
1658
+ if (queuedFollowUp.length > 0) {
1659
+ // Route the queued-tail batch through the same consumption hook as the
1660
+ // in-loop getFollowUpMessages path and the assistant-tail continue()
1661
+ // path: denied owned-completion envelopes are filtered before they
1662
+ // reach the loop, and delivered envelopes settle their registrations.
1663
+ // A terminal abort that leaves a tool/result tail and rearms an
1664
+ // authorized owned-completion follow-up reaches this branch, so
1665
+ // bypassing the hook would leak every such job's ownership tuple and
1666
+ // eventually exhaust the bounded ownership registries (review thread
1667
+ // P2). A maintenanceContinuation resumes the existing logical run (no
1668
+ // new agent_start), so its batch is in-run consumption, not an own-run
1669
+ // promotion (#4668 review P1).
1670
+ await this.onFollowUpConsumed?.(queuedFollowUp, {
1671
+ startsOwnRun: options?.maintenanceContinuation !== true,
1672
+ });
1673
+ // The hook can filter the WHOLE batch (every entry denied by a
1674
+ // scope:"owned" abort): starting an empty provider run would violate
1675
+ // the zero-final-call guarantee, so return without running the loop
1676
+ // (review thread P2).
1677
+ if (queuedFollowUp.length === 0) return;
1678
+ await this.#runLoop(queuedFollowUp, { ...options, consumedQueuedMessages: queuedFollowUp });
1679
+ return;
1680
+ }
1681
+ throw new Error("No queued messages to continue");
1682
+ }
1683
+
1684
+ /**
1685
+ * Run the agent loop.
1686
+ * If messages are provided, starts a new conversation turn with those messages.
1687
+ * Otherwise, continues from existing context.
1688
+ */
1689
+ async #runLoop(
1690
+ messages?: AgentMessage[],
1691
+ options?: AgentPromptOptions & {
1692
+ skipInitialSteeringPoll?: boolean;
1693
+ consumedQueuedMessages?: readonly AgentMessage[];
1694
+ },
1695
+ ) {
1696
+ const model = this.#state.model;
1697
+ if (!model) throw new Error("No model configured");
1698
+
1699
+ const maintenanceContinuation = options?.maintenanceContinuation === true;
1700
+ if (maintenanceContinuation && this.#managedLogicalRunOwner === undefined) {
1701
+ throw new Error("Maintenance continuation ownership is unavailable");
1702
+ }
1703
+ let skipInitialSteeringPoll = options?.skipInitialSteeringPoll === true;
1704
+
1705
+ const { promise, resolve } = Promise.withResolvers<void>();
1706
+ this.#runningPrompt = promise;
1707
+ this.#resolveRunningPrompt = resolve;
1708
+
1709
+ const runId = ++this.#runSequence;
1710
+ const continuationGeneration = ++this.#continuationGeneration;
1711
+ this.#activeRunId = runId;
1712
+ const abortController = new AbortController();
1713
+ this.#abortController = abortController;
1714
+ this.#state.isStreaming = true;
1715
+ this.#state.streamMessage = null;
1716
+ this.#state.error = undefined;
1717
+
1718
+ const fallbackManaged = options?.fallbackManaged === true;
1719
+ const managedLogicalRunOwner = fallbackManaged
1720
+ ? (this.#managedLogicalRunOwner ?? runId)
1721
+ : maintenanceContinuation
1722
+ ? this.#managedLogicalRunOwner
1723
+ : undefined;
1724
+ const continuesLogicalRun = fallbackManaged || maintenanceContinuation;
1725
+ const startsManagedLogicalRun = fallbackManaged && this.#managedLogicalRunOwner === undefined;
1726
+ this.#activeResourceRunId = String(managedLogicalRunOwner ?? runId);
1727
+ this.#activeResourceCancellationDomain = this.resourceLedger.open(this.#activeResourceRunId);
1728
+ if (!this.#activeResourceCancellationDomain) {
1729
+ this.#state.isStreaming = false;
1730
+ this.#abortController = undefined;
1731
+ this.#activeRunId = undefined;
1732
+ this.#activeResourceRunId = undefined;
1733
+ this.#activeResourceCancellationDomain = undefined;
1734
+ this.#runningPrompt = undefined;
1735
+ this.#resolveRunningPrompt = undefined;
1736
+ resolve();
1737
+ throw new Error("Prompt resource cancellation domain is unavailable");
1738
+ }
1739
+ const logicalRunId = managedLogicalRunOwner ?? runId;
1740
+ const scope = this.#attemptAuthority.mintMain();
1741
+ this.#observeMainAttemptScope(scope);
1742
+ const handle: AttemptRunHandle = { logicalRunId, scope };
1743
+ this.#runHandles.set(logicalRunId, handle);
1744
+ options?.onRunAccepted?.(handle, {
1745
+ consumedQueuedMessages: options.consumedQueuedMessages ?? [],
1746
+ });
1747
+ if (startsManagedLogicalRun) {
1748
+ this.#managedLogicalRunOwner = logicalRunId;
1749
+ this.#emit({ type: "agent_start", scope });
1750
+ }
1751
+ if (fallbackManaged && this.#cursorToolResultBuffer.length > 0) {
1752
+ const error = new ManagedCursorInvariantError(
1753
+ "Managed Cursor attempt started with buffered provider-side tool results",
1754
+ );
1755
+ this.#state.isStreaming = false;
1756
+ this.#abortController = undefined;
1757
+ this.#activeRunId = undefined;
1758
+ this.#activeResourceRunId = undefined;
1759
+ this.#activeResourceCancellationDomain = undefined;
1760
+ this.#runningPrompt = undefined;
1761
+ this.#resolveRunningPrompt = undefined;
1762
+ resolve();
1763
+ this.requestRunTerminal(managedLogicalRunOwner ?? runId, { stopReason: "error" });
1764
+ this.#managedLogicalRunOwner = undefined;
1765
+ throw error;
1766
+ }
1767
+ // Each run gets a fresh buffer only after managed stale-state validation.
1768
+ this.#cursorToolResultBuffer = [];
1769
+
1770
+ const reasoning = this.#state.thinkingLevel;
1771
+ const context: AgentContext = {
1772
+ systemPrompt: this.#state.systemPrompt,
1773
+ messages: this.#state.messages.slice(),
1774
+ tools: this.#state.tools,
1775
+ };
1776
+ // Cursor can execute native tools inside one remote turn, then return
1777
+ // `turnEnded` without another model request. Remember a Composer policy
1778
+ // rejection until the loop reaches that safe continuation boundary.
1779
+ let cursorComposerBashRecoveryPending = false;
1780
+ let cursorComposerBashRecoveryAttempted = false;
1781
+
1782
+ const cursorOnToolResult =
1783
+ !fallbackManaged && (this.#cursorExecHandlers || this.#cursorOnToolResult)
1784
+ ? async (message: ToolResultMessage) => {
1785
+ let finalMessage = message;
1786
+ if (this.#activeRunId !== runId) {
1787
+ return finalMessage;
1788
+ }
1789
+ if (this.#cursorOnToolResult) {
1790
+ try {
1791
+ const updated = await this.#cursorOnToolResult(message);
1792
+ if (this.#activeRunId !== runId) {
1793
+ return finalMessage;
1794
+ }
1795
+ if (updated) {
1796
+ finalMessage = updated;
1797
+ }
1798
+ } catch {}
1799
+ }
1800
+ if (isCursorComposerBashPolicyBlockedResult(finalMessage)) {
1801
+ cursorComposerBashRecoveryPending = true;
1802
+ } else if (
1803
+ cursorComposerBashRecoveryPending &&
1804
+ isSuccessfulCursorNativeRepositoryToolResult(finalMessage)
1805
+ ) {
1806
+ // The same remote turn already replanned through a native tool,
1807
+ // so do not create a redundant local continuation afterward.
1808
+ cursorComposerBashRecoveryPending = false;
1809
+ }
1810
+ // Cursor executes tools server-side during streaming, so the assistant message
1811
+ // already incorporates results. We buffer here and emit in correct order
1812
+ // when the assistant message ends.
1813
+ const textLength = this.#getAssistantTextLength(this.#state.streamMessage);
1814
+ this.#cursorToolResultBuffer.push({ toolResult: finalMessage, textLengthAtCall: textLength });
1815
+ return finalMessage;
1816
+ }
1817
+ : undefined;
1818
+
1819
+ const getToolChoice = () =>
1820
+ this.#getToolChoice?.() ?? refreshToolChoiceForActiveTools(options?.toolChoice, this.#state.tools);
1821
+ const cursorExecHandlers = fallbackManaged ? undefined : this.#cursorExecHandlersForRun(runId);
1822
+ let managedDecision: ManagedAttemptDecision | undefined;
1823
+ let managedOutcome: ManagedAttemptOutcome | undefined;
1824
+ let maintenanceInterrupted = false;
1825
+
1826
+ const config: AgentLoopConfig = {
1827
+ model,
1828
+ reasoning,
1829
+ temperature: this.#temperature,
1830
+ topP: this.#topP,
1831
+ topK: this.#topK,
1832
+ minP: this.#minP,
1833
+ presencePenalty: this.#presencePenalty,
1834
+ repetitionPenalty: this.#repetitionPenalty,
1835
+ serviceTier: this.#serviceTier,
1836
+ hideThinkingSummary: this.#hideThinkingSummary,
1837
+ interruptMode: this.#interruptMode,
1838
+ sessionId: this.#sessionId,
1839
+ providerSessionId: this.#providerSessionId,
1840
+ metadata: this.#metadataResolver ? undefined : this.#metadata,
1841
+ metadataResolver: this.#metadataResolver,
1842
+ providerSessionState: this.#providerSessionState,
1843
+ thinkingBudgets: this.#thinkingBudgets,
1844
+ maxRetryDelayMs: this.#maxRetryDelayMs,
1845
+ requestMaxRetries: this.#requestMaxRetries,
1846
+ streamMaxRetries: this.#streamMaxRetries,
1847
+ streamFirstEventTimeoutMs: this.#streamFirstEventTimeoutMs,
1848
+ ...(fallbackManaged
1849
+ ? {
1850
+ fallbackManaged: true,
1851
+ nextFallbackAttempt: options?.nextFallbackAttempt,
1852
+ onManagedAttemptAccepted: options?.onManagedAttemptAccepted,
1853
+ onManagedAttemptOutcome: async outcome => {
1854
+ managedOutcome = outcome;
1855
+ managedDecision = (await options?.onManagedAttemptOutcome?.(outcome)) ?? {
1856
+ type: "terminal",
1857
+ terminal: { stopReason: outcome.type === "run_terminal" ? outcome.reason : "error" },
1858
+ };
1859
+ return managedDecision;
1860
+ },
1861
+ }
1862
+ : {}),
1863
+ kimiApiFormat: this.#kimiApiFormat,
1864
+ preferWebsockets: this.#preferWebsockets,
1865
+ convertToLlm: this.#convertToLlm,
1866
+ transformContext: this.#transformContext,
1867
+ attemptMinter: {
1868
+ mint: () => {
1869
+ const scope = this.#attemptAuthority.mintMain();
1870
+ this.#observeMainAttemptScope(scope);
1871
+ return scope;
1872
+ },
1873
+ },
1874
+ initialScope: scope,
1875
+ onPayload: this.#onPayload,
1876
+ onResponse: this.#onResponse,
1877
+ onSseEvent: this.#onSseEvent,
1878
+ signal: abortController.signal,
1879
+ resourceLedger: this.resourceLedger,
1880
+ resourceRunId: this.#activeResourceRunId,
1881
+ resourceCancellationDomain: this.#activeResourceCancellationDomain,
1882
+ resourceSealOwner: "caller",
1883
+ getApiKey: this.getApiKey,
1884
+ getAuthCredentialType: this.getAuthCredentialType,
1885
+ getToolContext: this.#getToolContext,
1886
+ syncContextBeforeModelCall: async context => {
1887
+ if (this.#listeners.size > 0) {
1888
+ await Bun.sleep(0);
1889
+ }
1890
+ context.systemPrompt = this.#state.systemPrompt;
1891
+ context.tools = this.#state.tools;
1892
+ },
1893
+ ...(cursorExecHandlers ? { cursorExecHandlers } : {}),
1894
+ ...(cursorOnToolResult ? { cursorOnToolResult } : {}),
1895
+ transformToolCallArguments: this.#transformToolCallArguments,
1896
+ intentTracing: this.#intentTracing,
1897
+ appendOnlyContext: this.#appendOnlyContext,
1898
+ beforeToolCall: this.beforeToolCall
1899
+ ? async (ctx, signal) => {
1900
+ if (this.#activeRunId !== runId) return undefined;
1901
+ const result = await this.beforeToolCall?.(ctx, signal);
1902
+ if (this.#activeRunId !== runId) return undefined;
1903
+ return result;
1904
+ }
1905
+ : undefined,
1906
+ afterToolCall: this.afterToolCall
1907
+ ? async (ctx, signal) => {
1908
+ if (this.#activeRunId !== runId) return undefined;
1909
+ const result = await this.afterToolCall?.(ctx, signal);
1910
+ if (this.#activeRunId !== runId) return undefined;
1911
+ return result;
1912
+ }
1913
+ : undefined,
1914
+ onAssistantMessageEvent: (message, event) => {
1915
+ if (this.#activeRunId !== runId) return;
1916
+ this.#onAssistantMessageEvent?.(message, event);
1917
+ },
1918
+ onProvisionalAssistantMessageEvent: (message, event) => {
1919
+ if (this.#activeRunId !== runId) return;
1920
+ this.#state.streamMessage = message;
1921
+ this.#onProvisionalAssistantMessageEvent?.(message, event);
1922
+ },
1923
+ hasProvisionalAssistantMessageEventConsumer: this.#onProvisionalAssistantMessageEvent !== undefined,
1924
+ onToolChoiceIncapability: this.#onToolChoiceIncapability
1925
+ ? event => {
1926
+ if (this.#activeRunId !== runId) return;
1927
+ this.#onToolChoiceIncapability?.(event);
1928
+ }
1929
+ : undefined,
1930
+ onHarmonyLeak: this.#onHarmonyLeak,
1931
+ getToolChoice,
1932
+ getReasoning: () => this.#state.thinkingLevel,
1933
+ getSteeringMessages: async () => {
1934
+ if (this.#activeRunId !== runId) {
1935
+ return [];
1936
+ }
1937
+ if (skipInitialSteeringPoll) {
1938
+ skipInitialSteeringPoll = false;
1939
+ return [];
1940
+ }
1941
+ // Fenced: yield nothing and dequeue nothing, so a steer submitted while a
1942
+ // fold is being claimed is neither consumed by the run being wound down
1943
+ // nor lost.
1944
+ if (this.#steeringAdmissionFence?.() === true) {
1945
+ return [];
1946
+ }
1947
+ const queued = this.#dequeueSteeringMessages();
1948
+ if (this.#activeRunId !== runId) {
1949
+ this.#steeringQueue = [...queued, ...this.#steeringQueue];
1950
+ return [];
1951
+ }
1952
+ // Mid-run consumption into the CURRENT turn: the batch never starts
1953
+ // its own run (#4668).
1954
+ if (queued.length > 0) await this.onSteeringConsumed?.(queued, { startsOwnRun: false });
1955
+ return queued;
1956
+ },
1957
+ requeueSteeringMessages: (messages: AgentMessage[]) => {
1958
+ if (messages.length === 0) return;
1959
+ this.#steeringQueue = [...messages, ...this.#steeringQueue];
1960
+ },
1961
+ getFollowUpMessages: async () => {
1962
+ if (this.#activeRunId !== runId) {
1963
+ return [];
1964
+ }
1965
+ const queued = this.#dequeueFollowUpMessages();
1966
+ if (this.#activeRunId !== runId) {
1967
+ this.#followUpQueue = [...queued, ...this.#followUpQueue];
1968
+ return [];
1969
+ }
1970
+ if (queued.length > 0) {
1971
+ await this.onFollowUpConsumed?.(queued, { startsOwnRun: false });
1972
+ }
1973
+ return queued;
1974
+ },
1975
+ getSyntheticRecoveryMessage: async () => {
1976
+ if (
1977
+ this.#activeRunId !== runId ||
1978
+ !cursorComposerBashRecoveryPending ||
1979
+ cursorComposerBashRecoveryAttempted
1980
+ ) {
1981
+ return undefined;
1982
+ }
1983
+ cursorComposerBashRecoveryPending = false;
1984
+ cursorComposerBashRecoveryAttempted = true;
1985
+ return {
1986
+ role: "user",
1987
+ content: CURSOR_COMPOSER_BASH_POLICY_RECOVERY_PROMPT,
1988
+ synthetic: true,
1989
+ timestamp: Date.now(),
1990
+ };
1991
+ },
1992
+ onBeforeYield: async () => {
1993
+ if (this.#activeRunId !== runId) return;
1994
+ await this.#onBeforeYield?.();
1995
+ },
1996
+ shouldPause: () => {
1997
+ if (this.#activeRunId !== runId) return false;
1998
+ return this.#shouldPause?.() === true;
1999
+ },
2000
+ maintainContext: this.#maintainContext
2001
+ ? async (context, lifecycle) => {
2002
+ if (this.#activeRunId !== runId) return "not-needed";
2003
+ return (await this.#maintainContext?.(context, lifecycle)) ?? "not-needed";
2004
+ }
2005
+ : undefined,
2006
+ transientRecoveryMessage: options?.transientRecoveryMessage,
2007
+ telemetry: this.#telemetry,
2008
+ };
2009
+
2010
+ let partial: AgentMessage | null = null;
2011
+
2012
+ try {
2013
+ const stream = messages
2014
+ ? agentLoop(messages, context, config, abortController.signal, this.streamFn, !continuesLogicalRun, scope)
2015
+ : agentLoopContinue(context, config, abortController.signal, this.streamFn, !continuesLogicalRun, scope);
2016
+
2017
+ for await (const event of stream) {
2018
+ if (this.#activeRunId !== runId) {
2019
+ break;
2020
+ }
2021
+
2022
+ // Update internal state based on events
2023
+ switch (event.type) {
2024
+ case "message_start":
2025
+ partial = event.message;
2026
+ this.#state.streamMessage = event.message;
2027
+ break;
2028
+
2029
+ case "message_update":
2030
+ partial = event.message;
2031
+ this.#state.streamMessage = event.message;
2032
+ break;
2033
+
2034
+ case "message_end":
2035
+ if (fallbackManaged && this.#cursorToolResultBuffer.length > 0) {
2036
+ throw new ManagedCursorInvariantError();
2037
+ }
2038
+ partial = null;
2039
+ // Check if this is an assistant message with buffered Cursor tool results.
2040
+ // If so, split the message to emit tool results at the correct position.
2041
+ if (event.message.role === "assistant" && this.#cursorToolResultBuffer.length > 0) {
2042
+ this.#emitCursorSplitAssistantMessage(event.message as AssistantMessage);
2043
+ continue; // Skip default emit - split method handles everything
2044
+ }
2045
+ this.#state.streamMessage = null;
2046
+ this.appendMessage(event.message);
2047
+ break;
2048
+
2049
+ case "tool_execution_start": {
2050
+ const s = new Set(this.#state.pendingToolCalls);
2051
+ s.add(event.toolCallId);
2052
+ this.#state.pendingToolCalls = s;
2053
+ break;
2054
+ }
2055
+
2056
+ case "tool_execution_end": {
2057
+ const s = new Set(this.#state.pendingToolCalls);
2058
+ s.delete(event.toolCallId);
2059
+ this.#state.pendingToolCalls = s;
2060
+ break;
2061
+ }
2062
+
2063
+ case "turn_end":
2064
+ if (event.message.role === "assistant" && (event.message as any).errorMessage) {
2065
+ this.#state.error = (event.message as any).errorMessage;
2066
+ }
2067
+ break;
2068
+
2069
+ case "agent_end":
2070
+ if (fallbackManaged && managedOutcome) {
2071
+ continue;
2072
+ }
2073
+ this.#state.isStreaming = false;
2074
+ this.#state.streamMessage = null;
2075
+ // A maintenance checkpoint is only non-terminal while a continuation will
2076
+ // follow. An aborted maintenance yields none, and because the loop runs with
2077
+ // `resourceSealOwner: "caller"` it deliberately leaves sealing to us, so
2078
+ // treating it as a checkpoint here would leave the run open forever and make
2079
+ // every cancel report `run_not_sealed`.
2080
+ if (event.stopReason === "maintenance" && event.maintenanceOutcome !== "aborted") {
2081
+ this.#managedLogicalRunOwner ??= managedLogicalRunOwner ?? runId;
2082
+ maintenanceInterrupted = true;
2083
+ this.#emit(event);
2084
+ continue;
2085
+ }
2086
+ this.#finalizeRun(managedLogicalRunOwner ?? runId, event);
2087
+ continue;
2088
+ }
2089
+
2090
+ // Emit to listeners
2091
+ this.#emit(event);
2092
+ }
2093
+
2094
+ if (this.#activeRunId !== runId) {
2095
+ return;
2096
+ }
2097
+ if (managedOutcome) {
2098
+ if (managedDecision?.type === "terminal") {
2099
+ this.requestRunTerminal(managedLogicalRunOwner ?? runId, managedDecision.terminal);
2100
+ } else if (managedOutcome.type === "run_terminal") {
2101
+ this.requestRunTerminal(managedLogicalRunOwner ?? runId, { stopReason: managedOutcome.reason });
2102
+ } else if (managedDecision?.type !== "retry" && managedDecision?.type !== "maintenance") {
2103
+ this.#finalizeRun(managedLogicalRunOwner ?? runId);
2104
+ }
2105
+ }
2106
+
2107
+ // Handle any remaining partial message
2108
+ if (partial && partial.role === "assistant" && Array.isArray(partial.content) && partial.content.length > 0) {
2109
+ const onlyEmpty = !partial.content.some(
2110
+ c =>
2111
+ (c.type === "thinking" && c.thinking.trim().length > 0) ||
2112
+ (c.type === "text" && c.text.trim().length > 0) ||
2113
+ (c.type === "toolCall" && c.name.trim().length > 0),
2114
+ );
2115
+ if (!onlyEmpty) {
2116
+ this.appendMessage(partial);
2117
+ } else {
2118
+ if (abortController.signal.aborted) {
2119
+ throw new Error("Request was aborted");
2120
+ }
2121
+ }
2122
+ }
2123
+ } catch (err: any) {
2124
+ if (this.#activeRunId !== runId) {
2125
+ return;
2126
+ }
2127
+ const providerCode = providerFailureCode(err);
2128
+ const runtimeFailureCode = abortController.signal.aborted
2129
+ ? "aborted"
2130
+ : (managedLocalErrorDiagnostic(err)?.errorKind ?? providerCode);
2131
+
2132
+ const errorMsg: AgentMessage = {
2133
+ role: "assistant",
2134
+ content: [{ type: "text", text: "" }],
2135
+ api: model.api,
2136
+ provider: model.provider,
2137
+ model: model.id,
2138
+ usage: {
2139
+ input: 0,
2140
+ output: 0,
2141
+ cacheRead: 0,
2142
+ cacheWrite: 0,
2143
+ totalTokens: 0,
2144
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
2145
+ },
2146
+ stopReason: abortController.signal.aborted ? "aborted" : "error",
2147
+ errorMessage: sanitizeAgentFailure(err).message,
2148
+ errorStatus: safeErrorStatus(err),
2149
+ // Local-diagnostic authority (`errorKind` + structured
2150
+ // `bufferOverflow`) comes from ONE identity check: a foreign error
2151
+ // that self-declares a local kind gets neither field, so the parent
2152
+ // receipt never misattributes a provider/stream failure to the local
2153
+ // staging machinery (#4618).
2154
+ ...(managedLocalErrorDiagnostic(err) ?? {}),
2155
+ timestamp: Date.now(),
2156
+ } as AgentMessage;
2157
+
2158
+ // Store the sanitized message only: the raw provider error may carry request
2159
+ // bodies, credentials, or tokens (exact-head review P1).
2160
+ this.#state.error = sanitizeAgentFailure(err).message;
2161
+ this.#emit({
2162
+ type: "agent_failed",
2163
+ // Runtime-authenticated classifiers only: abort comes from the
2164
+ // signal, and a local staging failure comes from the identity-
2165
+ // checked managedLocalErrorDiagnostic — a foreign error that
2166
+ // self-declares a local kind still maps to agent_failed.
2167
+ error: sanitizeAgentFailure(err, runtimeFailureCode),
2168
+ scope: handle.scope,
2169
+ });
2170
+ this.requestRunTerminal(managedLogicalRunOwner ?? runId, {
2171
+ stopReason: abortController.signal.aborted ? "cancelled" : "error",
2172
+ messages: [errorMsg],
2173
+ });
2174
+ } finally {
2175
+ let continuation: ManagedAttemptContinuation | undefined;
2176
+ if (
2177
+ managedOutcome?.type !== "run_terminal" &&
2178
+ (managedDecision?.type === "retry" || managedDecision?.type === "maintenance")
2179
+ ) {
2180
+ continuation = managedDecision.continuation;
2181
+ }
2182
+ const domain = this.#activeResourceCancellationDomain;
2183
+ const continuationReservation =
2184
+ continuation && domain
2185
+ ? this.resourceLedger.reserveProducer(
2186
+ String(managedLogicalRunOwner ?? runId),
2187
+ domain,
2188
+ "post_prompt",
2189
+ "managed-continuation",
2190
+ )
2191
+ : undefined;
2192
+ if (continuation && !continuationReservation?.ok) {
2193
+ this.requestRunTerminal(managedLogicalRunOwner ?? runId, { stopReason: "error" });
2194
+ continuation = undefined;
2195
+ }
2196
+ const ownership: ManagedAttemptContinuationOwnership | undefined = continuationReservation?.ok
2197
+ ? {
2198
+ runId,
2199
+ logicalRunId: managedLogicalRunOwner ?? runId,
2200
+ generation: continuationGeneration,
2201
+ domain: continuationReservation.lease.domain,
2202
+ lease: continuationReservation.lease,
2203
+ handle,
2204
+ isCurrent: () =>
2205
+ this.#continuationGeneration === continuationGeneration && this.#activeRunId === undefined,
2206
+ }
2207
+ : undefined;
2208
+ if (this.#activeRunId === runId) {
2209
+ this.#state.isStreaming = false;
2210
+ this.#state.streamMessage = null;
2211
+ this.#state.pendingToolCalls = new Set<string>();
2212
+ this.#abortController = undefined;
2213
+ this.#activeRunId = undefined;
2214
+ this.#activeResourceRunId = undefined;
2215
+ this.#activeResourceCancellationDomain = undefined;
2216
+ this.#resolveRunningPrompt?.();
2217
+ this.#runningPrompt = undefined;
2218
+ this.#resolveRunningPrompt = undefined;
2219
+ }
2220
+ if (
2221
+ continuesLogicalRun &&
2222
+ !continuation &&
2223
+ !maintenanceInterrupted &&
2224
+ this.#managedLogicalRunOwner === managedLogicalRunOwner
2225
+ ) {
2226
+ this.#managedLogicalRunOwner = undefined;
2227
+ }
2228
+ if (continuation && ownership?.isCurrent()) {
2229
+ try {
2230
+ await continuation(ownership);
2231
+ if (
2232
+ managedDecision?.type === "maintenance" &&
2233
+ this.#terminalizedLogicalRunIds.has(managedLogicalRunOwner ?? runId) &&
2234
+ this.#managedLogicalRunOwner === managedLogicalRunOwner
2235
+ ) {
2236
+ this.#managedLogicalRunOwner = undefined;
2237
+ }
2238
+ if (
2239
+ managedDecision?.type !== "maintenance" &&
2240
+ this.#activeRunId === undefined &&
2241
+ this.#managedLogicalRunOwner === managedLogicalRunOwner
2242
+ ) {
2243
+ this.#managedLogicalRunOwner = undefined;
2244
+ }
2245
+ } catch (err) {
2246
+ if (ownership.isCurrent()) {
2247
+ this.#state.error = sanitizeAgentFailure(err).message;
2248
+ // The documented contract emits the sanitized diagnostic
2249
+ // before the error terminal on this path too (exact-head
2250
+ // review P2).
2251
+ this.#emit({ type: "agent_failed", error: sanitizeAgentFailure(err) });
2252
+ this.requestRunTerminal(managedLogicalRunOwner ?? runId, { stopReason: "error" });
2253
+ if (this.#managedLogicalRunOwner === managedLogicalRunOwner) this.#managedLogicalRunOwner = undefined;
2254
+ }
2255
+ } finally {
2256
+ ownership.lease.closeDiscovery();
2257
+ }
2258
+ }
2259
+ }
2260
+ }
2261
+
2262
+ #emit(e: AgentEvent) {
2263
+ for (const listener of this.#listeners) {
2264
+ try {
2265
+ listener(e);
2266
+ } catch (error) {
2267
+ // Listener isolation (exact-head review P1): a throwing subscriber
2268
+ // must never abort the emitting control path — in particular the
2269
+ // failure catch that publishes agent_failed and then MUST reach its
2270
+ // agent_end terminal boundary. An observer failure is logged as
2271
+ // diagnostic data and never rethrown into the run loop.
2272
+ console.warn("[pi-agent] event listener threw; swallowing:", sanitizeAgentFailure(error));
2273
+ }
2274
+ }
2275
+ }
2276
+
2277
+ /** Calculate total text length from an assistant message's content blocks */
2278
+ #finalizeRun(
2279
+ logicalRunId: ManagedLogicalRunId,
2280
+ event?: Extract<AgentEvent, { type: "agent_end" }>,
2281
+ beforeEvent?: () => void,
2282
+ knownDomain?: RunCancellationDomain,
2283
+ ): void {
2284
+ if (this.#terminalizedLogicalRunIds.has(logicalRunId)) return;
2285
+ const handle = this.#runHandles.get(logicalRunId);
2286
+ if (!handle && !event?.scope) {
2287
+ throw new Error(`finalizeRun: unknown logicalRunId ${logicalRunId} (no attempt handle)`);
2288
+ }
2289
+ const resourceRunId = String(logicalRunId);
2290
+ const boundDomain = this.resourceLedger.lookupDomain(resourceRunId);
2291
+ const domain = boundDomain ?? knownDomain;
2292
+ const terminalReservation = boundDomain
2293
+ ? this.resourceLedger.reserveProducer(resourceRunId, boundDomain, "post_prompt", "terminal-publication")
2294
+ : undefined;
2295
+ this.#terminalizedLogicalRunIds.add(logicalRunId);
2296
+ if (this.#terminalizedLogicalRunIds.size > 256) {
2297
+ this.#terminalizedLogicalRunIds.delete(this.#terminalizedLogicalRunIds.values().next().value!);
2298
+ }
2299
+ const terminalEvent: Extract<AgentEvent, { type: "agent_end" }> = event ?? {
2300
+ type: "agent_end",
2301
+ messages: [],
2302
+ scope: handle?.scope,
2303
+ };
2304
+ if (handle) terminalEvent.scope = handle.scope;
2305
+ if (domain) {
2306
+ setAgentTerminalOwnerContext(terminalEvent, {
2307
+ resourceRunId,
2308
+ domain,
2309
+ });
2310
+ }
2311
+ try {
2312
+ beforeEvent?.();
2313
+ this.#emit(terminalEvent);
2314
+ } finally {
2315
+ try {
2316
+ terminalReservation?.ok && terminalReservation.lease.closeDiscovery();
2317
+ } finally {
2318
+ try {
2319
+ this.resourceLedger.seal(resourceRunId);
2320
+ } finally {
2321
+ this.#runHandles.delete(logicalRunId);
2322
+ }
2323
+ }
2324
+ }
2325
+ }
2326
+
2327
+ #getAssistantTextLength(message: AgentMessage | null): number {
2328
+ if (message?.role !== "assistant" || !Array.isArray(message.content)) {
2329
+ return 0;
2330
+ }
2331
+ let length = 0;
2332
+ for (const block of message.content) {
2333
+ if (block.type === "text") {
2334
+ length += (block as TextContent).text.length;
2335
+ }
2336
+ }
2337
+ return length;
2338
+ }
2339
+
2340
+ /**
2341
+ * Emit a Cursor assistant message split around tool results.
2342
+ * This fixes the ordering issue where tool results appear after the full explanation.
2343
+ *
2344
+ * Output order: Assistant(preamble) -> ToolResults -> Assistant(continuation)
2345
+ */
2346
+ #emitCursorSplitAssistantMessage(assistantMessage: AssistantMessage): void {
2347
+ const buffer = this.#cursorToolResultBuffer;
2348
+ this.#cursorToolResultBuffer = [];
2349
+
2350
+ if (buffer.length === 0) {
2351
+ // No tool results, emit normally
2352
+ this.#state.streamMessage = null;
2353
+ this.appendMessage(assistantMessage);
2354
+ this.#emit({ type: "message_end", message: assistantMessage });
2355
+ return;
2356
+ }
2357
+
2358
+ // Find the split point: minimum text length at first tool call
2359
+ const splitPoint = Math.min(...buffer.map(r => r.textLengthAtCall));
2360
+
2361
+ // Extract text content from assistant message
2362
+ const content = assistantMessage.content;
2363
+ let fullText = "";
2364
+ for (const block of content) {
2365
+ if (block.type === "text") {
2366
+ fullText += block.text;
2367
+ }
2368
+ }
2369
+
2370
+ // If no text or split point is 0 or at/past end, don't split
2371
+ if (fullText.length === 0 || splitPoint <= 0 || splitPoint >= fullText.length) {
2372
+ // Emit assistant message first, then tool results (original behavior but with buffered results)
2373
+ this.#state.streamMessage = null;
2374
+ this.appendMessage(assistantMessage);
2375
+ this.#emit({ type: "message_end", message: assistantMessage });
2376
+
2377
+ // Emit buffered tool results
2378
+ for (const { toolResult } of buffer) {
2379
+ this.#emit({ type: "message_start", message: toolResult });
2380
+ this.appendMessage(toolResult);
2381
+ this.#emit({ type: "message_end", message: toolResult });
2382
+ }
2383
+ return;
2384
+ }
2385
+
2386
+ // Split the text
2387
+ const preambleText = fullText.slice(0, splitPoint);
2388
+ const continuationText = fullText.slice(splitPoint);
2389
+
2390
+ // Create preamble message (text before tools)
2391
+ const preambleContent = content.map(block => {
2392
+ if (block.type === "text") {
2393
+ return { ...block, text: preambleText };
2394
+ }
2395
+ return block;
2396
+ });
2397
+ const preambleMessage: AssistantMessage = {
2398
+ ...assistantMessage,
2399
+ content: preambleContent,
2400
+ };
2401
+
2402
+ // Emit preamble
2403
+ this.#state.streamMessage = null;
2404
+ this.appendMessage(preambleMessage);
2405
+ this.#emit({ type: "message_end", message: preambleMessage });
2406
+
2407
+ // Emit buffered tool results
2408
+ for (const { toolResult } of buffer) {
2409
+ this.#emit({ type: "message_start", message: toolResult });
2410
+ this.appendMessage(toolResult);
2411
+ this.#emit({ type: "message_end", message: toolResult });
2412
+ }
2413
+
2414
+ // Emit continuation message (text after tools) if non-empty
2415
+ const trimmedContinuation = continuationText.trim();
2416
+ if (trimmedContinuation.length > 0) {
2417
+ // Create continuation message with only text content (no thinking/toolCalls)
2418
+ const continuationContent: TextContent[] = [{ type: "text", text: continuationText }];
2419
+ const continuationMessage: AssistantMessage = {
2420
+ ...assistantMessage,
2421
+ content: continuationContent,
2422
+ // Zero out usage for continuation since it's part of same response
2423
+ usage: {
2424
+ input: 0,
2425
+ output: 0,
2426
+ cacheRead: 0,
2427
+ cacheWrite: 0,
2428
+ totalTokens: 0,
2429
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
2430
+ },
2431
+ };
2432
+ this.#emit({ type: "message_start", message: continuationMessage });
2433
+ this.appendMessage(continuationMessage);
2434
+ this.#emit({ type: "message_end", message: continuationMessage });
2435
+ }
2436
+ }
2437
+ }