carrick 0.3.151 → 0.3.153

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 (60) hide show
  1. package/bin/carrick.mjs +1 -0
  2. package/dist/contract.d.ts +35 -0
  3. package/dist/contract.js.map +1 -1
  4. package/dist/hook/bash-targets.d.ts +29 -0
  5. package/dist/hook/bash-targets.js +377 -0
  6. package/dist/hook/bash-targets.js.map +1 -0
  7. package/dist/hook/edit-flow.d.ts +13 -0
  8. package/dist/hook/edit-flow.js +170 -0
  9. package/dist/hook/edit-flow.js.map +1 -0
  10. package/dist/hook/grep-missed.d.ts +19 -0
  11. package/dist/hook/grep-missed.js +129 -0
  12. package/dist/hook/grep-missed.js.map +1 -0
  13. package/dist/hook/late.d.ts +1 -1
  14. package/dist/hook/late.js +24 -22
  15. package/dist/hook/late.js.map +1 -1
  16. package/dist/hook/lead.d.ts +13 -0
  17. package/dist/hook/lead.js +39 -0
  18. package/dist/hook/lead.js.map +1 -0
  19. package/dist/hook/post-bash.d.ts +2 -0
  20. package/dist/hook/post-bash.js +109 -0
  21. package/dist/hook/post-bash.js.map +1 -0
  22. package/dist/hook/post-edit.js +3 -136
  23. package/dist/hook/post-edit.js.map +1 -1
  24. package/dist/hook/post-read.js +3 -31
  25. package/dist/hook/post-read.js.map +1 -1
  26. package/dist/hook/read-answer.d.ts +6 -0
  27. package/dist/hook/read-answer.js +45 -0
  28. package/dist/hook/read-answer.js.map +1 -0
  29. package/dist/hook/reuse.d.ts +28 -0
  30. package/dist/hook/reuse.js +41 -0
  31. package/dist/hook/reuse.js.map +1 -1
  32. package/dist/hook/stop-wait.d.ts +2 -1
  33. package/dist/hook/stop-wait.js +79 -29
  34. package/dist/hook/stop-wait.js.map +1 -1
  35. package/dist/hook/stop.js +3 -5
  36. package/dist/hook/stop.js.map +1 -1
  37. package/dist/init/agents-section.js +3 -2
  38. package/dist/init/agents-section.js.map +1 -1
  39. package/dist/init/outdated.d.ts +3 -2
  40. package/dist/init/outdated.js +9 -2
  41. package/dist/init/outdated.js.map +1 -1
  42. package/dist/init/settings.js +6 -0
  43. package/dist/init/settings.js.map +1 -1
  44. package/dist/render.d.ts +33 -31
  45. package/dist/render.js +103 -90
  46. package/dist/render.js.map +1 -1
  47. package/package.json +6 -6
  48. package/plugin/.claude-plugin/plugin.json +1 -1
  49. package/sidecar/dist/src/capture/index.d.ts +1 -1
  50. package/sidecar/dist/src/capture/index.js +1 -1
  51. package/sidecar/dist/src/capture/reference-redirect.d.ts +16 -1
  52. package/sidecar/dist/src/capture/reference-redirect.js +137 -3
  53. package/sidecar/dist/src/index.js +1 -0
  54. package/sidecar/dist/src/library-claims.js +76 -9
  55. package/sidecar/dist/src/project-loader.d.ts +8 -0
  56. package/sidecar/dist/src/project-loader.js +9 -2
  57. package/sidecar/dist/src/types.d.ts +31 -0
  58. package/sidecar/dist/src/validators.d.ts +6 -0
  59. package/sidecar/dist/src/validators.js +1 -0
  60. package/templates/skills/carrick-reuse.md +1 -1
@@ -0,0 +1,170 @@
1
+ // The post-edit hook's work once the edited files are known: ask the binary
2
+ // once, queue what needs a re-scan, print what the edit needs acted on.
3
+ //
4
+ // Shared by `hook post-edit` (a file edit, either host) and `hook post-bash`
5
+ // (a shell command that wrote a source file), so a write through the shell
6
+ // gets the same answer an Edit does: a finding, or nothing.
7
+ import path from "node:path";
8
+ import { createLogger } from "../log.js";
9
+ import { renderPostToolUse } from "../render.js";
10
+ import { resolveRoot, rootNote } from "../root.js";
11
+ import { throttledVersionMismatch } from "../init/outdated.js";
12
+ import { currentVersion } from "../update.js";
13
+ import { markLed, markTold, recordEdit, toldKey, wasLed, wasTold } from "./reuse.js";
14
+ import { leadLines } from "./lead.js";
15
+ import { deliverLate } from "./late.js";
16
+ import { needsBackground, probeEdits } from "./probe.js";
17
+ import { inOtherWorktree } from "./worktree.js";
18
+ import { answerInHand, enqueue, ensureRunner } from "./rechecks.js";
19
+ const log = createLogger("carrick-hook");
20
+ /** Print one `additionalContext` object, which the host attaches to the tool result. */
21
+ export function emit(context) {
22
+ process.stdout.write(JSON.stringify({
23
+ hookSpecificOutput: {
24
+ hookEventName: "PostToolUse",
25
+ additionalContext: context,
26
+ },
27
+ }));
28
+ }
29
+ /**
30
+ * Check `edited` (absolute paths, at least one) as the post-edit hook does and
31
+ * print the one object for them. `extra` lines (a read answer from the same
32
+ * command) are printed first, in the same object.
33
+ */
34
+ export async function editFlow(input, edited, extra = []) {
35
+ const { cwd, session } = input;
36
+ const channel = { channel: input.channel };
37
+ // One root for the call: every file in one patch is in the workspace the tool
38
+ // ran in, and resolving it per file would ask the same question repeatedly.
39
+ const choice = resolveRoot({
40
+ clientRoot: cwd,
41
+ projectDir: process.env["CLAUDE_PROJECT_DIR"] ?? null,
42
+ filePath: edited[0],
43
+ });
44
+ const note = rootNote(choice);
45
+ if (note)
46
+ log(note);
47
+ // Stamped before any line is built: a `carrick check --changed` that began
48
+ // earlier did not read this edit (`checked.ts`). The files themselves are
49
+ // recorded once the probe has answered (below).
50
+ const editedAt = Date.now();
51
+ if (session)
52
+ recordEdit(session, choice.root, editedAt);
53
+ const contexts = [];
54
+ // Files whose uses lines this message carries, remembered only once it is
55
+ // delivered: who uses a file is shown once per file per session (carrick#2067).
56
+ const told = [];
57
+ // Lead lines (carrick#3249) are remembered the same way, once delivered.
58
+ const led = [];
59
+ const relatives = edited.map((file) => path.relative(choice.root, file));
60
+ // The edit just landed, so the index describes the file as it was before
61
+ // it. The hook asks the binary once, with a probe that never starts a
62
+ // re-scan: an answer that needs none (the file reads the same, no service
63
+ // reads it, a known miss) comes back at once and prints below. Every other
64
+ // file is queued for the background runner (carrick#3198).
65
+ const answers = await probeEdits(relatives, choice.root);
66
+ const queueable = channel.channel === "hook" && Boolean(session);
67
+ const toQueue = [];
68
+ const rendered = new Map();
69
+ const leads = new Map();
70
+ for (const [index, relative] of relatives.entries()) {
71
+ const result = answers.results[index] ?? null;
72
+ if (!result) {
73
+ // A probe with no answer queues every file; one unreadable file in a
74
+ // patch of four is no reason to say nothing about the other three.
75
+ log("no answer for", relative, answers.failure ?? "");
76
+ if (queueable)
77
+ toQueue.push(index);
78
+ continue;
79
+ }
80
+ // The lead rides on the probe answer, so a queued file gets it too.
81
+ const found = leadLines(result, relative, (key) => !session || !wasLed(session, key), log);
82
+ if (found.lines.length > 0)
83
+ leads.set(index, found);
84
+ if (queueable && needsBackground(result)) {
85
+ toQueue.push(index);
86
+ continue;
87
+ }
88
+ const key = toldKey(result, relative);
89
+ const show = !session || !wasTold(session, key);
90
+ if (show && result.uses_lines?.length)
91
+ told.push(key);
92
+ const context = renderPostToolUse(result, relative, { uses: show });
93
+ log(`check ${relative} -> ${context ? "context" : "nothing to say"} in ${answers.ms}ms`);
94
+ if (context)
95
+ rendered.set(index, context);
96
+ }
97
+ const queued = new Set();
98
+ if (toQueue.length > 0 && session) {
99
+ const entries = enqueue(choice.root, session, toQueue.map((index) => edited[index]), editedAt);
100
+ if (entries.length > 0)
101
+ ensureRunner(choice.root);
102
+ for (const entry of entries)
103
+ queued.add(entry.file);
104
+ // A queued file says nothing now: its answer is delivered with the next
105
+ // Carrick line only when it holds a finding, and a refused queue entry is a
106
+ // gap the Stop hook names (hook-wording audit T4).
107
+ }
108
+ // What the Stop hook states the session's check from (carrick#3466): each
109
+ // file, at the time its entry or in-hand answer carries, and every answer
110
+ // this hook had in hand as the session's answer for that edit. Only on the
111
+ // hook channel, which is the one that queues and speaks at Stop. A file the
112
+ // binary refused (`error`: no index, an unreadable one) is not recorded:
113
+ // there is no check to state, and a fallback would fail the same way.
114
+ if (session && channel.channel === "hook") {
115
+ const recorded = [];
116
+ for (const [index, file] of edited.entries()) {
117
+ const result = answers.results[index] ?? null;
118
+ if (result?.error)
119
+ continue;
120
+ // A subagent's own worktree is not this session's change (carrick#3543).
121
+ if (inOtherWorktree(file, cwd ?? process.cwd()))
122
+ continue;
123
+ if (result !== null && !queued.has(file))
124
+ answerInHand(choice.root, session, file, result, editedAt);
125
+ if (result !== null || queued.has(file))
126
+ recorded.push(file);
127
+ }
128
+ if (recorded.length > 0)
129
+ recordEdit(session, choice.root, editedAt, undefined, recorded);
130
+ }
131
+ // Delivered after this edit is queued, so an older answer for a file this
132
+ // edit queues again is skipped (the newer entry carries it).
133
+ const late = channel.channel === "hook" ? deliverLate(session) : [];
134
+ contexts.push(...late);
135
+ for (const index of relatives.keys()) {
136
+ const context = rendered.get(index);
137
+ if (context)
138
+ contexts.push(context);
139
+ const found = leads.get(index);
140
+ if (found) {
141
+ // A late answer delivered just above may have claimed the same function.
142
+ for (const [at, key] of found.keys.entries()) {
143
+ if (session && wasLed(session, key))
144
+ continue;
145
+ contexts.push(found.lines[at]);
146
+ led.push(key);
147
+ }
148
+ }
149
+ }
150
+ if (channel.channel !== "hook") {
151
+ log(`the ${channel.channel} channel owns delivery in this install; printing nothing`);
152
+ return;
153
+ }
154
+ // The `carrick` this hook is running is whatever PATH answered with, which
155
+ // an upgrade elsewhere can leave behind (carrick#1372). Claimed at the last
156
+ // possible moment, so the once-a-day stamp is spent only on a line that is
157
+ // actually delivered, and inside `additionalContext` rather than on a bare
158
+ // stdout line, which this host reads as a malformed hook response.
159
+ const mismatch = throttledVersionMismatch(choice.root, currentVersion());
160
+ if (mismatch)
161
+ contexts.push(mismatch);
162
+ if (told.length > 0 && session)
163
+ markTold(session, told);
164
+ if (led.length > 0 && session)
165
+ markLed(session, led);
166
+ contexts.unshift(...extra);
167
+ if (contexts.length > 0)
168
+ emit(contexts.join("\n\n"));
169
+ }
170
+ //# sourceMappingURL=edit-flow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"edit-flow.js","sourceRoot":"","sources":["../../src/hook/edit-flow.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,wEAAwE;AACxE,EAAE;AACF,6EAA6E;AAC7E,2EAA2E;AAC3E,4DAA4D;AAE5D,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW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ACH,CAAC;IACH,CAAC;IAED,IAAI,OAAO,CAAC,OAAO,KAAK,MAAM,EAAE,CAAC;QAC/B,GAAG,CAAC,OAAO,OAAO,CAAC,OAAO,0DAA0D,CAAC,CAAC;QACtF,OAAO;IACT,CAAC;IACD,2EAA2E;IAC3E,4EAA4E;IAC5E,2EAA2E;IAC3E,2EAA2E;IAC3E,mEAAmE;IACnE,MAAM,QAAQ,GAAG,wBAAwB,CAAC,MAAM,CAAC,IAAI,EAAE,cAAc,EAAE,CAAC,CAAC;IACzE,IAAI,QAAQ;QAAE,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IACtC,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO;QAAE,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACxD,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,OAAO;QAAE,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;IACrD,QAAQ,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;IAC3B,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC;QAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;AACvD,CAAC"}
@@ -0,0 +1,19 @@
1
+ type Site = {
2
+ file: string;
3
+ line: number;
4
+ };
5
+ type Definition = {
6
+ file: string;
7
+ line: number;
8
+ callers: Site[];
9
+ total: number;
10
+ };
11
+ type NameMap = Record<string, Definition[]>;
12
+ /** The name map for the index under `root`, from the cache when the index has not changed. Null when there is no index. */
13
+ export declare function nameMap(root: string, home?: string): NameMap | null;
14
+ /**
15
+ * One line per searched name whose index sites the grep output lacks, or null.
16
+ * `output` is the command's stdout, `root` the workspace holding the index.
17
+ */
18
+ export declare function missedSites(names: string[], operands: string[], output: string, root: string, cwd: string, session: string | undefined, home?: string): string | null;
19
+ export {};
@@ -0,0 +1,129 @@
1
+ // The call sites a grep did not print, from the local index.
2
+ //
3
+ // A search for a function's name prints the files the search reached. The
4
+ // index already holds every caller of each function it knows (`uses` in
5
+ // `.carrick/index.json`), so a caller the output lacks is a site the agent
6
+ // has not seen. This says where they are, and nothing when the output already
7
+ // has them all: it never repeats what grep printed. It runs only after a
8
+ // grep or rg whose pattern names one identifier (`searchedNames`), asks no
9
+ // one, and costs $0.
10
+ //
11
+ // The index is read through a small name map cached beside the session
12
+ // records, keyed by the index file's mtime and size, so a grep after the first
13
+ // does not parse the whole index again.
14
+ import crypto from "node:crypto";
15
+ import fs from "node:fs";
16
+ import os from "node:os";
17
+ import path from "node:path";
18
+ import { sessionsDir, markTold, wasTold, writeAtomic } from "./reuse.js";
19
+ /** At most this many sites are named; the rest are counted. */
20
+ const MAX_SITES = 5;
21
+ /** At most this many names are answered for one command. */
22
+ const MAX_NAMES = 2;
23
+ /** The functions of at least five characters in the index, by name, with absolute paths. */
24
+ function buildMap(indexFile) {
25
+ const index = JSON.parse(fs.readFileSync(indexFile, "utf8"));
26
+ const map = {};
27
+ for (const repo of index.repos ?? []) {
28
+ if (typeof repo.path !== "string")
29
+ continue;
30
+ for (const [file, entry] of Object.entries(repo.uses ?? {})) {
31
+ for (const fn of entry.functions ?? []) {
32
+ if (typeof fn.name !== "string" || fn.name.length < 5 || typeof fn.line !== "number")
33
+ continue;
34
+ const callers = (fn.callers ?? [])
35
+ .filter((caller) => typeof caller.file === "string" && typeof caller.line === "number")
36
+ .map((caller) => ({ file: path.join(repo.path, caller.file), line: caller.line }));
37
+ (map[fn.name] ??= []).push({
38
+ file: path.join(repo.path, file),
39
+ line: fn.line,
40
+ callers,
41
+ total: typeof fn.callers_total === "number" ? fn.callers_total : callers.length,
42
+ });
43
+ }
44
+ }
45
+ }
46
+ return map;
47
+ }
48
+ /** The name map for the index under `root`, from the cache when the index has not changed. Null when there is no index. */
49
+ export function nameMap(root, home = os.homedir()) {
50
+ const indexFile = path.join(root, ".carrick", "index.json");
51
+ let stamp;
52
+ try {
53
+ const stat = fs.statSync(indexFile);
54
+ stamp = `${stat.mtimeMs}:${stat.size}`;
55
+ }
56
+ catch {
57
+ return null;
58
+ }
59
+ const key = crypto.createHash("sha1").update(root).digest("hex").slice(0, 12);
60
+ const cache = path.join(sessionsDir(home), `uses-${key}.map`);
61
+ try {
62
+ const cached = JSON.parse(fs.readFileSync(cache, "utf8"));
63
+ if (cached.stamp === stamp && cached.names)
64
+ return cached.names;
65
+ }
66
+ catch {
67
+ // No cache, or a half-written one: rebuild.
68
+ }
69
+ let names;
70
+ try {
71
+ names = buildMap(indexFile);
72
+ }
73
+ catch {
74
+ return null;
75
+ }
76
+ writeAtomic(cache, JSON.stringify({ stamp, names }));
77
+ return names;
78
+ }
79
+ /** Whether `output` printed the file at `abs`: by its path from the root, from the cwd, or in full. */
80
+ function printed(output, abs, root, cwd, operands) {
81
+ if (operands.has(abs))
82
+ return true;
83
+ const fromRoot = path.relative(root, abs);
84
+ const fromCwd = path.relative(cwd, abs);
85
+ return (output.includes(abs) ||
86
+ (!fromRoot.startsWith("..") && output.includes(fromRoot)) ||
87
+ (!fromCwd.startsWith("..") && output.includes(fromCwd)));
88
+ }
89
+ /**
90
+ * One line per searched name whose index sites the grep output lacks, or null.
91
+ * `output` is the command's stdout, `root` the workspace holding the index.
92
+ */
93
+ export function missedSites(names, operands, output, root, cwd, session, home) {
94
+ const map = nameMap(root, home);
95
+ if (map === null)
96
+ return null;
97
+ const files = new Set(operands);
98
+ const lines = [];
99
+ for (const name of names) {
100
+ if (lines.length >= MAX_NAMES)
101
+ break;
102
+ const definitions = map[name];
103
+ // A name defined in more than one place is a word, not a function.
104
+ if (!definitions || definitions.length !== 1)
105
+ continue;
106
+ const told = `grep:${name}`;
107
+ if (session && wasTold(session, told, home))
108
+ continue;
109
+ const definition = definitions[0];
110
+ const rel = (file) => path.relative(root, file);
111
+ const missed = definition.callers.filter((caller) => caller.file !== definition.file && !printed(output, caller.file, root, cwd, files));
112
+ const definitionMissed = !printed(output, definition.file, root, cwd, files);
113
+ if (missed.length === 0 && !definitionMissed)
114
+ continue;
115
+ const where = `${rel(definition.file)}:${definition.line}`;
116
+ if (missed.length === 0) {
117
+ lines.push(`Carrick: \`${name}\` is defined at ${where}, where this search did not reach.`);
118
+ }
119
+ else {
120
+ const shown = missed.slice(0, MAX_SITES).map((site) => `${rel(site.file)}:${site.line}`);
121
+ const more = missed.length - shown.length;
122
+ lines.push(`Carrick: \`${name}\` (${where}) is also called where this search did not reach: ${shown.join(", ")}${more > 0 ? ` (+${more} more)` : ""}.`);
123
+ }
124
+ if (session)
125
+ markTold(session, [told], home);
126
+ }
127
+ return lines.length > 0 ? lines.join("\n") : null;
128
+ }
129
+ //# sourceMappingURL=grep-missed.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grep-missed.js","sourceRoot":"","sources":["../../src/hook/grep-missed.ts"],"names":[],"mappings":"AAAA,6DAA6D;AAC7D,EAAE;AACF,0EAA0E;AAC1E,wEAAwE;AACxE,2EAA2E;AAC3E,8EAA8E;AAC9E,yEAAyE;AACzE,2EAA2E;AAC3E,qBAAqB;AACrB,EAAE;AACF,uEAAuE;AACvE,+EAA+E;AAC/E,wCAAwC;AAExC,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzE,+DAA+D;AAC/D,MAAM,SAAS,GAAG,CAAC,CAAC;AACpB,4DAA4D;AAC5D,MAAM,SAAS,GAAG,CAAC,CAAC;AAMpB,4FAA4F;AAC5F,SAAS,QAAQ,CAAC,SAAiB;IACjC,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAK1D,CAAC;IACF,MAAM,GAAG,GAA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AAE,EAAE,CAAC,GAAG,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;YACzF,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC,MAAM,CAAC;YAC1C,KAAK,CAAC,IAAI,CACR,cAAc,IAAI,OAAO,KAAK,qDAAqD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,IAAI,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,CAC5I,CAAC;QACJ,CAAC;QACD,IAAI,OAAO;YAAE,QAAQ,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;AACpD,CAAC"}
@@ -1,6 +1,6 @@
1
1
  import type { Answer } from "./rechecks.ts";
