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
@@ -1,16 +1,16 @@
1
- // Copilot helpers (subagents) whose inner hook calls arrive under a session id CoCheck has never seen. VS Code 1.140.0 sends no
2
- // `agentId` on a helper's calls: they run under the helper's own `sessionId`, and the link to the main session appears only at the
3
- // main session's `subagentStop` (its `agentId` is that inner sessionId). With tracking off by default, those calls must neither be
4
- // recorded as an untracked session's nor lost. The rules, in order, for a Copilot event whose session is not tracked:
1
+ // Helpers (subagents) whose inner hook calls arrive under a session id CoCheck has never seen (a host with `capabilities.helperSessions`).
2
+ // Such a host may send no agent id on a helper's calls: they run under the helper's own session id, and the link to the main session
3
+ // appears only at the main session's helper-stop event (its agent id is that inner session id). With tracking off by default, those calls must neither be
4
+ // recorded as an untracked session's nor lost. The rules, in order, for an event whose session is not tracked:
5
5
  // 1. a link is on file (the helper's id -> its main session): the event is that helper's call, rewritten to name both;
6
- // 2. the event is a helper's first prompt and its text equals the `prompt` of a `task` call a tracked session has open: exact text, one
6
+ // 2. the event is a helper's first prompt and its text equals the `prompt` of a launcher call a tracked session has open: exact text, one
7
7
  // match per launch, makes the link (the same evidence the CLI gives with `agentId`);
8
- // 3. a tracked session has a `task` call open and the event is a tool call: it is held (raw bytes, 0600, under the data home), to be
8
+ // 3. a tracked session has a launcher call open and the event is a tool call: it is held (raw bytes, 0600, under the data home), to be
9
9
  // replayed when the link appears. The bound: held until the launching session's `subagentStop` names the helper, or HELD_TTL_MS
10
10
  // after the launch, whichever comes first; then it is deleted unrecorded. A held call of a session whose parent stops tracking is
11
11
  // deleted the same way;
12
12
  // 4. nothing tracked has a launch open: the event is dropped, as every untracked session's events are.
13
- // Only a tracked session's `task` calls open a launch, so an untracked session leaves nothing here.
13
+ // Only a tracked session's launcher calls open a launch, so an untracked session leaves nothing here.
14
14
  import { createHash } from "node:crypto";
15
15
  import { appendFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync } from "node:fs";
16
16
  import { scrubPayloadBytes } from "./patch-secrets.js";
@@ -18,7 +18,8 @@ import { writeFileAtomicSync } from "./home-perms.js";
18
18
  import path from "node:path";
19
19
  import { withFileLock } from "./file-lock.js";
20
20
  import { trackingOf } from "./tracking.js";
21
- import { profileOf } from "./hosts/index.js";
21
+ import { allProfiles, profileOf } from "./hosts/index.js";
22
+ import { isLauncherTool } from "./credit-guard.js";
22
23
  /** How long a launch stays open, and so how long a held call waits for its helper's link. */
23
24
  const HELD_TTL_MS = 15 * 60_000;
24
25
  /** How long a link stays on file, for the helper's late events. */
@@ -27,9 +28,12 @@ const MAX_HELD_EVENTS = 200;
27
28
  const MAX_HELD_BYTES = 2_000_000;
28
29
  const MAX_HELD_SESSIONS = 8;
29
30
  const MAX_LAUNCHES = 32;
30
- const STORE = "copilot-helpers.json";
31
- const HELD = "copilot-held";
32
- const lockOf = (home) => path.join(home, "copilot-helpers.lock");
31
+ // The three names below are files in the data home that installs already hold: legacy key kept on purpose.
32
+ const STORE = "copilot-helpers.json"; // legacy key kept on purpose
33
+ const HELD = "copilot-held"; // legacy key kept on purpose
34
+ const lockOf = (home) => path.join(home, "copilot-helpers.lock"); // legacy key kept on purpose
35
+ /** True when some host that links helper sessions tracks `session`. */
36
+ const trackedByHelperHost = (home, session) => allProfiles().some((p) => p.capabilities.helperSessions && trackingOf(home, p.key, session) !== undefined);
33
37
  const heldFile = (home, session) => path.join(home, HELD, createHash("sha256").update(session).digest("hex").slice(0, 40) + ".jsonl");
34
38
  let lastRead = "";
