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

Sentinel: memory-growth was degraded 166 minutes today: The forge's own memory is 4705 MiB, over twice its first-hour 1448 MiB and past the 2176 MiB more its database caches m… #15212

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/15210-sentinel-memory-growth-was-degraded-166 → main 0 commits fork

  • Merged. mason/15210-sentinel-memory-growth-was-degraded-166 was merged into main as ca966c3.

Review

Sign in to review this change.
7 files changed +144 −5
docs/features/memory-growth.md +1 −1
@@ -39,6 +39,6 @@ Found on a copy of production's data with no network, production's own work on (
3939 - **The plan and lane caches kept answers nobody could ask for again** (`src/web/map_cache.ts`, `src/view/cache.ts`). A plan was keyed by its stamp, which moves with every run that starts or spends, and a lane by its repository's latest event; an entry past its minute was only dropped when the same key was read again or the count (64 plans, 128 lanes) pushed it out. A plan holds its repository's whole graph: the copy held ten graphs of the busiest repository (4,593 issues each) after an hour of page views, and the cache had room for 64. Now there is one plan per repository, spend visibility and day, and one lane per repository, scope and day, each replaced when its stamp moves; and every view cache drops what is past its time on the first write after `SWEEP_MS` (30 s).
4040 - **A hosted forge that slept left its request counter and route timing behind** (`src/http/server.ts`), one more of each with every wake, each keeping the closed database and a flush that retried against it. They now go when the forge closes, written out first.
4141
42 A new long-lived collection names itself in `heldOf(app)` (a forge's) or `threadHeld` (the thread's), with a bound and a test of its eviction. Named today: `turn`, `requests in flight`, `event streams`, `request counters`, `route timings`, `map plans`, `map lanes`, `map layouts`, `finishing said` (the newest 512 pulls, `src/crew/said_once.ts`), and three the crew keeps per pull or per check that nothing sweeps yet (`autonomy conflicted pulls`, `checks retried`, `app links said`): each is a short string per entry, and its line says when one is worth a sweep.
42 A new long-lived collection names itself in `heldOf(app)` (a forge's) or `threadHeld` (the thread's), with a bound and a test of its eviction. Named today: `turn`, `requests in flight`, `event streams`, `request counters`, `route timings`, `map plans`, `map lanes`, `map layouts`, `finishing said` (the newest 512 pulls, `src/crew/said_once.ts`), and three the crew keeps per pull or per check (`autonomy conflicted pulls`, `checks retried`, `app links said`), each now bounded to its newest 1024 entries (`src/perf/bounded.ts`, #15210).
4343
4444 Tests: `test/app-turn.test.ts`, `test/cache-holds.test.ts`, `test/forge-sleep.test.ts`, `test/held.test.ts`.
src/crew/autonomy.ts +4 −1
@@ -162,6 +162,7 @@ import { passLanded } from "./box_pass.ts";
162162 import { reviewersForRepos, reviewsOff, sendToReview, sweepReviews } from "./review.ts";
163163 import { lastWaits, noteWaits, tallyWaits, type Wait, waitWords } from "./waits.ts";
164164 import { bootCounts } from "../kernel/boots.ts";
165 import { BoundedMap } from "../perf/bounded.ts";
165166 import { threadHeld } from "../perf/held.ts";
166167
167168 const log = createLog("autonomy");
@@ -1639,8 +1640,10 @@ async function nextOf(
16391640 );
16401641 }
16411642
1643 /** How many conflicted pulls are remembered; past it, the one set longest ago is tried again. */
1644 export const CONFLICTED_MAX = 1024;
16421645 /** Pull id → the `head:base` tips a merge last conflicted between. */
1643 const conflicted = new Map<string, string>();
1646 export const conflicted = new BoundedMap<string, string>(CONFLICTED_MAX);
16441647 threadHeld.add("autonomy conflicted pulls", () => conflicted.size);
16451648
16461649 /** A merge refusal in words a log can carry. */
src/crew/dispatch.ts +4 −1
@@ -143,6 +143,7 @@ import { STALE_AFTER_MS } from "../runs/lease.ts";
143143 import { stoppedMark } from "../spawn/stopped.ts";
144144 import { abortAgentRun } from "./running.ts";
145145 import { during } from "../perf/jobs.ts";
146 import { BoundedSet } from "../perf/bounded.ts";
146147 import { threadHeld } from "../perf/held.ts";
147148 import { recheckUsage } from "./usage_check.ts";
148149
@@ -1206,8 +1207,10 @@ function recheckWaiting(app: App, repo: Repo, state: unknown): void {
12061207 }
12071208 }
12081209
1210 /** How many retried checks are remembered; past it, the oldest head may be retried once more. */
1211 export const RETRIED_MAX = 1024;
12091212 /** Checks already run again once for a head; a second red is real. */
1210 const retriedChecks = new Set<string>();
1213 export const retriedChecks = new BoundedSet<string>(RETRIED_MAX);
12111214 threadHeld.add("checks retried", () => retriedChecks.size);
12121215
12131216 /** What a failed check led to, once the crew had read it. */
src/perf/bounded.ts added +50
@@ -0,0 +1,50 @@
1 /**
2 * Bounded collections (#15210): a Set or Map that forgets the entry added longest ago once
3 * it passes `max`, for the crew's per-pull and per-check memory that nothing else sweeps.
4 * Forgetting is safe where it is used: at worst a line is said once more, a check is rerun
5 * once more, or a conflicted pull is tried once more.
6 */
7
8 /** `max` as a bound: a positive whole number, or a RangeError naming what was given. */
9 function boundOf(max: number): number {
10 if (!Number.isInteger(max) || max < 1) throw new RangeError(`a bound is a positive whole number, not ${max}`);
11 return max;
12 }
13
14 /** A Set holding at most `max` entries; adding past that drops the one added longest ago. */
15 export class BoundedSet<T> extends Set<T> {
16 readonly max: number;
17
18 constructor(max: number, values?: Iterable<T>) {
19 super();
20 this.max = boundOf(max);
21 for (const value of values ?? []) this.add(value);
22 }
23
24 /** Put `value` newest; past `max`, the oldest goes. */
25 override add(value: T): this {
26 super.delete(value);
27 super.add(value);
28 if (this.size > this.max) super.delete(this.values().next().value as T);
29 return this;
30 }
31 }
32
33 /** A Map holding at most `max` entries; setting past that drops the key set longest ago. */
34 export class BoundedMap<K, V> extends Map<K, V> {
35 readonly max: number;
36
37 constructor(max: number, entries?: Iterable<readonly [K, V]>) {
38 super();
39 this.max = boundOf(max);
40 for (const [key, value] of entries ?? []) this.set(key, value);
41 }
42
43 /** Put `key` newest with `value`; past `max`, the oldest key goes. */
44 override set(key: K, value: V): this {
45 super.delete(key);
46 super.set(key, value);
47 if (this.size > this.max) super.delete(this.keys().next().value as K);
48 return this;
49 }
50 }
test/bounded.test.ts added +80
@@ -0,0 +1,80 @@
1 import { describe, expect, test } from "bun:test";
2 import { BoundedMap, BoundedSet } from "../src/perf/bounded.ts";
3 import { CONFLICTED_MAX, conflicted } from "../src/crew/autonomy.ts";
4 import { RETRIED_MAX, retriedChecks } from "../src/crew/dispatch.ts";
5 import { LINKS_SAID_MAX, said } from "../src/spawn/app_links.ts";
6
7 test("a bounded set drops the entry added longest ago", () => {
8 const set = new BoundedSet<string>(2);
9 set.add("a").add("b");
10 set.add("a");
11 set.add("c");
12 expect([...set]).toEqual(["a", "c"]);
13 });
14
15 test("a bounded map drops the oldest-set key and keeps reads working", () => {
16 const map = new BoundedMap<string, number>(2);
17 map.set("a", 1);
18 map.set("b", 2);
19 map.set("a", 3);
20 map.set("c", 4);
21 expect([...map]).toEqual([["a", 3], ["c", 4]]);
22 expect(map.get("c")).toBe(4);
23 expect(map.size).toBe(2);
24 });
25
26 test("a bound of one keeps only the newest", () => {
27 const set = new BoundedSet<string>(1, ["a", "b", "c"]);
28 expect([...set]).toEqual(["c"]);
29 const map = new BoundedMap<string, number>(1, [["a", 1], ["b", 2]]);
30 expect([...map]).toEqual([["b", 2]]);
31 });
32
33 test("entries given at construction are bounded too", () => {
34 expect([...new BoundedSet<number>(2, [1, 2, 3])]).toEqual([2, 3]);
35 expect(new Set(new BoundedSet<number>(2, [1, 2, 3])).size).toBe(2);
36 });
37
38 test("a bound that is not a positive whole number is refused", () => {
39 for (const max of [0, -1, 1.5, Number.NaN, Number.POSITIVE_INFINITY]) {
40 expect(() => new BoundedSet<string>(max)).toThrow(RangeError);
41 expect(() => new BoundedMap<string, string>(max)).toThrow(RangeError);
42 }
43 });
44
45 describe("the crew's per-pull and per-check memory is bounded (#15210)", () => {
46 const over = (max: number) => Array.from({ length: max + 100 }, (_, i) => `entry-${i}`);
47
48 test("autonomy's conflicted pulls", () => {
49 const held = conflicted;
50 expect(CONFLICTED_MAX).toBe(1024);
51 try {
52 for (const key of over(CONFLICTED_MAX)) held.set(key, "head:base");
53 expect(held.size).toBe(CONFLICTED_MAX);
54 } finally {
55 held.clear();
56 }
57 });
58
59 test("dispatch's retried checks", () => {
60 const held = retriedChecks;
61 expect(RETRIED_MAX).toBe(1024);
62 try {
63 for (const key of over(RETRIED_MAX)) held.add(key);
64 expect(held.size).toBe(RETRIED_MAX);
65 } finally {
66 held.clear();
67 }
68 });
69
70 test("app links' said lines", () => {
71 const held = said;
72 expect(LINKS_SAID_MAX).toBe(1024);
73 try {
74 for (const key of over(LINKS_SAID_MAX)) held.add(key);
75 expect(held.size).toBe(LINKS_SAID_MAX);
76 } finally {
77 held.clear();
78 }
79 });
80 });
test/module-state.test.ts +1 −1
@@ -16,7 +16,7 @@ import { describe, expect, test } from "bun:test";
1616 import { readFileSync } from "node:fs";
1717 import { Glob } from "bun";
1818
19 const DECL = /^(?:export )?(?:const|let) ([A-Za-z_$][\w$]*)(?:: [^=\n]+)? = (?:new (?:Map|Set)\b|\[\])/gm;
19 const DECL = /^(?:export )?(?:const|let) ([A-Za-z_$][\w$]*)(?:: [^=\n]+)? = (?:new (?:Bounded)?(?:Map|Set)\b|\[\])/gm;
2020
2121 /** `file:name` of each module-level collection that indented code writes to. */
2222 export function sharedState(): string[] {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs