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

Merged opened this

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

Merged

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.

Notes

opened this

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: keepFixedTexts now 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 own query, so it fails the same way as before.
  • src/perf/held.ts: adds a statements kept count to the memory 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 without keepFixedTexts.
  • docs/features/memory-growth.md describes 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 their OFFSET leave 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.ts called the diff too broad to narrow down because kernel/db.ts changed, 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

Closes #16942

commented

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: keepFixedTexts now 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 own query, so it fails the same way as before.
  • src/perf/held.ts: adds a statements kept count to the memory 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 without keepFixedTexts.
  • docs/features/memory-growth.md describes 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 their OFFSET leave 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.ts called the diff too broad to narrow down because kernel/db.ts changed, 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

commented

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 up is red on main at fc639f6 too, and the check already does not count it against this pull.
  • the converter on hostile HTML is 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

Sign in to join this conversation.

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

Keyboard shortcuts

Docs