@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
package/dist/webhooks.js
CHANGED
|
@@ -3,22 +3,19 @@
|
|
|
3
3
|
// once carried back). Each message is signed by the Standard Webhooks spec (standardwebhooks.com), so a receiver checks
|
|
4
4
|
// it with any of that spec's libraries: `webhook-id` (the same on every try: the receiver's idempotency key),
|
|
5
5
|
// `webhook-timestamp` (Unix seconds) and `webhook-signature: v1,<base64 HMAC-SHA256 of "{id}.{timestamp}.{body}">`
|
|
6
|
-
// under the secret `whsec_<base64 of 32 bytes>`, which this server makes at the first PUT
|
|
7
|
-
//
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
6
|
+
// under the secret `whsec_<base64 of 32 bytes>`, which this server makes at the first PUT and shows once. Everything is in
|
|
7
|
+
// the one store, on SQLite and Postgres alike: the webhook in the setting WEBHOOK_SETTING, its secret sealed under the
|
|
8
|
+
// server's seal key among the server's sealed settings (sealed-settings-store.ts, adapter OUTBOUND_WEBHOOK), and the
|
|
9
|
+
// outbox in the setting OUTBOX_SETTING. A message not answered 2xx is tried again on the spec's schedule, cut at about a
|
|
10
|
+
// day; a 410 turns the webhook off. The outbox holds the messages and the last attempts, so a restart goes on with those
|
|
11
|
+
// pending, and with them, in the same write, the ledger: the end and the wait each run's notices were queued for. A run's
|
|
12
|
+
// moment is queued once, by whichever server sees it, and a start queues each end the store holds that the ledger does not
|
|
13
|
+
// (catchUp): a kill between the run's end and the outbox write loses no run.ended, and sends none twice.
|
|
13
14
|
import { createHash, randomBytes } from "node:crypto";
|
|
14
|
-
import { readFileSync, rmSync } from "node:fs";
|
|
15
|
-
import { join } from "node:path";
|
|
16
|
-
import { seal, unseal } from "@parcha/pi-durable-agent";
|
|
17
15
|
import { ApiError, invalid } from "./errors.js";
|
|
18
|
-
import {
|
|
19
|
-
import {
|
|
16
|
+
import { SERVER_SCOPE } from "./sealed-settings-store.js";
|
|
17
|
+
import { signatureOf } from "./standard-webhooks.js";
|
|
20
18
|
import { webhookSecretMeta } from "./trigger-secrets-shown.js";
|
|
21
|
-
import { SEAL_KEY_ENV, sealKeyOf } from "./tool-keys.js";
|
|
22
19
|
import { isObject, unknownFields } from "./validate.js";
|
|
23
20
|
/** A run's two notices, which runs.ts tells. */
|
|
24
21
|
export const RUN_EVENTS = ["run.needs_input", "run.ended"];
|
|
@@ -26,8 +23,14 @@ export const RUN_EVENTS = ["run.needs_input", "run.ended"];
|
|
|
26
23
|
export const WEBHOOK_EVENTS = [...RUN_EVENTS, "trigger.failed"];
|
|
27
24
|
/** The message types: a run's two notices, and the test send's. */
|
|
28
25
|
export const MESSAGE_TYPES = [...WEBHOOK_EVENTS, "webhook.test"];
|
|
29
|
-
|
|
30
|
-
export const
|
|
26
|
+
/** The store setting the webhook is kept in: its URL, when its secret was made, and why it is off, if it is. */
|
|
27
|
+
export const WEBHOOK_SETTING = "webhook";
|
|
28
|
+
/** The store setting the outbox is kept in: the messages, the last attempts and the ledger, written whole. */
|
|
29
|
+
export const OUTBOX_SETTING = "webhook_outbox";
|
|
30
|
+
/** The webhook's signing secret among the server's sealed settings (sealed-settings-store.ts): minted at the first PUT,
|
|
31
|
+
* kept sealed in the store, retired (still cut from what runs say) when it goes. Not a trigger source: its own adapter id. */
|
|
32
|
+
export const OUTBOUND_WEBHOOK = { id: "outbound_webhook", settings: { scope: "server", fields: { secret: { mint: true } } } };
|
|
33
|
+
const SECRET_FIELD = "secret";
|
|
31
34
|
/** The waits before each try again, after the first at once: the Standard Webhooks schedule, cut at about a day. */
|
|
32
35
|
export const RETRY_MS = [5_000, 300_000, 1_800_000, 7_200_000, 18_000_000, 36_000_000];
|
|
33
36
|
const TIMEOUT_MS = 15_000;
|
|
@@ -36,53 +39,52 @@ const OUTBOX_RETRY_MS = 5_000;
|
|
|
36
39
|
/** How far back an end's run.ended is still queued: past the outbox's own retries (RETRY_MS, about a day), a notice
|
|
37
40
|
* would not be sent any more. */
|
|
38
41
|
const END_NOTICE_WINDOW_MS = 24 * 3_600_000;
|
|
39
|
-
/** The outbox
|
|
40
|
-
const OUTBOX_VERSION =
|
|
42
|
+
/** The outbox's version: one of another is not read. */
|
|
43
|
+
const OUTBOX_VERSION = 1;
|
|
41
44
|
/** How many attempts the log keeps, and with them the messages they tried, for a redelivery. */
|
|
42
45
|
export const ATTEMPTS_KEPT = 50;
|
|
43
|
-
const FORMAT = "webhook/1";
|
|
44
46
|
const MAX_URL = 2048;
|
|
45
|
-
|
|
47
|
+
/** How many of a run's waits the ledger keeps: a run asks again with a new mark, so only an old ask read again (a stale
|
|
48
|
+
* read of the run) needs its mark, and that is one of its last few. */
|
|
49
|
+
const WAITS_KEPT = 20;
|
|
46
50
|
const iso = (ms) => new Date(ms).toISOString();
|
|
47
51
|
export { signatureOf } from "./standard-webhooks.js";
|
|
48
|
-
/** The webhook as
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
const doc = JSON.parse(readFileSync(file, "utf8"));
|
|
52
|
-
return isObject(doc) && doc.v === 1 && typeof doc.url === "string" && typeof doc.secret === "string" ? doc : undefined;
|
|
53
|
-
}
|
|
54
|
-
catch {
|
|
55
|
-
return undefined;
|
|
56
|
-
}
|
|
52
|
+
/** The webhook as the store keeps it, if it holds one. */
|
|
53
|
+
function configOf(value) {
|
|
54
|
+
return isObject(value) && value.v === 1 && typeof value.url === "string" && typeof value.secret_at === "string" && typeof value.updated_at === "string" ? value : undefined;
|
|
57
55
|
}
|
|
58
|
-
/**
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
56
|
+
/** The mark of the wait a run stands at, its webhook-id's marker: one per ask, however often or whenever the run is read,
|
|
57
|
+
* so every read of the same ask makes the same webhook-id. A question's is its waiting row (run-view.ts withAsks: the
|
|
58
|
+
* asked call's result, `:waiting`); an approval's, the call it holds; a budget pause's, its time; a hand-over's, when it
|
|
59
|
+
* was asked; a stop, which waits on no ask, the last row that shows (not usage, a phase or learning's). Never the last
|
|
60
|
+
* row for an ask: a run read between the person's answer and its runner's take of it shows the answer last, and a
|
|
61
|
+
* marker from that row would tell the same ask twice, under two ids. Undefined when there is none. */
|
|
62
|
+
export function waitMarkerOf(rows, pending, waiting, learningLabel) {
|
|
63
|
+
const last = () => rows.findLast((r) => r.kind !== "usage" && r.kind !== "phase" && r.agent_label !== learningLabel)?.id;
|
|
64
|
+
if (!waiting)
|
|
65
|
+
return last();
|
|
66
|
+
const kind = pending?.kind;
|
|
67
|
+
if (kind === "approval" && typeof pending?.id === "string")
|
|
68
|
+
return `approval:${pending.id}`;
|
|
69
|
+
if (kind === "budget" && typeof pending?.at === "string")
|
|
70
|
+
return `budget:${pending.at}`;
|
|
71
|
+
if ((kind === "browser" || kind === "computer") && typeof pending?.asked_at === "string")
|
|
72
|
+
return `${kind}:${pending.asked_at}`;
|
|
73
|
+
return rows.findLast((r) => r.kind === "waiting")?.id ?? last();
|
|
75
74
|
}
|
|
76
75
|
export class Webhooks {
|
|
76
|
+
store;
|
|
77
|
+
secret;
|
|
77
78
|
say;
|
|
78
79
|
retryMs;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
key;
|
|
82
|
-
messages;
|
|
83
|
-
attempts;
|
|
80
|
+
messages = [];
|
|
81
|
+
attempts = [];
|
|
84
82
|
/** The ledger, by run: written with the messages, in the same write. */
|
|
85
|
-
notices;
|
|
83
|
+
notices = {};
|
|
84
|
+
/** The webhook as the store holds it: read at load, then kept with every write (this server is the store's one writer).
|
|
85
|
+
* A write that failed (a 410 that turned it off, on a store that refused the write) leaves it held here as it now is, and
|
|
86
|
+
* the next PUT writes it whole. */
|
|
87
|
+
current;
|
|
86
88
|
/** The runs told of since this server started: their ledger entries are this server's news, which a start's catch-up
|
|
87
89
|
* never drops. */
|
|
88
90
|
told = new Set();
|
|
@@ -92,30 +94,37 @@ export class Webhooks {
|
|
|
92
94
|
trying = new Set();
|
|
93
95
|
stop = new AbortController();
|
|
94
96
|
closed = false;
|
|
95
|
-
/** The outbox as held here is not
|
|
97
|
+
/** The outbox as held here is not in the store: its last write failed. Written again soon, and at the close. */
|
|
96
98
|
unsaved = false;
|
|
97
99
|
saving;
|
|
98
|
-
/** The
|
|
99
|
-
|
|
100
|
-
|
|
100
|
+
/** The outbox's writes, one after another: each writes the outbox as it stands when its turn comes. */
|
|
101
|
+
writing = Promise.resolve(true);
|
|
102
|
+
/** What reads the webhook and then writes it (a PUT, a DELETE, a try's 410 that turns it off), one after another: each
|
|
103
|
+
* reads it as the last one left it, so a 410 never turns off a webhook a PUT set again meanwhile. */
|
|
104
|
+
changing = Promise.resolve();
|
|
105
|
+
/** A DELETE under way: no notice is queued meanwhile, so none slips past its drop of the pending messages. */
|
|
106
|
+
removing = false;
|
|
101
107
|
/** The messages with a try in flight, by id, and how many: kept in the outbox until each lands, so one whose earlier
|
|
102
108
|
* attempts left the log meanwhile can still be redelivered. */
|
|
103
109
|
sending = new Map();
|
|
104
|
-
/** `
|
|
105
|
-
|
|
110
|
+
/** `store` keeps the webhook and its outbox, `secret` its signing secret (the server's sealed settings for
|
|
111
|
+
* OUTBOUND_WEBHOOK, loaded); `say` takes a log line's fields; `retryMs` the waits between tries. load() before use. */
|
|
112
|
+
constructor(store, secret, say, retryMs = RETRY_MS) {
|
|
113
|
+
this.store = store;
|
|
114
|
+
this.secret = secret;
|
|
106
115
|
this.say = say;
|
|
107
116
|
this.retryMs = retryMs;
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
117
|
+
}
|
|
118
|
+
/** Reads the webhook and the outbox as the last server left them, and goes on with the messages it left pending, each at
|
|
119
|
+
* its next try's time; a test send is tried once, so one left in flight failed. Awaited once, before anything else. */
|
|
120
|
+
async load() {
|
|
121
|
+
this.current = configOf(await this.store.settings.get(WEBHOOK_SETTING));
|
|
122
|
+
({ messages: this.messages, attempts: this.attempts, notices: this.notices } = this.outboxOf(await this.store.settings.get(OUTBOX_SETTING)));
|
|
114
123
|
const tests = this.messages.filter((m) => m.status === "pending" && m.type === "webhook.test");
|
|
115
124
|
for (const test of tests)
|
|
116
125
|
Object.assign(test, { status: "failed", next_at: null });
|
|
117
126
|
if (tests.length)
|
|
118
|
-
this.writeOutbox();
|
|
127
|
+
await this.writeOutbox();
|
|
119
128
|
for (const message of this.messages)
|
|
120
129
|
if (message.status === "pending")
|
|
121
130
|
this.schedule(message, Math.max(0, (message.next_at ?? 0) - Date.now()));
|
|
@@ -124,37 +133,18 @@ export class Webhooks {
|
|
|
124
133
|
attach(base) {
|
|
125
134
|
this.base = base.replace(/\/+$/, "");
|
|
126
135
|
}
|
|
127
|
-
/** The secret, opened here, and each one this server held before it since it started (removed by a DELETE, or
|
|
128
|
-
* replaced), for the server's list of its own secrets (server-secrets.ts): a run that echoes an old one is cut all the
|
|
129
|
-
* same. None that is not there, or does not open. */
|
|
130
|
-
signingSecrets() {
|
|
131
|
-
const now = this.signingSecret();
|
|
132
|
-
return [...(now ? [now] : []), ...this.retired];
|
|
133
|
-
}
|
|
134
|
-
/** The secrets this webhook signed with before, in memory alone, for this process's life. */
|
|
135
|
-
retired = new Set();
|
|
136
|
-
/** The secret, opened here; undefined when there is none, or it does not open. */
|
|
137
|
-
signingSecret() {
|
|
138
|
-
if (this.signing === undefined) {
|
|
139
|
-
const config = this.config();
|
|
140
|
-
const opened = config && this.secret(config);
|
|
141
|
-
this.signing = { value: opened && "secret" in opened ? opened.secret : undefined };
|
|
142
|
-
}
|
|
143
|
-
return this.signing.value;
|
|
144
|
-
}
|
|
145
|
-
/** signingSecret's last reading: read again after any write of the webhook's file, which only this server makes. */
|
|
146
|
-
signing;
|
|
147
136
|
/** GET: the URL, whether the secret signs here, and why the webhook is off, if it is; its secret's metadata (secret-writes.ts
|
|
148
137
|
* SecretMeta), never the secret. */
|
|
149
138
|
view() {
|
|
150
139
|
const config = this.config();
|
|
151
140
|
if (!config)
|
|
152
141
|
return { configured: false, secret: webhookSecretMeta(false, null) };
|
|
153
|
-
const
|
|
154
|
-
return { configured: true, url: config.url, signing
|
|
142
|
+
const signing = this.signingSecret() !== undefined ? "sealed" : "unreadable";
|
|
143
|
+
return { configured: true, url: config.url, signing, secret: webhookSecretMeta(true, config.secret_at), disabled: config.disabled ?? null, updated_at: config.updated_at };
|
|
155
144
|
}
|
|
156
|
-
/** PUT: the URL, and the webhook on again. The first makes the secret and answers it, once; a later one keeps it
|
|
157
|
-
|
|
145
|
+
/** PUT: the URL, and the webhook on again. The first makes the secret and answers it, once; a later one keeps it, or
|
|
146
|
+
* makes a new one (answered once) when the one kept does not open under this server's seal key. */
|
|
147
|
+
async save(body) {
|
|
158
148
|
const issues = unknownFields(body, ["url"]);
|
|
159
149
|
const url = typeof body.url === "string" && body.url.length <= MAX_URL ? URL.parse(body.url) : null;
|
|
160
150
|
if (body.url === undefined)
|
|
@@ -165,81 +155,108 @@ export class Webhooks {
|
|
|
165
155
|
issues.push({ field: "url", reason: "invalid", message: "url holds no user or password: the receiver checks the signature" });
|
|
166
156
|
if (issues.length)
|
|
167
157
|
throw invalid(issues);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
158
|
+
return this.change(async () => {
|
|
159
|
+
const was = this.config();
|
|
160
|
+
const now = new Date().toISOString();
|
|
161
|
+
const kept = was !== undefined && this.signingSecret() !== undefined;
|
|
162
|
+
const secret = kept ? undefined : await this.secret.mint(SERVER_SCOPE, SECRET_FIELD);
|
|
163
|
+
try {
|
|
164
|
+
await this.write({ url: url.href, secret_at: kept ? was.secret_at : now, updated_at: now });
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
// A secret made here and never shown signs nothing: removed (retired, still cut), so the next PUT makes and shows
|
|
168
|
+
// one, never keeping one nobody was given.
|
|
169
|
+
if (secret)
|
|
170
|
+
await this.secret.remove(SERVER_SCOPE).catch((removal) => this.say({ condition: "secret_unremoved", error: removal.message }));
|
|
171
|
+
throw error;
|
|
172
|
+
}
|
|
173
|
+
return { created: !was, view: this.view(), ...(secret ? { secret } : {}) };
|
|
174
|
+
});
|
|
176
175
|
}
|
|
177
|
-
/** DELETE: the webhook gone,
|
|
176
|
+
/** DELETE: the webhook gone, its secret with it (retired: still cut from what a run says), and its pending messages; the
|
|
177
|
+
* log of attempts stays. */
|
|
178
178
|
remove() {
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
179
|
+
return this.change(async () => {
|
|
180
|
+
if (this.config()) {
|
|
181
|
+
// Shut to new notices first, then its pending messages fail: whatever fails after, none of them is sent, to this
|
|
182
|
+
// receiver or a later one, and none told meanwhile is queued.
|
|
183
|
+
this.removing = true;
|
|
184
|
+
try {
|
|
185
|
+
await this.drop("the webhook was removed");
|
|
186
|
+
await this.store.settings.remove(WEBHOOK_SETTING);
|
|
187
|
+
await this.store.flush();
|
|
188
|
+
this.current = undefined;
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
this.removing = false;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
// Its secret goes last, and on every DELETE (a no-op once gone): a removal of it that failed is finished by the
|
|
195
|
+
// next DELETE, which finds no webhook.
|
|
196
|
+
await this.secret.remove(SERVER_SCOPE);
|
|
197
|
+
});
|
|
188
198
|
}
|
|
189
199
|
/** GET .../deliveries: the last attempts, newest first. */
|
|
190
200
|
attemptsView() {
|
|
191
201
|
return { attempts: [...this.attempts].reverse() };
|
|
192
202
|
}
|
|
193
|
-
/** A run's moment, told by
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
emit(type, run, marker) {
|
|
203
|
+
/** A run's moment, told by the server each time it sees it (a wait reached, an end the store has): queued once, when a
|
|
204
|
+
* webhook is set and on, and kept in the ledger in the same write. `marker` makes its webhook-id: the same moment, seen
|
|
205
|
+
* again after a restart, has the same id, which is a receiver's idempotency key. An end before the webhook was set, or
|
|
206
|
+
* past the outbox's day, counts as sent. */
|
|
207
|
+
async emit(type, run, marker) {
|
|
198
208
|
if (this.closed)
|
|
199
209
|
return "closed";
|
|
200
210
|
this.told.add(run.run_id);
|
|
201
211
|
const ends = type === "run.ended";
|
|
202
212
|
const seen = this.notices[run.run_id];
|
|
203
|
-
|
|
204
|
-
|
|
213
|
+
// One message per (run, wait): a wait the run was told of already, last or earlier, is never queued again.
|
|
214
|
+
if (ends ? seen?.ended === marker : seen?.waits?.includes(marker))
|
|
215
|
+
return this.landed();
|
|
216
|
+
// The waits told, this one newest; the oldest go past WAITS_KEPT.
|
|
217
|
+
const waits = [...(seen?.waits ?? []), marker].slice(-WAITS_KEPT);
|
|
205
218
|
const config = this.config();
|
|
206
|
-
if (!config || config.disabled) {
|
|
207
|
-
// A wait seen while nothing is to be sent is noted here, never written for it: told again, it
|
|
219
|
+
if (!config || config.disabled || this.removing) {
|
|
220
|
+
// A wait seen while nothing is to be sent is noted here, never written for it: told again, it writes nothing.
|
|
208
221
|
if (!ends)
|
|
209
|
-
this.notices[run.run_id] = {
|
|
222
|
+
this.notices[run.run_id] = { waits };
|
|
210
223
|
return "off";
|
|
211
224
|
}
|
|
212
225
|
if (ends && !this.due(marker, config))
|
|
213
226
|
return "queued";
|
|
214
227
|
const id = `msg_${createHash("sha256").update(`${run.run_id}\n${type}\n${marker}`).digest("hex").slice(0, 32)}`;
|
|
215
|
-
// A new wait
|
|
216
|
-
|
|
228
|
+
// A new wait is the one the run stands at; one after an end is a continued run's, past that end, which the store no
|
|
229
|
+
// longer holds.
|
|
230
|
+
this.notices[run.run_id] = ends ? { ...seen, ended: marker } : { waits };
|
|
217
231
|
this.prune(config);
|
|
218
232
|
const data = { run_id: run.run_id, agent_id: run.agent_id, status: run.status, title: run.title, url: `${this.base}/#/runs/${encodeURIComponent(run.run_id)}`, ...(run.pending ? { pending: run.pending } : {}) };
|
|
219
|
-
this.
|
|
220
|
-
|
|
233
|
+
const { message, saved } = this.enqueue(id, type, data);
|
|
234
|
+
this.schedule(message, 0);
|
|
235
|
+
return (await saved) ? "queued" : "unsaved";
|
|
221
236
|
}
|
|
222
237
|
/** A trigger's failed firing (triggers.ts), queued once: its webhook-id is made from the firing's key, so the same
|
|
223
238
|
* failure told again, by a later server too, is the same message. Its trigger keeps whether it was queued. */
|
|
224
|
-
emitTrigger(failed, firingKey) {
|
|
239
|
+
async emitTrigger(failed, firingKey) {
|
|
225
240
|
if (this.closed)
|
|
226
241
|
return "closed";
|
|
227
242
|
const config = this.config();
|
|
228
|
-
if (!config || config.disabled)
|
|
243
|
+
if (!config || config.disabled || this.removing)
|
|
229
244
|
return "off";
|
|
230
245
|
const id = `msg_${createHash("sha256").update(`trigger.failed\n${firingKey}`).digest("hex").slice(0, 32)}`;
|
|
231
|
-
if (
|
|
232
|
-
this.
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
* server
|
|
242
|
-
*
|
|
246
|
+
if (this.messages.some((m) => m.id === id))
|
|
247
|
+
return this.landed();
|
|
248
|
+
const { message, saved } = this.enqueue(id, "trigger.failed", failed);
|
|
249
|
+
this.schedule(message, 0);
|
|
250
|
+
return (await saved) ? "queued" : "unsaved";
|
|
251
|
+
}
|
|
252
|
+
/** A start's catch-up, once its runs are read (`rows`, the store's: a row with no state is an ended one). First, before
|
|
253
|
+
* anything is awaited, the ledger as the store now stands: a run the store no longer has goes, and a wait of a run it
|
|
254
|
+
* holds as ended goes (an end the webhook was off for never moved it past). Then each end the store holds that the
|
|
255
|
+
* ledger does not, and that is still due, is queued under the store's end time, so under the same webhook-id: its
|
|
256
|
+
* server was killed between the run's end and the outbox write, or that write never landed. `current` reads the run as
|
|
257
|
+
* the store holds it right before its end is queued, with its notice (null: no such run now). The server takes
|
|
258
|
+
* requests meanwhile, so a run continued since the rows were read has moved past that end, which was queued before it
|
|
259
|
+
* could be continued: its end is not queued again. */
|
|
243
260
|
async catchUp(rows, current) {
|
|
244
261
|
const config = this.config();
|
|
245
262
|
if (this.closed || !config || config.disabled)
|
|
@@ -248,7 +265,7 @@ export class Webhooks {
|
|
|
248
265
|
let dropped = false;
|
|
249
266
|
for (const [id, noticed] of Object.entries(this.notices)) {
|
|
250
267
|
const row = store.get(id);
|
|
251
|
-
if (this.told.has(id) || (row && (row.state === "running" || noticed.
|
|
268
|
+
if (this.told.has(id) || (row && (row.state === "running" || !noticed.waits?.length)))
|
|
252
269
|
continue;
|
|
253
270
|
if (row && noticed.ended)
|
|
254
271
|
this.notices[id] = { ended: noticed.ended };
|
|
@@ -257,7 +274,7 @@ export class Webhooks {
|
|
|
257
274
|
dropped = true;
|
|
258
275
|
}
|
|
259
276
|
if (dropped)
|
|
260
|
-
this.writeOutbox();
|
|
277
|
+
await this.writeOutbox();
|
|
261
278
|
for (const { id, state, endedAt } of store.values()) {
|
|
262
279
|
if (state === "running" || !endedAt || this.notices[id]?.ended === endedAt || !this.due(endedAt, config))
|
|
263
280
|
continue;
|
|
@@ -268,13 +285,13 @@ export class Webhooks {
|
|
|
268
285
|
this.say({ condition: "end_moved_on", run: id, ended: endedAt });
|
|
269
286
|
continue;
|
|
270
287
|
}
|
|
271
|
-
this.emit("run.ended", now.notice, endedAt);
|
|
288
|
+
await this.emit("run.ended", now.notice, endedAt);
|
|
272
289
|
}
|
|
273
290
|
}
|
|
274
291
|
/** POST .../test: a `webhook.test` message tried once now, never again; its attempt. */
|
|
275
292
|
test() {
|
|
276
293
|
this.need();
|
|
277
|
-
return this.attempt(this.enqueue(`msg_${randomBytes(16).toString("hex")}`, "webhook.test", {}), { retry: false, redelivery: false });
|
|
294
|
+
return this.attempt(this.enqueue(`msg_${randomBytes(16).toString("hex")}`, "webhook.test", {}).message, { retry: false, redelivery: false });
|
|
278
295
|
}
|
|
279
296
|
/** POST .../deliveries/{webhook_id}/redeliver: the message sent again once now, under its own webhook-id and body. */
|
|
280
297
|
redeliver(id) {
|
|
@@ -284,8 +301,9 @@ export class Webhooks {
|
|
|
284
301
|
throw new ApiError("not_found", `no message ${id} among the last ${ATTEMPTS_KEPT} attempts`);
|
|
285
302
|
return this.attempt(message, { retry: false, redelivery: true });
|
|
286
303
|
}
|
|
287
|
-
/** Stops: no try starts, those in flight are cut and waited for, and nothing more is written
|
|
288
|
-
* still pending
|
|
304
|
+
/** Stops: no try starts, those in flight are cut and waited for, and nothing more is written but what a failed write left
|
|
305
|
+
* only here. A message cut here is still pending in the store, for the next server (its receiver may get it twice,
|
|
306
|
+
* under the same webhook-id). Before the store closes. */
|
|
289
307
|
async close() {
|
|
290
308
|
this.closed = true;
|
|
291
309
|
for (const timer of this.timers)
|
|
@@ -294,37 +312,45 @@ export class Webhooks {
|
|
|
294
312
|
clearTimeout(this.saving);
|
|
295
313
|
this.stop.abort();
|
|
296
314
|
await Promise.allSettled([...this.trying]);
|
|
297
|
-
|
|
315
|
+
await this.writing;
|
|
316
|
+
// A notice a failed write left only here is written now, while the store is still this server's.
|
|
298
317
|
if (this.unsaved)
|
|
299
|
-
this.
|
|
318
|
+
await this.persist();
|
|
300
319
|
}
|
|
301
320
|
config() {
|
|
302
|
-
return this.
|
|
321
|
+
return this.current;
|
|
322
|
+
}
|
|
323
|
+
/** Whether what is held here is in the store: once every outbox write asked for so far has settled (each writes it
|
|
324
|
+
* whole), `queued` when the last one landed, else `unsaved`. A moment told again while its first write is still in
|
|
325
|
+
* flight waits for it, never answering `queued` before it lands. */
|
|
326
|
+
async landed() {
|
|
327
|
+
await this.writing;
|
|
328
|
+
return this.unsaved ? "unsaved" : "queued";
|
|
303
329
|
}
|
|
304
|
-
/**
|
|
305
|
-
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
this.signing = undefined;
|
|
310
|
-
// A write that replaces the secret keeps the one it replaced (signingSecrets).
|
|
311
|
-
if (was && was !== this.signingSecret())
|
|
312
|
-
this.retired.add(was);
|
|
330
|
+
/** `work`, once every change of the webhook asked for before it has settled, failed or not. */
|
|
331
|
+
change(work) {
|
|
332
|
+
const done = this.changing.then(work, work);
|
|
333
|
+
this.changing = done.then(() => undefined, () => undefined);
|
|
334
|
+
return done;
|
|
313
335
|
}
|
|
314
|
-
/** The
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
336
|
+
/** The signing secret, opened here; undefined when there is none, or it does not open under this server's seal key. */
|
|
337
|
+
signingSecret() {
|
|
338
|
+
return this.secret.get(SERVER_SCOPE, SECRET_FIELD);
|
|
339
|
+
}
|
|
340
|
+
/** Throws when the write fails: the webhook as held here stays as it was. */
|
|
341
|
+
async write(config) {
|
|
342
|
+
await this.store.settings.put(WEBHOOK_SETTING, { v: 1, ...config });
|
|
343
|
+
await this.store.flush();
|
|
344
|
+
this.current = config;
|
|
345
|
+
}
|
|
346
|
+
/** The outbox as the last server left it. One of another version is not read, and the log says which it was. */
|
|
347
|
+
outboxOf(doc) {
|
|
348
|
+
if (isObject(doc) && doc.v !== OUTBOX_VERSION)
|
|
349
|
+
this.say({ condition: "outbox_not_read", setting: OUTBOX_SETTING, version: String(doc.v), reason: `not this server's version (${OUTBOX_VERSION})` });
|
|
350
|
+
if (!isObject(doc) || doc.v !== OUTBOX_VERSION || !Array.isArray(doc.messages) || !Array.isArray(doc.attempts))
|
|
326
351
|
return { messages: [], attempts: [], notices: {} };
|
|
327
|
-
}
|
|
352
|
+
const notices = isObject(doc.notices) ? Object.fromEntries(Object.entries(doc.notices).filter(([, n]) => isObject(n))) : {};
|
|
353
|
+
return { messages: doc.messages.filter((m) => isObject(m) && typeof m.id === "string" && typeof m.body === "string"), attempts: doc.attempts.filter(isObject), notices };
|
|
328
354
|
}
|
|
329
355
|
/** Whether an end's run.ended is still to be queued: at or after the webhook was set and the outbox's day began (a
|
|
330
356
|
* marker that names no time is not judged by one). The catch-up and the ledger's pruning read the same line, so the
|
|
@@ -333,53 +359,55 @@ export class Webhooks {
|
|
|
333
359
|
const at = Date.parse(endedAt);
|
|
334
360
|
return !Number.isFinite(at) || at >= Math.max(Date.parse(config.updated_at), Date.now() - END_NOTICE_WINDOW_MS);
|
|
335
361
|
}
|
|
336
|
-
/** The ledger without what no start looks for: a run whose end is no longer due goes, with the
|
|
337
|
-
*
|
|
362
|
+
/** The ledger without what no start looks for: a run whose end is no longer due goes, with the waits it ended past.
|
|
363
|
+
* Waits with no end after them stay while the run stands at it, however long: never pruned by age, so the same question
|
|
338
364
|
* is never asked again. */
|
|
339
365
|
prune(config) {
|
|
340
366
|
for (const [id, noticed] of Object.entries(this.notices))
|
|
341
367
|
if (noticed.ended && !this.due(noticed.ended, config))
|
|
342
368
|
delete this.notices[id];
|
|
343
369
|
}
|
|
370
|
+
/** The outbox written, after every write asked for before it; whether it landed. One that failed is kept here and
|
|
371
|
+
* written again until a write lands (or the close writes it). Closed, nothing is written: the store may be the next
|
|
372
|
+
* server's already. */
|
|
344
373
|
writeOutbox() {
|
|
345
|
-
// Closed, the data directory may be another server's already.
|
|
346
374
|
if (this.closed)
|
|
347
|
-
return;
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
375
|
+
return Promise.resolve(!this.unsaved);
|
|
376
|
+
this.writing = this.writing.then(() => this.persist());
|
|
377
|
+
return this.writing.then((landed) => {
|
|
378
|
+
if (!landed && !this.saving && !this.closed) {
|
|
379
|
+
const timer = setTimeout(() => { this.saving = undefined; void this.writeOutbox(); }, OUTBOX_RETRY_MS);
|
|
380
|
+
timer.unref();
|
|
381
|
+
this.saving = timer;
|
|
382
|
+
}
|
|
383
|
+
return landed;
|
|
384
|
+
});
|
|
354
385
|
}
|
|
355
|
-
/** The outbox written whole: the last attempts, and the messages pending or tried by one of
|
|
356
|
-
* one again). False when the write failed, which the log says once until one lands. */
|
|
357
|
-
|
|
386
|
+
/** The outbox written whole, in one durable commit: the last attempts, and the messages pending or tried by one of
|
|
387
|
+
* those (a redelivery sends one again). False when the write failed, which the log says once until one lands. */
|
|
388
|
+
async persist() {
|
|
358
389
|
this.attempts = this.attempts.slice(-ATTEMPTS_KEPT);
|
|
359
390
|
const tried = new Set(this.attempts.map((a) => a.webhook_id));
|
|
360
391
|
this.messages = this.messages.filter((m) => m.status === "pending" || tried.has(m.id) || this.sending.has(m.id));
|
|
361
392
|
try {
|
|
362
|
-
|
|
393
|
+
await this.store.settings.put(OUTBOX_SETTING, { v: OUTBOX_VERSION, messages: this.messages, attempts: this.attempts, notices: this.notices });
|
|
394
|
+
await this.store.flush();
|
|
363
395
|
this.unsaved = false;
|
|
364
396
|
return true;
|
|
365
397
|
}
|
|
366
398
|
catch (error) {
|
|
367
399
|
if (!this.unsaved)
|
|
368
|
-
this.say({ condition: "outbox_unwritable",
|
|
400
|
+
this.say({ condition: "outbox_unwritable", setting: OUTBOX_SETTING, error: error.message });
|
|
369
401
|
this.unsaved = true;
|
|
370
402
|
return false;
|
|
371
403
|
}
|
|
372
404
|
}
|
|
373
|
-
/**
|
|
374
|
-
secret(config) {
|
|
375
|
-
return opened(this.key, config);
|
|
376
|
-
}
|
|
405
|
+
/** A new pending message, and its outbox write (whether it landed). */
|
|
377
406
|
enqueue(id, type, data) {
|
|
378
407
|
const created = new Date().toISOString();
|
|
379
408
|
const message = { id, type, body: JSON.stringify({ type, timestamp: created, data }), created_at: created, status: "pending", attempts: 0, next_at: Date.now() };
|
|
380
409
|
this.messages.push(message);
|
|
381
|
-
this.writeOutbox();
|
|
382
|
-
return message;
|
|
410
|
+
return { message, saved: this.writeOutbox() };
|
|
383
411
|
}
|
|
384
412
|
schedule(message, delayMs) {
|
|
385
413
|
if (this.closed)
|
|
@@ -425,16 +453,16 @@ export class Webhooks {
|
|
|
425
453
|
else if (config.disabled)
|
|
426
454
|
failure = "the webhook is off";
|
|
427
455
|
else {
|
|
428
|
-
const
|
|
429
|
-
if (
|
|
430
|
-
failure =
|
|
456
|
+
const secret = this.signingSecret();
|
|
457
|
+
if (secret === undefined)
|
|
458
|
+
failure = "the secret does not open here (unreadable): set the webhook again";
|
|
431
459
|
else {
|
|
432
460
|
const timestamp = String(Math.floor(now / 1000));
|
|
433
461
|
const started = performance.now();
|
|
434
462
|
try {
|
|
435
463
|
const res = await fetch(config.url, {
|
|
436
464
|
method: "POST", redirect: "manual", signal: AbortSignal.any([AbortSignal.timeout(TIMEOUT_MS), this.stop.signal]),
|
|
437
|
-
headers: { "content-type": "application/json", "user-agent": "agentrun-serve-webhook/1", "webhook-id": message.id, "webhook-timestamp": timestamp, "webhook-signature": signatureOf(
|
|
465
|
+
headers: { "content-type": "application/json", "user-agent": "agentrun-serve-webhook/1", "webhook-id": message.id, "webhook-timestamp": timestamp, "webhook-signature": signatureOf(secret, message.id, timestamp, message.body) },
|
|
438
466
|
body: message.body,
|
|
439
467
|
});
|
|
440
468
|
await res.body?.cancel().catch(() => undefined);
|
|
@@ -450,59 +478,64 @@ export class Webhooks {
|
|
|
450
478
|
}
|
|
451
479
|
if (this.closed)
|
|
452
480
|
return attempt;
|
|
453
|
-
|
|
454
|
-
this.
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
if (
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
const off = { ...standing, disabled };
|
|
483
|
-
try {
|
|
484
|
-
this.write(off);
|
|
481
|
+
// What the try found is settled against the webhook as it stands once every change before it has landed.
|
|
482
|
+
return this.change(async () => {
|
|
483
|
+
if (this.closed)
|
|
484
|
+
return attempt;
|
|
485
|
+
attempt.error = failure ?? null;
|
|
486
|
+
this.attempts.push(attempt);
|
|
487
|
+
// The schedule counts the message's own tries: a redelivery is a try of its own, and leaves it as it was.
|
|
488
|
+
if (!how.redelivery)
|
|
489
|
+
message.attempts += 1;
|
|
490
|
+
// A 410 turns off the webhook as this try found it: one set again or removed while the try waited is not its to turn
|
|
491
|
+
// off, and the 410 is then a failure like any other.
|
|
492
|
+
const standing = this.config();
|
|
493
|
+
const gone = attempt.status_code === 410 && config !== undefined && standing !== undefined && standing.url === config.url && standing.updated_at === config.updated_at && !standing.disabled;
|
|
494
|
+
const wait = how.retry && failure && config && !gone ? this.retryMs[message.attempts - 1] : undefined;
|
|
495
|
+
if (!failure)
|
|
496
|
+
Object.assign(message, { status: "delivered", next_at: null });
|
|
497
|
+
// A redelivery that failed leaves the message as it is, and so does a try that ends after one delivered it.
|
|
498
|
+
else if (!how.redelivery && message.status === "pending") {
|
|
499
|
+
if (wait !== undefined) {
|
|
500
|
+
// Jitter, so messages that failed together do not all come back together.
|
|
501
|
+
const delay = Math.round(wait * (0.8 + 0.4 * Math.random()));
|
|
502
|
+
message.next_at = Date.now() + delay;
|
|
503
|
+
attempt.retry_at = iso(message.next_at);
|
|
504
|
+
this.schedule(message, delay);
|
|
505
|
+
}
|
|
506
|
+
else {
|
|
507
|
+
Object.assign(message, { status: "failed", next_at: null });
|
|
508
|
+
this.say({ condition: "delivery_failed", type: message.type, webhook_id: message.id, attempts: message.attempts, ...(attempt.status_code !== null ? { status_code: attempt.status_code } : {}), error: failure });
|
|
509
|
+
}
|
|
485
510
|
}
|
|
486
|
-
|
|
487
|
-
//
|
|
488
|
-
|
|
489
|
-
|
|
511
|
+
if (gone) {
|
|
512
|
+
// Gone: the receiver says this endpoint is no more. Off, its pending messages dropped, until a PUT sets it again.
|
|
513
|
+
const disabled = { at: attempt.at, reason: "the receiver answered 410 Gone" };
|
|
514
|
+
const off = { ...standing, disabled };
|
|
515
|
+
try {
|
|
516
|
+
await this.write(off);
|
|
517
|
+
}
|
|
518
|
+
catch (error) {
|
|
519
|
+
// Off here all the same: held, and the next PUT writes the webhook whole.
|
|
520
|
+
this.current = off;
|
|
521
|
+
this.say({ condition: "webhook_unwritable", setting: WEBHOOK_SETTING, error: error.message });
|
|
522
|
+
}
|
|
523
|
+
this.say({ condition: "webhook_disabled", url: standing.url, reason: disabled.reason });
|
|
524
|
+
await this.drop("the webhook is off: its receiver answered 410");
|
|
525
|
+
return attempt;
|
|
490
526
|
}
|
|
491
|
-
this.
|
|
492
|
-
this.drop("the webhook is off: its receiver answered 410");
|
|
527
|
+
await this.writeOutbox();
|
|
493
528
|
return attempt;
|
|
494
|
-
}
|
|
495
|
-
this.writeOutbox();
|
|
496
|
-
return attempt;
|
|
529
|
+
});
|
|
497
530
|
}
|
|
498
531
|
/** Every pending message failed, with `why` in the log. */
|
|
499
|
-
drop(why) {
|
|
532
|
+
async drop(why) {
|
|
500
533
|
const pending = this.messages.filter((m) => m.status === "pending");
|
|
501
534
|
for (const message of pending)
|
|
502
535
|
Object.assign(message, { status: "failed", next_at: null });
|
|
503
536
|
if (pending.length)
|
|
504
537
|
this.say({ condition: "messages_dropped", count: pending.length, reason: why });
|
|
505
|
-
this.writeOutbox();
|
|
538
|
+
await this.writeOutbox();
|
|
506
539
|
}
|
|
507
540
|
/** The webhook, set and on: a test or a redelivery sends nothing to one a 410 turned off, as no try does. */
|
|
508
541
|
need() {
|