Skip to content

The TypeScript member merge copies documents without decoding them, so a string that is not UTF-8 reaches the converter - #1962

Merged
zlareb1 merged 12 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/ts-merge-record-level
Dec 2, 2026
Merged

zlareb1 merged 12 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/ts-merge-record-level

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Related: #1959
Related: #1782
Related: #1704

This covers the merge case of the linked issue. A pass over such a document completes only together with #1704 (open), which would make the converter skip the document; without it the pass still exits 1, one step later, in the converter.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

The merge of a TypeScript pass over member projects no longer fails when one document holds a string that is not valid UTF-8: the merge copies the documents without decoding them. With the converter change in #1704, such a document is skipped and its file keeps its rows, marked stale.

Why

scip_merge_indexes.py parsed each member's index as one scip.Index, and python-protobuf refuses the whole message when any string in it is not valid UTF-8. scip-typescript writes such strings, so on the repository from #1678 every pass that indexes its src project exited 1 in the merge, after every member had been indexed. #1704 (open) would make the converter skip an undecodable document, but that cannot help while the merge parses first.

The merge needs only the top-level records and each document's relative_path for the ownership rule. It now reads those with the converter's _top_level_records and _document_path, applies the same rule (_keeps, the body moved out of the loop), and writes the kept records out byte for byte. The parts are memory-mapped through the converter's _read_index, so a part is never held as parsed messages.

Scope and risk

scripts/scip_harness/scip_merge_indexes.py and its tests. The output is written to <output>.tmp and moved over --output with os.replace, because the parts stay memory-mapped while it is written; --output may name one of the inputs. The CLI, the ownership rule, the order of documents, which copy of a path wins, the metadata from the first non-empty part and the concatenated external symbols are as before; the existing merge and harness tests pass with only their protobuf loader changed. A document the converter cannot decode now reaches the converter: with #1704 it is skipped and its file keeps its rows, marked stale; without it the converter fails with DecodeError ... 'scip.Document', as it does on a non-member index today. _ensure_scip_pb2 leaves the merge, which no longer imports protobuf; the tests load the vendored bindings themselves.

The file-name case in the linked issue is not part of this change.

Compatibility, rollout and rollback

  • Wire: none.
  • Operator steps: none.
  • Rollback: reverting brings back the whole-index parse; nothing is stored.

Tests

  • tests/unit/test_scip_merge_indexes.py::test_merge_copies_a_document_whose_strings_are_not_utf8: one part holds an owned document, an owned document with a string that is not UTF-8 and a document another member owns. The merge keeps the first two, with the invalid bytes as they were, and drops the third. On main it fails with DecodeError: ... String field had bad UTF-8 at scip_merge_indexes.py:95.
  • test_merge_may_write_over_one_of_its_inputs: --output names one of the inputs. It passes on main too, and guards the mapped-input case for the rewrite (writing in place truncates a mapped part).
  • test_merge_keeps_every_parts_external_symbols: the external symbols of two parts are concatenated in order. It passes on main too and pins that behaviour for the rewrite.
  • Run here at head 831373cdf, cut from main at 3058a5816: the merge, test_run_typescript_delta_umbrella.py, test_run_typescript_delta_projects.py and test_scip_ts_projects.py tests, 51 passed; test_tunable_constants.py and test_public_copy.py pass. ruff check, ruff format --check, the function-length gate and mypy on the merge module pass. scripts/gates/mypy_gate.py reports errors in server/llm/vertex_auth.py on this host (missing google.auth stubs in the environment), a file this change does not touch. The full unit suite was not run here; CI runs it.
  • The same approach, measured on the repository from The TypeScript tier cannot index a large monorepo through its root tsconfig, and every delta repeats the full cost #1678 with scip-typescript 0.4.0: the merge took 15 s and 1.5 GB for 222 parts (the project list expanded at the measured commit; The TypeScript tier cannot index a large monorepo through its root tsconfig, and every delta repeats the full cost #1678's 223 was counted at an earlier one) and 5 s and 307 MB for a three-project delta, where the whole-index parse failed.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code wrote the diff and the tests. Checked: I reviewed the whole diff and its tests; the runs listed under Tests are the agent's.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py). None are affected.
  • A new migration is additive and numbered after the latest on main; no merged migration is edited. There is no migration.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version: no plugin file changed.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed: it did not.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes: no page describes the merge's parsing.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

