nova-personal-agent-server 0.0.0-stage → 0.4.0
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/LICENSE +201 -0
- package/README.md +30 -2
- package/bin/novapersonal-server.mjs +4 -0
- package/package.json +61 -4
- package/resources/endpointing/volcengine-v1/LICENSE.silero-vad.txt +21 -0
- package/resources/endpointing/volcengine-v1/MANIFEST.json +31 -0
- package/resources/endpointing/volcengine-v1/silence-16k-s16le.pcm +0 -0
- package/resources/endpointing/volcengine-v1/speech-16k-s16le.pcm +0 -0
- package/resources/native/codex-sandbox-probe +0 -0
- package/resources/native/project-native/nova_project_native.node +0 -0
- package/resources/native-resources-v1.json +1 -0
- package/runtime/dist/eval/demos.js +156 -0
- package/runtime/dist/eval/fixture-host.js +385 -0
- package/runtime/dist/eval/fixtures.js +319 -0
- package/runtime/dist/eval/realtime/session-fixtures.js +474 -0
- package/runtime/dist/eval/scorecard.js +285 -0
- package/runtime/dist/eval/sim.js +43 -0
- package/runtime/dist/src/cascaded-text-provider.js +31 -0
- package/runtime/dist/src/cli.js +96 -0
- package/runtime/dist/src/composition/assembly.js +352 -0
- package/runtime/dist/src/composition/cascaded-realtime-assembly.js +455 -0
- package/runtime/dist/src/composition/production-composition.js +389 -0
- package/runtime/dist/src/composition/realtime-assembly.js +1330 -0
- package/runtime/dist/src/config/capability-registry.js +315 -0
- package/runtime/dist/src/config/cascaded-realtime-config.js +146 -0
- package/runtime/dist/src/config/coding-backends.js +8 -0
- package/runtime/dist/src/config/coding-profiles.js +98 -0
- package/runtime/dist/src/config/config.js +857 -0
- package/runtime/dist/src/config/diagnostics.js +139 -0
- package/runtime/dist/src/config/environment-contract.js +202 -0
- package/runtime/dist/src/config/self-hosted.js +23 -0
- package/runtime/dist/src/connectors/composio/client.js +195 -0
- package/runtime/dist/src/connectors/composio/google.js +208 -0
- package/runtime/dist/src/connectors/composio/index.js +334 -0
- package/runtime/dist/src/connectors/feishu/cli.js +220 -0
- package/runtime/dist/src/connectors/feishu/index.js +980 -0
- package/runtime/dist/src/connectors/macos/calendar.js +35 -0
- package/runtime/dist/src/connectors/macos/mail.js +32 -0
- package/runtime/dist/src/connectors/macos/reader.js +62 -0
- package/runtime/dist/src/conversation-voice-provider.js +41 -0
- package/runtime/dist/src/core/approval-port.js +9 -0
- package/runtime/dist/src/core/approval.js +1328 -0
- package/runtime/dist/src/core/calls.js +35 -0
- package/runtime/dist/src/core/camera-session.js +32 -0
- package/runtime/dist/src/core/causal-runtime.js +650 -0
- package/runtime/dist/src/core/clock.js +139 -0
- package/runtime/dist/src/core/context-view.js +156 -0
- package/runtime/dist/src/core/effects.js +62 -0
- package/runtime/dist/src/core/events.js +217 -0
- package/runtime/dist/src/core/executor-diagnostic.js +9 -0
- package/runtime/dist/src/core/ids.js +32 -0
- package/runtime/dist/src/core/media-store.js +104 -0
- package/runtime/dist/src/core/memory.js +296 -0
- package/runtime/dist/src/core/pending-decision.js +64 -0
- package/runtime/dist/src/core/ports.js +125 -0
- package/runtime/dist/src/core/runtime.js +1459 -0
- package/runtime/dist/src/core/schedule-tools.js +15 -0
- package/runtime/dist/src/core/sims.js +186 -0
- package/runtime/dist/src/core/slots.js +97 -0
- package/runtime/dist/src/core/source-processing-instructions.js +3 -0
- package/runtime/dist/src/core/source-processing-tools.js +12 -0
- package/runtime/dist/src/core/suggestions.js +143 -0
- package/runtime/dist/src/core/task-tools.js +22 -0
- package/runtime/dist/src/core/tool-schema.js +212 -0
- package/runtime/dist/src/core/trace.js +34 -0
- package/runtime/dist/src/core/work-tools.js +94 -0
- package/runtime/dist/src/desktop/desktop-camera-source.js +31 -0
- package/runtime/dist/src/desktop/desktop-camera.js +281 -0
- package/runtime/dist/src/desktop/desktop-control.js +140 -0
- package/runtime/dist/src/desktop/desktop-progress.js +162 -0
- package/runtime/dist/src/desktop/desktop-session.js +2106 -0
- package/runtime/dist/src/desktop/desktop-tasks.js +80 -0
- package/runtime/dist/src/desktop/desktop-wire.js +605 -0
- package/runtime/dist/src/desktop/phone-desktop-entry.js +17 -0
- package/runtime/dist/src/desktop/startup-error.js +18 -0
- package/runtime/dist/src/desktop/workbench-acceptance.js +262 -0
- package/runtime/dist/src/desktop-entry.js +106 -0
- package/runtime/dist/src/desktop.js +1092 -0
- package/runtime/dist/src/executors/acp/backends.js +105 -0
- package/runtime/dist/src/executors/acp/mcp.js +81 -0
- package/runtime/dist/src/executors/acp/preflight.js +14 -0
- package/runtime/dist/src/executors/acp/process-owner.js +112 -0
- package/runtime/dist/src/executors/acp/transport.js +760 -0
- package/runtime/dist/src/executors/acp/windows-launch.js +68 -0
- package/runtime/dist/src/executors/agent-controller.js +244 -0
- package/runtime/dist/src/executors/autoglm.js +376 -0
- package/runtime/dist/src/executors/browser-device.js +102 -0
- package/runtime/dist/src/executors/browser.js +61 -0
- package/runtime/dist/src/executors/camera.js +101 -0
- package/runtime/dist/src/executors/chromium-frame-source.js +173 -0
- package/runtime/dist/src/executors/codex/adapter-live.js +292 -0
- package/runtime/dist/src/executors/codex/adapter-project.js +1306 -0
- package/runtime/dist/src/executors/codex/app-server-schema.js +712 -0
- package/runtime/dist/src/executors/codex/app-server-transport.js +1798 -0
- package/runtime/dist/src/executors/codex/approval-protocol.js +439 -0
- package/runtime/dist/src/executors/codex/common.js +655 -0
- package/runtime/dist/src/executors/codex/contract.js +581 -0
- package/runtime/dist/src/executors/codex/controller.js +141 -0
- package/runtime/dist/src/executors/codex/credential-snapshot.js +195 -0
- package/runtime/dist/src/executors/codex/factory.js +463 -0
- package/runtime/dist/src/executors/codex/host-config.js +238 -0
- package/runtime/dist/src/executors/codex/host.js +17 -0
- package/runtime/dist/src/executors/codex/index.js +19 -0
- package/runtime/dist/src/executors/codex/launch-profile.js +37 -0
- package/runtime/dist/src/executors/codex/local-sessions.js +119 -0
- package/runtime/dist/src/executors/codex/managed-mcp.js +196 -0
- package/runtime/dist/src/executors/codex/process-owner.js +398 -0
- package/runtime/dist/src/executors/codex/production-host.js +887 -0
- package/runtime/dist/src/executors/codex/progress-instructions.js +10 -0
- package/runtime/dist/src/executors/codex/protocol.js +581 -0
- package/runtime/dist/src/executors/codex/safe-json.js +87 -0
- package/runtime/dist/src/executors/codex/session-title.js +61 -0
- package/runtime/dist/src/executors/codex/shared-home.js +29 -0
- package/runtime/dist/src/executors/codex/spawn-env.js +162 -0
- package/runtime/dist/src/executors/codex/turn-projection.js +435 -0
- package/runtime/dist/src/executors/codex/version.js +38 -0
- package/runtime/dist/src/executors/codex/windows-guardian.js +716 -0
- package/runtime/dist/src/executors/coding/intake-model.js +103 -0
- package/runtime/dist/src/executors/coding/intake.js +824 -0
- package/runtime/dist/src/executors/coding/json-completion.js +14 -0
- package/runtime/dist/src/executors/coding/target-resolution.js +61 -0
- package/runtime/dist/src/executors/coding/work-order.js +50 -0
- package/runtime/dist/src/executors/coding-executor.js +23 -0
- package/runtime/dist/src/executors/computer-backend.js +188 -0
- package/runtime/dist/src/executors/computer-device.js +187 -0
- package/runtime/dist/src/executors/computer-worker.js +125 -0
- package/runtime/dist/src/executors/computer.js +100 -0
- package/runtime/dist/src/executors/device-lock.js +127 -0
- package/runtime/dist/src/executors/frame-source.js +29 -0
- package/runtime/dist/src/executors/gui-evidence.js +30 -0
- package/runtime/dist/src/executors/gui-execution.js +237 -0
- package/runtime/dist/src/executors/gui-runner.js +88 -0
- package/runtime/dist/src/executors/host-executor-capability.js +12 -0
- package/runtime/dist/src/executors/index.js +13 -0
- package/runtime/dist/src/executors/mcp-client.js +198 -0
- package/runtime/dist/src/executors/mcp.js +210 -0
- package/runtime/dist/src/executors/mobile-android.js +180 -0
- package/runtime/dist/src/executors/mobile-ios.js +303 -0
- package/runtime/dist/src/executors/mobile.js +78 -0
- package/runtime/dist/src/executors/search-mcp.js +146 -0
- package/runtime/dist/src/executors/search.js +524 -0
- package/runtime/dist/src/executors/task-resources.js +38 -0
- package/runtime/dist/src/executors/vision/assess.js +296 -0
- package/runtime/dist/src/executors/vision/controller-core.js +373 -0
- package/runtime/dist/src/executors/vision/controller.js +164 -0
- package/runtime/dist/src/executors/vision/lifecycle.js +69 -0
- package/runtime/dist/src/executors/vision.js +5 -0
- package/runtime/dist/src/executors/watcher.js +718 -0
- package/runtime/dist/src/http/bounded-response.js +37 -0
- package/runtime/dist/src/index.js +56 -0
- package/runtime/dist/src/knowledge/assembly.js +60 -0
- package/runtime/dist/src/knowledge/documents.js +520 -0
- package/runtime/dist/src/knowledge/embeddings.js +201 -0
- package/runtime/dist/src/knowledge/mcp.js +218 -0
- package/runtime/dist/src/knowledge/parse-worker.js +122 -0
- package/runtime/dist/src/knowledge/references.js +51 -0
- package/runtime/dist/src/knowledge/service.js +526 -0
- package/runtime/dist/src/knowledge/store-client.js +185 -0
- package/runtime/dist/src/knowledge/store-worker.js +843 -0
- package/runtime/dist/src/knowledge/types.js +2 -0
- package/runtime/dist/src/mcp/http-server.js +177 -0
- package/runtime/dist/src/mem0/resource.js +35 -0
- package/runtime/dist/src/mem0/store-worker.js +330 -0
- package/runtime/dist/src/memory/blackboard-session.js +205 -0
- package/runtime/dist/src/memory/blackboard-store.js +194 -0
- package/runtime/dist/src/memory/blackboard-worker.js +369 -0
- package/runtime/dist/src/memory/entry.js +39 -0
- package/runtime/dist/src/memory/factory.js +20 -0
- package/runtime/dist/src/memory/personal-memory-inspection.js +18 -0
- package/runtime/dist/src/memory/personal-memory.js +10 -0
- package/runtime/dist/src/memory/prerecall.js +20 -0
- package/runtime/dist/src/memory/provenance.js +17 -0
- package/runtime/dist/src/memory/remote-personal-memory.js +194 -0
- package/runtime/dist/src/memory/retrieval.js +235 -0
- package/runtime/dist/src/memory/sensitivity.js +261 -0
- package/runtime/dist/src/memory-ledger/models.js +98 -0
- package/runtime/dist/src/memory-ledger/store-client.js +210 -0
- package/runtime/dist/src/memory-ledger/store-worker.js +130 -0
- package/runtime/dist/src/memory-ledger/store.js +597 -0
- package/runtime/dist/src/memory-substrate/candidates.js +16 -0
- package/runtime/dist/src/memory-substrate/consolidation.js +124 -0
- package/runtime/dist/src/memory-substrate/file-authority.js +111 -0
- package/runtime/dist/src/memory-substrate/life-operations.js +280 -0
- package/runtime/dist/src/memory-substrate/markdown-repository.js +610 -0
- package/runtime/dist/src/memory-substrate/migration.js +87 -0
- package/runtime/dist/src/memory-substrate/profile-source-input.js +9 -0
- package/runtime/dist/src/memory-substrate/purge.js +398 -0
- package/runtime/dist/src/memory-substrate/resolution.js +32 -0
- package/runtime/dist/src/memory-substrate/resource.js +867 -0
- package/runtime/dist/src/memory-substrate/retrieval.js +85 -0
- package/runtime/dist/src/memory-substrate/source-operations.js +306 -0
- package/runtime/dist/src/memory-substrate/source-processing.js +326 -0
- package/runtime/dist/src/memory-substrate/source-state.js +93 -0
- package/runtime/dist/src/memory-substrate/store.js +534 -0
- package/runtime/dist/src/memory-substrate/workspace-projections.js +106 -0
- package/runtime/dist/src/memory-substrate/workspace.js +21 -0
- package/runtime/dist/src/model/jev-client.js +25 -0
- package/runtime/dist/src/model/model-adapters.js +64 -0
- package/runtime/dist/src/model/model-gateway.js +399 -0
- package/runtime/dist/src/model/model-lane.js +34 -0
- package/runtime/dist/src/model/model-purpose.js +5 -0
- package/runtime/dist/src/model/personal-writer.js +361 -0
- package/runtime/dist/src/model/proactivity.js +72 -0
- package/runtime/dist/src/model/prompting.js +279 -0
- package/runtime/dist/src/model/task-verifier.js +217 -0
- package/runtime/dist/src/model/vision-capability.js +10 -0
- package/runtime/dist/src/news/feeds.js +114 -0
- package/runtime/dist/src/news/jev-ranking.js +25 -0
- package/runtime/dist/src/news/ranking.js +26 -0
- package/runtime/dist/src/news/service.js +262 -0
- package/runtime/dist/src/notifications/adapters/feishu.js +10 -0
- package/runtime/dist/src/notifications/adapters/nova.js +15 -0
- package/runtime/dist/src/notifications/content.js +15 -0
- package/runtime/dist/src/notifications/contracts.js +9 -0
- package/runtime/dist/src/notifications/feed.js +8 -0
- package/runtime/dist/src/notifications/service.js +412 -0
- package/runtime/dist/src/notifications/store.js +4 -0
- package/runtime/dist/src/personal-agent/app-context.js +113 -0
- package/runtime/dist/src/personal-agent/approval-scope.js +8 -0
- package/runtime/dist/src/personal-agent/coding-targets.js +82 -0
- package/runtime/dist/src/personal-agent/context-candidates.js +94 -0
- package/runtime/dist/src/personal-agent/contracts.js +32 -0
- package/runtime/dist/src/personal-agent/conversation-runtime.js +536 -0
- package/runtime/dist/src/personal-agent/conversations.js +187 -0
- package/runtime/dist/src/personal-agent/daily-brief.js +53 -0
- package/runtime/dist/src/personal-agent/host.js +2403 -0
- package/runtime/dist/src/personal-agent/intake-task.js +37 -0
- package/runtime/dist/src/personal-agent/life.js +260 -0
- package/runtime/dist/src/personal-agent/memory-overview.js +19 -0
- package/runtime/dist/src/personal-agent/profile-sources.js +34 -0
- package/runtime/dist/src/personal-agent/profile-warmup.js +244 -0
- package/runtime/dist/src/personal-agent/project-digests.js +242 -0
- package/runtime/dist/src/personal-agent/sampling.js +20 -0
- package/runtime/dist/src/personal-agent/schedule-agent.js +60 -0
- package/runtime/dist/src/personal-agent/schedule-api.js +61 -0
- package/runtime/dist/src/personal-agent/schedule-presets.js +13 -0
- package/runtime/dist/src/personal-agent/schedule-reference.js +3 -0
- package/runtime/dist/src/personal-agent/scheduled-response-scope.js +38 -0
- package/runtime/dist/src/personal-agent/scheduled-run-authority.js +44 -0
- package/runtime/dist/src/personal-agent/scheduled-tasks.js +280 -0
- package/runtime/dist/src/personal-agent/scheduled-time.js +99 -0
- package/runtime/dist/src/personal-agent/scheduled-tool-runtime.js +109 -0
- package/runtime/dist/src/personal-agent/source-priority.js +142 -0
- package/runtime/dist/src/personal-agent/source-processing-agent.js +158 -0
- package/runtime/dist/src/personal-agent/source-walk.js +25 -0
- package/runtime/dist/src/personal-agent/sources.js +1484 -0
- package/runtime/dist/src/personal-agent/store-lock-worker.js +17 -0
- package/runtime/dist/src/personal-agent/store.js +146 -0
- package/runtime/dist/src/personal-agent/task-loop.js +163 -0
- package/runtime/dist/src/personal-agent/tasks.js +920 -0
- package/runtime/dist/src/personal-agent/turn-deadline.js +55 -0
- package/runtime/dist/src/personal-agent/understanding.js +160 -0
- package/runtime/dist/src/personal-agent/workbench-context.js +239 -0
- package/runtime/dist/src/projects/confirmed-project-capability.js +60 -0
- package/runtime/dist/src/projects/host-paths.js +133 -0
- package/runtime/dist/src/projects/managed-workspace-maintenance.js +351 -0
- package/runtime/dist/src/projects/project-confirmation.js +486 -0
- package/runtime/dist/src/projects/project-native-resource.js +284 -0
- package/runtime/dist/src/projects/project-node-files.js +169 -0
- package/runtime/dist/src/projects/project-root-file.js +16 -0
- package/runtime/dist/src/projects/project-state.js +504 -0
- package/runtime/dist/src/projects/project-store-files.js +1271 -0
- package/runtime/dist/src/projects/project-store.js +1345 -0
- package/runtime/dist/src/realtime/aoq.js +141 -0
- package/runtime/dist/src/realtime/bridge.js +652 -0
- package/runtime/dist/src/realtime/cascaded/adapter.js +1669 -0
- package/runtime/dist/src/realtime/cascaded/ark-llm.js +219 -0
- package/runtime/dist/src/realtime/cascaded/chat-completions-llm.js +478 -0
- package/runtime/dist/src/realtime/cascaded/gemini-speech.js +232 -0
- package/runtime/dist/src/realtime/cascaded/llm.js +37 -0
- package/runtime/dist/src/realtime/cascaded/ports.js +2 -0
- package/runtime/dist/src/realtime/cascaded/provider.js +216 -0
- package/runtime/dist/src/realtime/cascaded/qwen-llm.js +7 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-asr.js +110 -0
- package/runtime/dist/src/realtime/cascaded/self-hosted-tts.js +137 -0
- package/runtime/dist/src/realtime/cascaded/transcribe.js +42 -0
- package/runtime/dist/src/realtime/coding-progress-narration.js +28 -0
- package/runtime/dist/src/realtime/confirmation-turn-isolation.js +586 -0
- package/runtime/dist/src/realtime/dictation.js +28 -0
- package/runtime/dist/src/realtime/evidence.js +276 -0
- package/runtime/dist/src/realtime/floor.js +43 -0
- package/runtime/dist/src/realtime/frontend-instructions.js +195 -0
- package/runtime/dist/src/realtime/gemini.js +517 -0
- package/runtime/dist/src/realtime/history.js +122 -0
- package/runtime/dist/src/realtime/host-delivery.js +1711 -0
- package/runtime/dist/src/realtime/integrated-wire-profile.js +128 -0
- package/runtime/dist/src/realtime/memory-board.js +105 -0
- package/runtime/dist/src/realtime/openai-wire-profile.js +46 -0
- package/runtime/dist/src/realtime/playback.js +242 -0
- package/runtime/dist/src/realtime/project-confirmation-flow.js +1428 -0
- package/runtime/dist/src/realtime/prompt-language.js +17 -0
- package/runtime/dist/src/realtime/protocol.js +428 -0
- package/runtime/dist/src/realtime/provider-projection.js +526 -0
- package/runtime/dist/src/realtime/provider-session.js +491 -0
- package/runtime/dist/src/realtime/qwen-transport.js +144 -0
- package/runtime/dist/src/realtime/qwen.js +1377 -0
- package/runtime/dist/src/realtime/recall.js +341 -0
- package/runtime/dist/src/realtime/service-ports.js +8 -0
- package/runtime/dist/src/realtime/service-state.js +259 -0
- package/runtime/dist/src/realtime/service.js +1726 -0
- package/runtime/dist/src/realtime/session-state.js +684 -0
- package/runtime/dist/src/realtime/session.js +1976 -0
- package/runtime/dist/src/realtime/speech-prep.js +63 -0
- package/runtime/dist/src/realtime/streaming-speech.js +120 -0
- package/runtime/dist/src/realtime/system-prompts.en.js +136 -0
- package/runtime/dist/src/realtime/telemetry.js +144 -0
- package/runtime/dist/src/realtime/tool-continuations.js +1510 -0
- package/runtime/dist/src/realtime/transport.js +8 -0
- package/runtime/dist/src/realtime/usage.js +55 -0
- package/runtime/dist/src/realtime/user-origin-binding.js +173 -0
- package/runtime/dist/src/realtime/volcengine/ark.js +428 -0
- package/runtime/dist/src/realtime/volcengine/asr.js +573 -0
- package/runtime/dist/src/realtime/volcengine/audio.js +39 -0
- package/runtime/dist/src/realtime/volcengine/endpointing-capability.js +834 -0
- package/runtime/dist/src/realtime/volcengine/livekit-endpointing.js +714 -0
- package/runtime/dist/src/realtime/volcengine/local-eot-executor.js +33 -0
- package/runtime/dist/src/realtime/volcengine/protocol.js +247 -0
- package/runtime/dist/src/realtime/volcengine/silence-endpointing.js +237 -0
- package/runtime/dist/src/realtime/volcengine/tts.js +444 -0
- package/runtime/dist/src/realtime/volcengine/websocket.js +293 -0
- package/runtime/dist/src/server/aoq-chat-server.js +497 -0
- package/runtime/dist/src/server/client-pairing.js +192 -0
- package/runtime/dist/src/server/client-protocol.js +87 -0
- package/runtime/dist/src/server/client-server.js +312 -0
- package/runtime/dist/src/server/server-config.js +72 -0
- package/runtime/dist/src/server-entry.js +135 -0
- package/runtime/dist/src/storage/bounded-json.js +63 -0
- package/runtime/dist/src/storage/native-file-lock.js +8 -0
- package/runtime/dist/src/storage/native-resource-snapshot.js +41 -0
- package/runtime/dist/src/storage/private-database.js +120 -0
- package/runtime/dist/src/text/canonical-json.js +60 -0
- package/runtime/dist/src/text/python-number.js +18 -0
- package/runtime/dist/src/text/python-text.js +92 -0
- package/runtime/dist/src/text/unicode-casefold.js +29 -0
- package/runtime/dist/src/text/unicode-normalize.js +138 -0
- package/runtime/dist/src/text/unicode-tables.js +210 -0
- package/runtime/dist/src/understanding/candidates.js +77 -0
- package/runtime/dist/src/understanding/fixture.js +7 -0
- package/runtime/dist/src/understanding/jev.js +20 -0
- package/runtime/dist/src/understanding/pipeline.js +38 -0
- package/runtime/dist/src/voicemem/store-client.js +476 -0
- package/runtime/dist/src/voicemem/store-worker.js +380 -0
- package/runtime/dist/src/voicemem/versioned-memory.js +170 -0
- package/runtime/scripts/pair-device.mjs +109 -0
- package/src/command.mjs +48 -0
|
@@ -0,0 +1,1428 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import {} from '../executors/coding/intake.js';
|
|
3
|
+
import { USER_PRIORITY } from '../core/memory.js';
|
|
4
|
+
import { CONFIRM_TOOL, confirmArguments } from '../core/work-tools.js';
|
|
5
|
+
import { ConfirmationTurnIsolation } from './confirmation-turn-isolation.js';
|
|
6
|
+
import {} from './evidence.js';
|
|
7
|
+
import { MAX_HOST_FACT_CHARS, MAX_TRACKED_TOOL_CALLS, USER_HOLD_MAX_S, callKey, diagnosticName, hostFactIntent, isAbort, parseCallKey, projectCommitFailureText } from './service-state.js';
|
|
8
|
+
import { RealtimeDeliveryError } from './session.js';
|
|
9
|
+
const PROJECT_CONFIRMATION_CARRIER_RELEASE_TIMEOUT_S = 3;
|
|
10
|
+
function projectCommitSuccessText(operation, code) {
|
|
11
|
+
if (code !== 'committed')
|
|
12
|
+
return '';
|
|
13
|
+
if (operation.action === 'create') {
|
|
14
|
+
return `已确认,已创建并切换到工作区 ${operation.workspace_display_name}。`;
|
|
15
|
+
}
|
|
16
|
+
if (operation.action === 'select') {
|
|
17
|
+
return `已确认,已切换到工作区 ${operation.workspace_display_name}。`;
|
|
18
|
+
}
|
|
19
|
+
return '已确认,项目操作已完成。';
|
|
20
|
+
}
|
|
21
|
+
function projectConfirmationEventId(namespace, lifecycleId, transition) {
|
|
22
|
+
const digest = createHash('sha256')
|
|
23
|
+
.update(lifecycleId)
|
|
24
|
+
.update('\0')
|
|
25
|
+
.update(transition)
|
|
26
|
+
.digest('hex');
|
|
27
|
+
return `${namespace}:${digest}`;
|
|
28
|
+
}
|
|
29
|
+
export class ProjectConfirmationFlow {
|
|
30
|
+
reset() {
|
|
31
|
+
this.#projectConfirmationIsolation.invalidate();
|
|
32
|
+
this.#projectConfirmationShadowItems.clear();
|
|
33
|
+
this.#projectConfirmationClosingItems.clear();
|
|
34
|
+
this.#projectConfirmationCarrierReconnectAfterUser.clear();
|
|
35
|
+
this.#projectConfirmationPendingQuarantineEpoch = null;
|
|
36
|
+
this.#projectConfirmationClosingCalls.clear();
|
|
37
|
+
this.#projectConfirmationDecisionGates.clear();
|
|
38
|
+
this.#projectConfirmationClosedCalls.clear();
|
|
39
|
+
this.#projectConfirmationDecisionRetry = null;
|
|
40
|
+
this.#projectConfirmationCommittingLifecycles.clear();
|
|
41
|
+
this.#projectConfirmationExpiryFactOwners.clear();
|
|
42
|
+
this.#projectExpiryBatches.length = 0;
|
|
43
|
+
}
|
|
44
|
+
unsubscribeExpiry() {
|
|
45
|
+
if (this.#unsubscribeProjectExpiry !== null) {
|
|
46
|
+
this.#unsubscribeProjectExpiry();
|
|
47
|
+
this.#unsubscribeProjectExpiry = null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
subscribeExpiry() {
|
|
51
|
+
this.#unsubscribeProjectExpiry = this.#ports.projectConfirmation?.observeExpiry(() => {
|
|
52
|
+
const proposalId = this.#ports.projectConfirmation?.lifecycleId;
|
|
53
|
+
this.#projectConfirmationExpired();
|
|
54
|
+
if (proposalId !== undefined && proposalId !== null)
|
|
55
|
+
this.#ports.intake()?.decline(proposalId);
|
|
56
|
+
}) ?? null;
|
|
57
|
+
}
|
|
58
|
+
prepareIntake(intake) {
|
|
59
|
+
if (intake.target === null || this.#ports.projectConfirmation === undefined)
|
|
60
|
+
throw new TypeError('intake_confirmation_unavailable');
|
|
61
|
+
const target = intake.target;
|
|
62
|
+
const proposal = this.#ports.projectConfirmation.prepare({
|
|
63
|
+
action: target.action, workspace_display_name: target.workspace_display_name,
|
|
64
|
+
workspace_id: target.workspace_id,
|
|
65
|
+
session_title: target.action === 'create' || target.action === 'reuse' ? intake.title : target.session_title,
|
|
66
|
+
session_id: target.session_id,
|
|
67
|
+
work_order: intake.work_order, origin_ref: intake.origin_ref,
|
|
68
|
+
intake_id: intake.intake_id, plan_revision: intake.plan_revision,
|
|
69
|
+
});
|
|
70
|
+
this.syncProjectConfirmationIsolation();
|
|
71
|
+
this.#publishProjectView();
|
|
72
|
+
return proposal;
|
|
73
|
+
}
|
|
74
|
+
beforeEvent(event) {
|
|
75
|
+
const confirmationFencePendingAtStart = this.#projectConfirmationIsolation.responseFencePending;
|
|
76
|
+
const confirmationResponseStartsDuringSpeech = event.kind === 'response_started'
|
|
77
|
+
&& event.session_epoch === this.session.sessionEpoch
|
|
78
|
+
&& this.session.floor.state === 'user_speaking'
|
|
79
|
+
&& !this.#projectConfirmationIsolation.responseFencePending
|
|
80
|
+
&& this.#projectConfirmationIsolation.reservation?.sessionEpoch === event.session_epoch;
|
|
81
|
+
return { confirmationFencePendingAtStart, confirmationResponseStartsDuringSpeech };
|
|
82
|
+
}
|
|
83
|
+
afterEventAccepted(event, accepted, confirmationFencePendingAtStart, confirmationResponseStartsDuringSpeech) {
|
|
84
|
+
if (event.kind === 'response_started'
|
|
85
|
+
&& this.#projectConfirmationPendingQuarantineEpoch === event.session_epoch) {
|
|
86
|
+
this.#projectConfirmationPendingQuarantineEpoch = null;
|
|
87
|
+
this.#projectConfirmationIsolation.markBlockedResponse({
|
|
88
|
+
sessionEpoch: event.session_epoch,
|
|
89
|
+
responseId: event.response_id,
|
|
90
|
+
});
|
|
91
|
+
this.#projectConfirmationIsolation.markQuarantined({
|
|
92
|
+
sessionEpoch: event.session_epoch,
|
|
93
|
+
responseId: event.response_id,
|
|
94
|
+
});
|
|
95
|
+
this.session.suppressResponse(event.response_id);
|
|
96
|
+
}
|
|
97
|
+
if (event.kind === 'response_started'
|
|
98
|
+
&& event.session_epoch === this.session.sessionEpoch
|
|
99
|
+
&& this.#projectConfirmationIsBlocking()) {
|
|
100
|
+
// A fenced pre-start response is the stale question the user interrupted, not the response to
|
|
101
|
+
// their answer. It spends the one-shot fence but must not bind or release the reserved item.
|
|
102
|
+
if (accepted) {
|
|
103
|
+
this.#projectConfirmationIsolation.markBlockedResponse({
|
|
104
|
+
sessionEpoch: event.session_epoch,
|
|
105
|
+
responseId: event.response_id,
|
|
106
|
+
});
|
|
107
|
+
if (this.#ports.userOrigins.itemForResponse(event.session_epoch, event.response_id) === undefined) {
|
|
108
|
+
if (!this.#bindProjectConfirmationRetryResponse(event.session_epoch, event.response_id)) {
|
|
109
|
+
this.#ports.bindResponseUserOrigin(event.session_epoch, event.response_id);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
this.#bindProjectConfirmationResponse(event.session_epoch, event.response_id);
|
|
113
|
+
if (confirmationResponseStartsDuringSpeech) {
|
|
114
|
+
// The provider may now finish its structured function call, but it must not start talking
|
|
115
|
+
// after the user's floor opens. The deterministic confirmation fact remains the reply owner.
|
|
116
|
+
this.session.suppressResponse(event.response_id);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// The armed fence has been spent by this response, so it no longer holds the block open.
|
|
120
|
+
this.#projectConfirmationIsolation.setResponseFencePending(false);
|
|
121
|
+
}
|
|
122
|
+
if (event.kind === 'response_started'
|
|
123
|
+
&& this.#projectConfirmationIsolation.reservation?.sessionEpoch === event.session_epoch) {
|
|
124
|
+
this.#ports.telemetry?.record('project_confirmation.response_started', {
|
|
125
|
+
session_epoch: event.session_epoch,
|
|
126
|
+
response_id: event.response_id,
|
|
127
|
+
accepted,
|
|
128
|
+
started_during_user_speech: confirmationResponseStartsDuringSpeech,
|
|
129
|
+
fence_pending: confirmationFencePendingAtStart,
|
|
130
|
+
origin_bound: accepted
|
|
131
|
+
&& this.#ports.userOrigins.itemForResponse(event.session_epoch, event.response_id) !== undefined,
|
|
132
|
+
confirmation_item_count: this.#projectConfirmationIsolation.reservation === null ? 0 : 1,
|
|
133
|
+
proposal_id: this.#ports.projectConfirmation?.lifecycleId ?? 'none',
|
|
134
|
+
proposal_origin_ref: this.#ports.projectConfirmation?.proposalOriginRef ?? 'none',
|
|
135
|
+
delegate_origin_ref: this.#ports.projectConfirmation?.proposalOriginRef ?? 'none',
|
|
136
|
+
user_input_revision: this.session.providerTurnUserInputRevision(event.response_id) ?? -1,
|
|
137
|
+
item_id: this.#ports.userOrigins.itemForResponse(event.session_epoch, event.response_id) ?? 'none',
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
async settleTerminal(event, itemId) {
|
|
142
|
+
if (itemId !== undefined
|
|
143
|
+
&& this.#isProjectConfirmationItem(event.session_epoch, itemId)) {
|
|
144
|
+
const controller = this.#ports.projectConfirmation;
|
|
145
|
+
const itemKey = callKey(event.session_epoch, itemId);
|
|
146
|
+
const retry = this.#projectConfirmationDecisionRetry;
|
|
147
|
+
const isRetryTerminal = retry?.item_key === itemKey
|
|
148
|
+
&& retry.retry_response_id === event.response_id;
|
|
149
|
+
// A transport-level cancelled/failed terminal can be just as empty as completed. What
|
|
150
|
+
// matters at this boundary is whether the response supplied any audible decision, not the
|
|
151
|
+
// provider's terminal label.
|
|
152
|
+
const silentTerminal = !this.session.responseHasSpoken(event.response_id);
|
|
153
|
+
this.#ports.telemetry?.record('project_confirmation.response_terminal', {
|
|
154
|
+
session_epoch: event.session_epoch,
|
|
155
|
+
response_id: event.response_id,
|
|
156
|
+
item_id: itemId,
|
|
157
|
+
status: event.status,
|
|
158
|
+
transcript_ready: this.#ports.userOrigins.hasOriginRef(event.session_epoch, itemId),
|
|
159
|
+
decision_seen: false,
|
|
160
|
+
retry_attempt: isRetryTerminal ? 1 : 0,
|
|
161
|
+
user_input_revision: this.#ports.userOrigins.revisionForItem(event.session_epoch, itemId) ?? -1,
|
|
162
|
+
proposal_id: controller?.lifecycleId ?? 'none',
|
|
163
|
+
proposal_origin_ref: controller?.proposalOriginRef ?? 'none',
|
|
164
|
+
delegate_origin_ref: controller?.proposalOriginRef ?? 'none',
|
|
165
|
+
});
|
|
166
|
+
if (silentTerminal && controller?.pending === true && !isRetryTerminal) {
|
|
167
|
+
this.#projectConfirmationDecisionRetry ??= {
|
|
168
|
+
item_key: itemKey,
|
|
169
|
+
source_response_id: event.response_id,
|
|
170
|
+
requested: false,
|
|
171
|
+
retry_response_id: null,
|
|
172
|
+
};
|
|
173
|
+
await this.#maybeRequestProjectConfirmationDecisionRetry(event.session_epoch, itemId);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
controller?.releaseUndecided({ epoch: event.session_epoch, itemId });
|
|
177
|
+
this.#endProjectConfirmationItem(event.session_epoch, itemId);
|
|
178
|
+
if (isRetryTerminal) {
|
|
179
|
+
this.#ports.telemetry?.record('project_confirmation.decision_retry_exhausted', {
|
|
180
|
+
session_epoch: event.session_epoch,
|
|
181
|
+
item_id: itemId,
|
|
182
|
+
response_id: event.response_id,
|
|
183
|
+
proposal_id: controller?.lifecycleId ?? 'none',
|
|
184
|
+
proposal_origin_ref: controller?.proposalOriginRef ?? 'none',
|
|
185
|
+
delegate_origin_ref: controller?.proposalOriginRef ?? 'none',
|
|
186
|
+
user_input_revision: this.#ports.userOrigins.revisionForItem(event.session_epoch, itemId) ?? -1,
|
|
187
|
+
reason: 'no_confirmation_function',
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
clearTerminalShadow(epoch, itemId) {
|
|
194
|
+
if (itemId !== undefined) {
|
|
195
|
+
this.#projectConfirmationShadowItems.delete(callKey(epoch, itemId));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
async afterTranscriptFinal(event, originRef) {
|
|
199
|
+
if (this.#isProjectConfirmationItem(event.session_epoch, event.item_id)
|
|
200
|
+
&& this.#ports.projectConfirmation?.pending !== true) {
|
|
201
|
+
await this.#closeConfirmationDeferredCalls(event.item_id);
|
|
202
|
+
}
|
|
203
|
+
else if (this.isProjectConfirmationShadowItem(event.session_epoch, event.item_id)) {
|
|
204
|
+
await this.#closeConfirmationDeferredCalls(event.item_id);
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
await this.#ports.continuations.releaseDeferredOriginCalls(event.item_id, originRef);
|
|
208
|
+
}
|
|
209
|
+
if (this.#isProjectConfirmationItem(event.session_epoch, event.item_id)) {
|
|
210
|
+
await this.#maybeRequestProjectConfirmationDecisionRetry(event.session_epoch, event.item_id);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
async afterTranscriptFailed(event) {
|
|
214
|
+
if (this.#isProjectConfirmationItem(event.session_epoch, event.item_id)) {
|
|
215
|
+
await this.#failProjectConfirmation(event.session_epoch, event.item_id);
|
|
216
|
+
}
|
|
217
|
+
else if (this.isProjectConfirmationShadowItem(event.session_epoch, event.item_id)) {
|
|
218
|
+
await this.#closeConfirmationDeferredCalls(event.item_id);
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
await this.#ports.continuations.releaseDeferredOriginCalls(event.item_id, null);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
clearTerminal(event) {
|
|
225
|
+
this.#projectConfirmationIsolation.clearResponse({
|
|
226
|
+
sessionEpoch: event.session_epoch,
|
|
227
|
+
responseId: event.response_id,
|
|
228
|
+
});
|
|
229
|
+
if (this.#projectConfirmationPendingQuarantineEpoch === event.session_epoch) {
|
|
230
|
+
this.#projectConfirmationPendingQuarantineEpoch = null;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
responseIsQuarantined(sessionEpoch, responseId) {
|
|
234
|
+
return this.#projectConfirmationIsolation.responseState({ sessionEpoch, responseId })?.quarantined === true;
|
|
235
|
+
}
|
|
236
|
+
discardPendingExpiries() { this.#projectExpiryBatches.length = 0; }
|
|
237
|
+
expiryDrain() { return this.#projectExpiryDraining; }
|
|
238
|
+
carrierReleaseTasks() { return [...this.#projectConfirmationCarrierReleaseTasks]; }
|
|
239
|
+
get proposalId() { return this.#ports.projectConfirmation?.lifecycleId; }
|
|
240
|
+
get proposalOriginRef() { return this.#ports.projectConfirmation?.proposalOriginRef; }
|
|
241
|
+
#ports;
|
|
242
|
+
constructor(ports) { this.#ports = ports; }
|
|
243
|
+
get session() { return this.#ports.session; }
|
|
244
|
+
/** Policy-free project turn identity state; never shared with Codex approval occupancy. */
|
|
245
|
+
#projectConfirmationIsolation = new ConfirmationTurnIsolation(MAX_TRACKED_TOOL_CALLS);
|
|
246
|
+
/** Later utterances captured while another item owns the same confirmation. */
|
|
247
|
+
#projectConfirmationShadowItems = new Set();
|
|
248
|
+
/** Items mid-close: no longer answerable, still blocking tool calls. */
|
|
249
|
+
#projectConfirmationClosingItems = new Set();
|
|
250
|
+
/** Bounded carrier cancel/watchdog work, observed so it cannot outlive service shutdown silently. */
|
|
251
|
+
#projectConfirmationCarrierReleaseTasks = new Set();
|
|
252
|
+
/** Carrier recovery waits here while a newer user turn is still being transcribed. */
|
|
253
|
+
#projectConfirmationCarrierReconnectAfterUser = new Map();
|
|
254
|
+
/** Epoch whose requested confirmation retry has not revealed its response id yet. */
|
|
255
|
+
#projectConfirmationPendingQuarantineEpoch = null;
|
|
256
|
+
#projectConfirmationClosingCalls = new Set();
|
|
257
|
+
/** Voice decision handlers whose exact tool output must precede expiry cleanup when possible. */
|
|
258
|
+
#projectConfirmationDecisionGates = new Map();
|
|
259
|
+
/** Insertion-ordered so the oldest closed call is the one evicted. */
|
|
260
|
+
#projectConfirmationClosedCalls = new Map();
|
|
261
|
+
#projectConfirmationDecisionRetry = null;
|
|
262
|
+
/** Confirmation commits whose terminal user-facing fact has not been selected yet. */
|
|
263
|
+
#projectConfirmationCommittingLifecycles = new Set();
|
|
264
|
+
/** Active commits for which the expiry observer owns the terminal fact. */
|
|
265
|
+
#projectConfirmationExpiryFactOwners = new Set();
|
|
266
|
+
#projectExpiryBatches = [];
|
|
267
|
+
#projectExpiryDraining = null;
|
|
268
|
+
#unsubscribeProjectExpiry = null;
|
|
269
|
+
async projectConfirmationDecision(proposalId, confirmed) {
|
|
270
|
+
const controller = this.#ports.projectConfirmation;
|
|
271
|
+
const lifecycleId = controller?.lifecycleId ?? 'none';
|
|
272
|
+
this.#ports.telemetry?.record('project_confirmation.ui_decision_requested', {
|
|
273
|
+
proposal_id: proposalId,
|
|
274
|
+
confirmed,
|
|
275
|
+
lifecycle_id: lifecycleId,
|
|
276
|
+
});
|
|
277
|
+
if (controller === undefined)
|
|
278
|
+
return;
|
|
279
|
+
const outcome = controller.acceptDirectDecision({ proposalId, confirmed });
|
|
280
|
+
if (outcome.kind === 'cancelled')
|
|
281
|
+
this.#ports.intake()?.decline(proposalId);
|
|
282
|
+
if (outcome.kind === 'ignored') {
|
|
283
|
+
this.#ports.telemetry?.record('project_confirmation.ui_decision_refused', {
|
|
284
|
+
proposal_id: proposalId,
|
|
285
|
+
reason: 'stale_or_not_pending',
|
|
286
|
+
});
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
this.#publishProjectView();
|
|
290
|
+
// A click wins the same one-shot authority race as a function call. Close every voice carrier
|
|
291
|
+
// before awaiting commit I/O so a late provider call cannot act on the settled proposal.
|
|
292
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
293
|
+
const items = reserved === null
|
|
294
|
+
? []
|
|
295
|
+
: [{ sessionEpoch: reserved.sessionEpoch, id: reserved.itemId }];
|
|
296
|
+
const retry = this.#projectConfirmationDecisionRetry;
|
|
297
|
+
const reconnectOutstandingRetry = retry?.requested === true
|
|
298
|
+
&& retry.retry_response_id === null
|
|
299
|
+
&& parseCallKey(retry.item_key).sessionEpoch === this.session.sessionEpoch;
|
|
300
|
+
if (reconnectOutstandingRetry) {
|
|
301
|
+
this.#projectConfirmationPendingQuarantineEpoch = this.session.sessionEpoch;
|
|
302
|
+
}
|
|
303
|
+
for (const item of items)
|
|
304
|
+
this.#beginProjectConfirmationClose(item.sessionEpoch, item.id);
|
|
305
|
+
if (reconnectOutstandingRetry) {
|
|
306
|
+
this.#projectConfirmationPendingQuarantineEpoch = this.session.sessionEpoch;
|
|
307
|
+
}
|
|
308
|
+
await this.#quarantineProjectConfirmationResponses(this.session.sessionEpoch);
|
|
309
|
+
let state = outcome.kind === 'confirmed' ? 'accepted' : 'refused';
|
|
310
|
+
let text = outcome.response_text;
|
|
311
|
+
let expiryOwnsFact = false;
|
|
312
|
+
try {
|
|
313
|
+
for (const item of items)
|
|
314
|
+
await this.#closeConfirmationDeferredCalls(item.id);
|
|
315
|
+
if (outcome.kind === 'confirmed' && outcome.operation !== null) {
|
|
316
|
+
const committed = await this.#commitConfirmedProjectOperation(outcome.operation);
|
|
317
|
+
state = committed.state;
|
|
318
|
+
text = committed.text;
|
|
319
|
+
expiryOwnsFact = committed.expiryOwnsFact;
|
|
320
|
+
}
|
|
321
|
+
if (reconnectOutstandingRetry) {
|
|
322
|
+
try {
|
|
323
|
+
await this.#ports.reconnectProviderSession({
|
|
324
|
+
reason: 'project_confirmation_ui_retry',
|
|
325
|
+
expectedEpoch: this.session.sessionEpoch,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
catch (failure) {
|
|
329
|
+
this.#ports.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
330
|
+
? failure
|
|
331
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
finally {
|
|
336
|
+
for (const item of items)
|
|
337
|
+
this.#endProjectConfirmationClose(item.sessionEpoch, item.id);
|
|
338
|
+
this.#projectConfirmationShadowItems.clear();
|
|
339
|
+
this.#projectConfirmationClosingItems.clear();
|
|
340
|
+
this.#projectConfirmationDecisionRetry = null;
|
|
341
|
+
this.#projectConfirmationIsolation.setResponseFencePending(false);
|
|
342
|
+
}
|
|
343
|
+
// Expiry publishes through its observer-owned cleanup batch. Queuing here as well would create
|
|
344
|
+
// two differently keyed host facts for the same deadline transition.
|
|
345
|
+
if (outcome.kind !== 'expired' && !expiryOwnsFact && text !== null && text !== '') {
|
|
346
|
+
this.#queueProjectConfirmationFact(text, lifecycleId, `ui-decision:${outcome.kind}:${state}`);
|
|
347
|
+
}
|
|
348
|
+
this.#publishProjectView();
|
|
349
|
+
this.#ports.telemetry?.record('project_confirmation.ui_decision_completed', {
|
|
350
|
+
proposal_id: proposalId,
|
|
351
|
+
outcome: outcome.kind,
|
|
352
|
+
state,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Decide whether a tool call can be handled now, or has to wait for its evidence.
|
|
357
|
+
*
|
|
358
|
+
* Three cases, in the order the oracle checks them. If the response already has a bound user item,
|
|
359
|
+
* the call has its evidence -- unless the transcript for that item has not landed, in which case it
|
|
360
|
+
* waits. If no item is bound but a user turn is in flight, the call may belong to *that* turn, and
|
|
361
|
+
* the question becomes whether the response it names is the one that turn will answer. If nothing is
|
|
362
|
+
* pending at all, the call has whatever evidence it is going to get.
|
|
363
|
+
*
|
|
364
|
+
* The deferral queue is bounded. Full means the provider is producing calls faster than transcripts
|
|
365
|
+
* arrive, and no amount of waiting will fix it -- reconnecting is the way back to a session whose
|
|
366
|
+
* state can be reasoned about.
|
|
367
|
+
*/
|
|
368
|
+
/**
|
|
369
|
+
* Which confirmation FSM a `confirm(id, accepted)` call answers (spec 08: one tool for both).
|
|
370
|
+
*
|
|
371
|
+
* An id the approval FSM knows -- live, still holding voice authority, or its expiry tombstone --
|
|
372
|
+
* wins, so a late answer keeps that FSM's own classification; then the project proposal's id. Any
|
|
373
|
+
* other well-formed id names nothing and is `'none'` (`unknown_confirmation`, refused in
|
|
374
|
+
* `#interceptHost`); only a null / malformed id falls through to whichever FSM is pending, so each
|
|
375
|
+
* keeps its own malformed-call accounting. `null` is any other tool. The id is read through
|
|
376
|
+
* `confirmArguments` so a provider-supplied accessor is never evaluated here.
|
|
377
|
+
*/
|
|
378
|
+
confirmTarget(event) {
|
|
379
|
+
if (event.name !== CONFIRM_TOOL)
|
|
380
|
+
return null;
|
|
381
|
+
const id = confirmArguments(event.arguments)?.id ?? null;
|
|
382
|
+
const project = this.#ports.projectConfirmation;
|
|
383
|
+
if (id !== null && this.#ports.approvalHost.ownsId(id))
|
|
384
|
+
return 'approval';
|
|
385
|
+
if (id !== null && project?.lifecycleId === id)
|
|
386
|
+
return 'project';
|
|
387
|
+
if (id !== null)
|
|
388
|
+
return 'none';
|
|
389
|
+
if (project?.pending === true)
|
|
390
|
+
return 'project';
|
|
391
|
+
if (this.#ports.approvalHost.pending)
|
|
392
|
+
return 'approval';
|
|
393
|
+
if (event.response_id !== null
|
|
394
|
+
&& this.#ports.approvalHost.isExecutorApprovalResponseQuarantined(event.session_epoch, event.response_id))
|
|
395
|
+
return 'approval';
|
|
396
|
+
return 'none';
|
|
397
|
+
}
|
|
398
|
+
// ---------------------------------------------------------------------------------------------
|
|
399
|
+
// Family N: the acknowledgement a delegated call owes the user.
|
|
400
|
+
// ---------------------------------------------------------------------------------------------
|
|
401
|
+
/** Mirror a newly visible controller lifecycle without importing its expiry policy. */
|
|
402
|
+
syncProjectConfirmationIsolation() {
|
|
403
|
+
const controller = this.#ports.projectConfirmation;
|
|
404
|
+
const lifecycleId = controller?.lifecycleId;
|
|
405
|
+
const sessionEpoch = this.session.sessionEpoch;
|
|
406
|
+
if (controller?.pending !== true
|
|
407
|
+
|| lifecycleId === null
|
|
408
|
+
|| lifecycleId === undefined
|
|
409
|
+
|| sessionEpoch < 1)
|
|
410
|
+
return;
|
|
411
|
+
// Spec 08: a colliding executor approval waits behind the project confirmation. Its voice authority
|
|
412
|
+
// is withdrawn and its TTL paused (`hold`, never declined); `#publishProjectView` re-arms both once
|
|
413
|
+
// this one settles.
|
|
414
|
+
this.#ports.approvalHost.hold();
|
|
415
|
+
const current = this.#projectConfirmationIsolation.authority;
|
|
416
|
+
if (current?.authorityId === lifecycleId && current.sessionEpoch === sessionEpoch)
|
|
417
|
+
return;
|
|
418
|
+
const remaining = controller.view.pending_expires_in_seconds;
|
|
419
|
+
if (remaining === undefined || remaining === null || !Number.isFinite(remaining))
|
|
420
|
+
return;
|
|
421
|
+
this.#projectConfirmationIsolation.beginAuthority({
|
|
422
|
+
authorityId: lifecycleId,
|
|
423
|
+
sessionEpoch,
|
|
424
|
+
createdUserRevision: this.session.userInputRevision,
|
|
425
|
+
expiresAt: this.#ports.clock.now() + Math.max(0, remaining),
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
// ---------------------------------------------------------------------------------------------
|
|
429
|
+
// Family I: project confirmation.
|
|
430
|
+
//
|
|
431
|
+
// Changing which workspace the agent operates in needs the user to say yes out loud, and this is the
|
|
432
|
+
// machinery that makes that answer trustworthy in a conversation that keeps moving. The controller
|
|
433
|
+
// owns the decision; this owns the *isolation* around it.
|
|
434
|
+
//
|
|
435
|
+
// Three overlapping guards, because the failure modes are different. The reserved item makes one
|
|
436
|
+
// transcript the answer and nothing else. The response block permits only the dedicated decision
|
|
437
|
+
// function. And the pending-only fence cancels an old host-requested question without cancelling the
|
|
438
|
+
// model response that must produce that function. Each closes a hole the other two leave open.
|
|
439
|
+
// ---------------------------------------------------------------------------------------------
|
|
440
|
+
/**
|
|
441
|
+
* Claim the user's next utterance as the answer to a pending proposal.
|
|
442
|
+
*
|
|
443
|
+
* An utterance with no provider item id cannot be reserved, and an unreservable one cannot be
|
|
444
|
+
* answered -- so the proposal is cancelled outright rather than left waiting for a reply that can
|
|
445
|
+
* never be attributed to it.
|
|
446
|
+
*/
|
|
447
|
+
reserveProjectConfirmation(event) {
|
|
448
|
+
if (this.#ports.projectConfirmation?.pending !== true)
|
|
449
|
+
return;
|
|
450
|
+
const itemId = event.provider_item_id;
|
|
451
|
+
if (itemId === null) {
|
|
452
|
+
const lifecycleId = this.#projectConfirmationLifecycleId();
|
|
453
|
+
this.invalidateProjectConfirmation('missing_item_correlation');
|
|
454
|
+
this.#queueProjectConfirmationFact('缺少语音确认关联,本次操作已取消。', lifecycleId, 'missing-item-correlation');
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
if (!this.#ports.projectConfirmation.reserveUserItem({ epoch: event.session_epoch, itemId })) {
|
|
458
|
+
if (!this.#isProjectConfirmationItem(event.session_epoch, itemId)) {
|
|
459
|
+
this.#projectConfirmationShadowItems.add(callKey(event.session_epoch, itemId));
|
|
460
|
+
}
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
const mirrored = this.#projectConfirmationIsolation.reserveUserItem({
|
|
464
|
+
sessionEpoch: event.session_epoch,
|
|
465
|
+
itemId,
|
|
466
|
+
userRevision: this.session.userInputRevision,
|
|
467
|
+
});
|
|
468
|
+
if (mirrored !== 'reserved' && mirrored !== 'idempotent') {
|
|
469
|
+
const lifecycleId = this.#projectConfirmationLifecycleId();
|
|
470
|
+
this.invalidateProjectConfirmation('missing_item_correlation');
|
|
471
|
+
this.#queueProjectConfirmationFact('缺少语音确认关联,本次操作已取消。', lifecycleId, 'missing-item-correlation');
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
// Cancel only a confirmation question whose host-requested response has not started yet. The
|
|
475
|
+
// response created from this user answer must remain alive so the provider can emit the structured
|
|
476
|
+
// confirmation function after its response start.
|
|
477
|
+
this.#projectConfirmationIsolation.setResponseFencePending(this.session.armPendingResponseFence());
|
|
478
|
+
this.#publishProjectView();
|
|
479
|
+
}
|
|
480
|
+
/** Bind an initial carrier only after the shared origin ledger proves the exact item/revision. */
|
|
481
|
+
#bindProjectConfirmationResponse(epoch, responseId) {
|
|
482
|
+
const itemId = this.#ports.userOrigins.itemForResponse(epoch, responseId);
|
|
483
|
+
const revision = itemId === undefined
|
|
484
|
+
? undefined
|
|
485
|
+
: this.#ports.userOrigins.revisionForItem(epoch, itemId);
|
|
486
|
+
if (itemId === undefined || revision === undefined)
|
|
487
|
+
return false;
|
|
488
|
+
const result = this.#projectConfirmationIsolation.bindResponse({
|
|
489
|
+
sessionEpoch: epoch,
|
|
490
|
+
itemId,
|
|
491
|
+
userRevision: revision,
|
|
492
|
+
responseId,
|
|
493
|
+
});
|
|
494
|
+
return result === 'bound' || result === 'idempotent';
|
|
495
|
+
}
|
|
496
|
+
/** Bind the provider response created by the one bounded retry to its original user evidence. */
|
|
497
|
+
#bindProjectConfirmationRetryResponse(epoch, responseId) {
|
|
498
|
+
const retry = this.#projectConfirmationDecisionRetry;
|
|
499
|
+
if (retry === null || !retry.requested || retry.retry_response_id !== null)
|
|
500
|
+
return false;
|
|
501
|
+
const item = parseCallKey(retry.item_key);
|
|
502
|
+
if (item.sessionEpoch !== epoch)
|
|
503
|
+
return false;
|
|
504
|
+
const revision = this.#ports.userOrigins.revisionForItem(epoch, item.id);
|
|
505
|
+
if (revision === undefined || !this.session.responseMatchesUserItem(responseId, item.id, revision))
|
|
506
|
+
return false;
|
|
507
|
+
if (!this.#ports.userOrigins.bindRetryResponse({ epoch, responseId, itemId: item.id }))
|
|
508
|
+
return false;
|
|
509
|
+
const isolated = this.#projectConfirmationIsolation.bindRetryResponse({
|
|
510
|
+
sessionEpoch: epoch,
|
|
511
|
+
itemId: item.id,
|
|
512
|
+
userRevision: revision,
|
|
513
|
+
responseId,
|
|
514
|
+
});
|
|
515
|
+
if (isolated !== 'bound' && isolated !== 'idempotent')
|
|
516
|
+
return false;
|
|
517
|
+
retry.retry_response_id = responseId;
|
|
518
|
+
this.#ports.telemetry?.record('project_confirmation.decision_retry_started', {
|
|
519
|
+
session_epoch: epoch,
|
|
520
|
+
item_id: item.id,
|
|
521
|
+
response_id: responseId,
|
|
522
|
+
source_response_id: retry.source_response_id,
|
|
523
|
+
proposal_id: this.#ports.projectConfirmation?.lifecycleId ?? 'none',
|
|
524
|
+
proposal_origin_ref: this.#ports.projectConfirmation?.proposalOriginRef ?? 'none',
|
|
525
|
+
delegate_origin_ref: this.#ports.projectConfirmation?.proposalOriginRef ?? 'none',
|
|
526
|
+
user_input_revision: this.#ports.userOrigins.revisionForItem(epoch, item.id) ?? -1,
|
|
527
|
+
});
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
/** Once the same turn's transcript exists, ask the provider once more for the structured decision. */
|
|
531
|
+
async #maybeRequestProjectConfirmationDecisionRetry(epoch, itemId) {
|
|
532
|
+
const retry = this.#projectConfirmationDecisionRetry;
|
|
533
|
+
const controller = this.#ports.projectConfirmation;
|
|
534
|
+
if (retry?.item_key !== callKey(epoch, itemId)
|
|
535
|
+
|| retry.requested
|
|
536
|
+
|| !this.#ports.userOrigins.hasOriginRef(epoch, itemId)
|
|
537
|
+
|| controller?.pending !== true)
|
|
538
|
+
return;
|
|
539
|
+
retry.requested = true;
|
|
540
|
+
this.#ports.telemetry?.record('project_confirmation.decision_retry_requested', {
|
|
541
|
+
session_epoch: epoch,
|
|
542
|
+
item_id: itemId,
|
|
543
|
+
source_response_id: retry.source_response_id,
|
|
544
|
+
proposal_id: controller.lifecycleId ?? 'none',
|
|
545
|
+
transcript_ready: true,
|
|
546
|
+
retry_attempt: 1,
|
|
547
|
+
proposal_origin_ref: controller.proposalOriginRef ?? 'none',
|
|
548
|
+
delegate_origin_ref: controller.proposalOriginRef ?? 'none',
|
|
549
|
+
user_input_revision: this.#ports.userOrigins.revisionForItem(epoch, itemId) ?? -1,
|
|
550
|
+
});
|
|
551
|
+
let requested = false;
|
|
552
|
+
try {
|
|
553
|
+
requested = await this.session.requestUserResponse();
|
|
554
|
+
}
|
|
555
|
+
catch (failure) {
|
|
556
|
+
this.#ports.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
557
|
+
? failure
|
|
558
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
559
|
+
}
|
|
560
|
+
if (requested)
|
|
561
|
+
return;
|
|
562
|
+
controller.releaseUndecided({ epoch, itemId });
|
|
563
|
+
this.#endProjectConfirmationItem(epoch, itemId);
|
|
564
|
+
this.#ports.telemetry?.record('project_confirmation.decision_retry_exhausted', {
|
|
565
|
+
session_epoch: epoch,
|
|
566
|
+
item_id: itemId,
|
|
567
|
+
response_id: retry.source_response_id,
|
|
568
|
+
proposal_id: controller.lifecycleId ?? 'none',
|
|
569
|
+
reason: 'provider_retry_unavailable',
|
|
570
|
+
proposal_origin_ref: controller.proposalOriginRef ?? 'none',
|
|
571
|
+
delegate_origin_ref: controller.proposalOriginRef ?? 'none',
|
|
572
|
+
user_input_revision: this.#ports.userOrigins.revisionForItem(epoch, itemId) ?? -1,
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Whether this tool call arrives in a turn that is supposed to be waiting for a confirmation.
|
|
577
|
+
*
|
|
578
|
+
* Blocked by *epoch* as well as by response, because a reconnect renumbers responses and a
|
|
579
|
+
* confirmation spanning one would otherwise stop blocking. Recording the response id on the way
|
|
580
|
+
* through is what makes the block stick for the rest of that turn.
|
|
581
|
+
*/
|
|
582
|
+
blocksProjectConfirmationTool(event) {
|
|
583
|
+
const effectiveResponseId = event.response_id ?? this.session.activeProviderResponseId;
|
|
584
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
585
|
+
// A correlated coding dispatch revises the proposal through normal tool admission and intake.
|
|
586
|
+
// Keep host narration, shadow turns and unbound calls behind the confirmation fence.
|
|
587
|
+
if (event.name === 'dispatch' && this.#ports.intake() !== undefined
|
|
588
|
+
&& event.arguments !== null && typeof event.arguments === 'object'
|
|
589
|
+
&& Object.getOwnPropertyDescriptor(event.arguments, 'executor')?.value === this.#ports.coding?.channel
|
|
590
|
+
&& reserved?.sessionEpoch === event.session_epoch && effectiveResponseId !== null
|
|
591
|
+
&& this.#ports.userOrigins.itemForResponse(event.session_epoch, effectiveResponseId) === reserved.itemId
|
|
592
|
+
&& this.#ports.userOrigins.hasOriginRef(event.session_epoch, reserved.itemId)
|
|
593
|
+
&& reserved.userRevision === this.session.userInputRevision
|
|
594
|
+
&& this.#projectConfirmationIsolation.responseState({ sessionEpoch: event.session_epoch, responseId: effectiveResponseId })?.quarantined !== true)
|
|
595
|
+
return false;
|
|
596
|
+
if (effectiveResponseId !== null
|
|
597
|
+
&& this.#projectConfirmationIsolation.responseState({
|
|
598
|
+
sessionEpoch: event.session_epoch,
|
|
599
|
+
responseId: effectiveResponseId,
|
|
600
|
+
})?.quarantined === true)
|
|
601
|
+
return true;
|
|
602
|
+
if (this.#projectConfirmationIsolation.hasBlockedResponseInEpoch(event.session_epoch))
|
|
603
|
+
return true;
|
|
604
|
+
if (this.#projectConfirmationIsBlocking()) {
|
|
605
|
+
if (event.response_id !== null) {
|
|
606
|
+
this.#projectConfirmationIsolation.markBlockedResponse({
|
|
607
|
+
sessionEpoch: event.session_epoch,
|
|
608
|
+
responseId: event.response_id,
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
#isProjectConfirmationItem(epoch, itemId) {
|
|
616
|
+
const key = callKey(epoch, itemId);
|
|
617
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
618
|
+
return reserved?.sessionEpoch === epoch && reserved.itemId === itemId
|
|
619
|
+
|| this.#projectConfirmationClosingItems.has(key);
|
|
620
|
+
}
|
|
621
|
+
#projectConfirmationIsBlocking() {
|
|
622
|
+
return this.#projectConfirmationIsolation.reservation !== null
|
|
623
|
+
|| this.#projectConfirmationClosingItems.size > 0
|
|
624
|
+
|| this.#projectConfirmationIsolation.responseFencePending;
|
|
625
|
+
}
|
|
626
|
+
isProjectConfirmationShadowItem(epoch, itemId) {
|
|
627
|
+
return this.#projectConfirmationShadowItems.has(callKey(epoch, itemId));
|
|
628
|
+
}
|
|
629
|
+
/** A shadow turn is still transcribed into Memory, but it cannot speak or execute tools. */
|
|
630
|
+
suppressShadowConfirmationResponse(epoch, responseId) {
|
|
631
|
+
const itemId = this.#ports.userOrigins.itemForResponse(epoch, responseId);
|
|
632
|
+
if (itemId === undefined || !this.isProjectConfirmationShadowItem(epoch, itemId))
|
|
633
|
+
return;
|
|
634
|
+
this.session.suppressResponse(responseId);
|
|
635
|
+
}
|
|
636
|
+
/**
|
|
637
|
+
* Move an item from reserved to closing.
|
|
638
|
+
*
|
|
639
|
+
* A separate set rather than a flag, because closing involves provider I/O: during it the item is no
|
|
640
|
+
* longer accepting an answer but still has to block tool calls, and a single set could not say both.
|
|
641
|
+
*/
|
|
642
|
+
#beginProjectConfirmationClose(epoch, itemId) {
|
|
643
|
+
const key = callKey(epoch, itemId);
|
|
644
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
645
|
+
if (reserved?.sessionEpoch === epoch && reserved.itemId === itemId) {
|
|
646
|
+
this.#projectConfirmationIsolation.releaseReservation({
|
|
647
|
+
sessionEpoch: epoch,
|
|
648
|
+
itemId,
|
|
649
|
+
userRevision: reserved.userRevision,
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
this.#projectConfirmationClosingItems.add(key);
|
|
653
|
+
}
|
|
654
|
+
#endProjectConfirmationClose(epoch, itemId) {
|
|
655
|
+
const key = callKey(epoch, itemId);
|
|
656
|
+
this.#projectConfirmationClosingItems.delete(key);
|
|
657
|
+
if (this.#projectConfirmationDecisionRetry?.item_key === key) {
|
|
658
|
+
this.#projectConfirmationDecisionRetry = null;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
#endProjectConfirmationItem(epoch, itemId) {
|
|
662
|
+
const key = callKey(epoch, itemId);
|
|
663
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
664
|
+
if (reserved?.sessionEpoch === epoch && reserved.itemId === itemId) {
|
|
665
|
+
this.#projectConfirmationIsolation.releaseReservation({
|
|
666
|
+
sessionEpoch: epoch,
|
|
667
|
+
itemId,
|
|
668
|
+
userRevision: reserved.userRevision,
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
if (this.#projectConfirmationDecisionRetry?.item_key === key) {
|
|
672
|
+
this.#projectConfirmationDecisionRetry = null;
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
async handleProjectConfirmationDecision(event, origin) {
|
|
676
|
+
const call = callKey(event.session_epoch, event.call_id);
|
|
677
|
+
if (this.#projectConfirmationClosingCalls.has(call)
|
|
678
|
+
|| this.#projectConfirmationClosedCalls.has(call))
|
|
679
|
+
return;
|
|
680
|
+
this.#projectConfirmationClosingCalls.add(call);
|
|
681
|
+
let releaseDecisionGate;
|
|
682
|
+
const decisionGate = new Promise(resolve => { releaseDecisionGate = resolve; });
|
|
683
|
+
this.#projectConfirmationDecisionGates.set(call, decisionGate);
|
|
684
|
+
let code = 'confirmation_not_pending';
|
|
685
|
+
let state = 'refused';
|
|
686
|
+
let confirmationText = null;
|
|
687
|
+
let confirmationResponseId = null;
|
|
688
|
+
try {
|
|
689
|
+
const itemId = origin.originItemId;
|
|
690
|
+
const controller = this.#ports.projectConfirmation;
|
|
691
|
+
if (itemId === null) {
|
|
692
|
+
const recovered = await this.#recoverUnboundProjectConfirmation(event);
|
|
693
|
+
const proposalId = controller?.lifecycleId ?? 'none';
|
|
694
|
+
const responseId = event.response_id ?? 'none';
|
|
695
|
+
const activeResponseId = this.session.activeProviderResponseId ?? 'none';
|
|
696
|
+
const responsePhase = event.response_id === null
|
|
697
|
+
? 'unknown'
|
|
698
|
+
: this.session.providerTurnPhase(event.response_id) ?? 'unknown';
|
|
699
|
+
const responseFenced = event.response_id !== null
|
|
700
|
+
&& this.session.providerTurnWasFenced(event.response_id);
|
|
701
|
+
this.#ports.onDiagnostic('[realtime-diagnostic] project_confirmation_binding_missing'
|
|
702
|
+
+ ` session_epoch=${event.session_epoch}`
|
|
703
|
+
+ ` call_id=${event.call_id}`
|
|
704
|
+
+ ` response_id=${responseId}`
|
|
705
|
+
+ ` active_response_id=${activeResponseId}`
|
|
706
|
+
+ ` response_phase=${responsePhase}`
|
|
707
|
+
+ ` response_fenced=${responseFenced}`
|
|
708
|
+
+ ' origin_item_bound=false'
|
|
709
|
+
+ ` pending=${controller?.pending === true}`
|
|
710
|
+
+ ` recovered=${recovered}`
|
|
711
|
+
+ ` proposal_id=${proposalId}`);
|
|
712
|
+
this.#ports.telemetry?.record('project_confirmation.binding_missing', {
|
|
713
|
+
session_epoch: event.session_epoch,
|
|
714
|
+
call_id: event.call_id,
|
|
715
|
+
response_id: responseId,
|
|
716
|
+
active_response_id: activeResponseId,
|
|
717
|
+
response_phase: responsePhase,
|
|
718
|
+
response_fenced: responseFenced,
|
|
719
|
+
origin_item_bound: false,
|
|
720
|
+
pending: controller?.pending === true,
|
|
721
|
+
recovered,
|
|
722
|
+
proposal_id: proposalId,
|
|
723
|
+
proposal_origin_ref: controller?.proposalOriginRef ?? 'none',
|
|
724
|
+
delegate_origin_ref: controller?.proposalOriginRef ?? 'none',
|
|
725
|
+
user_input_revision: event.response_id === null
|
|
726
|
+
? -1
|
|
727
|
+
: this.session.providerTurnUserInputRevision(event.response_id) ?? -1,
|
|
728
|
+
item_id: 'none',
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
if (controller !== undefined
|
|
732
|
+
&& itemId !== null
|
|
733
|
+
&& origin.originRef !== null
|
|
734
|
+
&& origin.observedProviderResponseId !== null
|
|
735
|
+
&& event.response_id === origin.observedProviderResponseId
|
|
736
|
+
&& this.#projectConfirmationIsolation.isAuthorizationCarrier({
|
|
737
|
+
sessionEpoch: event.session_epoch,
|
|
738
|
+
userRevision: this.#ports.userOrigins.revisionForItem(event.session_epoch, itemId) ?? -1,
|
|
739
|
+
responseId: origin.observedProviderResponseId,
|
|
740
|
+
})
|
|
741
|
+
&& this.#isProjectConfirmationItem(event.session_epoch, itemId)) {
|
|
742
|
+
let text = null;
|
|
743
|
+
let expiryOwnsFact = false;
|
|
744
|
+
const decision = confirmArguments(event.arguments);
|
|
745
|
+
if (decision === null || decision.scope !== undefined) {
|
|
746
|
+
code = 'confirmation_invalid';
|
|
747
|
+
text = '确认请求无效,操作尚未执行。';
|
|
748
|
+
}
|
|
749
|
+
else {
|
|
750
|
+
const outcome = controller.acceptDecision({
|
|
751
|
+
epoch: event.session_epoch,
|
|
752
|
+
itemId,
|
|
753
|
+
proposalId: decision.id,
|
|
754
|
+
confirmed: decision.accepted,
|
|
755
|
+
});
|
|
756
|
+
if (outcome.kind === 'cancelled')
|
|
757
|
+
this.#ports.intake()?.decline(decision.id);
|
|
758
|
+
code = outcome.kind === 'ignored' ? 'confirmation_not_pending' : outcome.kind;
|
|
759
|
+
state = outcome.kind === 'confirmed' ? 'accepted' : 'refused';
|
|
760
|
+
text = outcome.response_text;
|
|
761
|
+
this.#publishProjectView();
|
|
762
|
+
if (outcome.kind !== 'invalid' && outcome.kind !== 'ignored') {
|
|
763
|
+
this.#beginProjectConfirmationClose(event.session_epoch, itemId);
|
|
764
|
+
try {
|
|
765
|
+
await this.#closeConfirmationDeferredCalls(itemId);
|
|
766
|
+
if (outcome.kind === 'confirmed' && outcome.operation !== null) {
|
|
767
|
+
const committed = await this.#commitConfirmedProjectOperation(outcome.operation);
|
|
768
|
+
state = committed.state;
|
|
769
|
+
text = committed.text;
|
|
770
|
+
expiryOwnsFact = committed.expiryOwnsFact;
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
finally {
|
|
774
|
+
this.#endProjectConfirmationClose(event.session_epoch, itemId);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
if (text !== null
|
|
779
|
+
&& !expiryOwnsFact
|
|
780
|
+
&& code !== 'confirmation_invalid'
|
|
781
|
+
&& code !== 'confirmation_not_pending') {
|
|
782
|
+
confirmationText = text;
|
|
783
|
+
confirmationResponseId = origin.observedProviderResponseId;
|
|
784
|
+
}
|
|
785
|
+
this.#publishProjectView();
|
|
786
|
+
}
|
|
787
|
+
const item = {
|
|
788
|
+
kind: 'tool_output',
|
|
789
|
+
host_item_id: this.#ports.idFactory(),
|
|
790
|
+
event_id: this.#ports.idFactory(),
|
|
791
|
+
call_id: event.call_id,
|
|
792
|
+
content: JSON.stringify({ code, state }),
|
|
793
|
+
};
|
|
794
|
+
const toolOutputInjected = await this.session.injectToolOutput(item);
|
|
795
|
+
// Empty text is a silent admission, but its carrier still needs settlement and fencing.
|
|
796
|
+
if (confirmationText !== null) {
|
|
797
|
+
if (toolOutputInjected && confirmationResponseId !== null) {
|
|
798
|
+
this.session.settleUserResponse(confirmationResponseId);
|
|
799
|
+
}
|
|
800
|
+
const carrierNeedsCancellation = confirmationResponseId === null
|
|
801
|
+
? false
|
|
802
|
+
: this.#prepareProjectConfirmationCarrier(event.session_epoch, confirmationResponseId);
|
|
803
|
+
if (confirmationText !== '')
|
|
804
|
+
this.#queueProjectConfirmationFact(confirmationText, this.#projectConfirmationLifecycleId(), `decision:${code}:${state}`);
|
|
805
|
+
if (carrierNeedsCancellation && confirmationResponseId !== null) {
|
|
806
|
+
this.#cancelProjectConfirmationCarrier(event.session_epoch, confirmationResponseId);
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
catch (cause) {
|
|
811
|
+
releaseDecisionGate?.();
|
|
812
|
+
this.#projectConfirmationDecisionGates.delete(call);
|
|
813
|
+
this.#projectConfirmationClosingCalls.delete(call);
|
|
814
|
+
throw cause;
|
|
815
|
+
}
|
|
816
|
+
releaseDecisionGate?.();
|
|
817
|
+
this.#projectConfirmationDecisionGates.delete(call);
|
|
818
|
+
this.#projectConfirmationClosingCalls.delete(call);
|
|
819
|
+
this.#rememberClosedProjectConfirmationCall(call);
|
|
820
|
+
}
|
|
821
|
+
async #commitConfirmedProjectOperation(operation) {
|
|
822
|
+
const intakeOperation = operation.intake_id !== undefined;
|
|
823
|
+
if (intakeOperation && this.#ports.intake()?.beginConfirmed(operation) !== true) {
|
|
824
|
+
this.#ports.projectConfirmation?.rejectConfirmed(operation);
|
|
825
|
+
return { state: 'failed', text: '计划已失效,尚未执行。请重新提出任务。', expiryOwnsFact: false };
|
|
826
|
+
}
|
|
827
|
+
const callback = this.#ports.commitProjectOperation;
|
|
828
|
+
const lifecycleId = operation.proposal_id;
|
|
829
|
+
this.#projectConfirmationCommittingLifecycles.add(lifecycleId);
|
|
830
|
+
this.#ports.telemetry?.record('project_confirmation.commit_started', {
|
|
831
|
+
session_epoch: this.session.sessionEpoch,
|
|
832
|
+
proposal_id: operation.proposal_id,
|
|
833
|
+
proposal_origin_ref: operation.origin_ref,
|
|
834
|
+
delegate_origin_ref: operation.origin_ref,
|
|
835
|
+
expires_at: operation.expires_at,
|
|
836
|
+
});
|
|
837
|
+
if (callback === undefined) {
|
|
838
|
+
if (intakeOperation)
|
|
839
|
+
this.#ports.intake()?.settleConfirmed({ accepted: false, code: 'callback_missing' });
|
|
840
|
+
this.#ports.projectConfirmation?.rollbackConfirmed(operation);
|
|
841
|
+
this.#ports.telemetry?.record(this.#ports.projectConfirmation?.pending === true
|
|
842
|
+
? 'project_confirmation.commit_rollback'
|
|
843
|
+
: 'project_confirmation.commit_settled', {
|
|
844
|
+
session_epoch: this.session.sessionEpoch,
|
|
845
|
+
proposal_id: operation.proposal_id,
|
|
846
|
+
proposal_origin_ref: operation.origin_ref,
|
|
847
|
+
delegate_origin_ref: operation.origin_ref,
|
|
848
|
+
code: 'callback_missing',
|
|
849
|
+
});
|
|
850
|
+
return this.#finishConfirmedProjectCommit(lifecycleId, {
|
|
851
|
+
state: 'failed',
|
|
852
|
+
text: '确认处理不可用,本次操作未执行。',
|
|
853
|
+
});
|
|
854
|
+
}
|
|
855
|
+
try {
|
|
856
|
+
const result = await callback(operation);
|
|
857
|
+
const controller = this.#ports.projectConfirmation;
|
|
858
|
+
this.#ports.telemetry?.record('project_confirmation.commit_admission', {
|
|
859
|
+
session_epoch: this.session.sessionEpoch,
|
|
860
|
+
proposal_id: operation.proposal_id,
|
|
861
|
+
proposal_origin_ref: operation.origin_ref,
|
|
862
|
+
delegate_origin_ref: operation.origin_ref,
|
|
863
|
+
accepted: result.accepted,
|
|
864
|
+
code: result.code,
|
|
865
|
+
delegate_id: result.delegate_id ?? 'none',
|
|
866
|
+
});
|
|
867
|
+
if (result.accepted && controller?.committing === true) {
|
|
868
|
+
if (intakeOperation)
|
|
869
|
+
this.#ports.intake()?.settleConfirmed({ accepted: false, code: 'confirmation_invalid' });
|
|
870
|
+
controller.rejectConfirmed(operation);
|
|
871
|
+
this.#ports.telemetry?.record('project_confirmation.commit_settled', {
|
|
872
|
+
session_epoch: this.session.sessionEpoch,
|
|
873
|
+
proposal_id: operation.proposal_id,
|
|
874
|
+
proposal_origin_ref: operation.origin_ref,
|
|
875
|
+
delegate_origin_ref: operation.origin_ref,
|
|
876
|
+
accepted: false,
|
|
877
|
+
code: 'confirmation_invalid',
|
|
878
|
+
delegate_id: result.delegate_id ?? 'none',
|
|
879
|
+
});
|
|
880
|
+
return this.#finishConfirmedProjectCommit(lifecycleId, {
|
|
881
|
+
state: 'failed',
|
|
882
|
+
text: '确认处理无效,本次操作未执行。',
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
if (!result.accepted && controller?.committing === true) {
|
|
886
|
+
if (result.code === 'runtime_rejected')
|
|
887
|
+
controller.rollbackConfirmed(operation);
|
|
888
|
+
else if (result.code !== 'confirmation_in_progress')
|
|
889
|
+
controller.rejectConfirmed(operation);
|
|
890
|
+
}
|
|
891
|
+
if (intakeOperation)
|
|
892
|
+
this.#ports.intake()?.settleConfirmed(result);
|
|
893
|
+
const transitionKind = result.code === 'confirmation_in_progress'
|
|
894
|
+
? 'project_confirmation.commit_duplicate_suppressed'
|
|
895
|
+
: controller?.pending === true
|
|
896
|
+
? 'project_confirmation.commit_rollback'
|
|
897
|
+
: 'project_confirmation.commit_settled';
|
|
898
|
+
this.#ports.telemetry?.record(transitionKind, {
|
|
899
|
+
session_epoch: this.session.sessionEpoch,
|
|
900
|
+
proposal_id: operation.proposal_id,
|
|
901
|
+
proposal_origin_ref: operation.origin_ref,
|
|
902
|
+
delegate_origin_ref: operation.origin_ref,
|
|
903
|
+
accepted: result.accepted,
|
|
904
|
+
code: result.code,
|
|
905
|
+
delegate_id: result.delegate_id ?? 'none',
|
|
906
|
+
});
|
|
907
|
+
return this.#finishConfirmedProjectCommit(lifecycleId, {
|
|
908
|
+
state: result.accepted ? 'accepted' : 'failed',
|
|
909
|
+
text: result.accepted
|
|
910
|
+
? projectCommitSuccessText(operation, result.code)
|
|
911
|
+
: result.code === 'confirmation_in_progress'
|
|
912
|
+
? ''
|
|
913
|
+
: projectCommitFailureText(result.code, this.#ports.coding?.display_name),
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
catch (failure) {
|
|
917
|
+
if (intakeOperation)
|
|
918
|
+
this.#ports.intake()?.settleConfirmed({ accepted: false, code: 'callback_failed' });
|
|
919
|
+
if (intakeOperation)
|
|
920
|
+
this.#ports.projectConfirmation?.rejectConfirmed(operation);
|
|
921
|
+
if (isAbort(failure)) {
|
|
922
|
+
this.#projectConfirmationCommittingLifecycles.delete(lifecycleId);
|
|
923
|
+
this.#projectConfirmationExpiryFactOwners.delete(lifecycleId);
|
|
924
|
+
throw failure;
|
|
925
|
+
}
|
|
926
|
+
if (!intakeOperation)
|
|
927
|
+
this.#ports.projectConfirmation?.rollbackConfirmed(operation);
|
|
928
|
+
this.#ports.telemetry?.record(this.#ports.projectConfirmation?.pending === true
|
|
929
|
+
? 'project_confirmation.commit_rollback'
|
|
930
|
+
: 'project_confirmation.commit_settled', {
|
|
931
|
+
session_epoch: this.session.sessionEpoch,
|
|
932
|
+
proposal_id: operation.proposal_id,
|
|
933
|
+
proposal_origin_ref: operation.origin_ref,
|
|
934
|
+
delegate_origin_ref: operation.origin_ref,
|
|
935
|
+
code: 'callback_failed',
|
|
936
|
+
});
|
|
937
|
+
return this.#finishConfirmedProjectCommit(lifecycleId, {
|
|
938
|
+
state: 'failed',
|
|
939
|
+
text: intakeOperation ? '' : '已确认,但操作未执行。',
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
#finishConfirmedProjectCommit(lifecycleId, result) {
|
|
944
|
+
this.#projectConfirmationCommittingLifecycles.delete(lifecycleId);
|
|
945
|
+
return {
|
|
946
|
+
...result,
|
|
947
|
+
expiryOwnsFact: this.#projectConfirmationExpiryFactOwners.delete(lifecycleId),
|
|
948
|
+
};
|
|
949
|
+
}
|
|
950
|
+
/**
|
|
951
|
+
* End one answer whose confirmation function cannot be tied back to a provider response.
|
|
952
|
+
*
|
|
953
|
+
* Refusing the function is necessary but insufficient: leaving the controller reservation alive
|
|
954
|
+
* turns every later utterance into a shadow and makes a still-valid proposal impossible to answer.
|
|
955
|
+
* Recovery is deliberately narrow -- current epoch, one exact reserved item, and a live proposal --
|
|
956
|
+
* and grants no authority. It only lets the user make a fresh, bindable attempt.
|
|
957
|
+
*/
|
|
958
|
+
async #recoverUnboundProjectConfirmation(event) {
|
|
959
|
+
const controller = this.#ports.projectConfirmation;
|
|
960
|
+
if (controller?.pending !== true
|
|
961
|
+
|| event.session_epoch !== this.session.sessionEpoch
|
|
962
|
+
|| this.#projectConfirmationIsolation.responseFencePending)
|
|
963
|
+
return false;
|
|
964
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
965
|
+
const itemId = reserved?.sessionEpoch === event.session_epoch ? reserved.itemId : undefined;
|
|
966
|
+
if (itemId === undefined || !controller.releaseUndecided({
|
|
967
|
+
epoch: event.session_epoch,
|
|
968
|
+
itemId,
|
|
969
|
+
}))
|
|
970
|
+
return false;
|
|
971
|
+
this.#beginProjectConfirmationClose(event.session_epoch, itemId);
|
|
972
|
+
try {
|
|
973
|
+
await this.#closeConfirmationDeferredCalls(itemId);
|
|
974
|
+
}
|
|
975
|
+
finally {
|
|
976
|
+
this.#endProjectConfirmationClose(event.session_epoch, itemId);
|
|
977
|
+
}
|
|
978
|
+
this.#ports.failOriginTranscriptAndRefresh(event.session_epoch, itemId);
|
|
979
|
+
this.#queueProjectConfirmationFact('我没能把这次语音和确认请求关联起来;请再说一次“确认”或“取消”。', this.#projectConfirmationLifecycleId(), `binding-missing:${callKey(event.session_epoch, itemId)}`);
|
|
980
|
+
this.#publishProjectView();
|
|
981
|
+
return true;
|
|
982
|
+
}
|
|
983
|
+
/** Transcription failed: release this answer and ask again without granting authority. */
|
|
984
|
+
async #failProjectConfirmation(epoch, itemId) {
|
|
985
|
+
if (this.#projectConfirmationClosingItems.has(callKey(epoch, itemId))) {
|
|
986
|
+
await this.#closeConfirmationDeferredCalls(itemId);
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
this.#beginProjectConfirmationClose(epoch, itemId);
|
|
990
|
+
try {
|
|
991
|
+
await this.#closeConfirmationDeferredCalls(itemId);
|
|
992
|
+
}
|
|
993
|
+
finally {
|
|
994
|
+
this.#endProjectConfirmationClose(epoch, itemId);
|
|
995
|
+
}
|
|
996
|
+
const controller = this.#ports.projectConfirmation;
|
|
997
|
+
if (controller === undefined)
|
|
998
|
+
return;
|
|
999
|
+
const outcome = controller.failTranscript({ epoch, itemId });
|
|
1000
|
+
if (outcome.response_text !== null && outcome.response_text !== '') {
|
|
1001
|
+
this.#queueProjectConfirmationFact(outcome.response_text, this.#projectConfirmationLifecycleId(), `transcript-failed:${callKey(epoch, itemId)}`);
|
|
1002
|
+
}
|
|
1003
|
+
this.#publishProjectView();
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* Give the provider a terminal result for a tool call the confirmation refused.
|
|
1007
|
+
*
|
|
1008
|
+
* Reserved *before* the first await: expiry cleanup and a provider event can both reach the same
|
|
1009
|
+
* call, and two terminal outputs for one function call is a protocol violation. Cleared on failure so
|
|
1010
|
+
* a retry is possible; recorded on success so a later attempt is a no-op.
|
|
1011
|
+
*/
|
|
1012
|
+
async closeProjectConfirmationTool(event) {
|
|
1013
|
+
const key = callKey(event.session_epoch, event.call_id);
|
|
1014
|
+
if (this.#projectConfirmationClosingCalls.has(key)
|
|
1015
|
+
|| this.#projectConfirmationClosedCalls.has(key)) {
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
this.#projectConfirmationClosingCalls.add(key);
|
|
1019
|
+
const item = {
|
|
1020
|
+
kind: 'tool_output',
|
|
1021
|
+
host_item_id: this.#ports.idFactory(),
|
|
1022
|
+
event_id: this.#ports.idFactory(),
|
|
1023
|
+
call_id: event.call_id,
|
|
1024
|
+
content: '{"code":"confirmation_reserved","state":"superseded"}',
|
|
1025
|
+
};
|
|
1026
|
+
try {
|
|
1027
|
+
await this.session.injectToolOutput(item);
|
|
1028
|
+
}
|
|
1029
|
+
catch (cause) {
|
|
1030
|
+
this.#projectConfirmationClosingCalls.delete(key);
|
|
1031
|
+
throw cause;
|
|
1032
|
+
}
|
|
1033
|
+
this.#projectConfirmationClosingCalls.delete(key);
|
|
1034
|
+
this.#rememberClosedProjectConfirmationCall(key);
|
|
1035
|
+
}
|
|
1036
|
+
#rememberClosedProjectConfirmationCall(key) {
|
|
1037
|
+
this.#projectConfirmationClosedCalls.delete(key);
|
|
1038
|
+
this.#projectConfirmationClosedCalls.set(key, null);
|
|
1039
|
+
while (this.#projectConfirmationClosedCalls.size > MAX_TRACKED_TOOL_CALLS) {
|
|
1040
|
+
const oldest = this.#projectConfirmationClosedCalls.keys().next();
|
|
1041
|
+
if (oldest.done === true)
|
|
1042
|
+
break;
|
|
1043
|
+
this.#projectConfirmationClosedCalls.delete(oldest.value);
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* Refuse the tool calls that were waiting on this transcript.
|
|
1048
|
+
*
|
|
1049
|
+
* Detached before awaiting: rebuilding the queue from a snapshot after provider I/O would overwrite
|
|
1050
|
+
* calls a concurrent event appended in the meantime.
|
|
1051
|
+
*/
|
|
1052
|
+
async #closeConfirmationDeferredCalls(itemId) {
|
|
1053
|
+
const matching = this.#ports.continuations.takeDeferredForItem(itemId);
|
|
1054
|
+
for (const call of matching) {
|
|
1055
|
+
await this.closeProjectConfirmationTool(call.event);
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
/** Say something to the user about the confirmation. Just below user priority: urgent, not louder. */
|
|
1059
|
+
#queueProjectConfirmationFact(text, lifecycleId, transition) {
|
|
1060
|
+
this.#ports.queueHostItem(hostFactIntent({
|
|
1061
|
+
kind: 'final',
|
|
1062
|
+
host_item_id: this.#ports.idFactory(),
|
|
1063
|
+
event_id: projectConfirmationEventId('project-confirmation', lifecycleId, transition),
|
|
1064
|
+
content: [...text].slice(0, MAX_HOST_FACT_CHARS).join(''),
|
|
1065
|
+
}), { priority: USER_PRIORITY - 1, preemptive: false });
|
|
1066
|
+
this.#ports.wakeDelivery();
|
|
1067
|
+
}
|
|
1068
|
+
/** Transfer reply ownership locally before any provider I/O can delay the deterministic fact. */
|
|
1069
|
+
#prepareProjectConfirmationCarrier(sessionEpoch, responseId) {
|
|
1070
|
+
const phase = this.session.providerTurnPhase(responseId);
|
|
1071
|
+
const generation = this.session.currentGeneration;
|
|
1072
|
+
const live = sessionEpoch === this.session.sessionEpoch && (this.session.activeProviderResponseId === responseId
|
|
1073
|
+
|| phase === 'active'
|
|
1074
|
+
|| phase === 'cancel_requested'
|
|
1075
|
+
|| (generation !== null
|
|
1076
|
+
&& generation.session_epoch === sessionEpoch
|
|
1077
|
+
&& generation.response_id === responseId));
|
|
1078
|
+
if (live) {
|
|
1079
|
+
this.#projectConfirmationIsolation.markQuarantined({ sessionEpoch, responseId });
|
|
1080
|
+
}
|
|
1081
|
+
this.session.suppressResponse(responseId);
|
|
1082
|
+
this.#ports.continuations.abandonProjectConfirmationContinuation(sessionEpoch, responseId);
|
|
1083
|
+
return live;
|
|
1084
|
+
}
|
|
1085
|
+
/** Cancel the carrier without holding up the event that queued its host-owned reply. */
|
|
1086
|
+
#cancelProjectConfirmationCarrier(sessionEpoch, responseId) {
|
|
1087
|
+
const cancellation = (async () => {
|
|
1088
|
+
try {
|
|
1089
|
+
const targeted = await this.session.quarantineResponse(responseId);
|
|
1090
|
+
if (!targeted) {
|
|
1091
|
+
this.#projectConfirmationIsolation.clearQuarantined({ sessionEpoch, responseId });
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
if (this.session.providerTurnPhase(responseId) !== 'cancel_requested') {
|
|
1095
|
+
// The carrier may have reached terminal while tool output was being confirmed even though
|
|
1096
|
+
// its audio was still queued. The exact fence was still required, but no provider terminal
|
|
1097
|
+
// remains to release a quarantine entry or justify a reconnect watchdog.
|
|
1098
|
+
this.#projectConfirmationIsolation.clearQuarantined({ sessionEpoch, responseId });
|
|
1099
|
+
return;
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
catch (failure) {
|
|
1103
|
+
this.#ports.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
1104
|
+
? failure
|
|
1105
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
1106
|
+
await this.recoverProjectConfirmationCarrier(sessionEpoch, responseId, 'cancel_failed');
|
|
1107
|
+
return;
|
|
1108
|
+
}
|
|
1109
|
+
await this.#projectConfirmationCarrierReleaseWatchdog(sessionEpoch, responseId);
|
|
1110
|
+
})();
|
|
1111
|
+
this.#trackProjectConfirmationCarrierRelease(cancellation);
|
|
1112
|
+
}
|
|
1113
|
+
#trackProjectConfirmationCarrierRelease(work) {
|
|
1114
|
+
const task = work.catch((failure) => {
|
|
1115
|
+
if (!isAbort(failure)) {
|
|
1116
|
+
this.#ports.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
1117
|
+
? failure
|
|
1118
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
1119
|
+
}
|
|
1120
|
+
}).finally(() => {
|
|
1121
|
+
this.#projectConfirmationCarrierReleaseTasks.delete(task);
|
|
1122
|
+
});
|
|
1123
|
+
this.#projectConfirmationCarrierReleaseTasks.add(task);
|
|
1124
|
+
}
|
|
1125
|
+
async #projectConfirmationCarrierReleaseWatchdog(sessionEpoch, responseId) {
|
|
1126
|
+
await this.#ports.clock.sleep(PROJECT_CONFIRMATION_CARRIER_RELEASE_TIMEOUT_S, this.#ports.stopSignal());
|
|
1127
|
+
await this.recoverProjectConfirmationCarrier(sessionEpoch, responseId, 'terminal_timeout');
|
|
1128
|
+
}
|
|
1129
|
+
async recoverProjectConfirmationCarrier(sessionEpoch, responseId, reason) {
|
|
1130
|
+
const key = callKey(sessionEpoch, responseId);
|
|
1131
|
+
if (sessionEpoch !== this.session.sessionEpoch
|
|
1132
|
+
|| this.#projectConfirmationIsolation.responseState({ sessionEpoch, responseId })
|
|
1133
|
+
?.quarantined !== true
|
|
1134
|
+
|| this.session.providerTurnPhase(responseId) !== 'cancel_requested')
|
|
1135
|
+
return;
|
|
1136
|
+
if (this.session.floor.state === 'user_speaking') {
|
|
1137
|
+
const alreadyDeferred = this.#projectConfirmationCarrierReconnectAfterUser.has(key);
|
|
1138
|
+
this.#projectConfirmationCarrierReconnectAfterUser.set(key, {
|
|
1139
|
+
sessionEpoch,
|
|
1140
|
+
responseId,
|
|
1141
|
+
reason,
|
|
1142
|
+
});
|
|
1143
|
+
if (!alreadyDeferred) {
|
|
1144
|
+
this.#trackProjectConfirmationCarrierRelease(this.#recoverProjectConfirmationCarrierAfterStaleUserHold(key));
|
|
1145
|
+
}
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
this.#projectConfirmationCarrierReconnectAfterUser.delete(key);
|
|
1149
|
+
this.#ports.telemetry?.record('project_confirmation.carrier_recovery', {
|
|
1150
|
+
session_epoch: sessionEpoch,
|
|
1151
|
+
response_id: responseId,
|
|
1152
|
+
reason,
|
|
1153
|
+
});
|
|
1154
|
+
await this.#ports.reconnectProviderSession({
|
|
1155
|
+
reason: 'project_confirmation_carrier_recovery',
|
|
1156
|
+
expectedEpoch: sessionEpoch,
|
|
1157
|
+
});
|
|
1158
|
+
}
|
|
1159
|
+
async #recoverProjectConfirmationCarrierAfterStaleUserHold(key) {
|
|
1160
|
+
if (!await this.session.waitForStaleHold(USER_HOLD_MAX_S))
|
|
1161
|
+
return;
|
|
1162
|
+
const pending = this.#projectConfirmationCarrierReconnectAfterUser.get(key);
|
|
1163
|
+
if (pending === undefined)
|
|
1164
|
+
return;
|
|
1165
|
+
if (this.session.releaseStaleUserHold(USER_HOLD_MAX_S)) {
|
|
1166
|
+
this.#ports.onDiagnostic('[realtime-diagnostic] project_confirmation_stale_user_hold_released');
|
|
1167
|
+
}
|
|
1168
|
+
await this.recoverProjectConfirmationCarrier(pending.sessionEpoch, pending.responseId, pending.reason);
|
|
1169
|
+
}
|
|
1170
|
+
async resumeProjectConfirmationCarrierRecoveryAfterUser() {
|
|
1171
|
+
if (this.session.floor.state === 'user_speaking')
|
|
1172
|
+
return;
|
|
1173
|
+
const pending = [...this.#projectConfirmationCarrierReconnectAfterUser.values()];
|
|
1174
|
+
this.#projectConfirmationCarrierReconnectAfterUser.clear();
|
|
1175
|
+
for (const carrier of pending) {
|
|
1176
|
+
await this.recoverProjectConfirmationCarrier(carrier.sessionEpoch, carrier.responseId, carrier.reason);
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
async #quarantineProjectConfirmationResponses(sessionEpoch) {
|
|
1180
|
+
const carriers = [];
|
|
1181
|
+
for (const response of this.#projectConfirmationIsolation.blockedResponses) {
|
|
1182
|
+
if (response.sessionEpoch !== sessionEpoch)
|
|
1183
|
+
continue;
|
|
1184
|
+
if (this.#prepareProjectConfirmationCarrier(sessionEpoch, response.responseId)) {
|
|
1185
|
+
carriers.push(response.responseId);
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
this.session.armPendingResponseFence();
|
|
1189
|
+
if (this.#projectConfirmationPendingQuarantineEpoch !== null
|
|
1190
|
+
&& this.#projectConfirmationPendingQuarantineEpoch === sessionEpoch) {
|
|
1191
|
+
try {
|
|
1192
|
+
await this.session.quarantineActiveOrAwaitingResponse();
|
|
1193
|
+
}
|
|
1194
|
+
catch (failure) {
|
|
1195
|
+
this.#ports.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
1196
|
+
? failure
|
|
1197
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
for (const responseId of carriers) {
|
|
1201
|
+
this.#cancelProjectConfirmationCarrier(sessionEpoch, responseId);
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
/** The proposal id is the lifecycle key; settlement deliberately does not erase it. */
|
|
1205
|
+
#projectConfirmationLifecycleId() {
|
|
1206
|
+
const lifecycleId = this.#ports.projectConfirmation?.lifecycleId;
|
|
1207
|
+
if (lifecycleId !== null && lifecycleId !== undefined)
|
|
1208
|
+
return lifecycleId;
|
|
1209
|
+
this.#ports.onDiagnostic('[realtime-diagnostic] project_confirmation_lifecycle_missing');
|
|
1210
|
+
return `session:${this.session.sessionEpoch}`;
|
|
1211
|
+
}
|
|
1212
|
+
/**
|
|
1213
|
+
* The proposal timed out on its own.
|
|
1214
|
+
*
|
|
1215
|
+
* Batched and drained by one task rather than handled inline, because cleanup involves provider I/O
|
|
1216
|
+
* and possibly a reconnect -- and the expiry observer is called from a timer that must not be left
|
|
1217
|
+
* awaiting either. A second expiry while one is draining joins the queue instead of racing it.
|
|
1218
|
+
*/
|
|
1219
|
+
#projectConfirmationExpired() {
|
|
1220
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
1221
|
+
const itemKeys = reserved === null
|
|
1222
|
+
? []
|
|
1223
|
+
: [callKey(reserved.sessionEpoch, reserved.itemId)];
|
|
1224
|
+
const sourceEpoch = this.session.sessionEpoch;
|
|
1225
|
+
const lifecycleId = this.#projectConfirmationLifecycleId();
|
|
1226
|
+
if (this.#projectConfirmationCommittingLifecycles.has(lifecycleId)) {
|
|
1227
|
+
this.#projectConfirmationExpiryFactOwners.add(lifecycleId);
|
|
1228
|
+
}
|
|
1229
|
+
// A reconnect is needed when the confirmation armed a fence or blocked a response in this epoch:
|
|
1230
|
+
// either leaves provider state the next turn would otherwise inherit.
|
|
1231
|
+
const reconnect = this.#projectConfirmationIsolation.responseFencePending
|
|
1232
|
+
|| (this.#projectConfirmationDecisionRetry?.requested === true
|
|
1233
|
+
&& this.#projectConfirmationDecisionRetry.retry_response_id === null)
|
|
1234
|
+
|| this.#projectConfirmationIsolation.hasBlockedResponseInEpoch(sourceEpoch);
|
|
1235
|
+
for (const key of itemKeys) {
|
|
1236
|
+
const { sessionEpoch, id } = parseCallKey(key);
|
|
1237
|
+
this.#beginProjectConfirmationClose(sessionEpoch, id);
|
|
1238
|
+
}
|
|
1239
|
+
this.#projectExpiryBatches.push({
|
|
1240
|
+
item_keys: itemKeys,
|
|
1241
|
+
source_epoch: sourceEpoch,
|
|
1242
|
+
reconnect,
|
|
1243
|
+
lifecycle_id: lifecycleId,
|
|
1244
|
+
});
|
|
1245
|
+
this.#startProjectConfirmationExpiryDrain();
|
|
1246
|
+
this.#publishProjectView();
|
|
1247
|
+
}
|
|
1248
|
+
/** Do not reconnect or inject expiry facts beside an in-flight confirmation tool output. */
|
|
1249
|
+
#startProjectConfirmationExpiryDrain() {
|
|
1250
|
+
if (this.#projectExpiryDraining !== null
|
|
1251
|
+
|| this.#projectExpiryBatches.length === 0)
|
|
1252
|
+
return;
|
|
1253
|
+
const signal = this.#ports.stopSignal();
|
|
1254
|
+
this.#projectExpiryDraining = this.#drainProjectConfirmationExpiries(signal)
|
|
1255
|
+
.catch((failure) => {
|
|
1256
|
+
this.#ports.onDiagnostic(`[realtime-diagnostic] project_expiry_failure type=${diagnosticName(failure)}`);
|
|
1257
|
+
})
|
|
1258
|
+
.finally(() => {
|
|
1259
|
+
this.#projectExpiryDraining = null;
|
|
1260
|
+
this.#startProjectConfirmationExpiryDrain();
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
async #drainProjectConfirmationExpiries(signal) {
|
|
1264
|
+
for (;;) {
|
|
1265
|
+
if (signal.aborted)
|
|
1266
|
+
return;
|
|
1267
|
+
const batch = this.#projectExpiryBatches.shift();
|
|
1268
|
+
if (batch === undefined)
|
|
1269
|
+
return;
|
|
1270
|
+
await this.#finishProjectConfirmationExpiry(batch, signal);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Clean up after one expired proposal.
|
|
1275
|
+
*
|
|
1276
|
+
* Every step is deadlined, because each one talks to a provider that may not answer and an expiry
|
|
1277
|
+
* that hangs leaves the confirmation state blocking every later turn. A step that times out is
|
|
1278
|
+
* treated as a failure of that step, not of the expiry: the loop carries on and the user is still
|
|
1279
|
+
* told the proposal lapsed.
|
|
1280
|
+
*
|
|
1281
|
+
* The re-drain loop matters: closing a call awaits, and a provider event during that await can defer
|
|
1282
|
+
* another call for the same epoch. Taking the queue once would leave it behind.
|
|
1283
|
+
*/
|
|
1284
|
+
async #finishProjectConfirmationExpiry(batch, signal) {
|
|
1285
|
+
const clearRevision = this.#ports.conversationClearRevision();
|
|
1286
|
+
if (this.#ports.clearingConversation())
|
|
1287
|
+
return;
|
|
1288
|
+
let closeFailed = false;
|
|
1289
|
+
const decisionGates = [...this.#projectConfirmationDecisionGates]
|
|
1290
|
+
.filter(([key]) => parseCallKey(key).sessionEpoch === batch.source_epoch)
|
|
1291
|
+
.map(([, gate]) => gate);
|
|
1292
|
+
if (decisionGates.length > 0) {
|
|
1293
|
+
const completed = await this.#runProjectExpiryStep(Promise.all(decisionGates));
|
|
1294
|
+
closeFailed = closeFailed || !completed;
|
|
1295
|
+
}
|
|
1296
|
+
for (;;) {
|
|
1297
|
+
// Checked at every resumption point, not just on entry: each close awaits the provider, and the
|
|
1298
|
+
// service can be closed during any of them. Reconnecting or injecting after that would be a
|
|
1299
|
+
// stopped service talking to a provider it has already released.
|
|
1300
|
+
if (signal.aborted
|
|
1301
|
+
|| this.#ports.clearingConversation()
|
|
1302
|
+
|| clearRevision !== this.#ports.conversationClearRevision())
|
|
1303
|
+
return;
|
|
1304
|
+
const deferred = this.#ports.continuations.takeConfirmationDeferredCalls(batch.source_epoch);
|
|
1305
|
+
if (deferred.length === 0)
|
|
1306
|
+
break;
|
|
1307
|
+
for (const call of deferred) {
|
|
1308
|
+
try {
|
|
1309
|
+
const completed = await this.#runProjectExpiryStep(this.closeProjectConfirmationTool(call.event));
|
|
1310
|
+
closeFailed = closeFailed || !completed;
|
|
1311
|
+
if (this.#ports.clearingConversation()
|
|
1312
|
+
|| clearRevision !== this.#ports.conversationClearRevision())
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
catch {
|
|
1316
|
+
closeFailed = true;
|
|
1317
|
+
}
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
if (signal.aborted
|
|
1321
|
+
|| this.#ports.clearingConversation()
|
|
1322
|
+
|| clearRevision !== this.#ports.conversationClearRevision())
|
|
1323
|
+
return;
|
|
1324
|
+
if (batch.reconnect || closeFailed) {
|
|
1325
|
+
try {
|
|
1326
|
+
await this.#runProjectExpiryStep(this.#ports.reconnectProviderSession({
|
|
1327
|
+
reason: 'project_confirmation_expiry_cleanup',
|
|
1328
|
+
expectedEpoch: batch.source_epoch,
|
|
1329
|
+
}));
|
|
1330
|
+
}
|
|
1331
|
+
catch (failure) {
|
|
1332
|
+
this.#ports.onDiagnostic(`[realtime-diagnostic] project_expiry_reconnect_failure type=${diagnosticName(failure)}`);
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
// The items are released even at shutdown: leaving one closing would block a service that is
|
|
1336
|
+
// restarted. Only the provider-facing half below is skipped.
|
|
1337
|
+
for (const key of batch.item_keys) {
|
|
1338
|
+
const { sessionEpoch, id } = parseCallKey(key);
|
|
1339
|
+
this.#endProjectConfirmationClose(sessionEpoch, id);
|
|
1340
|
+
}
|
|
1341
|
+
if (signal.aborted
|
|
1342
|
+
|| this.#ports.clearingConversation()
|
|
1343
|
+
|| clearRevision !== this.#ports.conversationClearRevision())
|
|
1344
|
+
return;
|
|
1345
|
+
this.#queueProjectConfirmationFact('确认已过期,本次操作已取消。', batch.lifecycle_id, 'expired');
|
|
1346
|
+
try {
|
|
1347
|
+
await this.#runProjectExpiryStep(this.#ports.deliveryPass());
|
|
1348
|
+
}
|
|
1349
|
+
catch (failure) {
|
|
1350
|
+
this.#ports.onDiagnostic(`[realtime-diagnostic] project_expiry_delivery_failure type=${diagnosticName(failure)}`);
|
|
1351
|
+
}
|
|
1352
|
+
this.#publishProjectView();
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Run one cleanup step, or give up on it.
|
|
1356
|
+
*
|
|
1357
|
+
* Returns whether it finished. A step that did not is abandoned rather than awaited: the work may
|
|
1358
|
+
* still complete in the background, and the alternative is an expiry that never ends.
|
|
1359
|
+
*/
|
|
1360
|
+
async #runProjectExpiryStep(work) {
|
|
1361
|
+
// Attached now so a rejection after the deadline is not an unhandled one.
|
|
1362
|
+
const settled = work.then(() => true, () => false);
|
|
1363
|
+
let timer;
|
|
1364
|
+
const deadline = new Promise(resolve => {
|
|
1365
|
+
timer = setTimeout(() => resolve(false), this.#ports.projectExpiryStepTimeoutMs);
|
|
1366
|
+
});
|
|
1367
|
+
try {
|
|
1368
|
+
return await Promise.race([settled, deadline]);
|
|
1369
|
+
}
|
|
1370
|
+
finally {
|
|
1371
|
+
if (timer !== undefined)
|
|
1372
|
+
clearTimeout(timer);
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
#publishProjectView() {
|
|
1376
|
+
const controller = this.#ports.projectConfirmation;
|
|
1377
|
+
if (controller === undefined)
|
|
1378
|
+
return;
|
|
1379
|
+
try {
|
|
1380
|
+
this.#ports.onProjectView?.(this.#ports.projectViewProvider?.(controller.pending || controller.committing) ?? controller.view);
|
|
1381
|
+
}
|
|
1382
|
+
catch {
|
|
1383
|
+
// A renderer that cannot accept the view must not prevent the state change that produced it.
|
|
1384
|
+
}
|
|
1385
|
+
// Spec 08: an executor approval that waited behind this confirmation gets a fresh TTL and is
|
|
1386
|
+
// voice-armed once it is over. `release` publishes, and the observer runs the host sync
|
|
1387
|
+
// with the re-armed view; a head that was never held is synced directly.
|
|
1388
|
+
if (!controller.pending && !controller.committing)
|
|
1389
|
+
this.#ports.approvalHost.release();
|
|
1390
|
+
}
|
|
1391
|
+
/**
|
|
1392
|
+
* Drop the proposal and every trace of its isolation.
|
|
1393
|
+
*
|
|
1394
|
+
* Called when the world the proposal described has changed underneath it -- a reconnect, a new
|
|
1395
|
+
* provider session -- so confirming it would commit against a context the user never saw.
|
|
1396
|
+
*/
|
|
1397
|
+
invalidateProjectConfirmation(reason) {
|
|
1398
|
+
if (reason !== 'intake_amended')
|
|
1399
|
+
this.#ports.intake()?.cancel();
|
|
1400
|
+
this.#ports.projectConfirmation?.invalidate(reason);
|
|
1401
|
+
this.#projectConfirmationIsolation.invalidate();
|
|
1402
|
+
this.#projectConfirmationShadowItems.clear();
|
|
1403
|
+
this.#projectConfirmationClosingItems.clear();
|
|
1404
|
+
this.#projectConfirmationCarrierReconnectAfterUser.clear();
|
|
1405
|
+
this.#projectConfirmationPendingQuarantineEpoch = null;
|
|
1406
|
+
this.#projectConfirmationDecisionRetry = null;
|
|
1407
|
+
this.#publishProjectView();
|
|
1408
|
+
}
|
|
1409
|
+
/** Which responses a confirmation has blocked. The block outliving its turn is the failure mode. */
|
|
1410
|
+
get confirmationResponsesForTest() {
|
|
1411
|
+
return this.#projectConfirmationIsolation.blockedResponses
|
|
1412
|
+
.map(response => callKey(response.sessionEpoch, response.responseId));
|
|
1413
|
+
}
|
|
1414
|
+
/** Items reserved as the answer to a proposal. One left here blocks every later turn. */
|
|
1415
|
+
get confirmationItemsForTest() {
|
|
1416
|
+
const reserved = this.#projectConfirmationIsolation.reservation;
|
|
1417
|
+
return reserved === null ? [] : [callKey(reserved.sessionEpoch, reserved.itemId)];
|
|
1418
|
+
}
|
|
1419
|
+
/** Items mid-close. One left here after an expiry would block every later turn. */
|
|
1420
|
+
get confirmationClosingItemsForTest() {
|
|
1421
|
+
return [...this.#projectConfirmationClosingItems];
|
|
1422
|
+
}
|
|
1423
|
+
/** Whether a confirmation is currently refusing tool calls. Invisible from outside otherwise. */
|
|
1424
|
+
get projectConfirmationBlockingForTest() {
|
|
1425
|
+
return this.#projectConfirmationIsBlocking();
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
//# sourceMappingURL=project-confirmation-flow.js.map
|