@parcha/agentrun-serve 0.1.0-beta.52 → 0.1.0-beta.53
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 +122 -1
- package/README.md +213 -24
- package/dist/agent-delete.d.ts +5 -6
- package/dist/agent-delete.js +30 -67
- package/dist/agent-package.d.ts +71 -0
- package/dist/agent-package.js +345 -0
- package/dist/agents-store.d.ts +26 -20
- package/dist/agents-store.js +41 -33
- package/dist/api-only.d.ts +26 -6
- package/dist/api-only.js +35 -13
- package/dist/claude-login.d.ts +1 -0
- package/dist/claude-login.js +1 -1
- package/dist/cli-learning/cli.d.ts +3 -0
- package/dist/cli-learning/cli.js +17 -3
- package/dist/cli-learning/connections.js +3 -2
- package/dist/cli-learning/schema.js +2 -1
- package/dist/cli.js +70 -7
- package/dist/composio-preset.d.ts +2 -1
- package/dist/composio-preset.js +4 -3
- package/dist/composio-triggers.d.ts +4 -0
- package/dist/composio-triggers.js +4 -1
- package/dist/computer-needs.d.ts +32 -0
- package/dist/computer-needs.js +80 -0
- package/dist/computer-setting.d.ts +1 -0
- package/dist/computer-setting.js +8 -1
- package/dist/console/assets/index-BFgyZzF_.js +734 -0
- package/dist/console/assets/index-DbilxljH.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/console-links.d.ts +1 -0
- package/dist/console-links.js +2 -0
- package/dist/env-switch.d.ts +14 -0
- package/dist/env-switch.js +23 -0
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +3 -0
- package/dist/eval-previous.js +4 -3
- package/dist/fal-price.js +10 -13
- package/dist/handed-secret.d.ts +4 -0
- package/dist/handed-secret.js +10 -0
- package/dist/harness-archive.d.ts +15 -5
- package/dist/harness-archive.js +71 -34
- package/dist/harness-check.d.ts +2 -1
- package/dist/harness-check.js +36 -95
- package/dist/harness-egress.d.ts +23 -0
- package/dist/harness-egress.js +69 -0
- package/dist/harness-recipe-entry.d.ts +71 -0
- package/dist/harness-recipe-entry.js +184 -0
- package/dist/harness-recipe.d.ts +73 -6
- package/dist/harness-recipe.js +185 -31
- package/dist/harness-sign-in.d.ts +29 -0
- package/dist/harness-sign-in.js +122 -0
- package/dist/harness-table.d.ts +21 -0
- package/dist/harness-table.js +58 -3
- package/dist/harness-zip.d.ts +6 -0
- package/dist/harness-zip.js +203 -0
- package/dist/ids.d.ts +11 -0
- package/dist/ids.js +36 -0
- package/dist/learn/commit.d.ts +14 -1
- package/dist/learn/commit.js +156 -1
- package/dist/learn/compile.js +22 -8
- package/dist/learn/folder.d.ts +5 -0
- package/dist/learn/folder.js +10 -1
- package/dist/learn/learning.sh +15 -3
- package/dist/learn/promote.d.ts +2 -2
- package/dist/learn/secrets.js +30 -7
- package/dist/learning-routes.d.ts +23 -11
- package/dist/learning-routes.js +110 -20
- package/dist/learning.d.ts +9 -6
- package/dist/learning.js +9 -4
- package/dist/mcp-gateway.js +15 -2
- package/dist/mcp-probe.d.ts +3 -0
- package/dist/mcp-probe.js +7 -2
- package/dist/mcp-resources.d.ts +44 -0
- package/dist/mcp-resources.js +100 -0
- package/dist/mcp-servers-store.d.ts +16 -12
- package/dist/model.d.ts +4 -4
- package/dist/model.js +6 -6
- package/dist/native/cli.js +9 -2
- package/dist/native/connection.d.ts +2 -2
- package/dist/native/connection.js +5 -2
- package/dist/native/discovery.d.ts +2 -0
- package/dist/native/discovery.js +10 -6
- package/dist/native/remote-worker-bridge.js +36 -4
- package/dist/native/remote.d.ts +18 -1
- package/dist/native/remote.js +126 -37
- package/dist/native/schema.d.ts +6 -0
- package/dist/native/schema.js +31 -6
- package/dist/native/source-account.d.ts +13 -0
- package/dist/native/source-account.js +114 -0
- package/dist/native/source-capture-hooks.d.ts +5 -0
- package/dist/native/source-capture-hooks.js +102 -0
- package/dist/native/source-mcp-selection.d.ts +13 -0
- package/dist/native/source-mcp-selection.js +72 -0
- package/dist/native/source-runtime-tools.d.ts +19 -0
- package/dist/native/source-runtime-tools.js +103 -0
- package/dist/native/source-snapshot-types.d.ts +138 -0
- package/dist/native/source-snapshot-types.js +12 -0
- package/dist/native/source-snapshot.d.ts +8 -0
- package/dist/native/source-snapshot.js +1201 -0
- package/dist/observation/learning.js +3 -2
- package/dist/openapi.js +167 -36
- package/dist/print.js +1 -1
- package/dist/purged-workflow.d.ts +12 -0
- package/dist/purged-workflow.js +43 -0
- package/dist/run-keys.d.ts +46 -0
- package/dist/run-keys.js +109 -0
- package/dist/run-launch.d.ts +16 -1
- package/dist/run-launch.js +30 -1
- package/dist/run-learning-served.d.ts +2 -2
- package/dist/run-model-key.d.ts +2 -2
- package/dist/run-model-key.js +5 -5
- package/dist/run-secrets.d.ts +2 -0
- package/dist/run-secrets.js +7 -4
- package/dist/run-view.d.ts +17 -0
- package/dist/run-view.js +25 -1
- package/dist/runner/adapter-loop.d.ts +11 -3
- package/dist/runner/adapter-loop.js +138 -22
- package/dist/runner/adapter-run-spec.js +25 -12
- package/dist/runner/audit.d.ts +2 -0
- package/dist/runner/audit.js +9 -1
- package/dist/runner/beside.js +2 -1
- package/dist/runner/claim-secrets.d.ts +29 -3
- package/dist/runner/claim-secrets.js +38 -4
- package/dist/runner/cli-run-spec.js +23 -12
- package/dist/runner/harness-loops.d.ts +6 -0
- package/dist/runner/harness-loops.js +11 -1
- package/dist/runner/held.d.ts +4 -0
- package/dist/runner/journal-kinds.d.ts +2 -0
- package/dist/runner/learned-first.d.ts +2 -0
- package/dist/runner/learned-first.js +1 -1
- package/dist/runner/learning-end.d.ts +7 -2
- package/dist/runner/learning-end.js +22 -10
- package/dist/runner/learning-seed.d.ts +9 -3
- package/dist/runner/learning-seed.js +2 -2
- package/dist/runner/main.d.ts +1 -0
- package/dist/runner/main.js +20 -13
- package/dist/runner/mcp-claim.js +6 -1
- package/dist/runner/mcp-run.d.ts +10 -7
- package/dist/runner/mcp-run.js +3 -5
- package/dist/runner/monid-run.js +9 -11
- package/dist/runner/project-checkpoint.d.ts +35 -0
- package/dist/runner/project-checkpoint.js +116 -0
- package/dist/runner/project-drain.d.ts +8 -0
- package/dist/runner/project-drain.js +25 -0
- package/dist/runner/project-host.d.ts +10 -0
- package/dist/runner/project-host.js +52 -0
- package/dist/runner/promotion.d.ts +14 -6
- package/dist/runner/promotion.js +98 -41
- package/dist/runner/redact.d.ts +13 -0
- package/dist/runner/redact.js +67 -1
- package/dist/runner/run-channel.d.ts +9 -0
- package/dist/runner/run-channel.js +15 -2
- package/dist/runner/run-child-host.d.ts +8 -4
- package/dist/runner/run-child-host.js +171 -41
- package/dist/runner/run-errors.js +2 -2
- package/dist/runner/runner-check.js +5 -2
- package/dist/runner/runner.js +26 -9
- package/dist/runner/store-secrets.js +7 -3
- package/dist/runner/workflow-first.d.ts +4 -0
- package/dist/runner/workflow-first.js +13 -0
- package/dist/runner/workflow-loop.d.ts +2 -1
- package/dist/runner/workflow-loop.js +2 -1
- package/dist/runner/workflow-spec.js +1 -1
- package/dist/runs-over-store.d.ts +34 -6
- package/dist/runs-over-store.js +133 -25
- package/dist/sdk/runs.d.ts +11 -1
- package/dist/sdk/runs.js +117 -26
- package/dist/sdk/schema.js +12 -2
- package/dist/sdk/workflows.d.ts +1 -1
- package/dist/server.d.ts +68 -32
- package/dist/server.js +600 -110
- package/dist/skills/agentrun-serve/SKILL.md +2 -1
- package/dist/source-sync/adoption.d.ts +15 -0
- package/dist/source-sync/adoption.js +36 -0
- package/dist/source-sync/auxiliary.d.ts +23 -0
- package/dist/source-sync/auxiliary.js +64 -0
- package/dist/source-sync/capture-git.d.ts +5 -0
- package/dist/source-sync/capture-git.js +272 -0
- package/dist/source-sync/capture-project.d.ts +53 -0
- package/dist/source-sync/capture-project.js +206 -0
- package/dist/source-sync/capture-refusal.d.ts +7 -0
- package/dist/source-sync/capture-refusal.js +16 -0
- package/dist/source-sync/capture.d.ts +11 -0
- package/dist/source-sync/capture.js +22 -0
- package/dist/source-sync/checkpoint-gate.d.ts +21 -0
- package/dist/source-sync/checkpoint-gate.js +52 -0
- package/dist/source-sync/checkpoints.d.ts +66 -0
- package/dist/source-sync/checkpoints.js +168 -0
- package/dist/source-sync/claim.d.ts +11 -0
- package/dist/source-sync/claim.js +48 -0
- package/dist/source-sync/destination.d.ts +47 -0
- package/dist/source-sync/destination.js +239 -0
- package/dist/source-sync/io.d.ts +15 -0
- package/dist/source-sync/io.js +52 -0
- package/dist/source-sync/local.d.ts +43 -0
- package/dist/source-sync/local.js +229 -0
- package/dist/source-sync/prepare.d.ts +59 -0
- package/dist/source-sync/prepare.js +93 -0
- package/dist/source-sync/projects.d.ts +61 -0
- package/dist/source-sync/projects.js +130 -0
- package/dist/source-sync/registry.d.ts +95 -0
- package/dist/source-sync/registry.js +226 -0
- package/dist/source-sync/service.d.ts +33 -0
- package/dist/source-sync/service.js +118 -0
- package/dist/source-sync/skill-edits.d.ts +12 -0
- package/dist/source-sync/skill-edits.js +122 -0
- package/dist/source-sync/snapshots.d.ts +48 -0
- package/dist/source-sync/snapshots.js +448 -0
- package/dist/source-sync/types.d.ts +9 -0
- package/dist/source-sync/types.js +14 -0
- package/dist/source-sync/workspace.d.ts +66 -0
- package/dist/source-sync/workspace.js +273 -0
- package/dist/store/agent-body.d.ts +4 -0
- package/dist/store/agent-body.js +16 -5
- package/dist/store/index.d.ts +2 -2
- package/dist/store/index.js +1 -1
- package/dist/store/schema.d.ts +2 -1
- package/dist/store/schema.js +2 -1
- package/dist/store/seal.d.ts +5 -1
- package/dist/store/seal.js +12 -3
- package/dist/store/sqlite/project-checkpoints.d.ts +11 -0
- package/dist/store/sqlite/project-checkpoints.js +70 -0
- package/dist/store/sqlite/run-launch.d.ts +4 -12
- package/dist/store/sqlite/run-launch.js +5 -19
- package/dist/store/sqlite/store.js +224 -170
- package/dist/store/store.d.ts +116 -39
- package/dist/store/store.js +14 -1
- package/dist/store-reads/learning.d.ts +2 -2
- package/dist/store-view.d.ts +3 -1
- package/dist/store-view.js +17 -5
- package/dist/template-import.d.ts +10 -2
- package/dist/template-import.js +116 -13
- package/dist/templates.d.ts +26 -5
- package/dist/templates.js +62 -10
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.js +1 -1
- package/dist/timeline/timeline.d.ts +5 -0
- package/dist/timeline/timeline.js +8 -0
- package/dist/tool-plane.d.ts +12 -12
- package/dist/tools.d.ts +8 -8
- package/dist/trigger-launch-hook.d.ts +47 -0
- package/dist/trigger-launch-hook.js +90 -0
- package/dist/trigger-targets.d.ts +14 -1
- package/dist/trigger-targets.js +20 -0
- package/dist/triggers.d.ts +54 -5
- package/dist/triggers.js +143 -8
- package/dist/uploads-archil.js +1 -1
- package/dist/uploads.js +1 -1
- package/dist/validate.js +1 -2
- package/dist/workspace/computer-for-claim.d.ts +2 -0
- package/dist/workspace/computer-for-claim.js +43 -0
- package/dist/workspace/paths.d.ts +6 -0
- package/dist/workspace/paths.js +6 -0
- package/dist/workspace/resolve-computer.d.ts +1 -0
- package/dist/workspace/run-tools.d.ts +2 -0
- package/dist/workspace/run-tools.js +8 -0
- package/launcher/agentrun-serve +1 -1
- package/package.json +10 -10
- package/skills/agentrun-serve/SKILL.md +2 -1
- package/templates/README.md +5 -2
- package/dist/console/assets/index-B-D0EM3j.js +0 -733
- package/dist/console/assets/index-DlQepP2J.css +0 -1
package/dist/learning-routes.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
2
|
import { CORRECTIONS_CAP, draftProblems, frontKeys, isHeld, LEARNED_KEYS, learnedPlace, learnedStatus, printable, released, restorable, restoredLearned, retiredLearned, UNCLEAR_FRONT, unclearFront, unstorable, WORKFLOW_NAME, WORKFLOWS_DIR } from "./learn/index.js";
|
|
3
3
|
import { readmeProblems } from "./learn/workflow-draft.js";
|
|
4
|
-
import { putCorrections, readCorrections } from "./learn/commit.js";
|
|
5
|
-
import { blobOid, FolderBusy, FolderRefused, FolderTooLarge, FolderUnreadable, readLearning, PATCH_CAP, readRunDiff, readSnapshot, runDiffKey, textsAt, writeLearning } from "./learn/folder.js";
|
|
4
|
+
import { forgetRunText, putCorrections, readCorrections } from "./learn/commit.js";
|
|
5
|
+
import { blobOid, FolderBusy, FolderRefused, FolderTooLarge, FolderUnreadable, readLearning, PATCH_CAP, readRunDiff, readSnapshot, runDiffKey, snapshotAt, textsAt, writeLearning } from "./learn/folder.js";
|
|
6
|
+
import { purgedRun, purgedWorkflows } from "./purged-workflow.js";
|
|
6
7
|
import { ApiError, invalid } from "./errors.js";
|
|
7
8
|
import { detailShown, filesView, learningFolderOf, reasonShown, withAgentRepo } from "./learning.js";
|
|
8
9
|
import { isObject } from "./validate.js";
|
|
@@ -49,6 +50,30 @@ function routeFailure(error) {
|
|
|
49
50
|
return error.link ? new Error(error.message) : new ApiError("unavailable", error.message);
|
|
50
51
|
return error;
|
|
51
52
|
}
|
|
53
|
+
/** Whether a change of `snapshot`'s log is served: none by a purged run (a run's purge, agent-delete.ts purgeRun), so
|
|
54
|
+
* the folder's history reads serve none of its merges. Asked at each read, so a log the folder rebuilds names them and
|
|
55
|
+
* still serves none. */
|
|
56
|
+
async function servedIn(snapshot, purgedOf) {
|
|
57
|
+
const runs = [...new Set(snapshot.log.flatMap(({ by }) => ("run" in by ? [by.run] : [])))];
|
|
58
|
+
const purged = runs.length ? await purgedOf(runs) : new Set();
|
|
59
|
+
return ({ by }) => !("run" in by && purged.has(by.run));
|
|
60
|
+
}
|
|
61
|
+
/** Run `runId`'s text taken off agent `agentId`'s learning folder's current files at its purge (learn/commit.ts
|
|
62
|
+
* forgetRunText), in the agent's queue, as every write of the folder is. */
|
|
63
|
+
export async function forgetRunLearning(data, agentId, runId) {
|
|
64
|
+
await withAgentRepo(data, agentId, async () => {
|
|
65
|
+
const { workspace: ws, end } = learningFolderOf(data, agentId);
|
|
66
|
+
try {
|
|
67
|
+
await forgetRunText(ws, runId);
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
throw routeFailure(error);
|
|
71
|
+
}
|
|
72
|
+
finally {
|
|
73
|
+
await end();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
52
77
|
/** A read of agent `agentId`'s folder: `read` over its snapshot and the bodies it names (learn/folder.ts readLearning),
|
|
53
78
|
* `none` for a folder with no main yet; the folder ended after. */
|
|
54
79
|
async function reading(data, agentId, read, none) {
|
|
@@ -110,7 +135,18 @@ function pageAt(agentId, snapshot, at) {
|
|
|
110
135
|
return kept ? { snapshot: kept, offset: at.offset } : { snapshot, offset: 0 };
|
|
111
136
|
}
|
|
112
137
|
/** A page of the changes on main, newest first, cut of the host's secrets. */
|
|
113
|
-
|
|
138
|
+
/** `served` changes only: the offset and the cursor count the log as the snapshot keeps it, so a run purged between two
|
|
139
|
+
* pages moves no change the next page starts at. */
|
|
140
|
+
function logPage(snapshot, offset, limit, shown, served) {
|
|
141
|
+
const { log } = snapshot;
|
|
142
|
+
const items = [];
|
|
143
|
+
let at = offset;
|
|
144
|
+
for (; at < log.length && items.length < limit; at++)
|
|
145
|
+
if (served(log[at]))
|
|
146
|
+
items.push(changeView(log[at], shown));
|
|
147
|
+
const more = log.slice(at).some(served);
|
|
148
|
+
return { items, total: log.filter(served).length, next_cursor: more ? cursorOf(snapshot.main, at) : null };
|
|
149
|
+
}
|
|
114
150
|
/** For tests: how many times a folder's statuses were read (once per commit of its main). */
|
|
115
151
|
export const pendingTestHooks = { reads: 0 };
|
|
116
152
|
/** By agent: its folder's main commit and each draft's status there. */
|
|
@@ -146,21 +182,23 @@ export async function pendingCount(data, agentId) {
|
|
|
146
182
|
const status = await statusesOf(data, agentId);
|
|
147
183
|
return status === undefined ? undefined : [...status.values()].filter((value) => value.status === "pending").length;
|
|
148
184
|
}
|
|
149
|
-
export async function agentLearning(data, agentId, shown, counts, impact, stateOf) {
|
|
185
|
+
export async function agentLearning(data, agentId, shown, counts, impact, stateOf, purgedOf) {
|
|
150
186
|
return reading(data, agentId, async ({ snapshot, body }) => {
|
|
187
|
+
const served = await servedIn(snapshot, purgedOf);
|
|
151
188
|
const corrections = (await readCorrections(snapshot, body)).text;
|
|
152
189
|
// A retired draft whose README.md can't be read is said so (unclearFront), read from its file now, as the snapshot's
|
|
153
190
|
// status was: a few retired items, one read each.
|
|
154
191
|
const unread = async (item) => item.status === "retired" && unclearFront(await body(item.file, item.oid));
|
|
155
|
-
return { agent: agentId, main: snapshot.main, corrections: shown.text(corrections), workflows: await Promise.all(snapshot.workflows.map(async (item) => workflowView(item, shown, stateOf, await unread(item)))), log: logPage(snapshot, 0, LOG_PAGE, shown), counts, impact };
|
|
192
|
+
return { agent: agentId, main: snapshot.main, corrections: shown.text(corrections), workflows: await Promise.all(snapshot.workflows.map(async (item) => workflowView(item, shown, stateOf, await unread(item)))), log: logPage(snapshot, 0, LOG_PAGE, shown, served), counts, impact };
|
|
156
193
|
}, () => ({ agent: agentId, main: null, corrections: "", workflows: [], log: EMPTY, counts, impact }));
|
|
157
194
|
}
|
|
158
|
-
/** GET /agents/{id}/learning/log?cursor=&limit=: a page of the changes on main, newest first
|
|
159
|
-
|
|
195
|
+
/** GET /agents/{id}/learning/log?cursor=&limit=: a page of the changes on main, newest first, a purged run's left out
|
|
196
|
+
* (`purgedOf`). */
|
|
197
|
+
export async function learningLog(data, agentId, query, shown, purgedOf) {
|
|
160
198
|
const { limit, at } = pageRequest(query, LOG_PAGE);
|
|
161
199
|
return reading(data, agentId, async ({ snapshot }) => {
|
|
162
200
|
const page = pageAt(agentId, snapshot, at);
|
|
163
|
-
return logPage(page.snapshot, page.offset, limit, shown);
|
|
201
|
+
return logPage(page.snapshot, page.offset, limit, shown, await servedIn(page.snapshot, purgedOf));
|
|
164
202
|
}, () => EMPTY);
|
|
165
203
|
}
|
|
166
204
|
/** GET /agents/{id}/learning/corrections: corrections.md on main as text (empty when main holds none), its ETag (its blob
|
|
@@ -206,8 +244,9 @@ const REVISIONS_DEFAULT = 20;
|
|
|
206
244
|
* commits on main's log that changed `workflows/<name>/workflow.json`), oldest first, each with the workflow's text at
|
|
207
245
|
* that commit, so a person can step through how it changed. One read-only exec per revision (textsAt), at most `limit`
|
|
208
246
|
* of them. `total` counts every such commit the log holds, `truncated` says the log itself is cut (it keeps the latest
|
|
209
|
-
* LOG_KEEP changes). A workflow no commit of the log changed is
|
|
210
|
-
|
|
247
|
+
* LOG_KEEP changes). A purged run's merge is no revision (`purgedOf`). A workflow no commit of the log changed is
|
|
248
|
+
* not_found. */
|
|
249
|
+
export async function workflowRevisions(data, agentId, name, query, shown, purgedOf) {
|
|
211
250
|
if (!WORKFLOW_NAME.test(name))
|
|
212
251
|
throw new ApiError("bad_request", "A workflow's folder is named with lowercase letters, digits, dashes and underscores.");
|
|
213
252
|
const given = query.get("limit");
|
|
@@ -218,7 +257,8 @@ export async function workflowRevisions(data, agentId, name, query, shown) {
|
|
|
218
257
|
if (shown.name(path) !== path)
|
|
219
258
|
throw new ApiError("not_found", "the agent's learning folder has no such workflow");
|
|
220
259
|
const found = await reading(data, agentId, async ({ ws, snapshot }) => {
|
|
221
|
-
const
|
|
260
|
+
const served = await servedIn(snapshot, purgedOf);
|
|
261
|
+
const touched = snapshot.log.filter((change) => served(change) && change.files.includes(path));
|
|
222
262
|
const latest = touched.slice(0, limit).reverse();
|
|
223
263
|
const revisions = [];
|
|
224
264
|
for (const change of latest) {
|
|
@@ -237,16 +277,22 @@ export async function workflowRevisions(data, agentId, name, query, shown) {
|
|
|
237
277
|
/** Learned workflow `name`'s text at `digest` (a champion's, as its promotion records name it), read by that digest and
|
|
238
278
|
* never by paging the workflow's history: at `commits` (the records' learning commits, newest first), then at main, the
|
|
239
279
|
* first commit whose workflow.json is that digest's text, with the commit it was read at. Undefined when none is (no
|
|
240
|
-
* main yet, or no commit read holds it).
|
|
241
|
-
|
|
280
|
+
* main yet, or no commit read holds it). A commit whose text a purged run wrote is never read (purged-workflow.ts),
|
|
281
|
+
* however old. One read-only exec per commit tried (textsAt), and one more for the commit that holds the digest, unless
|
|
282
|
+
* it is main (snapshotAt). */
|
|
283
|
+
export async function workflowAtDigest(data, agentId, name, digest, commits, store) {
|
|
242
284
|
if (!WORKFLOW_NAME.test(name))
|
|
243
285
|
return undefined;
|
|
244
286
|
const path = `${WORKFLOWS_DIR}/${name}/workflow.json`;
|
|
245
287
|
return reading(data, agentId, async ({ ws, snapshot }) => {
|
|
246
288
|
for (const commit of new Set([...commits, snapshot.main])) {
|
|
247
289
|
const raw = (await textsAt(ws, commit, [path]).catch(() => undefined))?.[path];
|
|
248
|
-
if (raw
|
|
249
|
-
|
|
290
|
+
if (raw === undefined || createHash("sha256").update(raw).digest("hex").slice(0, 16) !== digest)
|
|
291
|
+
continue;
|
|
292
|
+
const at = commit === snapshot.main ? snapshot : await snapshotAt(ws, commit).catch(() => undefined);
|
|
293
|
+
if (!at || (await purgedWorkflows(at, store)).has(name))
|
|
294
|
+
continue;
|
|
295
|
+
return { text: raw, commit };
|
|
250
296
|
}
|
|
251
297
|
return undefined;
|
|
252
298
|
}, () => undefined);
|
|
@@ -279,12 +325,20 @@ function fileOf(raw, use) {
|
|
|
279
325
|
return file;
|
|
280
326
|
}
|
|
281
327
|
/** GET /agents/{id}/learning/files/{file}: a draft's file on main as text, its ETag, and its type (workflow.json is
|
|
282
|
-
* JSON).
|
|
283
|
-
|
|
328
|
+
* JSON). A purged run's workflow left main with its purge (learn/commit.ts forgetRunText), so its files are the 404
|
|
329
|
+
* any missing file is. A workflow.json still on main whose writer is purged (a run purged before purges took its
|
|
330
|
+
* workflows off, until its DELETE is sent again) is 410 purged, as every route of that run is (purged-workflow.ts). */
|
|
331
|
+
export async function readLearnedFile(data, agentId, raw, shown, store) {
|
|
284
332
|
const file = fileOf(raw, "read");
|
|
333
|
+
const place = learnedPlace(file);
|
|
285
334
|
const found = await reading(data, agentId, async ({ snapshot, body }) => {
|
|
286
335
|
const oid = blobIn(snapshot, file);
|
|
287
|
-
|
|
336
|
+
if (oid === undefined)
|
|
337
|
+
return undefined;
|
|
338
|
+
const purged = place.kind === "workflow" ? (await purgedWorkflows(snapshot, store)).get(place.name) : undefined;
|
|
339
|
+
if (purged)
|
|
340
|
+
throw purgedRun(purged);
|
|
341
|
+
return { oid, text: await body(file, oid) };
|
|
288
342
|
}, () => undefined);
|
|
289
343
|
if (!found)
|
|
290
344
|
throw new ApiError("not_found", "the agent's learning folder has no such file on main");
|
|
@@ -476,10 +530,46 @@ export async function runDiffOverStore(data, store, runId, shown) {
|
|
|
476
530
|
};
|
|
477
531
|
if (end?.merged) {
|
|
478
532
|
const folder = learningFolderOf(data, row.agentId);
|
|
479
|
-
const diff = await readRunDiff(folder.workspace, runDiffKey(runId, kept?.round)).catch((error) => Promise.reject(routeFailure(error))).finally(() => folder.end());
|
|
480
|
-
if (!diff
|
|
533
|
+
const diff = await readRunDiff(folder.workspace, runDiffKey(runId, kept?.round)).then((read) => (read && read.merge === end.merged ? againstPurged(folder.workspace, read, store) : undefined)).catch((error) => Promise.reject(routeFailure(error))).finally(() => folder.end());
|
|
534
|
+
if (!diff)
|
|
481
535
|
throw new ApiError("not_found", "the run's merge is not in its agent's learning folder");
|
|
482
536
|
return { base: diff.parent, head: diff.merge, files: diff.files.map((entry) => ("patch" in entry ? file(entry.path, entry.status, entry.patch) : { path: shown.name(entry.path), status: entry.status, withheld: entry.withheld })) };
|
|
483
537
|
}
|
|
484
538
|
return { base: start?.seed ?? null, head: null, files: [] };
|
|
485
539
|
}
|
|
540
|
+
/** `diff` with each file of a learned workflow whose text at its parent a purged run wrote (purged-workflow.ts), its
|
|
541
|
+
* whole `workflows/<name>/` folder (README.md too), diffed against nothing, so its patch never shows that text: its
|
|
542
|
+
* text at the merge as an added file, or, deleted there, no file. One more read-only exec when the merge changed such
|
|
543
|
+
* a file (snapshotAt), and one per file served so. */
|
|
544
|
+
async function againstPurged(ws, diff, store) {
|
|
545
|
+
const replaced = (path, status) => {
|
|
546
|
+
const [dir, name, ...rest] = path.split("/");
|
|
547
|
+
return status !== "added" && dir === WORKFLOWS_DIR && rest.length ? name : undefined;
|
|
548
|
+
};
|
|
549
|
+
if (!diff.files.some(({ path, status }) => replaced(path, status)))
|
|
550
|
+
return diff;
|
|
551
|
+
const purged = await purgedWorkflows(await snapshotAt(ws, diff.parent), store);
|
|
552
|
+
const files = [];
|
|
553
|
+
for (const entry of diff.files) {
|
|
554
|
+
const name = replaced(entry.path, entry.status);
|
|
555
|
+
if (name === undefined || !purged.has(name))
|
|
556
|
+
files.push(entry);
|
|
557
|
+
else if (entry.status === "modified") {
|
|
558
|
+
const text = (await textsAt(ws, diff.merge, [entry.path]))?.[entry.path];
|
|
559
|
+
if (text !== undefined)
|
|
560
|
+
files.push({ path: entry.path, status: "added", patch: addedPatch(text) });
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return { ...diff, files };
|
|
564
|
+
}
|
|
565
|
+
/** `text` as git's unified diff writes an added file, its hunk alone (as the learning script keeps a patch): no hunk for
|
|
566
|
+
* an empty file, a one-line hunk's count left out, and a last line with no newline marked so. */
|
|
567
|
+
function addedPatch(text) {
|
|
568
|
+
if (text === "")
|
|
569
|
+
return "";
|
|
570
|
+
const lines = text.split("\n");
|
|
571
|
+
const ended = text.endsWith("\n");
|
|
572
|
+
if (ended)
|
|
573
|
+
lines.pop();
|
|
574
|
+
return `@@ -0,0 +1${lines.length === 1 ? "" : `,${lines.length}`} @@\n${lines.map((line) => `+${line}\n`).join("")}${ended ? "" : "\\n"}`;
|
|
575
|
+
}
|
package/dist/learning.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Problem } from "./learn/index.js";
|
|
2
2
|
import { type LearningFolder } from "./learn/folder.js";
|
|
3
|
+
import type { Store } from "./store/index.js";
|
|
3
4
|
import type { Workspace } from "./workspace/index.js";
|
|
4
5
|
export { learnsOf } from "./learning-setting.js";
|
|
5
6
|
/** The server's learning switch (`--learning`, AGENTRUN_SERVE_LEARNING; capabilities.features.learning). `on`, the
|
|
@@ -111,7 +112,7 @@ export declare const detailShown: (learning: Learning, shown: Shown, max?: numbe
|
|
|
111
112
|
export declare const learningView: (learning: Learning, shown: Shown) => {
|
|
112
113
|
workflows?: LearnedFile[];
|
|
113
114
|
state: "dropped" | "merged" | "seeded" | "reflecting";
|
|
114
|
-
reason: "cancelled" | "reset" | "use" | "empty" | "
|
|
115
|
+
reason: "cancelled" | "reset" | "use" | "empty" | "seed_failed" | "no_budget" | "unknown_cost" | "crashed" | "reflect_failed" | "checker" | null;
|
|
115
116
|
detail: string | null;
|
|
116
117
|
at: string;
|
|
117
118
|
};
|
|
@@ -126,9 +127,9 @@ export declare function shortOf<T extends {
|
|
|
126
127
|
at: string;
|
|
127
128
|
}>({ state, reason, detail, at: _, ...rest }: T): {
|
|
128
129
|
state: "dropped" | "merged" | "seeded" | "reflecting";
|
|
129
|
-
reason: "cancelled" | "reset" | "use" | "empty" | "
|
|
130
|
+
reason: "cancelled" | "reset" | "use" | "empty" | "seed_failed" | "no_budget" | "unknown_cost" | "crashed" | "reflect_failed" | "checker" | null;
|
|
130
131
|
detail: string | null;
|
|
131
|
-
} & Omit<T, "at" | "reason" | "
|
|
132
|
+
} & Omit<T, "at" | "reason" | "detail" | "state">;
|
|
132
133
|
/** The label of the end-of-run turn's timeline rows, and the kind and label of a run's learning row. */
|
|
133
134
|
export declare const LEARNING_LABEL = "learning";
|
|
134
135
|
/** A message a trigger sent the run that its owner did not write (a webhook body, a Slack or GitHub event), by its
|
|
@@ -175,9 +176,11 @@ export declare const learningFolderOf: (data: string, agentId: string) => Learni
|
|
|
175
176
|
* 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
177
|
* reset is (learning-reset.ts). No git in this process: the folder's own script and file API. */
|
|
177
178
|
export declare function seedOf(data: string, agentId: string): Promise<Seed>;
|
|
178
|
-
/** What commit `commit` of the folder holds for a run, as seedOf reads main (one read-only exec of the script)
|
|
179
|
-
*
|
|
180
|
-
|
|
179
|
+
/** What commit `commit` of the folder holds for a run, as seedOf reads main (one read-only exec of the script), but a
|
|
180
|
+
* learned workflow a purged run wrote, by `store`'s runs (purged-workflow.ts): no claim lays, checks, replays or serves
|
|
181
|
+
* it. `store` null: a run's own process, which reads no other run; its runner reads the seed again with its store
|
|
182
|
+
* (runner/learning-seed.ts). No commit (null) is the empty seed; a commit the folder no longer holds throws. */
|
|
183
|
+
export declare function seedAt(ws: Workspace, commit: string | null, store: Pick<Store, "runs"> | null): Promise<Seed>;
|
|
181
184
|
/** Whether `text` holds a host secret (`shown`): as written, or, read as JSON, in any string it decodes to (its escapes
|
|
182
185
|
* decoded), or nested past the bound. The one check a learned workflow's text passes at a run's start (chooseWorkflows)
|
|
183
186
|
* and every text its compile turn is told (runner/learning-end.ts compileAskOf); a text that is not JSON is read as
|
package/dist/learning.js
CHANGED
|
@@ -18,6 +18,7 @@ import { folderAt, learningInit, learningWorkspace, readLearning } from "./learn
|
|
|
18
18
|
import { agentFolderOf, serialBy } from "./files.js";
|
|
19
19
|
import { withoutLoopbackAddresses } from "./key-refusal.js";
|
|
20
20
|
import { DETAIL_MAX, LIST_DETAIL, said, short, wordsOf } from "./learning-words.js";
|
|
21
|
+
import { purgedWorkflows } from "./purged-workflow.js";
|
|
21
22
|
export { learnsOf } from "./learning-setting.js";
|
|
22
23
|
/** The server's learning switch (`--learning`, AGENTRUN_SERVE_LEARNING; capabilities.features.learning). `on`, the
|
|
23
24
|
* default: a run learns when its agent's definition says `learning: true`. `off`: no run this server starts does. */
|
|
@@ -204,20 +205,24 @@ export function seedOf(data, agentId) {
|
|
|
204
205
|
}
|
|
205
206
|
});
|
|
206
207
|
}
|
|
207
|
-
/** What commit `commit` of the folder holds for a run, as seedOf reads main (one read-only exec of the script)
|
|
208
|
-
*
|
|
209
|
-
|
|
208
|
+
/** What commit `commit` of the folder holds for a run, as seedOf reads main (one read-only exec of the script), but a
|
|
209
|
+
* learned workflow a purged run wrote, by `store`'s runs (purged-workflow.ts): no claim lays, checks, replays or serves
|
|
210
|
+
* it. `store` null: a run's own process, which reads no other run; its runner reads the seed again with its store
|
|
211
|
+
* (runner/learning-seed.ts). No commit (null) is the empty seed; a commit the folder no longer holds throws. */
|
|
212
|
+
export async function seedAt(ws, commit, store) {
|
|
210
213
|
if (commit === null)
|
|
211
214
|
return { sha: null, corrections: "", workflows: [] };
|
|
212
215
|
const at = await folderAt(ws, commit);
|
|
213
216
|
if (!at)
|
|
214
217
|
throw new Error(`the learning folder's read at ${commit} is gone`);
|
|
215
|
-
|
|
218
|
+
const purged = store ? await purgedWorkflows(at.snapshot, store) : new Map();
|
|
219
|
+
const seed = await seedIn(commit, at.snapshot, async (_path, oid) => {
|
|
216
220
|
const bytes = at.blobs.get(oid);
|
|
217
221
|
if (!bytes)
|
|
218
222
|
throw new Error(`the learning folder's read at ${commit} holds no blob ${oid}`);
|
|
219
223
|
return bytes.toString("utf8");
|
|
220
224
|
});
|
|
225
|
+
return { ...seed, workflows: seed.workflows.filter(({ name }) => !purged.has(name)) };
|
|
221
226
|
}
|
|
222
227
|
/** Whether `text` holds a host secret (`shown`): as written, or, read as JSON, in any string it decodes to (its escapes
|
|
223
228
|
* decoded), or nested past the bound. The one check a learned workflow's text passes at a run's start (chooseWorkflows)
|
package/dist/mcp-gateway.js
CHANGED
|
@@ -28,6 +28,7 @@ import { paidLedger, paidStep } from "./paid-calls.js";
|
|
|
28
28
|
import { metaToolOf } from "./mcp-meta.js";
|
|
29
29
|
import { SIGN_IN_AGAIN } from "./mcp-oauth.js";
|
|
30
30
|
import { capped, mcpClient, scrub } from "./mcp-wire.js";
|
|
31
|
+
import { resourceResult, resourceToolOperation, resourceToolProblem } from "./mcp-resources.js";
|
|
31
32
|
import { mcpNameOf, mcpToolName } from "./tool-names.js";
|
|
32
33
|
import { workspaceAt, workspaceErrorWords } from "./workspace/index.js";
|
|
33
34
|
/** The longest answer the model reads whole; past it, the rest is filed in the run's work/ as lines read pages through
|
|
@@ -121,6 +122,9 @@ export function mcpPinOf(names, source) {
|
|
|
121
122
|
throw new Error(`The MCP tool ${name} cannot be pinned: no MCP server ${parsed.server} is saved.`);
|
|
122
123
|
if (!tool)
|
|
123
124
|
throw new Error(`The MCP tool ${name} cannot be pinned: the MCP server ${parsed.server} does not list ${parsed.tool}.`);
|
|
125
|
+
const resourceProblem = resourceToolProblem(tool);
|
|
126
|
+
if (resourceProblem)
|
|
127
|
+
throw new Error(`The MCP tool ${name} cannot be pinned: ${resourceProblem}.`);
|
|
124
128
|
// A meta tool granted before the rule (mcp-meta.ts): refused here, never run.
|
|
125
129
|
const meta = metaToolOf(saved.url, tool.name);
|
|
126
130
|
if (meta)
|
|
@@ -325,6 +329,8 @@ export function mcpGateway(options) {
|
|
|
325
329
|
* takes the server's raw result, when one came back. */
|
|
326
330
|
const forward = async (server, url, headers, tool, args, signal, sending, outcome = {}, back) => {
|
|
327
331
|
let connected = false;
|
|
332
|
+
const operation = resourceToolOperation(tool);
|
|
333
|
+
const boundedSignal = operation ? AbortSignal.any([signal, AbortSignal.timeout(options.bounds?.callMs ?? CALL_MS)]) : signal;
|
|
328
334
|
// Read at each use: a token refreshed while the call is out, and sent, is scrubbed with the one it took.
|
|
329
335
|
const held = callSet(server);
|
|
330
336
|
const scrubbed = scrubberOf(server, hold(server, headers), held.values);
|
|
@@ -332,7 +338,7 @@ export function mcpGateway(options) {
|
|
|
332
338
|
const clean = back ? (text) => back(scrubbed(text)) : scrubbed;
|
|
333
339
|
hold(server, queryOf(server));
|
|
334
340
|
try {
|
|
335
|
-
const upstream = await until(connect(server, url, headers),
|
|
341
|
+
const upstream = await until(connect(server, url, headers), boundedSignal);
|
|
336
342
|
connected = true;
|
|
337
343
|
if (sending && !signal.aborted) {
|
|
338
344
|
const refused = await sending();
|
|
@@ -341,7 +347,9 @@ export function mcpGateway(options) {
|
|
|
341
347
|
}
|
|
342
348
|
// The pinned definition, so the SDK mirrors its x-mcp-header parameters into Mcp-Param-* headers on a 2026-07-28
|
|
343
349
|
// server, as its own listing would have it do.
|
|
344
|
-
const result =
|
|
350
|
+
const result = operation
|
|
351
|
+
? await resourceResult(upstream, operation, args, boundedSignal, options.bounds?.callMs ?? CALL_MS, scrubbed)
|
|
352
|
+
: (await upstream.callTool({ name: tool.name, arguments: args }, { signal, timeout: options.bounds?.callMs ?? CALL_MS, toolDefinition: { name: tool.name, inputSchema: tool.inputSchema } }));
|
|
345
353
|
outcome.raw = result;
|
|
346
354
|
// A meter's own read (`quiet`): its raw result is all that is wanted, nothing filed for a model to read.
|
|
347
355
|
return said(outcome.quiet ? "" : await textOf(server, tool.name, result, clean), result.isError === true);
|
|
@@ -349,6 +357,8 @@ export function mcpGateway(options) {
|
|
|
349
357
|
catch (error) {
|
|
350
358
|
if (signal.aborted)
|
|
351
359
|
return said(`The call to the MCP server ${server} was stopped.`, true);
|
|
360
|
+
if (boundedSignal.aborted)
|
|
361
|
+
return said(`The resource operation on the MCP server ${server} exceeded its deadline.`, true);
|
|
352
362
|
// The next call connects again (a server that dropped its session, say); this one is never sent again.
|
|
353
363
|
clients.delete(server);
|
|
354
364
|
// Its address refused before the request left (reach): nothing reached it. Read from this call's own error, never
|
|
@@ -381,6 +391,9 @@ export function mcpGateway(options) {
|
|
|
381
391
|
const pinned = pins.get(server);
|
|
382
392
|
if (!pinned?.tools.some((t) => t.name === tool))
|
|
383
393
|
return `${tool} is not a tool of the MCP server ${server} in this run.`;
|
|
394
|
+
const resourceProblem = resourceToolProblem(pinned.tools.find(t => t.name === tool));
|
|
395
|
+
if (resourceProblem)
|
|
396
|
+
return `The MCP operation ${tool} is invalid: ${resourceProblem}. Nothing was sent.`;
|
|
384
397
|
const meta = metaToolOf(pinned.url, tool);
|
|
385
398
|
if (meta)
|
|
386
399
|
return `mcp:${server}/${tool} can't be granted: ${meta.reason} Nothing was sent.`;
|
package/dist/mcp-probe.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { ApiError, type InputIssue } from "./errors.js";
|
|
2
2
|
import type { ComposioBinding } from "./store/seal.js";
|
|
3
|
+
import { type ResourceOperation } from "./mcp-resources.js";
|
|
3
4
|
/** `per_run`: the server keeps no credential; each run's caller hands one with the run (mcp_auth), held for that run alone. */
|
|
4
5
|
export declare const MCP_AUTH_TYPES: readonly ["none", "bearer", "headers", "oauth", "github_app", "per_run"];
|
|
5
6
|
/** Why a saved credential can't be used for a run's calls, each with its own remedy and no other, as the model reads it
|
|
@@ -35,6 +36,7 @@ export type McpTool = {
|
|
|
35
36
|
input_schema: unknown;
|
|
36
37
|
output_schema?: unknown;
|
|
37
38
|
annotations?: unknown;
|
|
39
|
+
resource?: ResourceOperation;
|
|
38
40
|
};
|
|
39
41
|
/** A registry tool (input_schema, as the server listed it) in the harnesses' shape (the kernel's McpTool: inputSchema),
|
|
40
42
|
* as a run is pinned to it and a session binds it. */
|
|
@@ -44,6 +46,7 @@ export declare function boundTool(tool: McpTool): {
|
|
|
44
46
|
description?: string;
|
|
45
47
|
inputSchema: Record<string, unknown>;
|
|
46
48
|
annotations?: Record<string, unknown>;
|
|
49
|
+
resource?: ResourceOperation;
|
|
47
50
|
};
|
|
48
51
|
export type Listed = {
|
|
49
52
|
protocol: string;
|
package/dist/mcp-probe.js
CHANGED
|
@@ -10,6 +10,7 @@ import { ApiError, invalid } from "./errors.js";
|
|
|
10
10
|
import { metaToolOf } from "./mcp-meta.js";
|
|
11
11
|
import { SIGN_IN_AGAIN } from "./mcp-oauth.js";
|
|
12
12
|
import { capped, MAX_RESPONSE, mcpClient, scrub } from "./mcp-wire.js";
|
|
13
|
+
import { reservedResourceName, resourceTools } from "./mcp-resources.js";
|
|
13
14
|
import { MCP_SERVER_NAME } from "./tool-names.js";
|
|
14
15
|
import { isObject, unknownFields } from "./validate.js";
|
|
15
16
|
/** The names AgentRun's own tools are called by, which no MCP server takes: claude-agent calls the kernel's host tools
|
|
@@ -67,7 +68,7 @@ const LOOPBACK = (host) => LOOPBACK_NAMES.includes(host) || LOOPBACK_V4.test(hos
|
|
|
67
68
|
/** A registry tool (input_schema, as the server listed it) in the harnesses' shape (the kernel's McpTool: inputSchema),
|
|
68
69
|
* as a run is pinned to it and a session binds it. */
|
|
69
70
|
export function boundTool(tool) {
|
|
70
|
-
return { name: tool.name, ...(tool.title ? { title: tool.title } : {}), ...(tool.description ? { description: tool.description } : {}), inputSchema: (tool.input_schema ?? {}), ...(tool.annotations ? { annotations: tool.annotations } : {}) };
|
|
71
|
+
return { name: tool.name, ...(tool.title ? { title: tool.title } : {}), ...(tool.description ? { description: tool.description } : {}), inputSchema: (tool.input_schema ?? {}), ...(tool.annotations ? { annotations: tool.annotations } : {}), ...(tool.resource !== undefined ? { resource: tool.resource } : {}) };
|
|
71
72
|
}
|
|
72
73
|
export const notFound = (name) => new ApiError("not_found", `No MCP server ${name}: GET /v2/settings/mcp-servers lists them.`);
|
|
73
74
|
export const credentialMissing = (type, message) => ({ field: type === "bearer" ? "auth.token" : type === "oauth" ? "auth.type" : "auth.headers", reason: "missing", message: type === "oauth" ? `${message} An OAuth server is signed in to first: POST /v2/settings/mcp-servers/{name}/oauth/start.` : message });
|
|
@@ -277,7 +278,7 @@ function composioBindingOf(field, entry, issues) {
|
|
|
277
278
|
const user = entry.composio_user_id;
|
|
278
279
|
const account = entry.connected_account_id;
|
|
279
280
|
if (typeof user !== "string" || !COMPOSIO_USER_ID.test(user))
|
|
280
|
-
issues.push({ field: `${field}.composio_user_id`, reason: "invalid", message: "composio_user_id is the Composio user this run's calls run as: letters, digits and . _ @ - (1 to 128)." });
|
|
281
|
+
issues.push({ field: `${field}.composio_user_id`, reason: "invalid", message: "composio_user_id is the Composio user this run's calls run as: letters, digits and . _ @ : - (1 to 128)." });
|
|
281
282
|
// The preset's default user is the one single-user preset servers call as: a run that forgot to name its own never lands there.
|
|
282
283
|
else if (user === COMPOSIO_USER)
|
|
283
284
|
issues.push({ field: `${field}.composio_user_id`, reason: "not_allowed", message: "That composio_user_id is the Composio preset's shared default user, the one single-user preset servers call as: bind the run to its own person's Composio user." });
|
|
@@ -320,6 +321,8 @@ export async function listServer(url, headers, version, stop, send = fetch) {
|
|
|
320
321
|
for (let cursor, pages = 0;;) {
|
|
321
322
|
const page = await client.request({ method: "tools/list", ...(cursor !== undefined ? { params: { cursor } } : {}) }, { timeout: PREVIEW_MS, signal: deadline });
|
|
322
323
|
for (const tool of page.tools) {
|
|
324
|
+
if (reservedResourceName(tool.name))
|
|
325
|
+
throw invalid([{ field: "url", reason: "reserved", message: `The tool name ${tool.name} is reserved for AgentRun's resource operations.` }]);
|
|
323
326
|
const kept = {
|
|
324
327
|
name: tool.name,
|
|
325
328
|
...(tool.title ? { title: tool.title } : {}),
|
|
@@ -342,6 +345,8 @@ export async function listServer(url, headers, version, stop, send = fetch) {
|
|
|
342
345
|
cursor = page.nextCursor;
|
|
343
346
|
}
|
|
344
347
|
}
|
|
348
|
+
if (client.getServerCapabilities()?.resources)
|
|
349
|
+
tools.push(...resourceTools());
|
|
345
350
|
const instructions = client.getInstructions();
|
|
346
351
|
return {
|
|
347
352
|
protocol: client.getNegotiatedProtocolVersion() ?? "unknown",
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare const RESOURCE_OPERATIONS: readonly ["list", "templates", "read"];
|
|
2
|
+
export type ResourceOperation = typeof RESOURCE_OPERATIONS[number];
|
|
3
|
+
export declare const RESOURCE_NAMES: Record<ResourceOperation, string>;
|
|
4
|
+
export declare const RESOURCE_URI_MAX = 8192;
|
|
5
|
+
export declare const RESOURCE_LIST_MAX = 500;
|
|
6
|
+
export declare const RESOURCE_LIST_BYTES: number;
|
|
7
|
+
export declare const RESOURCE_PAGES_MAX = 64;
|
|
8
|
+
export declare const reservedResourceName: (name: string) => boolean;
|
|
9
|
+
export declare function resourceTools(): {
|
|
10
|
+
name: string;
|
|
11
|
+
resource: "read" | "templates" | "list";
|
|
12
|
+
title: string;
|
|
13
|
+
description: string;
|
|
14
|
+
input_schema: {
|
|
15
|
+
additionalProperties: boolean;
|
|
16
|
+
required?: string[] | undefined;
|
|
17
|
+
type: string;
|
|
18
|
+
properties: {
|
|
19
|
+
uri: {
|
|
20
|
+
type: string;
|
|
21
|
+
minLength: number;
|
|
22
|
+
maxLength: number;
|
|
23
|
+
};
|
|
24
|
+
} | {
|
|
25
|
+
uri?: undefined;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
annotations: {
|
|
29
|
+
readOnlyHint: boolean;
|
|
30
|
+
destructiveHint: boolean;
|
|
31
|
+
idempotentHint: boolean;
|
|
32
|
+
openWorldHint: boolean;
|
|
33
|
+
};
|
|
34
|
+
}[];
|
|
35
|
+
/** A stored marker is accepted only with its exact operation name and bounded schema. Older ordinary tools without
|
|
36
|
+
* a marker remain ordinary tools; a name alone never turns tools/call into resources/read. */
|
|
37
|
+
export declare function resourceToolProblem(value: unknown): string | undefined;
|
|
38
|
+
export declare function resourceToolOperation(value: unknown): ResourceOperation | undefined;
|
|
39
|
+
type ResourceClient = {
|
|
40
|
+
request(input: unknown, options: unknown): Promise<unknown>;
|
|
41
|
+
};
|
|
42
|
+
/** One bounded logical operation. Pagination belongs to this operation's deadline and fails if incomplete. */
|
|
43
|
+
export declare function resourceResult(client: ResourceClient, operation: ResourceOperation, args: Record<string, unknown>, signal: AbortSignal, timeout: number, clean: (text: string) => string): Promise<Record<string, unknown>>;
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Resource operations use the same per-server grants, revision pins, credentials and run gate as MCP tools.
|
|
2
|
+
// Only our probe creates these entries; an upstream tools/list entry never supplies the operation marker.
|
|
3
|
+
export const RESOURCE_OPERATIONS = ["list", "templates", "read"];
|
|
4
|
+
export const RESOURCE_NAMES = {
|
|
5
|
+
list: "agentrun_list_resources", templates: "agentrun_list_resource_templates", read: "agentrun_read_resource",
|
|
6
|
+
};
|
|
7
|
+
export const RESOURCE_URI_MAX = 8192;
|
|
8
|
+
export const RESOURCE_LIST_MAX = 500;
|
|
9
|
+
export const RESOURCE_LIST_BYTES = 1024 * 1024;
|
|
10
|
+
export const RESOURCE_PAGES_MAX = 64;
|
|
11
|
+
const object = (value) => !!value && typeof value === "object" && !Array.isArray(value);
|
|
12
|
+
const keysAre = (value, keys) => Object.keys(value).length === keys.length && keys.every(key => Object.hasOwn(value, key));
|
|
13
|
+
export const reservedResourceName = (name) => Object.values(RESOURCE_NAMES).includes(name);
|
|
14
|
+
export function resourceTools() {
|
|
15
|
+
return RESOURCE_OPERATIONS.map(resource => ({
|
|
16
|
+
name: RESOURCE_NAMES[resource], resource,
|
|
17
|
+
title: resource === "list" ? "List resources" : resource === "templates" ? "List resource templates" : "Read resource",
|
|
18
|
+
description: resource === "list" ? "List the resources available from this MCP server."
|
|
19
|
+
: resource === "templates" ? "List this MCP server's resource URI templates."
|
|
20
|
+
: "Read a resource URI from this MCP server. The URI is sent to the server, never fetched directly.",
|
|
21
|
+
input_schema: { type: "object", properties: resource === "read" ? { uri: { type: "string", minLength: 1, maxLength: RESOURCE_URI_MAX } } : {}, ...(resource === "read" ? { required: ["uri"] } : {}), additionalProperties: false },
|
|
22
|
+
annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: true },
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
/** A stored marker is accepted only with its exact operation name and bounded schema. Older ordinary tools without
|
|
26
|
+
* a marker remain ordinary tools; a name alone never turns tools/call into resources/read. */
|
|
27
|
+
export function resourceToolProblem(value) {
|
|
28
|
+
if (!object(value) || value.resource === undefined)
|
|
29
|
+
return undefined;
|
|
30
|
+
const op = value.resource;
|
|
31
|
+
if (typeof op !== "string" || !RESOURCE_OPERATIONS.includes(op) || value.name !== RESOURCE_NAMES[op])
|
|
32
|
+
return "invalid resource operation marker";
|
|
33
|
+
const schema = value.inputSchema ?? value.input_schema;
|
|
34
|
+
const read = op === "read";
|
|
35
|
+
if (!object(schema) || !keysAre(schema, read ? ["type", "properties", "required", "additionalProperties"] : ["type", "properties", "additionalProperties"]) || schema.type !== "object" || schema.additionalProperties !== false || !object(schema.properties))
|
|
36
|
+
return "invalid resource operation schema";
|
|
37
|
+
if (!keysAre(schema.properties, read ? ["uri"] : []))
|
|
38
|
+
return "invalid resource operation arguments";
|
|
39
|
+
if (read) {
|
|
40
|
+
const uri = schema.properties.uri;
|
|
41
|
+
if (!object(uri) || !keysAre(uri, ["type", "minLength", "maxLength"]) || uri.type !== "string" || uri.minLength !== 1 || uri.maxLength !== RESOURCE_URI_MAX || !Array.isArray(schema.required) || schema.required.length !== 1 || schema.required[0] !== "uri")
|
|
42
|
+
return "invalid resource URI schema";
|
|
43
|
+
}
|
|
44
|
+
if (!object(value.annotations) || value.annotations.readOnlyHint !== true || value.annotations.destructiveHint !== false)
|
|
45
|
+
return "invalid resource operation effect";
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
export function resourceToolOperation(value) {
|
|
49
|
+
return object(value) && value.resource !== undefined && resourceToolProblem(value) === undefined ? value.resource : undefined;
|
|
50
|
+
}
|
|
51
|
+
/** One bounded logical operation. Pagination belongs to this operation's deadline and fails if incomplete. */
|
|
52
|
+
export async function resourceResult(client, operation, args, signal, timeout, clean) {
|
|
53
|
+
signal.throwIfAborted();
|
|
54
|
+
if (!keysAre(args, operation === "read" ? ["uri"] : []))
|
|
55
|
+
throw new Error("The resource operation has unexpected or missing arguments.");
|
|
56
|
+
if (operation === "read") {
|
|
57
|
+
if (typeof args.uri !== "string" || !args.uri.length || args.uri.length > RESOURCE_URI_MAX || /[\u0000-\u001f\u007f]/.test(args.uri))
|
|
58
|
+
throw new Error("A resource URI must be bounded text without control characters.");
|
|
59
|
+
const answer = await client.request({ method: "resources/read", params: { uri: args.uri } }, { signal, timeout });
|
|
60
|
+
if (!object(answer) || !Array.isArray(answer.contents) || answer.contents.length > RESOURCE_LIST_MAX || answer.contents.some(item => !object(item) || typeof item.uri !== "string" || (typeof item.text !== "string" && typeof item.blob !== "string")))
|
|
61
|
+
throw new Error("The MCP server returned invalid resource contents.");
|
|
62
|
+
for (const item of answer.contents)
|
|
63
|
+
if (typeof item.blob === "string") {
|
|
64
|
+
// The wire caps the whole response. Check the decoded binary too: a JSON-only scrub cannot see a token
|
|
65
|
+
// encoded inside a BlobResourceContents. Withhold it rather than corrupting arbitrary binary bytes.
|
|
66
|
+
const bytes = Buffer.from(item.blob, "base64");
|
|
67
|
+
if (bytes.toString("base64") !== item.blob)
|
|
68
|
+
throw new Error("The MCP server returned an invalid base64 resource.");
|
|
69
|
+
const decoded = bytes.toString("utf8");
|
|
70
|
+
if (clean(decoded) !== decoded)
|
|
71
|
+
throw new Error("The MCP resource contained a connection credential; its contents were withheld.");
|
|
72
|
+
}
|
|
73
|
+
return { content: answer.contents.map(resource => ({ type: "resource", resource })) };
|
|
74
|
+
}
|
|
75
|
+
const method = operation === "list" ? "resources/list" : "resources/templates/list";
|
|
76
|
+
const key = operation === "list" ? "resources" : "resourceTemplates";
|
|
77
|
+
const items = [], cursors = new Set();
|
|
78
|
+
let cursor, bytes = 0;
|
|
79
|
+
for (let pageNumber = 0; pageNumber < RESOURCE_PAGES_MAX; pageNumber++) {
|
|
80
|
+
signal.throwIfAborted();
|
|
81
|
+
const answer = await client.request({ method, ...(cursor === undefined ? {} : { params: { cursor } }) }, { signal, timeout });
|
|
82
|
+
if (!object(answer) || !Array.isArray(answer[key]))
|
|
83
|
+
throw new Error("The MCP server returned an invalid resource list.");
|
|
84
|
+
for (const item of answer[key]) {
|
|
85
|
+
if (!object(item) || typeof item.name !== "string" || typeof item[operation === "list" ? "uri" : "uriTemplate"] !== "string")
|
|
86
|
+
throw new Error("The MCP server returned an invalid resource entry.");
|
|
87
|
+
items.push(item);
|
|
88
|
+
bytes += Buffer.byteLength(JSON.stringify(item));
|
|
89
|
+
if (items.length > RESOURCE_LIST_MAX || bytes > RESOURCE_LIST_BYTES)
|
|
90
|
+
throw new Error("The MCP server's resource listing exceeds the item or byte limit; no incomplete listing was returned.");
|
|
91
|
+
}
|
|
92
|
+
if (answer.nextCursor === undefined)
|
|
93
|
+
return { structuredContent: { [key]: items } };
|
|
94
|
+
if (typeof answer.nextCursor !== "string" || answer.nextCursor.length > 4096 || cursors.has(answer.nextCursor))
|
|
95
|
+
throw new Error("The MCP server's resource cursor is invalid or repeats; no incomplete listing was returned.");
|
|
96
|
+
cursors.add(answer.nextCursor);
|
|
97
|
+
cursor = answer.nextCursor;
|
|
98
|
+
}
|
|
99
|
+
throw new Error("The MCP server's resource list did not finish within the page limit.");
|
|
100
|
+
}
|