Repository navigation
fix(csv): keep colliding flattened CSV keys in distinct columns - #3870
Mystery01092000 wants to merge 2 commits into
Conversation
A literal dotted key such as "user.login" and the nested path user -> login both flattened to the column user.login, so one value was silently overwritten depending on map iteration order. Escape "\" and "." inside each key when building column names (user\.login for the literal key) so distinct source fields always map to distinct columns. Fixes github#3865
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the CSV field-name escaping changes.
No open issues 🎉
What changed in this PR
Fixes #3865 by preventing literal dotted keys from colliding with nested field paths in the GitHub MCP Server’s CSV output.
Changes:
- Escapes dots and backslashes in field names for both CSV columns and metadata.
- Limits whitespace normalization to fields whose unescaped leaf key is
body. - Adds regression tests and documents the escaping format.
| File | Description |
|---|---|
pkg/github/csv_output.go |
Escapes column-name components and checks leaf keys for body normalization. |
pkg/github/csv_output_test.go |
Tests dotted-key collisions, backslash escaping, metadata, and body normalization. |
docs/insiders-features.md |
Documents escaped field names in the CSV format. |
Review effort: Balanced (adaptive)
SoulmatelynchVFX
left a comment
There was a problem hiding this comment.
The escaping handles the reported case, but the injectivity claim in the csvColumnName comment ("distinct source fields always map to distinct columns") has one residual hole: the root position is signalled by prefix == "" (pkg/github/csv_output.go:329-331), so an empty-string key whose value is an object contributes no path segment at all and its children inherit the root path. {"": {"b": 2}, "b": 3} flattens to a single column b, two leaves collapse into one, and which value survives is again decided by Go map iteration order — the non-determinism #3865 is about. {"": {"": {"c": 1}}, "c": 2} behaves the same. A depth counter, or a separate hasPrefix flag, would close it, since "" is a legal JSON key.
How I checked: no Go toolchain on this machine, so I transcribed csvColumnName and the appendFlattenedCSVFields recursion into Python and ran 7 key sets — the reproduction from the issue, the backslash set from TestJSONTextToCSVEscapesBackslashesInKeys, and empty-key variants. The issue's row, the backslash set and the nested-empty-key case all map 1:1 (4 leaves to 4 columns for the backslash set); only the empty-key-with-object case collapses leaves, 2 leaves to 1 column.
Not covered: I did not run go test, and I did not check whether any GitHub REST response can actually carry an empty-string key, so I read this as a boundary of the encoding rather than live data loss, and not as a blocker for the issue. The isBodyKey narrowing matches the documented "body fields" wording, and the four new tests exercise the paths this change touches.
Written with AI assistance (agent), with the cases above reproduced locally.
The root was detected by prefix == "", so children of a "" key inherited the
root path: {"":{"b":2},"b":3} flattened to one column b. Track nesting
explicitly so every key adds a segment ({"":{"b":2}} yields .b).
|
Thanks, good catch. Fixed in 85277a0. The recursion now tracks nesting explicitly (
With every key escaped and every path segment present, a column name splits back into its path in exactly one way: split on unescaped
|
Summary
CSV output no longer drops data silently when a literal dotted field name, such as
"user.login", flattens to the same column as a nested field (user→login). Inside each key,.and\are now escaped when column names are built, so distinct source fields always get distinct columns. The result is deterministic.Why
appendFlattenedCSVFieldsjoined key paths with.without escaping. With this input:[{"user.login":"literal","user":{"login":"nested"}}]both fields wrote to
row["user.login"]. Go map iteration order decided which value survived, and the other was lost. The same applied to metadata preamble lines, which use the same flattening.Fixes #3865
What changed
csvColumnNameescapes each key before joining:\becomes\\and.becomes\.. A bare.in a column name is always a nesting separator, so the mapping from key paths to column names is injective. This holds for rows and metadata alike, so no collision is possible and nothing depends on iteration order.{"user":{"login":"nested"}}givesuser.login(unchanged).{"user.login":"literal"}givesuser\.login..or\are unchanged, so existing column names for normal GitHub API responses stay the same.bodynow checks the unescaped leaf key (key == "body") instead ofstrings.HasSuffix(column, ".body"). Nestedcomment.bodyis still normalized. A literal top-level key"x.body"(columnx\.body) is no longer mistaken for a nestedbodyfield.docs/insiders-features.md: documented the escaping in the CSV format section.pkg/github/csv_output_test.go:TestJSONTextToCSVKeepsCollidingFlattenedKeysDistinct: the reproduction from the issue, run 20 times. It asserts the two columnsuser.loginanduser\.loginand that both values survive.TestJSONTextToCSVEscapesBackslashesInKeys:a.b,a\.b,a\→b, anda→beach get their own column.TestJSONTextToCSVKeepsCollidingMetadataKeysDistinct: the same collision in the#metadata preamble.TestJSONTextToCSVNormalizesOnlyBodyLeafKeys:bodynormalization after the change.All four new tests fail on
mainand pass with this change.MCP impact
This changes the CSV text produced by
list_*tools when thecsv_outputflag is on (Insiders). Column names change only for keys that contain.or\. Tool schemas are unchanged, and the toolsnaps did not change.Prompts tested (tool changes only)
Covered by unit tests of the CSV conversion; no tool schema changed.
--features csv_output)Security / limits
Tool renaming
deprecated_tool_aliases.goLint & tests
./script/lint(rangolangci-lint runv2.14.0 andgofmt -s -l .in Docker)./script/test(rango test ./...in Docker withgolang:1.27.4)Without the fix (
csv_output.goreverted tomain, new tests kept):With the fix:
(24 CSV tests passed, 0 failed.)
Full suite (
go test -count=1 -json ./...): 9057 passed, 2 skipped, 0 failed.UPDATE_TOOLSNAPS=true go test ./...andscript/generate-docsproduced no changes.Docs
docs/insiders-features.mdCSV format sectionSkills