@commonlyai/cli 0.1.334 → 0.1.335

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commonlyai/cli",
3
- "version": "0.1.334",
3
+ "version": "0.1.335",
4
4
  "license": "Apache-2.0",
5
5
  "description": "The Commonly CLI — connect agents, manage pods, iterate fast",
6
6
  "type": "module",
@@ -90,7 +90,7 @@ import { adapterFailure, spawnCredentials } from '../upstream-refusal.js';
90
90
  import { publicSeatWithheldTools } from '../public-withheld-tools.js';
91
91
  import { startProviderProxy } from '../provider-proxy.js';
92
92
  import { resolveLaunch, resolveMcpCommand, resolveWindowsCommandWithReason } from '../windows-command.js';
93
- import { codexContextTokens, codexHomeFor } from '../session-recycle.js';
93
+ import { codexContextTokens, codexHomeFor, codexRolloutPath } from '../session-recycle.js';
94
94
 
95
95
  // Default timeout for a single codex spawn (exec mode).
96
96
  //
@@ -370,10 +370,10 @@ const statOrNull = async (path) => {
370
370
  // refused. macOS has no arg0 helper and keeps its homes where they were.
371
371
  const LEGACY_CODEX_HOMES = (home) => join(home, '.commonly', 'codex-homes');
372
372
 
373
- const stateHomeUnderCommonly = (ctx, home) => new Error(
374
- `XDG_STATE_HOME (${ctx.env?.XDG_STATE_HOME}) puts the public Codex home under ${join(home, '.commonly')}, `
375
- + 'which the public sandbox denies on Linux. Point it elsewhere or unset it.',
376
- );
373
+ const stateHomeUnderCommonly = (ctx, home, seat = 'public') => Object.assign(new Error(
374
+ `XDG_STATE_HOME (${ctx.env?.XDG_STATE_HOME}) puts the ${seat} Codex home under ${join(home, '.commonly')}, `
375
+ + `which the ${seat} sandbox denies on Linux. Point it elsewhere or unset it.`,
376
+ ), { code: 'state_home_under_commonly' });
377
377
 
378
378
  const publicCodexHomesRoot = (ctx) => {
379
379
  const home = ctx._homeDir || homedir();
@@ -402,6 +402,103 @@ const operatorCodexHome = (ctx) => ctx.env?.CODEX_HOME
402
402
  || process.env.CODEX_HOME
403
403
  || join(homedir(), '.codex');
404
404
 
405
+ // F445: on Linux a private seat runs with a Codex home of its own, beside the
406
+ // public ones, so the folder its shell can see holds no other session's logs,
407
+ // databases or history: F444 denied a shared home entry by entry, from a
408
+ // listing taken when the turn's keys are built, and anything another session
409
+ // wrote there under a new name, or during a command, stayed readable. The home
410
+ // starts with no history: nothing is copied from the shared one, which holds
411
+ // other sessions' history, the thing being fenced off. What the agent is meant
412
+ // to remember lives in Commonly. Only the person's own setup is carried over,
413
+ // as symlinks Codex follows outside the sandbox: their login, config.toml (a
414
+ // private seat keeps its own config) and global AGENTS.md. macOS denies the
415
+ // shared home whole under Seatbelt and keeps running from it.
416
+ const PRIVATE_CODEX_HOMES = 'codex-private-homes';
417
+ const PRIVATE_HOME_LINKS = Object.freeze(['auth.json', 'config.toml', 'AGENTS.md']);
418
+
419
+ /** The shared Codex home a private seat's setup comes from. */
420
+ const privateSourceCodexHome = (ctx, env) => pathResolve(
421
+ ctx.cwd || process.cwd(),
422
+ env?.CODEX_HOME || join(env?.HOME || homedir(), '.codex'),
423
+ );
424
+
425
+ /** Where this private seat's own Codex home is, or null where it has none. */
426
+ export const privateCodexHomePath = (ctx) => {
427
+ if (ctx._privateCodexHome) return ctx._privateCodexHome;
428
+ const identity = ctx.agentName || ctx.cwd || 'anonymous';
429
+ const hash = createHash('sha256').update(`private:${identity}`).digest('hex').slice(0, 20);
430
+ let publicRoot;
431
+ try {
432
+ publicRoot = publicCodexHomesRoot(ctx);
433
+ } catch (error) {
434
+ if (error?.code !== 'state_home_under_commonly') throw error;
435
+ throw stateHomeUnderCommonly(ctx, ctx._homeDir || homedir(), 'private');
436
+ }
437
+ return join(dirname(publicRoot), PRIVATE_CODEX_HOMES, hash);
438
+ };
439
+
440
+ const preparePrivateCodexHome = async (ctx, childEnv) => {
441
+ const source = privateSourceCodexHome(ctx, childEnv);
442
+ const home = privateCodexHomePath(ctx);
443
+ if (pathResolve(home) === source) throw new Error('a private Codex home must be apart from the shared CODEX_HOME');
444
+ const workspacePath = pathResolve(ctx.cwd || process.cwd());
445
+ const userHome = ctx._homeDir || homedir();
446
+ const [workspace, realHome, realSource, realCommonly] = await Promise.all([
447
+ // As in realpathOfNearest: only a missing workspace falls back to its own path.
448
+ realpath(workspacePath).catch((error) => {
449
+ if (error?.code === 'ENOENT') return workspacePath;
450
+ throw error;
451
+ }),
452
+ realpathOfNearest(home),
453
+ realpathOfNearest(source),
454
+ realpathOfNearest(join(userHome, '.commonly')),
455
+ ]);
456
+ if (contains(workspace, realHome)) {
457
+ throw new Error(`private codex agents cannot run in ${ctx.cwd}: it contains the agent's own Codex home `
458
+ + `(${home}), which the sandbox would then expose. Run it in a project folder.`);
459
+ }
460
+ // Both are denied whole, and a home inside either would hide Codex's own
461
+ // sandbox helper, so every turn would stop at start.
462
+ if (contains(realSource, realHome) || contains(realCommonly, realHome)) {
463
+ throw new Error(`a private Codex agent's own home (${home}) resolves inside `
464
+ + `${contains(realSource, realHome) ? `the shared Codex home (${source})` : join(userHome, '.commonly')}, `
465
+ + 'which its sandbox denies on Linux. Point XDG_STATE_HOME elsewhere or unset it.');
466
+ }
467
+ await mkdir(home, { recursive: true, mode: 0o700 });
468
+ await chmod(home, 0o700);
469
+ for (const name of PRIVATE_HOME_LINKS) {
470
+ const from = join(source, name);
471
+ const to = join(home, name);
472
+ const [fromStat, toStat] = await Promise.all([statOrNull(from), statOrNull(to)]);
473
+ // A file the seat's Codex wrote in its own home is its own: kept.
474
+ if (toStat && !toStat.isSymbolicLink()) continue;
475
+ if (!fromStat) {
476
+ // A link to a login the person removed would sign in through it anyway.
477
+ if (toStat) await unlink(to);
478
+ continue;
479
+ }
480
+ const current = toStat ? pathResolve(dirname(to), await readlink(to)) : null;
481
+ if (current !== from) {
482
+ if (toStat) await unlink(to);
483
+ // Another turn of the same seat may have just made it.
484
+ await symlink(from, to).catch((error) => { if (error?.code !== 'EEXIST') throw error; });
485
+ }
486
+ }
487
+ await mkdir(join(home, 'tmp', 'arg0-linux'), { recursive: true, mode: 0o700 });
488
+ // Every other private seat's home, as it stands now. One created during this
489
+ // turn is denied from the next turn on: until then, what that seat's first
490
+ // turn writes there stays readable to this turn's commands.
491
+ const others = entriesOf(dirname(home)).filter((name) => name !== basename(home)).sort()
492
+ .map((name) => join(dirname(home), name));
493
+ // The links into the shared home, which is denied whole with their targets.
494
+ const links = [];
495
+ for (const name of PRIVATE_HOME_LINKS) {
496
+ const stat = await statOrNull(join(home, name));
497
+ if (stat?.isSymbolicLink() && pathResolve(home, await readlink(join(home, name))) === join(source, name)) links.push(name);
498
+ }
499
+ return { home, source, realSource, others, links };
500
+ };
501
+
405
502
  // `login: false` is the proxy route (D250): codex authenticates with the
406
503
  // per-turn token, so the public home gets no link to the person's Codex login,
407
504
  // and a link an earlier turn left is removed.
@@ -805,7 +902,13 @@ const entriesOf = (dir) => {
805
902
  }
806
903
  };
807
904
 
808
- export const privateCodexHomeDenies = ({ codexHome = null, platform = process.platform, home = homedir() } = {}) => {
905
+ // `links`: entries of the run home that are links into a home denied whole
906
+ // (F445). A key on one makes bwrap follow it into the hidden folder and stop
907
+ // every turn at start ("Can't create file at …/auth.json: No such file or
908
+ // directory", run 43114789288); its target is out of reach already.
909
+ export const privateCodexHomeDenies = ({
910
+ codexHome = null, platform = process.platform, home = homedir(), links = [],
911
+ } = {}) => {
809
912
  const defaultHome = join(home, '.codex');
810
913
  const runHome = codexHome ? realOrSelf(codexHome) : null;
811
914
  const runsFromDefault = !runHome || runHome === realOrSelf(defaultHome);
@@ -823,6 +926,7 @@ export const privateCodexHomeDenies = ({ codexHome = null, platform = process.pl
823
926
  .forEach((name) => SQLITE_SIDECARS.forEach((suffix) => names.add(`${name}${suffix}`)));
824
927
  }
825
928
  names.delete(keep);
929
+ if (depth === 0) links.forEach((name) => names.delete(name));
826
930
  denies.push(...[...names].sort().map((name) => join(dir, name)));
827
931
  dir = join(dir, keep);
828
932
  }
@@ -900,6 +1004,7 @@ const privatePermissionProfileFlags = ({
900
1004
  codexHome = null,
901
1005
  home = homedir(),
902
1006
  otherCodexHomes = [],
1007
+ codexHomeLinks = [],
903
1008
  } = {}) => {
904
1009
  const { commitHooksPath, originalHookPaths } = commitHookReadPaths(commitAttributionEnv);
905
1010
  const posix = platform !== 'win32';
@@ -917,7 +1022,7 @@ const privatePermissionProfileFlags = ({
917
1022
  // no drive and Codex would refuse the whole profile (as with /private/tmp
918
1023
  // in publicPermissionProfileFlags).
919
1024
  posix ? '"/"="read"' : '":minimal"="read"',
920
- ...[...PRIVATE_SECRET_DENIES, ...privateCodexHomeDenies({ codexHome, platform, home }), ...otherCodexHomes]
1025
+ ...[...PRIVATE_SECRET_DENIES, ...privateCodexHomeDenies({ codexHome, platform, home, links: codexHomeLinks }), ...otherCodexHomes]
921
1026
  .map((path) => `${toml(path)}="deny"`),
922
1027
  ...deniedTemp.map((path) => `${toml(path)}="deny"`),
923
1028
  ...(shellTemp ? [`${toml(shellTemp)}="write"`] : []),
@@ -1070,6 +1175,7 @@ const buildArgs = ({
1070
1175
  privateCodexHome = null,
1071
1176
  privateHome = homedir(),
1072
1177
  privateOtherCodexHomes = [],
1178
+ privateCodexHomeLinks = [],
1073
1179
  }) => {
1074
1180
  const publicSandbox = publicSandboxMode !== null;
1075
1181
  // F387: a private seat keeps the flag below only in a cloud-codex pod, or
@@ -1128,6 +1234,7 @@ const buildArgs = ({
1128
1234
  codexHome: privateCodexHome,
1129
1235
  home: privateHome,
1130
1236
  otherCodexHomes: privateOtherCodexHomes,
1237
+ codexHomeLinks: privateCodexHomeLinks,
1131
1238
  });
1132
1239
  const common = [
1133
1240
  '--json',
@@ -1353,8 +1460,20 @@ export default {
1353
1460
  // alone. claude.js adds parallel reads, whose cue names Claude's Agent tool;
1354
1461
  // Codex has agent tools of its own, under other names. A public seat answers
1355
1462
  // strangers rather than working board rows, so it is taught neither.
1463
+ // F445: a private seat on Linux resumes only a thread in its own Codex
1464
+ // home; one in the shared home (a run before F445) starts fresh, which the
1465
+ // prompt has to know.
1466
+ const ownPrivateHome = (ctx._platform || process.platform) === 'linux' && sandbox?.trust !== 'public'
1467
+ && !codexTrustedPerimeter(ctx.env || process.env) && ctx.hostUnconfined !== true
1468
+ ? privateCodexHomePath(ctx) : null;
1469
+ let sessionId = ctx.sessionId || null;
1470
+ if (ownPrivateHome && sessionId && !codexRolloutPath({ codexHome: ownPrivateHome, sessionId })) {
1471
+ process.stderr.write('[codex] this agent now keeps its own Codex home, so it starts a new thread; '
1472
+ + 'what it is meant to remember is in its Commonly memory.\n');
1473
+ sessionId = null;
1474
+ }
1356
1475
  const fullPrompt = buildPrompt(prompt, ctx.memoryLongTerm, {
1357
- freshSession: !ctx.sessionId,
1476
+ freshSession: !sessionId,
1358
1477
  workCues: sandbox?.trust === 'public' ? [] : [ROW_SLICING_CUE, BACKGROUND_PROCESS_CUE],
1359
1478
  // F118: a public seat reads its memory but is not offered a write.
1360
1479
  memoryReadOnly: sandbox?.trust === 'public' && ctx.ownerWoken !== true,
@@ -1496,6 +1615,9 @@ export default {
1496
1615
  const publicCodexHome = publicSandboxMode !== null
1497
1616
  ? await preparePublicCodexHome(ctx, childEnv, { login: !route })
1498
1617
  : null;
1618
+ const privateCodexHome = privateProfile && ownPrivateHome
1619
+ ? await preparePrivateCodexHome(ctx, childEnv)
1620
+ : null;
1499
1621
  // The private shell's TMPDIR: inside this turn's folder, which `finally`
1500
1622
  // removes, and beside (not over) the token's folder.
1501
1623
  const privateShellTemp = privateProfile ? join(dir, 'shell-tmp') : null;
@@ -1510,7 +1632,7 @@ export default {
1510
1632
  childEnv[CODEX_PROVIDER_TOKEN_ENV] = providerProxy.token;
1511
1633
  }
1512
1634
  const args = buildArgs({
1513
- sessionId: ctx.sessionId || null,
1635
+ sessionId,
1514
1636
  prompt: fullPrompt,
1515
1637
  outputFile,
1516
1638
  mcpFlags: mcp.flags,
@@ -1537,12 +1659,22 @@ export default {
1537
1659
  // The home Codex runs with, and the HOME its `~` keys expand against.
1538
1660
  // Codex reads a relative CODEX_HOME from its own folder, the turn's.
1539
1661
  privateCodexHome: privateProfile
1540
- ? pathResolve(ctx.cwd || process.cwd(), childEnv.CODEX_HOME || join(childEnv.HOME || homedir(), '.codex'))
1662
+ ? privateCodexHome?.home || privateSourceCodexHome(ctx, childEnv)
1541
1663
  : null,
1542
1664
  privateHome: childEnv.HOME || homedir(),
1543
1665
  // Public seats' Codex homes (their sessions and logs): under the denied
1544
- // ~/.commonly on macOS, outside it on Linux.
1545
- privateOtherCodexHomes: privateProfile ? privatePublicHomesDeny(ctx) : [],
1666
+ // ~/.commonly on macOS, outside it on Linux. With a home of its own, the
1667
+ // shared one it came from and every other private seat's are denied whole.
1668
+ privateOtherCodexHomes: privateProfile
1669
+ ? [...new Set([
1670
+ ...privatePublicHomesDeny(ctx),
1671
+ // By its real path too, when it is a link to another folder.
1672
+ ...(privateCodexHome
1673
+ ? [privateCodexHome.source, privateCodexHome.realSource, ...privateCodexHome.others]
1674
+ : []),
1675
+ ])]
1676
+ : [],
1677
+ privateCodexHomeLinks: privateCodexHome?.links || [],
1546
1678
  });
1547
1679
  // childEnv is derived from the process environment, so the bootstrap export has to be
1548
1680
  // taken back out: the declaration above moved our server onto the file,
@@ -1560,6 +1692,7 @@ export default {
1560
1692
  childEnv.CODEX_HOME = publicCodexHome.home;
1561
1693
  await writePublicCodexInstructions(childEnv.CODEX_HOME, ctx);
1562
1694
  }
1695
+ if (privateCodexHome) childEnv.CODEX_HOME = privateCodexHome.home;
1563
1696
 
1564
1697
  // The awaits above can outlast a supervisor stall window. Once it has
1565
1698
  // aborted, the attempt has already settled and a retry may be running,
@@ -1610,7 +1743,7 @@ export default {
1610
1743
  // a new session, often re-emitted on resume) > the persisted id we
1611
1744
  // came in with > null. The wrapper persists whichever non-null value
1612
1745
  // we return so subsequent turns hit `codex exec resume <id>`.
1613
- const newSessionId = threadId || ctx.sessionId || null;
1746
+ const newSessionId = threadId || sessionId || null;
1614
1747
  // F404: what the turn's last request was sent, from the rollout the child
1615
1748
  // wrote under its own CODEX_HOME. Null keeps resuming.
1616
1749
  const contextTokens = newSessionId