copperhead 0.9.0 → 0.11.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/NOTICE +1 -1
- package/README.md +47 -8
- package/dist/agent/context.js +2 -0
- package/dist/agent/context.js.map +1 -0
- package/dist/agent/dock-renderer.js +2 -2
- package/dist/agent/dock-renderer.js.map +1 -1
- package/dist/agent/envelope.js +105 -0
- package/dist/agent/envelope.js.map +1 -0
- package/dist/agent/filetools.js +24 -1
- package/dist/agent/filetools.js.map +1 -1
- package/dist/agent/ledger.js +24 -0
- package/dist/agent/ledger.js.map +1 -1
- package/dist/agent/loop.js +63 -72
- package/dist/agent/loop.js.map +1 -1
- package/dist/agent/prompts.js +4 -3
- package/dist/agent/prompts.js.map +1 -1
- package/dist/agent/providers/claude-code.js +17 -1
- package/dist/agent/providers/claude-code.js.map +1 -1
- package/dist/agent/providers/codex.js +84 -39
- package/dist/agent/providers/codex.js.map +1 -1
- package/dist/agent/providers/tool-protocol.js +21 -0
- package/dist/agent/providers/tool-protocol.js.map +1 -1
- package/dist/agent/recovery.js +186 -15
- package/dist/agent/recovery.js.map +1 -1
- package/dist/agent/registry.js +49 -0
- package/dist/agent/registry.js.map +1 -0
- package/dist/agent/render.js +2 -2
- package/dist/agent/render.js.map +1 -1
- package/dist/agent/theme.js +10 -5
- package/dist/agent/theme.js.map +1 -1
- package/dist/agent/tools.js +100 -586
- package/dist/agent/tools.js.map +1 -1
- package/dist/agent/transcript.js +2 -0
- package/dist/agent/transcript.js.map +1 -1
- package/dist/capabilities/define.js +35 -0
- package/dist/capabilities/define.js.map +1 -0
- package/dist/capabilities/handlers.js +744 -0
- package/dist/capabilities/handlers.js.map +1 -0
- package/dist/capabilities/helpers.js +39 -0
- package/dist/capabilities/helpers.js.map +1 -0
- package/dist/capabilities/index.js +50 -0
- package/dist/capabilities/index.js.map +1 -0
- package/dist/capabilities/skills/generate-report.js +23 -0
- package/dist/capabilities/skills/generate-report.js.map +1 -0
- package/dist/cli.js +158 -0
- package/dist/cli.js.map +1 -1
- package/dist/commands/check.js +33 -1
- package/dist/commands/check.js.map +1 -1
- package/dist/commands/create.js +181 -26
- package/dist/commands/create.js.map +1 -1
- package/dist/commands/doctor.js +83 -6
- package/dist/commands/doctor.js.map +1 -1
- package/dist/commands/skill.js +109 -0
- package/dist/commands/skill.js.map +1 -0
- package/dist/commands/sync.js +3 -1
- package/dist/commands/sync.js.map +1 -1
- package/dist/config.js +19 -6
- package/dist/config.js.map +1 -1
- package/dist/kicad/bootstrap.js +24 -3
- package/dist/kicad/bootstrap.js.map +1 -1
- package/dist/kicad/cli.js +106 -18
- package/dist/kicad/cli.js.map +1 -1
- package/dist/kicad/dossier.js +207 -0
- package/dist/kicad/dossier.js.map +1 -0
- package/dist/kicad/draft/draft.js +135 -0
- package/dist/kicad/draft/draft.js.map +1 -0
- package/dist/kicad/draft/engine.js +5310 -0
- package/dist/kicad/draft/engine.js.map +1 -0
- package/dist/kicad/draft/ir.js +368 -0
- package/dist/kicad/draft/ir.js.map +1 -0
- package/dist/kicad/draft/symsource.js +504 -0
- package/dist/kicad/draft/symsource.js.map +1 -0
- package/dist/kicad/emit.js +220 -0
- package/dist/kicad/emit.js.map +1 -0
- package/dist/kicad/fab.js +13 -0
- package/dist/kicad/fab.js.map +1 -1
- package/dist/kicad/legibility.js +608 -0
- package/dist/kicad/legibility.js.map +1 -0
- package/dist/kicad/score.js +431 -0
- package/dist/kicad/score.js.map +1 -0
- package/dist/kicad/sexp.js +265 -6
- package/dist/kicad/sexp.js.map +1 -1
- package/dist/kicad/symlib.js +346 -16
- package/dist/kicad/symlib.js.map +1 -1
- package/dist/mcp/server.js +485 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/memory/bom-table.js +75 -39
- package/dist/memory/bom-table.js.map +1 -1
- package/dist/memory/scaffold.js +14 -1
- package/dist/memory/scaffold.js.map +1 -1
- package/dist/util/redact.js +6 -0
- package/dist/util/redact.js.map +1 -1
- package/package.json +13 -8
- package/src/agent/context.ts +35 -0
- package/src/agent/dock-renderer.ts +3 -2
- package/src/agent/envelope.ts +124 -0
- package/src/agent/filetools.ts +26 -1
- package/src/agent/ledger.ts +24 -0
- package/src/agent/loop.ts +73 -78
- package/src/agent/prompts.ts +4 -3
- package/src/agent/providers/claude-code.ts +22 -1
- package/src/agent/providers/codex.ts +91 -42
- package/src/agent/providers/tool-protocol.ts +22 -0
- package/src/agent/recovery.ts +183 -13
- package/src/agent/registry.ts +58 -0
- package/src/agent/render.ts +4 -3
- package/src/agent/theme.ts +15 -5
- package/src/agent/tools.ts +124 -628
- package/src/agent/transcript.ts +6 -0
- package/src/agent/types.ts +10 -5
- package/src/capabilities/define.ts +88 -0
- package/src/capabilities/handlers.ts +769 -0
- package/src/capabilities/helpers.ts +37 -0
- package/src/capabilities/index.ts +53 -0
- package/src/capabilities/skills/generate-report.ts +25 -0
- package/src/cli.ts +154 -0
- package/src/commands/check.ts +51 -1
- package/src/commands/create.ts +183 -21
- package/src/commands/doctor.ts +85 -6
- package/src/commands/skill.ts +127 -0
- package/src/commands/sync.ts +5 -3
- package/src/config.ts +56 -8
- package/src/kicad/bootstrap.ts +24 -3
- package/src/kicad/cli.ts +129 -18
- package/src/kicad/dossier.ts +217 -0
- package/src/kicad/draft/draft.ts +173 -0
- package/src/kicad/draft/engine.ts +5274 -0
- package/src/kicad/draft/ir.ts +416 -0
- package/src/kicad/draft/symsource.ts +549 -0
- package/src/kicad/emit.ts +300 -0
- package/src/kicad/fab.ts +15 -0
- package/src/kicad/legibility.ts +695 -0
- package/src/kicad/score.ts +502 -0
- package/src/kicad/sexp.ts +346 -6
- package/src/kicad/symlib.ts +364 -18
- package/src/mcp/server.ts +560 -0
- package/src/memory/bom-table.ts +85 -38
- package/src/memory/scaffold.ts +14 -1
- package/src/util/redact.ts +6 -0
- package/dist/memory/synap.js +0 -152
- package/dist/memory/synap.js.map +0 -1
- package/src/memory/synap.ts +0 -217
package/src/agent/tools.ts
CHANGED
|
@@ -1,643 +1,139 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import type
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { verifySchematicSymbols } from '../kicad/symlib.js';
|
|
10
|
-
import { checkDrift } from '../memory/drift.js';
|
|
11
|
-
import { saveConstraint, classifyAffectsTarget, affectsTargetExists } from '../memory/constraints.js';
|
|
12
|
-
import { openspecValidate } from '../openspec/cli.js';
|
|
13
|
-
import { existsSync } from 'node:fs';
|
|
14
|
-
import type { CopperheadConfig } from '../config.js';
|
|
15
|
-
import { ObligationsLedger } from './ledger.js';
|
|
16
|
-
import type { Transcript } from './transcript.js';
|
|
1
|
+
import type { CatalogEntry, CatalogSkill } from '../capabilities/index.js';
|
|
2
|
+
import { corruptionError } from '../capabilities/helpers.js';
|
|
3
|
+
import { flatten, failResult, seal, unavailable, type ToolResult } from './envelope.js';
|
|
4
|
+
import { registry } from './registry.js';
|
|
5
|
+
import { withRetry, isRateLimit } from '../util/retry.js';
|
|
6
|
+
import { MAX_TURN_TIMEOUTS, TurnTimeoutError, withWatchdog } from './recovery.js';
|
|
7
|
+
import type { RunContext } from './context.js';
|
|
8
|
+
import type { Msg, Provider, Turn } from './types.js';
|
|
17
9
|
|
|
18
|
-
export
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
10
|
+
export type { RunContext, FinishRequest } from './context.js';
|
|
11
|
+
export type { CatalogEntry, CatalogSkill };
|
|
12
|
+
export { corruptionError, registry };
|
|
22
13
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
repoRoot: string;
|
|
26
|
-
config: CopperheadConfig;
|
|
27
|
-
transcript: Transcript;
|
|
28
|
-
ledger: ObligationsLedger;
|
|
29
|
-
runId: string;
|
|
30
|
-
interactive: boolean;
|
|
31
|
-
confirm: (question: string) => Promise<boolean>;
|
|
32
|
-
editsUnlocked: boolean;
|
|
33
|
-
changeId: string | null;
|
|
34
|
-
proposalValidated: boolean;
|
|
35
|
-
filesTouched: Set<string>;
|
|
36
|
-
decisions: string[];
|
|
37
|
-
lastErc: CheckReport | null;
|
|
38
|
-
lastDrc: CheckReport | null;
|
|
39
|
-
repairCycles: number;
|
|
40
|
-
finishRequest: FinishRequest | null;
|
|
14
|
+
export function availableTools(ctx: RunContext): CatalogEntry[] {
|
|
15
|
+
return registry.list(ctx);
|
|
41
16
|
}
|
|
42
17
|
|
|
43
|
-
export interface
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
|
|
47
|
-
handler: (ctx: RunContext, args: Record<string, unknown>) => Promise<string>;
|
|
18
|
+
export interface DispatchOpts {
|
|
19
|
+
provider?: Provider;
|
|
20
|
+
/** Skill sub-run: only these names may dispatch (never `finish`). */
|
|
21
|
+
only?: ReadonlySet<string>;
|
|
48
22
|
}
|
|
49
23
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
24
|
+
export async function dispatchToolResult(
|
|
25
|
+
ctx: RunContext,
|
|
26
|
+
name: string,
|
|
27
|
+
args: Record<string, unknown>,
|
|
28
|
+
opts: DispatchOpts = {},
|
|
29
|
+
): Promise<ToolResult> {
|
|
30
|
+
if (opts.only && !opts.only.has(name)) return unavailable(name, ctx.editsUnlocked, 'not part of this skill');
|
|
31
|
+
const entry = availableTools(ctx).find((e) => e.name === name);
|
|
32
|
+
if (!entry) return unavailable(name, ctx.editsUnlocked);
|
|
33
|
+
try {
|
|
34
|
+
if (entry.kind === 'skill') {
|
|
35
|
+
if (!opts.provider) return failResult('validation', `skill "${name}" requires a provider`, 'diagnostic');
|
|
36
|
+
return await runSkillSubRun({ ctx, skill: entry, args, provider: opts.provider });
|
|
37
|
+
}
|
|
38
|
+
// Unified retry policy (design D10): a thrown 429 backs off through the
|
|
39
|
+
// same withRetry/isRateLimit pair the provider path uses. Typed envelope
|
|
40
|
+
// failures (validation/refusal/unavailable) return, so they never retry;
|
|
41
|
+
// local tool errors are not 429s, so they surface on the first throw.
|
|
42
|
+
return await withRetry(() => entry.handler(ctx, args), { isRetryable: isRateLimit, baseMs: 250 });
|
|
43
|
+
} catch (err) {
|
|
44
|
+
return failResult('exception', `error: ${(err as Error).message}`);
|
|
45
|
+
}
|
|
72
46
|
}
|
|
73
47
|
|
|
74
|
-
function
|
|
75
|
-
ctx
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
ctx.ledger.onDocEdit(rel);
|
|
82
|
-
}
|
|
48
|
+
export async function dispatchTool(
|
|
49
|
+
ctx: RunContext,
|
|
50
|
+
name: string,
|
|
51
|
+
args: Record<string, unknown>,
|
|
52
|
+
opts: DispatchOpts = {},
|
|
53
|
+
): Promise<string> {
|
|
54
|
+
return flatten(await dispatchToolResult(ctx, name, args, opts));
|
|
83
55
|
}
|
|
84
56
|
|
|
85
|
-
export
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
schema: {
|
|
132
|
-
name: 'list_symbols',
|
|
133
|
-
description: 'List schematic symbols: ref, value, footprint, sheet.',
|
|
134
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
135
|
-
},
|
|
136
|
-
requiresUnlock: false,
|
|
137
|
-
handler: async (ctx) => {
|
|
138
|
-
if (!ctx.config.schematic) return 'no schematic configured';
|
|
139
|
-
const syms = await listSymbols(path.join(ctx.repoRoot, ctx.config.schematic));
|
|
140
|
-
return JSON.stringify(syms.map(({ ref, value, footprint, sheet }) => ({ ref, value, footprint, sheet })), null, 2);
|
|
141
|
-
},
|
|
142
|
-
},
|
|
143
|
-
{
|
|
144
|
-
schema: {
|
|
145
|
-
name: 'list_nets',
|
|
146
|
-
description: 'List net names found in the schematic.',
|
|
147
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
148
|
-
},
|
|
149
|
-
requiresUnlock: false,
|
|
150
|
-
handler: async (ctx) => {
|
|
151
|
-
if (!ctx.config.schematic) return 'no schematic configured';
|
|
152
|
-
return JSON.stringify(await listNets(path.join(ctx.repoRoot, ctx.config.schematic)));
|
|
153
|
-
},
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
schema: {
|
|
157
|
-
name: 'propose_change',
|
|
158
|
-
description:
|
|
159
|
-
'Write the OpenSpec change proposal for this run (the plan step). Must be called and validated before edit tools unlock.',
|
|
160
|
-
parameters: {
|
|
161
|
-
type: 'object',
|
|
162
|
-
properties: {
|
|
163
|
-
id: { type: 'string', description: 'kebab-case change id' },
|
|
164
|
-
why: { type: 'string' },
|
|
165
|
-
what_changes: { type: 'string', description: 'markdown bullet list of changes' },
|
|
166
|
-
tasks: { type: 'string', description: 'markdown checklist of implementation steps' },
|
|
167
|
-
},
|
|
168
|
-
required: ['id', 'why', 'what_changes', 'tasks'],
|
|
169
|
-
},
|
|
170
|
-
},
|
|
171
|
-
requiresUnlock: false,
|
|
172
|
-
handler: async (ctx, args) => {
|
|
173
|
-
const id = str(args, 'id');
|
|
174
|
-
const dir = resolveInRepo(ctx.repoRoot, path.join('openspec', 'changes', id));
|
|
175
|
-
await mkdir(dir, { recursive: true });
|
|
176
|
-
const auto = ctx.interactive ? '' : '\n> Marker: AUTO (autonomous mode; auto-approved, reviewable after the fact)\n';
|
|
177
|
-
await writeFile(
|
|
178
|
-
path.join(dir, 'proposal.md'),
|
|
179
|
-
`# Proposal: ${id}\n${auto}\n## Why\n\n${str(args, 'why')}\n\n## What Changes\n\n${str(args, 'what_changes')}\n`,
|
|
180
|
-
'utf8',
|
|
181
|
-
);
|
|
182
|
-
await writeFile(path.join(dir, 'tasks.md'), `# Tasks\n\n${str(args, 'tasks')}\n`, 'utf8');
|
|
183
|
-
ctx.changeId = id;
|
|
184
|
-
return `proposal written to openspec/changes/${id}/ — now call validate_change`;
|
|
185
|
-
},
|
|
186
|
-
},
|
|
187
|
-
{
|
|
188
|
-
schema: {
|
|
189
|
-
name: 'validate_change',
|
|
190
|
-
description: 'Validate the current change proposal; on success the edit tools unlock.',
|
|
191
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
192
|
-
},
|
|
193
|
-
requiresUnlock: false,
|
|
194
|
-
handler: async (ctx) => {
|
|
195
|
-
if (!ctx.changeId) return 'no proposal yet: call propose_change first';
|
|
196
|
-
let ok: boolean;
|
|
197
|
-
let detail: string;
|
|
198
|
-
if (existsSync(path.join(ctx.repoRoot, 'openspec', 'config.yaml'))) {
|
|
199
|
-
const res = await openspecValidate(ctx.repoRoot, ctx.changeId);
|
|
200
|
-
ok = res.ok;
|
|
201
|
-
detail = res.output;
|
|
202
|
-
} else {
|
|
203
|
-
// No OpenSpec workspace in the target repo: structural validation of the
|
|
204
|
-
// proposal files themselves (the invariant is the gate, not the CLI).
|
|
205
|
-
const dir = path.join(ctx.repoRoot, 'openspec', 'changes', ctx.changeId);
|
|
206
|
-
ok = existsSync(path.join(dir, 'proposal.md')) && existsSync(path.join(dir, 'tasks.md'));
|
|
207
|
-
detail = ok ? 'structural validation passed (no openspec workspace)' : 'proposal files missing';
|
|
208
|
-
}
|
|
209
|
-
if (!ok) return `validation FAILED:\n${detail}`;
|
|
210
|
-
ctx.proposalValidated = true;
|
|
211
|
-
if (ctx.interactive) {
|
|
212
|
-
const approved = await ctx.confirm(`Proposal ${ctx.changeId} validated. Unlock edit tools and proceed?`);
|
|
213
|
-
if (!approved) return 'proposal validated but human declined; edits remain locked';
|
|
214
|
-
}
|
|
215
|
-
ctx.editsUnlocked = true;
|
|
216
|
-
await ctx.transcript.event('edit-tools-unlocked', { changeId: ctx.changeId });
|
|
217
|
-
return `validation passed; edit tools are now unlocked`;
|
|
218
|
-
},
|
|
219
|
-
},
|
|
220
|
-
{
|
|
221
|
-
schema: {
|
|
222
|
-
name: 'edit_file',
|
|
223
|
-
description:
|
|
224
|
-
'Exact-match anchored replace in an existing file. Requires a validated change proposal first (call propose_change then validate_change to unlock edits; both may be in the same reply, before this call). The anchor must be unique; widen it with surrounding lines if not. For renames, pass replace_all: true to replace every occurrence in one call.',
|
|
225
|
-
parameters: {
|
|
226
|
-
type: 'object',
|
|
227
|
-
properties: {
|
|
228
|
-
path: { type: 'string' },
|
|
229
|
-
old_string: { type: 'string' },
|
|
230
|
-
new_string: { type: 'string' },
|
|
231
|
-
replace_all: { type: 'boolean' },
|
|
232
|
-
},
|
|
233
|
-
required: ['path', 'old_string', 'new_string'],
|
|
234
|
-
},
|
|
235
|
-
},
|
|
236
|
-
requiresUnlock: true,
|
|
237
|
-
handler: async (ctx, args) => {
|
|
238
|
-
const corrupt = corruptionError({ new_string: args.new_string });
|
|
239
|
-
if (corrupt) return corrupt;
|
|
240
|
-
const rel = str(args, 'path');
|
|
241
|
-
const abs = resolveInRepo(ctx.repoRoot, rel);
|
|
242
|
-
// Text edits can corrupt an s-expression file in ways the editor cannot
|
|
243
|
-
// see; a corrupted file then fails every later ERC/DRC with an opaque
|
|
244
|
-
// error. Validate loadability with KiCad itself and roll the edit back
|
|
245
|
-
// rather than letting the file drift unusable. Only schematics and
|
|
246
|
-
// boards are probeable; .kicad_pro/.kicad_sym/.kicad_mod edits must not
|
|
247
|
-
// be probed (a sch/pcb probe rejects them wholesale).
|
|
248
|
-
const before = isProbeableKicadFile(rel) ? await readFile(abs, 'utf8') : null;
|
|
249
|
-
const res = await toolEditFile(
|
|
250
|
-
ctx.repoRoot,
|
|
251
|
-
rel,
|
|
252
|
-
str(args, 'old_string'),
|
|
253
|
-
args.new_string as string,
|
|
254
|
-
args.replace_all === true,
|
|
255
|
-
);
|
|
256
|
-
if (before !== null) {
|
|
257
|
-
const loadErr = await kicadLoadError(abs);
|
|
258
|
-
if (loadErr) {
|
|
259
|
-
const after = await readFile(abs, 'utf8');
|
|
260
|
-
await writeFile(abs, before, 'utf8');
|
|
261
|
-
if (await kicadLoadError(abs)) {
|
|
262
|
-
// The file was already unloadable before this edit. Reverting
|
|
263
|
-
// would deadlock incremental repair (every partial fix undone
|
|
264
|
-
// unless one edit fixes the whole file), so keep the edit and
|
|
265
|
-
// keep the pressure on with the probe output.
|
|
266
|
-
await writeFile(abs, after, 'utf8');
|
|
267
|
-
markTouched(ctx, rel);
|
|
268
|
-
return `${res}\nnote: ${rel} was already unloadable before this edit, so the edit is KEPT. Keep repairing until it loads. kicad-cli says:\n${loadErr}`;
|
|
57
|
+
export async function runSkillSubRun(opts: {
|
|
58
|
+
ctx: RunContext;
|
|
59
|
+
skill: CatalogSkill;
|
|
60
|
+
args: Record<string, unknown>;
|
|
61
|
+
provider: Provider;
|
|
62
|
+
}): Promise<ToolResult> {
|
|
63
|
+
const { ctx, skill, args, provider } = opts;
|
|
64
|
+
const savedFinish = ctx.finishRequest;
|
|
65
|
+
const only = new Set(skill.tools.filter((n) => n !== 'finish'));
|
|
66
|
+
const results: { name: string; result: ToolResult }[] = [];
|
|
67
|
+
const messages: Msg[] = [
|
|
68
|
+
{ role: 'system', content: skill.prompt(ctx, args) },
|
|
69
|
+
{ role: 'user', content: 'Execute this skill. Use the available tools, then stop when the report is complete.' },
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
for (let turn = 0; turn < (skill.maxTurns ?? 8); turn++) {
|
|
74
|
+
if (await skill.isComplete(ctx, args)) break;
|
|
75
|
+
const tools = registry.list(ctx).filter((e) => e.kind === 'tool' && only.has(e.name)).map((e) => e.schema);
|
|
76
|
+
let res: Turn;
|
|
77
|
+
let timeoutRetries = 0;
|
|
78
|
+
while (true) {
|
|
79
|
+
try {
|
|
80
|
+
// The main loop's watchdog: streamed progress restarts the idle
|
|
81
|
+
// deadline, and turnMaxMs caps a turn that keeps streaming.
|
|
82
|
+
res = await withRetry(
|
|
83
|
+
() =>
|
|
84
|
+
withWatchdog((activity) => provider.chat(messages, tools, { onStream: () => activity() }), {
|
|
85
|
+
idleMs: ctx.config.turnTimeoutMs,
|
|
86
|
+
maxMs: ctx.config.turnMaxMs,
|
|
87
|
+
onTimeout: () => provider.close?.(),
|
|
88
|
+
}),
|
|
89
|
+
{ isRetryable: isRateLimit, baseMs: 250 },
|
|
90
|
+
);
|
|
91
|
+
break;
|
|
92
|
+
} catch (err) {
|
|
93
|
+
// Only a hung turn retries: one stopped at the hard cap is too large,
|
|
94
|
+
// and resending it would only run into the cap again.
|
|
95
|
+
if (err instanceof TurnTimeoutError && err.kind === 'idle' && timeoutRetries++ < MAX_TURN_TIMEOUTS) {
|
|
96
|
+
await ctx.transcript.event('skill-turn-timeout', {
|
|
97
|
+
skill: skill.name,
|
|
98
|
+
kind: err.kind,
|
|
99
|
+
ms: err.ms,
|
|
100
|
+
attempt: timeoutRetries,
|
|
101
|
+
});
|
|
102
|
+
continue;
|
|
269
103
|
}
|
|
270
|
-
|
|
104
|
+
throw err;
|
|
271
105
|
}
|
|
272
106
|
}
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
return res;
|
|
296
|
-
},
|
|
297
|
-
},
|
|
298
|
-
{
|
|
299
|
-
schema: {
|
|
300
|
-
name: 'run_erc',
|
|
301
|
-
description: 'Run kicad-cli ERC on the schematic. Clears the ERC obligation when clean.',
|
|
302
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
303
|
-
},
|
|
304
|
-
requiresUnlock: false,
|
|
305
|
-
handler: async (ctx) => {
|
|
306
|
-
if (!ctx.config.schematic)
|
|
307
|
-
return 'no schematic configured; ERC does not apply yet — skip it until a schematic exists and is set in .copperhead/config.json';
|
|
308
|
-
const schPath = path.join(ctx.repoRoot, ctx.config.schematic);
|
|
309
|
-
const report = await runErc(schPath);
|
|
310
|
-
ctx.lastErc = report;
|
|
311
|
-
if (report.ok) ctx.ledger.clear('erc');
|
|
312
|
-
else ctx.repairCycles++;
|
|
313
|
-
const out = formatViolations(report);
|
|
314
|
-
// A zero-symbol schematic passes ERC with 0 violations — a false green
|
|
315
|
-
// (3.2) that lets a premature finish look verified (an empty sheet also
|
|
316
|
-
// passes drift). The stage contract already requires symbols>0, but a bare
|
|
317
|
-
// "ERC clean" on the empty starting sheet still misleads the model, so warn
|
|
318
|
-
// here too: no gate should read as satisfied by the empty starting state.
|
|
319
|
-
if (report.ok && !(await listSymbols(schPath)).length) {
|
|
320
|
-
return `${out}\nwarning: ERC is clean but the schematic has ZERO symbols — an empty sheet always passes ERC, so this is NOT a verified design. Capture the parts from BOM.md (and re-run run_erc) before calling finish.`;
|
|
321
|
-
}
|
|
322
|
-
return out;
|
|
323
|
-
},
|
|
324
|
-
},
|
|
325
|
-
{
|
|
326
|
-
schema: {
|
|
327
|
-
name: 'verify_symbols',
|
|
328
|
-
description:
|
|
329
|
-
"Cross-check every lib_symbols entry in the schematic against the KiCad symbol library installed on this machine. Reports pins that diverge from the real part (wrong count, name, or electrical type) and lib_ids that do not exist in the current KiCad version (with the closest real names). ERC cannot catch these — a symbol whose lib_id claims to be a canonical part but whose pins are wrong passes ERC while being wrong. Run this after capturing symbols and reconcile every finding.",
|
|
330
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
331
|
-
},
|
|
332
|
-
requiresUnlock: false,
|
|
333
|
-
handler: async (ctx) => {
|
|
334
|
-
if (!ctx.config.schematic)
|
|
335
|
-
return 'no schematic configured; verify_symbols does not apply yet';
|
|
336
|
-
const { findings, checked, skipped } = await verifySchematicSymbols(
|
|
337
|
-
path.join(ctx.repoRoot, ctx.config.schematic),
|
|
338
|
-
);
|
|
339
|
-
if (!findings.length) {
|
|
340
|
-
return `verify_symbols: ${checked} symbol(s) match the installed KiCad library. No divergences.`;
|
|
341
|
-
}
|
|
342
|
-
const lines = findings.map((f) => ` - [${f.kind}] ${f.detail}`);
|
|
343
|
-
const mismatches = findings.filter((f) => f.kind !== 'no-library').length;
|
|
344
|
-
return `verify_symbols: ${checked} verified, ${skipped} unverifiable (library not installed), ${mismatches} issue(s) to reconcile:\n${lines.join('\n')}`;
|
|
345
|
-
},
|
|
346
|
-
},
|
|
347
|
-
{
|
|
348
|
-
schema: {
|
|
349
|
-
name: 'run_drc',
|
|
350
|
-
description: 'Run kicad-cli DRC on the board. Clears the DRC obligation when clean.',
|
|
351
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
352
|
-
},
|
|
353
|
-
requiresUnlock: false,
|
|
354
|
-
handler: async (ctx) => {
|
|
355
|
-
if (!ctx.config.board)
|
|
356
|
-
return 'no board configured; DRC does not apply yet — skip it until a board exists and is set in .copperhead/config.json';
|
|
357
|
-
const report = await runDrc(path.join(ctx.repoRoot, ctx.config.board));
|
|
358
|
-
ctx.lastDrc = report;
|
|
359
|
-
if (report.ok) ctx.ledger.clear('drc');
|
|
360
|
-
else ctx.repairCycles++;
|
|
361
|
-
return formatViolations(report);
|
|
362
|
-
},
|
|
363
|
-
},
|
|
364
|
-
{
|
|
365
|
-
schema: {
|
|
366
|
-
name: 'export_svg',
|
|
367
|
-
description: 'Export an SVG render of the schematic or board into .copperhead/renders/.',
|
|
368
|
-
parameters: {
|
|
369
|
-
type: 'object',
|
|
370
|
-
properties: { kind: { type: 'string', enum: ['sch', 'pcb'] } },
|
|
371
|
-
required: ['kind'],
|
|
372
|
-
},
|
|
373
|
-
},
|
|
374
|
-
requiresUnlock: false,
|
|
375
|
-
handler: async (ctx, args) => {
|
|
376
|
-
const kind = str(args, 'kind') as 'sch' | 'pcb';
|
|
377
|
-
const file = kind === 'sch' ? ctx.config.schematic : ctx.config.board;
|
|
378
|
-
if (!file) return `no ${kind} configured`;
|
|
379
|
-
const outDir = path.join(ctx.repoRoot, '.copperhead', 'renders');
|
|
380
|
-
await mkdir(outDir, { recursive: true });
|
|
381
|
-
return exportSvg(kind, path.join(ctx.repoRoot, file), outDir);
|
|
382
|
-
},
|
|
383
|
-
},
|
|
384
|
-
{
|
|
385
|
-
schema: {
|
|
386
|
-
name: 'export_outputs',
|
|
387
|
-
description:
|
|
388
|
-
'Export the fabrication package into outputs/: gerbers+drill, DXF outline, STEP, SVG renders. Reports per-artifact success/failure.',
|
|
389
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
390
|
-
},
|
|
391
|
-
requiresUnlock: true,
|
|
392
|
-
handler: async (ctx) => {
|
|
393
|
-
if (!ctx.config.board) return 'no board configured';
|
|
394
|
-
const outDir = path.join(ctx.repoRoot, 'outputs');
|
|
395
|
-
await mkdir(outDir, { recursive: true });
|
|
396
|
-
const res = await exportFab(
|
|
397
|
-
path.join(ctx.repoRoot, ctx.config.board),
|
|
398
|
-
ctx.config.schematic ? path.join(ctx.repoRoot, ctx.config.schematic) : null,
|
|
399
|
-
outDir,
|
|
400
|
-
);
|
|
401
|
-
ctx.filesTouched.add('outputs/');
|
|
402
|
-
const lines = [`produced: ${res.produced.join(', ') || '(none)'}`];
|
|
403
|
-
for (const f of res.failed) lines.push(`FAILED ${f.artifact}: ${f.reason}`);
|
|
404
|
-
return lines.join('\n');
|
|
405
|
-
},
|
|
406
|
-
},
|
|
407
|
-
{
|
|
408
|
-
schema: {
|
|
409
|
-
name: 'check_drift',
|
|
410
|
-
description: 'Compare BOM.md/PINOUT.md tables against the parsed schematic. Clears the drift obligation when clean.',
|
|
411
|
-
parameters: { type: 'object', properties: {}, required: [] },
|
|
412
|
-
},
|
|
413
|
-
requiresUnlock: false,
|
|
414
|
-
handler: async (ctx) => {
|
|
415
|
-
// No schematic yet means there is nothing for the docs to drift against,
|
|
416
|
-
// so the obligation is vacuously satisfied and must be cleared. Returning
|
|
417
|
-
// without clearing deadlocks every docs-only stage of the create pipeline
|
|
418
|
-
// (spec-seed, architecture, part-selection all run before the schematic
|
|
419
|
-
// exists): a doc edit opens the drift obligation, this is the only tool
|
|
420
|
-
// that clears it, and finish refuses while any obligation is open.
|
|
421
|
-
if (!ctx.config.schematic) {
|
|
422
|
-
ctx.ledger.clear('drift');
|
|
423
|
-
return 'no schematic configured; drift vacuously clean';
|
|
424
|
-
}
|
|
425
|
-
const mismatches = await checkDrift(ctx.repoRoot, ctx.config.docs, ctx.config.schematic);
|
|
426
|
-
if (!mismatches.length) {
|
|
427
|
-
ctx.ledger.clear('drift');
|
|
428
|
-
return 'drift: clean';
|
|
429
|
-
}
|
|
430
|
-
return mismatches.map((m) => `${m.doc}: claims "${m.claim}" but actual is "${m.actual}"`).join('\n');
|
|
431
|
-
},
|
|
432
|
-
},
|
|
433
|
-
{
|
|
434
|
-
schema: {
|
|
435
|
-
name: 'record_constraint',
|
|
436
|
-
description:
|
|
437
|
-
'Record a constraint in .copperhead/constraints.json (dual write: put the same fact in the relevant doc in this same turn). Opens revisit obligations for every item in affects.',
|
|
438
|
-
parameters: {
|
|
439
|
-
type: 'object',
|
|
440
|
-
properties: {
|
|
441
|
-
key: { type: 'string', description: 'e.g. power.sleep_current_uA' },
|
|
442
|
-
min: { type: 'number' },
|
|
443
|
-
max: { type: 'number' },
|
|
444
|
-
forbidden: { type: 'array', items: { type: 'string' } },
|
|
445
|
-
value: { type: 'string' },
|
|
446
|
-
source: { type: 'string', description: 'doc/spec location that states this' },
|
|
447
|
-
affects: { type: 'array', items: { type: 'string' } },
|
|
448
|
-
},
|
|
449
|
-
required: ['key', 'source', 'affects'],
|
|
450
|
-
},
|
|
451
|
-
},
|
|
452
|
-
requiresUnlock: true,
|
|
453
|
-
handler: async (ctx, args) => {
|
|
454
|
-
const key = str(args, 'key');
|
|
455
|
-
const affects = (args.affects as string[]) ?? [];
|
|
456
|
-
// An affects item whose target artifact is not built yet (no schematic or
|
|
457
|
-
// board configured, no BOM.md) has nothing to revisit; opening an
|
|
458
|
-
// obligation now only forces a ceremonial "not yet created" resolution.
|
|
459
|
-
// Defer it in the registry instead — reopenDeferredAffects re-opens it at
|
|
460
|
-
// the start of the first run where the artifact exists, which is when the
|
|
461
|
-
// revisit actually means something.
|
|
462
|
-
const deferred: string[] = [];
|
|
463
|
-
const openNow: string[] = [];
|
|
464
|
-
for (const item of affects) {
|
|
465
|
-
const target = classifyAffectsTarget(item);
|
|
466
|
-
if (target && !affectsTargetExists(target, ctx.repoRoot, ctx.config)) deferred.push(item);
|
|
467
|
-
else openNow.push(item);
|
|
468
|
-
}
|
|
469
|
-
await saveConstraint(ctx.repoRoot, key, {
|
|
470
|
-
...(args.min !== undefined ? { min: args.min as number } : {}),
|
|
471
|
-
...(args.max !== undefined ? { max: args.max as number } : {}),
|
|
472
|
-
...(args.forbidden !== undefined ? { forbidden: args.forbidden as string[] } : {}),
|
|
473
|
-
...(args.value !== undefined ? { value: args.value as string } : {}),
|
|
474
|
-
source: str(args, 'source'),
|
|
475
|
-
affects,
|
|
476
|
-
...(deferred.length ? { deferred } : {}),
|
|
477
|
-
});
|
|
478
|
-
ctx.ledger.onConstraintChange(key, openNow);
|
|
479
|
-
ctx.ledger.clear('constraint-dual-write', key);
|
|
480
|
-
const parts = [`constraint ${key} recorded`];
|
|
481
|
-
parts.push(`revisit obligations opened for: ${openNow.join(', ') || '(none)'}`);
|
|
482
|
-
if (deferred.length) {
|
|
483
|
-
parts.push(
|
|
484
|
-
`deferred until the target artifact exists (no resolve_affected needed now): ${deferred.join(', ')}`,
|
|
485
|
-
);
|
|
486
|
-
}
|
|
487
|
-
return parts.join('; ');
|
|
488
|
-
},
|
|
489
|
-
},
|
|
490
|
-
{
|
|
491
|
-
schema: {
|
|
492
|
-
name: 'resolve_affected',
|
|
493
|
-
description:
|
|
494
|
-
'Explicitly resolve affects-revisit obligations: state whether each affected item changed or why no change is needed. Pass resolutions[] to clear many in one call, or the single constraint_key/item/resolution form.',
|
|
495
|
-
parameters: {
|
|
496
|
-
type: 'object',
|
|
497
|
-
properties: {
|
|
498
|
-
constraint_key: { type: 'string' },
|
|
499
|
-
item: { type: 'string' },
|
|
500
|
-
resolution: { type: 'string', description: '"changed: ..." or "no change needed: <reason>"' },
|
|
501
|
-
resolutions: {
|
|
502
|
-
type: 'array',
|
|
503
|
-
description: 'batch form: resolve many obligations in one call',
|
|
504
|
-
items: {
|
|
505
|
-
type: 'object',
|
|
506
|
-
properties: {
|
|
507
|
-
constraint_key: { type: 'string' },
|
|
508
|
-
item: { type: 'string' },
|
|
509
|
-
resolution: { type: 'string' },
|
|
510
|
-
},
|
|
511
|
-
required: ['constraint_key', 'item', 'resolution'],
|
|
512
|
-
},
|
|
513
|
-
},
|
|
514
|
-
},
|
|
515
|
-
required: [],
|
|
516
|
-
},
|
|
517
|
-
},
|
|
518
|
-
requiresUnlock: true,
|
|
519
|
-
handler: async (ctx, args) => {
|
|
520
|
-
// An item that matches nothing must not read as success: the model would
|
|
521
|
-
// move on believing the obligation closed, and only find out at finish.
|
|
522
|
-
const resolveOne = (constraintKey: string, item: string, resolution: string): string => {
|
|
523
|
-
const detail = `${constraintKey} affects ${item}`;
|
|
524
|
-
if (!ctx.ledger.clear('affects-revisit', detail)) {
|
|
525
|
-
const open = ctx.ledger.openOfKind('affects-revisit');
|
|
526
|
-
if (!open.length) return `error: no open affects-revisit obligation matches "${detail}"`;
|
|
527
|
-
return [
|
|
528
|
-
`error: no open affects-revisit obligation matches "${detail}".`,
|
|
529
|
-
'Match these exactly:',
|
|
530
|
-
...open.map((o) => ` - ${o.detail}`),
|
|
531
|
-
].join('\n');
|
|
532
|
-
}
|
|
533
|
-
ctx.decisions.push(`[affects] ${detail}: ${resolution}`);
|
|
534
|
-
return `resolved: ${detail}`;
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
const batch = args.resolutions;
|
|
538
|
-
if (Array.isArray(batch) && batch.length) {
|
|
539
|
-
// Entries resolve independently: one bad key must not waste the call.
|
|
540
|
-
return batch
|
|
541
|
-
.map((entry, i) => {
|
|
542
|
-
const e = entry as Record<string, unknown>;
|
|
543
|
-
if (typeof e?.constraint_key !== 'string' || typeof e?.item !== 'string' || typeof e?.resolution !== 'string') {
|
|
544
|
-
return `error: resolutions[${i}] needs string constraint_key, item, and resolution`;
|
|
545
|
-
}
|
|
546
|
-
return resolveOne(e.constraint_key, e.item, e.resolution);
|
|
547
|
-
})
|
|
548
|
-
.join('\n');
|
|
549
|
-
}
|
|
550
|
-
if (typeof args.constraint_key === 'string' && typeof args.item === 'string' && typeof args.resolution === 'string') {
|
|
551
|
-
return resolveOne(args.constraint_key, args.item, args.resolution);
|
|
552
|
-
}
|
|
553
|
-
return 'error: pass either resolutions: [{constraint_key, item, resolution}, ...] or the single form constraint_key + item + resolution';
|
|
554
|
-
},
|
|
555
|
-
},
|
|
556
|
-
{
|
|
557
|
-
schema: {
|
|
558
|
-
name: 'record_decision',
|
|
559
|
-
description:
|
|
560
|
-
'Append a non-trivial decision to docs/DECISIONS.md: what was decided, the one-line why, and what it affects.',
|
|
561
|
-
parameters: {
|
|
562
|
-
type: 'object',
|
|
563
|
-
properties: {
|
|
564
|
-
decision: { type: 'string' },
|
|
565
|
-
rationale: { type: 'string' },
|
|
566
|
-
affects: { type: 'string', description: 'refdes/nets/docs affected' },
|
|
567
|
-
},
|
|
568
|
-
required: ['decision', 'rationale'],
|
|
569
|
-
},
|
|
570
|
-
},
|
|
571
|
-
requiresUnlock: true,
|
|
572
|
-
handler: async (ctx, args) => {
|
|
573
|
-
const corrupt = corruptionError({ decision: args.decision, rationale: args.rationale, affects: args.affects });
|
|
574
|
-
if (corrupt) return corrupt;
|
|
575
|
-
const decision = str(args, 'decision');
|
|
576
|
-
const rationale = str(args, 'rationale');
|
|
577
|
-
const affects = (args.affects as string | undefined) ?? '';
|
|
578
|
-
const date = new Date().toISOString().slice(0, 10);
|
|
579
|
-
const entry = `- ${date} [run ${ctx.runId}] ${decision} | why: ${rationale}${affects ? ` | affects: ${affects}` : ''}`;
|
|
580
|
-
const p = path.join(ctx.repoRoot, ctx.config.docs, 'DECISIONS.md');
|
|
581
|
-
await appendFile(p, entry + '\n', 'utf8');
|
|
582
|
-
ctx.decisions.push(`${decision} | why: ${rationale}`);
|
|
583
|
-
ctx.filesTouched.add(path.join(ctx.config.docs, 'DECISIONS.md'));
|
|
584
|
-
return 'decision recorded';
|
|
585
|
-
},
|
|
586
|
-
},
|
|
587
|
-
{
|
|
588
|
-
schema: {
|
|
589
|
-
name: 'finish',
|
|
590
|
-
description:
|
|
591
|
-
'End the run. outcome "done" requires all verification gates and sync obligations to be satisfied; outcome "refuse" ends without edits, citing the violated budget/constraint in summary.',
|
|
592
|
-
parameters: {
|
|
593
|
-
type: 'object',
|
|
594
|
-
properties: {
|
|
595
|
-
outcome: { type: 'string', enum: ['done', 'refuse'] },
|
|
596
|
-
summary: { type: 'string' },
|
|
597
|
-
},
|
|
598
|
-
required: ['outcome', 'summary'],
|
|
599
|
-
},
|
|
600
|
-
},
|
|
601
|
-
requiresUnlock: false,
|
|
602
|
-
handler: async (ctx, args) => {
|
|
603
|
-
const outcome = str(args, 'outcome') as 'done' | 'refuse';
|
|
604
|
-
const summary = str(args, 'summary');
|
|
605
|
-
if (outcome === 'refuse') {
|
|
606
|
-
ctx.finishRequest = { outcome, summary };
|
|
607
|
-
return 'refusal recorded; run will end';
|
|
608
|
-
}
|
|
609
|
-
const problems: string[] = [];
|
|
610
|
-
const touchedKicad = [...ctx.filesTouched].some((f) => isKicadFile(f));
|
|
611
|
-
if (touchedKicad) {
|
|
612
|
-
if (!ctx.lastErc?.ok) problems.push('ERC has not passed since the last schematic edit (run run_erc)');
|
|
613
|
-
const touchedPcb = [...ctx.filesTouched].some((f) => f.endsWith('.kicad_pcb'));
|
|
614
|
-
if (touchedPcb && !ctx.lastDrc?.ok) problems.push('DRC has not passed since the last board edit (run run_drc)');
|
|
615
|
-
}
|
|
616
|
-
// the changelog obligation is cleared by the commit path itself
|
|
617
|
-
const blocking = ctx.ledger.openObligations.filter((o) => o.kind !== 'changelog');
|
|
618
|
-
if (blocking.length) {
|
|
619
|
-
problems.push('open sync obligations:\n' + blocking.map((o) => ` - [${o.kind}] ${o.detail}`).join('\n'));
|
|
620
|
-
}
|
|
621
|
-
if (problems.length) {
|
|
622
|
-
return `cannot finish yet:\n${problems.map((p) => `- ${p}`).join('\n')}`;
|
|
623
|
-
}
|
|
624
|
-
ctx.finishRequest = { outcome, summary };
|
|
625
|
-
return 'all gates satisfied; run will commit';
|
|
626
|
-
},
|
|
627
|
-
},
|
|
628
|
-
];
|
|
629
|
-
|
|
630
|
-
/** Compose the tool list for the current state (design D2: the lock is structural). */
|
|
631
|
-
export function availableTools(ctx: RunContext): ToolDef[] {
|
|
632
|
-
return TOOLS.filter((t) => !t.requiresUnlock || ctx.editsUnlocked);
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
export async function dispatchTool(ctx: RunContext, name: string, args: Record<string, unknown>): Promise<string> {
|
|
636
|
-
const tool = availableTools(ctx).find((t) => t.schema.name === name);
|
|
637
|
-
if (!tool) return `tool "${name}" is not available${ctx.editsUnlocked ? '' : ' (edit tools unlock after the proposal validates)'}`;
|
|
638
|
-
try {
|
|
639
|
-
return await tool.handler(ctx, args);
|
|
640
|
-
} catch (err) {
|
|
641
|
-
return `error: ${(err as Error).message}`;
|
|
107
|
+
messages.push({ role: 'assistant', content: res.text, toolCalls: res.toolCalls });
|
|
108
|
+
if (!res.toolCalls.length) {
|
|
109
|
+
if (await skill.isComplete(ctx, args)) break;
|
|
110
|
+
messages.push({ role: 'user', content: 'Continue using tools until the report is complete.' });
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
for (const call of res.toolCalls) {
|
|
114
|
+
const envelope = await dispatchToolResult(ctx, call.name, call.args, { only });
|
|
115
|
+
results.push({ name: call.name, result: envelope });
|
|
116
|
+
const flat = flatten(envelope);
|
|
117
|
+
await ctx.transcript.event('skill-tool', {
|
|
118
|
+
skill: skill.name,
|
|
119
|
+
name: call.name,
|
|
120
|
+
args: call.args,
|
|
121
|
+
result: flat,
|
|
122
|
+
envelope,
|
|
123
|
+
});
|
|
124
|
+
messages.push({ role: 'tool', toolCallId: call.id, content: flat });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} finally {
|
|
128
|
+
ctx.finishRequest = savedFinish;
|
|
642
129
|
}
|
|
130
|
+
|
|
131
|
+
const detail = results.map(({ name, result }) => `${name}:\n${flatten(result)}`).join('\n\n') || 'no tool results';
|
|
132
|
+
const complete = await skill.isComplete(ctx, args);
|
|
133
|
+
return seal({
|
|
134
|
+
ok: complete,
|
|
135
|
+
summary: complete ? 'design report' : 'design report incomplete',
|
|
136
|
+
detail,
|
|
137
|
+
viewHint: 'diagnostic',
|
|
138
|
+
});
|
|
643
139
|
}
|