Skip to content

fix(core): distinguish identifier references from literal data in constant-response heuristic - #650

Merged
jimhoyd merged 1 commit into
mainfrom
claude/638-constant-response-word-boundary
Sep 24, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
claude/638-constant-response-word-boundary

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • The constant-response review signal in packages/core/src/review.ts used a word-boundary regex (dynamicHints) to guess whether a function body is dynamic. It flagged a body as dynamic whenever state, env, secrets or inputs appeared anywhere in the source — including as a bare object-literal key ({state: 'ok'}) or as a substring inside a string value — not only as an actual identifier reference (context.state, process.env.X).
  • A genuinely constant handler such as Response.json({service: 'cookbook', state: 'ok'}) was therefore never flagged as a native-alternative to respond, even though it's a fixed answer that could be expressed declaratively. This was confirmed with recipes/middleware/functions/status.mjs prior to its conversion in fix: express constant middleware-recipe/cookbook responses via respond #630.
  • This change masks string/template literal contents before testing for these four words, and only treats a remaining match as dynamic when it is not a bare word: object-literal key. The other dynamic hints (await, fetch, Date, process, control flow, etc.) are unchanged, and a genuine identifier reference (e.g. a bare env/state variable, or process.env.X, which is still caught via the unrelated process hint) still correctly keeps the handler out of the signal.

Test plan

  • Added regression tests in test/review.test.ts reproducing the exact blind spots from constant-response review detector's word-boundary heuristic has blind spots #638: an unquoted object-literal key (status.mjs's state: 'ok'), a string value containing one of the words as a substring, and quoted object keys — all now correctly flagged as constant-response.
  • Added a companion test confirming a genuine identifier reference to state/env/secrets/inputs (not as an object-literal key) is still correctly kept out of the constant-response signal.
  • node --test test/review.test.ts — 32/32 pass.
  • npm run verify (full pipeline: lint, typecheck, build, all workspace tests) — passes.

Closes #638

🤖 Generated with Claude Code

…stant-response heuristic

The constant-response review signal's dynamicHints regex flagged a
function as dynamic whenever state/env/secrets/inputs appeared
anywhere in the source, including as a bare object-literal key or
inside a string value. A genuinely constant handler such as
Response.json({service: 'cookbook', state: 'ok'}) was therefore
never flagged, even though respond: {json: ...} could express it
directly (recipes/middleware/functions/status.mjs, pre-#630).

Mask string/template literal contents before testing for these four
words, and only treat a remaining match as dynamic when it isn't a
bare `word:` object-literal key. The other dynamic hints (await,
fetch, Date, process, control flow, ...) are unchanged.

Closes #638

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jimhoyd
jimhoyd merged commit 298e895 into main Sep 24, 2026
23 checks passed
@jimhoyd
jimhoyd deleted the claude/638-constant-response-word-boundary branch September 24, 2026 13:14
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.

constant-response review detector's word-boundary heuristic has blind spots

1 participant