@parcha/agentrun-serve 0.1.0-beta.56 → 0.1.0-beta.58

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 (312) hide show
  1. package/CHANGELOG.md +281 -0
  2. package/README.md +246 -66
  3. package/dist/acp-package.d.ts +2 -0
  4. package/dist/adapters.d.ts +15 -3
  5. package/dist/adapters.js +17 -6
  6. package/dist/agent-package.d.ts +3 -3
  7. package/dist/agent-package.js +17 -8
  8. package/dist/api-keys.d.ts +76 -0
  9. package/dist/api-keys.js +217 -0
  10. package/dist/api-only.d.ts +15 -5
  11. package/dist/api-only.js +32 -8
  12. package/dist/box-paths.d.ts +4 -3
  13. package/dist/box-paths.js +7 -4
  14. package/dist/browser-hold.d.ts +23 -0
  15. package/dist/browser-hold.js +48 -0
  16. package/dist/browser-person.d.ts +3 -0
  17. package/dist/browser-person.js +4 -1
  18. package/dist/chatgpt-plan.d.ts +18 -8
  19. package/dist/chatgpt-plan.js +91 -34
  20. package/dist/claude-login-token.d.ts +117 -0
  21. package/dist/claude-login-token.js +737 -0
  22. package/dist/claude-login.d.ts +120 -22
  23. package/dist/claude-login.js +347 -63
  24. package/dist/claude-sign-in.d.ts +17 -6
  25. package/dist/claude-sign-in.js +42 -39
  26. package/dist/cli.js +33 -10
  27. package/dist/composio-made.d.ts +4 -0
  28. package/dist/composio-made.js +4 -0
  29. package/dist/composio-preset.d.ts +43 -2
  30. package/dist/composio-preset.js +92 -4
  31. package/dist/computer-attach.d.ts +35 -17
  32. package/dist/computer-attach.js +301 -49
  33. package/dist/computer-held-routes.d.ts +5 -4
  34. package/dist/computer-held-routes.js +29 -8
  35. package/dist/computer-held.d.ts +6 -1
  36. package/dist/computer-held.js +14 -0
  37. package/dist/computer-record.js +4 -2
  38. package/dist/computer-viewer.js +4 -1
  39. package/dist/computers.d.ts +10 -10
  40. package/dist/computers.js +5 -5
  41. package/dist/console/assets/{backend-Boh7tC4i.js → backend-CSAWUqtZ.js} +19 -19
  42. package/dist/console/assets/index-Bwn1oTem.css +1 -0
  43. package/dist/console/assets/index-DPh29cyz.js +747 -0
  44. package/dist/console/index.html +2 -2
  45. package/dist/decisions.d.ts +1 -1
  46. package/dist/decisions.js +1 -1
  47. package/dist/dial-relay.d.ts +25 -4
  48. package/dist/dial-relay.js +129 -1
  49. package/dist/docker-host-addresses.d.ts +22 -11
  50. package/dist/docker-host-addresses.js +38 -26
  51. package/dist/env-switch.d.ts +8 -0
  52. package/dist/env-switch.js +31 -2
  53. package/dist/errors.d.ts +1 -0
  54. package/dist/errors.js +2 -0
  55. package/dist/fal-price.d.ts +56 -20
  56. package/dist/fal-price.js +400 -90
  57. package/dist/handed-values.d.ts +43 -1
  58. package/dist/handed-values.js +119 -2
  59. package/dist/harness-archive.d.ts +13 -10
  60. package/dist/harness-archive.js +60 -34
  61. package/dist/harness-check.d.ts +3 -1
  62. package/dist/harness-check.js +25 -46
  63. package/dist/harness-keys.d.ts +73 -27
  64. package/dist/harness-keys.js +194 -61
  65. package/dist/harness-recipe-entry.d.ts +8 -7
  66. package/dist/harness-recipe-entry.js +21 -32
  67. package/dist/harness-recipe.d.ts +38 -25
  68. package/dist/harness-recipe.js +49 -36
  69. package/dist/harness-sign-in.js +12 -23
  70. package/dist/harness-table.d.ts +10 -3
  71. package/dist/harness-table.js +26 -21
  72. package/dist/harness.d.ts +1 -0
  73. package/dist/harness.js +17 -5
  74. package/dist/hosted-search.d.ts +1 -1
  75. package/dist/hosted-search.js +1 -2
  76. package/dist/import-worker.d.ts +4 -2
  77. package/dist/import-worker.js +9 -2
  78. package/dist/in-flight.d.ts +26 -0
  79. package/dist/in-flight.js +52 -0
  80. package/dist/key-refusal.d.ts +9 -4
  81. package/dist/key-refusal.js +107 -25
  82. package/dist/learn/ledger.d.ts +170 -0
  83. package/dist/learn/ledger.js +218 -0
  84. package/dist/learn/promote.d.ts +3 -3
  85. package/dist/learn/promote.js +1 -1
  86. package/dist/learn/secrets.d.ts +13 -3
  87. package/dist/learn/secrets.js +118 -17
  88. package/dist/learning-cost.d.ts +28 -23
  89. package/dist/learning-cost.js +19 -23
  90. package/dist/link-check.js +1 -1
  91. package/dist/live-reach.d.ts +2 -0
  92. package/dist/live-reach.js +2 -0
  93. package/dist/llms.txt +1 -1
  94. package/dist/marketplace.d.ts +77 -0
  95. package/dist/marketplace.js +224 -0
  96. package/dist/mcp-api-only-origins.d.ts +35 -0
  97. package/dist/mcp-api-only-origins.js +57 -0
  98. package/dist/mcp-gateway.d.ts +7 -5
  99. package/dist/mcp-gateway.js +9 -12
  100. package/dist/mcp-secret-writes.js +5 -3
  101. package/dist/mcp-servers-store.d.ts +28 -6
  102. package/dist/mcp-servers-store.js +53 -4
  103. package/dist/media-duration.d.ts +3 -0
  104. package/dist/media-duration.js +90 -9
  105. package/dist/micro.d.ts +2 -0
  106. package/dist/micro.js +2 -0
  107. package/dist/model-form.d.ts +5 -4
  108. package/dist/model-form.js +11 -7
  109. package/dist/model.d.ts +10 -6
  110. package/dist/model.js +16 -11
  111. package/dist/monid.d.ts +14 -1
  112. package/dist/monid.js +103 -26
  113. package/dist/ollama-provider.d.ts +7 -1
  114. package/dist/ollama-provider.js +14 -2
  115. package/dist/openapi.js +210 -37
  116. package/dist/otel/genai.d.ts +65 -0
  117. package/dist/otel/genai.js +116 -0
  118. package/dist/otel/tracer.d.ts +55 -0
  119. package/dist/otel/tracer.js +308 -0
  120. package/dist/page.d.ts +2 -0
  121. package/dist/page.js +3 -1
  122. package/dist/paid-calls.d.ts +1 -32
  123. package/dist/paid-calls.js +4 -39
  124. package/dist/place-offers.d.ts +4 -0
  125. package/dist/place-offers.js +7 -4
  126. package/dist/provider.d.ts +11 -2
  127. package/dist/provider.js +59 -8
  128. package/dist/redact/match.d.ts +30 -3
  129. package/dist/redact/match.js +422 -41
  130. package/dist/refusal-words.d.ts +11 -0
  131. package/dist/refusal-words.js +11 -0
  132. package/dist/run-census.d.ts +67 -0
  133. package/dist/run-census.js +240 -0
  134. package/dist/run-computer.d.ts +6 -3
  135. package/dist/run-computer.js +4 -1
  136. package/dist/run-cut.d.ts +15 -6
  137. package/dist/run-cut.js +30 -9
  138. package/dist/run-files.d.ts +3 -0
  139. package/dist/run-files.js +3 -0
  140. package/dist/run-launch.d.ts +12 -1
  141. package/dist/run-launch.js +9 -10
  142. package/dist/run-learning-served.d.ts +4 -2
  143. package/dist/run-model-key.d.ts +8 -0
  144. package/dist/run-model-key.js +31 -1
  145. package/dist/run-reads-over-store.d.ts +24 -6
  146. package/dist/run-reads-over-store.js +67 -23
  147. package/dist/run-recordings.d.ts +5 -1
  148. package/dist/run-recordings.js +39 -14
  149. package/dist/run-timeline-served.d.ts +2 -0
  150. package/dist/run-timeline-served.js +2 -2
  151. package/dist/run-usage.d.ts +1 -0
  152. package/dist/run-view.d.ts +1 -2
  153. package/dist/run-view.js +2 -1
  154. package/dist/runnable.d.ts +10 -1
  155. package/dist/runnable.js +20 -8
  156. package/dist/runner/adapter-loop.d.ts +11 -0
  157. package/dist/runner/adapter-loop.js +58 -26
  158. package/dist/runner/adapter-run-spec.d.ts +55 -11
  159. package/dist/runner/adapter-run-spec.js +183 -52
  160. package/dist/runner/adapters/pi.js +25 -3
  161. package/dist/runner/beside.d.ts +9 -2
  162. package/dist/runner/beside.js +27 -9
  163. package/dist/runner/big-writes.js +1 -10
  164. package/dist/runner/claim-secrets.d.ts +27 -10
  165. package/dist/runner/claim-secrets.js +49 -22
  166. package/dist/runner/cli-run-spec.d.ts +6 -0
  167. package/dist/runner/cli-run-spec.js +97 -38
  168. package/dist/runner/computer-journal.d.ts +9 -1
  169. package/dist/runner/computer-journal.js +26 -0
  170. package/dist/runner/conformance/harness.js +1 -1
  171. package/dist/runner/feedback-round.js +3 -0
  172. package/dist/runner/harness-loops.d.ts +6 -5
  173. package/dist/runner/harness-loops.js +7 -7
  174. package/dist/runner/held.d.ts +24 -6
  175. package/dist/runner/journal-kinds.d.ts +5 -0
  176. package/dist/runner/journal-kinds.js +5 -0
  177. package/dist/runner/main.d.ts +11 -6
  178. package/dist/runner/main.js +19 -9
  179. package/dist/runner/mcp-claim.js +31 -3
  180. package/dist/runner/mcp-run.d.ts +9 -0
  181. package/dist/runner/mcp-run.js +11 -5
  182. package/dist/runner/model-route.d.ts +17 -0
  183. package/dist/runner/model-route.js +120 -2
  184. package/dist/runner/presence.d.ts +11 -1
  185. package/dist/runner/presence.js +7 -0
  186. package/dist/runner/reflect-hook.js +1 -1
  187. package/dist/runner/run-child-host.d.ts +22 -8
  188. package/dist/runner/run-child-host.js +128 -41
  189. package/dist/runner/run-child.js +5 -2
  190. package/dist/runner/runner.d.ts +17 -3
  191. package/dist/runner/runner.js +130 -34
  192. package/dist/runner/save-slots.js +2 -1
  193. package/dist/runner/store-port.d.ts +1 -1
  194. package/dist/runner/store-secrets.d.ts +6 -0
  195. package/dist/runner/store-secrets.js +49 -7
  196. package/dist/runner/subagent.d.ts +11 -0
  197. package/dist/runner/subagent.js +16 -38
  198. package/dist/runner/workflow-first.d.ts +35 -15
  199. package/dist/runner/workflow-first.js +38 -25
  200. package/dist/runner/workflow-spec.js +4 -3
  201. package/dist/runs-over-store.d.ts +15 -6
  202. package/dist/runs-over-store.js +33 -21
  203. package/dist/scrub-command.d.ts +73 -0
  204. package/dist/scrub-command.js +307 -0
  205. package/dist/secret-cut.d.ts +73 -0
  206. package/dist/secret-cut.js +291 -4
  207. package/dist/secret-rules.d.ts +143 -0
  208. package/dist/secret-rules.js +2564 -0
  209. package/dist/secrets.d.ts +7 -0
  210. package/dist/secrets.js +20 -0
  211. package/dist/server-keys.d.ts +20 -0
  212. package/dist/server-keys.js +25 -6
  213. package/dist/server.d.ts +65 -13
  214. package/dist/server.js +470 -157
  215. package/dist/skills/agentrun-serve/SKILL.md +4 -0
  216. package/dist/slots.d.ts +9 -0
  217. package/dist/slots.js +45 -0
  218. package/dist/store/index.d.ts +1 -1
  219. package/dist/store/index.js +1 -1
  220. package/dist/store/journal/storage.js +1 -1
  221. package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
  222. package/dist/store/postgres/migrations/0028_learning_ledger.sql +44 -0
  223. package/dist/store/schema.d.ts +23 -0
  224. package/dist/store/schema.js +58 -2
  225. package/dist/store/seal.d.ts +6 -1
  226. package/dist/store/seal.js +5 -3
  227. package/dist/store/sqlite/run-launch.d.ts +9 -1
  228. package/dist/store/sqlite/run-launch.js +24 -13
  229. package/dist/store/sqlite/run-patch.d.ts +12 -2
  230. package/dist/store/sqlite/run-patch.js +42 -5
  231. package/dist/store/sqlite/store.js +105 -12
  232. package/dist/store/store.d.ts +57 -2
  233. package/dist/store/store.js +20 -0
  234. package/dist/store-reads/learning.d.ts +84 -124
  235. package/dist/store-reads/learning.js +210 -274
  236. package/dist/store-view.d.ts +3 -0
  237. package/dist/store-view.js +3 -3
  238. package/dist/suggestions.js +3 -2
  239. package/dist/template-eval.d.ts +39 -9
  240. package/dist/template-eval.js +191 -60
  241. package/dist/template-import.js +6 -2
  242. package/dist/templates.d.ts +10 -2
  243. package/dist/templates.js +28 -8
  244. package/dist/timeline/labels.d.ts +6 -4
  245. package/dist/timeline/labels.js +18 -6
  246. package/dist/timeline/timeline.d.ts +5 -0
  247. package/dist/timeline/timeline.js +14 -8
  248. package/dist/timeline/util.js +1 -1
  249. package/dist/timeline/web-calls.d.ts +22 -7
  250. package/dist/timeline/web-calls.js +106 -35
  251. package/dist/tool-gate.d.ts +42 -3
  252. package/dist/tool-gate.js +66 -5
  253. package/dist/tool-keys-store.d.ts +2 -4
  254. package/dist/tool-keys-store.js +5 -21
  255. package/dist/tool-keys.d.ts +2 -2
  256. package/dist/tool-keys.js +2 -2
  257. package/dist/tool-list.js +1 -1
  258. package/dist/tool-plane.d.ts +2 -2
  259. package/dist/tool-plane.js +35 -33
  260. package/dist/tools.js +13 -7
  261. package/dist/trigger-targets.d.ts +4 -0
  262. package/dist/trigger-targets.js +9 -2
  263. package/dist/triggers.d.ts +67 -4
  264. package/dist/triggers.js +171 -22
  265. package/dist/until.d.ts +3 -0
  266. package/dist/until.js +11 -0
  267. package/dist/web-fake.d.ts +1 -3
  268. package/dist/web-fake.js +7 -18
  269. package/dist/web-fetch.d.ts +6 -72
  270. package/dist/web-fetch.js +11 -265
  271. package/dist/web-run.d.ts +31 -0
  272. package/dist/web-run.js +90 -0
  273. package/dist/web-save.d.ts +10 -13
  274. package/dist/web-save.js +13 -55
  275. package/dist/web-search.d.ts +9 -127
  276. package/dist/web-search.js +23 -259
  277. package/dist/wires.d.ts +13 -0
  278. package/dist/wires.js +11 -0
  279. package/dist/workflow-run.d.ts +5 -4
  280. package/dist/workflow-run.js +5 -4
  281. package/dist/workspace/attached-computers.d.ts +32 -9
  282. package/dist/workspace/attached-computers.js +77 -13
  283. package/dist/workspace/computer-deps.d.ts +8 -2
  284. package/dist/workspace/computer-deps.js +10 -3
  285. package/dist/workspace/computer-for-claim.d.ts +12 -1
  286. package/dist/workspace/computer-for-claim.js +83 -11
  287. package/dist/workspace/computer-kept.d.ts +3 -0
  288. package/dist/workspace/computer-kept.js +11 -2
  289. package/dist/workspace/index.d.ts +1 -1
  290. package/dist/workspace/index.js +1 -1
  291. package/dist/workspace/kind-skills.d.ts +4 -0
  292. package/dist/workspace/kind-skills.js +9 -0
  293. package/dist/workspace/paired-route.d.ts +91 -0
  294. package/dist/workspace/paired-route.js +153 -0
  295. package/dist/workspace/synced-folder.js +3 -4
  296. package/dist/wrapped.d.ts +20 -6
  297. package/dist/wrapped.js +66 -25
  298. package/package.json +17 -11
  299. package/skills/agentrun-serve/SKILL.md +4 -0
  300. package/templates/README.md +32 -10
  301. package/dist/console/assets/index-CQ3cJRzI.js +0 -746
  302. package/dist/console/assets/index-DKkaS3a0.css +0 -1
  303. package/dist/exa-keyless.d.ts +0 -34
  304. package/dist/exa-keyless.js +0 -118
  305. package/dist/firecrawl-keyless.d.ts +0 -41
  306. package/dist/firecrawl-keyless.js +0 -152
  307. package/dist/keyless-mcp.d.ts +0 -36
  308. package/dist/keyless-mcp.js +0 -127
  309. package/dist/text-urls.d.ts +0 -38
  310. package/dist/text-urls.js +0 -170
  311. package/dist/web-fetch-direct.d.ts +0 -119
  312. package/dist/web-fetch-direct.js +0 -190
