Skip to content

Fix sed substitution of {{TOPIC}} mangling/breaking on special chars#5

Open
skytow wants to merge 1 commit into
promptadvisers:mainfrom
skytow:fix/start-loop-sed-topic-escaping
Open

Fix sed substitution of {{TOPIC}} mangling/breaking on special chars#5
skytow wants to merge 1 commit into
promptadvisers:mainfrom
skytow:fix/start-loop-sed-topic-escaping

Conversation

@skytow

@skytow skytow commented May 31, 2026

Copy link
Copy Markdown

Problem

plugins/claudex/scripts/start-loop.sh injects the user-provided topic into the plan-mode prompt templates with:

sed -e "s|{{TOPIC}}|$TOPIC|g" ...

In a sed replacement, three characters are special:

  • & — expands to the whole matched text ({{TOPIC}})
  • | — the substitution delimiter
  • \ — escape char

So a topic like add expiry & cleanup renders as add expiry {{TOPIC}} cleanup, and a topic containing | (e.g. an interview-enriched topic) crashes sed with bad flag in substitute command.

Repro:

/claudex:plan review Cal AI; Worries: Paywall & monetization friction
# round-1 drafting instructions show: "Paywall {{TOPIC}} monetization friction"

Fix

Escape \, &, and | (and collapse newlines, since sed is line-oriented) into a SED_SAFE_TOPIC before substitution, and use it in both sed calls. REVIEW_ID (generated id) and MAX_ROUNDS (integer) can't contain special chars, so they're left as-is.

SED_SAFE_TOPIC="$(printf '%s' "$TOPIC" | tr '\n\r' '  ' | sed -e 's/[\\&|]/\\&/g')"

Verification

  • A topic with & | \ now round-trips verbatim.
  • bash -n passes; doctor.sh healthy; smoke test 74/74 still green.
  • stop-hook.sh is unaffected — it expands $TOPIC via bash, not sed (so the actual Codex review prompts were always correct; only the round-1 drafting instructions were cosmetically mangled).

🤖 Generated with Claude Code

start-loop.sh injected the user-provided topic into the plan-mode prompt
templates via `sed "s|{{TOPIC}}|$TOPIC|g"`. In a sed replacement three
characters are special: `&` (the whole matched text, i.e. `{{TOPIC}}`),
the `|` delimiter, and `\`. So a topic like "Paywall & monetization"
rendered as "Paywall {{TOPIC}} monetization", and a topic containing `|`
crashed sed outright ("bad flag in substitute command").

Escape `\`, `&`, and `|` (and collapse newlines, since sed is
line-oriented) into SED_SAFE_TOPIC before substitution. REVIEW_ID and
MAX_ROUNDS are a generated id and an integer, so they need no escaping.

Verified: a topic with `& | \` now round-trips verbatim; bash -n passes;
doctor + smoke test (74/74) still green. stop-hook.sh is unaffected (it
expands $TOPIC via bash, not sed).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant