Skip to content

store: an append after one that was cut short starts on a line of its own - #261

Open
connorburgess wants to merge 5 commits into
spacesheep-dev:mainfrom
connorburgess:fix/store-append-after-cut-line
Open

connorburgess wants to merge 5 commits into
spacesheep-dev:mainfrom
connorburgess:fix/store-append-after-cut-line

Conversation

@connorburgess

@connorburgess connorburgess commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Updated against v0.8.19 / main e8bfe999 on 2026-12-05. Current validation and refresh details.

What

An event that arrives after an append to the session's file was cut short is now kept. Before this, it was accepted and shown live, then lost at the next restart with no log line.

Why

An append cut short leaves a line with no end. A full disk, a file-size limit or a process killed mid-write can each do this. EventFiles.append (server/store/files.ts) wrote the next event straight onto that line. readLines skips a line that does not parse but still counts its bytes, so at the next boot the whole event went with the cut one.

The outbox source's offset had already moved past that event, so nothing sends it again. Example: a session with seqs 0 and 1, a cut seq 2, then turn.ended (3) and session.ended (4) after a restart. After one more restart it reads as [0, 1, 4]; seq 3 is gone.

The offsets that append recorded came from the size the store last saw (entry.size), not from the file. After an append that failed part-way, they no longer match the bytes on disk, and readAt returns the wrong event or none.

The change, in EventFiles.append:

  • It opens the file once and takes its offsets from the file's real end (fstat).
  • It reads the last byte and begins with a newline when that byte is not one, in the same write. The cut line ends there and only its own event is lost.
  • A file that is empty or ends a line gets no extra newline.

This follows core's fix for its outbox, spacesheep-dev/sessionpipe@87441b9 ("outbox: an append after one that was cut short starts on a line of its own", spacesheep-dev/sessionpipe#202).

Two more points:

  • The store's storage figure adds what each append returns, less the size it knew. Boot already counts a file that holds no event yet (a first append cut short), so it now gives that file's size to the session that appends to it, in server/store/core.ts. Without this, those bytes would be counted twice. Forgetting the session takes them all off; on main they stayed in the figure.
    That size is kept by the session's file name, the harness and id after safeName (unreadKey), because a file with no whole line has no event to give the real harness and id, only its folder and file name. So a harness or id with a character outside [A-Za-z0-9_.-], or longer than 160 characters, finds it too. safeName moves to server/store/log.ts, which has no Node in it, so the core can use it and still bundles for the browser (server/store/browser.test.ts); files.ts re-exports it.
  • EventLog.append keeps its size argument, because MemoryLog sums from it. EventFiles no longer relies on it.

If a cut stops right before the line's newline, the line is valid JSON and this change ends it. That event is then read at the next boot, where it is deduplicated by seq like any other line.

An entry is now included under Unreleased in CHANGELOG.md. Other open PRs also add release notes; those entries may need combining as the PRs land.

Original proof (before the v0.8.18 refresh)

New tests in server/store/files.test.ts:

  1. "starts on a line of its own, so only the cut event is lost": the line layout and seqs [0, 2, 3] after a cut, and readAt of the new lines.
  2. "takes its offsets from the file's end, not from a size that a failed write left behind": append is given a stale size and still returns the file's real size, and readAt returns the right event.
  3. "adds no blank line to a file that is empty or ends a line".
  4. "a store reopened over a cut line keeps every whole event it accepts, across another restart": a real Store with two restarts reads seqs [0, 1, 3, 4], and its storage figure matches the file's size.
  5. "a session whose only line was cut is counted once in the storage figure when it appends again": checks the figure after boot, after an append, and after forget.
  6. "so is one whose harness and id the file name had to change (safeName), and forget takes the figure back to 0": the same with harness my harness and an id with a colon, a space, a slash and more than 160 characters.

On the branch:

 Test Files  1 passed (1)
      Tests  6 passed (6)

Test 6 fails on 9590e9b (this PR's first commit, before unreadKey): the cut line's 40 bytes are counted twice.

 FAIL  server/store/files.test.ts > EventFiles.append after one that was cut short > so is one whose harness and id the file name had to change (safeName), and forget takes the figure back to 0
AssertionError: expected 634 to be 594 // Object.is equality

On origin/main (4ab6eae), with the same test file, 1, 2, 4 and 5 fail. Test 3 is the guard and passes on both.

     × starts on a line of its own, so only the cut event is lost 5ms
     × takes its offsets from the file's end, not from a size that a failed write left behind 2ms
     × a store reopened over a cut line keeps every whole event it accepts, across another restart 4ms
     × a session whose only line was cut is counted once in the storage figure when it appends again 1ms
      Tests  4 failed | 1 passed (5)

 FAIL  server/store/files.test.ts > EventFiles.append after one that was cut short > starts on a line of its own, so only the cut event is lost
Expected: "{"protocol":1,"id":"01M8TNKMTDDDBJ2M122F"
Received: "{"protocol":1,"id":"01M8TNKMTDDDBJ2M122F{"protocol":1,"id":"01M8TNKNSNS4PNCWXJ4AZ4R6WG","type":"turn.ended", …

Test 5 fails on main at its last step: the storage figure is still 40 after forget.

Checks on the branch head (066bc4a), macOS, Node 22: npm run typecheck and npm run lint pass (lint: warnings only, the same 135 as on main). npm test:

 Test Files  1 failed | 246 passed (247)
      Tests  1 failed | 3366 passed | 2 skipped (3369)
     × machines, sends, refusals, jobs, a restart, a revoked key, a disconnect and another account: row for row 15016ms

That one is server/control/remote.parity.test.ts, which this change does not touch. It hits its 15 s timeout the same way in a full run on unmodified origin/main (4ab6eae):

     × machines, sends, refusals, jobs, a restart, a revoked key, a disconnect and another account: row for row 15029ms
TestTimeoutError: Test timed out in 15000ms.
 Test Files  1 failed | 245 passed (246)
      Tests  1 failed | 3360 passed | 2 skipped (3363)

Run on its own, it passes on both: 11/11 on main, and together with the new file 16/16 on 9590e9b.

Checklist

  • Every commit is signed off (git commit -s).
  • npm run typecheck && npm run lint && npm test pass (see above); no UI change, so no npm run test:e2e.
  • New source files start with // SPDX-License-Identifier: Apache-2.0.
  • No secret, real address, hostname or home path in code, fixtures or screenshots.
  • No change to shared/*.
  • A line under Unreleased in CHANGELOG.md.

… own

Symptom: an append to a session's events file that was cut short (a full
disk, a file-size limit, the process killed mid-write) leaves a line with
no end. After a restart the store wrote the next event straight onto it.
The event was accepted and shown live, but the next restart dropped it
with the cut line: a whole event lost, nothing logged, and the outbox
source's offset already past it.

Cause: `EventFiles.append` (server/store/files.ts) appended at the size
the store last recorded, without looking at the file's last byte, and
`readLines` skips a line that does not parse while counting its bytes.
The recorded offsets came from that size too, so a write that failed
part-way left them off from the real end of the file and `readAt`
returned the wrong event or none.

Fix: `append` opens the file once, takes the offsets from its real end
(fstat), reads the last byte and begins with a newline when it is not
one, in the same write. The cut line ends there and only its event is
lost. The `size` argument stays on `EventLog` for MemoryLog. This is the
fix core made for its outbox in spacesheep-dev/sessionpipe@87441b9
(spacesheep-dev/sessionpipe#202).

Since the store's storage figure adds what each append returns less the
size it knew, a file boot counted that holds no event yet (a first
append cut short) now gives its new session that size, so its bytes are
not counted twice; forgetting that session takes all of them off.

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@graphide-review

This comment has been minimized.

Symptom: a session whose harness or id holds a character outside
[A-Za-z0-9_.-], or is longer than 160 characters, still had its cut
first line counted twice in storageBytes after a restart, and its
unreadSizes entry was never cleared.

Cause: boot keyed unreadSizes by the folder and file name, which went
through safeName, while entry() looked it up by the raw harness and id.

Fix: key unreadSizes by the session's file name on both sides
(unreadKey). safeName moves to log.ts, which has no Node in it, so the
store's core can use it and still bundle for the browser; files.ts
re-exports it.

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@connorburgess

Copy link
Copy Markdown
Contributor Author

Minor item from the round on 9590e9b, "unreadSizes is keyed by the name on disk, not by the session's real key": fixed in 066bc4a. Both boot and entry() now key the saved size by the session's file name, the harness and id after safeName (unreadKey in server/store/core.ts), so a harness or id with a character outside [A-Za-z0-9_.-], or longer than 160 characters, finds its size and clears it. safeName moved to server/store/log.ts, which has no Node in it, so the core still bundles for the browser; files.ts re-exports it. New test 6 in server/store/files.test.ts (harness my harness, an id with a colon, a space, a slash and over 160 characters) fails on 9590e9b with the 40 cut bytes counted twice (expected 634 to be 594) and passes on 066bc4a.

@connorburgess

Copy link
Copy Markdown
Contributor Author

@graphide-review review

@graphide-review

Copy link
Copy Markdown
Contributor

Open in GraphideGraphide review

TL;DR

  • Users won't see anything different day to day. Behind the scenes, an event that arrives after a half-finished save is now kept instead of silently disappearing at the next restart.
  • The review found no problems and rates its confidence at the maximum.
  • Safe to ship.

Merge  nothing found that needs a look first

This review covers the pushes since 9590e9b, the last review. 0 findings from earlier still stand; 1 earlier finding is fixed, as a recheck of the new code found.

EventFiles.append now opens the file once, fstat's its real size for offsets, and writes a leading newline in the same write when the last byte is not one. A line cut short by a failed write then costs only its own event, not the next one. In StoreCore, sizes of files that boot finds with no valid line are kept under safeName(harness)/safeName(id) (unreadKey), so a session whose names safeName changes still inherits that size and the bytes are not counted twice. safeName moves to the Node-free log.ts so the core can use it; files.ts re-exports it. A new test covers a harness and id that safeName rewrites.

No findings sure enough to post from reading the change.

Merge confidence 5 of 5

Where to read first
  1. server/store/files.ts:170 - append: fstat-based offsets and the leading newline after a cut line
  2. server/store/core.ts:121 - unreadKey: the safeName'd file-name key
  3. server/store/core.ts:180 - boot records cut-only files' sizes under unreadKey
  4. server/store/core.ts:280 - entry() inherits and drops the unread size
  5. server/store/core.ts:553 - ingest adds append's returned size minus entry.size to storageBytes
  6. server/store/log.ts:42 - safeName moved here (no Node), re-exported by files.ts
1 earlier finding no longer carried
  • unreadSizes is keyed by the name on disk, not by the session's real key. server/store/core.ts: fixed: At boot, unreadSizes is now keyed by unreadKey(f.harness, f.id) (server/store/core.ts:183). In entry() it is looked up and deleted by unreadKey(harness, id) for the real harness and id (core.ts:284-286). unreadKey is safeName(harness)/safeName(id) (core.ts:121), and safeName is idempotent: its outpu...
Tests and reach

Reach: no graph yet, so this review could not trace what the change reaches.

Stages: 115 s
Stage State Seconds
diff done 0.0
reach skipped 0.0
risks skipped 0.0
ci skipped 0.0
find done 72.3
agent skipped 0.0
slop skipped 0.0
scenarios skipped 0.0
verify done 35.4
prove skipped 0.0
recheck done 7.4
score done 0.0
carry done 0.0

Reviewed 066bc4a in 115 s · graph check: no graph yet, so this review ran without it · Full report · For agents: gr review findings spacesheep-dev/sessionpipe-gui 261

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@connorburgess

Copy link
Copy Markdown
Contributor Author

The truncated event-log append and storage-accounting fix still applies to SessionPipe GUI v0.8.18. Merged current main (d16a2c01) into this branch; the refreshed head is 444ea764. The affected files have not changed upstream, and the original change is preserved.

Added the previously deferred Unreleased changelog entry.

Validation on this head, macOS / Node 22.23.2:

  • npm run typecheck, npm run lint, and npm run build passed. Lint has the same 135 warnings and one informational diagnostic as current main, with no errors.
  • npx vitest run --maxWorkers=2 server/store/files.test.ts server/store/store.test.ts: 74 tests passed.

GitHub CI run 47453784782 for head 444ea764 is not green. Passed: typecheck · lint · unit · build, e2e (desktop 3/3), e2e (desktop 1/3), e2e (control channel), e2e (desktop 2/3). Failed: e2e (phone · duo), typecheck · lint · unit · build · e2e.

The phone job failed mobile.spec.ts:1050, “Discard draft is an action in the tint, 44 tall; Cancel keeps the draft”: Chromium returned a button height of 43.999969482421875 against the strict >= 44 assertion at line 1068. This PR changes event-log storage, not that UI or its test. Six local repetitions of the same test on unmodified main passed (npx playwright test test/e2e/mobile.spec.ts --project=phone --grep "Discard draft is an action" --repeat-each=6 --workers=2); the runner failure was not reproduced.

Keep the failed run visible: a maintainer needs to rerun/investigate the phone job and aggregate gate before merging. The upstream job-rerun API rejects this account with HTTP 403 for insufficient repository permissions.

Also checked the combined source/test changes from all 19 refreshed PRs on the same main: no source conflicts; typecheck, lint and build passed; all 252 unit-test files passed (3,439 tests, two skipped). This was a local integration check, separate from the individual PR run above. Release-note entries may still need combining as PRs land.

Signed-off-by: Connor Burgess <connorburgesscodes@gmail.com>
@connorburgess

Copy link
Copy Markdown
Contributor Author

Merged current main (e8bfe999, v0.8.19) into this branch; the head is now 6820b117. The merge needed one resolution, in CHANGELOG.md: this PR's Unreleased entry stays above the new 0.8.19 section. No source file of this PR changed upstream.

CI run 47497344424 for head 6820b117 passed every job: typecheck · lint · unit · build, e2e (desktop 1/3), e2e (desktop 2/3), e2e (desktop 3/3), e2e (phone · duo), e2e (control channel) and the aggregate typecheck · lint · unit · build · e2e gate.

The earlier run 47453784782 on 444ea764 failed e2e (phone · duo) at mobile.spec.ts:1050 ("Discard draft is an action in the tint, 44 tall"): the button measured 43.999969482421875 px against >= 44. The same test failed with the same measured value on main itself, in run 47396304100 (586bae0b) and run 47470694290 (f7becfc1), so it is a sub-pixel rounding flake in the test, not this change. It passes in the new run.

Locally on 6820b117 (macOS, Node 22.23.2): npm run typecheck, npm run lint (135 warnings, no errors, as on main) and npm run build passed; npx vitest run server/store/files.test.ts server/store/store.test.ts: 74 tests passed.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant