Skip to content

fix(csv): keep colliding flattened CSV keys in distinct columns - #3870

Open
Mystery01092000 wants to merge 2 commits into
github:mainfrom
Mystery01092000:fix/csv-output-flattened-key-collisions
Open

Mystery01092000 wants to merge 2 commits into
github:mainfrom
Mystery01092000:fix/csv-output-flattened-key-collisions

Conversation

@Mystery01092000

Copy link
Copy Markdown

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

appendFlattenedCSVFields joined 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

  • csvColumnName escapes 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"}} gives user.login (unchanged).
    • {"user.login":"literal"} gives user\.login.
    • Keys without . or \ are unchanged, so existing column names for normal GitHub API responses stay the same.
  • Whitespace normalization for body now checks the unescaped leaf key (key == "body") instead of strings.HasSuffix(column, ".body"). Nested comment.body is still normalized. A literal top-level key "x.body" (column x\.body) is no longer mistaken for a nested body field.
  • docs/insiders-features.md: documented the escaping in the CSV format section.
  • Tests in pkg/github/csv_output_test.go:
    • TestJSONTextToCSVKeepsCollidingFlattenedKeysDistinct: the reproduction from the issue, run 20 times. It asserts the two columns user.login and user\.login and that both values survive.
    • TestJSONTextToCSVEscapesBackslashesInKeys: a.b, a\.b, a\ → b, and a → b each get their own column.
    • TestJSONTextToCSVKeepsCollidingMetadataKeysDistinct: the same collision in the # metadata preamble.
    • TestJSONTextToCSVNormalizesOnlyBodyLeafKeys: body normalization after the change.

All four new tests fail on main and pass with this change.

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
  • New tool added

This changes the CSV text produced by list_* tools when the csv_output flag 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.

  • "List open issues in this repo" (with --features csv_output)

Security / limits

  • No security or limits impact
  • Auth / permissions considered
  • Data exposure, filtering, or token/size limits considered

Tool renaming

  • I am renaming tools as part of this PR (e.g. a part of a consolidation effort)
    • I have added the new tool aliases in deprecated_tool_aliases.go
  • I am not renaming tools as part of this PR

Lint & tests

  • Linted locally with ./script/lint (ran golangci-lint run v2.14.0 and gofmt -s -l . in Docker)
  • Tested locally with ./script/test (ran go test ./... in Docker with golang:1.27.4)

Without the fix (csv_output.go reverted to main, new tests kept):

--- FAIL: TestJSONTextToCSVKeepsCollidingFlattenedKeysDistinct (0.00s)
--- FAIL: TestJSONTextToCSVEscapesBackslashesInKeys (0.00s)
--- FAIL: TestJSONTextToCSVKeepsCollidingMetadataKeysDistinct (0.00s)
--- FAIL: TestJSONTextToCSVNormalizesOnlyBodyLeafKeys (0.00s)
FAIL	github.com/github/github-mcp-server/v2/pkg/github	0.020s

With the fix:

$ go test -count=1 -v -run CSV ./pkg/github/
...
--- PASS: TestJSONTextToCSVKeepsCollidingFlattenedKeysDistinct (0.00s)
--- PASS: TestJSONTextToCSVEscapesBackslashesInKeys (0.00s)
--- PASS: TestJSONTextToCSVKeepsCollidingMetadataKeysDistinct (0.00s)
--- PASS: TestJSONTextToCSVNormalizesOnlyBodyLeafKeys (0.00s)
ok  	github.com/github/github-mcp-server/v2/pkg/github	0.015s

(24 CSV tests passed, 0 failed.)

Full suite (go test -count=1 -json ./...): 9057 passed, 2 skipped, 0 failed.

$ golangci-lint run
0 issues.

UPDATE_TOOLSNAPS=true go test ./... and script/generate-docs produced no changes.

Docs

  • Not needed
  • Updated (README / docs / examples): docs/insiders-features.md CSV format section

Skills

  • Updated relevant skills or confirmed no bundled skill applies. No tool or toolset instructions changed.

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
@Mystery01092000
Mystery01092000 requested a review from a team as a code owner November 16, 2026 19:28
Copilot AI adaptive review requested due to automatic review settings November 16, 2026 19:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 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 SoulmatelynchVFX 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.

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).
@Mystery01092000

Copy link
Copy Markdown
Author

Thanks, good catch. "" is a legal key, and inferring the root from prefix == "" let a "" parent vanish from the path.

Fixed in 85277a0. The recursion now tracks nesting explicitly (appendNestedCSVFields(row, value, prefix, nested), and csvColumnName takes nested), so every key adds a segment, including an empty one:

  • {"": {"b": 2}, "b": 3} gives columns .b and b
  • {"": {"": {"c": 1}}, "c": 2} gives ..c and c

With every key escaped and every path segment present, a column name splits back into its path in exactly one way: split on unescaped ., then unescape each piece.

TestJSONTextToCSVKeepsEmptyParentKeysDistinct covers both of your cases, 20 iterations each so map order can't hide a collision. On the previous head it fails with "[b c]" should have 4 item(s), but has 2. go test ./... and golangci-lint v2.14.0 are clean.

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.

csv_output silently overwrites distinct fields when flattened keys collide

3 participants