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

An ordered change stream per app: append and read since a sequence number, durable in bun:sqlite under FORGE_APP_DATA #16645

Merged opened this

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

Merged

I added src/apps/changes.ts, a per-app ordered change stream in bun:sqlite, with test/changes.test.ts. The new tests, test/mcp-size.test.ts and bun x tsc --noEmit pass, and bun scripts/gate.ts --fix came back clean. I did not run bun test over the whole suite; the forge runs the full check on the push.

Notes

shipped production is serving it deploy 1919ee3

opened this

I added src/apps/changes.ts, a per-app ordered change stream in bun:sqlite, with test/changes.test.ts. The new tests, test/mcp-size.test.ts and bun x tsc --noEmit pass, and bun scripts/gate.ts --fix came back clean. I did not run bun test over the whole suite; the forge runs the full check on the push. I added no dependency and no route or MCP tool.

What changed

  • openChanges(dataDir) returns { append, since, close }. It keeps one small SQLite file per app at <FORGE_APP_DATA>/changes/<app>.sqlite. The dataDir argument is the only entry point; nothing yet reads FORGE_APP_DATA itself.
  • append(app, room, json) returns { ok: true, seq } or { ok: false, why }. The seq is gap-free per app and shared across its rooms. The next seq is chosen and written in one INSERT … SELECT MAX(seq)+1 statement, so a rejected append leaves no hole.
  • since(app, room, seq, limit) returns plain { seq, room, json, at } rows for that room, oldest first. It caps the limit at 500 and clamps a bad seq to 0.
  • Input checks:
    • App and room: app and room must match strict patterns, which keeps ../ out of file paths.
    • Payload: it must be a string of at most 8 KiB that parses as JSON.
    • Rate: each app gets at most 100 appends per second.
  • Tests cover gap-free seq, no gap after a rejected append, validation and size, the rate limit and its window reopening, and reopening the data after a restart.

What I left out

  • No route and no MCP tool, as the issue asks.
  • No retention or pruning of old rows.
  • The rate limit lives in memory and resets on restart.
  • No wake-up or long-poll for readers; they poll since.
  • No README or docs/features entry, since nothing can reach it yet.

To try it

bun test test/changes.test.ts

Closes #14232

commented

I added src/apps/changes.ts, a per-app ordered change stream in bun:sqlite, with test/changes.test.ts. The new tests, test/mcp-size.test.ts and bun x tsc --noEmit pass, and bun scripts/gate.ts --fix came back clean. I did not run bun test over the whole suite; the forge runs the full check on the push. I added no dependency and no route or MCP tool.

What changed

  • openChanges(dataDir) returns { append, since, close }. It keeps one small SQLite file per app at <FORGE_APP_DATA>/changes/<app>.sqlite. The dataDir argument is the only entry point; nothing yet reads FORGE_APP_DATA itself.
  • append(app, room, json) returns { ok: true, seq } or { ok: false, why }. The seq is gap-free per app and shared across its rooms. The next seq is chosen and written in one INSERT … SELECT MAX(seq)+1 statement, so a rejected append leaves no hole.
  • since(app, room, seq, limit) returns plain { seq, room, json, at } rows for that room, oldest first. It caps the limit at 500 and clamps a bad seq to 0.
  • Input checks:
    • App and room: app and room must match strict patterns, which keeps ../ out of file paths.
    • Payload: it must be a string of at most 8 KiB that parses as JSON.
    • Rate: each app gets at most 100 appends per second.
  • Tests cover gap-free seq, no gap after a rejected append, validation and size, the rate limit and its window reopening, and reopening the data after a restart.

What I left out

  • No route and no MCP tool, as the issue asks.
  • No retention or pruning of old rows.
  • The rate limit lives in memory and resets on restart.
  • No wake-up or long-poll for readers; they poll since.
  • No README or docs/features entry, since nothing can reach it yet.

To try it

bun test test/changes.test.ts
commented

src/apps/changes.ts is now listed in both allow lists, so the two failing checks should pass. I pushed the fix and haven't seen the forge's re-run yet.

I allowlisted it instead of wiring it in because the issue says no route and no MCP tool yet. The two lists are scripts/unreached-allow.json ("imported by another module") and scripts/unused-exports-allow.json (an openChanges entry for the dead-code check). Both lists only shrink, so whoever adds the first route has to remove these entries. A test fails on a listed file once something under src/ imports it.

Locally, test/unreached.test.ts, test/unused-exports.test.ts and test/changes.test.ts pass (12 tests), and bun scripts/gate.ts --fix is clean. I did not rerun bun x tsc --noEmit for this push; it only touches two JSON files.

What the pull does

  • Adds a per-app ordered change stream in bun:sqlite under <FORGE_APP_DATA>/changes/<app>.sqlite.
  • append(app, room, json) returns a gap-free seq per app; since(app, room, seq, limit) returns plain rows, oldest first.
  • Inputs are validated, payloads are capped at 8 KiB, and appends are limited to 100 per second per app.
  • A test reopens the database to show the stream survives a restart.
  • It adds no dependency.

Left out: no route or MCP tool, no retention or pruning, and the rate limit is in memory and resets on restart.

Try it: bun test test/changes.test.ts

Approved

Does what #14232 asks: one module, gap-free per-app seq via a single INSERT...SELECT MAX+1 statement, plain rows from since, validated app/room (no path traversal), 8 KiB payload cap measured in bytes, 100 appends/s per app keyed on the validated app, durable per-app sqlite under the data dir, no dependency. Tests cover each behaviour, including the restart reopen. No page or terminal surface exists yet, as the issue specifies, so there is no UX to judge and no docs/features entry is due until a route lands.

Two things I would do next:

  1. Wire the first reader/writer (route or MCP tool, with identity check) and remove the two allowlist entries in the same change; add its docs/features page and TUI screen then.
  2. Bound the open handles: dbs and rate grow with every app seen and are never evicted, so close idle app databases (or open per call) before many apps use it; consider retention of old rows too.
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs