@parcha/agentrun-serve 0.1.0-beta.33 → 0.1.0-beta.35
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 +368 -0
- package/README.md +341 -208
- package/dist/adapters.d.ts +3 -4
- package/dist/adapters.js +18 -15
- package/dist/agent-rows.d.ts +99 -0
- package/dist/agent-rows.js +1 -0
- package/dist/agents-store.d.ts +6 -2
- package/dist/agents-store.js +75 -25
- package/dist/agents.d.ts +9 -125
- package/dist/agents.js +15 -305
- package/dist/answer-json.d.ts +23 -0
- package/dist/answer-json.js +58 -0
- package/dist/api-only.d.ts +1 -1
- package/dist/api-only.js +2 -2
- package/dist/archil-settings.d.ts +11 -1
- package/dist/archil-settings.js +10 -0
- package/dist/bash-read-only.d.ts +35 -0
- package/dist/bash-read-only.js +778 -0
- package/dist/box-settings.d.ts +3 -23
- package/dist/box-settings.js +7 -51
- package/dist/boxes.d.ts +17 -0
- package/dist/boxes.js +27 -20
- package/dist/browser-commands.d.ts +5 -2
- package/dist/browser-commands.js +4 -4
- package/dist/browser-computer.d.ts +5 -0
- package/dist/browser-computer.js +21 -0
- package/dist/browser-cost.d.ts +9 -6
- package/dist/browser-cost.js +95 -51
- package/dist/browser-live.d.ts +7 -20
- package/dist/browser-live.js +15 -93
- package/dist/browser-person.d.ts +18 -51
- package/dist/browser-person.js +94 -277
- package/dist/budget.d.ts +0 -5
- package/dist/budget.js +0 -5
- package/dist/cap.d.ts +14 -8
- package/dist/cap.js +19 -9
- package/dist/cli.js +71 -18
- package/dist/closed-pipes.d.ts +2 -0
- package/dist/closed-pipes.js +13 -0
- package/dist/computer-commands.d.ts +2 -0
- package/dist/computer-commands.js +2 -0
- package/dist/computer-keys.d.ts +20 -10
- package/dist/computer-keys.js +36 -54
- package/dist/computer-setting.d.ts +20 -3
- package/dist/computer-setting.js +58 -10
- package/dist/computers.d.ts +35 -2
- package/dist/computers.js +72 -11
- package/dist/confine.d.ts +120 -0
- package/dist/confine.js +436 -0
- package/dist/console/assets/index-B7NKTU8E.css +1 -0
- package/dist/console/assets/index-CXZMgJE7.js +550 -0
- package/dist/console/index.html +2 -2
- package/dist/content-filter.d.ts +7 -0
- package/dist/content-filter.js +23 -0
- package/dist/decision-cap.d.ts +7 -29
- package/dist/decision-cap.js +3 -94
- package/dist/decision-rows.d.ts +1 -1
- package/dist/decision-rows.js +1 -1
- package/dist/docker-daemon.d.ts +31 -0
- package/dist/docker-daemon.js +56 -0
- package/dist/effort-guidance.js +4 -3
- package/dist/environment.d.ts +0 -3
- package/dist/environment.js +3 -5
- package/dist/errors.d.ts +1 -2
- package/dist/errors.js +9 -9
- package/dist/explain.js +3 -1
- package/dist/filed-answer.d.ts +32 -0
- package/dist/filed-answer.js +160 -0
- package/dist/files.d.ts +1 -3
- package/dist/files.js +2 -4
- package/dist/github-connection.d.ts +1 -1
- package/dist/github-connection.js +25 -7
- package/dist/github-faux.d.ts +26 -0
- package/dist/github-faux.js +76 -0
- package/dist/github.d.ts +6 -2
- package/dist/github.js +5 -3
- package/dist/graph-served.d.ts +5 -3
- package/dist/graph-served.js +14 -6
- package/dist/graph-view.d.ts +3 -5
- package/dist/graph-view.js +9 -16
- package/dist/handed-values.d.ts +9 -0
- package/dist/handed-values.js +26 -0
- package/dist/harness-install.d.ts +1 -7
- package/dist/harness-install.js +1 -18
- package/dist/harness-opencode/adapter.d.ts +7 -0
- package/dist/harness-opencode/adapter.js +23 -6
- package/dist/harness-opencode/harness.js +0 -1
- package/dist/harness-opencode/host/policy.js +2 -2
- package/dist/harness-opencode/index.d.ts +1 -1
- package/dist/harness-opencode/index.js +1 -1
- package/dist/harness-opencode/verify.d.ts +17 -0
- package/dist/harness-opencode/verify.js +30 -10
- package/dist/harness-table.js +6 -1
- package/dist/heard.js +1 -1
- package/dist/held-fd.d.ts +5 -0
- package/dist/held-fd.js +14 -0
- package/dist/host-calls.d.ts +20 -0
- package/dist/host-calls.js +12 -0
- package/dist/host-tools.d.ts +4 -21
- package/dist/host-tools.js +4 -61
- package/dist/host.d.ts +2 -4
- package/dist/host.js +2 -7
- package/dist/import-summary.d.ts +1 -5
- package/dist/import-summary.js +0 -21
- package/dist/import.d.ts +6 -6
- package/dist/import.js +14 -15
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/key-refusal.d.ts +5 -1
- package/dist/key-refusal.js +19 -4
- package/dist/learn/check.d.ts +8 -2
- package/dist/learn/check.js +85 -31
- package/dist/learn/folder.d.ts +11 -4
- package/dist/learn/folder.js +17 -19
- package/dist/learn/grade.d.ts +18 -1
- package/dist/learn/grade.js +108 -14
- package/dist/learn/index.d.ts +5 -5
- package/dist/learn/index.js +5 -5
- package/dist/learn/learning.sh +1 -14
- package/dist/learn/notes.d.ts +14 -3
- package/dist/learn/notes.js +16 -6
- package/dist/learn/reflect.d.ts +31 -2
- package/dist/learn/reflect.js +83 -6
- package/dist/learn/relevance.d.ts +24 -0
- package/dist/learn/relevance.js +91 -0
- package/dist/learn/secrets.d.ts +7 -1
- package/dist/learn/secrets.js +86 -19
- package/dist/learn/skills.d.ts +159 -18
- package/dist/learn/skills.js +644 -64
- package/dist/learned-nodes.d.ts +14 -0
- package/dist/learned-nodes.js +8 -0
- package/dist/learned-workflow.d.ts +41 -6
- package/dist/learned-workflow.js +153 -18
- package/dist/learning-routes.d.ts +49 -4
- package/dist/learning-routes.js +113 -31
- package/dist/learning-run.d.ts +16 -31
- package/dist/learning-run.js +31 -38
- package/dist/learning-words.d.ts +12 -5
- package/dist/learning-words.js +18 -9
- package/dist/learning.d.ts +91 -97
- package/dist/learning.js +176 -320
- package/dist/live/stream.js +1 -1
- package/dist/live/translate.js +12 -2
- package/dist/llm.d.ts +4 -1
- package/dist/llm.js +4 -2
- package/dist/local-models.d.ts +11 -5
- package/dist/local-models.js +13 -8
- package/dist/mcp-gateway.d.ts +17 -49
- package/dist/mcp-gateway.js +55 -136
- package/dist/mcp-oauth.d.ts +10 -14
- package/dist/mcp-oauth.js +14 -32
- package/dist/mcp-probe.d.ts +0 -7
- package/dist/mcp-probe.js +4 -10
- package/dist/mcp-servers-store.d.ts +18 -18
- package/dist/mcp-servers-store.js +12 -14
- package/dist/mcp-store.js +3 -4
- package/dist/mcp.js +4 -3
- package/dist/model.d.ts +9 -7
- package/dist/model.js +51 -27
- package/dist/monid.d.ts +0 -2
- package/dist/monid.js +3 -6
- package/dist/openapi.d.ts +8 -1
- package/dist/openapi.js +156 -58
- package/dist/page.d.ts +1 -3
- package/dist/page.js +6 -18
- package/dist/paid-calls.d.ts +21 -27
- package/dist/paid-calls.js +26 -61
- package/dist/pause.d.ts +2 -19
- package/dist/pause.js +5 -74
- package/dist/port-models.d.ts +10 -3
- package/dist/port-models.js +26 -6
- package/dist/propose.d.ts +2 -0
- package/dist/propose.js +80 -45
- package/dist/push-handler.d.ts +1 -1
- package/dist/reflect-range.d.ts +25 -0
- package/dist/reflect-range.js +30 -0
- package/dist/refusal-words.d.ts +5 -1
- package/dist/refusal-words.js +5 -1
- package/dist/rows.d.ts +1 -1
- package/dist/run-cut.d.ts +0 -6
- package/dist/run-cut.js +0 -6
- package/dist/run-db.d.ts +2 -11
- package/dist/run-db.js +2 -37
- package/dist/run-launch.d.ts +21 -2
- package/dist/run-launch.js +14 -5
- package/dist/run-learning-served.d.ts +21 -4
- package/dist/run-learning-served.js +23 -13
- package/dist/run-reads-over-store.d.ts +5 -3
- package/dist/run-reads-over-store.js +25 -14
- package/dist/run-secrets.d.ts +1 -1
- package/dist/run-timeline-served.d.ts +5 -1
- package/dist/run-timeline-served.js +27 -6
- package/dist/run-view.d.ts +37 -22
- package/dist/run-view.js +85 -38
- package/dist/runnable.d.ts +21 -10
- package/dist/runnable.js +35 -12
- package/dist/runner/adapter-loop.d.ts +36 -5
- package/dist/runner/adapter-loop.js +663 -69
- package/dist/runner/adapter-run-spec.d.ts +42 -8
- package/dist/runner/adapter-run-spec.js +186 -52
- package/dist/runner/adapters/index.js +11 -1
- package/dist/runner/adapters/pi.d.ts +40 -2
- package/dist/runner/adapters/pi.js +110 -27
- package/dist/runner/beside.d.ts +38 -3
- package/dist/runner/beside.js +54 -5
- package/dist/runner/claim-secrets.d.ts +16 -1
- package/dist/runner/claim-secrets.js +62 -14
- package/dist/runner/cli-run-spec.d.ts +5 -0
- package/dist/runner/cli-run-spec.js +159 -41
- package/dist/runner/commands.js +1 -1
- package/dist/runner/computer-journal.d.ts +15 -2
- package/dist/runner/computer-journal.js +41 -5
- package/dist/runner/conformance/harness.d.ts +1 -0
- package/dist/runner/conformance/harness.js +123 -13
- package/dist/runner/conformance/subject.d.ts +4 -1
- package/dist/runner/dispatch.d.ts +8 -1
- package/dist/runner/dispatch.js +81 -7
- package/dist/runner/end-of-run.d.ts +8 -2
- package/dist/runner/end-of-run.js +10 -4
- package/dist/runner/github-app.d.ts +7 -2
- package/dist/runner/github-app.js +47 -16
- package/dist/runner/harness-loops.d.ts +15 -2
- package/dist/runner/harness-loops.js +38 -2
- package/dist/runner/journal-kinds.d.ts +60 -6
- package/dist/runner/journal-kinds.js +43 -5
- package/dist/runner/journal-reader.d.ts +1 -1
- package/dist/runner/journal-reader.js +2 -2
- package/dist/runner/learning-ask.d.ts +4 -0
- package/dist/runner/learning-ask.js +15 -1
- package/dist/runner/learning-end.d.ts +18 -3
- package/dist/runner/learning-end.js +194 -20
- package/dist/runner/learning-loop.d.ts +7 -0
- package/dist/runner/learning-loop.js +36 -8
- package/dist/runner/learning-seed.d.ts +6 -0
- package/dist/runner/learning-seed.js +6 -0
- package/dist/runner/loop-contract.d.ts +4 -0
- package/dist/runner/main.d.ts +61 -9
- package/dist/runner/main.js +144 -20
- package/dist/runner/mcp-claim.d.ts +3 -0
- package/dist/runner/mcp-claim.js +7 -3
- package/dist/runner/mcp-github.d.ts +11 -2
- package/dist/runner/mcp-github.js +55 -4
- package/dist/runner/mcp-run.d.ts +2 -0
- package/dist/runner/mcp-run.js +16 -5
- package/dist/runner/memory-claim.d.ts +9 -0
- package/dist/runner/memory-claim.js +25 -0
- package/dist/runner/metrics.d.ts +46 -1
- package/dist/runner/metrics.js +156 -1
- package/dist/runner/model-route.d.ts +16 -0
- package/dist/runner/model-route.js +134 -10
- package/dist/runner/reflect-hook.d.ts +12 -1
- package/dist/runner/reflect-hook.js +33 -3
- package/dist/runner/runner.d.ts +20 -0
- package/dist/runner/runner.js +146 -18
- package/dist/runner/spec-holds.d.ts +7 -2
- package/dist/runner/spec-holds.js +4 -3
- package/dist/runner/store-port.d.ts +1 -1
- package/dist/runner/store-secrets.d.ts +3 -3
- package/dist/runner/store-secrets.js +8 -12
- package/dist/runner/text-tools.d.ts +44 -0
- package/dist/runner/text-tools.js +237 -0
- package/dist/runner/workflow-host.d.ts +7 -2
- package/dist/runner/workflow-host.js +3 -1
- package/dist/runner/workflow-loop.d.ts +10 -1
- package/dist/runner/workflow-loop.js +14 -6
- package/dist/runner/workflow-spec.d.ts +21 -0
- package/dist/runner/workflow-spec.js +217 -93
- package/dist/runs-over-store.d.ts +14 -3
- package/dist/runs-over-store.js +100 -23
- package/dist/seal-key.d.ts +0 -2
- package/dist/seal-key.js +5 -45
- package/dist/sealed-settings-store.d.ts +19 -4
- package/dist/sealed-settings-store.js +9 -9
- package/dist/sealing.d.ts +4 -0
- package/dist/sealing.js +20 -0
- package/dist/secret-cut.d.ts +1 -1
- package/dist/secret-cut.js +1 -1
- package/dist/secret-name.d.ts +9 -0
- package/dist/secret-name.js +15 -0
- package/dist/secret-writes.d.ts +0 -1
- package/dist/secret-writes.js +0 -1
- package/dist/server-fs.d.ts +4 -0
- package/dist/server-fs.js +8 -2
- package/dist/server-lock.d.ts +0 -24
- package/dist/server-lock.js +5 -85
- package/dist/server-secrets-store.d.ts +4 -4
- package/dist/server-secrets-store.js +9 -31
- package/dist/server-secrets.d.ts +4 -9
- package/dist/server-secrets.js +12 -36
- package/dist/server.d.ts +37 -56
- package/dist/server.js +322 -134
- package/dist/sign-ins-store.d.ts +34 -36
- package/dist/sign-ins-store.js +27 -38
- package/dist/sign-ins.d.ts +2 -119
- package/dist/sign-ins.js +5 -311
- package/dist/standard-webhooks.d.ts +1 -1
- package/dist/standard-webhooks.js +1 -1
- package/dist/stats.d.ts +3 -2
- package/dist/stats.js +1 -1
- package/dist/stop-signals.d.ts +4 -2
- package/dist/stop-signals.js +5 -3
- package/dist/store/agent-body.d.ts +12 -2
- package/dist/store/agent-body.js +22 -4
- package/dist/store/deadline.d.ts +18 -0
- package/dist/store/deadline.js +45 -0
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +2 -2
- package/dist/store/open.d.ts +6 -3
- package/dist/store/open.js +2 -2
- package/dist/store/postgres/connection.d.ts +22 -2
- package/dist/store/postgres/connection.js +211 -41
- package/dist/store/postgres/migrate.d.ts +4 -0
- package/dist/store/postgres/migrate.js +9 -2
- package/dist/store/postgres/migrations/0003_commands_one_answer.check.sql +19 -0
- package/dist/store/postgres/migrations/0003_commands_one_answer.sql +13 -0
- package/dist/store/postgres/store.d.ts +9 -2
- package/dist/store/postgres/store.js +9 -0
- package/dist/store/schema.js +1 -1
- package/dist/store/seal.js +2 -2
- package/dist/store/sqlite/connection.js +14 -5
- package/dist/store/sqlite/run-patch.d.ts +3 -0
- package/dist/store/sqlite/run-patch.js +3 -0
- package/dist/store/sqlite/store.js +41 -12
- package/dist/store/store.d.ts +35 -6
- package/dist/store/store.js +12 -2
- package/dist/store-reads/cap.d.ts +1 -1
- package/dist/store-reads/cap.js +22 -4
- package/dist/store-reads/journal.d.ts +9 -6
- package/dist/store-reads/journal.js +129 -18
- package/dist/store-reads/learning.d.ts +82 -8
- package/dist/store-reads/learning.js +184 -11
- package/dist/store-reads/workflow.d.ts +1 -1
- package/dist/store-reads/workflow.js +1 -1
- package/dist/store-view.d.ts +19 -1
- package/dist/store-view.js +95 -27
- package/dist/template-eval.d.ts +60 -4
- package/dist/template-eval.js +120 -54
- package/dist/templates.d.ts +33 -14
- package/dist/templates.js +82 -46
- package/dist/timeline/group.d.ts +33 -0
- package/dist/timeline/group.js +77 -0
- package/dist/timeline/index.d.ts +7 -0
- package/dist/timeline/index.js +7 -0
- package/dist/timeline/labels.d.ts +79 -0
- package/dist/timeline/labels.js +150 -0
- package/dist/timeline/origin.d.ts +56 -0
- package/dist/timeline/origin.js +92 -0
- package/dist/timeline/render-text.d.ts +11 -0
- package/dist/timeline/render-text.js +62 -0
- package/dist/timeline/timeline.d.ts +282 -0
- package/dist/timeline/timeline.js +1308 -0
- package/dist/timeline/util.d.ts +17 -0
- package/dist/timeline/util.js +49 -0
- package/dist/tool-budget.d.ts +18 -0
- package/dist/tool-budget.js +50 -0
- package/dist/tool-gate.d.ts +47 -218
- package/dist/tool-gate.js +87 -545
- package/dist/tool-keys-store.d.ts +11 -12
- package/dist/tool-keys-store.js +24 -31
- package/dist/tool-keys.d.ts +5 -101
- package/dist/tool-keys.js +8 -267
- package/dist/tool-plane.d.ts +23 -52
- package/dist/tool-plane.js +23 -81
- package/dist/tool-settings-store.d.ts +1 -2
- package/dist/tool-settings-store.js +5 -8
- package/dist/tools.d.ts +67 -25
- package/dist/tools.js +170 -119
- package/dist/trigger-adapter.d.ts +2 -2
- package/dist/trigger-targets.d.ts +13 -1
- package/dist/trigger-targets.js +31 -1
- package/dist/triggers.d.ts +2 -2
- package/dist/triggers.js +3 -3
- package/dist/turn-end.d.ts +1 -1
- package/dist/turn-end.js +1 -1
- package/dist/turns.d.ts +0 -6
- package/dist/turns.js +0 -19
- package/dist/uploads.d.ts +28 -5
- package/dist/uploads.js +83 -10
- package/dist/usage.d.ts +1 -49
- package/dist/usage.js +3 -84
- package/dist/validate.d.ts +4 -0
- package/dist/validate.js +33 -14
- package/dist/webhooks.d.ts +102 -76
- package/dist/webhooks.js +266 -233
- package/dist/workflow-run.d.ts +114 -0
- package/dist/workflow-run.js +144 -0
- package/dist/workspace/archil-claim.d.ts +2 -2
- package/dist/workspace/archil-claim.js +4 -3
- package/dist/workspace/archil-disk.d.ts +10 -1
- package/dist/workspace/archil-disk.js +4 -0
- package/dist/workspace/archil-exec.d.ts +12 -2
- package/dist/workspace/archil-exec.js +16 -0
- package/dist/workspace/archil.d.ts +2 -2
- package/dist/workspace/archil.js +25 -5
- package/dist/workspace/attach.js +2 -0
- package/dist/workspace/beneath.d.ts +19 -0
- package/dist/workspace/beneath.js +106 -0
- package/dist/workspace/computer-deps.d.ts +26 -9
- package/dist/workspace/computer-deps.js +144 -43
- package/dist/workspace/computer-for-claim.d.ts +17 -14
- package/dist/workspace/computer-for-claim.js +99 -26
- package/dist/workspace/computer-hold.d.ts +6 -1
- package/dist/workspace/computer-hold.js +6 -1
- package/dist/workspace/computer-janitor.d.ts +47 -0
- package/dist/workspace/computer-janitor.js +182 -0
- package/dist/workspace/computer-ledger.d.ts +1 -8
- package/dist/workspace/computer-ledger.js +0 -3
- package/dist/workspace/computer-providers.d.ts +5 -3
- package/dist/workspace/computer-providers.js +11 -9
- package/dist/workspace/grep-worker.d.ts +5 -0
- package/dist/workspace/grep-worker.js +9 -6
- package/dist/workspace/hands.d.ts +5 -2
- package/dist/workspace/hands.js +4 -3
- package/dist/workspace/index.d.ts +6 -5
- package/dist/workspace/index.js +4 -4
- package/dist/workspace/local.d.ts +14 -2
- package/dist/workspace/local.js +170 -43
- package/dist/workspace/paths.js +3 -2
- package/dist/workspace/pi-env.d.ts +4 -0
- package/dist/workspace/pi-env.js +3 -2
- package/dist/workspace/resolve-computer.d.ts +55 -10
- package/dist/workspace/resolve-computer.js +66 -14
- package/dist/workspace/run-tools.d.ts +15 -31
- package/dist/workspace/run-tools.js +19 -30
- package/dist/workspace/workspace.d.ts +5 -0
- package/dist/workspace-paths.d.ts +0 -17
- package/dist/workspace-paths.js +1 -41
- package/dist/workspace.d.ts +0 -17
- package/dist/workspace.js +4 -110
- package/package.json +14 -14
- package/skills/agentrun-serve/SKILL.md +1 -1
- package/templates/README.md +11 -5
- package/templates/code-review/evals/mentions-defused/case.yaml +15 -0
- package/templates/code-review/evals/mentions-defused/fixtures/github-mcp.json +254 -0
- package/templates/code-review/evals/mentions-defused/graders/no-pings.md +16 -0
- package/templates/code-review/evals/mentions-defused/graders/posted-a-review.md +5 -0
- package/templates/code-review/evals/mentions-defused/graders/schema.md +5 -0
- package/templates/code-review/evals/mentions-defused/prompt.md +10 -0
- package/templates/kyb/evals/clean-public-company/graders/no-opensanctions.md +5 -0
- package/templates/kyb/evals/opensanctions-on/case.yaml +5 -0
- package/templates/kyb/evals/opensanctions-on/graders/licence-and-evidence.md +18 -0
- package/templates/kyb/evals/opensanctions-on/graders/schema.md +5 -0
- package/templates/kyb/evals/opensanctions-on/prompt.md +11 -0
- package/templates/market-monitor/agent.md +2 -2
- package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
- package/templates/release-notes/agent.md +11 -0
- package/dist/bound-files.d.ts +0 -4
- package/dist/bound-files.js +0 -164
- package/dist/coding-tools.d.ts +0 -47
- package/dist/coding-tools.js +0 -189
- package/dist/computer-adb.d.ts +0 -12
- package/dist/computer-adb.js +0 -14
- package/dist/console/assets/index-Dn0ngwG9.js +0 -535
- package/dist/console/assets/index-DvW6Aled.css +0 -1
- package/dist/grade.d.ts +0 -63
- package/dist/grade.js +0 -150
- package/dist/mcp-servers.d.ts +0 -269
- package/dist/mcp-servers.js +0 -456
- package/dist/packs.d.ts +0 -71
- package/dist/packs.js +0 -180
- package/dist/sealed-settings.d.ts +0 -60
- package/dist/sealed-settings.js +0 -171
- package/dist/sessions.d.ts +0 -6
- package/dist/sessions.js +0 -109
- package/dist/workspace/computer-serves.d.ts +0 -12
- package/dist/workspace/computer-serves.js +0 -15
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// rolls back. Errors come back typed: Fenced, RunFinal, StoreBusy, StoreConstraint, StoreClosed, StoreOwned.
|
|
6
6
|
import { realpathSync } from "node:fs";
|
|
7
7
|
import { Worker } from "node:worker_threads";
|
|
8
|
+
import { runnableDeadline } from "../deadline.js";
|
|
8
9
|
import { cleanText, SQLITE } from "../dialect.js";
|
|
9
10
|
import { inMemory } from "./memory.js";
|
|
10
11
|
import { Fenced, RunFinal, StoreBusy, StoreClosed, StoreConstraint, StoreDeadline, StoreOwned, StoreWorkerLost } from "../store.js";
|
|
@@ -234,10 +235,18 @@ export class Connection {
|
|
|
234
235
|
throw error;
|
|
235
236
|
}
|
|
236
237
|
let live = true;
|
|
237
|
-
let
|
|
238
|
-
const left = until !== undefined ? Math.max(0, until - Date.now()) : deadlineMs;
|
|
238
|
+
let clear = () => { };
|
|
239
239
|
const deadline = new Promise((_, reject) => {
|
|
240
|
-
|
|
240
|
+
const expire = () => reject(new StoreDeadline(deadlineMs));
|
|
241
|
+
// Counted from its call (a lease's), the deadline stays the wall clock's: MCP_REFRESH_LEASE_MS is reckoned from
|
|
242
|
+
// it. Counted from its begin, it is the time this process could run (store/deadline.ts runnableDeadline): while other work held
|
|
243
|
+
// the event loop, a statement answered meanwhile waited behind it, and that time was no statement's.
|
|
244
|
+
if (until !== undefined) {
|
|
245
|
+
const timer = setTimeout(expire, Math.max(0, until - Date.now()));
|
|
246
|
+
clear = () => clearTimeout(timer);
|
|
247
|
+
}
|
|
248
|
+
else
|
|
249
|
+
clear = runnableDeadline(deadlineMs, expire).clear;
|
|
241
250
|
});
|
|
242
251
|
let value;
|
|
243
252
|
try {
|
|
@@ -250,7 +259,7 @@ export class Connection {
|
|
|
250
259
|
}
|
|
251
260
|
catch (error) {
|
|
252
261
|
live = false;
|
|
253
|
-
|
|
262
|
+
clear();
|
|
254
263
|
try {
|
|
255
264
|
await this.#send({ op: "rollback" });
|
|
256
265
|
}
|
|
@@ -263,7 +272,7 @@ export class Connection {
|
|
|
263
272
|
throw error;
|
|
264
273
|
}
|
|
265
274
|
live = false;
|
|
266
|
-
|
|
275
|
+
clear();
|
|
267
276
|
// Counted from its call, a deadline reached by now is never committed past: rolled back, nothing written.
|
|
268
277
|
if (until !== undefined && Date.now() >= until) {
|
|
269
278
|
await this.#send({ op: "rollback" });
|
|
@@ -6,6 +6,9 @@ import { type StoreNow } from "../fence.js";
|
|
|
6
6
|
* runner, no box: its only writes are the learning folder's one script). */
|
|
7
7
|
/** A learning run's launch: harness "learning", its input the run that learned, and `model`, the grade model when one is
|
|
8
8
|
* named (gradeModelOf; none: the agent's own grades it, as main's). No cap: main caps no grade. */
|
|
9
|
+
/** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
|
|
10
|
+
* list of runs names. */
|
|
11
|
+
export declare const NOT_A_LEARNING_RUN = "id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL)";
|
|
9
12
|
export declare const learningLaunchOf: (runId: string, model?: string) => {
|
|
10
13
|
model?: string | undefined;
|
|
11
14
|
harness: string;
|
|
@@ -11,6 +11,9 @@ import { writeLaunch } from "./run-launch.js";
|
|
|
11
11
|
* runner, no box: its only writes are the learning folder's one script). */
|
|
12
12
|
/** A learning run's launch: harness "learning", its input the run that learned, and `model`, the grade model when one is
|
|
13
13
|
* named (gradeModelOf; none: the agent's own grades it, as main's). No cap: main caps no grade. */
|
|
14
|
+
/** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
|
|
15
|
+
* list of runs names. */
|
|
16
|
+
export const NOT_A_LEARNING_RUN = "id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL)";
|
|
14
17
|
export const learningLaunchOf = (runId, model) => ({ harness: "learning", input: { runId }, title: `Learning from ${runId}`, place: { mode: "runner" }, ...(model ? { model } : {}) });
|
|
15
18
|
/** The lock a write of a run's sign_in_handed and a sign-in's forget both take (Dialect.lockKey): the forget's check of
|
|
16
19
|
* the running runs' hashes and a launch's write of them are serialized, as SQLite's one writer serializes them. */
|
|
@@ -10,9 +10,9 @@ import { DatabaseSync } from "node:sqlite";
|
|
|
10
10
|
import { claimFence, storeNow } from "../fence.js";
|
|
11
11
|
import { RunStorage } from "../journal/storage.js";
|
|
12
12
|
import { COMMANDS_ONE_ANSWER, DDL, SCHEMA_VERSION } from "../schema.js";
|
|
13
|
-
import { AgentArchived, CommandTaken, ExternalBodyConflict, Fenced, RunFinal, SchemaVersionUnknown, STORE_WRITE_DEADLINE_MS, StoreConstraint, StoreFromEarlierVersion, TriggerFull, FINAL_STATES, MCP_REFRESH_CALL_MS, MCP_REFRESH_LEASE_MS, SignInHandedTo, SignInMissing,
|
|
13
|
+
import { AgentArchived, CommandTaken, ExternalBodyConflict, Fenced, RunFinal, SchemaVersionUnknown, STORE_WRITE_DEADLINE_MS, StoreConstraint, StoreFromEarlierVersion, TriggerFull, FINAL_STATES, MCP_REFRESH_CALL_MS, MCP_REFRESH_LEASE_MS, RevisionMoved, SignInHandedTo, SignInMissing, } from "../store.js";
|
|
14
14
|
import { Connection } from "./connection.js";
|
|
15
|
-
import { applyRunPatch, refuseLeftovers, RUN_COLUMNS, runOf, SIGN_IN_HANDED_LOCK } from "./run-patch.js";
|
|
15
|
+
import { applyRunPatch, NOT_A_LEARNING_RUN, refuseLeftovers, RUN_COLUMNS, runOf, SIGN_IN_HANDED_LOCK } from "./run-patch.js";
|
|
16
16
|
import { readLaunch, writeLaunch } from "./run-launch.js";
|
|
17
17
|
import { canonical } from "../agent-body.js";
|
|
18
18
|
import { aadOf, sealedAt } from "../seal.js";
|
|
@@ -127,8 +127,10 @@ async function versionOf(db) {
|
|
|
127
127
|
* triggers, tool keys and trigger secrets (all in the store now), and the per-run door's token (the per-run door is
|
|
128
128
|
* gone). This version's own top-level files are not among them, so a directory holding them opens: .token and
|
|
129
129
|
* .server.json beside the store and its lock, a leftover box-image.json, and the files this version still keeps beside
|
|
130
|
-
* the store in the same format an earlier version did (the
|
|
131
|
-
*
|
|
130
|
+
* the store in the same format an earlier version did (the kept idempotency answers and the learning usage ledger),
|
|
131
|
+
* which an earlier version's copy of reads the same. A webhook config and outbox an earlier build kept beside the store
|
|
132
|
+
* (webhook.json, webhook-outbox.json) do not refuse the directory either: nothing reads them, and the webhook, now in
|
|
133
|
+
* the store, is set again. */
|
|
132
134
|
const EARLIER_ROOT_FILES = ["mcp-servers.json", "triggers.json", "tool-keys.json", "trigger-secrets.json", ".door-token"];
|
|
133
135
|
/** The files an earlier version kept in a data directory, relative to it: its store (a STORE_FILE with tables and no
|
|
134
136
|
* store_meta row), agent homes (<agent>/agent.sqlite, agents/<agent>/agent.sqlite), run databases (runs/<run>/run.sqlite,
|
|
@@ -303,14 +305,17 @@ class SqliteStore {
|
|
|
303
305
|
const clock = () => storeNow(d, now);
|
|
304
306
|
this.#clock = clock;
|
|
305
307
|
const insertAgent = (tx, agent, at) => tx.run("INSERT INTO agents (id, name, description, created_at) VALUES (?, ?, ?, ?)", agent.id, agent.name, agent.description ?? null, at);
|
|
306
|
-
const insertRevision = async (tx, agentId, revision, at) => {
|
|
308
|
+
const insertRevision = async (tx, agentId, revision, at, ifCurrent) => {
|
|
307
309
|
// The parent is the agent's current revision now (null for its first): the agent must exist, which its first revision
|
|
308
|
-
// (create) and a later one (addRevision, upgrade) both guarantee before this. Held
|
|
309
|
-
// never both name the same parent
|
|
310
|
+
// (create) and a later one (addRevision, upgrade) both guarantee before this. Held (a writer's row lock), so two
|
|
311
|
+
// revisions added at once, by this server or another on the same store, never both name the same parent, and
|
|
312
|
+
// `ifCurrent` is compared with what no other writer can move until this commits.
|
|
310
313
|
await d.lockRow(tx, "agents", "id", agentId);
|
|
311
314
|
const agent = await tx.get("SELECT current_revision FROM agents WHERE id = ?", agentId);
|
|
312
315
|
if (!agent)
|
|
313
316
|
throw new StoreConstraint(`no agent ${agentId}`);
|
|
317
|
+
if (ifCurrent !== undefined && agent.current_revision !== ifCurrent)
|
|
318
|
+
throw new RevisionMoved(agentId, agent.current_revision);
|
|
314
319
|
await tx.run("INSERT INTO revisions (id, agent_id, parent_id, kind, body, skills, note, package, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", revision.id, agentId, agent.current_revision, revision.kind, json(revision.body), revision.skills === undefined || revision.skills === null ? null : json(revision.skills), revision.note ?? null, revision.package === undefined ? null : json(revision.package), at);
|
|
315
320
|
await tx.run("UPDATE agents SET current_revision = ? WHERE id = ?", revision.id, agentId);
|
|
316
321
|
};
|
|
@@ -381,8 +386,8 @@ class SqliteStore {
|
|
|
381
386
|
await writeScopes(tx, t.id, []);
|
|
382
387
|
return off.map(({ id: triggerId, name }) => ({ id: triggerId, name })).sort((a, b) => a.name.localeCompare(b.name));
|
|
383
388
|
}),
|
|
384
|
-
addRevision: async ({ agentId, ...revision }) => {
|
|
385
|
-
await db.transaction((tx) => insertRevision(tx, agentId, revision, now()));
|
|
389
|
+
addRevision: async ({ agentId, ifCurrent, ...revision }) => {
|
|
390
|
+
await db.transaction((tx) => insertRevision(tx, agentId, revision, now(), ifCurrent));
|
|
386
391
|
},
|
|
387
392
|
setCurrent: async (agentId, revisionId) => {
|
|
388
393
|
const changed = await db.transaction((tx) => tx.runCounted("UPDATE agents SET current_revision = ? WHERE id = ? AND EXISTS (SELECT 1 FROM revisions WHERE id = ? AND agent_id = ?)", revisionId, agentId, revisionId, agentId));
|
|
@@ -706,7 +711,9 @@ class SqliteStore {
|
|
|
706
711
|
},
|
|
707
712
|
launch: (runId) => readLaunch(db, runId),
|
|
708
713
|
list: async ({ agentId, revisionId, state, archived, limit }) => {
|
|
709
|
-
|
|
714
|
+
// A built-in learning run (a run's grade and merge, queued with its kept learning) is no run of a person's: no list,
|
|
715
|
+
// count, stat or notice names it.
|
|
716
|
+
const where = [NOT_A_LEARNING_RUN];
|
|
710
717
|
const params = [];
|
|
711
718
|
if (agentId !== undefined)
|
|
712
719
|
(where.push("agent_id = ?"), params.push(agentId));
|
|
@@ -743,6 +750,11 @@ class SqliteStore {
|
|
|
743
750
|
const rows = await db.all(`SELECT id FROM runs WHERE ${claimable.sql} ORDER BY started_at, id${bounded ? " LIMIT ?" : ""}`, ...claimable.params, ...(bounded ? [limit] : []));
|
|
744
751
|
return rows.map((row) => row.id);
|
|
745
752
|
},
|
|
753
|
+
heldAt: async (runId) => {
|
|
754
|
+
const n = clock();
|
|
755
|
+
const row = await db.get(`SELECT epoch FROM runs WHERE id = ? AND runner IS NOT NULL AND claimed_until >= ${n.sql}`, runId, ...n.params);
|
|
756
|
+
return row === undefined ? undefined : Number(row.epoch);
|
|
757
|
+
},
|
|
746
758
|
claimMany: (runner, k, { leaseMs = DEFAULT_LEASE_MS } = {}) => this.#claimRuns(runner, leaseMs, { k }),
|
|
747
759
|
update: async (claim, patch, rows = {}) => {
|
|
748
760
|
if (Array.isArray(rows) || typeof rows !== "object" || rows === null)
|
|
@@ -965,6 +977,23 @@ class SqliteStore {
|
|
|
965
977
|
return row ? proposalOf(row) : undefined;
|
|
966
978
|
},
|
|
967
979
|
proposals: async (agentId, { limit = 100 } = {}) => (await db.all("SELECT run_id, agent_id, turn, proposal, learning_run_id, created_at FROM learning_proposals WHERE agent_id = ? ORDER BY created_at DESC, run_id DESC LIMIT ?", agentId, limit)).map(proposalOf),
|
|
980
|
+
spend: async ({ from, to }) => {
|
|
981
|
+
const rows = await db.all(`SELECT lp.agent_id, lp.proposal, lp.created_at, lp.learning_run_id, lr.state AS run_state, lr.ended_at AS run_ended_at, lr.cost_usd AS run_cost_usd
|
|
982
|
+
FROM learning_proposals lp
|
|
983
|
+
JOIN runs src ON src.id = lp.run_id
|
|
984
|
+
LEFT JOIN runs lr ON lr.id = lp.learning_run_id
|
|
985
|
+
WHERE src.imported_from IS NULL AND COALESCE(lr.ended_at, lp.created_at) >= ? AND COALESCE(lr.ended_at, lp.created_at) < ?
|
|
986
|
+
ORDER BY COALESCE(lr.ended_at, lp.created_at), lp.run_id`, from, to);
|
|
987
|
+
return rows.flatMap((row) => {
|
|
988
|
+
// A proposal: once its learning run ended, failed or was stopped, its row's cost is the grade's spend (a stop
|
|
989
|
+
// seen after a paid grade keeps it, learning-loop.ts).
|
|
990
|
+
if (row.learning_run_id !== null)
|
|
991
|
+
return row.run_state === "ended" || row.run_state === "failed" || row.run_state === "stopped" ? [{ agentId: row.agent_id, at: row.run_ended_at ?? row.created_at, costUsd: row.run_cost_usd }] : [];
|
|
992
|
+
// Kept with no learning run: dropped before it was proposed (nothing graded), unless it is still a proposal.
|
|
993
|
+
const kept = JSON.parse(row.proposal);
|
|
994
|
+
return kept?.state === "proposed" ? [] : [{ agentId: row.agent_id, at: row.created_at, costUsd: 0 }];
|
|
995
|
+
});
|
|
996
|
+
},
|
|
968
997
|
};
|
|
969
998
|
this.settings = {
|
|
970
999
|
get: async (key) => {
|
|
@@ -1135,8 +1164,8 @@ class SqliteStore {
|
|
|
1135
1164
|
const at = now();
|
|
1136
1165
|
const tomb = await tx.get("INSERT INTO sign_in_tombstones (agent_id, provider, pointer_sealed, forgotten_at) VALUES (?, ?, ?, ?) RETURNING agent_id, provider, pointer_sealed, forgotten_at", agentId, provider, live.pointer_sealed, at);
|
|
1137
1166
|
await tx.run("DELETE FROM sign_ins WHERE agent_id = ? AND provider = ?", agentId, provider);
|
|
1138
|
-
//
|
|
1139
|
-
|
|
1167
|
+
// Every tombstone is kept: what the agent learned is cut of each pointer it ever held (handed-values.ts
|
|
1168
|
+
// agentPointers), and a dropped one would be shown again.
|
|
1140
1169
|
return signInTombstoneOf(tomb);
|
|
1141
1170
|
}),
|
|
1142
1171
|
handedHolder: async (hash, options) => (await handedHolderIn(db, hash, { settleMs: options?.settleMs ?? 0, now: options?.at !== undefined ? { sql: d.bigintParam, params: [options.at] } : clock() }))?.id,
|
package/dist/store/store.d.ts
CHANGED
|
@@ -241,6 +241,20 @@ export interface Learning {
|
|
|
241
241
|
proposals(agentId: string, options?: {
|
|
242
242
|
limit?: number;
|
|
243
243
|
}): Promise<LearningProposal[]>;
|
|
244
|
+
/** What learning spent outside any run, in one read: each run (not imported) whose learning ended in [from, to) (ms),
|
|
245
|
+
* its agent, when its learning ended and what its grade cost (null: unknown; 0: nothing graded). A learning dropped
|
|
246
|
+
* before it was proposed ended when it was kept; a proposal ended when its learning run ended (ended or failed), whose
|
|
247
|
+
* row keeps the grade's spend as its cost. One still waiting for its grade is none of them. */
|
|
248
|
+
spend(window: {
|
|
249
|
+
from: number;
|
|
250
|
+
to: number;
|
|
251
|
+
}): Promise<LearningSpend[]>;
|
|
252
|
+
}
|
|
253
|
+
/** One run's learning end as GET /usage's learning meter counts it (Learning.spend). */
|
|
254
|
+
export interface LearningSpend {
|
|
255
|
+
readonly agentId: string;
|
|
256
|
+
readonly at: number;
|
|
257
|
+
readonly costUsd: number | null;
|
|
244
258
|
}
|
|
245
259
|
/** A command a journal commit takes, with the id of the record that is its effect, an id the effect's batch newly
|
|
246
260
|
* allocates (an entry, a created document, a new task): written only by the commit that newly writes that record, in its
|
|
@@ -452,9 +466,13 @@ export interface Agents {
|
|
|
452
466
|
id: string;
|
|
453
467
|
name: string;
|
|
454
468
|
}[]>;
|
|
455
|
-
/** Adds a revision and makes it the agent's current one, in one commit.
|
|
469
|
+
/** Adds a revision and makes it the agent's current one, in one commit. With `ifCurrent` (a revision id, or null for
|
|
470
|
+
* none), only while that is still the agent's current revision, compared in the same transaction under the agent row's
|
|
471
|
+
* lock: RevisionMoved, and nothing written, when another writer (this server's, or another server's on the same store)
|
|
472
|
+
* moved it first. */
|
|
456
473
|
addRevision(revision: NewRevision & {
|
|
457
474
|
agentId: string;
|
|
475
|
+
ifCurrent?: string | null;
|
|
458
476
|
}): Promise<void>;
|
|
459
477
|
/** Makes `revisionId`, one of the agent's own, its current one. StoreConstraint when it is not a revision of `agentId`. */
|
|
460
478
|
setCurrent(agentId: string, revisionId: string): Promise<void>;
|
|
@@ -615,7 +633,7 @@ export interface Runs {
|
|
|
615
633
|
* none (it was not started with one), never a default. */
|
|
616
634
|
launch(runId: string): Promise<RunLaunchRow>;
|
|
617
635
|
/** Newest first, at most `limit` (no limit: all). `archived` filters on the run's own archive time (true: archived,
|
|
618
|
-
* false: not); its agent's archive is read apart. */
|
|
636
|
+
* false: not); its agent's archive is read apart. A built-in learning run (one a kept learning names) is never listed. */
|
|
619
637
|
list(filter: {
|
|
620
638
|
agentId?: string;
|
|
621
639
|
revisionId?: string;
|
|
@@ -641,6 +659,10 @@ export interface Runs {
|
|
|
641
659
|
claimable(options?: {
|
|
642
660
|
limit?: number;
|
|
643
661
|
}): Promise<string[]>;
|
|
662
|
+
/** The epoch a runner's claim holds run `runId` at now, judged by the store's own clock (the one claims are stamped
|
|
663
|
+
* and fenced by), never a reader's; undefined when no claim holds it (none, released, or run out) or there is no
|
|
664
|
+
* such run. */
|
|
665
|
+
heldAt(runId: string): Promise<number | undefined>;
|
|
644
666
|
/** Claims up to `k` claimable runs for `runner`, oldest first, in one transaction: each at its own epoch + 1, with its
|
|
645
667
|
* `from`. A runner fills its free loops with one call. */
|
|
646
668
|
claimMany(runner: string, k: number, options?: {
|
|
@@ -946,15 +968,15 @@ export interface SignInRow {
|
|
|
946
968
|
readonly verifiedAt: number | null;
|
|
947
969
|
}
|
|
948
970
|
/** A forgotten sign-in's pointer, kept sealed byte for byte as its live row held it (so it still opens under the same
|
|
949
|
-
* binding), only so a server started later can cut it from what a run it was handed shows
|
|
971
|
+
* binding), only so a server started later can cut it from what a run it was handed shows and from what its agent
|
|
972
|
+
* learned. Every one is kept, never only the newest: a note may quote the agent's first pointer, and a shorter list
|
|
973
|
+
* would show it. */
|
|
950
974
|
export interface SignInTombstoneRow {
|
|
951
975
|
readonly agentId: string;
|
|
952
976
|
readonly provider: string;
|
|
953
977
|
readonly pointerSealed: Uint8Array;
|
|
954
978
|
readonly forgottenAt: number;
|
|
955
979
|
}
|
|
956
|
-
/** How many forgotten pointers an agent keeps per store, the oldest dropped first. */
|
|
957
|
-
export declare const SIGN_IN_TOMBSTONES_KEPT = 32;
|
|
958
980
|
export interface SignIns {
|
|
959
981
|
/** Saves the one live sign-in for the agent and provider, its pointer already sealed by the caller: inserted, or replaced
|
|
960
982
|
* in place when one is saved already (one per agent and provider). */
|
|
@@ -993,7 +1015,7 @@ export interface SignIns {
|
|
|
993
1015
|
/** Forgets the agent's live sign-in for the provider, in the one transaction a launch shares as its lock: when a running
|
|
994
1016
|
* run holds `hash` in its sign_in_handed (or, with `heldUnreadable`, when any running run was handed a sign-in at all),
|
|
995
1017
|
* the forget is refused (SignInHandedTo, naming the run) and nothing is written; otherwise the live row's sealed pointer
|
|
996
|
-
* moves to a tombstone (
|
|
1018
|
+
* moves to a tombstone (every one kept) and the live row goes. SignInMissing when the agent has
|
|
997
1019
|
* no live sign-in for the provider. */
|
|
998
1020
|
forget(agentId: string, provider: string, held: {
|
|
999
1021
|
hash?: string;
|
|
@@ -1245,6 +1267,13 @@ export declare class StoreConstraint extends Error {
|
|
|
1245
1267
|
};
|
|
1246
1268
|
});
|
|
1247
1269
|
}
|
|
1270
|
+
/** An agent's current revision is not the one a conditional write expected (addRevision's `ifCurrent`): another write
|
|
1271
|
+
* moved it first. `current` is the agent's current revision now. Nothing was written. */
|
|
1272
|
+
export declare class RevisionMoved extends Error {
|
|
1273
|
+
readonly agentId: string;
|
|
1274
|
+
readonly current: string | null;
|
|
1275
|
+
constructor(agentId: string, current: string | null);
|
|
1276
|
+
}
|
|
1248
1277
|
/** Forgetting a sign-in a running run was handed: refused, nothing written. `runId` is the run that holds it. */
|
|
1249
1278
|
export declare class SignInHandedTo extends Error {
|
|
1250
1279
|
readonly runId: string;
|
package/dist/store/store.js
CHANGED
|
@@ -48,8 +48,6 @@ export const STORE_WRITE_DEADLINE_MS = 10_000;
|
|
|
48
48
|
* slow commit is still landing a set whose token the provider already spent, and a waiter then takes over and refreshes
|
|
49
49
|
* with the spent one. Made from the two bounds so it cannot drift. */
|
|
50
50
|
export const MCP_REFRESH_LEASE_MS = MCP_REFRESH_CALL_MS + STORE_WRITE_DEADLINE_MS + 2_000;
|
|
51
|
-
/** How many forgotten pointers an agent keeps per store, the oldest dropped first. */
|
|
52
|
-
export const SIGN_IN_TOMBSTONES_KEPT = 32;
|
|
53
51
|
/** A write under a claim whose epoch is no longer the run's: refused, and nothing written. */
|
|
54
52
|
export class Fenced extends Error {
|
|
55
53
|
runId;
|
|
@@ -172,6 +170,18 @@ export class StoreConstraint extends Error {
|
|
|
172
170
|
this.unique = details.unique;
|
|
173
171
|
}
|
|
174
172
|
}
|
|
173
|
+
/** An agent's current revision is not the one a conditional write expected (addRevision's `ifCurrent`): another write
|
|
174
|
+
* moved it first. `current` is the agent's current revision now. Nothing was written. */
|
|
175
|
+
export class RevisionMoved extends Error {
|
|
176
|
+
agentId;
|
|
177
|
+
current;
|
|
178
|
+
constructor(agentId, current) {
|
|
179
|
+
super(`the current revision of agent ${agentId} moved (now ${current ?? "none"})`);
|
|
180
|
+
this.agentId = agentId;
|
|
181
|
+
this.current = current;
|
|
182
|
+
this.name = "RevisionMoved";
|
|
183
|
+
}
|
|
184
|
+
}
|
|
175
185
|
/** Forgetting a sign-in a running run was handed: refused, nothing written. `runId` is the run that holds it. */
|
|
176
186
|
export class SignInHandedTo extends Error {
|
|
177
187
|
runId;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Context } from "@earendil-works/chord";
|
|
2
2
|
import { type Meters } from "../cap.js";
|
|
3
3
|
import type { Store } from "../store/store.js";
|
|
4
|
-
/** What a run
|
|
4
|
+
/** What a run has spent, as the cap reads it, from the one store's journal: its model requests and its paid tool calls. */
|
|
5
5
|
export declare function spentOverStore(store: Store, runId: string, context: Context): Promise<number | null>;
|
|
6
6
|
/** /usage's meters for a run that ended, over the one store's journal (design 2.1): cap.ts meterFiguresFrom over A's
|
|
7
7
|
* entries, with the run's paid tool charges folded back from its paid-tools session (journal.ts withPaidCosts) as the
|
package/dist/store-reads/cap.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import { meterFiguresFrom, spentFrom } from "../cap.js";
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import { bookedCapSpend, bookedSpend, costIsBooked } from "../runner/metrics.js";
|
|
3
|
+
import { isCliRecord, runEntries, withPaidCosts } from "./journal.js";
|
|
4
|
+
/** What a run has spent, as the cap reads it, from the one store's journal: its model requests and its paid tool calls. */
|
|
4
5
|
export async function spentOverStore(store, runId, context) {
|
|
5
|
-
|
|
6
|
+
const entries = await runEntries(store, runId, context);
|
|
7
|
+
// A run whose record carries no cost of its own (a CLI harness's, a workflow's) reads what its sessions booked, the
|
|
8
|
+
// figure its cap holds it to (metrics.ts bookedCapSpend), its record's answers reading $0: 0 before any request, an
|
|
9
|
+
// unknown booked spend unknown (null).
|
|
10
|
+
if (costIsBooked(entries, isCliRecord(entries)))
|
|
11
|
+
return bookedCapSpend(entries);
|
|
12
|
+
// A pi run's paid tool calls (a browser's time, a search, a monid call) are booked on the run's one ledger, not its root:
|
|
13
|
+
// folded back as the charges the file-backed root held (withPaidCosts), as metersOverStore reads them, so a live run's
|
|
14
|
+
// listed spend counts them.
|
|
15
|
+
return spentFrom(withPaidCosts(entries));
|
|
6
16
|
}
|
|
7
17
|
/** /usage's meters for a run that ended, over the one store's journal (design 2.1): cap.ts meterFiguresFrom over A's
|
|
8
18
|
* entries, with the run's paid tool charges folded back from its paid-tools session (journal.ts withPaidCosts) as the
|
|
@@ -13,5 +23,13 @@ export async function metersOverStore(store, runId, context) {
|
|
|
13
23
|
const raw = await runEntries(store, runId, context);
|
|
14
24
|
if (!raw.length)
|
|
15
25
|
return { tools: 0, tokens: null, browser: { usd: 0, seconds: 0 }, others: 0, known: { tokens: 0, browser: 0, others: 0 } };
|
|
16
|
-
|
|
26
|
+
const figures = meterFiguresFrom(withPaidCosts(raw).map((entry) => ({ conversation: Number(entry.conversationId ?? 0), entry })));
|
|
27
|
+
// A run whose record carries no cost of its own (a CLI harness's, a workflow's): its tokens meter is its model requests
|
|
28
|
+
// at what they were booked at (metrics.ts bookedSpend, the one reader), never the $0 its record reads.
|
|
29
|
+
if (!costIsBooked(raw, isCliRecord(raw)))
|
|
30
|
+
return figures;
|
|
31
|
+
const model = bookedSpend(raw, "model");
|
|
32
|
+
if (!model.requests)
|
|
33
|
+
return figures;
|
|
34
|
+
return { ...figures, tokens: model.usd, known: { ...figures.known, tokens: model.known } };
|
|
17
35
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Context } from "@earendil-works/chord";
|
|
2
|
-
import type { EntryLike, TimelineEvent, TimelineOptions } from "
|
|
2
|
+
import type { EntryLike, TimelineEvent, TimelineOptions } from "../timeline/index.js";
|
|
3
3
|
import { Hearing } from "../heard.js";
|
|
4
|
-
import { type Reflect } from "../
|
|
4
|
+
import { type Reflect } from "../reflect-range.js";
|
|
5
5
|
import type { Store } from "../store/store.js";
|
|
6
6
|
/** A run's entries from the one store, in log order (by id across every conversation), as the old "SELECT record FROM
|
|
7
7
|
* entries ORDER BY id" read them for a settled run. A run with no journal reads as none. */
|
|
@@ -18,12 +18,15 @@ export declare function runReflect(store: Store, runId: string, context: Context
|
|
|
18
18
|
* given back unused shows nothing. Each charge is placed right after its call's result, joined by `call`, as the ledger
|
|
19
19
|
* wrote it there. A run with no paid-tools session reads unchanged. */
|
|
20
20
|
export declare function withPaidCosts(entries: readonly EntryLike[]): EntryLike[];
|
|
21
|
+
/** Whether `entries` are a CLI harness's record (its loop's calls and messages). */
|
|
22
|
+
export declare const isCliRecord: (entries: readonly EntryLike[]) => boolean;
|
|
21
23
|
/** A run's timeline rows, the renderer (timelineOf) unchanged, over the one store's entries, with a CLI harness's record
|
|
22
24
|
* rendered as its pi-shaped entries (piShaped) and its paid tool charges read back from the paid-tools session
|
|
23
|
-
* (withPaidCosts).
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* (
|
|
25
|
+
* (withPaidCosts). A run whose record carries no per-request cost (a CLI harness's, a workflow's: metrics.ts
|
|
26
|
+
* costIsBooked) has the usage row the renderer folds from its model turns put in its booking's words (bookedUsage): its
|
|
27
|
+
* model requests at what the run's model proxy booked them at, and its paid tool calls at what they charged, as a pi
|
|
28
|
+
* run's usage row counts them; its decisions' rows and cost come from their receipts (decision-rows.ts withDecisions),
|
|
29
|
+
* once. The end-of-run turn's rows are labelled as the server labels them (reflect-range.ts labelled). */
|
|
27
30
|
export declare function runTimeline(store: Store, runId: string, context: Context, options?: TimelineOptions): Promise<TimelineEvent[]>;
|
|
28
31
|
/** How a reader serves an answer whole: its redactor over the whole text (key-refusal.ts redactedAnswer with the run's
|
|
29
32
|
* values), and a key for those values (their digest), so an answer is read once per held value set. */
|
|
@@ -3,15 +3,18 @@
|
|
|
3
3
|
// its own database file ("SELECT record FROM entries ORDER BY id") and fed the same pure renderer; these read the one store instead.
|
|
4
4
|
// The renderer and the hearing keep their words; only where the entries come from changes.
|
|
5
5
|
import { createHash } from "node:crypto";
|
|
6
|
-
import { CAPS, timelineOf, TOOL_COST } from "
|
|
6
|
+
import { CAPS, costLabel, timelineOf, TOOL_COST } from "../timeline/index.js";
|
|
7
7
|
import { RUNNER_RECORDS } from "@parcha/agentrun-kernel";
|
|
8
8
|
import { TOOL_SESSION } from "@parcha/agentrun-kernel/kernel/cap";
|
|
9
9
|
import { Hearing } from "../heard.js";
|
|
10
10
|
import { redactedAnswer } from "../key-refusal.js";
|
|
11
|
-
import {
|
|
11
|
+
import { LEARNED_NODES, LEARNED_NODES_MAX } from "../learned-nodes.js";
|
|
12
|
+
import { labelled } from "../reflect-range.js";
|
|
12
13
|
import { reflectFrom } from "../runner/end-of-run.js";
|
|
13
14
|
import { journalEntries, journalRecords } from "../runner/journal-reader.js";
|
|
14
|
-
import { KINDS } from "../runner/journal-kinds.js";
|
|
15
|
+
import { endedAnswer, KINDS } from "../runner/journal-kinds.js";
|
|
16
|
+
import { bookedSpend, costIsBooked } from "../runner/metrics.js";
|
|
17
|
+
import { waitingText } from "../tool-gate.js";
|
|
15
18
|
/** A run's entries from the one store, in log order (by id across every conversation), as the old "SELECT record FROM
|
|
16
19
|
* entries ORDER BY id" read them for a settled run. A run with no journal reads as none. */
|
|
17
20
|
export async function runEntries(store, runId, context) {
|
|
@@ -22,7 +25,7 @@ export async function runEntries(store, runId, context) {
|
|
|
22
25
|
* before its input; `until`, the run's end entry once written. Undefined for a run that asked none. */
|
|
23
26
|
export async function runReflect(store, runId, context) {
|
|
24
27
|
const records = await journalRecords(store.readJournal(runId), context);
|
|
25
|
-
const from = reflectFrom(records.submissions ?? []);
|
|
28
|
+
const from = reflectFrom(records.submissions ?? [], records.entries);
|
|
26
29
|
if (from === undefined)
|
|
27
30
|
return undefined;
|
|
28
31
|
const end = records.entries.find((entry) => entry.kind === KINDS.end && Number(entry.id) >= from);
|
|
@@ -33,9 +36,41 @@ export async function runReflect(store, runId, context) {
|
|
|
33
36
|
* tool call its assistant tool-call and its result. So a claude-agent / codex / opencode run reads the same rows, words
|
|
34
37
|
* and final answer as a pi run of the same conversation. A run's own pi entries and its end-of-run turn (already model
|
|
35
38
|
* shaped) pass through unchanged, so a pi run is untouched. */
|
|
39
|
+
/** The calls the CLI record holds parked at an MCP door (a start marked parked, not in its tool bridge) with no sent mark
|
|
40
|
+
* and no end. */
|
|
41
|
+
function parkedUnsent(entries) {
|
|
42
|
+
const parked = new Set();
|
|
43
|
+
const past = new Set();
|
|
44
|
+
for (const entry of entries) {
|
|
45
|
+
const callId = entry.data?.callId;
|
|
46
|
+
if (typeof callId !== "string")
|
|
47
|
+
continue;
|
|
48
|
+
const start = entry.data;
|
|
49
|
+
if (entry.kind === KINDS.callStart && start.parked === true && start.bridge !== true)
|
|
50
|
+
parked.add(callId);
|
|
51
|
+
else if (entry.kind === KINDS.callSent || entry.kind === KINDS.callEnd)
|
|
52
|
+
past.add(callId);
|
|
53
|
+
}
|
|
54
|
+
for (const callId of past)
|
|
55
|
+
parked.delete(callId);
|
|
56
|
+
return parked;
|
|
57
|
+
}
|
|
36
58
|
function piShaped(entries) {
|
|
37
|
-
|
|
59
|
+
const waiting = parkedUnsent(entries);
|
|
60
|
+
return entries.flatMap((entry) => {
|
|
38
61
|
const data = (entry.data ?? {});
|
|
62
|
+
// A call through a CLI's own MCP client that its gate parked (adapter-loop.ts park): the CLI was answered the gate's
|
|
63
|
+
// waiting words (mcp-run.ts), which the record never keeps as the call's end, since an end is its outcome and the call
|
|
64
|
+
// is answered from it. While the call is parked and unsent, those words show as its result row from its ask on, as pi's
|
|
65
|
+
// parked call shows them; its sent mark or its end (run, refused, withdrawn) ends that.
|
|
66
|
+
// Once per call, at its first ask: a call asked again (a resume, a raise while it waits) shows one waiting result.
|
|
67
|
+
if (entry.kind === KINDS.ask && typeof data.callId === "string" && waiting.delete(data.callId)) {
|
|
68
|
+
const tool = typeof data.tool === "string" ? data.tool : "tool";
|
|
69
|
+
const timestamp = typeof data.requestedAt === "string" && Number.isFinite(Date.parse(data.requestedAt)) ? { timestamp: Date.parse(data.requestedAt) } : {};
|
|
70
|
+
// Its own id (the ask's, marked), so the renderer never takes it for the ask entry seen twice.
|
|
71
|
+
const result = { ...entry, id: `${String(entry.id)}:parked`, kind: "pi.tool-result", data: {}, model: [{ role: "tool", toolCallId: data.callId, content: waitingText(tool), ...timestamp }] };
|
|
72
|
+
return [entry, result];
|
|
73
|
+
}
|
|
39
74
|
// When the record's entry was written (`at`), as pi's own message carries its timestamp: the Timeline dates the row by it.
|
|
40
75
|
const timestamp = typeof data.at === "number" ? { timestamp: data.at } : {};
|
|
41
76
|
if (entry.kind === KINDS.message) {
|
|
@@ -46,13 +81,22 @@ function piShaped(entries) {
|
|
|
46
81
|
const stopReason = data.stopReason === "length" ? "length" : "stop";
|
|
47
82
|
return { ...entry, kind: "pi.assistant", model: [{ role: "assistant", stopReason, content: [{ type: "text", text }], ...(typeof data.outputTokens === "number" ? { usage: { output: data.outputTokens } } : {}), ...timestamp }] };
|
|
48
83
|
}
|
|
84
|
+
// The end-of-run turn's answer, as the CLI record keeps it (adapter-loop.ts answered): a model turn that ended, so the
|
|
85
|
+
// timeline shows it as pi's end-of-run turn shows its own (labelled then marks it the learning's).
|
|
86
|
+
if (entry.kind === KINDS.endOfRunTurn && data.role === "model") {
|
|
87
|
+
const text = typeof data.text === "string" ? data.text : "";
|
|
88
|
+
return { ...entry, kind: "pi.assistant", model: [{ role: "assistant", stopReason: "stop", content: [{ type: "text", text }] }] };
|
|
89
|
+
}
|
|
49
90
|
if (entry.kind === KINDS.callStart) {
|
|
50
91
|
return { ...entry, kind: "pi.assistant", model: [{ role: "assistant", stopReason: "tool_use", content: [{ type: "toolCall", id: String(data.callId ?? ""), name: String(data.tool ?? "tool"), arguments: data.input ?? {} }], ...timestamp }] };
|
|
51
92
|
}
|
|
52
93
|
if (entry.kind === KINDS.callEnd) {
|
|
53
|
-
// A call that did not finish done is an error result, as a failed pi tool call is; its output is the result's text.
|
|
94
|
+
// A call that did not finish done is an error result, as a failed pi tool call is; its output is the result's text. One
|
|
95
|
+
// a crash cut, written with no output, reads in the words its model was given (endedAnswer: the kernel's interrupted
|
|
96
|
+
// text), as pi's own interrupted result does, never an empty failure.
|
|
54
97
|
const failed = data.status !== undefined && data.status !== "done";
|
|
55
|
-
|
|
98
|
+
const content = data.output === undefined && data.status === "interrupted" ? endedAnswer("", { status: "interrupted" }).text : (data.output ?? "");
|
|
99
|
+
return { ...entry, kind: "pi.tool-result", model: [{ role: "tool", toolCallId: String(data.callId ?? ""), content, ...(failed ? { isError: true } : {}), ...timestamp }] };
|
|
56
100
|
}
|
|
57
101
|
return entry;
|
|
58
102
|
});
|
|
@@ -71,13 +115,15 @@ export function withPaidCosts(entries) {
|
|
|
71
115
|
if (typeof conv !== "number")
|
|
72
116
|
return [...entries];
|
|
73
117
|
// Each paid request's tool and the call it ran under, by its turn; then each turn's cost, paired by turn.
|
|
118
|
+
// Its tool: the one the request names (`tool`: a browser step's "browser", which the renderer folds into its call's row
|
|
119
|
+
// with its session `tag`, as main's browser charge), else its paid name.
|
|
74
120
|
const named = new Map();
|
|
75
121
|
for (const e of entries) {
|
|
76
122
|
if (e.conversationId !== conv || e.kind !== RUNNER_RECORDS.request)
|
|
77
123
|
continue;
|
|
78
124
|
const d = (e.data ?? {});
|
|
79
125
|
if (typeof d.turn === "number" && typeof d.model === "string" && d.model.startsWith("paid:"))
|
|
80
|
-
named.set(d.turn, { tool: d.model.slice("paid:".length), ...(typeof d.call === "string" ? { call: d.call } : {}), ...(typeof d.charged_by === "string" && d.charged_by ? { chargedBy: d.charged_by } : {}) });
|
|
126
|
+
named.set(d.turn, { tool: typeof d.tool === "string" && d.tool ? d.tool : d.model.slice("paid:".length), ...(typeof d.call === "string" ? { call: d.call } : {}), ...(typeof d.charged_by === "string" && d.charged_by ? { chargedBy: d.charged_by } : {}), ...(typeof d.tag === "string" && d.tag ? { tag: d.tag } : {}) });
|
|
81
127
|
}
|
|
82
128
|
const charges = [];
|
|
83
129
|
for (const e of entries) {
|
|
@@ -90,7 +136,7 @@ export function withPaidCosts(entries) {
|
|
|
90
136
|
// is unknown (null) is a charge of an unknown amount (main's TOOL_COST with no usd).
|
|
91
137
|
if (!req || d.givenBack === true)
|
|
92
138
|
continue;
|
|
93
|
-
charges.push({ ...req, usd: typeof d.usd === "number" && Number.isFinite(d.usd) && d.usd >= 0 ? d.usd : null });
|
|
139
|
+
charges.push({ ...req, turn: d.turn, usd: typeof d.usd === "number" && Number.isFinite(d.usd) && d.usd >= 0 ? d.usd : null });
|
|
94
140
|
}
|
|
95
141
|
if (!charges.length)
|
|
96
142
|
return [...entries];
|
|
@@ -100,23 +146,88 @@ export function withPaidCosts(entries) {
|
|
|
100
146
|
// MCP server, a browser vendor).
|
|
101
147
|
const by = charge.chargedBy;
|
|
102
148
|
const at = charge.call === undefined ? -1 : out.findIndex((e) => e.kind === "pi.tool-result" && String(e.model?.[0]?.toolCallId ?? "") === charge.call);
|
|
103
|
-
const data = { tool: charge.tool, ...(by ? { charged_by: by } : {}), ...(charge.call ? { call: charge.call } : {}), ...(charge.usd !== null ? { usd: charge.usd } : {}) };
|
|
104
|
-
const entry = { conversationId: at >= 0 ? out[at].conversationId : conv, kind: TOOL_COST, data, id: `paid:${charge.call ?? charge.tool}` };
|
|
149
|
+
const data = { tool: charge.tool, ...(by ? { charged_by: by } : {}), ...(charge.call ? { call: charge.call } : {}), ...(charge.tag ? { tag: charge.tag } : {}), ...(charge.usd !== null ? { usd: charge.usd } : {}) };
|
|
150
|
+
const entry = { conversationId: at >= 0 ? out[at].conversationId : conv, kind: TOOL_COST, data, id: `paid:${charge.call ?? charge.tool}#${charge.turn}` };
|
|
105
151
|
out.splice(at >= 0 ? at + 1 : out.length, 0, entry);
|
|
106
152
|
}
|
|
107
153
|
return out;
|
|
108
154
|
}
|
|
155
|
+
/** Whether `entries` are a CLI harness's record (its loop's calls and messages). */
|
|
156
|
+
export const isCliRecord = (entries) => entries.some((entry) => entry.kind === KINDS.message || entry.kind === KINDS.callStart || entry.kind === KINDS.callEnd);
|
|
157
|
+
/** A path as a run's rows name it, for a harness working in `cwd`: one under `cwd` by its name there (`hello.md`), any
|
|
158
|
+
* other as given. */
|
|
159
|
+
const inWorkspace = (cwd) => (path) => {
|
|
160
|
+
const root = cwd.replace(/\/+$/, "");
|
|
161
|
+
return path.startsWith(`${root}/`) && path.length > root.length + 1 ? path.slice(root.length + 1) : path;
|
|
162
|
+
};
|
|
109
163
|
/** A run's timeline rows, the renderer (timelineOf) unchanged, over the one store's entries, with a CLI harness's record
|
|
110
164
|
* rendered as its pi-shaped entries (piShaped) and its paid tool charges read back from the paid-tools session
|
|
111
|
-
* (withPaidCosts).
|
|
112
|
-
*
|
|
113
|
-
*
|
|
114
|
-
* (
|
|
165
|
+
* (withPaidCosts). A run whose record carries no per-request cost (a CLI harness's, a workflow's: metrics.ts
|
|
166
|
+
* costIsBooked) has the usage row the renderer folds from its model turns put in its booking's words (bookedUsage): its
|
|
167
|
+
* model requests at what the run's model proxy booked them at, and its paid tool calls at what they charged, as a pi
|
|
168
|
+
* run's usage row counts them; its decisions' rows and cost come from their receipts (decision-rows.ts withDecisions),
|
|
169
|
+
* once. The end-of-run turn's rows are labelled as the server labels them (reflect-range.ts labelled). */
|
|
115
170
|
export async function runTimeline(store, runId, context, options = {}) {
|
|
116
171
|
const entries = await runEntries(store, runId, context);
|
|
117
|
-
const cli = entries
|
|
118
|
-
|
|
119
|
-
|
|
172
|
+
const cli = isCliRecord(entries);
|
|
173
|
+
// A CLI names a file by its absolute path under its working directory (CallStartData.cwd): the rows name it by its
|
|
174
|
+
// name in the workspace, never a path on the runner's machine. Its arguments, as the harness sent them, stay.
|
|
175
|
+
const cwd = cli && !options.path ? entries.find((entry) => entry.kind === KINDS.callStart && typeof entry.data?.cwd === "string")?.data : undefined;
|
|
176
|
+
const rendered = timelineOf(withPaidCosts(cli ? piShaped(entries) : entries), cwd ? { ...options, path: inWorkspace(cwd.cwd) } : options);
|
|
177
|
+
const rows = costIsBooked(entries, cli) ? bookedUsage(rendered, entries, options) : rendered;
|
|
178
|
+
return labelled((await runReflect(store, runId, context)), withLearnedNodes(await datedRows(store, runId, rows), entries, options.redact));
|
|
179
|
+
}
|
|
180
|
+
/** `rows` with each learned_workflow call's result row carrying the nodes its workflow ran (`learned_nodes`), from the
|
|
181
|
+
* LEARNED_NODES entry the call kept beside itself (learned-workflow.ts), joined by the call's id: each label through
|
|
182
|
+
* `redact` when given (the run's cut), as every text a row shows; never a node's error text. Rows of a run that ran none are as they were. */
|
|
183
|
+
function withLearnedNodes(rows, entries, redact) {
|
|
184
|
+
const traces = new Map();
|
|
185
|
+
const said = (text) => (redact ? redact(text) : text);
|
|
186
|
+
for (const entry of entries) {
|
|
187
|
+
if (entry.kind !== LEARNED_NODES)
|
|
188
|
+
continue;
|
|
189
|
+
const data = (entry.data ?? {});
|
|
190
|
+
if (typeof data.call !== "string" || !Array.isArray(data.nodes))
|
|
191
|
+
continue;
|
|
192
|
+
traces.set(data.call, data.nodes.slice(0, LEARNED_NODES_MAX).flatMap((node) => {
|
|
193
|
+
const one = (node ?? {});
|
|
194
|
+
if (typeof one.label !== "string" || typeof one.status !== "string")
|
|
195
|
+
return [];
|
|
196
|
+
return [{ label: said(one.label), kind: String(one.kind ?? ""), status: one.status, duration_ms: typeof one.duration_ms === "number" ? one.duration_ms : 0 }];
|
|
197
|
+
}));
|
|
198
|
+
}
|
|
199
|
+
if (!traces.size)
|
|
200
|
+
return rows;
|
|
201
|
+
return rows.map((row) => (row.kind === "tool_result" && row.tool_use_id && traces.has(row.tool_use_id) ? { ...row, learned_nodes: traces.get(row.tool_use_id) } : row));
|
|
202
|
+
}
|
|
203
|
+
/** A CLI or workflow run's `rows` with its usage row in its booking's words (runner/metrics.ts bookedSpend over every
|
|
204
|
+
* session of the run but its decisions'): its model requests and its paid tool calls, their sum (unknown while one's cost is, with what is known so far,
|
|
205
|
+
* costsofar/1), in the renderer's words (`Cost` once ended, else `Cost so far`). The renderer's own row is replaced in
|
|
206
|
+
* place; a run that booked a request and rendered none gets one at the end. One that booked none keeps its rows, with no
|
|
207
|
+
* usage row. */
|
|
208
|
+
function bookedUsage(rows, entries, options) {
|
|
209
|
+
const model = bookedSpend(entries, "model");
|
|
210
|
+
const paid = bookedSpend(entries, "paid");
|
|
211
|
+
const at = rows.findIndex((row) => row.kind === "usage");
|
|
212
|
+
const kept = rows.filter((row) => row.kind !== "usage");
|
|
213
|
+
if (!model.requests && !paid.requests)
|
|
214
|
+
return kept;
|
|
215
|
+
const usd = model.usd === null || paid.usd === null ? null : model.usd + paid.usd;
|
|
216
|
+
const unpriced = model.unpriced + paid.unpriced;
|
|
217
|
+
const dollars = (value) => (value === 0 ? "$0" : `$${value.toFixed(4)}`);
|
|
218
|
+
const spent = usd !== null ? dollars(usd) : `${dollars(model.known + paid.known)} known, ${unpriced} not priced,`;
|
|
219
|
+
const charges = paid.requests ? ` and ${paid.requests} paid tool call${paid.requests === 1 ? "" : "s"}` : "";
|
|
220
|
+
const summary = `${costLabel(options.ended === true)}: ${spent} over ${model.requests} model request${model.requests === 1 ? "" : "s"}${charges}`;
|
|
221
|
+
const usage = { usd, requests: model.requests, ...(usd === null ? { so_far_usd: model.known + paid.known, unpriced } : {}) };
|
|
222
|
+
const last = rows[rows.length - 1];
|
|
223
|
+
const base = at >= 0 ? rows[at] : {
|
|
224
|
+
...last, id: `${last?.conversation_id ?? 0}:usage`, agent_label: null, text: null,
|
|
225
|
+
tool_name: null, tool_use_id: null, tool_status: null, tool_input_preview: null, tool_result_preview: null, tool_result_size_bytes: null,
|
|
226
|
+
};
|
|
227
|
+
const row = { ...base, kind: "usage", summary, usage };
|
|
228
|
+
const out = [...kept];
|
|
229
|
+
out.splice(at >= 0 ? at : out.length, 0, row);
|
|
230
|
+
return out;
|
|
120
231
|
}
|
|
121
232
|
/** `rows` with none dated 1970: a row the renderer found no time for (an entry of a CLI record written before its entries
|
|
122
233
|
* carried `at`; the store's entry rows keep no write time of their own) reads the run's start, the earliest time it can
|