diff --git a/README.md b/README.md index f8ec838..b58a706 100644 --- a/README.md +++ b/README.md @@ -95,9 +95,10 @@ In Cosmo Code the task's ask rules apply in every other mode and are lifted in A would make even Auto ask. Automatic answers are recorded like the candidate's own. **A task with its own web app.** The platform previews only port 3000, which the simulation -uses. With `"app": { "port": 5173 }`, the shell gets an App pane beside the agent showing the -app, and the server passes every path outside its own (`/` and `/_cosmo/…`) to the app's dev -server on that port, WebSockets (hot reload) included. The task's side: +uses. With `"app": { "port": 5173 }`, the shell gets an App pane showing the app, beside the +agent when there's room (1200px or wider) and in its place otherwise, switched from the header. +The server passes every path outside its own (`/` and `/_cosmo/…`) to the app's dev server on +that port, WebSockets (hot reload) included. The task's side: - Serve the app under `/app/` on that port (Vite: `base: "/app/"`, `server.port` and `preview.port` the task's, `strictPort`). Absolute paths the app's code uses without the base @@ -125,6 +126,11 @@ downloads the release for the channel (`APP_ENVIRONMENT`), unpacks it in the ses they stop; 3. returns once the server on :3000 answers. +The workspace is a git repository, which the agents' change views compare against. The task's +own files, installed dependencies (`.venv`, `node_modules`) and build output (`dist`, `.cache`) +are git-excluded, and once the task's setup has finished (`/tmp/.setup_finished`) the +supervisor commits the workspace as "Starting point", so changes are the candidate's own. + [`.codesignal/README.md`](.codesignal/README.md) has the base task's settings. `OPENAI_BASE_URL`/`OPENAI_API_KEY` and `ANTHROPIC_BASE_URL`/`ANTHROPIC_API_KEY` must be set for the engines the task enables (the LLM proxy sets them); setup fails rather than let an engine diff --git a/client/app.js b/client/app.js index da6fa0f..7d12609 100644 --- a/client/app.js +++ b/client/app.js @@ -11,6 +11,7 @@ const switcherEl = document.getElementById('agent-switcher'); const statusEl = document.getElementById('agent-status'); const frames = new Map(); let session = null; +let appPane = null; let helpModal = null; function showMessage(text) { @@ -74,6 +75,7 @@ async function switchTo(agentId) { } session = body; show(session.active); + appPane?.showAgent(); } catch (error) { setStatus('Could not switch agents', 'error'); } @@ -119,7 +121,11 @@ function remember(key, value) { } } -/** The App pane: the task's web app beside the agent, resizable, opened from the header. */ +// Narrower than this, the agent UIs have no room beside the app, which then takes their place. +const SPLIT_MIN_WIDTH = 1200; +const AGENT_MIN_WIDTH = 640; + +/** The App pane: the task's web app beside the agent (or instead of it when narrow), opened from the header. */ function setupAppPane(app) { const button = document.createElement('button'); button.className = 'button button-text'; @@ -136,7 +142,7 @@ function setupAppPane(app) { }); const setWidth = (px) => { - const max = workspace.clientWidth - 320; + const max = workspace.clientWidth - AGENT_MIN_WIDTH; const width = Math.max(240, Math.min(max, px)); pane.style.setProperty('--app-width', `${width}px`); divider.setAttribute('aria-valuenow', String(Math.round(width))); @@ -145,17 +151,24 @@ function setupAppPane(app) { const saved = Number(remember('cosmo.app.width')); if (saved) pane.style.setProperty('--app-width', `${saved}px`); - const setOpen = (open) => { + let open = false; + const narrow = () => workspace.clientWidth < SPLIT_MIN_WIDTH; + const layout = () => { pane.hidden = !open; - divider.hidden = !open; - button.textContent = open ? 'Hide app' : 'Show app'; - button.setAttribute('aria-pressed', String(open)); + divider.hidden = !open || narrow(); + workspace.classList.toggle('app-only', open && narrow()); + button.textContent = open ? (narrow() ? 'Show agent' : 'Hide app') : 'Show app'; + }; + const setOpen = (value) => { + open = value; if (open && !frame.getAttribute('src')) frame.src = app.path; remember('cosmo.app.open', open ? '1' : '0'); + layout(); }; - button.addEventListener('click', () => setOpen(pane.hidden)); - setOpen(remember('cosmo.app.open') === '1'); - + new ResizeObserver(layout).observe(workspace); + button.addEventListener('click', () => setOpen(!open)); + // Where the app would hide the agent, a reload starts on the agent. + setOpen(remember('cosmo.app.open') === '1' && !narrow()); divider.addEventListener('pointerdown', (event) => { event.preventDefault(); divider.setPointerCapture(event.pointerId); @@ -177,6 +190,11 @@ function setupAppPane(app) { event.preventDefault(); remember('cosmo.app.width', String(setWidth(pane.getBoundingClientRect().width + step))); }); + return { + showAgent: () => { + if (open && narrow()) setOpen(false); + }, + }; } async function initialize() { @@ -197,7 +215,7 @@ async function initialize() { } renderSwitcher(); show(session.active); - if (session.app) setupAppPane(session.app); + if (session.app) appPane = setupAppPane(session.app); } if (document.readyState === 'loading') { diff --git a/client/shell.css b/client/shell.css index 818b426..809dbfa 100644 --- a/client/shell.css +++ b/client/shell.css @@ -51,13 +51,22 @@ .app-pane { display: flex; - flex: 0 0 var(--app-width, 45%); + flex: 0 0 var(--app-width, 40%); flex-direction: column; min-width: 240px; - max-width: calc(100% - 320px); + max-width: calc(100% - 640px); background: var(--Colors-Backgrounds-Main-Default); } +.workspace.app-only .agent-stage { + display: none; +} + +.workspace.app-only .app-pane { + flex: 1; + max-width: none; +} + .app-divider[hidden], .app-pane[hidden] { display: none; diff --git a/root/supervisor.mjs b/root/supervisor.mjs index b236bc0..c12a23e 100644 --- a/root/supervisor.mjs +++ b/root/supervisor.mjs @@ -109,21 +109,61 @@ function which(bin) { throw new Error(`${bin} is not installed`); } +/** Options to run a shell in the workspace as the session user. */ +function asSessionUser() { + if (USER_MODE) return { cwd: WORKSPACE, env: { PATH: "/usr/bin:/bin", HOME: os.homedir() } }; + const acct = account(SESSION_USER); + return { cwd: WORKSPACE, uid: acct.uid, gid: acct.gid, env: { PATH: "/usr/bin:/bin", HOME: acct.home } }; +} + +const inWorkspace = (script) => spawnSync("sh", ["-c", script], asSessionUser()); + /** * The workspace as a git repository (engines show changes against it), with the task's own - * files (.codesignal, main.sh) and installed dependencies (a Python .venv, node_modules) in the - * repo-local exclude list, so they stay out of change lists and file search (the OpenCode gateway - * also filters its search results; cosmetic: the agent can still read and use them). + * files (.codesignal, main.sh), installed dependencies (.venv, node_modules) and build output + * (dist, .cache) in the repo-local exclude list, so they stay out of change lists and file search + * (the OpenCode gateway also filters its search results; cosmetic: the agent can still use them). */ function gitWorkspace() { - const script = [ + inWorkspace([ "[ -d .git ] || git init -q", 'f=$(git rev-parse --git-path info/exclude) && mkdir -p "$(dirname "$f")"', - ...["/.codesignal/", "/main.sh", "/.venv/", "/node_modules/"].map((p) => `{ grep -qxF "${p}" "$f" 2>/dev/null || echo "${p}" >> "$f"; }`), - ].join(" && "); - if (USER_MODE) return spawnSync("sh", ["-c", script], { cwd: WORKSPACE, env: { PATH: "/usr/bin:/bin", HOME: os.homedir() } }); - const acct = account(SESSION_USER); - spawnSync("sh", ["-c", script], { cwd: WORKSPACE, uid: acct.uid, gid: acct.gid, env: { PATH: "/usr/bin:/bin", HOME: acct.home } }); + ...["/.codesignal/", "/main.sh", "/.venv/", "/node_modules/", "/dist/", "/.cache/"].map((p) => `{ grep -qxF "${p}" "$f" 2>/dev/null || echo "${p}" >> "$f"; }`), + ].join(" && ")); +} + +// The platform's task setup writes this last. +const SETUP_FINISHED = "/tmp/.setup_finished"; +// Done once per session: after a supervisor restart, the workspace holds the candidate's work. +const STARTING_POINT_DONE = `${STATE_DIR}/starting-point`; + +// A task's own history is kept; the commit is made only when setup changed something. +const STARTING_POINT_SCRIPT = + "git rev-parse --is-inside-work-tree >/dev/null 2>&1 || exit 0; git add -A || exit 1; " + + "git rev-parse -q --verify HEAD >/dev/null && git diff --cached --quiet && exit 0; " + + "git -c user.name=Workspace -c user.email=workspace@localhost -c commit.gpgsign=false -c core.hooksPath=/dev/null " + + 'commit -q --no-verify --allow-empty -m "Starting point"'; + +function commitStartingPoint(attempt = 1) { + if (fs.existsSync(STARTING_POINT_DONE)) return; + if (!fs.existsSync(SETUP_FINISHED)) return void setTimeout(commitStartingPoint, 250); + const child = spawn("sh", ["-c", STARTING_POINT_SCRIPT], { ...asSessionUser(), stdio: ["ignore", "ignore", "pipe"], timeout: 60_000 }); + let stderr = ""; + let settled = false; + const settle = (status, error) => { + if (settled) return; + settled = true; + if (status === 0) { + fs.writeFileSync(STARTING_POINT_DONE, ""); + return log("starting-point", { attempt }); + } + log("starting-point-failed", { attempt, status, error }); + // A git lock held by an agent's command is usually gone a moment later. + if (attempt < 5) setTimeout(() => commitStartingPoint(attempt + 1), 2000 * attempt); + }; + child.stderr.on("data", (d) => (stderr = (stderr + d).slice(-300))); + child.on("error", (e) => settle(null, e.message)); + child.on("close", (status) => settle(status, stderr.trim())); } // The task's own files in the workspace: an agent may read them but never change them. @@ -208,6 +248,7 @@ export function services({ policy, engineEnv, codexArgs = [], userMode = USER_MO cwd: WORKSPACE, env: { ...engineEnv.codex, ...proxy }, port: CODEX_PORT, + prepare: gitWorkspace, }); } if (policy.agents.includes("cosmo-code")) { @@ -349,6 +390,7 @@ function supervise(list) { fs.writeFileSync(PID_FILE, `${process.pid}\n`); list.forEach(start); setInterval(() => checkPorts(list), CHECK_MS); + commitStartingPoint(); const stop = () => { for (const svc of list) if (svc.child) try { process.kill(-svc.child.pid, "SIGTERM"); } catch { /* gone */ } process.exit(0); diff --git a/server/gateways/opencode.mjs b/server/gateways/opencode.mjs index 954cb81..8741a8c 100644 --- a/server/gateways/opencode.mjs +++ b/server/gateways/opencode.mjs @@ -14,11 +14,11 @@ const PERMISSION_REPLY = [ /^\/api\/session\/[^/]+\/permission\/[^/]+\/reply$/, ]; -// File and text search (v1 and v2 API). What the supervisor git-excludes (the task's own files and -// installed dependencies) stays out of their results; OpenCode 1.19 no longer reads git's exclude -// file for its search. +// File and text search (v1 and v2 API). What the supervisor git-excludes (the task's own files, +// installed dependencies, build output) stays out of their results; OpenCode 1.19 no longer reads +// git's exclude file for its search. const SEARCH = ["/find/file", "/find", "/api/fs/find"]; -const EXCLUDED = /^(\.codesignal(\/|$)|main\.sh$|\.venv(\/|$)|node_modules(\/|$))/; +const EXCLUDED = /^(\.codesignal(\/|$)|main\.sh$|(\.venv|node_modules|dist|\.cache)(\/|$))/; /** A search result without git-excluded paths: path lists, text matches or v2 entries. */ export function withoutExcluded(result, root) { diff --git a/test/container/run.sh b/test/container/run.sh index de41f05..d2501f8 100644 --- a/test/container/run.sh +++ b/test/container/run.sh @@ -172,6 +172,10 @@ else echo "SKIP E1-3 transcript extraction (set PROVISIONER_BUNDLE to the provisioner's bundle)" fi +# Last, as it commits the workspace: once the task's setup is done, its state is the starting point. +check W1 "setup's end commits the workspace as the starting point, without excluded files; later edits are changes" \ + as_runner 'touch /tmp/.setup_finished; sleep 2; [ "$(git log -1 --format=%s)" = "Starting point" ] && [ -z "$(git status --porcelain)" ] && [ "$(git ls-files .codesignal main.sh node_modules dist | wc -l)" -eq 0 ] && echo w1 >> w1-probe.txt && git status --porcelain | grep -q w1-probe.txt' + echo if [ "$FAILS" -eq 0 ]; then echo "all checks passed"; else echo "$FAILS check(s) failed"; fi [ "$KEEP" = "--keep" ] || docker rm -f "$NAME" >/dev/null diff --git a/test/opencode-gateway.test.mjs b/test/opencode-gateway.test.mjs index d7cfbdd..bf55139 100644 --- a/test/opencode-gateway.test.mjs +++ b/test/opencode-gateway.test.mjs @@ -135,11 +135,12 @@ test("permission answers are logged with the reply and reach the engine with the assert.deepEqual(events.map((e) => [e.type, e.reply]), Array(3).fill(["permission-reply", "once"])); }); -test("search results leave out what git excludes (.codesignal, main.sh, .venv, node_modules)", async (t) => { +test("search results leave out what git excludes (task files, dependencies, build output)", async (t) => { const root = "/usercode/FILESYSTEM"; assert.deepEqual(withoutExcluded([".codesignal/agent-sim.json", "main.sh", "notes/main.sh", "src/a.py", ".codesignal"], root), ["notes/main.sh", "src/a.py"]); assert.deepEqual(withoutExcluded([".venv/lib/x.py", ".venv", "venv-notes.md", "src/.venv.txt"], root), ["venv-notes.md", "src/.venv.txt"]); assert.deepEqual(withoutExcluded(["node_modules/vite/index.js", "src/node_modules.md"], root), ["src/node_modules.md"]); + assert.deepEqual(withoutExcluded(["dist/index.html", ".cache/x.tsbuildinfo", "src/dist.ts"], root), ["src/dist.ts"]); assert.deepEqual(withoutExcluded([{ path: { text: `${root}/.codesignal/x` } }, { path: { text: "a.py" } }], root), [{ path: { text: "a.py" } }]); assert.deepEqual(withoutExcluded({ location: {}, data: [{ path: "main.sh", type: "file" }, { path: "b.py", type: "file" }] }, root).data, [{ path: "b.py", type: "file" }]); const { base, seen, close } = await setup({ reply: () => JSON.stringify([".codesignal/agent-sim.json", "notes/agent-sim.md"]) }); diff --git a/test/stock/run.sh b/test/stock/run.sh index 4ff2a3d..4e6adf2 100755 --- a/test/stock/run.sh +++ b/test/stock/run.sh @@ -54,5 +54,7 @@ docker exec "$NAME" sh -c 'for p in /v1/messages /v1/responses; do if grep "\"ur docker exec "$NAME" sh -c 'sleep 3; for e in claude codex; do f=$(ls /usercode/FILESYSTEM/.codesignal/$e/interactions_summary.*.jsonl 2>/dev/null | head -1); t=$(ls /usercode/FILESYSTEM/.codesignal/$e/timeline.*.part1.txt 2>/dev/null | head -1); if [ -n "$f" ] && [ -n "$t" ] && grep -q "\"kind\": *\"session\"" "$f" && grep -q "\"input_text\"" "$f" && grep -q "USER:" "$t"; then echo "PASS L-$e the transcript record has sessions, prompts and a timeline"; else echo "FAIL L-$e no transcript record in .codesignal/$e"; fi; done' # The task's own web app (app.port 5173): every path outside the simulation's own reaches it. docker exec -d -u runner "$NAME" node -e 'require("http").createServer((q, s) => s.end("APP " + q.url)).listen(5173)' -docker exec "$NAME" sh -c 'sleep 1; a=$(curl -s localhost:3000/app/x); d=$(curl -s localhost:3000/data/lots.json); h=$(curl -s -o /dev/null -w %{http_code} localhost:3000/_cosmo/api/health); r=$(curl -s localhost:3000/ | grep -c "/_cosmo/assets/"); if [ "$a" = "APP /app/x" ] && [ "$d" = "APP /app/data/lots.json" ] && [ "$h" = 200 ] && [ "$r" -ge 1 ]; then echo "PASS A1 a task app gets every path outside the simulation'"'"'s own"; else echo "FAIL A1 task app routing: $a | $d | $h | $r"; fi' +docker exec "$NAME" sh -c 'sleep 1; a=$(curl -s localhost:3000/app/x); d=$(curl -s localhost:3000/data/lots.json); h=$(curl -s -o /dev/null -w %{http_code} localhost:3000/_cosmo/api/health); r=$(curl -s localhost:3000/ | grep -c "/_cosmo/assets/"); if [ "$a" = "APP /app/x" ] && [ "$d" = "APP /app/data/lots.json" ] && [ "$h" = 200 ] && [ "$r" -ge 1 ]; then echo "PASS A1 a task app gets every path outside the simulation'"'"'s own"; else echo "FAIL A1 task app routing: $a | $d | $h | $r"; fi' || echo "FAIL A1 the routing check could not run" +# Once the task's setup is done, the workspace is committed as the starting point (changes are the candidate's). +docker exec -u runner "$NAME" sh -c 'touch /tmp/.setup_finished; sleep 2; cd /usercode/FILESYSTEM; if [ "$(git log -1 --format=%s)" = "Starting point" ] && [ -z "$(git status --porcelain)" ] && [ "$(git ls-files .codesignal main.sh node_modules dist | wc -l)" -eq 0 ] && echo w1 >> w1-probe.txt && git status --porcelain | grep -q w1-probe.txt; then echo "PASS W1 setup'"'"'s end commits the workspace as the starting point; later edits are changes"; else echo "FAIL W1 starting point: $(git log -1 --format=%s 2>&1), $(git status --porcelain | wc -l) changes, $(git ls-files .codesignal main.sh node_modules dist | wc -l) excluded files tracked"; fi' || echo "FAIL W1 the starting-point check could not run" echo "http://localhost:$PORT/"