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

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 (275) hide show
  1. package/CHANGELOG.md +229 -0
  2. package/README.md +201 -52
  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 +17 -5
  11. package/dist/api-only.js +34 -8
  12. package/dist/browser-hold.d.ts +23 -0
  13. package/dist/browser-hold.js +48 -0
  14. package/dist/browser-person.d.ts +3 -0
  15. package/dist/browser-person.js +4 -1
  16. package/dist/chatgpt-plan.d.ts +18 -8
  17. package/dist/chatgpt-plan.js +91 -34
  18. package/dist/claude-login.d.ts +74 -16
  19. package/dist/claude-login.js +215 -56
  20. package/dist/claude-sign-in.d.ts +17 -6
  21. package/dist/claude-sign-in.js +42 -39
  22. package/dist/cli.js +63 -6
  23. package/dist/composio-made.d.ts +4 -0
  24. package/dist/composio-made.js +4 -0
  25. package/dist/composio-preset.d.ts +43 -2
  26. package/dist/composio-preset.js +92 -4
  27. package/dist/computer-held-routes.d.ts +5 -4
  28. package/dist/computer-held-routes.js +29 -8
  29. package/dist/computer-held.d.ts +6 -1
  30. package/dist/computer-held.js +14 -0
  31. package/dist/computer-record.js +4 -2
  32. package/dist/computer-viewer.js +4 -1
  33. package/dist/console/assets/{backend-Boh7tC4i.js → backend-DVhHzJX2.js} +19 -19
  34. package/dist/console/assets/index-Bwn1oTem.css +1 -0
  35. package/dist/console/assets/index-okP-y8PK.js +747 -0
  36. package/dist/console/index.html +2 -2
  37. package/dist/decisions.d.ts +1 -1
  38. package/dist/decisions.js +1 -1
  39. package/dist/docker-host-addresses.d.ts +22 -11
  40. package/dist/docker-host-addresses.js +38 -26
  41. package/dist/env-switch.d.ts +10 -0
  42. package/dist/env-switch.js +33 -2
  43. package/dist/errors.d.ts +1 -0
  44. package/dist/errors.js +2 -0
  45. package/dist/fal-price.d.ts +58 -20
  46. package/dist/fal-price.js +341 -82
  47. package/dist/handed-values.d.ts +43 -1
  48. package/dist/handed-values.js +119 -2
  49. package/dist/harness-archive.d.ts +13 -10
  50. package/dist/harness-archive.js +60 -34
  51. package/dist/harness-check.d.ts +3 -1
  52. package/dist/harness-check.js +25 -46
  53. package/dist/harness-keys.d.ts +73 -27
  54. package/dist/harness-keys.js +194 -61
  55. package/dist/harness-recipe-entry.d.ts +8 -7
  56. package/dist/harness-recipe-entry.js +21 -32
  57. package/dist/harness-recipe.d.ts +38 -25
  58. package/dist/harness-recipe.js +49 -36
  59. package/dist/harness-sign-in.js +12 -23
  60. package/dist/harness-table.d.ts +10 -3
  61. package/dist/harness-table.js +26 -21
  62. package/dist/harness.d.ts +1 -0
  63. package/dist/harness.js +17 -5
  64. package/dist/hosted-search.d.ts +1 -1
  65. package/dist/hosted-search.js +1 -2
  66. package/dist/import-worker.d.ts +4 -2
  67. package/dist/import-worker.js +9 -2
  68. package/dist/in-flight.d.ts +26 -0
  69. package/dist/in-flight.js +52 -0
  70. package/dist/key-refusal.d.ts +9 -4
  71. package/dist/key-refusal.js +107 -25
  72. package/dist/learn/secrets.d.ts +13 -3
  73. package/dist/learn/secrets.js +118 -17
  74. package/dist/learning-cost.d.ts +6 -3
  75. package/dist/learning-cost.js +4 -2
  76. package/dist/link-check.js +1 -1
  77. package/dist/llms.txt +1 -1
  78. package/dist/marketplace.d.ts +77 -0
  79. package/dist/marketplace.js +224 -0
  80. package/dist/mcp-api-only-origins.d.ts +30 -0
  81. package/dist/mcp-api-only-origins.js +57 -0
  82. package/dist/mcp-gateway.d.ts +7 -5
  83. package/dist/mcp-gateway.js +9 -12
  84. package/dist/mcp-secret-writes.js +5 -3
  85. package/dist/mcp-servers-store.d.ts +28 -6
  86. package/dist/mcp-servers-store.js +53 -4
  87. package/dist/media-duration.d.ts +3 -0
  88. package/dist/media-duration.js +90 -9
  89. package/dist/model-form.d.ts +5 -4
  90. package/dist/model-form.js +11 -7
  91. package/dist/model.d.ts +5 -2
  92. package/dist/model.js +9 -5
  93. package/dist/ollama-provider.d.ts +7 -1
  94. package/dist/ollama-provider.js +14 -2
  95. package/dist/openapi.js +178 -27
  96. package/dist/otel/genai.d.ts +65 -0
  97. package/dist/otel/genai.js +116 -0
  98. package/dist/otel/tracer.d.ts +55 -0
  99. package/dist/otel/tracer.js +308 -0
  100. package/dist/page.d.ts +2 -0
  101. package/dist/page.js +3 -1
  102. package/dist/paid-calls.d.ts +1 -32
  103. package/dist/paid-calls.js +4 -39
  104. package/dist/provider.d.ts +9 -0
  105. package/dist/provider.js +50 -3
  106. package/dist/redact/match.d.ts +30 -3
  107. package/dist/redact/match.js +422 -41
  108. package/dist/refusal-words.d.ts +11 -0
  109. package/dist/refusal-words.js +11 -0
  110. package/dist/run-census.d.ts +67 -0
  111. package/dist/run-census.js +240 -0
  112. package/dist/run-computer.d.ts +6 -3
  113. package/dist/run-computer.js +4 -1
  114. package/dist/run-cut.d.ts +15 -6
  115. package/dist/run-cut.js +30 -9
  116. package/dist/run-files.d.ts +3 -0
  117. package/dist/run-files.js +3 -0
  118. package/dist/run-launch.d.ts +12 -1
  119. package/dist/run-launch.js +9 -10
  120. package/dist/run-learning-served.d.ts +2 -0
  121. package/dist/run-model-key.d.ts +8 -0
  122. package/dist/run-model-key.js +31 -1
  123. package/dist/run-reads-over-store.d.ts +24 -6
  124. package/dist/run-reads-over-store.js +67 -23
  125. package/dist/run-recordings.d.ts +5 -1
  126. package/dist/run-recordings.js +39 -14
  127. package/dist/run-timeline-served.d.ts +2 -0
  128. package/dist/run-timeline-served.js +2 -2
  129. package/dist/run-usage.d.ts +1 -0
  130. package/dist/runnable.d.ts +10 -1
  131. package/dist/runnable.js +20 -8
  132. package/dist/runner/adapter-loop.d.ts +11 -0
  133. package/dist/runner/adapter-loop.js +55 -10
  134. package/dist/runner/adapter-run-spec.d.ts +45 -8
  135. package/dist/runner/adapter-run-spec.js +150 -28
  136. package/dist/runner/adapters/pi.js +25 -3
  137. package/dist/runner/beside.js +4 -3
  138. package/dist/runner/big-writes.js +1 -10
  139. package/dist/runner/claim-secrets.d.ts +18 -3
  140. package/dist/runner/claim-secrets.js +35 -6
  141. package/dist/runner/cli-run-spec.d.ts +6 -0
  142. package/dist/runner/cli-run-spec.js +82 -35
  143. package/dist/runner/computer-journal.d.ts +9 -1
  144. package/dist/runner/computer-journal.js +26 -0
  145. package/dist/runner/conformance/harness.js +1 -1
  146. package/dist/runner/feedback-round.js +3 -0
  147. package/dist/runner/harness-loops.d.ts +6 -5
  148. package/dist/runner/harness-loops.js +7 -7
  149. package/dist/runner/held.d.ts +11 -5
  150. package/dist/runner/journal-kinds.d.ts +5 -0
  151. package/dist/runner/journal-kinds.js +5 -0
  152. package/dist/runner/main.js +7 -2
  153. package/dist/runner/mcp-claim.js +30 -3
  154. package/dist/runner/mcp-run.d.ts +12 -0
  155. package/dist/runner/mcp-run.js +12 -5
  156. package/dist/runner/model-route.d.ts +17 -0
  157. package/dist/runner/model-route.js +120 -2
  158. package/dist/runner/run-child-host.d.ts +3 -0
  159. package/dist/runner/run-child-host.js +69 -28
  160. package/dist/runner/run-child.js +5 -2
  161. package/dist/runner/runner.d.ts +14 -3
  162. package/dist/runner/runner.js +129 -33
  163. package/dist/runner/store-port.d.ts +1 -1
  164. package/dist/runner/store-secrets.d.ts +6 -0
  165. package/dist/runner/store-secrets.js +49 -7
  166. package/dist/runner/subagent.js +4 -40
  167. package/dist/runner/workflow-first.d.ts +11 -4
  168. package/dist/runner/workflow-first.js +14 -8
  169. package/dist/runner/workflow-spec.js +3 -2
  170. package/dist/runs-over-store.d.ts +15 -6
  171. package/dist/runs-over-store.js +33 -21
  172. package/dist/scrub-command.d.ts +73 -0
  173. package/dist/scrub-command.js +307 -0
  174. package/dist/secret-cut.d.ts +73 -0
  175. package/dist/secret-cut.js +291 -4
  176. package/dist/secret-rules.d.ts +143 -0
  177. package/dist/secret-rules.js +2564 -0
  178. package/dist/secrets.d.ts +7 -0
  179. package/dist/secrets.js +20 -0
  180. package/dist/server-keys.d.ts +20 -0
  181. package/dist/server-keys.js +25 -6
  182. package/dist/server.d.ts +52 -11
  183. package/dist/server.js +345 -118
  184. package/dist/skills/agentrun-serve/SKILL.md +4 -0
  185. package/dist/store/index.d.ts +1 -1
  186. package/dist/store/index.js +1 -1
  187. package/dist/store/journal/storage.js +1 -1
  188. package/dist/store/postgres/migrations/0027_run_launch_census.sql +31 -0
  189. package/dist/store/schema.d.ts +6 -0
  190. package/dist/store/schema.js +11 -1
  191. package/dist/store/seal.d.ts +6 -1
  192. package/dist/store/seal.js +5 -3
  193. package/dist/store/sqlite/run-launch.d.ts +9 -1
  194. package/dist/store/sqlite/run-launch.js +24 -13
  195. package/dist/store/sqlite/run-patch.d.ts +1 -1
  196. package/dist/store/sqlite/run-patch.js +13 -4
  197. package/dist/store/sqlite/store.js +34 -4
  198. package/dist/store/store.d.ts +43 -2
  199. package/dist/store/store.js +20 -0
  200. package/dist/store-reads/learning.d.ts +6 -3
  201. package/dist/store-reads/learning.js +22 -7
  202. package/dist/store-view.d.ts +3 -0
  203. package/dist/store-view.js +3 -3
  204. package/dist/suggestions.js +3 -2
  205. package/dist/template-eval.d.ts +20 -9
  206. package/dist/template-eval.js +115 -33
  207. package/dist/template-import.d.ts +10 -0
  208. package/dist/template-import.js +16 -7
  209. package/dist/templates.d.ts +10 -2
  210. package/dist/templates.js +28 -8
  211. package/dist/timeline/labels.d.ts +6 -4
  212. package/dist/timeline/labels.js +18 -6
  213. package/dist/timeline/timeline.d.ts +5 -0
  214. package/dist/timeline/timeline.js +14 -8
  215. package/dist/timeline/util.js +1 -1
  216. package/dist/timeline/web-calls.d.ts +26 -9
  217. package/dist/timeline/web-calls.js +124 -37
  218. package/dist/tool-gate.d.ts +42 -3
  219. package/dist/tool-gate.js +66 -5
  220. package/dist/tool-keys-store.d.ts +2 -4
  221. package/dist/tool-keys-store.js +5 -21
  222. package/dist/tool-keys.d.ts +2 -2
  223. package/dist/tool-keys.js +2 -2
  224. package/dist/tool-list.js +1 -1
  225. package/dist/tool-plane.d.ts +2 -2
  226. package/dist/tool-plane.js +35 -33
  227. package/dist/tools.js +13 -7
  228. package/dist/trigger-targets.d.ts +4 -0
  229. package/dist/trigger-targets.js +9 -2
  230. package/dist/triggers.d.ts +67 -4
  231. package/dist/triggers.js +171 -22
  232. package/dist/until.d.ts +3 -0
  233. package/dist/until.js +11 -0
  234. package/dist/web-fake.d.ts +1 -3
  235. package/dist/web-fake.js +7 -18
  236. package/dist/web-fetch.d.ts +6 -72
  237. package/dist/web-fetch.js +11 -265
  238. package/dist/web-run.d.ts +31 -0
  239. package/dist/web-run.js +90 -0
  240. package/dist/web-save.d.ts +6 -4
  241. package/dist/web-save.js +9 -12
  242. package/dist/web-search.d.ts +9 -127
  243. package/dist/web-search.js +23 -259
  244. package/dist/wires.d.ts +13 -0
  245. package/dist/wires.js +11 -0
  246. package/dist/workflow-run.d.ts +5 -4
  247. package/dist/workflow-run.js +5 -4
  248. package/dist/workspace/computer-deps.d.ts +2 -1
  249. package/dist/workspace/computer-deps.js +9 -2
  250. package/dist/workspace/computer-for-claim.d.ts +12 -1
  251. package/dist/workspace/computer-for-claim.js +77 -8
  252. package/dist/workspace/computer-kept.d.ts +3 -0
  253. package/dist/workspace/computer-kept.js +11 -2
  254. package/dist/workspace/index.d.ts +1 -1
  255. package/dist/workspace/index.js +1 -1
  256. package/dist/workspace/kind-skills.d.ts +4 -0
  257. package/dist/workspace/kind-skills.js +9 -0
  258. package/dist/workspace/synced-folder.js +3 -4
  259. package/dist/wrapped.d.ts +13 -5
  260. package/dist/wrapped.js +52 -26
  261. package/package.json +17 -11
  262. package/skills/agentrun-serve/SKILL.md +4 -0
  263. package/templates/README.md +30 -8
  264. package/dist/console/assets/index-CQ3cJRzI.js +0 -746
  265. package/dist/console/assets/index-DKkaS3a0.css +0 -1
  266. package/dist/exa-keyless.d.ts +0 -34
  267. package/dist/exa-keyless.js +0 -118
  268. package/dist/firecrawl-keyless.d.ts +0 -41
  269. package/dist/firecrawl-keyless.js +0 -152
  270. package/dist/keyless-mcp.d.ts +0 -36
  271. package/dist/keyless-mcp.js +0 -127
  272. package/dist/text-urls.d.ts +0 -38
  273. package/dist/text-urls.js +0 -170
  274. package/dist/web-fetch-direct.d.ts +0 -119
  275. package/dist/web-fetch-direct.js +0 -190
@@ -0,0 +1,307 @@
1
+ // `agentrun-serve scrub RUN [--data DIR] [--json]`: what a share of one run would cut, counted, read from the local store.
2
+ // It gathers the text a share carries (share-design §1: the run view's allowlisted fields, its served Timeline rows, a
3
+ // workflow's graph, its agent's name), each string as the server serves it (the run's one cut applied, run-cut.ts), and
4
+ // runs the scrubber over each in count mode with the run's cut set as its held values (secret-cut.ts scrub). It prints
5
+ // counts by rule and by kind, and how many of the cuts the person could restore. Never a value, a piece of one, a shape
6
+ // or an offset: counts only. It reads and changes nothing (the store opened as a runner opens it; no seal key made).
7
+ //
8
+ // Exit 0 once counted, 1 when the run is withheld (its cut can't be read here: a sign-in pointer or a sealed launch that
9
+ // doesn't open, or no seal key for a store that keeps secrets; it never counts without the held values), 2 for bad arguments or a run the
10
+ // store does not hold.
11
+ import { existsSync } from "node:fs";
12
+ import { join } from "node:path";
13
+ import { parseArgs } from "node:util";
14
+ import { BACKGROUND_CONTEXT as ctx } from "@earendil-works/chord/context";
15
+ import { ApiError } from "./errors.js";
16
+ import { defaultData } from "./server-fs.js";
17
+ import { logLine } from "./print.js";
18
+ import { runCutValues } from "./run-cut.js";
19
+ import { runGraphView } from "./run-reads-over-store.js";
20
+ import { servedTimelineRows } from "./run-timeline-served.js";
21
+ import { scrub } from "./secret-cut.js";
22
+ import { hostSecrets, handedSecret, withSecrets } from "./secrets.js";
23
+ import { bootSealKey, SealKeyRefused, withSealKey } from "./seal-key.js";
24
+ import { signInReads } from "./sign-ins-store.js";
25
+ import { factsOverStore, viewOverStore } from "./store-view.js";
26
+ import { openStore, SchemaVersionUnknown, storeTarget } from "./store/index.js";
27
+ import { STORE_FILE } from "./store/sqlite/store.js";
28
+ import { SEAL_KEY_ENV } from "./tool-keys.js";
29
+ import { toolKeysOverStore } from "./tool-keys-store.js";
30
+ /** The kinds of cut, in the order they are printed (secret-rules.ts CutKind). */
31
+ export const CUT_KINDS = ["format", "held", "dump", "label", "personal", "jev"];
32
+ /** The run view's fields a share carries (share-design §1, the `run` allowlist, store-view.ts viewOfRun); every other
33
+ * field is dropped. `imported_from` carries its `source` alone. */
34
+ export const SHARE_RUN_FIELDS = ["kind", "status", "state", "title", "created_at", "ended_at", "result", "error", "review", "cost", "stopped_for", "budget_refused_calls", "effort", "approvals"];
35
+ /** Every string in a JSON value, its object keys included (a key can hold a value: a header's name a tool printed, a
36
+ * map keyed by a command line). */
37
+ export function stringsOf(value) {
38
+ const out = [];
39
+ const add = (each) => {
40
+ if (typeof each === "string")
41
+ out.push(each);
42
+ else if (Array.isArray(each))
43
+ for (const item of each)
44
+ add(item);
45
+ else if (each !== null && typeof each === "object") {
46
+ for (const [key, item] of Object.entries(each)) {
47
+ out.push(key);
48
+ add(item);
49
+ }
50
+ }
51
+ };
52
+ add(value);
53
+ return out;
54
+ }
55
+ /** A call that prints one file whole, by its tool's name: its output is that file's content (a write's or an edit's is
56
+ * not). Claude Code's and pi's `Read`, Gemini's `read_file`, ACP's `fs/read_text_file`. Its file is its input's
57
+ * `file_path` (Claude Code; Gemini CLI's current `read_file`), `absolute_path` (Gemini CLI's `read_file` in v0.1.18),
58
+ * `target_file` (Cursor, as ruled: in no published schema found) or `path` (pi; ACP; Cursor CLI's published
59
+ * `readToolCall.args.path`), ruling LXIV. */
60
+ const FILE_READ_TOOL = /^(?:read|read_?file|(?:fs\/)?read_text_file)$/i;
61
+ /** A shell `-c` launch (`["bash", "-lc", "cat ~/.pgpass"]`): its script is the command. */
62
+ const SHELL_LAUNCH = /^(?:(?:\/usr)?\/bin\/)?(?:ba|z|da)?sh$/;
63
+ const SHELL_SCRIPT_FLAG = /^-l?c$/;
64
+ /** The command a call's input runs, when it runs one: `command` or `cmd`, a string or an argv. */
65
+ function commandOf(input) {
66
+ const command = input.command ?? input.cmd;
67
+ if (typeof command === "string")
68
+ return command;
69
+ if (!Array.isArray(command) || command.length === 0 || !command.every((part) => typeof part === "string"))
70
+ return undefined;
71
+ const argv = command;
72
+ return argv.length === 3 && SHELL_LAUNCH.test(argv[0]) && SHELL_SCRIPT_FLAG.test(argv[1]) ? argv[2] : argv.join(" ");
73
+ }
74
+ /** Each call's context by its tool_use_id, from a Timeline read with its calls' arguments (calls=whole: `tool_input`). */
75
+ function callContexts(rows) {
76
+ const out = new Map();
77
+ for (const row of rows) {
78
+ const input = row.tool_input;
79
+ if (row.kind !== "tool_use" || row.tool_use_id === null || input === null || typeof input !== "object" || Array.isArray(input))
80
+ continue;
81
+ const args = input;
82
+ const file = args.file_path ?? args.absolute_path ?? args.target_file ?? args.path;
83
+ const path = row.tool_name !== null && FILE_READ_TOOL.test(row.tool_name) && typeof file === "string" ? file : undefined;
84
+ const command = commandOf(args);
85
+ if (path !== undefined || command !== undefined)
86
+ out.set(row.tool_use_id, { ...(path !== undefined ? { path } : {}), ...(command !== undefined ? { command } : {}) });
87
+ }
88
+ return out;
89
+ }
90
+ /** What a share of run `runId` would carry, as strings (object keys included: stringsOf), a tool's output with its
91
+ * call's context (ShareText), with the run's cut set read after them (the cut only grows, so the last read holds every
92
+ * value an earlier read cut); `unknown` for a run the store does not hold, `withheld` when its cut can't be read here. */
93
+ export async function shareTexts(store, runId, deps) {
94
+ const row = await store.runs.get(runId).catch(() => undefined);
95
+ if (!row)
96
+ return "unknown";
97
+ if ((await runCutValues(deps, store, runId)) === "unreadable")
98
+ return "withheld";
99
+ // The origin names the console's link, which the allowlist drops: none of it is read. A reserved .invalid name, so the
100
+ // package scan finds no service endpoint.
101
+ const viewDeps = { origin: "http://scrub.invalid", serverSecrets: deps.serverSecrets, keyEnv: deps.keyEnv, sealKey: deps.sealKey, ...(deps.signIns ? { signIns: deps.signIns } : {}) };
102
+ const view = await viewOverStore(store, runId, viewDeps);
103
+ if (!view)
104
+ return "unknown";
105
+ const texts = [];
106
+ const add = (value) => void texts.push(...stringsOf(value));
107
+ for (const field of SHARE_RUN_FIELDS)
108
+ add(view[field]);
109
+ add(view.imported_from?.source);
110
+ add((await store.agents.get(row.agentId))?.name);
111
+ // A result's output is scrubbed with its call's context (ruling LVI): its row's other strings as they are.
112
+ const calls = callContexts(await servedTimelineRows(deps, store, runId, ctx, true));
113
+ for (const row of await servedTimelineRows(deps, store, runId, ctx, false)) {
114
+ const call = row.kind === "tool_result" && row.tool_use_id !== null ? calls.get(row.tool_use_id) : undefined;
115
+ if (call === undefined || row.tool_result_preview === null) {
116
+ add(row);
117
+ continue;
118
+ }
119
+ add({ ...row, tool_result_preview: null });
120
+ texts.push({ text: row.tool_result_preview, ...call });
121
+ }
122
+ if (view.kind === "workflow") {
123
+ try {
124
+ add(await runGraphView(store, runId, {
125
+ runWorkspace: async () => null,
126
+ recordings: async () => null,
127
+ env: deps.keyEnv(),
128
+ sealKey: deps.sealKey,
129
+ runFacts: (_store, id) => factsOverStore(store, id, viewDeps),
130
+ withheld: async () => false,
131
+ cutValues: (_store, id) => runCutValues(deps, store, id),
132
+ context: ctx,
133
+ }));
134
+ }
135
+ catch (error) {
136
+ if (error instanceof ApiError && error.code === "forbidden")
137
+ return "withheld";
138
+ throw error;
139
+ }
140
+ }
141
+ const cut = await runCutValues(deps, store, runId);
142
+ if (cut === "unreadable")
143
+ return "withheld";
144
+ return { kind: String(view.kind), texts, held: cut.values };
145
+ }
146
+ /** The scrubber's count over `texts` with `held` as its held values, a ShareText with its call's context: each cut's
147
+ * rule, kind and whether it can be restored, never its value, shape or offsets. */
148
+ export async function countCuts(runId, kind, texts, held) {
149
+ const rules = new Map();
150
+ let already = 0;
151
+ let blocked = false;
152
+ for (const each of texts) {
153
+ const { text, path, command } = typeof each === "string" ? { text: each } : each;
154
+ already += text.split("[redacted]").length - 1;
155
+ const counted = await scrub(text, { mode: "count", held, ...(path !== undefined ? { path } : {}), ...(command !== undefined ? { command } : {}) });
156
+ blocked ||= counted.blocked;
157
+ for (const cut of counted.cuts) {
158
+ const line = rules.get(cut.rule) ?? { kind: cut.kind, restorable: cut.restorable, count: 0 };
159
+ line.count += 1;
160
+ rules.set(cut.rule, line);
161
+ }
162
+ }
163
+ const lines = [...rules].map(([rule, line]) => ({ rule, ...line }));
164
+ lines.sort((a, b) => kindOrder(a.kind) - kindOrder(b.kind) || a.rule.localeCompare(b.rule));
165
+ const byKind = Object.fromEntries(CUT_KINDS.map((k) => [k, 0]));
166
+ for (const line of lines)
167
+ byKind[line.kind] = (byKind[line.kind] ?? 0) + line.count;
168
+ const cuts = lines.reduce((n, line) => n + line.count, 0);
169
+ const restorable = lines.filter((line) => line.restorable).reduce((n, line) => n + line.count, 0);
170
+ return { run_id: runId, kind, strings: texts.length, rules: lines, by_kind: byKind, cuts, restorable, already_redacted: already, blocked };
171
+ }
172
+ const kindOrder = (kind) => {
173
+ const at = CUT_KINDS.indexOf(kind);
174
+ return at === -1 ? CUT_KINDS.length : at;
175
+ };
176
+ /** The count as a table: one line per rule, totals by kind, and the line that says what a share would cut. */
177
+ export function countTable(count) {
178
+ const out = [`Run ${count.run_id} (${count.kind}): ${count.strings} strings a share would carry, counted.`, ""];
179
+ const table = (head, rows, right) => {
180
+ const widths = head.map((cell, i) => Math.max(cell.length, ...rows.map((row) => row[i].length)));
181
+ const line = (row) => row.map((cell, i) => (right.includes(i) ? cell.padStart(widths[i]) : cell.padEnd(widths[i]))).join(" ").trimEnd();
182
+ out.push(line(head), ...rows.map(line), "");
183
+ };
184
+ if (count.rules.length === 0)
185
+ out.push("No rule matched.", "");
186
+ else {
187
+ table(["rule", "kind", "restorable", "count"], count.rules.map((r) => [r.rule, r.kind, r.restorable ? "yes" : "no", String(r.count)]), [3]);
188
+ const kinds = Object.entries(count.by_kind).filter(([, n]) => n > 0);
189
+ table(["kind", "count"], kinds.map(([k, n]) => [k, String(n)]), [1]);
190
+ }
191
+ if (count.already_redacted > 0)
192
+ out.push(`Already cut before this count: ${count.already_redacted} ([redacted] in the served text: known key formats and held values the server cuts where it serves the run, or keys an import took out).`);
193
+ out.push(`A share would make ${count.cuts} cut${count.cuts === 1 ? "" : "s"}; you could restore ${count.restorable} of them.`);
194
+ if (count.blocked)
195
+ out.push("But a share of this run would be refused: once cut, one of its strings still holds a held value or a known key format no rule cut in place.");
196
+ return out.join("\n");
197
+ }
198
+ /** Every secret this server never shows that a command beside the store can read (the server's own serverSecrets, less
199
+ * what only its running process holds): the host's (its environment's key-named values, the launcher's), the keys a
200
+ * person saved (sealed tool keys), and the server's own and its trigger sources' sealed secrets. */
201
+ async function heldSecretsOf(store, dir, env, sealed, say) {
202
+ if (!sealed)
203
+ return { serverSecrets: () => hostSecrets(env), stored: () => undefined };
204
+ const keys = toolKeysOverStore(store, env, say("tool_keys"));
205
+ await keys.load();
206
+ const stored = (name) => keys.value(name);
207
+ // import.ts ownSecrets: the server's API token and session secret (files beside the store), its webhook's signing
208
+ // secret and each trigger source's sealed settings.
209
+ const { ownSecrets } = await import("./import.js");
210
+ const own = await ownSecrets(store, dir, env, say("sealed_settings"));
211
+ return { serverSecrets: () => [...hostSecrets(env, stored), ...own], stored };
212
+ }
213
+ /** The line that says a run could not be counted: the error's own words, cut by the scrubber first with `held` as its
214
+ * held values (a store's error could quote what it read, a held value among it), else withheld. */
215
+ export async function couldNotCount(runId, error, held) {
216
+ const said = await scrub(error instanceof Error ? error.message : String(error), { mode: "cut", held });
217
+ return `agentrun-serve scrub: run ${runId} could not be counted: ${said.ok ? said.text : "the reason is withheld"}. Nothing was counted.`;
218
+ }
219
+ /** `agentrun-serve scrub RUN [--data DIR] [--json]`: the exit code (the file's head). Output goes to `out`, words about
220
+ * why nothing was counted to `err`. */
221
+ export async function scrubMain(argv, usage, io = { out: console.log, err: console.error }) {
222
+ let values;
223
+ let positionals;
224
+ try {
225
+ ({ values, positionals } = parseArgs({ args: [...argv], allowPositionals: true, options: { data: { type: "string" }, json: { type: "boolean" } } }));
226
+ }
227
+ catch (error) {
228
+ io.err(`${error.message}\n\n${usage}`);
229
+ return 2;
230
+ }
231
+ if (positionals.length !== 1) {
232
+ io.err(`scrub takes one run id\n\n${usage}`);
233
+ return 2;
234
+ }
235
+ const runId = positionals[0];
236
+ const dir = values.data ?? defaultData();
237
+ const unknown = () => (io.err(`agentrun-serve scrub: there is no run ${runId} in ${dir}.`), 2);
238
+ if (storeTarget(dir).kind === "sqlite" && !existsSync(join(dir, STORE_FILE)))
239
+ return unknown();
240
+ let store;
241
+ try {
242
+ // Opened as a runner opens it: no owner lock (a running server keeps serving), no schema made, nothing written.
243
+ store = await openStore({ dataDir: dir, role: "runner" });
244
+ }
245
+ catch (error) {
246
+ if (error instanceof SchemaVersionUnknown && error.found === null)
247
+ return unknown();
248
+ throw error;
249
+ }
250
+ const warnings = [];
251
+ const say = (event) => (fields) => void warnings.push(`agentrun-serve scrub: ${logLine(event, fields)}`);
252
+ let release = () => undefined;
253
+ // The held values read so far, for the error path's cut (couldNotCount).
254
+ const held = [];
255
+ try {
256
+ // The seal key as the import boots it (seal-key.ts): handed, else the data directory's; a dry boot makes none. A
257
+ // store that keeps secrets and has no key here refuses: its held values can't be read, so nothing is counted.
258
+ let env = withSecrets(process.env);
259
+ let seal;
260
+ try {
261
+ seal = await bootSealKey(store, dir, { given: handedSecret(SEAL_KEY_ENV), role: "import", write: false, say: (line) => void warnings.push(line.trimEnd()) });
262
+ }
263
+ catch (error) {
264
+ if (!(error instanceof SealKeyRefused))
265
+ throw error;
266
+ io.err(`agentrun-serve scrub: run ${runId} is withheld here: ${error.message} Without the run's held values nothing was counted.`);
267
+ return 1;
268
+ }
269
+ if (seal) {
270
+ release = seal.release;
271
+ env = withSealKey(env, seal.text);
272
+ }
273
+ const { serverSecrets, stored } = await heldSecretsOf(store, dir, env, seal !== undefined, say);
274
+ held.push(...serverSecrets());
275
+ const deps = {
276
+ serverSecrets,
277
+ ...(seal ? { signIns: signInReads(store, seal.key) } : {}),
278
+ sealKey: seal?.key,
279
+ keyEnv: () => withSecrets(env, "all", stored),
280
+ };
281
+ const gathered = await shareTexts(store, runId, deps);
282
+ for (const line of warnings.splice(0))
283
+ io.err(line);
284
+ if (gathered === "unknown")
285
+ return unknown();
286
+ if (gathered === "withheld") {
287
+ io.err(`agentrun-serve scrub: run ${runId} is withheld: this server can't read back what it was handed, a browser sign-in or the keys sealed with its launch (its seal key changed or is missing, or the record is damaged), so its held values are unknown and nothing was counted.`);
288
+ return 1;
289
+ }
290
+ held.push(...gathered.held);
291
+ const count = await countCuts(runId, gathered.kind, gathered.texts, gathered.held);
292
+ io.out(values.json ? JSON.stringify(count, null, 2) : countTable(count));
293
+ return 0;
294
+ }
295
+ catch (error) {
296
+ // A read or the scrubber failed: said in its own words, those words cut by the scrubber first (a store's error could
297
+ // quote what it read), and never a partial count.
298
+ io.err(await couldNotCount(runId, error, held));
299
+ return 1;
300
+ }
301
+ finally {
302
+ for (const line of warnings)
303
+ io.err(line);
304
+ release();
305
+ await store.close();
306
+ }
307
+ }
@@ -1,4 +1,6 @@
1
1
  import { keyShaped } from "./secret-name.js";
2
+ import { type Cut, type LintFind, type ScrubRule } from "./secret-rules.js";
3
+ export { type Cut, type CutKind, isPlaceholder, RESTORABLE, RULE_PATTERNS, SCRUB_RULES, type ScrubRule, viewsOf } from "./secret-rules.js";
2
4
  /** Whether a name is a secret's, whatever list names it or none does: one of its `_`-separated segments, in any case, is
3
5
  * KEY, TOKEN, SECRET, PASSWORD, PASSWD, PASS, PAT, CREDENTIAL, CREDENTIALS or DSN, or its last is PWD after another.
4
6
  * The one rule: this cut, the harness log's and a run's environment (secrets.ts' withoutSecrets)
@@ -25,3 +27,74 @@ export declare const secretlintCalls: {
25
27
  * itself where the rule gives it, else what it matched), and as a JSON string holding it would read it, decoded up to
26
28
  * three times, so the cut finds it in a record's parsed text and in a shell's output kept in one as JSON. */
27
29
  export declare function secretsIn(content: string): Promise<string[]>;
30
+ /** secretsIn's finds, each with the message secretlint reported it under (what anchors it: the scrubber's rules read a
31
+ * label-anchored message apart from a format's). */
32
+ export declare function lintFinds(content: string): Promise<LintFind[]>;
33
+ export type ScrubOptions = {
34
+ readonly mode: "count" | "cut";
35
+ /** Values the server holds (serverSecrets, handed refs, env key-shaped values): each cut in every form it stands in. */
36
+ readonly held?: readonly string[];
37
+ /** The path of the file `text` is the content of, when the caller knows it: a .env* file, one under ~/.ssh or a
38
+ * credentials file is dropped whole. */
39
+ readonly path?: string;
40
+ /** The shell command whose output `text` is, when the caller knows it (a tool call's, ruling LVI): a command that reads
41
+ * one file alone (`cat ~/.pgpass`) gives `path` when none is given, and an env command's output is a dump. */
42
+ readonly command?: string;
43
+ /** The homes the caller holds (rulings E, VII): a bare `/home/<user>` with no segment after it, or a Claude project
44
+ * folder's `-home-<user>-` outside `.claude/projects/`, is cut only for one of these. When not given, this process's
45
+ * user's home and $HOME. */
46
+ readonly homes?: readonly string[];
47
+ /** Cut mode: asked of each restorable cut alone (label, personal, jev); true leaves its text in place, the cut still
48
+ * reported, `kept: true`. Never asked of a format, held or dump cut: those are always cut. */
49
+ readonly keep?: (cut: Readonly<Cut>) => boolean;
50
+ };
51
+ /** Count mode's answer: the cuts, and `blocked`, whether cut mode would fail closed on the same text (its final check
52
+ * finds a held value or a format left in the cut text): a share of it would be refused. */
53
+ export type ScrubCount = {
54
+ counts: Record<string, number>;
55
+ cuts: Cut[];
56
+ blocked: boolean;
57
+ };
58
+ export type ScrubResult = {
59
+ ok: true;
60
+ text: string;
61
+ cuts: Cut[];
62
+ counts: Record<string, number>;
63
+ } | {
64
+ ok: false;
65
+ counts: Record<string, never>;
66
+ };
67
+ /** The engine's limits, for a test: the chunk length and the overlap between chunks, the time budget and its clock. */
68
+ export type ScrubLimits = {
69
+ readonly maxChars?: number;
70
+ readonly overlap?: number;
71
+ readonly budgetMs?: number;
72
+ readonly now?: () => number;
73
+ };
74
+ /** The longest piece of a text one pass reads: a longer text is read in pieces this long, each overlapping the next by
75
+ * SCRUB_OVERLAP (more than any rule's longest match), so a value across a piece's end is read whole in the next one.
76
+ * Never truncated, never skipped (ruling B). */
77
+ export declare const SCRUB_MAX_CHARS: number;
78
+ export declare const SCRUB_OVERLAP: number;
79
+ /** The most time a scrub may take, its final check included: past it, the scrub throws (cut mode fails closed, count
80
+ * mode rejects). Every rule is linear, so it is not met in good faith. */
81
+ export declare const SCRUB_BUDGET_MS = 30000;
82
+ /** `scrub` over `rules` in place of SCRUB_RULES: the engine, for a test that injects a rule (and its limits). */
83
+ export declare function scrubWith(rules: readonly ScrubRule[], text: string, opts: ScrubOptions & {
84
+ mode: "count";
85
+ }, limits?: ScrubLimits): Promise<ScrubCount>;
86
+ export declare function scrubWith(rules: readonly ScrubRule[], text: string, opts: ScrubOptions & {
87
+ mode: "cut";
88
+ }, limits?: ScrubLimits): Promise<ScrubResult>;
89
+ export declare function scrubWith(rules: readonly ScrubRule[], text: string, opts: ScrubOptions, limits?: ScrubLimits): Promise<ScrubCount | ScrubResult>;
90
+ /** Cut `text` of every rule's finds ("cut": the text, its cuts and counts; on ANY error `{ok:false}` and no text), or
91
+ * count them ("count": counts, cuts and whether a cut would be refused, never text). Async: secretlint's pass is. */
92
+ export declare function scrub(text: string, opts: ScrubOptions & {
93
+ mode: "count";
94
+ }): Promise<ScrubCount>;
95
+ export declare function scrub(text: string, opts: ScrubOptions & {
96
+ mode: "cut";
97
+ }): Promise<ScrubResult>;
98
+ export declare function scrub(text: string, opts: ScrubOptions): Promise<ScrubCount | ScrubResult>;
99
+ /** This process's home folders: its user's (the password database's) and $HOME. */
100
+ export declare function processHomes(): string[];