/** * @file Cleans up what older builds wrote into a lane: `removeStageDoc` takes * AgentHub's stage block back out of a codex lane's AGENTS.md. Plain file I/O, * so never in an S3 member's folder. * @author Nguyễn Ngọc Trí Vĩ */ const fs = require("node:fs"); const path = require("node:path"); // The block older builds wrote into a codex lane's AGENTS.md. Codex lanes no // longer carry a pipeline, so `removeStageDoc` takes it back out. const STAGE_BLOCK_START = ""; const STAGE_BLOCK_END = ""; /** * Remove AgentHub's stage block from `/AGENTS.md`, leaving every other * line as it was. A file left with nothing else in it was AgentHub's own * creation, so it is deleted. Never throws. * * @returns {boolean} whether the file changed */ function removeStageDoc(cwd) { const file = path.join(cwd, "AGENTS.md"); try { // S3: never in a member's folder — the host would read, rewrite or delete // through any link the member planted there (with CAP_DAC_READ_SEARCH). // Builds that wrote the block predate S3; a member removes it themselves. if (require("./run-as").ownerForRead(cwd)) return false; const doc = fs.readFileSync(file, "utf8"); const start = doc.indexOf(STAGE_BLOCK_START); const end = doc.indexOf(STAGE_BLOCK_END, start); if (start === -1 || end === -1) return false; const rest = ( doc.slice(0, start).trimEnd() + "\n\n" + doc.slice(end + STAGE_BLOCK_END.length).trimStart() ).trim(); if (rest) fs.writeFileSync(file, `${rest}\n`); else fs.rmSync(file); return true; } catch { return false; // no file, or unreadable — nothing of ours to take out } } module.exports = { removeStageDoc };