Skip to content

Add service isolation (ctx.isolate) and intercept (ctx.intercept) - #3

Merged
ryudoawaru merged 2 commits into
mainfrom
feat/isolate-intercept
Aug 16, 2026
Merged

ryudoawaru merged 2 commits into
mainfrom
feat/isolate-intercept

Conversation

@ryudoawaru

Copy link
Copy Markdown
Member

Summary

Round 3 of aligning with upstream packages/core (4.0.0-rc.8): service isolation and per-caller config interception.

  • ctx.isolate(name, label = nil) — returns a ctx view whose service name lives in its own realm. The service store is now keyed by isolate key: the name itself in the default realm, a unique token (or a user-supplied label) in isolated ones. Passing the same label to two isolate calls shares one realm between them.
  • Realm-aware notify — Context#notify takes { name => key } pairs and only touches fibers whose ctx resolves the name to the same key, so provides, provider swaps, and teardowns stay realm-local. Fiber#provided tracks { name => key } for ACTIVE-boundary notifications.
  • ctx.intercept(name, config) — carries per-caller config on the ctx view. Implemented as copy-on-write with the chain pre-merged at intercept time (upstream walks the prototype chain in Service[resolveConfig] instead). Hash-form inject configs (inject: { foo: {...} }) become intercepts on the plugin ctx, mirroring upstream fiber.ts:139. Consumers read the effective config with ctx.resolve_config(name, base).
  • One deliberate divergence (stricter than upstream): the service access walk checks the realm key of provided services at the terminal fiber. Upstream's walk is name-keyed at that point, which would let a plugin under an isolated view leak the default-realm service once the walk reaches root.

Test plan

  • bundle exec rspec — 53 examples, 0 failures (new spec/cordis/isolate_spec.rb mirrors upstream tests/isolate.spec.ts: isolated context, shared label; plus duplicate-per-realm, boundary walk, intercept resolution, and inject-config conversion)
  • bundle exec ruby examples/demo.rb still runs clean

🤖 Generated with Claude Code

ryudoawaru and others added 2 commits August 16, 2026 17:05
Aligns with upstream packages/core isolate/intercept semantics:

- ctx.isolate(name, label = nil) returns a ctx view whose service name
  lives in its own realm; the service store is now keyed by isolate key
  (the name itself in the default realm, a unique token or user label in
  isolated ones). Two isolates sharing a label share a realm.
- notify takes { name => key } pairs and only touches fibers whose ctx
  resolves the name to the same key, so provider swaps and boundary
  crossings stay realm-local.
- ctx.intercept(name, config) carries per-caller config on the ctx view
  (copy-on-write, chain pre-merged instead of upstream's prototype-chain
  walk); Hash inject configs become intercepts on the plugin ctx
  (upstream fiber.ts:139). Read via ctx.resolve_config(name, base).
- Stricter than upstream in one spot: the service access walk checks the
  realm key of provided services at the terminal fiber, so an isolated
  plugin cannot leak the default-realm service through root's store.

Specs mirror upstream tests/isolate.spec.ts (isolated context, shared
label) plus intercept resolution and inject-config conversion.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Two tenants isolate :db, run the same database/web plugin code in
separate realms, answer the same event with their own db, and tear
down independently.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ryudoawaru
ryudoawaru force-pushed the feat/isolate-intercept branch from 2b18633 to d776edf Compare August 16, 2026 09:05
@ryudoawaru
ryudoawaru merged commit 476fbc7 into main Aug 16, 2026
1 check passed
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