@radnine/claude-session-daemon 0.9.2 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/codex-process.js +360 -51
package/package.json
CHANGED
package/src/codex-process.js
CHANGED
|
@@ -22,6 +22,22 @@ const NO_CHATGPT_EXTRAS = ['apps', 'plugins', 'remote_plugin'];
|
|
|
22
22
|
// four; CLAUDE_SESSION_ID is the chat's own session id.
|
|
23
23
|
const RAD_ENV_VARS = ['CLAUDE_SESSION_ID', 'FLIGHTDECK_URL', 'RAD_URL', 'RAD_TOKEN'];
|
|
24
24
|
|
|
25
|
+
// Names an MCP entry's env map may not change in the child's env, because Codex
|
|
26
|
+
// itself, and every shell command a chat runs, runs with them. CLAUDE_SESSION_ID
|
|
27
|
+
// is the chat's own id. The proxy names match in any case, as tools read both.
|
|
28
|
+
const GUARDED_ENV_VARS = new Set(['PATH', 'HOME', 'CLAUDE_SESSION_ID', 'NODE_OPTIONS', 'SSL_CERT_FILE', 'SSL_CERT_DIR', 'NODE_EXTRA_CA_CERTS']);
|
|
29
|
+
const GUARDED_ENV_PREFIXES = ['CODEX_', 'OPENAI_', 'ANTHROPIC_', 'LD_', 'DYLD_'];
|
|
30
|
+
const PROXY_ENV_VARS = new Set(['HTTP_PROXY', 'HTTPS_PROXY', 'ALL_PROXY', 'NO_PROXY']);
|
|
31
|
+
|
|
32
|
+
function isGuardedEnvVar(name) {
|
|
33
|
+
return GUARDED_ENV_VARS.has(name)
|
|
34
|
+
|| PROXY_ENV_VARS.has(name.toUpperCase())
|
|
35
|
+
|| GUARDED_ENV_PREFIXES.some((prefix) => name.startsWith(prefix));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Codex server names become a dotted config key (mcp_servers.<name>.command).
|
|
39
|
+
const MCP_SERVER_NAME = /^[A-Za-z0-9_-]+$/;
|
|
40
|
+
|
|
25
41
|
/**
|
|
26
42
|
* Wraps one `codex app-server` child (JSON-RPC over stdio) for one FlightDeck
|
|
27
43
|
* chat, and keeps one Codex thread alive across turns. It follows the harness
|
|
@@ -47,6 +63,8 @@ class CodexProcess extends EventEmitter {
|
|
|
47
63
|
* @param {string} [options.effort] - reasoning effort for each turn
|
|
48
64
|
* @param {object} [options.sessionEnv] - extra env for the child
|
|
49
65
|
* @param {{command: string, args: string[], env?: object}} [options.radMcp] - how to start the rad MCP server
|
|
66
|
+
* @param {Array<{name: string, command: string, args: string[], env?: object}>} [options.mcpServers] - the other
|
|
67
|
+
* MCP servers; by default the stdio servers a Claude chat in workingDir gets (findMcpServers)
|
|
50
68
|
* @param {Function} [options._spawnFn] - tests: replacement for child_process.spawn
|
|
51
69
|
*/
|
|
52
70
|
constructor(sessionId, options = {}) {
|
|
@@ -64,6 +82,9 @@ class CodexProcess extends EventEmitter {
|
|
|
64
82
|
this._model = options.model || null;
|
|
65
83
|
this._effort = options.effort || null;
|
|
66
84
|
this._radMcp = options.radMcp;
|
|
85
|
+
this._mcpServers = options.mcpServers;
|
|
86
|
+
this._skillRoots = [];
|
|
87
|
+
this._memoryIndex = null;
|
|
67
88
|
|
|
68
89
|
this._state = 'idle';
|
|
69
90
|
this._child = null;
|
|
@@ -95,9 +116,20 @@ class CodexProcess extends EventEmitter {
|
|
|
95
116
|
|
|
96
117
|
this._state = 'spawning';
|
|
97
118
|
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
const
|
|
119
|
+
const baseEnv = buildCodexEnv(process.env, this._sessionEnv, this._sessionId);
|
|
120
|
+
baseEnv.CODEX_HOME = prepareCodexHome(baseEnv);
|
|
121
|
+
const mcpServers = this._mcpServers === undefined
|
|
122
|
+
? findMcpServers({ env: baseEnv, workingDir: this._workingDir, logger: this._logger })
|
|
123
|
+
: this._mcpServers;
|
|
124
|
+
const { args, env } = buildCodexLaunch({
|
|
125
|
+
env: baseEnv,
|
|
126
|
+
radMcp: this._radMcp,
|
|
127
|
+
mcpServers,
|
|
128
|
+
writableRoots: gitWritableRoots(this._workingDir),
|
|
129
|
+
logger: this._logger,
|
|
130
|
+
});
|
|
131
|
+
this._skillRoots = claudeSkillRoots(env, this._workingDir);
|
|
132
|
+
this._memoryIndex = memoryIndexPath(env, this._workingDir);
|
|
101
133
|
|
|
102
134
|
this._logger.info(`Spawning Codex app-server: ${this._codexCommand}`, {
|
|
103
135
|
sessionId: this._sessionId,
|
|
@@ -254,6 +286,17 @@ class CodexProcess extends EventEmitter {
|
|
|
254
286
|
});
|
|
255
287
|
this._notify('initialized');
|
|
256
288
|
|
|
289
|
+
// Codex loads repo skills only from .agents/skills and .codex/skills. Add
|
|
290
|
+
// the Claude skills folders before the thread starts, so its first turn
|
|
291
|
+
// sees them. A Codex without this call still gets a working chat.
|
|
292
|
+
if (this._skillRoots.length) {
|
|
293
|
+
try {
|
|
294
|
+
await this._request('skills/extraRoots/set', { extraRoots: this._skillRoots });
|
|
295
|
+
} catch (err) {
|
|
296
|
+
this._logger.warn('Codex did not take the Claude skills folders', { roots: this._skillRoots, error: err.message });
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
257
300
|
// workspace-write with network (set in buildCodexArgs): git commit works in
|
|
258
301
|
// a worktree, and git push / package installs reach the network.
|
|
259
302
|
const params = {
|
|
@@ -262,7 +305,8 @@ class CodexProcess extends EventEmitter {
|
|
|
262
305
|
sandbox: 'workspace-write',
|
|
263
306
|
};
|
|
264
307
|
if (this._model) params.model = this._model;
|
|
265
|
-
|
|
308
|
+
const instructions = developerInstructions(this._systemPrompt, this._memoryIndex);
|
|
309
|
+
if (instructions) params.developerInstructions = instructions;
|
|
266
310
|
|
|
267
311
|
let response;
|
|
268
312
|
if (this._resumeThreadId) {
|
|
@@ -596,23 +640,218 @@ function buildCodexEnv(baseEnv, sessionEnv, sessionId) {
|
|
|
596
640
|
* @returns {{command: string, args: string[], env?: object}|null}
|
|
597
641
|
*/
|
|
598
642
|
function findRadMcp(env = process.env, home = os.homedir()) {
|
|
599
|
-
const
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
const
|
|
603
|
-
if (
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
if (radEnv) found.env = radEnv;
|
|
607
|
-
return found;
|
|
608
|
-
}
|
|
609
|
-
} catch (_) { /* no Claude config, or not JSON */ }
|
|
643
|
+
const rad = readJson(claudeConfigFile(env, home))?.mcpServers?.rad;
|
|
644
|
+
if (rad && typeof rad.command === 'string' && Array.isArray(rad.args)) {
|
|
645
|
+
const found = { command: rad.command, args: rad.args.map(String) };
|
|
646
|
+
const radEnv = stringEnv(rad.env);
|
|
647
|
+
if (radEnv) found.env = radEnv;
|
|
648
|
+
return found;
|
|
649
|
+
}
|
|
610
650
|
|
|
611
651
|
const launcher = path.join(home, '.flightdeck', 'rad-mcp');
|
|
612
652
|
if (fs.existsSync(launcher)) return { command: 'ruby', args: [launcher] };
|
|
613
653
|
return null;
|
|
614
654
|
}
|
|
615
655
|
|
|
656
|
+
// ---------------------------------------------------------------------------
|
|
657
|
+
// What a Claude chat in the same directory gets (#491)
|
|
658
|
+
// ---------------------------------------------------------------------------
|
|
659
|
+
|
|
660
|
+
// The Claude CLI's config directory (global CLAUDE.md, skills, project memory)
|
|
661
|
+
// and its main config file (MCP servers), the way the CLI finds them.
|
|
662
|
+
function claudeConfigDir(env = process.env, home = os.homedir()) {
|
|
663
|
+
return env.CLAUDE_CONFIG_DIR || path.join(home, '.claude');
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
function claudeConfigFile(env = process.env, home = os.homedir()) {
|
|
667
|
+
return env.CLAUDE_CONFIG_DIR ? path.join(env.CLAUDE_CONFIG_DIR, '.claude.json') : path.join(home, '.claude.json');
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
function readJson(file) {
|
|
671
|
+
try {
|
|
672
|
+
return JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
673
|
+
} catch (_) {
|
|
674
|
+
return null; // missing, or not JSON
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
function isDirectory(dir) {
|
|
679
|
+
try {
|
|
680
|
+
return fs.statSync(dir).isDirectory();
|
|
681
|
+
} catch (_) {
|
|
682
|
+
return false;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
/**
|
|
687
|
+
* The Claude skills folders for a chat in `workingDir`: the repo's
|
|
688
|
+
* `.claude/skills` and the user's Claude skills folder, the ones that exist.
|
|
689
|
+
* Codex follows symlinked skill folders (checked on 0.162.1).
|
|
690
|
+
*
|
|
691
|
+
* A folder Codex already loads by itself (`.agents/skills` and `.codex/skills`
|
|
692
|
+
* in the repo, `~/.agents/skills`, `$CODEX_HOME/skills`), or one already in
|
|
693
|
+
* this list, is left out, compared by real path. A repo whose `.claude/skills`
|
|
694
|
+
* is a symlink to `.agents/skills` would otherwise get its skills relabelled
|
|
695
|
+
* from repo to user: Codex lists a folder once, under the scope of the last
|
|
696
|
+
* root that named it.
|
|
697
|
+
*/
|
|
698
|
+
function claudeSkillRoots(env = process.env, workingDir = process.cwd(), home = os.homedir()) {
|
|
699
|
+
const native = [
|
|
700
|
+
path.join(workingDir, '.agents', 'skills'),
|
|
701
|
+
path.join(workingDir, '.codex', 'skills'),
|
|
702
|
+
path.join(home, '.agents', 'skills'),
|
|
703
|
+
];
|
|
704
|
+
if (env.CODEX_HOME) native.push(path.join(env.CODEX_HOME, 'skills'));
|
|
705
|
+
const seen = new Set(native.filter(isDirectory).map(realPath));
|
|
706
|
+
|
|
707
|
+
const roots = [];
|
|
708
|
+
for (const root of [path.join(workingDir, '.claude', 'skills'), path.join(claudeConfigDir(env, home), 'skills')]) {
|
|
709
|
+
if (!isDirectory(root)) continue;
|
|
710
|
+
const real = realPath(root);
|
|
711
|
+
if (seen.has(real)) continue;
|
|
712
|
+
seen.add(real);
|
|
713
|
+
roots.push(root);
|
|
714
|
+
}
|
|
715
|
+
return roots;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
function realPath(dir) {
|
|
719
|
+
try {
|
|
720
|
+
return fs.realpathSync(dir);
|
|
721
|
+
} catch (_) {
|
|
722
|
+
return path.resolve(dir);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* The Claude project memory index for `workingDir`, or null. Claude names the
|
|
728
|
+
* project folder after the cwd with every character other than a letter or
|
|
729
|
+
* digit turned into "-".
|
|
730
|
+
*/
|
|
731
|
+
function memoryIndexPath(env = process.env, workingDir = process.cwd(), home = os.homedir()) {
|
|
732
|
+
const project = workingDir.replace(/[^a-zA-Z0-9]/g, '-');
|
|
733
|
+
const index = path.join(claudeConfigDir(env, home), 'projects', project, 'memory', 'MEMORY.md');
|
|
734
|
+
return fs.existsSync(index) ? index : null;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// The persona prompt, plus one line pointing at the memory index when there is one.
|
|
738
|
+
function developerInstructions(systemPrompt, memoryIndex) {
|
|
739
|
+
if (!memoryIndex) return systemPrompt || null;
|
|
740
|
+
const line = `Before you start, read your memory index at ${memoryIndex}. It lists what earlier chats in this repo learned; open the files it links to when they bear on your task.`;
|
|
741
|
+
return systemPrompt ? `${systemPrompt}\n\n${line}` : line;
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Expand `${VAR}` and `${VAR:-default}` against `env`, as Claude does for MCP
|
|
746
|
+
* entries. Null when a variable is unset and has no default.
|
|
747
|
+
*/
|
|
748
|
+
function expandEnvRefs(value, env) {
|
|
749
|
+
return expandRefs(value, env).value;
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// { value, missing }: missing names the first unset variable with no default.
|
|
753
|
+
function expandRefs(value, env) {
|
|
754
|
+
let missing = null;
|
|
755
|
+
const out = String(value).replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g, (_, name, fallback) => {
|
|
756
|
+
const set = env[name];
|
|
757
|
+
if (set !== undefined && (set !== '' || fallback === undefined)) return set;
|
|
758
|
+
if (fallback !== undefined) return fallback;
|
|
759
|
+
if (!missing) missing = name;
|
|
760
|
+
return '';
|
|
761
|
+
});
|
|
762
|
+
return { value: missing ? null : out, missing };
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
function mcpMap(servers) {
|
|
766
|
+
return servers && typeof servers === 'object' && !Array.isArray(servers) ? servers : {};
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// One Claude MCP entry as a server Codex can start, or null (logged once).
|
|
770
|
+
function stdioServer(name, entry, env, logger) {
|
|
771
|
+
const type = entry && (entry.type || (entry.url ? 'http' : 'stdio'));
|
|
772
|
+
if (!MCP_SERVER_NAME.test(name) || type !== 'stdio' || typeof entry.command !== 'string') {
|
|
773
|
+
logger.warn('Skipping MCP server for Codex: only stdio servers with a plain name can run there', { server: name, type });
|
|
774
|
+
return null;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
const unset = [];
|
|
778
|
+
const expand = (value) => {
|
|
779
|
+
const { value: out, missing } = expandRefs(value, env);
|
|
780
|
+
if (missing) unset.push(missing);
|
|
781
|
+
return out;
|
|
782
|
+
};
|
|
783
|
+
const server = { name, command: expand(entry.command), args: (Array.isArray(entry.args) ? entry.args : []).map(expand) };
|
|
784
|
+
const serverEnv = stringEnv(entry.env);
|
|
785
|
+
if (serverEnv) {
|
|
786
|
+
for (const key of Object.keys(serverEnv)) serverEnv[key] = expand(serverEnv[key]);
|
|
787
|
+
server.env = serverEnv;
|
|
788
|
+
}
|
|
789
|
+
if (unset.length) {
|
|
790
|
+
logger.warn('Skipping MCP server for Codex: it needs an environment variable that is not set', { server: name, variable: unset[0] });
|
|
791
|
+
return null;
|
|
792
|
+
}
|
|
793
|
+
return server;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
function stringList(value) {
|
|
797
|
+
return Array.isArray(value) ? value.filter((v) => typeof v === 'string') : [];
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
/**
|
|
801
|
+
* The stdio MCP servers a Claude chat in `workingDir` gets, besides rad: the
|
|
802
|
+
* user's `mcpServers`, the repo's `.mcp.json`, and this directory's entry under
|
|
803
|
+
* `projects` in the Claude config, in Claude's scope order (the project entry
|
|
804
|
+
* wins, then .mcp.json, then the user's). HTTP, SSE and OAuth servers are
|
|
805
|
+
* skipped with one log line each. A `rad` entry is left to findRadMcp, so a
|
|
806
|
+
* chat always reaches its own FlightDeck the way it does today.
|
|
807
|
+
*
|
|
808
|
+
* Claude's disable state applies too, each skip with one log line: a server
|
|
809
|
+
* named in this directory's `disabledMcpServers` (any scope, what `/mcp`
|
|
810
|
+
* disable writes), and a .mcp.json server named in `disabledMcpjsonServers`
|
|
811
|
+
* there or in a Claude settings file. `enabledMcpjsonServers` is not a gate:
|
|
812
|
+
* .mcp.json stdio servers load without approval.
|
|
813
|
+
*
|
|
814
|
+
* @returns {Array<{name: string, command: string, args: string[], env?: object}>}
|
|
815
|
+
*/
|
|
816
|
+
function findMcpServers({ env = process.env, workingDir = process.cwd(), logger = _defaultLogger(), home = os.homedir() } = {}) {
|
|
817
|
+
const config = readJson(claudeConfigFile(env, home)) || {};
|
|
818
|
+
const projects = mcpMap(config.projects);
|
|
819
|
+
let real = workingDir;
|
|
820
|
+
try { real = fs.realpathSync(workingDir); } catch (_) { /* keep as given */ }
|
|
821
|
+
const project = projects[workingDir] || projects[real] || {};
|
|
822
|
+
const repo = (readJson(path.join(workingDir, '.mcp.json')) || {}).mcpServers;
|
|
823
|
+
|
|
824
|
+
const disabled = new Set(stringList(project.disabledMcpServers));
|
|
825
|
+
const settingsFiles = [
|
|
826
|
+
path.join(claudeConfigDir(env, home), 'settings.json'),
|
|
827
|
+
path.join(workingDir, '.claude', 'settings.json'),
|
|
828
|
+
path.join(workingDir, '.claude', 'settings.local.json'),
|
|
829
|
+
];
|
|
830
|
+
const disabledJson = new Set([
|
|
831
|
+
...stringList(project.disabledMcpjsonServers),
|
|
832
|
+
...settingsFiles.flatMap((file) => stringList((readJson(file) || {}).disabledMcpjsonServers)),
|
|
833
|
+
]);
|
|
834
|
+
|
|
835
|
+
const merged = new Map(); // name -> { scope, entry }; a later scope wins
|
|
836
|
+
for (const [scope, map] of [['user', config.mcpServers], ['project', repo], ['local', project.mcpServers]]) {
|
|
837
|
+
for (const [name, entry] of Object.entries(mcpMap(map))) merged.set(name, { scope, entry });
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
const servers = [];
|
|
841
|
+
for (const [name, { scope, entry }] of merged) {
|
|
842
|
+
if (name === 'rad') continue;
|
|
843
|
+
const reason = disabled.has(name) ? 'disabledMcpServers'
|
|
844
|
+
: scope === 'project' && disabledJson.has(name) ? 'disabledMcpjsonServers' : null;
|
|
845
|
+
if (reason) {
|
|
846
|
+
logger.warn('Skipping MCP server for Codex: it is disabled for this directory in Claude', { server: name, reason });
|
|
847
|
+
continue;
|
|
848
|
+
}
|
|
849
|
+
const server = stdioServer(name, entry, env, logger);
|
|
850
|
+
if (server) servers.push(server);
|
|
851
|
+
}
|
|
852
|
+
return servers;
|
|
853
|
+
}
|
|
854
|
+
|
|
616
855
|
// Keep only string-valued entries of an MCP `env` map; null when none are left.
|
|
617
856
|
function stringEnv(map) {
|
|
618
857
|
if (!map || typeof map !== 'object' || Array.isArray(map)) return null;
|
|
@@ -645,33 +884,40 @@ function userCodexHome(env = process.env, home = os.homedir()) {
|
|
|
645
884
|
}
|
|
646
885
|
|
|
647
886
|
/**
|
|
648
|
-
* Create the dedicated Codex home and link
|
|
649
|
-
* Codex rewrites auth.json in place (open
|
|
650
|
-
* rename; codex-rs/login/src/auth/storage.rs,
|
|
651
|
-
* token is written through the symlink to the
|
|
652
|
-
*
|
|
653
|
-
*
|
|
654
|
-
*
|
|
887
|
+
* Create the dedicated Codex home and link two files into it:
|
|
888
|
+
* - auth.json, the user's Codex login. Codex rewrites auth.json in place (open
|
|
889
|
+
* with truncate, no temp file and rename; codex-rs/login/src/auth/storage.rs,
|
|
890
|
+
* rust-v0.162.1), so a refreshed token is written through the symlink to the
|
|
891
|
+
* user's file and the two never drift.
|
|
892
|
+
* - AGENTS.md, the user's Claude global CLAUDE.md, which Codex reads as the
|
|
893
|
+
* global rules for every chat (#491).
|
|
894
|
+
* Nothing in the user's Codex or Claude folders is modified, copied or
|
|
895
|
+
* removed; only the symlinks in the FlightDeck home are created or repaired.
|
|
896
|
+
* A regular file already at either path (a login done in this home, rules
|
|
897
|
+
* written for it) is left alone.
|
|
655
898
|
*
|
|
656
899
|
* @returns {string} the CODEX_HOME to give the child
|
|
657
900
|
*/
|
|
658
901
|
function prepareCodexHome(env = process.env, home = os.homedir()) {
|
|
659
902
|
const codexHome = flightdeckCodexHome(env, home);
|
|
660
903
|
fs.mkdirSync(codexHome, { recursive: true, mode: 0o700 });
|
|
904
|
+
linkInto(path.join(userCodexHome(env, home), 'auth.json'), path.join(codexHome, 'auth.json'));
|
|
905
|
+
linkInto(path.join(claudeConfigDir(env, home), 'CLAUDE.md'), path.join(codexHome, 'AGENTS.md'));
|
|
906
|
+
return codexHome;
|
|
907
|
+
}
|
|
661
908
|
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
if (!fs.existsSync(source)) return
|
|
909
|
+
// Point `link` at `source` when the source exists, unless a regular file is there.
|
|
910
|
+
function linkInto(source, link) {
|
|
911
|
+
if (!fs.existsSync(source)) return;
|
|
665
912
|
|
|
666
913
|
let existing = null;
|
|
667
914
|
try { existing = fs.lstatSync(link); } catch (_) { /* absent */ }
|
|
668
915
|
if (existing) {
|
|
669
|
-
if (!existing.isSymbolicLink()) return
|
|
670
|
-
if (fs.readlinkSync(link) === source) return
|
|
916
|
+
if (!existing.isSymbolicLink()) return;
|
|
917
|
+
if (fs.readlinkSync(link) === source) return;
|
|
671
918
|
fs.unlinkSync(link);
|
|
672
919
|
}
|
|
673
920
|
fs.symlinkSync(source, link);
|
|
674
|
-
return codexHome;
|
|
675
921
|
}
|
|
676
922
|
|
|
677
923
|
/**
|
|
@@ -707,19 +953,35 @@ function gitWritableRoots(workingDir, exec = childProcess.execFileSync) {
|
|
|
707
953
|
}
|
|
708
954
|
|
|
709
955
|
/**
|
|
710
|
-
* Arguments for `codex app-server`. Per-process `-c` overrides, so no
|
|
711
|
-
* file is written:
|
|
956
|
+
* Arguments and env for `codex app-server`. Per-process `-c` overrides, so no
|
|
957
|
+
* config file is written:
|
|
712
958
|
* - sandbox_workspace_write.network_access: workspace-write blocks the network
|
|
713
959
|
* by default, which breaks git push and installs.
|
|
960
|
+
* - project_doc_fallback_filenames: Codex reads CLAUDE.md in a directory with
|
|
961
|
+
* no AGENTS.md, so a chat gets the repo's Claude rules (#491).
|
|
714
962
|
* - mcp_servers.rad: the command a Claude chat uses, plus env_vars so that
|
|
715
|
-
* CLAUDE_SESSION_ID (and the URL and token, when set) reach the server
|
|
716
|
-
*
|
|
963
|
+
* CLAUDE_SESSION_ID (and the URL and token, when set) reach the server.
|
|
964
|
+
* - mcp_servers.<name>: every other stdio server a Claude chat gets
|
|
965
|
+
* (findMcpServers), enabled and approved like rad.
|
|
966
|
+
*
|
|
967
|
+
* No MCP env value goes on argv, where `ps` shows it. Each one is set in the
|
|
968
|
+
* child's env and its name listed in that server's env_vars, so Codex hands
|
|
969
|
+
* it to the server. A server's own `args` (as written, or after ${VAR}
|
|
970
|
+
* expansion) do go on argv, as they do on the server's command line in a
|
|
971
|
+
* Claude chat. The rad entry's map wins over the daemon's env, as in Claude.
|
|
972
|
+
* Two servers that need different values under one name cannot share the
|
|
973
|
+
* child's env: the later one is skipped with a log line. So is a server that
|
|
974
|
+
* would change a name Codex itself runs with (isGuardedEnvVar) to a value the
|
|
975
|
+
* daemon's env does not already have; the rad entry drops such a name with a
|
|
976
|
+
* log line instead. Log lines name the variable, never its value.
|
|
717
977
|
*
|
|
718
|
-
* The user's MCP servers and plugins
|
|
719
|
-
*
|
|
720
|
-
*
|
|
978
|
+
* The user's own Codex MCP servers and plugins never load: the child runs in
|
|
979
|
+
* the dedicated CODEX_HOME (prepareCodexHome), which has no config.toml.
|
|
980
|
+
*
|
|
981
|
+
* @returns {{args: string[], env: object}} env is a new object
|
|
721
982
|
*/
|
|
722
|
-
function
|
|
983
|
+
function buildCodexLaunch({ env = {}, radMcp, mcpServers = [], writableRoots, logger = _defaultLogger() } = {}) {
|
|
984
|
+
const childEnv = { ...env };
|
|
723
985
|
const args = ['app-server', '-c', 'sandbox_workspace_write.network_access=true'];
|
|
724
986
|
if (Array.isArray(writableRoots) && writableRoots.length) {
|
|
725
987
|
args.push('-c', `sandbox_workspace_write.writable_roots=${JSON.stringify(writableRoots)}`);
|
|
@@ -728,27 +990,68 @@ function buildCodexArgs({ env, radMcp, writableRoots } = {}) {
|
|
|
728
990
|
// ChatGPT account's connectors, 100+ tools) and the curated plugin catalog
|
|
729
991
|
// whenever the login is a ChatGPT login. Turn both features off.
|
|
730
992
|
for (const feature of NO_CHATGPT_EXTRAS) args.push('-c', `features.${feature}=false`);
|
|
993
|
+
args.push('-c', 'project_doc_fallback_filenames=["CLAUDE.md"]');
|
|
731
994
|
|
|
732
|
-
const
|
|
733
|
-
if (rad) {
|
|
995
|
+
const addServer = (name, server, envVars) => {
|
|
734
996
|
args.push(
|
|
735
|
-
'-c', `mcp_servers.
|
|
736
|
-
'-c', `mcp_servers.
|
|
737
|
-
|
|
738
|
-
|
|
997
|
+
'-c', `mcp_servers.${name}.command=${JSON.stringify(server.command)}`,
|
|
998
|
+
'-c', `mcp_servers.${name}.args=${JSON.stringify(server.args)}`,
|
|
999
|
+
);
|
|
1000
|
+
if (envVars.length) args.push('-c', `mcp_servers.${name}.env_vars=${JSON.stringify(envVars)}`);
|
|
1001
|
+
args.push(
|
|
1002
|
+
'-c', `mcp_servers.${name}.enabled=true`,
|
|
739
1003
|
// approval_policy "never" refuses any MCP tool the server does not mark
|
|
740
|
-
// read-only.
|
|
741
|
-
'-c',
|
|
1004
|
+
// read-only. These are the tools a Claude chat in this repo has.
|
|
1005
|
+
'-c', `mcp_servers.${name}.default_tools_approval_mode="approve"`,
|
|
742
1006
|
);
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
1007
|
+
};
|
|
1008
|
+
|
|
1009
|
+
const claimed = new Map(); // env name -> the value an MCP server needs
|
|
1010
|
+
const rad = radMcp === undefined ? findRadMcp(env) : radMcp;
|
|
1011
|
+
if (rad) {
|
|
1012
|
+
const applied = [];
|
|
1013
|
+
for (const [key, value] of Object.entries(stringEnv(rad.env) || {})) {
|
|
1014
|
+
if (isGuardedEnvVar(key) && childEnv[key] !== value) {
|
|
1015
|
+
logger.warn('Ignoring a variable in the rad MCP entry env: Codex itself runs with it', { server: 'rad', variable: key });
|
|
1016
|
+
continue;
|
|
1017
|
+
}
|
|
1018
|
+
childEnv[key] = value;
|
|
1019
|
+
applied.push(key);
|
|
1020
|
+
}
|
|
1021
|
+
const envVars = [...new Set([...RAD_ENV_VARS, ...applied])];
|
|
1022
|
+
for (const key of envVars) claimed.set(key, childEnv[key]);
|
|
1023
|
+
addServer('rad', { command: rad.command, args: rad.args }, envVars);
|
|
1024
|
+
}
|
|
1025
|
+
|
|
1026
|
+
for (const server of mcpServers) {
|
|
1027
|
+
const serverEnv = stringEnv(server.env) || {};
|
|
1028
|
+
const guarded = Object.entries(serverEnv).find(([key, value]) => isGuardedEnvVar(key) && childEnv[key] !== value);
|
|
1029
|
+
if (guarded) {
|
|
1030
|
+
logger.warn('Skipping MCP server for Codex: its env would change a variable Codex itself runs with', {
|
|
1031
|
+
server: server.name, variable: guarded[0],
|
|
1032
|
+
});
|
|
1033
|
+
continue;
|
|
749
1034
|
}
|
|
1035
|
+
const clash = Object.entries(serverEnv).find(([key, value]) => claimed.has(key) && claimed.get(key) !== value);
|
|
1036
|
+
if (clash) {
|
|
1037
|
+
logger.warn('Skipping MCP server for Codex: another server needs a different value for one of its env variables', {
|
|
1038
|
+
server: server.name, variable: clash[0],
|
|
1039
|
+
});
|
|
1040
|
+
continue;
|
|
1041
|
+
}
|
|
1042
|
+
for (const [key, value] of Object.entries(serverEnv)) {
|
|
1043
|
+
childEnv[key] = value;
|
|
1044
|
+
claimed.set(key, value);
|
|
1045
|
+
}
|
|
1046
|
+
addServer(server.name, server, Object.keys(serverEnv));
|
|
750
1047
|
}
|
|
751
|
-
|
|
1048
|
+
|
|
1049
|
+
return { args, env: childEnv };
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
// The `codex app-server` arguments alone (see buildCodexLaunch).
|
|
1053
|
+
function buildCodexArgs(options = {}) {
|
|
1054
|
+
return buildCodexLaunch(options).args;
|
|
752
1055
|
}
|
|
753
1056
|
|
|
754
1057
|
function _defaultLogger() {
|
|
@@ -770,3 +1073,9 @@ module.exports.prepareCodexHome = prepareCodexHome;
|
|
|
770
1073
|
module.exports.flightdeckCodexHome = flightdeckCodexHome;
|
|
771
1074
|
module.exports.codexAuthMode = codexAuthMode;
|
|
772
1075
|
module.exports.RAD_ENV_VARS = RAD_ENV_VARS;
|
|
1076
|
+
module.exports.buildCodexLaunch = buildCodexLaunch;
|
|
1077
|
+
module.exports.findMcpServers = findMcpServers;
|
|
1078
|
+
module.exports.claudeSkillRoots = claudeSkillRoots;
|
|
1079
|
+
module.exports.memoryIndexPath = memoryIndexPath;
|
|
1080
|
+
module.exports.developerInstructions = developerInstructions;
|
|
1081
|
+
module.exports.expandEnvRefs = expandEnvRefs;
|