Skip to content

feat(auth): controller-scoped MCP key — creator-tier, attributable, refused on grant surfaces - #3762

Draft
trinity-ability wants to merge 1 commit into
devfrom
feature/ent693-controller-scope-key
Draft

trinity-ability wants to merge 1 commit into
devfrom
feature/ent693-controller-scope-key

Conversation

@trinity-ability

Copy link
Copy Markdown
Contributor

⚠️ Before merge

  • Rebase onto the ent#662 PR and renumber the Alembic revision: 0099_execution_source_mcp_scope → 0100_execution_source_mcp_scope with down_revision = "0099_agent_peer_skill_grants" (ent#662 takes 0099 off 0098_proactive_consent_not_asked). Update the docstring Revision ID / Revises, the SQLite migration docstring that names the revision, and confirm python3 scripts/ci/check_alembic_heads.py src/backend/migrations/versions reports one head.
  • Merge after: 1.0.0 and the ent#662 PR.

Summary

Adds a fourth machine scope, controller, for a remote controller (fleet-facing automation that provisions and operates an instance from outside it). Today such a controller has two wrong options. An agent key is refused at admin gates and bound to one container. A user key is the person: it passes every admin gate on an admin owner, and every write is recorded as that human.

Related: abilityai/trinity-enterprise#693 (companion: abilityai/trinity-enterprise#692)

Scope semantics

Minting — POST /api/mcp/keys with {"scope": "controller"}. Admin only, and only from a signed-in session (require_interactive + assert_admin), the same as ops. A key can never mint one. The key is minted against the caller's account, so mint it under a dedicated service account. It never carries an agent_name.

Reach (creator-tier, not admin-blanket)

Refused on grant surfaces — _enforce_controller_key_fence in get_current_user runs before any route gate and answers 403 {"code": "controller_grant_refused", "surface": …}:

