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.

Registry lock waits 2 s on the clock, so its lock tests pass on agent machines (#4370) (#3502)

0xken15aa04a75f7e1d
diff --git a/cli/src/lib/registry.spec.ts b/cli/src/lib/registry.spec.ts
index 3383c69c8..4d4f6e3de 100644
--- a/cli/src/lib/registry.spec.ts
+++ b/cli/src/lib/registry.spec.ts
@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
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'
+import { processLockIdentity, processStartMarker } from './processLiveness.js'
import type { AgentEngine } from '../engines/types.js'
import type { ProcessIdentity, RegisteredSession, RegisterInput } from './registry.js'
@@ -63,6 +63,16 @@ async function loadRegistryModule() {
return import('./registry.js')
}
+/** Each pause of the registry lock's wait moves the fake clock by `taken(asked)` ms and returns at once: the wait is
+ * then measured on the clock alone. A real pause can take far longer than asked: on the Mac Studio, 2026-11-07, an
+ * agent's 20 ms `Atomics.wait` returned after ~150 ms. */
+function pausesTake(taken: (asked: number) => number): void {
+ vi.spyOn(Atomics, 'wait').mockImplementation((_view, _index, _value, asked) => {
+ vi.setSystemTime(Date.now() + taken(asked ?? 0))
+ return 'timed-out'
+ })
+}
+
describe('registry remote display names', () => {
beforeEach(() => {
vi.useFakeTimers()
@@ -70,6 +80,7 @@ describe('registry remote display names', () => {
})
afterEach(() => {
+ vi.restoreAllMocks()
vi.useRealTimers()
rmSync(dataDir, { recursive: true, force: true })
delete process.env.ADAPTER_DATA_DIR
@@ -515,6 +526,7 @@ describe('registry remote display names', () => {
'reclaims an old lock with %s, and leaves a fresh one to its creator', async (_label, owner) => {
const { registry } = await loadRegistryModule()
registry.load()
+ pausesTake((asked) => asked)
const lockDir = join(dataDir, 'registry.json.lock')
const leave = (age: number) => {
mkdirSync(lockDir, { mode: 0o700 })
@@ -534,6 +546,26 @@ describe('registry remote display names', () => {
rmSync(lockDir, { recursive: true, force: true })
})
+ it('gives up on a live lock after 2 s on the clock, however long each pause really takes', async () => {
+ const { registry } = await loadRegistryModule()
+ registry.load()
+ const lockDir = join(dataDir, 'registry.json.lock')
+ mkdirSync(lockDir, { mode: 0o700 })
+ writeFileSync(join(lockDir, 'owner.json'), JSON.stringify({
+ pid: process.pid, ...processLockIdentity(process.pid), token: 'live-holder',
+ }), { mode: 0o600 })
+ pausesTake((asked) => asked * 7.5)
+ const started = Date.now()
+
+ registry.openProcessAgent({ agentId: 'behind-a-live-lock', engine: 'claude', tmuxPane: '%36', processIdentity: processIdentity(836) })
+
+ expect(Date.now() - started).toBeLessThanOrEqual(2_000 + 150)
+ expect(readFileSync(join(lockDir, 'owner.json'), 'utf8')).toContain('live-holder')
+ expect(existsSync(join(dataDir, 'registry.json'))
+ && readFileSync(join(dataDir, 'registry.json'), 'utf8').includes('behind-a-live-lock')).toBe(false)
+ 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.ts
index dd3728f19..20dbc5723 100644
--- a/cli/src/lib/registry.ts
+++ b/cli/src/lib/registry.ts
@@ -446,7 +446,9 @@ function normalizedAgentEngine(value: unknown): AgentEngine {
: 'claude'
}
const LOCK_WAIT_MS = 20
-const LOCK_ATTEMPTS = 100
+/** How long a write waits for a live holder, on the clock: a pause can take far longer than asked (an agent's process
+ * on the Mac Studio, 2026-11-07: 20 ms took ~150 ms), and counted as 100 pauses the wait blocked for ~15 s, not 2. */
+const LOCK_BUDGET_MS = 2_000
/** 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. */
@@ -468,7 +470,8 @@ function withRegistryFileLock<T>(apply: () => T): T {
secureStateDirectory(env.ADAPTER_DATA_DIR)
const uid = typeof process.getuid === 'function' ? process.getuid() : null
const processIdentity = processLockIdentity(process.pid)
- for (let attempt = 0; attempt < LOCK_ATTEMPTS; attempt++) {
+ const deadline = Date.now() + LOCK_BUDGET_MS
+ for (;;) {
const token = randomUUID()
let created = false
try {
@@ -495,6 +498,7 @@ function withRegistryFileLock<T>(apply: () => T): T {
} catch (error) {
if (created) removeRegistryLockOwnedBy(token)
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') throw error
+ if (Date.now() >= deadline) throw new Error('registry lock is busy')
let ownerPid = 0
let ownerStartMarker = ''
let ownerToken = ''
@@ -539,7 +543,6 @@ function withRegistryFileLock<T>(apply: () => T): T {
sleepSync(LOCK_WAIT_MS)
}
}
- throw new Error('registry lock is busy')
}
function rowId(row: unknown): string {