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
@@ -21,6 +21,7 @@ import { mkdirSync } from 'node:fs';
21
21
  import { createRequire } from 'node:module';
22
22
  import { dirname } from 'node:path';
23
23
  import { isDeepStrictEqual } from 'node:util';
24
+ import { assertHandoffClaimSource } from '@maka/core/runtime-handoff';
24
25
  import { buildWorkspaceBaselineAuthorityEvents, buildWorkspaceSuccessorAuthorityEvent, scanWorkspaceBaselineAuthority, WORKSPACE_AUTHORITY_SESSION_ID, WORKSPACE_VERSION_AUTHORITY_CAPABILITY_V1, } from '@maka/core/workspace-version-authority';
25
26
  import { decodeRuntimeEvent, decodeRuntimeInvocationOpened, isPartialRuntimeEvent, isTerminalRuntimeEvent, runtimeEventInvocationOpening, } from '@maka/core/runtime-event';
26
27
  import { RunSealedError, RUNTIME_CONTINUATION_AUTHORITY_V1, TOOL_RECOVERY_BUNDLE_CAPABILITY_V1, } from '@maka/core/runtime-event-store';
@@ -34,19 +35,8 @@ import { registerWorkspaceBaselineAuthorityWriterInternal, } from './workspace-v
34
35
  import { assertEvidenceReadBudget, measureEvidenceRows, } from './bounded-evidence.js';
35
36
  import { immutableSteeringMessageId, isRuntimeStorageSafeId } from './runtime-event-invariants.js';
36
37
  import { assertNoReservedWorkspaceAuthorityAppend } from './runtime-event-authority.js';
38
+ import { RuntimeTranscriptQuery, TERMINAL_RUNTIME_EVENT_SQL, } from './runtime-transcript-query.js';
37
39
  export { SQLITE_RUNTIME_SCHEMA_VERSION } from './sqlite-runtime-schema.js';
38
- /**
39
- * `isTerminalRuntimeEvent` asked in SQL.
40
- *
41
- * The TypeScript predicate stays the authority; this only lets a query find the
42
- * terminal event without decoding every row it passes over. Both have to say the
43
- * same thing, so the SQL half is written once here instead of at each query.
44
- */
45
- const TERMINAL_RUNTIME_EVENT_SQL = `(
46
- json_extract(payload_json, '$.actions.endInvocation') = 1
47
- OR json_extract(payload_json, '$.status')
48
- IN ('completed', 'failed', 'aborted', 'cancelled')
49
- )`;
50
40
  const RUNTIME_EVENT_SCAN_BATCH_SIZE = 128;
51
41
  const RUNTIME_PARTIAL_SEGMENT_TARGET_BYTES = 64 * 1024;
