⚡ 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(integrations): distinguish first-write crash recovery
Tind468019fd950
diff --git a/apps/api/src/plugins/github/services/deferred-issue-edits.ts b/apps/api/src/plugins/github/services/deferred-issue-edits.tsindex fcf9e2ea4..b59650390 100644--- a/apps/api/src/plugins/github/services/deferred-issue-edits.ts+++ b/apps/api/src/plugins/github/services/deferred-issue-edits.ts@@ -19,6 +19,7 @@ import { inboundEcho, PendingEcho } from "../utils/inbound-echo";import { parseLinkMetadata } from "../utils/parse-link-metadata";import {inboundStamp,+ inboundOccurredAfterIntent,uncertainOutboundIntent,type SyncStamp,} from "../utils/sync-echo";@@ -72,13 +73,10 @@ export async function deferIssueEdit(async function taskRevision(taskId: string,projectId: string,- fields: IssueField[],database: IntegrationDatabase = db,) {const [task] = await database- .select({- revision: sql<string>`md5(jsonb_build_array(${taskTable.updatedAt}, ${fields.includes("title") ? taskTable.title : sql`null`}, ${fields.includes("description") ? taskTable.description : sql`null`}, ${fields.includes("state") ? taskTable.status : sql`null`})::text)`,- })+ .select({ revision: sql<string>`${taskTable}.xmin::text` }).from(taskTable).where(linkedTaskScope(taskId, projectId));return task?.revision;@@ -182,11 +180,7 @@ export async function replayDeferredIssueEdits() {))continue;- const revision = await taskRevision(- link.taskId,- integration.projectId,- job.fields,- );+ const revision = await taskRevision(link.taskId, integration.projectId);if (!revision) continue;const provider = await issueAccess(integration, link);const issue = await provider.read();@@ -222,12 +216,8 @@ export async function replayDeferredIssueEdits() {)return;if (- (await taskRevision(- link.taskId,- integration.projectId,- job.fields,- tx,- )) !== revision+ (await taskRevision(link.taskId, integration.projectId, tx)) !==+ revision)return;const task = await tx.query.taskTable.findFirst({@@ -250,7 +240,11 @@ export async function replayDeferredIssueEdits() {// local value. Its missing receipt does not make it a remote edit.if (uncertain &&- stamp?.source === "kaneo" &&+ !(+ stamp?.source !== "kaneo" &&+ stamp?.inboundValue === local &&+ inboundOccurredAfterIntent(stamp, uncertain)+ ) &&local !== values[field]) {if (uncertain.intentId)diff --git a/apps/api/src/plugins/github/utils/sync-echo.ts b/apps/api/src/plugins/github/utils/sync-echo.tsindex 444475d7d..e52ed1e8d 100644--- a/apps/api/src/plugins/github/utils/sync-echo.ts+++ b/apps/api/src/plugins/github/utils/sync-echo.ts@@ -1,4 +1,4 @@-import { createHash } from "node:crypto";+import { createHash, randomUUID } from "node:crypto";export type OutboundIntent = {intentId?: string;@@ -7,6 +7,7 @@ export type OutboundIntent = {uncertain?: boolean;observedUpdatedAt?: string;startedAt?: string;+ priorInboundId?: string;ambiguous?: boolean;};export type OutboundEntry = OutboundIntent & {@@ -19,6 +20,7 @@ export type SyncStamp = {source?: string;value?: string;inboundAt?: string;+ inboundId?: string;inboundValue?: string;outbound?: OutboundEntry[];};@@ -57,15 +59,16 @@ export function outboundStamp(observedUpdatedAt: previousIntent?.observedUpdatedAt,startedAt:previousIntent?.startedAt ?? (intent.pending ? timestamp : undefined),+ priorInboundId:+ previousIntent?.priorInboundId ??+ (intent.pending ? previous?.inboundId : undefined),ambiguous:previousIntent?.ambiguous ||!!(updatedAt &&previousIntent?.observedUpdatedAt === updatedAt &&- previous?.inboundAt &&- previousIntent.startedAt &&- previous.inboundAt >= previousIntent.startedAt &&- previous.inboundValue !== value+ inboundOccurredAfterIntent(previous, previousIntent) &&+ previous?.inboundValue !== value),...intent,});@@ -160,6 +163,7 @@ export function inboundStamp(source,value,inboundAt: timestamp,+ inboundId: randomUUID(),inboundValue: value,};}@@ -200,3 +204,13 @@ export function uncertainOutboundIntent((entry.pending && Date.now() - Date.parse(entry.timestamp) >= 300_000)),);}++export function inboundOccurredAfterIntent(+ stamp: SyncStamp | undefined,+ intent: OutboundEntry | undefined,+) {+ if (!stamp?.inboundAt || !intent?.startedAt) return false;+ // IDs preserve ordering even when two persisted stamps share a millisecond.+ if (stamp.inboundId) return stamp.inboundId !== intent.priorInboundId;+ return Date.parse(stamp.inboundAt) > Date.parse(intent.startedAt);+}diff --git a/tests/api-integration/deferred-issue-edits.test.ts b/tests/api-integration/deferred-issue-edits.test.tsindex 8cf56d37f..744692ecd 100644--- a/tests/api-integration/deferred-issue-edits.test.ts+++ b/tests/api-integration/deferred-issue-edits.test.ts@@ -472,3 +472,63 @@ it.each(["github", "gitea"])(expect((await current(task.id))?.title).toBe("B");},);++it.each([false, true])(+ "repairs a crashed first write after a later local edit (previousInbound=%s)",+ async (previousInbound) => {+ const { task, integration, link } = await seed();+ const prior = previousInbound+ ? inboundStamp(undefined, "B", "github")+ : undefined;+ const stamp = outboundStamp(prior, "A", undefined, {+ intentId: "first-orphan",+ pending: true,+ });+ const orphan = stamp.outbound?.find(+ (entry) => entry.intentId === "first-orphan",+ );+ if (!orphan) throw new Error("Missing orphan fixture");+ orphan.timestamp = new Date(Date.now() - 300_001).toISOString();+ await db+ .update(schema.externalLinkTable)+ .set({ metadata: JSON.stringify({ lastSync: { title: stamp } }) })+ .where(eq(schema.externalLinkTable.id, link.id));+ await db+ .update(schema.taskTable)+ .set({ title: "B" })+ .where(eq(schema.taskTable.id, task.id));+ await deferIssueEdit(link, integration, ["title"]);+ await replayDeferredIssueEdits();+ expect((await current(task.id))?.title).toBe("B");+ expect(m.write).toHaveBeenCalledWith(+ expect.objectContaining({ title: "B" }),+ );+ expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();+ },+);++it("protects a local ABA edit even when its final fields and timestamp are unchanged", async () => {+ const { task, integration, link } = await seed();+ const updatedAt = new Date("2026-09-30T00:00:00Z");+ await db+ .update(schema.taskTable)+ .set({ updatedAt })+ .where(eq(schema.taskTable.id, task.id));+ await deferIssueEdit(link, integration, ["description"]);+ m.read.mockImplementationOnce(async () => {+ await db.transaction(async (tx) => {+ await tx+ .update(schema.taskTable)+ .set({ description: "temporary edit", updatedAt })+ .where(eq(schema.taskTable.id, task.id));+ await tx+ .update(schema.taskTable)+ .set({ description: "old body", updatedAt })+ .where(eq(schema.taskTable.id, task.id));+ });+ return { title: "A", body: "stale provider body", state: "closed" };+ });+ await replayDeferredIssueEdits();+ expect((await current(task.id))?.description).toBe("old body");+ expect((await metadata(link.id)).deferredIssueEdit).toBeDefined();+});diff --git a/tests/api/plugins/github/outbound-intent.test.ts b/tests/api/plugins/github/outbound-intent.test.tsindex 36a0945f0..f5589f8f9 100644--- a/tests/api/plugins/github/outbound-intent.test.ts+++ b/tests/api/plugins/github/outbound-intent.test.ts@@ -7,6 +7,7 @@ import {import { mergeSyncMetadata } from "../../../../apps/api/src/plugins/github/utils/merge-sync-metadata";import {inboundStamp,+ inboundOccurredAfterIntent,isPendingOutboundEcho,outboundStamp,type OutboundIntent,@@ -392,3 +393,21 @@ it("durably defers a colliding event when provider confirmation fails", async ());expect(defer).toHaveBeenCalledOnce();});++it("distinguishes earlier and intervening inbound edits within one millisecond", () => {+ vi.useFakeTimers();+ const prior = inboundStamp(undefined, "B", "github");+ const pending = outboundStamp(prior, "A", undefined, {+ intentId: "pending",+ pending: true,+ });+ const intent = pending.outbound?.find(+ (entry) => entry.intentId === "pending",+ );+ expect(prior.inboundAt).toBe(intent?.startedAt);+ expect(inboundOccurredAfterIntent(prior, intent)).toBe(false);+ const later = inboundStamp(pending, "B", "github");+ expect(later.inboundAt).toBe(intent?.startedAt);+ expect(inboundOccurredAfterIntent(later, intent)).toBe(true);+ vi.useRealTimers();+});