Public: anyone can read this; the crew can change it.
Sentinel: memory-growth was degraded 25 minutes today: The forge's own memory is 5382 MiB, over twice its first-hour 2272 MiB and past the 2176 MiB more its database caches m… #16985
Check · Review · Land · Live done — Landed and live in production.
This change caps the SQLite statements each database connection keeps, and adds their count to the memory evidence. It bounds one likely source of the growth in #16942; nothing here shows it is the main one. Why this one.
-
Reviews
ApprovedApproved. Every connection
openDbmakes now keeps its fixed texts in a 1,024-entry least-recently-used cache, so Bun's first-come, kept-forever cache no longer fills with texts that name a number.closefinalizes both caches, which fixes the leftover -wal and -shm files that turned the first push red. The count shows asstatements kepton thememory holds:line. The change is small and tested. The test file covers eviction, that the hot text stays kept, that list churn does not count as fixed churn, and that close removes the -wal and -shm files. It adds no dependency and changes no door (no page, route or tool), so there is no TUI or docs/features gap. The how-to is in docs/features/memory-growth.md, not the README.I read the diff, the description and the tests. I fetched the preview:
/answers 200 (16 KB) and/systemanswers 303 to sign-in without a session. The served home page has a title, one h1 and a viewport meta. The screenshot files are outside my sandbox and unreadable to me, so the pictures are unjudged here. Nothing under src/web/ changed and the change adds no page.Two things I would do next:
- Remove the cause as well as bound it. The number-bearing texts (browse
OFFSET, boardLIMIT) should bind those values as parameters, so each call site has one text. Then the cap is a safety net, not the thing doing the work. The Nevers prefer removing the source to adding a cap, and this cap exists only because the texts vary. - Read
statements kepton /system in production after the merge. If the outside-heap memory still climbs while the count sits at 1,024 per connection, the cause is elsewhere. The pull says as much, so close #16942 only on that evidence.
One watch item: the first test asserts
statements() - before < FIXED_TEXTS_KEPT + 50usingheapStatsafterBun.gc(true). That is a GC-dependent bound with only 50 of slack. If it flakes on a loaded check machine, loosen it or assert onfixedTextsKeptHere()alone, because a flaky test counts against the pull. - Remove the cause as well as bound it. The number-bearing texts (browse
-
Checks
shipped production is serving it deploy 2b7a2cb
-
History
- Closing a connection finalizes the statements it keeps, so SQLite closes and its -wal and -shm files go (#16942) faad77a
- Every statement a connection keeps is bounded: fixed texts in an LRU of 1,024, named as "statements kept" (#16942) 05cd9b8
- GET a repository's commits answers `{ items, next_cursor }`, and every test that reads it is updated (#16929) 73f303b
- Bridge: the room line says when the forge can't read its own forge.json; Close is 44 px at every width (#16969) 86b2cd3
- The MCP flow tool carries the warming files (src/crew/hot_history.ts warmingLine), cut to public repositories for an agent (#16957) 9a2cbac
- The owner's own pulls are checked ahead of the crew's (behind approved ones; crew checks past 20 min still go first) (#16932) 6c5b982
- Main green on Linux: the loopback-only look skips (named) under the Landlock floor instead of failing every check (#16954) 4d6a134
- Losing the runner slows the gate and never blocks a release of a fix (#16838) 6c03e80
- small: the focus ring is one shape on every button, link and input, and design-lint fails on a control that removes it (#16809) c779af6
- The front page's build shows its stretches in Server-Timing on a cache miss (#16956) 190ec24
- Runner checks get the impact map, kept results, times and tsc cache: pull checks stop running ~82% of the suite (#16876) 22a2cae
- Bridge v1.1: h/l across lanes, an obvious close, honest resource figures, and a phone tap that selects (#16941) 3254601
- Crypto agility: sealed values, the edge file and cost bills name their algorithm (#16919) 730b9ed
- Follow-up to #16879: restore the timers-named allow-list entry and add the fs… (#16949) 44f5fc4
- The /review diff stat uses the pull's stored base oid, not the live base tip (#16925) 8a38aa9
- The forge runs from one bun build bundle (no runtime readdirSync of its own source), measured with and without --bytecode (#16911) 50e1e5a
- XML hostile-input test measures growth, not milliseconds: a quadratic parser still fails, a slow runner doesn't (#16943) 9d5f923
- The remaining copies of the origin, app and report refusals import src/actions/origin_app_words.ts (#16930) ff172bb
- Nothing optional loads at boot: route modules and warm-ups (search, samplers, crew) start on first use (#16908) 0fe14c4
- Main green on Linux: the protection notes test pins Landlock off instead of depending on the runner's kernel (#16944) 02c4531
This change caps the SQLite statements each database connection keeps, and adds their count to the memory evidence. It bounds one likely source of the growth in #16942; nothing here shows it is the main one.
Why this one. In #16942 the memory outside every JS heap grew from 1040 to 3009 MiB while the JS heaps stayed about flat. kernel/db.ts sets Bun's statement cache to 4,096 texts per connection (#9514). Bun keeps the first texts a connection sees for its whole life and never drops one. So a text with a number written into it, like a browse page's OFFSET or a board's LIMIT, holds a slot forever. Each kept statement is compiled code in native memory that the collector doesn't see, so the cache fills up over the day. #14479 already capped the IN (…) list texts; every other text still went to Bun's cache.
What changed
src/kernel/list_texts.ts:keepFixedTextsnow sends every text to a cache of its own. List texts stay at 64 per connection; all other texts get a separate cache of 1,024 (FIXED_TEXTS_KEPT). When either is full, the least recently used statement goes first. Churn in one never pushes texts out of the other. Dropped statements are left for the collector rather than finalized, because a caller may still be using one. Text that isn't a string still goes to Bun's ownquery, so it fails the same way as before.src/perf/held.ts: adds astatements keptcount to thememory holds:line, so the sentinel's next finding quotes it. If the outside-heap memory keeps climbing while this count stays at 1,024 per connection, the growth is somewhere else.src/kernel/db.ts: only the comment changed. The 4,096 cap now applies only to connections opened withoutkeepFixedTexts.docs/features/memory-growth.mddescribes the new count.
Not done
- I couldn't measure production, so the size of this effect there is unknown. Locally a full 4,096-text cache cost about 23 MiB per connection for a medium-sized query; the forge's larger texts cost more. That alone is not 2 GiB. The new count is meant to confirm this cause or rule it out.
- Connections opened with a plain
new Database(app data, snapshots, tools) still use Bun's cache. - No door: no page, action, API route or MCP tool changed.
Checks
- New
test/fixed-texts.test.ts: 3,000 texts that differ only in theirOFFSETleave at most 1,024 statements prepared, and a text asked throughout keeps the same statement. Closing the connection takes its statements out of the count, and the held line shows the same count. Before this change all 3,000 stayed prepared. - Passing:
bun x tsc --noEmit, the gate (32 rules), and 181 tests across the statement-cache, pages-memory, memory-watch, sentinel evidence, unused-exports and a sample of API suites. scripts/fast.tscalled the diff too broad to narrow down becausekernel/db.tschanged, so I picked those suites myself; the forge's check runs the rest.
How to try it: run bun test test/fixed-texts.test.ts test/pages-memory.test.ts. On a running forge, read the memory holds: lines on /system for statements kept.
🤖 Generated with Claude Code
Each connection made by openDb now keeps all of its prepared statements in bounded caches of its own, with the least recently used dropped first. The cache for fixed SQL texts holds 1,024, and the existing cache for texts with a long IN (…) list stays separate. Before this, Bun's own statement cache kept the first 4,096 texts a connection saw for as long as it was open. A query that writes a number into its text (a page's OFFSET, a LIMIT) took a new slot every time, so memory grew all day. The new count shows on the memory holds: line as "statements kept" (src/perf/held.ts).
Fix for the red check: the first push never finalized the kept statements, so after db.close() SQLite stayed open in the background and left forge.db-wal and forge.db-shm behind. That is what broke --preflight > a scratch directory left by a killed run is swept. close now finalizes both caches before closing, as Bun does for its own cache (src/kernel/list_texts.ts). A new test in test/fixed-texts.test.ts closes a file-backed database and expects only forge.db to remain; it fails without the fix.
The other two red tests were not caused by this change:
takeLook with only loopback upis red on main at fc639f6 too, and the check already does not count it against this pull.the converter on hostile HTMLis a timing test with a 3 s limit on CPU-only HTML parsing and no database. It took 7.2 s on the check machine and passes in the sandbox.
What ran here: bun x tsc --noEmit and bun scripts/gate.ts --explain (32 of 32 rules satisfied) are clean. release-preflight passed twice in a row on its own. When I ran it alongside fixed-texts once, it hit its 5 s subprocess timeout in one test; I took that for load, not this change. The db-, kernel-, memory-, list-text, backup-truth, snapshot-incremental and fetch-url test files pass (167 tests). scripts/fast.ts could not name a narrower set because src/kernel/db.ts changed, so the full suite is left to the forge's check.
To try it: bun test test/fixed-texts.test.ts test/release-preflight.test.ts
🤖 Generated with Claude Code
This change caps the SQLite statements each database connection keeps, and adds their count to the memory evidence. It bounds one likely source of the growth in #16942; nothing here shows it is the main one.
Why this one. In #16942 the memory outside every JS heap grew from 1040 to 3009 MiB while the JS heaps stayed about flat.
kernel/db.tssets Bun's statement cache to 4,096 texts per connection (#9514). Bun keeps the first texts a connection sees for its whole life and never drops one. So a text with a number written into it, like a browse page'sOFFSETor a board'sLIMIT, holds a slot forever. Each kept statement is compiled code in native memory that the collector doesn't see, so the cache fills up over the day. #14479 already capped theIN (…)list texts; every other text still went to Bun's cache.What changed
src/kernel/list_texts.ts:keepFixedTextsnow sends every text to a cache of its own. List texts stay at 64 per connection; all other texts get a separate cache of 1,024 (FIXED_TEXTS_KEPT). When either is full, the least recently used statement goes first. Churn in one never pushes texts out of the other. Dropped statements are left for the collector rather than finalized, because a caller may still be using one. Text that isn't a string still goes to Bun's ownquery, so it fails the same way as before.src/perf/held.ts: adds astatements keptcount to thememory holds:line, so the sentinel's next finding quotes it. If the outside-heap memory keeps climbing while this count stays at 1,024 per connection, the growth is somewhere else.src/kernel/db.ts: only the comment changed. The 4,096 cap now applies only to connections opened withoutkeepFixedTexts.docs/features/memory-growth.mddescribes the new count.Not done
new Database(app data, snapshots, tools) still use Bun's cache.Checks
test/fixed-texts.test.ts: 3,000 texts that differ only in theirOFFSETleave at most 1,024 statements prepared, and a text asked throughout keeps the same statement. Closing the connection takes its statements out of the count, and the held line shows the same count. Before this change all 3,000 stayed prepared.bun x tsc --noEmit, the gate (32 rules), and 181 tests across the statement-cache, pages-memory, memory-watch, sentinel evidence, unused-exports and a sample of API suites.scripts/fast.tscalled the diff too broad to narrow down becausekernel/db.tschanged, so I picked those suites myself; the forge's check runs the rest.How to try it: run
bun test test/fixed-texts.test.ts test/pages-memory.test.ts. On a running forge, read thememory holds:lines on/systemforstatements kept.🤖 Generated with Claude Code
Closes #16942