@github/copilot-sdk 1.0.9-preview.0 → 1.0.9-preview.1
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 +6 -0
- package/dist/cjs/client.js +9 -0
- package/dist/cjs/extension.js +21 -6
- package/dist/cjs/factory.js +123 -0
- package/dist/cjs/index.js +9 -2
- package/dist/cjs/session.js +589 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +9 -0
- package/dist/extension.d.ts +11 -2
- package/dist/extension.js +22 -6
- package/dist/factory.d.ts +273 -0
- package/dist/factory.js +96 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +5 -1
- package/dist/session.d.ts +21 -0
- package/dist/session.js +593 -0
- package/dist/types.d.ts +41 -7
- package/docs/extensions.md +1 -0
- package/docs/factories.md +240 -0
- package/docs/factory-patterns.md +194 -0
- package/package.json +1 -1
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
# Agent Factories
|
|
2
|
+
|
|
3
|
+
Agent Factories are extension-authored, session-scoped workflows that coordinate subagents and durable steps. The API is experimental.
|
|
4
|
+
|
|
5
|
+
## Define and register a factory
|
|
6
|
+
|
|
7
|
+
Use `defineFactory` and pass the returned handle to `joinSession`:
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { defineFactory, joinSession } from "@github/copilot-sdk/extension";
|
|
11
|
+
|
|
12
|
+
const reviewChanged = defineFactory({
|
|
13
|
+
meta: {
|
|
14
|
+
name: "review-changed",
|
|
15
|
+
description:
|
|
16
|
+
"Review changed files and verify the findings. " +
|
|
17
|
+
"args: { files: string[] } — the paths to review.",
|
|
18
|
+
phases: [{ title: "Review" }, { title: "Verify" }],
|
|
19
|
+
limits: {
|
|
20
|
+
maxConcurrentSubagents: 3,
|
|
21
|
+
maxTotalSubagents: 10,
|
|
22
|
+
timeoutSeconds: 90.5,
|
|
23
|
+
maxAiCredits: 5,
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
run: async (ctx) => {
|
|
27
|
+
ctx.phase("Review");
|
|
28
|
+
const reviews = await ctx.parallel(
|
|
29
|
+
ctx.args.files.map(
|
|
30
|
+
(file) => () => ctx.agent(`Review ${file}`, { label: `Review ${file}` })
|
|
31
|
+
)
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
ctx.phase("Verify");
|
|
35
|
+
const report = await ctx.step("report", () => ({ reviews }));
|
|
36
|
+
ctx.log(`Completed factory run ${ctx.runId}`);
|
|
37
|
+
return report;
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const session = await joinSession({ factories: [reviewChanged] });
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Factory metadata contains a stable `name`, a human-readable `description`, declared `phases`, and optional `limits`. Phase entries contain a `title` and optional `detail`.
|
|
45
|
+
|
|
46
|
+
There is no declared schema for `ctx.args`. The `run_factory` tool forwards `args` verbatim and its parameter is untyped, so **the `description` is the only thing telling an agent what arguments to supply** — state the expected shape there whenever a factory reads `ctx.args`, as the example above does. Arguments supplied by an extension calling `session.factory.run(...)` directly are typed through `defineFactory<TArgs>`, but that typing does not reach the model. A factory that reads `ctx.args` should validate it rather than assume a shape.
|
|
47
|
+
|
|
48
|
+
`defineFactory<TArgs, TResult>` accepts a `run(context)` function returning `Promise<TResult>`, where `TResult` is `JsonValue | void`. Objects, arrays, strings, numbers, booleans, and `null` are valid results. Returning `undefined` completes the factory with no result. Other non-JSON values are rejected.
|
|
49
|
+
|
|
50
|
+
## Factory context
|
|
51
|
+
|
|
52
|
+
The `run()` context provides:
|
|
53
|
+
|
|
54
|
+
- `ctx.runId`: Stable ID reused across resumed attempts.
|
|
55
|
+
- `ctx.args`: Invocation arguments, forwarded verbatim. When the caller omits `args`, this is `{}` rather than `undefined`.
|
|
56
|
+
- `ctx.agent(prompt, options?)`: Runs one factory-owned subagent. Options are exactly `label`, `schema`, and `model`. See [Subagent calls](#subagent-calls).
|
|
57
|
+
- `ctx.parallel(thunks)`: Runs thunks concurrently and awaits all of them (a barrier). A thunk that throws becomes `null` in the result array, so one failed item does not lose the rest. Cancellation and hard runtime failures (`ResponseError`, `ConnectionError`) are the exception — those propagate and reject the whole call, because they mean the run itself is in trouble rather than one item having failed. Handle them at run level; do not assume every failure arrives as a `null`. Rejects above 4096 items.
|
|
58
|
+
- `ctx.pipeline(items, ...stages)`: Flows each item through every stage without a barrier between stages, so one item can be in a later stage while another is still in an earlier one. Each stage is called as `(previous, item, index)`, where `previous` is the prior stage's result and `item` is the original input. A stage that throws drops that item to `null` and skips its remaining stages, with the same exception for cancellation and hard runtime failures. Rejects above 4096 items.
|
|
59
|
+
- `ctx.phase(title)`: Starts a named progress phase. This sets a single run-global value, so calling it from inside concurrent `parallel`/`pipeline` stages races. Call it at run-level transitions and distinguish concurrent work by `label` instead.
|
|
60
|
+
- `ctx.log(message)`: Appends a progress line. When a factory bounds its own coverage (top-N, sampling), log what was dropped.
|
|
61
|
+
- `ctx.step(key, producer, options?)`: Journals the producer's JSON result under a stable key so a resume replays it without re-running the producer. A journaled (default) producer must return a JSON-serializable value; `undefined` or a non-JSON value is rejected. Pass `{ volatile: true }` to bypass the journal and run the producer every time.
|
|
62
|
+
|
|
63
|
+
The key is the *sole* identity: neither the producer body nor its inputs contribute to it. A resume replays the cached value for a matching key even if the producer has since changed, so version the key (`"scan-v2"`) whenever its inputs or meaning change. Journaled producers are best-effort at-least-once and may run again across crashes or concurrent same-key callers, so keep side effects idempotent.
|
|
64
|
+
- `ctx.session`: The full session returned by `joinSession`.
|
|
65
|
+
- `ctx.signal`: Cooperative cancellation signal for extension work and subprocesses.
|
|
66
|
+
- `ctx.factory(...)`: Always rejects because nested factories are not supported.
|
|
67
|
+
|
|
68
|
+
Factory-owned subagents are intentionally hidden from `read_agent` and `write_agent`. Use the factory observability APIs instead.
|
|
69
|
+
|
|
70
|
+
### Subagent calls
|
|
71
|
+
|
|
72
|
+
`ctx.agent(prompt, options?)` spawns one factory-scoped subagent and awaits it. Without a schema it resolves to the subagent's final text. With `options.schema` it resolves to the parsed JSON value.
|
|
73
|
+
|
|
74
|
+
**Identical calls are memoized into one subagent.** Each call is journaled by its canonical prompt and options, including `label`. Two calls with the same prompt and the same options return one shared result — even when issued concurrently. To spawn N *independent* subagents, give each a unique `label` or vary the prompt:
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
// One subagent, awaited five times — almost certainly not what you want.
|
|
78
|
+
await ctx.parallel([1, 2, 3, 4, 5].map(() => () => ctx.agent("Find a bug")));
|
|
79
|
+
|
|
80
|
+
// Five independent subagents.
|
|
81
|
+
await ctx.parallel(
|
|
82
|
+
[1, 2, 3, 4, 5].map((i) => () => ctx.agent("Find a bug", { label: `finder:${i}` }))
|
|
83
|
+
);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**An ordinary failure resolves to `null` — it does not throw.** A subagent that errors, returns nothing, or (with a schema) produces output that still fails to parse or match after its one retry resolves `null`. Always guard the result before using it, including a bare `await ctx.agent(...)`:
|
|
87
|
+
|
|
88
|
+
```js
|
|
89
|
+
const finding = await ctx.agent(prompt, { label: "inspector" });
|
|
90
|
+
if (!finding) return { finding: null };
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Cancellation and hard runtime failures — a reached limit, a durable-state failure — reject instead, aborting the run. When filtering results, prefer `v => v !== null` over `Boolean`, which also discards a valid `false`, `0`, or `""`.
|
|
94
|
+
|
|
95
|
+
**`schema` is a structural subset of JSON Schema, not a validator.** Honored: `type`, `required`, `enum`, `const`, recursive `properties`/`items`, and `anyOf`/`oneOf`/`allOf` — where `oneOf` is treated as `anyOf`, meaning at least one branch matches rather than exactly one. Ignored and *not* enforced: `additionalProperties`, `pattern`, `minLength`/`maxLength`, `format`, numeric ranges, and boolean schemas. Do not rely on an ignored keyword to constrain a result. A schema call retries once on a parse or match failure, so it may spawn twice, and both spawns count toward `maxTotalSubagents`.
|
|
96
|
+
|
|
97
|
+
### Choosing between pipeline and parallel
|
|
98
|
+
|
|
99
|
+
Prefer `pipeline` for multi-stage work. It has no barrier between stages, so each item advances as soon as its own prior stage finishes.
|
|
100
|
+
|
|
101
|
+
Reach for a barrier — `parallel` between stages — only when a stage genuinely needs every prior result at once: deduplicating or merging across the full set, an early exit based on the total, or a prompt that compares one result against the others. Needing to map, filter, or flatten is not a reason to use a barrier; do that inside a pipeline stage. Barrier latency is real: if the slowest of N subagents takes three times the fastest, a barrier wastes the rest of the pool's time.
|
|
102
|
+
|
|
103
|
+
See [factory-patterns.md](./factory-patterns.md) for composable orchestration patterns built on these primitives.
|
|
104
|
+
|
|
105
|
+
## Resource limits
|
|
106
|
+
|
|
107
|
+
Limits may be declared in `meta.limits` and overridden per invocation. All limits must be positive when present.
|
|
108
|
+
|
|
109
|
+
- `maxConcurrentSubagents`: Positive integer concurrent-subagent cap. Additional subagents wait in a queue. Queueing applies backpressure and does not fail the run.
|
|
110
|
+
- `maxTotalSubagents`: Positive integer cumulative admission cap. An attempted subagent beyond the cap ends the attempt with failure kind `maxTotalSubagents`.
|
|
111
|
+
- `timeoutSeconds`: Positive finite number of seconds, including positive fractions, capped at `2_147_483.647`. It measures accumulated active-execution time across attempts, including the extension body, subprocess waits, queued-agent waits, and sleeps. Time between attempts is excluded. The timeout is soft because already-running work may take time to stop. Its failure kind is `timeoutSeconds`.
|
|
112
|
+
- `maxAiCredits`: Positive finite AI-credit budget for the whole run's factory subagent subtree, including descendants. AI credits are GitHub Copilot's universal usage metric. This is a soft, post-paid ceiling, so completed or parallel turns can settle above it before the run stops. Accounting is fail-closed: an accounting failure stops a budgeted run rather than allowing untracked use. Its failure kind is `maxAiCredits`.
|
|
113
|
+
|
|
114
|
+
`maxTotalSubagents`, `timeoutSeconds`, and `maxAiCredits` use reject-and-retry semantics. A rejected attempt ends with run status `error` and `failure.type` set to `factory_limit_reached`. The failed run keeps its ID, arguments, journal, and accounting. Resume the run with a raised limit when additional work is approved. Previously consumed resources still count.
|
|
115
|
+
|
|
116
|
+
## Run and resume
|
|
117
|
+
|
|
118
|
+
Run by registered name or handle:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
const run = await session.factory.run("review-changed", {
|
|
122
|
+
args: { files: ["src/a.ts"] },
|
|
123
|
+
limits: { maxAiCredits: 3 },
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
if (run.status === "completed") {
|
|
127
|
+
console.log(run.result);
|
|
128
|
+
} else {
|
|
129
|
+
console.error(`run ${run.runId} ended as ${run.status}`, run.failure ?? run.error);
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
The name overload is:
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
session.factory.run(
|
|
137
|
+
name: string,
|
|
138
|
+
options?: { args?: JsonValue; limits?: FactoryLimits },
|
|
139
|
+
): Promise<FactoryRunResult>;
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Resume by run ID without resending the name or arguments:
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const run = await session.factory.resume(runId, {
|
|
146
|
+
limits: { maxAiCredits: 6 },
|
|
147
|
+
});
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The signature is:
|
|
151
|
+
|
|
152
|
+
```ts
|
|
153
|
+
session.factory.resume(
|
|
154
|
+
runId: string,
|
|
155
|
+
options?: { limits?: FactoryLimits },
|
|
156
|
+
): Promise<FactoryRunResult>;
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Both resolve with the run envelope (`FactoryRunResult`) for **every** outcome — `completed`, `error`, `halted`, and `cancelled` alike. Inspect `status` and read `result` only when the run completed; a limit breach carries a typed `failure`. A declined fresh run is not a pre-execution failure: the run row already exists by the time the prompt is answered, so it resolves with a terminal `cancelled` envelope carrying the run ID. Only failures that occur *before* a run exists reject: an unknown factory name or an already-active session. Pre-execution resume failures, including a declined reapproval, throw `FactoryResumeError`, whose `code` is one of `not_found`, `non_resumable`, `already_active`, `reapproval_declined`, or `no_approval_provider`.
|
|
160
|
+
|
|
161
|
+
An agent that no longer has a prior run's ID in context can recover it with `factories_manage` and `operation: "runs"`, which lists the session's factory runs with their IDs and statuses. This matters for resume: a run that reached a limit keeps its journal, so resuming it replays completed work for free, while restarting it from scratch pays for that work twice.
|
|
162
|
+
|
|
163
|
+
The agent-facing `run_factory` tool has exactly two input branches:
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
{ name: string; args?: JsonValue; limits?: FactoryLimits }
|
|
167
|
+
{ resumeFromRunId: string; limits?: FactoryLimits }
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Authoring a factory from inside a session
|
|
171
|
+
|
|
172
|
+
The agent-facing `factories_manage` tool writes a factory into a session-scoped extension at runtime with `operation: "author"`. The rules above all apply, plus one constraint that does not affect an extension author.
|
|
173
|
+
|
|
174
|
+
**The `run` body is self-contained.** It is emitted verbatim into a generated module as a single async function expression. It closes over nothing: not the conversation that authored it, and not any authoring-time binding. Only its own locals, its `ctx` parameter, and standard Node and JavaScript globals are in scope, so every schema, constant, and helper must be defined *inside* the function. The generated module imports the SDK itself; the expression cannot add static `import` statements or use `require`. Load anything else with a dynamic `await import("...")` in the body.
|
|
175
|
+
|
|
176
|
+
```js
|
|
177
|
+
async ({ args, agent, phase }) => {
|
|
178
|
+
// Defined inside — there is no outer scope to close over.
|
|
179
|
+
const VERDICT = { type: "object", properties: { real: { type: "boolean" } }, required: ["real"] };
|
|
180
|
+
|
|
181
|
+
phase("Inspect");
|
|
182
|
+
const finding = await agent(`Name one likely bug in ${args.file ?? "the code"}.`, {
|
|
183
|
+
label: "inspector",
|
|
184
|
+
});
|
|
185
|
+
if (!finding) return { finding: null, real: false };
|
|
186
|
+
|
|
187
|
+
phase("Verify");
|
|
188
|
+
const verdict = await agent(`Is this a real bug? Claim: ${finding}`, {
|
|
189
|
+
label: "verifier",
|
|
190
|
+
schema: VERDICT,
|
|
191
|
+
});
|
|
192
|
+
return { finding, real: verdict?.real === true };
|
|
193
|
+
};
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Authoring registers the factory but does not run it. Invoke it afterwards with `run_factory`. Use `factories_manage` with `operation: "list"` to see the factories already registered in the session and `operation: "inspect"` to read one factory's description, phases, and limits before running it.
|
|
197
|
+
|
|
198
|
+
## Observe a run
|
|
199
|
+
|
|
200
|
+
The calling session can inspect its own factory runs:
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const runs = await session.factory.listRuns();
|
|
204
|
+
const detail = await session.factory.getRunDetail(runId);
|
|
205
|
+
const page = await session.factory.getRunProgress(runId, {
|
|
206
|
+
phaseId,
|
|
207
|
+
afterSeq,
|
|
208
|
+
beforeSeq,
|
|
209
|
+
limit,
|
|
210
|
+
});
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
- `listRuns()` returns summaries in durable creation order.
|
|
214
|
+
- `getRunDetail(runId)` returns phases, prompt-safe agent summaries, and the latest progress page.
|
|
215
|
+
- `getRunProgress(runId, options?)` pages progress forward, backward, by phase, or from the latest tail.
|
|
216
|
+
|
|
217
|
+
`getRun(runId)` reads the latest run envelope, and `cancel(runId)` cancels a run and returns its terminal envelope.
|
|
218
|
+
|
|
219
|
+
`waitForRun(runId, options?)` resolves with the terminal envelope once the run settles into `completed`, `error`, `halted`, or `cancelled`, and resolves immediately when it has already settled:
|
|
220
|
+
|
|
221
|
+
```ts
|
|
222
|
+
const settled = await session.factory.waitForRun(runId);
|
|
223
|
+
if (settled.status === "completed") {
|
|
224
|
+
console.log(settled.result);
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
It watches `factory.run_updated` and re-reads the durable envelope on each invalidation, collapsing a burst of events into a single in-flight read. A low-frequency periodic re-read runs alongside the subscription, so a dropped or missing invalidation degrades into a slightly late resolution rather than an unbounded wait. Pass a `signal` to stop waiting:
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const controller = new AbortController();
|
|
232
|
+
setTimeout(() => controller.abort(), 30_000);
|
|
233
|
+
const settled = await session.factory.waitForRun(runId, { signal: controller.signal });
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
Aborting rejects the wait and has no effect on the run, which keeps executing — use `cancel(runId)` to actually stop it. Because a terminal envelope is final, the resolved value never changes afterwards. `isFactoryRunTerminal(status)` exposes the same terminal-status test for callers driving their own loop.
|
|
237
|
+
|
|
238
|
+
Listen for the ephemeral `factory.run_updated` event. Its `{ runId, revision }` payload is an invalidation signal. Re-read the desired API when a newer monotonic revision arrives.
|
|
239
|
+
|
|
240
|
+
Revisions cover durable lifecycle, accounting, phase, agent, and progress changes. Continuous read-time fields can change without a new revision. These include `observedAt`, active-time calculations, live counts, and a live agent's status or prompt-safe activity text. Factory prompts are never exposed by these APIs. A run is visible only through the session that owns it.
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# Agent Factory patterns
|
|
2
|
+
|
|
3
|
+
Composable orchestration patterns built on the factory context. Read [factories.md](./factories.md) first for the API and its semantics. The API is experimental.
|
|
4
|
+
|
|
5
|
+
Every snippet below assumes the surrounding `async (ctx) => { ... }` run body and destructures the hooks it uses. Three rules apply throughout, because breaking them fails silently:
|
|
6
|
+
|
|
7
|
+
- **Give every independent subagent a unique `label`.** Identical prompt-and-options pairs memoize into a single shared subagent.
|
|
8
|
+
- **Guard every `agent()` result.** An ordinary failure resolves to `null` rather than throwing.
|
|
9
|
+
- **Filter with `v => v !== null`,** not `Boolean`, which also discards a valid `false`, `0`, or `""`.
|
|
10
|
+
|
|
11
|
+
## Multi-stage review
|
|
12
|
+
|
|
13
|
+
The default shape: fan out across dimensions, and let each dimension verify as soon as its own review lands. No barrier, so a slow dimension never holds up a fast one.
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
async ({ pipeline, parallel, agent, phase, log }) => {
|
|
17
|
+
const FINDINGS = {
|
|
18
|
+
type: "object",
|
|
19
|
+
properties: {
|
|
20
|
+
findings: {
|
|
21
|
+
type: "array",
|
|
22
|
+
items: {
|
|
23
|
+
type: "object",
|
|
24
|
+
properties: { title: { type: "string" } },
|
|
25
|
+
required: ["title"],
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
required: ["findings"],
|
|
30
|
+
};
|
|
31
|
+
const VERDICT = {
|
|
32
|
+
type: "object",
|
|
33
|
+
properties: { isReal: { type: "boolean" } },
|
|
34
|
+
required: ["isReal"],
|
|
35
|
+
};
|
|
36
|
+
const DIMENSIONS = [
|
|
37
|
+
{ key: "bugs", prompt: "Review the diff for correctness bugs. Return JSON {findings:[{title}]}." },
|
|
38
|
+
{ key: "perf", prompt: "Review the diff for performance issues. Return JSON {findings:[{title}]}." },
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
phase("Review"); // Run-global: set it before the fan-out, never inside a stage.
|
|
42
|
+
const perDimension = await pipeline(
|
|
43
|
+
DIMENSIONS,
|
|
44
|
+
(d) => agent(d.prompt, { label: `review:${d.key}`, schema: FINDINGS }),
|
|
45
|
+
(review, d) => {
|
|
46
|
+
if (!review) {
|
|
47
|
+
log(`review:${d.key} produced nothing`);
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
return parallel(
|
|
51
|
+
(review.findings ?? []).map((f, i) => () =>
|
|
52
|
+
agent(`Adversarially verify this finding is real: ${f.title}`, {
|
|
53
|
+
label: `verify:${d.key}:${i}`,
|
|
54
|
+
schema: VERDICT,
|
|
55
|
+
}).then((v) => (v && v.isReal ? f : null))
|
|
56
|
+
)
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return { confirmed: perDimension.flat().filter((v) => v !== null) };
|
|
62
|
+
};
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## When a barrier is correct
|
|
66
|
+
|
|
67
|
+
Deduplicating across every finding needs the whole set in hand, so the barrier earns its cost here. Dedup itself is plain JavaScript, done in the body between the two fan-outs. This excerpt reuses `FINDINGS`, `VERDICT`, and `DIMENSIONS` from the previous example — define them inside your own function.
|
|
68
|
+
|
|
69
|
+
```js
|
|
70
|
+
const all = await parallel(
|
|
71
|
+
DIMENSIONS.map((d) => () => agent(d.prompt, { label: `find:${d.key}`, schema: FINDINGS }))
|
|
72
|
+
);
|
|
73
|
+
const findings = all.filter((v) => v !== null).flatMap((r) => r.findings ?? []);
|
|
74
|
+
const deduped = [...new Map(findings.map((f) => [f.title, f])).values()]; // Needs all of them.
|
|
75
|
+
const verified = await parallel(
|
|
76
|
+
deduped.map((f, i) => () => agent(`Verify: ${f.title}`, { label: `verify:${i}`, schema: VERDICT }))
|
|
77
|
+
);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Loop until count
|
|
81
|
+
|
|
82
|
+
Accumulate toward a target. Each iteration needs a unique identity — a unique label plus a prompt that excludes what has already been found — a bounded attempt count, and a null guard.
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
const BUG = {
|
|
86
|
+
type: "object",
|
|
87
|
+
properties: { title: { type: "string" } },
|
|
88
|
+
required: ["title"],
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const bugs = [];
|
|
92
|
+
let attempt = 0;
|
|
93
|
+
while (bugs.length < 10 && attempt < 30) {
|
|
94
|
+
const r = await agent(
|
|
95
|
+
`Find ONE distinct bug NOT already listed: ${JSON.stringify(bugs.map((b) => b.title))}. Return JSON {title}.`,
|
|
96
|
+
{ label: `finder:${attempt}`, schema: BUG }
|
|
97
|
+
);
|
|
98
|
+
attempt++;
|
|
99
|
+
if (r && r.title) bugs.push(r);
|
|
100
|
+
log(`${bugs.length}/10 found`);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Loop until dry
|
|
105
|
+
|
|
106
|
+
Keep spawning finders until some number of consecutive rounds surface nothing new. Deduplicate against everything *seen*, not just what was kept, or discarded findings resurface every round.
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const BUGS = {
|
|
110
|
+
type: "object",
|
|
111
|
+
properties: {
|
|
112
|
+
bugs: {
|
|
113
|
+
type: "array",
|
|
114
|
+
items: { type: "object", properties: { title: { type: "string" } }, required: ["title"] },
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
required: ["bugs"],
|
|
118
|
+
};
|
|
119
|
+
const VERDICT = {
|
|
120
|
+
type: "object",
|
|
121
|
+
properties: { real: { type: "boolean" } },
|
|
122
|
+
required: ["real"],
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const seen = new Set();
|
|
126
|
+
const confirmed = [];
|
|
127
|
+
const keyOf = (b) => b.title.toLowerCase();
|
|
128
|
+
let dry = 0;
|
|
129
|
+
let round = 0;
|
|
130
|
+
|
|
131
|
+
while (dry < 2 && round < 20) {
|
|
132
|
+
const found = (
|
|
133
|
+
await parallel(
|
|
134
|
+
[0, 1, 2].map((i) => () =>
|
|
135
|
+
agent(`Find bugs (finder ${i}, round ${round}). Return JSON {bugs:[{title}]}.`, {
|
|
136
|
+
label: `find:${round}:${i}`,
|
|
137
|
+
schema: BUGS,
|
|
138
|
+
})
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
)
|
|
142
|
+
.filter((v) => v !== null)
|
|
143
|
+
.flatMap((r) => r.bugs ?? []);
|
|
144
|
+
|
|
145
|
+
const fresh = found.filter((b) => {
|
|
146
|
+
const k = keyOf(b);
|
|
147
|
+
if (seen.has(k)) return false;
|
|
148
|
+
seen.add(k);
|
|
149
|
+
return true;
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
if (!fresh.length) {
|
|
153
|
+
dry++;
|
|
154
|
+
round++;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
dry = 0;
|
|
158
|
+
|
|
159
|
+
const judged = await parallel(
|
|
160
|
+
fresh.map((b, i) => () =>
|
|
161
|
+
parallel(
|
|
162
|
+
["correctness", "security", "repro"].map((lens) => () =>
|
|
163
|
+
agent(`Judge via ${lens}: is "${b.title}" real? Return JSON {real}.`, {
|
|
164
|
+
label: `judge:${round}:${i}:${lens}`,
|
|
165
|
+
schema: VERDICT,
|
|
166
|
+
})
|
|
167
|
+
)
|
|
168
|
+
).then((vs) => ({ b, real: vs.filter((v) => v !== null).filter((v) => v.real).length >= 2 }))
|
|
169
|
+
)
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
confirmed.push(...judged.filter((v) => v !== null && v.real).map((v) => v.b));
|
|
173
|
+
round++;
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Quality patterns
|
|
178
|
+
|
|
179
|
+
Compose these freely.
|
|
180
|
+
|
|
181
|
+
- **Adversarial verify.** Spawn several independent skeptics per finding, each prompted to *refute* it and to default to refuted when uncertain. Keep only what a majority fails to refute.
|
|
182
|
+
- **Perspective-diverse verify.** Give each verifier a distinct lens — correctness, security, performance, does-it-reproduce — instead of several identical skeptics. The distinct prompts also stop them memoizing into one subagent.
|
|
183
|
+
- **Judge panel.** Generate several independent attempts from different angles, score them with parallel judges, then synthesize from the winner while grafting the best ideas from the runners-up.
|
|
184
|
+
- **Multi-modal sweep.** Run parallel searchers that each look a different way: by container, by content, by entity, by time.
|
|
185
|
+
- **Completeness critic.** End with an agent asking what is missing — an angle not run, a claim unverified, a source unread — and use its answer to seed the next round.
|
|
186
|
+
- **No silent caps.** When the factory bounds its own coverage with a top-N, a sampling step, or a no-retry rule, `log()` what was dropped.
|
|
187
|
+
|
|
188
|
+
## Scaling
|
|
189
|
+
|
|
190
|
+
Match the orchestration to what was asked. A quick check wants a couple of subagents and single-vote verification; a request to be thorough or comprehensive wants a larger finder pool, a three-to-five vote adversarial pass, and a synthesis stage.
|
|
191
|
+
|
|
192
|
+
There is no in-script budget object. Scale with your own counters, as in the loop patterns above, and treat the declared limits as the safety ceiling rather than the control mechanism. Only `agent()` spawns are throttled, by `maxConcurrentSubagents` falling back to `maxTotalSubagents`; with neither declared there is no built-in concurrency cap, so declare one before fanning out widely. `parallel` itself is `Promise.all`, so non-agent work in a thunk runs fully concurrently regardless.
|
|
193
|
+
|
|
194
|
+
These patterns are not exhaustive. Compose novel harnesses — tournament brackets, self-repair loops, staged escalation — when the task calls for it.
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"type": "git",
|
|
5
5
|
"url": "https://github.com/github/copilot-sdk.git"
|
|
6
6
|
},
|
|
7
|
-
"version": "1.0.9-preview.
|
|
7
|
+
"version": "1.0.9-preview.1",
|
|
8
8
|
"description": "TypeScript SDK for programmatic control of GitHub Copilot CLI via JSON-RPC",
|
|
9
9
|
"main": "./dist/cjs/index.js",
|
|
10
10
|
"types": "./dist/index.d.ts",
|