maka-agent 0.2.0-dev.30.20260912 → 0.2.0-dev.32.20260914

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 (245) hide show
  1. package/THIRD_PARTY_NOTICES.txt +392 -0
  2. package/dist/acp/maka-acp-agent.js +8 -2
  3. package/dist/acp/prompt-content.js +225 -0
  4. package/dist/acp/session-event-mapper.js +103 -0
  5. package/dist/acp/session-registry.js +654 -17
  6. package/dist/acp/stdio-server.js +10 -1
  7. package/dist/cli-core.js +1 -1
  8. package/dist/pi-transcript-tools.js +3 -2
  9. package/dist/pi-transcript.js +15 -6
  10. package/dist/runtime-host-session-channel.js +63 -9
  11. package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  12. package/node_modules/@maka/core/dist/agent-graph-schedule.js +15 -4
  13. package/node_modules/@maka/core/dist/artifacts.js +6 -0
  14. package/node_modules/@maka/core/dist/attachments.js +7 -2
  15. package/node_modules/@maka/core/dist/durable-tool-result-projection.js +3 -1
  16. package/node_modules/@maka/core/dist/events.js +21 -0
  17. package/node_modules/@maka/core/dist/executor-id.js +22 -0
  18. package/node_modules/@maka/core/dist/external-session.js +18 -0
  19. package/node_modules/@maka/core/dist/goal.js +44 -2
  20. package/node_modules/@maka/core/dist/model-call-usage-projection.js +0 -13
  21. package/node_modules/@maka/core/dist/permission.js +0 -1
  22. package/node_modules/@maka/core/dist/runtime-boundary.js +73 -29
  23. package/node_modules/@maka/core/dist/runtime-event.js +25 -6
  24. package/node_modules/@maka/core/dist/runtime-handoff.js +10 -2
  25. package/node_modules/@maka/core/dist/runtime-logical-execution.js +39 -14
  26. package/node_modules/@maka/core/dist/session-send-projection.js +6 -3
  27. package/node_modules/@maka/core/dist/session.js +8 -2
  28. package/node_modules/@maka/core/dist/shell-run-result.js +1 -0
  29. package/node_modules/@maka/core/dist/shell-run.js +4 -0
  30. package/node_modules/@maka/core/dist/tool-result-archive-evidence.js +3 -1
  31. package/node_modules/@maka/core/dist/tool-result-record-schema.js +2 -1
  32. package/node_modules/@maka/core/dist/unified-diff.js +33 -26
  33. package/node_modules/@maka/core/dist/work-board.js +94 -1
  34. package/node_modules/@maka/core/package.json +1 -0
  35. package/node_modules/@maka/eval/dist/fleet-simulation.js +270 -0
  36. package/node_modules/@maka/eval/dist/fleet-store.js +243 -0
  37. package/node_modules/@maka/eval/dist/fleet-worker.js +125 -0
  38. package/node_modules/@maka/eval/dist/fleet.js +329 -0
  39. package/node_modules/@maka/eval/dist/index.js +3 -0
  40. package/node_modules/@maka/runtime/dist/agent-run.js +2 -17
  41. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +55 -180
  42. package/node_modules/@maka/runtime/dist/ai-sdk-message-projection.js +43 -26
  43. package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +271 -382
  44. package/node_modules/@maka/runtime/dist/background-task-health-tool.js +104 -0
  45. package/node_modules/@maka/runtime/dist/bash-model-output.js +7 -1
  46. package/node_modules/@maka/runtime/dist/bash-tail-buffer.js +13 -52
  47. package/node_modules/@maka/runtime/dist/builtin-tools.js +59 -111
  48. package/node_modules/@maka/runtime/dist/child-fd-input.js +3 -2
  49. package/node_modules/@maka/runtime/dist/context-budget-policy.js +1 -11
  50. package/node_modules/@maka/runtime/dist/context-budget.js +12 -1
  51. package/node_modules/@maka/runtime/dist/conversation-copy.js +104 -9
  52. package/node_modules/@maka/runtime/dist/filesystem-executor.js +10 -6
  53. package/node_modules/@maka/runtime/dist/filesystem-worker/client.js +77 -19
  54. package/node_modules/@maka/runtime/dist/filesystem-worker/launch-spec.js +66 -55
  55. package/node_modules/@maka/runtime/dist/filesystem-worker/operations.js +34 -97
  56. package/node_modules/@maka/runtime/dist/filesystem-worker/protocol.js +25 -10
  57. package/node_modules/@maka/runtime/dist/filesystem-worker/worker-entry.js +2 -0
  58. package/node_modules/@maka/runtime/dist/glob-search.js +74 -0
  59. package/node_modules/@maka/runtime/dist/goal-continuation.js +78 -8
  60. package/node_modules/@maka/runtime/dist/goal-state.js +7 -0
  61. package/node_modules/@maka/runtime/dist/grep-search.js +155 -0
  62. package/node_modules/@maka/runtime/dist/history-compaction.js +3 -1
  63. package/node_modules/@maka/runtime/dist/ledger-tool-result-archive-reader.js +84 -94
  64. package/node_modules/@maka/runtime/dist/local-web-fetch.js +1 -1
  65. package/node_modules/@maka/runtime/dist/model-adapter.js +62 -53
  66. package/node_modules/@maka/runtime/dist/model-history.js +26 -8
  67. package/node_modules/@maka/runtime/dist/pipe-tail-collector.js +6 -9
  68. package/node_modules/@maka/runtime/dist/plugin-executor-backend.js +311 -0
  69. package/node_modules/@maka/runtime/dist/plugin-executor-service.js +344 -0
  70. package/node_modules/@maka/runtime/dist/provider-error-classification.js +7 -1
  71. package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +35 -8
  72. package/node_modules/@maka/runtime/dist/read-page.js +231 -0
  73. package/node_modules/@maka/runtime/dist/request-projection.js +2 -15
  74. package/node_modules/@maka/runtime/dist/ripgrep-executable.js +62 -0
  75. package/node_modules/@maka/runtime/dist/ripgrep-guidance.js +81 -0
  76. package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +7 -1
  77. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +188 -20
  78. package/node_modules/@maka/runtime/dist/runtime-invocation-route.js +49 -0
  79. package/node_modules/@maka/runtime/dist/runtime-kernel.js +72 -150
  80. package/node_modules/@maka/runtime/dist/runtime-read-model.js +2 -3
  81. package/node_modules/@maka/runtime/dist/sandbox/linux-profile-path.js +7 -1
  82. package/node_modules/@maka/runtime/dist/sandbox/linux-sandbox.js +20 -5
  83. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +3 -0
  84. package/node_modules/@maka/runtime/dist/session-manager.js +106 -64
  85. package/node_modules/@maka/runtime/dist/session-recap.js +9 -5
  86. package/node_modules/@maka/runtime/dist/shell-exec.js +7 -25
  87. package/node_modules/@maka/runtime/dist/shell-run-manager.js +10 -4
  88. package/node_modules/@maka/runtime/dist/shell-run-tool-result.js +2 -1
  89. package/node_modules/@maka/runtime/dist/shell-tools.js +6 -12
  90. package/node_modules/@maka/runtime/dist/stream-graph-schedule-reconcile.js +1 -0
  91. package/node_modules/@maka/runtime/dist/stream-graph-supervisor-tools.js +26 -4
  92. package/node_modules/@maka/runtime/dist/subagent-tools.js +7 -0
  93. package/node_modules/@maka/runtime/dist/tool-availability.js +0 -1
  94. package/node_modules/@maka/runtime/dist/tool-output.js +1 -1
  95. package/node_modules/@maka/runtime/dist/tool-result-archive-capability.js +24 -36
  96. package/node_modules/@maka/runtime/dist/tool-result-archive-resource.js +32 -527
  97. package/node_modules/@maka/runtime/dist/tool-result-archive-transition.js +17 -119
  98. package/node_modules/@maka/runtime/dist/tool-result-archive.js +7 -25
  99. package/node_modules/@maka/runtime/dist/tool-runtime.js +52 -471
  100. package/node_modules/@maka/runtime/dist/workers/filesystem-worker.js +6933 -3681
  101. package/node_modules/@maka/runtime/dist/workspace-executor.js +20 -24
  102. package/node_modules/@maka/runtime/package.json +5 -0
  103. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +7 -2
  104. package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +185 -191
  105. package/node_modules/@maka/runtime-host/dist/client/reconnecting-connection.js +3 -0
  106. package/node_modules/@maka/runtime-host/dist/client/session-catalog-summary.js +1 -0
  107. package/node_modules/@maka/runtime-host/dist/protocol/external-session.js +24 -3
  108. package/node_modules/@maka/runtime-host/dist/protocol/index.js +8 -1
  109. package/node_modules/@maka/runtime-host/dist/protocol/message.js +2 -2
  110. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
  111. package/node_modules/@maka/runtime-host/dist/protocol/plugin-platform.js +42 -5
  112. package/node_modules/@maka/runtime-host/dist/protocol/session-catalog.js +31 -3
  113. package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +8 -2
  114. package/node_modules/@maka/runtime-host/dist/protocol/turn.js +10 -2
  115. package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +38 -0
  116. package/node_modules/@maka/runtime-host/dist/server/child-agent-composition.js +1 -0
  117. package/node_modules/@maka/runtime-host/dist/server/execution-artifacts.js +3 -30
  118. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +56 -8
  119. package/node_modules/@maka/runtime-host/dist/server/external-session-coordinator.js +14 -1
  120. package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +81 -7
  121. package/node_modules/@maka/runtime-host/dist/server/host-session-availability.js +10 -2
  122. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-tool-profile.js +13 -13
  123. package/node_modules/@maka/runtime-host/dist/server/interaction-coordinator.js +65 -0
  124. package/node_modules/@maka/runtime-host/dist/server/plugin-platform-coordinator.js +6 -0
  125. package/node_modules/@maka/runtime-host/dist/server/plugin-platform.js +6 -0
  126. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +1 -0
  127. package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +56 -16
  128. package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +9 -5
  129. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +4 -0
  130. package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +88 -72
  131. package/node_modules/@maka/runtime-host/dist/server/web-fetch-tool.js +37 -1
  132. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +21 -4
  133. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +145 -1
  134. package/node_modules/@maka/storage/dist/agent-run-store.js +5 -2
  135. package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +450 -234
  136. package/node_modules/@maka/storage/dist/claude-code-transcript-lineage.js +106 -77
  137. package/node_modules/@maka/storage/dist/execution-stores.js +2 -1
  138. package/node_modules/@maka/storage/dist/legacy-run-header.js +2 -2
  139. package/node_modules/@maka/storage/dist/model-call-usage-sql.js +1 -1
  140. package/node_modules/@maka/storage/dist/runtime-transcript-query.js +27 -14
  141. package/node_modules/@maka/storage/dist/session-store.js +12 -2
  142. package/node_modules/@maka/storage/dist/sqlite-long-term-memory-store.js +86 -12
  143. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +69 -3
  144. package/node_modules/@maka/storage/dist/work-board-store.js +40 -1
  145. package/node_modules/balanced-match/LICENSE.md +23 -0
  146. package/node_modules/balanced-match/README.md +57 -0
  147. package/node_modules/balanced-match/dist/commonjs/index.js +59 -0
  148. package/node_modules/balanced-match/dist/commonjs/package.json +3 -0
  149. package/node_modules/balanced-match/dist/esm/index.js +54 -0
  150. package/node_modules/balanced-match/dist/esm/package.json +3 -0
  151. package/node_modules/balanced-match/package.json +68 -0
  152. package/node_modules/brace-expansion/LICENSE +23 -0
  153. package/node_modules/brace-expansion/README.md +105 -0
  154. package/node_modules/brace-expansion/dist/commonjs/index.js +289 -0
  155. package/node_modules/brace-expansion/dist/commonjs/package.json +3 -0
  156. package/node_modules/brace-expansion/dist/esm/index.js +285 -0
  157. package/node_modules/brace-expansion/dist/esm/package.json +3 -0
  158. package/node_modules/brace-expansion/package.json +64 -0
  159. package/node_modules/glob/LICENSE.md +63 -0
  160. package/node_modules/glob/README.md +1203 -0
  161. package/node_modules/glob/dist/commonjs/glob.js +248 -0
  162. package/node_modules/glob/dist/commonjs/has-magic.js +27 -0
  163. package/node_modules/glob/dist/commonjs/ignore.js +119 -0
  164. package/node_modules/glob/dist/commonjs/index.js +68 -0
  165. package/node_modules/glob/dist/commonjs/index.min.js +4 -0
  166. package/node_modules/glob/dist/commonjs/package.json +3 -0
  167. package/node_modules/glob/dist/commonjs/pattern.js +223 -0
  168. package/node_modules/glob/dist/commonjs/processor.js +301 -0
  169. package/node_modules/glob/dist/commonjs/walker.js +387 -0
  170. package/node_modules/glob/dist/esm/glob.js +244 -0
  171. package/node_modules/glob/dist/esm/has-magic.js +23 -0
  172. package/node_modules/glob/dist/esm/ignore.js +115 -0
  173. package/node_modules/glob/dist/esm/index.js +55 -0
  174. package/node_modules/glob/dist/esm/index.min.js +4 -0
  175. package/node_modules/glob/dist/esm/package.json +3 -0
  176. package/node_modules/glob/dist/esm/pattern.js +219 -0
  177. package/node_modules/glob/dist/esm/processor.js +294 -0
  178. package/node_modules/glob/dist/esm/walker.js +381 -0
  179. package/node_modules/glob/package.json +98 -0
  180. package/node_modules/minimatch/LICENSE.md +55 -0
  181. package/node_modules/minimatch/README.md +528 -0
  182. package/node_modules/minimatch/dist/commonjs/assert-valid-pattern.js +14 -0
  183. package/node_modules/minimatch/dist/commonjs/ast.js +845 -0
  184. package/node_modules/minimatch/dist/commonjs/brace-expressions.js +150 -0
  185. package/node_modules/minimatch/dist/commonjs/escape.js +30 -0
  186. package/node_modules/minimatch/dist/commonjs/index.js +1127 -0
  187. package/node_modules/minimatch/dist/commonjs/package.json +3 -0
  188. package/node_modules/minimatch/dist/commonjs/unescape.js +38 -0
  189. package/node_modules/minimatch/dist/esm/assert-valid-pattern.js +10 -0
  190. package/node_modules/minimatch/dist/esm/ast.js +841 -0
  191. package/node_modules/minimatch/dist/esm/brace-expressions.js +146 -0
  192. package/node_modules/minimatch/dist/esm/escape.js +26 -0
  193. package/node_modules/minimatch/dist/esm/index.js +1114 -0
  194. package/node_modules/minimatch/dist/esm/package.json +3 -0
  195. package/node_modules/minimatch/dist/esm/unescape.js +34 -0
  196. package/node_modules/minimatch/package.json +73 -0
  197. package/node_modules/minipass/LICENSE.md +55 -0
  198. package/node_modules/minipass/README.md +825 -0
  199. package/node_modules/minipass/dist/commonjs/index.js +1038 -0
  200. package/node_modules/minipass/dist/commonjs/package.json +3 -0
  201. package/node_modules/minipass/dist/esm/index.js +1020 -0
  202. package/node_modules/minipass/dist/esm/package.json +3 -0
  203. package/node_modules/minipass/package.json +77 -0
  204. package/node_modules/path-scurry/LICENSE.md +55 -0
  205. package/node_modules/path-scurry/README.md +636 -0
  206. package/node_modules/path-scurry/dist/commonjs/index.js +2018 -0
  207. package/node_modules/path-scurry/dist/commonjs/package.json +3 -0
  208. package/node_modules/path-scurry/dist/esm/index.js +1983 -0
  209. package/node_modules/path-scurry/dist/esm/package.json +3 -0
  210. package/node_modules/path-scurry/node_modules/lru-cache/LICENSE.md +55 -0
  211. package/node_modules/path-scurry/node_modules/lru-cache/README.md +469 -0
  212. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/browser/diagnostics-channel.js +7 -0
  213. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/browser/index.js +1726 -0
  214. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/browser/index.min.js +2 -0
  215. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/browser/perf.js +10 -0
  216. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/diagnostics-channel-cjs.d.cts.map +1 -0
  217. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/diagnostics-channel.js +7 -0
  218. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/index.js +1726 -0
  219. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/index.min.js +2 -0
  220. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/node/diagnostics-channel.js +9 -0
  221. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/node/index.js +1726 -0
  222. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/node/index.min.js +2 -0
  223. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/node/perf.js +10 -0
  224. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/package.json +3 -0
  225. package/node_modules/path-scurry/node_modules/lru-cache/dist/commonjs/perf.js +10 -0
  226. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/browser/diagnostics-channel.js +4 -0
  227. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/browser/index.js +1722 -0
  228. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/browser/index.min.js +2 -0
  229. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/browser/perf.js +7 -0
  230. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/diagnostics-channel-esm.d.mts.map +1 -0
  231. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/diagnostics-channel.js +19 -0
  232. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/index.js +1722 -0
  233. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/index.min.js +2 -0
  234. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/node/diagnostics-channel.js +6 -0
  235. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/node/index.js +1722 -0
  236. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/node/index.min.js +2 -0
  237. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/node/perf.js +7 -0
  238. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/package.json +3 -0
  239. package/node_modules/path-scurry/node_modules/lru-cache/dist/esm/perf.js +7 -0
  240. package/node_modules/path-scurry/node_modules/lru-cache/package.json +154 -0
  241. package/node_modules/path-scurry/package.json +72 -0
  242. package/package.json +1 -1
  243. package/node_modules/@maka/runtime/dist/active-tool-result-prune.js +0 -248
  244. package/node_modules/@maka/runtime/dist/active-tool-result-working-set.js +0 -218
  245. package/node_modules/@maka/runtime/dist/archive-read-tool.js +0 -115
