@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/store/open.js
CHANGED
|
@@ -41,10 +41,10 @@ export function storePlace(target) {
|
|
|
41
41
|
}
|
|
42
42
|
/** Opens the store `options` names (storeTarget): the server's and the runner's one way in. On Postgres its pool holds at
|
|
43
43
|
* most storePoolSize(env) connections; a pool size that is no size is refused on either store. */
|
|
44
|
-
export async function openStore({ dataDir, role = "server", now, url, env = process.env, warn }) {
|
|
44
|
+
export async function openStore({ dataDir, role = "server", now, url, env = process.env, warn, onOwnerLost }) {
|
|
45
45
|
const target = storeTarget(dataDir, env, url ?? storeUrl());
|
|
46
46
|
const poolSize = storePoolSize(env);
|
|
47
47
|
if (target.kind === "sqlite")
|
|
48
48
|
return openSqliteStore({ dataDir, role, ...(now ? { now } : {}) });
|
|
49
|
-
return openPostgresStore({ url: target.url, schema: target.schema, role, poolSize, ...(now ? { now } : {}), ...(warn ? { warn } : {}) });
|
|
49
|
+
return openPostgresStore({ url: target.url, schema: target.schema, role, poolSize, ...(now ? { now } : {}), ...(warn ? { warn } : {}), ...(onOwnerLost ? { onOwnerLost } : {}) });
|
|
50
50
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import pg from "pg";
|
|
2
|
+
import { StoreOwned } from "../store.js";
|
|
2
3
|
import { type SqliteValue, type Statement, type StatementResult, type StoreConnection, type TransactionOptions, type Tx } from "../sqlite/connection.js";
|
|
3
4
|
import type { Dialect } from "../dialect.js";
|
|
4
5
|
/** Which store this is, as two opens of the same data agree on it (StoreConnection.where): "postgres:" and the URL's host
|
|
@@ -10,8 +11,19 @@ export interface PgConnectionOptions {
|
|
|
10
11
|
/** postgres:// or postgresql:// URL. Never printed: it may carry a password. */
|
|
11
12
|
readonly url: string;
|
|
12
13
|
readonly schema: string;
|
|
13
|
-
/** Take the owner lock (a server):
|
|
14
|
+
/** Take the owner lock (a server): waited for while another server holds the schema (ownerWaitMs), StoreOwned past it. */
|
|
14
15
|
readonly owner?: boolean;
|
|
16
|
+
/** How long a server waits for the owner lock another server holds, at its open and to take its own back once its
|
|
17
|
+
* lock's connection is lost (OWNER_WAIT_MS by default; 0 refuses at once). */
|
|
18
|
+
readonly ownerWaitMs?: number;
|
|
19
|
+
/** Told once, in words, when the open waits for another server to let the schema go (a deploy's old server stopping). */
|
|
20
|
+
readonly onOwnerWait?: (message: string) => void;
|
|
21
|
+
/** Told when this server's owner lock is lost for good (another server took the schema, or it could not be taken back
|
|
22
|
+
* within ownerWaitMs): every call is refused from then, and the process should exit so its platform restarts it. */
|
|
23
|
+
readonly onOwnerLost?: (error: StoreOwned) => void;
|
|
24
|
+
/** Asked on the owner connection each time the lock is taken back: why the schema is not one this build serves (a newer
|
|
25
|
+
* server migrated it while the lock was not held), or undefined. A reason gives the lock up for good (onOwnerLost). */
|
|
26
|
+
readonly ownerCheck?: (client: pg.Client) => Promise<string | undefined>;
|
|
15
27
|
/** How long a statement waits for another writer's lock before StoreBusy. */
|
|
16
28
|
readonly busyTimeoutMs?: number;
|
|
17
29
|
/** Most clients open at once. */
|
|
@@ -41,7 +53,15 @@ export declare const LEASE_DEADLINE_MS = 8000;
|
|
|
41
53
|
export declare class Unanswered extends Error {
|
|
42
54
|
constructor(what: string, ms: number);
|
|
43
55
|
}
|
|
56
|
+
/** How long a server waits for the owner lock another server holds before it gives up (StoreOwned), in ms, at its open
|
|
57
|
+
* and to take its own back after its lock's connection is lost: a deploy's new server waits out the old one's drain
|
|
58
|
+
* (a runner's 35 s graceful stop, the server's own) instead of exiting, and a lost lock is never a 503 forever. */
|
|
59
|
+
export declare const OWNER_WAIT_MS = 120000;
|
|
44
60
|
export declare function numberParams(sql: string): string;
|
|
61
|
+
/** A Postgres error as the store's own: a refused row StoreConstraint, a lock or a conflict StoreBusy. */
|
|
62
|
+
/** Whether `error` says the database ended the session (its idle-in-transaction timeout, SQLSTATE 25P03), or that the
|
|
63
|
+
* connection is gone and takes no more statements: a transaction's work on it was rolled back by the database. */
|
|
64
|
+
export declare function sessionEnded(error: unknown): boolean;
|
|
45
65
|
export declare class PgConnection implements StoreConnection {
|
|
46
66
|
#private;
|
|
47
67
|
readonly dialect: Dialect;
|
|
@@ -50,7 +70,7 @@ export declare class PgConnection implements StoreConnection {
|
|
|
50
70
|
readonly where: string;
|
|
51
71
|
private constructor();
|
|
52
72
|
/** The pool on `url` in `schema`, and (owner) the owner lock: StoreOwned when another server holds the schema. */
|
|
53
|
-
static open({ url, schema, owner, busyTimeoutMs, poolSize, testClock, connectTimeoutMs }: PgConnectionOptions): Promise<PgConnection>;
|
|
73
|
+
static open({ url, schema, owner, busyTimeoutMs, poolSize, testClock, connectTimeoutMs, ownerWaitMs, onOwnerWait, onOwnerLost, ownerCheck }: PgConnectionOptions): Promise<PgConnection>;
|
|
54
74
|
get<T extends object>(sql: string, ...params: SqliteValue[]): Promise<T | undefined>;
|
|
55
75
|
all<T extends object>(sql: string, ...params: SqliteValue[]): Promise<T[]>;
|
|
56
76
|
/** One statement on its own (autocommit), its changes counted: the shared code's run outside a transaction. */
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
// no row rolls its transaction back with the typed error, as SQLite's worker does. A server's connection also holds the
|
|
6
6
|
// store's owner lock (one server per schema): a session advisory lock on a client of its own, held until close.
|
|
7
7
|
import pg from "pg";
|
|
8
|
+
import { runnableDeadline } from "../deadline.js";
|
|
8
9
|
import { cleanText } from "../dialect.js";
|
|
9
10
|
import { Fenced, RunFinal, StoreBusy, StoreClosed, StoreConstraint, StoreDeadline, StoreOwned } from "../store.js";
|
|
10
11
|
import { DEFAULT_TRANSACTION_DEADLINE_MS } from "../sqlite/connection.js";
|
|
@@ -100,6 +101,71 @@ function int8(text) {
|
|
|
100
101
|
return value;
|
|
101
102
|
}
|
|
102
103
|
const types = { getTypeParser: (oid, format) => (oid === 20 ? int8 : pg.types.getTypeParser(oid, format)) };
|
|
104
|
+
/** How long a server waits for the owner lock another server holds before it gives up (StoreOwned), in ms, at its open
|
|
105
|
+
* and to take its own back after its lock's connection is lost: a deploy's new server waits out the old one's drain
|
|
106
|
+
* (a runner's 35 s graceful stop, the server's own) instead of exiting, and a lost lock is never a 503 forever. */
|
|
107
|
+
export const OWNER_WAIT_MS = 120_000;
|
|
108
|
+
/** How often a waiting server asks for the owner lock again (ms). */
|
|
109
|
+
const OWNER_POLL_MS = 250;
|
|
110
|
+
/** How long after its lock's connection ends a server first asks for it back (ms): a server already waiting for the
|
|
111
|
+
* schema (a deploy's new one) takes it first, and this one then exits. */
|
|
112
|
+
const OWNER_RETAKE_DELAY_MS = 1_000;
|
|
113
|
+
/** The owner session's own keepalives on the database's end: a dead server's session, and its lock, end in about 60 s,
|
|
114
|
+
* later than this end finds the connection dead (Node's keepalive: 10 s, then 10 probes 1 s apart), so the database
|
|
115
|
+
* never lets the lock go while this server still believes it holds it. */
|
|
116
|
+
const OWNER_SESSION = "-c tcp_keepalives_idle=30 -c tcp_keepalives_interval=10 -c tcp_keepalives_count=3";
|
|
117
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
118
|
+
/** How long one ask for the owner lock may wait for the database's answer once connected (ms): a connection that stays
|
|
119
|
+
* open but never answers is destroyed past it, so a wait or a take-back always reaches its own end. */
|
|
120
|
+
const OWNER_ASK_MS = 5_000;
|
|
121
|
+
/** Destroys `client`'s socket and lets it go: for a connection that never answered, whose own end would wait on it. */
|
|
122
|
+
function destroyed(client) {
|
|
123
|
+
client.connection?.stream?.destroy();
|
|
124
|
+
return client.end().catch(() => undefined);
|
|
125
|
+
}
|
|
126
|
+
/** Ends backend `pid` (this server's own lost owner session) from a connection of its own, bounded as an ask is. */
|
|
127
|
+
async function endSession(url, pid, connectMs) {
|
|
128
|
+
const client = new pg.Client({ connectionString: url, types, application_name: "agentrun-store", connectionTimeoutMillis: connectMs });
|
|
129
|
+
client.on("error", () => undefined);
|
|
130
|
+
try {
|
|
131
|
+
await client.connect();
|
|
132
|
+
await answered(client.query("SELECT pg_terminate_backend($1)", [pid]), OWNER_ASK_MS, "the end of the lost owner session");
|
|
133
|
+
await client.end().catch(() => undefined);
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
await destroyed(client);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/** Asks for `schema`'s owner lock once, on a connection of its own: kept open, holding the lock, when taken. Bounded: its
|
|
140
|
+
* connect by `connectMs`, each of its statements by OWNER_ASK_MS (Unanswered past it, the connection destroyed). */
|
|
141
|
+
async function tryOwner(url, schema, connectMs, where) {
|
|
142
|
+
const client = new pg.Client({ connectionString: url, types, options: OWNER_SESSION, application_name: `agentrun-store-owner:${schema}`, connectionTimeoutMillis: connectMs, keepAlive: true, keepAliveInitialDelayMillis: KEEPALIVE_IDLE_S * 1_000 });
|
|
143
|
+
// A lost owner connection lets the lock go: its error is never left unhandled (the store watches its end).
|
|
144
|
+
client.on("error", () => undefined);
|
|
145
|
+
try {
|
|
146
|
+
await client.connect();
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
await client.end().catch(() => undefined);
|
|
150
|
+
throw connectTimedOut(error) ? connectDeadline(where, connectMs) : typed(error);
|
|
151
|
+
}
|
|
152
|
+
try {
|
|
153
|
+
const asked = (await answered(client.query("SELECT pg_try_advisory_lock(hashtext($1), hashtext('owner')) AS held, pg_backend_pid() AS pid, (SELECT backend_start::text FROM pg_stat_activity WHERE pid = pg_backend_pid()) AS started", [schema]), OWNER_ASK_MS, "the owner lock's ask")).rows[0];
|
|
154
|
+
if (asked.held === true)
|
|
155
|
+
return { taken: { client, pid: Number(asked.pid), startedAt: String(asked.started) } };
|
|
156
|
+
// Who holds it: the advisory lock (hashtext(schema), hashtext('owner')) in this database (advisory locks are per
|
|
157
|
+
// database; pg_locks shows the whole cluster's), its two keys as pg_locks shows them.
|
|
158
|
+
const holder = (await answered(client.query(`SELECT l.pid, a.backend_start::text AS started FROM pg_locks l LEFT JOIN pg_stat_activity a ON a.pid = l.pid
|
|
159
|
+
WHERE l.locktype = 'advisory' AND l.granted AND l.objsubid = 2 AND l.database = (SELECT oid FROM pg_database WHERE datname = current_database())
|
|
160
|
+
AND l.classid::bigint = (hashtext($1)::bigint & 4294967295) AND l.objid::bigint = (hashtext('owner')::bigint & 4294967295)`, [schema]), OWNER_ASK_MS, "the owner lock's holder")).rows[0];
|
|
161
|
+
await client.end().catch(() => undefined);
|
|
162
|
+
return { heldBy: holder ? { pid: Number(holder.pid), startedAt: holder.started ?? null } : undefined };
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
await destroyed(client);
|
|
166
|
+
throw error instanceof Unanswered ? error : typed(error);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
103
169
|
/** `?` parameters numbered $1, $2 … (outside quoted text and identifiers), kept per statement text. */
|
|
104
170
|
const numbered = new Map();
|
|
105
171
|
export function numberParams(sql) {
|
|
@@ -152,6 +218,15 @@ async function withUnique(error, cause, columnsOf) {
|
|
|
152
218
|
return columns.length ? new StoreConstraint(error.message, { rule: "unique", unique: { table: e.table, columns } }) : error;
|
|
153
219
|
}
|
|
154
220
|
/** A Postgres error as the store's own: a refused row StoreConstraint, a lock or a conflict StoreBusy. */
|
|
221
|
+
/** Whether `error` says the database ended the session (its idle-in-transaction timeout, SQLSTATE 25P03), or that the
|
|
222
|
+
* connection is gone and takes no more statements: a transaction's work on it was rolled back by the database. */
|
|
223
|
+
export function sessionEnded(error) {
|
|
224
|
+
const e = error;
|
|
225
|
+
if (e?.code === "25P03")
|
|
226
|
+
return true;
|
|
227
|
+
const message = typeof e?.message === "string" ? e.message : "";
|
|
228
|
+
return /not queryable|Connection terminated/i.test(message);
|
|
229
|
+
}
|
|
155
230
|
function typed(error, deadlineMs) {
|
|
156
231
|
// Already the store's own (a failed statement's error, met again at the commit of a callback that caught it).
|
|
157
232
|
if (error instanceof Fenced || error instanceof RunFinal || error instanceof StoreClosed || error instanceof StoreDeadline || error instanceof StoreConstraint || error instanceof StoreBusy || error instanceof Unanswered)
|
|
@@ -192,12 +267,16 @@ export class PgConnection {
|
|
|
192
267
|
#pool;
|
|
193
268
|
/** One connection kept for renewing claims (TransactionOptions.lease). */
|
|
194
269
|
#leasePool;
|
|
270
|
+
/** The owner lock's session (a server's), replaced when it is taken back after its connection was lost. */
|
|
195
271
|
#owner;
|
|
272
|
+
/** What taking the owner lock back needs. */
|
|
273
|
+
#ownerConfig;
|
|
196
274
|
#busyTimeoutMs;
|
|
197
275
|
/** The bound on getting a connection (PgConnectionOptions.connectTimeoutMs). */
|
|
198
276
|
#connectMs;
|
|
199
277
|
#closed = false;
|
|
200
|
-
/** Set
|
|
278
|
+
/** Set while the owner lock is not held (its connection ended or failed, and it is being taken back), and for good once
|
|
279
|
+
* another server has the schema: every call is refused with it meanwhile. */
|
|
201
280
|
#ownerLost;
|
|
202
281
|
/** Calls in flight: close waits for them. */
|
|
203
282
|
#inFlight = new Set();
|
|
@@ -216,27 +295,83 @@ export class PgConnection {
|
|
|
216
295
|
}
|
|
217
296
|
return columns;
|
|
218
297
|
};
|
|
219
|
-
constructor(pool, owner, schema, busyTimeoutMs, where, testClock, connectMs, leasePool) {
|
|
298
|
+
constructor(pool, owner, schema, busyTimeoutMs, where, testClock, connectMs, leasePool, ownerConfig) {
|
|
220
299
|
this.#connectMs = connectMs;
|
|
221
300
|
this.#pool = pool;
|
|
222
301
|
this.#leasePool = leasePool;
|
|
223
302
|
this.#owner = owner;
|
|
303
|
+
this.#ownerConfig = ownerConfig;
|
|
224
304
|
this.schema = schema;
|
|
225
305
|
this.where = where;
|
|
226
306
|
this.#busyTimeoutMs = busyTimeoutMs;
|
|
227
307
|
this.dialect = postgresDialect(schema, { testClock });
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
308
|
+
if (owner)
|
|
309
|
+
this.#watch(owner);
|
|
310
|
+
}
|
|
311
|
+
/** The owner lock lives on its own connection: when that connection ends or fails (the database restarted, a failover,
|
|
312
|
+
* an operator killed it), the lock may be gone and another server may take the schema. From then this store refuses
|
|
313
|
+
* every call (StoreOwned), never writing beside a server that may hold the schema now, and takes the lock back
|
|
314
|
+
* (#retake). */
|
|
315
|
+
#watch(session) {
|
|
231
316
|
const lost = () => {
|
|
232
|
-
if (
|
|
233
|
-
|
|
317
|
+
if (this.#closed || this.#owner !== session || this.#ownerLost)
|
|
318
|
+
return;
|
|
319
|
+
this.#ownerLost = new StoreOwned(`the owner lock on the Postgres schema ${this.schema} was lost (its connection ended): this server is taking it back, and refuses every call until it holds it again`);
|
|
320
|
+
void this.#retake(session);
|
|
234
321
|
};
|
|
235
|
-
|
|
236
|
-
|
|
322
|
+
session.client.on("error", lost);
|
|
323
|
+
session.client.on("end", lost);
|
|
324
|
+
}
|
|
325
|
+
/** Takes the owner lock back on a fresh connection, after OWNER_RETAKE_DELAY_MS (a server waiting for the schema takes
|
|
326
|
+
* it first). Held by this server's own lost session (the database not yet told its client is gone: a partition), that
|
|
327
|
+
* session is ended and the lock taken. Held by another server, or not taken within ownerWaitMs, it is lost for good:
|
|
328
|
+
* every call is refused from then, and onOwnerLost is told, so the process exits and its platform restarts it. */
|
|
329
|
+
async #retake(old) {
|
|
330
|
+
const config = this.#ownerConfig;
|
|
331
|
+
const until = Date.now() + OWNER_RETAKE_DELAY_MS + config.waitMs;
|
|
332
|
+
await sleep(OWNER_RETAKE_DELAY_MS);
|
|
333
|
+
while (!this.#closed) {
|
|
334
|
+
const asked = await tryOwner(config.url, this.schema, this.#connectMs, this.where).catch(() => undefined);
|
|
335
|
+
if (asked && "taken" in asked) {
|
|
336
|
+
if (this.#closed)
|
|
337
|
+
return void (await asked.taken.client.end().catch(() => undefined));
|
|
338
|
+
// While the lock was not held, a newer server may have taken the schema, migrated it and let it go: this build
|
|
339
|
+
// serves only the schema it opened, so a schema it does not know gives the lock up for good.
|
|
340
|
+
const unknown = config.check ? await answered(config.check(asked.taken.client), OWNER_ASK_MS, "the schema's check").catch((error) => `its schema could not be read (${error.message})`) : undefined;
|
|
341
|
+
if (unknown !== undefined) {
|
|
342
|
+
await asked.taken.client.end().catch(() => undefined);
|
|
343
|
+
return this.#lostForGood(new StoreOwned(`the owner lock on the Postgres schema ${this.schema} was taken back, but ${unknown}: this server no longer serves the store`));
|
|
344
|
+
}
|
|
345
|
+
// Closed while the schema was checked: close() ended only the old session, so this one lets the lock go here.
|
|
346
|
+
if (this.#closed)
|
|
347
|
+
return void (await asked.taken.client.end().catch(() => undefined));
|
|
348
|
+
this.#owner = asked.taken;
|
|
349
|
+
this.#watch(asked.taken);
|
|
350
|
+
this.#ownerLost = undefined;
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const holder = asked?.heldBy;
|
|
354
|
+
if (holder && holder.pid === old.pid && holder.startedAt === old.startedAt) {
|
|
355
|
+
// This server's own session, its client gone but its backend not: ended, so its lock comes back.
|
|
356
|
+
await endSession(config.url, old.pid, this.#connectMs);
|
|
357
|
+
}
|
|
358
|
+
else if (holder) {
|
|
359
|
+
return this.#lostForGood(new StoreOwned(`another server took the store in the Postgres schema ${this.schema} after this server's owner lock was lost: this server no longer holds it`));
|
|
360
|
+
}
|
|
361
|
+
if (Date.now() >= until) {
|
|
362
|
+
return this.#lostForGood(new StoreOwned(`the owner lock on the Postgres schema ${this.schema} was lost and could not be taken back within ${Math.round(config.waitMs / 1000)} s: this server no longer holds the store`));
|
|
363
|
+
}
|
|
364
|
+
await sleep(OWNER_POLL_MS);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
#lostForGood(error) {
|
|
368
|
+
if (this.#closed)
|
|
369
|
+
return;
|
|
370
|
+
this.#ownerLost = error;
|
|
371
|
+
this.#ownerConfig?.onLost?.(error);
|
|
237
372
|
}
|
|
238
373
|
/** The pool on `url` in `schema`, and (owner) the owner lock: StoreOwned when another server holds the schema. */
|
|
239
|
-
static async open({ url, schema, owner = false, busyTimeoutMs = 10_000, poolSize = 10, testClock = false, connectTimeoutMs = STORE_CONNECT_TIMEOUT_MS }) {
|
|
374
|
+
static async open({ url, schema, owner = false, busyTimeoutMs = 10_000, poolSize = 10, testClock = false, connectTimeoutMs = STORE_CONNECT_TIMEOUT_MS, ownerWaitMs = OWNER_WAIT_MS, onOwnerWait, onOwnerLost, ownerCheck }) {
|
|
240
375
|
if (!SCHEMA_NAME.test(schema))
|
|
241
376
|
throw new TypeError(`the store's schema is a lower-case name of letters, digits and underscores, not ${JSON.stringify(schema)}`);
|
|
242
377
|
// The database's end of the keepalives (KEEPALIVE_IDLE_S): a vanished client's session, and its locks, ended in ~25 s.
|
|
@@ -249,27 +384,26 @@ export class PgConnection {
|
|
|
249
384
|
const tcp = { keepAlive: true, keepAliveInitialDelayMillis: KEEPALIVE_IDLE_S * 1_000 };
|
|
250
385
|
const config = { connectionString: url, options: session, types, max: poolSize, allowExitOnIdle: true, application_name: "agentrun-store", connectionTimeoutMillis: connectMs, ...tcp };
|
|
251
386
|
let lock;
|
|
387
|
+
const waitMs = Math.max(0, Math.floor(ownerWaitMs));
|
|
252
388
|
if (owner) {
|
|
253
|
-
//
|
|
254
|
-
//
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
await lock.end().catch(() => undefined);
|
|
272
|
-
throw error instanceof StoreOwned ? error : typed(error);
|
|
389
|
+
// Another server's lock is waited for (a deploy's old server draining and stopping), asked again every
|
|
390
|
+
// OWNER_POLL_MS, up to waitMs; past it StoreOwned. Its keepalives: OWNER_SESSION.
|
|
391
|
+
const until = Date.now() + waitMs;
|
|
392
|
+
let told = false;
|
|
393
|
+
for (;;) {
|
|
394
|
+
const asked = await tryOwner(url, schema, connectMs, where);
|
|
395
|
+
if ("taken" in asked) {
|
|
396
|
+
lock = asked.taken;
|
|
397
|
+
break;
|
|
398
|
+
}
|
|
399
|
+
if (Date.now() >= until) {
|
|
400
|
+
throw new StoreOwned(`another server holds the store in the Postgres schema ${schema}${waitMs > 0 ? `, and still held it after ${Math.round(waitMs / 1000)} s` : ""}`);
|
|
401
|
+
}
|
|
402
|
+
if (!told) {
|
|
403
|
+
told = true;
|
|
404
|
+
onOwnerWait?.(`another server holds the store in the Postgres schema ${schema}: waiting up to ${Math.round(waitMs / 1000)} s for it to let go`);
|
|
405
|
+
}
|
|
406
|
+
await sleep(OWNER_POLL_MS);
|
|
273
407
|
}
|
|
274
408
|
}
|
|
275
409
|
const pool = new pg.Pool(config);
|
|
@@ -287,7 +421,7 @@ export class PgConnection {
|
|
|
287
421
|
each.on("error", () => undefined);
|
|
288
422
|
each.on("connect", (fresh) => fresh.on("error", () => undefined));
|
|
289
423
|
}
|
|
290
|
-
return new PgConnection(pool, lock, schema, busyTimeoutMs, where, testClock, connectMs, leasePool);
|
|
424
|
+
return new PgConnection(pool, lock, schema, busyTimeoutMs, where, testClock, connectMs, leasePool, lock ? { url, waitMs, onLost: onOwnerLost, check: ownerCheck } : undefined);
|
|
291
425
|
}
|
|
292
426
|
/** `work`, counted in flight, refused after close. A connection it never got in time is StoreDeadline, naming the
|
|
293
427
|
* database's place. */
|
|
@@ -394,6 +528,15 @@ export class PgConnection {
|
|
|
394
528
|
}
|
|
395
529
|
let live = true;
|
|
396
530
|
let broken;
|
|
531
|
+
// The database may end the session while the transaction waits between statements (its idle-in-transaction timeout:
|
|
532
|
+
// this process held up past the deadline and its slack): heard here, a statement or the commit after it answers
|
|
533
|
+
// StoreDeadline (home_busy, retryable), since the database rolled the transaction back and nothing of it was written.
|
|
534
|
+
let ended;
|
|
535
|
+
const onEnded = (error) => {
|
|
536
|
+
ended ??= error;
|
|
537
|
+
};
|
|
538
|
+
client.on("error", onEnded);
|
|
539
|
+
const lost = (error) => (ended !== undefined || sessionEnded(error) ? new StoreDeadline(deadlineMs) : undefined);
|
|
397
540
|
const check = () => {
|
|
398
541
|
if (!live)
|
|
399
542
|
throw new Error("the store's transaction is over: its handle takes no more statements");
|
|
@@ -417,9 +560,11 @@ export class PgConnection {
|
|
|
417
560
|
return await step(client, statement, columnsOf, deadlineMs);
|
|
418
561
|
}
|
|
419
562
|
catch (error) {
|
|
420
|
-
// A statement that failed has aborted the transaction: no later statement of it runs.
|
|
421
|
-
|
|
422
|
-
|
|
563
|
+
// A statement that failed has aborted the transaction: no later statement of it runs. On a session the database
|
|
564
|
+
// ended, it is the transaction's deadline.
|
|
565
|
+
const failure = lost(error) ?? error;
|
|
566
|
+
broken ??= failure;
|
|
567
|
+
throw failure;
|
|
423
568
|
}
|
|
424
569
|
};
|
|
425
570
|
const tx = {
|
|
@@ -436,15 +581,26 @@ export class PgConnection {
|
|
|
436
581
|
// drop, a failover) is bounded from its first statement on, not only once BEGIN has answered. Past it the client is
|
|
437
582
|
// let go with the error, which destroys its socket (Postgres rolls a dead session's transaction back), never
|
|
438
583
|
// waiting on a ROLLBACK that a silent connection would never answer.
|
|
584
|
+
// Counted from its begin, the deadline is the time this process could run (store/deadline.ts runnableDeadline, the
|
|
585
|
+
// rule SQLite's keeps): a statement answered while other work held the event loop waited behind it, and that time
|
|
586
|
+
// was no statement's, so a write that was fine is never rolled back for it. Counted from its call (a lease's), it is
|
|
587
|
+
// the wall clock's.
|
|
439
588
|
let timer;
|
|
440
|
-
const deadlineAt =
|
|
589
|
+
const deadlineAt = performance.now() + ms;
|
|
441
590
|
const deadline = new Promise((_, reject) => {
|
|
442
|
-
|
|
591
|
+
const expire = () => reject(new StoreDeadline(deadlineMs));
|
|
592
|
+
if (fromCall) {
|
|
593
|
+
const wall = setTimeout(expire, ms);
|
|
594
|
+
timer = { clear: () => clearTimeout(wall), held: () => 0 };
|
|
595
|
+
}
|
|
596
|
+
else
|
|
597
|
+
timer = runnableDeadline(ms, expire);
|
|
443
598
|
});
|
|
444
599
|
deadline.catch(() => undefined);
|
|
445
600
|
const abandon = (error) => {
|
|
446
601
|
live = false;
|
|
447
|
-
|
|
602
|
+
timer?.clear();
|
|
603
|
+
client.off("error", onEnded);
|
|
448
604
|
// Its socket destroyed here: with no query in flight (a deadline between statements) pg's own end would send a
|
|
449
605
|
// Terminate and wait for a FIN a silent connection never answers.
|
|
450
606
|
client.connection?.stream?.destroy();
|
|
@@ -472,19 +628,32 @@ export class PgConnection {
|
|
|
472
628
|
throw error;
|
|
473
629
|
}
|
|
474
630
|
live = false;
|
|
475
|
-
|
|
631
|
+
timer?.clear();
|
|
632
|
+
client.off("error", onEnded);
|
|
476
633
|
await rollBackAndRelease(client);
|
|
477
634
|
throw error;
|
|
478
635
|
}
|
|
479
636
|
live = false;
|
|
480
|
-
|
|
637
|
+
timer?.clear();
|
|
638
|
+
// Ended before its commit is sent: nothing of it was written, and the commit is never sent on a gone session.
|
|
639
|
+
const endedBefore = ended;
|
|
481
640
|
try {
|
|
641
|
+
if (endedBefore)
|
|
642
|
+
throw endedBefore;
|
|
482
643
|
// A callback that caught a failed statement's error and went on still rolls back: Postgres aborted the transaction.
|
|
483
644
|
if (broken)
|
|
484
645
|
throw broken;
|
|
485
|
-
await answered(client.query("COMMIT"), Math.max(COMMIT_FLOOR_MS, deadlineAt -
|
|
646
|
+
await answered(client.query("COMMIT"), Math.max(COMMIT_FLOOR_MS, deadlineAt + (timer?.held() ?? 0) - performance.now()), "its commit");
|
|
486
647
|
}
|
|
487
648
|
catch (error) {
|
|
649
|
+
if (endedBefore !== undefined || error?.code === "25P03") {
|
|
650
|
+
client.off("error", onEnded);
|
|
651
|
+
client.release(error);
|
|
652
|
+
if (read)
|
|
653
|
+
return value;
|
|
654
|
+
throw new StoreDeadline(deadlineMs);
|
|
655
|
+
}
|
|
656
|
+
client.off("error", onEnded);
|
|
488
657
|
if (error instanceof Unanswered) {
|
|
489
658
|
client.release(error);
|
|
490
659
|
// A read's snapshot has nothing to land: what it read stands.
|
|
@@ -495,6 +664,7 @@ export class PgConnection {
|
|
|
495
664
|
await rollBackAndRelease(client);
|
|
496
665
|
throw typed(error, deadlineMs);
|
|
497
666
|
}
|
|
667
|
+
client.off("error", onEnded);
|
|
498
668
|
client.release();
|
|
499
669
|
return value;
|
|
500
670
|
});
|
|
@@ -527,6 +697,6 @@ export class PgConnection {
|
|
|
527
697
|
await Promise.allSettled([...this.#inFlight]);
|
|
528
698
|
await this.#pool.end().catch(() => undefined);
|
|
529
699
|
await this.#leasePool.end().catch(() => undefined);
|
|
530
|
-
await this.#owner?.end().catch(() => undefined);
|
|
700
|
+
await this.#owner?.client.end().catch(() => undefined);
|
|
531
701
|
}
|
|
532
702
|
}
|
|
@@ -4,6 +4,10 @@ export interface Migration {
|
|
|
4
4
|
readonly name: string;
|
|
5
5
|
readonly sql: string;
|
|
6
6
|
readonly sha256: string;
|
|
7
|
+
/** The migration's check (NNNN_name.check.sql beside it), run just before it: a query whose rows are the reasons the
|
|
8
|
+
* schema cannot take the migration yet, each in a `refusal` column, in words that name what holds it back and what to do.
|
|
9
|
+
* Not part of the file's hash: it runs only before the migration applies. */
|
|
10
|
+
readonly check?: string;
|
|
7
11
|
}
|
|
8
12
|
/** The migrations shipped beside this module (dist/store/postgres/migrations), in version order, numbered 1, 2, 3 … with
|
|
9
13
|
* no gap. */
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
// that finds one changed refuses to start, naming it. A runner applies nothing; it reads the version and refuses one it does
|
|
5
5
|
// not know (SchemaVersionUnknown), as on SQLite.
|
|
6
6
|
import { createHash } from "node:crypto";
|
|
7
|
-
import { readdirSync, readFileSync } from "node:fs";
|
|
7
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
8
8
|
import { SchemaVersionUnknown } from "../store.js";
|
|
9
9
|
import { SCHEMA_NAME } from "./connection.js";
|
|
10
10
|
/** `schema` as SQL takes it here, unquoted: refused (TypeError) unless it is a store schema name (SCHEMA_NAME), before any
|
|
@@ -23,7 +23,9 @@ export function migrationsIn(dir = new URL("./migrations/", import.meta.url)) {
|
|
|
23
23
|
.filter((match) => match !== null)
|
|
24
24
|
.map((match) => {
|
|
25
25
|
const sql = readFileSync(new URL(match[0], dir), "utf8");
|
|
26
|
-
|
|
26
|
+
const checkFile = new URL(`${match[1]}_${match[2]}.check.sql`, dir);
|
|
27
|
+
const check = existsSync(checkFile) ? readFileSync(checkFile, "utf8") : undefined;
|
|
28
|
+
return { version: Number(match[1]), name: match[2], sql, sha256: createHash("sha256").update(sql).digest("hex"), ...(check === undefined ? {} : { check }) };
|
|
27
29
|
})
|
|
28
30
|
.sort((a, b) => a.version - b.version);
|
|
29
31
|
found.forEach((migration, index) => {
|
|
@@ -110,6 +112,11 @@ export async function migrate(client, schema, at, migrations = migrationsIn()) {
|
|
|
110
112
|
}
|
|
111
113
|
}
|
|
112
114
|
for (const migration of migrations.slice(top ?? 0)) {
|
|
115
|
+
if (migration.check !== undefined) {
|
|
116
|
+
const refusals = (await client.query(migration.check)).rows.map((row) => String(row.refusal));
|
|
117
|
+
if (refusals.length > 0)
|
|
118
|
+
throw new Error(`${placeOf(schema)} cannot take migration ${String(migration.version).padStart(4, "0")}_${migration.name}.sql, so nothing was applied: ${refusals.join("; ")}`);
|
|
119
|
+
}
|
|
113
120
|
await client.query(migration.sql);
|
|
114
121
|
await client.query("INSERT INTO schema_migrations (version, name, sha256, applied_at) VALUES ($1, $2, $3, $4)", [migration.version, migration.name, migration.sha256, at]);
|
|
115
122
|
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
-- The check before 0003 (migrate.ts runs it first; each row refuses the migration in its words). 0003's unique index
|
|
2
|
+
-- takes one unrefused answer per approval of a run; a store an earlier build left after an approve and a deny raced can
|
|
3
|
+
-- hold two. Nothing is changed here: the words name the run, the call and its answers, and give the statement that keeps
|
|
4
|
+
-- the first (the one the run's runner took first) and refuses the rest, as SQLite's open does.
|
|
5
|
+
|
|
6
|
+
SELECT format(
|
|
7
|
+
'run %s has %s answers to its tool call %s (commands %s), and only one may stand: keep the first and refuse the others with '
|
|
8
|
+
|| 'UPDATE %I.commands SET refused = %L, taken_at = COALESCE(taken_at, (extract(epoch FROM now()) * 1000)::bigint) WHERE id IN (%s), '
|
|
9
|
+
|| 'then start the server again',
|
|
10
|
+
run_id, count(*), body::json ->> 'approval',
|
|
11
|
+
string_agg(id || ' (' || kind || ')', ', ' ORDER BY id),
|
|
12
|
+
current_schema(), 'a second answer to one approval: the first one stands',
|
|
13
|
+
array_to_string((array_agg(id ORDER BY id))[2:], ', ')
|
|
14
|
+
) AS refusal
|
|
15
|
+
FROM commands
|
|
16
|
+
WHERE kind IN ('approve', 'deny') AND refused IS NULL AND body::json ->> 'approval' IS NOT NULL
|
|
17
|
+
GROUP BY run_id, body::json ->> 'approval'
|
|
18
|
+
HAVING count(*) > 1
|
|
19
|
+
ORDER BY min(id);
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
-- The store's schema on Postgres, version 3: one answer per approval, as store/schema.ts COMMANDS_ONE_ANSWER keeps it on
|
|
2
|
+
-- SQLite. Applied once, after 0002, inside the transaction migrate.ts opens. Never edit this file once applied: a change is
|
|
3
|
+
-- a new file.
|
|
4
|
+
--
|
|
5
|
+
-- commands_one_answer: an approve or deny naming one approval (the body's `approval`, the tool call's id) is unique per run
|
|
6
|
+
-- while the runner has not refused it. The first answer written wins; a second, whether the same answer or the other one,
|
|
7
|
+
-- is a unique violation (StoreConstraint), which the server reads back as the first one's decision (the same again
|
|
8
|
+
-- changes nothing, the other is 409 already_answered). An answer the runner refused (its run could not resume) holds no
|
|
9
|
+
-- place, so the person may answer again.
|
|
10
|
+
|
|
11
|
+
CREATE UNIQUE INDEX commands_one_answer ON commands (run_id, (body::json ->> 'approval')) WHERE kind IN ('approve', 'deny') AND refused IS NULL;
|
|
12
|
+
|
|
13
|
+
UPDATE store_meta SET schema_version = 3 WHERE singleton = 1;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Store, type StoreRole } from "../store.js";
|
|
1
|
+
import { type Store, type StoreOwned, type StoreRole } from "../store.js";
|
|
2
2
|
/** The schema a store is in when none is named. */
|
|
3
3
|
export declare const DEFAULT_SCHEMA = "agentrun";
|
|
4
4
|
/** How far a runner's clock may be from the database's before it says so at its start (ms). Leases are 30 s, so a gap this
|
|
@@ -27,8 +27,15 @@ export interface PostgresStoreOptions {
|
|
|
27
27
|
* come free (STORE_CONNECT_TIMEOUT_MS, 4 s, by default). Past it the call, or the open, is refused (StoreDeadline,
|
|
28
28
|
* naming the database's host, database and schema, never the URL), never left hanging. */
|
|
29
29
|
readonly connectTimeoutMs?: number;
|
|
30
|
-
/** Told, at a runner's open, when its clock is more than CLOCK_WARN_MS from the database's
|
|
30
|
+
/** Told, at a runner's open, when its clock is more than CLOCK_WARN_MS from the database's; at a server's, when it waits
|
|
31
|
+
* for another server to let the schema go. */
|
|
31
32
|
readonly warn?: (message: string) => void;
|
|
33
|
+
/** A server's: how long it waits for the owner lock another server holds, at its open and to take its own back after its
|
|
34
|
+
* lock's connection is lost (OWNER_WAIT_MS, 2 min, by default; 0 refuses at once with StoreOwned). */
|
|
35
|
+
readonly ownerWaitMs?: number;
|
|
36
|
+
/** A server's: told when its owner lock is lost for good (another server took the schema, or it could not be taken back
|
|
37
|
+
* within ownerWaitMs). Every call is refused from then: the process should exit non-zero, so its platform restarts it. */
|
|
38
|
+
readonly onOwnerLost?: (error: StoreOwned) => void;
|
|
32
39
|
}
|
|
33
40
|
/** Opens the store in `schema` on the Postgres at `url`. As the server (the default): takes the owner lock (StoreOwned when
|
|
34
41
|
* another server holds the schema) and applies the pending migrations. As a runner: sets nothing up, and warns when its
|
|
@@ -28,6 +28,15 @@ export async function openPostgresStore(options) {
|
|
|
28
28
|
...(options.busyTimeoutMs === undefined ? {} : { busyTimeoutMs: options.busyTimeoutMs }),
|
|
29
29
|
...(options.poolSize === undefined ? {} : { poolSize: options.poolSize }),
|
|
30
30
|
...(options.connectTimeoutMs === undefined ? {} : { connectTimeoutMs: options.connectTimeoutMs }),
|
|
31
|
+
...(options.ownerWaitMs === undefined ? {} : { ownerWaitMs: options.ownerWaitMs }),
|
|
32
|
+
...(options.warn ? { onOwnerWait: options.warn } : {}),
|
|
33
|
+
...(options.onOwnerLost ? { onOwnerLost: options.onOwnerLost } : {}),
|
|
34
|
+
// Taken back after a loss: the schema must still be the version this build migrated it to (a newer server may have
|
|
35
|
+
// taken the schema and migrated it meanwhile).
|
|
36
|
+
ownerCheck: async (client) => {
|
|
37
|
+
const found = await versionOf(client, schema);
|
|
38
|
+
return found === known ? undefined : `the schema is at version ${found ?? "none"} now, and this build knows ${known}`;
|
|
39
|
+
},
|
|
31
40
|
});
|
|
32
41
|
try {
|
|
33
42
|
const found = await connection.withClient((client) => (role === "server" ? migrate(client, schema, now(), migrations) : versionOf(client, schema)));
|
package/dist/store/schema.js
CHANGED
|
@@ -278,7 +278,7 @@ export const STORE_DDL = [
|
|
|
278
278
|
// sign_ins|<agent>:<provider>|pointer), which the store never opens. The owner sees its name ("default") and saved_at,
|
|
279
279
|
// never the pointer, and no run's tools ever do. Forgetting a sign-in moves its sealed pointer, byte for byte so it
|
|
280
280
|
// still opens, into a tombstone row (sign_in_tombstones), kept only so a server started later can still cut it from
|
|
281
|
-
// what a run it was handed shows
|
|
281
|
+
// what a run it was handed shows and from what its agent learned; every one is kept, never pruned.
|
|
282
282
|
`CREATE TABLE IF NOT EXISTS sign_ins (
|
|
283
283
|
agent_id TEXT NOT NULL REFERENCES agents (id),
|
|
284
284
|
provider TEXT NOT NULL,
|
package/dist/store/seal.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// The store's one sealing rule: every sealed column is today's AES-256-GCM seal (
|
|
1
|
+
// The store's one sealing rule: every sealed column is today's AES-256-GCM seal (sealing.ts seal/unseal, the one
|
|
2
2
|
// the tool keys use) under the server's seal key, bound to where it lives by its AAD, "<table>|<row key>|<column>". A value
|
|
3
3
|
// copied to another row, column or table does not open. The store holds only the sealed bytes; the key stays in the
|
|
4
4
|
// server's (and a runner's) environment, and the server's writer and the runner's unseal both seal and open through here.
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// credential as JSON with the server's origin inside it. Opened for a row whose url has another origin (an edit of the url
|
|
8
8
|
// column by someone who can write the store but has no key), it is refused as damaged, so a credential is never sent to a
|
|
9
9
|
// host it was not saved for.
|
|
10
|
-
import { seal, unseal } from "
|
|
10
|
+
import { seal, unseal } from "../sealing.js";
|
|
11
11
|
/** The sealed columns the store has. */
|
|
12
12
|
export const sealedAt = {
|
|
13
13
|
mcpServer: (name) => ({ table: "mcp_servers", row: name, column: "sealed" }),
|