Skip to content
Draft
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
27 changes: 26 additions & 1 deletion packages/cli/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,13 +594,38 @@ describe('@codor/cli', () => {
await once(listener, 'close');
}

const sleeps: number[] = [];
await expect(waitForCodor(
'http://127.0.0.1:65535',
async () => false,
async (milliseconds) => { sleeps.push(milliseconds); },
)).rejects.toThrow('if the port is listening, the service started');
expect(sleeps.length).toBeGreaterThan(19);
expect(sleeps.reduce((total, ms) => total + ms, 0)).toBe(60_000);
});

it('backs off progressively and resolves past the old 20-attempt limit', async () => {
let attempts = 0;
await expect(waitForCodor(
'http://127.0.0.1:65535',
async () => {
attempts += 1;
return attempts >= 25;
},
async () => {},
)).resolves.toBeUndefined();
expect(attempts).toBe(25);
});

it('caps readiness backoff at 1s after doubling from 250ms', async () => {
const sleeps: number[] = [];
await expect(waitForCodor(
'http://127.0.0.1:65535',
async () => false,
async (milliseconds) => { sleeps.push(milliseconds); },
)).rejects.toThrow('did not become ready');
expect(sleeps).toEqual(Array.from({ length: 19 }, () => 250));
expect(sleeps.slice(0, 3)).toEqual([250, 500, 1_000]);
expect(sleeps.slice(2, -1).every((ms) => ms === 1_000)).toBe(true);
});

posixHostIt('writes private setup files, runs confirmed host steps, and pairs against the Serve origin', async () => {
Expand Down
18 changes: 15 additions & 3 deletions packages/cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,17 +606,29 @@ export async function probeCodorStatus(endpoint: string): Promise<boolean> {
const defaultSleep = async (milliseconds: number): Promise<void> =>
new Promise((resolveSleep) => setTimeout(resolveSleep, milliseconds));

const READINESS_BUDGET_MS = 60_000;
const READINESS_INITIAL_DELAY_MS = 250;
const READINESS_MAX_DELAY_MS = 1_000;

// harn:assume setup-verifies-codor-before-creating-pairing-code ref=setup-readiness-and-pairing
export async function waitForCodor(
endpoint: string,
probe: (value: string) => Promise<boolean>,
sleep: (milliseconds: number) => Promise<void>,
): Promise<void> {
for (let attempt = 1; attempt <= 20; attempt += 1) {
let elapsedMs = 0;
let delayMs = READINESS_INITIAL_DELAY_MS;
for (;;) {
if (await probe(endpoint)) return;
if (attempt < 20) await sleep(250);
const wait = Math.min(delayMs, READINESS_BUDGET_MS - elapsedMs);
if (wait <= 0) break;
await sleep(wait);
elapsedMs += wait;
delayMs = Math.min(delayMs * 2, READINESS_MAX_DELAY_MS);
}
throw new Error(`Codor did not become ready at ${endpoint}; inspect the user-service logs`);
throw new Error(
`Codor did not become ready at ${endpoint}; if the port is listening, the service started — verify with \`codor channels\``,
);
}
// harn:end setup-verifies-codor-before-creating-pairing-code

Expand Down