Repository navigation
fix(encoding): every UTF-8 encode and write names its handler; five commands take a lone surrogate - #1654
fix(encoding): every UTF-8 encode and write names its handler; five commands take a lone surrogate#1654antonwolfy wants to merge 4 commits into
Conversation
…#1617) `catalog novelty`, `report opportunities`, `skill shorten --lexical-only`, `skill novelty retest` and `repo reach --harness` encoded text as strict UTF-8 where it can hold a lone surrogate (a file name that is not UTF-8, or a `"\ud800"` escape in a JSON or YAML file) and ended in a `UnicodeEncodeError` traceback and exit 5. `skill shorten` and `skill novelty retest` also left an empty output file behind. The two hashes (`_dirname` of `catalog novelty`, `Opportunity.source_key`) encode with `errors="surrogatepass"`: valid text gives the same bytes, so no output directory name or opportunity key moves. The Markdown of `skill shorten`, `skill novelty retest` and each skill's `novelty.md` in `catalog novelty` is written with `errors="replace"`, so the surrogate is a `?` as in the report writers; `catalog novelty` needs it too, since the sweep would otherwise pay for the analysis and then report the skill as an error row. `parse_profile` refuses a harness profile whose `config_files` value holds a lone surrogate when it loads, naming `config_files/<name>`: exit 2 in `repo reach` and `repo budget`, where the harness would otherwise read a config with a `?` in it. `write_config` writes with `errors="strict"`, the load being its guard. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…1619) Strict UTF-8 encoding of text that can hold a lone surrogate (a file name that is not UTF-8, a `"\ud800"` escape in JSON or YAML) ended five commands in a traceback and exit 5 (intel#1617), and it was one of the longest fix series. The rule is now a convention: a `.encode()` or `.encode("utf-8")`, a `write_text(..., encoding="utf-8")` and a write-mode `open(..., encoding="utf-8")` in the package each name `errors=`. The sweep covers the 116 sites left once the five bugs of intel#1617 are fixed in the commit before: 28 `errors="surrogatepass"`, 72 `errors="replace"` and 16 `errors="strict"`. `surrogatepass` is where the bytes go into a hash or digest: valid text gives the same bytes, so no stored digest, key or directory name changes. `replace` is where the text is written or sent, as the report writers do since intel#1101. `strict` is where the raise is the guard and a caller catches the `UnicodeEncodeError` and turns it into a refusal, a row or a note, so its behaviour is exactly as before. For valid UTF-8 text every byte any command writes, hashes or sends is unchanged. `scripts/check_encoding_errors.py` walks the package with `ast`, prints each finding as `path:line: <call>` with a closing line that says which handler fits which use, and exits 1; it has no baseline and no waiver. It is its own `Encoding error handlers` step in the `static` job of `ci.yml`, and so a step of `make ci-local`. `tests/test_check_encoding_errors.py` pins each flagged shape, each shape that passes, and that the package at HEAD has no finding. `CONTRIBUTING.md` and `AGENTS.md` say what the check refuses and which handler fits. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
…and exit 1; the refusal and the tests name their doc (intel#1617) `repo run` loads the same profile as `repo reach`, and the contract doc and the fragment say it ends a refusal with `✗` and exit 1, but no test ran it. A new test runs `repo run` with a `config_files` value `"\ud800"` and a `--bin` that does not exist, so it stops at the profile load, and asserts exit 1 and the `✗ ...: config_files/x.json is not valid UTF-8 text` line. On `main`'s code it reaches the missing `--bin` instead. `docs/repo-evaluation-contract.md` now says `repo run` ends the profile refusal with `✗` and exit 1. The new `ProfileError` in `_check_config_placeholders` names the contract sentence and intel#1617 in a comment, and the tests of `tests/test_encoding_surrogate_commands.py` cite that sentence or the intel#1617 row they check. Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
There was a problem hiding this comment.
🟡 Changes recommended
The encoding guard misses literal keyword unpacking, valid surrogate pairs are incorrectly rejected, and Windows path assumptions cause test failures.
4 open issues
What changed in this PR
4 new findings.
This PR fixes lone-surrogate failures in five Dokimi commands and establishes explicit UTF-8 error handling across the package, addressing #1617 and #1619.
Changes:
- Preserve surrogate-containing hash inputs, replace surrogates in reports, and reject invalid harness configuration text.
- Specify encoding handlers throughout the package without changing valid UTF-8 output.
- Add an encoding checker, CI enforcement, regression tests, and contributor guidance.
| File | Description |
|---|---|
dokimi/tests/test_encoding_surrogate_commands.py |
Adds command-level surrogate regressions. |
dokimi/tests/test_check_encoding_errors.py |
Tests checker detection and diagnostics. |
dokimi/tests/test_catalog_novelty.py |
Tests non-UTF-8 skill directory names. |
dokimi/scripts/check_encoding_errors.py |
Adds the encoding-handler checker. |
dokimi/dokimi/write_probe.py |
Names the probe’s write handler. |
dokimi/dokimi/tool_preference.py |
Specifies hash encoding handlers. |
dokimi/dokimi/subjects/evidence_check.py |
Keeps canonical JSON encoding strict. |
dokimi/dokimi/skill_coverage.py |
Specifies skill hash encoding. |
dokimi/dokimi/runs/rundir.py |
Names the run-marker write handler. |
dokimi/dokimi/runs/bundle.py |
Specifies manifest encoding. |
dokimi/dokimi/runner/plan.py |
Specifies plan hash handlers. |
dokimi/dokimi/runner/manifest.py |
Specifies manifest digest encoding. |
dokimi/dokimi/repos/workspace.py |
Keeps bridge writes strict. |
dokimi/dokimi/repos/transcript.py |
Specifies clipped-output byte counting. |
dokimi/dokimi/repos/support/no_background.py |
Specifies hook-log write handling. |
dokimi/dokimi/repos/source.py |
Specifies Git request encoding. |
dokimi/dokimi/repos/skill_reach.py |
Specifies salt and trace handlers. |
dokimi/dokimi/repos/selfcheck.py |
Specifies self-check output handlers. |
dokimi/dokimi/repos/security.py |
Specifies execution-log encoding. |
dokimi/dokimi/repos/runner.py |
Specifies trace, answer, and lock handlers. |
dokimi/dokimi/repos/replay/merged.py |
Keeps filename validation strict. |
dokimi/dokimi/repos/receipt.py |
Specifies source digest encoding. |
dokimi/dokimi/repos/reach.py |
Specifies validation and trace handlers. |
dokimi/dokimi/repos/l0/typecheck.py |
Specifies temporary-file write handlers. |
dokimi/dokimi/repos/host.py |
Specifies host fingerprint encoding. |
dokimi/dokimi/repos/harness_profile.py |
Rejects surrogate-containing configuration values. |
dokimi/dokimi/repos/guidance_arms.py |
Specifies guidance output encoding. |
dokimi/dokimi/repos/grading.py |
Specifies Git configuration writes. |
dokimi/dokimi/repos/graders/typecheck.py |
Keeps type-check configuration writes strict. |
dokimi/dokimi/repos/graders/main.py |
Specifies grade and oracle handlers. |
dokimi/dokimi/repos/graders/__init__.py |
Specifies grader digest encoding. |
dokimi/dokimi/repos/batch_ledger.py |
Specifies execution hash handlers. |
dokimi/dokimi/report/json_report.py |
Specifies JSON report writes. |
dokimi/dokimi/process_io.py |
Names JSON and truncation-marker handlers. |
dokimi/dokimi/plugins.py |
Specifies plugin hash encoding. |
dokimi/dokimi/opportunities.py |
Supports surrogates in opportunity keys. |
dokimi/dokimi/novelty/retention.py |
Specifies retention cache writes. |
dokimi/dokimi/novelty/provenance.py |
Specifies prompt digest encoding. |
dokimi/dokimi/novelty/needs.py |
Specifies prompt-version encoding. |
dokimi/dokimi/novelty/grounding.py |
Specifies grounding source hashes. |
dokimi/dokimi/novelty/bundle.py |
Specifies bundle text hashes. |
dokimi/dokimi/metrics/example_runner.py |
Specifies snippet write handlers. |
dokimi/dokimi/memory/lint.py |
Specifies fallback byte counting. |
dokimi/dokimi/llm_cli.py |
Keeps prompt-size encoding strict. |
dokimi/dokimi/harnesses/swebench.py |
Keeps task-file writes strict. |
dokimi/dokimi/harnesses/swebench_grade.py |
Specifies grading output handlers. |
dokimi/dokimi/harnesses/solver_stream.py |
Specifies solver record writes. |
dokimi/dokimi/harnesses/skill_treatment.py |
Specifies plugin metadata writes. |
dokimi/dokimi/harnesses/operations.py |
Keeps UTF-8 validation strict. |
dokimi/dokimi/harnesses/network_policy.py |
Specifies policy-error record writes. |
dokimi/dokimi/harnesses/harbor_export.py |
Specifies export hash and write handlers. |
dokimi/dokimi/harnesses/fidelity.py |
Specifies fixture response encoding. |
dokimi/dokimi/harnesses/egress_proxy.py |
Keeps proxy request encoding strict. |
dokimi/dokimi/harnesses/docker_solver.py |
Specifies solver output handlers. |
dokimi/dokimi/harnesses/delivery.py |
Specifies fixture lookup hashing. |
dokimi/dokimi/harnesses/container_agent.py |
Specifies egress-log writes. |
dokimi/dokimi/harnesses/command.py |
Specifies output byte-count encoding. |
dokimi/dokimi/harnesses/benchflow.py |
Specifies benchmark log writes. |
dokimi/dokimi/external/upstream.py |
Specifies Git and manifest handlers. |
dokimi/dokimi/eval/panel.py |
Specifies panel prompt hashing. |
dokimi/dokimi/eval/judge.py |
Specifies judge prompt hashing. |
dokimi/dokimi/eval/ask_outcome.py |
Specifies rubric hashing. |
dokimi/dokimi/digest.py |
Specifies digest metadata encoding. |
dokimi/dokimi/commands/tools.py |
Specifies item export writes. |
dokimi/dokimi/commands/subjects.py |
Specifies matrix configuration writes. |
dokimi/dokimi/commands/routing_compare.py |
Specifies comparison output writes. |
dokimi/dokimi/commands/repo_hits.py |
Specifies hit-sheet writes. |
dokimi/dokimi/commands/repair_receipt.py |
Specifies receipt output writes. |
dokimi/dokimi/commands/opportunities.py |
Keeps JSON Unicode validation strict. |
dokimi/dokimi/commands/novelty.py |
Handles surrogates in shortened Markdown. |
dokimi/dokimi/commands/novelty_run.py |
Specifies panel verdict writes. |
dokimi/dokimi/commands/novelty_routing.py |
Specifies refusal record writes. |
dokimi/dokimi/commands/novelty_routing_report.py |
Specifies routing result writes. |
dokimi/dokimi/commands/novelty_retest.py |
Handles surrogates in retest Markdown. |
dokimi/dokimi/commands/novelty_budget.py |
Specifies complete and partial writes. |
dokimi/dokimi/commands/headroom.py |
Specifies content digest encoding. |
dokimi/dokimi/commands/catalog.py |
Specifies trigger-set export writes. |
dokimi/dokimi/commands/catalog_scan.py |
Specifies scan record rewrites. |
dokimi/dokimi/commands/catalog_novelty.py |
Handles surrogate names and reports. |
dokimi/dokimi/commands/catalog_eval.py |
Specifies checker identity hashing. |
dokimi/dokimi/agent_run/runner.py |
Specifies host transcript writes. |
dokimi/dokimi/agent_run/container.py |
Specifies container trace writes. |
dokimi/dokimi/agent_run/adapters.py |
Specifies adapter configuration writes. |
dokimi/docs/repo-evaluation-contract.md |
Documents profile refusals and exit codes. |
dokimi/CONTRIBUTING.md |
Documents encoding enforcement. |
dokimi/changelog.d/20261205_100000_lone-surrogate-five-commands.md |
Records the command fixes. |
AGENTS.md |
Adds encoding-handler guidance. |
.github/workflows/ci.yml |
Adds the encoding checker step. |
Review effort: Balanced
|
✅ OpenCodeReview: Review complete: 0 finding(s) across 89 selected item(s). CI at Review details
|
Mutation of changed linesAt 9d6d010: 54 caught · 1 missed · 17 inconclusive Each row is a small deliberate break (a mutant) of a line this PR changes that every test reaching that line still passes. Add a test that fails on it or, if it cannot change behaviour, put
Not the whole picture:
Warning only: a missed break does not fail the job. Updated on every push; the full report is in the ci run summary. |
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1654 +/- ##
==========================================
- Coverage 96.49% 96.49% -0.01%
==========================================
Files 569 569
Lines 75249 75256 +7
Branches 12980 12980
==========================================
+ Hits 72613 72617 +4
Misses 1611 1611
- Partials 1025 1028 +3
🚀 New features to boost your workflow:
|
…irs, cover the index-size fallback
The encoding check read a call with any `*` or `**` as unknown, so `text.encode("utf-8", **{})` passed; a literal `*(...)`, `*[...]` or `**{"key": ...}` is now read as the arguments it holds, and a pack that is not a literal stays unknown.
A profile config file written as a YAML `"😀"` escape is one character, not a lone surrogate: the load check and `write_config` join the pair first, so only an unpaired surrogate is refused.
The check's tests build the expected path with the script's own `_shown` and split the finding line from the right, so a Windows drive prefix does not break them. A test reaches the `errors="replace"` fallback of the memory lint index size, which codecov named.
Signed-off-by: Anton Volkov <antonwolfy@gmail.com>
napetrov
left a comment
There was a problem hiding this comment.
Change class: CONTRACT (head 9d6d010)
- Contract: changed. errors dokimi/dokimi/repos/harness_profile.py:240; cli dokimi/dokimi/harnesses/docker_solver.py
- Responsibility: none
- Dependency: none
- Review depth: check the changed surface: callers, docs, tests pin new behaviour
- Coverage gaps: none
0 blocking, 1 minor.
The encoding sweep prevents the five reported command failures and adds an AST check. Two text writer APIs can still select implicit strict encoding with errors=None and pass the check.
Combined with #1562, the new encoding check fails on the trigger-query JSON writer. Add the appropriate explicit handler to that writer before both can land. The paired merge with #1582 also conflicts in harness_profile.py.
CI on head 9d6d010: 0 failing Actions jobs; merge base 4deffc0 is behind base tip 5225c2a.
Compared the change and input edges against the merge base and checked existing threads and GitHub Actions. Runtime checks used focused Linux tests and local fixtures. Focused tests on the head: head 243 passed in 35.62s.
| return ( | ||
| _is_write_mode(_arg(call, 1 - shift, "mode")) | ||
| and _is_utf8(_arg(call, 3 - shift, "encoding")) | ||
| and _arg(call, 4 - shift, "errors") is None |
There was a problem hiding this comment.
nit: errors=None passes the check and still uses implicit strict encoding
dokimi/scripts/check_encoding_errors.py:132 The same presence check in the write_text branch treats an explicit None as a handler (C7).
How to check: Both Path.write_text and Path.open with UTF-8 and errors=None pass the new checker. For these APIs None selects the default strict handling.
Expected: Flag a literal None for UTF-8 text writes alongside an omitted handler.
napetrov
left a comment
There was a problem hiding this comment.
Checked the five encoding fixes, focused Linux tests, and required CI on this head. The errors=None case remains a minor lint gap. Before #1562 lands, add an explicit handler to its trigger-query JSON writer so the combined tree passes the new check.


Summary
Five commands encode text as strict UTF-8 where it can hold a lone surrogate (a file or directory name with a byte that is not UTF-8, which Python reads as
\udcXX, or a"\ud800"escape in a JSON or YAML file) and end in aUnicodeEncodeErrortraceback and exit 5 (#1617). Onmain(4deffc0):maincatalog novelty ROOTa\xffcommands/catalog_novelty.py:187(_dirnamehasheskey.encode('utf-8')) before the first skillreport opportunities FILEa\xff.txtopportunities.py:42(source_key), nothing on stdoutskill shorten --lexical-only"\ud800"in a span text of the--atomsfilecommands/novelty.py,--outleft at 0 bytesskill novelty retest"\ud800"in onenovelty.json'sartifact.refretest.jsonwritten,retest.mdleft at 0 bytesrepo reach --harness bad.yamlconfig_filesvalue"\ud800"repos/harness_profile.py:172(write_config), noreach.jsonThe class had been fixed one site at a time (#761, #863, #886, #914, #1020, #1101). #1619 turns it into a convention: every UTF-8
.encode(),write_textand write-modeopenin the package names itserrors=handler, and a check keeps it so. The check found 122 sites at this head: the six sites of the five bugs above (catalog noveltyhas two) and 116 others, each given the handler of its use (a hash, written or sent text, or a raise a caller turns into a refusal).Fixes taken for #1617: the two hashes encode with
errors="surrogatepass"(valid text gives the same bytes, so no output directory name or opportunity key moves);skill shortenandskill novelty retestwrite their Markdown witherrors="replace", as #1101 did for the report writers;repo reach --harnessrefuses the profile when it loads (exit 2, namingconfig_files/<name>) instead of replacing, because the harness would otherwise read a config whose value was silently changed to?, and a refusal before any spend names the input.Closes #1617
Closes #1619
The same cause, in a custom profile whose
config_filesuse{config_dir}with a--work-dirthat is not UTF-8, is tracked in #1652.data import doclingwrites the converted Markdown with strict UTF-8 throughwrite_text_atomic; with the converter stubbed to return a lone surrogate it ends in a traceback and exit 5, tracked in #1653.Changes
catalog novelty:_dirnamehashes witherrors="surrogatepass"(a\xffgetsa-d76edf05;accel-tuningkeepsaccel-tuning-101d2518), and each skill'snovelty.mdis written witherrors="replace". Without the second change the sweep pays for the analysis and then reports the skill as an error row.report opportunities:Opportunity.source_keyhashes witherrors="surrogatepass"(the keys991d9233259f5640and364e0f3751016560of valid text are the same onmain).skill shorten --lexical-onlyandskill novelty retest: the Markdown is written witherrors="replace";retest.jsonis unchanged.repo reach --harness(andrepo budget):_check_config_placeholdersinrepos/harness_profile.pyrefuses aconfig_filesvalue with a lone surrogate (a"😀"pair is joined into its character first, here and inwrite_config, so only an unpaired surrogate is refused), beside the plain-file-name and placeholder refusals;write_configwrites witherrors="strict", the load being its guard.docs/repo-evaluation-contract.mdlists the profile refusals, with the exit code ofrepo reach,repo budgetandrepo run.surrogatepass(hashes and digests), 72replace(written or sent text), 16strict(the raise is the guard, a caller turns it into a refusal, a row or a note:_check_json_unicode,shadow_for,canonical_json,load_manifestand the like). For valid UTF-8 text every byte any command writes, hashes or sends is unchanged, and no existing test's expected value changed.scripts/check_encoding_errors.py: an AST walk over the package that flags.encode()and.encode(<utf-8>),write_text(..., encoding=<utf-8>)and write-modeopen,io.openand<path>.openwith noerrors; it printspath:line: <call>and one line saying which handler fits which use. A literal*(...),*[...]or**{"key": ...}is read as the arguments it holds (text.encode("utf-8", **{})is flagged). No baseline, no waiver. Not covered, and its docstring says so: a*or**of anything else (**options), an encode through a helper,bytes(text, "utf-8"), a non-literal encoding, other codecs and the decoding side.Encoding error handlersstep in thestaticjob ofci.yml(somake ci-localruns it), a paragraph inCONTRIBUTING.mdand a bullet inAGENTS.md.tests/test_encoding_surrogate_commands.py(one per command, from the issue's repros, and one that runsrepo runwith the surrogate profile and a--binthat does not exist, so it stops at the profile load with✗and exit 1), one intests/test_catalog_novelty.py, andtests/test_check_encoding_errors.py(its path and line assertions hold on Windows, where a path has a drive prefix). A test intests/test_memory_lint.pyreaches theerrors="replace"fallback of the index size inmemory/lint.py, used whenMEMORY.mdcannot be statted.Risk
Valid UTF-8 text is encoded, hashed and written as before, for every command. What differs is an input with a lone surrogate: the five commands above no longer exit 5, and
repo reachandrepo budgetexit 2 withError: <profile>: config_files/<name> is not valid UTF-8 text (a lone surrogate)where a profile holds one (repo runloads the same profile and ends it as it ends every profile refusal, with✗ <message>and exit 1). Acatalog noveltyskill directory whose name is not UTF-8 now gets an output directory named from the surrogate-passed hash, andnovelty.mdshows the name with?.The 16
strictsites keep their behaviour exactly.catalog novelty'snovelty.md(labelled "handled" in the study of the 93 sites) isreplacerather thanstrict: its caller turns the raise into an error row after the analysis is paid for, which is the failure #1617 asks to remove.Open PRs: #1559 rewrites the trace writes of
run_reach(repos/reach.pylines 561 and 563 here) and adds a field toparse_profile; #1582 touchesharness_profile.pyandreach.py, andbatch_ledger.pylines 111 and 113 are sites here. The edits are the keyword on the line as it stands, so a rebase keeps both changes. Any open PR that adds a new strict encode or write fails the new step once this merges: that is the check working.Testing
On a Linux review host, at
9d6d010b(onmain4deffc0):make ci-local: 39 passed, 0 failed, 16 skipped (docker and network jobs, jobs that do not run on a PR,semgrep,micromamba, the other Python legs and the fuzz run). 35929 tests passed, 28 skipped. The new step's row:pass 1s ci.yml:static / Encoding error handlers.mainand pass here:tests/test_encoding_surrogate_commands.pyis7 failed, 1 passedonmain's code (first failure:UnicodeEncodeError: 'utf-8' codec can't encode character '\ud800'atcommands/novelty_retest.py; therepo runtest reaches the missing--binand prints✗ harness executable ... not foundinstead of the profile refusal; the one that passes pins that a valid profile still loads), and the new test oftests/test_catalog_novelty.pyfails atcatalog_novelty.py:187.tests/test_check_encoding_errors.py(with the script copied ontomain's code) is1 failed, 41 passed: the check of the package at HEAD fails on the unswept sites.strict,replaceorignore, the profile refusal dropped (also failing therepo runtest) or made ASCII-only, the check's UTF-8 names, handler argument, write modes, layouts, walk, order and exit code) each fail a test.*or**as unknown again fails the four literal-pack cases oftests/test_check_encoding_errors.py; checkingconfig_fileswithout joining pairs, and writing them without joining, each failtest_a_profile_config_file_with_a_surrogate_pair_loads_and_is_written_as_the_character;errors="strict"in the index-size fallback failstest_index_size_without_the_file_counts_a_lone_surrogate_as_one_bytewith aUnicodeEncodeError.indent=2to3atcommands/novelty_run.py:165, a line the sweep only added a keyword to.fix/runs-export-unlistableonmain4deffc0:pytest -n auto35924 passed, 28 skipped,mypyclean,ruff checkclean.semgrep, and Windows (the path assertions oftests/test_check_encoding_errors.pyare built from the script's own_shownand split from the right; not run with a drive prefix).Open questions
None.
Revert
A plain revert restores the five tracebacks (exit 5), drops the check and its CI step, and leaves the sites without a named handler. Nothing is stored: the digests, keys and directory names of valid text are the same before and after, and a directory written for a name that is not UTF-8 stays on disk under its name.
Checklist
pytestpassesruff,mypy, and schema checks passAGENTS.mdraise/exit and test names the doc, contract or issue it traces to.github/.licenserc.yaml)