@diffra-ai/durable 0.0.0-stage → 0.2.2
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 +440 -0
- package/README.md +1153 -2
- package/dist/codemode/codemode.d.ts +174 -0
- package/dist/codemode/codemode.d.ts.map +1 -0
- package/dist/codemode/codemode.js +884 -0
- package/dist/codemode/codemode.js.map +1 -0
- package/dist/codemode/describe.d.ts +38 -0
- package/dist/codemode/describe.d.ts.map +1 -0
- package/dist/codemode/describe.js +127 -0
- package/dist/codemode/describe.js.map +1 -0
- package/dist/codemode/index.d.ts +3 -0
- package/dist/codemode/index.d.ts.map +1 -0
- package/dist/codemode/index.js +3 -0
- package/dist/codemode/index.js.map +1 -0
- package/dist/codemode/models.d.ts +29 -0
- package/dist/codemode/models.d.ts.map +1 -0
- package/dist/codemode/models.js +242 -0
- package/dist/codemode/models.js.map +1 -0
- package/dist/codemode/output.d.ts +43 -0
- package/dist/codemode/output.d.ts.map +1 -0
- package/dist/codemode/output.js +168 -0
- package/dist/codemode/output.js.map +1 -0
- package/dist/documents.d.ts +66 -0
- package/dist/documents.d.ts.map +1 -0
- package/dist/documents.js +98 -0
- package/dist/documents.js.map +1 -0
- package/dist/entries.d.ts +31 -0
- package/dist/entries.d.ts.map +1 -0
- package/dist/entries.js +31 -0
- package/dist/entries.js.map +1 -0
- package/dist/env/decode.d.ts +16 -0
- package/dist/env/decode.d.ts.map +1 -0
- package/dist/env/decode.js +28 -0
- package/dist/env/decode.js.map +1 -0
- package/dist/env/index.d.ts +308 -0
- package/dist/env/index.d.ts.map +1 -0
- package/dist/env/index.js +49 -0
- package/dist/env/index.js.map +1 -0
- package/dist/env/line-scan.d.ts +13 -0
- package/dist/env/line-scan.d.ts.map +1 -0
- package/dist/env/line-scan.js +147 -0
- package/dist/env/line-scan.js.map +1 -0
- package/dist/env/node-watch.d.ts +29 -0
- package/dist/env/node-watch.d.ts.map +1 -0
- package/dist/env/node-watch.js +487 -0
- package/dist/env/node-watch.js.map +1 -0
- package/dist/env/node.d.ts +56 -0
- package/dist/env/node.d.ts.map +1 -0
- package/dist/env/node.js +1185 -0
- package/dist/env/node.js.map +1 -0
- package/dist/errors.d.ts +48 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +76 -0
- package/dist/errors.js.map +1 -0
- package/dist/harness/agent.d.ts +46 -0
- package/dist/harness/agent.d.ts.map +1 -0
- package/dist/harness/agent.js +334 -0
- package/dist/harness/agent.js.map +1 -0
- package/dist/harness/compaction.d.ts +68 -0
- package/dist/harness/compaction.d.ts.map +1 -0
- package/dist/harness/compaction.js +431 -0
- package/dist/harness/compaction.js.map +1 -0
- package/dist/harness/context.d.ts +84 -0
- package/dist/harness/context.d.ts.map +1 -0
- package/dist/harness/context.js +301 -0
- package/dist/harness/context.js.map +1 -0
- package/dist/harness/define.d.ts +25 -0
- package/dist/harness/define.d.ts.map +1 -0
- package/dist/harness/define.js +32 -0
- package/dist/harness/define.js.map +1 -0
- package/dist/harness/generation.d.ts +117 -0
- package/dist/harness/generation.d.ts.map +1 -0
- package/dist/harness/generation.js +1098 -0
- package/dist/harness/generation.js.map +1 -0
- package/dist/harness/harness.d.ts +22 -0
- package/dist/harness/harness.d.ts.map +1 -0
- package/dist/harness/harness.js +438 -0
- package/dist/harness/harness.js.map +1 -0
- package/dist/harness/inbox.d.ts +84 -0
- package/dist/harness/inbox.d.ts.map +1 -0
- package/dist/harness/inbox.js +135 -0
- package/dist/harness/inbox.js.map +1 -0
- package/dist/harness/json.d.ts +26 -0
- package/dist/harness/json.d.ts.map +1 -0
- package/dist/harness/json.js +51 -0
- package/dist/harness/json.js.map +1 -0
- package/dist/harness/live.d.ts +285 -0
- package/dist/harness/live.d.ts.map +1 -0
- package/dist/harness/live.js +257 -0
- package/dist/harness/live.js.map +1 -0
- package/dist/harness/parks.d.ts +23 -0
- package/dist/harness/parks.d.ts.map +1 -0
- package/dist/harness/parks.js +52 -0
- package/dist/harness/parks.js.map +1 -0
- package/dist/harness/partial-changes.d.ts +54 -0
- package/dist/harness/partial-changes.d.ts.map +1 -0
- package/dist/harness/partial-changes.js +163 -0
- package/dist/harness/partial-changes.js.map +1 -0
- package/dist/harness/prompt.d.ts +27 -0
- package/dist/harness/prompt.d.ts.map +1 -0
- package/dist/harness/prompt.js +131 -0
- package/dist/harness/prompt.js.map +1 -0
- package/dist/harness/provider.d.ts +16 -0
- package/dist/harness/provider.d.ts.map +1 -0
- package/dist/harness/provider.js +33 -0
- package/dist/harness/provider.js.map +1 -0
- package/dist/harness/registry-reader.d.ts +11 -0
- package/dist/harness/registry-reader.d.ts.map +1 -0
- package/dist/harness/registry-reader.js +25 -0
- package/dist/harness/registry-reader.js.map +1 -0
- package/dist/harness/registry.d.ts +6 -0
- package/dist/harness/registry.d.ts.map +1 -0
- package/dist/harness/registry.js +101 -0
- package/dist/harness/registry.js.map +1 -0
- package/dist/harness/scheduler.d.ts +195 -0
- package/dist/harness/scheduler.d.ts.map +1 -0
- package/dist/harness/scheduler.js +3073 -0
- package/dist/harness/scheduler.js.map +1 -0
- package/dist/harness/submissions.d.ts +44 -0
- package/dist/harness/submissions.d.ts.map +1 -0
- package/dist/harness/submissions.js +265 -0
- package/dist/harness/submissions.js.map +1 -0
- package/dist/harness/task-graph.d.ts +75 -0
- package/dist/harness/task-graph.d.ts.map +1 -0
- package/dist/harness/task-graph.js +224 -0
- package/dist/harness/task-graph.js.map +1 -0
- package/dist/harness/telemetry.d.ts +307 -0
- package/dist/harness/telemetry.d.ts.map +1 -0
- package/dist/harness/telemetry.js +140 -0
- package/dist/harness/telemetry.js.map +1 -0
- package/dist/harness/tool.d.ts +106 -0
- package/dist/harness/tool.d.ts.map +1 -0
- package/dist/harness/tool.js +1182 -0
- package/dist/harness/tool.js.map +1 -0
- package/dist/harness/transcript.d.ts +72 -0
- package/dist/harness/transcript.d.ts.map +1 -0
- package/dist/harness/transcript.js +264 -0
- package/dist/harness/transcript.js.map +1 -0
- package/dist/harness/types.d.ts +1146 -0
- package/dist/harness/types.d.ts.map +1 -0
- package/dist/harness/types.js +2 -0
- package/dist/harness/types.js.map +1 -0
- package/dist/harness/usage.d.ts +19 -0
- package/dist/harness/usage.d.ts.map +1 -0
- package/dist/harness/usage.js +57 -0
- package/dist/harness/usage.js.map +1 -0
- package/dist/harness/util.d.ts +25 -0
- package/dist/harness/util.d.ts.map +1 -0
- package/dist/harness/util.js +76 -0
- package/dist/harness/util.js.map +1 -0
- package/dist/harness/view.d.ts +69 -0
- package/dist/harness/view.d.ts.map +1 -0
- package/dist/harness/view.js +265 -0
- package/dist/harness/view.js.map +1 -0
- package/dist/ids.d.ts +6 -0
- package/dist/ids.d.ts.map +1 -0
- package/dist/ids.js +9 -0
- package/dist/ids.js.map +1 -0
- package/dist/images/cloudflare.d.ts +4 -0
- package/dist/images/cloudflare.d.ts.map +1 -0
- package/dist/images/cloudflare.js +9 -0
- package/dist/images/cloudflare.js.map +1 -0
- package/dist/images/exif.d.ts +3 -0
- package/dist/images/exif.d.ts.map +1 -0
- package/dist/images/exif.js +71 -0
- package/dist/images/exif.js.map +1 -0
- package/dist/images/fit.d.ts +41 -0
- package/dist/images/fit.d.ts.map +1 -0
- package/dist/images/fit.js +108 -0
- package/dist/images/fit.js.map +1 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.d.ts.map +1 -0
- package/dist/images/index.js +125 -0
- package/dist/images/index.js.map +1 -0
- package/dist/images/node-worker.d.ts +19 -0
- package/dist/images/node-worker.d.ts.map +1 -0
- package/dist/images/node-worker.js +17 -0
- package/dist/images/node-worker.js.map +1 -0
- package/dist/images/node.d.ts +21 -0
- package/dist/images/node.d.ts.map +1 -0
- package/dist/images/node.js +121 -0
- package/dist/images/node.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/search.d.ts +29 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +148 -0
- package/dist/search.js.map +1 -0
- package/dist/session/cached-storage.d.ts +72 -0
- package/dist/session/cached-storage.d.ts.map +1 -0
- package/dist/session/cached-storage.js +269 -0
- package/dist/session/cached-storage.js.map +1 -0
- package/dist/session/conversation-tails.d.ts +33 -0
- package/dist/session/conversation-tails.d.ts.map +1 -0
- package/dist/session/conversation-tails.js +101 -0
- package/dist/session/conversation-tails.js.map +1 -0
- package/dist/session/forks.d.ts +10 -0
- package/dist/session/forks.d.ts.map +1 -0
- package/dist/session/forks.js +42 -0
- package/dist/session/forks.js.map +1 -0
- package/dist/session/keys.d.ts +69 -0
- package/dist/session/keys.d.ts.map +1 -0
- package/dist/session/keys.js +161 -0
- package/dist/session/keys.js.map +1 -0
- package/dist/session/session.d.ts +287 -0
- package/dist/session/session.d.ts.map +1 -0
- package/dist/session/session.js +1701 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/transaction.d.ts +205 -0
- package/dist/session/transaction.d.ts.map +1 -0
- package/dist/session/transaction.js +1059 -0
- package/dist/session/transaction.js.map +1 -0
- package/dist/session/writer.d.ts +74 -0
- package/dist/session/writer.d.ts.map +1 -0
- package/dist/session/writer.js +294 -0
- package/dist/session/writer.js.map +1 -0
- package/dist/signals.d.ts +180 -0
- package/dist/signals.d.ts.map +1 -0
- package/dist/signals.js +236 -0
- package/dist/signals.js.map +1 -0
- package/dist/storage/indexeddb.d.ts +69 -0
- package/dist/storage/indexeddb.d.ts.map +1 -0
- package/dist/storage/indexeddb.js +883 -0
- package/dist/storage/indexeddb.js.map +1 -0
- package/dist/storage/jsonl/index.d.ts +2 -0
- package/dist/storage/jsonl/index.d.ts.map +1 -0
- package/dist/storage/jsonl/index.js +2 -0
- package/dist/storage/jsonl/index.js.map +1 -0
- package/dist/storage/jsonl/node.d.ts +6 -0
- package/dist/storage/jsonl/node.d.ts.map +1 -0
- package/dist/storage/jsonl/node.js +8 -0
- package/dist/storage/jsonl/node.js.map +1 -0
- package/dist/storage/jsonl/storage.d.ts +206 -0
- package/dist/storage/jsonl/storage.d.ts.map +1 -0
- package/dist/storage/jsonl/storage.js +799 -0
- package/dist/storage/jsonl/storage.js.map +1 -0
- package/dist/storage/memory.d.ts +68 -0
- package/dist/storage/memory.d.ts.map +1 -0
- package/dist/storage/memory.js +761 -0
- package/dist/storage/memory.js.map +1 -0
- package/dist/storage/scan.d.ts +18 -0
- package/dist/storage/scan.d.ts.map +1 -0
- package/dist/storage/scan.js +29 -0
- package/dist/storage/scan.js.map +1 -0
- package/dist/storage/sqlite/cloudflare.d.ts +9 -0
- package/dist/storage/sqlite/cloudflare.d.ts.map +1 -0
- package/dist/storage/sqlite/cloudflare.js +11 -0
- package/dist/storage/sqlite/cloudflare.js.map +1 -0
- package/dist/storage/sqlite/commit.d.ts +52 -0
- package/dist/storage/sqlite/commit.d.ts.map +1 -0
- package/dist/storage/sqlite/commit.js +371 -0
- package/dist/storage/sqlite/commit.js.map +1 -0
- package/dist/storage/sqlite/database.d.ts +2 -0
- package/dist/storage/sqlite/database.d.ts.map +1 -0
- package/dist/storage/sqlite/database.js +2 -0
- package/dist/storage/sqlite/database.js.map +1 -0
- package/dist/storage/sqlite/index.d.ts +4 -0
- package/dist/storage/sqlite/index.d.ts.map +1 -0
- package/dist/storage/sqlite/index.js +3 -0
- package/dist/storage/sqlite/index.js.map +1 -0
- package/dist/storage/sqlite/migrations.d.ts +12 -0
- package/dist/storage/sqlite/migrations.d.ts.map +1 -0
- package/dist/storage/sqlite/migrations.js +121 -0
- package/dist/storage/sqlite/migrations.js.map +1 -0
- package/dist/storage/sqlite/node.d.ts +8 -0
- package/dist/storage/sqlite/node.d.ts.map +1 -0
- package/dist/storage/sqlite/node.js +12 -0
- package/dist/storage/sqlite/node.js.map +1 -0
- package/dist/storage/sqlite/rows.d.ts +33 -0
- package/dist/storage/sqlite/rows.d.ts.map +1 -0
- package/dist/storage/sqlite/rows.js +53 -0
- package/dist/storage/sqlite/rows.js.map +1 -0
- package/dist/storage/sqlite/schema.d.ts +45 -0
- package/dist/storage/sqlite/schema.d.ts.map +1 -0
- package/dist/storage/sqlite/schema.js +50 -0
- package/dist/storage/sqlite/schema.js.map +1 -0
- package/dist/storage/sqlite/statements.d.ts +13 -0
- package/dist/storage/sqlite/statements.d.ts.map +1 -0
- package/dist/storage/sqlite/statements.js +88 -0
- package/dist/storage/sqlite/statements.js.map +1 -0
- package/dist/storage/sqlite/storage.d.ts +86 -0
- package/dist/storage/sqlite/storage.d.ts.map +1 -0
- package/dist/storage/sqlite/storage.js +534 -0
- package/dist/storage/sqlite/storage.js.map +1 -0
- package/dist/storage/store/commit.d.ts +33 -0
- package/dist/storage/store/commit.d.ts.map +1 -0
- package/dist/storage/store/commit.js +286 -0
- package/dist/storage/store/commit.js.map +1 -0
- package/dist/storage/store/documents.d.ts +43 -0
- package/dist/storage/store/documents.d.ts.map +1 -0
- package/dist/storage/store/documents.js +102 -0
- package/dist/storage/store/documents.js.map +1 -0
- package/dist/storage/store/index.d.ts +2 -0
- package/dist/storage/store/index.d.ts.map +1 -0
- package/dist/storage/store/index.js +2 -0
- package/dist/storage/store/index.js.map +1 -0
- package/dist/storage/store/queries.d.ts +49 -0
- package/dist/storage/store/queries.d.ts.map +1 -0
- package/dist/storage/store/queries.js +93 -0
- package/dist/storage/store/queries.js.map +1 -0
- package/dist/storage/store/storage.d.ts +60 -0
- package/dist/storage/store/storage.d.ts.map +1 -0
- package/dist/storage/store/storage.js +438 -0
- package/dist/storage/store/storage.js.map +1 -0
- package/dist/storage/store/tables.d.ts +54 -0
- package/dist/storage/store/tables.d.ts.map +1 -0
- package/dist/storage/store/tables.js +108 -0
- package/dist/storage/store/tables.js.map +1 -0
- package/dist/tasks.d.ts +6 -0
- package/dist/tasks.d.ts.map +1 -0
- package/dist/tasks.js +5 -0
- package/dist/tasks.js.map +1 -0
- package/dist/testing/assertions.d.ts +5 -0
- package/dist/testing/assertions.d.ts.map +1 -0
- package/dist/testing/assertions.js +25 -0
- package/dist/testing/assertions.js.map +1 -0
- package/dist/testing/commit-writes.d.ts +5 -0
- package/dist/testing/commit-writes.d.ts.map +1 -0
- package/dist/testing/commit-writes.js +12 -0
- package/dist/testing/commit-writes.js.map +1 -0
- package/dist/testing/env-conformance.d.ts +7 -0
- package/dist/testing/env-conformance.d.ts.map +1 -0
- package/dist/testing/env-conformance.js +531 -0
- package/dist/testing/env-conformance.js.map +1 -0
- package/dist/testing/index.d.ts +8 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +7 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/runner.d.ts +12 -0
- package/dist/testing/runner.d.ts.map +1 -0
- package/dist/testing/runner.js +23 -0
- package/dist/testing/runner.js.map +1 -0
- package/dist/testing/storage-benchmark.d.ts +41 -0
- package/dist/testing/storage-benchmark.d.ts.map +1 -0
- package/dist/testing/storage-benchmark.js +351 -0
- package/dist/testing/storage-benchmark.js.map +1 -0
- package/dist/testing/storage-conformance.d.ts +4 -0
- package/dist/testing/storage-conformance.d.ts.map +1 -0
- package/dist/testing/storage-conformance.js +1554 -0
- package/dist/testing/storage-conformance.js.map +1 -0
- package/dist/testing/types.d.ts +39 -0
- package/dist/testing/types.d.ts.map +1 -0
- package/dist/testing/types.js +2 -0
- package/dist/testing/types.js.map +1 -0
- package/dist/timers.d.ts +21 -0
- package/dist/timers.d.ts.map +1 -0
- package/dist/timers.js +72 -0
- package/dist/timers.js.map +1 -0
- package/dist/tool-search/index.d.ts +2 -0
- package/dist/tool-search/index.d.ts.map +1 -0
- package/dist/tool-search/index.js +2 -0
- package/dist/tool-search/index.js.map +1 -0
- package/dist/tool-search/tool-search.d.ts +17 -0
- package/dist/tool-search/tool-search.d.ts.map +1 -0
- package/dist/tool-search/tool-search.js +96 -0
- package/dist/tool-search/tool-search.js.map +1 -0
- package/dist/tools/bash.d.ts +55 -0
- package/dist/tools/bash.d.ts.map +1 -0
- package/dist/tools/bash.js +139 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/edit-diff.d.ts +89 -0
- package/dist/tools/edit-diff.d.ts.map +1 -0
- package/dist/tools/edit-diff.js +386 -0
- package/dist/tools/edit-diff.js.map +1 -0
- package/dist/tools/edit.d.ts +18 -0
- package/dist/tools/edit.d.ts.map +1 -0
- package/dist/tools/edit.js +117 -0
- package/dist/tools/edit.js.map +1 -0
- package/dist/tools/env.d.ts +5 -0
- package/dist/tools/env.d.ts.map +1 -0
- package/dist/tools/env.js +7 -0
- package/dist/tools/env.js.map +1 -0
- package/dist/tools/file-mutation-queue.d.ts +9 -0
- package/dist/tools/file-mutation-queue.d.ts.map +1 -0
- package/dist/tools/file-mutation-queue.js +52 -0
- package/dist/tools/file-mutation-queue.js.map +1 -0
- package/dist/tools/image-processor.d.ts +57 -0
- package/dist/tools/image-processor.d.ts.map +1 -0
- package/dist/tools/image-processor.js +49 -0
- package/dist/tools/image-processor.js.map +1 -0
- package/dist/tools/image.d.ts +20 -0
- package/dist/tools/image.d.ts.map +1 -0
- package/dist/tools/image.js +178 -0
- package/dist/tools/image.js.map +1 -0
- package/dist/tools/index.d.ts +46 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +23 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/path-utils.d.ts +5 -0
- package/dist/tools/path-utils.d.ts.map +1 -0
- package/dist/tools/path-utils.js +26 -0
- package/dist/tools/path-utils.js.map +1 -0
- package/dist/tools/read.d.ts +30 -0
- package/dist/tools/read.d.ts.map +1 -0
- package/dist/tools/read.js +239 -0
- package/dist/tools/read.js.map +1 -0
- package/dist/tools/write.d.ts +10 -0
- package/dist/tools/write.d.ts.map +1 -0
- package/dist/tools/write.js +31 -0
- package/dist/tools/write.js.map +1 -0
- package/dist/truncate.d.ts +65 -0
- package/dist/truncate.d.ts.map +1 -0
- package/dist/truncate.js +124 -0
- package/dist/truncate.js.map +1 -0
- package/dist/types.d.ts +1244 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +179 -5
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { defineDoc } from "../documents.js";
|
|
2
|
+
import { convertPartial } from "./generation.js";
|
|
3
|
+
export const LiveDoc = defineDoc({
|
|
4
|
+
kind: "diffra.live",
|
|
5
|
+
version: 3,
|
|
6
|
+
scope: "conversation",
|
|
7
|
+
history: "latest",
|
|
8
|
+
fork: "initial",
|
|
9
|
+
initial: () => ({}),
|
|
10
|
+
// Version 1 named a compaction's trigger `reason`, in `compactions` and `compactionFailure`. Versions 1 and 2 (as
|
|
11
|
+
// builds before `run.turns` stored it) may lack `run.turns`: a run in flight across the upgrade counts its turns
|
|
12
|
+
// from 0. Version 2 already names the trigger `trigger`, so renaming leaves it alone.
|
|
13
|
+
migrate: (value) => {
|
|
14
|
+
const live = value;
|
|
15
|
+
const { run, compactions, compactionFailure } = live;
|
|
16
|
+
return {
|
|
17
|
+
...value,
|
|
18
|
+
...(run === undefined ? {} : { run: { ...run, turns: typeof run.turns === "number" ? run.turns : 0 } }),
|
|
19
|
+
...(compactions === undefined ? {} : { compactions: compactions.map(renameReason) }),
|
|
20
|
+
...(compactionFailure === undefined ? {} : { compactionFailure: renameReason(compactionFailure) }),
|
|
21
|
+
};
|
|
22
|
+
},
|
|
23
|
+
// REMINDER: a complete base whenever nothing runs (spec §8.2): no generation and no running tool slot. That holds
|
|
24
|
+
// while idle, in the commit handing a generation over to its tool round, and between tools, so the delta chain
|
|
25
|
+
// spans at most one generation or the overlapping execution of one round's tools. A slot holds output only while
|
|
26
|
+
// running, so every base is small; a nested call's result lives in its caller's task documents, never here. Do not
|
|
27
|
+
// add a delta-count bound; the tool output benchmark checks this rule.
|
|
28
|
+
checkpointWhen: (value) => value.generation === undefined &&
|
|
29
|
+
!(value.tools ?? []).some((slot) => slot.status === "running") &&
|
|
30
|
+
!(value.nestedTools ?? []).some((slot) => slot.status === "running"),
|
|
31
|
+
});
|
|
32
|
+
/** A copy of a version 1 compaction status or failure with `reason` renamed `trigger`, its keys in their order. */
|
|
33
|
+
function renameReason(value) {
|
|
34
|
+
return Object.fromEntries(Object.entries(value).map(([key, field]) => [key === "reason" ? "trigger" : key, field]));
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* How the progress throttles commit a partial answer and running tool output into `diffra.live`: deferred, so storage gets
|
|
38
|
+
* them together, at the next commit that reads or writes `diffra.live` or after `settings.progress.persistWithinMs`. Every
|
|
39
|
+
* commit that consumes or clears streamed fields reads `diffra.live`, so it stores them with its own changes (spec §8.2).
|
|
40
|
+
*/
|
|
41
|
+
export const PROGRESS_COMMIT = { persistence: { kind: "deferred" } };
|
|
42
|
+
/** Built-in task kinds that can own `diffra.live.run`. */
|
|
43
|
+
const RUN_TASK_KINDS = new Set(["diffra.generation"]);
|
|
44
|
+
const TOOL_TASK_KIND = "diffra.tool";
|
|
45
|
+
const COMPACTION_TASK_KIND = "diffra.compaction";
|
|
46
|
+
/**
|
|
47
|
+
* Built-in task kinds that end themselves once cut off `settings.restartLimit` times (`TaskRecord.restarts`); the
|
|
48
|
+
* scheduler faults a task of any other kind instead (spec §5.4).
|
|
49
|
+
*/
|
|
50
|
+
export const LIMITS_RESTARTS = new Set([...RUN_TASK_KINDS, TOOL_TASK_KIND, COMPACTION_TASK_KIND]);
|
|
51
|
+
/** Whether `task` was cut off `settings.restartLimit` times in a row, so its phase ends it instead of running. */
|
|
52
|
+
export function cutOffTooOften(task, runtime) {
|
|
53
|
+
// The settings are read only for a task that was cut off at all, so the normal path resolves nothing more.
|
|
54
|
+
return task.restarts !== undefined && task.restarts >= runtime.settings.restartLimit;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Whether a task at the limit may still do the cleanup it does when it ends, such as converting a partial: only at the
|
|
58
|
+
* limit. Past it, that cleanup may be what cut the task off, so the task ends without it.
|
|
59
|
+
*/
|
|
60
|
+
export function salvageable(task, runtime) {
|
|
61
|
+
return (task.restarts ?? 0) <= runtime.settings.restartLimit;
|
|
62
|
+
}
|
|
63
|
+
/** How often a task was cut off, for a message: "once", or "3 times in a row". */
|
|
64
|
+
export function cutOffCount(task) {
|
|
65
|
+
const restarts = task.restarts ?? 0;
|
|
66
|
+
return restarts === 1 ? "once" : `${restarts} times in a row`;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* End the run owned by `taskId`: settle each of its inputs, remove `run`, and, without an answer, record
|
|
70
|
+
* `runFailure`. A run that answered already, and runs only its `afterRun` tasks, records no failure, however it ends.
|
|
71
|
+
* Always removes `generation`, `tools`, and `nestedTools`, whose presentation belongs to the ending run.
|
|
72
|
+
*/
|
|
73
|
+
export function endRun(tx, live, taskId, end) {
|
|
74
|
+
if (live.run?.taskId === taskId) {
|
|
75
|
+
const inputs = [...live.run.inputs];
|
|
76
|
+
const { first } = live.run;
|
|
77
|
+
if (end.status === "done") {
|
|
78
|
+
for (const id of inputs)
|
|
79
|
+
tx.settleSubmission(id, { status: "done", answer: end.answer });
|
|
80
|
+
}
|
|
81
|
+
else if (live.run.afterRun === undefined) {
|
|
82
|
+
const { reason, detail } = end;
|
|
83
|
+
const settlement = end.settlement ?? {
|
|
84
|
+
status: "unanswered",
|
|
85
|
+
reason,
|
|
86
|
+
...(detail === undefined ? {} : { detail }),
|
|
87
|
+
};
|
|
88
|
+
for (const id of inputs)
|
|
89
|
+
tx.settleSubmission(id, settlement);
|
|
90
|
+
live.runFailure = {
|
|
91
|
+
taskId,
|
|
92
|
+
inputs,
|
|
93
|
+
reason,
|
|
94
|
+
...(detail === undefined ? {} : { detail }),
|
|
95
|
+
...(first === undefined ? {} : { first }),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
delete live.run;
|
|
99
|
+
}
|
|
100
|
+
delete live.generation;
|
|
101
|
+
delete live.tools;
|
|
102
|
+
delete live.nestedTools;
|
|
103
|
+
}
|
|
104
|
+
/** Add the status of a compaction task created in this commit; statuses stay in task ID order. */
|
|
105
|
+
export function addCompactionStatus(live, status) {
|
|
106
|
+
live.compactions ??= [];
|
|
107
|
+
live.compactions.push(status);
|
|
108
|
+
}
|
|
109
|
+
/** The status of compaction task `taskId`, if listed. */
|
|
110
|
+
export function compactionStatus(live, taskId) {
|
|
111
|
+
return live.compactions?.find((status) => status.taskId === taskId);
|
|
112
|
+
}
|
|
113
|
+
/** Remove the status of compaction task `taskId`, and the list once empty. */
|
|
114
|
+
export function removeCompactionStatus(live, taskId) {
|
|
115
|
+
const statuses = live.compactions;
|
|
116
|
+
if (statuses === undefined)
|
|
117
|
+
return;
|
|
118
|
+
const index = statuses.findIndex((status) => status.taskId === taskId);
|
|
119
|
+
if (index >= 0)
|
|
120
|
+
statuses.splice(index, 1);
|
|
121
|
+
if (statuses.length === 0)
|
|
122
|
+
delete live.compactions;
|
|
123
|
+
}
|
|
124
|
+
/** Remove the status of compaction task `taskId` and record that it failed with `error`. */
|
|
125
|
+
export function failCompactionStatus(live, taskId, error) {
|
|
126
|
+
const status = compactionStatus(live, taskId);
|
|
127
|
+
if (status !== undefined)
|
|
128
|
+
live.compactionFailure = { taskId: status.taskId, trigger: status.trigger, error };
|
|
129
|
+
removeCompactionStatus(live, taskId);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The slot of tool task `taskId`, model-issued or nested, if `diffra.live` still lists it. Nested slots are found by binary
|
|
133
|
+
* search: a tool may make thousands of nested calls, each of which looks up its slot in several commits, and every
|
|
134
|
+
* element a search reads costs a node in the draft's change tracker. Their task IDs ascend: each slot is appended in
|
|
135
|
+
* the commit that creates its task, task IDs ascend in commit order, and removal keeps the order.
|
|
136
|
+
*/
|
|
137
|
+
export function toolSlot(live, taskId) {
|
|
138
|
+
return live.tools?.find((each) => each.taskId === taskId) ?? nestedSlot(live, taskId);
|
|
139
|
+
}
|
|
140
|
+
/** The slot of tool task `taskId` with whose call it is, if `diffra.live` still lists it (see `toolSlot`). */
|
|
141
|
+
export function findSlot(live, taskId) {
|
|
142
|
+
const slot = live.tools?.find((each) => each.taskId === taskId);
|
|
143
|
+
if (slot !== undefined)
|
|
144
|
+
return { kind: "model", slot };
|
|
145
|
+
const nested = nestedSlot(live, taskId);
|
|
146
|
+
return nested === undefined ? undefined : { kind: "nested", slot: nested };
|
|
147
|
+
}
|
|
148
|
+
/** The nested slot of tool task `taskId`, by binary search over the ascending task IDs. */
|
|
149
|
+
function nestedSlot(live, taskId) {
|
|
150
|
+
const nested = live.nestedTools;
|
|
151
|
+
if (nested === undefined)
|
|
152
|
+
return undefined;
|
|
153
|
+
let low = 0;
|
|
154
|
+
let high = nested.length - 1;
|
|
155
|
+
while (low <= high) {
|
|
156
|
+
const middle = (low + high) >>> 1;
|
|
157
|
+
const candidate = nested[middle];
|
|
158
|
+
if (candidate.taskId === taskId)
|
|
159
|
+
return candidate;
|
|
160
|
+
if (candidate.taskId < taskId)
|
|
161
|
+
low = middle + 1;
|
|
162
|
+
else
|
|
163
|
+
high = middle - 1;
|
|
164
|
+
}
|
|
165
|
+
return undefined;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Mark a slot done and clear its running output; the caller sets a model-issued call's result `entry`, or a nested
|
|
169
|
+
* call's `summary`. Neither is set when the task faulted or was orphaned.
|
|
170
|
+
*/
|
|
171
|
+
export function finishSlot(slot) {
|
|
172
|
+
slot.status = "done";
|
|
173
|
+
clearProgress(slot);
|
|
174
|
+
}
|
|
175
|
+
/** Remove the nested slots below tool task `taskId`, transitively; the list holds parents before their children. */
|
|
176
|
+
export function removeNestedSlots(live, taskId) {
|
|
177
|
+
const nested = live.nestedTools;
|
|
178
|
+
if (nested === undefined)
|
|
179
|
+
return;
|
|
180
|
+
const removed = new Set([taskId]);
|
|
181
|
+
for (const slot of nested)
|
|
182
|
+
if (removed.has(slot.parentTaskId))
|
|
183
|
+
removed.add(slot.taskId);
|
|
184
|
+
for (let index = nested.length - 1; index >= 0; index--) {
|
|
185
|
+
if (removed.has(nested[index].parentTaskId))
|
|
186
|
+
nested.splice(index, 1);
|
|
187
|
+
}
|
|
188
|
+
if (nested.length === 0)
|
|
189
|
+
delete live.nestedTools;
|
|
190
|
+
}
|
|
191
|
+
/** Remove what a tool published while running; its result entry or a rerun replaces it. */
|
|
192
|
+
export function clearProgress(slot) {
|
|
193
|
+
clearOutput(slot);
|
|
194
|
+
delete slot.details;
|
|
195
|
+
delete slot.diagnostics;
|
|
196
|
+
}
|
|
197
|
+
/** Remove a slot's running output only: a parked call, and its next run, keep its details and diagnostics. */
|
|
198
|
+
export function clearOutput(slot) {
|
|
199
|
+
delete slot.output;
|
|
200
|
+
delete slot.droppedBytes;
|
|
201
|
+
delete slot.droppedLines;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Harness cleanup for a terminal outcome the scheduler writes itself (`faulted` or `orphaned`). A run task ends its
|
|
205
|
+
* run; a tool task's slot is marked done without an entry, and context derivation synthesizes the missing result; a
|
|
206
|
+
* compaction task's status is removed and its failure recorded.
|
|
207
|
+
* Ignores other kinds so it never creates `diffra.live` elsewhere. The scheduler calls this without knowing task kinds;
|
|
208
|
+
* the Harness passes it in (spec §5.4).
|
|
209
|
+
* REMINDER: a committed generation partial becomes an aborted assistant entry here, exactly as in the generation abort
|
|
210
|
+
* handler, so the transcript keeps what the model produced and `diffra.usage` counts its spend. The scheduler's commit has
|
|
211
|
+
* no task scope, so that entry has no `byTaskId`. Faults come from task bugs
|
|
212
|
+
* or malformed provider data (a non-JSON value in a response), or a commit the Storage rejected without effect; an
|
|
213
|
+
* uncertain storage failure poisons the Session instead and writes no outcome.
|
|
214
|
+
*/
|
|
215
|
+
export async function settleSchedulerOutcome(tx, record, outcome) {
|
|
216
|
+
if (record.kind === TOOL_TASK_KIND) {
|
|
217
|
+
const live = await tx.doc(LiveDoc, record.conversationId);
|
|
218
|
+
const slot = toolSlot(live, record.id);
|
|
219
|
+
if (slot !== undefined)
|
|
220
|
+
finishSlot(slot);
|
|
221
|
+
removeNestedSlots(live, record.id);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
// Only an abort handler is faulted for the limit here (the built-in phases end themselves): the task was aborted,
|
|
225
|
+
// and keeps that meaning. Its run ends `aborted`, with the limit as detail, and an aborted compaction is no failure.
|
|
226
|
+
const detail = outcome.status === "faulted" ? outcome.error.detail : undefined;
|
|
227
|
+
const cutOff = typeof detail === "object" && detail !== null && !Array.isArray(detail) && detail.reason === "restart_limit";
|
|
228
|
+
if (record.kind === COMPACTION_TASK_KIND) {
|
|
229
|
+
const live = await tx.doc(LiveDoc, record.conversationId);
|
|
230
|
+
if (cutOff)
|
|
231
|
+
removeCompactionStatus(live, record.id);
|
|
232
|
+
else
|
|
233
|
+
failCompactionStatus(live, record.id, outcome.status === "faulted" ? outcome.error.message : outcome.reason);
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (!RUN_TASK_KINDS.has(record.kind))
|
|
237
|
+
return;
|
|
238
|
+
const live = await tx.doc(LiveDoc, record.conversationId);
|
|
239
|
+
if (live.run?.taskId !== record.id)
|
|
240
|
+
return;
|
|
241
|
+
// The run answered and waited for its `onYield` tasks: it ends with its answer, without a failure.
|
|
242
|
+
const answered = live.run.yield?.answer;
|
|
243
|
+
if (answered !== undefined)
|
|
244
|
+
return endRun(tx, live, record.id, { status: "done", answer: answered });
|
|
245
|
+
// Cut off, the abort handler may have died converting the partial: the run ends without it.
|
|
246
|
+
if (!cutOff)
|
|
247
|
+
await convertPartial(tx, live, record.conversationId);
|
|
248
|
+
endRun(tx, live, record.id, outcome.status === "faulted"
|
|
249
|
+
? { status: "unanswered", reason: cutOff ? "aborted" : "faulted", detail: outcome.error.message }
|
|
250
|
+
: {
|
|
251
|
+
status: "unanswered",
|
|
252
|
+
reason: "orphaned",
|
|
253
|
+
detail: outcome.reason,
|
|
254
|
+
settlement: { status: "unanswered", reason: outcome.reason },
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
//# sourceMappingURL=live.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"live.js","sourceRoot":"","sources":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AAI,EAAE,MAAM,CAAC,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,CAAC;IACrG,4FAA4F;IAC5F,IAAI,CAAC,MAAM;QAAE,MAAM,cAAc,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,cAAc,CAAC,CAAC;IACnE,MAAM,CACL,EAAE,EACF,IAAI,EACJ,MAAM,CAAC,EAAE,EACT,OAAO,CAAC,MAAM,KAAK,SAAS;QAC3B,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE;QACjG,CAAC,CAAC;YACA,MAAM,EAAE,YAAY;YACpB,MAAM,EAAE,UAAU;YAClB,MAAM,EAAE,OAAO,CAAC,MAAM;YACtB,UAAU,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,EAAE,OAAO,CAAC,MAAM,EAAE;SAC5D,CACH,CAAC;AACH,CAAC","sourcesContent":["import type { AssistantMessage, Usage } from \"@diffra-ai/ai\";\nimport type { Draft } from \"@diffra-ai/chord/delta\";\nimport type { JsonRepresentation, JsonValue } from \"@diffra-ai/chord/json\";\nimport { defineDoc } from \"../documents.ts\";\nimport type { Transaction } from \"../session/transaction.ts\";\nimport type {\n\tCommitOptions,\n\tEntryId,\n\tJsonObject,\n\tSubmissionId,\n\tSubmissionSettlement,\n\tTaskId,\n\tTaskRecord,\n\tTx,\n} from \"../types.ts\";\nimport { convertPartial } from \"./generation.ts\";\nimport type { SchedulerOutcome } from \"./scheduler.ts\";\nimport type { CompactionReason, CompactionResult, ToolDiagnostic } from \"./types.ts\";\n\n/** What a running tool call publishes, shared by model-issued and nested calls. */\ntype SlotProgress = {\n\tcallId: string;\n\tname: string;\n\t/** `sleeping` or `waiting` while the call is parked (spec suspendable §4.2); a parked slot holds no output. */\n\tstatus: \"pending\" | \"running\" | \"sleeping\" | \"waiting\" | \"done\";\n\t/** Retained running output and what the bounds dropped. */\n\toutput?: string;\n\tdroppedBytes?: number;\n\tdroppedLines?: number;\n\t/** Last `details()` value. */\n\tdetails?: JsonValue;\n\t/** Diagnostics recorded through `api.diagnostic()`. */\n\tdiagnostics?: ToolDiagnostic[];\n};\n\n/** Presentation of one tool call of the current round. */\nexport type ToolSlot = SlotProgress & {\n\t/** Absent for a call not started yet (sequential round). */\n\ttaskId?: TaskId;\n\t/** A call to a tool its request did not offer: its task settles `tool_unavailable` without running anything. */\n\tunoffered?: true;\n\t/** Result entry once done; absent when the tool task faulted or was orphaned. */\n\tentry?: EntryId;\n};\n\n/**\n * Presentation of one nested call: a call a running tool made through `executeTool()`. `callId` is\n * `<parentCallId>/<key>`, the call's ID, unique because keys contain no `/`. The Harness relates\n * slots by task ID: `taskId` is the call's tool task, `parentTaskId` the calling one, model-issued or nested.\n */\nexport type NestedToolSlot = SlotProgress & {\n\tparentCallId: string;\n\tparentTaskId: TaskId;\n\ttaskId: TaskId;\n\t/**\n\t * What the call was made with, replaced by the arguments it runs with, after repair, `beforeTool`, and coercion,\n\t * once it starts executing. A model-issued call's arguments are in its assistant entry instead.\n\t */\n\targuments: JsonObject;\n\t/** Once done: how the call ended, for a status line. The result itself is in the caller's `NestedCallDoc`. */\n\tsummary?: NestedToolSummary;\n};\n\n/** How a nested call ended: an error flag, its execution time, its spend, and up to 500 characters of error text. */\nexport type NestedToolSummary = {\n\tisError: boolean;\n\tdurationMs?: number;\n\tusage?: JsonRepresentation<Usage>;\n\terror?: string;\n};\n\n/** Presentation of one live compaction task (spec §8.7). */\nexport type CompactionStatus = {\n\ttaskId: TaskId<CompactionResult>;\n\t/** What started it: `compact()`, a threshold, or a context overflow. */\n\ttrigger: CompactionReason;\n\t/** Whether a generation waits for it: a compaction the generation owns. */\n\tblocking: boolean;\n\tattempt: number;\n\t/** Durable backoff before the next summarization attempt. */\n\tretry?: { at: number; error: string };\n};\n\n/** The newest compaction that failed since a compaction last produced a summary (spec §8.7). */\nexport type CompactionFailure = {\n\ttaskId: TaskId<CompactionResult>;\n\t/** What started the compaction that failed. */\n\ttrigger: CompactionReason;\n\t/** The failed or faulted outcome's error message, or why the scheduler orphaned the task. */\n\terror: string;\n};\n\n/**\n * Why a run ended without an answer:\n * - `model_error`: the model's response failed, after any retries, or the compaction a request needed failed;\n * - `no_model`: no model is configured, or the configured one is not available;\n * - `aborted`: the run was aborted (`Conversation.abort()`, `abortTask()`); `detail` says when its abort handler was\n * cut off `settings.restartLimit` times and did not finish;\n * - `reset`: a queued reset started a new context at the post-tools boundary, before an answer (a tool's handoff\n * continues the run);\n * - `faulted`: the run's task threw where it cannot recover, or storage rejected its commit;\n * - `orphaned`: the run's task was aborted while no registered definition could take it;\n * - `interrupted`: the host stopped while the model was answering, more often than `settings.retry` retries;\n * - `restart_limit`: the run's generation was cut off `settings.restartLimit` times in a row; a request or poll it was\n * cut off during counts as a failed attempt first, so this ends one whose charge itself keeps failing;\n * - `nothing_to_answer`: the context did not end in a user message or tool results (empty, or ending in an answer), so\n * no request was sent, as for a continue after an answer;\n * - `turn_limit`: a tool round reached the conversation's `maxTurns`; a continue resumes on its results.\n */\nexport type RunFailureReason =\n\t| \"model_error\"\n\t| \"no_model\"\n\t| \"aborted\"\n\t| \"reset\"\n\t| \"faulted\"\n\t| \"orphaned\"\n\t| \"interrupted\"\n\t| \"restart_limit\"\n\t| \"nothing_to_answer\"\n\t| \"turn_limit\";\n\n/** The newest run that ended without an answer (spec §8.2). */\nexport type RunFailure = {\n\t/** The task that ended the run: the run's task at its end. */\n\ttaskId: TaskId;\n\t/** The run's inputs, settled `unanswered`. */\n\tinputs: SubmissionId[];\n\treason: RunFailureReason;\n\t/**\n\t * `model_error`: the error text; `no_model`: which model is missing; `faulted`: the error message; `orphaned`: why\n\t * the task could not run (`missing_task`, `task_too_old`, `migration_failed`); `interrupted` and `restart_limit`: what\n\t * was cut off, and how often; `turn_limit`: the tool rounds the run took, `\"1 turn\"` or `\"N turns\"`.\n\t */\n\tdetail?: string;\n\t/** The failed run's `run.first`, so a continue's `afterRun` hooks see its history too. */\n\tfirst?: EntryId;\n};\n\n/** Run control (spec §8.2). */\nexport type LiveRun = {\n\t/** The task that settles the run's inputs: its generation, handed from one to the next. */\n\ttaskId: TaskId;\n\t/** The run's placed inputs, settled when it ends; `[]` once it answered and runs only its `afterRun` tasks. */\n\tinputs: SubmissionId[];\n\t/** Tool rounds so far, which `AgentState.maxTurns` bounds; an `onYield` continuation starts the count over. */\n\tturns: number;\n\t/**\n\t * The run's first entry: the first one the boundary that started it placed, a continue's the failed run's it\n\t * continues; otherwise its first tool-calling answer. A run started before it was recorded may lack it; `afterRun`\n\t * hooks then get the answer as the first entry.\n\t */\n\tfirst?: EntryId;\n\t/**\n\t * Present once the run answered and its generation waits for the tasks `afterRun` hooks asked for, listed once\n\t * created: queued items stay queued until they are terminal, and the run then ends without a failure.\n\t */\n\tafterRun?: { tasks?: TaskId[] };\n\t/**\n\t * Present while tasks an `onYield` hook asked for decide whether the answer stands (spec suspendable §6): the\n\t * answer's entry, which the tasks read, and the tasks. The inputs stay open; every exit but a continuation settles\n\t * them `done` with this answer.\n\t */\n\tyield?: { answer: EntryId; tasks: TaskId[] };\n};\n\n/** Built-in live conversation state: run control and presentation of the current generation and tool round. */\nexport type LiveState = {\n\t/** Run control; present exactly while busy, also while a run that answered runs its `afterRun` tasks. */\n\trun?: LiveRun;\n\t/** Presentation of the current generation attempt. */\n\tgeneration?: {\n\t\tattempt: number;\n\t\t/** Committed throttled partial of the in-flight response. */\n\t\tmessage?: JsonRepresentation<AssistantMessage>;\n\t\t/** Durable backoff before the next attempt. */\n\t\tretry?: { at: number; error: string };\n\t\t/** Provider-side deferred response being polled. */\n\t\tdeferred?: { pollAt: number };\n\t};\n\t/** The current tool round in call order, from the tool-calling answer until the generation's `tools` phase ends it. */\n\ttools?: ToolSlot[];\n\t/**\n\t * Nested calls of running tool calls, in creation order, so a parent precedes its children and task IDs ascend. A\n\t * call's nested slots are removed when the call settles.\n\t */\n\tnestedTools?: NestedToolSlot[];\n\t/** Live compaction tasks in task ID order; absent when none. */\n\tcompactions?: CompactionStatus[];\n\t/**\n\t * The newest compaction failure, so a client can show it after the task ended. A compaction that produces a summary\n\t * removes it; one that is aborted or finds nothing to compact leaves it.\n\t */\n\tcompactionFailure?: CompactionFailure;\n\t/**\n\t * Why the newest run ended without an answer, so a client can show it after the run ended. Set in the commit that\n\t * ends the run; removed when a later commit starts a run. A run started in the same commit, by\n\t * `afterFailedRun: \"continue\"` or after a reset, keeps it.\n\t */\n\trunFailure?: RunFailure;\n};\n\nexport const LiveDoc = defineDoc<LiveState>({\n\tkind: \"diffra.live\",\n\tversion: 3,\n\tscope: \"conversation\",\n\thistory: \"latest\",\n\tfork: \"initial\",\n\tinitial: () => ({}),\n\t// Version 1 named a compaction's trigger `reason`, in `compactions` and `compactionFailure`. Versions 1 and 2 (as\n\t// builds before `run.turns` stored it) may lack `run.turns`: a run in flight across the upgrade counts its turns\n\t// from 0. Version 2 already names the trigger `trigger`, so renaming leaves it alone.\n\tmigrate: (value) => {\n\t\tconst live = value as { run?: JsonObject; compactions?: JsonObject[]; compactionFailure?: JsonObject };\n\t\tconst { run, compactions, compactionFailure } = live;\n\t\treturn {\n\t\t\t...value,\n\t\t\t...(run === undefined ? {} : { run: { ...run, turns: typeof run.turns === \"number\" ? run.turns : 0 } }),\n\t\t\t...(compactions === undefined ? {} : { compactions: compactions.map(renameReason) }),\n\t\t\t...(compactionFailure === undefined ? {} : { compactionFailure: renameReason(compactionFailure) }),\n\t\t} as LiveState;\n\t},\n\t// REMINDER: a complete base whenever nothing runs (spec §8.2): no generation and no running tool slot. That holds\n\t// while idle, in the commit handing a generation over to its tool round, and between tools, so the delta chain\n\t// spans at most one generation or the overlapping execution of one round's tools. A slot holds output only while\n\t// running, so every base is small; a nested call's result lives in its caller's task documents, never here. Do not\n\t// add a delta-count bound; the tool output benchmark checks this rule.\n\tcheckpointWhen: (value) =>\n\t\tvalue.generation === undefined &&\n\t\t!(value.tools ?? []).some((slot) => slot.status === \"running\") &&\n\t\t!(value.nestedTools ?? []).some((slot) => slot.status === \"running\"),\n});\n\n/** A copy of a version 1 compaction status or failure with `reason` renamed `trigger`, its keys in their order. */\nfunction renameReason(value: JsonObject): JsonObject {\n\treturn Object.fromEntries(Object.entries(value).map(([key, field]) => [key === \"reason\" ? \"trigger\" : key, field]));\n}\n\n/**\n * How the progress throttles commit a partial answer and running tool output into `diffra.live`: deferred, so storage gets\n * them together, at the next commit that reads or writes `diffra.live` or after `settings.progress.persistWithinMs`. Every\n * commit that consumes or clears streamed fields reads `diffra.live`, so it stores them with its own changes (spec §8.2).\n */\nexport const PROGRESS_COMMIT: CommitOptions = { persistence: { kind: \"deferred\" } };\n\n/** Built-in task kinds that can own `diffra.live.run`. */\nconst RUN_TASK_KINDS: ReadonlySet<string> = new Set([\"diffra.generation\"]);\nconst TOOL_TASK_KIND = \"diffra.tool\";\nconst COMPACTION_TASK_KIND = \"diffra.compaction\";\n\n/**\n * Built-in task kinds that end themselves once cut off `settings.restartLimit` times (`TaskRecord.restarts`); the\n * scheduler faults a task of any other kind instead (spec §5.4).\n */\nexport const LIMITS_RESTARTS: ReadonlySet<string> = new Set([...RUN_TASK_KINDS, TOOL_TASK_KIND, COMPACTION_TASK_KIND]);\n\n/** Whether `task` was cut off `settings.restartLimit` times in a row, so its phase ends it instead of running. */\nexport function cutOffTooOften(\n\ttask: { readonly restarts?: number },\n\truntime: { readonly settings: { readonly restartLimit: number } },\n): boolean {\n\t// The settings are read only for a task that was cut off at all, so the normal path resolves nothing more.\n\treturn task.restarts !== undefined && task.restarts >= runtime.settings.restartLimit;\n}\n\n/**\n * Whether a task at the limit may still do the cleanup it does when it ends, such as converting a partial: only at the\n * limit. Past it, that cleanup may be what cut the task off, so the task ends without it.\n */\nexport function salvageable(\n\ttask: { readonly restarts?: number },\n\truntime: { readonly settings: { readonly restartLimit: number } },\n): boolean {\n\treturn (task.restarts ?? 0) <= runtime.settings.restartLimit;\n}\n\n/** How often a task was cut off, for a message: \"once\", or \"3 times in a row\". */\nexport function cutOffCount(task: { readonly restarts?: number }): string {\n\tconst restarts = task.restarts ?? 0;\n\treturn restarts === 1 ? \"once\" : `${restarts} times in a row`;\n}\n\n/** How a run ends: with an answer, or without one, settling its inputs `unanswered` and recording why. */\nexport type RunEnd =\n\t| { readonly status: \"done\"; readonly answer: EntryId }\n\t| {\n\t\t\treadonly status: \"unanswered\";\n\t\t\treadonly reason: RunFailureReason;\n\t\t\treadonly detail?: string;\n\t\t\t/** The inputs' settlement; default `unanswered` with `reason` and `detail`. */\n\t\t\treadonly settlement?: SubmissionSettlement;\n\t };\n\n/**\n * End the run owned by `taskId`: settle each of its inputs, remove `run`, and, without an answer, record\n * `runFailure`. A run that answered already, and runs only its `afterRun` tasks, records no failure, however it ends.\n * Always removes `generation`, `tools`, and `nestedTools`, whose presentation belongs to the ending run.\n */\nexport function endRun(tx: Tx, live: Draft<LiveState>, taskId: TaskId, end: RunEnd): void {\n\tif (live.run?.taskId === taskId) {\n\t\tconst inputs = [...live.run.inputs];\n\t\tconst { first } = live.run;\n\t\tif (end.status === \"done\") {\n\t\t\tfor (const id of inputs) tx.settleSubmission(id, { status: \"done\", answer: end.answer });\n\t\t} else if (live.run.afterRun === undefined) {\n\t\t\tconst { reason, detail } = end;\n\t\t\tconst settlement = end.settlement ?? {\n\t\t\t\tstatus: \"unanswered\",\n\t\t\t\treason,\n\t\t\t\t...(detail === undefined ? {} : { detail }),\n\t\t\t};\n\t\t\tfor (const id of inputs) tx.settleSubmission(id, settlement);\n\t\t\tlive.runFailure = {\n\t\t\t\ttaskId,\n\t\t\t\tinputs,\n\t\t\t\treason,\n\t\t\t\t...(detail === undefined ? {} : { detail }),\n\t\t\t\t...(first === undefined ? {} : { first }),\n\t\t\t};\n\t\t}\n\t\tdelete live.run;\n\t}\n\tdelete live.generation;\n\tdelete live.tools;\n\tdelete live.nestedTools;\n}\n\n/** Add the status of a compaction task created in this commit; statuses stay in task ID order. */\nexport function addCompactionStatus(live: Draft<LiveState>, status: CompactionStatus): void {\n\tlive.compactions ??= [];\n\tlive.compactions.push(status);\n}\n\n/** The status of compaction task `taskId`, if listed. */\nexport function compactionStatus(live: Draft<LiveState>, taskId: TaskId): Draft<CompactionStatus> | undefined {\n\treturn live.compactions?.find((status) => status.taskId === taskId);\n}\n\n/** Remove the status of compaction task `taskId`, and the list once empty. */\nexport function removeCompactionStatus(live: Draft<LiveState>, taskId: TaskId): void {\n\tconst statuses = live.compactions;\n\tif (statuses === undefined) return;\n\tconst index = statuses.findIndex((status) => status.taskId === taskId);\n\tif (index >= 0) statuses.splice(index, 1);\n\tif (statuses.length === 0) delete live.compactions;\n}\n\n/** Remove the status of compaction task `taskId` and record that it failed with `error`. */\nexport function failCompactionStatus(live: Draft<LiveState>, taskId: TaskId, error: string): void {\n\tconst status = compactionStatus(live, taskId);\n\tif (status !== undefined) live.compactionFailure = { taskId: status.taskId, trigger: status.trigger, error };\n\tremoveCompactionStatus(live, taskId);\n}\n\n/**\n * The slot of tool task `taskId`, model-issued or nested, if `diffra.live` still lists it. Nested slots are found by binary\n * search: a tool may make thousands of nested calls, each of which looks up its slot in several commits, and every\n * element a search reads costs a node in the draft's change tracker. Their task IDs ascend: each slot is appended in\n * the commit that creates its task, task IDs ascend in commit order, and removal keeps the order.\n */\nexport function toolSlot(live: Draft<LiveState>, taskId: TaskId): Draft<ToolSlot | NestedToolSlot> | undefined {\n\treturn live.tools?.find((each) => each.taskId === taskId) ?? nestedSlot(live, taskId);\n}\n\n/** A tool task's slot in `diffra.live`, told apart by whose call it is: a model-issued call's, or a nested call's. */\nexport type FoundSlot =\n\t| { readonly kind: \"model\"; readonly slot: Draft<ToolSlot> }\n\t| { readonly kind: \"nested\"; readonly slot: Draft<NestedToolSlot> };\n\n/** The slot of tool task `taskId` with whose call it is, if `diffra.live` still lists it (see `toolSlot`). */\nexport function findSlot(live: Draft<LiveState>, taskId: TaskId): FoundSlot | undefined {\n\tconst slot = live.tools?.find((each) => each.taskId === taskId);\n\tif (slot !== undefined) return { kind: \"model\", slot };\n\tconst nested = nestedSlot(live, taskId);\n\treturn nested === undefined ? undefined : { kind: \"nested\", slot: nested };\n}\n\n/** The nested slot of tool task `taskId`, by binary search over the ascending task IDs. */\nfunction nestedSlot(live: Draft<LiveState>, taskId: TaskId): Draft<NestedToolSlot> | undefined {\n\tconst nested = live.nestedTools;\n\tif (nested === undefined) return undefined;\n\tlet low = 0;\n\tlet high = nested.length - 1;\n\twhile (low <= high) {\n\t\tconst middle = (low + high) >>> 1;\n\t\tconst candidate = nested[middle]!;\n\t\tif (candidate.taskId === taskId) return candidate;\n\t\tif (candidate.taskId < taskId) low = middle + 1;\n\t\telse high = middle - 1;\n\t}\n\treturn undefined;\n}\n\n/**\n * Mark a slot done and clear its running output; the caller sets a model-issued call's result `entry`, or a nested\n * call's `summary`. Neither is set when the task faulted or was orphaned.\n */\nexport function finishSlot(slot: Draft<ToolSlot | NestedToolSlot>): void {\n\tslot.status = \"done\";\n\tclearProgress(slot);\n}\n\n/** Remove the nested slots below tool task `taskId`, transitively; the list holds parents before their children. */\nexport function removeNestedSlots(live: Draft<LiveState>, taskId: TaskId): void {\n\tconst nested = live.nestedTools;\n\tif (nested === undefined) return;\n\tconst removed = new Set<TaskId>([taskId]);\n\tfor (const slot of nested) if (removed.has(slot.parentTaskId)) removed.add(slot.taskId);\n\tfor (let index = nested.length - 1; index >= 0; index--) {\n\t\tif (removed.has(nested[index]!.parentTaskId)) nested.splice(index, 1);\n\t}\n\tif (nested.length === 0) delete live.nestedTools;\n}\n\n/** Remove what a tool published while running; its result entry or a rerun replaces it. */\nexport function clearProgress(slot: Draft<SlotProgress>): void {\n\tclearOutput(slot);\n\tdelete slot.details;\n\tdelete slot.diagnostics;\n}\n\n/** Remove a slot's running output only: a parked call, and its next run, keep its details and diagnostics. */\nexport function clearOutput(slot: Draft<SlotProgress>): void {\n\tdelete slot.output;\n\tdelete slot.droppedBytes;\n\tdelete slot.droppedLines;\n}\n\n/**\n * Harness cleanup for a terminal outcome the scheduler writes itself (`faulted` or `orphaned`). A run task ends its\n * run; a tool task's slot is marked done without an entry, and context derivation synthesizes the missing result; a\n * compaction task's status is removed and its failure recorded.\n * Ignores other kinds so it never creates `diffra.live` elsewhere. The scheduler calls this without knowing task kinds;\n * the Harness passes it in (spec §5.4).\n * REMINDER: a committed generation partial becomes an aborted assistant entry here, exactly as in the generation abort\n * handler, so the transcript keeps what the model produced and `diffra.usage` counts its spend. The scheduler's commit has\n * no task scope, so that entry has no `byTaskId`. Faults come from task bugs\n * or malformed provider data (a non-JSON value in a response), or a commit the Storage rejected without effect; an\n * uncertain storage failure poisons the Session instead and writes no outcome.\n */\nexport async function settleSchedulerOutcome(\n\ttx: Transaction,\n\trecord: TaskRecord<JsonValue, JsonValue, JsonValue>,\n\toutcome: SchedulerOutcome,\n): Promise<void> {\n\tif (record.kind === TOOL_TASK_KIND) {\n\t\tconst live = await tx.doc(LiveDoc, record.conversationId);\n\t\tconst slot = toolSlot(live, record.id);\n\t\tif (slot !== undefined) finishSlot(slot);\n\t\tremoveNestedSlots(live, record.id);\n\t\treturn;\n\t}\n\t// Only an abort handler is faulted for the limit here (the built-in phases end themselves): the task was aborted,\n\t// and keeps that meaning. Its run ends `aborted`, with the limit as detail, and an aborted compaction is no failure.\n\tconst detail = outcome.status === \"faulted\" ? outcome.error.detail : undefined;\n\tconst cutOff =\n\t\ttypeof detail === \"object\" && detail !== null && !Array.isArray(detail) && detail.reason === \"restart_limit\";\n\tif (record.kind === COMPACTION_TASK_KIND) {\n\t\tconst live = await tx.doc(LiveDoc, record.conversationId);\n\t\tif (cutOff) removeCompactionStatus(live, record.id);\n\t\telse failCompactionStatus(live, record.id, outcome.status === \"faulted\" ? outcome.error.message : outcome.reason);\n\t\treturn;\n\t}\n\tif (!RUN_TASK_KINDS.has(record.kind)) return;\n\tconst live = await tx.doc(LiveDoc, record.conversationId);\n\tif (live.run?.taskId !== record.id) return;\n\t// The run answered and waited for its `onYield` tasks: it ends with its answer, without a failure.\n\tconst answered = live.run.yield?.answer;\n\tif (answered !== undefined) return endRun(tx, live, record.id, { status: \"done\", answer: answered });\n\t// Cut off, the abort handler may have died converting the partial: the run ends without it.\n\tif (!cutOff) await convertPartial(tx, live, record.conversationId);\n\tendRun(\n\t\ttx,\n\t\tlive,\n\t\trecord.id,\n\t\toutcome.status === \"faulted\"\n\t\t\t? { status: \"unanswered\", reason: cutOff ? \"aborted\" : \"faulted\", detail: outcome.error.message }\n\t\t\t: {\n\t\t\t\t\tstatus: \"unanswered\",\n\t\t\t\t\treason: \"orphaned\",\n\t\t\t\t\tdetail: outcome.reason,\n\t\t\t\t\tsettlement: { status: \"unanswered\", reason: outcome.reason },\n\t\t\t\t},\n\t);\n}\n"]}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ToolExecutionResult, ToolPark, ToolRegistration } from "./types.ts";
|
|
2
|
+
/** Whether `execute()` returned a park rather than a result. */
|
|
3
|
+
export declare function isPark(returned: ToolExecutionResult | ToolPark): returned is ToolPark;
|
|
4
|
+
/**
|
|
5
|
+
* The parks a wrapped base returned in one run of a tool, carried by that run's api under this key. An own enumerable
|
|
6
|
+
* symbol property, so a wrapper that spreads the api, as wrappers do, passes it on to the base.
|
|
7
|
+
*/
|
|
8
|
+
export declare const BASE_PARKS: unique symbol;
|
|
9
|
+
/** A run's api with the parks its wrapped base returned. */
|
|
10
|
+
export type ParkCarrier = {
|
|
11
|
+
readonly [BASE_PARKS]?: Set<ToolPark>;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Why a park breaks its shape (spec suspendable §4.1), or `undefined`: kind `sleep` or `wait`, no result field, and, when `tool` is a wrapper's composite (§9), one its base returned in this run, whose api is `api`. Rules
|
|
15
|
+
* on what it waits for are the commit's.
|
|
16
|
+
*/
|
|
17
|
+
export declare function parkError(tool: ToolRegistration, park: ToolPark, api: ParkCarrier): string | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Apply `wrap` to `tool` as tool wrappers apply (spec suspendable §9): the first wrapper of a base gets a copy whose
|
|
20
|
+
* `execute()` records every park it returns in its run's api, so the composite's own parks are told apart.
|
|
21
|
+
*/
|
|
22
|
+
export declare function wrapComposite<Tool extends ToolRegistration>(tool: Tool, wrap: (tool: Tool) => Tool): Tool;
|
|
23
|
+
//# sourceMappingURL=parks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parks.d.ts","sourceRoot":"","sources":["../../src/harness/parks.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAoB,mBAAmB,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEpG,gEAAgE;AAChE,wBAAgB,MAAM,CAAC,QAAQ,EAAE,mBAAmB,GAAG,QAAQ,GAAG,QAAQ,IAAI,QAAQ,CAGrF;AAED;;;GAGG;AACH,eAAO,MAAM,UAAU,EAAE,OAAO,MAAwC,CAAC;AAEzE,4DAA4D;AAC5D,MAAM,MAAM,WAAW,GAAG;IAAE,QAAQ,CAAC,CAAC,UAAU,CAAC,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAA;CAAE,CAAC;AAKpE;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,gBAAgB,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,EAAE,WAAW,GAAG,MAAM,GAAG,SAAS,CAUtG;AAKD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,IAAI,SAAS,gBAAgB,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,KAAK,IAAI,GAAG,IAAI,CAczG"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Whether `execute()` returned a park rather than a result. */
|
|
2
|
+
export function isPark(returned) {
|
|
3
|
+
// Tool code is a trust boundary: any `kind` makes a park, and `parkError` refuses one that is neither.
|
|
4
|
+
return returned.kind !== undefined;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* The parks a wrapped base returned in one run of a tool, carried by that run's api under this key. An own enumerable
|
|
8
|
+
* symbol property, so a wrapper that spreads the api, as wrappers do, passes it on to the base.
|
|
9
|
+
*/
|
|
10
|
+
export const BASE_PARKS = Symbol("diffra.tool.baseParks");
|
|
11
|
+
/** Fields of a result; a park carrying one of them is invalid. */
|
|
12
|
+
const RESULT_FIELDS = ["output", "structuredOutput", "isError", "details", "diagnostics", "usage", "control"];
|
|
13
|
+
/**
|
|
14
|
+
* Why a park breaks its shape (spec suspendable §4.1), or `undefined`: kind `sleep` or `wait`, no result field, and, when `tool` is a wrapper's composite (§9), one its base returned in this run, whose api is `api`. Rules
|
|
15
|
+
* on what it waits for are the commit's.
|
|
16
|
+
*/
|
|
17
|
+
export function parkError(tool, park, api) {
|
|
18
|
+
// Tool code is a trust boundary for the mixed objects its types forbid.
|
|
19
|
+
const kind = park.kind;
|
|
20
|
+
if (kind !== "sleep" && kind !== "wait")
|
|
21
|
+
return `a park's kind is sleep or wait, not ${String(kind)}`;
|
|
22
|
+
const field = RESULT_FIELDS.find((name) => name in park);
|
|
23
|
+
if (field !== undefined)
|
|
24
|
+
return `a park has no result field, but this one has ${field}`;
|
|
25
|
+
if (composites.has(tool) && api[BASE_PARKS]?.has(park) !== true) {
|
|
26
|
+
return "a wrapper returned a park its base did not return";
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
/** Composite tools a wrapper built; their parks must come from their base. */
|
|
31
|
+
const composites = new WeakSet();
|
|
32
|
+
/**
|
|
33
|
+
* Apply `wrap` to `tool` as tool wrappers apply (spec suspendable §9): the first wrapper of a base gets a copy whose
|
|
34
|
+
* `execute()` records every park it returns in its run's api, so the composite's own parks are told apart.
|
|
35
|
+
*/
|
|
36
|
+
export function wrapComposite(tool, wrap) {
|
|
37
|
+
const base = composites.has(tool)
|
|
38
|
+
? tool
|
|
39
|
+
: {
|
|
40
|
+
...tool,
|
|
41
|
+
execute: async (args, api, context) => {
|
|
42
|
+
const returned = await tool.execute(args, api, context);
|
|
43
|
+
if (isPark(returned))
|
|
44
|
+
api[BASE_PARKS]?.add(returned);
|
|
45
|
+
return returned;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
const composite = wrap(base);
|
|
49
|
+
composites.add(composite);
|
|
50
|
+
return composite;
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=parks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parks.js","sourceRoot":"","sources":["../../src/harness/parks.ts"],"names":[],"mappings":"AAIA,gEAAgE;AAChE,MAAM,UAAU,MAAM,CAAC,QAAwC;IAC9D,uGAAuG;IACvG,OAAO,QAAQ,CAAC,IAAI,KAAK,SAAS,CAAC;AACpC,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAkB,MAAM,CAAC,uBAAuB,CAAC,CAAC;AAKzE,kEAAkE;AAClE,MAAM,aAAa,GAAG,CAAC,QAAQ,EAAE,kBAAkB,EAAE,SAAS,EAAE,SAAS,EAAE,aAAa,EAAE,OAAO,EAAE,SAAS,CAAU,CAAC;AAEvH;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,IAAsB,EAAE,IAAc,EAAE,GAAgB;IACjF,wEAAwE;IACxE,MAAM,IAAI,GAAY,IAAI,CAAC,IAAI,CAAC;IAChC,IAAI,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,uCAAuC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;IACtG,MAAM,KAAK,GAAG,aAAa,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,IAAI,CAAC,CAAC;IACzD,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,gDAAgD,KAAK,EAAE,CAAC;IACxF,IAAI,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,UAAU,CAAC,EAAE,GAAG,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;QACjE,OAAO,mDAAmD,CAAC;IAC5D,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,GAAG,IAAI,OAAO,EAAoB,CAAC;AAEnD;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAgC,IAAU,EAAE,IAA0B;IAClG,MAAM,IAAI,GAAS,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC;QACtC,CAAC,CAAC,IAAI;QACN,CAAC,CAAC;YACA,GAAG,IAAI;YACP,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,GAAmC,EAAE,OAAO,EAAE,EAAE;gBACrE,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;gBACxD,IAAI,MAAM,CAAC,QAAQ,CAAC;oBAAE,GAAG,CAAC,UAAU,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACrD,OAAO,QAAQ,CAAC;YACjB,CAAC;SACD,CAAC;IACJ,MAAM,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,CAAC;IAC7B,UAAU,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;IAC1B,OAAO,SAAS,CAAC;AAClB,CAAC","sourcesContent":["// Tool parks: what `execute()` returns instead of a result to sleep or wait, its shape check, and how the Harness tells\n// a base tool's parks from a wrapper's.\nimport type { ToolExecutionApi, ToolExecutionResult, ToolPark, ToolRegistration } from \"./types.ts\";\n\n/** Whether `execute()` returned a park rather than a result. */\nexport function isPark(returned: ToolExecutionResult | ToolPark): returned is ToolPark {\n\t// Tool code is a trust boundary: any `kind` makes a park, and `parkError` refuses one that is neither.\n\treturn returned.kind !== undefined;\n}\n\n/**\n * The parks a wrapped base returned in one run of a tool, carried by that run's api under this key. An own enumerable\n * symbol property, so a wrapper that spreads the api, as wrappers do, passes it on to the base.\n */\nexport const BASE_PARKS: unique symbol = Symbol(\"diffra.tool.baseParks\");\n\n/** A run's api with the parks its wrapped base returned. */\nexport type ParkCarrier = { readonly [BASE_PARKS]?: Set<ToolPark> };\n\n/** Fields of a result; a park carrying one of them is invalid. */\nconst RESULT_FIELDS = [\"output\", \"structuredOutput\", \"isError\", \"details\", \"diagnostics\", \"usage\", \"control\"] as const;\n\n/**\n * Why a park breaks its shape (spec suspendable §4.1), or `undefined`: kind `sleep` or `wait`, no result field, and, when `tool` is a wrapper's composite (§9), one its base returned in this run, whose api is `api`. Rules\n * on what it waits for are the commit's.\n */\nexport function parkError(tool: ToolRegistration, park: ToolPark, api: ParkCarrier): string | undefined {\n\t// Tool code is a trust boundary for the mixed objects its types forbid.\n\tconst kind: unknown = park.kind;\n\tif (kind !== \"sleep\" && kind !== \"wait\") return `a park's kind is sleep or wait, not ${String(kind)}`;\n\tconst field = RESULT_FIELDS.find((name) => name in park);\n\tif (field !== undefined) return `a park has no result field, but this one has ${field}`;\n\tif (composites.has(tool) && api[BASE_PARKS]?.has(park) !== true) {\n\t\treturn \"a wrapper returned a park its base did not return\";\n\t}\n\treturn undefined;\n}\n\n/** Composite tools a wrapper built; their parks must come from their base. */\nconst composites = new WeakSet<ToolRegistration>();\n\n/**\n * Apply `wrap` to `tool` as tool wrappers apply (spec suspendable §9): the first wrapper of a base gets a copy whose\n * `execute()` records every park it returns in its run's api, so the composite's own parks are told apart.\n */\nexport function wrapComposite<Tool extends ToolRegistration>(tool: Tool, wrap: (tool: Tool) => Tool): Tool {\n\tconst base: Tool = composites.has(tool)\n\t\t? tool\n\t\t: {\n\t\t\t\t...tool,\n\t\t\t\texecute: async (args, api: ToolExecutionApi & ParkCarrier, context) => {\n\t\t\t\t\tconst returned = await tool.execute(args, api, context);\n\t\t\t\t\tif (isPark(returned)) api[BASE_PARKS]?.add(returned);\n\t\t\t\t\treturn returned;\n\t\t\t\t},\n\t\t\t};\n\tconst composite = wrap(base);\n\tcomposites.add(composite);\n\treturn composite;\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AssistantMessage, AssistantMessageEvent, ToolCall } from "@diffra-ai/ai";
|
|
2
|
+
import { type JsonChange } from "@diffra-ai/ai/utils/streaming-json";
|
|
3
|
+
import { type Draft } from "@diffra-ai/chord/delta";
|
|
4
|
+
import type { JsonRepresentation } from "@diffra-ai/chord/json";
|
|
5
|
+
import { type AssignPlan } from "./json.ts";
|
|
6
|
+
import type { LiveState } from "./live.ts";
|
|
7
|
+
/** A partial message as `diffra.live.generation.message` holds it. */
|
|
8
|
+
export type PartialMessage = JsonRepresentation<AssistantMessage>;
|
|
9
|
+
/** Committed arguments of a streamed tool call. */
|
|
10
|
+
type Arguments = JsonRepresentation<ToolCall>["arguments"];
|
|
11
|
+
/** What one partial commit writes besides the partial message itself; from `PartialChanges.take()`. */
|
|
12
|
+
export type PartialCommit = {
|
|
13
|
+
/** The plan `assignJson()` writes the message with: text appends, and the arguments it leaves to `changes`. */
|
|
14
|
+
readonly plan: AssignPlan;
|
|
15
|
+
/** Each tracked tool call by content index: its argument changes since the last commit, in order, and its snapshot. */
|
|
16
|
+
readonly toolCalls: ReadonlyMap<number, {
|
|
17
|
+
readonly changes: readonly JsonChange[];
|
|
18
|
+
readonly snapshot: Arguments;
|
|
19
|
+
}>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Collects, from a response's events, what changed in its partial message since the last commit, so the commit appends
|
|
23
|
+
* new text instead of reassigning growing strings. Reassigning makes Chord compare the old and new string, which costs
|
|
24
|
+
* O(length) per commit.
|
|
25
|
+
*
|
|
26
|
+
* - Text and thinking: the `text_delta` and `thinking_delta` events. The commit appends them where the committed string
|
|
27
|
+
* plus the deltas has the new string's length, and compares otherwise. A block's `*_end` event drops them, because
|
|
28
|
+
* providers may replace the text when a block ends.
|
|
29
|
+
* - Tool-call arguments: a `StreamingJsonParser` per call, fed with the raw argument text of each `toolcall_delta`,
|
|
30
|
+
* reports what each piece changed. The commit applies those changes to the committed arguments in order and leaves
|
|
31
|
+
* the arguments out of the message assignment, so the committed arguments are exactly the parsed text so far.
|
|
32
|
+
* `toolcall_end` ends the tracking; the call's last commit assigns its final arguments.
|
|
33
|
+
*/
|
|
34
|
+
export declare class PartialChanges {
|
|
35
|
+
#private;
|
|
36
|
+
/** Record what `event` changed. */
|
|
37
|
+
observe(event: AssistantMessageEvent): void;
|
|
38
|
+
/**
|
|
39
|
+
* Take what changed since the last call, for a commit of `message`, a copy of the partial. Clears the arguments of
|
|
40
|
+
* tracked tool calls in `message`: the commit writes them from the changes. Call `restore()` if the commit fails.
|
|
41
|
+
*/
|
|
42
|
+
take(message: PartialMessage): PartialCommit;
|
|
43
|
+
/**
|
|
44
|
+
* Write `message` with what `commit` took into `generation.message`: the message leaf by leaf with the plan, then the
|
|
45
|
+
* argument changes. A tracked call whose block is new here gets its arguments from its changes, which then start at
|
|
46
|
+
* its first piece; when the committed message lost a call whose changes were already committed (another writer reset
|
|
47
|
+
* it), the call falls back to its snapshot from now on.
|
|
48
|
+
*/
|
|
49
|
+
commit(generation: Draft<NonNullable<LiveState["generation"]>>, message: PartialMessage, commit: PartialCommit): void;
|
|
50
|
+
/** Put back what a failed commit took, so the next commit applies it first. */
|
|
51
|
+
restore(commit: PartialCommit): void;
|
|
52
|
+
}
|
|
53
|
+
export {};
|
|
54
|
+
//# sourceMappingURL=partial-changes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"partial-changes.d.ts","sourceRoot":"","sources":["../../src/harness/partial-changes.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACvF,OAAO,EAAE,KAAK,UAAU,EAAuB,MAAM,oCAAoC,CAAC;AAC1F,OAAO,EAAc,KAA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