maka-agent 0.2.0-dev.23.20260906 → 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 (148) 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-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
  28. package/node_modules/@maka/core/dist/canonical-runtime-event.js +3 -1
  29. package/node_modules/@maka/core/dist/model-failure.js +40 -0
  30. package/node_modules/@maka/core/dist/model-metadata.generated.js +4 -4
  31. package/node_modules/@maka/core/dist/model-metadata.js +3 -3
  32. package/node_modules/@maka/core/dist/model-web-search.js +5 -4
  33. package/node_modules/@maka/core/dist/provider-registry.js +45 -4
  34. package/node_modules/@maka/core/dist/record-schema.js +9 -2
  35. package/node_modules/@maka/core/dist/runtime-boundary.js +31 -11
  36. package/node_modules/@maka/core/dist/runtime-event.js +37 -6
  37. package/node_modules/@maka/core/dist/runtime-handoff.js +98 -0
  38. package/node_modules/@maka/core/dist/runtime-invocation.js +1 -1
  39. package/node_modules/@maka/core/dist/runtime-logical-execution.js +112 -0
  40. package/node_modules/@maka/core/dist/session.js +54 -52
  41. package/node_modules/@maka/core/dist/slash-command-catalog.js +2 -0
  42. package/node_modules/@maka/core/dist/tool-result-archive-evidence.js +20 -0
  43. package/node_modules/@maka/core/dist/usage-record-schema.js +26 -35
  44. package/node_modules/@maka/core/package.json +4 -0
  45. package/node_modules/@maka/mcp/dist/index.js +131 -1
  46. package/node_modules/@maka/runtime/dist/agent-run.js +272 -105
  47. package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +13 -5
  48. package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +109 -34
  49. package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +127 -133
  50. package/node_modules/@maka/runtime/dist/context-budget.js +14 -9
  51. package/node_modules/@maka/runtime/dist/conversation-copy.js +5 -4
  52. package/node_modules/@maka/runtime/dist/goal-continuation.js +36 -4
  53. package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +18 -9
  54. package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +30 -22
  55. package/node_modules/@maka/runtime/dist/history-compaction.js +29 -4
  56. package/node_modules/@maka/runtime/dist/ledger-tool-result-archive-reader.js +106 -0
  57. package/node_modules/@maka/runtime/dist/message-authority.js +9 -0
  58. package/node_modules/@maka/runtime/dist/model-adapter.js +19 -98
  59. package/node_modules/@maka/runtime/dist/model-factory.js +19 -81
  60. package/node_modules/@maka/runtime/dist/model-fetcher.js +7 -0
  61. package/node_modules/@maka/runtime/dist/model-runtime.js +85 -111
  62. package/node_modules/@maka/runtime/dist/native-web-search-tool.js +2 -1
  63. package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +2 -2
  64. package/node_modules/@maka/runtime/dist/opencode-session-header.js +4 -2
  65. package/node_modules/@maka/runtime/dist/provider-error-classification.js +84 -123
  66. package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +68 -108
  67. package/node_modules/@maka/runtime/dist/run-handoff-gate.js +97 -0
  68. package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +82 -54
  69. package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +117 -159
  70. package/node_modules/@maka/runtime/dist/runtime-kernel.js +150 -112
  71. package/node_modules/@maka/runtime/dist/runtime-ledger-repair.js +160 -79
  72. package/node_modules/@maka/runtime/dist/runtime-read-model.js +30 -89
  73. package/node_modules/@maka/runtime/dist/runtime-resume.js +80 -4
  74. package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +5 -0
  75. package/node_modules/@maka/runtime/dist/session-manager.js +171 -288
  76. package/node_modules/@maka/runtime/dist/session-projection-helpers.js +0 -27
  77. package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +3 -4
  78. package/node_modules/@maka/runtime/dist/test-connection.js +16 -16
  79. package/node_modules/@maka/runtime/dist/tool-result-archive-encoding.js +38 -0
  80. package/node_modules/@maka/runtime/dist/tool-runtime.js +3 -61
  81. package/node_modules/@maka/runtime/package.json +1 -0
  82. package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +35 -0
  83. package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +9 -1
  84. package/node_modules/@maka/runtime-host/dist/client/connection.js +3 -0
  85. package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +156 -0
  86. package/node_modules/@maka/runtime-host/dist/client/host-handoff.js +273 -0
  87. package/node_modules/@maka/runtime-host/dist/client/host-retirement.js +26 -5
  88. package/node_modules/@maka/runtime-host/dist/client/index.js +2 -0
  89. package/node_modules/@maka/runtime-host/dist/client/launcher.js +3 -0
  90. package/node_modules/@maka/runtime-host/dist/client/remote-compatibility-error.js +2 -0
  91. package/node_modules/@maka/runtime-host/dist/client/session-subscription.js +27 -5
  92. package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
  93. package/node_modules/@maka/runtime-host/dist/operator/retirement-cancellation.js +20 -0
  94. package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +21 -12
  95. package/node_modules/@maka/runtime-host/dist/protocol/index.js +25 -1
  96. package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
  97. package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +40 -20
  98. package/node_modules/@maka/runtime-host/dist/protocol/session-transcript.js +0 -5
  99. package/node_modules/@maka/runtime-host/dist/protocol/session-turns.js +29 -58
  100. package/node_modules/@maka/runtime-host/dist/protocol/turn.js +11 -1
  101. package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
  102. package/node_modules/@maka/runtime-host/dist/server/canonical-turn-snapshot.js +19 -4
  103. package/node_modules/@maka/runtime-host/dist/server/connection-session.js +7 -6
  104. package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +100 -24
  105. package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +5 -5
  106. package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +3 -2
  107. package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +40 -1
  108. package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +96 -17
  109. package/node_modules/@maka/runtime-host/dist/server/host-residency-registry.js +13 -7
  110. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-admission-registry.js +3 -1
  111. package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +124 -62
  112. package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +76 -6
  113. package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +325 -73
  114. package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +37 -2
  115. package/node_modules/@maka/runtime-host/dist/server/serial-outbound-writer.js +45 -3
  116. package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +50 -7
  117. package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +87 -25
  118. package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +8 -55
  119. package/node_modules/@maka/runtime-host/dist/server/session-sidecar-purge.js +1 -16
  120. package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +0 -1
  121. package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +460 -70
  122. package/node_modules/@maka/runtime-host/dist/server/shared-session-transcript.js +1 -1
  123. package/node_modules/@maka/runtime-host/dist/server/storage-maintenance.js +109 -0
  124. package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +0 -1
  125. package/node_modules/@maka/storage/dist/artifact-store.js +21 -10
  126. package/node_modules/@maka/storage/dist/artifact-stores.js +1 -1
  127. package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +0 -4
  128. package/node_modules/@maka/storage/dist/codex-session-adapter.js +0 -2
  129. package/node_modules/@maka/storage/dist/context-offload-snapshot.js +325 -0
  130. package/node_modules/@maka/storage/dist/execution-stores.js +9 -7
  131. package/node_modules/@maka/storage/dist/file-session-repository.js +1012 -0
  132. package/node_modules/@maka/storage/dist/opencode-session-adapter.js +0 -4
  133. package/node_modules/@maka/storage/dist/operational-state-backup.js +9 -5
  134. package/node_modules/@maka/storage/dist/runtime-event-persistence.js +1 -0
  135. package/node_modules/@maka/storage/dist/runtime-transcript-query.js +308 -0
  136. package/node_modules/@maka/storage/dist/session-bundle-policy.js +24 -4
  137. package/node_modules/@maka/storage/dist/session-message-projection.js +15 -0
  138. package/node_modules/@maka/storage/dist/session-repository.js +767 -0
  139. package/node_modules/@maka/storage/dist/session-store.js +25 -99
  140. package/node_modules/@maka/storage/dist/sqlite-artifact-metadata.js +10 -3
  141. package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +24 -8
  142. package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +7 -1
  143. package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +44 -1
  144. package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +129 -25
  145. package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +109 -892
  146. package/node_modules/@maka/storage/dist/tool-result-archive-evidence.js +111 -0
  147. package/node_modules/@maka/storage/package.json +2 -0
  148. package/package.json +1 -1
