harnery 0.35.0 → 0.36.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/dist/commander.d.ts +30 -5
- package/dist/commander.d.ts.map +1 -1
- package/dist/commander.js +124 -96
- package/dist/commands/agents.d.ts +23 -3
- package/dist/commands/agents.d.ts.map +1 -1
- package/dist/commands/agents.js +379 -198
- package/dist/commands/backup.d.ts.map +1 -1
- package/dist/commands/backup.js +10 -10
- package/dist/commands/browse-session.d.ts.map +1 -1
- package/dist/commands/browse-session.js +12 -0
- package/dist/commands/browse.d.ts +1 -1
- package/dist/commands/browse.d.ts.map +1 -1
- package/dist/commands/browse.js +5 -4
- package/dist/commands/conversations.d.ts +10 -0
- package/dist/commands/conversations.d.ts.map +1 -0
- package/dist/commands/conversations.js +201 -0
- package/dist/commands/deinit.d.ts.map +1 -1
- package/dist/commands/deinit.js +5 -0
- package/dist/commands/docs.d.ts.map +1 -1
- package/dist/commands/docs.js +13 -2
- package/dist/commands/doctor.d.ts +5 -0
- package/dist/commands/doctor.d.ts.map +1 -1
- package/dist/commands/doctor.js +51 -7
- package/dist/commands/fetch.js +6 -5
- package/dist/commands/files.d.ts +22 -0
- package/dist/commands/files.d.ts.map +1 -0
- package/dist/commands/files.js +113 -0
- package/dist/commands/governor.js +14 -12
- package/dist/commands/inbox.d.ts +5 -0
- package/dist/commands/inbox.d.ts.map +1 -0
- package/dist/commands/inbox.js +57 -0
- package/dist/commands/init.d.ts +4 -5
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +63 -13
- package/dist/commands/ledger-v3.d.ts.map +1 -1
- package/dist/commands/ledger-v3.js +347 -1
- package/dist/commands/logs.d.ts +4 -0
- package/dist/commands/logs.d.ts.map +1 -0
- package/dist/commands/logs.js +169 -0
- package/dist/commands/messages.d.ts +5 -0
- package/dist/commands/messages.d.ts.map +1 -0
- package/dist/commands/messages.js +45 -0
- package/dist/commands/outline.d.ts +1 -1
- package/dist/commands/outline.d.ts.map +1 -1
- package/dist/commands/outline.js +2 -2
- package/dist/commands/read.d.ts +1 -1
- package/dist/commands/read.d.ts.map +1 -1
- package/dist/commands/read.js +2 -2
- package/dist/commands/semantic.d.ts.map +1 -1
- package/dist/commands/semantic.js +54 -2
- package/dist/commands/storage.d.ts +4 -0
- package/dist/commands/storage.d.ts.map +1 -0
- package/dist/commands/storage.js +238 -0
- package/dist/commands/tunnel.d.ts +5 -1
- package/dist/commands/tunnel.d.ts.map +1 -1
- package/dist/commands/tunnel.js +44 -31
- package/dist/commands/web.d.ts +3 -0
- package/dist/commands/web.d.ts.map +1 -1
- package/dist/commands/web.js +67 -7
- package/dist/commands/work.js +3 -3
- package/dist/commands/workflow.js +13 -13
- package/dist/core/adapters/profiles.d.ts +1 -1
- package/dist/core/agents/cli.js +209 -39
- package/dist/core/agents/live-authority-v3.d.ts +47 -1
- package/dist/core/agents/live-authority-v3.d.ts.map +1 -1
- package/dist/core/agents/live-authority-v3.js +244 -10
- package/dist/core/agents/live-lifecycle-v3.js +6 -1
- package/dist/core/agents/live-observation-v3.d.ts.map +1 -1
- package/dist/core/agents/live-observation-v3.js +6 -1
- package/dist/core/agents/render/prompt-context.d.ts +9 -3
- package/dist/core/agents/render/prompt-context.d.ts.map +1 -1
- package/dist/core/agents/render/prompt-context.js +24 -7
- package/dist/core/agents/rules/stop-hook.d.ts +2 -0
- package/dist/core/agents/rules/stop-hook.d.ts.map +1 -1
- package/dist/core/agents/rules/stop-hook.js +94 -13
- package/dist/core/agents/rules/verdict.d.ts +1 -0
- package/dist/core/agents/rules/verdict.d.ts.map +1 -1
- package/dist/core/agents/session-events.d.ts.map +1 -1
- package/dist/core/agents/session-events.js +4 -0
- package/dist/core/agents/session-finalization-state-v3.js +1 -1
- package/dist/core/agents/session-finalizer-v3.js +5 -1
- package/dist/core/agents/state/activity-log.d.ts +3 -0
- package/dist/core/agents/state/activity-log.d.ts.map +1 -1
- package/dist/core/agents/state/activity-log.js +6 -3
- package/dist/core/agents/state/heartbeat-writer.d.ts +6 -0
- package/dist/core/agents/state/heartbeat-writer.d.ts.map +1 -1
- package/dist/core/agents/state/heartbeat-writer.js +18 -0
- package/dist/core/agents/state/live-coordination-view.d.ts +5 -7
- package/dist/core/agents/state/live-coordination-view.d.ts.map +1 -1
- package/dist/core/agents/state/live-coordination-view.js +15 -90
- package/dist/core/agents/state/live-coordination-writer.d.ts +12 -0
- package/dist/core/agents/state/live-coordination-writer.d.ts.map +1 -0
- package/dist/core/agents/state/live-coordination-writer.js +92 -0
- package/dist/core/artifacts/index.d.ts +21 -0
- package/dist/core/artifacts/index.d.ts.map +1 -1
- package/dist/core/artifacts/index.js +46 -1
- package/dist/core/config.d.ts +62 -4
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +98 -4
- package/dist/core/conversations/archive.d.ts +49 -0
- package/dist/core/conversations/archive.d.ts.map +1 -0
- package/dist/core/conversations/archive.js +136 -0
- package/dist/core/conversations/catalog.d.ts +10 -0
- package/dist/core/conversations/catalog.d.ts.map +1 -0
- package/dist/core/conversations/catalog.js +72 -0
- package/dist/core/conversations/context-pack.d.ts +6 -0
- package/dist/core/conversations/context-pack.d.ts.map +1 -0
- package/dist/core/conversations/context-pack.js +60 -0
- package/dist/core/conversations/contract.d.ts +155 -0
- package/dist/core/conversations/contract.d.ts.map +1 -0
- package/dist/core/conversations/contract.js +1 -0
- package/dist/core/conversations/evaluation.d.ts +16 -0
- package/dist/core/conversations/evaluation.d.ts.map +1 -0
- package/dist/core/conversations/evaluation.js +9 -0
- package/dist/core/conversations/index.d.ts +10 -0
- package/dist/core/conversations/index.d.ts.map +1 -0
- package/dist/core/conversations/index.js +9 -0
- package/dist/core/conversations/lifecycle.d.ts +28 -0
- package/dist/core/conversations/lifecycle.d.ts.map +1 -0
- package/dist/core/conversations/lifecycle.js +29 -0
- package/dist/core/conversations/normalize.d.ts +14 -0
- package/dist/core/conversations/normalize.d.ts.map +1 -0
- package/dist/core/conversations/normalize.js +82 -0
- package/dist/core/conversations/query.d.ts +8 -0
- package/dist/core/conversations/query.d.ts.map +1 -0
- package/dist/core/conversations/query.js +280 -0
- package/dist/core/conversations/search-index.d.ts +9 -0
- package/dist/core/conversations/search-index.d.ts.map +1 -0
- package/dist/core/conversations/search-index.js +29 -0
- package/dist/core/events/legacy-storage/canary.d.ts +18 -0
- package/dist/core/events/legacy-storage/canary.d.ts.map +1 -0
- package/dist/core/events/legacy-storage/canary.js +150 -0
- package/dist/core/events/legacy-storage/fence.d.ts +15 -0
- package/dist/core/events/legacy-storage/fence.d.ts.map +1 -0
- package/dist/core/events/legacy-storage/fence.js +115 -0
- package/dist/core/events/legacy-storage/index.d.ts +6 -0
- package/dist/core/events/legacy-storage/index.d.ts.map +1 -0
- package/dist/core/events/legacy-storage/index.js +5 -0
- package/dist/core/events/legacy-storage/inventory.d.ts +10 -0
- package/dist/core/events/legacy-storage/inventory.d.ts.map +1 -0
- package/dist/core/events/legacy-storage/inventory.js +40 -0
- package/dist/core/events/legacy-storage/manifest.d.ts +23 -0
- package/dist/core/events/legacy-storage/manifest.d.ts.map +1 -0
- package/dist/core/events/legacy-storage/manifest.js +108 -0
- package/dist/core/events/legacy-storage/reader.d.ts +15 -0
- package/dist/core/events/legacy-storage/reader.d.ts.map +1 -0
- package/dist/core/events/legacy-storage/reader.js +154 -0
- package/dist/core/events/v3/authority-outbox.d.ts +7 -2
- package/dist/core/events/v3/authority-outbox.d.ts.map +1 -1
- package/dist/core/events/v3/authority-outbox.js +16 -3
- package/dist/core/events/v3/base-contract.d.ts +81 -41
- package/dist/core/events/v3/base-contract.d.ts.map +1 -1
- package/dist/core/events/v3/base-contract.js +17 -7
- package/dist/core/events/v3/bootstrap.d.ts +39 -0
- package/dist/core/events/v3/bootstrap.d.ts.map +1 -1
- package/dist/core/events/v3/bootstrap.js +167 -3
- package/dist/core/events/v3/capabilities.js +3 -3
- package/dist/core/events/v3/control-witness.d.ts +44 -0
- package/dist/core/events/v3/control-witness.d.ts.map +1 -0
- package/dist/core/events/v3/control-witness.js +233 -0
- package/dist/core/events/v3/control-writer.d.ts +4 -0
- package/dist/core/events/v3/control-writer.d.ts.map +1 -0
- package/dist/core/events/v3/control-writer.js +16 -0
- package/dist/core/events/v3/control.d.ts +8 -2
- package/dist/core/events/v3/control.d.ts.map +1 -1
- package/dist/core/events/v3/control.js +46 -18
- package/dist/core/events/v3/coordination-transaction-recovery.d.ts +42 -0
- package/dist/core/events/v3/coordination-transaction-recovery.d.ts.map +1 -0
- package/dist/core/events/v3/coordination-transaction-recovery.js +342 -0
- package/dist/core/events/v3/coordination-view.d.ts.map +1 -1
- package/dist/core/events/v3/coordination-view.js +16 -1
- package/dist/core/events/v3/generated.d.ts +1 -1
- package/dist/core/events/v3/generated.js +1 -1
- package/dist/core/events/v3/index.d.ts +4 -2
- package/dist/core/events/v3/index.d.ts.map +1 -1
- package/dist/core/events/v3/index.js +3 -1
- package/dist/core/events/v3/live-route-observer.d.ts +31 -0
- package/dist/core/events/v3/live-route-observer.d.ts.map +1 -0
- package/dist/core/events/v3/live-route-observer.js +58 -0
- package/dist/core/events/v3/live-routing.d.ts +13 -21
- package/dist/core/events/v3/live-routing.d.ts.map +1 -1
- package/dist/core/events/v3/live-routing.js +45 -41
- package/dist/core/events/v3/producers/coordination-recorder.d.ts +11 -1
- package/dist/core/events/v3/producers/coordination-recorder.d.ts.map +1 -1
- package/dist/core/events/v3/producers/coordination-recorder.js +76 -10
- package/dist/core/events/v3/producers/diagnostic-summaries.d.ts +129 -0
- package/dist/core/events/v3/producers/diagnostic-summaries.d.ts.map +1 -0
- package/dist/core/events/v3/producers/diagnostic-summaries.js +499 -0
- package/dist/core/events/v3/producers/hook-base.js +1 -1
- package/dist/core/events/v3/producers/intake.d.ts +7 -0
- package/dist/core/events/v3/producers/intake.d.ts.map +1 -1
- package/dist/core/events/v3/producers/intake.js +25 -2
- package/dist/core/events/v3/producers/producer-state-observer.d.ts +8 -0
- package/dist/core/events/v3/producers/producer-state-observer.d.ts.map +1 -0
- package/dist/core/events/v3/producers/producer-state-observer.js +45 -0
- package/dist/core/events/v3/producers/recorder.d.ts +55 -1
- package/dist/core/events/v3/producers/recorder.d.ts.map +1 -1
- package/dist/core/events/v3/producers/recorder.js +353 -24
- package/dist/core/events/v3/reader.d.ts +51 -0
- package/dist/core/events/v3/reader.d.ts.map +1 -1
- package/dist/core/events/v3/reader.js +181 -1
- package/dist/core/events/v3/rotation-config.d.ts +9 -0
- package/dist/core/events/v3/rotation-config.d.ts.map +1 -0
- package/dist/core/events/v3/rotation-config.js +91 -0
- package/dist/core/events/v3/runtime-build.d.ts +11 -0
- package/dist/core/events/v3/runtime-build.d.ts.map +1 -0
- package/dist/core/events/v3/runtime-build.js +39 -0
- package/dist/core/events/v3/runtime-identity.d.ts +5 -0
- package/dist/core/events/v3/runtime-identity.d.ts.map +1 -0
- package/dist/core/events/v3/runtime-identity.js +18 -0
- package/dist/core/events/v3/runtime-telemetry-capabilities.d.ts +6 -5
- package/dist/core/events/v3/runtime-telemetry-capabilities.d.ts.map +1 -1
- package/dist/core/events/v3/runtime-telemetry-capabilities.js +1 -1
- package/dist/core/events/v3/support-storage/classify.d.ts +46 -0
- package/dist/core/events/v3/support-storage/classify.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/classify.js +149 -0
- package/dist/core/events/v3/support-storage/eligibility.d.ts +29 -0
- package/dist/core/events/v3/support-storage/eligibility.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/eligibility.js +35 -0
- package/dist/core/events/v3/support-storage/index.d.ts +10 -0
- package/dist/core/events/v3/support-storage/index.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/index.js +9 -0
- package/dist/core/events/v3/support-storage/inventory.d.ts +30 -0
- package/dist/core/events/v3/support-storage/inventory.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/inventory.js +94 -0
- package/dist/core/events/v3/support-storage/logical-authority.d.ts +14 -0
- package/dist/core/events/v3/support-storage/logical-authority.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/logical-authority.js +104 -0
- package/dist/core/events/v3/support-storage/maintenance.d.ts +98 -0
- package/dist/core/events/v3/support-storage/maintenance.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/maintenance.js +352 -0
- package/dist/core/events/v3/support-storage/pack-contract.d.ts +59 -0
- package/dist/core/events/v3/support-storage/pack-contract.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/pack-contract.js +249 -0
- package/dist/core/events/v3/support-storage/pack-reader.d.ts +34 -0
- package/dist/core/events/v3/support-storage/pack-reader.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/pack-reader.js +258 -0
- package/dist/core/events/v3/support-storage/pack-writer.d.ts +30 -0
- package/dist/core/events/v3/support-storage/pack-writer.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/pack-writer.js +268 -0
- package/dist/core/events/v3/support-storage/recovery.d.ts +15 -0
- package/dist/core/events/v3/support-storage/recovery.d.ts.map +1 -0
- package/dist/core/events/v3/support-storage/recovery.js +22 -0
- package/dist/core/events/v3/turn-telemetry.d.ts +14 -8
- package/dist/core/events/v3/turn-telemetry.d.ts.map +1 -1
- package/dist/core/events/v3/turn-telemetry.js +32 -2
- package/dist/core/events/v3/writer.d.ts +13 -1
- package/dist/core/events/v3/writer.d.ts.map +1 -1
- package/dist/core/events/v3/writer.js +126 -12
- package/dist/core/governor/plan-read.d.ts.map +1 -1
- package/dist/core/governor/plan-read.js +5 -21
- package/dist/core/governor/planning.js +6 -8
- package/dist/core/governor/service-read.d.ts +12 -0
- package/dist/core/governor/service-read.d.ts.map +1 -1
- package/dist/core/governor/service-read.js +266 -2
- package/dist/core/governor/service.d.ts +1 -0
- package/dist/core/governor/service.d.ts.map +1 -1
- package/dist/core/governor/service.js +53 -18
- package/dist/core/guard/evidence-v3.d.ts.map +1 -1
- package/dist/core/guard/evidence-v3.js +2 -0
- package/dist/core/hooks/adapter/events.d.ts.map +1 -1
- package/dist/core/hooks/adapter/events.js +1 -0
- package/dist/core/hooks/adapter/output.d.ts +4 -2
- package/dist/core/hooks/adapter/output.d.ts.map +1 -1
- package/dist/core/hooks/adapter/output.js +13 -5
- package/dist/core/hooks/adapter/runtime-telemetry.d.ts +27 -4
- package/dist/core/hooks/adapter/runtime-telemetry.d.ts.map +1 -1
- package/dist/core/hooks/adapter/runtime-telemetry.js +307 -6
- package/dist/core/hooks/cli.js +212 -30
- package/dist/core/hooks/effects/image-capture.d.ts +1 -1
- package/dist/core/hooks/effects/image-capture.d.ts.map +1 -1
- package/dist/core/hooks/effects/image-capture.js +4 -2
- package/dist/core/hooks/remediation-cap.d.ts +8 -3
- package/dist/core/hooks/remediation-cap.d.ts.map +1 -1
- package/dist/core/hooks/remediation-cap.js +3 -11
- package/dist/core/hooks/resolve/coord-root.d.ts +22 -9
- package/dist/core/hooks/resolve/coord-root.d.ts.map +1 -1
- package/dist/core/hooks/resolve/coord-root.js +92 -11
- package/dist/core/hooks/resolve/owner.d.ts.map +1 -1
- package/dist/core/hooks/resolve/owner.js +5 -0
- package/dist/core/hooks/resolve/transcript.d.ts +8 -0
- package/dist/core/hooks/resolve/transcript.d.ts.map +1 -1
- package/dist/core/hooks/resolve/transcript.js +11 -0
- package/dist/core/inbox/contract.d.ts +64 -0
- package/dist/core/inbox/contract.d.ts.map +1 -0
- package/dist/core/inbox/contract.js +13 -0
- package/dist/core/inbox/index.d.ts +5 -0
- package/dist/core/inbox/index.d.ts.map +1 -0
- package/dist/core/inbox/index.js +4 -0
- package/dist/core/inbox/lifecycle.d.ts +22 -0
- package/dist/core/inbox/lifecycle.d.ts.map +1 -0
- package/dist/core/inbox/lifecycle.js +41 -0
- package/dist/core/inbox/service.d.ts +36 -0
- package/dist/core/inbox/service.d.ts.map +1 -0
- package/dist/core/inbox/service.js +210 -0
- package/dist/core/inbox/watcher.d.ts +7 -0
- package/dist/core/inbox/watcher.d.ts.map +1 -0
- package/dist/core/inbox/watcher.js +26 -0
- package/dist/core/presence/codec-digest.d.ts.map +1 -1
- package/dist/core/presence/codec-digest.js +14 -3
- package/dist/core/presence/relay-client.d.ts +1 -0
- package/dist/core/presence/relay-client.d.ts.map +1 -1
- package/dist/core/presence/relay-client.js +52 -8
- package/dist/core/semantic/adapters.d.ts +11 -8
- package/dist/core/semantic/adapters.d.ts.map +1 -1
- package/dist/core/semantic/adapters.js +169 -31
- package/dist/core/semantic/contract.d.ts +532 -48
- package/dist/core/semantic/contract.d.ts.map +1 -1
- package/dist/core/semantic/contract.js +104 -3
- package/dist/core/semantic/index.d.ts +7 -3
- package/dist/core/semantic/index.d.ts.map +1 -1
- package/dist/core/semantic/index.js +7 -3
- package/dist/core/semantic/once.d.ts +6 -1
- package/dist/core/semantic/once.d.ts.map +1 -1
- package/dist/core/semantic/once.js +80 -9
- package/dist/core/semantic/prompt.d.ts.map +1 -1
- package/dist/core/semantic/prompt.js +14 -1
- package/dist/core/semantic/review.d.ts +144 -0
- package/dist/core/semantic/review.d.ts.map +1 -0
- package/dist/core/semantic/review.js +544 -0
- package/dist/core/semantic/routes.d.ts +10 -0
- package/dist/core/semantic/routes.d.ts.map +1 -0
- package/dist/core/semantic/routes.js +21 -0
- package/dist/core/semantic/scheduler.d.ts +14 -3
- package/dist/core/semantic/scheduler.d.ts.map +1 -1
- package/dist/core/semantic/scheduler.js +30 -6
- package/dist/core/semantic/service-status.d.ts +20 -0
- package/dist/core/semantic/service-status.d.ts.map +1 -1
- package/dist/core/semantic/service-status.js +60 -4
- package/dist/core/semantic/service.d.ts +1 -0
- package/dist/core/semantic/service.d.ts.map +1 -1
- package/dist/core/semantic/service.js +104 -15
- package/dist/core/semantic/soak.d.ts +75 -0
- package/dist/core/semantic/soak.d.ts.map +1 -0
- package/dist/core/semantic/soak.js +476 -0
- package/dist/core/semantic/storage.d.ts +11 -1
- package/dist/core/semantic/storage.d.ts.map +1 -1
- package/dist/core/semantic/storage.js +4 -3
- package/dist/core/semantic/usage.d.ts +43 -0
- package/dist/core/semantic/usage.d.ts.map +1 -0
- package/dist/core/semantic/usage.js +193 -0
- package/dist/core/semantic/validate.d.ts +3 -1
- package/dist/core/semantic/validate.d.ts.map +1 -1
- package/dist/core/semantic/validate.js +34 -1
- package/dist/core/storage/buffer.d.ts +28 -0
- package/dist/core/storage/buffer.d.ts.map +1 -0
- package/dist/core/storage/buffer.js +101 -0
- package/dist/core/storage/builtins.d.ts +3 -0
- package/dist/core/storage/builtins.d.ts.map +1 -0
- package/dist/core/storage/builtins.js +683 -0
- package/dist/core/storage/catalog.d.ts +19 -0
- package/dist/core/storage/catalog.d.ts.map +1 -0
- package/dist/core/storage/catalog.js +365 -0
- package/dist/core/storage/config.d.ts +16 -0
- package/dist/core/storage/config.d.ts.map +1 -0
- package/dist/core/storage/config.js +311 -0
- package/dist/core/storage/contract.d.ts +275 -0
- package/dist/core/storage/contract.d.ts.map +1 -0
- package/dist/core/storage/contract.js +3 -0
- package/dist/core/storage/durable-history.d.ts +36 -0
- package/dist/core/storage/durable-history.d.ts.map +1 -0
- package/dist/core/storage/durable-history.js +606 -0
- package/dist/core/storage/health.d.ts +4 -0
- package/dist/core/storage/health.d.ts.map +1 -0
- package/dist/core/storage/health.js +65 -0
- package/dist/core/storage/index.d.ts +16 -0
- package/dist/core/storage/index.d.ts.map +1 -0
- package/dist/core/storage/index.js +15 -0
- package/dist/core/storage/inventory.d.ts +25 -0
- package/dist/core/storage/inventory.d.ts.map +1 -0
- package/dist/core/storage/inventory.js +666 -0
- package/dist/core/storage/jsonl.d.ts +52 -0
- package/dist/core/storage/jsonl.d.ts.map +1 -0
- package/dist/core/storage/jsonl.js +156 -0
- package/dist/core/storage/log-retention.d.ts +52 -0
- package/dist/core/storage/log-retention.d.ts.map +1 -0
- package/dist/core/storage/log-retention.js +419 -0
- package/dist/core/storage/logger.d.ts +82 -0
- package/dist/core/storage/logger.d.ts.map +1 -0
- package/dist/core/storage/logger.js +411 -0
- package/dist/core/storage/maintenance-providers.d.ts +26 -0
- package/dist/core/storage/maintenance-providers.d.ts.map +1 -0
- package/dist/core/storage/maintenance-providers.js +141 -0
- package/dist/core/storage/maintenance.d.ts +125 -0
- package/dist/core/storage/maintenance.d.ts.map +1 -0
- package/dist/core/storage/maintenance.js +609 -0
- package/dist/core/storage/metrics.d.ts +60 -0
- package/dist/core/storage/metrics.d.ts.map +1 -0
- package/dist/core/storage/metrics.js +134 -0
- package/dist/core/storage/policy.d.ts +7 -0
- package/dist/core/storage/policy.d.ts.map +1 -0
- package/dist/core/storage/policy.js +110 -0
- package/dist/core/storage/process-log.d.ts +38 -0
- package/dist/core/storage/process-log.d.ts.map +1 -0
- package/dist/core/storage/process-log.js +165 -0
- package/dist/core/storage/query.d.ts +34 -0
- package/dist/core/storage/query.d.ts.map +1 -0
- package/dist/core/storage/query.js +295 -0
- package/dist/core/storage/receipts.d.ts +23 -0
- package/dist/core/storage/receipts.d.ts.map +1 -0
- package/dist/core/storage/receipts.js +117 -0
- package/dist/core/storage/segments.d.ts +84 -0
- package/dist/core/storage/segments.d.ts.map +1 -0
- package/dist/core/storage/segments.js +542 -0
- package/dist/core/storage/sink-graph.d.ts +65 -0
- package/dist/core/storage/sink-graph.d.ts.map +1 -0
- package/dist/core/storage/sink-graph.js +213 -0
- package/dist/core/storage/storm-control.d.ts +23 -0
- package/dist/core/storage/storm-control.d.ts.map +1 -0
- package/dist/core/storage/storm-control.js +59 -0
- package/dist/core/work/state.d.ts.map +1 -1
- package/dist/core/work/state.js +10 -14
- package/dist/core/workflow/live-session-v3.d.ts.map +1 -1
- package/dist/core/workflow/live-session-v3.js +2 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/lazy-commands.d.ts +26 -0
- package/dist/lazy-commands.d.ts.map +1 -0
- package/dist/lazy-commands.js +210 -0
- package/dist/lib/docs-lint.d.ts +2 -0
- package/dist/lib/docs-lint.d.ts.map +1 -1
- package/dist/lib/docs-lint.js +45 -80
- package/dist/lib/docs-metadata-audit.d.ts +3 -0
- package/dist/lib/docs-metadata-audit.d.ts.map +1 -1
- package/dist/lib/docs-metadata-audit.js +13 -23
- package/dist/lib/docs-metadata-sync.d.ts +1 -0
- package/dist/lib/docs-metadata-sync.d.ts.map +1 -1
- package/dist/lib/docs-metadata-sync.js +28 -10
- package/dist/lib/docs-repository-view.d.ts +22 -0
- package/dist/lib/docs-repository-view.d.ts.map +1 -0
- package/dist/lib/docs-repository-view.js +185 -0
- package/dist/lib/http/client.d.ts +11 -5
- package/dist/lib/http/client.d.ts.map +1 -1
- package/dist/lib/http/client.js +22 -4
- package/dist/lib/http/index.d.ts +1 -1
- package/dist/lib/http/index.d.ts.map +1 -1
- package/dist/lib/http/index.js +1 -1
- package/dist/lib/indexer-exclusions.d.ts +27 -0
- package/dist/lib/indexer-exclusions.d.ts.map +1 -0
- package/dist/lib/indexer-exclusions.js +176 -0
- package/dist/lib/instructions/templates.d.ts.map +1 -1
- package/dist/lib/instructions/templates.js +5 -0
- package/dist/lib/local-file-url.d.ts +9 -0
- package/dist/lib/local-file-url.d.ts.map +1 -0
- package/dist/lib/local-file-url.js +20 -0
- package/dist/lib/web-performance.d.ts +118 -0
- package/dist/lib/web-performance.d.ts.map +1 -0
- package/dist/lib/web-performance.js +192 -0
- package/package.json +29 -2
- package/schemas/config.schema.json +58 -0
- package/schemas/event-v3.schema.json +324 -112
- package/src/commander.ts +462 -95
- package/src/commands/agents.ts +517 -190
- package/src/commands/backup.ts +18 -14
- package/src/commands/browse-session.ts +12 -0
- package/src/commands/browse.ts +6 -7
- package/src/commands/conversations.ts +279 -0
- package/src/commands/deinit.ts +6 -0
- package/src/commands/docs.ts +25 -6
- package/src/commands/doctor.ts +75 -9
- package/src/commands/fetch.ts +7 -6
- package/src/commands/files.ts +143 -0
- package/src/commands/governor.ts +13 -12
- package/src/commands/inbox.ts +61 -0
- package/src/commands/init.ts +78 -16
- package/src/commands/ledger-v3.ts +421 -1
- package/src/commands/logs.ts +224 -0
- package/src/commands/messages.ts +61 -0
- package/src/commands/outline.ts +6 -2
- package/src/commands/read.ts +6 -2
- package/src/commands/semantic.ts +66 -1
- package/src/commands/storage.ts +310 -0
- package/src/commands/tunnel.ts +66 -52
- package/src/commands/web.ts +77 -7
- package/src/commands/work.ts +3 -3
- package/src/commands/workflow.ts +13 -13
- package/src/core/agents/cli.ts +259 -53
- package/src/core/agents/live-authority-v3.ts +341 -10
- package/src/core/agents/live-lifecycle-v3.ts +6 -1
- package/src/core/agents/live-observation-v3.ts +6 -1
- package/src/core/agents/render/prompt-context.ts +27 -6
- package/src/core/agents/rules/stop-hook.ts +107 -16
- package/src/core/agents/rules/verdict.ts +1 -0
- package/src/core/agents/session-events.ts +4 -0
- package/src/core/agents/session-finalization-state-v3.ts +1 -1
- package/src/core/agents/session-finalizer-v3.ts +4 -1
- package/src/core/agents/state/activity-log.ts +7 -2
- package/src/core/agents/state/heartbeat-writer.ts +27 -0
- package/src/core/agents/state/live-coordination-view.ts +16 -105
- package/src/core/agents/state/live-coordination-writer.ts +141 -0
- package/src/core/artifacts/index.ts +64 -1
- package/src/core/config.ts +126 -5
- package/src/core/conversations/archive.ts +237 -0
- package/src/core/conversations/catalog.ts +79 -0
- package/src/core/conversations/context-pack.ts +72 -0
- package/src/core/conversations/contract.ts +178 -0
- package/src/core/conversations/evaluation.ts +30 -0
- package/src/core/conversations/index.ts +9 -0
- package/src/core/conversations/lifecycle.ts +63 -0
- package/src/core/conversations/normalize.ts +105 -0
- package/src/core/conversations/query.ts +318 -0
- package/src/core/conversations/search-index.ts +48 -0
- package/src/core/events/legacy-storage/canary.ts +177 -0
- package/src/core/events/legacy-storage/fence.ts +129 -0
- package/src/core/events/legacy-storage/index.ts +5 -0
- package/src/core/events/legacy-storage/inventory.ts +51 -0
- package/src/core/events/legacy-storage/manifest.ts +136 -0
- package/src/core/events/legacy-storage/reader.ts +178 -0
- package/src/core/events/v3/authority-outbox.ts +22 -1
- package/src/core/events/v3/base-contract.ts +17 -7
- package/src/core/events/v3/bootstrap.ts +195 -2
- package/src/core/events/v3/capabilities.ts +3 -3
- package/src/core/events/v3/control-witness.ts +339 -0
- package/src/core/events/v3/control-writer.ts +16 -0
- package/src/core/events/v3/control.ts +54 -18
- package/src/core/events/v3/coordination-transaction-recovery.ts +477 -0
- package/src/core/events/v3/coordination-view.ts +17 -1
- package/src/core/events/v3/generated.ts +1 -1
- package/src/core/events/v3/index.ts +5 -2
- package/src/core/events/v3/live-route-observer.ts +80 -0
- package/src/core/events/v3/live-routing.ts +64 -46
- package/src/core/events/v3/producers/coordination-recorder.ts +95 -10
- package/src/core/events/v3/producers/diagnostic-summaries.ts +593 -0
- package/src/core/events/v3/producers/hook-base.ts +1 -1
- package/src/core/events/v3/producers/intake.ts +30 -2
- package/src/core/events/v3/producers/producer-state-observer.ts +52 -0
- package/src/core/events/v3/producers/recorder.ts +571 -33
- package/src/core/events/v3/reader.ts +243 -2
- package/src/core/events/v3/rotation-config.ts +98 -0
- package/src/core/events/v3/runtime-build.ts +41 -0
- package/src/core/events/v3/runtime-identity.ts +16 -0
- package/src/core/events/v3/runtime-telemetry-capabilities.ts +14 -8
- package/src/core/events/v3/support-storage/classify.ts +209 -0
- package/src/core/events/v3/support-storage/eligibility.ts +58 -0
- package/src/core/events/v3/support-storage/index.ts +9 -0
- package/src/core/events/v3/support-storage/inventory.ts +135 -0
- package/src/core/events/v3/support-storage/logical-authority.ts +117 -0
- package/src/core/events/v3/support-storage/maintenance.ts +501 -0
- package/src/core/events/v3/support-storage/pack-contract.ts +337 -0
- package/src/core/events/v3/support-storage/pack-reader.ts +346 -0
- package/src/core/events/v3/support-storage/pack-writer.ts +352 -0
- package/src/core/events/v3/support-storage/recovery.ts +43 -0
- package/src/core/events/v3/turn-telemetry.ts +58 -7
- package/src/core/events/v3/writer.ts +168 -16
- package/src/core/governor/plan-read.ts +5 -18
- package/src/core/governor/planning.ts +5 -8
- package/src/core/governor/service-read.ts +320 -2
- package/src/core/governor/service.ts +49 -22
- package/src/core/guard/evidence-v3.ts +1 -0
- package/src/core/hooks/adapter/events.ts +1 -0
- package/src/core/hooks/adapter/output.ts +12 -5
- package/src/core/hooks/adapter/runtime-telemetry.ts +408 -13
- package/src/core/hooks/cli.ts +226 -32
- package/src/core/hooks/effects/image-capture.ts +4 -1
- package/src/core/hooks/remediation-cap.ts +12 -6
- package/src/core/hooks/resolve/coord-root.ts +91 -11
- package/src/core/hooks/resolve/owner.ts +4 -0
- package/src/core/hooks/resolve/transcript.ts +18 -0
- package/src/core/inbox/contract.ts +81 -0
- package/src/core/inbox/index.ts +4 -0
- package/src/core/inbox/lifecycle.ts +104 -0
- package/src/core/inbox/service.ts +305 -0
- package/src/core/inbox/watcher.ts +36 -0
- package/src/core/presence/codec-digest.ts +15 -9
- package/src/core/presence/relay-client.ts +55 -7
- package/src/core/semantic/adapters.ts +200 -41
- package/src/core/semantic/contract.ts +135 -3
- package/src/core/semantic/index.ts +76 -2
- package/src/core/semantic/once.ts +93 -7
- package/src/core/semantic/prompt.ts +13 -1
- package/src/core/semantic/review.ts +803 -0
- package/src/core/semantic/routes.ts +30 -0
- package/src/core/semantic/scheduler.ts +49 -5
- package/src/core/semantic/service-status.ts +99 -5
- package/src/core/semantic/service.ts +116 -15
- package/src/core/semantic/soak.ts +638 -0
- package/src/core/semantic/storage.ts +20 -6
- package/src/core/semantic/usage.ts +272 -0
- package/src/core/semantic/validate.ts +31 -0
- package/src/core/storage/buffer.ts +124 -0
- package/src/core/storage/builtins.ts +916 -0
- package/src/core/storage/catalog.ts +467 -0
- package/src/core/storage/config.ts +423 -0
- package/src/core/storage/contract.ts +342 -0
- package/src/core/storage/durable-history.ts +738 -0
- package/src/core/storage/health.ts +76 -0
- package/src/core/storage/index.ts +15 -0
- package/src/core/storage/inventory.ts +859 -0
- package/src/core/storage/jsonl.ts +213 -0
- package/src/core/storage/log-retention.ts +555 -0
- package/src/core/storage/logger.ts +546 -0
- package/src/core/storage/maintenance-providers.ts +200 -0
- package/src/core/storage/maintenance.ts +929 -0
- package/src/core/storage/metrics.ts +199 -0
- package/src/core/storage/policy.ts +141 -0
- package/src/core/storage/process-log.ts +203 -0
- package/src/core/storage/query.ts +360 -0
- package/src/core/storage/receipts.ts +162 -0
- package/src/core/storage/segments.ts +721 -0
- package/src/core/storage/sink-graph.ts +343 -0
- package/src/core/storage/storm-control.ts +91 -0
- package/src/core/work/state.ts +12 -21
- package/src/core/workflow/live-session-v3.ts +2 -4
- package/src/index.ts +2 -0
- package/src/lazy-commands.ts +294 -0
- package/src/lib/docs-lint.ts +58 -77
- package/src/lib/docs-metadata-audit.ts +26 -21
- package/src/lib/docs-metadata-sync.ts +32 -9
- package/src/lib/docs-repository-view.ts +220 -0
- package/src/lib/http/client.ts +44 -8
- package/src/lib/http/index.ts +6 -1
- package/src/lib/indexer-exclusions.ts +173 -0
- package/src/lib/instructions/templates.ts +5 -0
- package/src/lib/local-file-url.ts +31 -0
- package/src/lib/web-performance.ts +335 -0
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
closeSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
fdatasyncSync,
|
|
6
|
+
constants as fsConstants,
|
|
7
|
+
fstatSync,
|
|
8
|
+
fsyncSync,
|
|
9
|
+
lstatSync,
|
|
10
|
+
mkdirSync,
|
|
11
|
+
openSync,
|
|
12
|
+
readFileSync,
|
|
13
|
+
readSync,
|
|
14
|
+
realpathSync,
|
|
15
|
+
renameSync,
|
|
16
|
+
rmSync,
|
|
17
|
+
unlinkSync,
|
|
18
|
+
writeSync,
|
|
19
|
+
} from "node:fs";
|
|
20
|
+
import { basename, dirname, isAbsolute, join, relative } from "node:path";
|
|
21
|
+
import { gzipSync } from "node:zlib";
|
|
22
|
+
import type { HarneryRegisteredStorageFamily } from "./contract.ts";
|
|
23
|
+
import { parseLogRecord } from "./jsonl.ts";
|
|
24
|
+
import type { HarneryLogMetricsDelta } from "./metrics.ts";
|
|
25
|
+
import { mergeMetricsSidecar } from "./metrics.ts";
|
|
26
|
+
|
|
27
|
+
export interface HarneryLogSegmentV1 {
|
|
28
|
+
sequence: number;
|
|
29
|
+
file: string;
|
|
30
|
+
bytes: number;
|
|
31
|
+
records: number;
|
|
32
|
+
first_emitted_at?: string;
|
|
33
|
+
last_emitted_at?: string;
|
|
34
|
+
sealed_at: string;
|
|
35
|
+
sha256: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface HarneryLogManifestV1 {
|
|
39
|
+
schema: "harnery.log-manifest/v1";
|
|
40
|
+
family_id: string;
|
|
41
|
+
policy_version: string;
|
|
42
|
+
/** Highest lifetime sequence intentionally expired by retention. */
|
|
43
|
+
pruned_through_sequence?: number;
|
|
44
|
+
next_sequence: number;
|
|
45
|
+
segments: readonly HarneryLogSegmentV1[];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface PruneSealedLogSegmentOptions {
|
|
49
|
+
directory: string;
|
|
50
|
+
family: HarneryRegisteredStorageFamily;
|
|
51
|
+
sequence: number;
|
|
52
|
+
file: string;
|
|
53
|
+
expected_bytes: number;
|
|
54
|
+
/** Digest of the compressed file at the exact target path. */
|
|
55
|
+
expected_file_sha256: string;
|
|
56
|
+
/** Digest of the uncompressed JSONL recorded by the manifest. */
|
|
57
|
+
expected_content_sha256: string;
|
|
58
|
+
/** Semantic fingerprint of the exact manifest state frozen at planning. */
|
|
59
|
+
expected_manifest_fingerprint: string;
|
|
60
|
+
/** Semantic fingerprint of the reduced manifest committed by this action. */
|
|
61
|
+
result_manifest_fingerprint: string;
|
|
62
|
+
lease_timeout_ms?: number;
|
|
63
|
+
lease_retry_ms?: number;
|
|
64
|
+
lease_stale_ms?: number;
|
|
65
|
+
/** Fault-injection seam after the manifest commit and before unlink. */
|
|
66
|
+
after_manifest_commit?: () => void;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface SegmentAppendOptions {
|
|
70
|
+
directory: string;
|
|
71
|
+
family: HarneryRegisteredStorageFamily;
|
|
72
|
+
max_segment_bytes?: number;
|
|
73
|
+
max_open_age_ms?: number;
|
|
74
|
+
minimum_day_segment_bytes?: number;
|
|
75
|
+
lease_timeout_ms?: number;
|
|
76
|
+
lease_retry_ms?: number;
|
|
77
|
+
lease_stale_ms?: number;
|
|
78
|
+
/** Fault-injection seam; production callers use the default syscall. */
|
|
79
|
+
write_sync?: SegmentWriteSync;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type SegmentWriteSync = (
|
|
83
|
+
fd: number,
|
|
84
|
+
buffer: Buffer,
|
|
85
|
+
offset: number,
|
|
86
|
+
length: number,
|
|
87
|
+
) => number;
|
|
88
|
+
|
|
89
|
+
export interface SegmentAppendResult {
|
|
90
|
+
appended_records: number;
|
|
91
|
+
appended_bytes: number;
|
|
92
|
+
rotated: boolean;
|
|
93
|
+
synced: boolean;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
interface ResolvedSegmentOptions {
|
|
97
|
+
directory: string;
|
|
98
|
+
family: HarneryRegisteredStorageFamily;
|
|
99
|
+
max_segment_bytes: number;
|
|
100
|
+
max_open_age_ms: number;
|
|
101
|
+
minimum_day_segment_bytes: number;
|
|
102
|
+
lease_timeout_ms: number;
|
|
103
|
+
lease_retry_ms: number;
|
|
104
|
+
lease_stale_ms: number;
|
|
105
|
+
write_sync: SegmentWriteSync;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export class HarneryLogLeaseError extends Error {
|
|
109
|
+
constructor(
|
|
110
|
+
readonly reason: "timeout" | "wrong_type" | "invalid_owner",
|
|
111
|
+
message: string,
|
|
112
|
+
) {
|
|
113
|
+
super(message);
|
|
114
|
+
this.name = "HarneryLogLeaseError";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function familyLogDirectory(family: HarneryRegisteredStorageFamily): string {
|
|
119
|
+
const root = family.resolved_roots.find(
|
|
120
|
+
(candidate) => candidate.match === "provider-partition" && candidate.ownership !== "external",
|
|
121
|
+
);
|
|
122
|
+
if (!root?.partition) throw new Error(`family ${family.id} has no writable log partition`);
|
|
123
|
+
return join(root.path, root.partition);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export class FileSegmentSink {
|
|
127
|
+
readonly #options: ResolvedSegmentOptions;
|
|
128
|
+
|
|
129
|
+
constructor(options: SegmentAppendOptions) {
|
|
130
|
+
this.#options = {
|
|
131
|
+
...options,
|
|
132
|
+
max_segment_bytes:
|
|
133
|
+
options.max_segment_bytes ??
|
|
134
|
+
options.family.policy.rotation.max_segment_bytes.limit ??
|
|
135
|
+
10 * 1024 * 1024,
|
|
136
|
+
max_open_age_ms:
|
|
137
|
+
options.max_open_age_ms ?? options.family.policy.rotation.max_open_age.limit ?? 86_400_000,
|
|
138
|
+
minimum_day_segment_bytes: options.minimum_day_segment_bytes ?? 256 * 1024,
|
|
139
|
+
lease_timeout_ms:
|
|
140
|
+
options.lease_timeout_ms ?? options.family.lease_policy?.acquisition_timeout_ms ?? 2_000,
|
|
141
|
+
lease_retry_ms: options.lease_retry_ms ?? options.family.lease_policy?.retry_backoff_ms ?? 10,
|
|
142
|
+
lease_stale_ms:
|
|
143
|
+
options.lease_stale_ms ?? options.family.lease_policy?.stale_owner_ms ?? 30_000,
|
|
144
|
+
write_sync: options.write_sync ?? nativeWriteSync,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async append(
|
|
149
|
+
records: readonly Buffer[],
|
|
150
|
+
metrics: HarneryLogMetricsDelta = {},
|
|
151
|
+
durable = false,
|
|
152
|
+
now = new Date(),
|
|
153
|
+
): Promise<SegmentAppendResult> {
|
|
154
|
+
if (records.length === 0)
|
|
155
|
+
return { appended_records: 0, appended_bytes: 0, rotated: false, synced: false };
|
|
156
|
+
return withFamilyLease(this.#options.directory, this.#options, async () => {
|
|
157
|
+
const directory = ensurePrivateDirectory(this.#options.directory);
|
|
158
|
+
const active = join(this.#options.directory, "active.jsonl");
|
|
159
|
+
let rotated = false;
|
|
160
|
+
if (existsSync(active) && shouldRotate(active, records, this.#options, now)) {
|
|
161
|
+
sealActive(active, this.#options, now);
|
|
162
|
+
rotated = true;
|
|
163
|
+
}
|
|
164
|
+
rejectExistingSymlink(active);
|
|
165
|
+
const fd = openSync(
|
|
166
|
+
active,
|
|
167
|
+
fsConstants.O_RDWR | fsConstants.O_APPEND | fsConstants.O_CREAT | noFollowFlag(),
|
|
168
|
+
0o600,
|
|
169
|
+
);
|
|
170
|
+
let bytes = 0;
|
|
171
|
+
try {
|
|
172
|
+
assertOpenFileIdentity(active, fd, directory);
|
|
173
|
+
assertCompleteTail(fd, active);
|
|
174
|
+
for (const record of records) {
|
|
175
|
+
writeAllSync(fd, record, this.#options.write_sync);
|
|
176
|
+
bytes += record.byteLength;
|
|
177
|
+
}
|
|
178
|
+
if (durable) fdatasyncSync(fd);
|
|
179
|
+
assertOpenFileIdentity(active, fd, directory);
|
|
180
|
+
} finally {
|
|
181
|
+
closeSync(fd);
|
|
182
|
+
}
|
|
183
|
+
mergeMetricsSidecar(
|
|
184
|
+
join(this.#options.directory, "metrics.json"),
|
|
185
|
+
{
|
|
186
|
+
...metrics,
|
|
187
|
+
appended: (metrics.appended ?? 0) + records.length,
|
|
188
|
+
bytes_appended: (metrics.bytes_appended ?? 0) + bytes,
|
|
189
|
+
rotations: (metrics.rotations ?? 0) + (rotated ? 1 : 0),
|
|
190
|
+
synced: (metrics.synced ?? 0) + (durable ? records.length : 0),
|
|
191
|
+
},
|
|
192
|
+
now,
|
|
193
|
+
);
|
|
194
|
+
return { appended_records: records.length, appended_bytes: bytes, rotated, synced: durable };
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function readSegmentManifest(
|
|
200
|
+
directory: string,
|
|
201
|
+
family?: HarneryRegisteredStorageFamily,
|
|
202
|
+
): HarneryLogManifestV1 {
|
|
203
|
+
const path = join(directory, "manifest.json");
|
|
204
|
+
if (!existsSync(path)) {
|
|
205
|
+
if (!family) throw new Error(`missing log manifest: ${path}`);
|
|
206
|
+
return emptyManifest(family);
|
|
207
|
+
}
|
|
208
|
+
const value = JSON.parse(
|
|
209
|
+
readRegularFile(path, 4 * 1024 * 1024).toString("utf8"),
|
|
210
|
+
) as HarneryLogManifestV1;
|
|
211
|
+
if (value.schema !== "harnery.log-manifest/v1" || !Array.isArray(value.segments)) {
|
|
212
|
+
throw new Error(`invalid log manifest: ${path}`);
|
|
213
|
+
}
|
|
214
|
+
if (
|
|
215
|
+
family &&
|
|
216
|
+
(value.family_id !== family.id || value.policy_version !== family.policy.policy_version)
|
|
217
|
+
) {
|
|
218
|
+
throw new Error(`log manifest identity mismatch: ${path}`);
|
|
219
|
+
}
|
|
220
|
+
const prunedThrough = value.pruned_through_sequence ?? 0;
|
|
221
|
+
if (
|
|
222
|
+
!Number.isSafeInteger(prunedThrough) ||
|
|
223
|
+
prunedThrough < 0 ||
|
|
224
|
+
!Number.isSafeInteger(value.next_sequence) ||
|
|
225
|
+
value.next_sequence < 1 ||
|
|
226
|
+
prunedThrough >= value.next_sequence ||
|
|
227
|
+
value.segments.some(
|
|
228
|
+
(segment, index) =>
|
|
229
|
+
segment.sequence !== prunedThrough + index + 1 ||
|
|
230
|
+
!/^segments\/[0-9]{8}-[0-9]{8}\.jsonl\.gz$/.test(segment.file) ||
|
|
231
|
+
!Number.isSafeInteger(segment.bytes) ||
|
|
232
|
+
segment.bytes <= 0 ||
|
|
233
|
+
!Number.isFinite(Date.parse(segment.sealed_at)) ||
|
|
234
|
+
(index > 0 &&
|
|
235
|
+
Date.parse(segment.sealed_at) < Date.parse(value.segments[index - 1]!.sealed_at)) ||
|
|
236
|
+
!/^[a-f0-9]{64}$/.test(segment.sha256),
|
|
237
|
+
) ||
|
|
238
|
+
value.next_sequence !== prunedThrough + value.segments.length + 1
|
|
239
|
+
) {
|
|
240
|
+
throw new Error(`invalid log manifest sequence: ${path}`);
|
|
241
|
+
}
|
|
242
|
+
return value;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export function logManifestFingerprint(manifest: HarneryLogManifestV1): string {
|
|
246
|
+
return createHash("sha256").update(JSON.stringify(manifest)).digest("hex");
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Expire exactly the oldest retained segment. The manifest is authoritative,
|
|
251
|
+
* so it commits before unlink. Replays cover all four crash states without
|
|
252
|
+
* ever accepting an absent source from an old manifest.
|
|
253
|
+
*/
|
|
254
|
+
export async function pruneSealedLogSegment(
|
|
255
|
+
options: PruneSealedLogSegmentOptions,
|
|
256
|
+
): Promise<"applied" | "already_applied"> {
|
|
257
|
+
if (!Number.isSafeInteger(options.sequence) || options.sequence < 1) {
|
|
258
|
+
throw new Error("invalid log retention sequence");
|
|
259
|
+
}
|
|
260
|
+
if (!/^segments\/[0-9]{8}-[0-9]{8}\.jsonl\.gz$/.test(options.file)) {
|
|
261
|
+
throw new Error("invalid log retention segment path");
|
|
262
|
+
}
|
|
263
|
+
if (!Number.isSafeInteger(options.expected_bytes) || options.expected_bytes <= 0) {
|
|
264
|
+
throw new Error("invalid log retention segment bytes");
|
|
265
|
+
}
|
|
266
|
+
if (
|
|
267
|
+
!/^[a-f0-9]{64}$/.test(options.expected_file_sha256) ||
|
|
268
|
+
!/^[a-f0-9]{64}$/.test(options.expected_content_sha256) ||
|
|
269
|
+
!/^[a-f0-9]{64}$/.test(options.expected_manifest_fingerprint) ||
|
|
270
|
+
!/^[a-f0-9]{64}$/.test(options.result_manifest_fingerprint)
|
|
271
|
+
) {
|
|
272
|
+
throw new Error("invalid log retention segment digest");
|
|
273
|
+
}
|
|
274
|
+
const policy = {
|
|
275
|
+
lease_timeout_ms:
|
|
276
|
+
options.lease_timeout_ms ?? options.family.lease_policy?.acquisition_timeout_ms ?? 2_000,
|
|
277
|
+
lease_retry_ms: options.lease_retry_ms ?? options.family.lease_policy?.retry_backoff_ms ?? 10,
|
|
278
|
+
lease_stale_ms: options.lease_stale_ms ?? options.family.lease_policy?.stale_owner_ms ?? 30_000,
|
|
279
|
+
};
|
|
280
|
+
return withFamilyLease(options.directory, policy, () => {
|
|
281
|
+
const manifest = readSegmentManifest(options.directory, options.family);
|
|
282
|
+
const currentManifestFingerprint = logManifestFingerprint(manifest);
|
|
283
|
+
const prunedThrough = manifest.pruned_through_sequence ?? 0;
|
|
284
|
+
const target = containedSegmentPath(options.directory, options.file);
|
|
285
|
+
if (options.sequence <= prunedThrough) {
|
|
286
|
+
if (currentManifestFingerprint !== options.result_manifest_fingerprint) {
|
|
287
|
+
throw new Error("log retention manifest changed after the planned prune");
|
|
288
|
+
}
|
|
289
|
+
if (!pathExists(target)) return "already_applied";
|
|
290
|
+
assertExactSegment(target, options.expected_bytes, options.expected_file_sha256);
|
|
291
|
+
unlinkSync(target);
|
|
292
|
+
return "applied";
|
|
293
|
+
}
|
|
294
|
+
if (options.sequence !== prunedThrough + 1) {
|
|
295
|
+
throw new Error("log retention segment is not the oldest retained sequence");
|
|
296
|
+
}
|
|
297
|
+
if (currentManifestFingerprint !== options.expected_manifest_fingerprint) {
|
|
298
|
+
throw new Error("log retention manifest changed after planning");
|
|
299
|
+
}
|
|
300
|
+
const entry = manifest.segments[0];
|
|
301
|
+
if (
|
|
302
|
+
!entry ||
|
|
303
|
+
entry.sequence !== options.sequence ||
|
|
304
|
+
entry.file !== options.file ||
|
|
305
|
+
entry.sha256 !== options.expected_content_sha256
|
|
306
|
+
) {
|
|
307
|
+
throw new Error("log retention segment does not match the current manifest");
|
|
308
|
+
}
|
|
309
|
+
if (!pathExists(target)) {
|
|
310
|
+
throw new Error("log retention source is absent while the manifest still references it");
|
|
311
|
+
}
|
|
312
|
+
assertExactSegment(target, options.expected_bytes, options.expected_file_sha256);
|
|
313
|
+
const next: HarneryLogManifestV1 = {
|
|
314
|
+
...manifest,
|
|
315
|
+
pruned_through_sequence: options.sequence,
|
|
316
|
+
segments: manifest.segments.slice(1),
|
|
317
|
+
};
|
|
318
|
+
if (logManifestFingerprint(next) !== options.result_manifest_fingerprint) {
|
|
319
|
+
throw new Error("log retention predicted manifest does not match apply state");
|
|
320
|
+
}
|
|
321
|
+
atomicJson(join(options.directory, "manifest.json"), next);
|
|
322
|
+
options.after_manifest_commit?.();
|
|
323
|
+
assertExactSegment(target, options.expected_bytes, options.expected_file_sha256);
|
|
324
|
+
unlinkSync(target);
|
|
325
|
+
return "applied";
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export async function withFamilyLease<T>(
|
|
330
|
+
directory: string,
|
|
331
|
+
policy: { lease_timeout_ms: number; lease_retry_ms: number; lease_stale_ms: number },
|
|
332
|
+
operation: () => Promise<T> | T,
|
|
333
|
+
): Promise<T> {
|
|
334
|
+
ensurePrivateDirectory(directory);
|
|
335
|
+
const lease = join(directory, ".append-lease");
|
|
336
|
+
const started = Date.now();
|
|
337
|
+
let owner: LeaseOwner | undefined;
|
|
338
|
+
while (true) {
|
|
339
|
+
try {
|
|
340
|
+
mkdirSync(lease, { mode: 0o700 });
|
|
341
|
+
const leaseIdentity = captureDirectoryIdentity(lease);
|
|
342
|
+
owner = {
|
|
343
|
+
owner_id: randomUUID(),
|
|
344
|
+
pid: process.pid,
|
|
345
|
+
acquired_at: new Date().toISOString(),
|
|
346
|
+
};
|
|
347
|
+
writeNewFile(join(lease, "owner.json"), Buffer.from(`${JSON.stringify(owner)}\n`));
|
|
348
|
+
assertDirectoryIdentity(leaseIdentity);
|
|
349
|
+
break;
|
|
350
|
+
} catch (error) {
|
|
351
|
+
if (pathExists(lease) && !safeLeaseDirectory(lease)) {
|
|
352
|
+
throw new HarneryLogLeaseError("wrong_type", `log lease is not a directory: ${lease}`);
|
|
353
|
+
}
|
|
354
|
+
if (recoverStaleLease(lease, policy.lease_stale_ms)) continue;
|
|
355
|
+
if (Date.now() - started >= policy.lease_timeout_ms) {
|
|
356
|
+
throw new HarneryLogLeaseError("timeout", `timed out acquiring log lease: ${lease}`);
|
|
357
|
+
}
|
|
358
|
+
await new Promise((resolve) => setTimeout(resolve, policy.lease_retry_ms));
|
|
359
|
+
void error;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
try {
|
|
363
|
+
return await operation();
|
|
364
|
+
} finally {
|
|
365
|
+
if (owner) releaseOwnedLease(lease, owner);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function shouldRotate(
|
|
370
|
+
active: string,
|
|
371
|
+
incoming: readonly Buffer[],
|
|
372
|
+
options: ResolvedSegmentOptions,
|
|
373
|
+
now: Date,
|
|
374
|
+
): boolean {
|
|
375
|
+
const stat = regularFileStat(active);
|
|
376
|
+
const incomingBytes = incoming.reduce((sum, record) => sum + record.byteLength, 0);
|
|
377
|
+
if (stat.size + incomingBytes > options.max_segment_bytes) return stat.size > 0;
|
|
378
|
+
const age = now.getTime() - stat.mtimeMs;
|
|
379
|
+
if (age >= options.max_open_age_ms) return stat.size > 0;
|
|
380
|
+
const opened = new Date(stat.mtimeMs);
|
|
381
|
+
return (
|
|
382
|
+
opened.toDateString() !== now.toDateString() && stat.size >= options.minimum_day_segment_bytes
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
function sealActive(active: string, options: ResolvedSegmentOptions, now: Date): void {
|
|
387
|
+
const family = options.family;
|
|
388
|
+
const directory = dirname(active);
|
|
389
|
+
const manifest = readSegmentManifest(directory, family);
|
|
390
|
+
const maximumActiveBytes = Math.max(
|
|
391
|
+
options.max_segment_bytes,
|
|
392
|
+
family.policy.records.max_record_bytes.limit ?? 1024 * 1024,
|
|
393
|
+
);
|
|
394
|
+
const content = readRegularFile(active, maximumActiveBytes);
|
|
395
|
+
if (content.byteLength === 0) return;
|
|
396
|
+
const lines = validateCompleteLogJsonl(content, active, family);
|
|
397
|
+
const sequence = manifest.next_sequence;
|
|
398
|
+
const name = `${now.toISOString().slice(0, 10).replaceAll("-", "")}-${String(sequence).padStart(8, "0")}.jsonl.gz`;
|
|
399
|
+
const target = join(directory, "segments", name);
|
|
400
|
+
mkdirSync(dirname(target), { recursive: true, mode: 0o700 });
|
|
401
|
+
const temporary = `${target}.tmp-${process.pid}-${randomUUID()}`;
|
|
402
|
+
const compressed = gzipSync(content);
|
|
403
|
+
writeNewFile(temporary, compressed);
|
|
404
|
+
rejectExistingSymlink(target);
|
|
405
|
+
renameSync(temporary, target);
|
|
406
|
+
assertRegularPath(target, captureDirectoryIdentity(dirname(target)));
|
|
407
|
+
const timestamps = lines
|
|
408
|
+
.map((record) => record.emitted_at)
|
|
409
|
+
.filter((value): value is string => Boolean(value));
|
|
410
|
+
const next: HarneryLogManifestV1 = {
|
|
411
|
+
...manifest,
|
|
412
|
+
next_sequence: sequence + 1,
|
|
413
|
+
segments: [
|
|
414
|
+
...manifest.segments,
|
|
415
|
+
{
|
|
416
|
+
sequence,
|
|
417
|
+
file: join("segments", basename(target)).replaceAll("\\", "/"),
|
|
418
|
+
bytes: content.byteLength,
|
|
419
|
+
records: lines.length,
|
|
420
|
+
...(timestamps[0] ? { first_emitted_at: timestamps[0] } : {}),
|
|
421
|
+
...(timestamps.at(-1) ? { last_emitted_at: timestamps.at(-1) } : {}),
|
|
422
|
+
sealed_at: now.toISOString(),
|
|
423
|
+
sha256: createHash("sha256").update(content).digest("hex"),
|
|
424
|
+
},
|
|
425
|
+
],
|
|
426
|
+
};
|
|
427
|
+
const sealedManifest = join(directory, "manifests", `${String(sequence).padStart(8, "0")}.json`);
|
|
428
|
+
mkdirSync(dirname(sealedManifest), { recursive: true, mode: 0o700 });
|
|
429
|
+
writeNewFile(sealedManifest, Buffer.from(`${JSON.stringify(next)}\n`));
|
|
430
|
+
atomicJson(join(directory, "manifest.json"), next);
|
|
431
|
+
truncateRegularFile(active);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
function emptyManifest(family: HarneryRegisteredStorageFamily): HarneryLogManifestV1 {
|
|
435
|
+
return {
|
|
436
|
+
schema: "harnery.log-manifest/v1",
|
|
437
|
+
family_id: family.id,
|
|
438
|
+
policy_version: family.policy.policy_version,
|
|
439
|
+
next_sequence: 1,
|
|
440
|
+
segments: [],
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function containedSegmentPath(directory: string, relativePath: string): string {
|
|
445
|
+
const path = join(directory, ...relativePath.split("/"));
|
|
446
|
+
const parent = captureDirectoryIdentity(dirname(path));
|
|
447
|
+
const fromRoot = relative(realpathSync(directory), parent.real_path);
|
|
448
|
+
if (fromRoot.startsWith("..") || isAbsolute(fromRoot)) {
|
|
449
|
+
throw new Error("log retention segment escapes its family directory");
|
|
450
|
+
}
|
|
451
|
+
return path;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
function assertExactSegment(path: string, expectedBytes: number, expectedSha256: string): void {
|
|
455
|
+
const stat = regularFileStat(path);
|
|
456
|
+
if (stat.nlink !== 1) throw new Error(`log retention rejects hard-linked segment: ${path}`);
|
|
457
|
+
if (stat.size !== expectedBytes) throw new Error(`log retention segment size changed: ${path}`);
|
|
458
|
+
const bytes = readRegularFile(path, expectedBytes);
|
|
459
|
+
if (createHash("sha256").update(bytes).digest("hex") !== expectedSha256) {
|
|
460
|
+
throw new Error(`log retention segment digest changed: ${path}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function atomicJson(path: string, value: unknown): void {
|
|
465
|
+
const temporary = `${path}.tmp-${process.pid}-${randomUUID()}`;
|
|
466
|
+
writeNewFile(temporary, Buffer.from(`${JSON.stringify(value)}\n`));
|
|
467
|
+
rejectExistingSymlink(path);
|
|
468
|
+
renameSync(temporary, path);
|
|
469
|
+
assertRegularPath(path, captureDirectoryIdentity(dirname(path)));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
interface LeaseOwner {
|
|
473
|
+
owner_id: string;
|
|
474
|
+
pid: number;
|
|
475
|
+
acquired_at: string;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function recoverStaleLease(lease: string, maximumAgeMs: number): boolean {
|
|
479
|
+
let identity: DirectoryIdentity;
|
|
480
|
+
let raw: string;
|
|
481
|
+
try {
|
|
482
|
+
identity = captureDirectoryIdentity(lease);
|
|
483
|
+
raw = readRegularFile(join(lease, "owner.json"), 4_096).toString("utf8");
|
|
484
|
+
} catch {
|
|
485
|
+
return false;
|
|
486
|
+
}
|
|
487
|
+
let owner: LeaseOwner;
|
|
488
|
+
try {
|
|
489
|
+
owner = JSON.parse(raw) as LeaseOwner;
|
|
490
|
+
} catch {
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
if (
|
|
494
|
+
typeof owner.owner_id !== "string" ||
|
|
495
|
+
owner.owner_id.length === 0 ||
|
|
496
|
+
!Number.isSafeInteger(owner.pid) ||
|
|
497
|
+
!Number.isFinite(Date.parse(owner.acquired_at)) ||
|
|
498
|
+
Date.now() - Date.parse(owner.acquired_at) < maximumAgeMs ||
|
|
499
|
+
processIsAlive(owner.pid)
|
|
500
|
+
) {
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
try {
|
|
504
|
+
assertDirectoryIdentity(identity);
|
|
505
|
+
if (readRegularFile(join(lease, "owner.json"), 4_096).toString("utf8") !== raw) return false;
|
|
506
|
+
rmSync(lease, { recursive: true, force: false });
|
|
507
|
+
return true;
|
|
508
|
+
} catch {
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function releaseOwnedLease(lease: string, owner: LeaseOwner): void {
|
|
514
|
+
try {
|
|
515
|
+
const current = JSON.parse(
|
|
516
|
+
readRegularFile(join(lease, "owner.json"), 4_096).toString("utf8"),
|
|
517
|
+
) as LeaseOwner;
|
|
518
|
+
if (current.owner_id !== owner.owner_id) return;
|
|
519
|
+
rmSync(lease, { recursive: true, force: false });
|
|
520
|
+
} catch {
|
|
521
|
+
// A missing or replaced lease does not belong to this operation.
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
function processIsAlive(pid: number): boolean {
|
|
526
|
+
try {
|
|
527
|
+
process.kill(pid, 0);
|
|
528
|
+
return true;
|
|
529
|
+
} catch (error) {
|
|
530
|
+
return (error as NodeJS.ErrnoException).code !== "ESRCH";
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
interface DirectoryIdentity {
|
|
535
|
+
path: string;
|
|
536
|
+
real_path: string;
|
|
537
|
+
dev: number;
|
|
538
|
+
ino: number;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
function ensurePrivateDirectory(path: string): DirectoryIdentity {
|
|
542
|
+
mkdirSync(path, { recursive: true, mode: 0o700 });
|
|
543
|
+
return captureDirectoryIdentity(path);
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function captureDirectoryIdentity(path: string): DirectoryIdentity {
|
|
547
|
+
const stat = lstatSync(path);
|
|
548
|
+
if (stat.isSymbolicLink()) throw new Error(`log storage rejects symlink: ${path}`);
|
|
549
|
+
if (!stat.isDirectory()) throw new Error(`log storage path is not a directory: ${path}`);
|
|
550
|
+
return { path, real_path: realpathSync(path), dev: stat.dev, ino: stat.ino };
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
function assertDirectoryIdentity(identity: DirectoryIdentity): void {
|
|
554
|
+
const current = lstatSync(identity.path);
|
|
555
|
+
if (
|
|
556
|
+
current.isSymbolicLink() ||
|
|
557
|
+
!current.isDirectory() ||
|
|
558
|
+
current.dev !== identity.dev ||
|
|
559
|
+
current.ino !== identity.ino ||
|
|
560
|
+
realpathSync(identity.path) !== identity.real_path
|
|
561
|
+
) {
|
|
562
|
+
throw new Error(`log storage directory identity changed: ${identity.path}`);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
function rejectExistingSymlink(path: string): void {
|
|
567
|
+
try {
|
|
568
|
+
if (lstatSync(path).isSymbolicLink()) throw new Error(`log storage rejects symlink: ${path}`);
|
|
569
|
+
} catch (error) {
|
|
570
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
function assertOpenFileIdentity(path: string, fd: number, parent: DirectoryIdentity): void {
|
|
575
|
+
assertDirectoryIdentity(parent);
|
|
576
|
+
const opened = fstatSync(fd);
|
|
577
|
+
const current = lstatSync(path);
|
|
578
|
+
if (
|
|
579
|
+
!opened.isFile() ||
|
|
580
|
+
current.isSymbolicLink() ||
|
|
581
|
+
!current.isFile() ||
|
|
582
|
+
opened.dev !== current.dev ||
|
|
583
|
+
opened.ino !== current.ino
|
|
584
|
+
) {
|
|
585
|
+
throw new Error(`log storage path identity changed: ${path}`);
|
|
586
|
+
}
|
|
587
|
+
const fromParent = relative(parent.real_path, realpathSync(path));
|
|
588
|
+
if (fromParent.startsWith("..") || isAbsolute(fromParent)) {
|
|
589
|
+
throw new Error(`log storage path escapes parent boundary: ${path}`);
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
function assertRegularPath(path: string, parent: DirectoryIdentity): void {
|
|
594
|
+
const fd = openSync(path, fsConstants.O_RDONLY | noFollowFlag());
|
|
595
|
+
try {
|
|
596
|
+
assertOpenFileIdentity(path, fd, parent);
|
|
597
|
+
} finally {
|
|
598
|
+
closeSync(fd);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function regularFileStat(path: string) {
|
|
603
|
+
const parent = captureDirectoryIdentity(dirname(path));
|
|
604
|
+
rejectExistingSymlink(path);
|
|
605
|
+
const fd = openSync(path, fsConstants.O_RDONLY | noFollowFlag());
|
|
606
|
+
try {
|
|
607
|
+
assertOpenFileIdentity(path, fd, parent);
|
|
608
|
+
return fstatSync(fd);
|
|
609
|
+
} finally {
|
|
610
|
+
closeSync(fd);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
function readRegularFile(path: string, maximumBytes: number): Buffer {
|
|
615
|
+
const parent = captureDirectoryIdentity(dirname(path));
|
|
616
|
+
rejectExistingSymlink(path);
|
|
617
|
+
const fd = openSync(path, fsConstants.O_RDONLY | noFollowFlag());
|
|
618
|
+
try {
|
|
619
|
+
assertOpenFileIdentity(path, fd, parent);
|
|
620
|
+
const stat = fstatSync(fd);
|
|
621
|
+
if (stat.size > maximumBytes) throw new Error(`log storage file exceeds bound: ${path}`);
|
|
622
|
+
const content = readFileSync(fd);
|
|
623
|
+
assertOpenFileIdentity(path, fd, parent);
|
|
624
|
+
return content;
|
|
625
|
+
} finally {
|
|
626
|
+
closeSync(fd);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function writeNewFile(path: string, content: Buffer): void {
|
|
631
|
+
const parent = captureDirectoryIdentity(dirname(path));
|
|
632
|
+
const fd = openSync(
|
|
633
|
+
path,
|
|
634
|
+
fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_EXCL | noFollowFlag(),
|
|
635
|
+
0o600,
|
|
636
|
+
);
|
|
637
|
+
try {
|
|
638
|
+
assertOpenFileIdentity(path, fd, parent);
|
|
639
|
+
writeAllSync(fd, content, nativeWriteSync);
|
|
640
|
+
fsyncSync(fd);
|
|
641
|
+
assertOpenFileIdentity(path, fd, parent);
|
|
642
|
+
} finally {
|
|
643
|
+
closeSync(fd);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
function truncateRegularFile(path: string): void {
|
|
648
|
+
const parent = captureDirectoryIdentity(dirname(path));
|
|
649
|
+
rejectExistingSymlink(path);
|
|
650
|
+
const fd = openSync(path, fsConstants.O_WRONLY | fsConstants.O_TRUNC | noFollowFlag());
|
|
651
|
+
try {
|
|
652
|
+
fdatasyncSync(fd);
|
|
653
|
+
assertOpenFileIdentity(path, fd, parent);
|
|
654
|
+
} finally {
|
|
655
|
+
closeSync(fd);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
function assertCompleteTail(fd: number, path: string): void {
|
|
660
|
+
const stat = fstatSync(fd);
|
|
661
|
+
if (stat.size === 0) return;
|
|
662
|
+
const last = Buffer.allocUnsafe(1);
|
|
663
|
+
if (readSync(fd, last, 0, 1, stat.size - 1) !== 1 || last[0] !== 10) {
|
|
664
|
+
throw new Error(`log storage has a partial JSONL record: ${path}`);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
function validateCompleteLogJsonl(
|
|
669
|
+
content: Buffer,
|
|
670
|
+
path: string,
|
|
671
|
+
family: HarneryRegisteredStorageFamily,
|
|
672
|
+
) {
|
|
673
|
+
if (content.at(-1) !== 10) throw new Error(`log storage has a partial JSONL record: ${path}`);
|
|
674
|
+
const rawLines = content.toString("utf8").split("\n");
|
|
675
|
+
rawLines.pop();
|
|
676
|
+
return rawLines.map((line) => {
|
|
677
|
+
if (!line) throw new Error(`log storage has a malformed JSONL record: ${path}`);
|
|
678
|
+
const record = parseLogRecord(line);
|
|
679
|
+
if (record.family_id !== family.id || record.policy_version !== family.policy.policy_version) {
|
|
680
|
+
throw new Error(`log storage record identity mismatch: ${path}`);
|
|
681
|
+
}
|
|
682
|
+
return record;
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
function writeAllSync(fd: number, buffer: Buffer, writer: SegmentWriteSync): void {
|
|
687
|
+
let offset = 0;
|
|
688
|
+
while (offset < buffer.byteLength) {
|
|
689
|
+
const written = writer(fd, buffer, offset, buffer.byteLength - offset);
|
|
690
|
+
if (!Number.isSafeInteger(written) || written <= 0 || written > buffer.byteLength - offset) {
|
|
691
|
+
throw new Error("log storage write made no forward progress");
|
|
692
|
+
}
|
|
693
|
+
offset += written;
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
function nativeWriteSync(fd: number, buffer: Buffer, offset: number, length: number): number {
|
|
698
|
+
return writeSync(fd, buffer, offset, length);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function safeLeaseDirectory(path: string): boolean {
|
|
702
|
+
try {
|
|
703
|
+
return !lstatSync(path).isSymbolicLink() && lstatSync(path).isDirectory();
|
|
704
|
+
} catch {
|
|
705
|
+
return false;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
function pathExists(path: string): boolean {
|
|
710
|
+
try {
|
|
711
|
+
lstatSync(path);
|
|
712
|
+
return true;
|
|
713
|
+
} catch (error) {
|
|
714
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return false;
|
|
715
|
+
throw error;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function noFollowFlag(): number {
|
|
720
|
+
return fsConstants.O_NOFOLLOW ?? 0;
|
|
721
|
+
}
|