@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
@@ -9,6 +9,10 @@ import { secretLintProfiler } from "@secretlint/profiler";
9
9
  secretLintProfiler.setEnabled(false);
10
10
  import { keyShaped } from "./secret-name.js";
11
11
  import { secretValues } from "./api-token.js";
12
+ import { MIN_SECRET, valueMatcher } from "./learn/secrets.js";
13
+ import { userInfo } from "node:os";
14
+ import { commandReadPath, decodedTimes, inputSpan, isPlaceholder, KEY_RUN_MAX, lineBounds, lineCount, RESTORABLE, SCRUB_RULES, shapeOf, viewsOf } from "./secret-rules.js";
15
+ export { isPlaceholder, RESTORABLE, RULE_PATTERNS, SCRUB_RULES, viewsOf } from "./secret-rules.js";
12
16
  /** Whether a name is a secret's, whatever list names it or none does: one of its `_`-separated segments, in any case, is
13
17
  * KEY, TOKEN, SECRET, PASSWORD, PASSWD, PASS, PAT, CREDENTIAL, CREDENTIALS or DSN, or its last is PWD after another.
14
18
  * The one rule: this cut, the harness log's and a run's environment (secrets.ts' withoutSecrets)
@@ -86,6 +90,11 @@ export const secretlintCalls = { count: 0 };
86
90
  * itself where the rule gives it, else what it matched), and as a JSON string holding it would read it, decoded up to
87
91
  * three times, so the cut finds it in a record's parsed text and in a shell's output kept in one as JSON. */
88
92
  export async function secretsIn(content) {
93
+ return [...new Set((await lintFinds(content)).map((find) => find.value))];
94
+ }
95
+ /** secretsIn's finds, each with the message secretlint reported it under (what anchors it: the scrubber's rules read a
96
+ * label-anchored message apart from a format's). */
97
+ export async function lintFinds(content) {
89
98
  if (!content)
90
99
  return [];
91
100
  secretlintCalls.count += 1;
@@ -99,14 +108,292 @@ export async function secretsIn(content) {
99
108
  return undefined;
100
109
  }
101
110
  };
102
- const found = new Set();
111
+ const found = new Map();
103
112
  for (const message of messages) {
113
+ const id = message.messageId ?? message.ruleId;
104
114
  const named = Object.values(message.data ?? {}).filter((value) => typeof value === "string" && value.length >= 8 && content.includes(value));
105
115
  for (const each of named.length ? named : [content.slice(message.range[0], message.range[1])]) {
106
116
  let value = each;
107
- for (let level = 0; value !== undefined && level < 3; level++)
108
- (found.add(value), (value = decoded(value)));
117
+ for (let level = 0; value !== undefined && level < 3; level++) {
118
+ if (!found.has(`${id}\u0000${value}`))
119
+ found.set(`${id}\u0000${value}`, { id, value });
120
+ value = decoded(value);
121
+ }
122
+ }
123
+ }
124
+ return [...found.values()];
125
+ }
126
+ /** The longest piece of a text one pass reads: a longer text is read in pieces this long, each overlapping the next by
127
+ * SCRUB_OVERLAP (more than any rule's longest match), so a value across a piece's end is read whole in the next one.
128
+ * Never truncated, never skipped (ruling B). */
129
+ export const SCRUB_MAX_CHARS = 32 * 1024 * 1024;
130
+ export const SCRUB_OVERLAP = 1024 * 1024;
131
+ /** The most time a scrub may take, its final check included: past it, the scrub throws (cut mode fails closed, count
132
+ * mode rejects). Every rule is linear, so it is not met in good faith. */
133
+ export const SCRUB_BUDGET_MS = 30_000;
134
+ /** Which hit names a cut where hits overlap (ruling C): the kind that drops the most and may not be restored first,
135
+ * then the longest, then the deeper view, then the table's order. */
136
+ const KIND_RANK = { held: 0, format: 1, dump: 2, label: 3, personal: 4, jev: 5 };
137
+ /** A cut's text in the output: its rule's replacement (a home folder's `~`), a value's `[redacted:<rule>]`, a dump's
138
+ * `[redacted:<rule> N lines]`, N counted in the view that found it (a JSON string's escaped line breaks are lines). */
139
+ const marker = (rule, value) => {
140
+ if (rule.replacement !== undefined)
141
+ return rule.replacement;
142
+ if (rule.kind !== "dump")
143
+ return `[redacted:${rule.id}]`;
144
+ return `[redacted:${rule.id} ${lineCount(value)} lines]`;
145
+ };
146
+ const MARKER = /\[redacted:[a-z0-9-]{1,64}(?: \d{1,9} lines)?\]/g;
147
+ const QUOTES = "\"'";
148
+ const runOf = (limits) => {
149
+ const now = limits.now ?? (() => performance.now());
150
+ const maxChars = limits.maxChars ?? SCRUB_MAX_CHARS;
151
+ const overlap = Math.min(limits.overlap ?? SCRUB_OVERLAP, Math.floor(maxChars / 2));
152
+ // A chunk's overlap holds a key's longest run (ruling XXXIII), so a key that straddles a bound is whole in one chunk;
153
+ // a chunk too short for that (a test's 4096) holds no such run anyway.
154
+ if (maxChars >= 2 * KEY_RUN_MAX && overlap < KEY_RUN_MAX)
155
+ throw new Error(`scrub: overlap ${overlap} is below KEY_RUN_MAX ${KEY_RUN_MAX}`);
156
+ return { maxChars, overlap, deadline: now() + (limits.budgetMs ?? SCRUB_BUDGET_MS), now };
157
+ };
158
+ const inTime = (run) => {
159
+ if (run.now() > run.deadline)
160
+ throw new Error("scrub: over its time budget");
161
+ };
162
+ /** The [start, end) of each chunk of `text`: one chunk when it fits, else maxChars long each, each starting `overlap`
163
+ * before the last one's end; a bound that falls inside a JSON escape moves to the escape's start, so no chunk starts
164
+ * or ends inside one (ruling XV). */
165
+ function chunksOf(text, run) {
166
+ const length = text.length;
167
+ if (length <= run.maxChars)
168
+ return [[0, length]];
169
+ const out = [];
170
+ for (let start = 0;; start += run.maxChars - run.overlap) {
171
+ const end = Math.min(length, start + run.maxChars);
172
+ out.push([escapeStartAt(text, start), escapeStartAt(text, end)]);
173
+ if (end === length)
174
+ return out;
175
+ }
176
+ }
177
+ const ESCAPE_LETTERS = "\"\\/bfnrt";
178
+ const HEX4 = /^[0-9A-Fa-f]{4}$/;
179
+ /** `at`, or the start of the JSON escape `at` falls strictly inside (`\n`, `\uXXXX`), read as a JSON string reads it:
180
+ * a backslash opens an escape when an even number of backslashes stand right before it. */
181
+ function escapeStartAt(text, at) {
182
+ for (let from = at - 1; from >= 0 && from >= at - 5; from--) {
183
+ if (text.charCodeAt(from) !== 92)
184
+ continue;
185
+ let before = 0;
186
+ while (from - before - 1 >= 0 && text.charCodeAt(from - before - 1) === 92)
187
+ before++;
188
+ if (before % 2)
189
+ continue;
190
+ const letter = text.charAt(from + 1);
191
+ const end = ESCAPE_LETTERS.includes(letter) && letter !== "" ? from + 2 : letter === "u" && HEX4.test(text.slice(from + 2, from + 6)) ? from + 6 : from + 1;
192
+ return from < at && at < end ? from : at;
193
+ }
194
+ return at;
195
+ }
196
+ /** The closing quote of the innermost string (of `lists`, each sorted and disjoint) that holds `at`, or Infinity. */
197
+ function stringEnd(lists, at) {
198
+ let end = Number.POSITIVE_INFINITY;
199
+ for (const spans of lists) {
200
+ let low = 0;
201
+ let high = spans.length;
202
+ while (low < high) {
203
+ const mid = (low + high) >> 1;
204
+ if (spans[mid][0] < at)
205
+ low = mid + 1;
206
+ else
207
+ high = mid;
208
+ }
209
+ const span = spans[low - 1];
210
+ if (span && span[1] >= at)
211
+ end = Math.min(end, span[1]);
212
+ }
213
+ return end;
214
+ }
215
+ /** Every rule's hits in one view of a chunk, mapped to the input (`offset`: the chunk's start in it), each widened to
216
+ * hold an escape it starts or ends inside. A label rule's hit on a placeholder (isPlaceholder) is no hit, whichever
217
+ * label rule found it. */
218
+ async function viewHits(rules, view, offset, opts, run, out) {
219
+ const ctx = contextOf(view, opts);
220
+ for (const [order, rule] of rules.entries()) {
221
+ if (!Object.hasOwn(RESTORABLE, rule.kind))
222
+ throw new Error(`scrub: rule ${rule.id} has no kind`);
223
+ for (const hit of await rule.find(view.text, ctx)) {
224
+ if (!Number.isInteger(hit.start) || !Number.isInteger(hit.end) || hit.start < 0 || hit.end > view.text.length || hit.end < hit.start)
225
+ throw new Error(`scrub: rule ${rule.id} gave a span outside the text`);
226
+ if (hit.end === hit.start)
227
+ continue;
228
+ if (rule.kind === "label") {
229
+ // What follows the value is read on its own line or in its own string only (ruling LIV, XLVI's bound): a JSON
230
+ // string's end and its object's `}` or `,` after it are no code's evidence for the value.
231
+ const slot = { name: hit.name, bare: hit.quoted !== true && !QUOTES.includes(view.source.charAt(hit.start - 1) || " "), after: view.text.slice(hit.end, Math.min(hit.end + 8, ctx.lines?.end(hit.end) ?? view.text.length)), code: hit.code, typed: hit.typed };
232
+ if (isPlaceholder(view.text.slice(hit.start, hit.end), slot))
233
+ continue;
234
+ }
235
+ const [start, end] = inputSpan(view, hit.start, hit.end);
236
+ if (end > start)
237
+ out.push({ start: offset + start, end: offset + end, rule, order, depth: view.depth });
238
+ }
239
+ inTime(run);
240
+ }
241
+ }
242
+ /** Every rule's hits in every view of every chunk of `text` (ruling A: the union), resolved to cuts: hits that overlap
243
+ * are one cut over all of them, named for the strongest (ruling C: KIND_RANK, then the longest in the input, then the
244
+ * deeper view, then the table's order). Each cut's offsets are the input's; its shape and its marker are read from its
245
+ * text decoded as often as the view that named it was. */
246
+ async function cutsOf(rules, text, opts, run) {
247
+ const hits = [];
248
+ for (const [from, to] of chunksOf(text, run)) {
249
+ const found = hits.length;
250
+ const views = viewsOf(text.slice(from, to));
251
+ for (const view of views)
252
+ await viewHits(rules, view, from, opts, run, hits);
253
+ // A hit that starts inside a JSON string ends by its closing quote at the latest (ruling XLIX): a string any view
254
+ // reads, in the input's offsets (a decoded string's quote is its escape there, `\"`), so a cut never eats a
255
+ // string's end and the output still parses.
256
+ const strings = views.map((view) => view.strings.map(([open, close]) => [from + view.at(open), from + view.at(close)]));
257
+ for (let at = found; at < hits.length; at++) {
258
+ const hit = hits[at];
259
+ hit.end = Math.min(hit.end, stringEnd(strings, hit.start));
260
+ if (hit.end <= hit.start)
261
+ hits.splice(at--, 1);
262
+ }
263
+ // A hit widened past its chunk's bounds read an escape the chunk split: fails closed (ruling XV).
264
+ for (let at = found; at < hits.length; at++)
265
+ if (hits[at].start < from || hits[at].end > to)
266
+ throw new Error("scrub: a cut crosses its chunk's bounds");
267
+ }
268
+ hits.sort((a, b) => a.start - b.start || b.end - a.end);
269
+ const stronger = (a, b) => {
270
+ const rank = KIND_RANK[a.rule.kind] - KIND_RANK[b.rule.kind];
271
+ if (rank)
272
+ return rank < 0;
273
+ const length = a.end - a.start - (b.end - b.start);
274
+ if (length)
275
+ return length > 0;
276
+ if (a.depth !== b.depth)
277
+ return a.depth > b.depth;
278
+ return a.order < b.order;
279
+ };
280
+ const cuts = [];
281
+ for (let at = 0; at < hits.length;) {
282
+ let best = hits[at];
283
+ const start = best.start;
284
+ let end = best.end;
285
+ let next = at + 1;
286
+ for (; next < hits.length && hits[next].start < end; next++) {
287
+ const hit = hits[next];
288
+ end = Math.max(end, hit.end);
289
+ if (stronger(hit, best))
290
+ best = hit;
291
+ }
292
+ const { rule } = best;
293
+ const value = decodedTimes(text.slice(start, end), best.depth);
294
+ cuts.push({ cut: { rule: rule.id, kind: rule.kind, restorable: RESTORABLE[rule.kind], start, end, shape: shapeOf(rule, value) }, marker: marker(rule, value) });
295
+ at = next;
296
+ }
297
+ return cuts;
298
+ }
299
+ const countsOf = (cuts) => {
300
+ const counts = {};
301
+ for (const cut of cuts)
302
+ counts[cut.rule] = (counts[cut.rule] ?? 0) + 1;
303
+ return counts;
304
+ };
305
+ /** The text with each cut's marker in its place, or its own text where `keep` (cut mode alone) leaves it. */
306
+ function cutText(text, cuts, keep) {
307
+ const parts = [];
308
+ let length = 0;
309
+ let from = 0;
310
+ const made = [];
311
+ for (const { cut, marker } of cuts) {
312
+ const value = text.slice(cut.start, cut.end);
313
+ // RESTORABLE, never the cut's own field, decides whether `keep` is asked at all.
314
+ const kept = RESTORABLE[cut.kind] && keep !== undefined && keep(Object.freeze({ ...cut })) === true;
315
+ const before = text.slice(from, cut.start);
316
+ const put = kept ? value : marker;
317
+ parts.push(before, put);
318
+ const outStart = length + before.length;
319
+ length = outStart + put.length;
320
+ made.push({ ...cut, outStart, outEnd: length, ...(kept ? { kept: true } : {}) });
321
+ from = cut.end;
322
+ }
323
+ parts.push(text.slice(from));
324
+ return { out: parts.join(""), made };
325
+ }
326
+ export async function scrubWith(rules, text, opts, limits = {}) {
327
+ if (opts.mode === "count") {
328
+ // A throw here rejects: a scorer must never read an error as zero cuts. The same output cut mode makes (no keep)
329
+ // and the same final check say whether cut mode would fail closed.
330
+ const run = runOf(limits);
331
+ const cuts = await cutsOf(rules, text, opts, run);
332
+ const blocked = !(await clean(rules, cutText(text, cuts, undefined).out, opts, run));
333
+ const made = cuts.map(({ cut }) => cut);
334
+ return { counts: countsOf(made), cuts: made, blocked };
335
+ }
336
+ try {
337
+ const run = runOf(limits);
338
+ const { out, made } = cutText(text, await cutsOf(rules, text, opts, run), opts.keep);
339
+ if (!(await clean(rules, out, opts, run)))
340
+ throw new Error("scrub: the cut text is not clean");
341
+ return { ok: true, text: out, cuts: made, counts: countsOf(made) };
342
+ }
343
+ catch {
344
+ // Fails closed: whatever threw, no text, no cut, no count leaves.
345
+ return { ok: false, counts: {} };
346
+ }
347
+ }
348
+ export function scrub(text, opts) {
349
+ return scrubWith(SCRUB_RULES, text, opts);
350
+ }
351
+ const WHITESPACE = /\s/g;
352
+ const heldOf = (opts) => [...new Set((opts.held ?? []).filter((value) => value.replace(WHITESPACE, "").length >= MIN_SECRET))];
353
+ /** This process's home folders: its user's (the password database's) and $HOME. */
354
+ export function processHomes() {
355
+ const homes = new Set();
356
+ try {
357
+ homes.add(userInfo().homedir);
358
+ }
359
+ catch {
360
+ // No user entry (a container's uid with none): $HOME alone.
361
+ }
362
+ if (process.env.HOME)
363
+ homes.add(process.env.HOME);
364
+ return [...homes].filter((home) => home.length > 1);
365
+ }
366
+ /** What the rules read beside a view: the held values, the view's unblanked text, the homes, the path and the command, and secretlint's
367
+ * finds in the view (one pass a view, on first ask). */
368
+ function contextOf(view, opts) {
369
+ let lint;
370
+ return { held: heldOf(opts), source: view.source, homes: opts.homes ?? processHomes(), path: opts.path ?? commandReadPath(opts.command), command: opts.command, lint: () => (lint ??= lintFinds(view.text)), lines: lineBounds(view.text, ...view.decodedStrings, view.strings) };
371
+ }
372
+ /** Whether a cut text holds nothing the scrub must cut: no known format the format rules still find in any view of any
373
+ * chunk of it (each marker read as a blank, so a marker is never one), and no held value in any form (valueMatcher:
374
+ * split by whitespace, partly encoded) in the text as written or in any decoding of it, each marker read as a
375
+ * character no form holds. The time budget holds here too (a throw). */
376
+ async function clean(rules, out, opts, run) {
377
+ const blanked = out.replace(MARKER, (found) => " ".repeat(found.length));
378
+ const formats = rules.filter((rule) => rule.kind === "format");
379
+ for (const [from, to] of chunksOf(blanked, run)) {
380
+ for (const view of viewsOf(blanked.slice(from, to))) {
381
+ const again = { held: [], source: view.source, homes: [], lint: async () => [], lines: lineBounds(view.text, ...view.decodedStrings, view.strings) };
382
+ for (const rule of formats) {
383
+ if ((await rule.find(view.text, again)).some((hit) => hit.end > hit.start))
384
+ return false;
385
+ inTime(run);
386
+ }
109
387
  }
110
388
  }
111
- return [...found];
389
+ const held = heldOf(opts);
390
+ if (!held.length)
391
+ return true;
392
+ const matches = valueMatcher(held);
393
+ for (const view of viewsOf(out.replace(MARKER, "\u0000"))) {
394
+ if (matches(view.source))
395
+ return false;
396
+ inTime(run);
397
+ }
398
+ return true;
112
399
  }
@@ -0,0 +1,143 @@
1
+ export type CutKind = "format" | "held" | "dump" | "label" | "personal" | "jev";
2
+ /** Whether a cut of each kind may be put back by a preview: fixed per kind, never per rule or per cut. */
3
+ export declare const RESTORABLE: Readonly<Record<CutKind, boolean>>;
4
+ /** A place a rule matched, by offsets into the text it read; a label rule names its label (`name`: a variable's name, a
5
+ * flag, a header) so the placeholder check can tell a metavar (`--token TOKEN`) from a value. `code: false` marks a slot
6
+ * code does not fill (an UPPER_NAME=, a YAML key at a line's start, a flag's value): there a code shape is a value
7
+ * (ruling XII). `typed` marks a slot a type annotation reads the same as (`name: Type`, a YAML key or a Python
8
+ * annotation), where a type's name stays no value. */
9
+ export type Hit = {
10
+ readonly start: number;
11
+ readonly end: number;
12
+ readonly name?: string;
13
+ readonly quoted?: boolean;
14
+ readonly code?: boolean;
15
+ readonly typed?: boolean;
16
+ };
17
+ /** One of secretlint's finds: the message it reports (its anchor: `AWSSecretAccessKey`, `NPM_ACCESS_TOKEN`, ...) and the
18
+ * value, as it names it and as each JSON string holding it would read it. */
19
+ export type LintFind = {
20
+ readonly id: string;
21
+ readonly value: string;
22
+ };
23
+ /** What a rule may read beside the view's text: the held values (8 characters or more), the view's text before its
24
+ * escapes were blanked (`source`: the held rule reads a held value holding a backslash there), the home folders the
25
+ * caller holds, the path the text is the content of and the command it is the output of when the caller knows them, and secretlint's finds in the view (read
26
+ * once a view, on first ask). */
27
+ export type ScrubContext = {
28
+ readonly held: readonly string[];
29
+ readonly source: string;
30
+ readonly homes: readonly string[];
31
+ readonly path?: string;
32
+ /** The shell command whose output the text is, when the caller knows it (ruling LVI): an env command's output is a
33
+ * dump though the command stands in no line of the text. */
34
+ readonly command?: string;
35
+ readonly lint: () => Promise<readonly LintFind[]>;
36
+ /** Where the line holding an offset starts and ends: a line break, or a JSON string's own quote the view decoded
37
+ * (ruling XXVIII). Absent: line breaks alone. */
38
+ readonly lines?: LineBounds;
39
+ };
40
+ /** A view's lines: the start of the line holding `at` (after a line break, or after the opening quote of the decoded
41
+ * string holding it) and that line's end (a line break, or the string's closing quote). */
42
+ export type LineBounds = {
43
+ readonly start: (at: number) => number;
44
+ readonly end: (at: number) => number;
45
+ };
46
+ /** A string's span, [its opening quote, its closing quote]. */
47
+ export type StringSpan = readonly [number, number];
48
+ /** `text`'s line bounds. Inside a string (one of `strings`, sorted and disjoint per list: the strings the view was
49
+ * decoded from, and its own), a line starts after the string's opening quote and ends at its closing quote, or at a
50
+ * line break in between. A position outside every string is bounded by line breaks alone (ruling XLVI): a value that
51
+ * starts outside every string never ends at a string's start (`os.environ["X"]`). */
52
+ export declare function lineBounds(text: string, ...lists: readonly (readonly StringSpan[])[]): LineBounds;
53
+ export type ScrubRule = {
54
+ readonly id: string;
55
+ readonly kind: CutKind;
56
+ readonly restorable: boolean;
57
+ /** The class a cut's shape names in place of a prefix (personal rules: "email", "ip", "home"). */
58
+ readonly shapeClass?: string;
59
+ /** The text a cut puts in the value's place, when not `[redacted:<id>]` (a home folder's: `~`). */
60
+ readonly replacement?: string;
61
+ readonly find: (text: string, ctx: ScrubContext) => readonly Hit[] | Promise<readonly Hit[]>;
62
+ };
63
+ /** One cut: its rule, kind, whether a preview may restore it, its span in the INPUT text, and its shape (a public prefix
64
+ * or a character class, and a length), never its value. In cut mode, `outStart`/`outEnd` are its replacement's span in
65
+ * the output (the input's own text there when `kept`: a restorable cut the caller's `keep` chose to leave). */
66
+ export type Cut = {
67
+ rule: string;
68
+ kind: CutKind;
69
+ restorable: boolean;
70
+ start: number;
71
+ end: number;
72
+ shape: string;
73
+ outStart?: number;
74
+ outEnd?: number;
75
+ kept?: true;
76
+ };
77
+ /** One reading of a text: `text` is what every rule reads (the decoded text with each escape still in it blanked, and
78
+ * each control character but a tab or a line break read as a blank); `source` is the same before that blanking (the
79
+ * held rule's); `depth` how many times the input was decoded to make it; `escapes` the spans of the escapes it blanked
80
+ * (a backslash it left as a typed path's separator is no escape: a hit is never widened over it, ruling XIII); `at`
81
+ * maps an offset in it to the input's (`at(text.length)` is the input's length). */
82
+ export type View = {
83
+ readonly text: string;
84
+ readonly source: string;
85
+ readonly depth: number;
86
+ readonly escapes: readonly (readonly [number, number])[];
87
+ readonly at: (offset: number) => number;
88
+ /** The quotes of the JSON strings it was decoded from, as written there (blanked in `text`). */
89
+ readonly delimiters: readonly number[];
90
+ /** The strings it was decoded from (their quotes `delimiters`), one list per view above it, each paired in the view
91
+ * that held it as its own (a string inside a string is its own level's). */
92
+ readonly decodedStrings: readonly (readonly StringSpan[])[];
93
+ /** Its own JSON strings (ruling XXXIX: whether or not there is anything to decode): its quotes, no escape's and none
94
+ * of `delimiters`, paired the JSON way (pairQuotes). Sorted. */
95
+ readonly strings: readonly StringSpan[];
96
+ };
97
+ /** The most times a text is decoded: a JSON string inside a JSON string inside one (a tool's output kept in a record). */
98
+ export declare const VIEW_DEPTH = 3;
99
+ /** `text`'s views: the raw text, then each JSON decoding of it up to VIEW_DEPTH times, while a decoding changes it. */
100
+ export declare function viewsOf(text: string): View[];
101
+ /** A view's span widened to hold each escape it starts or ends inside, mapped to the input: a cut never splits an
102
+ * escape, so a cut JSON string stays one. The escape holding each end is found by halves (ruling LXXXIII): a walk
103
+ * from the first escape is one per hit, quadratic in a JSON text holding many hits. */
104
+ export declare function inputSpan(view: View, start: number, end: number): [number, number];
105
+ /** `text` decoded `times` times (a cut's text as the view that found it reads it: its shape and its line count). */
106
+ export declare function decodedTimes(text: string, times: number): string;
107
+ /** A cut's shape: a dump's line count, a personal rule's class, or a public prefix (else a character class), and the
108
+ * cut's length in characters. */
109
+ export declare function shapeOf(of: Pick<ScrubRule, "kind" | "shapeClass">, value: string): string;
110
+ export declare const lineCount: (value: string) => number;
111
+ /** Where a value stands, as the placeholder check reads it: its label's name, whether it is bare (no quote before
112
+ * it), the text right after it, `code: false` for a slot code does not fill and `typed` for one a type annotation fills
113
+ * (Hit.code, Hit.typed). */
114
+ export type Slot = {
115
+ readonly name?: string;
116
+ readonly bare?: boolean;
117
+ readonly after?: string;
118
+ readonly code?: boolean;
119
+ readonly typed?: boolean;
120
+ };
121
+ export declare function isPlaceholder(value: string, slot?: Slot): boolean;
122
+ /** The private-key spans SECRET_SHAPES finds, each read from its BEGIN line's end to its block's end: the next END line,
123
+ * when only a block's characters stand between; else the base64 run after it. A span is its header alone or its whole
124
+ * block (either shape of redact/match.ts), so the header's end is read at the span's start; with no BEGIN line there
125
+ * this rule reads, the span's end stands. A header inside a block already cut is skipped, so a text of repeated
126
+ * headers is read once. Exported for its test. */
127
+ export declare function pemBlocks(text: string, spans: readonly (readonly [number, number])[], lines: LineBounds): Hit[];
128
+ /** How far a key's run is read past its shape's match (ruling XI): a longer run fails the scrub closed. Below
129
+ * SCRUB_OVERLAP, so a chunk's overlap still holds every cut whole. */
130
+ export declare const KEY_RUN_MAX = 65536;
131
+ /** The file a command prints whole, when it is one read alone (`cat ~/.pgpass`): its output is that file's content
132
+ * (ruling LVI). */
133
+ export declare const commandReadPath: (command: string | undefined) => string | undefined;
134
+ /** A .env file's path: .env, .envrc, .env.<name>, <name>.env; never a template (.env.example, .sample, ...). */
135
+ export declare const envFilePath: (path: string) => boolean;
136
+ /** A path under a .ssh folder. */
137
+ export declare const sshFilePath: (path: string) => boolean;
138
+ export declare const credentialFilePath: (path: string) => boolean;
139
+ export declare const commandPaths: (command: string | undefined) => string[];
140
+ export declare const SCRUB_RULES: readonly ScrubRule[];
141
+ /** Every module-level pattern the rules read, by name: secret-scrub.test.mjs walks each (redact/match.ts SECRET_SHAPES are scanners, linear by construction)
142
+ * and fails on an unbounded quantifier outside its allowlist, and fails on a module-level pattern missing here. */
143
+ export declare const RULE_PATTERNS: Readonly<Record<string, RegExp>>;