@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
package/dist/tool-gate.js
CHANGED
|
@@ -1,60 +1,38 @@
|
|
|
1
1
|
// The tool gate: one gate every tool call passes, on every harness (plan item 4, "approvals that pause and resume").
|
|
2
2
|
// Each tool has a permission: allow (it runs), deny (it is refused, and the model reads why), or ask (a person approves
|
|
3
|
-
// or denies it first). The
|
|
4
|
-
//
|
|
5
|
-
// the same park (harness-app.ts) and registers an executor for its approved calls.
|
|
3
|
+
// or denies it first). The runner's loops ask it before each call's intent (RunGate.decide): pi's tools, the kernel's
|
|
4
|
+
// host tools on claude-agent, codex and OpenCode, and the MCP gateway's calls alike.
|
|
6
5
|
//
|
|
7
6
|
// An MCP tool's setting may also carry a pin (pinRefusal): top-level arguments its calls must carry, each a JSON scalar
|
|
8
7
|
// or one of a list. A pin adds to a setting and is never a permission: the gate checks it after a deny and before an
|
|
9
8
|
// allow or an ask, so an allowed call not as pinned is refused unsent, and one under ask is refused before anyone is
|
|
10
9
|
// asked; an approved call is checked again before it runs. The agent's pin and the server's both hold.
|
|
11
10
|
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
//
|
|
15
|
-
//
|
|
16
|
-
//
|
|
17
|
-
//
|
|
11
|
+
// bash's setting in an agent's own tool settings may carry a policy the same way (policyRefusal): `policy: "read-only"`
|
|
12
|
+
// (bash-read-only.ts) reads each shell call's command and refuses one that could write, run code or read outside the
|
|
13
|
+
// run's folder, after a deny and a pin and before an allow or an ask, so approvals "never" lifts none of it; an approved
|
|
14
|
+
// call is decided again before it runs.
|
|
15
|
+
//
|
|
16
|
+
// An ask has one way to wait, every tool's alike (the owner's ruling of 11-04): it PARKS. The runner's loop answers the
|
|
17
|
+
// call with waitingText and ends the turn, as ask_person's does; the run goes needs_input with the pending approval. On
|
|
18
|
+
// an answer the loop runs the approved call once, through the journal, and resumes the session with a message naming the
|
|
19
|
+
// call (resumeText). One call waits at a time: another asked meanwhile is refused.
|
|
18
20
|
//
|
|
19
21
|
// The answer: POST /v2/runs/{id}/approvals/{approval_id} { decision: "allow" | "deny", note? } -> 204. The first answer
|
|
20
22
|
// wins; the same answer again is 204, a conflicting one 409. Only the owner's password session answers (server.ts), never
|
|
21
|
-
// a token. A cancel marks pending approvals canceled.
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
// In the run's process: the asked call's request is kept in the run's own store (an `agentrun.tool_approval`
|
|
25
|
-
// entry on its root conversation), so a restart keeps it. The server keeps a person's answer in the run's serve.json
|
|
26
|
-
// (runs.ts); the gate reads it there, runs an allowed call once through the kernel's journal (the intent-then-outcome
|
|
27
|
-
// effect any host tool's call is, on a session key of its own: a call a crash cut off runs again only where it is
|
|
28
|
-
// replay-safe, and is answered interrupted otherwise) on the run's own coding source, and resumes the session with one
|
|
29
|
-
// message that names the call and carries its result, or the refusal. A result is cut as every host tool's is
|
|
30
|
-
// (HOST_RESULT_MAX_CHARS); the gate spills nothing.
|
|
31
|
-
//
|
|
32
|
-
// A stop or a cancel wins over an answer (runs.ts supersede): the gate reads the answer again before it writes the
|
|
33
|
-
// call's intent, cuts an approved call in flight when its answer is withdrawn, and reads it again once the call is
|
|
34
|
-
// done. Two windows of milliseconds remain, the same class as a stop racing any tool call inside a turn:
|
|
35
|
-
// - A stop that lands between the read before the intent and drive.effect's intent lets a short call that does not
|
|
36
|
-
// heed its abort run. Its result is dropped (the read after the call), never resumed.
|
|
37
|
-
// - A stop that lands between the read after the call and resume()'s commit can start the resume turn on a stopped
|
|
38
|
-
// run: a stop, unlike a cancel, leaves no mark the resume is held to.
|
|
39
|
-
import { createHash } from "node:crypto";
|
|
40
|
-
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
41
|
-
import { join } from "node:path";
|
|
42
|
-
import { defineExtension, ROOT_CONVERSATION_ID, wrapTool } from "@earendil-works/pi-durable";
|
|
43
|
-
import { DESKTOP_TOOL_DEFS, EffectFailure, INTERRUPTED_TEXT, openDrive, PHONE_TOOL_DEFS } from "@parcha/agentrun-kernel/kernel";
|
|
44
|
-
import { boundResult } from "@parcha/agentrun-kernel/process";
|
|
23
|
+
// a token. A cancel marks pending approvals canceled. Where no one can approve (accounts off), an ask is refused at once
|
|
24
|
+
// in words, never parked.
|
|
25
|
+
import { defaultPermissionOf, DESKTOP_TOOL_DEFS, PHONE_TOOL_DEFS } from "@parcha/agentrun-kernel/kernel";
|
|
45
26
|
import { READ_SKILL_FILE } from "@parcha/agentrun-kernel";
|
|
46
|
-
import { BARRIER, MESSAGE_ORIGIN_KIND, syncNow } from "@parcha/pi-durable-disk";
|
|
47
|
-
import { dataOfRun, readJson, RUN_META } from "./files.js";
|
|
48
27
|
import { LEARNED_DIR } from "./learning.js";
|
|
49
28
|
import { COMPUTER_ROOT, portPath } from "./workspace/paths.js";
|
|
50
|
-
import { forModel } from "./result.js";
|
|
51
29
|
import { builtinHarness } from "./harness-table.js";
|
|
52
30
|
import { DEFAULT_HARNESS } from "./harness.js";
|
|
53
|
-
import { durableSessions } from "./sessions.js";
|
|
54
31
|
import { SHELL_TOOLS } from "./background.js";
|
|
55
|
-
import {
|
|
56
|
-
import { deniedText,
|
|
32
|
+
import { SERVE_OWN_TOOLS, TABLE_TOOLS } from "./tool-plane.js";
|
|
33
|
+
import { deniedText, NOT_RUN, RAN_UNKNOWN } from "./refusal-words.js";
|
|
57
34
|
import { approvalsOf } from "./approvals.js";
|
|
35
|
+
import { BASH_POLICIES, readOnlyReason, readOnlyText } from "./bash-read-only.js";
|
|
58
36
|
/** What the gate does with a tool's calls. */
|
|
59
37
|
export const PERMISSIONS = ["allow", "ask", "deny"];
|
|
60
38
|
/** An MCP tool's gate name: `mcp:<server>/<tool>`. */
|
|
@@ -63,8 +41,14 @@ export const mcpToolName = (server, tool) => `mcp:${server}/${tool}`;
|
|
|
63
41
|
export const isMcpTool = (name) => /^mcp:[^/]+\/.+$/.test(name);
|
|
64
42
|
/** A third-party catalog tool, by its name: monid's (`monid.<tool>`) or an MCP server's (`mcp:<server>/<tool>`). */
|
|
65
43
|
export const isCatalogTool = (name) => name.startsWith("monid.") || isMcpTool(name);
|
|
66
|
-
/** The owner's defaults (11-01): a built-in kernel tool allow; a third-party catalog tool ask.
|
|
67
|
-
|
|
44
|
+
/** The owner's defaults (11-01): a built-in kernel tool allow; a third-party catalog tool ask. On a run with no computer,
|
|
45
|
+
* the kernel's ruled rule (defaultPermissionOf): bash asks first, and the background tools, which need a computer, are
|
|
46
|
+
* refused. */
|
|
47
|
+
export const defaultPermission = (name, where = {}) => {
|
|
48
|
+
if (isCatalogTool(name))
|
|
49
|
+
return "ask";
|
|
50
|
+
return where.computer === false ? defaultPermissionOf(name, { computer: false }) : "allow";
|
|
51
|
+
};
|
|
68
52
|
/** The tools that run a command in the run's box (background.ts SHELL_TOOLS), each held to bash's setting: bash: deny
|
|
69
53
|
* refuses bash_background as it refuses bash, and none of them is a settings key of its own. Under bash: ask the
|
|
70
54
|
* watchers (SHELL_WATCHERS) run unasked. */
|
|
@@ -108,8 +92,8 @@ const HOST_TOOLS_SERVER = "hands";
|
|
|
108
92
|
* wire name) and a harness's own (its facts' toolAlias: OpenCode's `<server>_<tool>`), is `mcp:<server>/<tool>`; a catalog's wire name (`monid_run`) is its
|
|
109
93
|
* catalog name; the browser's registrations are `browser`; an adapter's read_skill_file is `read` (but an agent's MCP tool of that name is its own). OpenCode's `<server>_<tool>` maps only for a server of
|
|
110
94
|
* `mcpServers` (the run's configured MCP servers), never by its shape, since a host tool's name has the same shape.
|
|
111
|
-
* Undefined: nothing maps it, and the gate asks. The key only:
|
|
112
|
-
*
|
|
95
|
+
* Undefined: nothing maps it, and the gate asks. The key only: the runner's adapter loop asks the gate once per call,
|
|
96
|
+
* before its intent (RunGate.decide). */
|
|
113
97
|
export function settingsKeyOf(harness, name, mcpServers = []) {
|
|
114
98
|
// A tool that runs a command in the box (bash_background and the rest) is held to bash's key: before the table's own names.
|
|
115
99
|
if (Object.hasOwn(SHELL_FAMILY, name))
|
|
@@ -215,14 +199,14 @@ export function pinRefusal(name, args, server, agent = {}, ran = "no") {
|
|
|
215
199
|
* layer, nor an ask that stands in for a setting that does not read. Nor is anything RunGate.enter refuses before it
|
|
216
200
|
* decides between running and parking ever lifted (a template's pin, among them). Without never, the answer is as it
|
|
217
201
|
* always was. */
|
|
218
|
-
export function permissionFrom(name, server, agent = {}, approvals = "ask") {
|
|
219
|
-
const { permission, from } = settingOf(name, server, agent);
|
|
202
|
+
export function permissionFrom(name, server, agent = {}, approvals = "ask", where = {}) {
|
|
203
|
+
const { permission, from } = settingOf(name, server, agent, where);
|
|
220
204
|
if (approvals !== "never" || permission !== "ask" || standsBehind(name, server, agent))
|
|
221
205
|
return { permission, from };
|
|
222
206
|
return { permission: "allow", from: "unattended" };
|
|
223
207
|
}
|
|
224
|
-
/** Marks the server's settings map when its
|
|
225
|
-
* tool then takes its defaults, and
|
|
208
|
+
/** Marks the server's settings map when its stored value does not read (tool-plane.ts loadToolSettings): every
|
|
209
|
+
* tool then takes its defaults, and a deny it may hold can't be seen, so nothing lifts an ask over it. A symbol:
|
|
226
210
|
* carried by a spread, never by JSON (it is never served) nor by Object.entries (no reader takes it for a tool). */
|
|
227
211
|
export const SETTINGS_UNREAD = Symbol("agentrun.tool-settings.unread");
|
|
228
212
|
/** `settings` marked unread (SETTINGS_UNREAD), a copy: what a gate holds to while its settings' read fails, its last good
|
|
@@ -244,15 +228,18 @@ function standsBehind(name, server, agent) {
|
|
|
244
228
|
}));
|
|
245
229
|
}
|
|
246
230
|
/** permissionFrom before approvals: what the layers and the default say. */
|
|
247
|
-
function settingOf(name, server, agent) {
|
|
231
|
+
function settingOf(name, server, agent, where) {
|
|
248
232
|
// A shell tool's permission is bash's (SHELL_FAMILY), from bash's layer, wherever it is read: the gate, capabilities, an
|
|
249
233
|
// agent's tools. A watcher (SHELL_WATCHERS) runs unasked only under an ask set as written in a layer: an ask that stands
|
|
250
234
|
// in for anything else (a value not offered, nothing set) stays an ask. No key of its own is read, so none lifts bash's.
|
|
251
235
|
if (Object.hasOwn(SHELL_FAMILY, name) && SHELL_FAMILY[name] !== name) {
|
|
252
|
-
const bash = settingFrom(SHELL_FAMILY[name], server, agent);
|
|
236
|
+
const bash = settingFrom(SHELL_FAMILY[name], server, agent, where);
|
|
237
|
+
// Nothing set, on a run with no computer: the tool's own default (defaultPermissionOf: the background tools refused).
|
|
238
|
+
if (bash.from === "default" && where.computer === false)
|
|
239
|
+
return { permission: defaultPermission(name, where), from: "default" };
|
|
253
240
|
return { permission: bash.written ? shellPermission(name, bash.permission) : bash.permission, from: bash.from };
|
|
254
241
|
}
|
|
255
|
-
const { permission, from } = settingFrom(name, server, agent);
|
|
242
|
+
const { permission, from } = settingFrom(name, server, agent, where);
|
|
256
243
|
return { permission, from };
|
|
257
244
|
}
|
|
258
245
|
/** permissionFrom's own read, and whether the permission is `written`: a valid one set in a layer as it reads there, not
|
|
@@ -261,7 +248,7 @@ function settingOf(name, server, agent) {
|
|
|
261
248
|
* wildcard) is deny, that is the answer, from the server, before the agent's own settings are read, so no ask or allow
|
|
262
249
|
* of the agent's reaches past it. The server's own exact allow under its wildcard deny is its answer, as before.
|
|
263
250
|
* Otherwise the agent's own setting comes first, then the server's, then the default. */
|
|
264
|
-
function settingFrom(name, server, agent) {
|
|
251
|
+
function settingFrom(name, server, agent, where = {}) {
|
|
265
252
|
const own = permissionKeys(name).map((key) => server[key]).find((setting) => setting?.permission !== undefined);
|
|
266
253
|
if (own?.permission === "deny")
|
|
267
254
|
return { permission: "deny", from: "server", written: own.fallback !== true };
|
|
@@ -276,67 +263,22 @@ function settingFrom(name, server, agent) {
|
|
|
276
263
|
return { permission: set, from, written: setting.fallback !== true };
|
|
277
264
|
}
|
|
278
265
|
}
|
|
279
|
-
return { permission: defaultPermission(name), from: "default", written: false };
|
|
266
|
+
return { permission: defaultPermission(name, where), from: "default", written: false };
|
|
280
267
|
}
|
|
281
268
|
/** `name`'s permission in force (permissionFrom): the one rule every path reads, the gate's, the tool plane's and a
|
|
282
269
|
* workflow's call nodes'. */
|
|
283
270
|
export const permissionOf = (name, server, agent = {}, approvals = "ask") => permissionFrom(name, server, agent, approvals).permission;
|
|
284
|
-
/** The mark a tool registration carries when its calls are gated by whoever registered it (the MCP gateway, which marks
|
|
285
|
-
* its registrations only when it holds this run's gate and park, and enters the gate in each call): RunGate.extension
|
|
286
|
-
* does not wrap it again. A private symbol, never `Symbol.for`: a global-registry key could be recreated by any code
|
|
287
|
-
* with the string alone. Only code that imports this export can set it (mcp-gateway.ts does). */
|
|
288
|
-
export const GATED_BY_GATEWAY = Symbol("agentrun.gatedByGateway");
|
|
289
271
|
// The refusal words (SettingFrom, DenyFrom, NOT_RUN, RAN_UNKNOWN, denyPlace, deniedText, refusedText) live in
|
|
290
272
|
// refusal-words.ts, shared with the runner's loops.
|
|
291
273
|
export { deniedText, denyPlace, NOT_RUN, RAN_UNKNOWN, refusedText } from "./refusal-words.js";
|
|
292
274
|
/** The refusal of an ask where no one can approve it: a server with accounts off, whose answer route takes no token. */
|
|
293
275
|
export const noApproverText = (tool, ran = "no") => `Permission denied: ${tool} needs the owner's approval, and this server has accounts off (AGENTRUN_ACCOUNTS=0), so no one can give it: set the tool to allow or deny in the agent's Tools. ${ran === "no" ? NOT_RUN : RAN_UNKNOWN}`;
|
|
294
|
-
/** Set by the server in a run's environment when no one can approve here (accounts off): the gate refuses each ask. */
|
|
295
|
-
export const NO_APPROVER_ENV = "AGENTRUN_SERVE_NO_APPROVER";
|
|
296
276
|
/** A parked call's answer: the run pauses here. */
|
|
297
277
|
export const waitingText = (tool) => `This ${tool} call waits for a person's approval; the run pauses here and continues with the result when they answer.`;
|
|
298
278
|
/** The Timeline row of a call that waits for an approval. */
|
|
299
279
|
export const APPROVAL_ROW = "tool_approval_requested";
|
|
300
280
|
/** A call's arguments shown on its approval, at most this many bytes. */
|
|
301
281
|
export const ARGS_PREVIEW_BYTES = 2048;
|
|
302
|
-
/** A gate of `policy` (the run's permission of a tool now) with no run behind it: allow and deny are enforced and
|
|
303
|
-
* executors are kept; an ask throws, since nothing here can park a call (a run's is RunGate), and spill returns
|
|
304
|
-
* the text as it is. */
|
|
305
|
-
export function gateOf(policy) {
|
|
306
|
-
const executors = new Map();
|
|
307
|
-
return {
|
|
308
|
-
async enter(tool) {
|
|
309
|
-
const permission = policy(tool);
|
|
310
|
-
if (permission === "allow")
|
|
311
|
-
return undefined;
|
|
312
|
-
if (permission === "deny")
|
|
313
|
-
return { text: deniedText(tool), isError: true };
|
|
314
|
-
throw new Error(`A gate with no run cannot ask a person about ${tool}.`);
|
|
315
|
-
},
|
|
316
|
-
executor(prefix, run) {
|
|
317
|
-
executors.set(prefix, run);
|
|
318
|
-
},
|
|
319
|
-
spill(_callId, text) {
|
|
320
|
-
return text;
|
|
321
|
-
},
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
/** The file in a run's directory that names the origin of the turn it runs now: written by the server alone, as it sends
|
|
325
|
-
* the learning lane's reflect turn (runs.ts's toLiveDoor), and gone before a person's turn is sent (runs.ts's personsTurn).
|
|
326
|
-
* The gate refuses that turn's writes under work/.learning/ (NOTE_ONLY_TEXT). */
|
|
327
|
-
export const TURN_ORIGIN_FILE = "turn-origin.json";
|
|
328
|
-
/** The run's store's record of a call that waits for approval (`phase: "requested"`), and of its answer acted on
|
|
329
|
-
* (`phase: "resolved"`), on its root conversation. */
|
|
330
|
-
export const APPROVAL_ENTRY = "agentrun.tool_approval";
|
|
331
|
-
/** The run's store's record that the gate let a replay-safe pi call that can bill through (`{ callId }`), written before
|
|
332
|
-
* the tool runs. pi-durable reruns a cut replay-safe call with the same call id and no sign that it is a rerun: this mark
|
|
333
|
-
* is how the gate knows an earlier attempt may have been sent, so a deny of the rerun never says nothing was charged.
|
|
334
|
-
* Outside `agentrun.tool_`, the tool ledger's kinds (paid-calls.ts), which readers of a run's charges take by prefix. */
|
|
335
|
-
export const GATE_PASSED_ENTRY = "agentrun.gate_passed";
|
|
336
|
-
/** The replay-safe tools, by settings key, whose call can be charged (a provider's fee: Parallel's search or extract): the
|
|
337
|
-
* only ones the gate marks, so the root conversation does not grow by a mark per free call (read, the shell watchers,
|
|
338
|
-
* monid's discover and inspect), whose earlier attempt charged nothing. monid.run is never rerun (host-tools.ts REPLAY). */
|
|
339
|
-
const BILLED_REPLAYS = new Set(["web_fetch", "web_search"]);
|
|
340
282
|
/** The refusal of the end-of-run turn's write or edit under work/.learning/: its notes are kept only from its answer's
|
|
341
283
|
* note blocks (learn/reflect.ts parseReflection), which the host merges in its own store; a file there is never kept. */
|
|
342
284
|
export const NOTE_ONLY_TEXT = "A note is kept only from your answer: write it in a ```note block, not to a file.";
|
|
@@ -373,80 +315,57 @@ export function resumeText(request, shown, decision, result) {
|
|
|
373
315
|
const head = resumeHead(request, shown);
|
|
374
316
|
return decision === "allow" ? `${head} They allowed it, and it ran${result.isError ? " and failed" : ""}. Its result:\n${result.text}` : `${head}\n${result.text}`;
|
|
375
317
|
}
|
|
376
|
-
/** The gate of the run `runId`
|
|
377
|
-
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
export function runGate(where, runId, revision, serverSettings, files, approvers = process.env[NO_APPROVER_ENV] !== "1") {
|
|
383
|
-
return new RunGate({ root: where.root, work: where.work, data: dataOfRun(where.root), runId, harness: revision.body.harness ?? DEFAULT_HARNESS }, (revision.body.toolSettings ?? {}), revision.kind === "agentic", approvers, approvalsOf(revision.body.approvals), serverSettings, files);
|
|
318
|
+
/** The gate of the run `runId` whose folder is `where`, on its pinned revision. `serverSettings`: the server's tool
|
|
319
|
+
* settings as the runner read them from the one store at the claim (tool-plane.ts loadToolSettings), read at each call as
|
|
320
|
+
* the gate reads them. `approvers`: whether anyone can answer an ask here, the store's (server-approvers.ts, read at each
|
|
321
|
+
* claim). The gate reads nothing of the run's own files: they are the agent's to write. */
|
|
322
|
+
export function runGate(where, runId, revision, serverSettings, approvers) {
|
|
323
|
+
return new RunGate({ root: where.root, work: where.work, runId, harness: revision.body.harness ?? DEFAULT_HARNESS, ...(where.computer === undefined ? {} : { computer: where.computer }) }, serverSettings, (revision.body.toolSettings ?? {}), approvers, approvalsOf(revision.body.approvals));
|
|
384
324
|
}
|
|
385
|
-
/**
|
|
325
|
+
/** A run's tool gate on the runner: its policy, asked before each call's intent (decide). */
|
|
386
326
|
export class RunGate {
|
|
387
327
|
where;
|
|
328
|
+
readServerSettings;
|
|
388
329
|
overrides;
|
|
389
|
-
parks;
|
|
390
330
|
approvers;
|
|
391
331
|
approvals;
|
|
392
|
-
readServerSettings;
|
|
393
|
-
files;
|
|
394
|
-
requests = new Map();
|
|
395
|
-
/** The replay-safe pi calls the gate let through (GATE_PASSED_ENTRY), by call id: a rerun of one is a replay. */
|
|
396
|
-
passed = new Set();
|
|
397
|
-
durable;
|
|
398
|
-
/** The run's store, for the barrier before a call that is not replay-safe: from `prime` (before the resume) or `load`. */
|
|
399
|
-
syncTarget;
|
|
400
|
-
context;
|
|
401
|
-
coding;
|
|
402
|
-
/** The run's own tools beyond the table (its learned_workflow), on which an approved call of one runs. */
|
|
403
|
-
extra = [];
|
|
404
|
-
resume;
|
|
405
|
-
/** Told just before an allowed call runs (harness-app's): the run is un-parked, so the call's own model requests (a
|
|
406
|
-
* learned workflow's node) go out; the harness is idle then, as the park ended its turn. */
|
|
407
|
-
unpark;
|
|
408
|
-
drive;
|
|
409
|
-
timer;
|
|
410
|
-
settling = false;
|
|
411
|
-
stop = new AbortController();
|
|
412
|
-
/** `where.data`: the server's data directory, its tool settings beside the store; `overrides`: the pinned revision's
|
|
413
|
-
* own; `parks`: whether an asked call parks the run (an agentic run's), which a workflow's model node cannot;
|
|
332
|
+
/** `readServerSettings`: the server's tool settings, read at each call; `overrides`: the pinned revision's own;
|
|
414
333
|
* `approvers`: whether anyone can answer an ask here (the owner's password session: accounts on); `approvals`: the
|
|
415
|
-
* pinned revision's (definition.approvals): "never" runs the agent unattended, an ask allowed (permissionFrom)
|
|
416
|
-
|
|
417
|
-
constructor(where, overrides = {}, parks = true, approvers = true, approvals = "ask", readServerSettings, files = where.root) {
|
|
334
|
+
* pinned revision's (definition.approvals): "never" runs the agent unattended, an ask allowed (permissionFrom). */
|
|
335
|
+
constructor(where, readServerSettings, overrides = {}, approvers = true, approvals = "ask") {
|
|
418
336
|
this.where = where;
|
|
337
|
+
this.readServerSettings = readServerSettings;
|
|
419
338
|
this.overrides = overrides;
|
|
420
|
-
this.parks = parks;
|
|
421
339
|
this.approvers = approvers;
|
|
422
340
|
this.approvals = approvals;
|
|
423
|
-
this.readServerSettings = readServerSettings;
|
|
424
|
-
this.files = files;
|
|
425
341
|
}
|
|
426
342
|
/** A tool's permission now (permissionOf): the revision's own over the server's setting, read at each call, so a change
|
|
427
343
|
* reaches a live run at its next call; under approvals "never", an ask is an allow. */
|
|
428
344
|
policy = (tool) => this.setting(tool).permission;
|
|
429
345
|
/** The gate's answer for one call before its intent, for a loop that parks and answers asks itself (the runner's adapter
|
|
430
|
-
* loops, pi's and the CLI harnesses'): run it, ask a person, or refuse it with the words the model reads.
|
|
431
|
-
*
|
|
432
|
-
*
|
|
346
|
+
* loops, pi's and the CLI harnesses'): run it, ask a person, or refuse it with the words the model reads. The order:
|
|
347
|
+
* the deny (with where it was set, and that nothing was sent or charged), a pin, bash's policy (policyRefusal), then
|
|
348
|
+
* the allow, then the ask, so an allow that approvals "never" made is decided
|
|
433
349
|
* only after every refusal. Where no one can approve (accounts off: the store's approvers row a runner reads at each
|
|
434
|
-
* claim, server-approvers.ts), the ask is refused at once in words, never parked, as `enter` refuses it.
|
|
435
|
-
|
|
350
|
+
* claim, server-approvers.ts), the ask is refused at once in words, never parked, as `enter` refuses it. `replayed`:
|
|
351
|
+
* the call is run again after an earlier attempt that may have been sent (a replay-safe call rerun after a crash), so a
|
|
352
|
+
* refusal says whether it took effect is not known, never that nothing was sent. */
|
|
353
|
+
async decide(name, args, options = {}) {
|
|
436
354
|
const key = settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, name, this.where.mcpServers) ?? name;
|
|
437
355
|
const tool = this.askedAs(name);
|
|
438
|
-
const
|
|
439
|
-
if (learning)
|
|
440
|
-
return { deny: learning };
|
|
356
|
+
const ran = options.replayed ? "unknown" : "no";
|
|
441
357
|
const verdict = this.policy(name);
|
|
442
358
|
if (verdict === "deny")
|
|
443
|
-
return { deny: deniedText(tool, key, this.denyFrom(name),
|
|
444
|
-
const pinned = this.pinned(key, args,
|
|
359
|
+
return { deny: deniedText(tool, key, this.denyFrom(name), ran) };
|
|
360
|
+
const pinned = this.pinned(key, args, ran);
|
|
445
361
|
if (pinned)
|
|
446
362
|
return { deny: pinned.text };
|
|
363
|
+
const policed = this.policyRefusal(key, name, args);
|
|
364
|
+
if (policed)
|
|
365
|
+
return { deny: policed };
|
|
447
366
|
if (verdict === "allow")
|
|
448
367
|
return "allow";
|
|
449
|
-
return this.approvers ? "ask" : { deny: noApproverText(tool,
|
|
368
|
+
return this.approvers ? "ask" : { deny: noApproverText(tool, ran) };
|
|
450
369
|
}
|
|
451
370
|
/** The name a call is asked about by, as main's request names it (enter's `tool`): its settings key, a shell tool its
|
|
452
371
|
* own name; a name nothing maps, as it came. */
|
|
@@ -475,19 +394,37 @@ export class RunGate {
|
|
|
475
394
|
// No settings ever read: every tool asks, the shell watchers too (fail closed: an ask nothing read is never lifted),
|
|
476
395
|
// or keeps the revision's own deny. Approvals "never" lifts none of these asks: the server's settings may hold a deny
|
|
477
396
|
// no read has seen.
|
|
397
|
+
const where = { computer: this.where.computer };
|
|
478
398
|
if (server === undefined) {
|
|
479
|
-
const own = permissionFrom(key, {}, this.overrides);
|
|
399
|
+
const own = permissionFrom(key, {}, this.overrides, "ask", where);
|
|
480
400
|
return own.permission === "deny" ? own : { permission: "ask", from: "default" };
|
|
481
401
|
}
|
|
482
402
|
// A shell tool under every harness's name for it is read as its own name, by bash's key (permissionFrom): a watcher's
|
|
483
|
-
// ask is allow only where bash's ask is set as written.
|
|
484
|
-
return permissionFrom(shellToolOf(tool) ?? key, server, this.overrides, this.approvals);
|
|
403
|
+
// ask is allow only where bash's ask is set as written. Its default is where the run's shell is (defaultPermission).
|
|
404
|
+
return permissionFrom(shellToolOf(tool) ?? key, server, this.overrides, this.approvals, where);
|
|
485
405
|
}
|
|
486
406
|
/** Why a pin refuses the call of `key` with `args` now (pinRefusal): the revision's own pins and the server's, read at
|
|
487
407
|
* each call as its permission is, so a pin set or changed reaches a live run at its next call. */
|
|
488
408
|
pinned(key, args, ran = "no") {
|
|
489
409
|
return pinRefusal(key, args, this.serverSettings() ?? {}, this.overrides, ran);
|
|
490
410
|
}
|
|
411
|
+
/** Why bash's policy refuses the call `name` (held to `key`) with `args` now, or undefined: the revision's own setting
|
|
412
|
+
* (an agent's tool_settings.bash.policy), so every shell tool under every harness's name for it is held to it. A policy
|
|
413
|
+
* this server does not know refuses every shell call (fail closed), as a pin that does not read does. */
|
|
414
|
+
policyRefusal(key, name, args) {
|
|
415
|
+
if (key !== "bash")
|
|
416
|
+
return undefined;
|
|
417
|
+
const policy = this.overrides.bash?.policy;
|
|
418
|
+
if (policy === undefined)
|
|
419
|
+
return undefined;
|
|
420
|
+
// Its own name for a background tool (bash_background, under claude-agent's mcp__hands__ name too); bash for bash and
|
|
421
|
+
// a CLI's own shell (codex's shell, claude-agent's Bash).
|
|
422
|
+
const tool = shellToolOf(name) ?? "bash";
|
|
423
|
+
if (!BASH_POLICIES.includes(policy))
|
|
424
|
+
return `${tool} can't be called: this agent's bash policy ${JSON.stringify(policy).slice(0, 60)} is not one this server knows (${BASH_POLICIES.join(", ")}), so no command is let through until its tool_settings name one. ${NOT_RUN}`;
|
|
425
|
+
const reason = readOnlyReason(tool, args, [this.where.work, COMPUTER_ROOT]);
|
|
426
|
+
return reason === undefined ? undefined : readOnlyText(tool, reason, NOT_RUN);
|
|
427
|
+
}
|
|
491
428
|
lastGood;
|
|
492
429
|
told = new Set();
|
|
493
430
|
/** The server's settings now: read at each call, so a change reaches a live run; settings that do not read keep the
|
|
@@ -495,7 +432,7 @@ export class RunGate {
|
|
|
495
432
|
* while the read fails, as a deny may be in what did not read (undefined: none read yet, every tool asks). Each problem
|
|
496
433
|
* is said once, by file and field, never a value. */
|
|
497
434
|
serverSettings() {
|
|
498
|
-
const read = this.readServerSettings
|
|
435
|
+
const read = this.readServerSettings();
|
|
499
436
|
for (const line of read.problems) {
|
|
500
437
|
if (this.told.has(line))
|
|
501
438
|
continue;
|
|
@@ -507,399 +444,4 @@ export class RunGate {
|
|
|
507
444
|
this.lastGood = read.settings;
|
|
508
445
|
return this.lastGood;
|
|
509
446
|
}
|
|
510
|
-
/** The origin of the turn running now: what the server's internal path wrote for it (TURN_ORIGIN_FILE in the run's
|
|
511
|
-
* folder, read through `files`: the run's Workspace or the local folder's path); none for a person's turn, or a file
|
|
512
|
-
* that is not there or does not read. */
|
|
513
|
-
async turnOrigin() {
|
|
514
|
-
try {
|
|
515
|
-
const text = typeof this.files === "string" ? readFileSync(join(this.files, TURN_ORIGIN_FILE), "utf8") : new TextDecoder().decode(await this.files.read(TURN_ORIGIN_FILE));
|
|
516
|
-
return JSON.parse(text)?.origin === "reflect" ? "reflect" : undefined;
|
|
517
|
-
}
|
|
518
|
-
catch {
|
|
519
|
-
return undefined;
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
/** The end-of-run turn's refusal of a write or edit under work/.learning/ (NOTE_ONLY_TEXT, the adapter loop's one text
|
|
523
|
-
* too), whatever the tool's permission: its notes are kept only from its answer. Undefined for any other call or turn,
|
|
524
|
-
* whose write there follows its permission, as anywhere. The first check of `enter` and `decide`. */
|
|
525
|
-
async learningRefusal(key, args) {
|
|
526
|
-
if (key !== "write" && key !== "edit")
|
|
527
|
-
return undefined;
|
|
528
|
-
// The folder's path a file tool resolves against: the local folder's, or a computer's name for it (the Workspace's paths
|
|
529
|
-
// are in the run's folder, so a path there is relative or under COMPUTER_ROOT).
|
|
530
|
-
const work = typeof this.files === "string" ? this.where.work : COMPUTER_ROOT;
|
|
531
|
-
return underLearning(work, args) && (await this.turnOrigin()) === "reflect" ? NOTE_ONLY_TEXT : undefined;
|
|
532
|
-
}
|
|
533
|
-
/** One model-initiated call at the gate: undefined when it may run; else its answer, refused at once (deny) or parked
|
|
534
|
-
* (ask). One call waits at a time: another asked meanwhile is answered so, not run. */
|
|
535
|
-
async enter(name, args, callId, park, options = {}) {
|
|
536
|
-
// Held to its settings key (settingsKeyOf), the name its request, a refusal and the approved call give; a shell tool
|
|
537
|
-
// held to bash's (SHELL_FAMILY) keeps its own name, so an approved bash_background runs bash_background, never bash.
|
|
538
|
-
const key = settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, name, this.where.mcpServers) ?? name;
|
|
539
|
-
const tool = shellToolOf(name) ?? key;
|
|
540
|
-
// The end-of-run turn writes no file under work/.learning/, whatever the tool's permission (learningRefusal).
|
|
541
|
-
const learning = await this.learningRefusal(key, args);
|
|
542
|
-
if (learning)
|
|
543
|
-
return { text: learning, isError: true };
|
|
544
|
-
// Every refusal that does not come from the permission (the reflect turn's above, the deny and a pin's below) stays
|
|
545
|
-
// ABOVE the allow's return further down. An allow here may be an ask the agent's approvals "never" turned into one
|
|
546
|
-
// (permissionFrom's last step), and an allowed call runs at once: nothing after that return refuses it.
|
|
547
|
-
const verdict = this.policy(name);
|
|
548
|
-
const ran = options.replayed ? "unknown" : "no";
|
|
549
|
-
if (verdict === "deny")
|
|
550
|
-
return { text: deniedText(tool, key, this.denyFrom(name), ran), isError: true };
|
|
551
|
-
// A pinned MCP tool's call whose arguments are not the pinned ones: refused before it runs or asks anyone, so an
|
|
552
|
-
// allowed or approved call goes only where its settings fix.
|
|
553
|
-
const pinned = this.pinned(key, args, ran);
|
|
554
|
-
if (pinned) {
|
|
555
|
-
process.stderr.write(`agentrun-serve: the tool gate refused ${key} call ${callId}: ${pinned.mismatched.length ? `${pinned.mismatched.join(", ")} not as pinned` : "its pin does not read"}\n`);
|
|
556
|
-
return { text: pinned.text, isError: true };
|
|
557
|
-
}
|
|
558
|
-
if (verdict === "allow")
|
|
559
|
-
return undefined;
|
|
560
|
-
// No one can answer here: refused at once with why, never parked until a cancel.
|
|
561
|
-
if (!this.approvers)
|
|
562
|
-
return { text: noApproverText(tool, ran), isError: true };
|
|
563
|
-
if (!this.parks)
|
|
564
|
-
return { text: modelNodeAskText(tool), isError: true };
|
|
565
|
-
// Named by the call, so a call run again after a crash parks as the same request.
|
|
566
|
-
const id = `ap_${createHash("sha256").update(JSON.stringify([callId, tool, args ?? null])).digest("hex").slice(0, 16)}`;
|
|
567
|
-
const other = [...this.requests.values()].find((held) => !held.resolved && held.id !== id);
|
|
568
|
-
if (other)
|
|
569
|
-
return { text: `This ${tool} call was not run: the ${other.tool} call ${other.call_id} already waits for a person's approval. Call it again once that one is answered.`, isError: true };
|
|
570
|
-
if (!this.requests.has(id)) {
|
|
571
|
-
const turn = park.turn?.();
|
|
572
|
-
const request = { id, tool, call_id: callId, args: args ?? null, args_preview: argsPreview(args), requested_at: new Date().toISOString(), ...(turn !== undefined ? { turn } : {}), ...(options.price !== undefined ? { price: options.price } : {}), ...(options.replayed ? { replayed: true } : {}) };
|
|
573
|
-
// Held before the commit's await, in the tick of the look above: pi runs a turn's calls in parallel, and an ask
|
|
574
|
-
// entered meanwhile finds this one and is refused. A commit that fails lets it go.
|
|
575
|
-
this.requests.set(id, request);
|
|
576
|
-
try {
|
|
577
|
-
await park.commit((tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: APPROVAL_ENTRY, data: { phase: "requested", ...request } }));
|
|
578
|
-
}
|
|
579
|
-
catch (error) {
|
|
580
|
-
this.requests.delete(id);
|
|
581
|
-
throw error;
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
park.parked?.(callId);
|
|
585
|
-
this.watch();
|
|
586
|
-
return { text: waitingText(tool), isError: false, parked: true };
|
|
587
|
-
}
|
|
588
|
-
/** pi's `tools` behind the gate: wraps, by name, for the run's registry, which apply wherever it is selected (every
|
|
589
|
-
* agent selects it: the root by default, a workflow's model node by name, app.ts). */
|
|
590
|
-
extension(tools) {
|
|
591
|
-
// Every tool is wrapped, an mcp__ name too, except a registration marked GATED_BY_GATEWAY: the MCP gateway marks the
|
|
592
|
-
// ones whose calls it takes to this gate itself (gate.enter in its call), so such a call passes one gate, once. Nothing
|
|
593
|
-
// is skipped by its name: an unmarked mcp__ tool is gated here as any tool is.
|
|
594
|
-
const named = [...new Map(tools.filter((tool) => tool[GATED_BY_GATEWAY] !== true).map((tool) => [tool.name, tool])).values()];
|
|
595
|
-
return defineExtension({ name: "tool-gate", wraps: named.map((tool) => wrapTool(tool, (inner) => this.wrap(inner))) });
|
|
596
|
-
}
|
|
597
|
-
wrap(tool) {
|
|
598
|
-
const gate = this;
|
|
599
|
-
const billed = tool.replay === "safe" && BILLED_REPLAYS.has(settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, tool.name, this.where.mcpServers) ?? "");
|
|
600
|
-
return {
|
|
601
|
-
...tool,
|
|
602
|
-
async execute(args, api, context) {
|
|
603
|
-
const commit = (change) => api.commit(change, context);
|
|
604
|
-
// A rerun of a replay-safe call the gate let through before (pi-durable reruns it with the same call id after a
|
|
605
|
-
// crash, with no sign of it): an earlier attempt may have been sent, which the gate's refusal must not deny. The
|
|
606
|
-
// marks were read before the run resumed (prime), so a rerun the resume dispatches finds them.
|
|
607
|
-
const answer = await gate.enter(tool.name, args, api.callId, { commit }, gate.passed.has(api.callId) ? { replayed: true } : {});
|
|
608
|
-
// A parked call ends the turn, as ask_person's does.
|
|
609
|
-
if (answer)
|
|
610
|
-
return forModel(answer.text, answer.parked ? { control: { terminate: true } } : { isError: true });
|
|
611
|
-
// Let through: a call that can bill is marked in the store before the tool runs, so a crash after it leaves a
|
|
612
|
-
// rerun the gate knows for one. A mark that fails to commit stops the call (nothing has been sent).
|
|
613
|
-
if (billed && !gate.passed.has(api.callId)) {
|
|
614
|
-
await commit((tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: GATE_PASSED_ENTRY, data: { callId: api.callId } }));
|
|
615
|
-
gate.passed.add(api.callId);
|
|
616
|
-
}
|
|
617
|
-
// pi-durable committed the call's intent before it reached execute: durable before the call is sent, so a lost
|
|
618
|
-
// box never sends it twice (a resumed call with its intent is reported interrupted, not run again).
|
|
619
|
-
if (tool.replay !== "safe" || billed)
|
|
620
|
-
await syncNow(gate.syncTarget, BARRIER.piToolIntent);
|
|
621
|
-
return tool.execute(args, api, context);
|
|
622
|
-
},
|
|
623
|
-
};
|
|
624
|
-
}
|
|
625
|
-
/** Takes the open run, as the run's process opens (its app's onOpen), reading what its store holds of the gate unless
|
|
626
|
-
* `prime` read it before the resume. `coding`: the run's coding source (coding-tools.ts), on which an approved coding
|
|
627
|
-
* call runs, as the run's own would; `extra`, the run's own tools beyond the table (its learned_workflow), on which an
|
|
628
|
-
* approved call of one runs. */
|
|
629
|
-
async load(durable, context, coding, extra = []) {
|
|
630
|
-
this.durable = durable;
|
|
631
|
-
this.syncTarget = durable.store;
|
|
632
|
-
this.context = context;
|
|
633
|
-
this.coding = coding;
|
|
634
|
-
this.extra = extra;
|
|
635
|
-
if (!this.primed)
|
|
636
|
-
await this.prime(durable.harness, context);
|
|
637
|
-
}
|
|
638
|
-
primed = false;
|
|
639
|
-
/** Reads what the run's store holds of the gate (the calls that wait, the answers acted on, the calls it let through)
|
|
640
|
-
* before the run resumes: its app's beforeResume hook, ahead of harness.resume(), which dispatches the calls a crash
|
|
641
|
-
* cut. Read here, not in onOpen, which a recovered call races. A workflow's run calls only this; load reads nothing more. */
|
|
642
|
-
async prime(harness, context, syncTarget) {
|
|
643
|
-
if (syncTarget)
|
|
644
|
-
this.syncTarget = syncTarget;
|
|
645
|
-
const root = await harness.conversation(ROOT_CONVERSATION_ID, context);
|
|
646
|
-
for (let cursor; root;) {
|
|
647
|
-
const page = await root.entries({ order: "ascending" }, 500, cursor, context);
|
|
648
|
-
for (const entry of page.items)
|
|
649
|
-
this.note(entry);
|
|
650
|
-
if (page.next === undefined)
|
|
651
|
-
break;
|
|
652
|
-
cursor = page.next;
|
|
653
|
-
}
|
|
654
|
-
this.primed = true;
|
|
655
|
-
}
|
|
656
|
-
/** Whether a call waits for its answer, or for its answer to be acted on: a harness keeps the turn it ended closed. */
|
|
657
|
-
waiting() {
|
|
658
|
-
return [...this.requests.values()].some((held) => !held.resolved);
|
|
659
|
-
}
|
|
660
|
-
/** Whether a call of the turn that took `message` was parked, answered or not: that turn ended at the park, though a
|
|
661
|
-
* process that went before the next open settled it leaves it open in its session. */
|
|
662
|
-
parkedIn(message) {
|
|
663
|
-
return message !== undefined && [...this.requests.values()].some((held) => held.turn === message);
|
|
664
|
-
}
|
|
665
|
-
/** Acts on the answers from now on, resuming the session through `resume`; by default pi's root conversation. */
|
|
666
|
-
start(resume, unpark) {
|
|
667
|
-
this.resume = resume ?? this.submit;
|
|
668
|
-
this.unpark = unpark;
|
|
669
|
-
this.watch();
|
|
670
|
-
}
|
|
671
|
-
note(entry) {
|
|
672
|
-
if (entry.kind === GATE_PASSED_ENTRY) {
|
|
673
|
-
const callId = entry.data?.callId;
|
|
674
|
-
if (typeof callId === "string")
|
|
675
|
-
this.passed.add(callId);
|
|
676
|
-
return;
|
|
677
|
-
}
|
|
678
|
-
const data = entry.data;
|
|
679
|
-
if (entry.kind !== APPROVAL_ENTRY || typeof data?.id !== "string")
|
|
680
|
-
return;
|
|
681
|
-
const held = this.requests.get(data.id);
|
|
682
|
-
if (data.phase === "requested" && !held)
|
|
683
|
-
this.requests.set(data.id, data);
|
|
684
|
-
else if (data.phase === "resolved" && held)
|
|
685
|
-
held.resolved = true;
|
|
686
|
-
}
|
|
687
|
-
/** Reads the run's serve.json for answers while a call waits. */
|
|
688
|
-
watch() {
|
|
689
|
-
if (this.timer || !this.resume || !this.waiting())
|
|
690
|
-
return;
|
|
691
|
-
this.timer = setInterval(() => void this.settle(), 250);
|
|
692
|
-
this.timer.unref();
|
|
693
|
-
void this.settle();
|
|
694
|
-
}
|
|
695
|
-
/** pi's resume: an input to the root conversation under the request's own id, which pi takes once however often it
|
|
696
|
-
* is sent, then the mark. */
|
|
697
|
-
submit = async (id, content, mark, origin) => {
|
|
698
|
-
const root = await this.durable.harness.root(this.context);
|
|
699
|
-
const requestId = `approval-${id}`;
|
|
700
|
-
// pi writes its user entry with no data, so the resume's origin goes first as an entry of its own, which the Timeline
|
|
701
|
-
// matches to it by its text (pi-durable-disk's serve does the same). Once only: a resume tried again whose input is
|
|
702
|
-
// in already writes no second marker, which would be left open for a later message with the same text.
|
|
703
|
-
if (origin && (await root.commit((tx) => tx.submissionByRequest(root.id, requestId), this.context)) === undefined)
|
|
704
|
-
await root.submit({ type: "write", requestId: `origin of ${requestId}`, entry: { kind: MESSAGE_ORIGIN_KIND, data: { origin, text: content } } }, this.context);
|
|
705
|
-
await root.submit({ type: "input", requestId, content, whenBusy: "followUp" }, this.context);
|
|
706
|
-
await this.durable.harness.commit(mark, this.context);
|
|
707
|
-
};
|
|
708
|
-
async settle() {
|
|
709
|
-
// Acting on an answer already: an approved call may be in flight, which a stop or a cancel since cuts.
|
|
710
|
-
if (this.settling)
|
|
711
|
-
return void this.cutIfWithdrawn().catch(() => undefined);
|
|
712
|
-
const open = [...this.requests.values()].filter((held) => !held.resolved);
|
|
713
|
-
if (!open.length) {
|
|
714
|
-
clearInterval(this.timer);
|
|
715
|
-
this.timer = undefined;
|
|
716
|
-
return;
|
|
717
|
-
}
|
|
718
|
-
this.settling = true;
|
|
719
|
-
try {
|
|
720
|
-
const answers = (await readJson(join(this.where.root, RUN_META)))?.approvals;
|
|
721
|
-
for (const held of open) {
|
|
722
|
-
const answer = answers?.[held.id];
|
|
723
|
-
// Withdrawn with its run (a cancel, or a stop while it waited): never run and nothing resumed; resolved, so the
|
|
724
|
-
// run waits on it no more.
|
|
725
|
-
if (answer?.decision === "canceled" || answer?.decision === "withdrawn") {
|
|
726
|
-
await this.resolveSilently(held, answer.decision);
|
|
727
|
-
continue;
|
|
728
|
-
}
|
|
729
|
-
if (answer?.decision !== "allow" && answer?.decision !== "deny")
|
|
730
|
-
continue;
|
|
731
|
-
const decision = answer.decision;
|
|
732
|
-
// Read again just before acting: a stop or a cancel since overrides the answer (runs.ts supersede), and then
|
|
733
|
-
// nothing runs and nothing resumes. Once the call's intent is written, a stop takes its turn as any stop does.
|
|
734
|
-
const now = (await readJson(join(this.where.root, RUN_META)))?.approvals?.[held.id];
|
|
735
|
-
if (now?.decision !== decision) {
|
|
736
|
-
if (now?.decision === "canceled" || now?.decision === "withdrawn")
|
|
737
|
-
await this.resolveSilently(held, now.decision);
|
|
738
|
-
continue;
|
|
739
|
-
}
|
|
740
|
-
// An allow runs the call, unless its tool was set to deny since it asked.
|
|
741
|
-
// A request parked on a replay may follow an attempt that was sent: its refusal makes no "nothing was charged" claim.
|
|
742
|
-
if (decision === "allow")
|
|
743
|
-
this.unpark?.();
|
|
744
|
-
// And a pin holds for an approved call too: one whose arguments are no longer as pinned (a pin set since it asked)
|
|
745
|
-
// is refused as a deny since is.
|
|
746
|
-
const result = decision === "deny" ? { text: refusedText(held.tool, answer.note, held.replayed ? "unknown" : "no"), isError: true } : this.policy(held.tool) === "deny" || this.pinned(held.tool, held.args) ? await this.ranOrDenied(held) : await this.run(held);
|
|
747
|
-
// A test's seam, unset in a server (AGENTRUN_SERVE_TEST_CRASH_AFTER_CALL, a file that marks it once): an approved
|
|
748
|
-
// call that ran and whose outcome is in the journal, then the process goes before its resume, once the test says go
|
|
749
|
-
// (<file>.go), as a crash between the two would leave the run.
|
|
750
|
-
const seam = process.env.AGENTRUN_SERVE_TEST_CRASH_AFTER_CALL;
|
|
751
|
-
if (seam && decision === "allow" && !existsSync(seam)) {
|
|
752
|
-
while (!existsSync(`${seam}.go`))
|
|
753
|
-
await new Promise((done) => setTimeout(done, 50));
|
|
754
|
-
writeFileSync(seam, "");
|
|
755
|
-
process.kill(process.pid, "SIGKILL");
|
|
756
|
-
}
|
|
757
|
-
// And again once the call is done: a stop or a cancel while it ran drops its result, which never reaches the model.
|
|
758
|
-
const after = (await readJson(join(this.where.root, RUN_META)))?.approvals?.[held.id];
|
|
759
|
-
if (after?.decision === "canceled" || after?.decision === "withdrawn") {
|
|
760
|
-
await this.resolveSilently(held, after.decision);
|
|
761
|
-
continue;
|
|
762
|
-
}
|
|
763
|
-
// The arguments the message names are the server's redacted ones: the gate never writes a held value there. The
|
|
764
|
-
// result is cut as every host tool's is (boundResult, HOST_RESULT_MAX_CHARS, in its words): an executor's (an
|
|
765
|
-
// MCP call's) comes uncut, and its journal keeps it whole.
|
|
766
|
-
// The server's message, never the person's: a declining message's words are theirs, beside it.
|
|
767
|
-
const origin = decision === "deny" && answer.by === "message" && answer.note?.trim() ? { kind: "server", reason: "approval_declined", said: answer.note } : { kind: "server", reason: "approval" };
|
|
768
|
-
await this.resume(held.id, resumeText(held, answer.args_shown ?? "(not shown)", decision, { ...result, text: boundResult(result.text).text }), (tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: APPROVAL_ENTRY, data: { phase: "resolved", id: held.id, decision } }), origin);
|
|
769
|
-
held.resolved = true;
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
catch (error) {
|
|
773
|
-
// Tried again at the next tick: the call's effect answers from its journal, and the resume is taken once.
|
|
774
|
-
process.stderr.write(`agentrun-serve: an answered approval is not acted on yet: ${error?.message ?? error}\n`);
|
|
775
|
-
}
|
|
776
|
-
finally {
|
|
777
|
-
this.settling = false;
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
/** The approved call in flight, and its own abort: a stop or a cancel of the run cuts it (cutIfWithdrawn). */
|
|
781
|
-
inFlight;
|
|
782
|
-
/** Cuts the approved call in flight when a stop or a cancel since withdrew its answer (runs.ts supersede). */
|
|
783
|
-
async cutIfWithdrawn() {
|
|
784
|
-
const flying = this.inFlight;
|
|
785
|
-
if (!flying || flying.cut.signal.aborted)
|
|
786
|
-
return;
|
|
787
|
-
const now = (await readJson(join(this.where.root, RUN_META)))?.approvals?.[flying.id];
|
|
788
|
-
if (now?.decision === "canceled" || now?.decision === "withdrawn")
|
|
789
|
-
flying.cut.abort(new Error(INTERRUPTED_TEXT));
|
|
790
|
-
}
|
|
791
|
-
/** A call withdrawn with its run: resolved in the run's store with nothing run and nothing resumed. */
|
|
792
|
-
async resolveSilently(held, decision) {
|
|
793
|
-
await this.durable.harness.commit((tx) => tx.appendEntry(ROOT_CONVERSATION_ID, { kind: APPROVAL_ENTRY, data: { phase: "resolved", id: held.id, decision } }), this.context);
|
|
794
|
-
held.resolved = true;
|
|
795
|
-
}
|
|
796
|
-
/** The run's own `approvals:` session, the journal every approved call runs through: opened once. */
|
|
797
|
-
journal() {
|
|
798
|
-
this.drive ??= durableSessions(this.durable.harness, this.context, this.durable.store)
|
|
799
|
-
.open(`approvals:${this.where.runId}`)
|
|
800
|
-
.then((writer) => openDrive({ writer, signal: this.stop.signal }))
|
|
801
|
-
.catch((error) => {
|
|
802
|
-
this.drive = undefined;
|
|
803
|
-
throw error;
|
|
804
|
-
});
|
|
805
|
-
return this.drive;
|
|
806
|
-
}
|
|
807
|
-
/** An approved call whose tool was set to deny (or pinned so that its arguments no longer hold) before the gate acted
|
|
808
|
-
* on the answer. A call whose outcome is in the
|
|
809
|
-
* journal RAN (a crash came between it and its resume): the model gets that outcome, as any allowed call's, and nothing
|
|
810
|
-
* runs again; a later deny can't un-run it. One with an intent and no outcome may have been sent: denied, with no claim
|
|
811
|
-
* either way. One the journal never saw is denied, not run and not charged. */
|
|
812
|
-
async ranOrDenied(held) {
|
|
813
|
-
const drive = await this.journal();
|
|
814
|
-
const key = `approval:${held.id}`;
|
|
815
|
-
if (drive.isMemoized(key))
|
|
816
|
-
return this.run(held);
|
|
817
|
-
const heldTo = settingsKeyOf(this.where.harness ?? DEFAULT_HARNESS, held.tool, this.where.mcpServers);
|
|
818
|
-
const ran = held.replayed || drive.state().effects.has(key) ? "unknown" : "no";
|
|
819
|
-
if (this.policy(held.tool) !== "deny") {
|
|
820
|
-
const pinned = this.pinned(heldTo ?? held.tool, held.args, ran);
|
|
821
|
-
if (pinned)
|
|
822
|
-
return { text: pinned.text, isError: true };
|
|
823
|
-
}
|
|
824
|
-
return { text: deniedText(held.tool, heldTo, this.denyFrom(held.tool), ran), isError: true };
|
|
825
|
-
}
|
|
826
|
-
/** The approved call, run once by the kernel through the journal any host tool's call goes through, as an effect of
|
|
827
|
-
* the run's own `approvals:` session, on the run's coding source: its intent is durable before it runs, its outcome
|
|
828
|
-
* after; a replay-safe call a crash cut off runs again, any other is answered interrupted and never runs again. */
|
|
829
|
-
async run(held) {
|
|
830
|
-
const runner = await this.runnerOf(held);
|
|
831
|
-
if ("refused" in runner)
|
|
832
|
-
return { text: runner.refused, isError: true };
|
|
833
|
-
const drive = this.journal();
|
|
834
|
-
try {
|
|
835
|
-
// Its own abort beside the drive's: a stop or a cancel of the run cuts this call, which runs outside any turn.
|
|
836
|
-
const cut = new AbortController();
|
|
837
|
-
this.inFlight = { id: held.id, cut };
|
|
838
|
-
try {
|
|
839
|
-
return await (await drive).effect(`approval:${held.id}`, { name: held.tool, replay: runner.replay, input: held.args }, async ({ signal }) => {
|
|
840
|
-
try {
|
|
841
|
-
const answer = await runner.call(AbortSignal.any([signal, cut.signal]));
|
|
842
|
-
// A host tool answers its own abort as an error result: still the stop's cut.
|
|
843
|
-
if (cut.signal.aborted)
|
|
844
|
-
throw new Error(INTERRUPTED_TEXT);
|
|
845
|
-
return { text: answer.text, isError: answer.isError === true };
|
|
846
|
-
}
|
|
847
|
-
catch (error) {
|
|
848
|
-
// Cut by a stop: its journal answers it interrupted, as a crash's cut call is.
|
|
849
|
-
if (cut.signal.aborted)
|
|
850
|
-
throw new Error(INTERRUPTED_TEXT);
|
|
851
|
-
throw error;
|
|
852
|
-
}
|
|
853
|
-
});
|
|
854
|
-
}
|
|
855
|
-
finally {
|
|
856
|
-
this.inFlight = undefined;
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
catch (error) {
|
|
860
|
-
// Its outcome (interrupted, or a throw), journaled; anything else (the drive fenced) is tried again.
|
|
861
|
-
if (error instanceof EffectFailure)
|
|
862
|
-
return { text: error.message, isError: true };
|
|
863
|
-
throw error;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
executors = new Map();
|
|
867
|
-
/** Registers how an approved call of tools named `prefix…` runs (the MCP gateway's, "mcp:"): one per prefix and run.
|
|
868
|
-
* The gate runs it exactly once on its journaled path, as it runs a kernel host tool, with no deadline of its own: the
|
|
869
|
-
* executor bounds the call (Executor). */
|
|
870
|
-
executor(prefix, run) {
|
|
871
|
-
this.executors.set(prefix, run);
|
|
872
|
-
}
|
|
873
|
-
/** What runs an approved call: the executor its tool's prefix names, which may have effects anywhere, so a call a
|
|
874
|
-
* crash cut off is never run again; else the kernel's host tool of the name, as replay-safe as that tool is. Neither:
|
|
875
|
-
* refused, in words, and never run. */
|
|
876
|
-
async runnerOf(held) {
|
|
877
|
-
const prefix = [...this.executors.keys()].find((given) => held.tool.startsWith(given));
|
|
878
|
-
if (prefix) {
|
|
879
|
-
const executor = this.executors.get(prefix);
|
|
880
|
-
return { replay: "never", call: (signal) => executor(held.tool, held.args, held.call_id, signal) };
|
|
881
|
-
}
|
|
882
|
-
if (isMcpTool(held.tool))
|
|
883
|
-
return { refused: `The approved ${held.tool} call was not run: nothing in this run runs mcp: calls.` };
|
|
884
|
-
// Imported here, not at the top: host-tools reaches the web tools, which reach tool-plane, which reads this module's
|
|
885
|
-
// permission rule, and a cycle of top-level imports leaves one of them uninitialized for whichever loads first.
|
|
886
|
-
const { hostTools } = await import("./host-tools.js");
|
|
887
|
-
const [tool] = hostTools(this.durable, [held.tool], this.context, new Map(), this.coding, undefined, this.extra);
|
|
888
|
-
if (!tool)
|
|
889
|
-
return { refused: `The approved ${held.tool} call is not one the kernel runs outside the model's turn.` };
|
|
890
|
-
return { replay: tool.replay, call: (signal) => tool.call(held.args, { callId: held.call_id, signal }) };
|
|
891
|
-
}
|
|
892
|
-
}
|
|
893
|
-
/** `value` as JSON with every object's keys sorted, so the same arguments are the same text however they were built. */
|
|
894
|
-
const canonical = (value) => Array.isArray(value) ? `[${value.map(canonical).join(",")}]` : value !== null && typeof value === "object" ? `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(",")}}` : (JSON.stringify(value) ?? "null");
|
|
895
|
-
/** The digest a park by tool and arguments is matched on: the sha256 of the arguments' canonical JSON, in hex. */
|
|
896
|
-
export const argsDigest = (args) => createHash("sha256").update(canonical(args ?? {})).digest("hex");
|
|
897
|
-
/** Whether the tool result `result` ends the turn `parked` waits in (harness-app.ts asks it at each one): the parked
|
|
898
|
-
* call's own, by its id; or, for a park by tool and arguments, the next result of that tool with those arguments. */
|
|
899
|
-
export function endsTurn(parked, result) {
|
|
900
|
-
if (!parked)
|
|
901
|
-
return false;
|
|
902
|
-
if ("callId" in parked)
|
|
903
|
-
return parked.callId === result.callId;
|
|
904
|
-
return parked.tool === result.tool && parked.argsDigest === argsDigest(result.args);
|
|
905
447
|
}
|