Skip to content

investigation(skills): Claude Code may substitute positional $0 inside qa SKILL.md awk programs when /qa gets an argument #1289

Closed

Description

@admarble

Found: #1249's QA phase (2026-10-03) reported: "Skill-argument substitution replaces $0 in qa SKILL.md awk programs, silently breaking §6c Step 1 and manual-AC attribution." Unverified: QA gave no transcript excerpt, and the runner has not reproduced it.

Problem (if real). Claude Code substitutes $ARGUMENTS, $ARGUMENTS[N] and the positional shorthand $0, $1, … in a skill's markdown when the skill is invoked with arguments, as /qa 1249 and the orchestrator's phase prompts do. The docs don't say whether fenced code blocks are exempt. The documented escape is a single backslash (\$0).

.claude/skills/qa/SKILL.md has 5 bare $0 uses on main, all inside awk programs:

  • :2219: awk '/^\+[^+]/ { print substr($0, 2) }' (§6c Step 1, added-line extraction)
  • :2863–:2866: the manual-AC attribution program (tolower($0), ac = $0)

If $0 becomes 1249, substr(1249, 2) prints 249 for every added line. tolower(1249) never matches, so attribution goes silent. Neither fails loudly.

Other skills may have the same exposure: run rg -n '\$[0-9]' .claude/skills/*/SKILL.md.

AC

  • AC-1: Reproduce or refute. Invoke a scratch skill containing a bash-fenced awk '{ print $0 }' with an argument, and record what the model receives. Evidence: the result recorded on this issue, with the Claude Code version
  • AC-2: If AC-1 reproduces, every bare positional $N in the shipped skills (all mirrored copies) is escaped or rewritten. A lint (scripts/lint-skill-*.ts) rejects an unescaped $[0-9] inside skill bodies. Evidence: a gate test, mutation-verified
  • AC-3: If AC-1 refutes, close with the recorded result and drop the item from feat(qa): enforce the follow-up ledger — every deferred item resolves to filed/fixed/dropped; sibling scans search the defect class #1249's follow-up ledger as "not reproducible"

Graph metadata

Activity

  1. added
    bug
    on Oct 3, 2026
  2. admarble commented on Oct 4, 2026

    @admarble
    ContributorAuthor

    AC-1: reproduced (Claude Code 2.1.289)

    A scratch project skill (.claude/skills/zzsubst1289/SKILL.md) held a bash-fenced block with awk '{ print substr($0, 2) }' and awk '{ print $1, $2 }', plus an escaped `\$0`. Run with claude -p "<invocation>" --model haiku --output-format stream-json; the model was told to echo the body verbatim.

    Invocation Skill text Model received
    /zzsubst1289 1249 substr($0, 2) substr(1249, 2)
    /zzsubst1289 1249 $1, $2 unchanged
    /zzsubst1289 1249 --phase qa $1, $2 --phase, qa
    either `\$0` `$0`
    • Fenced code blocks are not exempt from substitution.
    • The \$N escape works.
    • $N is replaced only when argument N exists, so a one-argument /qa {issue} (the orchestrator's prompt, phase-registry.ts:289) breaks only $0.

    Exposure on main:

    • $0, broken on every argument-bearing /qa:
      • qa/SKILL.md:2220 (§6c Step 1)
      • qa/SKILL.md:2846-2849 (manual-AC attribution)
    • $1/$2, broken only when the skill gets two or more arguments:
      • qa/SKILL.md:1638-1639, 2057, 2961-2962
      • loop/SKILL.md:126
      • setup/SKILL.md:139
      • release/SKILL.md:382

    AC-1 is answered: proceed with AC-2. A suggestion for AC-2: move the two QA awk programs into a script (as scripts/qa/precheck.ts already does) rather than only escaping them, so that future inline awk can't reintroduce the problem. The lint is still needed for anything that stays inline.

  3. admarble commented on Oct 4, 2026

    @admarble
    ContributorAuthor

    Runner context (before sequant run)

    • AC-1 is done. The reproduction is in the comment above (Claude Code 2.1.289, $0 → 1249 inside fenced blocks). AC-3 does not apply. Implement AC-2.
    • Find the owner before choosing the fix. Inline shell inside SKILL.md is exposed because Claude Code treats skill text as a template. Before deciding "escape vs move to a script", list every producer of inline positional $N. That means all skills, every mirrored copy (.claude/skills, templates/skills, the plugin tree; see chore(plugin): retire the root hooks/ and skills/ copies once ./plugin ships #1271's shared mirror-roots list), and any skill-template generator. If the class needs a rule beyond this issue, surface it rather than patching locally.
    • Phase rules (both exec and qa): run the full suite at most once, in the foreground, with timeout, and only after a WIP commit. Run targeted vitest files otherwise; CI runs the full suite. QA: this note applies to you too. Put every SEQUANT_MUTATION marker and the AC table inside exec's final ## Summary; the orchestrator builds the PR body from it.
  4. admarble commented on Oct 4, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (3 ACs, 5 directories)

    AC Quality Check

    • AC-1: already satisfied by the reproduction comment (Claude Code 2.1.289). Fenced code is NOT exempt; \$N escape works; $N is replaced only when argument N exists.
    • AC-3: not applicable (AC-1 reproduced). Implement AC-2 only.
    • AC-2: "escaped or rewritten" is fine, but its Evidence: is not command-shaped. Tightened below to npx vitest run scripts/lint-skill-argument-substitution.test.ts (fails today on the 12 sites; passes only once fixed).

    Scope Assessment

    Non-Goals: ⚠️ none stated in the issue. Proposed: no change to how the orchestrator invokes skills; no change to $ARGUMENTS use.

    Metric Value Status
    Feature count 1 (fix + lint) ✅
    AC items 3 (1 done, 1 N/A) ✅
    Directory spread 5 (.claude/skills, templates/skills, plugin/skills, scripts, package.json/ci) ✅

    Verdict: ✅ SCOPE_OK

    Acceptance Criteria

    AC-1: Reproduce or refute — DONE

    Verification: Manual Test (recorded on issue)
    Scenario: Given a scratch skill with fenced awk '{ print substr($0, 2) }' → When invoked /x 1249 → Then model receives substr(1249, 2). Recorded in the first issue comment.

    AC-2: No bare positional $N in shipped skill bodies; a lint enforces it

    Verification: Unit Test (gate test) + CLI lint
    Scenario: Given any SKILL.md in .claude/skills, templates/skills, plugin/skills → When the lint scans the body → Then an unescaped $[0-9] is a violation; today 12 sites violate it, after the change 0.
    Assumptions: Only SKILL.md bodies are substituted. references/*.md and scripts/*.sh are loaded as files, not templated (to confirm in exec with the same scratch-skill technique, one extra row).

    AC-3: N/A (AC-1 reproduced).

    Implementation Plan

    Producer inventory (whole-repo grep, (?<!\\)\$[0-9] in SKILL.md, canonical copy; mirrors are generated copies via check-skill-sync --fix; no template generator exists for skill bodies):

    Site Program Breaks when
    qa/SKILL.md:2220 substr($0, 2) (§6c Step 1) any argument
    qa/SKILL.md:2846-2849 manual-AC attribution (tolower($0), ac = $0); the $0 in the comment at :2843 too any argument
    qa/SKILL.md:1638-1639, 2057, 2961-2962 awk '{print $1}', sed/grep $1/$2 2+ arguments
    loop:126, setup:139, release:382 awk '{print $2}' etc. 2+ arguments

    Not in scope (not substituted, so not violations): qa/scripts/quality-checks.sh (a real script file).

    Phase 1: Move the two always-broken QA programs out of the body

    1. Add scripts/qa/ helper(s) (precheck.ts is the precedent), e.g. scripts/qa/added-lines.ts and scripts/qa/manual-ac-attribution.ts, with unit tests that run the real logic against fixture diffs/spec text. The SKILL.md calls them instead of inline awk. Check how precheck is shipped to consumer projects before choosing the location; if consumers do not get scripts/qa/, put the helpers under the skill's own scripts/ dir (like quality-checks.sh) so they ship in all three roots.
    2. Update §6c Step 1 and the attribution block in .claude/skills/qa/SKILL.md, then npm run sync:skills (edit canonical first).

    Phase 2: Escape the remaining $N

    1. Escape \$1/\$2 in qa (1638-1639, 2057, 2961-2962), loop:126, setup:139, release:382. Open question below on non-Claude drivers.

    Phase 3: Lint

    1. scripts/lint-skill-argument-substitution.ts (+ test): scan SKILL_ROOTS imported from check-skill-sync.ts (the single list, chore(plugin): retire the root hooks/ and skills/ copies once ./plugin ships #1271), SKILL.md files only, flag (?<!\\)\$[0-9] outside a small allowlist for currency ($5, $0.50, $2/call: require the digit not be followed by .digit/k/M//... exact rule fixed by the corpus run). Exit 1 on violation.
    2. Wire lint:skill-argument-substitution into package.json and .github/workflows/ci.yml beside lint:skill-calls.
    3. Gate test scoped to the scanned SKILL.md region: fixture with awk '{print $0}' fails; \$0 passes; currency passes. Mutation-verify: delete the lint's $N pattern → the fixture test fails; restore. Record in PR body with Mutation-verified: AC-2 — <mutation>; scripts/lint-skill-argument-substitution.test.ts > … > <case> failed; restored. (path first).
    4. Run the lint against the real corpus (all three roots) and show 0 violations; also show it reports the 12 sites on pre-change main (git stash/base checkout).

    Design Review

    1. Where does this belong? Behavior lives in checked scripts (tested); the lint lives in scripts/lint-skill-*.ts, next to the other skill lints, roots from the shared list.
    2. Simplest correct approach? Escape all 12 sites. Rejected as the only fix: the escape leaves a literal \$0 for any driver that does not template (codex/opencode read the same SKILL.md; to verify), and inline awk stays untested (feat(qa): require detection-pattern verification for skill regex/grep/awk changes #551). So: script the two always-broken QA programs, escape the rest, lint everything.
    3. Pattern / re-patch count: new class, no prior fix (git log -S for substr($0 shows only feat(qa): require detection-pattern verification for skill regex/grep/awk changes #551/feat(#551): /qa Section 6c — detection-pattern verification for skill regex/grep/awk changes #572 introducing it). No recurrence ≥2. Class is guarded by the lint, so it stays fixed.
    4. Senior challenge: "why not escape only?" → driver portability + testability above. "Why not script all 12?" → 1-liner field extraction; cost exceeds benefit, lint catches regressions.
    5. Producers/consumers: producers = hand-written SKILL.md bodies in 3 roots (mirror via sync); no generator. Consumers = Claude Code substitution; non-Claude drivers. Reported, no divergence from the issue's prescription.

    Feature Quality Planning

    All standard checks pass. Notable:

    • Consumer projects get these skills via sequant init/sync; the helper scripts must ship with them (see step 1).
    • Lint false positives on prices are the main risk; verify on the real corpus.

    Open Questions

    1. Do non-Claude drivers (codex, opencode) see \$0 literally? Recommendation: check opencode debug skill / codex exec during exec; if yes, the script approach is required for the 2 QA sites and the escape is acceptable only for the 1-2-arg field extractions. Impact: broken awk under those drivers.
    2. Should the lint also cover $ARGUMENTS-style tokens and $(( ))/${1}? Recommendation: no, $[0-9] only (matches the documented substitution set plus the repro). Impact: low.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: Standard bug fix with a gate test (AC-2 declares gate evidence; testgen skipped: the test is specified in step 6). Note for exec/qa: run the full suite at most once; targeted vitest otherwise; put the SEQUANT_MUTATION markers and AC table in exec's final ## Summary.

    Label Review

    Current: bug. Recommended: bug, planned. Quality loop stays off.

  5. added
    planned
    on Oct 4, 2026
  6. admarble commented on Oct 4, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 2/3 met

    Gaps:

    • AC-2's gate test has no recorded mutation result in the PR body (QA verified it: gutting POSITIONAL_RE fails the flag tests)
    • Missing CHANGELOG entry for a user-facing skill fix
    • Non-Claude drivers (codex/opencode) may read the escaped $N literally and awk errors silently behind || true
    • Unconfirmed whether references/*.md files are also substituted (the lint scans SKILL.md only)
    • The manual-AC attribution awk program was escaped rather than moved to a script, as the spec preferred. It works under Claude Code, but it is still untested inline awk.
    • The escaped \$N lines are a syntax error for any driver that does not process templates (ledger item 1).
  7. admarble commented on Oct 4, 2026

    @admarble
    ContributorAuthor

    Execution Verification

    Command: npm run lint:skill-argument-substitution (in the #1289 worktree, after the runner's $(N) rewrite)
    Result: Verified by human review

    Execution Output

    Exit code: 0
    Duration: 1s

    ✅ No positional $N in skill bodies
    

    Negative check (\$0 reintroduced at qa/SKILL.md:2847, then restored). Exit code: 1

    ❌ 1 positional $N in skill bodies (Claude Code substitutes them; write $(N) or move to a script, #1289):
      .claude/skills/qa/SKILL.md:2847  isdecl { ac = \$0 }
    

    Human Confirmation:

    Yes, looks correct


    Verified by /verify command

  8. added a commit that references this issue on Oct 4, 2026
  9. admarble commented on Oct 4, 2026

    @admarble
    ContributorAuthor

    Runner follow-up ledger (QA pass on PR #1303)

    • Missing mutation record for AC-2: fixed in this PR. Mutation-verified and recorded in the PR body; the marker parses as valid.
    • Missing CHANGELOG entry: fixed in this PR.
    • Codex/opencode read \$N literally and awk fails: fixed in this PR. Fields are rewritten as $(N), which Claude Code leaves alone and awk evaluates the same way. The lint now rejects the escaped form as well.
    • The manual-AC awk program is escaped inline rather than moved to a script: dropped. In its $(N) form it works for every driver, and the runner executed it against sample input with output identical to main. Moving it would also have to move lint-skill-gates's pattern parser.
    • Whether references/*.md get substituted: dropped. Claude Code templates only the skill body it injects. References and scripts reach the model through Read or Bash as file contents, so they are not subject to substitution.
    • Found while recording the marker: filed fix(qa): a SEQUANT_MUTATION marker whose text contains "}" parses to nothing — renderMutationMarker and the parser do not round-trip #1305. A marker whose text contains } parses to nothing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions