Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Codex home: drop old chat links first, keep the person's home when the login may be in the keyring or the home can't be made

Bugfixer 2dcdb25cc7a4e
diff --git a/server/drivers/codex-shared-home.test.ts b/server/drivers/codex-shared-home.test.ts
index 4cae57ee51..4ead31dc80 100644
--- a/server/drivers/codex-shared-home.test.ts
+++ b/server/drivers/codex-shared-home.test.ts
@@ -79,6 +79,18 @@ describe("linkSharedCodexHome", () => {
expect(readFileSync(join(shared, "state_5.sqlite"), "utf8")).toBe("person's chat list");
});
+ it("removes links to the person's chats even when their home cannot be shared", () => {
+ write(join(shared, "sessions", "2026", "rollout-person.jsonl"), "person's chat");
+ write(join(shared, "mcp-oauth-locks"), "a file where a shared folder should be");
+ mkdirSync(plan, { recursive: true });
+ symlinkSync(join(shared, "sessions"), join(plan, "sessions"));
+
+ expect(linkSharedCodexHome(shared, plan)).toBe(false);
+
+ expect(() => lstatSync(join(plan, "sessions"))).toThrow();
+ expect(readFileSync(join(shared, "sessions", "2026", "rollout-person.jsonl"), "utf8")).toBe("person's chat");
+ });
+
it("links the person's own login into a home that uses it, and says when it cannot", () => {
write(join(shared, "auth.json"), "person's sign-in");
write(join(shared, "models_cache.json"), "person's models");
@@ -183,9 +195,9 @@ describe("linkSharedCodexHome", () => {
describe("keepChatsOutOfCodexHistory", () => {
const data = () => join(root, "data");
const mausHome = (instanceId: string) => join(data(), "providers", "codex-own", createHash("sha256").update(instanceId).digest("hex"), "codex");
- const turnEnv = (extra: Record<string, string> = {}, teamCloud = false) => {
+ const turnEnv = (extra: Record<string, string> = {}, teamCloud = false, managed: string[] = []) => {
const env: Record<string, string | undefined> = { HOME: join(root, "person"), ...extra };
- keepChatsOutOfCodexHistory(env, "codex", data(), teamCloud);
+ keepChatsOutOfCodexHistory(env, "codex", data(), teamCloud, managed);
return env;
};
@@ -217,4 +229,26 @@ describe("keepChatsOutOfCodexHistory", () => {
write(join(mausHome("codex"), "auth.json"), "a login of its own");
expect(turnEnv().CODEX_HOME).toBeUndefined();
});
+
+ it("keeps the person's home when it has no login file or a system-wide setting may put the login in the keyring", () => {
+ write(join(shared, "config.toml"), 'model = "gpt-5"\n');
+ expect(turnEnv().CODEX_HOME).toBeUndefined();
+ expect(existsSync(mausHome("codex"))).toBe(false);
+
+ write(join(shared, "auth.json"), "person's sign-in");
+ const managed = join(root, "etc", "codex", "requirements.toml");
+ write(managed, 'cli_auth_credentials_store = "keyring"\n');
+ expect(turnEnv({}, false, [managed, join(root, "etc", "codex", "missing.toml")]).CODEX_HOME).toBeUndefined();
+ write(managed, 'allowed_sandbox_modes = ["read-only"]\n');
+ expect(turnEnv({}, false, [managed]).CODEX_HOME).toBe(mausHome("codex"));
+ });
+
+ it("keeps the person's home, and the turn going, when MausBot's Codex home cannot be made", () => {
+ write(join(shared, "auth.json"), "person's sign-in");
+ write(join(data(), "providers", "codex-own"), "a file where a folder should be");
+ let env: Record<string, string | undefined> = {};
+ expect(() => { env = turnEnv(); }).not.toThrow();
+ expect(env.CODEX_HOME).toBeUndefined();
+ expect(env.CODEX_SQLITE_HOME).toBeUndefined();
+ });
});
diff --git a/server/drivers/codex-shared-home.ts b/server/drivers/codex-shared-home.ts
index c8afa3f420..067517952e 100644
--- a/server/drivers/codex-shared-home.ts
+++ b/server/drivers/codex-shared-home.ts
@@ -4,7 +4,7 @@
// Codex, but MausBot's chats stay in its own home: the Codex and ChatGPT apps list every session in the person's home as
// their history. Company Codex homes never come here: they stay fully separate.
import { createHash } from "node:crypto";
-import { lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, unlinkSync } from "node:fs";
+import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, rmSync, statSync, symlinkSync, unlinkSync } from "node:fs";
import { isAbsolute, join, relative, resolve } from "node:path";
import { DATA_DIR } from "../config.ts";
@@ -81,8 +81,17 @@ export type CodexHomeLogin = "own" | "person";
* home has the login it should: its own, or the person's (none when they have none). */
export function linkSharedCodexHome(sharedHome: string | null, mausHome: string, login: CodexHomeLogin = "own"): boolean {
mkdirSync(mausHome, { recursive: true, mode: 0o700 });
- if (!sharedHome || !isAbsolute(sharedHome) || !isAbsolute(mausHome)) return false;
- const shared = resolve(sharedHome), own = resolve(mausHome);
+ if (!isAbsolute(mausHome)) return false;
+ const own = resolve(mausHome);
+ const apart = (name: string) => CHATS.has(name) || DATABASE.test(name) || (login === "own" && PRIVATE.has(name));
+ // A link from before chats were kept apart goes, even when sharing fails below, so no new chat is written through it;
+ // the person's own files behind it stay as they are.
+ for (const name of readdirSync(own)) {
+ const path = join(own, name);
+ if (apart(name)) try { if (lstatSync(path).isSymbolicLink()) unlinkSync(path); } catch {}
+ }
+ if (!sharedHome || !isAbsolute(sharedHome)) return false;
+ const shared = resolve(sharedHome);
if (inside(shared, own) || inside(own, shared)) return false;
let names: string[];
try {
@@ -92,12 +101,6 @@ export function linkSharedCodexHome(sharedHome: string | null, mausHome: string,
note(shared, `could not share your Codex home with MausBot's: ${(error as Error).message}`);
return false;
}
- const apart = (name: string) => CHATS.has(name) || DATABASE.test(name) || (login === "own" && PRIVATE.has(name));
- // A link from before chats were kept apart goes; the person's own files behind it stay as they are.
- for (const name of readdirSync(own)) {
- const path = join(own, name);
- if (apart(name)) try { if (lstatSync(path).isSymbolicLink()) unlinkSync(path); } catch {}
- }
const kept: string[] = [];
for (const name of new Set([...SHARED_FOLDERS, ...names])) {
if (apart(name) || LOCAL.has(name) || SQLITE_JOURNAL.test(name)) continue;
@@ -119,23 +122,34 @@ function personLogin(shared: string, own: string): boolean {
try { return lstatSync(join(own, "auth.json")).isSymbolicLink(); } catch { return true; }
}
+/** System-wide Codex settings an administrator can use to choose where logins are kept (codex-cli 0.167.2). */
+const MANAGED_CODEX_CONFIGS = process.platform === "win32" ? [] : ["/etc/codex/managed_config.toml", "/etc/codex/requirements.toml"];
+
/** `cli_auth_credentials_store = "keyring"` or `"auto"`: Codex may keep the login in the system keyring under a name made
- * from the home's path, which another home cannot reach, and a refresh there could leave the person's own copy stale. */
-function keyringLogin(home: string): boolean {
- let text: string;
- try { text = readFileSync(join(home, "config.toml"), "utf8"); } catch { return false; }
- const topLevel = text.split(/^\s*\[/m)[0] ?? "";
+ * from the home's path, which another home cannot reach, and a refresh there could leave the person's own copy stale.
+ * Any system-wide setting about the login store counts too: it can choose the keyring without the home saying so. */
+function keyringLogin(home: string, managedConfigs: readonly string[]): boolean {
+ const read = (path: string) => { try { return readFileSync(path, "utf8"); } catch { return ""; } };
+ if (managedConfigs.some(path => read(path).includes("credentials_store"))) return true;
+ const topLevel = read(join(home, "config.toml")).split(/^\s*\[/m)[0] ?? "";
return /^\s*cli_auth_credentials_store\s*=\s*["'](?:keyring|auto)["']/m.test(topLevel);
}
/** A turn on the person's own Codex sign-in runs in MausBot's Codex home for this instance, so its chats stay out of their
* Codex and ChatGPT apps. It keeps their home when that is MausBot's already (a Team Cloud's engine homes, anything in
- * the data folder), when their login may be in the keyring, or when their login cannot be linked. */
-export function keepChatsOutOfCodexHistory(env: Record<string, string | undefined>, instanceId: string, dataDir = DATA_DIR, teamCloud = engineHomes.active()): void {
+ * the data folder), when their home has no login file or their login may be in the keyring, or when their login cannot
+ * be linked or MausBot's home cannot be made: the turn still runs. */
+export function keepChatsOutOfCodexHistory(env: Record<string, string | undefined>, instanceId: string, dataDir = DATA_DIR,
+ teamCloud = engineHomes.active(), managedConfigs: readonly string[] = MANAGED_CODEX_CONFIGS): void {
const person = codexHome(env);
- if (!person || teamCloud || inside(resolve(dataDir), person) || keyringLogin(person)) return;
+ if (!person || teamCloud || inside(resolve(dataDir), person) || !existsSync(join(person, "auth.json")) || keyringLogin(person, managedConfigs)) return;
const home = join(resolve(dataDir), "providers", "codex-own", createHash("sha256").update(instanceId).digest("hex"), "codex");
- if (!linkSharedCodexHome(person, home, "person")) return;
+ try {
+ if (!linkSharedCodexHome(person, home, "person")) return;
+ } catch (error) {
+ note(home, `could not make MausBot's Codex home, so this turn uses yours: ${(error as Error).message}`);
+ return;
+ }
env.CODEX_HOME = home;
// Codex keeps its databases here too unless config.toml names another folder.
env.CODEX_SQLITE_HOME = home;
diff --git a/server/drivers/codex.test.ts b/server/drivers/codex.test.ts
index 23febc723f..da69a81872 100644
--- a/server/drivers/codex.test.ts
+++ b/server/drivers/codex.test.ts
@@ -1228,6 +1228,9 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
it("uses the instance environment for the Codex process", async () => {
const codexHome = join(scratch, "custom-codex-home");
+ // Signed in with a login file: MausBot keeps the person's home when it has none (the login may be in the keyring).
+ mkdirSync(codexHome, { recursive: true });
+ writeFileSync(join(codexHome, "auth.json"), "{}");
await create({ environment: { CODEX_HOME: codexHome } });
const dump = join(scratch, "environment.json");
process.env.FAKE_CODEX_DUMP = dump;
@@ -2233,6 +2236,10 @@ process.stdout.write(JSON.stringify({jsonrpc:'2.0',id:m.id,result})+'\\n');});`)
});
vi.stubEnv("OPENMAUSBOT_CHATGPT_TOKEN", "inherited-token-must-not-leak");
vi.stubEnv("OPENAI_API_KEY", "inherited-api-key-must-not-leak");
+ if (!Object.keys(opts).length) {
+ mkdirSync(join(scratch, ".codex"), { recursive: true });
+ writeFileSync(join(scratch, ".codex", "auth.json"), "{}");
+ }
await create({ ...opts, mode: "resume", environment: { HOME: scratch, USERPROFILE: scratch, CODEX_HOME: join(scratch, ".codex") } });
const dump = join(scratch, "model-provider-resume.json");
process.env.FAKE_CODEX_DUMP = dump;