Skip to content

Studio: decode escaped text in spreadsheet previews - #16592

Merged
oobabooga merged 2 commits into
unslothai:mainfrom
DimitriChrysafis:studio-xlsx-text-escapes
Nov 4, 2026
Merged

oobabooga merged 2 commits into
unslothai:mainfrom
DimitriChrysafis:studio-xlsx-text-escapes

Conversation

@DimitriChrysafis

Copy link
Copy Markdown
Contributor

What was broken

XLSX previews display Excel's escaped text literally. A cell containing a Windows-style line break shows _x000D_, and a literal _x000D_ shows its _x005F_ quoting prefix. This affects both shared and inline cell strings, including Library thumbnails.

Repro

  1. Use main at 81dc73ac43b3bb487820752a4e749f6cf2c3f05f.
  2. Create an XLSX with XlsxWriter using worksheet.write_string(0, 0, "First\r\nSecond").
  3. Open it in Studio's spreadsheet preview. The first cell contains the visible text First_x000D_ instead of preserving the original cell text.

The regression tests reproduce this without a browser or external workbook dependency.

Fix

Decode SpreadsheetML text escapes after XML entities, within each text run and cached string value. Consume escaped literal tokens whole so their trailing underscore cannot be decoded again. This follows Excel's escaped-string rules without changing formulas, CSV text, or number formatting.

Test

Two regression tests cover shared strings, inline strings, cached formula strings, carriage returns, tabs, surrogate pairs, literal escape-looking text, XML entities, and rich-text run boundaries. Both tests fail before the fix.

From studio/frontend:

$ node --experimental-strip-types --test --test-reporter=tap --test-name-pattern='xlsx: decodes' tests/file-viewer-office.test.ts
TAP version 13
# Subtest: xlsx: decodes escaped text in shared, inline and cached formula strings
ok 1 - xlsx: decodes escaped text in shared, inline and cached formula strings
  ---
  duration_ms: 8.43675
  type: 'test'
  ...
# Subtest: xlsx: decodes rich text runs independently
ok 2 - xlsx: decodes rich text runs independently
  ---
  duration_ms: 0.800083
  type: 'test'
  ...
1..2
# tests 2
# suites 0
# pass 2
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 102.556708

The full frontend suite reports 14,785 passing tests, no failures, and one Windows-only skip on macOS. Locale parity, typecheck, build, bundle budget, and all seven repository lint commands also exit successfully. Separately checked XlsxWriter's shared and inline workbooks, including overlapping literal escape sequences.

@oobabooga

Copy link
Copy Markdown
Member

Thanks @DimitriChrysafis. XLSX previews and Library thumbnails showed SpreadsheetML escapes as text, so a cell with a Windows line break read First_x000D_ and a literal _x000D_ read _x005F_x000D_. decodeXstring now decodes them in each text run after XML entities. The same readXlsx output is what the model gets for an XLSX attached to chat (extractOfficeAttachmentText), so that path no longer sees the escapes either.

I pushed 98cacaaf75 to keep the _x introducer case-sensitive, as the spec defines it. Excel stores the literal text _X0000_ without escaping it (XlsxWriter's shared_strings02.xlsx reference file), and the i flag would have turned that cell into a NUL character.

I compared main and this PR on 1,845 real workbooks: the Apache POI, calamine, ExcelJS and ClosedXML test files, plus XlsxWriter's reference files saved by Excel. Across 1.17 million cells, the only changes are escapes that now decode, in 11 files. Where the source strings are known:

Files main PR
XlsxWriter's Excel files shared_strings01-03 and escapes09 (146 cells) 105 exact 145 exact
calamine's has_x000D_ inline and formula string files ABC_x000D_\nDEF ABC\r\nDEF, as calamine's tests expect

The new tests fail on main and pass here, and the office viewer, attachment preview and server document attachment tests pass (101). Mirror CI is clear at 98cacaaf75: 40 checks passed, and the 5 failures are cancellations: four macOS jobs that no hosted runner picked up, and the workflow-trigger lint hitting its 15-minute limit.

The one cell still off in the table is older than this PR: the reader keeps a literal \r\n inside <t> instead of reading it as \n. The backend knowledge base reader (_xlsx_text in office_formats.py) still indexes the raw escapes, and that can be fixed separately.

@oobabooga
oobabooga merged commit a17bfe9 into unslothai:main Nov 4, 2026
oobabooga pushed a commit that referenced this pull request Nov 6, 2026
## What was broken

Spreadsheet previews decode Excel's `_xHHHH_` text escapes after #16592,
but RAG ingestion uses a separate reader and still indexes the encoded
strings. Stored text differs from the preview, and lexical searches can
miss the decoded cell contents.

This is the backend follow-up noted in the merge comment on #16592.

## Repro

1. On main at `91be10960`, put `Zebra_x006D_arker` in an XLSX shared
string, inline string or cached formula result.
2. Upload it for RAG indexing. Ingestion completes, but the stored text
still contains the escape.
3. Search for `zebramarker`. There are no lexical matches.

## Fix

Decode each text run before joining it, preserving quoted underscores
and literal uppercase `_X` sequences. Combine UTF-16 surrogate pairs
after joining runs, and replace unmatched surrogate units so the result
remains valid UTF-8 for indexing. Cached formula strings use the same
decoder. Number and error cells are unchanged.

## Test

Nineteen regression cases fail before the fix; all 21 selected cases
pass afterward. Coverage includes shared, inline and cached strings,
rich-run boundaries, phonetic hints, quoted escapes, supplementary
Unicode, and Strict OOXML. Upload-to-FTS tests cover `.xlsx`, `.xlsm`,
`.xltx` and `.xltm`, using stub embeddings with the real ingestion and
SQLite paths.

From `studio/backend`:

```text
$ python -m pytest -q --tb=short --timeout=330 tests/test_rag_office_formats.py
161 passed in 4.57s
```

```text
$ python -m pytest -q --tb=short -n 4 --dist loadgroup --timeout=330 tests/test_rag_office_formats.py tests/test_rag_parsing.py tests/test_rag_ingestion.py tests/test_rag_retrieval.py
333 passed, 1 skipped in 7.94s
```

The skip is the opt-in real-embedder test. Separately checked 16 exact
string round trips through XlsxWriter-generated shared-string and
inline-string workbooks, including control characters and escape-looking
literals.

All seven repository checks and the pinned pre-commit formatter pass. No
schema or dependency changes.
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.

3 participants