Skip to content

fix(notifications): tolerate transient non-JSON gh api responses - #780

Merged
max-sixty merged 3 commits into
mainfrom
fix/issue-779
Jul 19, 2026
Merged

max-sixty merged 3 commits into
mainfrom
fix/issue-779

Conversation

@tend-agent

Copy link
Copy Markdown
Collaborator

Problem

The generated tend-notifications.yaml pre-check ("Check for unread notifications") opened with COUNT=$(gh api notifications --jq 'length') as its first command, running under the default bash -e. When GitHub transiently returns an HTML error page instead of JSON — a routine minutes-long API blip, sometimes even with a 200 status — gh's internal jq fails with invalid character '<' looking for beginning of value, gh exits non-zero, and set -e aborts the whole step. A scheduled janitor cycle becomes a red build over a transient upstream blip, and that notification-sweep cycle is skipped until the next schedule interval.

The three later bare gh api notifications re-fetches (Layers B/C/D) were untolerated too.

Solution

Fetch notifications once with a short retry, validate the body is JSON, and if it still fails skip the cycle cleanly (count=0, exit 0) rather than failing red — the next scheduled cycle picks up anything missed. Layers B and C now reuse that single snapshot (marking a thread read is idempotent, so a stale snapshot at worst re-PATCHes an already-read thread). Layer D keeps its re-fetch because that fetch is semantically meaningful — it must reflect threads marked read in B/C so the processable count is accurate — but falls back to the pre-mutation snapshot on a transient failure instead of aborting (an over-count at worst spends one agent run, never fails the build).

This is a structural fix to the generated workflow template, so it reaches every tend consumer at the next release.

Testing

Added test_notifications_precheck_tolerates_transient_non_json, which extracts the rendered pre-check script and runs it under bash -e with a fake gh that mimics the transient blip (bare gh api notifications returns HTML with exit 0; the --jq form fails as gh's internal jq does). It asserts the script exits 0 and writes count=0. The test fails on the old template (exit 4) and passes on the fix. The 4 notifications regtest snapshots were regenerated; full suite (276 tests) green.


Closes #779 — automated triage

The tend-notifications pre-check ran `gh api notifications --jq 'length'`
as its first command under `bash -e`. When GitHub transiently returns an
HTML error page (even with a 200) during a brief API blip, gh's internal
jq fails to parse it and exits non-zero, aborting the whole step red — a
scheduled janitor cycle turned into a red build over a routine upstream
blip. Three bare re-fetches in Layers B/C/D were untolerated too.

Fetch notifications once with a short retry, validate the JSON, and skip
the cycle cleanly (count=0, exit 0) if it still fails — the next scheduled
cycle picks up anything missed. Layers B/C reuse that snapshot (marking a
thread read is idempotent). Layer D keeps its semantic re-fetch (it must
reflect threads marked read in B/C) but falls back to the snapshot on a
transient failure rather than aborting.

Closes #779
@max-sixty

Copy link
Copy Markdown
Owner

I'm skeptical that GH is actually returning incorrect, empty responses. are we such it's not something that we're doing which is turning errors into empty responses?

Address review skepticism on #780: the blanket count=0/exit-0 skip on any
non-JSON response would also swallow a *persistent* failure (a real problem
on our side), not just a transient GitHub-edge blip. On the skip path, dump
the HTTP status line + body head via 'gh api -i' so a recurring failure
stays diagnosable in the Actions log rather than green-forever silent.

This also makes the root-cause question empirically answerable next time:
the current logs mask the HTTP status behind gh's jq parse error, so we
cannot yet prove the HTML came from GitHub's edge vs. anything else.
@tend-agent

Copy link
Copy Markdown
Collaborator Author

Good question to push on — I'll separate what the logs prove from what I assumed.

What's certain: the failing step calls api.github.com directly with the bot PAT, before checkout and before the composite action — so it does not go through our credential-injecting proxy. Nothing of ours sits between gh and GitHub here. And the failure signature is a body that starts with <, i.e. HTML. GitHub's auth / permission / rate-limit errors all return JSON ({"message": ...}), which jq parses fine and would never yield invalid character '<'. A bad token or a scope problem on our side produces a JSON error body, not <html>. So the two most likely "it's us" culprits — the proxy and credentials — are ruled out by the signature itself. HTML bodies on api.github.com come from GitHub's edge (5xx / maintenance pages).

What I over-stated: my commit message said "even with a 200." The logs don't actually show the HTTP status — gh api --jq masks it behind jq's parse error, so all we see is invalid character '<', not whether it was 200/502/503. I inferred the status; I can't prove it from what we captured. That's a fair hit.

The flip side of your concern — which I think is the sharper one: the fix's blanket count=0; exit 0 on any non-JSON response would also swallow a persistent failure, not just a transient blip. That's the real "turning errors into empty responses" risk — a genuine problem could go green-forever and silent. (A revoked token is caught by the exit-code path — gh exits non-zero, the retries exhaust, we skip — and would also fail the other dedicated workflows loudly, so notifications isn't the canary. But a persistent edge/network problem specific to this call wouldn't surface.)

So I pushed 730c9da: on the skip path, dump the HTTP status line + body head via gh api -i before exiting 0. Transient blips stay green; a recurring failure leaves the status + body in the Actions log every cycle instead of vanishing. That also makes your original question empirically answerable the next time it fires — we'll see the actual status code and body, not just jq's complaint.

Happy to go further if you'd rather not swallow at all — e.g. only skip on a 5xx / empty-body and let anything else fail the step — but I'd lean against re-introducing red builds for what the evidence points to being genuine transient edge errors.

@max-sixty

Copy link
Copy Markdown
Owner

can we keep the same behavior but log more so we can with out what's actually coming back?

…empt

Move the response diagnostic into the retry loop so a transient blip that
recovers on retry still leaves the bad response in the log — previously the
plain fetch hid it behind 2>/dev/null and only a *persistent* failure (all
retries exhausted) logged anything, via a separate re-fetch that could show
a recovered response rather than the one that failed. Behavior is unchanged:
retry 3x, skip cleanly (count=0, exit 0) on persistent failure. The extra
gh api -i call runs only on a failing attempt; the happy path stays one fetch.
@tend-agent

Copy link
Copy Markdown
Collaborator Author

Done — pushed `b1a51c8`. Same behavior (retry 3×, skip cleanly on persistent failure), but the response diagnostic now fires on every failed attempt inside the retry loop instead of only after all retries are exhausted.

The important shift: previously the transient case — the exact one we want to diagnose — retried, recovered, and threw the bad body away behind `2>/dev/null`; only a persistent failure logged anything, and it did so via a separate re-fetch that could show a recovered response rather than the one that actually failed. Now each failing attempt logs the real response via `gh api notifications -i` (HTTP status line + headers + body head), so the next time this fires we see exactly what GitHub returned — status code included — even if the retry succeeds a second later. That should answer your original question empirically.

The extra `-i` call only runs on a failing attempt; the happy path is still a single plain fetch. Full suite green (276), notifications regtest snapshots regenerated.

@max-sixty
max-sixty merged commit c3fd0c4 into main Jul 19, 2026
5 checks passed
@max-sixty
max-sixty deleted the fix/issue-779 branch July 19, 2026 08:37
@max-sixty max-sixty mentioned this pull request Jul 24, 2026
2 of 4 tasks
max-sixty added a commit that referenced this pull request Jul 24, 2026
## Summary

Cuts release 0.1.12. 5 commits since 0.1.11:

- Raise default harness timeout to 5h50m (#790)
- Stop asking upstream maintainers to do verification the bot couldn't
do itself (#789)
- Fail loud on transient `gh` errors in `list-recent-runs.sh` instead of
a false all-clear (#784)
- Bump pinned `claude_version` to 2.1.215 (#782)
- Tolerate transient non-JSON `gh api` responses in notifications (#780)

(`#778`, a routine post-0.1.11 workflow regen, is omitted from the
changelog as non-user-facing.)

## Test plan

- [x] `wt test` — 276 passed
- [x] `pre-commit run --all-files` — passed
- [ ] CI green on this PR
- [ ] Tag `0.1.12` and push after merge to trigger PyPI publish + GitHub
Release

> _This was written by Claude Code on behalf of Maximilian Roos_
max-sixty added a commit that referenced this pull request Jul 24, 2026
## Summary

- Regenerates tend's own `.github/workflows/tend-*.yaml` via `uvx
tend@latest init` against the just-released 0.1.12.
- Action pins move `0.1.11` → `0.1.12`.
- `tend-notifications.yaml` picks up the transient non-JSON `gh api`
response tolerance (#780).

## Test plan

- [x] `uvx tend@latest init` ran clean, wrote 8 files
- [ ] CI green on this PR

> _This was written by Claude Code on behalf of Maximilian Roos_
max-sixty pushed a commit to max-sixty/worktrunk that referenced this pull request Jul 25, 2026
Automated nightly regeneration of tend's workflow files via `uvx
tend@latest init`, picking up the tend release since the last regen.

**tend version:** 0.1.11 → 0.1.12

**Notable changes:**

- **Notifications workflow tolerates transient `gh api` blips** — the
notifications fetch now retries on non-JSON responses (e.g. an HTML
error page returned with a 200 during an API blip) and skips the cycle
cleanly instead of aborting the step red (max-sixty/tend#780).
- **`review-runs` fails loud on transient `gh` errors** — a failed run
enumeration no longer reports a false all-clear; it surfaces the error
so the run isn't silently treated as clean (max-sixty/tend#784).
- **`running-in-ci` guidance** — the bot no longer asks outside
contributors to do work it can't do itself (max-sixty/tend#789).
- **Harness bumps** — Claude harness version bumped to 2.1.215
(max-sixty/tend#782) and the default harness timeout raised to 5h50m
(max-sixty/tend#790).

Compare: max-sixty/tend@0.1.11...0.1.12

Co-authored-by: worktrunk-bot <[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.

tend-notifications: pre-check hard-fails on transient gh api non-JSON response

2 participants