@@ -10,9 +10,13 @@
10
10
  // (a credential parameter's value cut in place, the rest of the held value past a line break), and the rest would then
11
11
  // read as no value at all. A held value found in a view is cut only when every character the view read of it comes
12
12
  // from text a cut covers; otherwise the text is withheld whole.
13
- /** The characters the shape scanners have visited in this process: the text each prefix search passes over, each run
14
- * read, each guard read back. A test sets it to 0, redacts, and reads it: a scanner that read a run once per start
15
- * inside it would count the square of the run's length. */
13
+ /** The characters the redaction pipeline has visited in this process: the text each prefix search passes over, each
14
+ * run read, each guard read back (the shape scanners); each form, hex, folded and plain needle's search, and the text
15
+ * it passes over (find); each view built (its special characters found, its blanks, its decoders' reads, its non-ASCII
16
+ * stretches squeezed, each cut its cover passes), lowered and flattened; each covered stretch a find in a view is
17
+ * checked against (inside); the finds merged and the text cut; and
18
+ * key-refusal.ts's reads of a text in pieces. A test sets it to 0, redacts, and reads it: a stage that read again from
19
+ * the start at each find would count the text once per find. */
16
20
  export const scanned = { chars: 0 };
17
21
  // ---- Character classes, as the regexes they replace wrote them.
18
22
  /** JavaScript's \s. */
@@ -68,6 +72,12 @@ function seek(hay, prefix, from) {
68
72
  scanned.chars += (at < 0 ? hay.length : at + prefix.length) - from;
69
73
  return at < 0 ? Number.POSITIVE_INFINITY : at;
70
74
  }
75
+ /** Where `needle` next stands in `hay` at or after `from`, or -1: a needle's search, counted as seek counts a prefix's. */
76
+ function find(hay, needle, from) {
77
+ const at = hay.indexOf(needle, from);
78
+ scanned.chars += Math.max(0, (at < 0 ? hay.length : at + needle.length) - from);
79
+ return at;
80
+ }
71
81
  /** `text` with A-Z lowered and nothing else changed, so each offset is the same in both. */
72
82
  export const asciiLower = (text) => text.replace(/[A-Z]+/g, (upper) => upper.toLowerCase());
73
83
  const none = (next) => ({ start: -1, end: -1, next });
@@ -90,29 +100,241 @@ const prefixFixed = (kind, prefixes, cls, count) => ({
90
100
  prefixes,
91
101
  scan: (text, at, prefix) => (fixed(text, at + prefix.length, count, cls) ? { start: at, end: at + prefix.length + count, next: at + 1 } : none(at + 1)),
92
102
  });
