Skip to content

fix(token-report): set CLICOLOR_FORCE=0 to defeat forced colors - #220

Merged
max-sixty merged 2 commits into
mainfrom
hourly/review-24235684411-ansi
Apr 10, 2026
Merged

max-sixty merged 2 commits into
mainfrom
hourly/review-24235684411-ansi

Conversation

@tend-agent

@tend-agent tend-agent commented Apr 10, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

token-report.sh was exporting NO_COLOR=1 but gh run list --json still emitted ANSI color codes into its JSON in PRQL/prql's CI, breaking the downstream jq pipeline with:

jq: parse error: Invalid numeric literal at line 1, column 5

…making the token report silently empty.

Root cause (updated)

PRQL/prql's .github/actions/tend-setup/action.yaml sets CLICOLOR_FORCE=1 in $GITHUB_ENV to force cargo/clippy color output. gh's iostreams logic treats CLICOLOR_FORCE as higher priority than NO_COLOR, so NO_COLOR=1 alone cannot suppress colors when something upstream sets CLICOLOR_FORCE=1.

Verified against gh 2.89.0 in tend's own env:

env output
CLICOLOR_FORCE=1 gh run list --json ... \e[1;37m[\e[m\e[1;37m]\e[m
CLICOLOR_FORCE=1 NO_COLOR=1 gh run list --json ... \e[1;37m[\e[m... (still colored)
CLICOLOR_FORCE=1 CLICOLOR=0 gh run list --json ... \e[1;37m[\e[m... (still colored)
CLICOLOR_FORCE=0 gh run list --json ... [{...}] (clean)

Fix

Add export CLICOLOR_FORCE=0 next to the existing export NO_COLOR=1 at the top of token-report.sh. This overrides an inherited CLICOLOR_FORCE=1 from the ambient env and lets gh fall back to its normal non-TTY behavior (no colors).

Verification

  • Baseline repro: CLICOLOR_FORCE=1 NO_COLOR=1 gh run list --json databaseId --limit 2 → emits \e[1;37m[\e[m..., confirmed against tend's CI gh 2.89.0.
  • Fix repro: CLICOLOR_FORCE=1 NO_COLOR=1 bash -c 'export CLICOLOR_FORCE=0; gh run list --json databaseId --limit 2' → emits [{"databaseId":...}] cleanly.
  • End-to-end: Ran CLICOLOR_FORCE=1 ./plugins/tend-ci-runner/scripts/token-report.sh 2 → script parses successfully, downloads artifacts, outputs valid JSON.
  • Tests: cd generator && uv run pytest → 147/147 passed.
  • Syntax: bash -n passes.

Scope

Only token-report.sh, since that's the script the bug demonstrably broke. list-recent-runs.sh has the same gh run list --json pattern but is only invoked from the review-reviewers skill which runs on tend itself (where no upstream sets CLICOLOR_FORCE=1), so I'm leaving it alone rather than making speculative changes.

In PRQL/prql's CI environment, `gh run list --json` emits ANSI color
escape sequences even with NO_COLOR=1 (which the script sets) and even
when piped to a non-TTY. The colored output (`\e[1;37m[\e[m\e[1;37m]\e[m`
instead of `[]`) breaks the downstream jq parse, and with `set -euo
pipefail` the script exits with `jq: parse error: Invalid numeric
literal at line 1, column 5` — producing an empty token report.

Reproduced in review-runs session 24233554789 (PRQL/prql, gh 2.89.0).
The bot verified that NO_COLOR=1, CLICOLOR=0, GH_FORCE_TTY="" and
GH_NO_COLOR=1 all fail to suppress the codes — something in that
environment forces them on. The same gh version in tend's CI does not
exhibit the issue, so the root cause is environmental.

Fix: pipe through a small `strip_ansi` sed helper. It's a no-op when no
codes are present (tend CI) and strips them when they are (PRQL/prql),
so the script is robust across environments without needing to diagnose
the originating env var.

Verified by mocking gh to emit the exact colored output the bot saw:
before the fix, the pipeline fails with the same jq parse error; after
the fix, the JSON parses correctly and the script processes runs
normally.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@tend-agent tend-agent added the claude-behavior Behavioral issues found by review-reviewers label Apr 10, 2026
@max-sixty

Copy link
Copy Markdown
Owner

can we set env vars to disable color rather than stripping chars?

PRQL/prql's .github/actions/tend-setup/action.yaml sets CLICOLOR_FORCE=1
via $GITHUB_ENV to force cargo/clippy colors. gh treats CLICOLOR_FORCE as
higher priority than NO_COLOR, so NO_COLOR=1 alone didn't suppress colors
in --json output. Setting CLICOLOR_FORCE=0 in the script overrides the
inherited value and addresses the root cause directly — simpler and more
transparent than the sed-based strip_ansi approach.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@tend-agent tend-agent changed the title fix(token-report): strip ANSI codes from gh run list output fix(token-report): set CLICOLOR_FORCE=0 to defeat forced colors Apr 10, 2026
@tend-agent

Copy link
Copy Markdown
Collaborator Author

Yes — pushed 2d26285 replacing the strip with export CLICOLOR_FORCE=0.

The reason the existing export NO_COLOR=1 didn't work: PRQL/prql's .github/actions/tend-setup/action.yaml sets CLICOLOR_FORCE=1 in $GITHUB_ENV (to force cargo/clippy colors), and gh's iostreams treats CLICOLOR_FORCE as higher priority than NO_COLOR. I missed that in the original session — only tried NO_COLOR / CLICOLOR=0 / GH_FORCE_TTY / GH_NO_COLOR, never CLICOLOR_FORCE.

Verified against gh 2.89.0 in this env by simulating PRQL/prql's setup:

env first bytes of gh run list --json
CLICOLOR_FORCE=1 \e[1;37m[\e[m\e[1;37m]\e[m (colored)
CLICOLOR_FORCE=1 NO_COLOR=1 \e[1;37m[\e[m... (still colored)
CLICOLOR_FORCE=1 CLICOLOR=0 \e[1;37m[\e[m... (still colored)
CLICOLOR_FORCE=1 + script sets CLICOLOR_FORCE=0 [{"databaseId":... (clean)

End-to-end: CLICOLOR_FORCE=1 ./plugins/tend-ci-runner/scripts/token-report.sh 2 now parses successfully and exits 0. Generator tests still pass (147/147).

@max-sixty
max-sixty merged commit 553d1d4 into main Apr 10, 2026
3 checks passed
@max-sixty
max-sixty deleted the hourly/review-24235684411-ansi branch April 10, 2026 13:22
max-sixty pushed a commit that referenced this pull request Sep 16, 2026
Every `gh` reader in the repo's Python parses the response as JSON, and
each one ran `gh` on the environment it inherited. `gh` ranks a forced
colour setting above `NO_COLOR` and colorizes even when its output is a
pipe rather than a terminal — so under `CLICOLOR_FORCE=1`, a body
arrives wrapped in ANSI escapes and the parse fails. Each reader treats
a decode error as a transient blip, so under a forced colour setting the
fallback is permanent, and every one of them degrades quietly:

- `notifications_check.py` — `_notifications` and `_conflicts` catch
`json.JSONDecodeError` and return `0`. Every cycle reports no
notifications and no conflicted bot PRs, so the bot silently stops
draining its inbox and stops resolving conflicts on its own PRs.
- `mention_verify.py` — the relayed-dispatch readers catch it and fall
through to `verdict(False)`, printing `review N not found on PR M —
skipping`. Mentions on review events go unanswered.
- `checks.py` — `check_branch_protection` reads `rules/branches/<b>` and
`branches/<b>/protection` as plain JSON, and both decode handlers fall
through to `CheckResult(name, True, f"Branch '{branch}' is protected")`.
`tend check` reports the branch as protected without ever verifying that
the bot cannot merge its own PRs — the check
[`docs/security-model.md`](https://github.com/max-sixty/tend/blob/main/docs/security-model.md)
calls the primary security boundary. This is the worst of the set: a
false green on a security check, not a poll reporting zero.
- `shared/steps/_common.py` — `security_preflight`'s first read is
deliberately left to raise, so the gate goes red rather than quiet. It
gets the same guard anyway.
- `install_codex_subscription_auth._verify_secrets` — parses `gh secret
list --json name`; a painted body aborts provisioning with an uncaught
`JSONDecodeError`. Loud, same two lines.

The fix sets `NO_COLOR=1` and `CLICOLOR_FORCE=0` in the child's
environment. `CLICOLOR_FORCE=0` is the part that matters: `gh` treats a
forced value as higher priority than `NO_COLOR`, so `NO_COLOR` alone
loses to an inherited `CLICOLOR_FORCE=1`. This is the same two-line
guard `github_cli.run` and `refresh_consumers.github_json` already
carry; the readers here each kept their own `gh` — the two templates
because they're rendered inline into the generated workflow rather than
imported, the rest because they're separate deployment units that can't
share a helper.

`gh` paints a JSON object or array but leaves a scalar `--jq` result
alone. Checked against the `gh` on the runner (2.100.0) rather than
inferred: under `CLICOLOR_FORCE=1`, `--jq '{name}'` comes back
ANSI-wrapped and pretty-printed across lines while `--jq
.default_branch` prints `main` plain, and adding `NO_COLOR=1
CLICOLOR_FORCE=0` restores the compact single line. So the `gh api
--paginate … --jq '… | .id'` reaction-cleanup steps in the generated
workflows, and `check_branch_protection`'s own `.protected` read, are
genuinely unaffected and stay as they are.

One shell reader does parse a shape `gh` would paint:
[`.github/workflows/integration-secrets.yaml`](https://github.com/max-sixty/tend/blob/main/.github/workflows/integration-secrets.yaml)
slurps the integration repo's `deployment-branch-policies` with no
`--jq` and pipes the array into `jq`. It stays unguarded — that workflow
is hand-maintained here and its steps set their own `env:`, so no
adopter config and no shell reaches it.

Reachable from an adopter's own configuration: a
`workflow_extra`/`jobs.*` `env:` override, or a `setup:` step that
writes `CLICOLOR_FORCE=1` into `$GITHUB_ENV` in a workflow where setup
precedes the pre-check — and for `tend check` and the install script,
simply a shell that exports it. Forcing colour for `cargo`/`clippy`
output is a normal reason to set it, which is how the same interaction
previously broke `token-report.sh` (#220).

Each of the three gates that fails quietly gets a regression test that
drives it with `CLICOLOR_FORCE=1` against a fake `gh` mimicking the
precedence, and asserts it still does its job. The `colorize` helper
that models the precedence lives in `tests/__init__.py`'s `GH_PREAMBLE`
beside the `emit` it wraps, so one definition serves every fake and
stays inert unless a test sets `CLICOLOR_FORCE=1`. Each test fails with
its guard reverted: the notifications one on `count`, the mention one on
the verdict, and the `checks.py` one on the assume-OK message rather
than `(requires reviews)`.

The regenerated `_regtest_outputs` snapshots are the generated
notifications and mention workflows, which embed these scripts inline.
`.github/workflows/tend-notifications.yaml` and `tend-mention.yaml` are
deliberately untouched: tend's own workflows track the published release
and pick this up at the next one.

---------

Co-authored-by: tend-agent <[email protected]>
Co-authored-by: tend-agent <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude-behavior Behavioral issues found by review-reviewers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants