@nightloom/isy 1.0.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.
Files changed (223) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -0
  3. package/dist/agents/claude.d.ts +6 -0
  4. package/dist/agents/claude.js +66 -0
  5. package/dist/agents/claude.js.map +1 -0
  6. package/dist/agents/codex-records.d.ts +34 -0
  7. package/dist/agents/codex-records.js +492 -0
  8. package/dist/agents/codex-records.js.map +1 -0
  9. package/dist/agents/codex.d.ts +5 -0
  10. package/dist/agents/codex.js +205 -0
  11. package/dist/agents/codex.js.map +1 -0
  12. package/dist/agents/index.d.ts +25 -0
  13. package/dist/agents/index.js +50 -0
  14. package/dist/agents/index.js.map +1 -0
  15. package/dist/agents/kimi-records.d.ts +24 -0
  16. package/dist/agents/kimi-records.js +252 -0
  17. package/dist/agents/kimi-records.js.map +1 -0
  18. package/dist/agents/kimi-wire.d.ts +8 -0
  19. package/dist/agents/kimi-wire.js +158 -0
  20. package/dist/agents/kimi-wire.js.map +1 -0
  21. package/dist/agents/kimi.d.ts +6 -0
  22. package/dist/agents/kimi.js +332 -0
  23. package/dist/agents/kimi.js.map +1 -0
  24. package/dist/agents/types.d.ts +83 -0
  25. package/dist/agents/types.js +2 -0
  26. package/dist/agents/types.js.map +1 -0
  27. package/dist/alert.d.ts +24 -0
  28. package/dist/alert.js +68 -0
  29. package/dist/alert.js.map +1 -0
  30. package/dist/api.d.ts +103 -0
  31. package/dist/api.js +171 -0
  32. package/dist/api.js.map +1 -0
  33. package/dist/commands/analyze.d.ts +61 -0
  34. package/dist/commands/analyze.js +198 -0
  35. package/dist/commands/analyze.js.map +1 -0
  36. package/dist/commands/check.d.ts +55 -0
  37. package/dist/commands/check.js +180 -0
  38. package/dist/commands/check.js.map +1 -0
  39. package/dist/commands/commit.d.ts +26 -0
  40. package/dist/commands/commit.js +130 -0
  41. package/dist/commands/commit.js.map +1 -0
  42. package/dist/commands/health.d.ts +47 -0
  43. package/dist/commands/health.js +366 -0
  44. package/dist/commands/health.js.map +1 -0
  45. package/dist/commands/init.d.ts +3 -0
  46. package/dist/commands/init.js +173 -0
  47. package/dist/commands/init.js.map +1 -0
  48. package/dist/commands/logout.d.ts +1 -0
  49. package/dist/commands/logout.js +30 -0
  50. package/dist/commands/logout.js.map +1 -0
  51. package/dist/commands/notes.d.ts +11 -0
  52. package/dist/commands/notes.js +49 -0
  53. package/dist/commands/notes.js.map +1 -0
  54. package/dist/commands/scan.d.ts +47 -0
  55. package/dist/commands/scan.js +199 -0
  56. package/dist/commands/scan.js.map +1 -0
  57. package/dist/commands/settings.d.ts +15 -0
  58. package/dist/commands/settings.js +94 -0
  59. package/dist/commands/settings.js.map +1 -0
  60. package/dist/commands/status.d.ts +61 -0
  61. package/dist/commands/status.js +151 -0
  62. package/dist/commands/status.js.map +1 -0
  63. package/dist/commands/upload.d.ts +42 -0
  64. package/dist/commands/upload.js +429 -0
  65. package/dist/commands/upload.js.map +1 -0
  66. package/dist/config.d.ts +4 -0
  67. package/dist/config.js +32 -0
  68. package/dist/config.js.map +1 -0
  69. package/dist/detectors.d.ts +43 -0
  70. package/dist/detectors.js +1026 -0
  71. package/dist/detectors.js.map +1 -0
  72. package/dist/git.d.ts +22 -0
  73. package/dist/git.js +68 -0
  74. package/dist/git.js.map +1 -0
  75. package/dist/githook.d.ts +71 -0
  76. package/dist/githook.js +238 -0
  77. package/dist/githook.js.map +1 -0
  78. package/dist/hook.d.ts +39 -0
  79. package/dist/hook.js +173 -0
  80. package/dist/hook.js.map +1 -0
  81. package/dist/index.d.ts +2 -0
  82. package/dist/index.js +205 -0
  83. package/dist/index.js.map +1 -0
  84. package/dist/log.d.ts +19 -0
  85. package/dist/log.js +97 -0
  86. package/dist/log.js.map +1 -0
  87. package/dist/mask-paths.d.ts +75 -0
  88. package/dist/mask-paths.js +364 -0
  89. package/dist/mask-paths.js.map +1 -0
  90. package/dist/pairing.d.ts +60 -0
  91. package/dist/pairing.js +137 -0
  92. package/dist/pairing.js.map +1 -0
  93. package/dist/parser.d.ts +48 -0
  94. package/dist/parser.js +516 -0
  95. package/dist/parser.js.map +1 -0
  96. package/dist/paths.d.ts +41 -0
  97. package/dist/paths.js +181 -0
  98. package/dist/paths.js.map +1 -0
  99. package/dist/plan.d.ts +25 -0
  100. package/dist/plan.js +39 -0
  101. package/dist/plan.js.map +1 -0
  102. package/dist/queue.d.ts +26 -0
  103. package/dist/queue.js +120 -0
  104. package/dist/queue.js.map +1 -0
  105. package/dist/redact.d.ts +29 -0
  106. package/dist/redact.js +254 -0
  107. package/dist/redact.js.map +1 -0
  108. package/dist/spawn.d.ts +13 -0
  109. package/dist/spawn.js +32 -0
  110. package/dist/spawn.js.map +1 -0
  111. package/dist/stage0.d.ts +20 -0
  112. package/dist/stage0.js +90 -0
  113. package/dist/stage0.js.map +1 -0
  114. package/dist/state.d.ts +84 -0
  115. package/dist/state.js +150 -0
  116. package/dist/state.js.map +1 -0
  117. package/dist/test/analyze.test.d.ts +1 -0
  118. package/dist/test/analyze.test.js +408 -0
  119. package/dist/test/analyze.test.js.map +1 -0
  120. package/dist/test/check.test.d.ts +1 -0
  121. package/dist/test/check.test.js +193 -0
  122. package/dist/test/check.test.js.map +1 -0
  123. package/dist/test/cli.test.d.ts +1 -0
  124. package/dist/test/cli.test.js +132 -0
  125. package/dist/test/cli.test.js.map +1 -0
  126. package/dist/test/codex.test.d.ts +1 -0
  127. package/dist/test/codex.test.js +585 -0
  128. package/dist/test/codex.test.js.map +1 -0
  129. package/dist/test/commit.test.d.ts +1 -0
  130. package/dist/test/commit.test.js +430 -0
  131. package/dist/test/commit.test.js.map +1 -0
  132. package/dist/test/git.test.d.ts +1 -0
  133. package/dist/test/git.test.js +75 -0
  134. package/dist/test/git.test.js.map +1 -0
  135. package/dist/test/health.test.d.ts +1 -0
  136. package/dist/test/health.test.js +156 -0
  137. package/dist/test/health.test.js.map +1 -0
  138. package/dist/test/hook.test.d.ts +1 -0
  139. package/dist/test/hook.test.js +144 -0
  140. package/dist/test/hook.test.js.map +1 -0
  141. package/dist/test/kimi.test.d.ts +1 -0
  142. package/dist/test/kimi.test.js +349 -0
  143. package/dist/test/kimi.test.js.map +1 -0
  144. package/dist/test/mask-paths.test.d.ts +1 -0
  145. package/dist/test/mask-paths.test.js +230 -0
  146. package/dist/test/mask-paths.test.js.map +1 -0
  147. package/dist/test/notes.test.d.ts +1 -0
  148. package/dist/test/notes.test.js +41 -0
  149. package/dist/test/notes.test.js.map +1 -0
  150. package/dist/test/pairing.test.d.ts +1 -0
  151. package/dist/test/pairing.test.js +138 -0
  152. package/dist/test/pairing.test.js.map +1 -0
  153. package/dist/test/parser.test.d.ts +1 -0
  154. package/dist/test/parser.test.js +312 -0
  155. package/dist/test/parser.test.js.map +1 -0
  156. package/dist/test/plan.test.d.ts +1 -0
  157. package/dist/test/plan.test.js +34 -0
  158. package/dist/test/plan.test.js.map +1 -0
  159. package/dist/test/redact.test.d.ts +1 -0
  160. package/dist/test/redact.test.js +303 -0
  161. package/dist/test/redact.test.js.map +1 -0
  162. package/dist/test/settings.test.d.ts +1 -0
  163. package/dist/test/settings.test.js +32 -0
  164. package/dist/test/settings.test.js.map +1 -0
  165. package/dist/test/stage0.test.d.ts +1 -0
  166. package/dist/test/stage0.test.js +607 -0
  167. package/dist/test/stage0.test.js.map +1 -0
  168. package/dist/test/sweep.test.d.ts +1 -0
  169. package/dist/test/sweep.test.js +298 -0
  170. package/dist/test/sweep.test.js.map +1 -0
  171. package/dist/test/update.test.d.ts +1 -0
  172. package/dist/test/update.test.js +26 -0
  173. package/dist/test/update.test.js.map +1 -0
  174. package/dist/test/upload.test.d.ts +1 -0
  175. package/dist/test/upload.test.js +331 -0
  176. package/dist/test/upload.test.js.map +1 -0
  177. package/dist/types.d.ts +142 -0
  178. package/dist/types.js +2 -0
  179. package/dist/types.js.map +1 -0
  180. package/dist/update.d.ts +17 -0
  181. package/dist/update.js +46 -0
  182. package/dist/update.js.map +1 -0
  183. package/package.json +60 -0
  184. package/src/agents/claude.ts +89 -0
  185. package/src/agents/codex-records.ts +533 -0
  186. package/src/agents/codex.ts +238 -0
  187. package/src/agents/index.ts +58 -0
  188. package/src/agents/kimi-records.ts +274 -0
  189. package/src/agents/kimi-wire.ts +179 -0
  190. package/src/agents/kimi.ts +390 -0
  191. package/src/agents/types.ts +92 -0
  192. package/src/alert.ts +68 -0
  193. package/src/api.ts +269 -0
  194. package/src/commands/analyze.ts +308 -0
  195. package/src/commands/check.ts +239 -0
  196. package/src/commands/commit.ts +179 -0
  197. package/src/commands/health.ts +444 -0
  198. package/src/commands/init.ts +186 -0
  199. package/src/commands/logout.ts +33 -0
  200. package/src/commands/notes.ts +57 -0
  201. package/src/commands/scan.ts +278 -0
  202. package/src/commands/settings.ts +107 -0
  203. package/src/commands/status.ts +236 -0
  204. package/src/commands/upload.ts +524 -0
  205. package/src/config.ts +33 -0
  206. package/src/detectors.ts +1138 -0
  207. package/src/git.ts +86 -0
  208. package/src/githook.ts +269 -0
  209. package/src/hook.ts +237 -0
  210. package/src/index.ts +229 -0
  211. package/src/log.ts +113 -0
  212. package/src/mask-paths.ts +434 -0
  213. package/src/pairing.ts +190 -0
  214. package/src/parser.ts +581 -0
  215. package/src/paths.ts +191 -0
  216. package/src/plan.ts +56 -0
  217. package/src/queue.ts +145 -0
  218. package/src/redact.ts +335 -0
  219. package/src/spawn.ts +33 -0
  220. package/src/stage0.ts +121 -0
  221. package/src/state.ts +208 -0
  222. package/src/types.ts +165 -0
  223. package/src/update.ts +46 -0
