@oh-my-pi/pi-coding-agent 17.0.9 → 17.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.
Files changed (285) hide show
  1. package/CHANGELOG.md +54 -0
  2. package/README.md +1 -1
  3. package/dist/cli.js +4659 -4261
  4. package/dist/types/async/job-manager.d.ts +37 -1
  5. package/dist/types/cli/args.d.ts +2 -0
  6. package/dist/types/cli/bench-cli.d.ts +41 -0
  7. package/dist/types/commands/bench.d.ts +15 -3
  8. package/dist/types/commands/launch.d.ts +4 -0
  9. package/dist/types/config/model-registry.d.ts +3 -1
  10. package/dist/types/config/models-config-schema.d.ts +18 -6
  11. package/dist/types/config/models-config.d.ts +14 -5
  12. package/dist/types/config/provider-globals.d.ts +2 -2
  13. package/dist/types/config/service-tier.d.ts +5 -1
  14. package/dist/types/config/settings-schema.d.ts +316 -47
  15. package/dist/types/config/settings.d.ts +7 -0
  16. package/dist/types/dap/client.d.ts +20 -0
  17. package/dist/types/exec/bash-executor.d.ts +38 -0
  18. package/dist/types/exec/direnv.d.ts +33 -0
  19. package/dist/types/export/html/args.d.ts +15 -0
  20. package/dist/types/export/html/index.d.ts +11 -26
  21. package/dist/types/export/html/web-palette.d.ts +55 -116
  22. package/dist/types/extensibility/extensions/loader.d.ts +3 -0
  23. package/dist/types/extensibility/extensions/types.d.ts +17 -2
  24. package/dist/types/extensibility/plugins/legacy-pi-compat.d.ts +13 -0
  25. package/dist/types/live/controller.d.ts +40 -0
  26. package/dist/types/live/protocol.d.ts +85 -0
  27. package/dist/types/live/protocol.test.d.ts +1 -0
  28. package/dist/types/live/transport.d.ts +48 -0
  29. package/dist/types/live/visualizer.d.ts +33 -0
  30. package/dist/types/lsp/client.d.ts +16 -1
  31. package/dist/types/mcp/config-writer.test.d.ts +1 -0
  32. package/dist/types/mcp/oauth-discovery.d.ts +2 -0
  33. package/dist/types/mcp/smithery-auth.d.ts +1 -1
  34. package/dist/types/mcp/smithery-auth.test.d.ts +1 -0
  35. package/dist/types/memory-backend/tool-names.d.ts +2 -0
  36. package/dist/types/modes/components/login-dialog.d.ts +2 -2
  37. package/dist/types/modes/components/plan-review-overlay.d.ts +26 -0
  38. package/dist/types/modes/components/settings-defs.d.ts +7 -1
  39. package/dist/types/modes/components/status-line/component.jj-cache.test.d.ts +1 -0
  40. package/dist/types/modes/components/tool-execution.d.ts +9 -1
  41. package/dist/types/modes/controllers/command-controller.d.ts +1 -1
  42. package/dist/types/modes/controllers/event-controller.d.ts +6 -1
  43. package/dist/types/modes/controllers/live-command-controller.d.ts +14 -0
  44. package/dist/types/modes/interactive-mode.d.ts +5 -0
  45. package/dist/types/modes/theme/theme.d.ts +1 -1
  46. package/dist/types/modes/types.d.ts +3 -1
  47. package/dist/types/modes/utils/context-usage.d.ts +12 -2
  48. package/dist/types/plan-mode/plan-files.d.ts +10 -0
  49. package/dist/types/registry/agent-lifecycle.d.ts +21 -9
  50. package/dist/types/registry/agent-registry.d.ts +11 -4
  51. package/dist/types/sdk.d.ts +15 -1
  52. package/dist/types/secrets/index.d.ts +15 -2
  53. package/dist/types/secrets/obfuscator.d.ts +113 -15
  54. package/dist/types/session/acp-permission-gate.d.ts +19 -0
  55. package/dist/types/session/agent-session-events.d.ts +84 -0
  56. package/dist/types/session/agent-session-types.d.ts +314 -0
  57. package/dist/types/session/agent-session.d.ts +118 -755
  58. package/dist/types/session/async-job-delivery.d.ts +28 -0
  59. package/dist/types/session/bash-runner.d.ts +75 -0
  60. package/dist/types/session/blob-store.d.ts +11 -1
  61. package/dist/types/session/blob-store.test.d.ts +1 -0
  62. package/dist/types/session/checkpoint-entries.d.ts +28 -0
  63. package/dist/types/session/eval-runner.d.ts +49 -0
  64. package/dist/types/session/irc-bridge.d.ts +46 -0
  65. package/dist/types/session/messages.d.ts +22 -0
  66. package/dist/types/session/model-controls.d.ts +180 -0
  67. package/dist/types/session/prewalk.d.ts +46 -0
  68. package/dist/types/session/queued-messages.d.ts +22 -0
  69. package/dist/types/session/redis-session-storage.d.ts +1 -0
  70. package/dist/types/session/retry-fallback-chains.d.ts +74 -0
  71. package/dist/types/session/role-models.d.ts +15 -0
  72. package/dist/types/session/session-advisors.d.ts +228 -0
  73. package/dist/types/session/session-entries.d.ts +8 -0
  74. package/dist/types/session/session-handoff.d.ts +70 -0
  75. package/dist/types/session/session-history-format.d.ts +6 -1
  76. package/dist/types/session/session-maintenance.d.ts +246 -0
  77. package/dist/types/session/session-manager.d.ts +41 -0
  78. package/dist/types/session/session-memory.d.ts +56 -0
  79. package/dist/types/session/session-provider-boundary.d.ts +62 -0
  80. package/dist/types/session/session-stats.d.ts +48 -0
  81. package/dist/types/session/session-tools.d.ts +143 -0
  82. package/dist/types/session/session-workspace.d.ts +32 -0
  83. package/dist/types/session/stream-guards.d.ts +44 -0
  84. package/dist/types/session/todo-tracker.d.ts +56 -0
  85. package/dist/types/session/ttsr-coordinator.d.ts +51 -0
  86. package/dist/types/session/turn-recovery.d.ts +225 -0
  87. package/dist/types/system-prompt.d.ts +2 -0
  88. package/dist/types/task/executor.d.ts +4 -1
  89. package/dist/types/task/output-manager.d.ts +3 -1
  90. package/dist/types/tools/approval.d.ts +1 -0
  91. package/dist/types/tools/browser/launch.d.ts +5 -0
  92. package/dist/types/tools/image-gen.d.ts +4 -4
  93. package/dist/types/tools/image-providers.d.ts +38 -0
  94. package/dist/types/tools/index.d.ts +5 -0
  95. package/dist/types/tools/path-utils.d.ts +8 -0
  96. package/dist/types/tools/todo.d.ts +10 -4
  97. package/dist/types/tools/xdev.d.ts +5 -1
  98. package/dist/types/tts/streaming-player.d.ts +14 -0
  99. package/dist/types/tts/vocalizer.d.ts +5 -0
  100. package/dist/types/utils/jj.d.ts +29 -0
  101. package/dist/types/utils/lang-from-path.d.ts +1 -0
  102. package/dist/types/utils/title-generator.d.ts +30 -0
  103. package/dist/types/vibe/runtime.d.ts +36 -8
  104. package/dist/types/web/search/index.d.ts +1 -1
  105. package/dist/types/web/search/provider.d.ts +15 -5
  106. package/dist/types/web/search/types.d.ts +94 -0
  107. package/package.json +12 -12
  108. package/scripts/generate-share-viewer.ts +4 -6
  109. package/src/advisor/__tests__/advisor.test.ts +643 -0
  110. package/src/advisor/runtime.ts +144 -43
  111. package/src/async/job-manager.ts +90 -2
  112. package/src/cli/args.ts +2 -0
  113. package/src/cli/auth-gateway-cli.ts +18 -3
  114. package/src/cli/bench-cli.ts +395 -29
  115. package/src/cli/flag-tables.ts +3 -0
  116. package/src/cli/gc-cli.ts +2 -2
  117. package/src/cli/usage-cli.ts +10 -1
  118. package/src/collab/host.ts +3 -2
  119. package/src/commands/bench.ts +18 -4
  120. package/src/commands/launch.ts +4 -0
  121. package/src/config/model-registry.ts +10 -2
  122. package/src/config/models-config-schema.ts +15 -4
  123. package/src/config/provider-globals.ts +9 -9
  124. package/src/config/service-tier.ts +26 -1
  125. package/src/config/settings-schema.ts +209 -51
  126. package/src/config/settings.ts +81 -2
  127. package/src/dap/client.ts +37 -4
  128. package/src/edit/index.ts +49 -12
  129. package/src/eval/py/executor.ts +103 -33
  130. package/src/exec/bash-executor.ts +89 -6
  131. package/src/exec/direnv.ts +145 -0
  132. package/src/export/html/args.ts +20 -0
  133. package/src/export/html/index.ts +45 -47
  134. package/src/export/html/template.css +19 -1
  135. package/src/export/html/template.html +6 -0
  136. package/src/export/html/template.js +21 -0
  137. package/src/export/html/tool-views.generated.js +31 -31
  138. package/src/export/html/web-palette.ts +116 -132
  139. package/src/export/share.ts +249 -49
  140. package/src/extensibility/extensions/loader.ts +29 -1
  141. package/src/extensibility/extensions/runner.ts +6 -0
  142. package/src/extensibility/extensions/types.ts +33 -2
  143. package/src/extensibility/plugins/legacy-pi-compat.ts +19 -1
  144. package/src/hindsight/state.ts +64 -6
  145. package/src/internal-urls/local-protocol.ts +2 -1
  146. package/src/internal-urls/memory-protocol.ts +2 -2
  147. package/src/internal-urls/skill-protocol.ts +2 -2
  148. package/src/internal-urls/ssh-protocol.ts +2 -1
  149. package/src/internal-urls/vault-protocol.ts +2 -1
  150. package/src/live/audio-worklet.txt +59 -0
  151. package/src/live/browser-runtime.txt +221 -0
  152. package/src/live/controller.ts +464 -0
  153. package/src/live/prompts/agent-final-message.md +3 -0
  154. package/src/live/prompts/live-instructions.md +23 -0
  155. package/src/live/protocol.test.ts +140 -0
  156. package/src/live/protocol.ts +233 -0
  157. package/src/live/transport.ts +490 -0
  158. package/src/live/visualizer.ts +234 -0
  159. package/src/lsp/client.ts +29 -9
  160. package/src/lsp/index.ts +19 -4
  161. package/src/main.ts +8 -1
  162. package/src/mcp/config-writer.test.ts +43 -0
  163. package/src/mcp/config-writer.ts +109 -82
  164. package/src/mcp/oauth-discovery.ts +10 -2
  165. package/src/mcp/smithery-auth.test.ts +29 -0
  166. package/src/mcp/smithery-auth.ts +3 -2
  167. package/src/memories/index.ts +40 -20
  168. package/src/memory-backend/tool-names.ts +2 -0
  169. package/src/mnemopi/backend.ts +24 -7
  170. package/src/modes/acp/acp-agent.ts +56 -10
  171. package/src/modes/acp/acp-event-mapper.ts +8 -1
  172. package/src/modes/components/agent-hub.ts +1 -1
  173. package/src/modes/components/login-dialog.ts +14 -4
  174. package/src/modes/components/plan-review-overlay.ts +350 -35
  175. package/src/modes/components/settings-defs.ts +23 -1
  176. package/src/modes/components/settings-selector.ts +169 -1
  177. package/src/modes/components/status-line/component.jj-cache.test.ts +229 -0
  178. package/src/modes/components/status-line/component.ts +123 -2
  179. package/src/modes/components/tool-execution.ts +11 -2
  180. package/src/modes/controllers/command-controller.ts +56 -15
  181. package/src/modes/controllers/event-controller.ts +111 -16
  182. package/src/modes/controllers/extension-ui-controller.test.ts +1 -0
  183. package/src/modes/controllers/extension-ui-controller.ts +20 -4
  184. package/src/modes/controllers/live-command-controller.ts +178 -0
  185. package/src/modes/controllers/mcp-command-controller.ts +10 -1
  186. package/src/modes/controllers/omfg-controller.ts +44 -40
  187. package/src/modes/controllers/selector-controller.ts +18 -10
  188. package/src/modes/controllers/session-focus-controller.ts +6 -1
  189. package/src/modes/interactive-mode.ts +157 -38
  190. package/src/modes/runtime-init.ts +2 -0
  191. package/src/modes/setup-wizard/scenes/web-search.ts +17 -9
  192. package/src/modes/theme/theme.ts +1 -1
  193. package/src/modes/types.ts +3 -1
  194. package/src/modes/utils/context-usage.ts +23 -7
  195. package/src/modes/utils/ui-helpers.ts +10 -5
  196. package/src/plan-mode/plan-files.ts +40 -0
  197. package/src/prompts/bench/cache-prefix-chunk.md +1 -0
  198. package/src/prompts/bench/cache-prefix.md +3 -0
  199. package/src/prompts/bench/cache-suffix.md +1 -0
  200. package/src/prompts/system/custom-system-prompt.md +1 -1
  201. package/src/prompts/system/project-prompt.md +8 -1
  202. package/src/prompts/system/subagent-async-pending.md +6 -0
  203. package/src/prompts/system/system-prompt.md +1 -1
  204. package/src/prompts/system/workflow-notice.md +48 -40
  205. package/src/prompts/system/xdev-mount-notice.md +4 -0
  206. package/src/prompts/tools/todo.md +3 -1
  207. package/src/registry/agent-lifecycle.ts +116 -46
  208. package/src/registry/agent-registry.ts +35 -8
  209. package/src/registry/persisted-agents.ts +26 -2
  210. package/src/sdk.ts +210 -136
  211. package/src/secrets/index.ts +127 -4
  212. package/src/secrets/obfuscator.ts +2328 -105
  213. package/src/session/acp-permission-gate.ts +165 -0
  214. package/src/session/agent-session-error-log.test.ts +1 -1
  215. package/src/session/agent-session-events.ts +66 -0
  216. package/src/session/agent-session-types.ts +334 -0
  217. package/src/session/agent-session.ts +1892 -12226
  218. package/src/session/artifacts.ts +6 -5
  219. package/src/session/async-job-delivery.ts +74 -0
  220. package/src/session/bash-runner.ts +326 -0
  221. package/src/session/blob-store.test.ts +56 -0
  222. package/src/session/blob-store.ts +18 -2
  223. package/src/session/checkpoint-entries.ts +81 -0
  224. package/src/session/eval-runner.ts +212 -0
  225. package/src/session/indexed-session-storage.ts +9 -2
  226. package/src/session/irc-bridge.ts +203 -0
  227. package/src/session/messages.ts +263 -1
  228. package/src/session/model-controls.ts +714 -0
  229. package/src/session/prewalk.ts +252 -0
  230. package/src/session/queued-messages.ts +93 -0
  231. package/src/session/redis-session-storage.ts +51 -11
  232. package/src/session/retry-fallback-chains.ts +455 -0
  233. package/src/session/role-models.ts +85 -0
  234. package/src/session/session-advisors.ts +1679 -0
  235. package/src/session/session-context.ts +6 -3
  236. package/src/session/session-entries.ts +8 -0
  237. package/src/session/session-handoff.ts +308 -0
  238. package/src/session/session-history-format.ts +20 -9
  239. package/src/session/session-loader.ts +0 -46
  240. package/src/session/session-maintenance.ts +2983 -0
  241. package/src/session/session-manager.ts +390 -30
  242. package/src/session/session-memory.ts +222 -0
  243. package/src/session/session-provider-boundary.ts +307 -0
  244. package/src/session/session-stats.ts +293 -0
  245. package/src/session/session-tools.ts +943 -0
  246. package/src/session/session-workspace.ts +53 -0
  247. package/src/session/stream-guards.ts +417 -0
  248. package/src/session/todo-tracker.ts +380 -0
  249. package/src/session/ttsr-coordinator.ts +496 -0
  250. package/src/session/turn-recovery.ts +1629 -0
  251. package/src/slash-commands/builtin-registry.ts +92 -11
  252. package/src/slash-commands/helpers/usage-report.ts +22 -3
  253. package/src/system-prompt.ts +41 -17
  254. package/src/task/executor.ts +285 -70
  255. package/src/task/index.ts +98 -105
  256. package/src/task/output-manager.ts +25 -3
  257. package/src/task/persisted-revive.ts +4 -3
  258. package/src/task/structured-subagent.ts +1 -0
  259. package/src/tools/approval.ts +55 -10
  260. package/src/tools/bash-interactive.ts +26 -0
  261. package/src/tools/bash-skill-urls.ts +28 -2
  262. package/src/tools/bash.ts +339 -123
  263. package/src/tools/browser/launch.ts +9 -1
  264. package/src/tools/browser/tab-supervisor.ts +8 -5
  265. package/src/tools/browser.ts +94 -47
  266. package/src/tools/image-gen.ts +20 -28
  267. package/src/tools/image-providers.ts +50 -0
  268. package/src/tools/index.ts +9 -0
  269. package/src/tools/path-utils.ts +49 -2
  270. package/src/tools/read.ts +156 -88
  271. package/src/tools/renderers.ts +7 -1
  272. package/src/tools/todo.ts +101 -11
  273. package/src/tools/vibe.ts +1 -2
  274. package/src/tools/write.ts +51 -1
  275. package/src/tools/xdev.ts +67 -3
  276. package/src/tts/streaming-player.ts +62 -4
  277. package/src/tts/vocalizer.ts +18 -1
  278. package/src/utils/jj.ts +125 -4
  279. package/src/utils/lang-from-path.ts +7 -0
  280. package/src/utils/title-generator.ts +137 -1
  281. package/src/vibe/runtime.ts +852 -72
  282. package/src/web/search/index.ts +3 -6
  283. package/src/web/search/provider.ts +31 -16
  284. package/src/web/search/providers/xai.ts +7 -1
  285. package/src/web/search/types.ts +3 -0
@@ -1,3 +1,4 @@
1
+ import * as crypto from "node:crypto";
1
2
  import type { AgentMessage } from "@oh-my-pi/pi-agent-core";
2
3
  import type { AssistantMessage, Context, ImageContent, Message, TextContent } from "@oh-my-pi/pi-ai";
3
4
  import type { SessionContext } from "../session/session-context";
@@ -13,6 +14,7 @@ export interface SecretEntry {
13
14
  mode?: "obfuscate" | "replace";
14
15
  replacement?: string;
15
16
  flags?: string;
17
+ friendlyName?: string;
16
18
  }
17
19
 
18
20
  export type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue | undefined };
@@ -23,15 +25,25 @@ export type JsonRecord = { [key: string]: JsonValue | undefined };
23
25
  // ═══════════════════════════════════════════════════════════════════════════
24
26
 
25
27
  const REPLACEMENT_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
28
+ const NONMATCHING_REPLACEMENT_CHARS = `${REPLACEMENT_CHARS}!#$%&()*+,-./:;<=>?@[]^_{|}~`;
29
+ // Whitespace bytes used to build last-resort redactions for a default replace
30
+ // regex that matches every non-whitespace candidate (e.g. `\S{n}`). Only
31
+ // `space`/`tab` are used — never a line terminator — so a `.`-style
32
+ // match-everything regex (which matches space and tab but not `\n`) still
33
+ // exhausts to the sentinel instead of redacting to a newline run.
34
+ const WHITESPACE_REPLACEMENT_CHARS = " \t";
26
35
 
27
36
  /** Generate a deterministic same-length replacement string from a secret value. */
28
37
  function generateDeterministicReplacement(secret: string): string {
29
- // Simple hash: use Bun.hash for speed, seed from the secret bytes
38
+ if (secret.length === 0) return "";
39
+ // Prefix generated chunks with a fixed `ZZ` so re-redacting an already-emitted
40
+ // 1–2 char chunk is a fixed point (the deterministic replacement of a <=2-char
41
+ // value is itself `Z`/`ZZ`), keeping short default-replacement remainders next
42
+ // to a reversible placeholder stable across an obfuscator restart.
30
43
  const hash = BigInt(Bun.hash(secret));
31
- const chars: string[] = [];
44
+ const chars = secret.length === 1 ? ["Z"] : ["Z", "Z"];
32
45
  let h = hash;
33
- for (let i = 0; i < secret.length; i++) {
34
- // Mix the hash for each character position
46
+ for (let i = chars.length; i < secret.length; i++) {
35
47
  h = h ^ (BigInt(i + 1) * 0x9e3779b97f4a7c15n);
36
48
  const idx = Number((h < 0n ? -h : h) % BigInt(REPLACEMENT_CHARS.length));
37
49
  chars.push(REPLACEMENT_CHARS[idx]);
@@ -39,26 +51,511 @@ function generateDeterministicReplacement(secret: string): string {
39
51
  return chars.join("");
40
52
  }
41
53
 
54
+ /**
55
+ * Force a length-preserving deterministic replacement to differ from the secret
56
+ * it stands in for. `generateDeterministicReplacement` seeds its first 1–2 chars
57
+ * with the `Z`/`ZZ` sentinel, so a whole configured value that is exactly `Z` or
58
+ * `ZZ` (or an astronomically unlikely longer hash collision) would otherwise be
59
+ * emitted unchanged and ship the raw secret to the provider. Flip the first char
60
+ * to a fixed different glyph: same length, still deterministic, guaranteed != the
61
+ * secret. Only safe for a whole CONFIGURED value (a plain secret matches its own
62
+ * literal, so the perturbed output is no longer matched and stays a fixed point);
63
+ * per-chunk remainders must keep the sentinel to remain idempotent across restart.
64
+ */
65
+ function ensureDistinctReplacement(replacement: string, secret: string): string {
66
+ if (replacement.length === 0 || replacement !== secret) return replacement;
67
+ const alt = replacement[0] === REPLACEMENT_CHARS[0] ? REPLACEMENT_CHARS[1] : REPLACEMENT_CHARS[0];
68
+ return alt + replacement.slice(1);
69
+ }
70
+
71
+ // How far left of the matched span the re-match scan begins looking for a match
72
+ // that overlaps the candidate. This bounds ONLY the match-start search position,
73
+ // never the lookbehind/lookahead context: the probe below substitutes the
74
+ // candidate into the FULL text, so a regex's lookbehind/lookahead assertions
75
+ // always evaluate against complete context regardless of width. The single
76
+ // re-match this misses is one that begins more than this many bytes before the
77
+ // span and extends into it (a single match longer than the window) — that only
78
+ // churns the chosen redaction marker between candidates, never back to the raw
79
+ // matched value, so it cannot leak a secret.
80
+ const REGEX_REMATCH_BACKSCAN = 512;
81
+
82
+ interface RegexMatchContext {
83
+ /** Full text the match was found in (positions are offsets into it). */
84
+ text: string;
85
+ /** Start/end of the matched span being replaced. */
86
+ start: number;
87
+ end: number;
88
+ }
89
+
90
+ /**
91
+ * Whether `candidate`, substituted for the matched span in its surrounding text,
92
+ * is re-matched by `regex` at its own position. A replace-mode regex that depends
93
+ * on context (lookbehind/lookahead/`\b`) can match a candidate that does NOT match
94
+ * in isolation: e.g. `(?<=api=)[AZ]` never matches a bare `A`, but `api=A` does, so
95
+ * a candidate `A` chosen by an isolation test is re-redacted on the next obfuscate()
96
+ * pass and can oscillate back to the raw matched value. The probe substitutes the
97
+ * candidate into the FULL text — not a truncated window — so a wide lookbehind or
98
+ * lookahead (e.g. `(?<=A{600})`) still evaluates against the context that makes it
99
+ * match. Truncating that context dropped the assertion's reach and falsely
100
+ * accepted an oscillating, leaky candidate. The scan starts a bounded distance
101
+ * left of the span and stops once a match begins at/after the span's end (matches
102
+ * arrive in order), keeping per-candidate cost independent of total text length.
103
+ */
104
+ function regexRematchesInContext(candidate: string, regex: RegExp, ctx: RegexMatchContext): boolean {
105
+ const probe = ctx.text.slice(0, ctx.start) + candidate + ctx.text.slice(ctx.end);
106
+ const spanStart = ctx.start;
107
+ const spanEnd = spanStart + candidate.length;
108
+ regex.lastIndex = Math.max(0, spanStart - REGEX_REMATCH_BACKSCAN);
109
+ for (let m = regex.exec(probe); m !== null; m = regex.exec(probe)) {
110
+ const matchStart = m.index;
111
+ const matchEnd = m.index + m[0].length;
112
+ // Matches arrive in increasing position; once one starts at or past the
113
+ // span's end it cannot cover the candidate, and neither can any later one.
114
+ if (matchStart >= spanEnd) break;
115
+ // A match overlapping the candidate's own bytes means those bytes get
116
+ // re-redacted on a later pass — not a fixed point.
117
+ if (matchEnd > spanStart) return true;
118
+ // Zero-width matches do not advance lastIndex; step past to avoid a loop.
119
+ if (m[0].length === 0) regex.lastIndex++;
120
+ }
121
+ return false;
122
+ }
123
+
124
+ /**
125
+ * Search same-length replacements for one the regex does NOT match, so a default
126
+ * regex secret whose deterministic replacement collides with its own value (the
127
+ * `Z`/`ZZ` sentinel, or an astronomical hash collision) is still redacted to a
128
+ * STABLE nonmatching value instead of shipping the raw secret. A nonmatching
129
+ * candidate is a fixed point under re-obfuscation — the regex never re-matches it,
130
+ * so it cannot re-leak on a later pass. The search stays bounded to O(length *
131
+ * alphabet) regardless of value length: first exhaust every single-position
132
+ * substitution against a deterministic baseline (`AAAA…`, then `!AAA…`, `A!AA…`,
133
+ * …) so any regex that only needs one out-of-class byte — regardless of position —
134
+ * is found in a handful of probes rather than enumerating every combination (which
135
+ * for a 3-byte match-everything config, e.g. `[\s\S]{3}`, would otherwise run
136
+ * 90**3 = 729000 candidates through the regex on every single match, stalling
137
+ * provider requests). Candidates are enumerated deterministically over a stable
138
+ * ASCII alphabet: alphanumerics first (usually enough), then punctuation fallback
139
+ * bytes when the regex covers every alphanumeric candidate. When the regex still
140
+ * matches around a lone perturbed byte (for example `[A-Za-z0-9].*` matching the
141
+ * unperturbed tail), full-width same-byte candidates (`!!!!!`, `_____`, …) are
142
+ * tried next. When the regex covers every non-whitespace candidate (e.g. `\S{n}`),
143
+ * whitespace markers (a full space/tab run, then a single whitespace byte among
144
+ * non-whitespace filler) are tried as a last resort. A genuine match-everything
145
+ * regex (`.`/`[\s\S]`, which also matches space and tab) still exhausts this bounded
146
+ * sweep and returns undefined, letting the caller keep its own fixed-point fallback
147
+ * — bounded search can in principle miss an escape that depends jointly on
148
+ * multiple positions in a way no single-position swap reaches, but no realistic
149
+ * secret-redaction regex (character classes, literal matches, anchored/bounded
150
+ * repeats) has that shape.
151
+ */
152
+ function findNonMatchingReplacement(value: string, regex: RegExp, context: RegexMatchContext): string | undefined {
153
+ const len = value.length;
154
+ if (len === 0) return undefined;
155
+ // Exhaust every single-position substitution against the deterministic baseline
156
+ // first (covers the common case cheaply), then fall back to full-width same-byte
157
+ // candidates for a regex that only rejects a lone perturbed byte in context.
158
+ const baseline = NONMATCHING_REPLACEMENT_CHARS[0].repeat(len);
159
+ for (let position = 0; position < len; position++) {
160
+ for (const ch of NONMATCHING_REPLACEMENT_CHARS) {
161
+ const candidate = `${baseline.slice(0, position)}${ch}${baseline.slice(position + 1)}`;
162
+ if (candidate === value) continue;
163
+ if (!regexRematchesInContext(candidate, regex, context)) return candidate;
164
+ }
165
+ }
166
+ // If the regex can still match around a lone punctuation byte (for example
167
+ // `[A-Za-z0-9].*` matching the `AAAA` tail of `!AAAA`), try full-width
168
+ // same-byte fallbacks like `!!!!!`, `_____`, etc. before giving up.
169
+ for (const ch of NONMATCHING_REPLACEMENT_CHARS) {
170
+ const candidate = ch.repeat(len);
171
+ if (candidate === value) continue;
172
+ if (!regexRematchesInContext(candidate, regex, context)) return candidate;
173
+ }
174
+ return findWhitespaceFallbackReplacement(value, regex, context);
175
+ }
176
+
177
+ /**
178
+ * Last-resort fallback for a default replace regex that matches every
179
+ * non-whitespace candidate. Builds same-length whitespace markers the regex
180
+ * cannot match: first a full space/tab run (handles `\S`-class patterns), then a
181
+ * single whitespace byte among non-whitespace filler (` AAAA`, `A AAA`, …). The
182
+ * mixed marker defeats regexes that ALSO match all-space/all-tab runs, e.g.
183
+ * `(?:\S{n}| {n}|\t{n})`, because the lone whitespace byte breaks every
184
+ * fixed-length run. A genuine match-everything regex (`.`/`[\s\S]`) matches the
185
+ * filler and the whitespace alike, so this still returns undefined there, keeping
186
+ * the caller's sentinel as the sole fixed point.
187
+ */
188
+ function findWhitespaceFallbackReplacement(
189
+ value: string,
190
+ regex: RegExp,
191
+ context: RegexMatchContext,
192
+ ): string | undefined {
193
+ const len = value.length;
194
+ const filler = NONMATCHING_REPLACEMENT_CHARS[0];
195
+ for (const ws of WHITESPACE_REPLACEMENT_CHARS) {
196
+ const full = ws.repeat(len);
197
+ if (full !== value) {
198
+ if (!regexRematchesInContext(full, regex, context)) return full;
199
+ }
200
+ for (let pos = 0; pos < len; pos++) {
201
+ const candidate = `${filler.repeat(pos)}${ws}${filler.repeat(len - pos - 1)}`;
202
+ if (candidate === value) continue;
203
+ if (!regexRematchesInContext(candidate, regex, context)) return candidate;
204
+ }
205
+ }
206
+ return undefined;
207
+ }
208
+
209
+ /**
210
+ * Whether a default (no custom `replacement`) replace-mode regex can never
211
+ * safely redact a 1-2 char match: `findNonMatchingReplacement`'s bounded
212
+ * search — the same search `#generateRegexReplacement` runs at match time —
213
+ * finds no candidate the regex fails to re-match. This holds independent of
214
+ * any actual per-install key: the search already exhausts every character in
215
+ * `REPLACEMENT_CHARS` (the alphabet `buildKeyedReplacementRun` draws its
216
+ * fallback marker from) plus punctuation and whitespace, so if none of those
217
+ * escape the regex, no key-derived marker drawn from the same alphabet can
218
+ * either — the marker is guaranteed to re-match too, making every such match
219
+ * unresolvable: the fallback could only ever emit the raw matched text
220
+ * unchanged. Probed with a value (`"\0".repeat(length)`) the bounded search
221
+ * never treats as a real candidate, so the result depends only on the
222
+ * regex's own matching behavior, not on this specific probe.
223
+ */
224
+ export function regexHasUnresolvableShortMatchFallback(regex: RegExp): boolean {
225
+ return ([1, 2] as const).some(length => {
226
+ const probe = "\u0000".repeat(length);
227
+ const savedLastIndex = regex.lastIndex;
228
+ try {
229
+ return findNonMatchingReplacement(probe, regex, { text: probe, start: 0, end: length }) === undefined;
230
+ } finally {
231
+ regex.lastIndex = savedLastIndex;
232
+ }
233
+ });
234
+ }
235
+
42
236
  // ═══════════════════════════════════════════════════════════════════════════
43
237
  // Placeholder format
44
238
  // ═══════════════════════════════════════════════════════════════════════════
45
239
 
46
240
  const HASH_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
47
- const HASH_LEN = 4;
241
+ // Base length is sized for ~62 bits of entropy (64 bits of a keyed digest
242
+ // rendered as 12 base36 chars) so unrelated secrets do not collide on a shared
243
+ // base. A collision would let a persisted placeholder deobfuscate to the wrong
244
+ // secret when the configured secret set or its ordering changes across sessions.
245
+ const HASH_LEN = 12;
246
+ // Pre-friendly-name sessions persisted a 4-char, index-derived token; reproduce
247
+ // that exact legacy format so old session text still deobfuscates. The legacy
248
+ // token is keyed on the entry index, not the secret value, so it leaks nothing.
249
+ const LEGACY_HASH_LEN = 4;
250
+ const LEGACY_HASH_SEED = 0x5345_4352;
251
+ const MAX_FRIENDLY_NAME_LEN = 32;
252
+ // Plain/regex obfuscate matches shorter than this are toned down (never placed
253
+ // behind a reversible placeholder) to avoid redacting small words/fragments.
254
+ export const MIN_OBFUSCATE_SECRET_LEN = 8;
48
255
 
49
- /** Build an obfuscation placeholder for secret index N. Deterministic `#HASH#` token. */
50
- function buildPlaceholder(index: number): string {
51
- let v = Bun.hash.xxHash32(String(index), 0x5345_4352);
52
- let tag = "#";
256
+ // Per-process fallback key used when a caller does not supply a persisted
257
+ // per-install key. It is random (never shipped in source), so model-visible
258
+ // placeholders cannot be reversed by dictionary-hashing candidate secrets; it
259
+ // only forgoes cross-session token stability, which the persisted key provides.
260
+ let ephemeralPlaceholderKey: string | undefined;
261
+ function defaultPlaceholderKey(): string {
262
+ ephemeralPlaceholderKey ??= crypto.randomBytes(32).toString("base64url");
263
+ return ephemeralPlaceholderKey;
264
+ }
265
+
266
+ type PlaceholderCaseHint = "U" | "L" | "C" | "M";
267
+
268
+ /** Normalize a friendly name into the model-visible placeholder prefix. */
269
+ export function sanitizeSecretFriendlyName(name: string): string | undefined {
270
+ const sanitized = name
271
+ .replace(/[^A-Za-z0-9]/g, "")
272
+ .toUpperCase()
273
+ .slice(0, MAX_FRIENDLY_NAME_LEN);
274
+ return sanitized.length > 0 ? sanitized : undefined;
275
+ }
276
+
277
+ /**
278
+ * Normalize a secret value into the same alnum-only, uppercased shape a
279
+ * friendly-name label or placeholder prefix is sanitized into, so comparing a
280
+ * raw (possibly lowercase/punctuated) secret value against already-sanitized,
281
+ * model-visible text does not miss a case- or separator-only variant. Unlike
282
+ * `sanitizeSecretFriendlyName` this never truncates and never signals "empty"
283
+ * via `undefined` — callers already guard on `.length > 0` before comparing.
284
+ */
285
+ function sanitizeForCollisionCheck(value: string): string {
286
+ return value.replace(/[^A-Za-z0-9]/g, "").toUpperCase();
287
+ }
288
+
289
+ // A label leaks a secret either by containing the whole normalized secret or,
290
+ // once it reaches the public display cap, by being the secret's visible prefix.
291
+ // Shorter names like "TOKEN" can still be intentional generic labels.
292
+ function sanitizedLabelCollidesWithSecret(sanitizedLabel: string, sanitizedSecret: string): boolean {
293
+ if (sanitizedSecret.length === 0) return false;
294
+ if (sanitizedLabel.includes(sanitizedSecret)) return true;
295
+ return sanitizedLabel.length >= MAX_FRIENDLY_NAME_LEN && sanitizedSecret.startsWith(sanitizedLabel);
296
+ }
297
+
298
+ /**
299
+ * Whether an entry needs the persisted placeholder key: either because it can
300
+ * produce a reversible (keyed) obfuscate-mode placeholder, or because a default
301
+ * (no custom `replacement`) replace-mode regex can reach
302
+ * `#generateRegexReplacement`'s key-derived idempotent fallback marker (see
303
+ * `#generateReplacement`) when every same-length candidate re-matches a
304
+ * pathological match-everything config (e.g. `[\s\S]{8}`). That fallback depends
305
+ * on the persisted per-install key — not just length — to stay a fixed point
306
+ * across a process restart; without a persisted key, a fresh install falls back
307
+ * to a process-random key (`defaultPlaceholderKey()`), so the fallback marker
308
+ * would churn across restarts even though the algorithm itself is stable. A
309
+ * regex WITH a custom `replacement` never reaches that fallback (it always emits
310
+ * the literal configured string), and a plain replace secret's replacement is
311
+ * pure content-hash (`#generateSecretReplacement`), so neither needs the key.
312
+ * Short plain obfuscate entries are toned down (never placeheld), so they must
313
+ * NOT force key creation: otherwise a `secret-placeholder.key` file is written
314
+ * and persisted for a config that ends up with no active secrets, leaving the
315
+ * key readable via a tool and reusable for later placeholders.
316
+ */
317
+ export function secretEntryNeedsPlaceholderKey(entry: SecretEntry): boolean {
318
+ if ((entry.mode ?? "obfuscate") === "obfuscate") {
319
+ if (entry.type === "regex") return true;
320
+ return entry.content.length >= MIN_OBFUSCATE_SECRET_LEN;
321
+ }
322
+ return entry.type === "regex" && entry.replacement === undefined;
323
+ }
324
+
325
+ /**
326
+ * Whether a plain replace-mode replacement string can contribute a fragment that
327
+ * helps the replace phase reconstruct an obfuscate `content`. During obfuscate()'s
328
+ * replace phase the output is a tiling of passthrough bytes (adversary-controlled
329
+ * provider text) and whole replacement outputs; any contiguous occurrence of
330
+ * `content` in that output is covered by interior replacement tiles (each a
331
+ * substring of `content`) bordered by passthrough at the ends, where the border
332
+ * tile may be a suffix of a replacement (forming `content`'s prefix) or a prefix
333
+ * of a replacement (forming `content`'s suffix). An EMPTY replacement deletes its
334
+ * trigger entirely, joining the passthrough on both sides; with adversary-chosen
335
+ * surrounding bytes that can form any non-empty `content` across the deleted gap.
336
+ * So a replacement can help iff it is empty, is a substring of `content`,
337
+ * contains `content`, or shares such a border overlap.
338
+ */
339
+ function replacementCanFormContent(replacement: string, content: string): boolean {
340
+ if (replacement.length === 0) return content.length > 0;
341
+ if (content.includes(replacement) || replacement.includes(content)) return true;
342
+ const maxOverlap = Math.min(replacement.length, content.length);
343
+ for (let k = 1; k <= maxOverlap; k++) {
344
+ // A suffix of the replacement forms the prefix of the content (left border),
345
+ // or a prefix of the replacement forms the suffix of the content (right border).
346
+ if (content.startsWith(replacement.slice(replacement.length - k)) || content.endsWith(replacement.slice(0, k))) {
347
+ return true;
348
+ }
349
+ }
350
+ return false;
351
+ }
352
+
353
+ /**
354
+ * Whether a SET of entries needs the persisted placeholder key. `obfuscate()`
355
+ * applies plain replace-mode mappings before the plain-obfuscate pass, so a plain
356
+ * obfuscate entry only emits a reversible (keyed) placeholder when its content can
357
+ * still appear AFTER the replace phase. When no obfuscate entry can ever produce a
358
+ * placeholder, the persisted key must NOT be required/created — otherwise an
359
+ * effectively replace-only secret set still writes `secret-placeholder.key` and
360
+ * fails startup when the agent config dir is unwritable.
361
+ *
362
+ * The decision models the replace phase as the obfuscator actually runs it:
363
+ * replace mappings are content-keyed (later duplicate wins) and applied in
364
+ * descending content-length order; for a fresh probe (no prior placeholders) that
365
+ * phase is plain sequential substring replacement. A plain obfuscate entry needs
366
+ * the key when its content survives that simulated phase (direct typing) OR when
367
+ * any effective replacement can form the content via tiling — a substring,
368
+ * wholesale superstring, or prefix/suffix border that joins with surrounding
369
+ * passthrough bytes (see `replacementCanFormContent`). This covers direct
370
+ * shadowing (`SECRET -> safe`), reintroduction, duplicate ordering, transitive
371
+ * chains, and context-joined fragments uniformly. Default (omitted) replacements
372
+ * are deterministic, length-preserving, and distinct, so a same-content shadow
373
+ * with no other interacting replacement stays key-free.
374
+ * Replacement outputs are themselves rewritten by every later (shorter-content)
375
+ * replacement before the plain-obfuscate pass sees them, so a fragment that a
376
+ * subsequent replacement erases (`AA -> SEC` then `S -> X` turns every `SEC` into
377
+ * `XEC`) no longer forces the key. Surrounding bytes stay modeled as arbitrary
378
+ * passthrough, so testing the surviving fragment only drops false positives and
379
+ * never under-approximates a real key need.
380
+ */
381
+ export function secretEntriesNeedPlaceholderKey(entries: SecretEntry[]): boolean {
382
+ const replaceMap = new Map<string, string>();
383
+ for (const entry of entries) {
384
+ if (entry.type !== "plain" || (entry.mode ?? "obfuscate") !== "replace") continue;
385
+ replaceMap.set(
386
+ entry.content,
387
+ entry.replacement ?? ensureDistinctReplacement(generateDeterministicReplacement(entry.content), entry.content),
388
+ );
389
+ }
390
+ const replacePhase = [...replaceMap].sort((a, b) => b[0].length - a[0].length);
391
+ // Apply the replace phase from `start` onward. The phase runs in descending
392
+ // content-length order, so a replacement output emitted at index i is rewritten
393
+ // only by the later (shorter-content) replacements at i+1…; `start` 0 models a
394
+ // value typed directly into the input.
395
+ const applyReplacePhaseFrom = (text: string, start: number): string => {
396
+ let result = text;
397
+ for (let i = start; i < replacePhase.length; i++) {
398
+ result = result.split(replacePhase[i][0]).join(replacePhase[i][1]);
399
+ }
400
+ return result;
401
+ };
402
+ return entries.some(entry => {
403
+ if (!secretEntryNeedsPlaceholderKey(entry)) return false;
404
+ // Regex obfuscate entries match dynamically; conservatively require the key.
405
+ if (entry.type !== "plain") return true;
406
+ const content = entry.content;
407
+ if (applyReplacePhaseFrom(content, 0).includes(content)) return true;
408
+ // Test each replacement output in the form it SURVIVES the rest of the phase,
409
+ // so a fragment a later replacement erases no longer forces the key. The
410
+ // content it tiles into must also survive those later replacements: if a
411
+ // shorter-content replacement rewrites the surrounding passthrough bytes
412
+ // (e.g. `AA -> SEC` forms `SEC`+`RET12`, then `R -> X` turns the freshly
413
+ // formed `SECRET12` into `SECXET12`), the content can never reach the
414
+ // obfuscate pass, so the key is not needed. Requiring content stability only
415
+ // drops such false positives — a formation that genuinely survives is still
416
+ // caught at the replacement index that produces it.
417
+ return replacePhase.some(
418
+ ([, replacement], i) =>
419
+ applyReplacePhaseFrom(content, i + 1) === content &&
420
+ replacementCanFormContent(applyReplacePhaseFrom(replacement, i + 1), content),
421
+ );
422
+ });
423
+ }
424
+
425
+ // Derive the model-visible base from a KEYED digest of the secret. xxHash is
426
+ // fast and unkeyed, so a fixed-seed content hash of a low-entropy secret could
427
+ // be dictionaried from the transcript; HMAC-SHA256 under a private per-install
428
+ // key cannot, since the attacker lacks the key.
429
+ function buildHashBase(key: string, value: string): string {
430
+ const digest = new Bun.CryptoHasher("sha256", key).update(value).digest();
431
+ let v = 0n;
432
+ for (let i = 0; i < 8; i++) v = (v << 8n) | BigInt(digest[i]);
433
+ const radix = BigInt(HASH_CHARS.length);
434
+ let tag = "";
53
435
  for (let i = 0; i < HASH_LEN; i++) {
436
+ tag += HASH_CHARS[Number(v % radix)];
437
+ v /= radix;
438
+ }
439
+ return tag;
440
+ }
441
+
442
+ // Build a deterministic, key-derived run of REPLACEMENT_CHARS of the given
443
+ // length. Used to redact a per-chunk replace remainder to a marker that depends
444
+ // only on the per-install key and the remainder length, so a fresh obfuscator
445
+ // reproduces the identical marker (idempotent redaction across restarts) while
446
+ // the run stays unpredictable without the key (raw sentinel-shaped bytes cannot
447
+ // equal the marker, so they are still redacted rather than passed through).
448
+ function buildKeyedReplacementRun(key: string, length: number): string {
449
+ if (length <= 0) return "";
450
+ const radix = REPLACEMENT_CHARS.length;
451
+ let out = "";
452
+ for (let block = 0; out.length < length; block++) {
453
+ const digest = new Bun.CryptoHasher("sha256", key).update(`replace-chunk\0${length}\0${block}`).digest();
454
+ for (let i = 0; i < digest.length && out.length < length; i++) {
455
+ out += REPLACEMENT_CHARS[digest[i] % radix];
456
+ }
457
+ }
458
+ return out;
459
+ }
460
+
461
+ /** Build the pre-friendly-name index-derived placeholder for session resume compatibility. */
462
+ function buildLegacyPlaceholder(index: number): string {
463
+ let v = Bun.hash.xxHash32(String(index), LEGACY_HASH_SEED);
464
+ let tag = "#";
465
+ for (let i = 0; i < LEGACY_HASH_LEN; i++) {
54
466
  tag += HASH_CHARS[v % HASH_CHARS.length];
55
467
  v = Math.floor(v / HASH_CHARS.length);
56
468
  }
57
469
  return `${tag}#`;
58
470
  }
59
471
 
60
- /** Regex to match obfuscation placeholders: #HASH# */
61
- const PLACEHOLDER_RE = /#[A-Z0-9]{4}#/g;
472
+ function inferCaseHint(secret: string): PlaceholderCaseHint | undefined {
473
+ let hasCased = false;
474
+ let hasUpper = false;
475
+ let hasLower = false;
476
+ let capitalized = true;
477
+ let seenFirstCased = false;
478
+
479
+ for (let i = 0; i < secret.length; i++) {
480
+ const code = secret.charCodeAt(i);
481
+ const isUpper = code >= 65 && code <= 90;
482
+ const isLower = code >= 97 && code <= 122;
483
+ if (!isUpper && !isLower) continue;
484
+
485
+ hasCased = true;
486
+ if (isUpper) {
487
+ hasUpper = true;
488
+ if (seenFirstCased) capitalized = false;
489
+ } else {
490
+ hasLower = true;
491
+ if (!seenFirstCased) capitalized = false;
492
+ }
493
+ seenFirstCased = true;
494
+ }
495
+
496
+ if (!hasCased) return undefined;
497
+ if (hasUpper && !hasLower) return "U";
498
+ if (hasLower && !hasUpper) return "L";
499
+ if (capitalized) return "C";
500
+ return "M";
501
+ }
502
+
503
+ function buildPlaceholder(hint: PlaceholderCaseHint | undefined, base: string, friendlyName?: string): string {
504
+ const prefix = friendlyName ? `${friendlyName}_` : "";
505
+ return hint ? `#${prefix}${base}:${hint}#` : `#${prefix}${base}#`;
506
+ }
507
+
508
+ /** Regex to match #HASH#, #HASH:U#, and #FRIENDLY_HASH(:hint)# placeholders. */
509
+ const PLACEHOLDER_RE = /#(?:[A-Z0-9]+_)?[A-Z0-9]{4,}(?::[ULCM])?#/g;
510
+
511
+ function resumePlaceholderScanAfterRejectedCandidate(match: RegExpExecArray): void {
512
+ // RegExp#exec does not find overlapping matches. Restart at the rejected
513
+ // candidate's closing `#`, which can open an immediately adjacent placeholder.
514
+ PLACEHOLDER_RE.lastIndex = match.index + match[0].length - 1;
515
+ }
516
+
517
+ function placeholderWithoutFriendlyName(placeholder: string): string | undefined {
518
+ const match = /^#[A-Z0-9]+_([A-Z0-9]{4,}(?::[ULCM])?)#$/.exec(placeholder);
519
+ return match ? `#${match[1]}#` : undefined;
520
+ }
521
+
522
+ function lookupFriendlyPlaceholderAlias(
523
+ deobfuscateMap: ReadonlyMap<string, { secret: string; recursive: boolean }>,
524
+ placeholder: string,
525
+ ): { secret: string; recursive: boolean } | undefined {
526
+ const direct = deobfuscateMap.get(placeholder);
527
+ if (direct !== undefined) return direct;
528
+ const unprefixed = placeholderWithoutFriendlyName(placeholder);
529
+ return unprefixed !== undefined ? deobfuscateMap.get(unprefixed) : undefined;
530
+ }
531
+
532
+ const PENDING_PLACEHOLDER_SUFFIX_RE = /#(?:[A-Z0-9]+_)?[A-Z0-9]*(?::[ULCM]?)?$/;
533
+
534
+ // Withhold a trailing run that could be the start of a placeholder from streamed
535
+ // deltas, so a partial token is never emitted before deobfuscation can replace
536
+ // it. A lone trailing `#` is always buffered, even right after an alnum/`:`
537
+ // (e.g. `ID#`), because that `#` can open a placeholder; emitting it would
538
+ // corrupt the length-sliced live draft once the token completes. The final
539
+ // non-streamed flush re-emits any buffered tail, so nothing is lost.
540
+ export function stripPendingSecretPlaceholderSuffix(text: string): string {
541
+ const pendingPlaceholderStart = text.match(PENDING_PLACEHOLDER_SUFFIX_RE);
542
+ if (pendingPlaceholderStart?.index === undefined) return text;
543
+ return text.slice(0, pendingPlaceholderStart.index);
544
+ }
545
+
546
+ interface RegexScanSegment {
547
+ scanStart: number;
548
+ scanEnd: number;
549
+ textStart: number;
550
+ textEnd: number;
551
+ generatedPlaceholder: boolean;
552
+ recursive: boolean;
553
+ }
554
+
555
+ interface ReplaceRegexScan {
556
+ text: string;
557
+ segments: RegexScanSegment[];
558
+ }
62
559
 
63
560
  // ═══════════════════════════════════════════════════════════════════════════
64
561
  // SecretObfuscator
@@ -69,7 +566,8 @@ export class SecretObfuscator {
69
566
  #plainMappings = new Map<string, number>();
70
567
 
71
568
  /** Regex entries (patterns compiled at construction) */
72
- #regexEntries: Array<{ regex: RegExp; mode: "obfuscate" | "replace"; replacement?: string }> = [];
569
+ #regexEntries: Array<{ regex: RegExp; mode: "obfuscate" | "replace"; replacement?: string; friendlyName?: string }> =
570
+ [];
73
571
 
74
572
  /** All obfuscate-mode mappings: index → { secret, placeholder } */
75
573
  #obfuscateMappings = new Map<number, { secret: string; placeholder: string }>();
@@ -77,8 +575,43 @@ export class SecretObfuscator {
77
575
  /** Replace-mode plain mappings: secret → replacement */
78
576
  #replaceMappings = new Map<string, string>();
79
577
 
80
- /** Reverse lookup for deobfuscation: placeholder secret */
81
- #deobfuscateMap = new Map<string, string>();
578
+ /** Reverse lookup for LIVE deobfuscation (provider output, tool-call args):
579
+ * keyed placeholder secret plus recursion policy. Only placeholders this
580
+ * obfuscator generated under the per-install key (and their friendly-name-
581
+ * independent aliases) live here, so a prompt-injected model cannot synthesize
582
+ * one without the key. */
583
+ #deobfuscateMap = new Map<string, { secret: string; recursive: boolean }>();
584
+
585
+ /** Legacy index-derived aliases (unkeyed `#XRRS#`), honored ONLY when replaying
586
+ * stored session content. They are deterministic and trivially guessable, so
587
+ * accepting them on live provider/tool-call paths would let a prompt-injected
588
+ * model synthesize one to exfiltrate a secret; they exist solely so sessions
589
+ * persisted before keyed placeholders still deobfuscate on resume/display. */
590
+ #legacyDeobfuscateMap = new Map<string, { secret: string; recursive: boolean }>();
591
+
592
+ /** Exact placeholder tokens generated by this obfuscator revision (no aliases). */
593
+ #generatedPlaceholders = new Set<string>();
594
+
595
+ /** Deterministic replace chunks emitted by this obfuscator, used to keep re-obfuscation idempotent. */
596
+ #generatedReplaceChunks = new Set<string>();
597
+
598
+ /** Every configured plain-secret literal value (both obfuscate and replace
599
+ * mode), collected before any placeholder is minted. A generated placeholder
600
+ * must never equal one of these — otherwise a later-processed secret whose
601
+ * raw value happens to equal an earlier secret's placeholder would be
602
+ * indistinguishable from that placeholder on the NEXT obfuscate() pass (its
603
+ * own plain-secret redaction already ran, sorted by length, before the
604
+ * placeholder existed) and would survive verbatim in provider-visible text. */
605
+ #configuredSecretValues = new Set<string>();
606
+
607
+ /** Regex values seen in the current obfuscate input, used to keep friendly labels from exposing normalized matches that are discovered later in the same pass. */
608
+ #currentRegexSecretValues = new Set<string>();
609
+
610
+ /** Placeholder base-key (exact value for :M, case-folded otherwise) → base hash. */
611
+ #placeholderBaseByKey = new Map<string, string>();
612
+
613
+ /** Placeholder base hash → owner key, used to avoid ambiguous placeholders. */
614
+ #placeholderBaseOwners = new Map<string, string>();
82
615
 
83
616
  /** Next available index for regex match discoveries */
84
617
  #nextIndex: number;
@@ -86,39 +619,82 @@ export class SecretObfuscator {
86
619
  /** Whether any secrets were configured */
87
620
  #hasAny: boolean;
88
621
 
89
- constructor(entries: SecretEntry[]) {
622
+ /** Private per-install (or per-process) key for the keyed placeholder digest. */
623
+ readonly #key: string;
624
+
625
+ constructor(entries: SecretEntry[], key: string = defaultPlaceholderKey()) {
626
+ this.#key = key;
627
+ // The keyed-hash key makes obfuscate-mode placeholder bases un-dictionaryable,
628
+ // but it can be persisted in a user-readable file (`secret-placeholder.key`).
629
+ // A prompt-injected tool read (read/bash) could otherwise surface it to the
630
+ // provider verbatim and undo that protection, so redact the key itself from
631
+ // obfuscated (provider-visible) output as a one-way secret.
632
+ this.#replaceMappings.set(key, this.#generateSecretReplacement(key));
633
+ this.#configuredSecretValues.add(key);
634
+ // Collect every configured plain-secret literal AND compile every regex
635
+ // entry BEFORE minting any placeholder below, so a placeholder's friendly
636
+ // name (checked against both in `#createPlaceholder`) can never embed a
637
+ // LATER entry's raw value or regex coverage, regardless of entries[]
638
+ // order — same reasoning as the base-collision guard below, extended to
639
+ // the friendly-name collision guard.
640
+ for (const entry of entries) {
641
+ if (entry.type === "plain") {
642
+ this.#configuredSecretValues.add(entry.content);
643
+ continue;
644
+ }
645
+ try {
646
+ const regex = compileSecretRegex(entry.content, entry.flags);
647
+ const mode = entry.mode ?? "obfuscate";
648
+ // A default (no custom `replacement`) replace-mode regex that can
649
+ // never redact a 1-2 char match distinctly from itself (see
650
+ // `regexHasUnresolvableShortMatchFallback`) is dropped rather than
651
+ // risk a real secret round-tripping unredacted; `secrets/index.ts`
652
+ // warns loudly for the `secrets.yml`-loaded path — this is the
653
+ // silent backstop for direct construction.
654
+ if (
655
+ mode === "replace" &&
656
+ entry.replacement === undefined &&
657
+ regexHasUnresolvableShortMatchFallback(regex)
658
+ ) {
659
+ continue;
660
+ }
661
+ this.#regexEntries.push({
662
+ regex,
663
+ mode,
664
+ replacement: entry.replacement,
665
+ friendlyName: entry.friendlyName,
666
+ });
667
+ } catch {
668
+ // Invalid regex — skip silently (validation happens at load time)
669
+ }
670
+ }
90
671
  let index = 0;
91
- let hasRealSec = false;
672
+ let legacyIndex = 0;
673
+ let hasRealSec = this.#regexEntries.length > 0;
92
674
  for (const entry of entries) {
675
+ if (entry.type !== "plain") continue;
93
676
  const mode = entry.mode ?? "obfuscate";
94
-
95
- if (entry.type === "plain") {
96
- if (mode === "obfuscate") {
97
- if (entry.content.length < 8) {
98
- // Tone down short plain secret obfuscation to avoid false matches on small words like "esp"
99
- continue;
100
- }
101
- const placeholder = buildPlaceholder(index);
102
- this.#plainMappings.set(entry.content, index);
103
- this.#obfuscateMappings.set(index, { secret: entry.content, placeholder });
104
- this.#deobfuscateMap.set(placeholder, entry.content);
105
- index++;
106
- hasRealSec = true;
107
- } else {
108
- // replace mode
109
- const replacement = entry.replacement ?? generateDeterministicReplacement(entry.content);
110
- this.#replaceMappings.set(entry.content, replacement);
111
- hasRealSec = true;
677
+ if (mode === "obfuscate") {
678
+ if (entry.content.length < MIN_OBFUSCATE_SECRET_LEN) {
679
+ // Tone down short plain secret obfuscation to avoid false matches on small words like "esp".
680
+ continue;
112
681
  }
682
+ const placeholder = this.#createPlaceholder(entry.content, entry.friendlyName);
683
+ this.#legacyDeobfuscateMap.set(buildLegacyPlaceholder(legacyIndex), {
684
+ secret: entry.content,
685
+ recursive: false,
686
+ });
687
+ legacyIndex++;
688
+ this.#plainMappings.set(entry.content, index);
689
+ this.#obfuscateMappings.set(index, { secret: entry.content, placeholder });
690
+ this.#generatedPlaceholders.add(placeholder);
691
+ index++;
692
+ hasRealSec = true;
113
693
  } else {
114
- // regex type — compiled here, matches discovered during obfuscate()
115
- try {
116
- const regex = compileSecretRegex(entry.content, entry.flags);
117
- this.#regexEntries.push({ regex, mode, replacement: entry.replacement });
118
- hasRealSec = true;
119
- } catch {
120
- // Invalid regex — skip silently (validation happens at load time)
121
- }
694
+ // replace mode
695
+ const replacement = entry.replacement ?? this.#generateSecretReplacement(entry.content);
696
+ this.#replaceMappings.set(entry.content, replacement);
697
+ hasRealSec = true;
122
698
  }
123
699
  }
124
700
 
@@ -130,77 +706,1155 @@ export class SecretObfuscator {
130
706
  return this.#hasAny;
131
707
  }
132
708
 
709
+ /** Whether stored-session restoration can resolve keyed or legacy placeholders. */
710
+ hasStoredSecrets(): boolean {
711
+ return this.#hasAny || this.#legacyDeobfuscateMap.size > 0;
712
+ }
713
+
133
714
  /** Obfuscate all secrets in text. Bidirectional placeholders for obfuscate mode, one-way for replace. */
134
- obfuscate(text: string): string {
715
+ obfuscate(text: string, sharedRegexSecretValues?: ReadonlySet<string>): string {
135
716
  if (!this.#hasAny) return text;
717
+ this.#currentRegexSecretValues = this.collectRegexSecretValuesForObfuscation(text);
718
+ for (const secretValue of sharedRegexSecretValues ?? []) {
719
+ this.#currentRegexSecretValues.add(secretValue);
720
+ }
136
721
  let result = text;
137
-
722
+ // `origin` runs parallel to `result` (one tag char per result char): "I" for
723
+ // bytes carried from the INPUT (placeholders from a PRIOR obfuscate() call)
724
+ // and "F" for bytes this call freshly inserted. The SDK obfuscates messages
725
+ // in both convertToLlm and transformProviderContext, and prior-turn messages
726
+ // re-enter every turn, so obfuscate() must be a fixed point: a regex must not
727
+ // re-redact around a placeholder that arrived in the input (which would
728
+ // corrupt a prior replacement marker, e.g. REDACTED -> REDACTEDDACTED).
729
+ // Tracking by RANGE (not token value) keeps a fresh placeholder that happens
730
+ // to equal a prior one (same secret seen raw again) eligible for cross-match.
731
+ let origin = "I".repeat(text.length);
138
732
  // 1. Process replace-mode plain secrets
139
733
  for (const [secret, replacement] of [...this.#replaceMappings].sort((a, b) => b[0].length - a[0].length)) {
140
- result = replaceAll(result, secret, replacement);
734
+ ({ text: result, origin } = this.#replaceOutsidePlaceholdersTracked(result, origin, secret, replacement, "I"));
735
+ }
736
+ for (const secretValue of this.#collectRegexSecretValues(result)) {
737
+ this.#currentRegexSecretValues.add(secretValue);
141
738
  }
739
+ for (const secretValue of this.#collectRegexSecretValuesAfterRegexReplacements(result, origin)) {
740
+ this.#currentRegexSecretValues.add(secretValue);
741
+ }
742
+ for (const secretValue of sharedRegexSecretValues ?? []) {
743
+ this.#currentRegexSecretValues.add(secretValue);
744
+ }
745
+ ({ text: result, origin } = this.#stripUnsafeFriendlyPrefixes(result, origin));
142
746
 
143
747
  // 2. Process obfuscate-mode plain secrets
144
748
  for (const [secret, index] of [...this.#plainMappings].sort((a, b) => b[0].length - a[0].length)) {
145
749
  const mapping = this.#obfuscateMappings.get(index)!;
146
- result = replaceAll(result, secret, mapping.placeholder);
750
+ ({ text: result, origin } = this.#replaceOutsidePlaceholdersTracked(
751
+ result,
752
+ origin,
753
+ secret,
754
+ this.#placeholderForCurrentInput(mapping.placeholder),
755
+ "F",
756
+ ));
147
757
  }
148
758
 
149
759
  // 3. Process regex entries — discover new matches
150
760
  for (const entry of this.#regexEntries) {
151
761
  entry.regex.lastIndex = 0;
152
- const matches = new Set<string>();
153
- for (;;) {
154
- const match = entry.regex.exec(result);
155
- if (match === null) break;
156
- if (match[0].length === 0) {
157
- entry.regex.lastIndex++;
158
- continue;
159
- }
160
- matches.add(match[0]);
161
- }
762
+ const matches = this.#collectRegexMatches(result, entry.regex, entry.mode, origin, entry.replacement);
162
763
 
163
- for (const matchValue of matches) {
764
+ for (const match of matches) {
164
765
  if (entry.mode === "replace") {
165
- const replacement = entry.replacement ?? generateDeterministicReplacement(matchValue);
166
- result = replaceAll(result, matchValue, replacement);
766
+ if (match.preserveGeneratedPlaceholders) {
767
+ if (
768
+ match.preserveInputPlaceholders &&
769
+ entry.replacement === undefined &&
770
+ match.inputPlaceholderOutsideChunkCount === 1 &&
771
+ match.inputPlaceholderOutsideStart >= 0 &&
772
+ origin
773
+ .slice(
774
+ match.inputPlaceholderOutsideStart,
775
+ match.inputPlaceholderOutsideStart + match.inputPlaceholderOutside.length,
776
+ )
777
+ .includes("F") &&
778
+ this.#generatedReplaceChunks.has(match.inputPlaceholderOutside)
779
+ ) {
780
+ continue;
781
+ }
782
+ // Same greedy-spillover fixed point as the obfuscate branch below: when
783
+ // the match only reached across a prior-call placeholder because the
784
+ // placeholder's own value satisfies the regex, and the surrounding raw
785
+ // bytes do not independently match, re-redacting those bytes drifts the
786
+ // deterministic scramble across passes (e.g. `…#…#ZZJ5sotJ` →
787
+ // `…#…#ZZpvsotJ`), invalidating prompt-cache prefixes despite no new
788
+ // input. Leave the placeholder and its spillover verbatim — a fixed
789
+ // point. Structurally-required bytes (placeholder value alone cannot
790
+ // match) and independently-matching outside chunks still fall through to
791
+ // the redaction below. Only prior-call (`origin "I"`) placeholders set
792
+ // these flags, so first-pass redaction of genuinely new bytes is intact.
793
+ if (
794
+ match.inputPlaceholderInnerIndependentlyMatches &&
795
+ !match.inputPlaceholderOutsideIndependentlyMatches
796
+ ) {
797
+ continue;
798
+ }
799
+ let replaceEnd = match.end;
800
+ let span = result.slice(match.start, replaceEnd);
801
+ if (entry.replacement !== undefined) {
802
+ const trailingChunk = trailingOutsidePreservedPlaceholderChunk(span, placeholder =>
803
+ this.#isGeneratedPlaceholder(placeholder),
804
+ );
805
+ if (trailingChunk.length > 0 && entry.replacement.startsWith(trailingChunk)) {
806
+ const trailingSuffix = entry.replacement.slice(trailingChunk.length);
807
+ if (trailingSuffix.length > 0 && result.slice(replaceEnd).startsWith(trailingSuffix)) {
808
+ replaceEnd += trailingSuffix.length;
809
+ span = result.slice(match.start, replaceEnd);
810
+ }
811
+ }
812
+ }
813
+ // A custom replacement is a single redaction marker for the whole
814
+ // match, so emit it once around the preserved placeholder rather
815
+ // than per surrounding chunk (which duplicates it, e.g.
816
+ // `api_key=***#…#api_key=***`). Without one, each surrounding chunk
817
+ // gets its own length-matched fixed-point marker, checked in the
818
+ // expanded scan context so marker bytes cannot re-match beside the
819
+ // preserved placeholder on the next pass. The origin of any placeholder
820
+ // PRESERVED inside `span` must survive verbatim — blanket-tagging the
821
+ // whole redacted span "I" would relabel a same-call-fresh ("F")
822
+ // placeholder as prior-call, wrongly triggering the spillover-skip
823
+ // above for a LATER regex entry over content this call just redacted.
824
+ const spanOrigin = origin.slice(match.start, replaceEnd);
825
+ const redacted =
826
+ entry.replacement !== undefined
827
+ ? redactWithFixedReplacementOutsidePlaceholders(
828
+ span,
829
+ spanOrigin,
830
+ entry.replacement,
831
+ placeholder => this.#isGeneratedPlaceholder(placeholder),
832
+ )
833
+ : this.#redactRegexMatchOutsidePlaceholders(span, spanOrigin, entry.regex, match.scanContext);
834
+ result = replaceRange(result, match.start, replaceEnd, redacted.text);
835
+ origin = replaceRange(origin, match.start, replaceEnd, redacted.origin);
836
+ } else {
837
+ const replacement = entry.replacement ?? match.defaultReplacement;
838
+ if (replacement === undefined) {
839
+ throw new Error("regex replace match missing a generated replacement");
840
+ }
841
+ result = replaceRange(result, match.start, match.end, replacement);
842
+ origin = replaceRange(origin, match.start, match.end, "I".repeat(replacement.length));
843
+ }
167
844
  } else {
168
- if (matchValue.length < 8) {
169
- // Tone down short regex match obfuscation to avoid false matches on small words/fragments
845
+ if (match.scanMatchLength < MIN_OBFUSCATE_SECRET_LEN) {
846
+ // Tone down short regex matches to avoid obfuscating small
847
+ // words/fragments. Measure the regex's own match length in the
848
+ // canonical (placeholder-expanded) scan view, not the rewritten
849
+ // source span, so the threshold reflects how much content the regex
850
+ // actually matched.
851
+ continue;
852
+ }
853
+ if (match.preserveInputPlaceholders) {
854
+ // The match straddled a prior-call placeholder. When the placeholder's
855
+ // own value already satisfies the regex on its own AND the surrounding
856
+ // raw bytes do not independently match, those bytes are greedy spillover
857
+ // (e.g. the trailing `A` in `SECRETUV→#…#A`): obfuscating them mints
858
+ // fresh placeholders on re-obfuscation and drifts the provider-visible
859
+ // history and prompt-cache prefix. Leave the placeholder atomic and the
860
+ // spillover verbatim — a fixed point. Outside bytes are still obfuscated
861
+ // when the placeholder value alone cannot match (they are structurally
862
+ // part of the secret, e.g. an `api_key=` prefix the regex requires) or
863
+ // when they independently match the regex on their own.
864
+ if (
865
+ match.inputPlaceholderInnerIndependentlyMatches &&
866
+ !match.inputPlaceholderOutsideIndependentlyMatches
867
+ ) {
868
+ continue;
869
+ }
870
+ const span = result.slice(match.start, match.end);
871
+ const spanOrigin = origin.slice(match.start, match.end);
872
+ const obfuscated = this.#obfuscateOutsidePlaceholdersTracked(span, spanOrigin, entry.friendlyName);
873
+ result = replaceRange(result, match.start, match.end, obfuscated.text);
874
+ origin = replaceRange(origin, match.start, match.end, obfuscated.origin);
170
875
  continue;
171
876
  }
172
877
  // obfuscate mode — get or create stable index
173
- let index = this.#findObfuscateIndex(matchValue);
878
+ let index = this.#findObfuscateIndex(match.canonicalValue);
174
879
  if (index === undefined) {
175
880
  index = this.#nextIndex++;
176
- const placeholder = buildPlaceholder(index);
177
- this.#obfuscateMappings.set(index, { secret: matchValue, placeholder });
178
- this.#deobfuscateMap.set(placeholder, matchValue);
881
+ const placeholder = this.#createPlaceholder(
882
+ match.canonicalValue,
883
+ entry.friendlyName,
884
+ match.recursive,
885
+ );
886
+ this.#obfuscateMappings.set(index, { secret: match.canonicalValue, placeholder });
887
+ this.#generatedPlaceholders.add(placeholder);
179
888
  }
180
889
  const mapping = this.#obfuscateMappings.get(index)!;
181
- result = replaceAll(result, matchValue, mapping.placeholder);
890
+ const placeholder = this.#placeholderForCurrentInput(mapping.placeholder);
891
+ result = replaceRange(result, match.start, match.end, placeholder);
892
+ origin = replaceRange(origin, match.start, match.end, "F".repeat(placeholder.length));
893
+ }
894
+ }
895
+ if (entry.mode === "replace") {
896
+ for (const secretValue of this.#collectRegexSecretValues(result)) {
897
+ this.#currentRegexSecretValues.add(secretValue);
182
898
  }
183
899
  }
184
900
  }
901
+ ({ text: result, origin } = this.#stabilizeReplaceRegexPlaceholderSpillover(result, origin));
185
902
 
903
+ this.#currentRegexSecretValues = new Set();
186
904
  return result;
187
905
  }
188
906
 
189
- /** Deobfuscate obfuscate-mode placeholders back to original secrets. Replace-mode is NOT reversed. */
907
+ /**
908
+ * Deobfuscate keyed placeholders back to original secrets for LIVE paths
909
+ * (provider output, tool-call arguments). Replace-mode is NOT reversed, and
910
+ * legacy index-derived aliases are intentionally ignored so a prompt-injected
911
+ * model cannot synthesize one to recover a secret.
912
+ */
190
913
  deobfuscate(text: string): string {
191
- if (!this.#hasAny || !text.includes("#")) return text;
192
- return text.replace(PLACEHOLDER_RE, match => this.#deobfuscateMap.get(match) ?? match);
914
+ return this.#deobfuscate(text, false);
915
+ }
916
+
917
+ /**
918
+ * Deobfuscate stored session content for replay/display. Identical to
919
+ * {@link deobfuscate} but additionally honors legacy index-derived aliases so
920
+ * sessions persisted before keyed placeholders still resume correctly. Use
921
+ * only for trusted on-disk session content, never for live model output.
922
+ */
923
+ deobfuscateStored(text: string): string {
924
+ return this.#deobfuscate(text, true);
925
+ }
926
+
927
+ // Reverse-direction counterpart to `#isGeneratedPlaceholder`'s guard: the
928
+ // bare-alias fallback below intentionally accepts ANY prefix so a
929
+ // placeholder minted under a renamed friendly name still deobfuscates
930
+ // (see `#prefixIsSecretShaped`'s docstring for why), but unconditionally
931
+ // stripping and ignoring an attacker-authored prefix would let a forged
932
+ // token like `#GITHUBPATABC123_<suffix-copied-from-any-real-placeholder>#`
933
+ // restore to that OTHER secret's raw value with no check at all — worse
934
+ // than the obfuscate-direction leak, since deobfuscation is what feeds
935
+ // tool-call arguments and provider-output restoration. Refuse the
936
+ // fallback (leave the token as opaque, unresolved text) when the prefix
937
+ // is itself secret-shaped; an exact full-token match is unaffected, since
938
+ // that was minted by this instance and carries no forgery risk.
939
+ #lookupLiveAlias(placeholder: string): { secret: string; recursive: boolean } | undefined {
940
+ const direct = this.#deobfuscateMap.get(placeholder);
941
+ if (direct !== undefined) return direct;
942
+ const match = /^#([A-Z0-9]+)_([A-Z0-9]{4,}(?::[ULCM])?)#$/.exec(placeholder);
943
+ if (match === null || this.#prefixIsSecretShaped(match[1]!)) return undefined;
944
+ return this.#deobfuscateMap.get(`#${match[2]}#`);
945
+ }
946
+
947
+ #deobfuscate(text: string, allowLegacy: boolean): string {
948
+ if ((!this.#hasAny && (!allowLegacy || this.#legacyDeobfuscateMap.size === 0)) || !text.includes("#"))
949
+ return text;
950
+ let result = text;
951
+ for (;;) {
952
+ let shouldContinue = false;
953
+ const next = result.replace(PLACEHOLDER_RE, match => {
954
+ const mapped = this.#lookupLiveAlias(match);
955
+ if (mapped !== undefined) {
956
+ shouldContinue ||= mapped.recursive;
957
+ return mapped.secret;
958
+ }
959
+ if (allowLegacy) {
960
+ const legacy = this.#legacyDeobfuscateMap.get(match);
961
+ if (legacy !== undefined) {
962
+ shouldContinue ||= legacy.recursive;
963
+ return legacy.secret;
964
+ }
965
+ }
966
+ return match;
967
+ });
968
+ if (next === result || !shouldContinue || !next.includes("#")) return next;
969
+ result = next;
970
+ }
193
971
  }
972
+
973
+ /** Deep-walk an object, deobfuscating string values for LIVE paths (keyed placeholders only). */
974
+ deobfuscateObject<T>(obj: T): T {
975
+ if (!this.#hasAny) return obj;
976
+ return deepWalkStrings(obj, s => this.deobfuscate(s));
977
+ }
978
+
979
+ /** Deep-walk stored session content, deobfuscating string values incl. legacy aliases. */
980
+ deobfuscateStoredObject<T>(obj: T): T {
981
+ if (!this.hasStoredSecrets()) return obj;
982
+ return deepWalkStrings(obj, s => this.deobfuscateStored(s));
983
+ }
984
+
985
+ /** Deep-walk an object, obfuscating all string values. */
986
+ obfuscateObject<T>(obj: T): T {
987
+ if (!this.#hasAny) return obj;
988
+ return deepWalkStrings(obj, s => this.obfuscate(s));
989
+ }
990
+
991
+ #generateReplacement(chunk: string): string {
992
+ // Redact a per-chunk remainder to a marker that is a fixed point under
993
+ // re-redaction on ANY obfuscator sharing the key, so persisted obfuscated text
994
+ // — and the provider prompt-cache prefixes it anchors — never drifts across a
995
+ // restart. The marker keeps the `Z`/`ZZ` sentinel prefix (already a fixed point
996
+ // for <=2-char remainders) and, for longer remainders, a key-derived run that
997
+ // depends ONLY on the key and length. A fresh obfuscator reproduces the
998
+ // identical marker (so re-redacting it stays idempotent across restarts, where
999
+ // the content-derived chunk plus the session-local `#generatedReplaceChunks`
1000
+ // were not), yet the run is unpredictable without the per-install key, so raw
1001
+ // remainder bytes that merely look sentinel-shaped (e.g. `ZZZZ`) cannot equal
1002
+ // the marker and are still redacted instead of passed through.
1003
+ const replacement =
1004
+ chunk.length <= 2 ? "Z".repeat(chunk.length) : `ZZ${buildKeyedReplacementRun(this.#key, chunk.length - 2)}`;
1005
+ this.#generatedReplaceChunks.add(replacement);
1006
+ return replacement;
1007
+ }
1008
+
1009
+ /**
1010
+ * Replacement for a whole CONFIGURED secret value (a plain replace-mode entry
1011
+ * or the redacted key). Unlike a per-chunk remainder redaction, the output
1012
+ * must differ from the input so a value equal to the `Z`/`ZZ` sentinel is not
1013
+ * emitted verbatim. A plain secret only matches its own literal, so the
1014
+ * perturbed output stays a fixed point under re-obfuscation.
1015
+ */
1016
+ #generateSecretReplacement(secret: string): string {
1017
+ const replacement = ensureDistinctReplacement(generateDeterministicReplacement(secret), secret);
1018
+ this.#generatedReplaceChunks.add(replacement);
1019
+ return replacement;
1020
+ }
1021
+
1022
+ /**
1023
+ * Replacement for a default (no custom replacement) regex match. The output must
1024
+ * be a fixed point under re-obfuscation: a regex can re-match its own replacement,
1025
+ * and a later obfuscate() pass would then re-redact it — at best churning the
1026
+ * provider-visible text, at worst oscillating back to the raw matched value. The
1027
+ * deterministic replacement already differs from most values, but it is
1028
+ * all-alphanumeric and a regex may still match it, either directly (a `Z`/`ZZ`
1029
+ * sentinel collision, or a class like `[A-Za-z0-9]+`) or only in context
1030
+ * (lookbehind/lookahead/`\b`, e.g. `(?<=api=)[AZ]`). When it would re-match,
1031
+ * search same-length candidates IN CONTEXT for one the regex does not re-match:
1032
+ * that value is a stable fixed point and cannot re-leak. A single perturbation is
1033
+ * not enough — it may also match (e.g. `B`, not `A`, for `Z|A`) — so the search
1034
+ * tries further candidates before giving up. When no candidate avoids the regex (a
1035
+ * pathological match-everything config such as `.`/`[\s\S]`), the content-hash
1036
+ * deterministic value is NOT usable as a fallback: it will itself be re-matched on
1037
+ * the next pass, and because it is derived from the bytes being replaced, rehashing
1038
+ * those bytes (the marker itself, not the original secret) produces a DIFFERENT
1039
+ * value — churning the redaction, and the provider prompt-cache prefix it anchors,
1040
+ * across every re-obfuscation. Fall back instead to a marker that depends only
1041
+ * on `this.#key` and the value's length, not its content, so re-matching and
1042
+ * re-redacting it reproduces the IDENTICAL marker every time, which the
1043
+ * pathological case requires since no value can escape the regex at all. This
1044
+ * cannot reuse `#generateReplacement`'s own <=2-char branch directly: that
1045
+ * branch is the fixed `Z`/`ZZ` sentinel, which is itself a value this fallback
1046
+ * could be asked to replace (an input of exactly `Z` or `ZZ`), and returning it
1047
+ * unchanged would ship the raw secret to the provider — the exact failure plain
1048
+ * replace-mode secrets avoid via `ensureDistinctReplacement`. Neither
1049
+ * `ensureDistinctReplacement`'s single-char flip NOR a length-changing marker is
1050
+ * usable here: a pathological regex re-matches ANY same-length value, including
1051
+ * a flipped one, so a value-dependent flip oscillates between the two forever;
1052
+ * and a regex with no quantifier (matching one input character per match, e.g.
1053
+ * `.`) re-scans a LONGER marker as several independent same-regex matches on the
1054
+ * next pass, re-expanding each one — unbounded growth, not a fixed point. Use a
1055
+ * SAME-LENGTH keyed run instead of the sentinel for <=2 chars: content-independent
1056
+ * (so it is trivially its own fixed point once emitted) and no longer a public,
1057
+ * install-independent constant, closing the specific guessable collision
1058
+ * (`Z`/`ZZ`) the sentinel had. A same-length, content-independent marker cannot
1059
+ * mathematically rule out equaling some pathological input by construction (the
1060
+ * marker is itself a same-length string a match-everything regex also matches),
1061
+ * but that residual case now requires guessing this install's private key rather
1062
+ * than a universal constant — the same class of accepted risk
1063
+ * `generateDeterministicReplacement`'s hash collision already carries for longer
1064
+ * values.
1065
+ * A regex that is UNCONDITIONALLY pathological for a length <= 2 — matching
1066
+ * literally every candidate in isolation, independent of context, like `.`
1067
+ * or `[\s\S]` — is now rejected at construction/config-load time instead
1068
+ * (see `regexHasUnresolvableShortMatchFallback`), since for that narrow
1069
+ * case the residual risk above is fully avoidable rather than merely
1070
+ * unlikely. This branch, and the residual risk above, still applies to a
1071
+ * length > 2 pathological config and to a length <= 2 pattern that is only
1072
+ * pathological in a SPECIFIC match's surrounding context (the
1073
+ * construction-time check tests the regex in isolation, not every context
1074
+ * it could appear in).
1075
+ */
1076
+ #generateRegexReplacement(value: string, regex: RegExp, context: RegexMatchContext): string {
1077
+ let replacement = generateDeterministicReplacement(value);
1078
+ // Verify in context, not just against the sentinel collision: the
1079
+ // deterministic replacement is all-alphanumeric and a context-sensitive regex
1080
+ // (lookbehind/lookahead/`\b`) can re-match it even when it differs from the
1081
+ // value, so a later pass would re-redact and could oscillate back to the raw
1082
+ // secret. Search for a candidate the regex does not re-match in place.
1083
+ if (replacement === value || regexRematchesInContext(replacement, regex, context)) {
1084
+ const stable = findNonMatchingReplacement(value, regex, context);
1085
+ // See docstring above: same-length keyed run for <=2 chars (never the
1086
+ // `Z`/`ZZ` sentinel, which a <=2 char value could itself be), otherwise
1087
+ // the ordinary keyed-run fallback #generateReplacement already uses.
1088
+ replacement =
1089
+ stable ??
1090
+ (value.length <= 2 ? buildKeyedReplacementRun(this.#key, value.length) : this.#generateReplacement(value));
1091
+ regex.lastIndex = 0;
1092
+ }
1093
+ this.#generatedReplaceChunks.add(replacement);
1094
+ return replacement;
1095
+ }
1096
+
1097
+ #generateRegexChunkReplacement(chunk: string, regex: RegExp, context: RegexMatchContext): string {
1098
+ let replacement = this.#generateReplacement(chunk);
1099
+ if (regexRematchesInContext(replacement, regex, context)) {
1100
+ const stable = findNonMatchingReplacement(chunk, regex, context);
1101
+ if (stable !== undefined) {
1102
+ replacement = stable;
1103
+ this.#generatedReplaceChunks.add(replacement);
1104
+ }
1105
+ regex.lastIndex = 0;
1106
+ }
1107
+ return replacement;
1108
+ }
1109
+
1110
+ #redactRegexMatchOutsidePlaceholders(
1111
+ text: string,
1112
+ origin: string,
1113
+ regex: RegExp,
1114
+ context: RegexMatchContext,
1115
+ ): { text: string; origin: string } {
1116
+ let scanCursor = context.start;
1117
+ return transformOutsidePlaceholdersTracked(
1118
+ text,
1119
+ origin,
1120
+ placeholder => this.#isGeneratedPlaceholder(placeholder),
1121
+ chunk => {
1122
+ const start = scanCursor;
1123
+ scanCursor += chunk.length;
1124
+ if (chunk.length === 0) return "";
1125
+ return this.#generateRegexChunkReplacement(chunk, regex, {
1126
+ text: context.text,
1127
+ start,
1128
+ end: scanCursor,
1129
+ });
1130
+ },
1131
+ placeholder => {
1132
+ scanCursor +=
1133
+ lookupFriendlyPlaceholderAlias(this.#deobfuscateMap, placeholder)?.secret.length ?? placeholder.length;
1134
+ return placeholder;
1135
+ },
1136
+ );
1137
+ }
1138
+
1139
+ #stabilizeReplaceRegexPlaceholderSpillover(text: string, origin: string): { text: string; origin: string } {
1140
+ let result = text;
1141
+ let currentOrigin = origin;
1142
+ for (const entry of this.#regexEntries) {
1143
+ if (entry.mode !== "replace" || entry.replacement !== undefined) continue;
1144
+ entry.regex.lastIndex = 0;
1145
+ const matches = this.#collectRegexMatches(result, entry.regex, entry.mode, currentOrigin, entry.replacement);
1146
+ entry.regex.lastIndex = 0;
1147
+ for (const match of matches) {
1148
+ if (!match.preserveGeneratedPlaceholders) continue;
1149
+ if (
1150
+ match.preserveInputPlaceholders &&
1151
+ entry.replacement === undefined &&
1152
+ match.inputPlaceholderOutsideChunkCount === 1 &&
1153
+ match.inputPlaceholderOutsideStart >= 0 &&
1154
+ currentOrigin
1155
+ .slice(
1156
+ match.inputPlaceholderOutsideStart,
1157
+ match.inputPlaceholderOutsideStart + match.inputPlaceholderOutside.length,
1158
+ )
1159
+ .includes("F") &&
1160
+ this.#generatedReplaceChunks.has(match.inputPlaceholderOutside)
1161
+ ) {
1162
+ continue;
1163
+ }
1164
+ if (match.inputPlaceholderInnerIndependentlyMatches && !match.inputPlaceholderOutsideIndependentlyMatches) {
1165
+ continue;
1166
+ }
1167
+ const span = result.slice(match.start, match.end);
1168
+ const spanOrigin = currentOrigin.slice(match.start, match.end);
1169
+ const redacted =
1170
+ entry.replacement !== undefined
1171
+ ? redactWithFixedReplacementOutsidePlaceholders(span, spanOrigin, entry.replacement, placeholder =>
1172
+ this.#isGeneratedPlaceholder(placeholder),
1173
+ )
1174
+ : this.#redactRegexMatchOutsidePlaceholders(span, spanOrigin, entry.regex, match.scanContext);
1175
+ if (redacted.text === span) continue;
1176
+ result = replaceRange(result, match.start, match.end, redacted.text);
1177
+ currentOrigin = replaceRange(currentOrigin, match.start, match.end, redacted.origin);
1178
+ }
1179
+ }
1180
+ return { text: result, origin: currentOrigin };
1181
+ }
1182
+
194
1183
  /** Find the obfuscate index for a known secret value. */
195
1184
  #findObfuscateIndex(secret: string): number | undefined {
1185
+ // Check plain mappings first
196
1186
  const plainIndex = this.#plainMappings.get(secret);
197
1187
  if (plainIndex !== undefined) return plainIndex;
198
1188
 
1189
+ // Check regex-discovered mappings
199
1190
  for (const [index, mapping] of this.#obfuscateMappings) {
200
1191
  if (mapping.secret === secret) return index;
201
1192
  }
202
1193
  return undefined;
203
1194
  }
1195
+
1196
+ #createPlaceholder(secret: string, friendlyName?: string, recursive: boolean = false): string {
1197
+ const hint = inferCaseHint(secret);
1198
+ // Key the base on the EXACT secret value, never a case-folded form. The
1199
+ // case hint is only a model-visible label. If two distinct secrets that
1200
+ // differ solely by ASCII case shared one case-folded base, a provider that
1201
+ // saw one placeholder could swap the hint to synthesize the sibling
1202
+ // secret's keyed token, and live deobfuscation (provider output / tool-call
1203
+ // args) would restore a value that was never provider-visible. Exact-value
1204
+ // keying gives every secret an independent base, so a sibling token cannot
1205
+ // be derived without the per-install key.
1206
+ const baseKey = secret;
1207
+ // A friendly name that embeds a configured secret's literal (or matches a
1208
+ // configured regex pattern) would bake that secret straight into a
1209
+ // LEGITIMATE, exact-registered placeholder — later scans recognize the
1210
+ // whole token as already-generated on an EXACT match, before the
1211
+ // alias-fallback prefix check above ever runs, so the embedded secret
1212
+ // would never be scanned. Drop the label for this mint rather than risk
1213
+ // it; the secret still gets a bare (unprefixed) placeholder. The collision
1214
+ // check runs against the FULL normalized label — `sanitizeForCollisionCheck`,
1215
+ // not yet capped at `MAX_FRIENDLY_NAME_LEN` — so a secret longer than the
1216
+ // 32-char display cap (or one whose sanitized form exceeds it) still gets
1217
+ // caught: a truncated `requestedFriendlyName` can never contain a longer
1218
+ // secret's full sanitized form, so checking the truncated label would let
1219
+ // the secret's first 32 (post-cap) characters leak as an accepted prefix.
1220
+ const requestedFriendlyName = friendlyName ? sanitizeSecretFriendlyName(friendlyName) : undefined;
1221
+ const sanitizedFriendlyName =
1222
+ requestedFriendlyName !== undefined &&
1223
+ friendlyName !== undefined &&
1224
+ !this.#friendlyNameCollidesWithSecret(sanitizeForCollisionCheck(friendlyName), friendlyName, secret)
1225
+ ? requestedFriendlyName
1226
+ : undefined;
1227
+ const preferredBase = this.#resolvePreferredPlaceholderBase(baseKey);
1228
+ const preferredPlaceholder = buildPlaceholder(hint, preferredBase, sanitizedFriendlyName);
1229
+ if (!this.#placeholderConflicts(preferredPlaceholder, secret)) {
1230
+ this.#registerDeobfuscationAlias(preferredPlaceholder, secret, recursive);
1231
+ return preferredPlaceholder;
1232
+ }
1233
+
1234
+ for (let attempt = 1; ; attempt++) {
1235
+ const fallbackBase = this.#reserveFallbackPlaceholderBase(baseKey, attempt);
1236
+ const placeholder = buildPlaceholder(hint, fallbackBase, sanitizedFriendlyName);
1237
+ if (!this.#placeholderConflicts(placeholder, secret)) {
1238
+ this.#registerDeobfuscationAlias(placeholder, secret, recursive);
1239
+ return placeholder;
1240
+ }
1241
+ }
1242
+ }
1243
+
1244
+ #resolvePreferredPlaceholderBase(baseKey: string): string {
1245
+ const existing = this.#placeholderBaseByKey.get(baseKey);
1246
+ if (existing !== undefined) return existing;
1247
+
1248
+ for (let attempt = 0; ; attempt++) {
1249
+ const base =
1250
+ attempt === 0 ? buildHashBase(this.#key, baseKey) : buildHashBase(this.#key, `${baseKey}\0${attempt}`);
1251
+ const owner = this.#placeholderBaseOwners.get(base);
1252
+ if (owner !== undefined && owner !== baseKey) continue;
1253
+ this.#placeholderBaseOwners.set(base, baseKey);
1254
+ this.#placeholderBaseByKey.set(baseKey, base);
1255
+ return base;
1256
+ }
1257
+ }
1258
+
1259
+ #reserveFallbackPlaceholderBase(baseKey: string, startAttempt: number): string {
1260
+ for (let attempt = startAttempt; ; attempt++) {
1261
+ const owner = `${baseKey}\0collision\0${attempt}`;
1262
+ const base = buildHashBase(this.#key, `${baseKey}\0collision\0${attempt}`);
1263
+ if (this.#placeholderBaseOwners.has(base)) continue;
1264
+ this.#placeholderBaseOwners.set(base, owner);
1265
+ return base;
1266
+ }
1267
+ }
1268
+
1269
+ #placeholderCollides(placeholder: string, secret: string): boolean {
1270
+ const existing = this.#deobfuscateMap.get(placeholder);
1271
+ return existing !== undefined && existing.secret !== secret;
1272
+ }
1273
+
1274
+ // A friendly placeholder is only safe if BOTH its full token and its
1275
+ // friendly-name-independent alias are free (or already ours), AND the token
1276
+ // itself is not another configured secret's literal value. Without the
1277
+ // latter check, a placeholder minted for secret A that happens to equal
1278
+ // secret B's raw content passes silently: B's own plain-secret redaction
1279
+ // pass (sorted by length, run once per obfuscate() call) already completed
1280
+ // before A's placeholder existed in the text, so B is never redacted out of
1281
+ // it — A's placeholder becomes a verbatim, provider-visible copy of B.
1282
+ #placeholderConflicts(placeholder: string, secret: string): boolean {
1283
+ if (this.#placeholderCollides(placeholder, secret)) return true;
1284
+ if (this.#configuredSecretValues.has(placeholder) && placeholder !== secret) return true;
1285
+ const unprefixed = placeholderWithoutFriendlyName(placeholder);
1286
+ if (unprefixed === undefined) return false;
1287
+ if (this.#placeholderCollides(unprefixed, secret)) return true;
1288
+ return this.#configuredSecretValues.has(unprefixed) && unprefixed !== secret;
1289
+ }
1290
+
1291
+ // A sanitized friendly name must not double as a live secret: it becomes a
1292
+ // verbatim, model-visible prefix on every placeholder minted for THIS
1293
+ // secret, baked in via an exact `#deobfuscateMap` entry rather than the
1294
+ // alias fallback — so it needs its own check independent of the scan-skip
1295
+ // alias guard in `#isGeneratedPlaceholder`. Reuses `#prefixIsSecretShaped`
1296
+ // for the sanitized-vs-sanitized comparisons (configured plain secrets,
1297
+ // every regex-discovered secret this instance has ever minted a
1298
+ // placeholder for) and the sanitized-label-vs-regex-pattern check, then
1299
+ // adds two label-specific checks `#prefixIsSecretShaped` cannot make: the
1300
+ // CURRENT secret being minted right now — which, on its first-ever mint,
1301
+ // is not yet in `#prefixIsSecretShaped`'s previously-discovered set, since
1302
+ // that set is only populated AFTER this call returns — normalized the
1303
+ // same way (catches, e.g., `friendlyName: "TOKABC123"` for a regex secret
1304
+ // whose literal match is `tok_abc123`, which the sanitized-name-vs-pattern
1305
+ // check misses when the pattern is case-sensitive/punctuated); and the RAW
1306
+ // (pre-sanitization) label against every configured regex pattern (catches
1307
+ // `friendlyName: "tok_abc123"` literally, which the SANITIZED label
1308
+ // `"TOKABC123"` could never match against that same case-sensitive
1309
+ // pattern). Any of these means the text is meant to be redacted, not
1310
+ // stamped unredacted onto every use of this secret.
1311
+ #collectRegexSecretValues(text: string): Set<string> {
1312
+ const values = new Set<string>();
1313
+ for (const entry of this.#regexEntries) {
1314
+ entry.regex.lastIndex = 0;
1315
+ for (;;) {
1316
+ const match = entry.regex.exec(text);
1317
+ if (match === null) break;
1318
+ if (match[0].length === 0) {
1319
+ entry.regex.lastIndex++;
1320
+ continue;
1321
+ }
1322
+ values.add(match[0]);
1323
+ }
1324
+ entry.regex.lastIndex = 0;
1325
+ }
1326
+ return values;
1327
+ }
1328
+
1329
+ collectRegexSecretValuesForObfuscation(text: string): Set<string> {
1330
+ const values = this.#collectRegexSecretValues(text);
1331
+ let result = text;
1332
+ let origin = "I".repeat(text.length);
1333
+ for (const [secret, replacement] of [...this.#replaceMappings].sort((a, b) => b[0].length - a[0].length)) {
1334
+ ({ text: result, origin } = this.#replaceOutsidePlaceholdersTracked(result, origin, secret, replacement, "I"));
1335
+ }
1336
+ for (const secretValue of this.#collectRegexSecretValues(result)) {
1337
+ values.add(secretValue);
1338
+ }
1339
+ for (const secretValue of this.#collectRegexSecretValuesAfterRegexReplacements(result, origin)) {
1340
+ values.add(secretValue);
1341
+ }
1342
+ return values;
1343
+ }
1344
+
1345
+ #collectRegexSecretValuesAfterRegexReplacements(text: string, origin: string): Set<string> {
1346
+ const values = new Set<string>();
1347
+ let simulated = text;
1348
+ let simulatedOrigin = origin;
1349
+ for (const entry of this.#regexEntries) {
1350
+ if (entry.mode !== "replace") continue;
1351
+ entry.regex.lastIndex = 0;
1352
+ const matches = this.#collectRegexMatches(
1353
+ simulated,
1354
+ entry.regex,
1355
+ entry.mode,
1356
+ simulatedOrigin,
1357
+ entry.replacement,
1358
+ );
1359
+ entry.regex.lastIndex = 0;
1360
+ if (matches.length === 0) continue;
1361
+ for (const match of [...matches].sort((a, b) => b.start - a.start)) {
1362
+ const replacement = entry.replacement ?? match.defaultReplacement;
1363
+ if (replacement === undefined) continue;
1364
+ for (const secretValue of this.#collectRegexSecretValues(replacement)) {
1365
+ values.add(secretValue);
1366
+ }
1367
+ simulated = replaceRange(simulated, match.start, match.end, replacement);
1368
+ simulatedOrigin = replaceRange(simulatedOrigin, match.start, match.end, "I".repeat(replacement.length));
1369
+ }
1370
+ for (const secretValue of this.#collectRegexSecretValues(simulated)) {
1371
+ values.add(secretValue);
1372
+ }
1373
+ }
1374
+ return values;
1375
+ }
1376
+
1377
+ #friendlyNameCollidesWithSecret(sanitizedName: string, rawName: string, secret: string): boolean {
1378
+ if (this.#prefixIsSecretShaped(sanitizedName)) return true;
1379
+ const sanitizedSecretValue = sanitizeForCollisionCheck(secret);
1380
+ if (sanitizedLabelCollidesWithSecret(sanitizedName, sanitizedSecretValue)) return true;
1381
+ for (const entry of this.#regexEntries) {
1382
+ entry.regex.lastIndex = 0;
1383
+ const matches = entry.regex.test(rawName);
1384
+ entry.regex.lastIndex = 0;
1385
+ if (matches) return true;
1386
+ }
1387
+ return false;
1388
+ }
1389
+
1390
+ #placeholderForCurrentInput(placeholder: string): string {
1391
+ const unprefixed = placeholderWithoutFriendlyName(placeholder);
1392
+ if (unprefixed === undefined) return placeholder;
1393
+ const match = /^#([A-Z0-9]+)_/.exec(placeholder);
1394
+ if (match === null || !this.#prefixIsSecretShaped(match[1]!)) return placeholder;
1395
+ return unprefixed;
1396
+ }
1397
+
1398
+ #stripUnsafeFriendlyPrefixes(text: string, origin: string): { text: string; origin: string } {
1399
+ PLACEHOLDER_RE.lastIndex = 0;
1400
+ let result = "";
1401
+ let resultOrigin = "";
1402
+ let cursor = 0;
1403
+ for (;;) {
1404
+ const match = PLACEHOLDER_RE.exec(text);
1405
+ if (match === null) break;
1406
+ const placeholder = match[0];
1407
+ const unprefixed = placeholderWithoutFriendlyName(placeholder);
1408
+ const replacement = unprefixed !== undefined ? this.#placeholderForCurrentInput(placeholder) : placeholder;
1409
+ if (replacement === placeholder) {
1410
+ resumePlaceholderScanAfterRejectedCandidate(match);
1411
+ continue;
1412
+ }
1413
+ result += text.slice(cursor, match.index);
1414
+ resultOrigin += origin.slice(cursor, match.index);
1415
+ result += replacement;
1416
+ resultOrigin += origin[match.index]?.repeat(replacement.length) ?? "";
1417
+ cursor = match.index + placeholder.length;
1418
+ }
1419
+ result += text.slice(cursor);
1420
+ resultOrigin += origin.slice(cursor);
1421
+ return { text: result, origin: resultOrigin };
1422
+ }
1423
+
1424
+ stripUnsafeFriendlyPlaceholderPrefixes(text: string, sharedRegexSecretValues: ReadonlySet<string>): string {
1425
+ const previousRegexSecretValues = this.#currentRegexSecretValues;
1426
+ this.#currentRegexSecretValues = new Set(sharedRegexSecretValues);
1427
+ try {
1428
+ return this.#stripUnsafeFriendlyPrefixes(text, "I".repeat(text.length)).text;
1429
+ } finally {
1430
+ this.#currentRegexSecretValues = previousRegexSecretValues;
1431
+ }
1432
+ }
1433
+
1434
+ #registerDeobfuscationAlias(placeholder: string, secret: string, recursive: boolean): void {
1435
+ const existing = this.#deobfuscateMap.get(placeholder);
1436
+ if (existing === undefined || existing.secret === secret) {
1437
+ this.#deobfuscateMap.set(placeholder, { secret, recursive });
1438
+ }
1439
+ const unprefixed = placeholderWithoutFriendlyName(placeholder);
1440
+ if (unprefixed !== undefined) {
1441
+ const existingUnprefixed = this.#deobfuscateMap.get(unprefixed);
1442
+ if (existingUnprefixed === undefined || existingUnprefixed.secret === secret) {
1443
+ this.#deobfuscateMap.set(unprefixed, { secret, recursive });
1444
+ }
1445
+ }
1446
+ }
1447
+
1448
+ // Whether an alnum-only, uppercase friendly-name-shaped prefix dropped from
1449
+ // a candidate placeholder token is itself something that should have been
1450
+ // redacted, rather than an arbitrary label: a sanitized form of a
1451
+ // configured plain secret's value, a sanitized form of any regex-
1452
+ // discovered secret's value this instance has ever minted a placeholder
1453
+ // for, or text a configured regex pattern matches directly. Shared by the
1454
+ // obfuscate-direction guard below and the deobfuscate-direction bare-alias
1455
+ // guard in `#deobfuscate` — both fall back to a friendly-name-independent
1456
+ // alias keyed only by the hash suffix, so both need the same defense
1457
+ // against a forged/attacker-chosen prefix.
1458
+ #prefixIsSecretShaped(prefix: string): boolean {
1459
+ for (const secretValue of this.#configuredSecretValues) {
1460
+ const sanitizedSecret = sanitizeForCollisionCheck(secretValue);
1461
+ if (sanitizedLabelCollidesWithSecret(prefix, sanitizedSecret)) return true;
1462
+ }
1463
+ for (const secretValue of this.#currentRegexSecretValues) {
1464
+ const sanitizedSecret = sanitizeForCollisionCheck(secretValue);
1465
+ if (sanitizedLabelCollidesWithSecret(prefix, sanitizedSecret)) return true;
1466
+ }
1467
+ for (const { secret } of this.#obfuscateMappings.values()) {
1468
+ const sanitizedSecret = sanitizeForCollisionCheck(secret);
1469
+ if (sanitizedLabelCollidesWithSecret(prefix, sanitizedSecret)) return true;
1470
+ }
1471
+ for (const entry of this.#regexEntries) {
1472
+ entry.regex.lastIndex = 0;
1473
+ const matches = entry.regex.test(prefix);
1474
+ entry.regex.lastIndex = 0;
1475
+ if (matches) return true;
1476
+ }
1477
+ return false;
1478
+ }
1479
+
1480
+ // A placeholder is an exact match, or the friendly-name-independent bare
1481
+ // alias: needed so a placeholder minted under a NOW-renamed friendly name
1482
+ // (same secret, same key, different `secrets.yml` label) still round-trips
1483
+ // when older provider-visible text is re-scanned by a renamed-config
1484
+ // instance — the hash suffix is a keyed digest of the secret VALUE alone,
1485
+ // so a same-key instance recomputes it identically regardless of the
1486
+ // label. The dropped prefix is otherwise unconstrained text, though: an
1487
+ // attacker who has observed ANY live placeholder's hash suffix elsewhere
1488
+ // in the transcript could wrap it around a DIFFERENT real secret's
1489
+ // plaintext (or a normalized rendering of one) to make the whole token
1490
+ // look pre-redacted and smuggle that secret through untouched.
1491
+ // `#prefixIsSecretShaped` above guards against that, shared with the
1492
+ // deobfuscate-direction check in `#deobfuscate`.
1493
+ #isGeneratedPlaceholder(placeholder: string): boolean {
1494
+ if (this.#deobfuscateMap.has(placeholder)) return true;
1495
+ const match = /^#([A-Z0-9]+)_([A-Z0-9]{4,}(?::[ULCM])?)#$/.exec(placeholder);
1496
+ if (match === null) return false;
1497
+ if (this.#prefixIsSecretShaped(match[1]!)) return false;
1498
+ return this.#deobfuscateMap.has(`#${match[2]}#`);
1499
+ }
1500
+
1501
+ // Replace `search` with `replacement` outside known generated placeholders while
1502
+ // maintaining a parallel `origin` tag string (one char per result char): kept
1503
+ // bytes keep their tag, inserted `replacement` bytes get `tag`, and preserved
1504
+ // placeholder spans keep their original tags. Lets later passes tell prior-call
1505
+ // placeholders ("I") from ones freshly inserted this call ("F") by RANGE.
1506
+ #replaceOutsidePlaceholdersTracked(
1507
+ text: string,
1508
+ origin: string,
1509
+ search: string,
1510
+ replacement: string,
1511
+ tag: string,
1512
+ ): { text: string; origin: string } {
1513
+ if (search.length === 0) return { text, origin };
1514
+ PLACEHOLDER_RE.lastIndex = 0;
1515
+ let outText = "";
1516
+ let outOrigin = "";
1517
+ let pending = 0;
1518
+ const emitChunk = (from: number, to: number): void => {
1519
+ let last = from;
1520
+ let idx = text.indexOf(search, from);
1521
+ while (idx !== -1 && idx + search.length <= to) {
1522
+ outText += text.slice(last, idx) + replacement;
1523
+ outOrigin += origin.slice(last, idx) + tag.repeat(replacement.length);
1524
+ last = idx + search.length;
1525
+ idx = text.indexOf(search, last);
1526
+ }
1527
+ outText += text.slice(last, to);
1528
+ outOrigin += origin.slice(last, to);
1529
+ };
1530
+ for (;;) {
1531
+ const match = PLACEHOLDER_RE.exec(text);
1532
+ if (match === null) break;
1533
+ if (!(this.#isGeneratedPlaceholder(match[0]) && match[0] !== search)) {
1534
+ resumePlaceholderScanAfterRejectedCandidate(match);
1535
+ continue;
1536
+ }
1537
+ emitChunk(pending, match.index);
1538
+ outText += match[0];
1539
+ outOrigin += origin.slice(match.index, match.index + match[0].length);
1540
+ pending = match.index + match[0].length;
1541
+ }
1542
+ emitChunk(pending, text.length);
1543
+ return { text: outText, origin: outOrigin };
1544
+ }
1545
+
1546
+ #placeholderForRegexChunk(secret: string, friendlyName: string | undefined): string {
1547
+ let index = this.#findObfuscateIndex(secret);
1548
+ if (index === undefined) {
1549
+ index = this.#nextIndex++;
1550
+ const placeholder = this.#createPlaceholder(secret, friendlyName);
1551
+ this.#obfuscateMappings.set(index, { secret, placeholder });
1552
+ this.#generatedPlaceholders.add(placeholder);
1553
+ }
1554
+ return this.#placeholderForCurrentInput(this.#obfuscateMappings.get(index)!.placeholder);
1555
+ }
1556
+
1557
+ #obfuscateOutsidePlaceholdersTracked(
1558
+ text: string,
1559
+ origin: string,
1560
+ friendlyName: string | undefined,
1561
+ ): { text: string; origin: string } {
1562
+ PLACEHOLDER_RE.lastIndex = 0;
1563
+ let outText = "";
1564
+ let outOrigin = "";
1565
+ let pending = 0;
1566
+ const emitChunk = (from: number, to: number): void => {
1567
+ if (from >= to) return;
1568
+ const placeholder = this.#placeholderForRegexChunk(text.slice(from, to), friendlyName);
1569
+ outText += placeholder;
1570
+ outOrigin += "F".repeat(placeholder.length);
1571
+ };
1572
+ for (;;) {
1573
+ const match = PLACEHOLDER_RE.exec(text);
1574
+ if (match === null) break;
1575
+ if (!this.#isGeneratedPlaceholder(match[0])) {
1576
+ resumePlaceholderScanAfterRejectedCandidate(match);
1577
+ continue;
1578
+ }
1579
+ emitChunk(pending, match.index);
1580
+ outText += match[0];
1581
+ outOrigin += origin.slice(match.index, match.index + match[0].length);
1582
+ pending = match.index + match[0].length;
1583
+ }
1584
+ emitChunk(pending, text.length);
1585
+ return { text: outText, origin: outOrigin };
1586
+ }
1587
+
1588
+ #knownPlaceholderRanges(text: string): Array<{ start: number; end: number }> {
1589
+ PLACEHOLDER_RE.lastIndex = 0;
1590
+ const ranges: Array<{ start: number; end: number }> = [];
1591
+ for (;;) {
1592
+ const match = PLACEHOLDER_RE.exec(text);
1593
+ if (match === null) break;
1594
+ if (this.#isGeneratedPlaceholder(match[0])) {
1595
+ ranges.push({ start: match.index, end: match.index + match[0].length });
1596
+ } else {
1597
+ resumePlaceholderScanAfterRejectedCandidate(match);
1598
+ }
1599
+ }
1600
+ return ranges;
1601
+ }
1602
+
1603
+ #collectRegexMatches(
1604
+ text: string,
1605
+ regex: RegExp,
1606
+ mode: "obfuscate" | "replace",
1607
+ origin: string,
1608
+ replacement: string | undefined,
1609
+ ): Array<{
1610
+ start: number;
1611
+ end: number;
1612
+ value: string;
1613
+ canonicalValue: string;
1614
+ scanMatchLength: number;
1615
+ recursive: boolean;
1616
+ preserveGeneratedPlaceholders: boolean;
1617
+ preserveInputPlaceholders: boolean;
1618
+ inputPlaceholderOutside: string;
1619
+ inputPlaceholderOutsideIndependentlyMatches: boolean;
1620
+ inputPlaceholderOutsideStart: number;
1621
+ inputPlaceholderOutsideChunkCount: number;
1622
+ inputPlaceholderInnerIndependentlyMatches: boolean;
1623
+ defaultReplacement: string | undefined;
1624
+ scanContext: RegexMatchContext;
1625
+ }> {
1626
+ const knownPlaceholderRanges = this.#knownPlaceholderRanges(text);
1627
+ const regexScan = buildReplaceRegexScan(text, knownPlaceholderRanges, this.#deobfuscateMap);
1628
+ const scanText = regexScan.text;
1629
+ regex.lastIndex = 0;
1630
+ const matches: Array<{
1631
+ start: number;
1632
+ end: number;
1633
+ value: string;
1634
+ canonicalValue: string;
1635
+ scanMatchLength: number;
1636
+ recursive: boolean;
1637
+ preserveGeneratedPlaceholders: boolean;
1638
+ preserveInputPlaceholders: boolean;
1639
+ inputPlaceholderOutside: string;
1640
+ inputPlaceholderOutsideIndependentlyMatches: boolean;
1641
+ inputPlaceholderOutsideStart: number;
1642
+ inputPlaceholderOutsideChunkCount: number;
1643
+ inputPlaceholderInnerIndependentlyMatches: boolean;
1644
+ defaultReplacement: string | undefined;
1645
+ scanContext: RegexMatchContext;
1646
+ }> = [];
1647
+ for (;;) {
1648
+ const match = regex.exec(scanText);
1649
+ if (match === null) break;
1650
+ if (match[0].length === 0) {
1651
+ regex.lastIndex++;
1652
+ continue;
1653
+ }
1654
+ let start = match.index;
1655
+ let end = match.index + match[0].length;
1656
+ let scanMatchLength = match[0].length;
1657
+ let scanMatchValue = match[0];
1658
+ let canonicalValue = "";
1659
+ let recursive = false;
1660
+ let preserveGeneratedPlaceholders = false;
1661
+ let preserveInputPlaceholders = false;
1662
+ let inputPlaceholderOutside = "";
1663
+ let inputPlaceholderOutsideIndependentlyMatches = false;
1664
+ let inputPlaceholderOutsideStart = -1;
1665
+ let inputPlaceholderOutsideChunkCount = 0;
1666
+ let inputPlaceholderInnerIndependentlyMatches = false;
1667
+
1668
+ let mapped = mapReplaceRegexMatch(regexScan.segments, start, end);
1669
+ if (mapped.partialPlaceholderCut) {
1670
+ // The match straddles a generated placeholder (its boundary falls inside
1671
+ // the secret's expanded value). Rewriting across the token drops bytes
1672
+ // (obfuscate) or drifts the redaction across re-obfuscation passes
1673
+ // (replace), so the cut secret must stay as its existing placeholder.
1674
+ // But wholly-outside bytes on either side of the placeholder are still
1675
+ // provider-visible content covered by the regex. Probe the prefix against
1676
+ // the full expanded scan text so right-hand context supplied by the
1677
+ // placeholder still satisfies lookahead/alternatives, then clamp the
1678
+ // accepted match to the prefix boundary. If no prefix is available, redact
1679
+ // the outside suffix that was covered by the full-context match. Every
1680
+ // resume point computed below is chained through
1681
+ // `extendPastAdjacentPlaceholders`: a resume position that lands exactly on
1682
+ // the START of ANOTHER placeholder must skip that one too before a fresh
1683
+ // `regex.exec` attempt runs there, so a run of adjacent placeholders resolves
1684
+ // identically whether its LEADING member is raw text this call is about to
1685
+ // placeholder or is already a placeholder from a prior call/pass (see that
1686
+ // helper's doc for the cross-call drift this prevents).
1687
+ const cutResumeIndex = mapped.cutResumeIndex;
1688
+ const prefixScanEnd = mapped.firstPlaceholderScanStart;
1689
+ let handledOutside = false;
1690
+ if (prefixScanEnd > match.index) {
1691
+ regex.lastIndex = match.index;
1692
+ const prefixMatch = regex.exec(scanText);
1693
+ if (prefixMatch !== null && prefixMatch[0].length > 0 && prefixMatch.index < prefixScanEnd) {
1694
+ const prefixStart = prefixMatch.index;
1695
+ const prefixEnd = Math.min(prefixMatch.index + prefixMatch[0].length, prefixScanEnd);
1696
+ const prefixMapped = mapReplaceRegexMatch(regexScan.segments, prefixStart, prefixEnd);
1697
+ if (!prefixMapped.partialPlaceholderCut && prefixEnd > prefixStart) {
1698
+ start = prefixStart;
1699
+ end = prefixEnd;
1700
+ scanMatchValue = scanText.slice(prefixStart, prefixEnd);
1701
+ // Keep the full match length in the expanded scan view (not the
1702
+ // clamped prefix length) — the short-match guard below measures the
1703
+ // regex's own match length, so a full-size match with a short
1704
+ // outside-prefix remainder must not be undercounted as too short,
1705
+ // matching the suffix-clamp branch below.
1706
+ scanMatchLength = match[0].length;
1707
+ mapped = prefixMapped;
1708
+ regex.lastIndex = extendPastAdjacentPlaceholders(regexScan.segments, prefixEnd);
1709
+ handledOutside = true;
1710
+ }
1711
+ }
1712
+ }
1713
+ if (!handledOutside && cutResumeIndex < end) {
1714
+ const suffixStart = cutResumeIndex;
1715
+ const suffixEnd = end;
1716
+ const suffixMapped = mapReplaceRegexMatch(regexScan.segments, suffixStart, suffixEnd);
1717
+ if (!suffixMapped.partialPlaceholderCut) {
1718
+ start = suffixStart;
1719
+ end = suffixEnd;
1720
+ scanMatchValue = scanText.slice(suffixStart, suffixEnd);
1721
+ scanMatchLength = match[0].length;
1722
+ mapped = suffixMapped;
1723
+ regex.lastIndex = extendPastAdjacentPlaceholders(regexScan.segments, suffixEnd);
1724
+ handledOutside = true;
1725
+ }
1726
+ }
1727
+ if (!handledOutside) {
1728
+ regex.lastIndex = extendPastAdjacentPlaceholders(regexScan.segments, cutResumeIndex);
1729
+ continue;
1730
+ }
1731
+ }
1732
+ // Scan-space coordinates of the match (placeholders expanded). The default
1733
+ // redaction's fixed-point check must run against this expanded view — the
1734
+ // view re-obfuscation actually scans — not the literal `#…#` text, or a
1735
+ // redaction adjacent to a placeholder (e.g. an outside prefix before a cut
1736
+ // secret) could drift when the placeholder expands and connects to it.
1737
+ const scanMatchStart = start;
1738
+ const scanMatchEnd = end;
1739
+ let defaultReplacement: string | undefined;
1740
+ start = mapped.start;
1741
+ end = mapped.end;
1742
+ preserveGeneratedPlaceholders = mapped.preserveGeneratedPlaceholders;
1743
+ // A match overlapping a placeholder that arrived in the INPUT (origin tag
1744
+ // "I" — generated by a PRIOR obfuscate() call) must preserve that
1745
+ // placeholder atomically so repeated obfuscation stays a fixed point. If
1746
+ // raw bytes surround it, they still need redaction; branch-specific
1747
+ // replacement below keeps prior placeholders while covering the
1748
+ // non-placeholder chunks.
1749
+ const overlapsInputPlaceholder = knownPlaceholderRanges.some(
1750
+ range => start < range.end && end > range.start && origin[range.start] === "I",
1751
+ );
1752
+ preserveInputPlaceholders = overlapsInputPlaceholder;
1753
+ if (overlapsInputPlaceholder) {
1754
+ const firstOutside = firstOutsidePlaceholderRange(start, end, knownPlaceholderRanges);
1755
+ if (
1756
+ mode === "replace" &&
1757
+ replacement !== undefined &&
1758
+ firstOutside !== undefined &&
1759
+ text.slice(firstOutside.start, firstOutside.start + replacement.length) === replacement
1760
+ ) {
1761
+ const expandedEnd = firstOutside.start + replacement.length;
1762
+ if (expandedEnd > end) {
1763
+ regex.lastIndex = Math.max(regex.lastIndex, match.index + match[0].length + expandedEnd - end);
1764
+ end = expandedEnd;
1765
+ }
1766
+ }
1767
+ inputPlaceholderOutside = textOutsidePlaceholderRanges(text, start, end, knownPlaceholderRanges);
1768
+ inputPlaceholderOutsideStart =
1769
+ firstOutsidePlaceholderRange(start, end, knownPlaceholderRanges)?.start ?? -1;
1770
+ inputPlaceholderOutsideChunkCount = countOutsidePlaceholderRanges(start, end, knownPlaceholderRanges);
1771
+ if (inputPlaceholderOutside.length === 0) continue;
1772
+ const resumeIndex = regex.lastIndex;
1773
+ // Test each outside chunk in isolation rather than the concatenation of
1774
+ // all of them: concatenating chunks that sit on either side of the
1775
+ // placeholder erases the `#…#` token boundary between them (e.g. a
1776
+ // prefix "ABCDEFGH" that independently matches `\b[A-Z]{8}\b` next to a
1777
+ // placeholder, followed by a raw "I" suffix, concatenates to "ABCDEFGHI"
1778
+ // — which does NOT match, since the boundary the placeholder provided is
1779
+ // gone). That false negative would leave a genuinely independent,
1780
+ // secret-shaped chunk unredacted. Each chunk is tested in BOTH its real
1781
+ // literal-token context (where the token's own non-word boundary can
1782
+ // complete a boundary-sensitive pattern) and the EXPANDED scan context —
1783
+ // the same view re-obfuscation's own regex scan runs against — so a
1784
+ // lookbehind/lookahead that only resolves once the neighboring
1785
+ // placeholder is expanded (e.g. `(?<=ABCDEFGH)SECRET` beside a
1786
+ // placeholder for `ABCDEFGH`) also counts as an independent match.
1787
+ inputPlaceholderOutsideIndependentlyMatches = outsidePlaceholderRangesAnyIndependentlyMatch(
1788
+ text,
1789
+ scanText,
1790
+ regexScan.segments,
1791
+ start,
1792
+ end,
1793
+ knownPlaceholderRanges,
1794
+ regex,
1795
+ );
1796
+ // Whether the placeholder's own (deobfuscated) value satisfies the regex
1797
+ // with the surrounding raw bytes dropped. When it does, those raw bytes
1798
+ // are greedy spillover the match never needed (e.g. the trailing `A` in
1799
+ // `SECRETUV→#…#A`); obfuscating them on re-obfuscation drifts the
1800
+ // provider-visible history and prompt-cache prefix. When it does NOT
1801
+ // (e.g. `api_key=` literal that the placeholder value alone cannot match),
1802
+ // the outside bytes are structurally required and must be obfuscated.
1803
+ const innerText = placeholderInnerText(text, start, end, knownPlaceholderRanges, this.#deobfuscateMap);
1804
+ regex.lastIndex = 0;
1805
+ inputPlaceholderInnerIndependentlyMatches = innerText.length > 0 && regex.test(innerText);
1806
+ regex.lastIndex = resumeIndex;
1807
+ }
1808
+ if (mode === "replace") {
1809
+ canonicalValue = scanMatchValue;
1810
+ recursive = mapped.recursive;
1811
+ } else {
1812
+ const overlappingRanges = knownPlaceholderRanges.filter(range => start < range.end && end > range.start);
1813
+ const containedByPlaceholder = overlappingRanges.some(range => start >= range.start && end <= range.end);
1814
+ if (containedByPlaceholder) {
1815
+ continue;
1816
+ }
1817
+ const canonical = deobfuscateGeneratedPlaceholderRanges(
1818
+ text,
1819
+ start,
1820
+ end,
1821
+ knownPlaceholderRanges,
1822
+ this.#deobfuscateMap,
1823
+ );
1824
+ canonicalValue = canonical.text;
1825
+ recursive = canonical.recursive;
1826
+ }
1827
+
1828
+ const scanContext = {
1829
+ text: scanText,
1830
+ start: scanMatchStart,
1831
+ end: scanMatchEnd,
1832
+ };
1833
+ if (mode === "replace" && replacement === undefined && !preserveGeneratedPlaceholders) {
1834
+ const savedLastIndex = regex.lastIndex;
1835
+ defaultReplacement = this.#generateRegexReplacement(scanMatchValue, regex, scanContext);
1836
+ regex.lastIndex = savedLastIndex;
1837
+ }
1838
+ matches.push({
1839
+ start,
1840
+ end,
1841
+ value: text.slice(start, end),
1842
+ defaultReplacement,
1843
+ canonicalValue,
1844
+ scanMatchLength,
1845
+ recursive,
1846
+ preserveGeneratedPlaceholders,
1847
+ preserveInputPlaceholders,
1848
+ inputPlaceholderOutside,
1849
+ inputPlaceholderOutsideIndependentlyMatches,
1850
+ inputPlaceholderOutsideStart,
1851
+ inputPlaceholderOutsideChunkCount,
1852
+ inputPlaceholderInnerIndependentlyMatches,
1853
+ scanContext,
1854
+ });
1855
+ }
1856
+ return matches.reverse();
1857
+ }
204
1858
  }
205
1859
 
206
1860
  // ═══════════════════════════════════════════════════════════════════════════
@@ -214,37 +1868,54 @@ export class SecretObfuscator {
214
1868
  * tool-result messages are persisted with their literal text, so a literal
215
1869
  * `#ABCD#` the operator typed must survive untouched; those roles are never
216
1870
  * walked.
1871
+ *
1872
+ * Legacy index-derived aliases (`#XXXX#`) are unkeyed and trivially guessable,
1873
+ * so a prompt-injected model can plant one in any record it influences. Every
1874
+ * agent-feeding path (resume, history rewrite, branch switch) therefore restores
1875
+ * keyed placeholders ONLY (`allowLegacyAliases` false), leaving legacy tokens
1876
+ * inert; display-only transcripts that are never re-obfuscated opt in via
1877
+ * `allowLegacyAliases`.
217
1878
  */
218
1879
  export function deobfuscateSessionContext(
219
1880
  sessionContext: SessionContext,
220
1881
  obfuscator: SecretObfuscator | undefined,
1882
+ allowLegacyAliases = false,
221
1883
  ): SessionContext {
222
- if (!obfuscator?.hasSecrets()) return sessionContext;
223
- const messages = deobfuscateAgentMessages(obfuscator, sessionContext.messages);
1884
+ if (!obfuscator || !(allowLegacyAliases ? obfuscator.hasStoredSecrets() : obfuscator.hasSecrets()))
1885
+ return sessionContext;
1886
+ const messages = deobfuscateAgentMessages(obfuscator, sessionContext.messages, allowLegacyAliases);
224
1887
  return messages === sessionContext.messages ? sessionContext : { ...sessionContext, messages };
225
1888
  }
226
1889
 
227
- export function deobfuscateAgentMessages(obfuscator: SecretObfuscator, messages: AgentMessage[]): AgentMessage[] {
1890
+ export function deobfuscateAgentMessages(
1891
+ obfuscator: SecretObfuscator,
1892
+ messages: AgentMessage[],
1893
+ allowLegacyAliases = false,
1894
+ ): AgentMessage[] {
1895
+ const deob = (text: string): string =>
1896
+ allowLegacyAliases ? obfuscator.deobfuscateStored(text) : obfuscator.deobfuscate(text);
228
1897
  let changed = false;
229
1898
  const result = messages.map((message): AgentMessage => {
230
1899
  switch (message.role) {
231
1900
  case "assistant": {
232
- const content = deobfuscateAssistantContent(obfuscator, message.content);
1901
+ const content = deobfuscateAssistantContent(obfuscator, message.content, allowLegacyAliases);
233
1902
  if (content === message.content) return message;
234
1903
  changed = true;
235
1904
  return { ...message, content };
236
1905
  }
237
1906
  case "branchSummary": {
238
- const summary = obfuscator.deobfuscate(message.summary);
1907
+ const summary = deob(message.summary);
239
1908
  if (summary === message.summary) return message;
240
1909
  changed = true;
241
1910
  return { ...message, summary };
242
1911
  }
243
1912
  case "compactionSummary": {
244
- const summary = obfuscator.deobfuscate(message.summary);
245
- const shortSummary =
246
- message.shortSummary === undefined ? undefined : obfuscator.deobfuscate(message.shortSummary);
247
- const blocks = message.blocks === undefined ? undefined : deobfuscateTextBlocks(obfuscator, message.blocks);
1913
+ const summary = deob(message.summary);
1914
+ const shortSummary = message.shortSummary === undefined ? undefined : deob(message.shortSummary);
1915
+ const blocks =
1916
+ message.blocks === undefined
1917
+ ? undefined
1918
+ : deobfuscateTextBlocks(obfuscator, message.blocks, allowLegacyAliases);
248
1919
  if (summary === message.summary && shortSummary === message.shortSummary && blocks === message.blocks) {
249
1920
  return message;
250
1921
  }
@@ -266,20 +1937,24 @@ export function deobfuscateAgentMessages(obfuscator: SecretObfuscator, messages:
266
1937
  export function deobfuscateAssistantContent(
267
1938
  obfuscator: SecretObfuscator,
268
1939
  content: AssistantMessage["content"],
1940
+ allowLegacyAliases = false,
269
1941
  ): AssistantMessage["content"] {
270
- if (!obfuscator.hasSecrets()) return content;
1942
+ if (!(allowLegacyAliases ? obfuscator.hasStoredSecrets() : obfuscator.hasSecrets())) return content;
1943
+ const deob = (text: string): string =>
1944
+ allowLegacyAliases ? obfuscator.deobfuscateStored(text) : obfuscator.deobfuscate(text);
271
1945
  let changed = false;
272
1946
  const result = content.map((block): AssistantMessage["content"][number] => {
273
1947
  if (block.type === "text") {
274
- const text = obfuscator.deobfuscate(block.text);
1948
+ const text = deob(block.text);
275
1949
  if (text === block.text) return block;
276
1950
  changed = true;
277
1951
  return { ...block, text };
278
1952
  }
1953
+
279
1954
  if (block.type === "toolCall") {
280
- const args = deobfuscateToolArguments(obfuscator, block.arguments);
281
- const intent = block.intent === undefined ? undefined : obfuscator.deobfuscate(block.intent);
282
- const rawBlock = block.rawBlock === undefined ? undefined : obfuscator.deobfuscate(block.rawBlock);
1955
+ const args = deobfuscateToolArguments(obfuscator, block.arguments, allowLegacyAliases);
1956
+ const intent = block.intent === undefined ? undefined : deob(block.intent);
1957
+ const rawBlock = block.rawBlock === undefined ? undefined : deob(block.rawBlock);
283
1958
  if (args === block.arguments && intent === block.intent && rawBlock === block.rawBlock) return block;
284
1959
  changed = true;
285
1960
  return { ...block, arguments: args, intent, rawBlock };
@@ -297,18 +1972,23 @@ export function deobfuscateAssistantContent(
297
1972
  export function deobfuscateToolArguments(
298
1973
  obfuscator: SecretObfuscator,
299
1974
  args: Record<string, unknown>,
1975
+ allowLegacyAliases = false,
300
1976
  ): Record<string, unknown> {
301
- if (!obfuscator.hasSecrets()) return args;
302
- return mapJsonStrings(args as JsonValue, s => obfuscator.deobfuscate(s)) as Record<string, unknown>;
1977
+ if (!(allowLegacyAliases ? obfuscator.hasStoredSecrets() : obfuscator.hasSecrets())) return args;
1978
+ const deob = (text: string): string =>
1979
+ allowLegacyAliases ? obfuscator.deobfuscateStored(text) : obfuscator.deobfuscate(text);
1980
+ return mapJsonStrings(args as JsonValue, deob) as Record<string, unknown>;
303
1981
  }
304
1982
 
305
1983
  /** Redact secrets inside a tool call's arguments (same JSON-walk exception as {@link deobfuscateToolArguments}). */
306
1984
  export function obfuscateToolArguments(
307
1985
  obfuscator: SecretObfuscator,
308
1986
  args: Record<string, unknown>,
1987
+ sharedRegexSecretValues?: ReadonlySet<string>,
309
1988
  ): Record<string, unknown> {
310
1989
  if (!obfuscator.hasSecrets()) return args;
311
- return mapJsonStrings(args as JsonValue, s => obfuscator.obfuscate(s)) as Record<string, unknown>;
1990
+ const regexSecretValues = sharedRegexSecretValues ?? collectJsonRegexSecretValues(obfuscator, args as JsonValue);
1991
+ return mapJsonStrings(args as JsonValue, s => obfuscator.obfuscate(s, regexSecretValues)) as Record<string, unknown>;
312
1992
  }
313
1993
 
314
1994
  // ═══════════════════════════════════════════════════════════════════════════
@@ -321,11 +2001,12 @@ type UserFacingMessage = Extract<Message, { role: "user" | "developer" | "toolRe
321
2001
  function obfuscateTextBlocks(
322
2002
  obfuscator: SecretObfuscator,
323
2003
  content: (TextContent | ImageContent)[],
2004
+ sharedRegexSecretValues?: ReadonlySet<string>,
324
2005
  ): (TextContent | ImageContent)[] {
325
2006
  let changed = false;
326
2007
  const result = content.map((block): TextContent | ImageContent => {
327
2008
  if (block.type !== "text") return block;
328
- const text = obfuscator.obfuscate(block.text);
2009
+ const text = obfuscator.obfuscate(block.text, sharedRegexSecretValues);
329
2010
  if (text === block.text) return block;
330
2011
  changed = true;
331
2012
  return { ...block, text };
@@ -337,11 +2018,14 @@ function obfuscateTextBlocks(
337
2018
  function deobfuscateTextBlocks(
338
2019
  obfuscator: SecretObfuscator,
339
2020
  content: (TextContent | ImageContent)[],
2021
+ allowLegacyAliases = false,
340
2022
  ): (TextContent | ImageContent)[] {
2023
+ const deob = (text: string): string =>
2024
+ allowLegacyAliases ? obfuscator.deobfuscateStored(text) : obfuscator.deobfuscate(text);
341
2025
  let changed = false;
342
2026
  const result = content.map((block): TextContent | ImageContent => {
343
2027
  if (block.type !== "text") return block;
344
- const text = obfuscator.deobfuscate(block.text);
2028
+ const text = deob(block.text);
345
2029
  if (text === block.text) return block;
346
2030
  changed = true;
347
2031
  return { ...block, text };
@@ -350,15 +2034,99 @@ function deobfuscateTextBlocks(
350
2034
  }
351
2035
 
352
2036
  /**
353
- * Redact secrets from outbound messages. Opt-in by origin: only user messages,
354
- * tool results, and user-authored developer messages (e.g. `@file` mentions)
355
- * can carry operator secrets. System prompts, tool schemas, and assistant
356
- * output are author-controlled or model-generated and pass through untouched.
357
- * Within a targeted message only `text` blocks are rewritten — inline image
358
- * bytes are never walked.
2037
+ * Re-obfuscate assistant content before it returns to a provider after session
2038
+ * restoration, removing friendly prefixes made unsafe by this batch. A changed
2039
+ * thinking block loses its byte-bound replay signature.
2040
+ */
2041
+ function obfuscateAssistantContentForReplay(
2042
+ obfuscator: SecretObfuscator,
2043
+ content: AssistantMessage["content"],
2044
+ sharedRegexSecretValues: ReadonlySet<string>,
2045
+ ): AssistantMessage["content"] {
2046
+ const obfuscate = (text: string): string =>
2047
+ obfuscator.stripUnsafeFriendlyPlaceholderPrefixes(
2048
+ obfuscator.obfuscate(text, sharedRegexSecretValues),
2049
+ sharedRegexSecretValues,
2050
+ );
2051
+ let changed = false;
2052
+ const result = content.map((block): AssistantMessage["content"][number] => {
2053
+ if (block.type === "text") {
2054
+ const text = obfuscate(block.text);
2055
+ if (text === block.text) return block;
2056
+ changed = true;
2057
+ return { ...block, text };
2058
+ }
2059
+ if (block.type === "thinking") {
2060
+ const thinking = obfuscate(block.thinking);
2061
+ if (thinking === block.thinking) return block;
2062
+ changed = true;
2063
+ return { ...block, thinking, thinkingSignature: undefined };
2064
+ }
2065
+ if (block.type === "toolCall") {
2066
+ const args = mapJsonStrings(block.arguments as JsonValue, obfuscate) as Record<string, unknown>;
2067
+ const intent = block.intent === undefined ? undefined : obfuscate(block.intent);
2068
+ const rawBlock = block.rawBlock === undefined ? undefined : obfuscate(block.rawBlock);
2069
+ if (args === block.arguments && intent === block.intent && rawBlock === block.rawBlock) return block;
2070
+ changed = true;
2071
+ return { ...block, arguments: args, intent, rawBlock };
2072
+ }
2073
+ return block;
2074
+ });
2075
+ return changed ? result : content;
2076
+ }
2077
+
2078
+ function collectMessageRegexSecretValues(obfuscator: SecretObfuscator, messages: Message[]): Set<string> {
2079
+ const values = new Set<string>();
2080
+ const addText = (text: string | undefined): void => {
2081
+ if (text === undefined) return;
2082
+ for (const value of obfuscator.collectRegexSecretValuesForObfuscation(text)) {
2083
+ values.add(value);
2084
+ }
2085
+ };
2086
+ for (const message of messages) {
2087
+ if (message.role === "assistant") {
2088
+ for (const block of message.content) {
2089
+ if (block.type === "text") addText(block.text);
2090
+ else if (block.type === "thinking") addText(block.thinking);
2091
+ else if (block.type === "toolCall") {
2092
+ for (const value of collectJsonRegexSecretValues(obfuscator, block.arguments as JsonValue)) {
2093
+ values.add(value);
2094
+ }
2095
+ addText(block.intent);
2096
+ addText(block.rawBlock);
2097
+ }
2098
+ }
2099
+ continue;
2100
+ }
2101
+ if (
2102
+ message.role !== "user" &&
2103
+ message.role !== "toolResult" &&
2104
+ !(message.role === "developer" && message.attribution === "user")
2105
+ ) {
2106
+ continue;
2107
+ }
2108
+ const target = message as UserFacingMessage;
2109
+ if (typeof target.content === "string") {
2110
+ addText(target.content);
2111
+ continue;
2112
+ }
2113
+ for (const block of target.content) {
2114
+ if (block.type === "text") addText(block.text);
2115
+ }
2116
+ }
2117
+ return values;
2118
+ }
2119
+
2120
+ /**
2121
+ * Redact secrets from outbound messages. User messages, tool results, and
2122
+ * user-authored developer messages (e.g. `@file` mentions) are obfuscated.
2123
+ * Assistant replay content is re-obfuscated too, because session restoration
2124
+ * expands keyed placeholders locally before the next provider request. Inline
2125
+ * image bytes are never walked.
359
2126
  */
360
2127
  export function obfuscateMessages(obfuscator: SecretObfuscator, messages: Message[]): Message[] {
361
2128
  if (!obfuscator.hasSecrets()) return messages;
2129
+ const sharedRegexSecretValues = collectMessageRegexSecretValues(obfuscator, messages);
362
2130
  let changed = false;
363
2131
  const result = messages.map((message): Message => {
364
2132
  if (
@@ -366,16 +2134,20 @@ export function obfuscateMessages(obfuscator: SecretObfuscator, messages: Messag
366
2134
  message.role !== "toolResult" &&
367
2135
  !(message.role === "developer" && message.attribution === "user")
368
2136
  ) {
369
- return message;
2137
+ if (message.role !== "assistant") return message;
2138
+ const content = obfuscateAssistantContentForReplay(obfuscator, message.content, sharedRegexSecretValues);
2139
+ if (content === message.content) return message;
2140
+ changed = true;
2141
+ return { ...message, content };
370
2142
  }
371
2143
  const target = message as UserFacingMessage;
372
2144
  if (typeof target.content === "string") {
373
- const content = obfuscator.obfuscate(target.content);
2145
+ const content = obfuscator.obfuscate(target.content, sharedRegexSecretValues);
374
2146
  if (content === target.content) return message;
375
2147
  changed = true;
376
2148
  return { ...target, content } as Message;
377
2149
  }
378
- const content = obfuscateTextBlocks(obfuscator, target.content);
2150
+ const content = obfuscateTextBlocks(obfuscator, target.content, sharedRegexSecretValues);
379
2151
  if (content === target.content) return message;
380
2152
  changed = true;
381
2153
  return { ...target, content } as Message;
@@ -397,18 +2169,469 @@ export function obfuscateProviderContext(obfuscator: SecretObfuscator | undefine
397
2169
  // Helpers
398
2170
  // ═══════════════════════════════════════════════════════════════════════════
399
2171
 
400
- /** Replace all occurrences of `search` in `text` with `replacement`. */
401
- function replaceAll(text: string, search: string, replacement: string): string {
402
- if (search.length === 0) return text;
403
- let result = text;
404
- let idx = result.indexOf(search);
405
- while (idx !== -1) {
406
- result = result.slice(0, idx) + replacement + result.slice(idx + search.length);
407
- idx = result.indexOf(search, idx + replacement.length);
2172
+ // Like the untracked walk, but threads a parallel `origin` tag string through:
2173
+ // preserved placeholder spans keep their existing origin tag (so a
2174
+ // same-call-fresh "F" placeholder is never relabeled prior-call "I", and vice
2175
+ // versa), while `transform`'s output — always freshly generated or redacted
2176
+ // content in both callers below — is tagged "I" (it must not be re-matched as
2177
+ // though it arrived in the input, mirroring plain-secret replacement tagging).
2178
+ function transformOutsidePlaceholdersTracked(
2179
+ text: string,
2180
+ origin: string,
2181
+ shouldSkipPlaceholder: (placeholder: string) => boolean,
2182
+ transform: (chunk: string) => string,
2183
+ preservePlaceholder?: (placeholder: string) => string,
2184
+ ): { text: string; origin: string } {
2185
+ PLACEHOLDER_RE.lastIndex = 0;
2186
+ let result = "";
2187
+ let resultOrigin = "";
2188
+ let pendingIndex = 0;
2189
+ for (;;) {
2190
+ const match = PLACEHOLDER_RE.exec(text);
2191
+ if (match === null) break;
2192
+ if (!shouldSkipPlaceholder(match[0])) {
2193
+ resumePlaceholderScanAfterRejectedCandidate(match);
2194
+ continue;
2195
+ }
2196
+ const transformed = transform(text.slice(pendingIndex, match.index));
2197
+ result += transformed;
2198
+ resultOrigin += "I".repeat(transformed.length);
2199
+ const preserved = preservePlaceholder ? preservePlaceholder(match[0]) : match[0];
2200
+ result += preserved;
2201
+ resultOrigin += origin.slice(match.index, match.index + match[0].length);
2202
+ pendingIndex = match.index + match[0].length;
2203
+ }
2204
+ const trailing = transform(text.slice(pendingIndex));
2205
+ result += trailing;
2206
+ resultOrigin += "I".repeat(trailing.length);
2207
+ return { text: result, origin: resultOrigin };
2208
+ }
2209
+
2210
+ function trailingOutsidePreservedPlaceholderChunk(
2211
+ text: string,
2212
+ shouldPreservePlaceholder: (placeholder: string) => boolean,
2213
+ ): string {
2214
+ PLACEHOLDER_RE.lastIndex = 0;
2215
+ let pendingIndex = 0;
2216
+ let sawPlaceholder = false;
2217
+ for (;;) {
2218
+ const match = PLACEHOLDER_RE.exec(text);
2219
+ if (match === null) break;
2220
+ if (!shouldPreservePlaceholder(match[0])) {
2221
+ resumePlaceholderScanAfterRejectedCandidate(match);
2222
+ continue;
2223
+ }
2224
+ sawPlaceholder = true;
2225
+ pendingIndex = match.index + match[0].length;
2226
+ }
2227
+ return sawPlaceholder ? text.slice(pendingIndex) : "";
2228
+ }
2229
+
2230
+ function buildReplaceRegexScan(
2231
+ text: string,
2232
+ ranges: ReadonlyArray<{ start: number; end: number }>,
2233
+ deobfuscateMap: ReadonlyMap<string, { secret: string; recursive: boolean }>,
2234
+ ): ReplaceRegexScan {
2235
+ let scanText = "";
2236
+ let cursor = 0;
2237
+ const segments: RegexScanSegment[] = [];
2238
+ const appendSegment = (
2239
+ value: string,
2240
+ textStart: number,
2241
+ textEnd: number,
2242
+ generatedPlaceholder: boolean,
2243
+ recursive: boolean,
2244
+ ) => {
2245
+ if (value.length === 0) return;
2246
+ const scanStart = scanText.length;
2247
+ scanText += value;
2248
+ segments.push({
2249
+ scanStart,
2250
+ scanEnd: scanStart + value.length,
2251
+ textStart,
2252
+ textEnd,
2253
+ generatedPlaceholder,
2254
+ recursive,
2255
+ });
2256
+ };
2257
+
2258
+ for (const range of ranges) {
2259
+ appendSegment(text.slice(cursor, range.start), cursor, range.start, false, false);
2260
+ const placeholder = text.slice(range.start, range.end);
2261
+ const mapping = lookupFriendlyPlaceholderAlias(deobfuscateMap, placeholder);
2262
+ appendSegment(mapping?.secret ?? placeholder, range.start, range.end, true, mapping?.recursive ?? false);
2263
+ cursor = range.end;
2264
+ }
2265
+ appendSegment(text.slice(cursor), cursor, text.length, false, false);
2266
+
2267
+ return { text: scanText, segments };
2268
+ }
2269
+
2270
+ function mapReplaceRegexMatch(
2271
+ segments: ReadonlyArray<RegexScanSegment>,
2272
+ scanStart: number,
2273
+ scanEnd: number,
2274
+ ): {
2275
+ start: number;
2276
+ end: number;
2277
+ recursive: boolean;
2278
+ preserveGeneratedPlaceholders: boolean;
2279
+ partialPlaceholderCut: boolean;
2280
+ cutResumeIndex: number;
2281
+ firstPlaceholderScanStart: number;
2282
+ } {
2283
+ const startSegment = findScanSegment(segments, scanStart);
2284
+ const endSegment = findScanSegment(segments, scanEnd - 1);
2285
+ const start = startSegment.generatedPlaceholder
2286
+ ? startSegment.textStart
2287
+ : startSegment.textStart + (scanStart - startSegment.scanStart);
2288
+ const end = endSegment.generatedPlaceholder
2289
+ ? endSegment.textEnd
2290
+ : endSegment.textStart + (scanEnd - endSegment.scanStart);
2291
+ // A match boundary that falls strictly inside a generated placeholder's
2292
+ // expanded value cuts the underlying secret: the snap above pulls the span out
2293
+ // to the whole `#…#` token, so the obfuscate path can leave it alone instead of
2294
+ // consuming a partial placeholder expansion.
2295
+ const partialPlaceholderCut =
2296
+ (startSegment.generatedPlaceholder && scanStart > startSegment.scanStart) ||
2297
+ (endSegment.generatedPlaceholder && scanEnd < endSegment.scanEnd);
2298
+ let recursive = false;
2299
+ let preserveGeneratedPlaceholders = false;
2300
+ // When the match straddles a placeholder, resume scanning just past the last
2301
+ // overlapping placeholder so trailing wholly-outside content (e.g. an 8-char
2302
+ // run after the secret) still gets matched instead of being consumed by the
2303
+ // straddling span. `firstPlaceholderScanStart` marks where the leading
2304
+ // wholly-outside prefix ends, so a prefix that independently matches can be
2305
+ // redacted on its own rather than skipped along with the cut span.
2306
+ let cutResumeIndex = scanStart;
2307
+ let firstPlaceholderScanStart = -1;
2308
+ for (const segment of segments) {
2309
+ if (segment.scanStart >= scanEnd || segment.scanEnd <= scanStart) continue;
2310
+ recursive ||= segment.recursive;
2311
+ preserveGeneratedPlaceholders ||= segment.generatedPlaceholder;
2312
+ if (segment.generatedPlaceholder) {
2313
+ if (firstPlaceholderScanStart === -1) firstPlaceholderScanStart = segment.scanStart;
2314
+ if (segment.scanEnd > cutResumeIndex) cutResumeIndex = segment.scanEnd;
2315
+ }
2316
+ }
2317
+ return {
2318
+ start,
2319
+ end,
2320
+ recursive,
2321
+ preserveGeneratedPlaceholders,
2322
+ partialPlaceholderCut,
2323
+ cutResumeIndex,
2324
+ firstPlaceholderScanStart,
2325
+ };
2326
+ }
2327
+
2328
+ function findScanSegment(segments: ReadonlyArray<RegexScanSegment>, scanIndex: number): RegexScanSegment {
2329
+ for (const segment of segments) {
2330
+ if (scanIndex >= segment.scanStart && scanIndex < segment.scanEnd) return segment;
2331
+ }
2332
+ throw new Error("regex match did not map to source text");
2333
+ }
2334
+
2335
+ /**
2336
+ * Extend a scan-space resume position past a consecutive run of generated
2337
+ * placeholder segments starting exactly at it, with no raw gap in between. A
2338
+ * cut-resolution resume point that happens to land precisely on the START of
2339
+ * ANOTHER placeholder must not stop there and hand it to a fresh `regex.exec`
2340
+ * attempt — the same content, scanned as an opaque adjacent placeholder run,
2341
+ * must resolve identically whether the run's LEADING member is still raw text
2342
+ * (this call is about to placeholder it) or is ALREADY a placeholder from a
2343
+ * prior call or an earlier pass of this same call. Without this, a bounded
2344
+ * regex whose reach spans two adjacent secrets plus trailing spillover bytes
2345
+ * (e.g. `[A-Z]{9}` over `ABCDEFGH` + `SECRETUV` + `A`) resolves the leading
2346
+ * secret as its own independent redaction on the FIRST obfuscate() call (a
2347
+ * genuinely raw prefix gets its own match, then the discard for the rest
2348
+ * resumes right after it), but on a LATER call — once that prefix is itself a
2349
+ * placeholder — the very first match attempt starts already inside the
2350
+ * placeholder run, cannot be prefix-narrowed at all, and its discard resume
2351
+ * point lands mid-run instead of past it, exposing a shorter tail (`SECRETUV`
2352
+ * + `A`) to a clean, un-cut match the first call never attempted. Chaining the
2353
+ * resume point through every immediately-adjacent placeholder makes both
2354
+ * calls land on the exact same next scan position.
2355
+ */
2356
+ function extendPastAdjacentPlaceholders(segments: ReadonlyArray<RegexScanSegment>, index: number): number {
2357
+ let cursor = index;
2358
+ for (;;) {
2359
+ const segment = segments.find(candidate => candidate.scanStart === cursor && candidate.generatedPlaceholder);
2360
+ if (!segment) return cursor;
2361
+ cursor = segment.scanEnd;
2362
+ }
2363
+ }
2364
+
2365
+ // Apply a fixed custom replacement across a matched span while preserving any
2366
+ // inner generated placeholders. Usually the replacement is the user's single
2367
+ // redaction marker for the whole match, so emit it for the first non-empty
2368
+ // surrounding chunk and drop later chunks. But bounded regexes can cut through
2369
+ // an already-emitted marker on the trailing side (`X#…#RED` from
2370
+ // `XSECRETUVREDACTED`), where dropping the later prefix would leave raw bytes
2371
+ // (`ACTED`) to be consumed on the next pass. Promote later chunks that are a
2372
+ // prefix of the replacement to the FULL marker so the first pass is already a
2373
+ // fixed point. The reversible placeholder stays intact in its relative
2374
+ // position.
2375
+ function redactWithFixedReplacementOutsidePlaceholders(
2376
+ text: string,
2377
+ origin: string,
2378
+ replacement: string,
2379
+ shouldPreservePlaceholder: (placeholder: string) => boolean,
2380
+ ): { text: string; origin: string } {
2381
+ let emitted = false;
2382
+ return transformOutsidePlaceholdersTracked(
2383
+ text,
2384
+ origin,
2385
+ shouldPreservePlaceholder,
2386
+ chunk => {
2387
+ if (chunk.length === 0) return "";
2388
+ if (!emitted) {
2389
+ emitted = true;
2390
+ return replacement;
2391
+ }
2392
+ return replacement.startsWith(chunk) ? replacement : "";
2393
+ },
2394
+ placeholder => placeholder,
2395
+ );
2396
+ }
2397
+
2398
+ function deobfuscateGeneratedPlaceholderRanges(
2399
+ text: string,
2400
+ start: number,
2401
+ end: number,
2402
+ ranges: ReadonlyArray<{ start: number; end: number }>,
2403
+ deobfuscateMap: ReadonlyMap<string, { secret: string; recursive: boolean }>,
2404
+ ): { text: string; recursive: boolean } {
2405
+ let result = "";
2406
+ let cursor = start;
2407
+ let recursive = false;
2408
+ for (const range of ranges) {
2409
+ if (range.end <= start || range.start >= end) continue;
2410
+ const overlapStart = Math.max(range.start, start);
2411
+ const overlapEnd = Math.min(range.end, end);
2412
+ result += text.slice(cursor, overlapStart);
2413
+ const placeholder = text.slice(overlapStart, overlapEnd);
2414
+ const mapping = lookupFriendlyPlaceholderAlias(deobfuscateMap, placeholder);
2415
+ result += mapping?.secret ?? placeholder;
2416
+ recursive ||= mapping?.recursive ?? false;
2417
+ cursor = overlapEnd;
2418
+ }
2419
+ result += text.slice(cursor, end);
2420
+ return { text: result, recursive };
2421
+ }
2422
+
2423
+ // Concatenate ONLY the deobfuscated placeholder ranges within [start, end),
2424
+ // dropping the bytes that lie outside them. Used to test whether a regex match
2425
+ // that straddles a prior-call placeholder would still match on the placeholder's
2426
+ // own (expanded) secret value alone — i.e. the surrounding raw bytes are greedy
2427
+ // spillover the match does not need, rather than content the match depends on.
2428
+ function placeholderInnerText(
2429
+ text: string,
2430
+ start: number,
2431
+ end: number,
2432
+ ranges: ReadonlyArray<{ start: number; end: number }>,
2433
+ deobfuscateMap: ReadonlyMap<string, { secret: string; recursive: boolean }>,
2434
+ ): string {
2435
+ let result = "";
2436
+ for (const range of ranges) {
2437
+ if (range.end <= start || range.start >= end) continue;
2438
+ const overlapStart = Math.max(range.start, start);
2439
+ const overlapEnd = Math.min(range.end, end);
2440
+ const placeholder = text.slice(overlapStart, overlapEnd);
2441
+ const mapping = lookupFriendlyPlaceholderAlias(deobfuscateMap, placeholder);
2442
+ result += mapping?.secret ?? placeholder;
408
2443
  }
409
2444
  return result;
410
2445
  }
411
2446
 
2447
+ // Concatenate the bytes of [start, end) that lie OUTSIDE the given (ascending,
2448
+ // non-overlapping) placeholder ranges. Used to test whether a regex match that
2449
+ // straddles a prior-call placeholder would still match on its surrounding bytes
2450
+ // alone — i.e. those bytes are genuinely new content to redact rather than a
2451
+ // match that only exists because the deobfuscated placeholder bridges them.
2452
+ function textOutsidePlaceholderRanges(
2453
+ text: string,
2454
+ start: number,
2455
+ end: number,
2456
+ ranges: ReadonlyArray<{ start: number; end: number }>,
2457
+ ): string {
2458
+ let result = "";
2459
+ let cursor = start;
2460
+ for (const range of ranges) {
2461
+ if (range.end <= start || range.start >= end) continue;
2462
+ const overlapStart = Math.max(range.start, start);
2463
+ const overlapEnd = Math.min(range.end, end);
2464
+ result += text.slice(cursor, overlapStart);
2465
+ cursor = overlapEnd;
2466
+ }
2467
+ result += text.slice(cursor, end);
2468
+ return result;
2469
+ }
2470
+
2471
+ // Like `textOutsidePlaceholderRanges`, but tests each outside chunk against
2472
+ // `regex` in its REAL context instead of on an isolated slice — tried in BOTH
2473
+ // the literal `#…#` placeholder-token text AND the EXPANDED scan context
2474
+ // (placeholder resolved to its secret value), since either can be the reason a
2475
+ // chunk independently requires redaction:
2476
+ // - Literal-token context matters when the placeholder TOKEN's own non-word
2477
+ // boundary is what completes a boundary-sensitive pattern, e.g. a prefix
2478
+ // "ABCDEFGH" next to a placeholder token matches `\b[A-Z]{8}\b` because the
2479
+ // token's leading `#` is a non-word byte — but that boundary disappears
2480
+ // once the placeholder expands into more `[A-Z]` bytes with no separator.
2481
+ // - Expanded scan context matters when a lookbehind/lookahead only resolves
2482
+ // once the neighboring placeholder is expanded, e.g. a prior plain
2483
+ // placeholder for `ABCDEFGH` next to raw `SECRET`, matched by
2484
+ // `(?<=ABCDEFGH)SECRET`: the literal placeholder token before `SECRET`
2485
+ // never satisfies the lookbehind, so literal-context alone wrongly reports
2486
+ // no independent match.
2487
+ // A match only counts when it lies ENTIRELY within one outside chunk (in
2488
+ // whichever context it was tested); a match that reaches into the
2489
+ // placeholder itself is not evidence the outside chunk independently
2490
+ // requires redaction.
2491
+ function outsidePlaceholderRangesAnyIndependentlyMatch(
2492
+ text: string,
2493
+ scanText: string,
2494
+ segments: ReadonlyArray<RegexScanSegment>,
2495
+ start: number,
2496
+ end: number,
2497
+ ranges: ReadonlyArray<{ start: number; end: number }>,
2498
+ regex: RegExp,
2499
+ ): boolean {
2500
+ // A text-space outside chunk lies entirely within one non-placeholder scan
2501
+ // segment (placeholder ranges are exactly the gaps between such segments),
2502
+ // so its scan-space span is a fixed offset from its text-space span.
2503
+ const toScanSpace = (chunkStart: number, chunkEnd: number): [number, number] | undefined => {
2504
+ for (const segment of segments) {
2505
+ if (segment.generatedPlaceholder || segment.textStart > chunkStart || segment.textEnd < chunkEnd) continue;
2506
+ const offset = segment.scanStart - segment.textStart;
2507
+ return [chunkStart + offset, chunkEnd + offset];
2508
+ }
2509
+ return undefined;
2510
+ };
2511
+ const chunkIndependentlyMatches = (chunkStart: number, chunkEnd: number): boolean => {
2512
+ if (chunkMatchesInSourceContext(text, chunkStart, chunkEnd, regex)) return true;
2513
+ const scanSpan = toScanSpace(chunkStart, chunkEnd);
2514
+ return scanSpan !== undefined && chunkMatchesInSourceContext(scanText, scanSpan[0], scanSpan[1], regex);
2515
+ };
2516
+ let cursor = start;
2517
+ for (const range of ranges) {
2518
+ if (range.end <= start || range.start >= end) continue;
2519
+ const overlapStart = Math.max(range.start, start);
2520
+ const overlapEnd = Math.min(range.end, end);
2521
+ if (cursor < overlapStart && chunkIndependentlyMatches(cursor, overlapStart)) return true;
2522
+ cursor = overlapEnd;
2523
+ }
2524
+ return cursor < end && chunkIndependentlyMatches(cursor, end);
2525
+ }
2526
+
2527
+ // Whether `regex` (global) has a match fully contained in [chunkStart, chunkEnd)
2528
+ // when run against the full `text` — so lookbehind/lookahead see the actual
2529
+ // surrounding bytes rather than an isolated slice's edges.
2530
+ function chunkMatchesInSourceContext(text: string, chunkStart: number, chunkEnd: number, regex: RegExp): boolean {
2531
+ regex.lastIndex = chunkStart;
2532
+ for (;;) {
2533
+ const found = regex.exec(text);
2534
+ if (found === null || found.index >= chunkEnd) return false;
2535
+ const matchEnd = found.index + found[0].length;
2536
+ if (matchEnd <= chunkEnd) return true;
2537
+ regex.lastIndex = found[0].length === 0 ? found.index + 1 : matchEnd;
2538
+ }
2539
+ }
2540
+
2541
+ function firstOutsidePlaceholderRange(
2542
+ start: number,
2543
+ end: number,
2544
+ ranges: ReadonlyArray<{ start: number; end: number }>,
2545
+ ): { start: number; end: number } | undefined {
2546
+ let cursor = start;
2547
+ for (const range of ranges) {
2548
+ if (range.end <= start || range.start >= end) continue;
2549
+ const overlapStart = Math.max(range.start, start);
2550
+ const overlapEnd = Math.min(range.end, end);
2551
+ if (cursor < overlapStart) return { start: cursor, end: overlapStart };
2552
+ cursor = overlapEnd;
2553
+ }
2554
+ return cursor < end ? { start: cursor, end } : undefined;
2555
+ }
2556
+
2557
+ function countOutsidePlaceholderRanges(
2558
+ start: number,
2559
+ end: number,
2560
+ ranges: ReadonlyArray<{ start: number; end: number }>,
2561
+ ): number {
2562
+ let count = 0;
2563
+ let cursor = start;
2564
+ for (const range of ranges) {
2565
+ if (range.end <= start || range.start >= end) continue;
2566
+ const overlapStart = Math.max(range.start, start);
2567
+ const overlapEnd = Math.min(range.end, end);
2568
+ if (cursor < overlapStart) count++;
2569
+ cursor = overlapEnd;
2570
+ }
2571
+ if (cursor < end) count++;
2572
+ return count;
2573
+ }
2574
+
2575
+ function replaceRange(text: string, start: number, end: number, replacement: string): string {
2576
+ return text.slice(0, start) + replacement + text.slice(end);
2577
+ }
2578
+
2579
+ /** Deep-walk an object, transforming all string values. */
2580
+ function deepWalkStrings<T>(obj: T, transform: (s: string) => string): T {
2581
+ if (typeof obj === "string") {
2582
+ return transform(obj) as unknown as T;
2583
+ }
2584
+ if (Array.isArray(obj)) {
2585
+ let changed = false;
2586
+ const result = obj.map(item => {
2587
+ const transformed = deepWalkStrings(item, transform);
2588
+ if (transformed !== item) changed = true;
2589
+ return transformed;
2590
+ });
2591
+ return (changed ? result : obj) as unknown as T;
2592
+ }
2593
+ if (obj !== null && typeof obj === "object" && isPlainRecord(obj)) {
2594
+ let changed = false;
2595
+ const result: Record<string, unknown> = {};
2596
+ for (const key of Object.keys(obj)) {
2597
+ const value = (obj as Record<string, unknown>)[key];
2598
+ const transformed = deepWalkStrings(value, transform);
2599
+ if (transformed !== value) changed = true;
2600
+ result[key] = transformed;
2601
+ }
2602
+ return (changed ? result : obj) as T;
2603
+ }
2604
+ return obj;
2605
+ }
2606
+
2607
+ function isPlainRecord(obj: object): obj is Record<string, unknown> {
2608
+ const prototype = Object.getPrototypeOf(obj);
2609
+ return prototype === Object.prototype || prototype === null;
2610
+ }
2611
+
2612
+ function collectJsonRegexSecretValues(obfuscator: SecretObfuscator, value: JsonValue): Set<string> {
2613
+ const values = new Set<string>();
2614
+ const collect = (item: JsonValue): void => {
2615
+ if (typeof item === "string") {
2616
+ for (const secretValue of obfuscator.collectRegexSecretValuesForObfuscation(item)) {
2617
+ values.add(secretValue);
2618
+ }
2619
+ return;
2620
+ }
2621
+ if (Array.isArray(item)) {
2622
+ for (const child of item) collect(child);
2623
+ return;
2624
+ }
2625
+ if (item !== null && typeof item === "object") {
2626
+ for (const child of Object.values(item)) {
2627
+ if (child !== undefined) collect(child);
2628
+ }
2629
+ }
2630
+ };
2631
+ collect(value);
2632
+ return values;
2633
+ }
2634
+
412
2635
  /**
413
2636
  * Map every string in arbitrary JSON. Used ONLY for tool-call arguments, whose
414
2637
  * shape is model-authored and not known ahead of time. No other caller may walk