maka-agent 0.2.0-dev.22.20260905 → 0.2.0-dev.24.20260907
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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-peer/prebuilds/darwin-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-arm64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/linux-x64/maka_runtime_host_peer.node +0 -0
- package/native/runtime-host-peer/prebuilds/win32-x64/maka_runtime_host_peer.node +0 -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/bot-events.js +18 -63
- package/node_modules/@maka/core/dist/canonical-runtime-event.js +3 -1
- package/node_modules/@maka/core/dist/model-call-attempt.js +0 -82
- package/node_modules/@maka/core/dist/model-call-usage-projection.js +0 -186
- 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 +22 -2
- package/node_modules/@maka/core/dist/redaction.js +41 -58
- 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-trace.js +5 -1
- 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-ledger-merge.js +7 -9
- package/node_modules/@maka/core/dist/usage-record-schema.js +26 -35
- package/node_modules/@maka/core/dist/workhub-creation-intent.js +34 -20
- package/node_modules/@maka/core/package.json +4 -0
- package/node_modules/@maka/eval/harbor/egress-proxy/entrypoint.sh +2 -1
- 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 +155 -134
- package/node_modules/@maka/runtime/dist/bots/base-adapter.js +17 -0
- package/node_modules/@maka/runtime/dist/bots/bot-test.js +38 -22
- package/node_modules/@maka/runtime/dist/bots/dingtalk-bridge.js +7 -5
- package/node_modules/@maka/runtime/dist/bots/discord-bridge.js +4 -4
- package/node_modules/@maka/runtime/dist/bots/feishu-bridge.js +4 -5
- package/node_modules/@maka/runtime/dist/bots/qq-bridge.js +6 -4
- package/node_modules/@maka/runtime/dist/bots/slack-bridge.js +3 -4
- package/node_modules/@maka/runtime/dist/bots/telegram-bridge.js +4 -5
- package/node_modules/@maka/runtime/dist/bots/wechat-bridge.js +17 -19
- package/node_modules/@maka/runtime/dist/bots/wecom-bridge.js +5 -6
- package/node_modules/@maka/runtime/dist/bots/ws-bridge-base.js +2 -3
- 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/session-trace-projection.js +3 -30
- 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/candidate-entry.js +4 -1
- 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 +3 -1
- package/node_modules/@maka/runtime-host/dist/client/launcher.js +3 -0
- package/node_modules/@maka/runtime-host/dist/client/peer-client.js +13 -0
- package/node_modules/@maka/runtime-host/dist/client/registered-host-termination.js +0 -59
- 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/peer-mesh/node.js +28 -16
- package/node_modules/@maka/runtime-host/dist/protocol/external-session.js +4 -0
- package/node_modules/@maka/runtime-host/dist/protocol/host-status.js +29 -12
- package/node_modules/@maka/runtime-host/dist/protocol/index.js +37 -1
- package/node_modules/@maka/runtime-host/dist/protocol/operations.js +2 -0
- package/node_modules/@maka/runtime-host/dist/protocol/session-collaboration.js +28 -1
- 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/protocol/workhub-coordination.js +33 -0
- package/node_modules/@maka/runtime-host/dist/server/access-authority.js +26 -1
- package/node_modules/@maka/runtime-host/dist/server/access-credential-store.js +6 -1
- package/node_modules/@maka/runtime-host/dist/server/canonical-turn-snapshot.js +19 -4
- package/node_modules/@maka/runtime-host/dist/server/canonical-usage-reader.js +24 -11
- package/node_modules/@maka/runtime-host/dist/server/connection-session.js +7 -6
- package/node_modules/@maka/runtime-host/dist/server/daily-review-coordinator.js +7 -4
- package/node_modules/@maka/runtime-host/dist/server/execution-composition.js +168 -28
- 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/external-session-coordinator.js +6 -3
- package/node_modules/@maka/runtime-host/dist/server/goal-coordinator.js +40 -1
- package/node_modules/@maka/runtime-host/dist/server/host-kernel.js +108 -23
- 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 +85 -8
- package/node_modules/@maka/runtime-host/dist/server/root-admission-owner.js +7 -0
- package/node_modules/@maka/runtime-host/dist/server/root-turn-coordinator.js +344 -71
- 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 +201 -12
- 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/usage-pricing-coordinator.js +4 -11
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-action-gate.js +44 -15
- package/node_modules/@maka/runtime-host/dist/server/workhub-coordination-coordinator.js +6 -2
- package/node_modules/@maka/storage/dist/agent-run-store.js +44 -0
- 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/conversation-operational-state.js +4 -0
- package/node_modules/@maka/storage/dist/execution-stores.js +11 -7
- package/node_modules/@maka/storage/dist/file-session-repository.js +1012 -0
- package/node_modules/@maka/storage/dist/model-call-ledger.js +199 -32
- package/node_modules/@maka/storage/dist/model-call-usage-sql.js +136 -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/operational-state-store-public.js +1 -1
- package/node_modules/@maka/storage/dist/operational-state-store.js +5 -3
- 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 +17 -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/sqlite-usage-schema.js +200 -20
- package/node_modules/@maka/storage/dist/tool-result-archive-evidence.js +111 -0
- package/node_modules/@maka/storage/dist/usage-stores.js +6 -2
- package/node_modules/@maka/storage/package.json +2 -0
- package/package.json +1 -1
|
@@ -25,7 +25,7 @@ import { removeHostRegistration, writeHostRegistration } from '../control/regist
|
|
|
25
25
|
import { decodeClientFrame, encodeProtocolMessage, HOST_OPERATION_SPECS, negotiateProtocol, RUNTIME_HOST_COMPATIBILITY_EPOCH, RUNTIME_HOST_PROTOCOL_VERSION, RUNTIME_HOST_REGISTRATION_SCHEMA_VERSION, requireHostGeneration, } from '../protocol/index.js';
|
|
26
26
|
import { RuntimeHostConnectionSession, } from './connection-session.js';
|
|
27
27
|
import { composeOperationHandlers, createUnavailableDomainOperationHandlers, } from './operation-dispatcher.js';
|
|
28
|
-
import { issueAccessCredential, acknowledgeCollaborationTurnRequest, createCollaborationTurnRequest, decideCollaborationTurnRequest, withdrawCollaborationTurnRequest, finalizeAccessCredential, prepareCollaborationInvitation, queryCollaborationTurnRequests, prepareAccessCredential, prepareAccessCredentialRotation, replaceAccessCredential, revokeAccessCredential, revokeAccessPrincipal, revokeAccessCredentialRotation, revokeCollaborationGrant, revokeCollaborationPrincipal, } from './access-authority.js';
|
|
28
|
+
import { issueAccessCredential, acknowledgeCollaborationTurnRequest, createCollaborationTurnRequest, decideCollaborationTurnRequest, withdrawCollaborationTurnRequest, finalizeAccessCredential, prepareCollaborationInvitation, queryCollaborationTurnRequests, prepareAccessCredential, prepareAccessCredentialRotation, replaceAccessCredential, revokeAccessCredential, revokeAccessPrincipal, revokeAccessCredentialRotation, revokeCollaborationGrant, revokeCollaborationPrincipal, renameCollaborationPrincipal, } from './access-authority.js';
|
|
29
29
|
import { runtimeHostLogBuffer } from '../process-diagnostics.js';
|
|
30
30
|
import { startLocalRuntimeHostListenerSet, } from './listener-set.js';
|
|
31
31
|
import { HostResidencyRegistry } from './host-residency-registry.js';
|
|
@@ -70,6 +70,7 @@ export class RuntimeHostKernel {
|
|
|
70
70
|
#hasAcceptedConnection = false;
|
|
71
71
|
#activeOperations = 0;
|
|
72
72
|
#activeCommandOperations = 0;
|
|
73
|
+
#handoff;
|
|
73
74
|
#retainedUntilProcessExit = false;
|
|
74
75
|
#composition;
|
|
75
76
|
#compositionDrainBegun = false;
|
|
@@ -164,7 +165,8 @@ export class RuntimeHostKernel {
|
|
|
164
165
|
get compositionDescriptor() {
|
|
165
166
|
return this.#options.composition.descriptor;
|
|
166
167
|
}
|
|
167
|
-
close() {
|
|
168
|
+
close(input) {
|
|
169
|
+
this.#shutdownReason ??= input?.reason;
|
|
168
170
|
this.#requestDrain();
|
|
169
171
|
return this.closed;
|
|
170
172
|
}
|
|
@@ -230,6 +232,8 @@ export class RuntimeHostKernel {
|
|
|
230
232
|
}
|
|
231
233
|
this.#state = 'ready';
|
|
232
234
|
await this.#publishRegistration();
|
|
235
|
+
if (!this.#shutdownRequested)
|
|
236
|
+
this.#composition?.startMaintenance?.();
|
|
233
237
|
this.#scheduleIdleIfNeeded();
|
|
234
238
|
}
|
|
235
239
|
#accept(connection) {
|
|
@@ -305,7 +309,7 @@ export class RuntimeHostKernel {
|
|
|
305
309
|
}
|
|
306
310
|
async #admitHandshake(hello, transport, authority) {
|
|
307
311
|
const admittedState = await this.#readAdmissionState();
|
|
308
|
-
if (!admittedState) {
|
|
312
|
+
if (!admittedState || this.#handoff) {
|
|
309
313
|
return {
|
|
310
314
|
kind: 'draining',
|
|
311
315
|
hostEpoch: this.hostEpoch,
|
|
@@ -336,7 +340,13 @@ export class RuntimeHostKernel {
|
|
|
336
340
|
hello.generation !== undefined &&
|
|
337
341
|
hello.generation !== this.#options.generation;
|
|
338
342
|
if (generationMismatch && hello.takeover?.expectedHostEpoch === this.hostEpoch) {
|
|
339
|
-
|
|
343
|
+
const residencyCount = hello.activitySnapshotVersion === 2
|
|
344
|
+
? this.#residencies.drainCount
|
|
345
|
+
: this.#residencies.activeCount;
|
|
346
|
+
if (authority.principalKind === 'local_owner' &&
|
|
347
|
+
residencyCount === 0 &&
|
|
348
|
+
this.#hasNoObservedWork(transport)) {
|
|
349
|
+
this.#shutdownReason = 'retirement';
|
|
340
350
|
this.#requestDrain();
|
|
341
351
|
return {
|
|
342
352
|
kind: 'draining',
|
|
@@ -363,8 +373,9 @@ export class RuntimeHostKernel {
|
|
|
363
373
|
replacement: this.#lifecycle.kind === 'ephemeral' && this.#isSettledForReplacementAdvice()
|
|
364
374
|
? 'wait_for_idle_exit'
|
|
365
375
|
: 'blocked_by_residency',
|
|
366
|
-
...(
|
|
367
|
-
|
|
376
|
+
...(authority.principalKind === 'local_owner' &&
|
|
377
|
+
(generationMismatch || hello.activitySnapshotVersion === 2)
|
|
378
|
+
? { activity: this.#activitySnapshot(hello.activitySnapshotVersion) }
|
|
368
379
|
: {}),
|
|
369
380
|
};
|
|
370
381
|
}
|
|
@@ -375,6 +386,9 @@ export class RuntimeHostKernel {
|
|
|
375
386
|
this.#cancelIdle();
|
|
376
387
|
return {
|
|
377
388
|
kind: 'accepted',
|
|
389
|
+
...(hello.activitySnapshotVersion === 2 && this.#composition?.prepareHandoff
|
|
390
|
+
? { cooperativeHandoff: true }
|
|
391
|
+
: {}),
|
|
378
392
|
rootId: this.#options.owner.capability.rootId,
|
|
379
393
|
hostEpoch: this.hostEpoch,
|
|
380
394
|
connectionId: randomUUID(),
|
|
@@ -400,6 +414,10 @@ export class RuntimeHostKernel {
|
|
|
400
414
|
async #beginOperation(frame) {
|
|
401
415
|
if (!(await this.#readAdmissionState()))
|
|
402
416
|
return 'host_draining';
|
|
417
|
+
if (this.#handoff &&
|
|
418
|
+
HOST_OPERATION_SPECS[frame.operation].mode === 'command' &&
|
|
419
|
+
(this.#handoff.commandsClosed || frame.operation !== 'turn.stop'))
|
|
420
|
+
return 'host_draining';
|
|
403
421
|
if (HOST_OPERATION_SPECS[frame.operation].availability !== 'bootstrap' &&
|
|
404
422
|
this.#state !== 'ready') {
|
|
405
423
|
return 'host_not_ready';
|
|
@@ -474,14 +492,9 @@ export class RuntimeHostKernel {
|
|
|
474
492
|
this.#settleLifecycleAfterWork();
|
|
475
493
|
}
|
|
476
494
|
#acquireResidency(label) {
|
|
477
|
-
const residency = this.#residencies.acquire(label);
|
|
495
|
+
const residency = this.#residencies.acquire(label, 'drain', () => this.#settleLifecycleAfterWork());
|
|
478
496
|
this.#cancelIdle();
|
|
479
|
-
return
|
|
480
|
-
release: () => {
|
|
481
|
-
residency.release();
|
|
482
|
-
this.#settleLifecycleAfterWork();
|
|
483
|
-
},
|
|
484
|
-
};
|
|
497
|
+
return residency;
|
|
485
498
|
}
|
|
486
499
|
#retainUntilProcessExit() {
|
|
487
500
|
if (this.#retainedUntilProcessExit)
|
|
@@ -502,6 +515,7 @@ export class RuntimeHostKernel {
|
|
|
502
515
|
ok: true,
|
|
503
516
|
result: {
|
|
504
517
|
...this.#statusSnapshot(),
|
|
518
|
+
upgradeBlockingActivity: this.#hasUpgradeBlockingActivity(0),
|
|
505
519
|
compositionModules: this.#composition?.moduleIds ?? [],
|
|
506
520
|
residencies: this.#residencies.snapshot(),
|
|
507
521
|
protocolVersion: RUNTIME_HOST_PROTOCOL_VERSION,
|
|
@@ -521,7 +535,7 @@ export class RuntimeHostKernel {
|
|
|
521
535
|
ok: true,
|
|
522
536
|
result: await this.#resourceCollector.snapshot(this.hostEpoch),
|
|
523
537
|
}),
|
|
524
|
-
'host.upgrade.prepare': async (input) => {
|
|
538
|
+
'host.upgrade.prepare': async (input, context) => {
|
|
525
539
|
if (input.expectedHostEpoch !== this.hostEpoch) {
|
|
526
540
|
return {
|
|
527
541
|
ok: false,
|
|
@@ -531,8 +545,11 @@ export class RuntimeHostKernel {
|
|
|
531
545
|
},
|
|
532
546
|
};
|
|
533
547
|
}
|
|
534
|
-
if (!input.allowInterruptActiveTasks && this.#hasUpgradeBlockingActivity()) {
|
|
535
|
-
|
|
548
|
+
if (!input.allowInterruptActiveTasks && this.#hasUpgradeBlockingActivity(1)) {
|
|
549
|
+
if (!input.allowCooperativeHandoff ||
|
|
550
|
+
!(await this.#prepareCooperativeHandoff(context.inputClosedSignal))) {
|
|
551
|
+
return { ok: true, result: { kind: 'active_tasks' } };
|
|
552
|
+
}
|
|
536
553
|
}
|
|
537
554
|
this.#shutdownReason = 'retirement';
|
|
538
555
|
this.#requestDrain();
|
|
@@ -560,6 +577,7 @@ export class RuntimeHostKernel {
|
|
|
560
577
|
},
|
|
561
578
|
},
|
|
562
579
|
'collaboration.grant.revoke': async (input) => this.#settleAccessCredentialMutation(revokeCollaborationGrant(this.#options.accessAuthority, input)),
|
|
580
|
+
'collaboration.principal.rename': async (input) => renameCollaborationPrincipal(this.#options.accessAuthority, input),
|
|
563
581
|
'collaboration.principal.revoke': async (input) => this.#settleAccessCredentialMutation(revokeCollaborationPrincipal(this.#options.accessAuthority, input.principalId)),
|
|
564
582
|
'collaboration.turn-request.create': async (input, context) => this.#settleAccessCredentialMutation(createCollaborationTurnRequest(this.#options.accessAuthority, context.principal, input)),
|
|
565
583
|
'collaboration.turn-request.query': async (input, context) => queryCollaborationTurnRequests(this.#options.accessAuthority, {
|
|
@@ -597,24 +615,89 @@ export class RuntimeHostKernel {
|
|
|
597
615
|
: {}),
|
|
598
616
|
};
|
|
599
617
|
}
|
|
600
|
-
#activitySnapshot() {
|
|
618
|
+
#activitySnapshot(version) {
|
|
601
619
|
return {
|
|
602
620
|
connections: this.#acceptedTransports.size,
|
|
603
621
|
activeOperations: this.#activeOperations,
|
|
604
622
|
processUptimeSeconds: Math.max(0, Math.floor(process.uptime())),
|
|
605
623
|
residencies: this.#residencies.snapshot(),
|
|
624
|
+
...(version === 2
|
|
625
|
+
? {
|
|
626
|
+
drainResidencies: this.#residencies.drainCount,
|
|
627
|
+
...(this.#composition?.prepareHandoff ? { cooperativeHandoff: true } : {}),
|
|
628
|
+
}
|
|
629
|
+
: {}),
|
|
606
630
|
};
|
|
607
631
|
}
|
|
608
|
-
#hasUpgradeBlockingActivity() {
|
|
632
|
+
#hasUpgradeBlockingActivity(selfCommands) {
|
|
609
633
|
// The request's own accepted transport is expected. Any other live
|
|
610
634
|
// connection arrived after discovery or remained attached and therefore
|
|
611
|
-
// requires explicit interruption authority before retirement.
|
|
635
|
+
// requires explicit interruption authority before retirement. Callers
|
|
636
|
+
// pass how many of the in-flight commands are their own: the
|
|
637
|
+
// `host.upgrade.prepare` command counts itself, while the diagnostics
|
|
638
|
+
// query path runs outside the command counter.
|
|
612
639
|
if (this.#acceptedTransports.size > 1)
|
|
613
640
|
return true;
|
|
614
|
-
if (this.#activeCommandOperations >
|
|
641
|
+
if (this.#activeCommandOperations > selfCommands)
|
|
615
642
|
return true;
|
|
616
643
|
return this.#residencies.drainCount > 0;
|
|
617
644
|
}
|
|
645
|
+
async #prepareCooperativeHandoff(inputClosedSignal) {
|
|
646
|
+
if (!inputClosedSignal ||
|
|
647
|
+
inputClosedSignal.aborted ||
|
|
648
|
+
!this.#composition?.prepareHandoff ||
|
|
649
|
+
this.#handoff ||
|
|
650
|
+
this.#acceptedTransports.size > 1 ||
|
|
651
|
+
this.#activeCommandOperations > 1)
|
|
652
|
+
return false;
|
|
653
|
+
const handoff = { abort: new AbortController(), commandsClosed: false, committing: false };
|
|
654
|
+
this.#handoff = handoff;
|
|
655
|
+
const cancelOnDisconnect = () => {
|
|
656
|
+
if (!handoff.committing)
|
|
657
|
+
handoff.abort.abort();
|
|
658
|
+
};
|
|
659
|
+
inputClosedSignal.addEventListener('abort', cancelOnDisconnect, { once: true });
|
|
660
|
+
const timer = setTimeout(() => handoff.abort.abort(), 10_000);
|
|
661
|
+
let prepared;
|
|
662
|
+
try {
|
|
663
|
+
prepared = await this.#composition.prepareHandoff(this.hostEpoch, handoff.abort.signal);
|
|
664
|
+
if (!prepared || handoff.abort.signal.aborted || this.#activeCommandOperations !== 1)
|
|
665
|
+
return false;
|
|
666
|
+
handoff.commandsClosed = true;
|
|
667
|
+
if (!(await prepared.seal()))
|
|
668
|
+
return false;
|
|
669
|
+
const residencies = await prepared.residencies();
|
|
670
|
+
if (!residencies ||
|
|
671
|
+
handoff.abort.signal.aborted ||
|
|
672
|
+
!(await this.#hasLiveOwnerOrDrain()) ||
|
|
673
|
+
handoff.abort.signal.aborted ||
|
|
674
|
+
this.#handoff !== handoff ||
|
|
675
|
+
this.#acceptedTransports.size !== 1 ||
|
|
676
|
+
this.#activeCommandOperations !== 1 ||
|
|
677
|
+
this.#residencies.hasDrainResidenciesExcept(residencies))
|
|
678
|
+
return false;
|
|
679
|
+
// No await between the final exact-handle proof and closing all command
|
|
680
|
+
// admission. After this cut cancellation must not restart the old runs.
|
|
681
|
+
handoff.committing = true;
|
|
682
|
+
clearTimeout(timer);
|
|
683
|
+
await prepared.detach();
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
finally {
|
|
687
|
+
clearTimeout(timer);
|
|
688
|
+
inputClosedSignal.removeEventListener('abort', cancelOnDisconnect);
|
|
689
|
+
if (handoff.committing) {
|
|
690
|
+
this.#shutdownReason = 'retirement';
|
|
691
|
+
this.#requestDrain();
|
|
692
|
+
}
|
|
693
|
+
else {
|
|
694
|
+
handoff.abort.abort();
|
|
695
|
+
prepared?.cancel();
|
|
696
|
+
}
|
|
697
|
+
if (this.#handoff === handoff)
|
|
698
|
+
this.#handoff = undefined;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
618
701
|
#beginCompositionDrain() {
|
|
619
702
|
if (!this.#composition || this.#compositionDrainBegun)
|
|
620
703
|
return;
|
|
@@ -653,7 +736,10 @@ export class RuntimeHostKernel {
|
|
|
653
736
|
void this.#commitShutdown().catch(() => undefined);
|
|
654
737
|
}, this.#lifecycle.idleGraceMs);
|
|
655
738
|
}
|
|
656
|
-
#isTrueIdle(
|
|
739
|
+
#isTrueIdle() {
|
|
740
|
+
return this.#residencies.activeCount === 0 && this.#hasNoObservedWork();
|
|
741
|
+
}
|
|
742
|
+
#hasNoObservedWork(exceptHandshaking) {
|
|
657
743
|
// A transport mid-handshake keeps the Host busy, except the one whose
|
|
658
744
|
// admission is being decided right now: counting it would make every
|
|
659
745
|
// true-idle takeover observe itself as activity.
|
|
@@ -663,8 +749,7 @@ export class RuntimeHostKernel {
|
|
|
663
749
|
return (this.#state === 'ready' &&
|
|
664
750
|
this.#acceptedTransports.size === 0 &&
|
|
665
751
|
handshaking === 0 &&
|
|
666
|
-
this.#activeOperations === 0
|
|
667
|
-
this.#residencies.activeCount === 0);
|
|
752
|
+
this.#activeOperations === 0);
|
|
668
753
|
}
|
|
669
754
|
// The replacement advice in a rejection is what a stale Client acts on.
|
|
670
755
|
// In-flight handshakes resolve within milliseconds and must not flip that
|
|
@@ -21,34 +21,41 @@ export class HostResidencyRegistry {
|
|
|
21
21
|
#counts = new Map();
|
|
22
22
|
#drainWaiters = new Set();
|
|
23
23
|
#activeCount = 0;
|
|
24
|
-
#
|
|
24
|
+
#drainHandles = new Set();
|
|
25
25
|
/** Residencies of either kind keep the process alive against idle exit. */
|
|
26
26
|
get activeCount() {
|
|
27
27
|
return this.#activeCount;
|
|
28
28
|
}
|
|
29
29
|
/** Only drain-kind residencies block maintenance probes and graceful close. */
|
|
30
30
|
get drainCount() {
|
|
31
|
-
return this.#
|
|
31
|
+
return this.#drainHandles.size;
|
|
32
32
|
}
|
|
33
|
-
|
|
33
|
+
hasDrainResidenciesExcept(allowed) {
|
|
34
|
+
const proven = new Set(allowed);
|
|
35
|
+
return [...this.#drainHandles].some((handle) => !proven.has(handle));
|
|
36
|
+
}
|
|
37
|
+
acquire(label, kind = 'drain', onRelease) {
|
|
34
38
|
requireResidencyLabel(label);
|
|
35
39
|
this.#activeCount += 1;
|
|
36
|
-
if (kind === 'drain')
|
|
37
|
-
this.#drainCount += 1;
|
|
38
40
|
const counts = this.#counts.get(label) ?? { total: 0, drain: 0 };
|
|
39
41
|
counts.total += 1;
|
|
40
42
|
if (kind === 'drain')
|
|
41
43
|
counts.drain += 1;
|
|
42
44
|
this.#counts.set(label, counts);
|
|
43
45
|
let active = true;
|
|
44
|
-
|
|
46
|
+
const handle = {
|
|
45
47
|
release: () => {
|
|
46
48
|
if (!active)
|
|
47
49
|
return;
|
|
48
50
|
active = false;
|
|
51
|
+
this.#drainHandles.delete(handle);
|
|
49
52
|
this.#release(label, kind);
|
|
53
|
+
onRelease?.();
|
|
50
54
|
},
|
|
51
55
|
};
|
|
56
|
+
if (kind === 'drain')
|
|
57
|
+
this.#drainHandles.add(handle);
|
|
58
|
+
return handle;
|
|
52
59
|
}
|
|
53
60
|
snapshot() {
|
|
54
61
|
return [...this.#counts]
|
|
@@ -70,7 +77,6 @@ export class HostResidencyRegistry {
|
|
|
70
77
|
counts.total -= 1;
|
|
71
78
|
if (kind === 'drain') {
|
|
72
79
|
counts.drain -= 1;
|
|
73
|
-
this.#drainCount -= 1;
|
|
74
80
|
}
|
|
75
81
|
this.#activeCount -= 1;
|
|
76
82
|
if (counts.total === 0)
|
|
@@ -131,7 +131,9 @@ export class HostedExecutionAdmissionRegistry {
|
|
|
131
131
|
}
|
|
132
132
|
}
|
|
133
133
|
waitForSettledAdmissions() {
|
|
134
|
-
return Promise.all([...this.#bySession.values()]
|
|
134
|
+
return Promise.all([...this.#bySession.values()]
|
|
135
|
+
.filter((value) => value.kind === 'pending')
|
|
136
|
+
.map((value) => value.admissionSettled.promise)).then(() => undefined);
|
|
135
137
|
}
|
|
136
138
|
}
|
|
137
139
|
export function hostedExecutionDeferred() {
|
|
@@ -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);
|