@mono-agent/agent-runtime 0.4.0 → 0.4.1

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 (119) hide show
  1. package/ARCHITECTURE.md +37 -16
  2. package/MIGRATION.md +185 -7
  3. package/README.md +24 -14
  4. package/package.json +103 -10
  5. package/src/agent/allowlists.js +3 -0
  6. package/src/agent/approval.js +3 -0
  7. package/src/agent/compaction.js +244 -1
  8. package/src/agent/prompt/skill-index.js +6 -0
  9. package/src/agent/sandbox-seam.js +227 -0
  10. package/src/agent/tool-bloat.js +8 -2
  11. package/src/agent/tools/bash.js +16 -8
  12. package/src/agent/tools/edit.js +7 -3
  13. package/src/agent/tools/glob.js +11 -5
  14. package/src/agent/tools/grep.js +11 -5
  15. package/src/agent/tools/pi-bridge.js +227 -45
  16. package/src/agent/tools/read.js +28 -3
  17. package/src/agent/tools/shared/output-truncation.js +8 -3
  18. package/src/agent/tools/shared/path-resolver.js +24 -18
  19. package/src/agent/tools/shared/ripgrep.js +33 -6
  20. package/src/agent/tools/shared/runtime-context.js +60 -50
  21. package/src/agent/tools/shared/tool-context.js +157 -0
  22. package/src/agent/tools/web-fetch.js +21 -10
  23. package/src/agent/tools/web-search.js +11 -4
  24. package/src/agent/tools/write.js +7 -3
  25. package/src/agent/transcript.js +16 -1
  26. package/src/ai/cost.js +128 -3
  27. package/src/ai/failure.js +96 -4
  28. package/src/ai/live-input-prompt.js +11 -1
  29. package/src/ai/providers/claude-cli.js +2 -2
  30. package/src/ai/providers/claude-sdk.js +55 -12
  31. package/src/ai/providers/codex-app.js +36 -23
  32. package/src/ai/providers/opencode-app.js +2 -2
  33. package/src/ai/providers/opencode-discovery.js +2 -2
  34. package/src/ai/providers/pi-events.js +5 -0
  35. package/src/ai/providers/pi-models.js +21 -4
  36. package/src/ai/providers/pi-native/compaction-driver.js +394 -0
  37. package/src/ai/providers/pi-native/result-builder.js +312 -0
  38. package/src/ai/providers/pi-native/session-lifecycle.js +438 -0
  39. package/src/ai/providers/pi-native/stream-subscriber.js +148 -0
  40. package/src/ai/providers/pi-native/structured-output.js +130 -0
  41. package/src/ai/providers/pi-native/turn-runner.js +278 -0
  42. package/src/ai/providers/pi-native.js +415 -1106
  43. package/src/ai/runtime/model-refs.js +30 -0
  44. package/src/ai/runtime/registry.js +29 -0
  45. package/src/ai/runtime/router.js +96 -12
  46. package/src/ai/runtime/session-liveness.js +110 -0
  47. package/src/ai/runtime/sessions.js +16 -0
  48. package/src/ai/types.js +252 -19
  49. package/src/index.js +1 -1
  50. package/src/pi-auth.js +147 -16
  51. package/src/runtime-brand.js +21 -1
  52. package/src/runtime.js +72 -8
  53. package/types/agent/allowlists.d.ts +25 -0
  54. package/types/agent/approval.d.ts +30 -0
  55. package/types/agent/compaction.d.ts +97 -0
  56. package/types/agent/index.d.ts +5 -0
  57. package/types/agent/prompt/skill-index.d.ts +19 -0
  58. package/types/agent/sandbox-seam.d.ts +148 -0
  59. package/types/agent/tool-bloat.d.ts +22 -0
  60. package/types/agent/tools/bash.d.ts +16 -0
  61. package/types/agent/tools/edit.d.ts +14 -0
  62. package/types/agent/tools/glob.d.ts +16 -0
  63. package/types/agent/tools/grep.d.ts +22 -0
  64. package/types/agent/tools/index.d.ts +10 -0
  65. package/types/agent/tools/pi-bridge.d.ts +144 -0
  66. package/types/agent/tools/read.d.ts +19 -0
  67. package/types/agent/tools/shared/constants.d.ts +13 -0
  68. package/types/agent/tools/shared/dedup.d.ts +8 -0
  69. package/types/agent/tools/shared/output-truncation.d.ts +22 -0
  70. package/types/agent/tools/shared/path-resolver.d.ts +6 -0
  71. package/types/agent/tools/shared/ripgrep.d.ts +38 -0
  72. package/types/agent/tools/shared/runtime-context.d.ts +27 -0
  73. package/types/agent/tools/shared/tool-context.d.ts +48 -0
  74. package/types/agent/tools/web-fetch.d.ts +13 -0
  75. package/types/agent/tools/web-search.d.ts +11 -0
  76. package/types/agent/tools/write.d.ts +12 -0
  77. package/types/agent/transcript.d.ts +45 -0
  78. package/types/ai/backend.d.ts +41 -0
  79. package/types/ai/cost.d.ts +96 -0
  80. package/types/ai/failure.d.ts +117 -0
  81. package/types/ai/file-change-stats.d.ts +75 -0
  82. package/types/ai/index.d.ts +7 -0
  83. package/types/ai/live-input-prompt.d.ts +6 -0
  84. package/types/ai/observer.d.ts +68 -0
  85. package/types/ai/providers/claude-cli.d.ts +211 -0
  86. package/types/ai/providers/claude-sdk.d.ts +66 -0
  87. package/types/ai/providers/claude-subagents.d.ts +2 -0
  88. package/types/ai/providers/codex-app.d.ts +151 -0
  89. package/types/ai/providers/opencode-app.d.ts +95 -0
  90. package/types/ai/providers/opencode-discovery.d.ts +4 -0
  91. package/types/ai/providers/pi-errors.d.ts +3 -0
  92. package/types/ai/providers/pi-events.d.ts +24 -0
  93. package/types/ai/providers/pi-messages.d.ts +5 -0
  94. package/types/ai/providers/pi-models.d.ts +57 -0
  95. package/types/ai/providers/pi-native/compaction-driver.d.ts +86 -0
  96. package/types/ai/providers/pi-native/result-builder.d.ts +168 -0
  97. package/types/ai/providers/pi-native/session-lifecycle.d.ts +62 -0
  98. package/types/ai/providers/pi-native/stream-subscriber.d.ts +50 -0
  99. package/types/ai/providers/pi-native/structured-output.d.ts +69 -0
  100. package/types/ai/providers/pi-native/turn-runner.d.ts +60 -0
  101. package/types/ai/providers/pi-native.d.ts +18 -0
  102. package/types/ai/registry.d.ts +1 -0
  103. package/types/ai/runtime/capabilities-used.d.ts +21 -0
  104. package/types/ai/runtime/capabilities.d.ts +33 -0
  105. package/types/ai/runtime/context-windows.d.ts +8 -0
  106. package/types/ai/runtime/fast-mode.d.ts +2 -0
  107. package/types/ai/runtime/model-refs.d.ts +35 -0
  108. package/types/ai/runtime/registry.d.ts +38 -0
  109. package/types/ai/runtime/router.d.ts +56 -0
  110. package/types/ai/runtime/session-liveness.d.ts +55 -0
  111. package/types/ai/runtime/sessions.d.ts +38 -0
  112. package/types/ai/streaming/codex-events.d.ts +30 -0
  113. package/types/ai/streaming/opencode-events.d.ts +41 -0
  114. package/types/ai/types.d.ts +702 -0
  115. package/types/index.d.ts +7 -0
  116. package/types/pi-auth.d.ts +28 -0
  117. package/types/runtime-brand.d.ts +30 -0
  118. package/types/runtime.d.ts +10 -0
  119. package/src/ai/providers/pi-sdk.js +0 -35
