@ryuugg/stanza 0.1.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.
- package/LICENSE.md +21 -0
- package/README.md +163 -0
- package/package.json +63 -0
- package/src/cli.ts +820 -0
- package/src/compile/darwin-arm64.ts +3 -0
- package/src/compile/darwin-x64.ts +3 -0
- package/src/compile/linux-arm64-musl.ts +3 -0
- package/src/compile/linux-arm64.ts +3 -0
- package/src/compile/linux-x64-musl.ts +3 -0
- package/src/compile/linux-x64.ts +3 -0
- package/src/compiled.ts +6 -0
- package/src/engine/attach.ts +62 -0
- package/src/engine/directives.ts +154 -0
- package/src/engine/doc.ts +93 -0
- package/src/engine/edits.ts +79 -0
- package/src/engine/explain.ts +333 -0
- package/src/engine/gaps.ts +329 -0
- package/src/engine/hunks.ts +320 -0
- package/src/engine/index.ts +254 -0
- package/src/engine/model.ts +102 -0
- package/src/engine/rules.ts +103 -0
- package/src/engine/types.ts +37 -0
- package/src/files.ts +822 -0
- package/src/gitignore.ts +279 -0
- package/src/hook.ts +282 -0
- package/src/languages/index.ts +32 -0
- package/src/languages/javascript/ast.ts +199 -0
- package/src/languages/javascript/braces.ts +374 -0
- package/src/languages/javascript/config/biome.ts +112 -0
- package/src/languages/javascript/config/eslint.ts +128 -0
- package/src/languages/javascript/config/evaluate.ts +278 -0
- package/src/languages/javascript/config/find.ts +108 -0
- package/src/languages/javascript/config/glob.ts +150 -0
- package/src/languages/javascript/config/index.ts +48 -0
- package/src/languages/javascript/config/layers.ts +425 -0
- package/src/languages/javascript/config/module.ts +326 -0
- package/src/languages/javascript/config/oxlint.ts +170 -0
- package/src/languages/javascript/config/width.ts +715 -0
- package/src/languages/javascript/entry.ts +39 -0
- package/src/languages/javascript/facts.ts +268 -0
- package/src/languages/javascript/index.ts +20 -0
- package/src/languages/javascript/lists.ts +49 -0
- package/src/languages/javascript/oracle.ts +191 -0
- package/src/languages/javascript/parse.ts +58 -0
- package/src/languages/javascript/scan.ts +68 -0
- package/src/languages/language.ts +109 -0
- package/src/launcher +21 -0
- package/src/native-file.d.ts +4 -0
- package/src/step.ts +108 -0
- package/src/usage.ts +30 -0
package/src/gitignore.ts
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
type CharacterSet = {
|
|
2
|
+
singles: string[];
|
|
3
|
+
ranges: [string, string][];
|
|
4
|
+
classes: string[];
|
|
5
|
+
negated: boolean;
|
|
6
|
+
};
|
|
7
|
+
type Token =
|
|
8
|
+
| { kind: "literal"; value: string }
|
|
9
|
+
| { kind: "star" | "globstar" | "directories" | "any" }
|
|
10
|
+
| { kind: "set"; value: CharacterSet };
|
|
11
|
+
|
|
12
|
+
export interface Rule {
|
|
13
|
+
tokens: Token[];
|
|
14
|
+
anchored: boolean;
|
|
15
|
+
directory: boolean;
|
|
16
|
+
negated: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const posixClasses = new Set([
|
|
20
|
+
"alnum",
|
|
21
|
+
"alpha",
|
|
22
|
+
"blank",
|
|
23
|
+
"cntrl",
|
|
24
|
+
"digit",
|
|
25
|
+
"graph",
|
|
26
|
+
"lower",
|
|
27
|
+
"print",
|
|
28
|
+
"punct",
|
|
29
|
+
"space",
|
|
30
|
+
"upper",
|
|
31
|
+
"xdigit",
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
function characterSet(pattern: string, start: number): { token: Token; end: number } | undefined {
|
|
35
|
+
let index = start + 1;
|
|
36
|
+
const negated = pattern[index] === "!" || pattern[index] === "^";
|
|
37
|
+
if (negated) index++;
|
|
38
|
+
|
|
39
|
+
const singles: string[] = [];
|
|
40
|
+
const ranges: [string, string][] = [];
|
|
41
|
+
const classes: string[] = [];
|
|
42
|
+
|
|
43
|
+
let first = true;
|
|
44
|
+
let previous: string | undefined;
|
|
45
|
+
while (index < pattern.length) {
|
|
46
|
+
if (pattern[index] === "]" && !first) {
|
|
47
|
+
if (previous !== undefined) singles.push(previous);
|
|
48
|
+
return {
|
|
49
|
+
token: { kind: "set", value: { singles, ranges, classes, negated } },
|
|
50
|
+
end: index + 1,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
first = false;
|
|
55
|
+
const classMatch = pattern.slice(index).match(/^\[:([a-z]+):\]/);
|
|
56
|
+
if (classMatch && posixClasses.has(classMatch[1]!)) {
|
|
57
|
+
if (previous !== undefined) singles.push(previous);
|
|
58
|
+
previous = undefined;
|
|
59
|
+
classes.push(classMatch[1]!);
|
|
60
|
+
index += classMatch[0].length;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let character = pattern[index]!;
|
|
65
|
+
const escaped = character === "\\";
|
|
66
|
+
if (escaped) {
|
|
67
|
+
index++;
|
|
68
|
+
if (index === pattern.length) return undefined;
|
|
69
|
+
character = pattern[index]!;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
!escaped &&
|
|
74
|
+
character === "-" &&
|
|
75
|
+
previous !== undefined &&
|
|
76
|
+
pattern[index + 1] !== "]" &&
|
|
77
|
+
index + 1 < pattern.length
|
|
78
|
+
) {
|
|
79
|
+
index++;
|
|
80
|
+
|
|
81
|
+
let end = pattern[index]!;
|
|
82
|
+
if (end === "\\") {
|
|
83
|
+
index++;
|
|
84
|
+
if (index === pattern.length) return undefined;
|
|
85
|
+
end = pattern[index]!;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
ranges.push([previous, end]);
|
|
89
|
+
previous = undefined;
|
|
90
|
+
} else {
|
|
91
|
+
if (previous !== undefined) singles.push(previous);
|
|
92
|
+
previous = character;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
index++;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function tokenize(pattern: string): Token[] | undefined {
|
|
102
|
+
const tokens: Token[] = [];
|
|
103
|
+
for (let index = 0; index < pattern.length;) {
|
|
104
|
+
const character = pattern[index]!;
|
|
105
|
+
if (character === "\\") {
|
|
106
|
+
if (index + 1 === pattern.length) return undefined;
|
|
107
|
+
tokens.push({ kind: "literal", value: pattern[index + 1]! });
|
|
108
|
+
index += 2;
|
|
109
|
+
} else if (character === "[") {
|
|
110
|
+
const parsed = characterSet(pattern, index);
|
|
111
|
+
if (!parsed) return undefined;
|
|
112
|
+
|
|
113
|
+
tokens.push(parsed.token);
|
|
114
|
+
index = parsed.end;
|
|
115
|
+
} else if (character === "*") {
|
|
116
|
+
const start = index;
|
|
117
|
+
while (pattern[index] === "*") index++;
|
|
118
|
+
|
|
119
|
+
const run = index - start;
|
|
120
|
+
const afterBoundary = start === 0 || pattern[start - 1] === "/";
|
|
121
|
+
if (run > 1 && afterBoundary && pattern[index] === "/") {
|
|
122
|
+
tokens.push({ kind: "directories" });
|
|
123
|
+
index++;
|
|
124
|
+
} else if (run > 1 && afterBoundary && index === pattern.length)
|
|
125
|
+
tokens.push({ kind: "globstar" });
|
|
126
|
+
else tokens.push({ kind: "star" });
|
|
127
|
+
} else {
|
|
128
|
+
tokens.push(character === "?" ? { kind: "any" } : { kind: "literal", value: character });
|
|
129
|
+
index++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return tokens;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function bytes(text: string): string {
|
|
137
|
+
return Buffer.from(text, "utf8").toString("latin1");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function parseIgnore(text: string): Rule[] {
|
|
141
|
+
const rules: Rule[] = [];
|
|
142
|
+
for (let line of bytes(text).split("\n")) {
|
|
143
|
+
if (line.endsWith("\r")) line = line.slice(0, -1);
|
|
144
|
+
if (!line || line.startsWith("#")) continue;
|
|
145
|
+
|
|
146
|
+
let trailing = line.length;
|
|
147
|
+
while (trailing > 0 && line[trailing - 1] === " ") {
|
|
148
|
+
let slashes = 0;
|
|
149
|
+
for (let index = trailing - 2; index >= 0 && line[index] === "\\"; index--) slashes++;
|
|
150
|
+
|
|
151
|
+
if (slashes % 2 === 1) break;
|
|
152
|
+
|
|
153
|
+
trailing--;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
line = line.slice(0, trailing);
|
|
157
|
+
if (!line) continue;
|
|
158
|
+
|
|
159
|
+
const negated = line.startsWith("!");
|
|
160
|
+
if (negated) line = line.slice(1);
|
|
161
|
+
|
|
162
|
+
const directory = line.endsWith("/");
|
|
163
|
+
if (directory) line = line.slice(0, -1);
|
|
164
|
+
|
|
165
|
+
const anchored = line.includes("/");
|
|
166
|
+
if (line.startsWith("/")) line = line.slice(1);
|
|
167
|
+
|
|
168
|
+
const tokens = tokenize(line);
|
|
169
|
+
if (tokens) rules.push({ tokens, anchored, directory, negated });
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return rules;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function inClass(name: string, character: string): boolean {
|
|
176
|
+
const code = character.charCodeAt(0);
|
|
177
|
+
const upper = code >= 65 && code <= 90;
|
|
178
|
+
const lower = code >= 97 && code <= 122;
|
|
179
|
+
const digit = code >= 48 && code <= 57;
|
|
180
|
+
switch (name) {
|
|
181
|
+
case "alnum":
|
|
182
|
+
return upper || lower || digit;
|
|
183
|
+
|
|
184
|
+
case "alpha":
|
|
185
|
+
return upper || lower;
|
|
186
|
+
|
|
187
|
+
case "blank":
|
|
188
|
+
return character === " " || character === "\t";
|
|
189
|
+
|
|
190
|
+
case "cntrl":
|
|
191
|
+
return code < 32 || code === 127;
|
|
192
|
+
|
|
193
|
+
case "digit":
|
|
194
|
+
return digit;
|
|
195
|
+
|
|
196
|
+
case "graph":
|
|
197
|
+
return code >= 33 && code <= 126;
|
|
198
|
+
|
|
199
|
+
case "lower":
|
|
200
|
+
return lower;
|
|
201
|
+
|
|
202
|
+
case "print":
|
|
203
|
+
return code >= 32 && code <= 126;
|
|
204
|
+
|
|
205
|
+
case "punct":
|
|
206
|
+
return code >= 33 && code <= 126 && !upper && !lower && !digit;
|
|
207
|
+
|
|
208
|
+
case "space":
|
|
209
|
+
return character === " " || (code >= 9 && code <= 13);
|
|
210
|
+
|
|
211
|
+
case "upper":
|
|
212
|
+
return upper;
|
|
213
|
+
|
|
214
|
+
case "xdigit":
|
|
215
|
+
return digit || (code >= 65 && code <= 70) || (code >= 97 && code <= 102);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function matches(tokens: Token[], path: string): boolean {
|
|
222
|
+
const length = path.length;
|
|
223
|
+
|
|
224
|
+
let next = Array.from({ length: length + 1 }, (_, index) => index === length);
|
|
225
|
+
for (let tokenIndex = tokens.length - 1; tokenIndex >= 0; tokenIndex--) {
|
|
226
|
+
const token = tokens[tokenIndex]!;
|
|
227
|
+
const current = Array<boolean>(length + 1).fill(false);
|
|
228
|
+
const throughSlash = Array<boolean>(length + 1).fill(false);
|
|
229
|
+
for (let index = length; index >= 0; index--) {
|
|
230
|
+
const character = path[index];
|
|
231
|
+
if (token.kind === "literal") current[index] = character === token.value && next[index + 1]!;
|
|
232
|
+
else if (token.kind === "any")
|
|
233
|
+
current[index] = character !== undefined && character !== "/" && next[index + 1]!;
|
|
234
|
+
else if (token.kind === "set") {
|
|
235
|
+
if (character !== undefined && character !== "/") {
|
|
236
|
+
const set = token.value;
|
|
237
|
+
const included =
|
|
238
|
+
set.singles.includes(character) ||
|
|
239
|
+
set.ranges.some(([start, end]) => start <= character && character <= end) ||
|
|
240
|
+
set.classes.some((name) => inClass(name, character));
|
|
241
|
+
|
|
242
|
+
current[index] = (set.negated ? !included : included) && next[index + 1]!;
|
|
243
|
+
}
|
|
244
|
+
} else if (token.kind === "directories") {
|
|
245
|
+
throughSlash[index] =
|
|
246
|
+
character !== undefined &&
|
|
247
|
+
((character === "/" && next[index + 1]!) || throughSlash[index + 1]!);
|
|
248
|
+
|
|
249
|
+
current[index] = next[index]! || throughSlash[index]!;
|
|
250
|
+
} else
|
|
251
|
+
current[index] =
|
|
252
|
+
next[index]! ||
|
|
253
|
+
(character !== undefined &&
|
|
254
|
+
(token.kind === "globstar" || character !== "/") &&
|
|
255
|
+
current[index + 1]!);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
next = current;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
return next[0]!;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function ignoredByRules(
|
|
265
|
+
rules: Rule[],
|
|
266
|
+
path: string,
|
|
267
|
+
isDirectory: boolean,
|
|
268
|
+
): boolean | undefined {
|
|
269
|
+
const target = bytes(path);
|
|
270
|
+
|
|
271
|
+
let ignored: boolean | undefined;
|
|
272
|
+
for (const rule of rules) {
|
|
273
|
+
if (rule.directory && !isDirectory) continue;
|
|
274
|
+
if (matches(rule.tokens, rule.anchored ? target : target.slice(target.lastIndexOf("/") + 1)))
|
|
275
|
+
ignored = !rule.negated;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
return ignored;
|
|
279
|
+
}
|
package/src/hook.ts
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { readFileSync, readdirSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { RULES } from "./engine/rules.ts";
|
|
4
|
+
import type { Finding } from "./engine/types.ts";
|
|
5
|
+
|
|
6
|
+
export const claudeCodeHooks =
|
|
7
|
+
'{ "hooks": { "PreToolUse": [{ "matcher": "Write", "hooks": [{ "type": "command", "command": "stanza hook" }] }], "PostToolUse": [{ "matcher": "Edit|MultiEdit", "hooks": [{ "type": "command", "command": "stanza hook" }] }], "Stop": [{ "hooks": [{ "type": "command", "command": "stanza hook" }] }] } }';
|
|
8
|
+
|
|
9
|
+
export function hookInput(
|
|
10
|
+
text: string,
|
|
11
|
+
fallbackCwd: string,
|
|
12
|
+
):
|
|
13
|
+
| { event: "stop"; cwd: string; stopHookActive: boolean; transcriptPath: string | undefined }
|
|
14
|
+
| {
|
|
15
|
+
event: "write";
|
|
16
|
+
cwd: string;
|
|
17
|
+
toolInput: { file_path: string; content: string } & Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
| { event: "edit"; cwd: string; paths: string[] }
|
|
20
|
+
| { event: "ignored" }
|
|
21
|
+
| { error: string } {
|
|
22
|
+
let input: unknown;
|
|
23
|
+
try {
|
|
24
|
+
input = JSON.parse(text);
|
|
25
|
+
} catch {
|
|
26
|
+
return { error: "input must be valid JSON" };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (!isRecord(input)) return { error: "input must be a JSON object" };
|
|
30
|
+
|
|
31
|
+
const event = "hook_event_name" in input ? input.hook_event_name : undefined;
|
|
32
|
+
if (event != null && typeof event !== "string")
|
|
33
|
+
return { error: "hook_event_name must be a string" };
|
|
34
|
+
|
|
35
|
+
if (event != null && !events.has(event)) return { event: "ignored" };
|
|
36
|
+
|
|
37
|
+
const cwd = "cwd" in input ? input.cwd : undefined;
|
|
38
|
+
if (cwd != null && typeof cwd !== "string") return { error: "cwd must be a string" };
|
|
39
|
+
|
|
40
|
+
const dir = cwd == null ? fallbackCwd : resolve(fallbackCwd, cwd);
|
|
41
|
+
if (event === "PostToolUse") {
|
|
42
|
+
const paths = editedPaths(input);
|
|
43
|
+
if (paths.length === 0) return { event: "ignored" };
|
|
44
|
+
return { event: "edit", cwd: dir, paths: paths.map((path) => resolve(dir, path)) };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (event === "PreToolUse") {
|
|
48
|
+
const toolInput = "tool_input" in input ? input.tool_input : undefined;
|
|
49
|
+
if (
|
|
50
|
+
!("tool_name" in input && input.tool_name === "Write") ||
|
|
51
|
+
!isRecord(toolInput) ||
|
|
52
|
+
typeof toolInput.file_path !== "string" ||
|
|
53
|
+
typeof toolInput.content !== "string"
|
|
54
|
+
)
|
|
55
|
+
return { event: "ignored" };
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
event: "write",
|
|
59
|
+
cwd: dir,
|
|
60
|
+
toolInput: { ...toolInput, file_path: toolInput.file_path, content: toolInput.content },
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const transcriptPath = "transcript_path" in input ? input.transcript_path : undefined;
|
|
65
|
+
if (transcriptPath != null && typeof transcriptPath !== "string")
|
|
66
|
+
return { error: "transcript_path must be a string" };
|
|
67
|
+
|
|
68
|
+
const stopHookActive = "stop_hook_active" in input ? input.stop_hook_active : undefined;
|
|
69
|
+
if (stopHookActive != null && typeof stopHookActive !== "boolean")
|
|
70
|
+
return { error: "stop_hook_active must be a boolean" };
|
|
71
|
+
|
|
72
|
+
return {
|
|
73
|
+
event: "stop",
|
|
74
|
+
cwd: dir,
|
|
75
|
+
stopHookActive: stopHookActive ?? false,
|
|
76
|
+
transcriptPath: transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const events = new Set(["Stop", "SubagentStop", "PreToolUse", "PostToolUse"]);
|
|
81
|
+
const patchHeader = /^\*\*\* (Add File|Update File|Move to): (.+)$/;
|
|
82
|
+
const patchApplied = /^Success\. Updated the following files:$/m;
|
|
83
|
+
|
|
84
|
+
function editedPaths(input: Record<string, unknown>): string[] {
|
|
85
|
+
const toolInput = input.tool_input;
|
|
86
|
+
if (!isRecord(toolInput)) return [];
|
|
87
|
+
if (input.tool_name === "Edit" || input.tool_name === "MultiEdit")
|
|
88
|
+
return typeof toolInput.file_path === "string" ? [toolInput.file_path] : [];
|
|
89
|
+
|
|
90
|
+
if (input.tool_name !== "apply_patch" || typeof toolInput.command !== "string") return [];
|
|
91
|
+
if (typeof input.tool_response !== "string" || !patchApplied.test(input.tool_response)) return [];
|
|
92
|
+
|
|
93
|
+
const paths: string[] = [];
|
|
94
|
+
for (const line of toolInput.command.split(/\r?\n/)) {
|
|
95
|
+
const [, kind, path] = patchHeader.exec(line) ?? [];
|
|
96
|
+
if (path === undefined) continue;
|
|
97
|
+
if (kind === "Move to") paths.pop();
|
|
98
|
+
paths.push(path);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return paths;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
105
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const toolUseLine = /"type"\s*:\s*"tool_use"/;
|
|
109
|
+
const failedResultLine = /"is_error"\s*:\s*true/;
|
|
110
|
+
const itemCompletedLine = /"item_completed"/;
|
|
111
|
+
|
|
112
|
+
export function writtenFiles(transcriptPath: string): Set<string> | undefined {
|
|
113
|
+
let text: string;
|
|
114
|
+
try {
|
|
115
|
+
text = readFileSync(transcriptPath, "utf8");
|
|
116
|
+
} catch {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const codex = writtenFilesInCodexRollout(text);
|
|
121
|
+
if (codex !== undefined) return codex.size > 0 ? codex : undefined;
|
|
122
|
+
|
|
123
|
+
const main = writtenFilesInTranscript(text);
|
|
124
|
+
if (main === undefined) return undefined;
|
|
125
|
+
|
|
126
|
+
const written = new Set(main);
|
|
127
|
+
const subagents = join(dirname(transcriptPath), basename(transcriptPath, ".jsonl"), "subagents");
|
|
128
|
+
|
|
129
|
+
let names: string[];
|
|
130
|
+
try {
|
|
131
|
+
names = readdirSync(subagents);
|
|
132
|
+
} catch {
|
|
133
|
+
return written;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
for (const name of names) {
|
|
137
|
+
if (!name.endsWith(".jsonl")) continue;
|
|
138
|
+
|
|
139
|
+
let text: string;
|
|
140
|
+
try {
|
|
141
|
+
text = readFileSync(join(subagents, name), "utf8");
|
|
142
|
+
} catch {
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const subagent = writtenFilesInTranscript(text);
|
|
147
|
+
if (subagent === undefined) continue;
|
|
148
|
+
|
|
149
|
+
for (const path of subagent) written.add(path);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return written;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function writtenFilesInCodexRollout(text: string): Set<string> | undefined {
|
|
156
|
+
const written = new Set<string>();
|
|
157
|
+
|
|
158
|
+
let recognized = false;
|
|
159
|
+
for (const line of text.split("\n")) {
|
|
160
|
+
if (!itemCompletedLine.test(line)) continue;
|
|
161
|
+
|
|
162
|
+
let record: unknown;
|
|
163
|
+
try {
|
|
164
|
+
record = JSON.parse(line);
|
|
165
|
+
} catch {
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!isRecord(record) || record.type !== "event_msg" || !isRecord(record.payload)) continue;
|
|
170
|
+
if (record.payload.type !== "item_completed") continue;
|
|
171
|
+
|
|
172
|
+
recognized = true;
|
|
173
|
+
|
|
174
|
+
const item = record.payload.item;
|
|
175
|
+
if (!isRecord(item) || item.type !== "FileChange" || item.status !== "completed") continue;
|
|
176
|
+
if (!isRecord(item.changes)) continue;
|
|
177
|
+
|
|
178
|
+
for (const [source, change] of Object.entries(item.changes)) {
|
|
179
|
+
if (!isRecord(change) || change.type === "delete") continue;
|
|
180
|
+
|
|
181
|
+
const path = typeof change.move_path === "string" ? change.move_path : source;
|
|
182
|
+
if (!isAbsolute(path)) continue;
|
|
183
|
+
|
|
184
|
+
written.add(path);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return recognized ? written : undefined;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function writtenFilesInTranscript(text: string): Set<string> | undefined {
|
|
192
|
+
const attempted = new Map<unknown, string>();
|
|
193
|
+
const failed = new Set<unknown>();
|
|
194
|
+
|
|
195
|
+
let recognized = false;
|
|
196
|
+
for (const line of text.split("\n")) {
|
|
197
|
+
if (recognized && !toolUseLine.test(line) && !failedResultLine.test(line)) continue;
|
|
198
|
+
|
|
199
|
+
let record: unknown;
|
|
200
|
+
try {
|
|
201
|
+
record = JSON.parse(line);
|
|
202
|
+
} catch {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (!isRecord(record) || !isRecord(record.message)) continue;
|
|
207
|
+
|
|
208
|
+
const content = record.message.content;
|
|
209
|
+
if (!Array.isArray(content)) continue;
|
|
210
|
+
if (record.type === "assistant") recognized = true;
|
|
211
|
+
|
|
212
|
+
for (const item of content) {
|
|
213
|
+
if (!isRecord(item)) continue;
|
|
214
|
+
|
|
215
|
+
const path = editedPath(item);
|
|
216
|
+
if (record.type === "assistant" && path !== undefined) attempted.set(item.id, path);
|
|
217
|
+
if (record.type === "user" && item.type === "tool_result" && item.is_error === true)
|
|
218
|
+
failed.add(item.tool_use_id);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (!recognized) return undefined;
|
|
223
|
+
|
|
224
|
+
const written = new Set<string>();
|
|
225
|
+
for (const [id, path] of attempted) if (!failed.has(id)) written.add(path);
|
|
226
|
+
|
|
227
|
+
return written;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const editTools = new Set(["Write", "Edit", "MultiEdit"]);
|
|
231
|
+
|
|
232
|
+
function editedPath(item: Record<string, unknown>): string | undefined {
|
|
233
|
+
if (item.type !== "tool_use" || !editTools.has(String(item.name))) return undefined;
|
|
234
|
+
if (!isRecord(item.input) || typeof item.input.file_path !== "string") return undefined;
|
|
235
|
+
return isAbsolute(item.input.file_path) ? item.input.file_path : undefined;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const failures: Partial<Record<Finding["rule"], string>> = {
|
|
239
|
+
parse: "could not read or parse this file",
|
|
240
|
+
error: "stanza failed on this file",
|
|
241
|
+
write: "could not write the fixes to this file",
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
export function blockReason(findings: Finding[], rewritten: string[]): string | undefined {
|
|
245
|
+
if (findings.length === 0) return undefined;
|
|
246
|
+
|
|
247
|
+
const shown = findings.slice(0, 12);
|
|
248
|
+
const lines = shown.map((finding) => {
|
|
249
|
+
const failure = failures[finding.rule];
|
|
250
|
+
const message = failure
|
|
251
|
+
? `${failure}: ${finding.message}`
|
|
252
|
+
: `${finding.rule} ${finding.message}`;
|
|
253
|
+
|
|
254
|
+
return ` ${finding.path}:${finding.line}:${finding.col} ${message}`;
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
if (findings.length > shown.length)
|
|
258
|
+
lines.push(` ... and ${findings.length - shown.length} more`);
|
|
259
|
+
|
|
260
|
+
const present = new Set<string>(findings.map((finding) => finding.rule));
|
|
261
|
+
const rules = Object.entries(RULES)
|
|
262
|
+
.filter(([id]) => present.has(id))
|
|
263
|
+
.map(([id, rule]) => `${id}: ${rule.summary}`);
|
|
264
|
+
|
|
265
|
+
const sections = [["stanza could not fix these in the files you changed:", ...lines].join("\n")];
|
|
266
|
+
if (rules.length > 0) sections.push(rules.join("\n"));
|
|
267
|
+
|
|
268
|
+
sections.push("Fix those findings, then reply again.");
|
|
269
|
+
|
|
270
|
+
const paths = new Set(findings.map((finding) => finding.path));
|
|
271
|
+
const reread = rewritten.filter((path) => paths.has(path));
|
|
272
|
+
if (reread.length > 0) sections.push(rereadLine("rewrote", reread));
|
|
273
|
+
|
|
274
|
+
return sections.join("\n\n");
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export function rereadLine(verb: string, paths: string[]): string {
|
|
278
|
+
const names =
|
|
279
|
+
paths.length === 1 ? paths[0] : `${paths.slice(0, -1).join(", ")} and ${paths.at(-1)}`;
|
|
280
|
+
|
|
281
|
+
return `stanza ${verb} ${names}, so read ${paths.length === 1 ? "it" : "them"} again before editing.`;
|
|
282
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { basename, extname } from "node:path";
|
|
2
|
+
import { entry as javascript } from "./javascript/entry.ts";
|
|
3
|
+
import type { Entry, Language } from "./language.ts";
|
|
4
|
+
|
|
5
|
+
const entries: Entry[] = [javascript];
|
|
6
|
+
const byExtension = new Map(
|
|
7
|
+
entries.flatMap((entry) => entry.extensions.map((extension) => [extension, entry] as const)),
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
export function entryFor(path: string): Entry | undefined {
|
|
11
|
+
return byExtension.get(extname(path));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function languageOf(path: string): Language {
|
|
15
|
+
const entry = entryFor(path);
|
|
16
|
+
if (!entry) throw new Error(`no language is registered for ${path}`);
|
|
17
|
+
return entry.load();
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isCandidate(relativePath: string, keepGenerated = false): boolean {
|
|
21
|
+
const entry = entryFor(relativePath);
|
|
22
|
+
return (
|
|
23
|
+
entry !== undefined &&
|
|
24
|
+
!entry.skipsName(basename(relativePath), keepGenerated) &&
|
|
25
|
+
!relativePath.split(/[\\/]+/).some((segment) => entry.directories.has(segment))
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function prunes(relativeDirectory: string): boolean {
|
|
30
|
+
const segments = relativeDirectory.split(/[\\/]+/);
|
|
31
|
+
return entries.every((entry) => segments.some((segment) => entry.directories.has(segment)));
|
|
32
|
+
}
|