Q8: one writing run per workspace directory

Write-granted runs whose directories overlap (same folder, symlink or
junction, or nested) wait for each other, at claim time and for manual
runs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 03:25:39 -04:00
1 parent 72eb645067
commit 2a7b17a47a
5 files changed
+168 -5

No files matched your search

@@ -25,6 +25,7 @@ import {
} from "./task-prompts";
import { renderTaskHandoff } from "./agent-task-handoff";
import { createRunMessagePoller, loadChatWakeContext } from "./chat-wake-context";
import { runningWorkspaceWriter } from "./workspace-write-lock";
import { loadComposioTools } from "./composio/composio-tool-bridge";
import { connectorDisplayName } from "./composio/connector-brands";
import { ConnectorGrantsService } from "./composio/connector-grants";
@@ -321,6 +322,8 @@ export class TaskExecutionService {
const busy = this.db.prepare("select t.title from task_runs r join tasks t on t.id = r.task_id where r.agent_id = ? and r.state = 'running' limit 1")
.get(agentId) as { title: string } | undefined;
if (busy) throw new Error(`This agent is already working on "${busy.title}"; run again when that session ends.`);
const writer = runningWorkspaceWriter(this.db, taskId);
if (writer !== null) throw new Error(`"${writer.title}" is changing this workspace right now; run again when it finishes.`);
const runId = createId("run");
const now = new Date().toISOString();
this.db.prepare(`
+39
View File
@@ -0,0 +1,39 @@
import type { DatabaseSync } from "node:sqlite";
import { existsSync, realpathSync } from "node:fs";
import { basename, dirname, join, resolve } from "node:path";
/** A running write task whose directory overlaps this task's, or null. */
export function runningWorkspaceWriter(db: DatabaseSync, taskId: string): { title: string } | null {
const task = db.prepare(`
select t.requires_filesystem, w.local_path
from tasks t left join workspaces w on w.id = t.workspace_id
where t.id = ?
`).get(taskId) as { requires_filesystem: number; local_path: string | null } | undefined;
if (!task) throw new Error(`Task ${taskId} was not found.`);
if (task.requires_filesystem !== 1 || !task.local_path) return null;
const mine = normalizeDirectory(task.local_path);
const writers = db.prepare(`
select t.title, w.local_path
from task_runs r
join tasks t on t.id = r.task_id
join workspaces w on w.id = t.workspace_id
where r.state = 'running' and t.requires_filesystem = 1
`).all() as Array<{ title: string; local_path: string }>;
for (const writer of writers) {
const other = normalizeDirectory(writer.local_path);
if (mine === other || mine.startsWith(`${other}/`) || other.startsWith(`${mine}/`)) return { title: writer.title };
}
return null;
}
function normalizeDirectory(localPath: string): string {
let existing = resolve(localPath);
const missing: string[] = [];
while (!existsSync(existing) && dirname(existing) !== existing) {
missing.unshift(basename(existing));
existing = dirname(existing);
}
const real = existsSync(existing) ? realpathSync.native(existing) : existing;
const path = join(real, ...missing).replace(/\\/g, "/").replace(/\/+$/, "");
return process.platform === "win32" ? path.toLowerCase() : path;
}