@@ -0,0 +1,702 @@
1
+ /**
2
+ * @typedef {"claude" | "pi" | "codex" | "opencode" | (string & {})} RuntimeSdkId
3
+ * Canonical active runtime id. See ACTIVE_RUNTIME_KINDS (model-refs.js) for
4
+ * the enforced-at-runtime vocabulary.
5
+ */
6
+ /**
7
+ * @typedef {"claude" | "claude-code" | "codex-app" | "opencode-app" | "pi" | (string & {})} RuntimeBridgeId
8
+ * Registry bridge id (distinct from RuntimeSdkId: a single sdk can be served
9
+ * by more than one bridge, e.g. sdk "claude" is served by both the "claude"
10
+ * SDK bridge and the "claude-code" CLI bridge). See
11
+ * src/ai/runtime/registry.js's builtinBridgeSpecs.
12
+ */
13
+ /**
14
+ * @typedef {Object} RuntimeModelRef
15
+ * @property {RuntimeSdkId} sdk Canonical active runtime id.
16
+ * @property {string} model Provider model id.
17
+ * @property {string} [reference] Original canonical model reference; always set by
18
+ * parseRuntimeModelReference, but router.js's chain
19
+ * shorthand accepts bare {sdk, model} refs without one.
20
+ * @property {string} [provider] Pi/OpenCode provider id when sdk === "pi" | "opencode".
21
+ */
22
+ /**
23
+ * @typedef {{type: string, [key: string]: *}} RuntimeEvent
24
+ * Structured runtime/telemetry event. `type` is the only required field;
25
+ * every event kind (tool_approval_pending, provider_failover_started,
26
+ * context_compaction_applied, ...) adds its own extra fields.
27
+ */
28
+ /**
29
+ * @typedef {Object} RuntimeObserver
30
+ * Per-call or host-level observer merged by createObserverHub (ai/observer.js).
31
+ * Loose on purpose: observer.js is not a kernel seam file.
32
+ * @property {(event: RuntimeEvent) => (void|Promise<void>)} [onEvent]
33
+ * @property {() => (void|Promise<void>)} [flush]
34
+ */
35
+ /**
36
+ * @typedef {Object} RuntimeToolLimits
37
+ * Typed per-run tool-output limits (the supported replacement for the
38
+ * `agent_tool_*` / `agent_mcp_*` keys of the deprecated `settings` bag). Every
39
+ * field is optional; an omitted field falls back to the kernel default (see
40
+ * resolveAgentCompactionPolicy, agent/compaction.js).
41
+ * @property {number} [toolTextLimitChars] Max chars of a builtin tool's text result.
42
+ * @property {number} [bashOutputLimitChars] Max chars of bash stdout/stderr.
43
+ * @property {number} [mcpTextLimitChars] Max chars of an MCP tool's text result.
44
+ * @property {number} [searchResultLimit] Max Grep/search hits returned.
45
+ * @property {number} [imageInlineMaxBytes] Max bytes of an inlined image result.
46
+ * @property {number} [toolPayloadMaxBytes] Hard cap on a single tool_result payload.
47
+ * @property {number} [mcpCallTimeoutMs] Per-MCP-call inactivity timeout.
48
+ * @property {number} [mcpCallMaxTotalTimeoutMs] Hard wall-clock cap for one MCP call.
49
+ * @property {number} [bashTimeoutMs] Documented for forward-compat; NOT wired to
50
+ * any tool today (no `agent_bash_*_timeout` mechanism exists — bash reads its per-call
51
+ * `timeout` argument), so setting it has no effect until a run-level default is introduced.
52
+ */
53
+ /**
54
+ * @typedef {Object} RuntimeCompactionPolicy
55
+ * Typed per-run context-compaction policy (the supported replacement for the
56
+ * `agent_compaction_*` keys of the deprecated `settings` bag). Every field is
57
+ * optional; an omitted field falls back to the kernel default.
58
+ * @property {boolean} [enabled] Whether auto-compaction runs at all.
59
+ * @property {number} [triggerRatio] Fraction of the context window that arms the proactive trigger.
60
+ * @property {number} [keepRecentTokens] Recent-token budget preserved across a compaction.
61
+ * @property {number} [summaryMaxTokens] Max tokens of the generated summary.
62
+ * @property {number} [minSavingsTokens] Minimum token savings required to keep a compaction.
63
+ * @property {boolean} [fixedOverheadEnabled] Whether the system-prompt + tool-schema overhead correction is folded into the trigger.
64
+ * @property {number} [contextWindowOverride] Forces the compaction window instead of the live-model-recognized one (applied at resolveLiveCompactionPolicy; has no legacy settings equivalent).
65
+ */
66
+ /**
67
+ * @typedef {Object} RuntimePromptOverrides
68
+ * Optional overrides for the kernel's built-in prompt fragments. Precedence is
69
+ * run over host over the kernel default: an absent field leaves the built-in
70
+ * string in place. Supplied on both AgentRuntimeHostOptions (host default) and
71
+ * RuntimeRunOptions (per-run).
72
+ * @property {(systemPrompt: string) => string} [structuredOutputInstruction]
73
+ * Replaces the StructuredOutput system-prompt instruction (receives the raw
74
+ * system prompt, returns the augmented one). Only applied when an outputSchema is active.
75
+ * @property {() => string} [structuredOutputFinalization]
76
+ * Replaces the structured-output finalization re-prompt.
77
+ * @property {(body: string) => string} [liveInputGuidance]
78
+ * Replaces the live-input steering wrapper (receives the raw guidance body).
79
+ */
80
+ /**
81
+ * @typedef {Object} RuntimeRunOptions
82
+ * The options object a host passes to `createRuntime(host).run(systemPrompt, options)`.
83
+ * @property {RuntimeModelRef} model Resolved model reference; see parseRuntimeModelReference.
84
+ * @property {string} [executionMode] "sdk" (default) or "cli"; selects which bridge variant handles the model.
85
+ * @property {boolean} [liveInput] Whether this run expects a live/streaming input channel.
86
+ * @property {ReadonlyArray<*>} [observers] Per-call observers (see RuntimeObserver) merged with host-level (createRuntime) observers.
87
+ * @property {(event: RuntimeEvent) => void} [onEvent]
88
+ * @property {ReadonlyArray<Object>} [messages]
89
+ * @property {string} [effort]
90
+ * @property {boolean} [fastMode]
91
+ * @property {string} [cwd]
92
+ * @property {Object<string, Object>} [mcpServers]
93
+ * @property {ReadonlyArray<string>} [allowedTools]
94
+ * @property {ReadonlyArray<string>} [disallowedTools]
95
+ * @property {string} [permissionMode]
96
+ * @property {number} [maxTurns]
97
+ * @property {Object} [outputSchema]
98
+ * @property {string} [runArtifactDir]
99
+ * @property {AbortSignal} [abortSignal]
100
+ * @property {import('../agent/sandbox-seam.js').SandboxPolicy} [sandboxPolicy] Per-run sandbox policy; merged monotonically with the host policy (see resolveSandboxPolicy, agent/tools/shared/tool-context.js).
101
+ * @property {import('../agent/sandbox-seam.js').RuntimeSandboxEngine} [sandboxEngine] Per-run concrete sandbox engine handed to the active sandbox implementation.
102
+ * @property {import('../agent/sandbox-seam.js').RuntimeSandbox} [sandbox] Per-run sandbox IMPLEMENTATION override; when set it enforces this run's tools instead of the host/ToolContext impl (precedence run > host > passthrough). Policy DATA still merges monotonically (I13); this overrides only the enforcing code.
103
+ * @property {RuntimeToolLimits} [toolLimits] Typed per-run tool-output limits (supported replacement for the deprecated `settings` tool keys).
104
+ * @property {RuntimeCompactionPolicy} [compaction] Typed per-run compaction policy (supported replacement for the deprecated `settings` compaction keys).
105
+ * @property {RuntimePromptOverrides} [prompts] Per-run prompt-fragment overrides (run wins over the host default).
106
+ * @property {Object} [settings] DEPRECATED. Legacy flat settings bag; consumed only as a per-group FALLBACK when the corresponding typed object (`toolLimits` / `compaction`) is absent. Consuming any key emits one `deprecated_settings_option` runtime_warning per run. Migrate via resolveRuntimePolicies (@mono-agent/runtime-adapter).
107
+ * @property {Object} [nativeSubagents] Same-runtime teammate helpers exposed through native provider subagent surfaces.
108
+ * @property {Object} [diagnosticsSeed] Set by createRouterRuntime (ai/runtime/router.js) with a `resume_snapshot` when
109
+ * failing over mid-chain; a host-level coordinator may relay it forward (see agent/transcript.js), not read by any
110
+ * bridge in this package today.
111
+ * @property {string} [systemPromptPrefix] Set by createRouterRuntime alongside diagnosticsSeed; the router also
112
+ * prepends the same text to the systemPrompt argument directly, so bridges that ignore this field still continue
113
+ * correctly.
114
+ */
115
+ /**
116
+ * @typedef {RuntimeRunOptions
117
+ * & Pick<AgentRuntimeHostOptions, "resolveCustomPricing" | "resolvePiApiKey" | "persistArtifact" | "onCompactionRecorded" | "onToolApprovalRequest" | "toolRiskTiers" | "approvalDefaultRiskTier" | "approvalTimeoutMs" | "approvalAlwaysAllowTools">
118
+ * & {runtimeBrand: import('../runtime-brand.js').RuntimeBrand, toolContext?: import('../agent/tools/shared/tool-context.js').ToolContext, observerHub: {emit: (event: RuntimeEvent) => void, flush: () => Promise<void>}}
119
+ * } RuntimeRequest
120
+ * The request shape a bridge's `execute(systemPrompt, req)` receives as its
121
+ * second (options) argument: the host-supplied RuntimeRunOptions, merged by
122
+ * createRuntime (runtime.js) with the bound HOST_KEYS host-integration
123
+ * callbacks, the resolved runtimeBrand, the per-instance toolContext (read by
124
+ * internal tool helpers; absent when a host drives a bridge directly without
125
+ * createRuntime), and the per-run observerHub (onEvent is overridden to the
126
+ * hub's emit). `systemPrompt` is passed positionally, not folded into this object.
127
+ */
128
+ /**
129
+ * @typedef {Object} RuntimeResult
130
+ * @property {string|null} [text]
131
+ * @property {*} [structuredResult]
132
+ * @property {string|null} [structuredResultSource]
133
+ * @property {Array<RuntimeEvent>} [events]
134
+ * @property {Object} [usage]
135
+ * @property {number} [durationMs]
136
+ * @property {number} [numTurns]
137
+ * @property {string} [model]
138
+ * @property {string} [effort]
139
+ * @property {RuntimeSdkId} [sdk]
140
+ * @property {boolean} [cancelled]
141
+ * @property {string|null} [error]
142
+ * @property {Object|null} [errorDetails]
143
+ * @property {string|null} [failureKind]
144
+ * @property {string|null} [providerSessionId]
145
+ * @property {string|null} [stderrTail] Bounded stderr tail from a CLI-backed bridge; see createStderrTail (ai/failure.js).
146
+ * @property {Array<Object>} [runtimeWarnings]
147
+ * @property {Object} [diagnostics]
148
+ * @property {Object} [capabilitiesUsed]
149
+ * @property {Array<{model: RuntimeModelRef, failureKind: (string|null), requestId?: (string|null), retryableSubkind?: (string|null), requirements?: Object}>} [failoverHistory] Set by createRouterRuntime (ai/runtime/router.js) on every attempt after the first.
150
+ */
151
+ /**
152
+ * @typedef {Object} RuntimeCapabilities
153
+ * Capability flags a bridge reports for a given model reference. Structural,
154
+ * not sealed: bridges may report extra provider-specific flags (e.g.
155
+ * supports_fast_mode) alongside the common ones from COMMON_CAPABILITIES
156
+ * (ai/runtime/capabilities.js).
157
+ * @property {string} [kind]
158
+ * @property {string} [runtime]
159
+ * @property {boolean} [streaming]
160
+ * @property {boolean} [structured_output]
161
+ * @property {boolean} [supports_session_resume]
162
+ * @property {*} [native_runtime_config]
163
+ * @property {boolean} [supports_mcp]
164
+ * @property {boolean} [supports_skills]
165
+ * @property {boolean} [supports_builtin_tools]
166
+ * @property {boolean} [supports_live_input]
167
+ * @property {boolean} [supports_native_subagents]
168
+ * @property {boolean} [supports_fast_mode]
169
+ */
170
+ /**
171
+ * @typedef {Object} RuntimeBridge
172
+ * A fully loaded bridge (registry.js's resolveRuntimeBridge result): the
173
+ * executable provider implementation. Unlike RuntimeBridgeDescriptor,
174
+ * `capabilities` here is the bridge module's own static value (computed once
175
+ * at module load, e.g. `runtimeCapabilities("pi")`), not a callable.
176
+ * @property {RuntimeBridgeId} id
177
+ * @property {(ref: RuntimeModelRef, options?: Object) => boolean} supports
178
+ * @property {RuntimeCapabilities} capabilities
179
+ * @property {(systemPrompt: string, req: RuntimeRequest) => Promise<RuntimeResult>} execute
180
+ */
181
+ /**
182
+ * @typedef {Object} RuntimeBridgeDescriptor
183
+ * The introspection-only projection of a RuntimeBridge (registry.js's
184
+ * listRuntimeBridges result): id/supports/capabilities without execute, so
185
+ * hosts can describe backend support without loading provider modules.
186
+ * @property {RuntimeBridgeId} id
187
+ * @property {(ref: RuntimeModelRef, options?: Object) => boolean} supports
188
+ * @property {(ref?: RuntimeModelRef) => RuntimeCapabilities} capabilities
189
+ */
190
+ /**
191
+ * @typedef {Object} ApprovalRequestPayload
192
+ * Payload passed to a host's `onToolApprovalRequest` callback (agent/approval.js).
193
+ * @property {string} requestId
194
+ * @property {string} toolName
195
+ * @property {string|null} toolUseId
196
+ * @property {string} argumentsSummary Secret-redacted, length-capped JSON summary of the tool call arguments.
197
+ * @property {"low"|"medium"|"high"} riskTier
198
+ * @property {string|null} model
199
+ */
200
+ /**
201
+ * @typedef {Object} ApprovalDecision
202
+ * A host's response to an ApprovalRequestPayload.
203
+ * @property {"approve"|"deny"|"always"} decision
204
+ * @property {string} [reason]
205
+ */
206
+ /**
207
+ * @typedef {Object} CompactionRecordedPayload
208
+ * Payload passed to a host's `onCompactionRecorded` callback (ai/providers/pi-native.js)
209
+ * after a successful context compaction, so the host can persist the row.
210
+ * @property {string|null} task_run_id
211
+ * @property {string} trigger
212
+ * @property {string} provider_kind
213
+ * @property {string|null} model
214
+ * @property {number|null} tokens_before
215
+ * @property {string} summary
216
+ * @property {string|null} first_kept_entry_id
217
+ * @property {"succeeded"} status
218
+ * @property {number} created_at
219
+ */
220
+ /**
221
+ * @typedef {Object} AgentRuntimeHostOptions
222
+ * The `host` object passed to `createRuntime(host)` / `createRouterRuntime({host, chain})`.
223
+ * Combines the tool-runtime keys (forwarded to configureToolRuntime) and the
224
+ * host-integration callbacks (bound once, applied to every run via hostDefaults).
225
+ * @property {string} [workspace]
226
+ * @property {string} [repoRoot]
227
+ * @property {string} [ripgrepPath]
228
+ * @property {string} [qaOutputDir]
229
+ * @property {import('../agent/sandbox-seam.js').SandboxPolicy} [sandboxPolicy]
230
+ * @property {import('../agent/sandbox-seam.js').RuntimeSandboxEngine} [sandboxEngine]
231
+ * @property {import('../agent/sandbox-seam.js').RuntimeSandbox} [sandbox] Sandbox seam implementation (see agent/sandbox-seam.js); defaults to the zero-dependency passthroughSandbox. Real hosts inject runtime-adapter's implementation from packages/runtime-adapter/src/sandbox.ts.
232
+ * @property {RuntimePromptOverrides} [prompts] Host-level prompt-fragment override defaults; a per-run `options.prompts` field wins over these (see resolvePrompts, runtime.js).
233
+ * @property {ReadonlyArray<*>} [observers] Observer instances (see RuntimeObserver); loose because observer.js is not a kernel seam file.
234
+ * @property {*} [runtimeBrand] See resolveRuntimeBrand (runtime-brand.js); accepts a partial RuntimeBrand.
235
+ * @property {(parsed: {sdk: (string|null), provider?: string, model: string}) => (import('./cost.js').NormalizedPricing|null)} [resolveCustomPricing] See resolvePricing (ai/cost.js).
236
+ * @property {import('../pi-auth.js').PiApiKeyResolver} [resolvePiApiKey] See createPiOAuthApiKeyResolver (pi-auth.js) for a ready-made implementation.
237
+ * @property {(artifact: {filename: string, buffer: Buffer, toolName: string, toolUseId: (string|null)}) => (string|null)} [persistArtifact]
238
+ * @property {(record: CompactionRecordedPayload) => void} [onCompactionRecorded]
239
+ * @property {(payload: ApprovalRequestPayload) => Promise<ApprovalDecision>} [onToolApprovalRequest]
240
+ * @property {Object<string, ("low"|"medium"|"high")>} [toolRiskTiers]
241
+ * @property {"low"|"medium"|"high"} [approvalDefaultRiskTier]
242
+ * @property {number} [approvalTimeoutMs]
243
+ * @property {ReadonlyArray<string>} [approvalAlwaysAllowTools]
244
+ */
245
+ /**
246
+ * @typedef {Object} AgentRuntimeToolOptions
247
+ * The subset of AgentRuntimeHostOptions accepted by `runtime.configureTools(next)`
248
+ * (createRuntime's TOOL_RUNTIME_KEYS pick).
249
+ * @property {string} [workspace]
250
+ * @property {string} [repoRoot]
251
+ * @property {string} [ripgrepPath]
252
+ * @property {string} [qaOutputDir]
253
+ * @property {import('../agent/sandbox-seam.js').SandboxPolicy} [sandboxPolicy]
254
+ * @property {import('../agent/sandbox-seam.js').RuntimeSandboxEngine} [sandboxEngine]
255
+ * @property {import('../agent/sandbox-seam.js').RuntimeSandbox} [sandbox]
256
+ */
257
+ /**
258
+ * @typedef {Object} AgentRuntimeInstance
259
+ * The object `createRuntime`/`createRouterRuntime` return.
260
+ * @property {(systemPrompt: string, options: RuntimeRunOptions) => Promise<RuntimeResult>} run
261
+ * @property {(next?: AgentRuntimeToolOptions) => void} configureTools
262
+ * @property {(providerSessionId: string) => Promise<boolean|void>} disposeSession
263
+ * @property {() => Promise<void>} disposeAllSessions
264
+ */
265
+ export const PROVIDER_KIND_VALUES: string[];
266
+ /**
267
+ * Canonical active runtime id. See ACTIVE_RUNTIME_KINDS (model-refs.js) for
268
+ * the enforced-at-runtime vocabulary.
269
+ */
270
+ export type RuntimeSdkId = "claude" | "pi" | "codex" | "opencode" | (string & {});
271
+ /**
272
+ * Registry bridge id (distinct from RuntimeSdkId: a single sdk can be served
273
+ * by more than one bridge, e.g. sdk "claude" is served by both the "claude"
274
+ * SDK bridge and the "claude-code" CLI bridge). See
275
+ * src/ai/runtime/registry.js's builtinBridgeSpecs.
276
+ */
277
+ export type RuntimeBridgeId = "claude" | "claude-code" | "codex-app" | "opencode-app" | "pi" | (string & {});
278
+ export type RuntimeModelRef = {
279
+ /**
280
+ * Canonical active runtime id.
281
+ */
282
+ sdk: RuntimeSdkId;
283
+ /**
284
+ * Provider model id.
285
+ */
286
+ model: string;
287
+ /**
288
+ * Original canonical model reference; always set by
289
+ * parseRuntimeModelReference, but router.js's chain
290
+ * shorthand accepts bare {sdk, model} refs without one.
291
+ */
292
+ reference?: string;
293
+ /**
294
+ * Pi/OpenCode provider id when sdk === "pi" | "opencode".
295
+ */
296
+ provider?: string;
297
+ };
298
+ /**
299
+ * Structured runtime/telemetry event. `type` is the only required field;
300
+ * every event kind (tool_approval_pending, provider_failover_started,
301
+ * context_compaction_applied, ...) adds its own extra fields.
302
+ */
303
+ export type RuntimeEvent = {
304
+ type: string;
305
+ [key: string]: any;
306
+ };
307
+ /**
308
+ * Per-call or host-level observer merged by createObserverHub (ai/observer.js).
309
+ * Loose on purpose: observer.js is not a kernel seam file.
310
+ */
311
+ export type RuntimeObserver = {
312
+ onEvent?: (event: RuntimeEvent) => (void | Promise<void>);
313
+ flush?: () => (void | Promise<void>);
314
+ };
315
+ /**
316
+ * Typed per-run tool-output limits (the supported replacement for the
317
+ * `agent_tool_*` / `agent_mcp_*` keys of the deprecated `settings` bag). Every
318
+ * field is optional; an omitted field falls back to the kernel default (see
319
+ * resolveAgentCompactionPolicy, agent/compaction.js).
320
+ */
321
+ export type RuntimeToolLimits = {
322
+ /**
323
+ * Max chars of a builtin tool's text result.
324
+ */
325
+ toolTextLimitChars?: number;
326
+ /**
327
+ * Max chars of bash stdout/stderr.
328
+ */
329
+ bashOutputLimitChars?: number;
330
+ /**
331
+ * Max chars of an MCP tool's text result.
332
+ */
333
+ mcpTextLimitChars?: number;
334
+ /**
335
+ * Max Grep/search hits returned.
336
+ */
337
+ searchResultLimit?: number;
338
+ /**
339
+ * Max bytes of an inlined image result.
340
+ */
341
+ imageInlineMaxBytes?: number;
342
+ /**
343
+ * Hard cap on a single tool_result payload.
344
+ */
345
+ toolPayloadMaxBytes?: number;
346
+ /**
347
+ * Per-MCP-call inactivity timeout.
348
+ */
349
+ mcpCallTimeoutMs?: number;
350
+ /**
351
+ * Hard wall-clock cap for one MCP call.
352
+ */
353
+ mcpCallMaxTotalTimeoutMs?: number;
354
+ /**
355
+ * Documented for forward-compat; NOT wired to
356
+ * any tool today (no `agent_bash_*_timeout` mechanism exists — bash reads its per-call
357
+ * `timeout` argument), so setting it has no effect until a run-level default is introduced.
358
+ */
359
+ bashTimeoutMs?: number;
360
+ };
361
+ /**
362
+ * Typed per-run context-compaction policy (the supported replacement for the
363
+ * `agent_compaction_*` keys of the deprecated `settings` bag). Every field is
364
+ * optional; an omitted field falls back to the kernel default.
365
+ */
366
+ export type RuntimeCompactionPolicy = {
367
+ /**
368
+ * Whether auto-compaction runs at all.
369
+ */
370
+ enabled?: boolean;
371
+ /**
372
+ * Fraction of the context window that arms the proactive trigger.
373
+ */
374
+ triggerRatio?: number;
375
+ /**
376
+ * Recent-token budget preserved across a compaction.
377
+ */
378
+ keepRecentTokens?: number;
379
+ /**
380
+ * Max tokens of the generated summary.
381
+ */
382
+ summaryMaxTokens?: number;
383
+ /**
384
+ * Minimum token savings required to keep a compaction.
385
+ */
386
+ minSavingsTokens?: number;
387
+ /**
388
+ * Whether the system-prompt + tool-schema overhead correction is folded into the trigger.
389
+ */
390
+ fixedOverheadEnabled?: boolean;
391
+ /**
392
+ * Forces the compaction window instead of the live-model-recognized one (applied at resolveLiveCompactionPolicy; has no legacy settings equivalent).
393
+ */
394
+ contextWindowOverride?: number;
395
+ };
396
+ /**
397
+ * Optional overrides for the kernel's built-in prompt fragments. Precedence is
398
+ * run over host over the kernel default: an absent field leaves the built-in
399
+ * string in place. Supplied on both AgentRuntimeHostOptions (host default) and
400
+ * RuntimeRunOptions (per-run).
401
+ */
402
+ export type RuntimePromptOverrides = {
403
+ /**
404
+ * Replaces the StructuredOutput system-prompt instruction (receives the raw
405
+ * system prompt, returns the augmented one). Only applied when an outputSchema is active.
406
+ */
407
+ structuredOutputInstruction?: (systemPrompt: string) => string;
408
+ /**
409
+ * Replaces the structured-output finalization re-prompt.
410
+ */
411
+ structuredOutputFinalization?: () => string;
412
+ /**
413
+ * Replaces the live-input steering wrapper (receives the raw guidance body).
414
+ */
415
+ liveInputGuidance?: (body: string) => string;
416
+ };
417
+ /**
418
+ * The options object a host passes to `createRuntime(host).run(systemPrompt, options)`.
419
+ */
420
+ export type RuntimeRunOptions = {
421
+ /**
422
+ * Resolved model reference; see parseRuntimeModelReference.
423
+ */
424
+ model: RuntimeModelRef;
425
+ /**
426
+ * "sdk" (default) or "cli"; selects which bridge variant handles the model.
427
+ */
428
+ executionMode?: string;
429
+ /**
430
+ * Whether this run expects a live/streaming input channel.
431
+ */
432
+ liveInput?: boolean;
433
+ /**
434
+ * Per-call observers (see RuntimeObserver) merged with host-level (createRuntime) observers.
435
+ */
436
+ observers?: ReadonlyArray<any>;
437
+ onEvent?: (event: RuntimeEvent) => void;
438
+ messages?: ReadonlyArray<any>;
439
+ effort?: string;
440
+ fastMode?: boolean;
441
+ cwd?: string;
442
+ mcpServers?: {
443
+ [x: string]: any;
444
+ };
445
+ allowedTools?: ReadonlyArray<string>;
446
+ disallowedTools?: ReadonlyArray<string>;
447
+ permissionMode?: string;
448
+ maxTurns?: number;
449
+ outputSchema?: any;
450
+ runArtifactDir?: string;
451
+ abortSignal?: AbortSignal;
452
+ /**
453
+ * Per-run sandbox policy; merged monotonically with the host policy (see resolveSandboxPolicy, agent/tools/shared/tool-context.js).
454
+ */
455
+ sandboxPolicy?: import("../agent/sandbox-seam.js").SandboxPolicy;
456
+ /**
457
+ * Per-run concrete sandbox engine handed to the active sandbox implementation.
458
+ */
459
+ sandboxEngine?: import("../agent/sandbox-seam.js").RuntimeSandboxEngine;
460
+ /**
461
+ * Per-run sandbox IMPLEMENTATION override; when set it enforces this run's tools instead of the host/ToolContext impl (precedence run > host > passthrough). Policy DATA still merges monotonically (I13); this overrides only the enforcing code.
462
+ */
463
+ sandbox?: import("../agent/sandbox-seam.js").RuntimeSandbox;
464
+ /**
465
+ * Typed per-run tool-output limits (supported replacement for the deprecated `settings` tool keys).
466
+ */
467
+ toolLimits?: RuntimeToolLimits;
468
+ /**
469
+ * Typed per-run compaction policy (supported replacement for the deprecated `settings` compaction keys).
470
+ */
471
+ compaction?: RuntimeCompactionPolicy;
472
+ /**
473
+ * Per-run prompt-fragment overrides (run wins over the host default).
474
+ */
475
+ prompts?: RuntimePromptOverrides;
476
+ /**
477
+ * DEPRECATED. Legacy flat settings bag; consumed only as a per-group FALLBACK when the corresponding typed object (`toolLimits` / `compaction`) is absent. Consuming any key emits one `deprecated_settings_option` runtime_warning per run. Migrate via resolveRuntimePolicies (@mono-agent/runtime-adapter).
478
+ */
479
+ settings?: any;
480
+ /**
481
+ * Same-runtime teammate helpers exposed through native provider subagent surfaces.
482
+ */
483
+ nativeSubagents?: any;
484
+ /**
485
+ * Set by createRouterRuntime (ai/runtime/router.js) with a `resume_snapshot` when
486
+ * failing over mid-chain; a host-level coordinator may relay it forward (see agent/transcript.js), not read by any
487
+ * bridge in this package today.
488
+ */
489
+ diagnosticsSeed?: any;
490
+ /**
491
+ * Set by createRouterRuntime alongside diagnosticsSeed; the router also
492
+ * prepends the same text to the systemPrompt argument directly, so bridges that ignore this field still continue
493
+ * correctly.
494
+ */
495
+ systemPromptPrefix?: string;
496
+ };
497
+ /**
498
+ * The request shape a bridge's `execute(systemPrompt, req)` receives as its
499
+ * second (options) argument: the host-supplied RuntimeRunOptions, merged by
500
+ * createRuntime (runtime.js) with the bound HOST_KEYS host-integration
501
+ * callbacks, the resolved runtimeBrand, the per-instance toolContext (read by
502
+ * internal tool helpers; absent when a host drives a bridge directly without
503
+ * createRuntime), and the per-run observerHub (onEvent is overridden to the
504
+ * hub's emit). `systemPrompt` is passed positionally, not folded into this object.
505
+ */
506
+ export type RuntimeRequest = RuntimeRunOptions & Pick<AgentRuntimeHostOptions, "resolveCustomPricing" | "resolvePiApiKey" | "persistArtifact" | "onCompactionRecorded" | "onToolApprovalRequest" | "toolRiskTiers" | "approvalDefaultRiskTier" | "approvalTimeoutMs" | "approvalAlwaysAllowTools"> & {
507
+ runtimeBrand: import("../runtime-brand.js").RuntimeBrand;
508
+ toolContext?: import("../agent/tools/shared/tool-context.js").ToolContext;
509
+ observerHub: {
510
+ emit: (event: RuntimeEvent) => void;
511
+ flush: () => Promise<void>;
512
+ };
513
+ };
514
+ export type RuntimeResult = {
515
+ text?: string | null;
516
+ structuredResult?: any;
517
+ structuredResultSource?: string | null;
518
+ events?: Array<RuntimeEvent>;
519
+ usage?: any;
520
+ durationMs?: number;
521
+ numTurns?: number;
522
+ model?: string;
523
+ effort?: string;
524
+ sdk?: RuntimeSdkId;
525
+ cancelled?: boolean;
526
+ error?: string | null;
527
+ errorDetails?: any | null;
528
+ failureKind?: string | null;
529
+ providerSessionId?: string | null;
530
+ /**
531
+ * Bounded stderr tail from a CLI-backed bridge; see createStderrTail (ai/failure.js).
532
+ */
533
+ stderrTail?: string | null;
534
+ runtimeWarnings?: Array<any>;
535
+ diagnostics?: any;
536
+ capabilitiesUsed?: any;
537
+ /**
538
+ * Set by createRouterRuntime (ai/runtime/router.js) on every attempt after the first.
539
+ */
540
+ failoverHistory?: Array<{
541
+ model: RuntimeModelRef;
542
+ failureKind: (string | null);
543
+ requestId?: (string | null);
544
+ retryableSubkind?: (string | null);
545
+ requirements?: any;
546
+ }>;
547
+ };
548
+ /**
549
+ * Capability flags a bridge reports for a given model reference. Structural,
550
+ * not sealed: bridges may report extra provider-specific flags (e.g.
551
+ * supports_fast_mode) alongside the common ones from COMMON_CAPABILITIES
552
+ * (ai/runtime/capabilities.js).
553
+ */
554
+ export type RuntimeCapabilities = {
555
+ kind?: string;
556
+ runtime?: string;
557
+ streaming?: boolean;
558
+ structured_output?: boolean;
559
+ supports_session_resume?: boolean;
560
+ native_runtime_config?: any;
561
+ supports_mcp?: boolean;
562
+ supports_skills?: boolean;
563
+ supports_builtin_tools?: boolean;
564
+ supports_live_input?: boolean;
565
+ supports_native_subagents?: boolean;
566
+ supports_fast_mode?: boolean;
567
+ };
568
+ /**
569
+ * A fully loaded bridge (registry.js's resolveRuntimeBridge result): the
570
+ * executable provider implementation. Unlike RuntimeBridgeDescriptor,
571
+ * `capabilities` here is the bridge module's own static value (computed once
572
+ * at module load, e.g. `runtimeCapabilities("pi")`), not a callable.
573
+ */
574
+ export type RuntimeBridge = {
575
+ id: RuntimeBridgeId;
576
+ supports: (ref: RuntimeModelRef, options?: any) => boolean;
577
+ capabilities: RuntimeCapabilities;
578
+ execute: (systemPrompt: string, req: RuntimeRequest) => Promise<RuntimeResult>;
579
+ };
580
+ /**
581
+ * The introspection-only projection of a RuntimeBridge (registry.js's
582
+ * listRuntimeBridges result): id/supports/capabilities without execute, so
583
+ * hosts can describe backend support without loading provider modules.
584
+ */
585
+ export type RuntimeBridgeDescriptor = {
586
+ id: RuntimeBridgeId;
587
+ supports: (ref: RuntimeModelRef, options?: any) => boolean;
588
+ capabilities: (ref?: RuntimeModelRef) => RuntimeCapabilities;
589
+ };
590
+ /**
591
+ * Payload passed to a host's `onToolApprovalRequest` callback (agent/approval.js).
592
+ */
593
+ export type ApprovalRequestPayload = {
594
+ requestId: string;
595
+ toolName: string;
596
+ toolUseId: string | null;
597
+ /**
598
+ * Secret-redacted, length-capped JSON summary of the tool call arguments.
599
+ */
600
+ argumentsSummary: string;
601
+ riskTier: "low" | "medium" | "high";
602
+ model: string | null;
603
+ };
604
+ /**
605
+ * A host's response to an ApprovalRequestPayload.
606
+ */
607
+ export type ApprovalDecision = {
608
+ decision: "approve" | "deny" | "always";
609
+ reason?: string;
610
+ };
611
+ /**
612
+ * Payload passed to a host's `onCompactionRecorded` callback (ai/providers/pi-native.js)
613
+ * after a successful context compaction, so the host can persist the row.
614
+ */
615
+ export type CompactionRecordedPayload = {
616
+ task_run_id: string | null;
617
+ trigger: string;
618
+ provider_kind: string;
619
+ model: string | null;
620
+ tokens_before: number | null;
621
+ summary: string;
622
+ first_kept_entry_id: string | null;
623
+ status: "succeeded";
624
+ created_at: number;
625
+ };
626
+ /**
627
+ * The `host` object passed to `createRuntime(host)` / `createRouterRuntime({host, chain})`.
628
+ * Combines the tool-runtime keys (forwarded to configureToolRuntime) and the
629
+ * host-integration callbacks (bound once, applied to every run via hostDefaults).
630
+ */
631
+ export type AgentRuntimeHostOptions = {
632
+ workspace?: string;
633
+ repoRoot?: string;
634
+ ripgrepPath?: string;
635
+ qaOutputDir?: string;
636
+ sandboxPolicy?: import("../agent/sandbox-seam.js").SandboxPolicy;
637
+ sandboxEngine?: import("../agent/sandbox-seam.js").RuntimeSandboxEngine;
638
+ /**
639
+ * Sandbox seam implementation (see agent/sandbox-seam.js); defaults to the zero-dependency passthroughSandbox. Real hosts inject runtime-adapter's implementation from packages/runtime-adapter/src/sandbox.ts.
640
+ */
641
+ sandbox?: import("../agent/sandbox-seam.js").RuntimeSandbox;
642
+ /**
643
+ * Host-level prompt-fragment override defaults; a per-run `options.prompts` field wins over these (see resolvePrompts, runtime.js).
644
+ */
645
+ prompts?: RuntimePromptOverrides;
646
+ /**
647
+ * Observer instances (see RuntimeObserver); loose because observer.js is not a kernel seam file.
648
+ */
649
+ observers?: ReadonlyArray<any>;
650
+ /**
651
+ * See resolveRuntimeBrand (runtime-brand.js); accepts a partial RuntimeBrand.
652
+ */
653
+ runtimeBrand?: any;
654
+ /**
655
+ * See resolvePricing (ai/cost.js).
656
+ */
657
+ resolveCustomPricing?: (parsed: {
658
+ sdk: (string | null);
659
+ provider?: string;
660
+ model: string;
661
+ }) => (import("./cost.js").NormalizedPricing | null);
662
+ /**
663
+ * See createPiOAuthApiKeyResolver (pi-auth.js) for a ready-made implementation.
664
+ */
665
+ resolvePiApiKey?: import("../pi-auth.js").PiApiKeyResolver;
666
+ persistArtifact?: (artifact: {
667
+ filename: string;
668
+ buffer: Buffer;
669
+ toolName: string;
670
+ toolUseId: (string | null);
671
+ }) => (string | null);
672
+ onCompactionRecorded?: (record: CompactionRecordedPayload) => void;
673
+ onToolApprovalRequest?: (payload: ApprovalRequestPayload) => Promise<ApprovalDecision>;
674
+ toolRiskTiers?: {
675
+ [x: string]: "low" | "medium" | "high";
676
+ };
677
+ approvalDefaultRiskTier?: "low" | "medium" | "high";
678
+ approvalTimeoutMs?: number;
679
+ approvalAlwaysAllowTools?: ReadonlyArray<string>;
680
+ };
681
+ /**
682
+ * The subset of AgentRuntimeHostOptions accepted by `runtime.configureTools(next)`
683
+ * (createRuntime's TOOL_RUNTIME_KEYS pick).
684
+ */
685
+ export type AgentRuntimeToolOptions = {
686
+ workspace?: string;
687
+ repoRoot?: string;
688
+ ripgrepPath?: string;
689
+ qaOutputDir?: string;
690
+ sandboxPolicy?: import("../agent/sandbox-seam.js").SandboxPolicy;
691
+ sandboxEngine?: import("../agent/sandbox-seam.js").RuntimeSandboxEngine;
692
+ sandbox?: import("../agent/sandbox-seam.js").RuntimeSandbox;
693
+ };
694
+ /**
695
+ * The object `createRuntime`/`createRouterRuntime` return.
696
+ */
697
+ export type AgentRuntimeInstance = {
698
+ run: (systemPrompt: string, options: RuntimeRunOptions) => Promise<RuntimeResult>;
699
+ configureTools: (next?: AgentRuntimeToolOptions) => void;
700
+ disposeSession: (providerSessionId: string) => Promise<boolean | void>;
701
+ disposeAllSessions: () => Promise<void>;
702
+ };