background-agents icon indicating copy to clipboard operation
background-agents copied to clipboard

Linear workspace auth needs explicit health state

Open rubenlangeweg opened this issue 5 months ago • 1 comments

Problem

#864 fixes the immediate silence from #863. It does not fix the underlying auth model.

Linear OAuth health is still discovered inside request handling. By then a user has already tried to start or continue an AgentSession, and each path has to decide what auth failure means.

We should make workspace auth state explicit.

Proposed shape

Keep the current Linear bot boundary. One Worker can keep owning OAuth, webhooks, AgentActivity updates, callbacks, repo resolution, and KV state.

Add a small auth record next to the existing token record:

Key Purpose
oauth:token:<orgId> Access token, refresh token, expiry. Existing storage.
linear_auth:<orgId> Workspace auth status and last notification state. New storage.

The status should be one of:

  • connected
  • reauthorization_required
  • transient_failure

Add one gateway helper, for example getLinearAuthContext(env, orgId, traceId). It should return either a Linear client or a structured failure with the stored state and reconnect URL.

getLinearClient() can stay as a compatibility wrapper while call sites move over.

Flow

sequenceDiagram
  participant Linear
  participant Bot as Linear bot
  participant KV as LINEAR_KV
  participant Fallback as API-key comment fallback

  Linear->>Bot: AgentSessionEvent, callback, or app auth event
  Bot->>KV: read oauth:token:<orgId> and linear_auth:<orgId>
  alt OAuth valid or refresh succeeds
    Bot->>KV: mark connected
    Bot->>Linear: AgentActivity or AgentSession update
  else invalid_grant, revoked app, or missing token
    Bot->>KV: mark reauthorization_required
    Bot->>Fallback: post reconnect comment when issue is known
  else retryable OAuth failure
    Bot->>KV: mark transient_failure
    Bot->>Fallback: post retry message when useful
  end

Acceptance criteria

  • invalid_grant, missing tokens, malformed tokens, and missing refresh tokens store reauthorization_required.
  • OAuth callback stores connected and replaces the token.
  • OAuth install uses and validates state.
  • OAuth callback stores the Linear app user or installation identity when available.
  • Linear app revocation marks the workspace reauthorization_required.
  • Permission-change events update auth state or record a visible diagnostic state.
  • Retryable refresh failures store transient_failure.
  • AgentSession start, follow-up, completion callback, and tool-call callback all use the same auth result.
  • Missing or rejected LINEAR_API_KEY is recorded as notification failure.
  • Tests cover state transitions and at least one callback path, not only webhook start.

Out of scope

  • Rebuilding the Linear integration.
  • Moving all Linear state to D1.
  • Removing LINEAR_API_KEY.
  • Building a full admin dashboard in the first pass.

Sources

  • Linear Agents: https://linear.app/developers/agents
  • Linear OAuth 2.0: https://linear.app/developers/oauth-2-0-authentication
  • Linear OAuth actor authorization: https://linear.app/developers/oauth-actor-authorization
  • Linear Webhooks: https://linear.app/developers/webhooks
  • Linear GraphQL API: https://linear.app/developers/graphql

Related

#863 #864

rubenlangeweg avatar Jun 30 '26 15:06 rubenlangeweg

Closing — the bot moved to the client_credentials grant, so there's no refresh token to expire. getClientCredentialsTokenOrThrow() mints a fresh token on any cache miss and linearGraphQL() renews + retries on 401, which makes the reauthorization_required / transient_failure states moot. The remaining criteria (identity storage, shared auth path across all four handlers, LINEAR_API_KEY failure logging) already landed.

open-inspect avatar Sep 17 '26 00:09 open-inspect