Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
fix(ok): size the packaged Windows CDP wait from the app's own budget (#4271)
Nick Gomez71848caae813
diff --git a/packages/desktop/scripts/smoke-terminal-package-cdp.py b/packages/desktop/scripts/smoke-terminal-package-cdp.pyindex c7b77b3b6..52f52fed8 100755--- a/packages/desktop/scripts/smoke-terminal-package-cdp.py+++ b/packages/desktop/scripts/smoke-terminal-package-cdp.py@@ -6,35 +6,50 @@ import osimport sysimport timeimport urllib.request+from collections import Counterfrom typing import Dict, List, Optional, Tupleimport websocket-DEBUG_TARGETS_URL = "http://127.0.0.1:9222/json/list"+DEFAULT_DEBUG_TARGETS_URL = "http://127.0.0.1:9222/json/list"+DEFAULT_DISCOVERY_DEADLINE_MS = 20_000+DEFAULT_ECHO_DEADLINE_MS = 30_000+ECHO_REPORT_MARGIN_MS = 10_000MARKER = "OK_PACKAGED_PTY_ECHO"MAX_OBSERVED_TARGETS = 8MAX_TARGET_FIELD_LENGTH = 160-def summarize_target(target: Dict[str, object]) -> Tuple[str, str, str]:+def summarize_target(target: Dict[str, object], outcome: str) -> Tuple[str, str, str, str]:def bounded(field: str) -> str:return str(target.get(field, ""))[:MAX_TARGET_FIELD_LENGTH]- return (bounded("type"), bounded("url"), bounded("title"))+ return (bounded("type"), bounded("url"), bounded("title"), outcome)def record_observed_target(- observed_targets: List[Tuple[str, str, str]], target: Dict[str, object]+ observed_targets: List[Tuple[str, str, str, str]],+ target: Dict[str, object],+ outcome: str,) -> None:- summary = summarize_target(target)+ summary = summarize_target(target, outcome)if summary in observed_targets:observed_targets.remove(summary)observed_targets.append(summary)del observed_targets[:-MAX_OBSERVED_TARGETS]+class ConnectPhaseTimeoutError(TimeoutError):+ pass+++class ReplyPhaseTimeoutError(TimeoutError):+ pass++def evaluate_value(socket_url: str, expression: str, timeout: float = 5) -> object:+ deadline = time.monotonic() + timeoutconnection = websocket.create_connection(socket_url, timeout=timeout, origin="http://localhost")@@ -47,9 +62,22 @@ def evaluate_value(socket_url: str, expression: str, timeout: float = 5) -> obje"returnByValue": True,},}- connection.send(json.dumps(request))try:+ remaining = deadline - time.monotonic()+ if remaining <= 0:+ raise ConnectPhaseTimeoutError(+ f"connect and handshake spent the whole {timeout:.1f}s budget "+ "before the CDP request could be sent"+ )+ connection.settimeout(remaining)+ connection.send(json.dumps(request))while True:+ remaining = deadline - time.monotonic()+ if remaining <= 0:+ raise ReplyPhaseTimeoutError(+ f"no CDP reply for id=1 within {timeout:.1f}s"+ )+ connection.settimeout(remaining)message = json.loads(connection.recv())if message.get("id") != 1:continue@@ -67,19 +95,88 @@ def evaluate_value(socket_url: str, expression: str, timeout: float = 5) -> objeconnection.close()+def discovery_deadline_ms() -> int:+ return int(os.environ.get("OK_SMOKE_DISCOVERY_DEADLINE_MS") or DEFAULT_DISCOVERY_DEADLINE_MS)+++def echo_deadline_ms() -> int:+ return int(os.environ.get("OK_SMOKE_ECHO_DEADLINE_MS") or DEFAULT_ECHO_DEADLINE_MS)+++def renderer_echo_timeout_ms() -> int:+ budget = echo_deadline_ms()+ remaining = budget - ECHO_REPORT_MARGIN_MS+ if remaining <= 0:+ raise ValueError(+ f"OK_SMOKE_ECHO_DEADLINE_MS={budget} leaves no room for the renderer's own timer, "+ f"which must fire {ECHO_REPORT_MARGIN_MS}ms earlier so its partial-output message wins "+ f"the race against the socket bound; set it above {ECHO_REPORT_MARGIN_MS}"+ )+ return remaining+++def debug_targets_url() -> str:+ return os.environ.get("OK_SMOKE_CDP_LIST_URL") or DEFAULT_DEBUG_TARGETS_URL+++def describe_discovery_phase(+ budget_ms: int,+ elapsed: float,+ listed_at: Optional[float],+ paged_at: Optional[float],+ probes: int,+ targets_seen: int,+ list_failures: Counter,+ probe_failures: Counter,+) -> str:+ if listed_at is None:+ reached = "the debug endpoint never answered"+ elif paged_at is None:+ reached = f"the debug endpoint answered at {listed_at:.1f}s but listed no page target"+ elif probes == 0:+ reached = (+ f"the debug endpoint answered at {listed_at:.1f}s and a page target appeared at "+ f"{paged_at:.1f}s, but the budget ran out before it could be probed"+ )+ else:+ reached = (+ f"the debug endpoint answered at {listed_at:.1f}s, a page target appeared at "+ f"{paged_at:.1f}s, and {probes} probe(s) across {targets_seen} page target(s) "+ f"never reported an editor"+ )+ return (+ f"{reached}; gave up after {elapsed:.1f}s of a {budget_ms / 1000:.0f}s budget; "+ f"list_failures={dict(list_failures)}; probe_failures={dict(probe_failures)}"+ )++def find_editor_websocket() -> str:- deadline = time.monotonic() + 20- last_error: Optional[Exception] = None- observed_targets: List[Tuple[str, str, str]] = []+ budget_ms = discovery_deadline_ms()+ started = time.monotonic()+ deadline = started + budget_ms / 1000+ observed_targets: List[Tuple[str, str, str, str]] = []+ listed_at: Optional[float] = None+ paged_at: Optional[float] = None+ probes = 0+ probed_targets: set = set()+ list_failures: Counter = Counter()+ probe_failures: Counter = Counter()while time.monotonic() < deadline:try:- with urllib.request.urlopen(DEBUG_TARGETS_URL, timeout=2) as response:+ with urllib.request.urlopen(debug_targets_url(), timeout=2) as response:targets = json.load(response)+ if listed_at is None:+ listed_at = time.monotonic() - startedfor target in targets:socket_url = target.get("webSocketDebuggerUrl")if target.get("type") != "page" or not socket_url:continue- record_observed_target(observed_targets, target)+ if paged_at is None:+ paged_at = time.monotonic() - started+ if time.monotonic() >= deadline:+ break+ probes += 1+ probed_targets.add(summarize_target(target, "")[:3])try:is_project_editor = evaluate_value(str(socket_url),@@ -87,18 +184,38 @@ def find_editor_websocket() -> str:" && window.okDesktop.config.projectPath.length > 0",timeout=2,)+ record_observed_target(+ observed_targets,+ target,+ "editor" if is_project_editor is True else "not-editor",+ )if is_project_editor is True:return str(socket_url)+ except RuntimeError as error:+ record_observed_target(observed_targets, target, "answered-with-error")+ probe_failures[type(error).__name__] += 1+ except (ConnectPhaseTimeoutError, ReplyPhaseTimeoutError) as error:+ record_observed_target(observed_targets, target, "answered-but-silent")+ probe_failures[type(error).__name__] += 1except Exception as error:- # A renderer can disappear or still be loading while the- # project deep link replaces the initial Navigator window.- last_error = error- except Exception as error: # The app may still be creating its first page.- last_error = error+ record_observed_target(observed_targets, target, "unreachable")+ probe_failures[type(error).__name__] += 1+ except Exception as error:+ list_failures[type(error).__name__] += 1time.sleep(0.25)raise RuntimeError("no project editor debug target appeared; "- f"last_error={last_error!r}; observed_targets={observed_targets!r}"+ + describe_discovery_phase(+ budget_ms,+ time.monotonic() - started,+ listed_at,+ paged_at,+ probes,+ len(probed_targets),+ list_failures,+ probe_failures,+ )+ + f"; observed_targets={observed_targets!r}")@@ -113,11 +230,21 @@ def evaluate_pty_echo(socket_url: str) -> Dict[str, object]:throw new Error('packaged renderer did not expose its platform');}}const marker = {json.dumps(MARKER)};+ const startedAt = performance.now();return await new Promise(async (resolve, reject) => {{let output = '';let ptyId = null;let settled = false;let unsubscribe = () => {{}};+ let createdAt = null;+ let firstByteAt = null;+ let markerAt = null;+ const sinceStart = (at) => (at === null ? null : Math.round(at - startedAt));+ const timings = () => ({{+ createdMs: sinceStart(createdAt),+ firstByteMs: sinceStart(firstByteAt),+ markerMs: sinceStart(markerAt),+ }});const finish = async (error) => {{if (settled) return;settled = true;@@ -125,9 +252,10 @@ def evaluate_pty_echo(socket_url: str) -> Dict[str, object]:unsubscribe();if (ptyId !== null) await bridge.terminal.kill(ptyId).catch(() => {{}});if (error) reject(error);- else resolve({{ output, platform: bridge.platform }});+ else resolve({{ output, platform: bridge.platform, timings: timings() }});}};- const consume = (data) => {{+ const consume = (data, at) => {{+ if (firstByteAt === null) firstByteAt = at;output += data;const plainOutput = output.replace(@@ -143,15 +271,23 @@ def evaluate_pty_echo(socket_url: str) -> Dict[str, object]:plainOutput.match(/[^\\r\\n]*(?:\\r\\n|\\r|\\n)/g)?.some((line) => line.trim() === marker) ?? false;- if (markerReachedOutput) void finish(null);+ if (markerReachedOutput) {{+ markerAt = at;+ void finish(null);+ }}}};const timeout = setTimeout(- () => void finish(new Error(`PTY echo timed out; output=${{JSON.stringify(output)}}`)),- 20000,+ () =>+ void finish(+ new Error(+ `PTY echo timed out; output=${{JSON.stringify(output)}}; timings=${{JSON.stringify(timings())}}`,+ ),+ ),+ {renderer_echo_timeout_ms()},);unsubscribe = bridge.terminal.onData((message) => {{if (message.ptyId !== ptyId) return;- consume(message.data);+ consume(message.data, performance.now());}});const isWindows = bridge.platform === 'win32';const created = await bridge.terminal.create({{@@ -170,6 +306,7 @@ def evaluate_pty_echo(socket_url: str) -> Dict[str, object]:await finish(new Error(`PTY create failed: ${{created.reason}}`));return;}}+ createdAt = performance.now();ptyId = created.ptyId;const attached = await bridge.terminal.start(ptyId);if (!attached.ok) {{@@ -180,13 +317,29 @@ def evaluate_pty_echo(socket_url: str) -> Dict[str, object]:}});}})()"""- value = evaluate_value(socket_url, expression, timeout=30)+ budget_ms = echo_deadline_ms()+ started = time.monotonic()+ try:+ value = evaluate_value(socket_url, expression, timeout=budget_ms / 1000)+ except Exception as error:+ raise RuntimeError(+ f"the editor target did not report a PTY echo; "+ f"gave up after {time.monotonic() - started:.1f}s of a {budget_ms / 1000:.0f}s budget; "+ f"renderer_timer={renderer_echo_timeout_ms() / 1000:.0f}s; "+ f"error={type(error).__name__}: {error}"+ ) from errorif not isinstance(value, dict):raise RuntimeError(f"renderer returned no PTY smoke result: {value}")return valuedef main() -> int:+ try:+ discovery_deadline_ms()+ renderer_echo_timeout_ms()+ except ValueError as error:+ print(f"ERROR: packaged PTY smoke misconfigured: {error}", file=sys.stderr)+ return 1try:result = evaluate_pty_echo(find_editor_websocket())except Exception as error:@@ -202,7 +355,7 @@ def main() -> int:file=sys.stderr,)return 1- print(f"PTY echo marker observed: {MARKER}")+ print(f"PTY echo marker observed: {MARKER}; timings={json.dumps(result.get('timings'))}")return 0diff --git a/packages/desktop/scripts/smoke-windows-terminal-package.mjs b/packages/desktop/scripts/smoke-windows-terminal-package.mjsindex 8e18eaa1f..9513a4ef3 100644--- a/packages/desktop/scripts/smoke-windows-terminal-package.mjs+++ b/packages/desktop/scripts/smoke-windows-terminal-package.mjs@@ -14,6 +14,56 @@ import {import { tmpdir } from 'node:os';import { dirname, join, resolve } from 'node:path';import { fileURLToPath, pathToFileURL } from 'node:url';+import {+ SPAWN_STARTUP_DEADLINE_MS,+ SPAWN_WAIT_EXTENSION_FACTOR,+} from '../src/shared/boot-narration.ts';++export const PACKAGED_BOOT_ENVELOPE_MS = 10_000;+export const PACKAGED_PTY_ECHO_BUDGET_MS = 30_000;+export const PACKAGED_DISCOVERY_OVERRUN_MS = 3_000;+export const PACKAGED_DRIVER_MARGIN_MS = 5_000;++export function packagedStartupBoundMs(+ spawnStartupDeadlineMs = SPAWN_STARTUP_DEADLINE_MS,+ spawnWaitExtensionFactor = SPAWN_WAIT_EXTENSION_FACTOR,+) {+ return spawnStartupDeadlineMs * spawnWaitExtensionFactor;+}++export function packagedDiscoveryDeadlineMs(+ spawnStartupDeadlineMs = SPAWN_STARTUP_DEADLINE_MS,+ spawnWaitExtensionFactor = SPAWN_WAIT_EXTENSION_FACTOR,+) {+ return (+ packagedStartupBoundMs(spawnStartupDeadlineMs, spawnWaitExtensionFactor) ++ PACKAGED_BOOT_ENVELOPE_MS+ );+}++export const PACKAGED_DRIVER_TIMEOUT_MS =+ packagedDiscoveryDeadlineMs() ++ PACKAGED_PTY_ECHO_BUDGET_MS ++ PACKAGED_DISCOVERY_OVERRUN_MS ++ PACKAGED_DRIVER_MARGIN_MS;++export function describeDriverTimeoutBudget() {+ return (+ `the packaged PTY CDP driver outlived its ${PACKAGED_DRIVER_TIMEOUT_MS / 1000}s budget ` ++ `(${packagedDiscoveryDeadlineMs() / 1000}s discovery + ${PACKAGED_PTY_ECHO_BUDGET_MS / 1000}s echo + ` ++ `${PACKAGED_DISCOVERY_OVERRUN_MS / 1000}s overrun + ${PACKAGED_DRIVER_MARGIN_MS / 1000}s margin), ` ++ 'so one phase overran and was killed before it could report itself'+ );+}++export function packagedDriverSpawnOptions(packageDir, env = process.env) {+ return {+ cwd: packageDir,+ encoding: 'utf8',+ env: windowsPtyDriverEnv(env),+ timeout: PACKAGED_DRIVER_TIMEOUT_MS,+ };+}const scriptDir = dirname(fileURLToPath(import.meta.url));const defaultPackageDir = resolve(scriptDir, '../dist-desktop/win-unpacked');@@ -32,7 +82,12 @@ export function windowsPackageLaunchArgs(projectDir, userDataDir) {}export function windowsPtyDriverEnv(env = process.env) {- return { ...env, OK_SMOKE_EXPECT_PLATFORM: 'win32' };+ return {+ ...env,+ OK_SMOKE_EXPECT_PLATFORM: 'win32',+ OK_SMOKE_DISCOVERY_DEADLINE_MS: String(packagedDiscoveryDeadlineMs()),+ OK_SMOKE_ECHO_DEADLINE_MS: String(PACKAGED_PTY_ECHO_BUDGET_MS),+ };}export function seedWindowsPtySmokeProject(rootDir, shellPath) {@@ -90,16 +145,20 @@ export function runWindowsPackageTerminalSmoke({windowsHide: true,});- const driver = spawnSync(python, [cdpDriver], {- cwd: resolvedPackageDir,- encoding: 'utf8',- env: windowsPtyDriverEnv(env),- timeout: 60_000,- });+ const driver = spawnSync(+ python,+ [cdpDriver],+ packagedDriverSpawnOptions(resolvedPackageDir, env),+ );if (driver.stdout) process.stdout.write(driver.stdout);if (driver.stderr) process.stderr.write(driver.stderr);+ if (driver.error?.code === 'ETIMEDOUT') {+ fail(describeDriverTimeoutBudget());+ }if (driver.error) fail(`could not run packaged PTY CDP driver: ${driver.error.message}`);- if (driver.status !== 0) fail(`packaged PTY CDP driver exited ${driver.status}`);+ if (driver.status !== 0) {+ fail(`packaged PTY CDP driver exited ${driver.status} (signal ${driver.signal})`);+ }} catch (error) {closeSync(logFd);printAppLog(logPath);diff --git a/packages/desktop/scripts/smoke-windows-terminal-package.test.mjs b/packages/desktop/scripts/smoke-windows-terminal-package.test.mjsindex f68ab7caf..2da4b531e 100644--- a/packages/desktop/scripts/smoke-windows-terminal-package.test.mjs+++ b/packages/desktop/scripts/smoke-windows-terminal-package.test.mjs@@ -1,20 +1,202 @@-import { mkdtempSync, readFileSync, rmSync } from 'node:fs';+import { execFileSync } from 'node:child_process';+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';import { tmpdir } from 'node:os';-import { join } from 'node:path';+import { dirname, join } from 'node:path';+import { fileURLToPath, pathToFileURL } from 'node:url';import { afterEach, describe, expect, test } from 'vitest';import {+ SPAWN_STARTUP_DEADLINE_MS,+ SPAWN_WAIT_EXTENSION_FACTOR,+} from '../src/shared/boot-narration.ts';+import {+ describeDriverTimeoutBudget,+ PACKAGED_BOOT_ENVELOPE_MS,+ PACKAGED_DISCOVERY_OVERRUN_MS,+ PACKAGED_DRIVER_MARGIN_MS,+ PACKAGED_DRIVER_TIMEOUT_MS,+ PACKAGED_PTY_ECHO_BUDGET_MS,+ packagedDiscoveryDeadlineMs,+ packagedDriverSpawnOptions,+ packagedStartupBoundMs,runWindowsPackageTerminalSmoke,seedWindowsPtySmokeProject,windowsPackageLaunchArgs,windowsPtyDriverEnv,} from './smoke-windows-terminal-package.mjs';+const scriptDir = dirname(fileURLToPath(import.meta.url));+const driverPath = join(scriptDir, 'smoke-terminal-package-cdp.py');+const harnessPath = join(scriptDir, 'smoke-windows-terminal-package.mjs');+const pythonBin = process.env.OK_PYTHON ?? (process.platform === 'win32' ? 'python' : 'python3');+const DEAD_CDP_LIST_URL = 'http://127.0.0.1:9/json/list';++function runPython(args, env) {+ try {+ return {+ code: 0,+ output: execFileSync(pythonBin, args, {+ encoding: 'utf8',+ env: { ...process.env, ...env },+ stdio: ['ignore', 'pipe', 'pipe'],+ timeout: 25_000,+ }),+ };+ } catch (error) {+ if (error.code === 'ENOENT') {+ throw new Error(+ `no python interpreter at ${pythonBin}; the harness resolves env.OK_PYTHON ?? 'python', set OK_PYTHON to match`,+ );+ }+ return { code: error.status ?? 1, output: `${error.stdout ?? ''}${error.stderr ?? ''}` };+ }+}++function websocketStub(body) {+ return `def create_connection(url, timeout=None, origin=None):\n ${body}\n`;+}++const CHATTY_PEER_STUB = [+ 'import json',+ '',+ 'class _Chatty:',+ ' def __init__(self, timeout):',+ ' self.timeout = timeout',+ ' def settimeout(self, value):',+ ' self.timeout = value',+ ' def send(self, payload):',+ ' pass',+ ' def recv(self):',+ ' return json.dumps({"id": 99, "method": "Runtime.consoleAPICalled"})',+ ' def close(self):',+ ' pass',+ '',+ 'def create_connection(url, timeout=None, origin=None):',+ ' return _Chatty(timeout)',+ '',+].join('\n');++const WINDOW_RECORDING_STUB = [+ 'import json, time',+ '',+ 'class _Recorder:',+ ' def __init__(self):',+ ' self.seen = []',+ ' self.reads = 0',+ ' def settimeout(self, value):',+ ' self.seen.append(value)',+ ' def send(self, payload):',+ ' pass',+ ' def recv(self):',+ ' self.reads += 1',+ ' if self.reads >= 4:',+ ' raise RuntimeError("WINDOWS=" + json.dumps(self.seen))',+ ' time.sleep(0.05)',+ ' return json.dumps({"id": 99, "method": "Runtime.consoleAPICalled"})',+ ' def close(self):',+ ' pass',+ '',+ 'def create_connection(url, timeout=None, origin=None):',+ ' return _Recorder()',+ '',+].join('\n');++const SPENT_ON_CONNECT_STUB = [+ 'import json, time',+ '',+ 'class _Idle:',+ ' def settimeout(self, value):',+ ' pass',+ ' def send(self, payload):',+ ' raise AssertionError("send ran after the deadline was already spent")',+ ' def recv(self):',+ ' return json.dumps({"id": 99})',+ ' def close(self):',+ ' print("CLOSED")',+ '',+ 'def create_connection(url, timeout=None, origin=None):',+ ' time.sleep(0.4)',+ ' return _Idle()',+ '',+].join('\n');++function stubRootWithModule(source) {+ const stubRoot = mkdtempSync(join(tmpdir(), 'ok-cdp-driver-stub-'));+ fixtures.push(stubRoot);+ writeFileSync(join(stubRoot, 'websocket.py'), source);+ return stubRoot;+}++function stubRootWith(body) {+ return stubRootWithModule(websocketStub(body));+}+const MEASURED_SPAWN_TO_CDP_MS = 3_920;+const MEASURED_CDP_TO_EDITOR_MS = 5_940;+const fixtures = [];afterEach(() => {for (const fixture of fixtures.splice(0)) rmSync(fixture, { recursive: true, force: true });});+function ptyEchoSnippet() {+ const extractor = [+ 'import importlib.util, sys, types',+ "sys.modules['websocket'] = types.ModuleType('websocket')",+ `spec = importlib.util.spec_from_file_location('d', ${JSON.stringify(driverPath)})`,+ 'm = importlib.util.module_from_spec(spec); spec.loader.exec_module(m)',+ 'seen = {}',+ 'def fake(socket_url, expression, timeout=5):',+ " seen['e'] = expression",+ " return {'platform': 'win32'}",+ 'm.evaluate_value = fake',+ "m.evaluate_pty_echo('ws://stub')",+ "sys.stdout.write(seen['e'])",+ ].join('\n');+ const run = runPython(['-c', extractor], { OK_SMOKE_ECHO_DEADLINE_MS: '10500' });+ if (run.code !== 0) throw new Error(`snippet extraction failed: ${run.output}`);+ return run.output;+}++function stubRenderer({ duringStart = [] } = {}) {+ const listeners = [];+ const emit = (data) => {+ for (const listener of listeners) listener({ ptyId: 7, data });+ };+ return {+ okDesktop: {+ config: { ptyAvailable: true },+ platform: 'win32',+ terminal: {+ onData(listener) {+ listeners.push(listener);+ return () => {+ listeners.length = 0;+ };+ },+ async create() {+ return { ok: true, ptyId: 7 };+ },+ async start() {+ for (const chunk of duringStart) {+ await new Promise((resolve) => setTimeout(resolve, 5));+ emit(chunk);+ }+ return { ok: true };+ },+ async kill() {},+ },+ },+ };+}++function runPtyEchoSnippet(window) {+ return new Function('window', `return (\n${ptyEchoSnippet()}\n);`)(window);+}++const CONPTY_INIT = '\u001b[1t\u001b[c';+const MARKER_LINE = 'OK_PACKAGED_PTY_ECHO\r\n';+const RENDERER_TIMER_MS = 500;+describe('packaged Windows terminal smoke driver', () => {test('seeds deterministic project-local terminal config for the packaged app', () => {const root = mkdtempSync(join(tmpdir(), 'ok-win-pty-smoke-test-'));@@ -61,6 +243,252 @@ describe('packaged Windows terminal smoke driver', () => {expect(windowsPtyDriverEnv({ SENTINEL: 'preserved' })).toEqual({SENTINEL: 'preserved',OK_SMOKE_EXPECT_PLATFORM: 'win32',+ OK_SMOKE_DISCOVERY_DEADLINE_MS: String(packagedDiscoveryDeadlineMs()),+ OK_SMOKE_ECHO_DEADLINE_MS: String(PACKAGED_PTY_ECHO_BUDGET_MS),+ });+ });++ test('hands the CDP driver only env keys that driver reads', () => {+ const driver = readFileSync(driverPath, 'utf8');+ for (const key of Object.keys(windowsPtyDriverEnv({}))) {+ expect(driver).toMatch(new RegExp(`os\\.environ(?:\\.get\\(|\\[)"${key}"`));+ }+ });++ test('forwards that env and the driver budget to the spawn it actually makes', () => {+ const options = packagedDriverSpawnOptions('C:\\pkg', { SENTINEL: 'preserved' });+ expect(options.cwd).toBe('C:\\pkg');+ expect(options.timeout).toBe(PACKAGED_DRIVER_TIMEOUT_MS);+ expect(options.env).toEqual(windowsPtyDriverEnv({ SENTINEL: 'preserved' }));+ });++ test('stops re-arming the read window when its own deadline is spent', () => {+ const stubRoot = stubRootWithModule(CHATTY_PEER_STUB);+ const startedAt = Date.now();+ const { code, output } = runPython(+ [+ '-c',+ [+ 'import importlib.util,sys',+ `spec=importlib.util.spec_from_file_location("cdp",${JSON.stringify(driverPath)})`,+ 'm=importlib.util.module_from_spec(spec); spec.loader.exec_module(m)',+ 'try:',+ ' m.evaluate_value("ws://127.0.0.1:9/devtools/page/x", "1", timeout=1.5)',+ 'except Exception as error:',+ ' print(f"{type(error).__name__}: {error}")',+ ].join('\n'),+ ],+ { PYTHONPATH: stubRoot },+ );+ const elapsed = Date.now() - startedAt;+ expect(code).toBe(0);+ expect(output).toContain('ReplyPhaseTimeoutError: no CDP reply for id=1 within 1.5s');+ expect(elapsed).toBeGreaterThanOrEqual(1_400);+ expect(elapsed).toBeLessThan(20_000);+ });++ test('closes the socket instead of sending when the connect spent the deadline', () => {+ const stubRoot = stubRootWithModule(SPENT_ON_CONNECT_STUB);+ const { code, output } = runPython(+ [+ '-c',+ [+ 'import importlib.util,sys',+ `spec=importlib.util.spec_from_file_location("cdp",${JSON.stringify(driverPath)})`,+ 'm=importlib.util.module_from_spec(spec); spec.loader.exec_module(m)',+ 'try:',+ ' m.evaluate_value("ws://127.0.0.1:9/devtools/page/x", "1", timeout=0.3)',+ 'except Exception as error:',+ ' print(f"{type(error).__name__}: {error}")',+ ].join('\n'),+ ],+ { PYTHONPATH: stubRoot },+ );+ expect(code).toBe(0);+ expect(output).toContain(+ 'ConnectPhaseTimeoutError: connect and handshake spent the whole 0.3s budget',+ );+ expect(output).toContain('CLOSED');+ expect(output).not.toContain('send ran after the deadline');+ });++ test('arms a shrinking socket window before every read', () => {+ const stubRoot = stubRootWithModule(WINDOW_RECORDING_STUB);+ const { code, output } = runPython(+ [+ '-c',+ [+ 'import importlib.util,sys',+ `spec=importlib.util.spec_from_file_location("cdp",${JSON.stringify(driverPath)})`,+ 'm=importlib.util.module_from_spec(spec); spec.loader.exec_module(m)',+ 'try:',+ ' m.evaluate_value("ws://127.0.0.1:9/devtools/page/x", "1", timeout=5)',+ 'except Exception as error:',+ ' print(f"{error}")',+ ].join('\n'),+ ],+ { PYTHONPATH: stubRoot },+ );+ expect(code).toBe(0);+ const windows = JSON.parse(/WINDOWS=(\[[^\]]*\])/.exec(output)?.[1] ?? '[]');+ expect(windows.length).toBe(5);+ expect(windows[0] - windows[windows.length - 1]).toBeGreaterThanOrEqual(0.1);+ });++ test.each([['9000'], ['10000']])(+ 'refuses echo budget %s before it spends the discovery budget',+ (budget) => {+ const stubRoot = stubRootWith('raise OSError');+ const { code, output } = runPython([driverPath], {+ PYTHONPATH: stubRoot,+ OK_SMOKE_ECHO_DEADLINE_MS: budget,+ OK_SMOKE_CDP_LIST_URL: DEAD_CDP_LIST_URL,+ });+ expect(code).toBe(1);+ expect(output).toContain('ERROR: packaged PTY smoke misconfigured:');+ expect(output).toContain('leaves no room for the renderer');+ expect(output).not.toContain('Traceback');+ expect(output).not.toContain('no project editor debug target appeared');+ },+ );++ test('accepts the smallest echo budget that leaves the renderer its margin', () => {+ const stubRoot = stubRootWith('raise OSError');+ const { code, output } = runPython(+ [+ '-c',+ [+ 'import importlib.util,sys',+ `spec=importlib.util.spec_from_file_location("cdp",${JSON.stringify(driverPath)})`,+ 'm=importlib.util.module_from_spec(spec); spec.loader.exec_module(m)',+ 'print(f"RENDERER_TIMER={m.renderer_echo_timeout_ms()}")',+ ].join('\n'),+ ],+ { PYTHONPATH: stubRoot, OK_SMOKE_ECHO_DEADLINE_MS: '10001' },+ );+ expect(code).toBe(0);+ expect(output).toMatch(/RENDERER_TIMER=1(?![0-9])/);+ });++ test('hands the renderer echo timer the socket budget it must beat', () => {+ const stubRoot = stubRootWith('raise RuntimeError(f"SOCKET_TIMEOUT={timeout}")');+ const { code, output } = runPython(+ [+ '-c',+ [+ 'import importlib.util,sys',+ `spec=importlib.util.spec_from_file_location("cdp",${JSON.stringify(driverPath)})`,+ 'm=importlib.util.module_from_spec(spec); spec.loader.exec_module(m)',+ 'try:',+ ' m.evaluate_pty_echo("ws://127.0.0.1:9/devtools/page/x")',+ 'except Exception as error:',+ ' print(error)',+ ].join('\n'),+ ],+ { PYTHONPATH: stubRoot, OK_SMOKE_ECHO_DEADLINE_MS: String(PACKAGED_PTY_ECHO_BUDGET_MS) },+ );+ expect(code).toBe(0);+ expect(output).toMatch(+ new RegExp(`SOCKET_TIMEOUT=${(PACKAGED_PTY_ECHO_BUDGET_MS / 1000).toFixed(1)}(?![0-9])`),+ );+ expect(output).toContain(`of a ${PACKAGED_PTY_ECHO_BUDGET_MS / 1000}s budget`);+ expect(output).toContain('renderer_timer=20s');+ });++ test('reaches the app budget through a plain node import of the app module', () => {+ const output = execFileSync(+ process.execPath,+ [+ '-e',+ `import(${JSON.stringify(pathToFileURL(harnessPath).href)}).then((m) => console.log(m.packagedDiscoveryDeadlineMs()))`,+ ],+ { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] },+ );+ expect(output.trim()).toBe(String(packagedDiscoveryDeadlineMs()));+ });++ test('spends the deadline it was handed before reporting the phase it reached', () => {+ const stubRoot = stubRootWith('raise OSError');+ const { code, output } = runPython([driverPath], {+ PYTHONPATH: stubRoot,+ OK_SMOKE_DISCOVERY_DEADLINE_MS: '1200',+ OK_SMOKE_CDP_LIST_URL: DEAD_CDP_LIST_URL,});+ expect(code).not.toBe(0);+ expect(output).toContain('the debug endpoint never answered');+ const elapsed = output.match(/gave up after (\d+\.\d)s of a 1s budget/);+ expect(elapsed).not.toBeNull();+ expect(Number(elapsed[1])).toBeGreaterThanOrEqual(1.2);+ });++ test('waits as long as the packaged app is allowed to keep starting its server', () => {+ expect(packagedStartupBoundMs()).toBe(SPAWN_STARTUP_DEADLINE_MS * SPAWN_WAIT_EXTENSION_FACTOR);+ expect(packagedDiscoveryDeadlineMs()).toBe(+ packagedStartupBoundMs() + PACKAGED_BOOT_ENVELOPE_MS,+ );+ expect(PACKAGED_BOOT_ENVELOPE_MS).toBeGreaterThanOrEqual(+ MEASURED_SPAWN_TO_CDP_MS + MEASURED_CDP_TO_EDITOR_MS,+ );+ });++ test('moves with the spawn deadline the packaged path enforces', () => {+ expect(packagedDiscoveryDeadlineMs(20_000) - packagedDiscoveryDeadlineMs(15_000)).toBe(+ 5_000 * SPAWN_WAIT_EXTENSION_FACTOR,+ );+ expect(packagedDiscoveryDeadlineMs(15_000)).toBe(+ 15_000 * SPAWN_WAIT_EXTENSION_FACTOR + PACKAGED_BOOT_ENVELOPE_MS,+ );+ });++ test('moves with the extension factor the packaged path enforces', () => {+ expect(packagedDiscoveryDeadlineMs(SPAWN_STARTUP_DEADLINE_MS, 4)).toBe(+ SPAWN_STARTUP_DEADLINE_MS * 4 + PACKAGED_BOOT_ENVELOPE_MS,+ );+ expect(+ packagedDiscoveryDeadlineMs(SPAWN_STARTUP_DEADLINE_MS, SPAWN_WAIT_EXTENSION_FACTOR + 1) -+ packagedDiscoveryDeadlineMs(),+ ).toBe(SPAWN_STARTUP_DEADLINE_MS);+ });++ test('derives from no constant the packaged boot leaves unexecuted', () => {+ const harness = readFileSync(harnessPath, 'utf8');+ expect(harness).toContain('SPAWN_STARTUP_DEADLINE_MS');+ expect(harness).not.toContain('UTILITY_INIT_TIMEOUT_MS');+ });++ test('kills the driver only after every phase budget plus a margin has run out', () => {+ expect(PACKAGED_DRIVER_TIMEOUT_MS).toBe(+ packagedDiscoveryDeadlineMs() ++ PACKAGED_PTY_ECHO_BUDGET_MS ++ PACKAGED_DISCOVERY_OVERRUN_MS ++ PACKAGED_DRIVER_MARGIN_MS,+ );+ });++ test('measures each phase from when the snippet started, not from an absolute clock', async () => {+ const result = await runPtyEchoSnippet(+ stubRenderer({ duringStart: [CONPTY_INIT, MARKER_LINE] }),+ );+ const { createdMs, firstByteMs, markerMs } = result.timings;+ for (const value of [createdMs, firstByteMs, markerMs]) {+ expect(typeof value).toBe('number');+ expect(value).toBeLessThan(RENDERER_TIMER_MS);+ }+ expect(createdMs).toBeLessThanOrEqual(firstByteMs);+ expect(firstByteMs).toBeLessThan(markerMs);+ });++ test('reports the same measurements on the line it prints when nothing echoes', async () => {+ await expect(runPtyEchoSnippet(stubRenderer())).rejects.toThrow(+ /PTY echo timed out; output=""; timings=\{"createdMs":\d+,"firstByteMs":null,"markerMs":null\}/,+ );+ });++ test('names every phase budget in seconds when the kill fires', () => {+ expect(describeDriverTimeoutBudget()).toBe(+ 'the packaged PTY CDP driver outlived its 168s budget ' ++ '(130s discovery + 30s echo + 3s overrun + 5s margin), ' ++ 'so one phase overran and was killed before it could report itself',+ );});});