Skip to content

tooling: gh CLI --json/--jq body fetch + bash redirect silently mojibake-corrupts UTF-8 em-dashes on write-back (durable tracker content at risk) #1037

Closed

Description

@kyle-sexton

This was generated by AI during triage (lane: triage-decide, self-observation).

Lane: triage-decide. Category: operational (silent data-fidelity defect in durable tracker content).

Triggering example

Fetching #657's body via gh issue view 657 --json body --jq '.body' > file.md (Windows/Git-Bash, LANG=en_US.UTF-8, gh 2.95.0, git 2.54.0.windows.1) and later writing it back with gh issue edit 657 --body-file <path> (or the REST equivalent gh api ... -X PATCH -F body=@<path>) round-trips real UTF-8 em-dashes (—, U+2014) into the classic double-encoding mojibake — (U+00E2 U+20AC U+201D) for a subset of occurrences.

Discovered mid-cycle 2 (2026-07-22) while checking off two more #657 sweep lines: a live fetch of #657 via gh api repos/.../issues/657 + Python json.load (a decode path proven correct) showed the live GitHub-stored body already contained 51 mojibake instances alongside 80 correctly-encoded em-dashes — i.e. the corruption is not a local-display artifact, it is byte-for-byte wrong content actually stored on GitHub. Root cause of the ORIGINAL corruption is not fully isolated (could be gh/jq's interaction with Git-Bash's redirect codepage on this machine, or could pre-date this session from another lane's write) — but the round-trip path this repo's own standing rules mandate (--body-file for any comment/body containing backticks or $, itself a fix for a different corruption bug — see #657's own line ~370 finding) is a proven vector for this second, distinct corruption class on at least this machine: gh issue view --json body --jq '.body' > file (bash > redirect) is suspect; gh api ... -q '.body' > file reproduces it too. gh api <endpoint> > file.json followed by Python json.load() does NOT reproduce it (correct decode) — meaning the corruption is introduced somewhere in --json/--jq/bash-redirect text handling, not in the raw HTTP/JSON layer itself.

Observed vs expected

  • Observed: targeted substring — present in file content after a gh ... --json body --jq '.body' > file fetch, and (separately, already live on GitHub before this session touched it) present in lane sweep: triage — week of 7/20 (rolling paper-cut batch) #657's actual stored body — meaning some earlier write already pushed this corruption to GitHub.
  • Expected: every character round-trips byte-identical through gh CLI fetch → local file → gh CLI write-back, with no silent transcoding.

Impact

Silent — no error, no warning, both gh issue view --json and gh issue edit --body-file exit 0. Any lane (not just this one) that fetches a comment/issue body via --json ... --jq '.body' > file, edits it, and writes it back via --body-file risks corrupting every em-dash (and potentially other non-ASCII punctuation not yet observed in this specific body — only em-dash appeared in #657's content, so other characters are unconfirmed, not ruled out) in the untouched portions of that body. This is exactly the class of "silent-wrong output" this lane's own standing rules single out (durable-audit-trail fidelity: #502's fused telemetry comment and #657's sweep checklist are both edited via this exact pattern every cycle).

Fix applied this cycle (workaround, not a root-cause fix)

Fetched #657 via gh api <endpoint> > raw.json (no --jq), loaded with Python json.load() (confirmed-correct UTF-8 decode), replaced the literal 3-char mojibake substring — with —, wrote back with encoding='utf-8', pushed via gh api ... -X PATCH -F body=@<path> (capital -F, not -f, per the adjacent #943-class gotcha this lane already tracks). Verified 0 mojibake / 131 correct em-dashes post-fix. This workaround does not identify or fix the root cause in gh's --json/--jq/bash-redirect path.

Category

bug (tooling/environment) — silent data corruption on a specific character class through the exact write pattern this lane's and other lanes' standing rules mandate for all non-trivial comment/body content.

Suggested fix direction (not prescriptive — needs investigation)

Isolate whether the corruption originates in gh's own stdout encoding, --jq's UTF-8 handling, or Git-Bash's > redirect codepage on Windows; once isolated, either avoid --json ... --jq '.body' > file in favor of gh api <endpoint> > file.json + a JSON-aware read (proven clean in this incident), or find the correct gh/environment flag/setting that fixes the redirect path directly. Any lane's standing rules that prescribe the --body-file-from-fetched-body pattern should link this issue until resolved.

Work-class: C3 (bug-fix-shaped) — attended triage 2026-07-23, operator-ratified. 🤖

Activity

  1. added
    needs-triage
    on Jul 22, 2026
  2. added theissue type on Jul 22, 2026
  3. kyle-sexton commented on Jul 22, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI during triage.

    Lane: triage-decide (self-observation follow-up). Investigation outcome: the filed hypothesis is disproven; the real corruption vector is isolated.

    TL;DR

    The fetch path this issue accuses (gh issue view --json body --jq '.body' > file, gh api ... --jq '.body' > file, and the piped variant) does not corrupt em-dashes on this machine. It is byte-faithful, locale-independent, and byte-identical to the "proven clean" JSON-aware decode. The real — mojibake comes from decoding a fetched UTF-8 body with a tool that defaults to Windows cp1252 — on this box that is Python's open() / json.load(open(path)) (text mode) and PowerShell's Get-Content without -Encoding utf8. The corruption is a downstream-decode defect in an ad-hoc read step, not in gh / jq / the bash > redirect.

    Repro 1 — fetch path is byte-faithful (disproves the filed hypothesis)

    #657 is currently clean (the prior cycle's write-back workaround fixed it): 134 correct em-dashes, 0 mojibake. Fetched its body four ways and compared bytes:

    path command len correct — mojibake — md5
    A gh issue view 657 --json body --jq '.body' > f 63527 134 0 d3821b…c565c3
    B gh api repos/.../issues/657 --jq '.body' > f 63527 134 0 d3821b…c565c3
    D gh issue view 657 --json body -q '.body' | python -c "sys.stdin.buffer…" 63527 134 0 d3821b…c565c3
    C gh api repos/.../issues/657 > raw.json + json.loads(open(rb).read()) 63526 134 0 (control)

    A, B, D are md5-identical. A == C-body + b'\n' exactly (the 1-byte diff is jq -r's trailing newline). So --json --jq > file equals the JSON-aware decode. Locale made zero difference: LANG=C.UTF-8 and LANG=C both produced files byte-identical to A. jq (pipe and file) and the bash redirect, tested in isolation on a controlled em-dash, each preserved \xe2\x80\x94 exactly. No invocation shape reproduced fetch-side corruption.

    Repro 2 — the actual mechanism (reproduced to the exact documented byte sequence)

    locale.getpreferredencoding() = cp1252 on this machine (Python 3.14, LANG=en_US.UTF-8 in Git-Bash does not change Python's Windows default). Reading a clean UTF-8 body file with Python's default open() (no encoding=):

    open('utf8_body.md','wb').write(b'em\xe2\x80\x94dash')   # clean UTF-8, as any fetch produces
    open('utf8_body.md').read()          # default cp1252 decode -> 'em—dash'
    ...encode('utf-8')                   # -> bytes: 65 6d c3a2 e282ac e2809d 64 61 73 68
    

    That output byte sequence c3a2 e282ac e2809d is exactly the — (U+00E2 U+20AC U+201D) mojibake this issue documents. With encoding='utf-8' the same round-trip is clean (0 mojibake, 1 correct em-dash). The mechanism is a UTF-8 stream read as cp1252 and re-encoded as UTF-8 — a read-side decode error, which is why it is invisible to gh, jq, and the redirect (all of which only ever move bytes).

    Why the prior cycle misdiagnosed it

    The prior cycle observed 51 mojibake in a live fetch of #657 and attributed it to the fetch path — but it never compared the fetch against a known-clean baseline. A faithful fetch of already-corrupt stored bytes shows mojibake too. Their evidence is equally consistent with (and, given Repro 1, is actually) faithful reproduction of content some earlier ad-hoc write had already corrupted via the cp1252 read above. Their "workaround" (gh api > file.json + json.loads(bytes) + encoding='utf-8' write) worked not because it avoided the fetch path, but because json.loads() on bytes auto-detects UTF-8 per RFC 8259 and the write forced UTF-8 — i.e. it happened to use a correct decode.

    Reproduction limitation (stated up front)

    The prior cycle's exact "51 mojibake in a fetch" observation cannot be re-run: #657 is now clean. The disproof is therefore indirect but airtight — (a) the fetch is byte-faithful on clean content across all four shapes; (b) the cp1252 read reproduces the exact documented byte pattern. I could not pin the specific original corrupting command, because it was an in-session ad-hoc read (see next section), not a committed code path — so it leaves no artifact.

    Is the footgun instantiated in this repo's code? (grep result)

    Grepped the committed body read-modify-write surface. The GitHub tracker adapter (plugins/work-items/tools/work-item-tracker/adapters/github/*.sh) is entirely bash using --body-file - (stdin) — byte-faithful, no cp1252 layer, not a vector. The only json.load(open()) hits are eval assertions in docs/topics/babysit-prs-migration/PLAN.md over local ASCII files (not body content). The Get-Content-without--Encoding hits are all machine-health cache/catalog reads, unrelated to issue bodies. No committed code instantiates the footgun — the original #657 corruption came from a prior lane cycle's ad-hoc in-session Python patch of the body (exactly the python -c "…open(path)…" a triage agent types on the fly), which is unfixable-in-code and only preventable by convention.

    Recommended fix direction

    The issue's own "Suggested fix direction" (avoid --json --jq > file; prefer gh api > file.json + JSON-aware read) is built on the wrong diagnosis and should be corrected, not adopted: avoiding the fetch shape fixes nothing, and json.load(open(path)) (text mode) is itself the footgun on Windows. The correct convention is transport-agnostic:

    Any time a fetched issue/comment/PR body is decoded before write-back, force UTF-8 regardless of fetch shape: read bytes and .decode('utf-8') (or open(..., encoding='utf-8'); PowerShell Get-Content -Encoding utf8; or set PYTHONUTF8=1). Never rely on the platform default decode — it is cp1252 on Windows and silently double-encodes non-ASCII punctuation. The gh fetch itself (--json/--jq/gh api) and the bash > redirect are byte-faithful and are not the concern.

    Where it belongs: plugins/work-items/tools/work-item-tracker/adapters/github/README.md — the repo's authoritative gh-CLI mechanics reference (it already documents the --body-file WRITE identity, the -F vs -f #943-class gotcha, and read-modify-write). Add the UTF-8-decode note adjacent to the --body-file read-modify-write section (~lines 130-176), as a sibling to the existing gotchas. This is a documentation/convention change with no code fix required (no committed code is vulnerable), so it is low-risk and defaultable.

    Decision defaulted: document the transport-agnostic "force-UTF-8-on-decode" convention in plugins/work-items/tools/work-item-tracker/adapters/github/README.md (adjacent to the --body-file read-modify-write section) and correct this issue's misframed suggested-fix; no code fix needed — veto before merge

  4. added
    status: ready
    agent-ready
    needs-human
    and removed
    needs-triage
    on Jul 22, 2026
  5. kyle-sexton commented on Jul 24, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI on operator direction.

    Superseded — this item was already ratified and should not have been queued.

    The earlier first-drain ratification request on this issue was raised in error. The admission gate dispatches a C3 bug-fix-shaped item when it carries either its own recorded ratification or a completed first drain — and this issue already carried recorded ratification in its body from attended triage on 2026-07-23. The drain cycle applied the blanket first-drain rule without first reading bodies for an existing ratification, so it queued an item that was already admitted.

    Restored to the autonomous frontier: agent-ready re-applied, needs-human removed. No human action is required, and this issue no longer appears in the attended queue's ratify view.

  6. added
    agent-ready
    and removed
    agent-ready
    needs-human
    on Jul 24, 2026
  7. self-assigned this
    on Jul 25, 2026
  8. kyle-sexton commented on Jul 25, 2026

    @kyle-sexton
    MemberAuthor
    No description provided.
  9. kyle-sexton commented on Jul 25, 2026

    @kyle-sexton
    MemberAuthor

    This was generated by AI while working #1037.

    Spent scratch probe (round-trip encoding verification). Result folded into the PR; no content here.

  10. added 2 commits that reference this issue on Jul 25, 2026
    9d4e7fc
    17a18fb
  11. added 2 commits that reference this issue on Jul 25, 2026
    d8be922
    34edbed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

agent-readystatus: ready

Type

Fields

No fields configured for Bug.

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions