maka-agent 0.2.0-dev.8.20260830 → 0.2.0-dev.9.20260831

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 (63) hide show
  1. package/dist/cli-core.js +11 -0
  2. package/dist/runtime-host-cli.js +44 -0
  3. package/dist/runtime-host-installed-update-activator.js +70 -33
  4. package/dist/runtime-host-installed-update-coordinator.js +11 -96
  5. package/dist/runtime-host-lifecycle-transaction.js +2 -0
  6. package/dist/runtime-host-local-handoff.js +169 -38
  7. package/dist/runtime-host-local-source-retirement.js +103 -0
  8. package/dist/runtime-host-local-target-activation.js +141 -0
  9. package/dist/runtime-host-peer-management-command.js +10 -4
  10. package/native/runtime-host-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
  11. package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
  12. package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
  13. package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
  14. package/node_modules/@maka/core/dist/artifacts.js +23 -15
  15. package/node_modules/@maka/core/dist/durable-tool-result-projection.js +124 -0
  16. package/node_modules/@maka/core/dist/llm-connections.js +20 -0
  17. package/node_modules/@maka/core/dist/runtime-event.js +28 -3
  18. package/node_modules/@maka/core/dist/session-name.js +0 -1
  19. package/node_modules/@maka/core/package.json +1 -0
  20. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +95 -19
  21. package/node_modules/@maka/runtime/dist/conversation-copy.js +67 -0
  22. package/node_modules/@maka/runtime/dist/durable-tool-result-projection.js +390 -0
  23. package/node_modules/@maka/runtime/dist/model-history.js +12 -39
  24. package/node_modules/@maka/runtime/dist/recovery-resolver.js +18 -14
  25. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +20 -11
  26. package/node_modules/@maka/runtime/dist/session-manager.js +0 -170
  27. package/node_modules/@maka/runtime/dist/tool-runtime.js +118 -71
  28. package/node_modules/@maka/runtime/package.json +1 -0
  29. package/node_modules/@maka/runtime-host/dist/candidate-entry.js +6 -1
  30. package/node_modules/@maka/runtime-host/dist/candidate-launch-owner-guard.js +13 -0
  31. package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +4 -1
  32. package/node_modules/@maka/runtime-host/dist/client/host-profile.js +27 -1
  33. package/node_modules/@maka/runtime-host/dist/client/index.js +1 -1
  34. package/node_modules/@maka/runtime-host/dist/client/launcher.js +8 -2
  35. package/node_modules/@maka/runtime-host/dist/client/peer-client.js +11 -1
  36. package/node_modules/@maka/runtime-host/dist/peer-mesh/index.js +1 -1
  37. package/node_modules/@maka/runtime-host/dist/peer-mesh/node.js +394 -113
  38. package/node_modules/@maka/runtime-host/dist/peer-mesh/store.js +90 -21
  39. package/node_modules/@maka/runtime-host/dist/protocol/index.js +8 -1
  40. package/node_modules/@maka/runtime-host/dist/protocol/oauth.js +60 -7
  41. package/node_modules/@maka/runtime-host/dist/protocol/usage-pricing.js +4 -0
  42. package/node_modules/@maka/runtime-host/dist/server/artifact-coordinator.js +2 -4
  43. package/node_modules/@maka/runtime-host/dist/server/candidate.js +3 -0
  44. package/node_modules/@maka/runtime-host/dist/server/client-capability-recovery.js +10 -7
  45. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +5 -1
  46. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +9 -1
  47. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +10 -0
  48. package/node_modules/@maka/runtime-host/dist/server/oauth-coordinator.js +91 -42
  49. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +10 -1
  50. package/node_modules/@maka/runtime-host/dist/server/usage-pricing-coordinator.js +45 -5
  51. package/node_modules/@maka/storage/dist/artifact-attachments.js +62 -11
  52. package/node_modules/@maka/storage/dist/artifact-store.js +11 -0
  53. package/node_modules/@maka/storage/dist/artifact-stores.js +4 -1
  54. package/node_modules/@maka/storage/dist/quiescent-session-snapshot.js +1154 -0
  55. package/node_modules/@maka/storage/dist/runtime-policy/connection-catalog-document.js +36 -0
  56. package/node_modules/@maka/storage/dist/runtime-policy/coordinator.js +213 -46
  57. package/node_modules/@maka/storage/dist/runtime-policy/document-io.js +1 -1
  58. package/node_modules/@maka/storage/dist/runtime-policy/oauth-login-receipt-document.js +186 -0
  59. package/node_modules/@maka/storage/dist/runtime-policy/onboarding-transaction.js +120 -1
  60. package/node_modules/@maka/storage/dist/runtime-policy-stores.js +2 -1
  61. package/node_modules/@maka/storage/dist/session-bundle-ustar.js +12 -0
  62. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +5 -0
  63. package/package.json +1 -1
@@ -0,0 +1,390 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+ import { decodeDurableToolResultProjection, DURABLE_TOOL_RESULT_PROJECTION_FAILURE, DURABLE_TOOL_RESULT_PROJECTION_MAX_JSON_DEPTH, DURABLE_TOOL_RESULT_PROJECTION_MAX_JSON_NODES, DURABLE_TOOL_RESULT_PROJECTION_MAX_PARTS, DURABLE_TOOL_RESULT_PROJECTION_VERSION, } from '@maka/core/durable-tool-result-projection';
20
+ import { MAX_READ_IMAGE_BYTES } from '@maka/core/attachments';
21
+ import { isCanonicalArtifactEntityId, normalizeArtifactImagePreviewMime, } from '@maka/core/artifacts';
22
+ import { isCanonicalStorageRef } from '@maka/core/events';
23
+ import { decodeCanonicalShellToolResultContent } from '@maka/core/shell-run-result';
24
+ import { markPersisted } from '@maka/core/persisted-value';
25
+ import { decodePersistedToolResultContent } from '@maka/core/tool-result-record-schema';
26
+ import { toolResultOutput } from './tool-result-output.js';
27
+ import { withToolResultArchiveResourceRef } from './tool-result-archive.js';
28
+ import { projectBashToolResultForModel } from './bash-model-output.js';
29
+ import { projectFileWriteToolResultForModel } from './file-tool-model-output.js';
30
+ const OMITTED_BINARY_TEXT = '[Binary tool output omitted from the durable model projection; repeat the tool call if it is still needed.]';
31
+ /**
32
+ * The only new-write codec from Runtime's tool-output contract into the
33
+ * provider-neutral durable projection protocol. It is total by construction:
34
+ * invalid, unsafe, or oversized output becomes one stable failure sentinel.
35
+ */
36
+ export function encodeDurableToolResultOutput(output, sessionId) {
37
+ try {
38
+ const projection = encodeOutput(output, sessionId);
39
+ return decodeDurableToolResultProjection(projection);
40
+ }
41
+ catch {
42
+ return DURABLE_TOOL_RESULT_PROJECTION_FAILURE;
43
+ }
44
+ }
45
+ export function encodeDurableToolResultOutputWithArtifacts(output, sessionId, planArtifact) {
46
+ if (!planArtifact || output.type !== 'content' || !hasInlineImage(output)) {
47
+ return encodeDurableToolResultOutput(output, sessionId);
48
+ }
49
+ return (async () => {
50
+ try {
51
+ const prepared = prepareContentProjection(output, sessionId, planArtifact);
52
+ const persisted = new Set();
53
+ for (const artifact of prepared.artifacts) {
54
+ if (persisted.has(artifact.ref.relativePath))
55
+ continue;
56
+ await artifact.persist();
57
+ persisted.add(artifact.ref.relativePath);
58
+ }
59
+ return prepared.projection;
60
+ }
61
+ catch {
62
+ return DURABLE_TOOL_RESULT_PROJECTION_FAILURE;
63
+ }
64
+ })();
65
+ }
66
+ export function encodeDefaultDurableToolResultOutput(result, sessionId) {
67
+ const image = sessionImageResult(result, sessionId);
68
+ if (image) {
69
+ try {
70
+ const mediaType = normalizeArtifactImagePreviewMime(image.mimeType);
71
+ if (!mediaType)
72
+ throw new Error('Image has an unsafe media type');
73
+ return decodeDurableToolResultProjection({
74
+ version: DURABLE_TOOL_RESULT_PROJECTION_VERSION,
75
+ kind: 'content',
76
+ parts: [{ kind: 'artifact', mediaType, ref: image.ref }],
77
+ });
78
+ }
79
+ catch {
80
+ return DURABLE_TOOL_RESULT_PROJECTION_FAILURE;
81
+ }
82
+ }
83
+ return encodeDurableToolResultOutput(typeof result === 'string'
84
+ ? { type: 'text', value: result }
85
+ : { type: 'json', value: result }, sessionId);
86
+ }
87
+ export function durableProjectionToToolResultOutput(projection) {
88
+ switch (projection.kind) {
89
+ case 'text':
90
+ return projection.isError
91
+ ? { type: 'error-text', value: projection.text }
92
+ : { type: 'text', value: projection.text };
93
+ case 'json':
94
+ return projection.isError
95
+ ? { type: 'error-json', value: projection.value }
96
+ : { type: 'json', value: projection.value };
97
+ case 'content':
98
+ return {
99
+ type: 'content',
100
+ value: projection.parts.map((part) => part.kind === 'text'
101
+ ? { type: 'text', text: part.text }
102
+ : {
103
+ type: 'text',
104
+ text: `[Artifact ${JSON.stringify(part.ref.kind === 'session_context' ? part.ref.refId : part.ref.relativePath)} (${part.mediaType}) is stored in this Session.]`,
105
+ }),
106
+ };
107
+ case 'execution_denied':
108
+ return {
109
+ type: 'execution-denied',
110
+ ...(projection.reason !== undefined ? { reason: projection.reason } : {}),
111
+ };
112
+ case 'failure':
113
+ return { type: 'error-text', value: projection.message };
114
+ }
115
+ }
116
+ export function rewriteDurableToolResultProjectionArtifactRefs(projection, rewrite) {
117
+ if (projection.kind !== 'content')
118
+ return projection;
119
+ return {
120
+ ...projection,
121
+ parts: projection.parts.map((part) => part.kind === 'artifact' ? { ...part, ref: rewrite(part.ref) } : part),
122
+ };
123
+ }
124
+ /** The single compatibility boundary for both current and legacy response events. */
125
+ export function decodeEffectiveToolResultProjection(content, sessionId) {
126
+ if (content.providerExecuted && content.providerOutput !== undefined) {
127
+ return { kind: 'provider_native', output: content.providerOutput };
128
+ }
129
+ if (content.modelProjection !== undefined) {
130
+ try {
131
+ return {
132
+ kind: 'projection',
133
+ projection: decodeDurableToolResultProjection(content.modelProjection),
134
+ legacyOutput: content.result,
135
+ };
136
+ }
137
+ catch {
138
+ return {
139
+ kind: 'projection',
140
+ projection: DURABLE_TOOL_RESULT_PROJECTION_FAILURE,
141
+ legacyOutput: content.result,
142
+ };
143
+ }
144
+ }
145
+ let output = withToolResultArchiveResourceRef(content.result);
146
+ if (isRetiredExploreAgentResult(output)) {
147
+ try {
148
+ output = decodePersistedToolResultContent(markPersisted(output));
149
+ }
150
+ catch {
151
+ return {
152
+ kind: 'invalid_legacy',
153
+ message: 'function_response contains an invalid retired tool result',
154
+ };
155
+ }
156
+ }
157
+ const shellResult = decodeCanonicalShellToolResultContent(output);
158
+ if (shellResult.state === 'invalid') {
159
+ return {
160
+ kind: 'invalid_legacy',
161
+ message: 'function_response contains an invalid shell tool result',
162
+ };
163
+ }
164
+ if (shellResult.state === 'valid')
165
+ output = shellResult.content;
166
+ output =
167
+ content.name === 'Bash'
168
+ ? projectBashToolResultForModel(output)
169
+ : projectFileWriteToolResultForModel(content.name, output);
170
+ const projection = content.isError === true
171
+ ? encodeDurableToolResultOutput(compatibilityErrorOutput(output), sessionId)
172
+ : encodeDefaultDurableToolResultOutput(output, sessionId);
173
+ if (projection.kind === 'failure' && isLegacyPathImageResult(output, sessionId)) {
174
+ return { kind: 'legacy_output', output };
175
+ }
176
+ return {
177
+ kind: 'projection',
178
+ projection,
179
+ legacyOutput: output,
180
+ };
181
+ }
182
+ function compatibilityErrorOutput(output) {
183
+ return output !== null &&
184
+ typeof output === 'object' &&
185
+ !Array.isArray(output) &&
186
+ output.kind === 'text' &&
187
+ typeof output.text === 'string'
188
+ ? { type: 'error-text', value: new Error(output.text).toString() }
189
+ : toolResultOutput(output, true);
190
+ }
191
+ export function compatibilityToolResultProjection(content, sessionId) {
192
+ const effective = decodeEffectiveToolResultProjection(content, sessionId);
193
+ if (effective.kind === 'provider_native')
194
+ return undefined;
195
+ if (effective.kind === 'legacy_output')
196
+ return DURABLE_TOOL_RESULT_PROJECTION_FAILURE;
197
+ return effective.kind === 'projection'
198
+ ? effective.projection
199
+ : DURABLE_TOOL_RESULT_PROJECTION_FAILURE;
200
+ }
201
+ function encodeOutput(output, sessionId) {
202
+ switch (output.type) {
203
+ case 'text':
204
+ case 'error-text':
205
+ return {
206
+ version: DURABLE_TOOL_RESULT_PROJECTION_VERSION,
207
+ kind: 'text',
208
+ text: output.value,
209
+ ...(output.type === 'error-text' ? { isError: true } : {}),
210
+ };
211
+ case 'json':
212
+ case 'error-json':
213
+ return {
214
+ version: DURABLE_TOOL_RESULT_PROJECTION_VERSION,
215
+ kind: 'json',
216
+ value: sanitizeJson(output.value),
217
+ ...(output.type === 'error-json' ? { isError: true } : {}),
218
+ };
219
+ case 'execution-denied':
220
+ return {
221
+ version: DURABLE_TOOL_RESULT_PROJECTION_VERSION,
222
+ kind: 'execution_denied',
223
+ ...(output.reason !== undefined ? { reason: output.reason } : {}),
224
+ };
225
+ case 'content': {
226
+ return prepareContentProjection(output, sessionId).projection;
227
+ }
228
+ }
229
+ }
230
+ function prepareContentProjection(output, sessionId, planArtifact) {
231
+ if (output.value.length > DURABLE_TOOL_RESULT_PROJECTION_MAX_PARTS) {
232
+ throw new Error('Tool Result content exceeds the durable part limit');
233
+ }
234
+ const parts = [];
235
+ const artifacts = [];
236
+ for (const part of output.value) {
237
+ if (part.type === 'text') {
238
+ parts.push({ kind: 'text', text: part.text });
239
+ continue;
240
+ }
241
+ if (part.type !== 'file')
242
+ continue;
243
+ const ref = readSessionArtifactRef(part, sessionId);
244
+ if (ref) {
245
+ const mediaType = normalizeArtifactImagePreviewMime(part.mediaType);
246
+ if (!mediaType)
247
+ throw new Error('Artifact has an unsafe media type');
248
+ parts.push({ kind: 'artifact', mediaType, ref });
249
+ continue;
250
+ }
251
+ if (planArtifact &&
252
+ part.data.type === 'data' &&
253
+ part.mediaType.toLowerCase().startsWith('image/')) {
254
+ const mediaType = normalizeArtifactImagePreviewMime(part.mediaType);
255
+ if (!mediaType)
256
+ throw new Error('Inline image has an unsafe media type');
257
+ const artifact = planArtifact({
258
+ bytes: decodeBoundedImageData(part.data.data),
259
+ mediaType,
260
+ });
261
+ artifacts.push(artifact);
262
+ parts.push({ kind: 'artifact', mediaType, ref: artifact.ref });
263
+ continue;
264
+ }
265
+ parts.push({ kind: 'text', text: OMITTED_BINARY_TEXT });
266
+ }
267
+ if (parts.length === 0)
268
+ parts.push({ kind: 'text', text: 'Tool completed with no content.' });
269
+ return {
270
+ projection: decodeDurableToolResultProjection({
271
+ version: DURABLE_TOOL_RESULT_PROJECTION_VERSION,
272
+ kind: 'content',
273
+ parts,
274
+ }),
275
+ artifacts,
276
+ };
277
+ }
278
+ function readSessionArtifactRef(part, sessionId) {
279
+ if (part.type !== 'file')
280
+ return undefined;
281
+ const data = part.data;
282
+ if (!data || typeof data !== 'object' || Array.isArray(data))
283
+ return undefined;
284
+ const ref = data.ref;
285
+ return isCanonicalStorageRef(ref) &&
286
+ (ref.kind === 'session_context' || ref.kind === 'session_file') &&
287
+ ref.sessionId === sessionId
288
+ ? ref
289
+ : undefined;
290
+ }
291
+ function hasInlineImage(output) {
292
+ return output.value.some((part) => part.type === 'file' &&
293
+ part.data.type === 'data' &&
294
+ part.mediaType.toLowerCase().startsWith('image/'));
295
+ }
296
+ function decodeBoundedImageData(data) {
297
+ let bytes;
298
+ if (typeof data === 'string') {
299
+ if (data.length > Math.ceil((MAX_READ_IMAGE_BYTES * 4) / 3) + 4) {
300
+ throw new Error('Inline image exceeds the artifact byte limit');
301
+ }
302
+ const decoded = Buffer.from(data, 'base64');
303
+ if (decoded.toString('base64') !== data)
304
+ throw new Error('Inline image is not canonical base64');
305
+ bytes = decoded;
306
+ }
307
+ else if (data instanceof ArrayBuffer) {
308
+ if (data.byteLength > MAX_READ_IMAGE_BYTES) {
309
+ throw new Error('Inline image exceeds the artifact byte limit');
310
+ }
311
+ bytes = new Uint8Array(data.slice(0));
312
+ }
313
+ else if (ArrayBuffer.isView(data)) {
314
+ if (data.byteLength > MAX_READ_IMAGE_BYTES) {
315
+ throw new Error('Inline image exceeds the artifact byte limit');
316
+ }
317
+ bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength).slice();
318
+ }
319
+ else {
320
+ throw new Error('Inline image data is not representable');
321
+ }
322
+ if (bytes.byteLength > MAX_READ_IMAGE_BYTES) {
323
+ throw new Error('Inline image exceeds the artifact byte limit');
324
+ }
325
+ return bytes;
326
+ }
327
+ function sessionImageResult(result, sessionId) {
328
+ if (!result || typeof result !== 'object' || Array.isArray(result))
329
+ return undefined;
330
+ const image = result;
331
+ return image.kind === 'image' &&
332
+ typeof image.mimeType === 'string' &&
333
+ image.mimeType.length > 0 &&
334
+ isCanonicalStorageRef(image.ref) &&
335
+ (image.ref.kind === 'session_context' || image.ref.kind === 'session_file') &&
336
+ image.ref.sessionId === sessionId
337
+ ? { mimeType: image.mimeType, ref: image.ref }
338
+ : undefined;
339
+ }
340
+ function isLegacyPathImageResult(result, sessionId) {
341
+ const image = sessionImageResult(result, sessionId);
342
+ return image?.ref.kind === 'session_file' && !isCanonicalArtifactEntityId(image.ref.relativePath);
343
+ }
344
+ function sanitizeJson(value) {
345
+ const state = { nodes: 0 };
346
+ return sanitizeJsonValue(value, state, 0);
347
+ }
348
+ function sanitizeJsonValue(value, state, depth) {
349
+ state.nodes += 1;
350
+ if (state.nodes > DURABLE_TOOL_RESULT_PROJECTION_MAX_JSON_NODES ||
351
+ depth > DURABLE_TOOL_RESULT_PROJECTION_MAX_JSON_DEPTH) {
352
+ throw new Error('JSON exceeds limit');
353
+ }
354
+ if (value === null)
355
+ return null;
356
+ if (typeof value === 'string')
357
+ return value;
358
+ if (typeof value === 'boolean')
359
+ return value;
360
+ if (typeof value === 'number' && Number.isFinite(value))
361
+ return value;
362
+ if (Array.isArray(value)) {
363
+ return value.map((item) => sanitizeJsonValue(item, state, depth + 1));
364
+ }
365
+ if (!value || typeof value !== 'object')
366
+ throw new Error('JSON is not representable');
367
+ const prototype = Object.getPrototypeOf(value);
368
+ if (prototype !== Object.prototype && prototype !== null) {
369
+ throw new Error('JSON contains an opaque object');
370
+ }
371
+ if (typeof value.toJSON === 'function') {
372
+ throw new Error('JSON contains an opaque serializer');
373
+ }
374
+ const result = {};
375
+ for (const [key, item] of Object.entries(value)) {
376
+ Object.defineProperty(result, key, {
377
+ enumerable: true,
378
+ configurable: true,
379
+ writable: true,
380
+ value: sanitizeJsonValue(item, state, depth + 1),
381
+ });
382
+ }
383
+ return result;
384
+ }
385
+ function isRetiredExploreAgentResult(value) {
386
+ return (value !== null &&
387
+ typeof value === 'object' &&
388
+ !Array.isArray(value) &&
389
+ value.kind === 'explore_agent');
390
+ }
@@ -51,13 +51,8 @@
51
51
  * node re-exports it from the core barrel.
52
52
  */
53
53
  import { isPartialRuntimeEvent, isTerminalRuntimeEvent, runtimeEventHasModelVisibleContent, } from '@maka/core/runtime-event';
54
- import { withToolResultArchiveResourceRef } from './tool-result-archive.js';
55
54
  import { formatAttachmentResourceRef } from '@maka/core/attachments';
56
- import { decodeCanonicalShellToolResultContent } from '@maka/core/shell-run-result';
57
- import { markPersisted } from '@maka/core/persisted-value';
58
- import { decodePersistedToolResultContent } from '@maka/core/tool-result-record-schema';
59
- import { projectBashToolResultForModel } from './bash-model-output.js';
60
- import { projectFileWriteToolResultForModel } from './file-tool-model-output.js';
55
+ import { decodeEffectiveToolResultProjection } from './durable-tool-result-projection.js';
61
56
  export const PROVIDER_REPLAY_PROJECTION_VERSION = 1;
62
57
  // ============================================================================
63
58
  // Projection
@@ -328,35 +323,8 @@ export function buildRuntimeEventModelReplayPlan(events, options = {}) {
328
323
  }));
329
324
  continue;
330
325
  }
331
- const usesProviderOutput = event.content.providerExecuted && event.content.providerOutput !== undefined;
332
- let invalidResultMessage;
333
- let normalizedResult = usesProviderOutput
334
- ? event.content.providerOutput
335
- : withToolResultArchiveResourceRef(event.content.result);
336
- if (!usesProviderOutput && isRetiredExploreAgentResult(normalizedResult)) {
337
- try {
338
- normalizedResult = decodePersistedToolResultContent(markPersisted(normalizedResult));
339
- }
340
- catch {
341
- invalidResultMessage = 'function_response contains an invalid retired tool result';
342
- }
343
- }
344
- if (!invalidResultMessage) {
345
- const shellResult = decodeCanonicalShellToolResultContent(normalizedResult);
346
- if (shellResult.state === 'invalid') {
347
- invalidResultMessage = 'function_response contains an invalid shell tool result';
348
- }
349
- else if (shellResult.state === 'valid') {
350
- normalizedResult = shellResult.content;
351
- }
352
- }
353
- if (!invalidResultMessage && event.content.name === 'Bash') {
354
- normalizedResult = projectBashToolResultForModel(normalizedResult);
355
- }
356
- else if (!invalidResultMessage) {
357
- normalizedResult = projectFileWriteToolResultForModel(event.content.name, normalizedResult);
358
- }
359
- if (invalidResultMessage) {
326
+ const effective = decodeEffectiveToolResultProjection(event.content, event.sessionId);
327
+ if (effective.kind === 'invalid_legacy') {
360
328
  const call = callsById.get(event.content.id);
361
329
  if (call) {
362
330
  const callIndex = items.indexOf(call.item);
@@ -364,9 +332,12 @@ export function buildRuntimeEventModelReplayPlan(events, options = {}) {
364
332
  items.splice(callIndex, 1);
365
333
  callsById.delete(event.content.id);
366
334
  }
367
- diagnostics.push(diagnostic(event, 'unsupported_content', invalidResultMessage));
335
+ diagnostics.push(diagnostic(event, 'unsupported_content', effective.message));
368
336
  continue;
369
337
  }
338
+ const normalizedResult = effective.kind === 'provider_native' || effective.kind === 'legacy_output'
339
+ ? effective.output
340
+ : effective.legacyOutput;
370
341
  const call = callsById.get(event.content.id);
371
342
  if (!call) {
372
343
  diagnostics.push(diagnostic(event, 'unmatched_tool_result', 'function_response has no prior matching function_call', {
@@ -386,6 +357,11 @@ export function buildRuntimeEventModelReplayPlan(events, options = {}) {
386
357
  toolCallId: event.content.id,
387
358
  toolName: event.content.name,
388
359
  output: normalizedResult,
360
+ ...(effective.kind === 'projection'
361
+ ? {
362
+ modelProjection: effective.projection,
363
+ }
364
+ : {}),
389
365
  isError: event.content.isError === true,
390
366
  ...(event.content.providerExecuted !== undefined
391
367
  ? { providerExecuted: event.content.providerExecuted }
@@ -437,9 +413,6 @@ export function buildRuntimeEventModelReplayPlan(events, options = {}) {
437
413
  semanticKinds.includes('tool_result'),
438
414
  };
439
415
  }
440
- function isRetiredExploreAgentResult(value) {
441
- return (typeof value === 'object' && value !== null && 'kind' in value && value.kind === 'explore_agent');
442
- }
443
416
  /**
444
417
  * Convert projected RuntimeEvent history into the current AI SDK text-only
445
418
  * message shape. Tool/function and thinking entries are intentionally skipped.
@@ -19,6 +19,7 @@
19
19
  import { TOOL_BOUNDARY_PROTOCOL_V1, } from '@maka/core/runtime-event';
20
20
  import { scanToolLedger, } from '@maka/core/tool-ledger-scanner';
21
21
  import { interpretScannedToolRecovery } from '@maka/core/tool-recovery-bundle';
22
+ import { compatibilityToolResultProjection } from './durable-tool-result-projection.js';
22
23
  export function resolveRuntimeRecovery(events) {
23
24
  const firstProtocol = events[0]?.actions?.runtimeProtocol;
24
25
  const toolBoundaryProtocol = firstProtocol?.toolBoundary === TOOL_BOUNDARY_PROTOCOL_V1
@@ -66,6 +67,22 @@ export function buildInterruptedCodeModeOutcomeCommits(events, now, toolMode) {
66
67
  callEvent.modelVisibility === 'hidden') {
67
68
  return [];
68
69
  }
70
+ const result = {
71
+ kind: 'json',
72
+ value: {
73
+ kind: 'code_mode',
74
+ status: 'interrupted',
75
+ message: 'Code Mode execution was interrupted by runtime recovery.',
76
+ },
77
+ };
78
+ const responseContent = {
79
+ kind: 'function_response',
80
+ id: call.id,
81
+ name: call.name,
82
+ result,
83
+ isError: true,
84
+ };
85
+ const modelProjection = compatibilityToolResultProjection(responseContent, callEvent.sessionId);
69
86
  const runtimeEvent = {
70
87
  id: `${decision.operationId}_response`,
71
88
  invocationId: callEvent.invocationId,
@@ -78,20 +95,7 @@ export function buildInterruptedCodeModeOutcomeCommits(events, now, toolMode) {
78
95
  author: 'tool',
79
96
  origin: 'provider',
80
97
  modelVisibility: 'visible',
81
- content: {
82
- kind: 'function_response',
83
- id: call.id,
84
- name: call.name,
85
- result: {
86
- kind: 'json',
87
- value: {
88
- kind: 'code_mode',
89
- status: 'interrupted',
90
- message: 'Code Mode execution was interrupted by runtime recovery.',
91
- },
92
- },
93
- isError: true,
94
- },
98
+ content: { ...responseContent, ...(modelProjection ? { modelProjection } : {}) },
95
99
  refs: { operationId: decision.operationId, toolCallId: call.id },
96
100
  };
97
101
  return [
@@ -24,6 +24,7 @@
24
24
  * ledger.
25
25
  */
26
26
  import { failureClassFromCompleteStopReason, normalizeMessageContent, } from '@maka/core/events';
27
+ import { compatibilityToolResultProjection } from './durable-tool-result-projection.js';
27
28
  /**
28
29
  * Map a `CompleteEvent.stopReason` onto a terminal `RuntimeEventStatus`.
29
30
  *
@@ -267,6 +268,23 @@ function mapBackendSessionEvent(event, ctx, memory) {
267
268
  };
268
269
  case 'tool_result': {
269
270
  const name = memory.toolNameByUseId.get(event.toolUseId) ?? '';
271
+ const content = {
272
+ kind: 'function_response',
273
+ id: event.toolUseId,
274
+ name,
275
+ result: event.content,
276
+ ...(event.isError ? { isError: true } : {}),
277
+ ...(event.providerExecuted !== undefined
278
+ ? { providerExecuted: event.providerExecuted }
279
+ : {}),
280
+ ...(event.providerExecuted && event.providerOutput !== undefined
281
+ ? { providerOutput: structuredClone(event.providerOutput) }
282
+ : {}),
283
+ };
284
+ const modelProjection = event.modelProjection ??
285
+ (event.operationId === undefined
286
+ ? compatibilityToolResultProjection(content, ctx.sessionId)
287
+ : undefined);
270
288
  const ev = {
271
289
  ...base,
272
290
  role: 'tool',
@@ -274,17 +292,8 @@ function mapBackendSessionEvent(event, ctx, memory) {
274
292
  ...(event.origin !== undefined ? { origin: event.origin } : {}),
275
293
  ...(event.modelVisibility !== undefined ? { modelVisibility: event.modelVisibility } : {}),
276
294
  content: {
277
- kind: 'function_response',
278
- id: event.toolUseId,
279
- name,
280
- result: event.content,
281
- ...(event.isError ? { isError: true } : {}),
282
- ...(event.providerExecuted !== undefined
283
- ? { providerExecuted: event.providerExecuted }
284
- : {}),
285
- ...(event.providerExecuted && event.providerOutput !== undefined
286
- ? { providerOutput: structuredClone(event.providerOutput) }
287
- : {}),
295
+ ...content,
296
+ ...(modelProjection ? { modelProjection } : {}),
288
297
  },
289
298
  refs: {
290
299
  toolCallId: event.toolUseId,