@parcha/agentrun-serve 0.1.0-beta.50 → 0.1.0-beta.52
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 +185 -0
- package/README.md +236 -30
- package/dist/adapters.js +1 -1
- package/dist/agent-delete.d.ts +13 -2
- package/dist/agent-delete.js +14 -5
- package/dist/agents-store.d.ts +5 -1
- package/dist/agents-store.js +17 -7
- package/dist/api-only.d.ts +6 -3
- package/dist/api-only.js +8 -5
- package/dist/archil-routes.d.ts +1 -0
- package/dist/archil-routes.js +28 -2
- package/dist/capacity.d.ts +50 -0
- package/dist/capacity.js +49 -0
- package/dist/cli-learning/claude-wire.d.ts +41 -0
- package/dist/cli-learning/claude-wire.js +258 -0
- package/dist/cli-learning/cli.d.ts +11 -0
- package/dist/cli-learning/cli.js +241 -0
- package/dist/cli-learning/codex-worktree.d.ts +10 -0
- package/dist/cli-learning/codex-worktree.js +119 -0
- package/dist/cli-learning/connections.d.ts +62 -0
- package/dist/cli-learning/connections.js +87 -0
- package/dist/cli-learning/entry.d.ts +1 -0
- package/dist/cli-learning/entry.js +4 -0
- package/dist/cli-learning/evidence.d.ts +78 -0
- package/dist/cli-learning/evidence.js +492 -0
- package/dist/cli-learning/hook.d.ts +33 -0
- package/dist/cli-learning/hook.js +544 -0
- package/dist/cli-learning/install.d.ts +33 -0
- package/dist/cli-learning/install.js +335 -0
- package/dist/cli-learning/observation-proof.d.ts +14 -0
- package/dist/cli-learning/observation-proof.js +10 -0
- package/dist/cli-learning/policy.d.ts +48 -0
- package/dist/cli-learning/policy.js +363 -0
- package/dist/cli-learning/schema.d.ts +16 -0
- package/dist/cli-learning/schema.js +27 -0
- package/dist/cli-learning/state.d.ts +23 -0
- package/dist/cli-learning/state.js +61 -0
- package/dist/cli.js +24 -2
- package/dist/computer-setting.d.ts +8 -0
- package/dist/computer-setting.js +11 -0
- package/dist/computers.d.ts +6 -2
- package/dist/computers.js +2 -2
- package/dist/console/assets/index-B-D0EM3j.js +733 -0
- package/dist/console/assets/{index-D0mr9wfZ.css → index-DlQepP2J.css} +1 -1
- package/dist/console/index.html +2 -2
- package/dist/email/email-faux.d.ts +3 -1
- package/dist/email/email-faux.js +3 -2
- package/dist/email/routes.js +22 -12
- package/dist/fal-price.d.ts +76 -0
- package/dist/fal-price.js +368 -0
- package/dist/harness-install.d.ts +6 -2
- package/dist/harness-install.js +40 -8
- package/dist/harness.d.ts +1 -1
- package/dist/harness.js +2 -2
- package/dist/learn/commit.d.ts +5 -0
- package/dist/learn/commit.js +4 -1
- package/dist/learn/compile.d.ts +13 -5
- package/dist/learn/compile.js +24 -7
- package/dist/learn/delete.d.ts +12 -6
- package/dist/learn/delete.js +17 -11
- package/dist/learn/folder.d.ts +11 -0
- package/dist/learn/folder.js +20 -3
- package/dist/learn/learning-of.d.ts +8 -0
- package/dist/learn/learning-of.js +8 -0
- package/dist/learn/learning.sh +14 -1
- package/dist/learn/promote.d.ts +51 -7
- package/dist/learn/promote.js +51 -8
- package/dist/learn/secrets.js +14 -3
- package/dist/learning-reset.d.ts +18 -0
- package/dist/learning-reset.js +115 -0
- package/dist/learning-routes.d.ts +1 -1
- package/dist/learning-words.d.ts +6 -1
- package/dist/learning-words.js +6 -1
- package/dist/learning.d.ts +8 -6
- package/dist/learning.js +5 -4
- package/dist/llms.txt +1 -0
- package/dist/mcp-gateway.d.ts +39 -2
- package/dist/mcp-gateway.js +41 -10
- package/dist/mcp-pin-first.js +3 -2
- package/dist/mcp-private-hosts.d.ts +24 -3
- package/dist/mcp-private-hosts.js +53 -3
- package/dist/mcp-servers-store.d.ts +1 -0
- package/dist/mcp-servers-store.js +2 -1
- package/dist/media-duration.d.ts +2 -0
- package/dist/media-duration.js +136 -0
- package/dist/model-gateway.d.ts +2 -0
- package/dist/model-gateway.js +3 -1
- package/dist/native/runtimes.d.ts +2 -0
- package/dist/native/runtimes.js +2 -0
- package/dist/observation/learning.d.ts +32 -0
- package/dist/observation/learning.js +66 -0
- package/dist/observation/observer.d.ts +8 -1
- package/dist/observation/observer.js +89 -9
- package/dist/observation/schema.js +12 -4
- package/dist/observation/usage-identity.d.ts +9 -0
- package/dist/observation/usage-identity.js +11 -0
- package/dist/openapi.js +133 -24
- package/dist/provider.d.ts +8 -0
- package/dist/provider.js +14 -2
- package/dist/refusal-words.d.ts +0 -3
- package/dist/refusal-words.js +0 -3
- package/dist/request-metadata.d.ts +20 -0
- package/dist/request-metadata.js +24 -0
- package/dist/run-launch.d.ts +27 -4
- package/dist/run-launch.js +18 -3
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-usage.d.ts +48 -4
- package/dist/run-usage.js +156 -39
- package/dist/run-view.d.ts +2 -0
- package/dist/run-view.js +8 -7
- package/dist/runner/adapter-loop.d.ts +3 -1
- package/dist/runner/adapter-loop.js +41 -35
- package/dist/runner/adapter-run-spec.d.ts +1 -1
- package/dist/runner/adapter-run-spec.js +15 -15
- package/dist/runner/audit.d.ts +8 -3
- package/dist/runner/audit.js +21 -6
- package/dist/runner/beside.d.ts +13 -0
- package/dist/runner/beside.js +27 -8
- package/dist/runner/claim-secrets.d.ts +2 -1
- package/dist/runner/claim-secrets.js +5 -3
- package/dist/runner/cli-run-spec.d.ts +1 -1
- package/dist/runner/cli-run-spec.js +14 -13
- package/dist/runner/conformance/harness.js +4 -2
- package/dist/runner/disk-free.d.ts +44 -0
- package/dist/runner/disk-free.js +96 -0
- package/dist/runner/dispatch.js +4 -2
- package/dist/runner/feedback-round.d.ts +12 -8
- package/dist/runner/feedback-round.js +16 -30
- package/dist/runner/harness-loops.js +8 -3
- package/dist/runner/journal-kinds.d.ts +4 -1
- package/dist/runner/learning-end.d.ts +2 -1
- package/dist/runner/learning-end.js +51 -22
- package/dist/runner/main.d.ts +17 -6
- package/dist/runner/main.js +187 -45
- package/dist/runner/mcp-claim.js +1 -1
- package/dist/runner/mcp-run.d.ts +9 -0
- package/dist/runner/mcp-run.js +16 -1
- package/dist/runner/model-route.js +7 -4
- package/dist/runner/presence.d.ts +11 -2
- package/dist/runner/presence.js +31 -3
- package/dist/runner/promotion.d.ts +40 -21
- package/dist/runner/promotion.js +130 -59
- package/dist/runner/run-child-host.d.ts +12 -15
- package/dist/runner/run-child-host.js +52 -112
- package/dist/runner/run-errors.js +2 -0
- package/dist/runner/runner-check.d.ts +51 -0
- package/dist/runner/runner-check.js +206 -0
- package/dist/runner/runner-metrics.d.ts +19 -0
- package/dist/runner/runner-metrics.js +49 -0
- package/dist/runner/runner.d.ts +37 -0
- package/dist/runner/runner.js +58 -6
- package/dist/runner/sdk-loop.d.ts +2 -2
- package/dist/runner/sdk-loop.js +37 -25
- package/dist/runner/store-port.d.ts +3 -0
- package/dist/runner/workflow-spec.js +3 -3
- package/dist/runs-over-store.d.ts +13 -3
- package/dist/runs-over-store.js +71 -16
- package/dist/sdk/check.d.ts +6 -5
- package/dist/sdk/check.js +17 -16
- package/dist/sdk/runs.d.ts +22 -2
- package/dist/sdk/runs.js +167 -82
- package/dist/server.d.ts +3 -0
- package/dist/server.js +230 -43
- package/dist/skills/agentrun-serve/SKILL.md +52 -7
- package/dist/start-switches.js +2 -0
- package/dist/stats.d.ts +4 -0
- package/dist/stats.js +1 -1
- package/dist/store/agent-body.d.ts +16 -0
- package/dist/store/agent-body.js +20 -3
- package/dist/store/dialect.d.ts +2 -0
- package/dist/store/dialect.js +1 -0
- package/dist/store/index.d.ts +3 -3
- package/dist/store/index.js +2 -2
- package/dist/store/journal/storage.d.ts +2 -0
- package/dist/store/journal/storage.js +2 -1
- package/dist/store/postgres/connection.d.ts +5 -2
- package/dist/store/postgres/connection.js +19 -7
- package/dist/store/postgres/dialect.js +1 -0
- package/dist/store/postgres/migrate.d.ts +30 -0
- package/dist/store/postgres/migrate.js +72 -5
- package/dist/store/postgres/migration-lint.d.ts +8 -0
- package/dist/store/postgres/migration-lint.js +119 -0
- package/dist/store/postgres/migrations/0024_runner_capacity.sql +28 -0
- package/dist/store/postgres/migrations/0025_run_attempts.sql +27 -0
- package/dist/store/postgres/migrations/0026_run_disk.sql +23 -0
- package/dist/store/postgres/store.d.ts +8 -3
- package/dist/store/postgres/store.js +31 -11
- package/dist/store/schema.d.ts +32 -2
- package/dist/store/schema.js +74 -5
- package/dist/store/sqlite/run-launch.d.ts +31 -1
- package/dist/store/sqlite/run-launch.js +111 -2
- package/dist/store/sqlite/run-patch.d.ts +2 -1
- package/dist/store/sqlite/run-patch.js +4 -3
- package/dist/store/sqlite/store.d.ts +17 -0
- package/dist/store/sqlite/store.js +226 -37
- package/dist/store/store-schema.d.ts +34 -0
- package/dist/store/store-schema.js +125 -0
- package/dist/store/store.d.ts +120 -5
- package/dist/store/store.js +13 -0
- package/dist/store-reads/analytics.d.ts +3 -2
- package/dist/store-reads/analytics.js +20 -8
- package/dist/store-reads/learning.d.ts +12 -8
- package/dist/store-reads/learning.js +33 -24
- package/dist/store-reads/loop.d.ts +4 -3
- package/dist/store-reads/loop.js +7 -5
- package/dist/store-view.d.ts +4 -3
- package/dist/store-view.js +11 -7
- package/dist/template-eval.js +4 -4
- package/dist/templates.d.ts +6 -2
- package/dist/templates.js +7 -5
- package/dist/timeline/labels.js +1 -1
- package/dist/tool-gate.d.ts +4 -2
- package/dist/tools.js +1 -1
- package/dist/unified-usage.d.ts +24 -0
- package/dist/unified-usage.js +400 -0
- package/dist/uploads-archil.d.ts +100 -0
- package/dist/uploads-archil.js +431 -0
- package/dist/uploads.d.ts +52 -1
- package/dist/uploads.js +186 -108
- package/dist/validate.js +5 -1
- package/dist/web-save.d.ts +3 -0
- package/dist/web-save.js +17 -2
- package/dist/workspace/archil-disk.d.ts +6 -5
- package/dist/workspace/archil-disk.js +27 -12
- package/dist/workspace/archil-exec.js +1 -1
- package/dist/workspace/archil.js +4 -3
- package/dist/workspace/revision-skills.js +24 -11
- package/dist/workspace/synced-folder.d.ts +3 -3
- package/dist/workspace/synced-folder.js +7 -6
- package/package.json +10 -10
- package/skills/agentrun-serve/SKILL.md +52 -7
- package/dist/console/assets/index-D7G6o-1E.js +0 -720
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// DELETE /v2/agents/{agent_id}/learning: an agent's learning reset, the agent kept. What it learned goes (the learning
|
|
2
|
+
// module's deleteLearning: its learning folder's files, its git history and audit ledger among them, on this server and
|
|
3
|
+
// on every Archil disk its runs used, learn/learning-of.ts namedDisksOf, then its promotions and grades); the agent, its
|
|
4
|
+
// definition, its runs and everything kept of them (their files, journals, costs, the learning each one kept) stay, so
|
|
5
|
+
// the run list, the stats and the usage read the same before and after. A served run's cost carries its share of its
|
|
6
|
+
// revision's compile and check, read from the promotion records the reset deletes: each revision they name is kept
|
|
7
|
+
// first in the agent's reset ledger (learn/promote.ts keepResetLedger: workflow, digest and K alone), so the share reads
|
|
8
|
+
// the same after.
|
|
9
|
+
//
|
|
10
|
+
// What goes with the folder: each run's learning diff (GET /runs/{id}/learning/diff, the text it merged) is the folder's,
|
|
11
|
+
// so it is 404 after; what the run's own record keeps of its learning end stays with the run.
|
|
12
|
+
//
|
|
13
|
+
// In order, in the agent's delete turn (server.ts agentWrite: one learning write of an agent at a time, and none once
|
|
14
|
+
// its DELETE began):
|
|
15
|
+
// 1. The agent's learning work in flight is stopped. A run that exists only for its learning (a check's or an owner's
|
|
16
|
+
// replay, an audit's shadow, a built-in learning run) is cancelled through the cancel path and waited for until it
|
|
17
|
+
// is final. A run of the agent's own still going is not: its learning lives in it (the seed it was started from,
|
|
18
|
+
// a workflow it may be served, its compile turn and look-back at its end), and the owner's job is never cancelled
|
|
19
|
+
// to reset learning. Any such run, or a learning run this server cannot end (an SDK run, an imported one) or one
|
|
20
|
+
// not final by the wait's end, is 409 learning_busy, naming them, and the agent's learning is left as it was.
|
|
21
|
+
// 2. In the agent's learning queue (learning.ts withAgentRepo, where a run's start reads its seed and a person's edit
|
|
22
|
+
// commits), the agent's runs are read again: one still going (a run started meanwhile) is 409 learning_busy, nothing
|
|
23
|
+
// deleted. Then deleteLearning, which reads them once more itself.
|
|
24
|
+
// Idempotent: an agent that never learned, or one reset already, has nothing to delete, and is answered as done.
|
|
25
|
+
//
|
|
26
|
+
// Step 2 sets the agent's reset watermark (learn/promote.ts resetAtKey) before it deletes: from then on learning reads
|
|
27
|
+
// only runs after it (learn/compile.ts sinceReset: compile_after's count, a compile's traces, a check's inputs, an
|
|
28
|
+
// audit's draws, the money rule). A run whose start read the learning folder before step 2
|
|
29
|
+
// and whose row the store wrote after it keeps what it was started with (its corrections are in its launch), but
|
|
30
|
+
// learns nothing: its seed was read before the watermark, so its learning end drops it whole at its entry
|
|
31
|
+
// (runner/learning-end.ts, reason `reset`), and the learning script refuses a merge on a seed the folder no longer holds
|
|
32
|
+
// (learning.sh `seed`). No workflow is laid for it either: they are read at its seed's commit, which is gone.
|
|
33
|
+
import { ApiError } from "./errors.js";
|
|
34
|
+
import { LearningInFlight } from "./learn/delete.js";
|
|
35
|
+
import { isShadowRun } from "./learn/audit.js";
|
|
36
|
+
import { keepResetAt, keepResetLedger, promotionsOf, resetAtKey, resetAtOf } from "./learn/promote.js";
|
|
37
|
+
import { readLaunchBody } from "./run-launch.js";
|
|
38
|
+
import { FINAL_STATES, StoreLaunchMissing } from "./store/index.js";
|
|
39
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
40
|
+
const isFinal = (state) => FINAL_STATES.includes(state);
|
|
41
|
+
/** Whether run `runId` exists only for its agent's learning: an audit's shadow, a replay (a check's or an owner's), or a
|
|
42
|
+
* built-in learning run, by its launch. A run with no launch read here is the agent's own. */
|
|
43
|
+
async function learningOnly(store, runId) {
|
|
44
|
+
if (isShadowRun(runId))
|
|
45
|
+
return true;
|
|
46
|
+
const launch = await store.runs.launch(runId).then((row) => readLaunchBody(row.body), (error) => (error instanceof StoreLaunchMissing ? undefined : Promise.reject(error)));
|
|
47
|
+
return launch !== undefined && (launch.replay !== undefined || launch.audit !== undefined || launch.harness === "learning");
|
|
48
|
+
}
|
|
49
|
+
/** The agent's runs not final, its built-in learning runs included, each with whether it exists only for learning. */
|
|
50
|
+
async function goingOf(store, agentId) {
|
|
51
|
+
const going = (await store.runs.every(agentId)).filter((run) => !isFinal(run.state));
|
|
52
|
+
return Promise.all(going.map(async (run) => ({ id: run.id, learning: await learningOnly(store, run.id) })));
|
|
53
|
+
}
|
|
54
|
+
const busy = (agentId, runs, why, retryMs = 5_000) => new ApiError("learning_busy", `The agent ${agentId}'s learning was not reset: ${why} (${runs.slice(0, 5).join(", ")}${runs.length > 5 ? ` and ${runs.length - 5} more` : ""}). Nothing of its learning was deleted: send the DELETE again once ${runs.length === 1 ? "it has" : "they have"} ended.`, { details: { runs: [...runs] }, retry_after_ms: retryMs });
|
|
55
|
+
const OWN = (n) => (n === 1 ? "a run of its own is still going, and its learning lives in it" : `${n} runs of its own are still going, and their learning lives in them`);
|
|
56
|
+
/** Agent `agentId`'s learning reset in the two steps above. Throws ApiError learning_busy, nothing of its learning
|
|
57
|
+
* deleted, while a run of the agent's own is going or its learning runs cannot be stopped in time. */
|
|
58
|
+
export async function resetLearning(host, agentId) {
|
|
59
|
+
// 1. Its own runs refuse the reset before anything is cancelled; its learning runs are cancelled and waited for.
|
|
60
|
+
const going = await goingOf(host.store, agentId);
|
|
61
|
+
const own = going.filter((run) => !run.learning).map((run) => run.id);
|
|
62
|
+
if (own.length)
|
|
63
|
+
throw busy(agentId, own, OWN(own.length));
|
|
64
|
+
await stopLearningRuns(host, agentId, going.map((run) => run.id));
|
|
65
|
+
// 2. In the agent's learning queue: nothing going (a run started meanwhile refuses it), then its learning deleted.
|
|
66
|
+
await host.inQueue(agentId, async () => {
|
|
67
|
+
const still = await goingOf(host.store, agentId);
|
|
68
|
+
if (still.length)
|
|
69
|
+
throw busy(agentId, still.map((run) => run.id), still.some((run) => !run.learning) ? OWN(still.filter((run) => !run.learning).length) : "its learning work started again");
|
|
70
|
+
// What the agent's kept runs carry of their revisions' compile and check, kept before the records it is read from go.
|
|
71
|
+
await keepResetLedger(host.store.settings, agentId, await promotionsOf(host.store.settings, agentId));
|
|
72
|
+
// The watermark, before anything goes: learning reads no run that started, or read its seed, at or before it (each
|
|
73
|
+
// seed is read in this queue, so one read before this moment never holds a later time). A seed read after the reset
|
|
74
|
+
// in the same millisecond counts as before it: its run learns nothing (reason reset) and an SDK start is refused, to
|
|
75
|
+
// start again, never a run given what the reset deleted. Set first, so no moment has the folder gone and the old runs
|
|
76
|
+
// still read. A delete refused before it removed anything (LearningInFlight) puts the earlier one back, its learning
|
|
77
|
+
// left as it was; one that failed on the way (a disk, a file, a record) may have removed some of it, so the watermark
|
|
78
|
+
// stays and the next call finishes the reset: the old runs never feed a compile into a folder emptied part way.
|
|
79
|
+
const earlier = await resetAtOf(host.store.settings, agentId);
|
|
80
|
+
const at = Date.now();
|
|
81
|
+
await keepResetAt(host.store.settings, agentId, at);
|
|
82
|
+
try {
|
|
83
|
+
await host.deleteLearning(host.store, agentId, { keepResetLedger: true, agentDisk: true });
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
if (!(error instanceof LearningInFlight))
|
|
87
|
+
throw error;
|
|
88
|
+
await (earlier === undefined ? host.store.settings.remove(resetAtKey(agentId)) : keepResetAt(host.store.settings, agentId, earlier));
|
|
89
|
+
throw busy(agentId, error.runs, "its learning work is in flight");
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
/** Step 1's second half: each learning run cancelled once, then read again until each is final. One this server cannot
|
|
94
|
+
* end, or one still going at the wait's end, is learning_busy. */
|
|
95
|
+
async function stopLearningRuns(host, agentId, runs) {
|
|
96
|
+
if (!runs.length)
|
|
97
|
+
return;
|
|
98
|
+
const unended = [];
|
|
99
|
+
for (const runId of runs)
|
|
100
|
+
if (!(await host.cancel(runId)))
|
|
101
|
+
unended.push(runId);
|
|
102
|
+
if (unended.length)
|
|
103
|
+
throw busy(agentId, unended, `its learning work is in ${unended.length === 1 ? "a run" : "runs"} this server cannot end`);
|
|
104
|
+
const deadline = Date.now() + host.waitMs;
|
|
105
|
+
for (;;) {
|
|
106
|
+
const states = new Map((await host.store.runs.every(agentId)).map((run) => [run.id, run.state]));
|
|
107
|
+
// A run no longer there is ended.
|
|
108
|
+
const live = runs.filter((runId) => !isFinal(states.get(runId) ?? "ended"));
|
|
109
|
+
if (!live.length)
|
|
110
|
+
return;
|
|
111
|
+
if (Date.now() >= deadline)
|
|
112
|
+
throw busy(agentId, live, `its learning work was cancelled and ${live.length === 1 ? "one run has" : `${live.length} runs have`} not ended yet`, 2_000);
|
|
113
|
+
await sleep(host.pollMs);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -190,7 +190,7 @@ export declare function runLearningBody(input: {
|
|
|
190
190
|
}, shown: Shown, secrets: readonly string[]): {
|
|
191
191
|
run: string;
|
|
192
192
|
state: "dropped" | "merged" | "seeded" | "reflecting";
|
|
193
|
-
reason: "cancelled" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
|
|
193
|
+
reason: "cancelled" | "reset" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
|
|
194
194
|
detail: string | null;
|
|
195
195
|
at: string;
|
|
196
196
|
workflows: import("./learning.js").LearnedFile[];
|
package/dist/learning-words.d.ts
CHANGED
|
@@ -17,13 +17,16 @@ export declare const STOPPED: {
|
|
|
17
17
|
readonly notCopied: "what the agent learned could not be handed to the run, so the run started without it";
|
|
18
18
|
readonly noSeed: "the run started without what the agent learned";
|
|
19
19
|
readonly cancelled: "the run was cancelled";
|
|
20
|
-
readonly cancelledLooking: "
|
|
20
|
+
readonly cancelledLooking: "a stop or cancel cut it while it wrote what it learned";
|
|
21
21
|
readonly crashed: "the run's process ended before it could keep what it learned";
|
|
22
22
|
readonly failed: "writing what it learned ended on an error";
|
|
23
23
|
readonly unanswered: "the turn that writes its workflow ended before it answered";
|
|
24
24
|
readonly notChecked: "what it learned could not be checked";
|
|
25
25
|
readonly refuseUnread: "what the run must never learn (its agent's browser sign-in, the server's secrets) could not be read back, so nothing it learned is kept";
|
|
26
|
+
readonly otherDisk: "the run is on a disk its start named, and a run there learns nothing yet: it started without what the agent learned, and what it does is not learned";
|
|
26
27
|
};
|
|
28
|
+
/** Why a run whose start has no seed started with nothing learned (run-launch.ts SeedFailed), in words. */
|
|
29
|
+
export declare const seedStopped: (failed: string | undefined) => string;
|
|
27
30
|
/** A run's learning, in words, by where it stands. */
|
|
28
31
|
export declare const said: {
|
|
29
32
|
seeded: (corrections: number, workflows?: number) => string;
|
|
@@ -52,6 +55,8 @@ export declare const said: {
|
|
|
52
55
|
noBudget: string;
|
|
53
56
|
unknownCost: string;
|
|
54
57
|
stopped: (cause: string) => string;
|
|
58
|
+
/** A run whose learning seed predates a reset of its agent's learning (runner/learning-end.ts, reason `reset`). */
|
|
59
|
+
reset: string;
|
|
55
60
|
};
|
|
56
61
|
/** A run's learning in words as a read says them, whole: what it learned and what waits for the owner's OK, said again
|
|
57
62
|
* from its parts (`workflows`, `corrections`, `problems`), each name cut by `cut` first (the host's secrets as they are
|
package/dist/learning-words.js
CHANGED
|
@@ -55,13 +55,16 @@ export const STOPPED = {
|
|
|
55
55
|
notCopied: "what the agent learned could not be handed to the run, so the run started without it",
|
|
56
56
|
noSeed: "the run started without what the agent learned",
|
|
57
57
|
cancelled: "the run was cancelled",
|
|
58
|
-
cancelledLooking: "
|
|
58
|
+
cancelledLooking: "a stop or cancel cut it while it wrote what it learned",
|
|
59
59
|
crashed: "the run's process ended before it could keep what it learned",
|
|
60
60
|
failed: "writing what it learned ended on an error",
|
|
61
61
|
unanswered: "the turn that writes its workflow ended before it answered",
|
|
62
62
|
notChecked: "what it learned could not be checked",
|
|
63
63
|
refuseUnread: "what the run must never learn (its agent's browser sign-in, the server's secrets) could not be read back, so nothing it learned is kept",
|
|
64
|
+
otherDisk: "the run is on a disk its start named, and a run there learns nothing yet: it started without what the agent learned, and what it does is not learned",
|
|
64
65
|
};
|
|
66
|
+
/** Why a run whose start has no seed started with nothing learned (run-launch.ts SeedFailed), in words. */
|
|
67
|
+
export const seedStopped = (failed) => failed === "unreadable" ? STOPPED.unreadable : failed === "not_copied" ? STOPPED.notCopied : failed === "other_disk" ? STOPPED.otherDisk : STOPPED.noSeed;
|
|
65
68
|
/** A run's learning, in words, by where it stands. */
|
|
66
69
|
export const said = {
|
|
67
70
|
seeded: (corrections, workflows = 0) => {
|
|
@@ -108,6 +111,8 @@ export const said = {
|
|
|
108
111
|
noBudget: "Skipped: no budget left in this run to write what it learned.",
|
|
109
112
|
unknownCost: "Skipped: this run's spend is unknown, so its budget can't cover writing what it learned.",
|
|
110
113
|
stopped: (cause) => `Not kept: learning stopped (${cause}).`,
|
|
114
|
+
/** A run whose learning seed predates a reset of its agent's learning (runner/learning-end.ts, reason `reset`). */
|
|
115
|
+
reset: "Dropped: its learning seed predates a reset of the agent's learning.",
|
|
111
116
|
};
|
|
112
117
|
/** A run's learning in words as a read says them, whole: what it learned and what waits for the owner's OK, said again
|
|
113
118
|
* from its parts (`workflows`, `corrections`, `problems`), each name cut by `cut` first (the host's secrets as they are
|
package/dist/learning.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export declare function learningOf(env: NodeJS.ProcessEnv, given?: string): Lear
|
|
|
14
14
|
* is on main, or `dropped` with a reason from the closed list. A learning fault never refuses a run: it starts with
|
|
15
15
|
* nothing learned, `seed_failed`. */
|
|
16
16
|
export declare const LEARNING_STATES: readonly ["seeded", "reflecting", "merged", "dropped"];
|
|
17
|
-
export declare const LEARNING_REASONS: readonly ["seed_failed", "no_budget", "unknown_cost", "cancelled", "crashed", "reflect_failed", "empty", "checker", "use"];
|
|
17
|
+
export declare const LEARNING_REASONS: readonly ["seed_failed", "no_budget", "unknown_cost", "cancelled", "crashed", "reflect_failed", "empty", "checker", "use", "reset"];
|
|
18
18
|
/** A run's own `learning` on its start (POST /agents/{agent_id}/runs): it starts from what its agent learned, as a run that
|
|
19
19
|
* learns does, and keeps nothing (no compile, no correction, at any end), so runs of one agent read one learned state
|
|
20
20
|
* however many run at once. Its learning ends `dropped`, reason `use`. */
|
|
@@ -111,7 +111,7 @@ export declare const detailShown: (learning: Learning, shown: Shown, max?: numbe
|
|
|
111
111
|
export declare const learningView: (learning: Learning, shown: Shown) => {
|
|
112
112
|
workflows?: LearnedFile[];
|
|
113
113
|
state: "dropped" | "merged" | "seeded" | "reflecting";
|
|
114
|
-
reason: "cancelled" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
|
|
114
|
+
reason: "cancelled" | "reset" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
|
|
115
115
|
detail: string | null;
|
|
116
116
|
at: string;
|
|
117
117
|
};
|
|
@@ -126,7 +126,7 @@ export declare function shortOf<T extends {
|
|
|
126
126
|
at: string;
|
|
127
127
|
}>({ state, reason, detail, at: _, ...rest }: T): {
|
|
128
128
|
state: "dropped" | "merged" | "seeded" | "reflecting";
|
|
129
|
-
reason: "cancelled" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
|
|
129
|
+
reason: "cancelled" | "reset" | "use" | "empty" | "crashed" | "reflect_failed" | "no_budget" | "checker" | "unknown_cost" | "seed_failed" | null;
|
|
130
130
|
detail: string | null;
|
|
131
131
|
} & Omit<T, "at" | "reason" | "state" | "detail">;
|
|
132
132
|
/** The label of the end-of-run turn's timeline rows, and the kind and label of a run's learning row. */
|
|
@@ -138,11 +138,13 @@ export type Triggered = {
|
|
|
138
138
|
text: string;
|
|
139
139
|
};
|
|
140
140
|
/** What a run is seeded from: main's commit (null when the folder has none yet), corrections.md's text ("" when none),
|
|
141
|
-
* and its active learned workflows, by name.
|
|
141
|
+
* and its active learned workflows, by name. `at`: when a run's seed was read (seedOf, epoch ms, in the agent's
|
|
142
|
+
* learning queue), which the agent's reset watermark is set in too: a seed read at or before it predates the reset. */
|
|
142
143
|
export type Seed = {
|
|
143
144
|
sha: string | null;
|
|
144
145
|
corrections: string;
|
|
145
146
|
workflows: LearnedWorkflow[];
|
|
147
|
+
at?: number;
|
|
146
148
|
};
|
|
147
149
|
/** A workflow the agent learned, as a run may run it: its name, the line its README says what it does with, and its
|
|
148
150
|
* workflow.json whole. */
|
|
@@ -170,8 +172,8 @@ export declare const withAgentRepo: <T>(data: string, agentId: string, work: ()
|
|
|
170
172
|
* under `data`. The one point that names the place; its `end()` is called in a finally after every use. */
|
|
171
173
|
export declare const learningFolderOf: (data: string, agentId: string) => LearningFolder;
|
|
172
174
|
/** The seed of a run of agent `agentId` about to be made: main's commit, the folder made first when it is absent (the
|
|
173
|
-
* learning script's init),
|
|
174
|
-
* folder's own script and file API. */
|
|
175
|
+
* learning script's init), what main holds for a run (seedIn), and when it was read (`at`). In the agent's queue, as a
|
|
176
|
+
* reset is (learning-reset.ts). No git in this process: the folder's own script and file API. */
|
|
175
177
|
export declare function seedOf(data: string, agentId: string): Promise<Seed>;
|
|
176
178
|
/** What commit `commit` of the folder holds for a run, as seedOf reads main (one read-only exec of the script). No commit
|
|
177
179
|
* (null) is the empty seed; a commit the folder no longer holds throws. */
|
package/dist/learning.js
CHANGED
|
@@ -35,7 +35,7 @@ export function learningOf(env, given) {
|
|
|
35
35
|
* is on main, or `dropped` with a reason from the closed list. A learning fault never refuses a run: it starts with
|
|
36
36
|
* nothing learned, `seed_failed`. */
|
|
37
37
|
export const LEARNING_STATES = ["seeded", "reflecting", "merged", "dropped"];
|
|
38
|
-
export const LEARNING_REASONS = ["seed_failed", "no_budget", "unknown_cost", "cancelled", "crashed", "reflect_failed", "empty", "checker", "use"];
|
|
38
|
+
export const LEARNING_REASONS = ["seed_failed", "no_budget", "unknown_cost", "cancelled", "crashed", "reflect_failed", "empty", "checker", "use", "reset"];
|
|
39
39
|
/** A run's own `learning` on its start (POST /agents/{agent_id}/runs): it starts from what its agent learned, as a run that
|
|
40
40
|
* learns does, and keeps nothing (no compile, no correction, at any end), so runs of one agent read one learned state
|
|
41
41
|
* however many run at once. Its learning ends `dropped`, reason `use`. */
|
|
@@ -189,14 +189,15 @@ async function seedIn(sha, snapshot, text) {
|
|
|
189
189
|
return { sha, corrections, workflows };
|
|
190
190
|
}
|
|
191
191
|
/** The seed of a run of agent `agentId` about to be made: main's commit, the folder made first when it is absent (the
|
|
192
|
-
* learning script's init),
|
|
193
|
-
* folder's own script and file API. */
|
|
192
|
+
* learning script's init), what main holds for a run (seedIn), and when it was read (`at`). In the agent's queue, as a
|
|
193
|
+
* reset is (learning-reset.ts). No git in this process: the folder's own script and file API. */
|
|
194
194
|
export function seedOf(data, agentId) {
|
|
195
195
|
return withAgentRepo(data, agentId, async () => {
|
|
196
196
|
const { workspace, end } = learningFolderOf(data, agentId);
|
|
197
197
|
try {
|
|
198
198
|
await learningInit(workspace);
|
|
199
|
-
|
|
199
|
+
const seed = await readLearning(workspace, async ({ snapshot, body }) => seedIn(snapshot?.main ?? null, snapshot, body));
|
|
200
|
+
return { ...seed, at: Date.now() };
|
|
200
201
|
}
|
|
201
202
|
finally {
|
|
202
203
|
await end();
|
package/dist/llms.txt
CHANGED
|
@@ -20,6 +20,7 @@ Auth: the API's routes take `Authorization: Bearer <token>`, all but `GET /v2/he
|
|
|
20
20
|
- `POST /v2/files?path=notes.md` with the file's bytes as the body (`curl --data-binary @notes.md`) uploads a file, answered `201` with the file, ready, its `file_id` under `file`. Name it in a run's `file_ids`, and the run finds it at `inputs/notes.md`. `GET /v2/files` lists the uploads, `DELETE /v2/files/{id}` removes one.
|
|
21
21
|
- `GET /v2/runs/{id}`: status (`queued`, `working`, `input_required`, `completed`, `failed`, `cancelled`, `paused`), `result` (`final_answer`, or a workflow's `output`), `error`, `cost` (`usd` is null while unknown, never $0), `path` (`agent`, `workflow`, `handed_back`; null until it ends). A run its learned workflow answered alone (`path` `workflow`), or ran first and handed back (`handed_back`), carries `workflow` (`name`, `digest`, and `repick`: `found` when its answer re-picked after a quote miss and found its quotes, `missed` when the re-pick missed too, else null), on the read and on every `GET /v2/runs` row; null on any other run.
|
|
22
22
|
- An agent that learns: a run its learned workflow served (`path` `workflow`) has the workflow's answer as `result.final_answer`; one handed back (`handed_back`) has the agent's. Its timeline is its `user_message`, the learned_workflow `tool_use` and `tool_result` (its nodes in `learned_nodes`; they write no `decision` rows), then `final_answer` and `usage`; a hand-back puts a `phase` row and the agent's own rows before them. Learning spends on the agent's own model route, key and cap: on `GET /v2/runs/{id}`, `cost.usd` is the run's own spend, the job's; `cost.learning_usd` what learning spent for it (its audit's shadow run and its share of its workflow revision's compile turn and check, spread over the revision's first `compile_after` served runs; a compile that kept no draft is on no run, in the bill's `unattributed_usd`), `cost.all_in_usd` both. A shadow run is in no run list and sends no webhook. `GET /v2/agents/{id}/learning/cost` has the agent's whole bill (`bill`).
|
|
23
|
+
- `DELETE /v2/agents/{id}/learning` resets what the agent learned (its corrections, learned workflows, their history, promotions and grades), on this server and on every Archil disk its runs used, keeping the agent and its runs: `204`, again on a second call; `409 learning_busy` (the runs in `details.runs`) while a run of its own is still going, nothing deleted; `503` while a disk that holds it can't be reached. Learning then reads only the runs after the reset (`compile_after` counts from it); a run that read the agent's learning before it learns nothing (`learning.reason` `reset`).
|
|
23
24
|
- `GET /v2/runs/{id}/timeline?after=0&limit=100` pages its steps; follow `next_after` until a page is `final` with `has_more` false.
|
|
24
25
|
- `GET /v2/runs/{id}/files`, then `GET /v2/runs/{id}/files/{path}`: what it wrote.
|
|
25
26
|
- `POST /v2/runs/{id}/messages` with `{content}` answers a run that asked a question (`input_required`), or follows up; `POST /v2/runs/{id}/cancel` ends one.
|
package/dist/mcp-gateway.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { McpCallResult, McpTool } from "@parcha/agentrun-kernel";
|
|
2
|
+
import { type MediaReader } from "./fal-price.js";
|
|
2
3
|
import { type Workspace } from "./workspace/index.js";
|
|
3
4
|
/** The longest answer the model reads whole; past it, the rest is filed in the run's work/ as lines read pages through
|
|
4
5
|
* (filed-answer.ts). Within the host's result bound (HOST_RESULT_MAX_CHARS), so every harness passes it whole. */
|
|
@@ -6,11 +7,13 @@ export declare const MCP_CLIP = 60000;
|
|
|
6
7
|
/** The longest one upstream call may take, and its first connection. */
|
|
7
8
|
export declare const CALL_MS = 120000;
|
|
8
9
|
export declare const CONNECT_MS = 30000;
|
|
9
|
-
/** Each pinned server: its URL then, the granted tools as it listed them,
|
|
10
|
+
/** Each pinned server: its URL then, the granted tools as it listed them, the prices its owner declared for them, and
|
|
11
|
+
* the meter that prices its other calls (`fal`: fal-price.ts). */
|
|
10
12
|
export type McpPin = Record<string, {
|
|
11
13
|
url: string;
|
|
12
14
|
tools: McpTool[];
|
|
13
15
|
prices?: Record<string, number>;
|
|
16
|
+
meter?: string;
|
|
14
17
|
}>;
|
|
15
18
|
/** Each granted server's URL now, and its credential's headers when that URL is the pinned one (the claim step's). */
|
|
16
19
|
export type McpCredentials = Record<string, {
|
|
@@ -20,14 +23,40 @@ export type McpCredentials = Record<string, {
|
|
|
20
23
|
unopened?: string; /** When its credential ends (ms): a token minted for the run's launch (a github_app server's), whose calls past it are refused unsent, in words. */
|
|
21
24
|
expires_at?: number;
|
|
22
25
|
}>;
|
|
23
|
-
/** The servers a save pins from: each one's URL, its tools as it lists them now,
|
|
26
|
+
/** The servers a save pins from: each one's URL, its tools as it lists them now, its owner's prices, and either its
|
|
27
|
+
* owner's toolkit for it or, for a pin carried over, the meter it was pinned with. */
|
|
24
28
|
export interface McpServerSource {
|
|
25
29
|
server(name: string): {
|
|
26
30
|
url: string;
|
|
27
31
|
tools: readonly McpTool[];
|
|
28
32
|
prices?: Readonly<Record<string, number>>;
|
|
33
|
+
toolkit?: string | null;
|
|
34
|
+
meter?: string;
|
|
29
35
|
} | undefined;
|
|
30
36
|
}
|
|
37
|
+
/** A meter's price for one call: what to hold before it leaves and why (`basis`), why it can't be priced
|
|
38
|
+
* (`unpriceable`, in words; the hold then covers only what could be), and what it charged, by the server's reply
|
|
39
|
+
* (none: no reply came back). Null: unknown. */
|
|
40
|
+
export type McpQuote = {
|
|
41
|
+
holdUsd: number;
|
|
42
|
+
basis: string;
|
|
43
|
+
unpriceable?: string;
|
|
44
|
+
chargeOf(reply: Record<string, unknown> | undefined): number | null;
|
|
45
|
+
};
|
|
46
|
+
/** A server's keys scoped to the run (fal's submission keys, fal-price.ts falKeys): `out`, a call's arguments as they
|
|
47
|
+
* leave; `back`, the text of its answer or error as the model reads it, each scoped key read as the agent's own. */
|
|
48
|
+
export type McpKeys = {
|
|
49
|
+
out(tool: string, args: Record<string, unknown>): Record<string, unknown>;
|
|
50
|
+
back(text: string): string;
|
|
51
|
+
};
|
|
52
|
+
/** A server that prices its own calls. `ask` calls one of the server's tools with the run's credential, unbooked and
|
|
53
|
+
* unrecorded, and answers its raw result (none: it failed). `keys`: the keys it scopes to the run, on every call of
|
|
54
|
+
* the server, billed or not. */
|
|
55
|
+
export interface McpMeter {
|
|
56
|
+
keys?: McpKeys;
|
|
57
|
+
bills(tool: string): boolean;
|
|
58
|
+
quote(tool: string, args: Record<string, unknown>, ask: (tool: string, args: Record<string, unknown>) => Promise<Record<string, unknown> | undefined>, signal: AbortSignal): Promise<McpQuote>;
|
|
59
|
+
}
|
|
31
60
|
/** The most schema nodes, and the deepest nesting, a tool's declarations are checked through: past either the tool is
|
|
32
61
|
* refused, never walked further. */
|
|
33
62
|
export declare const HEADER_SCAN_NODES = 10000;
|
|
@@ -75,6 +104,14 @@ export interface McpGatewayOptions {
|
|
|
75
104
|
* throws (a store read failed, the call was stopped) refuses the call unsent too, in words, and the run goes on. Only
|
|
76
105
|
* the runner's MCP claim step passes it: a github_app token whose App was disconnected or replaced. */
|
|
77
106
|
beforeCall?: (server: string, signal: AbortSignal) => Promise<void>;
|
|
107
|
+
/** Whether the run has a spend cap: under one, a metered call that cannot be priced is refused unsent. None: it has. */
|
|
108
|
+
capped?: () => boolean;
|
|
109
|
+
/** Reads a metered job's input media, for one priced by its length. None: such a job cannot be priced. */
|
|
110
|
+
media?: MediaReader;
|
|
111
|
+
/** For tests: the clock a meter's prices are kept by. */
|
|
112
|
+
now?: () => number;
|
|
113
|
+
/** The run's id: what a meter scopes a server's keys to (McpMeter.keys). None: no key is scoped. */
|
|
114
|
+
run?: string;
|
|
78
115
|
}
|
|
79
116
|
/** A run's journal commit. */
|
|
80
117
|
export type McpCommit = <T>(change: (tx: unknown) => Promise<T>) => Promise<T>;
|
package/dist/mcp-gateway.js
CHANGED
|
@@ -15,9 +15,14 @@
|
|
|
15
15
|
// - A tool its owner priced (the server's `prices`) is held at that price on the run's one ledger and booked as the
|
|
16
16
|
// run's tool cost (paid-calls.ts paidStep): refused unsent when the cap cannot take it, or when no ledger books it. A
|
|
17
17
|
// tool with no price runs, marked "not metered" on the run's root (TOOL_UNMETERED), outside the run's cost.
|
|
18
|
+
// - A server with a meter (McpMeter; fal's, fal-price.ts) prices each billed call itself before it leaves: held and
|
|
19
|
+
// booked the same way, at the meter's quote. A call it cannot price is refused unsent under a spend cap, and with
|
|
20
|
+
// none runs booked as an unknown charge. A meter's keys (McpMeter.keys; fal's submission keys on fal's own host) are
|
|
21
|
+
// scoped to the run on every call of its server as it leaves, and read back as the agent's own in what the model reads.
|
|
18
22
|
import { randomUUID } from "node:crypto";
|
|
19
23
|
import { ROOT_CONVERSATION_ID } from "@earendil-works/pi-durable";
|
|
20
24
|
import { TOOL_UNMETERED } from "./timeline/index.js";
|
|
25
|
+
import { meterFor, meterNamed } from "./fal-price.js";
|
|
21
26
|
import { filedAnswer } from "./filed-answer.js";
|
|
22
27
|
import { paidLedger, paidStep } from "./paid-calls.js";
|
|
23
28
|
import { metaToolOf } from "./mcp-meta.js";
|
|
@@ -123,8 +128,9 @@ export function mcpPinOf(names, source) {
|
|
|
123
128
|
const declared = mcpHeaderProblem(tool.inputSchema);
|
|
124
129
|
if (declared)
|
|
125
130
|
throw new Error(`The MCP tool ${name} declares a header it cannot send: ${declared}.`);
|
|
131
|
+
const meter = saved.meter ?? meterNamed(saved.url, saved.toolkit);
|
|
126
132
|
if (!Object.hasOwn(pin, parsed.server))
|
|
127
|
-
pin[parsed.server] = { url: saved.url, tools: [] };
|
|
133
|
+
pin[parsed.server] = { url: saved.url, tools: [], ...(meter ? { meter } : {}) };
|
|
128
134
|
const pinned = pin[parsed.server];
|
|
129
135
|
pinned.tools.push(tool);
|
|
130
136
|
const price = saved.prices && Object.hasOwn(saved.prices, tool.name) ? saved.prices[tool.name] : undefined;
|
|
@@ -188,6 +194,7 @@ export function mcpGateway(options) {
|
|
|
188
194
|
* another account connects again, so no session made as one account carries another's calls. */
|
|
189
195
|
const clients = new Map();
|
|
190
196
|
const pins = new Map(Object.entries(options.pin));
|
|
197
|
+
const meters = new Map([...pins].map(([server, pinned]) => [server, meterFor(pinned, { media: options.media, now: options.now, run: options.run })]));
|
|
191
198
|
const credentials = { get: (server) => (Object.hasOwn(options.credentials, server) ? options.credentials[server] : undefined) };
|
|
192
199
|
/** Each server's last HTTP status, which tells a refused credential from another failure. (An address refusal is never
|
|
193
200
|
* kept here: it is read from each call's own error, since calls to one server run at once.) */
|
|
@@ -314,12 +321,15 @@ export function mcpGateway(options) {
|
|
|
314
321
|
/** The call itself, to a server whose address is the pinned one. A gate's decision or a paid call's hold goes around
|
|
315
322
|
* it, in `call` below: the one place every harness's call passes before anything is sent. */
|
|
316
323
|
/** `sending`, when given, runs once the server is connected and right before the call leaves: its refusal (a text)
|
|
317
|
-
* keeps the call from leaving. A call that never got that far (a failed connect, a stop) never ran it.
|
|
318
|
-
|
|
324
|
+
* keeps the call from leaving. A call that never got that far (a failed connect, a stop) never ran it. `outcome`
|
|
325
|
+
* takes the server's raw result, when one came back. */
|
|
326
|
+
const forward = async (server, url, headers, tool, args, signal, sending, outcome = {}, back) => {
|
|
319
327
|
let connected = false;
|
|
320
328
|
// Read at each use: a token refreshed while the call is out, and sent, is scrubbed with the one it took.
|
|
321
329
|
const held = callSet(server);
|
|
322
|
-
const
|
|
330
|
+
const scrubbed = scrubberOf(server, hold(server, headers), held.values);
|
|
331
|
+
// Every text the model reads of the call (its answer, a filed one, an error), its keys read as the agent's own.
|
|
332
|
+
const clean = back ? (text) => back(scrubbed(text)) : scrubbed;
|
|
323
333
|
hold(server, queryOf(server));
|
|
324
334
|
try {
|
|
325
335
|
const upstream = await until(connect(server, url, headers), signal);
|
|
@@ -332,7 +342,9 @@ export function mcpGateway(options) {
|
|
|
332
342
|
// The pinned definition, so the SDK mirrors its x-mcp-header parameters into Mcp-Param-* headers on a 2026-07-28
|
|
333
343
|
// server, as its own listing would have it do.
|
|
334
344
|
const result = (await upstream.callTool({ name: tool.name, arguments: args }, { signal, timeout: options.bounds?.callMs ?? CALL_MS, toolDefinition: { name: tool.name, inputSchema: tool.inputSchema } }));
|
|
335
|
-
|
|
345
|
+
outcome.raw = result;
|
|
346
|
+
// A meter's own read (`quiet`): its raw result is all that is wanted, nothing filed for a model to read.
|
|
347
|
+
return said(outcome.quiet ? "" : await textOf(server, tool.name, result, clean), result.isError === true);
|
|
336
348
|
}
|
|
337
349
|
catch (error) {
|
|
338
350
|
if (signal.aborted)
|
|
@@ -405,23 +417,29 @@ export function mcpGateway(options) {
|
|
|
405
417
|
const listed = pinnedTools.find((t) => t.name === tool);
|
|
406
418
|
// Whether the call left for the server: only then is it booked, at its price or as not metered.
|
|
407
419
|
let went = false;
|
|
420
|
+
let raw;
|
|
421
|
+
// The server's keys scoped to the run as the call leaves (fal's submission keys): its price is read from the call as
|
|
422
|
+
// the agent made it.
|
|
423
|
+
const keys = meters.get(server)?.keys;
|
|
408
424
|
const call = async (mark = async () => undefined) => {
|
|
409
425
|
const outcome = {};
|
|
410
|
-
const result = await forward(server, url, credentials.get(server)?.headers ?? {}, listed, args, signal, async () => {
|
|
426
|
+
const result = await forward(server, url, credentials.get(server)?.headers ?? {}, listed, keys ? keys.out(tool, args) : args, signal, async () => {
|
|
411
427
|
const refused = await mark();
|
|
412
428
|
went = refused === undefined;
|
|
413
429
|
return refused;
|
|
414
|
-
}, outcome);
|
|
430
|
+
}, outcome, keys?.back);
|
|
415
431
|
// The request's own address check refused it after the mark: it never left, so nothing is booked.
|
|
416
432
|
if (outcome.unsent)
|
|
417
433
|
went = false;
|
|
434
|
+
raw = outcome.raw;
|
|
418
435
|
return result;
|
|
419
436
|
};
|
|
420
437
|
const name = mcpToolName(server, tool);
|
|
421
438
|
const label = { tool: name, charged_by: server, call: callId };
|
|
422
439
|
const commit = options.commit?.();
|
|
423
440
|
const price = prices && Object.hasOwn(prices, tool) ? prices[tool] : undefined;
|
|
424
|
-
|
|
441
|
+
const meter = price === undefined && meters.get(server)?.bills(tool) ? meters.get(server) : undefined;
|
|
442
|
+
if (price === undefined && !meter) {
|
|
425
443
|
const result = await call();
|
|
426
444
|
if (commit && went)
|
|
427
445
|
await record(commit, TOOL_UNMETERED, label).catch(() => undefined);
|
|
@@ -430,11 +448,24 @@ export function mcpGateway(options) {
|
|
|
430
448
|
// On the runner's one ledger (paid-calls.ts Ledger.paid): held there at its price before it leaves and settled at its
|
|
431
449
|
// price once it left (nothing when it never left), in place of this process's spend and the root's marks.
|
|
432
450
|
const onLedger = paidLedger();
|
|
433
|
-
if (onLedger)
|
|
451
|
+
if (!onLedger)
|
|
452
|
+
return said(`${name} was not sent: this call has no run to book its price under.`, true);
|
|
453
|
+
if (price !== undefined) {
|
|
434
454
|
const stepped = await paidStep(onLedger, callId ?? name, { name, holdUsd: price, basis: `the price its owner set for ${name}`, by: label.charged_by }, () => call(), () => (went ? price : 0));
|
|
435
455
|
return "refused" in stepped ? said(stepped.refused, true) : stepped.result;
|
|
436
456
|
}
|
|
437
|
-
|
|
457
|
+
// The meter reads its prices from the server itself: the run's credential, no record, no booking.
|
|
458
|
+
const ask = async (asked, input) => {
|
|
459
|
+
const outcome = { quiet: true };
|
|
460
|
+
await forward(server, url, credentials.get(server)?.headers ?? {}, { name: asked, inputSchema: { type: "object" } }, input, signal, undefined, outcome);
|
|
461
|
+
return outcome.raw;
|
|
462
|
+
};
|
|
463
|
+
const quote = await meter.quote(tool, args, ask, signal);
|
|
464
|
+
if (quote.unpriceable && (options.capped?.() ?? true))
|
|
465
|
+
return said(`${name} was not sent: ${quote.unpriceable}, so a run with a spend cap can't send it. Nothing was sent.`, true);
|
|
466
|
+
// Settled by the server's reply; one that left and brought none back is an unknown charge.
|
|
467
|
+
const stepped = await paidStep(onLedger, callId ?? name, { name, holdUsd: quote.holdUsd, basis: quote.basis, by: label.charged_by }, () => call(), () => (went ? quote.chargeOf(raw) : 0));
|
|
468
|
+
return "refused" in stepped ? said(stepped.refused, true) : stepped.result;
|
|
438
469
|
};
|
|
439
470
|
const gateway = {
|
|
440
471
|
servers: [...pins].map(([name, pinned]) => ({ name, tools: pinned.tools })),
|
package/dist/mcp-pin-first.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cliLearningOf } from "./store/agent-body.js";
|
|
1
2
|
// A revision saved with no MCP pin (before pins were made at save, or an inline agent's version stored then) is pinned
|
|
2
3
|
// once, at its first run, as a save would pin it: each granted server still saved here listed once
|
|
3
4
|
// (McpServersOverStore.pinListing, the save's own listing), the pin made of those listings (mcp-gateway.ts mcpPinOf), and
|
|
@@ -22,7 +23,7 @@ import { GITHUB_UNUSABLE } from "./runner/mcp-github.js";
|
|
|
22
23
|
export async function pinFirst(revision, deps) {
|
|
23
24
|
// Native source MCP names and SDK callback tools belong to their source, never this server's managed registry.
|
|
24
25
|
// Their runnable guards must refuse managed execution without listing or pinning those names first.
|
|
25
|
-
if (nativeOf(revision.body) || sdkOf(revision.body))
|
|
26
|
+
if (nativeOf(revision.body) || sdkOf(revision.body) || cliLearningOf(revision.body))
|
|
26
27
|
return undefined;
|
|
27
28
|
if (revision.mcpPin !== undefined && revision.mcpPin !== null)
|
|
28
29
|
return revision.mcpPin;
|
|
@@ -53,7 +54,7 @@ export async function pinFirst(revision, deps) {
|
|
|
53
54
|
}
|
|
54
55
|
let pin;
|
|
55
56
|
try {
|
|
56
|
-
pin = mcpPinOf(granted, { server: (name) => { const listing = listings.get(name); return listing && { url: listing.url, tools: listing.tools.map(boundTool), prices: listing.prices }; } });
|
|
57
|
+
pin = mcpPinOf(granted, { server: (name) => { const listing = listings.get(name); return listing && { url: listing.url, tools: listing.tools.map(boundTool), prices: listing.prices, toolkit: listing.toolkit }; } });
|
|
57
58
|
}
|
|
58
59
|
catch {
|
|
59
60
|
return undefined;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import net from "node:net";
|
|
1
2
|
import type { Settings } from "./store/index.js";
|
|
2
3
|
/** The store.settings key the list lives under: `{ hosts: string[] }`, each a URL's hostname as hostKey (mcp-meta.ts)
|
|
3
4
|
* keeps it: lowercase, every final dot gone, an IPv6 literal in its URL form (`[fd00::1]`). */
|
|
@@ -6,19 +7,39 @@ export declare const MCP_PRIVATE_HOSTS_KEY = "mcp.private_oauth_hosts";
|
|
|
6
7
|
export declare const MAX_PRIVATE_HOSTS = 32;
|
|
7
8
|
/** Where an owner allows a host, as a refusal names it. */
|
|
8
9
|
export declare const PRIVATE_HOSTS_PAGE = "Settings \u2192 MCP servers";
|
|
10
|
+
/** The operator's bound on the owner's private hosts, read from the environment of the process that dials. */
|
|
11
|
+
export declare const PRIVATE_HOSTS_ENV = "AGENTRUN_PRIVATE_HOSTS";
|
|
12
|
+
/** What the operator set: the owner's list as it is (`owner`, unset), no owner's list (`none`, also for a value that is
|
|
13
|
+
* neither: `invalid` names why), or the ranges a listed host must also resolve in. */
|
|
14
|
+
export type PrivateHostsBound = {
|
|
15
|
+
readonly mode: "owner";
|
|
16
|
+
} | {
|
|
17
|
+
readonly mode: "none";
|
|
18
|
+
readonly invalid?: string;
|
|
19
|
+
} | {
|
|
20
|
+
readonly mode: "ranges";
|
|
21
|
+
readonly ranges: readonly string[];
|
|
22
|
+
readonly list: net.BlockList;
|
|
23
|
+
};
|
|
24
|
+
/** The operator's bound as `env` sets it (PRIVATE_HOSTS_ENV). */
|
|
25
|
+
export declare function privateHostsBound(env?: NodeJS.ProcessEnv): PrivateHostsBound;
|
|
26
|
+
/** The bound as the capabilities' switches show it: unset (null), `none`, or the ranges; a value read as none says so. */
|
|
27
|
+
export declare const privateHostsSwitchValue: (env?: NodeJS.ProcessEnv) => string | null;
|
|
28
|
+
/** Why an owner's list cannot be saved under the operator's bound, or undefined when it can. */
|
|
29
|
+
export declare const ownerListRefused: (env?: NodeJS.ProcessEnv) => string | undefined;
|
|
9
30
|
/** Whether `address` is in a range a listed host may resolve to (ALLOWABLE), and not a cloud metadata service's. */
|
|
10
31
|
export declare const inAllowableRange: (address: string) => boolean;
|
|
11
32
|
/** Every address on this machine's network interfaces: never an authorization server's, listed or not. */
|
|
12
33
|
export declare function localAddresses(): string[];
|
|
13
34
|
/** Whether a private address `address`, which a listed host resolves to, may be dialled: only in ALLOWABLE, and never one
|
|
14
35
|
* of `own` (this machine's addresses, and this server's own; an IPv4-mapped form is the same address). */
|
|
15
|
-
export declare function privateAllowed(address: string, own: readonly string[]): boolean;
|
|
36
|
+
export declare function privateAllowed(address: string, own: readonly string[], env?: NodeJS.ProcessEnv): boolean;
|
|
16
37
|
/** The private-network hosts the owner allows an authorization server, or an MCP server itself (mcp-oauth.ts
|
|
17
38
|
* mcpServerAt), on: read from the store at each use, so a host removed is refused from the next request on. The one read
|
|
18
39
|
* of the setting, for the server's sign-in, preview, save and refresh and the runner's token refresh and calls alike. */
|
|
19
40
|
export declare function privateOAuthHosts(store: {
|
|
20
41
|
settings: Pick<Settings, "get">;
|
|
21
|
-
}): Promise<string[]>;
|
|
42
|
+
}, env?: NodeJS.ProcessEnv): Promise<string[]>;
|
|
22
43
|
/** The store.settings key a server keeps its own addresses under at each start (Serve.own: where it listens, and its
|
|
23
44
|
* public address when that is on loopback): `{ own: string[] }`. Addresses alone, never its loopback opt-in, which each
|
|
24
45
|
* process reads from its own environment: a runner in its own process with its own opt-in reads them so its runs never
|
|
@@ -38,7 +59,7 @@ export declare function savePrivateOAuthHosts(store: {
|
|
|
38
59
|
}, hosts: readonly string[]): Promise<void>;
|
|
39
60
|
/** A PUT body `{ hosts }` checked: the list as kept (each entry hostKey'd, the first of any repeat kept), or 422 with
|
|
40
61
|
* every entry that is not a host name or an IP address on a private network, in words. */
|
|
41
|
-
export declare function checkPrivateHosts(body: Record<string, unknown
|
|
62
|
+
export declare function checkPrivateHosts(body: Record<string, unknown>, env?: NodeJS.ProcessEnv): string[];
|
|
42
63
|
/** What the route reads and writes: the store-backed registry (mcp-servers-store.ts McpServersOverStore). */
|
|
43
64
|
export type PrivateHostsRegistry = {
|
|
44
65
|
privateHosts(): Promise<{
|