35
39
  function read(home) {
@@ -53,9 +57,9 @@ function write(home, store, before) {
53
57
  }
54
58
  /** Drops what is past its bound, and the launches whose main session is no longer tracked; deletes held files nothing waits for. */
55
59
  function prune(home, store, now) {
56
- store.launches = store.launches.filter((l) => now - l.at < HELD_TTL_MS && trackingOf(home, "copilot", l.parent) !== undefined).slice(-MAX_LAUNCHES);
60
+ store.launches = store.launches.filter((l) => now - l.at < HELD_TTL_MS && trackedByHelperHost(home, l.parent)).slice(-MAX_LAUNCHES);
57
61
  for (const [id, link] of Object.entries(store.links))
58
- if (now - link.at >= LINK_TTL_MS || trackingOf(home, "copilot", link.parent) === undefined)
62
+ if (now - link.at >= LINK_TTL_MS || !trackedByHelperHost(home, link.parent))
59
63
  delete store.links[id];
60
64
  let names = [];
61
65
  try {
@@ -88,7 +92,7 @@ export async function pruneHelperState(home, now) {
88
92
  write(home, store, before);
89
93
  });
90
94
  }
91
- /** A tracked session's `task` call opened: a helper may start under a session id of its own. */
95
+ /** A tracked session's launcher call opened: a helper may start under a session id of its own. */
92
96
  async function noteLaunch(home, parent, prompt, agentType, now, key) {
93
97
  if (typeof prompt !== "string" || prompt.trim() === "")
94
98
  return;
@@ -100,19 +104,23 @@ async function noteLaunch(home, parent, prompt, agentType, now, key) {
100
104
  write(home, store);
101
105
  });
102
106
  }
