Multiplayer agent harness for work.
Read goal evidence files by absolute path and say why one can't be read (#2560)
Joshua Franced04682911579
diff --git a/src/harness/agent-tools.ts b/src/harness/agent-tools.tsindex 789f6d2c6..36a08a770 100644--- a/src/harness/agent-tools.ts+++ b/src/harness/agent-tools.ts@@ -377,6 +377,9 @@ export function pauseStampAfterToolCall(};}+const outsideWorkspace = (path: string): string =>+ `[not in the computer's workspace: ${path}; only workspace files can be read, so copy it there or name it relative to the workspace]`;+export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions): ToolDefinition[] {const scratchExec = !!opts?.scratchExec;const ownerAuthExec = !!opts?.ownerAuthExec;@@ -1154,7 +1157,12 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):});const signal = ref.abortSignal;signal?.throwIfAborted();- const { content, sourceScopeId, shared } = await tc.read(params.path, signal, params.sandbox_id);+ const {+ content,+ sourceScopeId,+ shared,+ outsideWorkspace: outside,+ } = await tc.read(params.path, signal, params.sandbox_id);signal?.throwIfAborted();return recordResult(callId,@@ -1165,7 +1173,7 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):found: content !== null,...(content !== null ? { bytes: content.length, sourceScopeId } : {}),},- text(content ?? `[no such file: ${params.path}]`),+ text(content ?? (outside ? outsideWorkspace(params.path) : `[no such file: ${params.path}]`)),content === null,sourceScopeId,false,@@ -3889,11 +3897,11 @@ export function createAgentTools(ref: ToolContextRef, opts?: AgentToolsOptions):for (const path of (paths ?? []).slice(0, GOAL_EVIDENCE_FILES)) {const content = tc? await tc.read(path, signal, sandboxId).then(- (r) => r.content,- () => null,+ (r) => r.content ?? (r.outsideWorkspace ? outsideWorkspace(path) : "[missing: no such file]"),+ (e: unknown) => `[unreadable: ${errMessage(e)}]`,)- : null;- let body = content ?? "[missing: no such file]";+ : "[missing: no such file]";+ let body = content;if (body.length > GOAL_EVIDENCE_FILE_CHARS)body = `${body.slice(0, GOAL_EVIDENCE_FILE_CHARS)}\n[truncated at ${GOAL_EVIDENCE_FILE_CHARS} of ${body.length} chars]`;out.push(`<file path="${path.replace(/"/g, "")}">\n${body}\n</file>`);diff --git a/src/sandbox/exec-file-ops.ts b/src/sandbox/exec-file-ops.tsindex 0e24588b8..8e813bd9a 100644--- a/src/sandbox/exec-file-ops.ts+++ b/src/sandbox/exec-file-ops.ts@@ -1,4 +1,5 @@import { randomBytes } from "node:crypto";+import { posix } from "node:path";import { shq } from "../util/shell.ts";import { swallow, swallowAs } from "../util/errors.ts";import { makeTar, parseTar } from "./tar.ts";@@ -27,8 +28,13 @@ function workspaceRel(rel: string): string {return rel.replace(/^\/+/, "");}+export function workspacePathOf(rootDir: string, absPath: string): string | null {+ const rel = posix.relative(posix.normalize(rootDir), posix.normalize(absPath));+ return rel && !rel.startsWith("..") && !posix.isAbsolute(rel) ? rel : null;+}+export function posixJoin(base: string, rel: string): string {- const clean = workspaceRel(rel);+ const clean = workspaceRel((rel.startsWith("/") && workspacePathOf(base, rel)) || rel);return clean ? `${base.replace(/\/+$/, "")}/${clean}` : base;}diff --git a/src/tools/primitives.ts b/src/tools/primitives.tsindex 3d96ce676..c77785b8b 100644--- a/src/tools/primitives.ts+++ b/src/tools/primitives.ts@@ -17,6 +17,7 @@ import { type TurnContext } from "../resolution/turn-context.ts";import { canReadNotebook, readNotebook, searchNotebooks } from "../memory/cross-scope.ts";import type { SandboxAccessPlan, SandboxResource, SandboxResources } from "../sandbox/sandbox-resources.ts";import { join } from "node:path";+import { workspacePathOf } from "../sandbox/exec-file-ops.ts";import { interpolateSplitEnv } from "../deployment/deployment-layer.ts";import type { CredentialPathSpec } from "../credentials/resident-paths.ts";import type { ComputerStatus, ExecResult, Sandbox, SandboxHandle } from "../sandbox/sandbox.ts";@@ -154,6 +155,7 @@ interface ReadResult {content: string | null;sourceScopeId: ScopeId | null;shared?: true;+ outsideWorkspace?: true;}export interface SkillResult {@@ -1007,11 +1009,13 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {signal?.throwIfAborted();const { handle, resource, crossScope } = await machine(sandboxId);const mounts = crossScope ? fallbackMounts.filter((mount) => mount.mountPath === "global") : fallbackMounts;+ const rel = path.startsWith("/") ? workspacePathOf(handle.rootDir, path) : path;+ if (rel === null) return { content: null, sourceScopeId: null, outsideWorkspace: true };return timed("file_op", async () => {- const direct = await withAbort(() => deps.sandbox.readFile(handle, path), signal);+ const direct = await withAbort(() => deps.sandbox.readFile(handle, rel), signal);if (direct !== null) return { content: direct, sourceScopeId: resource?.ownerScopeId ?? writableScopeId };for (const mount of mounts) {- const v = await withAbort(() => deps.sandbox.readFile(handle, join(mount.mountPath, path)), signal);+ const v = await withAbort(() => deps.sandbox.readFile(handle, join(mount.mountPath, rel)), signal);if (v !== null) return { content: v, sourceScopeId: mount.scopeId };}return { content: null, sourceScopeId: null };@@ -1070,11 +1074,16 @@ export function createToolContext(deps: ToolContextDeps): ToolContext {const { handle, resource, crossScope, durableRef } = await machine(sandboxId);if (crossScope) throw new Error("files write needs a computer owned by this conversation");const ownerScopeId = resource?.ownerScopeId ?? writableScopeId;+ const rel = path.startsWith("/") ? workspacePathOf(handle.rootDir, path) : path;+ if (rel === null)+ throw new Error(+ `${path} is not in the computer's workspace; only workspace files can be written, so name it relative to the workspace`,+ );await once(() =>timed("file_op", async () => {- await deps.sandbox.writeFile(handle, path, data);- if (ownerScopeId && !isUnderAnyDir(path, persistExclude))- await deps.workspace.write(ownerScopeId, durableRef(path), data);+ await deps.sandbox.writeFile(handle, rel, data);+ if (ownerScopeId && !isUnderAnyDir(rel, persistExclude))+ await deps.workspace.write(ownerScopeId, durableRef(rel), data);}),);},diff --git a/test/exec-file-ops.test.ts b/test/exec-file-ops.test.tsindex 3a1c2a606..536579010 100644--- a/test/exec-file-ops.test.ts+++ b/test/exec-file-ops.test.ts@@ -5,7 +5,7 @@ import { tmpdir } from "node:os";import { join } from "node:path";import { execFile } from "node:child_process";import { promisify } from "node:util";-import { createExecFileOps, posixJoin } from "../src/sandbox/exec-file-ops.ts";+import { createExecFileOps, posixJoin, workspacePathOf } from "../src/sandbox/exec-file-ops.ts";const exec = promisify(execFile);@@ -78,6 +78,21 @@ test("posixJoin rejects parent path segments", () => {assert.throws(() => posixJoin("/root/workspace", "a/../../x"), /must stay inside the workspace/);});+test("posixJoin resolves an absolute path inside the workspace instead of nesting it", () => {+ assert.equal(posixJoin("/root/workspace", "/root/workspace/a/b.txt"), "/root/workspace/a/b.txt");+ assert.equal(posixJoin("/root/workspace/", "/root/workspace/a.txt"), "/root/workspace/a.txt");+ assert.equal(posixJoin("/root/workspace", "/root/workspaces/a.txt"), "/root/workspace/root/workspaces/a.txt");+ assert.throws(() => posixJoin("/root/workspace", "/root/workspace/../x"), /must stay inside the workspace/);+});++test("workspacePathOf maps only paths that normalize into the workspace", () => {+ assert.equal(workspacePathOf("/root/workspace/", "/root/workspace/./a/b.txt"), "a/b.txt");+ assert.equal(workspacePathOf("/root/workspace", "/root/workspaces/a.txt"), null);+ assert.equal(workspacePathOf("/root/workspace", "/root/workspace/../x"), null);+ assert.equal(workspacePathOf("/root/workspace", "/root/workspace"), null);+ assert.equal(workspacePathOf("/", "/etc/x"), "etc/x");+});+test("concurrent imports keep separate archives through extraction and cleanup", { timeout: 10_000 }, async () => {const rootDir = await mkdtemp(join(tmpdir(), "qm-import-'"));const handle = { id: "test", rootDir };diff --git a/test/goal-tools.test.ts b/test/goal-tools.test.tsindex c8e7c76c3..f8d352b0f 100644--- a/test/goal-tools.test.ts+++ b/test/goal-tools.test.ts@@ -385,6 +385,44 @@ test("update reads named workspace files into the governor's evidence", async ()assert.equal(ref.goal?.status, "complete");});+test("update reads evidence from the named sandbox and says why a file could not be read", async () => {+ const { ref, create, update } = toolbox();+ const reads: Array<[string, string | undefined]> = [];+ ref.current = {+ read: async (path: string, _signal: AbortSignal | undefined, sandboxId?: string) => {+ reads.push([path, sandboxId]);+ if (sandboxId === "gone") throw new Error("sandbox not found: gone");+ if (path === "report.md") return { content: "on sb1", sourceScopeId: null };+ return { content: null, sourceScopeId: null, ...(path.startsWith("/home") ? { outsideWorkspace: true } : {}) };+ },+ } as unknown as ToolContextRef["current"];+ let seen = "";+ ref.governGoal = async (input) => {+ seen = input.evidence ?? "";+ return { verdict: "continue", reasons: "not yet" };+ };+ await create.execute("c1", { objective: "write the report" });+ await update.execute("u1", {+ status: "complete",+ note: "done",+ files: ["report.md", "/home/user/code.txt", "typo.md"],+ sandbox_id: "sb1",+ });+ assert.deepEqual(reads, [+ ["report.md", "sb1"],+ ["/home/user/code.txt", "sb1"],+ ["typo.md", "sb1"],+ ]);+ assert.match(seen, /<file path="report.md">\non sb1\n<\/file>/);+ assert.match(+ seen,+ /<file path="\/home\/user\/code.txt">\n\[not in the computer's workspace: \/home\/user\/code.txt; only workspace files/,+ );+ assert.match(seen, /<file path="typo.md">\n\[missing: no such file\]/);+ await update.execute("u2", { status: "complete", note: "done", files: ["report.md"], sandbox_id: "gone" });+ assert.match(seen, /<file path="report.md">\n\[unreadable: sandbox not found: gone\]/);+});+function nextTurn(goal: GoalRecord) {const box = toolbox();box.ref.goal = openGoal(structuredClone(goal));diff --git a/test/selected-sandbox-files.test.ts b/test/selected-sandbox-files.test.tsindex 5d9af5d2a..e11bfce99 100644--- a/test/selected-sandbox-files.test.ts+++ b/test/selected-sandbox-files.test.ts@@ -146,3 +146,25 @@ test("naming the default sandbox explicitly persists exactly like omitting it",await h.ctx.write("notes.md", "explicit", "sb1");assert.deepEqual(new Set(h.stored.map((e) => `${e.scope} ${e.path}`)), new Set([`${conversation} notes.md`]));});++test("an absolute path inside a computer's workspace reads and writes like its workspace path", async () => {+ const h = harness({ access: plan("sb1", conversation) });+ assert.equal((await h.ctx.read("/workspace/notes.md", undefined, "sb1")).content, "selected copy");+ assert.equal((await h.ctx.read("/workspace/./notes.md")).content, "default copy");+ await h.ctx.write("/workspace/notes.md", "edited", "sb1");+ assert.equal(h.disks.get("selected")!.get("notes.md"), "edited");+ assert.deepEqual(h.stored, [{ scope: conversation, path: "sandbox-files/sb1/notes.md" }]);+});++test("an absolute path outside the workspace reads nothing and says so", async () => {+ const h = harness({ access: plan("sb1", conversation) });+ for (const path of ["/elsewhere/notes.md", "/workspace/../etc/notes.md"])+ assert.deepEqual(await h.ctx.read(path, undefined, "sb1"), {+ content: null,+ sourceScopeId: null,+ outsideWorkspace: true,+ });+ assert.deepEqual(await h.ctx.read("/workspace/absent.md", undefined, "sb1"), { content: null, sourceScopeId: null });+ await assert.rejects(h.ctx.write("/etc/notes.md", "x", "sb1"), /not in the computer's workspace/);+ assert.equal(h.disks.get("selected")!.has("etc/notes.md"), false);+});