// Pi/OpenAI. Prepare an isolated trial; never launches/reloads an existing session. import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'; import { dirname, join, resolve } from 'node:path'; import { fileURLToPath, pathToFileURL } from 'node:url'; import { execFileSync } from 'node:child_process'; import { homedir, tmpdir } from 'node:os'; const repo = resolve(dirname(fileURLToPath(import.meta.url)), '..'); const sdkRoot = process.argv[2]; const noSandbox = process.argv.includes('--no-sandbox'); if (!sdkRoot) throw new Error('Usage: node scripts/prepare-trial.mjs INSTALLED_PI_ROOT'); const revision = execFileSync('git', ['-C', repo, 'rev-parse', 'HEAD'], {encoding:'utf8'}).trim(); const root = mkdtempSync(join(tmpdir(), 'goals-edxeth-trial-')); const cwd = join(root, 'project'); const agentDir = join(root, 'agent'); mkdirSync(cwd); mkdirSync(agentDir, {mode:0o700}); mkdirSync(join(agentDir,'agents')); const sourceAgent = process.env.PI_CODING_AGENT_DIR || join(homedir(),'.pi','agent'); const sourceSettings = JSON.parse(readFileSync(join(sourceAgent,'settings.json'),'utf8')); const sdk = await import(pathToFileURL(join(sdkRoot,'dist/index.js')).href); const settings = sdk.SettingsManager.create(repo, sourceAgent, { projectTrusted:false }); const manager = new sdk.DefaultPackageManager({cwd:repo, agentDir:sourceAgent, settingsManager:settings}); const packages = manager.listConfiguredPackages().filter((p) => p.scope !== 'project' && !/^\/\//.test(p.source)); const retained = packages.filter((p) => !/pi-subagents|pi-goals|pi-intercom|pi-schedule-prompt/.test(p.source)); for (const p of retained) if (!p.installedPath) throw new Error(`Missing installed package: ${p.source}`); writeFileSync(join(agentDir,'settings.json'), JSON.stringify({...sourceSettings, packages:[...retained.map((p)=>p.installedPath), repo]},null,2)); // Private copies, not symlinks: a trial OAuth refresh must not write the active auth file. for (const file of ['auth.json','models.json']) if (existsSync(join(sourceAgent,file))) copyFileSync(join(sourceAgent,file),join(agentDir,file)); const workerDefinition = readFileSync(join(repo,'agents/goals-worker.md'),'utf8'); writeFileSync(join(agentDir,'agents/goals-worker.md'), noSandbox ? workerDefinition.replace('mode: interactive', 'mode: interactive\nflags: --no-sandbox') : workerDefinition); execFileSync('git',['init','--quiet',cwd]); writeFileSync(join(cwd,'AGENTS.md'), 'Isolated functional trial. Work only in this project. Do not operate other Herdr panes, use live research sessions, or change global settings. Preserve evidence. The main chat supervises; the goals-worker implements.\n'); writeFileSync(join(cwd,'.gitignore'), 'evidence/\n'); const manifest={root,cwd,agentDir,repo,revision,noSandbox,retainedPackages:retained.map((p)=>p.source),replacedPackages:packages.filter((p)=>!retained.includes(p)).map((p)=>p.source)}; writeFileSync(join(root,'manifest.json'),JSON.stringify(manifest,null,2)); const quote=(s)=>`'${s.replaceAll("'", "'\\''")}'`; writeFileSync(join(root,'start.zsh'), `#!/usr/bin/env zsh\nset -e\ncd ${quote(cwd)}\nexport PI_CODING_AGENT_DIR=${quote(agentDir)}\nexport PI_SUBAGENT_MUX=herdr\nexport PI_ORCHESTRATOR_MODE=0\nexec pi --approve${noSandbox ? ' --no-sandbox' : ''}\n`,{mode:0o700}); console.log(JSON.stringify({root,cwd,agentDir,start:join(root,'start.zsh'),manifest:join(root,'manifest.json')},null,2));