@m4ike1/ion-agent-core 0.1.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/README.md +622 -0
- package/dist/agent-loop.d.ts +24 -0
- package/dist/agent-loop.d.ts.map +1 -0
- package/dist/agent-loop.js +560 -0
- package/dist/agent-loop.js.map +1 -0
- package/dist/agent.d.ts +122 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +422 -0
- package/dist/agent.js.map +1 -0
- package/dist/harness/agent-harness.d.ts +716 -0
- package/dist/harness/agent-harness.d.ts.map +1 -0
- package/dist/harness/agent-harness.js +6 -0
- package/dist/harness/agent-harness.js.map +1 -0
- package/dist/harness/compaction/branch-summarization.d.ts +68 -0
- package/dist/harness/compaction/branch-summarization.d.ts.map +1 -0
- package/dist/harness/compaction/branch-summarization.js +176 -0
- package/dist/harness/compaction/branch-summarization.js.map +1 -0
- package/dist/harness/compaction/compaction.d.ts +126 -0
- package/dist/harness/compaction/compaction.d.ts.map +1 -0
- package/dist/harness/compaction/compaction.js +560 -0
- package/dist/harness/compaction/compaction.js.map +1 -0
- package/dist/harness/compaction/utils.d.ts +25 -0
- package/dist/harness/compaction/utils.d.ts.map +1 -0
- package/dist/harness/compaction/utils.js +120 -0
- package/dist/harness/compaction/utils.js.map +1 -0
- package/dist/harness/config.d.ts +9 -0
- package/dist/harness/config.d.ts.map +1 -0
- package/dist/harness/config.js +35 -0
- package/dist/harness/config.js.map +1 -0
- package/dist/harness/context.d.ts +9 -0
- package/dist/harness/context.d.ts.map +1 -0
- package/dist/harness/context.js +13 -0
- package/dist/harness/context.js.map +1 -0
- package/dist/harness/env/nodejs.d.ts +43 -0
- package/dist/harness/env/nodejs.d.ts.map +1 -0
- package/dist/harness/env/nodejs.js +865 -0
- package/dist/harness/env/nodejs.js.map +1 -0
- package/dist/harness/events.d.ts +25 -0
- package/dist/harness/events.d.ts.map +1 -0
- package/dist/harness/events.js +250 -0
- package/dist/harness/events.js.map +1 -0
- package/dist/harness/execution/assistant.d.ts +42 -0
- package/dist/harness/execution/assistant.d.ts.map +1 -0
- package/dist/harness/execution/assistant.js +82 -0
- package/dist/harness/execution/assistant.js.map +1 -0
- package/dist/harness/execution/effect-gate.d.ts +22 -0
- package/dist/harness/execution/effect-gate.d.ts.map +1 -0
- package/dist/harness/execution/effect-gate.js +49 -0
- package/dist/harness/execution/effect-gate.js.map +1 -0
- package/dist/harness/execution/tools.d.ts +67 -0
- package/dist/harness/execution/tools.d.ts.map +1 -0
- package/dist/harness/execution/tools.js +121 -0
- package/dist/harness/execution/tools.js.map +1 -0
- package/dist/harness/hooks.d.ts +38 -0
- package/dist/harness/hooks.d.ts.map +1 -0
- package/dist/harness/hooks.js +406 -0
- package/dist/harness/hooks.js.map +1 -0
- package/dist/harness/messages.d.ts +51 -0
- package/dist/harness/messages.d.ts.map +1 -0
- package/dist/harness/messages.js +102 -0
- package/dist/harness/messages.js.map +1 -0
- package/dist/harness/prompt-templates.d.ts +49 -0
- package/dist/harness/prompt-templates.d.ts.map +1 -0
- package/dist/harness/prompt-templates.js +225 -0
- package/dist/harness/prompt-templates.js.map +1 -0
- package/dist/harness/result.d.ts +133 -0
- package/dist/harness/result.d.ts.map +1 -0
- package/dist/harness/result.js +81 -0
- package/dist/harness/result.js.map +1 -0
- package/dist/harness/runtime/drive/boundary.d.ts +28 -0
- package/dist/harness/runtime/drive/boundary.d.ts.map +1 -0
- package/dist/harness/runtime/drive/boundary.js +173 -0
- package/dist/harness/runtime/drive/boundary.js.map +1 -0
- package/dist/harness/runtime/drive/checkpoint.d.ts +8 -0
- package/dist/harness/runtime/drive/checkpoint.d.ts.map +1 -0
- package/dist/harness/runtime/drive/checkpoint.js +133 -0
- package/dist/harness/runtime/drive/checkpoint.js.map +1 -0
- package/dist/harness/runtime/drive/deferred.d.ts +15 -0
- package/dist/harness/runtime/drive/deferred.d.ts.map +1 -0
- package/dist/harness/runtime/drive/deferred.js +179 -0
- package/dist/harness/runtime/drive/deferred.js.map +1 -0
- package/dist/harness/runtime/drive/generation.d.ts +8 -0
- package/dist/harness/runtime/drive/generation.d.ts.map +1 -0
- package/dist/harness/runtime/drive/generation.js +205 -0
- package/dist/harness/runtime/drive/generation.js.map +1 -0
- package/dist/harness/runtime/drive/reconcile.d.ts +5 -0
- package/dist/harness/runtime/drive/reconcile.d.ts.map +1 -0
- package/dist/harness/runtime/drive/reconcile.js +140 -0
- package/dist/harness/runtime/drive/reconcile.js.map +1 -0
- package/dist/harness/runtime/drive/recovery.d.ts +8 -0
- package/dist/harness/runtime/drive/recovery.d.ts.map +1 -0
- package/dist/harness/runtime/drive/recovery.js +85 -0
- package/dist/harness/runtime/drive/recovery.js.map +1 -0
- package/dist/harness/runtime/drive/response.d.ts +23 -0
- package/dist/harness/runtime/drive/response.d.ts.map +1 -0
- package/dist/harness/runtime/drive/response.js +394 -0
- package/dist/harness/runtime/drive/response.js.map +1 -0
- package/dist/harness/runtime/drive/retry.d.ts +4 -0
- package/dist/harness/runtime/drive/retry.d.ts.map +1 -0
- package/dist/harness/runtime/drive/retry.js +34 -0
- package/dist/harness/runtime/drive/retry.js.map +1 -0
- package/dist/harness/runtime/drive/structural.d.ts +32 -0
- package/dist/harness/runtime/drive/structural.d.ts.map +1 -0
- package/dist/harness/runtime/drive/structural.js +929 -0
- package/dist/harness/runtime/drive/structural.js.map +1 -0
- package/dist/harness/runtime/drive/terminal.d.ts +7 -0
- package/dist/harness/runtime/drive/terminal.d.ts.map +1 -0
- package/dist/harness/runtime/drive/terminal.js +49 -0
- package/dist/harness/runtime/drive/terminal.js.map +1 -0
- package/dist/harness/runtime/drive/tool-placement.d.ts +14 -0
- package/dist/harness/runtime/drive/tool-placement.d.ts.map +1 -0
- package/dist/harness/runtime/drive/tool-placement.js +244 -0
- package/dist/harness/runtime/drive/tool-placement.js.map +1 -0
- package/dist/harness/runtime/drive/tools.d.ts +6 -0
- package/dist/harness/runtime/drive/tools.d.ts.map +1 -0
- package/dist/harness/runtime/drive/tools.js +449 -0
- package/dist/harness/runtime/drive/tools.js.map +1 -0
- package/dist/harness/runtime/drive.d.ts +6 -0
- package/dist/harness/runtime/drive.d.ts.map +1 -0
- package/dist/harness/runtime/drive.js +93 -0
- package/dist/harness/runtime/drive.js.map +1 -0
- package/dist/harness/runtime/harness.d.ts +59 -0
- package/dist/harness/runtime/harness.d.ts.map +1 -0
- package/dist/harness/runtime/harness.js +311 -0
- package/dist/harness/runtime/harness.js.map +1 -0
- package/dist/harness/runtime/index.d.ts +2 -0
- package/dist/harness/runtime/index.d.ts.map +1 -0
- package/dist/harness/runtime/index.js +2 -0
- package/dist/harness/runtime/index.js.map +1 -0
- package/dist/harness/runtime/lane.d.ts +101 -0
- package/dist/harness/runtime/lane.d.ts.map +1 -0
- package/dist/harness/runtime/lane.js +1575 -0
- package/dist/harness/runtime/lane.js.map +1 -0
- package/dist/harness/runtime/progress.d.ts +15 -0
- package/dist/harness/runtime/progress.d.ts.map +1 -0
- package/dist/harness/runtime/progress.js +66 -0
- package/dist/harness/runtime/progress.js.map +1 -0
- package/dist/harness/runtime/reducer.d.ts +5 -0
- package/dist/harness/runtime/reducer.d.ts.map +1 -0
- package/dist/harness/runtime/reducer.js +240 -0
- package/dist/harness/runtime/reducer.js.map +1 -0
- package/dist/harness/runtime/restore.d.ts +24 -0
- package/dist/harness/runtime/restore.d.ts.map +1 -0
- package/dist/harness/runtime/restore.js +117 -0
- package/dist/harness/runtime/restore.js.map +1 -0
- package/dist/harness/runtime/transcript.d.ts +21 -0
- package/dist/harness/runtime/transcript.d.ts.map +1 -0
- package/dist/harness/runtime/transcript.js +72 -0
- package/dist/harness/runtime/transcript.js.map +1 -0
- package/dist/harness/runtime/types.d.ts +105 -0
- package/dist/harness/runtime/types.d.ts.map +1 -0
- package/dist/harness/runtime/types.js +61 -0
- package/dist/harness/runtime/types.js.map +1 -0
- package/dist/harness/session/commit.d.ts +53 -0
- package/dist/harness/session/commit.d.ts.map +1 -0
- package/dist/harness/session/commit.js +57 -0
- package/dist/harness/session/commit.js.map +1 -0
- package/dist/harness/session/context.d.ts +10 -0
- package/dist/harness/session/context.d.ts.map +1 -0
- package/dist/harness/session/context.js +49 -0
- package/dist/harness/session/context.js.map +1 -0
- package/dist/harness/session/fork-policy.d.ts +21 -0
- package/dist/harness/session/fork-policy.d.ts.map +1 -0
- package/dist/harness/session/fork-policy.js +55 -0
- package/dist/harness/session/fork-policy.js.map +1 -0
- package/dist/harness/session/fork.d.ts +16 -0
- package/dist/harness/session/fork.d.ts.map +1 -0
- package/dist/harness/session/fork.js +79 -0
- package/dist/harness/session/fork.js.map +1 -0
- package/dist/harness/session/in-memory-storage-state.d.ts +39 -0
- package/dist/harness/session/in-memory-storage-state.d.ts.map +1 -0
- package/dist/harness/session/in-memory-storage-state.js +276 -0
- package/dist/harness/session/in-memory-storage-state.js.map +1 -0
- package/dist/harness/session/index.d.ts +11 -0
- package/dist/harness/session/index.d.ts.map +1 -0
- package/dist/harness/session/index.js +9 -0
- package/dist/harness/session/index.js.map +1 -0
- package/dist/harness/session/jsonl/codec.d.ts +21 -0
- package/dist/harness/session/jsonl/codec.d.ts.map +1 -0
- package/dist/harness/session/jsonl/codec.js +45 -0
- package/dist/harness/session/jsonl/codec.js.map +1 -0
- package/dist/harness/session/jsonl/fork.d.ts +38 -0
- package/dist/harness/session/jsonl/fork.d.ts.map +1 -0
- package/dist/harness/session/jsonl/fork.js +264 -0
- package/dist/harness/session/jsonl/fork.js.map +1 -0
- package/dist/harness/session/jsonl/index.d.ts +4 -0
- package/dist/harness/session/jsonl/index.d.ts.map +1 -0
- package/dist/harness/session/jsonl/index.js +4 -0
- package/dist/harness/session/jsonl/index.js.map +1 -0
- package/dist/harness/session/jsonl/io.d.ts +14 -0
- package/dist/harness/session/jsonl/io.d.ts.map +1 -0
- package/dist/harness/session/jsonl/io.js +86 -0
- package/dist/harness/session/jsonl/io.js.map +1 -0
- package/dist/harness/session/jsonl/legacy-v3.d.ts +43 -0
- package/dist/harness/session/jsonl/legacy-v3.d.ts.map +1 -0
- package/dist/harness/session/jsonl/legacy-v3.js +462 -0
- package/dist/harness/session/jsonl/legacy-v3.js.map +1 -0
- package/dist/harness/session/jsonl/repo.d.ts +35 -0
- package/dist/harness/session/jsonl/repo.d.ts.map +1 -0
- package/dist/harness/session/jsonl/repo.js +301 -0
- package/dist/harness/session/jsonl/repo.js.map +1 -0
- package/dist/harness/session/jsonl/storage.d.ts +40 -0
- package/dist/harness/session/jsonl/storage.d.ts.map +1 -0
- package/dist/harness/session/jsonl/storage.js +200 -0
- package/dist/harness/session/jsonl/storage.js.map +1 -0
- package/dist/harness/session/jsonl/types.d.ts +39 -0
- package/dist/harness/session/jsonl/types.d.ts.map +1 -0
- package/dist/harness/session/jsonl/types.js +3 -0
- package/dist/harness/session/jsonl/types.js.map +1 -0
- package/dist/harness/session/memory.d.ts +48 -0
- package/dist/harness/session/memory.d.ts.map +1 -0
- package/dist/harness/session/memory.js +369 -0
- package/dist/harness/session/memory.js.map +1 -0
- package/dist/harness/session/mutation-line.d.ts +8 -0
- package/dist/harness/session/mutation-line.d.ts.map +1 -0
- package/dist/harness/session/mutation-line.js +21 -0
- package/dist/harness/session/mutation-line.js.map +1 -0
- package/dist/harness/session/session.d.ts +82 -0
- package/dist/harness/session/session.d.ts.map +1 -0
- package/dist/harness/session/session.js +361 -0
- package/dist/harness/session/session.js.map +1 -0
- package/dist/harness/session/testing/benchmark/datasets.d.ts +12 -0
- package/dist/harness/session/testing/benchmark/datasets.d.ts.map +1 -0
- package/dist/harness/session/testing/benchmark/datasets.js +24 -0
- package/dist/harness/session/testing/benchmark/datasets.js.map +1 -0
- package/dist/harness/session/testing/benchmark/session-repo.d.ts +42 -0
- package/dist/harness/session/testing/benchmark/session-repo.d.ts.map +1 -0
- package/dist/harness/session/testing/benchmark/session-repo.js +127 -0
- package/dist/harness/session/testing/benchmark/session-repo.js.map +1 -0
- package/dist/harness/session/testing/benchmark/storage.d.ts +25 -0
- package/dist/harness/session/testing/benchmark/storage.d.ts.map +1 -0
- package/dist/harness/session/testing/benchmark/storage.js +114 -0
- package/dist/harness/session/testing/benchmark/storage.js.map +1 -0
- package/dist/harness/session/testing/conformance/session-repo.d.ts +23 -0
- package/dist/harness/session/testing/conformance/session-repo.d.ts.map +1 -0
- package/dist/harness/session/testing/conformance/session-repo.js +809 -0
- package/dist/harness/session/testing/conformance/session-repo.js.map +1 -0
- package/dist/harness/session/testing/conformance/storage.d.ts +4 -0
- package/dist/harness/session/testing/conformance/storage.d.ts.map +1 -0
- package/dist/harness/session/testing/conformance/storage.js +600 -0
- package/dist/harness/session/testing/conformance/storage.js.map +1 -0
- package/dist/harness/session/testing/gating-storage.d.ts +26 -0
- package/dist/harness/session/testing/gating-storage.d.ts.map +1 -0
- package/dist/harness/session/testing/gating-storage.js +100 -0
- package/dist/harness/session/testing/gating-storage.js.map +1 -0
- package/dist/harness/session/testing/index.d.ts +10 -0
- package/dist/harness/session/testing/index.d.ts.map +1 -0
- package/dist/harness/session/testing/index.js +9 -0
- package/dist/harness/session/testing/index.js.map +1 -0
- package/dist/harness/session/testing/instrumented-storage.d.ts +11 -0
- package/dist/harness/session/testing/instrumented-storage.d.ts.map +1 -0
- package/dist/harness/session/testing/instrumented-storage.js +16 -0
- package/dist/harness/session/testing/instrumented-storage.js.map +1 -0
- package/dist/harness/session/testing/storage-decorator.d.ts +20 -0
- package/dist/harness/session/testing/storage-decorator.d.ts.map +1 -0
- package/dist/harness/session/testing/storage-decorator.js +41 -0
- package/dist/harness/session/testing/storage-decorator.js.map +1 -0
- package/dist/harness/session/testing/types.d.ts +12 -0
- package/dist/harness/session/testing/types.d.ts.map +1 -0
- package/dist/harness/session/testing/types.js +2 -0
- package/dist/harness/session/testing/types.js.map +1 -0
- package/dist/harness/session/types.d.ts +512 -0
- package/dist/harness/session/types.d.ts.map +1 -0
- package/dist/harness/session/types.js +9 -0
- package/dist/harness/session/types.js.map +1 -0
- package/dist/harness/session/values.d.ts +95 -0
- package/dist/harness/session/values.d.ts.map +1 -0
- package/dist/harness/session/values.js +81 -0
- package/dist/harness/session/values.js.map +1 -0
- package/dist/harness/skills.d.ts +46 -0
- package/dist/harness/skills.d.ts.map +1 -0
- package/dist/harness/skills.js +326 -0
- package/dist/harness/skills.js.map +1 -0
- package/dist/harness/system-prompt.d.ts +3 -0
- package/dist/harness/system-prompt.d.ts.map +1 -0
- package/dist/harness/system-prompt.js +30 -0
- package/dist/harness/system-prompt.js.map +1 -0
- package/dist/harness/telemetry.d.ts +1242 -0
- package/dist/harness/telemetry.d.ts.map +1 -0
- package/dist/harness/telemetry.js +527 -0
- package/dist/harness/telemetry.js.map +1 -0
- package/dist/harness/tools/bash.d.ts +27 -0
- package/dist/harness/tools/bash.d.ts.map +1 -0
- package/dist/harness/tools/bash.js +105 -0
- package/dist/harness/tools/bash.js.map +1 -0
- package/dist/harness/tools/edit-diff.d.ts +89 -0
- package/dist/harness/tools/edit-diff.d.ts.map +1 -0
- package/dist/harness/tools/edit-diff.js +386 -0
- package/dist/harness/tools/edit-diff.js.map +1 -0
- package/dist/harness/tools/edit.d.ts +19 -0
- package/dist/harness/tools/edit.d.ts.map +1 -0
- package/dist/harness/tools/edit.js +108 -0
- package/dist/harness/tools/edit.js.map +1 -0
- package/dist/harness/tools/file-mutation-queue.d.ts +5 -0
- package/dist/harness/tools/file-mutation-queue.d.ts.map +1 -0
- package/dist/harness/tools/file-mutation-queue.js +46 -0
- package/dist/harness/tools/file-mutation-queue.js.map +1 -0
- package/dist/harness/tools/image.d.ts +3 -0
- package/dist/harness/tools/image.d.ts.map +1 -0
- package/dist/harness/tools/image.js +106 -0
- package/dist/harness/tools/image.js.map +1 -0
- package/dist/harness/tools/index.d.ts +6 -0
- package/dist/harness/tools/index.d.ts.map +1 -0
- package/dist/harness/tools/index.js +5 -0
- package/dist/harness/tools/index.js.map +1 -0
- package/dist/harness/tools/path-utils.d.ts +5 -0
- package/dist/harness/tools/path-utils.d.ts.map +1 -0
- package/dist/harness/tools/path-utils.js +26 -0
- package/dist/harness/tools/path-utils.js.map +1 -0
- package/dist/harness/tools/read.d.ts +35 -0
- package/dist/harness/tools/read.d.ts.map +1 -0
- package/dist/harness/tools/read.js +108 -0
- package/dist/harness/tools/read.js.map +1 -0
- package/dist/harness/tools/tool-context.d.ts +6 -0
- package/dist/harness/tools/tool-context.d.ts.map +1 -0
- package/dist/harness/tools/tool-context.js +2 -0
- package/dist/harness/tools/tool-context.js.map +1 -0
- package/dist/harness/tools/write.d.ts +11 -0
- package/dist/harness/tools/write.d.ts.map +1 -0
- package/dist/harness/tools/write.js +31 -0
- package/dist/harness/tools/write.js.map +1 -0
- package/dist/harness/types.d.ts +305 -0
- package/dist/harness/types.d.ts.map +1 -0
- package/dist/harness/types.js +75 -0
- package/dist/harness/types.js.map +1 -0
- package/dist/harness/utils/adaptive-publisher.d.ts +24 -0
- package/dist/harness/utils/adaptive-publisher.d.ts.map +1 -0
- package/dist/harness/utils/adaptive-publisher.js +79 -0
- package/dist/harness/utils/adaptive-publisher.js.map +1 -0
- package/dist/harness/utils/output-capture.d.ts +31 -0
- package/dist/harness/utils/output-capture.d.ts.map +1 -0
- package/dist/harness/utils/output-capture.js +217 -0
- package/dist/harness/utils/output-capture.js.map +1 -0
- package/dist/harness/utils/shell-output.d.ts +29 -0
- package/dist/harness/utils/shell-output.d.ts.map +1 -0
- package/dist/harness/utils/shell-output.js +71 -0
- package/dist/harness/utils/shell-output.js.map +1 -0
- package/dist/harness/utils/truncate.d.ts +71 -0
- package/dist/harness/utils/truncate.d.ts.map +1 -0
- package/dist/harness/utils/truncate.js +296 -0
- package/dist/harness/utils/truncate.js.map +1 -0
- package/dist/harness/utils/usage.d.ts +4 -0
- package/dist/harness/utils/usage.d.ts.map +1 -0
- package/dist/harness/utils/usage.js +33 -0
- package/dist/harness/utils/usage.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/node.d.ts +3 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +3 -0
- package/dist/node.js.map +1 -0
- package/dist/proxy.d.ts +72 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +310 -0
- package/dist/proxy.js.map +1 -0
- package/dist/search/index.d.ts +29 -0
- package/dist/search/index.d.ts.map +1 -0
- package/dist/search/index.js +2 -0
- package/dist/search/index.js.map +1 -0
- package/dist/stream-fn.d.ts +10 -0
- package/dist/stream-fn.d.ts.map +1 -0
- package/dist/stream-fn.js +17 -0
- package/dist/stream-fn.js.map +1 -0
- package/dist/types.d.ts +416 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +89 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { projectForkCurrentStateWrite, selectBranchFork } from "./fork-policy.js";
|
|
2
|
+
import { branchTip, laneConfig, laneState, value } from "./values.js";
|
|
3
|
+
function storedValuesInNamespace(values, address) {
|
|
4
|
+
return values.filter((stored) => stored.address.namespace === address.namespace);
|
|
5
|
+
}
|
|
6
|
+
function findStoredValue(values, address) {
|
|
7
|
+
return values.find((stored) => stored.address.namespace === address.namespace && stored.address.key === address.key);
|
|
8
|
+
}
|
|
9
|
+
/** Build the complete logical state for a forked destination session. */
|
|
10
|
+
export function createForkSnapshot(source, options) {
|
|
11
|
+
const sourceEntries = new Map(source.entries.map((entry) => [entry.id, entry]));
|
|
12
|
+
const sourceTips = storedValuesInNamespace(source.scalarValues, branchTip(""));
|
|
13
|
+
validateForkSourceSnapshot(source, sourceEntries, sourceTips, options);
|
|
14
|
+
const { entryIds, plan } = selectForkContents(sourceEntries, sourceTips, options);
|
|
15
|
+
const entries = new Map();
|
|
16
|
+
for (const id of entryIds)
|
|
17
|
+
entries.set(id, sourceEntries.get(id));
|
|
18
|
+
const scalarValues = [];
|
|
19
|
+
let nextSeq = Math.max(0, ...[...entries.values()].map((entry) => entry.seq)) + 1;
|
|
20
|
+
for (const stored of source.scalarValues) {
|
|
21
|
+
const projected = projectForkCurrentStateWrite({
|
|
22
|
+
kind: "value",
|
|
23
|
+
op: "set",
|
|
24
|
+
seq: stored.seq,
|
|
25
|
+
namespace: stored.address.namespace,
|
|
26
|
+
key: stored.address.key,
|
|
27
|
+
value: stored.value,
|
|
28
|
+
}, plan, (entryId) => entryIds.has(entryId));
|
|
29
|
+
if (projected !== undefined) {
|
|
30
|
+
scalarValues.push({
|
|
31
|
+
address: value(projected.namespace, projected.key),
|
|
32
|
+
value: projected.value,
|
|
33
|
+
seq: nextSeq++,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return { entries, scalarValues, nextSeq };
|
|
38
|
+
}
|
|
39
|
+
function selectForkContents(sourceEntries, sourceTips, options) {
|
|
40
|
+
const entryIds = new Set();
|
|
41
|
+
if (options.scope === "tree") {
|
|
42
|
+
for (const id of sourceEntries.keys())
|
|
43
|
+
entryIds.add(id);
|
|
44
|
+
return { entryIds, plan: { scope: "tree" } };
|
|
45
|
+
}
|
|
46
|
+
const sourceTip = sourceTips.find((stored) => stored.address.key === options.branch);
|
|
47
|
+
const plan = selectBranchFork(options, {
|
|
48
|
+
tip: sourceTip?.value,
|
|
49
|
+
getParent: (entryId) => sourceEntries.get(entryId)?.parentId,
|
|
50
|
+
selectEntry: (entryId) => entryIds.add(entryId),
|
|
51
|
+
});
|
|
52
|
+
return { entryIds, plan };
|
|
53
|
+
}
|
|
54
|
+
function validateForkSourceSnapshot(source, sourceEntries, sourceTips, options) {
|
|
55
|
+
const sourceTipKeys = new Set(sourceTips.map((stored) => stored.address.key));
|
|
56
|
+
for (const stored of source.scalarValues) {
|
|
57
|
+
if ((stored.address.namespace === laneConfig("").namespace ||
|
|
58
|
+
stored.address.namespace === laneState("").namespace) &&
|
|
59
|
+
!sourceTipKeys.has(stored.address.key)) {
|
|
60
|
+
throw new Error(`Source session branch ${JSON.stringify(stored.address.key)} is missing branch.tip`);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
for (const tip of sourceTips) {
|
|
64
|
+
const configuration = findStoredValue(source.scalarValues, laneConfig(tip.address.key));
|
|
65
|
+
const state = findStoredValue(source.scalarValues, laneState(tip.address.key));
|
|
66
|
+
if ((configuration === undefined) !== (state === undefined)) {
|
|
67
|
+
throw new Error(`Source session branch ${JSON.stringify(tip.address.key)} has incomplete lane state`);
|
|
68
|
+
}
|
|
69
|
+
if (options.scope === "branch" && tip.address.key === options.branch && configuration === undefined) {
|
|
70
|
+
throw new Error(`Source branch ${JSON.stringify(options.branch)} is not a configured AgentLane`);
|
|
71
|
+
}
|
|
72
|
+
if ((source.entriesComplete !== false || options.scope === "tree") &&
|
|
73
|
+
tip.value !== null &&
|
|
74
|
+
!sourceEntries.has(tip.value)) {
|
|
75
|
+
throw new Error(`Source session branch ${JSON.stringify(tip.address.key)} has an unknown tip`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=fork.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fork.js","sourceRoot":"","sources":["../../../src/harness/session/fork.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,4BAA4B,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAE7G,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAgC,KAAK,EAAE,MAAM,aAAa,CAAC;AAepG,SAAS,uBAAuB,CAAI,MAAuC,EAAE,OAAiB,EAAoB;IACjH,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,SAAS,KAAK,OAAO,CAAC,SAAS,CAAqB,CAAC;AAAA,CACrG;AAED,SAAS,eAAe,CAAI,MAAuC,EAAE,OAAiB,EAA8B;IACnH,OAAO,MAAM,CAAC,IAAI,CACjB,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,OAAO,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AAK,CAAC,KAAK,KAAK,SAAS,CAAC,EAAE,CAAC;YAC7D,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,4BAA4B,CAAC,CAAC;QACvG,CAAC;QACD,IAAI,OAAO,CAAC,KAAK,KAAK,QAAQ,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,KAAK,OAAO,CAAC,MAAM,IAAI,aAAa,KAAK,SAAS,EAAE,CAAC;YACrG,MAAM,IAAI,KAAK,CAAC,iBAAiB,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,gCAAgC,CAAC,CAAC;QAClG,CAAC;QACD,IACC,CAAC,MAAM,CAAC,eAAe,KAAK,KAAK,IAAI,OAAO,CAAC,KAAK,KAAK,MAAM,CAAC;YAC9D,GAAG,CAAC,KAAK,KAAK,IAAI;YAClB,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,EAC5B,CAAC;YACF,MAAM,IAAI,KAAK,CAAC,yBAAyB,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,qBAAqB,CAAC,CAAC;QAChG,CAAC;IACF,CAAC;AAAA,CACD","sourcesContent":["import { type ForkCurrentStatePlan, projectForkCurrentStateWrite, selectBranchFork } from \"./fork-policy.ts\";\nimport type { Entry, ForkOptions } from \"./types.ts\";\nimport { branchTip, laneConfig, laneState, type StoredValue, type Value, value } from \"./values.ts\";\n\nexport interface ForkSourceSnapshot {\n\tentries: Entry[];\n\tscalarValues: StoredValue<unknown>[];\n\t/** False when a backend supplied only the requested branch rather than the full tree. */\n\tentriesComplete?: boolean;\n}\n\nexport interface ForkDestinationSnapshot {\n\tentries: Map<string, Entry>;\n\tscalarValues: StoredValue<unknown>[];\n\tnextSeq: number;\n}\n\nfunction storedValuesInNamespace<T>(values: readonly StoredValue<unknown>[], address: Value<T>): StoredValue<T>[] {\n\treturn values.filter((stored) => stored.address.namespace === address.namespace) as StoredValue<T>[];\n}\n\nfunction findStoredValue<T>(values: readonly StoredValue<unknown>[], address: Value<T>): StoredValue<T> | undefined {\n\treturn values.find(\n\t\t(stored) => stored.address.namespace === address.namespace && stored.address.key === address.key,\n\t) as StoredValue<T> | undefined;\n}\n\n/** Build the complete logical state for a forked destination session. */\nexport function createForkSnapshot(source: ForkSourceSnapshot, options: ForkOptions): ForkDestinationSnapshot {\n\tconst sourceEntries = new Map(source.entries.map((entry) => [entry.id, entry]));\n\tconst sourceTips = storedValuesInNamespace(source.scalarValues, branchTip(\"\"));\n\tvalidateForkSourceSnapshot(source, sourceEntries, sourceTips, options);\n\n\tconst { entryIds, plan } = selectForkContents(sourceEntries, sourceTips, options);\n\tconst entries = new Map<string, Entry>();\n\tfor (const id of entryIds) entries.set(id, sourceEntries.get(id)!);\n\n\tconst scalarValues: StoredValue<unknown>[] = [];\n\tlet nextSeq = Math.max(0, ...[...entries.values()].map((entry) => entry.seq)) + 1;\n\tfor (const stored of source.scalarValues) {\n\t\tconst projected = projectForkCurrentStateWrite(\n\t\t\t{\n\t\t\t\tkind: \"value\",\n\t\t\t\top: \"set\",\n\t\t\t\tseq: stored.seq,\n\t\t\t\tnamespace: stored.address.namespace,\n\t\t\t\tkey: stored.address.key,\n\t\t\t\tvalue: stored.value,\n\t\t\t},\n\t\t\tplan,\n\t\t\t(entryId) => entryIds.has(entryId),\n\t\t);\n\t\tif (projected !== undefined) {\n\t\t\tscalarValues.push({\n\t\t\t\taddress: value<unknown>(projected.namespace, projected.key),\n\t\t\t\tvalue: projected.value,\n\t\t\t\tseq: nextSeq++,\n\t\t\t});\n\t\t}\n\t}\n\n\treturn { entries, scalarValues, nextSeq };\n}\n\nfunction selectForkContents(\n\tsourceEntries: Map<string, Entry>,\n\tsourceTips: StoredValue<string | null>[],\n\toptions: ForkOptions,\n): { entryIds: Set<string>; plan: ForkCurrentStatePlan } {\n\tconst entryIds = new Set<string>();\n\tif (options.scope === \"tree\") {\n\t\tfor (const id of sourceEntries.keys()) entryIds.add(id);\n\t\treturn { entryIds, plan: { scope: \"tree\" } };\n\t}\n\n\tconst sourceTip = sourceTips.find((stored) => stored.address.key === options.branch);\n\tconst plan = selectBranchFork(options, {\n\t\ttip: sourceTip?.value,\n\t\tgetParent: (entryId) => sourceEntries.get(entryId)?.parentId,\n\t\tselectEntry: (entryId) => entryIds.add(entryId),\n\t});\n\treturn { entryIds, plan };\n}\n\nfunction validateForkSourceSnapshot(\n\tsource: ForkSourceSnapshot,\n\tsourceEntries: Map<string, Entry>,\n\tsourceTips: StoredValue<string | null>[],\n\toptions: ForkOptions,\n): void {\n\tconst sourceTipKeys = new Set(sourceTips.map((stored) => stored.address.key));\n\n\tfor (const stored of source.scalarValues) {\n\t\tif (\n\t\t\t(stored.address.namespace === laneConfig(\"\").namespace ||\n\t\t\t\tstored.address.namespace === laneState(\"\").namespace) &&\n\t\t\t!sourceTipKeys.has(stored.address.key)\n\t\t) {\n\t\t\tthrow new Error(`Source session branch ${JSON.stringify(stored.address.key)} is missing branch.tip`);\n\t\t}\n\t}\n\tfor (const tip of sourceTips) {\n\t\tconst configuration = findStoredValue(source.scalarValues, laneConfig(tip.address.key));\n\t\tconst state = findStoredValue(source.scalarValues, laneState(tip.address.key));\n\t\tif ((configuration === undefined) !== (state === undefined)) {\n\t\t\tthrow new Error(`Source session branch ${JSON.stringify(tip.address.key)} has incomplete lane state`);\n\t\t}\n\t\tif (options.scope === \"branch\" && tip.address.key === options.branch && configuration === undefined) {\n\t\t\tthrow new Error(`Source branch ${JSON.stringify(options.branch)} is not a configured AgentLane`);\n\t\t}\n\t\tif (\n\t\t\t(source.entriesComplete !== false || options.scope === \"tree\") &&\n\t\t\ttip.value !== null &&\n\t\t\t!sourceEntries.has(tip.value)\n\t\t) {\n\t\t\tthrow new Error(`Source session branch ${JSON.stringify(tip.address.key)} has an unknown tip`);\n\t\t}\n\t}\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type CommittedWrite, type PreparedCommit } from "./commit.ts";
|
|
2
|
+
export type { CommittedEntryWrite, CommittedListAppendWrite, CommittedListDeleteWrite, CommittedUsageWrite, CommittedValueDeleteWrite, CommittedValueSetWrite, CommittedWrite, PreparedCommit, } from "./commit.ts";
|
|
3
|
+
import type { Entry, EntryScan, EntryStructure, ForkOptions, SessionStats, StorageBranchScan, UsageRow, UsageScan, Write } from "./types.ts";
|
|
4
|
+
import { type ListElement, type ListReadOptions, type StoredValue, type Value, type ValueList } from "./values.ts";
|
|
5
|
+
/**
|
|
6
|
+
* Complete materialized session state for MemoryStorage and JsonlStorage.
|
|
7
|
+
*
|
|
8
|
+
* This is intentionally unsuitable for database backends and long-running sessions that may not fit in memory.
|
|
9
|
+
* Those backends should query indexed durable state and update durable aggregates within each commit transaction.
|
|
10
|
+
*/
|
|
11
|
+
export declare class InMemoryStorageState {
|
|
12
|
+
private readonly entries;
|
|
13
|
+
private readonly entriesBySeq;
|
|
14
|
+
private readonly scalarValues;
|
|
15
|
+
private readonly listValues;
|
|
16
|
+
private readonly usage;
|
|
17
|
+
private stats;
|
|
18
|
+
private nextSeq;
|
|
19
|
+
constructor();
|
|
20
|
+
prepareCommit(writes: Write[], timestamp: number): PreparedCommit;
|
|
21
|
+
validateCommitted(writes: readonly CommittedWrite[]): void;
|
|
22
|
+
/** Apply writes already accepted by validateCommitted() and return the post-apply totals. */
|
|
23
|
+
applyValidated(writes: readonly CommittedWrite[]): SessionStats;
|
|
24
|
+
createFork(options: ForkOptions): InMemoryStorageState;
|
|
25
|
+
private selectForkPlan;
|
|
26
|
+
private applyValueSetOrListAppend;
|
|
27
|
+
advanceNextSeq(nextSeq: number): void;
|
|
28
|
+
getEntries(ids: readonly string[]): Map<string, Entry>;
|
|
29
|
+
getValue<T>(address: Value<T>): StoredValue<T> | undefined;
|
|
30
|
+
scanValues<T>(prefix: Value<T>): StoredValue<T>[];
|
|
31
|
+
readList<T>(address: ValueList<T>, options?: ListReadOptions): ListElement<T>[];
|
|
32
|
+
scanBranch(query: StorageBranchScan): Entry[];
|
|
33
|
+
scanBranchStructure(query: StorageBranchScan): EntryStructure[];
|
|
34
|
+
scanEntries(query: EntryScan): Entry[];
|
|
35
|
+
scanUsage(query: UsageScan): UsageRow[];
|
|
36
|
+
getStats(): SessionStats;
|
|
37
|
+
getNextSeq(): number;
|
|
38
|
+
}
|
|
39
|
+
//# sourceMappingURL=in-memory-storage-state.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory-storage-state.d.ts","sourceRoot":"","sources":["../../../src/harness/session/in-memory-storage-state.ts"],"names":[],"mappings":"AACA,OAAO,EAGN,KAAK,cAAc,EACnB,KAAK,cAAc,EAGnB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACX,mBAAmB,EACnB,wBAAwB,EACxB,wBAAwB,EACxB,mBAAmB,EACnB,yBAAyB,EACzB,sBAAsB,EACtB,cAAc,EACd,cAAc,GACd,MAAM,aAAa,CAAC;AAErB,OAAO,KAAK,EACX,KAAK,EACL,SAAS,EACT,cAAc,EACd,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,QAAQ,EACR,SAAS,EACT,KAAK,EACL,MAAM,YAAY,CAAC;AACpB,OAAO,EAEN,KAAK,WAAW,EAChB,KAAK,eAAe,EAKpB,KAAK,WAAW,EAChB,KAAK,KAAK,EACV,KAAK,SAAS,EAEd,MAAM,aAAa,CAAC;AA0BrB;;;;;GAKG;AACH,qBAAa,oBAAoB;IAChC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAU;IACvC,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAoC;IACjE,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAkC;IAC7D,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAwB;IAC9C,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,OAAO,CAAS;IAEx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\nimport {\n\ttype CommittedListAppendWrite,\n\ttype CommittedValueSetWrite,\n\ttype CommittedWrite,\n\ttype PreparedCommit,\n\tprepareStorageCommit,\n\tvalidateCommittedWrites,\n} from \"./commit.ts\";\nimport { projectForkCurrentStateWrite, selectBranchFork } from \"./fork-policy.ts\";\n\nexport type {\n\tCommittedEntryWrite,\n\tCommittedListAppendWrite,\n\tCommittedListDeleteWrite,\n\tCommittedUsageWrite,\n\tCommittedValueDeleteWrite,\n\tCommittedValueSetWrite,\n\tCommittedWrite,\n\tPreparedCommit,\n} from \"./commit.ts\";\n\nimport type {\n\tEntry,\n\tEntryScan,\n\tEntryStructure,\n\tForkOptions,\n\tSessionStats,\n\tStorageBranchScan,\n\tUsageRow,\n\tUsageScan,\n\tWrite,\n} from \"./types.ts\";\nimport {\n\tbranchTip,\n\ttype ListElement,\n\ttype ListReadOptions,\n\tlaneConfig,\n\tlaneState,\n\tlist,\n\tresolveListReadOptions,\n\ttype StoredValue,\n\ttype Value,\n\ttype ValueList,\n\tvalue,\n} from \"./values.ts\";\n\ninterface StoredListSnapshot {\n\taddress: ValueList<unknown>;\n\telements: ListElement<unknown>[];\n}\n\ntype MemoryForkPlan =\n\t| { scope: \"tree\" }\n\t| { scope: \"branch\"; branch: string; destinationTip: string | null; entryIds: Set<string> };\n\nfunction physicalKey(namespace: string, key: string): string {\n\treturn `${namespace}\\u0000${key}`;\n}\n\nfunction compareKeys(left: string, right: string): number {\n\tconst leftCodePoints = Array.from(left, (character) => character.codePointAt(0)!);\n\tconst rightCodePoints = Array.from(right, (character) => character.codePointAt(0)!);\n\tconst length = Math.min(leftCodePoints.length, rightCodePoints.length);\n\tfor (let index = 0; index < length; index++) {\n\t\tconst difference = leftCodePoints[index]! - rightCodePoints[index]!;\n\t\tif (difference !== 0) return difference;\n\t}\n\treturn leftCodePoints.length - rightCodePoints.length;\n}\n\n/**\n * Complete materialized session state for MemoryStorage and JsonlStorage.\n *\n * This is intentionally unsuitable for database backends and long-running sessions that may not fit in memory.\n * Those backends should query indexed durable state and update durable aggregates within each commit transaction.\n */\nexport class InMemoryStorageState {\n\tprivate readonly entries: Map<string, Entry>;\n\tprivate readonly entriesBySeq: Entry[];\n\tprivate readonly scalarValues: Map<string, StoredValue<unknown>>;\n\tprivate readonly listValues: Map<string, StoredListSnapshot>;\n\tprivate readonly usage: Map<string, UsageRow>;\n\tprivate stats: SessionStats;\n\tprivate nextSeq: number;\n\n\tconstructor() {\n\t\tthis.entries = new Map();\n\t\tthis.entriesBySeq = [];\n\t\tthis.scalarValues = new Map();\n\t\tthis.listValues = new Map();\n\t\tthis.usage = new Map();\n\t\tthis.stats = { messageCount: 0, usage: emptyUsage() };\n\t\tthis.nextSeq = 1;\n\t}\n\n\tprepareCommit(writes: Write[], timestamp: number): PreparedCommit {\n\t\tconst prepared = prepareStorageCommit(writes, this.nextSeq, timestamp);\n\t\tthis.validateCommitted(prepared.writes);\n\t\treturn prepared;\n\t}\n\n\tvalidateCommitted(writes: readonly CommittedWrite[]): void {\n\t\tvalidateCommittedWrites(writes, this.nextSeq, {\n\t\t\thasEntryOrUsageId: (id) => this.entries.has(id) || this.usage.has(id),\n\t\t\thasEntryId: (id) => this.entries.has(id),\n\t\t});\n\t}\n\n\t/** Apply writes already accepted by validateCommitted() and return the post-apply totals. */\n\tapplyValidated(writes: readonly CommittedWrite[]): SessionStats {\n\t\tfor (const write of writes) {\n\t\t\tswitch (write.kind) {\n\t\t\t\tcase \"entry\": {\n\t\t\t\t\tconst { kind: _kind, ...entry } = write;\n\t\t\t\t\tthis.entries.set(entry.id, entry);\n\t\t\t\t\tthis.entriesBySeq.push(entry);\n\t\t\t\t\tif (entry.type === \"message\") this.stats = { ...this.stats, messageCount: this.stats.messageCount + 1 };\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"usage\": {\n\t\t\t\t\tconst { kind: _kind, ...row } = write;\n\t\t\t\t\tthis.usage.set(row.id, row);\n\t\t\t\t\tthis.stats = { ...this.stats, usage: addUsage(this.stats.usage, row.usage) };\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"value\":\n\t\t\t\t\tif (write.op === \"delete\") this.scalarValues.delete(physicalKey(write.namespace, write.key));\n\t\t\t\t\telse this.applyValueSetOrListAppend(write);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"list\":\n\t\t\t\t\tif (write.op === \"delete\") this.listValues.delete(physicalKey(write.namespace, write.key));\n\t\t\t\t\telse this.applyValueSetOrListAppend(write);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tthis.nextSeq = write.seq + 1;\n\t\t}\n\t\treturn this.stats;\n\t}\n\n\tcreateFork(options: ForkOptions): InMemoryStorageState {\n\t\tconst plan = this.selectForkPlan(options);\n\n\t\tconst isEntryCopied = (entryId: string): boolean => {\n\t\t\tif (plan.scope === \"tree\") return true;\n\t\t\treturn plan.entryIds.has(entryId);\n\t\t};\n\t\tconst destination = new InMemoryStorageState();\n\t\tlet messageCount = 0;\n\t\tfor (const entry of this.entriesBySeq) {\n\t\t\tif (!isEntryCopied(entry.id)) continue;\n\t\t\tdestination.entries.set(entry.id, entry);\n\t\t\tdestination.entriesBySeq.push(entry);\n\t\t\tif (entry.type === \"message\") messageCount++;\n\t\t}\n\t\tdestination.stats = { ...destination.stats, messageCount };\n\n\t\tfor (const stored of this.scalarValues.values()) {\n\t\t\tconst projected = projectForkCurrentStateWrite(\n\t\t\t\t{\n\t\t\t\t\tkind: \"value\",\n\t\t\t\t\top: \"set\",\n\t\t\t\t\tseq: stored.seq,\n\t\t\t\t\tnamespace: stored.address.namespace,\n\t\t\t\t\tkey: stored.address.key,\n\t\t\t\t\tvalue: stored.value,\n\t\t\t\t},\n\t\t\t\tplan,\n\t\t\t\tisEntryCopied,\n\t\t\t);\n\t\t\tif (projected !== undefined) destination.applyValueSetOrListAppend(projected);\n\t\t}\n\n\t\tfor (const stored of this.listValues.values()) {\n\t\t\tfor (const element of stored.elements) {\n\t\t\t\tconst projected = projectForkCurrentStateWrite(\n\t\t\t\t\t{\n\t\t\t\t\t\tkind: \"list\",\n\t\t\t\t\t\top: \"append\",\n\t\t\t\t\t\tseq: element.seq,\n\t\t\t\t\t\tnamespace: stored.address.namespace,\n\t\t\t\t\t\tkey: stored.address.key,\n\t\t\t\t\t\tvalue: element.value,\n\t\t\t\t\t},\n\t\t\t\t\tplan,\n\t\t\t\t\tisEntryCopied,\n\t\t\t\t);\n\t\t\t\tif (projected !== undefined) destination.applyValueSetOrListAppend(projected);\n\t\t\t}\n\t\t}\n\t\tdestination.nextSeq = this.nextSeq;\n\t\treturn destination;\n\t}\n\n\tprivate selectForkPlan(options: ForkOptions): MemoryForkPlan {\n\t\tif (options.scope === \"tree\") return { scope: \"tree\" };\n\n\t\tconst entryIds = new Set<string>();\n\t\tconst plan = selectBranchFork(options, {\n\t\t\ttip: this.getValue(branchTip(options.branch))?.value,\n\t\t\tgetParent: (entryId) => this.entries.get(entryId)?.parentId,\n\t\t\tselectEntry: (entryId) => entryIds.add(entryId),\n\t\t});\n\t\tif (\n\t\t\tthis.getValue(laneConfig(options.branch)) === undefined ||\n\t\t\tthis.getValue(laneState(options.branch)) === undefined\n\t\t) {\n\t\t\tthrow new Error(`Source branch ${JSON.stringify(options.branch)} is not a configured AgentLane`);\n\t\t}\n\t\treturn { ...plan, entryIds };\n\t}\n\n\tprivate applyValueSetOrListAppend(write: CommittedValueSetWrite | CommittedListAppendWrite): void {\n\t\tconst key = physicalKey(write.namespace, write.key);\n\t\tif (write.kind === \"value\") {\n\t\t\tthis.scalarValues.set(key, {\n\t\t\t\taddress: value<unknown>(write.namespace, write.key),\n\t\t\t\tvalue: write.value,\n\t\t\t\tseq: write.seq,\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\n\t\tconst element = { seq: write.seq, value: write.value };\n\t\tconst stored = this.listValues.get(key);\n\t\tif (stored === undefined) {\n\t\t\tthis.listValues.set(key, {\n\t\t\t\taddress: list<unknown>(write.namespace, write.key),\n\t\t\t\telements: [element],\n\t\t\t});\n\t\t} else {\n\t\t\tstored.elements.push(element);\n\t\t}\n\t}\n\n\tadvanceNextSeq(nextSeq: number): void {\n\t\tif (!Number.isSafeInteger(nextSeq) || nextSeq < 1) {\n\t\t\tthrow new Error(`Invalid storage sequence high-water mark: ${nextSeq}`);\n\t\t}\n\t\tthis.nextSeq = Math.max(this.nextSeq, nextSeq);\n\t}\n\n\tgetEntries(ids: readonly string[]): Map<string, Entry> {\n\t\tconst found = new Map<string, Entry>();\n\t\tfor (const id of ids) {\n\t\t\tconst entry = this.entries.get(id);\n\t\t\tif (entry !== undefined) found.set(id, entry);\n\t\t}\n\t\treturn found;\n\t}\n\n\tgetValue<T>(address: Value<T>): StoredValue<T> | undefined {\n\t\treturn this.scalarValues.get(physicalKey(address.namespace, address.key)) as StoredValue<T> | undefined;\n\t}\n\n\tscanValues<T>(prefix: Value<T>): StoredValue<T>[] {\n\t\treturn [...this.scalarValues.values()]\n\t\t\t.filter((stored) => stored.address.namespace === prefix.namespace && stored.address.key.startsWith(prefix.key))\n\t\t\t.sort((left, right) => compareKeys(left.address.key, right.address.key)) as StoredValue<T>[];\n\t}\n\n\treadList<T>(address: ValueList<T>, options?: ListReadOptions): ListElement<T>[] {\n\t\tconst resolved = resolveListReadOptions(options);\n\t\tconst elements = this.listValues.get(physicalKey(address.namespace, address.key))?.elements ?? [];\n\t\tconst filtered = elements.filter((element) => {\n\t\t\tif (resolved.cursor === undefined) return true;\n\t\t\treturn resolved.order === \"asc\" ? element.seq > resolved.cursor.seq : element.seq < resolved.cursor.seq;\n\t\t});\n\t\tconst ordered = resolved.order === \"asc\" ? filtered : [...filtered].reverse();\n\t\treturn ordered.slice(0, resolved.limit) as ListElement<T>[];\n\t}\n\n\tscanBranch(query: StorageBranchScan): Entry[] {\n\t\tconst start = this.entries.get(query.start);\n\t\tif (start === undefined) throw new Error(`Unknown branch start: ${query.start}`);\n\n\t\tconst path: Entry[] = [];\n\t\tlet entry: Entry | undefined = start;\n\t\twhile (entry !== undefined) {\n\t\t\tpath.push(entry);\n\t\t\tif (entry.parentId === null) break;\n\t\t\tentry = this.entries.get(entry.parentId);\n\t\t\tif (entry === undefined) throw new Error(\"Corrupt branch: missing parent\");\n\t\t}\n\t\tif (query.order === \"oldestFirst\") path.reverse();\n\n\t\tconst stopped: Entry[] = [];\n\t\tfor (const candidate of path) {\n\t\t\tstopped.push(candidate);\n\t\t\tif (candidate.id === query.stopAtId || candidate.type === query.stopAtType) break;\n\t\t}\n\t\tconst filtered = stopped\n\t\t\t.filter((candidate) => query.type === undefined || candidate.type === query.type)\n\t\t\t.filter((candidate) => query.customType === undefined || candidate.customType === query.customType)\n\t\t\t.filter(\n\t\t\t\t(candidate) =>\n\t\t\t\t\tquery.cursor === undefined ||\n\t\t\t\t\t(query.order === \"oldestFirst\" ? candidate.seq > query.cursor.seq : candidate.seq < query.cursor.seq),\n\t\t\t);\n\t\treturn query.limit === undefined ? filtered : filtered.slice(0, Math.max(0, query.limit));\n\t}\n\n\tscanBranchStructure(query: StorageBranchScan): EntryStructure[] {\n\t\treturn this.scanBranch(query).map((entry) => ({\n\t\t\tid: entry.id,\n\t\t\tparentId: entry.parentId,\n\t\t\tseq: entry.seq,\n\t\t\ttimestamp: entry.timestamp,\n\t\t\ttype: entry.type,\n\t\t\t...(entry.customType === undefined ? {} : { customType: entry.customType }),\n\t\t}));\n\t}\n\n\tscanEntries(query: EntryScan): Entry[] {\n\t\tconst limit = query.limit === undefined ? Number.POSITIVE_INFINITY : Math.max(0, Math.trunc(query.limit));\n\t\tconst entries: Entry[] = [];\n\t\tconst descending = query.order === \"desc\";\n\t\tlet index = descending ? this.entriesBySeq.length - 1 : 0;\n\t\twhile (index >= 0 && index < this.entriesBySeq.length && entries.length < limit) {\n\t\t\tconst entry = this.entriesBySeq[index]!;\n\t\t\tif (\n\t\t\t\t(query.type === undefined || entry.type === query.type) &&\n\t\t\t\t(query.customType === undefined || entry.customType === query.customType) &&\n\t\t\t\t(query.fromSeq === undefined || entry.seq >= query.fromSeq) &&\n\t\t\t\t(query.toSeq === undefined || entry.seq <= query.toSeq)\n\t\t\t) {\n\t\t\t\tentries.push(entry);\n\t\t\t}\n\t\t\tindex += descending ? -1 : 1;\n\t\t}\n\t\treturn entries;\n\t}\n\n\tscanUsage(query: UsageScan): UsageRow[] {\n\t\tconst rows = [...this.usage.values()]\n\t\t\t.filter((row) => query.fromSeq === undefined || row.seq >= query.fromSeq)\n\t\t\t.filter((row) => query.toSeq === undefined || row.seq <= query.toSeq)\n\t\t\t.sort((left, right) => (query.order === \"desc\" ? right.seq - left.seq : left.seq - right.seq));\n\t\treturn query.limit === undefined ? rows : rows.slice(0, Math.max(0, query.limit));\n\t}\n\n\tgetStats(): SessionStats {\n\t\treturn this.stats;\n\t}\n\n\tgetNextSeq(): number {\n\t\treturn this.nextSeq;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { addUsage, emptyUsage } from "../utils/usage.js";
|
|
2
|
+
import { prepareStorageCommit, validateCommittedWrites, } from "./commit.js";
|
|
3
|
+
import { projectForkCurrentStateWrite, selectBranchFork } from "./fork-policy.js";
|
|
4
|
+
import { branchTip, laneConfig, laneState, list, resolveListReadOptions, value, } from "./values.js";
|
|
5
|
+
function physicalKey(namespace, key) {
|
|
6
|
+
return `${namespace}\u0000${key}`;
|
|
7
|
+
}
|
|
8
|
+
function compareKeys(left, right) {
|
|
9
|
+
const leftCodePoints = Array.from(left, (character) => character.codePointAt(0));
|
|
10
|
+
const rightCodePoints = Array.from(right, (character) => character.codePointAt(0));
|
|
11
|
+
const length = Math.min(leftCodePoints.length, rightCodePoints.length);
|
|
12
|
+
for (let index = 0; index < length; index++) {
|
|
13
|
+
const difference = leftCodePoints[index] - rightCodePoints[index];
|
|
14
|
+
if (difference !== 0)
|
|
15
|
+
return difference;
|
|
16
|
+
}
|
|
17
|
+
return leftCodePoints.length - rightCodePoints.length;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Complete materialized session state for MemoryStorage and JsonlStorage.
|
|
21
|
+
*
|
|
22
|
+
* This is intentionally unsuitable for database backends and long-running sessions that may not fit in memory.
|
|
23
|
+
* Those backends should query indexed durable state and update durable aggregates within each commit transaction.
|
|
24
|
+
*/
|
|
25
|
+
export class InMemoryStorageState {
|
|
26
|
+
entries;
|
|
27
|
+
entriesBySeq;
|
|
28
|
+
scalarValues;
|
|
29
|
+
listValues;
|
|
30
|
+
usage;
|
|
31
|
+
stats;
|
|
32
|
+
nextSeq;
|
|
33
|
+
constructor() {
|
|
34
|
+
this.entries = new Map();
|
|
35
|
+
this.entriesBySeq = [];
|
|
36
|
+
this.scalarValues = new Map();
|
|
37
|
+
this.listValues = new Map();
|
|
38
|
+
this.usage = new Map();
|
|
39
|
+
this.stats = { messageCount: 0, usage: emptyUsage() };
|
|
40
|
+
this.nextSeq = 1;
|
|
41
|
+
}
|
|
42
|
+
prepareCommit(writes, timestamp) {
|
|
43
|
+
const prepared = prepareStorageCommit(writes, this.nextSeq, timestamp);
|
|
44
|
+
this.validateCommitted(prepared.writes);
|
|
45
|
+
return prepared;
|
|
46
|
+
}
|
|
47
|
+
validateCommitted(writes) {
|
|
48
|
+
validateCommittedWrites(writes, this.nextSeq, {
|
|
49
|
+
hasEntryOrUsageId: (id) => this.entries.has(id) || this.usage.has(id),
|
|
50
|
+
hasEntryId: (id) => this.entries.has(id),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/** Apply writes already accepted by validateCommitted() and return the post-apply totals. */
|
|
54
|
+
applyValidated(writes) {
|
|
55
|
+
for (const write of writes) {
|
|
56
|
+
switch (write.kind) {
|
|
57
|
+
case "entry": {
|
|
58
|
+
const { kind: _kind, ...entry } = write;
|
|
59
|
+
this.entries.set(entry.id, entry);
|
|
60
|
+
this.entriesBySeq.push(entry);
|
|
61
|
+
if (entry.type === "message")
|
|
62
|
+
this.stats = { ...this.stats, messageCount: this.stats.messageCount + 1 };
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case "usage": {
|
|
66
|
+
const { kind: _kind, ...row } = write;
|
|
67
|
+
this.usage.set(row.id, row);
|
|
68
|
+
this.stats = { ...this.stats, usage: addUsage(this.stats.usage, row.usage) };
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
case "value":
|
|
72
|
+
if (write.op === "delete")
|
|
73
|
+
this.scalarValues.delete(physicalKey(write.namespace, write.key));
|
|
74
|
+
else
|
|
75
|
+
this.applyValueSetOrListAppend(write);
|
|
76
|
+
break;
|
|
77
|
+
case "list":
|
|
78
|
+
if (write.op === "delete")
|
|
79
|
+
this.listValues.delete(physicalKey(write.namespace, write.key));
|
|
80
|
+
else
|
|
81
|
+
this.applyValueSetOrListAppend(write);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
this.nextSeq = write.seq + 1;
|
|
85
|
+
}
|
|
86
|
+
return this.stats;
|
|
87
|
+
}
|
|
88
|
+
createFork(options) {
|
|
89
|
+
const plan = this.selectForkPlan(options);
|
|
90
|
+
const isEntryCopied = (entryId) => {
|
|
91
|
+
if (plan.scope === "tree")
|
|
92
|
+
return true;
|
|
93
|
+
return plan.entryIds.has(entryId);
|
|
94
|
+
};
|
|
95
|
+
const destination = new InMemoryStorageState();
|
|
96
|
+
let messageCount = 0;
|
|
97
|
+
for (const entry of this.entriesBySeq) {
|
|
98
|
+
if (!isEntryCopied(entry.id))
|
|
99
|
+
continue;
|
|
100
|
+
destination.entries.set(entry.id, entry);
|
|
101
|
+
destination.entriesBySeq.push(entry);
|
|
102
|
+
if (entry.type === "message")
|
|
103
|
+
messageCount++;
|
|
104
|
+
}
|
|
105
|
+
destination.stats = { ...destination.stats, messageCount };
|
|
106
|
+
for (const stored of this.scalarValues.values()) {
|
|
107
|
+
const projected = projectForkCurrentStateWrite({
|
|
108
|
+
kind: "value",
|
|
109
|
+
op: "set",
|
|
110
|
+
seq: stored.seq,
|
|
111
|
+
namespace: stored.address.namespace,
|
|
112
|
+
key: stored.address.key,
|
|
113
|
+
value: stored.value,
|
|
114
|
+
}, plan, isEntryCopied);
|
|
115
|
+
if (projected !== undefined)
|
|
116
|
+
destination.applyValueSetOrListAppend(projected);
|
|
117
|
+
}
|
|
118
|
+
for (const stored of this.listValues.values()) {
|
|
119
|
+
for (const element of stored.elements) {
|
|
120
|
+
const projected = projectForkCurrentStateWrite({
|
|
121
|
+
kind: "list",
|
|
122
|
+
op: "append",
|
|
123
|
+
seq: element.seq,
|
|
124
|
+
namespace: stored.address.namespace,
|
|
125
|
+
key: stored.address.key,
|
|
126
|
+
value: element.value,
|
|
127
|
+
}, plan, isEntryCopied);
|
|
128
|
+
if (projected !== undefined)
|
|
129
|
+
destination.applyValueSetOrListAppend(projected);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
destination.nextSeq = this.nextSeq;
|
|
133
|
+
return destination;
|
|
134
|
+
}
|
|
135
|
+
selectForkPlan(options) {
|
|
136
|
+
if (options.scope === "tree")
|
|
137
|
+
return { scope: "tree" };
|
|
138
|
+
const entryIds = new Set();
|
|
139
|
+
const plan = selectBranchFork(options, {
|
|
140
|
+
tip: this.getValue(branchTip(options.branch))?.value,
|
|
141
|
+
getParent: (entryId) => this.entries.get(entryId)?.parentId,
|
|
142
|
+
selectEntry: (entryId) => entryIds.add(entryId),
|
|
143
|
+
});
|
|
144
|
+
if (this.getValue(laneConfig(options.branch)) === undefined ||
|
|
145
|
+
this.getValue(laneState(options.branch)) === undefined) {
|
|
146
|
+
throw new Error(`Source branch ${JSON.stringify(options.branch)} is not a configured AgentLane`);
|
|
147
|
+
}
|
|
148
|
+
return { ...plan, entryIds };
|
|
149
|
+
}
|
|
150
|
+
applyValueSetOrListAppend(write) {
|
|
151
|
+
const key = physicalKey(write.namespace, write.key);
|
|
152
|
+
if (write.kind === "value") {
|
|
153
|
+
this.scalarValues.set(key, {
|
|
154
|
+
address: value(write.namespace, write.key),
|
|
155
|
+
value: write.value,
|
|
156
|
+
seq: write.seq,
|
|
157
|
+
});
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const element = { seq: write.seq, value: write.value };
|
|
161
|
+
const stored = this.listValues.get(key);
|
|
162
|
+
if (stored === undefined) {
|
|
163
|
+
this.listValues.set(key, {
|
|
164
|
+
address: list(write.namespace, write.key),
|
|
165
|
+
elements: [element],
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
stored.elements.push(element);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
advanceNextSeq(nextSeq) {
|
|
173
|
+
if (!Number.isSafeInteger(nextSeq) || nextSeq < 1) {
|
|
174
|
+
throw new Error(`Invalid storage sequence high-water mark: ${nextSeq}`);
|
|
175
|
+
}
|
|
176
|
+
this.nextSeq = Math.max(this.nextSeq, nextSeq);
|
|
177
|
+
}
|
|
178
|
+
getEntries(ids) {
|
|
179
|
+
const found = new Map();
|
|
180
|
+
for (const id of ids) {
|
|
181
|
+
const entry = this.entries.get(id);
|
|
182
|
+
if (entry !== undefined)
|
|
183
|
+
found.set(id, entry);
|
|
184
|
+
}
|
|
185
|
+
return found;
|
|
186
|
+
}
|
|
187
|
+
getValue(address) {
|
|
188
|
+
return this.scalarValues.get(physicalKey(address.namespace, address.key));
|
|
189
|
+
}
|
|
190
|
+
scanValues(prefix) {
|
|
191
|
+
return [...this.scalarValues.values()]
|
|
192
|
+
.filter((stored) => stored.address.namespace === prefix.namespace && stored.address.key.startsWith(prefix.key))
|
|
193
|
+
.sort((left, right) => compareKeys(left.address.key, right.address.key));
|
|
194
|
+
}
|
|
195
|
+
readList(address, options) {
|
|
196
|
+
const resolved = resolveListReadOptions(options);
|
|
197
|
+
const elements = this.listValues.get(physicalKey(address.namespace, address.key))?.elements ?? [];
|
|
198
|
+
const filtered = elements.filter((element) => {
|
|
199
|
+
if (resolved.cursor === undefined)
|
|
200
|
+
return true;
|
|
201
|
+
return resolved.order === "asc" ? element.seq > resolved.cursor.seq : element.seq < resolved.cursor.seq;
|
|
202
|
+
});
|
|
203
|
+
const ordered = resolved.order === "asc" ? filtered : [...filtered].reverse();
|
|
204
|
+
return ordered.slice(0, resolved.limit);
|
|
205
|
+
}
|
|
206
|
+
scanBranch(query) {
|
|
207
|
+
const start = this.entries.get(query.start);
|
|
208
|
+
if (start === undefined)
|
|
209
|
+
throw new Error(`Unknown branch start: ${query.start}`);
|
|
210
|
+
const path = [];
|
|
211
|
+
let entry = start;
|
|
212
|
+
while (entry !== undefined) {
|
|
213
|
+
path.push(entry);
|
|
214
|
+
if (entry.parentId === null)
|
|
215
|
+
break;
|
|
216
|
+
entry = this.entries.get(entry.parentId);
|
|
217
|
+
if (entry === undefined)
|
|
218
|
+
throw new Error("Corrupt branch: missing parent");
|
|
219
|
+
}
|
|
220
|
+
if (query.order === "oldestFirst")
|
|
221
|
+
path.reverse();
|
|
222
|
+
const stopped = [];
|
|
223
|
+
for (const candidate of path) {
|
|
224
|
+
stopped.push(candidate);
|
|
225
|
+
if (candidate.id === query.stopAtId || candidate.type === query.stopAtType)
|
|
226
|
+
break;
|
|
227
|
+
}
|
|
228
|
+
const filtered = stopped
|
|
229
|
+
.filter((candidate) => query.type === undefined || candidate.type === query.type)
|
|
230
|
+
.filter((candidate) => query.customType === undefined || candidate.customType === query.customType)
|
|
231
|
+
.filter((candidate) => query.cursor === undefined ||
|
|
232
|
+
(query.order === "oldestFirst" ? candidate.seq > query.cursor.seq : candidate.seq < query.cursor.seq));
|
|
233
|
+
return query.limit === undefined ? filtered : filtered.slice(0, Math.max(0, query.limit));
|
|
234
|
+
}
|
|
235
|
+
scanBranchStructure(query) {
|
|
236
|
+
return this.scanBranch(query).map((entry) => ({
|
|
237
|
+
id: entry.id,
|
|
238
|
+
parentId: entry.parentId,
|
|
239
|
+
seq: entry.seq,
|
|
240
|
+
timestamp: entry.timestamp,
|
|
241
|
+
type: entry.type,
|
|
242
|
+
...(entry.customType === undefined ? {} : { customType: entry.customType }),
|
|
243
|
+
}));
|
|
244
|
+
}
|
|
245
|
+
scanEntries(query) {
|
|
246
|
+
const limit = query.limit === undefined ? Number.POSITIVE_INFINITY : Math.max(0, Math.trunc(query.limit));
|
|
247
|
+
const entries = [];
|
|
248
|
+
const descending = query.order === "desc";
|
|
249
|
+
let index = descending ? this.entriesBySeq.length - 1 : 0;
|
|
250
|
+
while (index >= 0 && index < this.entriesBySeq.length && entries.length < limit) {
|
|
251
|
+
const entry = this.entriesBySeq[index];
|
|
252
|
+
if ((query.type === undefined || entry.type === query.type) &&
|
|
253
|
+
(query.customType === undefined || entry.customType === query.customType) &&
|
|
254
|
+
(query.fromSeq === undefined || entry.seq >= query.fromSeq) &&
|
|
255
|
+
(query.toSeq === undefined || entry.seq <= query.toSeq)) {
|
|
256
|
+
entries.push(entry);
|
|
257
|
+
}
|
|
258
|
+
index += descending ? -1 : 1;
|
|
259
|
+
}
|
|
260
|
+
return entries;
|
|
261
|
+
}
|
|
262
|
+
scanUsage(query) {
|
|
263
|
+
const rows = [...this.usage.values()]
|
|
264
|
+
.filter((row) => query.fromSeq === undefined || row.seq >= query.fromSeq)
|
|
265
|
+
.filter((row) => query.toSeq === undefined || row.seq <= query.toSeq)
|
|
266
|
+
.sort((left, right) => (query.order === "desc" ? right.seq - left.seq : left.seq - right.seq));
|
|
267
|
+
return query.limit === undefined ? rows : rows.slice(0, Math.max(0, query.limit));
|
|
268
|
+
}
|
|
269
|
+
getStats() {
|
|
270
|
+
return this.stats;
|
|
271
|
+
}
|
|
272
|
+
getNextSeq() {
|
|
273
|
+
return this.nextSeq;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=in-memory-storage-state.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory-storage-state.js","sourceRoot":"","sources":["../../../src/harness/session/in-memory-storage-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,EAKN,oBAAoB,EACpB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AC,OAAO,CAAC;aACxE,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,KAAK,SAAS,IAAI,GAAG,CAAC,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC;aACpE,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,KAAK,KAAK,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QAChG,OAAO,KAAK,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;IAAA,CAClF;IAED,QAAQ,GAAiB;QACxB,OAAO,IAAI,CAAC,KAAK,CAAC;IAAA,CAClB;IAED,UAAU,GAAW;QACpB,OAAO,IAAI,CAAC,OAAO,CAAC;IAAA,CACpB;CACD","sourcesContent":["import { addUsage, emptyUsage } from \"../utils/usage.ts\";\nimport {\n\ttype CommittedListAppendWrite,\n\ttype CommittedValueSetWrite,\n\ttype CommittedWrite,\n\ttype PreparedCommit,\n\tprepareStorageCommit,\n\tvalidateCommittedWrites,\n} from \"./commit.ts\";\nimport { projectForkCurrentStateWrite, selectBranchFork } from \"./fork-policy.ts\";\n\nexport type {\n\tCommittedEntryWrite,\n\tCommittedListAppendWrite,\n\tCommittedListDeleteWrite,\n\tCommittedUsageWrite,\n\tCommittedValueDeleteWrite,\n\tCommittedValueSetWrite,\n\tCommittedWrite,\n\tPreparedCommit,\n} from \"./commit.ts\";\n\nimport type {\n\tEntry,\n\tEntryScan,\n\tEntryStructure,\n\tForkOptions,\n\tSessionStats,\n\tStorageBranchScan,\n\tUsageRow,\n\tUsageScan,\n\tWrite,\n} from \"./types.ts\";\nimport {\n\tbranchTip,\n\ttype ListElement,\n\ttype ListReadOptions,\n\tlaneConfig,\n\tlaneState,\n\tlist,\n\tresolveListReadOptions,\n\ttype StoredValue,\n\ttype Value,\n\ttype ValueList,\n\tvalue,\n} from \"./values.ts\";\n\ninterface StoredListSnapshot {\n\taddress: ValueList<unknown>;\n\telements: ListElement<unknown>[];\n}\n\ntype MemoryForkPlan =\n\t| { scope: \"tree\" }\n\t| { scope: \"branch\"; branch: string; destinationTip: string | null; entryIds: Set<string> };\n\nfunction physicalKey(namespace: string, key: string): string {\n\treturn `${namespace}\\u0000${key}`;\n}\n\nfunction compareKeys(left: string, right: string): number {\n\tconst leftCodePoints = Array.from(left, (character) => character.codePointAt(0)!);\n\tconst rightCodePoints = Array.from(right, (character) => character.codePointAt(0)!);\n\tconst length = Math.min(leftCodePoints.length, rightCodePoints.length);\n\tfor (let index = 0; index < length; index++) {\n\t\tconst difference = leftCodePoints[index]! - rightCodePoints[index]!;\n\t\tif (difference !== 0) return difference;\n\t}\n\treturn leftCodePoints.length - rightCodePoints.length;\n}\n\n/**\n * Complete materialized session state for MemoryStorage and JsonlStorage.\n *\n * This is intentionally unsuitable for database backends and long-running sessions that may not fit in memory.\n * Those backends should query indexed durable state and update durable aggregates within each commit transaction.\n */\nexport class InMemoryStorageState {\n\tprivate readonly entries: Map<string, Entry>;\n\tprivate readonly entriesBySeq: Entry[];\n\tprivate readonly scalarValues: Map<string, StoredValue<unknown>>;\n\tprivate readonly listValues: Map<string, StoredListSnapshot>;\n\tprivate readonly usage: Map<string, UsageRow>;\n\tprivate stats: SessionStats;\n\tprivate nextSeq: number;\n\n\tconstructor() {\n\t\tthis.entries = new Map();\n\t\tthis.entriesBySeq = [];\n\t\tthis.scalarValues = new Map();\n\t\tthis.listValues = new Map();\n\t\tthis.usage = new Map();\n\t\tthis.stats = { messageCount: 0, usage: emptyUsage() };\n\t\tthis.nextSeq = 1;\n\t}\n\n\tprepareCommit(writes: Write[], timestamp: number): PreparedCommit {\n\t\tconst prepared = prepareStorageCommit(writes, this.nextSeq, timestamp);\n\t\tthis.validateCommitted(prepared.writes);\n\t\treturn prepared;\n\t}\n\n\tvalidateCommitted(writes: readonly CommittedWrite[]): void {\n\t\tvalidateCommittedWrites(writes, this.nextSeq, {\n\t\t\thasEntryOrUsageId: (id) => this.entries.has(id) || this.usage.has(id),\n\t\t\thasEntryId: (id) => this.entries.has(id),\n\t\t});\n\t}\n\n\t/** Apply writes already accepted by validateCommitted() and return the post-apply totals. */\n\tapplyValidated(writes: readonly CommittedWrite[]): SessionStats {\n\t\tfor (const write of writes) {\n\t\t\tswitch (write.kind) {\n\t\t\t\tcase \"entry\": {\n\t\t\t\t\tconst { kind: _kind, ...entry } = write;\n\t\t\t\t\tthis.entries.set(entry.id, entry);\n\t\t\t\t\tthis.entriesBySeq.push(entry);\n\t\t\t\t\tif (entry.type === \"message\") this.stats = { ...this.stats, messageCount: this.stats.messageCount + 1 };\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"usage\": {\n\t\t\t\t\tconst { kind: _kind, ...row } = write;\n\t\t\t\t\tthis.usage.set(row.id, row);\n\t\t\t\t\tthis.stats = { ...this.stats, usage: addUsage(this.stats.usage, row.usage) };\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase \"value\":\n\t\t\t\t\tif (write.op === \"delete\") this.scalarValues.delete(physicalKey(write.namespace, write.key));\n\t\t\t\t\telse this.applyValueSetOrListAppend(write);\n\t\t\t\t\tbreak;\n\t\t\t\tcase \"list\":\n\t\t\t\t\tif (write.op === \"delete\") this.listValues.delete(physicalKey(write.namespace, write.key));\n\t\t\t\t\telse this.applyValueSetOrListAppend(write);\n\t\t\t\t\tbreak;\n\t\t\t}\n\t\t\tthis.nextSeq = write.seq + 1;\n\t\t}\n\t\treturn this.stats;\n\t}\n\n\tcreateFork(options: ForkOptions): InMemoryStorageState {\n\t\tconst plan = this.selectForkPlan(options);\n\n\t\tconst isEntryCopied = (entryId: string): boolean => {\n\t\t\tif (plan.scope === \"tree\") return true;\n\t\t\treturn plan.entryIds.has(entryId);\n\t\t};\n\t\tconst destination = new InMemoryStorageState();\n\t\tlet messageCount = 0;\n\t\tfor (const entry of this.entriesBySeq) {\n\t\t\tif (!isEntryCopied(entry.id)) continue;\n\t\t\tdestination.entries.set(entry.id, entry);\n\t\t\tdestination.entriesBySeq.push(entry);\n\t\t\tif (entry.type === \"message\") messageCount++;\n\t\t}\n\t\tdestination.stats = { ...destination.stats, messageCount };\n\n\t\tfor (const stored of this.scalarValues.values()) {\n\t\t\tconst projected = projectForkCurrentStateWrite(\n\t\t\t\t{\n\t\t\t\t\tkind: \"value\",\n\t\t\t\t\top: \"set\",\n\t\t\t\t\tseq: stored.seq,\n\t\t\t\t\tnamespace: stored.address.namespace,\n\t\t\t\t\tkey: stored.address.key,\n\t\t\t\t\tvalue: stored.value,\n\t\t\t\t},\n\t\t\t\tplan,\n\t\t\t\tisEntryCopied,\n\t\t\t);\n\t\t\tif (projected !== undefined) destination.applyValueSetOrListAppend(projected);\n\t\t}\n\n\t\tfor (const stored of this.listValues.values()) {\n\t\t\tfor (const element of stored.elements) {\n\t\t\t\tconst projected = projectForkCurrentStateWrite(\n\t\t\t\t\t{\n\t\t\t\t\t\tkind: \"list\",\n\t\t\t\t\t\top: \"append\",\n\t\t\t\t\t\tseq: element.seq,\n\t\t\t\t\t\tnamespace: stored.address.namespace,\n\t\t\t\t\t\tkey: stored.address.key,\n\t\t\t\t\t\tvalue: element.value,\n\t\t\t\t\t},\n\t\t\t\t\tplan,\n\t\t\t\t\tisEntryCopied,\n\t\t\t\t);\n\t\t\t\tif (projected !== undefined) destination.applyValueSetOrListAppend(projected);\n\t\t\t}\n\t\t}\n\t\tdestination.nextSeq = this.nextSeq;\n\t\treturn destination;\n\t}\n\n\tprivate selectForkPlan(options: ForkOptions): MemoryForkPlan {\n\t\tif (options.scope === \"tree\") return { scope: \"tree\" };\n\n\t\tconst entryIds = new Set<string>();\n\t\tconst plan = selectBranchFork(options, {\n\t\t\ttip: this.getValue(branchTip(options.branch))?.value,\n\t\t\tgetParent: (entryId) => this.entries.get(entryId)?.parentId,\n\t\t\tselectEntry: (entryId) => entryIds.add(entryId),\n\t\t});\n\t\tif (\n\t\t\tthis.getValue(laneConfig(options.branch)) === undefined ||\n\t\t\tthis.getValue(laneState(options.branch)) === undefined\n\t\t) {\n\t\t\tthrow new Error(`Source branch ${JSON.stringify(options.branch)} is not a configured AgentLane`);\n\t\t}\n\t\treturn { ...plan, entryIds };\n\t}\n\n\tprivate applyValueSetOrListAppend(write: CommittedValueSetWrite | CommittedListAppendWrite): void {\n\t\tconst key = physicalKey(write.namespace, write.key);\n\t\tif (write.kind === \"value\") {\n\t\t\tthis.scalarValues.set(key, {\n\t\t\t\taddress: value<unknown>(write.namespace, write.key),\n\t\t\t\tvalue: write.value,\n\t\t\t\tseq: write.seq,\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\n\t\tconst element = { seq: write.seq, value: write.value };\n\t\tconst stored = this.listValues.get(key);\n\t\tif (stored === undefined) {\n\t\t\tthis.listValues.set(key, {\n\t\t\t\taddress: list<unknown>(write.namespace, write.key),\n\t\t\t\telements: [element],\n\t\t\t});\n\t\t} else {\n\t\t\tstored.elements.push(element);\n\t\t}\n\t}\n\n\tadvanceNextSeq(nextSeq: number): void {\n\t\tif (!Number.isSafeInteger(nextSeq) || nextSeq < 1) {\n\t\t\tthrow new Error(`Invalid storage sequence high-water mark: ${nextSeq}`);\n\t\t}\n\t\tthis.nextSeq = Math.max(this.nextSeq, nextSeq);\n\t}\n\n\tgetEntries(ids: readonly string[]): Map<string, Entry> {\n\t\tconst found = new Map<string, Entry>();\n\t\tfor (const id of ids) {\n\t\t\tconst entry = this.entries.get(id);\n\t\t\tif (entry !== undefined) found.set(id, entry);\n\t\t}\n\t\treturn found;\n\t}\n\n\tgetValue<T>(address: Value<T>): StoredValue<T> | undefined {\n\t\treturn this.scalarValues.get(physicalKey(address.namespace, address.key)) as StoredValue<T> | undefined;\n\t}\n\n\tscanValues<T>(prefix: Value<T>): StoredValue<T>[] {\n\t\treturn [...this.scalarValues.values()]\n\t\t\t.filter((stored) => stored.address.namespace === prefix.namespace && stored.address.key.startsWith(prefix.key))\n\t\t\t.sort((left, right) => compareKeys(left.address.key, right.address.key)) as StoredValue<T>[];\n\t}\n\n\treadList<T>(address: ValueList<T>, options?: ListReadOptions): ListElement<T>[] {\n\t\tconst resolved = resolveListReadOptions(options);\n\t\tconst elements = this.listValues.get(physicalKey(address.namespace, address.key))?.elements ?? [];\n\t\tconst filtered = elements.filter((element) => {\n\t\t\tif (resolved.cursor === undefined) return true;\n\t\t\treturn resolved.order === \"asc\" ? element.seq > resolved.cursor.seq : element.seq < resolved.cursor.seq;\n\t\t});\n\t\tconst ordered = resolved.order === \"asc\" ? filtered : [...filtered].reverse();\n\t\treturn ordered.slice(0, resolved.limit) as ListElement<T>[];\n\t}\n\n\tscanBranch(query: StorageBranchScan): Entry[] {\n\t\tconst start = this.entries.get(query.start);\n\t\tif (start === undefined) throw new Error(`Unknown branch start: ${query.start}`);\n\n\t\tconst path: Entry[] = [];\n\t\tlet entry: Entry | undefined = start;\n\t\twhile (entry !== undefined) {\n\t\t\tpath.push(entry);\n\t\t\tif (entry.parentId === null) break;\n\t\t\tentry = this.entries.get(entry.parentId);\n\t\t\tif (entry === undefined) throw new Error(\"Corrupt branch: missing parent\");\n\t\t}\n\t\tif (query.order === \"oldestFirst\") path.reverse();\n\n\t\tconst stopped: Entry[] = [];\n\t\tfor (const candidate of path) {\n\t\t\tstopped.push(candidate);\n\t\t\tif (candidate.id === query.stopAtId || candidate.type === query.stopAtType) break;\n\t\t}\n\t\tconst filtered = stopped\n\t\t\t.filter((candidate) => query.type === undefined || candidate.type === query.type)\n\t\t\t.filter((candidate) => query.customType === undefined || candidate.customType === query.customType)\n\t\t\t.filter(\n\t\t\t\t(candidate) =>\n\t\t\t\t\tquery.cursor === undefined ||\n\t\t\t\t\t(query.order === \"oldestFirst\" ? candidate.seq > query.cursor.seq : candidate.seq < query.cursor.seq),\n\t\t\t);\n\t\treturn query.limit === undefined ? filtered : filtered.slice(0, Math.max(0, query.limit));\n\t}\n\n\tscanBranchStructure(query: StorageBranchScan): EntryStructure[] {\n\t\treturn this.scanBranch(query).map((entry) => ({\n\t\t\tid: entry.id,\n\t\t\tparentId: entry.parentId,\n\t\t\tseq: entry.seq,\n\t\t\ttimestamp: entry.timestamp,\n\t\t\ttype: entry.type,\n\t\t\t...(entry.customType === undefined ? {} : { customType: entry.customType }),\n\t\t}));\n\t}\n\n\tscanEntries(query: EntryScan): Entry[] {\n\t\tconst limit = query.limit === undefined ? Number.POSITIVE_INFINITY : Math.max(0, Math.trunc(query.limit));\n\t\tconst entries: Entry[] = [];\n\t\tconst descending = query.order === \"desc\";\n\t\tlet index = descending ? this.entriesBySeq.length - 1 : 0;\n\t\twhile (index >= 0 && index < this.entriesBySeq.length && entries.length < limit) {\n\t\t\tconst entry = this.entriesBySeq[index]!;\n\t\t\tif (\n\t\t\t\t(query.type === undefined || entry.type === query.type) &&\n\t\t\t\t(query.customType === undefined || entry.customType === query.customType) &&\n\t\t\t\t(query.fromSeq === undefined || entry.seq >= query.fromSeq) &&\n\t\t\t\t(query.toSeq === undefined || entry.seq <= query.toSeq)\n\t\t\t) {\n\t\t\t\tentries.push(entry);\n\t\t\t}\n\t\t\tindex += descending ? -1 : 1;\n\t\t}\n\t\treturn entries;\n\t}\n\n\tscanUsage(query: UsageScan): UsageRow[] {\n\t\tconst rows = [...this.usage.values()]\n\t\t\t.filter((row) => query.fromSeq === undefined || row.seq >= query.fromSeq)\n\t\t\t.filter((row) => query.toSeq === undefined || row.seq <= query.toSeq)\n\t\t\t.sort((left, right) => (query.order === \"desc\" ? right.seq - left.seq : left.seq - right.seq));\n\t\treturn query.limit === undefined ? rows : rows.slice(0, Math.max(0, query.limit));\n\t}\n\n\tgetStats(): SessionStats {\n\t\treturn this.stats;\n\t}\n\n\tgetNextSeq(): number {\n\t\treturn this.nextSeq;\n\t}\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export type { CommittedEntryWrite, CommittedListAppendWrite, CommittedListDeleteWrite, CommittedUsageWrite, CommittedValueDeleteWrite, CommittedValueSetWrite, CommittedWrite, CommitValidationState, PreparedCommit, } from "./commit.ts";
|
|
2
|
+
export { commitWrite, insertEntry, insertUsage, prepareStorageCommit, validateCommittedWrites } from "./commit.ts";
|
|
3
|
+
export { createForkSnapshot, type ForkSourceSnapshot } from "./fork.ts";
|
|
4
|
+
export { type ForkCurrentStatePlan, projectForkCurrentStateWrite } from "./fork-policy.ts";
|
|
5
|
+
export { JSONL_STORAGE_VERSION, type JsonlSessionCreateOptions, type JsonlSessionListOptions, type JsonlSessionMetadata, JsonlSessionRepo, type JsonlSessionRepoOptions, } from "./jsonl/index.ts";
|
|
6
|
+
export type { MemorySessionRepoOptions } from "./memory.ts";
|
|
7
|
+
export { MemorySessionRepo } from "./memory.ts";
|
|
8
|
+
export { SessionBranchExistsError, SessionInvalidBranchError, SessionInvariantError, SessionPendingAssistantMessageError, SessionUnknownTargetError, StorageBackedSession, type StorageBackedSessionOptions, } from "./session.ts";
|
|
9
|
+
export * from "./types.ts";
|
|
10
|
+
export * from "./values.ts";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/harness/session/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACX,mBAAmB,EACnB,wBAAwB,EACxB,wBAAwB,EACxB,mBAAmB,EACnB,yBAAyB,EACzB,sBAAsB,EACtB,cAAc,EACd,qBAAqB,EACrB,cAAc,GACd,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACnH,OAAO,EAAE,kBAAkB,EAAE,KAAK,kBAAkB,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,KAAK,oBAAoB,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AAC3F,OAAO,EACN,qBAAqB,EACrB,KAAK,yBAAyB,EAC9B,KAAK,uBAAuB,EAC5B,KAAK,oBAAoB,EACzB,gBAAgB,EAChB,KAAK,uBAAuB,GAC5B,MAAM,kBAAkB,CAAC;AAC1B,YAAY,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACN,wBAAwB,EACxB,yBAAyB,EACzB,qBAAqB,EACrB,mCAAmC,EACnC,yBAAyB,EACzB,oBAAoB,EACpB,KAAK,2BAA2B,GAChC,MAAM,cAAc,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC","sourcesContent":["export type {\n\tCommittedEntryWrite,\n\tCommittedListAppendWrite,\n\tCommittedListDeleteWrite,\n\tCommittedUsageWrite,\n\tCommittedValueDeleteWrite,\n\tCommittedValueSetWrite,\n\tCommittedWrite,\n\tCommitValidationState,\n\tPreparedCommit,\n} from \"./commit.ts\";\nexport { commitWrite, insertEntry, insertUsage, prepareStorageCommit, validateCommittedWrites } from \"./commit.ts\";\nexport { createForkSnapshot, type ForkSourceSnapshot } from \"./fork.ts\";\nexport { type ForkCurrentStatePlan, projectForkCurrentStateWrite } from \"./fork-policy.ts\";\nexport {\n\tJSONL_STORAGE_VERSION,\n\ttype JsonlSessionCreateOptions,\n\ttype JsonlSessionListOptions,\n\ttype JsonlSessionMetadata,\n\tJsonlSessionRepo,\n\ttype JsonlSessionRepoOptions,\n} from \"./jsonl/index.ts\";\nexport type { MemorySessionRepoOptions } from \"./memory.ts\";\nexport { MemorySessionRepo } from \"./memory.ts\";\nexport {\n\tSessionBranchExistsError,\n\tSessionInvalidBranchError,\n\tSessionInvariantError,\n\tSessionPendingAssistantMessageError,\n\tSessionUnknownTargetError,\n\tStorageBackedSession,\n\ttype StorageBackedSessionOptions,\n} from \"./session.ts\";\nexport * from \"./types.ts\";\nexport * from \"./values.ts\";\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { commitWrite, insertEntry, insertUsage, prepareStorageCommit, validateCommittedWrites } from "./commit.js";
|
|
2
|
+
export { createForkSnapshot } from "./fork.js";
|
|
3
|
+
export { projectForkCurrentStateWrite } from "./fork-policy.js";
|
|
4
|
+
export { JSONL_STORAGE_VERSION, JsonlSessionRepo, } from "./jsonl/index.js";
|
|
5
|
+
export { MemorySessionRepo } from "./memory.js";
|
|
6
|
+
export { SessionBranchExistsError, SessionInvalidBranchError, SessionInvariantError, SessionPendingAssistantMessageError, SessionUnknownTargetError, StorageBackedSession, } from "./session.js";
|
|
7
|
+
export * from "./types.js";
|
|
8
|
+
export * from "./values.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/harness/session/index.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,WAAW,EAAE,oBAAoB,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AACnH,OAAO,EAAE,kBAAkB,EAA2B,MAAM,WAAW,CAAC;AACxE,OAAO,EAA6B,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AAC3F,OAAO,EACN,qBAAqB,EAIrB,gBAAgB,GAEhB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EACN,wBAAwB,EACxB,yBAAyB,EACzB,qBAAqB,EACrB,mCAAmC,EACnC,yBAAyB,EACzB,oBAAoB,GAEpB,MAAM,cAAc,CAAC;AACtB,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC","sourcesContent":["export type {\n\tCommittedEntryWrite,\n\tCommittedListAppendWrite,\n\tCommittedListDeleteWrite,\n\tCommittedUsageWrite,\n\tCommittedValueDeleteWrite,\n\tCommittedValueSetWrite,\n\tCommittedWrite,\n\tCommitValidationState,\n\tPreparedCommit,\n} from \"./commit.ts\";\nexport { commitWrite, insertEntry, insertUsage, prepareStorageCommit, validateCommittedWrites } from \"./commit.ts\";\nexport { createForkSnapshot, type ForkSourceSnapshot } from \"./fork.ts\";\nexport { type ForkCurrentStatePlan, projectForkCurrentStateWrite } from \"./fork-policy.ts\";\nexport {\n\tJSONL_STORAGE_VERSION,\n\ttype JsonlSessionCreateOptions,\n\ttype JsonlSessionListOptions,\n\ttype JsonlSessionMetadata,\n\tJsonlSessionRepo,\n\ttype JsonlSessionRepoOptions,\n} from \"./jsonl/index.ts\";\nexport type { MemorySessionRepoOptions } from \"./memory.ts\";\nexport { MemorySessionRepo } from \"./memory.ts\";\nexport {\n\tSessionBranchExistsError,\n\tSessionInvalidBranchError,\n\tSessionInvariantError,\n\tSessionPendingAssistantMessageError,\n\tSessionUnknownTargetError,\n\tStorageBackedSession,\n\ttype StorageBackedSessionOptions,\n} from \"./session.ts\";\nexport * from \"./types.ts\";\nexport * from \"./values.ts\";\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Result } from "../../types.ts";
|
|
2
|
+
import { type JsonlStorageHeader } from "./types.ts";
|
|
3
|
+
export interface LegacyV3SessionHeader {
|
|
4
|
+
type: "session";
|
|
5
|
+
version: 3;
|
|
6
|
+
id: string;
|
|
7
|
+
timestamp: string;
|
|
8
|
+
cwd: string;
|
|
9
|
+
parentSession?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function isLegacyV3SessionHeader(value: unknown): value is LegacyV3SessionHeader;
|
|
12
|
+
export declare function isJsonlStorageHeader(value: unknown): value is JsonlStorageHeader;
|
|
13
|
+
export type JsonlParsedSessionHeader = {
|
|
14
|
+
format: "v4";
|
|
15
|
+
header: JsonlStorageHeader;
|
|
16
|
+
} | {
|
|
17
|
+
format: "v3-legacy";
|
|
18
|
+
header: LegacyV3SessionHeader;
|
|
19
|
+
};
|
|
20
|
+
export declare function parseJsonlSessionHeader(line: string): Result<JsonlParsedSessionHeader, Error>;
|
|
21
|
+
//# sourceMappingURL=codec.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../../../src/harness/session/jsonl/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,KAAK,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAwB,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAE3E,MAAM,WAAW,qBAAqB;IACrC,IAAI,EAAE,SAAS,CAAC;IAChB,OAAO,EAAE,CAAC,CAAC;IACX,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,EAAE,MAAM,CAAC;IAClB,GAAG,EAAE,MAAM,CAAC;IACZ,aAAa,CAAC,EAAE,MAAM,CAAC;CACvB;AAUD,wBAAgB,uBAAuB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,qBAAqB,CAWtF;AAED,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,kBAAkB,CAahF;AAED,MAAM,MAAM,wBAAwB,GACjC;IAAE,MAAM,EAAE,IAAI,CAAC;IAAC,MAAM,EAAE,kBAAkB,CAAA;CAAE,GAC5C;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,qBAAqB,CAAA;CAAE,CAAC;AAE1D,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,wBAAwB,EAAE,KAAK,CAAC,CAU7F","sourcesContent":["import { err, ok, type Result } from \"../../types.ts\";\nimport { JSONL_FORMAT_VERSION, type JsonlStorageHeader } from \"./types.ts\";\n\nexport interface LegacyV3SessionHeader {\n\ttype: \"session\";\n\tversion: 3;\n\tid: string;\n\ttimestamp: string;\n\tcwd: string;\n\tparentSession?: string;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction isSafeIntegerAtLeast(value: unknown, minimum: number): value is number {\n\treturn Number.isSafeInteger(value) && (value as number) >= minimum;\n}\n\nexport function isLegacyV3SessionHeader(value: unknown): value is LegacyV3SessionHeader {\n\treturn (\n\t\tisRecord(value) &&\n\t\tvalue.type === \"session\" &&\n\t\tvalue.version === 3 &&\n\t\ttypeof value.id === \"string\" &&\n\t\ttypeof value.cwd === \"string\" &&\n\t\ttypeof value.timestamp === \"string\" &&\n\t\tNumber.isFinite(Date.parse(value.timestamp)) &&\n\t\t(value.parentSession === undefined || typeof value.parentSession === \"string\")\n\t);\n}\n\nexport function isJsonlStorageHeader(value: unknown): value is JsonlStorageHeader {\n\treturn (\n\t\tisRecord(value) &&\n\t\tvalue.kind === \"header\" &&\n\t\tvalue.v === JSONL_FORMAT_VERSION &&\n\t\ttypeof value.id === \"string\" &&\n\t\ttypeof value.cwd === \"string\" &&\n\t\tisSafeIntegerAtLeast(value.storageVersion, 1) &&\n\t\tisSafeIntegerAtLeast(value.createdAt, 0) &&\n\t\t(value.nextSeq === undefined || isSafeIntegerAtLeast(value.nextSeq, 1)) &&\n\t\t(value.parentSessionId === undefined || typeof value.parentSessionId === \"string\") &&\n\t\t(value.legacyParentSessionPath === undefined || typeof value.legacyParentSessionPath === \"string\")\n\t);\n}\n\nexport type JsonlParsedSessionHeader =\n\t| { format: \"v4\"; header: JsonlStorageHeader }\n\t| { format: \"v3-legacy\"; header: LegacyV3SessionHeader };\n\nexport function parseJsonlSessionHeader(line: string): Result<JsonlParsedSessionHeader, Error> {\n\tlet value: unknown;\n\ttry {\n\t\tvalue = JSON.parse(line);\n\t} catch (error) {\n\t\treturn err(new Error(\"Invalid JSONL session header: not valid JSON\", { cause: error }));\n\t}\n\tif (isJsonlStorageHeader(value)) return ok({ format: \"v4\", header: value });\n\tif (isLegacyV3SessionHeader(value)) return ok({ format: \"v3-legacy\", header: value });\n\treturn err(new Error(\"Unsupported JSONL session header\"));\n}\n"]}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { err, ok } from "../../types.js";
|
|
2
|
+
import { JSONL_FORMAT_VERSION } from "./types.js";
|
|
3
|
+
function isRecord(value) {
|
|
4
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
5
|
+
}
|
|
6
|
+
function isSafeIntegerAtLeast(value, minimum) {
|
|
7
|
+
return Number.isSafeInteger(value) && value >= minimum;
|
|
8
|
+
}
|
|
9
|
+
export function isLegacyV3SessionHeader(value) {
|
|
10
|
+
return (isRecord(value) &&
|
|
11
|
+
value.type === "session" &&
|
|
12
|
+
value.version === 3 &&
|
|
13
|
+
typeof value.id === "string" &&
|
|
14
|
+
typeof value.cwd === "string" &&
|
|
15
|
+
typeof value.timestamp === "string" &&
|
|
16
|
+
Number.isFinite(Date.parse(value.timestamp)) &&
|
|
17
|
+
(value.parentSession === undefined || typeof value.parentSession === "string"));
|
|
18
|
+
}
|
|
19
|
+
export function isJsonlStorageHeader(value) {
|
|
20
|
+
return (isRecord(value) &&
|
|
21
|
+
value.kind === "header" &&
|
|
22
|
+
value.v === JSONL_FORMAT_VERSION &&
|
|
23
|
+
typeof value.id === "string" &&
|
|
24
|
+
typeof value.cwd === "string" &&
|
|
25
|
+
isSafeIntegerAtLeast(value.storageVersion, 1) &&
|
|
26
|
+
isSafeIntegerAtLeast(value.createdAt, 0) &&
|
|
27
|
+
(value.nextSeq === undefined || isSafeIntegerAtLeast(value.nextSeq, 1)) &&
|
|
28
|
+
(value.parentSessionId === undefined || typeof value.parentSessionId === "string") &&
|
|
29
|
+
(value.legacyParentSessionPath === undefined || typeof value.legacyParentSessionPath === "string"));
|
|
30
|
+
}
|
|
31
|
+
export function parseJsonlSessionHeader(line) {
|
|
32
|
+
let value;
|
|
33
|
+
try {
|
|
34
|
+
value = JSON.parse(line);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
return err(new Error("Invalid JSONL session header: not valid JSON", { cause: error }));
|
|
38
|
+
}
|
|
39
|
+
if (isJsonlStorageHeader(value))
|
|
40
|
+
return ok({ format: "v4", header: value });
|
|
41
|
+
if (isLegacyV3SessionHeader(value))
|
|
42
|
+
return ok({ format: "v3-legacy", header: value });
|
|
43
|
+
return err(new Error("Unsupported JSONL session header"));
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=codec.js.map
|