⚡ 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): guard initialization and bound resume waits
Tin3fcdd0ca3032
diff --git a/apps/api/src/github-integration/controllers/import-issues.ts b/apps/api/src/github-integration/controllers/import-issues.tsindex 603f26cda..88b242bd4 100644--- a/apps/api/src/github-integration/controllers/import-issues.ts+++ b/apps/api/src/github-integration/controllers/import-issues.ts@@ -656,7 +656,7 @@ async function linkPull(.from(taskTable).where(and(eq(taskTable.id, task.id), eq(taskTable.projectId, project.id))).for("share");- if (!scopedTask || !(await canSyncTask(task.id, integrationId, tx))) return;+ if (!scopedTask) return;await tx.insert(externalLinkTable).values({taskId: task.id,integrationId,diff --git a/apps/api/src/integration-sync/controllers/resume-sync.ts b/apps/api/src/integration-sync/controllers/resume-sync.tsindex f09ad305e..5cb7363f0 100644--- a/apps/api/src/integration-sync/controllers/resume-sync.ts+++ b/apps/api/src/integration-sync/controllers/resume-sync.ts@@ -14,6 +14,7 @@ import { formatIssueBody } from "../../plugins/github/utils/format";import { reviewSyncResume } from "./review-resume";import { lockResumeScope } from "./lock-resume-scope";import { withSyncLease } from "../../plugins/sync/lease";+import { SyncLeaseBusyError } from "../../plugins/sync/lease-busy-error";import {publishTaskMutation,recordTaskMutation,@@ -27,9 +28,15 @@ export async function resumeSync(token: string,source: "kaneo" | "provider",) {- return withSyncLease(`sync-resume:${linkId}`, () =>- resumeWithLease(projectId, provider, linkId, token, source),- );+ try {+ return await withSyncLease(`sync-resume:${linkId}`, () =>+ resumeWithLease(projectId, provider, linkId, token, source),+ );+ } catch (error) {+ if (error instanceof SyncLeaseBusyError)+ throw new HTTPException(409, { message: error.message });+ throw error;+ }}async function resumeWithLease(@@ -61,6 +68,11 @@ async function resumeWithLease(updatedAt = (await review.access.write(review.local)).updatedAt;providerWritten = true;} catch {+ console.error("Sync resume provider write failed", {+ projectId,+ provider,+ linkId,+ });throw new HTTPException(502, {message: "External issue could not be updated; sync remains paused",});diff --git a/apps/api/src/integration-sync/controllers/review-resume.ts b/apps/api/src/integration-sync/controllers/review-resume.tsindex b3fe301e5..77df4eca4 100644--- a/apps/api/src/integration-sync/controllers/review-resume.ts+++ b/apps/api/src/integration-sync/controllers/review-resume.ts@@ -48,46 +48,53 @@ export async function reviewSyncResume(message:"The task must match the current rule and be paused before resuming",});+ const local = {+ title: task.title,+ description: task.description ?? "",+ state: (await isTaskInFinalState(task, database))+ ? ("closed" as const)+ : ("open" as const),+ };+ let access: Awaited<ReturnType<typeof providerIssue>>;+ let remoteIssue: Awaited<ReturnType<typeof access.read>>;try {- const access = await providerIssue(integration, link);- const remoteIssue = await access.read();- const local = {- title: task.title,- description: task.description ?? "",- state: (await isTaskInFinalState(task, database))- ? ("closed" as const)- : ("open" as const),- };- const remote = {- title: remoteIssue.title,- description: remoteIssue.description,- state: remoteIssue.state,- };- const token = createHash("sha256")- .update(- JSON.stringify({- binding: [integration.id, integration.config],- task,- link,- local,- remoteIssue,- }),- )- .digest("hex");- return {- integration,- link,- task,- access,- local,- remote,- remoteIssueLabels: remoteIssue.labels,- remoteIssueUpdatedAt: remoteIssue.updatedAt,- token,- };+ access = await providerIssue(integration, link);+ remoteIssue = await access.read();} catch {+ console.error("Sync resume provider read failed", {+ projectId,+ provider,+ linkId,+ });throw new HTTPException(502, {message: "External issue could not be read; sync remains paused",});}+ const remote = {+ title: remoteIssue.title,+ description: remoteIssue.description,+ state: remoteIssue.state,+ };+ const token = createHash("sha256")+ .update(+ JSON.stringify({+ binding: [integration.id, integration.config],+ task,+ link,+ local,+ remoteIssue,+ }),+ )+ .digest("hex");+ return {+ integration,+ link,+ task,+ access,+ local,+ remote,+ remoteIssueLabels: remoteIssue.labels,+ remoteIssueUpdatedAt: remoteIssue.updatedAt,+ token,+ };}diff --git a/apps/api/src/integration-sync/index.ts b/apps/api/src/integration-sync/index.tsindex 837e81592..24d06aaf4 100644--- a/apps/api/src/integration-sync/index.ts+++ b/apps/api/src/integration-sync/index.ts@@ -123,6 +123,9 @@ const resumeRoute = createRoute({responses: {200: jsonResponse("Sync resumed", z.object({ success: z.boolean() })),...errors,+ 409: errorResponse(+ "Comparison changed or synchronization is busy; review or retry",+ ),502: errorResponse("External issue unavailable; sync remains paused"),},});diff --git a/apps/api/src/plugins/gitea/utils/labels.ts b/apps/api/src/plugins/gitea/utils/labels.tsindex 9bac9da17..8d8ba2dec 100644--- a/apps/api/src/plugins/gitea/utils/labels.ts+++ b/apps/api/src/plugins/gitea/utils/labels.ts@@ -23,6 +23,7 @@ export async function ensureLabelsExistGitea(config: GiteaConfig,labels: string[],requireSuccess = false,+ write: IssueWrite = (send) => send(),): Promise<Map<string, number>> {const client = createGiteaClient(config);const map = new Map<string, number>();@@ -52,11 +53,8 @@ export async function ensureLabelsExistGitea(}const color = getLabelColor(name);- const created = await client.createLabel(- repositoryOwner,- repositoryName,- name,- color,+ const created = await write(() =>+ client.createLabel(repositoryOwner, repositoryName, name, color),);nameToId.set(name, created.id);map.set(name, created.id);@@ -81,6 +79,7 @@ export async function addLabelsToIssueGitea(config,labelNames,requireSuccess,+ write,);const ids: number[] = [];for (const name of labelNames) {diff --git a/apps/api/src/plugins/github/utils/labels.ts b/apps/api/src/plugins/github/utils/labels.tsindex 131df9d34..eafec3737 100644--- a/apps/api/src/plugins/github/utils/labels.ts+++ b/apps/api/src/plugins/github/utils/labels.ts@@ -24,6 +24,7 @@ export async function ensureLabelsExist(repo: string,labels: string[],requireSuccess = false,+ write: IssueWrite = (send) => send(),) {for (const labelName of labels) {try {@@ -35,12 +36,14 @@ export async function ensureLabelsExist(} catch {try {const color = getLabelColor(labelName);- await octokit.rest.issues.createLabel({- owner,- repo,- name: labelName,- color,- });+ await write(() =>+ octokit.rest.issues.createLabel({+ owner,+ repo,+ name: labelName,+ color,+ }),+ );} catch (createError) {if (requireSuccess) throw createError;console.error(`Failed to create label "${labelName}":`, createError);@@ -59,7 +62,14 @@ export async function addLabelsToIssue(write: IssueWrite = (send) => send(),) {try {- await ensureLabelsExist(octokit, owner, repo, labels, requireSuccess);+ await ensureLabelsExist(+ octokit,+ owner,+ repo,+ labels,+ requireSuccess,+ write,+ );await write(() =>octokit.rest.issues.addLabels({diff --git a/apps/api/src/plugins/gitlab/utils/labels.ts b/apps/api/src/plugins/gitlab/utils/labels.tsindex 480b36c55..44202b02e 100644--- a/apps/api/src/plugins/gitlab/utils/labels.ts+++ b/apps/api/src/plugins/gitlab/utils/labels.ts@@ -24,6 +24,7 @@ export async function ensureLabelsExistGitlab(config: GitlabConfig,labels: string[],requireSuccess = false,+ write: IssueWrite = (send) => send(),): Promise<void> {if (labels.length === 0) return;@@ -46,7 +47,9 @@ export async function ensureLabelsExistGitlab(if (existing.includes(name)) continue;try {- await client.createLabel(projectPath, name, getLabelColor(name));+ await write(() =>+ client.createLabel(projectPath, name, getLabelColor(name)),+ );} catch (error) {if (requireSuccess) throw error;console.error(`Failed to ensure GitLab label "${name}":`, error);@@ -69,7 +72,7 @@ export async function updateIssueLabelsGitlab(if (add.length === 0 && remove.length === 0) return;- await ensureLabelsExistGitlab(config, add, requireSuccess);+ await ensureLabelsExistGitlab(config, add, requireSuccess, write);const body: Record<string, string> = {};if (add.length > 0) {diff --git a/apps/api/src/plugins/sync/lease-busy-error.ts b/apps/api/src/plugins/sync/lease-busy-error.tsnew file mode 100644index 000000000..731beebde--- /dev/null+++ b/apps/api/src/plugins/sync/lease-busy-error.ts@@ -0,0 +1,6 @@+export class SyncLeaseBusyError extends Error {+ constructor() {+ super("Synchronization is busy; retry shortly");+ this.name = "SyncLeaseBusyError";+ }+}diff --git a/apps/api/src/plugins/sync/lease.ts b/apps/api/src/plugins/sync/lease.tsindex b521712d0..d41bc5278 100644--- a/apps/api/src/plugins/sync/lease.ts+++ b/apps/api/src/plugins/sync/lease.ts@@ -2,14 +2,18 @@ import { randomUUID } from "node:crypto";import { setTimeout as delay } from "node:timers/promises";import { sql } from "drizzle-orm";import db from "../../database";+import { SyncLeaseBusyError } from "./lease-busy-error";const LEASE_MS = 15 * 60 * 1000;export async function withSyncLease<T>(key: string,run: () => Promise<T>,+ { maxWaitMs = 5_000 }: { maxWaitMs?: number } = {},): Promise<T> {const owner = randomUUID();+ const deadline = performance.now() + maxWaitMs;+ let retryDelay = 100;for (;;) {const claimed = await db.execute(sql`insert into job_lease (name, owner, expires_at)@@ -19,7 +23,10 @@ export async function withSyncLease<T>(returning name`);if (claimed.rowCount) break;- await delay(100);+ const remaining = deadline - performance.now();+ if (remaining <= 0) throw new SyncLeaseBusyError();+ await delay(Math.min(retryDelay, remaining));+ retryDelay = Math.min(retryDelay * 2, 1_000);}// Claims use short statements rather than reserving application connections// during provider requests. Slow exports block only their own task key.diff --git a/apps/docs/openapi.json b/apps/docs/openapi.jsonindex 7dd6f3d07..6ca25821d 100644--- a/apps/docs/openapi.json+++ b/apps/docs/openapi.json@@ -14564,7 +14564,7 @@}},"409": {- "description": "Configuration, impact or comparison changed; review again",+ "description": "Comparison changed or synchronization is busy; review or retry","content": {"text/plain": {"schema": {diff --git a/tests/api-integration/github-import-bounds.test.ts b/tests/api-integration/github-import-bounds.test.tsindex a56003bd4..3882002af 100644--- a/tests/api-integration/github-import-bounds.test.ts+++ b/tests/api-integration/github-import-bounds.test.ts@@ -723,6 +723,74 @@ describe("bounded resumable GitHub import", () => {expect(mocks.publish).toHaveBeenCalledTimes(1);});+ it.each(["excluded", "paused"])(+ "imports pull request links for tasks with %s issue sync",+ async (scope) => {+ const { project, integration, config } = await setup();+ const [task] = await db+ .insert(schema.taskTable)+ .values({+ projectId: project.id,+ number: 1,+ title: "Existing task",+ status: "to-do",+ })+ .returning();+ if (scope === "excluded")+ await db+ .update(schema.integrationTable)+ .set({+ config: JSON.stringify({+ ...config,+ syncRules: {+ outgoing: { mode: "labels", match: "any", labels: ["missing"] },+ incoming: { mode: "all" },+ },+ }),+ })+ .where(eq(schema.integrationTable.id, integration.id));+ else+ await db.insert(schema.externalLinkTable).values({+ taskId: task!.id,+ integrationId: integration.id,+ resourceType: "issue",+ externalId: "1",+ url: "https://github.com/example/repo/issues/1",+ metadata: JSON.stringify({ syncFilterPaused: true }),+ });+ mocks.graphql.mockImplementation(async (query: string) => {+ if (query.includes("query ImportIssues(")) return issuePage([]);+ return {+ repository: {+ databaseId: 2,+ pullRequests: connection([+ {+ number: 5,+ title: `${project.slug.toUpperCase()}-1`,+ body: null,+ url: "https://github.com/example/repo/pull/5",+ state: "OPEN",+ createdAt: old,+ headRefName: `${project.slug.toLowerCase()}-1`,+ author: null,+ },+ ]),+ },+ };+ });+ expect((await importIssues(project.id)).pending).toBe(false);+ expect(+ await db.query.externalLinkTable.findFirst({+ where: eq(schema.externalLinkTable.resourceType, "pull_request"),+ }),+ ).toMatchObject({+ taskId: task!.id,+ integrationId: integration.id,+ externalId: "5",+ });+ },+ );+it("continues all pull request pages and links only matching tasks in this project", async () => {const { project, integration } = await setup();mocks.graphql.mockImplementation(diff --git a/tests/api-integration/integration-label-dispatch.test.ts b/tests/api-integration/integration-label-dispatch.test.tsindex 04d902c67..06436bc98 100644--- a/tests/api-integration/integration-label-dispatch.test.ts+++ b/tests/api-integration/integration-label-dispatch.test.ts@@ -1,3 +1,11 @@+import { initializeTaskIssue } from "../../apps/api/src/plugins/sync/initialize-task-issue";+import { addLabelsToIssue } from "../../apps/api/src/plugins/github/utils/labels";+import { addLabelsToIssueGitea } from "../../apps/api/src/plugins/gitea/utils/labels";+import { addLabelsToIssueGitlab } from "../../apps/api/src/plugins/gitlab/utils/labels";+import { getVerifiedInstallationOctokit } from "../../apps/api/src/plugins/github/utils/github-app";+import type { GiteaConfig } from "../../apps/api/src/plugins/gitea/config";+import type { GitlabConfig } from "../../apps/api/src/plugins/gitlab/config";+import type { GitHubConfig } from "../../apps/api/src/plugins/github/config";import { eq } from "drizzle-orm";import { beforeEach, expect, it, vi } from "vite-plus/test";import db, { schema } from "../../apps/api/src/database";@@ -22,6 +30,7 @@ import {} from "./helpers/fixtures";const mocks = vi.hoisted(() => ({+ lookup: vi.fn(),createLabel: vi.fn(),addLabel: vi.fn(),removeLabel: vi.fn(),@@ -31,6 +40,7 @@ vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({rest: {issues: {getLabel: async () => {+ await mocks.lookup();throw new Error("Label does not exist");},createLabel: mocks.createLabel,@@ -42,7 +52,10 @@ vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({}));vi.mock("../../apps/api/src/plugins/gitea/utils/gitea-api", () => ({createGiteaClient: () => ({- listLabels: async () => [{ id: 7, name: "bug" }],+ listLabels: async () => {+ await mocks.lookup();+ return [{ id: 7, name: "bug" }];+ },getIssue: async () => ({ labels: [] }),createLabel: mocks.createLabel,addLabelsToIssue: mocks.addLabel,@@ -51,7 +64,10 @@ vi.mock("../../apps/api/src/plugins/gitea/utils/gitea-api", () => ({}));vi.mock("../../apps/api/src/plugins/gitlab/utils/gitlab-api", () => ({createGitlabClient: () => ({- listLabels: async () => [],+ listLabels: async () => {+ await mocks.lookup();+ return [];+ },getIssue: async () => ({ labels: [] }),createLabel: mocks.createLabel,updateIssue: async (@@ -65,11 +81,165 @@ vi.mock("../../apps/api/src/plugins/gitlab/utils/gitlab-api", () => ({beforeEach(async () => {await resetTestDatabase();vi.clearAllMocks();+ mocks.lookup.mockResolvedValue(undefined);mocks.createLabel.mockResolvedValue({ id: 7, name: "bug" });mocks.addLabel.mockResolvedValue(undefined);mocks.removeLabel.mockResolvedValue(undefined);});+it.each(["github", "gitea", "gitlab"] as const)(+ "%s initialization does not create labels after a pause during label lookup",+ async (provider) => {+ const f = await fixture(provider);+ await updateExternalLink(f.link.id, {+ metadata: { syncInitializationPending: true, syncInitializedState: true },+ });+ const link = (await db.query.externalLinkTable.findFirst({+ where: eq(schema.externalLinkTable.id, f.link.id),+ }))!;+ const octokit = await getVerifiedInstallationOctokit(+ f.config as GitHubConfig,+ );+ let started!: () => void;+ let release!: () => void;+ const listed = new Promise<void>((resolve) => {+ started = resolve;+ });+ const gate = new Promise<void>((resolve) => {+ release = resolve;+ });+ mocks.lookup.mockImplementationOnce(async () => {+ started();+ await gate;+ });+ const initializing = initializeTaskIssue(+ {+ taskId: f.task.id,+ projectId: f.project.id,+ userId: "",+ title: f.task.title,+ description: null,+ priority: "low",+ status: "to-do",+ number: 1,+ },+ {+ integrationId: f.integration.id,+ projectId: f.project.id,+ config: f.config,+ },+ link,+ {+ close: mocks.addLabel,+ labels: (names, write) => {+ if (provider === "github")+ return addLabelsToIssue(+ octokit,+ "team",+ "repo",+ 12,+ names,+ true,+ write,+ );+ if (provider === "gitea")+ return addLabelsToIssueGitea(+ f.config as GiteaConfig,+ 12,+ names,+ true,+ write,+ );+ return addLabelsToIssueGitlab(+ f.config as GitlabConfig,+ 12,+ names,+ true,+ write,+ );+ },+ },+ ).then(+ () => undefined,+ (error: unknown) => error,+ );+ try {+ await listed;+ await updateExternalLink(f.link.id, {+ metadata: { syncFilterPaused: true },+ });+ } finally {+ release();+ }+ expect(await initializing).toBeInstanceOf(Error);+ expect(mocks.createLabel).not.toHaveBeenCalled();+ expect(mocks.addLabel).not.toHaveBeenCalled();+ expect(+ JSON.parse(+ (await db.query.externalLinkTable.findFirst({+ where: eq(schema.externalLinkTable.id, f.link.id),+ }))!.metadata!,+ ),+ ).toMatchObject({+ syncInitializationPending: true,+ syncFilterPaused: true,+ });+ },+);++async function fixture(provider: "github" | "gitea" | "gitlab") {+ const { user, workspace } = await createWorkspaceMember();+ const { project } = await createProjectFixture({+ workspaceId: workspace.id,+ });+ const config = {+ repositoryOwner: "team",+ repositoryName: "repo",+ repositoryId: 1,+ installationId: 2,+ verifiedGithubAccountId: "3",+ verifiedByUserId: user.id,+ baseUrl: "https://git.example",+ projectPath: "team/repo",+ accessToken: "fake-test-token",+ };+ const [integration] = await db+ .insert(schema.integrationTable)+ .values({+ projectId: project.id,+ type: provider,+ isActive: true,+ config: JSON.stringify(config),+ })+ .returning();+ const [task] = await db+ .insert(schema.taskTable)+ .values({+ projectId: project.id,+ title: "Label race",+ number: 1,+ status: "to-do",+ })+ .returning();+ const [link] = await db+ .insert(schema.externalLinkTable)+ .values({+ taskId: task!.id,+ integrationId: integration!.id,+ resourceType: "issue",+ externalId: "12",+ url: "https://git.example/team/repo/issues/12",+ })+ .returning();+ return {+ project,+ config,+ integration: integration!,+ task: task!,+ link: link!,+ };+}+it.each((["github", "gitea", "gitlab"] as const).flatMap((provider) =>(["add", "remove"] as const).flatMap((action) =>@@ -83,49 +253,7 @@ it.each()("$provider custom-label $action rechecks scope at dispatch ($change)",async ({ provider, action, change }) => {- const { user, workspace } = await createWorkspaceMember();- const { project } = await createProjectFixture({- workspaceId: workspace.id,- });- const config = {- repositoryOwner: "team",- repositoryName: "repo",- repositoryId: 1,- installationId: 2,- verifiedGithubAccountId: "3",- verifiedByUserId: user.id,- baseUrl: "https://git.example",- projectPath: "team/repo",- accessToken: "fake-test-token",- };- const [integration] = await db- .insert(schema.integrationTable)- .values({- projectId: project.id,- type: provider,- isActive: true,- config: JSON.stringify(config),- })- .returning();- const [task] = await db- .insert(schema.taskTable)- .values({- projectId: project.id,- title: "Label race",- number: 1,- status: "to-do",- })- .returning();- const [link] = await db- .insert(schema.externalLinkTable)- .values({- taskId: task!.id,- integrationId: integration!.id,- resourceType: "issue",- externalId: "12",- url: "https://git.example/team/repo/issues/12",- })- .returning();+ const { config, integration, task, link } = await fixture(provider);let started!: () => void;let release!: () => void;const checked = new Promise<void>((resolve) => {diff --git a/tests/api-integration/integration-sync-rules.test.ts b/tests/api-integration/integration-sync-rules.test.tsindex 07b21f927..269d9a5c0 100644--- a/tests/api-integration/integration-sync-rules.test.ts+++ b/tests/api-integration/integration-sync-rules.test.ts@@ -479,6 +479,34 @@ describe("reviewed sync resume", () => {const link = await f.link(true);return { ...f, link };}+ it("returns a retryable conflict when another instance retains the resume lease", async () => {+ const f = await paused();+ await db.insert(schema.jobLeaseTable).values({+ name: `sync-resume:${f.link.id}`,+ owner: "crashed-test-instance",+ expiresAt: new Date(Date.now() + 15 * 60_000),+ });+ const response = await f.request(`/links/${f.link.id}/resume`, "POST", {+ source: "kaneo",+ token: "a".repeat(64),+ });+ expect(response.status).toBe(409);+ expect(await response.text()).toBe(+ "Synchronization is busy; retry shortly",+ );+ expect(provider.read).not.toHaveBeenCalled();+ expect(provider.write).not.toHaveBeenCalled();+ expect((await db.query.jobLeaseTable.findFirst())?.owner).toBe(+ "crashed-test-instance",+ );+ expect(+ isSyncPaused(+ (await db.query.externalLinkTable.findFirst({+ where: eq(schema.externalLinkTable.id, f.link.id),+ }))!.metadata,+ ),+ ).toBe(true);+ });for (const authentication of ["custom-role", "api-key"] as const) {it.each([{}, { project: ["read"] }, { task: ["read"] }] as Array<Record<string, string[]>@@ -990,6 +1018,47 @@ describe("reviewed sync resume", () => {).rejects.toMatchObject({ status: 502 });expect(await canSyncTask(f.task.id, f.integration.id)).toBe(false);});+ it.each(["read", "write"] as const)(+ "logs the failed resume %s stage without provider secrets",+ async (stage) => {+ const f = await paused();+ const review = await reviewSyncResume(f.project.id, "gitea", f.link.id);+ const failure = Object.assign(new Error("private-provider-response"), {+ request: { headers: { authorization: "fake-test-secret" } },+ });+ provider[stage].mockRejectedValue(failure);+ const log = vi.spyOn(console, "error").mockImplementation(() => {});+ try {+ const response = await f.request(+ `/links/${f.link.id}/${stage === "read" ? "review" : "resume"}`,+ stage === "read" ? "GET" : "POST",+ stage === "read"+ ? undefined+ : { source: "kaneo", token: review.token },+ );+ expect(response.status).toBe(502);+ expect(await response.text()).not.toContain(+ "private-provider-response",+ );+ expect(log).toHaveBeenCalledWith(+ `Sync resume provider ${stage} failed`,+ {+ projectId: f.project.id,+ provider: "gitea",+ linkId: f.link.id,+ },+ );+ expect(JSON.stringify(log.mock.calls)).not.toContain(+ "fake-test-secret",+ );+ expect(JSON.stringify(log.mock.calls)).not.toContain(+ "private-provider-response",+ );+ } finally {+ log.mockRestore();+ }+ },+ );it("does not review an excluded task or a link from another integration", async () => {const f = await paused();await dbdiff --git a/tests/api-integration/sync-lease.test.ts b/tests/api-integration/sync-lease.test.tsnew file mode 100644index 000000000..4c8603e96--- /dev/null+++ b/tests/api-integration/sync-lease.test.ts@@ -0,0 +1,37 @@+import { eq } from "drizzle-orm";+import { beforeEach, expect, it, vi } from "vite-plus/test";+import db, { getDatabasePool, schema } from "../../apps/api/src/database";+import { withSyncLease } from "../../apps/api/src/plugins/sync/lease";+import { SyncLeaseBusyError } from "../../apps/api/src/plugins/sync/lease-busy-error";+import { resetTestDatabase } from "./helpers/database";++beforeEach(resetTestDatabase);++it("bounds contention with backoff, preserves another owner's lease, and allows a later retry", async () => {+ const key = "sync-create:crashed-holder";+ await db.insert(schema.jobLeaseTable).values({+ name: key,+ owner: "other-instance",+ expiresAt: new Date(Date.now() + 15 * 60_000),+ });+ const run = vi.fn(async () => "complete");+ const queries = vi.spyOn(getDatabasePool(), "query");+ const started = performance.now();+ await expect(+ withSyncLease(key, run, { maxWaitMs: 120 }),+ ).rejects.toBeInstanceOf(SyncLeaseBusyError);+ expect(performance.now() - started).toBeLessThan(1_000);+ expect(run).not.toHaveBeenCalled();+ expect(queries.mock.calls.length).toBeLessThanOrEqual(4);+ expect((await db.query.jobLeaseTable.findFirst())?.owner).toBe(+ "other-instance",+ );+ await db+ .delete(schema.jobLeaseTable)+ .where(eq(schema.jobLeaseTable.name, key));+ await expect(withSyncLease(key, run, { maxWaitMs: 120 })).resolves.toBe(+ "complete",+ );+ expect(run).toHaveBeenCalledOnce();+ expect(await db.query.jobLeaseTable.findMany()).toHaveLength(0);+});