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.
- package/dist/acp/maka-acp-agent.js +2 -1
- package/dist/acp/session-configuration.js +143 -0
- package/dist/acp/session-registry.js +90 -4
- package/dist/cli-core.js +2 -0
- package/dist/pi-transcript.js +5 -12
- package/dist/pi-tui-runner.js +74 -4
- package/dist/runtime-host-cli-context.js +167 -78
- package/dist/runtime-host-cli-installation.js +8 -2
- package/dist/runtime-host-cli.js +31 -1
- package/dist/runtime-host-handoff-surface.js +81 -0
- package/dist/runtime-host-installed-update-bootstrap.js +13 -0
- package/dist/runtime-host-installed-update-coordinator.js +33 -0
- package/dist/runtime-host-lifecycle-transaction.js +9 -1
- package/dist/runtime-host-local-handoff.js +37 -4
- package/dist/runtime-host-local-managed-activation.js +84 -0
- package/dist/runtime-host-local-source-retirement.js +18 -2
- package/dist/runtime-host-managed-lifecycle-manager.js +1 -0
- package/dist/runtime-host-operator-retirement.js +50 -0
- package/dist/runtime-host-service-manager.js +3 -0
- package/dist/runtime-host-session-driver.js +4 -25
- package/dist/runtime-host-session-update.js +62 -0
- package/dist/runtime-host-tui-command.js +38 -73
- package/dist/runtime-host-tui-context.js +1 -0
- package/dist/runtime-host-tui-owner.js +226 -0
- package/dist/tui-copy-catalog.js +140 -3
- package/dist/tui-host-owner-copy.js +24 -0
- package/native/runtime-host-windows-task-launcher/prebuilds/win32-x64/maka-runtime-host-task-launcher.exe +0 -0
- package/node_modules/@maka/core/dist/canonical-runtime-event.js +3 -1
- package/node_modules/@maka/core/dist/model-failure.js +40 -0
- package/node_modules/@maka/core/dist/model-metadata.generated.js +4 -4
- package/node_modules/@maka/core/dist/model-metadata.js +3 -3
- package/node_modules/@maka/core/dist/model-web-search.js +5 -4
- package/node_modules/@maka/core/dist/provider-registry.js +45 -4
- package/node_modules/@maka/core/dist/record-schema.js +9 -2
- package/node_modules/@maka/core/dist/runtime-boundary.js +31 -11
- package/node_modules/@maka/core/dist/runtime-event.js +37 -6
- package/node_modules/@maka/core/dist/runtime-handoff.js +98 -0
- package/node_modules/@maka/core/dist/runtime-invocation.js +1 -1
- package/node_modules/@maka/core/dist/runtime-logical-execution.js +112 -0
- package/node_modules/@maka/core/dist/session.js +54 -52
- package/node_modules/@maka/core/dist/slash-command-catalog.js +2 -0
- package/node_modules/@maka/core/dist/tool-result-archive-evidence.js +20 -0
- package/node_modules/@maka/core/dist/usage-record-schema.js +26 -35
- package/node_modules/@maka/core/package.json +4 -0
- package/node_modules/@maka/mcp/dist/index.js +131 -1
- package/node_modules/@maka/runtime/dist/agent-run.js +272 -105
- package/node_modules/@maka/runtime/dist/ai-sdk-backend.js +13 -5
- package/node_modules/@maka/runtime/dist/ai-sdk-compaction.js +109 -34
- package/node_modules/@maka/runtime/dist/ai-sdk-turn.js +127 -133
- package/node_modules/@maka/runtime/dist/context-budget.js +14 -9
- package/node_modules/@maka/runtime/dist/conversation-copy.js +5 -4
- package/node_modules/@maka/runtime/dist/goal-continuation.js +36 -4
- package/node_modules/@maka/runtime/dist/history-compact-checkpoint.js +18 -9
- package/node_modules/@maka/runtime/dist/history-compact-summarizer.js +30 -22
- package/node_modules/@maka/runtime/dist/history-compaction.js +29 -4
- package/node_modules/@maka/runtime/dist/ledger-tool-result-archive-reader.js +106 -0
- package/node_modules/@maka/runtime/dist/message-authority.js +9 -0
- package/node_modules/@maka/runtime/dist/model-adapter.js +19 -98
- package/node_modules/@maka/runtime/dist/model-factory.js +19 -81
- package/node_modules/@maka/runtime/dist/model-fetcher.js +7 -0
- package/node_modules/@maka/runtime/dist/model-runtime.js +85 -111
- package/node_modules/@maka/runtime/dist/native-web-search-tool.js +2 -1
- package/node_modules/@maka/runtime/dist/openai-codex-history-compactor.js +2 -2
- package/node_modules/@maka/runtime/dist/opencode-session-header.js +4 -2
- package/node_modules/@maka/runtime/dist/provider-error-classification.js +84 -123
- package/node_modules/@maka/runtime/dist/provider-request-telemetry.js +68 -108
- package/node_modules/@maka/runtime/dist/run-handoff-gate.js +97 -0
- package/node_modules/@maka/runtime/dist/runtime-event-backfill.js +82 -54
- package/node_modules/@maka/runtime/dist/runtime-event-read-model.js +117 -159
- package/node_modules/@maka/runtime/dist/runtime-kernel.js +150 -112
- package/node_modules/@maka/runtime/dist/runtime-ledger-repair.js +160 -79
- package/node_modules/@maka/runtime/dist/runtime-read-model.js +30 -89
- package/node_modules/@maka/runtime/dist/runtime-resume.js +80 -4
- package/node_modules/@maka/runtime/dist/session-event-runtime-mapper.js +5 -0
- package/node_modules/@maka/runtime/dist/session-manager.js +171 -288
- package/node_modules/@maka/runtime/dist/session-projection-helpers.js +0 -27
- package/node_modules/@maka/runtime/dist/subscription-model-fetch.js +3 -4
- package/node_modules/@maka/runtime/dist/test-connection.js +16 -16
- package/node_modules/@maka/runtime/dist/tool-result-archive-encoding.js +38 -0
- package/node_modules/@maka/runtime/dist/tool-runtime.js +3 -61
- package/node_modules/@maka/runtime/package.json +1 -0
- package/node_modules/@maka/runtime-host/dist/adapter/session-projector.js +35 -0
- package/node_modules/@maka/runtime-host/dist/client/connect-or-spawn.js +9 -1
- package/node_modules/@maka/runtime-host/dist/client/connection.js +3 -0
- package/node_modules/@maka/runtime-host/dist/client/host-handoff-copy.js +156 -0
- package/node_modules/@maka/runtime-host/dist/client/host-handoff.js +273 -0
- package/node_modules/@maka/runtime-host/dist/client/host-retirement.js +26 -5
- package/node_modules/@maka/runtime-host/dist/client/index.js +2 -0
- package/node_modules/@maka/runtime-host/dist/client/launcher.js +3 -0
- package/node_modules/@maka/runtime-host/dist/client/remote-compatibility-error.js +2 -0
- package/node_modules/@maka/runtime-host/dist/client/session-subscription.js +27 -5
- package/node_modules/@maka/runtime-host/dist/operator/index.js +2 -1
- package/node_modules/@maka/runtime-host/dist/operator/retirement-cancellation.js +20 -0
- package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +21 -12
- package/node_modules/@maka/runtime-host/dist/protocol/index.js +25 -1
- package/node_modules/@maka/runtime-host/dist/protocol/operations.js +1 -0
- package/node_modules/@maka/runtime-host/dist/protocol/session-continuity.js +40 -20
- package/node_modules/@maka/runtime-host/dist/protocol/session-transcript.js +0 -5
- package/node_modules/@maka/runtime-host/dist/protocol/session-turns.js +29 -58
- package/node_modules/@maka/runtime-host/dist/protocol/turn.js +11 -1
- package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +1 -0
- package/node_modules/@maka/runtime-host/dist/server/canonical-turn-snapshot.js +19 -4
- package/node_modules/@maka/runtime-host/dist/server/connection-session.js +7 -6
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +100 -24
- package/node_modules/@maka/runtime-host/dist/server/execution-model-authority.js +5 -5
- package/node_modules/@maka/runtime-host/dist/server/execution-model-composition.js +3 -2
- package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +40 -1
- package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +96 -17
- package/node_modules/@maka/runtime-host/dist/server/host-residency-registry.js +13 -7
- package/node_modules/@maka/runtime-host/dist/server/hosted-execution-admission-registry.js +3 -1
- package/node_modules/@maka/runtime-host/dist/server/hosted-execution-recovery.js +124 -62
- package/node_modules/@maka/runtime-host/dist/server/message-coordinator.js +76 -6
- package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +325 -73
- package/node_modules/@maka/runtime-host/dist/server/scheduled-task-coordinator.js +37 -2
- package/node_modules/@maka/runtime-host/dist/server/serial-outbound-writer.js +45 -3
- package/node_modules/@maka/runtime-host/dist/server/session-catalog-coordinator.js +50 -7
- package/node_modules/@maka/runtime-host/dist/server/session-continuity-coordinator.js +87 -25
- package/node_modules/@maka/runtime-host/dist/server/session-revision-coordinator.js +8 -55
- package/node_modules/@maka/runtime-host/dist/server/session-sidecar-purge.js +1 -16
- package/node_modules/@maka/runtime-host/dist/server/session-transcript-pager.js +0 -1
- package/node_modules/@maka/runtime-host/dist/server/session-transcript-reader.js +460 -70
- package/node_modules/@maka/runtime-host/dist/server/shared-session-transcript.js +1 -1
- package/node_modules/@maka/runtime-host/dist/server/storage-maintenance.js +109 -0
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +0 -1
- package/node_modules/@maka/storage/dist/artifact-store.js +21 -10
- package/node_modules/@maka/storage/dist/artifact-stores.js +1 -1
- package/node_modules/@maka/storage/dist/claude-code-session-adapter.js +0 -4
- package/node_modules/@maka/storage/dist/codex-session-adapter.js +0 -2
- package/node_modules/@maka/storage/dist/context-offload-snapshot.js +325 -0
- package/node_modules/@maka/storage/dist/execution-stores.js +9 -7
- package/node_modules/@maka/storage/dist/file-session-repository.js +1012 -0
- package/node_modules/@maka/storage/dist/opencode-session-adapter.js +0 -4
- package/node_modules/@maka/storage/dist/operational-state-backup.js +9 -5
- package/node_modules/@maka/storage/dist/runtime-event-persistence.js +1 -0
- package/node_modules/@maka/storage/dist/runtime-transcript-query.js +308 -0
- package/node_modules/@maka/storage/dist/session-bundle-policy.js +24 -4
- package/node_modules/@maka/storage/dist/session-message-projection.js +15 -0
- package/node_modules/@maka/storage/dist/session-repository.js +767 -0
- package/node_modules/@maka/storage/dist/session-store.js +25 -99
- package/node_modules/@maka/storage/dist/sqlite-artifact-metadata.js +10 -3
- package/node_modules/@maka/storage/dist/sqlite-context-offload-store.js +24 -8
- package/node_modules/@maka/storage/dist/sqlite-core-execution-schema.js +7 -1
- package/node_modules/@maka/storage/dist/sqlite-runtime-schema.js +44 -1
- package/node_modules/@maka/storage/dist/sqlite-runtime-store.js +129 -25
- package/node_modules/@maka/storage/dist/sqlite-session-metadata-store.js +109 -892
- package/node_modules/@maka/storage/dist/tool-result-archive-evidence.js +111 -0
- package/node_modules/@maka/storage/package.json +2 -0
- package/package.json +1 -1
|
@@ -17,39 +17,52 @@
|
|
|
17
17
|
* under the License.
|
|
18
18
|
*/
|
|
19
19
|
import { isDeepStrictEqual } from 'node:util';
|
|
20
|
+
import { readLogicalRuntimeExecution } from '@maka/core/runtime-logical-execution';
|
|
20
21
|
import { runtimeInvocationOutcome, } from '@maka/core/runtime-invocation';
|
|
21
22
|
import { messageContentsEqual, normalizeMessageContent, } from '@maka/core/events';
|
|
22
|
-
import {
|
|
23
|
+
import { projectRuntimeEventUserMessage } from '@maka/runtime/runtime-event-read-model';
|
|
24
|
+
import { admittedPromptEventId, RuntimeMessageAuthorityInvariantError, } from '@maka/runtime/message-authority';
|
|
23
25
|
/** Validates and repairs durable admission/message relationships before execution replay. */
|
|
24
26
|
export async function prepareHostedExecutionRecovery(input) {
|
|
25
27
|
// listHeaders() avoids listForRecovery()'s discarded per-Session message
|
|
26
|
-
// pre-read
|
|
27
|
-
//
|
|
28
|
+
// pre-read. The messages an admission is checked against are the Session's
|
|
29
|
+
// own RuntimeEvents: the ledger is where a Turn's user message is committed,
|
|
30
|
+
// so it is also the only place a missing one can be detected.
|
|
28
31
|
const sessions = await input.stores.sessionStore.listHeaders();
|
|
29
32
|
const prepared = [];
|
|
30
33
|
for (const session of sessions) {
|
|
31
34
|
const admissions = await input.rootAdmissions.recoverSession(session.id);
|
|
32
|
-
const messages = await input.stores.sessionStore.readMessagesForRecovery(session.id);
|
|
33
35
|
const runs = await input.stores.runtimeEventStore.listSessionInvocations(session.id);
|
|
34
36
|
const runsById = new Map(runs.map((run) => [run.runId, run]));
|
|
35
37
|
for (const run of runs) {
|
|
36
38
|
await input.stores.agentRunStore.readEventsForRecovery(session.id, run.runId);
|
|
37
39
|
await input.stores.runtimeEventStore.readRuntimeEvents(session.id, run.runId);
|
|
38
40
|
}
|
|
39
|
-
const messageIndex = indexRecoveryMessages(
|
|
41
|
+
const messageIndex = indexRecoveryMessages(recoveryUserMessagesFromLedger(await input.stores.runtimeEventStore.readSessionRuntimeEvents(session.id)));
|
|
40
42
|
const replayAdmissions = [];
|
|
41
43
|
const rootReplayAdmissions = [];
|
|
42
|
-
const missingMessages = [];
|
|
43
44
|
const pendingRecoveryClosures = [];
|
|
44
45
|
for (const admission of admissions) {
|
|
45
46
|
const run = runsById.get(admission.runId);
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
47
|
+
const logical = run && (await readLogicalRuntimeExecution(input.stores.runtimeEventStore, admission, run));
|
|
48
|
+
if (logical?.pendingHandoff) {
|
|
49
|
+
replayAdmissions.push(admission);
|
|
50
|
+
rootReplayAdmissions.push(admission);
|
|
51
|
+
}
|
|
52
|
+
const admittedMessageId = admittedPromptEventId(admission.runId, admission.userMessageId);
|
|
53
|
+
// Whether the prompt is on the ledger is a question about the Turn, not
|
|
54
|
+
// about the id it landed under: a Run written by an older build derived
|
|
55
|
+
// that id differently, and matching on the id would read its prompt as
|
|
56
|
+
// missing and record a second one.
|
|
57
|
+
const rootUserMessages = messageIndex.userMessagesByTurnId.get(admission.turnId) ?? [];
|
|
58
|
+
const messageIdOwners = messageIndex.messagesById.get(admittedMessageId) ?? [];
|
|
59
|
+
if (messageIdOwners.length > 1) {
|
|
60
|
+
throw new Error(`Admitted Turn ${admission.turnId} has a duplicated UserMessage identity`);
|
|
61
|
+
}
|
|
62
|
+
const messageIdOwner = messageIdOwners[0];
|
|
50
63
|
const executionContract = recoveryExecutionContract(admission.execution);
|
|
51
64
|
if (admission.execution.kind === 'scheduled_task' &&
|
|
52
|
-
(!
|
|
65
|
+
(!logical || runtimeInvocationOutcome(logical.tip) === undefined)) {
|
|
53
66
|
if (!input.assertScheduledTaskAdmission) {
|
|
54
67
|
throw new RuntimeMessageAuthorityInvariantError('ScheduledTask recovery admission has no canonical authority validator');
|
|
55
68
|
}
|
|
@@ -67,12 +80,9 @@ export async function prepareHostedExecutionRecovery(input) {
|
|
|
67
80
|
if (admission.sourceMessages.length > 0) {
|
|
68
81
|
await verifyQueueSourceMessages(admission, messageIndex, input.stores.agentRunStore);
|
|
69
82
|
}
|
|
70
|
-
if (rootUserMessages.length > 0) {
|
|
71
|
-
throw new Error(`Admitted Turn ${admission.turnId} must not record a UserMessage`);
|
|
72
|
-
}
|
|
73
83
|
if (!run) {
|
|
74
84
|
if (executionContract.pendingWithoutRun === 'host_recovery_closure') {
|
|
75
|
-
pendingRecoveryClosures.push(admission);
|
|
85
|
+
pendingRecoveryClosures.push({ admission });
|
|
76
86
|
}
|
|
77
87
|
else {
|
|
78
88
|
replayAdmissions.push(admission);
|
|
@@ -88,35 +98,35 @@ export async function prepareHostedExecutionRecovery(input) {
|
|
|
88
98
|
else {
|
|
89
99
|
await input.projection.assertRunIdentityAndContinuation(run, admission.turnId, admission.execution);
|
|
90
100
|
}
|
|
101
|
+
if (executionContract.requiresUserMessage) {
|
|
102
|
+
const recorded = verifyUserMessage(admission, rootUserMessages, messageIdOwner);
|
|
103
|
+
await recordAdmittedUserMessage(input.stores, admission, run, recorded);
|
|
104
|
+
}
|
|
91
105
|
continue;
|
|
92
106
|
}
|
|
93
|
-
if (messageIdOwners.length > 1) {
|
|
94
|
-
throw new Error(`Admitted Turn ${admission.turnId} has a duplicated UserMessage identity`);
|
|
95
|
-
}
|
|
96
|
-
const messageIdOwner = messageIdOwners[0];
|
|
97
107
|
if (!run && executionContract.pendingWithoutRun === 'host_recovery_closure') {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
108
|
+
// The closure below opens this Turn's invocation, so it is also what
|
|
109
|
+
// writes the message the crashed admission never got to record.
|
|
110
|
+
const recorded = verifyUserMessage(admission, rootUserMessages, messageIdOwner);
|
|
111
|
+
pendingRecoveryClosures.push({
|
|
112
|
+
admission,
|
|
113
|
+
...(recorded ? {} : { writesUserMessage: true }),
|
|
114
|
+
});
|
|
105
115
|
continue;
|
|
106
116
|
}
|
|
107
117
|
if (!run) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
throw new Error(`Admitted Turn ${admission.turnId} has a UserMessage but no Run`);
|
|
113
|
-
}
|
|
118
|
+
// Every remaining path replays the admission, and a replay opens the
|
|
119
|
+
// Turn with the admission's own message id — writing the message here
|
|
120
|
+
// would only race the Run that owns it.
|
|
121
|
+
verifyUserMessage(admission, rootUserMessages, messageIdOwner);
|
|
114
122
|
replayAdmissions.push(admission);
|
|
115
|
-
|
|
123
|
+
if (executionContract.pendingWithoutRun !== 'domain_replay') {
|
|
124
|
+
rootReplayAdmissions.push(admission);
|
|
125
|
+
}
|
|
116
126
|
continue;
|
|
117
127
|
}
|
|
118
128
|
await input.projection.assertRunIdentityAndContinuation(run, admission.turnId, admission.execution);
|
|
119
|
-
|
|
129
|
+
await recordAdmittedUserMessage(input.stores, admission, run, verifyUserMessage(admission, rootUserMessages, messageIdOwner));
|
|
120
130
|
}
|
|
121
131
|
if (replayAdmissions.length > 1) {
|
|
122
132
|
throw new Error(`Session ${session.id} has multiple admitted Turns without Runs`);
|
|
@@ -128,24 +138,30 @@ export async function prepareHostedExecutionRecovery(input) {
|
|
|
128
138
|
sessionId: session.id,
|
|
129
139
|
admissions,
|
|
130
140
|
...(rootReplayAdmissions[0] ? { rootReplayAdmission: rootReplayAdmissions[0] } : {}),
|
|
131
|
-
missingMessages,
|
|
132
141
|
pendingRecoveryClosures,
|
|
133
142
|
});
|
|
134
143
|
}
|
|
135
144
|
for (const plan of prepared) {
|
|
136
|
-
for (const
|
|
137
|
-
await input.stores.sessionStore.appendMessage(plan.sessionId, message);
|
|
138
|
-
}
|
|
139
|
-
for (const admission of plan.pendingRecoveryClosures) {
|
|
145
|
+
for (const { admission, writesUserMessage } of plan.pendingRecoveryClosures) {
|
|
140
146
|
if (!usesHostRecoveryClosure(admission.execution)) {
|
|
141
147
|
throw new Error('Execution domain cannot use Host recovery closure');
|
|
142
148
|
}
|
|
149
|
+
const origin = hostedExecutionMessageOrigin(admission.execution);
|
|
143
150
|
await input.runtime.closePendingHostedAdmission({
|
|
144
151
|
sessionId: admission.sessionId,
|
|
145
152
|
turnId: admission.turnId,
|
|
146
153
|
runId: admission.runId,
|
|
147
154
|
admittedAt: admission.admittedAt,
|
|
148
155
|
execution: admission.execution,
|
|
156
|
+
...(writesUserMessage && admission.userMessageId
|
|
157
|
+
? {
|
|
158
|
+
userMessage: {
|
|
159
|
+
id: admission.userMessageId,
|
|
160
|
+
content: requireHostedExecutionMessageContent(admission),
|
|
161
|
+
...(origin ? { origin } : {}),
|
|
162
|
+
},
|
|
163
|
+
}
|
|
164
|
+
: {}),
|
|
149
165
|
});
|
|
150
166
|
}
|
|
151
167
|
}
|
|
@@ -155,6 +171,48 @@ export async function prepareHostedExecutionRecovery(input) {
|
|
|
155
171
|
...(rootReplayAdmission ? { rootReplayAdmission } : {}),
|
|
156
172
|
}));
|
|
157
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* The message a crashed Turn was admitted with, written into the Run that had
|
|
176
|
+
* already opened for it.
|
|
177
|
+
*
|
|
178
|
+
* A Run records its own user message right after its opening fact, so a Run
|
|
179
|
+
* that exists without one crashed between those two writes. Nothing else will
|
|
180
|
+
* write it now: the terminal fact recovery is about to append would seal the
|
|
181
|
+
* Turn without ever saying what the user asked for.
|
|
182
|
+
*
|
|
183
|
+
* The admission's normalized input is what goes in, not its queue sources: a
|
|
184
|
+
* Root folded from several Messages ran as one prompt, and that is the prompt
|
|
185
|
+
* the Turn was executed with.
|
|
186
|
+
*/
|
|
187
|
+
async function recordAdmittedUserMessage(stores, admission, run, ledgerHasMessage) {
|
|
188
|
+
if (run.terminalEvent)
|
|
189
|
+
return;
|
|
190
|
+
const content = requireHostedExecutionMessageContent(admission);
|
|
191
|
+
const origin = hostedExecutionMessageOrigin(admission.execution);
|
|
192
|
+
const event = {
|
|
193
|
+
id: admittedPromptEventId(admission.runId, admission.userMessageId),
|
|
194
|
+
sessionId: admission.sessionId,
|
|
195
|
+
invocationId: run.invocationId,
|
|
196
|
+
runId: run.runId,
|
|
197
|
+
turnId: admission.turnId,
|
|
198
|
+
ts: admission.admittedAt,
|
|
199
|
+
partial: false,
|
|
200
|
+
role: 'user',
|
|
201
|
+
author: origin ? 'host' : 'user',
|
|
202
|
+
content: { kind: 'text', ...content, ...(origin ? { origin } : {}) },
|
|
203
|
+
};
|
|
204
|
+
if (!ledgerHasMessage) {
|
|
205
|
+
await stores.runtimeEventStore.appendRuntimeEvent(admission.sessionId, run.runId, event);
|
|
206
|
+
}
|
|
207
|
+
// The Turn never reached the commit that carries these, and no later path
|
|
208
|
+
// recomputes them: the connection lock is one-way and the preview is a write.
|
|
209
|
+
// Committed even when the ledger already holds the message, because the two
|
|
210
|
+
// are separate writes and a crash between them leaves exactly that state.
|
|
211
|
+
const message = projectRuntimeEventUserMessage(event, event.id);
|
|
212
|
+
if (message) {
|
|
213
|
+
await stores.sessionStore.commitMessageCatalogProjection(admission.sessionId, message);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
158
216
|
export function requireHostedExecutionMessageContent(admission) {
|
|
159
217
|
if (admission.normalizedInput === null) {
|
|
160
218
|
throw new RuntimeMessageAuthorityInvariantError(`Admitted Turn ${admission.turnId} has no message input`);
|
|
@@ -186,28 +244,46 @@ export function hostedExecutionMessageOrigin(execution) {
|
|
|
186
244
|
return undefined;
|
|
187
245
|
}
|
|
188
246
|
}
|
|
189
|
-
|
|
247
|
+
/**
|
|
248
|
+
* Whether the ledger already carries this admission's message, throwing when
|
|
249
|
+
* what it carries contradicts the admission.
|
|
250
|
+
*/
|
|
251
|
+
function verifyUserMessage(admission, rootUserMessages, messageIdOwner) {
|
|
190
252
|
if (rootUserMessages.length > 1) {
|
|
191
253
|
throw new Error(`Admitted Turn ${admission.turnId} has multiple UserMessages`);
|
|
192
254
|
}
|
|
193
255
|
const userMessage = rootUserMessages[0];
|
|
194
256
|
if (userMessage) {
|
|
195
|
-
if (messageIdOwner !== userMessage ||
|
|
196
|
-
userMessage.id !== admission.userMessageId ||
|
|
257
|
+
if ((messageIdOwner !== undefined && messageIdOwner !== userMessage) ||
|
|
197
258
|
!recoveryUserMessageOriginMatches(userMessage, admission.execution) ||
|
|
198
259
|
!messageContentsEqual(normalizeMessageContent(userMessage), requireHostedExecutionMessageContent(admission))) {
|
|
199
260
|
throw new Error(`Admitted Turn ${admission.turnId} does not match its UserMessage`);
|
|
200
261
|
}
|
|
201
|
-
return;
|
|
262
|
+
return true;
|
|
202
263
|
}
|
|
203
264
|
if (messageIdOwner) {
|
|
204
265
|
throw new Error(`Admitted Turn ${admission.turnId} reuses another message identity`);
|
|
205
266
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* The prompts a Session's ledger holds, as the transcript presents them.
|
|
271
|
+
*
|
|
272
|
+
* Recovery reads the raw events rather than the read model: a Session it is
|
|
273
|
+
* about to repair may be exactly the one whose projection is still incomplete.
|
|
274
|
+
* Steering is excluded — it is typed as a user message but is something said
|
|
275
|
+
* into a Turn that was already admitted, so it is never the Turn's own prompt.
|
|
276
|
+
*/
|
|
277
|
+
function recoveryUserMessagesFromLedger(events) {
|
|
278
|
+
const messages = [];
|
|
279
|
+
for (const event of events) {
|
|
280
|
+
if (event.role !== 'user' || event.content?.kind !== 'text' || event.partial)
|
|
281
|
+
continue;
|
|
282
|
+
const projected = projectRuntimeEventUserMessage(event, event.id);
|
|
283
|
+
if (projected && projected.steeringEventId === undefined)
|
|
284
|
+
messages.push(projected);
|
|
285
|
+
}
|
|
286
|
+
return messages;
|
|
211
287
|
}
|
|
212
288
|
async function verifyQueueSourceMessages(admission, index, proofReader) {
|
|
213
289
|
for (const source of admission.sourceMessages) {
|
|
@@ -232,20 +308,6 @@ async function verifyQueueSourceMessages(admission, index, proofReader) {
|
|
|
232
308
|
}
|
|
233
309
|
}
|
|
234
310
|
}
|
|
235
|
-
function recoveryUserMessage(admission) {
|
|
236
|
-
if (!admission.userMessageId || !admission.normalizedInput) {
|
|
237
|
-
throw new Error(`Admitted Turn ${admission.turnId} does not own a UserMessage`);
|
|
238
|
-
}
|
|
239
|
-
const origin = hostedExecutionMessageOrigin(admission.execution);
|
|
240
|
-
return {
|
|
241
|
-
type: 'user',
|
|
242
|
-
id: admission.userMessageId,
|
|
243
|
-
turnId: admission.turnId,
|
|
244
|
-
ts: admission.admittedAt,
|
|
245
|
-
...normalizeMessageContent(admission.normalizedInput),
|
|
246
|
-
...(origin ? { origin } : {}),
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
311
|
function recoveryUserMessageOriginMatches(message, execution) {
|
|
250
312
|
const expected = hostedExecutionMessageOrigin(execution);
|
|
251
313
|
return expected === undefined || isDeepStrictEqual(message.origin, expected);
|
|
@@ -246,11 +246,16 @@ export class HostMessageCoordinator {
|
|
|
246
246
|
}
|
|
247
247
|
bindRun(identity) {
|
|
248
248
|
const state = this.#state(identity.sessionId);
|
|
249
|
-
const exactPreStartStop = state.stopFence !== undefined &&
|
|
249
|
+
const exactPreStartStop = state.stopFence !== undefined &&
|
|
250
|
+
state.reservedRoot !== undefined &&
|
|
251
|
+
sameRun(state.stopFence.identity, state.reservedRoot);
|
|
250
252
|
if (state.phase !== 'open' && !exactPreStartStop) {
|
|
251
253
|
throw new RuntimeMessageAuthorityInvariantError('Message Run bound while admission was closed');
|
|
252
254
|
}
|
|
253
|
-
if (!state.reservedRoot ||
|
|
255
|
+
if (!state.reservedRoot ||
|
|
256
|
+
state.reservedRoot.turnId !== identity.turnId ||
|
|
257
|
+
state.reservedRoot.expectedRunId !== identity.runId ||
|
|
258
|
+
state.run) {
|
|
254
259
|
throw new RuntimeMessageAuthorityInvariantError(`Message Run ${identity.runId} was not the exact reserved root identity`);
|
|
255
260
|
}
|
|
256
261
|
const run = { ...identity, generation: state.generation, released: false };
|
|
@@ -273,9 +278,66 @@ export class HostMessageCoordinator {
|
|
|
273
278
|
if (state.run || state.transition) {
|
|
274
279
|
throw new RuntimeMessageAuthorityInvariantError('Cannot reserve a root Turn during live ownership');
|
|
275
280
|
}
|
|
276
|
-
state.reservedRoot = { ...identity };
|
|
281
|
+
state.reservedRoot = { ...identity, expectedRunId: identity.runId };
|
|
277
282
|
state.phase = 'open';
|
|
278
283
|
}
|
|
284
|
+
/** Change physical ownership, not the logical queue generation or Stop identity. */
|
|
285
|
+
async advanceHandoffRun(identity, successorRunId, admission) {
|
|
286
|
+
await this.#sessionAdmission.runAdmitted(identity.sessionId, admission, async () => {
|
|
287
|
+
const logical = await this.#durableProof.readLogicalExecution(identity);
|
|
288
|
+
const state = this.#requireState(identity.sessionId);
|
|
289
|
+
if (!logical?.pendingHandoff ||
|
|
290
|
+
logical.pendingHandoff.successorRunId !== successorRunId ||
|
|
291
|
+
!state.reservedRoot ||
|
|
292
|
+
!sameRun(state.reservedRoot, identity) ||
|
|
293
|
+
state.transition ||
|
|
294
|
+
(state.phase !== 'open' && !state.stopFence) ||
|
|
295
|
+
state.inFlight.size !== 0 ||
|
|
296
|
+
(state.run
|
|
297
|
+
? !state.run.released || state.run.runId !== logical.tip.runId
|
|
298
|
+
: state.reservedRoot.expectedRunId !== identity.runId)) {
|
|
299
|
+
throw new RuntimeMessageAuthorityInvariantError('Physical handoff lacks a sealed released root owner');
|
|
300
|
+
}
|
|
301
|
+
state.run = undefined;
|
|
302
|
+
state.reservedRoot.expectedRunId = successorRunId;
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
async #readDetachableHandoff(identity) {
|
|
306
|
+
const logical = await this.#durableProof.readLogicalExecution(identity);
|
|
307
|
+
const state = this.#requireState(identity.sessionId);
|
|
308
|
+
if (state.stopFence || state.pendingInterrupts.size !== 0)
|
|
309
|
+
return undefined;
|
|
310
|
+
if (!logical?.pendingHandoff ||
|
|
311
|
+
!state.reservedRoot ||
|
|
312
|
+
!sameRun(state.reservedRoot, identity) ||
|
|
313
|
+
!state.run?.released ||
|
|
314
|
+
state.run.runId !== logical.tip.runId ||
|
|
315
|
+
state.inFlight.size !== 0 ||
|
|
316
|
+
state.transition) {
|
|
317
|
+
throw new RuntimeMessageAuthorityInvariantError('Handoff cannot detach unsettled Message ownership');
|
|
318
|
+
}
|
|
319
|
+
return state;
|
|
320
|
+
}
|
|
321
|
+
async handoffResidencies(identity, admission) {
|
|
322
|
+
return this.#sessionAdmission.runAdmitted(identity.sessionId, admission, async () => {
|
|
323
|
+
const state = await this.#readDetachableHandoff(identity);
|
|
324
|
+
return state && allLiveEntries(state).map((entry) => entry.residency);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
async detachHandoffRoot(identity, admission) {
|
|
328
|
+
await this.#sessionAdmission.runAdmitted(identity.sessionId, admission, async () => {
|
|
329
|
+
const state = await this.#readDetachableHandoff(identity);
|
|
330
|
+
if (!state)
|
|
331
|
+
throw new RuntimeMessageAuthorityInvariantError('Stop took ownership before handoff detach');
|
|
332
|
+
// Confirmed admissions remain durable for the next Host. Only local leases
|
|
333
|
+
// and handles retire; do not write cancellation receipts or a queue fence.
|
|
334
|
+
this.#retractQueued(state);
|
|
335
|
+
state.run = undefined;
|
|
336
|
+
state.reservedRoot = undefined;
|
|
337
|
+
state.phase = 'closed';
|
|
338
|
+
this.#maybeReclaim(identity.sessionId, state);
|
|
339
|
+
});
|
|
340
|
+
}
|
|
279
341
|
abandonRootReservation(identity) {
|
|
280
342
|
const state = this.#requireState(identity.sessionId);
|
|
281
343
|
if (!state.reservedRoot || !sameRun(state.reservedRoot, identity) || state.run) {
|
|
@@ -296,7 +358,8 @@ export class HostMessageCoordinator {
|
|
|
296
358
|
if (!state.reservedRoot ||
|
|
297
359
|
!sameRun(state.reservedRoot, identity) ||
|
|
298
360
|
!run ||
|
|
299
|
-
|
|
361
|
+
run.turnId !== identity.turnId ||
|
|
362
|
+
run.runId !== state.reservedRoot.expectedRunId ||
|
|
300
363
|
!run.released) {
|
|
301
364
|
throw new RuntimeMessageAuthorityInvariantError('Terminal transition requires a released exact root owner');
|
|
302
365
|
}
|
|
@@ -344,7 +407,7 @@ export class HostMessageCoordinator {
|
|
|
344
407
|
state.generation += 1;
|
|
345
408
|
for (const entry of allLiveEntries(state))
|
|
346
409
|
entry.generation = state.generation;
|
|
347
|
-
state.reservedRoot = { ...identity };
|
|
410
|
+
state.reservedRoot = { ...identity, expectedRunId: identity.runId };
|
|
348
411
|
state.phase = 'open';
|
|
349
412
|
this.#mutated(state);
|
|
350
413
|
}
|
|
@@ -387,6 +450,7 @@ export class HostMessageCoordinator {
|
|
|
387
450
|
const provenRootMessages = [];
|
|
388
451
|
const provenSteeringMessages = [];
|
|
389
452
|
const admissions = await this.#admissions.listMessageAdmissions(input.sessionId);
|
|
453
|
+
let logicalRunIds;
|
|
390
454
|
for (const messageId of new Set(input.messageIds)) {
|
|
391
455
|
messageIds.add(messageId);
|
|
392
456
|
provenRootMessages.push(await this.#readProvenRootMessage(input, messageId));
|
|
@@ -396,7 +460,13 @@ export class HostMessageCoordinator {
|
|
|
396
460
|
continue;
|
|
397
461
|
}
|
|
398
462
|
const proof = await this.#durableProof.readImmutableSteeringMessageProof(input.sessionId, admission.messageId);
|
|
399
|
-
if (proof?.event.turnId === input.turnId &&
|
|
463
|
+
if (proof?.event.turnId === input.turnId &&
|
|
464
|
+
proof.event.runId !== input.runId &&
|
|
465
|
+
!logicalRunIds) {
|
|
466
|
+
logicalRunIds = (await this.#durableProof.readLogicalExecution(input))?.runIds ?? [];
|
|
467
|
+
}
|
|
468
|
+
if (proof?.event.turnId === input.turnId &&
|
|
469
|
+
(proof.event.runId === input.runId || logicalRunIds?.includes(proof.event.runId))) {
|
|
400
470
|
messageIds.add(admission.messageId);
|
|
401
471
|
provenSteeringMessages.push({
|
|
402
472
|
messageId: admission.messageId,
|