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
4 changes: 2 additions & 2 deletions .github/workflows/nightly-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -959,8 +959,8 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}
TELEGRAM_BOT_TOKEN_A: "test-fake-token-A-rotation-e2e"
TELEGRAM_BOT_TOKEN_B: "test-fake-token-B-rotation-e2e"
DISCORD_BOT_TOKEN_A: "test-fake-discord-A-rotation-e2e"
DISCORD_BOT_TOKEN_B: "test-fake-discord-B-rotation-e2e"
DISCORD_BOT_TOKEN_A: "discord-a"
DISCORD_BOT_TOKEN_B: "discord-b"
SLACK_BOT_TOKEN_A: "xoxb-fake-A-rotation-e2e"
SLACK_BOT_TOKEN_B: "xoxb-fake-B-rotation-e2e"
SLACK_APP_TOKEN_A: "xapp-fake-A-rotation-e2e"
Expand Down
18 changes: 8 additions & 10 deletions src/lib/actions/sandbox/process-recovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { OPENSHELL_PROBE_TIMEOUT_MS } from "../../adapters/openshell/timeouts";
import * as agentRuntime from "../../agent/runtime";
import { G, R } from "../../cli/terminal-style";
import { DASHBOARD_PORT } from "../../core/ports";
import { sleepSeconds } from "../../core/wait";
import { sleepSeconds, waitUntil } from "../../core/wait";
import { ROOT, shellQuote } from "../../runner";
import * as registry from "../../state/registry";
import { parseForwardList } from "../../state/sandbox-session";
Expand Down Expand Up @@ -321,15 +321,13 @@ function waitForRecoveredSandboxGateway(sandboxName: string): boolean {
? Math.max(1, Math.floor(timeoutSeconds / intervalSeconds) + 1)
: Math.max(1, Math.floor(timeoutSeconds) + 1);

for (let attempt = 0; attempt < attempts; attempt += 1) {
if (isSandboxGatewayRunning(sandboxName) === true) {
return true;
}
if (attempt < attempts - 1) {
sleepSeconds(intervalSeconds);
}
}
return false;
return waitUntil(() => isSandboxGatewayRunning(sandboxName) === true, {
initialIntervalMs: intervalSeconds * 1000,
maxIntervalMs: intervalSeconds * 1000,
backoffFactor: 1,
maxAttempts: attempts,
sleep: (ms) => sleepSeconds(ms / 1000),
});
}

/**
Expand Down
225 changes: 219 additions & 6 deletions src/lib/core/wait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@
import { buildValidatedCurlCommandArgs } from "../adapters/http/curl-args";
import { withLocalNoProxy } from "../subprocess-env.js";

export type WaitUntilOptions = {
/** Absolute deadline, in milliseconds, using the same clock as `now`. */
deadlineMs?: number;
/** First delay between failed attempts. */
initialIntervalMs?: number;
/** Maximum delay between failed attempts after backoff. */
maxIntervalMs?: number;
/** Multiplier applied to the interval after each failed attempt. */
backoffFactor?: number;
/** Optional cap on condition attempts, including the first immediate check. */
maxAttempts?: number;
/** Clock used for deadline comparisons. Defaults to Date.now. */
now?: () => number;
/** Sleep function. Defaults to sleepMs for waitUntil and sleepMsAsync for waitUntilAsync. */
sleep?: (ms: number) => void;
};

type NormalizedWaitUntilOptions = {
deadlineMs: number;
intervalMs: number;
maxIntervalMs: number;
backoffFactor: number;
maxAttempts: number;
hasAttemptCap: boolean;
now: () => number;
sleep: (ms: number) => void | Promise<void>;
};

const DEFAULT_TIMEOUT_SECONDS = 10;
const DEFAULT_INITIAL_INTERVAL_MS = 250;
const DEFAULT_MAX_INTERVAL_MS = 5_000;
const DEFAULT_BACKOFF_FACTOR = 1.5;
const MIN_UNCAPPED_SLEEP_MS = 1;

/**
* Build a curl-friendly env that injects NO_PROXY for loopback hosts so that
* probes against localhost-bound services (Ollama, gateway, dashboard, etc.)
Expand All @@ -32,27 +66,206 @@ export function sleepMs(ms: number): void {
Atomics.wait(buffer, 0, 0, ms);
}

/**
* Asynchronously sleep for the given number of milliseconds.
*/
export function sleepMsAsync(ms: number): Promise<void> {
if (ms <= 0 || !Number.isFinite(ms)) return Promise.resolve();
return new Promise((resolve) => setTimeout(resolve, ms));
}

/**
* Synchronously sleep for the given number of seconds.
*/
export function sleepSeconds(seconds: number): void {
sleepMs(seconds * 1000);
}

function positiveFiniteOr(value: number | undefined, fallback: number): number {
return value !== undefined && Number.isFinite(value) && value > 0 ? value : fallback;
}

function nonNegativeFiniteOr(value: number | undefined, fallback: number): number {
return value !== undefined && Number.isFinite(value) && value >= 0 ? value : fallback;
}

function normalizeWaitUntilOptions(
optionsOrTimeout?: WaitUntilOptions | number,
pollIntervalMs = DEFAULT_INITIAL_INTERVAL_MS,
defaultSleep: (ms: number) => void | Promise<void> = sleepMs,
): NormalizedWaitUntilOptions {
if (typeof optionsOrTimeout === "number" || optionsOrTimeout === undefined) {
const now = Date.now;
const timeoutSeconds =
optionsOrTimeout === undefined
? DEFAULT_TIMEOUT_SECONDS
: nonNegativeFiniteOr(optionsOrTimeout, DEFAULT_TIMEOUT_SECONDS);
const currentMs = now();
const pollMs = nonNegativeFiniteOr(pollIntervalMs, DEFAULT_INITIAL_INTERVAL_MS);
return {
deadlineMs: Number.isFinite(currentMs)
? currentMs + timeoutSeconds * 1000
: Number.NEGATIVE_INFINITY,
intervalMs: pollMs,
maxIntervalMs: pollMs,
backoffFactor: 1,
maxAttempts: Number.POSITIVE_INFINITY,
hasAttemptCap: false,
now,
sleep: defaultSleep,
};
}

const now = optionsOrTimeout.now ?? Date.now;
const maxIntervalMs = nonNegativeFiniteOr(
optionsOrTimeout.maxIntervalMs,
DEFAULT_MAX_INTERVAL_MS,
);
const maxAttempts =
optionsOrTimeout.maxAttempts !== undefined && Number.isFinite(optionsOrTimeout.maxAttempts)
? Math.max(0, Math.floor(optionsOrTimeout.maxAttempts))
: Number.POSITIVE_INFINITY;
const hasAttemptCap = Number.isFinite(maxAttempts);
const deadlineMs =
optionsOrTimeout.deadlineMs === undefined
? Number.POSITIVE_INFINITY
: Number(optionsOrTimeout.deadlineMs);

if (Number.isNaN(deadlineMs) || deadlineMs === Number.NEGATIVE_INFINITY) {
throw new TypeError("waitUntil requires a valid deadlineMs");
}
if (deadlineMs === Number.POSITIVE_INFINITY && !hasAttemptCap) {
throw new TypeError("waitUntil requires deadlineMs or maxAttempts");
}

return {
deadlineMs,
intervalMs: Math.min(
nonNegativeFiniteOr(optionsOrTimeout.initialIntervalMs, DEFAULT_INITIAL_INTERVAL_MS),
maxIntervalMs,
),
maxIntervalMs,
backoffFactor: Math.max(
1,
positiveFiniteOr(optionsOrTimeout.backoffFactor, DEFAULT_BACKOFF_FACTOR),
),
maxAttempts,
hasAttemptCap,
now,
sleep: optionsOrTimeout.sleep ?? defaultSleep,
};
}

function boundedSleepDurationMs(
intervalMs: number,
remainingMs: number,
hasAttemptCap: boolean,
): number {
const requestedSleepMs = Math.min(intervalMs, remainingMs);
const sleepDurationMs =
!hasAttemptCap && requestedSleepMs <= 0 ? MIN_UNCAPPED_SLEEP_MS : requestedSleepMs;
return Math.min(sleepDurationMs, remainingMs);
}

/**
* Synchronously wait until a condition is met.
*/
export function waitUntil(conditionFn: () => boolean): boolean;
export function waitUntil(conditionFn: () => boolean, options: WaitUntilOptions): boolean;
export function waitUntil(
conditionFn: () => boolean,
timeoutSeconds = 10,
pollIntervalMs = 250,
timeoutSeconds?: number,
pollIntervalMs?: number,
): boolean;
export function waitUntil(
conditionFn: () => boolean,
optionsOrTimeout?: WaitUntilOptions | number,
pollIntervalMs?: number,
): boolean {
const start = Date.now();
while (Date.now() - start < timeoutSeconds * 1000) {
const options = normalizeWaitUntilOptions(optionsOrTimeout, pollIntervalMs);
let attempts = 0;
let intervalMs = options.intervalMs;

for (;;) {
const currentMs = options.now();
if (!Number.isFinite(currentMs) || currentMs >= options.deadlineMs) {
return false;
}
if (attempts >= options.maxAttempts) {
return false;
}
attempts += 1;

if (conditionFn()) return true;
sleepMs(pollIntervalMs);
if (attempts >= options.maxAttempts) {
return false;
}

const postConditionMs = options.now();
if (!Number.isFinite(postConditionMs) || postConditionMs >= options.deadlineMs) {
return false;
}
options.sleep(
boundedSleepDurationMs(
intervalMs,
options.deadlineMs - postConditionMs,
options.hasAttemptCap,
),
);
intervalMs = Math.min(options.maxIntervalMs, intervalMs * options.backoffFactor);
}
}

/**
* Asynchronously wait until a condition is met.
*/
export function waitUntilAsync(conditionFn: () => boolean | Promise<boolean>): Promise<boolean>;
export function waitUntilAsync(
conditionFn: () => boolean | Promise<boolean>,
options: WaitUntilOptions,
): Promise<boolean>;
export function waitUntilAsync(
conditionFn: () => boolean | Promise<boolean>,
timeoutSeconds?: number,
pollIntervalMs?: number,
): Promise<boolean>;
export async function waitUntilAsync(
conditionFn: () => boolean | Promise<boolean>,
optionsOrTimeout?: WaitUntilOptions | number,
pollIntervalMs?: number,
): Promise<boolean> {
const options = normalizeWaitUntilOptions(optionsOrTimeout, pollIntervalMs, sleepMsAsync);
let attempts = 0;
let intervalMs = options.intervalMs;

for (;;) {
const currentMs = options.now();
if (!Number.isFinite(currentMs) || currentMs >= options.deadlineMs) {
return false;
}
if (attempts >= options.maxAttempts) {
return false;
}
attempts += 1;

if (await conditionFn()) return true;
if (attempts >= options.maxAttempts) {
return false;
}

const postConditionMs = options.now();
if (!Number.isFinite(postConditionMs) || postConditionMs >= options.deadlineMs) {
return false;
}
await options.sleep(
boundedSleepDurationMs(
intervalMs,
options.deadlineMs - postConditionMs,
options.hasAttemptCap,
),
);
intervalMs = Math.min(options.maxIntervalMs, intervalMs * options.backoffFactor);
}
return false;
}

// One-shot TCP reachability probe, evaluated in a short-lived Node subprocess.
Expand Down
35 changes: 35 additions & 0 deletions src/lib/inference/local-adapter-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
probeLocalAdapterHealth,
readLocalAdapterJsonFile,
readLocalAdapterTextFile,
waitForLocalAdapterHealth,
writeLocalAdapterJsonFile,
writeLocalAdapterSecretFile,
} from "../../../dist/lib/inference/local-adapter-lifecycle";
Expand Down Expand Up @@ -151,3 +152,37 @@ describe("ensureLocalAdapterStateDir", () => {
expect(stat.mode & 0o777).toBe(0o700);
});
});

describe("waitForLocalAdapterHealth", () => {
it("retries async health probes until the adapter responds", async () => {
let calls = 0;

await expect(
waitForLocalAdapterHealth(
async () => {
calls += 1;
return calls >= 3;
},
{ attempts: 5, intervalMs: 1 },
),
).resolves.toBe(true);

expect(calls).toBe(3);
});

it("returns false after the configured attempt budget is exhausted", async () => {
let calls = 0;

await expect(
waitForLocalAdapterHealth(
async () => {
calls += 1;
return false;
},
{ attempts: 2, intervalMs: 1 },
),
).resolves.toBe(false);

expect(calls).toBe(2);
});
});
13 changes: 8 additions & 5 deletions src/lib/inference/local-adapter-lifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import http from "node:http";
import os from "node:os";
import path from "node:path";

import { waitUntilAsync } from "../core/wait";

export type JsonObject = Record<string, unknown>;

export type RunCaptureFn = (
Expand Down Expand Up @@ -195,9 +197,10 @@ export async function waitForLocalAdapterHealth(
): Promise<boolean> {
const attempts = options.attempts || 20;
const intervalMs = options.intervalMs || 100;
for (let attempt = 0; attempt < attempts; attempt++) {
if (await probe()) return true;
await new Promise((resolve) => setTimeout(resolve, intervalMs));
}
return false;
return waitUntilAsync(probe, {
initialIntervalMs: intervalMs,
maxIntervalMs: intervalMs,
backoffFactor: 1,
maxAttempts: attempts,
});
}
Loading
Loading