Skip to content

feat(server): derive org + agent from auth in create_conversation_session#98

Merged
brentrager merged 1 commit into
mainfrom
create-session-derive-org-agent
Jun 25, 2026
Merged

feat(server): derive org + agent from auth in create_conversation_session#98
brentrager merged 1 commit into
mainfrom
create-session-derive-org-agent

Conversation

@brentrager

Copy link
Copy Markdown
Contributor

Problem

handle_create_session hard-coded organization_id = SEED_ORG_ID ("reference-org") and defaulted the agent id to a random UUID. That stub blocks multi-tenant chat: a relational host (SmooAI) has FKs requiring a real org + agent, so the session was being created under a bogus org with a random agent id. This was a known TODO in the handler (// A multi-tenant deployment derives this from auth.).

Solution

Derive the session's organization_id from the authenticated request, in priority order:

  1. Widget policy orgAgentWidgetAuth gains a new optional organization_id field. Widget visitors authenticate via origin + authContext (not a JWT), so their org rides on the agent's embed policy. A host policy service (or WIDGET_AUTH_URL) supplies it.
  2. JWT principal org — the connection's authenticated principal org_id is now threaded from the /ws handshake (resolve_ws_accessconnection_loophandle_framehandle_create_session) instead of being dropped when the Principal is reduced to an AccessContext (which only carries user_id + groups).
  3. Seed org — behavior-preserving fallback for the no-auth/local/dev flavor, so the in-memory + conformance + scenario flows are unchanged.

agent_id already came from the inbound agentId payload and is threaded onto the conversation/participants/session — unchanged.

The same JWT-org-then-configured-org derivation is applied to the lambda dispatch create-session path (resolve_frame_org, falling back to config.org_id).

Why widget-auth carries the org

AgentWidgetAuth previously only held allowed_origins + public_key — it did not carry the agent's org, and widget visitors don't present a JWT. Rather than force it, the change adds an additive, serde-default organization_id so a multi-tenant host can supply it; absent it, derivation falls through to the JWT principal, then the seed org.

Tests

New create_session_org_agent.rs:

  • authed principal org X + payload agent Y → persisted conversation + both participants carry org X (not seed), agent participant internal_id = Y, session agent_id = Y.
  • widget policy org takes precedence over the connection's JWT org.
  • seed fallback — no auth org, no widget-policy org → conversation belongs to SEED_ORG_ID (behavior preserved).

Gates

  • cargo build --manifest-path rust/Cargo.toml
  • cargo test -p smooai-smooth-operator-server ✅ (95 passed, incl. 3 new)
  • cargo test -p smooai-smooth-operator -p smooai-smooth-operator-lambda
  • cargo fmt --check
  • cargo clippy -p smooai-smooth-operator-server / -lambda / smooth-operator -- -D warnings ✅ (No issues found)

🤖 Generated with Claude Code

@changeset-bot

changeset-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 47866dc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@smooai/smooth-operator Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

…sion

handle_create_session hard-coded SEED_ORG_ID + a random agent id, which
blocks multi-tenant chat: a relational host (SmooAI) has FKs requiring a
real org + agent, so the stub injected "reference-org" + a random agent.

Derive the session's organization_id from the authenticated request, in
priority order:
  1. the agent's widget-auth policy organization_id (new optional
     AgentWidgetAuth.organization_id) — widget visitors authenticate via
     origin/authContext, not a JWT, so their org rides on the agent policy;
  2. the connection's authenticated JWT principal org — now threaded from
     the /ws handshake through handle_frame instead of being dropped at the
     AccessContext reduction;
  3. the seed org as a behavior-preserving fallback for the no-auth/local
     flavor.

agent_id continues to come from the inbound agentId payload. Same
JWT-org-then-configured-org derivation applied to the lambda dispatch
create-session path. Existing in-memory/seed flows unchanged.

Adds create_session_org_agent.rs proving authed org X + payload agent Y
land on the persisted conversation/participants/session, plus the
widget-policy-org precedence and the seed-fallback cases.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@brentrager
brentrager force-pushed the create-session-derive-org-agent branch from 282b30f to 47866dc Compare June 25, 2026 18:54
@brentrager
brentrager merged commit bdbf868 into main Jun 25, 2026
1 check passed
brentrager added a commit that referenced this pull request Jul 22, 2026
…327)

Heals the .NET server build on main after #323: the IToolHook seam needs the
core release that carries it (core #96/#97/#98, published as 1.7.1).


Claude-Session: https://claude.ai/code/session_01Ji3rA65cTfU9vPQh9HAASJ

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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