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.
TypeScript 3d-modelingagent-frameworkagent-harnessai-agentscadclaude-codecodexcoding-agentsdeveloper-toolsesp32game-developmentmcp-servermulti-agentopen-hardwareopencodeopenharnesspcbroboticsterminaltmux
fix(registry): clear an ownerless registry lock instead of failing every write (#1707)
0xdiego2505e3eba6f4bd
diff --git a/cli/src/lib/registry.spec.ts b/cli/src/lib/registry.spec.tsindex 565ba2244..998f96d02 100644--- a/cli/src/lib/registry.spec.ts+++ b/cli/src/lib/registry.spec.ts@@ -1,5 +1,5 @@import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'-import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'fs'+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from 'fs'import { tmpdir } from 'os'import { join } from 'path'import { processStartMarker } from './processLiveness.js'@@ -489,6 +489,29 @@ describe('registry remote display names', () => {expect(registry.byAgent('after-reused-pid-lock')).toBeTruthy()})+ it.each([['no owner file', null], ['an empty owner file', ''], ['an owner file naming no one', '{}']])(+ 'reclaims an old lock with %s, and leaves a fresh one to its creator', async (_label, owner) => {+ const { registry } = await loadRegistryModule()+ registry.load()+ const lockDir = join(dataDir, 'registry.json.lock')+ const leave = (age: number) => {+ mkdirSync(lockDir, { mode: 0o700 })+ if (owner !== null) writeFileSync(join(lockDir, 'owner.json'), owner, { mode: 0o600 })+ const at = (Date.now() - age) / 1000+ if (owner !== null) utimesSync(join(lockDir, 'owner.json'), at, at)+ utimesSync(lockDir, at, at)+ }+ leave(60_000)+ expect(registry.openProcessAgent({ agentId: 'after-ownerless-lock', engine: 'claude', tmuxPane: '%34',+ processIdentity: processIdentity(834) })?.entry.agentId).toBe('after-ownerless-lock')+ expect(existsSync(lockDir)).toBe(false)+ leave(0)+ registry.openProcessAgent({ agentId: 'during-fresh-lock', engine: 'claude', tmuxPane: '%35', processIdentity: processIdentity(835) })+ expect(existsSync(lockDir)).toBe(true) // its creator may be writing owner.json right now: never taken from it+ expect(readFileSync(join(dataDir, 'registry.json'), 'utf8')).not.toContain('during-fresh-lock')+ rmSync(lockDir, { recursive: true, force: true })+ })+it('deduplicates nested panes by process identity', async () => {const { registry } = await loadRegistryModule()registry.load()diff --git a/cli/src/lib/registry.ts b/cli/src/lib/registry.tsindex 1ad9ce005..0c66644ec 100644--- a/cli/src/lib/registry.ts+++ b/cli/src/lib/registry.ts@@ -411,6 +411,10 @@ function normalizedAgentEngine(value: unknown): AgentEngine {}const LOCK_WAIT_MS = 20const LOCK_ATTEMPTS = 100+/** A lock directory with no owner.json, or one that names no one, is a crash (or a full disk) between mkdir and the+ * owner write: a window of microseconds. Older than this it is debris, not a lock (daemonSpawnLock.ts does the same);+ * left alone it made every registry write fail with "registry lock is busy" until someone removed it by hand. */+const OWNERLESS_STALE_MS = 5_000function sleepSync(ms: number): void {const view = new Int32Array(new SharedArrayBuffer(4))@@ -435,11 +439,18 @@ function withRegistryFileLock<T>(apply: () => T): T {mkdirSync(LOCK_DIR, { mode: 0o700 })created = trueconst owner = join(LOCK_DIR, 'owner.json')- const fd = openSync(owner, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600)try {- writeFileSync(fd, JSON.stringify({ pid: process.pid, ...processIdentity, token }))- fsyncSync(fd)- } finally { closeSync(fd) }+ const fd = openSync(owner, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW, 0o600)+ try {+ writeFileSync(fd, JSON.stringify({ pid: process.pid, ...processIdentity, token }))+ fsyncSync(fd)+ } finally { closeSync(fd) }+ } catch (ownerError) {+ // The directory is ours (mkdir made it) but names no one: remove it whole, or it outlives this process ownerless.+ rmSync(LOCK_DIR, { recursive: true, force: true })+ created = false+ throw ownerError+ }try {return apply()} finally {@@ -451,11 +462,14 @@ function withRegistryFileLock<T>(apply: () => T): T {let ownerPid = 0let ownerStartMarker = ''let ownerToken = ''+ let since = Date.now()try {const stat = lstatSync(LOCK_DIR)+ since = stat.mtimeMsif (!stat.isDirectory() || stat.isSymbolicLink() || (uid !== null && stat.uid !== uid)|| (stat.mode & 0o777) !== 0o700) throw new Error('registry lock has unsafe owner, mode, or type')const ownerStat = lstatSync(join(LOCK_DIR, 'owner.json'))+ since = Math.max(since, ownerStat.mtimeMs)if (!ownerStat.isFile() || ownerStat.isSymbolicLink() || (uid !== null && ownerStat.uid !== uid)|| (ownerStat.mode & 0o777) !== 0o600) throw new Error('registry lock owner has unsafe owner, mode, or type')const owner = JSON.parse(readFileSync(join(LOCK_DIR, 'owner.json'), 'utf8')) as {@@ -467,7 +481,12 @@ function withRegistryFileLock<T>(apply: () => T): T {} catch (inspectionError) {if (inspectionError instanceof Error && inspectionError.message.startsWith('registry lock')) throw inspectionError}- if (ownerPid > 0 && ownerToken && !lockOwnerAlive(ownerPid, ownerStartMarker)) {+ if (!(ownerPid > 0 && ownerToken)) {+ if (Date.now() - since > OWNERLESS_STALE_MS) {+ rmSync(LOCK_DIR, { recursive: true, force: true })+ continue+ }+ } else if (!lockOwnerAlive(ownerPid, ownerStartMarker)) {try {const current = JSON.parse(readFileSync(join(LOCK_DIR, 'owner.json'), 'utf8')) as {pid?: unknown; startMarker?: unknown; generationMarker?: unknown; token?: unknown