Skip to content

🐛 Fix around() min middleware ordering for nested scopes#197

Merged
taras merged 1 commit intocontext-api/support-any-handler-typefrom
context-api/fix-min-middleware-ordering
Mar 19, 2026
Merged

🐛 Fix around() min middleware ordering for nested scopes#197
taras merged 1 commit intocontext-api/support-any-handler-typefrom
context-api/fix-min-middleware-ordering

Conversation

@taras
Copy link
Member

@taras taras commented Mar 19, 2026

Motivation

When around() is called with { at: "min" } in nested scopes, the outer scope's min middleware runs before the inner scope's — breaking the "innermost scope wins" invariant needed for provider/override patterns.

The root cause: min = [...min, middleware] appends the new middleware. Since combine() uses reduceRight, the first element in the array runs outermost. Appending makes the inner scope's middleware run closest to the handler, but the outer scope's middleware intercepts the call first — preventing the inner scope from overriding behavior.

Discovered in: https://github.com/taras/executable-markdown-agents/pull/55 (vendored fix at commit 6cb693e).

Approach

  • One-line fix in mod.ts: changed min = [...min, middleware] to min = [middleware, ...min] — prepending ensures the most-recently-installed (innermost scope) middleware appears first, giving it interception priority via reduceRight composition
  • Updated 2 existing test expectations to match the corrected ordering
  • Added 2 new tests covering nested scope min middleware: wrapping order verification and short-circuit interception
  • Version bump 0.5.0 → 0.5.1 (patch)

Prepend instead of append when installing { at: "min" } middleware so
that innermost scope middleware gets interception priority, matching
effection v4-1-alpha's scope.reduce() walk-up semantics.

Before: min = [...min, middleware] — outer scope's min ran first
After:  min = [middleware, ...min] — inner scope's min runs first

Ref: taras/executable-markdown-agents#55
@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: a3adf250-e866-4a96-8aff-b77d28ae028e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch context-api/fix-min-middleware-ordering
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 19, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@effectionx/context-api@197

commit: 9fa670c

@taras taras merged commit b635a81 into context-api/support-any-handler-type Mar 19, 2026
3 checks passed
taras added a commit that referenced this pull request Mar 20, 2026
* ✨ Support any handler type in context-api (sync, constants, operations)

Bring context-api to parity with effection v4-1-alpha's API system by
allowing API members to be any kind of value — not just Operations.

- Sync functions are lifted to Operation-returning functions
- Plain constants are lifted to Operations
- Middleware type matches the handler's raw signature (sync stays sync)
- Added isOperation() runtime guard with native iterable exclusion
- Operation check comes before function check in type mapping to prevent
  misclassification of Operation implementations

* 🐛 Fix around() min middleware ordering for nested scopes (#197)
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.

2 participants