Skip to content

fix(host-core): keep transcript writers working after a torn append tail - #1885

Merged
vastsa merged 4 commits into
vastsa:mainfrom
SeanLiu326:fix/transcript-torn-tail-writers
Oct 26, 2026
Merged

vastsa merged 4 commits into
vastsa:mainfrom
SeanLiu326:fix/transcript-torn-tail-writers

Conversation

@SeanLiu326

Copy link
Copy Markdown
Contributor

What

Two transcript writers were the only code paths that did not tolerate a torn append tail, which the rest of transcripts.rs treats as a first-class crash state.

Root cause

append_line writes and sync_datas one line at a time, so a crash or power loss can truncate a multi-byte character at the end of the file. The file already handles that everywhere else:

  • read_line_tolerant (transcripts.rs:268) exists solely to decode such bytes lossily — its doc says it exists so a reader does not "abort whole transcript reads with InvalidData";
  • read_last_line returns Ok(None) when the tail is not valid UTF-8;
  • append_line terminates a torn tail before the next append so the two records do not fuse (transcripts.rs:780).

The exceptions:

  1. update_message (transcripts.rs:1159) read the whole file with fs::read_to_string. On a torn tail that returns InvalidData, and the fast path cannot rescue it (read_last_line returns None). Consequences, all returning INTERNAL to the renderer until some later append repairs the tail:
    • streaming-assistant finalization (sessions.rs:2179) — and in the boot recovery sweep the in-flight checkpoint has already been deleted at that point;
    • session.saveActiveRevision fails at every turn end for a regenerated branch (sessions.rs:3508);
    • session.saveRevision with revisionIndex (sessions.rs:2335) and update_delegation_snapshot (sessions.rs:2521).
  2. read_inflight (transcripts.rs:535) documents "a missing or unreadable file is None: a torn checkpoint is worth less than the transcript it would sit in", but InvalidData propagated as an error instead of the documented None.

Fix

Both read bytes and decode lossily:

  • update_message keeps its exact behavior for valid files (the String::from_utf8 success path) and only falls back to lossy decoding for a torn file — then the rewrite it was already performing replaces the target record and terminates the torn line, so the file self-heals.
  • read_inflight decodes lossily, which lands on its existing "skip invalid checkpoint" branch (warn + None).

Tests

  • update_message_survives_a_torn_tail — appends the same torn-tail bytes the existing read_skips_torn_tail_and_unknown_lines fixture uses, asserts the update lands, the record reads back with the new content, and the next append still works.
  • torn_inflight_checkpoint_reads_as_absent — a torn checkpoint reads as None instead of erroring.

Both run in CI (cargo test -p host-core); no cargo on the machine this was prepared on.

A crash mid-append can leave a torn multi-byte character at the end of a
transcript; the file treats that as a first-class state everywhere else —
read_transcript decodes torn lines lossily (read_line_tolerant), the
layout reader tolerates invalid boundaries, and append_line terminates the
torn line before writing the next record. Two writers were the exceptions:

- update_message read the file with fs::read_to_string, so every settle
  path that rewrites the transcript (streaming assistant finalization,
  saveActiveRevision, saveRevision with revisionIndex, delegation
  snapshots) answered InvalidData for an idle session until some later
  append happened to repair the tail.
- read_inflight documented 'a torn checkpoint is worth less than the
  transcript' and returns None for an unreadable file, but InvalidData
  propagated as an error instead.

Both now read bytes and decode lossily: update_message replaces the torn
line in the rewrite it was performing anyway, and read_inflight falls
through to its existing skip-and-warn path.
The branch-root contract asserted fs::read_to_string(&path) as its proxy for "update_message re-reads the file it rewrites". The torn-tail fix decodes lossily through fs::read, so the proxy no longer matches while the intent holds. Scope the assertion to the update_message body and pin the re-read itself.
The Electron E2E failed in pnpm test:e2e:provider-client-certificate: the probe hung and was SIGKILLed after 65s with dbus connection errors, the known headless-runner signature. Unrelated to this branch.
@vastsa
vastsa merged commit b9d5e45 into vastsa:main Oct 26, 2026
5 checks passed

vastsa commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Merged as b9d5e4522 after independent root-cause verification. Both new torn-tail regressions fail on main c099b6a4c with InvalidData, then pass with this change. The synthetic integration tree passed 52 transcript tests, 4 regenerate-branch contracts, cargo fmt --check, all-target Clippy (one existing layout warning), and isolated real-Host transcript-cache E2E covering restart and invalid UTF-8 reads. The tested tree matches the landed tree. Contributor commits are preserved. CI status was not used as the landing gate for this review.

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.

2 participants