Skip to content

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

Closed

Description

@jimhoyd

Problem or use case

Epic #837 Stage B ("Define the generic handoff") needs a contract PR that settles the section-12 decision table before core/extension implementation starts. This issue tracks settling those decisions; implementation is a separate, subsequent PR once the decisions below are agreed.

Per #837's own inventory (see comment): every typed exports contract (AuthExports, StoreExports, FormsExports, MailExports, AuditExports, AbuseExports) is wired exclusively through HostContext.get inside composeHost, extension-to-extension, resolved once at host() time. The one carve-out is auth's sessionUserId() — a narrow, one-field, one-way x-urlcode-context-* header projection. Stage B has to decide how ordinary function/middleware routes get a typed, request-scoped, capability-limited accessor without core importing any extension.

Decisions to settle (from #837 §12, adopting its recommended starting points as the working baseline)

Decision Working baseline Revisit only when
Distribution Extension-owned facades + a generic core handoff; no mandatory new adapter package A shared package demonstrably reduces duplication without pulling extensions into core
Application exposure Explicit per-route/operation logical bindings using existing schema/policy metadata where feasible Existing mechanisms can't represent the contract cleanly
Principal binding Bind internally from the verified invocation; expose only minimal read-only facts by default A separate reviewed actor mode is required
Sandbox Pure projections only; refuse host-backed facades before serving A bounded RPC design has its own contract and adversarial fixtures
Custom auth wire format Keep native lifecycle by default; #830 stays auth-owned A fixed external API needs a reviewed auth-owned mapping

Also settle, per #837 §17 ("gaps found in plan review") — priority: settle these four before publishing the generalized binding API:

  1. Transitive authority/policy parity — when one facade calls another service, which policies apply (ownership, quota, audit, recipient checks)? Define caller attribution/delegation; an explicit target user must never replace the acting principal.
  2. Aggregate resource budgets/backpressure — deadlines, bounded concurrency, result/page/byte limits, queue saturation for host-backed facades; how nested calls share budgets.
  3. Discovery content as untrusted input — provenance/revision pins identify content but don't make its prose safe instructions; inert discovery vs. trusted host-module execution must be distinguishable.
  4. Cross-transport value semantics — the portable value subset shared across HTTP/MCP/other targets (absent vs. null, timestamp/decimal/bigint, byte payloads, pagination tokens, error serialization).

Concrete mechanism question this issue must answer

Given the sessionUserId precedent (one-way header projection, one field, no verification burden on core), choose one:

  • (a) Generalize the header-projection pattern per declared capability (core stays purely a byte-forwarder; each extension defines its own projected fields), or
  • (b) Give ordinary routes a typed, request-scoped accessor equivalent to HostContext.get, scoped to the route's own declared capability grants (core owns binding/lifecycle/refusal; extensions still own their facade's behavior).

