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,1328 @@
|
|
|
1
|
+
import { PendingDecision } from './pending-decision.js';
|
|
2
|
+
import { APPROVAL_TTL_SECONDS } from './approval-port.js';
|
|
3
|
+
import { MAX_CONCURRENT_WORK, confirmArguments } from './work-tools.js';
|
|
4
|
+
import { codePointLengthLikePython, isWellFormed } from '../text/python-text.js';
|
|
5
|
+
import { USER_PRIORITY } from './memory.js';
|
|
6
|
+
import { ConfirmationTurnIsolation } from '../realtime/confirmation-turn-isolation.js';
|
|
7
|
+
import { RealtimeDeliveryError } from '../realtime/session.js';
|
|
8
|
+
import { MAX_HOST_FACT_CHARS, MAX_TRACKED_TOOL_CALLS, callKey, hostFactIntent } from '../realtime/service-state.js';
|
|
9
|
+
const APPROVAL_ID_LIMIT = 128;
|
|
10
|
+
/**
|
|
11
|
+
* Owns the executor permission FIFO (spec 08): exactly one approval is voice-visible (the head); later
|
|
12
|
+
* offers queue with their server request still open and get a full TTL once they become head.
|
|
13
|
+
*/
|
|
14
|
+
export class HostApprovalController {
|
|
15
|
+
#clock;
|
|
16
|
+
#decision;
|
|
17
|
+
#idFactory;
|
|
18
|
+
#onDiagnostic;
|
|
19
|
+
#capacity;
|
|
20
|
+
#observers = [];
|
|
21
|
+
#holds = new Set();
|
|
22
|
+
#current = null;
|
|
23
|
+
#queue = [];
|
|
24
|
+
constructor(options) {
|
|
25
|
+
this.#clock = options.clock;
|
|
26
|
+
this.#decision = new PendingDecision(options.clock);
|
|
27
|
+
this.#idFactory = options.idFactory;
|
|
28
|
+
this.#onDiagnostic = options.onDiagnostic;
|
|
29
|
+
this.#capacity = options.capacity ?? MAX_CONCURRENT_WORK;
|
|
30
|
+
}
|
|
31
|
+
get view() {
|
|
32
|
+
const current = this.#current;
|
|
33
|
+
if (current === null)
|
|
34
|
+
return emptyView();
|
|
35
|
+
return {
|
|
36
|
+
pending_approval: true,
|
|
37
|
+
pending_approval_busy: current.state === 'responding',
|
|
38
|
+
pending_approval_id: current.id,
|
|
39
|
+
...(current.offer.executorIdentity === undefined ? {} : { executorIdentity: current.offer.executorIdentity }),
|
|
40
|
+
kind: current.offer.kind,
|
|
41
|
+
local_detail: current.offer.local_detail,
|
|
42
|
+
operation_summary: current.offer.operation_summary,
|
|
43
|
+
expires_at: current.expiresAt,
|
|
44
|
+
...(current.offer.allowed_decisions === undefined ? {} : { allowed_decisions: current.offer.allowed_decisions }),
|
|
45
|
+
work: current.work,
|
|
46
|
+
queued: this.#queue.length,
|
|
47
|
+
...(current.held ? { held: true } : {}),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** The same surface bound to one running work: its offers carry `work`, its invalidations touch only its own entries. */
|
|
51
|
+
forWork(work) {
|
|
52
|
+
return {
|
|
53
|
+
offer: (input, signal) => this.offer(input, signal, work),
|
|
54
|
+
consume: resolution => this.consume(resolution),
|
|
55
|
+
invalidate: reason => this.invalidateWork(work.work_id, reason),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
get pending() {
|
|
59
|
+
return this.#current?.state === 'pending' && (this.#current.held || this.#clock.now() < this.#current.expiresAt);
|
|
60
|
+
}
|
|
61
|
+
/** Park the head (spec 08: it waits behind a project confirmation). The timer stops; a renderer click still decides. */
|
|
62
|
+
hold(reason = 'project') {
|
|
63
|
+
if (this.#holds.has(reason))
|
|
64
|
+
return false;
|
|
65
|
+
const current = this.#current;
|
|
66
|
+
if (!current && reason === 'project')
|
|
67
|
+
return false;
|
|
68
|
+
if (current?.state === 'pending' && !current.held && this.#clock.now() >= current.expiresAt) {
|
|
69
|
+
this.#drop(current);
|
|
70
|
+
return this.hold(reason);
|
|
71
|
+
}
|
|
72
|
+
this.#holds.add(reason);
|
|
73
|
+
if (reason === 'background')
|
|
74
|
+
for (const entry of [this.#current, ...this.#queue])
|
|
75
|
+
if (entry)
|
|
76
|
+
entry.awaitingPresentation = true;
|
|
77
|
+
if (current?.state === 'pending') {
|
|
78
|
+
current.held = true;
|
|
79
|
+
this.#decision.hold(current);
|
|
80
|
+
this.#publish();
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/** Resume only after every independent pause owner has released its reason. */
|
|
85
|
+
release(reason = 'project', options = {}) {
|
|
86
|
+
const current = this.#current;
|
|
87
|
+
const removed = this.#holds.delete(reason);
|
|
88
|
+
if (!removed && !(reason === 'background' && current?.awaitingPresentation))
|
|
89
|
+
return false;
|
|
90
|
+
if (reason === 'background' && current && !options.awaitPresentation)
|
|
91
|
+
current.awaitingPresentation = false;
|
|
92
|
+
if (this.#holds.size === 0 && current?.state === 'pending' && current.held && !current.awaitingPresentation) {
|
|
93
|
+
current.held = false;
|
|
94
|
+
current.expiresAt = this.#clock.now() + APPROVAL_TTL_SECONDS;
|
|
95
|
+
this.#decision.release(current, current.expiresAt);
|
|
96
|
+
this.#publish();
|
|
97
|
+
}
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
observe(observer) {
|
|
101
|
+
this.#observers.push(observer);
|
|
102
|
+
return () => {
|
|
103
|
+
const index = this.#observers.indexOf(observer);
|
|
104
|
+
if (index !== -1)
|
|
105
|
+
this.#observers.splice(index, 1);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/** Offer only host-sanitized display facts. A concurrent offer queues behind the head. */
|
|
109
|
+
async offer(input, signal, work = null) {
|
|
110
|
+
if (!(signal instanceof AbortSignal) || signal.aborted)
|
|
111
|
+
return null;
|
|
112
|
+
const offer = input;
|
|
113
|
+
// ponytail: the FIFO holds at most MAX_CONCURRENT_WORK entries and one per work. An executor turn blocks
|
|
114
|
+
// on its pending approval, so a second request from the same work is a protocol anomaly, and there
|
|
115
|
+
// are never more asking works than run slots. An over-cap offer is declined at once (the shape the
|
|
116
|
+
// transport already handles) and publishes nothing; a per-work sub-queue is the upgrade path if a
|
|
117
|
+
// transport ever legitimately pipelines approvals.
|
|
118
|
+
const pending = this.#current === null ? this.#queue : [this.#current, ...this.#queue];
|
|
119
|
+
if (pending.length >= this.#capacity
|
|
120
|
+
|| (work !== null && pending.some(entry => entry.work?.work_id === work.work_id)))
|
|
121
|
+
return Object.freeze({ decision: 'decline' });
|
|
122
|
+
const id = validateApprovalId(this.#idFactory());
|
|
123
|
+
let resolve;
|
|
124
|
+
const decision = new Promise(done => { resolve = done; });
|
|
125
|
+
const entry = {
|
|
126
|
+
id,
|
|
127
|
+
offer,
|
|
128
|
+
work,
|
|
129
|
+
expiresAt: 0,
|
|
130
|
+
signal,
|
|
131
|
+
resolve,
|
|
132
|
+
held: false,
|
|
133
|
+
awaitingPresentation: this.#holds.has('background'),
|
|
134
|
+
onSignalAbort: null,
|
|
135
|
+
state: 'pending',
|
|
136
|
+
resolution: null,
|
|
137
|
+
};
|
|
138
|
+
entry.onSignalAbort = () => { this.#drop(entry); };
|
|
139
|
+
signal.addEventListener('abort', entry.onSignalAbort, { once: true });
|
|
140
|
+
if (this.#current === null)
|
|
141
|
+
this.#promote(entry);
|
|
142
|
+
else
|
|
143
|
+
this.#queue.push(entry);
|
|
144
|
+
this.#publish();
|
|
145
|
+
return await decision;
|
|
146
|
+
}
|
|
147
|
+
/** Accept exactly one structured decision for the current Nova-generated public ID. */
|
|
148
|
+
acceptDecision(input) {
|
|
149
|
+
const current = this.#current;
|
|
150
|
+
if (current?.state !== 'pending'
|
|
151
|
+
|| typeof input.approvalId !== 'string'
|
|
152
|
+
|| input.approvalId !== current.id
|
|
153
|
+
|| !(current.offer.allowed_decisions ?? ['accept', 'decline']).includes(input.decision))
|
|
154
|
+
return false;
|
|
155
|
+
if ((!current.held && this.#clock.now() >= current.expiresAt) || current.signal.aborted) {
|
|
156
|
+
this.#drop(current);
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
const resolution = Object.freeze({ decision: input.decision });
|
|
160
|
+
current.state = 'responding';
|
|
161
|
+
current.resolution = resolution;
|
|
162
|
+
this.#decision.consume(current);
|
|
163
|
+
current.resolve(resolution);
|
|
164
|
+
this.#publish();
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
/** Spend a returned resolution once; stale or invalidated resolutions become decline. */
|
|
168
|
+
consume(resolution) {
|
|
169
|
+
const current = this.#current;
|
|
170
|
+
if (current?.state !== 'responding'
|
|
171
|
+
|| current.resolution !== resolution
|
|
172
|
+
|| current.signal.aborted)
|
|
173
|
+
return 'decline';
|
|
174
|
+
const decision = resolution.decision;
|
|
175
|
+
this.#detach(current);
|
|
176
|
+
this.#current = null;
|
|
177
|
+
this.#holds.delete('project');
|
|
178
|
+
this.#promoteNext();
|
|
179
|
+
this.#publish();
|
|
180
|
+
return decision;
|
|
181
|
+
}
|
|
182
|
+
/** Drop the head (expiry, epoch change, carrier loss); the next queued entry becomes visible. */
|
|
183
|
+
invalidate(reason) {
|
|
184
|
+
const current = this.#current;
|
|
185
|
+
if (current === null)
|
|
186
|
+
return false;
|
|
187
|
+
this.#onDiagnostic?.(`executor_approval_invalidated:${reason}`);
|
|
188
|
+
this.#drop(current);
|
|
189
|
+
return true;
|
|
190
|
+
}
|
|
191
|
+
/** Drop every entry of one work (its turn ended or its transport closed) without touching other works. */
|
|
192
|
+
invalidateWork(workId, reason) {
|
|
193
|
+
const owned = [this.#current, ...this.#queue].filter((entry) => entry?.work?.work_id === workId);
|
|
194
|
+
if (owned.length)
|
|
195
|
+
this.#onDiagnostic?.(`executor_approval_invalidated:${reason}`);
|
|
196
|
+
for (const entry of owned)
|
|
197
|
+
this.#drop(entry);
|
|
198
|
+
return owned.length > 0;
|
|
199
|
+
}
|
|
200
|
+
#promote(entry) {
|
|
201
|
+
this.#current = entry;
|
|
202
|
+
this.#arm(entry);
|
|
203
|
+
}
|
|
204
|
+
#arm(entry) {
|
|
205
|
+
entry.expiresAt = this.#clock.now() + APPROVAL_TTL_SECONDS;
|
|
206
|
+
this.#decision.offer('approval', entry, entry.expiresAt, (_kind, expired) => {
|
|
207
|
+
this.#diagnose('executor_approval_expired');
|
|
208
|
+
this.#drop(expired);
|
|
209
|
+
});
|
|
210
|
+
if (this.#holds.size || entry.awaitingPresentation) {
|
|
211
|
+
entry.held = true;
|
|
212
|
+
this.#decision.hold(entry);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
#promoteNext() {
|
|
216
|
+
const next = this.#queue.shift();
|
|
217
|
+
if (next !== undefined)
|
|
218
|
+
this.#promote(next);
|
|
219
|
+
}
|
|
220
|
+
/** Remove one entry wherever it sits, declining it if still undecided; a dropped head promotes the next. */
|
|
221
|
+
#drop(entry) {
|
|
222
|
+
if (this.#current === entry) {
|
|
223
|
+
this.#current = null;
|
|
224
|
+
this.#holds.delete('project');
|
|
225
|
+
this.#promoteNext();
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
const index = this.#queue.indexOf(entry);
|
|
229
|
+
if (index === -1)
|
|
230
|
+
return;
|
|
231
|
+
this.#queue.splice(index, 1);
|
|
232
|
+
}
|
|
233
|
+
this.#detach(entry);
|
|
234
|
+
if (entry.state === 'pending')
|
|
235
|
+
entry.resolve(Object.freeze({ decision: 'decline' }));
|
|
236
|
+
this.#publish();
|
|
237
|
+
}
|
|
238
|
+
#detach(entry) {
|
|
239
|
+
this.#decision.consume(entry);
|
|
240
|
+
if (entry.onSignalAbort !== null) {
|
|
241
|
+
entry.signal.removeEventListener('abort', entry.onSignalAbort);
|
|
242
|
+
entry.onSignalAbort = null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
#publish() {
|
|
246
|
+
const view = this.view;
|
|
247
|
+
for (const observer of [...this.#observers]) {
|
|
248
|
+
try {
|
|
249
|
+
observer(view);
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
this.#diagnose('executor_approval_observer_failed');
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
#diagnose(code) {
|
|
257
|
+
try {
|
|
258
|
+
this.#onDiagnostic?.(code);
|
|
259
|
+
}
|
|
260
|
+
catch { /* diagnostics cannot change approval authority */ }
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function emptyView() {
|
|
264
|
+
return {
|
|
265
|
+
pending_approval: false,
|
|
266
|
+
pending_approval_busy: false,
|
|
267
|
+
kind: null,
|
|
268
|
+
local_detail: null,
|
|
269
|
+
operation_summary: null,
|
|
270
|
+
expires_at: null,
|
|
271
|
+
work: null,
|
|
272
|
+
queued: 0,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function validateApprovalId(value) {
|
|
276
|
+
if (typeof value !== 'string'
|
|
277
|
+
|| !isWellFormed(value)
|
|
278
|
+
|| value === ''
|
|
279
|
+
|| codePointLengthLikePython(value) > APPROVAL_ID_LIMIT)
|
|
280
|
+
throw new TypeError('invalid approval id');
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
283
|
+
/** Host-owned voice approval authority. Realtime supplies transport and forwards lifecycle events. */
|
|
284
|
+
const APPROVAL_CLARIFICATION = '这次授权还没有确认,操作仍在等待。请问你同意还是拒绝?';
|
|
285
|
+
/**
|
|
286
|
+
* The host fact for a pending executor approval (spec 08): names the work when the executor knows it,
|
|
287
|
+
* carries the id the model must copy into `confirm`, and never a wire tool name.
|
|
288
|
+
*/
|
|
289
|
+
function approvalFactText(view, id, executorDisplayName) {
|
|
290
|
+
const who = view.work === null ? executorDisplayName : `项目 ${view.work.project} · 会话 ${view.work.title}`;
|
|
291
|
+
const queued = view.queued > 0 ? `(还有 ${view.queued} 个等待)` : '';
|
|
292
|
+
const summary = (view.operation_summary ?? '').replace(/。$/u, '');
|
|
293
|
+
const text = `权限请求 id=${id}:${who} 请求批准 ${view.kind ?? ''}:${summary}${queued}。`
|
|
294
|
+
+ (view.allowed_decisions === undefined ? '' : `allowed_decisions=${JSON.stringify(view.allowed_decisions)}。`)
|
|
295
|
+
+ '只有用户本轮明确同意或拒绝后才调用 confirm(id, accepted);不要朗读 id。';
|
|
296
|
+
return [...text].slice(0, MAX_HOST_FACT_CHARS).join('');
|
|
297
|
+
}
|
|
298
|
+
export class ApprovalHost {
|
|
299
|
+
#port;
|
|
300
|
+
#executorApprovalPromptReleaseTasks = new Set();
|
|
301
|
+
#executorApprovalIsolation = new ConfirmationTurnIsolation(MAX_TRACKED_TOOL_CALLS);
|
|
302
|
+
#executorApprovalAuthority = null;
|
|
303
|
+
/** Current controller identity observed independently of the optional provider voice path. */
|
|
304
|
+
#executorApprovalObservedIdentity = null;
|
|
305
|
+
/** One closed expiry tombstone used only to classify a late decision; never restored or emitted. */
|
|
306
|
+
#executorApprovalExpiredIdentity = null;
|
|
307
|
+
#executorApprovalLifecycleToken = 0;
|
|
308
|
+
#executorApprovalDecisionRetry = null;
|
|
309
|
+
#executorApprovalPendingResponseQuarantine = null;
|
|
310
|
+
#executorApprovalQuarantinedResponses = new Map();
|
|
311
|
+
/** Internal response identity -> closed attempt number; IDs never enter approval telemetry. */
|
|
312
|
+
#executorApprovalCarrierAttempts = new Map();
|
|
313
|
+
#executorApprovalNeedsFreshResponse = false;
|
|
314
|
+
constructor(port) { this.#port = port; }
|
|
315
|
+
get pendingTasks() { return [...this.#executorApprovalPromptReleaseTasks]; }
|
|
316
|
+
async routeExecutorApprovalCall(event, observedResponseId) {
|
|
317
|
+
if (event.response_id !== null
|
|
318
|
+
&& this.isExecutorApprovalResponseQuarantined(event.session_epoch, event.response_id)) {
|
|
319
|
+
await this.handleExecutorApprovalDecision(event, {
|
|
320
|
+
observedProviderResponseId: observedResponseId,
|
|
321
|
+
originItemId: null,
|
|
322
|
+
originRef: null,
|
|
323
|
+
});
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const authority = this.#executorApprovalIsolation.authority;
|
|
327
|
+
const responseId = event.response_id;
|
|
328
|
+
const providerRevision = responseId === null
|
|
329
|
+
? undefined
|
|
330
|
+
: this.#port.session.providerTurnUserInputRevision(responseId);
|
|
331
|
+
const reservation = this.#executorApprovalIsolation.reservation;
|
|
332
|
+
const revision = responseId !== null
|
|
333
|
+
&& reservation !== null
|
|
334
|
+
&& this.#executorApprovalIsolation.isAuthorizationCarrier({
|
|
335
|
+
sessionEpoch: event.session_epoch,
|
|
336
|
+
userRevision: reservation.userRevision,
|
|
337
|
+
responseId,
|
|
338
|
+
})
|
|
339
|
+
? reservation.userRevision
|
|
340
|
+
: providerRevision;
|
|
341
|
+
if (authority !== null
|
|
342
|
+
&& responseId !== null
|
|
343
|
+
&& observedResponseId === responseId
|
|
344
|
+
&& authority.sessionEpoch === event.session_epoch
|
|
345
|
+
&& revision !== undefined
|
|
346
|
+
&& revision > authority.createdUserRevision
|
|
347
|
+
&& revision === this.#port.session.userInputRevision) {
|
|
348
|
+
const reservation = this.#executorApprovalIsolation.reservation;
|
|
349
|
+
if (this.#executorApprovalIsolation.isAuthorizationCarrier({
|
|
350
|
+
sessionEpoch: event.session_epoch,
|
|
351
|
+
userRevision: revision,
|
|
352
|
+
responseId,
|
|
353
|
+
})) {
|
|
354
|
+
await this.handleExecutorApprovalDecision(event, {
|
|
355
|
+
observedProviderResponseId: responseId,
|
|
356
|
+
originItemId: reservation?.itemId ?? null,
|
|
357
|
+
originRef: null,
|
|
358
|
+
});
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
const deferred = this.#executorApprovalIsolation.deferCall({
|
|
362
|
+
sessionEpoch: event.session_epoch,
|
|
363
|
+
userRevision: revision,
|
|
364
|
+
responseId,
|
|
365
|
+
call: event,
|
|
366
|
+
});
|
|
367
|
+
if (deferred === 'deferred')
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const decision = confirmArguments(event.arguments);
|
|
371
|
+
if (authority !== null
|
|
372
|
+
&& responseId !== null
|
|
373
|
+
&& observedResponseId === responseId
|
|
374
|
+
&& authority.sessionEpoch === event.session_epoch
|
|
375
|
+
&& providerRevision === authority.createdUserRevision
|
|
376
|
+
&& this.#port.controller?.pending === true
|
|
377
|
+
&& this.#port.clock.now() < authority.expiresAt
|
|
378
|
+
&& decision?.id === authority.authorityId) {
|
|
379
|
+
const deferred = this.#executorApprovalIsolation.deferProvisionalCall({
|
|
380
|
+
sessionEpoch: event.session_epoch,
|
|
381
|
+
responseId,
|
|
382
|
+
call: event,
|
|
383
|
+
});
|
|
384
|
+
if (deferred === 'deferred')
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
await this.handleExecutorApprovalDecision(event, {
|
|
388
|
+
observedProviderResponseId: observedResponseId,
|
|
389
|
+
originItemId: null,
|
|
390
|
+
originRef: null,
|
|
391
|
+
});
|
|
392
|
+
}
|
|
393
|
+
syncExecutorApproval(view) {
|
|
394
|
+
if (view.pending_approval
|
|
395
|
+
&& !view.pending_approval_busy
|
|
396
|
+
&& view.pending_approval_id !== undefined
|
|
397
|
+
&& view.kind !== null
|
|
398
|
+
&& view.operation_summary !== null) {
|
|
399
|
+
if (this.#port.session.sessionEpoch < 1 || view.expires_at === null)
|
|
400
|
+
return;
|
|
401
|
+
this.#executorApprovalObservedIdentity = {
|
|
402
|
+
approvalId: view.pending_approval_id,
|
|
403
|
+
sessionEpoch: this.#port.session.sessionEpoch,
|
|
404
|
+
expiresAt: view.expires_at,
|
|
405
|
+
};
|
|
406
|
+
if (view.held && !this.#port.projectBlocking()) {
|
|
407
|
+
if (this.#executorApprovalAuthority !== null)
|
|
408
|
+
this.#clearExecutorApprovalVoiceState();
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (this.#port.projectBlocking()) {
|
|
412
|
+
// Spec 08: the approval keeps its queue place, its TTL is paused (`hold`) and it is not voice-armed
|
|
413
|
+
// while a project confirmation holds the floor; `#publishProjectView` releases it once that settles.
|
|
414
|
+
if (this.#executorApprovalAuthority !== null)
|
|
415
|
+
this.#clearExecutorApprovalVoiceState();
|
|
416
|
+
this.#port.controller?.hold();
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
if (this.#executorApprovalAuthority?.approvalId === view.pending_approval_id
|
|
420
|
+
&& this.#executorApprovalAuthority.sessionEpoch === this.#port.session.sessionEpoch)
|
|
421
|
+
return;
|
|
422
|
+
this.#executorApprovalExpiredIdentity = null;
|
|
423
|
+
if (this.#executorApprovalAuthority !== null)
|
|
424
|
+
this.#clearExecutorApprovalVoiceState();
|
|
425
|
+
this.#executorApprovalLifecycleToken += 1;
|
|
426
|
+
const contextItem = hostFactIntent({
|
|
427
|
+
kind: 'final',
|
|
428
|
+
host_item_id: this.#port.idFactory(),
|
|
429
|
+
event_id: `approval:${view.pending_approval_id}:requested`,
|
|
430
|
+
content: approvalFactText(view, view.pending_approval_id, view.executorIdentity?.display_name ?? this.#port.displayName()),
|
|
431
|
+
}).item;
|
|
432
|
+
contextItem.speech_content = `${view.work === null ? this.#port.displayName() : view.work.project}需要你的授权:${view.operation_summary ?? '执行当前操作'}。是否允许?`;
|
|
433
|
+
const authority = {
|
|
434
|
+
approvalId: view.pending_approval_id,
|
|
435
|
+
sessionEpoch: this.#port.session.sessionEpoch,
|
|
436
|
+
expiresAt: view.expires_at,
|
|
437
|
+
lifecycleToken: this.#executorApprovalLifecycleToken,
|
|
438
|
+
contextItem,
|
|
439
|
+
contextReady: false,
|
|
440
|
+
preContextOnset: false,
|
|
441
|
+
attempt: 0,
|
|
442
|
+
clarificationQueued: false,
|
|
443
|
+
};
|
|
444
|
+
this.#executorApprovalAuthority = authority;
|
|
445
|
+
this.#executorApprovalDecisionRetry = null;
|
|
446
|
+
if (this.#executorApprovalPendingResponseQuarantine?.sessionEpoch
|
|
447
|
+
!== this.#port.session.sessionEpoch)
|
|
448
|
+
this.#executorApprovalPendingResponseQuarantine = null;
|
|
449
|
+
this.#executorApprovalNeedsFreshResponse = false;
|
|
450
|
+
this.#executorApprovalIsolation.invalidate();
|
|
451
|
+
this.#trackExecutorApprovalTask(this.#prepareExecutorApprovalContext(authority));
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const observed = this.#executorApprovalObservedIdentity;
|
|
455
|
+
if (observed !== null) {
|
|
456
|
+
this.#executorApprovalExpiredIdentity = this.#port.clock.now() >= observed.expiresAt
|
|
457
|
+
? {
|
|
458
|
+
approvalId: observed.approvalId,
|
|
459
|
+
sessionEpoch: observed.sessionEpoch,
|
|
460
|
+
}
|
|
461
|
+
: null;
|
|
462
|
+
this.#executorApprovalObservedIdentity = null;
|
|
463
|
+
}
|
|
464
|
+
this.#clearExecutorApprovalVoiceState();
|
|
465
|
+
this.#port.deliveryReady();
|
|
466
|
+
}
|
|
467
|
+
async #prepareExecutorApprovalContext(authority) {
|
|
468
|
+
let injected = false;
|
|
469
|
+
try {
|
|
470
|
+
injected = await this.#port.session.injectHostContext(authority.contextItem);
|
|
471
|
+
}
|
|
472
|
+
catch (failure) {
|
|
473
|
+
this.#port.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
474
|
+
? failure
|
|
475
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
476
|
+
}
|
|
477
|
+
if (!this.#executorApprovalAuthorityIsCurrent(authority)) {
|
|
478
|
+
this.#port.telemetry?.record('approval.context', {
|
|
479
|
+
session_epoch: authority.sessionEpoch,
|
|
480
|
+
outcome: 'stale',
|
|
481
|
+
});
|
|
482
|
+
if (injected)
|
|
483
|
+
await this.#port.retireProviderHostEventNow(authority.contextItem.event_id);
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
if (!injected) {
|
|
487
|
+
this.#port.telemetry?.record('approval.context', {
|
|
488
|
+
session_epoch: authority.sessionEpoch,
|
|
489
|
+
outcome: 'failed',
|
|
490
|
+
});
|
|
491
|
+
// A context that was not confirmed cannot support voice authority. Keep the renderer's
|
|
492
|
+
// controller pending, but release the foreground so an existing task receipt is not held until
|
|
493
|
+
// the approval TTL. Uncertain injection is intentionally not retried: the provider may already
|
|
494
|
+
// have accepted the fact even though this session could not confirm it.
|
|
495
|
+
this.#clearExecutorApprovalVoiceState();
|
|
496
|
+
this.#port.deliveryReady();
|
|
497
|
+
return;
|
|
498
|
+
}
|
|
499
|
+
authority.contextReady = true;
|
|
500
|
+
this.#port.telemetry?.record('approval.context', {
|
|
501
|
+
session_epoch: authority.sessionEpoch,
|
|
502
|
+
outcome: 'ready',
|
|
503
|
+
});
|
|
504
|
+
if (authority.preContextOnset) {
|
|
505
|
+
this.#beginExecutorApprovalAttempt(authority, 2, this.#port.session.userInputRevision, 'rotated');
|
|
506
|
+
this.#queueExecutorApprovalClarification(authority);
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
this.#beginExecutorApprovalAttempt(authority, 1, this.#port.session.userInputRevision, 'begun');
|
|
510
|
+
// The fact is already in provider context. This queued delivery therefore only creates its
|
|
511
|
+
// host-owned audible response and cannot expose a response before context confirmation.
|
|
512
|
+
this.#port.queueHostItem({
|
|
513
|
+
kind: 'host_fact',
|
|
514
|
+
item: authority.contextItem,
|
|
515
|
+
task_summary: null,
|
|
516
|
+
origin_spoken: false,
|
|
517
|
+
}, {
|
|
518
|
+
priority: USER_PRIORITY - 1,
|
|
519
|
+
preemptive: true,
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
#executorApprovalAuthorityIsCurrent(authority) {
|
|
523
|
+
return this.#executorApprovalAuthority === authority
|
|
524
|
+
&& authority.lifecycleToken === this.#executorApprovalLifecycleToken
|
|
525
|
+
&& authority.sessionEpoch === this.#port.session.sessionEpoch
|
|
526
|
+
&& this.#port.controller?.pending === true
|
|
527
|
+
&& this.#port.clock.now() < authority.expiresAt;
|
|
528
|
+
}
|
|
529
|
+
#trackExecutorApprovalTask(work) {
|
|
530
|
+
const task = work.finally(() => {
|
|
531
|
+
this.#executorApprovalPromptReleaseTasks.delete(task);
|
|
532
|
+
});
|
|
533
|
+
this.#executorApprovalPromptReleaseTasks.add(task);
|
|
534
|
+
}
|
|
535
|
+
#beginExecutorApprovalAttempt(authority, attempt, createdUserRevision, action) {
|
|
536
|
+
if (!this.#executorApprovalAuthorityIsCurrent(authority))
|
|
537
|
+
return;
|
|
538
|
+
authority.attempt = attempt;
|
|
539
|
+
this.#executorApprovalDecisionRetry = null;
|
|
540
|
+
this.#executorApprovalIsolation.beginAuthority({
|
|
541
|
+
authorityId: authority.approvalId,
|
|
542
|
+
sessionEpoch: authority.sessionEpoch,
|
|
543
|
+
createdUserRevision,
|
|
544
|
+
expiresAt: authority.expiresAt,
|
|
545
|
+
});
|
|
546
|
+
this.#port.telemetry?.record('approval.attempt', {
|
|
547
|
+
session_epoch: authority.sessionEpoch,
|
|
548
|
+
attempt,
|
|
549
|
+
action,
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
/** The only extra audible prompt; it uses host createResponse, never a free provider retry. */
|
|
553
|
+
#queueExecutorApprovalClarification(authority) {
|
|
554
|
+
if (!this.#executorApprovalAuthorityIsCurrent(authority) || authority.clarificationQueued)
|
|
555
|
+
return;
|
|
556
|
+
authority.clarificationQueued = true;
|
|
557
|
+
this.#port.queueHostItem(hostFactIntent({
|
|
558
|
+
kind: 'final',
|
|
559
|
+
host_item_id: this.#port.idFactory(),
|
|
560
|
+
event_id: `approval:${authority.approvalId}:clarification`,
|
|
561
|
+
content: APPROVAL_CLARIFICATION,
|
|
562
|
+
}), { priority: USER_PRIORITY - 1, preemptive: true });
|
|
563
|
+
}
|
|
564
|
+
noteExecutorApprovalOnsetBeforeContext() {
|
|
565
|
+
const authority = this.#executorApprovalAuthority;
|
|
566
|
+
if (authority !== null && !authority.contextReady)
|
|
567
|
+
authority.preContextOnset = true;
|
|
568
|
+
}
|
|
569
|
+
/** Retain one provider inference whose response id has not arrived yet across lifecycle changes. */
|
|
570
|
+
#capturePendingExecutorApprovalResponse(requestFreshResponse) {
|
|
571
|
+
const retry = this.#executorApprovalDecisionRetry;
|
|
572
|
+
const reservation = this.#executorApprovalIsolation.reservation;
|
|
573
|
+
const pendingRetry = retry?.requested === true && retry.retry_response_id === null;
|
|
574
|
+
const pendingInitial = !pendingRetry
|
|
575
|
+
&& reservation !== null
|
|
576
|
+
&& !this.#executorApprovalIsolation.blockedResponses.some(response => (response.authorizationCarrier && response.userRevision === reservation.userRevision));
|
|
577
|
+
if (!pendingRetry && !pendingInitial)
|
|
578
|
+
return;
|
|
579
|
+
const current = this.#executorApprovalPendingResponseQuarantine;
|
|
580
|
+
if (current !== null) {
|
|
581
|
+
if (current.sessionEpoch === this.#port.session.sessionEpoch) {
|
|
582
|
+
current.requestFreshResponse ||= requestFreshResponse;
|
|
583
|
+
}
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
this.#executorApprovalPendingResponseQuarantine = {
|
|
587
|
+
sessionEpoch: this.#port.session.sessionEpoch,
|
|
588
|
+
sourceRetry: pendingRetry ? retry : null,
|
|
589
|
+
requestFreshResponse,
|
|
590
|
+
responseId: null,
|
|
591
|
+
terminal: false,
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
/** The first response identity observed for the retained one-inference provider slot owns it. */
|
|
595
|
+
#claimPendingExecutorApprovalResponseQuarantine(sessionEpoch, responseId) {
|
|
596
|
+
const pending = this.#executorApprovalPendingResponseQuarantine;
|
|
597
|
+
if (responseId === null
|
|
598
|
+
|| pending?.sessionEpoch !== sessionEpoch
|
|
599
|
+
|| pending.responseId !== null)
|
|
600
|
+
return;
|
|
601
|
+
pending.responseId = responseId;
|
|
602
|
+
this.#rememberExecutorApprovalQuarantinedResponse(sessionEpoch, responseId);
|
|
603
|
+
this.#port.session.suppressResponse(responseId);
|
|
604
|
+
}
|
|
605
|
+
isExecutorApprovalResponseQuarantined(sessionEpoch, responseId) {
|
|
606
|
+
return this.#executorApprovalQuarantinedResponses.has(callKey(sessionEpoch, responseId));
|
|
607
|
+
}
|
|
608
|
+
#rememberExecutorApprovalQuarantinedResponse(sessionEpoch, responseId) {
|
|
609
|
+
const key = callKey(sessionEpoch, responseId);
|
|
610
|
+
this.#executorApprovalQuarantinedResponses.delete(key);
|
|
611
|
+
this.#executorApprovalQuarantinedResponses.set(key, null);
|
|
612
|
+
while (this.#executorApprovalQuarantinedResponses.size > MAX_TRACKED_TOOL_CALLS) {
|
|
613
|
+
const oldest = this.#executorApprovalQuarantinedResponses.keys().next();
|
|
614
|
+
if (oldest.done)
|
|
615
|
+
break;
|
|
616
|
+
this.#executorApprovalQuarantinedResponses.delete(oldest.value);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
async finishPendingExecutorApprovalResponseQuarantine(sessionEpoch, responseId) {
|
|
620
|
+
const pending = this.#executorApprovalPendingResponseQuarantine;
|
|
621
|
+
if (pending?.sessionEpoch !== sessionEpoch
|
|
622
|
+
|| pending.responseId !== responseId)
|
|
623
|
+
return;
|
|
624
|
+
pending.terminal = true;
|
|
625
|
+
if (pending.requestFreshResponse)
|
|
626
|
+
this.#executorApprovalNeedsFreshResponse = true;
|
|
627
|
+
this.#executorApprovalPendingResponseQuarantine = null;
|
|
628
|
+
await this.maybeRequestFreshExecutorApprovalResponse();
|
|
629
|
+
this.#port.deliveryReady();
|
|
630
|
+
}
|
|
631
|
+
/** A failed stale request proves that its not-yet-identified response can no longer arrive. */
|
|
632
|
+
#releaseFailedExecutorApprovalResponseRequest(retry) {
|
|
633
|
+
const pending = this.#executorApprovalPendingResponseQuarantine;
|
|
634
|
+
if (pending?.sourceRetry === retry && pending.responseId === null) {
|
|
635
|
+
this.#executorApprovalPendingResponseQuarantine = null;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
#clearExecutorApprovalVoiceState() {
|
|
639
|
+
const authority = this.#executorApprovalAuthority;
|
|
640
|
+
this.#executorApprovalLifecycleToken += 1;
|
|
641
|
+
this.#capturePendingExecutorApprovalResponse(false);
|
|
642
|
+
const abandonedCalls = this.#executorApprovalIsolation.takeAbandonedCalls();
|
|
643
|
+
if (authority !== null) {
|
|
644
|
+
this.#port.removeQueuedPrompt(authority.approvalId);
|
|
645
|
+
this.#port.releaseQuestion(authority.approvalId);
|
|
646
|
+
this.#quarantineExecutorApprovalCarriers();
|
|
647
|
+
this.#retireExecutorApprovalProviderContext(authority.approvalId);
|
|
648
|
+
}
|
|
649
|
+
this.#executorApprovalAuthority = null;
|
|
650
|
+
this.#executorApprovalDecisionRetry = null;
|
|
651
|
+
this.#executorApprovalNeedsFreshResponse = false;
|
|
652
|
+
this.#executorApprovalIsolation.invalidate();
|
|
653
|
+
this.#scheduleExecutorApprovalRefusals(abandonedCalls);
|
|
654
|
+
}
|
|
655
|
+
#retireExecutorApprovalProviderContext(approvalId) {
|
|
656
|
+
this.#port.retireProviderHostEvent(`approval:${approvalId}:requested`);
|
|
657
|
+
this.#port.retireProviderHostEvent(`approval:${approvalId}:clarification`);
|
|
658
|
+
}
|
|
659
|
+
#isExecutorApprovalHostResponse(responseId) {
|
|
660
|
+
return this.#port.session.responseEventIds(responseId).some(eventId => (eventId.startsWith('approval:')
|
|
661
|
+
&& (eventId.endsWith(':requested') || eventId.endsWith(':clarification'))));
|
|
662
|
+
}
|
|
663
|
+
/** Quarantine only exact initial/retry/provisional responses recorded by executor isolation. */
|
|
664
|
+
#quarantineExecutorApprovalCarriers() {
|
|
665
|
+
for (const response of this.#executorApprovalIsolation.blockedResponses) {
|
|
666
|
+
this.cancelExecutorApprovalPromptResponse(response.sessionEpoch, response.responseId);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
/** Fence one exact already-audible approval response without ever targeting a newer user turn. */
|
|
670
|
+
cancelExecutorApprovalPromptResponse(sessionEpoch, responseId) {
|
|
671
|
+
const cancellation = (async () => {
|
|
672
|
+
if (sessionEpoch !== this.#port.session.sessionEpoch)
|
|
673
|
+
return;
|
|
674
|
+
try {
|
|
675
|
+
await this.#port.session.quarantineResponse(responseId);
|
|
676
|
+
}
|
|
677
|
+
catch (failure) {
|
|
678
|
+
this.#port.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
679
|
+
? failure
|
|
680
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
681
|
+
}
|
|
682
|
+
finally {
|
|
683
|
+
this.#port.deliveryReady();
|
|
684
|
+
}
|
|
685
|
+
})();
|
|
686
|
+
const task = cancellation.finally(() => {
|
|
687
|
+
this.#executorApprovalPromptReleaseTasks.delete(task);
|
|
688
|
+
});
|
|
689
|
+
this.#executorApprovalPromptReleaseTasks.add(task);
|
|
690
|
+
}
|
|
691
|
+
/** Reserve one exact item in the current attempt; one newer revision gets a fresh isolation. */
|
|
692
|
+
async reserveExecutorApprovalItem(sessionEpoch, itemId) {
|
|
693
|
+
const state = this.#executorApprovalAuthority;
|
|
694
|
+
let authority = this.#executorApprovalIsolation.authority;
|
|
695
|
+
if (itemId === null
|
|
696
|
+
|| this.#port.controller?.pending !== true
|
|
697
|
+
|| state === null
|
|
698
|
+
|| !state.contextReady
|
|
699
|
+
|| authority?.sessionEpoch !== sessionEpoch)
|
|
700
|
+
return;
|
|
701
|
+
const userRevision = this.#port.session.userInputRevision;
|
|
702
|
+
const existing = this.#executorApprovalIsolation.reservation;
|
|
703
|
+
const supersedesTrackedCarrier = existing === null
|
|
704
|
+
&& this.#executorApprovalIsolation.blockedResponses.some(response => (response.userRevision !== null && response.userRevision < userRevision));
|
|
705
|
+
if (existing !== null && userRevision > existing.userRevision
|
|
706
|
+
|| supersedesTrackedCarrier) {
|
|
707
|
+
if (state.attempt !== 1) {
|
|
708
|
+
await this.#retireExecutorApprovalVoiceAuthority();
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
this.#rotateExecutorApprovalAttempt(state, userRevision - 1);
|
|
712
|
+
authority = this.#executorApprovalIsolation.authority;
|
|
713
|
+
if (authority?.sessionEpoch !== sessionEpoch)
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
const abandonedCalls = this.#executorApprovalIsolation.takeAbandonedCalls({
|
|
717
|
+
sessionEpoch,
|
|
718
|
+
userRevision,
|
|
719
|
+
});
|
|
720
|
+
if (abandonedCalls.length > 0) {
|
|
721
|
+
await this.#refuseExecutorApprovalCalls(abandonedCalls);
|
|
722
|
+
}
|
|
723
|
+
const result = this.#executorApprovalIsolation.reserveUserItem({
|
|
724
|
+
sessionEpoch,
|
|
725
|
+
itemId,
|
|
726
|
+
userRevision,
|
|
727
|
+
});
|
|
728
|
+
if (result === 'stale') {
|
|
729
|
+
await this.#retireExecutorApprovalVoiceAuthority();
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
if (result !== 'reserved' && result !== 'idempotent')
|
|
733
|
+
return;
|
|
734
|
+
const provisional = this.#executorApprovalIsolation.bindProvisionalResponse();
|
|
735
|
+
if (provisional.kind === 'ambiguous') {
|
|
736
|
+
await this.#exhaustExecutorApprovalAttempt();
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
if (provisional.kind === 'bound') {
|
|
740
|
+
await this.#bindExecutorApprovalResponse(sessionEpoch, provisional.responseId, userRevision);
|
|
741
|
+
if (provisional.terminal && this.#port.controller?.pending === true) {
|
|
742
|
+
this.#executorApprovalDecisionRetry ??= {
|
|
743
|
+
item_key: callKey(sessionEpoch, itemId),
|
|
744
|
+
source_response_id: provisional.responseId,
|
|
745
|
+
requested: false,
|
|
746
|
+
retry_response_id: null,
|
|
747
|
+
};
|
|
748
|
+
await this.#maybeRequestExecutorApprovalDecisionRetry();
|
|
749
|
+
}
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
const responseId = this.#port.session.activeProviderResponseId;
|
|
753
|
+
if (responseId !== null)
|
|
754
|
+
await this.#bindExecutorApprovalResponse(sessionEpoch, responseId);
|
|
755
|
+
}
|
|
756
|
+
/** Replace attempt one before any await so old calls/responses cannot enter attempt two. */
|
|
757
|
+
#rotateExecutorApprovalAttempt(state, createdUserRevision) {
|
|
758
|
+
if (!this.#executorApprovalAuthorityIsCurrent(state) || state.attempt !== 1)
|
|
759
|
+
return;
|
|
760
|
+
this.#capturePendingExecutorApprovalResponse(true);
|
|
761
|
+
this.#quarantineExecutorApprovalCarriers();
|
|
762
|
+
const abandoned = this.#executorApprovalIsolation.takeAbandonedCalls();
|
|
763
|
+
this.#beginExecutorApprovalAttempt(state, 2, createdUserRevision, 'rotated');
|
|
764
|
+
this.#scheduleExecutorApprovalRefusals(abandoned);
|
|
765
|
+
this.#port.deliveryReady();
|
|
766
|
+
}
|
|
767
|
+
/** Spend one attempt; only the first exhaustion may create the single host clarification. */
|
|
768
|
+
async #exhaustExecutorApprovalAttempt() {
|
|
769
|
+
const state = this.#executorApprovalAuthority;
|
|
770
|
+
if (state === null || !this.#executorApprovalAuthorityIsCurrent(state))
|
|
771
|
+
return;
|
|
772
|
+
const attempt = state.attempt;
|
|
773
|
+
if (attempt === 1 || attempt === 2) {
|
|
774
|
+
this.#port.telemetry?.record('approval.attempt', {
|
|
775
|
+
session_epoch: state.sessionEpoch,
|
|
776
|
+
attempt,
|
|
777
|
+
action: 'exhausted',
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
this.#quarantineExecutorApprovalCarriers();
|
|
781
|
+
const abandoned = this.#executorApprovalIsolation.takeAbandonedCalls();
|
|
782
|
+
this.#executorApprovalDecisionRetry = null;
|
|
783
|
+
this.#executorApprovalIsolation.invalidate();
|
|
784
|
+
await this.#refuseExecutorApprovalCalls(abandoned);
|
|
785
|
+
if (!this.#executorApprovalAuthorityIsCurrent(state))
|
|
786
|
+
return;
|
|
787
|
+
if (attempt === 1 && !state.clarificationQueued) {
|
|
788
|
+
this.#beginExecutorApprovalAttempt(state, 2, this.#port.session.userInputRevision, 'rotated');
|
|
789
|
+
this.#queueExecutorApprovalClarification(state);
|
|
790
|
+
this.#port.deliveryReady();
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
state.attempt = 0;
|
|
794
|
+
this.#port.deliveryReady();
|
|
795
|
+
}
|
|
796
|
+
/** Bind initial/retry carrier solely through exact authority/item/response/revision identity. */
|
|
797
|
+
async #bindExecutorApprovalResponse(sessionEpoch, responseId, correlatedRevision) {
|
|
798
|
+
const reservation = this.#executorApprovalIsolation.reservation;
|
|
799
|
+
const revision = correlatedRevision ?? this.#port.session.providerTurnUserInputRevision(responseId);
|
|
800
|
+
if (reservation?.sessionEpoch !== sessionEpoch
|
|
801
|
+
|| revision === undefined
|
|
802
|
+
|| revision !== reservation.userRevision
|
|
803
|
+
|| revision !== this.#port.session.userInputRevision
|
|
804
|
+
|| !this.#port.session.responseMatchesUserItem(responseId, reservation.itemId, revision))
|
|
805
|
+
return false;
|
|
806
|
+
await this.#refuseExecutorApprovalCalls(this.#executorApprovalIsolation.takeAbandonedCalls({
|
|
807
|
+
sessionEpoch,
|
|
808
|
+
userRevision: revision,
|
|
809
|
+
responseId,
|
|
810
|
+
}));
|
|
811
|
+
const retry = this.#executorApprovalDecisionRetry;
|
|
812
|
+
const result = retry?.requested === true
|
|
813
|
+
&& retry.retry_response_id === null
|
|
814
|
+
? this.#executorApprovalIsolation.bindRetryResponse({
|
|
815
|
+
sessionEpoch,
|
|
816
|
+
itemId: reservation.itemId,
|
|
817
|
+
userRevision: revision,
|
|
818
|
+
responseId,
|
|
819
|
+
})
|
|
820
|
+
: this.#executorApprovalIsolation.bindResponse({
|
|
821
|
+
sessionEpoch,
|
|
822
|
+
itemId: reservation.itemId,
|
|
823
|
+
userRevision: revision,
|
|
824
|
+
responseId,
|
|
825
|
+
});
|
|
826
|
+
if (result !== 'bound' && result !== 'idempotent')
|
|
827
|
+
return false;
|
|
828
|
+
if (retry?.requested === true && retry.retry_response_id === null) {
|
|
829
|
+
retry.retry_response_id = responseId;
|
|
830
|
+
}
|
|
831
|
+
this.#executorApprovalIsolation.markBlockedResponse({ sessionEpoch, responseId });
|
|
832
|
+
this.#port.session.suppressResponse(responseId);
|
|
833
|
+
if (result === 'bound') {
|
|
834
|
+
const attempt = this.#executorApprovalAuthority?.attempt;
|
|
835
|
+
if (attempt === 1 || attempt === 2) {
|
|
836
|
+
const key = callKey(sessionEpoch, responseId);
|
|
837
|
+
this.#executorApprovalCarrierAttempts.delete(key);
|
|
838
|
+
this.#executorApprovalCarrierAttempts.set(key, attempt);
|
|
839
|
+
while (this.#executorApprovalCarrierAttempts.size > MAX_TRACKED_TOOL_CALLS) {
|
|
840
|
+
const oldest = this.#executorApprovalCarrierAttempts.keys().next();
|
|
841
|
+
if (oldest.done)
|
|
842
|
+
break;
|
|
843
|
+
this.#executorApprovalCarrierAttempts.delete(oldest.value);
|
|
844
|
+
}
|
|
845
|
+
this.#port.telemetry?.record('approval.carrier', {
|
|
846
|
+
session_epoch: sessionEpoch,
|
|
847
|
+
attempt,
|
|
848
|
+
action: 'bound',
|
|
849
|
+
});
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
const calls = this.#executorApprovalIsolation.releaseCallsForResponse({
|
|
853
|
+
sessionEpoch,
|
|
854
|
+
userRevision: revision,
|
|
855
|
+
responseId,
|
|
856
|
+
});
|
|
857
|
+
for (const call of calls) {
|
|
858
|
+
await this.handleExecutorApprovalDecision(call, {
|
|
859
|
+
observedProviderResponseId: responseId,
|
|
860
|
+
originItemId: reservation.itemId,
|
|
861
|
+
originRef: null,
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
return true;
|
|
865
|
+
}
|
|
866
|
+
/** One exact silent carrier may request one same-reservation structured-decision retry. */
|
|
867
|
+
async #maybeRequestExecutorApprovalDecisionRetry() {
|
|
868
|
+
const retry = this.#executorApprovalDecisionRetry;
|
|
869
|
+
const reservation = this.#executorApprovalIsolation.reservation;
|
|
870
|
+
const authority = this.#executorApprovalIsolation.authority;
|
|
871
|
+
const state = this.#executorApprovalAuthority;
|
|
872
|
+
if (retry === null
|
|
873
|
+
|| retry.requested
|
|
874
|
+
|| reservation === null
|
|
875
|
+
|| authority === null
|
|
876
|
+
|| state === null
|
|
877
|
+
|| retry.item_key !== callKey(reservation.sessionEpoch, reservation.itemId)
|
|
878
|
+
|| this.#port.controller?.pending !== true
|
|
879
|
+
|| this.#port.clock.now() >= authority.expiresAt)
|
|
880
|
+
return;
|
|
881
|
+
const attempt = state.attempt;
|
|
882
|
+
retry.requested = true;
|
|
883
|
+
if (attempt === 1 || attempt === 2) {
|
|
884
|
+
this.#port.telemetry?.record('approval.carrier', {
|
|
885
|
+
session_epoch: state.sessionEpoch,
|
|
886
|
+
attempt,
|
|
887
|
+
action: 'retry_requested',
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
let requested = false;
|
|
891
|
+
try {
|
|
892
|
+
requested = await this.#port.session.requestUserResponse();
|
|
893
|
+
}
|
|
894
|
+
catch (failure) {
|
|
895
|
+
this.#port.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
896
|
+
? failure
|
|
897
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
898
|
+
}
|
|
899
|
+
if (!requested && (attempt === 1 || attempt === 2)) {
|
|
900
|
+
this.#port.telemetry?.record('approval.carrier', {
|
|
901
|
+
session_epoch: state.sessionEpoch,
|
|
902
|
+
attempt,
|
|
903
|
+
action: 'retry_failed',
|
|
904
|
+
});
|
|
905
|
+
}
|
|
906
|
+
const stillCurrent = this.#executorApprovalAuthorityIsCurrent(state)
|
|
907
|
+
&& state.attempt === attempt
|
|
908
|
+
&& this.#executorApprovalDecisionRetry === retry
|
|
909
|
+
&& this.#executorApprovalIsolation.authority === authority
|
|
910
|
+
&& this.#executorApprovalIsolation.reservation === reservation;
|
|
911
|
+
if (!stillCurrent) {
|
|
912
|
+
if (!requested)
|
|
913
|
+
this.#releaseFailedExecutorApprovalResponseRequest(retry);
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
if (requested)
|
|
917
|
+
return;
|
|
918
|
+
await this.#exhaustExecutorApprovalAttempt();
|
|
919
|
+
}
|
|
920
|
+
/** Replace one ambiguous old retry with an initial carrier request for the fresh attempt. */
|
|
921
|
+
async maybeRequestFreshExecutorApprovalResponse() {
|
|
922
|
+
const state = this.#executorApprovalAuthority;
|
|
923
|
+
if (!this.#executorApprovalNeedsFreshResponse
|
|
924
|
+
|| state === null
|
|
925
|
+
|| !this.#executorApprovalAuthorityIsCurrent(state)
|
|
926
|
+
|| state.attempt !== 2
|
|
927
|
+
|| this.#executorApprovalIsolation.reservation === null)
|
|
928
|
+
return;
|
|
929
|
+
let requested = false;
|
|
930
|
+
try {
|
|
931
|
+
requested = await this.#port.session.requestUserResponse();
|
|
932
|
+
}
|
|
933
|
+
catch (failure) {
|
|
934
|
+
this.#port.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
935
|
+
? failure
|
|
936
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
937
|
+
}
|
|
938
|
+
if (requested)
|
|
939
|
+
this.#executorApprovalNeedsFreshResponse = false;
|
|
940
|
+
}
|
|
941
|
+
blocksExecutorApprovalTool(event) {
|
|
942
|
+
if (event.response_id !== null
|
|
943
|
+
&& this.isExecutorApprovalResponseQuarantined(event.session_epoch, event.response_id))
|
|
944
|
+
return true;
|
|
945
|
+
return event.response_id !== null && this.#executorApprovalIsolation.responseState({
|
|
946
|
+
sessionEpoch: event.session_epoch,
|
|
947
|
+
responseId: event.response_id,
|
|
948
|
+
})?.blocked === true;
|
|
949
|
+
}
|
|
950
|
+
async closeExecutorApprovalCarrierTool(event) {
|
|
951
|
+
await this.#port.session.injectToolOutput({
|
|
952
|
+
kind: 'tool_output',
|
|
953
|
+
host_item_id: this.#port.idFactory(),
|
|
954
|
+
event_id: this.#port.idFactory(),
|
|
955
|
+
call_id: event.call_id,
|
|
956
|
+
content: JSON.stringify({ code: 'approval_carrier_tool_refused', state: 'refused' }),
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
/** Complete every abandoned current-session function without routing output to a retired epoch. */
|
|
960
|
+
async #refuseExecutorApprovalCalls(calls) {
|
|
961
|
+
for (const call of calls) {
|
|
962
|
+
if (call.session_epoch !== this.#port.session.sessionEpoch)
|
|
963
|
+
continue;
|
|
964
|
+
try {
|
|
965
|
+
await this.#port.session.injectToolOutput({
|
|
966
|
+
kind: 'tool_output',
|
|
967
|
+
host_item_id: this.#port.idFactory(),
|
|
968
|
+
event_id: this.#port.idFactory(),
|
|
969
|
+
call_id: call.call_id,
|
|
970
|
+
content: JSON.stringify({ code: 'approval_not_authorized', state: 'refused' }),
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
catch (failure) {
|
|
974
|
+
this.#port.reportDeliveryFailure(failure instanceof RealtimeDeliveryError
|
|
975
|
+
? failure
|
|
976
|
+
: new RealtimeDeliveryError(String(failure)));
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
/** A controller observer is synchronous, so own its protocol-completion work as a tracked task. */
|
|
981
|
+
#scheduleExecutorApprovalRefusals(calls) {
|
|
982
|
+
if (calls.length === 0)
|
|
983
|
+
return;
|
|
984
|
+
const refusal = this.#refuseExecutorApprovalCalls(calls);
|
|
985
|
+
const task = refusal.finally(() => {
|
|
986
|
+
this.#executorApprovalPromptReleaseTasks.delete(task);
|
|
987
|
+
});
|
|
988
|
+
this.#executorApprovalPromptReleaseTasks.add(task);
|
|
989
|
+
}
|
|
990
|
+
/** Permanently retire voice authority while leaving renderer/controller/provider-fact policy intact. */
|
|
991
|
+
async #retireExecutorApprovalVoiceAuthority() {
|
|
992
|
+
const state = this.#executorApprovalAuthority;
|
|
993
|
+
if (state?.attempt === 1 || state?.attempt === 2) {
|
|
994
|
+
this.#port.telemetry?.record('approval.attempt', {
|
|
995
|
+
session_epoch: state.sessionEpoch,
|
|
996
|
+
attempt: state.attempt,
|
|
997
|
+
action: 'exhausted',
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
this.#capturePendingExecutorApprovalResponse(false);
|
|
1001
|
+
this.#quarantineExecutorApprovalCarriers();
|
|
1002
|
+
const abandonedCalls = this.#executorApprovalIsolation.takeAbandonedCalls();
|
|
1003
|
+
await this.#refuseExecutorApprovalCalls(abandonedCalls);
|
|
1004
|
+
this.#executorApprovalDecisionRetry = null;
|
|
1005
|
+
this.#executorApprovalNeedsFreshResponse = false;
|
|
1006
|
+
this.#executorApprovalIsolation.invalidate();
|
|
1007
|
+
if (state !== null)
|
|
1008
|
+
state.attempt = 0;
|
|
1009
|
+
this.#port.deliveryReady();
|
|
1010
|
+
}
|
|
1011
|
+
async handleExecutorApprovalDecision(event, origin) {
|
|
1012
|
+
const controller = this.#port.controller;
|
|
1013
|
+
const authority = this.#executorApprovalIsolation.authority;
|
|
1014
|
+
const reservation = this.#executorApprovalIsolation.reservation;
|
|
1015
|
+
const decision = confirmArguments(event.arguments);
|
|
1016
|
+
const voiceState = this.#executorApprovalAuthority;
|
|
1017
|
+
let code = decision === null
|
|
1018
|
+
? 'approval_invalid'
|
|
1019
|
+
: controller?.pending === true ? 'approval_not_authorized' : 'approval_not_pending';
|
|
1020
|
+
let state = 'refused';
|
|
1021
|
+
let telemetryOutcome = 'refused';
|
|
1022
|
+
let telemetryReason;
|
|
1023
|
+
const expired = this.#executorApprovalExpiredIdentity;
|
|
1024
|
+
if (decision === null)
|
|
1025
|
+
telemetryReason = 'malformed_arguments';
|
|
1026
|
+
else if (controller?.pending !== true) {
|
|
1027
|
+
telemetryReason = expired?.sessionEpoch === event.session_epoch
|
|
1028
|
+
&& expired.approvalId === decision.id
|
|
1029
|
+
? 'expired'
|
|
1030
|
+
: 'not_pending';
|
|
1031
|
+
}
|
|
1032
|
+
else if (voiceState === null)
|
|
1033
|
+
telemetryReason = 'authority_missing';
|
|
1034
|
+
else if (decision.id !== voiceState.approvalId)
|
|
1035
|
+
telemetryReason = 'replaced';
|
|
1036
|
+
else if (voiceState.sessionEpoch !== event.session_epoch)
|
|
1037
|
+
telemetryReason = 'epoch_mismatch';
|
|
1038
|
+
else if (this.#port.clock.now() >= voiceState.expiresAt)
|
|
1039
|
+
telemetryReason = 'expired';
|
|
1040
|
+
else if (!voiceState.contextReady)
|
|
1041
|
+
telemetryReason = 'context_not_ready';
|
|
1042
|
+
else if (voiceState.attempt === 0)
|
|
1043
|
+
telemetryReason = 'voice_exhausted';
|
|
1044
|
+
else if (authority === null || reservation === null)
|
|
1045
|
+
telemetryReason = 'authority_missing';
|
|
1046
|
+
else if (origin.originItemId !== null
|
|
1047
|
+
&& origin.originItemId !== reservation.itemId)
|
|
1048
|
+
telemetryReason = 'item_mismatch';
|
|
1049
|
+
else if (event.response_id === null
|
|
1050
|
+
|| origin.observedProviderResponseId !== event.response_id)
|
|
1051
|
+
telemetryReason = 'response_mismatch';
|
|
1052
|
+
if (decision !== null
|
|
1053
|
+
&& controller?.pending === true
|
|
1054
|
+
&& authority !== null
|
|
1055
|
+
&& telemetryReason === undefined) {
|
|
1056
|
+
const responseId = event.response_id;
|
|
1057
|
+
const providerRevision = responseId === null
|
|
1058
|
+
? undefined
|
|
1059
|
+
: this.#port.session.providerTurnUserInputRevision(responseId);
|
|
1060
|
+
const revision = responseId !== null
|
|
1061
|
+
&& reservation !== null
|
|
1062
|
+
&& this.#executorApprovalIsolation.isAuthorizationCarrier({
|
|
1063
|
+
sessionEpoch: event.session_epoch,
|
|
1064
|
+
userRevision: reservation.userRevision,
|
|
1065
|
+
responseId,
|
|
1066
|
+
})
|
|
1067
|
+
? reservation.userRevision
|
|
1068
|
+
: providerRevision;
|
|
1069
|
+
const authorized = authority.authorityId === decision.id
|
|
1070
|
+
&& authority.sessionEpoch === event.session_epoch
|
|
1071
|
+
&& this.#port.clock.now() < authority.expiresAt
|
|
1072
|
+
&& reservation !== null
|
|
1073
|
+
&& reservation.sessionEpoch === event.session_epoch
|
|
1074
|
+
&& responseId !== null
|
|
1075
|
+
&& origin.observedProviderResponseId === responseId
|
|
1076
|
+
&& revision !== undefined
|
|
1077
|
+
&& revision === reservation.userRevision
|
|
1078
|
+
&& revision > authority.createdUserRevision
|
|
1079
|
+
&& revision === this.#port.session.userInputRevision
|
|
1080
|
+
&& this.#port.session.responseMatchesUserItem(responseId, reservation.itemId, revision)
|
|
1081
|
+
&& this.#executorApprovalIsolation.isAuthorizationCarrier({
|
|
1082
|
+
sessionEpoch: event.session_epoch,
|
|
1083
|
+
userRevision: revision,
|
|
1084
|
+
responseId,
|
|
1085
|
+
});
|
|
1086
|
+
if (!authorized) {
|
|
1087
|
+
code = 'approval_not_authorized';
|
|
1088
|
+
telemetryReason = revision === undefined || reservation === null
|
|
1089
|
+
? 'revision_mismatch'
|
|
1090
|
+
: revision !== reservation.userRevision
|
|
1091
|
+
|| revision <= authority.createdUserRevision
|
|
1092
|
+
|| revision !== this.#port.session.userInputRevision
|
|
1093
|
+
? 'revision_mismatch'
|
|
1094
|
+
: 'response_mismatch';
|
|
1095
|
+
}
|
|
1096
|
+
else if (decision.scope === 'session' && !(controller.view.allowed_decisions ?? []).includes('acceptForSession')) {
|
|
1097
|
+
code = 'approval_scope_unsupported';
|
|
1098
|
+
state = 'retryable';
|
|
1099
|
+
telemetryReason = 'unsupported_scope';
|
|
1100
|
+
}
|
|
1101
|
+
else if (controller.acceptDecision({
|
|
1102
|
+
approvalId: decision.id,
|
|
1103
|
+
decision: decision.accepted ? decision.scope === 'session' ? 'acceptForSession' : 'accept' : 'decline',
|
|
1104
|
+
})) {
|
|
1105
|
+
this.#port.session.settleUserResponse(responseId);
|
|
1106
|
+
code = decision.accepted ? 'approval_accepted' : 'approval_declined';
|
|
1107
|
+
state = decision.accepted ? 'accepted' : 'refused';
|
|
1108
|
+
telemetryOutcome = decision.accepted ? 'accepted' : 'refused';
|
|
1109
|
+
telemetryReason = undefined;
|
|
1110
|
+
}
|
|
1111
|
+
else {
|
|
1112
|
+
telemetryReason = 'not_pending';
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
this.#recordExecutorApprovalDecision(event.session_epoch, 'function', telemetryOutcome, telemetryReason, decision?.accepted === true && (telemetryOutcome === 'accepted' || telemetryReason === 'unsupported_scope')
|
|
1116
|
+
? decision.scope ?? 'once' : undefined);
|
|
1117
|
+
await this.#port.session.injectToolOutput({
|
|
1118
|
+
kind: 'tool_output',
|
|
1119
|
+
host_item_id: this.#port.idFactory(),
|
|
1120
|
+
event_id: this.#port.idFactory(),
|
|
1121
|
+
call_id: event.call_id,
|
|
1122
|
+
content: JSON.stringify({ code, state,
|
|
1123
|
+
...(code === 'approval_accepted' && decision?.scope === 'session' ? { scope: 'session' } : {}) }),
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
#recordExecutorApprovalDecision(sessionEpoch, source, outcome, reason, scope) {
|
|
1127
|
+
this.#port.telemetry?.record('approval.decision', { session_epoch: sessionEpoch, source, outcome,
|
|
1128
|
+
...(reason === undefined ? {} : { reason }), ...(scope === undefined ? {} : { requested_scope: scope }) });
|
|
1129
|
+
}
|
|
1130
|
+
invalidateExecutorApproval(reason) {
|
|
1131
|
+
this.#port.controller?.invalidate(reason);
|
|
1132
|
+
if (this.#executorApprovalAuthority !== null)
|
|
1133
|
+
this.#clearExecutorApprovalVoiceState();
|
|
1134
|
+
}
|
|
1135
|
+
executorApprovalDecision(approvalId, approved, scope) {
|
|
1136
|
+
if (typeof approved !== 'boolean' || scope !== undefined && (scope !== 'session' || !approved))
|
|
1137
|
+
return false;
|
|
1138
|
+
const controller = this.#port.controller;
|
|
1139
|
+
if (controller === undefined)
|
|
1140
|
+
return false;
|
|
1141
|
+
const accepted = controller.acceptDecision({
|
|
1142
|
+
approvalId,
|
|
1143
|
+
decision: approved ? scope === 'session' ? 'acceptForSession' : 'accept' : 'decline',
|
|
1144
|
+
});
|
|
1145
|
+
const expired = this.#executorApprovalExpiredIdentity;
|
|
1146
|
+
this.#recordExecutorApprovalDecision(this.#port.session.sessionEpoch, 'renderer', accepted && approved ? 'accepted' : 'refused', accepted
|
|
1147
|
+
? undefined
|
|
1148
|
+
: controller.pending
|
|
1149
|
+
? controller.view.pending_approval_id !== approvalId ? 'replaced' : 'not_pending'
|
|
1150
|
+
: expired?.sessionEpoch === this.#port.session.sessionEpoch
|
|
1151
|
+
&& expired.approvalId === approvalId
|
|
1152
|
+
? 'expired'
|
|
1153
|
+
: 'not_pending');
|
|
1154
|
+
return accepted;
|
|
1155
|
+
}
|
|
1156
|
+
sync() { if (this.#port.controller !== undefined)
|
|
1157
|
+
this.syncExecutorApproval(this.#port.controller.view); }
|
|
1158
|
+
get pending() { return this.#port.controller?.view.pending_approval === true; }
|
|
1159
|
+
ownsId(id) {
|
|
1160
|
+
return this.#port.controller?.view.pending_approval_id === id
|
|
1161
|
+
|| this.#executorApprovalAuthority?.approvalId === id
|
|
1162
|
+
|| this.#executorApprovalExpiredIdentity?.approvalId === id;
|
|
1163
|
+
}
|
|
1164
|
+
factEligible(eventId) {
|
|
1165
|
+
const authority = this.#executorApprovalAuthority;
|
|
1166
|
+
return authority !== null && eventId.startsWith(`approval:${authority.approvalId}:`)
|
|
1167
|
+
&& this.#executorApprovalAuthorityIsCurrent(authority);
|
|
1168
|
+
}
|
|
1169
|
+
blocksSemanticAcknowledgement(eventId) {
|
|
1170
|
+
return this.#executorApprovalAuthority !== null && eventId !== null;
|
|
1171
|
+
}
|
|
1172
|
+
hold() {
|
|
1173
|
+
if (this.#executorApprovalAuthority !== null)
|
|
1174
|
+
this.#clearExecutorApprovalVoiceState();
|
|
1175
|
+
this.#port.controller?.hold();
|
|
1176
|
+
}
|
|
1177
|
+
release() {
|
|
1178
|
+
const approval = this.#port.controller;
|
|
1179
|
+
if (approval?.view.pending_approval === true && !approval.release())
|
|
1180
|
+
this.syncExecutorApproval(approval.view);
|
|
1181
|
+
}
|
|
1182
|
+
setResponseFencePending(pending) { this.#executorApprovalIsolation.setResponseFencePending(pending); }
|
|
1183
|
+
releaseQuestionOnOnset() {
|
|
1184
|
+
const approvalId = this.#executorApprovalAuthority?.approvalId;
|
|
1185
|
+
if (approvalId !== undefined) {
|
|
1186
|
+
this.#port.removeQueuedPrompt(approvalId);
|
|
1187
|
+
this.#port.releaseQuestion(approvalId);
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
beforeEvent(event) {
|
|
1191
|
+
const executorEventResponseId = 'response_id' in event ? event.response_id : null;
|
|
1192
|
+
const pendingExecutorResponseQuarantineAtStart = event.kind === 'response_started'
|
|
1193
|
+
&& this.#executorApprovalPendingResponseQuarantine?.sessionEpoch === event.session_epoch
|
|
1194
|
+
&& this.#executorApprovalPendingResponseQuarantine.responseId === null;
|
|
1195
|
+
if (event.kind !== 'response_started'
|
|
1196
|
+
&& (executorEventResponseId === null
|
|
1197
|
+
|| this.#port.session.responseEventIds(executorEventResponseId).length === 0)) {
|
|
1198
|
+
this.#claimPendingExecutorApprovalResponseQuarantine(event.session_epoch, executorEventResponseId);
|
|
1199
|
+
}
|
|
1200
|
+
const executorQuarantinedResponse = executorEventResponseId !== null
|
|
1201
|
+
&& this.isExecutorApprovalResponseQuarantined(event.session_epoch, executorEventResponseId);
|
|
1202
|
+
const executorFencePendingAtStart = this.#executorApprovalIsolation.responseFencePending;
|
|
1203
|
+
const executorResponseCandidate = event.kind === 'response_started'
|
|
1204
|
+
&& event.session_epoch === this.#port.session.sessionEpoch
|
|
1205
|
+
&& !executorFencePendingAtStart
|
|
1206
|
+
&& this.#port.controller?.pending === true
|
|
1207
|
+
&& this.#executorApprovalIsolation.authority?.sessionEpoch === event.session_epoch
|
|
1208
|
+
&& !pendingExecutorResponseQuarantineAtStart
|
|
1209
|
+
&& this.#port.session.userInputRevision
|
|
1210
|
+
> (this.#executorApprovalIsolation.authority?.createdUserRevision ?? Number.MAX_SAFE_INTEGER);
|
|
1211
|
+
const provisionalCandidate = event.kind === 'response_started'
|
|
1212
|
+
&& event.origin === undefined
|
|
1213
|
+
&& event.session_epoch === this.#port.session.sessionEpoch
|
|
1214
|
+
&& !executorFencePendingAtStart
|
|
1215
|
+
&& this.#port.controller?.pending === true
|
|
1216
|
+
&& this.#executorApprovalIsolation.reservation === null
|
|
1217
|
+
&& !pendingExecutorResponseQuarantineAtStart
|
|
1218
|
+
&& this.#port.session.userInputRevision
|
|
1219
|
+
=== this.#executorApprovalIsolation.authority?.createdUserRevision;
|
|
1220
|
+
const responseStartsDuringSpeech = executorResponseCandidate
|
|
1221
|
+
&& this.#port.session.floor.state === 'user_speaking';
|
|
1222
|
+
const orphanedExecutorRetryCandidate = event.kind === 'response_started'
|
|
1223
|
+
&& (executorQuarantinedResponse || pendingExecutorResponseQuarantineAtStart);
|
|
1224
|
+
return { executorFencePendingAtStart, executorResponseCandidate, provisionalCandidate, orphanedExecutorRetryCandidate, pendingExecutorResponseQuarantineAtStart, executorQuarantinedResponse, responseStartsDuringSpeech };
|
|
1225
|
+
}
|
|
1226
|
+
async afterEventAccepted(event, accepted, observation) {
|
|
1227
|
+
const { executorFencePendingAtStart, executorResponseCandidate, provisionalCandidate, orphanedExecutorRetryCandidate, pendingExecutorResponseQuarantineAtStart } = observation;
|
|
1228
|
+
let { executorQuarantinedResponse } = observation;
|
|
1229
|
+
if (event.kind === 'response_started'
|
|
1230
|
+
&& accepted
|
|
1231
|
+
&& pendingExecutorResponseQuarantineAtStart
|
|
1232
|
+
&& this.#port.session.responseEventIds(event.response_id).length === 0) {
|
|
1233
|
+
this.#claimPendingExecutorApprovalResponseQuarantine(event.session_epoch, event.response_id);
|
|
1234
|
+
executorQuarantinedResponse = true;
|
|
1235
|
+
}
|
|
1236
|
+
const executorHostOwnedResponse = event.kind === 'response_started'
|
|
1237
|
+
&& accepted
|
|
1238
|
+
&& this.#isExecutorApprovalHostResponse(event.response_id);
|
|
1239
|
+
if (event.kind === 'response_started' && executorFencePendingAtStart) {
|
|
1240
|
+
this.#executorApprovalIsolation.setResponseFencePending(false);
|
|
1241
|
+
}
|
|
1242
|
+
if (event.kind === 'response_started' && accepted && executorQuarantinedResponse) {
|
|
1243
|
+
this.#port.session.suppressResponse(event.response_id);
|
|
1244
|
+
this.cancelExecutorApprovalPromptResponse(event.session_epoch, event.response_id);
|
|
1245
|
+
}
|
|
1246
|
+
if (event.kind === 'response_started'
|
|
1247
|
+
&& accepted
|
|
1248
|
+
&& executorResponseCandidate
|
|
1249
|
+
&& !executorHostOwnedResponse
|
|
1250
|
+
&& !orphanedExecutorRetryCandidate) {
|
|
1251
|
+
this.#executorApprovalIsolation.markBlockedResponse({
|
|
1252
|
+
sessionEpoch: event.session_epoch,
|
|
1253
|
+
responseId: event.response_id,
|
|
1254
|
+
});
|
|
1255
|
+
this.#port.session.suppressResponse(event.response_id);
|
|
1256
|
+
await this.#bindExecutorApprovalResponse(event.session_epoch, event.response_id);
|
|
1257
|
+
}
|
|
1258
|
+
if (event.kind === 'response_started'
|
|
1259
|
+
&& accepted
|
|
1260
|
+
&& provisionalCandidate
|
|
1261
|
+
&& !executorHostOwnedResponse
|
|
1262
|
+
&& !orphanedExecutorRetryCandidate
|
|
1263
|
+
&& this.#port.session.responseEventIds(event.response_id).length === 0) {
|
|
1264
|
+
const authority = this.#executorApprovalIsolation.authority;
|
|
1265
|
+
const tracked = authority === null ? 'stale' : this.#executorApprovalIsolation.trackProvisionalResponse({
|
|
1266
|
+
sessionEpoch: event.session_epoch,
|
|
1267
|
+
userRevision: authority.createdUserRevision + 1,
|
|
1268
|
+
responseId: event.response_id,
|
|
1269
|
+
});
|
|
1270
|
+
if (tracked === 'tracked' || tracked === 'idempotent') {
|
|
1271
|
+
this.#port.session.suppressResponse(event.response_id);
|
|
1272
|
+
}
|
|
1273
|
+
else if (tracked === 'overflow') {
|
|
1274
|
+
await this.#retireExecutorApprovalVoiceAuthority();
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
return executorQuarantinedResponse;
|
|
1278
|
+
}
|
|
1279
|
+
noteTerminal(event) {
|
|
1280
|
+
const executorCarrierKey = callKey(event.session_epoch, event.response_id);
|
|
1281
|
+
const executorCarrierAttempt = this.#executorApprovalCarrierAttempts.get(executorCarrierKey);
|
|
1282
|
+
if (executorCarrierAttempt !== undefined) {
|
|
1283
|
+
this.#executorApprovalCarrierAttempts.delete(executorCarrierKey);
|
|
1284
|
+
this.#port.telemetry?.record('approval.carrier', {
|
|
1285
|
+
session_epoch: event.session_epoch,
|
|
1286
|
+
attempt: executorCarrierAttempt,
|
|
1287
|
+
action: 'terminal',
|
|
1288
|
+
});
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
async settleTerminal(event) {
|
|
1292
|
+
const carrier = this.#executorApprovalIsolation.responseState({
|
|
1293
|
+
sessionEpoch: event.session_epoch,
|
|
1294
|
+
responseId: event.response_id,
|
|
1295
|
+
});
|
|
1296
|
+
if (carrier?.authorizationCarrier === true
|
|
1297
|
+
&& !this.#port.session.responseHasSpoken(event.response_id)
|
|
1298
|
+
&& this.#port.controller?.pending === true) {
|
|
1299
|
+
const reservation = this.#executorApprovalIsolation.reservation;
|
|
1300
|
+
const retry = this.#executorApprovalDecisionRetry;
|
|
1301
|
+
const isRetryTerminal = retry?.retry_response_id === event.response_id;
|
|
1302
|
+
if (reservation !== null && !isRetryTerminal) {
|
|
1303
|
+
this.#executorApprovalDecisionRetry ??= {
|
|
1304
|
+
item_key: callKey(reservation.sessionEpoch, reservation.itemId),
|
|
1305
|
+
source_response_id: event.response_id,
|
|
1306
|
+
requested: false,
|
|
1307
|
+
retry_response_id: null,
|
|
1308
|
+
};
|
|
1309
|
+
await this.#maybeRequestExecutorApprovalDecisionRetry();
|
|
1310
|
+
}
|
|
1311
|
+
else if (reservation !== null && isRetryTerminal) {
|
|
1312
|
+
await this.#exhaustExecutorApprovalAttempt();
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
clearTerminal(event) {
|
|
1317
|
+
if (!this.#executorApprovalIsolation.markProvisionalTerminal({
|
|
1318
|
+
sessionEpoch: event.session_epoch,
|
|
1319
|
+
responseId: event.response_id,
|
|
1320
|
+
})) {
|
|
1321
|
+
this.#executorApprovalIsolation.clearResponse({
|
|
1322
|
+
sessionEpoch: event.session_epoch,
|
|
1323
|
+
responseId: event.response_id,
|
|
1324
|
+
});
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
//# sourceMappingURL=approval.js.map
|