cocheck 0.4.0 → 0.4.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.
Files changed (81) hide show
  1. package/README.md +17 -3
  2. package/dist/agent-transcripts.js +15 -12
  3. package/dist/attribution/evidence.js +2 -1
  4. package/dist/attribution/reconstruct.js +19 -0
  5. package/dist/attribution/tag.js +3 -3
  6. package/dist/build-info.json +1 -1
  7. package/dist/cli.js +31 -11
  8. package/dist/connector/export-view.js +1 -1
  9. package/dist/coverage.js +5 -3
  10. package/dist/credit-guard.js +59 -54
  11. package/dist/harness-cmd/create.js +76 -0
  12. package/dist/harness-cmd/index.js +19 -0
  13. package/dist/harness-cmd/record.js +327 -0
  14. package/dist/harness-cmd/scrub.js +25 -0
  15. package/dist/harness-cmd/test.js +318 -0
  16. package/dist/harnesses/README.md +5 -0
  17. package/dist/harnesses/claude/cocheck-harness.json +344 -0
  18. package/dist/harnesses/codex/cocheck-harness.json +212 -0
  19. package/dist/harnesses/copilot/cocheck-harness.json +296 -0
  20. package/dist/{copilot-helpers.js → helper-linker.js} +43 -32
  21. package/dist/hook-adapter.js +287 -63
  22. package/dist/hook-cli.js +73 -36
  23. package/dist/hook-journal.js +40 -12
  24. package/dist/hosts/claude.js +23 -159
  25. package/dist/hosts/codex.js +41 -81
  26. package/dist/hosts/compile.js +275 -0
  27. package/dist/hosts/copilot.js +7 -162
  28. package/dist/hosts/extras.js +55 -0
  29. package/dist/hosts/harnesses.js +66 -0
  30. package/dist/hosts/index.js +176 -8
  31. package/dist/hosts/install-block.js +332 -0
  32. package/dist/hosts/manifest.js +372 -0
  33. package/dist/hosts/shared.js +21 -0
  34. package/dist/hosts/tool-classes.js +6 -0
  35. package/dist/hosts/types.js +1 -0
  36. package/dist/install.js +69 -22
  37. package/dist/ledger.js +11 -0
  38. package/dist/normalize.js +33 -9
  39. package/dist/open-pick.js +5 -3
  40. package/dist/patch-secrets.js +11 -11
  41. package/dist/review-server.js +2 -2
  42. package/dist/timeline/attribution.js +2 -2
  43. package/dist/timeline/describe.js +20 -17
  44. package/dist/timeline/load.js +2 -2
  45. package/dist/timeline/rail/assets/review.js +1 -1
  46. package/dist/timeline/rail/model.js +8 -8
  47. package/dist/timeline/read-signal.js +4 -4
  48. package/dist/timeline/render/view-v1.js +2 -13
  49. package/dist/timeline/runs.js +9 -9
  50. package/dist/timeline/timeline.js +8 -8
  51. package/dist/timeline/write-signal.js +4 -3
  52. package/package.json +2 -2
  53. package/public-kit/harness/GUIDE.md +81 -0
  54. package/public-kit/harness/README.md +10 -0
  55. package/public-kit/harness/cases/claude/shell-writes-file.json +120 -0
  56. package/public-kit/harness/cases/codex/apply-patch-writes-files.json +177 -0
  57. package/public-kit/harness/cases/codex/helper-writes-file.json +160 -0
  58. package/public-kit/harness/cases/codex/shell-writes-and-fails.json +110 -0
  59. package/public-kit/harness/cases/copilot/camel-case-patch-and-failure.json +126 -0
  60. package/public-kit/harness/cases/copilot/helper-writes-file.json +209 -0
  61. package/public-kit/harness/cases/copilot/vscode-helper-writes-file.json +236 -0
  62. package/public-kit/harness/examples/toyagent/cases/write-notes.json +142 -0
  63. package/public-kit/harness/examples/toyagent/cocheck-harness.json +47 -0
  64. package/public-kit/harness/examples/toyagent/recordings/payloads.jsonl +8 -0
  65. package/public-kit/harness/harnesses/antigravity/README.md +32 -0
  66. package/public-kit/harness/harnesses/antigravity/cases/failed-edit.json +99 -0
  67. package/public-kit/harness/harnesses/antigravity/cases/helper-writes-file.json +270 -0
  68. package/public-kit/harness/harnesses/antigravity/cases/shell-ok-fail-and-denied.json +211 -0
  69. package/public-kit/harness/harnesses/antigravity/cases/write-then-edit.json +191 -0
  70. package/public-kit/harness/harnesses/antigravity/cocheck-harness.json +62 -0
  71. package/public-kit/harness/harnesses/antigravity/recordings/payloads.jsonl +63 -0
  72. package/public-kit/harness/harnesses/cursor/README.md +40 -0
  73. package/public-kit/harness/harnesses/cursor/cases/editor-write-edit-shell-helper.json +359 -0
  74. package/public-kit/harness/harnesses/cursor/cases/read-then-write-one-call-id.json +159 -0
  75. package/public-kit/harness/harnesses/cursor/cocheck-harness.json +67 -0
  76. package/public-kit/harness/harnesses/cursor/recordings/payloads.jsonl +28 -0
  77. package/public-kit/harness/scaffold/README.md +42 -0
  78. package/public-kit/harness/scaffold/cocheck-harness.json +72 -0
  79. package/public-kit/harness/scaffold/recordings/.gitkeep +0 -0
  80. package/public-kit/harness/spec/v1.md +286 -0
  81. package/scripts/hook-launch.sh +1 -1
