Skip to content

fix(ci-runner): replace if-not polling loop with || to avoid bash escaping - #217

Merged
max-sixty merged 1 commit into
mainfrom
daily/review-runs-polling-24233425909
Apr 10, 2026
Merged

max-sixty merged 1 commit into
mainfrom
daily/review-runs-polling-24233425909

Conversation

@tend-agent

Copy link
Copy Markdown
Collaborator

Summary

The CI polling loop in running-in-ci and review skills uses if ! cmd, but the Claude Code Bash tool escapes ! as \! before handing commands to bash. Bash then parses the escaped token as a literal command (!: command not found), so the early-exit branch of if ! pipeline is unreachable. Every poll runs all 10 iterations and prints a false "CI still running after 10 minutes".

Switched both loops to cmd || { ...; }, which doesn't rely on !.

Evidence

Reproduced in the bash tool this run:

$ echo 'if ! false; then echo then; else echo else; fi' > /tmp/t.sh
$ cat /tmp/t.sh
if \! false; then echo then; else echo else; fi
$ bash /tmp/t.sh
/tmp/t.sh: line 1: !: command not found
else

And similarly printf '%s\n' '!test' writes \!test — the escape happens even inside single quotes, so there is no source-level workaround.

Observed in a real polling run — tend-mention PR #212, run 24220018870. The background task output was:

CI still running after 10 minutes
no required checks reported on the 'hourly/review-24219300860' branch

With a working !, the first iteration's pipeline exits 1 (grep -q no match), the ! flips it to 0, and the loop exits immediately. Instead, the loop ran to completion — consistent with if ! pipeline always evaluating to the else branch (because ! is treated as an unknown command, exit 127, which is non-zero, which if sees as false).

The bot on that run attributed the timeout to "no required checks" but that explanation doesn't hold — any polling of any PR in this harness suffers the same fate.

Fix

Replace

if ! gh pr checks <n> --required 2>&1 | grep -v ... | grep -q 'pending\|...'; then
  gh pr checks <n> --required
  exit 0
fi

with

gh pr checks <n> --required 2>&1 | grep -v ... | grep -q 'pending\|...' || {
  gh pr checks <n> --required
  exit 0
}

|| is a distinct operator (not !), so the escape doesn't apply. Verified locally that the rewritten loop exits on iteration 1 when the pipeline returns non-zero.

Gate assessment

  • Confidence: Critical. Every polling run hits this; reproduced in-session and observed in a real run. One occurrence is sufficient per the gates.
  • Classification: Structural. The same conditions will recur on every polling invocation.
  • Magnitude: Targeted fix — 6 lines changed across 2 skill files, no behavior change on the happy path.

Test plan

  • cd generator && uv run pytest (147 passed)
  • Next tend-review run on this branch polls CI and exits cleanly within one iteration instead of timing out at 10 minutes

…aping

The CI polling loop in running-in-ci and review skills uses `if ! cmd`, but
the Bash tool escapes `!` as `\!` before passing commands to bash. Bash then
parses the escaped token as a literal command and prints
`!: command not found`, making the loop's early-exit branch unreachable.
Every poll runs all 10 iterations and reports a false "CI still running"
result — visible in recent tend-mention run logs.

Reproduced locally: `if ! false; then echo A; else echo B; fi` prints `B`
under the Bash tool, confirming `!` is treated as an unknown command rather
than the pipeline-negation reserved word.

Switched both polling loops to `cmd || { ...; }` form, which doesn't depend
on `!` and evaluates correctly.
@max-sixty

Copy link
Copy Markdown
Owner

I thought this was fixed in newer Claude versions; what version is running?

@tend-agent

Copy link
Copy Markdown
Collaborator Author

Claude Code 2.1.98. The bug still reproduces — just verified in this session. Running if \! false; then echo then; else echo else; fi directly via the Bash tool (no file, no quoting) still produces /bin/bash: line 1: \!: command not found followed by else. The escape is applied to the command string before bash sees it, so there's no source-level workaround.

@max-sixty

Copy link
Copy Markdown
Owner

Running if ! false; then echo then; else echo else; fi directly via the Bash tool

was the escape in the command that got sent to the bash tool??

@tend-agent

Copy link
Copy Markdown
Collaborator Author

No — I sent a plain \! in both commands. In this session just now I ran exactly if \! false; then echo then; else echo else; fi (no backslash) and bash still reported /bin/bash: line 1: \!: command not found then else. So the \ is added somewhere between the tool call and bash exec.

Concrete demonstration — I ran printf 'arg1=[%s]\n' "$1" -- "\!test" (unescaped \! in the source argument) and got:

arg1=[]
arg1=[--]
arg1=[\\!test]

The literal backslash arrives inside an already-quoted argument, so this isn't bash doing it on its own. Also confirmed set -o | grep histexpand prints histexpand off, so it's not interactive history expansion either — bash wouldn't escape \! in a non-interactive shell regardless.

The tool layer is adding the backslash before handing the string to bash. It's not something I can avoid by changing how I write the command, which is why the skill-level fix (rewriting if \! pipeline as pipeline || { ... }) is the right workaround.

@max-sixty

Copy link
Copy Markdown
Owner

No — I sent a plain ! in both commands. In this session just now I ran exactly if ! false; then echo then; else echo else; fi (no backslash) and bash still reported /bin/bash: line 1: !: command not found then else. So the \ is added somewhere between the tool call and bash exec.

all these have slashes in them! is that because they are getting added even when you try to write out this comment? are you trying to say:

  1. "In this session just now I ran exactly if ! false;"
  2. or "In this session just now I ran exactly if \! false;"

@tend-agent

Copy link
Copy Markdown
Collaborator Author

Sorry, that was confusing. Answer: option 1 — in my Bash tool call I wrote if ! false; ... with no backslash. The backslash is added somewhere between my tool call and bash exec, which is exactly the bug.

Just reproduced it deterministically in this session. I ran a tiny script that prints the raw bytes of its first argument, passing "if ! false; then echo then; else echo else; fi" (no backslash in my source):

ARG: [if \! false; then echo then; else echo else; fi]
0000000   i   f       \   !       f   a   l   s   e   ;       t   h   e

The argument as received has a literal \ before ! (byte \134 then !), even though I wrote no backslash. So bash sees \!, treats it as the command ! (the backslash quotes the next char), gets exit 127, and if takes the else branch.

And to rule out the Write tool: I wrote a file with plain if ! false via Write, then ran od -c on it — no backslash in the bytes. So the escape is specific to the Bash tool, not a universal ! mangling.

The \! that appeared in my earlier comments was me quoting what bash received (the escaped form from tool output), not what I typed. That was a bad choice — it made the claim look self-contradictory. Sorry for the noise.

@max-sixty

Copy link
Copy Markdown
Owner

is there a Claude Code issue for this? I had thought it was fixed; alas

@tend-agent

Copy link
Copy Markdown
Collaborator Author

Yes — anthropics/claude-code#35701, filed 2026-03-18, still open. Exactly this: shell-quote escapes \! to \\! on the double-quote wrapping path, and since commands run under bash -c (non-interactive, no histexpand), the backslash becomes literal.

@max-sixty

Copy link
Copy Markdown
Owner

OK, I'll merge this, and hope they fix that issue

@max-sixty
max-sixty merged commit 79909c7 into main Apr 10, 2026
3 checks passed
@max-sixty
max-sixty deleted the daily/review-runs-polling-24233425909 branch April 10, 2026 20:07
tend-agent pushed a commit that referenced this pull request Apr 10, 2026
The review skill hardcoded `grep -v "tend-review"` and the running-in-ci
skill used `grep -v "$GITHUB_WORKFLOW"` to exclude the current workflow's
own pending check. Both filters are structurally wrong: `gh pr checks`
prints the job name, not the workflow name, so neither `"tend-review"` nor
`$GITHUB_WORKFLOW` (which equals `"tend-review"`) matches the actual
`review` check row. The filter keeps every line, the own-job row always
reports `pending`, and the polling loop runs the full 10 iterations.

Observed in worktrunk runs 24260154110 (PR #2062) and 24260939035
(PR #2063) this hour — both tend-review sessions timed out after 10
minutes, each logging "the filter didn't catch it" before recovering via
manual per-check verification.

Match on the run URL (`/runs/$GITHUB_RUN_ID/`) instead. Every GitHub
Actions check row includes that URL, `$GITHUB_RUN_ID` is stable, and
non-Actions checks (codecov, pre-commit.ci) pass through unchanged.

Also incorporates #217's fix: replace `if \! cmd` with `cmd || {}`. The
Bash tool escapes `\!` as `\\!`, so `if \!` always takes the else branch
and the loop never short-circuits even when the filter is correct.
Both fixes are required together — either alone leaves the loop
stuck until the timeout. This PR supersedes #217.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
tend-agent pushed a commit that referenced this pull request Apr 10, 2026
PR #217 added a code-comment explaining the `||` vs `if \!` workaround,
but the comment itself contained `` `if \!` ``. Claude Code's slash-command
preprocessor scans the file for `` \!` `` (exec-command prefix) regardless of
markdown code fences, extracts the text up to the next backtick as a shell
command, and runs it. The extracted command was `` — the Bash tool escapes `,
which bash fails to parse (`—: command not found`), and the preprocessor
delivered an empty prompt body to Claude. Every `/review <N>` invocation
since the merge of PR #217 produced a 5-line session with no assistant
turn and no review posted — PRs #225–#233 all received zero reviews from
the bot.

Rephrase the comment in both `review/SKILL.md` and `running-in-ci/SKILL.md`
to avoid the `` \!` `` sequence entirely. The guidance is unchanged; the
text no longer trips the slash-command preprocessor.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
max-sixty pushed a commit that referenced this pull request Apr 10, 2026
#234)

## Summary

Every `/tend-ci-runner:review` invocation has been **silently broken**
since PR #217 merged at 2026-04-10 20:07 UTC. PRs #225–#233 all received
**zero reviews from the bot** — the review runs report `success` but the
session artifacts show no assistant turn at all. This PR removes the
poison and restores reviews.

## Root cause

PR #217 (`fix(ci-runner): replace if-not polling loop with || to avoid
bash escaping`) added an explanatory comment inside the CI-polling code
block:

```markdown
# Use `||` rather than `if \!` — the Bash tool escapes `\!` as `\\!`, which
# prevents bash from recognizing the pipeline-negation reserved word...
```

Claude Code's slash-command preprocessor scans the loaded file for ``
\!` `` (a prefix that means "execute the next backticked string as a
shell command") **regardless of markdown code fences**. It found `` \!`
`` inside `` `if \!` ``, consumed text through the next backtick, and
ran `` — the Bash tool escapes ` `` as bash. The em-dash is not a
command, so bash reported:

```
Error: Shell command failed for pattern "\!` — the Bash tool escapes `":
/bin/bash: line 1: —: command not found
```

When the preprocessor fails, the prompt body is not delivered. Every
`/review <N>` session in this window produced the same 5-line trace —
two internal `queue-operation` messages, the command caveat, the command
invocation, the stderr — and then nothing. The model was never given
instructions to follow.

## Evidence

All tend-review sessions in the past hour's window ran this preprocessor
and failed identically:

| Run | PR | Branch | Session size | Review posted |
|---|---|---|---|---|
| 24261252532 | #224 | `review-runs-timeout-detection` | ~120 lines | ✅
APPROVED (ran **before** #217 merged at 20:07Z) |
| 24262024783 | #225 | `tend-mention-dedup-review-events` | 5 lines | ❌
|
| 24262028666 | #226 | `hourly/review-24261599977` | — | ❌ (checkout
failure — infra race, separate issue) |
| 24262305841 | #226 | `hourly/review-24261599977` | 5 lines | ❌ |
| 24262406137 | #227 | `raise-job-timeouts` | 5 lines | ❌ |
| 24262970448 | #228 | `mention-verify-paginate` | 5 lines | ❌ |
| 24262987799 | #229 | `tend-claude-md-workflow-update-note` | 5 lines |
❌ |
| 24263043865 | #230 | `raise-job-timeouts` | 5 lines | ❌ |
| 24263117206 | #231 | `mention-review-inline-reply-prompt` | 5 lines |
❌ |
| 24263177723 | #232 | `raise-job-timeouts` | 5 lines | ❌ |
| 24263220447 | #233 | `tend-mention-dedup-review-events` | 5 lines | ❌
|

Sample JSONL from run 24262987799 (PR #229):
```json
{"type":"queue-operation"}
{"type":"queue-operation"}
{"type":"user","content":"<local-command-caveat>..."}
{"type":"user","content":"<command-name>/review</command-name>\n<command-args>229</command-args>"}
{"type":"user","content":"<local-command-stderr>Error: Shell command failed for pattern \"\!` — the Bash tool escapes `\": [stderr]\n/bin/bash: line 1: —: command not found</local-command-stderr>"}
```

No assistant turn. No Skill loads. No tool calls. No review.

Confirmation via the target PRs: `gh pr view <N> --json reviews` returns
`[]` for every one of PRs #225–#233. PR #224, reviewed before #217
landed, has the bot's APPROVED review.

The same preprocessor error is also visible in the `tend-mention` run
24261917519 when it loaded `running-in-ci` via the Skill tool — there
the error appeared as a tool_result and the model continued working,
because skill-load failures are non-fatal. Only the top-level
slash-command invocation is lethal.

## Gate assessment

- **Evidence level**: Critical (wrong outcome — reviews 100%
non-functional)
- **Classification**: Structural (deterministic; every invocation fails
the same way)
- **Occurrences this run**: 9 (plus 1 non-fatal skill-load collision in
tend-mention)
- **Historical evidence**: N/A — this regression was introduced by #217
earlier today and has no prior tracking entries
- **Change type**: Targeted fix (two-line comment rephrase in two files)
- **Both gates**: pass

## Fix

Rephrase the comments in
`plugins/tend-ci-runner/skills/review/SKILL.md:294` and
`plugins/tend-ci-runner/skills/running-in-ci/SKILL.md:142` to avoid any
`` \!` `` sequence. The guidance content is unchanged: "Use `||` rather
than `if`-based negation; the Bash tool escapes the exclamation mark to
a literal backslash-exclamation." No `\!` now appears adjacent to a
backtick anywhere in `plugins/`.

`grep '\!\`' plugins/` after the fix returns zero matches.

## Test plan

- [ ] A tend-review run on this PR completes with an assistant session
longer than 5 lines and posts an actual review (self-approval blocked,
but the bot must at least read the diff and comment)
- [ ] `grep -r '\!\`' plugins/` returns no matches
- [ ] `tend-mention` and `tend-notifications` skill loads of
`running-in-ci` no longer show `Shell command failed for pattern` in
tool_results

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

Co-authored-by: continuous-bot <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
tend-agent pushed a commit that referenced this pull request Apr 11, 2026
The Claude Code slash-command preprocessor treats a backtick-enclosed
token beginning with an exclamation mark as shell command substitution.
Any plugin skill file loaded by /review (or another slash command) that
contains this sequence crashes the session before the assistant turn
runs. The pattern has regressed twice already: PR #217 introduced it,
PR #234 removed it, PR #226 reintroduced it, PR #243 removed it again.
Each regression broke 100% of tend-review runs until the next fix
merged.

Add a local pre-commit hook that rejects the pattern at commit time.
The check constructs its search string as chr(33)+chr(96) so the hook
file itself is safe from the pattern it is searching for.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
max-sixty pushed a commit that referenced this pull request Apr 30, 2026
## Symptom

Triage bot posted [a comment on issue
#360](#360 (comment))
opening with `Thanks for the detailed write-up\!` — a literal
backslash-bang where the template intends an exclamation mark. The Bash
tool rewrites every `!` in a command string to `\!` before bash parses
it, so any comment body posted via `gh issue comment --body "..."` (or
`<<EOF` heredoc) that contains an exclamation mark ships visibly
corrupted.

## Why this slipped past the existing guidance


[`running-in-ci/SKILL.md`](https://github.com/max-sixty/tend/blob/febec9d2839e8e78d5ca585451f63ce80850f31c/plugins/tend-ci-runner/skills/running-in-ci/SKILL.md#L356)
already warns: *"Use the Write tool for any comment body containing an
exclamation mark, then pass the file to `gh ... --body-file`."* But the
triage skill's own templates ([lines 172, 178, 184, 192, 198, 204, 210,
218](https://github.com/max-sixty/tend/blob/febec9d2839e8e78d5ca585451f63ce80850f31c/plugins/tend-ci-runner/skills/triage/SKILL.md#L172))
literally contain exclamation-mark greetings — *"Thanks for reporting
this!"*, *"Thanks for the suggestion!"*, *"Thanks for reaching out!"* —
so a bot following the template via the heredoc pattern recommended in
the same skill at line 166 hits the rewrite. The trigger is in the
template itself; the general warning fires too late.

## Evidence

Two confirmed user-facing corruptions in posted issue comments since
2026-04-01, both matching the template opener pattern:

- 2026-04-30 [issue #360
comment](#360 (comment)):
`Thanks for the detailed write-up\!` (this run, run
[25179241285](https://github.com/max-sixty/tend/actions/runs/25179241285))
- 2026-04-10 [issue #221
comment](#221 (comment)):
`Thanks for the detailed writeup and cross-posted repro\!`

Scan recipe used:

```bash
gh api 'repos/max-sixty/tend/issues/comments?per_page=100&since=2026-04-01T00:00:00Z' \
  --paginate --jq '.[] | select(.user.login == "tend-agent")' \
  > /tmp/all-bot-comments.jsonl
grep -P '\\\\!' /tmp/all-bot-comments.jsonl
```

Three other matches in the [#217
thread](#217) are intentional
discussion of this same bug class (the issue was about the bash escape
itself), not corruption.

## Fix

Replace `!` with `.` across all 8 templates in
`plugins/tend-ci-runner/skills/triage/SKILL.md`, and add a short inline
note explaining that the templates intentionally avoid exclamation
marks. The note redirects future authors to the Write + `--body-file`
path if they ever need an exclamation mark (e.g. quoting a user
verbatim).

This is a structural fix at the trigger: removing the literal `!`
characters the bot copies prevents the failure regardless of which
post-path the bot chooses (heredoc vs. `--body-file`). It does not
depend on the bot remembering the general warning.

## Gate assessment

- **Evidence level**: High — 2 confirmed user-facing comment corruptions
traceable to the same templates, plus 11+ cumulative bang-escape
occurrences across other surfaces (full audit in the [evidence
gist](https://gist.github.com/8f10d7c1abcb07c79bef00ee6d02d315)).
- **Failure type**: Structural at the trigger (templates supply the
`!`); stochastic at the bot (whether the bot recalls running-in-ci's
general warning when composing the body). Removing the trigger
eliminates the failure mode.
- **Magnitude**: Removal/simplification — Low evidence bar; passes both
gates.

## Evidence

[Evidence gist for `max-sixty/tend`
2026-04](https://gist.github.com/8f10d7c1abcb07c79bef00ee6d02d315)

---------

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