diff --git a/server/src/services/online-workspace-source.ts b/server/src/services/online-workspace-source.ts index 3a52d3a29a5..c1892888434 100644 --- a/server/src/services/online-workspace-source.ts +++ b/server/src/services/online-workspace-source.ts @@ -6,6 +6,7 @@ import { executionWorkspaces, heartbeatRuns, issueRecoveryActions, type Db } fro import { resolvePaperclipInstanceRoot } from "../home-paths.js"; import { explainOnlineOrdinarySource, inspectImportedWorkspaceSourcePaths, physicalPath, requiresOnlineOrdinarySource, type WorkspaceSourceScope } from "./remote-workspace-migration-tree.js"; import { workspaceSourceIdentities } from "./workspace-source-lock.js"; +import { WORKSPACE_SOURCE_KEY_LIMIT } from "./workspace-source-key-limit.js"; import { readNestedWorkspaceGitSourceRoots, readWorkspaceGitLinkedRoots, readWorkspaceGitSourceRoots, type WorkspaceGitSourceRoots } from "./workspace-git-source-roots.js"; import { inspectOnlineLegacyWorkspaceRecoveryHold, inspectOnlineLegacyWorkspaceRecoverySource, type OnlineLegacyWorkspaceRecoveryHold } from "./online-legacy-workspace-recovery.js"; import type { LegacyWorkspaceRecoverySource } from "./legacy-workspace-recovery-migration.js"; @@ -199,7 +200,7 @@ export async function explainOnlineWorkspaceSource(db: ReadDb, workspace: Worksp if ([...linkedSources.values()].some(proof => proof.links.some(link => !registeredRoots.some(root => link.root.path === root || link.root.path.startsWith(root + path.sep))))) return unproven("git_link_unregistered"); const issueIds = [...new Set([...physicalIssueIds, ...included.map(node => node.row.sourceIssueId!)])].sort(); - if (issueIds.length + projectIds.size > 128) return unproven("source_keys_exceeded"); + if (issueIds.length + projectIds.size > WORKSPACE_SOURCE_KEY_LIMIT) return unproven("source_keys_exceeded"); const instance = await physicalPath(resolvePaperclipInstanceRoot()); const gitSourceRoots = [...[...projectIds].map(id => path.join(instance, "projects", workspace.companyId, id)), ...issueIds.flatMap(id => ["isolated-workspaces", "isolated-workspace-attempts", "isolated-repository-sources"].map(kind => diff --git a/server/src/services/remote-workspace-online-source.test.ts b/server/src/services/remote-workspace-online-source.test.ts index ffed9fbdc68..d0196e3e4f3 100644 --- a/server/src/services/remote-workspace-online-source.test.ts +++ b/server/src/services/remote-workspace-online-source.test.ts @@ -420,9 +420,25 @@ describe.skipIf(!support.supported)("online ordinary physical source fence", () } finally { release(); await owner; } }); it("bounds source-lock closures before reserving a connection", async () => { - await expect(withWorkspaceSourceLocks(db, { companyId: randomUUID(), issueIds: Array.from({ length: 129 }, () => randomUUID()) }, + await expect(withWorkspaceSourceLocks(db, { companyId: randomUUID(), issueIds: Array.from({ length: 513 }, () => randomUUID()) }, async () => { throw new Error("scope must not be admitted"); })).rejects.toThrow("workspace_source_scope_limit"); + await expect(withWorkspaceSourceLocks(db, { companyId: randomUUID(), issueIds: Array.from({ length: 511 }, () => randomUUID()), + projectIds: [randomUUID()] }, async lock => (await lock.execute(sql`select count(*)::int as held from pg_locks + where locktype='advisory' and pid=pg_backend_pid()`))[0]?.held)).resolves.toBe(512); }); + it("moves a Git folder online when its alias closure needs more than 128 source locks", async () => { + const f = await fixture(), repoUrl = "https://example.invalid/wide-closure.git"; + await git(f.cwd, "init", "-q", "-b", "main"); await git(f.cwd, "add", "."); await git(f.cwd, "commit", "-qm", "Saved legacy work"); + const extra = Array.from({ length: 130 }, () => randomUUID()); + await db.insert(issues).values(extra.map((id, n) => ({ id, companyId: f.companyId, projectId: f.projectId, title: `Wide alias ${n}` }))); + await db.insert(executionWorkspaces).values(extra.map((id, n) => ({ companyId: f.companyId, projectId: f.projectId, sourceIssueId: id, + name: `Wide alias ${n}`, mode: "shared_workspace" as const, strategyType: "project_primary", providerType: "local_fs", cwd: f.cwd, providerRef: f.cwd }))); + await db.update(executionWorkspaces).set({ repoUrl, branchName: "main" }).where(eq(executionWorkspaces.companyId, f.companyId)); + expect(await inventoryRemoteWorkspaceMigration(db, f)).toMatchObject({ eligible: true, reasons: [] }); + expect((await inspectOnlineWorkspaceSource(db, await stored(f.workspaceId)))?.issueIds).toHaveLength(133); + await expect(migrateRemoteWorkspace({ db, storage, ...f, apply: true })).resolves.toMatchObject({ applied: true }); + expect(await fs.readFile(path.join(f.cwd, "saved.txt"), "utf8")).toBe("saved legacy edits\n"); + }, 120_000); it.each(["task", "operation", "foreign", "descendant"] as const)("retains the %s guard across the complete alias closure", async kind => { const f = await fixture(), alias = f.aliases[2]; if (kind === "task") { diff --git a/server/src/services/remote-workspace-snapshot-archive.test.ts b/server/src/services/remote-workspace-snapshot-archive.test.ts index 8d501d19ac4..5ae4f1cbc1a 100644 --- a/server/src/services/remote-workspace-snapshot-archive.test.ts +++ b/server/src/services/remote-workspace-snapshot-archive.test.ts @@ -7,6 +7,7 @@ import path from "node:path"; import { promisify } from "node:util"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { workspaceArchive, readRemoteWorkspaceArchiveDirectories, remoteWorkspaceArchiveScript, remoteWorkspaceCaptureFailureMessage } from "./remote-workspace-snapshot-archive.js"; +import { WORKSPACE_SOURCE_KEY_LIMIT } from "./workspace-source-key-limit.js"; const archiveIo = vi.hoisted(() => ({ afterChmod: undefined as ((target: string, mode: number) => void) | undefined, @@ -118,6 +119,15 @@ describe("portable remote workspace archives", () => { expect(await fs.readFile(path.join(restored, "new.txt"), "utf8")).toBe("untracked\n"); await git(restored, "fsck", "--connectivity-only", "--no-dangling"); }); + it("accepts the Git source roots of a source-lock scope at its key bound and refuses more", async () => { + const cwd = await source(); await git(cwd, "init", "-q", "-b", "main"); + await fs.writeFile(path.join(cwd, "work.txt"), "saved\n"); await git(cwd, "add", "."); await git(cwd, "commit", "-qm", "base"); + const owned = await fs.realpath(root), others = (count: number) => Array.from({ length: count }, (_, n) => path.join(owned, "unrelated", String(n))); + await expect(captureRemoteWorkspaceDirectory({ cwd, archivePath: path.join(root, "bounded.pcws"), singlePass: true, + gitSourceRoots: [owned, ...others(WORKSPACE_SOURCE_KEY_LIMIT * 3 - 1)] })).resolves.toMatchObject({ manifestDigest: expect.any(String) }); + await expect(captureRemoteWorkspaceDirectory({ cwd, archivePath: path.join(root, "unbounded.pcws"), singlePass: true, + gitSourceRoots: [owned, ...others(WORKSPACE_SOURCE_KEY_LIMIT * 3)] })).rejects.toThrow("online_source_git_requires_shared_fence"); + }); it.each([".", "nested"])("requires a shared Git fence before reading included %s Git storage", async location => { const cwd = await source(), repository = path.join(cwd, location); await fs.mkdir(repository, { recursive: true }); diff --git a/server/src/services/remote-workspace-snapshot-archive.ts b/server/src/services/remote-workspace-snapshot-archive.ts index 40e231ca649..011a6cfdd52 100644 --- a/server/src/services/remote-workspace-snapshot-archive.ts +++ b/server/src/services/remote-workspace-snapshot-archive.ts @@ -309,7 +309,7 @@ export async function workspaceArchive(input: { }; const assertGitSourceFence = async (repo: string) => { if (!input.gitSourceRoots) return null; - if (!input.gitSourceRoots.length || input.gitSourceRoots.length > 384 + if (!input.gitSourceRoots.length || input.gitSourceRoots.length > 1536 || input.gitSourceRoots.some(root => !path.isAbsolute(root) || path.resolve(root) !== root)) fail("online_source_git_requires_shared_fence"); const directory = async (target: string) => { const actual = await fs.realpath(target), entry = await fs.lstat(target, { bigint: true }); diff --git a/server/src/services/workspace-source-key-limit.ts b/server/src/services/workspace-source-key-limit.ts new file mode 100644 index 00000000000..095252f69c2 --- /dev/null +++ b/server/src/services/workspace-source-key-limit.ts @@ -0,0 +1 @@ +export const WORKSPACE_SOURCE_KEY_LIMIT = 512; diff --git a/server/src/services/workspace-source-lock.ts b/server/src/services/workspace-source-lock.ts index 59fa5a334e0..f4d7b05f87a 100644 --- a/server/src/services/workspace-source-lock.ts +++ b/server/src/services/workspace-source-lock.ts @@ -6,6 +6,7 @@ import { resolvePaperclipInstanceRoot } from "../home-paths.js"; import { canonicalRepositoryRoot } from "./execution-workspace-source-roots.js"; import { readWorkspaceGitSourceRoots } from "./workspace-git-source-roots.js"; import { withWorkspaceLockKeepalive } from "./native-runtime/native-workspace-lock-keepalive.js"; +import { WORKSPACE_SOURCE_KEY_LIMIT } from "./workspace-source-key-limit.js"; type SourceTransaction = Parameters[0]>[0]; type SourceLockScope = { db: Db; companyId: string; keys: Set; transaction: SourceTransaction; active: boolean }; @@ -85,7 +86,7 @@ export async function withWorkspaceSourceLocks(db: Db, ...(input.issueIds ?? []).map(id => `isolated_repository_source:${input.companyId}:${id}`), ...(input.projectIds ?? []).map(id => `managed_project_source:${input.companyId}:${id}`), ])].sort(); - if (keys.length > 128) throw new Error("workspace_source_scope_limit"); + if (keys.length > WORKSPACE_SOURCE_KEY_LIMIT) throw new Error("workspace_source_scope_limit"); const held = heldScope(); if (wait && held?.db === db) throw new Error("workspace_source_wait_requires_independent_scope"); const acquire = async (scope: SourceLockScope, nonblocking: boolean) => { @@ -99,7 +100,7 @@ export async function withWorkspaceSourceLocks(db: Db, } }; if (held?.db === db) { - if (new Set([...held.keys, ...keys]).size > 128) throw new Error("workspace_source_scope_limit"); + if (new Set([...held.keys, ...keys]).size > WORKSPACE_SOURCE_KEY_LIMIT) throw new Error("workspace_source_scope_limit"); if (held.companyId !== input.companyId) throw new Error("workspace_source_company_mismatch"); const onlyProjectKeys = keys.filter(key => !held.keys.has(key)).every(key => key.startsWith("managed_project_source:")); const alreadyHasProject = [...held.keys].some(key => key.startsWith("managed_project_source:"));