@parcha/agentrun-serve 0.1.0-beta.36 → 0.1.0-beta.37
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 +170 -0
- package/README.md +158 -52
- package/dist/attach-conformance-cli.d.ts +1 -0
- package/dist/attach-conformance-cli.js +18 -0
- package/dist/attach-conformance.d.ts +25 -0
- package/dist/attach-conformance.js +343 -0
- package/dist/browser-cost.d.ts +1 -1
- package/dist/browser-live.d.ts +20 -8
- package/dist/browser-live.js +50 -27
- package/dist/cap.d.ts +0 -17
- package/dist/cap.js +0 -91
- package/dist/catalog.d.ts +1 -12
- package/dist/catalog.js +3 -33
- package/dist/cli.js +13 -4
- package/dist/composio-preset.d.ts +36 -0
- package/dist/composio-preset.js +179 -0
- package/dist/computer-attach.d.ts +125 -0
- package/dist/computer-attach.js +330 -0
- package/dist/computer-calls.d.ts +48 -0
- package/dist/computer-calls.js +126 -0
- package/dist/computer-channel.d.ts +57 -0
- package/dist/computer-channel.js +115 -0
- package/dist/computer-commands.d.ts +1 -1
- package/dist/computer-held-routes.d.ts +35 -0
- package/dist/computer-held-routes.js +106 -0
- package/dist/computer-held.d.ts +48 -0
- package/dist/computer-held.js +87 -0
- package/dist/computer-keys.d.ts +21 -1
- package/dist/computer-keys.js +53 -11
- package/dist/computer-live.d.ts +160 -0
- package/dist/computer-live.js +643 -0
- package/dist/computer-view.d.ts +13 -0
- package/dist/computer-view.js +50 -3
- package/dist/computer-viewer.d.ts +6 -0
- package/dist/computer-viewer.js +297 -0
- package/dist/computers.d.ts +33 -7
- package/dist/computers.js +50 -8
- package/dist/console/assets/index-Cn6JX1uH.js +563 -0
- package/dist/console/assets/index-DRN62V5v.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/faux-computers.d.ts +9 -20
- package/dist/faux-computers.js +53 -25
- package/dist/github-connection.d.ts +10 -2
- package/dist/github-connection.js +65 -2
- package/dist/github-faux.js +1 -1
- package/dist/graph-view.d.ts +1 -1
- package/dist/harness.d.ts +1 -0
- package/dist/harness.js +35 -1
- package/dist/host-tools.d.ts +1 -1
- package/dist/hosted-search.d.ts +5 -4
- package/dist/hosted-search.js +2 -7
- package/dist/import-sources.d.ts +10 -0
- package/dist/import-sources.js +44 -2
- package/dist/import.d.ts +3 -3
- package/dist/import.js +37 -6
- package/dist/learn/check.d.ts +4 -1
- package/dist/learn/check.js +1 -1
- package/dist/learn/commit.d.ts +4 -2
- package/dist/learn/commit.js +5 -5
- package/dist/learn/folder.d.ts +6 -0
- package/dist/learn/folder.js +4 -1
- package/dist/learn/general.d.ts +66 -0
- package/dist/learn/general.js +73 -0
- package/dist/learn/grade.d.ts +4 -0
- package/dist/learn/grade.js +22 -1
- package/dist/learn/reflect.d.ts +10 -1
- package/dist/learn/reflect.js +14 -4
- package/dist/learn/skills.d.ts +33 -5
- package/dist/learn/skills.js +53 -7
- package/dist/learned-workflow.d.ts +24 -0
- package/dist/learned-workflow.js +66 -3
- package/dist/learning-batch.d.ts +283 -0
- package/dist/learning-batch.js +574 -0
- package/dist/learning-evals.d.ts +33 -0
- package/dist/learning-evals.js +98 -0
- package/dist/learning-routes.d.ts +25 -2
- package/dist/learning-routes.js +39 -6
- package/dist/learning-words.d.ts +6 -0
- package/dist/learning-words.js +6 -0
- package/dist/learning.d.ts +28 -15
- package/dist/learning.js +84 -44
- package/dist/live/stream.js +6 -3
- package/dist/live/translate.d.ts +4 -3
- package/dist/live/translate.js +8 -4
- package/dist/live-reach.d.ts +37 -0
- package/dist/live-reach.js +62 -0
- package/dist/mcp-gateway.d.ts +9 -0
- package/dist/mcp-gateway.js +60 -11
- package/dist/mcp-oauth.d.ts +57 -7
- package/dist/mcp-oauth.js +133 -18
- package/dist/mcp-private-hosts.d.ts +4 -4
- package/dist/mcp-private-hosts.js +20 -12
- package/dist/mcp-probe.d.ts +19 -2
- package/dist/mcp-probe.js +44 -9
- package/dist/mcp-secret-writes.js +4 -0
- package/dist/mcp-servers-store.d.ts +38 -15
- package/dist/mcp-servers-store.js +112 -20
- package/dist/mcp-store.d.ts +10 -4
- package/dist/mcp-store.js +17 -6
- package/dist/model-form.d.ts +26 -0
- package/dist/model-form.js +80 -0
- package/dist/model.js +23 -10
- package/dist/openapi.js +386 -21
- package/dist/page.d.ts +18 -0
- package/dist/page.js +48 -20
- package/dist/paid-calls.d.ts +1 -1
- package/dist/place-offers.d.ts +2 -3
- package/dist/place-offers.js +3 -7
- package/dist/place.js +1 -2
- package/dist/print.d.ts +15 -1
- package/dist/print.js +14 -3
- package/dist/propose.d.ts +3 -0
- package/dist/propose.js +13 -5
- package/dist/provider.js +3 -2
- package/dist/reflect-range.d.ts +8 -1
- package/dist/reflect-range.js +1 -1
- package/dist/run-launch.d.ts +14 -2
- package/dist/run-launch.js +9 -4
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-reads-over-store.d.ts +1 -1
- package/dist/run-repo.d.ts +20 -0
- package/dist/run-repo.js +63 -0
- package/dist/run-timeline-served.d.ts +1 -1
- package/dist/run-view.d.ts +3 -3
- package/dist/run-view.js +14 -7
- package/dist/runner/adapter-loop.d.ts +23 -7
- package/dist/runner/adapter-loop.js +648 -62
- package/dist/runner/adapter-run-spec.d.ts +11 -3
- package/dist/runner/adapter-run-spec.js +77 -22
- package/dist/runner/adapters/pi-computer.d.ts +7 -0
- package/dist/runner/adapters/pi-computer.js +43 -0
- package/dist/runner/adapters/pi.d.ts +9 -4
- package/dist/runner/adapters/pi.js +76 -14
- package/dist/runner/beside.d.ts +7 -3
- package/dist/runner/beside.js +21 -11
- package/dist/runner/claim-secrets.d.ts +4 -0
- package/dist/runner/claim-secrets.js +6 -1
- package/dist/runner/cli-run-spec.d.ts +20 -3
- package/dist/runner/cli-run-spec.js +113 -43
- package/dist/runner/commands.d.ts +3 -1
- package/dist/runner/commands.js +28 -3
- package/dist/runner/computer-channel.d.ts +34 -0
- package/dist/runner/computer-channel.js +61 -0
- package/dist/runner/conformance/harness.js +80 -14
- package/dist/runner/conformance/subject.d.ts +1 -1
- package/dist/runner/dispatch.js +3 -0
- package/dist/runner/end-of-run.d.ts +22 -12
- package/dist/runner/end-of-run.js +38 -16
- package/dist/runner/github-app.d.ts +2 -1
- package/dist/runner/github-app.js +4 -2
- package/dist/runner/harness-loops.js +15 -1
- package/dist/runner/held-run.d.ts +65 -0
- package/dist/runner/held-run.js +158 -0
- package/dist/runner/journal-kinds.d.ts +20 -1
- package/dist/runner/journal-kinds.js +14 -0
- package/dist/runner/journal-reader.d.ts +5 -4
- package/dist/runner/journal-reader.js +12 -10
- package/dist/runner/learning-ask.d.ts +7 -1
- package/dist/runner/learning-ask.js +21 -6
- package/dist/runner/learning-dispatch.d.ts +22 -0
- package/dist/runner/learning-dispatch.js +77 -10
- package/dist/runner/learning-end.d.ts +22 -1
- package/dist/runner/learning-end.js +92 -12
- package/dist/runner/learning-factory.d.ts +1 -1
- package/dist/runner/learning-factory.js +11 -5
- package/dist/runner/learning-loop.d.ts +67 -7
- package/dist/runner/learning-loop.js +173 -21
- package/dist/runner/learning-seed.d.ts +7 -0
- package/dist/runner/learning-seed.js +13 -0
- package/dist/runner/live-ticker.js +1 -0
- package/dist/runner/loop-contract.d.ts +8 -1
- package/dist/runner/loop.d.ts +5 -1
- package/dist/runner/loop.js +2 -1
- package/dist/runner/main.d.ts +13 -1
- package/dist/runner/main.js +27 -5
- package/dist/runner/mcp-claim.d.ts +10 -7
- package/dist/runner/mcp-claim.js +113 -24
- package/dist/runner/mcp-github.js +1 -1
- package/dist/runner/mcp-run.d.ts +5 -5
- package/dist/runner/mcp-run.js +15 -3
- package/dist/runner/metrics.d.ts +11 -2
- package/dist/runner/metrics.js +12 -2
- package/dist/runner/pr-checkout.d.ts +1 -0
- package/dist/runner/pr-checkout.js +1 -1
- package/dist/runner/reflect-hook.d.ts +18 -0
- package/dist/runner/reflect-hook.js +20 -8
- package/dist/runner/runner.d.ts +26 -0
- package/dist/runner/runner.js +184 -15
- package/dist/runner/scratch-sweep.d.ts +13 -0
- package/dist/runner/scratch-sweep.js +62 -0
- package/dist/runner/sessions.d.ts +1 -1
- package/dist/runner/store-port.d.ts +4 -2
- package/dist/runner/store-secrets.js +4 -3
- package/dist/runner/subagent.js +3 -1
- package/dist/runner/workflow-host.d.ts +6 -1
- package/dist/runner/workflow-host.js +1 -1
- package/dist/runner/workflow-loop.d.ts +1 -1
- package/dist/runner/workflow-spec.d.ts +14 -2
- package/dist/runner/workflow-spec.js +56 -20
- package/dist/runs-over-store.d.ts +6 -1
- package/dist/runs-over-store.js +38 -9
- package/dist/seal-key.js +3 -3
- package/dist/sealed-settings-store.d.ts +5 -2
- package/dist/sealed-settings-store.js +10 -4
- package/dist/secret-writes.d.ts +5 -4
- package/dist/secret-writes.js +5 -4
- package/dist/server-secrets-store.d.ts +4 -3
- package/dist/server-secrets-store.js +15 -9
- package/dist/server-secrets.js +3 -2
- package/dist/server.d.ts +43 -14
- package/dist/server.js +472 -126
- package/dist/settings-wake.d.ts +21 -0
- package/dist/settings-wake.js +59 -0
- package/dist/standard-webhooks.d.ts +4 -1
- package/dist/standard-webhooks.js +8 -4
- package/dist/store/command-wake.d.ts +12 -0
- package/dist/store/command-wake.js +37 -0
- package/dist/store/dialect.d.ts +6 -0
- package/dist/store/dialect.js +5 -0
- package/dist/store/index.d.ts +1 -1
- package/dist/store/journal/storage.d.ts +3 -2
- package/dist/store/journal/storage.js +13 -5
- package/dist/store/postgres/dialect.js +4 -0
- package/dist/store/postgres/migrations/0005_learning_rounds.sql +14 -0
- package/dist/store/postgres/migrations/0006_run_changes_channel_attempts.sql +57 -0
- package/dist/store/postgres/migrations/0007_viewer_host.sql +11 -0
- package/dist/store/postgres/migrations/0008_paired_computers.sql +30 -0
- package/dist/store/postgres/migrations/0009_commands_answer.sql +10 -0
- package/dist/store/schema.d.ts +1 -1
- package/dist/store/schema.js +77 -8
- package/dist/store/sqlite/run-patch.d.ts +7 -5
- package/dist/store/sqlite/run-patch.js +28 -13
- package/dist/store/sqlite/store.js +272 -54
- package/dist/store/store.d.ts +268 -9
- package/dist/store/store.js +12 -1
- package/dist/store-reads/cap.d.ts +1 -1
- package/dist/store-reads/cap.js +8 -1
- package/dist/store-reads/decisions.d.ts +2 -2
- package/dist/store-reads/decisions.js +1 -1
- package/dist/store-reads/import-summary.d.ts +1 -1
- package/dist/store-reads/journal.d.ts +5 -4
- package/dist/store-reads/journal.js +73 -15
- package/dist/store-reads/learning.d.ts +20 -4
- package/dist/store-reads/learning.js +74 -18
- package/dist/store-reads/runs.d.ts +1 -1
- package/dist/store-reads/workflow.d.ts +1 -1
- package/dist/store-view.d.ts +5 -0
- package/dist/store-view.js +26 -11
- package/dist/template-eval.js +6 -4
- package/dist/template-github-permissions.d.ts +22 -8
- package/dist/template-github-permissions.js +35 -15
- package/dist/timeline/timeline.d.ts +27 -0
- package/dist/timeline/timeline.js +10 -9
- package/dist/timeline/util.d.ts +4 -0
- package/dist/timeline/util.js +14 -0
- package/dist/tool-budget.d.ts +19 -7
- package/dist/tool-budget.js +14 -8
- package/dist/tool-gate.d.ts +16 -9
- package/dist/tool-gate.js +53 -42
- package/dist/tool-plane.d.ts +16 -12
- package/dist/tool-plane.js +9 -4
- package/dist/tools.d.ts +13 -4
- package/dist/tools.js +13 -4
- package/dist/trigger-adapter.d.ts +4 -0
- package/dist/trigger-secrets-shown.d.ts +2 -2
- package/dist/trigger-secrets-shown.js +1 -1
- package/dist/trigger-signatures.d.ts +25 -6
- package/dist/trigger-signatures.js +41 -7
- package/dist/trigger-targets.d.ts +2 -0
- package/dist/trigger-targets.js +1 -0
- package/dist/trigger-webhook.js +5 -2
- package/dist/triggers.d.ts +7 -3
- package/dist/triggers.js +35 -5
- package/dist/turns.js +16 -1
- package/dist/validate.js +11 -1
- package/dist/web-fetch.d.ts +1 -1
- package/dist/web-search.d.ts +10 -5
- package/dist/web-search.js +7 -6
- package/dist/webhooks.d.ts +145 -102
- package/dist/webhooks.js +559 -322
- package/dist/workflow/decisions.d.ts +1 -1
- package/dist/workflow/node-runner.d.ts +2 -1
- package/dist/workflow/node.d.ts +2 -1
- package/dist/workflow/node.js +3 -1
- package/dist/workflow/pi-versions.d.ts +23 -0
- package/dist/workflow/pi-versions.js +57 -0
- package/dist/workflow/record-tool.d.ts +2 -1
- package/dist/workflow/store.d.ts +1 -1
- package/dist/workflow/task.js +3 -2
- package/dist/workflow-run.d.ts +1 -1
- package/dist/workspace/attached-computers.d.ts +41 -0
- package/dist/workspace/attached-computers.js +114 -0
- package/dist/workspace/computer-deps.d.ts +16 -9
- package/dist/workspace/computer-deps.js +19 -7
- package/dist/workspace/computer-for-claim.d.ts +17 -8
- package/dist/workspace/computer-for-claim.js +62 -21
- package/dist/workspace/computer-hold.d.ts +11 -27
- package/dist/workspace/computer-hold.js +36 -56
- package/dist/workspace/computer-ledger.d.ts +3 -1
- package/dist/workspace/computer-ledger.js +4 -0
- package/dist/workspace/computer-mcp.d.ts +25 -0
- package/dist/workspace/computer-mcp.js +41 -0
- package/dist/workspace/computer-ports.d.ts +4 -13
- package/dist/workspace/computer-ports.js +4 -107
- package/dist/workspace/computer-script.d.ts +37 -0
- package/dist/workspace/computer-script.js +138 -0
- package/dist/workspace/executors.d.ts +1 -1
- package/dist/workspace/index.d.ts +4 -2
- package/dist/workspace/index.js +3 -1
- package/dist/workspace/kind-skills/desktop/SKILL.md +119 -84
- package/dist/workspace/kind-skills/phone/SKILL.md +100 -85
- package/dist/workspace/resolve-computer.d.ts +3 -0
- package/dist/workspace/resolve-computer.js +8 -2
- package/dist/workspace/run-tools.d.ts +32 -15
- package/dist/workspace/run-tools.js +23 -30
- package/package.json +19 -16
- package/templates/market-monitor/agent.md +1 -1
- package/dist/console/assets/index-BI4Byidu.js +0 -550
- package/dist/console/assets/index-C19gUF2n.css +0 -1
- package/dist/run-db.d.ts +0 -53
- package/dist/run-db.js +0 -116
- package/dist/workflow/readers.d.ts +0 -36
- package/dist/workflow/readers.js +0 -145
- package/dist/workflow/wal.d.ts +0 -11
- package/dist/workflow/wal.js +0 -106
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Store } from "./store/index.js";
|
|
2
|
+
import type { ToolSettingsMap, ToolSettingsRead } from "./tool-plane.js";
|
|
3
|
+
/** The store slice a wake reads and writes: the parked runs, their pinned revisions and journals, and the wake itself. */
|
|
4
|
+
export type WakeStore = Pick<Store, "runs" | "agents" | "readJournal">;
|
|
5
|
+
/** Whether a save changed any tool key's permission (added, removed or changed). It does not predict which edit flips a
|
|
6
|
+
* call's answer (an exact `ask` under a wildcard `deny` flips when only the wildcard moves): it only skips a save that
|
|
7
|
+
* touches no permission at all (a provider alone, a pin). The per-run decision is permissionFrom's. Approvals live on the
|
|
8
|
+
* run's revision, not in the server's settings, so a save cannot change them. */
|
|
9
|
+
export declare function permissionsChanged(before: ToolSettingsMap, after: ToolSettingsMap): boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Wakes every parked run with a call waiting on its person whose tool reads allow now on the run's pinned revision: its
|
|
12
|
+
* own tool settings and approvals over the server's `read` (tool-gate.ts permissionFrom, the gate's one rule, a deny set
|
|
13
|
+
* on the server absolute). A run whose waiting calls all still read ask or deny is left parked, never claimed. Answers
|
|
14
|
+
* the runs woken.
|
|
15
|
+
*
|
|
16
|
+
* The run's gate stays the judge: the claim reads the settings again at the call, so a run woken here whose call reads
|
|
17
|
+
* otherwise by then (a pin, a setting changed since) parks again or is refused, in the gate's words. Where the server
|
|
18
|
+
* can't tell the run's answer, nothing is woken: settings that do not read (the gate holds to its last good read), and a
|
|
19
|
+
* shell tool on its default, which is where the run's shell is (no computer: bash asks).
|
|
20
|
+
*/
|
|
21
|
+
export declare function wakeAnsweredBySetting(store: WakeStore, read: ToolSettingsRead, before: ToolSettingsMap): Promise<string[]>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// A change of the server's tool settings (PUT /settings/tools) wakes the runs parked on an ask it answers. A call a person
|
|
2
|
+
// was asked about and has not answered, whose tool now reads allow, is answered by the setting at the run's next claim
|
|
3
|
+
// (runner/adapter-loop.ts allowedBySetting: the ask closes "Allowed by the tool's setting" and the call runs). That claim
|
|
4
|
+
// came only with a person's answer or another command; here the flip itself brings it, by the store's wake (runs.wake),
|
|
5
|
+
// so the run goes on at once. No second resume path: the claim is any claim, and its gate decides as it always does.
|
|
6
|
+
import { BACKGROUND_CONTEXT } from "@earendil-works/chord/context";
|
|
7
|
+
import { approvalsOf } from "./approvals.js";
|
|
8
|
+
import { answersOf, asksOf } from "./run-view.js";
|
|
9
|
+
import { journalEntries } from "./runner/journal-reader.js";
|
|
10
|
+
import { permissionFrom, SHELL_FAMILY } from "./tool-gate.js";
|
|
11
|
+
/** Whether a save changed any tool key's permission (added, removed or changed). It does not predict which edit flips a
|
|
12
|
+
* call's answer (an exact `ask` under a wildcard `deny` flips when only the wildcard moves): it only skips a save that
|
|
13
|
+
* touches no permission at all (a provider alone, a pin). The per-run decision is permissionFrom's. Approvals live on the
|
|
14
|
+
* run's revision, not in the server's settings, so a save cannot change them. */
|
|
15
|
+
export function permissionsChanged(before, after) {
|
|
16
|
+
for (const key of new Set([...Object.keys(before), ...Object.keys(after)])) {
|
|
17
|
+
if (before[key]?.permission !== after[key]?.permission)
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Wakes every parked run with a call waiting on its person whose tool reads allow now on the run's pinned revision: its
|
|
24
|
+
* own tool settings and approvals over the server's `read` (tool-gate.ts permissionFrom, the gate's one rule, a deny set
|
|
25
|
+
* on the server absolute). A run whose waiting calls all still read ask or deny is left parked, never claimed. Answers
|
|
26
|
+
* the runs woken.
|
|
27
|
+
*
|
|
28
|
+
* The run's gate stays the judge: the claim reads the settings again at the call, so a run woken here whose call reads
|
|
29
|
+
* otherwise by then (a pin, a setting changed since) parks again or is refused, in the gate's words. Where the server
|
|
30
|
+
* can't tell the run's answer, nothing is woken: settings that do not read (the gate holds to its last good read), and a
|
|
31
|
+
* shell tool on its default, which is where the run's shell is (no computer: bash asks).
|
|
32
|
+
*/
|
|
33
|
+
export async function wakeAnsweredBySetting(store, read, before) {
|
|
34
|
+
if (read.unreadable)
|
|
35
|
+
return [];
|
|
36
|
+
// A save that changed no permission (a provider alone, a pin) answers no ask: no run is listed, no journal read.
|
|
37
|
+
if (!permissionsChanged(before, read.settings))
|
|
38
|
+
return [];
|
|
39
|
+
const server = read.settings;
|
|
40
|
+
const woken = [];
|
|
41
|
+
for (const run of await store.runs.list({ state: "parked" })) {
|
|
42
|
+
const entries = await journalEntries(store.readJournal(run.id), BACKGROUND_CONTEXT);
|
|
43
|
+
const answers = answersOf(entries);
|
|
44
|
+
// The ask names the call by its settings key (a shell tool by its own name), as the gate's askedAs gives it.
|
|
45
|
+
const waiting = [...asksOf(entries).values()].filter((ask) => !ask.resolved && !(ask.id in answers)).map((ask) => ask.tool);
|
|
46
|
+
if (waiting.length === 0)
|
|
47
|
+
continue;
|
|
48
|
+
const revision = await store.agents.revision(run.revisionId);
|
|
49
|
+
const body = (revision?.body ?? {});
|
|
50
|
+
const agent = (body.toolSettings ?? {});
|
|
51
|
+
const allowed = waiting.some((tool) => {
|
|
52
|
+
const { permission, from } = permissionFrom(tool, server, agent, approvalsOf(body.approvals));
|
|
53
|
+
return permission === "allow" && !(from === "default" && Object.hasOwn(SHELL_FAMILY, tool));
|
|
54
|
+
});
|
|
55
|
+
if (allowed && (await store.runs.wake(run.id)))
|
|
56
|
+
woken.push(run.id);
|
|
57
|
+
}
|
|
58
|
+
return woken;
|
|
59
|
+
}
|
|
@@ -27,4 +27,7 @@ export declare const signatureOf: (secret: string, id: string, timestamp: string
|
|
|
27
27
|
export declare function headersRefusal(headers: Record<string, string | string[] | undefined>, nowMs: number): string | undefined;
|
|
28
28
|
/** Why a request is not signed by `secret`, or undefined when it is: `webhook-signature` lists signatures, space
|
|
29
29
|
* separated, and any one that matches passes; the timestamp is within HOOK_TOLERANCE_S; the key has MIN_KEY_BYTES. */
|
|
30
|
-
export declare
|
|
30
|
+
export declare const verify: (secret: string, headers: Record<string, string | string[] | undefined>, body: Buffer, nowMs: number) => string | undefined;
|
|
31
|
+
/** `verify` under key bytes the caller derived from its secret: Standard Webhooks' are the secret's decoded `whsec_`
|
|
32
|
+
* key; a sender that signs the same message under the secret string's UTF-8 bytes (Composio's) passes those. */
|
|
33
|
+
export declare function verifyWithKey(key: Buffer, headers: Record<string, string | string[] | undefined>, body: Buffer, nowMs: number): string | undefined;
|
|
@@ -22,7 +22,8 @@ export const mintSecret = () => `${SECRET_PREFIX}${randomBytes(32).toString("bas
|
|
|
22
22
|
const keyOf = (secret) => Buffer.from(secret.startsWith(SECRET_PREFIX) ? secret.slice(SECRET_PREFIX.length) : secret, "base64");
|
|
23
23
|
/** How many bytes `value` holds as a key: a `whsec_` secret's decoded key, any other value's UTF-8. */
|
|
24
24
|
export const keyBytes = (value) => (value.startsWith(SECRET_PREFIX) ? keyOf(value).length : Buffer.byteLength(value, "utf8"));
|
|
25
|
-
const
|
|
25
|
+
const hmacWith = (key, id, timestamp, body) => createHmac("sha256", key).update(`${id}.${timestamp}.`).update(body).digest();
|
|
26
|
+
const hmacOf = (secret, id, timestamp, body) => hmacWith(keyOf(secret), id, timestamp, body);
|
|
26
27
|
/** Each spelling of `value` a cut needs beyond the ones redaction derives of any text (learn/secrets.ts redactSecrets:
|
|
27
28
|
* as written, its hex, its base64): for a `whsec_` secret, itself, its key as written after the prefix, that key as
|
|
28
29
|
* base64url (- and _ for + and /, no padding), and the key's bytes as hex, in either case; each padded one printed
|
|
@@ -54,19 +55,22 @@ export function headersRefusal(headers, nowMs) {
|
|
|
54
55
|
}
|
|
55
56
|
/** Why a request is not signed by `secret`, or undefined when it is: `webhook-signature` lists signatures, space
|
|
56
57
|
* separated, and any one that matches passes; the timestamp is within HOOK_TOLERANCE_S; the key has MIN_KEY_BYTES. */
|
|
57
|
-
export
|
|
58
|
+
export const verify = (secret, headers, body, nowMs) => verifyWithKey(keyOf(secret), headers, body, nowMs);
|
|
59
|
+
/** `verify` under key bytes the caller derived from its secret: Standard Webhooks' are the secret's decoded `whsec_`
|
|
60
|
+
* key; a sender that signs the same message under the secret string's UTF-8 bytes (Composio's) passes those. */
|
|
61
|
+
export function verifyWithKey(key, headers, body, nowMs) {
|
|
58
62
|
const early = headersRefusal(headers, nowMs);
|
|
59
63
|
if (early)
|
|
60
64
|
return early;
|
|
61
65
|
const [id, timestamp, signatures] = ["webhook-id", "webhook-timestamp", "webhook-signature"].map((name) => headers[name]);
|
|
62
|
-
const expected =
|
|
66
|
+
const expected = hmacWith(key, id, timestamp, body);
|
|
63
67
|
// Compared all the same, so a short key costs what a good one does; it fails after.
|
|
64
68
|
const matched = signatures.split(" ").some((part) => {
|
|
65
69
|
const [version, signature] = part.split(",");
|
|
66
70
|
const given = Buffer.from(signature ?? "", "base64");
|
|
67
71
|
return version === "v1" && given.length === expected.length && timingSafeEqual(given, expected);
|
|
68
72
|
});
|
|
69
|
-
if (
|
|
73
|
+
if (key.length < MIN_KEY_BYTES)
|
|
70
74
|
return "short_key";
|
|
71
75
|
return matched ? undefined : "bad_signature";
|
|
72
76
|
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** What woke a listener: a command row was written for the run, or one was taken, refused or answered. */
|
|
2
|
+
export interface CommandWake {
|
|
3
|
+
readonly runId: string;
|
|
4
|
+
readonly what: "written" | "answered";
|
|
5
|
+
}
|
|
6
|
+
type Listener = (event: CommandWake) => void;
|
|
7
|
+
/** Subscribes `listener` to the commands of the data at `where`; answers the unsubscribe. */
|
|
8
|
+
export declare function watchCommandsAt(where: string, listener: Listener): () => void;
|
|
9
|
+
/** Tells the listeners of `where` about `events`, after the write committed: asynchronously, each listener's throw
|
|
10
|
+
* swallowed, so a write never waits on or fails with a listener. */
|
|
11
|
+
export declare function wakeCommands(where: string, ...events: readonly CommandWake[]): void;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// A wake for command rows: a hint to read now, not a source of truth. An in-process bus keyed by a store's `where` (two
|
|
2
|
+
// handles on the same data share one), so a write through one handle wakes the readers of the others without waiting for
|
|
3
|
+
// their poll. It reaches handles in this process only: a row another process writes wakes nothing here, and the reader's
|
|
4
|
+
// poll finds it.
|
|
5
|
+
const listeners = new Map();
|
|
6
|
+
/** Subscribes `listener` to the commands of the data at `where`; answers the unsubscribe. */
|
|
7
|
+
export function watchCommandsAt(where, listener) {
|
|
8
|
+
let set = listeners.get(where);
|
|
9
|
+
if (set === undefined)
|
|
10
|
+
listeners.set(where, (set = new Set()));
|
|
11
|
+
const mine = set;
|
|
12
|
+
mine.add(listener);
|
|
13
|
+
return () => {
|
|
14
|
+
mine.delete(listener);
|
|
15
|
+
if (mine.size === 0 && listeners.get(where) === mine)
|
|
16
|
+
listeners.delete(where);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/** Tells the listeners of `where` about `events`, after the write committed: asynchronously, each listener's throw
|
|
20
|
+
* swallowed, so a write never waits on or fails with a listener. */
|
|
21
|
+
export function wakeCommands(where, ...events) {
|
|
22
|
+
const set = listeners.get(where);
|
|
23
|
+
if (set === undefined || events.length === 0)
|
|
24
|
+
return;
|
|
25
|
+
queueMicrotask(() => {
|
|
26
|
+
for (const listener of [...set]) {
|
|
27
|
+
for (const event of events) {
|
|
28
|
+
try {
|
|
29
|
+
listener(event);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// A listener's failure is its own.
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
package/dist/store/dialect.d.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface Dialect {
|
|
|
12
12
|
jsonArrayHas(column: string): string;
|
|
13
13
|
/** The length of the JSON array in TEXT column `column`. */
|
|
14
14
|
jsonArrayLength(column: string): string;
|
|
15
|
+
/** The JSON object in TEXT column `column` without its top-level `key`. */
|
|
16
|
+
jsonWithout(column: string, key: string): string;
|
|
15
17
|
/** trigger_firings' insertion order: the tie-break of rows written in the same ms. */
|
|
16
18
|
readonly firingOrder: string;
|
|
17
19
|
/** No row limit, before an OFFSET. */
|
|
@@ -57,3 +59,7 @@ export interface Dialect {
|
|
|
57
59
|
export declare function cleanText(text: string): string;
|
|
58
60
|
/** SQLite: one writer at a time (BEGIN IMMEDIATE), so every lock is already held. */
|
|
59
61
|
export declare const SQLITE: Dialect;
|
|
62
|
+
/** The SET fragment every statement that settles command rows (taken or refused) carries: a computer call's params go
|
|
63
|
+
* with its settling, so text an owner typed or wrote through the computer call channel (a password, a credential's file)
|
|
64
|
+
* never stays in the store once the call is done. The row keeps its method. */
|
|
65
|
+
export declare const settledBody: (dialect: Dialect) => string;
|
package/dist/store/dialect.js
CHANGED
|
@@ -16,6 +16,7 @@ export const SQLITE = {
|
|
|
16
16
|
jsonNotFalse: (column, path) => `json_extract(${column}, ${jsonPath(path)}) IS NOT 0`,
|
|
17
17
|
jsonArrayHas: (column) => `EXISTS (SELECT 1 FROM json_each(${column}) WHERE value = ?)`,
|
|
18
18
|
jsonArrayLength: (column) => `json_array_length(${column})`,
|
|
19
|
+
jsonWithout: (column, key) => `json_remove(${column}, ${jsonPath([key])})`,
|
|
19
20
|
firingOrder: "rowid",
|
|
20
21
|
limitAll: "LIMIT -1",
|
|
21
22
|
nullSafeEquals: (column) => `${column} IS ?`,
|
|
@@ -30,3 +31,7 @@ export const SQLITE = {
|
|
|
30
31
|
lockRow: async () => undefined,
|
|
31
32
|
lockKey: async () => undefined,
|
|
32
33
|
};
|
|
34
|
+
/** The SET fragment every statement that settles command rows (taken or refused) carries: a computer call's params go
|
|
35
|
+
* with its settling, so text an owner typed or wrote through the computer call channel (a password, a credential's file)
|
|
36
|
+
* never stays in the store once the call is done. The row keeps its method. */
|
|
37
|
+
export const settledBody = (dialect) => `body = CASE WHEN kind = 'computer_call' THEN ${dialect.jsonWithout("body", "params")} ELSE body END`;
|
package/dist/store/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { AgentArchived, AnswerTaken, CommandTaken, endedReason, ExternalBodyConflict, FINAL_STATES, Fenced, MCP_REFRESH_CALL_MS, MCP_REFRESH_LEASE_MS, RevisionMoved, RunFinal, SchemaVersionUnknown, SignInHandedTo, SignInMissing, StoreBusy, StoreClosed, StoreConstraint, StoreDeadline, StoreFromEarlierVersion, StoreLaunchMissing, StoreOwned, StoreWorkerLost, STORE_WRITE_DEADLINE_MS, stoppedByPerson, TriggerFull } from "./store.js";
|
|
2
|
-
export type { ConstraintRule, AgentRow, Agents, AlsoRow, ChannelPost, Claim, Command, ComputerLedger, ComputerLedgerRow, Firing, FiringChange, FiringRow, FiringStatus, FiringStreak, Journal, Live, LiveRow, Mcp, McpCredentialMark, McpOauth, McpOauthPendingRow, McpPinRow, McpServerRow, McpServerWrite, NewFiring, NewRevision, NewTrigger, ReleasedState, RevisionRow, RunPatch, RunRow, Runs, RunState, SealedKeyRow, SealedKeys, SealedPlane, SealedSecretRow, SealedSecrets, SettingRow, Settings, Staged, StagedTake, Store, StoreRole, TemplatePackage, Templates, TriggerDef, TriggerPatch, TriggerRow, Triggers, TriggerScope, TriggerSettingRow, TriggerSettings, ImportedFrom, ImportSource, ImportRunInput, JournalReads, RunOrigin, RunOutcome, RunLaunchInput, RunLaunchRow, Learning, LearningProposal, LiveViewerRow, LiveViewers, SignIns, SignInRow, SignInTombstoneRow, UpdatedRun, ViewerKind, } from "./store.js";
|
|
2
|
+
export type { ConstraintRule, AgentRow, Agents, AlsoRow, ChannelPost, Claim, Command, ComputerLedger, ComputerLedgerRow, Firing, FiringChange, FiringRow, FiringStatus, FiringStreak, Journal, Live, LiveRow, Mcp, McpCredentialMark, McpRemoved, McpOauth, McpOauthPendingRow, McpPinRow, McpServerRow, McpServerStamp, McpServerWrite, NewFiring, NewPairedComputer, NewRevision, NewTrigger, PairedComputerRow, PairedComputers, ReleasedState, RevisionRow, RunPatch, RunRow, Runs, RunState, SealedKeyRow, SealedKeys, SealedPlane, SealedSecretRow, SealedSecrets, SettingRow, Settings, Staged, StagedTake, Store, StoreRole, TemplatePackage, Templates, TriggerDef, TriggerPatch, TriggerRow, Triggers, TriggerScope, TriggerSettingRow, TriggerSettings, Channels, ChannelAttemptRow, ChannelPostRow, ChannelToldRow, ImportedFrom, ImportSource, ImportRunInput, JournalReads, RunOrigin, RunOutcome, RunLaunchInput, RunLaunchRow, Learning, LearningProposal, LiveViewerRow, LiveViewers, SignIns, SignInRow, SignInTombstoneRow, UpdatedRun, ViewerKind, } from "./store.js";
|
|
3
3
|
export { aadOf, type McpCredential, type OpenedMcpCredential, openMcpCredential, openRunMcpAuth, type RunMcpAuth, sealColumn, type SealedAt, sealedAt, sealMcpCredential, sealRunMcpAuth, unsealColumn } from "./seal.js";
|
|
4
4
|
export { AgentBodyInvalid, type AgenticBody, bodyOf, canonical, isObject, isSkillPath, type PromptVariant, type ModelTiers, type RevisionBodyInput, type RevisionComputer, type RevisionPlace, sha256, type ToolSettings, type WorkflowBody, } from "./agent-body.js";
|
|
5
5
|
export { checkPrompts, OTHER_OPTION, PromptsInvalid } from "./prompts.js";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Context
|
|
1
|
+
import type { Context } from "@earendil-works/chord/context";
|
|
2
|
+
import type { JsonValue } from "@earendil-works/chord/json";
|
|
2
3
|
import { type CommitResult, type ConversationId, type ConversationQuery, type ConversationRecord, type Cursor, type DocumentAddress, type DocumentId, type DocumentPoint, type DocumentQuery, type DocumentRecord, type EntryId, type EntryQuery, type EntryRecord, type Id, type Page, type Seq, type StorageWrite, type StoredDocument, type SubmissionId, type SubmissionQuery, type SubmissionRecord, type TaskId, type TaskQuery, type TaskRecord } from "@earendil-works/pi-durable";
|
|
3
4
|
import { type Dialect } from "../dialect.js";
|
|
4
5
|
import type { StoreConnection } from "../sqlite/connection.js";
|
|
@@ -51,7 +52,7 @@ export declare class RunStorage implements Journal {
|
|
|
51
52
|
#private;
|
|
52
53
|
/** The journal of `claim`'s run on `db` (the store's connection, its schema in place). Its id namespace is read, and made
|
|
53
54
|
* on first use, by its first commit or mintId. `now`: the store's clock (epoch ms). */
|
|
54
|
-
constructor(db: JournalConnection, claim: JournalClaim, now?: () => number, onClose?: () => void);
|
|
55
|
+
constructor(db: JournalConnection, claim: JournalClaim, now?: () => number, onClose?: () => void, where?: string);
|
|
55
56
|
/** The journal of `claim`'s run on `db`, its id namespace read (and made on first use). */
|
|
56
57
|
static open(db: JournalConnection, claim: JournalClaim, now?: () => number): Promise<RunStorage>;
|
|
57
58
|
/** Whether the journal is closed (store.journal hands out a new one then). */
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { apply } from "@earendil-works/chord/delta";
|
|
2
2
|
import { StorageRequestError, } from "@earendil-works/pi-durable";
|
|
3
|
+
import { wakeCommands } from "../command-wake.js";
|
|
3
4
|
import { PI_TABLE as T } from "../schema.js";
|
|
4
|
-
import { SQLITE } from "../dialect.js";
|
|
5
|
+
import { settledBody, SQLITE } from "../dialect.js";
|
|
5
6
|
import { claimFence, storeNow } from "../fence.js";
|
|
6
7
|
import { applyRunPatch, refuseLeftovers } from "../sqlite/run-patch.js";
|
|
7
8
|
import { CommandTaken, Fenced } from "../store.js";
|
|
@@ -130,6 +131,8 @@ export class RunStorage {
|
|
|
130
131
|
#staged;
|
|
131
132
|
/** Commands staged to be taken, each with the record of its effect: taken only by the commit that stores that record. */
|
|
132
133
|
#takes = new Map();
|
|
134
|
+
/** The store's identity (Store.where): where a commit that answers a command wakes the readers in this process. */
|
|
135
|
+
#where;
|
|
133
136
|
/** Ids this journal minted and has not yet written: the only ones a staged take may name as its effect. */
|
|
134
137
|
#minted = new Set();
|
|
135
138
|
#closed = false;
|
|
@@ -140,8 +143,9 @@ export class RunStorage {
|
|
|
140
143
|
#readsDrained;
|
|
141
144
|
/** The journal of `claim`'s run on `db` (the store's connection, its schema in place). Its id namespace is read, and made
|
|
142
145
|
* on first use, by its first commit or mintId. `now`: the store's clock (epoch ms). */
|
|
143
|
-
constructor(db, claim, now = Date.now, onClose) {
|
|
146
|
+
constructor(db, claim, now = Date.now, onClose, where) {
|
|
144
147
|
this.#db = db;
|
|
148
|
+
this.#where = where;
|
|
145
149
|
this.#dialect = db.dialect ?? SQLITE;
|
|
146
150
|
this.#read = readsOf(db);
|
|
147
151
|
this.#runId = claim.runId;
|
|
@@ -212,7 +216,7 @@ export class RunStorage {
|
|
|
212
216
|
}
|
|
213
217
|
}
|
|
214
218
|
for (const take of staged.take ?? [])
|
|
215
|
-
this.#takes.set(take.command, Number(take.effect));
|
|
219
|
+
this.#takes.set(take.command, { effect: Number(take.effect), answer: take.answer === undefined ? null : JSON.stringify(take.answer) });
|
|
216
220
|
// A stage that names a run patch or rows replaces the ones staged before; one that only takes leaves them.
|
|
217
221
|
if (staged.run !== undefined || staged.also !== undefined)
|
|
218
222
|
this.#staged = { ...(staged.run !== undefined ? { run: staged.run } : {}), ...(staged.also !== undefined ? { also: staged.also } : {}), effect: Number(staged.effect) };
|
|
@@ -295,7 +299,7 @@ export class RunStorage {
|
|
|
295
299
|
seqs.push(seq);
|
|
296
300
|
candidateNextId = Math.max(candidateNextId, this.#candidateNextId(writes));
|
|
297
301
|
// A take whose effect this stored batch newly writes is this commit's, and so is the staged patch.
|
|
298
|
-
for (const [command, effect] of this.#takes)
|
|
302
|
+
for (const [command, { effect }] of this.#takes)
|
|
299
303
|
if (fresh.has(effect) && !taking.includes(command))
|
|
300
304
|
taking.push(command);
|
|
301
305
|
if (staged?.effect !== undefined && fresh.has(staged.effect))
|
|
@@ -310,7 +314,7 @@ export class RunStorage {
|
|
|
310
314
|
await applyRunPatch(tx, { runId: this.#runId, epoch: this.#epoch }, staged?.run ?? {}, staged?.also ?? [], now, this.#dialect);
|
|
311
315
|
}
|
|
312
316
|
for (const commandId of taking) {
|
|
313
|
-
if ((await tx.runCounted(
|
|
317
|
+
if ((await tx.runCounted(`UPDATE commands SET taken_at = ?, answer = ?, ${settledBody(this.#dialect)} WHERE id = ? AND run_id = ? AND taken_at IS NULL`, now, this.#takes.get(commandId)?.answer ?? null, commandId, this.#runId)) === 0) {
|
|
314
318
|
throw new CommandTaken(this.#runId, commandId);
|
|
315
319
|
}
|
|
316
320
|
}
|
|
@@ -337,6 +341,10 @@ export class RunStorage {
|
|
|
337
341
|
// Taken with their effects, now durable: no longer waiting.
|
|
338
342
|
for (const command of taking)
|
|
339
343
|
this.#takes.delete(command);
|
|
344
|
+
// A command taken, or a run gone final (its leftover rows refused): the readers of those answers in this process wake.
|
|
345
|
+
if (this.#where !== undefined && (taking.length > 0 || (patching && ["ended", "stopped", "failed"].includes(staged?.run?.state ?? "")))) {
|
|
346
|
+
wakeCommands(this.#where, { runId: this.#runId, what: "answered" });
|
|
347
|
+
}
|
|
340
348
|
// COMMIT returned: the stored batches are durable (to the store's synchronous level; flush makes them so on disk).
|
|
341
349
|
for (const [index, seq] of seqs.entries())
|
|
342
350
|
receive(index, seq);
|
|
@@ -13,6 +13,10 @@ export function postgresDialect(schema, { testClock = false } = {}) {
|
|
|
13
13
|
jsonNotFalse: (column, keys) => `COALESCE((${column}::json #> ${path(keys)})::text NOT IN ('false', '0'), TRUE)`,
|
|
14
14
|
jsonArrayHas: (column) => `EXISTS (SELECT 1 FROM json_array_elements_text(${column}::json) AS element(value) WHERE element.value = ?)`,
|
|
15
15
|
jsonArrayLength: (column) => `json_array_length(${column}::json)`,
|
|
16
|
+
// Through jsonb, the one place: a body that loses a key is never read for its key order. Every Postgres JSON function
|
|
17
|
+
// decodes the whole document's strings, so a body holding a string jsonb refuses (an escaped NUL, a lone surrogate)
|
|
18
|
+
// fails it: a computer call's params are written as one JSON text (computer-channel.ts), whose escapes jsonb keeps.
|
|
19
|
+
jsonWithout: (column, key) => `(${column}::jsonb - '${key.replaceAll("'", "''")}')::text`,
|
|
16
20
|
firingOrder: "ord",
|
|
17
21
|
limitAll: "LIMIT ALL",
|
|
18
22
|
nullSafeEquals: (column) => `${column} IS NOT DISTINCT FROM ?`,
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
-- The store's schema on Postgres, version 5: a run's learning is kept by round, as store/schema.ts keeps it on SQLite
|
|
2
|
+
-- (version 7). A run's first end is round 1; each end after a follow-up opened the run again looks back once more, a
|
|
3
|
+
-- round of its own, beside the rounds before it. Applied once, after 0004, inside the transaction migrate.ts opens. Never
|
|
4
|
+
-- edit this file once applied: a change is a new file.
|
|
5
|
+
--
|
|
6
|
+
-- learning_proposals_pkey is Postgres's name for 0001's primary key on run_id.
|
|
7
|
+
-- Safe to apply again (a store whose later migrations are unrecorded re-applies them from the first one it cannot vouch for): every statement here is idempotent.
|
|
8
|
+
|
|
9
|
+
ALTER TABLE learning_proposals ADD COLUMN IF NOT EXISTS round BIGINT NOT NULL DEFAULT 1 CHECK (round >= 1);
|
|
10
|
+
ALTER TABLE learning_proposals ALTER COLUMN round DROP DEFAULT;
|
|
11
|
+
ALTER TABLE learning_proposals DROP CONSTRAINT IF EXISTS learning_proposals_pkey;
|
|
12
|
+
ALTER TABLE learning_proposals ADD PRIMARY KEY (run_id, round);
|
|
13
|
+
|
|
14
|
+
UPDATE store_meta SET schema_version = 5 WHERE singleton = 1;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
-- The store's schema on Postgres, version 6: the run-change feed (Runs.changedSince), each channel post's tries (the
|
|
2
|
+
-- outbound webhook's deliveries log) and the claim a post's try settles under, as SQLite's schema version 8 has them.
|
|
3
|
+
--
|
|
4
|
+
-- A change is a run reaching a final state, or let go by its runner (parked, or running with none), written by the
|
|
5
|
+
-- trigger below in the transaction of the write that made it. Postgres has many writers, so a sequence alone does not
|
|
6
|
+
-- number changes in commit order: the trigger takes one transaction-scoped advisory lock before it inserts, so two
|
|
7
|
+
-- transactions that each write a change commit them in seq order, and a reader that has read seq n has every change at
|
|
8
|
+
-- or below it. The trigger is deferred to the commit, so the lock is held only while the change is numbered and the
|
|
9
|
+
-- transaction commits: a run's end write and the statements after it never wait on another run's, which would make a
|
|
10
|
+
-- runner of many loops end its runs one at a time.
|
|
11
|
+
CREATE TABLE IF NOT EXISTS run_changes (
|
|
12
|
+
seq BIGSERIAL PRIMARY KEY,
|
|
13
|
+
run_id TEXT COLLATE "C" NOT NULL,
|
|
14
|
+
state TEXT COLLATE "C" NOT NULL,
|
|
15
|
+
ended_at BIGINT,
|
|
16
|
+
epoch BIGINT NOT NULL,
|
|
17
|
+
runner TEXT COLLATE "C"
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
CREATE OR REPLACE FUNCTION run_changed() RETURNS trigger LANGUAGE plpgsql AS $$
|
|
21
|
+
BEGIN
|
|
22
|
+
PERFORM pg_advisory_xact_lock(hashtext(TG_TABLE_SCHEMA || '.run_changes'));
|
|
23
|
+
EXECUTE format('INSERT INTO %I.run_changes (run_id, state, ended_at, epoch, runner) VALUES ($1, $2, $3, $4, $5)', TG_TABLE_SCHEMA)
|
|
24
|
+
USING NEW.id, NEW.state, NEW.ended_at, NEW.epoch, NEW.runner;
|
|
25
|
+
RETURN NULL;
|
|
26
|
+
END
|
|
27
|
+
$$;
|
|
28
|
+
|
|
29
|
+
DROP TRIGGER IF EXISTS runs_changed ON runs;
|
|
30
|
+
CREATE CONSTRAINT TRIGGER runs_changed AFTER UPDATE OF state, runner ON runs
|
|
31
|
+
DEFERRABLE INITIALLY DEFERRED
|
|
32
|
+
FOR EACH ROW
|
|
33
|
+
WHEN ((OLD.state IS DISTINCT FROM NEW.state OR OLD.runner IS DISTINCT FROM NEW.runner)
|
|
34
|
+
AND (NEW.state IN ('ended', 'stopped', 'failed', 'parked') OR (NEW.state = 'running' AND NEW.runner IS NULL)))
|
|
35
|
+
EXECUTE FUNCTION run_changed();
|
|
36
|
+
|
|
37
|
+
-- A post's try settles under the claim that made it (Channels.settle's fence): the claim's token, kept on the post until
|
|
38
|
+
-- a newer claim or a write outside every claim takes it.
|
|
39
|
+
ALTER TABLE channel_posts ADD COLUMN IF NOT EXISTS claim TEXT COLLATE "C";
|
|
40
|
+
|
|
41
|
+
-- Each try once, by its id: a settle written again (its outcome unknown) logs it once.
|
|
42
|
+
CREATE TABLE IF NOT EXISTS channel_attempts (
|
|
43
|
+
seq BIGSERIAL PRIMARY KEY,
|
|
44
|
+
try_id TEXT COLLATE "C" NOT NULL UNIQUE,
|
|
45
|
+
channel TEXT COLLATE "C" NOT NULL,
|
|
46
|
+
post_id TEXT COLLATE "C" NOT NULL,
|
|
47
|
+
moment TEXT COLLATE "C" NOT NULL,
|
|
48
|
+
at BIGINT NOT NULL,
|
|
49
|
+
status_code BIGINT,
|
|
50
|
+
latency_ms BIGINT NOT NULL,
|
|
51
|
+
error TEXT COLLATE "C",
|
|
52
|
+
retry_at BIGINT,
|
|
53
|
+
redelivery BIGINT NOT NULL CHECK (redelivery IN (0, 1))
|
|
54
|
+
);
|
|
55
|
+
CREATE INDEX IF NOT EXISTS channel_attempts_by_channel ON channel_attempts (channel, seq);
|
|
56
|
+
|
|
57
|
+
UPDATE store_meta SET schema_version = 6 WHERE singleton = 1;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
-- The store's schema on Postgres, version 7: where a live viewer answers when its runner is on another host than the
|
|
2
|
+
-- server (live-reach.ts), as store/schema.ts keeps it on SQLite (version 9). Applied once, after 0006, inside the transaction
|
|
3
|
+
-- migrate.ts opens. Never edit this file once applied: a change is a new file.
|
|
4
|
+
--
|
|
5
|
+
-- computer_viewers.host: the runner's live host (AGENTRUN_RUNNER_LIVE_HOST); null, the server's own machine (loopback).
|
|
6
|
+
-- IF NOT EXISTS: applied again over a store that has it (the migration-0003 repair re-runs every migration after 0002),
|
|
7
|
+
-- it changes nothing.
|
|
8
|
+
|
|
9
|
+
ALTER TABLE computer_viewers ADD COLUMN IF NOT EXISTS host TEXT COLLATE "C";
|
|
10
|
+
|
|
11
|
+
UPDATE store_meta SET schema_version = 7 WHERE singleton = 1;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
-- The store's schema on Postgres, version 8: a person's own computer paired with this server (spec/computer/attach.md),
|
|
2
|
+
-- and the codes that pair one, as store/schema.ts keeps them on SQLite (version 10). Applied once, after 0007, inside the transaction
|
|
3
|
+
-- migrate.ts opens. Never edit this file once applied: a change is a new file.
|
|
4
|
+
--
|
|
5
|
+
-- paired_computers: the token's SHA-256 alone is kept (hex); revoked_at marks a removed computer, whose token dials no more.
|
|
6
|
+
-- pairing_codes: a code's SHA-256 alone (hex), good once until expires_at; a used one is deleted as it pairs. Its id names
|
|
7
|
+
-- the pairing and is kept on the computer it made (paired_computers.pairing_id), so the console that made the code can
|
|
8
|
+
-- tell its own code was used, and by which computer.
|
|
9
|
+
-- Each table is made only where it is missing, so a store whose record of this file was lost applies it again unharmed.
|
|
10
|
+
|
|
11
|
+
CREATE TABLE IF NOT EXISTS paired_computers (
|
|
12
|
+
id TEXT COLLATE "C" PRIMARY KEY,
|
|
13
|
+
name TEXT COLLATE "C" NOT NULL,
|
|
14
|
+
hello TEXT COLLATE "C" NOT NULL CHECK (hello::json IS NOT NULL),
|
|
15
|
+
driver TEXT COLLATE "C" NOT NULL,
|
|
16
|
+
labels TEXT COLLATE "C" NOT NULL CHECK (labels::json IS NOT NULL),
|
|
17
|
+
token_sha256 TEXT COLLATE "C" NOT NULL UNIQUE,
|
|
18
|
+
pairing_id TEXT COLLATE "C" NOT NULL UNIQUE,
|
|
19
|
+
created_at BIGINT NOT NULL,
|
|
20
|
+
seen_at BIGINT,
|
|
21
|
+
revoked_at BIGINT
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
CREATE TABLE IF NOT EXISTS pairing_codes (
|
|
25
|
+
id TEXT COLLATE "C" NOT NULL UNIQUE,
|
|
26
|
+
code_sha256 TEXT COLLATE "C" PRIMARY KEY,
|
|
27
|
+
expires_at BIGINT NOT NULL
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
UPDATE store_meta SET schema_version = 8 WHERE singleton = 1;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
-- The store's schema on Postgres, version 9: a command keeps its answer, as store/schema.ts keeps it on SQLite
|
|
2
|
+
-- (version 11, commands.answer). The runner writes it with the commit that takes the row; the server reads it once and clears it
|
|
3
|
+
-- (Store.answerOf). Applied once, after 0008, inside the transaction migrate.ts opens. Never edit this file once applied: a
|
|
4
|
+
-- change is a new file.
|
|
5
|
+
--
|
|
6
|
+
-- answer: JSON text, NULL until a take writes one and again once it is read.
|
|
7
|
+
|
|
8
|
+
ALTER TABLE commands ADD COLUMN answer TEXT COLLATE "C" CHECK (answer IS NULL OR answer::json IS NOT NULL);
|
|
9
|
+
|
|
10
|
+
UPDATE store_meta SET schema_version = 9 WHERE singleton = 1;
|
package/dist/store/schema.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const SCHEMA_VERSION =
|
|
1
|
+
export declare const SCHEMA_VERSION = 11;
|
|
2
2
|
/** One answer per approval of a run: a second approve or deny command naming the same approval is refused
|
|
3
3
|
* (StoreConstraint), so the first answer a person gives wins from the moment it is written, before the runner takes it
|
|
4
4
|
* (runs-over-store.ts approve). In STORE_DDL, and added at a server open to a store an earlier build of this version made. */
|
package/dist/store/schema.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
// The store's one schema, version
|
|
1
|
+
// The store's one schema, version 11 (design 2.1). Nothing migrates into it: the rewrite replaces the old stores, and a store
|
|
2
2
|
// an older build made is refused as a version this build does not know. The server sets it up once at its start (a
|
|
3
3
|
// store_meta row holds the version); a runner refuses a version it does not know. Version 2 adds the computer ledger;
|
|
4
4
|
// version 3 numbers each save of a sign-in (sign_ins.save); version 4 stamps an MCP token's end (mcp_servers.token_until);
|
|
5
5
|
// version 5 maps an externally owned agent (agents.external_ref, revisions.external_version); version 6 tells a person's
|
|
6
|
-
// stop from a cancel (runs.outcome 'stopped' or 'cancelled')
|
|
6
|
+
// stop from a cancel (runs.outcome 'stopped' or 'cancelled'); version 7 keeps a run's learning by round
|
|
7
|
+
// (learning_proposals.round: a follow-up to a run that ended looks back again, a round of its own); version 8 adds the
|
|
8
|
+
// run-change feed (run_changes), each channel post's tries (channel_attempts) and the claim a post's try settles under
|
|
9
|
+
// (channel_posts.claim); version 9 adds where a live viewer answers (computer_viewers.host, a runner's live host,
|
|
10
|
+
// live-reach.ts); version 10 pairs a person's own computer (paired_computers, pairing_codes); version 11 keeps a command's answer
|
|
11
|
+
// (commands.answer).
|
|
7
12
|
// Conventions: every time is INTEGER epoch milliseconds; JSON is TEXT checked by json_valid; money is REAL US dollars;
|
|
8
13
|
// sealed columns hold ciphertext only (sealed with the server's key outside the store), so the store never sees a secret
|
|
9
14
|
// in the clear. Foreign keys are on (the connection's pragma).
|
|
@@ -14,7 +19,7 @@
|
|
|
14
19
|
// learning state, the MCP registry, pending MCP sign-ins, each run's MCP pin, and the computer ledger. PI_DDL is pi-durable's
|
|
15
20
|
// journal tables, keyed by run_id (store/journal): one schema for every run's journal, so a query joins runs and their
|
|
16
21
|
// journals.
|
|
17
|
-
export const SCHEMA_VERSION =
|
|
22
|
+
export const SCHEMA_VERSION = 11;
|
|
18
23
|
/** One answer per approval of a run: a second approve or deny command naming the same approval is refused
|
|
19
24
|
* (StoreConstraint), so the first answer a person gives wins from the moment it is written, before the runner takes it
|
|
20
25
|
* (runs-over-store.ts approve). In STORE_DDL, and added at a server open to a store an earlier build of this version made. */
|
|
@@ -107,6 +112,25 @@ export const STORE_DDL = [
|
|
|
107
112
|
`CREATE INDEX IF NOT EXISTS runs_by_agent ON runs (agent_id, started_at)`,
|
|
108
113
|
`CREATE INDEX IF NOT EXISTS runs_by_revision ON runs (revision_id, started_at)`,
|
|
109
114
|
`CREATE INDEX IF NOT EXISTS runs_claimable ON runs (state, started_at)`,
|
|
115
|
+
// The run-change feed (Runs.changedSince): a row for each run reaching a final state or let go by its runner (parked, or
|
|
116
|
+
// running with no runner), written by the trigger below in the transaction of the write that made it, so no writer of a
|
|
117
|
+
// run's state or runner can miss it and none lands without its write. `seq` numbers them in commit order (one writer).
|
|
118
|
+
`CREATE TABLE IF NOT EXISTS run_changes (
|
|
119
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
120
|
+
run_id TEXT NOT NULL,
|
|
121
|
+
state TEXT NOT NULL,
|
|
122
|
+
ended_at INTEGER,
|
|
123
|
+
epoch INTEGER NOT NULL,
|
|
124
|
+
runner TEXT
|
|
125
|
+
) STRICT`,
|
|
126
|
+
// A change is a move of the state or the runner into a final state, parked, or running with none: a fence's touch of
|
|
127
|
+
// the row (epoch = epoch), a claim, a renew and a cost or summary write are none.
|
|
128
|
+
`CREATE TRIGGER IF NOT EXISTS runs_changed AFTER UPDATE OF state, runner ON runs
|
|
129
|
+
WHEN (OLD.state IS NOT NEW.state OR OLD.runner IS NOT NEW.runner)
|
|
130
|
+
AND (NEW.state IN ('ended', 'stopped', 'failed', 'parked') OR (NEW.state = 'running' AND NEW.runner IS NULL))
|
|
131
|
+
BEGIN
|
|
132
|
+
INSERT INTO run_changes (run_id, state, ended_at, epoch, runner) VALUES (NEW.id, NEW.state, NEW.ended_at, NEW.epoch, NEW.runner);
|
|
133
|
+
END`,
|
|
110
134
|
// One run per imported session: Store.importRun is refused a session it already imported (as the old store's
|
|
111
135
|
// UNIQUE (source, session) did).
|
|
112
136
|
`CREATE UNIQUE INDEX IF NOT EXISTS runs_import ON runs (json_extract(imported_from, '$.source'), json_extract(imported_from, '$.session')) WHERE imported_from IS NOT NULL`,
|
|
@@ -119,7 +143,8 @@ export const STORE_DDL = [
|
|
|
119
143
|
created_at INTEGER NOT NULL,
|
|
120
144
|
taken_at INTEGER,
|
|
121
145
|
refused TEXT,
|
|
122
|
-
tries INTEGER NOT NULL DEFAULT 0
|
|
146
|
+
tries INTEGER NOT NULL DEFAULT 0,
|
|
147
|
+
${json("answer", true)}
|
|
123
148
|
) STRICT`,
|
|
124
149
|
`CREATE INDEX IF NOT EXISTS commands_by_run ON commands (run_id, id)`,
|
|
125
150
|
`CREATE INDEX IF NOT EXISTS commands_untaken ON commands (run_id) WHERE taken_at IS NULL`,
|
|
@@ -223,6 +248,7 @@ export const STORE_DDL = [
|
|
|
223
248
|
attempts INTEGER NOT NULL DEFAULT 0,
|
|
224
249
|
next_at INTEGER NOT NULL,
|
|
225
250
|
claimed_until INTEGER,
|
|
251
|
+
claim TEXT,
|
|
226
252
|
state TEXT NOT NULL CHECK (state IN ('queued', 'sending', 'sent', 'dropped')),
|
|
227
253
|
reason TEXT,
|
|
228
254
|
created_at INTEGER NOT NULL
|
|
@@ -236,6 +262,22 @@ export const STORE_DDL = [
|
|
|
236
262
|
at INTEGER NOT NULL,
|
|
237
263
|
PRIMARY KEY (channel, run_id, marker)
|
|
238
264
|
) STRICT`,
|
|
265
|
+
// Each try of a channel's post (the outbound webhook's deliveries log): kept to the newest few per channel, each once by
|
|
266
|
+
// its try's id.
|
|
267
|
+
`CREATE TABLE IF NOT EXISTS channel_attempts (
|
|
268
|
+
seq INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
269
|
+
try_id TEXT NOT NULL UNIQUE,
|
|
270
|
+
channel TEXT NOT NULL,
|
|
271
|
+
post_id TEXT NOT NULL,
|
|
272
|
+
moment TEXT NOT NULL,
|
|
273
|
+
at INTEGER NOT NULL,
|
|
274
|
+
status_code INTEGER,
|
|
275
|
+
latency_ms INTEGER NOT NULL,
|
|
276
|
+
error TEXT,
|
|
277
|
+
retry_at INTEGER,
|
|
278
|
+
redelivery INTEGER NOT NULL CHECK (redelivery IN (0, 1))
|
|
279
|
+
) STRICT`,
|
|
280
|
+
`CREATE INDEX IF NOT EXISTS channel_attempts_by_channel ON channel_attempts (channel, seq)`,
|
|
239
281
|
// A code review's ledger, per head and firing.
|
|
240
282
|
`CREATE TABLE IF NOT EXISTS review_ledger (
|
|
241
283
|
repo TEXT NOT NULL,
|
|
@@ -268,6 +310,29 @@ export const STORE_DDL = [
|
|
|
268
310
|
${json("detail")},
|
|
269
311
|
updated_at INTEGER NOT NULL,
|
|
270
312
|
PRIMARY KEY (provider, session_id)
|
|
313
|
+
) STRICT`,
|
|
314
|
+
// A person's own machine paired with this server (spec/computer/attach.md): its driver dials the attach socket with the
|
|
315
|
+
// token pairing gave it, of which only the SHA-256 is kept (hex). `hello`: the driver's hello at pairing, what it paired
|
|
316
|
+
// as; `driver`: the build its last hello named. `seen_at`: its last socket's hello; `revoked_at`: when it was removed,
|
|
317
|
+
// after which its token dials no more (the row stays, so a run that names it is told it was removed).
|
|
318
|
+
`CREATE TABLE IF NOT EXISTS paired_computers (
|
|
319
|
+
id TEXT PRIMARY KEY,
|
|
320
|
+
name TEXT NOT NULL,
|
|
321
|
+
${json("hello")},
|
|
322
|
+
driver TEXT NOT NULL,
|
|
323
|
+
${json("labels")},
|
|
324
|
+
token_sha256 TEXT NOT NULL UNIQUE,
|
|
325
|
+
pairing_id TEXT NOT NULL UNIQUE,
|
|
326
|
+
created_at INTEGER NOT NULL,
|
|
327
|
+
seen_at INTEGER,
|
|
328
|
+
revoked_at INTEGER
|
|
329
|
+
) STRICT`,
|
|
330
|
+
// A pairing code, by its SHA-256 alone (hex): good once, until expires_at. A used one is deleted as it pairs. `id` names
|
|
331
|
+
// the pairing (not the code): the console that made it reads it back on the computer the code made (paired_computers.pairing_id).
|
|
332
|
+
`CREATE TABLE IF NOT EXISTS pairing_codes (
|
|
333
|
+
id TEXT NOT NULL UNIQUE,
|
|
334
|
+
code_sha256 TEXT PRIMARY KEY,
|
|
335
|
+
expires_at INTEGER NOT NULL
|
|
271
336
|
) STRICT`,
|
|
272
337
|
`CREATE TABLE IF NOT EXISTS learning (
|
|
273
338
|
agent_id TEXT NOT NULL REFERENCES agents (id),
|
|
@@ -382,19 +447,23 @@ export const STORE_DDL = [
|
|
|
382
447
|
at INTEGER NOT NULL,
|
|
383
448
|
runner TEXT NOT NULL,
|
|
384
449
|
epoch INTEGER NOT NULL,
|
|
450
|
+
host TEXT,
|
|
385
451
|
PRIMARY KEY (run_id, kind, tag)
|
|
386
452
|
) STRICT`,
|
|
387
453
|
// Learning on the runner: what a run's end-of-run turn kept (the turn it asked and answered, and the proposal checked from
|
|
388
454
|
// it), written in the same commit as the run's end, beside the learning run that grades and merges it (learning_run_id,
|
|
389
|
-
// none for a learning dropped before it was proposed).
|
|
390
|
-
//
|
|
455
|
+
// none for a learning dropped before it was proposed). One row a round: a run's first end is round 1, and each end
|
|
456
|
+
// after a follow-up opened it again is the next. Read by the learning routes (the pre-merge view), by the learning run,
|
|
457
|
+
// and by the reader of a run's learning end (store-reads/learning.ts), which reads the newest round.
|
|
391
458
|
`CREATE TABLE IF NOT EXISTS learning_proposals (
|
|
392
|
-
run_id TEXT
|
|
459
|
+
run_id TEXT NOT NULL REFERENCES runs (id),
|
|
460
|
+
round INTEGER NOT NULL CHECK (round >= 1),
|
|
393
461
|
agent_id TEXT NOT NULL REFERENCES agents (id),
|
|
394
462
|
${json("turn")},
|
|
395
463
|
${json("proposal")},
|
|
396
464
|
learning_run_id TEXT,
|
|
397
|
-
created_at INTEGER NOT NULL
|
|
465
|
+
created_at INTEGER NOT NULL,
|
|
466
|
+
PRIMARY KEY (run_id, round)
|
|
398
467
|
) STRICT`,
|
|
399
468
|
`CREATE INDEX IF NOT EXISTS learning_proposals_by_agent ON learning_proposals (agent_id, created_at DESC)`,
|
|
400
469
|
];
|