From e9a9fe2cc0204e60c22a894da48bd6767be7dc2c Mon Sep 17 00:00:00 2001 From: Brendan Date: Wed, 16 Sep 2026 18:46:11 +0000 Subject: [PATCH] Handle Linear token expiry response variants --- packages/app/src/linear.ts | 11 +++++++++++ packages/app/src/tenant.ts | 5 ++--- packages/app/test/linear.test.ts | 14 +++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/app/src/linear.ts b/packages/app/src/linear.ts index 5f04b4c1..c1e0f8e2 100644 --- a/packages/app/src/linear.ts +++ b/packages/app/src/linear.ts @@ -10,6 +10,17 @@ export const LINEAR_OPTION_QUERIES = { labels: "query { issueLabels(first: 100) { nodes { id name } } }", } as const; +type LinearError = { message?: string; extensions?: { code?: string } }; + +export const isLinearAuthenticationError = (status: number, errors: LinearError[] | undefined): boolean => { + if (status === 401) return true; + return Boolean(errors?.some(error => { + const code = error.extensions?.code?.toUpperCase(); + if (code === "UNAUTHENTICATED" || code === "AUTHENTICATION_ERROR") return true; + return /authentication required|not authenticated|access token.*(?:invalid|expired)|(?:invalid|expired).*access token/i.test(error.message ?? ""); + })); +}; + export const refreshLinearToken = async (refreshToken: string, clientId: string, clientSecret: string): Promise<{ access_token: string; refresh_token?: string }> => { const response = await fetch("https://api.linear.app/oauth/token", { method: "POST", diff --git a/packages/app/src/tenant.ts b/packages/app/src/tenant.ts index a75dad1a..f3db578c 100644 --- a/packages/app/src/tenant.ts +++ b/packages/app/src/tenant.ts @@ -3,7 +3,7 @@ import Mustache from "mustache"; import { decrypt, encrypt } from "./crypto"; import { agentListCommand, agentOutputCommand, agentStartupBlocked, agentStatusCommand, connectionCheckCommand, defaultAgentCommand, exec, garbageCollectPaneCommand, herdrAgentStatus, herdrCheckCommand, paneGetCommand, paneProcessInfoCommand, replaceForegroundCommand, startAgentCommand, startAgentInPaneCommand, stopAgentCommand, validateWorktreeLeaseCommand, type ExeConnection } from "./exe"; import { findOwnedAgent, ownsPane, parseAgent, parseAgentList, parsePaneProcess, type HerdrIdentity } from "./recovery"; -import { LINEAR_ISSUE_PROJECT_QUERY, LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, refreshLinearToken } from "./linear"; +import { LINEAR_ISSUE_PROJECT_QUERY, LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, isLinearAuthenticationError, refreshLinearToken } from "./linear"; import { matchingIssue } from "./matcher"; import type { Env, ExeConnectionInput, FilterType, MatchRule, PipeInput, RunState } from "./types"; import { workingDirectoryFor, workspaceNameFor } from "./workspace"; @@ -789,8 +789,7 @@ export class Tenant extends DurableObject { return { response, payload }; }; let result = await request(token); - const message = result.payload.errors?.[0]?.message; - if ((result.response.status === 401 || /authentication required|not authenticated/i.test(message ?? ""))) { + if (isLinearAuthenticationError(result.response.status, result.payload.errors)) { const refreshed = await this.refreshLinearAccessToken(token); result = await request(refreshed); } diff --git a/packages/app/test/linear.test.ts b/packages/app/test/linear.test.ts index fa47249a..9dbba2d9 100644 --- a/packages/app/test/linear.test.ts +++ b/packages/app/test/linear.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, it, vi } from "vitest"; -import { LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, refreshLinearToken } from "../src/linear"; +import { LINEAR_OPTION_QUERIES, LINEAR_PROJECTS_QUERY, isLinearAuthenticationError, refreshLinearToken } from "../src/linear"; afterEach(() => vi.restoreAllMocks()); @@ -19,6 +19,18 @@ describe("Linear queries", () => { } }); + it("recognizes Linear's expired-token response variants", () => { + expect(isLinearAuthenticationError(401, undefined)).toBe(true); + expect(isLinearAuthenticationError(200, [{ message: "Authentication required, not authenticated" }])).toBe(true); + expect(isLinearAuthenticationError(200, [{ message: "The access token has expired" }])).toBe(true); + expect(isLinearAuthenticationError(200, [{ extensions: { code: "UNAUTHENTICATED" } }])).toBe(true); + }); + + it("does not refresh for unrelated Linear errors", () => { + expect(isLinearAuthenticationError(200, [{ message: "Query too complex", extensions: { code: "BAD_USER_INPUT" } }])).toBe(false); + expect(isLinearAuthenticationError(403, [{ message: "You do not have permission" }])).toBe(false); + }); + it("exchanges a refresh token for a fresh Linear token pair", async () => { const request = vi.spyOn(globalThis, "fetch").mockResolvedValue(Response.json({ access_token: "access-new", refresh_token: "refresh-new" }));