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

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 (234) hide show
  1. package/CHANGELOG.md +142 -2
  2. package/README.md +310 -98
  3. package/dist/adapters.d.ts +11 -0
  4. package/dist/adapters.js +49 -0
  5. package/dist/agent-package.d.ts +3 -3
  6. package/dist/agent-package.js +6 -13
  7. package/dist/agents-store.js +6 -5
  8. package/dist/answer-json.d.ts +6 -2
  9. package/dist/answer-json.js +22 -4
  10. package/dist/api-only.d.ts +16 -4
  11. package/dist/api-only.js +23 -7
  12. package/dist/calendar/tools.js +3 -2
  13. package/dist/chatgpt-plan.d.ts +16 -1
  14. package/dist/chatgpt-plan.js +116 -12
  15. package/dist/claude-login.d.ts +34 -5
  16. package/dist/claude-login.js +31 -7
  17. package/dist/claude-sign-in.d.ts +35 -0
  18. package/dist/claude-sign-in.js +90 -0
  19. package/dist/cli.js +5 -3
  20. package/dist/composio-made.d.ts +26 -1
  21. package/dist/composio-made.js +39 -2
  22. package/dist/composio-preset.d.ts +3 -2
  23. package/dist/composio-preset.js +46 -14
  24. package/dist/computer-held-routes.js +2 -3
  25. package/dist/computer-setting.d.ts +4 -2
  26. package/dist/computer-setting.js +0 -2
  27. package/dist/computers-running.d.ts +8 -0
  28. package/dist/computers-running.js +27 -0
  29. package/dist/console/assets/backend-Boh7tC4i.js +166 -0
  30. package/dist/console/assets/index-CQ3cJRzI.js +746 -0
  31. package/dist/console/assets/index-DKkaS3a0.css +1 -0
  32. package/dist/console/index.html +2 -2
  33. package/dist/docker-host-addresses.d.ts +23 -3
  34. package/dist/docker-host-addresses.js +90 -18
  35. package/dist/exa-keyless.d.ts +2 -3
  36. package/dist/exa-keyless.js +43 -127
  37. package/dist/firecrawl-keyless.d.ts +41 -0
  38. package/dist/firecrawl-keyless.js +152 -0
  39. package/dist/handed-secret.d.ts +3 -1
  40. package/dist/handed-secret.js +4 -2
  41. package/dist/harness-keys.d.ts +98 -0
  42. package/dist/harness-keys.js +247 -0
  43. package/dist/harness-recipe-entry.d.ts +14 -9
  44. package/dist/harness-recipe-entry.js +25 -14
  45. package/dist/harness-table.d.ts +16 -1
  46. package/dist/harness-table.js +19 -7
  47. package/dist/harness.d.ts +4 -0
  48. package/dist/harness.js +12 -1
  49. package/dist/host-check.d.ts +6 -0
  50. package/dist/host-check.js +10 -0
  51. package/dist/import-summary.d.ts +1 -1
  52. package/dist/import-summary.js +1 -1
  53. package/dist/import.d.ts +5 -5
  54. package/dist/import.js +54 -67
  55. package/dist/key-refusal.d.ts +21 -5
  56. package/dist/key-refusal.js +231 -37
  57. package/dist/keyless-mcp.d.ts +36 -0
  58. package/dist/keyless-mcp.js +127 -0
  59. package/dist/learn/audit.d.ts +3 -3
  60. package/dist/learn/audit.js +3 -3
  61. package/dist/learn/commit.d.ts +8 -7
  62. package/dist/learn/commit.js +60 -20
  63. package/dist/learn/compile.d.ts +4 -3
  64. package/dist/learn/compile.js +7 -5
  65. package/dist/learn/folder.d.ts +16 -3
  66. package/dist/learn/folder.js +46 -13
  67. package/dist/learn/promote.d.ts +37 -13
  68. package/dist/learn/promote.js +112 -40
  69. package/dist/learn/secrets.d.ts +20 -10
  70. package/dist/learn/secrets.js +44 -176
  71. package/dist/learning-routes.d.ts +22 -17
  72. package/dist/learning-routes.js +40 -39
  73. package/dist/learning.js +2 -2
  74. package/dist/llms.txt +1 -1
  75. package/dist/manifest.d.ts +10 -0
  76. package/dist/manifest.js +30 -0
  77. package/dist/mcp-pin-first.d.ts +4 -2
  78. package/dist/mcp-pin-first.js +5 -2
  79. package/dist/mcp-secret-writes.d.ts +16 -7
  80. package/dist/mcp-secret-writes.js +35 -27
  81. package/dist/mcp-servers-store.d.ts +30 -6
  82. package/dist/mcp-servers-store.js +112 -28
  83. package/dist/mcp-sources.d.ts +10 -2
  84. package/dist/mcp-sources.js +9 -2
  85. package/dist/mcp-store.d.ts +13 -7
  86. package/dist/mcp-store.js +12 -9
  87. package/dist/model-form.d.ts +3 -2
  88. package/dist/model-form.js +11 -7
  89. package/dist/model.d.ts +20 -4
  90. package/dist/model.js +68 -9
  91. package/dist/native/history.d.ts +1 -1
  92. package/dist/native/history.js +2 -2
  93. package/dist/ollama-provider.d.ts +92 -0
  94. package/dist/ollama-provider.js +495 -0
  95. package/dist/openapi/agentrun-v2.openapi.json +2 -2
  96. package/dist/openapi.d.ts +3 -3
  97. package/dist/openapi.js +197 -55
  98. package/dist/page.d.ts +11 -3
  99. package/dist/page.js +22 -7
  100. package/dist/place-offers.d.ts +25 -1
  101. package/dist/place-offers.js +62 -8
  102. package/dist/print.js +1 -1
  103. package/dist/purged-workflow.d.ts +35 -7
  104. package/dist/purged-workflow.js +73 -26
  105. package/dist/redact/match.d.ts +74 -0
  106. package/dist/redact/match.js +771 -0
  107. package/dist/run-computer.d.ts +2 -0
  108. package/dist/run-computer.js +2 -0
  109. package/dist/run-files.d.ts +4 -0
  110. package/dist/run-files.js +4 -0
  111. package/dist/run-keys.d.ts +2 -4
  112. package/dist/run-keys.js +6 -7
  113. package/dist/run-launch.d.ts +4 -0
  114. package/dist/run-launch.js +4 -0
  115. package/dist/run-reads-over-store.d.ts +3 -2
  116. package/dist/run-reads-over-store.js +66 -2
  117. package/dist/run-secrets.js +5 -2
  118. package/dist/run-timeline-served.d.ts +1 -1
  119. package/dist/run-timeline-served.js +4 -4
  120. package/dist/runner/adapter-loop.d.ts +9 -3
  121. package/dist/runner/adapter-loop.js +34 -15
  122. package/dist/runner/adapter-run-spec.d.ts +3 -0
  123. package/dist/runner/adapter-run-spec.js +9 -4
  124. package/dist/runner/audit.js +8 -4
  125. package/dist/runner/beside.d.ts +2 -1
  126. package/dist/runner/beside.js +13 -5
  127. package/dist/runner/claim-secrets.d.ts +5 -8
  128. package/dist/runner/claim-secrets.js +6 -0
  129. package/dist/runner/cli-run-spec.d.ts +1 -1
  130. package/dist/runner/cli-run-spec.js +41 -7
  131. package/dist/runner/harness-loops.d.ts +22 -9
  132. package/dist/runner/harness-loops.js +21 -11
  133. package/dist/runner/held.d.ts +3 -0
  134. package/dist/runner/learning-end.d.ts +1 -1
  135. package/dist/runner/learning-end.js +8 -3
  136. package/dist/runner/loop-contract.d.ts +9 -4
  137. package/dist/runner/main.js +12 -6
  138. package/dist/runner/mcp-claim.d.ts +3 -0
  139. package/dist/runner/mcp-claim.js +26 -2
  140. package/dist/runner/mcp-run.d.ts +3 -0
  141. package/dist/runner/mcp-run.js +2 -1
  142. package/dist/runner/metrics.d.ts +10 -10
  143. package/dist/runner/metrics.js +19 -38
  144. package/dist/runner/model-route.d.ts +3 -0
  145. package/dist/runner/model-route.js +6 -2
  146. package/dist/runner/presence.d.ts +4 -3
  147. package/dist/runner/presence.js +4 -2
  148. package/dist/runner/promotion.d.ts +15 -9
  149. package/dist/runner/promotion.js +25 -15
  150. package/dist/runner/run-child-host.d.ts +2 -0
  151. package/dist/runner/run-child-host.js +19 -9
  152. package/dist/runner/run-child.js +3 -1
  153. package/dist/runner/runner.d.ts +4 -0
  154. package/dist/runner/runner.js +33 -3
  155. package/dist/runner/sdk-loop.js +2 -0
  156. package/dist/runner/workflow-loop.d.ts +2 -2
  157. package/dist/runner/workflow-loop.js +1 -2
  158. package/dist/runner/workflow-spec.d.ts +1 -1
  159. package/dist/runner/workflow-spec.js +6 -6
  160. package/dist/sdk/check.d.ts +4 -1
  161. package/dist/sdk/check.js +2 -1
  162. package/dist/sdk/runs.d.ts +2 -2
  163. package/dist/sdk/runs.js +8 -6
  164. package/dist/secret-cut.js +2 -1
  165. package/dist/secret-writes.d.ts +3 -2
  166. package/dist/secret-writes.js +1 -1
  167. package/dist/server.d.ts +49 -10
  168. package/dist/server.js +324 -118
  169. package/dist/standard-webhooks.d.ts +11 -0
  170. package/dist/standard-webhooks.js +20 -2
  171. package/dist/store/dialect.d.ts +3 -0
  172. package/dist/store/dialect.js +1 -0
  173. package/dist/store/index.d.ts +2 -2
  174. package/dist/store/index.js +1 -1
  175. package/dist/store/postgres/dialect.js +3 -0
  176. package/dist/store/schema.js +5 -2
  177. package/dist/store/sqlite/store.js +133 -47
  178. package/dist/store/store.d.ts +64 -15
  179. package/dist/store/store.js +7 -0
  180. package/dist/store-reads/journal.d.ts +7 -2
  181. package/dist/store-reads/journal.js +9 -6
  182. package/dist/store-reads/learning.d.ts +12 -9
  183. package/dist/store-reads/learning.js +18 -24
  184. package/dist/store-view.d.ts +0 -5
  185. package/dist/store-view.js +10 -9
  186. package/dist/template-eval.d.ts +51 -6
  187. package/dist/template-eval.js +177 -53
  188. package/dist/template-import.js +4 -2
  189. package/dist/templates.d.ts +5 -3
  190. package/dist/templates.js +19 -40
  191. package/dist/timeline/index.d.ts +1 -1
  192. package/dist/timeline/index.js +1 -1
  193. package/dist/timeline/labels.d.ts +2 -2
  194. package/dist/timeline/labels.js +2 -2
  195. package/dist/timeline/timeline.d.ts +14 -9
  196. package/dist/timeline/timeline.js +15 -7
  197. package/dist/tool-gate.js +3 -1
  198. package/dist/tool-plane.d.ts +17 -9
  199. package/dist/tool-plane.js +38 -17
  200. package/dist/tools.js +2 -2
  201. package/dist/trigger-launch-hook.d.ts +16 -8
  202. package/dist/trigger-launch-hook.js +32 -23
  203. package/dist/trigger-targets.d.ts +18 -8
  204. package/dist/trigger-targets.js +42 -19
  205. package/dist/triggers.d.ts +85 -23
  206. package/dist/triggers.js +303 -96
  207. package/dist/validate.js +1 -1
  208. package/dist/web-fake.js +3 -3
  209. package/dist/web-fetch-direct.d.ts +4 -0
  210. package/dist/web-fetch-direct.js +8 -1
  211. package/dist/web-fetch.d.ts +12 -3
  212. package/dist/web-fetch.js +47 -14
  213. package/dist/web-search.d.ts +33 -4
  214. package/dist/web-search.js +100 -41
  215. package/dist/webhooks.js +4 -5
  216. package/dist/workspace/computer-deps.d.ts +2 -1
  217. package/dist/workspace/computer-deps.js +5 -4
  218. package/dist/workspace/computer-for-claim.d.ts +8 -2
  219. package/dist/workspace/computer-for-claim.js +28 -9
  220. package/dist/workspace/computer-janitor.d.ts +15 -7
  221. package/dist/workspace/computer-janitor.js +33 -8
  222. package/dist/workspace/computer-kept.d.ts +52 -4
  223. package/dist/workspace/computer-kept.js +118 -6
  224. package/dist/workspace/run-end-computers.d.ts +5 -1
  225. package/dist/workspace/run-end-computers.js +27 -1
  226. package/dist/wrapped.d.ts +57 -0
  227. package/dist/wrapped.js +51 -0
  228. package/launcher/agentrun-serve +5 -1
  229. package/package.json +17 -13
  230. package/templates/README.md +10 -9
  231. package/dist/console/assets/index-B_85An2t.js +0 -745
  232. package/dist/console/assets/index-n1YsbnYh.css +0 -1
  233. package/dist/import-sources.d.ts +0 -116
  234. package/dist/import-sources.js +0 -693
