Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): pause open/closed issue sync for archived tasks

Tin68ef62dcbf07
diff --git a/apps/api/src/integration-sync/controllers/review-resume.ts b/apps/api/src/integration-sync/controllers/review-resume.ts
index b54311c70..e97dd6ef9 100644
--- a/apps/api/src/integration-sync/controllers/review-resume.ts
+++ b/apps/api/src/integration-sync/controllers/review-resume.ts
@@ -5,6 +5,7 @@ import db from "../../database";
import { externalLinkTable, taskTable } from "../../database/schema";
import { isTaskInFinalState } from "../../plugins/github/services/task-service";
import { taskMatchesRule } from "../../plugins/sync/eligibility";
+import { isStateSyncPaused } from "../../plugins/sync/state-sync-paused";
import { providerIssue } from "../../plugins/sync/provider-issue";
import { isSyncPaused, readSyncRules } from "../../plugins/sync/rules";
import { getSyncIntegration } from "./get-integration";
@@ -83,13 +84,11 @@ export async function reviewSyncResume(
const local = {
title: task.title,
description: task.description ?? "",
- // Archived tasks have no issue state of their own; keep the provider's.
- state:
- task.status === "archived"
- ? remote.state
- : (await isTaskInFinalState(task, database))
- ? ("closed" as const)
- : ("open" as const),
+ state: isStateSyncPaused(task.status)
+ ? remote.state
+ : (await isTaskInFinalState(task, database))
+ ? ("closed" as const)
+ : ("open" as const),
};
const token = createHash("sha256")
.update(
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-closed.ts b/apps/api/src/plugins/gitea/webhooks/issue-closed.ts
index a661dc75a..7425dd8cd 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-closed.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-closed.ts
@@ -1,4 +1,5 @@
import { issueEditScope } from "../../github/utils/deferred-issue-edit";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
import { deferIssueEdit } from "../../github/services/deferred-issue-edits";
import { inboundStamp } from "../../github/utils/sync-echo";
import { withIntegrationLink } from "../../github/services/with-integration-link";
@@ -107,6 +108,8 @@ export async function handleGiteaIssueClosed(
return;
}
+ if (isStateSyncPaused(task.status)) return;
+
const existingMetadata = parseLinkMetadata<
Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
>(externalLink.metadata, {
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts b/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts
index 04f79c07b..3682ec1fe 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts
@@ -1,4 +1,5 @@
import { issueEditScope } from "../../github/utils/deferred-issue-edit";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
import { deferIssueEdit } from "../../github/services/deferred-issue-edits";
import { inboundStamp } from "../../github/utils/sync-echo";
import { withIntegrationLink } from "../../github/services/with-integration-link";
@@ -108,6 +109,8 @@ export async function handleGiteaIssueReopened(
if (!task) {
return;
}
+
+ if (isStateSyncPaused(task.status)) return;
const existingMetadata = parseLinkMetadata<
Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
>(externalLink.metadata, {
diff --git a/apps/api/src/plugins/github/services/deferred-issue-edits.ts b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
index a649ffbbf..6a8dfc1a4 100644
--- a/apps/api/src/plugins/github/services/deferred-issue-edits.ts
+++ b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
@@ -29,6 +29,7 @@ import { writeInboundTaskField } from "./apply-observed-task-value";
import { updateExternalLink } from "./link-manager";
import { syncLatestTaskValue } from "./sync-latest-task-value";
import { isTaskInFinalState } from "./task-service";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
import {
linkedTaskScope,
integrationTaskRevision,
@@ -259,20 +260,12 @@ async function replayClaimedIssueEdits() {
const taskIsClosed = fields.includes("state")
? await isTaskInFinalState(task, tx)
: false;
- // Archived tasks have no outbound state target; keep the provider's.
- const keepsState =
- fields.includes("state") && task.status === "archived";
- if (keepsState) current.state = values.state;
- // Its own uncertain write echoing back settles the intent instead,
- // unless a provider edit was observed after that write.
- const stateStamp = current.lastSync?.state;
- const stateEcho =
- keepsState &&
- uncertainOutboundIntents(stateStamp, values.state).some(
- (entry) => !inboundOccurredAfterIntent(stateStamp, entry),
- );
+ // Skip state for archived tasks and settle what still waits on it.
+ const statePaused =
+ fields.includes("state") && isStateSyncPaused(task.status);
+ if (statePaused) current.state = values.state;
for (const field of fields) {
- if (field === "state" && keepsState) continue;
+ if (field === "state" && statePaused) continue;
const stamp = current.lastSync?.[field];
const uncertain = uncertainOutboundIntents(stamp, values[field]);
const intentIds = uncertain.flatMap((entry) =>
@@ -321,7 +314,7 @@ async function replayClaimedIssueEdits() {
const accepted = job.fields.filter(
(field) =>
!repairs.some((repair) => repair.field === field) &&
- !(field === "state" && stateEcho) &&
+ !(field === "state" && statePaused) &&
!(
field === "state" &&
integration.type === "github" &&
@@ -371,19 +364,9 @@ async function replayClaimedIssueEdits() {
{
metadata: current,
...(accepted.includes("title") ? { title: values.title } : {}),
- ...(keepsState
+ ...(statePaused
? { retireUncertainOutbound: "state" as const }
: {}),
- // A retained job must not replay the settled echo as an edit.
- ...(stateEcho && repairs.length
- ? {
- deferredEdit: {
- fields: [],
- repairFields: ["state" as const],
- scope: job.scope,
- },
- }
- : {}),
...(repairs.length ? {} : { completeDeferredEdit: job.id }),
},
tx,
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.ts
index 2ec8139f4..01fd2285b 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
@@ -16,6 +16,7 @@ import {
} from "./integration-task-scope";
import { findExternalLinksByTask, updateExternalLink } from "./link-manager";
import { isTaskInFinalState } from "./task-service";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
// Provider requests may complete out of order across API instances. Every late
// completion repairs the provider using the current, still-linked task value.
@@ -197,8 +198,7 @@ export async function syncLatestTaskValue(
},
});
if (!task) return;
- // Archiving keeps the issue state just written; see resolveIssueStateChange.
- if (field === "state" && task.status === "archived") return true;
+ if (field === "state" && isStateSyncPaused(task.status)) return true;
const current =
field === "state"
? (await isTaskInFinalState(task))
diff --git a/apps/api/src/plugins/github/webhooks/issue-closed.ts b/apps/api/src/plugins/github/webhooks/issue-closed.ts
index 22fcee820..9f172361f 100644
--- a/apps/api/src/plugins/github/webhooks/issue-closed.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-closed.ts
@@ -1,4 +1,5 @@
import { issueEditScope } from "../utils/deferred-issue-edit";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
import { deferIssueEdit } from "../services/deferred-issue-edits";
import type { GitHubConfig } from "../config";
import { getVerifiedInstallationOctokit } from "../utils/github-app";
@@ -84,6 +85,8 @@ export async function handleIssueClosed(payload: IssueClosedPayload) {
return;
}
+ if (isStateSyncPaused(task.status)) return;
+
const existingMetadata = parseLinkMetadata<
Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
>(externalLink.metadata, {
diff --git a/apps/api/src/plugins/github/webhooks/issue-reopened.ts b/apps/api/src/plugins/github/webhooks/issue-reopened.ts
index fe4aa2beb..ca83c4c03 100644
--- a/apps/api/src/plugins/github/webhooks/issue-reopened.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-reopened.ts
@@ -1,4 +1,5 @@
import { issueEditScope } from "../utils/deferred-issue-edit";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
import { deferIssueEdit } from "../services/deferred-issue-edits";
import type { GitHubConfig } from "../config";
import { getVerifiedInstallationOctokit } from "../utils/github-app";
@@ -84,6 +85,8 @@ export async function handleIssueReopened(payload: IssueReopenedPayload) {
return;
}
+ if (isStateSyncPaused(task.status)) return;
+
const existingMetadata = parseLinkMetadata<
Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
>(externalLink.metadata, {
diff --git a/apps/api/src/plugins/gitlab/webhooks/issue-closed.ts b/apps/api/src/plugins/gitlab/webhooks/issue-closed.ts
index 405d794f7..f9e37d1ab 100644
--- a/apps/api/src/plugins/gitlab/webhooks/issue-closed.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/issue-closed.ts
@@ -1,4 +1,5 @@
import { withIntegrationLink } from "../../github/services/with-integration-link";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
import { linkedTaskScope } from "../../github/services/integration-task-scope";
import { and, eq } from "drizzle-orm";
import db from "../../../database";
@@ -75,6 +76,8 @@ export async function handleGitlabIssueClosed(
return;
}
+ if (isStateSyncPaused(task.status)) return;
+
let existingMetadata: Record<string, unknown> = {};
if (externalLink.metadata) {
try {
diff --git a/apps/api/src/plugins/gitlab/webhooks/issue-reopened.ts b/apps/api/src/plugins/gitlab/webhooks/issue-reopened.ts
index 1bfc698dc..f5d7e1071 100644
--- a/apps/api/src/plugins/gitlab/webhooks/issue-reopened.ts
+++ b/apps/api/src/plugins/gitlab/webhooks/issue-reopened.ts
@@ -1,4 +1,5 @@
import { withIntegrationLink } from "../../github/services/with-integration-link";
+import { isStateSyncPaused } from "../../sync/state-sync-paused";
import { linkedTaskScope } from "../../github/services/integration-task-scope";
import { and, eq } from "drizzle-orm";
import db from "../../../database";
@@ -75,6 +76,8 @@ export async function handleGitlabIssueReopened(
return;
}
+ if (isStateSyncPaused(task.status)) return;
+
let existingMetadata: Record<string, unknown> = {};
if (externalLink.metadata) {
try {
diff --git a/apps/api/src/plugins/sync/issue-state-change.ts b/apps/api/src/plugins/sync/issue-state-change.ts
index 410d4f41e..fa90fb189 100644
--- a/apps/api/src/plugins/sync/issue-state-change.ts
+++ b/apps/api/src/plugins/sync/issue-state-change.ts
@@ -1,4 +1,5 @@
import { isTaskInFinalState } from "../github/services/task-service";
+import { isStateSyncPaused } from "./state-sync-paused";
// Archiving keeps the issue's current state: a completed task's issue stays
// closed, and shelving unfinished work does not close it. Unarchiving into an
@@ -8,11 +9,11 @@ export async function resolveIssueStateChange(
oldStatus: string,
status: string,
): Promise<"close" | "reopen" | undefined> {
- if (status === "archived") return;
+ if (isStateSyncPaused(status)) return;
if (await isTaskInFinalState({ projectId, status, columnId: null }))
return "close";
if (
- oldStatus === "archived" ||
+ isStateSyncPaused(oldStatus) ||
(await isTaskInFinalState({ projectId, status: oldStatus, columnId: null }))
)
return "reopen";
diff --git a/apps/api/src/plugins/sync/state-sync-paused.ts b/apps/api/src/plugins/sync/state-sync-paused.ts
new file mode 100644
index 000000000..cfe480071
--- /dev/null
+++ b/apps/api/src/plugins/sync/state-sync-paused.ts
@@ -0,0 +1,5 @@
+// Archived tasks take no part in open/closed issue sync in either direction.
+// Unarchiving lets the status handlers bring the issue back in line.
+export function isStateSyncPaused(status: string) {
+ return status === "archived";
+}
diff --git a/tests/api-integration/deferred-issue-edits.test.ts b/tests/api-integration/deferred-issue-edits.test.ts
index e63622300..729680aff 100644
--- a/tests/api-integration/deferred-issue-edits.test.ts
+++ b/tests/api-integration/deferred-issue-edits.test.ts
@@ -1843,7 +1843,7 @@ it.each(["github", "gitea"])(
);
it.each(["github", "gitea"])(
- "applies a genuine %s close to an archived task after an accepted reopen",
+ "settles a %s close for an archived task after an accepted close observation",
async (type) => {
const { task, integration, link } = await seed(type);
await db
@@ -1866,32 +1866,74 @@ it.each(["github", "gitea"])(
},
),
).rejects.toThrow("provider timeout");
- await deferIssueEdit(link, integration, ["state"]);
- m.read.mockResolvedValue({
- title: "B",
- body: "old body",
- state: "open",
- updated_at: "2026-09-30T00:00:05Z",
- });
- await replayDeferredIssueEdits();
- expect((await current(task.id))?.status).toBe("to-do");
-
+ // The completed task accepts a provider close; the uncertain close remains.
+ const observed = await metadata(link.id);
+ observed.lastSync.state = inboundStamp(
+ observed.lastSync.state,
+ "closed",
+ type,
+ "2026-09-30T00:00:02Z",
+ );
+ await db
+ .update(schema.externalLinkTable)
+ .set({ metadata: JSON.stringify(observed) })
+ .where(eq(schema.externalLinkTable.id, link.id));
await db
.update(schema.taskTable)
.set({ status: "archived", columnId: null })
.where(eq(schema.taskTable.id, task.id));
- await deferIssueEdit(link, integration, ["state"]);
- m.read.mockResolvedValue({
- title: "B",
- body: "old body",
- state: "closed",
- updated_at: "2026-09-30T00:00:06Z",
- });
+ await deferIssueEdit(link, integration, ["state", "title", "description"]);
+
await replayDeferredIssueEdits();
expect(m.write).not.toHaveBeenCalled();
+ const after = await metadata(link.id);
+ expect(after.deferredIssueEdit).toBeUndefined();
+ expect(
+ after.lastSync.state.outbound.filter(
+ (entry: { uncertain?: boolean; cancelled?: boolean }) =>
+ entry.uncertain && !entry.cancelled,
+ ),
+ ).toEqual([]);
+ expect(await current(task.id)).toMatchObject({
+ status: "archived",
+ title: "A",
+ description: "recovered body",
+ });
+ },
+);
+
+it.each(["github", "gitea"])(
+ "ignores live %s close and reopen webhooks for an archived task",
+ 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: "archived", columnId: null })
+ .where(eq(schema.taskTable.id, task.id));
+
+ await deliverField(
+ type,
+ "state",
+ "closed",
+ "2026-09-30T00:00:05Z",
+ integration.id,
+ );
+ await deliverField(
+ type,
+ "state",
+ "open",
+ "2026-09-30T00:00:06Z",
+ integration.id,
+ );
+
+ expect((await current(task.id))?.status).toBe("archived");
expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();
- expect((await current(task.id))?.status).toBe("done");
+ expect(m.write).not.toHaveBeenCalled();
},
);
diff --git a/tests/api-integration/integration-sync-providers.test.ts b/tests/api-integration/integration-sync-providers.test.ts
index 8d4ffa564..327e49e84 100644
--- a/tests/api-integration/integration-sync-providers.test.ts
+++ b/tests/api-integration/integration-sync-providers.test.ts
@@ -23,6 +23,8 @@ import { githubPlugin } from "../../apps/api/src/plugins/github";
import { handleIssueOpened } from "../../apps/api/src/plugins/github/webhooks/issue-opened";
import { gitlabPlugin } from "../../apps/api/src/plugins/gitlab";
import { handleGitlabIssueOpened } from "../../apps/api/src/plugins/gitlab/webhooks/issue-opened";
+import { handleGitlabIssueClosed } from "../../apps/api/src/plugins/gitlab/webhooks/issue-closed";
+import { handleGitlabIssueReopened } from "../../apps/api/src/plugins/gitlab/webhooks/issue-reopened";
import { registerPlugin } from "../../apps/api/src/plugins/registry";
import {
reconcileProjectSync,
@@ -660,6 +662,40 @@ describe.each(["github", "gitea", "gitlab"] as const)(
).toMatchObject({ status: "archived", columnId: null });
},
);
+
+ it.runIf(type === "gitlab")(
+ "ignores live close and reopen webhooks for an archived task",
+ async () => {
+ const f = await exported();
+ await f.changeStatus(f.columns.todo.slug, "archived", null);
+ for (const [handle, state, action] of [
+ [handleGitlabIssueClosed, "closed", "close"],
+ [handleGitlabIssueReopened, "opened", "reopen"],
+ ] as const)
+ await handle(
+ {
+ object_attributes: {
+ iid: 12,
+ title: f.task.title,
+ url: "https://gitlab.example/team/repo/-/issues/12",
+ state,
+ action,
+ updated_at: new Date().toISOString(),
+ },
+ project: {
+ path_with_namespace: "team/repo",
+ web_url: "https://gitlab.example/team/repo",
+ },
+ },
+ f.integration.id,
+ );
+ expect(
+ await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, f.task.id),
+ }),
+ ).toMatchObject({ status: "archived", columnId: null });
+ },
+ );
});
it("repairs a late initialization close after a concurrent reopen", async () => {