co-maintainer 0.7.9 → 0.7.10-beta.2

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.
@@ -7,6 +7,87 @@ import { askConfirm } from "../cli/prompt.js";
7
7
  * someone else's machine. Raise it, adjust whatever reads the output, done. */
8
8
  export const CODEGRAPH_VERSION = "1.6.0";
9
9
  export const CODEGRAPH_PACKAGE = "@colbymchenry/codegraph";
10
+ /** The file extensions codegraph {@link CODEGRAPH_VERSION} extracts symbols
11
+ * from, lower case. `search-non-code` searches every other tracked text file,
12
+ * so update this list whenever the version is raised. Taken from
13
+ * `EXTENSION_MAP` in the installed package's `extraction/grammars.js`, without
14
+ * the languages it tracks only as files and extracts nothing from (`.yml`,
15
+ * `.yaml`, `.twig`, `.properties`, and `.xml` outside MyBatis mappers). */
16
+ export const CODEGRAPH_EXTENSIONS = [
17
+ ".ts",
18
+ ".tsx",
19
+ ".mts",
20
+ ".cts",
21
+ ".ets",
22
+ ".js",
23
+ ".mjs",
24
+ ".cjs",
25
+ ".xsjs",
26
+ ".xsjslib",
27
+ ".jsx",
28
+ ".py",
29
+ ".pyw",
30
+ ".go",
31
+ ".rs",
32
+ ".java",
33
+ ".c",
34
+ ".h",
35
+ ".cpp",
36
+ ".cc",
37
+ ".cxx",
38
+ ".hpp",
39
+ ".hxx",
40
+ ".cs",
41
+ ".cshtml",
42
+ ".razor",
43
+ ".php",
44
+ ".module",
45
+ ".install",
46
+ ".theme",
47
+ ".inc",
48
+ ".rb",
49
+ ".rake",
50
+ ".swift",
51
+ ".kt",
52
+ ".kts",
53
+ ".dart",
54
+ ".liquid",
55
+ ".svelte",
56
+ ".vue",
57
+ ".astro",
58
+ ".r",
59
+ ".pas",
60
+ ".dpr",
61
+ ".dpk",
62
+ ".lpr",
63
+ ".dfm",
64
+ ".fmx",
65
+ ".scala",
66
+ ".sc",
67
+ ".lua",
68
+ ".luau",
69
+ ".m",
70
+ ".mm",
71
+ ".sol",
72
+ ".cfc",
73
+ ".cfm",
74
+ ".cfs",
75
+ ".metal",
76
+ ".cu",
77
+ ".cuh",
78
+ ".nix",
79
+ ".cbl",
80
+ ".cob",
81
+ ".cobol",
82
+ ".cpy",
83
+ ".vb",
84
+ ".erl",
85
+ ".hrl",
86
+ ".escript",
87
+ ".tf",
88
+ ".tfvars",
89
+ ".tofu",
90
+ ];
10
91
  /** A version that is safe in a directory name and an npm argument: a bare
11
92
  * semver, optionally with a prerelease suffix. Anything else (a path, a
12
93
  * leading `-`, whitespace) is refused, so a bad config value can never reach
@@ -0,0 +1,58 @@
1
+ /** `search-non-code`: a review tool for the files codegraph does not index
2
+ * (issue 79). Codegraph follows code, and a review missed a paragraph a change
3
+ * made false or a compose entry outside the changed hunk because nothing could
4
+ * read docs or configuration.
5
+ *
6
+ * It is not a general grep. It searches tracked files only, through
7
+ * `git grep`, so an untracked `.env` is never read. Binary files are skipped,
8
+ * and so is every file codegraph parses (`CODEGRAPH_EXTENSIONS`) and every
9
+ * file the repository's `agentInstructions` pattern matches, because
10
+ * `INSTRUCTIONS.md` already carries their rules. */
11
+ import type { ToolHandler } from "../ai/mermaid_loop.ts";
12
+ import type { Json } from "../types.ts";
13
+ export declare const SEARCH_NON_CODE = "search-non-code";
14
+ /** Most matches one answer lists. */
15
+ export declare const SEARCH_MAX_MATCHES = 30;
16
+ /** Most characters of one listed line. */
17
+ export declare const SEARCH_LINE_MAX_CHARS = 200;
18
+ /** Time limit of a regex search. */
19
+ export declare const SEARCH_REGEX_TIMEOUT_MS = 5000;
20
+ /** Default for `reviewSearchMaxContextLines`. */
21
+ export declare const DEFAULT_SEARCH_MAX_CONTEXT_LINES = 3;
22
+ /** The repository's most context lines around a match, or `null` for no
23
+ * limit. Unset takes the default, 0 or a negative number means no limit. */
24
+ export declare function searchMaxContextLines(repoConfig: {
25
+ reviewSearchMaxContextLines?: number;
26
+ } | undefined): number | null;
27
+ export declare const SEARCH_NON_CODE_TOOL: Json;
28
+ export type GrepResult = {
29
+ code: number;
30
+ stdout: string;
31
+ stderr: string;
32
+ timedOut: boolean;
33
+ };
34
+ /** Runs `git` with `args` in `cwd`, killed after `timeoutMs` when set. */
35
+ export type GrepRun = (args: string[], cwd: string, timeoutMs: number | null) => Promise<GrepResult>;
36
+ export declare const runGit: GrepRun;
37
+ export type SearchOptions = {
38
+ /** Most context lines, `null` for no limit. */
39
+ maxContextLines: number | null;
40
+ /** Files this pattern matches are skipped. */
41
+ instructions: RegExp | null;
42
+ run?: GrepRun;
43
+ timeoutMs?: number;
44
+ };
45
+ /** `git grep` arguments for one search. Every file codegraph parses is left
46
+ * out by a pathspec, so git never reads it. */
47
+ export declare function grepArgs(input: {
48
+ pattern: string;
49
+ regex: boolean;
50
+ path?: string;
51
+ context: number;
52
+ }): string[];
53
+ /** One search, as the model reads it. */
54
+ export declare function searchNonCode(root: string, args: unknown, options: SearchOptions): Promise<string>;
55
+ /** The search settings of one repository, from config.json. */
56
+ export declare function searchOptionsFor(repo: string): SearchOptions;
57
+ /** The tool, searching the tracked files of the tree at `root`. */
58
+ export declare function searchNonCodeTool(root: string, options: SearchOptions): ToolHandler;
@@ -0,0 +1,255 @@
1
+ import { readConfig } from "../config.js";
2
+ import { agentInstructionsPattern } from "../knowledge/instruction_files.js";
3
+ import { commandSpawn } from "../util/runtime.js";
4
+ import { CODEGRAPH_EXTENSIONS } from "./codegraph.js";
5
+ import { guardToolArgs, rejectEscapingPath, toolError, } from "../pr/codegraph_tool_args.js";
6
+ export const SEARCH_NON_CODE = "search-non-code";
7
+ /** Most matches one answer lists. */
8
+ export const SEARCH_MAX_MATCHES = 30;
9
+ /** Most characters of one listed line. */
10
+ export const SEARCH_LINE_MAX_CHARS = 200;
11
+ /** Time limit of a regex search. */
12
+ export const SEARCH_REGEX_TIMEOUT_MS = 5000;
13
+ /** Default for `reviewSearchMaxContextLines`. */
14
+ export const DEFAULT_SEARCH_MAX_CONTEXT_LINES = 3;
15
+ /** Most files the `and N more matches` line names. */
16
+ const MAX_MORE_FILES = 30;
17
+ /** The repository's most context lines around a match, or `null` for no
18
+ * limit. Unset takes the default, 0 or a negative number means no limit. */
19
+ export function searchMaxContextLines(repoConfig) {
20
+ const raw = repoConfig?.reviewSearchMaxContextLines;
21
+ if (raw === undefined)
22
+ return DEFAULT_SEARCH_MAX_CONTEXT_LINES;
23
+ if (!Number.isFinite(raw) || raw <= 0)
24
+ return null;
25
+ return Math.floor(raw);
26
+ }
27
+ export const SEARCH_NON_CODE_TOOL = {
28
+ type: "function",
29
+ function: {
30
+ name: SEARCH_NON_CODE,
31
+ description: "Search the repository's tracked files that codegraph does not index: docs, configuration, YAML, JSON, TOML, ini and env templates, Dockerfiles, SQL, shell scripts, HTML and CSS. Code files are not searched, use codegraph for code. The pattern is literal text unless regex is true. Answers at most 30 matches with their line numbers.",
32
+ parameters: {
33
+ type: "object",
34
+ properties: {
35
+ pattern: {
36
+ type: "string",
37
+ description: "The text to find. Literal unless regex is true.",
38
+ },
39
+ regex: {
40
+ type: "boolean",
41
+ description: "Treat pattern as a POSIX extended regular expression.",
42
+ },
43
+ path: {
44
+ type: "string",
45
+ description: "Limit the search to this folder or file.",
46
+ },
47
+ context: {
48
+ type: "number",
49
+ description: "Lines to show around each match.",
50
+ },
51
+ },
52
+ required: ["pattern"],
53
+ additionalProperties: false,
54
+ },
55
+ },
56
+ };
57
+ export const runGit = async (args, cwd, timeoutMs) => {
58
+ // Spawned directly, never through a shell: the pattern is the model's text.
59
+ const proc = commandSpawn("git", {
60
+ args,
61
+ cwd,
62
+ stdout: "piped",
63
+ stderr: "piped",
64
+ });
65
+ let timedOut = false;
66
+ const timer = timeoutMs === null
67
+ ? undefined
68
+ : setTimeout(() => {
69
+ timedOut = true;
70
+ proc.kill("SIGKILL");
71
+ }, timeoutMs);
72
+ const output = await proc.output();
73
+ if (timer)
74
+ clearTimeout(timer);
75
+ const decoder = new TextDecoder();
76
+ return {
77
+ code: output.code,
78
+ stdout: decoder.decode(output.stdout),
79
+ stderr: decoder.decode(output.stderr),
80
+ timedOut,
81
+ };
82
+ };
83
+ /** `path\0line\0text` records, the `-z` output of `git grep -n`. */
84
+ function parseRecords(stdout) {
85
+ const rows = [];
86
+ for (const record of stdout.split("\n")) {
87
+ const first = record.indexOf("\0");
88
+ if (first < 0)
89
+ continue;
90
+ const second = record.indexOf("\0", first + 1);
91
+ if (second < 0)
92
+ continue;
93
+ const line = Number(record.slice(first + 1, second));
94
+ if (!Number.isInteger(line))
95
+ continue;
96
+ rows.push({
97
+ path: record.slice(0, first),
98
+ line,
99
+ text: record.slice(second + 1).replace(/\r$/, ""),
100
+ });
101
+ }
102
+ return rows;
103
+ }
104
+ function cutLine(text) {
105
+ return text.length > SEARCH_LINE_MAX_CHARS
106
+ ? `${text.slice(0, SEARCH_LINE_MAX_CHARS)}...`
107
+ : text;
108
+ }
109
+ function plural(count, one, many) {
110
+ return `${count} ${count === 1 ? one : many}`;
111
+ }
112
+ /** `git grep` arguments for one search. Every file codegraph parses is left
113
+ * out by a pathspec, so git never reads it. */
114
+ export function grepArgs(input) {
115
+ return [
116
+ "grep",
117
+ "-n",
118
+ "-I",
119
+ "-z",
120
+ "--no-color",
121
+ "--no-column",
122
+ input.regex ? "-E" : "--fixed-strings",
123
+ ...(input.context > 0 ? [`-C${input.context}`] : []),
124
+ "-e",
125
+ input.pattern,
126
+ "--",
127
+ input.path ? `:(literal)${input.path}` : ".",
128
+ ...CODEGRAPH_EXTENSIONS.map((ext) => `:(exclude,icase,glob)**/*${ext}`),
129
+ ];
130
+ }
131
+ /** One search, as the model reads it. */
132
+ export async function searchNonCode(root, args, options) {
133
+ const a = (args ?? {});
134
+ const guard = guardToolArgs(a, new Set(["pattern", "regex", "path", "context"]));
135
+ if (guard)
136
+ return guard;
137
+ const pattern = typeof a.pattern === "string" ? a.pattern : "";
138
+ if (pattern === "")
139
+ return toolError(`${SEARCH_NON_CODE} requires 'pattern'`);
140
+ if (/[\r\n]/.test(pattern)) {
141
+ return toolError("pattern must be a single line");
142
+ }
143
+ const regex = a.regex === true;
144
+ const path = typeof a.path === "string" && a.path.trim() !== "" ? a.path.trim() : "";
145
+ if (path) {
146
+ const escaping = rejectEscapingPath(path);
147
+ if (escaping)
148
+ return escaping;
149
+ }
150
+ const asked = typeof a.context === "number" && Number.isFinite(a.context)
151
+ ? Math.max(0, Math.floor(a.context))
152
+ : 0;
153
+ const context = options.maxContextLines === null
154
+ ? asked
155
+ : Math.min(asked, options.maxContextLines);
156
+ const run = options.run ?? runGit;
157
+ const timeoutMs = regex
158
+ ? (options.timeoutMs ?? SEARCH_REGEX_TIMEOUT_MS)
159
+ : null;
160
+ // The match list comes without context, so a context line is never
161
+ // mistaken for a match. `-z` marks both kinds the same way.
162
+ const [matched, around] = await Promise.all([
163
+ run(grepArgs({ pattern, regex, path, context: 0 }), root, timeoutMs),
164
+ context > 0
165
+ ? run(grepArgs({ pattern, regex, path, context }), root, timeoutMs)
166
+ : Promise.resolve(undefined),
167
+ ]);
168
+ if (matched.timedOut || around?.timedOut) {
169
+ return `Search timed out after ${(timeoutMs ?? 0) / 1000} seconds. Use a simpler pattern or a narrower path.`;
170
+ }
171
+ if (matched.code === 1)
172
+ return noMatches(path);
173
+ if (matched.code !== 0) {
174
+ const why = matched.stderr.trim().split("\n")[0] ?? "";
175
+ return toolError(`search failed${why ? `: ${why}` : ""}`);
176
+ }
177
+ const skip = (row) => options.instructions?.test(row.path) === true;
178
+ const matches = parseRecords(matched.stdout).filter((row) => !skip(row));
179
+ if (matches.length === 0)
180
+ return noMatches(path);
181
+ const lines = new Map();
182
+ for (const row of around ? parseRecords(around.stdout) : []) {
183
+ if (skip(row))
184
+ continue;
185
+ let file = lines.get(row.path);
186
+ if (!file)
187
+ lines.set(row.path, (file = new Map()));
188
+ file.set(row.line, row.text);
189
+ }
190
+ const shown = matches.slice(0, SEARCH_MAX_MATCHES);
191
+ const isMatch = new Set(shown.map((row) => `${row.path}\0${row.line}`));
192
+ const out = [];
193
+ let last;
194
+ for (const row of shown) {
195
+ const from = Math.max(1, row.line - context);
196
+ for (let line = from; line <= row.line + context; line++) {
197
+ if (last && last.path === row.path && line <= last.line)
198
+ continue;
199
+ const text = line === row.line ? row.text : lines.get(row.path)?.get(line);
200
+ if (text === undefined)
201
+ continue;
202
+ if (context > 0 &&
203
+ last &&
204
+ (last.path !== row.path || line > last.line + 1)) {
205
+ out.push("--");
206
+ }
207
+ const match = isMatch.has(`${row.path}\0${line}`);
208
+ out.push(`${row.path}${match ? ":" : "-"}${line}${match ? ":" : "-"} ${cutLine(text)}`);
209
+ last = { path: row.path, line };
210
+ }
211
+ }
212
+ const files = new Set(matches.map((row) => row.path));
213
+ const rest = matches.slice(SEARCH_MAX_MATCHES);
214
+ if (rest.length > 0) {
215
+ const perFile = new Map();
216
+ for (const row of rest) {
217
+ perFile.set(row.path, (perFile.get(row.path) ?? 0) + 1);
218
+ }
219
+ const named = [...perFile]
220
+ .slice(0, MAX_MORE_FILES)
221
+ .map(([file, count]) => `${file} (${count})`);
222
+ const unnamed = perFile.size - named.length;
223
+ out.push(`and ${plural(rest.length, "more match", "more matches")}: ${named.join(", ")}${unnamed > 0 ? `, and ${plural(unnamed, "more file", "more files")}` : ""}`);
224
+ }
225
+ out.push(`${plural(matches.length, "match", "matches")} in ${plural(files.size, "file", "files")}`);
226
+ return out.join("\n");
227
+ }
228
+ function noMatches(path) {
229
+ return `No matches in the tracked files codegraph does not index${path ? ` under ${path}` : ""}.`;
230
+ }
231
+ /** The search settings of one repository, from config.json. */
232
+ export function searchOptionsFor(repo) {
233
+ const repoConfig = readConfig().repos?.[repo];
234
+ let instructions = null;
235
+ try {
236
+ instructions = agentInstructionsPattern(repoConfig?.agentInstructions);
237
+ }
238
+ catch {
239
+ // A saved pattern that no longer compiles skips no file here. `sync`
240
+ // names the problem.
241
+ instructions = null;
242
+ }
243
+ return {
244
+ maxContextLines: searchMaxContextLines(repoConfig),
245
+ instructions,
246
+ };
247
+ }
248
+ /** The tool, searching the tracked files of the tree at `root`. */
249
+ export function searchNonCodeTool(root, options) {
250
+ return {
251
+ name: SEARCH_NON_CODE,
252
+ tool: SEARCH_NON_CODE_TOOL,
253
+ run: (args) => searchNonCode(root, args, options),
254
+ };
255
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "co-maintainer",
3
- "version": "0.7.9",
3
+ "version": "0.7.10-beta.2",
4
4
  "description": "An imitator of yours, A PR review tool like co-maintainer.",
5
5
  "keywords": [
6
6
  "ai",