Repository navigation
fix(host-core): keep transcript writers working after a torn append tail - #1885
Merged
Merged
Conversation
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.
Owner
|
Merged as |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Two transcript writers were the only code paths that did not tolerate a torn append tail, which the rest of
transcripts.rstreats as a first-class crash state.Root cause
append_linewrites andsync_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 withInvalidData";read_last_linereturnsOk(None)when the tail is not valid UTF-8;append_lineterminates a torn tail before the next append so the two records do not fuse (transcripts.rs:780).The exceptions:
update_message(transcripts.rs:1159) read the whole file withfs::read_to_string. On a torn tail that returnsInvalidData, and the fast path cannot rescue it (read_last_linereturnsNone). Consequences, all returningINTERNALto the renderer until some later append repairs the tail:sessions.rs:2179) — and in the boot recovery sweep the in-flight checkpoint has already been deleted at that point;session.saveActiveRevisionfails at every turn end for a regenerated branch (sessions.rs:3508);session.saveRevisionwithrevisionIndex(sessions.rs:2335) andupdate_delegation_snapshot(sessions.rs:2521).read_inflight(transcripts.rs:535) documents "a missing or unreadable file isNone: a torn checkpoint is worth less than the transcript it would sit in", butInvalidDatapropagated as an error instead of the documentedNone.Fix
Both read bytes and decode lossily:
update_messagekeeps its exact behavior for valid files (theString::from_utf8success 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_inflightdecodes 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 existingread_skips_torn_tail_and_unknown_linesfixture 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 asNoneinstead of erroring.Both run in CI (
cargo test -p host-core); no cargo on the machine this was prepared on.