@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
57 lines • 2.82 kB
JavaScript
import { appendFileSync } from "node:fs";
// Healthy first navigations on windows-latest measured p95 3.4 s and at most 18 s (the first Chrome
// of a cold runner); two bounded launches fit inside the eval cell's 60 s default budget.
export const DEFAULT_READINESS = { attachBoundMs: 20_000, launchAttempts: 2 };
export const READINESS_URL = "about:blank";
/** Appends one line per launch outcome to the file `SENPI_WEBVIEW_READINESS_LOG` names (CI diagnostics). */
export function readinessLogFromEnvironment() {
const path = process.env.SENPI_WEBVIEW_READINESS_LOG;
if (!path)
return undefined;
return (event) => {
const outcome = event.type === "ready"
? `ready launch=${event.launch} attachMs=${event.attachMs}`
: `stalled phase=${event.phase} launch=${event.launch} (Chrome retired)`;
appendFileSync(path, `${new Date().toISOString()} pid=${process.pid} webview ${outcome}\n`);
};
}
/** Appends one line per failed Chrome retirement to the same CI diagnostics file (senpi#2993). */
export function retireFailureLogFromEnvironment() {
const path = process.env.SENPI_WEBVIEW_READINESS_LOG;
if (!path)
return undefined;
return (message) => {
appendFileSync(path, `${new Date().toISOString()} pid=${process.pid} webview retire-failed ${JSON.stringify(message)}\n`);
};
}
export class WebViewNotReadyError extends Error {
constructor(launches, policy) {
super(`WebView never became ready: phase cdp-target-attach (the new view did not finish its first navigation within ${policy.attachBoundMs} ms) on ${launches} launch(es); each stalled launch was retired`);
this.code = "ERR_WEBVIEW_NOT_READY";
this.phase = "cdp-target-attach";
this.name = "WebViewNotReadyError";
}
}
export function timedDeadline(boundMs) {
let timer;
const reached = new Promise((resolve) => {
timer = setTimeout(resolve, boundMs);
});
return { reached, cancel: () => clearTimeout(timer) };
}
/** Resolves the readiness navigation's duration once it settled, undefined when the deadline came first. */
export async function attachedWithin(view, deadline) {
const startedAt = performance.now();
const bound = deadline.reached.then(() => undefined);
// Bun throws synchronously for some invalid states; the async wrapper turns that into a rejection.
const navigation = (async () => await view.navigate(READINESS_URL))().then(() => Math.round(performance.now() - startedAt));
try {
return await Promise.race([navigation, bound]);
}
finally {
deadline.cancel();
// A stalled navigation is abandoned; closing its view rejects it with nobody left waiting.
navigation.catch(() => { });
}
}
//# sourceMappingURL=webview-readiness.js.map