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(integrations): stop sync retries after provider reconfiguration

Tin7ec77a0a1648
diff --git a/apps/api/src/plugins/gitea/events/task-description-changed.ts b/apps/api/src/plugins/gitea/events/task-description-changed.ts
index 25a4c064c..2d57d2ce6 100644
--- a/apps/api/src/plugins/gitea/events/task-description-changed.ts
+++ b/apps/api/src/plugins/gitea/events/task-description-changed.ts
@@ -116,6 +116,7 @@ export async function handleTaskDescriptionChanged(
.body ?? null,
event.taskId,
),
+ { type: "gitea", config: JSON.stringify(config) },
);
console.log(`Synced task description to Gitea issue #${issueNumber}`);
diff --git a/apps/api/src/plugins/gitea/events/task-status-changed.ts b/apps/api/src/plugins/gitea/events/task-status-changed.ts
index f0306bbcc..46d8061cd 100644
--- a/apps/api/src/plugins/gitea/events/task-status-changed.ts
+++ b/apps/api/src/plugins/gitea/events/task-status-changed.ts
@@ -56,6 +56,7 @@ export async function handleTaskStatusChanged(
async () =>
(await client.getIssue(repositoryOwner, repositoryName, issueNumber))
.state ?? "open",
+ { type: "gitea", config: JSON.stringify(config) },
);
}
} catch (error) {
diff --git a/apps/api/src/plugins/gitea/events/task-title-changed.ts b/apps/api/src/plugins/gitea/events/task-title-changed.ts
index 8d33f4c8e..ed850d78c 100644
--- a/apps/api/src/plugins/gitea/events/task-title-changed.ts
+++ b/apps/api/src/plugins/gitea/events/task-title-changed.ts
@@ -103,6 +103,7 @@ export async function handleTaskTitleChanged(
async () =>
(await client.getIssue(repositoryOwner, repositoryName, issueNumber))
.title,
+ { type: "gitea", config: JSON.stringify(config) },
);
console.log(`Synced task title to Gitea issue #${issueNumber}`);
diff --git a/apps/api/src/plugins/github/events/task-description-changed.ts b/apps/api/src/plugins/github/events/task-description-changed.ts
index 54ea73108..5ded46218 100644
--- a/apps/api/src/plugins/github/events/task-description-changed.ts
+++ b/apps/api/src/plugins/github/events/task-description-changed.ts
@@ -95,6 +95,7 @@ export async function handleTaskDescriptionChanged(
).data.body ?? null,
event.taskId,
),
+ { type: "github", config: JSON.stringify(config) },
);
console.log(`Synced task description to GitHub issue #${issueNumber}`);
diff --git a/apps/api/src/plugins/github/events/task-status-changed.ts b/apps/api/src/plugins/github/events/task-status-changed.ts
index 4ab533bbb..7962f057a 100644
--- a/apps/api/src/plugins/github/events/task-status-changed.ts
+++ b/apps/api/src/plugins/github/events/task-status-changed.ts
@@ -76,6 +76,7 @@ export async function handleTaskStatusChanged(
issue_number: issueNumber,
})
).data.state ?? "open",
+ { type: "github", config: JSON.stringify(config) },
);
}
} catch (error) {
diff --git a/apps/api/src/plugins/github/events/task-title-changed.ts b/apps/api/src/plugins/github/events/task-title-changed.ts
index f50a560a0..6140bed03 100644
--- a/apps/api/src/plugins/github/events/task-title-changed.ts
+++ b/apps/api/src/plugins/github/events/task-title-changed.ts
@@ -81,6 +81,7 @@ export async function handleTaskTitleChanged(
issue_number: issueNumber,
})
).data.title,
+ { type: "github", config: JSON.stringify(config) },
);
console.log(`Synced task title to GitHub issue #${issueNumber}`);
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.ts
index 8f8797410..373578d5c 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
@@ -28,6 +28,7 @@ export async function applyObservedTaskValue(
intentId: string,
updatedAt: string | undefined,
expectedRevision: string,
+ expectedBinding?: Parameters<typeof withIntegrationLink>[3],
) {
return withIntegrationLink(
link,
@@ -91,6 +92,7 @@ export async function applyObservedTaskValue(
);
return true;
},
+ expectedBinding,
);
}
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 e44a6a19c..6e413ff7e 100644
--- a/apps/api/src/plugins/github/services/deferred-issue-edits.ts
+++ b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
@@ -380,6 +380,7 @@ export async function replayDeferredIssueEdits() {
)
: current[repair.field];
},
+ integration,
);
}
if (repairs.length)
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 4a5b8f113..c504d684a 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
@@ -19,9 +19,11 @@ export async function syncLatestTaskValue(
initialValue: string,
write: (value: string) => Promise<string | undefined>,
readCurrent?: () => Promise<string>,
+ expectedBinding?: { type?: string; config?: string },
) {
let value = initialValue;
- for (;;) {
+ let identity = expectedBinding;
+ const currentBinding = async () => {
const binding = (await findExternalLinksByTask(taskId)).find(
(candidate) =>
candidate.id === link.id &&
@@ -32,15 +34,27 @@ export async function syncLatestTaskValue(
!binding ||
(binding.integration &&
(binding.integration.isActive === false ||
- binding.integration.projectId !== projectId))
+ binding.integration.projectId !== projectId ||
+ (identity?.type !== undefined &&
+ binding.integration.type !== identity.type) ||
+ (identity?.config !== undefined &&
+ binding.integration.config !== identity.config)))
)
return;
+ return binding;
+ };
+ for (;;) {
+ const binding = await currentBinding();
+ if (!binding) return;
+ identity ??= binding.integration
+ ? { type: binding.integration.type, config: binding.integration.config }
+ : undefined;
const intentId = randomUUID();
// Webhooks may arrive before PATCH returns, including from another instance.
const persisted = await updateExternalLink(link.id, {
outbound: { field, value, intentId, pending: true },
});
- if (persisted === false) return;
+ if (persisted === false || !(await currentBinding())) return;
let updatedAt: string | undefined;
try {
updatedAt = await write(value);
@@ -63,6 +77,7 @@ export async function syncLatestTaskValue(
}).catch(() => {});
throw error;
}
+ if (!(await currentBinding())) return;
await updateExternalLink(link.id, {
...(field === "title" ? { title: value } : {}),
outbound: { field, value, updatedAt, intentId, pending: false },
@@ -70,13 +85,7 @@ export async function syncLatestTaskValue(
? { metadata: { state: value, lastOutboundStateSyncAt: Date.now() } }
: {}),
});
- const currentLinks = await findExternalLinksByTask(taskId);
- const currentLink = currentLinks.find(
- (candidate) =>
- candidate.id === link.id &&
- candidate.integrationId === link.integrationId &&
- candidate.resourceType === "issue",
- );
+ const currentLink = await currentBinding();
if (!currentLink) return;
const metadata = parseLinkMetadata<{
lastSync?: Record<string, SyncStamp>;
@@ -95,6 +104,7 @@ export async function syncLatestTaskValue(
readCurrent
) {
const revision = await integrationTaskRevision(taskId, projectId);
+ if (!(await currentBinding())) return;
if (
revision &&
(await readCurrent()) === value &&
@@ -106,6 +116,7 @@ export async function syncLatestTaskValue(
intentId,
updatedAt,
revision,
+ identity,
)) === true
)
return;
diff --git a/tests/api-integration/outbound-intent-correlation.test.ts b/tests/api-integration/outbound-intent-correlation.test.ts
index 6b625c685..8f531118d 100644
--- a/tests/api-integration/outbound-intent-correlation.test.ts
+++ b/tests/api-integration/outbound-intent-correlation.test.ts
@@ -288,3 +288,152 @@ it.each(
expect(write).toHaveBeenCalledTimes(2);
},
);
+
+it.each(
+ (["github", "gitea"] as const).flatMap((provider) =>
+ (["title", "description", "state"] as const).flatMap((field) =>
+ ["retry", "read", "apply"].map((phase) => ({ provider, field, phase })),
+ ),
+ ),
+)(
+ "stops $field sync against a reconfigured provider ($provider/$phase)",
+ async ({ provider, field, phase }) => {
+ const { workspace } = await createWorkspaceMember();
+ const { project } = await createProjectFixture({
+ workspaceId: workspace.id,
+ });
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({
+ projectId: project.id,
+ number: 1,
+ title: "B",
+ description: "B",
+ status: "to-do",
+ })
+ .returning();
+ const [integration] = await db
+ .insert(schema.integrationTable)
+ .values({
+ projectId: project.id,
+ type: provider,
+ config: JSON.stringify({ repositoryName: "old", accessToken: "fake" }),
+ })
+ .returning();
+ const [link] = await db
+ .insert(schema.externalLinkTable)
+ .values({
+ taskId: task.id,
+ integrationId: integration.id,
+ resourceType: "issue",
+ externalId: "1",
+ url: "https://provider.example/1",
+ })
+ .returning();
+ const older = field === "state" ? "closed" : "A";
+ const reconfigure = async () => {
+ await db
+ .update(schema.integrationTable)
+ .set({
+ config: JSON.stringify({
+ repositoryName: "new",
+ accessToken: "fake",
+ }),
+ })
+ .where(eq(schema.integrationTable.id, integration.id));
+ };
+ const write = vi.fn(async () => {
+ if (write.mock.calls.length === 1) {
+ if (phase !== "retry") {
+ const saved = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.id, link.id),
+ });
+ const stamp = JSON.parse(saved!.metadata!).lastSync[field];
+ await updateExternalLink(link.id, {
+ metadata: {
+ lastSync: {
+ [field]: inboundStamp(
+ stamp,
+ field === "state" ? "open" : "B",
+ provider,
+ "2026-09-30T00:00:02Z",
+ ),
+ },
+ },
+ observedOutbound: {
+ field,
+ intentId: stamp.outbound[0].intentId,
+ updatedAt: "2026-09-30T00:00:03Z",
+ },
+ });
+ }
+ if (phase !== "apply") await reconfigure();
+ }
+ return "2026-09-30T00:00:01Z";
+ });
+ const read = vi.fn(async () => {
+ await reconfigure();
+ return older;
+ });
+ await syncLatestTaskValue(
+ task.id,
+ project.id,
+ link,
+ field,
+ older,
+ write,
+ read,
+ );
+ expect(write).toHaveBeenCalledTimes(1);
+ expect(read).toHaveBeenCalledTimes(phase === "apply" ? 1 : 0);
+ const current = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, task.id),
+ });
+ expect(field === "state" ? current?.status : current?.[field]).toBe(
+ field === "state" ? "to-do" : "B",
+ );
+ },
+);
+it.each(["github", "gitea"])(
+ "rejects a stale provider context before the initial %s write",
+ async (provider) => {
+ const { workspace } = await createWorkspaceMember();
+ const { project } = await createProjectFixture({
+ workspaceId: workspace.id,
+ });
+ const [task] = await db
+ .insert(schema.taskTable)
+ .values({ projectId: project.id, number: 1, title: "A" })
+ .returning();
+ const [integration] = await db
+ .insert(schema.integrationTable)
+ .values({
+ projectId: project.id,
+ type: provider,
+ config: JSON.stringify({ repositoryName: "new" }),
+ })
+ .returning();
+ const [link] = await db
+ .insert(schema.externalLinkTable)
+ .values({
+ taskId: task.id,
+ integrationId: integration.id,
+ resourceType: "issue",
+ externalId: "1",
+ url: "https://provider.example/1",
+ })
+ .returning();
+ const write = vi.fn(async () => "2026-09-30T00:00:01Z");
+ await syncLatestTaskValue(
+ task.id,
+ project.id,
+ link,
+ "title",
+ "A",
+ write,
+ undefined,
+ { type: provider, config: JSON.stringify({ repositoryName: "old" }) },
+ );
+ expect(write).not.toHaveBeenCalled();
+ },
+);