@@ -0,0 +1,332 @@
1
+ // A generic editor for a host's JSON hook config, driven by a declarative install block (data only). Claude Code and Copilot are two blocks;
2
+ // a host whose config is not JSON (Codex's TOML) keeps its own editor.
3
+ import path from "node:path";
4
+ import { platform } from "../platform/index.js";
5
+ const fail = (where, field, want) => {
6
+ throw new Error(`${where}.${field}: expected ${want}`);
7
+ };
8
+ const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
9
+ const strings = (v, where, field) => {
10
+ if (!Array.isArray(v) || !v.every((x) => typeof x === "string"))
11
+ fail(where, field, "an array of strings");
12
+ return v;
13
+ };
14
+ /** The tokens a config path template may use: {home}, {project}, {env:NAME} and {env:NAME|fallback} (the fallback a template itself). */
15
+ export function pathTemplateProblem(template) {
16
+ for (let i = 0; i < template.length; i++) {
17
+ if (template[i] === "}")
18
+ return `unmatched "}" in ${template}`;
19
+ if (template[i] !== "{")
20
+ continue;
21
+ let depth = 1;
22
+ let j = i + 1;
23
+ for (; j < template.length && depth > 0; j++)
24
+ depth += template[j] === "{" ? 1 : template[j] === "}" ? -1 : 0;
25
+ if (depth !== 0)
26
+ return `unclosed "{" in ${template}`;
27
+ const token = template.slice(i + 1, j - 1);
28
+ i = j - 1;
29
+ if (token === "home" || token === "project")
30
+ continue;
31
+ if (token.startsWith("env:")) {
32
+ const body = token.slice(4);
33
+ const bar = body.indexOf("|");
34
+ const name = bar < 0 ? body : body.slice(0, bar);
35
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name))
36
+ return `{env:${name}} does not name an environment variable`;
37
+ if (bar >= 0) {
38
+ const inner = pathTemplateProblem(body.slice(bar + 1));
39
+ if (inner !== undefined)
40
+ return inner;
41
+ }
42
+ continue;
43
+ }
44
+ return `token {${token}} is not available for a config file (use {home}, {project}, {env:NAME} or {env:NAME|fallback})`;
45
+ }
46
+ return undefined;
47
+ }
48
+ /** Checks an install block read from a manifest; throws an Error that names the offending field. */
49
+ export function validateInstallBlock(v, where) {
50
+ if (!isObj(v))
51
+ throw new Error(`${where}: expected an object`);
52
+ if (v.format !== "json")
53
+ fail(where, "format", `"json"`);
54
+ if (typeof v.path !== "string" || v.path === "")
55
+ fail(where, "path", "a string");
56
+ const pathProblem = pathTemplateProblem(v.path);
57
+ if (pathProblem !== undefined)
58
+ throw new Error(`${where}.path: ${pathProblem}`);
59
+ if (v.mode !== "merge" && v.mode !== "owned")
60
+ fail(where, "mode", `"merge" or "owned"`);
61
+ if (typeof v.rootKey !== "string" || v.rootKey === "")
62
+ fail(where, "rootKey", "a string");
63
+ if (v.envelope !== undefined && !isObj(v.envelope))
64
+ fail(where, "envelope", "an object");
65
+ const events = strings(v.events, where, "events");
66
+ if (events.length === 0)
67
+ fail(where, "events", "at least one event");
68
+ if (!isObj(v.entryTemplate))
69
+ fail(where, "entryTemplate", "an object");
70
+ if (!JSON.stringify(v.entryTemplate).includes("{command}"))
71
+ fail(where, "entryTemplate", "an object that uses {command}");
72
+ if (v.entryTemplates !== undefined) {
73
+ if (!isObj(v.entryTemplates))
74
+ fail(where, "entryTemplates", "an object of event name to entry");
75
+ const written = new Set([...events, ...(Array.isArray(v.extraEvents) ? v.extraEvents : [])]);
76
+ for (const [event, tpl] of Object.entries(v.entryTemplates)) {
77
+ if (!written.has(event))
78
+ fail(where, `entryTemplates.${event}`, "an event listed in events or extraEvents");
79
+ if (!isObj(tpl))
80
+ fail(where, `entryTemplates.${event}`, "an object");
81
+ if (!JSON.stringify(tpl).includes("{command}"))
82
+ fail(where, `entryTemplates.${event}`, "an object that uses {command}");
83
+ }
84
+ }
85
+ if (v.matcherEvents !== undefined) {
86
+ if (!isObj(v.matcherEvents) || !Object.values(v.matcherEvents).every((m) => typeof m === "string"))
87
+ fail(where, "matcherEvents", "an object of strings");
88
+ }
89
+ if (v.eventInCommand !== undefined && typeof v.eventInCommand !== "boolean")
90
+ fail(where, "eventInCommand", "a boolean");
91
+ if (v.failOpenEvents !== undefined)
92
+ strings(v.failOpenEvents, where, "failOpenEvents");
93
+ if (v.windowsCommandKey !== undefined && (typeof v.windowsCommandKey !== "string" || v.windowsCommandKey === ""))
94
+ fail(where, "windowsCommandKey", "a string");
95
+ if (v.extraEvents !== undefined)
96
+ strings(v.extraEvents, where, "extraEvents");
97
+ if (v.managedHost !== undefined && typeof v.managedHost !== "string")
98
+ fail(where, "managedHost", "a string");
99
+ return v;
100
+ }
101
+ /** The text a settings file is refused with: the file is left as it was. */
102
+ class ConfigRefusal extends Error {
103
+ }
104
+ /** Resolves a path template. `{env:NAME|fallback}` may nest tokens in the fallback; an empty variable counts as unset. `project`: the task folder, for {project}. */
105
+ function resolvePath(template, env, userHome, project) {
106
+ let out = "";
107
+ for (let i = 0; i < template.length; i++) {
108
+ if (template[i] !== "{") {
109
+ out += template[i];
110
+ continue;
111
+ }
112
+ let depth = 1;
113
+ let j = i + 1;
114
+ for (; j < template.length && depth > 0; j++)
115
+ depth += template[j] === "{" ? 1 : template[j] === "}" ? -1 : 0;
116
+ if (depth !== 0)
117
+ throw new Error(`install.path: unclosed "{" in ${template}`);
118
+ const token = template.slice(i + 1, j - 1);
119
+ i = j - 1;
120
+ if (token === "home")
121
+ out += userHome;
122
+ else if (token === "project") {
123
+ if (project === undefined || project === "")
124
+ throw new Error(`install.path: {project} needs the task folder (give --folder, or run from the folder)`);
125
+ out += project;
126
+ }
127
+ else if (token.startsWith("env:")) {
128
+ const body = token.slice(4);
129
+ const bar = body.indexOf("|");
130
+ const name = bar < 0 ? body : body.slice(0, bar);
131
+ const value = env[name];
132
+ if (value)
133
+ out += value;
134
+ else if (bar >= 0)
135
+ out += resolvePath(body.slice(bar + 1), env, userHome, project);
136
+ else
137
+ throw new Error(`install.path: environment variable ${name} is not set`);
138
+ }
139
+ else
140
+ throw new Error(`install.path: token {${token}} is not available for a config file`);
141
+ }
142
+ return out;
143
+ }
144
+ const substitute = (v, command, event) => {
145
+ if (typeof v === "string")
146
+ return v.split("{command}").join(command).split("{event}").join(event);
147
+ if (Array.isArray(v))
148
+ return v.map((x) => substitute(x, command, event));
149
+ if (isObj(v))
150
+ return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, substitute(x, command, event)]));
151
+ return v;
152
+ };
153
+ /** The keys of a template object that hold the command, wherever they sit in the entry. */
154
+ function commandKeysOf(v, out = new Set()) {
155
+ if (Array.isArray(v))
156
+ v.forEach((x) => commandKeysOf(x, out));
157
+ else if (isObj(v)) {
158
+ for (const [k, x] of Object.entries(v)) {
159
+ if (typeof x === "string" && x.includes("{command}"))
160
+ out.add(k);
161
+ else
162
+ commandKeysOf(x, out);
163
+ }
164
+ }
165
+ return out;
166
+ }
167
+ /** The editor for a JSON hook config described by `block`: the same members a host profile's `install` has. */
168
+ export function jsonHookEditor(block) {
169
+ const root = block.rootKey;
170
+ const template = block.entryTemplate;
171
+ const perEvent = (block.entryTemplates ?? {});
172
+ /** The entry template of an event: its own from `entryTemplates`, else `entryTemplate`. */
173
+ const templateOf = (event) => (Object.hasOwn(perEvent, event) ? perEvent[event] : template);
174
+ const commandKeys = new Set();
175
+ for (const t of [template, ...Object.values(perEvent)])
176
+ commandKeysOf(t, commandKeys);
177
+ if (block.windowsCommandKey !== undefined)
178
+ commandKeys.add(block.windowsCommandKey);
179
+ // A merge-mode entry that nests its commands under an array key (a group of hooks) is removed command by command. Each event's own
180
+ // template decides; an event CoCheck no longer writes is read by `entryTemplate`.
181
+ const nestKeyOf = (event) => {
182
+ const t = templateOf(event);
183
+ return Object.keys(t).find((k) => Array.isArray(t[k]));
184
+ };
185
+ const writeEvents = [...block.events, ...(block.extraEvents ?? [])];
186
+ const isOurs = (cmd, plat) => {
187
+ if (typeof cmd !== "string")
188
+ return false;
189
+ if (block.managedHost !== undefined)
190
+ return plat.hookCommand.parse(cmd)?.host === block.managedHost;
191
+ return plat.hookCommand.isMarked(cmd) || platform().hookCommand.isMarked(cmd);
192
+ };
193
+ const flatEntryIsOurs = (e, plat) => [...commandKeys].some((k) => isOurs(e?.[k], plat));
194
+ const groupHasOurs = (g, nestKey, plat) => nestKey !== undefined && Array.isArray(g?.[nestKey]) && g[nestKey].some((h) => [...commandKeys].some((k) => isOurs(h?.[k], plat)));
195
+ const nestedIsOurs = (h, plat) => [...commandKeys].some((k) => isOurs(h?.[k], plat));
196
+ const entryFor = (event, base, plat) => {
197
+ const windows = plat.shellDialect.kind === "cmd";
198
+ let command = base;
199
+ if (block.eventInCommand)
200
+ command += ` --event ${event}`;
201
+ if (block.failOpenEvents?.includes(event))
202
+ command += windows ? "; exit 0" : " || true";
203
+ let tpl = templateOf(event);
204
+ if (windows && block.windowsCommandKey !== undefined) {
205
+ tpl = Object.fromEntries(Object.entries(tpl).map(([k, v]) => [v === "{command}" ? block.windowsCommandKey : k, v]));
206
+ }
207
+ const entry = substitute(tpl, command, event);
208
+ const matcher = block.matcherEvents?.[event];
209
+ return matcher === undefined ? entry : { matcher, ...entry };
210
+ };
211
+ /** Strict read of a merge-mode file: an empty file is an empty object; anything else that is not a settings object with sane hooks is refused untouched. */
212
+ const parseMerge = (text, file) => {
213
+ if (text.trim() === "")
214
+ return {};
215
+ let value;
216
+ try {
217
+ value = JSON.parse(text);
218
+ }
219
+ catch {
220
+ throw new ConfigRefusal(`${file} is not valid JSON; nothing was changed`);
221
+ }
222
+ if (!isObj(value))
223
+ throw new ConfigRefusal(`${file} is not a JSON object; nothing was changed`);
224
+ const hooks = value[root];
225
+ if (hooks !== undefined) {
226
+ if (!isObj(hooks))
227
+ throw new ConfigRefusal(`${file}: "${root}" is not an object; nothing was changed`);
228
+ for (const [event, groups] of Object.entries(hooks)) {
229
+ if (!Array.isArray(groups))
230
+ throw new ConfigRefusal(`${file}: ${root}.${event} is not an array; nothing was changed`);
231
+ }
232
+ }
233
+ return value;
234
+ };
235
+ /** Lenient read of an owned file: anything that is not a JSON object has no entries of ours. */
236
+ const parseOwned = (text) => {
237
+ try {
238
+ const v = JSON.parse(text);
239
+ return isObj(v) ? v : undefined;
240
+ }
241
+ catch {
242
+ return undefined;
243
+ }
244
+ };
245
+ const ownedEvents = (text, plat) => {
246
+ const hooks = parseOwned(text)?.[root];
247
+ if (hooks === undefined || hooks === null || typeof hooks !== "object")
248
+ return [];
249
+ return Object.entries(hooks).filter(([, list]) => Array.isArray(list) && list.some((e) => flatEntryIsOurs(e, plat))).map(([event]) => event);
250
+ };
251
+ /** Removes every CoCheck entry. Groups, events and the root key that this empties are dropped; ones the user left empty are not touched. */
252
+ const removeManaged = (settings, plat) => {
253
+ const hooks = settings[root];
254
+ if (hooks === undefined)
255
+ return false;
256
+ let removed = false;
257
+ for (const event of Object.keys(hooks)) {
258
+ const kept = [];
259
+ let touched = false;
260
+ const nestKey = nestKeyOf(event);
261
+ for (const group of hooks[event]) {
262
+ if (nestKey === undefined) {
263
+ if (flatEntryIsOurs(group, plat))
264
+ removed = touched = true;
265
+ else
266
+ kept.push(group);
267
+ continue;
268
+ }
269
+ if (!groupHasOurs(group, nestKey, plat)) {
270
+ kept.push(group);
271
+ continue;
272
+ }
273
+ removed = touched = true;
274
+ const rest = group[nestKey].filter((h) => !nestedIsOurs(h, plat));
275
+ if (rest.length > 0)
276
+ kept.push({ ...group, [nestKey]: rest });
277
+ }
278
+ if (touched) {
279
+ if (kept.length === 0)
280
+ delete hooks[event];
281
+ else
282
+ hooks[event] = kept;
283
+ }
284
+ }
285
+ if (removed && Object.keys(hooks).length === 0)
286
+ delete settings[root];
287
+ return removed;
288
+ };
289
+ const hasManaged = (settings, event, plat) => Array.isArray(settings[root]?.[event]) && settings[root][event].some((g) => {
290
+ const nestKey = nestKeyOf(event);
291
+ return nestKey === undefined ? flatEntryIsOurs(g, plat) : groupHasOurs(g, nestKey, plat);
292
+ });
293
+ const text = (v) => JSON.stringify(v, null, 2) + "\n";
294
+ return {
295
+ configFile: (env, userHome, project) => path.join(resolvePath(block.path, env, userHome, project)),
296
+ addHooks: (current, file, spec, plat = platform()) => {
297
+ const base = spec === undefined ? "" : plat.hookCommand.build(spec);
298
+ if (block.mode === "owned") {
299
+ const hooks = {};
300
+ for (const event of writeEvents)
301
+ hooks[event] = [entryFor(event, base, plat)];
302
+ return text({ ...(block.envelope ?? {}), [root]: hooks });
303
+ }
304
+ const isNew = current.trim() === "";
305
+ const settings = parseMerge(current, file);
306
+ // A new (or empty) file gets the envelope; an existing file keeps its own keys and gets none added.
307
+ if (isNew)
308
+ for (const [k, v] of Object.entries(block.envelope ?? {}))
309
+ settings[k] ??= v;
310
+ removeManaged(settings, plat);
311
+ settings[root] ??= {};
312
+ for (const event of writeEvents)
313
+ (settings[root][event] ??= []).push(entryFor(event, base, plat));
314
+ return text(settings);
315
+ },
316
+ removeHooks: (current, file, plat = platform()) => {
317
+ if (block.mode === "owned")
318
+ return ownedEvents(current, plat).length > 0 ? { text: "", changed: true } : { text: current, changed: false };
319
+ const settings = parseMerge(current, file);
320
+ const changed = removeManaged(settings, plat);
321
+ return { text: changed ? text(settings) : current, changed };
322
+ },
323
+ missingEvents: (current, file, plat = platform()) => {
324
+ if (block.mode === "owned") {
325
+ const have = new Set(ownedEvents(current, plat));
326
+ return block.events.filter((e) => !have.has(e));
327
+ }
328
+ const settings = parseMerge(current, file);
329
+ return block.events.filter((e) => !hasManaged(settings, e, plat));
330
+ },
331
+ };
332
+ }