maka-agent 0.2.0-dev.23.20260906 → 0.2.0-dev.25.20260908

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 (215) hide show
  1. package/README.md +38 -19
  2. package/README.zh-CN.md +34 -19
  3. package/dist/acp/maka-acp-agent.js +2 -1
  4. package/dist/acp/session-configuration.js +143 -0
  5. package/dist/acp/session-registry.js +90 -4
  6. package/dist/cli-core.js +2 -0
  7. package/dist/pi-transcript-tools.js +6 -0
  8. package/dist/pi-transcript.js +5 -12
  9. package/dist/pi-tui-layout.js +56 -10
  10. package/dist/pi-tui-pickers.js +102 -1
  11. package/dist/pi-tui-runner.js +230 -46
  12. package/dist/pi-tui-todo.js +221 -0
  13. package/dist/pi-tui-transcript-viewer.js +134 -7
  14. package/dist/runtime-host-cli-context.js +224 -78
  15. package/dist/runtime-host-cli-installation.js +8 -2
  16. package/dist/runtime-host-cli.js +31 -1
  17. package/dist/runtime-host-handoff-surface.js +106 -0
  18. package/dist/runtime-host-installed-update-bootstrap.js +13 -0
  19. package/dist/runtime-host-installed-update-coordinator.js +33 -0
  20. package/dist/runtime-host-lifecycle-transaction.js +9 -1
  21. package/dist/runtime-host-local-handoff.js +48 -5
  22. package/dist/runtime-host-local-managed-activation.js +84 -0
  23. package/dist/runtime-host-local-source-retirement.js +18 -2
  24. package/dist/runtime-host-managed-lifecycle-manager.js +1 -0
  25. package/dist/runtime-host-operator-retirement.js +50 -0
  26. package/dist/runtime-host-service-manager.js +3 -3
  27. package/dist/runtime-host-session-channel.js +13 -0
  28. package/dist/runtime-host-session-driver.js +46 -26
  29. package/dist/runtime-host-session-update.js +62 -0
  30. package/dist/runtime-host-tui-command.js +38 -73
  31. package/dist/runtime-host-tui-context.js +1 -0
  32. package/dist/runtime-host-tui-owner.js +226 -0
  33. package/dist/tui-copy-catalog.js +219 -6
  34. package/dist/tui-host-owner-copy.js +24 -0
  35. package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  36. package/node_modules/@maka/core/dist/agent-run.js +1 -0
  37. package/node_modules/@maka/core/dist/attachments.js +3 -0
  38. package/node_modules/@maka/core/dist/canonical-json.js +27 -0
  39. package/node_modules/@maka/core/dist/canonical-runtime-event.js +3 -1
  40. package/node_modules/@maka/core/dist/capabilities.js +35 -0
  41. package/node_modules/@maka/core/dist/computer-use.js +0 -1
  42. package/node_modules/@maka/core/dist/display-redaction.js +18 -0
  43. package/node_modules/@maka/core/dist/llm-connections.js +13 -0
  44. package/node_modules/@maka/core/dist/model-call-attempt.js +2 -0
  45. package/node_modules/@maka/core/dist/model-failure.js +40 -0
  46. package/node_modules/@maka/core/dist/model-metadata.generated.js +4 -4
  47. package/node_modules/@maka/core/dist/model-metadata.js +7 -3
  48. package/node_modules/@maka/core/dist/model-web-search.js +5 -4
  49. package/node_modules/@maka/core/dist/provider-registry.js +45 -4
  50. package/node_modules/@maka/core/dist/record-schema.js +9 -2
  51. package/node_modules/@maka/core/dist/redaction.js +35 -0
  52. package/node_modules/@maka/core/dist/run-composition.js +77 -2
  53. package/node_modules/@maka/core/dist/runtime-boundary.js +31 -11
  54. package/node_modules/@maka/core/dist/runtime-event.js +37 -6
  55. package/node_modules/@maka/core/dist/runtime-handoff.js +98 -0
  56. package/node_modules/@maka/core/dist/runtime-invocation.js +1 -1
  57. package/node_modules/@maka/core/dist/runtime-logical-execution.js +112 -0
  58. package/node_modules/@maka/core/dist/session.js +84 -56
  59. package/node_modules/@maka/core/dist/slash-command-catalog.js +3 -0
  60. package/node_modules/@maka/core/dist/tool-result-archive-evidence.js +20 -0
  61. package/node_modules/@maka/core/dist/tool-result-record-schema.js +2 -1
  62. package/node_modules/@maka/core/dist/usage-record-schema.js +26 -35
  63. package/node_modules/@maka/core/package.json +5 -0
  64. package/node_modules/@maka/eval/dist/experiment-directory.js +4 -13
  65. package/node_modules/@maka/mcp/dist/index.js +131 -1
  66. package/node_modules/@maka/runtime/dist/agent-run-inspect.js +0 -13
  67. package/node_modules/@maka/runtime/dist/agent-run.js +351 -105
  68. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +29 -13
  69. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +109 -34
  70. package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +184 -183
  71. package/node_modules/@maka/runtime/dist/archive-read-tool.js +2 -2
  72. package/node_modules/@maka/runtime/dist/context-budget-helpers.js +0 -16
  73. package/node_modules/@maka/runtime/dist/context-budget.js +14 -9
  74. package/node_modules/@maka/runtime/dist/conversation-copy.js +158 -15
  75. package/node_modules/@maka/runtime/dist/github-copilot-oauth-enrollment.js +3 -3
  76. package/node_modules/@maka/runtime/dist/goal-continuation.js +36 -4
  77. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +18 -9
  78. package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +30 -22
  79. package/node_modules/@maka/runtime/dist/history-compaction.js +29 -4
  80. package/node_modules/@maka/runtime/dist/ledger-tool-result-archive-reader.js +174 -0
  81. package/node_modules/@maka/runtime/dist/mcp-tools.js +7 -2
  82. package/node_modules/@maka/runtime/dist/message-authority.js +9 -0
  83. package/node_modules/@maka/runtime/dist/model-adapter.js +19 -98
  84. package/node_modules/@maka/runtime/dist/model-factory.js +19 -81
  85. package/node_modules/@maka/runtime/dist/model-fetcher.js +49 -2
  86. package/node_modules/@maka/runtime/dist/model-history.js +0 -112
  87. package/node_modules/@maka/runtime/dist/model-runtime.js +85 -111
  88. package/node_modules/@maka/runtime/dist/native-web-search-tool.js +2 -1
  89. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +2 -2
  90. package/node_modules/@maka/runtime/dist/opencode-session-header.js +4 -2
  91. package/node_modules/@maka/runtime/dist/plugin-runtime.js +20 -10
  92. package/node_modules/@maka/runtime/dist/plugin-tool-service.js +205 -0
  93. package/node_modules/@maka/runtime/dist/provider-error-classification.js +84 -123
  94. package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +76 -109
  95. package/node_modules/@maka/runtime/dist/request-shape.js +23 -0
  96. package/node_modules/@maka/runtime/dist/run-handoff-gate.js +97 -0
  97. package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +83 -55
  98. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +121 -159
  99. package/node_modules/@maka/runtime/dist/runtime-kernel.js +157 -112
  100. package/node_modules/@maka/runtime/dist/runtime-ledger-repair.js +160 -79
  101. package/node_modules/@maka/runtime/dist/runtime-read-model.js +30 -89
  102. package/node_modules/@maka/runtime/dist/runtime-resume.js +80 -4
  103. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +5 -0
  104. package/node_modules/@maka/runtime/dist/session-manager.js +171 -288
  105. package/node_modules/@maka/runtime/dist/session-projection-helpers.js +0 -27
  106. package/node_modules/@maka/runtime/dist/stream-graph-coordinator.js +21 -0
  107. package/node_modules/@maka/runtime/dist/stream-graph-supervisor-tools.js +0 -5
  108. package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +3 -4
  109. package/node_modules/@maka/runtime/dist/test-connection.js +16 -17
  110. package/node_modules/@maka/runtime/dist/tool-activation-identity.js +41 -0
  111. package/node_modules/@maka/runtime/dist/tool-availability.js +11 -1
  112. package/node_modules/@maka/runtime/dist/tool-result-archive-encoding.js +38 -0
  113. package/node_modules/@maka/runtime/dist/tool-result-archive-resource.js +63 -1
  114. package/node_modules/@maka/runtime/dist/tool-result-archive-transition.js +42 -12
  115. package/node_modules/@maka/runtime/dist/tool-result-archive.js +27 -4
  116. package/node_modules/@maka/runtime/dist/tool-runtime.js +3 -61
  117. package/node_modules/@maka/runtime/package.json +2 -0
  118. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +35 -0
  119. package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +9 -1
  120. package/node_modules/@maka/runtime-host/dist/client/connection.js +3 -0
  121. package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +179 -0
  122. package/node_modules/@maka/runtime-host/dist/client/host-handoff.js +275 -0
  123. package/node_modules/@maka/runtime-host/dist/client/host-retirement.js +26 -5
  124. package/node_modules/@maka/runtime-host/dist/client/index.js +2 -0
  125. package/node_modules/@maka/runtime-host/dist/client/launcher.js +3 -0
  126. package/node_modules/@maka/runtime-host/dist/client/remote-compatibility-error.js +2 -0
  127. package/node_modules/@maka/runtime-host/dist/client/session-subscription.js +46 -5
  128. package/node_modules/@maka/runtime-host/dist/control/endpoint.js +1 -3
  129. package/node_modules/@maka/runtime-host/dist/node-error.js +22 -0
  130. package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
  131. package/node_modules/@maka/runtime-host/dist/operator/managed-deployment.js +1 -3
  132. package/node_modules/@maka/runtime-host/dist/operator/retirement-cancellation.js +20 -0
  133. package/node_modules/@maka/runtime-host/dist/peer-mesh/limits.js +0 -1
  134. package/node_modules/@maka/runtime-host/dist/peer-mesh/model.js +1 -1
  135. package/node_modules/@maka/runtime-host/dist/peer-reachability/publisher.js +1 -3
  136. package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +21 -12
  137. package/node_modules/@maka/runtime-host/dist/protocol/index.js +29 -1
  138. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
  139. package/node_modules/@maka/runtime-host/dist/protocol/plan.js +1 -0
  140. package/node_modules/@maka/runtime-host/dist/protocol/plugin-platform.js +27 -5
  141. package/node_modules/@maka/runtime-host/dist/protocol/project-catalog.js +2 -2
  142. package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +40 -20
  143. package/node_modules/@maka/runtime-host/dist/protocol/session-transcript.js +0 -5
  144. package/node_modules/@maka/runtime-host/dist/protocol/session-turns.js +29 -58
  145. package/node_modules/@maka/runtime-host/dist/protocol/turn.js +11 -1
  146. package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +31 -2
  147. package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
  148. package/node_modules/@maka/runtime-host/dist/server/agent-graph-coordinator.js +0 -3
  149. package/node_modules/@maka/runtime-host/dist/server/canonical-turn-snapshot.js +19 -4
  150. package/node_modules/@maka/runtime-host/dist/server/connection-session.js +7 -6
  151. package/node_modules/@maka/runtime-host/dist/server/execution-artifacts.js +32 -32
  152. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +144 -33
  153. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +5 -5
  154. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +45 -16
  155. package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +40 -1
  156. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +96 -17
  157. package/node_modules/@maka/runtime-host/dist/server/host-residency-registry.js +13 -7
  158. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-admission-registry.js +3 -1
  159. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +124 -62
  160. package/node_modules/@maka/runtime-host/dist/server/interactive-run-composer.js +47 -22
  161. package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +76 -6
  162. package/node_modules/@maka/runtime-host/dist/server/plugin-platform-coordinator.js +6 -0
  163. package/node_modules/@maka/runtime-host/dist/server/plugin-platform.js +6 -0
  164. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +325 -73
  165. package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +37 -2
  166. package/node_modules/@maka/runtime-host/dist/server/serial-outbound-writer.js +45 -3
  167. package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +50 -7
  168. package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +87 -25
  169. package/node_modules/@maka/runtime-host/dist/server/session-retirement-coordinator.js +5 -2
  170. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +12 -55
  171. package/node_modules/@maka/runtime-host/dist/server/session-sidecar-purge.js +1 -16
  172. package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +0 -1
  173. package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +460 -70
  174. package/node_modules/@maka/runtime-host/dist/server/shared-session-transcript.js +1 -1
  175. package/node_modules/@maka/runtime-host/dist/server/storage-maintenance.js +109 -0
  176. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +20 -2
  177. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +13 -4
  178. package/node_modules/@maka/runtime-host/dist/server/workhub-message-attachments.js +46 -0
  179. package/node_modules/@maka/storage/dist/artifact-store.js +21 -10
  180. package/node_modules/@maka/storage/dist/artifact-stores.js +1 -1
  181. package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +0 -5
  182. package/node_modules/@maka/storage/dist/codex-session-adapter.js +284 -198
  183. package/node_modules/@maka/storage/dist/context-offload-snapshot.js +325 -0
  184. package/node_modules/@maka/storage/dist/execution-stores.js +9 -7
  185. package/node_modules/@maka/storage/dist/file-session-repository.js +1012 -0
  186. package/node_modules/@maka/storage/dist/git-exec.js +61 -0
  187. package/node_modules/@maka/storage/dist/git-worktree-child-executor.js +8 -35
  188. package/node_modules/@maka/storage/dist/goal-authority.js +2 -2
  189. package/node_modules/@maka/storage/dist/interaction-store.js +2 -2
  190. package/node_modules/@maka/storage/dist/opencode-session-adapter.js +0 -4
  191. package/node_modules/@maka/storage/dist/operational-state-backup.js +9 -5
  192. package/node_modules/@maka/storage/dist/project-catalog.js +2 -23
  193. package/node_modules/@maka/storage/dist/runtime-event-invariants.js +2 -2
  194. package/node_modules/@maka/storage/dist/runtime-event-persistence.js +1 -0
  195. package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +8 -3
  196. package/node_modules/@maka/storage/dist/runtime-transcript-query.js +308 -0
  197. package/node_modules/@maka/storage/dist/scheduled-task-store.js +249 -283
  198. package/node_modules/@maka/storage/dist/session-bundle-manifest.js +5 -17
  199. package/node_modules/@maka/storage/dist/session-bundle-policy.js +26 -5
  200. package/node_modules/@maka/storage/dist/session-copy-cleanup.js +2 -1
  201. package/node_modules/@maka/storage/dist/session-message-projection.js +15 -0
  202. package/node_modules/@maka/storage/dist/session-repository.js +767 -0
  203. package/node_modules/@maka/storage/dist/session-store.js +25 -99
  204. package/node_modules/@maka/storage/dist/sqlite-artifact-metadata.js +10 -3
  205. package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +24 -8
  206. package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +7 -1
  207. package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +44 -1
  208. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +130 -27
  209. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +109 -892
  210. package/node_modules/@maka/storage/dist/storage-id.js +28 -0
  211. package/node_modules/@maka/storage/dist/tool-result-archive-evidence.js +138 -0
  212. package/node_modules/@maka/storage/dist/workspace-identity.js +2 -15
  213. package/node_modules/@maka/storage/dist/workspace-version-authority-internal.js +1 -8
  214. package/node_modules/@maka/storage/package.json +2 -0
  215. package/package.json +1 -1
@@ -29,7 +29,7 @@ import { AiSdkTurn } from './ai-sdk-turn.js';
29
29
  import { buildInvalidMakaTool } from './ai-sdk-tool-repair.js';
30
30
  import { ToolAvailabilityRuntime } from './tool-availability.js';
31
31
  import { MEMORY_EXTRACT_TOOL_NAME, MEMORY_REMEMBER_TOOL_NAME, buildMemoryExtractionTriggerTools, } from './memory-extraction.js';
32
- import { modelUsesNativeOpenAiResponses, resolveModelRuntime } from './model-runtime.js';
32
+ import { resolveModelRuntime } from './model-runtime.js';
33
33
  import { routeApplyPatchTools } from './apply-patch-profile.js';
34
34
  import { bindToolResultArchiveDecoder } from './tool-result-archive-capability.js';
35
35
  import { resolveSelectedModelContextWindow } from './context-budget-policy.js';
@@ -71,7 +71,8 @@ export class AiSdkBackend {
71
71
  messageProjection;
72
72
  providerTelemetry;
73
73
  resolvedProviderOptions;
74
- toolAvailabilityRuntime;
74
+ memoryTools;
75
+ applyPatchProfile;
75
76
  /** Bounds outstanding Code Mode cells on this backend. */
76
77
  codeCellAdmission = new AdmissionLimiter(MAX_ACTIVE_CODE_MODE_CELLS);
77
78
  /**
@@ -111,15 +112,17 @@ export class AiSdkBackend {
111
112
  // One resolved options value for every reader: the main call, the
112
113
  // auxiliary memory-extraction call, and the provider request all use the
113
114
  // same options value, so they cannot disagree on what was sent.
115
+ const runtime = resolveModelRuntime(input.connection, input.modelId);
114
116
  this.resolvedProviderOptions =
115
117
  input.providerOptions ??
116
- buildProviderOptions(input.connection, input.modelId, input.header.thinkingLevel);
118
+ buildProviderOptions(input.connection, input.modelId, input.header.thinkingLevel, runtime);
117
119
  this.modelAdapter = new ModelAdapter({
118
120
  sessionId: input.sessionId,
119
121
  connection: input.connection,
120
122
  apiKey: input.apiKey,
121
123
  modelId: input.modelId,
122
124
  modelFactory: input.modelFactory,
125
+ resolvedRuntime: runtime,
123
126
  // `input.providerOptions` is an override escape hatch: when set it owns
124
127
  // the whole provider-options namespace (including reasoning effort), and
125
128
  // the computed defaults are dropped entirely. Keep providerOptions the
@@ -144,8 +147,8 @@ export class AiSdkBackend {
144
147
  assertModelCallAccountingReady: input.assertModelCallAccountingReady,
145
148
  beforeRunProviderDispatch: input.beforeRunProviderDispatch,
146
149
  });
147
- const runtime = resolveModelRuntime(input.connection, input.modelId);
148
150
  const applyPatchProfile = runtime.applyPatchProfile;
151
+ this.applyPatchProfile = applyPatchProfile;
149
152
  this.messageProjection = new AiSdkMessageProjection({
150
153
  modelAdapter: this.modelAdapter,
151
154
  applyPatchProfile,
@@ -167,7 +170,7 @@ export class AiSdkBackend {
167
170
  if (input.tools.some((tool) => tool.name === MEMORY_REMEMBER_TOOL_NAME || tool.name === MEMORY_EXTRACT_TOOL_NAME)) {
168
171
  throw new Error('Long-term Memory trigger tool names are reserved by Runtime');
169
172
  }
170
- const memoryTools = input.memoryExtraction
173
+ this.memoryTools = input.memoryExtraction
171
174
  ? buildMemoryExtractionTriggerTools({
172
175
  capabilities: input.memoryExtraction,
173
176
  snapshot: (trigger, context) => this.memorySourceSnapshot(trigger, context),
@@ -176,16 +179,22 @@ export class AiSdkBackend {
176
179
  if (turn)
177
180
  turn.memoryExtractRequested = true;
178
181
  },
179
- ...(modelUsesNativeOpenAiResponses(input.connection, input.modelId)
182
+ ...(input.connection.providerType === 'openai' && runtime.wire === 'openai-responses'
180
183
  ? { unsupportedReason: 'provider_unsupported' }
181
184
  : {}),
182
185
  })
183
186
  : [];
184
- const modelTools = routeApplyPatchTools(input.tools, applyPatchProfile);
185
- this.toolAvailabilityRuntime = new ToolAvailabilityRuntime(
186
- // The archive decoder is a runtime protocol tool, not a host binding:
187
- // this session's placeholders name it, so this session advertises it.
188
- bindToolResultArchiveDecoder([...modelTools, ...memoryTools], input.toolResultArchive), input.toolAvailability, buildInvalidMakaTool());
187
+ }
188
+ snapshotToolAvailability() {
189
+ const hostTools = Object.freeze([...(this.input.resolveTools?.() ?? this.input.tools)]);
190
+ if (hostTools.some((tool) => tool.name === MEMORY_REMEMBER_TOOL_NAME || tool.name === MEMORY_EXTRACT_TOOL_NAME)) {
191
+ throw new Error('Long-term Memory trigger tool names are reserved by Runtime');
192
+ }
193
+ const modelTools = routeApplyPatchTools(hostTools, this.applyPatchProfile);
194
+ return {
195
+ hostTools,
196
+ runtime: new ToolAvailabilityRuntime(bindToolResultArchiveDecoder([...modelTools, ...this.memoryTools], this.input.toolResultArchive), this.input.toolAvailability, buildInvalidMakaTool()),
197
+ };
189
198
  }
190
199
  memorySourceSnapshot(trigger, context) {
191
200
  if (trigger !== 'remember')
@@ -206,11 +215,11 @@ export class AiSdkBackend {
206
215
  createToolRuntime(identity) {
207
216
  const input = this.input;
208
217
  return new ToolRuntime({
218
+ inheritedSandboxBoundaryDenied: identity.inheritedSandboxBoundaryDenied,
209
219
  sessionId: input.sessionId,
210
220
  header: input.header,
211
221
  connection: input.connection,
212
222
  modelId: input.modelId,
213
- appendMessage: input.appendMessage,
214
223
  readExecutionBoundary: input.readExecutionBoundary,
215
224
  createSandboxBoundaryRequest: input.createSandboxBoundaryRequest,
216
225
  settleSandboxBoundaryRequest: input.settleSandboxBoundaryRequest,
@@ -241,6 +250,12 @@ export class AiSdkBackend {
241
250
  // --------------------------------------------------------------------------
242
251
  // send()
243
252
  // --------------------------------------------------------------------------
253
+ async prepareRunComposition(input) {
254
+ if (!this.input.beforeRunProviderDispatch) {
255
+ throw new Error('Backend has no durable Run Composition preparation authority');
256
+ }
257
+ await this.input.beforeRunProviderDispatch({ sessionId: this.sessionId, ...input });
258
+ }
244
259
  openTurnScope(input) {
245
260
  const turn = new AiSdkTurn({
246
261
  backend: this.input,
@@ -248,7 +263,7 @@ export class AiSdkBackend {
248
263
  messageProjection: this.messageProjection,
249
264
  providerTelemetry: this.providerTelemetry,
250
265
  compaction: this.compaction,
251
- toolAvailabilityRuntime: this.toolAvailabilityRuntime,
266
+ snapshotToolAvailability: () => this.snapshotToolAvailability(),
252
267
  codeCellAdmission: this.codeCellAdmission,
253
268
  resolvedProviderOptions: this.resolvedProviderOptions,
254
269
  session: this.turnSessionState,
@@ -257,6 +272,7 @@ export class AiSdkBackend {
257
272
  maxSteps: this.maxSteps,
258
273
  providerRetrySleep: this.providerRetrySleep,
259
274
  createToolRuntime: (owner) => this.createToolRuntime({
275
+ inheritedSandboxBoundaryDenied: input.continuation?.sandboxBoundaryDenied === true,
260
276
  turnId: owner.turnId,
261
277
  runId: owner.runId,
262
278
  invocationId: input.invocationId ?? input.runId,
@@ -17,9 +17,9 @@
17
17
  * under the License.
18
18
  */
19
19
  import { compactionDecisionDiagnosticPatch } from './compaction-boundary.js';
20
- import { buildContextBudgetDiagnosticShell, mergeContextBudgetDiagnostic, } from './context-budget.js';
20
+ import { buildContextBudgetDiagnosticShell, mergeContextBudgetDiagnostic, mergeContextBudgetDiagnosticPatches, } from './context-budget.js';
21
21
  import { isHistoryCompactContentEvent } from './history-compaction.js';
22
- import { canContinueHistoryCompactCheckpointForModel, canReplayHistoryCompactCheckpointForModel, matchHistoryCompactCheckpointPrefix, projectHistoryCompactCheckpointReplay, } from './history-compact-checkpoint.js';
22
+ import { canContinueHistoryCompactCheckpointForModel, canReplayHistoryCompactCheckpointForModel, historyCompactSourceDigest, matchHistoryCompactCheckpointPrefix, projectHistoryCompactCheckpointReplay, } from './history-compact-checkpoint.js';
23
23
  import { HistoryCompactSummarizerError, isMalformedHistoryCompactSummaryReason, } from './history-compact-error.js';
24
24
  import { createHash } from 'node:crypto';
25
25
  import { rewriteActiveToolResultsInMessages, } from './active-tool-result-prune.js';
@@ -186,21 +186,28 @@ export class AiSdkCompaction {
186
186
  : {}),
187
187
  ...(automaticMemoryBoundary ? { memoryExtractionBoundary: automaticMemoryBoundary } : {}),
188
188
  ...(previousCheckpoint ? { previousCheckpoint } : {}),
189
- summarize: async ({ coveredRuntimeEvents, newlyFoldedRuntimeEvents, previousCheckpoint }) => await this.summarizeWithFailureCircuit(summarizer, {
190
- sessionId: this.sessionId,
191
- turnId: input.turnId,
192
- runId: input.runId,
193
- source: {
194
- foldedRuntimeEvents: [...coveredRuntimeEvents],
195
- ...(input.runtimeContextInvocations
196
- ? { invocations: input.runtimeContextInvocations }
197
- : {}),
198
- },
199
- newlyFoldedRuntimeEvents: [...newlyFoldedRuntimeEvents],
200
- ...(previousCheckpoint ? { previousCheckpoint } : {}),
201
- abortSignal: historyCompactAbortController.signal,
202
- ...(tracker ? { providerRequestTracker: tracker } : {}),
203
- }),
189
+ // The planner projects the covered span to its effective view before
190
+ // summarizing and pins its digest as coverage.effectiveSourceDigest:
191
+ // the summary can never quote a body a durable transition removed, and
192
+ // a later transition invalidates the checkpoint at replay (#4845).
193
+ projectEffectiveCoverage: (covered) => this.foldEffectiveModelHistory(covered),
194
+ summarize: async ({ coveredRuntimeEvents, newlyFoldedRuntimeEvents, previousCheckpoint, }) => {
195
+ return await this.summarizeWithFailureCircuit(summarizer, {
196
+ sessionId: this.sessionId,
197
+ turnId: input.turnId,
198
+ runId: input.runId,
199
+ source: {
200
+ foldedRuntimeEvents: [...coveredRuntimeEvents],
201
+ ...(input.runtimeContextInvocations
202
+ ? { invocations: input.runtimeContextInvocations }
203
+ : {}),
204
+ },
205
+ newlyFoldedRuntimeEvents: [...newlyFoldedRuntimeEvents],
206
+ ...(previousCheckpoint ? { previousCheckpoint } : {}),
207
+ abortSignal: historyCompactAbortController.signal,
208
+ ...(tracker ? { providerRequestTracker: tracker } : {}),
209
+ });
210
+ },
204
211
  });
