⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(api): guard initialization and observed state for archived tasks
Tin1e9c30b175e9
diff --git a/apps/api/src/plugins/github/services/apply-observed-task-value.ts b/apps/api/src/plugins/github/services/apply-observed-task-value.tsindex 31732a32e..7e0c3bc37 100644--- a/apps/api/src/plugins/github/services/apply-observed-task-value.ts+++ b/apps/api/src/plugins/github/services/apply-observed-task-value.ts@@ -17,6 +17,7 @@ import {import { withIntegrationLink } from "./with-integration-link";import { updateExternalLink } from "./link-manager";import { updateTaskStatus } from "./task-service";+import { isStateSyncPaused } from "../../sync/state-sync-paused";// The observed matching value is durable even if the inbound HTTP request// times out waiting for our response. Apply it only after version correlation.@@ -106,6 +107,12 @@ export async function writeInboundTaskField(value: string,) {if (field === "state") {+ // Covers observed-value adoption and deferred replay in the same lock.+ const task = await tx.query.taskTable.findFirst({+ where: linkedTaskScope(link.taskId, integration.projectId),+ columns: { status: true },+ });+ if (!task || isStateSyncPaused(task.status)) return;const resolveStatus =integration.type === "gitea" ? resolveGiteaStatus : resolveGithubStatus;const status = await resolveStatus(diff --git a/apps/api/src/plugins/github/services/sync-latest-task-value.ts b/apps/api/src/plugins/github/services/sync-latest-task-value.tsindex 01fd2285b..bc5cc9cca 100644--- a/apps/api/src/plugins/github/services/sync-latest-task-value.ts+++ b/apps/api/src/plugins/github/services/sync-latest-task-value.ts@@ -74,6 +74,14 @@ export async function syncLatestTaskValue(for (;;) {const binding = await currentBinding();if (!binding) return;+ // Archived tasks send no state, even for retried initialization or repairs.+ if (field === "state") {+ const task = await db.query.taskTable.findFirst({+ where: linkedTaskScope(taskId, projectId),+ columns: { status: true },+ });+ if (task && isStateSyncPaused(task.status)) return true;+ }identity ??= binding.integration? { type: binding.integration.type, config: binding.integration.config }: undefined;@@ -198,7 +206,6 @@ export async function syncLatestTaskValue(},});if (!task) return;- if (field === "state" && isStateSyncPaused(task.status)) return true;const current =field === "state"? (await isTaskInFinalState(task))diff --git a/tests/api-integration/deferred-issue-edits.test.ts b/tests/api-integration/deferred-issue-edits.test.tsindex 729680aff..ea50fa8b8 100644--- a/tests/api-integration/deferred-issue-edits.test.ts+++ b/tests/api-integration/deferred-issue-edits.test.ts@@ -1937,6 +1937,54 @@ it.each(["github", "gitea"])(},);+it.each(["github", "gitea"])(+ "does not adopt an observed %s close for a task archived mid-flight",+ async (type) => {+ const { task, integration, link } = await seed(type);+ await db+ .update(schema.externalLinkTable)+ .set({ metadata: "{}" })+ .where(eq(schema.externalLinkTable.id, link.id));+ await db+ .update(schema.taskTable)+ .set({ status: "done" })+ .where(eq(schema.taskTable.id, task.id));+ const write = vi.fn(async () => {+ // A newer close webhook is observed, then the task is archived.+ const pending = (await metadata(link.id)).lastSync.state.outbound.find(+ (entry: { pending?: boolean }) => entry.pending,+ );+ await updateExternalLink(link.id, {+ observedOutbound: {+ field: "state",+ intentId: pending.intentId,+ updatedAt: "2026-09-30T00:00:10Z",+ },+ });+ await db+ .update(schema.taskTable)+ .set({ status: "archived", columnId: null })+ .where(eq(schema.taskTable.id, task.id));+ return "2026-09-30T00:00:05Z";+ });++ expect(+ await syncLatestTaskValue(+ task.id,+ integration.projectId,+ link,+ "state",+ "closed",+ write,+ async () => "closed",+ ),+ ).toBe(true);++ expect(write).toHaveBeenCalledOnce();+ expect((await current(task.id))?.status).toBe("archived");+ },+);+it("skips provider reads and writes while another replica holds the worker lease", async () => {const { link, integration } = await seed();await deferTaskSync(link, integration, ["title"]);diff --git a/tests/api-integration/integration-sync-providers.test.ts b/tests/api-integration/integration-sync-providers.test.tsindex 327e49e84..13fe35c32 100644--- a/tests/api-integration/integration-sync-providers.test.ts+++ b/tests/api-integration/integration-sync-providers.test.ts@@ -663,6 +663,44 @@ describe.each(["github", "gitea", "gitlab"] as const)(},);+ it("does not reopen the issue when initialization retries after archiving", async () => {+ const f = await setup(type);+ await f.assign();+ await db+ .update(schema.taskTable)+ .set({ status: f.columns.done.slug, columnId: f.columns.done.id })+ .where(eq(schema.taskTable.id, f.task.id));+ let remoteState = "open";+ vi.spyOn(console, "error").mockImplementation(() => {});+ mocks.update.mockImplementationOnce(async () => {+ remoteState = "closed";+ throw new Error("provider timeout");+ });+ await reconcileTaskSync(f.project.id, f.task.id);+ await db+ .update(schema.taskTable)+ .set({ status: "archived", columnId: null })+ .where(eq(schema.taskTable.id, f.task.id));+ mocks.update.mockImplementation(async (...args: unknown[]) => {+ const patch = args.at(-1) as { state?: string; state_event?: string };+ remoteState =+ patch.state ??+ (patch.state_event === "close"+ ? "closed"+ : patch.state_event+ ? "open"+ : remoteState);+ });+ await reconcileProjectSync(f.project.id, f.integration.id);+ expect(remoteState).toBe("closed");+ expect(+ JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),+ ).toMatchObject({+ syncInitializedState: true,+ syncInitializationPending: false,+ });+ });+it.runIf(type === "gitlab")("ignores live close and reopen webhooks for an archived task",async () => {