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,1271 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { addAbortListener } from 'node:events';
|
|
3
|
+
import { constants, lstatSync, realpathSync } from 'node:fs';
|
|
4
|
+
import { open } from 'node:fs/promises';
|
|
5
|
+
import { basename, dirname, isAbsolute, join, resolve, } from 'node:path';
|
|
6
|
+
import { TextDecoder } from 'node:util';
|
|
7
|
+
import { RealClock } from '../core/clock.js';
|
|
8
|
+
import {} from '../storage/native-file-lock.js';
|
|
9
|
+
import { canonicalJsonWithNumberFormatter } from '../text/canonical-json.js';
|
|
10
|
+
import { isWellFormed } from '../text/python-text.js';
|
|
11
|
+
import { unsupportedProjectRootFiles, } from './project-root-file.js';
|
|
12
|
+
import { bumpStateRevision, decodeMaintenanceJournal, decodeState, emptyState, encodeMaintenanceJournal, encodeState, ProjectStateError, projectTimestampNumber, requireProjectBasename, validateState, } from './project-state.js';
|
|
13
|
+
export const PROJECT_STATE_FILE = 'codex-projects-v1.json';
|
|
14
|
+
export const PROJECT_PRE_ACP_BACKUP_FILE = 'codex-projects-v1.pre-acp.json';
|
|
15
|
+
export const PROJECT_TRANSACTION_LOCK_FILE = 'codex-projects-v1.lock';
|
|
16
|
+
export const PROJECT_OWNER_LOCK_FILE = 'codex-projects-v1.owner.lock';
|
|
17
|
+
export const PROJECT_MAINTENANCE_JOURNAL_FILE = 'managed-workspace-maintenance-v1.json';
|
|
18
|
+
export const PROJECT_CODEX_HOMES_DIRECTORY = 'codex-homes';
|
|
19
|
+
export const LEGACY_PROJECT_CODEX_HOMES_DIRECTORY = 'codex-workspaces';
|
|
20
|
+
export const MAX_PROJECT_STATE_BYTES = 1024 * 1024;
|
|
21
|
+
const PROJECT_LOCK_WAIT_SECONDS = 2;
|
|
22
|
+
const PROJECT_LOCK_RETRY_SECONDS = 0.025;
|
|
23
|
+
const MAX_MAINTENANCE_JOURNAL_BYTES = 64 * 1024;
|
|
24
|
+
class TransactionProjectStateError extends ProjectStateError {
|
|
25
|
+
committed;
|
|
26
|
+
constructor(code, committed) {
|
|
27
|
+
super(code);
|
|
28
|
+
this.committed = committed;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const hostProjectRootBrand = Symbol('HostProjectRoot');
|
|
32
|
+
const rootValues = new WeakMap();
|
|
33
|
+
const hostManagedProjectRootBrand = Symbol('HostManagedProjectRoot');
|
|
34
|
+
const managedRootValues = new WeakMap();
|
|
35
|
+
export function hostProjectRootFromConfig(configured) {
|
|
36
|
+
return brandProjectRoot(requireProjectRoot(configured, 'state_permissions', process.platform));
|
|
37
|
+
}
|
|
38
|
+
/** Test-only constructor; it enforces the same canonical owner-only directory contract. */
|
|
39
|
+
export function hostProjectRootForTest(configured, platform = process.platform) {
|
|
40
|
+
return brandProjectRoot(requireProjectRoot(configured, 'state_permissions', platform));
|
|
41
|
+
}
|
|
42
|
+
export function hostManagedProjectRootFromConfig(configured) {
|
|
43
|
+
return brandManagedProjectRoot(requireManagedProjectRoot(configured, process.platform));
|
|
44
|
+
}
|
|
45
|
+
/** Test-only constructor; it enforces the same canonical owner-controlled directory contract. */
|
|
46
|
+
export function hostManagedProjectRootForTest(configured, platform = process.platform) {
|
|
47
|
+
return brandManagedProjectRoot(requireManagedProjectRoot(configured, platform));
|
|
48
|
+
}
|
|
49
|
+
/** Owns retained root handles, locks, transactions and durable state/journal writes. */
|
|
50
|
+
export class ProjectStoreFiles {
|
|
51
|
+
#stateRoot;
|
|
52
|
+
#managedRoot;
|
|
53
|
+
#nativeLocks;
|
|
54
|
+
#rootFiles;
|
|
55
|
+
#recoverStarting;
|
|
56
|
+
#lockClock;
|
|
57
|
+
#onDurabilityStep;
|
|
58
|
+
#platform;
|
|
59
|
+
#activeTransactions = new Set();
|
|
60
|
+
#closeAbort = new AbortController();
|
|
61
|
+
#stateRootHandle = null;
|
|
62
|
+
#stateRootIdentity = null;
|
|
63
|
+
#managedRootHandle = null;
|
|
64
|
+
#managedRootIdentity = null;
|
|
65
|
+
#managedRootPoisoned = false;
|
|
66
|
+
#stateRootPoisoned = false;
|
|
67
|
+
#startupLoaded = false;
|
|
68
|
+
#closed = false;
|
|
69
|
+
#ownerLock = null;
|
|
70
|
+
#closePromise = null;
|
|
71
|
+
constructor(options) {
|
|
72
|
+
this.#stateRoot = projectRootPath(options.stateRoot);
|
|
73
|
+
this.#managedRoot = managedProjectRootPath(options.managedRoot);
|
|
74
|
+
this.#nativeLocks = options.nativeLocks;
|
|
75
|
+
this.#rootFiles = options.rootFiles ?? unsupportedProjectRootFiles;
|
|
76
|
+
this.#recoverStarting = options.live === true;
|
|
77
|
+
this.#lockClock = options.lockClock ?? new RealClock();
|
|
78
|
+
this.#onDurabilityStep = options.onDurabilityStep;
|
|
79
|
+
this.#platform = options.platform ?? process.platform;
|
|
80
|
+
}
|
|
81
|
+
async open() {
|
|
82
|
+
await this.#retainStateRoot();
|
|
83
|
+
await this.#retainManagedRoot();
|
|
84
|
+
this.#probeRootFileAuthority();
|
|
85
|
+
if (this.#recoverStarting) {
|
|
86
|
+
this.#ownerLock = await this.openAndAcquireLock(PROJECT_OWNER_LOCK_FILE);
|
|
87
|
+
await this.revalidateStateRoot();
|
|
88
|
+
await this.migrateLegacyHomes(this.requireStateRootHandle());
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async disposeFailedOpen() {
|
|
92
|
+
const owner = this.#ownerLock;
|
|
93
|
+
this.#ownerLock = null;
|
|
94
|
+
if (owner !== null) {
|
|
95
|
+
await Promise.resolve(owner.release()).catch(() => undefined);
|
|
96
|
+
await owner.file.close().catch(() => undefined);
|
|
97
|
+
}
|
|
98
|
+
if (this.#stateRootHandle)
|
|
99
|
+
this.#rootFiles.unbindDirectory?.(this.#stateRootHandle.fd);
|
|
100
|
+
await this.#stateRootHandle?.close().catch(() => undefined);
|
|
101
|
+
if (this.#managedRootHandle)
|
|
102
|
+
this.#rootFiles.unbindDirectory?.(this.#managedRootHandle.fd);
|
|
103
|
+
await this.#managedRootHandle?.close().catch(() => undefined);
|
|
104
|
+
this.#stateRootHandle = null;
|
|
105
|
+
this.#stateRootIdentity = null;
|
|
106
|
+
this.#managedRootHandle = null;
|
|
107
|
+
this.#managedRootIdentity = null;
|
|
108
|
+
}
|
|
109
|
+
get stateRoot() { return this.#stateRoot; }
|
|
110
|
+
get platform() { return this.#platform; }
|
|
111
|
+
get hasOwnerLock() { return this.#ownerLock !== null; }
|
|
112
|
+
removeManagedTree(root, name, identity) {
|
|
113
|
+
return this.#callRootFile(() => this.#rootFiles.removeTreeAt(root.fd, name, identity));
|
|
114
|
+
}
|
|
115
|
+
close() {
|
|
116
|
+
if (this.#closePromise !== null)
|
|
117
|
+
return this.#closePromise;
|
|
118
|
+
this.#closed = true;
|
|
119
|
+
this.#closeAbort.abort();
|
|
120
|
+
const active = [...this.#activeTransactions];
|
|
121
|
+
this.#closePromise = this.#finishClose(active);
|
|
122
|
+
return this.#closePromise;
|
|
123
|
+
}
|
|
124
|
+
async #finishClose(active) {
|
|
125
|
+
await Promise.all(active);
|
|
126
|
+
const owner = this.#ownerLock;
|
|
127
|
+
this.#ownerLock = null;
|
|
128
|
+
let failed = false;
|
|
129
|
+
if (owner !== null) {
|
|
130
|
+
try {
|
|
131
|
+
await owner.release();
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
failed = true;
|
|
135
|
+
}
|
|
136
|
+
await owner.file.close().catch(() => { failed = true; });
|
|
137
|
+
}
|
|
138
|
+
const root = this.#stateRootHandle;
|
|
139
|
+
const managedRoot = this.#managedRootHandle;
|
|
140
|
+
this.#stateRootHandle = null;
|
|
141
|
+
this.#stateRootIdentity = null;
|
|
142
|
+
this.#managedRootHandle = null;
|
|
143
|
+
this.#managedRootIdentity = null;
|
|
144
|
+
if (root)
|
|
145
|
+
this.#rootFiles.unbindDirectory?.(root.fd);
|
|
146
|
+
await root?.close().catch(() => { failed = true; });
|
|
147
|
+
if (managedRoot)
|
|
148
|
+
this.#rootFiles.unbindDirectory?.(managedRoot.fd);
|
|
149
|
+
await managedRoot?.close().catch(() => { failed = true; });
|
|
150
|
+
if (failed)
|
|
151
|
+
throw new ProjectStateError('state_lock_failed');
|
|
152
|
+
}
|
|
153
|
+
async validateManagedWorkspaceBinding(path, refreshAcl = false) {
|
|
154
|
+
const managed = await this.validateManagedRoot();
|
|
155
|
+
if (!isDirectChild(managed, path)) {
|
|
156
|
+
throw new ProjectStateError('workspace_boundary_changed');
|
|
157
|
+
}
|
|
158
|
+
const root = this.requireManagedRootHandle();
|
|
159
|
+
let file = null;
|
|
160
|
+
try {
|
|
161
|
+
file = await open(path, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
162
|
+
this.#requireWorkspaceMatchesAt(root, basename(path), file, 'workspace_boundary_changed');
|
|
163
|
+
if (refreshAcl) {
|
|
164
|
+
this.protectAt(root, basename(path), file, 'workspace_boundary_changed');
|
|
165
|
+
this.#requireWorkspaceMatchesAt(root, basename(path), file, 'workspace_boundary_changed');
|
|
166
|
+
}
|
|
167
|
+
const info = await file.stat({ bigint: true });
|
|
168
|
+
const canonical = realpathSync(path);
|
|
169
|
+
if (!info.isDirectory()
|
|
170
|
+
|| canonical !== path
|
|
171
|
+
|| !privateDirectoryMetadata(info, this.#platform)
|
|
172
|
+
|| !isDirectChild(managed, canonical))
|
|
173
|
+
throw new Error('unsafe');
|
|
174
|
+
await this.validateManagedRoot();
|
|
175
|
+
this.#requireWorkspaceMatchesAt(root, basename(path), file, 'workspace_boundary_changed');
|
|
176
|
+
return { canonical, identity: fileIdentity(info) };
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
throw new ProjectStateError('workspace_boundary_changed');
|
|
180
|
+
}
|
|
181
|
+
finally {
|
|
182
|
+
await file?.close().catch(() => undefined);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async transaction(operation, options) {
|
|
186
|
+
if (this.#closed)
|
|
187
|
+
throw new ProjectStateError('state_lock_failed');
|
|
188
|
+
const predecessors = [...this.#activeTransactions];
|
|
189
|
+
let complete;
|
|
190
|
+
const ownership = new Promise(resolveOwnership => { complete = resolveOwnership; });
|
|
191
|
+
this.#activeTransactions.add(ownership);
|
|
192
|
+
try {
|
|
193
|
+
await waitForTransactions(predecessors, options?.signal, this.#closeAbort.signal);
|
|
194
|
+
if (this.#closed)
|
|
195
|
+
throw new ProjectStateError('state_lock_failed');
|
|
196
|
+
await this.revalidateStateRoot();
|
|
197
|
+
const mayRecover = this.#recoverStarting && !this.#startupLoaded;
|
|
198
|
+
const held = await this.openAndAcquireLock(PROJECT_TRANSACTION_LOCK_FILE, options?.wait === true || mayRecover, options?.signal);
|
|
199
|
+
let releaseFailure = false;
|
|
200
|
+
let committed = false;
|
|
201
|
+
try {
|
|
202
|
+
await this.revalidateStateRoot();
|
|
203
|
+
const shouldRecover = this.#recoverStarting && !this.#startupLoaded;
|
|
204
|
+
const [state, recovered] = await this.#loadState(shouldRecover);
|
|
205
|
+
const [value, changed] = await operation(state);
|
|
206
|
+
if (recovered || changed)
|
|
207
|
+
bumpStateRevision(state);
|
|
208
|
+
validateState(state);
|
|
209
|
+
if (recovered || changed) {
|
|
210
|
+
await this.#saveState(state, () => { committed = true; });
|
|
211
|
+
}
|
|
212
|
+
await this.revalidateStateRoot();
|
|
213
|
+
this.#startupLoaded = true;
|
|
214
|
+
return value;
|
|
215
|
+
}
|
|
216
|
+
catch (error) {
|
|
217
|
+
if (committed && error instanceof ProjectStateError) {
|
|
218
|
+
throw new TransactionProjectStateError(error.code, true);
|
|
219
|
+
}
|
|
220
|
+
throw error;
|
|
221
|
+
}
|
|
222
|
+
finally {
|
|
223
|
+
try {
|
|
224
|
+
await held.release();
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
releaseFailure = true;
|
|
228
|
+
}
|
|
229
|
+
await held.file.close().catch(() => { releaseFailure = true; });
|
|
230
|
+
if (releaseFailure) {
|
|
231
|
+
throw new TransactionProjectStateError('state_lock_failed', committed);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
finally {
|
|
236
|
+
this.#activeTransactions.delete(ownership);
|
|
237
|
+
complete();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
async #retainStateRoot() {
|
|
241
|
+
const retained = await openStateRoot(this.#stateRoot, this.#platform);
|
|
242
|
+
this.#stateRootHandle = retained.file;
|
|
243
|
+
this.#stateRootIdentity = retained.identity;
|
|
244
|
+
this.#rootFiles.bindDirectory?.(retained.file.fd, this.#stateRoot);
|
|
245
|
+
}
|
|
246
|
+
async #retainManagedRoot() {
|
|
247
|
+
const retained = await openManagedRoot(this.#managedRoot, this.#platform);
|
|
248
|
+
this.#managedRootHandle = retained.file;
|
|
249
|
+
this.#managedRootIdentity = retained.identity;
|
|
250
|
+
this.#rootFiles.bindDirectory?.(retained.file.fd, this.#managedRoot);
|
|
251
|
+
}
|
|
252
|
+
#probeRootFileAuthority() {
|
|
253
|
+
const stateRoot = this.requireStateRootHandle();
|
|
254
|
+
const managedRoot = this.requireManagedRootHandle();
|
|
255
|
+
if (this.#callRootFile(() => this.#rootFiles.probe(stateRoot.fd)).status !== 'ok'
|
|
256
|
+
|| this.#callRootFile(() => this.#rootFiles.probe(managedRoot.fd)).status !== 'ok')
|
|
257
|
+
throw new ProjectStateError('state_permissions');
|
|
258
|
+
}
|
|
259
|
+
async revalidateStateRoot() {
|
|
260
|
+
if (this.#stateRootPoisoned)
|
|
261
|
+
throw new ProjectStateError('state_permissions');
|
|
262
|
+
const retained = this.#stateRootHandle;
|
|
263
|
+
const expected = this.#stateRootIdentity;
|
|
264
|
+
if (retained === null || expected === null) {
|
|
265
|
+
this.#stateRootPoisoned = true;
|
|
266
|
+
throw new ProjectStateError('state_permissions');
|
|
267
|
+
}
|
|
268
|
+
let current = null;
|
|
269
|
+
try {
|
|
270
|
+
const retainedInfo = await retained.stat({ bigint: true });
|
|
271
|
+
if (!stateRootMatches(retainedInfo, expected, this.#platform))
|
|
272
|
+
throw new Error('retained root changed');
|
|
273
|
+
current = await open(this.#stateRoot, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
274
|
+
const currentInfo = await current.stat({ bigint: true });
|
|
275
|
+
const canonical = realpathSync(this.#stateRoot);
|
|
276
|
+
if (!stateRootMatches(currentInfo, expected, this.#platform) || canonical !== expected.canonical) {
|
|
277
|
+
throw new Error('state root identity changed');
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
this.#stateRootPoisoned = true;
|
|
282
|
+
throw new ProjectStateError('state_permissions');
|
|
283
|
+
}
|
|
284
|
+
finally {
|
|
285
|
+
await current?.close().catch(() => undefined);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async validateManagedRoot() {
|
|
289
|
+
if (this.#managedRootPoisoned)
|
|
290
|
+
throw new ProjectStateError('managed_root_unsafe');
|
|
291
|
+
const retained = this.#managedRootHandle;
|
|
292
|
+
const expected = this.#managedRootIdentity;
|
|
293
|
+
if (retained === null || expected === null) {
|
|
294
|
+
this.#managedRootPoisoned = true;
|
|
295
|
+
throw new ProjectStateError('managed_root_unsafe');
|
|
296
|
+
}
|
|
297
|
+
let current = null;
|
|
298
|
+
try {
|
|
299
|
+
const retainedInfo = await retained.stat({ bigint: true });
|
|
300
|
+
if (!retainedInfo.isDirectory()
|
|
301
|
+
|| !managedDirectoryMetadata(retainedInfo, this.#platform)
|
|
302
|
+
|| !sameFileIdentity(expected, fileIdentity(retainedInfo)))
|
|
303
|
+
throw new Error('retained managed root changed');
|
|
304
|
+
current = await open(this.#managedRoot, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
305
|
+
const currentInfo = await current.stat({ bigint: true });
|
|
306
|
+
const canonical = realpathSync(this.#managedRoot);
|
|
307
|
+
if (canonical !== this.#managedRoot
|
|
308
|
+
|| !currentInfo.isDirectory()
|
|
309
|
+
|| !managedDirectoryMetadata(currentInfo, this.#platform)
|
|
310
|
+
|| !sameFileIdentity(expected, fileIdentity(currentInfo)))
|
|
311
|
+
throw new Error('managed root identity changed');
|
|
312
|
+
return canonical;
|
|
313
|
+
}
|
|
314
|
+
catch {
|
|
315
|
+
this.#managedRootPoisoned = true;
|
|
316
|
+
throw new ProjectStateError('managed_root_unsafe');
|
|
317
|
+
}
|
|
318
|
+
finally {
|
|
319
|
+
await current?.close().catch(() => undefined);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
requireStateRootHandle() {
|
|
323
|
+
if (this.#stateRootHandle === null)
|
|
324
|
+
throw new ProjectStateError('state_permissions');
|
|
325
|
+
return this.#stateRootHandle;
|
|
326
|
+
}
|
|
327
|
+
requireManagedRootHandle() {
|
|
328
|
+
if (this.#managedRootHandle === null)
|
|
329
|
+
throw new ProjectStateError('managed_root_unsafe');
|
|
330
|
+
return this.#managedRootHandle;
|
|
331
|
+
}
|
|
332
|
+
#callRootFile(operation) {
|
|
333
|
+
try {
|
|
334
|
+
const result = operation();
|
|
335
|
+
return validProjectRootFileResult(result) ? result : { status: 'failed' };
|
|
336
|
+
}
|
|
337
|
+
catch {
|
|
338
|
+
return { status: 'failed' };
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
#callRootFileLookup(operation) {
|
|
342
|
+
try {
|
|
343
|
+
const result = operation();
|
|
344
|
+
return validProjectRootFileLookupResult(result) ? result : { status: 'failed' };
|
|
345
|
+
}
|
|
346
|
+
catch {
|
|
347
|
+
return { status: 'failed' };
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
#callRootFileCreate(operation) {
|
|
351
|
+
try {
|
|
352
|
+
const result = operation();
|
|
353
|
+
return validProjectRootFileCreateResult(result) ? result : { status: 'failed' };
|
|
354
|
+
}
|
|
355
|
+
catch {
|
|
356
|
+
return { status: 'failed' };
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
requireMatchesAt(root, name, child, code) {
|
|
360
|
+
requireProjectBasename(name, code);
|
|
361
|
+
const result = this.#callRootFile(() => this.#rootFiles.matchesAt(root.fd, name, child.fd));
|
|
362
|
+
if (result.status !== 'ok')
|
|
363
|
+
throw new ProjectStateError(code);
|
|
364
|
+
}
|
|
365
|
+
#requireWorkspaceMatchesAt(root, name, child, code) {
|
|
366
|
+
requireProjectBasename(name, code);
|
|
367
|
+
const result = this.#callRootFile(() => (this.#rootFiles.matchesWorkspaceAt?.(root.fd, name, child.fd)
|
|
368
|
+
?? this.#rootFiles.matchesAt(root.fd, name, child.fd)));
|
|
369
|
+
if (result.status !== 'ok')
|
|
370
|
+
throw new ProjectStateError(code);
|
|
371
|
+
}
|
|
372
|
+
lookupAt(root, name, code) {
|
|
373
|
+
requireProjectBasename(name, code);
|
|
374
|
+
const result = this.#callRootFileLookup(() => this.#rootFiles.lookupAt(root.fd, name));
|
|
375
|
+
if (result.status === 'unsupported' || result.status === 'failed') {
|
|
376
|
+
throw new ProjectStateError(code);
|
|
377
|
+
}
|
|
378
|
+
return result;
|
|
379
|
+
}
|
|
380
|
+
lookupWorkspaceAt(root, name, code) {
|
|
381
|
+
requireProjectBasename(name, code);
|
|
382
|
+
const result = this.#callRootFileLookup(() => (this.#rootFiles.lookupWorkspaceAt?.(root.fd, name)
|
|
383
|
+
?? this.#rootFiles.lookupAt(root.fd, name)));
|
|
384
|
+
if (result.status === 'unsupported' || result.status === 'failed') {
|
|
385
|
+
throw new ProjectStateError(code);
|
|
386
|
+
}
|
|
387
|
+
return result;
|
|
388
|
+
}
|
|
389
|
+
#mkdirAt(root, name, code) {
|
|
390
|
+
requireProjectBasename(name, code);
|
|
391
|
+
const result = this.#callRootFileCreate(() => this.#rootFiles.mkdirAt(root.fd, name));
|
|
392
|
+
if (result.status === 'unsupported' || result.status === 'failed') {
|
|
393
|
+
throw new ProjectStateError(code);
|
|
394
|
+
}
|
|
395
|
+
return result;
|
|
396
|
+
}
|
|
397
|
+
mkdirPrivateAt(root, name, code) {
|
|
398
|
+
if (this.#platform !== 'win32')
|
|
399
|
+
return this.#mkdirAt(root, name, code);
|
|
400
|
+
requireProjectBasename(name, code);
|
|
401
|
+
const result = this.#callRootFileCreate(() => this.#rootFiles.mkdirPrivateAt?.(root.fd, name) ?? { status: 'unsupported' });
|
|
402
|
+
if (result.status === 'unsupported' || result.status === 'failed') {
|
|
403
|
+
throw new ProjectStateError(code);
|
|
404
|
+
}
|
|
405
|
+
return result;
|
|
406
|
+
}
|
|
407
|
+
protectAt(root, name, child, code) {
|
|
408
|
+
if (this.#platform !== 'win32')
|
|
409
|
+
return;
|
|
410
|
+
requireProjectBasename(name, code);
|
|
411
|
+
const result = this.#callRootFile(() => this.#rootFiles.protectAt?.(root.fd, name, child.fd) ?? { status: 'unsupported' });
|
|
412
|
+
if (result.status !== 'ok')
|
|
413
|
+
throw new ProjectStateError(code);
|
|
414
|
+
}
|
|
415
|
+
#createFileAt(root, name, exclusive, code) {
|
|
416
|
+
requireProjectBasename(name, code);
|
|
417
|
+
const result = this.#callRootFileCreate(() => this.#rootFiles.createFileAt(root.fd, name, exclusive));
|
|
418
|
+
if (result.status === 'unsupported'
|
|
419
|
+
|| result.status === 'failed')
|
|
420
|
+
throw new ProjectStateError(code);
|
|
421
|
+
if (exclusive && result.status !== 'ok')
|
|
422
|
+
throw new ProjectStateError(code);
|
|
423
|
+
if (!exclusive && result.status !== 'ok' && result.status !== 'exists') {
|
|
424
|
+
throw new ProjectStateError(code);
|
|
425
|
+
}
|
|
426
|
+
return result;
|
|
427
|
+
}
|
|
428
|
+
#renameAt(root, from, to) {
|
|
429
|
+
requireProjectBasename(from, 'state_write_failed');
|
|
430
|
+
requireProjectBasename(to, 'state_write_failed');
|
|
431
|
+
const result = this.#callRootFile(() => this.#rootFiles.renameAt(root.fd, from, to));
|
|
432
|
+
if (result.status !== 'ok')
|
|
433
|
+
throw new ProjectStateError('state_write_failed');
|
|
434
|
+
}
|
|
435
|
+
renameManagedNoReplace(root, from, to, expected) {
|
|
436
|
+
requireProjectBasename(from, 'workspace_boundary_changed');
|
|
437
|
+
requireProjectBasename(to, 'workspace_boundary_changed');
|
|
438
|
+
const result = this.#callRootFile(() => this.#rootFiles.renameNoReplaceAt?.(root.fd, from, to, expected)
|
|
439
|
+
?? { status: 'unsupported' });
|
|
440
|
+
if (result.status === 'unsupported' || result.status === 'failed') {
|
|
441
|
+
throw new ProjectStateError('workspace_boundary_changed');
|
|
442
|
+
}
|
|
443
|
+
return result;
|
|
444
|
+
}
|
|
445
|
+
async syncManagedRoot() {
|
|
446
|
+
await this.validateManagedRoot();
|
|
447
|
+
const root = this.requireManagedRootHandle();
|
|
448
|
+
const result = this.#callRootFile(() => this.#rootFiles.syncDirectory?.(root.fd) ?? { status: 'unsupported' });
|
|
449
|
+
if (result.status !== 'ok')
|
|
450
|
+
throw new ProjectStateError('workspace_boundary_changed');
|
|
451
|
+
await this.validateManagedRoot();
|
|
452
|
+
}
|
|
453
|
+
async migrateLegacyHomes(root) {
|
|
454
|
+
const current = this.lookupAt(root, PROJECT_CODEX_HOMES_DIRECTORY, 'state_permissions');
|
|
455
|
+
if (current.status === 'ok')
|
|
456
|
+
return;
|
|
457
|
+
if (current.status !== 'missing')
|
|
458
|
+
throw new ProjectStateError('state_permissions');
|
|
459
|
+
const legacy = this.lookupAt(root, LEGACY_PROJECT_CODEX_HOMES_DIRECTORY, 'state_permissions');
|
|
460
|
+
if (legacy.status === 'missing')
|
|
461
|
+
return;
|
|
462
|
+
if (legacy.status !== 'ok')
|
|
463
|
+
throw new ProjectStateError('state_permissions');
|
|
464
|
+
let legacyDirectory = null;
|
|
465
|
+
try {
|
|
466
|
+
legacyDirectory = await this.ensurePrivateDirectoryAt(root, this.#stateRoot, LEGACY_PROJECT_CODEX_HOMES_DIRECTORY);
|
|
467
|
+
if (!sameFileIdentity(legacy.identity, legacyDirectory.binding.identity)) {
|
|
468
|
+
throw new ProjectStateError('state_permissions');
|
|
469
|
+
}
|
|
470
|
+
const currentAgain = this.lookupAt(root, PROJECT_CODEX_HOMES_DIRECTORY, 'state_permissions');
|
|
471
|
+
if (currentAgain.status === 'ok')
|
|
472
|
+
return;
|
|
473
|
+
if (currentAgain.status !== 'missing')
|
|
474
|
+
throw new ProjectStateError('state_permissions');
|
|
475
|
+
await this.revalidateStateRoot();
|
|
476
|
+
this.requireMatchesAt(root, LEGACY_PROJECT_CODEX_HOMES_DIRECTORY, legacyDirectory.file, 'state_permissions');
|
|
477
|
+
const renamed = this.#callRootFile(() => this.#rootFiles.renameAt(root.fd, LEGACY_PROJECT_CODEX_HOMES_DIRECTORY, PROJECT_CODEX_HOMES_DIRECTORY));
|
|
478
|
+
if (renamed.status !== 'ok')
|
|
479
|
+
throw new ProjectStateError('state_permissions');
|
|
480
|
+
this.requireMatchesAt(root, PROJECT_CODEX_HOMES_DIRECTORY, legacyDirectory.file, 'state_permissions');
|
|
481
|
+
const legacyAfter = this.lookupAt(root, LEGACY_PROJECT_CODEX_HOMES_DIRECTORY, 'state_permissions');
|
|
482
|
+
if (legacyAfter.status !== 'missing')
|
|
483
|
+
throw new ProjectStateError('state_permissions');
|
|
484
|
+
await this.revalidateStateRoot();
|
|
485
|
+
}
|
|
486
|
+
finally {
|
|
487
|
+
await legacyDirectory?.file.close().catch(() => undefined);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
unlinkAt(root, name, expected, kind, code) {
|
|
491
|
+
requireProjectBasename(name, code);
|
|
492
|
+
const result = this.#callRootFile(() => this.#rootFiles.unlinkAt(root.fd, name, expected, kind));
|
|
493
|
+
if (result.status === 'unsupported' || result.status === 'failed') {
|
|
494
|
+
throw new ProjectStateError(code);
|
|
495
|
+
}
|
|
496
|
+
return result;
|
|
497
|
+
}
|
|
498
|
+
async ensurePrivateDirectoryAt(root, rootPath, name, exclusive = false, create = true) {
|
|
499
|
+
requireProjectBasename(name, 'state_permissions');
|
|
500
|
+
const created = create ? this.mkdirPrivateAt(root, name, 'state_permissions') : { status: 'exists' };
|
|
501
|
+
if (created.status !== 'ok' && (exclusive || created.status !== 'exists')) {
|
|
502
|
+
throw new ProjectStateError('state_permissions');
|
|
503
|
+
}
|
|
504
|
+
const createdIdentity = created.status === 'ok' ? created.identity : null;
|
|
505
|
+
const path = join(rootPath, name);
|
|
506
|
+
let file = null;
|
|
507
|
+
try {
|
|
508
|
+
file = await open(path, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
509
|
+
this.requireMatchesAt(root, name, file, 'state_permissions');
|
|
510
|
+
const initialInfo = await file.stat({ bigint: true });
|
|
511
|
+
const initialIdentity = fileIdentity(initialInfo);
|
|
512
|
+
if (createdIdentity !== null && !sameFileIdentity(createdIdentity, initialIdentity)) {
|
|
513
|
+
throw new ProjectStateError('state_permissions');
|
|
514
|
+
}
|
|
515
|
+
this.protectAt(root, name, file, 'state_permissions');
|
|
516
|
+
if (created.status === 'ok' && this.#platform !== 'win32')
|
|
517
|
+
await file.chmod(0o700);
|
|
518
|
+
const info = await file.stat({ bigint: true });
|
|
519
|
+
const identity = fileIdentity(info);
|
|
520
|
+
const canonical = realpathSync(path);
|
|
521
|
+
if (!info.isDirectory()
|
|
522
|
+
|| !privateDirectoryMetadata(info, this.#platform)
|
|
523
|
+
|| canonical !== path
|
|
524
|
+
|| !isDirectChild(rootPath, canonical))
|
|
525
|
+
throw new ProjectStateError('state_permissions');
|
|
526
|
+
this.requireMatchesAt(root, name, file, 'state_permissions');
|
|
527
|
+
return { file, binding: { canonical, identity } };
|
|
528
|
+
}
|
|
529
|
+
catch (error) {
|
|
530
|
+
await file?.close().catch(() => undefined);
|
|
531
|
+
if (createdIdentity !== null) {
|
|
532
|
+
try {
|
|
533
|
+
this.unlinkAt(root, name, createdIdentity, 'directory', 'state_permissions');
|
|
534
|
+
}
|
|
535
|
+
catch {
|
|
536
|
+
// A newly-created directory is removed only through an exact descriptor-relative match.
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
if (error instanceof ProjectStateError)
|
|
540
|
+
throw error;
|
|
541
|
+
throw new ProjectStateError('state_permissions');
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
async openAndAcquireLock(fileName, wait = false, signal) {
|
|
545
|
+
const root = this.requireStateRootHandle();
|
|
546
|
+
requireProjectBasename(fileName, 'state_permissions');
|
|
547
|
+
const created = this.#createFileAt(root, fileName, false, 'state_permissions');
|
|
548
|
+
const createdIdentity = created.status === 'ok' ? created.identity : null;
|
|
549
|
+
const file = await openValidatedRegularFile(join(this.#stateRoot, fileName), constants.O_RDWR | noFollowFlag(), null, this.#platform);
|
|
550
|
+
const waitSignal = signal === undefined
|
|
551
|
+
? this.#closeAbort.signal
|
|
552
|
+
: AbortSignal.any([signal, this.#closeAbort.signal]);
|
|
553
|
+
let deadline = 0;
|
|
554
|
+
try {
|
|
555
|
+
this.requireMatchesAt(root, fileName, file, 'state_permissions');
|
|
556
|
+
if (createdIdentity !== null) {
|
|
557
|
+
const opened = await file.stat({ bigint: true });
|
|
558
|
+
if (!sameFileIdentity(createdIdentity, fileIdentity(opened))) {
|
|
559
|
+
throw new ProjectStateError('state_permissions');
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
deadline = readClock(this.#lockClock) + PROJECT_LOCK_WAIT_SECONDS;
|
|
563
|
+
while (true) {
|
|
564
|
+
if (waitSignal.aborted)
|
|
565
|
+
throw projectAbortError();
|
|
566
|
+
const result = this.#nativeLocks.acquire(file.fd);
|
|
567
|
+
if (!validNativeLockResult(result))
|
|
568
|
+
throw new ProjectStateError('state_lock_failed');
|
|
569
|
+
if (result.status === 'acquired') {
|
|
570
|
+
if (!waitSignal.aborted) {
|
|
571
|
+
try {
|
|
572
|
+
await this.revalidateStateRoot();
|
|
573
|
+
this.requireMatchesAt(root, fileName, file, 'state_permissions');
|
|
574
|
+
return { file, release: result.release };
|
|
575
|
+
}
|
|
576
|
+
catch (error) {
|
|
577
|
+
try {
|
|
578
|
+
await result.release();
|
|
579
|
+
}
|
|
580
|
+
catch { /* preserve the root failure */ }
|
|
581
|
+
throw error;
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
try {
|
|
585
|
+
await result.release();
|
|
586
|
+
}
|
|
587
|
+
catch {
|
|
588
|
+
throw new ProjectStateError('state_lock_failed');
|
|
589
|
+
}
|
|
590
|
+
throw projectAbortError();
|
|
591
|
+
}
|
|
592
|
+
if (result.status !== 'busy')
|
|
593
|
+
throw new ProjectStateError('state_lock_failed');
|
|
594
|
+
if (!wait)
|
|
595
|
+
throw new ProjectStateError('state_busy');
|
|
596
|
+
const remaining = deadline - readClock(this.#lockClock);
|
|
597
|
+
if (remaining <= 0)
|
|
598
|
+
throw new ProjectStateError('state_busy');
|
|
599
|
+
await this.#lockClock.sleep(Math.min(PROJECT_LOCK_RETRY_SECONDS, remaining), waitSignal);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
catch (error) {
|
|
603
|
+
await file.close().catch(() => undefined);
|
|
604
|
+
if (error instanceof ProjectStateError || isAbortError(error))
|
|
605
|
+
throw error;
|
|
606
|
+
throw new ProjectStateError('state_lock_failed');
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
async #loadState(recoverStarting) {
|
|
610
|
+
await this.revalidateStateRoot();
|
|
611
|
+
const root = this.requireStateRootHandle();
|
|
612
|
+
const path = join(this.#stateRoot, PROJECT_STATE_FILE);
|
|
613
|
+
let file;
|
|
614
|
+
try {
|
|
615
|
+
file = await openValidatedRegularFile(path, constants.O_RDONLY | nonblockFlag() | noFollowFlag(), null, this.#platform);
|
|
616
|
+
}
|
|
617
|
+
catch (error) {
|
|
618
|
+
if (isNodeError(error, 'ENOENT')) {
|
|
619
|
+
await this.revalidateStateRoot();
|
|
620
|
+
if (this.lookupAt(root, PROJECT_STATE_FILE, 'state_permissions').status !== 'missing') {
|
|
621
|
+
throw new ProjectStateError('state_permissions');
|
|
622
|
+
}
|
|
623
|
+
return [emptyState(), false];
|
|
624
|
+
}
|
|
625
|
+
throw error;
|
|
626
|
+
}
|
|
627
|
+
try {
|
|
628
|
+
await this.revalidateStateRoot();
|
|
629
|
+
this.requireMatchesAt(root, PROJECT_STATE_FILE, file, 'state_permissions');
|
|
630
|
+
const info = await file.stat();
|
|
631
|
+
if (info.size > MAX_PROJECT_STATE_BYTES)
|
|
632
|
+
throw new ProjectStateError('state_too_large');
|
|
633
|
+
const buffer = Buffer.alloc(MAX_PROJECT_STATE_BYTES + 1);
|
|
634
|
+
let bytesRead = 0;
|
|
635
|
+
while (bytesRead < buffer.length) {
|
|
636
|
+
const read = await file.read(buffer, bytesRead, buffer.length - bytesRead, bytesRead);
|
|
637
|
+
if (read.bytesRead === 0)
|
|
638
|
+
break;
|
|
639
|
+
bytesRead += read.bytesRead;
|
|
640
|
+
}
|
|
641
|
+
if (bytesRead > MAX_PROJECT_STATE_BYTES)
|
|
642
|
+
throw new ProjectStateError('state_too_large');
|
|
643
|
+
let parsed;
|
|
644
|
+
try {
|
|
645
|
+
const text = new TextDecoder('utf-8', { fatal: true }).decode(buffer.subarray(0, bytesRead));
|
|
646
|
+
parsed = JSON.parse(text);
|
|
647
|
+
}
|
|
648
|
+
catch {
|
|
649
|
+
throw new ProjectStateError('state_corrupt');
|
|
650
|
+
}
|
|
651
|
+
const state = decodeState(parsed);
|
|
652
|
+
const legacy = parsed.version === 1;
|
|
653
|
+
if (legacy)
|
|
654
|
+
await this.#backupLegacyState(buffer.subarray(0, bytesRead));
|
|
655
|
+
let recovered = legacy;
|
|
656
|
+
if (recoverStarting) {
|
|
657
|
+
for (const [sessionId, session] of state.sessions) {
|
|
658
|
+
if (session.state === 'starting' && session.backend_session_id === null) {
|
|
659
|
+
state.sessions.set(sessionId, Object.freeze({ ...session, state: 'unavailable' }));
|
|
660
|
+
recovered = true;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return [state, recovered];
|
|
665
|
+
}
|
|
666
|
+
catch (error) {
|
|
667
|
+
if (error instanceof ProjectStateError)
|
|
668
|
+
throw error;
|
|
669
|
+
throw new ProjectStateError('state_corrupt');
|
|
670
|
+
}
|
|
671
|
+
finally {
|
|
672
|
+
let closeFailed = false;
|
|
673
|
+
await file.close().catch(() => { closeFailed = true; });
|
|
674
|
+
if (closeFailed)
|
|
675
|
+
throw new ProjectStateError('state_corrupt');
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
async #readMaintenanceJournal() {
|
|
679
|
+
const root = this.requireStateRootHandle();
|
|
680
|
+
const path = join(this.#stateRoot, PROJECT_MAINTENANCE_JOURNAL_FILE);
|
|
681
|
+
let file;
|
|
682
|
+
try {
|
|
683
|
+
file = await openValidatedRegularFile(path, constants.O_RDONLY | nonblockFlag() | noFollowFlag(), null, this.#platform);
|
|
684
|
+
}
|
|
685
|
+
catch (error) {
|
|
686
|
+
if (isNodeError(error, 'ENOENT')) {
|
|
687
|
+
if (this.lookupAt(root, PROJECT_MAINTENANCE_JOURNAL_FILE, 'state_permissions').status === 'missing')
|
|
688
|
+
return null;
|
|
689
|
+
}
|
|
690
|
+
throw new ProjectStateError('state_permissions');
|
|
691
|
+
}
|
|
692
|
+
try {
|
|
693
|
+
await this.revalidateStateRoot();
|
|
694
|
+
this.requireMatchesAt(root, PROJECT_MAINTENANCE_JOURNAL_FILE, file, 'state_permissions');
|
|
695
|
+
const info = await file.stat({ bigint: true });
|
|
696
|
+
if (Number(info.size) > MAX_MAINTENANCE_JOURNAL_BYTES) {
|
|
697
|
+
throw new ProjectStateError('state_corrupt');
|
|
698
|
+
}
|
|
699
|
+
const raw = await file.readFile();
|
|
700
|
+
if (raw.byteLength > MAX_MAINTENANCE_JOURNAL_BYTES) {
|
|
701
|
+
throw new ProjectStateError('state_corrupt');
|
|
702
|
+
}
|
|
703
|
+
let parsed;
|
|
704
|
+
try {
|
|
705
|
+
parsed = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(raw));
|
|
706
|
+
}
|
|
707
|
+
catch {
|
|
708
|
+
throw new ProjectStateError('state_corrupt');
|
|
709
|
+
}
|
|
710
|
+
return { journal: decodeMaintenanceJournal(parsed), identity: fileIdentity(info) };
|
|
711
|
+
}
|
|
712
|
+
finally {
|
|
713
|
+
await file.close().catch(() => undefined);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
async loadMaintenanceJournal() {
|
|
717
|
+
return (await this.#readMaintenanceJournal())?.journal ?? null;
|
|
718
|
+
}
|
|
719
|
+
async writeMaintenanceJournal(journal) {
|
|
720
|
+
const raw = Buffer.from(canonicalJsonWithNumberFormatter(encodeMaintenanceJournal(journal), () => undefined), 'utf8');
|
|
721
|
+
if (raw.byteLength > MAX_MAINTENANCE_JOURNAL_BYTES) {
|
|
722
|
+
throw new ProjectStateError('state_too_large');
|
|
723
|
+
}
|
|
724
|
+
const root = this.requireStateRootHandle();
|
|
725
|
+
const tempName = `.${PROJECT_MAINTENANCE_JOURNAL_FILE}.${randomUUID()}.tmp`;
|
|
726
|
+
const tempPath = join(this.#stateRoot, tempName);
|
|
727
|
+
let file = null;
|
|
728
|
+
let tempIdentity = null;
|
|
729
|
+
try {
|
|
730
|
+
const created = this.#createFileAt(root, tempName, true, 'state_write_failed');
|
|
731
|
+
if (created.status !== 'ok')
|
|
732
|
+
throw new ProjectStateError('state_write_failed');
|
|
733
|
+
tempIdentity = created.identity;
|
|
734
|
+
file = await open(tempPath, constants.O_WRONLY | noFollowFlag());
|
|
735
|
+
const info = await file.stat({ bigint: true });
|
|
736
|
+
if (!info.isFile() || !privateRegularFileMetadata(info, this.#platform)
|
|
737
|
+
|| !sameFileIdentity(created.identity, fileIdentity(info))) {
|
|
738
|
+
throw new ProjectStateError('state_permissions');
|
|
739
|
+
}
|
|
740
|
+
this.requireMatchesAt(root, tempName, file, 'state_permissions');
|
|
741
|
+
await file.writeFile(raw);
|
|
742
|
+
await file.sync();
|
|
743
|
+
await file.close();
|
|
744
|
+
file = null;
|
|
745
|
+
const before = this.lookupAt(root, tempName, 'state_permissions');
|
|
746
|
+
if (before.status !== 'ok' || !sameFileIdentity(before.identity, created.identity)) {
|
|
747
|
+
throw new ProjectStateError('state_permissions');
|
|
748
|
+
}
|
|
749
|
+
this.#renameAt(root, tempName, PROJECT_MAINTENANCE_JOURNAL_FILE);
|
|
750
|
+
const after = this.lookupAt(root, PROJECT_MAINTENANCE_JOURNAL_FILE, 'state_permissions');
|
|
751
|
+
if (after.status !== 'ok' || !sameFileIdentity(after.identity, created.identity)) {
|
|
752
|
+
throw new ProjectStateError('state_permissions');
|
|
753
|
+
}
|
|
754
|
+
if (this.#platform !== 'win32')
|
|
755
|
+
await root.sync();
|
|
756
|
+
}
|
|
757
|
+
catch (error) {
|
|
758
|
+
if (error instanceof ProjectStateError)
|
|
759
|
+
throw error;
|
|
760
|
+
throw new ProjectStateError('state_write_failed');
|
|
761
|
+
}
|
|
762
|
+
finally {
|
|
763
|
+
await file?.close().catch(() => undefined);
|
|
764
|
+
this.#removeOwnedTemp(tempName, tempIdentity);
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
async clearMaintenanceJournal(expectedOperationId) {
|
|
768
|
+
const loaded = await this.#readMaintenanceJournal();
|
|
769
|
+
if (loaded === null)
|
|
770
|
+
return;
|
|
771
|
+
if (loaded.journal.operation_id !== expectedOperationId) {
|
|
772
|
+
throw new ProjectStateError('state_busy');
|
|
773
|
+
}
|
|
774
|
+
const result = this.unlinkAt(this.requireStateRootHandle(), PROJECT_MAINTENANCE_JOURNAL_FILE, loaded.identity, 'file', 'state_write_failed');
|
|
775
|
+
if (result.status !== 'ok')
|
|
776
|
+
throw new ProjectStateError('state_write_failed');
|
|
777
|
+
if (this.#platform !== 'win32')
|
|
778
|
+
await this.requireStateRootHandle().sync();
|
|
779
|
+
}
|
|
780
|
+
async #saveState(state, markCommitted) {
|
|
781
|
+
let raw;
|
|
782
|
+
try {
|
|
783
|
+
raw = Buffer.from(canonicalJsonWithNumberFormatter(encodeState(state), projectTimestampNumber), 'utf8');
|
|
784
|
+
}
|
|
785
|
+
catch {
|
|
786
|
+
throw new ProjectStateError('state_corrupt');
|
|
787
|
+
}
|
|
788
|
+
if (raw.byteLength > MAX_PROJECT_STATE_BYTES)
|
|
789
|
+
throw new ProjectStateError('state_too_large');
|
|
790
|
+
await this.#writeStateBytes(raw, PROJECT_STATE_FILE, markCommitted);
|
|
791
|
+
}
|
|
792
|
+
/** Keep the old filename so older clients refuse version 2 rather than create a split store. */
|
|
793
|
+
async #backupLegacyState(raw) {
|
|
794
|
+
if (await this.#backupAt(raw, PROJECT_PRE_ACP_BACKUP_FILE) === 'match')
|
|
795
|
+
return;
|
|
796
|
+
// A downgrade can leave a different v1 file under the first backup; never overwrite it, keep both.
|
|
797
|
+
const digest = createHash('sha256').update(raw).digest('hex').slice(0, 16);
|
|
798
|
+
const sibling = PROJECT_PRE_ACP_BACKUP_FILE.replace(/\.json$/u, `.${digest}.json`);
|
|
799
|
+
if (await this.#backupAt(raw, sibling) !== 'match')
|
|
800
|
+
throw new ProjectStateError('state_corrupt');
|
|
801
|
+
}
|
|
802
|
+
async #backupAt(raw, file) {
|
|
803
|
+
const root = this.requireStateRootHandle();
|
|
804
|
+
const entry = this.lookupAt(root, file, 'state_permissions');
|
|
805
|
+
if (entry.status === 'missing') {
|
|
806
|
+
await this.#writeStateBytes(raw, file, () => undefined);
|
|
807
|
+
}
|
|
808
|
+
else if (entry.status !== 'ok')
|
|
809
|
+
throw new ProjectStateError('state_permissions');
|
|
810
|
+
const backup = await openValidatedRegularFile(join(this.#stateRoot, file), (this.#platform === 'win32' ? constants.O_RDWR : constants.O_RDONLY)
|
|
811
|
+
| nonblockFlag() | noFollowFlag(), null, this.#platform);
|
|
812
|
+
try {
|
|
813
|
+
await this.revalidateStateRoot();
|
|
814
|
+
this.requireMatchesAt(root, file, backup, 'state_permissions');
|
|
815
|
+
if ((await backup.stat()).size !== raw.length)
|
|
816
|
+
return 'differs';
|
|
817
|
+
const copy = Buffer.alloc(raw.length);
|
|
818
|
+
let offset = 0;
|
|
819
|
+
while (offset < copy.length) {
|
|
820
|
+
const read = await backup.read(copy, offset, copy.length - offset, offset);
|
|
821
|
+
if (read.bytesRead === 0)
|
|
822
|
+
throw new ProjectStateError('state_corrupt');
|
|
823
|
+
offset += read.bytesRead;
|
|
824
|
+
}
|
|
825
|
+
if (!copy.equals(raw))
|
|
826
|
+
return 'differs';
|
|
827
|
+
this.requireMatchesAt(root, file, backup, 'state_permissions');
|
|
828
|
+
// A previous attempt may have stopped after rename but before directory fsync.
|
|
829
|
+
await backup.sync();
|
|
830
|
+
if (this.#platform !== 'win32')
|
|
831
|
+
await root.sync();
|
|
832
|
+
this.#publishDurability(this.#platform === 'win32' ? 'windows_metadata_commit' : 'dir_fsync');
|
|
833
|
+
return 'match';
|
|
834
|
+
}
|
|
835
|
+
finally {
|
|
836
|
+
await backup.close();
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
async #writeStateBytes(raw, name, markCommitted) {
|
|
840
|
+
const root = this.requireStateRootHandle();
|
|
841
|
+
const tempName = `.${name}.${randomUUID()}.tmp`;
|
|
842
|
+
requireProjectBasename(tempName, 'state_write_failed');
|
|
843
|
+
const temp = join(this.#stateRoot, tempName);
|
|
844
|
+
let file = null;
|
|
845
|
+
let tempIdentity = null;
|
|
846
|
+
try {
|
|
847
|
+
await this.revalidateStateRoot();
|
|
848
|
+
const created = this.#createFileAt(root, tempName, true, 'state_write_failed');
|
|
849
|
+
if (created.status !== 'ok')
|
|
850
|
+
throw new ProjectStateError('state_write_failed');
|
|
851
|
+
tempIdentity = created.identity;
|
|
852
|
+
file = await open(temp, constants.O_WRONLY | noFollowFlag());
|
|
853
|
+
const info = await file.stat({ bigint: true });
|
|
854
|
+
if (!info.isFile()
|
|
855
|
+
|| !privateRegularFileMetadata(info, this.#platform)
|
|
856
|
+
|| !sameFileIdentity(tempIdentity, fileIdentity(info)))
|
|
857
|
+
throw new ProjectStateError('state_permissions');
|
|
858
|
+
await this.revalidateStateRoot();
|
|
859
|
+
this.requireMatchesAt(root, tempName, file, 'state_permissions');
|
|
860
|
+
this.#publishDurability('temp_open');
|
|
861
|
+
await file.writeFile(raw);
|
|
862
|
+
await file.sync();
|
|
863
|
+
this.#publishDurability('file_fsync');
|
|
864
|
+
await file.close();
|
|
865
|
+
file = null;
|
|
866
|
+
await this.revalidateStateRoot();
|
|
867
|
+
const beforeRename = this.lookupAt(root, tempName, 'state_permissions');
|
|
868
|
+
if (beforeRename.status !== 'ok'
|
|
869
|
+
|| tempIdentity === null
|
|
870
|
+
|| !sameFileIdentity(beforeRename.identity, tempIdentity))
|
|
871
|
+
throw new ProjectStateError('state_permissions');
|
|
872
|
+
this.#renameAt(root, tempName, name);
|
|
873
|
+
markCommitted();
|
|
874
|
+
this.#publishDurability('atomic_replace');
|
|
875
|
+
await this.revalidateStateRoot();
|
|
876
|
+
const replaced = this.lookupAt(root, name, 'state_permissions');
|
|
877
|
+
if (replaced.status !== 'ok'
|
|
878
|
+
|| tempIdentity === null
|
|
879
|
+
|| !sameFileIdentity(replaced.identity, tempIdentity))
|
|
880
|
+
throw new ProjectStateError('state_permissions');
|
|
881
|
+
const directory = this.#stateRootHandle;
|
|
882
|
+
if (directory === null)
|
|
883
|
+
throw new ProjectStateError('state_permissions');
|
|
884
|
+
if (this.#platform === 'win32') {
|
|
885
|
+
// Node maps FileHandle.sync() to FlushFileBuffers(), which rejects
|
|
886
|
+
// directory handles with EPERM on Windows. The replace is already
|
|
887
|
+
// committed by the native descriptor-relative rename above; the
|
|
888
|
+
// retained-root and replacement identity checks are the Windows
|
|
889
|
+
// metadata commit boundary.
|
|
890
|
+
this.#publishDurability('windows_metadata_commit');
|
|
891
|
+
}
|
|
892
|
+
else {
|
|
893
|
+
await directory.sync();
|
|
894
|
+
this.#publishDurability('dir_fsync');
|
|
895
|
+
}
|
|
896
|
+
await this.revalidateStateRoot();
|
|
897
|
+
}
|
|
898
|
+
catch (error) {
|
|
899
|
+
if (error instanceof ProjectStateError)
|
|
900
|
+
throw error;
|
|
901
|
+
throw new ProjectStateError('state_write_failed');
|
|
902
|
+
}
|
|
903
|
+
finally {
|
|
904
|
+
await file?.close().catch(() => undefined);
|
|
905
|
+
this.#removeOwnedTemp(tempName, tempIdentity);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
#removeOwnedTemp(name, expected) {
|
|
909
|
+
if (expected === null)
|
|
910
|
+
return;
|
|
911
|
+
try {
|
|
912
|
+
const root = this.requireStateRootHandle();
|
|
913
|
+
const result = this.unlinkAt(root, name, expected, 'file', 'state_write_failed');
|
|
914
|
+
if (result.status === 'ok' || result.status === 'missing' || result.status === 'mismatch')
|
|
915
|
+
return;
|
|
916
|
+
}
|
|
917
|
+
catch {
|
|
918
|
+
// Exact descriptor-relative cleanup is best effort and never falls back to a path delete.
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
async rollbackCreatedDirectory(candidate) {
|
|
922
|
+
if (candidate.identity === null)
|
|
923
|
+
return false;
|
|
924
|
+
let file = null;
|
|
925
|
+
try {
|
|
926
|
+
const managed = await this.validateManagedRoot();
|
|
927
|
+
if (!isDirectChild(managed, candidate.path))
|
|
928
|
+
return false;
|
|
929
|
+
const root = this.requireManagedRootHandle();
|
|
930
|
+
const name = basename(candidate.path);
|
|
931
|
+
file = await open(candidate.path, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
932
|
+
this.requireMatchesAt(root, name, file, 'workspace_boundary_changed');
|
|
933
|
+
const info = await file.stat({ bigint: true });
|
|
934
|
+
const canonical = realpathSync(candidate.path);
|
|
935
|
+
if (!info.isDirectory()
|
|
936
|
+
|| !privateDirectoryMetadata(info, this.#platform)
|
|
937
|
+
|| !sameFileIdentity(candidate.identity, fileIdentity(info))
|
|
938
|
+
|| canonical !== candidate.path)
|
|
939
|
+
return false;
|
|
940
|
+
this.requireMatchesAt(root, name, file, 'workspace_boundary_changed');
|
|
941
|
+
const removed = this.unlinkAt(root, name, candidate.identity, 'directory', 'workspace_boundary_changed');
|
|
942
|
+
if (removed.status !== 'ok')
|
|
943
|
+
return false;
|
|
944
|
+
await this.validateManagedRoot();
|
|
945
|
+
return true;
|
|
946
|
+
}
|
|
947
|
+
catch {
|
|
948
|
+
// Rollback is best effort and never removes an unproven replacement or non-empty directory.
|
|
949
|
+
return false;
|
|
950
|
+
}
|
|
951
|
+
finally {
|
|
952
|
+
await file?.close().catch(() => undefined);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
#publishDurability(step) {
|
|
956
|
+
try {
|
|
957
|
+
this.#onDurabilityStep?.(step);
|
|
958
|
+
}
|
|
959
|
+
catch { /* an audit sink never owns state */ }
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
function validNativeLockResult(value) {
|
|
963
|
+
const record = ownDataRecord(value);
|
|
964
|
+
if (record === null)
|
|
965
|
+
return false;
|
|
966
|
+
const status = record.status;
|
|
967
|
+
if (status === 'acquired') {
|
|
968
|
+
return exactKeys(record, ['status', 'release']) && typeof record.release === 'function';
|
|
969
|
+
}
|
|
970
|
+
return exactKeys(record, ['status'])
|
|
971
|
+
&& (status === 'busy' || status === 'unsupported' || status === 'failed');
|
|
972
|
+
}
|
|
973
|
+
function validProjectRootFileResult(value) {
|
|
974
|
+
const record = ownDataRecord(value);
|
|
975
|
+
if (record === null || !exactKeys(record, ['status']))
|
|
976
|
+
return false;
|
|
977
|
+
const status = record.status;
|
|
978
|
+
return status === 'ok'
|
|
979
|
+
|| status === 'mismatch'
|
|
980
|
+
|| status === 'exists'
|
|
981
|
+
|| status === 'missing'
|
|
982
|
+
|| status === 'unsupported'
|
|
983
|
+
|| status === 'failed';
|
|
984
|
+
}
|
|
985
|
+
function validProjectRootFileLookupResult(value) {
|
|
986
|
+
const record = ownDataRecord(value);
|
|
987
|
+
if (record === null)
|
|
988
|
+
return false;
|
|
989
|
+
if (record.status === 'ok') {
|
|
990
|
+
if (!exactKeys(record, ['status', 'identity']))
|
|
991
|
+
return false;
|
|
992
|
+
const identity = ownDataRecord(record.identity);
|
|
993
|
+
return identity !== null
|
|
994
|
+
&& exactKeys(identity, ['device', 'inode'])
|
|
995
|
+
&& typeof identity.device === 'bigint'
|
|
996
|
+
&& typeof identity.inode === 'bigint'
|
|
997
|
+
&& identity.device >= 0n
|
|
998
|
+
&& identity.inode >= 0n;
|
|
999
|
+
}
|
|
1000
|
+
return exactKeys(record, ['status'])
|
|
1001
|
+
&& (record.status === 'missing'
|
|
1002
|
+
|| record.status === 'unsupported'
|
|
1003
|
+
|| record.status === 'failed');
|
|
1004
|
+
}
|
|
1005
|
+
function validProjectRootFileCreateResult(value) {
|
|
1006
|
+
const record = ownDataRecord(value);
|
|
1007
|
+
if (record === null)
|
|
1008
|
+
return false;
|
|
1009
|
+
if (record.status === 'ok') {
|
|
1010
|
+
if (!exactKeys(record, ['status', 'identity']))
|
|
1011
|
+
return false;
|
|
1012
|
+
return validFileIdentity(record.identity);
|
|
1013
|
+
}
|
|
1014
|
+
return exactKeys(record, ['status'])
|
|
1015
|
+
&& (record.status === 'exists'
|
|
1016
|
+
|| record.status === 'unsupported'
|
|
1017
|
+
|| record.status === 'failed');
|
|
1018
|
+
}
|
|
1019
|
+
function validFileIdentity(value) {
|
|
1020
|
+
const identity = ownDataRecord(value);
|
|
1021
|
+
return identity !== null
|
|
1022
|
+
&& exactKeys(identity, ['device', 'inode'])
|
|
1023
|
+
&& typeof identity.device === 'bigint'
|
|
1024
|
+
&& typeof identity.inode === 'bigint'
|
|
1025
|
+
&& identity.device >= 0n
|
|
1026
|
+
&& identity.inode >= 0n;
|
|
1027
|
+
}
|
|
1028
|
+
function ownDataRecord(value) {
|
|
1029
|
+
if (typeof value !== 'object' || value === null)
|
|
1030
|
+
return null;
|
|
1031
|
+
const prototype = Reflect.getPrototypeOf(value);
|
|
1032
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
1033
|
+
return null;
|
|
1034
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
1035
|
+
for (const descriptor of Object.values(descriptors)) {
|
|
1036
|
+
if (!Object.hasOwn(descriptor, 'value'))
|
|
1037
|
+
return null;
|
|
1038
|
+
}
|
|
1039
|
+
return value;
|
|
1040
|
+
}
|
|
1041
|
+
function exactKeys(record, expected) {
|
|
1042
|
+
const keys = Reflect.ownKeys(record);
|
|
1043
|
+
return keys.length === expected.length
|
|
1044
|
+
&& expected.every(key => Object.hasOwn(record, key));
|
|
1045
|
+
}
|
|
1046
|
+
export function isCommittedTransactionFailure(error) {
|
|
1047
|
+
return error instanceof TransactionProjectStateError && error.committed;
|
|
1048
|
+
}
|
|
1049
|
+
function brandProjectRoot(path) {
|
|
1050
|
+
const value = Object.freeze({ [hostProjectRootBrand]: true });
|
|
1051
|
+
rootValues.set(value, path);
|
|
1052
|
+
return value;
|
|
1053
|
+
}
|
|
1054
|
+
function brandManagedProjectRoot(path) {
|
|
1055
|
+
const value = Object.freeze({ [hostManagedProjectRootBrand]: true });
|
|
1056
|
+
managedRootValues.set(value, path);
|
|
1057
|
+
return value;
|
|
1058
|
+
}
|
|
1059
|
+
function projectRootPath(value) {
|
|
1060
|
+
const path = rootValues.get(value);
|
|
1061
|
+
if (path === undefined)
|
|
1062
|
+
throw new ProjectStateError('state_permissions');
|
|
1063
|
+
return path;
|
|
1064
|
+
}
|
|
1065
|
+
function managedProjectRootPath(value) {
|
|
1066
|
+
const path = managedRootValues.get(value);
|
|
1067
|
+
if (path === undefined)
|
|
1068
|
+
throw new ProjectStateError('managed_root_unsafe');
|
|
1069
|
+
return path;
|
|
1070
|
+
}
|
|
1071
|
+
function requireProjectRoot(configured, code, platform) {
|
|
1072
|
+
try {
|
|
1073
|
+
if (typeof configured !== 'string' || !isAbsolute(configured) || !isWellFormed(configured)) {
|
|
1074
|
+
throw new Error('invalid');
|
|
1075
|
+
}
|
|
1076
|
+
const info = lstatSync(configured);
|
|
1077
|
+
const canonical = realpathSync(configured);
|
|
1078
|
+
if (info.isSymbolicLink()
|
|
1079
|
+
|| !info.isDirectory()
|
|
1080
|
+
|| canonical !== resolve(configured)
|
|
1081
|
+
|| !privateDirectoryMetadata(info, platform))
|
|
1082
|
+
throw new Error('unsafe');
|
|
1083
|
+
return canonical;
|
|
1084
|
+
}
|
|
1085
|
+
catch {
|
|
1086
|
+
throw new ProjectStateError(code);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
function requireManagedProjectRoot(configured, platform) {
|
|
1090
|
+
try {
|
|
1091
|
+
if (typeof configured !== 'string' || !isAbsolute(configured) || !isWellFormed(configured)) {
|
|
1092
|
+
throw new Error('invalid');
|
|
1093
|
+
}
|
|
1094
|
+
const info = lstatSync(configured);
|
|
1095
|
+
const canonical = realpathSync(configured);
|
|
1096
|
+
if (info.isSymbolicLink()
|
|
1097
|
+
|| !info.isDirectory()
|
|
1098
|
+
|| canonical !== resolve(configured)
|
|
1099
|
+
|| !managedDirectoryMetadata(info, platform))
|
|
1100
|
+
throw new Error('unsafe');
|
|
1101
|
+
return canonical;
|
|
1102
|
+
}
|
|
1103
|
+
catch {
|
|
1104
|
+
throw new ProjectStateError('managed_root_unsafe');
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
export async function validateRegisteredWorkspace(path, failure = 'workspace_invalid') {
|
|
1108
|
+
try {
|
|
1109
|
+
const file = await open(path, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
1110
|
+
try {
|
|
1111
|
+
const info = await file.stat({ bigint: true });
|
|
1112
|
+
const canonical = realpathSync(path);
|
|
1113
|
+
if (!info.isDirectory() || canonical !== path)
|
|
1114
|
+
throw new Error('unsafe');
|
|
1115
|
+
return { canonical, identity: fileIdentity(info) };
|
|
1116
|
+
}
|
|
1117
|
+
finally {
|
|
1118
|
+
await file.close();
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
catch {
|
|
1122
|
+
throw new ProjectStateError(failure);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
async function openValidatedRegularFile(path, flags, createMode, platform) {
|
|
1126
|
+
let file = null;
|
|
1127
|
+
try {
|
|
1128
|
+
file = createMode === null ? await open(path, flags) : await open(path, flags, createMode);
|
|
1129
|
+
const info = await file.stat();
|
|
1130
|
+
if (!info.isFile()
|
|
1131
|
+
|| !privateRegularFileMetadata(info, platform))
|
|
1132
|
+
throw new ProjectStateError('state_permissions');
|
|
1133
|
+
return file;
|
|
1134
|
+
}
|
|
1135
|
+
catch (error) {
|
|
1136
|
+
await file?.close().catch(() => undefined);
|
|
1137
|
+
if (isNodeError(error, 'ENOENT'))
|
|
1138
|
+
throw error;
|
|
1139
|
+
if (error instanceof ProjectStateError)
|
|
1140
|
+
throw error;
|
|
1141
|
+
throw new ProjectStateError('state_permissions');
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
export function privateDirectoryMetadata(info, platform) {
|
|
1145
|
+
if (platform === 'win32')
|
|
1146
|
+
return true;
|
|
1147
|
+
return ownedByCurrentUserValue(info.uid) && (BigInt(info.mode) & 4095n) === 448n;
|
|
1148
|
+
}
|
|
1149
|
+
function privateRegularFileMetadata(info, platform) {
|
|
1150
|
+
if (platform === 'win32')
|
|
1151
|
+
return true;
|
|
1152
|
+
return ownedByCurrentUserValue(info.uid) && (BigInt(info.mode) & 4095n) === 384n;
|
|
1153
|
+
}
|
|
1154
|
+
function managedDirectoryMetadata(info, platform) {
|
|
1155
|
+
if (platform === 'win32')
|
|
1156
|
+
return true;
|
|
1157
|
+
return ownedByCurrentUserValue(info.uid) && !unsafeManagedMode(Number(info.mode));
|
|
1158
|
+
}
|
|
1159
|
+
function ownedByCurrentUserValue(uid) {
|
|
1160
|
+
return typeof process.getuid === 'function' && BigInt(uid) === BigInt(process.getuid());
|
|
1161
|
+
}
|
|
1162
|
+
export function fileIdentity(info) {
|
|
1163
|
+
return Object.freeze({ device: info.dev, inode: info.ino });
|
|
1164
|
+
}
|
|
1165
|
+
export function sameFileIdentity(left, right) {
|
|
1166
|
+
return left.device === right.device && left.inode === right.inode;
|
|
1167
|
+
}
|
|
1168
|
+
async function openStateRoot(path, platform) {
|
|
1169
|
+
let file = null;
|
|
1170
|
+
try {
|
|
1171
|
+
file = await open(path, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
1172
|
+
const info = await file.stat({ bigint: true });
|
|
1173
|
+
const canonical = realpathSync(path);
|
|
1174
|
+
if (!info.isDirectory()
|
|
1175
|
+
|| canonical !== path
|
|
1176
|
+
|| !privateDirectoryMetadata(info, platform))
|
|
1177
|
+
throw new Error('unsafe');
|
|
1178
|
+
return {
|
|
1179
|
+
file,
|
|
1180
|
+
identity: Object.freeze({
|
|
1181
|
+
...fileIdentity(info),
|
|
1182
|
+
canonical,
|
|
1183
|
+
owner: info.uid,
|
|
1184
|
+
mode: info.mode & 4095n,
|
|
1185
|
+
}),
|
|
1186
|
+
};
|
|
1187
|
+
}
|
|
1188
|
+
catch {
|
|
1189
|
+
await file?.close().catch(() => undefined);
|
|
1190
|
+
throw new ProjectStateError('state_permissions');
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
async function openManagedRoot(path, platform) {
|
|
1194
|
+
let file = null;
|
|
1195
|
+
try {
|
|
1196
|
+
file = await open(path, constants.O_RDONLY | directoryFlag() | noFollowFlag());
|
|
1197
|
+
const info = await file.stat({ bigint: true });
|
|
1198
|
+
const canonical = realpathSync(path);
|
|
1199
|
+
if (!info.isDirectory()
|
|
1200
|
+
|| canonical !== path
|
|
1201
|
+
|| !managedDirectoryMetadata(info, platform))
|
|
1202
|
+
throw new Error('unsafe');
|
|
1203
|
+
return { file, identity: fileIdentity(info) };
|
|
1204
|
+
}
|
|
1205
|
+
catch {
|
|
1206
|
+
await file?.close().catch(() => undefined);
|
|
1207
|
+
throw new ProjectStateError('managed_root_unsafe');
|
|
1208
|
+
}
|
|
1209
|
+
}
|
|
1210
|
+
function stateRootMatches(info, expected, platform) {
|
|
1211
|
+
return info.isDirectory()
|
|
1212
|
+
&& info.dev === expected.device
|
|
1213
|
+
&& info.ino === expected.inode
|
|
1214
|
+
&& (platform === 'win32' || (info.uid === expected.owner
|
|
1215
|
+
&& (info.mode & 4095n) === expected.mode
|
|
1216
|
+
&& privateDirectoryMetadata(info, platform)));
|
|
1217
|
+
}
|
|
1218
|
+
function unsafeManagedMode(mode) {
|
|
1219
|
+
return (mode & 0o7022) !== 0;
|
|
1220
|
+
}
|
|
1221
|
+
function readClock(clock) {
|
|
1222
|
+
const value = clock.now();
|
|
1223
|
+
if (!Number.isFinite(value))
|
|
1224
|
+
throw new ProjectStateError('state_lock_failed');
|
|
1225
|
+
return value;
|
|
1226
|
+
}
|
|
1227
|
+
async function waitForTransactions(transactions, signal, closeSignal) {
|
|
1228
|
+
if (transactions.length === 0)
|
|
1229
|
+
return;
|
|
1230
|
+
if (signal?.aborted === true)
|
|
1231
|
+
throw projectAbortError();
|
|
1232
|
+
if (closeSignal.aborted)
|
|
1233
|
+
throw new ProjectStateError('state_lock_failed');
|
|
1234
|
+
const combined = signal === undefined ? closeSignal : AbortSignal.any([signal, closeSignal]);
|
|
1235
|
+
let subscription;
|
|
1236
|
+
try {
|
|
1237
|
+
await new Promise((resolve, reject) => {
|
|
1238
|
+
subscription = addAbortListener(combined, () => {
|
|
1239
|
+
reject(closeSignal.aborted ? new ProjectStateError('state_lock_failed') : projectAbortError());
|
|
1240
|
+
});
|
|
1241
|
+
void Promise.all(transactions).then(() => { resolve(); }, reject);
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
finally {
|
|
1245
|
+
subscription?.[Symbol.dispose]();
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
function isAbortError(error) {
|
|
1249
|
+
return error instanceof Error && error.name === 'AbortError';
|
|
1250
|
+
}
|
|
1251
|
+
function projectAbortError() {
|
|
1252
|
+
const error = new Error('project state operation aborted');
|
|
1253
|
+
error.name = 'AbortError';
|
|
1254
|
+
return error;
|
|
1255
|
+
}
|
|
1256
|
+
export function noFollowFlag() {
|
|
1257
|
+
return constants.O_NOFOLLOW ?? 0;
|
|
1258
|
+
}
|
|
1259
|
+
export function directoryFlag() {
|
|
1260
|
+
return constants.O_DIRECTORY ?? 0;
|
|
1261
|
+
}
|
|
1262
|
+
function nonblockFlag() {
|
|
1263
|
+
return constants.O_NONBLOCK ?? 0;
|
|
1264
|
+
}
|
|
1265
|
+
export function isNodeError(error, code) {
|
|
1266
|
+
return error instanceof Error && error.code === code;
|
|
1267
|
+
}
|
|
1268
|
+
export function isDirectChild(parent, child) {
|
|
1269
|
+
return child !== parent && dirname(child) === parent;
|
|
1270
|
+
}
|
|
1271
|
+
//# sourceMappingURL=project-store-files.js.map
|