93
- const PRIVATE_KEY_HEADER = only(` ${UPPER}${DIGIT}`);
94
- /** A private key block's header: "-----BEGIN", its key type (RSA, EC, OPENSSH, ENCRYPTED) at most 64 characters each
95
- * side of "PRIVATE KEY", then "-----". Each start reads at most 140 characters. */
103
+ const PRIVATE_KEY_TYPE = only(` ${UPPER}${DIGIT}`);
104
+ /** Where a private key block's armor line at `at` ends, past its closing "-----", or -1. Its opening ("-----BEGIN" or
105
+ * "-----END", `head` characters) is already read; then its key type (RSA, EC, OPENSSH, ENCRYPTED, an armored key's
106
+ * "PGP ... BLOCK"), at most 64 characters each side of "PRIVATE KEY", then "-----". It reads at most 140 characters,
107
+ * and 11 at each place "PRIVATE KEY" may stand. */
108
+ function armorEnd(text, at, head) {
109
+ const from = at + head;
110
+ let end = from;
111
+ while (end < text.length && end - from < 140 && PRIVATE_KEY_TYPE(text.charCodeAt(end)))
112
+ end++;
113
+ scanned.chars += end - from + 1;
114
+ const length = end - from;
115
+ if (length >= 140 || !text.startsWith("-----", end))
116
+ return -1;
117
+ for (let i = Math.max(0, length - 75); i <= Math.min(64, length - 11); i++) {
118
+ scanned.chars += 11;
119
+ if (text.startsWith("PRIVATE KEY", from + i))
120
+ return end + 5;
121
+ }
122
+ return -1;
123
+ }
124
+ /** The most characters between a key's header and its END line, and the most base64 characters on a body line. */
125
+ const KEY_BODY_MAX = 16384;
126
+ /** The most lines of a key's body read when no END line is in reach. */
127
+ const KEY_LINES = 1024;
128
+ /** The most characters of a legacy PEM's or armored key's header line after its colon. */
129
+ const KEY_HEADER_MAX = 256;
130
+ const KEY_BASE64 = only(`${ALNUM}+=`);
131
+ /** A header line's text: any character but a line break, a backslash, a quote or a "<". */
132
+ const KEY_HEADER_TEXT = (code) => code !== 10 && code !== 13 && code !== 92 && code !== 34 && code !== 39 && code !== 60;
133
+ const KEY_HEADER_WORDS = ["Proc-Type:", "DEK-Info:", "Version:", "Comment:"];
134
+ /** How many backslashes stand at `at`, at most 9 read: an escape takes 1 to 8 (a string four deep); 9 is too many. */
135
+ function backslashes(text, at) {
136
+ let end = at;
137
+ while (end < text.length && end - at < 9 && text.charCodeAt(end) === 92)
138
+ end++;
139
+ scanned.chars += end - at + 1;
140
+ return end - at;
141
+ }
142
+ /** Where an escape at `at` of one of `chars`, after 1 to 8 backslashes, ends, or -1. */
143
+ function escapeEnd(text, at, chars) {
144
+ const count = backslashes(text, at);
145
+ const ok = count >= 1 && count <= 8 && at + count < text.length && chars.includes(text[at + count]);
146
+ return ok ? at + count + 1 : -1;
147
+ }
148
+ /** Where each spelling of a line break at `at` ends, in the order the key's reading tries them: CRLF, then CR alone;
149
+ * LF; an escaped CR followed by an escaped LF, then the CR alone; an escaped LF. */
150
+ function keyBreaks(text, at) {
151
+ if (at >= text.length)
152
+ return [];
153
+ scanned.chars += 2;
154
+ const code = text.charCodeAt(at);
155
+ if (code === 13)
156
+ return text.charCodeAt(at + 1) === 10 ? [at + 2, at + 1] : [at + 1];
157
+ if (code === 10)
158
+ return [at + 1];
159
+ if (code !== 92)
160
+ return [];
161
+ const count = backslashes(text, at);
162
+ if (count > 8)
163
+ return [];
164
+ const after = text[at + count];
165
+ if (after === "n")
166
+ return [at + count + 1];
167
+ if (after !== "r")
168
+ return [];
169
+ const lf = escapeEnd(text, at + count + 1, "n");
170
+ return lf >= 0 ? [lf, at + count + 1] : [at + count + 1];
171
+ }
172
+ /** Where the blanks at `at` end: spaces, tabs and escaped tabs. */
173
+ function keyBlanks(text, at) {
174
+ let end = at;
175
+ for (;;) {
176
+ const code = text.charCodeAt(end);
177
+ scanned.chars += 1;
178
+ if (code === 32 || code === 9)
179
+ end++;
180
+ else if (code !== 92)
181
+ return end;
182
+ else {
183
+ const tab = escapeEnd(text, end, "t");
184
+ if (tab < 0)
185
+ return end;
186
+ end = tab;
187
+ }
188
+ }
189
+ }
190
+ /** Where the base64 at `at` ends: at most KEY_BODY_MAX of its characters, each a letter, a digit, "+", "=" or a "/"
191
+ * (maybe escaped, "\/"). */
192
+ function keyBase64(text, at) {
193
+ let end = at;
194
+ for (let count = 0; count < KEY_BODY_MAX; count++) {
195
+ const code = text.charCodeAt(end);
196
+ scanned.chars += 1;
197
+ if (KEY_BASE64(code) || code === 47)
198
+ end++;
199
+ else if (code !== 92)
200
+ return end;
201
+ else {
202
+ const slash = escapeEnd(text, end, "/");
203
+ if (slash < 0)
204
+ return end;
205
+ end = slash;
206
+ }
207
+ }
208
+ return end;
209
+ }
210
+ /** Where a legacy PEM's or armored key's header line at `at` ends ("Proc-Type:", "DEK-Info:", "Version:" or "Comment:",
211
+ * then at most KEY_HEADER_MAX characters of its text), or -1. */
212
+ function keyHeaderLine(text, at) {
213
+ for (const word of KEY_HEADER_WORDS) {
214
+ scanned.chars += word.length;
215
+ if (!text.startsWith(word, at))
216
+ continue;
217
+ const from = at + word.length;
218
+ let end = from;
219
+ while (end < text.length && end - from < KEY_HEADER_MAX && KEY_HEADER_TEXT(text.charCodeAt(end)))
220
+ end++;
221
+ scanned.chars += end - from + 1;
222
+ return end;
223
+ }
224
+ return -1;
225
+ }
226
+ /** Whether a body line may end at `at`: a line break or a closing quote (either maybe escaped), a tag's "<", the text's
227
+ * end. */
228
+ function keyLineEnds(text, at) {
229
+ if (at >= text.length)
230
+ return true;
231
+ scanned.chars += 1;
232
+ const code = text.charCodeAt(at);
233
+ if (code === 10 || code === 13 || code === 34 || code === 39 || code === 60)
234
+ return true;
235
+ return code === 92 && escapeEnd(text, at, `rn"'`) >= 0;
236
+ }
237
+ /** Where a body line at `at` ends, or -1: blanks, then base64, or a header line, or nothing, then blanks, at a place a
238
+ * line may end. Each is tried in that order and the first that ends at such a place is the line. */
239
+ function keyLine(text, at) {
240
+ const start = keyBlanks(text, at);
241
+ const base64 = keyBase64(text, start);
242
+ if (base64 > start) {
243
+ const end = keyBlanks(text, base64);
244
+ if (keyLineEnds(text, end))
245
+ return end;
246
+ }
247
+ const header = keyHeaderLine(text, start);
248
+ if (header >= 0) {
249
+ const end = keyBlanks(text, header);
250
+ if (keyLineEnds(text, end))
251
+ return end;
252
+ }
253
+ return keyLineEnds(text, start) ? start : -1;
254
+ }
255
+ /** Where a line break at `at` and the body line after it end, the break's spellings tried in order, or -1. */
256
+ function keyNextLine(text, at) {
257
+ for (const start of keyBreaks(text, at)) {
258
+ const end = keyLine(text, start);
259
+ if (end >= 0)
260
+ return end;
261
+ }
262
+ return -1;
263
+ }
264
+ /** Where the END line of a block whose header ends at `header` ends, or -1: the first "-----" after the header, at most
265
+ * KEY_BODY_MAX characters on, must start one. The search stops at that "-----", so a reading from one header never
266
+ * passes the next header's dashes. */
267
+ function keyEndLine(text, header) {
268
+ const dashes = find(text, "-----", header);
269
+ if (dashes < 0 || dashes - header > KEY_BODY_MAX)
270
+ return -1;
271
+ scanned.chars += 3;
272
+ return text.startsWith("-----END", dashes) ? armorEnd(text, dashes, 8) : -1;
273
+ }
274
+ /** Where the body lines after a header that ends at `header` end, or -1 when not even an empty line stands there: a
275
+ * first line (after a line break, or glued to the header), then at most KEY_LINES - 1 more, each after a line break,
276
+ * as long as one follows. */
277
+ function keyBodyLines(text, header) {
278
+ let end = keyNextLine(text, header);
279
+ if (end < 0)
280
+ end = keyLine(text, header);
281
+ for (let lines = 1; end >= 0 && lines < KEY_LINES; lines++) {
282
+ const next = keyNextLine(text, end);
283
+ if (next < 0)
284
+ break;
285
+ end = next;
286
+ }
287
+ return end;
288
+ }
289
+ /** A private key block, whole: its BEGIN line through its body to its END line, the key itself, not its header alone (a
290
+ * cut read serves a run's whole file). Ported from #3617's rule, one regex there, read here by hand with every
291
+ * character counted.
292
+ * - With its END line within KEY_BODY_MAX characters, all between is cut, whatever wraps the body's lines (a code
293
+ * string literal on each, a "#" or ">" before each, an XML or HTML line break, an escape, an armored key's headers):
294
+ * the body is any character but a "-" that starts "-----", so the END line can only start at the first "-----"
295
+ * after the header, and the reading from one header stops there.
296
+ * - With no END line in reach (a key cut short, one past the bound, one the edge of a window key-refusal.ts reads
297
+ * crosses), it is cut through its body's lines: the first maybe glued to its header, at most KEY_LINES in all. A
298
+ * line break is LF, CRLF or CR, or one escaped in a quoted string ("\n", "\r\n", "\\n" in a string within a string:
299
+ * at most 8 backslashes, four strings deep). A line is blanks (a space, a tab, or an escaped tab), then base64 of at
300
+ * most KEY_BODY_MAX characters (its slashes maybe escaped, "\/"), or a legacy PEM's or armored key's header
301
+ * (Proc-Type, DEK-Info, Version, Comment) of at most KEY_HEADER_MAX after its colon, or nothing (a blank line), then
302
+ * blanks; it ends at a line break or a closing quote (either maybe escaped), an XML or HTML tag's "<", or the text's
303
+ * end. Text after the body that is no such line (prose, a field) stays.
304
+ * - A header with not even an empty line after it (prose on its line, another header glued to it) is cut alone, as
305
+ * the header shape before this one cut it.
306
+ * A block read whole or through its lines reads on from its end: a "-----BEGIN" inside it can only stand in a header
307
+ * line's text, already cut. Each part is bounded, a line break's spellings try the line after it at most three times,
308
+ * and the search for the END line stops at the next "-----": the reading stays linear. */
96
309
  const PRIVATE_KEY = {
97
310
  kind: "private key block",
98
311
  prefixes: ["-----BEGIN"],
99
312
  scan(text, at) {
100
- const from = at + 10;
101
- let end = from;
102
- while (end < text.length && end - from < 140 && PRIVATE_KEY_HEADER(text.charCodeAt(end)))
103
- end++;
104
- scanned.chars += end - from + 1;
105
- const length = end - from;
106
- if (length >= 140 || !text.startsWith("-----", end))
313
+ const header = armorEnd(text, at, 10);
314
+ if (header < 0)
107
315
  return none(at + 1);
108
- for (let i = Math.max(0, length - 75); i <= Math.min(64, length - 11); i++) {
109
- scanned.chars += 11;
110
- if (text.startsWith("PRIVATE KEY", from + i))
111
- return { start: at, end: end + 5, next: at + 1 };
112
- }
113
- return none(at + 1);
316
+ const whole = keyEndLine(text, header);
317
+ if (whole >= 0)
318
+ return { start: at, end: whole, next: whole };
319
+ const lines = keyBodyLines(text, header);
320
+ return lines >= 0 ? { start: at, end: lines, next: lines } : { start: at, end: header, next: at + 1 };
114
321
  },
115
322
  };
