fix: wait for a busy Codex lease instead of skipping the review

The Codex auth lease is exclusive per account so two Codex processes never
rotate the same refresh token at once. With the single configured account that
also serializes every Codex run, and claimCodexAuthLease() failed immediately
while another room's Codex run held it: the turn errored with "Codex rotation
pool unavailable", the task closed as reviewer_codex_unavailable and the
review was silently skipped.

All three such failures on 2026-09-28 coincided exactly with another room's
Codex run holding the lease (10:25:55, 18:08:25, 18:22:50 KST).

runAgentProcess now waits (up to 15 min, polling every 2s) when the only
obstacle is a lease held by a live run. A dead or rate-limited pool still
fails fast, and the normal path adds no async hop because the wait is only
entered when the lease is actually busy. The exclusivity itself is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-10-01 14:25:14 +09:00
parent 8b688853cc
commit 30ba51016a
3 changed files with 187 additions and 1 deletions

View File

@@ -434,3 +434,90 @@ describe('codex-token-rotation single-account fallback', () => {
]);
});
});
describe('codex-token-rotation waitForCodexAuthLease (single account)', () => {
let tempHome: string;
beforeEach(() => {
vi.resetModules();
tempHome = fs.mkdtempSync(path.join('/tmp', 'ejclaw-codex-rot-'));
process.env.CODEX_ROT_TEST_HOME = tempHome;
createFakeAccounts(tempHome, 1);
});
afterEach(() => {
delete process.env.CODEX_ROT_TEST_HOME;
fs.rmSync(tempHome, { recursive: true, force: true });
});
it('reports lease availability synchronously', async () => {
const mod = await import('./codex-token-rotation.js');
mod.initCodexTokenRotation();
expect(mod.isCodexAuthLeaseAvailable()).toBe(true);
const held = mod.claimCodexAuthLease();
expect(mod.isCodexAuthLeaseAvailable()).toBe(false);
held?.release();
expect(mod.isCodexAuthLeaseAvailable()).toBe(true);
});
it('returns immediately when the account is free', async () => {
const mod = await import('./codex-token-rotation.js');
mod.initCodexTokenRotation();
const sleep = vi.fn(async () => {});
await expect(mod.waitForCodexAuthLease({ sleep })).resolves.toBe(true);
expect(sleep).not.toHaveBeenCalled();
});
it('waits for a concurrent run to release the lease instead of failing', async () => {
const mod = await import('./codex-token-rotation.js');
mod.initCodexTokenRotation();
// Another room's Codex run holds the only account. Before the wait, a
// second claim fails outright — that was the skipped-review bug.
const held = mod.claimCodexAuthLease();
expect(held).not.toBeNull();
expect(mod.claimCodexAuthLease()).toBeNull();
let polls = 0;
const sleep = vi.fn(async () => {
polls += 1;
if (polls === 3) held?.release();
});
await expect(mod.waitForCodexAuthLease({ sleep, pollMs: 1 })).resolves.toBe(
true,
);
expect(polls).toBe(3);
const next = mod.claimCodexAuthLease();
expect(next).not.toBeNull();
next?.release();
});
it('gives up after the timeout while the lease stays held', async () => {
const mod = await import('./codex-token-rotation.js');
mod.initCodexTokenRotation();
const held = mod.claimCodexAuthLease();
try {
await expect(
mod.waitForCodexAuthLease({
timeoutMs: 0,
sleep: async () => {},
}),
).resolves.toBe(false);
} finally {
held?.release();
}
});
it('does not wait when the account is exhausted rather than leased', async () => {
const mod = await import('./codex-token-rotation.js');
mod.initCodexTokenRotation();
mod.updateCodexAccountUsage(80, undefined, 0, 100, undefined);
const sleep = vi.fn(async () => {});
await expect(mod.waitForCodexAuthLease({ sleep })).resolves.toBe(false);
expect(sleep).not.toHaveBeenCalled();
});
});