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,1459 @@
|
|
|
1
|
+
import { taskGrantService } from '../personal-agent/tasks.js';
|
|
2
|
+
import { CodingProgressNarrationState, codingProgressSummary } from '../realtime/coding-progress-narration.js';
|
|
3
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
4
|
+
import { canonicalJson, compareCodePoints } from '../text/canonical-json.js';
|
|
5
|
+
import { FRESH_WINDOW, RECENT_LIMIT, compileContextView, } from './context-view.js';
|
|
6
|
+
import { EventQueue, validProgressSummary, } from './events.js';
|
|
7
|
+
import { Floor } from '../realtime/floor.js';
|
|
8
|
+
import { stripLikePython } from '../text/python-text.js';
|
|
9
|
+
import { executorWithRole } from '../executors/coding-executor.js';
|
|
10
|
+
import { beginConfirmedProjectAdmission, finishConfirmedProjectAdmission, } from '../projects/confirmed-project-capability.js';
|
|
11
|
+
import { CONVERSATION_CHANNEL, USER_PRIORITY, Memory, parseMemoryRef, } from './memory.js';
|
|
12
|
+
import { compressorOutputSchema, delegateRequestSchema, delegateSchema, executorHandoffSchema, fastBrainOutputSchema, proactiveOutputSchema, } from './ports.js';
|
|
13
|
+
import { DEFAULT_COOLDOWN, SELECTED_WAKE_KIND, SuggestionPool, isSuggestionAvailable, } from './suggestions.js';
|
|
14
|
+
import { SLOTS, SlotSet, slotSchema, wakeReasonSchema, } from './slots.js';
|
|
15
|
+
const REJECTED_WAKE_KIND = 'delegate_rejected';
|
|
16
|
+
export class CoreRuntime {
|
|
17
|
+
queue = new EventQueue();
|
|
18
|
+
memory;
|
|
19
|
+
suggestions;
|
|
20
|
+
appliedEvents = [];
|
|
21
|
+
executorEffects = [];
|
|
22
|
+
floorDecisions = [];
|
|
23
|
+
diagnostics = [];
|
|
24
|
+
slots;
|
|
25
|
+
#ids;
|
|
26
|
+
#manifests = new Map();
|
|
27
|
+
#inFlight = new Map();
|
|
28
|
+
#dispatches = [];
|
|
29
|
+
#routableDelegates = new Map();
|
|
30
|
+
#terminationKind = new Map();
|
|
31
|
+
#terminationOutcome = new Map();
|
|
32
|
+
#handoffSeen = new Set();
|
|
33
|
+
/**
|
|
34
|
+
* What the event currently being applied did to a delegate, for the observer that runs next.
|
|
35
|
+
*
|
|
36
|
+
* The oracle keeps the same one-slot claim and compares it to the event by *object identity*. That
|
|
37
|
+
* is unavailable here: the observer is handed a `structuredClone`, so the object it sees is never
|
|
38
|
+
* the one that set the claim. The queue sequence is the faithful substitute -- every applied event
|
|
39
|
+
* in one runtime carries a unique one, and the observer runs synchronously between `apply` and the
|
|
40
|
+
* next `apply`, so a matching sequence means the same event and nothing else can have intervened.
|
|
41
|
+
*
|
|
42
|
+
* (The oracle rejected sequence comparison for its own version, on the grounds that a
|
|
43
|
+
* hand-constructed `Deadline` defaults to `seq=-1` and two of them compare equal. That is about
|
|
44
|
+
* events built in a test, not about events that went through the queue.)
|
|
45
|
+
*/
|
|
46
|
+
#eventClaim;
|
|
47
|
+
#eventDeadlineTermination;
|
|
48
|
+
#eventEpoch = new WeakMap();
|
|
49
|
+
#delegateEpoch = new WeakMap();
|
|
50
|
+
#conversationEpoch = 0;
|
|
51
|
+
#fencedDelegates = new Set();
|
|
52
|
+
#retainRoutingHistory;
|
|
53
|
+
#freshWindow;
|
|
54
|
+
#wiredSlots;
|
|
55
|
+
#onModelCall;
|
|
56
|
+
#onExecutorDispatch;
|
|
57
|
+
#suggestionSelectedObserver = null;
|
|
58
|
+
#jobs = new Map();
|
|
59
|
+
#results = new Map();
|
|
60
|
+
#preparedSpeech = new Map();
|
|
61
|
+
#latestProgressSuggestion = new Map();
|
|
62
|
+
#latestProgressSummary = new Map();
|
|
63
|
+
#latestObservationSuggestion = new Map();
|
|
64
|
+
#latestProgress = new Map();
|
|
65
|
+
#compressDeferred = new Set();
|
|
66
|
+
#compressNotBefore = new Map();
|
|
67
|
+
#compressBacklog = [];
|
|
68
|
+
#compressScheduled = new Set();
|
|
69
|
+
#jobSequence = 0;
|
|
70
|
+
#utteranceSequence = 0;
|
|
71
|
+
floor = new Floor();
|
|
72
|
+
codingProgressNarration;
|
|
73
|
+
constructor(options) {
|
|
74
|
+
this.codingProgressNarration = options.codingProgressNarration ?? new CodingProgressNarrationState();
|
|
75
|
+
this.codingProgressNarration.observe(() => {
|
|
76
|
+
for (const delegate of this.#inFlight.values()) {
|
|
77
|
+
if (this.#manifests.get(delegate.executor)?.roles.includes('coding')) {
|
|
78
|
+
this.#withdrawProgressSuggestion(delegate.delegate_id);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
for (const manifest of options.manifests)
|
|
83
|
+
this.#manifests.set(manifest.name, manifest);
|
|
84
|
+
this.memory = new Memory({ policies: options.manifests.map(manifest => manifest.policy),
|
|
85
|
+
scope: { conversation_id: options.conversationId ?? randomUUID() },
|
|
86
|
+
...(options.recovery === undefined ? {} : { recovery: options.recovery }) });
|
|
87
|
+
this.#ids = options.ids;
|
|
88
|
+
this.#wiredSlots = new Set(options.modelSlots ?? []);
|
|
89
|
+
this.#onModelCall = options.onModelCall;
|
|
90
|
+
this.#onExecutorDispatch = options.onExecutorDispatch;
|
|
91
|
+
this.#retainRoutingHistory = options.retainRoutingHistory ?? false;
|
|
92
|
+
this.#freshWindow = options.freshWindow ?? FRESH_WINDOW;
|
|
93
|
+
this.suggestions = options.suggestionCooldown === undefined
|
|
94
|
+
? new SuggestionPool()
|
|
95
|
+
: new SuggestionPool({ defaultCooldown: options.suggestionCooldown });
|
|
96
|
+
this.slots = new SlotSet((slot, reason) => this.#spawnSlot(slot, reason));
|
|
97
|
+
}
|
|
98
|
+
post(input, at) {
|
|
99
|
+
const event = this.queue.push(input, at);
|
|
100
|
+
this.#eventEpoch.set(event, this.#conversationEpoch);
|
|
101
|
+
return event;
|
|
102
|
+
}
|
|
103
|
+
get conversationEpoch() { return this.#conversationEpoch; }
|
|
104
|
+
/** Event epochs are host-only: a queued record never exposes this control metadata on the wire. */
|
|
105
|
+
isCurrentConversationEvent(event) {
|
|
106
|
+
return (this.#eventEpoch.get(event) ?? this.#conversationEpoch) === this.#conversationEpoch;
|
|
107
|
+
}
|
|
108
|
+
/** Session persistence owns Memory.clear; Core only retires process-local conversation state. */
|
|
109
|
+
resetConversationState() {
|
|
110
|
+
this.#conversationEpoch += 1;
|
|
111
|
+
this.queue.removeWhere(event => !this.isCurrentConversationEvent(event)
|
|
112
|
+
&& event.kind !== 'handoff' && event.kind !== 'deadline');
|
|
113
|
+
this.#jobs.clear();
|
|
114
|
+
this.#results.clear();
|
|
115
|
+
this.#preparedSpeech.clear();
|
|
116
|
+
this.slots.clear();
|
|
117
|
+
this.#compressBacklog.length = 0;
|
|
118
|
+
this.#compressScheduled.clear();
|
|
119
|
+
this.#compressNotBefore.clear();
|
|
120
|
+
this.#compressDeferred.clear();
|
|
121
|
+
this.#latestProgress.clear();
|
|
122
|
+
this.#latestProgressSuggestion.clear();
|
|
123
|
+
this.#latestProgressSummary.clear();
|
|
124
|
+
this.#latestObservationSuggestion.clear();
|
|
125
|
+
this.suggestions.clear();
|
|
126
|
+
this.floor = new Floor();
|
|
127
|
+
this.appliedEvents.length = 0;
|
|
128
|
+
this.executorEffects.length = 0;
|
|
129
|
+
this.floorDecisions.length = 0;
|
|
130
|
+
this.diagnostics.length = 0;
|
|
131
|
+
this.#eventClaim = undefined;
|
|
132
|
+
this.#eventDeadlineTermination = undefined;
|
|
133
|
+
}
|
|
134
|
+
startUserSpeech(speechId) {
|
|
135
|
+
this.floor = this.floor.onUserSpeakStart(speechId);
|
|
136
|
+
}
|
|
137
|
+
endUserSpeech(speechId) {
|
|
138
|
+
this.floor = this.floor.onUserSpeakEnd(speechId);
|
|
139
|
+
}
|
|
140
|
+
startAgentSpeech(utteranceId, priority) {
|
|
141
|
+
this.floor = this.floor.onSpeakStart(utteranceId, priority);
|
|
142
|
+
}
|
|
143
|
+
endAgentSpeech(utteranceId) {
|
|
144
|
+
this.floor = this.floor.onSpeakEnd(utteranceId);
|
|
145
|
+
}
|
|
146
|
+
/** Bind the realtime speech outlet for selected proactive communication. */
|
|
147
|
+
bindSuggestionSelected(observer) {
|
|
148
|
+
if (this.#suggestionSelectedObserver !== null) {
|
|
149
|
+
throw new Error('suggestion selected observer is already bound');
|
|
150
|
+
}
|
|
151
|
+
this.#suggestionSelectedObserver = observer;
|
|
152
|
+
let bound = true;
|
|
153
|
+
return () => {
|
|
154
|
+
if (!bound)
|
|
155
|
+
return;
|
|
156
|
+
bound = false;
|
|
157
|
+
if (this.#suggestionSelectedObserver === observer)
|
|
158
|
+
this.#suggestionSelectedObserver = null;
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Request the Floor, called by a streaming port before its first sink write.
|
|
163
|
+
*
|
|
164
|
+
* Ported from `Runtime._open_floor`. Three steps, and the order of the last two is the
|
|
165
|
+
* point: post the event so every Floor transition stays replayable, THEN claim an
|
|
166
|
+
* in-place reservation. Posting alone only queues the event; `this.floor` would not
|
|
167
|
+
* change until apply, and with two concurrent slots there is a real window where
|
|
168
|
+
* `speak_start` is still queued while text already streams. A `surrogate.watch` view
|
|
169
|
+
* compiled in that window would read `floor=idle` and conclude it may speak, which is
|
|
170
|
+
* the opposite of reality rather than a conservative guess. The reservation does not
|
|
171
|
+
* break replay: applying the queued `speak_start` later recomputes the same transition
|
|
172
|
+
* from the same inputs.
|
|
173
|
+
*
|
|
174
|
+
* `allow` and `preempt` share this path. Floor's `onSpeakStart` replaces the current
|
|
175
|
+
* utterance wholesale, and the preempted utterance's late `speak_end` cannot enter
|
|
176
|
+
* because `onSpeakEnd` checks the utterance id, so no compensating event is needed.
|
|
177
|
+
*/
|
|
178
|
+
openFloor(jobId, utteranceId, priority, at) {
|
|
179
|
+
if (!this.#jobs.has(jobId))
|
|
180
|
+
return false;
|
|
181
|
+
const decision = this.floor.decide(priority);
|
|
182
|
+
this.#preparedSpeech.set(jobId, { decision });
|
|
183
|
+
if (decision === 'defer')
|
|
184
|
+
return false;
|
|
185
|
+
this.post({ kind: 'speak_start', payload: { utterance_id: utteranceId, priority } }, at);
|
|
186
|
+
this.floor = this.floor.onSpeakStart(utteranceId, priority);
|
|
187
|
+
return true;
|
|
188
|
+
}
|
|
189
|
+
/** Release the Floor when a streaming utterance ends. */
|
|
190
|
+
closeFloor(utteranceId, at) {
|
|
191
|
+
return this.post({ kind: 'speak_end', payload: { utterance_id: utteranceId } }, at);
|
|
192
|
+
}
|
|
193
|
+
postExecutorCompletion(dispatchIndex, completion, at) {
|
|
194
|
+
const delegate = this.#dispatches[dispatchIndex];
|
|
195
|
+
if (delegate === undefined)
|
|
196
|
+
throw new Error(`unknown dispatch index: ${dispatchIndex}`);
|
|
197
|
+
return this.#postForDelegate(delegate, {
|
|
198
|
+
kind: 'handoff',
|
|
199
|
+
payload: {
|
|
200
|
+
channel: delegate.executor,
|
|
201
|
+
delegate_id: delegate.delegate_id,
|
|
202
|
+
origin_ref: delegate.origin_ref,
|
|
203
|
+
outcome: completion.outcome,
|
|
204
|
+
trust: executorTrust(completion.trust),
|
|
205
|
+
content: structuredClone(completion.content),
|
|
206
|
+
refs: [...(completion.refs ?? [])],
|
|
207
|
+
},
|
|
208
|
+
}, at);
|
|
209
|
+
}
|
|
210
|
+
postExecutorResult(dispatchIndex, output, at) {
|
|
211
|
+
const parsed = executorHandoffSchema.safeParse(output);
|
|
212
|
+
return this.postExecutorCompletion(dispatchIndex, parsed.success ? parsed.data : {
|
|
213
|
+
outcome: 'unknown',
|
|
214
|
+
trust: 'trusted_system',
|
|
215
|
+
content: {
|
|
216
|
+
error: 'adapter_raised',
|
|
217
|
+
exception: 'ExecutorContractError',
|
|
218
|
+
detail: 'invalid_executor_output',
|
|
219
|
+
},
|
|
220
|
+
refs: [],
|
|
221
|
+
}, at);
|
|
222
|
+
}
|
|
223
|
+
postExecutorProgress(dispatchIndex, progress, at) {
|
|
224
|
+
const delegate = this.#dispatches[dispatchIndex];
|
|
225
|
+
if (delegate === undefined)
|
|
226
|
+
throw new Error(`unknown dispatch index: ${dispatchIndex}`);
|
|
227
|
+
return this.#postForDelegate(delegate, {
|
|
228
|
+
kind: 'progress',
|
|
229
|
+
payload: {
|
|
230
|
+
channel: delegate.executor,
|
|
231
|
+
delegate_id: delegate.delegate_id,
|
|
232
|
+
op: delegate.op,
|
|
233
|
+
phase: progress.phase,
|
|
234
|
+
internal_activity: progress.internal_activity,
|
|
235
|
+
elapsed: progress.elapsed,
|
|
236
|
+
summary: progress.summary,
|
|
237
|
+
},
|
|
238
|
+
}, at);
|
|
239
|
+
}
|
|
240
|
+
postExecutorObservation(dispatchIndex, observation, at) {
|
|
241
|
+
const delegate = this.#dispatches[dispatchIndex];
|
|
242
|
+
if (delegate === undefined)
|
|
243
|
+
throw new Error(`unknown dispatch index: ${dispatchIndex}`);
|
|
244
|
+
return this.#postForDelegate(delegate, {
|
|
245
|
+
kind: 'observation',
|
|
246
|
+
payload: {
|
|
247
|
+
channel: delegate.executor,
|
|
248
|
+
delegate_id: delegate.delegate_id,
|
|
249
|
+
op: delegate.op,
|
|
250
|
+
origin_ref: delegate.origin_ref,
|
|
251
|
+
trust: executorTrust(observation.trust),
|
|
252
|
+
content: structuredClone(observation.content),
|
|
253
|
+
refs: [...(observation.refs ?? [])],
|
|
254
|
+
},
|
|
255
|
+
}, at);
|
|
256
|
+
}
|
|
257
|
+
#postForDelegate(delegate, input, at) {
|
|
258
|
+
const event = this.post(input, at);
|
|
259
|
+
const epoch = this.#delegateEpoch.get(delegate);
|
|
260
|
+
if (epoch !== undefined)
|
|
261
|
+
this.#eventEpoch.set(event, epoch);
|
|
262
|
+
return event;
|
|
263
|
+
}
|
|
264
|
+
completeModelCall(jobId, output, at) {
|
|
265
|
+
const job = this.#jobs.get(jobId);
|
|
266
|
+
if (job === undefined)
|
|
267
|
+
throw new Error(`unknown model job: ${jobId}`);
|
|
268
|
+
if (this.#results.has(jobId))
|
|
269
|
+
throw new Error(`model job already completed: ${jobId}`);
|
|
270
|
+
if (!Number.isFinite(at) || at < job.startedAt) {
|
|
271
|
+
throw new RangeError(`invalid model completion timestamp: ${at}`);
|
|
272
|
+
}
|
|
273
|
+
this.#results.set(jobId, structuredClone(output));
|
|
274
|
+
if (job.slot === 'compress') {
|
|
275
|
+
if (job.compression === null)
|
|
276
|
+
throw new Error(`missing compression record: ${jobId}`);
|
|
277
|
+
return this.post({
|
|
278
|
+
kind: 'compress_done',
|
|
279
|
+
payload: { channel: job.compression.channel, job_id: jobId },
|
|
280
|
+
}, at);
|
|
281
|
+
}
|
|
282
|
+
if (job.slot === 'fast')
|
|
283
|
+
this.#prepareSpeech(job, output, at);
|
|
284
|
+
const completion = this.post({
|
|
285
|
+
kind: 'model_done',
|
|
286
|
+
payload: { slot: job.slot, job_id: jobId },
|
|
287
|
+
}, at);
|
|
288
|
+
const prepared = this.#preparedSpeech.get(jobId);
|
|
289
|
+
if (prepared !== undefined) {
|
|
290
|
+
this.floorDecisions.push({
|
|
291
|
+
event_seq: completion.seq,
|
|
292
|
+
priority: job.reason.priority,
|
|
293
|
+
decision: prepared.decision,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
return completion;
|
|
297
|
+
}
|
|
298
|
+
/** Refresh only after durable retention has been applied, before the actual model sees context. */
|
|
299
|
+
refreshModelCall(call) {
|
|
300
|
+
const job = this.#jobs.get(call.job_id);
|
|
301
|
+
if (job === undefined || this.#results.has(job.jobId))
|
|
302
|
+
throw new Error('model job is no longer pending');
|
|
303
|
+
if (job.compression !== null) {
|
|
304
|
+
const channel = this.memory.channels.get(job.compression.channel);
|
|
305
|
+
const items = structuredClone(channel.items.filter(item => item.seq <= job.compression.throughSequence));
|
|
306
|
+
const compression = { ...job.compression, items, throughSequence: items.at(-1)?.seq ?? 0,
|
|
307
|
+
retentionRevision: channel.retentionRevision };
|
|
308
|
+
this.#jobs.set(job.jobId, { ...job, compression });
|
|
309
|
+
return { ...call, compression_items: items };
|
|
310
|
+
}
|
|
311
|
+
const context = compileContextView(this.memory, this.floor.state, job.startedAt, {
|
|
312
|
+
inFlight: this.#currentConversationDelegates(), manifests: [...this.#manifests.values()],
|
|
313
|
+
suggestions: this.suggestions.all().filter(suggestion => job.offeredSuggestions.has(suggestion.id)),
|
|
314
|
+
selectedSuggestion: job.selectedSuggestion, triggerKind: job.reason.kind, freshWindow: this.#freshWindow,
|
|
315
|
+
});
|
|
316
|
+
this.#jobs.set(job.jobId, { ...job, visibleRefs: this.#visibleMemoryRefs() });
|
|
317
|
+
return { ...call, context_view: context };
|
|
318
|
+
}
|
|
319
|
+
apply(event) {
|
|
320
|
+
if (!this.isCurrentConversationEvent(event)) {
|
|
321
|
+
if (event.kind === 'handoff') {
|
|
322
|
+
const delegate = this.#applyHandoff(event, false);
|
|
323
|
+
if (delegate !== undefined)
|
|
324
|
+
this.#eventClaim = { seq: event.seq, delegate };
|
|
325
|
+
}
|
|
326
|
+
else if (event.kind === 'deadline') {
|
|
327
|
+
const delegate = this.#applyDeadline(event, false);
|
|
328
|
+
if (delegate !== undefined) {
|
|
329
|
+
this.#eventDeadlineTermination = { seq: event.seq, delegateId: delegate.delegate_id };
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return null;
|
|
333
|
+
}
|
|
334
|
+
this.appliedEvents.push(event);
|
|
335
|
+
let target = null;
|
|
336
|
+
switch (event.kind) {
|
|
337
|
+
case 'user_input': {
|
|
338
|
+
const content = { text: event.payload.text };
|
|
339
|
+
if (event.payload.media_refs !== undefined) {
|
|
340
|
+
content.media_refs = [...event.payload.media_refs];
|
|
341
|
+
}
|
|
342
|
+
const item = this.#appendMemory(CONVERSATION_CHANNEL, {
|
|
343
|
+
ts: event.ts,
|
|
344
|
+
trust: 'trusted_user',
|
|
345
|
+
priority: USER_PRIORITY,
|
|
346
|
+
content,
|
|
347
|
+
});
|
|
348
|
+
target = {
|
|
349
|
+
slot: 'fast',
|
|
350
|
+
reason: wakeReasonSchema.parse({
|
|
351
|
+
kind: 'user_input',
|
|
352
|
+
priority: USER_PRIORITY,
|
|
353
|
+
routing_class: 'user_awaited',
|
|
354
|
+
origin: memoryItemRef(item),
|
|
355
|
+
}),
|
|
356
|
+
};
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
case 'handoff': {
|
|
360
|
+
const delegate = this.#applyHandoff(event);
|
|
361
|
+
if (delegate !== undefined)
|
|
362
|
+
this.#eventClaim = { seq: event.seq, delegate };
|
|
363
|
+
target = this.#handoffTarget(event, delegate);
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
case 'deadline': {
|
|
367
|
+
const delegate = this.#applyDeadline(event);
|
|
368
|
+
if (delegate !== undefined) {
|
|
369
|
+
this.#eventDeadlineTermination = { seq: event.seq, delegateId: delegate.delegate_id };
|
|
370
|
+
}
|
|
371
|
+
target = delegate === undefined ? null : this.#resultTarget(delegate, event.kind);
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
case 'progress': {
|
|
375
|
+
const applied = this.#applyProgress(event);
|
|
376
|
+
const delegate = applied?.delegate;
|
|
377
|
+
const policy = delegate === undefined ? undefined : this.memory.policies.get(delegate.executor);
|
|
378
|
+
if (applied === undefined || delegate === undefined || policy === undefined)
|
|
379
|
+
break;
|
|
380
|
+
const coding = this.#manifests.get(delegate.executor)?.roles.includes('coding') === true;
|
|
381
|
+
if (this.codingProgressNarration.viaProactive(coding, policy.progress_via_surrogate)) {
|
|
382
|
+
if (applied.proactiveCandidateCreated) {
|
|
383
|
+
target = {
|
|
384
|
+
slot: 'surrogate.watch',
|
|
385
|
+
reason: wakeReasonSchema.parse({
|
|
386
|
+
kind: event.kind,
|
|
387
|
+
priority: policy.priority,
|
|
388
|
+
routing_class: 'ambient',
|
|
389
|
+
origin: delegate.delegate_id,
|
|
390
|
+
}),
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
const direct = this.#resultTarget(delegate, event.kind);
|
|
396
|
+
target = coding && this.codingProgressNarration.mode === 'continuous'
|
|
397
|
+
? (direct === null ? null : { ...direct, slot: 'fast' })
|
|
398
|
+
: direct;
|
|
399
|
+
}
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case 'observation': {
|
|
403
|
+
const applied = this.#applyObservation(event);
|
|
404
|
+
if (applied?.shouldWake === true) {
|
|
405
|
+
target = this.#resultTarget(applied.delegate, event.kind);
|
|
406
|
+
}
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
case 'speak_start':
|
|
410
|
+
this.floor = this.floor.onSpeakStart(event.payload.utterance_id, event.payload.priority);
|
|
411
|
+
break;
|
|
412
|
+
case 'speak_end':
|
|
413
|
+
this.floor = this.floor.onSpeakEnd(event.payload.utterance_id);
|
|
414
|
+
break;
|
|
415
|
+
case 'compress':
|
|
416
|
+
this.#applyCompress(event);
|
|
417
|
+
target = {
|
|
418
|
+
slot: 'compress',
|
|
419
|
+
reason: wakeReasonSchema.parse({ kind: event.kind, priority: 0 }),
|
|
420
|
+
};
|
|
421
|
+
break;
|
|
422
|
+
case 'model_done':
|
|
423
|
+
this.#applyModelDone(event);
|
|
424
|
+
break;
|
|
425
|
+
case 'compress_done':
|
|
426
|
+
this.#applyCompressDone(event);
|
|
427
|
+
break;
|
|
428
|
+
case 'discovery_tick':
|
|
429
|
+
break;
|
|
430
|
+
case 'assistant_spoken':
|
|
431
|
+
this.#appendMemory(CONVERSATION_CHANNEL, {
|
|
432
|
+
ts: event.ts,
|
|
433
|
+
trust: 'trusted_system',
|
|
434
|
+
priority: USER_PRIORITY,
|
|
435
|
+
content: event.payload,
|
|
436
|
+
});
|
|
437
|
+
break;
|
|
438
|
+
default:
|
|
439
|
+
return exhaustiveEvent(event);
|
|
440
|
+
}
|
|
441
|
+
if (target !== null)
|
|
442
|
+
this.#wake(target.slot, target.reason);
|
|
443
|
+
return target?.reason ?? null;
|
|
444
|
+
}
|
|
445
|
+
consumeFastBrain(output, reason, eventSequence) {
|
|
446
|
+
return this.#consumeFastBrain(output, reason, eventSequence);
|
|
447
|
+
}
|
|
448
|
+
#consumeFastBrain(output, reason, eventSequence, job) {
|
|
449
|
+
const result = fastBrainOutputSchema.safeParse(output);
|
|
450
|
+
if (!result.success) {
|
|
451
|
+
return this.#refuse({
|
|
452
|
+
error: 'model_contract_failure',
|
|
453
|
+
code: 'invalid_fastbrain_output',
|
|
454
|
+
tool_name: null,
|
|
455
|
+
}, reason);
|
|
456
|
+
}
|
|
457
|
+
const parsed = result.data;
|
|
458
|
+
this.#consumeSpeech(parsed.speak, reason, eventSequence, job);
|
|
459
|
+
// Two gates, in this order, both of which suppress the action entirely. They answer
|
|
460
|
+
// different questions: is the output structurally valid, and does the world it
|
|
461
|
+
// answers still exist? Speech has already been consumed either way -- a rejected
|
|
462
|
+
// action does not retract words the user heard.
|
|
463
|
+
if (parsed.contract_failures.length > 0) {
|
|
464
|
+
return this.#rejectContract(parsed.contract_failures, reason);
|
|
465
|
+
}
|
|
466
|
+
if (parsed.extra_actions > 0) {
|
|
467
|
+
return this.#rejectMultipleActions(parsed.action.act, parsed.extra_actions, reason);
|
|
468
|
+
}
|
|
469
|
+
if (parsed.action.act !== 'none' && job !== undefined) {
|
|
470
|
+
const supersededBy = this.#supersedingUserInput(job);
|
|
471
|
+
if (supersededBy !== undefined) {
|
|
472
|
+
this.#dropStaleAction(parsed.action, reason, supersededBy);
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (parsed.action.act === 'delegate') {
|
|
477
|
+
return this.#dispatch(parsed.action.delegate, reason, job?.visibleRefs).wake;
|
|
478
|
+
}
|
|
479
|
+
return null;
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Admit one already-normalized external proposal without awaiting its worker.
|
|
483
|
+
*
|
|
484
|
+
* The model's own dispatches arrive through `consumeFastBrain` with the visible refs of the call
|
|
485
|
+
* that produced them. An external proposal has no such call, so the refs are compiled fresh here:
|
|
486
|
+
* the oracle passes a newly compiled view for exactly this reason, and skipping it would let a
|
|
487
|
+
* caller cite a memory item that has aged out of the recent window -- the one check that turns
|
|
488
|
+
* "may only reference what it has actually seen" into something enforceable.
|
|
489
|
+
*/
|
|
490
|
+
dispatchTaskExternal(request, reason, grant) {
|
|
491
|
+
taskGrantService(grant);
|
|
492
|
+
if (request.origin_ref !== grant.origin_ref)
|
|
493
|
+
throw Error('invalid_origin_ref');
|
|
494
|
+
const admission = this.#dispatch(request, reason, new Set([grant.origin_ref]), grant);
|
|
495
|
+
return { accepted: admission.accepted, delegate_id: admission.delegate_id, problem: admission.problem };
|
|
496
|
+
}
|
|
497
|
+
dispatchExternal(request, reason) {
|
|
498
|
+
const admission = this.#dispatch(request, reason, this.#visibleMemoryRefs());
|
|
499
|
+
return {
|
|
500
|
+
accepted: admission.accepted,
|
|
501
|
+
delegate_id: admission.delegate_id,
|
|
502
|
+
problem: admission.problem,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Admit the one private confirmed-project operation with its immutable proposal capability.
|
|
507
|
+
*
|
|
508
|
+
* Only this path may keep a real proposal origin visible after it leaves RECENT_LIMIT. Every other
|
|
509
|
+
* external dispatch continues to compile the ordinary recent view above.
|
|
510
|
+
*/
|
|
511
|
+
dispatchConfirmedExternal(request, reason, capability, taskGrant) {
|
|
512
|
+
const parsed = delegateRequestSchema.safeParse(request);
|
|
513
|
+
const coding = executorWithRole(this.#manifests.values(), 'coding');
|
|
514
|
+
if (!parsed.success || !beginConfirmedProjectAdmission(capability, parsed.data, coding?.name ?? null)) {
|
|
515
|
+
const refused = this.#refuseDelegate(refusalDelegateRequest(request), 'confirmed_capability_invalid', reason);
|
|
516
|
+
return {
|
|
517
|
+
accepted: refused.accepted,
|
|
518
|
+
delegate_id: refused.delegate_id,
|
|
519
|
+
problem: refused.problem,
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
let accepted = false;
|
|
523
|
+
try {
|
|
524
|
+
// The capability pins only its exact origin. #dispatch still validates that the Memory item
|
|
525
|
+
// exists and applies duplicate, executor, operation and deadline guards normally.
|
|
526
|
+
const admission = this.#dispatch(parsed.data, reason, new Set([parsed.data.origin_ref]), taskGrant);
|
|
527
|
+
accepted = admission.accepted;
|
|
528
|
+
return {
|
|
529
|
+
accepted: admission.accepted,
|
|
530
|
+
delegate_id: admission.delegate_id,
|
|
531
|
+
problem: admission.problem,
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
finally {
|
|
535
|
+
finishConfirmedProjectAdmission(capability, accepted);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* The delegate a handoff event claimed, if this exact event claimed one.
|
|
540
|
+
*
|
|
541
|
+
* A handoff that claimed nothing -- a duplicate, or one for a delegate already settled -- returns
|
|
542
|
+
* undefined even when an earlier handoff left a claim behind, which is the whole point of matching
|
|
543
|
+
* the sequence rather than just reading the slot.
|
|
544
|
+
*/
|
|
545
|
+
claimedHandoff(seq) {
|
|
546
|
+
return this.#eventClaim?.seq === seq ? this.#eventClaim.delegate : undefined;
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Whether this exact deadline event is the one that terminated its delegate.
|
|
550
|
+
*
|
|
551
|
+
* Not "is it still in flight": the four-step termination already removed it from the in-flight
|
|
552
|
+
* table inside `apply`, so a later deadline for the same delegate would look identical. And not
|
|
553
|
+
* "was it terminated by a deadline at some point", which stays true for a second deadline and would
|
|
554
|
+
* announce the same timeout twice.
|
|
555
|
+
*/
|
|
556
|
+
terminatedByDeadline(seq, delegateId) {
|
|
557
|
+
return this.#eventDeadlineTermination?.seq === seq
|
|
558
|
+
&& this.#eventDeadlineTermination.delegateId === delegateId;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* The delegate from either table, whether or not it is still in flight.
|
|
562
|
+
*
|
|
563
|
+
* A late handoff needs the values bound at dispatch time, so a terminated delegate has to stay
|
|
564
|
+
* findable.
|
|
565
|
+
*/
|
|
566
|
+
delegateFor(delegateId) {
|
|
567
|
+
return this.#inFlight.get(delegateId) ?? this.#routableDelegates.get(delegateId);
|
|
568
|
+
}
|
|
569
|
+
/**
|
|
570
|
+
* The delegate only if it is still in flight.
|
|
571
|
+
*
|
|
572
|
+
* The difference from `delegateFor` is the whole distinction between "this run is live" and "this
|
|
573
|
+
* run happened": a progress or observation event for a settled delegate describes a run that is
|
|
574
|
+
* over, and projecting it would report activity that has stopped.
|
|
575
|
+
*/
|
|
576
|
+
inFlightDelegate(delegateId) {
|
|
577
|
+
return this.#inFlight.get(delegateId);
|
|
578
|
+
}
|
|
579
|
+
activeDelegates() {
|
|
580
|
+
return [...this.#inFlight.values()].sort((left, right) => (left.dispatched_at - right.dispatched_at || compareCodePoints(left.delegate_id, right.delegate_id)));
|
|
581
|
+
}
|
|
582
|
+
#currentConversationDelegates() {
|
|
583
|
+
return this.activeDelegates().filter(delegate => (this.#delegateEpoch.get(delegate) === this.#conversationEpoch));
|
|
584
|
+
}
|
|
585
|
+
assertQuiescent() {
|
|
586
|
+
for (const slot of SLOTS) {
|
|
587
|
+
if (this.slots.inflight[slot] || this.slots.pending[slot] !== null) {
|
|
588
|
+
throw new Error(`model slot is not quiescent: ${slot}`);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
if (this.#jobs.size > 0 || this.#results.size > 0 || this.#preparedSpeech.size > 0) {
|
|
592
|
+
throw new Error('model jobs remain at quiescence');
|
|
593
|
+
}
|
|
594
|
+
if (this.#compressBacklog.length > 0)
|
|
595
|
+
throw new Error('compression backlog remains');
|
|
596
|
+
}
|
|
597
|
+
#dispatch(request, reason, visibleRefs, taskGrant) {
|
|
598
|
+
const result = delegateRequestSchema.safeParse(request);
|
|
599
|
+
if (!result.success)
|
|
600
|
+
return this.#refuseDelegate(request, 'invalid_delegate_request', reason);
|
|
601
|
+
const parsed = result.data;
|
|
602
|
+
const manifest = this.#manifests.get(parsed.executor);
|
|
603
|
+
if (manifest === undefined)
|
|
604
|
+
return this.#refuseDelegate(parsed, 'unknown_executor', reason);
|
|
605
|
+
const operation = manifest.ops.find(candidate => candidate.name === parsed.op);
|
|
606
|
+
if (operation === undefined)
|
|
607
|
+
return this.#refuseDelegate(parsed, 'unknown_operation', reason);
|
|
608
|
+
const duplicate = [...this.#inFlight.values()].find(delegate => (sameDelegateRequest(delegate, parsed)));
|
|
609
|
+
if (duplicate !== undefined) {
|
|
610
|
+
return this.#refuseDelegate(parsed, `${duplicate.delegate_id} is already performing the same request`, reason);
|
|
611
|
+
}
|
|
612
|
+
if (!operation.readonly) {
|
|
613
|
+
const unresolved = [...this.#routableDelegates.values()].find(delegate => {
|
|
614
|
+
if (this.#fencedDelegates.has(delegate.delegate_id))
|
|
615
|
+
return false;
|
|
616
|
+
const termination = this.#terminationKind.get(delegate.delegate_id);
|
|
617
|
+
const outcome = this.#terminationOutcome.get(delegate.delegate_id);
|
|
618
|
+
return sameDelegateRequest(delegate, parsed)
|
|
619
|
+
&& (termination === 'deadline' || (termination === 'handoff' && outcome === 'unknown'));
|
|
620
|
+
});
|
|
621
|
+
if (unresolved !== undefined) {
|
|
622
|
+
this.#fencedDelegates.add(unresolved.delegate_id);
|
|
623
|
+
if (this.#handoffSeen.has(unresolved.delegate_id) && !this.#retainRoutingHistory) {
|
|
624
|
+
this.#reclaimRouting(unresolved.delegate_id);
|
|
625
|
+
}
|
|
626
|
+
return this.#refuseDelegate(parsed, `${unresolved.delegate_id} stopped at unknown; verify before retrying`, reason);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
const originProblem = this.#originProblem(parsed.origin_ref, visibleRefs, taskGrant);
|
|
630
|
+
if (originProblem !== null)
|
|
631
|
+
return this.#refuseDelegate(parsed, originProblem, reason);
|
|
632
|
+
const dispatchedAt = this.appliedEvents.at(-1)?.ts ?? 0;
|
|
633
|
+
const delegate = delegateSchema.parse({
|
|
634
|
+
...parsed,
|
|
635
|
+
request: structuredClone(parsed.request),
|
|
636
|
+
delegate_id: this.#ids.next('delegate'),
|
|
637
|
+
deadline: operation.deadline_budget === null ? null : dispatchedAt + operation.deadline_budget,
|
|
638
|
+
routing_class: reason.routing_class,
|
|
639
|
+
dispatched_at: dispatchedAt,
|
|
640
|
+
});
|
|
641
|
+
this.#delegateEpoch.set(delegate, this.#conversationEpoch);
|
|
642
|
+
this.#inFlight.set(delegate.delegate_id, delegate);
|
|
643
|
+
this.#dispatches.push(delegate);
|
|
644
|
+
this.#routableDelegates.set(delegate.delegate_id, delegate);
|
|
645
|
+
if (delegate.deadline !== null)
|
|
646
|
+
this.post({ kind: 'deadline', payload: { delegate_id: delegate.delegate_id } }, delegate.deadline);
|
|
647
|
+
// Python's runtime uses one job sequence for model and executor tasks. The
|
|
648
|
+
// executor job id stays private, but reserving it keeps later public
|
|
649
|
+
// model_done ids cross-language exact.
|
|
650
|
+
this.#jobSequence += 1;
|
|
651
|
+
this.executorEffects.push({ kind: 'dispatch', delegate });
|
|
652
|
+
this.#onExecutorDispatch?.(this.#dispatches.length - 1, delegate);
|
|
653
|
+
return { accepted: true, delegate_id: delegate.delegate_id, problem: null, wake: null };
|
|
654
|
+
}
|
|
655
|
+
#applyHandoff(event, record = true) {
|
|
656
|
+
const delegate = this.#routableDelegates.get(event.payload.delegate_id);
|
|
657
|
+
const active = delegate === undefined ? undefined : this.#inFlight.get(delegate.delegate_id);
|
|
658
|
+
const previousOutcome = delegate === undefined
|
|
659
|
+
? undefined
|
|
660
|
+
: this.#terminationOutcome.get(delegate.delegate_id);
|
|
661
|
+
const definitive = event.payload.outcome === 'ok'
|
|
662
|
+
|| event.payload.outcome === 'refused'
|
|
663
|
+
|| event.payload.outcome === 'failed'
|
|
664
|
+
|| event.payload.outcome === 'cancelled';
|
|
665
|
+
let claimed;
|
|
666
|
+
if (delegate !== undefined
|
|
667
|
+
&& (active !== undefined
|
|
668
|
+
|| (!this.#handoffSeen.has(delegate.delegate_id)
|
|
669
|
+
&& this.#terminationKind.get(delegate.delegate_id) === 'deadline')
|
|
670
|
+
|| (previousOutcome === 'unknown' && definitive))) {
|
|
671
|
+
claimed = delegate;
|
|
672
|
+
this.#handoffSeen.add(delegate.delegate_id);
|
|
673
|
+
}
|
|
674
|
+
if (active !== undefined) {
|
|
675
|
+
this.#inFlight.delete(active.delegate_id);
|
|
676
|
+
this.#terminationKind.set(active.delegate_id, 'handoff');
|
|
677
|
+
}
|
|
678
|
+
if (delegate !== undefined && (active !== undefined || (previousOutcome === 'unknown' && definitive))) {
|
|
679
|
+
this.#terminationOutcome.set(delegate.delegate_id, event.payload.outcome);
|
|
680
|
+
}
|
|
681
|
+
if (delegate !== undefined) {
|
|
682
|
+
this.#clearObservationCondition(delegate.delegate_id);
|
|
683
|
+
this.#withdrawProgressSuggestion(delegate.delegate_id);
|
|
684
|
+
this.#latestProgressSummary.delete(delegate.delegate_id);
|
|
685
|
+
this.#latestProgress.delete(delegate.delegate_id);
|
|
686
|
+
}
|
|
687
|
+
if (!record) {
|
|
688
|
+
if (claimed !== undefined
|
|
689
|
+
&& !this.#retainRoutingHistory
|
|
690
|
+
&& (definitive || this.#fencedDelegates.has(claimed.delegate_id))) {
|
|
691
|
+
this.#reclaimRouting(claimed.delegate_id);
|
|
692
|
+
}
|
|
693
|
+
return claimed;
|
|
694
|
+
}
|
|
695
|
+
const policy = this.memory.policies.get(event.payload.channel);
|
|
696
|
+
if (policy === undefined)
|
|
697
|
+
throw new Error(`missing policy for handoff: ${event.payload.channel}`);
|
|
698
|
+
const refs = [
|
|
699
|
+
event.payload.origin_ref,
|
|
700
|
+
...event.payload.refs.filter(reference => reference !== event.payload.origin_ref),
|
|
701
|
+
];
|
|
702
|
+
const appended = this.#appendMemory(event.payload.channel, {
|
|
703
|
+
ts: event.ts,
|
|
704
|
+
trust: event.payload.trust,
|
|
705
|
+
priority: policy.priority,
|
|
706
|
+
content: event.payload.content,
|
|
707
|
+
outcome: event.payload.outcome,
|
|
708
|
+
refs,
|
|
709
|
+
});
|
|
710
|
+
const explicitMiss = policy.suggest && event.payload.content.hit === false;
|
|
711
|
+
if (policy.suggest
|
|
712
|
+
&& event.payload.outcome === 'ok'
|
|
713
|
+
&& !explicitMiss
|
|
714
|
+
&& claimed?.executor === event.payload.channel
|
|
715
|
+
&& claimed.routing_class === 'ambient') {
|
|
716
|
+
this.suggestions.add({
|
|
717
|
+
origin: 'executor',
|
|
718
|
+
kind: 'notify',
|
|
719
|
+
content: event.payload.content,
|
|
720
|
+
evidence_refs: [memoryItemRef(appended)],
|
|
721
|
+
salience: policy.priority,
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
if (claimed !== undefined
|
|
725
|
+
&& !this.#retainRoutingHistory
|
|
726
|
+
&& (definitive || this.#fencedDelegates.has(claimed.delegate_id))) {
|
|
727
|
+
this.#reclaimRouting(claimed.delegate_id);
|
|
728
|
+
}
|
|
729
|
+
return claimed;
|
|
730
|
+
}
|
|
731
|
+
#applyDeadline(event, record = true) {
|
|
732
|
+
const delegate = this.#inFlight.get(event.payload.delegate_id);
|
|
733
|
+
if (delegate === undefined)
|
|
734
|
+
return undefined;
|
|
735
|
+
this.#clearObservationCondition(delegate.delegate_id);
|
|
736
|
+
this.#withdrawProgressSuggestion(delegate.delegate_id);
|
|
737
|
+
this.#latestProgressSummary.delete(delegate.delegate_id);
|
|
738
|
+
this.#latestProgress.delete(delegate.delegate_id);
|
|
739
|
+
if (record) {
|
|
740
|
+
const policy = this.memory.policies.get(delegate.executor);
|
|
741
|
+
const manifest = this.#manifests.get(delegate.executor);
|
|
742
|
+
const operation = manifest?.ops.find(candidate => candidate.name === delegate.op);
|
|
743
|
+
if (policy === undefined || operation === undefined) {
|
|
744
|
+
throw new Error(`missing bound executor metadata: ${delegate.executor}.${delegate.op}`);
|
|
745
|
+
}
|
|
746
|
+
const sensitive = new Set(operation.sensitive_params);
|
|
747
|
+
const request = Object.fromEntries(Object.entries(delegate.request).map(([key, value]) => [
|
|
748
|
+
key,
|
|
749
|
+
sensitive.has(key) ? '[REDACTED]' : value,
|
|
750
|
+
]));
|
|
751
|
+
this.#appendMemory(delegate.executor, {
|
|
752
|
+
ts: event.ts,
|
|
753
|
+
trust: 'trusted_system',
|
|
754
|
+
priority: policy.priority,
|
|
755
|
+
content: { error: 'deadline_exceeded', op: delegate.op, request },
|
|
756
|
+
outcome: 'unknown',
|
|
757
|
+
refs: [delegate.origin_ref],
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
this.#inFlight.delete(delegate.delegate_id);
|
|
761
|
+
this.#terminationKind.set(delegate.delegate_id, 'deadline');
|
|
762
|
+
this.#terminationOutcome.set(delegate.delegate_id, 'unknown');
|
|
763
|
+
return delegate;
|
|
764
|
+
}
|
|
765
|
+
#applyProgress(event) {
|
|
766
|
+
if (!validProgress(event.payload))
|
|
767
|
+
return undefined;
|
|
768
|
+
const delegate = this.#inFlight.get(event.payload.delegate_id);
|
|
769
|
+
if (delegate?.executor !== event.payload.channel || delegate.op !== event.payload.op) {
|
|
770
|
+
return undefined;
|
|
771
|
+
}
|
|
772
|
+
const policy = this.memory.policies.get(delegate.executor);
|
|
773
|
+
if (policy === undefined)
|
|
774
|
+
return undefined;
|
|
775
|
+
const previous = this.#latestProgress.get(delegate.delegate_id);
|
|
776
|
+
this.#latestProgress.set(delegate.delegate_id, event.payload);
|
|
777
|
+
// Node-only heartbeat coalescing: oracle fixtures currently contain no repeated phase/summary.
|
|
778
|
+
// Retain the latest payload (including counters) without adding rows or model wakes.
|
|
779
|
+
if (previous?.phase === event.payload.phase && previous.summary === event.payload.summary) {
|
|
780
|
+
return undefined;
|
|
781
|
+
}
|
|
782
|
+
const content = {
|
|
783
|
+
op: event.payload.op,
|
|
784
|
+
phase: event.payload.phase,
|
|
785
|
+
internal_activity: event.payload.internal_activity,
|
|
786
|
+
elapsed: event.payload.elapsed,
|
|
787
|
+
};
|
|
788
|
+
if (event.payload.summary !== null)
|
|
789
|
+
content.summary = event.payload.summary;
|
|
790
|
+
const appended = this.#appendMemory(delegate.executor, {
|
|
791
|
+
ts: event.ts,
|
|
792
|
+
trust: 'trusted_system',
|
|
793
|
+
priority: policy.priority,
|
|
794
|
+
content,
|
|
795
|
+
refs: [delegate.origin_ref],
|
|
796
|
+
});
|
|
797
|
+
// The summary comparison is Node-only: `Runtime._apply_progress` in the oracle builds a
|
|
798
|
+
// candidate for every working summary and `_wake_progress` wakes on every one of them. A
|
|
799
|
+
// Codex heartbeat repeats an unchanged summary for as long as a stage runs, and each repeat
|
|
800
|
+
// cost a Proactive selection that had already been declined. No committed fixture repeats a
|
|
801
|
+
// summary, so the oracle-generated expectations still agree; the first one that does will
|
|
802
|
+
// diverge here rather than in the fixture.
|
|
803
|
+
let proactiveCandidateCreated = false;
|
|
804
|
+
const coding = this.#manifests.get(delegate.executor)?.roles.includes('coding') === true;
|
|
805
|
+
const summary = coding ? codingProgressSummary(event.payload.summary) : event.payload.summary;
|
|
806
|
+
const previousSummary = this.#latestProgressSummary.get(delegate.delegate_id);
|
|
807
|
+
// Received history is route-independent. A mode switch withdraws delivery, not receipt:
|
|
808
|
+
// an unchanged heartbeat remains old even when its prior queued fact was never spoken.
|
|
809
|
+
if (coding && event.payload.phase === 'working' && summary !== null) {
|
|
810
|
+
this.#latestProgressSummary.set(delegate.delegate_id, summary);
|
|
811
|
+
}
|
|
812
|
+
if (this.codingProgressNarration.viaProactive(coding, policy.progress_via_surrogate)
|
|
813
|
+
&& event.payload.phase === 'working'
|
|
814
|
+
&& summary !== null
|
|
815
|
+
&& previousSummary !== summary) {
|
|
816
|
+
this.#latestProgressSummary.set(delegate.delegate_id, summary);
|
|
817
|
+
this.#withdrawProgressSuggestion(delegate.delegate_id);
|
|
818
|
+
const suggestion = this.suggestions.add({
|
|
819
|
+
origin: 'executor',
|
|
820
|
+
kind: 'notify',
|
|
821
|
+
content: {
|
|
822
|
+
summary: event.payload.summary,
|
|
823
|
+
previous_summary: previousSummary ?? null,
|
|
824
|
+
...(coding ? { work_order: delegate.request.work_order ?? null } : {}),
|
|
825
|
+
},
|
|
826
|
+
evidence_refs: [memoryItemRef(appended)],
|
|
827
|
+
salience: policy.priority,
|
|
828
|
+
expires_at: event.ts + DEFAULT_COOLDOWN,
|
|
829
|
+
delivery_policy: 'once',
|
|
830
|
+
});
|
|
831
|
+
this.#latestProgressSuggestion.set(delegate.delegate_id, suggestion.id);
|
|
832
|
+
proactiveCandidateCreated = true;
|
|
833
|
+
}
|
|
834
|
+
return { delegate, proactiveCandidateCreated };
|
|
835
|
+
}
|
|
836
|
+
#applyObservation(event) {
|
|
837
|
+
const delegate = this.#inFlight.get(event.payload.delegate_id);
|
|
838
|
+
if (delegate?.executor !== event.payload.channel
|
|
839
|
+
|| delegate.op !== event.payload.op
|
|
840
|
+
|| delegate.origin_ref !== event.payload.origin_ref)
|
|
841
|
+
return undefined;
|
|
842
|
+
const policy = this.memory.policies.get(delegate.executor);
|
|
843
|
+
if (policy === undefined)
|
|
844
|
+
return undefined;
|
|
845
|
+
const appended = this.#appendMemory(delegate.executor, {
|
|
846
|
+
ts: event.ts,
|
|
847
|
+
trust: event.payload.trust === 'trusted_user' ? 'trusted_system' : event.payload.trust,
|
|
848
|
+
priority: policy.priority,
|
|
849
|
+
content: event.payload.content,
|
|
850
|
+
refs: [
|
|
851
|
+
delegate.origin_ref,
|
|
852
|
+
...event.payload.refs.filter(reference => reference !== delegate.origin_ref),
|
|
853
|
+
],
|
|
854
|
+
});
|
|
855
|
+
const resetObserved = event.payload.content.hit === false || (event.payload.content.state === 'armed'
|
|
856
|
+
&& event.payload.content.reset_count === 0);
|
|
857
|
+
if (resetObserved)
|
|
858
|
+
this.#clearObservationCondition(delegate.delegate_id);
|
|
859
|
+
if (event.payload.content.hit === true && policy.suggest && delegate.routing_class === 'ambient') {
|
|
860
|
+
const previous = this.#latestObservationSuggestion.get(delegate.delegate_id);
|
|
861
|
+
if (previous !== undefined) {
|
|
862
|
+
const current = this.suggestions.get(previous);
|
|
863
|
+
if (current?.delivery_policy === 'while_condition_true' && current.condition_key !== null) {
|
|
864
|
+
const refreshed = this.suggestions.refreshCondition({
|
|
865
|
+
condition_key: current.condition_key,
|
|
866
|
+
now: event.ts,
|
|
867
|
+
evidence_ref: memoryItemRef(appended),
|
|
868
|
+
content: event.payload.content,
|
|
869
|
+
});
|
|
870
|
+
if (refreshed)
|
|
871
|
+
return { delegate, shouldWake: true };
|
|
872
|
+
if (current.status === 'pending' || current.status === 'fired') {
|
|
873
|
+
return { delegate, shouldWake: false };
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
this.suggestions.withdraw(previous);
|
|
877
|
+
this.#latestObservationSuggestion.delete(delegate.delegate_id);
|
|
878
|
+
}
|
|
879
|
+
const condition = event.payload.content.condition;
|
|
880
|
+
const repeatable = typeof condition === 'string' && condition !== '';
|
|
881
|
+
const suggestion = this.suggestions.add({
|
|
882
|
+
origin: 'executor',
|
|
883
|
+
kind: 'notify',
|
|
884
|
+
content: event.payload.content,
|
|
885
|
+
evidence_refs: [memoryItemRef(appended)],
|
|
886
|
+
salience: policy.priority,
|
|
887
|
+
...(repeatable ? {
|
|
888
|
+
delivery_policy: 'while_condition_true',
|
|
889
|
+
// Delegate identity is host-owned and binds every observation to one monitor condition.
|
|
890
|
+
// Hashing keeps an arbitrary IdFactory value within the public condition-key bound.
|
|
891
|
+
condition_key: `delegate:${createHash('sha256').update(delegate.delegate_id).digest('hex')}`,
|
|
892
|
+
} : {}),
|
|
893
|
+
});
|
|
894
|
+
this.#latestObservationSuggestion.set(delegate.delegate_id, suggestion.id);
|
|
895
|
+
return { delegate, shouldWake: true };
|
|
896
|
+
}
|
|
897
|
+
return { delegate, shouldWake: event.payload.content.hit === true };
|
|
898
|
+
}
|
|
899
|
+
#clearObservationCondition(delegateId) {
|
|
900
|
+
const suggestionId = this.#latestObservationSuggestion.get(delegateId);
|
|
901
|
+
if (suggestionId === undefined)
|
|
902
|
+
return;
|
|
903
|
+
const suggestion = this.suggestions.get(suggestionId);
|
|
904
|
+
if (suggestion?.delivery_policy === 'while_condition_true' && suggestion.condition_key !== null) {
|
|
905
|
+
this.suggestions.clearCondition(suggestion.condition_key);
|
|
906
|
+
}
|
|
907
|
+
else {
|
|
908
|
+
this.suggestions.withdraw(suggestionId);
|
|
909
|
+
}
|
|
910
|
+
this.#latestObservationSuggestion.delete(delegateId);
|
|
911
|
+
}
|
|
912
|
+
#resultTarget(delegate, kind, channel = delegate.executor) {
|
|
913
|
+
const policy = this.memory.policies.get(channel);
|
|
914
|
+
if (policy === undefined)
|
|
915
|
+
return null;
|
|
916
|
+
const slot = delegate.routing_class === 'user_awaited'
|
|
917
|
+
? 'fast'
|
|
918
|
+
: policy.wake === 'fast'
|
|
919
|
+
? 'fast'
|
|
920
|
+
: 'surrogate.watch';
|
|
921
|
+
return {
|
|
922
|
+
slot,
|
|
923
|
+
reason: wakeReasonSchema.parse({
|
|
924
|
+
kind,
|
|
925
|
+
priority: policy.priority,
|
|
926
|
+
routing_class: delegate.routing_class,
|
|
927
|
+
origin: delegate.delegate_id,
|
|
928
|
+
}),
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
#handoffTarget(event, delegate) {
|
|
932
|
+
const policy = this.memory.policies.get(event.payload.channel);
|
|
933
|
+
if (policy === undefined)
|
|
934
|
+
return null;
|
|
935
|
+
if ((delegate?.routing_class ?? 'ambient') === 'ambient'
|
|
936
|
+
&& policy.suggest
|
|
937
|
+
&& event.payload.content.hit === false)
|
|
938
|
+
return null;
|
|
939
|
+
if (delegate !== undefined) {
|
|
940
|
+
const target = this.#resultTarget(delegate, event.kind, event.payload.channel);
|
|
941
|
+
return target;
|
|
942
|
+
}
|
|
943
|
+
const slot = policy.wake === 'fast'
|
|
944
|
+
? 'fast'
|
|
945
|
+
: policy.wake === 'surrogate'
|
|
946
|
+
? 'surrogate.watch'
|
|
947
|
+
: null;
|
|
948
|
+
if (slot === null)
|
|
949
|
+
return null;
|
|
950
|
+
return {
|
|
951
|
+
slot,
|
|
952
|
+
reason: wakeReasonSchema.parse({
|
|
953
|
+
kind: event.kind,
|
|
954
|
+
priority: policy.priority,
|
|
955
|
+
routing_class: 'ambient',
|
|
956
|
+
origin: event.payload.delegate_id,
|
|
957
|
+
}),
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
#wake(slot, reason) {
|
|
961
|
+
if (!this.#wiredSlots.has(slot))
|
|
962
|
+
return;
|
|
963
|
+
if (slot === 'compress') {
|
|
964
|
+
// Compression owns a channel backlog; a second pending SlotSet wake can outlive pruned sources.
|
|
965
|
+
if (this.slots.inflight.compress)
|
|
966
|
+
return;
|
|
967
|
+
while (this.#compressBacklog.length > 0) {
|
|
968
|
+
const channel = this.#compressBacklog[0];
|
|
969
|
+
if ((this.memory.channels.get(channel)?.items.length ?? 0) > 0)
|
|
970
|
+
break;
|
|
971
|
+
this.#compressBacklog.shift();
|
|
972
|
+
this.#compressScheduled.delete(channel);
|
|
973
|
+
}
|
|
974
|
+
if (this.#compressBacklog.length === 0)
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
if (this.#onModelCall === undefined)
|
|
978
|
+
throw new Error(`model slot is not connected: ${slot}`);
|
|
979
|
+
this.slots.wake(slot, reason);
|
|
980
|
+
}
|
|
981
|
+
#spawnSlot(slot, reason) {
|
|
982
|
+
if (this.#onModelCall === undefined)
|
|
983
|
+
throw new Error(`model slot is not connected: ${slot}`);
|
|
984
|
+
const startedAt = this.appliedEvents.at(-1)?.ts ?? 0;
|
|
985
|
+
this.#jobSequence += 1;
|
|
986
|
+
const jobId = `job-${this.#jobSequence}`;
|
|
987
|
+
let selectedSuggestion = null;
|
|
988
|
+
let utteranceId = null;
|
|
989
|
+
let progressTrigger = null;
|
|
990
|
+
let compression = null;
|
|
991
|
+
if (slot === 'fast') {
|
|
992
|
+
this.#utteranceSequence += 1;
|
|
993
|
+
utteranceId = `u-${this.#utteranceSequence}`;
|
|
994
|
+
selectedSuggestion = this.#checkedSelection(reason.selected_suggestion, startedAt);
|
|
995
|
+
}
|
|
996
|
+
else if (slot === 'surrogate.watch') {
|
|
997
|
+
progressTrigger = this.#progressTriggerFor(reason, startedAt);
|
|
998
|
+
}
|
|
999
|
+
else {
|
|
1000
|
+
const channel = this.#compressBacklog.shift();
|
|
1001
|
+
if (channel === undefined)
|
|
1002
|
+
throw new Error('compression slot spawned without backlog');
|
|
1003
|
+
const memoryChannel = this.memory.channels.get(channel);
|
|
1004
|
+
if (memoryChannel === undefined)
|
|
1005
|
+
throw new Error(`unknown compression channel: ${channel}`);
|
|
1006
|
+
compression = {
|
|
1007
|
+
channel,
|
|
1008
|
+
throughSequence: memoryChannel.items.at(-1)?.seq ?? 0,
|
|
1009
|
+
retentionRevision: memoryChannel.retentionRevision,
|
|
1010
|
+
items: structuredClone(memoryChannel.items),
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
const offeredSuggestions = new Set(this.suggestions.all()
|
|
1014
|
+
.filter(suggestion => isSuggestionAvailable(suggestion, startedAt))
|
|
1015
|
+
.filter(suggestion => {
|
|
1016
|
+
if (slot !== 'surrogate.watch')
|
|
1017
|
+
return true;
|
|
1018
|
+
const latestProgress = new Set(this.#latestProgressSuggestion.values());
|
|
1019
|
+
return !latestProgress.has(suggestion.id) || suggestion.id === progressTrigger?.suggestionId;
|
|
1020
|
+
})
|
|
1021
|
+
.map(suggestion => suggestion.id));
|
|
1022
|
+
const job = {
|
|
1023
|
+
jobId,
|
|
1024
|
+
slot,
|
|
1025
|
+
reason,
|
|
1026
|
+
startedAt,
|
|
1027
|
+
seenUserSequence: this.#latestUserSequence(),
|
|
1028
|
+
visibleRefs: this.#visibleMemoryRefs(),
|
|
1029
|
+
offeredSuggestions,
|
|
1030
|
+
selectedSuggestion,
|
|
1031
|
+
utteranceId,
|
|
1032
|
+
progressTrigger,
|
|
1033
|
+
compression,
|
|
1034
|
+
};
|
|
1035
|
+
this.#jobs.set(jobId, job);
|
|
1036
|
+
const contextView = slot === 'compress'
|
|
1037
|
+
? undefined
|
|
1038
|
+
: compileContextView(this.memory, this.floor.state, startedAt, {
|
|
1039
|
+
inFlight: this.#currentConversationDelegates(),
|
|
1040
|
+
suggestions: this.suggestions.all().filter(suggestion => offeredSuggestions.has(suggestion.id)),
|
|
1041
|
+
manifests: [...this.#manifests.values()],
|
|
1042
|
+
selectedSuggestion,
|
|
1043
|
+
triggerKind: reason.kind,
|
|
1044
|
+
freshWindow: this.#freshWindow,
|
|
1045
|
+
});
|
|
1046
|
+
this.#onModelCall({
|
|
1047
|
+
job_id: jobId,
|
|
1048
|
+
slot,
|
|
1049
|
+
reason,
|
|
1050
|
+
started_at: startedAt,
|
|
1051
|
+
...(utteranceId === null ? {} : { utterance_id: utteranceId }),
|
|
1052
|
+
...(compression === null ? {} : { channel: compression.channel }),
|
|
1053
|
+
...(compression === null ? {} : { compression_items: compression.items }),
|
|
1054
|
+
...(contextView === undefined ? {} : { context_view: contextView }),
|
|
1055
|
+
});
|
|
1056
|
+
return jobId;
|
|
1057
|
+
}
|
|
1058
|
+
#applyModelDone(event) {
|
|
1059
|
+
const slot = slotSchema.parse(event.payload.slot);
|
|
1060
|
+
if (slot === 'compress')
|
|
1061
|
+
throw new Error(`compress job used model_done: ${event.payload.job_id}`);
|
|
1062
|
+
const job = this.#jobs.get(event.payload.job_id);
|
|
1063
|
+
if (job?.slot !== slot || !this.#results.has(event.payload.job_id)) {
|
|
1064
|
+
throw new Error(`unknown model completion: ${slot}/${event.payload.job_id}`);
|
|
1065
|
+
}
|
|
1066
|
+
this.slots.onDone(slot, job.jobId, () => {
|
|
1067
|
+
const output = this.#results.get(job.jobId);
|
|
1068
|
+
this.#results.delete(job.jobId);
|
|
1069
|
+
this.#jobs.delete(job.jobId);
|
|
1070
|
+
if (slot === 'surrogate.watch') {
|
|
1071
|
+
this.#consumeProactiveSelection(output, job);
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
const compensation = this.#consumeFastBrain(output, job.reason, event.seq, job);
|
|
1075
|
+
if (compensation !== null)
|
|
1076
|
+
this.#wake('fast', compensation);
|
|
1077
|
+
});
|
|
1078
|
+
}
|
|
1079
|
+
#consumeProactiveSelection(output, job) {
|
|
1080
|
+
const parsed = proactiveOutputSchema.safeParse(output);
|
|
1081
|
+
if (!parsed.success) {
|
|
1082
|
+
this.diagnostics.push({ code: 'invalid_surrogate_output' });
|
|
1083
|
+
this.#settleProgressTrigger(job.progressTrigger, null);
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
if (job.progressTrigger !== null && (parsed.data.progress_class === null)) {
|
|
1087
|
+
this.diagnostics.push({ code: 'invalid_surrogate_progress_decision' });
|
|
1088
|
+
this.#settleProgressTrigger(job.progressTrigger, null);
|
|
1089
|
+
return;
|
|
1090
|
+
}
|
|
1091
|
+
const selected = parsed.data.speak ? parsed.data.suggestion_id : null;
|
|
1092
|
+
const suggestion = selected === null ? undefined : this.suggestions.get(selected);
|
|
1093
|
+
const valid = suggestion !== undefined
|
|
1094
|
+
&& job.offeredSuggestions.has(suggestion.id)
|
|
1095
|
+
&& isSuggestionAvailable(suggestion, this.appliedEvents.at(-1)?.ts ?? job.startedAt)
|
|
1096
|
+
? suggestion
|
|
1097
|
+
: undefined;
|
|
1098
|
+
this.#settleProgressTrigger(job.progressTrigger, valid?.id ?? null);
|
|
1099
|
+
if (valid === undefined)
|
|
1100
|
+
return;
|
|
1101
|
+
const selectedReason = wakeReasonSchema.parse({
|
|
1102
|
+
kind: SELECTED_WAKE_KIND,
|
|
1103
|
+
priority: job.reason.priority,
|
|
1104
|
+
routing_class: job.reason.routing_class,
|
|
1105
|
+
origin: job.reason.origin,
|
|
1106
|
+
selected_suggestion: valid.id,
|
|
1107
|
+
});
|
|
1108
|
+
if (this.#wiredSlots.has('fast'))
|
|
1109
|
+
this.#wake('fast', selectedReason);
|
|
1110
|
+
else
|
|
1111
|
+
this.#suggestionSelectedObserver?.(valid, selectedReason);
|
|
1112
|
+
}
|
|
1113
|
+
#settleProgressTrigger(trigger, selectedId) {
|
|
1114
|
+
if (trigger === null)
|
|
1115
|
+
return;
|
|
1116
|
+
if (selectedId !== trigger.suggestionId)
|
|
1117
|
+
this.suggestions.withdraw(trigger.suggestionId);
|
|
1118
|
+
if (this.#latestProgressSuggestion.get(trigger.delegateId) === trigger.suggestionId) {
|
|
1119
|
+
this.#latestProgressSuggestion.delete(trigger.delegateId);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
#applyCompress(event) {
|
|
1123
|
+
this.#compressDeferred.delete(event.payload.channel);
|
|
1124
|
+
if (!this.#wiredSlots.has('compress') || this.#compressScheduled.has(event.payload.channel))
|
|
1125
|
+
return;
|
|
1126
|
+
const notBefore = this.#compressNotBefore.get(event.payload.channel) ?? event.ts;
|
|
1127
|
+
if (event.ts < notBefore) {
|
|
1128
|
+
this.#deferCompression(event.payload.channel, notBefore);
|
|
1129
|
+
return;
|
|
1130
|
+
}
|
|
1131
|
+
if (!this.memory.channels.has(event.payload.channel)) {
|
|
1132
|
+
throw new Error(`unknown compression channel: ${event.payload.channel}`);
|
|
1133
|
+
}
|
|
1134
|
+
this.#compressScheduled.add(event.payload.channel);
|
|
1135
|
+
this.#compressBacklog.push(event.payload.channel);
|
|
1136
|
+
}
|
|
1137
|
+
#applyCompressDone(event) {
|
|
1138
|
+
const job = this.#jobs.get(event.payload.job_id);
|
|
1139
|
+
if (job?.slot !== 'compress'
|
|
1140
|
+
|| job.compression?.channel !== event.payload.channel
|
|
1141
|
+
|| !this.#results.has(event.payload.job_id))
|
|
1142
|
+
throw new Error(`unknown compression completion: ${event.payload.job_id}`);
|
|
1143
|
+
this.slots.onDone('compress', job.jobId, () => {
|
|
1144
|
+
const output = this.#results.get(job.jobId);
|
|
1145
|
+
this.#results.delete(job.jobId);
|
|
1146
|
+
this.#jobs.delete(job.jobId);
|
|
1147
|
+
this.#compressScheduled.delete(event.payload.channel);
|
|
1148
|
+
// ponytail: full retained windows are still compressed. A per-channel 60s cooldown
|
|
1149
|
+
// bounds retries (including new appends); incremental compression is deliberately deferred.
|
|
1150
|
+
this.#compressNotBefore.set(event.payload.channel, event.ts + 60);
|
|
1151
|
+
const parsed = compressorOutputSchema.safeParse(output);
|
|
1152
|
+
const summary = parsed.success && parsed.data.channel === event.payload.channel
|
|
1153
|
+
? stripLikePython(parsed.data.summary)
|
|
1154
|
+
: '';
|
|
1155
|
+
if (!parsed.success || parsed.data.channel !== event.payload.channel) {
|
|
1156
|
+
this.diagnostics.push({ code: 'invalid_compressor_output' });
|
|
1157
|
+
}
|
|
1158
|
+
const channel = this.memory.channels.get(event.payload.channel);
|
|
1159
|
+
if (channel === undefined || job.compression === null)
|
|
1160
|
+
return;
|
|
1161
|
+
if (summary !== '') {
|
|
1162
|
+
const applied = channel.replaceSummary(summary, job.compression.throughSequence, job.compression.retentionRevision);
|
|
1163
|
+
if (!applied)
|
|
1164
|
+
this.diagnostics.push({ code: 'stale_compressor_output' });
|
|
1165
|
+
const policy = this.memory.policies.get(event.payload.channel);
|
|
1166
|
+
if (policy !== undefined && channel.uncompressed >= policy.compress_watermark) {
|
|
1167
|
+
this.#deferCompression(event.payload.channel, event.ts + 60);
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
});
|
|
1171
|
+
this.#continueCompression();
|
|
1172
|
+
}
|
|
1173
|
+
#deferCompression(channel, at) {
|
|
1174
|
+
if (this.#compressDeferred.has(channel))
|
|
1175
|
+
return;
|
|
1176
|
+
this.#compressDeferred.add(channel);
|
|
1177
|
+
this.post({ kind: 'compress', payload: { channel } }, at);
|
|
1178
|
+
}
|
|
1179
|
+
#continueCompression() {
|
|
1180
|
+
if (this.#compressBacklog.length === 0 || this.slots.inflight.compress)
|
|
1181
|
+
return;
|
|
1182
|
+
this.#wake('compress', wakeReasonSchema.parse({ kind: 'compress', priority: 0 }));
|
|
1183
|
+
}
|
|
1184
|
+
#appendMemory(channel, input) {
|
|
1185
|
+
const item = this.memory.append(channel, input);
|
|
1186
|
+
const target = this.memory.channels.get(channel);
|
|
1187
|
+
const policy = this.memory.policies.get(channel);
|
|
1188
|
+
if (target !== undefined
|
|
1189
|
+
&& policy !== undefined
|
|
1190
|
+
&& this.#wiredSlots.has('compress')
|
|
1191
|
+
&& target.uncompressed >= policy.compress_watermark
|
|
1192
|
+
&& !this.#compressScheduled.has(channel)) {
|
|
1193
|
+
const notBefore = this.#compressNotBefore.get(channel) ?? input.ts;
|
|
1194
|
+
if (input.ts < notBefore) {
|
|
1195
|
+
// Queue once even if no further input arrives after the cooldown.
|
|
1196
|
+
this.#deferCompression(channel, notBefore);
|
|
1197
|
+
}
|
|
1198
|
+
else {
|
|
1199
|
+
this.#compressScheduled.add(channel);
|
|
1200
|
+
this.#compressBacklog.push(channel);
|
|
1201
|
+
this.post({ kind: 'compress', payload: { channel } }, input.ts);
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
return item;
|
|
1205
|
+
}
|
|
1206
|
+
#withdrawProgressSuggestion(delegateId) {
|
|
1207
|
+
const suggestionId = this.#latestProgressSuggestion.get(delegateId);
|
|
1208
|
+
if (suggestionId === undefined)
|
|
1209
|
+
return;
|
|
1210
|
+
this.#latestProgressSuggestion.delete(delegateId);
|
|
1211
|
+
this.suggestions.withdraw(suggestionId);
|
|
1212
|
+
}
|
|
1213
|
+
#progressTriggerFor(reason, now) {
|
|
1214
|
+
if (reason.kind !== 'progress' || reason.origin === null)
|
|
1215
|
+
return null;
|
|
1216
|
+
const delegate = this.#inFlight.get(reason.origin);
|
|
1217
|
+
const suggestionId = this.#latestProgressSuggestion.get(reason.origin);
|
|
1218
|
+
const suggestion = suggestionId === undefined ? undefined : this.suggestions.get(suggestionId);
|
|
1219
|
+
if (suggestion !== undefined && !isSuggestionAvailable(suggestion, now)) {
|
|
1220
|
+
this.#latestProgressSuggestion.delete(reason.origin);
|
|
1221
|
+
return null;
|
|
1222
|
+
}
|
|
1223
|
+
if (delegate === undefined
|
|
1224
|
+
|| suggestion?.evidence_refs.length !== 1
|
|
1225
|
+
|| !suggestion.evidence_refs[0].startsWith(`${delegate.executor}:`))
|
|
1226
|
+
return null;
|
|
1227
|
+
return { suggestionId: suggestion.id, delegateId: delegate.delegate_id };
|
|
1228
|
+
}
|
|
1229
|
+
#consumeSpeech(speak, reason, eventSequence, job) {
|
|
1230
|
+
if (speak.act === 'none' || speak.text === '')
|
|
1231
|
+
return;
|
|
1232
|
+
const prepared = job === undefined ? undefined : this.#preparedSpeech.get(job.jobId);
|
|
1233
|
+
if (job !== undefined)
|
|
1234
|
+
this.#preparedSpeech.delete(job.jobId);
|
|
1235
|
+
const decision = prepared?.decision ?? this.floor.decide(reason.priority);
|
|
1236
|
+
if (prepared === undefined) {
|
|
1237
|
+
this.floorDecisions.push({ event_seq: eventSequence, priority: reason.priority, decision });
|
|
1238
|
+
}
|
|
1239
|
+
const now = this.appliedEvents.at(-1)?.ts ?? 0;
|
|
1240
|
+
const utteranceId = job?.utteranceId ?? `u-${eventSequence}`;
|
|
1241
|
+
if (decision === 'defer') {
|
|
1242
|
+
if (job?.selectedSuggestion === null || job === undefined) {
|
|
1243
|
+
this.suggestions.add({
|
|
1244
|
+
origin: 'fast_brain',
|
|
1245
|
+
kind: speak.act === 'ask' ? 'question' : 'notify',
|
|
1246
|
+
content: { text: speak.text, utterance_id: utteranceId },
|
|
1247
|
+
salience: reason.priority,
|
|
1248
|
+
});
|
|
1249
|
+
}
|
|
1250
|
+
return;
|
|
1251
|
+
}
|
|
1252
|
+
this.#appendMemory(CONVERSATION_CHANNEL, {
|
|
1253
|
+
ts: now,
|
|
1254
|
+
trust: 'trusted_system',
|
|
1255
|
+
priority: USER_PRIORITY,
|
|
1256
|
+
content: { text: speak.text, utterance_id: utteranceId },
|
|
1257
|
+
});
|
|
1258
|
+
if (job?.selectedSuggestion !== null && job?.selectedSuggestion !== undefined) {
|
|
1259
|
+
this.suggestions.fire(job.selectedSuggestion, now);
|
|
1260
|
+
}
|
|
1261
|
+
else if (speak.act === 'ask') {
|
|
1262
|
+
const asked = this.suggestions.add({
|
|
1263
|
+
origin: 'fast_brain',
|
|
1264
|
+
kind: 'question',
|
|
1265
|
+
content: { text: speak.text, utterance_id: utteranceId },
|
|
1266
|
+
salience: reason.priority,
|
|
1267
|
+
});
|
|
1268
|
+
this.suggestions.fire(asked.id, now);
|
|
1269
|
+
}
|
|
1270
|
+
}
|
|
1271
|
+
#prepareSpeech(job, output, at) {
|
|
1272
|
+
// A streaming port already arbitrated at its first text chunk and posted both Floor
|
|
1273
|
+
// events itself. Deciding again here would consult a Floor that has moved on and
|
|
1274
|
+
// would post a second speak_start for the same utterance.
|
|
1275
|
+
if (this.#preparedSpeech.has(job.jobId))
|
|
1276
|
+
return;
|
|
1277
|
+
const parsed = fastBrainOutputSchema.safeParse(output);
|
|
1278
|
+
if (!parsed.success || parsed.data.speak.act === 'none' || parsed.data.speak.text === '') {
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
const decision = this.floor.decide(job.reason.priority);
|
|
1282
|
+
this.#preparedSpeech.set(job.jobId, { decision });
|
|
1283
|
+
if (decision === 'defer' || job.utteranceId === null)
|
|
1284
|
+
return;
|
|
1285
|
+
this.post({
|
|
1286
|
+
kind: 'speak_start',
|
|
1287
|
+
payload: { utterance_id: job.utteranceId, priority: job.reason.priority },
|
|
1288
|
+
}, at);
|
|
1289
|
+
this.floor = this.floor.onSpeakStart(job.utteranceId, job.reason.priority);
|
|
1290
|
+
this.post({ kind: 'speak_end', payload: { utterance_id: job.utteranceId } }, at);
|
|
1291
|
+
}
|
|
1292
|
+
#checkedSelection(suggestionId, now) {
|
|
1293
|
+
if (suggestionId === null)
|
|
1294
|
+
return null;
|
|
1295
|
+
const suggestion = this.suggestions.get(suggestionId);
|
|
1296
|
+
return suggestion !== undefined && isSuggestionAvailable(suggestion, now)
|
|
1297
|
+
? suggestionId
|
|
1298
|
+
: null;
|
|
1299
|
+
}
|
|
1300
|
+
#supersedingUserInput(job) {
|
|
1301
|
+
return [...(this.memory.channels.get(CONVERSATION_CHANNEL)?.items ?? [])]
|
|
1302
|
+
.filter(item => item.seq > job.seenUserSequence && item.trust === 'trusted_user')
|
|
1303
|
+
.at(-1);
|
|
1304
|
+
}
|
|
1305
|
+
#dropStaleAction(action, reason, supersededBy) {
|
|
1306
|
+
if (action.act === 'none')
|
|
1307
|
+
return;
|
|
1308
|
+
const content = {
|
|
1309
|
+
error: 'action_superseded',
|
|
1310
|
+
act: action.act,
|
|
1311
|
+
by: memoryItemRef(supersededBy),
|
|
1312
|
+
};
|
|
1313
|
+
if (action.act === 'delegate') {
|
|
1314
|
+
content.executor = action.delegate.executor;
|
|
1315
|
+
content.op = action.delegate.op;
|
|
1316
|
+
}
|
|
1317
|
+
this.#appendMemory(CONVERSATION_CHANNEL, {
|
|
1318
|
+
ts: this.appliedEvents.at(-1)?.ts ?? 0,
|
|
1319
|
+
trust: 'trusted_system',
|
|
1320
|
+
priority: reason.priority,
|
|
1321
|
+
content,
|
|
1322
|
+
outcome: 'failed',
|
|
1323
|
+
refs: [memoryItemRef(supersededBy)],
|
|
1324
|
+
});
|
|
1325
|
+
}
|
|
1326
|
+
#latestUserSequence() {
|
|
1327
|
+
return [...(this.memory.channels.get(CONVERSATION_CHANNEL)?.items ?? [])]
|
|
1328
|
+
.filter(item => item.trust === 'trusted_user')
|
|
1329
|
+
.at(-1)?.seq ?? 0;
|
|
1330
|
+
}
|
|
1331
|
+
#visibleMemoryRefs() {
|
|
1332
|
+
return new Set([...this.memory.channels.values()]
|
|
1333
|
+
.flatMap(channel => channel.items.slice(-RECENT_LIMIT).map(item => memoryItemRef(item))));
|
|
1334
|
+
}
|
|
1335
|
+
/**
|
|
1336
|
+
* Reject every action when one call returned more than one.
|
|
1337
|
+
*
|
|
1338
|
+
* Executing the first and rejecting the rest would produce the ambiguous failure "one
|
|
1339
|
+
* of your two requests was handled; guess which". Executing none and telling the model
|
|
1340
|
+
* lets the next call dispatch one, consume its handoff, and dispatch the second through
|
|
1341
|
+
* a fully traceable path. Last-write-wins previously discarded the earlier action
|
|
1342
|
+
* without a trace.
|
|
1343
|
+
*/
|
|
1344
|
+
#rejectMultipleActions(act, extra, reason) {
|
|
1345
|
+
return this.#refuse({ error: 'multiple_actions', count: extra + 1, act }, reason);
|
|
1346
|
+
}
|
|
1347
|
+
/** Record malformed model structure and use the same one-hop compensation brake. */
|
|
1348
|
+
#rejectContract(failures, reason) {
|
|
1349
|
+
const first = failures[0];
|
|
1350
|
+
const content = {
|
|
1351
|
+
error: 'model_contract_failure',
|
|
1352
|
+
code: first.code,
|
|
1353
|
+
tool_name: first.tool_name,
|
|
1354
|
+
};
|
|
1355
|
+
if (failures.length > 1)
|
|
1356
|
+
content.count = failures.length;
|
|
1357
|
+
return this.#refuse(content, reason);
|
|
1358
|
+
}
|
|
1359
|
+
#refuseDelegate(request, problem, reason) {
|
|
1360
|
+
const wake = this.#refuse({
|
|
1361
|
+
error: 'delegate_rejected',
|
|
1362
|
+
problem,
|
|
1363
|
+
executor: boundedModelText(request.executor),
|
|
1364
|
+
op: boundedModelText(request.op),
|
|
1365
|
+
origin_ref: boundedModelText(request.origin_ref),
|
|
1366
|
+
}, reason);
|
|
1367
|
+
return { accepted: false, delegate_id: null, problem, wake };
|
|
1368
|
+
}
|
|
1369
|
+
#reclaimRouting(delegateId) {
|
|
1370
|
+
this.#routableDelegates.delete(delegateId);
|
|
1371
|
+
this.#terminationKind.delete(delegateId);
|
|
1372
|
+
this.#terminationOutcome.delete(delegateId);
|
|
1373
|
+
this.#handoffSeen.delete(delegateId);
|
|
1374
|
+
this.#fencedDelegates.delete(delegateId);
|
|
1375
|
+
}
|
|
1376
|
+
#refuse(content, reason) {
|
|
1377
|
+
this.#appendMemory(CONVERSATION_CHANNEL, {
|
|
1378
|
+
ts: this.appliedEvents.at(-1)?.ts ?? 0,
|
|
1379
|
+
trust: 'trusted_system',
|
|
1380
|
+
priority: reason.priority,
|
|
1381
|
+
content,
|
|
1382
|
+
outcome: 'failed',
|
|
1383
|
+
});
|
|
1384
|
+
if (reason.kind === REJECTED_WAKE_KIND)
|
|
1385
|
+
return null;
|
|
1386
|
+
return wakeReasonSchema.parse({
|
|
1387
|
+
kind: REJECTED_WAKE_KIND,
|
|
1388
|
+
priority: reason.priority,
|
|
1389
|
+
routing_class: reason.routing_class,
|
|
1390
|
+
});
|
|
1391
|
+
}
|
|
1392
|
+
#memoryItemExists(reference) {
|
|
1393
|
+
try {
|
|
1394
|
+
const [channel, sequence] = parseMemoryRef(reference);
|
|
1395
|
+
return this.memory.channels.get(channel)?.getBySeq(sequence) !== undefined;
|
|
1396
|
+
}
|
|
1397
|
+
catch {
|
|
1398
|
+
return false;
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
#originProblem(reference, visibleRefs, grant) {
|
|
1402
|
+
try {
|
|
1403
|
+
parseMemoryRef(reference);
|
|
1404
|
+
}
|
|
1405
|
+
catch {
|
|
1406
|
+
return 'invalid_origin_ref';
|
|
1407
|
+
}
|
|
1408
|
+
if (!this.#memoryItemExists(reference))
|
|
1409
|
+
return 'origin_not_found';
|
|
1410
|
+
const [channel, seq] = parseMemoryRef(reference);
|
|
1411
|
+
if (this.memory.isHistorical({ channel, seq })) {
|
|
1412
|
+
const task = grant ? taskGrantService(grant).get(grant.fence.task_id) : undefined;
|
|
1413
|
+
if (!task || grant?.origin_ref !== reference || task.conversation_generation === undefined || this.memory.scope.conversation_id !== task.conversation_id + ':' + task.conversation_generation)
|
|
1414
|
+
return 'historical_origin';
|
|
1415
|
+
}
|
|
1416
|
+
if (visibleRefs !== undefined && !visibleRefs.has(reference))
|
|
1417
|
+
return 'origin_not_visible';
|
|
1418
|
+
return null;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
function memoryItemRef(item) {
|
|
1422
|
+
return `${item.channel}:${item.seq}`;
|
|
1423
|
+
}
|
|
1424
|
+
function sameDelegateRequest(left, right) {
|
|
1425
|
+
return left.executor === right.executor
|
|
1426
|
+
&& left.op === right.op
|
|
1427
|
+
&& canonicalJson(left.request) === canonicalJson(right.request);
|
|
1428
|
+
}
|
|
1429
|
+
function boundedModelText(value) {
|
|
1430
|
+
return [...value].slice(0, 128).join('');
|
|
1431
|
+
}
|
|
1432
|
+
function refusalDelegateRequest(value) {
|
|
1433
|
+
const record = typeof value === 'object' && value !== null
|
|
1434
|
+
? value
|
|
1435
|
+
: {};
|
|
1436
|
+
return {
|
|
1437
|
+
executor: typeof record.executor === 'string' ? record.executor : 'invalid',
|
|
1438
|
+
op: typeof record.op === 'string' ? record.op : 'invalid',
|
|
1439
|
+
request: {},
|
|
1440
|
+
origin_ref: typeof record.origin_ref === 'string' ? record.origin_ref : 'invalid',
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
function executorTrust(trust) {
|
|
1444
|
+
return trust === 'untrusted_external' ? trust : 'trusted_system';
|
|
1445
|
+
}
|
|
1446
|
+
function validProgress(payload) {
|
|
1447
|
+
if (payload.phase !== 'started' && payload.phase !== 'working')
|
|
1448
|
+
return false;
|
|
1449
|
+
if (payload.phase === 'started') {
|
|
1450
|
+
return payload.internal_activity === 0 && validProgressSummary(payload.summary, payload.phase);
|
|
1451
|
+
}
|
|
1452
|
+
if (payload.internal_activity < 1 || payload.internal_activity > 1_048_576)
|
|
1453
|
+
return false;
|
|
1454
|
+
return validProgressSummary(payload.summary, payload.phase);
|
|
1455
|
+
}
|
|
1456
|
+
function exhaustiveEvent(value) {
|
|
1457
|
+
throw new Error(`unhandled event: ${String(value)}`);
|
|
1458
|
+
}
|
|
1459
|
+
//# sourceMappingURL=runtime.js.map
|