package/src/redact.ts ADDED
@@ -0,0 +1,335 @@
1
+ import { createReadStream } from "node:fs";
2
+ import { homedir } from "node:os";
3
+ import { basename } from "node:path";
4
+ import { createInterface } from "node:readline";
5
+ import {
6
+ PATH_KEYS,
7
+ REDACTION_MARKER,
8
+ marker,
9
+ maskPath,
10
+ maskPaths,
11
+ resolveRoots,
12
+ rootOf,
13
+ } from "./mask-paths.js";
14
+ import type { MaskRoots, PreparedRoots } from "./mask-paths.js";
15
+
16
+ // The marker vocabulary lives with the path rules, which cannot import this
17
+ // module back without a cycle. Re-exported so every caller has one import.
18
+ export { REDACTION_MARKER, marker };
19
+
20
+ export interface RedactionRule {
21
+ type: string;
22
+ pattern: RegExp;
23
+ replacement: string;
24
+ }
25
+
26
+ export const BUILTIN_RULES: RedactionRule[] = [
27
+ {
28
+ type: "private_key",
29
+ pattern: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY-----|$)/g,
30
+ replacement: marker("private_key"),
31
+ },
32
+ {
33
+ type: "jwt",
34
+ pattern: /\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g,
35
+ replacement: marker("jwt"),
36
+ },
37
+ {
38
+ type: "anthropic_key",
39
+ pattern: /\bsk-ant-[A-Za-z0-9_-]{16,}/g,
40
+ replacement: marker("anthropic_key"),
41
+ },
42
+ {
43
+ type: "openai_key",
44
+ pattern: /\bsk-[A-Za-z0-9_-]{16,}/g,
45
+ replacement: marker("openai_key"),
46
+ },
47
+ {
48
+ type: "github_token",
49
+ pattern: /\b(?:ghp|gho|ghs|ghu)_[A-Za-z0-9]{36}\b/g,
50
+ replacement: marker("github_token"),
51
+ },
52
+ {
53
+ type: "github_token",
54
+ pattern: /\bgithub_pat_[A-Za-z0-9_]{20,}/g,
55
+ replacement: marker("github_token"),
56
+ },
57
+ {
58
+ type: "aws_key",
59
+ pattern: /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/g,
60
+ replacement: marker("aws_key"),
61
+ },
62
+ {
63
+ type: "google_key",
64
+ pattern: /\bAIza[A-Za-z0-9_-]{35}\b/g,
65
+ replacement: marker("google_key"),
66
+ },
67
+ {
68
+ type: "connection_string",
69
+ pattern: /\b(postgres|postgresql|mysql|mongodb\+srv|mongodb|redis|amqp):\/\/([^\s:/@]+):([^\s/@]+)@/g,
70
+ replacement: `$1://$2:${marker("connection_string")}@`,
71
+ },
72
+ {
73
+ type: "assignment",
74
+ pattern:
75
+ /(?<![A-Za-z0-9])(password|passwd|secret|token|api[-_]?key|private[-_]?key)(\s*[=:]\s*)(["']?)(?!\[ISY_REDACTED)[^\s"'{}()[\]<>$]{8,}/gi,
76
+ replacement: `$1$2$3${marker("assignment")}`,
77
+ },
78
+ ];
79
+
80
+ const SENSITIVE_KEY = /^(password|passwd|secret|token|api[-_]?key|private[-_]?key)$/i;
81
+ const MIN_SENSITIVE_VALUE = 8;
82
+ const EDIT_INPUT_FIELDS = ["content", "old_string", "new_string", "new_source"];
83
+
84
+ export interface RedactionSummary {
85
+ counts: Record<string, number>;
86
+ replacements: number;
87
+ lines: number;
88
+ unparsedLines: number;
89
+ invalidExtraPatterns: string[];
90
+ }
91
+
92
+ interface RedactContext {
93
+ rules: RedactionRule[];
94
+ counts: Record<string, number>;
95
+ envToolUseIds: Set<string>;
96
+ /**
97
+ * Mutable: a transcript names its own working directory on every record, so a
98
+ * caller that did not pass one still gets repository-relative paths. The
99
+ * first record that carries one wins — a session that wanders between
100
+ * directories is judged from where it started.
101
+ */
102
+ roots: PreparedRoots;
103
+ /** Whether the working directory was given rather than read off a record. */
104
+ rootGiven: boolean;
105
+ }
106
+
107
+ function isObject(value: unknown): value is Record<string, unknown> {
108
+ return typeof value === "object" && value !== null && !Array.isArray(value);
109
+ }
110
+
111
+ function count(context: RedactContext, type: string, amount: number): void {
112
+ context.counts[type] = (context.counts[type] ?? 0) + amount;
113
+ }
114
+
115
+ export function compileExtraRules(
116
+ patterns: readonly string[] | undefined,
117
+ invalid: string[],
118
+ ): RedactionRule[] {
119
+ const rules: RedactionRule[] = [];
120
+ for (const source of patterns ?? []) {
121
+ try {
122
+ rules.push({ type: "custom", pattern: new RegExp(source, "g"), replacement: marker("custom") });
123
+ } catch {
124
+ invalid.push(source);
125
+ }
126
+ }
127
+ return rules;
128
+ }
129
+
130
+ export function applyRules(
131
+ text: string,
132
+ rules: readonly RedactionRule[],
133
+ onMatch?: (type: string, amount: number) => void,
134
+ ): string {
135
+ let result = text;
136
+ for (const rule of rules) {
137
+ const matches = result.match(rule.pattern);
138
+ if (!matches) continue;
139
+ onMatch?.(rule.type, matches.length);
140
+ result = result.replace(rule.pattern, rule.replacement);
141
+ }
142
+ return result;
143
+ }
144
+
145
+ function isDotenvPath(filePath: unknown): boolean {
146
+ return typeof filePath === "string" && basename(filePath).startsWith(".env");
147
+ }
148
+
149
+ function cutDotenvInput(input: Record<string, unknown>, context: RedactContext): void {
150
+ for (const field of EDIT_INPUT_FIELDS) {
151
+ if (typeof input[field] === "string") {
152
+ input[field] = marker("dotenv");
153
+ count(context, "dotenv", 1);
154
+ }
155
+ }
156
+ }
157
+
158
+ function scanDotenv(record: Record<string, unknown>, context: RedactContext): void {
159
+ const message = record.message;
160
+ const content = isObject(message) ? message.content : undefined;
161
+ if (!Array.isArray(content)) return;
162
+
163
+ for (const block of content) {
164
+ if (!isObject(block)) continue;
165
+
166
+ if (block.type === "tool_use") {
167
+ const input = isObject(block.input) ? block.input : undefined;
168
+ if (!input) continue;
169
+ if (!isDotenvPath(input.file_path) && !isDotenvPath(input.notebook_path)) continue;
170
+ if (typeof block.id === "string") context.envToolUseIds.add(block.id);
171
+ cutDotenvInput(input, context);
172
+ }
173
+
174
+ if (block.type === "tool_result") {
175
+ const id = block.tool_use_id;
176
+ if (typeof id !== "string" || !context.envToolUseIds.has(id)) continue;
177
+ block.content = marker("dotenv");
178
+ count(context, "dotenv", 1);
179
+ if (record.toolUseResult !== undefined) {
180
+ record.toolUseResult = marker("dotenv");
181
+ count(context, "dotenv", 1);
182
+ }
183
+ }
184
+ }
185
+ }
186
+
187
+ function redactValue(value: unknown, context: RedactContext, key?: string): unknown {
188
+ if (typeof value === "string") {
189
+ if (key && SENSITIVE_KEY.test(key) && value.length >= MIN_SENSITIVE_VALUE) {
190
+ if (value.startsWith(`[${REDACTION_MARKER}`)) return value;
191
+ count(context, "assignment", 1);
192
+ return marker("assignment");
193
+ }
194
+ const secretsGone = applyRules(value, context.rules, (type, amount) =>
195
+ count(context, type, amount),
196
+ );
197
+ // Paths last: a secret already replaced by its marker must not then be read
198
+ // as a directory, and `[ISY_REDACTED:…]` carries no slash to be read as one.
199
+ const mask = key && PATH_KEYS.has(key) ? maskPath : maskPaths;
200
+ return mask(secretsGone, context.roots, (amount) => count(context, "path", amount));
201
+ }
202
+
203
+ if (Array.isArray(value)) return value.map((item) => redactValue(item, context));
204
+
205
+ if (isObject(value)) {
206
+ const result: Record<string, unknown> = {};
207
+ for (const [entryKey, entryValue] of Object.entries(value)) {
208
+ // A key holds a path too — Claude Code keys its per-file state by
209
+ // absolute path — and `maskDeep` masks keys for the same reason. Only
210
+ // the path rules run on it: a secret is a value, never a field name.
211
+ const masked = maskPath(entryKey, context.roots, (amount) => count(context, "path", amount));
212
+ result[masked] = redactValue(entryValue, context, entryKey);
213
+ }
214
+ return result;
215
+ }
216
+
217
+ return value;
218
+ }
219
+
220
+ /** The working directory this transcript recorded, before anything is rewritten. */
221
+ function learnRoot(record: Record<string, unknown>, context: RedactContext): void {
222
+ if (context.rootGiven || context.roots.cwd) return;
223
+ const cwd = rootOf(record);
224
+ if (!cwd) return;
225
+ context.roots = { ...context.roots, ...resolveRoots({ cwd }) };
226
+ }
227
+
228
+ function redactRecord(
229
+ record: Record<string, unknown>,
230
+ context: RedactContext,
231
+ ): Record<string, unknown> {
232
+ learnRoot(record, context);
233
+ scanDotenv(record, context);
234
+ return redactValue(record, context) as Record<string, unknown>;
235
+ }
236
+
237
+ export interface RedactOptions extends MaskRoots {
238
+ extraPatterns?: readonly string[];
239
+ }
240
+
241
+ function createContext(options: RedactOptions, invalid: string[]): RedactContext {
242
+ return {
243
+ rules: [...BUILTIN_RULES, ...compileExtraRules(options.extraPatterns, invalid)],
244
+ counts: {},
245
+ envToolUseIds: new Set(),
246
+ // The home directory is this machine's and is never in the transcript's
247
+ // gift, so it is read here rather than passed by every caller.
248
+ roots: resolveRoots({ cwd: options.cwd, home: options.home ?? homedir() }),
249
+ rootGiven: Boolean(options.cwd),
250
+ };
251
+ }
252
+
253
+ function summarize(
254
+ context: RedactContext,
255
+ lines: number,
256
+ unparsedLines: number,
257
+ invalidExtraPatterns: string[],
258
+ ): RedactionSummary {
259
+ const replacements = Object.values(context.counts).reduce((total, value) => total + value, 0);
260
+ return { counts: context.counts, replacements, lines, unparsedLines, invalidExtraPatterns };
261
+ }
262
+
263
+ function redactOneLine(line: string, context: RedactContext): { line: string; unparsed: boolean } {
264
+ const trimmed = line.trim();
265
+ if (trimmed.length === 0) return { line: trimmed, unparsed: false };
266
+
267
+ let parsed: unknown;
268
+ try {
269
+ parsed = JSON.parse(trimmed);
270
+ } catch {
271
+ const secretsGone = applyRules(trimmed, context.rules, (type, amount) =>
272
+ count(context, type, amount),
273
+ );
274
+ return {
275
+ line: maskPaths(secretsGone, context.roots, (amount) => count(context, "path", amount)),
276
+ unparsed: true,
277
+ };
278
+ }
279
+
280
+ if (!isObject(parsed)) {
281
+ return {
282
+ line: JSON.stringify(redactValue(parsed, context)),
283
+ unparsed: true,
284
+ };
285
+ }
286
+
287
+ return { line: JSON.stringify(redactRecord(parsed, context)), unparsed: false };
288
+ }
289
+
290
+ export function redactLines(
291
+ lines: Iterable<string>,
292
+ options: RedactOptions = {},
293
+ ): { lines: string[]; summary: RedactionSummary } {
294
+ const invalid: string[] = [];
295
+ const context = createContext(options, invalid);
296
+ const output: string[] = [];
297
+ let total = 0;
298
+ let unparsed = 0;
299
+
300
+ for (const line of lines) {
301
+ total += 1;
302
+ const result = redactOneLine(line, context);
303
+ if (result.line.length === 0) continue;
304
+ if (result.unparsed) unparsed += 1;
305
+ output.push(result.line);
306
+ }
307
+
308
+ return { lines: output, summary: summarize(context, total, unparsed, invalid) };
309
+ }
310
+
311
+ export async function redactTranscriptFile(
312
+ filePath: string,
313
+ options: RedactOptions = {},
314
+ ): Promise<{ lines: string[]; summary: RedactionSummary }> {
315
+ const invalid: string[] = [];
316
+ const context = createContext(options, invalid);
317
+ const reader = createInterface({
318
+ input: createReadStream(filePath, { encoding: "utf8" }),
319
+ crlfDelay: Infinity,
320
+ });
321
+
322
+ const output: string[] = [];
323
+ let total = 0;
324
+ let unparsed = 0;
325
+
326
+ for await (const line of reader) {
327
+ total += 1;
328
+ const result = redactOneLine(line, context);
329
+ if (result.line.length === 0) continue;
330
+ if (result.unparsed) unparsed += 1;
331
+ output.push(result.line);
332
+ }
333
+
334
+ return { lines: output, summary: summarize(context, total, unparsed, invalid) };
335
+ }
package/src/spawn.ts ADDED
@@ -0,0 +1,33 @@
1
+ import { spawn } from "node:child_process";
2
+ import { logError } from "./log.js";
3
+
4
+ export type SpawnOutcome = "spawned" | "not-attempted";
5
+
6
+ /**
7
+ * Run another isy command in the background and stop caring about it.
8
+ *
9
+ * Two callers, one reason: neither a commit nor the start of a session may wait
10
+ * on the network. The child re-executes this same entry point rather than
11
+ * resolving `isy` on PATH, because a hook fired by a desktop app inherits none
12
+ * of the shell's environment and `npx` is frequently not on it.
13
+ *
14
+ * A failure to spawn is not reported to the user: the transcript keeps its
15
+ * unsent hash, so the next trigger tries again.
16
+ */
17
+ export function spawnIsy(args: readonly string[], cwd?: string): SpawnOutcome {
18
+ const entry = process.argv[1];
19
+ if (!entry) return "not-attempted";
20
+
21
+ try {
22
+ const child = spawn(process.execPath, [entry, ...args], {
23
+ ...(cwd ? { cwd } : {}),
24
+ detached: true,
25
+ stdio: "ignore",
26
+ });
27
+ child.unref();
28
+ return "spawned";
29
+ } catch (cause) {
30
+ logError(`spawn: could not start isy ${args.join(" ")}: ${String(cause)}`);
31
+ return "not-attempted";
32
+ }
33
+ }
package/src/stage0.ts ADDED
@@ -0,0 +1,121 @@
1
+ import {
2
+ detectAbandonedApproach,
3
+ detectDestructiveCommand,
4
+ detectErrorSuppressed,
5
+ detectExternalDependency,
6
+ detectStaleContext,
7
+ detectTestModifiedToPass,
8
+ detectUnverifiedAssumption,
9
+ detectUnverifiedFix,
10
+ } from "./detectors.js";
11
+ import { looksAbsolute, maskPath } from "./mask-paths.js";
12
+ import type { Candidate, ParsedSession, SignalCategory } from "./types.js";
13
+
14
+ export const MIN_ASSISTANT_RECORDS = 20;
15
+
16
+ export type IneligibleReason = "too-short" | "no-file-edits";
17
+
18
+ export interface Stage0Result {
19
+ sessionId?: string;
20
+ eligible: boolean;
21
+ reason?: IneligibleReason;
22
+ thinkingBlocks: number;
23
+ knownGapReachable: boolean;
24
+ candidates: Candidate[];
25
+ }
26
+
27
+ export function eligibility(session: ParsedSession): IneligibleReason | undefined {
28
+ if (session.meta.assistantRecords < MIN_ASSISTANT_RECORDS) return "too-short";
29
+ if (!session.meta.hasFileEdits) return "no-file-edits";
30
+ return undefined;
31
+ }
32
+
33
+ function order(a: Candidate, b: Candidate): number {
34
+ if (a.recordIndex !== b.recordIndex) return a.recordIndex - b.recordIndex;
35
+ if (a.category !== b.category) return a.category < b.category ? -1 : 1;
36
+ return (a.filePath ?? "").localeCompare(b.filePath ?? "");
37
+ }
38
+
39
+ /**
40
+ * A finding about a file no repository holds — `/tmp/reset.py`, a scratch
41
+ * script — has nothing to say to a pull request. Masking decides it the way the
42
+ * upload does: under the session's directory a path turns relative, under the
43
+ * home it turns `~/…`, and only what stays absolute lies outside both. The
44
+ * server reads transcripts already masked, `cwd` included (it reads `.`), so
45
+ * there the paths are judged as they are.
46
+ *
47
+ * ponytail: a repository outside the home, with the session started in one of
48
+ * its subdirectories, loses findings on sibling directories; the git root
49
+ * recorded at upload is the way up.
50
+ */
51
+ function inTree(candidate: Candidate, cwd: string | undefined): boolean {
52
+ if (candidate.filePath === undefined || cwd === undefined) return true;
53
+ return !looksAbsolute(maskPath(candidate.filePath, looksAbsolute(cwd) ? { cwd } : {}));
54
+ }
55
+
56
+ export function runStage0(session: ParsedSession): Stage0Result {
57
+ const thinkingBlocks = session.meta.thinkingBlocks;
58
+ const reason = eligibility(session);
59
+
60
+ if (reason) {
61
+ return {
62
+ sessionId: session.sessionId,
63
+ eligible: false,
64
+ reason,
65
+ thinkingBlocks,
66
+ knownGapReachable: thinkingBlocks > 0,
67
+ candidates: [],
68
+ };
69
+ }
70
+
71
+ const abandoned = detectAbandonedApproach(session);
72
+ // A `git reset --hard` that threw away this session's own edits is already
73
+ // reported as an abandoned approach. Saying it twice, under two headings, is
74
+ // one command and two notes for the reviewer to reconcile.
75
+ const alreadyReported = new Set(abandoned.map((candidate) => candidate.toolUseId));
76
+
77
+ const candidates = [
78
+ ...abandoned,
79
+ ...detectTestModifiedToPass(session),
80
+ ...detectExternalDependency(session),
81
+ ...detectUnverifiedAssumption(session),
82
+ ...detectUnverifiedFix(session),
83
+ ...detectErrorSuppressed(session),
84
+ ...detectStaleContext(session),
85
+ ...detectDestructiveCommand(session).filter((c) => !alreadyReported.has(c.toolUseId)),
86
+ ]
87
+ .filter((candidate) => inTree(candidate, session.meta.cwd))
88
+ .sort(order);
89
+
90
+ return {
91
+ sessionId: session.sessionId,
92
+ eligible: true,
93
+ thinkingBlocks,
94
+ knownGapReachable: thinkingBlocks > 0,
95
+ candidates,
96
+ };
97
+ }
98
+
99
+ export function countByCategory(candidates: readonly Candidate[]): Record<string, number> {
100
+ const counts: Record<string, number> = {};
101
+ for (const candidate of candidates) {
102
+ counts[candidate.category] = (counts[candidate.category] ?? 0) + 1;
103
+ }
104
+ return counts;
105
+ }
106
+
107
+ export function runStage0All(sessions: readonly ParsedSession[]): {
108
+ results: Stage0Result[];
109
+ candidates: Candidate[];
110
+ byCategory: Record<SignalCategory, number> | Record<string, number>;
111
+ silent: boolean;
112
+ } {
113
+ const results = sessions.map(runStage0);
114
+ const candidates = results.flatMap((result) => result.candidates);
115
+ return {
116
+ results,
117
+ candidates,
118
+ byCategory: countByCategory(candidates),
119
+ silent: candidates.length === 0,
120
+ };
121
+ }
package/src/state.ts ADDED
@@ -0,0 +1,208 @@
1
+ import { mkdir, rename, stat, unlink, writeFile, readFile } from "node:fs/promises";
2
+ import { dirname, join } from "node:path";
3
+ import { isyHome, sessionStatePath } from "./paths.js";
4
+ import type { Candidate } from "./types.js";
5
+
6
+ /**
7
+ * Entries past this are dropped, least recently touched first.
8
+ *
9
+ * Sized for the sweep, which tracks every session on the machine rather than
10
+ * every session in one directory: an entry evicted while its transcript is
11
+ * still inside `SWEEP_WINDOW_MS` reads as changed again and is re-uploaded, so
12
+ * the cap has to sit above a fortnight's worth of sessions with room to spare.
13
+ */
14
+ const MAX_SESSIONS = 2000;
15
+ /** Mirrors the server's `recentShas` cap: one session never spans more usefully. */
16
+ const MAX_COMMITS = 100;
17
+
18
+ export interface CommitMark {
19
+ sha: string;
20
+ /** Records the transcript held when this commit was made. */
21
+ records: number;
22
+ }
23
+
24
+ /** What isy already did with one session. */
25
+ export interface SessionState {
26
+ /** contentHash of the last upload the server accepted. */
27
+ uploadedHash?: string;
28
+ /**
29
+ * Transcript size and mtime at that upload. Both unchanged means the bytes
30
+ * are unchanged, which lets a scan skip reading a transcript entirely.
31
+ */
32
+ sizeBytes?: number;
33
+ modifiedMs?: number;
34
+ /**
35
+ * SHA of the newest commit mark that upload carried. A commit made after the
36
+ * session ended leaves the transcript alone, yet its SHA is what a pull request
37
+ * is matched on, so it has to count as a change.
38
+ */
39
+ uploadedCommit?: string;
40
+ /** Candidate keys already printed, so a later commit does not repeat them. */
41
+ shown?: string[];
42
+ /** Record count at each commit, oldest first: maps a record index to a commit. */
43
+ commits?: CommitMark[];
44
+ /** Last write to this entry, for pruning. */
45
+ touchedMs?: number;
46
+ }
47
+
48
+ /**
49
+ * Keyed by absolute transcript path, not by session id: the scan has the path
50
+ * before it has parsed anything, which is what lets an unchanged transcript be
51
+ * skipped without being read.
52
+ */
53
+ export type ClientState = Record<string, SessionState>;
54
+
55
+ function isObject(value: unknown): value is Record<string, unknown> {
56
+ return typeof value === "object" && value !== null && !Array.isArray(value);
57
+ }
58
+
59
+ /**
60
+ * Never throws. A corrupt or unreadable state file must not stop an upload —
61
+ * the worst it costs is a repeated send the server answers with `deduplicated`.
62
+ */
63
+ export async function readState(): Promise<ClientState> {
64
+ try {
65
+ const parsed: unknown = JSON.parse(await readFile(sessionStatePath(), "utf8"));
66
+ return isObject(parsed) ? (parsed as ClientState) : {};
67
+ } catch {
68
+ return {};
69
+ }
70
+ }
71
+
72
+ /** Past this a lock is treated as abandoned: a sweep's own budget is 30s. */
73
+ const SWEEP_LOCK_MS = 5 * 60_000;
74
+
75
+ function sweepLockPath(): string {
76
+ return join(isyHome(), "sweep.lock");
77
+ }
78
+
79
+ /**
80
+ * Whether this process may sweep. Only one at a time: a sweep is spawned at
81
+ * every SessionStart, so two CLIs opened together would otherwise scan the same
82
+ * transcripts, upload each of them twice against an hourly allowance, and race
83
+ * each other's `updateSession` — which has no lock precisely because its writers
84
+ * were assumed not to touch the same fields.
85
+ *
86
+ * `wx` is the whole mechanism: creating a file that must not already exist is
87
+ * atomic on every filesystem isy runs on. A lock left behind by a killed sweep
88
+ * expires rather than blocking the machine forever.
89
+ */
90
+ export async function claimSweep(): Promise<boolean> {
91
+ const path = sweepLockPath();
92
+
93
+ try {
94
+ await mkdir(isyHome(), { recursive: true, mode: 0o700 });
95
+ await writeFile(path, `${process.pid}\n`, { flag: "wx", mode: 0o600 });
96
+ return true;
97
+ } catch {
98
+ // Either someone holds it or the write failed; the stale check tells us which.
99
+ }
100
+
101
+ try {
102
+ const held = await stat(path);
103
+ if (Date.now() - held.mtime.getTime() < SWEEP_LOCK_MS) return false;
104
+ await unlink(path);
105
+ await writeFile(path, `${process.pid}\n`, { flag: "wx", mode: 0o600 });
106
+ return true;
107
+ } catch {
108
+ // Another process took it between the unlink and the write, or the lock
109
+ // cannot be written at all. Not sweeping is the safe answer to both.
110
+ return false;
111
+ }
112
+ }
113
+
114
+ export async function releaseSweep(): Promise<void> {
115
+ try {
116
+ await unlink(sweepLockPath());
117
+ } catch {
118
+ // A lock that cannot be removed expires on its own.
119
+ }
120
+ }
121
+
122
+ function prune(state: ClientState): ClientState {
123
+ const entries = Object.entries(state);
124
+ if (entries.length <= MAX_SESSIONS) return state;
125
+
126
+ entries.sort(([, a], [, b]) => (b.touchedMs ?? 0) - (a.touchedMs ?? 0));
127
+ return Object.fromEntries(entries.slice(0, MAX_SESSIONS));
128
+ }
129
+
130
+ /**
131
+ * Read-modify-write, atomically enough that a reader never sees a torn file.
132
+ *
133
+ * ponytail: two isy processes writing at once can lose one update — `commit`
134
+ * spawns the uploader, and both touch this file. The fields they write are
135
+ * disjoint and a lost one only costs a reprint or a resend, so there is no lock.
136
+ * Add one if a third writer ever needs a field the others also set.
137
+ */
138
+ export async function updateSession(
139
+ transcriptPath: string,
140
+ patch: (current: SessionState) => SessionState,
141
+ ): Promise<void> {
142
+ const state = await readState();
143
+ const next = { ...patch(state[transcriptPath] ?? {}), touchedMs: Date.now() };
144
+
145
+ const path = sessionStatePath();
146
+ const temporary = join(dirname(path), `.sessions.json.isy-${process.pid}`);
147
+
148
+ try {
149
+ await mkdir(isyHome(), { recursive: true, mode: 0o700 });
150
+ await writeFile(
151
+ temporary,
152
+ `${JSON.stringify(prune({ ...state, [transcriptPath]: next }), null, 2)}\n`,
153
+ { mode: 0o600 },
154
+ );
155
+ await rename(temporary, path);
156
+ } catch {
157
+ // A cache that cannot be written is still a cache. Never fail the caller.
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Identity of one candidate across runs. A transcript is append-only, so a
163
+ * record that exists keeps its index and uuid as the session grows — the same
164
+ * finding produces the same key at every later commit.
165
+ */
166
+ export function candidateKey(candidate: Candidate): string {
167
+ return [
168
+ candidate.category,
169
+ candidate.filePath ?? "",
170
+ candidate.uuid ?? "",
171
+ candidate.toolUseId ?? "",
172
+ candidate.recordIndex,
173
+ // Escaped, never a raw byte: a NUL in the source makes git treat the file as binary.
174
+ ].join("\x00");
175
+ }
176
+
177
+ /** Candidates this session has not printed before. */
178
+ export function unseen(state: SessionState, candidates: readonly Candidate[]): Candidate[] {
179
+ const shown = new Set(state.shown ?? []);
180
+ return candidates.filter((candidate) => !shown.has(candidateKey(candidate)));
181
+ }
182
+
183
+ /**
184
+ * Append this commit's mark, replacing any earlier mark for the same SHA so an
185
+ * amend or a re-run does not record the commit twice.
186
+ */
187
+ export function withCommit(
188
+ state: SessionState,
189
+ mark: CommitMark | undefined,
190
+ ): CommitMark[] | undefined {
191
+ if (!mark) return state.commits;
192
+ const kept = (state.commits ?? []).filter((entry) => entry.sha !== mark.sha);
193
+ return [...kept, mark].slice(-MAX_COMMITS);
194
+ }
195
+
196
+ /**
197
+ * Whether the transcript or its commit marks changed since the last accepted
198
+ * upload. Size and mtime both matching means the bytes match, so the file need
199
+ * not be read at all.
200
+ */
201
+ export function looksUnchanged(
202
+ state: SessionState,
203
+ file: { sizeBytes: number; modifiedAt: Date },
204
+ ): boolean {
205
+ if (state.uploadedHash === undefined) return false;
206
+ if (state.commits?.at(-1)?.sha !== state.uploadedCommit) return false;
207
+ return state.sizeBytes === file.sizeBytes && state.modifiedMs === file.modifiedAt.getTime();
208
+ }