Skip to content

fix: insight regex didn't match the real backtick-wrapped format - #307

Merged
evansenter merged 2 commits into
mainfrom
fix/insight-regex-backticks
Apr 22, 2026
Merged

fix: insight regex didn't match the real backtick-wrapped format#307
evansenter merged 2 commits into
mainfrom
fix/insight-regex-backticks

Conversation

@evansenter

Copy link
Copy Markdown
Owner

Summary

PR #303's enforce-insight-publish Stop hook never fired in production. My regex (?:^|\n)★ Insight[ \t]+[─━═]{3,} expected immediately after a newline, but the explanatory-output-style plugin wraps the decorator line in literal backticks:

`★ Insight ─────────────────────────────────────`
- ...
`─────────────────────────────────────────────────`

So the newline is followed by a backtick, not the star. Regex never matched. Unit tests in PR #303 passed because fixtures were hand-written without backticks and didn't model real output.

Fix

  • Regex: allow optional leading backtick → (?:^|\n)\?★ Insight[ \t]+[─━═]{3,}`
  • Test fixtures: rewritten to use the actual backtick-wrapped production format
  • New regression test: blocks_bare_format — confirms bare (no-backtick) form still triggers, since the backtick is optional in the regex

How this got missed

  1. Hand-written unit-test fixtures didn't match real production output. Green tests + false confidence.
  2. Pre-merge live-validation on PR Enforce ★ Insight → publish_event via Stop hook #303 ran against a transcript that had no fresh insights at that moment — the match case wasn't exercised, only the turn-boundary + perf logic.

Lesson captured as gotcha in event bus (event #4003): sample a real insight from the transcript before building regex fixtures.

Verification

Live-run against this session's real 1.4MB transcript:

{"insights": 3, "publishes": 1}  ← correctly detected
exit=0                           ← lenient rule satisfied, no false block

Test plan

  • 88/88 hook tests pass (was 87 — added blocks_bare_format regression test)
  • shellcheck --severity=error clean
  • make check green
  • Live-validated hook against this session's transcript — correct behavior

🤖 Generated with Claude Code

PR #303's hook never fired in production because the regex required
'★' immediately after a newline, but explanatory-output-style wraps the
decorator line in literal backticks (`★ Insight ─────`). Unit tests
passed only because fixtures were hand-written without backticks and
didn't model the real output format.

- Regex: add optional leading backtick — `(?:^|\n)\`?★ Insight[ \t]+[─━═]{3,}`
- Test fixtures: rewrite to use the actual backtick-wrapped format
- New regression test: bare format (no backticks) still blocks, because
  the new regex makes the backtick optional rather than required

Live-validated against this session's transcript: hook now correctly
counts 3 insights and 1 publish in the current turn.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
claude[bot]
claude Bot previously approved these changes Apr 22, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

Summary: Surgical fix for a regex that never matched real production output. The optional-backtick modification in enforce-insight-publish.sh:65 is correct, test fixtures are updated to mirror the actual backtick-wrapped format from explanatory-output-style, and the new regression test guards the bare (no-backtick) format.

Verdict: APPROVE - fix is correct and well-covered. One inline suggestion below, non-blocking.

Suggestion (tests/test-hooks.sh:1522): The comment "earlier versions of the regex required a backtick" is slightly misleading - no prior version of this regex ever required a backtick (pre-fix had no backtick support at all; this PR introduced the optional backtick). A clearer framing for the regression intent: "guards against a future tightening that makes the backtick required - the bare format must still match." The test itself is useful as-is; this is purely about the explanatory comment wording.


Automated review by Claude Code

No earlier version of the regex required a backtick — this PR introduced
the optional-backtick support. Correct framing: the test guards against
a future tightening that would make the backtick required.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@evansenter

Copy link
Copy Markdown
Owner Author

Feedback Addressed

Implemented

  • [Suggestion] Test comment wording corrected (tests/test-hooks.sh:1522) — now accurately describes the regression intent (guarding against future tightening that would make the backtick required, rather than the incorrect "earlier versions required a backtick").

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Prompt: evansenter/dotfiles/.../claude-review.md

Code Review

Summary

Surgical regex fix: adds an optional leading backtick so the Stop hook matches the real explanatory-output-style output (backtick-wrapped decorator line), fixing the missed matches from PR #303. Test fixtures are updated to mirror real production output, and a new blocks_bare_format regression test keeps the bare (no-backtick) path covered so a future tightening of the regex would be caught.

Previously Addressed (Filtered)

  • [Suggestion] tests/test-hooks.sh:1522 comment wording — rephrased to correctly describe the regression intent (guards against a future tightening, not "earlier versions required a backtick").

Verdict

APPROVE — fix is correct, tests mirror real output, and prior feedback was addressed. No new issues.


Automated review by Claude Code

@evansenter
evansenter merged commit e7e556e into main Apr 22, 2026
6 checks passed
@evansenter
evansenter deleted the fix/insight-regex-backticks branch April 22, 2026 21:54
evansenter added a commit that referenced this pull request May 21, 2026
…ism in hook-authoring (#309)

* Add live-validate gate to /pr-create + fixture-realism nudge

Two harness-level recall fixes for lessons memory alone failed to fire:

1. /pr-create: new step 4 gates on `git diff --name-only` matching
   home/.claude/{hooks,plugins,settings.json}. Surfaces a live-validation
   prompt before PR creation — for behavioral guards (hook/lint/policy
   that BLOCKS something), CI fixtures can't prove the guard is wired
   in and reachable. References the new memory.

2. hook-authoring SKILL.md: "Fixture realism" nudge in the Testing
   section. Sample real transcript content before writing regex; don't
   hand-write fixtures from a mental model.

Both surfaced by improve-workflow reflection on the three-PR Stop-hook
arc (#303#307#308) where each round shipped with insufficient
empirical validation. Skill prose stays tight (~3-5 lines each) to
respect the cost-per-invocation rule from PR #306.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Address reviewer feedback on PR #309

- pr-create.md: replace prose trigger with deterministic `grep -qE`
  one-liner, and drop the dead `home/.claude/plugins/` arm (plugins
  aren't vendored in this repo; they're referenced via enabledPlugins
  in settings.json).
- hook-authoring SKILL.md: fix dangling reference. The backtick case
  study lives inline in enforce-insight-publish.sh, not in hooks/README.md
  Gotchas (which covers jq multiline, PATH-stubbing, and the transcript
  race — not backticks). Point at the script with its inline comment.

Verified the new regex via dry-run: matches `home/.claude/hooks/foo.sh`
and `home/.claude/settings.json`, correctly does NOT match
`home/.claude/skills/` or `home/.claude/commands/`.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

* Fix backtick imbalance on fixture-realism line

PR #309 round-2 review caught that my dangling-reference fix introduced
an odd backtick count (9) on hook-authoring/SKILL.md:209 — the inlined
regex contained a literal backtick that closed an inline code span
prematurely.

Drop the inlined regex from the prose entirely; the file pointer
itself is the durable reference, and the script's inline comment has
the regex + narrative anyway.

Backtick count is now even (6, three pairs).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.7 (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