@kodax-ai/kodax 0.7.43 → 0.7.44

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 (41) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/chunks/{chunk-YMRZBS4G.js → chunk-35BDEEC5.js} +1 -1
  3. package/dist/chunks/{chunk-7G5PSL6C.js → chunk-4YPL2UVZ.js} +253 -235
  4. package/dist/chunks/chunk-DI2G3YWL.js +31 -0
  5. package/dist/chunks/chunk-HHQ7YTGM.js +425 -0
  6. package/dist/chunks/chunk-QHILHQBB.js +519 -0
  7. package/dist/chunks/{chunk-IYSK7LUK.js → chunk-RUDYNAK7.js} +1 -1
  8. package/dist/chunks/{compaction-config-3E57ABCT.js → compaction-config-NAPRF7XR.js} +1 -1
  9. package/dist/chunks/{construction-bootstrap-JR63KI5N.js → construction-bootstrap-PHTGBRNU.js} +1 -1
  10. package/dist/chunks/dist-CCYBJJZY.js +2 -0
  11. package/dist/chunks/{dist-XANXEVTU.js → dist-RHIHZAYX.js} +1 -1
  12. package/dist/chunks/{utils-HQ2QCKJA.js → utils-TV3UYCHQ.js} +1 -1
  13. package/dist/index.d.ts +8 -8
  14. package/dist/index.js +2 -2
  15. package/dist/kodax_cli.js +624 -589
  16. package/dist/provider-capabilities.json +167 -0
  17. package/dist/sdk-agent.d.ts +62 -7
  18. package/dist/sdk-agent.js +1 -1
  19. package/dist/sdk-coding.d.ts +367 -13
  20. package/dist/sdk-coding.js +1 -1
  21. package/dist/sdk-llm.d.ts +1 -1
  22. package/dist/sdk-llm.js +1 -1
  23. package/dist/sdk-mcp.js +1 -1
  24. package/dist/sdk-repl.d.ts +6 -6
  25. package/dist/sdk-repl.js +1 -1
  26. package/dist/sdk-session.d.ts +2 -2
  27. package/dist/sdk-session.js +1 -1
  28. package/dist/sdk-skills.js +1 -1
  29. package/dist/types-chunks/{bash-prefix-extractor.d-DMrGImMl.d.ts → bash-prefix-extractor.d-DdoSeghD.d.ts} +417 -5
  30. package/dist/types-chunks/file-tracker.d-DOfaoCbJ.d.ts +633 -0
  31. package/dist/types-chunks/{resolver.d-CA68_NeH.d.ts → resolver.d-B7ZnVuuf.d.ts} +16 -13
  32. package/dist/types-chunks/{storage.d-DPAEX7zS.d.ts → storage.d-DFD9ln5c.d.ts} +1 -1
  33. package/dist/types-chunks/{file-tracker.d-zaLZeNBK.d.ts → types.d-DM8zEJgF.d.ts} +1029 -535
  34. package/dist/types-chunks/{types.d-mM8vqvhT.d.ts → types.d-HBbWT-iA.d.ts} +41 -3
  35. package/dist/types-chunks/{utils.d-DkLZD_wa.d.ts → utils.d-C5fzCE9W.d.ts} +3 -3
  36. package/package.json +2 -2
  37. package/dist/chunks/chunk-K75O2CAE.js +0 -31
  38. package/dist/chunks/chunk-UG4262JI.js +0 -502
  39. package/dist/chunks/chunk-VHKAJDQD.js +0 -425
  40. package/dist/chunks/dist-KWHUKXEL.js +0 -2
  41. package/dist/types-chunks/types.d-CKJtjo-6.d.ts +0 -1127
@@ -1,1127 +0,0 @@
1
- import { X as KodaXToolDefinition, m as KodaXMessage, D as KodaXReasoningMode, Z as KodaXToolResultContentItem, T as KodaXThinkingBlock, G as KodaXRedactedThinkingBlock } from './types.d-B1uGoVTE.js';
2
- import { a as KodaXCompactMemorySeed, h as KodaXSessionArtifactLedgerEntry } from './types.d-mM8vqvhT.js';
3
-
4
- /**
5
- * Layer A Primitive: Agent / Handoff / Guardrail / AgentReasoningProfile
6
- *
7
- * FEATURE_080 (v0.7.23): Agent-as-data types. Declarative dataclass shape.
8
- * The runtime counterpart is `Runner` in `./runner.ts`.
9
- *
10
- * History: extracted to `@kodax-ai/core` in FEATURE_082 (v0.7.24); merged back
11
- * into `@kodax-ai/agent` in v0.7.35.1 FEATURE_142 (single-consumer rule —
12
- * @kodax-ai/core had only @kodax-ai/coding as consumer). `@kodax-ai/coding` retains
13
- * a barrel re-export for batteries-included consumers.
14
- *
15
- * Status: @experimental — API shape may be refined during v0.7.x. Used by
16
- * the task-engine rewrite in FEATURE_084 (v0.7.26).
17
- *
18
- * Guardrail and AgentReasoningProfile are declared here but their runtime
19
- * behavior is deferred:
20
- * - Guardrail runtime → FEATURE_085 (v0.7.26)
21
- * - AgentReasoningProfile behavior → FEATURE_078 (v0.7.29)
22
- */
23
-
24
- /**
25
- * Reasoning depth / mode selector. Alias for `KodaXReasoningMode` to keep the
26
- * Layer A surface independent of the `KodaX*` brand; unified during the prefix
27
- * cleanup in FEATURE_086 (v0.7.27).
28
- */
29
- type ReasoningDepth = KodaXReasoningMode;
30
- /**
31
- * Tool binding accepted by an `Agent`. Layer A treats tools as opaque
32
- * definitions; the executor lives in `@kodax-ai/coding` and is wired up by the
33
- * Runner when it dispatches through `runKodaX`.
34
- */
35
- type AgentTool = KodaXToolDefinition;
36
- /**
37
- * Transport-level message reused from the AI layer. Kept as an alias so
38
- * consumers of the Layer A primitives do not need to import from `@kodax-ai/llm`
39
- * directly.
40
- */
41
- type AgentMessage = KodaXMessage;
42
- /**
43
- * Declarative reasoning profile attached to an Agent.
44
- *
45
- * In v0.7.23 this is a placeholder shape — only the `default` depth is read
46
- * when the Runner dispatches to `runKodaX`. Escalation on revise/replan and
47
- * max clamping are implemented in FEATURE_078 (v0.7.29).
48
- */
49
- interface AgentReasoningProfile {
50
- readonly default: ReasoningDepth;
51
- readonly max?: ReasoningDepth;
52
- readonly escalateOnRevise?: boolean;
53
- }
54
- /**
55
- * Declarative middleware reference attached to an Agent.
56
- *
57
- * FEATURE_100 (v0.7.29) introduces this field so the coding preset
58
- * can declare the four substrate middlewares (auto-reroute,
59
- * mutation-reflection, pre-answer-judge, post-tool-judge) on the
60
- * Agent declaration itself. Today these middlewares fire inside
61
- * the substrate body; the declaration field serves as the
62
- * machine-readable contract that the substrate honours, and lets
63
- * SDK consumers introspect / override middleware policy without
64
- * touching `runKodaX` internals.
65
- *
66
- * `enabled` is the only knob today; future versions add config
67
- * payload as additional fields (kept declarative — no fn callbacks).
68
- */
69
- interface AgentMiddlewareDeclaration {
70
- readonly name: string;
71
- readonly enabled: boolean;
72
- }
73
- /**
74
- * Guardrail placeholder. Layer A declares the slot; the actual
75
- * input/output/tool-call gating runtime lives in FEATURE_085 (v0.7.26).
76
- *
77
- * A guardrail targets one of three hook points:
78
- * - `input`: inspect / veto prompts before they enter the agent loop.
79
- * - `output`: inspect / rewrite assistant messages before they leave.
80
- * - `tool`: inspect / veto tool invocations during the loop.
81
- */
82
- interface Guardrail {
83
- readonly kind: 'input' | 'output' | 'tool';
84
- readonly name: string;
85
- }
86
- /**
87
- * Handoff between Agents.
88
- *
89
- * - `continuation`: ownership of the conversation transfers to `target` and
90
- * the caller exits. Mirrors the Scout → Generator upgrade path.
91
- * - `as-tool`: `target` is invoked like a tool from within the caller loop;
92
- * only the generated input is passed, and control returns on completion.
93
- * Mirrors FEATURE_067 `dispatch_child_task`.
94
- *
95
- * `inputFilter` is applied to the visible history before the target runs;
96
- * default is no filtering.
97
- */
98
- interface Handoff<TTo = unknown> {
99
- readonly target: Agent<TTo>;
100
- readonly kind: 'continuation' | 'as-tool';
101
- readonly description?: string;
102
- readonly inputFilter?: (history: readonly AgentMessage[]) => readonly AgentMessage[];
103
- }
104
- /**
105
- * Agent-as-data. A declarative specification of "who is running, with which
106
- * instructions, tools, handoffs, and reasoning profile."
107
- *
108
- * Runtime note: in v0.7.23 the only Agent that is fully executed is the
109
- * built-in coding preset (`createDefaultCodingAgent()`), which dispatches
110
- * through `runKodaX`. Custom Agents defined by SDK consumers run through a
111
- * generic Runner loop with limited capabilities (LLM call + Agent-declared
112
- * tools only — no extensions, no managed-task harness). The full runtime
113
- * arrives with FEATURE_084 (v0.7.26).
114
- */
115
- interface Agent<TContext = unknown> {
116
- readonly name: string;
117
- readonly instructions: string | ((ctx: TContext) => string);
118
- readonly tools?: readonly AgentTool[];
119
- readonly handoffs?: readonly Handoff[];
120
- readonly reasoning?: AgentReasoningProfile;
121
- readonly guardrails?: readonly Guardrail[];
122
- /**
123
- * FEATURE_191 — one-sentence human-readable summary surfaced to
124
- * other agents that may dispatch this one (e.g., via the
125
- * `dispatch_child_task(subagent_type=<name>)` Worker SP block). The
126
- * field propagates from `AgentContent.description` when the agent is
127
- * built via the construction substrate; built-in / SDK-created
128
- * agents may set it directly. Optional for backward compatibility
129
- * with the FEATURE_089 minimal-agent shape and pre-FEATURE_191
130
- * built-ins.
131
- */
132
- readonly description?: string;
133
- /** Reserved for structured-output agents; not consumed in v0.7.23. */
134
- readonly outputSchema?: unknown;
135
- readonly model?: string;
136
- readonly provider?: string;
137
- /**
138
- * FEATURE_100 (v0.7.29) substrate executor: when set, `Runner.run`
139
- * delegates execution to this function instead of consulting the
140
- * preset-dispatcher registry or running the generic LLM loop. The
141
- * coding preset attaches `runKodaX`'s full execution pipeline here so
142
- * the SDK surface `Runner.run(createDefaultCodingAgent(), prompt, opts)`
143
- * directly drives substrate without a `registerPresetDispatcher`
144
- * indirection (the v0.7.23 "Option Y" facade).
145
- *
146
- * Type is intentionally `unknown` to avoid a `core/agent.ts` ↔
147
- * `core/runner.ts` module cycle. `Runner.run` casts to the
148
- * `PresetDispatcher` shape declared in `runner.ts` at the call site.
149
- */
150
- readonly substrateExecutor?: unknown;
151
- /**
152
- * FEATURE_100 (v0.7.29) declarative middleware list. The coding
153
- * preset declares the four substrate middlewares it ships with
154
- * (auto-reroute, mutation-reflection, pre-answer-judge,
155
- * post-tool-judge). Substrate consults this list on entry and
156
- * skips the corresponding step when `enabled === false`. SDK
157
- * consumers can introspect or override declared middleware
158
- * without touching the substrate body.
159
- */
160
- readonly middleware?: readonly AgentMiddlewareDeclaration[];
161
- }
162
- /**
163
- * Ergonomic factory. Equivalent to a plain object literal but freezes the
164
- * shape so tests cannot mutate a shared Agent by accident.
165
- */
166
- declare function createAgent<TContext = unknown>(spec: Agent<TContext>): Agent<TContext>;
167
- /**
168
- * Ergonomic factory for Handoff.
169
- */
170
- declare function createHandoff<TTo = unknown>(spec: Handoff<TTo>): Handoff<TTo>;
171
-
172
- /**
173
- * SpanData variants — payload shapes carried by each `Span`.
174
- *
175
- * FEATURE_083 (v0.7.24): the Agent-era tracing model uses a discriminated
176
- * union so consumers (OpenTelemetry adapter, Langfuse adapter, KodaX
177
- * built-in file processor) can render each span kind with type safety.
178
- *
179
- * Variants mirror the semantic events KodaX emits today:
180
- * - AgentSpanData : one `Runner.run(agent, ...)` round
181
- * - GenerationSpanData: one provider LLM call
182
- * - ToolCallSpanData : one tool invocation (including MCP tool)
183
- * - HandoffSpanData : continuation or as-tool handoff between agents
184
- * - CompactionSpanData: one compaction pass (token-threshold or lineage)
185
- * - GuardrailSpanData : one guardrail check at input/output/tool
186
- * - EvidenceSpanData : repo-intelligence / evidence acquisition
187
- * - FanoutSpanData : parallel fanout bracket (winner-cancel capable)
188
- *
189
- * API surface is `@experimental` until v0.8.0 — shape may be refined as
190
- * FEATURE_084 (v0.7.26) starts emitting these.
191
- */
192
- interface AgentSpanData {
193
- readonly kind: 'agent';
194
- readonly agentName: string;
195
- readonly model?: string;
196
- readonly provider?: string;
197
- readonly tools?: readonly string[];
198
- readonly handoffs?: readonly string[];
199
- readonly outputMessages?: number;
200
- readonly error?: string;
201
- }
202
- interface GenerationSpanData {
203
- readonly kind: 'generation';
204
- readonly agentName: string;
205
- readonly provider: string;
206
- readonly model: string;
207
- readonly inputMessages?: number;
208
- readonly outputTokens?: number;
209
- readonly inputTokens?: number;
210
- readonly reasoningTokens?: number;
211
- readonly cachedTokens?: number;
212
- readonly usage?: {
213
- readonly inputTokens?: number;
214
- readonly outputTokens?: number;
215
- readonly totalTokens?: number;
216
- readonly reasoningTokens?: number;
217
- readonly cachedTokens?: number;
218
- readonly costUsd?: number;
219
- };
220
- readonly finishReason?: string;
221
- readonly error?: string;
222
- }
223
- interface ToolCallSpanData {
224
- readonly kind: 'tool_call';
225
- readonly toolName: string;
226
- readonly providerId?: string;
227
- readonly capabilityId?: string;
228
- readonly inputPreview?: string;
229
- readonly outputPreview?: string;
230
- readonly status: 'ok' | 'error';
231
- readonly error?: string;
232
- }
233
- interface HandoffSpanData {
234
- readonly kind: 'handoff';
235
- readonly fromAgent: string;
236
- readonly toAgent: string;
237
- readonly handoffKind: 'continuation' | 'as-tool';
238
- readonly description?: string;
239
- }
240
- interface CompactionSpanData {
241
- readonly kind: 'compaction';
242
- readonly policyName: string;
243
- readonly tokensUsed: number;
244
- readonly budget: number;
245
- readonly replacedMessageCount: number;
246
- readonly summaryLength: number;
247
- readonly error?: string;
248
- }
249
- interface GuardrailSpanData {
250
- readonly kind: 'guardrail';
251
- readonly guardrailName: string;
252
- readonly hookPoint: 'input' | 'output' | 'tool';
253
- readonly decision: 'pass' | 'veto' | 'rewrite' | 'error';
254
- readonly reason?: string;
255
- readonly error?: string;
256
- }
257
- interface EvidenceSpanData {
258
- readonly kind: 'evidence';
259
- readonly source: string;
260
- readonly queryPreview?: string;
261
- readonly resultCount?: number;
262
- readonly cacheHit?: boolean;
263
- readonly error?: string;
264
- }
265
- interface FanoutSpanData {
266
- readonly kind: 'fanout';
267
- readonly agentName: string;
268
- readonly childCount: number;
269
- readonly winnerChildId?: string;
270
- readonly cancelledChildIds?: readonly string[];
271
- }
272
- /**
273
- * FEATURE_184 (v0.7.45) — Stop hook observability.
274
- *
275
- * Emitted when the Runner's `RunOptions.stopHook` is invoked or fails.
276
- * `outcome` records what the hook returned (or `'error'` for thrown
277
- * exceptions — fail-open path), `reanimateCount` is the running count
278
- * after this invocation. `reason` carries the abort/reanimate text
279
- * when relevant, truncated by consumers as needed.
280
- */
281
- interface StopHookSpanData {
282
- readonly kind: 'stop-hook';
283
- readonly outcome: 'accept' | 'reanimate' | 'abort' | 'budget-exhausted' | 'error';
284
- readonly reanimateCount: number;
285
- readonly reanimateBudget: number;
286
- readonly reason?: string;
287
- readonly error?: string;
288
- }
289
- /**
290
- * Discriminated union of all span payload shapes. Additional variants may
291
- * be added in future features — consumers should check `kind` before
292
- * reading specific fields.
293
- */
294
- type SpanData = AgentSpanData | GenerationSpanData | ToolCallSpanData | HandoffSpanData | CompactionSpanData | GuardrailSpanData | EvidenceSpanData | FanoutSpanData | StopHookSpanData;
295
-
296
- /**
297
- * Span — a single timed unit of work inside a Trace.
298
- *
299
- * FEATURE_083 (v0.7.24): minimal Span implementation modeled after the
300
- * openai-agents-python Trace/Span pattern.
301
- *
302
- * Design constraints:
303
- * - Span creation must be cheap (no await, no serialisation). Processors
304
- * do their own batching / flushing.
305
- * - `addChild()` is synchronous and immediately visible in the Trace tree.
306
- * - `end()` is idempotent; calling it twice is a no-op.
307
- * - `error` is an optional field that sets a flag on the span without
308
- * throwing. The consumer decides how to surface errors.
309
- */
310
-
311
- interface SpanError {
312
- readonly message: string;
313
- readonly stack?: string;
314
- readonly data?: unknown;
315
- }
316
- /**
317
- * Public Span interface. Concrete implementation is `SpanImpl`.
318
- */
319
- interface Span {
320
- readonly id: string;
321
- readonly traceId: string;
322
- readonly parentId?: string;
323
- readonly name: string;
324
- readonly data: SpanData;
325
- readonly startedAt: number;
326
- readonly endedAt?: number;
327
- readonly error?: SpanError;
328
- readonly children: readonly Span[];
329
- addChild(name: string, data: SpanData): Span;
330
- setError(err: SpanError | Error): void;
331
- end(): void;
332
- }
333
- interface SpanImplOptions {
334
- readonly id: string;
335
- readonly traceId: string;
336
- readonly parentId?: string;
337
- readonly name: string;
338
- readonly data: SpanData;
339
- readonly startedAt?: number;
340
- readonly now?: () => number;
341
- readonly nextSpanId?: () => string;
342
- readonly onChildCreated?: (span: Span) => void;
343
- readonly onSpanEnd?: (span: Span) => void;
344
- }
345
- declare class SpanImpl implements Span {
346
- readonly id: string;
347
- readonly traceId: string;
348
- readonly parentId?: string;
349
- readonly name: string;
350
- readonly data: SpanData;
351
- readonly startedAt: number;
352
- private _endedAt?;
353
- private _error?;
354
- private readonly _children;
355
- private readonly now;
356
- private readonly nextSpanId;
357
- private readonly onChildCreated?;
358
- private readonly onSpanEnd?;
359
- private _ended;
360
- constructor(opts: SpanImplOptions);
361
- get endedAt(): number | undefined;
362
- get error(): SpanError | undefined;
363
- get children(): readonly Span[];
364
- addChild(name: string, data: SpanData): Span;
365
- setError(err: SpanError | Error): void;
366
- end(): void;
367
- }
368
-
369
- /**
370
- * Runner Tool Loop — FEATURE_084 Shard 1 (v0.7.26).
371
- *
372
- * Extends the Layer A Runner generic-dispatch path with tool-call support.
373
- * Before Shard 1 the Runner could only do a single `system+user → assistant`
374
- * turn. Now the injected LLM callback may return a structured result that
375
- * declares tool calls; the Runner executes them, appends tool_use +
376
- * tool_result content blocks, and loops until the LLM stops emitting tool
377
- * calls (or MAX_TOOL_LOOP_ITERATIONS is reached).
378
- *
379
- * This Shard only lands the capability. No built-in Agent consumes it yet —
380
- * the coding preset (SA path) continues to dispatch through
381
- * `registerPresetDispatcher` unchanged.
382
- *
383
- * @experimental Shape may be refined during the v0.7.26 shard rollout.
384
- */
385
-
386
- /**
387
- * Hard ceiling on tool-loop iterations. A single run may invoke at most this
388
- * many LLM turns (counting the initial turn); if the model keeps returning
389
- * tool calls past this limit we abort to prevent runaway behaviour.
390
- */
391
- declare const MAX_TOOL_LOOP_ITERATIONS = 20;
392
- /**
393
- * One tool invocation requested by the LLM.
394
- */
395
- interface RunnerToolCall {
396
- readonly id: string;
397
- readonly name: string;
398
- readonly input: Record<string, unknown>;
399
- }
400
- /**
401
- * Structured LLM result. Returning this instead of a plain string lets the
402
- * Runner drive a tool loop. If `toolCalls` is empty or omitted the loop
403
- * terminates and `text` becomes the final output.
404
- */
405
- interface RunnerLlmResult {
406
- readonly text: string;
407
- readonly toolCalls?: readonly RunnerToolCall[];
408
- readonly stopReason?: string;
409
- /**
410
- * v0.7.26 parity: extended-thinking blocks from the provider stream.
411
- * Must be preserved on the assistant message so (a) session resume can
412
- * re-render them and (b) Anthropic's extended-thinking API contract is
413
- * honoured — provider rejects the next turn with a 400 when a tool_use
414
- * turn's `thinking` block is missing from prior assistant history.
415
- */
416
- readonly thinkingBlocks?: readonly (KodaXThinkingBlock | KodaXRedactedThinkingBlock)[];
417
- }
418
- /**
419
- * LLM callback return type. `string` preserves the v0.7.23 single-turn
420
- * behaviour; `RunnerLlmResult` opts into the tool loop.
421
- */
422
- type RunnerLlmReturn = string | RunnerLlmResult;
423
- /**
424
- * Observer callbacks fired around every tool invocation. Preset
425
- * dispatchers (e.g. the coding Runner-driven path) pass these through
426
- * `RunOptions.toolObserver` so REPL / CLI consumers see live
427
- * `onToolCall` / `onToolResult` events, matching the legacy task-engine
428
- * event surface (v0.7.22 agent.ts fired `events.onToolResult` at three
429
- * sites per invocation).
430
- */
431
- interface RunnerToolObserver {
432
- /**
433
- * Permission / policy gate fired BEFORE each tool invocation. Return
434
- * `true` (or `undefined`) to allow, `false` to block with a generic
435
- * message, or a `string` to block with that message as the tool result.
436
- * Used to hook plan-mode / accept-edits / extension `tool:before`
437
- * policies onto the Runner-driven path (v0.7.22 parity — legacy
438
- * `events.beforeToolExecute` surface).
439
- */
440
- readonly beforeTool?: (call: RunnerToolCall) => Promise<boolean | string | undefined>;
441
- readonly onToolCall?: (call: RunnerToolCall) => void;
442
- readonly onToolResult?: (call: RunnerToolCall, result: RunnerToolResult) => void;
443
- }
444
- /**
445
- * Context passed to a RunnableTool's `execute` function.
446
- */
447
- interface RunnerToolContext {
448
- readonly agent: Agent;
449
- readonly abortSignal?: AbortSignal;
450
- /** The agent's Span, so tool implementations can nest custom spans if needed. */
451
- readonly agentSpan?: Span | null;
452
- /**
453
- * Current tool_use call id. Passed through so tool wrappers can correlate
454
- * progress events (`onToolProgress`) and other per-call side-effects with
455
- * the REPL's tool-block in the transcript. Populated by `executeRunnerToolCall`.
456
- */
457
- readonly toolCallId?: string;
458
- }
459
- /**
460
- * Value returned by `RunnableTool.execute`. The `content` is what the LLM
461
- * sees in the next turn as `tool_result`:
462
- *
463
- * - `string` — plain text (the default for most tools).
464
- * - `readonly KodaXToolResultContentItem[]` — an array of typed items
465
- * (text + image), used by multimodal tools like `read` on an image
466
- * path. Provider serializers lower each item to the wire format
467
- * (Anthropic accepts inline; OpenAI-compat downgrades image to text
468
- * placeholder).
469
- */
470
- interface RunnerToolResult {
471
- readonly content: string | readonly KodaXToolResultContentItem[];
472
- readonly isError?: boolean;
473
- readonly metadata?: Record<string, unknown>;
474
- }
475
- /**
476
- * A tool bundled with its executor. Extends `AgentTool` (the wire-format
477
- * `KodaXToolDefinition`) so it can be passed through to the provider
478
- * unchanged while also carrying a function the Runner can invoke.
479
- */
480
- interface RunnableTool extends AgentTool {
481
- readonly execute: (input: Record<string, unknown>, ctx: RunnerToolContext) => Promise<RunnerToolResult>;
482
- }
483
- /**
484
- * Narrowing helper — distinguishes a `RunnableTool` from a plain
485
- * `AgentTool`. An agent may declare both: the Runner only executes the
486
- * tools that carry an `execute` function.
487
- */
488
- declare function isRunnableTool(tool: AgentTool): tool is RunnableTool;
489
- /**
490
- * Narrowing helper for the LLM callback return shape.
491
- */
492
- declare function isRunnerLlmResult(value: unknown): value is RunnerLlmResult;
493
- /**
494
- * Execute one tool call against the agent's declared tools. Emits a
495
- * ToolCallSpan under `ctx.agentSpan` when tracing is active. Returns a
496
- * `RunnerToolResult` — tool errors do not throw, they are surfaced with
497
- * `isError: true` so the LLM can see them in the next turn and react.
498
- */
499
- declare function executeRunnerToolCall(call: RunnerToolCall, agent: Agent, ctx: RunnerToolContext): Promise<RunnerToolResult>;
500
- /**
501
- * Build the assistant message that captures one LLM turn. Preserves
502
- * thinking blocks (extended-thinking contract), text blocks, and tool_use
503
- * blocks in the order Anthropic's wire format expects: thinking → text →
504
- * tool_use. This mirrors v0.7.22 `agent.ts:2230`
505
- * (`[...thinkingBlocks, ...textBlocks, ...visibleToolBlocks]`) which the
506
- * Runner-driven path must preserve — without it Anthropic returns 400 on
507
- * the next turn when extended thinking is active, and session resume
508
- * loses the reasoning trace.
509
- */
510
- declare function buildAssistantMessageFromLlmResult(result: RunnerLlmResult): AgentMessage;
511
- /**
512
- * Build the user message that carries tool_result blocks back to the LLM.
513
- * Provider serializers (Anthropic, OpenAI) both accept tool_result on the
514
- * user turn.
515
- */
516
- declare function buildToolResultMessage(calls: readonly RunnerToolCall[], results: readonly RunnerToolResult[]): AgentMessage;
517
-
518
- /**
519
- * Guardrail Runtime — FEATURE_085 (v0.7.26).
520
- *
521
- * Three-tier runtime for Agent guardrails:
522
- *
523
- * - `InputGuardrail`: runs once before the first LLM turn, inspects the
524
- * full input transcript, may allow / rewrite / block / escalate.
525
- * - `OutputGuardrail`: runs once before returning, inspects the final
526
- * assistant message, may allow / rewrite / block / escalate.
527
- * - `ToolGuardrail`: runs before and/or after each tool invocation,
528
- * inspects the call / result, may allow / rewrite / block / escalate.
529
- *
530
- * The four verdict actions:
531
- *
532
- * - `allow`: continue with the current value.
533
- * - `rewrite`: replace the current value with `payload`.
534
- * - `block`: throw `GuardrailBlockedError` (for input/output) or surface
535
- * an error tool_result (for tool-before); the LLM / caller sees a
536
- * rejection and must adapt.
537
- * - `escalate`: throw `GuardrailEscalateError`; the SDK consumer catches
538
- * and decides whether to prompt the user, retry under different
539
- * constraints, etc.
540
- *
541
- * Every guardrail invocation emits a `GuardrailSpan` under the agent's
542
- * span when tracing is active.
543
- *
544
- * @experimental API shape may adjust during v0.7.x rollout.
545
- */
546
-
547
- /**
548
- * Shared execution context passed to every guardrail.
549
- *
550
- * `messages` is the live conversation transcript at the moment this
551
- * guardrail fires. For tool-side guardrails this is the transcript at
552
- * call-site time — it does NOT yet include the assistant turn that
553
- * emitted the current tool_use, since that turn is appended only after
554
- * the full tool batch settles. Optional so existing guardrails that
555
- * don't read context still type-check; populated by the Runner for all
556
- * production hook points.
557
- *
558
- * Added in FEATURE_092 (v0.7.33) so the auto-mode classifier guardrail
559
- * can extract intent context (user prompt + prior tool_use / tool_result
560
- * blocks) without reaching into Runner internals.
561
- */
562
- interface GuardrailContext {
563
- readonly agent: Agent;
564
- readonly abortSignal?: AbortSignal;
565
- readonly messages?: readonly AgentMessage[];
566
- }
567
- /**
568
- * Outcome of a single guardrail check. `payload` shape depends on the hook
569
- * point — see the specific guardrail interface for the expected type.
570
- */
571
- type GuardrailVerdict = {
572
- readonly action: 'allow';
573
- } | {
574
- readonly action: 'rewrite';
575
- readonly payload: unknown;
576
- readonly reason?: string;
577
- } | {
578
- readonly action: 'block';
579
- readonly reason: string;
580
- } | {
581
- readonly action: 'escalate';
582
- readonly reason: string;
583
- };
584
- /**
585
- * Input-side guardrail. Expected `rewrite` payload shape:
586
- * `readonly AgentMessage[]` — the replacement transcript.
587
- */
588
- interface InputGuardrail extends Guardrail {
589
- readonly kind: 'input';
590
- check(input: readonly AgentMessage[], ctx: GuardrailContext): Promise<GuardrailVerdict>;
591
- }
592
- /**
593
- * Output-side guardrail. Expected `rewrite` payload shape:
594
- * `AgentMessage` — the replacement final assistant message.
595
- */
596
- interface OutputGuardrail extends Guardrail {
597
- readonly kind: 'output';
598
- check(output: AgentMessage, ctx: GuardrailContext): Promise<GuardrailVerdict>;
599
- }
600
- /**
601
- * Tool-side guardrail. `beforeTool` rewrite payload shape: `RunnerToolCall`
602
- * (replacement call). `afterTool` rewrite payload shape: `RunnerToolResult`
603
- * (replacement result). Either hook is optional.
604
- */
605
- interface ToolGuardrail extends Guardrail {
606
- readonly kind: 'tool';
607
- beforeTool?(call: RunnerToolCall, ctx: GuardrailContext): Promise<GuardrailVerdict>;
608
- afterTool?(call: RunnerToolCall, result: RunnerToolResult, ctx: GuardrailContext): Promise<GuardrailVerdict>;
609
- }
610
- /**
611
- * Thrown when any guardrail returns `{ action: 'block' }`. The Runner
612
- * propagates this up to the caller — the run is aborted at that point.
613
- */
614
- declare class GuardrailBlockedError extends Error {
615
- readonly guardrailName: string;
616
- readonly hookPoint: 'input' | 'output' | 'tool';
617
- constructor(guardrailName: string, hookPoint: 'input' | 'output' | 'tool', reason: string);
618
- }
619
- /**
620
- * Thrown when any guardrail returns `{ action: 'escalate' }`. Callers can
621
- * catch and prompt the user or apply a stricter policy before retrying.
622
- */
623
- declare class GuardrailEscalateError extends Error {
624
- readonly guardrailName: string;
625
- readonly hookPoint: 'input' | 'output' | 'tool';
626
- constructor(guardrailName: string, hookPoint: 'input' | 'output' | 'tool', reason: string);
627
- }
628
- /** Filter a guardrail list by hook-point. */
629
- declare function collectGuardrails(guardrails: readonly Guardrail[] | undefined): {
630
- input: readonly InputGuardrail[];
631
- output: readonly OutputGuardrail[];
632
- tool: readonly ToolGuardrail[];
633
- };
634
- /**
635
- * Run all input guardrails in declaration order. Returns the (possibly
636
- * rewritten) transcript. Throws on block / escalate.
637
- */
638
- declare function runInputGuardrails(transcript: readonly AgentMessage[], guardrails: readonly InputGuardrail[], ctx: GuardrailContext, agentSpan: Span | null): Promise<readonly AgentMessage[]>;
639
- /**
640
- * Run all output guardrails in declaration order. Returns the (possibly
641
- * rewritten) final assistant message. Throws on block / escalate.
642
- */
643
- declare function runOutputGuardrails(output: AgentMessage, guardrails: readonly OutputGuardrail[], ctx: GuardrailContext, agentSpan: Span | null): Promise<AgentMessage>;
644
- /**
645
- * Outcome of the before-tool guardrail stage.
646
- * - `{ kind: 'allow', call }`: continue to executeRunnerToolCall with `call`
647
- * - `{ kind: 'block', result }`: skip execution; return `result` as the
648
- * tool_result to the LLM (so it sees the rejection and can adapt)
649
- */
650
- type ToolBeforeOutcome = {
651
- readonly kind: 'allow';
652
- readonly call: RunnerToolCall;
653
- } | {
654
- readonly kind: 'block';
655
- readonly result: RunnerToolResult;
656
- };
657
- /**
658
- * Run before-tool guardrails in declaration order. Rewrite replaces the
659
- * tool call. Block surfaces an error tool_result to the LLM instead of
660
- * throwing — the LLM sees the rejection and adapts. Escalate still throws.
661
- */
662
- declare function runToolBeforeGuardrails(call: RunnerToolCall, guardrails: readonly ToolGuardrail[], ctx: GuardrailContext, agentSpan: Span | null): Promise<ToolBeforeOutcome>;
663
- /**
664
- * Run after-tool guardrails in declaration order. Rewrite replaces the
665
- * result content. Block replaces with an error result. Escalate throws.
666
- */
667
- declare function runToolAfterGuardrails(call: RunnerToolCall, result: RunnerToolResult, guardrails: readonly ToolGuardrail[], ctx: GuardrailContext, agentSpan: Span | null): Promise<RunnerToolResult>;
668
-
669
- /**
670
- * Child task registry primitive — generic fan-out tracking.
671
- *
672
- * FEATURE_120 v0.7.39 Step 0 (package-attribution migration, ADR-021).
673
- * Lifted from `@kodax-ai/coding`'s `KodaXToolExecutionContext.childTaskRegistry`
674
- * field + inline cleanup chain in `tools/dispatch-child-tasks.ts`. The
675
- * coding side now consumes this primitive specialized to its
676
- * `KodaXChildExecutionResult` type; any other agent-flavor downstream
677
- * can specialize on its own child-result type without re-implementing
678
- * the cleanup contract.
679
- *
680
- * The shape is intentionally minimal: a Map plus a `register` helper
681
- * that bundles the v0.7.38 FEATURE_155 Bug A hotfix (`c1bdaf4e`)
682
- * cleanup chain into a single call site. The helper exists because
683
- * the cleanup is **not optional** — without it, every settled promise
684
- * stays in the registry forever, gets re-wrapped by the next
685
- * idle-yield `waitForWakeEvent` call, and fires spurious
686
- * `child-completed` wakes (production symptom: Evaluator gets
687
- * bombarded by duplicate `<task-completed>` notifications, consuming
688
- * an LLM turn each up to `IDLE_YIELD_MAX_ITERATIONS=64`).
689
- */
690
- /**
691
- * Map of `task_id` → in-flight child-execution promise. Generic over
692
- * the child-result type so the agent layer doesn't depend on any
693
- * specific agent flavor's result shape.
694
- *
695
- * Mutation contract:
696
- * - Owned by the runner's per-turn execution context. The dispatch
697
- * tool writes via `registerChildTask`; the idle-yield outer loop
698
- * reads via `Map.prototype.entries()` / `.size`.
699
- * - **Never delete entries manually** — call `registerChildTask`
700
- * and the cleanup chain it installs will run on settle.
701
- */
702
- type ChildTaskRegistry<T> = Map<string, Promise<T>>;
703
- /**
704
- * Register an in-flight child-execution promise in the registry and
705
- * install the cleanup chain that removes the entry once the promise
706
- * settles (success or failure).
707
- *
708
- * The cleanup chain is two stages:
709
- * 1. `.finally(() => registry.delete(childId))` — runs on settle
710
- * regardless of outcome, removing the entry before the next
711
- * idle-yield outer-loop iteration observes the registry.
712
- * 2. `.catch(() => {})` — swallows the rejection on the cleanup
713
- * chain so a child that crashes before any consumer awaits it
714
- * doesn't surface as `unhandledRejection` on Node. Must come
715
- * AFTER `.finally` because `.finally` returns a NEW promise
716
- * that rejects with the same reason.
717
- *
718
- * The original `promise` argument is **not** returned — the helper's
719
- * value-add is the cleanup side-effect, not promise transformation.
720
- * Callers that need to await the result read from `registry.get(id)`
721
- * or hold their own reference.
722
- *
723
- * @throws Error when `childId` already exists in the registry. Caller
724
- * should report this to the LLM as a tool-error (duplicate task_id);
725
- * the helper does NOT swallow the conflict because that would
726
- * silently overwrite an in-flight child's tracking entry.
727
- */
728
- declare function registerChildTask<T>(registry: ChildTaskRegistry<T>, childId: string, promise: Promise<T>): void;
729
-
730
- /**
731
- * Generic per-task abort primitive — `requestTaskStop`.
732
- *
733
- * FEATURE_120 v0.7.39 Phase 3a (ADR-021). Coordinator-style agents need
734
- * to request that a specific in-flight child task exit gracefully. The
735
- * @kodax-ai/agent layer owns the abort-controller registry shape and
736
- * the abort-dispatch decision; agent-flavor wrappers (e.g. the coding
737
- * `task_stop` tool, Phase 3b) layer in domain framing such as the
738
- * `<coordinator-stop-request>` message tag.
739
- *
740
- * What this primitive owns:
741
- * - A `TaskAbortRegistry` type alias = `Map<string, AbortController>`.
742
- * The map is owned + mutated by the caller; the primitive only
743
- * reads it. Callers use `registry.set(id, controller)` /
744
- * `registry.delete(id)` directly — the standard `Map` mutators
745
- * are simple enough that wrapping them adds no value.
746
- * - `requestTaskStop({taskId, registry, reason?})` — looks up the
747
- * controller, decides whether to abort, calls `controller.abort`,
748
- * returns a structured outcome.
749
- *
750
- * Abort semantics (matches the existing FEATURE_115 soft-pause
751
- * principle): aborting fires the signal but does NOT interrupt any
752
- * synchronous tool that's already executing. The child's next abort
753
- * check (`signal.throwIfAborted()` or an `signal.aborted` poll)
754
- * surfaces the abort. This matches Node's AbortController contract.
755
- *
756
- * What this primitive does NOT do (deliberate):
757
- * - Enqueue a coordinator-stop-request message — that's a
758
- * coding-flavor convenience and uses the existing
759
- * `routeMessage` primitive at the tool layer.
760
- * - Track abort lifecycle / auto-cleanup the registry — the
761
- * controller's lifetime is tied to its owning task's Promise;
762
- * the caller removes the registry entry when the task settles
763
- * (typically in a `.finally` chain alongside the child-task
764
- * registry cleanup).
765
- * - Time-out enforcement / retry — orthogonal concerns owned at
766
- * higher layers if needed.
767
- */
768
- /**
769
- * Registry mapping task ids to their owning AbortController.
770
- * Lifetime: created per parent-run, populated at child dispatch,
771
- * cleared when the child Promise settles.
772
- */
773
- type TaskAbortRegistry = Map<string, AbortController>;
774
- interface RequestTaskStopOptions {
775
- /** Target task id. Must exist as a key in `registry`. */
776
- readonly taskId: string;
777
- /** Registry of in-flight task abort controllers. */
778
- readonly registry: ReadonlyMap<string, AbortController>;
779
- /**
780
- * Optional cause forwarded to `AbortController.abort(reason)`.
781
- * - Error → passed through verbatim (preserves stack / custom
782
- * subclasses).
783
- * - string → wrapped in `new Error(reason)`.
784
- * - undefined → a default Error mentioning the taskId is
785
- * fabricated so the child receives a non-empty signal.reason.
786
- */
787
- readonly reason?: string | Error;
788
- }
789
- type RequestTaskStopResult = {
790
- readonly ok: true;
791
- readonly taskId: string;
792
- } | {
793
- readonly ok: false;
794
- readonly reason: 'unknown-target';
795
- readonly taskId: string;
796
- } | {
797
- readonly ok: false;
798
- readonly reason: 'already-aborted';
799
- readonly taskId: string;
800
- };
801
- /**
802
- * Look up `taskId` in `registry`. If found and not yet aborted, abort
803
- * the controller with the supplied reason. Returns a discriminated
804
- * outcome so callers can render success / error UX without string
805
- * matching.
806
- *
807
- * `already-aborted` is reported separately from success because the
808
- * first-abort `signal.reason` is preserved verbatim — debugging
809
- * chains depend on the original cause not being overwritten by
810
- * subsequent stop requests.
811
- *
812
- * Synchronous: `AbortController.abort` is synchronous; no async work
813
- * is performed by this primitive.
814
- */
815
- declare function requestTaskStop(opts: RequestTaskStopOptions): RequestTaskStopResult;
816
-
817
- /**
818
- * FEATURE_125 (v0.7.41) — Team Mode Layer 1: per-instance state broadcast.
819
- *
820
- * Each running KodaX session registers a directory under
821
- * `<agentConfigHome>/instances/<pid>/` containing three files:
822
- *
823
- * meta.json — written once at registration; cwd / startedAt /
824
- * optional git branch + remote. Static for the session.
825
- * state.json — re-written whenever the session's `currentIntent`,
826
- * `agentPhase`, or active/recently-modified file set
827
- * changes. Read by sibling sessions for context.
828
- * heartbeat — empty file whose mtime is touched on every refresh.
829
- * Sibling sessions use the mtime to declare an instance
830
- * stale (default 30s of no heartbeat → cleanup).
831
- *
832
- * Atomic write strategy:
833
- * - state.json is written via `<path>.tmp` + `rename()`. On POSIX the
834
- * rename is atomic; on Windows it is atomic when source + target sit
835
- * on the same filesystem (always true for `<agentConfigHome>/...`).
836
- * - heartbeat is touched via `utimesSync()`. Cheap, no rename needed.
837
- *
838
- * Lifecycle:
839
- * - `createStateWriter` writes meta.json + state.json + heartbeat once,
840
- * then starts an interval timer (default 1000ms) that refreshes
841
- * state.json and touches heartbeat.
842
- * - `update(patch)` shallow-merges the patch into the in-memory state
843
- * and flushes immediately so peer sessions see the change at the
844
- * next tool boundary, not at the next heartbeat tick.
845
- * - `shutdown()` clears the timer, removes the instance directory,
846
- * and resolves. Idempotent — safe to call multiple times.
847
- *
848
- * Crash recovery:
849
- * - If a process is killed mid-run, the directory is left on disk.
850
- * The next session's discovery scan (S2, `instance-discovery.ts`)
851
- * detects the stale heartbeat and removes the directory.
852
- *
853
- * DI-clean: every fs / clock dependency is injectable for hermetic tests.
854
- */
855
- /**
856
- * Live session state surfaced to sibling KodaX sessions. Mirrors the
857
- * shape documented in `docs/features/v0.7.41.md#feature_125-step-1`.
858
- */
859
- interface SessionStateSnapshot {
860
- readonly agentPhase: 'idle' | 'awaiting_llm' | 'running_tool';
861
- /** Single-line description of what the agent is currently doing. */
862
- readonly currentIntent?: string;
863
- /** Files the session is actively editing right now. */
864
- readonly activeFiles?: readonly string[];
865
- /** Files modified in the recent past (sibling sessions read this to detect "their content may be stale"). */
866
- readonly recentlyModifiedFiles?: readonly RecentlyModifiedFile[];
867
- /**
868
- * FEATURE_170 (v0.7.41) — optional one-line summary of the active
869
- * todo list. Lets sibling sessions display "they're currently
870
- * working on: <X>" without owning the todo store.
871
- */
872
- readonly currentTodoSummary?: CurrentTodoSummary;
873
- /**
874
- * v0.7.43 (FEATURE_173 Part B follow-up) — REPL session id
875
- * (e.g. `YYYYMMDD_HHMMSS`). Lets `listRunningSessions()` correlate
876
- * a sibling instance with its `.jsonl` file. Mutable: starts
877
- * undefined during bootstrap, set after `createInteractiveContext`,
878
- * re-published on `/new`. Older writers omit this; readers MUST
879
- * treat as optional.
880
- */
881
- readonly sessionId?: string;
882
- }
883
- interface RecentlyModifiedFile {
884
- readonly path: string;
885
- readonly modifiedAt: number;
886
- }
887
- interface CurrentTodoSummary {
888
- readonly inProgress?: string;
889
- readonly pendingCount: number;
890
- readonly completedCount: number;
891
- }
892
- interface SessionMeta {
893
- readonly cwd: string;
894
- readonly startedAt: number;
895
- readonly gitBranch?: string;
896
- readonly gitRemote?: string;
897
- }
898
- /**
899
- * Stored shape of `state.json` on disk — additive over SessionStateSnapshot.
900
- *
901
- * Reader contract (S2 `instance-discovery.ts`): parse the JSON, verify
902
- * `version === '1'` before reading any other field. On an unknown
903
- * version, log + skip the instance — this lets a newer writer coexist
904
- * with an older reader during an in-place upgrade.
905
- *
906
- * Fields are camelCase + a nested `meta` object (cwd / startedAt /
907
- * gitBranch / gitRemote). Do NOT assume the snake_case / flat shape
908
- * shown in early design-doc drafts — the typed interface here is the
909
- * ground truth; the doc has been updated to match.
910
- */
911
- interface PersistedSessionState extends SessionStateSnapshot {
912
- readonly version: '1';
913
- readonly pid: number;
914
- readonly updatedAt: number;
915
- readonly meta: SessionMeta;
916
- }
917
- /** Minimal injectable fs surface — lets tests drive the writer without disk I/O. */
918
- interface StateWriterFs {
919
- mkdirSync(dirPath: string, options: {
920
- recursive: true;
921
- }): void;
922
- writeFileSync(filePath: string, data: string): void;
923
- /** Atomic write helper: writes to `${filePath}.tmp` then renames. */
924
- atomicWriteSync(filePath: string, data: string): void;
925
- utimesSync(filePath: string, atime: number, mtime: number): void;
926
- rmSync(dirPath: string, options: {
927
- recursive: true;
928
- force: true;
929
- }): void;
930
- existsSync(targetPath: string): boolean;
931
- }
932
- interface StateWriterOptions {
933
- /** Defaults to `process.pid`. Tests / multi-instance fixtures override. */
934
- readonly pid?: number;
935
- readonly meta: SessionMeta;
936
- readonly initialState: SessionStateSnapshot;
937
- /** Defaults to 1000ms. Tests pass a faster tick. */
938
- readonly heartbeatIntervalMs?: number;
939
- /** Defaults to `Date.now`. Tests inject a controllable clock. */
940
- readonly clock?: () => number;
941
- /** Defaults to {@link REAL_FS}. Tests inject an in-memory fs. */
942
- readonly fs?: StateWriterFs;
943
- /**
944
- * Root directory under which `<pid>/` is created. Defaults to
945
- * `getAgentConfigPath('instances')`. Tests can point at a temp dir.
946
- */
947
- readonly instancesRoot?: string;
948
- }
949
- interface StateWriter {
950
- readonly pid: number;
951
- readonly instanceDir: string;
952
- /** Apply a partial update to the in-memory state and flush to disk. */
953
- update(patch: Partial<SessionStateSnapshot>): void;
954
- /** Touch the heartbeat and re-write state.json without changing state. */
955
- refresh(): void;
956
- /** Stop the interval, remove the instance directory, resolve when done. */
957
- shutdown(): Promise<void>;
958
- /** Read-only snapshot of the current state. Useful for tests. */
959
- getState(): SessionStateSnapshot;
960
- }
961
- /**
962
- * Construct a writer, register the instance directory, and start the
963
- * heartbeat interval. Returns synchronously so the caller can rely on
964
- * `instanceDir` being live the moment the function returns.
965
- */
966
- declare function createStateWriter(options: StateWriterOptions): StateWriter;
967
-
968
- /**
969
- * FEATURE_125 (v0.7.41) — Team Mode Layer 2a: sibling instance discovery.
970
- *
971
- * Scans `<agentConfigHome>/instances/`, filters out the caller's own
972
- * pid, drops any directory whose `heartbeat` file is stale (>30s of no
973
- * touch), parses `state.json`, validates `version === '1'`, and returns
974
- * a typed list of live sibling instances. Stale directories are
975
- * optionally reaped (`reapStale: true`) — the next session entering
976
- * Team Mode does the cleanup so crashed processes don't accumulate
977
- * forever.
978
- *
979
- * Per-instance failures (corrupt JSON, vanished file mid-read, permission
980
- * error) are isolated: the bad directory is logged + skipped, the rest
981
- * of the scan completes. Discovery NEVER throws to its caller — a
982
- * Team-Mode-disabled return is `[]`, not an exception. This keeps the
983
- * worker LLM call path resilient to one peer session's bad state.
984
- *
985
- * DI-clean: every fs / clock / logger dependency is injectable so
986
- * hermetic tests can simulate stale / corrupt / mid-scan-deletion
987
- * scenarios without real disk.
988
- */
989
-
990
- /** A sibling KodaX session that passed stale + version-guard checks. */
991
- interface DiscoveredInstance {
992
- readonly pid: number;
993
- readonly state: PersistedSessionState;
994
- /** Heartbeat mtime in ms (epoch). Useful for ordering "freshest first". */
995
- readonly heartbeatMtimeMs: number;
996
- }
997
- /** Minimal injectable fs surface used by `discoverInstances`. */
998
- interface InstanceDiscoveryFs {
999
- existsSync(targetPath: string): boolean;
1000
- readdirSync(dirPath: string): string[];
1001
- /** Returns the mtime of the path in ms, or `null` if missing / unreadable. */
1002
- statMtimeMs(filePath: string): number | null;
1003
- readFileSync(filePath: string, encoding: 'utf8'): string;
1004
- rmSync(dirPath: string, options: {
1005
- recursive: true;
1006
- force: true;
1007
- }): void;
1008
- }
1009
- interface DiscoveryOptions {
1010
- /**
1011
- * pid to exclude from the result. Defaults to `process.pid` — the
1012
- * caller's own state.json should not appear in its own sibling list.
1013
- */
1014
- readonly excludePid?: number;
1015
- /**
1016
- * Heartbeat mtime older than `now - staleThresholdMs` → directory is
1017
- * stale. Default 30_000 (matches v0.7.41 spec).
1018
- */
1019
- readonly staleThresholdMs?: number;
1020
- /**
1021
- * When true, stale directories are removed during the scan (best-
1022
- * effort `rmSync(force:true)`; failure is swallowed). When false,
1023
- * stale directories are skipped but left on disk. Defaults to false
1024
- * — wire from the session-startup path with `true` so crashed-process
1025
- * dirs don't accumulate.
1026
- */
1027
- readonly reapStale?: boolean;
1028
- readonly clock?: () => number;
1029
- readonly fs?: InstanceDiscoveryFs;
1030
- readonly instancesRoot?: string;
1031
- /** Per-instance failure log; defaults to a no-op. Pass `console.warn` in dev. */
1032
- readonly logger?: (message: string) => void;
1033
- }
1034
- /**
1035
- * Synchronous discovery scan. Returns a freshness-sorted array
1036
- * (newest heartbeat first) so callers that want only the N most-recent
1037
- * siblings can slice without re-sorting.
1038
- *
1039
- * Never throws. A missing `<instancesRoot>` directory means the user
1040
- * is the first session ever on this machine → `[]`. A scan failure on
1041
- * one entry is logged + skipped, not propagated.
1042
- */
1043
- declare function discoverInstances(options?: DiscoveryOptions): DiscoveredInstance[];
1044
-
1045
- /**
1046
- * ../../index.js Compaction Types
1047
- */
1048
-
1049
- interface CompactionConfig {
1050
- /** Whether automatic compaction is enabled. */
1051
- enabled: boolean;
1052
- /** Trigger compaction when context usage exceeds this percentage of the window. */
1053
- triggerPercent: number;
1054
- /**
1055
- * @deprecated V2 compaction no longer uses this option.
1056
- *
1057
- * The system now combines protected recent context, lightweight pruning, and
1058
- * rolling summaries automatically.
1059
- */
1060
- keepRecentPercent?: number;
1061
- /** Percentage of the most recent context that is never compacted or pruned. Defaults to 20. */
1062
- protectionPercent?: number;
1063
- /**
1064
- * Percentage of the context window used as the chunk size for each rolling
1065
- * summary pass. Defaults to 10.
1066
- */
1067
- rollingSummaryPercent?: number;
1068
- /** Prune oversized tool results when they exceed roughly this many tokens. Defaults to 500. */
1069
- pruningThresholdTokens?: number;
1070
- /**
1071
- * Gap ratio for prune fast-return. After pruning, if remaining tokens still exceed
1072
- * triggerTokens * pruningGapRatio, the system continues to the summarization path
1073
- * instead of returning early. Defaults to 0.8.
1074
- */
1075
- pruningGapRatio?: number;
1076
- /** Optional override for the provider context window. */
1077
- contextWindow?: number;
1078
- }
1079
- interface CompactionDetails {
1080
- readFiles: string[];
1081
- modifiedFiles: string[];
1082
- }
1083
- interface CompactionAnchor {
1084
- summary: string;
1085
- tokensBefore: number;
1086
- tokensAfter: number;
1087
- entriesRemoved: number;
1088
- reason: string;
1089
- artifactLedgerId?: string;
1090
- details?: CompactionDetails;
1091
- memorySeed?: KodaXCompactMemorySeed;
1092
- }
1093
- interface CompactionUpdate {
1094
- anchor?: CompactionAnchor;
1095
- artifactLedger?: KodaXSessionArtifactLedgerEntry[];
1096
- memorySeed?: KodaXCompactMemorySeed;
1097
- /**
1098
- * FEATURE_072: ledger-summary + file-content messages produced by
1099
- * `buildPostCompactAttachments` + `buildFileContentMessages`. Agent.ts
1100
- * passes these separately from the kept-tail messages so REPL-side
1101
- * `applySessionCompaction` can store them natively on the CompactionEntry
1102
- * rather than inlining them as loose `[Post-compact: ...]` system messages
1103
- * in lineage. Agent.ts keeps inlining them into its local flat `messages`
1104
- * via `injectPostCompactAttachments` (P4 belt-and-suspenders); the lineage
1105
- * is the persistence source of truth.
1106
- */
1107
- postCompactAttachments?: readonly KodaXMessage[];
1108
- }
1109
- interface CompactionResult {
1110
- compacted: boolean;
1111
- messages: KodaXMessage[];
1112
- summary?: string;
1113
- tokensBefore: number;
1114
- tokensAfter: number;
1115
- entriesRemoved: number;
1116
- details?: CompactionDetails;
1117
- artifactLedger?: KodaXSessionArtifactLedgerEntry[];
1118
- anchor?: CompactionAnchor;
1119
- memorySeed?: KodaXCompactMemorySeed;
1120
- }
1121
- interface FileOperations {
1122
- readFiles: string[];
1123
- modifiedFiles: string[];
1124
- }
1125
-
1126
- export { MAX_TOOL_LOOP_ITERATIONS as M, SpanImpl as X, buildAssistantMessageFromLlmResult as a5, buildToolResultMessage as a6, collectGuardrails as a7, createAgent as a8, createHandoff as a9, createStateWriter as aa, discoverInstances as ab, executeRunnerToolCall as ac, isRunnableTool as ad, isRunnerLlmResult as ae, registerChildTask as af, requestTaskStop as ag, runInputGuardrails as ah, runOutputGuardrails as ai, runToolAfterGuardrails as aj, runToolBeforeGuardrails as ak, GuardrailBlockedError as p, GuardrailEscalateError as r };
1127
- export type { StateWriterOptions as $, Agent as A, RunnerLlmResult as B, ChildTaskRegistry as C, DiscoveredInstance as D, EvidenceSpanData as E, FanoutSpanData as F, GenerationSpanData as G, Handoff as H, InputGuardrail as I, RunnerLlmReturn as J, RunnerToolCall as K, RunnerToolContext as L, RunnerToolObserver as N, OutputGuardrail as O, PersistedSessionState as P, RunnerToolResult as Q, ReasoningDepth as R, SessionMeta as S, SessionStateSnapshot as T, Span as U, SpanData as V, SpanError as W, SpanImplOptions as Y, StateWriter as Z, StateWriterFs as _, AgentMessage as a, StopHookSpanData as a0, TaskAbortRegistry as a1, ToolBeforeOutcome as a2, ToolCallSpanData as a3, ToolGuardrail as a4, AgentMiddlewareDeclaration as b, AgentReasoningProfile as c, AgentSpanData as d, AgentTool as e, CompactionAnchor as f, CompactionConfig as g, CompactionDetails as h, CompactionResult as i, CompactionSpanData as j, CompactionUpdate as k, CurrentTodoSummary as l, DiscoveryOptions as m, FileOperations as n, Guardrail as o, GuardrailContext as q, GuardrailSpanData as s, GuardrailVerdict as t, HandoffSpanData as u, InstanceDiscoveryFs as v, RecentlyModifiedFile as w, RequestTaskStopOptions as x, RequestTaskStopResult as y, RunnableTool as z };