2
2
  /** The words for one finished answer, or null when there is nothing to say. */
3
- export declare function lateContext(answer: Answer, uses: boolean, settled?: boolean): string | null;
3
+ export declare function lateContext(answer: Answer, uses: boolean): string | null;
4
4
  /**
5
5
  * Claim and render every finished answer this session has not been given.
6
6
  * Returns the contexts to print, oldest first. Nothing here throws.
package/dist/hook/late.js CHANGED
@@ -7,31 +7,24 @@
7
7
  // any hook speaks. Each answer is claimed with `claimDelivery`, so parallel
8
8
  // hooks print it once.
9
9
  import { createLogger } from "../log.js";
10
- import { answersTheEdit, changedFailedLine, changedLines, notRecheckedLine, renderPostToolUse } from "../render.js";
10
+ import { answersTheEdit, changedLines, renderPostToolUse } from "../render.js";
11
11
  import { claimDelivery, finishedFor, isChanged } from "./rechecks.js";
12
- import { checkedSinceLastEdit } from "./checked.js";
13
- import { markTold, toldKey, wasTold } from "./reuse.js";
12
+ import { markLed, markTold, toldKey, wasLed, wasTold } from "./reuse.js";
13
+ import { leadLines } from "./lead.js";
14
14
  const log = createLogger("carrick-late");
15
15
  /** The words for one finished answer, or null when there is nothing to say. */
16
- export function lateContext(answer, uses, settled = false) {
17
- // The whole-change check a Stop queued (carrick#3466): its verdict, or that
18
- // it gave none. Never a clause: it is the check the clause asks for.
19
- if (isChanged(answer)) {
20
- if (answer.changed)
21
- return changedLines(answer.changed);
22
- return changedFailedLine(answer.state === "stopped" ? (answer.failure ?? "it stopped") : "the binary failed or printed no answer");
23
- }
24
- if (!answer.result) {
25
- // An internal failure is logged, never shown: the reader needs the next step.
26
- const reason = answer.state === "stopped" ? (answer.failure ?? "the background re-check stopped") : "the background re-check gave no answer";
27
- return notRecheckedLine(answer.relative, reason, settled);
28
- }
29
- if (answer.result.error)
16
+ export function lateContext(answer, uses) {
17
+ // The whole-change check a Stop queued (carrick#3466) speaks only with a
18
+ // problem: a clean run, a run that could not check and a run that gave no
19
+ // answer print nothing (a failure is logged by `deliverLate`).
20
+ if (isChanged(answer))
21
+ return answer.changed ? changedLines(answer.changed) || null : null;
22
+ // A re-check that stopped, gave no answer or answered nothing prints
23
+ // nothing: a failure is logged by `deliverLate`, and the Stop hook names a
24
+ // file left unchecked.
25
+ if (!answer.result || answer.result.error || !answersTheEdit(answer.result))
30
26
  return null;
31
- if (!answersTheEdit(answer.result)) {
32
- return notRecheckedLine(answer.relative, answer.result.recheck?.reason, settled);
33
- }
34
- return renderPostToolUse(answer.result, answer.relative, { uses, late: true, settled });
27
+ return renderPostToolUse(answer.result, answer.relative, { uses, late: true });
35
28
  }
36
29
  /**
37
30
  * Claim and render every finished answer this session has not been given.
@@ -49,9 +42,18 @@ export function deliverLate(session, now = Date.now()) {
49
42
  log("background re-check failed", answer.relative, answer.failure);
50
43
  const key = answer.result ? toldKey(answer.result, answer.relative) : null;
51
44
  const uses = key === null || !wasTold(session, key);
52
- const context = lateContext(answer, uses, checkedSinceLastEdit(session, answer.root));
45
+ const context = lateContext(answer, uses);
53
46
  const seconds = Math.round((now - Date.parse(answer.enqueued_at)) / 1000);
54
47
  log(`late answer ${answer.relative} delivered ${seconds} s after the edit`);
48
+ // A lead the probe already delivered is in `led`; this one is for a file
49
+ // whose probe had no answer (carrick#3249).
50
+ const found = answer.result
51
+ ? leadLines(answer.result, answer.relative, (leadKey) => !wasLed(session, leadKey), log)
52
+ : null;
53
+ if (found && found.lines.length > 0) {
54
+ contexts.push(...found.lines);
55
+ markLed(session, found.keys);
56
+ }
55
57
  if (!context)
56
58
  continue;
57
59
  contexts.push(context);
@@ -1 +1 @@
1
- {"version":3,"file":"late.js","sourceRoot":"","sources":["../../src/hook/late.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,EAAE;AACF,+EAA+E;AAC/E,+EAA+E;AAC/E,6EAA6E;AAC7E,2EAA2E;AAC3E,4EAA4E;AAC5E,uBAAuB;AAEvB,OAAO,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,YAAY,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACpH,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAEtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAExD,MAAM,GAAG,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;AAEzC,+EAA+E;AAC/E,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,IAAa,EAAE,OAAO,GAAG,KAAK;IACxE,4EAA4E;IAC5E,qEAAqE;IACrE,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC;QACtB,IAAI,MAAM,CAAC,OAAO;YAAE,OAAO,YAAY,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACxD,OAAO,iBAAiB,CACtB,M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IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;gBAC9B,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,IAAI,CAAC,CAAC;YAC/B,CAAC;YACD,IAAI,CAAC,OAAO;gBAAE,SAAS;YACvB,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;YACvB,IAAI,GAAG,KAAK,IAAI,IAAI,IAAI,IAAI,MAAM,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM;gBAAE,QAAQ,CAAC,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;QAC1F,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,GAAG,CAAC,sBAAsB,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC7C,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
@@ -0,0 +1,13 @@
1
+ import type { CheckResult } from "../contract.ts";
2
+ /** What one answer's leads need to print: the lines, and the keys to claim on delivery. */
3
+ export type LeadLines = {
4
+ lines: string[];
5
+ keys: string[];
6
+ };
7
+ /** The session key of a lead: the edited file and the new function's name. */
8
+ export declare function leadKey(result: CheckResult, relative: string, name: string): string;
9
+ /**
10
+ * One line for each new function of `result` that carries a lead and has not
11
+ * been shown (`isNew`). The score and basis go to the log, never into the text.
12
+ */
13
+ export declare function leadLines(result: CheckResult, relative: string, isNew: (key: string) => boolean, log?: (...parts: string[]) => void): LeadLines;
@@ -0,0 +1,39 @@
1
+ // The line an edit gets when a function it added looks like one the index
2
+ // already holds (carrick#3249). The binary decides (`recheck.new_functions[].lead`,
3
+ // `docs/local-mode-output.md`); this renders it, once per function per session.
4
+ import path from "node:path";
5
+ /** The session key of a lead: the edited file and the new function's name. */
6
+ export function leadKey(result, relative, name) {
7
+ return `lead::${result.repo ?? ""}::${result.file ?? relative}::${name}`;
8
+ }
9
+ function where(lead, ownRepo) {
10
+ // `lead.repo` is a checkout folder name or an indexed repo name, never a path.
11
+ const spot = `${lead.file}:${lead.line}`;
12
+ return ownRepo !== null && lead.repo !== ownRepo ? `${lead.repo}/${spot}` : spot;
13
+ }
14
+ /**
15
+ * One line for each new function of `result` that carries a lead and has not
16
+ * been shown (`isNew`). The score and basis go to the log, never into the text.
17
+ */
18
+ export function leadLines(result, relative, isNew, log = () => { }) {
19
+ const out = { lines: [], keys: [] };
20
+ if (result.error)
21
+ return out;
22
+ const ownRepo = result.repo ? path.basename(result.repo) : null;
23
+ for (const made of result.recheck?.new_functions ?? []) {
24
+ const lead = made.lead;
25
+ if (!lead)
26
+ continue;
27
+ const key = leadKey(result, relative, made.name);
28
+ if (out.keys.includes(key) || !isNew(key))
29
+ continue;
30
+ const callers = lead.callers.map((caller) => `${caller.file}:${caller.line}`);
31
+ const more = lead.callers_total - callers.length;
32
+ const calledFrom = callers.length === 0 ? "" : ` (called from ${callers.join(", ")}${more > 0 ? `, +${more} more` : ""})`;
33
+ out.lines.push(`Carrick: \`${made.name}\` looks like \`${lead.name}\` at ${where(lead, ownRepo)}${calledFrom}: reuse or extend it if it does the same job.`);
34
+ out.keys.push(key);
35
+ log(`lead ${relative}::${made.name} -> ${lead.file}:${lead.line} basis=${lead.basis} score=${lead.score}`);
36
+ }
37
+ return out;
38
+ }
39
+ //# sourceMappingURL=lead.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lead.js","sourceRoot":"","sources":["../../src/hook/lead.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,oFAAoF;AACpF,gFAAgF;AAEhF,OAAO,IAAI,MAAM,WAAW,CAAC;AAM7B,8EAA8E;AAC9E,MAAM,UAAU,OAAO,CAAC,MAAmB,EAAE,QAAgB,EAAE,IAAY;IACzE,OAAO,SAAS,MAAM,CAAC,IAAI,IAAI,EAAE,KAAK,MAAM,CAAC,IAAI,IAAI,QAAQ,KAAK,IAAI,EAAE,CAAC;AAC3E,CAAC;AAED,SAAS,KAAK,CAAC,IAAU,EAAE,OAAsB;IAC/C,+EAA+E;IA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C,MAAM,CAAC;QACjD,MAAM,UAAU,GACd,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,iBAAiB,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC;QACzG,GAAG,CAAC,KAAK,CAAC,IAAI,CACZ,cAAc,IAAI,CAAC,IAAI,mBAAmB,IAAI,CAAC,IAAI,SAAS,KAAK,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,UAAU,+CAA+C,CAC7I,CAAC;QACF,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnB,GAAG,CAAC,QAAQ,QAAQ,KAAK,IAAI,CAAC,IAAI,OAAO,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,UAAU,IAAI,CAAC,KAAK,UAAU,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;IAC7G,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,109 @@
1
+ #!/usr/bin/env node
2
+ // PostToolUse hook for a shell command, on Claude Code.
3
+ //
4
+ // Most reads and writes of a source file in an agent session go through the
5
+ // shell, where the Read and Edit hooks cannot fire. This decides in one process
6
+ // what the command touched. A read of a TypeScript file gets the Read hook's
7
+ // answer (who depends on it). A write of one (a redirect, `tee`, `sed -i`, a
8
+ // script that rewrites it, an `apply_patch`) goes through the post-edit flow
9
+ // (`edit-flow.ts`): the same answer an Edit gives, a finding or nothing.
10
+ //
11
+ // A write is a path the command names in a write shape whose mtime moved since
12
+ // this session last saw it. A path with no record counts when it was modified
13
+ // in the last two minutes (a file the command created, or rewrote just now).
14
+ // Commands that move the tree in bulk (`git checkout`, `npm ci`) start nothing.
15
+ //
16
+ // It runs on every shell command, so the work before a target is found is a
17
+ // node start, a stdin read and one regex: nothing else is imported. A command
18
+ // that names no source file prints nothing and starts nothing. Whatever
19
+ // happens it exits 0 and prints at most the one JSON object.
20
+ import { readStdin } from "./stdin.js";
21
+ import path from "node:path";
22
+ import fs from "node:fs";
23
+ import { bashTargets, MENTIONS_SOURCE, movesTreeInBulk, searchedNames } from "./bash-targets.js";
24
+ /** At most this many files are asked about for one command. */
25
+ const MAX_FILES = 2;
26
+ /** At most this many written files go through the edit flow for one command. */
27
+ const MAX_WRITES = 5;
28
+ /** A written file with no record counts when modified this recently. */
29
+ const FRESH_MS = 120_000;
30
+ /** A command that may search the tree. */
31
+ const SEARCH_WORD = /\b(grep|rg|egrep|fgrep)\b/;
32
+ async function main() {
33
+ let payload;
34
+ try {
35
+ payload = JSON.parse((await readStdin()) || "{}");
36
+ }
37
+ catch {
38
+ return;
39
+ }
40
+ const command = payload.tool_input?.command;
41
+ if (!command || !(MENTIONS_SOURCE.test(command) || SEARCH_WORD.test(command)))
42
+ return;
43
+ const cwd = payload.cwd ?? process.cwd();
44
+ const { reads, writes } = bashTargets(command, path.resolve(cwd));
45
+ const candidates = writes.length > 0 && !movesTreeInBulk(command, path.resolve(cwd)) ? writes : [];
46
+ // A search for a function's name: the index sites its output did not print.
47
+ const searched = SEARCH_WORD.test(command) ? searchedNames(command, path.resolve(cwd)) : { names: [], operands: [] };
48
+ if (reads.length === 0 && candidates.length === 0 && searched.names.length === 0)
49
+ return;
50
+ const session = payload.session_id;
51
+ const { markInvoker } = await import("./invoker.js");
52
+ const { resolveChannel } = await import("../channel.js");
53
+ const channel = resolveChannel({ hooksInstalled: true });
54
+ const reuse = await import("./reuse.js");
55
+ // Written: the mtime moved since the session last saw the file.
56
+ const written = [];
57
+ if (candidates.length > 0 && channel.channel !== "off") {
58
+ const mtimes = {};
59
+ for (const file of candidates) {
60
+ let mtime;
61
+ try {
62
+ mtime = fs.statSync(file).mtimeMs;
63
+ }
64
+ catch {
65
+ continue;
66
+ }
67
+ mtimes[file] = mtime;
68
+ const seen = session ? reuse.writtenMtime(session, file) : undefined;
69
+ if (seen === undefined ? Date.now() - mtime < FRESH_MS : mtime !== seen)
70
+ written.push(file);
71
+ }
72
+ if (session && Object.keys(mtimes).length > 0)
73
+ reuse.markWritten(session, mtimes);
74
+ }
75
+ const toEdit = written.slice(0, MAX_WRITES);
76
+ // The answer is the Read hook's (or the edit hook's), so the cloud's log tells it the same way.
77
+ markInvoker(toEdit.length > 0 ? "post-edit" : "post-read");
78
+ if (toEdit.length === 0 && channel.channel !== "hook")
79
+ return;
80
+ const { wasRead } = reuse;
81
+ const fresh = channel.channel === "hook" ? reads.filter((file) => !(session && wasRead(session, file))).slice(0, MAX_FILES) : [];
82
+ const { answerRead } = await import("./read-answer.js");
83
+ const answers = (await Promise.all(fresh.map((file) => answerRead(file, payload.cwd, session)))).filter((line) => line !== null);
84
+ const response = payload.tool_response;
85
+ const output = typeof response === "string" ? response : response?.stdout;
86
+ if (channel.channel === "hook" && searched.names.length > 0 && typeof output === "string") {
87
+ const { resolveRoot } = await import("../root.js");
88
+ const root = resolveRoot({ clientRoot: payload.cwd ?? null, projectDir: process.env["CLAUDE_PROJECT_DIR"] ?? null }).root;
89
+ const { missedSites } = await import("./grep-missed.js");
90
+ const line = missedSites(searched.names, searched.operands, output, root, path.resolve(cwd), session);
91
+ if (line !== null)
92
+ answers.push(line);
93
+ }
94
+ if (toEdit.length > 0) {
95
+ const { editFlow } = await import("./edit-flow.js");
96
+ await editFlow({ cwd: payload.cwd ?? null, session, channel: channel.channel }, toEdit, answers);
97
+ return;
98
+ }
99
+ const { deliverLate } = await import("./late.js");
100
+ const contexts = [...deliverLate(session), ...answers];
101
+ if (contexts.length === 0)
102
+ return;
103
+ process.stdout.write(JSON.stringify({
104
+ hookSpecificOutput: { hookEventName: "PostToolUse", additionalContext: contexts.join("\n\n") },
105
+ }));
106
+ }
107
+ await main();
108
+ process.exit(0);
109
+ //# sourceMappingURL=post-bash.js.map
@@ -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