Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

claudeTrust: never corrupt or detach ~/.claude.json and ~/.codex/config.toml

dee670c46bb961c
diff --git a/cli/src/lib/claudeTrust.spec.ts b/cli/src/lib/claudeTrust.spec.ts
index 415e53e88..801ef4da0 100644
--- a/cli/src/lib/claudeTrust.spec.ts
+++ b/cli/src/lib/claudeTrust.spec.ts
@@ -1,4 +1,4 @@
-import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from 'node:fs'
+import { lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, symlinkSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { describe, expect, it } from 'vitest'
@@ -27,6 +27,53 @@ describe('preTrustClaudeProject', () => {
expect(preTrustClaudeProject('/w', home)).toBe('skipped')
})
+ it('starts the projects map when Claude Code has none yet, and a second call changes nothing', () => {
+ const home = mkdtempSync(join(tmpdir(), 'trust-'))
+ writeFileSync(join(home, '.claude.json'), JSON.stringify({ numStartups: 1 }))
+ expect(preTrustClaudeProject('/Users/example/harnesses/harness-1', home)).toBe('trusted')
+ const once = readFileSync(join(home, '.claude.json'), 'utf8')
+ expect(JSON.parse(once)).toEqual({ numStartups: 1, projects: { '/Users/example/harnesses/harness-1': { allowedTools: [], hasTrustDialogAccepted: true } } })
+ expect(preTrustClaudeProject('/Users/example/harnesses/harness-1', home)).toBe('already')
+ expect(readFileSync(join(home, '.claude.json'), 'utf8')).toBe(once)
+ // the atomic write leaves no temporary file behind
+ expect(readdirSync(home)).toEqual(['.claude.json'])
+ })
+
+ it('leaves a file of any other shape byte for byte as it was', () => {
+ // Writing trust into an array, over a non-object projects value, or by spreading a string entry
+ // into an object would each rewrite something the person has; the answer is to not touch it.
+ for (const text of ['[]', 'null', '42', '{"projects":[]}', '{"projects":"none"}', '{"projects":null}', '{"projects":{"/w":"yes"}}', '{"projects":{"/w":[true]}}']) {
+ const home = mkdtempSync(join(tmpdir(), 'trust-'))
+ writeFileSync(join(home, '.claude.json'), text)
+ expect(preTrustClaudeProject('/w', home), text).toBe('skipped')
+ expect(readFileSync(join(home, '.claude.json'), 'utf8'), text).toBe(text)
+ }
+ })
+
+ it('writes through a symlinked ~/.claude.json instead of replacing the link', () => {
+ const home = mkdtempSync(join(tmpdir(), 'trust-'))
+ mkdirSync(join(home, 'dotfiles'))
+ writeFileSync(join(home, 'dotfiles', 'claude.json'), JSON.stringify({ projects: {} }))
+ symlinkSync(join(home, 'dotfiles', 'claude.json'), join(home, '.claude.json'))
+ expect(preTrustClaudeProject('/Users/example/harnesses/harness-2', home)).toBe('trusted')
+ expect(lstatSync(join(home, '.claude.json')).isSymbolicLink()).toBe(true)
+ const target = JSON.parse(readFileSync(join(home, 'dotfiles', 'claude.json'), 'utf8'))
+ expect(target.projects['/Users/example/harnesses/harness-2'].hasTrustDialogAccepted).toBe(true)
+ expect(readdirSync(join(home, 'dotfiles'))).toEqual(['claude.json'])
+ })
+})
+
+describe('preTrustCodexProject', () => {
+ const codexHome = (config?: string): string => {
+ const home = mkdtempSync(join(tmpdir(), 'trust-'))
+ if (config !== undefined) {
+ mkdirSync(join(home, '.codex'))
+ writeFileSync(join(home, '.codex', 'config.toml'), config)
+ }
+ return home
+ }
+ const configOf = (home: string): string => readFileSync(join(home, '.codex', 'config.toml'), 'utf8')
+
it('records Codex trust as a projects table, once, and only when Codex is here', () => {
const home = mkdtempSync(join(tmpdir(), 'trust-'))
expect(preTrustCodexProject('/w', home)).toBe('skipped')
@@ -39,4 +86,63 @@ describe('preTrustClaudeProject', () => {
expect(after.endsWith('[projects."/Users/x/harnesses/harness-3"]\ntrust_level = "trusted"\n')).toBe(true)
expect(preTrustCodexProject('/Users/x/harnesses/harness-3', home)).toBe('already')
})
+
+ it('is idempotent: a second call leaves the file byte for byte as the first left it', () => {
+ const home = codexHome('model = "gpt-5"')
+ expect(preTrustCodexProject('/Users/example/harnesses/harness-1', home)).toBe('trusted')
+ const once = configOf(home)
+ expect(once).toBe('model = "gpt-5"\n\n[projects."/Users/example/harnesses/harness-1"]\ntrust_level = "trusted"\n')
+ expect(preTrustCodexProject('/Users/example/harnesses/harness-1', home)).toBe('already')
+ expect(configOf(home)).toBe(once)
+ expect(readdirSync(join(home, '.codex'))).toEqual(['config.toml'])
+ })
+
+ it('knows the folder under any quoting or spacing of its table header', () => {
+ for (const header of [`[projects.'/Users/example/w']`, `[ projects . "/Users/example/w" ]`, ` [projects."/Users/\\u0065xample/w"] # trusted by hand`]) {
+ const text = `${header}\ntrust_level = "untrusted"\n`
+ const home = codexHome(text)
+ expect(preTrustCodexProject('/Users/example/w', home), header).toBe('already')
+ expect(configOf(home), header).toBe(text)
+ }
+ })
+
+ it('does not count a header that is only a comment', () => {
+ const home = codexHome('# [projects."/Users/example/w"]\n')
+ expect(preTrustCodexProject('/Users/example/w', home)).toBe('trusted')
+ expect(configOf(home)).toBe('# [projects."/Users/example/w"]\n\n[projects."/Users/example/w"]\ntrust_level = "trusted"\n')
+ })
+
+ it('leaves a config alone when appending a table could make it one Codex refuses to load', () => {
+ for (const text of [
+ // an inline table, and dotted keys: a later [projects."…"] would redefine them
+ 'projects = { "/Users/example/old" = { trust_level = "trusted" } }\n',
+ 'projects."/Users/example/w".trust_level = "trusted"\n',
+ '[projects]\n"/Users/example/w" = { trust_level = "trusted" }\n',
+ // a key in an escape JSON cannot read: it might be this very folder
+ '[projects."/Users/example/\\U0001F600"]\ntrust_level = "trusted"\n',
+ ]) {
+ const home = codexHome(text)
+ expect(preTrustCodexProject('/Users/example/w', home), text).toBe('skipped')
+ expect(configOf(home), text).toBe(text)
+ }
+ })
+
+ it('writes a header TOML can read for a folder with a quote or a DEL in its name, and finds it again', () => {
+ const cwd = '/Users/example/a"b\x7f'
+ const home = codexHome('')
+ expect(preTrustCodexProject(cwd, home)).toBe('trusted')
+ expect(configOf(home)).toBe('\n\n[projects."/Users/example/a\\"b\\u007f"]\ntrust_level = "trusted"\n')
+ expect(preTrustCodexProject(cwd, home)).toBe('already')
+ })
+
+ it('writes through a symlinked config.toml instead of replacing the link', () => {
+ const home = codexHome()
+ mkdirSync(join(home, 'dotfiles'))
+ mkdirSync(join(home, '.codex'))
+ writeFileSync(join(home, 'dotfiles', 'codex.toml'), 'model = "gpt-5"\n')
+ symlinkSync(join(home, 'dotfiles', 'codex.toml'), join(home, '.codex', 'config.toml'))
+ expect(preTrustCodexProject('/Users/example/w', home)).toBe('trusted')
+ expect(lstatSync(join(home, '.codex', 'config.toml')).isSymbolicLink()).toBe(true)
+ expect(readFileSync(join(home, 'dotfiles', 'codex.toml'), 'utf8')).toContain('[projects."/Users/example/w"]')
+ })
})
diff --git a/cli/src/lib/claudeTrust.ts b/cli/src/lib/claudeTrust.ts
index 92ab7c937..3082c8741 100644
--- a/cli/src/lib/claudeTrust.ts
+++ b/cli/src/lib/claudeTrust.ts
@@ -8,44 +8,79 @@
* themselves, never removes anything, and does nothing when Claude Code has never run here (no
* `~/.claude.json`), when the file does not parse, or when the entry already says yes.
*/
-import { existsSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
+import { existsSync, readFileSync, realpathSync, renameSync, writeFileSync } from 'node:fs'
import { homedir } from 'node:os'
import { join } from 'node:path'
+function isPlainObject(value: unknown): value is Record<string, unknown> {
+ return typeof value === 'object' && value !== null && !Array.isArray(value)
+}
+
+/**
+ * Replace a config file atomically, THROUGH a symlink. Dotfile managers keep `~/.claude.json` and
+ * `~/.codex/config.toml` as links into a repo; renaming a temporary file over the link itself would
+ * swap it for a plain file and quietly detach the person's dotfiles.
+ */
+function replaceConfigFile(file: string, text: string): void {
+ const target = realpathSync(file)
+ const tmp = `${target}.harness-${process.pid}.tmp`
+ writeFileSync(tmp, text, { mode: 0o600 })
+ renameSync(tmp, target)
+}
+
export function preTrustClaudeProject(cwd: string, home = homedir()): 'trusted' | 'already' | 'skipped' {
const file = join(home, '.claude.json')
if (!existsSync(file)) return 'skipped'
- let config: Record<string, unknown>
+ let config: unknown
try {
- config = JSON.parse(readFileSync(file, 'utf8')) as Record<string, unknown>
+ config = JSON.parse(readFileSync(file, 'utf8'))
} catch {
return 'skipped'
}
- if (!config || typeof config !== 'object') return 'skipped'
- const projects = (config.projects && typeof config.projects === 'object' ? config.projects : {}) as Record<string, Record<string, unknown>>
+ // Anything but the shape Claude Code writes is left exactly as it is: writing trust into an array,
+ // or spreading a string entry into an object, would rewrite what the person has.
+ if (!isPlainObject(config)) return 'skipped'
+ if (config.projects !== undefined && !isPlainObject(config.projects)) return 'skipped'
+ const projects = (config.projects ?? {}) as Record<string, unknown>
const existing = projects[cwd]
+ if (existing !== undefined && !isPlainObject(existing)) return 'skipped'
if (existing?.hasTrustDialogAccepted === true) return 'already'
projects[cwd] = { allowedTools: [], ...(existing ?? {}), hasTrustDialogAccepted: true }
config.projects = projects
- const tmp = `${file}.harness-${process.pid}.tmp`
- writeFileSync(tmp, JSON.stringify(config, null, 2), { mode: 0o600 })
- renameSync(tmp, file)
+ replaceConfigFile(file, JSON.stringify(config, null, 2))
return 'trusted'
}
+/** A `[projects.<key>]` header, however it is spaced or quoted. */
+const CODEX_PROJECT_HEADER_RE = /^[ \t]*\[[ \t]*projects[ \t]*\.[ \t]*("(?:[^"\\\r\n]|\\.)*"|'[^'\r\n]*')[ \t]*\]/gm
+/** `projects` defined any other way: an inline table, dotted keys, or a bare `[projects]` table. */
+const CODEX_PROJECTS_OTHERWISE_RE = /^[ \t]*(?:projects[ \t]*[.=]|\[[ \t]*projects[ \t]*\])/m
+
+/** The key a quoted TOML key names; null for an escape JSON does not share (`\U0001F600`). */
+function tomlKey(quoted: string): string | null {
+ if (quoted.startsWith("'")) return quoted.slice(1, -1)
+ try { return JSON.parse(quoted) as string } catch { return null }
+}
+
/**
* Codex keeps the same answer in `~/.codex/config.toml` as a `[projects."<path>"]` table with
* `trust_level = "trusted"`. Same rules: only a folder the daemon made, only when Codex has a
* config here, never rewriting what is there — the table is appended at the end.
+ *
+ * Appending is only safe when nothing else defines that table. The same folder under another quoting,
+ * or `projects` written as an inline table or dotted keys, would make the appended table a duplicate
+ * definition — a config.toml Codex refuses to load. Those are left alone.
*/
export function preTrustCodexProject(cwd: string, home = homedir()): 'trusted' | 'already' | 'skipped' {
const file = join(home, '.codex', 'config.toml')
if (!existsSync(file)) return 'skipped'
const text = readFileSync(file, 'utf8')
- const header = `[projects.${JSON.stringify(cwd)}]`
- if (text.includes(header)) return 'already'
- const tmp = `${file}.harness-${process.pid}.tmp`
- writeFileSync(tmp, `${text.replace(/\s*$/, '')}\n\n${header}\ntrust_level = "trusted"\n`, { mode: 0o600 })
- renameSync(tmp, file)
+ const keys = [...text.matchAll(CODEX_PROJECT_HEADER_RE)].map((match) => tomlKey(match[1]))
+ if (keys.includes(cwd)) return 'already'
+ // A key this cannot read might be this folder spelled another way: not safe to append after.
+ if (keys.includes(null) || CODEX_PROJECTS_OTHERWISE_RE.test(text)) return 'skipped'
+ // A TOML basic string is a JSON string, except that DEL must be escaped too.
+ const header = `[projects.${JSON.stringify(cwd).replace(/\x7f/g, '\\u007f')}]`
+ replaceConfigFile(file, `${text.replace(/\s*$/, '')}\n\n${header}\ntrust_level = "trusted"\n`)
return 'trusted'
}