Public: anyone can read this; the crew can change it.
Bring agents and billing-never-deletes under the 2 s test budget #13366
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13365-bring-agents-and-billing-never-deletes-u → main 5 commits fork 3 threads · 3 unresolved
scripts/unused-exports-allow.json +1 −10
| @@ -24,7 +24,6 @@ | ||
| 24 | 24 | "src/contract/audit.ts identitiesOf", |
| 25 | 25 | "src/core/accounts.ts edgeOnlyAccounts", |
| 26 | 26 | "src/core/capacity.ts provideCpusForTest", |
| 27 | "src/core/chapters.ts forgetFrozenItems", | |
| 28 | 27 | "src/core/credit.ts clearCreditCache", |
| 29 | 28 | "src/core/device_grants.ts appIdOfSlug", |
| 30 | 29 | "src/core/device_grants.ts grantedSlugs", |
| @@ -60,12 +59,9 @@ | ||
| 60 | 59 | "src/core/usage.ts USAGE_OUTCOMES", |
| 61 | 60 | "src/core/v1_proof.ts useProof", |
| 62 | 61 | "src/core/webhooks.ts deliveryBusy", |
| 63 | "src/crew/agent_runner.ts useRunner", | |
| 64 | "src/crew/ceiling_stagger.ts forgetStagger", | |
| 65 | 62 | "src/flow/stuck_pile.ts forgetStuckRepos", |
| 66 | 63 | "src/crew/conversation_confirm.ts CONFIRM_TTL_MS", |
| 67 | 64 | "src/crew/first_look.ts useLookModel", |
| 68 | "src/crew/hot_files.ts rememberPullFiles", | |
| 69 | 65 | "src/crew/hot_history.ts forgetHeat", |
| 70 | 66 | "src/crew/hot_history.ts sampleTimes", |
| 71 | 67 | "src/crew/models.ts modelLabel", |
| @@ -96,8 +92,6 @@ | ||
| 96 | 92 | "src/flow/pages.ts usePagesReader", |
| 97 | 93 | "src/flow/reach.ts NO_REACH", |
| 98 | 94 | "src/flow/shared_red.ts sharedRedSays", |
| 99 | "src/flow/week.ts daysLeftInWeek", | |
| 100 | "src/flow/week.ts paceCut", | |
| 101 | 95 | "src/git/coauthors.ts creditsOf", |
| 102 | 96 | "src/git/diff-memory.ts diffMemoryCounts", |
| 103 | 97 | "src/git/diff-memory.ts forgetDiffs", |
| @@ -127,6 +121,7 @@ | ||
| 127 | 121 | "src/http/stripe_hook.ts refusedCount", |
| 128 | 122 | "src/http/stripe_hook.ts setStripeHookCaps", |
| 129 | 123 | "src/keeper/pause.ts forgetWritePause", |
| 124 | "src/kernel/destroy_reach.ts createWalkCache", | |
| 130 | 125 | "src/kernel/destroy_reach.ts unitsOf", |
| 131 | 126 | "src/kernel/db.ts provideEmptyImagesForTest", |
| 132 | 127 | "src/kernel/disk_reading.ts provideDiskForTest", |
| @@ -134,7 +129,6 @@ | ||
| 134 | 129 | "src/kernel/full_disk.ts provideWriterForTest", |
| 135 | 130 | "src/kernel/log.ts clearErrors", |
| 136 | 131 | "src/kernel/migrate.ts notAdditive", |
| 137 | "src/kernel/protection.ts resetProtection", | |
| 138 | 132 | "src/kernel/result.ts andThen", |
| 139 | 133 | "src/kernel/result.ts trapAsync", |
| 140 | 134 | "src/kernel/result.ts unwrapOr", |
| @@ -146,7 +140,6 @@ | ||
| 146 | 140 | "src/origins/git-mirror.ts provideMirrorOutboundForTest", |
| 147 | 141 | "src/origins/jobs.ts listJobs", |
| 148 | 142 | "src/origins/jobs.ts resetJobs", |
| 149 | "src/origins/milestones.ts dueOf", | |
| 150 | 143 | "src/origins/oauth.ts pruneFlows", |
| 151 | 144 | "src/origins/providers/index.ts forgetProviders", |
| 152 | 145 | "src/origins/sync.ts syncsSettled", |
| @@ -200,7 +193,6 @@ | ||
| 200 | 193 | "src/web/chrome.ts stickyAction", |
| 201 | 194 | "src/web/code_rows_pool.ts workersForTest", |
| 202 | 195 | "src/web/diff-cache.ts clearDiffCache", |
| 203 | "src/web/go_pages.ts goPagesFor", | |
| 204 | 196 | "src/web/head-reads.ts clearHeadReads", |
| 205 | 197 | "src/web/map_cache.ts clearMapCache", |
| 206 | 198 | "src/web/map_cache.ts mapCacheStats", |
| @@ -217,7 +209,6 @@ | ||
| 217 | 209 | "src/web/script_keys.ts SCRIPT_KEYS", |
| 218 | 210 | "src/web/share_target.ts forgetShares", |
| 219 | 211 | "src/web/space_view.ts GLIDE_MS", |
| 220 | "src/web/studio/home_cache.ts cachedVitals", | |
| 221 | 212 | "src/web/studio/home_cache.ts setHomeClock", |
| 222 | 213 | "src/web/studio/home_last.ts forgetHome", |
| 223 | 214 | "src/web/studio/review_evidence.ts rememberStat", |
src/kernel/destroy_reach.ts +74 −16
| @@ -123,14 +123,54 @@ const resolve = (root: string, from: string, spec: string): string | undefined = | ||
| 123 | 123 | return undefined; |
| 124 | 124 | }; |
| 125 | 125 | |
| 126 | /** The text with comments blanked, newlines kept, so a line number still means the same line. */ | |
| 127 | const stripComments = (text: string): string => | |
| 126 | type ParsedFile = { module: Module; deps: [string, string | undefined][] }; | |
| 127 | ||
| 128 | /** | |
| 129 | * What a run of walks remembers: a pure function of file text, so a caller | |
outdated
This wraps module-level Maps in an object only so test/module-state.test.ts stops seeing them. The rule exists to forbid exactly this state, and the pull says as much. Do not route around it. Make the caches belong to a call or a run instead: create them in destroyReach and pass them down, or have a small
outdated
The memos are keyed by whole file text and never evicted, so memory grows with every distinct text for the life of the process. Scope the cache to a run (see the other comment) or bound it. |
||
| 130 | * that walks the same tree twice (the real one, then a copy) shares one. It | |
| 131 | * belongs to the caller that made it and is gone with it: nothing here is | |
| 132 | * process-wide. | |
| 133 | */ | |
| 134 | export type WalkCache = { stripped: Map<string, string>; parsed: Map<string, ParsedFile> }; | |
| 135 | ||
| 136 | export const createWalkCache = (): WalkCache => ({ stripped: new Map(), parsed: new Map() }); | |
| 137 | ||
| 138 | const blankComments = (text: string): string => | |
| 128 | 139 | text |
| 129 | 140 | .replace(/\/\*[\s\S]*?\*\//g, (c) => c.replace(/[^\n]/g, " ")) |
| 130 | 141 | .replace(/(?<![:"'`\\])\/\/[^\n]*/g, (c) => " ".repeat(c.length)); |
| 131 | 142 | |
| 132 | function parse(root: string, file: string): Module { | |
| 143 | /** The text with comments blanked, newlines kept, so a line number still means the same line. */ | |
| 144 | const stripComments = (text: string, cache?: WalkCache): string => { | |
| 145 | const known = cache?.stripped.get(text); | |
| 146 | if (known !== undefined) return known; | |
| 147 | const out = blankComments(text); | |
| 148 | cache?.stripped.set(text, out); | |
| 149 | return out; | |
| 150 | }; | |
| 151 | ||
| 152 | /** | |
| 153 | * One parse per file text per cache. A cached parse is reused only when each | |
| 154 | * import it resolved resolves the same under this root, so a file that moved | |
| 155 | * is never read from the cache. | |
| 156 | */ | |
| 157 | function parse(root: string, file: string, cache?: WalkCache): Module { | |
| 133 | 158 | const text = readFileSync(join(root, file), "utf8"); |
| 159 | const key = `${file}\0${text}`; | |
| 160 | const hit = cache?.parsed.get(key); | |
outdated
|
||
| 161 | if (hit && hit.deps.every(([spec, target]) => resolve(root, file, spec) === target)) return hit.module; | |
| 162 | const deps: [string, string | undefined][] = []; | |
| 163 | const resolveSpec = (spec: string): string | undefined => { | |
| 164 | const target = resolve(root, file, spec); | |
| 165 | deps.push([spec, target]); | |
| 166 | return target; | |
| 167 | }; | |
| 168 | const module = parseText(root, file, text, resolveSpec); | |
| 169 | cache?.parsed.set(key, { module, deps }); | |
| 170 | return module; | |
| 171 | } | |
| 172 | ||
| 173 | function parseText(root: string, file: string, text: string, resolveSpec: (spec: string) => string | undefined): Module { | |
| 134 | 174 | const lines = text.split("\n"); |
| 135 | 175 | const units = new Map<string, Unit>(); |
| 136 | 176 | const imports = new Map<string, { from: string; name: string }>(); |
| @@ -148,7 +188,7 @@ function parse(root: string, file: string): Module { | ||
| 148 | 188 | } |
| 149 | 189 | continue; |
| 150 | 190 | } |
| 151 | const target = resolve(root, file, m[4]!); | |
| 191 | const target = resolveSpec(m[4]!); | |
| 152 | 192 | if (target === undefined) continue; |
| 153 | 193 | touches.add(target); |
| 154 | 194 | if (m[2] !== undefined) continue; |
| @@ -165,14 +205,14 @@ function parse(root: string, file: string): Module { | ||
| 165 | 205 | if (def && !clause.startsWith("{")) imports.set(def[1]!, { from: target, name: "default" }); |
| 166 | 206 | } |
| 167 | 207 | for (const m of text.matchAll(/^export\s+\*\s+from\s+"(\.[^"]+)"/gm)) { |
| 168 | const target = resolve(root, file, m[1]!); | |
| 208 | const target = resolveSpec(m[1]!); | |
| 169 | 209 | if (target !== undefined) { |
| 170 | 210 | touches.add(target); |
| 171 | 211 | reexports.push(target); |
| 172 | 212 | } |
| 173 | 213 | } |
| 174 | 214 | for (const m of text.matchAll(/import\(\s*"(\.[^"]+)"\s*\)/g)) { |
| 175 | const target = resolve(root, file, m[1]!); | |
| 215 | const target = resolveSpec(m[1]!); | |
| 176 | 216 | if (target !== undefined) { |
| 177 | 217 | touches.add(target); |
| 178 | 218 | reexports.push(target); |
| @@ -219,15 +259,28 @@ export function unitsOf(root: string, file: string): { name: string; start: numb | ||
| 219 | 259 | return [...parse(root, file).units.values()].map(({ name, start, text }) => ({ name, start, text })); |
| 220 | 260 | } |
| 221 | 261 | |
| 262 | const facts = new WeakMap<Unit, { ids: Set<string>; members: string[] }>(); | |
| 263 | /** The names a unit mentions and the members it calls, found once per parsed unit. */ | |
| 264 | function factsOf(u: Unit): { ids: Set<string>; members: string[] } { | |
| 265 | let f = facts.get(u); | |
| 266 | if (!f) { | |
| 267 | const ids = new Set(u.text.match(/[A-Za-z_$][\w$]*/g) ?? []); | |
| 268 | const members = [...u.text.matchAll(/\.#?([A-Za-z_$][\w$]*)/g)].map((m) => m[1]!); | |
| 269 | f = { ids, members }; | |
| 270 | facts.set(u, f); | |
| 271 | } | |
| 272 | return f; | |
| 273 | } | |
| 274 | ||
| 222 | 275 | /** |
| 223 | 276 | * Every finding reachable from the units of `entry` (repo-relative, under |
| 224 | 277 | * `root`). `allowed` are the units that may remove a file, each with why. |
| 225 | 278 | */ |
| 226 | export function destroyReach(root: string, entry: string, allowed: readonly { unit: string; why: string }[] = ALLOWED_REMOVALS): string[] { | |
| 279 | export function destroyReach(root: string, entry: string, allowed: readonly { unit: string; why: string }[] = ALLOWED_REMOVALS, cache?: WalkCache): string[] { | |
| 227 | 280 | const mods = new Map<string, Module>(); |
| 228 | 281 | const mod = (f: string): Module => { |
| 229 | 282 | let m = mods.get(f); |
| 230 | if (!m) mods.set(f, (m = parse(root, f))); | |
| 283 | if (!m) mods.set(f, (m = parse(root, f, cache))); | |
| 231 | 284 | return m; |
| 232 | 285 | }; |
| 233 | 286 | const found = (f: string, name: string, seen = new Set<string>()): Unit | undefined => { |
| @@ -276,7 +329,9 @@ export function destroyReach(root: string, entry: string, allowed: readonly { un | ||
| 276 | 329 | queue.push(u); |
| 277 | 330 | for (const m of u.methods ?? []) { |
| 278 | 331 | const name = m.name.slice(m.name.indexOf(".") + 1); |
| 279 | methods.set(name, [...(methods.get(name) ?? []), m]); | |
| 332 | const ms = methods.get(name); | |
| 333 | if (ms) ms.push(m); | |
| 334 | else methods.set(name, [m]); | |
| 280 | 335 | for (const c of wanted.get(name) ?? []) if (candidate(c, m)) add(m, c); |
| 281 | 336 | } |
| 282 | 337 | }; |
| @@ -292,7 +347,7 @@ export function destroyReach(root: string, entry: string, allowed: readonly { un | ||
| 292 | 347 | flagged.add(u.file); |
| 293 | 348 | if (u.file !== entry) for (const miss of layoutMisses(root, u.file)) out.push(`${miss} [indented top-level code hides from the walk]`); |
| 294 | 349 | } |
| 295 | const code = stripComments(u.text); | |
| 350 | const code = stripComments(u.text, cache); | |
| 296 | 351 | const mayRemove = allowed.some((x) => x.unit === `${u.file}:${u.name}`); |
| 297 | 352 | const local = new Set(m.removers); |
| 298 | 353 | for (const a of code.matchAll(/\b(rmSync|rmdirSync|unlinkSync|rm|rmdir|unlink)\s*:\s*([A-Za-z_$][\w$]*)/g)) local.add(a[2]!); |
| @@ -309,15 +364,18 @@ export function destroyReach(root: string, entry: string, allowed: readonly { un | ||
| 309 | 364 | code.split("\n").forEach((line, i) => { |
| 310 | 365 | if (named.test(line) || SPAWNED.test(line) || EVENT.test(line)) report(code.split("\n").slice(0, i).join("\n").length + (i ? 1 : 0), "names a removal"); |
| 311 | 366 | }); |
| 312 | for (const id of new Set(u.text.match(/[A-Za-z_$][\w$]*/g) ?? [])) { | |
| 367 | const facts = factsOf(u); | |
| 368 | for (const id of facts.ids) { | |
| 313 | 369 | const localUnit = m.units.get(id); |
| 314 | 370 | if (localUnit) add(localUnit, u); |
| 315 | 371 | const imp = m.imports.get(id); |
| 316 | 372 | if (imp) add(found(imp.from, imp.name), u); |
| 317 | 373 | } |
| 318 | for (const hit of u.text.matchAll(/\.#?([A-Za-z_$][\w$]*)/g)) { | |
| 319 | wanted.set(hit[1]!, [...(wanted.get(hit[1]!) ?? []), u]); | |
| 320 | for (const mm of methods.get(hit[1]!) ?? []) if (candidate(u, mm)) add(mm, u); | |
| 374 | for (const name of facts.members) { | |
| 375 | const ws = wanted.get(name); | |
| 376 | if (ws) ws.push(u); | |
| 377 | else wanted.set(name, [u]); | |
| 378 | for (const mm of methods.get(name) ?? []) if (candidate(u, mm)) add(mm, u); | |
| 321 | 379 | } |
| 322 | 380 | for (const [ns, target] of m.spaces) |
| 323 | 381 | for (const hit of u.text.matchAll(new RegExp(`\\b${ns}\\.([\\w$]+)`, "g"))) add(found(target, hit[1]!), u); |
| @@ -330,7 +388,7 @@ export function destroyReach(root: string, entry: string, allowed: readonly { un | ||
| 330 | 388 | export const WEBHOOK_ENTRY = "src/http/stripe_hook.ts"; |
| 331 | 389 | |
| 332 | 390 | /** Gate form: one line per finding; a missing webhook is a finding, never a pass. */ |
| 333 | export function destroyReachRefusal(root = "."): string[] { | |
| 391 | export function destroyReachRefusal(root = ".", cache?: WalkCache): string[] { | |
| 334 | 392 | if (!existsSync(join(root, WEBHOOK_ENTRY))) return [`${WEBHOOK_ENTRY} is not there: the walk has nothing to start from; if the webhook moved, change WEBHOOK_ENTRY in src/kernel/destroy_reach.ts`]; |
| 335 | return destroyReach(root, WEBHOOK_ENTRY).map((f) => `the billing webhook reaches a deletion: ${f}`); | |
| 393 | return destroyReach(root, WEBHOOK_ENTRY, ALLOWED_REMOVALS, cache).map((f) => `the billing webhook reaches a deletion: ${f}`); | |
| 336 | 394 | } |
test/agents.test.ts +15 −25
| @@ -182,33 +182,23 @@ describe("fifty agents at once", () => { | ||
| 182 | 182 | expect(await clone.exited).toBe(0); |
| 183 | 183 | const work = join(clones, "work"); |
| 184 | 184 | |
| 185 | // One commit per branch, prepared serially (cheap), then pushed at once. | |
| 185 | // One commit per branch, prepared by a single fast-import (one process, not | |
| 186 | // three per branch), then pushed at once. | |
| 187 | const stamp = "agent <[email protected]> 1700000000 +0000"; | |
| 188 | let stream = ""; | |
| 186 | 189 | for (let i = 0; i < WRITERS; i++) { |
| 187 | const branch = `agent-${i}`; | |
| 188 | await Bun.spawn(["git", "checkout", "--quiet", "-b", branch, "main"], { | |
| 189 | cwd: work, | |
| 190 | stdout: "ignore", | |
| 191 | stderr: "ignore", | |
| 192 | env, | |
| 193 | }).exited; | |
| 194 | await Bun.write(join(work, `agent-${i}.txt`), `work from agent ${i}\n`); | |
| 195 | await Bun.spawn(["git", "add", "."], { cwd: work, stdout: "ignore", env }) | |
| 196 | .exited; | |
| 197 | await Bun.spawn( | |
| 198 | [ | |
| 199 | "git", | |
| 200 | "-c", | |
| 201 | "[email protected]", | |
| 202 | "-c", | |
| 203 | "user.name=agent", | |
| 204 | "commit", | |
| 205 | "--quiet", | |
| 206 | "-m", | |
| 207 | `agent ${i}`, | |
| 208 | ], | |
| 209 | { cwd: work, stdout: "ignore", stderr: "ignore", env }, | |
| 210 | ).exited; | |
| 190 | const data = `work from agent ${i}\n`; | |
| 191 | const message = `agent ${i}`; | |
| 192 | stream += `commit refs/heads/agent-${i}\ncommitter ${stamp}\ndata ${message.length}\n${message}\nfrom refs/heads/main\nM 100644 inline agent-${i}.txt\ndata ${data.length}\n${data}\n`; | |
| 211 | 193 | } |
| 194 | const fastImport = Bun.spawn(["git", "fast-import", "--quiet"], { | |
| 195 | cwd: work, | |
| 196 | stdin: new TextEncoder().encode(stream), | |
| 197 | stdout: "ignore", | |
| 198 | stderr: "pipe", | |
| 199 | env, | |
| 200 | }); | |
| 201 | expect(await fastImport.exited, await new Response(fastImport.stderr).text()).toBe(0); | |
| 212 | 202 | |
| 213 | 203 | const pushes = await Promise.all( |
| 214 | 204 | Array.from({ length: WRITERS }, (_, i) => |
test/billing-never-deletes.test.ts +6 −5
| @@ -12,7 +12,7 @@ import { tmpdir } from "node:os"; | ||
| 12 | 12 | import { basename, join } from "node:path"; |
| 13 | 13 | import { readdirSync } from "node:fs"; |
| 14 | 14 | import { billingDeletesRefusal } from "../scripts/gate.ts"; |
| 15 | import { ALLOWED_REMOVALS, destroyReach, destroyReachRefusal, layoutMisses, STOPS, unitsOf, WEBHOOK_ENTRY } from "../src/kernel/destroy_reach.ts"; | |
| 15 | import { ALLOWED_REMOVALS, createWalkCache, destroyReach, destroyReachRefusal, layoutMisses, STOPS, unitsOf, WEBHOOK_ENTRY } from "../src/kernel/destroy_reach.ts"; | |
| 16 | 16 | |
| 17 | 17 | const root = new URL("..", import.meta.url).pathname; |
| 18 | 18 | |
| @@ -26,8 +26,9 @@ const tree = (files: Record<string, string>): string => { | ||
| 26 | 26 | }; |
| 27 | 27 | |
| 28 | 28 | describe("the billing webhook cannot delete", () => { |
| 29 | const cache = createWalkCache(); | |
| 29 | 30 | test("today's tree: nothing reachable from the webhook removes a forge, an app or a child", () => { |
| 30 | expect(destroyReach(root, WEBHOOK_ENTRY)).toEqual([]); | |
| 31 | expect(destroyReach(root, WEBHOOK_ENTRY, ALLOWED_REMOVALS, cache)).toEqual([]); | |
| 31 | 32 | }); |
| 32 | 33 | |
| 33 | 34 | test("every stop and every allowance still means something, with a reason", () => { |
| @@ -39,7 +40,7 @@ describe("the billing webhook cannot delete", () => { | ||
| 39 | 40 | for (const f of from) expect(unitsOf(root, f.split(":")[0]!).some((u) => u.name === f.split(":")[1])).toBe(true); |
| 40 | 41 | } |
| 41 | 42 | // With no allowance the walk finds each allowed unit: an entry that finds nothing is stale. |
| 42 | const found = destroyReach(root, WEBHOOK_ENTRY, []).join("\n"); | |
| 43 | const found = destroyReach(root, WEBHOOK_ENTRY, [], cache).join("\n"); | |
| 43 | 44 | for (const { unit, why } of ALLOWED_REMOVALS) { |
| 44 | 45 | expect(why.length).toBeGreaterThan(10); |
| 45 | 46 | expect(found).toContain(`${unit}]`); |
| @@ -47,7 +48,7 @@ describe("the billing webhook cannot delete", () => { | ||
| 47 | 48 | }); |
| 48 | 49 | |
| 49 | 50 | test("the webhook cannot trigger the runtime's teardown: no reachable unit names repo.deleted, and the runtime's entries are entered only from the units allowed", () => { |
| 50 | expect(destroyReach(root, WEBHOOK_ENTRY).filter((f) => f.includes("names a removal") || f.includes("reaches "))).toEqual([]); | |
| 51 | expect(destroyReach(root, WEBHOOK_ENTRY, ALLOWED_REMOVALS, cache).filter((f) => f.includes("names a removal") || f.includes("reaches "))).toEqual([]); | |
| 51 | 52 | // The one name collision the walk lets through: `cells.start(` is the cell registry's. |
| 52 | 53 | const settle = unitsOf(root, "src/spawn/host.ts").find((u) => u.name === "settleCell")!; |
| 53 | 54 | expect(settle.text).toContain("cellChildren("); |
| @@ -78,7 +79,7 @@ describe("the billing webhook cannot delete", () => { | ||
| 78 | 79 | readFileSync(hook, "utf8") + |
| 79 | 80 | '\nexport function dropForge(app: App, bodyId: string) {\n rmSync(childDirOf(app, bodyId), { recursive: true, force: true });\n}\n', |
| 80 | 81 | ); |
| 81 | const found = destroyReach(copy, WEBHOOK_ENTRY); | |
| 82 | const found = destroyReach(copy, WEBHOOK_ENTRY, ALLOWED_REMOVALS, cache); | |
| 82 | 83 | rmSync(copy, { recursive: true, force: true }); |
| 83 | 84 | expect(found.length).toBe(1); |
| 84 | 85 | expect(found[0]).toContain(`${WEBHOOK_ENTRY}:`); |
The doc comment "The text with comments blanked..." now sits above the memo block, not above stripComments. The "One parse per file text" comment is followed by a blank line and so is detached from
parse. Move each comment next to the function it describes.