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: preserve concurrent outbound history during inbound edits

Tina4a3d2c8f2d3
diff --cc apps/api/src/plugins/gitea/webhooks/issue-edited.ts
index 6105aaccc,7709dade2..ba13a0007
--- a/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-edited.ts
@@@ -8,6 -5,6 +8,7 @@@ import
import { taskTable } from "../../../database/schema";
import {
findExternalLink,
++ lockExternalLink,
updateExternalLink,
} from "../../github/services/link-manager";
import { formatTaskDescriptionFromIssue } from "../../github/utils/format";
@@@ -69,41 -65,6 +70,46 @@@ export async function handleGiteaIssueE
continue;
}
+ const echoMetadata = externalLink.metadata
+ ? JSON.parse(externalLink.metadata)
+ : {};
+ const fetchCurrentIssue = async () => {
+ const config = JSON.parse(integration.config) as GiteaConfig;
+ return createGiteaClient(config).getIssue(
+ config.repositoryOwner,
+ config.repositoryName,
+ issue.number,
+ );
+ };
+ let current: ReturnType<typeof fetchCurrentIssue> | undefined;
+ const currentIssue = () => (current ??= fetchCurrentIssue());
+ // Read the provider before taking task/project locks.
+ const titleEcho = changes.title
+ ? await confirmedOutboundEcho(
+ echoMetadata.lastSync?.title,
+ issue.title,
+ issue.updated_at,
+ async () => (await currentIssue()).title,
+ )
+ : false;
+ const descriptionEcho = changes.body
+ ? await confirmedOutboundEcho(
+ echoMetadata.lastSync?.description,
+ formatTaskDescriptionFromIssue(issue.body, externalLink.taskId),
+ issue.updated_at,
+ async () =>
+ formatTaskDescriptionFromIssue(
+ (await currentIssue()).body ?? null,
+ externalLink.taskId,
+ ),
+ )
+ : false;
+
++ // Prepare provider confirmation before the locked reread, including stamps
++ // that may be committed between the initial snapshot and this transaction.
++ if ((changes.title && !titleEcho) || (changes.body && !descriptionEcho))
++ await currentIssue();
++
await withIntegrationTask(externalLink.taskId, integration, async (db) => {
const task = await db.query.taskTable.findFirst({
where: linkedTaskScope(externalLink.taskId, integration.projectId),
@@@ -113,8 -74,8 +119,10 @@@
return;
}
-- const metadata = externalLink.metadata
-- ? JSON.parse(externalLink.metadata)
++ const lockedLink = await lockExternalLink(externalLink.id, db);
++ if (!lockedLink) return;
++ const metadata = lockedLink.metadata
++ ? JSON.parse(lockedLink.metadata)
: {};
const updateData: Record<string, unknown> = {};
@@@ -130,9 -91,17 +138,17 @@@
let shouldUpdateTitle = true;
if (lastTitleSync) {
- if (titleEcho) {
+ if (
- lastTitleSync.value === issue.title &&
- lastTitleSync.source === "kaneo"
++ titleEcho ||
++ (await confirmedOutboundEcho(
++ lastTitleSync,
++ issue.title,
++ issue.updated_at,
++ async () => (await currentIssue()).title,
++ ))
+ ) {
shouldUpdateTitle = false;
}
- const timeSinceLastSync =
- Date.now() - new Date(lastTitleSync.timestamp).getTime();
- if (timeSinceLastSync < 2000 && shouldUpdateTitle) {
- shouldUpdateTitle = false;
- }
}
if (shouldUpdateTitle) {
@@@ -156,9 -121,17 +172,21 @@@
let shouldUpdateDescription = true;
if (lastDescSync) {
- if (descriptionEcho) {
+ if (
- lastDescSync.value === formattedDescription &&
- lastDescSync.source === "kaneo"
++ descriptionEcho ||
++ (await confirmedOutboundEcho(
++ lastDescSync,
++ formattedDescription,
++ issue.updated_at,
++ async () =>
++ formatTaskDescriptionFromIssue(
++ (await currentIssue()).body ?? null,
++ task.id,
++ ),
++ ))
+ ) {
shouldUpdateDescription = false;
}
- const timeSinceLastSync =
- Date.now() - new Date(lastDescSync.timestamp).getTime();
- if (timeSinceLastSync < 2000 && shouldUpdateDescription) {
- shouldUpdateDescription = false;
- }
}
if (shouldUpdateDescription) {
diff --cc apps/api/src/plugins/github/services/link-manager.ts
index bab3bc857,c62f3b860..79524ad23
--- a/apps/api/src/plugins/github/services/link-manager.ts
+++ b/apps/api/src/plugins/github/services/link-manager.ts
@@@ -209,3 -169,3 +209,12 @@@ export async function getExternalLinksB
where: eq(externalLinkTable.integrationId, integrationId),
});
}
++
++export async function lockExternalLink(id: string, database: DbOrTx) {
++ const [link] = await database
++ .select({ id: externalLinkTable.id, metadata: externalLinkTable.metadata })
++ .from(externalLinkTable)
++ .where(eq(externalLinkTable.id, id))
++ .for("update");
++ return link;
++}
diff --cc apps/api/src/plugins/github/webhooks/issue-edited.ts
index 1fa8e4411,7a91fd900..7c190fb14
--- a/apps/api/src/plugins/github/webhooks/issue-edited.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-edited.ts
@@@ -6,7 -3,7 +6,11 @@@ import
withIntegrationTask,
} from "../services/integration-task-scope";
import { taskTable } from "../../../database/schema";
--import { findExternalLink, updateExternalLink } from "../services/link-manager";
++import {
++ findExternalLink,
++ updateExternalLink,
++ lockExternalLink,
++} from "../services/link-manager";
import { findAllIntegrationsByRepo } from "../services/task-service";
import { formatTaskDescriptionFromIssue } from "../utils/format";
import { parseLinkMetadata } from "../utils/parse-link-metadata";
@@@ -71,45 -71,6 +75,50 @@@ export async function handleIssueEdited
continue;
}
+ const echoMetadata = parseLinkMetadata<IssueEditedMetadata>(
+ externalLink.metadata,
+ { externalLinkId: externalLink.id, source: "issue_edited" },
+ );
+ const fetchCurrentIssue = async () => {
+ const config = JSON.parse(integration.config) as GitHubConfig;
+ const octokit = await getVerifiedInstallationOctokit(config);
+ return (
+ await octokit.rest.issues.get({
+ owner: config.repositoryOwner,
+ repo: config.repositoryName,
+ issue_number: issue.number,
+ })
+ ).data;
+ };
+ let current: ReturnType<typeof fetchCurrentIssue> | undefined;
+ const currentIssue = () => (current ??= fetchCurrentIssue());
+ // Read the provider before taking task/project locks.
+ const titleEcho = changes.title
+ ? await confirmedOutboundEcho(
+ echoMetadata.lastSync?.title,
+ issue.title,
+ issue.updated_at,
+ async () => (await currentIssue()).title,
+ )
+ : false;
+ const descriptionEcho = changes.body
+ ? await confirmedOutboundEcho(
+ echoMetadata.lastSync?.description,
+ formatTaskDescriptionFromIssue(issue.body, externalLink.taskId),
+ issue.updated_at,
+ async () =>
+ formatTaskDescriptionFromIssue(
+ (await currentIssue()).body ?? null,
+ externalLink.taskId,
+ ),
+ )
+ : false;
+
++ // Prepare provider confirmation before the locked reread, including stamps
++ // that may be committed between the initial snapshot and this transaction.
++ if ((changes.title && !titleEcho) || (changes.body && !descriptionEcho))
++ await currentIssue();
++
await withIntegrationTask(externalLink.taskId, integration, async (db) => {
const task = await db.query.taskTable.findFirst({
where: linkedTaskScope(externalLink.taskId, integration.projectId),
@@@ -120,8 -81,8 +129,10 @@@
return;
}
++ const lockedLink = await lockExternalLink(externalLink.id, db);
++ if (!lockedLink) return;
const metadata = parseLinkMetadata<IssueEditedMetadata>(
-- externalLink.metadata,
++ lockedLink.metadata,
{
externalLinkId: externalLink.id,
source: "issue_edited",
@@@ -141,7 -102,10 +152,15 @@@
let shouldUpdateTitle = true;
if (lastTitleSync) {
- if (titleEcho) {
+ if (
- lastTitleSync.value === issue.title &&
- lastTitleSync.source === "kaneo"
++ titleEcho ||
++ (await confirmedOutboundEcho(
++ lastTitleSync,
++ issue.title,
++ issue.updated_at,
++ async () => (await currentIssue()).title,
++ ))
+ ) {
console.log("Skipping title update - already synced from Kaneo");
shouldUpdateTitle = false;
}
@@@ -171,7 -142,10 +190,19 @@@
let shouldUpdateDescription = true;
if (lastDescSync) {
- if (descriptionEcho) {
+ if (
- lastDescSync.value === formattedDescription &&
- lastDescSync.source === "kaneo"
++ descriptionEcho ||
++ (await confirmedOutboundEcho(
++ lastDescSync,
++ formattedDescription,
++ issue.updated_at,
++ async () =>
++ formatTaskDescriptionFromIssue(
++ (await currentIssue()).body ?? null,
++ task.id,
++ ),
++ ))
+ ) {
console.log(
"Skipping description update - already synced from Kaneo",
);
diff --cc tests/api-integration/integration-task-ownership.test.ts
index 82ee8fb9e,191d92b4b..e26df83da
--- a/tests/api-integration/integration-task-ownership.test.ts
+++ b/tests/api-integration/integration-task-ownership.test.ts
@@@ -24,6 -24,6 +24,7 @@@ import
const m = vi.hoisted(() => ({
listIssues: vi.fn(),
++ getIssue: vi.fn(),
listIssueComments: vi.fn(async () => []),
listIssueNotes: vi.fn(async () => []),
listPulls: vi.fn(async () => []),
@@@ -381,39 -381,3 +382,74 @@@ it("preserves legacy links belonging t
});
expect(await db.query.externalLinkTable.findMany()).toEqual([compatible]);
});
+
+it("preserves outbound history and unrelated fields across concurrent sync completions", async () => {
+ const { updateExternalLink } =
+ await import("../../apps/api/src/plugins/github/services/link-manager");
+ const { isOutboundEcho } =
+ await import("../../apps/api/src/plugins/github/utils/sync-echo");
+ const { link } = await setup();
+ await db
+ .update(schema.externalLinkTable)
+ .set({ metadata: JSON.stringify({ remoteMarker: "keep" }) })
+ .where(eq(schema.externalLinkTable.id, link.id));
+ await Promise.all(
+ Array.from({ length: 12 }, (_, i) =>
+ updateExternalLink(link.id, {
+ outbound: {
+ field: i % 2 ? "description" : "title",
+ value: `edit-${i}`,
+ updatedAt: `time-${i}`,
+ },
+ }),
+ ),
+ );
+ const saved = await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.id, link.id),
+ });
+ const metadata = JSON.parse(saved!.metadata!);
+ expect(metadata.remoteMarker).toBe("keep");
+ for (let i = 0; i < 12; i++)
+ expect(
+ isOutboundEcho(
+ metadata.lastSync[i % 2 ? "description" : "title"],
+ `edit-${i}`,
+ `time-${i}`,
+ ),
+ ).toBe(true);
+});
++
++it("keeps outbound history committed during an inbound provider read", async () => {
++ const { updateExternalLink } =
++ await import("../../apps/api/src/plugins/github/services/link-manager");
++ const { isOutboundEcho } =
++ await import("../../apps/api/src/plugins/github/utils/sync-echo");
++ const { link } = await setup();
++ m.getIssue.mockImplementationOnce(async () => {
++ await updateExternalLink(link.id, {
++ outbound: {
++ field: "title",
++ value: "Local edit",
++ updatedAt: "local-time",
++ },
++ });
++ return remoteIssue;
++ });
++ await handleGiteaIssueEdited({
++ action: "edited",
++ repository,
++ issue: remoteIssue,
++ changes: { title: { from: "Old" } },
++ });
++ expect(m.getIssue).toHaveBeenCalledOnce();
++ const saved = await db.query.externalLinkTable.findFirst({
++ where: eq(schema.externalLinkTable.id, link.id),
++ });
++ expect(
++ isOutboundEcho(
++ JSON.parse(saved!.metadata!).lastSync.title,
++ "Local edit",
++ "local-time",
++ ),
++ ).toBe(true);
++});
diff --cc tests/api/plugins/gitea/rapid-text-sync.test.ts
index 22496c9dc,000000000..015862599
mode 100644,000000..100644
--- a/tests/api/plugins/gitea/rapid-text-sync.test.ts
+++ b/tests/api/plugins/gitea/rapid-text-sync.test.ts
@@@ -1,306 -1,0 +1,340 @@@
+import { outboundStamp } from "../../../../apps/api/src/plugins/github/utils/sync-echo";
+import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
+import { handleGiteaIssueEdited } from "../../../../apps/api/src/plugins/gitea/webhooks/issue-edited";
+const m = vi.hoisted(() => ({
+ metadata: "",
++ lockedMetadata: undefined as string | undefined,
+ getIssue: vi.fn(),
+ status: vi.fn(),
+ writes: vi.fn(),
+ linkWrites: vi.fn(),
+}));
+vi.mock("../../../../apps/api/src/database", () => ({
+ default: {
+ query: {
+ externalLinkTable: {
+ findFirst: async () => ({
+ id: "link",
+ taskId: "task",
+ metadata: m.metadata,
+ }),
+ },
+ taskTable: {
+ findFirst: async () => ({ id: "task", projectId: "project" }),
+ },
+ },
+ update: () => ({
+ set: m.writes.mockReturnValue({ where: async () => undefined }),
+ }),
+ },
+}));
+vi.mock(
+ "../../../../apps/api/src/plugins/github/services/integration-task-scope",
+ async (original) => ({
+ ...(await original<
+ typeof import("../../../../apps/api/src/plugins/github/services/integration-task-scope")
+ >()),
+ withIntegrationTask: async (
+ _id: string,
+ _scope: unknown,
+ apply: (database: unknown) => Promise<unknown>,
+ ) => apply((await import("../../../../apps/api/src/database")).default),
+ }),
+);
+vi.mock(
+ "../../../../apps/api/src/plugins/github/services/link-manager",
+ () => ({
+ findExternalLink: async () => ({
+ id: "link",
+ taskId: "task",
+ metadata: m.metadata,
+ }),
+ updateExternalLink: m.linkWrites,
++ lockExternalLink: async () => ({
++ id: "link",
++ metadata: m.lockedMetadata ?? m.metadata,
++ }),
+ }),
+);
+vi.mock(
+ "../../../../apps/api/src/plugins/gitea/services/integration-lookup",
+ () => ({
+ findAllIntegrationsByGiteaRepo: async () => [
+ {
+ id: "integration",
+ projectId: "project",
+ config: JSON.stringify({
+ baseUrl: "https://gitea.example",
+ accessToken: "test",
+ repositoryOwner: "owner",
+ repositoryName: "repo",
+ }),
+ },
+ ],
+ repoOwnerLogin: () => "owner",
+ }),
+);
+vi.mock("../../../../apps/api/src/plugins/gitea/utils/gitea-api", () => ({
+ createGiteaClient: () => ({ getIssue: m.getIssue }),
+}));
+const payload = {
+ action: "edited",
+ repository: {
+ owner: { login: "owner" },
+ name: "repo",
+ html_url: "https://gitea.example/owner/repo",
+ },
+ issue: {
+ number: 1,
+ title: "Remote correction",
+ body: "Remote description",
+ html_url: "https://gitea.example/owner/repo/issues/1",
+ },
+ changes: { title: { from: "Old" }, body: { from: "Old body" } },
+};
+beforeEach(() => {
+ vi.clearAllMocks();
++ m.lockedMetadata = undefined;
+ m.getIssue.mockResolvedValue({ title: "Newest title", body: "Newest body" });
+ m.metadata = JSON.stringify({
+ lastSync: {
+ title: {
+ source: "kaneo",
+ value: "Local title",
+ timestamp: new Date().toISOString(),
+ },
+ description: {
+ source: "kaneo",
+ value: "Local description",
+ timestamp: new Date().toISOString(),
+ },
+ },
+ });
+});
+describe("rapid gitea text edits", () => {
+ it("applies a different title and description immediately after an outbound sync", async () => {
+ await handleGiteaIssueEdited(payload);
+ expect(m.writes).toHaveBeenCalledWith({
+ title: "Remote correction",
+ description: "Remote description",
+ });
+ });
+ it("still ignores exact title and description echoes", async () => {
+ await handleGiteaIssueEdited({
+ ...payload,
+ issue: {
+ ...payload.issue,
+ title: "Local title",
+ body: "Local description",
+ },
+ });
+ expect(m.writes).not.toHaveBeenCalled();
+ });
+});
+
+it("ignores a delayed first echo after a newer outbound edit", async () => {
+ m.metadata = JSON.stringify({
+ lastSync: {
+ title: outboundStamp(
+ outboundStamp(undefined, "Earlier title", "2026-01-01T00:00:00Z"),
+ "Newest title",
+ "2026-01-01T00:00:01Z",
+ ),
+ description: outboundStamp(
+ outboundStamp(undefined, "Earlier body", "2026-01-01T00:00:00Z"),
+ "Newest body",
+ "2026-01-01T00:00:01Z",
+ ),
+ },
+ });
+ await handleGiteaIssueEdited({
+ ...payload,
+ issue: {
+ ...payload.issue,
+ title: "Earlier title",
+ body: "Earlier body",
+ updated_at: "2026-01-01T00:00:00Z",
+ },
+ });
+ expect(m.writes).not.toHaveBeenCalled();
+});
+it("allows a new remote edit back to an older outbound value", async () => {
+ m.metadata = JSON.stringify({
+ lastSync: {
+ title: outboundStamp(
+ outboundStamp(undefined, "Earlier title", "2026-01-01T00:00:00Z"),
+ "Newest title",
+ "2026-01-01T00:00:01Z",
+ ),
+ },
+ });
+ await handleGiteaIssueEdited({
+ ...payload,
+ changes: { title: { from: "Newest title" } },
+ issue: {
+ ...payload.issue,
+ title: "Earlier title",
+ updated_at: "2026-01-01T00:00:02Z",
+ },
+ });
+ expect(m.writes).toHaveBeenCalledWith({ title: "Earlier title" });
+});
+
+vi.mock(
+ "../../../../apps/api/src/plugins/github/services/task-service",
+ () => ({ updateTaskStatus: m.status }),
+);
+vi.mock("../../../../apps/api/src/plugins/gitea/utils/resolve-column", () => ({
+ resolveTargetStatus: async (
+ _project: string,
+ _event: string,
+ fallback: string,
+ ) => fallback,
+}));
+it("ignores an older close echo after a newer outbound reopen", async () => {
+ const { handleGiteaIssueClosed } =
+ await import("../../../../apps/api/src/plugins/gitea/webhooks/issue-closed");
+ m.metadata = JSON.stringify({
+ state: "open",
+ lastSync: {
+ state: outboundStamp(
+ outboundStamp(undefined, "closed", "2026-01-01T00:00:00Z"),
+ "open",
+ "2026-01-01T00:00:01Z",
+ ),
+ },
+ });
+ await handleGiteaIssueClosed({
+ ...payload,
+ action: "closed",
+ issue: {
+ ...payload.issue,
+ state: "closed",
+ updated_at: "2026-01-01T00:00:00Z",
+ },
+ });
+ expect(m.status).not.toHaveBeenCalled();
+});
+it("ignores an older reopen echo after a newer outbound close", async () => {
+ const { handleGiteaIssueReopened } =
+ await import("../../../../apps/api/src/plugins/gitea/webhooks/issue-reopened");
+ m.metadata = JSON.stringify({
+ state: "closed",
+ lastSync: {
+ state: outboundStamp(
+ outboundStamp(undefined, "open", "2026-01-01T00:00:00Z"),
+ "closed",
+ "2026-01-01T00:00:01Z",
+ ),
+ },
+ });
+ await handleGiteaIssueReopened({
+ ...payload,
+ action: "reopened",
+ issue: {
+ ...payload.issue,
+ state: "open",
+ updated_at: "2026-01-01T00:00:00Z",
+ },
+ });
+ expect(m.status).not.toHaveBeenCalled();
+});
+
+it("accepts a legitimate remote edit matching a historical outbound value within the same timestamp", async () => {
+ const stamp = "2026-01-01T00:00:00Z";
+ m.metadata = JSON.stringify({
+ lastSync: {
+ title: outboundStamp(
+ outboundStamp(undefined, "Earlier title", stamp),
+ "Newest title",
+ stamp,
+ ),
+ description: outboundStamp(
+ outboundStamp(undefined, "Earlier body", stamp),
+ "Newest body",
+ stamp,
+ ),
+ },
+ });
+ m.getIssue.mockResolvedValue({
+ title: "Earlier title",
+ body: "Earlier body",
+ });
+ await handleGiteaIssueEdited({
+ ...payload,
+ issue: {
+ ...payload.issue,
+ title: "Earlier title",
+ body: "Earlier body",
+ updated_at: stamp,
+ },
+ });
+ expect(m.writes).toHaveBeenCalledWith({
+ title: "Earlier title",
+ description: "Earlier body",
+ });
+ expect(m.getIssue).toHaveBeenCalledTimes(1);
+});
+
+it("ignores the linked task footer when detecting a description echo", async () => {
+ await handleGiteaIssueEdited({
+ ...payload,
+ changes: { body: { from: "Old" } },
+ issue: {
+ ...payload.issue,
+ body: "Local description\n\n---\n<sub>Task: task</sub>",
+ },
+ });
+ expect(m.writes).not.toHaveBeenCalled();
+});
+it("accepts a same-timestamp remote close matching historical outbound state", async () => {
+ const { handleGiteaIssueClosed } =
+ await import("../../../../apps/api/src/plugins/gitea/webhooks/issue-closed");
+ const stamp = "2026-01-01T00:00:00Z";
+ m.metadata = JSON.stringify({
+ state: "open",
+ lastSync: {
+ state: outboundStamp(
+ outboundStamp(undefined, "closed", stamp),
+ "open",
+ stamp,
+ ),
+ },
+ });
+ m.getIssue.mockResolvedValue({ state: "closed" });
+ m.status.mockResolvedValue({ applied: false });
+ await handleGiteaIssueClosed({
+ ...payload,
+ action: "closed",
+ issue: { ...payload.issue, state: "closed", updated_at: stamp },
+ });
+ expect(m.status).toHaveBeenCalled();
+});
++
++it("retains outbound stamps added after the initial inbound snapshot", async () => {
++ m.metadata = JSON.stringify({ lastSync: {} });
++ const stamp = outboundStamp(undefined, "New local title", "new-time");
++ m.lockedMetadata = JSON.stringify({
++ marker: "preserve",
++ lastSync: { title: stamp },
++ });
++ await handleGiteaIssueEdited(payload);
++ expect(
++ m.linkWrites.mock.calls[0][1].metadata.lastSync.title.outbound,
++ ).toEqual(stamp.outbound);
++ expect(m.linkWrites.mock.calls[0][1].metadata.marker).toBe("preserve");
++});
++
++it("recognizes a delayed echo whose stamp arrives before the locked reread", async () => {
++ m.metadata = JSON.stringify({ lastSync: {} });
++ m.lockedMetadata = JSON.stringify({
++ lastSync: {
++ title: outboundStamp(undefined, payload.issue.title, undefined),
++ },
++ });
++ await handleGiteaIssueEdited({
++ ...payload,
++ changes: { title: payload.changes.title },
++ });
++ expect(m.writes).not.toHaveBeenCalled();
++});