@@ -25,6 +25,7 @@ import { isPartialRuntimeEvent, isTerminalRuntimeEvent, isTerminalRuntimeEventSt
25
25
  import { decodePersistedToolResultContent } from '@maka/core/tool-result-record-schema';
26
26
  const SETTLED_SANDBOX_BOUNDARY_STATUSES = SANDBOX_BOUNDARY_REQUEST_STATUSES.filter((status) => status !== 'pending');
27
27
  import { isArchivedToolResultPlaceholder } from './tool-result-archive.js';
28
+ import { truncateToolOutput } from './tool-output.js';
28
29
  /**
29
30
  * Whether a diagnostic means the projection may have lost user-visible content.
30
31
  *
@@ -94,7 +95,54 @@ const UNCLAIMED_RUNTIME_EVENT_DIAGNOSTIC_CODES = [
94
95
  export function isUnclaimedRuntimeEventDiagnostic(diagnostic) {
95
96
  return UNCLAIMED_RUNTIME_EVENT_DIAGNOSTIC_CODES.includes(diagnostic.code);
96
97
  }
98
+ /**
99
+ * Keep a completed local terminal result useful but bounded in transcript views.
100
+ * The durable RuntimeEvent remains untouched and the marker names its retained
101
+ * result, so readers can fetch the full output without repeating the command.
102
+ */
103
+ export function projectTranscriptToolResult(event, content) {
104
+ if (content.kind !== 'terminal' ||
105
+ content.output.mode !== 'pipes' ||
106
+ !hasLocalTerminalModelProjection(event)) {
107
+ return content;
108
+ }
109
+ const recoveryHint = `Read ${JSON.stringify({
110
+ path: `maka://runtime/tool-results/${encodeURIComponent(event.id)}`,
111
+ })} for the retained output; follow next to continue.`;
112
+ const options = { maxBytes: 1024, maxLines: 20, direction: 'tail', recoveryHint };
113
+ const stdout = truncateToolOutput(content.output.stdout, options);
114
+ const stderr = truncateToolOutput(content.output.stderr, options);
115
+ if (!stdout.truncated && !stderr.truncated)
116
+ return content;
117
+ return {
118
+ ...content,
119
+ output: {
120
+ ...content.output,
121
+ stdout: stdout.content,
122
+ stderr: stderr.content,
123
+ stdoutTruncated: content.output.stdoutTruncated || stdout.truncated,
124
+ stderrTruncated: content.output.stderrTruncated || stderr.truncated,
125
+ },
126
+ };
127
+ }
128
+ function hasLocalTerminalModelProjection(event) {
129
+ const content = event.content;
130
+ if (content?.kind !== 'function_response' || content.providerExecuted)
131
+ return false;
132
+ const projection = content.modelProjection;
133
+ return (projection?.kind === 'json' &&
134
+ projection.value !== null &&
135
+ typeof projection.value === 'object' &&
136
+ 'kind' in projection.value &&
137
+ projection.value.kind === 'terminal');
138
+ }
97
139
  export function projectRuntimeEventsToStoredMessages(events, options) {
140
+ const projector = createRuntimeEventStoredMessageProjector(options);
141
+ for (const event of events)
142
+ projector.push(event);
143
+ return projector.finish();
144
+ }
145
+ export function createRuntimeEventStoredMessageProjector(options) {
98
146
  const state = {
99
147
  invocations: normalizeInvocations(options.invocations),
100
148
  diagnostics: [],
@@ -102,6 +150,8 @@ export function projectRuntimeEventsToStoredMessages(events, options) {
102
150
  permissionRequestById: new Map(),
103
151
  thinkingByMessageId: new Map(),
104
152
  contentOrderByMessageId: new Map(),
153
+ ...(options.projectToolResult ? { projectToolResult: options.projectToolResult } : {}),
154
+ sourceOrder: -1,
105
155
  };
106
156
  const messages = [];
107
157
  /**
@@ -112,19 +162,48 @@ export function projectRuntimeEventsToStoredMessages(events, options) {
112
162
  * handled are exactly that event's rows. A durable reader numbers its pages
113
163
  * from this, which is why it is recorded here rather than rediscovered.
114
164
  */
115
- const sourceEventIds = [];
116
- let reading;
117
- const attributeEmitted = () => {
118
- while (sourceEventIds.length < messages.length)
119
- sourceEventIds.push(reading.id);
165
+ const messageSources = [];
166
+ const diagnosticSources = [];
167
+ const deferredPermissionAcceptances = [];
168
+ const permissionRequestIds = new Set();
169
+ let nextSourceOrder = 0;
170
+ let finished;
171
+ const attributeEmitted = (event, sourceOrder, firstSourcePosition) => {
172
+ let sourcePosition = firstSourcePosition;
173
+ while (messageSources.length < messages.length) {
174
+ const message = messages[messageSources.length];
175
+ messageSources.push({
176
+ eventId: event.id,
177
+ sourceOrder,
178
+ sourcePosition,
179
+ emittedOrder: messageSources.length,
180
+ });
181
+ sourcePosition += 1;
182
+ options.onMessage?.(message, event.id);
183
+ }
184
+ return sourcePosition;
120
185
  };
121
- for (const event of events) {
122
- attributeEmitted();
123
- reading = event;
186
+ const attributeDiagnostics = (sourceOrder, firstSourcePosition) => {
187
+ let sourcePosition = firstSourcePosition;
188
+ while (diagnosticSources.length < state.diagnostics.length) {
189
+ diagnosticSources.push({
190
+ sourceOrder,
191
+ sourcePosition,
192
+ emittedOrder: diagnosticSources.length,
193
+ });
194
+ sourcePosition += 1;
195
+ }
196
+ return sourcePosition;
197
+ };
198
+ const projectEvent = (event, sourceOrder) => {
199
+ let nextMessagePosition = 0;
200
+ let nextDiagnosticPosition = 0;
201
+ state.sourceOrder = sourceOrder;
124
202
  recordStepContentOrder(event, state);
125
203
  if (isPartialRuntimeEvent(event)) {
126
204
  diagnostic(state, event, 'partial_skipped', 'partial RuntimeEvent skipped');
127
- continue;
205
+ attributeDiagnostics(sourceOrder, nextDiagnosticPosition);
206
+ return;
128
207
  }
129
208
  let projected = false;
130
209
  const content = event.content;
@@ -197,7 +276,17 @@ export function projectRuntimeEventsToStoredMessages(events, options) {
197
276
  projected = true;
198
277
  }
199
278
  if (event.actions?.permissionAnswerAccepted) {
200
- projectCanonicalPermissionOutcome(event, state, messages, options.canonicalPermissionOutcomes);
279
+ nextMessagePosition = attributeEmitted(event, sourceOrder, nextMessagePosition);
280
+ nextDiagnosticPosition = attributeDiagnostics(sourceOrder, nextDiagnosticPosition);
281
+ const requestId = event.actions.permissionAnswerAccepted.requestId;
282
+ permissionRequestIds.add(requestId);
283
+ deferredPermissionAcceptances.push({
284
+ event: permissionAcceptanceProjectionEvent(event),
285
+ ledgerRequest: state.permissionRequestById.get(requestId),
286
+ sourceOrder,
287
+ messagePosition: nextMessagePosition++,
288
+ diagnosticPosition: nextDiagnosticPosition++,
289
+ });
201
290
  projected = true;
202
291
  }
203
292
  if (event.actions?.permissionClosureAccepted) {
@@ -284,14 +373,74 @@ export function projectRuntimeEventsToStoredMessages(events, options) {
284
373
  diagnostic(state, event, 'unsupported_event', 'RuntimeEvent shape is not supported by the legacy read-model projection');
285
374
  }
286
375
  }
287
- }
288
- for (const pendingItems of state.thinkingByMessageId.values()) {
289
- for (const pending of pendingItems) {
290
- diagnostic(state, pending.event, 'unsupported_event', 'thinking content has no assistant text row with a matching message id');
376
+ attributeEmitted(event, sourceOrder, nextMessagePosition);
377
+ attributeDiagnostics(sourceOrder, nextDiagnosticPosition);
378
+ };
379
+ const push = (event) => {
380
+ if (finished)
381
+ throw new Error('RuntimeEvent StoredMessage projector is already finished');
382
+ const sourceOrder = nextSourceOrder++;
383
+ projectEvent(options.active ? settledPresentationEvent(event) : event, sourceOrder);
384
+ };
385
+ const finish = () => {
386
+ if (finished)
387
+ return finished;
388
+ for (const acceptance of deferredPermissionAcceptances) {
389
+ const before = messages.length;
390
+ projectCanonicalPermissionOutcome(acceptance.event, state, messages, options.canonicalPermissionOutcomes, acceptance.ledgerRequest);
391
+ if (messages.length > before) {
392
+ attributeEmitted(acceptance.event, acceptance.sourceOrder, acceptance.messagePosition);
393
+ }
394
+ attributeDiagnostics(acceptance.sourceOrder, acceptance.diagnosticPosition);
291
395
  }
292
- }
293
- attributeEmitted();
294
- return { messages, diagnostics: state.diagnostics, sourceEventIds };
396
+ if (options.active) {
397
+ for (const pendingItems of [...state.thinkingByMessageId.values()]) {
398
+ const pending = pendingItems.at(-1);
399
+ if (pending)
400
+ projectEvent(emptyAssistantText(pending.event), pending.sourceOrder);
401
+ }
402
+ }
403
+ const orderedDiagnostics = state.diagnostics
404
+ .map((diagnostic, index) => ({ diagnostic, source: diagnosticSources[index] }))
405
+ .sort((left, right) => left.source.sourceOrder - right.source.sourceOrder ||
406
+ left.source.sourcePosition - right.source.sourcePosition ||
407
+ left.source.emittedOrder - right.source.emittedOrder);
408
+ state.diagnostics.splice(0, state.diagnostics.length, ...orderedDiagnostics.map(({ diagnostic }) => diagnostic));
409
+ if (!options.active) {
410
+ for (const pendingItems of state.thinkingByMessageId.values()) {
411
+ for (const pending of pendingItems) {
412
+ diagnostic(state, pending.event, 'unsupported_event', 'thinking content has no assistant text row with a matching message id');
413
+ }
414
+ }
415
+ }
416
+ const ordered = messages
417
+ .map((message, index) => ({ message, source: messageSources[index] }))
418
+ .sort((left, right) => left.source.sourceOrder - right.source.sourceOrder ||
419
+ left.source.sourcePosition - right.source.sourcePosition ||
420
+ left.source.emittedOrder - right.source.emittedOrder);
421
+ finished = {
422
+ messages: ordered.map(({ message }) => message),
423
+ diagnostics: state.diagnostics,
424
+ sourceEventIds: ordered.map(({ source }) => source.eventId),
425
+ };
426
+ deferredPermissionAcceptances.length = 0;
427
+ messageSources.length = 0;
428
+ diagnosticSources.length = 0;
429
+ state.invocations.clear();
430
+ state.toolNameByUseId.clear();
431
+ state.permissionRequestById.clear();
432
+ state.thinkingByMessageId.clear();
433
+ state.contentOrderByMessageId.clear();
434
+ state.projectToolResult = undefined;
435
+ return finished;
436
+ };
437
+ return {
438
+ push,
439
+ finish,
440
+ get permissionRequestIds() {
441
+ return [...permissionRequestIds];
442
+ },
443
+ };
295
444
  }
296
445
  /**
297
446
  * A running invocation's events as the transcript should show them right now.
@@ -489,6 +638,7 @@ function projectText(event, state, messages) {
489
638
  turnId: event.turnId,
490
639
  ts: event.ts,
491
640
  text: event.content.text,
641
+ ...(event.content.interrupted ? { interrupted: true } : {}),
492
642
  ...(event.content.providerOptions !== undefined
493
643
  ? { providerOptions: structuredClone(event.content.providerOptions) }
494
644
  : {}),
@@ -564,6 +714,7 @@ function projectThinking(event, state, messages) {
564
714
  event,
565
715
  messageId,
566
716
  text: event.content.text,
717
+ sourceOrder: state.sourceOrder,
567
718
  ...(event.content.signature !== undefined ? { signature: event.content.signature } : {}),
568
719
  ...(event.content.providerOptions !== undefined
569
720
  ? { providerOptions: structuredClone(event.content.providerOptions) }
@@ -669,7 +820,7 @@ function projectFunctionResponse(event, state, messages) {
669
820
  }
670
821
  if (event.content.name)
671
822
  state.toolNameByUseId.set(toolUseId, event.content.name);
672
- const resultContent = archivedPlaceholder
823
+ const decodedResult = archivedPlaceholder
673
824
  ? {
674
825
  kind: 'archived_tool_result',
675
826
  status: 'not_loaded',
@@ -687,6 +838,9 @@ function projectFunctionResponse(event, state, messages) {
687
838
  reason: archivedPlaceholder.reason,
688
839
  }
689
840
  : normalizedResult;
841
+ const resultContent = state.projectToolResult
842
+ ? state.projectToolResult(event, decodedResult)
843
+ : decodedResult;
690
844
  messages.push({
691
845
  type: 'tool_result',
692
846
  id: stableMessageId(event, state, 'tool_result'),
@@ -766,11 +920,10 @@ function projectPermissionDecision(event, state, messages) {
766
920
  });
767
921
  return true;
768
922
  }
769
- function projectCanonicalPermissionOutcome(event, state, messages, outcomes) {
923
+ function projectCanonicalPermissionOutcome(event, state, messages, outcomes, ledgerRequest) {
770
924
  const accepted = event.actions?.permissionAnswerAccepted;
771
925
  if (!accepted)
772
926
  return;
773
- const ledgerRequest = state.permissionRequestById.get(accepted.requestId);
774
927
  const canonical = outcomes?.get(accepted.requestId);
775
928
  const toolUseId = event.refs?.toolCallId;
776
929
  if (!canonical || !toolUseId) {
@@ -807,6 +960,21 @@ function projectCanonicalPermissionOutcome(event, state, messages, outcomes) {
807
960
  ...(ledgerRequest?.hint !== undefined ? { hint: ledgerRequest.hint } : {}),
808
961
  });
809
962
  }
963
+ function permissionAcceptanceProjectionEvent(event) {
964
+ return {
965
+ id: event.id,
966
+ sessionId: event.sessionId,
967
+ invocationId: event.invocationId,
968
+ runId: event.runId,
969
+ turnId: event.turnId,
970
+ ts: event.ts,
971
+ partial: event.partial,
972
+ role: event.role,
973
+ author: event.author,
974
+ actions: { permissionAnswerAccepted: event.actions.permissionAnswerAccepted },
975
+ ...(event.refs?.toolCallId ? { refs: { toolCallId: event.refs.toolCallId } } : {}),
976
+ };
977
+ }
810
978
  function projectTokenUsage(event, state, messages) {
811
979
  const usage = event.actions?.tokenUsage;
812
980
  if (!usage)
@@ -0,0 +1,49 @@
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 { isExecutorId } from '@maka/core/executor-id';
20
+ export function runtimeInvocationRouteForHeader(header, providerStateIdentity) {
21
+ if (header.backend === 'plugin-executor' &&
22
+ isExecutorId(header.executorId) &&
23
+ providerStateIdentity) {
24
+ return {
25
+ provenance: 'runtime',
26
+ backendKind: header.backend,
27
+ executorId: header.executorId,
28
+ llmConnectionSlug: header.llmConnectionSlug,
29
+ modelId: header.model,
30
+ providerStateIdentity,
31
+ };
32
+ }
33
+ if (header.backend !== 'plugin-executor' && header.llmConnectionId !== undefined) {
34
+ return {
35
+ provenance: 'runtime',
36
+ backendKind: header.backend,
37
+ llmConnectionId: header.llmConnectionId,
38
+ llmConnectionSlug: header.llmConnectionSlug,
39
+ modelId: header.model,
40
+ ...(providerStateIdentity ? { providerStateIdentity } : {}),
41
+ };
42
+ }
43
+ return {
44
+ provenance: 'unknown',
45
+ backendKind: header.backend,
46
+ llmConnectionSlug: header.llmConnectionSlug,
47
+ modelId: header.model,
48
+ };
49
+ }