Exit evidence (per #837's Stage B row): an external test provider works without a core import of any first-party extension; two simultaneous users/requests stay isolated; missing/unsupported/reload cases fail deterministically before serving.

Depends on

Alternatives

Do nothing further and leave sessionUserId as the only precedent — rejected by the epic's own scope (§1, §5) which calls for closing this gap across auth/store/forms/mail/audit/abuse, not just auth.

Activity

  1. jimhoyd commented on Sep 28, 2026

    @jimhoyd
    CollaboratorAuthor

    Decision (proceeding with implementation)

    Choosing mechanism (b): a typed, request-scoped accessor equivalent to HostContext.get, scoped to the route's own declared capability grants — not a generalized version of the header-projection trick.

    Reasoning: the header-projection channel (x-urlcode-context-*) only works for bounded, string-serializable, pre-computed-at-authorize-time facts (that's what sessionUserId is). Store/mail/audit/abuse facades need live method calls bound to the current invocation (records.update(id, {ifMatch}), mail.send(...)), which can't be precomputed into a header before the handler runs. Generalizing (a) would mean re-inventing RPC-over-headers for every capability; (b) reuses the exact lifecycle/activation machinery HostContext.get already has, just re-scoped from "another extension's host()" to "this route's declared grants, resolved per invocation."

    Concrete shape landing in the Stage B PR

    • Core stays capability-blind. packages/core/src/extensions.ts gains a generic ApplicationProvider contract and an InvocationContext (request id, transport, route/operation, principal {provider, id} | null, cancellation/deadline). Core never imports auth/store/etc.; it only knows "an extension registered a provider under capability name X" and "this route declared it wants X."
    • Extensions opt in explicitly. ExtensionDefinition.host() may return an additional provide?: (capability: string) => ApplicationProvider | undefined alongside exports. Nothing changes for extensions that don't provide one.
    • Routes opt in explicitly. New route-level YAML field (name TBD in the PR, e.g. capabilities:) lists declared capability names, analogous to how extensions already declare requires/uses — reusing that existing metadata shape rather than inventing new syntax where avoidable.
    • Binding happens per invocation, not per activation. Core resolves each declared capability at request time, calls provide(name).forInvocation(ctx), and only then hands the bound facade to the handler via context. A missing/incompatible/denied capability refuses before the handler runs (not a thrown exception mid-handler).
    • Sandbox refuses non-pure capabilities up front. sandbox: true + a declared capability that isn't marked as a pure projection fails activation, not silently at first call.
    • Exit evidence delivered as a fixture, not prose: an external test-provider extension (lives in a test fixture, not a first-party package) registers one capability; a route consumes it; tests assert (1) no first-party import appears anywhere in core for this to work, (2) two concurrent invocations get isolated bound facades, (3) a route declaring an unregistered/unsupported/sandboxed-refused capability fails deterministically before serving, (4) reload/close tears down cleanly with no stale bound handle usable afterward.

    This generalizes the identity precedent without deciding auth/store's own facade method names yet — those stay Stage C's job. Implementation starting now on a branch off main; will open the contract PR referencing this issue and #837.

  2. jimhoyd commented on Sep 28, 2026

    @jimhoyd
    CollaboratorAuthor

    Implementation landing

    The generic handoff described above is implemented, tested and documented; opening the contract PR against main next (branch claude/epic-837-stage-b-handoff).

    What shipped, generalizing sessionUserId's one-way header projection into a live, per-invocation call, with zero new YAML:

    • RuntimeExtension.capabilities?: readonly string[] — a registration's pure availability signal (parallel to providesPrincipal/streams), validated at prepareExtensions (≤32 distinct lowercase names).
    • ExtensionInstance.provide?(capability, invocation): unknown|Promise<unknown> — called once per declared name, per request, only for an extension the route already names in policies.extensions (so it's still gated by that extension's own authorize()/middleware() — no side door around existing admission, per the transitive-authority-parity gap in §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 the route's own context.capabilities.<extension>.<capability>, built right after FunctionContext, alongside context.signal.
    • Sandbox refused at compile time, not per request: sandbox: true + a route naming a capability-declaring extension fails route compilation with capability-in-sandbox, the same place and pattern as the existing stream-in-sandbox check — a live object can't cross the QuickJS worker's JSON boundary.
    • provide() returning undefined just omits that one entry (e.g., an anonymous request asking for a capability that needs a principal) — not an error.

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

    Docs: new "Request-bound capabilities" section in EXTENSIONS.md and a RIM-EXT-CAPABILITY-001 contract card in RUNTIME-IMPLEMENTATION.md.

    Deliberately left out of this PR (Stage C+ per the epic's sequencing): no store/auth/mail/audit/abuse package actually declares a capability yet — that's each package's own facade-naming decision. The other three priority gaps from §17 (aggregate budgets/backpressure, discovery-as-untrusted-input, cross-transport value semantics) are also out of scope here; they don't block this mechanism and can land incrementally.

  3. jimhoyd commented on Sep 28, 2026

    @jimhoyd
    CollaboratorAuthor

    Implementation opened as PR #840; npm run verify passes in full, auto-merge enabled — will land once CI is green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions