Skip to content

fix: express constant middleware-recipe/cookbook responses via respond - #630

Merged
jimhoyd merged 1 commit into
mainfrom
claude/constant-response-recipes-599
Sep 24, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
claude/constant-response-recipes-599

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • recipes/middleware/functions/items.mjs (/api/items, behind envelope.mjs), functions/catalog.mjs (/versioned, behind etag.mjs) and functions/status.mjs (/negotiated, behind negotiate.mjs) were trusted function: handlers that always returned the same literal body — exactly what respond: {json: ...} expresses declaratively, per the declarative-first direction.
  • Replaced all three function: handlers with respond: {json: ...} in recipes/middleware/urlcode.yaml and the identical routes in examples/cookbook/routes/middleware.yaml, and deleted the now-unused function modules from both trees (test/recipes.test.ts requires the two to stay identical for any file that still exists in the recipe).
  • status.mjs was also genuinely constant but wasn't flagged by the constant-response review signal — its body contains the word state, which the conservative detector in packages/core/src/review.ts treats as a dynamic hint. Converted it anyway since it's constant in fact.
  • Regenerated examples/cookbook/route-index.json (npm run docs:cookbook-index) and trimmed recipes/middleware/recipe.yaml's files list to match.
  • Reworded the "Native bodies stay opaque" bullet in docs/MIDDLEWARE-EXAMPLES.md: that framing only applies to sandbox: true routes, where the guest truly cannot read a native response body. On these trusted cookbook/recipe routes, envelope, negotiate and etag can read and rewrite a respond body exactly as they could a function's. Touched up two stale "function response"/"function output" comments in envelope.mjs/etag.mjs for the same reason.
  • Updated the hardcoded handler-count assertions in test/context.test.ts (function 10→7, respond 16→19, files.functions.length 10→7) to match the cookbook's new handler mix.

Verification

  • urlcode test --project recipes/middleware: 64/64 passed (matches the issue's prior manual trial).
  • urlcode test --project examples/cookbook: 99/99 passed.
  • urlcode review --project recipes/middleware: 0 observations (previously 2 constant-response findings).
  • node --test test/recipes.test.ts test/review.test.ts test/context.test.ts: all green.
  • npm run typecheck, npm run check:docs: clean.
  • npm run verify: full suite passed except 4 unrelated, timing-sensitive tests (cli.test.ts, provider-verification.test.ts, sandbox-pool.test.ts, verify-deployment.test.ts — wall-clock deadline assertions) that failed under heavy concurrent load from other sessions on this shared machine; re-ran them in isolation and they still occasionally miss their wall-clock windows under load, but none touch recipes/cookbook/review/context code and they aren't in the diff's blast radius.
  • npm run test:package: passed (recipes ship in the package).

Remaining

  • The constant-response review detector's conservative state/env/etc. word-boundary heuristic still won't flag a genuinely constant handler whose body happens to contain one of those words (that's how status.mjs slipped through here). Not fixing the detector itself in this PR — out of scope for this issue, which only asked to convert the three modules and confirm the signal now reports nothing for the recipe.

Closes #599

🤖 Generated with Claude Code

/api/items, /negotiated and /versioned in the middleware recipe and cookbook
returned the same literal body on every request through a trusted `function:`
handler. `respond: {json: ...}` expresses that directly with no code, matching
the declarative-first direction. Delete the now-unused function modules
(items.mjs, catalog.mjs, status.mjs) from both the recipe and the cookbook,
regenerate examples/cookbook/route-index.json, and update the hardcoded
handler-count assertions in test/context.test.ts to match.

status.mjs was also a fixed answer despite not being flagged by the
constant-response review detector (its body contains the word "state", which
the conservative detector treats as dynamic); convert it too since it is
genuinely constant.

Reword the "Native bodies stay opaque" bullet in docs/MIDDLEWARE-EXAMPLES.md:
that framing only holds for sandbox: true routes, where the guest cannot read
a native response body. These cookbook routes are trusted, so envelope,
negotiate and etag can read and rewrite a respond body the same as a function
response. Also touch up two middleware comments (envelope.mjs, etag.mjs) that
described the body they rewrite as a "function response"/"function output".

Closes #599

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd merged commit eaccbc7 into main Sep 24, 2026
21 checks passed
@jimhoyd
jimhoyd deleted the claude/constant-response-recipes-599 branch September 24, 2026 03:31
jimhoyd added a commit that referenced this pull request Sep 24, 2026
…stant-response heuristic (#650)

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]>
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.

[Gap]: middleware recipe and cookbook keep constant-response functions that respond expresses

1 participant