323
+ /** Where the private key block whose "-----BEGIN" stands at `at` ends, past its END line, or -1: no key's header there,
324
+ * or no END line in reach, as PRIVATE_KEY reads it whole. key-refusal.ts reads it so that no piece of an answer ends
325
+ * inside a block. Whether any "-----" stands within KEY_BODY_MAX of the header is read back from there first (the
326
+ * header's own closing dashes stop that read), so a header with none after it reads no further. */
327
+ export function privateKeyEnd(text, at) {
328
+ if (!text.startsWith("-----BEGIN", at))
329
+ return -1;
330
+ const header = armorEnd(text, at, 10);
331
+ if (header < 0)
332
+ return -1;
333
+ const reach = Math.min(text.length, header + KEY_BODY_MAX);
334
+ const last = text.lastIndexOf("-----", reach);
335
+ scanned.chars += reach - Math.max(last, header - 5);
336
+ return last < header ? -1 : keyEndLine(text, header);
337
+ }
116
338
  /** A JWT: three base64url parts, the first two JSON objects ("eyJ"), each 8 characters or more after it. It starts at
117
339
  * the first "eyJ" of a run of a part's characters, wherever that run starts ("token-eyJ…", "x9eyJ…"): a later "eyJ" in
118
340
  * the same run reaches the same "." with fewer characters. Whether an "eyJ" is the first is read back to the "eyJ"
@@ -261,8 +483,9 @@ const BEARER_TOKEN = {
261
483
  return { start: token, end, next: token };
262
484
  },
263
485
  };
264
- /** Each name a credential parameter goes by, as written after "?" or "&" and lowered. */
265
- const CREDENTIAL_NAMES = new Set([
486
+ /** Each name a credential parameter goes by, as written after "?" or "&" and lowered; and each key, lowered, of a held
487
+ * JSON value whose string is held as itself (learn/secrets.ts heldFields). */
488
+ export const CREDENTIAL_NAMES = new Set([
266
489
  ...["api", "access", "auth", "id", "refresh", "client"].flatMap((head) => {
267
490
  const tail = head === "api" ? "key" : head === "client" ? "secret" : "token";
268
491
  return [`${head}${tail}`, `${head}_${tail}`, `${head}-${tail}`];
@@ -291,6 +514,32 @@ const URL_CREDENTIAL = {
291
514
  return end - (name + 1) >= 16 ? { start: name + 1, end, next: end } : none(end);
292
515
  },
293
516
  };
517
+ const isUpper = (code) => code >= 65 && code <= 90;
518
+ /** An API key made in this server's console (api-keys.ts): `ar_key_` and its 32 random bytes in base64url, 43
519
+ * characters or more to the end of that run, so a key glued to more of its characters is cut whole. The server keeps
520
+ * no copy of such a key (only a keyed hash), so its shape is the one way it is found in a text, and it is found
521
+ * whatever stands before it: after a word's character too (the `n` of a line break as JSON text writes it, a name
522
+ * glued to it). What tells a key from a long name that only holds the prefix's letters (`similar_key_for_the_...`) is
523
+ * in the run itself: a capital or a digit, which 32 random bytes all but always hold and a snake_case name does not.
524
+ * A start inside a run already read ends where that run ends and holds no more of it, so it is not read again. */
525
+ const AGENTRUN_API_KEY = {
526
+ kind: "AgentRun API key",
527
+ prefixes: ["ar_key_"],
528
+ scan(text, at, prefix) {
529
+ const from = at + prefix.length;
530
+ let end = from;
531
+ let mixed = false;
532
+ for (; end < text.length; end++) {
533
+ const code = text.charCodeAt(end);
534
+ if (!PART(code))
535
+ break;
536
+ mixed ||= isDigit(code) || isUpper(code);
537
+ }
538
+ scanned.chars += end - from + 1;
539
+ const next = end - (prefix.length - 1);
540
+ return end - from >= 43 && mixed ? { start: at, end, next } : none(next);
541
+ },
542
+ };
294
543
  /** Text shaped like a secret, by kind. The same shapes the Pi learning loop's guard refuses. */
295
544
  export const SECRET_SHAPES = [
296
545
  prefixFixed("AWS access key id", ["AKIA", "ASIA"], only(UPPER + DIGIT), 16),
@@ -299,6 +548,7 @@ export const SECRET_SHAPES = [
299
548
  prefixRun("GitHub token", ["ghp_", "gho_", "ghu_", "ghs_", "ghr_"], ALNUM_CLASS, 30),
300
549
  prefixRun("GitHub token", ["github_pat_"], WORD, 20),
301
550
  prefixRun("sk- key", ["sk-"], PART, 20, (text, at) => at === 0 || !ALNUM_CLASS(text.charCodeAt(at - 1))),
551
+ AGENTRUN_API_KEY,
302
552
  prefixRun("Slack token", [..."abposre"].map((kind) => `xox${kind}-`).concat("xapp-"), only(`${ALNUM}-`), 10),
303
553
  JWT,
304
554
  WEBHOOK_SECRET,
@@ -334,8 +584,10 @@ const besideWord = (kind, words, pattern, head, digits = false) => {
334
584
  const ACCOUNT_NUMBER = /\b(?:bank\s{1,3})?(?:account|acct|a\/c)(?:\s{1,3}(?:number|no\.?|num))?\s{0,3}(?:#|:|\bis\b)?\s{0,3}(?!\d{4}-\d{2}-\d{2}\b)\d(?:[ -]?\d){5,}/;
335
585
  /** An IBAN beside its word: two letters, two check digits, then ten to thirty letters or digits, spaces between. */
336
586
  const IBAN = /\bIBAN\s{0,3}(?:#|:|\bis\b)?\s{0,3}[A-Z]{2}\d{2}(?:\s?[A-Z0-9]){10,30}/;
337
- /** A US routing (ABA) number beside its word: nine digits. */
338
- const ROUTING_NUMBER = /\b(?:routing|ABA)(?:\s{1,3}(?:number|no\.?|num))?\s{0,3}(?:#|:|\bis\b)?\s{0,3}\d{9}\b/;
587
+ /** A US routing (ABA) number beside its word: nine digits. The word ends at a word end, so an id or hash whose hex
588
+ * holds "aba" glued to nine digits (a UUID's last group, a sha cut to twelve) is none. The other words hold a letter
589
+ * no hex does, so no id spells them. */
590
+ const ROUTING_NUMBER = /\b(?:routing|ABA)\b(?:\s{1,3}(?:number|no\.?|num))?\s{0,3}(?:#|:|\bis\b)?\s{0,3}\d{9}\b/;
339
591
  /** A UK sort code beside its word: three pairs of digits. */
340
592
  const SORT_CODE = /\bsort\s{1,3}code\s{0,3}(?:#|:|\bis\b)?\s{0,3}\d{2}[- ]?\d{2}[- ]?\d{2}\b/;
341
593
  /** A bank's numbers beside their words, which learning never keeps: an account, IBAN, routing or sort-code number. Not
@@ -387,6 +639,12 @@ function lowered(text) {
387
639
  return low;
388
640
  };
389
641
  }
642
+ /** Every span `shape` matches in `text`, each on its own (not merged), in order: the scrubber's known-format rules
643
+ * (secret-rules.ts) read each match, a URL's password inside its `://user:password@`, a PEM header stretched to its
644
+ * block's end. */
645
+ export function shapeSpans(text, shape) {
646
+ return spansOf(text, lowered(text), shape);
647
+ }
390
648
  /** The kinds of `shapes` in `text`, each once, in their order. */
391
649
  export function shapeKinds(text, shapes) {
392
650
  const lower = lowered(text);
@@ -400,7 +658,7 @@ export function shapeKinds(text, shapes) {
400
658
  function places(hay, needle, out) {
401
659
  if (!needle)
402
660
  return;
403
- for (let at = hay.indexOf(needle); at >= 0; at = hay.indexOf(needle, at + 1))
661
+ for (let at = find(hay, needle, 0); at >= 0; at = find(hay, needle, at + 1))
404
662
  out.push([at, at + needle.length]);
405
663
  }
406
664
  const isSeparator = (code) => code === 45 || code === 95 || code === 58 || code === 46;
@@ -408,6 +666,8 @@ const isSeparator = (code) => code === 45 || code === 95 || code === 58 || code
408
666
  * two. Found in the text lowered with its separators gone, each character's offset in the text kept, and a place kept
409
667
  * only where no two of its digits are more than one separator apart. */
410
668
  function foldSpans(text, folds, out) {
669
+ // The text lowered, then read once for its separators.
670
+ scanned.chars += 2 * text.length;
411
671
  const lower = asciiLower(text);
412
672
  const at = new Int32Array(text.length);
413
673
  const parts = [];
@@ -430,7 +690,7 @@ function foldSpans(text, folds, out) {
430
690
  for (const needle of folds) {
431
691
  if (!needle)
432
692
  continue;
433
- for (let i = flat.indexOf(needle); i >= 0; i = flat.indexOf(needle, i + 1)) {
693
+ for (let i = find(flat, needle, 0); i >= 0; i = find(flat, needle, i + 1)) {
434
694
  const last = i + needle.length - 1;
435
695
  if (stretch[last] <= i)
436
696
  out.push([at[i], at[last] + 1]);
@@ -439,6 +699,8 @@ function foldSpans(text, folds, out) {
439
699
  }
440
700
  /** `found` in order, each run of overlapping or touching spans as one. */
441
701
  export function merged(found) {
702
+ // Each find read once: they come as one sorted run per form, hex, fold and shape, which the sort merges.
703
+ scanned.chars += found.length;
442
704
  const out = [];
443
705
  for (const [start, end] of [...found].sort((a, b) => a[0] - b[0])) {
444
706
  const last = out.at(-1);
@@ -451,6 +713,7 @@ export function merged(found) {
451
713
  }
452
714
  /** `text` with each of `spans` (merged, in order) replaced by "[redacted]". */
453
715
  export function cutAt(text, spans) {
716
+ scanned.chars += text.length;
454
717
  let out = "";
455
718
  let from = 0;
456
719
  for (const [start, end] of spans) {
@@ -461,6 +724,13 @@ export function cutAt(text, spans) {
461
724
  }
462
725
  const HEX = only("0123456789abcdefABCDEF");
463
726
  const BLANKS = /[\t-\r ]+/y;
727
+ /** A character that renders as nothing (a zero-width space, a soft hyphen, a byte order mark, a variation selector). */
728
+ const IGNORABLE = /\p{Default_Ignorable_Code_Point}+/gu;
729
+ const GONE = /[\s\p{Default_Ignorable_Code_Point}]+/gu;
730
+ /** `text` as a view reads it, and as a held value's needles are read: NFKC, with its whitespace and its characters that
731
+ * render as nothing gone, before NFKC (so one between a letter and its accent hides neither) and after. A held value
732
+ * with a zero-width space or a soft hyphen between its characters reads as itself. */
733
+ export const squeeze = (text) => text.replace(IGNORABLE, "").normalize("NFKC").replace(GONE, "");
464
734
  /** A run of percent-encoded bytes, decoded as UTF-8 (decoded). */
465
735
  const PERCENT = {
466
736
  trigger: "%",
@@ -468,6 +738,8 @@ const PERCENT = {
468
738
  let end = at;
469
739
  while (text[end] === "%" && HEX(text.charCodeAt(end + 1)) && HEX(text.charCodeAt(end + 2)))
470
740
  end += 3;
741
+ // Each byte read, and the three characters past the run that end it.
742
+ scanned.chars += end - at + 3;
471
743
  if (end === at)
472
744
  return -1;
473
745
  decoded(text, at, end, unit);
@@ -475,23 +747,112 @@ const PERCENT = {
475
747
  },
476
748
  };
477
749
  /** "+" as a space, as a form's encoding writes one. */
478
- const PLUS = { trigger: "+", decode: (_text, at, unit) => (unit(" ", at, at + 1), at + 1) };
750
+ const PLUS = { trigger: "+", decode: (_text, at, unit) => (scanned.chars++, unit(" ", at, at + 1), at + 1) };
751
+ /** What JSON's one-character escapes stand for. */
752
+ const ESCAPED = {
753
+ n: "\n", r: "\r", t: "\t", '"': '"', "\\": "\\", "/": "/", b: "\b", f: "\f",
754
+ };
755
+ function unescaped(text, at) {
756
+ // Counted as the most it reads, six characters ("\u" and four hex digits), whatever it reads.
757
+ if (at >= text.length)
758
+ return undefined;
759
+ scanned.chars += Math.min(6, text.length - at);
760
+ if (text[at] !== "\\")
761
+ return { char: text[at], end: at + 1 };
762
+ const next = text[at + 1];
763
+ if (next !== undefined && ESCAPED[next] !== undefined)
764
+ return { char: ESCAPED[next], end: at + 2 };
765
+ if (next !== "u")
766
+ return undefined;
767
+ for (let i = at + 2; i < at + 6; i++)
768
+ if (!HEX(text.charCodeAt(i)))
769
+ return undefined;
770
+ return { char: String.fromCharCode(Number.parseInt(text.slice(at + 2, at + 6), 16)), end: at + 6 };
771
+ }
772
+ /** JSON's escapes, decoded twice: a held value a JSON writer wrote, every character or some ("\n", "\"", a "\u" and
773
+ * four hex digits), and one written again as a JSON string's text (JSON in JSON: "\\n", "\\\""). Where one level's "\"
774
+ * starts an escape of the next, each character that escape decodes to comes from all of the raw text the two levels
775
+ * read; a "\" that starts none is itself. A surrogate pair is two escapes, two units, NFKC'd together as any non-ASCII
776
+ * stretch is. Line breaks written "\r\n" decode to whitespace, gone from the view. */
777
+ const JSON_ESCAPE = {
778
+ trigger: "\\",
779
+ decode(text, at, unit) {
780
+ const first = unescaped(text, at);
781
+ if (!first)
782
+ return -1;
783
+ if (first.char !== "\\") {
784
+ unit(first.char, at, first.end);
785
+ return first.end;
786
+ }
787
+ // The first level decoded a "\": what follows it, itself read through the first level, may be a second escape.
788
+ const second = unescaped(text, first.end);
789
+ if (second && ESCAPED[second.char] !== undefined) {
790
+ unit(ESCAPED[second.char], at, second.end);
791
+ return second.end;
792
+ }
793
+ if (second?.char === "u") {
794
+ let end = second.end;
795
+ let digits = "";
796
+ for (let i = 0; i < 4; i++) {
797
+ const digit = unescaped(text, end);
798
+ if (!digit || !HEX(digit.char.charCodeAt(0)))
799
+ break;
800
+ digits += digit.char;
801
+ end = digit.end;
802
+ }
803
+ if (digits.length === 4) {
804
+ unit(String.fromCharCode(Number.parseInt(digits, 16)), at, end);
805
+ return end;
806
+ }
807
+ }
808
+ unit("\\", at, first.end);
809
+ return first.end;
810
+ },
811
+ };
812
+ /** JSON's escapes, decoded once: a "\" that starts an escape and what follows it, as one character. A held value that
813
+ * holds a backslash before a character an escape starts with ("\n", "A" as written), written by a JSON writer
814
+ * ("\\n", "\u0041"), reads as itself here, where the two levels JSON_ESCAPE reads make the escaped backslash and
815
+ * what follows it one escape. */
816
+ const JSON_ESCAPE_ONCE = {
817
+ trigger: "\\",
818
+ decode(text, at, unit) {
819
+ const char = unescaped(text, at);
820
+ if (!char)
821
+ return -1;
822
+ unit(char.char, at, char.end);
823
+ return char.end;
824
+ },
825
+ };
826
+ /** What JSON_ESCAPE reads at `at`: the character the escape there decodes to, through both levels, and where it ends;
827
+ * undefined where no escape starts (the "\" is then itself). key-refusal.ts's peek counts an escape by it, so the
828
+ * read past a call's stop and the JSON view see the same characters. */
829
+ export function jsonEscapeAt(text, at) {
830
+ let char;
831
+ const end = JSON_ESCAPE.decode(text, at, (decoded) => {
832
+ char = decoded;
833
+ });
834
+ return end < 0 || char === undefined ? undefined : { char, end };
835
+ }
479
836
  const viewSpec = (when, decoders) => ({
480
837
  when,
481
838
  decoders,
482
839
  special: new RegExp(`[\\s${decoders.map((decoder) => `\\${decoder.trigger}`).join("")}\\u0080-\\uffff]`, "g"),
483
840
  });
484
841
  /** The views of a text read for each held value, in order: squeezed; percent-decoded; percent-decoded with "+" as a
485
- * space. Each is lowered for hexes and flattened for folded identifiers in holds. */
842
+ * space; JSON's escapes decoded twice; and decoded once, where the text holds an escape that decodes to a backslash
843
+ * (elsewhere the two levels read as one does). Each is lowered for hexes and flattened for folded identifiers in
844
+ * holds. */
486
845
  const VIEWS = [
487
846
  viewSpec(() => true, []),
488
847
  viewSpec((text) => /%[0-9a-fA-F]{2}/.test(text), [PERCENT]),
489
848
  viewSpec((text) => text.includes("+"), [PERCENT, PLUS]),
849
+ viewSpec((text) => /\\(?:u[0-9a-fA-F]{4}|["\\/bfnrt])/.test(text), [JSON_ESCAPE]),
850
+ viewSpec((text) => /\\(?:\\|u005[cC])/.test(text), [JSON_ESCAPE_ONCE]),
490
851
  ];
491
- /** `text` as the whole-text check reads it: decoded by `spec`'s decoders, NFKC, and its whitespace gone, as
492
- * learn/secrets.ts has always squeezed it. NFKC is applied to each stretch of non-ASCII characters with the ASCII
493
- * character before it, which gives what it gives over the whole text (an ASCII character is a starter nothing composes
494
- * onto), and each character it makes comes from that whole stretch. With `cuts` (merged spans of `text`), the view's
852
+ /** `text` as the whole-text check reads it: decoded by `spec`'s decoders, then squeezed (squeeze: NFKC, its whitespace
853
+ * and its characters that render as nothing gone), as a held value's needles are. The squeeze is applied to each
854
+ * stretch of non-ASCII characters with the ASCII character before it, which gives what it gives over the whole text (an
855
+ * ASCII character is a starter nothing composes onto), and each character it makes comes from that whole stretch. With `cuts` (merged spans of `text`), the view's
495
856
  * covered stretches: where every character comes from inside one cut. */
496
857
  function viewOf(text, spec, cuts) {
497
858
  const out = [];
@@ -511,9 +872,13 @@ function viewOf(text, spec, cuts) {
511
872
  return;
512
873
  out.push(text.slice(from, to));
513
874
  if (cuts) {
514
- while (cut < cuts.length && cuts[cut][1] <= from)
875
+ // The cover's cursor only goes forward: each cut is passed once in the whole view.
876
+ while (cut < cuts.length && cuts[cut][1] <= from) {
515
877
  cut++;
878
+ scanned.chars++;
879
+ }
516
880
  for (let k = cut; k < cuts.length && cuts[k][0] < to; k++) {
881
+ scanned.chars++;
517
882
  const start = Math.max(from, cuts[k][0]);
518
883
  const end = Math.min(to, cuts[k][1]);
519
884
  if (start < end)
@@ -528,8 +893,10 @@ function viewOf(text, spec, cuts) {
528
893
  return;
529
894
  out.push(chars);
530
895
  if (cuts) {
531
- while (cut < cuts.length && cuts[cut][1] <= from)
896
+ while (cut < cuts.length && cuts[cut][1] <= from) {
532
897
  cut++;
898
+ scanned.chars++;
899
+ }
533
900
  if (cut < cuts.length && cuts[cut][0] <= from && to <= cuts[cut][1])
534
901
  cover(length, length + chars.length);
535
902
  }
@@ -542,16 +909,18 @@ function viewOf(text, spec, cuts) {
542
909
  emit(held.char, held.from, held.to);
543
910
  held = undefined;
544
911
  };
545
- const nfkc = (chars) => chars.normalize("NFKC").replace(/\s+/gu, "");
546
912
  const flushGroup = () => {
547
913
  if (!group)
548
914
  return;
549
- const made = nfkc(group.chars);
915
+ // The stretch squeezed twice: whole, and without its lead.
916
+ scanned.chars += 2 * group.chars.length;
917
+ const made = squeeze(group.chars);
550
918
  const { lead } = group;
551
- const rest = lead ? nfkc(group.chars.slice(lead.char.length)) : "";
919
+ const rest = lead ? squeeze(group.chars.slice(lead.char.length)) : "";
552
920
  // Where nothing composed onto the ASCII character, it comes from itself alone: a value cut in place right before
553
- // a non-ASCII character is still covered to its last character.
554
- if (lead && made === nfkc(lead.char) + rest) {
921
+ // a non-ASCII character is still covered to its last character. A stretch that squeezes to nothing (characters that
922
+ // render as nothing) adds nothing to the view, as whitespace adds nothing.
923
+ if (lead && made === squeeze(lead.char) + rest) {
555
924
  held = lead;
556
925
  flushHeld();
557
926
  emit(rest, lead.to, group.to);
@@ -582,6 +951,8 @@ function viewOf(text, spec, cuts) {
582
951
  special.lastIndex = at;
583
952
  const hit = special.exec(text);
584
953
  const stop = hit ? hit.index : text.length;
954
+ // The characters copied as they are, and the special one that stopped the search.
955
+ scanned.chars += (hit ? stop + 1 : stop) - at;
585
956
  if (stop > at) {
586
957
  flushGroup();
587
958
  flushHeld();
@@ -602,6 +973,7 @@ function viewOf(text, spec, cuts) {
602
973
  // character after it could compose onto.
603
974
  BLANKS.lastIndex = stop;
604
975
  BLANKS.test(text);
976
+ scanned.chars += BLANKS.lastIndex - stop;
605
977
  flushGroup();
606
978
  flushHeld();
607
979
  held = { char: text[BLANKS.lastIndex - 1], from: BLANKS.lastIndex - 1, to: BLANKS.lastIndex };
@@ -629,6 +1001,8 @@ function decoded(text, from, to, unit) {
629
1001
  unit(String.fromCharCode(Number.parseInt(text.slice(at + 1, at + 3), 16)), at, at + 3);
630
1002
  return;
631
1003
  }
1004
+ // The run read three times: its hex parsed, decoded whole, and decoded one sequence at a time.
1005
+ scanned.chars += 3 * (to - from);
632
1006
  const bytes = Buffer.from(text.slice(from, to).replaceAll("%", ""), "hex");
633
1007
  const whole = bytes.toString("utf8");
634
1008
  const pieces = [];
@@ -650,6 +1024,7 @@ function decoded(text, from, to, unit) {
650
1024
  }
651
1025
  /** A view with its separators ("-", "_", ":", ".") gone, its covered stretches moved to match. */
652
1026
  function flatOf(view) {
1027
+ scanned.chars += view.text.length + view.covered.length;
653
1028
  const parts = [];
654
1029
  const kept = new Int32Array(view.text.length + 1);
655
1030
  let from = 0;
@@ -680,11 +1055,13 @@ function flatOf(view) {
680
1055
  }
681
1056
  return { text: parts.join(""), covered };
682
1057
  }
683
- /** Whether [start, end) of a view lies in one of its covered stretches. */
1058
+ /** Whether [start, end) of a view lies in one of its covered stretches: a binary search, each stretch it visits
1059
+ * counted. */
684
1060
  function inside(covered, start, end) {
685
1061
  let lo = 0;
686
1062
  let hi = covered.length - 1;
687
1063
  while (lo <= hi) {
1064
+ scanned.chars++;
688
1065
  const mid = (lo + hi) >> 1;
689
1066
  if (covered[mid][0] <= start)
690
1067
  lo = mid + 1;
@@ -699,11 +1076,11 @@ function holdsIn(view, covered, needles) {
699
1076
  if (!needle)
700
1077
  continue;
701
1078
  if (!covered) {
702
- if (view.includes(needle))
1079
+ if (find(view, needle, 0) >= 0)
703
1080
  return true;
704
1081
  continue;
705
1082
  }
706
- for (let at = view.indexOf(needle); at >= 0; at = view.indexOf(needle, at + 1))
1083
+ for (let at = find(view, needle, 0); at >= 0; at = find(view, needle, at + 1))
707
1084
  if (!inside(covered, at, at + needle.length))
708
1085
  return true;
709
1086
  }
@@ -717,6 +1094,9 @@ export function holds(text, needles, { fold = true, cuts } = {}) {
717
1094
  if (!needles.plain.length && !needles.hexes.length && !folds.length)
718
1095
  return false;
719
1096
  for (const spec of VIEWS) {
1097
+ // Whether a decoding view is read: its test reads the text to its first trigger at most.
1098
+ if (spec.decoders.length)
1099
+ scanned.chars += text.length;
720
1100
  if (!spec.when(text))
721
1101
  continue;
722
1102
  const view = viewOf(text, spec, cuts);
@@ -725,6 +1105,7 @@ export function holds(text, needles, { fold = true, cuts } = {}) {
725
1105
  return true;
726
1106
  if (!needles.hexes.length && !folds.length)
727
1107
  continue;
1108
+ scanned.chars += view.text.length;
728
1109
  const lower = { text: asciiLower(view.text), covered: view.covered };
729
1110
  if (holdsIn(lower.text, covered, needles.hexes))
730
1111
  return true;