#!/usr/bin/env bun import * as fs from 'node:fs'; import * as path from 'node:path'; import { discoverTemplates, includesSkill } from './discover-skills'; import { externalSkillName, extractNameAndDescription } from './external-skill-names'; import { getHostConfig } from '../hosts'; const args = process.argv.slice(2); const value = (flag: string): string => { const index = args.indexOf(flag); if (index < 0 || !args[index + 1]) throw new Error(`missing ${flag}`); return args[index + 1]; }; const exists = (file: string) => fs.lstatSync(file, { throwIfNoEntry: false }); const inside = (file: string, root: string) => file === root || file.startsWith(`${root}${path.sep}`); const refuse = (operation: string, target: string) => { throw new Error(`Refusing: Codex ${operation} overlaps source or escapes its namespace: ${target}`); }; const physical = (file: string): string => { let ancestor = path.resolve(file); const suffix: string[] = []; const visited = new Set(); while (true) { if (exists(ancestor)) { try { return path.join(fs.realpathSync(ancestor), ...suffix); } catch (error) { if (!exists(ancestor)?.isSymbolicLink() || visited.has(ancestor)) throw error; visited.add(ancestor); ancestor = path.resolve(path.dirname(ancestor), fs.readlinkSync(ancestor)); continue; } } const parent = path.dirname(ancestor); if (parent === ancestor) throw new Error(`unresolvable path: ${file}`); suffix.unshift(path.basename(ancestor)); ancestor = parent; } }; const source = fs.realpathSync(value('--source')); const generation = path.join(source, '.agents/skills'); const namespace = value('--namespace'); const selected = value('--selected') === '1'; const windows = value('--windows') === '1'; const local = value('--local') === '1'; const runtime = path.join(namespace, 'gstack'); const runtimeStat = exists(runtime); const migrating = selected && !local && !!runtimeStat && runtimeStat.isDirectory() && !runtimeStat.isSymbolicLink() && physical(runtime) === source; const relocated = migrating ? value('--relocation') : source; if (migrating && (exists(relocated) || inside(physical(relocated), source))) refuse('checkout relocation', relocated); const generationRoot = physical(generation); if (!inside(generationRoot, source) || generationRoot === source) refuse('generation namespace', generation); const physicalNamespace = physical(namespace); if (selected && inside(physicalNamespace, source) && (!local || physicalNamespace !== generationRoot)) refuse('host namespace', namespace); const checkOutput = (file: string, root: string, operation: string) => { if (!inside(physical(file), root)) refuse(operation, file); }; const checkAtomicCopy = (file: string, root: string, operation: string, detachesParent = false) => { const parent = path.dirname(file); const writeParent = detachesParent && exists(parent)?.isSymbolicLink() ? path.dirname(parent) : parent; const resolved = physical(writeParent); if (!inside(resolved, root) || (inside(resolved, source) && !inside(resolved, generationRoot))) { refuse(operation, file); } }; const checkPostRelocationAlias = (file: string) => { if (!migrating) return; let entry = file; while (inside(entry, source) && entry !== source) { if (exists(entry)?.isSymbolicLink() && path.isAbsolute(fs.readlinkSync(entry)) && inside(physical(entry), source)) { refuse(entry === path.join(source, '.agents') || entry === generation ? 'post-relocation generation namespace' : 'post-relocation generated alias', entry); } entry = path.dirname(entry); } }; const checkReplace = (file: string, operation: string) => { const stat = exists(file); if (!stat || stat.isSymbolicLink() || !stat.isDirectory()) return; if (inside(source, physical(file))) { if ((migrating || local) && file === runtime && physical(file) === source) return; refuse(operation, file); } }; const userOwnedRoot = (root: string): boolean => { const stat = exists(root); const skill = path.join(root, 'SKILL.md'); return !!stat && stat.isDirectory() && !stat.isSymbolicLink() && !!fs.statSync(skill, { throwIfNoEntry: false })?.isFile() && !fs.readFileSync(skill, 'utf8').includes('