Skip to content

Saves no longer fail on stray non-UTF-8 bytes - #351

Merged
varun369 merged 16 commits into
mainfrom
fix/save-non-utf8-bytes
Nov 21, 2026
Merged

varun369 merged 16 commits into
mainfrom
fix/save-non-utf8-bytes

Conversation

@varun369

Copy link
Copy Markdown
Member

Saves were dead-lettered with 'utf-8' codec can't decode byte 0x81 in position 2.

  • Stored JSON values that hold bytes which are not text are read safely everywhere (one reader, an AST ratchet guards it). Writers rebuild such a value from its source instead of starting empty, and keep the original bytes first (change ledger, version rows); saved versions link their facts so erasure still reaches them. Visibility still fails closed.
  • Re-saving the same text skips a damaged earlier memory instead of failing.
  • The embedding, recall and reranker workers reply on a private channel, so a model library printing to stdout cannot corrupt a reply; the parents read tolerantly.
  • slm doctor gains a read-only "Stored JSON values" row that counts such values per column and says what happens to each.
  • A dead-lettered save of this kind completes with slm ops resolve <id> --action retry.
  • A save checks only its own row for damage, never the whole table.

🤖 Generated with Claude Code

varun369 and others added 16 commits November 21, 2026 13:40
…hat are not UTF-8

The embedding worker answers on its stdout in JSON lines. A model library
that writes to the same file descriptor (a warning in the console's legacy
code page, a progress line) put raw bytes into that channel. The parent read
the pipe as strict UTF-8 in the locale's default encoding, so:

- on the thread reader (Windows pipes) the read raised "'utf-8' codec can't
  decode byte 0x81 in position 2: invalid start byte" out of embed(); the
  materializer stored that as the save's error on every attempt and
  dead-lettered it;
- on POSIX the same bytes were swallowed as a timeout, the worker was killed
  and the save failed on its missing embedding.

The worker now keeps the parent's pipe for replies only: it replies on a
private copy of the descriptor and points descriptor 1 and sys.stdout at
stderr, which the parent discards, before any model library loads. Its stdin
is read as UTF-8. The parent opens both pipes as UTF-8 with replacement, so a
stray byte is a malformed line handled by the existing respawn path, never an
exception. Only the vector channel is decoded tolerantly; memory content is
sent as ASCII-escaped JSON and is stored unchanged.

A save that dead-lettered with this error completes on an operator retry
(slm ops resolve <id> --action retry) and its dead-letter row clears.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ds bytes that are not text

SQLite keeps whatever a writer binds: a bytes value lands in a TEXT column
as a BLOB and comes back as bytes, and json.loads(bytes) decodes strictly.
One such row in atomic_facts (entities_json, canonical_entities_json,
source_turn_ids_json, langevin_position, shared_with), memory_scenes
(entity_ids_json), entity_profiles (fact_ids_json) or memories
(metadata_json) made every later save whose enrichment read the row fail
with "'utf-8' codec can't decode byte 0x81 in position 2: invalid start
byte", and dead-lettered it after ten attempts. This is the only path found
that gives that exact text on macOS and Linux.

storage/json_columns.load_json_column reads a BLOB holding UTF-8 JSON as
before; one that is not text reads as the column's empty value and is
reported once per column with a query to find such rows. The damaged row is
left as it is: nothing is rewritten or invented. Text values behave exactly
as before. Used by the fact, scene, entity-profile and memory readers on the
enrichment path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…d a non-text body is refused, not a crash

The recall and reranker workers had the embedding worker's flaw: a library
printing on file descriptor 1 put bytes that are not UTF-8 into the reply
pipe, and the parent decoded it strictly. On the thread reader that raised a
decode error out of the request; on POSIX the reply was lost as a timeout
and the worker was killed.

core/worker_stdio.claim_protocol_channel, now shared by all three workers,
sends replies on a private copy of the descriptor and points descriptor 1
and sys.stdout at stderr before any model library loads; stdin is UTF-8.
The recall pool and the reranker open their pipes as UTF-8 with
replacement.

outbound_http.outbound_content decoded a request body before its guard, so
a body that is not UTF-8, bound for another machine, raised a bare decode
error instead of the intended OutboundBlocked. The decode is inside the
guard now.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…perator retry

A store whose older facts hold non-UTF-8 bytes in entities_json and whose
scenes hold them in entity_ids_json: with the reader as it was, a related
save fails every automatic attempt and is dead-lettered with the codec
error. With the tolerant reader, `slm ops resolve <id> --action retry`
completes it with its text unchanged and clears the dead-letter row, and the
damaged rows are left as they were.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…le and column

A JSON text column holding bytes that are not text used to fail every save
that read its row. The readers now treat such a value as empty; doctor's new
"Stored JSON values" row (also in `slm doctor --json`) counts BLOB values in
each column those readers cover, as table.column -> count. It warns, never
fails, says the rows are read safely and nothing is changed, and prints the
read-only query that lists them. It opens the store read-only and repairs
nothing. The column list lives beside the reader (TOLERATED_COLUMNS), so the
two cannot drift apart.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…y pipe is flushed when it is stopped

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… tells readable ones from unreadable ones

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…are kept, and saving the same text again no longer fails on it

An entity profile, scene, fact entity column or langevin position that holds
bytes which are not text used to be replaced by a value built from empty, and
the bytes were lost. Writers now rebuild from the facts and keep the original
bytes in a version row or the change ledger. A source row with damaged
metadata is never merged into on a re-save, and is not a same-context
candidate.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…st a damaged stored value instead of failing

Every reader of a stored JSON column that decoded it strictly now reads it
tolerantly, so one value holding bytes that are not text no longer fails a
dashboard listing or an export. A damaged audience or member list still
fails closed where it guards visibility. A ratchet test fails if a strict
decode of such a column returns.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ontext hides sources or media

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…het follows a column value through a local name

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… facts, so erasing one removes it

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…and undo of the damaged-values run restores them

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…r the platform; doc_pages.fact_ids_json is read tolerantly and counted from the media store

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…le table

Saving a scene or an entity profile asked `damaged_ids`, which runs
`typeof(col)='blob'` over every row of the profile: no index can serve it, so
each save paid a full scan of memory_scenes or entity_profiles on a mature store.
`row_is_damaged` looks the one row up by its id. Batch passes keep `damaged_ids`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@varun369
varun369 merged commit 1d3493e into main Nov 21, 2026
7 checks passed
@varun369
varun369 deleted the fix/save-non-utf8-bytes branch November 21, 2026 10:33
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