@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
@@ -0,0 +1,533 @@
1
+ /** Agent class that uses the agent-loop directly.
2
+ * No transport abstraction - calls streamSimple via the loop.
3
+ */
4
+ import { type AssistantMessage, type AssistantMessageEvent, type CursorExecHandlers, type CursorToolResultHandler, type Effort, type ImageContent, type Message, type Model, type ProviderSessionState, type ServiceTier, type SimpleStreamOptions, type ThinkingBudgets, type ToolChoice, type UserMessage } from "@vib-rato/ai";
5
+ import type { AppendOnlyContextManager } from "./append-only-context";
6
+ import type { AttemptRunHandle, AttemptScope } from "./attempt-scope";
7
+ import type { HarmonyAuditEvent } from "./harmony-leak";
8
+ import type { AgentEvent, AgentLoopConfig, AgentMessage, AgentMetadataResolverContext, AgentState, AgentTool, AgentToolContext, ManagedLogicalRunId, RunCancellationDomainBridge, RunResourceLedger, RunTerminalRequest, StreamFn, ToolCallContext } from "./types";
9
+ /**
10
+ * Whether persisted history ends at a point where a new model turn can resume.
11
+ * Assistant-ended histories require an in-memory queued message and are handled
12
+ * separately by `Agent.continue()`.
13
+ */
14
+ export declare function canContinuePersistedHistory(messages: readonly AgentMessage[]): boolean;
15
+ export declare class ManagedCursorInvariantError extends Error {
16
+ constructor(message?: string);
17
+ }
18
+ export declare class AgentBusyError extends Error {
19
+ constructor(message?: string);
20
+ }
21
+ export interface AgentOptions {
22
+ initialState?: Partial<AgentState>;
23
+ /**
24
+ * Converts AgentMessage[] to LLM-compatible Message[] before each LLM call.
25
+ * Default filters to user/assistant/toolResult and converts attachments.
26
+ */
27
+ convertToLlm?: (messages: AgentMessage[]) => Message[] | Promise<Message[]>;
28
+ /**
29
+ * Optional transform applied to context before convertToLlm.
30
+ * Use for context pruning, injecting external context, etc.
31
+ */
32
+ transformContext?: (messages: AgentMessage[], signal?: AbortSignal, scope?: AttemptScope) => Promise<AgentMessage[]>;
33
+ /**
34
+ * Steering mode: "all" = send all steering messages at once, "one-at-a-time" = one per turn
35
+ */
36
+ steeringMode?: "all" | "one-at-a-time";
37
+ /**
38
+ * Follow-up mode: "all" = send all follow-up messages at once, "one-at-a-time" = one per turn
39
+ */
40
+ followUpMode?: "all" | "one-at-a-time";
41
+ /**
42
+ * When to interrupt tool execution for steering messages.
43
+ * - "immediate": check after each tool call (default)
44
+ * - "wait": defer steering until the current turn completes
45
+ */
46
+ interruptMode?: "immediate" | "wait";
47
+ /** Cooperative pause checkpoint passed through to AgentLoopConfig.shouldPause. */
48
+ shouldPause?: AgentLoopConfig["shouldPause"];
49
+ /**
50
+ * API format for Kimi Code provider: "openai" or "anthropic" (default: "anthropic")
51
+ */
52
+ kimiApiFormat?: "openai" | "anthropic";
53
+ /** Hint that websocket transport should be preferred when supported by the provider implementation. */
54
+ preferWebsockets?: boolean;
55
+ /**
56
+ * Custom stream function (for proxy backends, etc.). Default uses streamSimple.
57
+ */
58
+ streamFn?: StreamFn;
59
+ /**
60
+ * Optional session identifier forwarded to LLM providers.
61
+ * Used by providers that support session-based caching (e.g., OpenAI code provider).
62
+ */
63
+ sessionId?: string;
64
+ /** Provider-facing cache/session affinity identifier. */
65
+ providerSessionId?: string;
66
+ /**
67
+ * Shared provider state map for session-scoped transport/session caches.
68
+ */
69
+ providerSessionState?: Map<string, ProviderSessionState>;
70
+ /**
71
+ * Resolves an API key dynamically for each LLM call.
72
+ * Useful for expiring tokens (e.g., GitHub Copilot OAuth).
73
+ */
74
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
75
+ getAuthCredentialType?: (provider: string) => "api_key" | "oauth" | undefined;
76
+ /**
77
+ * Inspect or replace provider payloads before they are sent.
78
+ */
79
+ onPayload?: SimpleStreamOptions["onPayload"];
80
+ /**
81
+ * Inspect provider response metadata after headers arrive and before streaming body consumption.
82
+ */
83
+ onResponse?: SimpleStreamOptions["onResponse"];
84
+ /**
85
+ * Inspect raw Server-Sent Events from HTTP streaming providers.
86
+ */
87
+ onSseEvent?: SimpleStreamOptions["onSseEvent"];
88
+ /**
89
+ * Inspect assistant streaming events before they are emitted to subscribers.
90
+ * Use this when abort decisions must happen before buffered events continue flowing.
91
+ */
92
+ onAssistantMessageEvent?: (message: AssistantMessage, event: AssistantMessageEvent) => void;
93
+ /** Called for non-content tool-choice incapability stream events. */
94
+ onToolChoiceIncapability?: AgentLoopConfig["onToolChoiceIncapability"];
95
+ /**
96
+ * Called when GPT-5 Harmony protocol leakage is detected and mitigated.
97
+ */
98
+ onHarmonyLeak?: (event: HarmonyAuditEvent) => void | Promise<void>;
99
+ /**
100
+ * Custom token budgets for thinking levels (token-based providers only).
101
+ */
102
+ thinkingBudgets?: ThinkingBudgets;
103
+ /**
104
+ * Sampling temperature for LLM calls. `undefined` uses provider default.
105
+ */
106
+ temperature?: number;
107
+ /** Additional sampling controls for providers that support them. */
108
+ topP?: number;
109
+ topK?: number;
110
+ minP?: number;
111
+ presencePenalty?: number;
112
+ repetitionPenalty?: number;
113
+ serviceTier?: ServiceTier;
114
+ /**
115
+ * If true, request that the underlying provider omit reasoning/thinking summaries
116
+ * from the response. The model still reasons internally; only the human-readable
117
+ * summary stream is suppressed. Useful when the UI hides thinking blocks anyway.
118
+ */
119
+ hideThinkingSummary?: boolean;
120
+ /**
121
+ * Maximum delay in milliseconds to wait for a retry when the server requests a long wait.
122
+ * If the server's requested delay exceeds this value, the request fails immediately,
123
+ * allowing higher-level retry logic to handle it with user visibility.
124
+ * Default: 60000 (60 seconds). Set to 0 to disable the cap.
125
+ */
126
+ maxRetryDelayMs?: number;
127
+ /** Provider request retry budget. Counts retries, not the initial attempt. */
128
+ requestMaxRetries?: number;
129
+ /** Provider stream replay retry budget. Counts retries, not the initial attempt. */
130
+ streamMaxRetries?: number;
131
+ /** Explicit first-event stream watchdog override in milliseconds. Set to 0 to disable. */
132
+ streamFirstEventTimeoutMs?: number;
133
+ /**
134
+ * Provides tool execution context, resolved per tool call.
135
+ * Use for late-bound UI or session state access.
136
+ */
137
+ getToolContext?: (toolCall?: ToolCallContext) => AgentToolContext | undefined;
138
+ /**
139
+ * Optional transform applied to tool call arguments before execution.
140
+ * Use for deobfuscating secrets or rewriting arguments.
141
+ */
142
+ transformToolCallArguments?: (args: Record<string, unknown>, toolName: string) => Record<string, unknown>;
143
+ /** Enable intent tracing schema injection/stripping in the harness. */
144
+ intentTracing?: boolean;
145
+ /** Dynamic tool choice override, resolved per LLM call. */
146
+ getToolChoice?: () => ToolChoice | undefined;
147
+ /**
148
+ * Cursor exec handlers for local tool execution.
149
+ */
150
+ cursorExecHandlers?: CursorExecHandlers;
151
+ /**
152
+ * Cursor tool result callback for exec tool responses.
153
+ */
154
+ cursorOnToolResult?: CursorToolResultHandler;
155
+ /**
156
+ * Called after a tool call has been validated and is about to execute.
157
+ * See {@link AgentLoopConfig.beforeToolCall} for full semantics.
158
+ */
159
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
160
+ /**
161
+ * Called after a tool finishes executing, before `tool_execution_end` and the tool-result
162
+ * message are emitted. See {@link AgentLoopConfig.afterToolCall} for full semantics.
163
+ */
164
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
165
+ /** Invoked with the follow-up messages dequeued for the next turn (reassignable). */
166
+ onFollowUpConsumed?: AgentLoopConfig["onFollowUpConsumed"];
167
+ /** Invoked with the steering messages dequeued mid-run for the current turn (reassignable). */
168
+ onSteeringConsumed?: AgentLoopConfig["onSteeringConsumed"];
169
+ /**
170
+ * Opt-in OpenTelemetry instrumentation. Passing `{}` enables the loop's
171
+ * GenAI-semantic-convention spans using the global tracer provider. See
172
+ * {@link AgentLoopConfig.telemetry} for the full surface.
173
+ */
174
+ telemetry?: AgentLoopConfig["telemetry"];
175
+ /**
176
+ * Immutable context mode — stabilizes system prompt + tool spec bytes
177
+ * across turns so DeepSeek/Anthropic prefix caches hit at maximum rate.
178
+ */
179
+ appendOnlyContext?: AppendOnlyContextManager;
180
+ }
181
+ export interface AgentPromptOptions {
182
+ /** One-shot transient recovery instruction sent only to the provider for the next assistant request; never committed to durable history. */
183
+ transientRecoveryMessage?: UserMessage;
184
+ toolChoice?: ToolChoice;
185
+ /** Disable transport replay; fallback accounting is owned by the caller. */
186
+ fallbackManaged?: boolean;
187
+ /** Continue a cooperative maintenance checkpoint under its existing logical run and cancellation domain. */
188
+ maintenanceContinuation?: boolean;
189
+ /** Called synchronously after this invocation claims the agent run, before asynchronous provider work. */
190
+ onRunAccepted?: (handle: AttemptRunHandle, acceptance: {
191
+ consumedQueuedMessages: readonly AgentMessage[];
192
+ }) => void;
193
+ /** Called once immediately before every managed upstream request. */
194
+ nextFallbackAttempt?: AgentLoopConfig["nextFallbackAttempt"];
195
+ /** Called after a managed upstream request is accepted and committed. */
196
+ onManagedAttemptAccepted?: AgentLoopConfig["onManagedAttemptAccepted"];
197
+ /** Receives a discarded managed attempt without exposing assistant lifecycle events. */
198
+ onManagedAttemptOutcome?: AgentLoopConfig["onManagedAttemptOutcome"];
199
+ }
200
+ export type AgentQueueSnapshot = {
201
+ steering: AgentMessage[];
202
+ followUp: AgentMessage[];
203
+ };
204
+ export declare class Agent {
205
+ #private;
206
+ get intentTracing(): boolean;
207
+ readonly resourceLedger: RunResourceLedger;
208
+ bindRunCancellationDomainBridge(bridge: RunCancellationDomainBridge, agentSessionClaimKey?: object): void;
209
+ /** Mint a side-attempt scope and its authority unregister function. */
210
+ mintSideAttemptScope(): {
211
+ scope: AttemptScope;
212
+ dispose: () => void;
213
+ };
214
+ /** Return the Agent-owned attempt scope authority for session record injection. */
215
+ getAttemptScopeAuthority(): import("./attempt-scope").AttemptScopeAuthority;
216
+ /**
217
+ * Observe each main-attempt scope synchronously, before any provider or
218
+ * extension-capable lifecycle work can begin.
219
+ */
220
+ setMainAttemptScopeObserver(observer: ((scope: AttemptScope) => void) | undefined): void;
221
+ streamFn: StreamFn;
222
+ getApiKey?: (provider: string) => Promise<string | undefined> | string | undefined;
223
+ getAuthCredentialType?: (provider: string) => "api_key" | "oauth" | undefined;
224
+ /**
225
+ * Hook invoked after tool arguments are validated and before execution.
226
+ * Reassign at any time to swap the implementation (e.g. on extension reload).
227
+ */
228
+ beforeToolCall?: AgentLoopConfig["beforeToolCall"];
229
+ /**
230
+ * Hook invoked after tool execution and before `tool_execution_end` / tool-result
231
+ * message emission. Reassign at any time to swap the implementation.
232
+ */
233
+ afterToolCall?: AgentLoopConfig["afterToolCall"];
234
+ /** Invoked with the follow-up messages dequeued for the next turn. Reassign at any time. */
235
+ onFollowUpConsumed?: AgentLoopConfig["onFollowUpConsumed"];
236
+ /** Invoked with the steering messages dequeued mid-run for the current turn. Reassign at any time. */
237
+ onSteeringConsumed?: AgentLoopConfig["onSteeringConsumed"];
238
+ constructor(opts?: AgentOptions);
239
+ /**
240
+ * Get the current session ID used for provider caching.
241
+ */
242
+ get sessionId(): string | undefined;
243
+ /**
244
+ * Set the session ID for provider caching.
245
+ * Call this when switching sessions (new session, branch, resume).
246
+ */
247
+ set sessionId(value: string | undefined);
248
+ get providerSessionId(): string | undefined;
249
+ set providerSessionId(value: string | undefined);
250
+ /**
251
+ * Whether websocket transport is preferred when the provider implementation
252
+ * supports it. Read by maintenance one-shot calls (compaction, handoff,
253
+ * branch summary) so they forward the same transport preference as live turns.
254
+ */
255
+ get preferWebsockets(): boolean | undefined;
256
+ /**
257
+ * Static metadata forwarded to every API request when no resolver is installed
258
+ * (e.g. `metadata.user_id` for Anthropic session attribution). Setting this
259
+ * clears any installed resolver.
260
+ *
261
+ * For live/provider-aware metadata (e.g. Anthropic OAuth `account_uuid` that
262
+ * must reflect the credential selected per-request), use
263
+ * {@link setMetadataResolver} and read via {@link metadataForProvider}.
264
+ */
265
+ get metadata(): Record<string, unknown> | undefined;
266
+ set metadata(value: Record<string, unknown> | undefined);
267
+ /**
268
+ * Resolve request metadata for the given provider at call time. When a
269
+ * resolver is installed via {@link setMetadataResolver}, it is invoked with
270
+ * the provider string so the result can be scoped (e.g. `account_uuid` is
271
+ * only included for `"anthropic"` requests). Falls back to the static
272
+ * {@link metadata} value when no resolver is set.
273
+ */
274
+ metadataForProvider(provider: string, model?: Model, transport?: AgentMetadataResolverContext["transport"]): Record<string, unknown> | undefined;
275
+ /**
276
+ * Install a function that resolves request metadata at call time. The
277
+ * resolver receives the target provider string and can gate provider-specific
278
+ * fields (e.g. `account_uuid` only for `"anthropic"`). Invoked per LLM
279
+ * request by `agent-loop` after `getApiKey` selects the session-sticky
280
+ * credential. Pass `undefined` to clear and revert to the static
281
+ * {@link metadata} value.
282
+ */
283
+ setMetadataResolver(resolver: ((context: AgentMetadataResolverContext) => Record<string, unknown> | undefined) | undefined): void;
284
+ /**
285
+ * Read the active OpenTelemetry configuration. Returns `undefined` when
286
+ * instrumentation is disabled. Callers spawning child runs (e.g. subagent
287
+ * dispatch) forward this to the child's loop so its spans appear under the
288
+ * parent's active context with the subagent's own identity stamped.
289
+ */
290
+ get telemetry(): AgentLoopConfig["telemetry"] | undefined;
291
+ /**
292
+ * Replace the active OpenTelemetry configuration. Pass `undefined` to
293
+ * disable instrumentation. Applies to the *next* `agentLoop` invocation —
294
+ * in-flight loops keep the configuration they started with.
295
+ */
296
+ setTelemetry(telemetry: AgentLoopConfig["telemetry"] | undefined): void;
297
+ /**
298
+ * Get provider-scoped mutable session state store.
299
+ */
300
+ get providerSessionState(): Map<string, ProviderSessionState> | undefined;
301
+ /**
302
+ * Set provider-scoped mutable session state store.
303
+ */
304
+ set providerSessionState(value: Map<string, ProviderSessionState> | undefined);
305
+ /**
306
+ * Get the current thinking budgets.
307
+ */
308
+ get thinkingBudgets(): ThinkingBudgets | undefined;
309
+ /**
310
+ * Set custom thinking budgets for token-based providers.
311
+ */
312
+ set thinkingBudgets(value: ThinkingBudgets | undefined);
313
+ /**
314
+ * Get the current sampling temperature.
315
+ */
316
+ get temperature(): number | undefined;
317
+ /**
318
+ * Set sampling temperature for LLM calls. `undefined` uses provider default.
319
+ */
320
+ set temperature(value: number | undefined);
321
+ get topP(): number | undefined;
322
+ set topP(value: number | undefined);
323
+ get topK(): number | undefined;
324
+ set topK(value: number | undefined);
325
+ get minP(): number | undefined;
326
+ set minP(value: number | undefined);
327
+ get presencePenalty(): number | undefined;
328
+ set presencePenalty(value: number | undefined);
329
+ get repetitionPenalty(): number | undefined;
330
+ set repetitionPenalty(value: number | undefined);
331
+ get serviceTier(): ServiceTier | undefined;
332
+ set serviceTier(value: ServiceTier | undefined);
333
+ get hideThinkingSummary(): boolean | undefined;
334
+ set hideThinkingSummary(value: boolean | undefined);
335
+ /**
336
+ * Get the current max retry delay in milliseconds.
337
+ */
338
+ get maxRetryDelayMs(): number | undefined;
339
+ /**
340
+ * Set the maximum delay to wait for server-requested retries.
341
+ * Set to 0 to disable the cap.
342
+ */
343
+ set maxRetryDelayMs(value: number | undefined);
344
+ get requestMaxRetries(): number | undefined;
345
+ set requestMaxRetries(value: number | undefined);
346
+ get streamMaxRetries(): number | undefined;
347
+ set streamMaxRetries(value: number | undefined);
348
+ get streamFirstEventTimeoutMs(): number | undefined;
349
+ set streamFirstEventTimeoutMs(value: number | undefined);
350
+ get state(): AgentState;
351
+ get contextRevision(): number;
352
+ get appendOnlyContext(): AppendOnlyContextManager | undefined;
353
+ setAppendOnlyContext(manager?: AppendOnlyContextManager): void;
354
+ subscribe(fn: (e: AgentEvent) => void): () => void;
355
+ setProviderResponseInterceptor(fn: SimpleStreamOptions["onResponse"] | undefined): void;
356
+ setRawSseEventInterceptor(fn: SimpleStreamOptions["onSseEvent"] | undefined): void;
357
+ setAssistantMessageEventInterceptor(fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined): void;
358
+ setProvisionalAssistantMessageEventInterceptor(fn: ((message: AssistantMessage, event: AssistantMessageEvent) => void) | undefined): void;
359
+ setOnBeforeYield(fn: (() => Promise<void> | void) | undefined): void;
360
+ setShouldPause(fn: AgentLoopConfig["shouldPause"] | undefined): void;
361
+ /** The currently installed cooperative pause checkpoint, if any. */
362
+ get shouldPause(): AgentLoopConfig["shouldPause"] | undefined;
363
+ /**
364
+ * Fence old-turn steering admission.
365
+ *
366
+ * The loop polls steering UPSTREAM of its pause checkpoint (and again on the
367
+ * immediate-interrupt path), so a cooperative stop alone cannot prevent one
368
+ * more old-turn model call once a steering message has already been dequeued.
369
+ * While the fence returns true the poll yields no messages AND does not
370
+ * dequeue, so the queue survives intact for the next turn.
371
+ */
372
+ setSteeringAdmissionFence(fn: (() => boolean) | undefined): void;
373
+ setMaintainContext(fn: AgentLoopConfig["maintainContext"] | undefined): void;
374
+ /**
375
+ * Publish an event produced OUTSIDE the agent loop (a provider that executed the tool
376
+ * itself, a host bridge, a replay).
377
+ *
378
+ * Identity is the PRODUCER's to prove: whoever dispatched the call binds the tool object
379
+ * it actually ran (see `bindDispatchedToolIdentity`) before handing the event here, and
380
+ * that binding is never touched from this side. Re-resolving `event.toolName` against the
381
+ * mutable current tool list would let a mid-run `setTools`, MCP reload, or plain name
382
+ * collision overwrite a proven object with one that never ran — and would invent an
383
+ * identity for replays and host bridges that never executed an AgentTool at all. An
384
+ * unbound external event stays unbound; unproven provenance is `custom`.
385
+ */
386
+ emitExternalEvent(event: AgentEvent): void;
387
+ createExternalEventEmitterForCurrentRun(): ((event: AgentEvent) => void) | undefined;
388
+ setSystemPrompt(v: string[]): void;
389
+ setModel(m: Model | undefined): void;
390
+ setThinkingLevel(l: Effort | undefined): void;
391
+ setSteeringMode(mode: "all" | "one-at-a-time"): void;
392
+ getSteeringMode(): "all" | "one-at-a-time";
393
+ setFollowUpMode(mode: "all" | "one-at-a-time"): void;
394
+ getFollowUpMode(): "all" | "one-at-a-time";
395
+ setInterruptMode(mode: "immediate" | "wait"): void;
396
+ getInterruptMode(): "immediate" | "wait";
397
+ setTools(t: AgentTool<any>[]): void;
398
+ replaceMessages(ms: AgentMessage[], options?: {
399
+ historyRewrite?: {
400
+ reason: string;
401
+ preserveSeededPrefix?: boolean;
402
+ };
403
+ }): void;
404
+ appendMessage(m: AgentMessage): void;
405
+ popMessage(): AgentMessage | undefined;
406
+ /**
407
+ * For callers that mutate committed messages or the system prompt in place
408
+ * outside Agent-owned mutators.
409
+ */
410
+ touchContext(): void;
411
+ /**
412
+ * Queue a steering message to interrupt the agent mid-run.
413
+ * Delivered after current tool execution, skips remaining tools.
414
+ */
415
+ steer(m: AgentMessage): void;
416
+ /**
417
+ * Resolves when a steering message is queued (or is already queued), or when
418
+ * `signal` aborts. The queue is not consumed. Long observation tools use this
419
+ * to end their wait early so a busy user message is handled at the next tool
420
+ * boundary instead of after the full wait window.
421
+ */
422
+ waitForSteeringArrival(signal: AbortSignal): Promise<void>;
423
+ /**
424
+ * Queue a follow-up message to be processed after the agent finishes.
425
+ * Delivered only when agent has no more tool calls or steering messages.
426
+ *
427
+ * `forceOneAtATime` lets UI composer queues preserve prompt-by-prompt
428
+ * delivery even when the session-wide follow-up mode is set to `all` for
429
+ * other integration paths.
430
+ */
431
+ followUp(m: AgentMessage, options?: {
432
+ forceOneAtATime?: boolean;
433
+ }): void;
434
+ clearSteeringQueue(): void;
435
+ clearFollowUpQueue(): void;
436
+ clearAllQueues(): void;
437
+ hasQueuedMessages(): boolean;
438
+ hasQueuedSteering(): boolean;
439
+ /**
440
+ * Snapshot the steering queue without mutating it. Used to preserve queued
441
+ * steering across maintenance ops (compaction/handoff) that call reset().
442
+ */
443
+ snapshotSteering(): AgentMessage[];
444
+ /**
445
+ * Restore previously snapshotted steering messages ahead of any newly
446
+ * queued ones. No-op for an empty snapshot.
447
+ */
448
+ restoreSteering(messages: AgentMessage[]): void;
449
+ /** Snapshot the follow-up queue without mutating it. */
450
+ snapshotFollowUp(): AgentMessage[];
451
+ /** Restore previously snapshotted follow-up messages ahead of any newly queued ones. */
452
+ restoreFollowUp(messages: AgentMessage[]): void;
453
+ /** Snapshot both executable queues as one atomic session-level view. */
454
+ snapshotQueues(): AgentQueueSnapshot;
455
+ /** Replace both executable queues with a prior snapshot. */
456
+ restoreQueues(snapshot: AgentQueueSnapshot): void;
457
+ /**
458
+ * Remove and return the last steering message from the queue (LIFO).
459
+ * Used by dequeue keybinding.
460
+ */
461
+ popLastSteer(): AgentMessage | undefined;
462
+ removeSteerAt(index: number): AgentMessage | undefined;
463
+ moveSteer(fromIndex: number, toIndex: number): boolean;
464
+ /**
465
+ * Remove and return the last follow-up message from the queue (LIFO).
466
+ * Used by dequeue keybinding.
467
+ */
468
+ popLastFollowUp(): AgentMessage | undefined;
469
+ removeFollowUpAt(index: number): AgentMessage | undefined;
470
+ moveFollowUp(fromIndex: number, toIndex: number): boolean;
471
+ /**
472
+ * Remove ALL queued STEERING messages without touching the follow-up queue.
473
+ * Used by the terminal-abort path to purge steering queued for the aborted
474
+ * turn (the loop may exit on the abort signal without polling it); the
475
+ * follow-up queue is preserved because it may carry owned-completion
476
+ * resumes that must still deliver.
477
+ */
478
+ clearSteeringMessages(): void;
479
+ /**
480
+ * Remove queued steering/follow-up messages matching `predicate`, preserving
481
+ * order of the rest. `scope` restricts the removal to one queue — the
482
+ * terminal-abort steering purge must not wipe the follow-up queue, which
483
+ * the owned-completion resume policy preserves.
484
+ */
485
+ removeQueuedMessages(predicate: (message: AgentMessage) => boolean, scope?: "both" | "steering" | "followUp"): {
486
+ steering: number;
487
+ followUp: number;
488
+ total: number;
489
+ };
490
+ clearMessages(): void;
491
+ abort(): void;
492
+ /**
493
+ * Force the current run out of the busy/streaming state when cooperative abort
494
+ * did not drain. The abandoned provider/tool stream may still settle later, so
495
+ * #runLoop guards every state mutation with a run id.
496
+ */
497
+ forceAbort(reason?: string, logicalRunId?: ManagedLogicalRunId | number): boolean;
498
+ waitForIdle(): Promise<void>;
499
+ /** The active per-attempt run identifier. */
500
+ get activeRunId(): number | undefined;
501
+ /** Stable resource ownership identifier for the active prompt run. */
502
+ get activeResourceRunId(): string | undefined;
503
+ /**
504
+ * Stable identifier for the active managed logical run, shared by every retry
505
+ * attempt. Pass this value to requestRunTerminal(); never retain activeRunId
506
+ * for managed terminal completion.
507
+ */
508
+ get currentManagedLogicalRunId(): ManagedLogicalRunId | undefined;
509
+ /**
510
+ * Request terminal completion through the single logical-run keyed finalizer.
511
+ *
512
+ * For managed runs, logicalRunId must be currentManagedLogicalRunId from any
513
+ * attempt in the retry chain. Non-managed runs use their activeRunId. Terminal
514
+ * requests with messages emit a committed message_start/message_end lifecycle
515
+ * for each diagnostic before agent_end. Requests without messages (such as
516
+ * cancellation) emit only agent_end.
517
+ */
518
+ requestRunTerminal(logicalRunId: ManagedLogicalRunId, request: RunTerminalRequest): boolean;
519
+ reset(): void;
520
+ /** Send a prompt with an AgentMessage */
521
+ prompt(message: AgentMessage | AgentMessage[], options?: AgentPromptOptions): Promise<void>;
522
+ prompt(input: string, options?: AgentPromptOptions): Promise<void>;
523
+ prompt(input: string, images?: ImageContent[], options?: AgentPromptOptions): Promise<void>;
524
+ /**
525
+ * Continue from current context (used for retries and resuming queued messages).
526
+ */
527
+ continue(options?: AgentPromptOptions): Promise<void>;
528
+ /**
529
+ * Continue by consuming queued steering/follow-up messages without replaying
530
+ * the current non-assistant tail.
531
+ */
532
+ continueQueuedMessages(options?: AgentPromptOptions): Promise<void>;
533
+ }
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Append-only context mode — stabilizes the byte prefix sent to the LLM
3
+ * across turns so provider prefix caches (DeepSeek, Anthropic, etc.)
4
+ * hit at the maximum possible rate.
5
+ *
6
+ * Two mechanisms:
7
+ *
8
+ * 1. **StablePrefix** — system prompt + tool specs are computed once
9
+ * and frozen. Subsequent turns reuse the exact same byte sequence
10
+ * unless `invalidate()` is called (e.g. after MCP reconnect).
11
+ *
12
+ * 2. **AppendOnlyLog** — messages only grow; prior turns are never
13
+ * re-serialized. Combined with a stable prefix, only the user's new
14
+ * message delta is a cache miss each turn.
15
+ */
16
+ import type { Context, Message, Tool } from "@vib-rato/ai";
17
+ import type { AgentContext } from "./types";
18
+ /** Frozen system prompt + tool spec snapshot. */
19
+ export interface StablePrefixSnapshot {
20
+ systemPrompt: string[];
21
+ tools: Tool[];
22
+ fingerprint: string;
23
+ }
24
+ /** Options threaded through `build()` so the snapshot reflects loop-time settings. */
25
+ export interface BuildOptions {
26
+ /** Inject the `_i` intent field into tool schemas (must match agent-loop's normalizeTools). */
27
+ intentTracing: boolean;
28
+ }
29
+ /**
30
+ * A frozen prefix (system prompt + tools) that produces stable byte
31
+ * sequences across `build()` calls.
32
+ *
33
+ * The first `build()` snapshots the live state. Subsequent calls reuse
34
+ * the cached copy until `invalidate()` is called or the live state's
35
+ * fingerprint changes.
36
+ */
37
+ export declare class StablePrefix {
38
+ #private;
39
+ get fingerprint(): string;
40
+ get version(): number;
41
+ get built(): boolean;
42
+ exportSnapshot(): StablePrefixSnapshot | null;
43
+ importSnapshot(snapshot: StablePrefixSnapshot, options: BuildOptions): void;
44
+ /**
45
+ * Build or rebuild from live context.
46
+ * Returns `true` if the prefix actually changed (cache miss imminent).
47
+ */
48
+ build(context: AgentContext, options: BuildOptions): boolean;
49
+ /** Force rebuild on the next `build()` call. */
50
+ invalidate(): void;
51
+ /**
52
+ * Returns the cached prefix.
53
+ * @throws if `build()` was never called.
54
+ */
55
+ toContext(): {
56
+ systemPrompt: string[];
57
+ tools: Tool[];
58
+ };
59
+ }
60
+ /**
61
+ * Append-only message log at the `Message[]` (provider-level) layer.
62
+ *
63
+ * The only mutation path is `replaceTail()`, reserved for compaction.
64
+ * Every other operation is append-only.
65
+ */
66
+ export declare class AppendOnlyLog {
67
+ #private;
68
+ get length(): number;
69
+ append(message: any): void;
70
+ extend(messages: any[]): void;
71
+ /** Replace the last entry — only legal for compaction. */
72
+ replaceTail(replacement: any): void;
73
+ /** Returns a shallow copy of all entries. */
74
+ toMessages(): Message[];
75
+ /** Direct readonly access for in-place inspection. */
76
+ entries(): readonly Message[];
77
+ clear(): void;
78
+ }
79
+ /**
80
+ * Manages a stable prefix + append-only log for the agent loop.
81
+ *
82
+ * Call `build(context)` each turn to get a `Context` with stable
83
+ * `systemPrompt` and `tools` and append-only messages. Call
84
+ * `syncMessages(normalizedMessages)` after `convertToLlm` each
85
+ * turn to keep the log in sync.
86
+ *
87
+ * Example:
88
+ * ```
89
+ * const mgr = new AppendOnlyContextManager();
90
+ * const ctx = mgr.build(context); // first call snapshots prefix
91
+ * mgr.syncMessages(normalized); // grow the log
92
+ * ctx = mgr.build(context); // subsequent calls use cache
93
+ * ```
94
+ */
95
+ export interface AppendOnlyContextManagerOptions {
96
+ /**
97
+ * Invoked whenever the stable prefix fingerprint changes on `build()` (a
98
+ * provider prompt-cache prefix reset). Used for per-session diagnostics; must
99
+ * not throw. `from` is `<unbuilt>` on the first build.
100
+ */
101
+ readonly onPrefixChange?: (info: {
102
+ from: string;
103
+ to: string;
104
+ version: number;
105
+ }) => void;
106
+ }
107
+ export declare class AppendOnlyContextManager {
108
+ #private;
109
+ readonly prefix: StablePrefix;
110
+ readonly log: AppendOnlyLog;
111
+ constructor(options?: AppendOnlyContextManagerOptions);
112
+ static forkFromSeed(args: {
113
+ prefixSnapshot?: StablePrefixSnapshot;
114
+ messages?: readonly Message[];
115
+ options: BuildOptions;
116
+ }): AppendOnlyContextManager;
117
+ build(context: AgentContext, options: BuildOptions): Context;
118
+ /**
119
+ * Sync normalized (provider-level) messages into the append-only log.
120
+ *
121
+ * Detects both compaction (shorter array) and in-place rewrites
122
+ * (same length, changed content via a rolling digest).
123
+ */
124
+ syncMessages(normalizedMessages: any[]): void;
125
+ seedNormalizedMessages(messages: readonly Message[], options?: {
126
+ reset?: boolean;
127
+ }): void;
128
+ /** Reset prefix + log for a model/provider switch while mode stays active. */
129
+ invalidateForModelChange(): void;
130
+ /** Reset the sync cursor AND clear the log. */
131
+ resetSyncCursor(): void;
132
+ appendMessage(message: any): void;
133
+ replaceTailMessage(message: any): void;
134
+ /** Release provider-normalized retainers as one history-rewrite transaction. */
135
+ releaseAfterHistoryRewrite(options?: {
136
+ preserveSeededPrefix?: boolean;
137
+ }): void;
138
+ invalidate(): void;
139
+ reset(context: AgentContext, options: BuildOptions): void;
140
+ }
141
+ export declare function cloneJson<T>(value: T): T;