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,650 @@
|
|
|
1
|
+
import { taskGrantService } from '../personal-agent/tasks.js';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { CONVERSATION_CHANNEL } from './memory.js';
|
|
4
|
+
import { BlackboardSession } from '../memory/blackboard-session.js';
|
|
5
|
+
import { bindHostExecutorCapability } from '../executors/host-executor-capability.js';
|
|
6
|
+
import { CoreRuntime, } from './runtime.js';
|
|
7
|
+
import { SLOTS } from './slots.js';
|
|
8
|
+
const DEFAULT_SHUTDOWN_GRACE = 1;
|
|
9
|
+
/** Applied events between forced event-loop turns while the queue stays ready. */
|
|
10
|
+
const DRAIN_YIELD_INTERVAL = 64;
|
|
11
|
+
function yieldToEventLoop() {
|
|
12
|
+
return new Promise(resolve => { setImmediate(resolve); });
|
|
13
|
+
}
|
|
14
|
+
export class CausalRuntime {
|
|
15
|
+
core;
|
|
16
|
+
#clock;
|
|
17
|
+
#models;
|
|
18
|
+
#executors = new Map();
|
|
19
|
+
#tasks = new Set();
|
|
20
|
+
#observers = new Set();
|
|
21
|
+
#pendingUserInputs = new Map();
|
|
22
|
+
#hostExecutorCapabilities = new Map();
|
|
23
|
+
#userTurns = new Map();
|
|
24
|
+
#instructionReceipts = new Map();
|
|
25
|
+
#taskBindings = new Map();
|
|
26
|
+
#taskGrants = new Map();
|
|
27
|
+
#launchChecks = new Map();
|
|
28
|
+
#shutdownGrace;
|
|
29
|
+
#state = 'new';
|
|
30
|
+
#acceptCompletions = true;
|
|
31
|
+
#failure;
|
|
32
|
+
#workVersion = 0;
|
|
33
|
+
#workWaiter;
|
|
34
|
+
#blackboard;
|
|
35
|
+
#memoryReady;
|
|
36
|
+
#memoryOpened = false;
|
|
37
|
+
#memoryClosed = false;
|
|
38
|
+
#serving;
|
|
39
|
+
#maintenance;
|
|
40
|
+
#clearing;
|
|
41
|
+
#applying;
|
|
42
|
+
get codingProgressNarration() { return this.core.codingProgressNarration; }
|
|
43
|
+
constructor(options) {
|
|
44
|
+
this.#clock = options.clock;
|
|
45
|
+
this.#models = { ...options.models };
|
|
46
|
+
this.#shutdownGrace = options.shutdownGrace ?? DEFAULT_SHUTDOWN_GRACE;
|
|
47
|
+
if (!Number.isFinite(this.#shutdownGrace) || this.#shutdownGrace < 0) {
|
|
48
|
+
throw new RangeError('shutdown grace must be a non-negative finite number');
|
|
49
|
+
}
|
|
50
|
+
for (const adapter of options.executors ?? []) {
|
|
51
|
+
if (this.#executors.has(adapter.manifest.name)) {
|
|
52
|
+
throw new Error(`duplicate executor adapter: ${adapter.manifest.name}`);
|
|
53
|
+
}
|
|
54
|
+
this.#executors.set(adapter.manifest.name, adapter);
|
|
55
|
+
}
|
|
56
|
+
const modelSlots = SLOTS.filter(slot => this.#models[slot] !== undefined);
|
|
57
|
+
this.core = new CoreRuntime({
|
|
58
|
+
manifests: [...this.#executors.values()].map(adapter => adapter.manifest),
|
|
59
|
+
...(options.conversationId !== undefined ? { conversationId: options.conversationId }
|
|
60
|
+
: options.blackboard === undefined ? {}
|
|
61
|
+
: { conversationId: createHash('sha256').update(options.blackboard.ownerId).digest('hex') }),
|
|
62
|
+
ids: options.ids,
|
|
63
|
+
modelSlots,
|
|
64
|
+
...(options.codingProgressNarration === undefined ? {} : { codingProgressNarration: options.codingProgressNarration }),
|
|
65
|
+
...(options.retainRoutingHistory === undefined
|
|
66
|
+
? {}
|
|
67
|
+
: { retainRoutingHistory: options.retainRoutingHistory }),
|
|
68
|
+
...(options.suggestionCooldown === undefined
|
|
69
|
+
? {}
|
|
70
|
+
: { suggestionCooldown: options.suggestionCooldown }),
|
|
71
|
+
...(options.freshWindow === undefined ? {} : { freshWindow: options.freshWindow }),
|
|
72
|
+
onModelCall: call => this.#startModelCall(call),
|
|
73
|
+
onExecutorDispatch: (dispatchIndex, delegate) => {
|
|
74
|
+
this.#startExecutorDispatch(dispatchIndex, delegate);
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
this.#blackboard = options.blackboard === undefined ? undefined : new BlackboardSession(this.memory, options.blackboard);
|
|
78
|
+
}
|
|
79
|
+
get hasPersistentMemory() { return this.#blackboard !== undefined; }
|
|
80
|
+
openMemory() {
|
|
81
|
+
if (this.#memoryClosed)
|
|
82
|
+
return Promise.reject(new Error('causal runtime memory is closed'));
|
|
83
|
+
this.#memoryReady ??= (this.#blackboard?.open() ?? Promise.resolve()).then(() => { if (!this.#memoryClosed)
|
|
84
|
+
this.#memoryOpened = true; });
|
|
85
|
+
return this.#memoryReady;
|
|
86
|
+
}
|
|
87
|
+
/** Also used by the host intake writer, which does not enter through the reducer. */
|
|
88
|
+
async flushMemory(maintenance = false) {
|
|
89
|
+
if (this.#clearing !== undefined)
|
|
90
|
+
await this.#clearing;
|
|
91
|
+
if (this.#blackboard === undefined)
|
|
92
|
+
return;
|
|
93
|
+
try {
|
|
94
|
+
await this.#blackboard.flush(maintenance);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
this.#failure ??= runtimeError(error);
|
|
98
|
+
this.#notifyWork();
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/** Host must fence provider input, playback and projections around this transition. */
|
|
103
|
+
clearConversation() {
|
|
104
|
+
if (this.#clearing !== undefined)
|
|
105
|
+
return this.#clearing;
|
|
106
|
+
if (this.#state === 'closed' || this.#memoryClosed || !this.#memoryOpened) {
|
|
107
|
+
return Promise.reject(new Error('causal runtime memory is not ready'));
|
|
108
|
+
}
|
|
109
|
+
if (this.#failure !== undefined)
|
|
110
|
+
return Promise.reject(this.#failure);
|
|
111
|
+
const applying = this.#applying;
|
|
112
|
+
// Set the gate before aborting models: abort listeners can re-enter host callbacks synchronously.
|
|
113
|
+
this.#clearing = Promise.resolve().then(async () => {
|
|
114
|
+
try {
|
|
115
|
+
await applying;
|
|
116
|
+
if (this.#blackboard === undefined)
|
|
117
|
+
this.memory.clear();
|
|
118
|
+
else
|
|
119
|
+
await this.#blackboard.clear();
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
this.#failure ??= runtimeError(error);
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
finally {
|
|
126
|
+
this.#clearing = undefined;
|
|
127
|
+
this.#notifyWork();
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
this.core.resetConversationState();
|
|
131
|
+
for (const task of this.#tasks)
|
|
132
|
+
if (task.kind === 'model')
|
|
133
|
+
task.controller.abort();
|
|
134
|
+
for (const pending of this.#pendingUserInputs.values())
|
|
135
|
+
pending.reject(new Error('conversation cleared before input was acknowledged'));
|
|
136
|
+
this.#pendingUserInputs.clear();
|
|
137
|
+
// Existing executor work keeps its lifecycle; unused one-shot authority does not survive clear.
|
|
138
|
+
this.#hostExecutorCapabilities.clear();
|
|
139
|
+
this.#userTurns.clear();
|
|
140
|
+
this.#launchChecks.clear();
|
|
141
|
+
this.#notifyWork();
|
|
142
|
+
return this.#clearing;
|
|
143
|
+
}
|
|
144
|
+
post(input, at = this.#clock.now()) {
|
|
145
|
+
if (this.#state === 'closed' || this.#memoryClosed)
|
|
146
|
+
throw new Error('causal runtime is closed');
|
|
147
|
+
if (this.#failure !== undefined)
|
|
148
|
+
throw this.#failure;
|
|
149
|
+
if (this.#clearing !== undefined)
|
|
150
|
+
throw new Error('conversation is clearing');
|
|
151
|
+
const event = this.core.post(input, at);
|
|
152
|
+
this.#notifyWork();
|
|
153
|
+
return structuredClone(event);
|
|
154
|
+
}
|
|
155
|
+
ingestUserInput(input) {
|
|
156
|
+
if (this.#state === 'closed' || this.#memoryClosed)
|
|
157
|
+
return Promise.reject(new Error('causal runtime is closed'));
|
|
158
|
+
if (this.#failure !== undefined)
|
|
159
|
+
return Promise.reject(this.#failure);
|
|
160
|
+
if (this.#clearing !== undefined)
|
|
161
|
+
return Promise.reject(new Error('conversation is clearing'));
|
|
162
|
+
const event = this.core.post({
|
|
163
|
+
kind: 'user_input',
|
|
164
|
+
payload: input.media_refs === undefined
|
|
165
|
+
? { text: input.text }
|
|
166
|
+
: { text: input.text, media_refs: [...input.media_refs] },
|
|
167
|
+
}, this.#clock.now());
|
|
168
|
+
const applied = new Promise((resolve, reject) => {
|
|
169
|
+
this.#pendingUserInputs.set(event.seq, { resolve, reject });
|
|
170
|
+
});
|
|
171
|
+
this.#notifyWork();
|
|
172
|
+
return applied;
|
|
173
|
+
}
|
|
174
|
+
/** The clock the runtime schedules against, so a caller measures the same time it does. */
|
|
175
|
+
get clock() {
|
|
176
|
+
return this.#clock;
|
|
177
|
+
}
|
|
178
|
+
/** The executor adapters, by manifest name. Read-only: wiring happens at construction. */
|
|
179
|
+
get executors() {
|
|
180
|
+
return this.#executors;
|
|
181
|
+
}
|
|
182
|
+
/** The blackboard. Recall projects from it, so it has to be the same instance the reducer writes. */
|
|
183
|
+
get memory() {
|
|
184
|
+
return this.core.memory;
|
|
185
|
+
}
|
|
186
|
+
/** Admit and durably publish before the adapter starts on the next event-loop turn.
|
|
187
|
+
* Callers install correlation immediately after awaiting admission, before any further I/O.
|
|
188
|
+
*/
|
|
189
|
+
async dispatchExternal(request, reason, userTurn, stillWanted) {
|
|
190
|
+
if (this.#state === 'closed' || this.#memoryClosed)
|
|
191
|
+
return { accepted: false, delegate_id: null, problem: 'closed' };
|
|
192
|
+
if (this.#clearing !== undefined)
|
|
193
|
+
return { accepted: false, delegate_id: null, problem: 'conversation_clearing' };
|
|
194
|
+
if (this.#blackboard !== undefined && !this.#memoryOpened)
|
|
195
|
+
return { accepted: false, delegate_id: null, problem: 'memory_not_ready' };
|
|
196
|
+
if (this.#failure !== undefined)
|
|
197
|
+
throw this.#failure;
|
|
198
|
+
const epoch = this.core.conversationEpoch;
|
|
199
|
+
const admission = this.core.dispatchExternal(request, reason);
|
|
200
|
+
if (admission.accepted) {
|
|
201
|
+
if (admission.delegate_id !== null && userTurn !== undefined)
|
|
202
|
+
this.#userTurns.set(admission.delegate_id, userTurn);
|
|
203
|
+
if (admission.delegate_id !== null && stillWanted !== undefined)
|
|
204
|
+
this.#launchChecks.set(admission.delegate_id, stillWanted);
|
|
205
|
+
this.#notifyWork();
|
|
206
|
+
}
|
|
207
|
+
await this.flushMemory();
|
|
208
|
+
if (epoch !== this.core.conversationEpoch)
|
|
209
|
+
return { accepted: false, delegate_id: null, problem: 'conversation_cleared' };
|
|
210
|
+
return admission;
|
|
211
|
+
}
|
|
212
|
+
async dispatchTaskExternal(request, reason, grant, receipt, stillWanted) {
|
|
213
|
+
const tasks = taskGrantService(grant);
|
|
214
|
+
if (this.#failure !== undefined)
|
|
215
|
+
throw this.#failure;
|
|
216
|
+
if (this.#blackboard !== undefined && !this.#memoryOpened)
|
|
217
|
+
return { accepted: false, delegate_id: null, problem: 'memory_not_ready' };
|
|
218
|
+
if (this.#state === 'closed' || this.#memoryClosed || this.#clearing !== undefined)
|
|
219
|
+
return { accepted: false, delegate_id: null, problem: 'closed' };
|
|
220
|
+
const admission = this.core.dispatchTaskExternal(request, reason, grant);
|
|
221
|
+
if (admission.accepted && admission.delegate_id) {
|
|
222
|
+
const work = admission.delegate_id;
|
|
223
|
+
this.#taskGrants.set(work, grant);
|
|
224
|
+
this.#launchChecks.set(work, () => grant.stillWanted() && (stillWanted?.() ?? true));
|
|
225
|
+
if (receipt)
|
|
226
|
+
this.#instructionReceipts.set(work, receipt);
|
|
227
|
+
const binding = tasks.bindWork(grant.fence, work, undefined, request.op !== 'steer');
|
|
228
|
+
this.#taskBindings.set(work, binding);
|
|
229
|
+
this.#notifyWork();
|
|
230
|
+
await binding;
|
|
231
|
+
}
|
|
232
|
+
await this.flushMemory();
|
|
233
|
+
return admission;
|
|
234
|
+
}
|
|
235
|
+
cancelWork(delegateId) {
|
|
236
|
+
this.cancelPendingDispatch(delegateId);
|
|
237
|
+
const delegate = this.inFlightDelegate(delegateId);
|
|
238
|
+
if (delegate)
|
|
239
|
+
this.#executors.get(delegate.executor)?.cancelWork?.(delegateId);
|
|
240
|
+
}
|
|
241
|
+
cancelPendingDispatch(delegateId) {
|
|
242
|
+
if (!this.#launchChecks.has(delegateId))
|
|
243
|
+
return false;
|
|
244
|
+
this.#launchChecks.set(delegateId, null);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
/** Admit and privately carry the exact one-shot confirmed project capability. */
|
|
248
|
+
async dispatchConfirmedExternal(request, reason, capability, launchAuthorized, taskContext) {
|
|
249
|
+
if (this.#state === 'closed' || this.#memoryClosed)
|
|
250
|
+
return { accepted: false, delegate_id: null, problem: 'closed' };
|
|
251
|
+
if (this.#clearing !== undefined)
|
|
252
|
+
return { accepted: false, delegate_id: null, problem: 'conversation_clearing' };
|
|
253
|
+
if (this.#blackboard !== undefined && !this.#memoryOpened)
|
|
254
|
+
return { accepted: false, delegate_id: null, problem: 'memory_not_ready' };
|
|
255
|
+
if (this.#failure !== undefined)
|
|
256
|
+
throw this.#failure;
|
|
257
|
+
const epoch = this.core.conversationEpoch;
|
|
258
|
+
if (taskContext) {
|
|
259
|
+
taskGrantService(taskContext);
|
|
260
|
+
if (taskContext.origin_ref !== request.origin_ref)
|
|
261
|
+
throw Error('invalid_origin_ref');
|
|
262
|
+
}
|
|
263
|
+
const admission = this.core.dispatchConfirmedExternal(request, reason, capability, taskContext);
|
|
264
|
+
if (admission.accepted) {
|
|
265
|
+
if (admission.delegate_id !== null) {
|
|
266
|
+
if (taskContext) {
|
|
267
|
+
this.#taskGrants.set(admission.delegate_id, taskContext);
|
|
268
|
+
const binding = taskGrantService(taskContext).bindWork(taskContext.fence, admission.delegate_id, undefined, request.op !== 'steer');
|
|
269
|
+
this.#taskBindings.set(admission.delegate_id, binding);
|
|
270
|
+
void binding.catch(() => { });
|
|
271
|
+
}
|
|
272
|
+
this.#hostExecutorCapabilities.set(admission.delegate_id, capability);
|
|
273
|
+
this.#launchChecks.set(admission.delegate_id, launchAuthorized);
|
|
274
|
+
}
|
|
275
|
+
this.#notifyWork();
|
|
276
|
+
}
|
|
277
|
+
await this.flushMemory();
|
|
278
|
+
if (epoch !== this.core.conversationEpoch)
|
|
279
|
+
return { accepted: false, delegate_id: null, problem: 'conversation_cleared' };
|
|
280
|
+
return admission;
|
|
281
|
+
}
|
|
282
|
+
/** The delegate a handoff claimed, for an observer projecting that exact event. */
|
|
283
|
+
claimedHandoff(seq) {
|
|
284
|
+
return this.core.claimedHandoff(seq);
|
|
285
|
+
}
|
|
286
|
+
/** Whether this exact deadline terminated its delegate. */
|
|
287
|
+
terminatedByDeadline(seq, delegateId) {
|
|
288
|
+
return this.core.terminatedByDeadline(seq, delegateId);
|
|
289
|
+
}
|
|
290
|
+
/** The delegate from either table, whether or not it is still in flight. */
|
|
291
|
+
delegateFor(delegateId) {
|
|
292
|
+
return this.core.delegateFor(delegateId);
|
|
293
|
+
}
|
|
294
|
+
/** The delegate only if it is still in flight. */
|
|
295
|
+
inFlightDelegate(delegateId) {
|
|
296
|
+
return this.core.inFlightDelegate(delegateId);
|
|
297
|
+
}
|
|
298
|
+
observe(observer) {
|
|
299
|
+
this.#observers.add(observer);
|
|
300
|
+
return () => this.#observers.delete(observer);
|
|
301
|
+
}
|
|
302
|
+
bindSuggestionSelected(observer) {
|
|
303
|
+
return this.core.bindSuggestionSelected(observer);
|
|
304
|
+
}
|
|
305
|
+
suggestionFor(suggestionId) {
|
|
306
|
+
return this.core.suggestions.get(suggestionId) ?? null;
|
|
307
|
+
}
|
|
308
|
+
confirmSuggestionSpoken(suggestionId) {
|
|
309
|
+
this.core.suggestions.fire(suggestionId, this.#clock.now());
|
|
310
|
+
}
|
|
311
|
+
serve(signal) {
|
|
312
|
+
const serving = this.#serve(signal);
|
|
313
|
+
this.#serving ??= serving;
|
|
314
|
+
return serving;
|
|
315
|
+
}
|
|
316
|
+
/** Called after service stop has aborted serving; persistence has its own bounded RPC drain. */
|
|
317
|
+
async closeMemory() {
|
|
318
|
+
if (this.#blackboard === undefined)
|
|
319
|
+
return;
|
|
320
|
+
this.#memoryClosed = true;
|
|
321
|
+
this.#memoryOpened = false;
|
|
322
|
+
try {
|
|
323
|
+
await this.#serving;
|
|
324
|
+
await this.#memoryReady;
|
|
325
|
+
}
|
|
326
|
+
finally {
|
|
327
|
+
await this.#blackboard.close();
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
async #serve(signal) {
|
|
331
|
+
if (this.#state !== 'new')
|
|
332
|
+
throw new Error('causal runtime can only be served once');
|
|
333
|
+
this.#state = 'serving';
|
|
334
|
+
const onAbort = () => this.#notifyWork();
|
|
335
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
336
|
+
let sinceYield = 0;
|
|
337
|
+
try {
|
|
338
|
+
await this.openMemory();
|
|
339
|
+
if (this.#blackboard !== undefined) {
|
|
340
|
+
this.#maintenance = setInterval(() => {
|
|
341
|
+
void this.flushMemory(true).catch(() => { });
|
|
342
|
+
}, 60_000);
|
|
343
|
+
this.#maintenance.unref();
|
|
344
|
+
}
|
|
345
|
+
while (!signal.aborted) {
|
|
346
|
+
// A microtask yield lets owned task completions re-enter the queue, but it
|
|
347
|
+
// never returns control to the macrotask queue. An event queue that stays
|
|
348
|
+
// ready would therefore starve socket reads and timers for as long as it
|
|
349
|
+
// keeps producing work, so punctuate a long drain with a real event-loop
|
|
350
|
+
// turn.
|
|
351
|
+
if (sinceYield >= DRAIN_YIELD_INTERVAL) {
|
|
352
|
+
sinceYield = 0;
|
|
353
|
+
await yieldToEventLoop();
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
await Promise.resolve();
|
|
357
|
+
}
|
|
358
|
+
if (this.#failure !== undefined)
|
|
359
|
+
throw this.#failure;
|
|
360
|
+
if (this.#clearing !== undefined) {
|
|
361
|
+
await this.#clearing;
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
const event = this.core.queue.popReady(this.#clock.now());
|
|
365
|
+
if (event !== undefined) {
|
|
366
|
+
sinceYield += 1;
|
|
367
|
+
this.#applying = this.#applyEvent(event);
|
|
368
|
+
try {
|
|
369
|
+
await this.#applying;
|
|
370
|
+
}
|
|
371
|
+
finally {
|
|
372
|
+
this.#applying = undefined;
|
|
373
|
+
}
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
sinceYield = 0;
|
|
377
|
+
await this.#waitForWork(signal);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
finally {
|
|
381
|
+
signal.removeEventListener('abort', onAbort);
|
|
382
|
+
clearInterval(this.#maintenance);
|
|
383
|
+
this.#state = 'closed';
|
|
384
|
+
for (const receipt of this.#instructionReceipts.values())
|
|
385
|
+
receipt('unknown');
|
|
386
|
+
this.#instructionReceipts.clear();
|
|
387
|
+
this.#taskBindings.clear();
|
|
388
|
+
this.#taskGrants.clear();
|
|
389
|
+
this.#acceptCompletions = false;
|
|
390
|
+
const stopped = new Error('causal runtime stopped before input was applied');
|
|
391
|
+
for (const pending of this.#pendingUserInputs.values())
|
|
392
|
+
pending.reject(this.#failure ?? stopped);
|
|
393
|
+
this.#pendingUserInputs.clear();
|
|
394
|
+
await this.#shutdownTasks();
|
|
395
|
+
this.#hostExecutorCapabilities.clear();
|
|
396
|
+
this.#userTurns.clear();
|
|
397
|
+
this.#launchChecks.clear();
|
|
398
|
+
await this.#blackboard?.close();
|
|
399
|
+
}
|
|
400
|
+
if (this.#failure !== undefined)
|
|
401
|
+
throw this.#failure;
|
|
402
|
+
}
|
|
403
|
+
get ownedTaskCount() {
|
|
404
|
+
return this.#tasks.size;
|
|
405
|
+
}
|
|
406
|
+
async #applyEvent(event) {
|
|
407
|
+
this.core.apply(event);
|
|
408
|
+
const userItem = event.kind === 'user_input'
|
|
409
|
+
? this.memory.channels.get(CONVERSATION_CHANNEL)?.items.at(-1) : undefined;
|
|
410
|
+
if (this.#blackboard !== undefined)
|
|
411
|
+
await this.flushMemory();
|
|
412
|
+
const current = this.core.isCurrentConversationEvent(event);
|
|
413
|
+
if (!current) {
|
|
414
|
+
if (event.kind === 'handoff' || event.kind === 'deadline') {
|
|
415
|
+
const control = event.kind === 'handoff'
|
|
416
|
+
? { ...event, payload: { ...event.payload, content: {}, refs: [] } } : event;
|
|
417
|
+
for (const observer of this.#observers)
|
|
418
|
+
observer(structuredClone(control), false);
|
|
419
|
+
}
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
this.#finishIngress(event, userItem);
|
|
423
|
+
for (const observer of this.#observers)
|
|
424
|
+
observer(structuredClone(event), true);
|
|
425
|
+
}
|
|
426
|
+
#finishIngress(event, item) {
|
|
427
|
+
if (event.kind !== 'user_input')
|
|
428
|
+
return;
|
|
429
|
+
const pending = this.#pendingUserInputs.get(event.seq);
|
|
430
|
+
if (pending === undefined)
|
|
431
|
+
return;
|
|
432
|
+
this.#pendingUserInputs.delete(event.seq);
|
|
433
|
+
if (item === undefined || this.memory.channels.get(CONVERSATION_CHANNEL)?.getBySeq(item.seq) === undefined) {
|
|
434
|
+
pending.reject(new Error('applied user input did not create Memory'));
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
pending.resolve(`${item.channel}:${item.seq}`);
|
|
438
|
+
}
|
|
439
|
+
#startModelCall(call) {
|
|
440
|
+
const epoch = this.core.conversationEpoch;
|
|
441
|
+
const port = this.#models[call.slot];
|
|
442
|
+
if (port === undefined)
|
|
443
|
+
throw new Error(`model slot is not connected: ${call.slot}`);
|
|
444
|
+
this.#ownTask(signal => {
|
|
445
|
+
const prepared = this.#blackboard === undefined ? call : this.core.refreshModelCall(call);
|
|
446
|
+
if (prepared.compression_items?.length === 0)
|
|
447
|
+
return Promise.resolve({ channel: prepared.channel, summary: '' });
|
|
448
|
+
return port.complete(structuredClone(prepared), signal);
|
|
449
|
+
}, output => { if (epoch === this.core.conversationEpoch)
|
|
450
|
+
this.core.completeModelCall(call.job_id, output, this.#clock.now()); }, () => { if (epoch === this.core.conversationEpoch)
|
|
451
|
+
this.core.completeModelCall(call.job_id, { port_failure: true }, this.#clock.now()); });
|
|
452
|
+
}
|
|
453
|
+
#startExecutorDispatch(dispatchIndex, delegate) {
|
|
454
|
+
const epoch = this.core.conversationEpoch;
|
|
455
|
+
const adapter = this.#executors.get(delegate.executor);
|
|
456
|
+
if (adapter === undefined)
|
|
457
|
+
throw new Error(`executor adapter is not connected: ${delegate.executor}`);
|
|
458
|
+
if (this.#blackboard !== undefined) {
|
|
459
|
+
this.memory.append(delegate.executor, {
|
|
460
|
+
ts: this.#clock.now(), trust: 'trusted_system', priority: adapter.manifest.policy.priority,
|
|
461
|
+
content: { kind: 'task_admitted', delegate_id: delegate.delegate_id, executor: delegate.executor, op: delegate.op },
|
|
462
|
+
refs: [delegate.origin_ref],
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
this.#ownTask(async (signal) => {
|
|
466
|
+
const binding = this.#taskBindings.get(delegate.delegate_id);
|
|
467
|
+
this.#taskBindings.delete(delegate.delegate_id);
|
|
468
|
+
if (binding)
|
|
469
|
+
try {
|
|
470
|
+
await binding;
|
|
471
|
+
}
|
|
472
|
+
catch {
|
|
473
|
+
return { outcome: 'refused', trust: 'trusted_system', content: { error: 'stale_task' }, refs: [] };
|
|
474
|
+
}
|
|
475
|
+
const userTurn = this.#userTurns.get(delegate.delegate_id);
|
|
476
|
+
this.#userTurns.delete(delegate.delegate_id);
|
|
477
|
+
const launchCheck = this.#launchChecks.get(delegate.delegate_id);
|
|
478
|
+
const wanted = launchCheck ?? userTurn?.stillWanted;
|
|
479
|
+
this.#launchChecks.delete(delegate.delegate_id);
|
|
480
|
+
if (launchCheck === null) {
|
|
481
|
+
this.#hostExecutorCapabilities.delete(delegate.delegate_id);
|
|
482
|
+
return Promise.resolve({ outcome: 'cancelled', trust: 'trusted_system', content: {}, refs: [] });
|
|
483
|
+
}
|
|
484
|
+
let live = true;
|
|
485
|
+
try {
|
|
486
|
+
live = wanted?.() ?? true;
|
|
487
|
+
}
|
|
488
|
+
catch {
|
|
489
|
+
live = false;
|
|
490
|
+
}
|
|
491
|
+
live &&= epoch === this.core.conversationEpoch && this.#clearing === undefined && this.#acceptCompletions;
|
|
492
|
+
if (!live) {
|
|
493
|
+
this.#hostExecutorCapabilities.delete(delegate.delegate_id);
|
|
494
|
+
return Promise.resolve({ outcome: 'refused', trust: 'trusted_system', content: { error: 'superseded' }, refs: [] });
|
|
495
|
+
}
|
|
496
|
+
const grant = this.#taskGrants.get(delegate.delegate_id);
|
|
497
|
+
this.#taskGrants.delete(delegate.delegate_id);
|
|
498
|
+
const activityTasks = grant ? taskGrantService(grant) : undefined;
|
|
499
|
+
let activitySession;
|
|
500
|
+
const context = {
|
|
501
|
+
...(activityTasks ? { resourceWaiting: (reason) => activityTasks.resourceState(grant.fence.task_id, reason) } : {}),
|
|
502
|
+
...(activityTasks ? { activity: (item) => { void Promise.resolve().then(() => activityTasks.appendEvent({ task_id: grant.fence.task_id, work_id: delegate.delegate_id, thread_id: item.thread_id, turn_id: item.turn_id, item_id: item.item_id, stage: item.stage, ...(activitySession ? { session_id: activitySession } : {}), kind: item.kind, ...(item.sender ? { sender: item.sender } : {}), text: item.text, refs: item.refs, ...(item.text_truncated ? { text_truncated: true } : {}) }, JSON.stringify([delegate.delegate_id, item.thread_id, item.turn_id, item.item_id, item.stage]))).catch(() => { this.core.diagnostics.push({ code: 'task_event_persistence_failed', details: { task_id: grant.fence.task_id, work_id: delegate.delegate_id } }); activityTasks.markReplayIncomplete(grant.fence.task_id); }); } } : {}),
|
|
503
|
+
instructionAccepted: () => { this.#instructionReceipts.get(delegate.delegate_id)?.('accepted'); this.#instructionReceipts.delete(delegate.delegate_id); },
|
|
504
|
+
...(grant ? { bindSession: async (sessionId, target) => { const tasks = taskGrantService(grant); await tasks.bindWork(grant.fence, delegate.delegate_id, sessionId, true, target); activitySession = sessionId; } } : {}),
|
|
505
|
+
...(wanted === undefined ? {} : { beforeWrite: () => { if (!wanted())
|
|
506
|
+
throw Error('superseded'); } }),
|
|
507
|
+
...(userTurn === undefined ? {} : { userTurn }),
|
|
508
|
+
clock: this.#clock,
|
|
509
|
+
delegate: structuredClone(delegate),
|
|
510
|
+
signal,
|
|
511
|
+
progress: payload => this.#postDecoration(() => {
|
|
512
|
+
this.core.postExecutorProgress(dispatchIndex, payload, this.#clock.now());
|
|
513
|
+
}),
|
|
514
|
+
observe: payload => this.#postDecoration(() => {
|
|
515
|
+
this.core.postExecutorObservation(dispatchIndex, payload, this.#clock.now());
|
|
516
|
+
}),
|
|
517
|
+
};
|
|
518
|
+
const capability = this.#hostExecutorCapabilities.get(delegate.delegate_id);
|
|
519
|
+
this.#hostExecutorCapabilities.delete(delegate.delegate_id);
|
|
520
|
+
if (capability !== undefined)
|
|
521
|
+
bindHostExecutorCapability(context, capability);
|
|
522
|
+
return adapter.dispatch(delegate.op, structuredClone(delegate.request), context);
|
|
523
|
+
}, output => { this.#instructionReceipts.get(delegate.delegate_id)?.(output?.outcome === 'unknown' ? 'unknown' : output?.outcome === 'ok' ? 'accepted' : 'failed'); this.#instructionReceipts.delete(delegate.delegate_id); this.core.postExecutorResult(dispatchIndex, output, this.#clock.now()); }, error => {
|
|
524
|
+
this.#instructionReceipts.get(delegate.delegate_id)?.('unknown');
|
|
525
|
+
this.#instructionReceipts.delete(delegate.delegate_id);
|
|
526
|
+
this.core.postExecutorCompletion(dispatchIndex, {
|
|
527
|
+
outcome: 'unknown',
|
|
528
|
+
trust: 'trusted_system',
|
|
529
|
+
content: {
|
|
530
|
+
error: 'adapter_raised',
|
|
531
|
+
exception: 'ExecutorFailure',
|
|
532
|
+
detail: 'dispatch_failed',
|
|
533
|
+
...dispatchFailureCause(error),
|
|
534
|
+
},
|
|
535
|
+
refs: [],
|
|
536
|
+
}, this.#clock.now());
|
|
537
|
+
}, true);
|
|
538
|
+
}
|
|
539
|
+
#postDecoration(post) {
|
|
540
|
+
if (!this.#acceptCompletions)
|
|
541
|
+
return;
|
|
542
|
+
try {
|
|
543
|
+
post();
|
|
544
|
+
this.#notifyWork();
|
|
545
|
+
}
|
|
546
|
+
catch {
|
|
547
|
+
// Progress and live observations are decorative and cannot break the terminal handoff.
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
#ownTask(run, complete, fail, publishAdmission = false) {
|
|
551
|
+
if (!this.#acceptCompletions)
|
|
552
|
+
return;
|
|
553
|
+
const controller = new AbortController();
|
|
554
|
+
const owned = { controller, kind: publishAdmission ? 'executor' : 'model', promise: Promise.resolve() };
|
|
555
|
+
let started = false;
|
|
556
|
+
owned.promise = Promise.resolve()
|
|
557
|
+
.then(async () => {
|
|
558
|
+
if (this.#blackboard !== undefined) {
|
|
559
|
+
await this.flushMemory();
|
|
560
|
+
}
|
|
561
|
+
// This boundary also applies without persistence: admission itself is now asynchronous.
|
|
562
|
+
if (publishAdmission)
|
|
563
|
+
await yieldToEventLoop();
|
|
564
|
+
if (!this.#acceptCompletions || controller.signal.aborted)
|
|
565
|
+
return undefined;
|
|
566
|
+
started = true;
|
|
567
|
+
return run(controller.signal);
|
|
568
|
+
})
|
|
569
|
+
.then(output => {
|
|
570
|
+
if (started && this.#acceptCompletions && !controller.signal.aborted)
|
|
571
|
+
complete(output);
|
|
572
|
+
}, (error) => {
|
|
573
|
+
if (started && this.#acceptCompletions && !controller.signal.aborted)
|
|
574
|
+
fail(error);
|
|
575
|
+
})
|
|
576
|
+
.catch(error => {
|
|
577
|
+
if (this.#failure === undefined)
|
|
578
|
+
this.#failure = runtimeError(error);
|
|
579
|
+
})
|
|
580
|
+
.finally(() => {
|
|
581
|
+
this.#tasks.delete(owned);
|
|
582
|
+
this.#notifyWork();
|
|
583
|
+
});
|
|
584
|
+
this.#tasks.add(owned);
|
|
585
|
+
}
|
|
586
|
+
async #waitForWork(signal) {
|
|
587
|
+
const version = this.#workVersion;
|
|
588
|
+
const timer = new AbortController();
|
|
589
|
+
let release;
|
|
590
|
+
const work = new Promise(resolve => {
|
|
591
|
+
release = resolve;
|
|
592
|
+
this.#workWaiter = resolve;
|
|
593
|
+
if (this.#workVersion !== version || signal.aborted)
|
|
594
|
+
resolve();
|
|
595
|
+
});
|
|
596
|
+
const next = this.core.queue.nextTimestamp();
|
|
597
|
+
const deadline = next === undefined
|
|
598
|
+
? new Promise(() => undefined)
|
|
599
|
+
: this.#clock.sleep(Math.max(0, next - this.#clock.now()), timer.signal)
|
|
600
|
+
.catch(error => {
|
|
601
|
+
if (!timer.signal.aborted)
|
|
602
|
+
throw error;
|
|
603
|
+
});
|
|
604
|
+
try {
|
|
605
|
+
await Promise.race([work, deadline]);
|
|
606
|
+
}
|
|
607
|
+
finally {
|
|
608
|
+
timer.abort();
|
|
609
|
+
if (this.#workWaiter === release)
|
|
610
|
+
this.#workWaiter = undefined;
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
#notifyWork() {
|
|
614
|
+
this.#workVersion += 1;
|
|
615
|
+
this.#workWaiter?.();
|
|
616
|
+
}
|
|
617
|
+
async #shutdownTasks() {
|
|
618
|
+
const tasks = [...this.#tasks];
|
|
619
|
+
for (const task of tasks)
|
|
620
|
+
task.controller.abort();
|
|
621
|
+
if (tasks.length === 0)
|
|
622
|
+
return;
|
|
623
|
+
const timer = new AbortController();
|
|
624
|
+
try {
|
|
625
|
+
await Promise.race([
|
|
626
|
+
Promise.allSettled(tasks.map(task => task.promise)),
|
|
627
|
+
this.#clock.sleep(this.#shutdownGrace, timer.signal).catch(error => {
|
|
628
|
+
if (!timer.signal.aborted)
|
|
629
|
+
throw error;
|
|
630
|
+
}),
|
|
631
|
+
]);
|
|
632
|
+
}
|
|
633
|
+
finally {
|
|
634
|
+
timer.abort();
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
function runtimeError(value) {
|
|
639
|
+
return value instanceof Error ? value : new Error('causal runtime task failed', { cause: value });
|
|
640
|
+
}
|
|
641
|
+
/** Error class and machine code only; adapter messages may carry untrusted text. */
|
|
642
|
+
function dispatchFailureCause(error) {
|
|
643
|
+
const name = error instanceof Error && /^[A-Za-z]{1,64}$/u.test(error.name) ? error.name : 'unknown';
|
|
644
|
+
const isCode = (value) => typeof value === 'string' && /^[a-z_]{1,64}$/u.test(value);
|
|
645
|
+
const code = error?.code;
|
|
646
|
+
// Internal errors often use a bare snake_case code as the message; prose never matches.
|
|
647
|
+
const fallback = error instanceof Error ? error.message : undefined;
|
|
648
|
+
return { cause: name, ...(isCode(code) ? { cause_code: code } : isCode(fallback) ? { cause_code: fallback } : {}) };
|
|
649
|
+
}
|
|
650
|
+
//# sourceMappingURL=causal-runtime.js.map
|