@ryuugg/stanza 0.3.1 → 0.3.3

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/src/hook.ts CHANGED
@@ -52,6 +52,22 @@ export function hookCall(args: string[], io: Io): HookCall | number {
52
52
  if (input.event === "edit") return input.paths.some(isCandidateFile) ? input : 0;
53
53
  if (input.stopHookActive) return 0;
54
54
 
55
+ if ("agentTranscriptPath" in input) {
56
+ if (input.agentTranscriptPath === undefined) return 0;
57
+
58
+ let text: string;
59
+ try {
60
+ text = readFileSync(input.agentTranscriptPath, "utf8");
61
+ } catch {
62
+ return 0;
63
+ }
64
+
65
+ const written = writtenFilesInCodexRollout(text) ?? writtenFilesInTranscript(text);
66
+ if (written === undefined || ![...written].some(isCandidateFile)) return 0;
67
+
68
+ return { event: "stop", cwd: input.cwd, written };
69
+ }
70
+
55
71
  const written =
56
72
  input.transcriptPath === undefined ? undefined : writtenFiles(input.transcriptPath);
57
73
 
@@ -67,7 +83,11 @@ export function hookInput(
67
83
  text: string,
68
84
  fallbackCwd: string,
69
85
  ):
70
- | { event: "stop"; cwd: string; stopHookActive: boolean; transcriptPath: string | undefined }
86
+ | ({
87
+ event: "stop";
88
+ cwd: string;
89
+ stopHookActive: boolean;
90
+ } & ({ transcriptPath: string | undefined } | { agentTranscriptPath: string | undefined }))
71
91
  | {
72
92
  event: "write";
73
93
  cwd: string;
@@ -89,7 +109,7 @@ export function hookInput(
89
109
  if (event != null && typeof event !== "string")
90
110
  return { error: "hook_event_name must be a string" };
91
111
 
92
- if (event != null && !events.has(event)) return { event: "ignored" };
112
+ if (event != null && !hookEvents.has(event)) return { event: "ignored" };
93
113
 
94
114
  const cwd = "cwd" in input ? input.cwd : undefined;
95
115
  if (cwd != null && typeof cwd !== "string") return { error: "cwd must be a string" };
@@ -104,7 +124,7 @@ export function hookInput(
104
124
  if (event === "PreToolUse") {
105
125
  const toolInput = "tool_input" in input ? input.tool_input : undefined;
106
126
  if (
107
- !("tool_name" in input && input.tool_name === "Write") ||
127
+ !usesTool(input, "PreToolUse") ||
108
128
  !isRecord(toolInput) ||
109
129
  typeof toolInput.file_path !== "string" ||
110
130
  typeof toolInput.content !== "string"
@@ -118,32 +138,53 @@ export function hookInput(
118
138
  };
119
139
  }
120
140
 
121
- const transcriptPath = "transcript_path" in input ? input.transcript_path : undefined;
141
+ const transcriptField = event === "SubagentStop" ? "agent_transcript_path" : "transcript_path";
142
+
143
+ const transcriptPath = transcriptField in input ? input[transcriptField] : undefined;
122
144
  if (transcriptPath != null && typeof transcriptPath !== "string")
123
- return { error: "transcript_path must be a string" };
145
+ return { error: `${transcriptField} must be a string` };
124
146
 
125
147
  const stopHookActive = "stop_hook_active" in input ? input.stop_hook_active : undefined;
126
148
  if (stopHookActive != null && typeof stopHookActive !== "boolean")
127
149
  return { error: "stop_hook_active must be a boolean" };
128
150
 
129
- return {
130
- event: "stop",
151
+ const stop = {
152
+ event: "stop" as const,
131
153
  cwd: dir,
132
154
  stopHookActive: stopHookActive ?? false,
133
- transcriptPath: transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath),
134
155
  };
156
+
157
+ const path = transcriptPath == null ? undefined : resolve(fallbackCwd, transcriptPath);
158
+ return event === "SubagentStop"
159
+ ? { ...stop, agentTranscriptPath: path }
160
+ : { ...stop, transcriptPath: path };
161
+ }
162
+
163
+ export const hookEvents: ReadonlySet<string> = new Set([
164
+ "Stop",
165
+ "SubagentStop",
166
+ "PreToolUse",
167
+ "PostToolUse",
168
+ ]);
169
+
170
+ export const hookTools: Readonly<Record<"PostToolUse" | "PreToolUse", ReadonlySet<string>>> = {
171
+ PostToolUse: new Set(["Edit", "MultiEdit", "apply_patch"]),
172
+ PreToolUse: new Set(["Write"]),
173
+ };
174
+
175
+ function usesTool(input: Record<string, unknown>, event: keyof typeof hookTools): boolean {
176
+ return typeof input.tool_name === "string" && hookTools[event].has(input.tool_name);
135
177
  }
136
178
 
137
- const events = new Set(["Stop", "SubagentStop", "PreToolUse", "PostToolUse"]);
138
179
  const patchHeader = /^\*\*\* (Add File|Update File|Move to): (.+)$/;
139
180
  const patchApplied = /^Success\. Updated the following files:$/m;
140
181
  function editedPaths(input: Record<string, unknown>): string[] {
141
182
  const toolInput = input.tool_input;
142
- if (!isRecord(toolInput)) return [];
143
- if (input.tool_name === "Edit" || input.tool_name === "MultiEdit")
183
+ if (!isRecord(toolInput) || !usesTool(input, "PostToolUse")) return [];
184
+ if (input.tool_name !== "apply_patch")
144
185
  return typeof toolInput.file_path === "string" ? [toolInput.file_path] : [];
145
186
 
146
- if (input.tool_name !== "apply_patch" || typeof toolInput.command !== "string") return [];
187
+ if (typeof toolInput.command !== "string") return [];
147
188
  if (typeof input.tool_response !== "string" || !patchApplied.test(input.tool_response)) return [];
148
189
 
149
190
  const paths: string[] = [];
@@ -1,5 +1,6 @@
1
- import { existsSync, readFileSync, realpathSync, statSync } from "node:fs";
2
- import { basename, dirname, isAbsolute, join, resolve } from "node:path";
1
+ import { readFileSync, realpathSync, statSync } from "node:fs";
2
+ import { dirname, isAbsolute, join, resolve } from "node:path";
3
+ import { holds } from "../../../directories.ts";
3
4
 
4
5
  const suffixes = ["", ".json", ".jsonc", ".js", ".cjs", ".mjs", ".ts", ".mts", ".cts", "/index.js"];
5
6
 
@@ -76,12 +77,6 @@ export function resolveModule(specifier: string, from: string, loader: Loader):
76
77
  }
77
78
  }
78
79
 
79
- export function realDirectory(dir: string): string {
80
- if (existsSync(dir)) return realpathSync(dir);
81
- const parent = dirname(dir);
82
- return parent === dir ? dir : join(realDirectory(parent), basename(dir));
83
- }
84
-
85
80
  export function* configDirectories(
86
81
  dir: string,
87
82
  reader: { family: string; files: readonly string[]; candidate?(file: string): boolean },
@@ -91,9 +86,11 @@ export function* configDirectories(
91
86
 
92
87
  let files = directoryFiles.get(key);
93
88
  if (!files) {
89
+ const current = dir;
94
90
  files = reader.files
95
- .map((name) => join(dir, name))
96
- .filter((file) => existsSync(file) && (reader.candidate?.(file) ?? true));
91
+ .filter((name) => holds(current, name))
92
+ .map((name) => join(current, name))
93
+ .filter((file) => reader.candidate?.(file) ?? true);
97
94
 
98
95
  directoryFiles.set(key, files);
99
96
  }
@@ -1,7 +1,7 @@
1
1
  import { resolve } from "node:path";
2
2
  import { biome } from "./biome.ts";
3
3
  import { flat, legacy } from "./eslint.ts";
4
- import { realDirectory } from "./find.ts";
4
+ import { realDirectory } from "../../../directories.ts";
5
5
  import type { Decision, Reader } from "./layers.ts";
6
6
  import { oxlint } from "./oxlint.ts";
7
7
 
@@ -1,4 +1,5 @@
1
- import { readFileSync, realpathSync } from "node:fs";
1
+ import { readFileSync } from "node:fs";
2
+ import { realPath } from "../../../directories.ts";
2
3
  import type { Node, Program } from "oxc-parser";
3
4
  import { boundNames, children, declared, referenceChild, walk } from "../ast.ts";
4
5
  import { parse } from "../parse.ts";
@@ -294,7 +295,7 @@ function indexModule(module: ConfigModule): void {
294
295
 
295
296
  export function moduleAt(file: string): ConfigModule | null {
296
297
  try {
297
- file = realpathSync(file);
298
+ file = realPath(file);
298
299
  if (modules.has(file)) return modules.get(file) ?? null;
299
300
 
300
301
  const text = readFileSync(file, "utf8");
@@ -2,7 +2,8 @@ import { existsSync, readFileSync } from "node:fs";
2
2
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
3
3
  import type { Width } from "../../language.ts";
4
4
  import { UNKNOWN, type Value, exported, object, property } from "./evaluate.ts";
5
- import { configDirectories, realDirectory } from "./find.ts";
5
+ import { holds, invocationMap, realDirectory } from "../../../directories.ts";
6
+ import { configDirectories } from "./find.ts";
6
7
  import { moduleAt } from "./module.ts";
7
8
 
8
9
  type Formatter = "oxfmt" | "prettier" | "biome";
@@ -72,7 +73,7 @@ const FILES = [
72
73
 
73
74
  const parsed = new Map<string, Read>();
74
75
  const directories = new Map<string, Directory>();
75
- const dependencies = new Map<string, Formatter[]>();
76
+ const dependencies = invocationMap<string, Formatter[]>();
76
77
  const globs = new Map<string, Bun.Glob>();
77
78
 
78
79
  const editorGlobs = new Map<string, { regex: RegExp; ranges: [bigint, bigint][] }>();
@@ -261,27 +262,32 @@ function directory(dir: string, files: string[]): Directory {
261
262
  }
262
263
 
263
264
  function dependent(dir: string): Formatter[] {
264
- const cached = dependencies.get(dir);
265
- if (cached) return cached;
265
+ const visited: string[] = [];
266
266
 
267
+ let found: Formatter[] = [];
267
268
  let current = dir;
268
269
  while (true) {
269
- try {
270
- const manifest = JSON.parse(readFileSync(join(current, "package.json"), "utf8")) as Record<
271
- string,
272
- Record<string, unknown>
273
- >;
274
-
275
- const names = { ...manifest.dependencies, ...manifest.devDependencies };
276
- const found: Formatter[] = [];
277
- if ("oxfmt" in names) found.push("oxfmt");
278
- if ("@biomejs/biome" in names) found.push("biome");
279
- if ("prettier" in names) found.push("prettier");
280
- if (found.length) {
281
- dependencies.set(dir, found);
282
- return found;
283
- }
284
- } catch {}
270
+ const cached = dependencies.get(current);
271
+ if (cached) {
272
+ found = cached;
273
+ break;
274
+ }
275
+
276
+ visited.push(current);
277
+
278
+ if (holds(current, "package.json"))
279
+ try {
280
+ const manifest = JSON.parse(readFileSync(join(current, "package.json"), "utf8")) as Record<
281
+ string,
282
+ Record<string, unknown>
283
+ >;
284
+
285
+ const names = { ...manifest.dependencies, ...manifest.devDependencies };
286
+ if ("oxfmt" in names) found.push("oxfmt");
287
+ if ("@biomejs/biome" in names) found.push("biome");
288
+ if ("prettier" in names) found.push("prettier");
289
+ if (found.length) break;
290
+ } catch {}
285
291
 
286
292
  const parent = dirname(current);
287
293
  if (parent === current) break;
@@ -289,8 +295,8 @@ function dependent(dir: string): Formatter[] {
289
295
  current = parent;
290
296
  }
291
297
 
292
- dependencies.set(dir, []);
293
- return [];
298
+ for (const directory of visited) dependencies.set(directory, found);
299
+ return found;
294
300
  }
295
301
 
296
302
  function number(value: Value): number | undefined {
@@ -586,7 +592,7 @@ function editorconfig(
586
592
  const file = join(dir, ".editorconfig");
587
593
 
588
594
  let root = false;
589
- if (existsSync(file)) {
595
+ if (holds(dir, ".editorconfig")) {
590
596
  const editor = parsedEditor(file);
591
597
  if (!editor) unread.push(file);
592
598
  else {
@@ -612,10 +618,10 @@ function editorconfig(
612
618
  // oxfmt 0.70 reads only the nearest .editorconfig file.
613
619
  if (
614
620
  biomeDir ||
615
- (nearest && existsSync(file)) ||
621
+ (nearest && holds(dir, ".editorconfig")) ||
616
622
  root ||
617
- existsSync(join(dir, ".git")) ||
618
- existsSync(join(dir, ".hg"))
623
+ holds(dir, ".git") ||
624
+ holds(dir, ".hg")
619
625
  )
620
626
  break;
621
627