205
212
  if (historyCompactAbortController.signal.aborted) {
206
213
  return { outcome: { kind: 'failed', reason: 'aborted' } };
@@ -333,9 +340,35 @@ export class AiSdkCompaction {
333
340
  */
334
341
  async foldEffectiveModelHistory(events) {
335
342
  const loaded = await this.loadModelProjectionTransitions();
336
- if (loaded.transitions.length === 0)
343
+ if (loaded.transitions.length === 0 && loaded.unreadableTargets.size === 0) {
337
344
  return [...events];
338
- return reduceEffectiveModelProjections(events, loaded.transitions).events;
345
+ }
346
+ // Forward the unreadable set: a target whose transition record this build
347
+ // cannot decode must fold to the withholding sentinel here too, or this
348
+ // path becomes the one consumer that replays the body a record removed.
349
+ return reduceEffectiveModelProjections(events, loaded.transitions, loaded.unreadableTargets)
350
+ .events;
351
+ }
352
+ /**
353
+ * Whether the checkpoint's pinned effective view still is the covered
354
+ * prefix's effective view. The raw identity match happens later in the
355
+ * replay authority; this gate is about content currency: a projection
356
+ * transition committed after the fold changes what the model may see of the
357
+ * covered span without touching the raw ledger, and the checkpoint's summary
358
+ * or provider state must not survive that drift (#4845 review).
359
+ */
360
+ checkpointEffectiveCoverageMatches(checkpoint, effectiveEvents) {
361
+ const pinned = checkpoint.coverage.effectiveSourceDigest;
362
+ if (pinned === undefined)
363
+ return false;
364
+ const covered = effectiveEvents
365
+ .filter(isHistoryCompactContentEvent)
366
+ .slice(0, checkpoint.coverage.eventCount);
367
+ if (covered.length !== checkpoint.coverage.eventCount)
368
+ return false;
369
+ if (covered.at(-1)?.id !== checkpoint.coverage.through.runtimeEventId)
370
+ return false;
371
+ return historyCompactSourceDigest(covered) === pinned;
339
372
  }
340
373
  /**
341
374
  * Fold the durable transition ledger onto this session's prior history, and
@@ -422,10 +455,39 @@ export class AiSdkCompaction {
422
455
  }
423
456
  if (loadedCheckpoint &&
424
457
  canReplayHistoryCompactCheckpointForModel(loadedCheckpoint, this.input.connection, this.targetConnectionId, this.input.modelId)) {
425
- nextPolicy = {
426
- ...nextPolicy,
427
- historyCompact: { ...nextPolicy.historyCompact, checkpoint: loadedCheckpoint },
428
- };
458
+ // Raw identity first: a coverage miss keeps the apply-stage path, whose
459
+ // matcher reports the real identity reason (coverage_miss and friends).
460
+ // This gate is only for the case where the raw identity matches but a
461
+ // projection transition committed after the fold changed the effective
462
+ // view the summary (or provider state) was built from (#4845 review).
463
+ const rawIdentityMatch = matchHistoryCompactCheckpointPrefix(loadedCheckpoint, runtimeContext.filter(isHistoryCompactContentEvent));
464
+ if (rawIdentityMatch.reason) {
465
+ nextPolicy = {
466
+ ...nextPolicy,
467
+ historyCompact: { ...nextPolicy.historyCompact, checkpoint: loadedCheckpoint },
468
+ };
469
+ }
470
+ else if (this.checkpointEffectiveCoverageMatches(loadedCheckpoint, effective.events)) {
471
+ nextPolicy = {
472
+ ...nextPolicy,
473
+ historyCompact: { ...nextPolicy.historyCompact, checkpoint: loadedCheckpoint },
474
+ };
475
+ }
476
+ else {
477
+ // Rejecting keeps the stale block from restoring what the transition
478
+ // removed; the next fold re-summarizes (#4845 review).
479
+ diagnosticPatch = mergeContextBudgetDiagnosticPatches(diagnosticPatch, compactionDecisionDiagnosticPatch({
480
+ stage: 'priorReplay',
481
+ sourceKind: 'runtimeEvents',
482
+ decision: 'failedOpen',
483
+ phase: 'pre_turn',
484
+ boundaryKind: 'historyCompact',
485
+ ...(loadedCheckpoint.coverage.effectiveSourceDigest !== undefined
486
+ ? { boundaryIds: [loadedCheckpoint.checkpointId] }
487
+ : {}),
488
+ failOpenReason: 'effective_history_changed',
489
+ }));
490
+ }
429
491
  }
430
492
  return {
431
493
  policy: nextPolicy,
@@ -543,12 +605,15 @@ export class AiSdkCompaction {
543
605
  headAnchor.sessionId !== this.sessionId ||
544
606
  headAnchor.turnId !== input.turnId ||
545
607
  headAnchor.role !== 'user' ||
546
- headAnchor.author !== 'user' ||
608
+ (headAnchor.author !== 'user' && headAnchor.author !== 'host') ||
547
609
  !isHistoryCompactContentEvent(headAnchor)) {
548
610
  return undefined;
549
611
  }
612
+ // Handoff replay is an authenticated predecessor prefix, including the
613
+ // current logical Turn. The live reader remains physical-run scoped.
614
+ const handoff = input.continuation?.sourceTurnId === input.turnId;
550
615
  const priorContentEvents = (input.runtimeContext ?? [])
551
- .filter((event) => event.turnId !== input.turnId)
616
+ .filter((event) => handoff || event.turnId !== input.turnId)
552
617
  .filter(isHistoryCompactContentEvent);
553
618
  const state = new MidTurnCapacityCompactState(headAnchor, priorContentEvents, input.runtimeContextInvocations ?? [], resolveDeclaredContextWindow(this.input.connection, this.input.modelId));
554
619
  // Seed the turn's FIRST request with the last request the provider
@@ -605,9 +670,8 @@ export class AiSdkCompaction {
605
670
  state.replyReserveTokens = replyReserveTokens(lastUsage?.outputTokens);
606
671
  }
607
672
  }
608
- // The turn's first request folds as a pre_turn boundary, like the
609
- // reactive step-0 recovery; later steps fold mid_turn.
610
- const phase = options.stepNumber === 0 ? 'pre_turn' : 'mid_turn';
673
+ // Physical step zero may already be mid-Turn after a handoff.
674
+ const phase = state.compactionPhase(options.stepNumber);
611
675
  // A skipped trigger is never silent: every failure-driven skip records a
612
676
  // failedOpen decision.
613
677
  const failOpen = (failOpenReason) => {
@@ -767,15 +831,15 @@ export class AiSdkCompaction {
767
831
  const currentTurnEvents = turnLedger
768
832
  .filter((event) => event.turnId === turnId)
769
833
  .filter(isHistoryCompactContentEvent);
770
- // The head anchor is persisted before backend.send() is invoked, so
771
- // its absence is a wiring error, not replication lag — fail open now.
772
- if (!currentTurnEvents.some((event) => event.id === state.headAnchor.id)) {
834
+ const orderedEvents = [...state.priorContentEvents, ...currentTurnEvents];
835
+ // A fresh anchor belongs to the live ledger; after handoff it belongs to
836
+ // the authenticated predecessor prefix. Neither path manufactures it.
837
+ if (!orderedEvents.some((event) => event.id === state.headAnchor.id)) {
773
838
  return {
774
839
  decision: 'fail',
775
840
  diagnosticReason: 'head_anchor_not_durable',
776
841
  };
777
842
  }
778
- const orderedEvents = [...state.priorContentEvents, ...currentTurnEvents];
779
843
  const memoryDecision = input.memoryCompactionDecision?.();
780
844
  const plan = await planHistoryCompaction({
781
845
  sessionId: this.sessionId,
@@ -804,7 +868,12 @@ export class AiSdkCompaction {
804
868
  },
805
869
  }
806
870
  : {}),
871
+ projectEffectiveCoverage: (covered) => this.foldEffectiveModelHistory(covered),
807
872
  summarize: async ({ coveredRuntimeEvents, newlyFoldedRuntimeEvents, previousCheckpoint }) => {
873
+ // Same contract as the standalone path: the planner hands the
874
+ // effective (transition-folded) view to the summarizer and pins its
875
+ // digest, so a summary can never quote a body a durable transition
876
+ // removed (#4845).
808
877
  return await this.summarizeWithFailureCircuit(summarizer, {
809
878
  sessionId: this.sessionId,
810
879
  turnId,
@@ -902,7 +971,7 @@ export class AiSdkCompaction {
902
971
  const state = input.midTurnState;
903
972
  if (input.retryAlreadyUsed || !state)
904
973
  return undefined;
905
- if (this.modelAdapter.classifyError(input.error) !== 'ContextLength')
974
+ if (this.modelAdapter.classifyError(input.error) !== 'context_overflow')
906
975
  return undefined;
907
976
  const eligibleImages = collectHistoricalImageToolResults(state.priorContentEvents);
908
977
  const imageOmission = omitHistoricalImageToolResults(input.currentMessages, eligibleImages);
@@ -914,7 +983,7 @@ export class AiSdkCompaction {
914
983
  state.baselineTokens = undefined;
915
984
  return { messages: imageOmission.messages };
916
985
  }
917
- const phase = input.stepNumber === 0 ? 'pre_turn' : 'mid_turn';
986
+ const phase = state.compactionPhase(input.stepNumber);
918
987
  // Entering the module spends the send's one attempt whether or not a fold
919
988
  // comes out of it; only a selected projection sets `applied`.
920
989
  state.compactionAttemptedThisSend = true;
@@ -1140,6 +1209,12 @@ export class MidTurnCapacityCompactState {
1140
1209
  this.priorInvocations = priorInvocations;
1141
1210
  this.capacity = capacity;
1142
1211
  }
1212
+ compactionPhase(stepNumber) {
1213
+ return stepNumber === 0 &&
1214
+ !this.priorContentEvents.some((event) => event.id === this.headAnchor.id)
1215
+ ? 'pre_turn'
1216
+ : 'mid_turn';
1217
+ }
1143
1218
  }
1144
1219
  /**
1145
1220
  * The baseline one accepted request leaves for the next: its real input plus