openapi: 3.1.0
info:
  title: AtMem governance control API
  version: 1.1.0
  description: >-
    Govern agent identities, memory scopes, access rules and workspace activity.
    Agents and canonical memory run on user infrastructure. Use MCP for governed
    native memory operations and this HTTPS API for workspace administration.
    Account registration and payments require an owner Google or X sign-in.
x-availability: available
servers:
  - url: https://atmem.ai/api/governance
security:
  - bearer: []
paths:
  /v1/setup-keys:
    post:
      summary: Owner issues a management-only agent setup key
      description: >-
        Requires a Google/X owner's Firebase ID token. The client generates the secret
        locally and sends only its digest. Creates the initial workspace automatically
        if necessary. The key can manage owned workspaces, policies and scoped agent keys,
        but cannot manage billing, delete the owner account or directly access native memory.
        Agents cannot issue additional account setup keys; use ordinary scoped keys for memory.
      operationId: issueSetupKey
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              additionalProperties: false
              required: [keyId, keyDigest, expiresAt]
              properties:
                keyId: {type: string, pattern: '^[a-f0-9]{32}$'}
                keyDigest: {type: string, pattern: '^[a-f0-9]{64}$'}
                expiresAt: {type: integer, description: 'Future epoch milliseconds, at most 90 days.'}
      responses:
        '200': {description: Registered setup-key metadata. The raw secret is never returned or stored.}
        '403': {description: Owner authentication required.}
        '409': {description: Key registration conflicts with an existing record.}
        '429': {description: Plan or active-key limit reached.}
  /v1/entitlements:
    get:
      summary: Discover the authenticated account's plan limits without financial credentials
      operationId: readEntitlements
      description: Reports workspace/request/history limits and reset time. No Stripe customer or subscription identifiers are exposed.
      responses:
        '200': {description: Current plan limits and user-owned runtime/data placement.}
  /v1/identity:
    get:
      summary: Inspect the authenticated owner or bound agent identity
      operationId: identity
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/account:
    get:
      summary: Inspect your governance account; owner Firebase token required
      operationId: account
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
    post:
      summary: Register once and start the durable three-day trial
      description: Repeating signup does not reset the trial or revive a deleted account.
      operationId: registerAccount
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
    delete:
      summary: Disable agents immediately and delete governance data in resumable batches
      description: >-
        Owner only. Paid accounts must use /v1/billing/delete-account to cancel
        their subscription first. Repeat until result.complete is true. Retains payment records
        and an anti-abuse account marker; does not delete the shared Firebase
        identity, local memory or third-party data.
      operationId: deleteAccountPage
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              additionalProperties: false
              required: [confirm]
              properties:
                confirm: {const: DELETE GOVERNANCE}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/account/export:
    get:
      summary: Export an owner-bound page; key digests and secrets are excluded
      operationId: exportAccountPage
      parameters:
        - name: kind
          in: query
          schema: {type: string, enum: [workspaces, policies, keys, events, usage, daily, checkouts, payments, subscriptions, payment_adjustments], default: workspaces}
        - name: after
          in: query
          schema: {type: string, maxLength: 128}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/workspaces:
    get:
      summary: List owned workspaces as an owner or account setup agent
      operationId: listWorkspaces
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
    post:
      summary: Create an owned workspace as an owner or account setup agent with idempotent request identity
      operationId: createWorkspace
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              additionalProperties: false
              required: [requestId, name]
              properties:
                requestId: {$ref: '#/components/schemas/Identifier'}
                name: {type: string, minLength: 1, maxLength: 100}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/workspaces/{workspaceId}:
    parameters:
      - {$ref: '#/components/parameters/Workspace'}
    get:
      summary: Inspect an owned workspace or the ordinary agent key's bound workspace
      operationId: workspace
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/workspaces/{workspaceId}/policy:
    parameters:
      - {$ref: '#/components/parameters/Workspace'}
    get:
      parameters:
        - in: query
          name: view
          schema: {type: string, enum: [administration]}
          description: Full configuration view for account setup keys; ordinary native policy reads remain scope-filtered.
      summary: Synchronize policy intersected with the key's scopes
      description: >-
        Agent requests count against the current plan allowance. Failed requests
        release their reservation; interrupted requests reconcile on a later request.
        Client retries are separate requests, not exactly-once delivery. Administration is separate from
        memory grants. Native MCP checks the bound agent against the returned
        allowedAgentIds before returning local memory.
      operationId: policy
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
        '402': {$ref: '#/components/responses/Error'}
    put:
      summary: Save a complete policy using its expected revision
      description: Owner, account setup key or delegated policy:write. Ordinary project keys cannot expand their scopes.
      operationId: setPolicy
      requestBody:
        required: true
        content:
          application/json:
            schema: {$ref: '#/components/schemas/PolicyInput'}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
        '409': {$ref: '#/components/responses/Error'}
  /v1/workspaces/{workspaceId}/keys:
    parameters:
      - {$ref: '#/components/parameters/Workspace'}
    get:
      summary: List up to 32 enabled keys; owner or delegated keys:manage
      operationId: listKeys
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
    post:
      summary: Register a client-generated key digest; never send the raw secret
      description: >-
        Generate 16 random bytes for keyId and 32 random bytes for its base64url
        secret. The credential is atm_KEYID.SECRET; keyDigest is SHA-256 of the
        secret string. Save the credential privately before registration so an
        uncertain outcome can retry identically. Delegation cannot expand scopes,
        permissions, workspace or expiry, and has at most four parent-child hops. activateMemory explicitly enables
        native memory for this agent; it requires policy:write for agent callers.
        The first owner key can establish its exact initial scopes. Later keys
        can join existing scopes but cannot silently widen scope or provider grants.
      operationId: registerKey
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              additionalProperties: false
              required: [keyId, keyDigest, agentId, allowedScopes, expiresAt]
              properties:
                keyId: {type: string, pattern: '^[a-f0-9]{32}$'}
                keyDigest: {type: string, pattern: '^[a-f0-9]{64}$'}
                agentId: {$ref: '#/components/schemas/Identifier'}
                allowedScopes: {$ref: '#/components/schemas/Scopes'}
                expiresAt: {type: integer, description: Unix milliseconds; future and at most 90 days ahead}
                activateMemory:
                  type: boolean
                  default: false
                permissions:
                  type: array
                  uniqueItems: true
                  items: {type: string, enum: ['policy:write', 'keys:manage']}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
        '409': {$ref: '#/components/responses/Error'}
  /v1/workspaces/{workspaceId}/keys/{keyId}:
    parameters:
      - {$ref: '#/components/parameters/Workspace'}
      - name: keyId
        in: path
        required: true
        schema: {$ref: '#/components/schemas/Identifier'}
    delete:
      summary: Revoke a key and its delegated descendants
      operationId: revokeKey
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/billing:
    get:
      summary: Inspect the owner plan, request allowance, trial and subscription status
      operationId: billing
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/billing/checkout:
    post:
      summary: Create or reuse an owner-approved USD Stripe Checkout session
      description: >-
        Owner only. Creates a USD monthly subscription: Starter $14 or Pro $175.
        Only a verified paid subscription enables paid capacity; a checkout
        redirect does not grant it. No request overages or storage add-on bills.
        Existing subscribers change plans through the billing portal.
      operationId: checkout
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              additionalProperties: false
              required: [requestId, plan]
              properties:
                requestId: {$ref: '#/components/schemas/Identifier'}
                plan: {type: string, enum: [starter, pro]}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
        '503': {$ref: '#/components/responses/Error'}
  /v1/workspaces/{workspaceId}/primary:
    parameters:
      - {$ref: '#/components/parameters/Workspace'}
    put:
      summary: Select the active workspace as an owner or account setup agent
      operationId: setPrimaryWorkspace
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/activity:
    get:
      summary: Read attributed request and management activity as an owner or account setup agent
      operationId: activity
      parameters:
        - name: after
          in: query
          schema: {type: string, maxLength: 256}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/billing/portal:
    post:
      summary: Open Stripe to change or cancel the owner subscription
      operationId: billingPortal
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
  /v1/billing/delete-account:
    post:
      summary: Cancel billing before deleting the owner account in resumable batches
      operationId: cancelAndDeleteAccount
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              additionalProperties: false
              required: [confirm]
              properties:
                confirm: {const: DELETE GOVERNANCE}
      responses:
        '200': {$ref: '#/components/responses/Envelope'}
        default: {$ref: '#/components/responses/Error'}
components:
  securitySchemes:
    bearer:
      type: http
      scheme: bearer
      description: Google/X Firebase ID token for owner operations, a management-only setup key, or a scoped project agent key.
  parameters:
    Workspace:
      name: workspaceId
      in: path
      required: true
      schema: {type: string, pattern: '^[a-f0-9]{32}$'}
  schemas:
    Identifier:
      type: string
      pattern: '^[A-Za-z0-9][A-Za-z0-9_.:-]{0,127}$'
    Names:
      type: array
      maxItems: 50
      items: {$ref: '#/components/schemas/Identifier'}
    Scopes:
      type: array
      maxItems: 50
      items:
        type: string
        pattern: '^[A-Za-z0-9_.:@/+~%|#-]{1,128}$'
      description: Exact scope IDs, including email and resource IDs; no wildcard expansion.
    PolicyInput:
      type: object
      additionalProperties: false
      required: [expectedRevision, allowedAgentIds, sharedScopes, blockedScopes, providerKinds, remoteContextAllowed, maxContextBytes]
      properties:
        expectedRevision: {type: integer, minimum: 0}
        allowedAgentIds: {$ref: '#/components/schemas/Names'}
        sharedScopes: {$ref: '#/components/schemas/Scopes'}
        blockedScopes: {$ref: '#/components/schemas/Scopes'}
        providerKinds:
          allOf: [{$ref: '#/components/schemas/Names'}]
          description: Exact provider IDs from local registrations, despite the legacy field name.
        remoteContextAllowed:
          type: boolean
          description: Permit queries to allowed delegated providers, including loopback adapters; not a network firewall.
        maxContextBytes: {type: integer, minimum: 256, maximum: 65536}
  responses:
    Envelope:
      description: >-
        JSON with format atmem-governance-v1 and result object/array. An optional
        usageReceipt contains the request reservation ID. Inspect result.complete or
        nextAfter for resumable work. Additional fields do not expand authority.
      content:
        application/json:
          schema:
            type: object
            required: [format, result]
            properties:
              format: {const: atmem-governance-v1}
              result: {type: [object, array]}
              usageReceipt: {type: object}
    Error:
      description: >-
        JSON error code. 400 invalid parameters; 401 expired/revoked key or
        ancestor; 403 denied; 404 unavailable workspace/key; 409 revision or
        idempotency conflict; 429 plan allowance or rate limit; 503 temporarily
        or unavailable service. Inspect state before retrying a mutation.
      content:
        application/json:
          schema:
            type: object
            required: [error]
            properties:
              error: {type: string}
