Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions packages/opencode/src/tests/custody-handle-manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1655,27 +1655,30 @@ describe('withCustodyManifestLock', () => {
test.serial('reports a held lock as lock_busy', async () => {
await withTempDirectory(async (directory) => {
const path = join(directory, 'handles.json')
const firstEntered = Promise.withResolvers<void>()
const releaseFirst = Promise.withResolvers<void>()
const lockPath = `${path}.lock`
const times = [0, 29, 30]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This test only passes because of the exact count and order of now() invocations inside the production retry loop (startedAt, then stale-check before the two deadline checks), and at the decision moment the owner is already stale (30-0 >= ttl 30). A static owner record means the "holder remains fresh" scenario the PR describes (issue #220: fresh holder offset by renewal) is no longer exercised at all, and any refactor that samples now() once per loop iteration (e.g. const current = now()) makes iteration 2 see stale owner 30 -> evict -> lock acquired -> test fails with 'acquired', recreating exactly the flake class this PR removes. Recommend keeping a live holder: start a real withCustodyManifestLock renewal (renewalIntervalMs) on the same scripted clock so writeOwner(now()) rewrites claimed_at_ms to fresh values that stay below the staleness threshold until after the contender's deadline throw, making the busy decision deterministic without depending on now() call order.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/opencode/src/tests/custody-handle-manifest.test.ts, line 1659:

<comment>This test only passes because of the exact count and order of `now()` invocations inside the production retry loop (startedAt, then stale-check before the two deadline checks), and at the decision moment the owner is already stale (30-0 >= ttl 30). A static owner record means the "holder remains fresh" scenario the PR describes (issue #220: fresh holder offset by renewal) is no longer exercised at all, and any refactor that samples `now()` once per loop iteration (e.g. `const current = now()`) makes iteration 2 see stale owner 30 -> evict -> lock acquired -> test fails with 'acquired', recreating exactly the flake class this PR removes. Recommend keeping a live holder: start a real `withCustodyManifestLock` renewal (renewalIntervalMs) on the same scripted clock so `writeOwner(now())` rewrites `claimed_at_ms` to fresh values that stay below the staleness threshold until after the contender's deadline throw, making the busy decision deterministic without depending on `now()` call order.</comment>

<file context>
@@ -1655,27 +1655,30 @@ describe('withCustodyManifestLock', () => {
-      const firstEntered = Promise.withResolvers<void>()
-      const releaseFirst = Promise.withResolvers<void>()
+      const lockPath = `${path}.lock`
+      const times = [0, 29, 30]
+      let timeIndex = 0
+      await fs.mkdir(lockPath, { mode: 0o700 })
</file context>

let timeIndex = 0
await fs.mkdir(lockPath, { mode: 0o700 })
await fs.writeFile(
join(lockPath, 'owner'),
`${JSON.stringify({
tenant: 'anthropic-auth',
pid: process.pid,
claimed_at_ms: 0,
nonce: 'held-test',
})}\n`,
)
__setCustodyManifestLockTestOptions({
ttlMs: 30,
retryMinMs: 1,
retryMaxMs: 1,
renewalIntervalMs: 5,
})
const first = withCustodyManifestLock(path, async () => {
firstEntered.resolve()
await releaseFirst.promise
now: () => times[Math.min(timeIndex++, times.length - 1)]!,
})
try {
await firstEntered.promise
await expect(
withCustodyManifestLock(path, async () => 'acquired'),
).rejects.toMatchObject({ code: 'lock_busy' })
} finally {
releaseFirst.resolve()
await first
}

await expect(
withCustodyManifestLock(path, async () => 'acquired'),
).rejects.toMatchObject({ code: 'lock_busy' })
await expect(fs.lstat(lockPath)).resolves.toBeDefined()
})
})

Expand Down
Loading