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): retire all captured uncertain writes after repair

Tin75c47ada3fb2
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 881ad14dc..c482172cf 100644
--- a/apps/api/src/plugins/github/services/deferred-issue-edits.ts
+++ b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
@@ -20,7 +20,7 @@ import { parseLinkMetadata } from "../utils/parse-link-metadata";
import {
inboundStamp,
inboundOccurredAfterIntent,
- uncertainOutboundIntent,
+ uncertainOutboundIntents,
type SyncStamp,
} from "../utils/sync-echo";
import { writeInboundTaskField } from "./apply-observed-task-value";
@@ -199,8 +199,7 @@ export async function replayDeferredIssueEdits() {
const repairs: Array<{
field: IssueField;
value: string;
- intentId?: string;
- providerValue: string;
+ intentIds: string[];
}> = [];
const applied = await withIntegrationLink(
link,
@@ -234,7 +233,10 @@ export async function replayDeferredIssueEdits() {
if (!task) return;
for (const field of fields) {
const stamp = current.lastSync?.[field];
- const uncertain = uncertainOutboundIntent(stamp, values[field]);
+ const uncertain = uncertainOutboundIntents(stamp, values[field]);
+ const intentIds = uncertain.flatMap((entry) =>
+ entry.intentId ? [entry.intentId] : [],
+ );
const local =
field === "state"
? task.status === "done"
@@ -247,41 +249,27 @@ export async function replayDeferredIssueEdits() {
repairs.push({
field,
value: local,
- intentId: uncertain?.intentId,
- providerValue: values[field],
+ intentIds,
});
continue;
}
// A crashed older writer can leave the provider behind our completed
// local value. Its missing receipt does not make it a remote edit.
if (
- uncertain &&
- !(
- stamp?.source !== "kaneo" &&
- stamp?.inboundValue === local &&
- inboundOccurredAfterIntent(stamp, uncertain)
- ) &&
- local !== values[field]
+ local !== values[field] &&
+ uncertain.some(
+ (entry) =>
+ !(
+ stamp?.source !== "kaneo" &&
+ stamp?.inboundValue === local &&
+ inboundOccurredAfterIntent(stamp, entry)
+ ),
+ )
) {
- if (uncertain.intentId)
- await updateExternalLink(
- link.id,
- {
- outbound: {
- field,
- value: values[field],
- intentId: uncertain.intentId,
- pending: false,
- uncertain: true,
- },
- },
- tx,
- );
repairs.push({
field,
value: local,
- intentId: uncertain.intentId,
- providerValue: values[field],
+ intentIds,
});
}
}
@@ -379,21 +367,16 @@ export async function replayDeferredIssueEdits() {
link,
integration,
async (tx) => {
- // The repair succeeded. Retire its uncertain intent so a future
- // genuine provider edit to the old value remains importable.
+ // Retire only the matching intents captured for this repair.
+ // New writes arriving during provider HTTP must remain reconcilable.
for (const repair of repairs) {
- if (repair.intentId)
+ if (repair.intentIds.length)
await updateExternalLink(
link.id,
{
- outbound: {
+ retireOutboundIntents: {
field: repair.field,
- value: repair.providerValue,
- intentId: repair.intentId,
- updatedAt: issue.updated_at,
- pending: false,
- uncertain: false,
- cancelled: true,
+ intentIds: repair.intentIds,
},
},
tx,
diff --git a/apps/api/src/plugins/github/services/link-manager.ts b/apps/api/src/plugins/github/services/link-manager.ts
index 41169bcdf..249899064 100644
--- a/apps/api/src/plugins/github/services/link-manager.ts
+++ b/apps/api/src/plugins/github/services/link-manager.ts
@@ -35,6 +35,7 @@ export type UpdateExternalLinkParams = {
scope: string;
};
completeDeferredEdit?: string;
+ retireOutboundIntents?: { field: IssueField; intentIds: string[] };
outbound?: {
field: "title" | "description" | "state";
value: string;
@@ -149,7 +150,8 @@ export async function updateExternalLink(
params.metadata ||
params.observedOutbound ||
params.deferredEdit ||
- params.completeDeferredEdit
+ params.completeDeferredEdit ||
+ params.retireOutboundIntents
) {
return database.transaction(async (tx) => {
const link = await lockExternalLink(id, tx);
@@ -204,6 +206,17 @@ export async function updateExternalLink(
: entry,
);
}
+ if (params.retireOutboundIntents) {
+ const { field, intentIds } = params.retireOutboundIntents;
+ const ids = new Set(intentIds);
+ const stamp = merged.lastSync?.[field];
+ if (stamp)
+ stamp.outbound = stamp.outbound?.map((entry) =>
+ entry.intentId && ids.has(entry.intentId)
+ ? { ...entry, pending: false, uncertain: false, cancelled: true }
+ : entry,
+ );
+ }
if (params.outbound) {
const { field, value, updatedAt, ...intent } = params.outbound;
merged.lastSync = {
diff --git a/apps/api/src/plugins/github/utils/inbound-echo.ts b/apps/api/src/plugins/github/utils/inbound-echo.ts
index e18bd187d..3ecfa9abf 100644
--- a/apps/api/src/plugins/github/utils/inbound-echo.ts
+++ b/apps/api/src/plugins/github/utils/inbound-echo.ts
@@ -4,7 +4,7 @@ import {
ambiguousOutboundEcho,
isOutboundEcho,
pendingOutboundIntent,
- uncertainOutboundIntent,
+ uncertainOutboundIntents,
inboundOccurredAfterIntent,
type SyncStamp,
} from "./sync-echo";
@@ -98,15 +98,17 @@ export function inboundEcho(
) {
if (confirmedValue() !== value) return true;
}
- const uncertain = uncertainOutboundIntent(stamp, value);
+ const uncertain = uncertainOutboundIntents(stamp, value);
const localValue = context?.localValue ?? stamp?.value;
if (
- uncertain &&
localValue !== value &&
- !(
- stamp?.source !== "kaneo" &&
- stamp?.inboundValue === localValue &&
- inboundOccurredAfterIntent(stamp, uncertain)
+ uncertain.some(
+ (entry) =>
+ !(
+ stamp?.source !== "kaneo" &&
+ stamp?.inboundValue === localValue &&
+ inboundOccurredAfterIntent(stamp, entry)
+ ),
)
) {
if (confirmedValue() !== value) return true;
diff --git a/apps/api/src/plugins/github/utils/sync-echo.ts b/apps/api/src/plugins/github/utils/sync-echo.ts
index 84c2be442..d0e9fb033 100644
--- a/apps/api/src/plugins/github/utils/sync-echo.ts
+++ b/apps/api/src/plugins/github/utils/sync-echo.ts
@@ -195,14 +195,15 @@ export function hasNewerObservedEdit(
);
}
-export function uncertainOutboundIntent(
+export function uncertainOutboundIntents(
stamp: SyncStamp | undefined,
value: string,
) {
- return stamp?.outbound?.find(
+ const valueHash = hash(value);
+ return (stamp?.outbound ?? []).filter(
(entry) =>
!entry.cancelled &&
- entry.hash === hash(value) &&
+ entry.hash === valueHash &&
(entry.uncertain ||
(entry.pending && Date.now() - Date.parse(entry.timestamp) >= 300_000)),
);
diff --git a/tests/api-integration/deferred-issue-edits.test.ts b/tests/api-integration/deferred-issue-edits.test.ts
index 9ce634ec9..22cbe3ac6 100644
--- a/tests/api-integration/deferred-issue-edits.test.ts
+++ b/tests/api-integration/deferred-issue-edits.test.ts
@@ -724,9 +724,13 @@ it.each(
);
},
);
-it.each(providerFields)(
- "repairs a late timed-out $provider $field write without reverting the newer local value",
- async ({ provider, field }) => {
+it.each(
+ providerFields.flatMap((entry) =>
+ [1, 2].map((count) => ({ ...entry, count })),
+ ),
+)(
+ "repairs a late timed-out $provider $field write without reverting the newer local value (uncertain=$count)",
+ async ({ provider, field, count }) => {
const { task, integration, link } = await seed(provider);
const older = field === "state" ? "closed" : "remote-old";
const latest =
@@ -735,11 +739,17 @@ it.each(providerFields)(
intentId: "timed-out",
pending: true,
});
- const uncertain = outboundStamp(pending, older, undefined, {
+ let uncertain = outboundStamp(pending, older, undefined, {
intentId: "timed-out",
pending: false,
uncertain: true,
});
+ if (count === 2)
+ uncertain = outboundStamp(uncertain, older, undefined, {
+ intentId: "second-timed-out",
+ pending: false,
+ uncertain: true,
+ });
const stamp = outboundStamp(uncertain, latest, "2026-09-30T00:00:02Z", {
intentId: "newer-completed",
});
@@ -1170,20 +1180,31 @@ it("preserves a newer correction queued while its worker reads the provider", as
expect((await metadata(link.id)).deferredIssueEdit).toBeUndefined();
});
-it.each(["github", "gitea"])(
- "retires an uncertain intent after queued %s correction so future genuine edits apply",
- async (provider) => {
+it.each(
+ ["github", "gitea"].flatMap((provider) =>
+ [1, 2].map((count) => ({ provider, count })),
+ ),
+)(
+ "retires uncertain intents after queued $provider correction so future genuine edits apply (uncertain=$count)",
+ async ({ provider, count }) => {
const { task, integration, link } = await seed(provider);
+ let stamp = outboundStamp(undefined, "A", undefined, {
+ intentId: "old-unknown",
+ pending: false,
+ uncertain: true,
+ });
+ if (count === 2)
+ stamp = outboundStamp(stamp, "A", undefined, {
+ intentId: "second-unknown",
+ pending: false,
+ uncertain: true,
+ });
await db
.update(schema.externalLinkTable)
.set({
metadata: JSON.stringify({
lastSync: {
- title: outboundStamp(undefined, "A", undefined, {
- intentId: "old-unknown",
- pending: false,
- uncertain: true,
- }),
+ title: stamp,
},
}),
})
@@ -1210,3 +1231,126 @@ it.each(["github", "gitea"])(
expect((await current(task.id))?.title).toBe("A");
},
);
+
+it.each(
+ ["github", "gitea"].flatMap((provider) =>
+ ["webhook", "worker"].map((mode) => ({ provider, mode })),
+ ),
+)(
+ "protects a local edit when only the older of two uncertain $provider writes precedes it ($mode)",
+ async ({ provider, mode }) => {
+ const { task, integration, link } = await seed(provider);
+ let stamp = outboundStamp(undefined, "A", undefined, {
+ intentId: "before-local",
+ pending: true,
+ });
+ stamp = inboundStamp(stamp, "B", provider);
+ stamp = outboundStamp(stamp, "A", undefined, {
+ intentId: "after-local",
+ pending: true,
+ });
+ stamp = outboundStamp(stamp, "A", undefined, {
+ intentId: "before-local",
+ pending: false,
+ uncertain: true,
+ });
+ stamp = outboundStamp(stamp, "A", undefined, {
+ intentId: "after-local",
+ pending: false,
+ uncertain: true,
+ });
+ await db
+ .update(schema.externalLinkTable)
+ .set({ metadata: JSON.stringify({ lastSync: { title: stamp } }) })
+ .where(eq(schema.externalLinkTable.id, link.id));
+ if (mode === "webhook")
+ await deliverField(
+ provider,
+ "title",
+ "A",
+ "2026-09-30T00:00:03Z",
+ integration.id,
+ );
+ else await deferIssueEdit(link, integration, ["title"]);
+ await replayDeferredIssueEdits();
+ expect((await current(task.id))?.title).toBe("B");
+ expect(m.write).toHaveBeenCalledWith(
+ expect.objectContaining({ title: "B" }),
+ );
+ },
+);
+
+it.each(["uncertain", "pending"])(
+ "keeps a newer matching %s write that arrives while a queued repair is in flight",
+ async (kind) => {
+ const { integration, link } = await seed();
+ await db
+ .update(schema.externalLinkTable)
+ .set({
+ metadata: JSON.stringify({
+ lastSync: {
+ title: outboundStamp(undefined, "A", undefined, {
+ intentId: "old",
+ uncertain: true,
+ }),
+ },
+ }),
+ })
+ .where(eq(schema.externalLinkTable.id, link.id));
+ await deferTaskSync(link, integration, ["title"]);
+ m.write.mockImplementationOnce(async () => {
+ await updateExternalLink(link.id, {
+ outbound: {
+ field: "title",
+ value: "A",
+ intentId: "new",
+ pending: kind === "pending",
+ uncertain: kind === "uncertain",
+ },
+ });
+ return { updated_at: "2026-09-30T00:00:04Z" };
+ });
+ await replayDeferredIssueEdits();
+ const entries = (await metadata(link.id)).lastSync.title.outbound;
+ expect(
+ entries.find((entry: { intentId: string }) => entry.intentId === "old"),
+ ).toMatchObject({ cancelled: true, uncertain: false, pending: false });
+ expect(
+ entries.find((entry: { intentId: string }) => entry.intentId === "new"),
+ ).toMatchObject({
+ pending: kind === "pending",
+ uncertain: kind === "uncertain",
+ });
+ expect(
+ entries.find((entry: { intentId: string }) => entry.intentId === "new")
+ .cancelled,
+ ).not.toBe(true);
+ },
+);
+it("retains every captured uncertain intent when the corrective provider write fails", async () => {
+ const { integration, link } = await seed();
+ let stamp = outboundStamp(undefined, "A", undefined, {
+ intentId: "first",
+ uncertain: true,
+ });
+ stamp = outboundStamp(stamp, "A", undefined, {
+ intentId: "second",
+ uncertain: true,
+ });
+ await db
+ .update(schema.externalLinkTable)
+ .set({ metadata: JSON.stringify({ lastSync: { title: stamp } }) })
+ .where(eq(schema.externalLinkTable.id, link.id));
+ await deferTaskSync(link, integration, ["title"]);
+ m.write.mockRejectedValueOnce(new Error("provider unavailable"));
+ expect(await replayDeferredIssueEdits()).toEqual({ degraded: true });
+ const saved = await metadata(link.id);
+ expect(saved.deferredIssueEdit).toBeDefined();
+ for (const id of ["first", "second"]) {
+ const intent = saved.lastSync.title.outbound.find(
+ (entry: { intentId: string }) => entry.intentId === id,
+ );
+ expect(intent.uncertain).toBe(true);
+ expect(intent.cancelled).not.toBe(true);
+ }
+});