maka-agent 0.2.0-dev.22.20260905 → 0.2.0-dev.24.20260907

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 (194) hide show
  1. package/dist/acp/maka-acp-agent.js +2 -1
  2. package/dist/acp/session-configuration.js +143 -0
  3. package/dist/acp/session-registry.js +90 -4
  4. package/dist/cli-core.js +2 -0
  5. package/dist/pi-transcript.js +5 -12
  6. package/dist/pi-tui-runner.js +74 -4
  7. package/dist/runtime-host-cli-context.js +167 -78
  8. package/dist/runtime-host-cli-installation.js +8 -2
  9. package/dist/runtime-host-cli.js +31 -1
  10. package/dist/runtime-host-handoff-surface.js +81 -0
  11. package/dist/runtime-host-installed-update-bootstrap.js +13 -0
  12. package/dist/runtime-host-installed-update-coordinator.js +33 -0
  13. package/dist/runtime-host-lifecycle-transaction.js +9 -1
  14. package/dist/runtime-host-local-handoff.js +37 -4
  15. package/dist/runtime-host-local-managed-activation.js +84 -0
  16. package/dist/runtime-host-local-source-retirement.js +18 -2
  17. package/dist/runtime-host-managed-lifecycle-manager.js +1 -0
  18. package/dist/runtime-host-operator-retirement.js +50 -0
  19. package/dist/runtime-host-service-manager.js +3 -0
  20. package/dist/runtime-host-session-driver.js +4 -25
  21. package/dist/runtime-host-session-update.js +62 -0
  22. package/dist/runtime-host-tui-command.js +38 -73
  23. package/dist/runtime-host-tui-context.js +1 -0
  24. package/dist/runtime-host-tui-owner.js +226 -0
  25. package/dist/tui-copy-catalog.js +140 -3
  26. package/dist/tui-host-owner-copy.js +24 -0
  27. package/native/runtime-host-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
  28. package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
  29. package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
  30. package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -0
  31. package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  32. package/node_modules/@maka/core/dist/bot-events.js +18 -63
  33. package/node_modules/@maka/core/dist/canonical-runtime-event.js +3 -1
  34. package/node_modules/@maka/core/dist/model-call-attempt.js +0 -82
  35. package/node_modules/@maka/core/dist/model-call-usage-projection.js +0 -186
  36. package/node_modules/@maka/core/dist/model-failure.js +40 -0
  37. package/node_modules/@maka/core/dist/model-metadata.generated.js +4 -4
  38. package/node_modules/@maka/core/dist/model-metadata.js +3 -3
  39. package/node_modules/@maka/core/dist/model-web-search.js +5 -4
  40. package/node_modules/@maka/core/dist/provider-registry.js +45 -4
  41. package/node_modules/@maka/core/dist/record-schema.js +22 -2
  42. package/node_modules/@maka/core/dist/redaction.js +41 -58
  43. package/node_modules/@maka/core/dist/runtime-boundary.js +31 -11
  44. package/node_modules/@maka/core/dist/runtime-event.js +37 -6
  45. package/node_modules/@maka/core/dist/runtime-handoff.js +98 -0
  46. package/node_modules/@maka/core/dist/runtime-invocation.js +1 -1
  47. package/node_modules/@maka/core/dist/runtime-logical-execution.js +112 -0
  48. package/node_modules/@maka/core/dist/session-trace.js +5 -1
  49. package/node_modules/@maka/core/dist/session.js +54 -52
  50. package/node_modules/@maka/core/dist/slash-command-catalog.js +2 -0
  51. package/node_modules/@maka/core/dist/tool-result-archive-evidence.js +20 -0
  52. package/node_modules/@maka/core/dist/usage-ledger-merge.js +7 -9
  53. package/node_modules/@maka/core/dist/usage-record-schema.js +26 -35
  54. package/node_modules/@maka/core/dist/workhub-creation-intent.js +34 -20
  55. package/node_modules/@maka/core/package.json +4 -0
  56. package/node_modules/@maka/eval/harbor/egress-proxy/entrypoint.sh +2 -1
  57. package/node_modules/@maka/mcp/dist/index.js +131 -1
  58. package/node_modules/@maka/runtime/dist/agent-run.js +272 -105
  59. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +13 -5
  60. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +109 -34
  61. package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +155 -134
  62. package/node_modules/@maka/runtime/dist/bots/base-adapter.js +17 -0
  63. package/node_modules/@maka/runtime/dist/bots/bot-test.js +38 -22
  64. package/node_modules/@maka/runtime/dist/bots/dingtalk-bridge.js +7 -5
  65. package/node_modules/@maka/runtime/dist/bots/discord-bridge.js +4 -4
  66. package/node_modules/@maka/runtime/dist/bots/feishu-bridge.js +4 -5
  67. package/node_modules/@maka/runtime/dist/bots/qq-bridge.js +6 -4
  68. package/node_modules/@maka/runtime/dist/bots/slack-bridge.js +3 -4
  69. package/node_modules/@maka/runtime/dist/bots/telegram-bridge.js +4 -5
  70. package/node_modules/@maka/runtime/dist/bots/wechat-bridge.js +17 -19
  71. package/node_modules/@maka/runtime/dist/bots/wecom-bridge.js +5 -6
  72. package/node_modules/@maka/runtime/dist/bots/ws-bridge-base.js +2 -3
  73. package/node_modules/@maka/runtime/dist/context-budget.js +14 -9
  74. package/node_modules/@maka/runtime/dist/conversation-copy.js +5 -4
  75. package/node_modules/@maka/runtime/dist/goal-continuation.js +36 -4
  76. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +18 -9
  77. package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +30 -22
  78. package/node_modules/@maka/runtime/dist/history-compaction.js +29 -4
  79. package/node_modules/@maka/runtime/dist/ledger-tool-result-archive-reader.js +106 -0
  80. package/node_modules/@maka/runtime/dist/message-authority.js +9 -0
  81. package/node_modules/@maka/runtime/dist/model-adapter.js +19 -98
  82. package/node_modules/@maka/runtime/dist/model-factory.js +19 -81
  83. package/node_modules/@maka/runtime/dist/model-fetcher.js +7 -0
  84. package/node_modules/@maka/runtime/dist/model-runtime.js +85 -111
  85. package/node_modules/@maka/runtime/dist/native-web-search-tool.js +2 -1
  86. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +2 -2
  87. package/node_modules/@maka/runtime/dist/opencode-session-header.js +4 -2
  88. package/node_modules/@maka/runtime/dist/provider-error-classification.js +84 -123
  89. package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +68 -108
  90. package/node_modules/@maka/runtime/dist/run-handoff-gate.js +97 -0
  91. package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +82 -54
  92. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +117 -159
  93. package/node_modules/@maka/runtime/dist/runtime-kernel.js +150 -112
  94. package/node_modules/@maka/runtime/dist/runtime-ledger-repair.js +160 -79
  95. package/node_modules/@maka/runtime/dist/runtime-read-model.js +30 -89
  96. package/node_modules/@maka/runtime/dist/runtime-resume.js +80 -4
  97. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +5 -0
  98. package/node_modules/@maka/runtime/dist/session-manager.js +171 -288
  99. package/node_modules/@maka/runtime/dist/session-projection-helpers.js +0 -27
  100. package/node_modules/@maka/runtime/dist/session-trace-projection.js +3 -30
  101. package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +3 -4
  102. package/node_modules/@maka/runtime/dist/test-connection.js +16 -16
  103. package/node_modules/@maka/runtime/dist/tool-result-archive-encoding.js +38 -0
  104. package/node_modules/@maka/runtime/dist/tool-runtime.js +3 -61
  105. package/node_modules/@maka/runtime/package.json +1 -0
  106. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +35 -0
  107. package/node_modules/@maka/runtime-host/dist/candidate-entry.js +4 -1
  108. package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +9 -1
  109. package/node_modules/@maka/runtime-host/dist/client/connection.js +3 -0
  110. package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +156 -0
  111. package/node_modules/@maka/runtime-host/dist/client/host-handoff.js +273 -0
  112. package/node_modules/@maka/runtime-host/dist/client/host-retirement.js +26 -5
  113. package/node_modules/@maka/runtime-host/dist/client/index.js +3 -1
  114. package/node_modules/@maka/runtime-host/dist/client/launcher.js +3 -0
  115. package/node_modules/@maka/runtime-host/dist/client/peer-client.js +13 -0
  116. package/node_modules/@maka/runtime-host/dist/client/registered-host-termination.js +0 -59
  117. package/node_modules/@maka/runtime-host/dist/client/remote-compatibility-error.js +2 -0
  118. package/node_modules/@maka/runtime-host/dist/client/session-subscription.js +27 -5
  119. package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
  120. package/node_modules/@maka/runtime-host/dist/operator/retirement-cancellation.js +20 -0
  121. package/node_modules/@maka/runtime-host/dist/peer-mesh/node.js +28 -16
  122. package/node_modules/@maka/runtime-host/dist/protocol/external-session.js +4 -0
  123. package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +29 -12
  124. package/node_modules/@maka/runtime-host/dist/protocol/index.js +37 -1
  125. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +2 -0
  126. package/node_modules/@maka/runtime-host/dist/protocol/session-collaboration.js +28 -1
  127. package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +40 -20
  128. package/node_modules/@maka/runtime-host/dist/protocol/session-transcript.js +0 -5
  129. package/node_modules/@maka/runtime-host/dist/protocol/session-turns.js +29 -58
  130. package/node_modules/@maka/runtime-host/dist/protocol/turn.js +11 -1
  131. package/node_modules/@maka/runtime-host/dist/protocol/workhub-coordination.js +33 -0
  132. package/node_modules/@maka/runtime-host/dist/server/access-authority.js +26 -1
  133. package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +6 -1
  134. package/node_modules/@maka/runtime-host/dist/server/canonical-turn-snapshot.js +19 -4
  135. package/node_modules/@maka/runtime-host/dist/server/canonical-usage-reader.js +24 -11
  136. package/node_modules/@maka/runtime-host/dist/server/connection-session.js +7 -6
  137. package/node_modules/@maka/runtime-host/dist/server/daily-review-coordinator.js +7 -4
  138. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +168 -28
  139. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +5 -5
  140. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +3 -2
  141. package/node_modules/@maka/runtime-host/dist/server/external-session-coordinator.js +6 -3
  142. package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +40 -1
  143. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +108 -23
  144. package/node_modules/@maka/runtime-host/dist/server/host-residency-registry.js +13 -7
  145. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-admission-registry.js +3 -1
  146. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +124 -62
  147. package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +85 -8
  148. package/node_modules/@maka/runtime-host/dist/server/root-admission-owner.js +7 -0
  149. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +344 -71
  150. package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +37 -2
  151. package/node_modules/@maka/runtime-host/dist/server/serial-outbound-writer.js +45 -3
  152. package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +201 -12
  153. package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +87 -25
  154. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +8 -55
  155. package/node_modules/@maka/runtime-host/dist/server/session-sidecar-purge.js +1 -16
  156. package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +0 -1
  157. package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +460 -70
  158. package/node_modules/@maka/runtime-host/dist/server/shared-session-transcript.js +1 -1
  159. package/node_modules/@maka/runtime-host/dist/server/storage-maintenance.js +109 -0
  160. package/node_modules/@maka/runtime-host/dist/server/usage-pricing-coordinator.js +4 -11
  161. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +44 -15
  162. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +6 -2
  163. package/node_modules/@maka/storage/dist/agent-run-store.js +44 -0
  164. package/node_modules/@maka/storage/dist/artifact-store.js +21 -10
  165. package/node_modules/@maka/storage/dist/artifact-stores.js +1 -1
  166. package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +0 -4
  167. package/node_modules/@maka/storage/dist/codex-session-adapter.js +0 -2
  168. package/node_modules/@maka/storage/dist/context-offload-snapshot.js +325 -0
  169. package/node_modules/@maka/storage/dist/conversation-operational-state.js +4 -0
  170. package/node_modules/@maka/storage/dist/execution-stores.js +11 -7
  171. package/node_modules/@maka/storage/dist/file-session-repository.js +1012 -0
  172. package/node_modules/@maka/storage/dist/model-call-ledger.js +199 -32
  173. package/node_modules/@maka/storage/dist/model-call-usage-sql.js +136 -0
  174. package/node_modules/@maka/storage/dist/opencode-session-adapter.js +0 -4
  175. package/node_modules/@maka/storage/dist/operational-state-backup.js +9 -5
  176. package/node_modules/@maka/storage/dist/operational-state-store-public.js +1 -1
  177. package/node_modules/@maka/storage/dist/operational-state-store.js +5 -3
  178. package/node_modules/@maka/storage/dist/runtime-event-persistence.js +1 -0
  179. package/node_modules/@maka/storage/dist/runtime-transcript-query.js +308 -0
  180. package/node_modules/@maka/storage/dist/session-bundle-policy.js +24 -4
  181. package/node_modules/@maka/storage/dist/session-message-projection.js +15 -0
  182. package/node_modules/@maka/storage/dist/session-repository.js +767 -0
  183. package/node_modules/@maka/storage/dist/session-store.js +25 -99
  184. package/node_modules/@maka/storage/dist/sqlite-artifact-metadata.js +10 -3
  185. package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +24 -8
  186. package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +17 -1
  187. package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +44 -1
  188. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +129 -25
  189. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +109 -892
  190. package/node_modules/@maka/storage/dist/sqlite-usage-schema.js +200 -20
  191. package/node_modules/@maka/storage/dist/tool-result-archive-evidence.js +111 -0
  192. package/node_modules/@maka/storage/dist/usage-stores.js +6 -2
  193. package/node_modules/@maka/storage/package.json +2 -0
  194. package/package.json +1 -1
@@ -0,0 +1,308 @@
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 { decodeRuntimeEvent } from '@maka/core/runtime-event';
20
+ /**
21
+ * SQL counterpart of isTerminalRuntimeEvent; shared with the ledger store.
22
+ *
23
+ * The `json_valid` guard is what keeps this usable as a partial index: SQLite
24
+ * evaluates the index predicate over every row while building it, and
25
+ * `json_extract` on a malformed payload fails the whole statement. That would
26
+ * abort the migration that creates the index, roll back its version bump, and
27
+ * leave the next open to try — and fail — again.
28
+ */
29
+ export const TERMINAL_RUNTIME_EVENT_SQL = `(
30
+ json_valid(payload_json)
31
+ AND (
32
+ json_extract(payload_json, '$.actions.endInvocation') = 1
33
+ OR json_extract(payload_json, '$.status') IN ('completed', 'failed', 'aborted', 'cancelled')
34
+ )
35
+ )`;
36
+ export class RuntimeTranscriptOversizedTurnError extends Error {
37
+ name = 'RuntimeTranscriptOversizedTurnError';
38
+ }
39
+ /**
40
+ * A Turn the Session transcript shows: one this Session ran itself rather than
41
+ * on behalf of a subagent.
42
+ *
43
+ * This is a fact about the invocation, not about any row it produces — which
44
+ * rows it produces is the read model's question, and is not asked here.
45
+ */
46
+ const visibleOpening = (payload) => `
47
+ (${payload} IS NOT NULL
48
+ AND (json_extract(${payload}, '$.lineage.parentRunId') IS NULL
49
+ OR (json_extract(${payload}, '$.source.kind') <> 'fresh'
50
+ AND json_extract(${payload}, '$.lineage.agentId') IS NULL)))`;
51
+ /** Where an invocation ends; NULL while it is still running. */
52
+ const endingOrdinal = (invocation) => `
53
+ (SELECT MIN(o2.ordinal) FROM runtime_events t
54
+ JOIN runtime_session_event_ordinals o2 ON o2.event_id = t.event_id
55
+ WHERE t.invocation_id = ${invocation}
56
+ AND ${TERMINAL_RUNTIME_EVENT_SQL.replaceAll('payload_json', 't.payload_json')})`;
57
+ /**
58
+ * A Session migrated from run headers keeps some openings beside the ledger
59
+ * rather than in it, ordered by the anchor event each one names.
60
+ */
61
+ const migratedOpening = `
62
+ FROM runtime_legacy_invocation_openings legacy
63
+ JOIN runtime_session_event_ordinals o ON o.event_id = legacy.anchor_event_id
64
+ WHERE legacy.session_id = :sessionId
65
+ AND ${visibleOpening('legacy.opening_json')}
66
+ AND NOT EXISTS (
67
+ SELECT 1 FROM runtime_events opened
68
+ WHERE opened.invocation_id = legacy.invocation_id
69
+ AND opened.event_kind = 'invocation_opened'
70
+ )`;
71
+ const ledgerOpening = `
72
+ FROM runtime_session_event_ordinals o
73
+ JOIN runtime_events e ON e.event_id = o.event_id
74
+ WHERE o.session_id = :sessionId
75
+ AND e.event_kind = 'invocation_opened'
76
+ AND ${visibleOpening("json_extract(e.payload_json, '$.content')")}`;
77
+ /** Either shelf's opening for one invocation, reached from an event it owns. */
78
+ const openingOrdinal = (invocation) => `
79
+ COALESCE(
80
+ (SELECT o3.ordinal FROM runtime_events op
81
+ JOIN runtime_session_event_ordinals o3 ON o3.event_id = op.event_id
82
+ WHERE op.invocation_id = ${invocation} AND op.event_kind = 'invocation_opened'),
83
+ (SELECT o3.ordinal FROM runtime_legacy_invocation_openings lg
84
+ JOIN runtime_session_event_ordinals o3 ON o3.event_id = lg.anchor_event_id
85
+ WHERE lg.invocation_id = ${invocation}))`;
86
+ const openingContent = (invocation) => `
87
+ COALESCE(
88
+ (SELECT json_extract(op.payload_json, '$.content') FROM runtime_events op
89
+ WHERE op.invocation_id = ${invocation} AND op.event_kind = 'invocation_opened'),
90
+ (SELECT lg.opening_json FROM runtime_legacy_invocation_openings lg
91
+ WHERE lg.invocation_id = ${invocation}))`;
92
+ /** Selects invocations by Session ordinal. Payloads are decoded, never classified. */
93
+ export class RuntimeTranscriptQuery {
94
+ db;
95
+ invocation;
96
+ constructor(db, invocation) {
97
+ this.db = db;
98
+ this.invocation = invocation;
99
+ }
100
+ highWater(sessionId) {
101
+ // The furthest a transcript reaches is the last ending on it.
102
+ const [row] = this.byEnding(sessionId, {
103
+ order: 'DESC',
104
+ from: 0,
105
+ throughOrdinal: Number.MAX_SAFE_INTEGER,
106
+ limit: 1,
107
+ });
108
+ return row?.last ?? null;
109
+ }
110
+ invocations(sessionId, request) {
111
+ assertOrdinal(request.throughOrdinal);
112
+ assertOrdinal(request.position);
113
+ if (request.direction !== 'older' && request.direction !== 'newer') {
114
+ throw new Error('Invalid transcript direction');
115
+ }
116
+ // An invocation is selected by where its own events sit, so a walk that
117
+ // starts inside a Turn still finds that Turn and can serve its rows. Both
118
+ // ends are the invocation's own two events — its opening and its ending —
119
+ // rather than the extremes of everything between them.
120
+ //
121
+ // Each direction walks the end of the Turn that `position` bounds, which
122
+ // is the one the ordinal index can seek to: backward that is the opening,
123
+ // forward the ending. Neither assumes Turns do not overlap, and each stops
124
+ // at the page, so a page costs the page rather than the Session.
125
+ const rows = request.direction === 'older'
126
+ ? this.byOpening(sessionId, request)
127
+ : this.byEnding(sessionId, {
128
+ order: 'ASC',
129
+ from: request.position,
130
+ throughOrdinal: request.throughOrdinal,
131
+ limit: request.limit,
132
+ }).sort((a, b) => a.first - b.first);
133
+ return rows.map((row) => ({
134
+ invocation: this.invocation(sessionId, row.invocation_id),
135
+ firstOrdinal: row.first,
136
+ lastOrdinal: row.last,
137
+ events: this.events(row.invocation_id, request),
138
+ }));
139
+ }
140
+ landmarks(sessionId, throughOrdinal, limit) {
141
+ assertOrdinal(throughOrdinal);
142
+ if (limit < 1)
143
+ return [];
144
+ // Evenly spaced Turn starts, chosen before any payload is read.
145
+ const rows = this.db
146
+ .prepare(`
147
+ WITH settled AS (
148
+ SELECT e.invocation_id AS invocation_id, o.ordinal AS ordinal ${ledgerOpening}
149
+ AND ${endingOrdinal('e.invocation_id')} <= :throughOrdinal
150
+ UNION ALL
151
+ SELECT legacy.invocation_id, o.ordinal ${migratedOpening}
152
+ AND ${endingOrdinal('legacy.invocation_id')} <= :throughOrdinal
153
+ ), candidates AS (
154
+ SELECT invocation_id, ordinal,
155
+ ROW_NUMBER() OVER (ORDER BY ordinal) - 1 AS rank, COUNT(*) OVER () AS total
156
+ FROM settled
157
+ ), samples(n) AS (
158
+ SELECT 0 UNION ALL SELECT n + 1 FROM samples WHERE n + 1 < :limit
159
+ )
160
+ SELECT DISTINCT invocation_id, ordinal FROM candidates
161
+ JOIN samples ON rank = CASE WHEN :limit = 1 THEN total - 1
162
+ ELSE CAST(n * (total - 1) / (:limit - 1) AS INTEGER) END
163
+ ORDER BY ordinal
164
+ `)
165
+ .all({ sessionId, throughOrdinal, limit });
166
+ return rows.map((row) => {
167
+ // The prompt is the Turn's first user text event, which is what the read
168
+ // model projects a user message from. Only that one event is loaded: a
169
+ // landmark is a label, and projecting whole Turns to build a scrollbar
170
+ // would read most of the Session.
171
+ const prompt = this.db
172
+ .prepare(`
173
+ SELECT o.ordinal, e.event_id FROM runtime_events e
174
+ JOIN runtime_session_event_ordinals o ON o.event_id = e.event_id
175
+ WHERE e.invocation_id = ? AND o.ordinal <= ?
176
+ AND e.event_kind = 'text' AND json_extract(e.payload_json, '$.role') = 'user'
177
+ ORDER BY e.event_seq LIMIT 1
178
+ `)
179
+ .get(row.invocation_id, throughOrdinal);
180
+ return {
181
+ invocation: this.invocation(sessionId, row.invocation_id),
182
+ firstOrdinal: row.ordinal,
183
+ ...(prompt
184
+ ? { prompt: { ordinal: prompt.ordinal, event: this.event(prompt.event_id) } }
185
+ : {}),
186
+ };
187
+ });
188
+ }
189
+ /**
190
+ * The page of settled visible invocations that opened at or before
191
+ * `position`, newest first.
192
+ *
193
+ * The two shelves are read as separate statements and merged rather than
194
+ * unioned, so each keeps its own index walk and stops at the page — and the
195
+ * common Session pays nothing for a table its history never wrote to.
196
+ */
197
+ byOpening(sessionId, request) {
198
+ const bind = {
199
+ sessionId,
200
+ position: request.position,
201
+ throughOrdinal: request.throughOrdinal,
202
+ limit: request.limit,
203
+ };
204
+ const ledger = this.db
205
+ .prepare(`
206
+ SELECT e.invocation_id AS invocation_id, o.ordinal AS first,
207
+ ${endingOrdinal('e.invocation_id')} AS last
208
+ ${ledgerOpening}
209
+ AND o.ordinal <= :position
210
+ AND ${endingOrdinal('e.invocation_id')} <= :throughOrdinal
211
+ ORDER BY o.ordinal DESC
212
+ LIMIT :limit
213
+ `)
214
+ .all(bind);
215
+ const migrated = this.db
216
+ .prepare(`
217
+ SELECT legacy.invocation_id AS invocation_id, o.ordinal AS first,
218
+ ${endingOrdinal('legacy.invocation_id')} AS last
219
+ ${migratedOpening}
220
+ AND o.ordinal <= :position
221
+ AND ${endingOrdinal('legacy.invocation_id')} <= :throughOrdinal
222
+ ORDER BY o.ordinal DESC
223
+ LIMIT :limit
224
+ `)
225
+ .all(bind);
226
+ if (migrated.length === 0)
227
+ return ledger;
228
+ return [...ledger, ...migrated].sort((a, b) => b.first - a.first).slice(0, request.limit);
229
+ }
230
+ /**
231
+ * The page of settled visible invocations whose ending sits between `from`
232
+ * and `throughOrdinal`, in `order` of that ending.
233
+ *
234
+ * An ending is an event of the invocation like any other, so this walks the
235
+ * same ordinal index — one statement, because the ending is on the ledger
236
+ * whichever shelf the opening came from.
237
+ */
238
+ byEnding(sessionId, bounds) {
239
+ return this.db
240
+ .prepare(`
241
+ SELECT ending.invocation_id AS invocation_id,
242
+ ${openingOrdinal('ending.invocation_id')} AS first,
243
+ o.ordinal AS last
244
+ FROM runtime_session_event_ordinals o
245
+ JOIN runtime_events ending ON ending.event_id = o.event_id
246
+ WHERE o.session_id = :sessionId
247
+ AND o.ordinal BETWEEN :from AND :throughOrdinal
248
+ AND ${TERMINAL_RUNTIME_EVENT_SQL.replaceAll('payload_json', 'ending.payload_json')}
249
+ AND o.ordinal = ${endingOrdinal('ending.invocation_id')}
250
+ AND ${visibleOpening(openingContent('ending.invocation_id'))}
251
+ ORDER BY o.ordinal ${bounds.order}
252
+ LIMIT :limit
253
+ `)
254
+ .all({
255
+ sessionId,
256
+ from: bounds.from,
257
+ throughOrdinal: bounds.throughOrdinal,
258
+ limit: bounds.limit,
259
+ });
260
+ }
261
+ events(invocationId, limits) {
262
+ // Walked row by row: the limits cap what one Turn may pull into memory, so
263
+ // a check after `.all()` has already paid the cost it was meant to refuse.
264
+ const cursor = this.db
265
+ .prepare(`
266
+ SELECT o.ordinal, e.event_id, e.session_id, e.invocation_id, e.run_id, e.turn_id, e.payload_json
267
+ FROM runtime_events e JOIN runtime_session_event_ordinals o ON o.event_id = e.event_id
268
+ WHERE e.invocation_id = ? ORDER BY e.event_seq
269
+ `)
270
+ .iterate(invocationId);
271
+ const events = [];
272
+ let bytes = 0;
273
+ for (const row of cursor) {
274
+ if (events.length === limits.maxEvents) {
275
+ throw new RuntimeTranscriptOversizedTurnError(`Turn ${invocationId} holds more RuntimeEvents than a transcript page may read`);
276
+ }
277
+ bytes += Buffer.byteLength(row.payload_json);
278
+ if (bytes > limits.maxBytes) {
279
+ throw new RuntimeTranscriptOversizedTurnError(`Turn ${invocationId} holds more RuntimeEvent bytes than a transcript page may read`);
280
+ }
281
+ events.push({ ordinal: row.ordinal, event: decodeStoredEvent(row) });
282
+ }
283
+ return events;
284
+ }
285
+ event(id) {
286
+ const row = this.db
287
+ .prepare('SELECT event_id, session_id, invocation_id, run_id, turn_id, payload_json FROM runtime_events WHERE event_id = ?')
288
+ .get(id);
289
+ if (!row)
290
+ throw new Error(`Transcript RuntimeEvent ${id} is missing`);
291
+ return decodeStoredEvent(row);
292
+ }
293
+ }
294
+ function decodeStoredEvent(row) {
295
+ const event = decodeRuntimeEvent(JSON.parse(row.payload_json));
296
+ if (event.id !== row.event_id ||
297
+ event.sessionId !== row.session_id ||
298
+ event.invocationId !== row.invocation_id ||
299
+ event.runId !== row.run_id ||
300
+ event.turnId !== row.turn_id) {
301
+ throw new Error(`Transcript RuntimeEvent ${row.event_id} has inconsistent storage identity`);
302
+ }
303
+ return event;
304
+ }
305
+ function assertOrdinal(value) {
306
+ if (!Number.isSafeInteger(value) || value < 0)
307
+ throw new Error('Invalid transcript event ordinal');
308
+ }
@@ -22,8 +22,15 @@ import { dirname, isAbsolute, relative, resolve, sep } from 'node:path';
22
22
  import { DatabaseSync } from 'node:sqlite';
23
23
  import { decodeArtifactRecordJsons } from './artifact-metadata-codec.js';
24
24
  import { withArtifactWriterLock } from './artifact-writer-lock.js';
25
+ import { withOfflineContextSnapshot, copyContextSnapshot, validateContextSnapshot, planContextSnapshotFiles, } from './context-offload-snapshot.js';
26
+ import { CONTEXT_OFFLOAD_DATABASE_NAME, CONTEXT_OFFLOAD_VALUES_DIRECTORY_NAME, } from './sqlite-context-offload-store.js';
25
27
  import { acquireOperationalStateDatabase, OPERATIONAL_STATE_DATABASE_NAME, } from './operational-state-store.js';
26
- export const SESSION_BUNDLE_STATE_ENTRIES = ['artifacts', OPERATIONAL_STATE_DATABASE_NAME];
28
+ export const SESSION_BUNDLE_STATE_ENTRIES = [
29
+ 'artifacts',
30
+ OPERATIONAL_STATE_DATABASE_NAME,
31
+ CONTEXT_OFFLOAD_DATABASE_NAME,
32
+ CONTEXT_OFFLOAD_VALUES_DIRECTORY_NAME,
33
+ ];
27
34
  export const SESSION_BUNDLE_PROTECTED_ENTRIES = [];
28
35
  export class SessionBundleExportError extends Error {
29
36
  code;
@@ -85,6 +92,14 @@ export async function planSessionBundleExport(input) {
85
92
  }
86
93
  includedEntries.push('artifacts');
87
94
  }
95
+ const contextFiles = await planContextSnapshotFiles(stateRoot, input.sessionId);
96
+ for (const relativePath of contextFiles) {
97
+ entries.push({ relativePath, kind: 'file', source: 'context_snapshot' });
98
+ }
99
+ if (contextFiles.length > 0)
100
+ includedEntries.push(CONTEXT_OFFLOAD_DATABASE_NAME);
101
+ if (contextFiles.length > 1)
102
+ includedEntries.push(CONTEXT_OFFLOAD_VALUES_DIRECTORY_NAME);
88
103
  const allowed = new Set([...SESSION_BUNDLE_STATE_ENTRIES]);
89
104
  const excludedEntries = (await readdir(stateRoot)).filter((entry) => !allowed.has(entry)).sort();
90
105
  return {
@@ -98,13 +113,15 @@ export async function planSessionBundleExport(input) {
98
113
  };
99
114
  }
100
115
  export async function exportSessionBundleState(input) {
101
- return withArtifactWriterLock(input.stateRoot, async (stateRoot) => {
116
+ return withOfflineContextSnapshot(input.stateRoot, (contextLocked) => withArtifactWriterLock(input.stateRoot, async (stateRoot) => {
102
117
  const plan = await planSessionBundleExport({ ...input, stateRoot });
103
118
  await assertDestinationMissing(plan.destinationRoot);
104
119
  const stagingRoot = `${plan.destinationRoot}.${process.pid}.${randomUUID()}.tmp`;
105
120
  try {
106
- await mkdir(stagingRoot, { recursive: true });
121
+ await mkdir(stagingRoot, { recursive: true, mode: 0o700 });
107
122
  for (const entry of plan.entries) {
123
+ if (entry.source === 'context_snapshot')
124
+ continue;
108
125
  const destination = resolveInside(stagingRoot, entry.relativePath);
109
126
  if (entry.kind === 'directory') {
110
127
  await mkdir(destination, { recursive: true });
@@ -118,6 +135,8 @@ export async function exportSessionBundleState(input) {
118
135
  await exportFilteredDatabase(plan.stateRoot, destination, plan.sessionId);
119
136
  }
120
137
  }
138
+ await copyContextSnapshot(stateRoot, stagingRoot, contextLocked, plan.sessionId);
139
+ await validateContextSnapshot(stagingRoot);
121
140
  await mkdir(dirname(plan.destinationRoot), { recursive: true });
122
141
  await rename(stagingRoot, plan.destinationRoot);
123
142
  return plan;
@@ -126,7 +145,7 @@ export async function exportSessionBundleState(input) {
126
145
  await rm(stagingRoot, { recursive: true, force: true }).catch(() => { });
127
146
  throw error;
128
147
  }
129
- });
148
+ }));
130
149
  }
131
150
  async function exportFilteredDatabase(stateRoot, destinationPath, sessionId) {
132
151
  const lease = acquireOperationalStateDatabase(stateRoot);
@@ -209,6 +228,7 @@ async function exportFilteredDatabase(stateRoot, destinationPath, sessionId) {
209
228
  .get(sessionId);
210
229
  if (!session)
211
230
  throw new Error(`Filtered session is missing: ${sessionId}`);
231
+ database.exec('PRAGMA journal_mode = DELETE');
212
232
  }
213
233
  catch (error) {
214
234
  try {
@@ -74,3 +74,18 @@ function truncatePreview(text, maxLength = 96) {
74
74
  return text;
75
75
  return `${chars.slice(0, maxLength - 1).join('')}…`;
76
76
  }
77
+ /** One Turn's summary, folded message by message in transcript order. */
78
+ export function foldTurnContribution(current, turnId, sequence, message) {
79
+ const contribution = current ?? {
80
+ turnId,
81
+ firstSequence: sequence,
82
+ latestState: null,
83
+ userPromptPreview: null,
84
+ };
85
+ const userPrompt = message.type === 'user' ? (message.displayText ?? message.text).trim() : '';
86
+ return {
87
+ ...contribution,
88
+ latestState: message.type === 'turn_state' ? { sequence, message } : contribution.latestState,
89
+ userPromptPreview: contribution.userPromptPreview ?? (userPrompt || null),
90
+ };
91
+ }