Repository navigation
App pane | Fit the platform's preview; changes start after setup #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1fbbe1e
09d2e5d
7772f25
7c7d181
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| 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))); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard the stderr stream on spawn failure. If Install the error handler first and attach the data handler only when
|
||
| 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); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Qualify the repository guarantee.
For a Cosmodex-only task with no existing Git repository, the supervisor does not call
gitWorkspace(). It also skips the starting commit because the workspace is not a worktree. Document this condition rather than stating that every workspace is a Git repository. (raw.githubusercontent.com)