Public: anyone can read this; the crew can change it.
Sentinel: memory-growth was degraded 32 minutes today: The forge's own memory is 4479 MiB, over twice its first-hour 1909 MiB and past the 2176 MiB more its database caches m… #17251
Merged 44h 57m ago, live in production
mason/forge:mason/17205-sentinel-memory-growth-was-degraded-32-m → main 0 commits fork 1 thread · 1 unresolved
4 files
changed +61
−6
docs/features/memory-growth.md +2
| @@ -29,6 +29,8 @@ The sentinel's memory-growth issue quotes the watch's evidence: each VM's heap, | ||
| 29 | 29 | |
| 30 | 30 | **The jobs open at the trip (#17083).** Memory outside every heap that climbs for an hour and falls back (production on 2026-12-01: 1321 MiB to 4536 MiB from 12:26, back to 1442 MiB by 14:07, the main heap flat) is held by work that ended, and neither the heap tally nor the held counts see native bytes. So each minute the sampler also hands the watch the jobs begun under `during` and not yet settled (`runningOf` in `perf/jobs.ts`: the thread's and the forge's, by name, with how many are open and when the oldest began), and the trip keeps them. The finding then says `memory jobs open at the trip: begun since the growth began: snapshot since …; open before it: runner-wait ×3 since …` and `memory jobs open now: …`. "Since the growth began" counts from the last minute RSS was under 1.25x its first hour, so a job begun in the minute before the first sample over the line is named with the growth; a long poll open all day sits apart under `open before it`. A job open at the trip and gone from the `now` line, with RSS fallen back, is the one to read. Five are named; the rest are counted as `and N more`. Test: `test/sentinel-memory-evidence.test.ts`, `test/perf-jobs-per-forge.test.ts`. |
| 31 | 31 | |
| 32 | **The stream pass open across the trip (#17205).** The first finding with the jobs open at the trip named one: `snapshot-stream`, begun 20:27:01, two minutes before RSS stood at 5458 MiB with the main heap at 140 MiB just after a collection, and still open seven minutes later (a pass runs every five). Each hosted forge in a pass listed the bucket's whole `chunks/` prefix twice: once for the chunks already there, once for the check before its manifest. Each listing is a `Set` of every key the bucket holds, and the bucket gains keys all day until the nightly sweep. A pass's forges now share one view (`ChunkView` in `snapshot/snapshot.ts`): the first lists, each forge's check listing is the next forge's start, and a forge that throws leaves the view empty, so the next lists afresh. A pass of N forges lists N + 1 times, not 2N. The check before each manifest is still a fresh listing. Measured off production, one listing of 300,000 keys cost about 100 MiB of heap and was collected, so this cuts the churn but does not prove the stream is the whole of the growth; the next finding's `jobs open at the trip` says whether `snapshot-stream` is still there. Test: `test/snapshot-stream.test.ts`. | |
| 33 | ||
| 32 | 34 | A restart between the growth and the filing (a release restarts the forge several times a day) leaves a watch that never saw it: the sentence is the stored one of the process that grew, and its own evidence would say `trip none`. So each minute the need is open, the sampler writes the watch's evidence to `memory-kept.json` under the data directory (`perf/memory_kept.ts`, #16872); a finding filed by a later process that has not tripped quotes those lines, kept that day, under `memory kept by the process started …, at its last grown minute …; the forge restarted since`, and ends with one `memory now (process started …)` line for the process reading it. |
| 33 | 35 | |
| 34 | 36 | The need clears by itself once RSS falls under twice the baseline. A process that slept through its first hour has no baseline and raises none. |
src/snapshot/service.ts +5 −1
| @@ -23,6 +23,7 @@ import { | ||
| 23 | 23 | notInSnapshot, |
| 24 | 24 | pruneSnapshots, |
| 25 | 25 | takeSnapshot, |
| 26 | type ChunkView, | |
| 26 | 27 | type Manifest, |
| 27 | 28 | type Progress, |
| 28 | 29 | } from "./snapshot.ts"; |
| @@ -376,9 +377,12 @@ export function startSnapshots( | ||
| 376 | 377 | await (inFlight ?? Promise.resolve()).catch(() => {}); |
| 377 | 378 | const store = current(); |
| 378 | 379 | if (store === undefined || stopped) return undefined; |
| 380 | // One view of the bucket's chunks for the pass: each forge's check listing is the next one's | |
| 381 | // start, so a pass of N forges lists the chunks N + 1 times, not 2N (#17205). | |
| 382 | const chunks: ChunkView = {}; | |
| 379 | 383 | return streamPass(app.config.dataDir, (name) => |
| 380 | 384 | takeSnapshot( |
| 381 | { db: app.db, dataDir: app.config.dataDir, reposRoot: app.reposRoot, version: options.version, passphrase: options.passphrase, stream: name }, | |
| 385 | { db: app.db, dataDir: app.config.dataDir, reposRoot: app.reposRoot, version: options.version, passphrase: options.passphrase, stream: name, chunks }, | |
| 382 | 386 | store, |
| 383 | 387 | ), |
| 384 | 388 | ); |
src/snapshot/snapshot.ts +29 −4
| @@ -157,10 +157,14 @@ export function chunksNamed(m: Manifest): string[] { | ||
| 157 | 157 | /** A chunk's key as `list` gives it: hidden on a sealed lane, as given elsewhere. */ |
| 158 | 158 | export const storedChunk = async (store: Store, id: string): Promise<string> => (await store.storedKey?.(chunkKey(id))) ?? chunkKey(id); |
| 159 | 159 | |
| 160 | /** The chunks among `ids` the store does not list, by one fresh listing. */ | |
| 161 | export async function missingChunks(store: Store, ids: readonly string[]): Promise<string[]> { | |
| 160 | /** | |
| 161 | * The chunks among `ids` the store does not list, by one fresh listing. `listed` is handed that | |
| 162 | * listing, for a caller that would otherwise list again. | |
| 163 | */ | |
| 164 | export async function missingChunks(store: Store, ids: readonly string[], listed?: (there: Set<string>) => void): Promise<string[]> { | |
| 162 | 165 | if (ids.length === 0) return []; |
| 163 | 166 | const there = new Set(await store.list(CHUNK_PREFIX)); |
| 167 | listed?.(there); | |
| 164 | 168 | const missing: string[] = []; |
| 165 | 169 | for (const id of ids) if (!there.has(await storedChunk(store, id))) missing.push(id); |
| 166 | 170 | return missing; |
| @@ -337,6 +341,21 @@ export interface Snapshotter { | ||
| 337 | 341 | * the snapshot's, so a forge unchanged since the last pass sends nothing. |
| 338 | 342 | */ |
| 339 | 343 | readonly stream?: string | undefined; |
| 344 | /** | |
| 345 | * The store's chunks as one stream pass last listed them, shared by its forges one after | |
| 346 | * another (#17205): a forge starts from it in place of a listing of its own, and leaves it the | |
| 347 | * fresh listing its check before the manifest made. A forge that throws leaves it empty, so the | |
| 348 | * next one lists afresh. | |
| 349 | */ | |
| 350 | readonly chunks?: ChunkView | undefined; | |
| 351 | } | |
| 352 | ||
| 353 | /** | |
| 354 | * What a run of snapshots knows of the store's chunks between them: `keys` as `list` gave them, | |
| 355 | * or undefined when the next must list. | |
| 356 | */ | |
| 357 | export interface ChunkView { | |
| 358 | keys?: Set<string> | undefined; | |
| 340 | 359 | } |
| 341 | 360 | |
| 342 | 361 | /** Where the stream's manifests sit: one per hosted forge, always the newest pass. */ |
| @@ -544,7 +563,11 @@ export async function takeSnapshot( | ||
| 544 | 563 | listed(path, body.byteLength, chunkId(body), chunks); |
| 545 | 564 | }; |
| 546 | 565 | try { |
| 547 | there = new Set(await store.list(CHUNK_PREFIX)); | |
| 566 | // A pass's view is taken, not shared while this forge adds to it: it goes back only as the | |
| 567 | // fresh listing below, so a forge that throws leaves the next to list afresh. | |
| 568 | const viewed = forge.chunks?.keys; | |
| 569 | if (forge.chunks !== undefined) forge.chunks.keys = undefined; | |
| 570 | there = viewed ?? new Set(await store.list(CHUNK_PREFIX)); | |
| 548 | 571 | const heldBefore = there.size; |
| 549 | 572 | // The database, consistently, from the live connection. |
| 550 | 573 | if (!streaming) { |
| @@ -765,13 +788,15 @@ export async function takeSnapshot( | ||
| 765 | 788 | if (refused !== undefined) throw new Error(`the stream of ${refused.name} was not written: ${refused.why}`); |
| 766 | 789 | // Announced only when whole: every chunk it names, seen in the store now. |
| 767 | 790 | const named = chunksNamed(manifest); |
| 768 | const missing = await missingChunks(store, named); | |
| 791 | let fresh: Set<string> | undefined; | |
| 792 | const missing = await missingChunks(store, named, (listed) => void (fresh = listed)); | |
| 769 | 793 | if (missing.length > 0) |
| 770 | 794 | throw new Error(`${missing.length} of the ${named.length} chunks this snapshot names are not in ${store.where} (first ${missing[0]}); no manifest written`); |
| 771 | 795 | await store.put( |
| 772 | 796 | forge.stream === undefined ? `${prefix}manifest.json` : streamManifestKey(forge.stream), |
| 773 | 797 | JSON.stringify(manifest, null, 2), |
| 774 | 798 | ); |
| 799 | if (forge.chunks !== undefined) forge.chunks.keys = fresh; | |
| 775 | 800 | log.info("snapshot taken", { |
| 776 | 801 | name, |
| 777 | 802 | where: store.where, |
test/snapshot-stream.test.ts +25 −1
| @@ -16,7 +16,8 @@ import { commitLog } from "../src/git/read.ts"; | ||
| 16 | 16 | import { repoDir } from "../src/git/repos.ts"; |
| 17 | 17 | import { restoreHostedInto } from "../src/snapshot/restore.ts"; |
| 18 | 18 | import { startSnapshots } from "../src/snapshot/service.ts"; |
| 19 | import { pruneSnapshots, streamManifestKey, takeSnapshot } from "../src/snapshot/snapshot.ts"; | |
| 19 | import { CHUNK_PREFIX } from "../src/snapshot/chunks.ts"; | |
| 20 | import { pruneSnapshots, streamManifestKey, takeSnapshot, type ChunkView } from "../src/snapshot/snapshot.ts"; | |
| 20 | 21 | import { readStream, STREAM_LATE_MS, streamLag, streamPass } from "../src/snapshot/stream.ts"; |
| 21 | 22 | import type { Ctx } from "../src/http/context.ts"; |
| 22 | 23 | import { tool as systemTool } from "../src/http/mcp/tools/system.ts"; |
| @@ -129,6 +130,29 @@ describe("the stream of hosted forges", () => { | ||
| 129 | 130 | expect((await restoreHostedInto(store, "stream", "kid", dir, { passphrase: PASS })).repos).toBe(1); |
| 130 | 131 | }); |
| 131 | 132 | |
| 133 | test("forges of one pass share its view of the chunks: N + 1 listings, not 2N (#17205)", async () => { | |
| 134 | const inner = dirStore(join(root, `store-${n++}`)); | |
| 135 | let listings = 0; | |
| 136 | const store = { ...inner, list: (prefix: string) => (prefix === CHUNK_PREFIX && listings++, inner.list(prefix)) }; | |
| 137 | const chunks: ChunkView = {}; | |
| 138 | await takeSnapshot({ ...shape(), stream: "kid", chunks }, store); | |
| 139 | expect(listings).toBe(2); | |
| 140 | // The view left is the fresh listing the check made, the store as it is now. | |
| 141 | expect([...chunks.keys!].sort()).toEqual((await inner.list(CHUNK_PREFIX)).sort()); | |
| 142 | const second = await takeSnapshot({ ...shape(), stream: "kid", chunks }, store); | |
| 143 | expect(listings).toBe(3); | |
| 144 | expect(second.sent?.chunks).toBeLessThan(3); | |
| 145 | // A forge that throws leaves no view: the next lists afresh, and what it says is whole. | |
| 146 | const refuse: NonNullable<Parameters<typeof takeSnapshot>[0]["hostedCopy"]> = async () => { | |
| 147 | throw new Error("copy refused"); | |
| 148 | }; | |
| 149 | await expect(takeSnapshot({ ...shape(), stream: "kid", chunks, hostedCopy: refuse }, store)).rejects.toThrow("copy refused"); | |
| 150 | expect(chunks.keys).toBeUndefined(); | |
| 151 | await takeSnapshot({ ...shape(), stream: "kid", chunks }, store); | |
| 152 | expect(listings).toBe(5); | |
| 153 | expect((await restoreHostedInto(inner, "stream", "kid", join(root, "after-view"), { passphrase: PASS })).repos).toBe(1); | |
| 154 | }); | |
| 155 | ||
| 132 | 156 | test("the MCP system tool's stream read gives an admin each forge's lag and refuses anyone else", async () => { |
| 133 | 157 | const store = dirStore(join(root, `store-${n++}`)); |
| 134 | 158 | await pass(store, new Date()); |
new above
offline
Works, but assigning through a void callback is indirect. Next time, let missingChunks return both the missing ids and the listing.