@@ -222,7 +222,6 @@ export function convertTranscript(sessionId, messages, parts) {
222
222
  ts: turn.lastTs,
223
223
  status: 'failed',
224
224
  errorClass: 'opencode_error',
225
- partialOutputRetained: true,
226
225
  });
227
226
  }
228
227
  else if (turn.aborted) {
@@ -234,7 +233,6 @@ export function convertTranscript(sessionId, messages, parts) {
234
233
  status: 'aborted',
235
234
  abortedAt: turn.lastTs,
236
235
  abortSource: EXTERNAL_SNAPSHOT_ABORT_SOURCE,
237
- partialOutputRetained: true,
238
236
  });
239
237
  }
240
238
  else if (turn.closed) {
@@ -244,7 +242,6 @@ export function convertTranscript(sessionId, messages, parts) {
244
242
  turnId: turn.turnId,
245
243
  ts: turn.lastTs,
246
244
  status: 'completed',
247
- partialOutputRetained: true,
248
245
  });
249
246
  }
250
247
  else {
@@ -259,7 +256,6 @@ export function convertTranscript(sessionId, messages, parts) {
259
256
  status: 'aborted',
260
257
  abortedAt: turn.lastTs,
261
258
  abortSource: EXTERNAL_SNAPSHOT_ABORT_SOURCE,
262
- partialOutputRetained: true,
263
259
  });
