@namzu/sdk 38.1.0 → 39.0.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/CHANGELOG.md +758 -0
- package/dist/advisory/executor.d.ts +10 -1
- package/dist/advisory/executor.d.ts.map +1 -1
- package/dist/advisory/executor.js +5 -26
- package/dist/advisory/executor.js.map +1 -1
- package/dist/advisory/history.d.ts +9 -0
- package/dist/advisory/history.d.ts.map +1 -0
- package/dist/advisory/history.js +120 -0
- package/dist/advisory/history.js.map +1 -0
- package/dist/advisory/index.d.ts +1 -1
- package/dist/advisory/index.d.ts.map +1 -1
- package/dist/advisory/index.js.map +1 -1
- package/dist/agents/ReactiveAgent.d.ts.map +1 -1
- package/dist/agents/ReactiveAgent.js +3 -0
- package/dist/agents/ReactiveAgent.js.map +1 -1
- package/dist/agents/runAgent.d.ts +10 -0
- package/dist/agents/runAgent.d.ts.map +1 -1
- package/dist/agents/runAgent.js +3 -0
- package/dist/agents/runAgent.js.map +1 -1
- package/dist/compaction/manual.d.ts +6 -0
- package/dist/compaction/manual.d.ts.map +1 -1
- package/dist/compaction/manual.js +21 -2
- package/dist/compaction/manual.js.map +1 -1
- package/dist/compaction/summary.d.ts.map +1 -1
- package/dist/compaction/summary.js +4 -1
- package/dist/compaction/summary.js.map +1 -1
- package/dist/config/runtime.js +2 -2
- package/dist/config/runtime.js.map +1 -1
- package/dist/contracts/schemas.js +1 -1
- package/dist/contracts/schemas.js.map +1 -1
- package/dist/eval/harness-protection.d.ts +18 -0
- package/dist/eval/harness-protection.d.ts.map +1 -0
- package/dist/eval/harness-protection.js +58 -0
- package/dist/eval/harness-protection.js.map +1 -0
- package/dist/eval/harness-verification.d.ts +6 -1
- package/dist/eval/harness-verification.d.ts.map +1 -1
- package/dist/eval/harness-verification.js +19 -2
- package/dist/eval/harness-verification.js.map +1 -1
- package/dist/eval/index.d.ts +1 -0
- package/dist/eval/index.d.ts.map +1 -1
- package/dist/eval/index.js.map +1 -1
- package/dist/manager/resident/activity.d.ts +50 -0
- package/dist/manager/resident/activity.d.ts.map +1 -0
- package/dist/manager/resident/activity.js +125 -0
- package/dist/manager/resident/activity.js.map +1 -0
- package/dist/manager/resident/agenda.d.ts +137 -0
- package/dist/manager/resident/agenda.d.ts.map +1 -0
- package/dist/manager/resident/agenda.js +586 -0
- package/dist/manager/resident/agenda.js.map +1 -0
- package/dist/manager/resident/consumption.d.ts +104 -0
- package/dist/manager/resident/consumption.d.ts.map +1 -0
- package/dist/manager/resident/consumption.js +233 -0
- package/dist/manager/resident/consumption.js.map +1 -0
- package/dist/manager/resident/delivery-window.d.ts +22 -0
- package/dist/manager/resident/delivery-window.d.ts.map +1 -0
- package/dist/manager/resident/delivery-window.js +78 -0
- package/dist/manager/resident/delivery-window.js.map +1 -0
- package/dist/manager/resident/evidence-recall.d.ts +24 -0
- package/dist/manager/resident/evidence-recall.d.ts.map +1 -0
- package/dist/manager/resident/evidence-recall.js +295 -0
- package/dist/manager/resident/evidence-recall.js.map +1 -0
- package/dist/manager/resident/history-disk.d.ts +10 -0
- package/dist/manager/resident/history-disk.d.ts.map +1 -0
- package/dist/manager/resident/history-disk.js +50 -0
- package/dist/manager/resident/history-disk.js.map +1 -0
- package/dist/manager/resident/history.d.ts +79 -0
- package/dist/manager/resident/history.d.ts.map +1 -0
- package/dist/manager/resident/history.js +203 -0
- package/dist/manager/resident/history.js.map +1 -0
- package/dist/manager/resident/host.d.ts +78 -0
- package/dist/manager/resident/host.d.ts.map +1 -0
- package/dist/manager/resident/host.js +252 -0
- package/dist/manager/resident/host.js.map +1 -0
- package/dist/manager/resident/initiative.d.ts +111 -0
- package/dist/manager/resident/initiative.d.ts.map +1 -0
- package/dist/manager/resident/initiative.js +129 -0
- package/dist/manager/resident/initiative.js.map +1 -0
- package/dist/manager/resident/learning-cycle.d.ts +131 -0
- package/dist/manager/resident/learning-cycle.d.ts.map +1 -0
- package/dist/manager/resident/learning-cycle.js +306 -0
- package/dist/manager/resident/learning-cycle.js.map +1 -0
- package/dist/manager/resident/learning-observation.d.ts +80 -0
- package/dist/manager/resident/learning-observation.d.ts.map +1 -0
- package/dist/manager/resident/learning-observation.js +22 -0
- package/dist/manager/resident/learning-observation.js.map +1 -0
- package/dist/manager/resident/learning-store.d.ts +106 -0
- package/dist/manager/resident/learning-store.d.ts.map +1 -0
- package/dist/manager/resident/learning-store.js +598 -0
- package/dist/manager/resident/learning-store.js.map +1 -0
- package/dist/manager/resident/learning.d.ts +726 -0
- package/dist/manager/resident/learning.d.ts.map +1 -0
- package/dist/manager/resident/learning.js +353 -0
- package/dist/manager/resident/learning.js.map +1 -0
- package/dist/manager/resident/loop.d.ts +31 -0
- package/dist/manager/resident/loop.d.ts.map +1 -0
- package/dist/manager/resident/loop.js +69 -0
- package/dist/manager/resident/loop.js.map +1 -0
- package/dist/manager/resident/outbox.d.ts +180 -0
- package/dist/manager/resident/outbox.d.ts.map +1 -0
- package/dist/manager/resident/outbox.js +214 -0
- package/dist/manager/resident/outbox.js.map +1 -0
- package/dist/manager/resident/proposal.d.ts +91 -0
- package/dist/manager/resident/proposal.d.ts.map +1 -0
- package/dist/manager/resident/proposal.js +75 -0
- package/dist/manager/resident/proposal.js.map +1 -0
- package/dist/manager/resident/store.d.ts +182 -0
- package/dist/manager/resident/store.d.ts.map +1 -0
- package/dist/manager/resident/store.js +199 -0
- package/dist/manager/resident/store.js.map +1 -0
- package/dist/manager/resident/tool-evidence.d.ts +71 -0
- package/dist/manager/resident/tool-evidence.d.ts.map +1 -0
- package/dist/manager/resident/tool-evidence.js +285 -0
- package/dist/manager/resident/tool-evidence.js.map +1 -0
- package/dist/manager/run/persistence.d.ts.map +1 -1
- package/dist/manager/run/persistence.js +6 -0
- package/dist/manager/run/persistence.js.map +1 -1
- package/dist/plugin/loader.d.ts.map +1 -1
- package/dist/plugin/loader.js +5 -3
- package/dist/plugin/loader.js.map +1 -1
- package/dist/prompt/coding-agent-doctrine.d.ts +1 -1
- package/dist/prompt/coding-agent-doctrine.d.ts.map +1 -1
- package/dist/prompt/coding-agent-doctrine.js +2 -0
- package/dist/prompt/coding-agent-doctrine.js.map +1 -1
- package/dist/prompt/contributions.d.ts +5 -2
- package/dist/prompt/contributions.d.ts.map +1 -1
- package/dist/prompt/contributions.js.map +1 -1
- package/dist/prompt/index.d.ts +4 -0
- package/dist/prompt/index.d.ts.map +1 -1
- package/dist/prompt/index.js +2 -0
- package/dist/prompt/index.js.map +1 -1
- package/dist/prompt/resident-learning.d.ts +19 -0
- package/dist/prompt/resident-learning.d.ts.map +1 -0
- package/dist/prompt/resident-learning.js +125 -0
- package/dist/prompt/resident-learning.js.map +1 -0
- package/dist/prompt/resident-step.d.ts +36 -0
- package/dist/prompt/resident-step.d.ts.map +1 -0
- package/dist/prompt/resident-step.js +135 -0
- package/dist/prompt/resident-step.js.map +1 -0
- package/dist/provider/collect-chat-completion.d.ts +2 -1
- package/dist/provider/collect-chat-completion.d.ts.map +1 -1
- package/dist/provider/collect-chat-completion.js +7 -6
- package/dist/provider/collect-chat-completion.js.map +1 -1
- package/dist/provider/fallback.d.ts.map +1 -1
- package/dist/provider/fallback.js +2 -1
- package/dist/provider/fallback.js.map +1 -1
- package/dist/provider/stream-text.d.ts +16 -0
- package/dist/provider/stream-text.d.ts.map +1 -0
- package/dist/provider/stream-text.js +51 -0
- package/dist/provider/stream-text.js.map +1 -0
- package/dist/public-runtime.d.ts +22 -3
- package/dist/public-runtime.d.ts.map +1 -1
- package/dist/public-runtime.js +21 -3
- package/dist/public-runtime.js.map +1 -1
- package/dist/public-tools.d.ts +2 -0
- package/dist/public-tools.d.ts.map +1 -1
- package/dist/public-tools.js +2 -0
- package/dist/public-tools.js.map +1 -1
- package/dist/public-types.d.ts +29 -3
- package/dist/public-types.d.ts.map +1 -1
- package/dist/registry/index.d.ts +1 -1
- package/dist/registry/index.d.ts.map +1 -1
- package/dist/registry/tool/execute.d.ts +18 -0
- package/dist/registry/tool/execute.d.ts.map +1 -1
- package/dist/registry/tool/execute.js +43 -6
- package/dist/registry/tool/execute.js.map +1 -1
- package/dist/run/LimitChecker.js +3 -3
- package/dist/run/LimitChecker.js.map +1 -1
- package/dist/run/evidence-query.d.ts +41 -0
- package/dist/run/evidence-query.d.ts.map +1 -0
- package/dist/run/evidence-query.js +270 -0
- package/dist/run/evidence-query.js.map +1 -0
- package/dist/run/evidence-recall.d.ts +99 -0
- package/dist/run/evidence-recall.d.ts.map +1 -0
- package/dist/run/evidence-recall.js +633 -0
- package/dist/run/evidence-recall.js.map +1 -0
- package/dist/run/index.d.ts +2 -0
- package/dist/run/index.d.ts.map +1 -1
- package/dist/run/index.js +1 -0
- package/dist/run/index.js.map +1 -1
- package/dist/run/json-claim-verifier.d.ts +83 -0
- package/dist/run/json-claim-verifier.d.ts.map +1 -0
- package/dist/run/json-claim-verifier.js +200 -0
- package/dist/run/json-claim-verifier.js.map +1 -0
- package/dist/run/preparation-context-error.d.ts +10 -0
- package/dist/run/preparation-context-error.d.ts.map +1 -0
- package/dist/run/preparation-context-error.js +15 -0
- package/dist/run/preparation-context-error.js.map +1 -0
- package/dist/run-query/index.d.ts +3 -1
- package/dist/run-query/index.d.ts.map +1 -1
- package/dist/runtime/query/callback-inference.d.ts +8 -0
- package/dist/runtime/query/callback-inference.d.ts.map +1 -0
- package/dist/runtime/query/callback-inference.js +89 -0
- package/dist/runtime/query/callback-inference.js.map +1 -0
- package/dist/runtime/query/checkpoint.d.ts +4 -0
- package/dist/runtime/query/checkpoint.d.ts.map +1 -1
- package/dist/runtime/query/checkpoint.js +13 -0
- package/dist/runtime/query/checkpoint.js.map +1 -1
- package/dist/runtime/query/events.d.ts +7 -1
- package/dist/runtime/query/events.d.ts.map +1 -1
- package/dist/runtime/query/events.js +39 -12
- package/dist/runtime/query/events.js.map +1 -1
- package/dist/runtime/query/executor.d.ts +7 -1
- package/dist/runtime/query/executor.d.ts.map +1 -1
- package/dist/runtime/query/executor.js +38 -7
- package/dist/runtime/query/executor.js.map +1 -1
- package/dist/runtime/query/file-evidence-context.d.ts +5 -0
- package/dist/runtime/query/file-evidence-context.d.ts.map +1 -0
- package/dist/runtime/query/file-evidence-context.js +64 -0
- package/dist/runtime/query/file-evidence-context.js.map +1 -0
- package/dist/runtime/query/guard.d.ts.map +1 -1
- package/dist/runtime/query/guard.js +4 -0
- package/dist/runtime/query/guard.js.map +1 -1
- package/dist/runtime/query/index.d.ts +10 -1
- package/dist/runtime/query/index.d.ts.map +1 -1
- package/dist/runtime/query/index.js +40 -25
- package/dist/runtime/query/index.js.map +1 -1
- package/dist/runtime/query/iteration/index.d.ts +9 -31
- package/dist/runtime/query/iteration/index.d.ts.map +1 -1
- package/dist/runtime/query/iteration/index.js +231 -93
- package/dist/runtime/query/iteration/index.js.map +1 -1
- package/dist/runtime/query/iteration/phases/advisory.d.ts +2 -1
- package/dist/runtime/query/iteration/phases/advisory.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/advisory.js +2 -1
- package/dist/runtime/query/iteration/phases/advisory.js.map +1 -1
- package/dist/runtime/query/iteration/phases/context.d.ts +2 -1
- package/dist/runtime/query/iteration/phases/context.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/context.js.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.d.ts.map +1 -1
- package/dist/runtime/query/iteration/phases/tool-review.js +3 -0
- package/dist/runtime/query/iteration/phases/tool-review.js.map +1 -1
- package/dist/runtime/query/iteration/provider-rejected-image.d.ts +2 -1
- package/dist/runtime/query/iteration/provider-rejected-image.d.ts.map +1 -1
- package/dist/runtime/query/iteration/provider-rejected-image.js +5 -2
- package/dist/runtime/query/iteration/provider-rejected-image.js.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.d.ts +4 -1
- package/dist/runtime/query/iteration/stream-turn.d.ts.map +1 -1
- package/dist/runtime/query/iteration/stream-turn.js +22 -8
- package/dist/runtime/query/iteration/stream-turn.js.map +1 -1
- package/dist/runtime/query/prompt-cache.d.ts.map +1 -1
- package/dist/runtime/query/prompt-cache.js +21 -42
- package/dist/runtime/query/prompt-cache.js.map +1 -1
- package/dist/runtime/query/resume-pending.d.ts +18 -33
- package/dist/runtime/query/resume-pending.d.ts.map +1 -1
- package/dist/runtime/query/resume-pending.js +59 -42
- package/dist/runtime/query/resume-pending.js.map +1 -1
- package/dist/runtime/query/review-policy.d.ts +4 -4
- package/dist/runtime/query/review-policy.d.ts.map +1 -1
- package/dist/runtime/query/review-policy.js +10 -9
- package/dist/runtime/query/review-policy.js.map +1 -1
- package/dist/runtime/query/sandbox-lifecycle.d.ts.map +1 -1
- package/dist/runtime/query/sandbox-lifecycle.js +4 -0
- package/dist/runtime/query/sandbox-lifecycle.js.map +1 -1
- package/dist/runtime/query/tool-output-budget.d.ts +10 -6
- package/dist/runtime/query/tool-output-budget.d.ts.map +1 -1
- package/dist/runtime/query/tool-output-budget.js +54 -12
- package/dist/runtime/query/tool-output-budget.js.map +1 -1
- package/dist/runtime/query/tooling.d.ts +3 -1
- package/dist/runtime/query/tooling.d.ts.map +1 -1
- package/dist/runtime/query/tooling.js +4 -0
- package/dist/runtime/query/tooling.js.map +1 -1
- package/dist/scheduler/completion-inbox.d.ts +3 -0
- package/dist/scheduler/completion-inbox.d.ts.map +1 -1
- package/dist/scheduler/completion-inbox.js +28 -0
- package/dist/scheduler/completion-inbox.js.map +1 -1
- package/dist/store/evidence/compaction-archive.d.ts +109 -0
- package/dist/store/evidence/compaction-archive.d.ts.map +1 -0
- package/dist/store/evidence/compaction-archive.js +125 -0
- package/dist/store/evidence/compaction-archive.js.map +1 -0
- package/dist/store/evidence/compaction-provenance.d.ts +7 -0
- package/dist/store/evidence/compaction-provenance.d.ts.map +1 -0
- package/dist/store/evidence/compaction-provenance.js +49 -0
- package/dist/store/evidence/compaction-provenance.js.map +1 -0
- package/dist/store/evidence/compaction-text.d.ts +16 -0
- package/dist/store/evidence/compaction-text.d.ts.map +1 -0
- package/dist/store/evidence/compaction-text.js +51 -0
- package/dist/store/evidence/compaction-text.js.map +1 -0
- package/dist/store/evidence/disk.d.ts +11 -0
- package/dist/store/evidence/disk.d.ts.map +1 -0
- package/dist/store/evidence/disk.js +367 -0
- package/dist/store/evidence/disk.js.map +1 -0
- package/dist/store/evidence/format.d.ts +25 -0
- package/dist/store/evidence/format.d.ts.map +1 -0
- package/dist/store/evidence/format.js +115 -0
- package/dist/store/evidence/format.js.map +1 -0
- package/dist/store/evidence/index-page.d.ts +208 -0
- package/dist/store/evidence/index-page.d.ts.map +1 -0
- package/dist/store/evidence/index-page.js +262 -0
- package/dist/store/evidence/index-page.js.map +1 -0
- package/dist/store/evidence/io.d.ts +24 -0
- package/dist/store/evidence/io.d.ts.map +1 -0
- package/dist/store/evidence/io.js +69 -0
- package/dist/store/evidence/io.js.map +1 -0
- package/dist/store/evidence/linked.d.ts +9 -0
- package/dist/store/evidence/linked.d.ts.map +1 -0
- package/dist/store/evidence/linked.js +314 -0
- package/dist/store/evidence/linked.js.map +1 -0
- package/dist/store/evidence/passages.d.ts +15 -0
- package/dist/store/evidence/passages.d.ts.map +1 -0
- package/dist/store/evidence/passages.js +72 -0
- package/dist/store/evidence/passages.js.map +1 -0
- package/dist/store/evidence/record-chain.d.ts +42 -0
- package/dist/store/evidence/record-chain.d.ts.map +1 -0
- package/dist/store/evidence/record-chain.js +111 -0
- package/dist/store/evidence/record-chain.js.map +1 -0
- package/dist/store/evidence/search-input.d.ts +21 -0
- package/dist/store/evidence/search-input.d.ts.map +1 -0
- package/dist/store/evidence/search-input.js +44 -0
- package/dist/store/evidence/search-input.js.map +1 -0
- package/dist/store/evidence/selection.d.ts +9 -0
- package/dist/store/evidence/selection.d.ts.map +1 -0
- package/dist/store/evidence/selection.js +17 -0
- package/dist/store/evidence/selection.js.map +1 -0
- package/dist/store/evidence/source-kind.d.ts +11 -0
- package/dist/store/evidence/source-kind.d.ts.map +1 -0
- package/dist/store/evidence/source-kind.js +26 -0
- package/dist/store/evidence/source-kind.js.map +1 -0
- package/dist/store/evidence/source-text.d.ts +51 -0
- package/dist/store/evidence/source-text.d.ts.map +1 -0
- package/dist/store/evidence/source-text.js +213 -0
- package/dist/store/evidence/source-text.js.map +1 -0
- package/dist/store/evidence/types.d.ts +162 -0
- package/dist/store/evidence/types.d.ts.map +1 -0
- package/dist/store/evidence/types.js +2 -0
- package/dist/store/evidence/types.js.map +1 -0
- package/dist/store/memory/disk.d.ts +2 -0
- package/dist/store/memory/disk.d.ts.map +1 -1
- package/dist/store/memory/disk.js +2 -1
- package/dist/store/memory/disk.js.map +1 -1
- package/dist/store/run/disk.d.ts +10 -0
- package/dist/store/run/disk.d.ts.map +1 -1
- package/dist/store/run/disk.js +87 -7
- package/dist/store/run/disk.js.map +1 -1
- package/dist/store/run/memory.d.ts +1 -0
- package/dist/store/run/memory.d.ts.map +1 -1
- package/dist/store/run/memory.js +10 -0
- package/dist/store/run/memory.js.map +1 -1
- package/dist/store/run/tool-executions.d.ts +13 -0
- package/dist/store/run/tool-executions.d.ts.map +1 -0
- package/dist/store/run/tool-executions.js +99 -0
- package/dist/store/run/tool-executions.js.map +1 -0
- package/dist/store/session/index.d.ts +2 -0
- package/dist/store/session/index.d.ts.map +1 -1
- package/dist/store/session/index.js +1 -0
- package/dist/store/session/index.js.map +1 -1
- package/dist/store/session/sqlite.d.ts +57 -0
- package/dist/store/session/sqlite.d.ts.map +1 -0
- package/dist/store/session/sqlite.js +430 -0
- package/dist/store/session/sqlite.js.map +1 -0
- package/dist/tools/builtins/job.d.ts.map +1 -1
- package/dist/tools/builtins/job.js +4 -5
- package/dist/tools/builtins/job.js.map +1 -1
- package/dist/tools/builtins/write-file.js +2 -2
- package/dist/tools/builtins/write-file.js.map +1 -1
- package/dist/tools/defineTool.d.ts +2 -1
- package/dist/tools/defineTool.d.ts.map +1 -1
- package/dist/tools/defineTool.js +1 -1
- package/dist/tools/defineTool.js.map +1 -1
- package/dist/tools/file-read-tracker.d.ts.map +1 -1
- package/dist/tools/file-read-tracker.js +12 -3
- package/dist/tools/file-read-tracker.js.map +1 -1
- package/dist/tools/resident-history.d.ts +10 -0
- package/dist/tools/resident-history.d.ts.map +1 -0
- package/dist/tools/resident-history.js +80 -0
- package/dist/tools/resident-history.js.map +1 -0
- package/dist/tools/resident-tool-evidence.d.ts +5 -0
- package/dist/tools/resident-tool-evidence.d.ts.map +1 -0
- package/dist/tools/resident-tool-evidence.js +57 -0
- package/dist/tools/resident-tool-evidence.js.map +1 -0
- package/dist/types/advisory/config.d.ts +7 -0
- package/dist/types/advisory/config.d.ts.map +1 -1
- package/dist/types/agent/reactive.d.ts +1 -0
- package/dist/types/agent/reactive.d.ts.map +1 -1
- package/dist/types/authorization/index.d.ts +9 -9
- package/dist/types/authorization/index.d.ts.map +1 -1
- package/dist/types/authorization/index.js +1 -1
- package/dist/types/authorization/index.js.map +1 -1
- package/dist/types/hitl/index.d.ts +4 -0
- package/dist/types/hitl/index.d.ts.map +1 -1
- package/dist/types/hitl/index.js.map +1 -1
- package/dist/types/message/index.d.ts +16 -2
- package/dist/types/message/index.d.ts.map +1 -1
- package/dist/types/message/index.js +8 -1
- package/dist/types/message/index.js.map +1 -1
- package/dist/types/provider/chat.d.ts +2 -0
- package/dist/types/provider/chat.d.ts.map +1 -1
- package/dist/types/provider/stream.d.ts +4 -0
- package/dist/types/provider/stream.d.ts.map +1 -1
- package/dist/types/run/answer-review.d.ts +34 -3
- package/dist/types/run/answer-review.d.ts.map +1 -1
- package/dist/types/run/config.d.ts +3 -0
- package/dist/types/run/config.d.ts.map +1 -1
- package/dist/types/run/entity.d.ts +7 -0
- package/dist/types/run/entity.d.ts.map +1 -1
- package/dist/types/run/events.d.ts +31 -8
- package/dist/types/run/events.d.ts.map +1 -1
- package/dist/types/run/events.js.map +1 -1
- package/dist/types/run/prepare-step.d.ts +53 -8
- package/dist/types/run/prepare-step.d.ts.map +1 -1
- package/dist/types/run/store.d.ts +29 -4
- package/dist/types/run/store.d.ts.map +1 -1
- package/dist/types/run/store.js +0 -28
- package/dist/types/run/store.js.map +1 -1
- package/dist/types/tool/index.d.ts +15 -1
- package/dist/types/tool/index.d.ts.map +1 -1
- package/dist/types/tool/index.js.map +1 -1
- package/dist/utils/await-with-abort.d.ts +8 -0
- package/dist/utils/await-with-abort.d.ts.map +1 -0
- package/dist/utils/await-with-abort.js +28 -0
- package/dist/utils/await-with-abort.js.map +1 -0
- package/dist/utils/evidence-time.d.ts +3 -0
- package/dist/utils/evidence-time.d.ts.map +1 -0
- package/dist/utils/evidence-time.js +10 -0
- package/dist/utils/evidence-time.js.map +1 -0
- package/dist/utils/evidence-tokens.d.ts +13 -0
- package/dist/utils/evidence-tokens.d.ts.map +1 -0
- package/dist/utils/evidence-tokens.js +29 -0
- package/dist/utils/evidence-tokens.js.map +1 -0
- package/package.json +1 -1
- package/src/advisory/executor.ts +15 -30
- package/src/advisory/history.ts +126 -0
- package/src/advisory/index.ts +5 -1
- package/src/agents/ReactiveAgent.ts +3 -0
- package/src/agents/runAgent.ts +14 -1
- package/src/compaction/manual.ts +30 -2
- package/src/compaction/summary.ts +4 -1
- package/src/config/runtime.ts +2 -2
- package/src/contracts/schemas.ts +1 -1
- package/src/eval/harness-protection.ts +79 -0
- package/src/eval/harness-verification.ts +31 -1
- package/src/eval/index.ts +1 -0
- package/src/manager/resident/activity.ts +187 -0
- package/src/manager/resident/agenda.ts +907 -0
- package/src/manager/resident/consumption.ts +311 -0
- package/src/manager/resident/delivery-window.ts +93 -0
- package/src/manager/resident/evidence-recall.ts +363 -0
- package/src/manager/resident/history-disk.ts +63 -0
- package/src/manager/resident/history.ts +312 -0
- package/src/manager/resident/host.ts +356 -0
- package/src/manager/resident/initiative.ts +205 -0
- package/src/manager/resident/learning-cycle.ts +499 -0
- package/src/manager/resident/learning-observation.ts +39 -0
- package/src/manager/resident/learning-store.ts +813 -0
- package/src/manager/resident/learning.ts +493 -0
- package/src/manager/resident/loop.ts +101 -0
- package/src/manager/resident/outbox.ts +307 -0
- package/src/manager/resident/proposal.ts +105 -0
- package/src/manager/resident/store.ts +257 -0
- package/src/manager/resident/tool-evidence.ts +412 -0
- package/src/manager/run/persistence.ts +6 -0
- package/src/plugin/loader.ts +8 -3
- package/src/prompt/coding-agent-doctrine.ts +2 -0
- package/src/prompt/contributions.ts +5 -2
- package/src/prompt/index.ts +4 -0
- package/src/prompt/resident-learning.ts +143 -0
- package/src/prompt/resident-step.ts +176 -0
- package/src/provider/collect-chat-completion.ts +7 -6
- package/src/provider/fallback.ts +2 -1
- package/src/provider/stream-text.ts +56 -0
- package/src/public-runtime.ts +44 -0
- package/src/public-tools.ts +3 -0
- package/src/public-types.ts +165 -0
- package/src/registry/index.ts +1 -1
- package/src/registry/tool/execute.ts +56 -9
- package/src/run/LimitChecker.ts +3 -3
- package/src/run/evidence-query.ts +333 -0
- package/src/run/evidence-recall.ts +854 -0
- package/src/run/index.ts +11 -0
- package/src/run/json-claim-verifier.ts +298 -0
- package/src/run/preparation-context-error.ts +16 -0
- package/src/run-query/index.ts +1 -1
- package/src/runtime/query/callback-inference.ts +94 -0
- package/src/runtime/query/checkpoint.ts +14 -0
- package/src/runtime/query/events.ts +46 -14
- package/src/runtime/query/executor.ts +51 -9
- package/src/runtime/query/file-evidence-context.ts +65 -0
- package/src/runtime/query/guard.ts +2 -0
- package/src/runtime/query/index.ts +50 -27
- package/src/runtime/query/iteration/index.ts +269 -86
- package/src/runtime/query/iteration/phases/advisory.ts +3 -0
- package/src/runtime/query/iteration/phases/context.ts +2 -0
- package/src/runtime/query/iteration/phases/tool-review.ts +3 -0
- package/src/runtime/query/iteration/provider-rejected-image.ts +6 -1
- package/src/runtime/query/iteration/stream-turn.ts +35 -8
- package/src/runtime/query/prompt-cache.ts +24 -47
- package/src/runtime/query/resume-pending.ts +65 -40
- package/src/runtime/query/review-policy.ts +13 -9
- package/src/runtime/query/sandbox-lifecycle.ts +3 -0
- package/src/runtime/query/tool-output-budget.ts +65 -13
- package/src/runtime/query/tooling.ts +7 -1
- package/src/scheduler/completion-inbox.ts +26 -0
- package/src/store/evidence/compaction-archive.ts +139 -0
- package/src/store/evidence/compaction-provenance.ts +52 -0
- package/src/store/evidence/compaction-text.ts +61 -0
- package/src/store/evidence/disk.ts +461 -0
- package/src/store/evidence/format.ts +126 -0
- package/src/store/evidence/index-page.ts +292 -0
- package/src/store/evidence/io.ts +95 -0
- package/src/store/evidence/linked.ts +365 -0
- package/src/store/evidence/passages.ts +88 -0
- package/src/store/evidence/record-chain.ts +108 -0
- package/src/store/evidence/search-input.ts +62 -0
- package/src/store/evidence/selection.ts +25 -0
- package/src/store/evidence/source-kind.ts +36 -0
- package/src/store/evidence/source-text.ts +285 -0
- package/src/store/evidence/types.ts +177 -0
- package/src/store/memory/disk.ts +4 -1
- package/src/store/run/disk.ts +110 -8
- package/src/store/run/memory.ts +10 -0
- package/src/store/run/tool-executions.ts +112 -0
- package/src/store/session/index.ts +2 -0
- package/src/store/session/sqlite.ts +584 -0
- package/src/tools/builtins/job.ts +4 -5
- package/src/tools/builtins/write-file.ts +2 -2
- package/src/tools/defineTool.ts +4 -2
- package/src/tools/file-read-tracker.ts +8 -2
- package/src/tools/resident-history.ts +86 -0
- package/src/tools/resident-tool-evidence.ts +68 -0
- package/src/types/advisory/config.ts +7 -0
- package/src/types/agent/reactive.ts +1 -0
- package/src/types/authorization/index.ts +2 -2
- package/src/types/hitl/index.ts +4 -0
- package/src/types/message/index.ts +20 -0
- package/src/types/provider/chat.ts +2 -0
- package/src/types/provider/stream.ts +4 -0
- package/src/types/run/answer-review.ts +34 -3
- package/src/types/run/config.ts +3 -0
- package/src/types/run/entity.ts +7 -0
- package/src/types/run/events.ts +31 -8
- package/src/types/run/prepare-step.ts +59 -8
- package/src/types/run/store.ts +35 -4
- package/src/types/tool/index.ts +19 -1
- package/src/utils/await-with-abort.ts +26 -0
- package/src/utils/evidence-time.ts +9 -0
- package/src/utils/evidence-tokens.ts +32 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from 'node:crypto'
|
|
2
|
+
import { link, mkdir, rename, unlink, writeFile } from 'node:fs/promises'
|
|
3
|
+
import type { FileHandle } from 'node:fs/promises'
|
|
4
|
+
import { dirname, join } from 'node:path'
|
|
5
|
+
import { z } from 'zod'
|
|
6
|
+
import { compactionArchiveSchema } from './compaction-archive.js'
|
|
7
|
+
import type { CompactedToolMetadata } from './compaction-provenance.js'
|
|
8
|
+
import { compactedTexts } from './compaction-text.js'
|
|
9
|
+
import { digest, textFilter, tokenFilter } from './format.js'
|
|
10
|
+
import {
|
|
11
|
+
type EvidenceBudget,
|
|
12
|
+
EvidencePageLimit,
|
|
13
|
+
RECORD_BYTES,
|
|
14
|
+
decode,
|
|
15
|
+
noLinks,
|
|
16
|
+
readBytes,
|
|
17
|
+
readSmall,
|
|
18
|
+
} from './io.js'
|
|
19
|
+
|
|
20
|
+
const integer = z.number().int().nonnegative().safe()
|
|
21
|
+
export const entrySchema = z.object({
|
|
22
|
+
offset: integer,
|
|
23
|
+
length: integer.max(RECORD_BYTES),
|
|
24
|
+
sha256: z.string().regex(/^[a-f0-9]{64}$/),
|
|
25
|
+
seq: integer,
|
|
26
|
+
source: z.enum([
|
|
27
|
+
'tool_completed',
|
|
28
|
+
'message_completed',
|
|
29
|
+
'compaction_shed:system',
|
|
30
|
+
'compaction_shed:summary',
|
|
31
|
+
'compaction_shed:user',
|
|
32
|
+
'compaction_shed:assistant',
|
|
33
|
+
'compaction_shed:tool',
|
|
34
|
+
]),
|
|
35
|
+
part: integer.default(0),
|
|
36
|
+
toolName: z.string().max(1024).optional(),
|
|
37
|
+
toolUseId: z.string().max(1024).optional(),
|
|
38
|
+
isError: z.boolean().optional(),
|
|
39
|
+
truncated: z.boolean(),
|
|
40
|
+
spill: z
|
|
41
|
+
.string()
|
|
42
|
+
.regex(/^[a-f0-9]{64}$/)
|
|
43
|
+
.optional(),
|
|
44
|
+
filter: z.string().max(1400),
|
|
45
|
+
tokenFilter: z.string().max(1400).optional(),
|
|
46
|
+
})
|
|
47
|
+
export type IndexEntry = z.infer<typeof entrySchema>
|
|
48
|
+
export const positionSchema = z.object({
|
|
49
|
+
offset: integer,
|
|
50
|
+
seq: integer,
|
|
51
|
+
textIndex: integer.default(0),
|
|
52
|
+
})
|
|
53
|
+
export type IndexPosition = z.infer<typeof positionSchema>
|
|
54
|
+
const pageSchema = z.object({
|
|
55
|
+
start: positionSchema,
|
|
56
|
+
entries: z.array(entrySchema).max(64),
|
|
57
|
+
next: positionSchema.nullable(),
|
|
58
|
+
records: integer.max(64),
|
|
59
|
+
})
|
|
60
|
+
export type IndexPage = z.infer<typeof pageSchema>
|
|
61
|
+
|
|
62
|
+
export interface EvidenceSeal {
|
|
63
|
+
pack(value: unknown): string
|
|
64
|
+
unpack(value: string): unknown
|
|
65
|
+
dir: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export async function evidenceSeal(
|
|
69
|
+
root: string,
|
|
70
|
+
scopeKey: string,
|
|
71
|
+
sourceKey: string,
|
|
72
|
+
budget: EvidenceBudget,
|
|
73
|
+
): Promise<EvidenceSeal> {
|
|
74
|
+
try {
|
|
75
|
+
await noLinks(root)
|
|
76
|
+
} catch (error) {
|
|
77
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
|
|
78
|
+
await noLinks(dirname(root))
|
|
79
|
+
try {
|
|
80
|
+
await mkdir(root, { mode: 0o700 })
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error
|
|
83
|
+
}
|
|
84
|
+
await noLinks(root)
|
|
85
|
+
}
|
|
86
|
+
const dir = join(root, scopeKey)
|
|
87
|
+
await mkdir(dir, { mode: 0o700, recursive: true })
|
|
88
|
+
await noLinks(dir)
|
|
89
|
+
const keyPath = join(dir, 'key')
|
|
90
|
+
let key: Buffer
|
|
91
|
+
try {
|
|
92
|
+
key = await readSmall(keyPath, budget, 32)
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error
|
|
95
|
+
const candidate = `${keyPath}.${randomBytes(8).toString('hex')}.tmp`
|
|
96
|
+
await writeFile(candidate, randomBytes(32), { flag: 'wx', mode: 0o600 })
|
|
97
|
+
try {
|
|
98
|
+
try {
|
|
99
|
+
await link(candidate, keyPath)
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error
|
|
102
|
+
}
|
|
103
|
+
} finally {
|
|
104
|
+
await unlink(candidate)
|
|
105
|
+
}
|
|
106
|
+
key = await readSmall(keyPath, budget, 32)
|
|
107
|
+
}
|
|
108
|
+
if (key.length !== 32) throw new Error('Incomplete evidence index key.')
|
|
109
|
+
const mac = (value: string) => createHmac('sha256', key).update(`${sourceKey}\n${value}`).digest()
|
|
110
|
+
return {
|
|
111
|
+
dir,
|
|
112
|
+
pack(value) {
|
|
113
|
+
const body = Buffer.from(JSON.stringify(value)).toString('base64url')
|
|
114
|
+
return `${body}.${mac(body).toString('base64url')}`
|
|
115
|
+
},
|
|
116
|
+
unpack(value) {
|
|
117
|
+
const [body, signature, extra] = value.split('.')
|
|
118
|
+
if (!body || !signature || extra) throw new Error('Invalid evidence address.')
|
|
119
|
+
const actual = Buffer.from(signature, 'base64url')
|
|
120
|
+
if (actual.length !== 32 || !timingSafeEqual(actual, mac(body)))
|
|
121
|
+
throw new Error('Evidence address belongs to a different scope or changed source.')
|
|
122
|
+
return JSON.parse(decode(Buffer.from(body, 'base64url')))
|
|
123
|
+
},
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function toolEntry(
|
|
128
|
+
event: Record<string, unknown>,
|
|
129
|
+
offset: number,
|
|
130
|
+
bytes: Buffer,
|
|
131
|
+
): IndexEntry | null {
|
|
132
|
+
if (event.type !== 'tool_completed') return null
|
|
133
|
+
if (
|
|
134
|
+
typeof event.result !== 'string' ||
|
|
135
|
+
typeof event.isError !== 'boolean' ||
|
|
136
|
+
typeof event.toolName !== 'string' ||
|
|
137
|
+
typeof event.toolUseId !== 'string'
|
|
138
|
+
)
|
|
139
|
+
throw new Error('Malformed tool evidence.')
|
|
140
|
+
return entrySchema.parse({
|
|
141
|
+
offset,
|
|
142
|
+
length: bytes.length,
|
|
143
|
+
sha256: digest(bytes),
|
|
144
|
+
seq: event.seq,
|
|
145
|
+
source: 'tool_completed',
|
|
146
|
+
part: 0,
|
|
147
|
+
toolName: event.toolName,
|
|
148
|
+
toolUseId: event.toolUseId,
|
|
149
|
+
isError: event.isError,
|
|
150
|
+
truncated: event.outputTruncated === true,
|
|
151
|
+
...(typeof event.outputSpillIntegrity === 'string'
|
|
152
|
+
? { spill: event.outputSpillIntegrity }
|
|
153
|
+
: {}),
|
|
154
|
+
filter: textFilter(event.result),
|
|
155
|
+
tokenFilter: tokenFilter(event.result),
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Validate all textual parts before publishing any part of a record. */
|
|
160
|
+
export function eventTexts(
|
|
161
|
+
event: Record<string, unknown>,
|
|
162
|
+
): ({ source: string; text: string } & CompactedToolMetadata)[] {
|
|
163
|
+
if (event.type === 'compaction_archive') {
|
|
164
|
+
return compactionArchiveSchema.parse(event).archive.parts.map((part) => ({
|
|
165
|
+
source: `compaction_shed:${part.role === 'system' && part.summary ? 'summary' : part.role}`,
|
|
166
|
+
text: '',
|
|
167
|
+
...(part.role === 'tool' ? { toolName: part.toolName, isError: part.isError } : {}),
|
|
168
|
+
}))
|
|
169
|
+
}
|
|
170
|
+
if (event.type === 'tool_completed') {
|
|
171
|
+
if (typeof event.result !== 'string') throw new Error('Invalid tool text.')
|
|
172
|
+
return [{ source: 'tool_completed', text: event.result }]
|
|
173
|
+
}
|
|
174
|
+
if (event.type === 'message_completed') {
|
|
175
|
+
if (event.content === undefined) return []
|
|
176
|
+
if (typeof event.content !== 'string') throw new Error('Invalid message text.')
|
|
177
|
+
return [{ source: 'message_completed', text: event.content }]
|
|
178
|
+
}
|
|
179
|
+
if (event.type !== 'compaction_shed') return []
|
|
180
|
+
return compactedTexts(event.messages).map(({ role, summary, ...part }) => ({
|
|
181
|
+
source: `compaction_shed:${summary ? 'summary' : role}`,
|
|
182
|
+
...part,
|
|
183
|
+
}))
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Each cache page indexes at most 64 records, 64 textual parts and 4 MiB of input. */
|
|
187
|
+
export async function indexPage(
|
|
188
|
+
handle: FileHandle,
|
|
189
|
+
size: number,
|
|
190
|
+
runId: string,
|
|
191
|
+
start: IndexPosition,
|
|
192
|
+
seal: EvidenceSeal,
|
|
193
|
+
sourceKey: string,
|
|
194
|
+
budget: EvidenceBudget,
|
|
195
|
+
): Promise<{ page: IndexPage; cacheHit: boolean }> {
|
|
196
|
+
const path = join(
|
|
197
|
+
seal.dir,
|
|
198
|
+
`${digest(`${sourceKey}:${start.offset}:${start.seq}:${start.textIndex}:summary-provenance-v1:${process.version}:${process.versions.v8}:${process.versions.unicode}`)}.page`,
|
|
199
|
+
)
|
|
200
|
+
try {
|
|
201
|
+
const cached = await readSmall(path, budget, 512 * 1024)
|
|
202
|
+
const page = pageSchema.parse(seal.unpack(decode(cached)))
|
|
203
|
+
if (
|
|
204
|
+
page.start.offset !== start.offset ||
|
|
205
|
+
page.start.seq !== start.seq ||
|
|
206
|
+
page.start.textIndex !== start.textIndex
|
|
207
|
+
)
|
|
208
|
+
throw new Error('Index page position mismatch.')
|
|
209
|
+
return { page, cacheHit: true }
|
|
210
|
+
} catch (error) {
|
|
211
|
+
budget.signal?.throwIfAborted()
|
|
212
|
+
if (error instanceof EvidencePageLimit) throw error
|
|
213
|
+
// The cache is disposable. A missing, damaged or incompatible page is rebuilt.
|
|
214
|
+
}
|
|
215
|
+
let offset = start.offset
|
|
216
|
+
let seq = start.seq
|
|
217
|
+
let textIndex = start.textIndex
|
|
218
|
+
let records = 0
|
|
219
|
+
let buffered = Buffer.alloc(0)
|
|
220
|
+
let readOffset = offset
|
|
221
|
+
const entries: IndexEntry[] = []
|
|
222
|
+
while (offset < size && records < 64 && entries.length < 64) {
|
|
223
|
+
budget.signal?.throwIfAborted()
|
|
224
|
+
let newline = buffered.indexOf(10)
|
|
225
|
+
while (newline < 0) {
|
|
226
|
+
if (buffered.length >= RECORD_BYTES || readOffset >= size)
|
|
227
|
+
throw new Error('Oversized or torn transcript record; history is incomplete.')
|
|
228
|
+
const count = Math.min(65_536, size - readOffset, RECORD_BYTES - buffered.length)
|
|
229
|
+
if (readOffset - start.offset + count > RECORD_BYTES && records > 0) break
|
|
230
|
+
buffered = Buffer.concat([buffered, await readBytes(handle, readOffset, count, budget)])
|
|
231
|
+
readOffset += count
|
|
232
|
+
newline = buffered.indexOf(10)
|
|
233
|
+
}
|
|
234
|
+
if (newline < 0) break
|
|
235
|
+
const raw = buffered.subarray(0, newline + 1)
|
|
236
|
+
const event = JSON.parse(decode(raw)) as Record<string, unknown>
|
|
237
|
+
if (
|
|
238
|
+
event.runId !== runId ||
|
|
239
|
+
event.seq !== seq + 1 ||
|
|
240
|
+
typeof event.type !== 'string' ||
|
|
241
|
+
(offset === 0 && event.type !== 'run_started')
|
|
242
|
+
)
|
|
243
|
+
throw new Error('Transcript identity or event sequence is invalid.')
|
|
244
|
+
const parts = eventTexts(event)
|
|
245
|
+
const archive =
|
|
246
|
+
event.type === 'compaction_archive' ? compactionArchiveSchema.parse(event) : undefined
|
|
247
|
+
if (textIndex > parts.length) throw new Error('Invalid textual part position.')
|
|
248
|
+
const hash = digest(raw)
|
|
249
|
+
const tool = toolEntry(event, offset, raw)
|
|
250
|
+
records++
|
|
251
|
+
while (textIndex < parts.length && entries.length < 64) {
|
|
252
|
+
const part = parts[textIndex]
|
|
253
|
+
if (!part) throw new Error('Invalid textual part.')
|
|
254
|
+
const archivedPart = archive?.archive.parts[textIndex]
|
|
255
|
+
if (archive && !archivedPart) throw new Error('Missing archived text part.')
|
|
256
|
+
entries.push(
|
|
257
|
+
tool ??
|
|
258
|
+
entrySchema.parse({
|
|
259
|
+
offset,
|
|
260
|
+
length: raw.length,
|
|
261
|
+
sha256: hash,
|
|
262
|
+
seq: event.seq,
|
|
263
|
+
source: part.source,
|
|
264
|
+
toolName: part.toolName,
|
|
265
|
+
isError: part.isError,
|
|
266
|
+
part: textIndex,
|
|
267
|
+
truncated: false,
|
|
268
|
+
...(archivedPart ? { spill: archivedPart.manifest } : {}),
|
|
269
|
+
filter: textFilter(part.text),
|
|
270
|
+
tokenFilter: archivedPart ? undefined : tokenFilter(part.text),
|
|
271
|
+
}),
|
|
272
|
+
)
|
|
273
|
+
textIndex++
|
|
274
|
+
}
|
|
275
|
+
if (textIndex < parts.length) break
|
|
276
|
+
textIndex = 0
|
|
277
|
+
offset += raw.length
|
|
278
|
+
seq++
|
|
279
|
+
buffered = buffered.subarray(raw.length)
|
|
280
|
+
}
|
|
281
|
+
const page: IndexPage = {
|
|
282
|
+
start,
|
|
283
|
+
entries,
|
|
284
|
+
records,
|
|
285
|
+
next: offset < size ? { offset, seq, textIndex } : null,
|
|
286
|
+
}
|
|
287
|
+
const temp = `${path}.${randomBytes(8).toString('hex')}.tmp`
|
|
288
|
+
budget.signal?.throwIfAborted()
|
|
289
|
+
await writeFile(temp, seal.pack(page), { flag: 'wx', mode: 0o600 })
|
|
290
|
+
await rename(temp, path)
|
|
291
|
+
return { page, cacheHit: false }
|
|
292
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { constants } from 'node:fs'
|
|
2
|
+
import { lstat, open } from 'node:fs/promises'
|
|
3
|
+
import type { FileHandle } from 'node:fs/promises'
|
|
4
|
+
import { join, parse, resolve, sep } from 'node:path'
|
|
5
|
+
|
|
6
|
+
export const PAGE_BYTES = 8 * 1024 * 1024
|
|
7
|
+
export const RECORD_BYTES = 4 * 1024 * 1024
|
|
8
|
+
export class EvidencePageLimit extends Error {}
|
|
9
|
+
export interface EvidenceBudget {
|
|
10
|
+
bytes: number
|
|
11
|
+
limit?: number
|
|
12
|
+
signal?: AbortSignal
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function stamp(stat: {
|
|
16
|
+
dev: number
|
|
17
|
+
ino: number
|
|
18
|
+
size: number
|
|
19
|
+
mtimeMs: number
|
|
20
|
+
ctimeMs: number
|
|
21
|
+
}): string {
|
|
22
|
+
return [stat.dev, stat.ino, stat.size, stat.mtimeMs, stat.ctimeMs].join(':')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function noLinks(path: string): Promise<void> {
|
|
26
|
+
const absolute = resolve(path)
|
|
27
|
+
let current = parse(absolute).root
|
|
28
|
+
for (const part of absolute.slice(current.length).split(sep).filter(Boolean)) {
|
|
29
|
+
current = join(current, part)
|
|
30
|
+
if ((await lstat(current)).isSymbolicLink()) throw new Error('Evidence symlinks are refused.')
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export async function openEvidence(path: string): Promise<FileHandle> {
|
|
35
|
+
await noLinks(path)
|
|
36
|
+
const handle = await open(path, constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK)
|
|
37
|
+
if (!(await handle.stat()).isFile()) {
|
|
38
|
+
await handle.close()
|
|
39
|
+
throw new Error('Evidence must be a regular file.')
|
|
40
|
+
}
|
|
41
|
+
return handle
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function readBytes(
|
|
45
|
+
handle: FileHandle,
|
|
46
|
+
offset: number,
|
|
47
|
+
length: number,
|
|
48
|
+
budget: EvidenceBudget,
|
|
49
|
+
): Promise<Buffer> {
|
|
50
|
+
budget.signal?.throwIfAborted()
|
|
51
|
+
if (budget.bytes + length > (budget.limit ?? PAGE_BYTES)) throw new EvidencePageLimit()
|
|
52
|
+
const bytes = Buffer.alloc(length)
|
|
53
|
+
let received = 0
|
|
54
|
+
while (received < length) {
|
|
55
|
+
budget.signal?.throwIfAborted()
|
|
56
|
+
const { bytesRead } = await handle.read(
|
|
57
|
+
bytes,
|
|
58
|
+
received,
|
|
59
|
+
Math.min(65_536, length - received),
|
|
60
|
+
offset + received,
|
|
61
|
+
)
|
|
62
|
+
budget.bytes += bytesRead
|
|
63
|
+
if (!bytesRead) throw new Error('Evidence shortened while reading.')
|
|
64
|
+
received += bytesRead
|
|
65
|
+
}
|
|
66
|
+
return bytes
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function readSmall(
|
|
70
|
+
path: string,
|
|
71
|
+
budget: EvidenceBudget,
|
|
72
|
+
limit = RECORD_BYTES,
|
|
73
|
+
): Promise<Buffer> {
|
|
74
|
+
const handle = await openEvidence(path)
|
|
75
|
+
try {
|
|
76
|
+
const before = await handle.stat()
|
|
77
|
+
if (before.size > limit) throw new Error('Evidence record exceeds its read limit.')
|
|
78
|
+
const bytes = await readBytes(handle, 0, before.size, budget)
|
|
79
|
+
if (stamp(before) !== stamp(await handle.stat()))
|
|
80
|
+
throw new Error('Evidence changed during read.')
|
|
81
|
+
return bytes
|
|
82
|
+
} finally {
|
|
83
|
+
await handle.close()
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function decode(bytes: Uint8Array): string {
|
|
88
|
+
return new TextDecoder('utf8', { fatal: true, ignoreBOM: true }).decode(bytes)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function utf8Page(bytes: Buffer, limit: number): Buffer {
|
|
92
|
+
let end = Math.min(bytes.length, limit)
|
|
93
|
+
while (end > 0 && end < bytes.length && ((bytes[end] ?? 0) & 0xc0) === 0x80) end--
|
|
94
|
+
return bytes.subarray(0, end)
|
|
95
|
+
}
|