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,597 @@
|
|
|
1
|
+
import { completePurgeIndex, purgeEntry, purgeStatus, recoverMemoryPurges } from '../memory-substrate/purge.js';
|
|
2
|
+
import { enableMemoryFiles, flushMemoryFiles, hasMemoryFileAuthority, memoryFilesEnabled } from '../memory-substrate/file-authority.js';
|
|
3
|
+
import { MarkdownRepository } from '../memory-substrate/markdown-repository.js';
|
|
4
|
+
import { reconcileMemoryFiles } from '../memory-substrate/store.js';
|
|
5
|
+
import { recordWorkspaceRevision } from '../memory-substrate/workspace.js';
|
|
6
|
+
import { migrateLegacyMemory } from '../memory-substrate/migration.js';
|
|
7
|
+
import { initializeMemory, memoryOperation } from '../memory-substrate/store.js';
|
|
8
|
+
import { initializeSourceState, assertSourceStateSchema } from '../memory-substrate/source-state.js';
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import { canonicalJson } from '../text/canonical-json.js';
|
|
11
|
+
import { EvidenceRefSchema, LogicalWorkspaceSchema, RelationCardSchema, WorkspaceInstanceSchema, relationTypeSchema, } from './models.js';
|
|
12
|
+
export const MEMORY_LEDGER_SCHEMA_VERSION = 4;
|
|
13
|
+
const MEMORY_LOCK_WAIT_MS = 10_000;
|
|
14
|
+
const column = (name, type, notnull = 1, pk = 0) => ({ name, type, notnull, pk });
|
|
15
|
+
const V1_TABLE_SHAPES = Object.freeze({
|
|
16
|
+
observations: Object.freeze([
|
|
17
|
+
column('observation_id', 'TEXT'), column('observation_type', 'TEXT'),
|
|
18
|
+
column('occurred_at', 'REAL'), column('source', 'TEXT'), column('ref', 'TEXT'),
|
|
19
|
+
column('trust', 'TEXT'), column('logical_workspace_id', 'TEXT', 0),
|
|
20
|
+
column('workspace_instance_id', 'TEXT', 0),
|
|
21
|
+
column('related_logical_workspace_id', 'TEXT', 0), column('summary', 'TEXT', 0),
|
|
22
|
+
column('outcome', 'TEXT', 0), column('payload_json', 'TEXT'),
|
|
23
|
+
]),
|
|
24
|
+
logical_workspaces: Object.freeze([
|
|
25
|
+
column('logical_workspace_id', 'TEXT', 1, 1), column('display_name', 'TEXT'),
|
|
26
|
+
column('canonical_remote', 'TEXT', 0), column('created_at', 'REAL'),
|
|
27
|
+
column('updated_at', 'REAL'), column('revision', 'INTEGER'), column('payload_json', 'TEXT'),
|
|
28
|
+
]),
|
|
29
|
+
workspace_instances: Object.freeze([
|
|
30
|
+
column('instance_id', 'TEXT', 1, 1), column('logical_workspace_id', 'TEXT'),
|
|
31
|
+
column('display_name', 'TEXT'), column('path_label', 'TEXT'), column('branch', 'TEXT', 0),
|
|
32
|
+
column('repository_fingerprint', 'TEXT', 0), column('status', 'TEXT'),
|
|
33
|
+
column('first_seen_at', 'REAL'), column('last_seen_at', 'REAL'),
|
|
34
|
+
column('revision', 'INTEGER'), column('payload_json', 'TEXT'),
|
|
35
|
+
]),
|
|
36
|
+
relation_cards: Object.freeze([
|
|
37
|
+
column('source_logical_id', 'TEXT', 1, 1), column('target_logical_id', 'TEXT', 1, 2),
|
|
38
|
+
column('relation_type', 'TEXT', 1, 3), column('confidence', 'REAL'),
|
|
39
|
+
column('reason', 'TEXT'), column('first_seen_at', 'REAL'), column('last_seen_at', 'REAL'),
|
|
40
|
+
column('status', 'TEXT'), column('revision', 'INTEGER'), column('payload_json', 'TEXT'),
|
|
41
|
+
]),
|
|
42
|
+
relation_evidence: Object.freeze([
|
|
43
|
+
column('source_logical_id', 'TEXT', 1, 1), column('target_logical_id', 'TEXT', 1, 2),
|
|
44
|
+
column('relation_type', 'TEXT', 1, 3), column('evidence_source', 'TEXT', 1, 4),
|
|
45
|
+
column('evidence_ref', 'TEXT', 1, 5), column('observed_at', 'REAL'),
|
|
46
|
+
column('evidence_json', 'TEXT'),
|
|
47
|
+
]),
|
|
48
|
+
});
|
|
49
|
+
const V2_TABLE_SHAPES = Object.freeze({
|
|
50
|
+
...V1_TABLE_SHAPES,
|
|
51
|
+
operation_receipts: Object.freeze([
|
|
52
|
+
column('receipt_sequence', 'INTEGER', 0, 1), column('operation_id', 'TEXT'),
|
|
53
|
+
column('operation_type', 'TEXT'), column('input_digest', 'TEXT'),
|
|
54
|
+
column('committed_at', 'INTEGER'), column('result_json', 'TEXT'),
|
|
55
|
+
]),
|
|
56
|
+
});
|
|
57
|
+
const V3_TABLE_SHAPES = Object.freeze({
|
|
58
|
+
...V2_TABLE_SHAPES,
|
|
59
|
+
identity_bindings: Object.freeze([
|
|
60
|
+
column('binding_id', 'TEXT', 1, 1), column('instance_id', 'TEXT'),
|
|
61
|
+
column('payload_json', 'TEXT'),
|
|
62
|
+
]),
|
|
63
|
+
alias_observations: Object.freeze([
|
|
64
|
+
column('observation_id', 'TEXT', 1, 1), column('logical_workspace_id', 'TEXT'),
|
|
65
|
+
column('evidence_ref', 'TEXT'), column('payload_json', 'TEXT'),
|
|
66
|
+
]),
|
|
67
|
+
projection_records: Object.freeze([
|
|
68
|
+
column('record_id', 'TEXT', 1, 1), column('observation_source', 'TEXT'),
|
|
69
|
+
column('observation_id', 'TEXT'), column('relation_delta_digest', 'TEXT'),
|
|
70
|
+
column('payload_json', 'TEXT'),
|
|
71
|
+
]),
|
|
72
|
+
});
|
|
73
|
+
const TABLE_UNIQUE_KEYS = Object.freeze({
|
|
74
|
+
observations: Object.freeze([
|
|
75
|
+
Object.freeze(['observation_id']),
|
|
76
|
+
Object.freeze(['source', 'ref']),
|
|
77
|
+
]),
|
|
78
|
+
operation_receipts: Object.freeze([Object.freeze(['operation_id'])]),
|
|
79
|
+
alias_observations: Object.freeze([Object.freeze(['evidence_ref'])]),
|
|
80
|
+
projection_records: Object.freeze([
|
|
81
|
+
Object.freeze(['observation_source', 'observation_id']),
|
|
82
|
+
]),
|
|
83
|
+
});
|
|
84
|
+
const RELATION_EVIDENCE_FOREIGN_KEYS = Object.freeze([
|
|
85
|
+
Object.freeze({
|
|
86
|
+
id: 0, seq: 0, target_table: 'relation_cards', from_column: 'source_logical_id',
|
|
87
|
+
to_column: 'source_logical_id', on_update: 'NO ACTION', on_delete: 'CASCADE', match: 'NONE',
|
|
88
|
+
}),
|
|
89
|
+
Object.freeze({
|
|
90
|
+
id: 0, seq: 1, target_table: 'relation_cards', from_column: 'target_logical_id',
|
|
91
|
+
to_column: 'target_logical_id', on_update: 'NO ACTION', on_delete: 'CASCADE', match: 'NONE',
|
|
92
|
+
}),
|
|
93
|
+
Object.freeze({
|
|
94
|
+
id: 0, seq: 2, target_table: 'relation_cards', from_column: 'relation_type',
|
|
95
|
+
to_column: 'relation_type', on_update: 'NO ACTION', on_delete: 'CASCADE', match: 'NONE',
|
|
96
|
+
}),
|
|
97
|
+
]);
|
|
98
|
+
function compareSchemaKeys(left, right) {
|
|
99
|
+
const leftKey = left.join('\u0000');
|
|
100
|
+
const rightKey = right.join('\u0000');
|
|
101
|
+
return leftKey < rightKey ? -1 : leftKey > rightKey ? 1 : 0;
|
|
102
|
+
}
|
|
103
|
+
const storeErrorMessages = {
|
|
104
|
+
STORE_ALREADY_OPEN: 'memory ledger store is already open',
|
|
105
|
+
STORE_CLOSED: 'memory ledger store is closed',
|
|
106
|
+
STORE_IDEMPOTENCY_CONFLICT: 'memory ledger observation replay conflict',
|
|
107
|
+
STORE_INVALID_CARD: 'invalid memory ledger card',
|
|
108
|
+
STORE_INVALID_OBSERVATION: 'invalid memory ledger observation',
|
|
109
|
+
STORE_INVALID_RELATION: 'invalid memory ledger relation',
|
|
110
|
+
STORE_MIGRATION_FAILED: 'memory ledger schema migration failed',
|
|
111
|
+
STORE_NOT_FOUND: 'memory ledger record was not found',
|
|
112
|
+
STORE_INVALID_OPERATION: 'memory ledger operation is invalid',
|
|
113
|
+
STORE_PURGED_ID: 'memory identifier was permanently purged',
|
|
114
|
+
STORE_STATED_EVIDENCE_REQUIRED: 'stated memory requires trusted user evidence',
|
|
115
|
+
STORE_OPERATION_CONFLICT: 'memory ledger operation replay conflict',
|
|
116
|
+
STORE_READ_FAILED: 'memory ledger read failed',
|
|
117
|
+
STORE_SCHEMA_UNSUPPORTED: 'memory ledger schema version is unsupported',
|
|
118
|
+
STORE_SENSITIVE_CONTENT_REJECTED: 'memory ledger sensitive content was rejected',
|
|
119
|
+
STORE_SENSITIVE_PATH_DENIED: 'memory ledger sensitive path was denied',
|
|
120
|
+
STORE_STALE_REVISION: 'memory ledger revision is stale',
|
|
121
|
+
STORE_MEMORY_CONFLICT: 'memory documents conflict; user edits were preserved',
|
|
122
|
+
STORE_WRITE_FAILED: 'memory ledger write failed',
|
|
123
|
+
};
|
|
124
|
+
export class MemoryLedgerStoreError extends Error {
|
|
125
|
+
code;
|
|
126
|
+
constructor(code) {
|
|
127
|
+
super(storeErrorMessages[code]);
|
|
128
|
+
this.name = 'MemoryLedgerStoreError';
|
|
129
|
+
this.code = code;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export class MemoryLedgerStore {
|
|
133
|
+
#path;
|
|
134
|
+
#databaseFactory;
|
|
135
|
+
#database;
|
|
136
|
+
#memoryLockHeld = false;
|
|
137
|
+
#fileRepository;
|
|
138
|
+
constructor(path, databaseFactory, options = {}) {
|
|
139
|
+
this.#path = path;
|
|
140
|
+
this.#fileRepository = new MarkdownRepository(path + '.memory', { lockWaitMs: options.memoryLockWaitMs ?? MEMORY_LOCK_WAIT_MS });
|
|
141
|
+
this.#databaseFactory = databaseFactory;
|
|
142
|
+
}
|
|
143
|
+
open() { this.withMemoryFilesLock(() => this.#openLocked()); }
|
|
144
|
+
#openLocked() {
|
|
145
|
+
if (this.#database !== undefined)
|
|
146
|
+
throw new MemoryLedgerStoreError('STORE_ALREADY_OPEN');
|
|
147
|
+
let database;
|
|
148
|
+
try {
|
|
149
|
+
database = this.#databaseFactory(this.#path);
|
|
150
|
+
database.exec('PRAGMA busy_timeout=1000');
|
|
151
|
+
database.exec('PRAGMA foreign_keys=ON');
|
|
152
|
+
this.#migrate(database);
|
|
153
|
+
initializeMemory(database);
|
|
154
|
+
recoverMemoryPurges(database, this.#path);
|
|
155
|
+
const authoritative = hasMemoryFileAuthority(database, this.#path + '.memory');
|
|
156
|
+
if (authoritative) {
|
|
157
|
+
enableMemoryFiles(database, this.#path + '.memory', { alreadyLocked: true });
|
|
158
|
+
reconcileMemoryFiles(database);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
database.exec('BEGIN IMMEDIATE');
|
|
162
|
+
try {
|
|
163
|
+
for (const [table, kind] of [['logical_workspaces', 'LogicalWorkspace'], ['workspace_instances', 'WorkspaceInstance'], ['relation_cards', 'RelationCard']]) {
|
|
164
|
+
for (const row of database.prepare(`SELECT payload_json FROM ${table}`).all()) {
|
|
165
|
+
recordWorkspaceRevision(database, kind, JSON.parse(String(row.payload_json)));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
database.exec('COMMIT');
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
database.exec('ROLLBACK');
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// A fresh database must finish its serialized schema transaction before
|
|
176
|
+
// concurrent clients negotiate WAL. On Windows, racing journal_mode with
|
|
177
|
+
// another connection's first migration fails immediately despite the busy
|
|
178
|
+
// timeout, while BEGIN IMMEDIATE itself waits correctly.
|
|
179
|
+
database.exec('PRAGMA journal_mode=WAL');
|
|
180
|
+
this.#database = database;
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
try {
|
|
184
|
+
database?.close();
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
// The stable migration error below is the only failure exposed across RPC.
|
|
188
|
+
}
|
|
189
|
+
if (error instanceof MemoryLedgerStoreError)
|
|
190
|
+
throw error;
|
|
191
|
+
if (error instanceof Error && error.message.startsWith('MEMORY_MARKDOWN_'))
|
|
192
|
+
throw new MemoryLedgerStoreError('STORE_MEMORY_CONFLICT');
|
|
193
|
+
throw new MemoryLedgerStoreError('STORE_MIGRATION_FAILED');
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
memory(operation, input, openLegacy) {
|
|
197
|
+
return this.withMemoryFilesLock(() => {
|
|
198
|
+
if (operation === 'enable_files') {
|
|
199
|
+
enableMemoryFiles(this.#requireDatabase(), this.#path + '.memory', { alreadyLocked: true });
|
|
200
|
+
reconcileMemoryFiles(this.#requireDatabase());
|
|
201
|
+
return { enabled: true };
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
if (operation === 'purge')
|
|
205
|
+
return purgeEntry(this.#requireDatabase(), this.#path, input);
|
|
206
|
+
if (operation === 'purge_index_complete')
|
|
207
|
+
return completePurgeIndex(this.#requireDatabase(), this.#path, input);
|
|
208
|
+
if (operation === 'purge_status')
|
|
209
|
+
return purgeStatus(this.#requireDatabase(), z.object({ entry_prefix: z.string().min(1) }).strict().parse(input).entry_prefix);
|
|
210
|
+
this.#joinMemoryFileAuthority();
|
|
211
|
+
return operation === 'migrate_legacy' ? migrateLegacyMemory(this.#requireDatabase(), input, openLegacy ?? (() => { throw new Error('STORE_INVALID_OPERATION'); })) : memoryOperation(this.#requireDatabase(), operation, input);
|
|
212
|
+
}
|
|
213
|
+
catch (error) {
|
|
214
|
+
const message = error instanceof Error ? error.message : '';
|
|
215
|
+
const code = { version_conflict: 'STORE_STALE_REVISION', request_id_conflict: 'STORE_IDEMPOTENCY_CONFLICT', item_not_found: 'STORE_NOT_FOUND' }[message] ?? message;
|
|
216
|
+
if (message.startsWith('MEMORY_MARKDOWN_'))
|
|
217
|
+
throw new MemoryLedgerStoreError('STORE_MEMORY_CONFLICT');
|
|
218
|
+
if (['STORE_STALE_REVISION', 'STORE_NOT_FOUND', 'STORE_INVALID_OPERATION', 'STORE_PURGED_ID', 'STORE_STATED_EVIDENCE_REQUIRED', 'STORE_IDEMPOTENCY_CONFLICT'].includes(code))
|
|
219
|
+
throw new MemoryLedgerStoreError(code);
|
|
220
|
+
throw new MemoryLedgerStoreError('STORE_WRITE_FAILED');
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
/** Process and worker independent path lock; only this store's synchronous nested calls may reuse it. */
|
|
225
|
+
withMemoryFilesLock(fn) {
|
|
226
|
+
if (this.#memoryLockHeld || this.#path === ':memory:')
|
|
227
|
+
return fn();
|
|
228
|
+
return this.#fileRepository.withLock(() => { this.#memoryLockHeld = true; try {
|
|
229
|
+
return fn();
|
|
230
|
+
}
|
|
231
|
+
finally {
|
|
232
|
+
this.#memoryLockHeld = false;
|
|
233
|
+
} });
|
|
234
|
+
}
|
|
235
|
+
#joinMemoryFileAuthority() {
|
|
236
|
+
const db = this.#database;
|
|
237
|
+
if (db)
|
|
238
|
+
recoverMemoryPurges(db, this.#path);
|
|
239
|
+
if (db && !memoryFilesEnabled(db) && hasMemoryFileAuthority(db, this.#path + '.memory'))
|
|
240
|
+
enableMemoryFiles(db, this.#path + '.memory', { alreadyLocked: true });
|
|
241
|
+
}
|
|
242
|
+
syncMemoryFiles() { if (this.#database) {
|
|
243
|
+
this.#joinMemoryFileAuthority();
|
|
244
|
+
reconcileMemoryFiles(this.#database);
|
|
245
|
+
} }
|
|
246
|
+
flushMemoryFiles() { if (this.#database)
|
|
247
|
+
flushMemoryFiles(this.#database); }
|
|
248
|
+
close() {
|
|
249
|
+
if (this.#database === undefined)
|
|
250
|
+
return;
|
|
251
|
+
const database = this.#database;
|
|
252
|
+
this.#database = undefined;
|
|
253
|
+
try {
|
|
254
|
+
database.close();
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
throw new MemoryLedgerStoreError('STORE_WRITE_FAILED');
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
listLogicalWorkspaces() {
|
|
261
|
+
const database = this.#requireDatabase();
|
|
262
|
+
return this.#read(() => this.#parseRows(database.prepare('SELECT payload_json FROM logical_workspaces ORDER BY logical_workspace_id').all(), LogicalWorkspaceSchema));
|
|
263
|
+
}
|
|
264
|
+
getWorkspaceInstance(instanceId) {
|
|
265
|
+
const database = this.#requireDatabase();
|
|
266
|
+
return this.#read(() => {
|
|
267
|
+
const row = database.prepare(`
|
|
268
|
+
SELECT payload_json FROM workspace_instances WHERE instance_id = ?
|
|
269
|
+
`).get(instanceId);
|
|
270
|
+
return row === undefined ? undefined : WorkspaceInstanceSchema.parse(JSON.parse(stringColumn(row, 'payload_json')));
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
listRelations() {
|
|
274
|
+
const database = this.#requireDatabase();
|
|
275
|
+
return this.#read(() => this.#parseRows(database.prepare(`
|
|
276
|
+
SELECT payload_json FROM relation_cards
|
|
277
|
+
ORDER BY source_logical_id, target_logical_id, relation_type
|
|
278
|
+
`).all(), RelationCardSchema));
|
|
279
|
+
}
|
|
280
|
+
listRelationEvidence(sourceId, targetId, relationType) {
|
|
281
|
+
const parsedType = relationTypeSchema.safeParse(relationType);
|
|
282
|
+
if (!parsedType.success)
|
|
283
|
+
throw new MemoryLedgerStoreError('STORE_INVALID_RELATION');
|
|
284
|
+
const database = this.#requireDatabase();
|
|
285
|
+
return this.#read(() => database.prepare(`
|
|
286
|
+
SELECT evidence_json FROM relation_evidence
|
|
287
|
+
WHERE source_logical_id = ? AND target_logical_id = ? AND relation_type = ?
|
|
288
|
+
ORDER BY observed_at, evidence_source, evidence_ref
|
|
289
|
+
`).all(sourceId, targetId, parsedType.data).map(row => (EvidenceRefSchema.parse(JSON.parse(stringColumn(row, 'evidence_json'))))));
|
|
290
|
+
}
|
|
291
|
+
#migrate(database) {
|
|
292
|
+
database.exec('BEGIN IMMEDIATE');
|
|
293
|
+
try {
|
|
294
|
+
database.exec(`
|
|
295
|
+
CREATE TABLE IF NOT EXISTS schema_migrations(
|
|
296
|
+
version INTEGER PRIMARY KEY,
|
|
297
|
+
applied_at INTEGER NOT NULL
|
|
298
|
+
) STRICT;
|
|
299
|
+
`);
|
|
300
|
+
this.#assertTableShape(database, 'schema_migrations', [
|
|
301
|
+
column('version', 'INTEGER', 0, 1),
|
|
302
|
+
column('applied_at', 'INTEGER'),
|
|
303
|
+
]);
|
|
304
|
+
let version = this.#schemaVersion(database);
|
|
305
|
+
if (version > MEMORY_LEDGER_SCHEMA_VERSION) {
|
|
306
|
+
throw new MemoryLedgerStoreError('STORE_SCHEMA_UNSUPPORTED');
|
|
307
|
+
}
|
|
308
|
+
if (version === 0) {
|
|
309
|
+
if (Object.keys(V3_TABLE_SHAPES).some(table => this.#tableExists(database, table))) {
|
|
310
|
+
throw new Error('unversioned memory ledger tables');
|
|
311
|
+
}
|
|
312
|
+
this.#createV1Schema(database);
|
|
313
|
+
this.#createV2Schema(database);
|
|
314
|
+
this.#createV3Schema(database);
|
|
315
|
+
this.#recordMigration(database, 3);
|
|
316
|
+
version = 3;
|
|
317
|
+
}
|
|
318
|
+
if (version === 1) {
|
|
319
|
+
this.#assertSchemaShape(database, V1_TABLE_SHAPES);
|
|
320
|
+
this.#createV2Schema(database);
|
|
321
|
+
this.#recordMigration(database, 2);
|
|
322
|
+
version = 2;
|
|
323
|
+
}
|
|
324
|
+
if (version === 2) {
|
|
325
|
+
this.#assertSchemaShape(database, V2_TABLE_SHAPES);
|
|
326
|
+
this.#createV3Schema(database);
|
|
327
|
+
this.#recordMigration(database, 3);
|
|
328
|
+
version = 3;
|
|
329
|
+
}
|
|
330
|
+
if (version === 3) {
|
|
331
|
+
initializeSourceState(database);
|
|
332
|
+
this.#recordMigration(database, 4);
|
|
333
|
+
version = 4;
|
|
334
|
+
}
|
|
335
|
+
if (version !== MEMORY_LEDGER_SCHEMA_VERSION)
|
|
336
|
+
throw new Error('schema version gap');
|
|
337
|
+
assertSourceStateSchema(database);
|
|
338
|
+
this.#assertSchemaShape(database, V3_TABLE_SHAPES);
|
|
339
|
+
this.#createIndexes(database);
|
|
340
|
+
database.exec('COMMIT');
|
|
341
|
+
}
|
|
342
|
+
catch (error) {
|
|
343
|
+
rollback(database);
|
|
344
|
+
if (error instanceof MemoryLedgerStoreError)
|
|
345
|
+
throw error;
|
|
346
|
+
throw new MemoryLedgerStoreError('STORE_MIGRATION_FAILED');
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
#createV1Schema(database) {
|
|
350
|
+
database.exec(`
|
|
351
|
+
CREATE TABLE IF NOT EXISTS observations(
|
|
352
|
+
observation_id TEXT NOT NULL UNIQUE,
|
|
353
|
+
observation_type TEXT NOT NULL,
|
|
354
|
+
occurred_at REAL NOT NULL,
|
|
355
|
+
source TEXT NOT NULL,
|
|
356
|
+
ref TEXT NOT NULL,
|
|
357
|
+
trust TEXT NOT NULL,
|
|
358
|
+
logical_workspace_id TEXT,
|
|
359
|
+
workspace_instance_id TEXT,
|
|
360
|
+
related_logical_workspace_id TEXT,
|
|
361
|
+
summary TEXT,
|
|
362
|
+
outcome TEXT,
|
|
363
|
+
payload_json TEXT NOT NULL,
|
|
364
|
+
UNIQUE(source, ref)
|
|
365
|
+
) STRICT;
|
|
366
|
+
CREATE INDEX IF NOT EXISTS observations_scope_time
|
|
367
|
+
ON observations(logical_workspace_id, occurred_at);
|
|
368
|
+
CREATE TABLE IF NOT EXISTS logical_workspaces(
|
|
369
|
+
logical_workspace_id TEXT PRIMARY KEY,
|
|
370
|
+
display_name TEXT NOT NULL,
|
|
371
|
+
canonical_remote TEXT,
|
|
372
|
+
created_at REAL NOT NULL,
|
|
373
|
+
updated_at REAL NOT NULL,
|
|
374
|
+
revision INTEGER NOT NULL,
|
|
375
|
+
payload_json TEXT NOT NULL
|
|
376
|
+
) STRICT;
|
|
377
|
+
CREATE TABLE IF NOT EXISTS workspace_instances(
|
|
378
|
+
instance_id TEXT PRIMARY KEY,
|
|
379
|
+
logical_workspace_id TEXT NOT NULL,
|
|
380
|
+
display_name TEXT NOT NULL,
|
|
381
|
+
path_label TEXT NOT NULL,
|
|
382
|
+
branch TEXT,
|
|
383
|
+
repository_fingerprint TEXT,
|
|
384
|
+
status TEXT NOT NULL,
|
|
385
|
+
first_seen_at REAL NOT NULL,
|
|
386
|
+
last_seen_at REAL NOT NULL,
|
|
387
|
+
revision INTEGER NOT NULL,
|
|
388
|
+
payload_json TEXT NOT NULL
|
|
389
|
+
) STRICT;
|
|
390
|
+
CREATE INDEX IF NOT EXISTS workspace_instances_logical_status
|
|
391
|
+
ON workspace_instances(logical_workspace_id, status);
|
|
392
|
+
CREATE TABLE IF NOT EXISTS relation_cards(
|
|
393
|
+
source_logical_id TEXT NOT NULL,
|
|
394
|
+
target_logical_id TEXT NOT NULL,
|
|
395
|
+
relation_type TEXT NOT NULL,
|
|
396
|
+
confidence REAL NOT NULL,
|
|
397
|
+
reason TEXT NOT NULL,
|
|
398
|
+
first_seen_at REAL NOT NULL,
|
|
399
|
+
last_seen_at REAL NOT NULL,
|
|
400
|
+
status TEXT NOT NULL,
|
|
401
|
+
revision INTEGER NOT NULL,
|
|
402
|
+
payload_json TEXT NOT NULL,
|
|
403
|
+
PRIMARY KEY(source_logical_id, target_logical_id, relation_type)
|
|
404
|
+
) STRICT;
|
|
405
|
+
CREATE INDEX IF NOT EXISTS relation_cards_source_status
|
|
406
|
+
ON relation_cards(source_logical_id, status);
|
|
407
|
+
CREATE TABLE IF NOT EXISTS relation_evidence(
|
|
408
|
+
source_logical_id TEXT NOT NULL,
|
|
409
|
+
target_logical_id TEXT NOT NULL,
|
|
410
|
+
relation_type TEXT NOT NULL,
|
|
411
|
+
evidence_source TEXT NOT NULL,
|
|
412
|
+
evidence_ref TEXT NOT NULL,
|
|
413
|
+
observed_at REAL NOT NULL,
|
|
414
|
+
evidence_json TEXT NOT NULL,
|
|
415
|
+
PRIMARY KEY(
|
|
416
|
+
source_logical_id, target_logical_id, relation_type,
|
|
417
|
+
evidence_source, evidence_ref
|
|
418
|
+
),
|
|
419
|
+
FOREIGN KEY(source_logical_id, target_logical_id, relation_type)
|
|
420
|
+
REFERENCES relation_cards(source_logical_id, target_logical_id, relation_type)
|
|
421
|
+
ON DELETE CASCADE
|
|
422
|
+
) STRICT;
|
|
423
|
+
`);
|
|
424
|
+
}
|
|
425
|
+
#createV2Schema(database) {
|
|
426
|
+
database.exec(`
|
|
427
|
+
CREATE TABLE IF NOT EXISTS operation_receipts(
|
|
428
|
+
receipt_sequence INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
429
|
+
operation_id TEXT NOT NULL UNIQUE,
|
|
430
|
+
operation_type TEXT NOT NULL,
|
|
431
|
+
input_digest TEXT NOT NULL,
|
|
432
|
+
committed_at INTEGER NOT NULL,
|
|
433
|
+
result_json TEXT NOT NULL
|
|
434
|
+
) STRICT;
|
|
435
|
+
`);
|
|
436
|
+
}
|
|
437
|
+
#createV3Schema(database) {
|
|
438
|
+
database.exec(`
|
|
439
|
+
CREATE TABLE IF NOT EXISTS identity_bindings(
|
|
440
|
+
binding_id TEXT PRIMARY KEY,
|
|
441
|
+
instance_id TEXT NOT NULL,
|
|
442
|
+
payload_json TEXT NOT NULL
|
|
443
|
+
) STRICT;
|
|
444
|
+
CREATE INDEX IF NOT EXISTS identity_bindings_instance
|
|
445
|
+
ON identity_bindings(instance_id);
|
|
446
|
+
CREATE TABLE IF NOT EXISTS alias_observations(
|
|
447
|
+
observation_id TEXT PRIMARY KEY,
|
|
448
|
+
logical_workspace_id TEXT NOT NULL,
|
|
449
|
+
evidence_ref TEXT NOT NULL UNIQUE,
|
|
450
|
+
payload_json TEXT NOT NULL
|
|
451
|
+
) STRICT;
|
|
452
|
+
CREATE INDEX IF NOT EXISTS alias_observations_logical
|
|
453
|
+
ON alias_observations(logical_workspace_id);
|
|
454
|
+
CREATE TABLE IF NOT EXISTS projection_records(
|
|
455
|
+
record_id TEXT PRIMARY KEY,
|
|
456
|
+
observation_source TEXT NOT NULL,
|
|
457
|
+
observation_id TEXT NOT NULL,
|
|
458
|
+
relation_delta_digest TEXT NOT NULL,
|
|
459
|
+
payload_json TEXT NOT NULL,
|
|
460
|
+
UNIQUE(observation_source, observation_id)
|
|
461
|
+
) STRICT;
|
|
462
|
+
`);
|
|
463
|
+
}
|
|
464
|
+
#createIndexes(database) {
|
|
465
|
+
database.exec(`
|
|
466
|
+
CREATE INDEX IF NOT EXISTS observations_scope_time
|
|
467
|
+
ON observations(logical_workspace_id, occurred_at);
|
|
468
|
+
CREATE INDEX IF NOT EXISTS workspace_instances_logical_status
|
|
469
|
+
ON workspace_instances(logical_workspace_id, status);
|
|
470
|
+
CREATE INDEX IF NOT EXISTS relation_cards_source_status
|
|
471
|
+
ON relation_cards(source_logical_id, status);
|
|
472
|
+
CREATE INDEX IF NOT EXISTS identity_bindings_instance
|
|
473
|
+
ON identity_bindings(instance_id);
|
|
474
|
+
CREATE INDEX IF NOT EXISTS alias_observations_logical
|
|
475
|
+
ON alias_observations(logical_workspace_id);
|
|
476
|
+
`);
|
|
477
|
+
}
|
|
478
|
+
#recordMigration(database, version) {
|
|
479
|
+
database.prepare('INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)')
|
|
480
|
+
.run(version, Date.now());
|
|
481
|
+
}
|
|
482
|
+
#assertSchemaShape(database, shapes) {
|
|
483
|
+
for (const [table, columns] of Object.entries(shapes)) {
|
|
484
|
+
this.#assertTableShape(database, table, columns);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
#assertTableShape(database, table, expected) {
|
|
488
|
+
const rawColumns = database.prepare(`PRAGMA table_xinfo("${table}")`).all();
|
|
489
|
+
if (rawColumns.some(row => numberColumn(row, 'hidden') !== 0)) {
|
|
490
|
+
throw new Error(`invalid memory ledger hidden column: ${table}`);
|
|
491
|
+
}
|
|
492
|
+
const actual = rawColumns.map(row => ({
|
|
493
|
+
name: stringColumn(row, 'name'),
|
|
494
|
+
type: stringColumn(row, 'type'),
|
|
495
|
+
notnull: numberColumn(row, 'notnull'),
|
|
496
|
+
pk: numberColumn(row, 'pk'),
|
|
497
|
+
}));
|
|
498
|
+
const tableInfo = database.prepare('PRAGMA table_list').all().find(row => (row.name === table && row.type === 'table'));
|
|
499
|
+
if (tableInfo === undefined
|
|
500
|
+
|| numberColumn(tableInfo, 'strict') !== 1
|
|
501
|
+
|| canonicalJson(actual) !== canonicalJson(expected))
|
|
502
|
+
throw new Error(`invalid memory ledger table shape: ${table}`);
|
|
503
|
+
this.#assertTableConstraints(database, table);
|
|
504
|
+
}
|
|
505
|
+
#assertTableConstraints(database, table) {
|
|
506
|
+
const uniqueKeys = database.prepare(`
|
|
507
|
+
SELECT name FROM pragma_index_list(?)
|
|
508
|
+
WHERE "unique" = 1 AND origin = 'u'
|
|
509
|
+
`).all(table).map(row => database.prepare(`
|
|
510
|
+
SELECT name FROM pragma_index_info(?) ORDER BY seqno
|
|
511
|
+
`).all(stringColumn(row, 'name')).map(columnRow => stringColumn(columnRow, 'name')))
|
|
512
|
+
.sort(compareSchemaKeys);
|
|
513
|
+
const expectedUniqueKeys = [...(TABLE_UNIQUE_KEYS[table] ?? [])]
|
|
514
|
+
.map(key => [...key])
|
|
515
|
+
.sort(compareSchemaKeys);
|
|
516
|
+
if (canonicalJson(uniqueKeys) !== canonicalJson(expectedUniqueKeys)) {
|
|
517
|
+
throw new Error(`invalid memory ledger unique constraints: ${table}`);
|
|
518
|
+
}
|
|
519
|
+
const foreignKeys = database.prepare(`
|
|
520
|
+
SELECT
|
|
521
|
+
id, seq, "table" AS target_table, "from" AS from_column, "to" AS to_column,
|
|
522
|
+
on_update, on_delete, match
|
|
523
|
+
FROM pragma_foreign_key_list(?) ORDER BY id, seq
|
|
524
|
+
`).all(table).map(row => ({
|
|
525
|
+
id: numberColumn(row, 'id'),
|
|
526
|
+
seq: numberColumn(row, 'seq'),
|
|
527
|
+
target_table: stringColumn(row, 'target_table'),
|
|
528
|
+
from_column: stringColumn(row, 'from_column'),
|
|
529
|
+
to_column: stringColumn(row, 'to_column'),
|
|
530
|
+
on_update: stringColumn(row, 'on_update'),
|
|
531
|
+
on_delete: stringColumn(row, 'on_delete'),
|
|
532
|
+
match: stringColumn(row, 'match'),
|
|
533
|
+
}));
|
|
534
|
+
const expectedForeignKeys = table === 'relation_evidence'
|
|
535
|
+
? RELATION_EVIDENCE_FOREIGN_KEYS
|
|
536
|
+
: [];
|
|
537
|
+
if (canonicalJson(foreignKeys) !== canonicalJson(expectedForeignKeys)) {
|
|
538
|
+
throw new Error(`invalid memory ledger foreign keys: ${table}`);
|
|
539
|
+
}
|
|
540
|
+
const createRow = database.prepare("SELECT sql FROM sqlite_master WHERE type = 'table' AND name = ?").get(table);
|
|
541
|
+
const hasAutoincrement = createRow !== undefined
|
|
542
|
+
&& /\bAUTOINCREMENT\b/iu.test(stringColumn(createRow, 'sql'));
|
|
543
|
+
if (hasAutoincrement !== (table === 'operation_receipts')) {
|
|
544
|
+
throw new Error(`invalid memory ledger autoincrement: ${table}`);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
#tableExists(database, table) {
|
|
548
|
+
return database.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?").get(table) !== undefined;
|
|
549
|
+
}
|
|
550
|
+
#schemaVersion(database) {
|
|
551
|
+
const row = database.prepare('SELECT MAX(version) AS version FROM schema_migrations').get();
|
|
552
|
+
if (row === undefined || row.version === null)
|
|
553
|
+
return 0;
|
|
554
|
+
return numberColumn(row, 'version');
|
|
555
|
+
}
|
|
556
|
+
#parseRows(rows, schema) {
|
|
557
|
+
return rows.map(row => schema.parse(JSON.parse(stringColumn(row, 'payload_json'))));
|
|
558
|
+
}
|
|
559
|
+
#read(operation) {
|
|
560
|
+
try {
|
|
561
|
+
return operation();
|
|
562
|
+
}
|
|
563
|
+
catch (error) {
|
|
564
|
+
if (error instanceof MemoryLedgerStoreError)
|
|
565
|
+
throw error;
|
|
566
|
+
throw new MemoryLedgerStoreError('STORE_READ_FAILED');
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
#requireDatabase() {
|
|
570
|
+
if (this.#database === undefined)
|
|
571
|
+
throw new MemoryLedgerStoreError('STORE_CLOSED');
|
|
572
|
+
return this.#database;
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
function rollback(database) {
|
|
576
|
+
try {
|
|
577
|
+
database.exec('ROLLBACK');
|
|
578
|
+
}
|
|
579
|
+
catch {
|
|
580
|
+
// Preserve the safe primary store error.
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
function stringColumn(row, key) {
|
|
584
|
+
const value = row?.[key];
|
|
585
|
+
if (typeof value !== 'string')
|
|
586
|
+
throw new MemoryLedgerStoreError('STORE_READ_FAILED');
|
|
587
|
+
return value;
|
|
588
|
+
}
|
|
589
|
+
function numberColumn(row, key) {
|
|
590
|
+
const value = row?.[key];
|
|
591
|
+
if (typeof value === 'number')
|
|
592
|
+
return value;
|
|
593
|
+
if (typeof value === 'bigint')
|
|
594
|
+
return Number(value);
|
|
595
|
+
throw new MemoryLedgerStoreError('STORE_READ_FAILED');
|
|
596
|
+
}
|
|
597
|
+
//# sourceMappingURL=store.js.map
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CandidateSchema } from './store.js';
|
|
2
|
+
/**
|
|
3
|
+
* Bind an automatic proposal to a host-owned target snapshot, taken before model work.
|
|
4
|
+
* This prepares the existing merge contract; it neither grants consent nor writes data.
|
|
5
|
+
* Producers must refresh and recompute after a conflict, never rebase a stale proposal.
|
|
6
|
+
*/
|
|
7
|
+
export function prepareAutomaticCandidate(input, current) {
|
|
8
|
+
if ('written_by' in input || 'expected_revision' in input)
|
|
9
|
+
throw Error('STORE_AUTOMATIC_AUTHORITY');
|
|
10
|
+
// Tombstones still have an observed version. Merge owns whether new evidence may
|
|
11
|
+
// revive a system-retired entry or must leave a user's forgotten entry untouched.
|
|
12
|
+
if (current && (current.entry_id !== input.entry_id || current.kind !== input.kind))
|
|
13
|
+
throw Error('STORE_INVALID_TARGET');
|
|
14
|
+
return CandidateSchema.parse({ ...input, written_by: 'merge', expected_revision: current?.revision ?? 0 });
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=candidates.js.map
|