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): recover deferred edits after writer crashes

Tin7faaa960c89c
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-closed.ts b/apps/api/src/plugins/gitea/webhooks/issue-closed.ts
index 360e739b9..8be080ddf 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-closed.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-closed.ts
@@ -1,3 +1,4 @@
+import { deferIssueEdit } from "../../github/services/deferred-issue-edits";
import { inboundStamp } from "../../github/utils/sync-echo";
import { withIntegrationLink } from "../../github/services/with-integration-link";
import type { GiteaConfig } from "../config";
@@ -88,100 +89,107 @@ export async function handleGiteaIssueClosed(
return issue;
}
};
- await withEchoConfirmation(readCurrent, (current) =>
- withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(externalLink.taskId, integration.projectId),
- });
+ await withEchoConfirmation(
+ readCurrent,
+ (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
+ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(
+ externalLink.taskId,
+ integration.projectId,
+ ),
+ });
- if (!task) {
- return;
- }
+ if (!task) {
+ return;
+ }
- const existingMetadata = parseLinkMetadata<
- Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
- >(externalLink.metadata, {
- externalLinkId: externalLink.id,
- source: "gitea_issue_closed",
- });
- if (
- inboundEcho(
- existingMetadata.lastSync?.state,
- "closed",
- issue.updated_at,
- current ? (current.state ?? issue.state) : undefined,
- { linkId: externalLink.id, field: "state" },
+ const existingMetadata = parseLinkMetadata<
+ Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
+ >(externalLink.metadata, {
+ externalLinkId: externalLink.id,
+ source: "gitea_issue_closed",
+ });
+ if (
+ inboundEcho(
+ existingMetadata.lastSync?.state,
+ "closed",
+ issue.updated_at,
+ current ? (current.state ?? issue.state) : undefined,
+ { linkId: externalLink.id, field: "state" },
+ )
)
- )
- return;
- const lastOutbound = existingMetadata.lastOutboundStateSyncAt;
- if (
- typeof lastOutbound === "number" &&
- Number.isFinite(lastOutbound) &&
- existingMetadata.state === "closed"
- ) {
- const eventMs = parseIssueUpdatedAtMs(issue);
+ return;
+ const lastOutbound = existingMetadata.lastOutboundStateSyncAt;
if (
- eventMs !== null &&
- Math.abs(eventMs - lastOutbound) <= OUTBOUND_STATE_ECHO_WINDOW_MS
+ typeof lastOutbound === "number" &&
+ Number.isFinite(lastOutbound) &&
+ existingMetadata.state === "closed"
) {
- return;
+ const eventMs = parseIssueUpdatedAtMs(issue);
+ if (
+ eventMs !== null &&
+ Math.abs(eventMs - lastOutbound) <=
+ OUTBOUND_STATE_ECHO_WINDOW_MS
+ ) {
+ return;
+ }
}
- }
- const targetStatus = await resolveTargetStatus(
- task.projectId,
- "issue_closed",
- "done",
- db,
- );
+ const targetStatus = await resolveTargetStatus(
+ task.projectId,
+ "issue_closed",
+ "done",
+ db,
+ );
- const statusResult = await updateTaskStatus(
- task.id,
- targetStatus,
- db,
- );
- if (
- statusResult.applied &&
- statusResult.before.status !== statusResult.after.status
- ) {
- afterCommit(() =>
- publishEvent("task.status_changed", {
- taskId: statusResult.after.id,
- projectId: statusResult.after.projectId,
- userId: null,
- oldStatus: statusResult.before.status,
- newStatus: statusResult.after.status,
- title: statusResult.after.title,
- assigneeId: statusResult.after.userId,
- type: "status_changed",
- }),
+ const statusResult = await updateTaskStatus(
+ task.id,
+ targetStatus,
+ db,
);
- }
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ afterCommit(() =>
+ publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ }),
+ );
+ }
- await updateExternalLink(
- externalLink.id,
- {
- metadata: {
- ...existingMetadata,
- state: "closed",
- lastSync: {
- ...existingMetadata.lastSync,
- state: inboundStamp(
- existingMetadata.lastSync?.state,
- "closed",
- "gitea",
- ),
+ await updateExternalLink(
+ externalLink.id,
+ {
+ metadata: {
+ ...existingMetadata,
+ state: "closed",
+ lastSync: {
+ ...existingMetadata.lastSync,
+ state: inboundStamp(
+ existingMetadata.lastSync?.state,
+ "closed",
+ "gitea",
+ ),
+ },
},
},
- },
- db,
- );
- },
- ),
+ db,
+ );
+ },
+ ),
+ () => deferIssueEdit(externalLink, integration, ["state"]),
);
}
}
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-edited.ts b/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
index 5c92a83bb..297311165 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
@@ -1,3 +1,4 @@
+import { deferIssueEdit } from "../../github/services/deferred-issue-edits";
import { inboundStamp } from "../../github/utils/sync-echo";
import { publishEvent } from "../../../events";
import { withIntegrationLink } from "../../github/services/with-integration-link";
@@ -84,121 +85,131 @@ export async function handleGiteaIssueEdited(
return issue;
}
};
- await withEchoConfirmation(readCurrent, (current) =>
- withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(externalLink.taskId, integration.projectId),
- });
-
- if (!task) {
- return;
- }
- const metadata = externalLink.metadata
- ? JSON.parse(externalLink.metadata)
- : {};
-
- const updateData: Record<string, unknown> = {};
- const updatedMetadata = { ...metadata };
+ await withEchoConfirmation(
+ readCurrent,
+ (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
+ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(
+ externalLink.taskId,
+ integration.projectId,
+ ),
+ });
+
+ if (!task) {
+ return;
+ }
+ const metadata = externalLink.metadata
+ ? JSON.parse(externalLink.metadata)
+ : {};
- if (!updatedMetadata.lastSync) {
- updatedMetadata.lastSync = {};
- }
+ const updateData: Record<string, unknown> = {};
+ const updatedMetadata = { ...metadata };
- if (changes.title) {
- const lastTitleSync = metadata.lastSync?.title;
+ if (!updatedMetadata.lastSync) {
+ updatedMetadata.lastSync = {};
+ }
- let shouldUpdateTitle = true;
+ if (changes.title) {
+ const lastTitleSync = metadata.lastSync?.title;
+
+ let shouldUpdateTitle = true;
+
+ if (lastTitleSync) {
+ if (
+ inboundEcho(
+ lastTitleSync,
+ issue.title,
+ issue.updated_at,
+ current?.title,
+ { linkId: externalLink.id, field: "title" },
+ )
+ ) {
+ shouldUpdateTitle = false;
+ }
+ }
- if (lastTitleSync) {
- if (
- inboundEcho(
- lastTitleSync,
+ if (shouldUpdateTitle) {
+ updateData.title = issue.title;
+ updatedMetadata.lastSync.title = inboundStamp(
+ metadata.lastSync?.title,
issue.title,
- issue.updated_at,
- current?.title,
- { linkId: externalLink.id, field: "title" },
- )
- ) {
- shouldUpdateTitle = false;
+ "gitea",
+ );
}
}
- if (shouldUpdateTitle) {
- updateData.title = issue.title;
- updatedMetadata.lastSync.title = inboundStamp(
- metadata.lastSync?.title,
- issue.title,
- "gitea",
+ if (changes.body) {
+ const lastDescSync = metadata.lastSync?.description;
+ const formattedDescription = formatTaskDescriptionFromIssue(
+ issue.body,
+ externalLink.taskId,
);
- }
- }
- if (changes.body) {
- const lastDescSync = metadata.lastSync?.description;
- const formattedDescription = formatTaskDescriptionFromIssue(
- issue.body,
- externalLink.taskId,
- );
-
- let shouldUpdateDescription = true;
+ let shouldUpdateDescription = true;
+
+ if (lastDescSync) {
+ if (
+ inboundEcho(
+ lastDescSync,
+ formattedDescription,
+ issue.updated_at,
+ current
+ ? formatTaskDescriptionFromIssue(
+ current.body ?? null,
+ task.id,
+ )
+ : undefined,
+ { linkId: externalLink.id, field: "description" },
+ )
+ ) {
+ shouldUpdateDescription = false;
+ }
+ }
- if (lastDescSync) {
- if (
- inboundEcho(
- lastDescSync,
+ if (shouldUpdateDescription) {
+ updateData.description = formattedDescription;
+ updatedMetadata.lastSync.description = inboundStamp(
+ metadata.lastSync?.description,
formattedDescription,
- issue.updated_at,
- current
- ? formatTaskDescriptionFromIssue(
- current.body ?? null,
- task.id,
- )
- : undefined,
- { linkId: externalLink.id, field: "description" },
- )
- ) {
- shouldUpdateDescription = false;
+ "gitea",
+ );
}
}
- if (shouldUpdateDescription) {
- updateData.description = formattedDescription;
- updatedMetadata.lastSync.description = inboundStamp(
- metadata.lastSync?.description,
- formattedDescription,
- "gitea",
+ if (Object.keys(updateData).length > 0) {
+ await db
+ .update(taskTable)
+ .set(updateData)
+ .where(linkedTaskScope(task.id, integration.projectId));
+
+ await updateExternalLink(
+ externalLink.id,
+ {
+ title: issue.title,
+ metadata: updatedMetadata,
+ },
+ db,
+ );
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: externalLink.taskId,
+ }),
);
}
- }
-
- if (Object.keys(updateData).length > 0) {
- await db
- .update(taskTable)
- .set(updateData)
- .where(linkedTaskScope(task.id, integration.projectId));
-
- await updateExternalLink(
- externalLink.id,
- {
- title: issue.title,
- metadata: updatedMetadata,
- },
- db,
- );
- afterCommit(() =>
- publishEvent("task.updated", {
- projectId: integration.projectId,
- taskId: externalLink.taskId,
- }),
- );
- }
-
- return;
- },
- ),
+
+ return;
+ },
+ ),
+ () =>
+ deferIssueEdit(externalLink, integration, [
+ ...(changes.title ? ["title" as const] : []),
+ ...(changes.body ? ["description" as const] : []),
+ ]),
);
}
}
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts b/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts
index ee83a799f..dcb9423fb 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-reopened.ts
@@ -1,3 +1,4 @@
+import { deferIssueEdit } from "../../github/services/deferred-issue-edits";
import { inboundStamp } from "../../github/utils/sync-echo";
import { withIntegrationLink } from "../../github/services/with-integration-link";
import type { GiteaConfig } from "../config";
@@ -90,103 +91,106 @@ export async function handleGiteaIssueReopened(
return issue;
}
};
- await withEchoConfirmation(readCurrent, (current) =>
- withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(
- externalLink.taskId,
- integration.projectId,
- ),
- });
+ await withEchoConfirmation(
+ readCurrent,
+ (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
+ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(
+ externalLink.taskId,
+ integration.projectId,
+ ),
+ });
- if (!task) {
- return;
- }
- const existingMetadata = parseLinkMetadata<
- Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
- >(externalLink.metadata, {
- externalLinkId: externalLink.id,
- source: "gitea_issue_reopened",
- });
- if (
- inboundEcho(
- existingMetadata.lastSync?.state,
- "open",
- issue.updated_at,
- current ? (current.state ?? issue.state) : undefined,
- { linkId: externalLink.id, field: "state" },
+ if (!task) {
+ return;
+ }
+ const existingMetadata = parseLinkMetadata<
+ Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
+ >(externalLink.metadata, {
+ externalLinkId: externalLink.id,
+ source: "gitea_issue_reopened",
+ });
+ if (
+ inboundEcho(
+ existingMetadata.lastSync?.state,
+ "open",
+ issue.updated_at,
+ current ? (current.state ?? issue.state) : undefined,
+ { linkId: externalLink.id, field: "state" },
+ )
)
- )
- return;
- const lastOutbound = existingMetadata.lastOutboundStateSyncAt;
- if (
- typeof lastOutbound === "number" &&
- Number.isFinite(lastOutbound) &&
- existingMetadata.state === "open"
- ) {
- const eventMs = parseIssueUpdatedAtMs(issue);
+ return;
+ const lastOutbound = existingMetadata.lastOutboundStateSyncAt;
if (
- eventMs !== null &&
- Math.abs(eventMs - lastOutbound) <=
- OUTBOUND_STATE_ECHO_WINDOW_MS
+ typeof lastOutbound === "number" &&
+ Number.isFinite(lastOutbound) &&
+ existingMetadata.state === "open"
) {
- return;
+ const eventMs = parseIssueUpdatedAtMs(issue);
+ if (
+ eventMs !== null &&
+ Math.abs(eventMs - lastOutbound) <=
+ OUTBOUND_STATE_ECHO_WINDOW_MS
+ ) {
+ return;
+ }
}
- }
- const targetStatus = await resolveTargetStatus(
- task.projectId,
- "issue_reopened",
- "to-do",
- db,
- );
+ const targetStatus = await resolveTargetStatus(
+ task.projectId,
+ "issue_reopened",
+ "to-do",
+ db,
+ );
- const statusResult = await updateTaskStatus(
- task.id,
- targetStatus,
- db,
- );
- if (
- statusResult.applied &&
- statusResult.before.status !== statusResult.after.status
- ) {
- afterCommit(() =>
- publishEvent("task.status_changed", {
- taskId: statusResult.after.id,
- projectId: statusResult.after.projectId,
- userId: null,
- oldStatus: statusResult.before.status,
- newStatus: statusResult.after.status,
- title: statusResult.after.title,
- assigneeId: statusResult.after.userId,
- type: "status_changed",
- }),
+ const statusResult = await updateTaskStatus(
+ task.id,
+ targetStatus,
+ db,
);
- }
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ afterCommit(() =>
+ publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ }),
+ );
+ }
- await updateExternalLink(
- externalLink.id,
- {
- metadata: {
- ...existingMetadata,
- state: "open",
- lastSync: {
- ...existingMetadata.lastSync,
- state: inboundStamp(
- existingMetadata.lastSync?.state,
- "open",
- "gitea",
- ),
+ await updateExternalLink(
+ externalLink.id,
+ {
+ metadata: {
+ ...existingMetadata,
+ state: "open",
+ lastSync: {
+ ...existingMetadata.lastSync,
+ state: inboundStamp(
+ existingMetadata.lastSync?.state,
+ "open",
+ "gitea",
+ ),
+ },
},
},
- },
- db,
- );
- },
- ),
+ db,
+ );
+ },
+ ),
+ () => deferIssueEdit(externalLink, integration, ["state"]),
);
} catch (error) {
if (error instanceof PendingResponseTimeout) throw error;
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 b1e4be861..1570ee606 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
@@ -1,3 +1,5 @@
+import type { IntegrationDatabase } from "./integration-task-scope";
+import type { IssueField } from "../utils/deferred-issue-edit";
import { publishEvent } from "../../../events";
import { taskTable } from "../../../database/schema";
import { resolveTargetStatus as resolveGiteaStatus } from "../../gitea/utils/resolve-column";
@@ -41,37 +43,14 @@ export async function applyObservedTaskValue(
)
)
return;
- if (field === "state") {
- const resolveStatus =
- integration.type === "gitea"
- ? resolveGiteaStatus
- : resolveGithubStatus;
- const status = await resolveStatus(
- integration.projectId,
- value === "closed" ? "issue_closed" : "issue_reopened",
- value === "closed" ? "done" : "to-do",
- tx,
- );
- const result = await updateTaskStatus(link.taskId, status, tx);
- if (result.applied && result.before.status !== result.after.status)
- afterCommit(() =>
- publishEvent("task.status_changed", {
- taskId: result.after.id,
- projectId: result.after.projectId,
- userId: null,
- oldStatus: result.before.status,
- newStatus: result.after.status,
- title: result.after.title,
- assigneeId: result.after.userId,
- type: "status_changed",
- }),
- );
- } else {
- await tx
- .update(taskTable)
- .set({ [field]: value })
- .where(linkedTaskScope(link.taskId, integration.projectId));
- }
+ await writeInboundTaskField(
+ tx,
+ afterCommit,
+ link,
+ integration,
+ field,
+ value,
+ );
await updateExternalLink(
link.id,
{
@@ -95,3 +74,42 @@ export async function applyObservedTaskValue(
},
);
}
+
+export async function writeInboundTaskField(
+ tx: IntegrationDatabase,
+ afterCommit: (effect: () => Promise<void>) => void,
+ link: { taskId: string },
+ integration: { projectId: string; type: string },
+ field: IssueField,
+ value: string,
+) {
+ if (field === "state") {
+ const resolveStatus =
+ integration.type === "gitea" ? resolveGiteaStatus : resolveGithubStatus;
+ const status = await resolveStatus(
+ integration.projectId,
+ value === "closed" ? "issue_closed" : "issue_reopened",
+ value === "closed" ? "done" : "to-do",
+ tx,
+ );
+ const result = await updateTaskStatus(link.taskId, status, tx);
+ if (result.applied && result.before.status !== result.after.status)
+ afterCommit(() =>
+ publishEvent("task.status_changed", {
+ taskId: result.after.id,
+ projectId: result.after.projectId,
+ userId: null,
+ oldStatus: result.before.status,
+ newStatus: result.after.status,
+ title: result.after.title,
+ assigneeId: result.after.userId,
+ type: "status_changed",
+ }),
+ );
+ } else {
+ await tx
+ .update(taskTable)
+ .set({ [field]: value })
+ .where(linkedTaskScope(link.taskId, integration.projectId));
+ }
+}
diff --git a/apps/api/src/plugins/github/services/deferred-issue-edits.ts b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
new file mode 100644
index 000000000..ac9d2e0cc
--- /dev/null
+++ b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
@@ -0,0 +1,228 @@
+import { and, asc, eq, gt, like } from "drizzle-orm";
+import db from "../../../database";
+import { externalLinkTable } from "../../../database/schema";
+import { publishEvent } from "../../../events";
+import type { GiteaConfig } from "../../gitea/config";
+import { createGiteaClient } from "../../gitea/utils/gitea-api";
+import type { GitHubConfig } from "../config";
+import {
+ issueEditScope,
+ parseDeferredIssueEdit,
+ type IssueField,
+} from "../utils/deferred-issue-edit";
+import { formatTaskDescriptionFromIssue } from "../utils/format";
+import { getVerifiedInstallationOctokit } from "../utils/github-app";
+import { inboundEcho, PendingEcho } from "../utils/inbound-echo";
+import { parseLinkMetadata } from "../utils/parse-link-metadata";
+import { inboundStamp, type SyncStamp } from "../utils/sync-echo";
+import { writeInboundTaskField } from "./apply-observed-task-value";
+import { updateExternalLink } from "./link-manager";
+import { withIntegrationLink } from "./with-integration-link";
+
+type Integration = {
+ id: string;
+ projectId: string;
+ type: string;
+ config: string;
+};
+type Metadata = Record<string, unknown> & {
+ lastSync?: Record<string, SyncStamp>;
+};
+const metadataFor = (link: { id: string; metadata: string | null }) =>
+ parseLinkMetadata<Metadata>(link.metadata, {
+ externalLinkId: link.id,
+ source: "deferred_issue_edit",
+ });
+
+// Commit the intent before acknowledging a delivery which cannot wait for its writer.
+export async function deferIssueEdit(
+ link: { id: string; taskId: string },
+ integration: Integration,
+ fields: IssueField[],
+) {
+ await withIntegrationLink(
+ link,
+ integration,
+ async (tx) => {
+ await updateExternalLink(
+ link.id,
+ { deferredEdit: { fields, scope: issueEditScope(integration) } },
+ tx,
+ );
+ },
+ integration,
+ );
+}
+
+async function readIssue(integration: Integration, externalId: string) {
+ const number = Number(externalId);
+ if (!Number.isSafeInteger(number) || number <= 0)
+ throw new Error("Invalid issue number");
+ if (integration.type === "gitea") {
+ const config = JSON.parse(integration.config) as GiteaConfig;
+ return createGiteaClient(config).getIssue(
+ config.repositoryOwner,
+ config.repositoryName,
+ number,
+ );
+ }
+ const config = JSON.parse(integration.config) as GitHubConfig;
+ const octokit = await getVerifiedInstallationOctokit(config, true);
+ return (
+ await octokit.rest.issues.get({
+ owner: config.repositoryOwner,
+ repo: config.repositoryName,
+ issue_number: number,
+ request: { timeout: 10_000 },
+ })
+ ).data;
+}
+
+let running = false;
+let cursor: string | undefined;
+export async function replayDeferredIssueEdits() {
+ if (running) return {};
+ running = true;
+ let degraded = false;
+ const deadline = Date.now() + 45_000;
+ try {
+ const links = await db.query.externalLinkTable.findMany({
+ where: and(
+ eq(externalLinkTable.resourceType, "issue"),
+ like(externalLinkTable.metadata, '%"deferredIssueEdit":%'),
+ cursor ? gt(externalLinkTable.id, cursor) : undefined,
+ ),
+ with: { integration: true },
+ orderBy: asc(externalLinkTable.id),
+ limit: 20,
+ });
+ if (!links.length) cursor = undefined;
+ let processed = 0;
+ for (const link of links) {
+ if (Date.now() >= deadline) break;
+ cursor = link.id;
+ processed++;
+ try {
+ const metadata = metadataFor(link);
+ const job = parseDeferredIssueEdit(metadata.deferredIssueEdit);
+ if (!job) continue;
+ const integration = link.integration;
+ if (
+ !integration?.isActive ||
+ !["github", "gitea"].includes(integration.type) ||
+ issueEditScope(integration) !== job.scope
+ ) {
+ await updateExternalLink(link.id, { completeDeferredEdit: job.id });
+ continue;
+ }
+ // An orphaned writer expires after five minutes; until then let it settle.
+ if (
+ job.fields.some((field) =>
+ metadata.lastSync?.[field]?.outbound?.some(
+ (entry) =>
+ entry.pending &&
+ !entry.cancelled &&
+ Date.now() - Date.parse(entry.timestamp) < 300_000,
+ ),
+ )
+ )
+ continue;
+ const issue = await readIssue(integration, link.externalId);
+ if (
+ typeof issue.title !== "string" ||
+ !["open", "closed"].includes(issue.state)
+ )
+ throw new Error("Invalid issue response");
+ const values = {
+ title: issue.title,
+ description: formatTaskDescriptionFromIssue(
+ issue.body ?? null,
+ link.taskId,
+ ),
+ state: issue.state,
+ };
+ await withIntegrationLink(
+ link,
+ integration,
+ async (tx, afterCommit, locked) => {
+ const current = metadataFor(locked);
+ if (
+ parseDeferredIssueEdit(current.deferredIssueEdit)?.id !== job.id
+ )
+ return;
+ if (
+ job.fields.some(
+ (field) =>
+ JSON.stringify(current.lastSync?.[field]) !==
+ JSON.stringify(metadata.lastSync?.[field]),
+ )
+ )
+ return;
+ // Classify every field before writing any: PendingEcho commits only its observation.
+ const accepted = job.fields.filter(
+ (field) =>
+ !(
+ field === "state" &&
+ integration.type === "github" &&
+ current.createdFrom === "kaneo"
+ ) &&
+ !inboundEcho(
+ current.lastSync?.[field],
+ values[field],
+ issue.updated_at,
+ values[field],
+ { linkId: link.id, field },
+ ),
+ );
+ for (const field of accepted) {
+ await writeInboundTaskField(
+ tx,
+ afterCommit,
+ link,
+ integration,
+ field,
+ values[field],
+ );
+ current.lastSync = {
+ ...current.lastSync,
+ [field]: inboundStamp(
+ current.lastSync?.[field],
+ values[field],
+ integration.type,
+ ),
+ };
+ if (field === "state") current.state = values.state;
+ }
+ await updateExternalLink(
+ link.id,
+ {
+ metadata: current,
+ ...(accepted.includes("title") ? { title: values.title } : {}),
+ completeDeferredEdit: job.id,
+ },
+ tx,
+ );
+ if (accepted.length)
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ taskId: link.taskId,
+ projectId: integration.projectId,
+ }),
+ );
+ },
+ integration,
+ );
+ } catch (error) {
+ if (error instanceof PendingEcho) continue;
+ degraded = true;
+ console.error("Deferred issue edit failed", {
+ externalLinkId: link.id,
+ });
+ }
+ }
+ if (processed === links.length && links.length < 20) cursor = undefined;
+ return { degraded };
+ } finally {
+ running = false;
+ }
+}
diff --git a/apps/api/src/plugins/github/services/integration-task-scope.ts b/apps/api/src/plugins/github/services/integration-task-scope.ts
index 1db5efb30..36e03f210 100644
--- a/apps/api/src/plugins/github/services/integration-task-scope.ts
+++ b/apps/api/src/plugins/github/services/integration-task-scope.ts
@@ -25,6 +25,7 @@ export async function withIntegrationTask<T>(
database: IntegrationDatabase,
afterCommit: (effect: () => Promise<void>) => void,
) => Promise<T>,
+ expectedBinding?: { config: string; type: string },
): Promise<T | undefined> {
const effects: Array<() => Promise<void>> = [];
const result = await db.transaction(async (tx) => {
@@ -46,6 +47,21 @@ export async function withIntegrationTask<T>(
)
.for("key share", { of: [projectTable, integrationTable] });
if (!project) return undefined;
+ if (expectedBinding) {
+ const [binding] = await tx
+ .select({ id: integrationTable.id })
+ .from(integrationTable)
+ .where(
+ and(
+ eq(integrationTable.id, integration.id),
+ eq(integrationTable.config, expectedBinding.config),
+ eq(integrationTable.type, expectedBinding.type),
+ eq(integrationTable.isActive, true),
+ ),
+ )
+ .for("share");
+ if (!binding) return undefined;
+ }
if (taskId !== null) {
const [task] = await tx
.select({ id: taskTable.id })
diff --git a/apps/api/src/plugins/github/services/link-manager.ts b/apps/api/src/plugins/github/services/link-manager.ts
index 1c387194f..4e0ece4d0 100644
--- a/apps/api/src/plugins/github/services/link-manager.ts
+++ b/apps/api/src/plugins/github/services/link-manager.ts
@@ -1,3 +1,8 @@
+import { randomUUID } from "node:crypto";
+import {
+ parseDeferredIssueEdit,
+ type IssueField,
+} from "../utils/deferred-issue-edit";
import { mergeSyncMetadata } from "../utils/merge-sync-metadata";
import { parseLinkMetadata } from "../utils/parse-link-metadata";
import { outboundStamp, type SyncStamp } from "../utils/sync-echo";
@@ -24,6 +29,8 @@ export type CreateExternalLinkParams = {
};
export type UpdateExternalLinkParams = {
+ deferredEdit?: { fields: IssueField[]; scope: string };
+ completeDeferredEdit?: string;
outbound?: {
field: "title" | "description" | "state";
value: string;
@@ -133,7 +140,13 @@ export async function updateExternalLink(
params: UpdateExternalLinkParams,
database: DbOrTx = db,
) {
- if (params.outbound || params.metadata || params.observedOutbound) {
+ if (
+ params.outbound ||
+ params.metadata ||
+ params.observedOutbound ||
+ params.deferredEdit ||
+ params.completeDeferredEdit
+ ) {
await database.transaction(async (tx) => {
const link = await lockExternalLink(id, tx);
if (!link) return;
@@ -141,6 +154,26 @@ export async function updateExternalLink(
Record<string, unknown> & { lastSync?: Record<string, SyncStamp> }
>(link.metadata, { externalLinkId: id, source: "sync_update" });
const merged = mergeSyncMetadata(metadata, params.metadata ?? {});
+ // Ordinary sync metadata cannot resurrect or clear a scheduler job.
+ delete merged.deferredIssueEdit;
+ if (metadata.deferredIssueEdit)
+ merged.deferredIssueEdit = metadata.deferredIssueEdit;
+ const previousJob = parseDeferredIssueEdit(metadata.deferredIssueEdit);
+ if (params.completeDeferredEdit === previousJob?.id)
+ delete merged.deferredIssueEdit;
+ if (params.deferredEdit) {
+ const { fields, scope } = params.deferredEdit;
+ merged.deferredIssueEdit = {
+ id: randomUUID(),
+ scope,
+ fields: [
+ ...new Set([
+ ...(previousJob?.scope === scope ? previousJob.fields : []),
+ ...fields,
+ ]),
+ ],
+ };
+ }
if (params.observedOutbound) {
const { field, intentId, updatedAt } = params.observedOutbound;
const stamp = merged.lastSync?.[field];
diff --git a/apps/api/src/plugins/github/services/with-integration-link.ts b/apps/api/src/plugins/github/services/with-integration-link.ts
index cb52494c1..30a88a0c2 100644
--- a/apps/api/src/plugins/github/services/with-integration-link.ts
+++ b/apps/api/src/plugins/github/services/with-integration-link.ts
@@ -15,6 +15,7 @@ export function withIntegrationLink<T>(
afterCommit: (effect: () => Promise<void>) => void,
lockedLink: typeof externalLinkTable.$inferSelect,
) => Promise<T>,
+ expectedBinding?: { config: string; type: string },
) {
return withIntegrationTask(
link.taskId,
@@ -53,5 +54,6 @@ export function withIntegrationLink<T>(
return error;
}
},
+ expectedBinding,
);
}
diff --git a/apps/api/src/plugins/github/utils/deferred-issue-edit.ts b/apps/api/src/plugins/github/utils/deferred-issue-edit.ts
new file mode 100644
index 000000000..4fb5a7ee7
--- /dev/null
+++ b/apps/api/src/plugins/github/utils/deferred-issue-edit.ts
@@ -0,0 +1,46 @@
+import { createHash } from "node:crypto";
+import { normalizeGiteaBaseUrl } from "../../gitea/config";
+
+export type IssueField = "title" | "description" | "state";
+export type DeferredIssueEdit = {
+ id: string;
+ fields: IssueField[];
+ scope: string;
+};
+
+export function parseDeferredIssueEdit(
+ value: unknown,
+): DeferredIssueEdit | undefined {
+ if (!value || typeof value !== "object") return;
+ const job = value as DeferredIssueEdit;
+ if (
+ typeof job.id !== "string" ||
+ typeof job.scope !== "string" ||
+ !Array.isArray(job.fields) ||
+ !job.fields.length ||
+ job.fields.some(
+ (field) => !["title", "description", "state"].includes(field),
+ )
+ )
+ return;
+ return job;
+}
+
+// Bind only repository identity; tokens and webhook secrets never enter metadata.
+export function issueEditScope(integration: { type: string; config: string }) {
+ const config = JSON.parse(integration.config);
+ return createHash("sha256")
+ .update(
+ JSON.stringify([
+ integration.type,
+ integration.type === "gitea"
+ ? normalizeGiteaBaseUrl(config.baseUrl)
+ : null,
+ config.repositoryOwner,
+ config.repositoryName,
+ integration.type === "github" ? config.installationId : null,
+ integration.type === "github" ? config.repositoryId : null,
+ ]),
+ )
+ .digest("hex");
+}
diff --git a/apps/api/src/plugins/github/utils/inbound-echo.ts b/apps/api/src/plugins/github/utils/inbound-echo.ts
index c21b78f5a..e5c8a458e 100644
--- a/apps/api/src/plugins/github/utils/inbound-echo.ts
+++ b/apps/api/src/plugins/github/utils/inbound-echo.ts
@@ -40,7 +40,8 @@ export function inboundEcho(
export async function withEchoConfirmation<Provider, Result>(
read: () => Promise<Provider>,
apply: (current?: Provider) => Promise<Result>,
-): Promise<Result> {
+ defer?: () => Promise<void>,
+): Promise<Result | undefined> {
const recorded = new Set<string>();
let current: Provider | undefined;
let delay = 50;
@@ -66,10 +67,22 @@ export async function withEchoConfirmation<Provider, Result>(
});
recorded.add(key);
}
- if (Date.now() >= deadline)
+ if (Date.now() >= deadline) {
+ if (defer) {
+ try {
+ await defer();
+ return;
+ } catch (cause) {
+ throw new PendingResponseTimeout(
+ "Could not persist deferred webhook delivery",
+ { cause },
+ );
+ }
+ }
throw new PendingResponseTimeout(
"Outbound response is still pending; retry this webhook delivery",
);
+ }
await new Promise((resolve) =>
setTimeout(resolve, Math.min(delay, deadline - Date.now())),
);
diff --git a/apps/api/src/plugins/github/webhooks/issue-closed.ts b/apps/api/src/plugins/github/webhooks/issue-closed.ts
index 771c50698..94cf95742 100644
--- a/apps/api/src/plugins/github/webhooks/issue-closed.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-closed.ts
@@ -1,3 +1,4 @@
+import { deferIssueEdit } from "../services/deferred-issue-edits";
import type { GitHubConfig } from "../config";
import { getVerifiedInstallationOctokit } from "../utils/github-app";
import { inboundEcho, withEchoConfirmation } from "../utils/inbound-echo";
@@ -67,91 +68,97 @@ export async function handleIssueClosed(payload: IssueClosedPayload) {
return issue;
}
};
- await withEchoConfirmation(readCurrent, (current) =>
- withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(externalLink.taskId, integration.projectId),
- });
+ await withEchoConfirmation(
+ readCurrent,
+ (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
+ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(
+ externalLink.taskId,
+ integration.projectId,
+ ),
+ });
- if (!task) {
- return;
- }
+ if (!task) {
+ return;
+ }
- const existingMetadata = parseLinkMetadata<
- Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
- >(externalLink.metadata, {
- externalLinkId: externalLink.id,
- source: "issue_closed",
- });
+ const existingMetadata = parseLinkMetadata<
+ Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
+ >(externalLink.metadata, {
+ externalLinkId: externalLink.id,
+ source: "issue_closed",
+ });
- if (existingMetadata.createdFrom === "kaneo") {
- return;
- }
+ if (existingMetadata.createdFrom === "kaneo") {
+ return;
+ }
- if (
- inboundEcho(
- existingMetadata.lastSync?.state,
- "closed",
- issue.updated_at,
- current?.state,
- { linkId: externalLink.id, field: "state" },
+ if (
+ inboundEcho(
+ existingMetadata.lastSync?.state,
+ "closed",
+ issue.updated_at,
+ current?.state,
+ { linkId: externalLink.id, field: "state" },
+ )
)
- )
- return;
+ return;
- const targetStatus = await resolveTargetStatus(
- task.projectId,
- "issue_closed",
- "done",
- db,
- );
+ const targetStatus = await resolveTargetStatus(
+ task.projectId,
+ "issue_closed",
+ "done",
+ db,
+ );
- const statusResult = await updateTaskStatus(
- task.id,
- targetStatus,
- db,
- );
- if (
- statusResult.applied &&
- statusResult.before.status !== statusResult.after.status
- ) {
- afterCommit(() =>
- publishEvent("task.status_changed", {
- taskId: statusResult.after.id,
- projectId: statusResult.after.projectId,
- userId: null,
- oldStatus: statusResult.before.status,
- newStatus: statusResult.after.status,
- title: statusResult.after.title,
- assigneeId: statusResult.after.userId,
- type: "status_changed",
- }),
+ const statusResult = await updateTaskStatus(
+ task.id,
+ targetStatus,
+ db,
);
- }
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ afterCommit(() =>
+ publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ }),
+ );
+ }
- await updateExternalLink(
- externalLink.id,
- {
- metadata: {
- ...existingMetadata,
- state: "closed",
- lastSync: {
- ...existingMetadata.lastSync,
- state: inboundStamp(
- existingMetadata.lastSync?.state,
- "closed",
- "github",
- ),
+ await updateExternalLink(
+ externalLink.id,
+ {
+ metadata: {
+ ...existingMetadata,
+ state: "closed",
+ lastSync: {
+ ...existingMetadata.lastSync,
+ state: inboundStamp(
+ existingMetadata.lastSync?.state,
+ "closed",
+ "github",
+ ),
+ },
},
},
- },
- db,
- );
- },
- ),
+ db,
+ );
+ },
+ ),
+ () => deferIssueEdit(externalLink, integration, ["state"]),
);
}
}
diff --git a/apps/api/src/plugins/github/webhooks/issue-edited.ts b/apps/api/src/plugins/github/webhooks/issue-edited.ts
index f6bd00252..317702eaa 100644
--- a/apps/api/src/plugins/github/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-edited.ts
@@ -1,3 +1,4 @@
+import { deferIssueEdit } from "../services/deferred-issue-edits";
import { inboundStamp } from "../../github/utils/sync-echo";
import { publishEvent } from "../../../events";
import { withIntegrationLink } from "../services/with-integration-link";
@@ -86,144 +87,154 @@ export async function handleIssueEdited(payload: IssueEditedPayload) {
return issue;
}
};
- await withEchoConfirmation(readCurrent, (current) =>
- withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(externalLink.taskId, integration.projectId),
- });
-
- if (!task) {
- console.error(`Task ${externalLink.taskId} not found`);
- return;
- }
- const metadata = parseLinkMetadata<IssueEditedMetadata>(
- externalLink.metadata,
- {
- externalLinkId: externalLink.id,
- source: "issue_edited",
- },
- );
-
- const updateData: Record<string, unknown> = {};
- const updatedMetadata: IssueEditedMetadata = { ...metadata };
-
- if (!updatedMetadata.lastSync) {
- updatedMetadata.lastSync = {};
- }
-
- if (changes.title) {
- const lastTitleSync = metadata.lastSync?.title;
-
- let shouldUpdateTitle = true;
-
- if (lastTitleSync) {
- if (
- inboundEcho(
- lastTitleSync,
+ await withEchoConfirmation(
+ readCurrent,
+ (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
+ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(
+ externalLink.taskId,
+ integration.projectId,
+ ),
+ });
+
+ if (!task) {
+ console.error(`Task ${externalLink.taskId} not found`);
+ return;
+ }
+ const metadata = parseLinkMetadata<IssueEditedMetadata>(
+ externalLink.metadata,
+ {
+ externalLinkId: externalLink.id,
+ source: "issue_edited",
+ },
+ );
+
+ const updateData: Record<string, unknown> = {};
+ const updatedMetadata: IssueEditedMetadata = { ...metadata };
+
+ if (!updatedMetadata.lastSync) {
+ updatedMetadata.lastSync = {};
+ }
+
+ if (changes.title) {
+ const lastTitleSync = metadata.lastSync?.title;
+
+ let shouldUpdateTitle = true;
+
+ if (lastTitleSync) {
+ if (
+ inboundEcho(
+ lastTitleSync,
+ issue.title,
+ issue.updated_at,
+ current?.title,
+ { linkId: externalLink.id, field: "title" },
+ )
+ ) {
+ console.log(
+ "Skipping title update - already synced from Kaneo",
+ );
+ shouldUpdateTitle = false;
+ }
+ }
+
+ if (shouldUpdateTitle) {
+ updateData.title = issue.title;
+ updatedMetadata.lastSync.title = inboundStamp(
+ metadata.lastSync?.title,
issue.title,
- issue.updated_at,
- current?.title,
- { linkId: externalLink.id, field: "title" },
- )
- ) {
+ "github",
+ );
console.log(
- "Skipping title update - already synced from Kaneo",
+ `Updating task title from GitHub: "${changes.title.from}" → "${issue.title}"`,
);
- shouldUpdateTitle = false;
}
}
- if (shouldUpdateTitle) {
- updateData.title = issue.title;
- updatedMetadata.lastSync.title = inboundStamp(
- metadata.lastSync?.title,
- issue.title,
- "github",
- );
- console.log(
- `Updating task title from GitHub: "${changes.title.from}" → "${issue.title}"`,
+ if (changes.body) {
+ const lastDescSync = metadata.lastSync?.description;
+ const formattedDescription = formatTaskDescriptionFromIssue(
+ issue.body,
+ task.id,
);
- }
- }
-
- if (changes.body) {
- const lastDescSync = metadata.lastSync?.description;
- const formattedDescription = formatTaskDescriptionFromIssue(
- issue.body,
- task.id,
- );
- let shouldUpdateDescription = true;
+ let shouldUpdateDescription = true;
+
+ if (lastDescSync) {
+ if (
+ inboundEcho(
+ lastDescSync,
+ formattedDescription,
+ issue.updated_at,
+ current
+ ? formatTaskDescriptionFromIssue(
+ current.body ?? null,
+ task.id,
+ )
+ : undefined,
+ { linkId: externalLink.id, field: "description" },
+ )
+ ) {
+ console.log(
+ "Skipping description update - already synced from Kaneo",
+ );
+ shouldUpdateDescription = false;
+ }
+ }
- if (lastDescSync) {
- if (
- inboundEcho(
- lastDescSync,
+ if (shouldUpdateDescription) {
+ updateData.description = formattedDescription;
+ updatedMetadata.lastSync.description = inboundStamp(
+ metadata.lastSync?.description,
formattedDescription,
- issue.updated_at,
- current
- ? formatTaskDescriptionFromIssue(
- current.body ?? null,
- task.id,
- )
- : undefined,
- { linkId: externalLink.id, field: "description" },
- )
- ) {
- console.log(
- "Skipping description update - already synced from Kaneo",
+ "github",
);
- shouldUpdateDescription = false;
+ console.log("Updating task description from GitHub");
}
}
- if (shouldUpdateDescription) {
- updateData.description = formattedDescription;
- updatedMetadata.lastSync.description = inboundStamp(
- metadata.lastSync?.description,
- formattedDescription,
- "github",
+ if (Object.keys(updateData).length > 0) {
+ await db
+ .update(taskTable)
+ .set(updateData)
+ .where(linkedTaskScope(task.id, integration.projectId));
+
+ await updateExternalLink(
+ externalLink.id,
+ {
+ title: issue.title,
+ metadata: updatedMetadata,
+ },
+ db,
+ );
+ afterCommit(() =>
+ publishEvent("task.updated", {
+ projectId: integration.projectId,
+ taskId: externalLink.taskId,
+ }),
);
- console.log("Updating task description from GitHub");
- }
- }
-
- if (Object.keys(updateData).length > 0) {
- await db
- .update(taskTable)
- .set(updateData)
- .where(linkedTaskScope(task.id, integration.projectId));
-
- await updateExternalLink(
- externalLink.id,
- {
- title: issue.title,
- metadata: updatedMetadata,
- },
- db,
- );
- afterCommit(() =>
- publishEvent("task.updated", {
- projectId: integration.projectId,
- taskId: externalLink.taskId,
- }),
- );
- console.log(
- `Synced ${Object.keys(updateData).join(", ")} from GitHub issue #${issue.number} to task ${task.id}`,
- );
- } else {
- console.log(
- `No updates needed for task ${task.id} from issue #${issue.number}`,
- );
- }
+ console.log(
+ `Synced ${Object.keys(updateData).join(", ")} from GitHub issue #${issue.number} to task ${task.id}`,
+ );
+ } else {
+ console.log(
+ `No updates needed for task ${task.id} from issue #${issue.number}`,
+ );
+ }
- return;
- },
- ),
+ return;
+ },
+ ),
+ () =>
+ deferIssueEdit(externalLink, integration, [
+ ...(changes.title ? ["title" as const] : []),
+ ...(changes.body ? ["description" as const] : []),
+ ]),
);
}
}
diff --git a/apps/api/src/plugins/github/webhooks/issue-reopened.ts b/apps/api/src/plugins/github/webhooks/issue-reopened.ts
index c128f1d4c..4a05a36d2 100644
--- a/apps/api/src/plugins/github/webhooks/issue-reopened.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-reopened.ts
@@ -1,3 +1,4 @@
+import { deferIssueEdit } from "../services/deferred-issue-edits";
import type { GitHubConfig } from "../config";
import { getVerifiedInstallationOctokit } from "../utils/github-app";
import { inboundEcho, withEchoConfirmation } from "../utils/inbound-echo";
@@ -67,94 +68,100 @@ export async function handleIssueReopened(payload: IssueReopenedPayload) {
return issue;
}
};
- await withEchoConfirmation(readCurrent, (current) =>
- withIntegrationLink(
- externalLink,
- integration,
- async (db, afterCommit, externalLink) => {
- const task = await db.query.taskTable.findFirst({
- where: linkedTaskScope(externalLink.taskId, integration.projectId),
- });
+ await withEchoConfirmation(
+ readCurrent,
+ (current) =>
+ withIntegrationLink(
+ externalLink,
+ integration,
+ async (db, afterCommit, externalLink) => {
+ const task = await db.query.taskTable.findFirst({
+ where: linkedTaskScope(
+ externalLink.taskId,
+ integration.projectId,
+ ),
+ });
- if (!task) {
- return;
- }
+ if (!task) {
+ return;
+ }
- const existingMetadata = parseLinkMetadata<
- Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
- >(externalLink.metadata, {
- externalLinkId: externalLink.id,
- source: "issue_reopened",
- });
+ const existingMetadata = parseLinkMetadata<
+ Record<string, unknown> & { lastSync?: { state?: SyncStamp } }
+ >(externalLink.metadata, {
+ externalLinkId: externalLink.id,
+ source: "issue_reopened",
+ });
- if (
- existingMetadata.createdFrom === "kaneo" ||
- existingMetadata.state === "open"
- ) {
- return;
- }
+ if (
+ existingMetadata.createdFrom === "kaneo" ||
+ existingMetadata.state === "open"
+ ) {
+ return;
+ }
- if (
- inboundEcho(
- existingMetadata.lastSync?.state,
- "open",
- issue.updated_at,
- current?.state,
- { linkId: externalLink.id, field: "state" },
+ if (
+ inboundEcho(
+ existingMetadata.lastSync?.state,
+ "open",
+ issue.updated_at,
+ current?.state,
+ { linkId: externalLink.id, field: "state" },
+ )
)
- )
- return;
+ return;
- const targetStatus = await resolveTargetStatus(
- task.projectId,
- "issue_reopened",
- "to-do",
- db,
- );
+ const targetStatus = await resolveTargetStatus(
+ task.projectId,
+ "issue_reopened",
+ "to-do",
+ db,
+ );
- const statusResult = await updateTaskStatus(
- task.id,
- targetStatus,
- db,
- );
- if (
- statusResult.applied &&
- statusResult.before.status !== statusResult.after.status
- ) {
- afterCommit(() =>
- publishEvent("task.status_changed", {
- taskId: statusResult.after.id,
- projectId: statusResult.after.projectId,
- userId: null,
- oldStatus: statusResult.before.status,
- newStatus: statusResult.after.status,
- title: statusResult.after.title,
- assigneeId: statusResult.after.userId,
- type: "status_changed",
- }),
+ const statusResult = await updateTaskStatus(
+ task.id,
+ targetStatus,
+ db,
);
- }
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ afterCommit(() =>
+ publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ }),
+ );
+ }
- await updateExternalLink(
- externalLink.id,
- {
- metadata: {
- ...existingMetadata,
- state: "open",
- lastSync: {
- ...existingMetadata.lastSync,
- state: inboundStamp(
- existingMetadata.lastSync?.state,
- "open",
- "github",
- ),
+ await updateExternalLink(
+ externalLink.id,
+ {
+ metadata: {
+ ...existingMetadata,
+ state: "open",
+ lastSync: {
+ ...existingMetadata.lastSync,
+ state: inboundStamp(
+ existingMetadata.lastSync?.state,
+ "open",
+ "github",
+ ),
+ },
},
},
- },
- db,
- );
- },
- ),
+ db,
+ );
+ },
+ ),
+ () => deferIssueEdit(externalLink, integration, ["state"]),
);
}
}
diff --git a/apps/api/src/scheduler/index.ts b/apps/api/src/scheduler/index.ts
index 7e671422f..0b596374c 100644
--- a/apps/api/src/scheduler/index.ts
+++ b/apps/api/src/scheduler/index.ts
@@ -1,3 +1,4 @@
+import { replayDeferredIssueEdits } from "../plugins/github/services/deferred-issue-edits";
import * as Sentry from "@sentry/node";
import { Cron } from "croner";
import { checkDueDateReminders } from "./due-date-reminders";
@@ -45,6 +46,12 @@ function withCheckIn<T>(name: string, fn: () => Promise<T>) {
}
export function initializeScheduler(): void {
+ jobs.push(
+ new Cron(
+ "* * * * *",
+ withCheckIn("deferred-issue-edits", replayDeferredIssueEdits),
+ ),
+ );
jobs.push(
new Cron(
"*/5 * * * *",
@@ -67,7 +74,7 @@ export function initializeScheduler(): void {
new Cron("23 * * * *", withCheckIn("trial-reminders", checkTrialReminders)),
);
console.log(
- "⏰ Scheduler started (reminders every 5 minutes, seat reconciliation and trial reminders hourly)",
+ "⏰ Scheduler started (deferred integration edits every minute, reminders every 5 minutes, seat reconciliation and trial reminders hourly)",
);
}
diff --git a/tests/api-integration/deferred-issue-edits.test.ts b/tests/api-integration/deferred-issue-edits.test.ts
new file mode 100644
index 000000000..ed1a5fb8d
--- /dev/null
+++ b/tests/api-integration/deferred-issue-edits.test.ts
@@ -0,0 +1,276 @@
+import { eq } from "drizzle-orm";
+import { beforeEach, expect, it, vi } from "vite-plus/test";
+import db, { schema } from "../../apps/api/src/database";
+import { publishEvent } from "../../apps/api/src/events";
+import {
+ deferIssueEdit,
+ replayDeferredIssueEdits,
+} from "../../apps/api/src/plugins/github/services/deferred-issue-edits";
+import { updateExternalLink } from "../../apps/api/src/plugins/github/services/link-manager";
+import { handleIssueEdited } from "../../apps/api/src/plugins/github/webhooks/issue-edited";
+import {
+ inboundStamp,
+ outboundStamp,
+} from "../../apps/api/src/plugins/github/utils/sync-echo";
+import { resetTestDatabase } from "./helpers/database";
+import {
+ createProjectFixture,
+ createWorkspaceMember,
+} from "./helpers/fixtures";
+const m = vi.hoisted(() => ({ integrations: [] as unknown[], read: vi.fn() }));
+vi.mock("../../apps/api/src/events", () => ({
+ publishEvent: vi.fn(async () => undefined),
+}));
+vi.mock(
+ "../../apps/api/src/plugins/github/services/task-service",
+ async (original) => ({
+ ...(await original<
+ typeof import("../../apps/api/src/plugins/github/services/task-service")
+ >()),
+ findAllIntegrationsByRepo: async () => m.integrations,
+ }),
+);
+vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({
+ getVerifiedInstallationOctokit: async () => ({
+ rest: { issues: { get: async () => ({ data: await m.read() }) } },
+ }),
+}));
+vi.mock("../../apps/api/src/plugins/gitea/utils/gitea-api", () => ({
+ createGiteaClient: () => ({ getIssue: () => m.read() }),
+}));
+beforeEach(async () => {
+ await resetTestDatabase();
+ vi.clearAllMocks();
+ m.read.mockReset().mockResolvedValue({
+ title: "A",
+ body: "recovered body",
+ state: "closed",
+ updated_at: "2026-09-30T00:00:03Z",
+ });
+});
+async function seed(type = "github") {
+ 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: "old body",
+ status: "to-do",
+ })
+ .returning();
+ const [integration] = await db
+ .insert(schema.integrationTable)
+ .values({
+ projectId: project.id,
+ type,
+ config: JSON.stringify({
+ baseUrl: "https://git.example",
+ repositoryOwner: "owner",
+ repositoryName: "repo",
+ installationId: 10,
+ repositoryId: 20,
+ accessToken: "test-secret-never-metadata",
+ }),
+ })
+ .returning();
+ const stamp = inboundStamp(
+ outboundStamp(undefined, "A", undefined, {
+ intentId: "dead-writer",
+ pending: true,
+ }),
+ "B",
+ type,
+ );
+ const [link] = await db
+ .insert(schema.externalLinkTable)
+ .values({
+ taskId: task.id,
+ integrationId: integration.id,
+ resourceType: "issue",
+ externalId: "1",
+ url: "https://git.example/owner/repo/issues/1",
+ metadata: JSON.stringify({ lastSync: { title: stamp } }),
+ })
+ .returning();
+ m.integrations = [integration];
+ return { project, task, integration, link };
+}
+async function metadata(linkId: string) {
+ const link = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.id, linkId),
+ });
+ return JSON.parse(link?.metadata ?? "{}");
+}
+async function current(taskId: string) {
+ return db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, taskId),
+ });
+}
+it("durably acknowledges an orphaned delivery and later recovers every changed field", async () => {
+ const { task, integration, link } = await seed();
+ await handleIssueEdited({
+ action: "edited",
+ issue: {
+ number: 1,
+ title: "A",
+ body: "recovered body",
+ html_url: link.url,
+ updated_at: "2026-09-30T00:00:03Z",
+ },
+ changes: { title: { from: "B" }, body: { from: "old body" } },
+ repository: {
+ id: 20,
+ owner: { login: "owner" },
+ name: "repo",
+ full_name: "owner/repo",
+ },
+ });
+ const queued = await metadata(link.id);
+ expect(queued.deferredIssueEdit.fields).toEqual(["title", "description"]);
+ expect(JSON.stringify(queued)).not.toContain("test-secret-never-metadata");
+ expect(JSON.stringify(queued)).not.toContain("recovered body");
+ expect((await current(task.id))?.title).toBe("B");
+ await replayDeferredIssueEdits();
+ expect(m.read).not.toHaveBeenCalled();
+ queued.lastSync.title.outbound[0].timestamp = new Date(
+ Date.now() - 300_001,
+ ).toISOString();
+ await db
+ .update(schema.externalLinkTable)
+ .set({ metadata: JSON.stringify(queued) })
+ .where(eq(schema.externalLinkTable.id, link.id));
+ await replayDeferredIssueEdits();
+ expect(await current(task.id)).toMatchObject({
+ title: "A",
+ description: "recovered body",
+ });
+ expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();
+ expect(publishEvent).toHaveBeenCalledWith("task.updated", {
+ taskId: task.id,
+ projectId: integration.projectId,
+ });
+}, 15_000);
+it.each(["github", "gitea"])(
+ "recovers orphaned %s state through project workflows",
+ async (type) => {
+ const { task, integration, link } = await seed(type);
+ await deferIssueEdit(link, integration, ["state"]);
+ await replayDeferredIssueEdits();
+ expect((await current(task.id))?.status).toBe("done");
+ expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();
+ expect(publishEvent).toHaveBeenCalledWith(
+ "task.status_changed",
+ expect.objectContaining({
+ taskId: task.id,
+ oldStatus: "to-do",
+ newStatus: "done",
+ }),
+ );
+ },
+);
+it("retains failed provider reads and retries without sender redelivery", async () => {
+ const { task, integration, link } = await seed();
+ await deferIssueEdit(link, integration, ["description"]);
+ m.read.mockRejectedValueOnce(new Error("provider unavailable"));
+ expect(await replayDeferredIssueEdits()).toEqual({ degraded: true });
+ expect((await metadata(link.id)).deferredIssueEdit.fields).toEqual([
+ "description",
+ ]);
+ await replayDeferredIssueEdits();
+ expect((await current(task.id))?.description).toBe("recovered body");
+ expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();
+});
+it("does not clear fields queued concurrently with a provider read", async () => {
+ const { task, integration, link } = await seed();
+ await deferIssueEdit(link, integration, ["state"]);
+ m.read.mockImplementationOnce(async () => {
+ await deferIssueEdit(link, integration, ["description"]);
+ return { title: "A", body: "stale read", state: "closed" };
+ });
+ await replayDeferredIssueEdits();
+ expect(await current(task.id)).toMatchObject({
+ status: "to-do",
+ description: "old body",
+ });
+ expect((await metadata(link.id)).deferredIssueEdit.fields).toEqual([
+ "state",
+ "description",
+ ]);
+ await replayDeferredIssueEdits();
+ expect(await current(task.id)).toMatchObject({
+ status: "done",
+ description: "recovered body",
+ });
+});
+it.each(["disabled", "rebound", "moved"])(
+ "guards deferred edits when their integration is %s during HTTP",
+ async (change) => {
+ const { project, task, integration, link } = await seed();
+ await deferIssueEdit(link, integration, ["description"]);
+ m.read.mockImplementationOnce(async () => {
+ if (change === "moved") {
+ const other = await createProjectFixture({
+ workspaceId: project.workspaceId,
+ });
+ await db
+ .update(schema.taskTable)
+ .set({ projectId: other.project.id })
+ .where(eq(schema.taskTable.id, task.id));
+ } else {
+ await db
+ .update(schema.integrationTable)
+ .set(
+ change === "disabled"
+ ? { isActive: false }
+ : {
+ config: JSON.stringify({
+ ...JSON.parse(integration.config),
+ repositoryName: "other",
+ }),
+ },
+ )
+ .where(eq(schema.integrationTable.id, integration.id));
+ }
+ return { title: "A", body: "wrong source", state: "closed" };
+ });
+ await replayDeferredIssueEdits();
+ expect((await current(task.id))?.description).toBe("old body");
+ expect(publishEvent).not.toHaveBeenCalled();
+ if (change !== "moved") {
+ await replayDeferredIssueEdits();
+ expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();
+ }
+ },
+);
+it("rechecks provider state after a newer inbound edit during HTTP", async () => {
+ const { task, integration, link } = await seed();
+ await deferIssueEdit(link, integration, ["description"]);
+ m.read.mockImplementationOnce(async () => {
+ await db
+ .update(schema.taskTable)
+ .set({ description: "newer edit" })
+ .where(eq(schema.taskTable.id, task.id));
+ await updateExternalLink(link.id, {
+ metadata: {
+ lastSync: {
+ description: inboundStamp(undefined, "newer edit", "github"),
+ },
+ },
+ });
+ return { title: "A", body: "stale read", state: "closed" };
+ });
+ await replayDeferredIssueEdits();
+ expect((await current(task.id))?.description).toBe("newer edit");
+ expect((await metadata(link.id)).deferredIssueEdit).toBeDefined();
+ m.read.mockResolvedValueOnce({
+ title: "A",
+ body: "newer edit",
+ state: "closed",
+ });
+ await replayDeferredIssueEdits();
+ expect((await current(task.id))?.description).toBe("newer edit");
+ expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();
+});
diff --git a/tests/api/plugins/github/outbound-intent.test.ts b/tests/api/plugins/github/outbound-intent.test.ts
index 27eecb2a3..2496fadbb 100644
--- a/tests/api/plugins/github/outbound-intent.test.ts
+++ b/tests/api/plugins/github/outbound-intent.test.ts
@@ -329,3 +329,31 @@ it("bounds abandoned intent polling and reports an unacknowledged delivery", asy
expect(attempts).toBeLessThanOrEqual(11);
vi.useRealTimers();
});
+
+it.each([false, true])(
+ "acknowledges an abandoned writer only after durable deferral (persistFailure=%s)",
+ async (persistFailure) => {
+ vi.useFakeTimers();
+ m.stamps.title = outboundStamp(undefined, "A", undefined, {
+ intentId: "orphan",
+ pending: true,
+ });
+ const defer = vi.fn(async () => {
+ if (persistFailure) throw new Error("database unavailable");
+ });
+ const run = withEchoConfirmation(
+ async () => "A",
+ async () => inboundEcho(m.stamps.title, "A", "version"),
+ defer,
+ );
+ const result = persistFailure
+ ? expect(run).rejects.toThrow(
+ "Could not persist deferred webhook delivery",
+ )
+ : expect(run).resolves.toBeUndefined();
+ await vi.advanceTimersByTimeAsync(5000);
+ await result;
+ expect(defer).toHaveBeenCalledOnce();
+ vi.useRealTimers();
+ },
+);