@parcha/agentrun-serve 0.1.0-beta.32 → 0.1.0-beta.34
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 +270 -0
- package/README.md +343 -246
- package/dist/adapters.d.ts +0 -7
- package/dist/adapters.js +11 -22
- package/dist/agent-rows.d.ts +98 -0
- package/dist/agent-rows.js +1 -0
- package/dist/agents-store.d.ts +6 -2
- package/dist/agents-store.js +60 -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 +14 -0
- package/dist/api-only.js +33 -0
- package/dist/archil-routes.d.ts +1 -1
- package/dist/bash-read-only.d.ts +22 -0
- package/dist/bash-read-only.js +701 -0
- package/dist/box-egress-vm.d.ts +0 -8
- package/dist/box-egress-vm.js +0 -10
- package/dist/box-egress.d.ts +0 -4
- package/dist/box-egress.js +1 -16
- package/dist/box-paths.d.ts +25 -2
- package/dist/box-paths.js +79 -11
- package/dist/box-relay.d.ts +0 -11
- package/dist/box-relay.js +0 -20
- package/dist/box-settings.d.ts +26 -0
- package/dist/box-settings.js +63 -0
- package/dist/boxes.d.ts +21 -1
- 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 +94 -51
- package/dist/browser-live.d.ts +7 -20
- package/dist/browser-live.js +15 -93
- package/dist/browser-person.d.ts +18 -53
- package/dist/browser-person.js +94 -279
- package/dist/budget.d.ts +0 -4
- package/dist/budget.js +0 -7
- package/dist/cap.d.ts +8 -12
- package/dist/cap.js +12 -27
- package/dist/cli.js +61 -20
- 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.js +5 -2
- package/dist/computer-setting.d.ts +20 -4
- package/dist/computer-setting.js +25 -5
- package/dist/computers.d.ts +31 -3
- package/dist/computers.js +70 -11
- package/dist/confine.d.ts +120 -0
- package/dist/confine.js +436 -0
- package/dist/console/assets/index-RPHGlXmM.js +540 -0
- package/dist/console/assets/index-opTN5Naq.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/daytona-api.d.ts +0 -11
- package/dist/daytona-api.js +1 -82
- 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/docker-host-addresses.d.ts +37 -0
- package/dist/docker-host-addresses.js +110 -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 +0 -2
- package/dist/errors.js +6 -9
- package/dist/filed-answer.d.ts +32 -0
- package/dist/filed-answer.js +160 -0
- package/dist/files.d.ts +1 -11
- package/dist/files.js +2 -21
- package/dist/github-connection.d.ts +2 -2
- package/dist/github-connection.js +1 -1
- package/dist/graph-view.d.ts +3 -5
- package/dist/graph-view.js +4 -14
- package/dist/handed-values.d.ts +9 -0
- package/dist/handed-values.js +26 -0
- package/dist/harness-install.d.ts +22 -5
- package/dist/harness-install.js +98 -20
- package/dist/harness-opencode/adapter.d.ts +7 -0
- package/dist/harness-opencode/adapter.js +23 -6
- 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/harness.d.ts +4 -3
- package/dist/harness.js +16 -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 +62 -24
- package/dist/key-refusal.d.ts +5 -1
- package/dist/key-refusal.js +19 -4
- package/dist/learn/check.d.ts +6 -2
- package/dist/learn/check.js +83 -29
- package/dist/learn/folder.d.ts +5 -1
- package/dist/learn/folder.js +10 -3
- package/dist/learn/grade.d.ts +14 -0
- package/dist/learn/grade.js +68 -12
- package/dist/learn/index.d.ts +4 -4
- package/dist/learn/index.js +4 -4
- package/dist/learn/reflect.d.ts +31 -2
- package/dist/learn/reflect.js +79 -4
- package/dist/learn/relevance.d.ts +24 -0
- package/dist/learn/relevance.js +91 -0
- package/dist/learn/secrets.d.ts +8 -1
- package/dist/learn/secrets.js +95 -15
- package/dist/learn/skills.d.ts +112 -14
- package/dist/learn/skills.js +556 -52
- package/dist/learned-workflow.d.ts +37 -5
- package/dist/learned-workflow.js +106 -12
- package/dist/learning-routes.d.ts +16 -49
- package/dist/learning-routes.js +46 -36
- package/dist/learning-run.d.ts +6 -250
- package/dist/learning-run.js +11 -665
- package/dist/learning-words.d.ts +12 -5
- package/dist/learning-words.js +18 -9
- package/dist/learning.d.ts +93 -77
- package/dist/learning.js +176 -304
- package/dist/live/stream.js +1 -1
- package/dist/live/translate.js +13 -3
- package/dist/local-models.d.ts +11 -5
- package/dist/local-models.js +13 -8
- package/dist/mcp-gateway.d.ts +11 -52
- package/dist/mcp-gateway.js +44 -148
- 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 +6 -6
- package/dist/mcp-servers-store.js +13 -15
- package/dist/mcp-store.js +3 -4
- package/dist/mcp.js +4 -3
- package/dist/model.d.ts +6 -7
- package/dist/model.js +43 -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 +124 -50
- package/dist/page.d.ts +1 -3
- package/dist/page.js +9 -19
- package/dist/paid-calls.d.ts +21 -27
- package/dist/paid-calls.js +28 -62
- package/dist/pause.d.ts +0 -2
- package/dist/pause.js +1 -8
- package/dist/place.d.ts +0 -5
- package/dist/place.js +0 -10
- package/dist/print.d.ts +2 -1
- package/dist/print.js +4 -2
- package/dist/propose.js +83 -39
- 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 -13
- package/dist/run-db.js +2 -41
- package/dist/run-launch.d.ts +19 -2
- package/dist/run-launch.js +13 -5
- package/dist/run-learning-served.d.ts +46 -2
- package/dist/run-learning-served.js +26 -7
- package/dist/run-reads-over-store.d.ts +1 -1
- package/dist/run-reads-over-store.js +15 -6
- package/dist/run-secrets.d.ts +33 -0
- package/dist/run-secrets.js +53 -0
- package/dist/run-timeline-served.d.ts +5 -1
- package/dist/run-timeline-served.js +19 -5
- package/dist/run-view.d.ts +12 -17
- package/dist/run-view.js +29 -28
- package/dist/runnable.d.ts +18 -10
- package/dist/runnable.js +21 -10
- package/dist/runner/adapter-loop.d.ts +14 -3
- package/dist/runner/adapter-loop.js +464 -62
- package/dist/runner/adapter-run-spec.d.ts +29 -8
- package/dist/runner/adapter-run-spec.js +96 -37
- package/dist/runner/adapters/index.js +11 -1
- package/dist/runner/adapters/pi.d.ts +15 -1
- package/dist/runner/adapters/pi.js +19 -5
- package/dist/runner/beside.d.ts +30 -3
- package/dist/runner/beside.js +54 -12
- package/dist/runner/claim-secrets.d.ts +10 -1
- package/dist/runner/claim-secrets.js +49 -15
- package/dist/runner/cli-run-spec.d.ts +2 -0
- package/dist/runner/cli-run-spec.js +60 -17
- 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.js +118 -8
- 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.js +1 -1
- package/dist/runner/harness-loops.d.ts +9 -1
- package/dist/runner/harness-loops.js +15 -2
- package/dist/runner/journal-kinds.d.ts +30 -6
- package/dist/runner/journal-kinds.js +20 -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 +113 -11
- package/dist/runner/learning-loop.d.ts +5 -0
- package/dist/runner/learning-loop.js +26 -6
- package/dist/runner/live-ticker.d.ts +1 -1
- package/dist/runner/live-ticker.js +28 -4
- package/dist/runner/loop-contract.d.ts +3 -2
- package/dist/runner/main.d.ts +64 -10
- package/dist/runner/main.js +165 -27
- package/dist/runner/mcp-claim.d.ts +1 -1
- package/dist/runner/mcp-claim.js +2 -3
- package/dist/runner/mcp-run.d.ts +2 -0
- package/dist/runner/mcp-run.js +15 -5
- package/dist/runner/memory-claim.d.ts +9 -0
- package/dist/runner/memory-claim.js +25 -0
- package/dist/runner/metrics.d.ts +33 -1
- package/dist/runner/metrics.js +85 -1
- package/dist/runner/model-route.d.ts +13 -0
- package/dist/runner/model-route.js +154 -11
- package/dist/runner/redact.d.ts +8 -2
- package/dist/runner/redact.js +11 -2
- 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 +56 -10
- 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-loop.d.ts +6 -0
- package/dist/runner/workflow-loop.js +6 -4
- package/dist/runner/workflow-spec.d.ts +18 -0
- package/dist/runner/workflow-spec.js +68 -26
- package/dist/runs-over-store.d.ts +8 -3
- package/dist/runs-over-store.js +76 -19
- package/dist/seal-key.d.ts +54 -15
- package/dist/seal-key.js +232 -82
- 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/secrets.d.ts +2 -10
- package/dist/secrets.js +21 -17
- 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 +1 -80
- package/dist/server-secrets-store.d.ts +4 -4
- package/dist/server-secrets-store.js +10 -32
- package/dist/server-secrets.d.ts +4 -9
- package/dist/server-secrets.js +12 -33
- package/dist/server.d.ts +26 -44
- package/dist/server.js +229 -142
- 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/store/agent-body.d.ts +8 -2
- package/dist/store/agent-body.js +16 -4
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.js +3 -3
- package/dist/store/open.d.ts +18 -5
- package/dist/store/open.js +23 -3
- package/dist/store/postgres/connection.d.ts +18 -2
- package/dist/store/postgres/connection.js +156 -32
- package/dist/store/postgres/migrate.d.ts +4 -0
- package/dist/store/postgres/migrate.js +27 -3
- 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 +49 -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 +64 -20
- package/dist/store/store.d.ts +38 -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 +25 -6
- package/dist/store-reads/journal.js +173 -21
- package/dist/store-reads/learning.d.ts +26 -8
- package/dist/store-reads/learning.js +12 -4
- package/dist/store-reads/workflow.d.ts +1 -1
- package/dist/store-reads/workflow.js +1 -1
- package/dist/store-view.d.ts +5 -1
- package/dist/store-view.js +91 -23
- package/dist/template-eval.d.ts +60 -4
- package/dist/template-eval.js +120 -54
- package/dist/templates.d.ts +24 -10
- package/dist/templates.js +69 -38
- 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 +274 -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-gate.d.ts +41 -216
- package/dist/tool-gate.js +79 -540
- package/dist/tool-keys-store.d.ts +11 -12
- package/dist/tool-keys-store.js +24 -31
- package/dist/tool-keys.d.ts +9 -104
- package/dist/tool-keys.js +15 -271
- package/dist/tool-plane.d.ts +14 -42
- 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 +31 -19
- package/dist/tools.js +101 -108
- package/dist/trigger-adapter.d.ts +2 -2
- package/dist/trigger-schedule.js +2 -1
- package/dist/trigger-secrets-shown.js +5 -1
- 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.js +1 -1
- package/dist/usage.d.ts +0 -2
- package/dist/usage.js +0 -2
- package/dist/validate.d.ts +2 -0
- package/dist/validate.js +23 -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/beneath.d.ts +19 -0
- package/dist/workspace/beneath.js +106 -0
- package/dist/workspace/computer-deps.d.ts +52 -5
- package/dist/workspace/computer-deps.js +154 -19
- package/dist/workspace/computer-for-claim.d.ts +19 -14
- package/dist/workspace/computer-for-claim.js +77 -16
- 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-providers.d.ts +6 -2
- package/dist/workspace/computer-providers.js +1 -1
- package/dist/workspace/grep-worker.d.ts +5 -0
- package/dist/workspace/grep-worker.js +9 -6
- package/dist/workspace/index.d.ts +4 -3
- package/dist/workspace/index.js +4 -4
- package/dist/workspace/local.d.ts +11 -2
- package/dist/workspace/local.js +161 -43
- package/dist/workspace/resolve-computer.d.ts +50 -10
- package/dist/workspace/resolve-computer.js +46 -11
- package/dist/workspace/run-tools.d.ts +13 -31
- package/dist/workspace/run-tools.js +10 -27
- 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 +3 -99
- package/launcher/agentrun-serve +6 -33
- package/package.json +14 -14
- package/templates/README.md +9 -3
- package/templates/inbound-lead/agent.md +1 -1
- package/templates/kyb/agent.md +1 -1
- package/templates/market-monitor/agent.md +2 -2
- package/templates/market-monitor/skills/monitor/SKILL.md +6 -4
- package/dist/app.d.ts +0 -5
- package/dist/app.js +0 -514
- package/dist/bound-files.d.ts +0 -4
- package/dist/bound-files.js +0 -164
- package/dist/bounds.d.ts +0 -26
- package/dist/bounds.js +0 -137
- 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-35-7aeIZ.js +0 -519
- package/dist/console/assets/index-Cejbov-Y.css +0 -1
- package/dist/content-filter.d.ts +0 -7
- package/dist/content-filter.js +0 -23
- package/dist/disk.d.ts +0 -33
- package/dist/disk.js +0 -85
- package/dist/door-line.d.ts +0 -23
- package/dist/door-line.js +0 -83
- package/dist/grade.d.ts +0 -63
- package/dist/grade.js +0 -150
- package/dist/harness-app.d.ts +0 -69
- package/dist/harness-app.js +0 -1162
- 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/request-start.d.ts +0 -27
- package/dist/request-start.js +0 -381
- package/dist/run-box.d.ts +0 -205
- package/dist/run-box.js +0 -661
- package/dist/run-holder.d.ts +0 -52
- package/dist/run-holder.js +0 -139
- package/dist/runs.d.ts +0 -1200
- package/dist/runs.js +0 -4103
- package/dist/sealed-settings.d.ts +0 -60
- package/dist/sealed-settings.js +0 -178
- package/dist/served-only.d.ts +0 -5
- package/dist/served-only.js +0 -12
- 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
|
@@ -100,6 +100,71 @@ function int8(text) {
|
|
|
100
100
|
return value;
|
|
101
101
|
}
|
|
102
102
|
const types = { getTypeParser: (oid, format) => (oid === 20 ? int8 : pg.types.getTypeParser(oid, format)) };
|
|
103
|
+
/** How long a server waits for the owner lock another server holds before it gives up (StoreOwned), in ms, at its open
|
|
104
|
+
* 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
|
|
105
|
+
* (a runner's 35 s graceful stop, the server's own) instead of exiting, and a lost lock is never a 503 forever. */
|
|
106
|
+
export const OWNER_WAIT_MS = 120_000;
|
|
107
|
+
/** How often a waiting server asks for the owner lock again (ms). */
|
|
108
|
+
const OWNER_POLL_MS = 250;
|
|
109
|
+
/** How long after its lock's connection ends a server first asks for it back (ms): a server already waiting for the
|
|
110
|
+
* schema (a deploy's new one) takes it first, and this one then exits. */
|
|
111
|
+
const OWNER_RETAKE_DELAY_MS = 1_000;
|
|
112
|
+
/** The owner session's own keepalives on the database's end: a dead server's session, and its lock, end in about 60 s,
|
|
113
|
+
* later than this end finds the connection dead (Node's keepalive: 10 s, then 10 probes 1 s apart), so the database
|
|
114
|
+
* never lets the lock go while this server still believes it holds it. */
|
|
115
|
+
const OWNER_SESSION = "-c tcp_keepalives_idle=30 -c tcp_keepalives_interval=10 -c tcp_keepalives_count=3";
|
|
116
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
117
|
+
/** How long one ask for the owner lock may wait for the database's answer once connected (ms): a connection that stays
|
|
118
|
+
* open but never answers is destroyed past it, so a wait or a take-back always reaches its own end. */
|
|
119
|
+
const OWNER_ASK_MS = 5_000;
|
|
120
|
+
/** Destroys `client`'s socket and lets it go: for a connection that never answered, whose own end would wait on it. */
|
|
121
|
+
function destroyed(client) {
|
|
122
|
+
client.connection?.stream?.destroy();
|
|
123
|
+
return client.end().catch(() => undefined);
|
|
124
|
+
}
|
|
125
|
+
/** Ends backend `pid` (this server's own lost owner session) from a connection of its own, bounded as an ask is. */
|
|
126
|
+
async function endSession(url, pid, connectMs) {
|
|
127
|
+
const client = new pg.Client({ connectionString: url, types, application_name: "agentrun-store", connectionTimeoutMillis: connectMs });
|
|
128
|
+
client.on("error", () => undefined);
|
|
129
|
+
try {
|
|
130
|
+
await client.connect();
|
|
131
|
+
await answered(client.query("SELECT pg_terminate_backend($1)", [pid]), OWNER_ASK_MS, "the end of the lost owner session");
|
|
132
|
+
await client.end().catch(() => undefined);
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
await destroyed(client);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/** Asks for `schema`'s owner lock once, on a connection of its own: kept open, holding the lock, when taken. Bounded: its
|
|
139
|
+
* connect by `connectMs`, each of its statements by OWNER_ASK_MS (Unanswered past it, the connection destroyed). */
|
|
140
|
+
async function tryOwner(url, schema, connectMs, where) {
|
|
141
|
+
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 });
|
|
142
|
+
// A lost owner connection lets the lock go: its error is never left unhandled (the store watches its end).
|
|
143
|
+
client.on("error", () => undefined);
|
|
144
|
+
try {
|
|
145
|
+
await client.connect();
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
await client.end().catch(() => undefined);
|
|
149
|
+
throw connectTimedOut(error) ? connectDeadline(where, connectMs) : typed(error);
|
|
150
|
+
}
|
|
151
|
+
try {
|
|
152
|
+
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];
|
|
153
|
+
if (asked.held === true)
|
|
154
|
+
return { taken: { client, pid: Number(asked.pid), startedAt: String(asked.started) } };
|
|
155
|
+
// Who holds it: the advisory lock (hashtext(schema), hashtext('owner')) in this database (advisory locks are per
|
|
156
|
+
// database; pg_locks shows the whole cluster's), its two keys as pg_locks shows them.
|
|
157
|
+
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
|
|
158
|
+
WHERE l.locktype = 'advisory' AND l.granted AND l.objsubid = 2 AND l.database = (SELECT oid FROM pg_database WHERE datname = current_database())
|
|
159
|
+
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];
|
|
160
|
+
await client.end().catch(() => undefined);
|
|
161
|
+
return { heldBy: holder ? { pid: Number(holder.pid), startedAt: holder.started ?? null } : undefined };
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
await destroyed(client);
|
|
165
|
+
throw error instanceof Unanswered ? error : typed(error);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
103
168
|
/** `?` parameters numbered $1, $2 … (outside quoted text and identifiers), kept per statement text. */
|
|
104
169
|
const numbered = new Map();
|
|
105
170
|
export function numberParams(sql) {
|
|
@@ -192,12 +257,16 @@ export class PgConnection {
|
|
|
192
257
|
#pool;
|
|
193
258
|
/** One connection kept for renewing claims (TransactionOptions.lease). */
|
|
194
259
|
#leasePool;
|
|
260
|
+
/** The owner lock's session (a server's), replaced when it is taken back after its connection was lost. */
|
|
195
261
|
#owner;
|
|
262
|
+
/** What taking the owner lock back needs. */
|
|
263
|
+
#ownerConfig;
|
|
196
264
|
#busyTimeoutMs;
|
|
197
265
|
/** The bound on getting a connection (PgConnectionOptions.connectTimeoutMs). */
|
|
198
266
|
#connectMs;
|
|
199
267
|
#closed = false;
|
|
200
|
-
/** Set
|
|
268
|
+
/** Set while the owner lock is not held (its connection ended or failed, and it is being taken back), and for good once
|
|
269
|
+
* another server has the schema: every call is refused with it meanwhile. */
|
|
201
270
|
#ownerLost;
|
|
202
271
|
/** Calls in flight: close waits for them. */
|
|
203
272
|
#inFlight = new Set();
|
|
@@ -216,27 +285,83 @@ export class PgConnection {
|
|
|
216
285
|
}
|
|
217
286
|
return columns;
|
|
218
287
|
};
|
|
219
|
-
constructor(pool, owner, schema, busyTimeoutMs, where, testClock, connectMs, leasePool) {
|
|
288
|
+
constructor(pool, owner, schema, busyTimeoutMs, where, testClock, connectMs, leasePool, ownerConfig) {
|
|
220
289
|
this.#connectMs = connectMs;
|
|
221
290
|
this.#pool = pool;
|
|
222
291
|
this.#leasePool = leasePool;
|
|
223
292
|
this.#owner = owner;
|
|
293
|
+
this.#ownerConfig = ownerConfig;
|
|
224
294
|
this.schema = schema;
|
|
225
295
|
this.where = where;
|
|
226
296
|
this.#busyTimeoutMs = busyTimeoutMs;
|
|
227
297
|
this.dialect = postgresDialect(schema, { testClock });
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
298
|
+
if (owner)
|
|
299
|
+
this.#watch(owner);
|
|
300
|
+
}
|
|
301
|
+
/** The owner lock lives on its own connection: when that connection ends or fails (the database restarted, a failover,
|
|
302
|
+
* an operator killed it), the lock may be gone and another server may take the schema. From then this store refuses
|
|
303
|
+
* every call (StoreOwned), never writing beside a server that may hold the schema now, and takes the lock back
|
|
304
|
+
* (#retake). */
|
|
305
|
+
#watch(session) {
|
|
231
306
|
const lost = () => {
|
|
232
|
-
if (
|
|
233
|
-
|
|
307
|
+
if (this.#closed || this.#owner !== session || this.#ownerLost)
|
|
308
|
+
return;
|
|
309
|
+
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`);
|
|
310
|
+
void this.#retake(session);
|
|
234
311
|
};
|
|
235
|
-
|
|
236
|
-
|
|
312
|
+
session.client.on("error", lost);
|
|
313
|
+
session.client.on("end", lost);
|
|
314
|
+
}
|
|
315
|
+
/** Takes the owner lock back on a fresh connection, after OWNER_RETAKE_DELAY_MS (a server waiting for the schema takes
|
|
316
|
+
* it first). Held by this server's own lost session (the database not yet told its client is gone: a partition), that
|
|
317
|
+
* session is ended and the lock taken. Held by another server, or not taken within ownerWaitMs, it is lost for good:
|
|
318
|
+
* every call is refused from then, and onOwnerLost is told, so the process exits and its platform restarts it. */
|
|
319
|
+
async #retake(old) {
|
|
320
|
+
const config = this.#ownerConfig;
|
|
321
|
+
const until = Date.now() + OWNER_RETAKE_DELAY_MS + config.waitMs;
|
|
322
|
+
await sleep(OWNER_RETAKE_DELAY_MS);
|
|
323
|
+
while (!this.#closed) {
|
|
324
|
+
const asked = await tryOwner(config.url, this.schema, this.#connectMs, this.where).catch(() => undefined);
|
|
325
|
+
if (asked && "taken" in asked) {
|
|
326
|
+
if (this.#closed)
|
|
327
|
+
return void (await asked.taken.client.end().catch(() => undefined));
|
|
328
|
+
// While the lock was not held, a newer server may have taken the schema, migrated it and let it go: this build
|
|
329
|
+
// serves only the schema it opened, so a schema it does not know gives the lock up for good.
|
|
330
|
+
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;
|
|
331
|
+
if (unknown !== undefined) {
|
|
332
|
+
await asked.taken.client.end().catch(() => undefined);
|
|
333
|
+
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`));
|
|
334
|
+
}
|
|
335
|
+
// Closed while the schema was checked: close() ended only the old session, so this one lets the lock go here.
|
|
336
|
+
if (this.#closed)
|
|
337
|
+
return void (await asked.taken.client.end().catch(() => undefined));
|
|
338
|
+
this.#owner = asked.taken;
|
|
339
|
+
this.#watch(asked.taken);
|
|
340
|
+
this.#ownerLost = undefined;
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const holder = asked?.heldBy;
|
|
344
|
+
if (holder && holder.pid === old.pid && holder.startedAt === old.startedAt) {
|
|
345
|
+
// This server's own session, its client gone but its backend not: ended, so its lock comes back.
|
|
346
|
+
await endSession(config.url, old.pid, this.#connectMs);
|
|
347
|
+
}
|
|
348
|
+
else if (holder) {
|
|
349
|
+
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`));
|
|
350
|
+
}
|
|
351
|
+
if (Date.now() >= until) {
|
|
352
|
+
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`));
|
|
353
|
+
}
|
|
354
|
+
await sleep(OWNER_POLL_MS);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
#lostForGood(error) {
|
|
358
|
+
if (this.#closed)
|
|
359
|
+
return;
|
|
360
|
+
this.#ownerLost = error;
|
|
361
|
+
this.#ownerConfig?.onLost?.(error);
|
|
237
362
|
}
|
|
238
363
|
/** 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 }) {
|
|
364
|
+
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
365
|
if (!SCHEMA_NAME.test(schema))
|
|
241
366
|
throw new TypeError(`the store's schema is a lower-case name of letters, digits and underscores, not ${JSON.stringify(schema)}`);
|
|
242
367
|
// The database's end of the keepalives (KEEPALIVE_IDLE_S): a vanished client's session, and its locks, ended in ~25 s.
|
|
@@ -249,27 +374,26 @@ export class PgConnection {
|
|
|
249
374
|
const tcp = { keepAlive: true, keepAliveInitialDelayMillis: KEEPALIVE_IDLE_S * 1_000 };
|
|
250
375
|
const config = { connectionString: url, options: session, types, max: poolSize, allowExitOnIdle: true, application_name: "agentrun-store", connectionTimeoutMillis: connectMs, ...tcp };
|
|
251
376
|
let lock;
|
|
377
|
+
const waitMs = Math.max(0, Math.floor(ownerWaitMs));
|
|
252
378
|
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);
|
|
379
|
+
// Another server's lock is waited for (a deploy's old server draining and stopping), asked again every
|
|
380
|
+
// OWNER_POLL_MS, up to waitMs; past it StoreOwned. Its keepalives: OWNER_SESSION.
|
|
381
|
+
const until = Date.now() + waitMs;
|
|
382
|
+
let told = false;
|
|
383
|
+
for (;;) {
|
|
384
|
+
const asked = await tryOwner(url, schema, connectMs, where);
|
|
385
|
+
if ("taken" in asked) {
|
|
386
|
+
lock = asked.taken;
|
|
387
|
+
break;
|
|
388
|
+
}
|
|
389
|
+
if (Date.now() >= until) {
|
|
390
|
+
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` : ""}`);
|
|
391
|
+
}
|
|
392
|
+
if (!told) {
|
|
393
|
+
told = true;
|
|
394
|
+
onOwnerWait?.(`another server holds the store in the Postgres schema ${schema}: waiting up to ${Math.round(waitMs / 1000)} s for it to let go`);
|
|
395
|
+
}
|
|
396
|
+
await sleep(OWNER_POLL_MS);
|
|
273
397
|
}
|
|
274
398
|
}
|
|
275
399
|
const pool = new pg.Pool(config);
|
|
@@ -287,7 +411,7 @@ export class PgConnection {
|
|
|
287
411
|
each.on("error", () => undefined);
|
|
288
412
|
each.on("connect", (fresh) => fresh.on("error", () => undefined));
|
|
289
413
|
}
|
|
290
|
-
return new PgConnection(pool, lock, schema, busyTimeoutMs, where, testClock, connectMs, leasePool);
|
|
414
|
+
return new PgConnection(pool, lock, schema, busyTimeoutMs, where, testClock, connectMs, leasePool, lock ? { url, waitMs, onLost: onOwnerLost, check: ownerCheck } : undefined);
|
|
291
415
|
}
|
|
292
416
|
/** `work`, counted in flight, refused after close. A connection it never got in time is StoreDeadline, naming the
|
|
293
417
|
* database's place. */
|
|
@@ -527,6 +651,6 @@ export class PgConnection {
|
|
|
527
651
|
await Promise.allSettled([...this.#inFlight]);
|
|
528
652
|
await this.#pool.end().catch(() => undefined);
|
|
529
653
|
await this.#leasePool.end().catch(() => undefined);
|
|
530
|
-
await this.#owner?.end().catch(() => undefined);
|
|
654
|
+
await this.#owner?.client.end().catch(() => undefined);
|
|
531
655
|
}
|
|
532
656
|
}
|
|
@@ -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) => {
|
|
@@ -70,6 +72,19 @@ export async function dropPostgresSchema(url, schema) {
|
|
|
70
72
|
await client.end().catch(() => undefined);
|
|
71
73
|
}
|
|
72
74
|
}
|
|
75
|
+
/** Makes `schema` (absent, under the migration lock). A role with no CREATE on the database is refused in words that say
|
|
76
|
+
* what to do: the schema made ahead of time, owned by this role, or CREATE granted. */
|
|
77
|
+
async function createSchema(client, schema) {
|
|
78
|
+
try {
|
|
79
|
+
await client.query(`CREATE SCHEMA ${schema}`);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
// 42501: insufficient_privilege.
|
|
83
|
+
if (error?.code !== "42501")
|
|
84
|
+
throw error;
|
|
85
|
+
throw new Error(`${placeOf(schema)} does not exist, and this role may not create it (no CREATE on the database): create the schema ahead of time, owned by this role, or grant this role CREATE on the database`, { cause: error });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
73
88
|
/** Applies every pending migration to `schema`, in one transaction under the schema's migration lock, at `at` (ms). A
|
|
74
89
|
* schema at a version this build does not know is refused (SchemaVersionUnknown), and so is an applied file whose text
|
|
75
90
|
* changed since. Answers the version it leaves. */
|
|
@@ -79,7 +94,11 @@ export async function migrate(client, schema, at, migrations = migrationsIn()) {
|
|
|
79
94
|
await client.query("BEGIN");
|
|
80
95
|
try {
|
|
81
96
|
await client.query("SELECT pg_advisory_xact_lock(hashtext($1), hashtext('migrate'))", [schema]);
|
|
82
|
-
|
|
97
|
+
// Made only when absent: Postgres checks CREATE on the database before IF NOT EXISTS, so a role that owns a schema an
|
|
98
|
+
// administrator made ahead of time, with no CREATE on the database, could not start on it otherwise.
|
|
99
|
+
const exists = (await client.query("SELECT 1 FROM pg_namespace WHERE nspname = $1", [schema])).rowCount !== 0;
|
|
100
|
+
if (!exists)
|
|
101
|
+
await createSchema(client, schema);
|
|
83
102
|
await client.query(`SET LOCAL search_path TO ${schema}`);
|
|
84
103
|
await client.query("CREATE TABLE IF NOT EXISTS schema_migrations (version BIGINT PRIMARY KEY, name TEXT NOT NULL, sha256 TEXT NOT NULL, applied_at BIGINT NOT NULL)");
|
|
85
104
|
const applied = (await client.query("SELECT version, name, sha256 FROM schema_migrations ORDER BY version")).rows;
|
|
@@ -93,6 +112,11 @@ export async function migrate(client, schema, at, migrations = migrationsIn()) {
|
|
|
93
112
|
}
|
|
94
113
|
}
|
|
95
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
|
+
}
|
|
96
120
|
await client.query(migration.sql);
|
|
97
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]);
|
|
98
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" }),
|
|
@@ -10,6 +10,42 @@ import { inMemory } from "./memory.js";
|
|
|
10
10
|
import { Fenced, RunFinal, StoreBusy, StoreClosed, StoreConstraint, StoreDeadline, StoreOwned, StoreWorkerLost } from "../store.js";
|
|
11
11
|
/** How long a transaction may stay open (it holds every other call of the process) before it is rolled back. */
|
|
12
12
|
export const DEFAULT_TRANSACTION_DEADLINE_MS = 10_000;
|
|
13
|
+
/** How often an open transaction's deadline looks at the process's event loop, and how far past its look a look may
|
|
14
|
+
* come before the gap counts as the process held up (runnableDeadline). */
|
|
15
|
+
const TICK_MS = 50;
|
|
16
|
+
const STALL_MS = 50;
|
|
17
|
+
/** Calls `expire` once `ms` of the time this process could run have passed, counted from now on the monotonic clock
|
|
18
|
+
* (performance.now: the host's clock set forward or back is never counted). A look every TICK_MS measures the event
|
|
19
|
+
* loop: a gap more than STALL_MS past its look was the process held up by other work (a harness binary hashed, a
|
|
20
|
+
* command run synchronously, the machine holding the process), wherever in the transaction it fell, before its
|
|
21
|
+
* deadline or across it, and is not counted. A wait of the transaction's own (a call awaited inside it) leaves the
|
|
22
|
+
* loop free, so it counts in full and is refused on time. Answers its clear. */
|
|
23
|
+
function runnableDeadline(ms, expire) {
|
|
24
|
+
const start = performance.now();
|
|
25
|
+
let last = start;
|
|
26
|
+
let held = 0;
|
|
27
|
+
const look = () => {
|
|
28
|
+
const now = performance.now();
|
|
29
|
+
const gap = now - last - TICK_MS;
|
|
30
|
+
if (gap > STALL_MS)
|
|
31
|
+
held += gap;
|
|
32
|
+
last = now;
|
|
33
|
+
return now;
|
|
34
|
+
};
|
|
35
|
+
const ticks = setInterval(look, TICK_MS);
|
|
36
|
+
let timer;
|
|
37
|
+
const check = () => {
|
|
38
|
+
const left = ms - (look() - start - held);
|
|
39
|
+
if (left <= 0)
|
|
40
|
+
return expire();
|
|
41
|
+
timer = setTimeout(check, Math.ceil(left));
|
|
42
|
+
};
|
|
43
|
+
timer = setTimeout(check, ms);
|
|
44
|
+
return () => {
|
|
45
|
+
clearInterval(ticks);
|
|
46
|
+
clearTimeout(timer);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
13
49
|
/** A statement's text parameters as the store writes text (dialect.ts cleanText: no NUL), on both stores alike. */
|
|
14
50
|
const cleaned = (params) => (params ?? []).map((value) => (typeof value === "string" ? cleanText(value) : value));
|
|
15
51
|
const cleanStatement = (statement) => ({ ...statement, params: cleaned(statement.params) });
|
|
@@ -234,10 +270,18 @@ export class Connection {
|
|
|
234
270
|
throw error;
|
|
235
271
|
}
|
|
236
272
|
let live = true;
|
|
237
|
-
let
|
|
238
|
-
const left = until !== undefined ? Math.max(0, until - Date.now()) : deadlineMs;
|
|
273
|
+
let clear = () => { };
|
|
239
274
|
const deadline = new Promise((_, reject) => {
|
|
240
|
-
|
|
275
|
+
const expire = () => reject(new StoreDeadline(deadlineMs));
|
|
276
|
+
// Counted from its call (a lease's), the deadline stays the wall clock's: MCP_REFRESH_LEASE_MS is reckoned from
|
|
277
|
+
// it. Counted from its begin, it is the time this process could run (runnableDeadline): while other work held
|
|
278
|
+
// the event loop, a statement answered meanwhile waited behind it, and that time was no statement's.
|
|
279
|
+
if (until !== undefined) {
|
|
280
|
+
const timer = setTimeout(expire, Math.max(0, until - Date.now()));
|
|
281
|
+
clear = () => clearTimeout(timer);
|
|
282
|
+
}
|
|
283
|
+
else
|
|
284
|
+
clear = runnableDeadline(deadlineMs, expire);
|
|
241
285
|
});
|
|
242
286
|
let value;
|
|
243
287
|
try {
|
|
@@ -250,7 +294,7 @@ export class Connection {
|
|
|
250
294
|
}
|
|
251
295
|
catch (error) {
|
|
252
296
|
live = false;
|
|
253
|
-
|
|
297
|
+
clear();
|
|
254
298
|
try {
|
|
255
299
|
await this.#send({ op: "rollback" });
|
|
256
300
|
}
|
|
@@ -263,7 +307,7 @@ export class Connection {
|
|
|
263
307
|
throw error;
|
|
264
308
|
}
|
|
265
309
|
live = false;
|
|
266
|
-
|
|
310
|
+
clear();
|
|
267
311
|
// Counted from its call, a deadline reached by now is never committed past: rolled back, nothing written.
|
|
268
312
|
if (until !== undefined && Date.now() >= until) {
|
|
269
313
|
await this.#send({ op: "rollback" });
|
|
@@ -6,6 +6,9 @@ import { type StoreNow } from "../fence.js";
|
|
|
6
6
|
* runner, no box: its only writes are the learning folder's one script). */
|
|
7
7
|
/** A learning run's launch: harness "learning", its input the run that learned, and `model`, the grade model when one is
|
|
8
8
|
* named (gradeModelOf; none: the agent's own grades it, as main's). No cap: main caps no grade. */
|
|
9
|
+
/** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
|
|
10
|
+
* list of runs names. */
|
|
11
|
+
export declare const NOT_A_LEARNING_RUN = "id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL)";
|
|
9
12
|
export declare const learningLaunchOf: (runId: string, model?: string) => {
|
|
10
13
|
model?: string | undefined;
|
|
11
14
|
harness: string;
|
|
@@ -11,6 +11,9 @@ import { writeLaunch } from "./run-launch.js";
|
|
|
11
11
|
* runner, no box: its only writes are the learning folder's one script). */
|
|
12
12
|
/** A learning run's launch: harness "learning", its input the run that learned, and `model`, the grade model when one is
|
|
13
13
|
* named (gradeModelOf; none: the agent's own grades it, as main's). No cap: main caps no grade. */
|
|
14
|
+
/** A WHERE condition on `runs`: the run is no built-in learning run (one a kept learning names, queued with it), which no
|
|
15
|
+
* list of runs names. */
|
|
16
|
+
export const NOT_A_LEARNING_RUN = "id NOT IN (SELECT learning_run_id FROM learning_proposals WHERE learning_run_id IS NOT NULL)";
|
|
14
17
|
export const learningLaunchOf = (runId, model) => ({ harness: "learning", input: { runId }, title: `Learning from ${runId}`, place: { mode: "runner" }, ...(model ? { model } : {}) });
|
|
15
18
|
/** The lock a write of a run's sign_in_handed and a sign-in's forget both take (Dialect.lockKey): the forget's check of
|
|
16
19
|
* the running runs' hashes and a launch's write of them are serialized, as SQLite's one writer serializes them. */
|