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,610 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { execFileSync } from 'node:child_process';
|
|
3
|
+
import { chmodSync, closeSync, constants, fstatSync, fsyncSync, linkSync, lstatSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, unlinkSync, writeFileSync } from 'node:fs';
|
|
4
|
+
import { dirname, join, parse, resolve } from 'node:path';
|
|
5
|
+
import { z } from 'zod';
|
|
6
|
+
import { EntryRevisionSchema } from './store.js';
|
|
7
|
+
import { canonicalJson } from '../text/canonical-json.js';
|
|
8
|
+
const MAX_FILE = 8 * 1024 * 1024;
|
|
9
|
+
const MAX_BATCH = 64 * 1024 * 1024;
|
|
10
|
+
const MAX_ENTRIES = 10_000;
|
|
11
|
+
const MARKER = '.nova-memory.json';
|
|
12
|
+
const LOCK_POLL_MS = 25;
|
|
13
|
+
const LOCK_SLEEP = new Int32Array(new SharedArrayBuffer(4));
|
|
14
|
+
const JOURNAL = '.nova-memory-batch.json';
|
|
15
|
+
const markerText = '{"format":"nova-understanding","version":1}\n';
|
|
16
|
+
const hasPath = (path) => lstatSync(path, { throwIfNoEntry: false }) !== undefined;
|
|
17
|
+
const digest = (text) => createHash('sha256').update(text).digest('hex');
|
|
18
|
+
const entryPath = (id) => `entries/${digest(id)}.md`;
|
|
19
|
+
const failure = (code) => new Error(`MEMORY_MARKDOWN_${code}`);
|
|
20
|
+
const journalSchema = z.object({ version: z.literal(1), operationId: z.string().regex(/^[A-Za-z0-9_-]{1,128}$/), baseCommit: z.string().regex(/^[a-f0-9]{40,64}$/).nullable(), files: z.array(z.object({ path: z.string().regex(/^(entries\/[a-f0-9]{64}\.md|\.nova-memory\.json)$/), beforeHash: z.string().regex(/^[a-f0-9]{64}$/).nullable(), after: z.string().max(MAX_FILE) }).strict()).max(MAX_ENTRIES + 1) }).strict();
|
|
21
|
+
function history(revisions) {
|
|
22
|
+
if (revisions.length > 100_000)
|
|
23
|
+
throw failure('LIMIT');
|
|
24
|
+
const result = new Map();
|
|
25
|
+
for (const value of revisions) {
|
|
26
|
+
const revision = EntryRevisionSchema.parse(value);
|
|
27
|
+
const list = result.get(revision.entry_id) ?? [];
|
|
28
|
+
list.push(revision);
|
|
29
|
+
result.set(revision.entry_id, list);
|
|
30
|
+
}
|
|
31
|
+
if (result.size > MAX_ENTRIES)
|
|
32
|
+
throw failure('LIMIT');
|
|
33
|
+
for (const list of result.values()) {
|
|
34
|
+
list.sort((a, b) => a.revision - b.revision);
|
|
35
|
+
for (let i = 0; i < list.length; i++)
|
|
36
|
+
if (list[i].revision !== i + 1 || list[i].supersedes !== (i === 0 ? null : i))
|
|
37
|
+
throw failure('HISTORY');
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
function body(current) { return typeof current.content.text === 'string' ? current.content.text : JSON.stringify(current.content, null, 2); }
|
|
42
|
+
function render(list) {
|
|
43
|
+
const current = list.at(-1);
|
|
44
|
+
// The header is immutable revision history. Only text below the divider is editable.
|
|
45
|
+
const metadata = canonicalJson({ format: 1, revisions: list }).replace(/-->/g, '--\\u003e');
|
|
46
|
+
const text = `<!-- nova-memory-v1\n${metadata}\n-->\n# ${current.kind}\n\n<!-- editable-current -->\n${body(current)}\n`;
|
|
47
|
+
if (Buffer.byteLength(text) > MAX_FILE)
|
|
48
|
+
throw failure('LIMIT');
|
|
49
|
+
return text;
|
|
50
|
+
}
|
|
51
|
+
function decode(text) {
|
|
52
|
+
const match = /^<!-- nova-memory-v1\n([^\n]+)\n-->\n# ([^\n]*)\n\n<!-- editable-current -->\n([\s\S]*)\n$/.exec(text);
|
|
53
|
+
if (!match)
|
|
54
|
+
throw failure('FORMAT');
|
|
55
|
+
const metadata = z.object({ format: z.literal(1), revisions: z.array(EntryRevisionSchema).min(1) }).strict().parse(JSON.parse(match[1]));
|
|
56
|
+
const grouped = history(metadata.revisions);
|
|
57
|
+
if (grouped.size !== 1)
|
|
58
|
+
throw failure('FORMAT');
|
|
59
|
+
const current = metadata.revisions.at(-1);
|
|
60
|
+
if (current.kind !== match[2])
|
|
61
|
+
throw failure('FORMAT');
|
|
62
|
+
const content = typeof current.content.text === 'string' ? { ...current.content, text: match[3] } : z.record(z.string(), z.json()).parse(JSON.parse(match[3]));
|
|
63
|
+
return { revisions: metadata.revisions, content };
|
|
64
|
+
}
|
|
65
|
+
/** Synchronous, worker-owned repository. Caller makes its SQLite outbox durable before prepare(). */
|
|
66
|
+
export class MarkdownRepository {
|
|
67
|
+
root;
|
|
68
|
+
baseline;
|
|
69
|
+
initialized = false;
|
|
70
|
+
lockWaitMs;
|
|
71
|
+
constructor(root, options = {}) { this.root = resolve(root); this.lockWaitMs = options.lockWaitMs ?? 0; }
|
|
72
|
+
/** Initializes an independent, local-only Git repository; never recovers an operation implicitly. */
|
|
73
|
+
initialize() {
|
|
74
|
+
if (this.initialized) {
|
|
75
|
+
this.ensureRoot();
|
|
76
|
+
this.assertSafe(join(this.root, 'entries'), true);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
this.ensureRoot();
|
|
80
|
+
const gitPath = join(this.root, '.git');
|
|
81
|
+
if (hasPath(gitPath)) {
|
|
82
|
+
this.assertSafe(gitPath, true);
|
|
83
|
+
// A nonempty unrelated repository is not a migration target.
|
|
84
|
+
if (!hasPath(join(this.root, MARKER)) && !hasPath(join(this.root, JOURNAL))) {
|
|
85
|
+
const tracked = this.git(['ls-tree', '-r', '--name-only', 'HEAD'], undefined, true);
|
|
86
|
+
if (tracked)
|
|
87
|
+
throw failure('UNOWNED_REPOSITORY');
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
else
|
|
91
|
+
this.git(['init', '--quiet', '--template=']);
|
|
92
|
+
const entries = join(this.root, 'entries');
|
|
93
|
+
if (!hasPath(entries))
|
|
94
|
+
mkdirSync(entries, { mode: 0o700 });
|
|
95
|
+
this.assertSafe(entries, true);
|
|
96
|
+
chmodSync(entries, 0o700);
|
|
97
|
+
this.initialized = true;
|
|
98
|
+
}
|
|
99
|
+
/** Covers the caller's full SQLite read/admit/commit/file-publication cycle. Never steals a live owner's lock. */
|
|
100
|
+
withLock(fn) {
|
|
101
|
+
this.ensureRoot();
|
|
102
|
+
const lock = join(this.root, '.nova-memory.lock');
|
|
103
|
+
const owner = { pid: process.pid, token: randomUUID() };
|
|
104
|
+
const bytes = JSON.stringify(owner);
|
|
105
|
+
const contender = `.nova-lock-${owner.pid}-${owner.token}.tmp`;
|
|
106
|
+
this.writeExclusive(contender, bytes);
|
|
107
|
+
let acquired = false;
|
|
108
|
+
try {
|
|
109
|
+
const deadline = Date.now() + this.lockWaitMs;
|
|
110
|
+
for (let stale = 0;;) {
|
|
111
|
+
try {
|
|
112
|
+
linkSync(join(this.root, contender), lock);
|
|
113
|
+
acquired = true;
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
if (error.code !== 'EEXIST')
|
|
118
|
+
throw error;
|
|
119
|
+
}
|
|
120
|
+
let previous;
|
|
121
|
+
try {
|
|
122
|
+
previous = this.lockOwner(lock);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
if (error.code === 'ENOENT')
|
|
126
|
+
continue;
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
let dead = false;
|
|
130
|
+
try {
|
|
131
|
+
process.kill(previous.owner.pid, 0);
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
dead = error.code === 'ESRCH';
|
|
135
|
+
}
|
|
136
|
+
if (!dead) {
|
|
137
|
+
if (Date.now() >= deadline)
|
|
138
|
+
throw failure('BUSY');
|
|
139
|
+
Atomics.wait(LOCK_SLEEP, 0, 0, LOCK_POLL_MS);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (stale++ >= 1)
|
|
143
|
+
throw failure('BUSY');
|
|
144
|
+
const checked = this.lockOwner(lock);
|
|
145
|
+
if (checked.bytes !== previous.bytes || checked.ino !== previous.ino || checked.dev !== previous.dev)
|
|
146
|
+
throw failure('BUSY');
|
|
147
|
+
unlinkSync(lock);
|
|
148
|
+
}
|
|
149
|
+
this.syncDirectory(this.root);
|
|
150
|
+
return fn();
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
if (acquired) {
|
|
154
|
+
const current = this.lockOwner(lock);
|
|
155
|
+
if (current.bytes !== bytes)
|
|
156
|
+
throw failure('LOCK_CONFLICT');
|
|
157
|
+
unlinkSync(lock);
|
|
158
|
+
}
|
|
159
|
+
if (hasPath(join(this.root, contender))) {
|
|
160
|
+
// Once the canonical link is removed this is again a regular, single-link owned file.
|
|
161
|
+
if (this.readFile(contender) !== bytes)
|
|
162
|
+
throw failure('LOCK_CONFLICT');
|
|
163
|
+
unlinkSync(join(this.root, contender));
|
|
164
|
+
}
|
|
165
|
+
this.syncDirectory(this.root);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
hasSnapshot() { this.initialize(); return hasPath(join(this.root, MARKER)); }
|
|
169
|
+
open(initialRevisions) {
|
|
170
|
+
this.initialize();
|
|
171
|
+
if (this.pendingOperationId())
|
|
172
|
+
throw failure('PENDING_OPERATION');
|
|
173
|
+
if (!hasPath(join(this.root, MARKER))) {
|
|
174
|
+
if (readdirSync(join(this.root, 'entries')).length)
|
|
175
|
+
throw failure('UNOWNED_REPOSITORY');
|
|
176
|
+
this.publish(initialRevisions);
|
|
177
|
+
}
|
|
178
|
+
return this.read();
|
|
179
|
+
}
|
|
180
|
+
read() {
|
|
181
|
+
this.initialize();
|
|
182
|
+
if (this.pendingOperationId())
|
|
183
|
+
throw failure('PENDING_OPERATION');
|
|
184
|
+
const revisions = [];
|
|
185
|
+
const edits = [];
|
|
186
|
+
const baselines = {};
|
|
187
|
+
if (hasPath(join(this.root, MARKER))) {
|
|
188
|
+
if (this.readFile(MARKER) !== markerText)
|
|
189
|
+
throw failure('FORMAT');
|
|
190
|
+
baselines[MARKER] = digest(markerText);
|
|
191
|
+
}
|
|
192
|
+
const names = readdirSync(join(this.root, 'entries')).sort();
|
|
193
|
+
if (names.length > MAX_ENTRIES)
|
|
194
|
+
throw failure('LIMIT');
|
|
195
|
+
const head = this.head();
|
|
196
|
+
const tracked = head ? this.git(['ls-tree', '-r', '--name-only', head, '--', 'entries']).split('\n').filter(Boolean) : [];
|
|
197
|
+
for (const path of tracked)
|
|
198
|
+
if (!names.includes(path.slice('entries/'.length)))
|
|
199
|
+
throw failure('CONFLICT');
|
|
200
|
+
let bytes = 0;
|
|
201
|
+
for (const name of names) {
|
|
202
|
+
const path = `entries/${name}`;
|
|
203
|
+
this.assertSafe(join(this.root, path), false);
|
|
204
|
+
if (!/^[a-f0-9]{64}\.md$/.test(name))
|
|
205
|
+
throw failure('UNSAFE_PATH');
|
|
206
|
+
const text = this.readFile(path);
|
|
207
|
+
bytes += Buffer.byteLength(text);
|
|
208
|
+
if (bytes > MAX_BATCH)
|
|
209
|
+
throw failure('LIMIT');
|
|
210
|
+
const parsed = decode(text);
|
|
211
|
+
const current = parsed.revisions.at(-1);
|
|
212
|
+
if (path !== entryPath(current.entry_id))
|
|
213
|
+
throw failure('FORMAT');
|
|
214
|
+
if (head) {
|
|
215
|
+
if (!tracked.includes(path))
|
|
216
|
+
throw failure('UNADMITTED_EDIT');
|
|
217
|
+
const committed = decode(this.git(['show', `${head}:${path}`]));
|
|
218
|
+
if (canonicalJson(parsed.revisions) !== canonicalJson(committed.revisions))
|
|
219
|
+
throw failure('HISTORY_EDIT');
|
|
220
|
+
}
|
|
221
|
+
revisions.push(...parsed.revisions);
|
|
222
|
+
baselines[path] = digest(text);
|
|
223
|
+
if (canonicalJson(parsed.content) !== canonicalJson(current.content))
|
|
224
|
+
edits.push({ entry_id: current.entry_id, expected_revision: current.revision, content: parsed.content, path, hash: digest(text) });
|
|
225
|
+
}
|
|
226
|
+
revisions.sort((a, b) => a.entry_id < b.entry_id ? -1 : a.entry_id > b.entry_id ? 1 : a.revision - b.revision);
|
|
227
|
+
this.baseline = { ...baselines };
|
|
228
|
+
return { revisions, edits, baselines };
|
|
229
|
+
}
|
|
230
|
+
/** Preserve this baseline across admission of external corrections and the SQLite outbox commit. */
|
|
231
|
+
prepare(revisions, operationId = randomUUID(), baselines = this.baseline, approvedEdits = []) {
|
|
232
|
+
this.initialize();
|
|
233
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(operationId))
|
|
234
|
+
throw failure('OPERATION_ID');
|
|
235
|
+
if (this.pendingOperationId())
|
|
236
|
+
throw failure('PENDING_OPERATION');
|
|
237
|
+
const current = this.read();
|
|
238
|
+
if (baselines && canonicalJson(baselines) !== canonicalJson(current.baselines))
|
|
239
|
+
throw failure('CONFLICT');
|
|
240
|
+
const grouped = history(revisions);
|
|
241
|
+
for (const old of current.revisions) {
|
|
242
|
+
const replacement = grouped.get(old.entry_id)?.find(item => item.revision === old.revision);
|
|
243
|
+
if (!replacement || canonicalJson(replacement) !== canonicalJson(old))
|
|
244
|
+
throw failure('HISTORY');
|
|
245
|
+
}
|
|
246
|
+
for (const edit of current.edits) {
|
|
247
|
+
const accepted = grouped.get(edit.entry_id)?.at(-1);
|
|
248
|
+
const approval = approvedEdits.some(item => item.entry_id === edit.entry_id && item.expected_revision === edit.expected_revision && item.path === edit.path && item.hash === edit.hash);
|
|
249
|
+
if (accepted?.revision !== edit.expected_revision + 1 || accepted.supersedes !== edit.expected_revision || accepted.written_by !== 'user_correction' || accepted.origin !== 'stated' || (!approval && canonicalJson(accepted.content) !== canonicalJson(edit.content)))
|
|
250
|
+
throw failure('UNADMITTED_EDIT');
|
|
251
|
+
}
|
|
252
|
+
const files = [{ path: MARKER, beforeHash: current.baselines[MARKER] ?? null, after: markerText }];
|
|
253
|
+
for (const [id, list] of [...grouped].sort(([a], [b]) => a.localeCompare(b)))
|
|
254
|
+
files.push({ path: entryPath(id), beforeHash: current.baselines[entryPath(id)] ?? null, after: render(list) });
|
|
255
|
+
const journal = { version: 1, operationId, baseCommit: this.head(), files };
|
|
256
|
+
const text = JSON.stringify(journal);
|
|
257
|
+
if (Buffer.byteLength(text) > MAX_BATCH)
|
|
258
|
+
throw failure('LIMIT');
|
|
259
|
+
// Exclusive creation is the inter-process batch lock. No stale lock is ever removed implicitly.
|
|
260
|
+
const stagedJournal = `.nova-batch-${operationId}.tmp`;
|
|
261
|
+
if (hasPath(join(this.root, stagedJournal))) {
|
|
262
|
+
if (this.readFile(stagedJournal, MAX_BATCH) !== text)
|
|
263
|
+
throw failure('CONFLICT');
|
|
264
|
+
}
|
|
265
|
+
else
|
|
266
|
+
this.writeExclusive(stagedJournal, text);
|
|
267
|
+
linkSync(join(this.root, stagedJournal), join(this.root, JOURNAL));
|
|
268
|
+
unlinkSync(join(this.root, stagedJournal));
|
|
269
|
+
this.syncDirectory(this.root);
|
|
270
|
+
return operationId;
|
|
271
|
+
}
|
|
272
|
+
publish(revisions) { const operation = this.prepare(revisions); this.publishPrepared(operation); this.finalize(operation); }
|
|
273
|
+
pendingOperationId() { this.initialize(); return hasPath(join(this.root, JOURNAL)) ? this.journal().operationId : null; }
|
|
274
|
+
publishPrepared(operationId) {
|
|
275
|
+
this.initialize();
|
|
276
|
+
const journal = this.journal();
|
|
277
|
+
if (journal.operationId !== operationId)
|
|
278
|
+
throw failure('OPERATION_ID');
|
|
279
|
+
this.checkBatch(journal);
|
|
280
|
+
const initialHead = this.head();
|
|
281
|
+
if (initialHead !== journal.baseCommit && !this.isOperationCommit(initialHead, journal))
|
|
282
|
+
throw failure('CONFLICT');
|
|
283
|
+
for (const file of journal.files) {
|
|
284
|
+
if (this.fileHash(file.path) === digest(file.after))
|
|
285
|
+
continue;
|
|
286
|
+
this.checkFile(file);
|
|
287
|
+
this.atomicWrite(file, journal.operationId);
|
|
288
|
+
}
|
|
289
|
+
this.checkBatch(journal, true);
|
|
290
|
+
const head = this.head();
|
|
291
|
+
if (head !== journal.baseCommit) {
|
|
292
|
+
if (!this.isOperationCommit(head, journal))
|
|
293
|
+
throw failure('CONFLICT');
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
// Build exactly the owned understanding tree. Untracked files and the batch journal never enter Git.
|
|
297
|
+
const entries = [];
|
|
298
|
+
let marker = '';
|
|
299
|
+
for (const file of journal.files) {
|
|
300
|
+
const blob = this.git(['hash-object', '-w', '--stdin'], file.after).trim();
|
|
301
|
+
if (file.path === MARKER)
|
|
302
|
+
marker = blob;
|
|
303
|
+
else
|
|
304
|
+
entries.push(`100644 blob ${blob}\t${file.path.slice('entries/'.length)}\n`);
|
|
305
|
+
}
|
|
306
|
+
const treeEntries = this.git(['mktree'], entries.sort().join('')).trim();
|
|
307
|
+
const tree = this.git(['mktree'], `100644 blob ${marker}\t${MARKER}\n040000 tree ${treeEntries}\tentries\n`).trim();
|
|
308
|
+
const previousTree = head ? this.git(['rev-parse', `${head}^{tree}`]).trim() : null;
|
|
309
|
+
if (tree !== previousTree) {
|
|
310
|
+
const commit = this.git(['commit-tree', tree, ...(head ? ['-p', head] : [])], `Nova understanding ${operationId}\n`).trim();
|
|
311
|
+
this.checkBatch(journal, true);
|
|
312
|
+
this.git(['update-ref', 'HEAD', commit, head ?? '0'.repeat(40)]);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
this.checkBatch(journal, true);
|
|
316
|
+
}
|
|
317
|
+
finalize(operationId) {
|
|
318
|
+
const journal = this.journal();
|
|
319
|
+
if (journal.operationId !== operationId)
|
|
320
|
+
throw failure('OPERATION_ID');
|
|
321
|
+
this.checkBatch(journal, true);
|
|
322
|
+
if (!this.isOperationCommit(this.head(), journal))
|
|
323
|
+
throw failure('UNCOMMITTED');
|
|
324
|
+
const stagedJournal = join(this.root, `.nova-batch-${operationId}.tmp`);
|
|
325
|
+
if (hasPath(stagedJournal)) {
|
|
326
|
+
const staged = lstatSync(stagedJournal), active = lstatSync(join(this.root, JOURNAL));
|
|
327
|
+
if (staged.ino !== active.ino || staged.dev !== active.dev)
|
|
328
|
+
throw failure('CONFLICT');
|
|
329
|
+
unlinkSync(stagedJournal);
|
|
330
|
+
}
|
|
331
|
+
this.assertSafe(join(this.root, JOURNAL), false);
|
|
332
|
+
unlinkSync(join(this.root, JOURNAL));
|
|
333
|
+
this.syncDirectory(this.root);
|
|
334
|
+
this.baseline = Object.fromEntries(journal.files.map(file => [file.path, digest(file.after)]));
|
|
335
|
+
}
|
|
336
|
+
recover(operationId, expectedRevisions) {
|
|
337
|
+
this.initialize();
|
|
338
|
+
if (expectedRevisions) {
|
|
339
|
+
const journal = this.journal(), expected = history(expectedRevisions);
|
|
340
|
+
if (journal.operationId !== operationId || journal.files.length !== expected.size + 1)
|
|
341
|
+
throw failure('CONFLICT');
|
|
342
|
+
for (const [id, revisions] of expected) {
|
|
343
|
+
const file = journal.files.find(item => item.path === entryPath(id));
|
|
344
|
+
if (file?.after !== render(revisions))
|
|
345
|
+
throw failure('CONFLICT');
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
this.publishPrepared(operationId);
|
|
349
|
+
this.finalize(operationId);
|
|
350
|
+
}
|
|
351
|
+
/** The caller first durably records the retained snapshot and these preimages in SQLite. */
|
|
352
|
+
purge(revisions, operationId, baselines) {
|
|
353
|
+
this.initialize();
|
|
354
|
+
if (this.pendingOperationId())
|
|
355
|
+
throw failure('PENDING_OPERATION');
|
|
356
|
+
if (readdirSync(this.root).some(name => name.startsWith('.nova-') && name.endsWith('.tmp') && !name.startsWith('.nova-lock-')))
|
|
357
|
+
throw failure('PURGE_INCOMPLETE');
|
|
358
|
+
if (!/^[A-Za-z0-9_-]{1,128}$/.test(operationId))
|
|
359
|
+
throw failure('OPERATION_ID');
|
|
360
|
+
const files = new Map([[MARKER, markerText]]);
|
|
361
|
+
for (const [id, list] of history(revisions))
|
|
362
|
+
files.set(entryPath(id), render(list));
|
|
363
|
+
const check = () => {
|
|
364
|
+
for (const [path, expected] of files)
|
|
365
|
+
if (this.fileHash(path) !== digest(expected))
|
|
366
|
+
throw failure('CONFLICT');
|
|
367
|
+
for (const name of readdirSync(join(this.root, 'entries'))) {
|
|
368
|
+
const path = `entries/${name}`;
|
|
369
|
+
if (!files.has(path) && (!Object.hasOwn(baselines, path) || this.fileHash(path) !== baselines[path]))
|
|
370
|
+
throw failure('CONFLICT');
|
|
371
|
+
}
|
|
372
|
+
for (const path of Object.keys(baselines))
|
|
373
|
+
if (path !== MARKER && !/^entries\/[a-f0-9]{64}\.md$/.test(path))
|
|
374
|
+
throw failure('UNSAFE_PATH');
|
|
375
|
+
};
|
|
376
|
+
check();
|
|
377
|
+
// Other refs or a staged user change require explicit reconciliation, never silent removal.
|
|
378
|
+
const branch = this.git(['symbolic-ref', '-q', 'HEAD']).trim();
|
|
379
|
+
if (this.git(['for-each-ref', '--format=%(refname)']).trim().split('\n').filter(Boolean).some(ref => ref !== branch) || this.git(['ls-files']).trim())
|
|
380
|
+
throw failure('CONFLICT');
|
|
381
|
+
// No path outside the known Git directory may participate in object cleanup.
|
|
382
|
+
this.assertGitRefs(join(this.root, '.git'));
|
|
383
|
+
if (hasPath(join(this.root, '.git', 'objects', 'info', 'alternates')))
|
|
384
|
+
throw failure('UNSAFE_PATH');
|
|
385
|
+
const entries = [];
|
|
386
|
+
let marker = '';
|
|
387
|
+
for (const [path, text] of files) {
|
|
388
|
+
const blob = this.git(['hash-object', '-w', '--stdin'], text).trim();
|
|
389
|
+
if (path === MARKER)
|
|
390
|
+
marker = blob;
|
|
391
|
+
else
|
|
392
|
+
entries.push(`100644 blob ${blob}\t${path.slice('entries/'.length)}\n`);
|
|
393
|
+
}
|
|
394
|
+
const treeEntries = this.git(['mktree'], entries.sort().join('')).trim();
|
|
395
|
+
const tree = this.git(['mktree'], `100644 blob ${marker}\t${MARKER}\n040000 tree ${treeEntries}\tentries\n`).trim();
|
|
396
|
+
const head = this.head(), parents = head ? this.git(['rev-list', '--parents', '-n', '1', head]).trim().split(' ') : [];
|
|
397
|
+
if (!head || this.git(['rev-parse', `${head}^{tree}`]).trim() !== tree || parents.length !== 1) {
|
|
398
|
+
const commit = this.git(['commit-tree', tree], `Nova permanent purge ${operationId}\n`).trim();
|
|
399
|
+
check();
|
|
400
|
+
this.git(['update-ref', 'HEAD', commit, head ?? '0'.repeat(40)]);
|
|
401
|
+
}
|
|
402
|
+
check();
|
|
403
|
+
for (const name of readdirSync(join(this.root, 'entries'))) {
|
|
404
|
+
const path = `entries/${name}`;
|
|
405
|
+
if (!files.has(path)) {
|
|
406
|
+
if (this.fileHash(path) !== baselines[path])
|
|
407
|
+
throw failure('CONFLICT');
|
|
408
|
+
unlinkSync(join(this.root, path));
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
this.syncDirectory(join(this.root, 'entries'));
|
|
412
|
+
this.git(['reflog', 'expire', '--expire=now', '--all']);
|
|
413
|
+
this.git(['-c', 'gc.auto=0', 'gc', '--prune=now', '--quiet']);
|
|
414
|
+
if (this.git(['fsck', '--no-reflogs', '--unreachable']).trim())
|
|
415
|
+
throw failure('PURGE_INCOMPLETE');
|
|
416
|
+
this.baseline = Object.fromEntries([...files].map(([path, text]) => [path, digest(text)]));
|
|
417
|
+
}
|
|
418
|
+
journal() {
|
|
419
|
+
const text = this.readFile(JOURNAL, MAX_BATCH, true);
|
|
420
|
+
const value = journalSchema.parse(JSON.parse(text));
|
|
421
|
+
const active = lstatSync(join(this.root, JOURNAL));
|
|
422
|
+
if (active.nlink !== 1) {
|
|
423
|
+
const staged = join(this.root, `.nova-batch-${value.operationId}.tmp`);
|
|
424
|
+
if (active.nlink !== 2 || !hasPath(staged))
|
|
425
|
+
throw failure('UNSAFE_PATH');
|
|
426
|
+
const original = lstatSync(staged);
|
|
427
|
+
if (original.isSymbolicLink() || original.ino !== active.ino || original.dev !== active.dev)
|
|
428
|
+
throw failure('UNSAFE_PATH');
|
|
429
|
+
}
|
|
430
|
+
if (new Set(value.files.map(file => file.path)).size !== value.files.length || value.files.filter(file => file.path === MARKER && file.after === markerText).length !== 1)
|
|
431
|
+
throw failure('FORMAT');
|
|
432
|
+
for (const file of value.files)
|
|
433
|
+
if (file.path !== MARKER) {
|
|
434
|
+
const entry = decode(file.after);
|
|
435
|
+
if (entryPath(entry.revisions.at(-1).entry_id) !== file.path)
|
|
436
|
+
throw failure('FORMAT');
|
|
437
|
+
}
|
|
438
|
+
return value;
|
|
439
|
+
}
|
|
440
|
+
checkFile(file, published = false) {
|
|
441
|
+
const current = this.fileHash(file.path);
|
|
442
|
+
if (current !== digest(file.after) && (published || current !== file.beforeHash))
|
|
443
|
+
throw failure('CONFLICT');
|
|
444
|
+
}
|
|
445
|
+
checkBatch(journal, published = false) {
|
|
446
|
+
for (const file of journal.files)
|
|
447
|
+
this.checkFile(file, published);
|
|
448
|
+
const expected = new Set(journal.files.filter(file => file.path !== MARKER).map(file => file.path));
|
|
449
|
+
for (const name of readdirSync(join(this.root, 'entries')))
|
|
450
|
+
if (!expected.has(`entries/${name}`))
|
|
451
|
+
throw failure('CONFLICT');
|
|
452
|
+
}
|
|
453
|
+
isOperationCommit(head, journal) {
|
|
454
|
+
if (!head)
|
|
455
|
+
return false;
|
|
456
|
+
// Also handles a no-op batch whose tree already matched its base commit.
|
|
457
|
+
const paths = this.git(['ls-tree', '-r', '--name-only', head]).trim().split('\n').filter(Boolean);
|
|
458
|
+
if (paths.length !== journal.files.length)
|
|
459
|
+
return false;
|
|
460
|
+
return journal.files.every(file => paths.includes(file.path) && digest(this.git(['show', `${head}:${file.path}`])) === digest(file.after));
|
|
461
|
+
}
|
|
462
|
+
head() { const value = this.git(['rev-parse', '--verify', 'HEAD'], undefined, true).trim(); return /^[a-f0-9]{40,64}$/.test(value) ? value : null; }
|
|
463
|
+
git(args, input, optional = false) {
|
|
464
|
+
const gitDir = join(this.root, '.git');
|
|
465
|
+
if (hasPath(gitDir)) {
|
|
466
|
+
this.assertSafe(gitDir, true);
|
|
467
|
+
if (hasPath(join(gitDir, 'commondir')) || hasPath(join(gitDir, 'gitdir')))
|
|
468
|
+
throw failure('UNSAFE_PATH');
|
|
469
|
+
// Git must not follow user-data symlinks into another repository or command configuration.
|
|
470
|
+
for (const name of ['config', 'HEAD', 'objects', 'refs', 'index', 'packed-refs', 'shallow'])
|
|
471
|
+
if (hasPath(join(gitDir, name)))
|
|
472
|
+
this.assertSafe(join(gitDir, name), ['objects', 'refs'].includes(name));
|
|
473
|
+
const objects = join(gitDir, 'objects');
|
|
474
|
+
if (hasPath(objects))
|
|
475
|
+
for (const name of readdirSync(objects))
|
|
476
|
+
this.assertSafe(join(objects, name), true);
|
|
477
|
+
const refs = join(gitDir, 'refs');
|
|
478
|
+
if (hasPath(refs))
|
|
479
|
+
this.assertGitRefs(refs);
|
|
480
|
+
}
|
|
481
|
+
const env = { ...process.env };
|
|
482
|
+
for (const key of Object.keys(env))
|
|
483
|
+
if (key.startsWith('GIT_'))
|
|
484
|
+
delete env[key];
|
|
485
|
+
Object.assign(env, { GIT_CONFIG_NOSYSTEM: '1', GIT_CONFIG_GLOBAL: process.platform === 'win32' ? 'NUL' : '/dev/null', GIT_TERMINAL_PROMPT: '0', GIT_AUTHOR_NAME: 'Nova', GIT_AUTHOR_EMAIL: 'nova@localhost', GIT_COMMITTER_NAME: 'Nova', GIT_COMMITTER_EMAIL: 'nova@localhost' });
|
|
486
|
+
try {
|
|
487
|
+
return execFileSync('git', ['-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false', '-c', 'commit.gpgSign=false', '-C', this.root, ...args], { input, encoding: 'utf8', env, timeout: 15_000, maxBuffer: MAX_BATCH, stdio: ['pipe', 'pipe', 'pipe'] });
|
|
488
|
+
}
|
|
489
|
+
catch (error) {
|
|
490
|
+
if (optional)
|
|
491
|
+
return '';
|
|
492
|
+
throw new Error('MEMORY_MARKDOWN_GIT', { cause: error });
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
ensureRoot() {
|
|
496
|
+
this.assertAncestors();
|
|
497
|
+
if (!hasPath(this.root))
|
|
498
|
+
mkdirSync(this.root, { recursive: true, mode: 0o700 });
|
|
499
|
+
this.assertSafe(this.root, true);
|
|
500
|
+
chmodSync(this.root, 0o700);
|
|
501
|
+
}
|
|
502
|
+
lockOwner(path) {
|
|
503
|
+
const stat = lstatSync(path);
|
|
504
|
+
if (stat.isSymbolicLink() || !stat.isFile() || stat.size > 4096)
|
|
505
|
+
throw failure('UNSAFE_PATH');
|
|
506
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
507
|
+
try {
|
|
508
|
+
const bytes = readFileSync(fd, 'utf8');
|
|
509
|
+
const owner = z.object({ pid: z.number().int().positive(), token: z.string().min(1).max(128) }).strict().parse(JSON.parse(bytes));
|
|
510
|
+
return { owner, bytes, ino: stat.ino, dev: stat.dev };
|
|
511
|
+
}
|
|
512
|
+
finally {
|
|
513
|
+
closeSync(fd);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
assertGitRefs(path) {
|
|
517
|
+
for (const name of readdirSync(path)) {
|
|
518
|
+
const child = join(path, name);
|
|
519
|
+
const stat = lstatSync(child);
|
|
520
|
+
this.assertSafe(child, stat.isDirectory());
|
|
521
|
+
if (stat.isDirectory())
|
|
522
|
+
this.assertGitRefs(child);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
assertAncestors() {
|
|
526
|
+
const root = parse(this.root).root;
|
|
527
|
+
let cursor = root;
|
|
528
|
+
for (const component of this.root.slice(root.length).split(/[\\/]/).filter(Boolean)) {
|
|
529
|
+
cursor = join(cursor, component);
|
|
530
|
+
if (!hasPath(cursor))
|
|
531
|
+
continue;
|
|
532
|
+
// macOS has these system aliases; user-controlled symlink ancestors remain forbidden.
|
|
533
|
+
if (process.platform === 'darwin' && ['/tmp', '/var', '/etc'].includes(cursor))
|
|
534
|
+
continue;
|
|
535
|
+
this.assertSafe(cursor, true);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
assertSafe(path, directory) {
|
|
539
|
+
const stat = lstatSync(path);
|
|
540
|
+
if (stat.isSymbolicLink() || (directory ? !stat.isDirectory() : !stat.isFile()) || (!directory && stat.nlink !== 1))
|
|
541
|
+
throw failure('UNSAFE_PATH');
|
|
542
|
+
}
|
|
543
|
+
readFile(relative, limit = MAX_FILE, journalLink = false) {
|
|
544
|
+
const path = join(this.root, relative);
|
|
545
|
+
this.assertSafe(dirname(path), true);
|
|
546
|
+
if (journalLink) {
|
|
547
|
+
const stat = lstatSync(path);
|
|
548
|
+
if (stat.isSymbolicLink() || !stat.isFile())
|
|
549
|
+
throw failure('UNSAFE_PATH');
|
|
550
|
+
}
|
|
551
|
+
else
|
|
552
|
+
this.assertSafe(path, false);
|
|
553
|
+
const fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
554
|
+
try {
|
|
555
|
+
if (fstatSync(fd).size > limit)
|
|
556
|
+
throw failure('LIMIT');
|
|
557
|
+
const text = readFileSync(fd, 'utf8');
|
|
558
|
+
if (Buffer.byteLength(text) > limit)
|
|
559
|
+
throw failure('LIMIT');
|
|
560
|
+
return text;
|
|
561
|
+
}
|
|
562
|
+
finally {
|
|
563
|
+
closeSync(fd);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
fileHash(path) { return hasPath(join(this.root, path)) ? digest(this.readFile(path)) : null; }
|
|
567
|
+
writeExclusive(relative, text) {
|
|
568
|
+
const path = join(this.root, relative);
|
|
569
|
+
this.assertSafe(dirname(path), true);
|
|
570
|
+
const fd = openSync(path, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600);
|
|
571
|
+
try {
|
|
572
|
+
writeFileSync(fd, text);
|
|
573
|
+
fsyncSync(fd);
|
|
574
|
+
}
|
|
575
|
+
finally {
|
|
576
|
+
closeSync(fd);
|
|
577
|
+
}
|
|
578
|
+
this.syncDirectory(dirname(path));
|
|
579
|
+
}
|
|
580
|
+
atomicWrite(file, operationId) {
|
|
581
|
+
const path = join(this.root, file.path);
|
|
582
|
+
const temporary = `.nova-${operationId}-${digest(file.path)}.tmp`;
|
|
583
|
+
const temporaryPath = join(this.root, temporary);
|
|
584
|
+
// The operation ID and expected bytes prove ownership of a crash-left staging file.
|
|
585
|
+
if (hasPath(temporaryPath)) {
|
|
586
|
+
if (this.readFile(temporary) !== file.after)
|
|
587
|
+
throw failure('CONFLICT');
|
|
588
|
+
}
|
|
589
|
+
else
|
|
590
|
+
this.writeExclusive(temporary, file.after);
|
|
591
|
+
this.checkFile(file);
|
|
592
|
+
this.assertSafe(dirname(path), true);
|
|
593
|
+
renameSync(temporaryPath, path);
|
|
594
|
+
chmodSync(path, 0o600);
|
|
595
|
+
this.syncDirectory(dirname(path));
|
|
596
|
+
this.syncDirectory(this.root);
|
|
597
|
+
}
|
|
598
|
+
syncDirectory(path) {
|
|
599
|
+
if (process.platform === 'win32')
|
|
600
|
+
return;
|
|
601
|
+
const fd = openSync(path, constants.O_RDONLY);
|
|
602
|
+
try {
|
|
603
|
+
fsyncSync(fd);
|
|
604
|
+
}
|
|
605
|
+
finally {
|
|
606
|
+
closeSync(fd);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
//# sourceMappingURL=markdown-repository.js.map
|