Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

Merge origin/main into fix/atomic-writes-windows

enesa6d2470e89f1
diff --cc src/core/codex-identity-proxy.ts
index 00fa867de,9670adc9f..fcd10558e
--- a/src/core/codex-identity-proxy.ts
+++ b/src/core/codex-identity-proxy.ts
@@@ -32,8 -39,36 +38,37 @@@ import
} from 'fs'
import path from 'path'
import { createHmac, timingSafeEqual } from 'crypto'
+import { renameAtomicSync } from './fs-atomic'
import { platform } from './platform'
+ import { ACCOUNT_ID_RE, isSafeAccountId } from '../shared/codex-account'
+
+ /**
+ * The scope string for the SYSTEM account (`~/.codex`, no managed id). Empty/undefined account ids
+ * normalise to this both in the HMAC preimage and — for the directory — to the bare record root.
+ */
+ export const SYSTEM_ACCOUNT_SCOPE = 'system'
+
+ /**
+ * The one shape of a safe managed account id, re-exported from the shared seam so there is a single
+ * definition of the alphabet the path builders and this proxy both trust. Must start alphanumeric,
+ * which blocks `.`/`..`/leading-separator ids at the door before an id becomes a directory scope.
+ */
+ export const SAFE_ACCOUNT_ID = ACCOUNT_ID_RE
+
+ /**
+ * The scope string for an account id: `system` for empty/undefined, else the validated id itself.
+ * THROWS on an id that could escape the mapping directory (`..`, `a/b`, `/abs`, whitespace) — the
+ * supply-chain guard, since account ids arrive from hand-editable `settings.json` / `project.json`.
+ * A literal id of `system` is refused so the reserved bare-root scope can never be impersonated by
+ * a managed subdirectory.
+ */
+ export function accountScope(accountId?: string): string {
+ if (!accountId) return SYSTEM_ACCOUNT_SCOPE
+ if (accountId === SYSTEM_ACCOUNT_SCOPE || !isSafeAccountId(accountId)) {
+ throw new Error('Invalid NodeTerm Codex account scope')
+ }
+ return accountId
+ }
/**
* How long the SERVER gives itself to mint a thread (`startCodexThreadAt`'s default).
@@@ -192,17 -386,23 +386,23 @@@ export function writeCodexThreadIdentit
if (!isSafeThreadId(threadId) || !validCodexIdentity(nodeId, hookEndpoint)) {
throw new Error('Invalid NodeTerm Codex thread identity')
}
- const signature = identitySignature(threadId, nodeId, hookEndpoint)
- const file = identityFile(threadId, root)
- const tmp = path.join(root, `.${threadId}.${process.pid}.${Date.now()}`)
- mkdirSync(root, { recursive: true, mode: 0o700 })
+ const scope = accountScope(accountId) // throws on an id that could escape the mapping directory
+ const signature = identitySignature(threadId, scope, nodeId, hookEndpoint)
+ const file = identityFile(threadId, scope, root)
+ const dir = path.dirname(file)
+ const tmp = path.join(dir, `.${threadId}.${process.pid}.${Date.now()}`)
+ mkdirSync(dir, { recursive: true, mode: 0o700 })
let renamed = false
try {
- writeFileSync(tmp, `nodeId=${nodeId}\nendpoint=${hookEndpoint}\nsignature=${signature}\n`, {
- encoding: 'utf8',
- mode: 0o600
- })
+ writeFileSync(
+ tmp,
+ `accountId=${accountId ?? ''}\nnodeId=${nodeId}\nendpoint=${hookEndpoint}\nsignature=${signature}\n`,
+ {
+ encoding: 'utf8',
+ mode: 0o600
+ }
+ )
- renameSync(tmp, file)
+ renameAtomicSync(tmp, file)
renamed = true
} finally {
if (!renamed) {
diff --cc src/core/workspace-store.ts
index c0e4ab5f0,c0e7470c9..2d6ff2a03
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@@ -47,14 -63,22 +64,14 @@@ interface LoadedEntry
file?: ProjectFileV1
}
- async function writeAtomic(filePath: string, content: string): Promise<void> {
-let tmpSeq = 0
+ export async function writeAtomic(filePath: string, content: string): Promise<void> {
- // Unique per write: writers that bypass each other's queue (a second app instance, the SSH
+ // Unique temp per write: writers that bypass each other's queue (a second app instance, the SSH
// poll's index write) must never share a tmp file — interleaved writes into one shared tmp
- // published spliced JSON under the atomic rename.
- const tmp = `${filePath}.${process.pid}.${++tmpSeq}.tmp`
- try {
- await fs.writeFile(tmp, content, 'utf-8')
- await fs.rename(tmp, filePath)
- } catch (e) {
- // A unique name never self-heals the way the old fixed one did (the next save just reused
- // it), so a failed write removes its own temp — project.json temps live in the USER'S repo,
- // where litter is visible. The error still propagates; per-file callers swallow it by design.
- await fs.rm(tmp, { force: true }).catch(() => {})
- throw e
- }
+ // published spliced JSON under the atomic rename. writeFileAtomic also removes its own temp on
+ // failure (project.json temps live in the USER'S repo, where litter is visible) and retries the
+ // rename over Windows sharing violations. The error still propagates; per-file callers swallow
+ // it by design.
+ await writeFileAtomic(filePath, content)
}
/** Remove tmp litter next to `target` left by writers that died mid-write: the legacy fixed