@scotthuang/agent-knock-knock 0.12.11 → 0.12.13
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/CHANGELOG.md +40 -0
- package/README.md +43 -30
- package/dist/src/approval-policy.d.ts +11 -2
- package/dist/src/approval-policy.js +33 -7
- package/dist/src/approval-policy.js.map +1 -1
- package/dist/src/callback-cli-adapter.d.ts +47 -0
- package/dist/src/callback-cli-adapter.js +156 -0
- package/dist/src/callback-cli-adapter.js.map +1 -0
- package/dist/src/callback-outbox-policy.d.ts +95 -0
- package/dist/src/callback-outbox-policy.js +149 -0
- package/dist/src/callback-outbox-policy.js.map +1 -0
- package/dist/src/callback-outbox-service.d.ts +247 -0
- package/dist/src/callback-outbox-service.js +1053 -0
- package/dist/src/callback-outbox-service.js.map +1 -0
- package/dist/src/callback-outbox-settlement.d.ts +54 -0
- package/dist/src/callback-outbox-settlement.js +241 -0
- package/dist/src/callback-outbox-settlement.js.map +1 -0
- package/dist/src/canonical-json.d.ts +1 -0
- package/dist/src/canonical-json.js +14 -0
- package/dist/src/canonical-json.js.map +1 -0
- package/dist/src/claude-lifecycle-compatibility.js +15 -0
- package/dist/src/claude-lifecycle-compatibility.js.map +1 -1
- package/dist/src/claude-local-transcript-provider.d.ts +82 -1
- package/dist/src/claude-local-transcript-provider.js +873 -23
- package/dist/src/claude-local-transcript-provider.js.map +1 -1
- package/dist/src/cli-command-runtime.d.ts +14 -0
- package/dist/src/cli-command-runtime.js +164 -0
- package/dist/src/cli-command-runtime.js.map +1 -0
- package/dist/src/cli-core.d.ts +4 -42
- package/dist/src/cli-core.js +958 -28684
- package/dist/src/cli-core.js.map +1 -1
- package/dist/src/cli-runtime-context.d.ts +57 -0
- package/dist/src/cli-runtime-context.js +118 -0
- package/dist/src/cli-runtime-context.js.map +1 -0
- package/dist/src/codex-lifecycle-compatibility.js +2 -1
- package/dist/src/codex-lifecycle-compatibility.js.map +1 -1
- package/dist/src/codex-local-session-provider.d.ts +18 -0
- package/dist/src/codex-local-session-provider.js +67 -0
- package/dist/src/codex-local-session-provider.js.map +1 -1
- package/dist/src/codex-session-provider.js +1 -3
- package/dist/src/codex-session-provider.js.map +1 -1
- package/dist/src/conversation-trace.d.ts +30 -0
- package/dist/src/conversation-trace.js +185 -0
- package/dist/src/conversation-trace.js.map +1 -0
- package/dist/src/deferred-foreground-application-service.d.ts +84 -0
- package/dist/src/deferred-foreground-application-service.js +742 -0
- package/dist/src/deferred-foreground-application-service.js.map +1 -0
- package/dist/src/deferred-foreground-authority-cli-adapter.d.ts +78 -0
- package/dist/src/deferred-foreground-authority-cli-adapter.js +795 -0
- package/dist/src/deferred-foreground-authority-cli-adapter.js.map +1 -0
- package/dist/src/deferred-foreground-boundary.d.ts +61 -0
- package/dist/src/deferred-foreground-boundary.js +2 -0
- package/dist/src/deferred-foreground-boundary.js.map +1 -0
- package/dist/src/deferred-foreground-capability.d.ts +4 -0
- package/dist/src/deferred-foreground-capability.js +184 -0
- package/dist/src/deferred-foreground-capability.js.map +1 -0
- package/dist/src/deferred-foreground-preparation-cli-adapter.d.ts +53 -0
- package/dist/src/deferred-foreground-preparation-cli-adapter.js +210 -0
- package/dist/src/deferred-foreground-preparation-cli-adapter.js.map +1 -0
- package/dist/src/deferred-foreground-preparation-service.d.ts +72 -0
- package/dist/src/deferred-foreground-preparation-service.js +179 -0
- package/dist/src/deferred-foreground-preparation-service.js.map +1 -0
- package/dist/src/deferred-foreground-recovery-cli-adapter.d.ts +139 -0
- package/dist/src/deferred-foreground-recovery-cli-adapter.js +1125 -0
- package/dist/src/deferred-foreground-recovery-cli-adapter.js.map +1 -0
- package/dist/src/deferred-foreground-recovery-service.d.ts +48 -0
- package/dist/src/deferred-foreground-recovery-service.js +91 -0
- package/dist/src/deferred-foreground-recovery-service.js.map +1 -0
- package/dist/src/deferred-foreground-transfer-policy.d.ts +2 -0
- package/dist/src/deferred-foreground-transfer-policy.js +10 -0
- package/dist/src/deferred-foreground-transfer-policy.js.map +1 -0
- package/dist/src/deferred-foreground-transfer.js +46 -105
- package/dist/src/deferred-foreground-transfer.js.map +1 -1
- package/dist/src/doctor-capabilities.d.ts +9 -0
- package/dist/src/doctor-capabilities.js +22 -0
- package/dist/src/doctor-capabilities.js.map +1 -1
- package/dist/src/durable-json-file.d.ts +22 -0
- package/dist/src/durable-json-file.js +127 -0
- package/dist/src/durable-json-file.js.map +1 -0
- package/dist/src/file-lock-cli-adapter.d.ts +43 -0
- package/dist/src/file-lock-cli-adapter.js +150 -0
- package/dist/src/file-lock-cli-adapter.js.map +1 -0
- package/dist/src/herdr-terminal-control-provider.js +1 -3
- package/dist/src/herdr-terminal-control-provider.js.map +1 -1
- package/dist/src/install-doctor-command-adapter.d.ts +17 -0
- package/dist/src/install-doctor-command-adapter.js +225 -0
- package/dist/src/install-doctor-command-adapter.js.map +1 -0
- package/dist/src/live-lifecycle-evidence.js +76 -46
- package/dist/src/live-lifecycle-evidence.js.map +1 -1
- package/dist/src/live-lifecycle-smoke.js +74 -50
- package/dist/src/live-lifecycle-smoke.js.map +1 -1
- package/dist/src/managed-session.d.ts +2 -0
- package/dist/src/managed-session.js +24 -0
- package/dist/src/managed-session.js.map +1 -1
- package/dist/src/managed-turn-recovery-service.d.ts +86 -0
- package/dist/src/managed-turn-recovery-service.js +145 -0
- package/dist/src/managed-turn-recovery-service.js.map +1 -0
- package/dist/src/mutation-transaction.d.ts +48 -0
- package/dist/src/mutation-transaction.js +73 -0
- package/dist/src/mutation-transaction.js.map +1 -0
- package/dist/src/native-thread-lifecycle-cli-adapter.d.ts +154 -0
- package/dist/src/native-thread-lifecycle-cli-adapter.js +786 -0
- package/dist/src/native-thread-lifecycle-cli-adapter.js.map +1 -0
- package/dist/src/native-thread-lifecycle-ledger-cli-adapter.d.ts +25 -0
- package/dist/src/native-thread-lifecycle-ledger-cli-adapter.js +50 -0
- package/dist/src/native-thread-lifecycle-ledger-cli-adapter.js.map +1 -0
- package/dist/src/native-thread-lifecycle-query-service.d.ts +75 -0
- package/dist/src/native-thread-lifecycle-query-service.js +297 -0
- package/dist/src/native-thread-lifecycle-query-service.js.map +1 -0
- package/dist/src/native-thread-lifecycle-recovery-adapter.d.ts +45 -0
- package/dist/src/native-thread-lifecycle-recovery-adapter.js +154 -0
- package/dist/src/native-thread-lifecycle-recovery-adapter.js.map +1 -0
- package/dist/src/native-thread-lifecycle-recovery-service.d.ts +211 -0
- package/dist/src/native-thread-lifecycle-recovery-service.js +1093 -0
- package/dist/src/native-thread-lifecycle-recovery-service.js.map +1 -0
- package/dist/src/native-thread-resume-snapshot-policy.d.ts +73 -0
- package/dist/src/native-thread-resume-snapshot-policy.js +218 -0
- package/dist/src/native-thread-resume-snapshot-policy.js.map +1 -0
- package/dist/src/native-thread-resume-snapshot.d.ts +3 -61
- package/dist/src/native-thread-resume-snapshot.js +2 -143
- package/dist/src/native-thread-resume-snapshot.js.map +1 -1
- package/dist/src/native-thread-transition-application.d.ts +147 -0
- package/dist/src/native-thread-transition-application.js +974 -0
- package/dist/src/native-thread-transition-application.js.map +1 -0
- package/dist/src/native-thread-transition-policy.d.ts +127 -0
- package/dist/src/native-thread-transition-policy.js +154 -0
- package/dist/src/native-thread-transition-policy.js.map +1 -0
- package/dist/src/native-thread-transition-resource-adapter.d.ts +12 -0
- package/dist/src/native-thread-transition-resource-adapter.js +44 -0
- package/dist/src/native-thread-transition-resource-adapter.js.map +1 -0
- package/dist/src/native-thread-transition-settlement-service.d.ts +94 -0
- package/dist/src/native-thread-transition-settlement-service.js +134 -0
- package/dist/src/native-thread-transition-settlement-service.js.map +1 -0
- package/dist/src/native-thread-transition-verification-adapter.d.ts +62 -0
- package/dist/src/native-thread-transition-verification-adapter.js +451 -0
- package/dist/src/native-thread-transition-verification-adapter.js.map +1 -0
- package/dist/src/openclaw-callback-transport.d.ts +69 -0
- package/dist/src/openclaw-callback-transport.js +583 -0
- package/dist/src/openclaw-callback-transport.js.map +1 -0
- package/dist/src/openclaw-plugin-callback-adapter.d.ts +1 -0
- package/dist/src/openclaw-plugin-callback-adapter.js +423 -0
- package/dist/src/openclaw-plugin-callback-adapter.js.map +1 -0
- package/dist/src/openclaw-plugin-command-adapter.d.ts +16 -0
- package/dist/src/openclaw-plugin-command-adapter.js +1868 -0
- package/dist/src/openclaw-plugin-command-adapter.js.map +1 -0
- package/dist/src/openclaw-plugin-helpers.d.ts +27 -4
- package/dist/src/openclaw-plugin-helpers.js +356 -81
- package/dist/src/openclaw-plugin-helpers.js.map +1 -1
- package/dist/src/openclaw-plugin-schemas.d.ts +358 -0
- package/dist/src/openclaw-plugin-schemas.js +366 -0
- package/dist/src/openclaw-plugin-schemas.js.map +1 -0
- package/dist/src/openclaw-plugin-supervisor.d.ts +6 -0
- package/dist/src/openclaw-plugin-supervisor.js +123 -0
- package/dist/src/openclaw-plugin-supervisor.js.map +1 -0
- package/dist/src/openclaw-plugin.js +7 -2212
- package/dist/src/openclaw-plugin.js.map +1 -1
- package/dist/src/openclaw-private-authority-offers.d.ts +22 -0
- package/dist/src/openclaw-private-authority-offers.js +177 -0
- package/dist/src/openclaw-private-authority-offers.js.map +1 -0
- package/dist/src/protocol.d.ts +5 -0
- package/dist/src/protocol.js +59 -3
- package/dist/src/protocol.js.map +1 -1
- package/dist/src/session-store.d.ts +1 -0
- package/dist/src/session-store.js +84 -106
- package/dist/src/session-store.js.map +1 -1
- package/dist/src/store.d.ts +1 -0
- package/dist/src/store.js +5 -2
- package/dist/src/store.js.map +1 -1
- package/dist/src/terminal-acceptance-application-service.d.ts +65 -0
- package/dist/src/terminal-acceptance-application-service.js +63 -0
- package/dist/src/terminal-acceptance-application-service.js.map +1 -0
- package/dist/src/terminal-acceptance-cli-adapter.d.ts +210 -0
- package/dist/src/terminal-acceptance-cli-adapter.js +957 -0
- package/dist/src/terminal-acceptance-cli-adapter.js.map +1 -0
- package/dist/src/terminal-action-projection.d.ts +309 -0
- package/dist/src/terminal-action-projection.js +376 -0
- package/dist/src/terminal-action-projection.js.map +1 -0
- package/dist/src/terminal-agent-bridge.d.ts +3 -0
- package/dist/src/terminal-agent-bridge.js +29 -8
- package/dist/src/terminal-agent-bridge.js.map +1 -1
- package/dist/src/terminal-authority-policy.d.ts +141 -0
- package/dist/src/terminal-authority-policy.js +367 -0
- package/dist/src/terminal-authority-policy.js.map +1 -0
- package/dist/src/terminal-binding-authority.d.ts +118 -0
- package/dist/src/terminal-binding-authority.js +245 -0
- package/dist/src/terminal-binding-authority.js.map +1 -0
- package/dist/src/terminal-binding-reconciliation-service.d.ts +75 -0
- package/dist/src/terminal-binding-reconciliation-service.js +73 -0
- package/dist/src/terminal-binding-reconciliation-service.js.map +1 -0
- package/dist/src/terminal-command-cli-adapter.d.ts +380 -0
- package/dist/src/terminal-command-cli-adapter.js +3288 -0
- package/dist/src/terminal-command-cli-adapter.js.map +1 -0
- package/dist/src/terminal-control-ref.d.ts +4 -0
- package/dist/src/terminal-control-ref.js +11 -8
- package/dist/src/terminal-control-ref.js.map +1 -1
- package/dist/src/terminal-delegate-cli-adapter.d.ts +41 -0
- package/dist/src/terminal-delegate-cli-adapter.js +242 -0
- package/dist/src/terminal-delegate-cli-adapter.js.map +1 -0
- package/dist/src/terminal-dispatch-abort.d.ts +22 -0
- package/dist/src/terminal-dispatch-abort.js +30 -0
- package/dist/src/terminal-dispatch-abort.js.map +1 -0
- package/dist/src/terminal-dispatch-application.d.ts +115 -0
- package/dist/src/terminal-dispatch-application.js +478 -0
- package/dist/src/terminal-dispatch-application.js.map +1 -0
- package/dist/src/terminal-dispatch-capability.d.ts +63 -0
- package/dist/src/terminal-dispatch-capability.js +133 -0
- package/dist/src/terminal-dispatch-capability.js.map +1 -0
- package/dist/src/terminal-dispatch-completion-cli-adapter.d.ts +22 -0
- package/dist/src/terminal-dispatch-completion-cli-adapter.js +96 -0
- package/dist/src/terminal-dispatch-completion-cli-adapter.js.map +1 -0
- package/dist/src/terminal-dispatch-composition.d.ts +79 -0
- package/dist/src/terminal-dispatch-composition.js +2 -0
- package/dist/src/terminal-dispatch-composition.js.map +1 -0
- package/dist/src/terminal-dispatch-execution.d.ts +290 -0
- package/dist/src/terminal-dispatch-execution.js +916 -0
- package/dist/src/terminal-dispatch-execution.js.map +1 -0
- package/dist/src/terminal-dispatch-ledger-codec.d.ts +110 -0
- package/dist/src/terminal-dispatch-ledger-codec.js +399 -0
- package/dist/src/terminal-dispatch-ledger-codec.js.map +1 -0
- package/dist/src/terminal-dispatch-policy.d.ts +93 -0
- package/dist/src/terminal-dispatch-policy.js +122 -0
- package/dist/src/terminal-dispatch-policy.js.map +1 -0
- package/dist/src/terminal-dispatch-presenter.d.ts +65 -0
- package/dist/src/terminal-dispatch-presenter.js +258 -0
- package/dist/src/terminal-dispatch-presenter.js.map +1 -0
- package/dist/src/terminal-dispatch-receipt.d.ts +79 -0
- package/dist/src/terminal-dispatch-receipt.js +314 -0
- package/dist/src/terminal-dispatch-receipt.js.map +1 -0
- package/dist/src/terminal-dispatch-recovery-cli-adapter.d.ts +132 -0
- package/dist/src/terminal-dispatch-recovery-cli-adapter.js +1330 -0
- package/dist/src/terminal-dispatch-recovery-cli-adapter.js.map +1 -0
- package/dist/src/terminal-dispatch-recovery-service.d.ts +271 -0
- package/dist/src/terminal-dispatch-recovery-service.js +374 -0
- package/dist/src/terminal-dispatch-recovery-service.js.map +1 -0
- package/dist/src/terminal-dispatch-repository-cli-adapter.d.ts +35 -0
- package/dist/src/terminal-dispatch-repository-cli-adapter.js +483 -0
- package/dist/src/terminal-dispatch-repository-cli-adapter.js.map +1 -0
- package/dist/src/terminal-handoff-application-service.d.ts +7 -0
- package/dist/src/terminal-handoff-application-service.js +18 -0
- package/dist/src/terminal-handoff-application-service.js.map +1 -0
- package/dist/src/terminal-handoff-cli-adapter.d.ts +229 -0
- package/dist/src/terminal-handoff-cli-adapter.js +1389 -0
- package/dist/src/terminal-handoff-cli-adapter.js.map +1 -0
- package/dist/src/terminal-handoff-facts.d.ts +12 -0
- package/dist/src/terminal-handoff-facts.js +16 -0
- package/dist/src/terminal-handoff-facts.js.map +1 -0
- package/dist/src/terminal-identity-authority-cli-adapter.d.ts +318 -0
- package/dist/src/terminal-identity-authority-cli-adapter.js +1072 -0
- package/dist/src/terminal-identity-authority-cli-adapter.js.map +1 -0
- package/dist/src/terminal-identity-authority-service.d.ts +48 -0
- package/dist/src/terminal-identity-authority-service.js +45 -0
- package/dist/src/terminal-identity-authority-service.js.map +1 -0
- package/dist/src/terminal-list-cli-adapter.d.ts +274 -0
- package/dist/src/terminal-list-cli-adapter.js +2444 -0
- package/dist/src/terminal-list-cli-adapter.js.map +1 -0
- package/dist/src/terminal-list-renderer.d.ts +34 -0
- package/dist/src/terminal-list-renderer.js +643 -0
- package/dist/src/terminal-list-renderer.js.map +1 -0
- package/dist/src/terminal-maintenance-cli-adapter.d.ts +102 -0
- package/dist/src/terminal-maintenance-cli-adapter.js +1059 -0
- package/dist/src/terminal-maintenance-cli-adapter.js.map +1 -0
- package/dist/src/terminal-monitor-application-service.d.ts +275 -0
- package/dist/src/terminal-monitor-application-service.js +982 -0
- package/dist/src/terminal-monitor-application-service.js.map +1 -0
- package/dist/src/terminal-monitor-cli-adapter.d.ts +130 -0
- package/dist/src/terminal-monitor-cli-adapter.js +442 -0
- package/dist/src/terminal-monitor-cli-adapter.js.map +1 -0
- package/dist/src/terminal-monitor-decision-policy.d.ts +98 -0
- package/dist/src/terminal-monitor-decision-policy.js +246 -0
- package/dist/src/terminal-monitor-decision-policy.js.map +1 -0
- package/dist/src/terminal-monitor-launch-plan.d.ts +45 -0
- package/dist/src/terminal-monitor-launch-plan.js +94 -0
- package/dist/src/terminal-monitor-launch-plan.js.map +1 -0
- package/dist/src/terminal-monitor-ownership-policy.d.ts +31 -0
- package/dist/src/terminal-monitor-ownership-policy.js +64 -0
- package/dist/src/terminal-monitor-ownership-policy.js.map +1 -0
- package/dist/src/terminal-monitor-poll-policy.d.ts +48 -0
- package/dist/src/terminal-monitor-poll-policy.js +67 -0
- package/dist/src/terminal-monitor-poll-policy.js.map +1 -0
- package/dist/src/terminal-monitor-reconciliation-eligibility.d.ts +53 -0
- package/dist/src/terminal-monitor-reconciliation-eligibility.js +114 -0
- package/dist/src/terminal-monitor-reconciliation-eligibility.js.map +1 -0
- package/dist/src/terminal-monitor-state-cli-adapter.d.ts +106 -0
- package/dist/src/terminal-monitor-state-cli-adapter.js +1492 -0
- package/dist/src/terminal-monitor-state-cli-adapter.js.map +1 -0
- package/dist/src/terminal-monitor-state-reconciliation-service.d.ts +87 -0
- package/dist/src/terminal-monitor-state-reconciliation-service.js +106 -0
- package/dist/src/terminal-monitor-state-reconciliation-service.js.map +1 -0
- package/dist/src/terminal-monitor-supervision-cli-adapter.d.ts +122 -0
- package/dist/src/terminal-monitor-supervision-cli-adapter.js +685 -0
- package/dist/src/terminal-monitor-supervision-cli-adapter.js.map +1 -0
- package/dist/src/terminal-process-facts.d.ts +2 -0
- package/dist/src/terminal-process-facts.js +19 -0
- package/dist/src/terminal-process-facts.js.map +1 -0
- package/dist/src/terminal-process-source.d.ts +6 -4
- package/dist/src/terminal-process-source.js +25 -20
- package/dist/src/terminal-process-source.js.map +1 -1
- package/dist/src/terminal-runtime-cli-adapter.d.ts +80 -0
- package/dist/src/terminal-runtime-cli-adapter.js +488 -0
- package/dist/src/terminal-runtime-cli-adapter.js.map +1 -0
- package/dist/src/terminal-scoped-approval-authority.d.ts +62 -0
- package/dist/src/terminal-scoped-approval-authority.js +281 -0
- package/dist/src/terminal-scoped-approval-authority.js.map +1 -0
- package/dist/src/terminal-selector-projection-service.d.ts +26 -0
- package/dist/src/terminal-selector-projection-service.js +81 -0
- package/dist/src/terminal-selector-projection-service.js.map +1 -0
- package/dist/src/terminal-status-cli-adapter.d.ts +126 -0
- package/dist/src/terminal-status-cli-adapter.js +577 -0
- package/dist/src/terminal-status-cli-adapter.js.map +1 -0
- package/dist/src/terminal-status-facts.d.ts +56 -0
- package/dist/src/terminal-status-facts.js +199 -0
- package/dist/src/terminal-status-facts.js.map +1 -0
- package/dist/src/terminal-submission-acceptance.d.ts +62 -145
- package/dist/src/terminal-submission-acceptance.js +555 -222
- package/dist/src/terminal-submission-acceptance.js.map +1 -1
- package/dist/src/terminal-submission-facts.d.ts +123 -0
- package/dist/src/terminal-submission-facts.js +205 -0
- package/dist/src/terminal-submission-facts.js.map +1 -0
- package/dist/src/terminal-turn-binding-authority-cli-adapter.d.ts +14 -0
- package/dist/src/terminal-turn-binding-authority-cli-adapter.js +90 -0
- package/dist/src/terminal-turn-binding-authority-cli-adapter.js.map +1 -0
- package/dist/src/terminal-watch-callback-cli-adapter.d.ts +20 -0
- package/dist/src/terminal-watch-callback-cli-adapter.js +55 -0
- package/dist/src/terminal-watch-callback-cli-adapter.js.map +1 -0
- package/dist/src/terminal-watch-cli-adapter.d.ts +64 -0
- package/dist/src/terminal-watch-cli-adapter.js +508 -0
- package/dist/src/terminal-watch-cli-adapter.js.map +1 -0
- package/dist/src/terminal-watch-service.d.ts +97 -0
- package/dist/src/terminal-watch-service.js +714 -0
- package/dist/src/terminal-watch-service.js.map +1 -0
- package/dist/src/terminal-watch-store.d.ts +149 -0
- package/dist/src/terminal-watch-store.js +861 -0
- package/dist/src/terminal-watch-store.js.map +1 -0
- package/dist/src/value-guards.d.ts +4 -0
- package/dist/src/value-guards.js +12 -0
- package/dist/src/value-guards.js.map +1 -0
- package/dist/src/verified-dead-agent-policy.d.ts +192 -0
- package/dist/src/verified-dead-agent-policy.js +262 -0
- package/dist/src/verified-dead-agent-policy.js.map +1 -0
- package/docs/quickstart-herdr.md +8 -0
- package/docs/quickstart-tmux.md +33 -6
- package/openclaw.plugin.json +11 -3
- package/package.json +3 -1
- package/templates/openclaw-skills/agent-knock-knock/SKILL.md +58 -40
|
@@ -0,0 +1,957 @@
|
|
|
1
|
+
// CLI infrastructure for native acceptance and managed Turn persistence.
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { detectClaudeTranscriptAcceptance } from "./claude-local-transcript-provider.js";
|
|
4
|
+
import { applyMessageToConversation, createConversation, createMessage, executorForConversation, isSessionSendBlockingStatus, resolveExecutor, sessionIdForConversation } from "./protocol.js";
|
|
5
|
+
import { managedSessionRevision } from "./managed-session.js";
|
|
6
|
+
import { loadManagedSession, saveManagedSession, tryLoadManagedSession } from "./session-store.js";
|
|
7
|
+
import { appendEvent, defaultStoreDir, listConversations, loadState, pathsForConversation, pathsForConversationDir, saveState, withStoreWriterLease, withStoreWriterLeaseAsync } from "./store.js";
|
|
8
|
+
import { hasCanonicalTerminalEndpoint, terminalControlEvidence } from "./terminal-control-ref.js";
|
|
9
|
+
import { isCompleteNativeRollout } from "./terminal-binding-authority.js";
|
|
10
|
+
import { terminalControlsShareIncarnation } from "./terminal-authority-policy.js";
|
|
11
|
+
import { validTerminalMonitorTimestampMs } from "./terminal-monitor-decision-policy.js";
|
|
12
|
+
import { TerminalDispatchExecutionService, assertManagedSessionCanStartTurnPolicy, managedTurnMatchesTerminal } from "./terminal-dispatch-execution.js";
|
|
13
|
+
import { applyTerminalBridgeSubmission, terminalBridgeRequestFingerprint, terminalBridgeSubmission } from "./terminal-dispatch-receipt.js";
|
|
14
|
+
import { captureCodexRolloutAcceptanceAnchor, detectCodexCandidateSetRolloutAcceptance, detectCodexRolloutAcceptance } from "./terminal-submission-acceptance.js";
|
|
15
|
+
import { TerminalAcceptanceApplicationService } from "./terminal-acceptance-application-service.js";
|
|
16
|
+
import { ManagedTurnRecoveryService, isCurrentVirginCodexRecoveryBoundary, isVirginCodexRecoveryCandidate } from "./managed-turn-recovery-service.js";
|
|
17
|
+
import { isFinalDeferredForegroundTransferStatus } from "./deferred-foreground-transfer-policy.js";
|
|
18
|
+
import { cliCwd, cliEnv, cliNow, cliNowMs, cliPid, cliRuntimeLog, cliSleep } from "./cli-runtime-context.js";
|
|
19
|
+
import { expandHome, resolveOptionalExecutable } from "./cli-command-runtime.js";
|
|
20
|
+
import { isRecord, nonBlankString } from "./value-guards.js";
|
|
21
|
+
import { compareManagedConversationRecency } from "./terminal-action-projection.js";
|
|
22
|
+
/** Factory-scoped CLI composition; it retains no mutable module runtime. */
|
|
23
|
+
export function createTerminalAcceptanceCliFacade(dependencies) {
|
|
24
|
+
const application = new TerminalAcceptanceCliApplication(dependencies);
|
|
25
|
+
return Object.freeze({
|
|
26
|
+
execution: (options, bridge) => application.execution(options, bridge),
|
|
27
|
+
recoverVirgin: (input) => application.recoverVirgin(input),
|
|
28
|
+
reconcileMonitor: (input) => application.reconcileMonitor(input),
|
|
29
|
+
markUncertain: (input) => application.markUncertain(input),
|
|
30
|
+
inspectCodexOpenRoots: (input) => application.inspectCodexOpenRoots(input),
|
|
31
|
+
resolveNativeIdentity: (input) => application.resolveNativeIdentity(input),
|
|
32
|
+
observeNativeIdentity: (input) => application.observeNativeIdentity(input),
|
|
33
|
+
assertTurnIdentity: (input) => application.assertTurnIdentity(input),
|
|
34
|
+
withNativeIdentity: (conversation, identity) => application.withNativeIdentity(conversation, identity),
|
|
35
|
+
storeDirForConversation: (conversation) => application.storeDirForConversation(conversation),
|
|
36
|
+
refineSessionIdentity: (input) => application.refineSessionIdentity(input),
|
|
37
|
+
persistSessionIdentity: (input) => application.persistSessionIdentity(input),
|
|
38
|
+
quarantineSession: (input) => application.quarantineSession(input),
|
|
39
|
+
turnsForSession: (storeDir, sessionId) => application.turnsForSession(storeDir, sessionId),
|
|
40
|
+
assertSessionCanStartTurn: (turns) => application.assertSessionCanStartTurn(turns),
|
|
41
|
+
turnMatchesTerminal: (input) => application.turnMatchesTerminal(input),
|
|
42
|
+
createManagedTurn: (input) => application.createManagedTurn(input)
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
class TerminalAcceptanceCliApplication {
|
|
46
|
+
#dependencies;
|
|
47
|
+
constructor(dependencies) {
|
|
48
|
+
this.#dependencies = dependencies;
|
|
49
|
+
}
|
|
50
|
+
execution(options, bridge) {
|
|
51
|
+
const provider = () => this.#dependencies.native.codexProvider(options);
|
|
52
|
+
let currentRuntime;
|
|
53
|
+
const runtime = () => currentRuntime ??=
|
|
54
|
+
this.#dependencies.terminal.runtime(options);
|
|
55
|
+
return new TerminalDispatchExecutionService(cliEnv().AKK_TEST_ALLOW_SYNTHETIC_TERMINAL_ACCEPTANCE === "1"
|
|
56
|
+
? cliEnv().AKK_TEST_TERMINAL_ACCEPTANCE_OUTCOME ?? "accepted"
|
|
57
|
+
: undefined, {
|
|
58
|
+
clock: { now: cliNow, nowMs: cliNowMs, sleep: cliSleep },
|
|
59
|
+
native: {
|
|
60
|
+
resolveCodex: (request) => provider().resolveActiveSessionIdentityForPid(request.pid, request.cwd, request.preferredSessionId, request.allowedCompanionIdentity
|
|
61
|
+
? companionIdentity(request.allowedCompanionIdentity, "managed_transition_before_identity")
|
|
62
|
+
: undefined, request.allowedAdditionalIdentities?.map((identity) => companionIdentity(identity, "managed_transition_ancestor_identity"))),
|
|
63
|
+
inspectCodexOpenRoots: async (pid, cwd) => {
|
|
64
|
+
const currentProvider = provider();
|
|
65
|
+
const inspect = currentProvider.inspectOpenRootRolloutInventoryForPid;
|
|
66
|
+
if (!inspect) {
|
|
67
|
+
throw new Error("Codex open-root rollout inventory inspection is unavailable");
|
|
68
|
+
}
|
|
69
|
+
return inspect.call(currentProvider, pid, cwd);
|
|
70
|
+
},
|
|
71
|
+
claudeRows: () => runtime().loadClaudeAgentRows({ required: true }),
|
|
72
|
+
codexProcessIncarnation: this.#dependencies.native.codexProcessIncarnation
|
|
73
|
+
},
|
|
74
|
+
acceptance: {
|
|
75
|
+
captureCodex: captureCodexRolloutAcceptanceAnchor,
|
|
76
|
+
detectCodexCandidates: detectCodexCandidateSetRolloutAcceptance,
|
|
77
|
+
detectBoundCodex: detectCodexRolloutAcceptance,
|
|
78
|
+
detectClaude: (conversation, control) => detectClaudeTranscriptAcceptance(this.#dependencies.terminal.durableRequest(conversation, control), {
|
|
79
|
+
claudeHome: expandHome(options.claudeHome),
|
|
80
|
+
agentRows: runtime().loadClaudeAgentRows()
|
|
81
|
+
})
|
|
82
|
+
},
|
|
83
|
+
terminal: {
|
|
84
|
+
proveExactDraftStillPresent: (input) => (bridge ?? runtime().createBridge())
|
|
85
|
+
.proveExactDraftStillPresent(input.executor, input.terminalControl, input.requestText, {
|
|
86
|
+
scrollbackLines: input.scrollbackLines,
|
|
87
|
+
runtime: this.#dependencies.terminal.runtimeIdentity(input.conversation, input.terminalControl)
|
|
88
|
+
})
|
|
89
|
+
},
|
|
90
|
+
authority: {
|
|
91
|
+
assertTurnCurrent: this.#dependencies.authority.assertTurnCurrent
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
async inspectCodexOpenRoots(input) {
|
|
96
|
+
return this.execution(input.options)
|
|
97
|
+
.inspectCodexOpenRootInventory(input.pid, input.cwd);
|
|
98
|
+
}
|
|
99
|
+
async resolveNativeIdentity(input) {
|
|
100
|
+
const { options, ...request } = input;
|
|
101
|
+
return this.execution(options).resolveCurrentNativeIdentity(request);
|
|
102
|
+
}
|
|
103
|
+
async observeNativeIdentity(input) {
|
|
104
|
+
const { options, ...request } = input;
|
|
105
|
+
return this.execution(options).observeCurrentNativeIdentity(request);
|
|
106
|
+
}
|
|
107
|
+
assertTurnIdentity(input) {
|
|
108
|
+
this.execution({}).assertTurnIdentity(input);
|
|
109
|
+
}
|
|
110
|
+
withNativeIdentity(conversation, identity) {
|
|
111
|
+
return this.execution({}).withNativeIdentity(conversation, identity);
|
|
112
|
+
}
|
|
113
|
+
async recoverVirgin(input) {
|
|
114
|
+
const initialControl = this.#dependencies.authority.terminalControl(takeoverFor(input.conversation));
|
|
115
|
+
const initialFacts = this.#virginFacts(input.conversation, initialControl, undefined);
|
|
116
|
+
if (!isVirginCodexRecoveryCandidate(initialFacts)) {
|
|
117
|
+
return { conversation: input.conversation, state: "not_applicable" };
|
|
118
|
+
}
|
|
119
|
+
const storeDir = pathsForConversationDir(path.dirname(input.statePath)).storeDir;
|
|
120
|
+
const releaseTerminal = input.terminalLockHeld
|
|
121
|
+
? () => { }
|
|
122
|
+
: this.#dependencies.repository.acquireTerminalLock(storeDir, initialControl, { timeoutMs: 30000 });
|
|
123
|
+
try {
|
|
124
|
+
return await this.#recoverVirginWithWriter({
|
|
125
|
+
...input,
|
|
126
|
+
initialControl: initialControl,
|
|
127
|
+
storeDir
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
finally {
|
|
131
|
+
releaseTerminal();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async #recoverVirginWithWriter(input) {
|
|
135
|
+
return withStoreWriterLeaseAsync(input.storeDir, async () => {
|
|
136
|
+
const releaseState = this.#dependencies.repository.acquireStateLock(`${input.statePath}.lock`);
|
|
137
|
+
try {
|
|
138
|
+
return await this.#recoverVirginLocked(input);
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
releaseState();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async #recoverVirginLocked(input) {
|
|
146
|
+
let current = loadState(input.statePath);
|
|
147
|
+
const currentCandidate = this.#virginFacts(current, input.initialControl, undefined);
|
|
148
|
+
if (!isCurrentVirginCodexRecoveryBoundary(currentCandidate)) {
|
|
149
|
+
return { conversation: current, state: "not_applicable" };
|
|
150
|
+
}
|
|
151
|
+
const session = loadManagedSession(input.storeDir, sessionIdForConversation(current));
|
|
152
|
+
const facts = this.#virginFacts(current, input.initialControl, session);
|
|
153
|
+
const rawAnchor = takeoverFor(current)?.codex_rollout_acceptance_anchor;
|
|
154
|
+
let recoveredAt;
|
|
155
|
+
const service = new ManagedTurnRecoveryService({
|
|
156
|
+
identity: {
|
|
157
|
+
resolve: (request) => this.resolveNativeIdentity({
|
|
158
|
+
options: input.options,
|
|
159
|
+
agent: "codex",
|
|
160
|
+
...request
|
|
161
|
+
})
|
|
162
|
+
},
|
|
163
|
+
acceptance: {
|
|
164
|
+
detect: (identity, requestHash) => Boolean(detectCodexRolloutAcceptance({
|
|
165
|
+
anchor: rawAnchor,
|
|
166
|
+
currentIdentity: identity,
|
|
167
|
+
requestHash
|
|
168
|
+
}))
|
|
169
|
+
},
|
|
170
|
+
authority: {
|
|
171
|
+
assertExclusive: (request) => this.#dependencies.native.assertExclusive({
|
|
172
|
+
options: input.options,
|
|
173
|
+
agent: "codex",
|
|
174
|
+
currentPid: request.pid,
|
|
175
|
+
nativeThreadId: request.nativeThreadId,
|
|
176
|
+
storeDir: input.storeDir,
|
|
177
|
+
terminalControl: request.terminalControl,
|
|
178
|
+
excludedManagedSessionId: request.sessionId
|
|
179
|
+
}),
|
|
180
|
+
assertTurn: (identity) => {
|
|
181
|
+
if (!identity) {
|
|
182
|
+
this.#dependencies.authority.assertTurnCurrent(current, "recover virgin Codex binding for");
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
recoveredAt ??= cliNow().toISOString();
|
|
186
|
+
this.assertTurnIdentity({
|
|
187
|
+
conversation: current,
|
|
188
|
+
currentIdentity: identity,
|
|
189
|
+
operation: "recover virgin Codex binding for"
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
persistence: {
|
|
194
|
+
persistSessionIdentity: (identity) => {
|
|
195
|
+
const persisted = this.persistSessionIdentity({
|
|
196
|
+
conversation: current,
|
|
197
|
+
terminalControl: facts.terminalControl,
|
|
198
|
+
identity,
|
|
199
|
+
storeDir: input.storeDir
|
|
200
|
+
});
|
|
201
|
+
return persisted?.binding
|
|
202
|
+
? {
|
|
203
|
+
nativeThreadId: persisted.binding.native_thread_id,
|
|
204
|
+
processUuid: persisted.binding.native_process.process_uuid,
|
|
205
|
+
processBirth: persisted.binding.native_process.process_birth,
|
|
206
|
+
rollout: persisted.binding.native_process.rollout
|
|
207
|
+
}
|
|
208
|
+
: undefined;
|
|
209
|
+
},
|
|
210
|
+
persistTurnIdentity: (identity) => {
|
|
211
|
+
recoveredAt = cliNow().toISOString();
|
|
212
|
+
current = {
|
|
213
|
+
...this.withNativeIdentity(current, identity),
|
|
214
|
+
updated_at: recoveredAt
|
|
215
|
+
};
|
|
216
|
+
saveState(input.statePath, current);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
const result = await service.recover(facts);
|
|
221
|
+
if (result.state !== "recovered") {
|
|
222
|
+
return { conversation: current, state: result.state };
|
|
223
|
+
}
|
|
224
|
+
this.#appendVirginRecoveryEvent({
|
|
225
|
+
...input,
|
|
226
|
+
conversation: current,
|
|
227
|
+
facts,
|
|
228
|
+
identity: result.identity,
|
|
229
|
+
recoveredAt: recoveredAt ?? cliNow().toISOString()
|
|
230
|
+
});
|
|
231
|
+
return { conversation: current, state: "recovered" };
|
|
232
|
+
}
|
|
233
|
+
#virginFacts(conversation, initialControl, session) {
|
|
234
|
+
const takeover = takeoverFor(conversation);
|
|
235
|
+
const rawAnchor = isRecord(takeover?.codex_rollout_acceptance_anchor)
|
|
236
|
+
? takeover.codex_rollout_acceptance_anchor
|
|
237
|
+
: undefined;
|
|
238
|
+
const submission = terminalBridgeSubmission(conversation);
|
|
239
|
+
const terminalControl = this.#dependencies.authority.terminalControl(takeover);
|
|
240
|
+
const binding = session?.binding;
|
|
241
|
+
const requestText = String(takeover?.terminal_bridge_request_text ?? conversation.user_request ?? "");
|
|
242
|
+
return {
|
|
243
|
+
agent: executorForConversation(conversation).kind,
|
|
244
|
+
anchorVersion: numericValue(rawAnchor?.version),
|
|
245
|
+
anchorNativeThreadBinding: nonBlankString(rawAnchor?.native_thread_binding),
|
|
246
|
+
anchorProcessUuid: nonBlankString(rawAnchor?.process_uuid),
|
|
247
|
+
anchorProcessBirth: nonBlankString(rawAnchor?.process_birth),
|
|
248
|
+
terminalControl,
|
|
249
|
+
initialTerminalIncarnationMatches: Boolean(terminalControl && initialControl &&
|
|
250
|
+
terminalControlsShareIncarnation(terminalControl, initialControl)),
|
|
251
|
+
submissionStatus: nonBlankString(submission?.status),
|
|
252
|
+
messageId: nonBlankString(submission?.message_id),
|
|
253
|
+
takeoverMessageId: nonBlankString(takeover?.terminal_bridge_message_id),
|
|
254
|
+
requestHash: nonBlankString(takeover?.terminal_bridge_request_hash),
|
|
255
|
+
computedRequestHash: terminalBridgeRequestFingerprint(requestText),
|
|
256
|
+
bindingId: nonBlankString(conversation.terminal_binding_id),
|
|
257
|
+
bindingGeneration: safeInteger(conversation.terminal_binding_generation),
|
|
258
|
+
pid: safeInteger(takeover?.terminal_agent_pid),
|
|
259
|
+
sessionId: session?.session_id,
|
|
260
|
+
sessionAgent: session?.agent,
|
|
261
|
+
sessionStatus: session?.status,
|
|
262
|
+
sessionBinding: binding
|
|
263
|
+
? {
|
|
264
|
+
bindingId: binding.binding_id,
|
|
265
|
+
generation: binding.generation,
|
|
266
|
+
pid: binding.native_process.pid,
|
|
267
|
+
terminalIncarnationMatches: Boolean(terminalControl &&
|
|
268
|
+
terminalControlsShareIncarnation(binding.terminal_control, terminalControl)),
|
|
269
|
+
nativeThreadId: binding.native_thread_id,
|
|
270
|
+
processUuid: binding.native_process.process_uuid,
|
|
271
|
+
processBirth: binding.native_process.process_birth,
|
|
272
|
+
rollout: binding.native_process.rollout
|
|
273
|
+
}
|
|
274
|
+
: undefined,
|
|
275
|
+
turnNativeThreadId: nonBlankString(conversation.native_thread_id) ??
|
|
276
|
+
nonBlankString(takeover?.terminal_agent_session_id),
|
|
277
|
+
turnProcessUuid: nonBlankString(takeover?.terminal_agent_process_uuid),
|
|
278
|
+
turnProcessBirth: nonBlankString(takeover?.terminal_agent_process_birth),
|
|
279
|
+
turnRollout: isCompleteNativeRollout(takeover?.terminal_agent_rollout)
|
|
280
|
+
? takeover.terminal_agent_rollout
|
|
281
|
+
: undefined
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
#appendVirginRecoveryEvent(input) {
|
|
285
|
+
try {
|
|
286
|
+
appendEvent(input.logPath, {
|
|
287
|
+
ts: input.recoveredAt,
|
|
288
|
+
conversation_id: input.conversation.conversation_id,
|
|
289
|
+
event: "virgin_codex_post_submission_binding_recovered",
|
|
290
|
+
message_id: input.facts.messageId,
|
|
291
|
+
native_thread_id: input.identity.sessionId,
|
|
292
|
+
terminal_control: input.facts.terminalControl
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
catch (error) {
|
|
296
|
+
cliRuntimeLog("warn", "virgin_codex_binding_recovery_event_failed", {
|
|
297
|
+
conversation_id: input.conversation.conversation_id,
|
|
298
|
+
terminal_target: input.facts.terminalControl?.target,
|
|
299
|
+
error: errorText(error)
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
async reconcileMonitor(input) {
|
|
304
|
+
const deferred = await this.#reconcileDeferredAcceptance(input);
|
|
305
|
+
if (deferred)
|
|
306
|
+
return deferred;
|
|
307
|
+
const service = new TerminalAcceptanceApplicationService({
|
|
308
|
+
clock: { nowMs: cliNowMs },
|
|
309
|
+
binding: {
|
|
310
|
+
recover: async (conversation) => {
|
|
311
|
+
const recovered = await this.recoverVirgin({
|
|
312
|
+
...input,
|
|
313
|
+
conversation,
|
|
314
|
+
terminalLockHeld: true
|
|
315
|
+
});
|
|
316
|
+
return { turn: recovered.conversation, state: recovered.state };
|
|
317
|
+
}
|
|
318
|
+
},
|
|
319
|
+
acceptance: {
|
|
320
|
+
detect: (executor, conversation) => this.execution(input.options).detectAcceptance({
|
|
321
|
+
executor,
|
|
322
|
+
conversation,
|
|
323
|
+
terminalControl: input.terminalControl
|
|
324
|
+
})
|
|
325
|
+
},
|
|
326
|
+
terminal: {
|
|
327
|
+
proveExactDraftStillPresent: (conversation, requestText) => input.terminalBridge.proveExactDraftStillPresent(input.executor.kind, input.terminalControl, requestText, {
|
|
328
|
+
scrollbackLines: Number(input.options.scrollbackLines ?? 240),
|
|
329
|
+
runtime: this.#dependencies.terminal.runtimeIdentity(conversation, input.terminalControl)
|
|
330
|
+
})
|
|
331
|
+
},
|
|
332
|
+
repository: {
|
|
333
|
+
commit: (request) => this.#commitAcceptance(input, request)
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
const result = await service.reconcile({
|
|
337
|
+
executor: input.executor.kind,
|
|
338
|
+
turn: input.conversation,
|
|
339
|
+
project: acceptanceFacts
|
|
340
|
+
});
|
|
341
|
+
return result.outcome === "pending"
|
|
342
|
+
? result
|
|
343
|
+
: { outcome: result.outcome, conversation: result.turn };
|
|
344
|
+
}
|
|
345
|
+
async #reconcileDeferredAcceptance(input) {
|
|
346
|
+
const transferId = nonBlankString(takeoverFor(input.conversation)?.deferred_foreground_transfer_id);
|
|
347
|
+
if (!transferId)
|
|
348
|
+
return undefined;
|
|
349
|
+
const storeDir = pathsForConversationDir(path.dirname(input.statePath)).storeDir;
|
|
350
|
+
let transfer = this.#dependencies.deferred.loadTransfer(storeDir, transferId);
|
|
351
|
+
const pid = safeInteger(takeoverFor(input.conversation)?.terminal_agent_pid);
|
|
352
|
+
if (!pid || pid <= 1) {
|
|
353
|
+
throw new Error("deferred Codex acceptance monitor lost its exact process identity");
|
|
354
|
+
}
|
|
355
|
+
const terminal = await input.terminalBridge.resolveStoredTerminal("codex", pid, input.terminalControl, { pid });
|
|
356
|
+
let conversation = input.conversation;
|
|
357
|
+
if (!isFinalDeferredForegroundTransferStatus(transfer.status)) {
|
|
358
|
+
await this.#dependencies.deferred.recover({
|
|
359
|
+
options: { ...input.options, storeDir },
|
|
360
|
+
terminal
|
|
361
|
+
});
|
|
362
|
+
transfer = this.#dependencies.deferred.loadTransfer(storeDir, transferId);
|
|
363
|
+
conversation = loadState(input.statePath);
|
|
364
|
+
if (!isFinalDeferredForegroundTransferStatus(transfer.status)) {
|
|
365
|
+
return { outcome: "pending" };
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
if (transfer.status === "abort_resolved") {
|
|
369
|
+
assertDeferredAbortReceipt(conversation, transfer);
|
|
370
|
+
return { outcome: "not_accepted", conversation };
|
|
371
|
+
}
|
|
372
|
+
return this.#acceptedDeferredAuthority(storeDir, terminal, transfer);
|
|
373
|
+
}
|
|
374
|
+
#acceptedDeferredAuthority(storeDir, terminal, transfer) {
|
|
375
|
+
const authority = this.#dependencies.deferred.loadAuthority({
|
|
376
|
+
storeDir,
|
|
377
|
+
terminal,
|
|
378
|
+
transfer
|
|
379
|
+
});
|
|
380
|
+
const ledger = this.#dependencies.repository.loadLedger(terminal.terminalControl);
|
|
381
|
+
if (!ledger) {
|
|
382
|
+
throw new Error(`deferred foreground transfer ${transfer.transfer_id} resolved ` +
|
|
383
|
+
"without an exact terminal dispatch ledger");
|
|
384
|
+
}
|
|
385
|
+
this.#dependencies.deferred.assertLedgerAuthority({
|
|
386
|
+
storeDir,
|
|
387
|
+
terminal,
|
|
388
|
+
transfer,
|
|
389
|
+
ledger,
|
|
390
|
+
statePath: authority.statePath,
|
|
391
|
+
expectedMessageBodyHash: nonBlankString(authority.submission.message_body_hash)
|
|
392
|
+
});
|
|
393
|
+
const acceptedIdentity = deferredAcceptedIdentity(transfer);
|
|
394
|
+
this.assertTurnIdentity({
|
|
395
|
+
conversation: authority.conversation,
|
|
396
|
+
currentIdentity: acceptedIdentity,
|
|
397
|
+
operation: "continue deferred foreground monitor for"
|
|
398
|
+
});
|
|
399
|
+
return { outcome: "accepted", conversation: authority.conversation };
|
|
400
|
+
}
|
|
401
|
+
async #commitAcceptance(input, request) {
|
|
402
|
+
const resolvedAt = cliNow().toISOString();
|
|
403
|
+
const storeDir = pathsForConversationDir(path.dirname(input.statePath)).storeDir;
|
|
404
|
+
return withStoreWriterLeaseAsync(storeDir, async () => {
|
|
405
|
+
const releaseState = this.#dependencies.repository.acquireStateLock(`${input.statePath}.lock`);
|
|
406
|
+
try {
|
|
407
|
+
return this.#commitAcceptanceLocked(input, request, resolvedAt);
|
|
408
|
+
}
|
|
409
|
+
finally {
|
|
410
|
+
releaseState();
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
#commitAcceptanceLocked(input, request, resolvedAt) {
|
|
415
|
+
const current = loadState(input.statePath);
|
|
416
|
+
assertAcceptanceGeneration(current, request.turn, request.expected);
|
|
417
|
+
const accepted = resolvedAcceptanceConversation(current, request.expected, request.resolution, resolvedAt, this.storeDirForConversation(current), this.#dependencies.authority.terminalControl(takeoverFor(current)));
|
|
418
|
+
const ledger = this.#dependencies.repository.reconcileLedger(input.terminalControl, this.#dependencies.repository.loadLedger(input.terminalControl));
|
|
419
|
+
if (nonBlankString(ledger?.message_id) !== request.expected.messageId ||
|
|
420
|
+
ledger?.status !== "enter_dispatched") {
|
|
421
|
+
throw new Error("terminal dispatch ledger changed before acceptance reconciliation");
|
|
422
|
+
}
|
|
423
|
+
saveState(input.statePath, accepted);
|
|
424
|
+
this.#persistResolvedAcceptanceLedger(input.terminalControl, accepted, ledger, request.resolution, resolvedAt);
|
|
425
|
+
this.#appendResolvedAcceptanceEvent(input, accepted, request.resolution, resolvedAt);
|
|
426
|
+
return accepted;
|
|
427
|
+
}
|
|
428
|
+
#persistResolvedAcceptanceLedger(terminalControl, conversation, ledger, resolution, resolvedAt) {
|
|
429
|
+
try {
|
|
430
|
+
if (cliEnv().AKK_TEST_MONITOR_FINAL_TERMINAL_LEDGER_FAILURE === "1") {
|
|
431
|
+
throw new Error("injected monitor final terminal ledger persistence failure");
|
|
432
|
+
}
|
|
433
|
+
this.#dependencies.repository.saveLedger(terminalControl, {
|
|
434
|
+
...ledger,
|
|
435
|
+
...this.#dependencies.repository.bindingFields(conversation),
|
|
436
|
+
status: resolution.outcome,
|
|
437
|
+
...(resolution.outcome === "not_accepted"
|
|
438
|
+
? { not_accepted_at: resolvedAt }
|
|
439
|
+
: {
|
|
440
|
+
agent_accepted_at: resolvedAt,
|
|
441
|
+
acceptance_evidence: resolution.evidence
|
|
442
|
+
}),
|
|
443
|
+
dispatcher_pid: null
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
catch (error) {
|
|
447
|
+
this.#warnAcceptanceLag("terminal_acceptance_monitor_ledger_lagging", conversation, terminalControl, resolution, error);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
#appendResolvedAcceptanceEvent(input, conversation, resolution, resolvedAt) {
|
|
451
|
+
try {
|
|
452
|
+
if (cliEnv().AKK_TEST_MONITOR_FINAL_EVENT_FAILURE === "1") {
|
|
453
|
+
throw new Error("injected monitor final acceptance event persistence failure");
|
|
454
|
+
}
|
|
455
|
+
appendEvent(input.logPath, {
|
|
456
|
+
ts: resolvedAt,
|
|
457
|
+
conversation_id: conversation.conversation_id,
|
|
458
|
+
event: resolution.outcome === "not_accepted"
|
|
459
|
+
? "terminal_message_not_accepted"
|
|
460
|
+
: "terminal_message_agent_accepted",
|
|
461
|
+
message_id: nonBlankString(terminalBridgeSubmission(conversation)?.message_id),
|
|
462
|
+
terminal_control: input.terminalControl,
|
|
463
|
+
do_not_retry: resolution.outcome === "not_accepted"
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
catch (error) {
|
|
467
|
+
this.#warnAcceptanceLag("terminal_acceptance_monitor_event_lagging", conversation, input.terminalControl, resolution, error);
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
#warnAcceptanceLag(event, conversation, terminalControl, resolution, error) {
|
|
471
|
+
cliRuntimeLog("warn", event, {
|
|
472
|
+
conversation_id: conversation.conversation_id,
|
|
473
|
+
message_id: nonBlankString(terminalBridgeSubmission(conversation)?.message_id),
|
|
474
|
+
terminal_target: terminalControl.target,
|
|
475
|
+
durable_submission_status: resolution.outcome,
|
|
476
|
+
error: errorText(error)
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
markUncertain(input) {
|
|
480
|
+
const messageId = nonBlankString(terminalBridgeSubmission(input.conversation)?.message_id);
|
|
481
|
+
const uncertainAt = cliNow().toISOString();
|
|
482
|
+
const storeDir = pathsForConversationDir(path.dirname(input.statePath)).storeDir;
|
|
483
|
+
return withStoreWriterLease(storeDir, () => {
|
|
484
|
+
const releaseState = this.#dependencies.repository.acquireStateLock(`${input.statePath}.lock`);
|
|
485
|
+
try {
|
|
486
|
+
return this.#markUncertainLocked(input, messageId, uncertainAt);
|
|
487
|
+
}
|
|
488
|
+
finally {
|
|
489
|
+
releaseState();
|
|
490
|
+
}
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
#markUncertainLocked(input, messageId, uncertainAt) {
|
|
494
|
+
const current = loadState(input.statePath);
|
|
495
|
+
const submission = terminalBridgeSubmission(current);
|
|
496
|
+
if (!messageId ||
|
|
497
|
+
nonBlankString(submission?.message_id) !== messageId ||
|
|
498
|
+
!["text_injected", "enter_dispatched"].includes(String(submission?.status))) {
|
|
499
|
+
return current;
|
|
500
|
+
}
|
|
501
|
+
const requestText = String(takeoverFor(current)?.terminal_bridge_request_text ??
|
|
502
|
+
current.user_request ?? "");
|
|
503
|
+
const uncertain = applyTerminalBridgeSubmission({
|
|
504
|
+
conversation: {
|
|
505
|
+
...current,
|
|
506
|
+
status: "stalled",
|
|
507
|
+
stalled_at: uncertainAt,
|
|
508
|
+
stalled_reason: input.reason,
|
|
509
|
+
updated_at: uncertainAt
|
|
510
|
+
},
|
|
511
|
+
messageId,
|
|
512
|
+
requestText,
|
|
513
|
+
status: "uncertain",
|
|
514
|
+
preparedAt: nonBlankString(submission?.prepared_at) ?? uncertainAt,
|
|
515
|
+
textInjectedAt: nonBlankString(submission?.text_injected_at),
|
|
516
|
+
enterDispatchedAt: nonBlankString(submission?.enter_dispatched_at),
|
|
517
|
+
uncertainAt,
|
|
518
|
+
error: input.reason,
|
|
519
|
+
lastProvenStage: submission?.status === "enter_dispatched"
|
|
520
|
+
? "enter_dispatched"
|
|
521
|
+
: "text_injected"
|
|
522
|
+
}, receiptContext(current, this.#dependencies.authority.terminalControl(takeoverFor(current))));
|
|
523
|
+
this.#persistUncertainLedger(input, messageId, uncertainAt);
|
|
524
|
+
saveState(input.statePath, uncertain);
|
|
525
|
+
appendEvent(input.logPath, {
|
|
526
|
+
ts: uncertainAt,
|
|
527
|
+
conversation_id: uncertain.conversation_id,
|
|
528
|
+
event: "terminal_message_acceptance_uncertain",
|
|
529
|
+
message_id: messageId,
|
|
530
|
+
terminal_control: input.terminalControl,
|
|
531
|
+
error: textSummary(input.reason),
|
|
532
|
+
do_not_retry: true
|
|
533
|
+
});
|
|
534
|
+
return uncertain;
|
|
535
|
+
}
|
|
536
|
+
#persistUncertainLedger(input, messageId, uncertainAt) {
|
|
537
|
+
const ledger = this.#dependencies.repository.loadLedger(input.terminalControl);
|
|
538
|
+
if (nonBlankString(ledger?.message_id) === messageId &&
|
|
539
|
+
["text_injected", "enter_dispatched"].includes(String(ledger?.status))) {
|
|
540
|
+
this.#dependencies.repository.saveLedger(input.terminalControl, {
|
|
541
|
+
...ledger,
|
|
542
|
+
status: "uncertain",
|
|
543
|
+
uncertain_at: uncertainAt,
|
|
544
|
+
error: textSummary(input.reason),
|
|
545
|
+
dispatcher_pid: null
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
storeDirForConversation(conversation) {
|
|
550
|
+
const explicit = nonBlankString(conversation.store_dir);
|
|
551
|
+
if (explicit)
|
|
552
|
+
return explicit;
|
|
553
|
+
const statePath = nonBlankString(conversation.state_path);
|
|
554
|
+
return statePath
|
|
555
|
+
? pathsForConversationDir(path.dirname(statePath)).storeDir
|
|
556
|
+
: undefined;
|
|
557
|
+
}
|
|
558
|
+
refineSessionIdentity(input) {
|
|
559
|
+
const binding = input.session.binding;
|
|
560
|
+
if (input.session.status !== "bound" || !binding)
|
|
561
|
+
return input.session;
|
|
562
|
+
if (!terminalControlsShareIncarnation(binding.terminal_control, input.terminalControl) ||
|
|
563
|
+
(input.identity && binding.native_thread_id &&
|
|
564
|
+
binding.native_thread_id !== input.identity.sessionId)) {
|
|
565
|
+
throw new Error(`managed Session ${input.session.session_id} changed native identity before send`);
|
|
566
|
+
}
|
|
567
|
+
const refined = refinedSessionBinding(input);
|
|
568
|
+
if (!refined.changed)
|
|
569
|
+
return input.session;
|
|
570
|
+
const now = cliNow().toISOString();
|
|
571
|
+
return saveManagedSession(input.storeDir, {
|
|
572
|
+
...input.session,
|
|
573
|
+
binding: {
|
|
574
|
+
...binding,
|
|
575
|
+
...(refined.endpoint ? { terminal_endpoint: refined.endpoint } : {}),
|
|
576
|
+
native_thread_id: refined.nativeThreadId,
|
|
577
|
+
native_process: {
|
|
578
|
+
...binding.native_process,
|
|
579
|
+
process_uuid: refined.processUuid,
|
|
580
|
+
process_birth: refined.processBirth,
|
|
581
|
+
rollout: refined.rollout,
|
|
582
|
+
evidence: input.identity?.evidence ?? binding.native_process.evidence
|
|
583
|
+
},
|
|
584
|
+
last_verified_at: now
|
|
585
|
+
},
|
|
586
|
+
updated_at: now
|
|
587
|
+
}, { expectedRevision: managedSessionRevision(input.session) });
|
|
588
|
+
}
|
|
589
|
+
persistSessionIdentity(input) {
|
|
590
|
+
const boundary = this.#managedSessionIdentityBoundary(input.conversation, input.storeDir, "managed Session native identity escaped its exact Store writer");
|
|
591
|
+
if (!boundary)
|
|
592
|
+
return undefined;
|
|
593
|
+
const canonicalStoreDir = path.resolve(input.storeDir);
|
|
594
|
+
const current = tryLoadManagedSession(canonicalStoreDir, boundary.sessionId);
|
|
595
|
+
assertSessionIdentityCommitAuthority(current, boundary, input);
|
|
596
|
+
const binding = current.binding;
|
|
597
|
+
if (binding.native_thread_id &&
|
|
598
|
+
binding.native_thread_id !== input.identity.sessionId) {
|
|
599
|
+
throw new Error(`managed Session ${boundary.sessionId} expected native thread ` +
|
|
600
|
+
`${binding.native_thread_id}, observed ${input.identity.sessionId}`);
|
|
601
|
+
}
|
|
602
|
+
const now = cliNow().toISOString();
|
|
603
|
+
const next = {
|
|
604
|
+
...current,
|
|
605
|
+
binding: {
|
|
606
|
+
...binding,
|
|
607
|
+
native_thread_id: input.identity.sessionId,
|
|
608
|
+
native_process: {
|
|
609
|
+
...binding.native_process,
|
|
610
|
+
process_uuid: input.identity.processUuid,
|
|
611
|
+
process_birth: input.identity.processBirth,
|
|
612
|
+
rollout: input.identity.rollout,
|
|
613
|
+
evidence: input.identity.evidence
|
|
614
|
+
},
|
|
615
|
+
last_verified_at: now
|
|
616
|
+
},
|
|
617
|
+
updated_at: now
|
|
618
|
+
};
|
|
619
|
+
return saveManagedSession(canonicalStoreDir, next, {
|
|
620
|
+
expectedRevision: managedSessionRevision(current)
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
#managedSessionIdentityBoundary(conversation, storeDir, escapedStoreMessage) {
|
|
624
|
+
const bindingId = nonBlankString(conversation.terminal_binding_id);
|
|
625
|
+
const bindingGeneration = safeInteger(conversation.terminal_binding_generation);
|
|
626
|
+
const conversationStoreDir = this.storeDirForConversation(conversation);
|
|
627
|
+
if (!bindingId || bindingGeneration === undefined || !conversationStoreDir) {
|
|
628
|
+
return undefined;
|
|
629
|
+
}
|
|
630
|
+
if (path.resolve(conversationStoreDir) !== path.resolve(storeDir)) {
|
|
631
|
+
throw new Error(escapedStoreMessage);
|
|
632
|
+
}
|
|
633
|
+
return {
|
|
634
|
+
sessionId: sessionIdForConversation(conversation),
|
|
635
|
+
bindingId,
|
|
636
|
+
bindingGeneration
|
|
637
|
+
};
|
|
638
|
+
}
|
|
639
|
+
quarantineSession(input) {
|
|
640
|
+
const boundary = this.#managedSessionIdentityBoundary(input.conversation, input.storeDir, "managed Session quarantine escaped its exact Store writer");
|
|
641
|
+
if (!boundary)
|
|
642
|
+
return;
|
|
643
|
+
const canonicalStoreDir = path.resolve(input.storeDir);
|
|
644
|
+
const current = tryLoadManagedSession(canonicalStoreDir, boundary.sessionId);
|
|
645
|
+
if (!current?.binding ||
|
|
646
|
+
current.binding.binding_id !== boundary.bindingId ||
|
|
647
|
+
current.binding.generation !== boundary.bindingGeneration) {
|
|
648
|
+
return;
|
|
649
|
+
}
|
|
650
|
+
const now = cliNow().toISOString();
|
|
651
|
+
saveManagedSession(canonicalStoreDir, {
|
|
652
|
+
...current,
|
|
653
|
+
status: "quarantined",
|
|
654
|
+
quarantine_reason: input.reason.slice(0, 2000),
|
|
655
|
+
updated_at: now
|
|
656
|
+
}, { expectedRevision: managedSessionRevision(current) });
|
|
657
|
+
}
|
|
658
|
+
turnsForSession(storeDir, sessionId) {
|
|
659
|
+
return listConversations(storeDir)
|
|
660
|
+
.filter(this.#dependencies.authority.isDiscoverableTurn)
|
|
661
|
+
.filter((conversation) => sessionIdForConversation(conversation) === sessionId)
|
|
662
|
+
.sort(compareManagedConversationRecency);
|
|
663
|
+
}
|
|
664
|
+
assertSessionCanStartTurn(turns) {
|
|
665
|
+
assertManagedSessionCanStartTurnPolicy(turns, (conversation) => isSessionSendBlockingStatus(conversation.status));
|
|
666
|
+
}
|
|
667
|
+
turnMatchesTerminal(input) {
|
|
668
|
+
const storedControl = this.#dependencies.authority.terminalControl(takeoverFor(input.conversation));
|
|
669
|
+
return managedTurnMatchesTerminal({
|
|
670
|
+
...input,
|
|
671
|
+
storedControlExists: storedControl !== undefined,
|
|
672
|
+
terminalIncarnationMatches: Boolean(storedControl &&
|
|
673
|
+
terminalControlsShareIncarnation(storedControl, input.terminal.terminalControl)),
|
|
674
|
+
workspaceMatches: this.#dependencies.authority.workspaceMatches(input.conversation.workspace, input.terminal.terminalControl.currentPath)
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
createManagedTurn(input) {
|
|
678
|
+
const workspace = input.terminalControl.currentPath ?? cliCwd();
|
|
679
|
+
const storeDir = expandHome(input.options.storeDir ?? input.options.logDir ?? defaultStoreDir(workspace));
|
|
680
|
+
const executor = input.previousTurn
|
|
681
|
+
? executorForConversation(input.previousTurn)
|
|
682
|
+
: resolveExecutor({ kind: input.agent, session: input.conversationId });
|
|
683
|
+
const now = cliNow();
|
|
684
|
+
const base = createConversation({
|
|
685
|
+
userRequest: input.messageBody,
|
|
686
|
+
sessionId: input.managedSession?.session_id ??
|
|
687
|
+
(input.previousTurn
|
|
688
|
+
? sessionIdForConversation(input.previousTurn)
|
|
689
|
+
: undefined),
|
|
690
|
+
workspace,
|
|
691
|
+
openclawSession: input.options.openclawSession ??
|
|
692
|
+
input.previousTurn?.openclaw_session ?? "agent:main:main",
|
|
693
|
+
executorKind: executor.kind,
|
|
694
|
+
executorSession: executor.session,
|
|
695
|
+
softLimit: Number(input.options.softLimit ?? 50),
|
|
696
|
+
hardLimit: Number(input.options.hardLimit ?? 100),
|
|
697
|
+
now
|
|
698
|
+
});
|
|
699
|
+
const paths = pathsForConversation(base.conversation_id, storeDir);
|
|
700
|
+
const attached = attachManagedTurn(base, input, executor, now, paths);
|
|
701
|
+
const message = createMessage({
|
|
702
|
+
conversation: attached,
|
|
703
|
+
id: nonBlankString(input.options.messageId),
|
|
704
|
+
from: "openclaw",
|
|
705
|
+
to: executor.actor,
|
|
706
|
+
type: (input.options.type ?? "task"),
|
|
707
|
+
body: input.messageBody,
|
|
708
|
+
metadata: {
|
|
709
|
+
executor_kind: executor.kind,
|
|
710
|
+
executor_session: executor.session,
|
|
711
|
+
source_conversation_id: input.conversationId
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
return {
|
|
715
|
+
conversation: attached,
|
|
716
|
+
nextConversation: applyMessageToConversation(attached, message),
|
|
717
|
+
statePath: paths.statePath,
|
|
718
|
+
logPath: paths.logPath,
|
|
719
|
+
executor,
|
|
720
|
+
message
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
function companionIdentity(identity, evidence) {
|
|
725
|
+
return { ...identity, evidence };
|
|
726
|
+
}
|
|
727
|
+
function takeoverFor(conversation) {
|
|
728
|
+
return isRecord(conversation.native_session_takeover)
|
|
729
|
+
? conversation.native_session_takeover
|
|
730
|
+
: undefined;
|
|
731
|
+
}
|
|
732
|
+
function safeInteger(value) {
|
|
733
|
+
const number = Number(value);
|
|
734
|
+
return Number.isSafeInteger(number) ? number : undefined;
|
|
735
|
+
}
|
|
736
|
+
function numericValue(value) {
|
|
737
|
+
return typeof value === "number" && Number.isFinite(value)
|
|
738
|
+
? value
|
|
739
|
+
: undefined;
|
|
740
|
+
}
|
|
741
|
+
function acceptanceFacts(conversation) {
|
|
742
|
+
const takeover = takeoverFor(conversation);
|
|
743
|
+
const submission = terminalBridgeSubmission(conversation);
|
|
744
|
+
const anchor = isRecord(takeover?.codex_rollout_acceptance_anchor)
|
|
745
|
+
? takeover.codex_rollout_acceptance_anchor
|
|
746
|
+
: undefined;
|
|
747
|
+
return {
|
|
748
|
+
turnStatus: conversation.status,
|
|
749
|
+
messageId: nonBlankString(submission?.message_id),
|
|
750
|
+
submissionStatus: nonBlankString(submission?.status),
|
|
751
|
+
requestText: String(takeover?.terminal_bridge_request_text ?? ""),
|
|
752
|
+
enterDispatchedAtMs: validTerminalMonitorTimestampMs(submission?.enter_dispatched_at),
|
|
753
|
+
codexAnchorVersion: numericValue(anchor?.version)
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
function assertDeferredAbortReceipt(conversation, transfer) {
|
|
757
|
+
const submission = terminalBridgeSubmission(conversation);
|
|
758
|
+
if (conversation.conversation_id !== transfer.turn_id ||
|
|
759
|
+
nonBlankString(submission?.message_id) !== transfer.message_id ||
|
|
760
|
+
submission?.status !== "aborted" ||
|
|
761
|
+
submission.safe_to_retry !== true) {
|
|
762
|
+
throw new Error(`deferred foreground transfer ${transfer.transfer_id} resolved its ` +
|
|
763
|
+
"abort without an exact zero-input Turn receipt");
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
function deferredAcceptedIdentity(transfer) {
|
|
767
|
+
const binding = transfer.target_accepted_binding;
|
|
768
|
+
const rollout = binding?.native_process.rollout;
|
|
769
|
+
if (!binding ||
|
|
770
|
+
!transfer.target_native_thread_id ||
|
|
771
|
+
binding.native_thread_id !== transfer.target_native_thread_id ||
|
|
772
|
+
!isCompleteNativeRollout(rollout)) {
|
|
773
|
+
throw new Error(`deferred foreground transfer ${transfer.transfer_id} resolved ` +
|
|
774
|
+
"without an exact accepted native binding");
|
|
775
|
+
}
|
|
776
|
+
return {
|
|
777
|
+
sessionId: transfer.target_native_thread_id,
|
|
778
|
+
processUuid: binding.native_process.process_uuid,
|
|
779
|
+
processBirth: binding.native_process.process_birth,
|
|
780
|
+
rollout,
|
|
781
|
+
evidence: binding.native_process.evidence
|
|
782
|
+
};
|
|
783
|
+
}
|
|
784
|
+
function assertAcceptanceGeneration(current, expectedTurn, facts) {
|
|
785
|
+
const submission = terminalBridgeSubmission(current);
|
|
786
|
+
if (current.status !== expectedTurn.status ||
|
|
787
|
+
nonBlankString(takeoverFor(current)?.terminal_bridge_message_id) !==
|
|
788
|
+
facts.messageId ||
|
|
789
|
+
nonBlankString(submission?.message_id) !== facts.messageId ||
|
|
790
|
+
submission?.status !== "enter_dispatched") {
|
|
791
|
+
throw new Error("terminal acceptance generation changed before monitor reconciliation");
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
function resolvedAcceptanceConversation(current, expected, resolution, resolvedAt, storeDir, terminalControl) {
|
|
795
|
+
const submission = terminalBridgeSubmission(current);
|
|
796
|
+
const base = resolution.outcome === "not_accepted"
|
|
797
|
+
? {
|
|
798
|
+
...current,
|
|
799
|
+
status: "stalled",
|
|
800
|
+
stalled_at: resolvedAt,
|
|
801
|
+
stalled_reason: resolution.reason,
|
|
802
|
+
updated_at: resolvedAt
|
|
803
|
+
}
|
|
804
|
+
: current;
|
|
805
|
+
return applyTerminalBridgeSubmission({
|
|
806
|
+
conversation: base,
|
|
807
|
+
messageId: expected.messageId,
|
|
808
|
+
requestText: String(takeoverFor(current)?.terminal_bridge_request_text ??
|
|
809
|
+
current.user_request ?? ""),
|
|
810
|
+
status: resolution.outcome,
|
|
811
|
+
preparedAt: nonBlankString(submission?.prepared_at) ?? resolvedAt,
|
|
812
|
+
textInjectedAt: nonBlankString(submission?.text_injected_at),
|
|
813
|
+
enterDispatchedAt: nonBlankString(submission?.enter_dispatched_at),
|
|
814
|
+
...(resolution.outcome === "not_accepted"
|
|
815
|
+
? { notAcceptedAt: resolvedAt }
|
|
816
|
+
: {
|
|
817
|
+
agentAcceptedAt: resolvedAt,
|
|
818
|
+
acceptanceEvidence: resolution.evidence
|
|
819
|
+
})
|
|
820
|
+
}, {
|
|
821
|
+
dispatcherPid: cliPid(),
|
|
822
|
+
storeDir,
|
|
823
|
+
terminalControl
|
|
824
|
+
});
|
|
825
|
+
}
|
|
826
|
+
function receiptContext(conversation, terminalControl) {
|
|
827
|
+
const explicit = nonBlankString(conversation.store_dir);
|
|
828
|
+
const statePath = nonBlankString(conversation.state_path);
|
|
829
|
+
return {
|
|
830
|
+
dispatcherPid: cliPid(),
|
|
831
|
+
storeDir: explicit ?? (statePath
|
|
832
|
+
? pathsForConversationDir(path.dirname(statePath)).storeDir
|
|
833
|
+
: undefined),
|
|
834
|
+
terminalControl
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
function refinedSessionBinding(input) {
|
|
838
|
+
const binding = input.session.binding;
|
|
839
|
+
const nativeThreadId = binding.native_thread_id ?? input.identity?.sessionId;
|
|
840
|
+
const processUuid = binding.native_process.process_uuid ??
|
|
841
|
+
input.identity?.processUuid;
|
|
842
|
+
const processBirth = binding.native_process.process_birth ??
|
|
843
|
+
input.identity?.processBirth;
|
|
844
|
+
const rollout = binding.native_process.rollout ?? input.identity?.rollout;
|
|
845
|
+
const endpoint = binding.terminal_endpoint ??
|
|
846
|
+
(hasCanonicalTerminalEndpoint(input.terminalControl)
|
|
847
|
+
? terminalControlEvidence(input.terminalControl)
|
|
848
|
+
: undefined);
|
|
849
|
+
return {
|
|
850
|
+
nativeThreadId,
|
|
851
|
+
processUuid,
|
|
852
|
+
processBirth,
|
|
853
|
+
rollout,
|
|
854
|
+
endpoint,
|
|
855
|
+
changed: nativeThreadId !== binding.native_thread_id ||
|
|
856
|
+
processUuid !== binding.native_process.process_uuid ||
|
|
857
|
+
processBirth !== binding.native_process.process_birth ||
|
|
858
|
+
JSON.stringify(rollout) !== JSON.stringify(binding.native_process.rollout) ||
|
|
859
|
+
endpoint !== binding.terminal_endpoint
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
function assertSessionIdentityCommitAuthority(current, boundary, input) {
|
|
863
|
+
if (!current ||
|
|
864
|
+
current.status !== "bound" ||
|
|
865
|
+
!current.binding ||
|
|
866
|
+
current.binding.binding_id !== boundary.bindingId ||
|
|
867
|
+
current.binding.generation !== boundary.bindingGeneration ||
|
|
868
|
+
!terminalControlsShareIncarnation(current.binding.terminal_control, input.terminalControl)) {
|
|
869
|
+
throw new Error(`managed Session ${boundary.sessionId} binding changed before native identity commit`);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
function attachManagedTurn(base, input, executor, now, paths) {
|
|
873
|
+
const previousTakeover = input.previousTurn
|
|
874
|
+
? takeoverFor(input.previousTurn)
|
|
875
|
+
: undefined;
|
|
876
|
+
const nativeProcess = input.managedSession?.binding?.native_process;
|
|
877
|
+
return withStoragePaths({
|
|
878
|
+
...base,
|
|
879
|
+
terminal_binding_id: input.managedSession?.binding?.binding_id,
|
|
880
|
+
terminal_binding_generation: input.managedSession?.binding?.generation,
|
|
881
|
+
native_thread_id: input.managedSession?.binding?.native_thread_id ??
|
|
882
|
+
input.nativeAgentIdentity?.sessionId,
|
|
883
|
+
executor,
|
|
884
|
+
status: "idle",
|
|
885
|
+
idle_since: now.toISOString(),
|
|
886
|
+
updated_at: now.toISOString(),
|
|
887
|
+
gateway_url: input.options.gatewayUrl ?? input.previousTurn?.gateway_url ??
|
|
888
|
+
"ws://127.0.0.1:18789",
|
|
889
|
+
gateway_method: input.options.gatewayMethod ??
|
|
890
|
+
input.previousTurn?.gateway_method,
|
|
891
|
+
gateway_session: input.options.gatewaySession ??
|
|
892
|
+
input.options.openclawSession ?? input.previousTurn?.gateway_session ??
|
|
893
|
+
input.previousTurn?.openclaw_session ?? "agent:main:main",
|
|
894
|
+
openclaw_bin: input.options.openclawBin ??
|
|
895
|
+
input.previousTurn?.openclaw_bin ?? resolveOptionalExecutable("openclaw"),
|
|
896
|
+
native_session_takeover: {
|
|
897
|
+
agent: input.agent,
|
|
898
|
+
terminal_agent_identity_protocol: 1,
|
|
899
|
+
native_session_id: input.conversationId,
|
|
900
|
+
terminal_agent_pid: input.pid,
|
|
901
|
+
terminal_agent_session_id: input.nativeAgentIdentity?.sessionId,
|
|
902
|
+
terminal_agent_expected_session_id: input.managedSession?.binding?.native_thread_id,
|
|
903
|
+
terminal_binding_id: input.managedSession?.binding?.binding_id,
|
|
904
|
+
terminal_binding_generation: input.managedSession?.binding?.generation,
|
|
905
|
+
terminal_agent_process_uuid: input.nativeAgentIdentity?.processUuid ??
|
|
906
|
+
nativeProcess?.process_uuid,
|
|
907
|
+
terminal_agent_process_birth: input.nativeAgentIdentity?.processBirth ??
|
|
908
|
+
nativeProcess?.process_birth,
|
|
909
|
+
terminal_agent_rollout: input.nativeAgentIdentity?.rollout ??
|
|
910
|
+
nativeProcess?.rollout,
|
|
911
|
+
terminal_agent_identity_evidence: input.nativeAgentIdentity?.evidence ??
|
|
912
|
+
nativeProcess?.evidence,
|
|
913
|
+
source_cwd: base.workspace,
|
|
914
|
+
source_title: `Terminal-controlled ${executor.display_name} ${input.terminalControl.target}`,
|
|
915
|
+
strategy: "terminal_control",
|
|
916
|
+
attached_at: nonBlankString(previousTakeover?.attached_at) ??
|
|
917
|
+
now.toISOString(),
|
|
918
|
+
takeover_match_kind: input.previousTurn
|
|
919
|
+
? "managed_session_send"
|
|
920
|
+
: "raw_terminal_send",
|
|
921
|
+
...terminalEndpointTakeoverFields(input.terminalControl),
|
|
922
|
+
needs_bootstrap: false,
|
|
923
|
+
terminal_bridge: true,
|
|
924
|
+
...(input.deferredForegroundTransferId
|
|
925
|
+
? { deferred_foreground_transfer_id: input.deferredForegroundTransferId }
|
|
926
|
+
: {})
|
|
927
|
+
}
|
|
928
|
+
}, paths);
|
|
929
|
+
}
|
|
930
|
+
function terminalEndpointTakeoverFields(terminalControl) {
|
|
931
|
+
return {
|
|
932
|
+
terminal_control: terminalControl,
|
|
933
|
+
...(hasCanonicalTerminalEndpoint(terminalControl)
|
|
934
|
+
? { terminal_endpoint: terminalControlEvidence(terminalControl) }
|
|
935
|
+
: {})
|
|
936
|
+
};
|
|
937
|
+
}
|
|
938
|
+
function withStoragePaths(conversation, paths) {
|
|
939
|
+
return {
|
|
940
|
+
...conversation,
|
|
941
|
+
store_dir: paths.storeDir,
|
|
942
|
+
conversation_dir: paths.conversationDir,
|
|
943
|
+
event_log_path: paths.logPath,
|
|
944
|
+
state_path: paths.statePath
|
|
945
|
+
};
|
|
946
|
+
}
|
|
947
|
+
function textSummary(text, maxLength = 240) {
|
|
948
|
+
const value = String(text ?? "");
|
|
949
|
+
return {
|
|
950
|
+
length: value.length,
|
|
951
|
+
preview: value ? value.slice(0, maxLength) : undefined
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
function errorText(error) {
|
|
955
|
+
return error instanceof Error ? error.message : String(error);
|
|
956
|
+
}
|
|
957
|
+
//# sourceMappingURL=terminal-acceptance-cli-adapter.js.map
|