Skip to content

feat(core): generic request-bound capability handoff for extensions (epic #837 Stage B) - #840

Merged
jimhoyd merged 1 commit into
mainfrom
claude/epic-837-stage-b-handoff
Sep 28, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
claude/epic-837-stage-b-handoff

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Stage B of epic #837

Resolves #839 (the contract decision issue) and implements it.

The gap

Every typed extension export (AuthExports, StoreExports, FormsExports, MailExports, AuditExports, AbuseExports) exists only as a live object passed extension-to-extension through HostContext.get, never reachable from an ordinary function/middleware route. The only precedent for ordinary route code reaching anything extension-provided is auth's sessionUserId() (#833): a narrow, one-way, one-field header projection computed before the handler runs.

The decision (#839) and what's implemented

Generalize that precedent into a live, per-invocation call rather than inventing a second header-projection channel per capability:

  • RuntimeExtension.capabilities?: readonly string[] — a pure availability signal, parallel to providesPrincipal/streams, validated at prepareExtensions.
  • ExtensionInstance.provide?(capability, invocation) — called once per declared name per request, only for an extension the route already names in policies.extensions (so it rides that extension's existing authorize()/middleware() gate — no side door around admission, addressing the transitive-authority-parity gap in [Re-scoped; legacy expansion on hold]: Ecosystem integration, discovery and AI authoring #837 §17).
  • InvocationContext — frozen, request-scoped requestId, route.pattern, the committed principal (never caller-supplied), and the request's AbortSignal off the sandbox path.
  • Bound into context.capabilities.<extension>.<capability>, built right beside context.signal.
  • No new YAML. Reusing policies.extensions as the grant surface needed no new top-level vocabulary, per the epic's own bar for adding one.
  • Sandbox refused at compile time. A live object can't cross the sandbox worker's JSON boundary, so sandbox: true + a route naming a capability-declaring extension fails route compilation (capability-in-sandbox) — same place and pattern as the existing stream-in-sandbox check, not a per-request surprise.
  • provide() returning undefined just omits that entry (e.g. an anonymous request asking for a capability that needs a principal) — not an error.

Evidence

test/extension-capability.test.ts (8 cases) with a synthetic, non-first-party "vault" provider proves: no first-party import needed anywhere in core; two requests never share a bound object or requestId; a route not naming the extension gets nothing; an extension with no declared capabilities or no provide() is a no-op, not a crash; sandbox: true refuses deterministically when capabilities are named and still activates cleanly when they aren't; registration-time validation rejects malformed/duplicate/oversized capability lists.

npm run verify passes in full, including audit:packages (core package budget raised 864→870 KiB packed / 3430→3445 KiB unpacked, measured and justified in scripts/package-audit.ts, same pattern as every prior bump) and npm run test:addons.

Docs

New "Request-bound capabilities" section in EXTENSIONS.md; RIM-EXT-CAPABILITY-001 contract card in RUNTIME-IMPLEMENTATION.md.

Deliberately out of scope here

No first-party package (store/auth/mail/audit/abuse) declares a capability yet — naming their specific operations is each package's own decision, in Stage C+ per the epic's sequencing. The other three priority gaps from #837 §17 (aggregate budgets/backpressure, discovery-as-untrusted-input, cross-transport value semantics) don't block this mechanism and can land incrementally.

  • Changes match the documented portable contract; docs/examples are updated where needed.
  • Relevant tests and package checks pass, or limitations are explained above.
  • No credentials, customer data or private project material are included.
  • Runtime/starter changes remain aligned where applicable.

🤖 Generated with Claude Code

…837 Stage B)

Ordinary function/middleware routes have had exactly one way to reach an
installed extension's live behavior: sessionUserId()'s one-way projection
of a single opaque id through the reserved x-urlcode-context-* header,
built before the handler runs. Everything else an extension exports
(a caller-scoped store collection, a declared form flow, a templated
email) exists only as a live object passed extension-to-extension
through HostContext.get, never reachable from a route's own code.

Generalize the identity precedent into a live, per-invocation call
instead of a second header-projection channel per capability, per
Stage B's contract decision (#839): a registration declares the
capability names its instance can provide (RuntimeExtension.capabilities,
parallel to providesPrincipal/streams); its instance implements
provide(capability, invocation), called once per declared name per
request, only for a route that already names it in policies.extensions
(so it rides the same authorize()/middleware() gate that extension
already applies, never a side door around it); the result binds into
context.capabilities.<extension>.<capability>, right beside
context.signal. invocation (InvocationContext) is frozen and
request-scoped: requestId, route.pattern, the committed principal
(never caller-supplied) and the request's AbortSignal off the sandbox
path.

No new YAML: reusing policies.extensions as the existing grant surface
needed no new top-level vocabulary. A live bound object cannot cross the
sandbox worker's JSON boundary, so sandbox: true plus a route naming a
capability-declaring extension refuses at route-compile time
(capability-in-sandbox), the same place and pattern as the existing
stream-in-sandbox check.

Adds RIM-EXT-CAPABILITY-001 to docs/RUNTIME-IMPLEMENTATION.md and a
"Request-bound capabilities" section to docs/EXTENSIONS.md.
test/extension-capability.test.ts proves the mechanism with a synthetic,
non-first-party provider: no first-party import is needed anywhere in
core; two requests never share a bound object; missing/undeclared/
provide-less/sandboxed cases each fail or no-op deterministically before
or without touching the handler; registration-time validation rejects
malformed capability lists.

No first-party package declares a capability yet; naming the specific
store/auth/mail/audit/abuse operations an application can reach this way
is each package's own decision in a later stage.

Resolves #839. Part of epic #837 Stage B.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 28, 2026 05:10
@jimhoyd
jimhoyd merged commit a996f0d into main Sep 28, 2026
30 checks passed
@jimhoyd
jimhoyd deleted the claude/epic-837-stage-b-handoff branch September 28, 2026 05:18
jimhoyd added a commit that referenced this pull request Sep 28, 2026
* proof(#843): embedded Better Auth in one local URLCode site

Adds proofs/private-requests: a packed-consumer site where Better Auth
1.7.6 on node:sqlite owns accounts, sessions and its tables, an operator
adapter mounts its native handler and turns its verified session into
the request principal and #840's request-bound `identity` capability,
and ordinary function routes implement owner-private requests and one
reviewer-only approval with parameterized SQL.

The adapter refuses before serving on a missing secret, an uninitialized
schema, an origin or basePath mismatch, telemetry left on, or a disabled
rate limiter; forwards only an exact allowlist of Better Auth paths
(disabledPaths misses parametric routes); and supplies the admitted client
address for throttling in a header it always overwrites.

test/private-requests.integration.ts (npm run test:proof) packs this
checkout, installs it into a copy of the site, and exercises setup,
explicit policy approval, provider/target refusals, declarative fixtures
with audit ready, and the HTTP flow including foreign-owner and reviewer
denial, malformed input, concurrent approval, cancellation, sign-out,
revocation and throttling.

Core: explain/report state that an extension mount's subpaths are
provider-defined and not inspected, and list the capabilities a
registration declares on the routes it protects.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* ci: run the embedded Better Auth proof with the packed add-on integration

verify:workspaces and the workspace-integration job now run
npm run test:proof after test:addons. The proof packs the already-built
core with --ignore-scripts, like the other packaging tests, so prepare
never rebuilds dist under another test. Changes under proofs/ or to the
proof's test are high-impact, so they select that job on a pull request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(proof): stop the server before removing the site, run npm without a shell

On Windows the proof's behavior passed but teardown did not: separate
t.after hooks run in registration order, so the temp site was removed
while the served runtime still held its SQLite files (EBUSY), the
server-stop hook never ran, and the live child kept the test process
alive until the job timeout. One unwind stack now attempts every
closer, newest first. npm runs through npm_execpath under this Node, so
Windows needs no shell (DEP0190).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
jimhoyd added a commit that referenced this pull request Sep 28, 2026
…thoring MCP server (#834) (#849)

* proof(#843): embedded Better Auth in one local URLCode site

Adds proofs/private-requests: a packed-consumer site where Better Auth
1.7.6 on node:sqlite owns accounts, sessions and its tables, an operator
adapter mounts its native handler and turns its verified session into
the request principal and #840's request-bound `identity` capability,
and ordinary function routes implement owner-private requests and one
reviewer-only approval with parameterized SQL.

The adapter refuses before serving on a missing secret, an uninitialized
schema, an origin or basePath mismatch, telemetry left on, or a disabled
rate limiter; forwards only an exact allowlist of Better Auth paths
(disabledPaths misses parametric routes); and supplies the admitted client
address for throttling in a header it always overwrites.

test/private-requests.integration.ts (npm run test:proof) packs this
checkout, installs it into a copy of the site, and exercises setup,
explicit policy approval, provider/target refusals, declarative fixtures
with audit ready, and the HTTP flow including foreign-owner and reviewer
denial, malformed input, concurrent approval, cancellation, sign-out,
revocation and throttling.

Core: explain/report state that an extension mount's subpaths are
provider-defined and not inspected, and list the capabilities a
registration declares on the routes it protects.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* ci: run the embedded Better Auth proof with the packed add-on integration

verify:workspaces and the workspace-integration job now run
npm run test:proof after test:addons. The proof packs the already-built
core with --ignore-scripts, like the other packaging tests, so prepare
never rebuilds dist under another test. Changes under proofs/ or to the
proof's test are high-impact, so they select that job on a pull request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(proof): stop the server before removing the site, run npm without a shell

On Windows the proof's behavior passed but teardown did not: separate
t.after hooks run in registration order, so the temp site was removed
while the served runtime still held its SQLite files (EBUSY), the
server-stop hook never ran, and the live child kept the test process
alive until the job timeout. One unwind stack now attempts every
closer, newest first. npm runs through npm_execpath under this Node, so
Windows needs no shell (DEP0190).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(cli,mcp): the reviewed operator policy reaches inspection and authoring (#834)

bootstrap, context, explain, review, plan-feature and mcp accept the
operator's --policy. A verified policy pins the host file to its reviewed
projectSha256, so no PROJECT_SHA256 export is needed; bootstrap and
context repeat it in their emitted commands and drop it from
prerequisites. The MCP server verifies it once at startup and its
run_validate/run_test/run_audit runners forward the same file, and
run_tests uses its grants. No tool argument can name, create or change
a policy; an edited project still invalidates it until the operator
reviews and replaces the file.

An empty, unparseable or missing policy file is now named (#847).
bootstrap no longer offers the site's data/ directory for serving, and its
serving hint says operator code, credentials and data stay outside the
project.

The #843 proof now runs its connected MCP workflow: run_validate and
run_test pass on the packed site with only the operator's flags.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
jimhoyd added a commit that referenced this pull request Sep 28, 2026
)

* proof(#843): embedded Better Auth in one local URLCode site

Adds proofs/private-requests: a packed-consumer site where Better Auth
1.7.6 on node:sqlite owns accounts, sessions and its tables, an operator
adapter mounts its native handler and turns its verified session into
the request principal and #840's request-bound `identity` capability,
and ordinary function routes implement owner-private requests and one
reviewer-only approval with parameterized SQL.

The adapter refuses before serving on a missing secret, an uninitialized
schema, an origin or basePath mismatch, telemetry left on, or a disabled
rate limiter; forwards only an exact allowlist of Better Auth paths
(disabledPaths misses parametric routes); and supplies the admitted client
address for throttling in a header it always overwrites.

test/private-requests.integration.ts (npm run test:proof) packs this
checkout, installs it into a copy of the site, and exercises setup,
explicit policy approval, provider/target refusals, declarative fixtures
with audit ready, and the HTTP flow including foreign-owner and reviewer
denial, malformed input, concurrent approval, cancellation, sign-out,
revocation and throttling.

Core: explain/report state that an extension mount's subpaths are
provider-defined and not inspected, and list the capabilities a
registration declares on the routes it protects.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* ci: run the embedded Better Auth proof with the packed add-on integration

verify:workspaces and the workspace-integration job now run
npm run test:proof after test:addons. The proof packs the already-built
core with --ignore-scripts, like the other packaging tests, so prepare
never rebuilds dist under another test. Changes under proofs/ or to the
proof's test are high-impact, so they select that job on a pull request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(proof): stop the server before removing the site, run npm without a shell

On Windows the proof's behavior passed but teardown did not: separate
t.after hooks run in registration order, so the temp site was removed
while the served runtime still held its SQLite files (EBUSY), the
server-stop hook never ran, and the live child kept the test process
alive until the job timeout. One unwind stack now attempts every
closer, newest first. npm runs through npm_execpath under this Node, so
Windows needs no shell (DEP0190).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(cli,mcp): the reviewed operator policy reaches inspection and authoring (#834)

bootstrap, context, explain, review, plan-feature and mcp accept the
operator's --policy. A verified policy pins the host file to its reviewed
projectSha256, so no PROJECT_SHA256 export is needed; bootstrap and
context repeat it in their emitted commands and drop it from
prerequisites. The MCP server verifies it once at startup and its
run_validate/run_test/run_audit runners forward the same file, and
run_tests uses its grants. No tool argument can name, create or change
a policy; an edited project still invalidates it until the operator
reviews and replaces the file.

An empty, unparseable or missing policy file is now named (#847).
bootstrap no longer offers the site's data/ directory for serving, and its
serving hint says operator code, credentials and data stay outside the
project.

The #843 proof now runs its connected MCP workflow: run_validate and
run_test pass on the packed site with only the operator's flags.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* wip: authoring MCP on the official SDK (#846)

* docs(tooling): the authoring MCP protocol is the official SDK's (#846)

Describe what the SDK now owns (lifecycle, framing, version negotiation,
JSON-RPC errors) and what URLCode keeps (tools, argument validation,
in-order calls, the 1 MiB bounds), and how malformed input, oversized
messages, closing stdin and client cancellation behave.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.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.

[Request]: Stage B — settle the generic application-provider handoff contract (epic #837)

1 participant