52
42
  function assertRuntimeEventScanBudget(budget) {
@@ -287,6 +277,33 @@ export class SqliteRuntimeStore {
287
277
  async readRuntimeEvents(sessionId, runId) {
288
278
  return this.readRuntimeEventsSync(sessionId, runId);
289
279
  }
280
+ transcriptQuery() {
281
+ return new RuntimeTranscriptQuery(this.db, (sessionId, invocationId) => {
282
+ // By invocation rather than by run: both shelves key their opening on it,
283
+ // so a page's records cost the page instead of the Session's Turns.
284
+ const opening = this.readInvocationOpeningsSync(sessionId, {
285
+ direction: 'asc',
286
+ invocationId,
287
+ }).at(0);
288
+ if (!opening)
289
+ throw new Error(`Transcript invocation ${invocationId} is missing`);
290
+ return this.completeInvocationRecordSync(opening);
291
+ });
292
+ }
293
+ async readTranscriptHighWater(sessionId) {
294
+ assertRuntimeStorageSafeId(sessionId, 'Invalid session id');
295
+ return this.readTransaction(() => this.transcriptQuery().highWater(sessionId));
296
+ }
297
+ async readTranscriptInvocations(sessionId, request) {
298
+ assertRuntimeStorageSafeId(sessionId, 'Invalid session id');
299
+ assertInvocationSearchLimit(request.limit);
300
+ return this.readTransaction(() => this.transcriptQuery().invocations(sessionId, request));
301
+ }
302
+ async readTranscriptLandmarks(sessionId, throughOrdinal, limit) {
303
+ assertRuntimeStorageSafeId(sessionId, 'Invalid session id');
304
+ assertInvocationSearchLimit(limit);
305
+ return this.readTransaction(() => this.transcriptQuery().landmarks(sessionId, throughOrdinal, limit));
306
+ }
290
307
  /**
291
308
  * Enumerate a Session's invocations: the opening fact names each one, and its
292
309
  * highest-sequence event says whether it ended.
@@ -400,6 +417,8 @@ export class SqliteRuntimeStore {
400
417
  1 AS from_events
401
418
  FROM runtime_events
402
419
  WHERE session_id = :sessionId AND event_kind = 'invocation_opened'
420
+ ${options.runId === undefined ? '' : 'AND run_id = :runId'}
421
+ ${options.invocationId === undefined ? '' : 'AND invocation_id = :invocationId'}
403
422
  UNION ALL
404
423
  SELECT
405
424
  NULL,
@@ -411,15 +430,15 @@ export class SqliteRuntimeStore {
411
430
  0
412
431
  FROM runtime_legacy_invocation_openings AS legacy
413
432
  WHERE legacy.session_id = :sessionId
433
+ ${options.runId === undefined ? '' : 'AND legacy.run_id = :runId'}
434
+ ${options.invocationId === undefined ? '' : 'AND legacy.invocation_id = :invocationId'}
414
435
  AND NOT EXISTS (
415
436
  SELECT 1 FROM runtime_events
416
437
  WHERE runtime_events.invocation_id = legacy.invocation_id
417
438
  AND runtime_events.event_kind = 'invocation_opened'
418
439
  )
419
440
  )
420
- WHERE (:invocationId IS NULL OR invocation_id = :invocationId)
421
- AND (:runId IS NULL OR run_id = :runId)
422
- AND (
441
+ WHERE (
423
442
  :beforeOpenedAt IS NULL
424
443
  OR opened_at < :beforeOpenedAt
425
444
  OR (opened_at = :beforeOpenedAt AND invocation_id < :beforeInvocationId)
@@ -429,8 +448,8 @@ export class SqliteRuntimeStore {
429
448
  `)
430
449
  .all({
431
450
  sessionId,
432
- invocationId: options.invocationId ?? null,
433
- runId: options.runId ?? null,
451
+ ...(options.invocationId === undefined ? {} : { invocationId: options.invocationId }),
452
+ ...(options.runId === undefined ? {} : { runId: options.runId }),
434
453
  beforeOpenedAt: options.before?.openedAt ?? null,
435
454
  beforeInvocationId: options.before?.invocationId ?? null,
436
455
  limit: options.limit ?? -1,
@@ -801,12 +820,12 @@ export class SqliteRuntimeStore {
801
820
  const conflict = this.readContinuationClaimRow(`claim_id = ?
802
821
  OR target_invocation_id = ?
803
822
  OR target_run_id = ?
804
- OR (target_session_id = ? AND target_turn_id = ?)
823
+ OR (? = 0 AND target_session_id = ? AND target_turn_id = ?)
805
824
  OR (
806
825
  source_session_id = ?
807
826
  AND source_run_id = ?
808
827
  AND source_event_high_water = ?
809
- )`, claim.claimId, claim.target.invocationId, claim.target.runId, claim.target.sessionId, claim.target.turnId, source.identity.sessionId, source.identity.runId, source.position.lastEventSeq);
828
+ )`, claim.claimId, claim.target.invocationId, claim.target.runId, claim.targetOpening.source.kind === 'handoff' ? 1 : 0, claim.target.sessionId, claim.target.turnId, source.identity.sessionId, source.identity.runId, source.position.lastEventSeq);
810
829
  if (conflict) {
811
830
  return { kind: 'conflict', claim: decodeContinuationClaimRow(conflict) };
812
831
  }
@@ -844,12 +863,12 @@ export class SqliteRuntimeStore {
844
863
  this.readContinuationClaimRow(`claim_id = ?
845
864
  OR target_invocation_id = ?
846
865
  OR target_run_id = ?
847
- OR (target_session_id = ? AND target_turn_id = ?)
866
+ OR (? = 0 AND target_session_id = ? AND target_turn_id = ?)
848
867
  OR (
849
868
  source_session_id = ?
850
869
  AND source_run_id = ?
851
870
  AND source_event_high_water = ?
852
- )`, claim.claimId, claim.target.invocationId, claim.target.runId, claim.target.sessionId, claim.target.turnId, source.identity.sessionId, source.identity.runId, source.position.lastEventSeq);
871
+ )`, claim.claimId, claim.target.invocationId, claim.target.runId, claim.targetOpening.source.kind === 'handoff' ? 1 : 0, claim.target.sessionId, claim.target.turnId, source.identity.sessionId, source.identity.runId, source.position.lastEventSeq);
853
872
  if (!raced)
854
873
  throw error;
855
874
  const racedClaim = decodeContinuationClaimRow(raced);
@@ -1015,6 +1034,57 @@ export class SqliteRuntimeStore {
1015
1034
  return { ordinal: row.ordinal, event };
1016
1035
  });
1017
1036
  }
1037
+ async resequenceSessionEventOrdinals(sessionId) {
1038
+ assertRuntimeStorageSafeId(sessionId, 'Invalid session id');
1039
+ this.transaction(() => {
1040
+ // Lifted above the range first: the second statement renumbers into the
1041
+ // space these rows occupy, and (session_id, ordinal) is a primary key.
1042
+ // Shifting up rather than below zero keeps every intermediate value
1043
+ // inside the table's own `ordinal > 0`, and lands them past the 1..N the
1044
+ // renumber assigns, since the count cannot exceed the maximum.
1045
+ const { shift } = this.db
1046
+ .prepare(`
1047
+ SELECT COALESCE(MAX(ordinal), 0) AS shift
1048
+ FROM runtime_session_event_ordinals
1049
+ WHERE session_id = ?
1050
+ `)
1051
+ .get(sessionId);
1052
+ this.db
1053
+ .prepare(`
1054
+ UPDATE runtime_session_event_ordinals
1055
+ SET ordinal = ordinal + :shift
1056
+ WHERE session_id = :sessionId
1057
+ `)
1058
+ .run({ sessionId, shift });
1059
+ this.db
1060
+ .prepare(`
1061
+ WITH opening AS (
1062
+ SELECT invocation_id, CAST(json_extract(payload_json, '$.ts') AS INTEGER) AS opened_at
1063
+ FROM runtime_events
1064
+ WHERE session_id = :sessionId AND event_kind = 'invocation_opened'
1065
+ ),
1066
+ ordered AS MATERIALIZED (
1067
+ SELECT
1068
+ o.event_id AS event_id,
1069
+ ROW_NUMBER() OVER (
1070
+ ORDER BY COALESCE(opening.opened_at, e.committed_at), e.invocation_id, o.ordinal
1071
+ ) AS ordinal
1072
+ FROM runtime_session_event_ordinals o
1073
+ JOIN runtime_events e ON e.event_id = o.event_id
1074
+ LEFT JOIN opening ON opening.invocation_id = e.invocation_id
1075
+ WHERE o.session_id = :sessionId
1076
+ )
1077
+ UPDATE runtime_session_event_ordinals
1078
+ SET ordinal = (
1079
+ SELECT ordered.ordinal
1080
+ FROM ordered
1081
+ WHERE ordered.event_id = runtime_session_event_ordinals.event_id
1082
+ )
1083
+ WHERE session_id = :sessionId
1084
+ `)
1085
+ .run({ sessionId });
1086
+ });
1087
+ }
1018
1088
  async #commitWorkspaceBaseline(input, rootId) {
1019
1089
  const events = buildWorkspaceBaselineAuthorityEvents(input);
1020
1090
  return this.transaction(() => {
@@ -2428,6 +2498,7 @@ export class SqliteRuntimeStore {
2428
2498
  target.invocationId,
2429
2499
  target.sessionId,
2430
2500
  target.runId,
2501
+ claim.targetOpening.source.kind === 'handoff' ? 1 : 0,
2431
2502
  target.sessionId,
2432
2503
  target.turnId,
2433
2504
  ];
@@ -2437,7 +2508,7 @@ export class SqliteRuntimeStore {
2437
2508
  FROM runtime_events
2438
2509
  WHERE invocation_id = ?
2439
2510
  OR (session_id = ? AND run_id = ?)
2440
- OR (session_id = ? AND turn_id = ?)
2511
+ OR (? = 0 AND session_id = ? AND turn_id = ?)
2441
2512
  LIMIT 1
2442
2513
  `)
2443
2514
  .get(...values);
@@ -2449,7 +2520,7 @@ export class SqliteRuntimeStore {
2449
2520
  FROM runtime_partial_snapshots
2450
2521
  WHERE invocation_id = ?
2451
2522
  OR (session_id = ? AND run_id = ?)
2452
- OR (session_id = ? AND turn_id = ?)
2523
+ OR (? = 0 AND session_id = ? AND turn_id = ?)
2453
2524
  LIMIT 1
2454
2525
  `)
2455
2526
  .get(...values) !== undefined);
@@ -2474,6 +2545,7 @@ export class SqliteRuntimeStore {
2474
2545
  }
2475
2546
  assertContinuationBoundaryMatchesLedger(claim) {
2476
2547
  const lastIndex = claim.boundary.segments.length - 1;
2548
+ let previousPrefix;
2477
2549
  for (const [index, segment] of claim.boundary.segments.entries()) {
2478
2550
  let prefix;
2479
2551
  try {
@@ -2501,13 +2573,34 @@ export class SqliteRuntimeStore {
2501
2573
  ? 'Continuation source boundary changed'
2502
2574
  : `Continuation ancestor boundary changed for ${segment.identity.runId}`);
2503
2575
  }
2576
+ const opening = prefix.events[0]?.content;
2577
+ const repeatsTurn = previousPrefix?.identity.turnId === prefix.identity.turnId;
2578
+ if (repeatsTurn ||
2579
+ (opening?.kind === 'invocation_opened' && opening.source.kind === 'handoff')) {
2580
+ if (!previousPrefix ||
2581
+ opening?.kind !== 'invocation_opened' ||
2582
+ opening.source.kind !== 'handoff') {
2583
+ throw new Error('Same-turn boundary requires an authenticated handoff edge');
2584
+ }
2585
+ const row = this.readContinuationClaimRow('claim_id = ?', opening.source.claimId);
2586
+ const state = row && this.decodeContinuationClaimStateRow(row);
2587
+ if (!state ||
2588
+ state.startEventId !== prefix.events[0]?.id ||
2589
+ !isDeepStrictEqual(state.claim.boundary.segments, claim.boundary.segments.slice(0, index)) ||
2590
+ !continuationStartEventMatchesClaim(prefix.events[0], state.claim, state.startKind)) {
2591
+ throw new Error('Same-turn boundary handoff claim does not authenticate its lineage');
2592
+ }
2593
+ assertHandoffClaimSource(state.claim, previousPrefix);
2594
+ }
2504
2595
  if (index === lastIndex) {
2596
+ assertHandoffClaimSource(claim, prefix);
2505
2597
  const terminalEvents = prefix.events.filter(isTerminalRuntimeEvent);
2506
2598
  const terminal = terminalEvents[0];
2507
2599
  if (terminalEvents.length !== 1 || !terminal || prefix.events.at(-1)?.id !== terminal.id) {
2508
2600
  throw new Error('Continuation source boundary must end with exactly one terminal RuntimeEvent');
2509
2601
  }
2510
2602
  }
2603
+ previousPrefix = prefix;
2511
2604
  }
2512
2605
  }
2513
2606
  assertToolLedgerTransition(candidateEvents, expectedTransition) {
@@ -2639,16 +2732,27 @@ export class SqliteRuntimeStore {
2639
2732
  }
2640
2733
  }
2641
2734
  assertContinuationAuthorityAllowsEvent(event, authorizedPendingClaimId, exactRetry = false) {
2642
- for (const row of this.readContinuationClaimRows()) {
2735
+ const rows = this.readContinuationClaimRows();
2736
+ const ownClaim = rows.find((row) => row.target_session_id === event.sessionId &&
2737
+ row.target_invocation_id === event.invocationId &&
2738
+ row.target_run_id === event.runId &&
2739
+ row.target_turn_id === event.turnId);
2740
+ const ownHandoff = ownClaim && decodeContinuationClaimRow(ownClaim);
2741
+ for (const row of rows) {
2643
2742
  const claim = decodeContinuationClaimRow(row);
2644
2743
  const source = claim.boundary.segments.find((segment) => segment.identity.sessionId === event.sessionId && segment.identity.runId === event.runId);
2645
2744
  if (source && !exactRetry) {
2646
2745
  throw new Error(`RuntimeEvent source boundary is sealed by continuation claim ${claim.claimId}`);
2647
2746
  }
2747
+ if (source && exactRetry)
2748
+ continue;
2648
2749
  const target = claim.target;
2649
2750
  const collidesWithTarget = event.invocationId === target.invocationId ||
2650
2751
  (event.sessionId === target.sessionId && event.runId === target.runId) ||
2651
- (event.sessionId === target.sessionId && event.turnId === target.turnId);
2752
+ (event.sessionId === target.sessionId &&
2753
+ event.turnId === target.turnId &&
2754
+ !(ownHandoff?.targetOpening.source.kind === 'handoff' &&
2755
+ ownHandoff.boundary.segments.some((segment) => segment.identity.runId === target.runId)));
2652
2756
  if (!collidesWithTarget)
2653
2757
  continue;
2654
2758
  if (event.sessionId !== target.sessionId ||