@@ -14,6 +14,13 @@ import { isObject } from "../validate.js";
14
14
  /** How many grades, and how many promotion records, an agent keeps (newest kept). */
15
15
  export const GRADES_KEPT = 500;
16
16
  export const PROMOTIONS_KEPT = 200;
17
+ /** How many champions of a workflow the promotions trim keeps back from the one the purge gate's answer leaves, that
18
+ * one included (trimPromotions): a purge that lands inside one write's ask-to-write window and refuses fewer than this
19
+ * many champions in a row still leaves one before them that serves. */
20
+ export const CHAMPIONS_KEPT = 5;
21
+ /** How many of the newest records the promotions trim keeps ahead of the champions before (CHAMPIONS_KEPT): those only
22
+ * take the room the newest leave, so they never crowd out a decision a check just landed. */
23
+ const RECENT_KEPT = PROMOTIONS_KEPT / 2;
17
24
  /** The most characters a grade's words (`label`) may hold, and the most of them and of an answer a compile ask shows. */
18
25
  export const LABEL_MAX = 4_000;
19
26
  export const GRADE_SOURCES = ["feedback", "eval"];
@@ -176,12 +183,18 @@ export const promotionOf = (records, workflow, digest) => records.findLast((one)
176
183
  /** The champion of `workflow`: the newest record of the digest promoted last, while that digest's newest record still
177
184
  * says promote. A newer revision replaying or held never stops it; it stops when a newer revision's check promotes that
178
185
  * one (then the champion), or when its own newest record is a hold (a held revision its owner replays, askReplay, that
179
- * promotes wins again). The money rule stops it on its own (runner/workflow-first.ts). Undefined: none. */
180
- export function championOf(records, workflow) {
181
- const last = lastPromotedOf(records, workflow);
182
- const now = last === undefined ? undefined : promotionOf(records, workflow, last);
186
+ * promotes wins again). The money rule stops it on its own (runner/workflow-first.ts). A record `refused` names is
187
+ * skipped, as if absent: one whose commit holds its workflow as a purged run wrote it (purged-workflow.ts refusedOf), so
188
+ * after a purge of the champion's run the newest promotion the purge gate lets through serves again. Undefined: none. */
189
+ export function championOf(records, workflow, refused = NONE_REFUSED) {
190
+ const kept = records.filter((one) => one.workflow === workflow && !refused(one));
191
+ const last = lastPromotedOf(kept, workflow);
192
+ const now = last === undefined ? undefined : promotionOf(kept, workflow, last);
183
193
  return now?.decided === "promote" ? now : undefined;
184
194
  }
195
+ const NONE_REFUSED = () => false;
196
+ /** The gate's answer when it could not be asked at all: refuses none, said unread. */
197
+ const UNREAD = Object.assign(() => false, { unread: true });
185
198
  /** The newest promotion of `workflow` at `digest`: the promote record that began its newest run of promote records (its
186
199
  * first, or the first after a record of that digest that was not promote: an owner's replay that promoted a held
187
200
  * revision), or undefined when none was ever promoted. Its `at` names that promotion: an audit row keeps the one serving
@@ -218,19 +231,19 @@ export function replayingOf(records, workflow, digest) {
218
231
  }
219
232
  /** What each learned workflow with a record serves, by name: `digest`, its champion's (championOf; null: none serves,
220
233
  * the agent takes its runs), and `replaying`, the revision other than it whose check is at work (null: none). What the
221
- * promotions list says beside the records (GET /agents/{id}/learning/promotions). */
222
- export function servingOf(records) {
234
+ * promotions list says beside the records (GET /agents/{id}/learning/promotions), its champion skipping `refused`. */
235
+ export function servingOf(records, refused) {
223
236
  const names = [...new Set(records.map((one) => one.workflow))].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
224
237
  return names.map((workflow) => {
225
- const digest = championOf(records, workflow)?.digest ?? null;
238
+ const digest = championOf(records, workflow, refused)?.digest ?? null;
226
239
  return { workflow, digest, replaying: replayingOf(records, workflow, digest)[0]?.digest ?? null };
227
240
  });
228
241
  }
229
- export function servingStateOf(records, workflow, costStop) {
242
+ export function servingStateOf(records, workflow, costStop, refused) {
230
243
  const newest = records.findLast((one) => one.workflow === workflow);
231
244
  if (newest === undefined || newest.decided === "replaying")
232
245
  return "checking";
233
- const champion = championOf(records, workflow);
246
+ const champion = championOf(records, workflow, refused);
234
247
  if (champion)
235
248
  return costStop(workflow, champion.digest) ? "stopped" : "serving";
236
249
  return newest.decided === "hold" && (newest.check?.stopped || newest.audit?.stopped) ? "stopped" : "held";
@@ -239,9 +252,9 @@ export function servingStateOf(records, workflow, costStop) {
239
252
  * text in them cut by `shown` (learning.ts secretsShown, as the other learning reads cut theirs), at any depth and under
240
253
  * any field, so a field added later is cut too: a workflow's name whole (`shown.name`), any other text of its part
241
254
  * (`shown.text`: a reason, a digest, a run id). Numbers and the rest as they are. A replaying record's `launch` (a run
242
- * launch: its notes, model and plane) never leaves: aggregates only. */
243
- export function promotionsShown(records, shown) {
244
- return { promotions: records.map(({ launch: _, ...one }) => shownDeep(one, shown)), serving: servingOf(records).map((one) => shownDeep(one, shown)) };
255
+ * launch: its notes, model and plane) never leaves: aggregates only. Its champions skip `refused` (championOf). */
256
+ export function promotionsShown(records, shown, refused) {
257
+ return { promotions: records.map(({ launch: _, ...one }) => shownDeep(one, shown)), serving: servingOf(records, refused).map((one) => shownDeep(one, shown)) };
245
258
  }
246
259
  /** `value` cut as the promotions read cuts its records (promotionsShown), at any depth and under any field: a workflow's
247
260
  * name whole (`shown.name`), any other text of its part (`shown.text`). Numbers and the rest as they are. */
@@ -258,46 +271,105 @@ const blankedRuns = (record) => [...(record?.check?.disagreements ?? []), ...(re
258
271
  * and of every record kept (`items`: a check across revisions, one at a time, in the same guarded write): so of two
259
272
  * claims deciding at once only the first decision is kept, and only its claim acts on it (Recorded.won). What the one
260
273
  * there now has blanked is blanked in `record` too (forgetRunIn): a record built on a copy read before a run's purge
261
- * (a connected agent's next turn, release or decision carries its turns on) never writes that run's text back. */
262
- export async function recordPromotion(settings, agentId, record, still) {
263
- let out = { record, won: true };
264
- await updateKept(settings, promotionsKey(agentId), (saved) => {
265
- const items = writtenOf(saved);
266
- const now = promotionOf(items, record.workflow, record.digest);
267
- if (!still(now, items)) {
268
- out = { record: now, won: false };
269
- return items;
270
- }
271
- const kept = blankedRuns(now).reduce(forgetRunIn, record);
272
- out = { record: kept, won: true };
273
- return trimPromotions([...items, kept]);
274
- });
275
- return out;
274
+ * (a connected agent's next turn, release or decision carries its turns on) never writes that run's text back. The trim
275
+ * keeps the champion past what the purge gate refuses too (`refused`, purged-workflow.ts refusedIn: asked before the
276
+ * guarded write when it would trim, over the records that write reads and `record` after them, so a promotion it lands
277
+ * for a revision the gate refuses is skipped as its readers skip it; a write that would trim records other than those
278
+ * it asked over, another writer's landing in between, reads and asks again). Fails closed: a gate that could not read
279
+ * a commit (Refused.unread) or could not be asked leaves every record, the write trims nothing. */
280
+ export async function recordPromotion(settings, agentId, record, still, refused) {
281
+ for (let tries = 0; tries < 20; tries++) {
282
+ let out;
283
+ const before = refused ? writtenOf(itemsOf(await settings.get(promotionsKey(agentId)))) : [];
284
+ const skip = refused && before.length >= PROMOTIONS_KEPT ? await refused([...before, record]).catch(() => UNREAD) : undefined;
285
+ const asked = JSON.stringify(before);
286
+ await updateKept(settings, promotionsKey(agentId), (saved) => {
287
+ const items = writtenOf(saved);
288
+ const now = promotionOf(items, record.workflow, record.digest);
289
+ if (!still(now, items)) {
290
+ out = { record: now, won: false };
291
+ return items;
292
+ }
293
+ const kept = blankedRuns(now).reduce(forgetRunIn, record);
294
+ if (refused && items.length >= PROMOTIONS_KEPT && JSON.stringify(items) !== asked) {
295
+ out = undefined;
296
+ return items;
297
+ }
298
+ out = { record: kept, won: true };
299
+ return skip?.unread ? [...items, kept] : trimPromotions([...items, kept], skip);
300
+ });
301
+ if (out)
302
+ return out;
303
+ }
304
+ throw new Error(`${promotionsKey(agentId)} kept changing under this write; try again`);
276
305
  }
277
306
  /** The newest records, at most PROMOTIONS_KEPT, in order, with each workflow's champion (championOf) the same after as
278
307
  * before, whatever was dropped: championOf reads the digest promoted last and its newest record, so the trim keeps both
279
- * (a held last revision is never trimmed into handing the champion back), and a replaying revision's newest record (what
280
- * its settle, its runs' claims and the one-check-at-a-time rule read: dropped, a newer revision would start a second
281
- * check while its runs run, and its verdict would never land). The rest is the newest records. */
282
- export function trimPromotions(items) {
308
+ * (a held last revision is never trimmed into handing the champion back), read over every record and again past those
309
+ * `refused` names (the purge gate's: a champion its readers skip, so the earlier one they serve is kept), and a
310
+ * replaying revision's newest record (what its settle, its runs' claims and the one-check-at-a-time rule read: dropped,
311
+ * a newer revision would start a second check while its runs run, and its verdict would never land). Then the newest
312
+ * RECENT_KEPT records (the newest one, the record a write just landed, always: its write said it won, so its decision
313
+ * is there for the next claim). Then, in the room those leave, the champions before the one `refused` leaves, nearest
314
+ * first, CHAMPIONS_KEPT in all from that one (a purge landing after the gate answered refuses every champion its run
315
+ * wrote, two or more in a row: the newest one before them still serves), each one's promote and newest record whole
316
+ * or neither. The rest is the newest records. */
317
+ export function trimPromotions(items, refused = NONE_REFUSED) {
283
318
  if (items.length <= PROMOTIONS_KEPT)
284
319
  return [...items];
285
320
  const keep = new Set();
321
+ // The champions before, by how far back: [0] is each workflow's nearest one, so the room goes to those first. Each is
322
+ // its promote and its newest record together.
323
+ const before = [];
286
324
  for (const workflow of new Set(items.map((one) => one.workflow))) {
287
325
  for (const one of replayingOf(items, workflow, null))
288
326
  keep.add(items.lastIndexOf(one));
289
- const last = lastPromotedOf(items, workflow);
290
- if (last === undefined)
291
- continue;
292
- keep.add(items.findLastIndex((one) => one.workflow === workflow && one.digest === last && one.decided === "promote"));
293
- keep.add(items.findLastIndex((one) => one.workflow === workflow && one.digest === last));
327
+ const passes = [NONE_REFUSED, refused];
328
+ for (let pass = 0; pass < passes.length; pass++) {
329
+ const skip = passes[pass];
330
+ const own = (one) => one.workflow === workflow && !skip(one);
331
+ const last = items.findLast((one) => own(one) && one.decided === "promote")?.digest;
332
+ if (last === undefined)
333
+ continue;
334
+ const found = [items.findLastIndex((one) => own(one) && one.digest === last && one.decided === "promote"), items.findLastIndex((one) => own(one) && one.digest === last)];
335
+ if (pass < 2)
336
+ for (const i of found)
337
+ keep.add(i);
338
+ else
339
+ (before[pass - 2] ??= []).push({ workflow, found });
340
+ // The champions before the one `refused` leaves: a purge that lands between the write's ask of the gate and its
341
+ // write refuses each one its run wrote, and the guard (the records read) never sees it, so its readers serve the
342
+ // newest one before them. Past CHAMPIONS_KEPT, or past the room, it fails closed: none serves, never a purged
343
+ // run's text.
344
+ if (pass >= 1 && pass < CHAMPIONS_KEPT)
345
+ passes.push((one) => skip(one) || (one.workflow === workflow && one.digest === last));
346
+ }
294
347
  }
295
- for (let i = items.length - 1, room = PROMOTIONS_KEPT - keep.size; i >= 0 && room > 0; i--) {
296
- if (keep.has(i))
348
+ const newest = (count) => {
349
+ for (let i = items.length - 1; i >= 0 && count > 0; i--) {
350
+ if (keep.has(i))
351
+ continue;
352
+ keep.add(i);
353
+ count -= 1;
354
+ }
355
+ };
356
+ newest(Math.max(keep.has(items.length - 1) ? 0 : 1, Math.min(RECENT_KEPT, PROMOTIONS_KEPT - keep.size)));
357
+ // A champion before goes in whole or not at all: its promote kept without the hold after it would read as promoted, and
358
+ // a revision held for its answers would serve again. One that does not fit cuts its workflow's chain there, so none
359
+ // further back is kept past it (that one would serve in place of the nearer one). The newest records that fill the
360
+ // rest never split one either: a promote they keep, they kept every record after it first.
361
+ const cut = new Set();
362
+ for (const { workflow, found } of before.flat()) {
363
+ if (cut.has(workflow))
297
364
  continue;
298
- keep.add(i);
299
- room -= 1;
365
+ const missing = new Set(found.filter((i) => !keep.has(i)));
366
+ if (keep.size + missing.size > PROMOTIONS_KEPT)
367
+ cut.add(workflow);
368
+ else
369
+ for (const i of missing)
370
+ keep.add(i);
300
371
  }
372
+ newest(PROMOTIONS_KEPT - keep.size);
301
373
  return items.filter((_, i) => keep.has(i));
302
374
  }
303
375
  /** The text inside an answer that is one unlabelled fence and nothing else, or undefined. */
@@ -1,8 +1,9 @@
1
+ import { type Span } from "../redact/match.js";
1
2
  /** Whether `text` ends in a `Bearer` scheme and the blanks after it: the one place a shape is read across whitespace
2
3
  * (the scheme, then its token), so a reader that cuts a text at whitespace must not cut there (key-refusal.ts
3
4
  * redactedAnswer). */
4
5
  export declare const endsInScheme: (text: string) => boolean;
5
- /** The kinds of secret-shaped text in `text`. */
6
+ /** The kinds of secret-shaped text in `text` (redact/match.ts SECRET_SHAPES). */
6
7
  export declare const secretShapes: (text: string) => string[];
7
8
  /** The kinds of text in `text` learning never keeps: every secret shape, and a bank's numbers beside their words. */
8
9
  export declare const learnedSecretShapes: (text: string) => string[];
@@ -14,18 +15,28 @@ export declare const PRINT_MAX = 4096;
14
15
  * any case, base64, a hex identifier in it folded) and each secret-shaped run replaced by "[redacted]": what the
15
16
  * checker prints never holds a secret, wherever it was found, a file's name included, each value as a path names it too
16
17
  * (pathSpellings) and as a URL's path writes it (percentForms). Every form is found in `text` as given, and overlapping
17
- * finds are redacted as one, so
18
- * one value never hides part of another from the search. A text
19
- * that still holds a value or a shape after that (a form not found as it stands: percent encoded, split by whitespace,
20
- * an NFKC variant), or that is over `max` bytes (PRINT_MAX unless the caller names more), is redacted whole, never in
21
- * part. The folded finds only ever add to that: the whole-text check is first made with them left in (a value split by
22
- * a line break is still seen whole, never hidden by its hex part cut alone), and the fold-aware one always after, on
23
- * what is left (a folded value split by whitespace or percent-encoded is found there, never handed back). */
18
+ * finds are redacted as one, so one value never hides part of another from the search. A text that holds a value in a
19
+ * form not found as it stands (percent encoded, split by whitespace, an NFKC variant), read in `text` as given, is
20
+ * redacted whole, never in part, unless every character of it is inside what the finds cut: a cut can hide part of a
21
+ * value, so the rest is looked for before the cut (redact/match.ts planCut). So is a text over `max` bytes (PRINT_MAX
22
+ * unless the caller names more), and one whose cut leaves a value or a shape. */
24
23
  export declare function redactSecrets(text: string, values: readonly string[], max?: number): string;
25
24
  /** `text` as redactSecrets cuts it, and of a bank's numbers beside their words too (MONEY_SHAPES): what learning prints
26
25
  * and serves of what it learned or refused (a problem's path and detail, a note read), never a run's own view, which
27
26
  * may show its user an account number. */
28
27
  export declare function redactLearned(text: string, values: readonly string[], max?: number): string;
28
+ /** redactSecrets, and where it cut: the spans of `text` cut in place and the text so cut, or "whole" where redactSecrets
29
+ * answers "[redacted]". A reader that redacts a text in pieces (key-refusal.ts) reads the pieces as they came for a
30
+ * value split across them (valueLeft), each piece's cuts covering it. */
31
+ export declare function cutSecrets(text: string, values: readonly string[], max?: number): {
32
+ spans: Span[];
33
+ out: string;
34
+ } | "whole";
35
+ /** Whether `text` holds one of `values` in a form valueMatcher finds, a character of which no cut covers: what
36
+ * a reader that cut `text` there would still show of it. The cuts are `cuts` (merged spans of `text`, in order: where a
37
+ * reader that cut it in pieces cut each), or else those redactSecrets makes of `text` whole (its values' forms, hexes
38
+ * and folded identifiers, and the secret shapes, found as they stand). */
39
+ export declare function valueLeft(text: string, values: readonly string[], cuts?: readonly Span[]): boolean;
29
40
  /** A value shorter than this matches too much text by chance to be refused. */
30
41
  export declare const MIN_SECRET = 8;
31
42
  /** An excerpt of a run's input or answer as learning keeps it: `text` cut of the secrets `shown` knows, whole, then
@@ -38,8 +49,7 @@ export declare function excerptShown(text: string, shown: {
38
49
  }, max: number): string;
39
50
  /** A matcher for `values` in a text: plain, split by whitespace, as JSON writes it, percent or form encoded, base64 or
40
51
  * hex, and each hex identifier in a value folded: in any case, its separators dropped or moved (foldedOf). Values
41
- * shorter than MIN_SECRET are left out. `fold: false`, the matcher without the folded needles (redactSecrets' first
42
- * whole-text check). */
52
+ * shorter than MIN_SECRET are left out. `fold: false`, the matcher without the folded needles. */
43
53
  export declare function valueMatcher(values: readonly string[], { fold }?: {
44
54
  fold?: boolean;
45
55
  }): (text: string) => boolean;
@@ -1,52 +1,5 @@
1
1
  import { posix } from "node:path";
2
- const SK_KEY = /(?<![A-Za-z0-9])sk-[A-Za-z0-9_-]{20,}/;
3
- const SLACK_TOKEN = /xox[abposre]-[A-Za-z0-9-]{10,}|xapp-[A-Za-z0-9-]{10,}/;
4
- const GITHUB_TOKEN = /gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{20,}/;
5
- const JWT = /eyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/;
6
- /** A Standard Webhooks signing secret: `whsec_` and base64, this server's or any other's. */
7
- const WEBHOOK_SECRET = /whsec_[A-Za-z0-9+/]{24,}={0,2}/;
8
- /** The token after a `Bearer` scheme (RFC 6750's characters), in any case: twenty characters or more with a digit among
9
- * them, so a word after "bearer" ("bearer authentication/authorization"), a placeholder (`<token>`, `$TOKEN`,
10
- * `YOUR_API_KEY`) and a challenge's parameter (`Bearer realm="x"`) are none. The token alone is the match: the scheme
11
- * before it stays, so a reader still sees a header was there. The scheme is at most three blanks before the token, and
12
- * the digit is looked for only through the token's own characters, so the reading stays linear. */
13
- const BEARER_TOKEN = /(?<=\bBearer\s{1,3})(?=[A-Za-z._~+/-]*\d)[A-Za-z0-9._~+/-]{20,}={0,2}/i;
14
- /** The value of a URL parameter named like a credential, after `?` or `&`: sixteen characters or more, to the next
15
- * separator. The name is the whole parameter's (`token`, never `page_token`), and the value alone is the match, so the
16
- * name stays. A signed URL's signature is one: whoever holds the URL holds what it grants. No bracket is a value's
17
- * character (a URL writes one percent-encoded), so a value already cut to "[redacted]" is never read as one. */
18
- const URL_CREDENTIAL = /(?<=[?&](?:api[_-]?key|access[_-]?token|auth[_-]?token|id[_-]?token|refresh[_-]?token|client[_-]?secret|x-amz-signature|x-amz-security-token|x-goog-signature|token|secret|password|passwd|signature|sig)=)[^\s&#"'`<>\\[\]]{16,}/i;
19
- const URL_PASSWORD = /:\/\/[^\s/?#@:]+:[^\s/?#@]*@/;
20
- /** A complete, single-quoted sed search expression whose password is only a capture pattern.
21
- * The @ inside [^@] is regex syntax, not a password terminator. Require the closing capture,
22
- * substitution/backreference and quote too; bracketed passwords or bare URLs are never exempt. */
23
- const SED_URL_CAPTURE = /(?:^|[\s;(])sed[ \t]+-n[ \t]+'s\|https?:\/\/(?:[A-Za-z0-9._~-]+|\[\^:\]\*):\\\(\[\^@\]\*\\\)@[A-Za-z0-9.-]+\|\\1\|p'/g;
24
- /** A bank account number beside its word: "account", "acct", "a/c" or "bank account", maybe "number", "no.", "#", ":"
25
- * or "is" after it, then six digits or more, single dashes or spaces between them. A count before the word ("4
26
- * accounts"), a date (YYYY-MM-DD), a year or a card's last four is none: the word must come first, the digits right
27
- * after it, and there must be six. Every gap is at most three whitespace characters, so the reading stays linear on a
28
- * text of long blank runs. */
29
- 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,}/i;
30
- /** An IBAN beside its word: two letters, two check digits, then ten to thirty letters or digits, spaces between. */
31
- const IBAN = /\bIBAN\s{0,3}(?:#|:|\bis\b)?\s{0,3}[A-Z]{2}\d{2}(?:\s?[A-Z0-9]){10,30}/i;
32
- /** A US routing (ABA) number beside its word: nine digits. */
33
- const ROUTING_NUMBER = /\b(?:routing|ABA)(?:\s{1,3}(?:number|no\.?|num))?\s{0,3}(?:#|:|\bis\b)?\s{0,3}\d{9}\b/i;
34
- /** A UK sort code beside its word: three pairs of digits. */
35
- const SORT_CODE = /\bsort\s{1,3}code\s{0,3}(?:#|:|\bis\b)?\s{0,3}\d{2}[- ]?\d{2}[- ]?\d{2}\b/i;
36
- /** Text shaped like a secret, by kind. The same shapes the Pi learning loop's guard refuses. */
37
- const SHAPES = [
38
- ["AWS access key id", /(?:AKIA|ASIA)[0-9A-Z]{16}/],
39
- ["Google API key", /AIza[0-9A-Za-z_-]{35}/],
40
- ["private key block", /-----BEGIN[ A-Z0-9]*PRIVATE KEY[ A-Z0-9]*-----/],
41
- ["GitHub token", GITHUB_TOKEN],
42
- ["sk- key", SK_KEY],
43
- ["Slack token", SLACK_TOKEN],
44
- ["JWT", JWT],
45
- ["webhook signing secret", WEBHOOK_SECRET],
46
- ["URL with a password", URL_PASSWORD],
47
- ["bearer token", BEARER_TOKEN],
48
- ["URL parameter named like a credential", URL_CREDENTIAL],
49
- ];
2
+ import { cutSpans, holds, MONEY_SHAPES, planCut, SECRET_SHAPES, shapeKinds } from "../redact/match.js";
50
3
  /** Whether `text` ends in a `Bearer` scheme and the blanks after it: the one place a shape is read across whitespace
51
4
  * (the scheme, then its token), so a reader that cuts a text at whitespace must not cut there (key-refusal.ts
52
5
  * redactedAnswer). */
@@ -54,65 +7,12 @@ export const endsInScheme = (text) => {
54
7
  const head = text.trimEnd();
55
8
  return head.length < text.length && text.length - head.length <= 3 && /\bBearer$/i.test(head.slice(-7));
56
9
  };
57
- /** The kinds of secret-shaped text in `text`. */
58
- export const secretShapes = (text) => SHAPES.filter(([, pattern]) => pattern === URL_PASSWORD ? spans(text, pattern).length > 0 : pattern.test(text)).map(([kind]) => kind);
59
- /** A bank's numbers beside their words, which learning never keeps: an account, IBAN, routing or sort-code number. Not
60
- * one of SHAPES: a run may show its user an account number (a finance job's answer), and SHAPES cut every run's view;
61
- * only what learning keeps refuses them (LS-09: graders held "send refunds to account 4471-0098" for the owner's OK,
62
- * which would have served it). */
63
- const MONEY_SHAPES = [
64
- ["bank account number", ACCOUNT_NUMBER],
65
- ["IBAN", IBAN],
66
- ["routing number", ROUTING_NUMBER],
67
- ["sort code", SORT_CODE],
68
- ];
10
+ /** The kinds of secret-shaped text in `text` (redact/match.ts SECRET_SHAPES). */
11
+ export const secretShapes = (text) => shapeKinds(text, SECRET_SHAPES);
12
+ /** Every secret shape, then a bank's numbers beside their words (MONEY_SHAPES): what learning never keeps. */
13
+ const LEARNED_SHAPES = [...SECRET_SHAPES, ...MONEY_SHAPES];
69
14
  /** The kinds of text in `text` learning never keeps: every secret shape, and a bank's numbers beside their words. */
70
- export const learnedSecretShapes = (text) => [...secretShapes(text), ...MONEY_SHAPES.filter(([, pattern]) => pattern.test(text)).map(([kind]) => kind)];
71
- /** Every place `pattern` matches in `text`, overlapping places included. */
72
- function spans(text, pattern) {
73
- const re = new RegExp(pattern.source, `${pattern.flags.replace("g", "")}g`);
74
- const out = [];
75
- const expressions = pattern === URL_PASSWORD ? [...text.matchAll(SED_URL_CAPTURE)].map(match => [match.index, match.index + match[0].length]) : [];
76
- let expression = 0;
77
- const next = RESUME.get(pattern) ?? ((m) => m.index + 1);
78
- for (let m = re.exec(text); m; m = re.exec(text)) {
79
- while (expression < expressions.length && expressions[expression][1] <= m.index)
80
- expression++;
81
- const range = expressions[expression];
82
- if (!range || m.index < range[0] || m.index + m[0].length > range[1])
83
- out.push([m.index, m.index + m[0].length]);
84
- re.lastIndex = Math.max(m.index + 1, next(m));
85
- }
86
- return out;
87
- }
88
- const toEnd = (m) => m.index + m[0].length;
89
- /** Where spans() reads on after a match, by shape: past each place inside it where a match would end where it ends, as
90
- * such a match adds nothing to what is redacted. A shape not here reads on from the next character. Without it, a run
91
- * that repeats a shape's start ("sk-sk-sk-…", "github_pat_github_pat_…", a JWT's first part "eyJeyJ…") is read again
92
- * from every repeat.
93
- * - sk- and Slack: a prefix and one greedy run of a class holding the prefix's characters, so a match inside ends at
94
- * the same end.
95
- * - GitHub: the same for github_pat_. A ghp_ run's class has no "_", and a github_pat_ can start in its last characters
96
- * and run past it, so it reads on from the next character.
97
- * - JWT: a match that starts in the first part reads on through the same second and third parts, so it ends where this
98
- * one does; one that starts in the second part can run past it, into a fourth. It reads on from the second part.
99
- * - A URL's credential: its value runs to the next separator and may hold "?" and "=", so a parameter of the same
100
- * name inside it ("?token=a?token=a…") starts a match that runs to the same end.
101
- * - whsec_: not here. Its run's class has no "_", so a run takes the next secret's "whsec" and stops at its "_": that
102
- * secret starts inside this match and runs past it, and is found only by reading on from the next character. */
103
- const RESUME = new Map([
104
- [SK_KEY, toEnd],
105
- [SLACK_TOKEN, toEnd],
106
- [GITHUB_TOKEN, (m) => (m[0].startsWith("github_pat_") ? toEnd(m) : m.index + 1)],
107
- [JWT, (m) => m.index + m[0].indexOf(".") + 1],
108
- [URL_CREDENTIAL, toEnd],
109
- ]);
110
- function places(text, form) {
111
- const out = [];
112
- for (let at = text.indexOf(form); at >= 0; at = text.indexOf(form, at + 1))
113
- out.push([at, at + form.length]);
114
- return out;
115
- }
15
+ export const learnedSecretShapes = (text) => shapeKinds(text, LEARNED_SHAPES);
116
16
  /** The longest field `redactSecrets` reads, in UTF-8 bytes, unless its caller names another bound. A longer one is printed
117
17
  * whole as "[redacted]", never scanned: no field the checker prints is that long in good faith, and a crafted one (a
118
18
  * long path) costs no scan. */
@@ -121,47 +21,51 @@ export const PRINT_MAX = 4096;
121
21
  * any case, base64, a hex identifier in it folded) and each secret-shaped run replaced by "[redacted]": what the
122
22
  * checker prints never holds a secret, wherever it was found, a file's name included, each value as a path names it too
123
23
  * (pathSpellings) and as a URL's path writes it (percentForms). Every form is found in `text` as given, and overlapping
124
- * finds are redacted as one, so
125
- * one value never hides part of another from the search. A text
126
- * that still holds a value or a shape after that (a form not found as it stands: percent encoded, split by whitespace,
127
- * an NFKC variant), or that is over `max` bytes (PRINT_MAX unless the caller names more), is redacted whole, never in
128
- * part. The folded finds only ever add to that: the whole-text check is first made with them left in (a value split by
129
- * a line break is still seen whole, never hidden by its hex part cut alone), and the fold-aware one always after, on
130
- * what is left (a folded value split by whitespace or percent-encoded is found there, never handed back). */
24
+ * finds are redacted as one, so one value never hides part of another from the search. A text that holds a value in a
25
+ * form not found as it stands (percent encoded, split by whitespace, an NFKC variant), read in `text` as given, is
26
+ * redacted whole, never in part, unless every character of it is inside what the finds cut: a cut can hide part of a
27
+ * value, so the rest is looked for before the cut (redact/match.ts planCut). So is a text over `max` bytes (PRINT_MAX
28
+ * unless the caller names more), and one whose cut leaves a value or a shape. */
131
29
  export function redactSecrets(text, values, max = PRINT_MAX) {
132
- return redactWith(text, values, max, SHAPES, secretShapes);
30
+ return redactWith(text, values, max, SECRET_SHAPES);
133
31
  }
134
32
  /** `text` as redactSecrets cuts it, and of a bank's numbers beside their words too (MONEY_SHAPES): what learning prints
135
33
  * and serves of what it learned or refused (a problem's path and detail, a note read), never a run's own view, which
136
34
  * may show its user an account number. */
137
35
  export function redactLearned(text, values, max = PRINT_MAX) {
138
- return redactWith(text, values, max, [...SHAPES, ...MONEY_SHAPES], learnedSecretShapes);
36
+ return redactWith(text, values, max, LEARNED_SHAPES);
139
37
  }
140
- /** redactSecrets over `shapes`, each found in place, and `shapesOf` read of what is left (a shape still there cuts the
141
- * text whole). */
142
- function redactWith(text, values, max, shapes, shapesOf) {
38
+ /** redactSecrets, and where it cut: the spans of `text` cut in place and the text so cut, or "whole" where redactSecrets
39
+ * answers "[redacted]". A reader that redacts a text in pieces (key-refusal.ts) reads the pieces as they came for a
40
+ * value split across them (valueLeft), each piece's cuts covering it. */
41
+ export function cutSecrets(text, values, max = PRINT_MAX) {
42
+ if (Buffer.byteLength(text) > max)
43
+ return "whole";
44
+ return planCut(text, derivedOf(values), SECRET_SHAPES);
45
+ }
46
+ /** Whether `text` holds one of `values` in a form valueMatcher finds, a character of which no cut covers: what
47
+ * a reader that cut `text` there would still show of it. The cuts are `cuts` (merged spans of `text`, in order: where a
48
+ * reader that cut it in pieces cut each), or else those redactSecrets makes of `text` whole (its values' forms, hexes
49
+ * and folded identifiers, and the secret shapes, found as they stand). */
50
+ export function valueLeft(text, values, cuts) {
51
+ const needles = derivedOf(values);
52
+ return holds(text, needles, { cuts: cuts ?? cutSpans(text, needles, SECRET_SHAPES) });
53
+ }
54
+ /** redactSecrets over `shapes`, each found in place (redact/match.ts planCut). */
55
+ function redactWith(text, values, max, shapes) {
143
56
  if (Buffer.byteLength(text) > max)
144
57
  return "[redacted]";
145
- const { forms, hexes, folds: foldPatterns, matchUnfolded, match } = derivedOf(values);
146
- const found = [
147
- ...forms.flatMap((form) => places(text, form)),
148
- ...hexes.flatMap((hex) => spans(text, hex)),
149
- ...shapes.flatMap(([, pattern]) => spans(text, pattern)),
150
- ];
151
- const unfolded = cutAt(text, found);
152
- if (matchUnfolded(unfolded) || shapesOf(unfolded).length > 0)
153
- return "[redacted]";
154
- // Then the folded finds cut in place, and the fold-aware check over what is left, always: a folded value split by
155
- // whitespace or percent-encoded has no span here, but the matcher's squeezed and decoded views still see it.
156
- const folds = foldPatterns.flatMap((pattern) => spans(text, pattern));
157
- const out = folds.length ? cutAt(text, [...found, ...folds]) : unfolded;
158
- return match(out) || shapesOf(out).length > 0 ? "[redacted]" : out;
58
+ const cut = planCut(text, derivedOf(values), shapes);
59
+ return cut === "whole" ? "[redacted]" : cut.out;
159
60
  }
160
- /** Each value set's Derived, by the array a caller passes, with a copy of the values it was derived of. A run's view
61
+ /** Each value set's Needles, by the array a caller passes, with a copy of the values it was derived of. A run's view
161
62
  * redacts every row of its timeline with one array, and deriving it again for each row (path spellings, base64 at three
162
- * alignments, percent forms, folded patterns, for every value, a private key's PEM among them) took seconds per view.
63
+ * alignments, percent forms, folded needles, for every value, a private key's PEM among them) took seconds per view.
163
64
  * An array whose values changed since is derived again, never served a stale set: what a caller adds is always cut. */
164
65
  const DERIVED = new WeakMap();
66
+ /** What the matcher looks for of a set of values, whatever the text: each value and its path spellings, its forms found
67
+ * as they stand (as written, as JSON writes it, its plain needles and percent forms), its hex and folded needles, and
68
+ * the needles the whole-text check reads (redact/match.ts Needles). */
165
69
  function derivedOf(values) {
166
70
  const kept = DERIVED.get(values);
167
71
  if (kept && kept.values.length === values.length && kept.values.every((value, at) => value === values[at]))
@@ -173,32 +77,13 @@ function derivedOf(values) {
173
77
  const of = needles.get(v);
174
78
  return [v, asJson(v), ...(of ? [...of.plain, ...percentForms(v)] : [])];
175
79
  }).filter((form) => form.length > 0))],
176
- hexes: [...needles.values()].map((of) => new RegExp(of.hex, "i")),
177
- folds: [...needles.values()].flatMap((of) => of.folded.map(foldedPattern)),
178
- matchUnfolded: spelledMatcher(given, false),
179
- match: spelledMatcher(given),
80
+ hexes: [...needles.values()].map((of) => of.hex),
81
+ folds: [...needles.values()].flatMap((of) => of.folded),
82
+ plain: [...new Set([...needles.values()].flatMap((of) => of.plain))],
180
83
  };
181
84
  DERIVED.set(values, { values: [...values], derived });
182
85
  return derived;
183
86
  }
184
- /** `text` with each of `found` replaced by "[redacted]", overlapping ones as one. */
185
- function cutAt(text, found) {
186
- const merged = [];
187
- for (const [start, end] of [...found].sort((a, b) => a[0] - b[0])) {
188
- const last = merged.at(-1);
189
- if (last && start <= last[1])
190
- last[1] = Math.max(last[1], end);
191
- else
192
- merged.push([start, end]);
193
- }
194
- let out = "";
195
- let from = 0;
196
- for (const [start, end] of merged) {
197
- out += `${text.slice(from, start)}[redacted]`;
198
- from = end;
199
- }
200
- return out + text.slice(from);
201
- }
202
87
  /** A value shorter than this matches too much text by chance to be refused. */
203
88
  export const MIN_SECRET = 8;
204
89
  /** An excerpt of a run's input or answer as learning keeps it: `text` cut of the secrets `shown` knows, whole, then
@@ -226,9 +111,6 @@ function base64Needles(secret) {
226
111
  }
227
112
  return [...out];
228
113
  }
229
- function percentDecode(text, plusIsSpace) {
230
- return (plusIsSpace ? text.replaceAll("+", " ") : text).replace(/(?:%[0-9a-fA-F]{2})+/g, (run) => Buffer.from(run.replaceAll("%", ""), "hex").toString("utf8"));
231
- }
232
114
  const asJson = (value) => JSON.stringify(value).slice(1, -1);
233
115
  /** A path with each run of "/" collapsed to one, each "." segment dropped ("/./", a leading "./") and a leading and a
234
116
  * trailing "/" dropped, as a resolver writes it. */
@@ -300,8 +182,6 @@ function foldedOf(value) {
300
182
  }
301
183
  return [...out];
302
184
  }
303
- /** A folded needle as it stands in a text: its hex digits in order, in any case, a separator allowed between any two. */
304
- const foldedPattern = (needle) => new RegExp([...needle].join("[-_:.]?"), "i");
305
185
  /** What a matcher looks for of `value`: its forms as they stand in a squeezed text (plain, as JSON writes it, base64 or
306
186
  * base64url), its hex, in any case, and its hex identifiers folded (foldedOf). */
307
187
  function needlesOf(value) {
@@ -311,20 +191,8 @@ function needlesOf(value) {
311
191
  const longEnough = (value) => [...value].length >= MIN_SECRET;
312
192
  /** A matcher for `values` in a text: plain, split by whitespace, as JSON writes it, percent or form encoded, base64 or
313
193
  * hex, and each hex identifier in a value folded: in any case, its separators dropped or moved (foldedOf). Values
314
- * shorter than MIN_SECRET are left out. `fold: false`, the matcher without the folded needles (redactSecrets' first
315
- * whole-text check). */
194
+ * shorter than MIN_SECRET are left out. `fold: false`, the matcher without the folded needles. */
316
195
  export function valueMatcher(values, { fold = true } = {}) {
317
- return spelledMatcher(withPathSpellings(values), fold);
318
- }
319
- /** valueMatcher over values already given with their path spellings (redactSecrets'), each spelled once. */
320
- function spelledMatcher(spelled, fold = true) {
321
- const needles = spelled.filter(longEnough).map(needlesOf).map((n) => (fold ? n : { ...n, folded: [] }));
322
- if (!needles.length)
323
- return () => false;
324
- return (text) => {
325
- const views = [...new Set([squeeze(text), squeeze(percentDecode(text, false)), squeeze(percentDecode(text, true))])];
326
- const lower = views.map((view) => view.toLowerCase());
327
- const flat = needles.some((n) => n.folded.length) ? views.map(folded) : [];
328
- return needles.some((n) => views.some((view) => n.plain.some((needle) => view.includes(needle))) || lower.some((view) => view.includes(n.hex)) || n.folded.some((needle) => flat.some((view) => view.includes(needle))));
329
- };
196
+ const needles = derivedOf(values);
197
+ return (text) => holds(text, needles, { fold });
330
198
  }