8000 fix(bulk): reject one lone-surrogate doc per-item, not the whole batch by xerj-org · Pull Request #1311 · xerj-org/xerj · GitHub
[go: up one dir, main page]

Skip to content

fix(bulk): reject one lone-surrogate doc per-item, not the whole batch - #1311

Merged
xerj-org merged 1 commit into
mainfrom
fix/issue-1304
Oct 11, 2026
Merged

xerj-org merged 1 commit into
mainfrom
fix/issue-1304

Conversation

@xerj-org
Copy link
Copy Markdown
Owner

What

A bulk load of auto-id documents rejected its whole batch when a single
document carried an unpaired UTF-16 surrogate in a \uXXXX string escape —
the shape a fixed-width-character chunker produces when it cuts an emoji in
half ("a" + String.fromCharCode(0xd83d) + "b" → "a\ud83db"). ES's bulk
contract is per-document: the one bad document errors and its neighbours
index. This restores that.

Root cause

  • The index turbo path validates each source line with
    serde_json::IgnoredAny (bulk.rs), which walks the JSON without
    decoding
    string contents, so an escape encoding an unpaired surrogate is
    accepted and the raw bytes retained.
  • The auto-id docs are grouped and handed as one batch to
    Index::index_batch_turbo_raw, which parses every doc into a
    serde_json::Value (parse_validated_raw_batch →
    IndexStore::parse_raw_batch → serde_json::from_slice::<Value>). That
    parse does decode escapes and rejects the unpaired surrogate — but it
    fails the whole batch with a single Err, so process_bulk marks every
    item in the batch failed.

Unpaired surrogates are the only semantic string check Value makes that
IgnoredAny skips; nesting depth is already capped identically upstream in
validate_raw_json_nesting.

Fix

Close that one gap at the per-item bulk layer, which already does per-item
error reporting. After IgnoredAny accepts an index source line,
lone_surrogate_escape_reason scans it for an unpaired surrogate and, if
found, returns a per-item 400 naming the bad document. The bad document never
enters a turbo batch, so the batch Value parse succeeds and the neighbours
index.

The scan runs only after IgnoredAny has guaranteed well-formed structure
(every \u has four hex digits, every string is terminated), so it only
checks surrogate pairing. A leading memchr fast-path skips the common
backslash-free document entirely, preserving the turbo ingest win.

Before / after

A 128-doc auto-id batch with one unpaired-surrogate document:

items 400 indexed
before 128 (whole batch) 0
after 1 (names the surrogate) 127

Tests (cargo test -p xerj-engine --lib lone_surrogate)

  • surrogate_scan_matches_serde_value_decision — pins the scan's
    accept/reject decision to serde_json's own Value decision across valid
    pairs, lone high/low surrogates (in body and key), escaped backslashes and
    ordinary escapes.
  • one_lone_surrogate_does_not_sink_its_bulk_batch — the issue repro: 127
    indexed, 1 per-item 400. Fails before the fix (all 128 fail), passes after.
  • multiple_bad_surrogates_each_fail_individually — two bad docs each fail
    on their own; the rest index.

cargo fmt -p xerj-engine --check and cargo clippy -p xerj-engine --lib
are clean. The change is ES-compat-positive and touches no valid-JSON path,
so the ES-YAML conformance gate is unaffected; the full gate runs in CI.

Closes #1304

🤖 Generated with Claude Code

One document carrying an unpaired UTF-16 surrogate in a `\uXXXX` string
escape (e.g. an emoji cut in half by a fixed-width chunker, serialised as
`"a\ud83db"`) made the bulk endpoint reject its entire batch of auto-id
documents instead of that one document. A ~58,000-chunk load failed far
from the cause with a single `lone leading surrogate in hex escape` and
nothing indexed, violating the ES bulk contract of per-document error
reporting.

Root cause: the `index` turbo path validates each source line with
`serde_json::IgnoredAny` (bulk.rs), which walks the JSON *without decoding*
string contents, so an escape encoding an unpaired surrogate is ACCEPTED
and the raw bytes retained. The auto-id docs are then grouped and handed as
one batch to `Index::index_batch_turbo_raw`, which parses every doc into a
`serde_json::Value` (`parse_validated_raw_batch` ->
`IndexStore::parse_raw_batch` -> `serde_json::from_slice::<Value>`). That
parse DOES decode escapes and rejects the unpaired surrogate, but it fails
the whole batch with a single `Err`, so `process_bulk` marks every item in
the batch failed. Unpaired surrogates are the sole semantic string check
`Value` makes that `IgnoredAny` skips (nesting depth is already capped
identically upstream in `validate_raw_json_nesting`).

Fix: close that one gap at the per-item bulk layer, which already reports
per-item errors. After `IgnoredAny` accepts an `index` source line,
`lone_surrogate_escape_reason` scans it for an unpaired surrogate in a
`\uXXXX` escape and, if found, returns a per-item 400 `document_parsing`
error naming the bad document. The bad document therefore never enters a
turbo batch, so the batch `Value` parse succeeds and its neighbours index.
The scan runs only after `IgnoredAny` has guaranteed well-formed structure
(every `\u` has four hex digits, every string is terminated), so it only
checks surrogate pairing; a leading memchr fast-path skips the common
backslash-free document entirely, preserving the turbo ingest win. A unit
test pins the scan's accept/reject decision to serde_json's own `Value`
decision across valid pairs, lone high/low surrogates (in body and key),
escaped backslashes and ordinary escapes.

Before: a 128-doc auto-id batch with one unpaired-surrogate doc -> all 128
items 400, 0 indexed. After: 1 item 400 (names the surrogate), 127 indexed.

Files:
- engine/crates/xerj-engine/src/bulk.rs: `lone_surrogate_escape_reason`,
  `lone_surrogate_document_reason`, the per-item `index` check, and tests.

The fix is ES-compat-positive (per-item reporting mirrors ES) and touches no
valid-JSON path, so the ES-YAML conformance gate is unaffected; the full
gate runs in CI.

Closes #1304
@cla-bot cla-bot Bot added the cla-signed label Oct 11, 2026
@xerj-org
xerj-org merged commit babde39 into main Oct 11, 2026
41 of 42 checks passed
neoneye pushed a commit to agent-memory-atlas-archive/xerj-org--xerj that referenced this pull request Oct 13, 2026
Window since v1.0.0-rc.93 (29 merged PRs): a wave of external-contributor
fixes (Vinz2168, thomas-villani, yogthos, pierreaubert) plus a backlog-fix
sweep closing six defects — hybrid query+knn page-local ranking (xerj-org#1303/xerj-org#1312),
bulk lone-surrogate whole-batch rejection (xerj-org#1304/xerj-org#1311), autoindex --watch
double-owner (xerj-org#1232/xerj-org#1307), sibling-fork reconcile abort (xerj-org#1230/xerj-org#1309),
map-only --dataset accept-and-ignore (xerj-org#1231/xerj-org#1308), non-note Second Brain
vocabulary (xerj-org#1100/xerj-org#1310). Three closed as already-fixed (xerj-org#1212, xerj-org#1244, xerj-org#1173).

Stamp: workspace version 1.0.0-rc.93 -> 1.0.0-rc.94 (Cargo.toml + Cargo.lock),
landing data-latest-tag, CHANGELOG [1.0.0-rc.94] section, ROADMA
77F6
P review line
(now v1.0.0-rc.94) and rc.94 roll (nine open defects carried). Release-cut
machine checks (docs_capability_lists, docs_do_not_promise_the_next_release)
pass locally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rc.93: one lone surrogate in a document rejects the whole bulk batch instead of the one document

1 participant

0