103
- /** The payload as the CLI sends a helper's event: the helper's id as `agentId`, the main session as `parentAgentId`. The event keeps everything else. */
104
- function relink(bytes, helper, link) {
107
+ /** The payload as the host sends a helper's event: the helper's id as the agent id, the main session as the parent session. The event keeps everything else. */
108
+ function relink(bytes, helper, link, f) {
109
+ if (f.parentSession === undefined || f.launchKey === undefined)
110
+ return bytes;
105
111
  const p = JSON.parse(bytes.toString("utf8"));
106
- delete p.cocheckLaunchKey;
107
- return Buffer.from(JSON.stringify({ ...p, agentId: helper, parentAgentId: link.parent, ...(link.launchKey === undefined ? {} : { cocheckLaunchKey: link.launchKey }), ...(link.agentType === undefined || typeof p.agentType === "string" ? {} : { agentType: link.agentType }) }));
112
+ delete p[f.launchKey];
113
+ return Buffer.from(JSON.stringify({ ...p, [f.agentId]: helper, [f.parentSession]: link.parent, ...(link.launchKey === undefined ? {} : { [f.launchKey]: link.launchKey }), ...(link.agentType === undefined || typeof p[f.agentType] === "string" ? {} : { [f.agentType]: link.agentType }) }));
108
114
  }
109
115
  /** An event of the host cannot carry the key itself: it is CoCheck's, put on by the gate. */
110
- function stripLaunchKey(bytes) {
116
+ function stripLaunchKey(bytes, f) {
117
+ if (f.launchKey === undefined)
118
+ return bytes;
111
119
  try {
112
120
  const p = JSON.parse(bytes.toString("utf8"));
113
- if (p === null || typeof p !== "object" || !("cocheckLaunchKey" in p))
121
+ if (p === null || typeof p !== "object" || !(f.launchKey in p))
114
122
  return bytes;
115
- delete p.cocheckLaunchKey;
123
+ delete p[f.launchKey];
116
124
  return Buffer.from(JSON.stringify(p));
117
125
  }
118
126
  catch {
@@ -121,7 +129,7 @@ function stripLaunchKey(bytes) {
121
129
  }
122
130
  const isToolKind = (k) => k === "tool-pre" || k === "tool-post" || k === "tool-failure";
123
131
  /**
124
- * The gate for a Copilot event after the tracking switch was read (`started`). A tracked session's event goes on (a `task` call also opens a
132
+ * The gate for an event of a host with helper sessions after the tracking switch was read (`started`). A tracked session's event goes on (a launcher call also opens a
125
133
  * launch, and a `subagentStop` links its helper and releases what was held for it). An untracked session's event follows the four rules above.
126
134
  */
127
135
  export async function helperGate(home, started, ev, bytes, eventName, now) {
@@ -129,10 +137,13 @@ export async function helperGate(home, started, ev, bytes, eventName, now) {
129
137
  const stop = { stop: true, bytes, started, replay: [] };
130
138
  if (typeof ev.session !== "string" || ev.session === "")
131
139
  return go(bytes, started);
140
+ const profile = profileOf(ev.host);
141
+ const f = profile.fields;
142
+ const promptEvents = Object.entries(profile.eventKinds).filter(([, k]) => k === "prompt").map(([n]) => n);
132
143
  const input = ev.toolInput !== null && typeof ev.toolInput === "object" ? ev.toolInput : {};
133
144
  if (started !== undefined) {
134
- if (ev.kind === "tool-pre" && ev.toolName === "task" && ev.agentId === undefined)
135
- await noteLaunch(home, ev.session, input.prompt, input.agent_type, now, profileOf("copilot").calls?.argsKey(ev.toolInput));
145
+ if (ev.kind === "tool-pre" && isLauncherTool(ev.toolName, ev.host) && ev.agentId === undefined)
146
+ await noteLaunch(home, ev.session, input.prompt, input.agent_type, now, profile.calls?.argsKey(ev.toolInput));
136
147
  if (typeof ev.agentId === "string" && ev.agentId !== "" && ev.kind !== "subagent-stop" && existsSync(path.join(home, STORE))) {
137
148
  // A helper's first prompt (the CLI sends agentId on it) is matched, word for word, to the one open launch carrying that prompt;
138
149
  // its later calls name that launch. Two launches with the same prompt make no link; the helper's calls then name no launch.
@@ -140,7 +151,7 @@ export async function helperGate(home, started, ev, bytes, eventName, now) {
140
151
  const store = read(home);
141
152
  const before = lastRead;
142
153
  const agent = ev.agentId;
143
- if (ev.eventName === "userPromptSubmitted" && typeof ev.prompt === "string" && store.links[agent] === undefined) {
154
+ if (ev.eventName !== undefined && promptEvents.includes(ev.eventName) && typeof ev.prompt === "string" && store.links[agent] === undefined) {
144
155
  const same = store.launches.filter((l) => l.parent === ev.session && l.prompt === ev.prompt.trim());
145
156
  if (same.length === 1 && same[0].key !== undefined) {
146
157
  store.launches.splice(store.launches.indexOf(same[0]), 1);
@@ -149,7 +160,7 @@ export async function helperGate(home, started, ev, bytes, eventName, now) {
149
160
  }
150
161
  const link = store.links[agent];
151
162
  write(home, store, before);
152
- return go(link?.launchKey !== undefined && link.parent === ev.session ? relink(bytes, agent, link) : stripLaunchKey(bytes), started);
163
+ return go(link?.launchKey !== undefined && link.parent === ev.session ? relink(bytes, agent, link, f) : stripLaunchKey(bytes, f), started);
153
164
  });
154
165
  }
155
166
  if (ev.kind === "subagent-stop" && typeof ev.agentId === "string") {
@@ -159,7 +170,7 @@ export async function helperGate(home, started, ev, bytes, eventName, now) {
159
170
  const i = linked !== undefined && linked.parent === ev.session ? -1 : store.launches.findIndex((l) => l.parent === ev.session && (ev.agentType === undefined || l.agentType === undefined || l.agentType === ev.agentType));
160
171
  const launch = i >= 0 ? store.launches.splice(i, 1)[0] : undefined;
161
172
  const link = linked !== undefined && linked.parent === ev.session ? { ...linked, at: now } : { parent: ev.session, ...(launch?.agentType !== undefined ? { agentType: launch.agentType } : {}), at: now };
162
- const held = takeHeldLocked(home, ev.agentId, link, now);
173
+ const held = takeHeldLocked(home, ev.agentId, link, now, f);
163
174
  store.links[ev.agentId] = link;
164
175
  prune(home, store, now);
165
176
  write(home, store);
@@ -177,9 +188,9 @@ export async function helperGate(home, started, ev, bytes, eventName, now) {
177
188
  prune(home, store, now);
178
189
  const link = store.links[ev.session];
179
190
  if (link !== undefined) {
180
- const parent = trackingOf(home, "copilot", link.parent);
191
+ const parent = trackingOf(home, ev.host, link.parent);
181
192
  write(home, store, before);
182
- return parent === undefined ? stop : go(relink(bytes, ev.session, link), parent);
193
+ return parent === undefined ? stop : go(relink(bytes, ev.session, link, f), parent);
183
194
  }
184
195
  // A helper's first prompt carrying a tracked session's open launch prompt, word for word.
185
196
  if (ev.kind === "prompt" && typeof ev.prompt === "string") {
@@ -190,7 +201,7 @@ export async function helperGate(home, started, ev, bytes, eventName, now) {
190
201
  const made = { parent: launch.parent, ...(launch.agentType === undefined ? {} : { agentType: launch.agentType }), ...(launch.key === undefined ? {} : { launchKey: launch.key }), at: now };
191
202
  store.links[ev.session] = made;
192
203
  write(home, store);
193
- return go(relink(bytes, ev.session, made), trackingOf(home, "copilot", made.parent));
204
+ return go(relink(bytes, ev.session, made, f), trackingOf(home, ev.host, made.parent));
194
205
  }
195
206
  }
196
207
  if (ev.kind === "subagent-stop" && typeof ev.agentId === "string")
@@ -221,7 +232,7 @@ function hold(home, session, event, bytes, now) {
221
232
  appendFileSync(file, JSON.stringify({ event, at: now, body: scrubPayloadBytes(bytes).toString("utf8") }) + "\n", { mode: 0o600 });
222
233
  }
223
234
  /** The held events of `helper`, relinked to its main session, oldest first; the file goes. */
224
- function takeHeldLocked(home, helper, link, now) {
235
+ function takeHeldLocked(home, helper, link, now, f) {
225
236
  const file = heldFile(home, helper);
226
237
  let text = "";
227
238
  try {
@@ -236,7 +247,7 @@ function takeHeldLocked(home, helper, link, now) {
236
247
  try {
237
248
  const v = JSON.parse(line);
238
249
  if (typeof v?.event === "string" && typeof v?.body === "string" && typeof v?.at === "number" && now - v.at < HELD_TTL_MS)
239
- out.push({ event: v.event, bytes: relink(Buffer.from(v.body), helper, link) });
250
+ out.push({ event: v.event, bytes: relink(Buffer.from(v.body), helper, link, f) });
240
251
  }
241
252
  catch {
242
253
  // a half-written line is not an event