Skip to content

feat(server): open the identity broker session before any change - #2606

Merged
ChrisJBurns merged 4 commits into
mainfrom
feat/identitybroker-session-open-server
Oct 29, 2026
Merged

ChrisJBurns merged 4 commits into
mainfrom
feat/identitybroker-session-open-server

Conversation

@ChrisJBurns

@ChrisJBurns ChrisJBurns commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Server side of #2492 (the broker side merged in #2593 and #2601): mecak8s opens the identity broker session, which converts the user's AS-1 access token, before a request changes anything, and maps the broker's refusals to client outcomes from the design's error-reasons table.

  • Open before any change, on every path.

    • Create, fork, and clear open before saving (clear before cancelling the source run).
    • Prompt opens before its repair saves.
    • MCP authorization cancel and recheck open before touching the MCP broker or saving.
    • Retry, approval, run ask, plan ask, compact, and team runs already opened first and are now pinned by tests.
  • A token is presented at most once per request. The first open that presents it takes it from the request, so later opens carry none, and a stream drops it at its first response. A conversion lost to a closed session in the same request becomes the tagged 401 only before anything changed.

  • Create, fork, and clear open before saving instead of rolling back (the ticket's fallback). Rollback cannot be made safe: deleting a session may be unsupported or fail, its title event has already reached live watchers, and a cleared run cannot be restored. A broker session opened for a successor that is never saved is closed by its broker ID, so a lost create race never closes the winner's session. Registration errors after the save are logged and retried at run entry, not returned, so a client retry cannot create a duplicate.

  • Error mapping:

    Broker refusal at open Client sees
    token_used, subject_invalid bearer_rejected, 401 with invalid_token challenge; mecatui refreshes and resends once
    signed_out, upstream_revoked, owner_action_required sign_in_required, 401 with the challenge; mecatui opens Reauthenticate
    owner_mismatch, session_owned identity_broker_session_refused, 403 (was 500)
    unavailable identity_broker_unavailable, 503 (unchanged)

    The authenticator's own REST 401 now carries the bearer_rejected problem code, so the code means the same on gRPC and REST.

  • app.Config.IdentityBrokerAS1 is exported, without a flag, so TestIdentityBrokerAgainstOfflineHarness runs through real startup. Host AS-1 in mecak8s #2490 fills it from operator configuration.

  • Guard against a forgotten early open: the three late opens (prompt run start and both MCP continuations) fail with an internal error if the request still holds a token that would be presented, so a path that changes state before opening fails loudly in tests. Client-visible refusal text is fixed per error; the broker's reason stays in diagnostics.

  • Docs: the caller identity page (user-docs/features/security-and-execution/caller-identity.md) and the HTTP/SSE reference's authentication errors table (user-docs/reference/http-sse-api.md, now with each code and what to do); SDK error list and generated references updated.

Known edge, documented in code: a create whose host session ID already has a broker session for another incarnation (a cross-replica race only) skips the early open, and the open after the save then only logs a refusal.

Issue relationship

Closes #2492

API compatibility

  • Assessment: additive; API major stays 1; no feature identifier.
  • New error codes (open strings): bearer_rejected, sign_in_required, identity_broker_session_refused.
  • bearer_rejected keeps its contract. It now also comes from handlers, but only before the request changed anything, and each request presents its token at most once, so clients' existing refresh-and-retry stays correct.
  • Authenticator REST 401 code changes from internal to bearer_rejected, for a missing as well as a rejected bearer. Status and WWW-Authenticate are unchanged, and gRPC already carried bearer_rejected. Codes are open strings and 401s were always distinguishable by status.
  • Pairing refusals move from 500 internal to 403. Still a failure for any client.
  • Older clients treat sign_in_required as a rejected credential. mecatui before this change shows "The server rejected this credential. Re-login is disabled for this target; check issuer, audience, and CA settings." and offers no sign-in from the connect panel, which points the user at configuration instead. They must run mecatui login <target> by hand or upgrade. Not a break (the request still fails safely), but a worse experience until they upgrade.
  • Gated: everything sits behind --enable-identity-broker, and token conversion needs Config.IdentityBrokerAS1, which no deployment sets until Host AS-1 in mecak8s #2490. Under the flag, a create, fork, or clear carrying a token now fails with a retryable 503 when the broker is unreachable, saving nothing (before, it succeeded and the run failed), and a clear whose token is refused no longer cancels the source's run.
  • Upgrade notes: SDK users get three new codes in MECATL_ERROR_CODES (the SDK's AuthenticationError does not expose code yet; follow-up). REST clients should key on the invalid_token challenge or the bearer_rejected code for refresh-and-retry.

Test plan

  • One test per open path: a refused token leaves the store, event log, and run side effects unchanged, presents the token once, and the same request then succeeds with an accepted token
  • Lost create race across two replicas, clear with a running source, clear closing an unsaved successor's broker session, MCP expiry and pending polls, a refusal after work started staying untagged, stream and HTTP token drop, mapping table, gRPC/REST parity, TUI mapping, and the extended offline harness test
  • Mutation check: removing each fix fails its test
  • Panel review (spec, standards, test adequacy, security, architecture, reuse, duplication): blocker and important findings fixed
  • go test -race on internal/adapter/server, internal/identitybroker/..., internal/app, cmd/mecak8s, cmd/mecatui/...: only failures that also occur on unmodified main on this machine
  • task lint: clean except the pre-existing gosec finding in environment/microvm/launch_ownership_darwin.go
  • matlatl check user-docs --strict: no new findings
  • task site:build: not run locally (pnpm toolchain); CI covers it

mecak8s now opens the identity broker session, which converts the
user's AS-1 access token, before a request changes anything: create,
fork, and clear open before saving (clear before cancelling the source
run), prompt before its repair saves, and MCP authorization cancel and
recheck before touching the MCP broker. A request presents its token at
most once, and a stream drops it at its first response. A broker
session opened for a successor that is never saved is closed by ID.

Broker refusals reach clients from the design's error reasons:
token_used and subject_invalid as bearer_rejected (401, invalid_token
challenge), revoked sign-ins as sign_in_required (mecatui asks to sign
in again), and pairing refusals as identity_broker_session_refused
(403). The authenticator's REST 401 now carries the bearer_rejected
code too. app.Config.IdentityBrokerAS1 is exported for #2490.

Closes #2492.

Co-authored-by: Mecatl <[email protected]>
ChrisJBurns and others added 2 commits October 29, 2026 16:48
A request's first broker open now takes the access token whether or not
it presents it, so a later open in the same request can never present a
token an earlier request already converted and return a tagged 401
after a write. The broker's reasons map to the new client errors only
for the session open; registration keeps its mapping (#2552). An
explicit-ID create whose ID another owner's broker session holds gets
the same answer as a plain ID collision. Adds a lost-create-race test
with a shared broker.

Co-authored-by: Mecatl <[email protected]>

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 80b4cac. The retained-token fix (the first open now takes the token whether or not it presents it) closes the race I had been checking: a prompt that reuses a broker session which already holds a refresh token can no longer present the token at a later open after the request saved something. The shared-broker lost-create-race test also covers close-by-ID.

Still open, as inline comments:

  • The two MCP broker-state-loss branches settle the authorization without any open (mcp_authorization.go).
  • Test gaps: the MCP refusal test cannot see event-log changes, no test sends a user token into a scheduled session, fork's close of an unsaved broker session is untested, and one assertion accepts any untagged error. One test leaks its observer goroutine when it fails early.
  • Two follow-ups on structure: open-before-change is enforced by hand at six call sites, and clear's cleanup depends on a return-value convention.
  • Docs wording in caller-identity.md.

Two points that have no diff line to anchor to:

  • user-docs/reference/http-sse-api.md does not describe bearer_rejected, sign_in_required and identity_broker_session_refused, or what a client should do for each.
  • The "Older clients" bullet in the PR description is milder than what happens. Older mecatui maps sign_in_required to its rejected-credential path (cmd/mecatui/ui/connect.go:138, :294). It tells the user to check issuer, audience and CA settings and disables re-login from the connect panel. The user has to run mecatui login by hand or upgrade. That is still not a break, but the note should say so.

I read the code and did not run tests.

Comment thread internal/adapter/server/service.go Resolved
The MCP recheck and cancel branches that settle an authorization after
the MCP broker lost its state now open the identity broker session
first, like every other path that changes state. A guard at the three
late opens (prompt run start and both MCP continuations) fails loudly
if a path changed state before its early open. Client-visible broker
refusals use fixed text; the reason stays in diagnostics.

Tests: event log in the MCP refusal fixture, the broker-state-loss
branches, fork closing an unsaved successor's broker session, the
guard, fixed error text, and tighter assertions. Docs: authentication
error codes in the HTTP/SSE reference and the caller identity page.

Co-authored-by: Mecatl <[email protected]>
@ChrisJBurns
ChrisJBurns merged commit aa2e94a into main Oct 29, 2026
45 checks passed
@ChrisJBurns
ChrisJBurns deleted the feat/identitybroker-session-open-server branch October 29, 2026 18:19
jbeda added a commit that referenced this pull request Oct 29, 2026
Co-authored-by: Mecatl <[email protected]>

* origin/main:
  ci: add a stable omnibus CI gate (#2611)
  feat(server): open the identity broker session before any change (#2606)
  fix(learning): fit manual synchronous reflection to request budgets (#2600)
This was referenced Nov 11, 2026

This branch was successfully deployed

1 active deployment
Preview — 4baf0dda Deployed Oct 29, 2026 by vercel[bot]
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.

Convert the sign-in at session open

2 participants