Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
Keep PTY readiness tied to observed shell progress (#5387)
Nick Gomezf2290a873ab1
diff --git a/knip.config.ts b/knip.config.tsindex be41ab16c..37d9ce912 100644--- a/knip.config.ts+++ b/knip.config.ts@@ -145,11 +145,15 @@ export default {'packages/server': {entry: ['src/**/*.test.ts', 'src/parse-worker.ts'],project: 'src/**',- ignoreDependencies: ['@types/shell-quote'],+ ignoreDependencies: [+ '@types/shell-quote',+ ],},'packages/cli': {entry: ['src/**/*.test.ts', 'scripts/*.ts', 'tests/**/*.ts', 'src/parse-worker.ts'],- ignoreDependencies: ['yjs'],+ ignoreDependencies: [+ 'yjs',+ ],},'packages/desktop': {entry: [diff --git a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts b/packages/app/tests/integration/bug3-source-mode-writeback.test.tsindex 8a60e5826..a28cc2b18 100644--- a/packages/app/tests/integration/bug3-source-mode-writeback.test.ts+++ b/packages/app/tests/integration/bug3-source-mode-writeback.test.ts@@ -1,3 +1,4 @@+import { readFileSync } from 'node:fs';import { join } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';diff --git a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts b/packages/app/tests/integration/indented-jsx-byte-sacred.test.tsindex 6fcdc23f2..4fc2c11f9 100644--- a/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts+++ b/packages/app/tests/integration/indented-jsx-byte-sacred.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { normalizeBridge } from '@inkeep/open-knowledge-core';import { updateYFragment } from '@tiptap/y-tiptap';diff --git a/packages/app/tests/integration/interior-edit-freshness.test.ts b/packages/app/tests/integration/interior-edit-freshness.test.tsindex 40b5cdfd6..9f2f6feb6 100644--- a/packages/app/tests/integration/interior-edit-freshness.test.ts+++ b/packages/app/tests/integration/interior-edit-freshness.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts b/packages/app/tests/integration/keystroke-cadence-danger-space.test.tsindex 93666af70..2a085742b 100644--- a/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts+++ b/packages/app/tests/integration/keystroke-cadence-danger-space.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/producer-guard-wiring.test.ts b/packages/app/tests/integration/producer-guard-wiring.test.tsindex 867598d34..878d5196b 100644--- a/packages/app/tests/integration/producer-guard-wiring.test.ts+++ b/packages/app/tests/integration/producer-guard-wiring.test.ts@@ -1,3 +1,4 @@+import { setTimeout as wait } from 'node:timers/promises';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';import { afterAll, beforeAll, describe, expect, test } from 'vitest';diff --git a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.tsindex 0970ac12f..4ada3cf26 100644--- a/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts+++ b/packages/app/tests/integration/qa-039-reconciliation-counters-http.test.ts@@ -1,3 +1,4 @@+import { randomUUID } from 'node:crypto';import { afterEach, describe, expect, test } from 'vitest';import { agentWriteMd, createTestServer, type TestServer } from './test-harness';diff --git a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts b/packages/app/tests/integration/qa-045-restore-after-gc.test.tsindex b632185e1..cbe2b705f 100644--- a/packages/app/tests/integration/qa-045-restore-after-gc.test.ts+++ b/packages/app/tests/integration/qa-045-restore-after-gc.test.ts@@ -1,3 +1,4 @@+import { execFileSync } from 'node:child_process';import { randomUUID } from 'node:crypto';import { join } from 'node:path';diff --git a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.tsindex c530ce4ba..89bc9b4d9 100644--- a/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts+++ b/packages/app/tests/integration/qa-050-rollback-undo-shipped-path.test.ts@@ -1,3 +1,4 @@+import { randomUUID } from 'node:crypto';import { afterAll, afterEach, beforeAll, describe, expect, test } from 'vitest';import type * as Y from 'yjs';diff --git a/packages/app/tests/integration/undo-after-rollback.test.ts b/packages/app/tests/integration/undo-after-rollback.test.tsindex d3767adc9..a78aeb0e4 100644--- a/packages/app/tests/integration/undo-after-rollback.test.ts+++ b/packages/app/tests/integration/undo-after-rollback.test.ts@@ -1,3 +1,4 @@+import { ROLLBACK_ORIGIN } from '@inkeep/open-knowledge-server';import { afterAll, beforeAll, describe, expect, test } from 'vitest';import * as Y from 'yjs';diff --git a/packages/app/tests/integration/undo-recycle-reset.test.ts b/packages/app/tests/integration/undo-recycle-reset.test.tsindex b076f955c..e7cc69c3f 100644--- a/packages/app/tests/integration/undo-recycle-reset.test.ts+++ b/packages/app/tests/integration/undo-recycle-reset.test.ts@@ -1,3 +1,4 @@+import { writeFileSync } from 'node:fs';import { join } from 'node:path';import { setTimeout as wait } from 'node:timers/promises';diff --git a/packages/app/tests/integration/undo-rederive-survival.test.ts b/packages/app/tests/integration/undo-rederive-survival.test.tsindex 7935e35f8..9ddcd0730 100644--- a/packages/app/tests/integration/undo-rederive-survival.test.ts+++ b/packages/app/tests/integration/undo-rederive-survival.test.ts@@ -1,3 +1,4 @@+import { prependFrontmatter } from '@inkeep/open-knowledge-core';import { Plugin, PluginKey } from '@tiptap/pm/state';import { updateYFragment, yXmlFragmentToProseMirrorRootNode } from '@tiptap/y-tiptap';diff --git a/packages/core/src/bridge/compose.ts b/packages/core/src/bridge/compose.tsindex f4cc8a286..2a597ec3d 100644--- a/packages/core/src/bridge/compose.ts+++ b/packages/core/src/bridge/compose.ts@@ -1,3 +1,4 @@+import { prependFrontmatter, stripFrontmatter } from '../extensions/frontmatter.ts';export type ComposeAdjustment = 'none' | 'body' | 'frontmatter' | 'unresolved';diff --git a/packages/core/src/bridge/doc-boundary-space.ts b/packages/core/src/bridge/doc-boundary-space.tsindex 8fb428b90..01ef76a9f 100644--- a/packages/core/src/bridge/doc-boundary-space.ts+++ b/packages/core/src/bridge/doc-boundary-space.ts@@ -1,3 +1,4 @@+import { FRONTMATTER_RE, stripFrontmatter } from '../extensions/frontmatter.ts';import { carriedEdgeEmpties, type DocEdgeEmpties } from '../markdown/doc-edge-blank-runs.ts';diff --git a/packages/core/src/markdown/comment-promoter.ts b/packages/core/src/markdown/comment-promoter.tsindex fc267cbea..fb0ba5a7c 100644--- a/packages/core/src/markdown/comment-promoter.ts+++ b/packages/core/src/markdown/comment-promoter.ts@@ -1,3 +1,4 @@+import type { Nodes, Paragraph, PhrasingContent, Root, RootContent, Text } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/merged-walker.ts b/packages/core/src/markdown/merged-walker.tsindex bfd7e3109..fa3cfcd86 100644--- a/packages/core/src/markdown/merged-walker.ts+++ b/packages/core/src/markdown/merged-walker.ts@@ -1,3 +1,4 @@+import type { Nodes, Parent, Root } from 'mdast';import { SKIP, visit } from 'unist-util-visit';import type { VFile } from 'vfile';diff --git a/packages/core/src/markdown/safe-url.ts b/packages/core/src/markdown/safe-url.tsindex 4d3250d3a..8be632ba4 100644--- a/packages/core/src/markdown/safe-url.ts+++ b/packages/core/src/markdown/safe-url.ts@@ -1,3 +1,4 @@+export const SAFE_URL_SCHEMES = ['https', 'http', 'mailto', 'tel', 'ftp', 'sms'] as const;const SCHEME_ALT = SAFE_URL_SCHEMES.map((s) => `${s}:`).join('|');diff --git a/packages/desktop/tests/support/pty-readiness.test-helper.ts b/packages/desktop/tests/support/pty-readiness.test-helper.tsindex 9adb5aa9d..c76e53a40 100644--- a/packages/desktop/tests/support/pty-readiness.test-helper.ts+++ b/packages/desktop/tests/support/pty-readiness.test-helper.ts@@ -86,7 +86,7 @@ export function createPtyHostProbe(options: PtyHostProbeOptions): PtyHostProbe {if (error !== null) return `spawn-error: ${error}`;const exit = exitOf(ptyId);if (exit === null) return null;- return `exited (code ${exit.exitCode ?? 'none'}, signal ${exit.signal ?? 'none'})`;+ return describeExit(exit);},}),};@@ -102,6 +102,36 @@ export interface ShellReadyOptions extends WaitOptions {quietSamples?: number;}+export interface HarnessReadinessObserver {+ waitForCondition(+ predicate: () => boolean,+ label: string,+ options?: Omit<WaitOptions, 'backstopAt'>,+ ): Promise<void>;+ waitForCompletion(+ predicate: () => boolean,+ label: string,+ options?: Omit<WaitOptions, 'backstopAt'>,+ ): Promise<HarnessCompletion>;+ waitForShellReady(label: string, options?: Omit<ShellReadyOptions, 'backstopAt'>): Promise<void>;+ waitForEvaluatedInput(+ send: (data: string) => void,+ probe: EvaluatedInputProbe,+ label: string,+ options?: Omit<EvaluatedInputOptions, 'budgetMs'>,+ ): Promise<EvaluatedInputTiming>;+}++export interface HarnessCompletion {+ readonly observedAt: number;+}++export interface HarnessCompletionBounds {+ readonly after: HarnessCompletion;+ readonly initialDeadlineAt: number;+ readonly reportDeadlineAt: number;+}+const DEFAULT_INTERVAL_MS = 15;const DEFAULT_STALL_MS = 8_000;const DEFAULT_READY_INTERVAL_MS = 50;@@ -145,8 +175,8 @@ const HARNESS_TEARDOWN_GRACE_MS = 15_000;export const HARNESS_VERDICT_POLL_INTERVAL_MS = 25;export const HARNESS_CHILD_KILL_WAIT_MS = 2_000;export const HARNESS_REPORT_RESERVE_MS = 1_000;-export const harnessWindowsLaunchWait = (backstopAt: number) => ({ stallMs: 20_000, backstopAt });-export const harnessExitAfterKillWait = (backstopAt: number) => ({ stallMs: 12_000, backstopAt });+export const HARNESS_WINDOWS_LAUNCH_STALL_MS = 20_000;+export const HARNESS_EXIT_AFTER_KILL_STALL_MS = 12_000;const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms));@@ -171,6 +201,84 @@ function describeLeading(text: string): string {return `${JSON.stringify(text.slice(0, RECEIVED_EXCERPT_CHARS))}...`;}+function describeExit(exit: { exitCode: number | undefined; signal: number | null }): string {+ return `exited (code ${exit.exitCode ?? 'none'}, signal ${exit.signal ?? 'none'})`;+}++interface ShellOutputObservation {+ advanced: number;+ lastAdvanceAt: number | null;+ observe(stream: PtyStream, now: number): void;+}++function createShellOutputObservation(): ShellOutputObservation {+ let leadingAttachChars: number | null = null;+ return {+ advanced: 0,+ lastAdvanceAt: null,+ observe(stream, now) {+ const text = stream.read();+ if (leadingAttachChars === null) {+ const beyondAttach = shellOutputBeyondAttach(text);+ if (beyondAttach.length === 0) return;+ leadingAttachChars = text.length - beyondAttach.length;+ }+ const seen = text.length - leadingAttachChars;+ if (seen > this.advanced) {+ this.advanced = seen;+ this.lastAdvanceAt = now;+ }+ },+ };+}++interface ReadinessState {+ initialDeadline: WaitDeadline;+ reportDeadlineAt: number;+ output: ShellOutputObservation;+}++interface WaitDeadline {+ at: number;+ bound: string;+}++function cappedByReportDeadline(phase: WaitDeadline, reportDeadlineAt: number): WaitDeadline {+ return reportDeadlineAt <= phase.at+ ? { at: reportDeadlineAt, bound: 'the report deadline' }+ : phase;+}++function readinessDeadline(readiness: ReadinessState, stallMs: number): WaitDeadline {+ const { lastAdvanceAt } = readiness.output;+ const stallEndsAt = lastAdvanceAt === null ? null : lastAdvanceAt + stallMs;+ return cappedByReportDeadline(+ stallEndsAt !== null && stallEndsAt > readiness.initialDeadline.at+ ? { at: stallEndsAt, bound: `its ${Math.round(stallMs)}ms stall window ran out` }+ : readiness.initialDeadline,+ readiness.reportDeadlineAt,+ );+}++function harnessExitDeadline(bounds: HarnessCompletionBounds): WaitDeadline {+ const windowEndsAt = bounds.after.observedAt + HARNESS_EXIT_AFTER_KILL_STALL_MS;+ return cappedByReportDeadline(+ bounds.initialDeadlineAt > windowEndsAt+ ? { at: bounds.initialDeadlineAt, bound: 'its initial allowance ended' }+ : { at: windowEndsAt, bound: `its ${HARNESS_EXIT_AFTER_KILL_STALL_MS}ms exit window ended` },+ bounds.reportDeadlineAt,+ );+}++function throwIfShellFailed(stream: PtyStream, label: string, startedAt: number): void {+ const failure = stream.failure();+ if (failure !== null) {+ throw new Error(+ `shell failed before ${label}: ${failure} (after ${Math.round(performance.now() - startedAt)}ms, received ${describeReceived(stream.read())})`,+ );+ }+}+export async function waitForCondition(stream: PtyStream,predicate: () => boolean,@@ -201,45 +309,28 @@ export async function waitForCondition(Math.round(stallWindowMs) < Math.round(stallMs)? ` (the containment it runs inside cut the ${Math.round(stallMs)}ms it declared)`: '';- let leadingAttachChars: number | null = null;- const shellChars = (): number => {- const text = stream.read();- if (leadingAttachChars === null) {- const beyondAttach = shellOutputBeyondAttach(text);- if (beyondAttach.length === 0) return 0;- leadingAttachChars = text.length - beyondAttach.length;- }- return text.length - leadingAttachChars;- };- let advanced = shellChars();- let lastAdvanceAt = startedAt;+ const output = createShellOutputObservation();+ output.observe(stream, startedAt);+ let lastAdvanceAt = output.lastAdvanceAt ?? startedAt;for (;;) {- const failure = stream.failure();- if (failure !== null) {- throw new Error(- `shell failed before ${label}: ${failure} (after ${Math.round(performance.now() - startedAt)}ms, received ${describeReceived(stream.read())})`,- );- }+ throwIfShellFailed(stream, label, startedAt);const now = performance.now();- const seen = shellChars();- if (seen !== advanced) {- advanced = seen;- lastAdvanceAt = now;- }+ output.observe(stream, now);+ lastAdvanceAt = output.lastAdvanceAt ?? lastAdvanceAt;if (now >= backstopAt) {const silentPastWindow = now - lastAdvanceAt >= stallWindowMs;if (declaredWindowSpansContainment && silentPastWindow) {throw new Error(- `timeout waiting for: ${label} after ${Math.round(stallWindowMs)}ms${cutNote} without ${advanced === 0 ? 'any' : 'new'} shell output, the only progress signal this wait watches (received ${describeReceived(stream.read())})`,+ `timeout waiting for: ${label} after ${Math.round(stallWindowMs)}ms${cutNote} without ${output.advanced === 0 ? 'any' : 'new'} shell output, the only progress signal this wait watches (received ${describeReceived(stream.read())})`,);}const stallNote = silentPastWindow? `, longer than its ${Math.round(stallWindowMs)}ms stall window`: '';throw new Error(- advanced === 0+ output.advanced === 0? `${label} was not reached inside its ${Math.round(containmentMs)}ms containment without any shell output (received ${describeReceived(stream.read())})`- : `${label} was not reached inside its ${Math.round(containmentMs)}ms containment; the stream last advanced ${Math.round(now - lastAdvanceAt)}ms ago${stallNote}, ${advanced} characters past the shell's first output (received ${describeReceived(stream.read())})`,+ : `${label} was not reached inside its ${Math.round(containmentMs)}ms containment; the stream last advanced ${Math.round(now - lastAdvanceAt)}ms ago${stallNote}, ${output.advanced} characters counted from the shell's first output on (received ${describeReceived(stream.read())})`,);}await sleep(intervalMs);@@ -247,11 +338,89 @@ export async function waitForCondition(}}-export async function waitForShellReady(+async function waitForReadinessCondition(stream: PtyStream,+ predicate: () => boolean,label: string,- options: ShellReadyOptions = {},-): Promise<void> {+ readiness: ReadinessState,+ options: Omit<WaitOptions, 'backstopAt'>,+): Promise<HarnessCompletion> {+ const intervalMs = requireDuration(+ options.intervalMs ?? DEFAULT_INTERVAL_MS,+ 'the poll interval',+ label,+ );+ const stallMs = requireDuration(options.stallMs ?? DEFAULT_STALL_MS, 'the stall window', label);+ const startedAt = performance.now();+ const { output } = readiness;+ for (;;) {+ throwIfShellFailed(stream, label, startedAt);+ const now = performance.now();+ output.observe(stream, now);+ const deadline = readinessDeadline(readiness, stallMs);+ if (now >= deadline.at) {+ throw new Error(+ output.lastAdvanceAt === null+ ? `${label} was not reached before ${deadline.bound}, without any shell output (received ${describeReceived(stream.read())})`+ : `${label} was not reached before ${deadline.bound}; the stream last advanced ${Math.round(now - output.lastAdvanceAt)}ms ago, ${output.advanced} characters counted from the shell's first output on (received ${describeReceived(stream.read())})`,+ );+ }+ if (predicate()) return { observedAt: now };+ await sleep(intervalMs);+ }+}++export async function waitForHarnessExit(+ stream: PtyStream,+ exitOf: () => { exitCode: number | undefined; signal: number | null } | null,+ label: string,+ bounds: HarnessCompletionBounds,+): Promise<HarnessCompletion> {+ const deadline = harnessExitDeadline(bounds);+ const startedAt = performance.now();+ for (;;) {+ const now = performance.now();+ if (now >= deadline.at) {+ const exit = exitOf();+ const elapsedMs = Math.round(now - bounds.after.observedAt);+ throw new Error(+ exit === null+ ? `${label} was not reached before ${deadline.bound}: no exit observed (${elapsedMs}ms after the completion it follows, received ${describeReceived(stream.read())})`+ : `${label} was not reached before ${deadline.bound}: the exit (${describeExit(exit)}) was observed only after that bound (${elapsedMs}ms after the completion it follows, received ${describeReceived(stream.read())})`,+ );+ }+ if (exitOf() !== null) return { observedAt: now };+ throwIfShellFailed(stream, label, startedAt);+ await sleep(DEFAULT_INTERVAL_MS);+ }+}++export function createHarnessReadinessAfterCompletion(+ stream: PtyStream,+ bounds: HarnessCompletionBounds,+): HarnessReadinessObserver {+ const windowEndsAt = bounds.after.observedAt + DEFAULT_STALL_MS;+ const initialDeadline = cappedByReportDeadline(+ bounds.initialDeadlineAt > windowEndsAt+ ? { at: bounds.initialDeadlineAt, bound: 'its initial allowance ended' }+ : { at: windowEndsAt, bound: `its ${DEFAULT_STALL_MS}ms first-output window ended` },+ bounds.reportDeadlineAt,+ );+ return createReadinessObserver(stream, initialDeadline, bounds.reportDeadlineAt);+}++interface QuietShellWait {+ quietSamples: number;+ quietWindowMs: number;+ pacing: { stallMs: number; intervalMs: number };+ isQuiet: () => boolean;+}++function quietShellWait(+ stream: PtyStream,+ label: string,+ options: Omit<ShellReadyOptions, 'backstopAt'>,+): QuietShellWait {const quietSamples = options.quietSamples ?? DEFAULT_QUIET_SAMPLES;const intervalMs = requireDuration(options.intervalMs ?? DEFAULT_READY_INTERVAL_MS,@@ -269,6 +438,27 @@ export async function waitForShellReady(`the stall window for ${label} must outlast the ${quietWindowMs}ms of quiet it counts as ready, got ${stallMs}ms`,);}+ let previous: string | null = null;+ let stable = 0;+ return {+ quietSamples,+ quietWindowMs,+ pacing: { stallMs, intervalMs },+ isQuiet: () => {+ const current = stream.read();+ stable = current.length > 0 && current === previous ? stable + 1 : 0;+ previous = current;+ return stable >= quietSamples;+ },+ };+}++export async function waitForShellReady(+ stream: PtyStream,+ label: string,+ options: ShellReadyOptions = {},+): Promise<void> {+ const { quietSamples, quietWindowMs, pacing, isQuiet } = quietShellWait(stream, label, options);if (options.backstopAt !== undefined && Number.isFinite(options.backstopAt)) {const containmentMs = options.backstopAt - performance.now();if (!(containmentMs > quietWindowMs)) {@@ -277,23 +467,10 @@ export async function waitForShellReady();}}- let previous: string | null = null;- let stable = 0;- await waitForCondition(- stream,- () => {- const current = stream.read();- stable = current.length > 0 && current === previous ? stable + 1 : 0;- previous = current;- return stable >= quietSamples;- },- label,- {- stallMs,- intervalMs,- ...(options.backstopAt === undefined ? {} : { backstopAt: options.backstopAt }),- },- );+ await waitForCondition(stream, isQuiet, label, {+ ...pacing,+ ...(options.backstopAt === undefined ? {} : { backstopAt: options.backstopAt }),+ });}export interface EvaluatedInputProbe {@@ -331,6 +508,7 @@ export function harnessTimeouts(platform: NodeJS.Platform): HarnessTimeouts {export interface HarnessBudget {grantMs(before: string): number;+ readonly reportDeadlineAt: number;}export class HarnessBudgetRefusal extends Error {@@ -347,6 +525,7 @@ export function createHarnessBudget(const startedAt = now();const remainingMs = (): number => budgetMs - (now() - startedAt);return {+ reportDeadlineAt: startedAt + budgetMs - reserveMs,grantMs: (before) => {const granted = Math.min(remainingMs() - reserveMs, remainingMs() / 2);if (granted <= 0) {@@ -455,16 +634,34 @@ async function waitForShellFirstOutput(}}-export async function waitForEvaluatedInput(+async function runEvaluatedInputProbe(stream: PtyStream,send: (data: string) => void,probe: EvaluatedInputProbe,- label: string,- options: EvaluatedInputOptions,+ firstOutputWait: () => Promise<{ firstOutputMs: number; firstOutput: string }>,+ prepareReply: (+ firstOutputMs: number,+ predicate: () => boolean,+ ) => (startedAt: number) => Promise<void>,): Promise<EvaluatedInputTiming> {if (probe.input.includes(probe.marker)) {throw new Error(`readiness probe input must not contain its marker: ${probe.marker}`);}+ const { firstOutputMs, firstOutput } = await firstOutputWait();+ const replyWait = prepareReply(firstOutputMs, () => stream.read().includes(probe.marker));+ const startedAt = performance.now();+ send(probe.input);+ await replyWait(startedAt);+ return { firstOutputMs, firstOutput, roundTripMs: performance.now() - startedAt };+}++export async function waitForEvaluatedInput(+ stream: PtyStream,+ send: (data: string) => void,+ probe: EvaluatedInputProbe,+ label: string,+ options: EvaluatedInputOptions,+): Promise<EvaluatedInputTiming> {const budgetMs = requireDuration(options.budgetMs, 'the budget', label);const roundTripStallMs = requireDuration(options.roundTripStallMs ?? DEFAULT_INPUT_READY_STALL_MS,@@ -472,24 +669,95 @@ export async function waitForEvaluatedInput(label,);const interval = options.intervalMs === undefined ? {} : { intervalMs: options.intervalMs };- const { firstOutputMs, firstOutput } = await waitForShellFirstOutput(stream, label, {- timeoutMs: budgetMs,- ...interval,- });- const remainingMs = budgetMs - firstOutputMs;- if (remainingMs <= 0) {- throw new HarnessBudgetRefusal(- `the ${Math.round(budgetMs)}ms grant for ${label} was spent before the round trip could start: first output after ${Math.round(firstOutputMs)}ms left nothing`,- );- }- const startedAt = performance.now();- send(probe.input);- await waitForCondition(stream, () => stream.read().includes(probe.marker), label, {- stallMs: roundTripStallMs,- backstopAt: startedAt + remainingMs,- ...interval,- });- return { firstOutputMs, firstOutput, roundTripMs: performance.now() - startedAt };+ return runEvaluatedInputProbe(+ stream,+ send,+ probe,+ () => waitForShellFirstOutput(stream, label, { timeoutMs: budgetMs, ...interval }),+ (firstOutputMs, predicate) => {+ const remainingMs = budgetMs - firstOutputMs;+ if (remainingMs <= 0) {+ throw new HarnessBudgetRefusal(+ `the ${Math.round(budgetMs)}ms grant for ${label} was spent before the round trip could start: first output after ${Math.round(firstOutputMs)}ms left nothing`,+ );+ }+ return (startedAt) =>+ waitForCondition(stream, predicate, label, {+ stallMs: roundTripStallMs,+ backstopAt: startedAt + remainingMs,+ ...interval,+ });+ },+ );+}++export function createHarnessReadinessObserver(+ stream: PtyStream,+ initialDeadlineAt: number,+ reportDeadlineAt: number,+): HarnessReadinessObserver {+ return createReadinessObserver(+ stream,+ { at: initialDeadlineAt, bound: 'its initial allowance ended' },+ reportDeadlineAt,+ );+}++function createReadinessObserver(+ stream: PtyStream,+ initialDeadline: WaitDeadline,+ reportDeadlineAt: number,+): HarnessReadinessObserver {+ const readiness: ReadinessState = {+ initialDeadline,+ reportDeadlineAt,+ output: createShellOutputObservation(),+ };+ return {+ waitForCondition: (predicate, label, options = {}) =>+ waitForReadinessCondition(stream, predicate, label, readiness, options).then(() => undefined),+ waitForCompletion: (predicate, label, options = {}) =>+ waitForReadinessCondition(stream, predicate, label, readiness, options),+ waitForShellReady: async (label, options = {}) => {+ const { pacing, isQuiet } = quietShellWait(stream, label, options);+ await waitForReadinessCondition(stream, isQuiet, label, readiness, pacing);+ },+ waitForEvaluatedInput: (send, probe, label, options = {}) => {+ const roundTripStallMs = requireDuration(+ options.roundTripStallMs ?? DEFAULT_INPUT_READY_STALL_MS,+ 'the round-trip stall window',+ label,+ );+ const interval = options.intervalMs === undefined ? {} : { intervalMs: options.intervalMs };+ return runEvaluatedInputProbe(+ stream,+ send,+ probe,+ async () => {+ const startedAt = performance.now();+ await waitForReadinessCondition(+ stream,+ () => shellOutputBeyondAttach(stream.read()).length > 0,+ label,+ readiness,+ { stallMs: DEFAULT_INPUT_READY_STALL_MS, ...interval },+ );+ return {+ firstOutputMs: performance.now() - startedAt,+ firstOutput: describeLeading(shellOutputBeyondAttach(stream.read())),+ };+ },+ (_firstOutputMs, predicate) => {+ return async () => {+ await waitForReadinessCondition(stream, predicate, label, readiness, {+ stallMs: roundTripStallMs,+ ...interval,+ });+ };+ },+ );+ },+ };}export function buildCwdFileProofCommand(platform: NodeJS.Platform, fileName: string): string {diff --git a/packages/desktop/tests/utility/pty-harness-environment.test-helper.ts b/packages/desktop/tests/utility/pty-harness-environment.test-helper.tsnew file mode 100644index 000000000..904a687f0--- /dev/null+++ b/packages/desktop/tests/utility/pty-harness-environment.test-helper.ts@@ -0,0 +1,396 @@+import { readFileSync, rmSync } from 'node:fs';+import { basename, join } from 'node:path';+import { vi } from 'vitest';+import type { PtyProcessLike, SpawnPty } from '../../src/utility/pty-host.ts';++export interface ControlledHarnessOptions {+ firstScenario: 'fast' | 'slow';+ platform?: 'win32' | 'linux';+ lifecycle?: {+ launchAtMs: number;+ exitAfterKillMs: number | null;+ outputAfterKill?: boolean;+ replacementOutputAfterCreateMs?: number | null;+ replacementExitAfterCreateMs?: number;+ firstKillDeliveryMs?: number;+ replacementCreateAdvanceMs?: number;+ };+ phase?:+ | 'launch-after-grant'+ | 'first-input-after-grant'+ | 'arithmetic-after-grant'+ | 'cwd-after-grant'+ | 'environment-after-grant'+ | 'silent-contained'+ | 'attach-only'+ | 'stale-launch'+ | 'echo-only'+ | 'input-window'+ | 'delayed-input-write'+ | 'delayed-exit'+ | 'continuous-wrong-output'+ | 'posix-late-quiet'+ | 'posix-unstable-quiet';+ launchReadiness?: 'advancing' | 'stuck' | 'dead';+ budgetOverride?: string;+}++export interface ControlledHarnessResult {+ exitCode: Parameters<typeof process.exit>[0];+ lines: string[];+ events: Array<{ shell: number; event: string; at: number }>;+}++export async function runControlledHarness(+ options: ControlledHarnessOptions,+): Promise<ControlledHarnessResult> {+ const result: ControlledHarnessResult = { exitCode: undefined, lines: [], events: [] };+ const dirs: string[] = [];+ const timers = new Set<ReturnType<typeof setTimeout>>();+ const platformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform');+ if (platformDescriptor === undefined) throw new Error('process.platform is unavailable');+ const systemRoot = 'C:\\Windows';+ const platform = options.platform ?? 'win32';+ const lifecycle =+ options.lifecycle ??+ (options.phase === 'delayed-exit' ? { launchAtMs: 44_000, exitAfterKillMs: 1_800 } : undefined);+ let shellCount = 0;+ const record = (shell: number, event: string): void => {+ result.events.push({ shell, event, at: performance.now() });+ };+ const spawn: SpawnPty = (file, args, spawnOptions): PtyProcessLike => {+ if (file.includes('no-such-shell-xyz')) throw new Error('file not found');+ const shell = ++shellCount;+ const launch = spawnOptions.env.OK_HARNESS_LAUNCH_TOKEN !== undefined;+ const spawnedAt = performance.now();+ let dataListener: (data: string) => void = () => undefined;+ let exitListener: Parameters<PtyProcessLike['onExit']>[0] = () => undefined;+ let closed = false;+ let killRequested = false;+ const ownedTimers = new Set<ReturnType<typeof setTimeout>>();+ const later = (delay: number, action: () => void): void => {+ const timer = setTimeout(() => {+ timers.delete(timer);+ ownedTimers.delete(timer);+ if (!closed) action();+ }, delay);+ timers.add(timer);+ ownedTimers.add(timer);+ };+ const emit = (data: string): void => {+ if (shell === 4 && killRequested && lifecycle?.outputAfterKill === false) return;+ record(shell, 'output');+ dataListener(data);+ };+ const at = (target: number, action: () => void): void => {+ later(Math.max(1, target - (performance.now() - spawnedAt)), action);+ };+ const progressingUntil = (last: number, cadence: number, message: string): void => {+ for (let when = cadence; when <= last; when += cadence) {+ at(when, () => emit(message));+ }+ };+ const exit = (code: number): void => {+ closed = true;+ for (const timer of ownedTimers) {+ clearTimeout(timer);+ timers.delete(timer);+ }+ ownedTimers.clear();+ record(shell, `exit:${code}`);+ exitListener({ exitCode: code });+ };+ record(shell, 'spawn');+ if (shell === 5 && lifecycle?.replacementCreateAdvanceMs !== undefined) {+ vi.advanceTimersByTime(lifecycle.replacementCreateAdvanceMs);+ record(shell, 'creation-delivery-complete');+ }+ if (+ platform === 'win32' &&+ !(shell === 5 && lifecycle?.replacementOutputAfterCreateMs !== undefined)+ ) {+ later(1, () => emit('\u001b[1t\u001b[c\u001b[?1004h\u001b[?9001h'));+ }+ if (shell === 5 && lifecycle?.replacementOutputAfterCreateMs != null) {+ later(lifecycle.replacementOutputAfterCreateMs, () => {+ record(shell, 'replacement-first-data');+ emit('PowerShell\r\nPS C:\\project> ');+ });+ }+ if (shell === 5 && lifecycle?.replacementExitAfterCreateMs !== undefined) {+ later(lifecycle.replacementExitAfterCreateMs, () => exit(1));+ }+ if (launch) {+ if (!Array.isArray(args) || !args.includes('-EncodedCommand')) {+ throw new Error('structured launch did not provide an encoded command');+ }+ if (options.phase === 'attach-only') {+ progressingUntil(84_000, 500, '\u001b[?1004h');+ } else {+ for (const when of [1_000, 8_000, 15_000, 22_000]) {+ later(when, () => emit('PowerShell startup\r\n'));+ }+ if (options.phase === 'launch-after-grant') {+ for (const when of [24_000, 25_000]) at(when, () => emit('PowerShell startup\r\n'));+ }+ if (lifecycle !== undefined) {+ for (let when = 29_000; when < lifecycle.launchAtMs; when += 7_000) {+ at(when, () => emit('PowerShell startup\r\n'));+ }+ }+ const launchAt = lifecycle?.launchAtMs ?? 23_000;+ at(options.phase === 'launch-after-grant' ? 26_000 : launchAt, () => {+ record(shell, 'launch-token');+ emit(`${spawnOptions.env.OK_HARNESS_LAUNCH_TOKEN}\r\nPS C:\\project> `);+ });+ }+ } else {+ if (platform === 'linux' && shell === 1 && options.phase === 'posix-late-quiet') {+ progressingUntil(14_400, 800, 'starting shell\r\n');+ at(15_200, () => emit('$ '));+ } else if (platform === 'linux' && shell === 1 && options.phase === 'posix-unstable-quiet') {+ at(1_000, () => emit('$ '));+ progressingUntil(29_000, 800, 'working\r\n');+ } else {+ if (!(shell === 5 && lifecycle?.replacementOutputAfterCreateMs !== undefined)) {+ later(1_000, () => emit(platform === 'win32' ? 'PowerShell\r\nPS C:\\project> ' : '$ '));+ }+ }+ }+ return {+ pid: 4_000 + shell,+ onData(listener) {+ dataListener = listener;+ },+ onExit(listener) {+ exitListener = listener;+ },+ write(data) {+ record(shell, 'input');+ const command = data.trim();+ const arithmetic =+ platform === 'win32'+ ? /^Write-Output "([^"]*)_\$\(\((\d+)\*(\d+)\)\)_([^"]*)"$/u.exec(command)+ : /^echo (.*)_\$\(\((\d+)\*(\d+)\)\)_(.*)$/u.exec(command);+ if (arithmetic !== null) {+ const output = `${arithmetic[1]}_${Number(arithmetic[2]) * Number(arithmetic[3])}_${arithmetic[4]}\r\n`;+ if (launch && arithmetic[1]?.startsWith('OK_INPUT_READY_')) {+ const readiness = options.launchReadiness ?? 'advancing';+ if (readiness === 'dead') {+ later(500, () => exit(1));+ } else if (readiness === 'advancing') {+ if (options.phase === 'echo-only') {+ later(500, () => emit(`${command}\r\n`));+ } else if (options.phase === 'input-window') {+ later(18_000, () => emit(output));+ } else if (options.phase === 'delayed-input-write') {+ vi.advanceTimersByTime(10_000);+ later(8_000, () => emit(output));+ } else if (options.phase === 'continuous-wrong-output') {+ progressingUntil(84_000, 500, 'working\r\n');+ } else if (options.phase === 'stale-launch') {+ later(500, () => emit('PS C:\\project> '));+ } else {+ later(500, () => emit('PS C:\\project> '));+ later(1_500, () => emit('evaluating\r\n'));+ later(2_500, () => emit('result\r\n'));+ later(3_000, () => {+ record(shell, 'evaluated-reply');+ emit(output);+ });+ }+ }+ } else if (+ shell === 1 &&+ options.firstScenario === 'slow' &&+ arithmetic[1]?.startsWith('OK_INPUT_READY_')+ ) {+ const readyAt = 31_000;+ for (const at of [6_000, 12_000, 18_000, 24_000, 30_000]) {+ later(at - (performance.now() - spawnedAt), () => emit('loading profile\r\n'));+ }+ later(Math.max(1, readyAt - (performance.now() - spawnedAt)), () => emit(output));+ } else if (+ shell === 1 &&+ options.phase === 'first-input-after-grant' &&+ arithmetic[1]?.startsWith('OK_INPUT_READY_')+ ) {+ progressingUntil(43_000, 7_000, 'loading profile\r\n');+ at(44_000, () => {+ record(shell, 'evaluated-reply');+ emit(output);+ });+ } else if (+ shell === 1 &&+ options.phase === 'silent-contained' &&+ arithmetic[1]?.startsWith('OK_INPUT_READY_')+ ) {+ later(18_000, () => {+ record(shell, 'evaluated-reply');+ emit(output);+ });+ } else if (+ shell === 1 &&+ options.phase === 'arithmetic-after-grant' &&+ arithmetic[1] === 'HARNESS'+ ) {+ progressingUntil(43_000, 7_000, 'calculating\r\n');+ at(44_000, () => {+ record(shell, 'arithmetic-output');+ emit(output);+ });+ } else {+ later(shell === 2 ? 3_000 : 1, () => emit(output));+ }+ return;+ }+ const environment =+ platform === 'win32'+ ? /^Write-Output "([^=]+)=\[\$env:([A-Z_]+)\]"$/u.exec(command)+ : /^echo "([^=]+)=\[\$([A-Z_]+)\]"$/u.exec(command);+ if (environment !== null) {+ const name = environment[2];+ if (name === undefined) throw new Error('environment command has no variable');+ if (shell === 2 && options.phase === 'environment-after-grant') {+ progressingUntil(42_000, 7_000, 'checking environment\r\n');+ at(43_000, () => {+ record(shell, 'environment-output');+ emit(`${environment[1]}=[${spawnOptions.env[name] ?? ''}]\r\n`);+ });+ } else {+ later(1, () => emit(`${environment[1]}=[${spawnOptions.env[name] ?? ''}]\r\n`));+ }+ return;+ }+ if (+ command.startsWith('Write-Output "CWD_PROOF=') ||+ command.startsWith("printf 'CWD_PROOF=%s\\n'")+ ) {+ const proof = readFileSync(join(spawnOptions.cwd, '.ok-pty-cwd-proof'), 'utf8');+ if (shell === 1 && options.phase === 'cwd-after-grant') {+ progressingUntil(43_000, 7_000, 'reading project\r\n');+ at(44_000, () => {+ record(shell, 'cwd-output');+ emit(`CWD_PROOF=${proof}\r\n`);+ });+ } else {+ later(1, () => emit(`CWD_PROOF=${proof}\r\n`));+ }+ return;+ }+ throw new Error(`unsupported controlled shell input: ${command}`);+ },+ resize() {},+ kill() {+ record(shell, 'kill-request');+ if (shell !== 4 || lifecycle === undefined) return exit(1);+ if (killRequested) return;+ killRequested = true;+ if (lifecycle.outputAfterKill) {+ for (let when = 500; when <= 30_000; when += 500) {+ later(when, () => emit('terminating\r\n'));+ }+ }+ if (lifecycle.exitAfterKillMs !== null) {+ later(lifecycle.exitAfterKillMs, () => exit(1));+ }+ if (lifecycle.firstKillDeliveryMs !== undefined) {+ vi.advanceTimersByTime(lifecycle.firstKillDeliveryMs);+ }+ },+ pause() {},+ resume() {},+ };+ };++ vi.resetModules();+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ vi.stubEnv('SystemRoot', systemRoot);+ if (platform === 'linux') vi.stubEnv('SHELL', '/bin/sh');+ vi.stubEnv('OK_PTY_HARNESS_BUDGET_MS', options.budgetOverride);+ vi.doMock('node:module', async (importOriginal) => {+ const original = await importOriginal<typeof import('node:module')>();+ return {+ ...original,+ createRequire: (url: string | URL) => {+ const realRequire = original.createRequire(url);+ return Object.assign(+ (name: string) => {+ if (name !== 'node-pty') {+ throw new Error(`unsupported controlled module: ${name}`);+ }+ return { spawn };+ },+ { resolve: realRequire.resolve },+ );+ },+ };+ });+ vi.doMock('node:child_process', async (importOriginal) => {+ const original = await importOriginal<typeof import('node:child_process')>();+ return {+ ...original,+ execFileSync: (file: string) => {+ if (!file.endsWith('\\where.exe')) {+ throw new Error('controlled harness cannot execute a native subprocess');+ }+ return 'C:\\Program Files\\PowerShell\\7\\pwsh.exe\r\n';+ },+ spawn: () => {+ throw new Error('controlled harness cannot spawn a native subprocess');+ },+ };+ });+ vi.doMock('node:fs', async (importOriginal) => {+ const original = await importOriginal<typeof import('node:fs')>();+ return {+ ...original,+ chmodSync: (path: Parameters<typeof original.chmodSync>[0]) => {+ if (basename(String(path)) !== 'spawn-helper') {+ throw new Error('controlled harness cannot chmod a native file');+ }+ record(0, 'helper-chmod-doubled');+ },+ existsSync: (path: Parameters<typeof original.existsSync>[0]) =>+ path === '/bin/sh' ||+ /(?:powershell|pwsh)\.exe$/iu.test(String(path)) ||+ original.existsSync(path),+ mkdtempSync: (prefix: string) => {+ const dir = original.mkdtempSync(prefix);+ dirs.push(dir);+ return dir;+ },+ };+ });+ const log = vi.spyOn(console, 'log').mockImplementation((line: string) => {+ result.lines.push(line);+ });+ const exit = vi.spyOn(process, 'exit').mockImplementation((code) => {+ result.exitCode = code;+ vi.clearAllTimers();+ return undefined as never;+ });+ const signal = vi.spyOn(process, 'kill').mockImplementation(() => {+ throw new Error('controlled harness cannot send a native signal');+ });+ try {+ Object.defineProperty(process, 'platform', { ...platformDescriptor, value: platform });+ await import('./pty-host.real-io-harness.ts');+ await vi.runAllTimersAsync();+ return result;+ } finally {+ Object.defineProperty(process, 'platform', platformDescriptor);+ for (const timer of timers) clearTimeout(timer);+ vi.clearAllTimers();+ vi.useRealTimers();+ log.mockRestore();+ exit.mockRestore();+ signal.mockRestore();+ vi.unstubAllEnvs();+ vi.doUnmock('node:module');+ vi.doUnmock('node:child_process');+ vi.doUnmock('node:fs');+ vi.resetModules();+ for (const dir of dirs) rmSync(dir, { recursive: true, force: true });+ }+}diff --git a/packages/desktop/tests/utility/pty-host.real-io-harness.ts b/packages/desktop/tests/utility/pty-host.real-io-harness.tsindex 7ee4bdd5e..9644ba39d 100644--- a/packages/desktop/tests/utility/pty-host.real-io-harness.ts+++ b/packages/desktop/tests/utility/pty-host.real-io-harness.ts@@ -11,17 +11,17 @@ import {import {buildCwdFileProofCommand,createHarnessBudget,+ createHarnessReadinessAfterCompletion,+ createHarnessReadinessObserver,createPtyHostProbe,HARNESS_REPORT_RESERVE_MS,+ HARNESS_WINDOWS_LAUNCH_STALL_MS,HarnessBudgetRefusal,- harnessExitAfterKillWait,+ type HarnessReadinessObserver,harnessTimeouts,- harnessWindowsLaunchWait,- remainingGrantMs,resolveHarnessBudgetMs,waitForCondition,- waitForEvaluatedInput,- waitForShellReady,+ waitForHarnessExit,} from '../support/pty-readiness.test-helper.ts';import { createHarnessScenarioRunner } from '../support/pty-startup-trace.test-helper.ts';import { harnessScenarioTitles } from '../support/real-io-harness-roster.test-helper.ts';@@ -75,15 +75,13 @@ async function waitForWindowsInputReady(host: ReturnType<typeof createHost>,ptyId: string,label: string,- deadlineAt: number,+ readiness: HarnessReadinessObserver,): Promise<void> {const probe = buildInputReadyProbe();- const timing = await waitForEvaluatedInput(- host.streamOf(ptyId),+ const timing = await readiness.waitForEvaluatedInput((data) => host.send({ type: 'input', ptyId, data }),{ input: `${probe.command}\r`, marker: probe.marker },label,- { budgetMs: remainingGrantMs(deadlineAt, label) },);console.log(`INPUT_READY ${label} firstOutputMs=${timing.firstOutputMs} firstOutput=${timing.firstOutput} readyMs=${timing.roundTripMs}`,@@ -94,13 +92,13 @@ async function waitForInteractiveShellReady(host: ReturnType<typeof createHost>,ptyId: string,label: string,- deadlineAt: number,+ readiness: HarnessReadinessObserver,): Promise<void> {if (process.platform === 'win32') {- await waitForWindowsInputReady(host, ptyId, label, deadlineAt);+ await waitForWindowsInputReady(host, ptyId, label, readiness);return;}- await waitForShellReady(host.streamOf(ptyId), label, { backstopAt: deadlineAt });+ await readiness.waitForShellReady(label);}async function main(): Promise<void> {@@ -113,34 +111,35 @@ async function main(): Promise<void> {writeFileSync(join(tmp, CWD_PROOF_FILE), cwdToken, 'utf8');const host = createHost(BASE_ENV);const io = host.streamOf('io');+ const readiness = createHarnessReadinessObserver(+ io,+ deadlineAt,+ harnessBudget.reportDeadlineAt,+ );host.send({ type: 'create', ptyId: 'io', cwd: tmp, cols: 80, rows: 24 });await waitForInteractiveShellReady(host,'io','interactive shell ready at project root',- deadlineAt,+ readiness,);host.send({type: 'input',ptyId: 'io',data: `${shellCommands.arithmetic('HARNESS', 6, 7, 'DONE')}\r`,});- await waitForCondition(- io,+ await readiness.waitForCondition(() => io.read().includes('HARNESS_42_DONE'),'evaluated command output',- { backstopAt: deadlineAt },);host.send({type: 'input',ptyId: 'io',data: `${buildCwdFileProofCommand(process.platform, CWD_PROOF_FILE)}\r`,});- await waitForCondition(- io,+ await readiness.waitForCondition(() => io.read().includes(`CWD_PROOF=${cwdToken}`),'relative sentinel read at project root',- { backstopAt: deadlineAt },);});@@ -151,12 +150,17 @@ async function main(): Promise<void> {OK_LOCK_KIND: 'interactive',});const env = host.streamOf('env');+ const readiness = createHarnessReadinessObserver(+ env,+ deadlineAt,+ harnessBudget.reportDeadlineAt,+ );host.send({ type: 'create', ptyId: 'env', cwd: tmp, cols: 80, rows: 24 });await waitForInteractiveShellReady(host,'env','interactive shell ready with desktop markers stripped',- deadlineAt,+ readiness,);host.send({type: 'input',@@ -168,11 +172,9 @@ async function main(): Promise<void> {ptyId: 'env',data: `${shellCommands.readEnvironment('OK_ELECTRON_PROTOCOL_HOST', 'HOST')}\r`,});- await waitForCondition(- env,+ await readiness.waitForCondition(() => env.read().includes('LOCK=[]') && env.read().includes('HOST=[]'),'empty markers in shell',- { backstopAt: deadlineAt },);if (env.read().includes('LOCK=[interactive]')) {throw new Error('OK_LOCK_KIND leaked into the shell');@@ -196,6 +198,11 @@ async function main(): Promise<void> {OK_HARNESS_LAUNCH_TOKEN: launchToken,});const launch = host.streamOf('launch');+ const readiness = createHarnessReadinessObserver(+ launch,+ deadlineAt,+ harnessBudget.reportDeadlineAt,+ );host.send({type: 'create',ptyId: 'launch',@@ -208,17 +215,16 @@ async function main(): Promise<void> {args: ['/d', '/c', 'echo', '%OK_HARNESS_LAUNCH_TOKEN%'],},});- await waitForCondition(- launch,+ await readiness.waitForCondition(() => launch.read().includes(launchToken),'PowerShell EncodedCommand output',- harnessWindowsLaunchWait(deadlineAt),+ { stallMs: HARNESS_WINDOWS_LAUNCH_STALL_MS },);await waitForWindowsInputReady(host,'launch','PowerShell remains interactive after EncodedCommand',- deadlineAt,+ readiness,);if (host.errorOf('launch') !== null) {throw new Error(`PowerShell launch failed: ${host.errorOf('launch')}`);@@ -229,24 +235,32 @@ async function main(): Promise<void> {await runner.run('host survives a PTY death and respawns', async (deadlineAt) => {const host = createHost(BASE_ENV);const first = host.streamOf('c1');- const second = host.streamOf('c2');- host.send({ type: 'create', ptyId: 'c1', cwd: tmp, cols: 80, rows: 24 });- await waitForCondition(first, () => first.read().length > 0, 'first shell prompt', {- backstopAt: deadlineAt,- });- host.send({ type: 'kill', ptyId: 'c1' });- await waitForCondition(+ const firstReadiness = createHarnessReadinessObserver(first,- () => host.exitOf('c1') !== null,- 'exit after kill',- harnessExitAfterKillWait(deadlineAt),+ deadlineAt,+ harnessBudget.reportDeadlineAt,+ );+ host.send({ type: 'create', ptyId: 'c1', cwd: tmp, cols: 80, rows: 24 });+ const firstOutput = await firstReadiness.waitForCompletion(+ () => first.read().length > 0,+ 'first shell prompt',);+ host.send({ type: 'kill', ptyId: 'c1' });+ const exited = await waitForHarnessExit(first, () => host.exitOf('c1'), 'exit after kill', {+ after: firstOutput,+ initialDeadlineAt: deadlineAt,+ reportDeadlineAt: harnessBudget.reportDeadlineAt,+ });+ const second = host.streamOf('c2');+ const secondReadiness = createHarnessReadinessAfterCompletion(second, {+ after: exited,+ initialDeadlineAt: deadlineAt,+ reportDeadlineAt: harnessBudget.reportDeadlineAt,+ });host.send({ type: 'create', ptyId: 'c2', cwd: tmp, cols: 80, rows: 24 });- await waitForCondition(- second,+ await secondReadiness.waitForCondition(() => second.read().length > 0,'second shell prompt (host survived)',- { backstopAt: deadlineAt },);});diff --git a/packages/desktop/tests/utility/pty-readiness.test.ts b/packages/desktop/tests/utility/pty-readiness.test.tsindex 7568101e9..12882b759 100644--- a/packages/desktop/tests/utility/pty-readiness.test.ts+++ b/packages/desktop/tests/utility/pty-readiness.test.ts@@ -6,16 +6,17 @@ import type { PtyProcessLike, PtySpawnOptions, SpawnPty } from '../../src/utilitimport {buildCwdFileProofCommand,createHarnessBudget,+ createHarnessReadinessAfterCompletion,+ createHarnessReadinessObserver,createPtyHostProbe,type EvaluatedInputOptions,type EvaluatedInputTiming,HARNESS_CHILD_KILL_WAIT_MS,+ HARNESS_EXIT_AFTER_KILL_STALL_MS,HARNESS_REPORT_RESERVE_MS,HARNESS_VERDICT_POLL_INTERVAL_MS,HarnessBudgetRefusal,- harnessExitAfterKillWait,harnessTimeouts,- harnessWindowsLaunchWait,type PtyStream,remainingGrantMs,resolveHarnessBudgetMs,@@ -24,8 +25,13 @@ import {type WaitOptions,waitForCondition,waitForEvaluatedInput,+ waitForHarnessExit,waitForShellReady,} from '../support/pty-readiness.test-helper.ts';+import {+ type ControlledHarnessResult,+ runControlledHarness,+} from './pty-harness-environment.test-helper.ts';interface FakeStream extends PtyStream {emit(chunk: string): void;@@ -49,11 +55,607 @@ function createFakeStream(): FakeStream {const FAST_READY = { intervalMs: 5, quietSamples: 20, stallMs: 5_000 } as const;+function lifecycleEventIndex(+ result: ControlledHarnessResult,+ shell: number,+ event: string,+): number {+ return result.events.findIndex((entry) => entry.shell === shell && entry.event === event);+}++function scenarioVerdicts(result: ControlledHarnessResult): string[] {+ return result.lines+ .filter((line) => /^(?:PASS|FAIL|REFUSED) /u.test(line))+ .map((line) => {+ const detailAt = line.indexOf(' :: ');+ return detailAt === -1 ? line : line.slice(0, detailAt);+ });+}++function scenarioFailureLine(result: ControlledHarnessResult, scenario: string): string {+ const prefix = `FAIL ${scenario} :: `;+ const line = result.lines.find((entry) => entry.startsWith(prefix));+ if (line === undefined) throw new Error(`the harness printed no line starting ${prefix}`);+ return line;+}++function expectLifecyclePredecessors(result: ControlledHarnessResult): void {+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines).toContain('PASS PowerShell executes a structured launch command');+ expect(lifecycleEventIndex(result, 3, 'launch-token')).toBeGreaterThan(-1);+ expect(lifecycleEventIndex(result, 3, 'evaluated-reply')).toBeGreaterThan(-1);+ expect(lifecycleEventIndex(result, 4, 'spawn')).toBeGreaterThan(-1);+ expect(lifecycleEventIndex(result, 4, 'output')).toBeGreaterThan(-1);+ expect(lifecycleEventIndex(result, 4, 'kill-request')).toBeGreaterThan(-1);+}++function expectLifecyclePass(result: ControlledHarnessResult): void {+ expectLifecyclePredecessors(result);+ const exited = lifecycleEventIndex(result, 4, 'exit:1');+ const respawned = lifecycleEventIndex(result, 5, 'spawn');+ expect(exited).toBeGreaterThan(lifecycleEventIndex(result, 4, 'kill-request'));+ expect(respawned).toBeGreaterThan(exited);+ expect(result.events.slice(respawned + 1)).toContainEqual(+ expect.objectContaining({ shell: 5, event: 'output' }),+ );+ expect(result.lines).toContain('PASS host survives a PTY death and respawns');+ expect(result.lines).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');+ expect(result.exitCode).toBe(0);+}++function expectLifecycleExitFailure(+ result: ControlledHarnessResult,+ verdicts: readonly string[],+ harnessResult: string,+): void {+ expectLifecyclePredecessors(result);+ expect(scenarioVerdicts(result)).toEqual(verdicts);+ expect(result.lines).toContain(harnessResult);+ expect(result.lines.join('\n')).toContain('exit after kill');+ expect(result.lines).not.toContain('PASS host survives a PTY death and respawns');+ expect(lifecycleEventIndex(result, 5, 'spawn')).toBe(-1);+ expect(result.exitCode).toBe(1);+}+afterEach(() => {vi.useRealTimers();vi.restoreAllMocks();});+describe('real harness readiness across sequential scenarios', () => {+ test('keeps an advancing later shell eligible after an earlier scenario passes slowly', async () => {+ const fast = await runControlledHarness({ firstScenario: 'fast' });+ expect(fast.lines.join('\n')).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');+ expect(fast.exitCode).toBe(0);+ const slow = await runControlledHarness({ firstScenario: 'slow' });+ expect(slow.lines).toContain('PASS real command round-trip at project root');+ expect(slow.lines).toContain('PASS strips desktop env markers from the shell');+ expect(slow.lines.join('\n')).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');+ expect(slow.exitCode).toBe(0);+ });++ test('keeps a delayed exit and respawn eligible after an earlier scenario passes slowly', async () => {+ for (const firstScenario of ['fast', 'slow'] as const) {+ const result = await runControlledHarness({ firstScenario, phase: 'delayed-exit' });+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines).toContain('PASS PowerShell executes a structured launch command');+ expect(result.lines).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');+ expect(result.exitCode).toBe(0);+ const exited = result.events.findIndex(+ (event) => event.shell === 4 && event.event === 'exit:1',+ );+ const respawned = result.events.findIndex(+ (event) => event.shell === 5 && event.event === 'spawn',+ );+ expect(exited).toBeGreaterThan(-1);+ expect(respawned).toBeGreaterThan(exited);+ }+ });++ test('accepts a silent exit acknowledgement after the kill request', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ lifecycle: { launchAtMs: 44_000, exitAfterKillMs: 1_800, outputAfterKill: false },+ });+ expectLifecyclePass(result);+ const killed = lifecycleEventIndex(result, 4, 'kill-request');+ const exited = lifecycleEventIndex(result, 4, 'exit:1');+ expect(result.events.slice(killed + 1, exited)).not.toContainEqual(+ expect.objectContaining({ shell: 4, event: 'output' }),+ );+ });++ test('waits for replacement output after accepting a late exit', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ lifecycle: {+ launchAtMs: 44_000,+ exitAfterKillMs: 1_200,+ replacementOutputAfterCreateMs: 400,+ },+ });+ expectLifecyclePass(result);+ expect(lifecycleEventIndex(result, 5, 'replacement-first-data')).toBeGreaterThan(+ lifecycleEventIndex(result, 5, 'spawn'),+ );+ });++ test('uses the exit window for a silent acknowledgement', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ lifecycle: { launchAtMs: 33_000, exitAfterKillMs: 9_000, outputAfterKill: false },+ });+ expectLifecyclePass(result);+ const killed = lifecycleEventIndex(result, 4, 'kill-request');+ const exited = lifecycleEventIndex(result, 4, 'exit:1');+ expect(result.events.slice(killed + 1, exited)).not.toContainEqual(+ expect.objectContaining({ shell: 4, event: 'output' }),+ );+ });++ test.each([null, 35_000])(+ 'never treats output during termination as an exit acknowledgement when exit is %s',+ async (exitAfterKillMs) => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ lifecycle: { launchAtMs: 26_000, exitAfterKillMs, outputAfterKill: true },+ });+ expectLifecycleExitFailure(+ result,+ [+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'PASS PowerShell executes a structured launch command',+ 'FAIL host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ],+ 'HARNESS_RESULT ok=4 fail=1 refused=0',+ );+ const killed = lifecycleEventIndex(result, 4, 'kill-request');+ expect(result.events.slice(killed + 1)).toContainEqual(+ expect.objectContaining({ shell: 4, event: 'output' }),+ );+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ },+ );++ test.each([+ {+ ending: 'stays alive',+ replacement: { replacementOutputAfterCreateMs: null },+ reason: `before its ${INITIAL_ALLOWANCE_BOUND} ended, ${NO_SHELL_OUTPUT_VERDICT}`,+ notReason: 'shell failed before',+ },+ {+ ending: 'then exits',+ replacement: { replacementOutputAfterCreateMs: null, replacementExitAfterCreateMs: 500 },+ reason: `shell failed before second shell prompt (host survived): ${SHELL_EXIT}`,+ notReason: NO_SHELL_OUTPUT_VERDICT,+ },+ ])(+ 'fails the respawn scenario when the replacement writes nothing and $ending',+ async ({ replacement, reason, notReason }) => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ lifecycle: { launchAtMs: 26_000, exitAfterKillMs: 1_200, ...replacement },+ });+ expectLifecyclePredecessors(result);+ const exited = lifecycleEventIndex(result, 4, 'exit:1');+ const respawned = lifecycleEventIndex(result, 5, 'spawn');+ expect(exited).toBeGreaterThan(-1);+ expect(respawned).toBeGreaterThan(exited);+ expect(result.events.slice(respawned + 1)).not.toContainEqual(+ expect.objectContaining({ shell: 5, event: 'output' }),+ );+ expect(result.lines.join('\n')).toContain('FAIL host survives a PTY death and respawns');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ expect(scenarioVerdicts(result)).toEqual([+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'PASS PowerShell executes a structured launch command',+ 'FAIL host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.exitCode).toBe(1);+ const respawnFailure = scenarioFailureLine(result, 'host survives a PTY death and respawns');+ expect(respawnFailure).toContain(reason);+ expect(respawnFailure).not.toContain(notReason);+ expect(respawnFailure).not.toContain(SHELL_PROGRESS_ADVANCED);+ },+ );++ test('rejects replacement output when that shell has already exited', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ lifecycle: {+ launchAtMs: 26_000,+ exitAfterKillMs: 1_200,+ replacementOutputAfterCreateMs: 1,+ replacementExitAfterCreateMs: 1,+ },+ });+ expectLifecyclePredecessors(result);+ const respawned = lifecycleEventIndex(result, 5, 'spawn');+ const output = lifecycleEventIndex(result, 5, 'output');+ const exited = lifecycleEventIndex(result, 5, 'exit:1');+ expect(respawned).toBeGreaterThan(lifecycleEventIndex(result, 4, 'exit:1'));+ expect(output).toBeGreaterThan(respawned);+ expect(exited).toBeGreaterThan(output);+ expect(result.lines.join('\n')).toContain('FAIL host survives a PTY death and respawns');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ expect(scenarioVerdicts(result)).toEqual([+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'PASS PowerShell executes a structured launch command',+ 'FAIL host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.exitCode).toBe(1);+ });++ test.each([null, 3_000])(+ 'keeps the report bound authoritative when exit is %s',+ async (exitAfterKillMs) => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ lifecycle: { launchAtMs: 44_000, exitAfterKillMs, outputAfterKill: true },+ });+ expectLifecycleExitFailure(+ result,+ [+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'PASS PowerShell executes a structured launch command',+ 'FAIL host survives a PTY death and respawns',+ 'REFUSED bad shell surfaces as a spawn failure',+ ],+ 'HARNESS_RESULT ok=3 fail=1 refused=1',+ );+ expect(result.lines.join('\n')).not.toContain('hard timeout');+ const killed = lifecycleEventIndex(result, 4, 'kill-request');+ expect(result.events.slice(killed + 1)).toContainEqual(+ expect.objectContaining({ shell: 4, event: 'output' }),+ );+ const respawnFailure = scenarioFailureLine(result, 'host survives a PTY death and respawns');+ expect(respawnFailure).toContain(REPORT_DEADLINE_BOUND);+ expect(respawnFailure).toContain(NO_EXIT_OBSERVED);+ },+ );++ test('does not re-anchor the exit window after delayed kill delivery', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ lifecycle: {+ launchAtMs: 33_000,+ exitAfterKillMs: 12_500,+ firstKillDeliveryMs: 9_000,+ },+ });+ expectLifecycleExitFailure(+ result,+ [+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'PASS PowerShell executes a structured launch command',+ 'FAIL host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ],+ 'HARNESS_RESULT ok=4 fail=1 refused=0',+ );+ const kills = result.events.filter(+ (event) => event.shell === 4 && event.event === 'kill-request',+ );+ expect(kills.length).toBeGreaterThan(1);+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ });++ test('preserves the original longer allowance for a fast predecessor', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ lifecycle: { launchAtMs: 44_000, exitAfterKillMs: 14_000, outputAfterKill: false },+ });+ expectLifecyclePass(result);+ });++ test('preserves the original replacement allowance after a fast exit', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ lifecycle: {+ launchAtMs: 44_000,+ exitAfterKillMs: 1_800,+ replacementOutputAfterCreateMs: 10_000,+ },+ });+ expectLifecyclePass(result);+ });++ test('keeps a slow replacement inside its own output window', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ lifecycle: {+ launchAtMs: 26_000,+ exitAfterKillMs: 3_000,+ replacementOutputAfterCreateMs: 10_000,+ },+ });+ expectLifecyclePredecessors(result);+ const exited = lifecycleEventIndex(result, 4, 'exit:1');+ const respawned = lifecycleEventIndex(result, 5, 'spawn');+ expect(exited).toBeGreaterThan(-1);+ expect(respawned).toBeGreaterThan(exited);+ expect(scenarioVerdicts(result)).toEqual([+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'PASS PowerShell executes a structured launch command',+ 'FAIL host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.lines.join('\n')).toContain('second shell prompt (host survived)');+ expect(result.exitCode).toBe(1);+ const respawnFailure = scenarioFailureLine(result, 'host survives a PTY death and respawns');+ expectOnlyBoundNamed(respawnFailure, FIRST_OUTPUT_WINDOW_BOUND);+ expect(respawnFailure).toContain(NO_SHELL_OUTPUT_VERDICT);+ });++ test('anchors replacement readiness before delayed creation returns', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ lifecycle: {+ launchAtMs: 26_000,+ exitAfterKillMs: 3_000,+ replacementCreateAdvanceMs: 9_000,+ replacementOutputAfterCreateMs: 500,+ },+ });+ expectLifecyclePredecessors(result);+ const exited = lifecycleEventIndex(result, 4, 'exit:1');+ const respawned = lifecycleEventIndex(result, 5, 'spawn');+ expect(exited).toBeGreaterThan(-1);+ expect(respawned).toBeGreaterThan(exited);+ expect(lifecycleEventIndex(result, 5, 'creation-delivery-complete')).toBeGreaterThan(respawned);+ expect(scenarioVerdicts(result)).toEqual([+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'PASS PowerShell executes a structured launch command',+ 'FAIL host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.lines.join('\n')).toContain('second shell prompt (host survived)');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ expect(result.exitCode).toBe(1);+ const respawnFailure = scenarioFailureLine(result, 'host survives a PTY death and respawns');+ expectOnlyBoundNamed(respawnFailure, FIRST_OUTPUT_WINDOW_BOUND);+ expect(respawnFailure).toContain(NO_SHELL_OUTPUT_VERDICT);+ });++ test('fails when a later shell stops producing output without answering', async () => {+ const result = await runControlledHarness({ firstScenario: 'slow', launchReadiness: 'stuck' });+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines.join('\n')).toContain(+ 'FAIL PowerShell executes a structured launch command',+ );+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.exitCode).toBe(1);+ });++ test('fails when a later shell exits before answering', async () => {+ const result = await runControlledHarness({ firstScenario: 'slow', launchReadiness: 'dead' });+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines.join('\n')).toContain(+ 'shell failed before PowerShell remains interactive after EncodedCommand: exited',+ );+ expect(scenarioVerdicts(result)).toEqual([+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'FAIL PowerShell executes a structured launch command',+ 'PASS host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.exitCode).toBe(1);+ });++ test('accepts a launch token after the original allowance and then evaluates input', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ phase: 'launch-after-grant',+ });+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');+ expect(result.exitCode).toBe(0);+ expect(result.events).toEqual(+ expect.arrayContaining([+ expect.objectContaining({ shell: 3, event: 'launch-token' }),+ expect.objectContaining({ shell: 3, event: 'evaluated-reply' }),+ ]),+ );+ expect(result.lines).toContain('PASS PowerShell executes a structured launch command');+ });++ test.each([+ { phase: 'first-input-after-grant', proof: 'evaluated-reply' },+ { phase: 'arithmetic-after-grant', proof: 'arithmetic-output' },+ { phase: 'cwd-after-grant', proof: 'cwd-output' },+ { phase: 'environment-after-grant', proof: 'environment-output' },+ ] as const)('continues $phase through its own output phase', async ({ phase, proof }) => {+ const result = await runControlledHarness({ firstScenario: 'fast', phase });+ expect(result.lines).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');+ expect(result.exitCode).toBe(0);+ expect(result.events).toContainEqual(expect.objectContaining({ event: proof }));+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ });++ test('keeps a silent but contained evaluated reply eligible', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ phase: 'silent-contained',+ });+ expect(result.events).toContainEqual(+ expect.objectContaining({ shell: 1, event: 'evaluated-reply' }),+ );+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');+ expect(result.exitCode).toBe(0);+ });++ test('keeps input evaluation within its current phase window', async () => {+ const result = await runControlledHarness({ firstScenario: 'slow', phase: 'input-window' });+ expect(result.events).toContainEqual(expect.objectContaining({ shell: 3, event: 'input' }));+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines.join('\n')).toContain(+ 'FAIL PowerShell executes a structured launch command',+ );+ expect(result.lines).toContain('PASS host survives a PTY death and respawns');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.exitCode).toBe(1);+ });++ test.each([+ { phase: 'attach-only', prerequisite: 'output' },+ { phase: 'stale-launch', prerequisite: 'launch-token' },+ { phase: 'echo-only', prerequisite: 'input' },+ ] as const)('does not accept $phase as a launch reply', async ({ phase, prerequisite }) => {+ const result = await runControlledHarness({ firstScenario: 'slow', phase });+ expect(result.events).toContainEqual(+ expect.objectContaining({ shell: 3, event: prerequisite }),+ );+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines.join('\n')).toContain(+ 'FAIL PowerShell executes a structured launch command',+ );+ expect(result.lines).toContain('PASS host survives a PTY death and respawns');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.exitCode).toBe(1);+ if (phase === 'attach-only') {+ const launchFailure = scenarioFailureLine(+ result,+ 'PowerShell executes a structured launch command',+ );+ expect(launchFailure).toContain(NO_SHELL_OUTPUT_VERDICT);+ expect(launchFailure).not.toContain(SHELL_PROGRESS_ADVANCED);+ }+ });++ test('retains the last shell observation when input delivery is delayed', async () => {+ const result = await runControlledHarness({+ firstScenario: 'slow',+ phase: 'delayed-input-write',+ });+ expect(result.events).toContainEqual(expect.objectContaining({ shell: 3, event: 'input' }));+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(scenarioVerdicts(result)).toEqual([+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'FAIL PowerShell executes a structured launch command',+ 'PASS host survives a PTY death and respawns',+ 'PASS bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines).toContain('PASS host survives a PTY death and respawns');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=1 refused=0');+ expect(result.exitCode).toBe(1);+ });++ test('reports a bounded failure for a shell that continually writes the wrong answer', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ phase: 'continuous-wrong-output',+ });+ expect(result.events).toContainEqual(+ expect.objectContaining({ shell: 3, event: 'launch-token' }),+ );+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(scenarioVerdicts(result)).toEqual([+ 'PASS real command round-trip at project root',+ 'PASS strips desktop env markers from the shell',+ 'FAIL PowerShell executes a structured launch command',+ 'REFUSED host survives a PTY death and respawns',+ 'REFUSED bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines.join('\n')).not.toContain('hard timeout during');+ expect(result.lines).toContain('HARNESS_RESULT ok=2 fail=1 refused=2');+ expect(result.exitCode).toBe(1);+ const launchFailure = scenarioFailureLine(+ result,+ 'PowerShell executes a structured launch command',+ );+ expect(launchFailure).toContain(REPORT_DEADLINE_BOUND);+ expect(launchFailure).toContain(SHELL_PROGRESS_ADVANCED);+ });++ test('refuses every scenario when the run has no admissible grant', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ budgetOverride: '1',+ });+ expect(result.lines.filter((line) => line.startsWith('REFUSED '))).toHaveLength(5);+ expect(result.lines).toContain('HARNESS_RESULT ok=0 fail=0 refused=5');+ expect(result.exitCode).toBe(1);+ });+});++describe('real harness POSIX quiet readiness', () => {+ test('completes all four scenarios with a prompt inside the allowance', async () => {+ const result = await runControlledHarness({ firstScenario: 'fast', platform: 'linux' });+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines).toContain('PASS host survives a PTY death and respawns');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=0 refused=0');+ expect(result.exitCode).toBe(0);+ });++ test('counts the full quiet sequence after a late prompt', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ platform: 'linux',+ phase: 'posix-late-quiet',+ });+ expect(result.lines).toContain('HARNESS_RESULT ok=4 fail=0 refused=0');+ expect(result.exitCode).toBe(0);+ expect(result.events).toContainEqual(expect.objectContaining({ shell: 1, event: 'output' }));+ expect(result.lines).toContain('PASS real command round-trip at project root');+ expect(result.lines).toContain('PASS strips desktop env markers from the shell');+ expect(result.lines).toContain('PASS host survives a PTY death and respawns');+ expect(result.lines).toContain('PASS bad shell surfaces as a spawn failure');+ });++ test('does not count a changing prompt as consecutive quiet polls', async () => {+ const result = await runControlledHarness({+ firstScenario: 'fast',+ platform: 'linux',+ phase: 'posix-unstable-quiet',+ });+ expect(result.events).toContainEqual(expect.objectContaining({ shell: 1, event: 'output' }));+ expect(scenarioVerdicts(result)).toEqual([+ 'FAIL real command round-trip at project root',+ 'REFUSED strips desktop env markers from the shell',+ 'REFUSED host survives a PTY death and respawns',+ 'REFUSED bad shell surfaces as a spawn failure',+ ]);+ expect(result.lines).toContain('HARNESS_RESULT ok=0 fail=1 refused=3');+ expect(result.lines).not.toContain('PASS real command round-trip at project root');+ expect(result.lines.join('\n')).not.toContain('hard timeout during');+ expect(result.exitCode).toBe(1);+ });+});+describe('shell readiness gate', () => {test('does not report ready while the shell is still producing startup output', async () => {const stream = createFakeStream();@@ -540,7 +1142,22 @@ const READINESS_CEILING_MS = 16_000;const SILENT_SHELL_VERDICT = 'without new shell output, the only progress signal this wait watches';const ROUND_TRIP_CONTAINMENT_VERDICT = 'input ready was not reached inside its';const NO_SHELL_OUTPUT_VERDICT = 'without any shell output';-const SHELL_PROGRESS_COUNTED = "characters past the shell's first output";+const SHELL_PROGRESS_COUNTED = "characters counted from the shell's first output on";+const SHELL_PROGRESS_ADVANCED = 'last advanced';+const INITIAL_ALLOWANCE_BOUND = 'initial allowance';+const STALL_WINDOW_BOUND = 'stall window';+const REPORT_DEADLINE_BOUND = 'report deadline';+const EXIT_WINDOW_BOUND = 'exit window';+const FIRST_OUTPUT_WINDOW_BOUND = 'first-output window';+const WAIT_BOUNDS = [+ INITIAL_ALLOWANCE_BOUND,+ STALL_WINDOW_BOUND,+ REPORT_DEADLINE_BOUND,+ EXIT_WINDOW_BOUND,+ FIRST_OUTPUT_WINDOW_BOUND,+] as const;+const NO_EXIT_OBSERVED = 'no exit observed';+const EXIT_OBSERVED_LATE = 'observed only after';function driveEvaluatingShell(stream: FakeStream,@@ -1591,7 +2208,7 @@ describe('a console host speaking before the shell is never counted as shell outexpect(verdict.message).toContain(CONTAINMENT_VERDICT);expect(verdict.message).not.toContain(SILENT_SHELL_VERDICT);expect(verdict.message).toContain(- `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters past the shell's first output`,+ `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters counted from the shell's first output on`,);} finally {stopHostNoise();@@ -1677,7 +2294,7 @@ describe("a plain wait counts progress from the shell's own first byte, whicheveexpect(verdict.settled).toBe('refused');expect(verdict.message).toContain(CONTAINMENT_VERDICT);expect(verdict.message).toContain(- `${verdict.buffer.length - prologue.length} characters past the shell's first output`,+ `${verdict.buffer.length - prologue.length} characters counted from the shell's first output on`,);} finally {stopShell();@@ -1837,7 +2454,7 @@ describe('a grant gone before a wait could poll once is refused as spent, not re);expect(stall).toContain('without any shell output');expect(stall).not.toContain(SILENT_SHELL_VERDICT);- expect(stall).not.toContain("characters past the shell's first output");+ expect(stall).not.toContain(SHELL_PROGRESS_COUNTED);});});@@ -1926,7 +2543,7 @@ describe('a wait window the containment cut is named as cut, so a red tells starexpect(verdict.message).toContain(CONTAINMENT_VERDICT);expect(verdict.message).not.toContain(SILENT_SHELL_VERDICT);expect(verdict.message).toContain(- `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters past the shell's first output`,+ `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters counted from the shell's first output on`,);} finally {stopShell();@@ -1961,7 +2578,7 @@ describe('a wait window the containment cut is named as cut, so a red tells starexpect(verdict.message).toContain(CONTAINMENT_VERDICT);expect(verdict.message).not.toContain(SILENT_SHELL_VERDICT);expect(verdict.message).toContain(- `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters past the shell's first output`,+ `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters counted from the shell's first output on`,);}expect(silentPast.verdict.message).toContain(@@ -2094,8 +2711,6 @@ describe('a wait whose readiness is silence is refused at entry when its stall wconst RECORDED_S1_BANNER_THEN_SILENCE ='\u001b[1t\u001b[c\u001b[?1004h\u001b[?9001hPowerShell 7.6.6\r\n\u001b]0;Administrator: C:\\Program Files\\PowerShell\\7\\pwsh.exe\u001b\\';-const RECORDED_LAUNCH_TITLE_THEN_SILENCE =- '\u001b[1t\u001b[c\u001b[?1004h\u001b[?9001h\u001b]0;Administrator: C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\u001b\\';const RECORDED_FIRST_OUTPUT_AT_MS = 3_647;const HARNESS_COMMAND_ECHO = 'Write-Output "HARNESS_$((6*7))_DONE"\r\n';const HARNESS_COMMAND_OUTPUT = 'HARNESS_42_DONE';@@ -2188,36 +2803,6 @@ const READINESS_SITES: readonly ReadinessSite[] = [};},},- {- site: 'the EncodedCommand launch wait',- platform: 'win32',- label: ENCODED_COMMAND_LABEL,- speaks: [- { atMs: 0, chunk: BUNDLED_ATTACH_PROLOGUE },- {- atMs: RECORDED_FIRST_OUTPUT_AT_MS,- chunk: RECORDED_LAUNCH_TITLE_THEN_SILENCE.slice(BUNDLED_ATTACH_PROLOGUE.length),- },- ],- probesWritten: null,- begin: (stream, label, containmentMs, answerAtMs) => {- const stopAnswer = answerAt(- stream,- answerAtMs,- `${WINDOWS_POWERSHELL_WRAP_TOGGLE}${LAUNCH_OUTPUT}`,- );- return {- settled: waitForCondition(- stream,- () => stream.read().includes(LAUNCH_OUTPUT),- label,- harnessWindowsLaunchWait(performance.now() + containmentMs),- ),- probesWritten: () => null,- dispose: stopAnswer,- };- },- },{site: 'a command wait after shell output',platform: 'win32',@@ -2428,12 +3013,9 @@ async function waitOutExitAfterKill(message: '',exited: false,};- void waitForCondition(- first,- () => host.exitOf('c1') !== null,- EXIT_AFTER_KILL_LABEL,- harnessExitAfterKillWait(startedAt + containmentMs),- ).then(+ void waitForCondition(first, () => host.exitOf('c1') !== null, EXIT_AFTER_KILL_LABEL, {+ backstopAt: startedAt + containmentMs,+ }).then(() => {verdict.settled = 'reached';verdict.atMs = performance.now() - startedAt;@@ -2474,3 +3056,279 @@ describe('a wait whose condition is the shell exiting keeps its meaning: the exiexpect(verdict.atMs).toBeLessThan(containmentMs * (1 + NOTICE_SHARE_OF_CONTAINMENT));});});++const BOUND_STALL_MS = 400;+const BOUND_POLL_MS = BOUND_STALL_MS / 20;+const BOUND_WAIT = { stallMs: BOUND_STALL_MS, intervalMs: BOUND_POLL_MS } as const;+const EXIT_WINDOW_MS = HARNESS_EXIT_AFTER_KILL_STALL_MS;+const EXIT_WAIT_BOUNDS = [+ {+ bound: EXIT_WINDOW_BOUND,+ initialMs: EXIT_WINDOW_MS / 2,+ reportMs: EXIT_WINDOW_MS * 2,+ endsAtMs: EXIT_WINDOW_MS,+ },+ {+ bound: INITIAL_ALLOWANCE_BOUND,+ initialMs: EXIT_WINDOW_MS * 2,+ reportMs: EXIT_WINDOW_MS * 4,+ endsAtMs: EXIT_WINDOW_MS * 2,+ },+ {+ bound: REPORT_DEADLINE_BOUND,+ initialMs: EXIT_WINDOW_MS * 2,+ reportMs: EXIT_WINDOW_MS / 2,+ endsAtMs: EXIT_WINDOW_MS / 2,+ },+] as const;+const WIN32_HARNESS_BUDGET_MS = harnessTimeouts('win32').budgetMs;++interface BoundedWaitVerdict {+ settled: 'pending' | 'reached' | 'refused';+ message: string;+}++async function settleBoundedWait(+ wait: Promise<unknown>,+ runForMs: number,+): Promise<BoundedWaitVerdict> {+ const verdict: BoundedWaitVerdict = { settled: 'pending', message: '' };+ void wait.then(+ () => {+ verdict.settled = 'reached';+ },+ (error: unknown) => {+ verdict.settled = 'refused';+ verdict.message = error instanceof Error ? error.message : String(error);+ },+ );+ await vi.advanceTimersByTimeAsync(runForMs);+ return verdict;+}++function expectOnlyBoundNamed(message: string, fired: (typeof WAIT_BOUNDS)[number]): void {+ expect(message).toContain(fired);+ for (const bound of WAIT_BOUNDS.filter((name) => name !== fired)) {+ expect(message).not.toContain(bound);+ }+}++function expectNoShellOutputReported(message: string): void {+ expect(message).toContain(NO_SHELL_OUTPUT_VERDICT);+ expect(message).not.toContain(SHELL_PROGRESS_ADVANCED);+ expect(message).not.toContain(SHELL_PROGRESS_COUNTED);+}++function expectShellProgressReported(message: string): void {+ expect(message).toContain(SHELL_PROGRESS_ADVANCED);+ expect(message).toContain(SHELL_PROGRESS_COUNTED);+ expect(message).not.toContain(NO_SHELL_OUTPUT_VERDICT);+}++function shellExitingAt(+ stream: FakeStream,+ atMs: number,+): {+ exitOf: () => { exitCode: number | undefined; signal: number | null } | null;+ dispose: () => void;+} {+ let exit: { exitCode: number | undefined; signal: number | null } | null = null;+ const timer = setTimeout(() => {+ exit = { exitCode: 1, signal: null };+ stream.fail(SHELL_EXIT);+ }, atMs);+ return { exitOf: () => exit, dispose: () => clearTimeout(timer) };+}++describe('a readiness or exit wait names the bound that ended it, and reports shell progress or an exit only when it saw one', () => {+ test('a readiness wait that sees nothing past attach is refused at its initial allowance and says it saw no shell output', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(ATTACH_PROLOGUE);+ const startedAt = performance.now();+ const readiness = createHarnessReadinessObserver(+ stream,+ startedAt + BOUND_STALL_MS * 2,+ startedAt + BOUND_STALL_MS * 4,+ );+ const verdict = await settleBoundedWait(+ readiness.waitForCondition(() => false, ENCODED_COMMAND_LABEL, BOUND_WAIT),+ BOUND_STALL_MS * 4 + BOUND_POLL_MS,+ );+ expect(verdict.settled).toBe('refused');+ expectOnlyBoundNamed(verdict.message, INITIAL_ALLOWANCE_BOUND);+ expectNoShellOutputReported(verdict.message);+ });++ test('a readiness wait that sees nothing past attach is refused at the report deadline that caps its allowance and says it saw no shell output', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(ATTACH_PROLOGUE);+ const startedAt = performance.now();+ const readiness = createHarnessReadinessAfterCompletion(stream, {+ after: { observedAt: startedAt },+ initialDeadlineAt: startedAt + BOUND_STALL_MS * 4,+ reportDeadlineAt: startedAt + BOUND_STALL_MS * 2,+ });+ const verdict = await settleBoundedWait(+ readiness.waitForCondition(() => false, ENCODED_COMMAND_LABEL, BOUND_WAIT),+ BOUND_STALL_MS * 4 + BOUND_POLL_MS,+ );+ expect(verdict.settled).toBe('refused');+ expectOnlyBoundNamed(verdict.message, REPORT_DEADLINE_BOUND);+ expectNoShellOutputReported(verdict.message);+ });++ test.each([+ {+ bound: FIRST_OUTPUT_WINDOW_BOUND,+ outlasts: INITIAL_ALLOWANCE_BOUND,+ initialMs: 0,+ reportMs: WIN32_HARNESS_BUDGET_MS,+ },+ {+ bound: INITIAL_ALLOWANCE_BOUND,+ outlasts: FIRST_OUTPUT_WINDOW_BOUND,+ initialMs: WIN32_HARNESS_BUDGET_MS,+ reportMs: WIN32_HARNESS_BUDGET_MS * 2,+ },+ ] as const)(+ 'a readiness wait that follows a completion and sees nothing past attach is refused at its $bound when that outlasts its $outlasts, and says it saw no shell output',+ async ({ bound, initialMs, reportMs }) => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(ATTACH_PROLOGUE);+ const completedAt = performance.now();+ const readiness = createHarnessReadinessAfterCompletion(stream, {+ after: { observedAt: completedAt },+ initialDeadlineAt: completedAt + initialMs,+ reportDeadlineAt: completedAt + reportMs,+ });+ const verdict = await settleBoundedWait(+ readiness.waitForCondition(() => false, ENCODED_COMMAND_LABEL, BOUND_WAIT),+ reportMs + BOUND_POLL_MS,+ );+ expect(verdict.settled).toBe('refused');+ expectOnlyBoundNamed(verdict.message, bound);+ expectNoShellOutputReported(verdict.message);+ },+ );++ test('a readiness wait whose shell spoke and then stayed silent for its stall window is refused at that window and says how far the shell got', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`);+ const startedAt = performance.now();+ const readiness = createHarnessReadinessObserver(+ stream,+ startedAt + BOUND_STALL_MS / 2,+ startedAt + BOUND_STALL_MS * 4,+ );+ const verdict = await settleBoundedWait(+ readiness.waitForCondition(() => false, ENCODED_COMMAND_LABEL, BOUND_WAIT),+ BOUND_STALL_MS * 4 + BOUND_POLL_MS,+ );+ expect(verdict.settled).toBe('refused');+ expectOnlyBoundNamed(verdict.message, STALL_WINDOW_BOUND);+ expectShellProgressReported(verdict.message);+ });++ test('a readiness wait whose initial allowance outlasts the stall window after the last shell output is refused at that allowance and says how far the shell got', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`);+ const startedAt = performance.now();+ const readiness = createHarnessReadinessObserver(+ stream,+ startedAt + BOUND_STALL_MS * 3,+ startedAt + BOUND_STALL_MS * 6,+ );+ const verdict = await settleBoundedWait(+ readiness.waitForCondition(() => false, ENCODED_COMMAND_LABEL, BOUND_WAIT),+ BOUND_STALL_MS * 6 + BOUND_POLL_MS,+ );+ expect(verdict.settled).toBe('refused');+ expectOnlyBoundNamed(verdict.message, INITIAL_ALLOWANCE_BOUND);+ expectShellProgressReported(verdict.message);+ });++ test('a readiness wait whose shell is still writing when the report deadline passes is refused at that deadline and says how far the shell got', async () => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`);+ const stopShell = scheduleEmissions(+ stream,+ evenCadence(SHELL_WRITTEN_STEP, BOUND_STALL_MS / 4, BOUND_STALL_MS * 3),+ );+ const startedAt = performance.now();+ const readiness = createHarnessReadinessObserver(+ stream,+ startedAt + BOUND_STALL_MS / 2,+ startedAt + BOUND_STALL_MS * 2,+ );+ try {+ const verdict = await settleBoundedWait(+ readiness.waitForCondition(() => false, ENCODED_COMMAND_LABEL, BOUND_WAIT),+ BOUND_STALL_MS * 3,+ );+ expect(verdict.settled).toBe('refused');+ expectOnlyBoundNamed(verdict.message, REPORT_DEADLINE_BOUND);+ expectShellProgressReported(verdict.message);+ } finally {+ stopShell();+ }+ });++ test.each(EXIT_WAIT_BOUNDS)(+ 'an exit wait that sees no exit is refused at its $bound and says no exit was observed',+ async ({ bound, initialMs, reportMs }) => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`);+ const startedAt = performance.now();+ const verdict = await settleBoundedWait(+ waitForHarnessExit(stream, () => null, EXIT_AFTER_KILL_LABEL, {+ after: { observedAt: startedAt },+ initialDeadlineAt: startedAt + initialMs,+ reportDeadlineAt: startedAt + reportMs,+ }),+ Math.max(initialMs, reportMs),+ );+ expect(verdict.settled).toBe('refused');+ expectOnlyBoundNamed(verdict.message, bound);+ expect(verdict.message).toContain(NO_EXIT_OBSERVED);+ expect(verdict.message).not.toContain(EXIT_OBSERVED_LATE);+ },+ );++ test.each(EXIT_WAIT_BOUNDS)(+ 'an exit that lands just as the $bound ends an exit wait is reported as observed only after that bound, not as no exit',+ async ({ bound, initialMs, reportMs, endsAtMs }) => {+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });+ const stream = createFakeStream();+ stream.emit(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`);+ const shell = shellExitingAt(stream, endsAtMs);+ const startedAt = performance.now();+ let exitedWhenSettled = false;+ try {+ const verdict = await settleBoundedWait(+ waitForHarnessExit(stream, shell.exitOf, EXIT_AFTER_KILL_LABEL, {+ after: { observedAt: startedAt },+ initialDeadlineAt: startedAt + initialMs,+ reportDeadlineAt: startedAt + reportMs,+ }).finally(() => {+ exitedWhenSettled = shell.exitOf() !== null;+ }),+ Math.max(initialMs, reportMs),+ );+ expect(verdict.settled).toBe('refused');+ expect(exitedWhenSettled).toBe(true);+ expect(verdict.message).toContain(EXIT_OBSERVED_LATE);+ expect(verdict.message).not.toContain(NO_EXIT_OBSERVED);+ expectOnlyBoundNamed(verdict.message, bound);+ } finally {+ shell.dispose();+ }+ },+ );+});