Delegation: role bindings, groups and preset roles (twin of cloud#6665)

Merged
KH
7d ago
+3426-88

What

The relay-control twin of AgentWorkforce/cloud#6665. It includes Cloud's review fixes 784b9415a, 6ecf6934f, c1de4d1f1, f37fb2a14 and 4acae109e.

Delegation becomes an IAM-like table:

  • who: one agent, a custom group, a connector kind, or every Relay agent;
  • which role: Trusted, Standard, Read-only, a custom role, or excluded;
  • what scope: everything the owner has standing over, or some repos;
  • until when: no expiry by default.

Built to Khaliq's answers in #owner-decisions 251526750352171008.

This PR carries migration 0099 and an operator data step (the role seed). Do not merge, migrate or deploy without Khaliq's direct go. merge-train:hold stays on.

Same as Cloud

  • Presets (src/delegation/roles.ts):

    PresetClassesAsk firstLimits
    Trustedmerge, pr_ops, external_comms, deploy, release, destructive, secret_change, spenddeploy, release, destructive, secret_change, spendmerge requires ci_green and mergeable
    Standardpr_ops, merge, deploy, destructive, secret_changemerge, deploy, destructive, secret_changenone
    Read-onlypr_opsnonecomment only

    A parity test pins the presets and the class descriptions to Cloud's strings.

  • Precedence: explicit grant, then agent binding, then custom group (the strictest of two), then connector kind, then Relay agents. Nothing is unioned.

    • An excluded binding gives nothing and never falls through.
    • Two groups with no overlapping classes or repos cover nothing (coversNothing), never every repo.
  • No inheritance: a launched agent gets Read-only, within its root binding's repos and expiry, unless its own group or binding says otherwise.

  • Legacy rules: a legacy rule keeps its old meaning until it is converted.

    • Conversion happens through the role seed (dry run by default) or a PATCH.
    • Conversion carries the rule's owner_excluded exclusions into excluded agent bindings.
    • A legacy rule and a Relay agents binding never stand together: each refuses the other with 409 binding_exists.
  • Re-resolution: every binding, group or exclusion write re-resolves the owner's system grants at once, under an owner-scoped advisory lock. A materialization takes that lock shared and decides under it. A new grant is dated no earlier than:

    • its bindings' creation and changes;
    • a group member's joining;
    • any earlier grant's end.
  • Answer 3: a Tier D approval from another owner's grant is refused with tier_o_required for a class the acting agent must ask its own owner about. This is checked before the deciding transaction and again inside it. approval.redeemed records tier and minter.

  • Routes: delegates/roles (each class with its description), bindings, bindings/{id}, groups, groups/{id}, agents, plus POST /internal/v1/delegation/role-seed. The field names, error codes and recommendation match Cloud's.

  • Admins: an admin acting for another owner may only exclude one agent (or use the existing grant revoke and revoke-all). Deleting a binding or a group, and every group change, is refused with 403.

  • Errors: any thrown error, a 40P01 deadlock included, answers unavailable at attest and redeem.

Where relay-control differs, and why

  1. Required conditions fail closed. relay-control reads no GitHub state, so ci_green and mergeable answer condition_unavailable. A Trusted merge cannot be redeemed through relay-control. Cloud checks them live.
  2. The connector kinds include cursor-cloud. relay-control's classifier knows it and Cloud's does not.
  3. A legacy rule's updated_at does not date new grants. In relay-control a legacy rule's Tier O PUT bumps updated_at and updates its grants in place, as before roles existed. Only a role binding's change dates a new grant. Cloud's decisionSince counts the legacy rule's updated_at too.

Review fixes (mirrors cloud c0b8608fa)

  • New grants never date into a restriction. A new system grant is dated no earlier than ownerChangeFloor, the latest of:
    • each of the owner's rules: its created_at (plain legacy rule) or updated_at (anything else), its revoked_at, and its expires_at once passed;
    • each of the owner's groups: its revoked_at, and each member's added_at and removed_at.
      So removing or outliving a restrictive binding never covers an approval sent while it decided.
  • A deleted exclusion is lifted. A pre-roles exclusion (a revoked grant row) counts only while the agent has never had an agent binding. Conversion carries an old exclusion only for an agent with no agent binding of any status, so a deleted exclusion is not brought back.
  • Exclusions while delegation is off. POST of an excluded binding is allowed while delegation is off.
  • One-click upgrade. The recommendation carries binding_id: the legacy rule's id when one is live (the UI PATCHes it), else null.
  • Every workspace. all_workspaces writes only to the same organization's workspaces, and needs a browser session (400 for a bearer).
  • Rollback quiesce. POST /internal/v1/delegation/role-quiesce (service token, a dry run by default) revokes ALL live rules (legacy included) of every owner who has a role binding, with their grants, reason role_bindings_quiesced, one transaction per owner under the owner lock. An owner with only a plain legacy rule is untouched. It pages at most limit owners per call. The runbook is below; exclusions do not survive a rollback.
  • Routing. The role-seed and role-quiesce endpoints are now reachable (isDelegationPath), tested with the operator credential through the HTTP handler.
  • Response schema. DelegateGrant.scopes uses a response-only DelegationGrantScope that admits empty targets, ops and conditions. A contract test validates a role binding's system grant against it.

Rollback runbook

A build from before roles reads every live rule row as a legacy every-class rule. That includes an exclusion or a Read-only binding. Before rolling back below this build:

  1. Stop writes first. Turn delegation off for the affected workspaces, or hold a read-only window. A binding written after the quiesce survives it and reads as a legacy every-class rule.
  2. Apply the quiesce, with the operator's go: POST /internal/v1/delegation/role-quiesce with the service token and {"apply": true}. A {} dry run first lists every rule an apply would end.
    • Every owner with a live role binding loses ALL their live rules, the legacy rule included, with their grants, in one transaction per owner. A legacy rule left live would be what restricted agents fall through to: every class.
    • An owner with only a plain legacy rule is untouched.
  3. Rerun until a dry run lists nothing. One call handles at most limit owners (default 1000), and the report does not say whether more remain.
  4. Dry-run again right before cutover, and require an empty list. Any row written in between survives and reads as legacy every-class.
  5. Roll back. Any owner who had a role binding has no rule at all now.

Exclusions do not survive a rollback.

  • The old build checks owner_excluded grant rows only for the rule's own id.
  • An exclusion made since roles may have no grant row at all.
  • An owner who turns the legacy rule on again gets a new rule id.

So that owner must revoke the excluded agents' grants again once they reappear.

Migration

0099_delegation_role_bindings.sql is additive:

  • the binding columns, with defaults that read every existing row as a legacy rule;
  • the two group tables;
  • the live-principal index, replacing the live-owner index. It is a superset for legacy rows.

The two tables are added to CONTROL_TABLES. The schema manifest was regenerated with the PostgreSQL 15 dry-run harness; the diff is exactly 0099's objects (30 added, the old index removed). openapi.yaml and docs/owner-delegates.md are updated.

Tests

  • test/delegation-role-bindings.test.ts: parity, precedence, no inheritance, coversNothing and the overlap, the op filter, field rules, the deadlock mapping.
  • test/delegation-role-bindings-postgres.test.ts (real PostgreSQL):
    • precedence and immediate re-resolution, exclusion with no fall-through, no inheritance with group promotion and demotion;
    • two non-overlapping groups, the legacy guards, removal falling through, the duplicate principal race, a write racing a materialize in both orders;
    • the seed (dry run, apply, idempotent, skip), group revoke, legacy exclusions carried, group dating, the locked acting read.
  • test/owner-delegates.test.ts (HTTP):
    • the route contract, exclusion through DELETE /delegates/{id}, the redeem audit minter;
    • the cross-owner Tier D refusal at attest and redeem;
    • admin narrowing only, the legacy and Relay agents 409.
  • Typecheck is clean. The delegation, owner-delegates, migrations and cloud-import-verify suites pass 203/203 with TEST_DATABASE_URL set (0 skipped).

🤖 Generated with Claude Code