@parcha/agentrun-serve 0.1.0-beta.36 → 0.1.0-beta.38
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 +452 -0
- package/README.md +311 -142
- package/dist/agent-rows.d.ts +1 -1
- package/dist/agents-store.d.ts +35 -3
- package/dist/agents-store.js +39 -14
- package/dist/answer-json.d.ts +34 -1
- package/dist/answer-json.js +91 -9
- package/dist/api-only.d.ts +1 -1
- package/dist/api-only.js +2 -2
- package/dist/attach-conformance-cli.d.ts +1 -0
- package/dist/attach-conformance-cli.js +18 -0
- package/dist/attach-conformance.d.ts +25 -0
- package/dist/attach-conformance.js +343 -0
- package/dist/browser-cost.d.ts +1 -1
- package/dist/browser-cost.js +52 -93
- package/dist/browser-live.d.ts +20 -8
- package/dist/browser-live.js +50 -27
- package/dist/cap.d.ts +0 -407
- package/dist/cap.js +4 -1028
- package/dist/catalog.d.ts +1 -12
- package/dist/catalog.js +4 -34
- package/dist/cli.js +13 -4
- package/dist/composio-preset.d.ts +53 -0
- package/dist/composio-preset.js +221 -0
- package/dist/computer-attach.d.ts +125 -0
- package/dist/computer-attach.js +330 -0
- package/dist/computer-calls.d.ts +60 -0
- package/dist/computer-calls.js +209 -0
- package/dist/computer-channel.d.ts +57 -0
- package/dist/computer-channel.js +115 -0
- package/dist/computer-commands.d.ts +1 -1
- package/dist/computer-held-routes.d.ts +35 -0
- package/dist/computer-held-routes.js +106 -0
- package/dist/computer-held.d.ts +48 -0
- package/dist/computer-held.js +87 -0
- package/dist/computer-keys.d.ts +21 -1
- package/dist/computer-keys.js +53 -11
- package/dist/computer-live.d.ts +165 -0
- package/dist/computer-live.js +655 -0
- package/dist/computer-setting.d.ts +15 -2
- package/dist/computer-setting.js +20 -4
- package/dist/computer-view.d.ts +14 -0
- package/dist/computer-view.js +51 -3
- package/dist/computer-viewer.d.ts +6 -0
- package/dist/computer-viewer.js +311 -0
- package/dist/computers.d.ts +37 -8
- package/dist/computers.js +62 -14
- package/dist/console/assets/index-CjqQirbo.css +1 -0
- package/dist/console/assets/index-b6-XY5A9.js +576 -0
- package/dist/console/index.html +2 -2
- package/dist/console-links.d.ts +2 -0
- package/dist/console-links.js +3 -0
- package/dist/decisions.js +4 -0
- package/dist/faux-computers.d.ts +9 -20
- package/dist/faux-computers.js +56 -26
- package/dist/github-connection.d.ts +10 -2
- package/dist/github-connection.js +65 -2
- package/dist/github-faux.js +1 -1
- package/dist/graph-view.d.ts +1 -1
- package/dist/harness-opencode/adapter.js +7 -3
- package/dist/harness-opencode/facts.js +0 -2
- package/dist/harness-opencode/host/policy.d.ts +2 -5
- package/dist/harness-opencode/host/policy.js +8 -32
- package/dist/harness-table.d.ts +0 -2
- package/dist/harness-table.js +0 -2
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +35 -1
- package/dist/host-tools.d.ts +2 -6
- package/dist/host-tools.js +7 -10
- package/dist/hosted-search.d.ts +5 -6
- package/dist/hosted-search.js +2 -28
- package/dist/import-sources.d.ts +10 -0
- package/dist/import-sources.js +44 -2
- package/dist/import.d.ts +3 -3
- package/dist/import.js +37 -6
- package/dist/key-words.d.ts +3 -2
- package/dist/key-words.js +4 -2
- package/dist/learn/check.d.ts +10 -2
- package/dist/learn/check.js +13 -5
- package/dist/learn/commit.d.ts +14 -2
- package/dist/learn/commit.js +23 -9
- package/dist/learn/compile.d.ts +243 -0
- package/dist/learn/compile.js +471 -0
- package/dist/learn/folder.d.ts +6 -0
- package/dist/learn/folder.js +4 -1
- package/dist/learn/general.d.ts +69 -0
- package/dist/learn/general.js +75 -0
- package/dist/learn/grade.d.ts +5 -1
- package/dist/learn/grade.js +29 -7
- package/dist/learn/notes.d.ts +8 -0
- package/dist/learn/notes.js +18 -0
- package/dist/learn/promote.d.ts +253 -0
- package/dist/learn/promote.js +428 -0
- package/dist/learn/refit.d.ts +39 -0
- package/dist/learn/refit.js +236 -0
- package/dist/learn/reflect.d.ts +9 -20
- package/dist/learn/reflect.js +18 -125
- package/dist/learn/replay-calls.d.ts +63 -0
- package/dist/learn/replay-calls.js +158 -0
- package/dist/learn/skills.d.ts +50 -14
- package/dist/learn/skills.js +183 -47
- package/dist/learn/workflow-nodes.d.ts +15 -0
- package/dist/learn/workflow-nodes.js +44 -0
- package/dist/learned-nodes.d.ts +50 -2
- package/dist/learned-nodes.js +41 -2
- package/dist/learned-workflow.d.ts +43 -3
- package/dist/learned-workflow.js +170 -28
- package/dist/learning-batch.d.ts +283 -0
- package/dist/learning-batch.js +574 -0
- package/dist/learning-cost.d.ts +18 -6
- package/dist/learning-cost.js +15 -7
- package/dist/learning-evals.d.ts +33 -0
- package/dist/learning-evals.js +98 -0
- package/dist/learning-routes.d.ts +37 -2
- package/dist/learning-routes.js +52 -8
- package/dist/learning-words.d.ts +13 -0
- package/dist/learning-words.js +13 -0
- package/dist/learning.d.ts +55 -17
- package/dist/learning.js +91 -48
- package/dist/live/stream.js +6 -3
- package/dist/live/translate.d.ts +4 -3
- package/dist/live/translate.js +8 -4
- package/dist/live-reach.d.ts +37 -0
- package/dist/live-reach.js +62 -0
- package/dist/llm.js +0 -3
- package/dist/mcp-gateway.d.ts +21 -20
- package/dist/mcp-gateway.js +76 -55
- package/dist/mcp-oauth.d.ts +62 -7
- package/dist/mcp-oauth.js +134 -19
- package/dist/mcp-pin-first.d.ts +37 -0
- package/dist/mcp-pin-first.js +107 -0
- package/dist/mcp-private-hosts.d.ts +4 -4
- package/dist/mcp-private-hosts.js +20 -12
- package/dist/mcp-probe.d.ts +27 -11
- package/dist/mcp-probe.js +60 -42
- package/dist/mcp-secret-writes.js +4 -0
- package/dist/mcp-servers-store.d.ts +115 -25
- package/dist/mcp-servers-store.js +241 -43
- package/dist/mcp-sources.d.ts +39 -0
- package/dist/mcp-sources.js +74 -0
- package/dist/mcp-store.d.ts +21 -4
- package/dist/mcp-store.js +33 -8
- package/dist/mcp-wire.d.ts +22 -0
- package/dist/mcp-wire.js +36 -0
- package/dist/mcp.js +3 -3
- package/dist/meta-tool-names.d.ts +1 -0
- package/dist/meta-tool-names.js +3 -0
- package/dist/model-form.d.ts +26 -0
- package/dist/model-form.js +80 -0
- package/dist/model.d.ts +13 -0
- package/dist/model.js +69 -25
- package/dist/monid.d.ts +51 -25
- package/dist/monid.js +226 -140
- package/dist/openapi.js +694 -59
- package/dist/page.d.ts +18 -0
- package/dist/page.js +48 -20
- package/dist/paid-calls.d.ts +26 -29
- package/dist/paid-calls.js +46 -102
- package/dist/place-offers.d.ts +2 -3
- package/dist/place-offers.js +3 -7
- package/dist/place.js +1 -2
- package/dist/print.d.ts +15 -1
- package/dist/print.js +14 -3
- package/dist/propose.d.ts +10 -1
- package/dist/propose.js +30 -10
- package/dist/provider.js +12 -4
- package/dist/reflect-range.d.ts +8 -1
- package/dist/reflect-range.js +3 -2
- package/dist/rows.d.ts +5 -3
- package/dist/rows.js +15 -4
- package/dist/run-launch.d.ts +27 -2
- package/dist/run-launch.js +15 -4
- package/dist/run-learning-served.d.ts +13 -2
- package/dist/run-manifest.d.ts +5 -0
- package/dist/run-manifest.js +20 -0
- package/dist/run-reads-over-store.d.ts +1 -1
- package/dist/run-repo.d.ts +20 -0
- package/dist/run-repo.js +63 -0
- package/dist/run-secrets.js +2 -3
- package/dist/run-timeline-served.d.ts +1 -1
- package/dist/run-timeline-served.js +5 -2
- package/dist/run-view.d.ts +19 -8
- package/dist/run-view.js +62 -26
- package/dist/runnable.d.ts +0 -3
- package/dist/runnable.js +37 -29
- package/dist/runner/adapter-loop.d.ts +47 -17
- package/dist/runner/adapter-loop.js +1427 -303
- package/dist/runner/adapter-run-spec.d.ts +23 -9
- package/dist/runner/adapter-run-spec.js +162 -56
- package/dist/runner/adapters/pi-computer.d.ts +9 -0
- package/dist/runner/adapters/pi-computer.js +51 -0
- package/dist/runner/adapters/pi.d.ts +25 -11
- package/dist/runner/adapters/pi.js +168 -45
- package/dist/runner/beside.d.ts +7 -3
- package/dist/runner/beside.js +21 -11
- package/dist/runner/claim-secrets.d.ts +4 -0
- package/dist/runner/claim-secrets.js +6 -1
- package/dist/runner/cli-run-spec.d.ts +20 -3
- package/dist/runner/cli-run-spec.js +174 -64
- package/dist/runner/commands.d.ts +3 -1
- package/dist/runner/commands.js +28 -3
- package/dist/runner/computer-channel.d.ts +34 -0
- package/dist/runner/computer-channel.js +62 -0
- package/dist/runner/conformance/harness.d.ts +3 -0
- package/dist/runner/conformance/harness.js +224 -17
- package/dist/runner/conformance/subject.d.ts +12 -5
- package/dist/runner/dispatch.js +3 -0
- package/dist/runner/end-of-run.d.ts +23 -12
- package/dist/runner/end-of-run.js +41 -16
- package/dist/runner/feedback-round.d.ts +52 -0
- package/dist/runner/feedback-round.js +79 -0
- package/dist/runner/github-app.d.ts +2 -1
- package/dist/runner/github-app.js +4 -2
- package/dist/runner/harness-loops.js +15 -1
- package/dist/runner/held-run.d.ts +65 -0
- package/dist/runner/held-run.js +158 -0
- package/dist/runner/journal-kinds.d.ts +44 -7
- package/dist/runner/journal-kinds.js +29 -0
- package/dist/runner/journal-reader.d.ts +5 -4
- package/dist/runner/journal-reader.js +12 -10
- package/dist/runner/learning-ask.d.ts +7 -1
- package/dist/runner/learning-ask.js +21 -6
- package/dist/runner/learning-dispatch.d.ts +22 -0
- package/dist/runner/learning-dispatch.js +77 -10
- package/dist/runner/learning-end.d.ts +69 -13
- package/dist/runner/learning-end.js +340 -87
- package/dist/runner/learning-factory.d.ts +1 -1
- package/dist/runner/learning-factory.js +11 -5
- package/dist/runner/learning-loop.d.ts +86 -8
- package/dist/runner/learning-loop.js +178 -23
- package/dist/runner/learning-seed.d.ts +11 -0
- package/dist/runner/learning-seed.js +27 -0
- package/dist/runner/live-ticker.js +1 -0
- package/dist/runner/loop-contract.d.ts +26 -3
- package/dist/runner/loop.d.ts +21 -2
- package/dist/runner/loop.js +23 -2
- package/dist/runner/main.d.ts +13 -1
- package/dist/runner/main.js +27 -5
- package/dist/runner/mcp-claim.d.ts +15 -8
- package/dist/runner/mcp-claim.js +176 -71
- package/dist/runner/mcp-github.js +1 -1
- package/dist/runner/mcp-run.d.ts +21 -57
- package/dist/runner/mcp-run.js +25 -85
- package/dist/runner/meta-tools.d.ts +37 -0
- package/dist/runner/meta-tools.js +268 -0
- package/dist/runner/metrics.d.ts +11 -2
- package/dist/runner/metrics.js +26 -5
- package/dist/runner/model-route.js +12 -3
- package/dist/runner/monid-run.d.ts +16 -0
- package/dist/runner/monid-run.js +96 -0
- package/dist/runner/pr-checkout.d.ts +1 -0
- package/dist/runner/pr-checkout.js +1 -1
- package/dist/runner/promotion.d.ts +80 -0
- package/dist/runner/promotion.js +562 -0
- package/dist/runner/reflect-hook.d.ts +46 -13
- package/dist/runner/reflect-hook.js +149 -48
- package/dist/runner/runner.d.ts +26 -0
- package/dist/runner/runner.js +208 -17
- package/dist/runner/scratch-sweep.d.ts +13 -0
- package/dist/runner/scratch-sweep.js +62 -0
- package/dist/runner/sessions.d.ts +9 -4
- package/dist/runner/sessions.js +5 -1
- package/dist/runner/step-guard.d.ts +62 -0
- package/dist/runner/step-guard.js +93 -0
- package/dist/runner/store-port.d.ts +4 -2
- package/dist/runner/store-secrets.js +4 -3
- package/dist/runner/subagent.d.ts +0 -3
- package/dist/runner/subagent.js +10 -13
- package/dist/runner/workflow-first.d.ts +216 -0
- package/dist/runner/workflow-first.js +303 -0
- package/dist/runner/workflow-host.d.ts +6 -1
- package/dist/runner/workflow-host.js +1 -1
- package/dist/runner/workflow-loop.d.ts +1 -1
- package/dist/runner/workflow-loop.js +17 -10
- package/dist/runner/workflow-spec.d.ts +15 -2
- package/dist/runner/workflow-spec.js +63 -23
- package/dist/runs-over-store.d.ts +17 -3
- package/dist/runs-over-store.js +59 -20
- package/dist/seal-key.js +3 -3
- package/dist/sealed-settings-store.d.ts +5 -2
- package/dist/sealed-settings-store.js +10 -4
- package/dist/secret-writes.d.ts +5 -4
- package/dist/secret-writes.js +5 -4
- package/dist/server-secrets-store.d.ts +4 -3
- package/dist/server-secrets-store.js +15 -9
- package/dist/server-secrets.js +3 -2
- package/dist/server.d.ts +119 -18
- package/dist/server.js +841 -202
- package/dist/settings-wake.d.ts +23 -0
- package/dist/settings-wake.js +65 -0
- package/dist/standard-webhooks.d.ts +4 -1
- package/dist/standard-webhooks.js +8 -4
- package/dist/store/agent-body.d.ts +14 -3
- package/dist/store/agent-body.js +14 -10
- package/dist/store/command-wake.d.ts +13 -0
- package/dist/store/command-wake.js +37 -0
- package/dist/store/dialect.d.ts +6 -0
- package/dist/store/dialect.js +5 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/journal/storage.d.ts +3 -2
- package/dist/store/journal/storage.js +13 -5
- package/dist/store/postgres/connection.js +3 -1
- package/dist/store/postgres/dialect.js +4 -0
- package/dist/store/postgres/migrations/0005_learning_rounds.sql +14 -0
- package/dist/store/postgres/migrations/0006_run_changes_channel_attempts.sql +57 -0
- package/dist/store/postgres/migrations/0007_viewer_host.sql +11 -0
- package/dist/store/postgres/migrations/0008_paired_computers.sql +30 -0
- package/dist/store/postgres/migrations/0009_commands_answer.sql +10 -0
- package/dist/store/postgres/migrations/0010_imported_templates.sql +18 -0
- package/dist/store/postgres/migrations/0011_revision_mcp_pin.sql +13 -0
- package/dist/store/schema.d.ts +10 -1
- package/dist/store/schema.js +104 -15
- package/dist/store/sqlite/connection.d.ts +2 -2
- package/dist/store/sqlite/connection.js +2 -2
- package/dist/store/sqlite/run-patch.d.ts +7 -5
- package/dist/store/sqlite/run-patch.js +45 -18
- package/dist/store/sqlite/store.js +331 -72
- package/dist/store/store.d.ts +312 -21
- package/dist/store/store.js +12 -1
- package/dist/store-reads/cap.d.ts +1 -1
- package/dist/store-reads/cap.js +8 -1
- package/dist/store-reads/decisions.d.ts +2 -2
- package/dist/store-reads/decisions.js +1 -1
- package/dist/store-reads/import-summary.d.ts +1 -1
- package/dist/store-reads/journal.d.ts +30 -5
- package/dist/store-reads/journal.js +128 -56
- package/dist/store-reads/learning.d.ts +114 -12
- package/dist/store-reads/learning.js +238 -48
- package/dist/store-reads/loop.d.ts +61 -0
- package/dist/store-reads/loop.js +111 -0
- package/dist/store-reads/runs.d.ts +1 -1
- package/dist/store-reads/workflow.d.ts +1 -1
- package/dist/store-view.d.ts +6 -0
- package/dist/store-view.js +59 -26
- package/dist/template-eval.d.ts +18 -9
- package/dist/template-eval.js +95 -68
- package/dist/template-github-permissions.d.ts +24 -10
- package/dist/template-github-permissions.js +36 -15
- package/dist/template-import.d.ts +31 -0
- package/dist/template-import.js +121 -0
- package/dist/template-source.d.ts +51 -0
- package/dist/template-source.js +371 -0
- package/dist/templates.d.ts +218 -46
- package/dist/templates.js +493 -181
- package/dist/timeline/computer.d.ts +48 -0
- package/dist/timeline/computer.js +169 -0
- package/dist/timeline/index.d.ts +4 -3
- package/dist/timeline/index.js +4 -3
- package/dist/timeline/labels.d.ts +6 -2
- package/dist/timeline/labels.js +93 -2
- package/dist/timeline/origin.d.ts +19 -5
- package/dist/timeline/origin.js +11 -4
- package/dist/timeline/timeline.d.ts +22 -1
- package/dist/timeline/timeline.js +34 -18
- package/dist/timeline/util.d.ts +4 -0
- package/dist/timeline/util.js +14 -0
- package/dist/tool-gate.d.ts +84 -50
- package/dist/tool-gate.js +179 -101
- package/dist/tool-keys.d.ts +4 -1
- package/dist/tool-keys.js +8 -3
- package/dist/tool-list.d.ts +26 -0
- package/dist/tool-list.js +43 -0
- package/dist/tool-names.d.ts +71 -0
- package/dist/tool-names.js +99 -0
- package/dist/tool-plane.d.ts +33 -27
- package/dist/tool-plane.js +72 -39
- package/dist/tools.d.ts +32 -7
- package/dist/tools.js +37 -24
- package/dist/trigger-adapter.d.ts +4 -0
- package/dist/trigger-secrets-shown.d.ts +2 -2
- package/dist/trigger-secrets-shown.js +1 -1
- package/dist/trigger-signatures.d.ts +25 -6
- package/dist/trigger-signatures.js +41 -7
- package/dist/trigger-targets.d.ts +2 -0
- package/dist/trigger-targets.js +1 -0
- package/dist/trigger-webhook.js +5 -2
- package/dist/triggers.d.ts +7 -3
- package/dist/triggers.js +35 -5
- package/dist/turns.js +16 -1
- package/dist/validate.d.ts +15 -7
- package/dist/validate.js +102 -45
- package/dist/web-fetch.d.ts +2 -8
- package/dist/web-fetch.js +2 -22
- package/dist/web-search.d.ts +10 -5
- package/dist/web-search.js +7 -6
- package/dist/webhooks.d.ts +145 -102
- package/dist/webhooks.js +559 -322
- package/dist/workflow/decisions.d.ts +1 -1
- package/dist/workflow/node-runner.d.ts +2 -1
- package/dist/workflow/node.d.ts +2 -1
- package/dist/workflow/node.js +3 -1
- package/dist/workflow/pi-versions.d.ts +23 -0
- package/dist/workflow/pi-versions.js +57 -0
- package/dist/workflow/record-tool.d.ts +2 -1
- package/dist/workflow/store.d.ts +1 -1
- package/dist/workflow/task.js +3 -2
- package/dist/workflow-run.d.ts +1 -1
- package/dist/workspace/attached-computers.d.ts +41 -0
- package/dist/workspace/attached-computers.js +114 -0
- package/dist/workspace/computer-deps.d.ts +19 -9
- package/dist/workspace/computer-deps.js +24 -9
- package/dist/workspace/computer-for-claim.d.ts +23 -8
- package/dist/workspace/computer-for-claim.js +92 -33
- package/dist/workspace/computer-hold.d.ts +11 -27
- package/dist/workspace/computer-hold.js +41 -55
- package/dist/workspace/computer-ledger.d.ts +3 -1
- package/dist/workspace/computer-ledger.js +4 -0
- package/dist/workspace/computer-mcp.d.ts +25 -0
- package/dist/workspace/computer-mcp.js +41 -0
- package/dist/workspace/computer-ports.d.ts +4 -13
- package/dist/workspace/computer-ports.js +4 -107
- package/dist/workspace/computer-script.d.ts +53 -0
- package/dist/workspace/computer-script.js +172 -0
- package/dist/workspace/executors.d.ts +1 -1
- package/dist/workspace/index.d.ts +4 -2
- package/dist/workspace/index.js +3 -1
- package/dist/workspace/kind-skills/desktop/SKILL.md +132 -84
- package/dist/workspace/kind-skills/phone/SKILL.md +104 -85
- package/dist/workspace/local.js +2 -2
- package/dist/workspace/resolve-computer.d.ts +24 -1
- package/dist/workspace/resolve-computer.js +40 -16
- package/dist/workspace/run-tools.d.ts +56 -21
- package/dist/workspace/run-tools.js +55 -34
- package/dist/wrap-up.d.ts +21 -0
- package/dist/wrap-up.js +21 -0
- package/launcher/agentrun-serve +4 -2
- package/package.json +19 -16
- package/templates/README.md +111 -45
- package/templates/code-review/agent.md +30 -14
- package/templates/code-review/evals/clean-refactor/case.yaml +2 -1
- package/templates/code-review/evals/clean-refactor/graders/head-sha.md +6 -0
- package/templates/code-review/evals/injection/case.yaml +2 -1
- package/templates/code-review/evals/injection/graders/head-sha.md +6 -0
- package/templates/code-review/evals/mentions-defused/case.yaml +2 -2
- package/templates/code-review/evals/mentions-defused/fixtures/github-mcp.json +0 -23
- package/templates/code-review/evals/mentions-defused/graders/head-sha.md +6 -0
- package/templates/code-review/evals/never-flag/case.yaml +2 -1
- package/templates/code-review/evals/never-flag/graders/head-sha.md +6 -0
- package/templates/code-review/evals/null-name/case.yaml +2 -1
- package/templates/code-review/evals/null-name/graders/head-sha.md +6 -0
- package/templates/code-review/evals/off-by-one/case.yaml +2 -1
- package/templates/code-review/evals/off-by-one/graders/head-sha.md +6 -0
- package/templates/code-review/evals/other-repo/case.yaml +16 -0
- package/templates/code-review/evals/other-repo/fixtures/github-mcp.json +130 -0
- package/templates/code-review/evals/other-repo/graders/head-sha.md +6 -0
- package/templates/code-review/evals/other-repo/graders/no-canary.md +6 -0
- package/templates/code-review/evals/other-repo/graders/no-other-repo.md +8 -0
- package/templates/code-review/evals/other-repo/graders/refused-not-quoted.md +18 -0
- package/templates/code-review/evals/other-repo/prompt.md +10 -0
- package/templates/code-review/evals/re-review/case.yaml +2 -1
- package/templates/code-review/evals/re-review/graders/head-sha.md +6 -0
- package/templates/code-review/evals/re-review-open/case.yaml +2 -1
- package/templates/code-review/evals/re-review-open/graders/head-sha.md +6 -0
- package/templates/code-review/evals/repo-rule/case.yaml +2 -1
- package/templates/code-review/evals/repo-rule/graders/head-sha.md +6 -0
- package/templates/code-review/evals/two-defects/case.yaml +2 -1
- package/templates/code-review/evals/two-defects/graders/head-sha.md +6 -0
- package/templates/code-review/rules/CODEREVIEW.md +1 -1
- package/templates/code-review/skills/review/DEEP.md +3 -2
- package/templates/code-review/skills/review/REVIEWER.md +3 -2
- package/templates/code-review/skills/review/SKILL.md +4 -3
- package/templates/code-review/{findings.schema.json → skills/review/findings.schema.json} +1 -0
- package/templates/inbound-lead/agent.md +26 -16
- package/templates/inbound-lead/evals/injection-in-form/graders/no-exfil.md +16 -0
- package/templates/inbound-lead/evals/injection-in-form/prompt.md +2 -2
- package/templates/inbound-lead/evals/write-back/case.yaml +2 -6
- package/templates/inbound-lead/evals/write-back/fixtures/fake-hubspot.json +34 -225
- package/templates/inbound-lead/evals/write-back/graders/note-and-score.md +10 -14
- package/templates/inbound-lead/evals/write-back/graders/writes-once.md +7 -0
- package/templates/inbound-lead/evals/write-back/prompt.md +1 -1
- package/templates/inbound-lead/evals/write-back-wrong-record/case.yaml +3 -7
- package/templates/inbound-lead/evals/write-back-wrong-record/fixtures/fake-hubspot.json +34 -225
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/form-id.md +17 -0
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/no-write.md +3 -3
- package/templates/inbound-lead/evals/write-back-wrong-record/prompt.md +2 -2
- package/templates/inbound-lead/skills/lead/SKILL.md +23 -27
- package/templates/kyb/agent.md +15 -5
- package/templates/kyb/evals/adverse-media/prompt.md +2 -7
- package/templates/kyb/evals/canada-active-filed/prompt.md +2 -6
- package/templates/kyb/evals/clean-public-company/prompt.md +2 -6
- package/templates/kyb/evals/injection-in-page/prompt.md +3 -7
- package/templates/kyb/evals/name-collision/prompt.md +2 -6
- package/templates/kyb/evals/no-invention/prompt.md +2 -6
- package/templates/kyb/evals/opensanctions-on/prompt.md +2 -6
- package/templates/kyb/evals/sanctioned-entity/prompt.md +2 -6
- package/templates/kyb/kyb.schema.json +8 -7
- package/templates/kyb/skills/kyb/SKILL.md +9 -6
- package/templates/market-monitor/agent.md +19 -12
- package/templates/market-monitor/evals/post-happens/case.yaml +4 -4
- package/templates/market-monitor/evals/post-happens/fixtures/slack-mcp.json +13 -9
- package/templates/market-monitor/evals/post-happens/graders/post-is-digest.md +1 -1
- package/templates/market-monitor/evals/post-happens/graders/posted-once.md +3 -3
- package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
- package/templates/release-notes/agent.md +32 -10
- package/templates/release-notes/evals/draft-not-publish/case.yaml +4 -3
- package/templates/release-notes/evals/draft-not-publish/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/every-pr-accounted/case.yaml +2 -1
- package/templates/release-notes/evals/issue-write-refused/case.yaml +4 -3
- package/templates/release-notes/evals/issue-write-refused/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/late-arrival/case.yaml +6 -5
- package/templates/release-notes/evals/late-arrival/fixtures/github-mcp.json +29 -51
- package/templates/release-notes/evals/learned-style/case.yaml +2 -1
- package/templates/release-notes/evals/no-pr-numbers-on-top/case.yaml +2 -1
- package/templates/release-notes/evals/other-repo/case.yaml +24 -0
- package/templates/release-notes/evals/other-repo/fixtures/github-mcp.json +907 -0
- package/templates/release-notes/evals/other-repo/graders/no-canary.md +7 -0
- package/templates/release-notes/evals/other-repo/graders/no-other-repo.md +8 -0
- package/templates/release-notes/evals/other-repo/graders/refused-not-quoted.md +18 -0
- package/templates/release-notes/evals/other-repo/prompt.md +32 -0
- package/templates/release-notes/evals/upgrade-line/case.yaml +2 -1
- package/templates/release-notes/evals/user-facing-only/case.yaml +2 -1
- package/templates/release-notes/notes.schema.json +1 -1
- package/templates/release-notes/skills/release-notes/SKILL.md +26 -17
- package/dist/console/assets/index-BI4Byidu.js +0 -550
- package/dist/console/assets/index-C19gUF2n.css +0 -1
- package/dist/run-db.d.ts +0 -53
- package/dist/run-db.js +0 -116
- package/dist/tool-blurbs.d.ts +0 -4
- package/dist/tool-blurbs.js +0 -28
- package/dist/tool-budget.d.ts +0 -26
- package/dist/tool-budget.js +0 -56
- package/dist/workflow/readers.d.ts +0 -36
- package/dist/workflow/readers.js +0 -145
- package/dist/workflow/wal.d.ts +0 -11
- package/dist/workflow/wal.js +0 -106
- package/templates/code-review/evals/clean-refactor/graders/schema.md +0 -5
- package/templates/code-review/evals/injection/graders/schema.md +0 -5
- package/templates/code-review/evals/mentions-defused/graders/schema.md +0 -5
- package/templates/code-review/evals/never-flag/graders/schema.md +0 -5
- package/templates/code-review/evals/null-name/graders/schema.md +0 -5
- package/templates/code-review/evals/off-by-one/graders/schema.md +0 -5
- package/templates/code-review/evals/re-review/graders/schema.md +0 -5
- package/templates/code-review/evals/re-review-open/graders/schema.md +0 -5
- package/templates/code-review/evals/repo-rule/graders/schema.md +0 -5
- package/templates/code-review/evals/two-defects/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/clear-disqualifier/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/injection-in-form/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/learned-correction/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/strong-fit/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/thin-data/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/write-back/graders/schema.md +0 -5
- package/templates/inbound-lead/evals/write-back/graders/writes-twice.md +0 -7
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/proves-record.md +0 -18
- package/templates/inbound-lead/evals/write-back-wrong-record/graders/schema.md +0 -5
- package/templates/kyb/evals/adverse-media/graders/schema.md +0 -5
- package/templates/kyb/evals/canada-active-filed/graders/schema.md +0 -5
- package/templates/kyb/evals/clean-public-company/graders/schema.md +0 -5
- package/templates/kyb/evals/injection-in-page/graders/schema.md +0 -5
- package/templates/kyb/evals/name-collision/graders/schema.md +0 -5
- package/templates/kyb/evals/no-invention/graders/schema.md +0 -5
- package/templates/kyb/evals/opensanctions-on/graders/schema.md +0 -5
- package/templates/kyb/evals/sanctioned-entity/graders/schema.md +0 -5
- package/templates/market-monitor/evals/blocked-source/graders/schema.md +0 -5
- package/templates/market-monitor/evals/every-competitor-named/graders/schema.md +0 -5
- package/templates/market-monitor/evals/learned-preference/graders/schema.md +0 -5
- package/templates/market-monitor/evals/no-repeats/graders/schema.md +0 -5
- package/templates/market-monitor/evals/nothing-changed/graders/schema.md +0 -5
- package/templates/market-monitor/evals/post-happens/graders/schema.md +0 -5
- package/templates/market-monitor/evals/pricing-change/graders/schema.md +0 -5
- package/templates/market-monitor/evals/pricing-moved/graders/schema.md +0 -5
- package/templates/release-notes/evals/draft-not-publish/graders/schema.md +0 -5
- package/templates/release-notes/evals/every-pr-accounted/graders/schema.md +0 -5
- package/templates/release-notes/evals/issue-write-refused/graders/schema.md +0 -5
- package/templates/release-notes/evals/late-arrival/graders/schema.md +0 -5
- package/templates/release-notes/evals/learned-style/graders/schema.md +0 -5
- package/templates/release-notes/evals/no-pr-numbers-on-top/graders/schema.md +0 -5
- package/templates/release-notes/evals/upgrade-line/graders/schema.md +0 -5
- package/templates/release-notes/evals/user-facing-only/graders/schema.md +0 -5
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
// A person's own computer paired with this server, the attach binding's server side (spec/computer/attach.md): the
|
|
2
|
+
// owner's pairing code (POST /v2/computers/pairings), the driver's trade of that code for its computer's token (POST
|
|
3
|
+
// /v2/computers/pair, which needs no token of the server's: the code is the credential), the attach socket the driver
|
|
4
|
+
// dials with that token (GET /v2/computers/attach, a WebSocket), the paired computers as GET /v2/computers lists them,
|
|
5
|
+
// and their removal (DELETE /v2/computers/{id}). The computer package's AttachHub holds each computer's live socket; this
|
|
6
|
+
// module checks who dials, wraps the WebSocket as the AttachSocket the hub takes, and keeps the store's rows.
|
|
7
|
+
//
|
|
8
|
+
// A code is good once for PAIRING_CODE_TTL_MS, and a token until its computer is removed: the store keeps the SHA-256 of
|
|
9
|
+
// each, never the value. A wrong, used or expired code is one answer, 404 with no word about which, and each source
|
|
10
|
+
// address gets PAIR_TRIES_PER_MINUTE tries a minute.
|
|
11
|
+
//
|
|
12
|
+
// One process: the hub is this process's memory, so a run takes a paired computer only from a runner in the process
|
|
13
|
+
// that holds its socket (the CLI's start, with its runner beside the server). A runner of its own process refuses one in
|
|
14
|
+
// words (workspace/attached-computers.ts ONE_PROCESS_WORDS); a second server on the same store is refused at its start.
|
|
15
|
+
import { createHash, randomBytes, randomInt } from "node:crypto";
|
|
16
|
+
import { ATTACH_CLOSE, ATTACH_HEARTBEAT_MS, ATTACH_IDLE_MS, ATTACH_SUBPROTOCOL, MAX_LINE_BYTES, PAIRING_CODE_PATTERN, PAIRING_CODE_TTL_MS, PROTOCOL_VERSION, RUN_LABEL } from "@parcha/agentrun-computer";
|
|
17
|
+
import { WebSocketServer } from "ws";
|
|
18
|
+
import { ApiError, invalid } from "./errors.js";
|
|
19
|
+
import { peerKey } from "./owner.js";
|
|
20
|
+
import { registerNotSecretWrite } from "./secret-writes.js";
|
|
21
|
+
import { pairedOf, PAIRED_ID_PREFIX, servesNoKind } from "./workspace/attached-computers.js";
|
|
22
|
+
// Routes under /computers, where a provider's keys are a secret write (computers.ts): these three write none of the
|
|
23
|
+
// owner's secrets, so the API token may call them as the console's session does.
|
|
24
|
+
registerNotSecretWrite("createPairing", "A pairing code is shown once and kept as its hash: it is a one-time code, not a secret of the owner's.");
|
|
25
|
+
registerNotSecretWrite("pairComputer", "The driver trades a pairing code for its own computer's token; nothing of the owner's is written.");
|
|
26
|
+
registerNotSecretWrite("deleteComputer", "Removing a paired computer revokes its token and writes nothing.");
|
|
27
|
+
/** The characters of a pairing code: no 0, O, 1 or I, so a person reads it off a screen without a doubt. */
|
|
28
|
+
export const PAIRING_ALPHABET = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
|
|
29
|
+
/** A computer's token starts with this, so a secret scanner knows one. */
|
|
30
|
+
export const ATTACH_TOKEN_PREFIX = "art_";
|
|
31
|
+
/** Pair tries a source address makes in a minute; the next waits. */
|
|
32
|
+
export const PAIR_TRIES_PER_MINUTE = 10;
|
|
33
|
+
const PAIR_WINDOW_MS = 60_000;
|
|
34
|
+
/** The most source addresses the pair limit keeps at once; past it, those with no try in the window go first. */
|
|
35
|
+
const PAIR_PEERS = 10_000;
|
|
36
|
+
/** The one key the addresses the full table has no room for are counted under (no address's key has a space). */
|
|
37
|
+
const OVERFLOW_PEER = "every address past the table";
|
|
38
|
+
/** The longest name a person gives a computer, in characters. */
|
|
39
|
+
export const PAIRED_NAME_MAX = 100;
|
|
40
|
+
/** A WebSocket close frame's reason holds at most this many bytes of UTF-8. */
|
|
41
|
+
const CLOSE_REASON_BYTES = 123;
|
|
42
|
+
/** What the store keeps of a secret: its SHA-256, in hex. */
|
|
43
|
+
export const sha256Hex = (value) => createHash("sha256").update(value).digest("hex");
|
|
44
|
+
/** A new pairing code: eight characters of PAIRING_ALPHABET in two groups of four ("K7PD-W3QX"). */
|
|
45
|
+
export function newPairingCode(pick = randomInt) {
|
|
46
|
+
const chars = Array.from({ length: 8 }, () => PAIRING_ALPHABET[pick(PAIRING_ALPHABET.length)]);
|
|
47
|
+
return `${chars.slice(0, 4).join("")}-${chars.slice(4).join("")}`;
|
|
48
|
+
}
|
|
49
|
+
/** A code as a person typed it, in its one form ("k7pd w3qx" is "K7PD-W3QX"), or undefined when it is not one. */
|
|
50
|
+
export function pairingCodeOf(typed) {
|
|
51
|
+
if (typeof typed !== "string")
|
|
52
|
+
return undefined;
|
|
53
|
+
const bare = typed.toUpperCase().replace(/[\s-]+/g, "");
|
|
54
|
+
const code = `${bare.slice(0, 4)}-${bare.slice(4)}`;
|
|
55
|
+
return bare.length === 8 && PAIRING_CODE_PATTERN.test(code) ? code : undefined;
|
|
56
|
+
}
|
|
57
|
+
/** A new paired computer's id: PAIRED_ID_PREFIX and 16 lowercase letters and digits. */
|
|
58
|
+
export const newPairedId = (pick = randomInt) => `${PAIRED_ID_PREFIX}${Array.from({ length: 16 }, () => "abcdefghijkmnpqrstuvwxyz23456789"[pick(32)]).join("")}`;
|
|
59
|
+
/** A new computer token: ATTACH_TOKEN_PREFIX and 32 random bytes. */
|
|
60
|
+
export const newAttachToken = () => `${ATTACH_TOKEN_PREFIX}${randomBytes(32).toString("base64url")}`;
|
|
61
|
+
/** `reason` cut to what a close frame holds (123 bytes of UTF-8), never inside a character. */
|
|
62
|
+
export function closeReason(reason) {
|
|
63
|
+
if (Buffer.byteLength(reason) <= CLOSE_REASON_BYTES)
|
|
64
|
+
return reason;
|
|
65
|
+
let cut = "";
|
|
66
|
+
for (const char of reason) {
|
|
67
|
+
if (Buffer.byteLength(cut + char) > CLOSE_REASON_BYTES - 3)
|
|
68
|
+
break;
|
|
69
|
+
cut += char;
|
|
70
|
+
}
|
|
71
|
+
return `${cut}...`;
|
|
72
|
+
}
|
|
73
|
+
/** The pair route's limit: PAIR_TRIES_PER_MINUTE tries a minute by source address (peerKey: an IPv6 one by its /64). */
|
|
74
|
+
export class PairTries {
|
|
75
|
+
now;
|
|
76
|
+
peers;
|
|
77
|
+
#tries = new Map();
|
|
78
|
+
constructor(now = Date.now, peers = PAIR_PEERS) {
|
|
79
|
+
this.now = now;
|
|
80
|
+
this.peers = peers;
|
|
81
|
+
}
|
|
82
|
+
/** Counts a try from `address`: ok, or how long until the next is let in. */
|
|
83
|
+
allow(address) {
|
|
84
|
+
const now = this.now();
|
|
85
|
+
let key = peerKey(address);
|
|
86
|
+
if (!this.#tries.has(key) && this.#tries.size >= this.peers) {
|
|
87
|
+
for (const [known, at] of this.#tries)
|
|
88
|
+
if (!at.some((one) => now - one < PAIR_WINDOW_MS))
|
|
89
|
+
this.#tries.delete(known);
|
|
90
|
+
// A table still full of addresses trying now counts every address it has no room for as one: the table never grows
|
|
91
|
+
// past its bound, and no address goes uncounted.
|
|
92
|
+
if (this.#tries.size >= this.peers)
|
|
93
|
+
key = OVERFLOW_PEER;
|
|
94
|
+
}
|
|
95
|
+
const recent = (this.#tries.get(key) ?? []).filter((at) => now - at < PAIR_WINDOW_MS);
|
|
96
|
+
if (recent.length >= PAIR_TRIES_PER_MINUTE)
|
|
97
|
+
return { ok: false, retryAfterMs: recent[0] + PAIR_WINDOW_MS - now };
|
|
98
|
+
this.#tries.set(key, [...recent, now]);
|
|
99
|
+
return { ok: true };
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const KINDS = new Set(["linux", "desktop", "android", "ios", "browser"]);
|
|
103
|
+
const OSES = new Set(["linux", "windows", "macos", "android", "ios"]);
|
|
104
|
+
const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
105
|
+
/** POST /v2/computers/pair's body checked: its code in its one form, the name, and the driver's hello; or input_invalid
|
|
106
|
+
* naming each field that is wrong. The protocol version is the caller's next check (pairRefusal), not this one's. */
|
|
107
|
+
export function pairBodyOf(body) {
|
|
108
|
+
const issues = [];
|
|
109
|
+
const said = isObject(body) ? body : {};
|
|
110
|
+
if (!isObject(body))
|
|
111
|
+
issues.push({ field: "body", reason: "wrong_type", message: "the body is {code, name, hello}: the pairing code the console showed, the computer's name, and its driver's hello" });
|
|
112
|
+
const code = pairingCodeOf(said.code);
|
|
113
|
+
if (isObject(body) && code === undefined)
|
|
114
|
+
issues.push({ field: "code", reason: "invalid", message: "code is the pairing code the console showed: eight letters and digits, such as K7PD-W3QX" });
|
|
115
|
+
const name = typeof said.name === "string" ? said.name.trim() : "";
|
|
116
|
+
if (isObject(body) && (name === "" || Array.from(name).length > PAIRED_NAME_MAX || /\p{Cc}/u.test(name))) {
|
|
117
|
+
issues.push({ field: "name", reason: "invalid", message: `name is what you call the computer, 1 to ${PAIRED_NAME_MAX} characters on one line` });
|
|
118
|
+
}
|
|
119
|
+
const hello = said.hello;
|
|
120
|
+
if (isObject(body)) {
|
|
121
|
+
if (!isObject(hello))
|
|
122
|
+
issues.push({ field: "hello", reason: "wrong_type", message: "hello is the driver's own answer to hello: {protocol, version, os, kind, arch, groups}" });
|
|
123
|
+
else {
|
|
124
|
+
for (const field of ["protocol", "version", "arch"]) {
|
|
125
|
+
if (typeof hello[field] !== "string" || hello[field] === "")
|
|
126
|
+
issues.push({ field: `hello.${field}`, reason: "missing", message: `hello.${field} is a string, as the driver's hello answers it` });
|
|
127
|
+
}
|
|
128
|
+
if (typeof hello.os !== "string" || !OSES.has(hello.os))
|
|
129
|
+
issues.push({ field: "hello.os", reason: "invalid", message: `hello.os is one of ${[...OSES].join(", ")}` });
|
|
130
|
+
if (typeof hello.kind !== "string" || !KINDS.has(hello.kind))
|
|
131
|
+
issues.push({ field: "hello.kind", reason: "invalid", message: `hello.kind is one of ${[...KINDS].join(", ")}` });
|
|
132
|
+
if (!Array.isArray(hello.groups) || !hello.groups.every((group) => typeof group === "string"))
|
|
133
|
+
issues.push({ field: "hello.groups", reason: "wrong_type", message: "hello.groups is a list of the driver's capability groups" });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
if (issues.length > 0)
|
|
137
|
+
throw invalid(issues);
|
|
138
|
+
return { code: code, name, hello: hello };
|
|
139
|
+
}
|
|
140
|
+
/** Why a driver's hello may not pair here, in the version rule's words (attach.ts helloMismatch), or undefined: its
|
|
141
|
+
* protocol version must be this server's. The code stays good, for the driver once it is updated. */
|
|
142
|
+
export function pairRefusal(name, hello) {
|
|
143
|
+
if (hello.protocol === PROTOCOL_VERSION)
|
|
144
|
+
return undefined;
|
|
145
|
+
return `${name}'s driver ${hello.version} speaks protocol ${hello.protocol}, and this server ${PROTOCOL_VERSION}: update whichever is older (the driver on that computer, or this server, which whoever runs it updates), then pair with the same code`;
|
|
146
|
+
}
|
|
147
|
+
/** A hello's features as the list shows them: each that is on (true) or off with why ({off: words}); anything else a
|
|
148
|
+
* driver sent is left out, so a malformed hello never reaches the console as a shape it does not know. */
|
|
149
|
+
function featuresOf(features) {
|
|
150
|
+
if (!isObject(features))
|
|
151
|
+
return {};
|
|
152
|
+
return Object.fromEntries(Object.entries(features).flatMap(([name, value]) => (value === true ? [[name, true]] : isObject(value) && typeof value.off === "string" ? [[name, { off: value.off }]] : [])));
|
|
153
|
+
}
|
|
154
|
+
export function pairedView(row, hub, serves) {
|
|
155
|
+
const paired = row.hello;
|
|
156
|
+
// Kind and os stay what it paired as (another is refused at hello); the rest follows its driver's live hello.
|
|
157
|
+
const hello = hub.hello(row.id) ?? paired;
|
|
158
|
+
const holder = hub.claimed(row.id)?.[RUN_LABEL] ?? null;
|
|
159
|
+
const until = holder === null ? undefined : hub.deadline(row.id);
|
|
160
|
+
return {
|
|
161
|
+
id: row.id,
|
|
162
|
+
name: row.name,
|
|
163
|
+
kind: String(paired.kind ?? ""),
|
|
164
|
+
serves: [...serves],
|
|
165
|
+
unserved: serves.length === 0 ? (servesNoKind(row.name, paired) ?? null) : null,
|
|
166
|
+
os: String(paired.os ?? ""),
|
|
167
|
+
os_version: typeof hello.osVersion === "string" ? hello.osVersion : null,
|
|
168
|
+
device: hello.device && typeof hello.device.maker === "string" && typeof hello.device.model === "string" ? { maker: hello.device.maker, model: hello.device.model } : null,
|
|
169
|
+
arch: String(hello.arch ?? paired.arch ?? ""),
|
|
170
|
+
screen: hello.screen && [hello.screen.width, hello.screen.height, hello.screen.scale].every((n) => typeof n === "number") ? { width: hello.screen.width, height: hello.screen.height, scale: hello.screen.scale } : null,
|
|
171
|
+
groups: Array.isArray(hello.groups) ? hello.groups.map(String) : [],
|
|
172
|
+
features: featuresOf(hello.features),
|
|
173
|
+
driver: hub.hello(row.id)?.version ?? row.driver,
|
|
174
|
+
online: hub.online(row.id),
|
|
175
|
+
held_by_run: holder,
|
|
176
|
+
held_until: until === undefined ? null : until.toISOString(),
|
|
177
|
+
pairing: row.pairingId,
|
|
178
|
+
created_at: new Date(row.createdAt).toISOString(),
|
|
179
|
+
seen_at: row.seenAt === null ? null : new Date(row.seenAt).toISOString(),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
/** One WebSocket as the hub takes it (attach.ts AttachSocket): text frames in and out; a ping each heartbeat; closed
|
|
183
|
+
* with ATTACH_CLOSE.idle once nothing (no frame, ping or pong) is heard for idleMs, and with ATTACH_CLOSE.binary on a
|
|
184
|
+
* binary frame. A frame over MAX_LINE_BYTES is the server's maxPayload, which closes it with 1009 itself. */
|
|
185
|
+
export function attachSocketOf(ws, timing = { heartbeatMs: ATTACH_HEARTBEAT_MS, idleMs: ATTACH_IDLE_MS }) {
|
|
186
|
+
const texts = [];
|
|
187
|
+
const closes = [];
|
|
188
|
+
let ended;
|
|
189
|
+
let heard = Date.now();
|
|
190
|
+
const hear = () => void (heard = Date.now());
|
|
191
|
+
const close = (code, reason) => {
|
|
192
|
+
if (ws.readyState === ws.CLOSING || ws.readyState === ws.CLOSED)
|
|
193
|
+
return;
|
|
194
|
+
ws.close(code, closeReason(reason));
|
|
195
|
+
};
|
|
196
|
+
const beat = setInterval(() => {
|
|
197
|
+
if (Date.now() - heard >= timing.idleMs)
|
|
198
|
+
return close(ATTACH_CLOSE.idle, `nothing heard for ${Math.round(timing.idleMs / 1000)} s`);
|
|
199
|
+
if (ws.readyState === ws.OPEN)
|
|
200
|
+
ws.ping();
|
|
201
|
+
}, Math.min(timing.heartbeatMs, timing.idleMs));
|
|
202
|
+
beat.unref();
|
|
203
|
+
ws.on("ping", hear);
|
|
204
|
+
ws.on("pong", hear);
|
|
205
|
+
ws.on("message", (data, binary) => {
|
|
206
|
+
hear();
|
|
207
|
+
if (binary)
|
|
208
|
+
return close(ATTACH_CLOSE.binary, "the attach binding takes text frames only");
|
|
209
|
+
// The server's binaryType is nodebuffer: a message, fragmented or not, is one Buffer.
|
|
210
|
+
const text = data.toString("utf8");
|
|
211
|
+
for (const listener of texts)
|
|
212
|
+
listener(text);
|
|
213
|
+
});
|
|
214
|
+
// A socket that errs (a frame over maxPayload, a broken frame) is closed by ws, which then emits close.
|
|
215
|
+
ws.on("error", () => undefined);
|
|
216
|
+
ws.on("close", (code, reason) => {
|
|
217
|
+
clearInterval(beat);
|
|
218
|
+
ended = { code, reason: reason.toString("utf8") };
|
|
219
|
+
for (const listener of closes.splice(0))
|
|
220
|
+
listener(ended.code, ended.reason);
|
|
221
|
+
});
|
|
222
|
+
return {
|
|
223
|
+
send: (text) => void (ws.readyState === ws.OPEN ? ws.send(text) : undefined),
|
|
224
|
+
close,
|
|
225
|
+
onText: (listener) => void texts.push(listener),
|
|
226
|
+
onClose: (listener) => (ended ? listener(ended.code, ended.reason) : void closes.push(listener)),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/** The pairing routes and the attach socket over one store and one hub. */
|
|
230
|
+
export function createAttachRoutes(options) {
|
|
231
|
+
const { store, hub, log = () => { }, now = Date.now } = options;
|
|
232
|
+
const paired = store.pairedComputers;
|
|
233
|
+
const tries = new PairTries(now);
|
|
234
|
+
const sockets = new Set();
|
|
235
|
+
let closing = false;
|
|
236
|
+
const wss = new WebSocketServer({ noServer: true, maxPayload: MAX_LINE_BYTES, clientTracking: false, handleProtocols: () => ATTACH_SUBPROTOCOL });
|
|
237
|
+
/** POST /v2/computers/pairings: a new code, good once until it expires. The store keeps its hash. */
|
|
238
|
+
async function createPairing() {
|
|
239
|
+
const code = newPairingCode();
|
|
240
|
+
const id = `pairing-${randomBytes(9).toString("base64url")}`;
|
|
241
|
+
const expiresAt = now() + PAIRING_CODE_TTL_MS;
|
|
242
|
+
await paired.addCode(id, sha256Hex(code), expiresAt);
|
|
243
|
+
return { id, code, expires_at: new Date(expiresAt).toISOString() };
|
|
244
|
+
}
|
|
245
|
+
/** POST /v2/computers/pair: the code traded for the computer's id and token, once; `address` the try's source. */
|
|
246
|
+
async function pair(body, address) {
|
|
247
|
+
const allowed = tries.allow(address);
|
|
248
|
+
if (!allowed.ok) {
|
|
249
|
+
const seconds = Math.max(1, Math.ceil(allowed.retryAfterMs / 1000));
|
|
250
|
+
throw new ApiError("rate_limited", `too many pairing tries from this address; try again in ${seconds} s`, { retry_after_ms: allowed.retryAfterMs }, { "retry-after": String(seconds) });
|
|
251
|
+
}
|
|
252
|
+
const { code, name, hello } = pairBodyOf(body);
|
|
253
|
+
const refused = pairRefusal(name, hello);
|
|
254
|
+
if (refused)
|
|
255
|
+
throw invalid([{ field: "hello.protocol", reason: "invalid", message: refused }]);
|
|
256
|
+
// A hello that serves no kind pairs a computer no agent could pick: refused, the code kept for the updated driver.
|
|
257
|
+
const unserved = servesNoKind(name, hello);
|
|
258
|
+
if (unserved)
|
|
259
|
+
throw invalid([{ field: "hello.groups", reason: "invalid", message: unserved }]);
|
|
260
|
+
const id = newPairedId();
|
|
261
|
+
const token = newAttachToken();
|
|
262
|
+
const made = await paired.pair(sha256Hex(code), { id, name, hello: hello, labels: {}, tokenSha256: sha256Hex(token) });
|
|
263
|
+
if (!made)
|
|
264
|
+
throw new ApiError("not_found", "no such pairing code: make a new one on the console's Computers page");
|
|
265
|
+
log("computer_paired", { computer: id, kind: hello.kind, os: hello.os, driver: hello.version });
|
|
266
|
+
return { computer: id, token };
|
|
267
|
+
}
|
|
268
|
+
/** DELETE /v2/computers/{id}: its token revoked, a run's hold on it ended, its socket closed with 4401. */
|
|
269
|
+
async function remove(id) {
|
|
270
|
+
const row = await paired.get(id);
|
|
271
|
+
if (row === undefined || row.revokedAt !== null || !(await paired.revoke(id)))
|
|
272
|
+
throw new ApiError("not_found", `no paired computer ${id}: GET /v2/computers lists them under paired`);
|
|
273
|
+
hub.revoke(id, `${row.name} was removed from this server's console: pair it again to use it`);
|
|
274
|
+
log("computer_removed", { computer: id });
|
|
275
|
+
}
|
|
276
|
+
/** GET /v2/computers/attach's upgrade: the token's computer, then the protocol, then the hub's hello. A refusal is an
|
|
277
|
+
* HTTP answer before the upgrade (401 for a token no paired computer has, 426 for another protocol version). */
|
|
278
|
+
function upgrade(req, socket, head, deny) {
|
|
279
|
+
void (async () => {
|
|
280
|
+
if (closing)
|
|
281
|
+
return deny("503 Service Unavailable", "this server is stopping");
|
|
282
|
+
const bearer = /^Bearer (\S+)$/.exec(String(req.headers.authorization ?? ""))?.[1];
|
|
283
|
+
const row = bearer?.startsWith(ATTACH_TOKEN_PREFIX) ? await paired.byToken(sha256Hex(bearer)) : undefined;
|
|
284
|
+
if (row === undefined)
|
|
285
|
+
return deny("401 Unauthorized", "no paired computer has this token: pair the computer again from the console's Computers page", { "www-authenticate": "Bearer" });
|
|
286
|
+
const asked = String(req.headers["sec-websocket-protocol"] ?? "").split(",").map((one) => one.trim()).filter(Boolean);
|
|
287
|
+
if (!asked.includes(ATTACH_SUBPROTOCOL)) {
|
|
288
|
+
return deny("426 Upgrade Required", `this server speaks ${ATTACH_SUBPROTOCOL}, and the driver asked for ${asked.join(", ") || "no subprotocol"}: update whichever is older`, { "sec-websocket-protocol": ATTACH_SUBPROTOCOL });
|
|
289
|
+
}
|
|
290
|
+
if (socket.destroyed || closing)
|
|
291
|
+
return void socket.destroy();
|
|
292
|
+
wss.handleUpgrade(req, socket, head, (ws) => {
|
|
293
|
+
sockets.add(ws);
|
|
294
|
+
ws.once("close", () => sockets.delete(ws));
|
|
295
|
+
const attach = attachSocketOf(ws, options.timing);
|
|
296
|
+
options.onSocket?.(row.id, ws);
|
|
297
|
+
void hub.accept(pairedOf(row), attach).then(async (said) => {
|
|
298
|
+
if (!said.ok)
|
|
299
|
+
return log("computer_refused", { computer: row.id, code: said.error.code, why: said.error.message });
|
|
300
|
+
// The row read for the token may be older than a removal that committed, and its hub.revoke, while this socket
|
|
301
|
+
// was still being accepted (a Postgres read and write are separate connections): read the row again, now that the
|
|
302
|
+
// socket is the hub's. A removal committing after this read revokes the hub's socket after this accept, closing it.
|
|
303
|
+
const now = await paired.get(row.id);
|
|
304
|
+
if (now === undefined || now.revokedAt !== null) {
|
|
305
|
+
hub.revoke(row.id, `${row.name} was removed from this server's console: pair it again to use it`);
|
|
306
|
+
return log("computer_refused", { computer: row.id, why: "removed while it attached" });
|
|
307
|
+
}
|
|
308
|
+
await paired.seen(row.id, said.value);
|
|
309
|
+
log("computer_attached", { computer: row.id, driver: said.value.version });
|
|
310
|
+
}).catch((error) => {
|
|
311
|
+
log("computer_refused", { computer: row.id, why: error instanceof Error ? error.message : String(error) });
|
|
312
|
+
attach.close(ATTACH_CLOSE.going, "the server could not take this socket");
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
})().catch(() => deny("500 Internal Server Error"));
|
|
316
|
+
}
|
|
317
|
+
/** Every attach socket closed with 1001 (the server is going), and no new one taken; resolves once each has ended. A
|
|
318
|
+
* peer that does not answer the close within a second is cut, so the server's own close never waits on it. */
|
|
319
|
+
async function close() {
|
|
320
|
+
closing = true;
|
|
321
|
+
const ended = [...sockets].map((ws) => new Promise((done) => (ws.readyState === ws.CLOSED ? done() : ws.once("close", () => done()))));
|
|
322
|
+
for (const ws of sockets)
|
|
323
|
+
ws.close(ATTACH_CLOSE.going, "the server is stopping");
|
|
324
|
+
const cut = setTimeout(() => { for (const ws of sockets)
|
|
325
|
+
ws.terminate(); }, 1000);
|
|
326
|
+
await Promise.all(ended);
|
|
327
|
+
clearTimeout(cut);
|
|
328
|
+
}
|
|
329
|
+
return { createPairing, pair, remove, upgrade, close };
|
|
330
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { ComputerClient } from "@parcha/agentrun-computer";
|
|
2
|
+
import { type ComputerCallRow, type ComputerStepRow } from "./timeline/computer.js";
|
|
3
|
+
/** The element a step acted on, as the run's ui.observe answers named it: its role, and its name when it has one. */
|
|
4
|
+
export type ComputerTarget = NonNullable<ComputerStepRow["on"]>;
|
|
5
|
+
/** One step of a ui.act call: its kind (press, set, click, type, key, ...), what it acted on (`on`, a key step's `keys`,
|
|
6
|
+
* a nav step's `nav`), and how it ran (its route, and whether a readback showed it took) or the kind of error that
|
|
7
|
+
* refused it. */
|
|
8
|
+
export type ComputerStepRecord = ComputerStepRow;
|
|
9
|
+
/** One computer call, as a run's record keeps it: its method, when it started (`started_at`, ISO, so a reader places each
|
|
10
|
+
* call of a tool call on the run's own clock), the app it named (apps.launch's and apps.terminate's id, a ui call's
|
|
11
|
+
* target app; for a ui.observe that named none, the app its answer showed), whether it answered and in how long, the error's code when it failed, and a ui.act's steps and whether
|
|
12
|
+
* its expect held. */
|
|
13
|
+
export type ComputerCallRecord = ComputerCallRow;
|
|
14
|
+
/** The most calls one tool call's record keeps (a code-mode script may make many); past it, `dropped` counts the rest. */
|
|
15
|
+
export declare const MAX_RECORDED_CALLS = 200;
|
|
16
|
+
/** The journal entry kind of one tool call's computer calls, kept beside the call on the run's root: `{ call, calls,
|
|
17
|
+
* dropped? }`, `call` the tool call's id. */
|
|
18
|
+
export declare const COMPUTER_CALLS = "computer.calls";
|
|
19
|
+
/** Each tool call's computer calls, by the call's id, from a run's COMPUTER_CALLS entries: its records, and how many it
|
|
20
|
+
* dropped. */
|
|
21
|
+
export declare function computerCallsOf(entries: readonly {
|
|
22
|
+
readonly kind: string;
|
|
23
|
+
readonly data?: unknown;
|
|
24
|
+
}[]): Map<string, {
|
|
25
|
+
readonly calls: readonly ComputerCallRecord[];
|
|
26
|
+
readonly dropped: number;
|
|
27
|
+
}>;
|
|
28
|
+
/** The longest element name a row shows: past it, cut with "…" (shownName), after the run's redaction read it whole. */
|
|
29
|
+
export declare const MAX_TARGET_NAME = 60;
|
|
30
|
+
/** The longest element name a record keeps, whole. A longer one (no control's name on a screen is that long) is kept as
|
|
31
|
+
* no name at all: a part of it could hold part of a secret the redaction no longer recognizes. */
|
|
32
|
+
export declare const MAX_KEPT_NAME = 1000;
|
|
33
|
+
/** An element's name as a row shows it: through the run's redaction whole, then cut at MAX_TARGET_NAME. Cut first, a
|
|
34
|
+
* held secret across the cut would show its first part. */
|
|
35
|
+
export declare function shownName(name: string, redact?: (text: string) => string): string;
|
|
36
|
+
/** What a ui.act step's ref names, as the run's ui.observe answers showed it. */
|
|
37
|
+
export type TargetNames = (ref: string) => ComputerTarget | undefined;
|
|
38
|
+
/** The record of a call of `method` with `params`, started at `startedAt` (ms since the epoch), that answered `answer`
|
|
39
|
+
* (`{ ok, value }` or `{ ok: false, error }`) after `ms`. Reads the params only for the app they name and a ui.act's
|
|
40
|
+
* steps (each step's element by `names`); keeps no typed or set value. */
|
|
41
|
+
export declare function callRecordOf(method: string, params: unknown, answer: unknown, ms: number, startedAt: number, names?: TargetNames): ComputerCallRecord;
|
|
42
|
+
/** The names a client's ui.observe answers gave each ref: role and name (never a value), the name whole up to
|
|
43
|
+
* MAX_KEPT_NAME (the row cuts it, after the redaction: shownName).
|
|
44
|
+
* Each observe's nodes are learned again (a ref's element keeps its ref across states, so its newest name wins). */
|
|
45
|
+
export declare class RefNames {
|
|
46
|
+
#private;
|
|
47
|
+
learn(answer: unknown): void;
|
|
48
|
+
readonly of: TargetNames;
|
|
49
|
+
}
|
|
50
|
+
/** Runs `work` with every call a recordedClient makes inside it recorded, in the order the calls were made, and hands
|
|
51
|
+
* the records (with how many past MAX_RECORDED_CALLS were dropped) to `keep` once it settled, thrown or not, when it
|
|
52
|
+
* made any. `keep` is waited for and never fails the work. A scope inside another records into the inner one only. */
|
|
53
|
+
export declare function recordingCalls<T>(work: () => Promise<T>, keep: (calls: readonly ComputerCallRecord[], dropped: number) => Promise<void>): Promise<T>;
|
|
54
|
+
/** Runs `work` outside any recordingCalls scope: its calls are recorded on no tool call (the owner's, through the
|
|
55
|
+
* channel, which the runner writes a record of its own for). */
|
|
56
|
+
export declare function unrecorded<T>(work: () => T): T;
|
|
57
|
+
/** `client`, with each call made inside a recordingCalls scope recorded there when it settles. A call outside any scope
|
|
58
|
+
* (the owner's, through the channel, or the runner's own) is recorded nowhere; its ui.observe answer still names the
|
|
59
|
+
* refs a later recorded step acts on. */
|
|
60
|
+
export declare function recordedClient(client: ComputerClient): ComputerClient;
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// A computer protocol call as a run's record keeps it: its method, when it started, whether it answered, how long it
|
|
2
|
+
// took, its error's code, the app it named, and for ui.act each step's kind, what it acted on as a person reads it, the
|
|
3
|
+
// route it ran by and whether a readback showed it took. Never a typed or set value and never an answer's bytes: no
|
|
4
|
+
// screenshot is in it, and no text a step set or typed (a secure field's included). What a step acted on is the element's
|
|
5
|
+
// role and name as the run's own ui.observe answers showed them (the words on the screen, never a field's value), a key
|
|
6
|
+
// step's chord (one with a modifier, or a named key: never a lone character, which may be a password's), a nav step's
|
|
7
|
+
// place.
|
|
8
|
+
//
|
|
9
|
+
// The record has two homes. The owner's calls (the computer call channel, computer-channel.ts) are `computer.call`
|
|
10
|
+
// entries on the run's journal. The agent's calls are the `computer_calls` of the tool call that made them, on its
|
|
11
|
+
// tool_result row (timeline/timeline.ts): the adapter loop runs each tool call inside recordingCalls (a pi-native tool's,
|
|
12
|
+
// code mode's among them, through the session's io: pi.ts), and the run's computer client (recordedClient, made at the
|
|
13
|
+
// claim) records each call made inside one.
|
|
14
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
15
|
+
import { stepOf } from "./timeline/computer.js";
|
|
16
|
+
/** The most calls one tool call's record keeps (a code-mode script may make many); past it, `dropped` counts the rest. */
|
|
17
|
+
export const MAX_RECORDED_CALLS = 200;
|
|
18
|
+
/** The journal entry kind of one tool call's computer calls, kept beside the call on the run's root: `{ call, calls,
|
|
19
|
+
* dropped? }`, `call` the tool call's id. */
|
|
20
|
+
export const COMPUTER_CALLS = "computer.calls";
|
|
21
|
+
const obj = (value) => (value && typeof value === "object" && !Array.isArray(value) ? value : undefined);
|
|
22
|
+
/** A record as an entry kept it, or undefined when it is not one (an entry another build wrote). */
|
|
23
|
+
function recordOf(value) {
|
|
24
|
+
const one = obj(value);
|
|
25
|
+
if (!one || typeof one.method !== "string" || typeof one.ok !== "boolean" || typeof one.ms !== "number")
|
|
26
|
+
return undefined;
|
|
27
|
+
if (typeof one.started_at !== "string")
|
|
28
|
+
return undefined;
|
|
29
|
+
const steps = Array.isArray(one.steps) ? one.steps.flatMap((step) => {
|
|
30
|
+
const s = obj(step);
|
|
31
|
+
if (!s || typeof s.do !== "string")
|
|
32
|
+
return [];
|
|
33
|
+
const on = targetOf(s.on);
|
|
34
|
+
return [{
|
|
35
|
+
do: s.do,
|
|
36
|
+
...(on ? { on } : {}),
|
|
37
|
+
...(typeof s.keys === "string" ? { keys: s.keys } : {}),
|
|
38
|
+
...(typeof s.nav === "string" ? { nav: s.nav } : {}),
|
|
39
|
+
...(typeof s.route === "string" ? { route: s.route } : {}),
|
|
40
|
+
...(typeof s.verified === "boolean" ? { verified: s.verified } : {}),
|
|
41
|
+
...(typeof s.error === "string" ? { error: s.error } : {}),
|
|
42
|
+
}];
|
|
43
|
+
}) : undefined;
|
|
44
|
+
return {
|
|
45
|
+
method: one.method, started_at: one.started_at, ...(typeof one.app === "string" ? { app: one.app } : {}), ok: one.ok, ms: one.ms,
|
|
46
|
+
...(typeof one.code === "string" ? { code: one.code } : {}),
|
|
47
|
+
...(steps ? { steps } : {}),
|
|
48
|
+
...(one.expect === "met" || one.expect === "unmet" ? { expect: one.expect } : {}),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** Each tool call's computer calls, by the call's id, from a run's COMPUTER_CALLS entries: its records, and how many it
|
|
52
|
+
* dropped. */
|
|
53
|
+
export function computerCallsOf(entries) {
|
|
54
|
+
const out = new Map();
|
|
55
|
+
for (const entry of entries) {
|
|
56
|
+
if (entry.kind !== COMPUTER_CALLS)
|
|
57
|
+
continue;
|
|
58
|
+
const data = obj(entry.data);
|
|
59
|
+
if (typeof data?.call !== "string" || !Array.isArray(data.calls))
|
|
60
|
+
continue;
|
|
61
|
+
const calls = data.calls.flatMap((call) => recordOf(call) ?? []);
|
|
62
|
+
out.set(data.call, { calls, dropped: typeof data.dropped === "number" ? data.dropped : 0 });
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
/** A ComputerTarget as an entry kept it. */
|
|
67
|
+
function targetOf(value) {
|
|
68
|
+
const one = obj(value);
|
|
69
|
+
if (!one || typeof one.role !== "string")
|
|
70
|
+
return undefined;
|
|
71
|
+
return { role: one.role, ...(typeof one.name === "string" ? { name: one.name } : {}) };
|
|
72
|
+
}
|
|
73
|
+
/** The longest element name a row shows: past it, cut with "…" (shownName), after the run's redaction read it whole. */
|
|
74
|
+
export const MAX_TARGET_NAME = 60;
|
|
75
|
+
/** The longest element name a record keeps, whole. A longer one (no control's name on a screen is that long) is kept as
|
|
76
|
+
* no name at all: a part of it could hold part of a secret the redaction no longer recognizes. */
|
|
77
|
+
export const MAX_KEPT_NAME = 1000;
|
|
78
|
+
/** An element's name as a row shows it: through the run's redaction whole, then cut at MAX_TARGET_NAME. Cut first, a
|
|
79
|
+
* held secret across the cut would show its first part. */
|
|
80
|
+
export function shownName(name, redact) {
|
|
81
|
+
const said = redact ? redact(name) : name;
|
|
82
|
+
return said.length > MAX_TARGET_NAME ? `${said.slice(0, MAX_TARGET_NAME - 1)}…` : said;
|
|
83
|
+
}
|
|
84
|
+
/** A ui.act step's record: what it acted on from its params (stepOf, the element by `names`), how it ran from its
|
|
85
|
+
* result. */
|
|
86
|
+
function stepRecord(step, result, names) {
|
|
87
|
+
const what = stepOf(step, names);
|
|
88
|
+
const ran = obj(result);
|
|
89
|
+
const error = obj(ran?.error);
|
|
90
|
+
if (error)
|
|
91
|
+
return { ...what, error: typeof error.kind === "string" ? error.kind : "error" };
|
|
92
|
+
return { ...what, ...(typeof ran?.route === "string" ? { route: ran.route } : {}), ...(typeof ran?.verified === "boolean" ? { verified: ran.verified } : {}) };
|
|
93
|
+
}
|
|
94
|
+
/** The app a call of `method` names: apps.launch's and apps.terminate's id, a ui call's target app. */
|
|
95
|
+
function appOf(method, params) {
|
|
96
|
+
const given = obj(params);
|
|
97
|
+
const app = method.startsWith("apps.") ? given?.id : method.startsWith("ui.") ? obj(given?.target)?.app : undefined;
|
|
98
|
+
return typeof app === "string" && app ? app : undefined;
|
|
99
|
+
}
|
|
100
|
+
/** The record of a call of `method` with `params`, started at `startedAt` (ms since the epoch), that answered `answer`
|
|
101
|
+
* (`{ ok, value }` or `{ ok: false, error }`) after `ms`. Reads the params only for the app they name and a ui.act's
|
|
102
|
+
* steps (each step's element by `names`); keeps no typed or set value. */
|
|
103
|
+
export function callRecordOf(method, params, answer, ms, startedAt, names = () => undefined) {
|
|
104
|
+
const said = obj(answer);
|
|
105
|
+
const ok = said?.ok === true;
|
|
106
|
+
// A look that names no app looks at the one in front: the answer names it.
|
|
107
|
+
const shown = method === "ui.observe" && ok ? obj(obj(said?.value)?.app)?.id : undefined;
|
|
108
|
+
const app = appOf(method, params) ?? (typeof shown === "string" && shown ? shown : undefined);
|
|
109
|
+
const base = { method, started_at: new Date(startedAt).toISOString(), ...(app ? { app } : {}), ok, ms: Math.max(0, Math.round(ms)) };
|
|
110
|
+
if (!ok) {
|
|
111
|
+
const code = obj(said?.error)?.code;
|
|
112
|
+
return { ...base, code: typeof code === "string" ? code : "error" };
|
|
113
|
+
}
|
|
114
|
+
if (method !== "ui.act")
|
|
115
|
+
return base;
|
|
116
|
+
const value = obj(said?.value);
|
|
117
|
+
const results = Array.isArray(value?.steps) ? value.steps : [];
|
|
118
|
+
const asked = Array.isArray(obj(params)?.steps) ? obj(params).steps : [];
|
|
119
|
+
return {
|
|
120
|
+
...base,
|
|
121
|
+
steps: results.map((result, index) => stepRecord(asked[index], result, names)),
|
|
122
|
+
...(value?.expect === "met" || value?.expect === "unmet" ? { expect: value.expect } : {}),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** The most refs a client keeps the names of: past it, the ones learned first go. */
|
|
126
|
+
const MAX_NAMED_REFS = 10_000;
|
|
127
|
+
/** The names a client's ui.observe answers gave each ref: role and name (never a value), the name whole up to
|
|
128
|
+
* MAX_KEPT_NAME (the row cuts it, after the redaction: shownName).
|
|
129
|
+
* Each observe's nodes are learned again (a ref's element keeps its ref across states, so its newest name wins). */
|
|
130
|
+
export class RefNames {
|
|
131
|
+
#byRef = new Map();
|
|
132
|
+
learn(answer) {
|
|
133
|
+
const nodes = obj(obj(answer)?.value)?.nodes;
|
|
134
|
+
if (!Array.isArray(nodes))
|
|
135
|
+
return;
|
|
136
|
+
for (const node of nodes) {
|
|
137
|
+
const one = obj(node);
|
|
138
|
+
if (typeof one?.ref !== "string" || typeof one.role !== "string")
|
|
139
|
+
continue;
|
|
140
|
+
const name = typeof one.name === "string" ? one.name.replace(/\s+/g, " ").trim() : "";
|
|
141
|
+
this.#byRef.delete(one.ref);
|
|
142
|
+
this.#byRef.set(one.ref, { role: one.role, ...(name && name.length <= MAX_KEPT_NAME ? { name } : {}) });
|
|
143
|
+
}
|
|
144
|
+
for (const ref of this.#byRef.keys()) {
|
|
145
|
+
if (this.#byRef.size <= MAX_NAMED_REFS)
|
|
146
|
+
break;
|
|
147
|
+
this.#byRef.delete(ref);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
of = (ref) => this.#byRef.get(ref);
|
|
151
|
+
}
|
|
152
|
+
const scope = new AsyncLocalStorage();
|
|
153
|
+
/** Runs `work` with every call a recordedClient makes inside it recorded, in the order the calls were made, and hands
|
|
154
|
+
* the records (with how many past MAX_RECORDED_CALLS were dropped) to `keep` once it settled, thrown or not, when it
|
|
155
|
+
* made any. `keep` is waited for and never fails the work. A scope inside another records into the inner one only. */
|
|
156
|
+
export async function recordingCalls(work, keep) {
|
|
157
|
+
const mine = { calls: [], dropped: 0 };
|
|
158
|
+
try {
|
|
159
|
+
return await scope.run(mine, work);
|
|
160
|
+
}
|
|
161
|
+
finally {
|
|
162
|
+
if (mine.calls.length > 0)
|
|
163
|
+
await keep(mine.calls, mine.dropped).catch(() => undefined);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** Runs `work` outside any recordingCalls scope: its calls are recorded on no tool call (the owner's, through the
|
|
167
|
+
* channel, which the runner writes a record of its own for). */
|
|
168
|
+
export function unrecorded(work) {
|
|
169
|
+
return scope.exit(work);
|
|
170
|
+
}
|
|
171
|
+
/** `client`, with each call made inside a recordingCalls scope recorded there when it settles. A call outside any scope
|
|
172
|
+
* (the owner's, through the channel, or the runner's own) is recorded nowhere; its ui.observe answer still names the
|
|
173
|
+
* refs a later recorded step acts on. */
|
|
174
|
+
export function recordedClient(client) {
|
|
175
|
+
const names = new RefNames();
|
|
176
|
+
// Bound: a provider's DaemonClient reads its own fields through `this`.
|
|
177
|
+
const send = client.call.bind(client);
|
|
178
|
+
return {
|
|
179
|
+
call: (async (method, params, options) => {
|
|
180
|
+
const into = scope.getStore();
|
|
181
|
+
if (!into) {
|
|
182
|
+
const answer = await send(method, params, options);
|
|
183
|
+
if (method === "ui.observe")
|
|
184
|
+
names.learn(answer);
|
|
185
|
+
return answer;
|
|
186
|
+
}
|
|
187
|
+
// Its place is kept when it starts, so calls made together read in the order they were made, and the bound keeps
|
|
188
|
+
// the first ones made. A call still going when its tool call ended reads unfinished.
|
|
189
|
+
const startedAt = Date.now();
|
|
190
|
+
const app = appOf(method, params);
|
|
191
|
+
const open = { method, started_at: new Date(startedAt).toISOString(), ...(app ? { app } : {}), ok: false };
|
|
192
|
+
const slot = into.calls.length < MAX_RECORDED_CALLS ? into.calls.push({ ...open, ms: 0, code: "unfinished" }) - 1 : undefined;
|
|
193
|
+
if (slot === undefined)
|
|
194
|
+
into.dropped += 1;
|
|
195
|
+
const started = performance.now();
|
|
196
|
+
const answer = await send(method, params, options).catch((error) => {
|
|
197
|
+
if (slot !== undefined)
|
|
198
|
+
into.calls[slot] = { ...open, ms: Math.round(performance.now() - started), code: "gone" };
|
|
199
|
+
throw error;
|
|
200
|
+
});
|
|
201
|
+
if (method === "ui.observe")
|
|
202
|
+
names.learn(answer);
|
|
203
|
+
if (slot !== undefined)
|
|
204
|
+
into.calls[slot] = callRecordOf(method, params, answer, performance.now() - started, startedAt, names.of);
|
|
205
|
+
return answer;
|
|
206
|
+
}),
|
|
207
|
+
close: () => client.close(),
|
|
208
|
+
};
|
|
209
|
+
}
|