copperhead 0.9.0 → 0.10.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.
Files changed (90) hide show
  1. package/NOTICE +1 -1
  2. package/README.md +6 -6
  3. package/dist/agent/filetools.js +24 -1
  4. package/dist/agent/filetools.js.map +1 -1
  5. package/dist/agent/ledger.js +24 -0
  6. package/dist/agent/ledger.js.map +1 -1
  7. package/dist/agent/loop.js +29 -58
  8. package/dist/agent/loop.js.map +1 -1
  9. package/dist/agent/prompts.js +4 -3
  10. package/dist/agent/prompts.js.map +1 -1
  11. package/dist/agent/providers/tool-protocol.js +21 -0
  12. package/dist/agent/providers/tool-protocol.js.map +1 -1
  13. package/dist/agent/recovery.js +95 -1
  14. package/dist/agent/recovery.js.map +1 -1
  15. package/dist/agent/tools.js +185 -1
  16. package/dist/agent/tools.js.map +1 -1
  17. package/dist/agent/transcript.js +2 -0
  18. package/dist/agent/transcript.js.map +1 -1
  19. package/dist/cli.js +75 -0
  20. package/dist/cli.js.map +1 -1
  21. package/dist/commands/check.js +33 -1
  22. package/dist/commands/check.js.map +1 -1
  23. package/dist/commands/create.js +177 -25
  24. package/dist/commands/create.js.map +1 -1
  25. package/dist/commands/doctor.js +50 -3
  26. package/dist/commands/doctor.js.map +1 -1
  27. package/dist/config.js +1 -0
  28. package/dist/config.js.map +1 -1
  29. package/dist/kicad/bootstrap.js +24 -3
  30. package/dist/kicad/bootstrap.js.map +1 -1
  31. package/dist/kicad/dossier.js +207 -0
  32. package/dist/kicad/dossier.js.map +1 -0
  33. package/dist/kicad/draft/draft.js +132 -0
  34. package/dist/kicad/draft/draft.js.map +1 -0
  35. package/dist/kicad/draft/engine.js +2389 -0
  36. package/dist/kicad/draft/engine.js.map +1 -0
  37. package/dist/kicad/draft/ir.js +368 -0
  38. package/dist/kicad/draft/ir.js.map +1 -0
  39. package/dist/kicad/draft/symsource.js +490 -0
  40. package/dist/kicad/draft/symsource.js.map +1 -0
  41. package/dist/kicad/emit.js +181 -0
  42. package/dist/kicad/emit.js.map +1 -0
  43. package/dist/kicad/fab.js +13 -0
  44. package/dist/kicad/fab.js.map +1 -1
  45. package/dist/kicad/legibility.js +561 -0
  46. package/dist/kicad/legibility.js.map +1 -0
  47. package/dist/kicad/score.js +261 -0
  48. package/dist/kicad/score.js.map +1 -0
  49. package/dist/kicad/sexp.js +239 -6
  50. package/dist/kicad/sexp.js.map +1 -1
  51. package/dist/kicad/symlib.js +346 -16
  52. package/dist/kicad/symlib.js.map +1 -1
  53. package/dist/memory/bom-table.js +75 -39
  54. package/dist/memory/bom-table.js.map +1 -1
  55. package/dist/memory/scaffold.js +6 -0
  56. package/dist/memory/scaffold.js.map +1 -1
  57. package/dist/util/redact.js +6 -0
  58. package/dist/util/redact.js.map +1 -1
  59. package/package.json +9 -7
  60. package/src/agent/filetools.ts +26 -1
  61. package/src/agent/ledger.ts +24 -0
  62. package/src/agent/loop.ts +28 -61
  63. package/src/agent/prompts.ts +4 -3
  64. package/src/agent/providers/tool-protocol.ts +22 -0
  65. package/src/agent/recovery.ts +94 -1
  66. package/src/agent/tools.ts +189 -1
  67. package/src/agent/transcript.ts +6 -0
  68. package/src/cli.ts +71 -0
  69. package/src/commands/check.ts +51 -1
  70. package/src/commands/create.ts +179 -20
  71. package/src/commands/doctor.ts +51 -3
  72. package/src/config.ts +24 -0
  73. package/src/kicad/bootstrap.ts +24 -3
  74. package/src/kicad/dossier.ts +217 -0
  75. package/src/kicad/draft/draft.ts +171 -0
  76. package/src/kicad/draft/engine.ts +2466 -0
  77. package/src/kicad/draft/ir.ts +416 -0
  78. package/src/kicad/draft/symsource.ts +535 -0
  79. package/src/kicad/emit.ts +236 -0
  80. package/src/kicad/fab.ts +15 -0
  81. package/src/kicad/legibility.ts +646 -0
  82. package/src/kicad/score.ts +323 -0
  83. package/src/kicad/sexp.ts +315 -6
  84. package/src/kicad/symlib.ts +364 -18
  85. package/src/memory/bom-table.ts +85 -38
  86. package/src/memory/scaffold.ts +6 -0
  87. package/src/util/redact.ts +6 -0
  88. package/dist/memory/synap.js +0 -152
  89. package/dist/memory/synap.js.map +0 -1
  90. package/src/memory/synap.ts +0 -217
@@ -1,152 +0,0 @@
1
- /**
2
- * Cross-run memory via Synap (https://docs.maximem.ai).
3
- *
4
- * copperhead's own memory is per-repo and file-based: docs/DECISIONS.md, the
5
- * constraint registry, the drift checker. Synap is the layer above that — what
6
- * this user tends to want across every board they've ever asked about. It
7
- * complements the docs; it does not replace them, and nothing here is a source
8
- * of truth. Recalled text is advisory context in the system prompt, while
9
- * as-built facts still come from the KiCad files.
10
- *
11
- * Three properties are load-bearing:
12
- *
13
- * 1. Opt-in. Inactive unless SYNAP_API_KEY is set, so the default install is
14
- * unchanged and `check` stays network-free (it never constructs this).
15
- * 2. Optional at runtime. @maximem/synap-js-sdk is an optionalDependency and is
16
- * imported lazily through a non-literal specifier, so a missing package (or
17
- * a host without the Python 3.11+ runtime its bridge needs) degrades to
18
- * "no memory" instead of breaking the CLI.
19
- * 3. Fail-soft on read, loud on write. A recall failure must never cost someone
20
- * a design run; a record failure is reported, because silently losing writes
21
- * lets memory drift away from what actually happened.
22
- */
23
- import { randomUUID } from 'node:crypto';
24
- import { execa } from 'execa';
25
- import { redactSecrets } from '../util/redact.js';
26
- /** Wall-clock ceiling on any single Synap call. The bridge is a Python subprocess. */
27
- const RECALL_TIMEOUT_MS = 10_000;
28
- const RECORD_TIMEOUT_MS = 15_000;
29
- const MAX_RECALLED = 8;
30
- export function synapEnabled(env = process.env) {
31
- return Boolean(env.SYNAP_API_KEY);
32
- }
33
- /**
34
- * Identity for memory scoping. The git committer email is the natural stable
35
- * user id here: it is already the identity every run is attributed to.
36
- */
37
- async function resolveUserId(repoRoot, env) {
38
- if (env.SYNAP_USER_ID)
39
- return env.SYNAP_USER_ID;
40
- try {
41
- const { stdout } = await execa('git', ['config', 'user.email'], { cwd: repoRoot });
42
- if (stdout.trim())
43
- return stdout.trim();
44
- }
45
- catch {
46
- // not configured; fall through
47
- }
48
- return 'copperhead-local';
49
- }
50
- function withTimeout(p, ms, label) {
51
- return new Promise((resolve, reject) => {
52
- const timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
53
- p.then((v) => {
54
- clearTimeout(timer);
55
- resolve(v);
56
- }, (e) => {
57
- clearTimeout(timer);
58
- reject(e);
59
- });
60
- });
61
- }
62
- /**
63
- * Returns null when memory is disabled or unavailable — every caller treats
64
- * null as "run without cross-run memory".
65
- */
66
- export async function openSynapMemory(opts) {
67
- const env = opts.env ?? process.env;
68
- const log = opts.log ?? (() => { });
69
- if (!synapEnabled(env))
70
- return null;
71
- let client;
72
- try {
73
- // Non-literal specifier: keeps tsc from resolving an optional dependency at
74
- // build time, so the package may legitimately be absent.
75
- const specifier = '@maximem/synap-js-sdk';
76
- const mod = (await import(specifier));
77
- const createClient = mod.createClient ?? mod.default?.createClient;
78
- if (!createClient)
79
- throw new Error('createClient not exported');
80
- client = createClient({ apiKey: env.SYNAP_API_KEY, requestTimeoutMs: RECORD_TIMEOUT_MS });
81
- await withTimeout(client.init(), RECORD_TIMEOUT_MS, 'synap init');
82
- }
83
- catch (err) {
84
- // Missing package, missing Python runtime, bad key: all non-fatal.
85
- log(`synap memory unavailable (${err.message}); continuing without it`);
86
- return null;
87
- }
88
- const userId = await resolveUserId(opts.repoRoot, env);
89
- const customerId = env.SYNAP_CUSTOMER_ID ?? 'copperhead';
90
- const conversationId = randomUUID();
91
- return {
92
- async recall(request) {
93
- try {
94
- const res = await withTimeout(client.searchMemory({
95
- userId,
96
- customerId,
97
- query: redactSecrets(request),
98
- maxResults: MAX_RECALLED,
99
- }), RECALL_TIMEOUT_MS, 'synap recall');
100
- const items = (res.results ?? []).filter((r) => r.memory?.trim());
101
- if (!items.length)
102
- return null;
103
- const lines = items.map((r) => `- ${r.memory.trim()}${r.contextType ? ` _(${r.contextType})_` : ''}`);
104
- return [
105
- '## Recalled from prior runs (Synap)',
106
- '',
107
- 'Context from earlier work by this user, possibly on other boards. Advisory only:',
108
- 'the KiCad files and this repo’s docs remain the source of truth. If any of this',
109
- 'conflicts with what you read in the repo, the repo wins — say so rather than',
110
- 'acting on a stale memory.',
111
- '',
112
- ...lines,
113
- ].join('\n');
114
- }
115
- catch (err) {
116
- log(`synap recall failed (${err.message}); continuing without it`);
117
- return null;
118
- }
119
- },
120
- async record(run) {
121
- const assistant = [
122
- `Outcome: ${run.outcome}`,
123
- `Summary: ${run.summary}`,
124
- `OpenSpec change: ${run.changeId ?? 'n/a'}`,
125
- `Verification: ${run.verification}`,
126
- run.filesTouched.length ? `Files: ${run.filesTouched.join(', ')}` : null,
127
- run.decisions.length ? `Decisions:\n${run.decisions.map((d) => `- ${d}`).join('\n')}` : null,
128
- ]
129
- .filter(Boolean)
130
- .join('\n');
131
- await withTimeout(client.addMemory({
132
- userId,
133
- customerId,
134
- conversationId,
135
- messages: [
136
- { role: 'user', content: redactSecrets(run.request) },
137
- { role: 'assistant', content: redactSecrets(assistant) },
138
- ],
139
- metadata: { source: 'copperhead', outcome: run.outcome, changeId: run.changeId },
140
- }), RECORD_TIMEOUT_MS, 'synap record');
141
- },
142
- async close() {
143
- try {
144
- await withTimeout(client.shutdown(), RECORD_TIMEOUT_MS, 'synap shutdown');
145
- }
146
- catch {
147
- // Best effort: a failed shutdown must not change the run's outcome.
148
- }
149
- },
150
- };
151
- }
152
- //# sourceMappingURL=synap.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"synap.js","sourceRoot":"","sources":["../../src/memory/synap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAElD,sFAAsF;AACtF,MAAM,iBAAiB,GAAG,MAAM,CAAC;AACjC,MAAM,iBAAiB,GAAG,MAAM,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C,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,UAAU,GAAG,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI;gBACxE,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,eAAe,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI;aAC7F;iBACE,MAAM,CAAC,OAAO,CAAC;iBACf,IAAI,CAAC,IAAI,CAAC,CAAC;YAEd,MAAM,WAAW,CACf,MAAM,CAAC,SAAS,CAAC;gBACf,MAAM;gBACN,UAAU;gBACV,cAAc;gBACd,QAAQ,EAAE;oBACR,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa,CAAC,GAAG,CAAC,OAAO,CAAC,EAAE;oBACrD,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,aAAa,CAAC,SAAS,CAAC,EAAE;iBACzD;gBACD,QAAQ,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,QAAQ,EAAE;aACjF,CAAC,EACF,iBAAiB,EACjB,cAAc,CACf,CAAC;QACJ,CAAC;QAED,KAAK,CAAC,KAAK;YACT,IAAI,CAAC;gBACH,MAAM,WAAW,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,iBAAiB,EAAE,gBAAgB,CAAC,CAAC;YAC5E,CAAC;YAAC,MAAM,CAAC;gBACP,oEAAoE;YACtE,CAAC;QACH,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -1,217 +0,0 @@
1
- /**
2
- * Cross-run memory via Synap (https://docs.maximem.ai).
3
- *
4
- * copperhead's own memory is per-repo and file-based: docs/DECISIONS.md, the
5
- * constraint registry, the drift checker. Synap is the layer above that — what
6
- * this user tends to want across every board they've ever asked about. It
7
- * complements the docs; it does not replace them, and nothing here is a source
8
- * of truth. Recalled text is advisory context in the system prompt, while
9
- * as-built facts still come from the KiCad files.
10
- *
11
- * Three properties are load-bearing:
12
- *
13
- * 1. Opt-in. Inactive unless SYNAP_API_KEY is set, so the default install is
14
- * unchanged and `check` stays network-free (it never constructs this).
15
- * 2. Optional at runtime. @maximem/synap-js-sdk is an optionalDependency and is
16
- * imported lazily through a non-literal specifier, so a missing package (or
17
- * a host without the Python 3.11+ runtime its bridge needs) degrades to
18
- * "no memory" instead of breaking the CLI.
19
- * 3. Fail-soft on read, loud on write. A recall failure must never cost someone
20
- * a design run; a record failure is reported, because silently losing writes
21
- * lets memory drift away from what actually happened.
22
- */
23
- import { randomUUID } from 'node:crypto';
24
- import { execa } from 'execa';
25
- import { redactSecrets } from '../util/redact.js';
26
-
27
- /** Wall-clock ceiling on any single Synap call. The bridge is a Python subprocess. */
28
- const RECALL_TIMEOUT_MS = 10_000;
29
- const RECORD_TIMEOUT_MS = 15_000;
30
- const MAX_RECALLED = 8;
31
-
32
- /**
33
- * Structural subset of @maximem/synap-js-sdk's surface. Declared locally rather
34
- * than imported so `tsc` succeeds when the optional dependency is absent.
35
- */
36
- interface SynapSearchItem {
37
- memory: string;
38
- score?: number;
39
- contextType?: string;
40
- }
41
- interface SynapClientLike {
42
- init(): Promise<void>;
43
- searchMemory(input: {
44
- userId: string;
45
- customerId?: string;
46
- query: string;
47
- maxResults?: number;
48
- }): Promise<{ results: SynapSearchItem[] }>;
49
- addMemory(input: {
50
- userId: string;
51
- customerId: string;
52
- conversationId?: string;
53
- messages: Array<{ role?: 'user' | 'assistant'; content: string }>;
54
- metadata?: Record<string, unknown>;
55
- }): Promise<{ success: boolean }>;
56
- shutdown(): Promise<void>;
57
- }
58
-
59
- export interface RunRecord {
60
- request: string;
61
- outcome: 'success' | 'refused';
62
- summary: string;
63
- changeId: string | null;
64
- filesTouched: string[];
65
- decisions: string[];
66
- verification: string;
67
- }
68
-
69
- export interface SynapMemory {
70
- /** Prior context relevant to this request, as a prompt-ready markdown block. */
71
- recall(request: string): Promise<string | null>;
72
- /** Persist a finished run. Rejects if the write fails. */
73
- record(run: RunRecord): Promise<void>;
74
- /** Stop the bridge subprocess. Always call this, or the CLI will not exit. */
75
- close(): Promise<void>;
76
- }
77
-
78
- export function synapEnabled(env: NodeJS.ProcessEnv = process.env): boolean {
79
- return Boolean(env.SYNAP_API_KEY);
80
- }
81
-
82
- /**
83
- * Identity for memory scoping. The git committer email is the natural stable
84
- * user id here: it is already the identity every run is attributed to.
85
- */
86
- async function resolveUserId(repoRoot: string, env: NodeJS.ProcessEnv): Promise<string> {
87
- if (env.SYNAP_USER_ID) return env.SYNAP_USER_ID;
88
- try {
89
- const { stdout } = await execa('git', ['config', 'user.email'], { cwd: repoRoot });
90
- if (stdout.trim()) return stdout.trim();
91
- } catch {
92
- // not configured; fall through
93
- }
94
- return 'copperhead-local';
95
- }
96
-
97
- function withTimeout<T>(p: Promise<T>, ms: number, label: string): Promise<T> {
98
- return new Promise<T>((resolve, reject) => {
99
- const timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms);
100
- p.then(
101
- (v) => {
102
- clearTimeout(timer);
103
- resolve(v);
104
- },
105
- (e) => {
106
- clearTimeout(timer);
107
- reject(e);
108
- },
109
- );
110
- });
111
- }
112
-
113
- /**
114
- * Returns null when memory is disabled or unavailable — every caller treats
115
- * null as "run without cross-run memory".
116
- */
117
- export async function openSynapMemory(opts: {
118
- repoRoot: string;
119
- log?: (line: string) => void;
120
- env?: NodeJS.ProcessEnv;
121
- }): Promise<SynapMemory | null> {
122
- const env = opts.env ?? process.env;
123
- const log = opts.log ?? (() => {});
124
- if (!synapEnabled(env)) return null;
125
-
126
- let client: SynapClientLike;
127
- try {
128
- // Non-literal specifier: keeps tsc from resolving an optional dependency at
129
- // build time, so the package may legitimately be absent.
130
- const specifier = '@maximem/synap-js-sdk';
131
- const mod = (await import(specifier)) as {
132
- createClient?: (o?: Record<string, unknown>) => SynapClientLike;
133
- default?: { createClient?: (o?: Record<string, unknown>) => SynapClientLike };
134
- };
135
- const createClient = mod.createClient ?? mod.default?.createClient;
136
- if (!createClient) throw new Error('createClient not exported');
137
- client = createClient({ apiKey: env.SYNAP_API_KEY, requestTimeoutMs: RECORD_TIMEOUT_MS });
138
- await withTimeout(client.init(), RECORD_TIMEOUT_MS, 'synap init');
139
- } catch (err) {
140
- // Missing package, missing Python runtime, bad key: all non-fatal.
141
- log(`synap memory unavailable (${(err as Error).message}); continuing without it`);
142
- return null;
143
- }
144
-
145
- const userId = await resolveUserId(opts.repoRoot, env);
146
- const customerId = env.SYNAP_CUSTOMER_ID ?? 'copperhead';
147
- const conversationId = randomUUID();
148
-
149
- return {
150
- async recall(request) {
151
- try {
152
- const res = await withTimeout(
153
- client.searchMemory({
154
- userId,
155
- customerId,
156
- query: redactSecrets(request),
157
- maxResults: MAX_RECALLED,
158
- }),
159
- RECALL_TIMEOUT_MS,
160
- 'synap recall',
161
- );
162
- const items = (res.results ?? []).filter((r) => r.memory?.trim());
163
- if (!items.length) return null;
164
- const lines = items.map((r) => `- ${r.memory.trim()}${r.contextType ? ` _(${r.contextType})_` : ''}`);
165
- return [
166
- '## Recalled from prior runs (Synap)',
167
- '',
168
- 'Context from earlier work by this user, possibly on other boards. Advisory only:',
169
- 'the KiCad files and this repo’s docs remain the source of truth. If any of this',
170
- 'conflicts with what you read in the repo, the repo wins — say so rather than',
171
- 'acting on a stale memory.',
172
- '',
173
- ...lines,
174
- ].join('\n');
175
- } catch (err) {
176
- log(`synap recall failed (${(err as Error).message}); continuing without it`);
177
- return null;
178
- }
179
- },
180
-
181
- async record(run) {
182
- const assistant = [
183
- `Outcome: ${run.outcome}`,
184
- `Summary: ${run.summary}`,
185
- `OpenSpec change: ${run.changeId ?? 'n/a'}`,
186
- `Verification: ${run.verification}`,
187
- run.filesTouched.length ? `Files: ${run.filesTouched.join(', ')}` : null,
188
- run.decisions.length ? `Decisions:\n${run.decisions.map((d) => `- ${d}`).join('\n')}` : null,
189
- ]
190
- .filter(Boolean)
191
- .join('\n');
192
-
193
- await withTimeout(
194
- client.addMemory({
195
- userId,
196
- customerId,
197
- conversationId,
198
- messages: [
199
- { role: 'user', content: redactSecrets(run.request) },
200
- { role: 'assistant', content: redactSecrets(assistant) },
201
- ],
202
- metadata: { source: 'copperhead', outcome: run.outcome, changeId: run.changeId },
203
- }),
204
- RECORD_TIMEOUT_MS,
205
- 'synap record',
206
- );
207
- },
208
-
209
- async close() {
210
- try {
211
- await withTimeout(client.shutdown(), RECORD_TIMEOUT_MS, 'synap shutdown');
212
- } catch {
213
- // Best effort: a failed shutdown must not change the run's outcome.
214
- }
215
- },
216
- };
217
- }