Public: anyone can read this; the crew can change it.

A closing forge forgets its repositories in every process-wide map keyed by repository #17594

opened this

Merged 16h 24m ago, live in production

fix/app-close-resets → main 0 commits

  • Merged. fix/app-close-resets was merged into main as ddf713b.

Review

Sign in to review this change.
22 files changed +139 −14
src/actions/detach.ts +2
@@ -9,6 +9,7 @@
99 * asking again finishes the job.
1010 */
1111
12 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
1213 import type { App } from "../app.ts";
1314 import { forkFollowMode } from "../core/fork_follow.ts";
1415 import { clearForkParent, forkParentFor, type Repo } from "../core/repos.ts";
@@ -37,6 +38,7 @@ export interface DetachJob {
3738 * forks in this forge, and empties on restart.
3839 */
3940 const jobs = new Map<string, DetachJob>();
41 REPO_KEYED.add(jobs);
4042
4143 /** The detach under way on this repository, or the last that failed. */
4244 export function detachJob(repoId: string): DetachJob | undefined {
src/actions/upstream_follow.ts +2
@@ -18,6 +18,7 @@
1818 * repository at a time, whoever asks.
1919 */
2020
21 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
2122 import type { Database } from "bun:sqlite";
2223 import type { App } from "../app.ts";
2324 import {
@@ -167,6 +168,7 @@ export function mayCarryCredential(upstreamUrl: string, webUrl: string, loopback
167168 * a time each.
168169 */
169170 const checking = new Map<string, Set<string>>();
171 REPO_KEYED.add(checking);
170172
171173 /** Take the claim on one check; `undefined` when it is already running, else the release. */
172174 export function claimCheck(repoId: string, sourceId = ""): (() => void) | undefined {
src/app.ts +8
@@ -4,6 +4,7 @@
44 * side-effect-contained, so tests boot real forges by the dozen.
55 */
66
7 import { forgetRepositories } from "./kernel/repo_keyed.ts";
78 import { reviewAfterPush } from "./actions/quarantine.ts";
89 import { onPushLarge } from "./blob/large.ts";
910 import { settleEstimates } from "./actions/estimates_settle.ts";
@@ -377,6 +378,13 @@ export function createApp(config: ForgeConfig): App {
377378 closing,
378379 close: () => {
379380 closing.abort();
381 // Its repositories leave the process-wide maps keyed by repository (`kernel/repo_keyed.ts`):
382 // another forge in this process, or the next test file, never reads them.
383 try {
384 forgetRepositories(db.query<{ id: string }, []>("SELECT id FROM repo").all().map((r) => r.id));
385 } catch {
386 // A database already closed holds nothing to read; nothing is kept for it either.
387 }
380388 // The pooled `cat-file` readers for this forge's repos are processes,
381389 // and processes outlive a closed database unless somebody says
382390 // otherwise. One data dir is one forge, so closing the forge is where
src/apps/egress.ts +2
@@ -17,6 +17,7 @@
1717 * second host the first answer did not cover.
1818 */
1919
20 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
2021 import { storedText } from "../kernel/blocked.ts";
2122 import { lookup } from "node:dns/promises";
2223 import { isIP } from "node:net";
@@ -237,6 +238,7 @@ const meters = new Map<string, { bytesIn: number; npmIn: number; holders: number
237238 * `NPM_REPOS_MAX` repositories the quietest is dropped first.
238239 */
239240 const npmByRepo = new Map<string, Moved>();
241 REPO_KEYED.add(npmByRepo);
240242 const NPM_REPOS_MAX = 256;
241243
242244 function ring(): Moved {
src/checks/queue.ts +2
@@ -63,6 +63,7 @@
6363 * the flow panel on /boards.
6464 */
6565
66 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
6667 import type { Pull } from "../core/pulls.ts";
6768 import { runGit } from "../git/env.ts";
6869 import { mergeable, resolveOid, treeOf } from "../git/merge.ts";
@@ -373,6 +374,7 @@ interface Flight {
373374 const log = createLog("queue");
374375
375376 const lines = new Map<string, Line>();
377 REPO_KEYED.add(lines);
376378
377379 /** Gates in flight beyond each line's first, every line together. */
378380 let extra = 0;
src/checks/revert_red.ts +2
@@ -10,6 +10,7 @@
1010 * shipped or closed, is never proposed twice.
1111 */
1212
13 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
1314 import type { App } from "../app.ts";
1415 import { LANE_LINE, openLaneRevert } from "./revert_lane.ts";
1516 import { addComment, getIssueById } from "../core/issues.ts";
@@ -25,6 +26,7 @@ import { sameFailure } from "./verify.ts";
2526
2627 /** The reds being answered now: a check finishing twice at once proposes once. */
2728 const working = new Set<string>();
29 REPO_KEYED.add(working);
2830
2931 /** The id of the last passed default-branch check before run `below`, with its commit. */
3032 function greenBefore(app: App, repoId: string, branch: string, below: string): string | undefined {
src/core/recombine.ts +3
@@ -6,6 +6,7 @@
66 * a push that moves the default branch (`http/server.ts`).
77 */
88
9 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
910 import type { Database } from "bun:sqlite";
1011 import { combine, readFailure, setFailure } from "../git/combined.ts";
1112 import { repoDir } from "../git/repos.ts";
@@ -23,7 +24,9 @@ export interface RecombineForge {
2324 }
2425
2526 const running = new Map<string, { again: boolean; done: Promise<number> }>();
27 REPO_KEYED.add(running);
2628 const started = new Map<string, number>();
29 REPO_KEYED.add(started);
2730
2831 /** How many runs have been started for the repository: what a test counts. */
2932 export function runsStarted(repoId: string): number {
src/crew/dispatch.ts +2
@@ -8,6 +8,7 @@
88 * a run.
99 */
1010
11 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
1112 import { provideCrewWork, registerCrew, forgetCrew } from "./registry.ts";
1213 import { agentFor, lastHumanWord, taskOf } from "./task.ts";
1314 import { CHECK_FAILED_LIMIT, INTERRUPTED_OUT } from "./give_up.ts";
@@ -1318,6 +1319,7 @@ export function decideStrandedAtBoot(app: App): void {
13181319 * pull's next push or a person's ask checks it again.
13191320 */
13201321 const waitingOnMain = new Map<string, Map<string, { env: string; commitOid: string }>>();
1322 REPO_KEYED.add(waitingOnMain);
13211323
13221324 /**
13231325 * Whether a failed check is main's red or the lane's, shared: if so, the pull is noted as waiting.
src/crew/finishing.ts +2
@@ -40,6 +40,7 @@
4040 * pulls to this pass (`approvedOnHead`).
4141 */
4242
43 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
4344 import type { Database } from "bun:sqlite";
4445 import type { App } from "../app.ts";
4546 import { addComment, assignIssue } from "../actions/issues.ts";
@@ -374,6 +375,7 @@ threadHeld.add("finishing said", said.size);
374375
375376 /** What each repository's pass last said it read, so its entry is said when that changes. */
376377 const entered = new Map<string, string>();
378 REPO_KEYED.add(entered);
377379
378380 function enter(repo: Repo, what: string): void {
379381 if (entered.get(repo.id) === what) return;
src/crew/governed.ts +2
@@ -6,6 +6,7 @@
66 * the governor holds the crew below the operator's slots.
77 */
88
9 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
910 import type { App } from "../app.ts";
1011 import { canSeeOwnerFacts, type Repo } from "../core/repos.ts";
1112 import type { User } from "../core/users.ts";
@@ -50,6 +51,7 @@ export function governorLine(g: GovernorSeen): string {
5051
5152 /** The governed number each repository was last seen at; unseen means the operator's slots. */
5253 const seen = new Map<string, number>();
54 REPO_KEYED.add(seen);
5355
5456 /**
5557 * Says a change of the governed number, once: the event is appended only
src/crew/green_sweep.ts +2
@@ -1,5 +1,6 @@
11 /** The sweep that lands green pulls that have not landed, and the merge queue's line it prunes. */
22
3 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
34 import type { App } from "../app.ts";
45 import { dropFromLine, enqueue, type Queued, waitingPulls } from "../checks/queue.ts";
56 import { getHold } from "../core/holds.ts";
@@ -67,6 +68,7 @@ export function pruneLine(app: App, repo: Repo): number {
6768
6869 /** The sweep each repository has in flight, by repository id. */
6970 const sweeping = new Map<string, Promise<number>>();
71 REPO_KEYED.add(sweeping);
7072
7173 /**
7274 * Starts the green sweep beside the tick, never in front of it: a landing
src/crew/hot_files.ts +4
@@ -16,6 +16,7 @@
1616 * the feed shows while they stand.
1717 */
1818
19 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
1920 import type { App } from "../app.ts";
2021 import { getIssuesByIds, type Issue } from "../core/issues.ts";
2122 import { joinMany, pullDir } from "../core/pulls.ts";
@@ -83,6 +84,7 @@ export interface HotFile {
8384
8485 /** Per repository, per open pull: the head and base it was read at, and the files. */
8586 const touched = new Map<string, Map<string, { at: string; number: number; files: readonly string[] }>>();
87 REPO_KEYED.add(touched);
8688
8789 const OID = /^[0-9a-f]{40,64}$/;
8890
@@ -236,6 +238,7 @@ export interface HotHold {
236238
237239 /** The pulls too wide to count as the picker last saw them, per repository, held builds or not. */
238240 const standingWideBy = new Map<string, { at: string; wide: readonly WidePull[] }>();
241 REPO_KEYED.add(standingWideBy);
239242
240243 /** The pulls too wide to count in `repoId` as the picker last saw them, if any. */
241244 export function standingWide(repoId: string): { at: string; wide: readonly WidePull[] } | undefined {
@@ -244,6 +247,7 @@ export function standingWide(repoId: string): { at: string; wide: readonly WideP
244247
245248 /** What the picker last held on hot files, per repository, and when. */
246249 const standing = new Map<string, { at: string; holds: readonly HotHold[]; wide: readonly WidePull[] }>();
250 REPO_KEYED.add(standing);
247251
248252 /** Remember the builds the picker holds on hot files in `repoId` now; none clears it. */
249253 export function holdHot(
src/crew/hot_history.ts +2
@@ -6,6 +6,7 @@
66 * and nothing is said until a sample at least `MIN_AGE_MS` old exists.
77 */
88
9 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
910 import type { Database } from "bun:sqlite";
1011 import { getRepoById } from "../core/repos.ts";
1112 import { byRise, hotTrend, hotTrendSays, TREND_NAMED, type Warming } from "../flow/hot_trend.ts";
@@ -19,6 +20,7 @@ const KEEP_MS = HOUR_MS + HOUR_MS / 2;
1920 const EVERY_MS = 5 * 60_000;
2021
2122 const samples = new Map<string, { at: number; counts: ReadonlyMap<string, number> }[]>();
23 REPO_KEYED.add(samples);
2224
2325 /** Keep `counts` for `repoId` as read at `at` (epoch ms), and drop samples too old to matter. */
2426 export function sampleHeat(repoId: string, counts: ReadonlyMap<string, number>, at: number): void {
src/crew/repeat_checks.ts +2
@@ -6,6 +6,7 @@
66 * issues a sweep, and a sweep at most every {@link EVERY_MS} per repository.
77 */
88
9 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
910 import type { App } from "../app.ts";
1011 import { createIssue } from "../core/issues.ts";
1112 import type { Repo } from "../core/repos.ts";
@@ -15,6 +16,7 @@ export const EVERY_MS = 6 * 3_600_000;
1516 export const PER_SWEEP = 2;
1617
1718 const swept = new Map<string, number>();
19 REPO_KEYED.add(swept);
1820
1921 export interface RepeatMoves {
2022 readonly proposed: number[];
src/crew/waits.ts +3
@@ -7,6 +7,8 @@
77 * re-check (#3316).
88 */
99
10 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
11
1012 /** Why a pull did not join the merge queue on this sweep, or that it did. */
1113 export type Wait =
1214 | "queued"
@@ -45,6 +47,7 @@ export function waitWords(waits: Waits): string | undefined {
4547
4648 /** The last finished sweep's tally, by repository id: the sweep runs beside the tick. */
4749 const last = new Map<string, Waits>();
50 REPO_KEYED.add(last);
4851
4952 export function noteWaits(repoId: string, waits: Waits): void {
5053 last.set(repoId, waits);
src/display/config.ts +2
@@ -23,6 +23,7 @@
2323 * than a guess.
2424 */
2525
26 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
2627 import type { App } from "../app.ts";
2728 import type { Repo } from "../core/repos.ts";
2829 import { exactRefOid } from "../git/branches.ts";
@@ -191,6 +192,7 @@ export async function readDisplay(app: App, repo: Repo): Promise<Result<DisplayC
191192 const KNOWN_TTL_MS = 15_000;
192193 const KNOWN_MAX = 2000;
193194 const known = new Map<string, { has: boolean; at: number }>();
195 REPO_KEYED.add(known);
194196
195197 function remember(repo: Repo, has: boolean): void {
196198 known.delete(repo.id);
src/flows/probe.ts +2
@@ -6,6 +6,7 @@
66 * Admin only, never an agent, one at a time per repository, a few a minute.
77 */
88
9 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
910 import { checkSource, checkUpstream } from "../actions/upstream_follow.ts";
1011 import type { App } from "../app.ts";
1112 import { getFollow } from "../core/upstream_follow.ts";
@@ -37,6 +38,7 @@ export const LANES = 4;
3738 export const DEADLINE_MS = 30_000;
3839
3940 const running = new Set<string>();
41 REPO_KEYED.add(running);
4042 const defaultLimiter = new RateLimiter({ perMinute: 4, adminPerMinute: 4, enabled: true });
4143
4244 export async function probeLinks(
src/flows/start.ts +2
@@ -4,6 +4,7 @@
44 * time, and one identity gets a few runs a minute.
55 */
66
7 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
78 import type { App } from "../app.ts";
89 import { RateLimiter } from "../core/ratelimit.ts";
910 import { canAdmin, type Repo } from "../core/repos.ts";
@@ -14,6 +15,7 @@ import { runRadar } from "./run.ts";
1415 export type Started = { ok: true; read: number } | { ok: false; why: string };
1516
1617 const running = new Set<string>();
18 REPO_KEYED.add(running);
1719 const RUNS_PER_MINUTE = 4;
1820 const defaultLimiter = new RateLimiter({ perMinute: RUNS_PER_MINUTE, adminPerMinute: RUNS_PER_MINUTE, enabled: true });
1921
src/kernel/repo_keyed.ts added +19
@@ -0,0 +1,19 @@
1 /**
2 * Module-level maps and sets keyed by a repository id, which a forge forgets when it closes. Such a
3 * map is process-wide (`test/module-state.test.ts` holds the older ones on its baseline), so it
4 * outlived the forge whose repositories it described: a second forge in the same process, an
5 * in-process restart, or the next test file under `bun test --no-isolate` read the first one's
6 * entries (a flow page named another forge's hot file, 2026-12-02). A module adds its map to this
7 * set at module level, once, as it loads, never from inside a function; `app.close()` deletes its
8 * own repositories from each.
9 */
10
11 /**
12 * The maps to forget a closing forge's repositories in; filled as modules load, never by a request.
13 */
14 export const REPO_KEYED = new Set<{ delete(repoId: string): unknown }>();
15
16 /** Forget `repoIds` in every registered map: what a closing forge does for its own repositories. */
17 export function forgetRepositories(repoIds: readonly string[]): void {
18 for (const map of REPO_KEYED) for (const id of repoIds) map.delete(id);
19 }
src/origins/mirror-pulls.ts +2
@@ -16,6 +16,7 @@
1616 * per repository, so the two never make two copies of one pull.
1717 */
1818
19 import { REPO_KEYED } from "../kernel/repo_keyed.ts";
1920 import type { App } from "../app.ts";
2021 import { openPull } from "../actions/pulls.ts";
2122 import { getMirrorTarget, type MirrorTarget } from "../core/migrate_mirror.ts";
@@ -76,6 +77,7 @@ export interface MirrorPullsReport {
7677 }
7778
7879 const running = new Map<string, Promise<MirrorPullsReport>>();
80 REPO_KEYED.add(running);
7981
8082 /**
8183 * Bring `repo`'s mirror's open pulls in. A second caller while one runs
test/flow-clog-page.test.ts +1 −14
@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";
55 import { join } from "node:path";
66 import { createRepo } from "../src/actions/repos.ts";
77 import { createApp, type App } from "../src/app.ts";
8 import { adoptTouched, learnedPulls, rememberPullFiles } from "../src/crew/hot_files.ts";
8 import { rememberPullFiles } from "../src/crew/hot_files.ts";
99 import { createUser } from "../src/core/users.ts";
1010 import { serve, type Servers } from "../src/http/server.ts";
1111 import { id } from "../src/kernel/id.ts";
@@ -44,19 +44,7 @@ const red = (num: number, names: string[]) => {
4444 .run(id(), repoId, issue.id, JSON.stringify({ names }));
4545 };
4646
47 /**
48 * The pulls each repository's open pulls touch are one map per process (`crew/hot_files.ts`), and
49 * the clog sentence names the hottest file of all of them. Under `bun test --no-isolate` a file
50 * before this one in the worker left its own there (hot-files-relieves: "8 open pulls touch
51 * src/tui/forge.ts"), and this page named that file, not src/a.ts (red on three pulls, 2026-12-02).
52 * This file starts from none, and leaves none.
53 */
54 const forgetLearned = (): void => {
55 for (const repo of new Set(learnedPulls().map((p) => p.repoId))) adoptTouched(repo, undefined);
56 };
57
5847 beforeAll(async () => {
59 forgetLearned();
6048 dataDir = await mkdtemp(join(tmpdir(), "forge-clogpage-"));
6149 app = createApp({ dataDir, port: 0 });
6250 servers = await serve(app);
@@ -73,7 +61,6 @@ beforeAll(async () => {
7361 }, 60_000);
7462
7563 afterAll(async () => {
76 forgetLearned();
7764 rememberPullFiles(repoId, []);
7865 servers.stop();
7966 app.close();
test/repo-keyed.test.ts added +71
@@ -0,0 +1,71 @@
1 /**
2 * A closing forge forgets its repositories in the process-wide maps keyed by repository
3 * (`kernel/repo_keyed.ts`), and only its own: another forge in the process keeps its entries. And a
4 * module-level map keyed by a repository id is registered, so the next one cannot leak either
5 * (a flow page named another forge's hot file under `bun test --no-isolate`, 2026-12-02).
6 *
7 * @tree-scan: reads every file under src/.
8 */
9 import { afterAll, expect, test } from "bun:test";
10 import { readFileSync } from "node:fs";
11 import { mkdtemp, rm } from "node:fs/promises";
12 import { tmpdir } from "node:os";
13 import { join } from "node:path";
14 import { Glob } from "bun";
15 import { createRepo } from "../src/actions/repos.ts";
16 import { createApp, type App } from "../src/app.ts";
17 import { learnedPulls, rememberPullFiles } from "../src/crew/hot_files.ts";
18 import { createUser } from "../src/core/users.ts";
19
20 const dirs: string[] = [];
21 afterAll(async () => {
22 for (const d of dirs) await rm(d, { recursive: true, force: true });
23 });
24
25 async function forgeWithRepo(): Promise<{ app: App; repoId: string }> {
26 const dir = await mkdtemp(join(tmpdir(), "repo-keyed-"));
27 dirs.push(dir);
28 const app = createApp({ dataDir: dir, port: 0 });
29 const u = await createUser(app.db, {
30 origin: "seed",
31 trusted: true,
32 username: "ada",
33 email: "[email protected]",
34 password: "a-good-passphrase",
35 adminIfFirst: true,
36 });
37 if (!u.ok) throw new Error(u.error);
38 const r = await createRepo(app, u.value, { name: "lib", visibility: "public" });
39 if (!r.ok) throw new Error(String(r.error));
40 return { app, repoId: r.value.id };
41 }
42
43 test("closing a forge forgets its repositories' learned pulls, and keeps another forge's", async () => {
44 const a = await forgeWithRepo();
45 const b = await forgeWithRepo();
46 rememberPullFiles(a.repoId, [{ number: 1, files: ["src/a.ts"] }]);
47 rememberPullFiles(b.repoId, [{ number: 2, files: ["src/b.ts"] }]);
48 const held = () => new Set(learnedPulls().map((p) => p.repoId));
49 expect(held().has(a.repoId)).toBe(true);
50 a.app.close();
51 expect(held().has(a.repoId)).toBe(false);
52 expect(held().has(b.repoId)).toBe(true);
53 b.app.close();
54 expect(held().has(b.repoId)).toBe(false);
55 });
56
57 test("every module-level map or set a module keys by repository id is registered to be forgotten", () => {
58 const DECL = /^(?:export )?(?:const|let) ([A-Za-z_$][\w$]*)(?:: [^=\n]+)? = new (?:Bounded)?(?:Map|Set)\b/gm;
59 const unregistered: string[] = [];
60 for (const file of new Glob("src/**/*.ts").scanSync(".")) {
61 const text = readFileSync(file, "utf8");
62 for (const m of text.matchAll(DECL)) {
63 const name = m[1]!;
64 const byRepo = new RegExp(
65 `\\b${name}\\.(set|get|delete|has|add)\\(\\s*(repo\\.id|repoId|r\\.repoId|repo_id|run\\.repoId|x\\.repoId|record\\.repoId)\\b`,
66 );
67 if (byRepo.test(text) && !text.includes(`REPO_KEYED.add(${name});`)) unregistered.push(`${file}:${name}`);
68 }
69 }
70 expect(unregistered).toEqual([]);
71 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs