Rewrite resolveWorkspace to per-task lockMode inference; drop company_fallback silent share (B4-adjacent)
Old resolver: task → agent → project → any company workspace, all
marked exclusive. That fourth step folded every runnable agent in a
multi-agent company onto a single workspace row and serialised the
C-suite (four agents, one exclusive lock, three starved).
New resolver: lockMode='exclusive' is now an opt-in decision inferred
per task. Three rules:
1. task.workspaceId set → exclusive on that workspace (explicit).
2. filesystem signal (task.requiresFilesystem OR agent in a
coding role/engineering department) → exclusive on
agent.workspaceId / project primary; throws if neither exists.
3. otherwise → none / null. Pure cognitive work runs with no
workspace lock and fully in parallel.
Removed:
- company_fallback silent-share path. Filesystem-touching runs
with no legitimate binding now throw WorkspaceConfigurationError
at resolve time so the caller decides recovery rather than the
resolver picking a random shared workspace.
- workspacesMod.getAnyCompanyWorkspace (now dead code).
Invariants now enforced by types:
- lockMode='exclusive' ⇒ workspace is a live WorkspaceRecord.
- lockMode='none' ⇒ workspace is null.
Tests updated to cover the new decision table and the four-non-
coding-agents parallel case that was the original bug.
This commit is contained in:
1 parent
7829acc7f0
commit
28cdf7c56f
4 files changed
+252
-80
No files matched your search
@@ -13,17 +13,22 @@ vi.mock("electron", () => ({
|
||||
}));
|
||||
|
||||
import { AppDatabase } from "../../src/main/database";
|
||||
import { resolveWorkspaceWithLockMode } from "../../src/main/orchestration/resolve-workspace";
|
||||
import {
|
||||
resolveWorkspaceWithLockMode,
|
||||
WorkspaceConfigurationError,
|
||||
} from "../../src/main/orchestration/resolve-workspace";
|
||||
|
||||
/**
|
||||
* Regression tests for the current workspace lock policy: every
|
||||
* resolved workspace is held exclusively for the duration of the run.
|
||||
* Pi's coding tools always include bash/write/edit, so there is no
|
||||
* read-only category where concurrent runs are safe; teammates
|
||||
* sharing one repo queue through the wakeup table instead of racing
|
||||
* the filesystem.
|
||||
* Workspace lock policy (v52+).
|
||||
*
|
||||
* See `resolve-workspace.ts` for the full rationale.
|
||||
* `lockMode='exclusive'` is opt-in — either the task carries an
|
||||
* explicit workspaceId, or the task's requiresFilesystem column is
|
||||
* true, or the resolving agent is in a coding role. Every other
|
||||
* case returns `lockMode='none'` and `workspace=null`, so four
|
||||
* pure-cognitive agents run fully in parallel without queueing on
|
||||
* a single workspace lock. The previous `company_fallback` step
|
||||
* (which returned any workspace in the company as the lock target)
|
||||
* has been removed and now throws `WorkspaceConfigurationError`.
|
||||
*/
|
||||
|
||||
function seedCompany(db: AppDatabase) {
|
||||
@@ -64,12 +69,12 @@ describe("resolveWorkspaceWithLockMode", () => {
|
||||
await rm(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
it("task-bound workspace → exclusive (explicit opt-in)", () => {
|
||||
it("task.workspaceId set → exclusive on that workspace", () => {
|
||||
const { companyId, workspaceId } = seedCompany(db);
|
||||
const result = resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
{ workspaceId, projectId: null },
|
||||
{ workspaceId: null },
|
||||
{ workspaceId: null, role: "CMO", department: "marketing" },
|
||||
companyId,
|
||||
);
|
||||
expect(result.source).toBe("task");
|
||||
@@ -77,19 +82,20 @@ describe("resolveWorkspaceWithLockMode", () => {
|
||||
expect(result.workspace?.id).toBe(workspaceId);
|
||||
});
|
||||
|
||||
it("agent-bound workspace → exclusive (single writer per repo)", () => {
|
||||
it("coding-role agent + agent workspace → exclusive on agent workspace", () => {
|
||||
const { companyId, workspaceId } = seedCompany(db);
|
||||
const result = resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
null,
|
||||
{ workspaceId },
|
||||
{ workspaceId, role: "CTO", department: "engineering" },
|
||||
companyId,
|
||||
);
|
||||
expect(result.source).toBe("agent");
|
||||
expect(result.lockMode).toBe("exclusive");
|
||||
expect(result.workspace?.id).toBe(workspaceId);
|
||||
});
|
||||
|
||||
it("project-bound workspace → exclusive (single writer per repo)", () => {
|
||||
it("coding-role agent + project primary workspace → exclusive on project workspace", () => {
|
||||
const { companyId } = seedCompany(db);
|
||||
const projectId = db.entityCrud.saveProject({
|
||||
companyId,
|
||||
@@ -97,7 +103,7 @@ describe("resolveWorkspaceWithLockMode", () => {
|
||||
description: "",
|
||||
status: "active",
|
||||
});
|
||||
db.workspacesMod.saveWorkspace({
|
||||
const projectWsId = db.workspacesMod.saveWorkspace({
|
||||
companyId,
|
||||
name: "Project repo",
|
||||
localPath: "/tmp/project-ws",
|
||||
@@ -106,30 +112,43 @@ describe("resolveWorkspaceWithLockMode", () => {
|
||||
projectId,
|
||||
isPrimary: true,
|
||||
});
|
||||
|
||||
const result = resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
{ workspaceId: null, projectId },
|
||||
{ workspaceId: null },
|
||||
{ workspaceId: null, projectId, requiresFilesystem: true },
|
||||
{ workspaceId: null, role: "CEO", department: "executive" },
|
||||
companyId,
|
||||
);
|
||||
expect(result.source).toBe("project");
|
||||
expect(result.lockMode).toBe("exclusive");
|
||||
expect(result.workspace?.id).toBe(projectWsId);
|
||||
});
|
||||
|
||||
it("company-fallback → exclusive (single writer per repo)", () => {
|
||||
it("task.requiresFilesystem signals exclusive even for non-coding role", () => {
|
||||
const { companyId, workspaceId } = seedCompany(db);
|
||||
const result = resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
{ workspaceId: null, projectId: null, requiresFilesystem: true },
|
||||
{ workspaceId, role: "COO", department: "operations" },
|
||||
companyId,
|
||||
);
|
||||
expect(result.lockMode).toBe("exclusive");
|
||||
expect(result.source).toBe("agent");
|
||||
});
|
||||
|
||||
it("pure cognitive task (no coding role, no requiresFilesystem) → none", () => {
|
||||
const { companyId } = seedCompany(db);
|
||||
const result = resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
null,
|
||||
{ workspaceId: null },
|
||||
{ workspaceId: null, projectId: null },
|
||||
{ workspaceId: null, role: "CMO", department: "marketing" },
|
||||
companyId,
|
||||
);
|
||||
expect(result.source).toBe("company_fallback");
|
||||
expect(result.lockMode).toBe("exclusive");
|
||||
expect(result.source).toBe("none");
|
||||
expect(result.lockMode).toBe("none");
|
||||
expect(result.workspace).toBeNull();
|
||||
});
|
||||
|
||||
it("task workspace wins over agent workspace even when both are set", () => {
|
||||
it("task.workspaceId wins over agent.workspaceId even when both are set", () => {
|
||||
const { companyId, workspaceId: agentWsId } = seedCompany(db);
|
||||
const taskWsId = db.workspacesMod.saveWorkspace({
|
||||
companyId,
|
||||
@@ -142,7 +161,7 @@ describe("resolveWorkspaceWithLockMode", () => {
|
||||
const result = resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
{ workspaceId: taskWsId, projectId: null },
|
||||
{ workspaceId: agentWsId },
|
||||
{ workspaceId: agentWsId, role: "CTO", department: "engineering" },
|
||||
companyId,
|
||||
);
|
||||
expect(result.source).toBe("task");
|
||||
@@ -150,7 +169,23 @@ describe("resolveWorkspaceWithLockMode", () => {
|
||||
expect(result.workspace?.id).toBe(taskWsId);
|
||||
});
|
||||
|
||||
it("no workspace at all → none", () => {
|
||||
it("filesystem-requiring task with no resolvable workspace → throws WorkspaceConfigurationError", () => {
|
||||
const companyId = db.company.saveCompany({
|
||||
name: "Empty Co",
|
||||
description: "",
|
||||
status: "active",
|
||||
});
|
||||
expect(() =>
|
||||
resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
{ workspaceId: null, projectId: null, requiresFilesystem: true },
|
||||
{ workspaceId: null, role: "CTO", department: "engineering" },
|
||||
companyId,
|
||||
),
|
||||
).toThrow(WorkspaceConfigurationError);
|
||||
});
|
||||
|
||||
it("non-coding role with no workspace bindings → none (never throws, never falls back to shared lock)", () => {
|
||||
const companyId = db.company.saveCompany({
|
||||
name: "Empty Co",
|
||||
description: "",
|
||||
@@ -159,11 +194,27 @@ describe("resolveWorkspaceWithLockMode", () => {
|
||||
const result = resolveWorkspaceWithLockMode(
|
||||
db,
|
||||
null,
|
||||
{ workspaceId: null },
|
||||
{ workspaceId: null, role: "CFO", department: "finance" },
|
||||
companyId,
|
||||
);
|
||||
expect(result.source).toBe("none");
|
||||
expect(result.lockMode).toBe("none");
|
||||
expect(result.workspace).toBeNull();
|
||||
});
|
||||
|
||||
it("four non-coding agents resolving in parallel all get lockMode=none (no shared lock collision)", () => {
|
||||
const { companyId } = seedCompany(db);
|
||||
const roles = [
|
||||
{ role: "CEO", department: "executive" as const },
|
||||
{ role: "CMO", department: "marketing" as const },
|
||||
{ role: "CPO", department: "product" as const },
|
||||
{ role: "COO", department: "operations" as const },
|
||||
];
|
||||
const modes = roles.map((r) =>
|
||||
resolveWorkspaceWithLockMode(db, null, { workspaceId: null, ...r }, companyId),
|
||||
);
|
||||
for (const m of modes) {
|
||||
expect(m.lockMode).toBe("none");
|
||||
expect(m.workspace).toBeNull();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user