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

Linear agent is silent when workspace OAuth refresh fails

Open rubenlangeweg opened this issue 5 months ago • 2 comments

Problem

A Linear agent session can appear to start, but the bot never posts any visible status or failure back to Linear when the workspace OAuth token is expired and the refresh token can no longer be used.

Observed behavior

When a Linear AgentSessionEvent arrives for a workspace with an expired oauth:token:<orgId> KV entry and the Linear OAuth refresh endpoint returns invalid_grant, the worker logs the failure and returns without creating an agent activity, issue comment, or Open-Inspect session.

Repro evidence from a focused local Vitest probe against current main:

agent_session.received action=created agent_session_id=agent-session-1 issue_identifier=ORI-229 org_id=org-1
oauth.refresh org_id=org-1
oauth.refresh_failed org_id=org-1 status=400 oauth_error=invalid_grant oauth_error_description="Refresh token has expired."
agent_session.no_oauth_token org_id=org-1 agent_session_id=agent-session-1

The same probe configured LINEAR_API_KEY, but no commentCreate GraphQL request was made. The only outbound call after the webhook was the failed OAuth refresh request.

Root cause

The Linear bot collapses all OAuth-token failure modes into null, and the agent-session handlers treat that as a terminal silent return.

Code path:

  1. packages/linear-bot/src/utils/linear-client.ts#getOAuthToken reads oauth:token:<orgId> from KV.
  2. If the token is expired, it calls https://api.linear.app/oauth/token with the stored refresh token.
  3. If refresh fails, e.g. invalid_grant, it logs oauth.refresh_failed and returns null.
  4. getLinearClient() returns null when getOAuthToken() returns null.
  5. packages/linear-bot/src/webhook-handler.ts#handleNewSession and #handleFollowUp call getLinearClient() before the first visible Linear activity.
  6. On !client, both handlers only log agent_session.no_oauth_token and return.

That return happens before updateAgentSession(), emitAgentActivity(), repo resolution, control-plane session creation, or follow-up forwarding.

Related existing code: packages/linear-bot/src/callbacks.ts already has a LINEAR_API_KEY comment fallback for completion callbacks when AgentActivity cannot be posted, via postIssueComment() in packages/linear-bot/src/utils/linear-client.ts. The new-session and follow-up paths do not use an equivalent fallback.

Expected behavior

When the workspace OAuth token is missing, expired, malformed, or fails refresh, the Linear user should see an actionable error, for example:

Open-Inspect needs to be re-authorized for this Linear workspace. Please reinstall or re-authorize the Linear app at <worker>/oauth/authorize and retry this request.

If the AgentActivity API cannot be used because the OAuth token is unavailable, the handler should use a fallback path that does not depend on that token, such as LINEAR_API_KEY + commentCreate when configured. If no fallback credential is configured, logs should clearly say that user notification could not be delivered.

Suggested fix

  • Preserve the OAuth failure reason instead of returning a bare null from getOAuthToken() / getLinearClient().
  • In handleNewSession() and handleFollowUp(), replace the silent !client return with a visible fallback notification.
  • Reuse or extract the existing postIssueComment() fallback used by completion callbacks.
  • Include the re-authorization URL based on env.WORKER_URL.
  • Consider clearing invalid oauth:token:<orgId> KV entries after non-recoverable refresh failures such as invalid_grant, or at least logging a distinct non-recoverable token state.

Regression tests to add

  • packages/linear-bot/src/webhook-handler.test.ts: expired workspace OAuth token + refresh invalid_grant + LINEAR_API_KEY configured should call Linear commentCreate with a re-authorization message and should not create an Open-Inspect session.
  • packages/linear-bot/src/webhook-handler.test.ts: same failure with no LINEAR_API_KEY should log a distinct fallback-unavailable state.
  • packages/linear-bot/src/utils/linear-client.test.ts: refresh failure should expose enough structured reason for callers to distinguish missing token, malformed token, missing refresh token, and non-recoverable refresh failure.

Existing tests did not catch this because src/index.test.ts mocks handleAgentSessionEvent, src/webhook-handler.test.ts mostly covers pure prompt builders, and src/utils/linear-client.test.ts currently covers fetchUser but not OAuth refresh failure behavior.

Environment checked

  • Node: v24.15.0
  • npm: 11.12.1
  • Verification: npm run build -w @open-inspect/shared, then npm test -w @open-inspect/linear-bot passed with 9 test files / 113 tests.

rubenlangeweg avatar Jun 30 '26 08:06 rubenlangeweg

The report is specific enough that it could probably be turned into a small failing test for “Linear agent is silent when workspace OAuth refresh fails”. I would try to assert the exact behavior around AgentSessionEvent rather than only checking that the command or UI path no longer errors, so the test guards the intended fix.

hiSandog avatar Jul 06 '26 02:07 hiSandog

this issue now looks closable. #864 has been merged. the commend of @hiSandog is satisfied as AgentSessionEvent tests are in place for both new-session and follow-up invalid_grand auth failures.

rubenlangeweg avatar Jul 06 '26 07:07 rubenlangeweg