Skip to content

fix: defeat a forced-colour environment in every gh reader - #1254

Merged
max-sixty merged 3 commits into
mainfrom
fix/notifications-precheck-forced-color
Sep 16, 2026
Merged

max-sixty merged 3 commits into
mainfrom
fix/notifications-precheck-forced-color

Conversation

@tend-agent

@tend-agent tend-agent commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

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

The pre-check parses every gh response as JSON. gh ranks a forced colour
setting above NO_COLOR and colorizes even a piped response, so a job whose
environment carries CLICOLOR_FORCE=1 gets ANSI codes inside each body. Both
readers catch the decode error and fall back to zero, so the poll reports no
work every cycle behind a green job and a ::warning::.

Set NO_COLOR=1 and CLICOLOR_FORCE=0 in the child's environment, matching
github_cli.run and refresh_consumers.github_json.

@tend-agent tend-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The diagnosis holds and the fix is the right one — gh does rank a forced colour setting above NO_COLOR, and a pre-check that silently reports "no work" every cycle is the worst shape this failure could take. Two things about the scope it landed in.

The guard covers one of three copies of the same helper. generator/src/tend/templates/mention_verify.py has the identical gh() — same inline-rendered pre-check, same subprocess.run(["gh", *args], ...) inheriting the job environment, same JSON parse downstream. Its dispatch readers swallow the decode error the same way the notifications ones do: except (subprocess.CalledProcessError, json.JSONDecodeError) around the reviews/{item_id} and pulls/comments/{item_id} fetches, each falling through to verdict(False). So under the same environment a relayed review dispatch prints review N not found on PR M — skipping and the mention goes unanswered, on a green job — the notifications failure mode exactly, on the other pre-check. shared/steps/_common.py's gh() is the third; its exposure is louder, because security_preflight's first read (gh_json("api", f"repos/{repo}")["default_branch"]) is deliberately left to raise, so the gate goes red rather than quiet. Still the same two lines.

The count assertion in the new test passes without the fix. FAKE_GH_NOTIFICATIONS's colour override wraps emit, but the api:notifications\?* branch never calls it — it writes its pages straight out with echo '[]' and printf '%s\n' "$pages" | jq -c '.[] | [.]'. Revert the env=env line and the test fails on conflict_count, not on count: only the _conflicts reader is actually pinned, and a later change that narrowed the guard to that one call site would keep the test green. That also makes the PR body's "Without the fix it reports count=0, conflict_count=0" untrue of this fake.

I'm pushing both to this branch: the guard on the two remaining copies with a forced-colour regression test for the mention gate, and the fake reworked so every body — --paginate pages included — goes through one colorize helper, which makes the count assertion fail without the fix. The PR title and description will move to match the widened scope.

The mention gate and the shared composite-action transport run the same
unguarded `subprocess.run(["gh", ...])` the notifications pre-check did, so
a job environment carrying `CLICOLOR_FORCE=1` puts ANSI codes inside their
JSON bodies too. `mention_verify` is the matching silent failure: its
dispatch readers catch the decode error and fall through to
`verdict(False)`, so a relayed review goes unanswered on a green job.
`_common.gh` fails loudly instead — `security_preflight`'s first read is
left to raise — but wants the same two lines.

The notifications regression test only pinned `_conflicts`: the fake's
`--paginate` branch wrote its pages directly rather than through `emit`,
which was the only function the colour override wrapped, so the `count`
assertion passed with the fix reverted. Both fakes now route every body
through one `colorize` helper, and each pre-check has a forced-colour test
that fails without its guard.
@tend-agent tend-agent changed the title fix(notifications): defeat a forced-colour environment in the pre-check fix: defeat a forced-colour job environment in every gh reader Sep 16, 2026

@tend-agent tend-agent left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Two more gh readers have the same gap, and one of them fails worse than the two this started from.

generator/src/tend/checks.py's _gh turns the security audit green without reading it. check_branch_protection confirms .protected through a scalar --jq, which real gh leaves unpainted, and then reads repos/{repo}/rules/branches/{branch} and .../protection as plain JSON, which it does paint. Both decode handlers treat an unparsable body as "can't read the details, the branch is protected, assume OK" — except json.JSONDecodeError: return CheckResult(name, True, f"Branch '{branch}' is protected"). So under a forced-colour environment tend check reports branch-protection:main as passing without ever verifying that the bot cannot merge its own PRs, which docs/security-model.md calls the primary security boundary. That is a false green on a security check rather than a poll reporting zero, so it is the sharpest instance of this bug in the repo. Confirmed by running the real _gh against a gh stand-in that paints JSON bodies and leaves scalar --jq results alone: passed=True, message Branch 'main' is protected — the assume-OK fallback, not (requires reviews).

install_codex_subscription_auth._verify_secrets parses gh secret list --json name from an inherited environment, so a painted body aborts provisioning with an uncaught JSONDecodeError. Loud rather than silent, same two lines — the same reasoning the PR already applied to shared/steps/_common.py.

The two fakes' colorize/emit are byte-identical. They belong in tests/__init__.py's GH_PREAMBLE, which already owns emit: one definition instead of two, and every other fake gets the same realism for free. It stays inert unless a test sets CLICOLOR_FORCE=1, so no existing fake changes behaviour.

One boundary worth recording, since it decides how far this should widen: the gh api --paginate … --jq '… | .id' reaction-cleanup steps in the generated workflows need no guard. gh paints a JSON object or array but leaves a scalar --jq result alone — --jq .default_branch prints main plain while --jq '{name}' comes back wrapped — so those steps and check_branch_protection's own .protected read are genuinely unaffected. With checks.py and the install script guarded, every gh reader in the repo that parses a response is covered, and the title's "every gh reader" becomes true.

I'm pushing all three to this branch, with a regression test on check_branch_protection that fails with the guard reverted (it returns the assume-OK message), and moving the title and description onto the widened scope.

`generator/src/tend/checks.py`'s `_gh` is the sharpest instance of this bug
in the repo: `check_branch_protection` reads the rules and protection
payloads as plain JSON, which `gh` paints, and both decode handlers fall
through to `CheckResult(name, True, "Branch '<b>' is protected")`. So under
a forced-colour environment `tend check` reports the primary security
boundary as verified without having read it. The new test runs the real
`_gh` against a `gh` stand-in that paints JSON bodies and leaves scalar
`--jq` results alone — the rule real `gh` follows — and fails with the guard
reverted, on the assume-OK message rather than `(requires reviews)`.

`install_codex_subscription_auth._verify_secrets` parses `gh secret list
--json name` from an inherited environment; a painted body aborts
provisioning with an uncaught JSONDecodeError. Loud rather than silent,
same two lines.

The two fakes' `colorize`/`emit` were byte-identical, so they move to
`tests/__init__.py`'s `GH_PREAMBLE`, which already owned `emit`. Inert
unless a test sets `CLICOLOR_FORCE=1`, so no existing fake changes.
@tend-agent tend-agent changed the title fix: defeat a forced-colour job environment in every gh reader fix: defeat a forced-colour environment in every gh reader Sep 16, 2026
@max-sixty
max-sixty merged commit 1901e12 into main Sep 16, 2026
11 checks passed
@max-sixty
max-sixty deleted the fix/notifications-precheck-forced-color branch September 16, 2026 17:12
@tend-agent tend-agent mentioned this pull request Sep 16, 2026
max-sixty added a commit that referenced this pull request Sep 16, 2026
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Sep 16, 2026
Bumps the generator to 0.2.9, syncs `uv.lock`, and adds the `## 0.2.9`
changelog section that `pypi-release.yaml` publishes as the GitHub
Release notes.

The release covers 7 commits since 0.2.8. #1254 sets `NO_COLOR=1` and
`CLICOLOR_FORCE=0` on tend's Python `gh` readers, including the
pre-check scripts inlined into the generated `tend-notifications` and
`tend-mention` workflows, so an inherited forced colour setting no
longer breaks their JSON parsing. #1259 has a session review its own
diff before the push that opens a PR or pushes a fix to one under
review. #1253 makes `running-in-ci`'s references index a table keyed by
action, so PR and issue creation reads `posting.md`. #1258 adds a
comment to the generated `tend-mention-relay.yaml`. No config format
changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> _This was written by Claude Code on behalf of max-sixty_

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
max-sixty added a commit that referenced this pull request Sep 16, 2026
Deploys the 0.2.9 release to tend's own workflows: `uvx [email protected] init`
regenerates the nine `tend-*.yaml` files onto
`max-sixty/tend/[email protected]`, and the hand-maintained
`review-reviewers.yaml` is restamped onto the same ref.

Beyond the version stamp, the regenerated `tend-notifications.yaml` and
`tend-mention.yaml` pick up the `NO_COLOR=1` / `CLICOLOR_FORCE=0` guard
from #1254 in their inline pre-check scripts, and
`tend-mention-relay.yaml` gains the comment from #1258.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

> _This was written by Claude Code on behalf of max-sixty_

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
max-sixty pushed a commit to max-sixty/worktrunk that referenced this pull request Sep 17, 2026
Regenerates the `tend-*.yaml` workflows with tend 0.2.9 (from 0.2.8) and
moves the
`max-sixty/tend/claude` action pin with them.

Two changes reach the generated files:

- **`gh` JSON reads are no longer corrupted by a forced-colour
environment.** The
preflight scripts in `tend-mention` and `tend-notifications` parse `gh
--json`
output; when the job environment forces colour, `gh` writes ANSI codes
into that
  piped response and the parse sees them. Both readers now run `gh` with
`NO_COLOR=1` and `CLICOLOR_FORCE=0` — `gh` ranks a forced value above
`NO_COLOR`,
  so the second variable is the one that actually settles it
  (max-sixty/tend#1254).
- **`tend-mention-relay` records why it carries no pause check.**
Comment only: the
relay job boots no agent, and `TEND_ENABLED=false` already skips the
jobs that do,
  so a paused repo relays an event that then reaches nothing
  (max-sixty/tend#1258).

The action bump also picks up the bundled-skill changes at that ref,
which change
how the bot behaves rather than what the workflow files say: it now
reviews a change
before the push that opens or fixes a PR (max-sixty/tend#1259), and the
`running-in-ci` references index is keyed by when to read each file
(max-sixty/tend#1253).

Release mechanics and tend-internal test/doc fixes in the range are
omitted.

Full comparison: max-sixty/tend@0.2.8...0.2.9

`.config/tend.yaml` pins no model, so nothing there needed updating.

Co-authored-by: worktrunk-bot <[email protected]>
max-sixty pushed a commit to max-sixty/leaf that referenced this pull request Sep 17, 2026
Regenerates the tend workflows from 0.2.8 to 0.2.9 and moves the
`max-sixty/tend/claude`
action pin with them.

The change adopters will notice is the forced-colour fix in the
preflight readers
(max-sixty/tend#1254). `tend-mention` and `tend-notifications` each
embed a small Python
`gh` wrapper whose `--json` output is parsed before the agent boots.
`gh` colourizes a
piped JSON response when the job environment forces colour, and those
ANSI codes landed
inside the body the wrapper hands to `json.loads`, failing the preflight
and stranding the
trigger. Both wrappers now run `gh` with `NO_COLOR=1` and
`CLICOLOR_FORCE=0`; the second is
what actually defeats it, since `gh` ranks a forced value above
`NO_COLOR`.

The bundled skills carry one behaviour change with it: the bot now
reviews its own change
before the push that opens or fixes a PR (max-sixty/tend#1259), so a PR
it opens should
arrive having had a pass over its own diff.

`tend-mention-relay` gains comment-only text recording why the relay job
carries no
`TEND_ENABLED` check — it boots no agent, and a paused repo stops at
`tend-mention`'s
`verify` regardless. No behaviour change there.

Support: <max-sixty/tend@0.2.8...0.2.9>

<details><summary>Upstream commits in this range</summary>

The remaining entries are tend-internal and produce no adopter-visible
effect: the
references-index rule and its scoping (max-sixty/tend#1253), an
integration-test count
correction (max-sixty/tend#1255), and release mechanics
(max-sixty/tend#1251, max-sixty/tend#1260).

</details>

Co-authored-by: leaf-agent <[email protected]>
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.

2 participants