surface routes
key_minting /api/mcp/keys/**, /api/agents/{n}/mcp-key/**
permissions agent permission writes, share/unshare, capability-grant PUT, user role PUT, email-whitelist add/remove, canvas share links
credential_values credentials inject/export/import, encryption key, GitHub PAT writes (agent + user), POST /api/subscriptions, Nevermined config POST/DELETE, webhook signing-secret mint/revoke
system_teardown DELETE /api/enterprise/system-teardown/{name}
destructive_delete DELETE /api/agents/{name}, a soft delete whose workspace/shared/public volumes are purged after retention. Anchored on the agent itself, so sub-resources stay reachable

A controller may name a credential (status, requirements reads pass) but may never read or set one.

Why a denylist, and the human-only backstop

The Operator ruling ships the controller ungated inside the instance, so its reach is deliberately broad, and an allowlist of what it may reach would be the whole API. The fence therefore names the grant surfaces. Three layers keep a denylist from failing open:

  1. Allowlisted primitives. require_person / require_interactive and the ADMIN_GATE_SCOPES allowlist never contain controller, so any route gated by them (including every new grant route classified under the bug(auth): agent-scoped keys can toggle their own agent's autonomy — PUT /api/agents/{name}/autonomy lacks reject_agent_principal #2996 census) refuses it with no edit here.
  2. Live-route guard (new). test_every_grant_shaped_route_refuses_a_controller imports the real app (the bug(auth): agent-scoped keys can toggle their own agent's autonomy — PUT /api/agents/{name}/autonomy lacks reject_agent_principal #2996 census subprocess; it fails, never skips). Every route whose shape is key / permission / credential-value must either carry a gate the controller is refused by, or have a fence entry. Its first run found five ownership-only routes the fence was missing (webhook secret POST/DELETE, email-whitelist POST, canvas share POST, Nevermined config DELETE); all five are now fenced.
  3. Stale-entry guard. Every fence entry must match a live route, because a stale deny entry fails open. The teardown route is exempt because it lives in the separately licensed module.

Attribution (AC 2: partial)

  • Audit rows: actor_type="controller", actor_id=<key id>, mcp_scope="controller", key name, and the owner as actor_email. Never user, so the owner's activity view doesn't show the controller's acts as theirs.
  • Execution rows: new nullable schedule_executions.source_mcp_scope, read from the key row inside the INSERT so it survives key deletion. Execution detail shows Controller: .
  • Skill-assignment history: agent_skills.assigned_by / agent_skill_sets.assigned_by record controller:<key id>:<key name> (dependencies.assignment_author) on every assignment write: single assign, bulk replace, set assign/replace/unassign. Every other principal keeps its username, so existing rows and readers are unchanged and no schema change is needed.
  • Agent activity rows: NOT done. Deferred (accepted), so AC 2 is partially met.

Migrations

One additive nullable column, schedule_executions.source_mcp_scope, with no backfill. Both tracks: SQLite execution_source_mcp_scope in db/migrations.py, and Alembic 0099_execution_source_mcp_scope (renumber before merge, see the top). schema.py and tables.py are updated. Alembic heads: one head.

Deferred

  • MCP tool exposure (OPERATOR_SCOPES / access.ts). The key gets no MCP tools until then (fail-closed); it's a REST credential for now. Kept out of this PR to avoid colliding with ent#662.
  • Controller attribution in agent activity rows (partial AC 2, above).
  • /ws/events admission (refused today).
  • The agent-key verify verdict for a controller key found in an agent's .mcp.json (reports it as a personal user key).
  • Admin-route opt-ins (with Config Validation: 5 critical config issues found (TRINITY_PASSWORD P0, 4x undocumented vars) #692).
  • The webhook URL token (POST …/webhook) stays reachable as schedule management; only the signing secret is fenced.

Test evidence

  • tests/unit/test_ent693_controller_scope_key.py: 93 passed. Driven through the real gates: the real get_current_user with a key minted into the real schema and presented as a bearer, the real assign route via TestClient, the real audit and execution writers, and the live route table.
  • Red first: 61 failures before the first implementation, and 13 before this round's additions (assignment attribution, destructive_delete, new fence entries, the guard).
  • Mutation check: 13 production changes reverted one at a time, and each turned tests red: the fence call; capability admission; the audit branch; the execution INSERT; the DB mint scope; the router mint scope; the admin named code; the SQLite migration registration; assignment_author; the single-assign wiring; the set-assign wiring; the destructive_delete entry; the webhook-secret entry.
  • Guards (test_293, test_ent629_agent_reach, test_1310_auth_consolidation/wiring, test_2996_human_only_routes + runtime census, test_186, test_2323, test_3102, test_mcp_key_creation_requires_session, test_schema_parity, test_migrations) plus all skills/canvas/webhook/whitelist/nevermined unit files: 1876 passed, 1 failed, 1 skipped. The one failure is test_1310_auth_consolidation::test_admin_sites_admit_admin[avatar.generate_default_avatars], which fails identically on dev: it writes to /data, which is read-only on macOS.
  • Every unit file touching the audit service, execution writers or capability check: 4257 passed, 4 skipped (before this round's changes).
  • Not run: MCP-server tsc (no local node_modules in the worktree; the change is one added value in a type union) and the frontend unit suite (raw-color class counts are unchanged in both touched files).

🤖 Generated with Claude Code

…efused on grant surfaces

Adds a fourth machine scope, `controller`, for a remote controller that
provisions and operates an instance from outside it
(Abilityai/trinity-enterprise#693).

- Reach: passes require_role("creator"), the owner/access tier and the agent
  capability gates (Operator tier). Kept out of ADMIN_GATE_SCOPES; an admin
  route admits it only via allow_scopes={"controller"} (named code
  controller_route_not_admitted otherwise; role == admin still enforced).
- Refused at the auth entry point on grant surfaces with
  controller_grant_refused + surface: key_minting, permissions,
  credential_values, system_teardown, destructive_delete (agent delete).
- Backstop guard walks the live route table and fails any grant-shaped route
  with neither a refusing gate nor a fence entry.
- Attribution: audit rows actor_type=controller / actor_id=key id; execution
  rows source_mcp_scope (new nullable column, SQLite + Alembic 0099);
  skill-assignment history assigned_by=controller:<key id>:<key name>.
- Minting: admin + signed-in session only, like ops.

Agent activity attribution and MCP tool exposure are deferred.

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

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

⚠️ Alembic head fork if this PR is merged into dev.

dev has advanced since this PR's checks last ran. GitHub recomputes the merge ref when the base moves but does not re-trigger workflows, so a green schema-parity here can describe a base that no longer exists (#2533).

scripts/ci/check_alembic_heads.py against dev + this PR, merged in memory
alembic-heads: FAIL — src/backend/migrations/versions resolves to 2 heads across 128 revision(s); exactly 1 is required.
  • 0099_execution_source_mcp_scope  (0099_execution_source_mcp_scope.py)
  • 0126_schedule_email_results  (0126_schedule_email_results.py)

They fork at: 0098_proactive_consent_not_asked  (0098_proactive_consent_not_asked.py)

`alembic upgrade head` is singular and resolves its target BEFORE applying anything,
so this graph applies ZERO revisions — every revision since the fork stops arriving,
not only the one that forked. Fix by chaining the newer revision off the real head,
or — if the forked revision may already be applied somewhere — by adding a merge
revision (`alembic merge -m "…" <head-a> <head-b>`), whose tuple `down_revision`
converges the line from any starting state. See Architectural Invariant #3.
alembic-heads: src/backend/enterprise/backend/migrations/versions — version directory absent (submodule not initialised) — skipped.

Evaluated on the version line only — this PR also conflicts with dev in 12 unrelated file(s), which do not change this verdict but must be resolved before merge:

docs/memory/architecture/security.md
docs/memory/feature-flows/mcp-api-keys.md
docs/memory/requirements/security.md
src/backend/db/mcp_keys.py
src/backend/db/migrations.py
src/backend/dependencies.py
src/backend/models.py
src/backend/routers/mcp_keys.py
src/backend/routers/skills.py
src/frontend/src/components/settings/McpKeysTab.vue
src/mcp-server/src/types.ts
tests/registry.json

Fix: rechain this PR's revision off 0126_schedule_email_results (the current dev head), or — if the forked revision may already be applied somewhere — add a merge revision (alembic merge -m "…" 0099_execution_source_mcp_scope 0126_schedule_email_results), whose tuple down_revision converges the line from any starting state. See Architectural Invariant #3.

Push the fix and schema-parity re-checks it against the merge ref immediately; this comment clears on the next push to dev touching src/backend/migrations/versions/**.

Advisory — this check does not block merge. · head_sha: 2cac23e8bf6e8a8a5bbc540df54297f31f5443f0 · run

@github-actions

Copy link
Copy Markdown

⚠️ Nightly unit-suite check skipped — merge conflict against dev.

Resolve by running git merge dev locally and pushing the result. The next nightly run will re-test once the conflict is gone.

@github-actions

Copy link
Copy Markdown

⚠️ Live-instance suite skipped — merge conflict against dev.

Resolve by merging dev locally and pushing the result; the next nightly re-tests.

This branch has not been deployed

No deployments
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