264
260
  }
265
261
  turn = undefined;
@@ -23,13 +23,14 @@ import { dirname, relative, resolve } from 'node:path';
23
23
  import { DatabaseSync } from 'node:sqlite';
24
24
  import { decodeArtifactRecordJsons } from './artifact-metadata-codec.js';
25
25
  import { withArtifactWriterLock } from './artifact-writer-lock.js';
26
+ import { withOfflineContextSnapshot, copyContextSnapshot, validateContextSnapshot, } from './context-offload-snapshot.js';
26
27
  import { decodeStoredMessage } from './execution-record-codec.js';
27
28
  import { acquireOperationalStateDatabase, inspectOperationalStateSchema, OPERATIONAL_STATE_DATABASE_NAME, } from './operational-state-store.js';
28
29
  import { assertCurrentOperationalTargetSchema } from './operational-target-schema.js';
29
30
  import { SQLITE_SESSION_MESSAGE_CHUNK_BYTES, SQLITE_SESSION_MESSAGE_CHUNK_MARKER, } from './sqlite-session-metadata-schema.js';
30
31
  import { syncDirectory, syncDirectoryChain, syncFile } from './stable-storage.js';
31
32
  export const OPERATIONAL_BACKUP_FORMAT = 'maka-operational-backup';
32
- export const OPERATIONAL_BACKUP_SCHEMA_VERSION = 3;
33
+ export const OPERATIONAL_BACKUP_SCHEMA_VERSION = 4;
33
34
  export const OPERATIONAL_BACKUP_MANIFEST_FILE = 'operational-backup.json';
34
35
  export class OperationalBackupError extends Error {
35
36
  code;
@@ -44,7 +45,7 @@ export async function createOperationalStateBackup(input) {
44
45
  const destinationRoot = resolve(input.destinationRoot);
45
46
  assertSeparateRoots(stateRoot, destinationRoot);
46
47
  await assertMissing(destinationRoot, 'backup destination');
47
- return withArtifactWriterLock(stateRoot, async (canonicalStateRoot) => {
48
+ return withOfflineContextSnapshot(stateRoot, (contextLocked) => withArtifactWriterLock(stateRoot, async (canonicalStateRoot) => {
48
49
  assertSeparateRoots(canonicalStateRoot, destinationRoot);
49
50
  const stagingRoot = `${destinationRoot}.${process.pid}.${randomUUID()}.tmp`;
50
51
  try {
@@ -64,6 +65,7 @@ export async function createOperationalStateBackup(input) {
64
65
  if (await pathExists(artifactRoot)) {
65
66
  await copyRegularTree(artifactRoot, resolve(stagingRoot, 'artifacts'), artifactRoot, stagingRoot);
66
67
  }
68
+ await copyContextSnapshot(canonicalStateRoot, stagingRoot, contextLocked);
67
69
  const createdAt = (input.now ?? Date.now)();
68
70
  if (!Number.isSafeInteger(createdAt) || createdAt < 0) {
69
71
  throw new OperationalBackupError('corrupt_backup', 'Backup creation time is invalid');
@@ -92,7 +94,7 @@ export async function createOperationalStateBackup(input) {
92
94
  await rm(stagingRoot, { recursive: true, force: true }).catch(() => { });
93
95
  throw error;
94
96
  }
95
- });
97
+ }));
96
98
  }
97
99
  export async function validateOperationalStateBackup(backupRoot) {
98
100
  const root = resolve(backupRoot);
@@ -111,6 +113,7 @@ export async function validateOperationalStateBackup(backupRoot) {
111
113
  throw new OperationalBackupError('corrupt_backup', 'Backup file inventory does not match');
112
114
  }
113
115
  validateSqlite(resolve(root, OPERATIONAL_STATE_DATABASE_NAME), manifest.files);
116
+ await validateContextSnapshot(root);
114
117
  return manifest;
115
118
  }
116
119
  export async function restoreOperationalStateBackup(input) {
@@ -136,6 +139,7 @@ export async function restoreOperationalStateBackup(input) {
136
139
  throw new OperationalBackupError('corrupt_backup', 'Restored file inventory does not match');
137
140
  }
138
141
  validateSqlite(resolve(stagingRoot, OPERATIONAL_STATE_DATABASE_NAME), manifest.files);
142
+ await validateContextSnapshot(stagingRoot);
139
143
  await syncDirectoryChain(stagingRoot, stagingRoot);
140
144
  await mkdir(dirname(destinationRoot), { recursive: true });
141
145
  await rename(stagingRoot, destinationRoot);
@@ -155,7 +159,7 @@ function decodeManifest(value) {
155
159
  if (record.format !== OPERATIONAL_BACKUP_FORMAT) {
156
160
  throw new OperationalBackupError('corrupt_backup', 'Backup format is invalid');
157
161
  }
158
- if (record.schemaVersion !== OPERATIONAL_BACKUP_SCHEMA_VERSION) {
162
+ if (record.schemaVersion !== 3 && record.schemaVersion !== OPERATIONAL_BACKUP_SCHEMA_VERSION) {
159
163
  throw new OperationalBackupError('unsupported_schema', 'Backup schema is unsupported');
160
164
  }
161
165
  if (!Number.isSafeInteger(record.createdAt) ||
@@ -184,7 +188,7 @@ function decodeManifest(value) {
184
188
  }
185
189
  return {
186
190
  format: OPERATIONAL_BACKUP_FORMAT,
187
- schemaVersion: OPERATIONAL_BACKUP_SCHEMA_VERSION,
191
+ schemaVersion: record.schemaVersion,
188
192
  createdAt: record.createdAt,
189
193
  files,
190
194
  };
@@ -48,6 +48,7 @@ export async function openRuntimeEventReadPersistence(input) {
48
48
  readRuntimeEventsBounded: (sessionId, runId, budget) => store.readRuntimeEventsBounded(sessionId, runId, budget),
49
49
  readImmutableRuntimeEvents: (sessionId, runId) => store.readImmutableRuntimeEvents(sessionId, runId),
50
50
  readSessionRuntimeEvents: (sessionId) => store.readSessionRuntimeEvents(sessionId),
51
+ readSessionRuntimeEventEntries: (sessionId) => store.readSessionRuntimeEventEntries(sessionId),
51
52
  }),
52
53
  close: () => store.close(),
53
54
  };
@@ -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
+ }