Note

Medium Risk
Core TypeScript index merge path changes parsing and I/O behavior; correctness is heavily tested but any mismatch in streaming protobuf logic could affect indexing passes.

Overview
Replaces whole-index protobuf parsing in scip_merge_indexes.py with a byte-level stream merge: it walks top-level SCIP records via shared helpers from scip_to_delta, keys documents on raw path bytes (UTF-8 decode only for ownership), and re-serializes kept payloads unchanged—so indexes containing non-UTF-8 strings no longer fail in the merge step. Output is written through a temp file + os.replace so inputs can be memory-mapped and --output can name an input file; merge_indexes now returns the set of kept document paths as text.

scip_to_delta._top_level_records is tightened to reject or skip wire data the same way python-protobuf does (varint/tag/length limits, groups, corrupt tails). _document_path_bytes and metadata merging in _project_root align streaming reads with parser semantics. Tests add broad coverage for UTF-8 documents, corruption, protobuf parity, and converter CLI behavior.

Reviewed by Cursor Bugbot for commit cdc76fe. Bugbot is set up for automated code reviews on this repo. Configure here.

scip_merge_indexes.py parsed each member's index as one scip.Index, and
python-protobuf refuses a whole message when any string in it is not
UTF-8. scip-typescript writes such strings, so one document failed the
merge, and the pass, after every member had been indexed.

The merge now reads the top-level records and each document's
relative_path with the converter's reader, applies the same ownership
rule, and writes the kept records out byte for byte. Nothing is decoded,
the parts are memory-mapped, and the converter decides what to do with a
document it cannot read.

The output is written beside its target and swapped in with os.replace,
so --output may name one of the inputs while those are still mapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@vercel

vercel Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 2, 2026 9:21am UTC
loom-telemetry Ready Ready Preview Dec 2, 2026 9:21am UTC

Request Review

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge preserves an undecodable string for the converter. It also accepts truncated top-level records that the old merge rejected.

KaloyanNaumov and others added 2 commits December 1, 2026 23:57
_top_level_records cut a record to the bytes that were left when its
declared length ran past the end of the data. The merge then wrote a
fresh header for the shorter slice, so a truncated or damaged part came
out as a valid document where the whole-index parse had refused it. The
converter read the same slice and converted it.

The reader now raises ValueError naming the offset and how far the record
overruns. The merge stops with the part's name for that error and for a
part that ends inside a record header.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length-delimited truncation tests pass. The merge still accepts a truncated fixed-width record that the protobuf parser rejects.

Comment thread scripts/scip_harness/scip_to_delta.py Resolved
_top_level_records bounds-checked length-delimited records only. A
fixed32 or fixed64 field cut short was skipped by moving past the end of
the data, so a part whose valid records were followed by one payload byte
merged and converted with exit 0 where the protobuf parser refuses the
index.

The reader now refuses what the parser refuses and raises ValueError in
the existing form: fixed32 and fixed64 spans that run past the end, a
varint or tag that runs off the end or exceeds ten bytes, field number 0,
and unbalanced or mismatched groups and wire types 6 and 7. A closed
group in an unknown field is skipped, as the parser skips it. The merge's
IndexError branch is gone because the reader no longer raises it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The truncated fixed-width records are rejected. A field number outside protobuf’s allowed range can still pass the new reader.

Comment thread scripts/scip_harness/scip_to_delta.py Resolved
_top_level_records refused only field number 0. A field number of 2**29
or more, at the top level or in a group tag, is refused by the parser but
was skipped here, so a part carrying one merged and converted with exit 0.

Tags and length prefixes are now read as the parser reads them: a varint
of at most five bytes that fits in 32 bits, which bounds the field number
to 1..2**29 - 1. Value varints keep the ten-byte limit. The reserved range
19000..19999 and redundant trailing zero bytes inside the five-byte limit
are accepted, as the parser accepts them. Lengths of 2**63 and above were
already refused and are pinned. Behaviour was compared with the upb
parser, the installed default.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 159 focused merge and converter tests pass, including the new tag bounds. A valid document can still be merged under the wrong path.

KaloyanNaumov and others added 2 commits December 2, 2026 04:58
_document_path returned the first relative_path record in a document. The
protobuf parser keeps the last value of a singular field, so a document
carrying pkg/first.ts then pkg/second.ts is pkg/second.ts, and the merge
treated it as pkg/first.ts and dropped a separate pkg/first.ts document
as its duplicate. The converter shares the reader and ordered, filtered
and deduplicated by the wrong path.

_document_path now returns the last relative_path. Metadata is the other
singular field read from raw records: the parser merges repeated metadata
records, but _project_root used the first and the merge copied only the
last. _project_root now merges every metadata record before the first
document, and the merge copies every metadata record of the first part.
Document, symbol and occurrence fields are parsed by the protobuf runtime
and need no change.

Each reader is compared with the parser on the same bytes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 0267d57. Configure here.

Comment thread scripts/scip_harness/scip_to_delta.py Resolved
A value varint of ten bytes is accepted whatever its tenth byte holds.
The protobuf parser (upb, and the pure-Python backend) accepts it, so a
part with such a trailing unknown varint parses whole and merges the
same way. Tag and length varints are bounded
at five bytes and refused beyond, as the parser refuses them.

The reader already matches, so this adds tests only: a value varint
ending in 0x00, 0x01, 0x02 or 0x7f, at top level and inside a group, is
accepted by the reader and the merge, each compared with the parser.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last relative_path value now wins for duplicate protobuf fields, and all 189 focused tests passed. Distinct document paths can still collapse into one merge record.

KaloyanNaumov and others added 2 commits December 2, 2026 06:48
The merge keyed documents by the relative_path decoded with replacement,
so two paths that differ only by a malformed byte and a literal U+FFFD
collapsed to one key and the later document replaced the earlier one.
Reversing the inputs dropped the other document.

The merge now keys on the raw path bytes (the last value of field 1) and
decodes the path only to ask which project owns it. Ownership is
unchanged: both names decode to the same text, so both get the same
answer. scip_to_delta gains _document_path_bytes; _document_path decodes
it as before.

The new test merges a document named with a 0xff byte and one named with
U+FFFD, in both input orders, with and without a project; both survive.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
mypy on the two modules stays at its baseline of 54 errors.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The analyzer job reports the same mentor evidence shape mismatch on this head's tested base. The focused test passes on current main with the snapshot update from #1957. The existing digest repair in #1990 addresses the SQL comment that made the literal affect the shape. Failed CI job · Digest repair

The merge keys its documents on raw bytes, so a caller that matches the
kept set against member paths (text) would never find a path in it.
merge_indexes now returns the kept paths decoded with "replace", the
same text ownership reads, and deduplication still runs on the bytes.

The unused `_document_path` alias is gone; the one test that read the
converter's function through it names the converter.

Assisted-by: Claude Code:claude-sonnet-5-5
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Raw path keys now preserve both documents in the earlier collision. The merge can still truncate an input when it opens the temporary output.

An input named <output>.tmp was truncated when the merge opened its
temporary output, and removed on an error. Create the temporary beside the
output with exclusive creation under a fresh name, then replace the output
once the write succeeds.

Assisted-by: Claude Code
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The merge uses an exclusive temporary file beside the output. It preserves an input named like the old temporary path and replaces the output only after the write succeeds. All 195 focused merge and converter tests passed.

@zlareb1
zlareb1 merged commit 57ca94f into ClickHouse:main Dec 2, 2026
77 checks passed
@KaloyanNaumov
KaloyanNaumov deleted the pr/ts-merge-record-level branch December 4, 2026 23:07
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