@nightloom/isy 1.0.2 → 1.0.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.
Files changed (62) hide show
  1. package/dist/agents/codex-records.js +13 -1
  2. package/dist/agents/codex-records.js.map +1 -1
  3. package/dist/api.d.ts +3 -0
  4. package/dist/api.js +1 -1
  5. package/dist/api.js.map +1 -1
  6. package/dist/commands/health.js +2 -2
  7. package/dist/commands/health.js.map +1 -1
  8. package/dist/commands/init.js +8 -0
  9. package/dist/commands/init.js.map +1 -1
  10. package/dist/commands/upload.d.ts +2 -0
  11. package/dist/commands/upload.js +30 -3
  12. package/dist/commands/upload.js.map +1 -1
  13. package/dist/mask-paths.js +31 -15
  14. package/dist/mask-paths.js.map +1 -1
  15. package/dist/parser.js +20 -7
  16. package/dist/parser.js.map +1 -1
  17. package/dist/plan.d.ts +3 -1
  18. package/dist/plan.js +5 -1
  19. package/dist/plan.js.map +1 -1
  20. package/dist/queue.d.ts +14 -2
  21. package/dist/queue.js +25 -3
  22. package/dist/queue.js.map +1 -1
  23. package/dist/redact.d.ts +7 -0
  24. package/dist/redact.js +156 -7
  25. package/dist/redact.js.map +1 -1
  26. package/dist/state.d.ts +9 -3
  27. package/dist/state.js +4 -4
  28. package/dist/state.js.map +1 -1
  29. package/dist/subagents.d.ts +8 -4
  30. package/dist/subagents.js +13 -7
  31. package/dist/subagents.js.map +1 -1
  32. package/dist/test/codex.test.js +15 -0
  33. package/dist/test/codex.test.js.map +1 -1
  34. package/dist/test/init.test.js +8 -0
  35. package/dist/test/init.test.js.map +1 -1
  36. package/dist/test/mask-paths.test.js +31 -0
  37. package/dist/test/mask-paths.test.js.map +1 -1
  38. package/dist/test/parser.test.js +16 -0
  39. package/dist/test/parser.test.js.map +1 -1
  40. package/dist/test/plan.test.js +7 -6
  41. package/dist/test/plan.test.js.map +1 -1
  42. package/dist/test/redact.test.js +178 -5
  43. package/dist/test/redact.test.js.map +1 -1
  44. package/dist/test/subagents.test.js +18 -0
  45. package/dist/test/subagents.test.js.map +1 -1
  46. package/dist/test/sweep.test.js +25 -0
  47. package/dist/test/sweep.test.js.map +1 -1
  48. package/dist/test/upload.test.js +68 -6
  49. package/dist/test/upload.test.js.map +1 -1
  50. package/package.json +3 -2
  51. package/src/agents/codex-records.ts +14 -1
  52. package/src/api.ts +5 -2
  53. package/src/commands/health.ts +2 -2
  54. package/src/commands/init.ts +8 -0
  55. package/src/commands/upload.ts +38 -3
  56. package/src/mask-paths.ts +31 -14
  57. package/src/parser.ts +16 -7
  58. package/src/plan.ts +4 -1
  59. package/src/queue.ts +30 -4
  60. package/src/redact.ts +168 -8
  61. package/src/state.ts +10 -4
  62. package/src/subagents.ts +20 -11
package/src/mask-paths.ts CHANGED
@@ -110,9 +110,10 @@ const URL_LIKE = /[A-Za-z][A-Za-z0-9+.-]*:\/\/[^\s"'`<>]*/;
110
110
  * rather than an address on the network. Node, vitest and Next print
111
111
  * `imported from file:///home/alice/…` on an unresolved import, and that lands
112
112
  * in a tool result like any other line of stdout; an editor spells the same
113
- * path `vscode://file/home/alice/…`. The scheme is kept, the path masked.
113
+ * path `vscode://file/home/alice/…`, and some tools `file://localhost/home/…`.
114
+ * The scheme is kept, the path masked.
114
115
  */
115
- const LOCAL_URL = /^((?:file|vscode(?:-insiders)?):\/\/(?:file)?)(\/[^\s"'`<>]*)$/;
116
+ const LOCAL_URL = /^((?:file|vscode(?:-insiders)?):\/\/(?:file|localhost)?)(\/[^\s"'`<>]*)$/;
116
117
 
117
118
  /**
118
119
  * `\\host\share\…`. The lookbehind keeps a drive letter out: a transcript line
@@ -127,9 +128,19 @@ const UNC = new RegExp(
127
128
  `(?<![A-Za-z0-9:~\\\\])\\\\\\\\${SEG.source}+(?:[\\\\/]+${SEG.source}+)*[\\\\/]?`,
128
129
  );
129
130
 
131
+ /**
132
+ * What ends an escape rather than a word, so a path may open right after it:
133
+ * `\n`, `\u001b`, `\x22`, and a terminal's colour code (`ESC[2m`), raw or
134
+ * escaped — test runners colour the paths they print.
135
+ */
136
+ const ESCAPE_END = String.raw`(?:\\[A-Za-z0-9]|\\u[0-9A-Fa-f]{4}|\\x[0-9A-Fa-f]{2}|(?:\x1b|\\u001b|\\x1b|\\033|\\e)\[[0-9;]*m)`;
137
+
138
+ /** A letter or digit right before a path continues a word, unless it ends an escape. */
139
+ const NOT_A_WORD = `(?<![A-Za-z0-9](?<!${ESCAPE_END}))`;
140
+
130
141
  /** Separators repeat for the same reason: JSON doubles every backslash. */
131
142
  const WINDOWS = new RegExp(
132
- `(?<![A-Za-z0-9])[A-Za-z]:[\\\\/]+(?:${SEG.source}+[\\\\/]+)*${SEG.source}*`,
143
+ `${NOT_A_WORD}[A-Za-z]:[\\\\/]+(?:${SEG.source}+[\\\\/]+)*${SEG.source}*`,
133
144
  );
134
145
 
135
146
  /**
@@ -140,14 +151,17 @@ const WINDOWS = new RegExp(
140
151
  * `~` stays, and that is all idempotence needs: masking twice is masking once,
141
152
  * because `./dist/b.js` and `100%/x` are under no root and come back unchanged.
142
153
  *
143
- * The inner lookbehind is the one exception to the letter rule: a letter that
144
- * is itself behind a backslash ends an escape sequence rather than a word. Tool
145
- * output reaches a transcript JSON-encoded twice often enough for that to
146
- * matter — Prisma reports `invocation in\\n/home/alice/…`, and read as a word
147
- * `in\\n` carried the account name through untouched.
154
+ * The one exception to the letter rule is a letter that ends an escape rather
155
+ * than a word (`ESCAPE_END`). Tool output reaches a transcript JSON-encoded
156
+ * twice often enough for that to matter — Prisma reports
157
+ * `invocation in\\n/home/alice/…`, and read as a word `in\\n` carried the
158
+ * account name through untouched.
159
+ *
160
+ * A slash may come escaped, `\/home\/alice\/…`, as PHP and some loggers write
161
+ * JSON; that is still one path.
148
162
  */
149
163
  const POSIX = new RegExp(
150
- `(?<!~)(?<!(?<!\\\\)[A-Za-z0-9])\\/(?:${SEG.source}+\\/)*${SEG.source}+\\/?`,
164
+ `(?<!~)${NOT_A_WORD}\\\\?\\/(?:${SEG.source}+\\\\?\\/)*${SEG.source}+(?:\\\\?\\/)?`,
151
165
  );
152
166
 
153
167
  /**
@@ -180,7 +194,7 @@ const TRAILING = /[.,;:!?]+$/;
180
194
  * slug behind a newline is as much the account name as one behind a slash.
181
195
  */
182
196
  const PROJECT_SLUG =
183
- /(?<![A-Za-z0-9._-])-(?:home|root|users|[a-z]-users)-[A-Za-z0-9._-]*-[A-Za-z0-9._-]+(?![A-Za-z0-9._-])/gi;
197
+ /(?<![A-Za-z0-9._-])-(?:home|root|users|(?:mnt-|cygdrive-)?[a-z]-users)-[A-Za-z0-9._-]*-[A-Za-z0-9._-]+(?![A-Za-z0-9._-])/gi;
184
198
 
185
199
  /**
186
200
  * `-home-alice`: the slug of a session that ran in the home directory itself.
@@ -191,7 +205,7 @@ const PROJECT_SLUG =
191
205
  * known nothing here fires at all.
192
206
  */
193
207
  const HOME_SLUG =
194
- /(?<![A-Za-z0-9._-])-(?:home|root|users|[a-z]-users)-[A-Za-z0-9._]+(?![A-Za-z0-9._-])/gi;
208
+ /(?<![A-Za-z0-9._-])-(?:home|root|users|(?:mnt-|cygdrive-)?[a-z]-users)-[A-Za-z0-9._]+(?![A-Za-z0-9._-])/gi;
195
209
 
196
210
  function maskSlugs(value: string, home: readonly string[] | undefined): string {
197
211
  const masked = value.replace(PROJECT_SLUG, MASK_PROJECT);
@@ -221,7 +235,8 @@ const NOT_AN_ACCOUNT: ReadonlySet<string> = new Set(["shared", "public", "defaul
221
235
  * Which segment of a path would be the account name, or -1 when the path is
222
236
  * not shaped like a home at all. `/home/alice/…` and `/Users/alice/…` name it
223
237
  * second; `C:\Users\alice\…` and the Git Bash spelling `/c/Users/alice/…`
224
- * name it third.
238
+ * name it third, and WSL's `/mnt/c/Users/alice/…` and Cygwin's
239
+ * `/cygdrive/c/Users/alice/…` fourth.
225
240
  *
226
241
  * The spellings are exact because the near misses are ordinary text:
227
242
  * `/users/42/profile` is a URL and `/root/app` is a `WORKDIR`, and neither
@@ -231,7 +246,8 @@ const NOT_AN_ACCOUNT: ReadonlySet<string> = new Set(["shared", "public", "defaul
231
246
  function accountAt(parts: readonly string[]): number {
232
247
  const head = parts[0] ?? "";
233
248
  if (head === "home" || head === "Users") return 1;
234
- if (/^[a-z]:?$/i.test(head) && (parts[1] ?? "").toLowerCase() === "users") return 2;
249
+ const drive = head === "mnt" || head === "cygdrive" ? 1 : 0;
250
+ if (/^[a-z]:?$/i.test(parts[drive] ?? "") && (parts[drive + 1] ?? "").toLowerCase() === "users") return drive + 2;
235
251
  return -1;
236
252
  }
237
253
 
@@ -259,8 +275,9 @@ function genericHome(parts: readonly string[]): string[] | undefined {
259
275
  return parts.slice(account + 1);
260
276
  }
261
277
 
262
- /** `\` when the path was written the Windows way, so a `~` path keeps its shape. */
278
+ /** The separator the path was written with, so a `~` path keeps its shape. */
263
279
  function joiner(value: string): string {
280
+ if (value.includes("\\/")) return "\\/";
264
281
  return value.includes("\\") ? "\\" : "/";
265
282
  }
266
283
 
package/src/parser.ts CHANGED
@@ -16,9 +16,10 @@ export const LOOKUP_TOOLS = new Set(["Read", "Grep", "Glob"]);
16
16
  /**
17
17
  * Every record type Claude Code writes to a session file: the table its 2.1.285
18
18
  * session writer routes each append by, plus `progress`, which older builds
19
- * wrote inline. A type outside it is kept and counted in `skipped.unknownTypes`,
20
- * which is what `isy status` reports as records it could not place — so a type
21
- * missing here reads to the user as a transcript this client half-understood.
19
+ * wrote inline, and `tools-used`, which 2.1.304 and later add. A type outside
20
+ * it is kept and counted in `skipped.unknownTypes`, which is what `isy status`
21
+ * reports as records it could not place — so a type missing here reads to the
22
+ * user as a transcript this client half-understood.
22
23
  */
23
24
  const KNOWN_RECORD_TYPES = new Set([
24
25
  "user",
@@ -61,6 +62,7 @@ const KNOWN_RECORD_TYPES = new Set([
61
62
  "api-request-blob",
62
63
  "fork-context-ref",
63
64
  "observer-ref",
65
+ "tools-used",
64
66
  ]);
65
67
 
66
68
  interface ParseState {
@@ -794,13 +796,20 @@ function finish(state: ParseState): ParsedSession {
794
796
 
795
797
  for (const record of state.records) {
796
798
  sessionId ??= asString(record.sessionId);
797
- cwd = asString(record.cwd) ?? cwd;
798
- gitBranch = asString(record.gitBranch) ?? gitBranch;
799
- claudeVersion = asString(record.version) ?? claudeVersion;
799
+ // A subagent can work in a directory of its own and outlast the session, and
800
+ // one whose call is nowhere follows all of it (`subagents.ts`): where and on
801
+ // what the session ran comes off its own records, and off a subagent's only
802
+ // when it has none — a subagent's file read alone.
803
+ const own = record.isSidechain !== true;
804
+ if (own || cwd === undefined) cwd = asString(record.cwd) ?? cwd;
805
+ if (own || gitBranch === undefined) gitBranch = asString(record.gitBranch) ?? gitBranch;
806
+ if (own || claudeVersion === undefined) claudeVersion = asString(record.version) ?? claudeVersion;
800
807
  const timestamp = asString(record.timestamp);
801
808
  if (timestamp) {
802
809
  startedAt ??= timestamp;
803
- endedAt = timestamp;
810
+ // The latest, not the last: a subagent that follows the session can have
811
+ // ended before it.
812
+ if (endedAt === undefined || timestamp > endedAt) endedAt = timestamp;
804
813
  }
805
814
  if (record.type === "assistant") assistantRecords += 1;
806
815
  if (record.isSidechain === true) sidechainRecords += 1;
package/src/plan.ts CHANGED
@@ -46,9 +46,12 @@ function resetDay(iso: string | undefined): string | undefined {
46
46
  /**
47
47
  * The line, or nothing while there is still room. Credits count as room: they
48
48
  * are what the gate spends once the allowance is gone (`billing.ts:checkQuota`).
49
+ * So does a cycle that has rolled over since: the snapshot is only as new as
50
+ * the last upload, which can be days old.
49
51
  */
50
- export function planNotice(plan: PlanStatus | undefined): string | undefined {
52
+ export function planNotice(plan: PlanStatus | undefined, now = Date.now()): string | undefined {
51
53
  if (!plan || plan.analysesLeft > 0 || plan.credits > 0) return undefined;
54
+ if (plan.resetsAt && Date.parse(plan.resetsAt) <= now) return undefined;
52
55
  const day = resetDay(plan.resetsAt);
53
56
  return `plan limit reached on ${plan.tier} — pull requests are not analysed${
54
57
  day ? ` until ${day}` : ""
package/src/queue.ts CHANGED
@@ -3,7 +3,14 @@ import { join } from "node:path";
3
3
  import { queueDir } from "./paths.js";
4
4
  import type { UploadPayload } from "./api.js";
5
5
 
6
- export const MAX_ATTEMPTS = 3;
6
+ /**
7
+ * Attempts are spaced rather than made once per run: the wait before the next
8
+ * one doubles from ten minutes, so the last comes nearly two days after the
9
+ * session was queued. Runs come minutes apart — a session's start and end,
10
+ * every commit — so counted per run, one short outage would use them all up.
11
+ */
12
+ export const MAX_ATTEMPTS = 8;
13
+ const RETRY_AFTER_MS = 10 * 60_000;
7
14
  const FAILED_SUFFIX = ".failed";
8
15
 
9
16
  export interface QueueEntry {
@@ -11,6 +18,8 @@ export interface QueueEntry {
11
18
  contentHash: string;
12
19
  attempts: number;
13
20
  queuedAt: string;
21
+ /** When the last attempt failed. isy 1.0 did not record it; `queuedAt` stands in. */
22
+ lastAttemptAt?: string;
14
23
  lastError?: string;
15
24
  payload: UploadPayload;
16
25
  }
@@ -42,12 +51,17 @@ export async function enqueue(payload: UploadPayload, lastError?: string): Promi
42
51
 
43
52
  const path = join(dir, entryName(payload.sessionId, payload.contentHash));
44
53
  const existing = await readEntry(path);
54
+ const now = new Date().toISOString();
45
55
 
46
56
  const entry: QueueEntry = {
47
57
  sessionId: payload.sessionId,
48
58
  contentHash: payload.contentHash,
49
59
  attempts: existing ? existing.attempts : 0,
50
- queuedAt: existing ? existing.queuedAt : new Date().toISOString(),
60
+ queuedAt: existing ? existing.queuedAt : now,
61
+ // The send that failed starts the wait, or the drain closing the same run
62
+ // would try again a second later. Queued again by a later failed send, an
63
+ // entry keeps both this and `attempts`: the queue keeps its own schedule.
64
+ lastAttemptAt: existing ? existing.lastAttemptAt : now,
51
65
  lastError,
52
66
  payload,
53
67
  };
@@ -93,6 +107,12 @@ export async function listPending(): Promise<QueueItem[]> {
93
107
  return listBySuffix(".json");
94
108
  }
95
109
 
110
+ /** Whether a queued upload has waited out the delay since it last failed. */
111
+ export function isDue(entry: QueueEntry, now = Date.now()): boolean {
112
+ const last = Date.parse(entry.lastAttemptAt ?? entry.queuedAt);
113
+ return Number.isNaN(last) || now - last >= RETRY_AFTER_MS * 2 ** entry.attempts;
114
+ }
115
+
96
116
  /** Uploads that ran out of attempts. Their `lastError` is the only record of why. */
97
117
  export async function listFailed(): Promise<QueueItem[]> {
98
118
  return listBySuffix(FAILED_SUFFIX);
@@ -103,7 +123,8 @@ export async function recordFailure(
103
123
  error: string,
104
124
  ): Promise<"retained" | "abandoned"> {
105
125
  const attempts = item.entry.attempts + 1;
106
- const updated: QueueEntry = { ...item.entry, attempts, lastError: error };
126
+ const lastAttemptAt = new Date().toISOString();
127
+ const updated: QueueEntry = { ...item.entry, attempts, lastAttemptAt, lastError: error };
107
128
  await writeFile(item.path, JSON.stringify(updated), { mode: 0o600 });
108
129
 
109
130
  if (attempts >= MAX_ATTEMPTS) {
@@ -113,7 +134,7 @@ export async function recordFailure(
113
134
  return "retained";
114
135
  }
115
136
 
116
- export async function drop(item: QueueItem): Promise<void> {
137
+ export async function drop(item: Pick<QueueItem, "path">): Promise<void> {
117
138
  try {
118
139
  await unlink(item.path);
119
140
  } catch {
@@ -121,6 +142,11 @@ export async function drop(item: QueueItem): Promise<void> {
121
142
  }
122
143
  }
123
144
 
145
+ /** Bytes that went up some other way: a copy waiting out its delay here is done. */
146
+ export async function forget(sessionId: string, contentHash: string): Promise<void> {
147
+ await drop({ path: join(queueDir(), entryName(sessionId, contentHash)) });
148
+ }
149
+
124
150
  export async function abandon(item: QueueItem, error: string): Promise<void> {
125
151
  const updated: QueueEntry = { ...item.entry, attempts: MAX_ATTEMPTS, lastError: error };
126
152
  await writeFile(item.path, JSON.stringify(updated), { mode: 0o600 });
package/src/redact.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { homedir } from "node:os";
2
2
  import { basename } from "node:path";
3
3
  import { fileLines } from "./lines.js";
4
+ import { commandSegments, withoutHeredocs } from "./parser.js";
4
5
  import {
5
6
  PATH_KEYS,
6
7
  REDACTION_MARKER,
@@ -22,10 +23,42 @@ export interface RedactionRule {
22
23
  replacement: string;
23
24
  }
24
25
 
26
+ /** `[pP][aA]…`: case by hand, so a rule can still tell a camelCase hump from a glued prefix. */
27
+ function anyCase(source: string): string {
28
+ return source.replace(/[a-z]/g, (letter) => `[${letter}${letter.toUpperCase()}]`);
29
+ }
30
+
31
+ const SECRET_WORD = anyCase("(?:password|passwd|secret|token|api[-_]?key|private[-_]?key|access[-_]?key)");
32
+
33
+ /**
34
+ * A name that holds a secret: one of the words, wherever it sits in an
35
+ * identifier — `PGPASSWORD`, `STRIPE_SECRET_KEY`, `aws_secret_access_key`,
36
+ * `x-api-key` — and followed by nothing but `KEY` and `BASE` (`SECRET_KEY_BASE`).
37
+ * That tail is the line between the secret and what is said about it:
38
+ * `tokenHash`, `max_tokens`, `TOKENS_PER_ANALYSIS` and `PASSWORD_FILE` turn up
39
+ * in a real corpus as often as the secrets do, and none of them is one.
40
+ */
41
+ function secretName(atWord = ""): string {
42
+ return String.raw`[A-Za-z0-9_.-]*?${atWord}${SECRET_WORD}${anyCase("(?:[-_]?key)?(?:[-_]?base)?")}(?![A-Za-z0-9_.-])`;
43
+ }
44
+
45
+ /** The word glued to what came before, but not as a camelCase hump. */
46
+ const PLAIN_NAME = secretName("(?!(?<=[a-z0-9])[A-Z])");
47
+ /** `adminPassword`, `_authToken`: as often a variable in code as a key in a file. */
48
+ const CAMEL_NAME = secretName("(?<=[a-z0-9])(?=[A-Z])");
49
+
50
+ /**
51
+ * What an assigned secret looks like: long enough to be one, and not code. A
52
+ * number never is — usage counts sit under names like these in every record,
53
+ * and stdout quotes them as JSON text.
54
+ */
55
+ const SECRET_VALUE = String.raw`(?!\[ISY_REDACTED)(?!-?\d[\d.,eE+-]*(?![^\s"'{}()[\]<>$]))[^\s"'{}()[\]<>$]{8,}`;
56
+
25
57
  export const BUILTIN_RULES: RedactionRule[] = [
26
58
  {
27
59
  type: "private_key",
28
- pattern: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY-----|$)/g,
60
+ // `BLOCK` is how PGP armors one.
61
+ pattern: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g,
29
62
  replacement: marker("private_key"),
30
63
  },
31
64
  {
@@ -64,25 +97,67 @@ export const BUILTIN_RULES: RedactionRule[] = [
64
97
  replacement: marker("google_key"),
65
98
  },
66
99
  {
100
+ // Secret and restricted keys; `pk_` is publishable by design.
101
+ type: "stripe_key",
102
+ pattern: /\b[rs]k_(?:live|test)_[A-Za-z0-9]{16,}/g,
103
+ replacement: marker("stripe_key"),
104
+ },
105
+ {
106
+ type: "slack_token",
107
+ pattern: /\bxox[abposr]-[A-Za-z0-9-]{10,}/g,
108
+ replacement: marker("slack_token"),
109
+ },
110
+ {
111
+ type: "npm_token",
112
+ pattern: /\bnpm_[A-Za-z0-9]{36}\b/g,
113
+ replacement: marker("npm_token"),
114
+ },
115
+ {
116
+ // After every shape a bearer token can have, so a JWT or a GitHub token
117
+ // keeps its own label and its marker is not marked again.
118
+ type: "bearer_token",
119
+ pattern: /\b(Bearer\s+)(?!\[ISY_REDACTED)[A-Za-z0-9._~+/-]{20,}=*/gi,
120
+ replacement: `$1${marker("bearer_token")}`,
121
+ },
122
+ {
123
+ // Any scheme: a password in a URL is one whatever speaks it — rediss,
124
+ // amqps, https to a bare IP. The scheme is capped because `\b` lets a match
125
+ // start inside a dotted or dashed run: unbounded, every start rescans the
126
+ // rest of the run, and a long one costs minutes.
67
127
  type: "connection_string",
68
- pattern: /\b(postgres|postgresql|mysql|mongodb\+srv|mongodb|redis|amqp):\/\/([^\s:/@]+):([^\s/@]+)@/g,
128
+ pattern: /\b([A-Za-z][A-Za-z0-9+.-]{0,31}):\/\/([^\s:/@]+):([^\s/@]+)@/g,
69
129
  replacement: `$1://$2:${marker("connection_string")}@`,
70
130
  },
71
131
  {
72
132
  // After the connection string: its password sits right before an `@` too.
133
+ // Starts only where a run of local-part characters starts: with `\b`, every
134
+ // dot, dash and `%` in a run began a new scan of the rest of it, and a
135
+ // version list or a URL-encoded query took minutes to get through.
73
136
  type: "email",
74
- pattern: /\b[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b/g,
137
+ pattern: /(?<![A-Za-z0-9._%+-])[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}\b/g,
75
138
  replacement: marker("email"),
76
139
  },
77
140
  {
141
+ // The name may close a quote of its own: `"password": "…"`.
78
142
  type: "assignment",
79
- pattern:
80
- /(?<![A-Za-z0-9])(password|passwd|secret|token|api[-_]?key|private[-_]?key)(\s*[=:]\s*)(["']?)(?!\[ISY_REDACTED)[^\s"'{}()[\]<>$]{8,}/gi,
143
+ pattern: new RegExp(String.raw`(?<![A-Za-z0-9_.-])(${PLAIN_NAME})(["']?\s*[=:]\s*)(["']?)${SECRET_VALUE}`, "g"),
81
144
  replacement: `$1$2$3${marker("assignment")}`,
82
145
  },
146
+ {
147
+ // A camelCase name counts where it is plainly data — a quoted key with a
148
+ // quoted value (`{"accessToken":"…"}`) or `name=value` with no space, as
149
+ // `.npmrc` writes `_authToken=` — and not in `adminPassword = someVariable`.
150
+ type: "assignment",
151
+ pattern: new RegExp(
152
+ String.raw`(?<=["'])(${CAMEL_NAME})(["']\s*:\s*["'])${SECRET_VALUE}|(?<![A-Za-z0-9_.-])(${CAMEL_NAME})(=)${SECRET_VALUE}`,
153
+ "g",
154
+ ),
155
+ replacement: `$1$2$3$4${marker("assignment")}`,
156
+ },
83
157
  ];
84
158
 
85
- const SENSITIVE_KEY = /^(password|passwd|secret|token|api[-_]?key|private[-_]?key)$/i;
159
+ /** A JSON key is data, never code, so any spelling of a secret's name counts. */
160
+ const SENSITIVE_KEY = new RegExp(`^${secretName()}$`);
86
161
  const MIN_SENSITIVE_VALUE = 8;
87
162
  const EDIT_INPUT_FIELDS = ["content", "old_string", "new_string", "new_source"];
88
163
 
@@ -151,6 +226,25 @@ function isDotenvPath(filePath: unknown): boolean {
151
226
  return typeof filePath === "string" && basename(filePath).startsWith(".env");
152
227
  }
153
228
 
229
+ /** Commands that print a file they are given, or load it into the shell. */
230
+ const SHELL_READER =
231
+ /^\s*(?:sudo\s+)?(?:cat|head|tail|less|more|bat|nl|grep|egrep|fgrep|rg|sed|awk|jq|xxd|od|diff|source|\.|git\s+(?:show|diff|log|blame)|get-content|gc|type)(?:\s|$)/i;
232
+
233
+ /**
234
+ * `cat .env`, `grep KEY config/.env.local`, `source .env`: a shell call that
235
+ * reads a dotenv file is as much a read of it as `Read {file_path}` is, and
236
+ * its output is the file. The command itself stays — it names the file, not
237
+ * what is in it.
238
+ */
239
+ function readsDotenv(command: unknown): boolean {
240
+ if (typeof command !== "string" || !command.includes(".env")) return false;
241
+ return commandSegments(withoutHeredocs(command)).some(
242
+ (segment) =>
243
+ SHELL_READER.test(segment) &&
244
+ segment.trim().split(/\s+/).slice(1).some((word) => isDotenvPath(word.replace(/["'`]/g, "").replace(/^\w*:/, ""))),
245
+ );
246
+ }
247
+
154
248
  function cutDotenvInput(input: Record<string, unknown>, context: RedactContext): void {
155
249
  for (const field of EDIT_INPUT_FIELDS) {
156
250
  if (typeof input[field] === "string") {
@@ -171,6 +265,10 @@ function scanDotenv(record: Record<string, unknown>, context: RedactContext): vo
171
265
  if (block.type === "tool_use") {
172
266
  const input = isObject(block.input) ? block.input : undefined;
173
267
  if (!input) continue;
268
+ if ((block.name === "Bash" || block.name === "PowerShell") && readsDotenv(input.command)) {
269
+ if (typeof block.id === "string") context.envToolUseIds.add(block.id);
270
+ continue;
271
+ }
174
272
  if (!isDotenvPath(input.file_path) && !isDotenvPath(input.notebook_path)) continue;
175
273
  if (typeof block.id === "string") context.envToolUseIds.add(block.id);
176
274
  cutDotenvInput(input, context);
@@ -241,12 +339,24 @@ function learnRoot(record: Record<string, unknown>, context: RedactContext): voi
241
339
  * Claude Code 2.1.30x also records the request it sends: `api-request-shape`
242
340
  * carries the system prompt and the tool definitions, `api-request-blob` the
243
341
  * context blocks sent beside the conversation — memory, CLAUDE.md, the email
244
- * again. The parser reads neither, so only the fields that name them go.
342
+ * again — and `api-request` the parameters, whose `safeguards` hand a
343
+ * classifier the account's user name, home directory and permission rules.
344
+ * `bridge-session` and `artifact-autoreact-ledger` are cut the same way: one
345
+ * names the account and organisation that own the session's bridge, the other
346
+ * the account. The parser reads none of them past their type, so only the
347
+ * fields that name them go.
245
348
  */
246
349
  const REQUEST_RECORD_KEYS = new Set(["type", "uuid", "parentUuid", "sessionId", "timestamp", "version", "shapeHash", "hash"]);
350
+ const STRIPPED_RECORD_TYPES = new Set([
351
+ "api-request",
352
+ "api-request-shape",
353
+ "api-request-blob",
354
+ "bridge-session",
355
+ "artifact-autoreact-ledger",
356
+ ]);
247
357
 
248
358
  function stripAttachment(record: Record<string, unknown>, context: RedactContext): void {
249
- if (record.type === "api-request-shape" || record.type === "api-request-blob") {
359
+ if (typeof record.type === "string" && STRIPPED_RECORD_TYPES.has(record.type)) {
250
360
  for (const key of Object.keys(record)) if (!REQUEST_RECORD_KEYS.has(key)) delete record[key];
251
361
  count(context, "attachment", 1);
252
362
  return;
@@ -259,6 +369,47 @@ function stripAttachment(record: Record<string, unknown>, context: RedactContext
259
369
  count(context, "attachment", 1);
260
370
  }
261
371
 
372
+ /**
373
+ * Harness bookkeeping Claude Code writes beside a turn, read by nothing here or
374
+ * on the server. `wireToolInputs` repeats every call's input, out of reach of
375
+ * the dotenv cut, which edits the copy in `message` (`scanDotenv`);
376
+ * `serverClassifierRequest` and `serverClassifierContext` are what a permission
377
+ * classifier was shown, the working tree's git state included;
378
+ * `wireIngestContext` and `permissionDecision` say where a call ran and what
379
+ * allowed it.
380
+ */
381
+ const HARNESS_FIELDS = [
382
+ "wireToolInputs",
383
+ "serverClassifierRequest",
384
+ "serverClassifierContext",
385
+ "wireIngestContext",
386
+ "permissionDecision",
387
+ ];
388
+
389
+ function stripHarnessFields(record: Record<string, unknown>, context: RedactContext): void {
390
+ let stripped = false;
391
+ for (const field of HARNESS_FIELDS) {
392
+ if (!(field in record)) continue;
393
+ delete record[field];
394
+ stripped = true;
395
+ }
396
+ if (stripped) count(context, "attachment", 1);
397
+ }
398
+
399
+ /**
400
+ * A user record marked `isMeta` is the harness talking in the user's place: a
401
+ * skill's body, a `<system-reminder>`, a command's caveat, an extension's
402
+ * instructions, a peer agent's message. Read as text it is a turn the
403
+ * developer never typed, holding files the developer never sent, so only a
404
+ * marker goes. None carries a tool result, and the record stays where it is:
405
+ * the next one names it as its `parentUuid`.
406
+ */
407
+ function stripMeta(record: Record<string, unknown>, context: RedactContext): void {
408
+ if (record.type !== "user" || record.isMeta !== true || !isObject(record.message)) return;
409
+ record.message = { ...record.message, content: marker("meta") };
410
+ count(context, "attachment", 1);
411
+ }
412
+
262
413
  function redactRecord(
263
414
  record: Record<string, unknown>,
264
415
  context: RedactContext,
@@ -266,6 +417,8 @@ function redactRecord(
266
417
  learnRoot(record, context);
267
418
  scanDotenv(record, context);
268
419
  stripAttachment(record, context);
420
+ stripHarnessFields(record, context);
421
+ stripMeta(record, context);
269
422
  return redactValue(record, context) as Record<string, unknown>;
270
423
  }
271
424
 
@@ -322,6 +475,13 @@ function redactOneLine(line: string, context: RedactContext): { line: string; un
322
475
  return { line: JSON.stringify(redactRecord(parsed, context)), unparsed: false };
323
476
  }
324
477
 
478
+ /**
479
+ * A transcript's lines, redacted. With no `cwd`, paths are rooted at the first
480
+ * record that names one; with no `home`, at the home that directory implies,
481
+ * else this machine's. Exported as `isy/redact` so the server can re-run the
482
+ * rules over a transcript an older client uploaded: there the machine is the
483
+ * server, so it passes `home: ""` and roots paths in the transcript alone.
484
+ */
325
485
  export function redactLines(
326
486
  lines: Iterable<string>,
327
487
  options: RedactOptions = {},
package/src/state.ts CHANGED
@@ -25,6 +25,12 @@ export interface CommitMark {
25
25
  export interface SessionState {
26
26
  /** contentHash of the last upload the server accepted. */
27
27
  uploadedHash?: string;
28
+ /**
29
+ * contentHash of an upload refused for what it holds — too large, or a body
30
+ * the server will not take. The same bytes would be refused again, so the
31
+ * fingerprint below is taken at the refusal too and a scan passes them over.
32
+ */
33
+ rejectedHash?: string;
28
34
  /**
29
35
  * Transcript size and mtime at that upload. Both unchanged means the bytes
30
36
  * are unchanged, which lets a scan skip reading a transcript entirely.
@@ -213,15 +219,15 @@ export function withCommit(
213
219
  }
214
220
 
215
221
  /**
216
- * Whether the transcript or its commit marks changed since the last accepted
217
- * upload. Size and mtime both matching means the bytes match, so the file need
218
- * not be read at all.
222
+ * Whether the transcript or its commit marks changed since the last upload the
223
+ * server accepted or refused for good. Size and mtime both matching means the
224
+ * bytes match, so the file need not be read at all.
219
225
  */
220
226
  export function looksUnchanged(
221
227
  state: SessionState,
222
228
  file: { sizeBytes: number; modifiedAt: Date },
223
229
  ): boolean {
224
- if (state.uploadedHash === undefined) return false;
230
+ if (state.uploadedHash === undefined && state.rejectedHash === undefined) return false;
225
231
  if (state.commits?.at(-1)?.sha !== state.uploadedCommit) return false;
226
232
  return state.sizeBytes === file.sizeBytes && state.modifiedMs === file.modifiedAt.getTime();
227
233
  }
package/src/subagents.ts CHANGED
@@ -7,10 +7,12 @@ import { fileLines } from "./lines.js";
7
7
  * Claude Code 2.1 writes every subagent to a file of its own beside the session:
8
8
  * `<session>/subagents/agent-<id>.jsonl`, with `agent-<id>.meta.json` next to it
9
9
  * naming the `Agent` call that started it (`toolUseId`). Workflow agents go one
10
- * level deeper (`subagents/workflows/<run>/`). The session file keeps only the
11
- * call and its result, so read alone it hid most of the work — in one session
12
- * 12 calls stood in the session file and 134 in its subagents — and a session
13
- * whose files were all edited by a subagent read as one that edited nothing.
10
+ * level deeper (`subagents/workflows/<run>/`), and their meta names no call: the
11
+ * `Workflow` call's result names the run (`toolUseResult.runId`). The session
12
+ * file keeps only the call and its result, so read alone it hid most of the
13
+ * work — in one session 12 calls stood in the session file and 134 in its
14
+ * subagents — and a session whose files were all edited by a subagent read as
15
+ * one that edited nothing.
14
16
  *
15
17
  * So the session is read as one transcript, each subagent's records merged in
16
18
  * as sidechain records carrying `parentToolUseID`: by time, and never ahead of
@@ -27,6 +29,8 @@ interface Subagent {
27
29
  path: string;
28
30
  /** The `Agent` call that started it, from the meta file. */
29
31
  toolUseId?: string;
32
+ /** The workflow run it worked in: its directory under `subagents/workflows/`. */
33
+ run?: string;
30
34
  }
31
35
 
32
36
  function isObject(value: unknown): value is Record<string, unknown> {
@@ -74,7 +78,9 @@ export async function subagentsOf(sessionPath: string): Promise<Subagent[]> {
74
78
  for (const path of await agentFiles(subagentsDir(sessionPath))) {
75
79
  const meta = await readMeta(path);
76
80
  const toolUseId = typeof meta?.toolUseId === "string" && meta.toolUseId.length > 0 ? meta.toolUseId : undefined;
77
- agents.push({ agentId: basename(path, ".jsonl").slice("agent-".length), path, toolUseId });
81
+ const dir = dirname(path);
82
+ const run = basename(dirname(dir)) === "workflows" ? basename(dir) : undefined;
83
+ agents.push({ agentId: basename(path, ".jsonl").slice("agent-".length), path, toolUseId, run });
78
84
  }
79
85
  return agents;
80
86
  }
@@ -156,11 +162,14 @@ function callsIn(record: Record<string, unknown> | undefined): string[] {
156
162
  }
157
163
 
158
164
  /**
159
- * The call a subagent without a meta file answered: its result in the session
160
- * names the agent (`toolUseResult.agentId`), and the result names the call.
165
+ * The call a subagent answered when its meta file names none: its result in the
166
+ * session names the agent (`toolUseResult.agentId`), or a workflow's agent by
167
+ * its run (`runId`), and the result names the call.
161
168
  */
162
- function answeredCall(record: Record<string, unknown> | undefined, agentId: string): string | undefined {
163
- if (!isObject(record?.toolUseResult) || record.toolUseResult.agentId !== agentId) return undefined;
169
+ function answeredCall(record: Record<string, unknown> | undefined, agent: Subagent): string | undefined {
170
+ if (!isObject(record?.toolUseResult)) return undefined;
171
+ const { agentId, runId } = record.toolUseResult;
172
+ if (agentId !== agent.agentId && (agent.run === undefined || runId !== agent.run)) return undefined;
164
173
  const content = isObject(record.message) ? record.message.content : undefined;
165
174
  const result = Array.isArray(content)
166
175
  ? content.find((block) => isObject(block) && block.type === "tool_result")
@@ -237,7 +246,7 @@ export async function* sessionLines(sessionPath: string): AsyncGenerator<string>
237
246
  const unlinked = agents.filter((agent) => agent.toolUseId === undefined);
238
247
  const placed = new Set<string>();
239
248
 
240
- /** The subagents a record starts: by the call it makes, or by the result that names one with no meta file. */
249
+ /** The subagents a record starts: by the call it makes, or by the result that names one whose meta file names no call. */
241
250
  const startedBy = (record: Record<string, unknown> | undefined): { agent: Subagent; call: string }[] => {
242
251
  const started: { agent: Subagent; call: string }[] = [];
243
252
  for (const call of callsIn(record)) {
@@ -245,7 +254,7 @@ export async function* sessionLines(sessionPath: string): AsyncGenerator<string>
245
254
  }
246
255
  for (const agent of unlinked) {
247
256
  if (placed.has(agent.agentId)) continue;
248
- const call = answeredCall(record, agent.agentId);
257
+ const call = answeredCall(record, agent);
249
258
  if (call !== undefined) started.push({ agent, call });
250
259
  }
251
260
  return started;