Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

141 KB
Raw Blame
import { readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { dirname, join } from 'node:path';
import { afterEach, describe, expect, test, vi } from 'vitest';
import type { PtyProcessLike, PtySpawnOptions, SpawnPty } from '../../src/utility/pty-host.ts';
import {
  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,
  harnessTimeouts,
  type PtyStream,
  remainingGrantMs,
  resolveHarnessBudgetMs,
  type ShellReadyOptions,
  shellOutputBeyondAttach,
  type WaitOptions,
  waitForCondition,
  waitForEvaluatedInput,
  waitForHarnessExit,
  waitForShellReady,
} from '../support/pty-readiness.test-helper.ts';
import {
  type ControlledHarnessOptions,
  type ControlledHarnessResult,
  runControlledHarness,
} from './pty-harness-environment.test-helper.ts';

interface FakeStream extends PtyStream {
  emit(chunk: string): void;
  fail(reason: string): void;
}

function createFakeStream(): FakeStream {
  let text = '';
  let failure: string | null = null;
  return {
    read: () => text,
    failure: () => failure,
    emit(chunk) {
      text += chunk;
    },
    fail(reason) {
      failure = reason;
    },
  };
}

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.each([
    {
      name: 'initial interactive readiness',
      options: { firstScenario: 'fast', silentAt: 'initial-input' },
      shell: 1,
      predecessor: 'unanswered-readiness-input',
      scenario: 'real command round-trip at project root',
    },
    {
      name: 'env-stripped interactive readiness',
      options: { firstScenario: 'fast', silentAt: 'environment-input' },
      shell: 2,
      predecessor: 'unanswered-readiness-input',
      scenario: 'strips desktop env markers from the shell',
    },
    {
      name: 'structured launch token',
      options: { firstScenario: 'fast', silentAt: 'launch-token' },
      shell: 3,
      predecessor: 'launch-startup-output',
      scenario: 'PowerShell executes a structured launch command',
    },
    {
      name: 'post-launch evaluated reply',
      options: { firstScenario: 'slow', launchReadiness: 'stuck' },
      shell: 3,
      predecessor: 'input',
      scenario: 'PowerShell executes a structured launch command',
    },
    {
      name: 'post-output command marker',
      options: { firstScenario: 'fast', silentAt: 'command-output' },
      shell: 1,
      predecessor: 'command-input',
      scenario: 'real command round-trip at project root',
    },
    {
      name: 'silent replacement output',
      options: {
        firstScenario: 'fast',
        lifecycle: {
          launchAtMs: 26_000,
          exitAfterKillMs: 1_200,
          replacementOutputAfterCreateMs: null,
        },
      },
      shell: 5,
      predecessor: 'spawn',
      scenario: 'host survives a PTY death and respawns',
    },
  ] as const)(
    '$name retains an OS observation in the real harness before release',
    async ({ options, shell, predecessor, scenario }) => {
      const result = await runControlledHarness(options as ControlledHarnessOptions);
      const reached = lifecycleEventIndex(result, shell, predecessor);
      expect(reached).toBeGreaterThan(-1);
      if (shell === 5) {
        expect(reached).toBeGreaterThan(lifecycleEventIndex(result, 4, 'exit:1'));
      }
      expect(scenarioVerdicts(result)).toContain(`FAIL ${scenario}`);
      expect(result.lines.join('\n')).not.toContain('hard timeout during');
      const failure = result.traceEvents.find(
        (entry) => entry.stage === 'snapshot' && entry.reason === 'failure',
      );
      expect(failure).toEqual(expect.objectContaining({ publicPid: 4_000 + shell }));
      expect(result.traceEvents).toContainEqual(
        expect.objectContaining({
          event: 'pty-host-startup',
          stage: 'observer-unavailable',
          traceId: failure?.traceId,
          attempt: failure?.attempt,
        }),
      );
      const observation = result.traceEvents.find(
        (entry) =>
          entry.event === 'pty-host-startup' &&
          entry.stage === 'os-snapshot' &&
          entry.traceId === failure?.traceId &&
          entry.attempt === failure?.attempt,
      );
      expect(observation?.observation).toEqual(
        expect.objectContaining({
          status: 'unavailable',
          reason: 'query-start-failed',
          shell: { status: 'unavailable', reason: 'query-start-failed' },
          console: { status: 'unavailable', reason: 'query-start-failed' },
          worker: { status: 'unavailable', reason: 'worker-unavailable' },
        }),
      );
      const observed = result.events.findIndex((entry) => entry.trace === observation);
      expect(observed).toBeGreaterThan(reached);
      expect(
        result.events.findIndex(
          (entry, index) =>
            index > reached &&
            index < observed &&
            entry.shell === 0 &&
            entry.event === 'query-start',
        ),
      ).toBeGreaterThan(-1);
      expect(lifecycleEventIndex(result, shell, 'kill-request')).toBeGreaterThan(observed);
      if (shell === 1 && predecessor === 'unanswered-readiness-input') {
        const queryStart = lifecycleEventIndex(result, 0, 'query-start');
        expect(queryStart).toBeGreaterThan(reached);
        expect(lifecycleEventIndex(result, shell, 'kill-request')).toBeGreaterThan(queryStart);
      }
      if (shell === 5) {
        expect(
          result.events.filter((entry) => entry.shell === 0 && entry.event === 'query-start'),
        ).toHaveLength(1);
      }
      expect(JSON.stringify(observation)).not.toContain('PowerShell startup');
      expect(JSON.stringify(observation)).not.toContain(
        'controlled harness cannot spawn a native subprocess',
      );
    },
  );

  test('a delayed OS query cannot rewrite a released failure attempt', async () => {
    const result = await runControlledHarness({
      firstScenario: 'fast',
      silentAt: 'initial-input',
      queryMode: 'delayed-invalid',
    });
    const sent = lifecycleEventIndex(result, 1, 'unanswered-readiness-input');
    expect(sent).toBeGreaterThan(-1);
    expect(scenarioVerdicts(result)).toContain('FAIL real command round-trip at project root');
    const failure = result.traceEvents.find(
      (entry) => entry.stage === 'snapshot' && entry.reason === 'failure',
    );
    expect(failure).toEqual(expect.objectContaining({ publicPid: 4_001 }));
    const release = lifecycleEventIndex(result, 1, 'kill-request');
    expect(release).toBeGreaterThan(sent);
    const observation = result.traceEvents.find(
      (entry) =>
        entry.event === 'pty-host-startup' &&
        entry.stage === 'os-snapshot' &&
        entry.traceId === failure?.traceId &&
        entry.attempt === failure?.attempt,
    );
    expect(observation).toEqual(expect.objectContaining({ observation: expect.any(Object) }));
    expect(lifecycleEventIndex(result, 0, 'query-start')).toBeGreaterThan(sent);
    expect(lifecycleEventIndex(result, 0, 'late-query-output')).toBeGreaterThan(release);
    expect(result.delayedQuery).toEqual(
      expect.objectContaining({
        traceBeforeDelivery: expect.any(String),
        linesBeforeDelivery: expect.any(String),
      }),
    );
    expect(result.events.findIndex((entry) => entry.trace === observation)).toBeLessThan(release);
    expect(JSON.stringify(result.traceEvents)).toBe(result.delayedQuery?.traceBeforeDelivery);
    expect(JSON.stringify(result.lines)).toBe(result.delayedQuery?.linesBeforeDelivery);
    expect(scenarioVerdicts(result)).toContain('FAIL real command round-trip at project root');
    expect(JSON.stringify(result.traceEvents)).not.toContain('private shell text');
    expect(JSON.stringify(result.traceEvents)).not.toContain('private fixture stderr');
  });

  test('passing cleanup and spent-budget refusal issue no OS query', async () => {
    const passing = await runControlledHarness({ firstScenario: 'fast' });
    expect(passing.lines).toContain('HARNESS_RESULT ok=5 fail=0 refused=0');
    expect(passing.exitCode).toBe(0);
    expect(lifecycleEventIndex(passing, 1, 'kill-request')).toBeGreaterThan(-1);
    expect(passing.events.filter((entry) => entry.event === 'query-start')).toEqual([]);

    const refused = await runControlledHarness({ firstScenario: 'fast', budgetOverride: '1' });
    expect(refused.lines.filter((line) => line.startsWith('REFUSED '))).toHaveLength(5);
    expect(refused.lines).toContain('HARNESS_RESULT ok=0 fail=0 refused=5');
    expect(refused.events.filter((entry) => entry.event === 'query-start')).toEqual([]);
  });

  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);
    const launchToken = lifecycleEventIndex(result, 3, 'launch-token');
    const queryStart = lifecycleEventIndex(result, 0, 'query-start');
    const release = lifecycleEventIndex(result, 3, 'kill-request');
    const failure = result.traceEvents.find(
      (entry) => entry.stage === 'snapshot' && entry.reason === 'failure',
    );
    const observation = result.traceEvents.find(
      (entry) =>
        entry.event === 'pty-host-startup' &&
        entry.stage === 'os-snapshot' &&
        entry.traceId === failure?.traceId &&
        entry.attempt === failure?.attempt,
    );
    const observed = result.events.findIndex((entry) => entry.trace === observation);
    expect(queryStart).toBeGreaterThan(launchToken);
    expect(observation).toEqual(expect.objectContaining({ observation: expect.any(Object) }));
    expect(observed).toBeGreaterThan(queryStart);
    expect(release).toBeGreaterThan(observed);
  });

  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();
    stream.emit('\u001b[2J\u001b[H');
    const chunks = ['loading profile\r\n', 'startup notice\r\n', 'PS C:\\project> '];
    const timers = chunks.map((chunk, index) =>
      setTimeout(() => stream.emit(chunk), (index + 1) * 40),
    );
    try {
      await waitForShellReady(stream, 'shell ready', FAST_READY);
      expect(stream.read()).toContain('PS C:\\project> ');
    } finally {
      for (const timer of timers) clearTimeout(timer);
    }
  });

  test('the quiet window is every sample the caller asked for, each separated by a real poll', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const quietSamples = 5;
    const intervalMs = 50;
    const stream = createFakeStream();
    stream.emit('PS C:\\project> ');
    const startedAt = performance.now();
    let elapsedMs = Number.NaN;
    const pending = waitForShellReady(stream, 'shell ready', { quietSamples, intervalMs }).then(
      () => {
        elapsedMs = performance.now() - startedAt;
      },
    );
    await vi.advanceTimersByTimeAsync((quietSamples + 2) * intervalMs);
    await pending;
    expect(elapsedMs).toBe(quietSamples * intervalMs);
  });

  test('reports ready once the stream settles instead of waiting out the budget', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit('PS C:\\project> ');
    const startedAt = performance.now();
    let elapsedMs = Number.NaN;
    const pending = waitForShellReady(stream, 'shell ready', FAST_READY).then(() => {
      elapsedMs = performance.now() - startedAt;
    });
    await vi.advanceTimersByTimeAsync(FAST_READY.stallMs);
    await pending;
    expect(elapsedMs).toBeLessThan(FAST_READY.stallMs);
  });

  test('never reports a silent shell ready', async () => {
    const stream = createFakeStream();
    await expect(
      waitForShellReady(stream, 'shell ready', { intervalMs: 5, quietSamples: 3, stallMs: 120 }),
    ).rejects.toThrow(/shell ready/u);
  });

  test('surfaces a spawn failure that lands during startup', async () => {
    const stream = createFakeStream();
    stream.emit('\u001b[2J');
    const timer = setTimeout(() => stream.fail('spawn-error: File not found'), 20);
    try {
      await expect(waitForShellReady(stream, 'shell ready', FAST_READY)).rejects.toThrow(
        /File not found/u,
      );
    } finally {
      clearTimeout(timer);
    }
  });
});

describe('attach classifier direction', () => {
  const INTRODUCER = '\u001b]0;';

  test('a title the classifier cannot terminate stops withholding once a control byte follows', () => {
    const wedge = `${INTRODUCER}C:\\no\\terminator\r\nPowerShell 7.6.5`;
    expect(shellOutputBeyondAttach(wedge)).toBe('\r\nPowerShell 7.6.5');
  });

  test('a read cut inside a title is still attach, so the gate keeps waiting', () => {
    expect(shellOutputBeyondAttach(`${INTRODUCER}C:\\partial`)).toBe('');
    expect(shellOutputBeyondAttach('\u001b]')).toBe('');
    expect(shellOutputBeyondAttach('\u001b[?100')).toBe('');
  });

  test('a generic frame member as the shell own first output is consumed, not treated as speech', () => {
    expect(shellOutputBeyondAttach('\u001b[2Jcleared by a profile')).toBe('cleared by a profile');
    expect(shellOutputBeyondAttach('\u001b[2J')).toBe('');
  });
});

describe('harness budget override', () => {
  test('an override may narrow the declared budget but never widen it', () => {
    expect(resolveHarnessBudgetMs('1', 85_000)).toBe(1);
    expect(resolveHarnessBudgetMs('84999', 85_000)).toBe(84_999);
    expect(resolveHarnessBudgetMs('85001', 85_000)).toBe(85_000);
    expect(resolveHarnessBudgetMs('999999999', 85_000)).toBe(85_000);
  });

  test('an absent or empty override reads as unset and falls back to the declared budget', () => {
    for (const raw of [undefined, '']) {
      expect(resolveHarnessBudgetMs(raw, 85_000)).toBe(85_000);
    }
  });

  test('an override that is present but cannot bound the run is named instead of ignored', () => {
    for (const raw of ['soon', '0', '-1', 'NaN', 'Infinity']) {
      expect(() => resolveHarnessBudgetMs(raw, 85_000)).toThrow(
        /the OK_PTY_HARNESS_BUDGET_MS override for the harness budget must be a positive finite duration/u,
      );
    }
  });
});

describe('harness budget clock', () => {
  const FAKE_BUDGET_MS = 1_000;
  const FAKE_RESERVE_MS = 100;

  test('every later grant is strictly smaller than the one before it', () => {
    let nowMs = 0;
    const budget = createHarnessBudget(FAKE_BUDGET_MS, FAKE_RESERVE_MS, () => nowMs);
    const atStart = budget.grantMs('this scenario started');
    nowMs = FAKE_BUDGET_MS / 4;
    expect(budget.grantMs('this scenario started')).toBeLessThan(atStart);
  });

  test('a grant the reserve or the deadline has eaten is named in the harness wording, not as a bad argument', () => {
    let nowMs = 0;
    const budget = createHarnessBudget(FAKE_BUDGET_MS, FAKE_RESERVE_MS, () => nowMs);
    expect(budget.grantMs('this scenario started')).toBeGreaterThan(0);
    nowMs = FAKE_BUDGET_MS - FAKE_RESERVE_MS;
    expect(() => budget.grantMs('this scenario started')).toThrow(
      `the ${FAKE_BUDGET_MS}ms harness budget was spent before this scenario started`,
    );
    nowMs = FAKE_BUDGET_MS * 2;
    expect(() => budget.grantMs('this scenario started')).toThrow(
      `the ${FAKE_BUDGET_MS}ms harness budget was spent before this scenario started`,
    );
  });

  test('the default clock is the monotonic one the harness runs on', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const wallNow = vi.spyOn(Date, 'now');
    const budget = createHarnessBudget(FAKE_BUDGET_MS, FAKE_RESERVE_MS);
    const atStart = budget.grantMs('this scenario started');
    await vi.advanceTimersByTimeAsync(FAKE_BUDGET_MS / 4);
    const afterAdvance = budget.grantMs('this scenario started');
    expect(wallNow).not.toHaveBeenCalled();
    expect(afterAdvance).toBeLessThan(atStart);
  });

  test('a budget or reserve that is not a positive finite duration is refused at construction', () => {
    expect(() => createHarnessBudget(Number.NaN, FAKE_RESERVE_MS)).toThrow(
      'the budget for the harness must be a positive finite duration in milliseconds, got NaN',
    );
    expect(() => createHarnessBudget(FAKE_BUDGET_MS, Number.POSITIVE_INFINITY)).toThrow(
      'the report reserve for the harness must be a positive finite duration in milliseconds, got Infinity',
    );
  });
});

describe('harness timeout ladder', () => {
  test('each rung leaves the one below it room to report its own verdict', () => {
    for (const platform of ['win32', 'darwin', 'linux'] as const) {
      const ladder = harnessTimeouts(platform);
      expect(ladder.budgetMs).toBeGreaterThan(0);
      expect(ladder.verdictDeadlineMs).toBeGreaterThan(ladder.budgetMs);
      expect(ladder.testTimeoutMs).toBeGreaterThan(ladder.verdictDeadlineMs);
      expect(ladder.verdictDeadlineMs - ladder.budgetMs).toBeGreaterThan(
        HARNESS_VERDICT_POLL_INTERVAL_MS,
      );
      expect(ladder.testTimeoutMs - ladder.verdictDeadlineMs).toBeGreaterThan(
        HARNESS_CHILD_KILL_WAIT_MS,
      );
    }
  });

  test('windows gets the wider ladder its slower shell startup needs', () => {
    expect(harnessTimeouts('win32').budgetMs).toBeGreaterThan(harnessTimeouts('linux').budgetMs);
  });
});

describe('cwd file proof command', () => {
  test('reads a relative sentinel without embedding its random contents', () => {
    expect(buildCwdFileProofCommand('win32', '.ok-cwd-proof')).toBe(
      `Write-Output "CWD_PROOF=$(Get-Content -Raw -LiteralPath './.ok-cwd-proof')"`,
    );
    expect(buildCwdFileProofCommand('linux', '.ok-cwd-proof')).toBe(
      `printf 'CWD_PROOF=%s\\n' "$(cat './.ok-cwd-proof')"`,
    );
  });

  test('rejects a sentinel name that could inject shell syntax', () => {
    expect(() => buildCwdFileProofCommand('win32', "proof'; exit 1")).toThrow(
      /invalid cwd proof file name/u,
    );
  });
});

describe('condition waits', () => {
  test('uses the monotonic clock to enforce its timeout deadline', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const wallNow = vi.spyOn(Date, 'now').mockReturnValue(1_000);
    const stream = createFakeStream();
    let outcome = 'pending';
    const pending = waitForCondition(stream, () => false, 'evaluated command output', {
      intervalMs: 5,
      stallMs: 10,
    }).then(
      () => {
        outcome = 'resolved';
      },
      (error: unknown) => {
        outcome = error instanceof Error ? error.message : String(error);
      },
    );
    try {
      await vi.advanceTimersByTimeAsync(9);
      expect(outcome).toBe('pending');
      await vi.advanceTimersByTimeAsync(1);
      expect(outcome).toMatch(/timeout waiting for: evaluated command output/u);
    } finally {
      wallNow.mockReturnValue(1_010);
      await vi.advanceTimersByTimeAsync(5);
      await pending;
    }
  });

  test('surfaces a spawn failure instead of expiring as a timeout', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    const stallMs = 3_000;
    setTimeout(() => stream.fail('spawn-error: posix_spawnp failed'), 20);
    const startedAt = performance.now();
    const settled = waitForCondition(stream, () => false, 'evaluated command output', {
      intervalMs: 5,
      stallMs,
    }).then(
      () => ({ message: 'resolved', elapsedMs: performance.now() - startedAt }),
      (error: unknown) => ({
        message: error instanceof Error ? error.message : String(error),
        elapsedMs: performance.now() - startedAt,
      }),
    );
    await vi.advanceTimersByTimeAsync(stallMs);
    const { message, elapsedMs } = await settled;
    expect(message).toMatch(/posix_spawnp failed/u);
    expect(elapsedMs).toBeLessThan(stallMs);
  });

  test('surfaces an early exit instead of expiring as a timeout', async () => {
    const stream = createFakeStream();
    const timer = setTimeout(() => stream.fail('exited (code 1, signal none)'), 20);
    try {
      await expect(
        waitForCondition(stream, () => false, 'evaluated command output', {
          intervalMs: 5,
          stallMs: 3_000,
        }),
      ).rejects.toThrow(/exited \(code 1/u);
    } finally {
      clearTimeout(timer);
    }
  });

  test('reads an awaited exit as success rather than as a failure', async () => {
    const stream = createFakeStream();
    stream.fail('exited (code 1, signal none)');
    await expect(
      waitForCondition(stream, () => true, 'failure for unspawnable shell', {
        intervalMs: 5,
        stallMs: 200,
      }),
    ).resolves.toBeUndefined();
  });

  test('a poll interval that cannot pace the wait is named instead of looped on', async () => {
    const stream = createFakeStream();
    for (const intervalMs of [Number.NaN, 0, -1]) {
      await expect(
        waitForCondition(stream, () => false, 'evaluated command output', {
          intervalMs,
          stallMs: 60,
        }),
      ).rejects.toThrow(
        /the poll interval for evaluated command output must be a positive finite duration/u,
      );
      await expect(
        waitForShellReady(stream, 'evaluated command output', { intervalMs, stallMs: 60 }),
      ).rejects.toThrow(
        /the poll interval for evaluated command output must be a positive finite duration/u,
      );
    }
  });

  test('a stall window that cannot bound the wait is named instead of polled against', async () => {
    const stream = createFakeStream();
    for (const stallMs of [Number.NaN, 0, -1]) {
      await expect(
        waitForCondition(stream, () => false, 'evaluated command output', {
          stallMs,
          intervalMs: 5,
        }),
      ).rejects.toThrow(
        /the stall window for evaluated command output must be a positive finite duration/u,
      );
    }
  });

  test('names what did arrive when a condition times out', async () => {
    const stream = createFakeStream();
    stream.emit('PS C:\\project> Write-Output "HARNESS_$((6*7))_DONE"');
    await expect(
      waitForCondition(stream, () => false, 'evaluated command output', {
        intervalMs: 5,
        stallMs: 60,
      }),
    ).rejects.toThrow(/HARNESS_/u);
  });
});

function evaluateFakePowerShellCommand(command: string): string | null {
  const arithmetic = /^Write-Output "([^"]*)_\$\(\((\d+)\*(\d+)\)\)_([^"]*)"$/u.exec(command);
  if (arithmetic !== null) {
    return `${arithmetic[1]}_${Number(arithmetic[2]) * Number(arithmetic[3])}_${arithmetic[4]}`;
  }
  return /^Write-Output "([^"]*)"$/u.exec(command)?.[1] ?? null;
}

function createStartupRaceSpawn(readyAfterMs: number): SpawnPty {
  return (_file: string, _args: string[] | string, _options: PtySpawnOptions): PtyProcessLike => {
    let emit: (data: string) => void = () => undefined;
    let accepting = false;
    const timers = [
      setTimeout(() => emit('loading profile\r\n'), readyAfterMs / 2),
      setTimeout(() => {
        accepting = true;
        emit('PS C:\\project> ');
      }, readyAfterMs),
    ];
    queueMicrotask(() => emit('\u001b[2J\u001b[H'));
    return {
      pid: 4242,
      onData(listener) {
        emit = listener;
      },
      onExit() {},
      write(data) {
        if (!accepting) return;
        const typed = data.replace(/\r$/u, '');
        emit(`${typed}\r\n`);
        const output = evaluateFakePowerShellCommand(typed);
        if (output === null) return;
        timers.push(setTimeout(() => emit(`${output}\r\n`), 0));
      },
      resize() {},
      kill() {
        for (const timer of timers) clearTimeout(timer);
      },
      pause() {},
      resume() {},
    };
  };
}

describe('driving a real host through a shell that starts slowly', () => {
  test('the command lands because the drive waits for the read loop', async () => {
    const host = createPtyHostProbe({
      spawn: createStartupRaceSpawn(60),
      env: { PATH: '/usr/bin', SHELL: '/bin/sh' },
      platform: 'linux',
      shellExists: () => true,
    });
    const io = host.streamOf('io');
    try {
      host.send({ type: 'create', ptyId: 'io', cwd: '/tmp', cols: 80, rows: 24 });
      await waitForShellReady(io, 'interactive shell ready', FAST_READY);
      host.send({
        type: 'input',
        ptyId: 'io',
        data: 'Write-Output "HARNESS_$((6*7))_DONE"\r',
      });
      await waitForCondition(io, () => io.read().includes('HARNESS_42_DONE'), 'command output', {
        intervalMs: 5,
        stallMs: 2_000,
      });
      expect(io.read()).toContain('HARNESS_42_DONE');
    } finally {
      host.killActive();
    }
  });

  test('maps a real host spawn failure into the readiness failure channel', async () => {
    const host = createPtyHostProbe({
      spawn: () => {
        throw new Error('EMFILE: too many open files');
      },
      env: { PATH: '/usr/bin', SHELL: '/bin/sh' },
      platform: 'linux',
      shellExists: () => true,
    });
    const io = host.streamOf('io');
    try {
      host.send({ type: 'create', ptyId: 'io', cwd: '/tmp', cols: 80, rows: 24 });
      await expect(
        waitForCondition(io, () => false, 'shell ready', { intervalMs: 5, stallMs: 100 }),
      ).rejects.toThrow(/shell failed before shell ready: spawn-error: EMFILE/u);
    } finally {
      host.killActive();
    }
  });

  test('maps a real host exit into the readiness failure channel', async () => {
    const spawn: SpawnPty = () => ({
      pid: 4243,
      onData() {},
      onExit(listener) {
        queueMicrotask(() => listener({ exitCode: 3, signal: undefined }));
      },
      write() {},
      resize() {},
      kill() {},
      pause() {},
      resume() {},
    });
    const host = createPtyHostProbe({
      spawn,
      env: { PATH: '/usr/bin', SHELL: '/bin/sh' },
      platform: 'linux',
      shellExists: () => true,
    });
    const io = host.streamOf('io');
    try {
      host.send({ type: 'create', ptyId: 'io', cwd: '/tmp', cols: 80, rows: 24 });
      await expect(
        waitForCondition(io, () => false, 'shell ready', { intervalMs: 5, stallMs: 100 }),
      ).rejects.toThrow(/shell failed before shell ready: exited \(code 3, signal none\)/u);
    } finally {
      host.killActive();
    }
  });

  test('warnings the host raises reach a logger handed to the probe, so a run can say which ConPTY it used', () => {
    const warnings: Record<string, unknown>[] = [];
    const attempts: PtySpawnOptions[] = [];
    const raceSpawn = createStartupRaceSpawn(0);
    const host = createPtyHostProbe({
      spawn: (file, args, options) => {
        attempts.push(options);
        if (attempts.length === 1) throw new Error('Cannot find conpty.dll beside conpty.node');
        return raceSpawn(file, args, options);
      },
      env: { SystemRoot: 'C:\\Windows' },
      platform: 'win32',
      shellExists: () => true,
      logger: { warn: (entry) => warnings.push(entry) },
    });
    try {
      host.send({
        type: 'create',
        ptyId: 'io',
        cwd: 'C:\\project',
        cols: 80,
        rows: 24,
        shell: 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
      });
      expect(attempts.map((options) => options.useConptyDll)).toEqual([true, false]);
      expect(warnings).toContainEqual(
        expect.objectContaining({ event: 'pty-host-conpty-dll-fallback' }),
      );
    } finally {
      host.killActive();
    }
  });
});

const INPUT_READY_MARKER = 'OK_INPUT_READY_deadbeef_42_READY';
const INPUT_READY_PROBE = {
  input: 'Write-Output "OK_INPUT_READY_deadbeef_$((6*7))_READY"\r',
  marker: INPUT_READY_MARKER,
} as const;
const INPUT_READY_FAST = { roundTripStallMs: 200, intervalMs: 5 } as const;
const BOOTED_PROMPT = 'PS C:\\project> ';
const SLOW_EVALUATION_MS = 120;
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 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,
  options: { evaluatesAfterMs?: number; evaluates?: boolean } = {},
): { sent: string[]; send: (data: string) => void; dispose: () => void } {
  const sent: string[] = [];
  const timers: ReturnType<typeof setTimeout>[] = [];
  return {
    sent,
    dispose: () => {
      for (const timer of timers) clearTimeout(timer);
    },
    send: (data) => {
      sent.push(data);
      if (options.evaluates === false) return;
      const typed = data.replace(/\r$/u, '');
      const output = evaluateFakePowerShellCommand(typed);
      if (output === null) return;
      timers.push(
        setTimeout(() => stream.emit(`${typed}\r\n${output}\r\n`), options.evaluatesAfterMs ?? 0),
      );
    },
  };
}

describe('evaluated-input readiness', () => {
  test('returns the monotonic clock elapsed delta for a single evaluated probe', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    vi.spyOn(Date, 'now').mockReturnValue(1_000);
    const stream = createFakeStream();
    stream.emit(BOOTED_PROMPT);
    const shell = driveEvaluatingShell(stream, { evaluatesAfterMs: SLOW_EVALUATION_MS });
    try {
      const pending = waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
        roundTripStallMs: 5_000,
        intervalMs: 5,
        budgetMs: 5_000,
      });
      await vi.advanceTimersByTimeAsync(SLOW_EVALUATION_MS);
      expect((await pending).roundTripMs).toBe(SLOW_EVALUATION_MS);
      expect(shell.sent).toEqual([INPUT_READY_PROBE.input]);
    } finally {
      shell.dispose();
    }
  });

  test('a booted shell gone silent is held to its containment, not refused when its silence window passes', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(BOOTED_PROMPT);
    const shell = driveEvaluatingShell(stream, { evaluates: false });
    const budgetMs = READINESS_CEILING_MS * 4;
    let outcome: string = 'pending';
    const pending = waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
      budgetMs,
    }).then(
      () => {
        outcome = 'resolved';
      },
      (error: unknown) => {
        outcome = error instanceof Error ? error.message : String(error);
      },
    );
    await vi.advanceTimersByTimeAsync(budgetMs - 1_000);
    expect(outcome).toBe('pending');
    await vi.advanceTimersByTimeAsync(1_100);
    expect(outcome).toContain(ROUND_TRIP_CONTAINMENT_VERDICT);
    expect(outcome).toContain(`(received ${JSON.stringify(BOOTED_PROMPT)})`);
    await pending;
  });

  test('a shell that only echoes the probe never reports ready', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(BOOTED_PROMPT);
    const { verdict, settled } = watchEvaluatedInput(stream, (data) => stream.emit(data), {
      ...INPUT_READY_FAST,
      budgetMs: 5_000,
    });
    await vi.advanceTimersByTimeAsync(5_000 + INPUT_READY_FAST.intervalMs);
    await settled;
    expect(verdict.settled).toBe('failed');
    expect(verdict.message).toContain(ROUND_TRIP_CONTAINMENT_VERDICT);
    expect(stream.read()).toContain('Write-Output');
    expect(stream.read()).not.toContain(INPUT_READY_MARKER);
  });

  test('a shell that never evaluates is refused at its containment having written the probe once', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(BOOTED_PROMPT);
    const shell = driveEvaluatingShell(stream, { evaluates: false });
    const { verdict, settled } = watchEvaluatedInput(stream, shell.send, {
      ...INPUT_READY_FAST,
      budgetMs: 5_000,
    });
    await vi.advanceTimersByTimeAsync(5_000 + INPUT_READY_FAST.intervalMs);
    await settled;
    expect(verdict.settled).toBe('failed');
    expect(verdict.message).toContain(ROUND_TRIP_CONTAINMENT_VERDICT);
    expect(shell.sent).toEqual([INPUT_READY_PROBE.input]);
  });

  test('rejects a probe whose own echo would satisfy it', async () => {
    const stream = createFakeStream();
    const shell = driveEvaluatingShell(stream);
    await expect(
      waitForEvaluatedInput(
        stream,
        shell.send,
        { ...INPUT_READY_PROBE, input: `echo ${INPUT_READY_MARKER}` },
        'input ready',
        { ...INPUT_READY_FAST, budgetMs: 5_000 },
      ),
    ).rejects.toThrow(/must not contain its marker/u);
    expect(shell.sent).toEqual([]);
  });

  test('a dead shell short-circuits instead of waiting out the budget', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    const shell = driveEvaluatingShell(stream, { evaluates: false });
    stream.fail('exited (code 1, signal none)');
    const budgetMs = 5_000;
    const { verdict, settled } = watchEvaluatedInput(stream, shell.send, {
      roundTripStallMs: 5_000,
      intervalMs: 5,
      budgetMs,
    });
    await vi.advanceTimersByTimeAsync(budgetMs);
    await settled;
    expect(verdict.settled).toBe('failed');
    expect(verdict.message).toMatch(
      /shell died before producing output for input ready, probe unwritten/u,
    );
    expect(verdict.atMs).toBeLessThan(budgetMs);
    expect(shell.sent).toEqual([]);
  });
});

const ATTACH_PROLOGUE = '\u001b[?9001h\u001b[?1004h';
const BUNDLED_WINDOW_SHOWN = '\u001b[1t';
const BUNDLED_DA1_QUERY_AND_INPUT_MODES = '\u001b[c\u001b[?1004h\u001b[?9001h';
const BUNDLED_ATTACH_PROLOGUE = `${BUNDLED_WINDOW_SHOWN}${BUNDLED_DA1_QUERY_AND_INPUT_MODES}`;
const BOOT_PAST_ROUND_TRIP_MS = READINESS_CEILING_MS + 500;
const CONPTY_REPAINT = '\u001b[?25l\u001b[2J\u001b[m\u001b[H';
const CONPTY_SHOW_CURSOR = '\u001b[?25h';
const WINDOWS_POWERSHELL_TITLE_BEL =
  '\u001b]0;Administrator: C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\u0007';
const WINDOWS_POWERSHELL_TITLE_ST =
  '\u001b]0;Administrator: C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\u001b\\';
const CONPTY_INIT_FRAME = `${CONPTY_REPAINT}${WINDOWS_POWERSHELL_TITLE_BEL}${CONPTY_SHOW_CURSOR}`;
const CONPTY_INIT_FRAME_ST_TITLE = `${CONPTY_REPAINT}${WINDOWS_POWERSHELL_TITLE_ST}${CONPTY_SHOW_CURSOR}`;
const CONPTY_INIT_FRAME_CUT_MID_TITLE = `${CONPTY_REPAINT}\u001b]0;C:\\Prog`;
const WINDOWS_POWERSHELL_GUID_ECHO = '7768de99-595e-40d4-9a3d-a4760b05683b\r\n';
const WINDOWS_POWERSHELL_WRAP_TOGGLE = '\u001b[?7l\u001b[?7h';
const PWSH_BANNER = 'PowerShell 7.6.5\r\n';
const PWSH_TITLE = '\u001b]0;C:\\Program Files\\PowerShell\\7\\pwsh.exe\u0007';
const PWSH_ADMIN_TITLE = '\u001b]0;Administrator: C:\\Program Files\\PowerShell\\7\\pwsh.exe\u0007';
const CI_CAPTURE_CONTENT_FREE_FRAME = `${CONPTY_INIT_FRAME}${WINDOWS_POWERSHELL_GUID_ECHO}`;
const CI_CAPTURE_BANNER_INSIDE_FRAME = `${CONPTY_REPAINT}${PWSH_BANNER}${PWSH_TITLE}${CONPTY_SHOW_CURSOR}`;
const CI_CAPTURE_BANNER_THEN_RETITLE = `${CI_CAPTURE_BANNER_INSIDE_FRAME}${PWSH_ADMIN_TITLE}`;
const BUNDLED_CI_CAPTURE_LAUNCH_THEN_RETITLE =
  '\u001b[1t\u001b[c\u001b[?1004h\u001b[?9001h\u001b]0;Administrator: C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\u001b\\\u001b[?7l\u001b[?7hf8bc0b8a-d339-406f-b6d1-f0b1185054f6\r\n\u001b]0;Administrator: C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\u001b\\';
const BUNDLED_CI_CAPTURE_LAUNCH_ONLY =
  '\u001b[1t\u001b[c\u001b[?1004h\u001b[?9001h\u001b]0;Administrator: C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\u001b\\\u001b[?7l\u001b[?7hd603e31a-bc79-47cd-b36f-7967714a865f\r\n';
const BUNDLED_CONPTY_RECORDED_WITH_NODE_PTY = '1.2.0-beta.15';

describe('shell startup is a liveness wait, not a round-trip budget', () => {
  test('a shell whose first output arrives after the round-trip ceiling still reports ready', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE);
    const shell = driveEvaluatingShell(stream);
    const boot = setTimeout(() => stream.emit(BOOTED_PROMPT), BOOT_PAST_ROUND_TRIP_MS);
    let outcome: string = 'pending';
    const pending = waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
      budgetMs: BOOT_PAST_ROUND_TRIP_MS * 4,
    }).then(
      (result): EvaluatedInputTiming | null => {
        outcome = 'resolved';
        return result;
      },
      (error: unknown): EvaluatedInputTiming | null => {
        outcome = error instanceof Error ? error.message : String(error);
        return null;
      },
    );
    try {
      await vi.advanceTimersByTimeAsync(READINESS_CEILING_MS);
      expect(outcome).toBe('pending');
      await vi.advanceTimersByTimeAsync(BOOT_PAST_ROUND_TRIP_MS - READINESS_CEILING_MS + 100);
      expect(outcome).toBe('resolved');
      const timing = await pending;
      if (timing === null) throw new Error('the wait resolved without reporting any timing');
      expect(timing.firstOutputMs).toBeGreaterThanOrEqual(BOOT_PAST_ROUND_TRIP_MS);
      expect(timing.firstOutput).toContain('PS C:');
      expect(shell.sent).toEqual([INPUT_READY_PROBE.input]);
    } finally {
      clearTimeout(boot);
      shell.dispose();
      await pending;
    }
  });

  test('a shell that never writes is named as never having produced output', async () => {
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE);
    const shell = driveEvaluatingShell(stream);
    try {
      const failure = await waitForEvaluatedInput(
        stream,
        shell.send,
        INPUT_READY_PROBE,
        'input ready',
        { budgetMs: 150, intervalMs: 5 },
      ).catch((error: unknown) => error);
      expect(failure).toBeInstanceOf(Error);
      expect(failure).not.toBeInstanceOf(HarnessBudgetRefusal);
      expect((failure as Error).message).toMatch(/shell never produced output before input ready/u);
      expect(shell.sent).toEqual([]);
    } finally {
      shell.dispose();
    }
  });

  test('a shell that boots but never answers is named at the round trip, not at startup', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`);
    const shell = driveEvaluatingShell(stream, { evaluates: false });
    try {
      const { verdict, settled } = watchEvaluatedInput(stream, shell.send, {
        budgetMs: 5_000,
        roundTripStallMs: 200,
        intervalMs: 5,
      });
      await vi.advanceTimersByTimeAsync(5_000 + 5);
      await settled;
      expect(verdict.settled).toBe('failed');
      expect(verdict.message).toContain(ROUND_TRIP_CONTAINMENT_VERDICT);
      expect(verdict.message).not.toContain('shell never produced output');
      expect(shell.sent).toEqual([INPUT_READY_PROBE.input]);
    } finally {
      shell.dispose();
    }
  });

  test('the attach handshake alone is not the shell speaking, and one byte past it is', () => {
    expect(shellOutputBeyondAttach(ATTACH_PROLOGUE)).toBe('');
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`)).toBe(BOOTED_PROMPT);
    expect(shellOutputBeyondAttach(ATTACH_PROLOGUE.slice(0, -2))).toBe('');
    expect(shellOutputBeyondAttach('')).toBe('');
  });

  test('the order the bundled ConPTY ships is not the shell speaking either', () => {
    expect(shellOutputBeyondAttach(BUNDLED_ATTACH_PROLOGUE)).toBe('');
    expect(shellOutputBeyondAttach(`${BUNDLED_ATTACH_PROLOGUE}${BOOTED_PROMPT}`)).toBe(
      BOOTED_PROMPT,
    );
    expect(shellOutputBeyondAttach(BUNDLED_ATTACH_PROLOGUE.slice(0, -2))).toBe('');
  });

  test("the host's screen-init frame on attach is not the shell speaking", () => {
    expect(shellOutputBeyondAttach(CONPTY_INIT_FRAME)).toBe('');
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME}`)).toBe('');
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME_ST_TITLE}`)).toBe('');
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME_CUT_MID_TITLE}`)).toBe(
      '',
    );
  });

  test('a Windows capture reduces to the bytes the shell itself wrote', () => {
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CI_CAPTURE_CONTENT_FREE_FRAME}`)).toBe(
      WINDOWS_POWERSHELL_GUID_ECHO,
    );
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CI_CAPTURE_BANNER_INSIDE_FRAME}`)).toBe(
      `${PWSH_BANNER}${PWSH_TITLE}${CONPTY_SHOW_CURSOR}`,
    );
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CI_CAPTURE_BANNER_THEN_RETITLE}`)).toBe(
      `${PWSH_BANNER}${PWSH_TITLE}${CONPTY_SHOW_CURSOR}${PWSH_ADMIN_TITLE}`,
    );
    expect(
      shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME_ST_TITLE}${BOOTED_PROMPT}`),
    ).toBe(BOOTED_PROMPT);
  });

  test('shell output behind the init frame is never stripped with it', () => {
    expect(
      shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME}${BOOTED_PROMPT}`),
    ).toContain(BOOTED_PROMPT);
    expect(shellOutputBeyondAttach(CI_CAPTURE_BANNER_INSIDE_FRAME)).toContain('PowerShell 7.6.5');
    expect(shellOutputBeyondAttach(CI_CAPTURE_CONTENT_FREE_FRAME)).toContain(
      WINDOWS_POWERSHELL_GUID_ECHO,
    );
    expect(
      shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME_ST_TITLE}${BOOTED_PROMPT}`),
    ).toContain(BOOTED_PROMPT);
  });

  test('the first output the gate reports leads with the bytes that opened it', async () => {
    const stream = createFakeStream();
    const head = 'OK_GATE_OPENED_HERE';
    stream.emit(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME}${head}${'x'.repeat(600)}${BOOTED_PROMPT}`);
    const shell = driveEvaluatingShell(stream);
    try {
      const timing = await waitForEvaluatedInput(
        stream,
        shell.send,
        INPUT_READY_PROBE,
        'input ready',
        { budgetMs: 5_000, roundTripStallMs: 2_000, intervalMs: 5 },
      );
      expect(timing.firstOutput).toContain(head);
      expect(timing.firstOutput.startsWith('...')).toBe(false);
    } finally {
      shell.dispose();
    }
  });

  test('the shipped attach burst leaves the gate shut, and a prompt behind it opens it', async () => {
    const attaching = createFakeStream();
    attaching.emit(BUNDLED_ATTACH_PROLOGUE);
    const attachingShell = driveEvaluatingShell(attaching);
    const booted = createFakeStream();
    booted.emit(`${BUNDLED_ATTACH_PROLOGUE}${BOOTED_PROMPT}`);
    const bootedShell = driveEvaluatingShell(booted);
    try {
      await expect(
        waitForEvaluatedInput(attaching, attachingShell.send, INPUT_READY_PROBE, 'input ready', {
          budgetMs: 150,
          intervalMs: 5,
        }),
      ).rejects.toThrow(/shell never produced output before input ready/u);
      expect(attachingShell.sent).toEqual([]);

      const timing = await waitForEvaluatedInput(
        booted,
        bootedShell.send,
        INPUT_READY_PROBE,
        'input ready',
        { budgetMs: 5_000, roundTripStallMs: 2_000, intervalMs: 5 },
      );
      expect(timing.firstOutput).toContain('PS C:');
      expect(bootedShell.sent).toEqual([INPUT_READY_PROBE.input]);
    } finally {
      attachingShell.dispose();
      bootedShell.dispose();
    }
  });

  test('a handshake cut by a read boundary does not open the gate', async () => {
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE.slice(0, -2));
    const shell = driveEvaluatingShell(stream);
    try {
      await expect(
        waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
          budgetMs: 150,
          intervalMs: 5,
        }),
      ).rejects.toThrow(/shell never produced output before input ready/u);
      expect(shell.sent).toEqual([]);
    } finally {
      shell.dispose();
    }
  });

  test('a budget that cannot bound the wait is named instead of polled against', async () => {
    const stream = createFakeStream();
    const shell = driveEvaluatingShell(stream);
    try {
      await expect(
        waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
          intervalMs: 5,
        } as unknown as EvaluatedInputOptions),
      ).rejects.toThrow(/budget for input ready must be a positive finite duration/u);
      for (const budgetMs of [Number.NaN, 0, -1]) {
        await expect(
          waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
            budgetMs,
            intervalMs: 5,
          }),
        ).rejects.toThrow(/budget for input ready must be a positive finite duration/u);
      }
      await expect(
        waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
          budgetMs: 5_000,
          roundTripStallMs: Number.NaN,
          intervalMs: 5,
        }),
      ).rejects.toThrow(
        /the round-trip stall window for input ready must be a positive finite duration/u,
      );
      expect(shell.sent).toEqual([]);
    } finally {
      shell.dispose();
    }
  });

  test('a late-booting shell leaves the round trip only what the budget has left', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE);
    const shell = driveEvaluatingShell(stream, { evaluates: false });
    const boot = setTimeout(() => stream.emit(BOOTED_PROMPT), BOOT_PAST_ROUND_TRIP_MS);
    let outcome: string = 'pending';
    const pending = waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
      budgetMs: BOOT_PAST_ROUND_TRIP_MS + 1_000,
    }).then(
      () => {
        outcome = 'resolved';
      },
      (error: unknown) => {
        outcome = error instanceof Error ? error.message : String(error);
      },
    );
    try {
      await vi.advanceTimersByTimeAsync(BOOT_PAST_ROUND_TRIP_MS);
      expect(outcome).toBe('pending');
      await vi.advanceTimersByTimeAsync(1_100);
      expect(outcome).toMatch(/^timeout waiting for: input ready after 1000ms/u);
      await pending;
    } finally {
      clearTimeout(boot);
      shell.dispose();
    }
  });

  test('startup that outruns its grant is refused as a spent budget, with the probe unsent', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE);
    const shell = driveEvaluatingShell(stream);
    const boot = setTimeout(() => stream.emit(BOOTED_PROMPT), 25);
    let outcome: unknown = 'pending';
    const pending = waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
      budgetMs: 20,
      intervalMs: 15,
    }).then(
      () => {
        outcome = 'resolved';
      },
      (error: unknown) => {
        outcome = error;
      },
    );
    try {
      await vi.advanceTimersByTimeAsync(40);
      expect(outcome).toBeInstanceOf(HarnessBudgetRefusal);
      expect((outcome as Error).message).toMatch(
        /^the 20ms grant for input ready was spent before the round trip could start/u,
      );
      expect(shell.sent).toEqual([]);
      await pending;
    } finally {
      clearTimeout(boot);
      shell.dispose();
    }
  });

  test('the startup timeout tells a silent PTY apart from one that only handshook', async () => {
    const silent = createFakeStream();
    const handshaken = createFakeStream();
    handshaken.emit(ATTACH_PROLOGUE);
    const budget = { budgetMs: 60, intervalMs: 5 } as const;
    const cases = [
      { stream: silent, tail: 'received nothing' },
      { stream: handshaken, tail: `received ${JSON.stringify(ATTACH_PROLOGUE)}` },
    ];
    for (const { stream, tail } of cases) {
      const failure = await waitForEvaluatedInput(
        stream,
        () => undefined,
        INPUT_READY_PROBE,
        'input ready',
        budget,
      ).then(
        () => null,
        (error: unknown) => (error as Error).message,
      );
      expect(failure).toContain('shell never produced output before input ready');
      expect(failure).toContain(tail);
    }
  });

  test('a shell that dies during startup is surfaced without writing the probe into it', async () => {
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE);
    const shell = driveEvaluatingShell(stream);
    stream.fail('exited (code -1, signal none)');
    try {
      await expect(
        waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, 'input ready', {
          budgetMs: 5_000,
          intervalMs: 5,
        }),
      ).rejects.toThrow(
        /shell died before producing output for input ready, probe unwritten: exited \(code -1/u,
      );
      expect(shell.sent).toEqual([]);
    } finally {
      shell.dispose();
    }
  });
});

const RECORDED_ATTACH_PROLOGUES = [
  { host: 'the inbox ConPTY', prologue: ATTACH_PROLOGUE },
  { host: 'the bundled ConPTY', prologue: BUNDLED_ATTACH_PROLOGUE },
] as const;
const BUNDLED_CI_CAPTURE_LAUNCHES = [
  [
    'the shell retitles after the launch output',
    BUNDLED_CI_CAPTURE_LAUNCH_THEN_RETITLE,
    `${WINDOWS_POWERSHELL_TITLE_ST}${WINDOWS_POWERSHELL_WRAP_TOGGLE}f8bc0b8a-d339-406f-b6d1-f0b1185054f6\r\n${WINDOWS_POWERSHELL_TITLE_ST}`,
  ],
  [
    'the launch output is the last thing written',
    BUNDLED_CI_CAPTURE_LAUNCH_ONLY,
    `${WINDOWS_POWERSHELL_TITLE_ST}${WINDOWS_POWERSHELL_WRAP_TOGGLE}d603e31a-bc79-47cd-b36f-7967714a865f\r\n`,
  ],
] as const;
const LEADING_TITLES_BY_HOST = [
  [
    'the bundled ConPTY, which paints no title of its own, so the title is the one the shell set',
    `${BUNDLED_ATTACH_PROLOGUE}${WINDOWS_POWERSHELL_TITLE_ST}`,
    WINDOWS_POWERSHELL_TITLE_ST,
  ],
  [
    'the inbox ConPTY, which paints the title inside its first-paint frame',
    `${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME_ST_TITLE}`,
    '',
  ],
] as const;

describe('what the bundled ConPTY writes before the shell speaks is the host speaking, wherever a read cuts it', () => {
  test.each(RECORDED_ATTACH_PROLOGUES)(
    'no read of what $host writes on attach opens the gate, and a prompt behind it does',
    ({ prologue }) => {
      const reads = Array.from({ length: prologue.length + 1 }, (_, end) => prologue.slice(0, end));
      expect(reads.filter((read) => shellOutputBeyondAttach(read) !== '')).toEqual([]);
      expect(shellOutputBeyondAttach(`${prologue}${BOOTED_PROMPT}`)).toBe(BOOTED_PROMPT);
    },
  );

  test.each(BUNDLED_CI_CAPTURE_LAUNCHES)(
    'a launch captured behind the bundled ConPTY drops only the prologue, and keeps everything from the title the shell set on launch, when %s',
    (_launch, capture, fromShellTitle) => {
      const kept = shellOutputBeyondAttach(capture);
      const consumed = capture.slice(0, capture.length - kept.length);
      expect(consumed).toBe(BUNDLED_ATTACH_PROLOGUE);
      expect(kept).toBe(fromShellTitle);
    },
  );

  test.each(LEADING_TITLES_BY_HOST)(
    'a title right behind the attach bytes is shell output only when the host paints no title itself: %s',
    (_host, stream, shellOutput) => {
      expect(shellOutputBeyondAttach(stream)).toBe(shellOutput);
    },
  );

  test('the bundled ConPTY bytes these tests replay were recorded with the node-pty version the desktop package pins and loads', () => {
    const desktopPackage: { optionalDependencies?: Record<string, string> } = JSON.parse(
      readFileSync(new URL('../../package.json', import.meta.url), 'utf8'),
    );
    const loadedNodePtyRoot = dirname(dirname(createRequire(import.meta.url).resolve('node-pty')));
    const loadedNodePty: { version?: string } = JSON.parse(
      readFileSync(join(loadedNodePtyRoot, 'package.json'), 'utf8'),
    );
    const recapture = `BUNDLED_CONPTY_ATTACH_SEQUENCES in the readiness helper and the bundled captures in this file were recorded from node-pty ${BUNDLED_CONPTY_RECORDED_WITH_NODE_PTY} with its bundled conpty 1.25.260303002. A passing Windows real-PTY run does not print the bytes the classifier strips: compare each INPUT_READY firstOutput head on this change's Windows run with a run before it, check any new leading sequence against the OpenConsole source of the new conpty build, re-record the captures from a Windows run that prints the raw stream, then set BUNDLED_CONPTY_RECORDED_WITH_NODE_PTY to the new version`;
    expect(desktopPackage.optionalDependencies?.['node-pty'], recapture).toBe(
      BUNDLED_CONPTY_RECORDED_WITH_NODE_PTY,
    );
    expect(loadedNodePty.version, recapture).toBe(BUNDLED_CONPTY_RECORDED_WITH_NODE_PTY);
  });
});

const LIVENESS_STALL_MS = 400;
const LIVENESS_POLL_MS = LIVENESS_STALL_MS / 20;
const LIVENESS_GRANT_MS = LIVENESS_STALL_MS * 4;
const SLOW_SHELL_BOOT_MS = LIVENESS_STALL_MS * 2;
const LATER_ATTACH_READ_MS = LIVENESS_STALL_MS / 2;
const LIVENESS_WAIT = {
  budgetMs: LIVENESS_GRANT_MS,
  roundTripStallMs: LIVENESS_STALL_MS,
  intervalMs: LIVENESS_POLL_MS,
} as const;
const RECORDED_ATTACH_ARRIVALS = [
  { host: 'the inbox ConPTY', reads: [ATTACH_PROLOGUE] },
  { host: 'the bundled ConPTY', reads: [BUNDLED_ATTACH_PROLOGUE] },
  {
    host: 'the bundled ConPTY across two reads',
    reads: [BUNDLED_WINDOW_SHOWN, BUNDLED_DA1_QUERY_AND_INPUT_MODES],
  },
] as const;

function arriveAttachReads(stream: FakeStream, reads: readonly string[]): () => void {
  const [first = '', ...later] = reads;
  stream.emit(first);
  return scheduleEmissions(
    stream,
    later.map((chunk, index) => ({ atMs: LATER_ATTACH_READ_MS * (index + 1), chunk })),
  );
}

function driveShellBootingAt(
  stream: FakeStream,
  bootAtMs: number,
): {
  sends: Array<{ data: string; atMs: number }>;
  send: (data: string) => void;
  dispose: () => void;
} {
  const startedAt = performance.now();
  const sends: Array<{ data: string; atMs: number }> = [];
  const typeahead: string[] = [];
  let booted = false;
  const evaluate = (data: string): void => {
    const typed = data.replace(/\r$/u, '');
    const output = evaluateFakePowerShellCommand(typed);
    if (output !== null) stream.emit(`${typed}\r\n${output}\r\n`);
  };
  const boot = setTimeout(() => {
    booted = true;
    stream.emit(BOOTED_PROMPT);
    for (const data of typeahead.splice(0)) evaluate(data);
  }, bootAtMs);
  return {
    sends,
    send: (data) => {
      sends.push({ data, atMs: performance.now() - startedAt });
      if (booted) evaluate(data);
      else typeahead.push(data);
    },
    dispose: () => clearTimeout(boot),
  };
}

describe('a shell behind the bundled ConPTY is held to its startup grant, as one behind the inbox ConPTY is', () => {
  test.each(RECORDED_ATTACH_ARRIVALS)(
    'a shell that never writes behind $host is refused at its grant as never having produced output, with the probe unwritten',
    async ({ reads }) => {
      vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
      const stream = createFakeStream();
      const stopReads = arriveAttachReads(stream, reads);
      const shell = driveEvaluatingShell(stream, { evaluates: false });
      const { verdict, settled } = watchEvaluatedInput(stream, shell.send, LIVENESS_WAIT);
      try {
        await vi.advanceTimersByTimeAsync(LIVENESS_GRANT_MS + LIVENESS_POLL_MS);
        await settled;
        expect(verdict.message).toBe(
          `shell never produced output before input ready within ${LIVENESS_GRANT_MS}ms (received ${JSON.stringify(reads.join(''))})`,
        );
        expect(verdict.atMs).toBeGreaterThanOrEqual(LIVENESS_GRANT_MS);
        expect(shell.sent).toEqual([]);
      } finally {
        stopReads();
        shell.dispose();
      }
    },
  );

  test.each(RECORDED_ATTACH_ARRIVALS)(
    'a shell that first writes after the round-trip stall window but inside its grant is admitted behind $host, and the probe waits for it',
    async ({ reads }) => {
      vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
      const stream = createFakeStream();
      const stopReads = arriveAttachReads(stream, reads);
      const shell = driveShellBootingAt(stream, SLOW_SHELL_BOOT_MS);
      const { verdict, settled } = watchEvaluatedInput(stream, shell.send, LIVENESS_WAIT);
      try {
        await vi.advanceTimersByTimeAsync(LIVENESS_GRANT_MS + LIVENESS_POLL_MS);
        await settled;
        expect(verdict.message).toBe('');
        expect(verdict.settled).toBe('ready');
        expect(verdict.timing?.firstOutput).toBe(JSON.stringify(BOOTED_PROMPT));
        expect(verdict.timing?.firstOutputMs).toBeGreaterThanOrEqual(SLOW_SHELL_BOOT_MS);
        expect(shell.sends.map((sent) => sent.data)).toEqual([INPUT_READY_PROBE.input]);
        expect(shell.sends[0]?.atMs).toBeGreaterThanOrEqual(SLOW_SHELL_BOOT_MS);
      } finally {
        stopReads();
        shell.dispose();
      }
    },
  );
});

const FIRST_OUTPUT_AT_MS = READINESS_CEILING_MS / 40;
const MARKER_PAST_CEILING_AT_MS = READINESS_CEILING_MS + READINESS_CEILING_MS / 8;
const ADVANCEMENT_BUDGET_MS = READINESS_CEILING_MS * 2;
const ADVANCEMENT_POLL_MS = 100;
const IDENTICAL_TAIL_CHARS = 1_024;
const CI_BOOT_CAPTURE = `${CI_CAPTURE_BANNER_THEN_RETITLE}${BOOTED_PROMPT}`;
const REPEATED_BOOT_BLOCK = CI_BOOT_CAPTURE.repeat(
  Math.ceil(IDENTICAL_TAIL_CHARS / CI_BOOT_CAPTURE.length),
);

interface OracleVerdict {
  settled: 'pending' | 'ready' | 'failed';
  atMs: number;
  message: string;
  tail: string;
  timing: EvaluatedInputTiming | null;
}

function scheduleEmissions(
  stream: FakeStream,
  steps: ReadonlyArray<{ atMs: number; chunk: string }>,
): () => void {
  const timers = steps.map((step) => setTimeout(() => stream.emit(step.chunk), step.atMs));
  return () => {
    for (const timer of timers) clearTimeout(timer);
  };
}

function watchEvaluatedInput(
  stream: FakeStream,
  send: (data: string) => void,
  options: EvaluatedInputOptions,
): { verdict: OracleVerdict; settled: Promise<void> } {
  const startedAt = performance.now();
  const verdict: OracleVerdict = {
    settled: 'pending',
    atMs: Number.NaN,
    message: '',
    tail: '',
    timing: null,
  };
  const record = (): void => {
    verdict.atMs = performance.now() - startedAt;
    verdict.tail = stream.read().slice(-IDENTICAL_TAIL_CHARS);
  };
  const settled = waitForEvaluatedInput(
    stream,
    send,
    INPUT_READY_PROBE,
    'input ready',
    options,
  ).then(
    (timing) => {
      verdict.settled = 'ready';
      verdict.timing = timing;
      record();
    },
    (error: unknown) => {
      verdict.settled = 'failed';
      verdict.message = error instanceof Error ? error.message : String(error);
      record();
    },
  );
  return { verdict, settled };
}

async function raceWedgedAgainstAdvancing(): Promise<{
  wedged: OracleVerdict;
  advancing: OracleVerdict;
}> {
  vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
  const wedgedStream = createFakeStream();
  const advancingStream = createFakeStream();
  wedgedStream.emit(ATTACH_PROLOGUE);
  advancingStream.emit(ATTACH_PROLOGUE);
  const wedgedShell = driveEvaluatingShell(wedgedStream, { evaluates: false });
  const advancingShell = driveEvaluatingShell(advancingStream, { evaluates: false });
  const boot = [{ atMs: FIRST_OUTPUT_AT_MS, chunk: REPEATED_BOOT_BLOCK }];
  const advances = [...boot];
  for (
    let atMs = FIRST_OUTPUT_AT_MS + ADVANCEMENT_POLL_MS;
    atMs <= ADVANCEMENT_BUDGET_MS;
    atMs += ADVANCEMENT_POLL_MS
  ) {
    advances.push({ atMs, chunk: REPEATED_BOOT_BLOCK });
  }
  const stopWedged = scheduleEmissions(wedgedStream, boot);
  const stopAdvancing = scheduleEmissions(advancingStream, advances);
  const waitOptions = {
    budgetMs: ADVANCEMENT_BUDGET_MS,
    intervalMs: ADVANCEMENT_POLL_MS,
  } as const;
  const wedged = watchEvaluatedInput(wedgedStream, wedgedShell.send, waitOptions);
  const advancing = watchEvaluatedInput(advancingStream, advancingShell.send, waitOptions);
  try {
    await vi.advanceTimersByTimeAsync(ADVANCEMENT_BUDGET_MS + ADVANCEMENT_POLL_MS);
    return { wedged: wedged.verdict, advancing: advancing.verdict };
  } finally {
    stopWedged();
    stopAdvancing();
    wedgedShell.dispose();
    advancingShell.dispose();
    await Promise.all([wedged.settled, advancing.settled]);
  }
}

describe('readiness is refused when its containment is spent, not for elapsed time, whether the shell is still advancing or has gone quiet', () => {
  test('a shell still advancing when the ceiling passes is ready once its marker lands inside the budget', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE);
    const shell = driveEvaluatingShell(stream, { evaluates: false });
    const stopEmitting = scheduleEmissions(stream, [
      { atMs: FIRST_OUTPUT_AT_MS, chunk: CI_CAPTURE_BANNER_INSIDE_FRAME },
      { atMs: READINESS_CEILING_MS / 4, chunk: PWSH_ADMIN_TITLE },
      { atMs: READINESS_CEILING_MS / 2, chunk: BOOTED_PROMPT },
      { atMs: (READINESS_CEILING_MS * 7) / 8, chunk: `${INPUT_READY_PROBE.input}\n` },
      { atMs: MARKER_PAST_CEILING_AT_MS, chunk: `${INPUT_READY_MARKER}\r\n` },
    ]);
    const { verdict, settled } = watchEvaluatedInput(stream, shell.send, {
      budgetMs: ADVANCEMENT_BUDGET_MS,
      intervalMs: ADVANCEMENT_POLL_MS,
    });
    try {
      await vi.advanceTimersByTimeAsync(MARKER_PAST_CEILING_AT_MS + ADVANCEMENT_POLL_MS);
      expect({ settled: verdict.settled, failure: verdict.message }).toEqual({
        settled: 'ready',
        failure: '',
      });
      expect(verdict.atMs).toBeLessThan(ADVANCEMENT_BUDGET_MS);
      const timing = verdict.timing;
      if (timing === null) throw new Error('the wait reported ready without reporting any timing');
      expect(timing.roundTripMs).toBeGreaterThan(READINESS_CEILING_MS);
      expect(shell.sent).toEqual([INPUT_READY_PROBE.input]);
    } finally {
      stopEmitting();
      shell.dispose();
      await settled;
    }
  });

  test('a wedged live shell is held to its containment as one still advancing is, and both are refused there', async () => {
    const { wedged, advancing } = await raceWedgedAgainstAdvancing();
    expect(wedged.settled).toBe('failed');
    expect(advancing.settled).toBe('failed');
    expect(wedged.atMs).toBeGreaterThanOrEqual(ADVANCEMENT_BUDGET_MS);
    expect(advancing.atMs).toBeGreaterThanOrEqual(ADVANCEMENT_BUDGET_MS);
    expect(wedged.message).toContain(ROUND_TRIP_CONTAINMENT_VERDICT);
  });

  test('a wedged shell and an advancing one showing the reader the same bytes are not refused alike', async () => {
    const { wedged, advancing } = await raceWedgedAgainstAdvancing();
    expect(wedged.tail).toBe(advancing.tail);
    expect(wedged.message).not.toBe(advancing.message);
  });
});

const SCENARIO_BUDGET_MS = 1_000;
const SCENARIO_RESERVE_MS = 100;

function grantOutcome(
  budget: ReturnType<typeof createHarnessBudget>,
  before: string,
): 'granted' | 'refused' {
  try {
    budget.grantMs(before);
    return 'granted';
  } catch {
    return 'refused';
  }
}

function thrownFrom(call: () => unknown): Error {
  try {
    call();
  } catch (error) {
    return error instanceof Error ? error : new Error(String(error));
  }
  throw new Error('the call under test was expected to throw and did not');
}

function verdictShapeOf(error: Error): string {
  return `${error.constructor.name}|${Object.keys(error).sort().join(',')}`;
}

describe('the harness budget bounds each scenario, not only the run', () => {
  test('no single scenario is granted everything the budget has left', () => {
    const budget = createHarnessBudget(SCENARIO_BUDGET_MS, SCENARIO_RESERVE_MS, () => 0);
    expect(budget.grantMs('the first scenario started')).toBeLessThan(
      SCENARIO_BUDGET_MS - SCENARIO_RESERVE_MS,
    );
  });

  test('a scenario that spends its whole grant leaves the scenarios behind it able to run', () => {
    let nowMs = 0;
    const budget = createHarnessBudget(SCENARIO_BUDGET_MS, SCENARIO_RESERVE_MS, () => nowMs);
    nowMs += budget.grantMs('the first scenario started');
    expect(grantOutcome(budget, 'the second scenario started')).toBe('granted');
  });

  test('a remainder one poll can act on is granted, and anything under one poll is a spent grant', () => {
    const deadlineAt = SCENARIO_BUDGET_MS;
    const pollMs = 20;
    expect(
      remainingGrantMs(deadlineAt, 'a wait inside the scenario', {
        minimumMs: pollMs,
        now: () => deadlineAt - pollMs,
      }),
    ).toBe(pollMs);
    for (const remaining of [pollMs - 1, 1, 0, -1, Number.NaN]) {
      const spent = thrownFrom(() =>
        remainingGrantMs(deadlineAt, 'a wait inside the scenario', {
          minimumMs: pollMs,
          now: () => deadlineAt - remaining,
        }),
      );
      expect(spent).toBeInstanceOf(HarnessBudgetRefusal);
      expect(spent.message).toContain('a wait inside the scenario');
      expect(spent.message).not.toContain('must be a positive finite duration');
    }
  });

  test('refusing to start a scenario is not the verdict a scenario that ran and failed carries', () => {
    let nowMs = 0;
    const budget = createHarnessBudget(SCENARIO_BUDGET_MS, SCENARIO_RESERVE_MS, () => nowMs);
    nowMs = SCENARIO_BUDGET_MS;
    const refusal = thrownFrom(() => budget.grantMs('the second scenario started'));
    const ranAndFailed = new Error(refusal.message);
    expect(verdictShapeOf(refusal)).not.toBe(verdictShapeOf(ranAndFailed));
  });
});

const CONTAINED_WAIT_WINDOW_MS = READINESS_CEILING_MS / 8;
const CONTAINED_WAIT_CONTAINMENT_MS = READINESS_CEILING_MS;
const CONTAINED_WAIT_STEP_MS = CONTAINED_WAIT_WINDOW_MS / 4;
const HOST_WRITTEN_STEP = CONPTY_REPAINT;
const SHELL_WRITTEN_STEP = BOOTED_PROMPT;
const ENCODED_COMMAND_LABEL = 'PowerShell EncodedCommand output';
const CONTAINMENT_VERDICT = `${ENCODED_COMMAND_LABEL} was not reached inside its`;
const SPENT_GRANT_REFUSAL = `the grant for ${ENCODED_COMMAND_LABEL} was spent before the wait could poll once`;

interface ContainedWaitOptions extends WaitOptions {
  backstopAt: number;
}

interface ContainedWaitVerdict {
  settled: 'pending' | 'reached' | 'refused';
  atMs: number;
  message: string;
  buffer: string;
}

function evenCadence(
  chunk: string,
  stepMs: number,
  untilMs: number,
): Array<{ atMs: number; chunk: string }> {
  const steps: Array<{ atMs: number; chunk: string }> = [];
  for (let atMs = stepMs; atMs <= untilMs; atMs += stepMs) steps.push({ atMs, chunk });
  return steps;
}

function watchContainedWait(
  stream: FakeStream,
  options: ContainedWaitOptions,
  reached: () => boolean = () => false,
): { verdict: ContainedWaitVerdict; settled: Promise<void> } {
  const startedAt = performance.now();
  const verdict: ContainedWaitVerdict = {
    settled: 'pending',
    atMs: Number.NaN,
    message: '',
    buffer: '',
  };
  const record = (): void => {
    verdict.atMs = performance.now() - startedAt;
    verdict.buffer = stream.read();
  };
  const settled = waitForCondition(stream, reached, ENCODED_COMMAND_LABEL, options).then(
    () => {
      verdict.settled = 'reached';
      record();
    },
    (error: unknown) => {
      verdict.settled = 'refused';
      verdict.message = error instanceof Error ? error.message : String(error);
      record();
    },
  );
  return { verdict, settled };
}

async function raceHostNoiseAgainstShellOutput(): Promise<{
  hostNoise: ContainedWaitVerdict;
  shellSpeaking: ContainedWaitVerdict;
}> {
  vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
  const hostNoiseStream = createFakeStream();
  const shellSpeakingStream = createFakeStream();
  const stopHostNoise = scheduleEmissions(
    hostNoiseStream,
    evenCadence(HOST_WRITTEN_STEP, CONTAINED_WAIT_STEP_MS, CONTAINED_WAIT_CONTAINMENT_MS),
  );
  const stopShellSpeaking = scheduleEmissions(
    shellSpeakingStream,
    evenCadence(SHELL_WRITTEN_STEP, CONTAINED_WAIT_STEP_MS, CONTAINED_WAIT_CONTAINMENT_MS),
  );
  const waitOptions: ContainedWaitOptions = {
    stallMs: CONTAINED_WAIT_WINDOW_MS,
    intervalMs: ADVANCEMENT_POLL_MS,
    backstopAt: performance.now() + CONTAINED_WAIT_CONTAINMENT_MS,
  };
  const hostNoise = watchContainedWait(hostNoiseStream, waitOptions);
  const shellSpeaking = watchContainedWait(shellSpeakingStream, waitOptions);
  try {
    await vi.advanceTimersByTimeAsync(CONTAINED_WAIT_CONTAINMENT_MS + ADVANCEMENT_POLL_MS);
    return { hostNoise: hostNoise.verdict, shellSpeaking: shellSpeaking.verdict };
  } finally {
    stopHostNoise();
    stopShellSpeaking();
    await Promise.all([hostNoise.settled, shellSpeaking.settled]);
  }
}

describe('a console host speaking before the shell is never counted as shell output, and every byte after the shell speaks is', () => {
  test('console-host attach bytes arriving mid-wait are not counted as shell output and shell bytes are, and neither wait is refused before its containment', async () => {
    const { hostNoise, shellSpeaking } = await raceHostNoiseAgainstShellOutput();
    expect(hostNoise.buffer.length).toBeGreaterThan(HOST_WRITTEN_STEP.length);
    expect(shellSpeaking.buffer.length).toBeGreaterThan(SHELL_WRITTEN_STEP.length);
    expect(shellOutputBeyondAttach(hostNoise.buffer)).toBe('');
    expect(shellOutputBeyondAttach(shellSpeaking.buffer)).toBe(shellSpeaking.buffer);
    expect(hostNoise.settled).toBe('refused');
    expect(shellSpeaking.settled).toBe('refused');
    expect(hostNoise.atMs).toBeGreaterThanOrEqual(CONTAINED_WAIT_CONTAINMENT_MS);
    expect(shellSpeaking.atMs).toBeGreaterThanOrEqual(CONTAINED_WAIT_CONTAINMENT_MS);
    expect(hostNoise.message).toContain(CONTAINMENT_VERDICT);
    expect(hostNoise.message).toContain(NO_SHELL_OUTPUT_VERDICT);
    expect(hostNoise.message).not.toContain(SHELL_PROGRESS_COUNTED);
    expect(shellSpeaking.message).toContain(SHELL_PROGRESS_COUNTED);
  });

  test('a shell that speaks once and then leaves the host writing runs to containment, and the verdict names what it counted', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = createFakeStream();
    stream.emit(`${ATTACH_PROLOGUE}${SHELL_WRITTEN_STEP}`);
    const stopHostNoise = scheduleEmissions(
      stream,
      evenCadence(HOST_WRITTEN_STEP, CONTAINED_WAIT_STEP_MS, CONTAINED_WAIT_CONTAINMENT_MS),
    );
    const { verdict, settled } = watchContainedWait(stream, {
      stallMs: CONTAINED_WAIT_WINDOW_MS,
      intervalMs: ADVANCEMENT_POLL_MS,
      backstopAt: performance.now() + CONTAINED_WAIT_CONTAINMENT_MS,
    });
    try {
      await vi.advanceTimersByTimeAsync(CONTAINED_WAIT_CONTAINMENT_MS + ADVANCEMENT_POLL_MS);
      expect(verdict.settled).toBe('refused');
      expect(verdict.atMs).toBeGreaterThanOrEqual(CONTAINED_WAIT_CONTAINMENT_MS);
      expect(verdict.message).toContain(CONTAINMENT_VERDICT);
      expect(verdict.message).not.toContain(SILENT_SHELL_VERDICT);
      expect(verdict.message).toContain(
        `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters counted from the shell's first output on`,
      );
    } finally {
      stopHostNoise();
      await settled;
    }
  });
});

const LAUNCH_OUTPUT = 'd603e31a-bc79-47cd-b36f-7967714a865f\r\n';
const TITLE_INSIDE_THE_WINDOW_AT_MS = CONTAINED_WAIT_WINDOW_MS / 2;
const OUTPUT_PAST_THE_WINDOW_FROM_START_AT_MS =
  CONTAINED_WAIT_WINDOW_MS + CONTAINED_WAIT_WINDOW_MS / 4;

async function waitForLaunchOutputBehind(
  prologue: string,
  titleWrite: string,
): Promise<ContainedWaitVerdict> {
  vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
  const stream = createFakeStream();
  stream.emit(prologue);
  const stopWriting = scheduleEmissions(stream, [
    { atMs: TITLE_INSIDE_THE_WINDOW_AT_MS, chunk: titleWrite },
    { atMs: OUTPUT_PAST_THE_WINDOW_FROM_START_AT_MS, chunk: LAUNCH_OUTPUT },
  ]);
  const { verdict, settled } = watchContainedWait(
    stream,
    {
      stallMs: CONTAINED_WAIT_WINDOW_MS,
      intervalMs: ADVANCEMENT_POLL_MS,
      backstopAt: performance.now() + CONTAINED_WAIT_CONTAINMENT_MS,
    },
    () => stream.read().includes(LAUNCH_OUTPUT),
  );
  try {
    await vi.advanceTimersByTimeAsync(CONTAINED_WAIT_CONTAINMENT_MS + ADVANCEMENT_POLL_MS);
    return verdict;
  } finally {
    stopWriting();
    await settled;
  }
}

describe("a plain wait counts progress from the shell's own first byte, whichever ConPTY spoke before it", () => {
  test.each(RECORDED_ATTACH_PROLOGUES)(
    'a wait that sees only what $host writes on attach is refused at its containment and says it saw no shell output',
    async ({ prologue }) => {
      vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
      const stream = createFakeStream();
      stream.emit(prologue);
      const { verdict, settled } = watchContainedWait(stream, {
        stallMs: CONTAINED_WAIT_WINDOW_MS,
        intervalMs: ADVANCEMENT_POLL_MS,
        backstopAt: performance.now() + CONTAINED_WAIT_CONTAINMENT_MS,
      });
      await vi.advanceTimersByTimeAsync(CONTAINED_WAIT_CONTAINMENT_MS + ADVANCEMENT_POLL_MS);
      await settled;
      expect(verdict.settled).toBe('refused');
      expect(verdict.atMs).toBeGreaterThanOrEqual(CONTAINED_WAIT_CONTAINMENT_MS);
      expect(verdict.message).toContain(CONTAINMENT_VERDICT);
      expect(verdict.message).toContain(NO_SHELL_OUTPUT_VERDICT);
      expect(verdict.message).not.toContain(SHELL_PROGRESS_COUNTED);
      expect(verdict.message).toContain(`(received ${JSON.stringify(prologue)})`);
    },
  );

  test.each(RECORDED_ATTACH_PROLOGUES)(
    'a shell that keeps writing behind what $host wrote on attach is counted from its own first byte',
    async ({ prologue }) => {
      vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
      const stream = createFakeStream();
      stream.emit(`${prologue}${SHELL_WRITTEN_STEP}`);
      const stopShell = scheduleEmissions(
        stream,
        evenCadence(SHELL_WRITTEN_STEP, CONTAINED_WAIT_STEP_MS, CONTAINED_WAIT_CONTAINMENT_MS),
      );
      const { verdict, settled } = watchContainedWait(stream, {
        stallMs: CONTAINED_WAIT_WINDOW_MS,
        intervalMs: ADVANCEMENT_POLL_MS,
        backstopAt: performance.now() + CONTAINED_WAIT_CONTAINMENT_MS,
      });
      try {
        await vi.advanceTimersByTimeAsync(CONTAINED_WAIT_CONTAINMENT_MS + ADVANCEMENT_POLL_MS);
        expect(verdict.settled).toBe('refused');
        expect(verdict.message).toContain(CONTAINMENT_VERDICT);
        expect(verdict.message).toContain(
          `${verdict.buffer.length - prologue.length} characters counted from the shell's first output on`,
        );
      } finally {
        stopShell();
        await settled;
      }
    },
  );

  test('a title the shell sets behind the bundled ConPTY is shell output, and launch output past the window from the start still lands inside the containment', async () => {
    const verdict = await waitForLaunchOutputBehind(
      BUNDLED_ATTACH_PROLOGUE,
      WINDOWS_POWERSHELL_TITLE_ST,
    );
    expect({ settled: verdict.settled, message: verdict.message }).toEqual({
      settled: 'reached',
      message: '',
    });
    expect(verdict.atMs).toBeGreaterThanOrEqual(OUTPUT_PAST_THE_WINDOW_FROM_START_AT_MS);
  });

  test('the title the inbox ConPTY paints in its first-paint frame is not shell output, and launch output past the window from the start still lands inside the containment', async () => {
    expect(shellOutputBeyondAttach(`${ATTACH_PROLOGUE}${CONPTY_INIT_FRAME_ST_TITLE}`)).toBe('');
    const verdict = await waitForLaunchOutputBehind(ATTACH_PROLOGUE, CONPTY_INIT_FRAME_ST_TITLE);
    expect({ settled: verdict.settled, message: verdict.message }).toEqual({
      settled: 'reached',
      message: '',
    });
    expect(verdict.atMs).toBeGreaterThanOrEqual(OUTPUT_PAST_THE_WINDOW_FROM_START_AT_MS);
  });
});

const VALIDATED_WAIT = { intervalMs: 5, stallMs: 60 } as const;
const NON_FINITE_BACKSTOP_REFUSAL = `backstop for ${ENCODED_COMMAND_LABEL} must be a finite`;

async function messageFromRefusal(call: () => Promise<unknown>): Promise<string> {
  try {
    await call();
  } catch (error) {
    return error instanceof Error ? error.message : String(error);
  }
  throw new Error('the wait under test was expected to be refused and was not');
}

describe('a wait may only be generous about elapsed time once its containment is real', () => {
  test('a containment instant that cannot bound the wait is named instead of polled against', async () => {
    const stream = createFakeStream();
    stream.emit(ATTACH_PROLOGUE);
    for (const backstopAt of [performance.now() + VALIDATED_WAIT.stallMs, 0]) {
      const contained: ContainedWaitOptions = { ...VALIDATED_WAIT, backstopAt };
      const refusal = await messageFromRefusal(() =>
        waitForCondition(stream, () => false, ENCODED_COMMAND_LABEL, contained),
      );
      expect(refusal).toContain(ENCODED_COMMAND_LABEL);
      expect(refusal).not.toContain(NON_FINITE_BACKSTOP_REFUSAL);
    }
    for (const backstopAt of [Number.NaN, Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY]) {
      const unbounded: ContainedWaitOptions = { ...VALIDATED_WAIT, backstopAt };
      await expect(
        waitForCondition(stream, () => false, ENCODED_COMMAND_LABEL, unbounded),
      ).rejects.toThrow(NON_FINITE_BACKSTOP_REFUSAL);
    }
  });
});

function wedgedBootedStream(): FakeStream {
  const stream = createFakeStream();
  stream.emit(`${ATTACH_PROLOGUE}${BOOTED_PROMPT}`);
  return stream;
}

describe('a grant gone before a wait could poll once is refused as spent, not reported as silence or as a containment the wait had', () => {
  test('a containment a poll fits inside refuses for silence; one that no longer does refuses as a spent grant', async () => {
    const silenced = await messageFromRefusal(() =>
      waitForCondition(wedgedBootedStream(), () => false, ENCODED_COMMAND_LABEL, {
        ...VALIDATED_WAIT,
        backstopAt: performance.now() + VALIDATED_WAIT.stallMs,
      }),
    );
    expect(silenced).toContain(SILENT_SHELL_VERDICT);
    expect(silenced).not.toContain(CONTAINMENT_VERDICT);
    expect(silenced).not.toContain(SPENT_GRANT_REFUSAL);
    for (const backstopAt of [
      performance.now() + VALIDATED_WAIT.intervalMs - 1,
      0,
      performance.now() - VALIDATED_WAIT.stallMs,
    ]) {
      const spent = await messageFromRefusal(() =>
        waitForCondition(wedgedBootedStream(), () => false, ENCODED_COMMAND_LABEL, {
          ...VALIDATED_WAIT,
          backstopAt,
        }),
      );
      expect(spent).toContain(SPENT_GRANT_REFUSAL);
      expect(spent).not.toContain(SILENT_SHELL_VERDICT);
      expect(spent).not.toContain(CONTAINMENT_VERDICT);
    }
  });

  test('a spent grant reaches the refusal class from a plain wait, the way it does from the evaluated-input one', async () => {
    await expect(
      waitForCondition(wedgedBootedStream(), () => false, ENCODED_COMMAND_LABEL, {
        ...VALIDATED_WAIT,
        backstopAt: 0,
      }),
    ).rejects.toBeInstanceOf(HarnessBudgetRefusal);
    await expect(
      waitForShellReady(wedgedBootedStream(), ENCODED_COMMAND_LABEL, {
        ...VALIDATED_WAIT,
        quietSamples: 2,
        backstopAt: 0,
      }),
    ).rejects.toBeInstanceOf(HarnessBudgetRefusal);
    await expect(
      waitForEvaluatedInput(
        wedgedBootedStream(),
        () => undefined,
        INPUT_READY_PROBE,
        ENCODED_COMMAND_LABEL,
        { budgetMs: VALIDATED_WAIT.intervalMs - 2, intervalMs: VALIDATED_WAIT.intervalMs },
      ),
    ).rejects.toBeInstanceOf(HarnessBudgetRefusal);
    const expired = await waitForCondition(
      wedgedBootedStream(),
      () => false,
      ENCODED_COMMAND_LABEL,
      { ...VALIDATED_WAIT, backstopAt: performance.now() + VALIDATED_WAIT.stallMs },
    ).catch((error: unknown) => error);
    expect(expired).toBeInstanceOf(Error);
    expect(expired).not.toBeInstanceOf(HarnessBudgetRefusal);
  });

  test('a condition already true when the wait is called resolves however thin the containment is', async () => {
    for (const backstopAt of [performance.now() + VALIDATED_WAIT.intervalMs - 1, 0]) {
      await expect(
        waitForCondition(wedgedBootedStream(), () => true, ENCODED_COMMAND_LABEL, {
          ...VALIDATED_WAIT,
          backstopAt,
        }),
      ).resolves.toBeUndefined();
      await expect(
        waitForCondition(wedgedBootedStream(), () => false, ENCODED_COMMAND_LABEL, {
          ...VALIDATED_WAIT,
          backstopAt,
        }),
      ).rejects.toBeInstanceOf(HarnessBudgetRefusal);
    }
  });

  test('a wait that observed no shell output at all says so rather than report progress it never saw', async () => {
    const attachOnly = createFakeStream();
    attachOnly.emit(ATTACH_PROLOGUE);
    const stall = await messageFromRefusal(() =>
      waitForCondition(attachOnly, () => false, ENCODED_COMMAND_LABEL, {
        ...VALIDATED_WAIT,
        backstopAt: performance.now() + VALIDATED_WAIT.stallMs,
      }),
    );
    expect(stall).toContain('without any shell output');
    expect(stall).not.toContain(SILENT_SHELL_VERDICT);
    expect(stall).not.toContain(SHELL_PROGRESS_COUNTED);
  });
});

describe('a wait window the containment cut is named as cut, so a red tells starvation from a stalled shell', () => {
  test('the window the call site declared is named as cut only when the containment took it away, and a containment that outlasts it ends as a spent containment', async () => {
    const declaredStallMs = VALIDATED_WAIT.stallMs * 4;
    const cut = await messageFromRefusal(() =>
      waitForCondition(wedgedBootedStream(), () => false, ENCODED_COMMAND_LABEL, {
        ...VALIDATED_WAIT,
        stallMs: declaredStallMs,
        backstopAt: performance.now() + VALIDATED_WAIT.stallMs,
      }),
    );
    expect(cut).toContain(
      `after ${VALIDATED_WAIT.stallMs}ms (the containment it runs inside cut the ${declaredStallMs}ms it declared)`,
    );

    const uncut = await messageFromRefusal(() =>
      waitForCondition(wedgedBootedStream(), () => false, ENCODED_COMMAND_LABEL, {
        ...VALIDATED_WAIT,
        backstopAt: performance.now() + declaredStallMs,
      }),
    );
    expect(uncut).toContain(CONTAINMENT_VERDICT);
    expect(uncut).not.toContain('it declared');
  });

  test('a reduction too small to change the printed number does not claim a cut', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const { verdict, settled } = watchContainedWait(wedgedBootedStream(), {
      stallMs: VALIDATED_WAIT.stallMs,
      intervalMs: VALIDATED_WAIT.intervalMs,
      backstopAt: performance.now() + VALIDATED_WAIT.stallMs - 0.4,
    });
    try {
      await vi.advanceTimersByTimeAsync(VALIDATED_WAIT.stallMs + VALIDATED_WAIT.intervalMs);
      expect(verdict.message).toContain(
        `after ${VALIDATED_WAIT.stallMs}ms ${SILENT_SHELL_VERDICT}`,
      );
      expect(verdict.message).not.toContain('it declared');
    } finally {
      await settled;
    }
  });

  test('an input round trip names the silence window its grant cut, and a grant larger than the window ends as a spent containment with no cut claimed', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const cutStream = createFakeStream();
    const uncutStream = createFakeStream();
    cutStream.emit(BOOTED_PROMPT);
    uncutStream.emit(BOOTED_PROMPT);
    const cut = watchEvaluatedInput(
      cutStream,
      driveEvaluatingShell(cutStream, { evaluates: false }).send,
      { budgetMs: READINESS_CEILING_MS / 8 },
    );
    const uncut = watchEvaluatedInput(
      uncutStream,
      driveEvaluatingShell(uncutStream, { evaluates: false }).send,
      { budgetMs: READINESS_CEILING_MS * 2 },
    );
    await vi.advanceTimersByTimeAsync(READINESS_CEILING_MS * 2 + READINESS_CEILING_MS / 8);
    await Promise.all([cut.settled, uncut.settled]);
    expect(uncut.verdict.message).toContain(ROUND_TRIP_CONTAINMENT_VERDICT);
    expect(uncut.verdict.message).toContain(`(received ${JSON.stringify(BOOTED_PROMPT)})`);
    expect(uncut.verdict.message).not.toContain('it declared');
    expect(cut.verdict.message).toBe(
      `timeout waiting for: input ready after ${READINESS_CEILING_MS / 8}ms (the containment it runs inside cut the ${READINESS_CEILING_MS}ms it declared) ${SILENT_SHELL_VERDICT} (received ${JSON.stringify(BOOTED_PROMPT)})`,
    );
  });

  test('a window that spans its containment reports silence only when the shell wrote nothing through it, so a shell that wrote just before the containment is counted instead', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const stream = wedgedBootedStream();
    const stopShell = scheduleEmissions(stream, [
      { atMs: VALIDATED_WAIT.stallMs - 2 * VALIDATED_WAIT.intervalMs, chunk: SHELL_WRITTEN_STEP },
    ]);
    const { verdict, settled } = watchContainedWait(stream, {
      ...VALIDATED_WAIT,
      stallMs: VALIDATED_WAIT.stallMs * 4,
      backstopAt: performance.now() + VALIDATED_WAIT.stallMs,
    });
    try {
      await vi.advanceTimersByTimeAsync(VALIDATED_WAIT.stallMs + VALIDATED_WAIT.intervalMs);
      expect(verdict.settled).toBe('refused');
      expect(verdict.message).toContain(CONTAINMENT_VERDICT);
      expect(verdict.message).not.toContain(SILENT_SHELL_VERDICT);
      expect(verdict.message).toContain(
        `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters counted from the shell's first output on`,
      );
    } finally {
      stopShell();
      await settled;
    }
  });

  test('a window its containment outlasts is named when the shell went silent past it, and not when the shell last wrote inside it', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const lastWriteAtMs = CONTAINED_WAIT_CONTAINMENT_MS / 2;
    const silentPastWindowMs = CONTAINED_WAIT_WINDOW_MS;
    const wroteInsideWindowMs = CONTAINED_WAIT_CONTAINMENT_MS - CONTAINED_WAIT_WINDOW_MS;
    const watchWithWindow = (stallMs: number) => {
      const stream = wedgedBootedStream();
      const stopShell = scheduleEmissions(stream, [
        { atMs: lastWriteAtMs, chunk: SHELL_WRITTEN_STEP },
      ]);
      const watched = watchContainedWait(stream, {
        stallMs,
        intervalMs: ADVANCEMENT_POLL_MS,
        backstopAt: performance.now() + CONTAINED_WAIT_CONTAINMENT_MS,
      });
      return { ...watched, stopShell };
    };
    const silentPast = watchWithWindow(silentPastWindowMs);
    const wroteInside = watchWithWindow(wroteInsideWindowMs);
    try {
      await vi.advanceTimersByTimeAsync(CONTAINED_WAIT_CONTAINMENT_MS + ADVANCEMENT_POLL_MS);
      for (const { verdict } of [silentPast, wroteInside]) {
        expect(verdict.settled).toBe('refused');
        expect(verdict.atMs).toBeGreaterThanOrEqual(CONTAINED_WAIT_CONTAINMENT_MS);
        expect(verdict.message).toContain(CONTAINMENT_VERDICT);
        expect(verdict.message).not.toContain(SILENT_SHELL_VERDICT);
        expect(verdict.message).toContain(
          `${verdict.buffer.length - ATTACH_PROLOGUE.length} characters counted from the shell's first output on`,
        );
      }
      expect(silentPast.verdict.message).toContain(
        `longer than its ${silentPastWindowMs}ms stall window`,
      );
      expect(wroteInside.verdict.message).not.toContain('stall window');
    } finally {
      silentPast.stopShell();
      wroteInside.stopShell();
      await Promise.all([silentPast.settled, wroteInside.settled]);
    }
  });
});

const QUIET_READY_LABEL = 'interactive shell ready';

interface ShellReadyVerdict {
  settled: 'pending' | 'ready' | 'refused';
  atMs: number;
  message: string;
  error: unknown;
}

function watchShellReady(
  stream: PtyStream,
  options: ShellReadyOptions,
): { verdict: ShellReadyVerdict; settled: Promise<void> } {
  const startedAt = performance.now();
  const verdict: ShellReadyVerdict = {
    settled: 'pending',
    atMs: Number.NaN,
    message: '',
    error: null,
  };
  const settled = waitForShellReady(stream, QUIET_READY_LABEL, options).then(
    () => {
      verdict.settled = 'ready';
      verdict.atMs = performance.now() - startedAt;
    },
    (error: unknown) => {
      verdict.settled = 'refused';
      verdict.atMs = performance.now() - startedAt;
      verdict.message = error instanceof Error ? error.message : String(error);
      verdict.error = error;
    },
  );
  return { verdict, settled };
}

describe('a wait whose readiness is silence is refused at entry when its stall window or its grant cannot outlast that silence', () => {
  test.each([
    ['one poll longer than', VALIDATED_WAIT.stallMs / VALIDATED_WAIT.intervalMs + 1],
    ['exactly as long as', VALIDATED_WAIT.stallMs / VALIDATED_WAIT.intervalMs],
  ])(
    'a quiet window %s the stall window is refused at entry as configuration, never reported as a silent shell',
    async (_position, quietSamples) => {
      vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
      const quietWindowMs = quietSamples * VALIDATED_WAIT.intervalMs;
      const booted = createFakeStream();
      booted.emit(BOOTED_PROMPT);
      const { verdict, settled } = watchShellReady(booted, { ...VALIDATED_WAIT, quietSamples });
      await vi.advanceTimersByTimeAsync(quietWindowMs + VALIDATED_WAIT.intervalMs);
      await settled;
      expect({ settled: verdict.settled, message: verdict.message }).toEqual({
        settled: 'refused',
        message: `the stall window for ${QUIET_READY_LABEL} must outlast the ${quietWindowMs}ms of quiet it counts as ready, got ${VALIDATED_WAIT.stallMs}ms`,
      });
      expect(verdict.atMs).toBe(0);
      expect(verdict.error).toBeInstanceOf(Error);
      expect(verdict.error).not.toBeInstanceOf(HarnessBudgetRefusal);
    },
  );

  test('a stall window one poll longer than the quiet window lets a booted shell report ready once that quiet has passed', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const quietSamples = VALIDATED_WAIT.stallMs / VALIDATED_WAIT.intervalMs - 1;
    const booted = createFakeStream();
    booted.emit(BOOTED_PROMPT);
    const { verdict, settled } = watchShellReady(booted, { ...VALIDATED_WAIT, quietSamples });
    await vi.advanceTimersByTimeAsync(VALIDATED_WAIT.stallMs + VALIDATED_WAIT.intervalMs);
    await settled;
    expect({ settled: verdict.settled, message: verdict.message }).toEqual({
      settled: 'ready',
      message: '',
    });
    expect(verdict.atMs).toBe(quietSamples * VALIDATED_WAIT.intervalMs);
  });

  test('a containment exactly as long as the quiet window is refused at entry as a spent grant, not failed as a containment the wait had', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const quietSamples = VALIDATED_WAIT.stallMs / VALIDATED_WAIT.intervalMs - 1;
    const quietWindowMs = quietSamples * VALIDATED_WAIT.intervalMs;
    const spawning = createFakeStream();
    const { verdict, settled } = watchShellReady(spawning, {
      ...VALIDATED_WAIT,
      quietSamples,
      backstopAt: performance.now() + quietWindowMs,
    });
    spawning.emit(BOOTED_PROMPT);
    await vi.advanceTimersByTimeAsync(quietWindowMs + VALIDATED_WAIT.intervalMs);
    await settled;
    expect({ settled: verdict.settled, message: verdict.message }).toEqual({
      settled: 'refused',
      message: `the grant for ${QUIET_READY_LABEL} was spent before the wait could count ${quietSamples} quiet polls: ${quietWindowMs}ms left does not outlast the ${quietWindowMs}ms they take`,
    });
    expect(verdict.atMs).toBe(0);
    expect(verdict.error).toBeInstanceOf(HarnessBudgetRefusal);
  });

  test('a containment one poll longer than the quiet window lets a shell that boots after the call report ready', async () => {
    vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
    const quietSamples = VALIDATED_WAIT.stallMs / VALIDATED_WAIT.intervalMs - 1;
    const quietWindowMs = quietSamples * VALIDATED_WAIT.intervalMs;
    const spawning = createFakeStream();
    const { verdict, settled } = watchShellReady(spawning, {
      ...VALIDATED_WAIT,
      quietSamples,
      backstopAt: performance.now() + quietWindowMs + VALIDATED_WAIT.intervalMs,
    });
    spawning.emit(BOOTED_PROMPT);
    await vi.advanceTimersByTimeAsync(quietWindowMs + 2 * VALIDATED_WAIT.intervalMs);
    await settled;
    expect({ settled: verdict.settled, message: verdict.message }).toEqual({
      settled: 'ready',
      message: '',
    });
    expect(verdict.atMs).toBe(quietWindowMs + VALIDATED_WAIT.intervalMs);
  });
});

const 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_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';
const POSIX_PROMPT = 'bash-5.2$ ';
const SHELL_EXIT = 'exited (code 1, signal none)';
const LATE_SHARE_OF_CONTAINMENT = 7 / 8;
const EARLY_SHARE_OF_CONTAINMENT = 1 / 16;
const PAST_CONTAINMENT_SHARE = 1 / 8;
const NOTICE_SHARE_OF_CONTAINMENT = 1 / 100;

function grantWithWholeBudgetLeft(platform: NodeJS.Platform): number {
  return createHarnessBudget(
    harnessTimeouts(platform).budgetMs,
    HARNESS_REPORT_RESERVE_MS,
    () => 0,
  ).grantMs('the first scenario started');
}

function answerAt(stream: FakeStream, atMs: number | null, chunk: string): () => void {
  return atMs === null ? () => undefined : scheduleEmissions(stream, [{ atMs, chunk }]);
}

function driveShellAnsweringAt(
  stream: FakeStream,
  answerAtMs: number | null,
): { sent: string[]; send: (data: string) => void; dispose: () => void } {
  const sent: string[] = [];
  const answer =
    answerAtMs === null
      ? null
      : setTimeout(() => {
          for (const data of sent) {
            const typed = data.replace(/\r$/u, '');
            const output = evaluateFakePowerShellCommand(typed);
            if (output !== null) stream.emit(`${typed}\r\n${output}\r\n`);
          }
        }, answerAtMs);
  return {
    sent,
    send: (data) => {
      sent.push(data);
    },
    dispose: () => {
      if (answer !== null) clearTimeout(answer);
    },
  };
}

interface ReadinessSiteRun {
  settled: Promise<unknown>;
  probesWritten: () => readonly string[] | null;
  dispose: () => void;
}

interface ReadinessSite {
  site: string;
  platform: NodeJS.Platform;
  label: string;
  speaks: ReadonlyArray<{ atMs: number; chunk: string }>;
  probesWritten: readonly string[] | null;
  begin(
    stream: FakeStream,
    label: string,
    containmentMs: number,
    answerAtMs: number | null,
  ): ReadinessSiteRun;
}

const READINESS_SITES: readonly ReadinessSite[] = [
  {
    site: 'the Windows input round trip',
    platform: 'win32',
    label: 'interactive shell ready at project root',
    speaks: [
      { atMs: 0, chunk: BUNDLED_ATTACH_PROLOGUE },
      {
        atMs: RECORDED_FIRST_OUTPUT_AT_MS,
        chunk: RECORDED_S1_BANNER_THEN_SILENCE.slice(BUNDLED_ATTACH_PROLOGUE.length),
      },
    ],
    probesWritten: [INPUT_READY_PROBE.input],
    begin: (stream, label, containmentMs, answerAtMs) => {
      const shell = driveShellAnsweringAt(stream, answerAtMs);
      return {
        settled: waitForEvaluatedInput(stream, shell.send, INPUT_READY_PROBE, label, {
          budgetMs: containmentMs,
        }),
        probesWritten: () => shell.sent,
        dispose: shell.dispose,
      };
    },
  },
  {
    site: 'a command wait after shell output',
    platform: 'win32',
    label: 'evaluated command output',
    speaks: [
      { atMs: 0, chunk: `${BUNDLED_ATTACH_PROLOGUE}${BOOTED_PROMPT}${HARNESS_COMMAND_ECHO}` },
    ],
    probesWritten: null,
    begin: (stream, label, containmentMs, answerAtMs) => {
      const stopAnswer = answerAt(stream, answerAtMs, `${HARNESS_COMMAND_OUTPUT}\r\n`);
      return {
        settled: waitForCondition(
          stream,
          () => stream.read().includes(HARNESS_COMMAND_OUTPUT),
          label,
          { backstopAt: performance.now() + containmentMs },
        ),
        probesWritten: () => null,
        dispose: stopAnswer,
      };
    },
  },
  {
    site: 'the POSIX interactive-shell wait',
    platform: 'linux',
    label: 'interactive shell ready at project root',
    speaks: [],
    probesWritten: null,
    begin: (stream, label, containmentMs, answerAtMs) => {
      const stopAnswer = answerAt(stream, answerAtMs, POSIX_PROMPT);
      return {
        settled: waitForShellReady(stream, label, {
          backstopAt: performance.now() + containmentMs,
        }),
        probesWritten: () => null,
        dispose: stopAnswer,
      };
    },
  },
];

interface LiveShellVerdict {
  settled: 'pending' | 'ready' | 'refused';
  atMs: number;
  message: string;
  probesWritten: readonly string[] | null;
}

async function runReadinessSite(
  site: ReadinessSite,
  containmentMs: number,
  script: { answerAtMs: number | null; exitAtMs: number | null },
): Promise<LiveShellVerdict> {
  vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
  const stream = createFakeStream();
  for (const step of site.speaks) if (step.atMs === 0) stream.emit(step.chunk);
  const stopSpeaking = scheduleEmissions(
    stream,
    site.speaks.filter((step) => step.atMs > 0),
  );
  const exit =
    script.exitAtMs === null ? null : setTimeout(() => stream.fail(SHELL_EXIT), script.exitAtMs);
  const startedAt = performance.now();
  const verdict: LiveShellVerdict = {
    settled: 'pending',
    atMs: Number.NaN,
    message: '',
    probesWritten: null,
  };
  const run = site.begin(stream, site.label, containmentMs, script.answerAtMs);
  void run.settled.then(
    () => {
      verdict.settled = 'ready';
      verdict.atMs = performance.now() - startedAt;
    },
    (error: unknown) => {
      verdict.settled = 'refused';
      verdict.atMs = performance.now() - startedAt;
      verdict.message = error instanceof Error ? error.message : String(error);
    },
  );
  try {
    await vi.advanceTimersByTimeAsync(
      Math.ceil(containmentMs * (1 + PAST_CONTAINMENT_SHARE + NOTICE_SHARE_OF_CONTAINMENT)),
    );
    verdict.probesWritten = run.probesWritten();
    return verdict;
  } finally {
    stopSpeaking();
    if (exit !== null) clearTimeout(exit);
    run.dispose();
  }
}

describe.each(READINESS_SITES)(
  "$site takes its verdict from the shell's answer, its exit or its spent containment, never from its silence",
  (site) => {
    const containmentMs = grantWithWholeBudgetLeft(site.platform);
    const lateAtMs = Math.round(containmentMs * LATE_SHARE_OF_CONTAINMENT);
    const lastSpokeAtMs = Math.max(0, ...site.speaks.map((step) => step.atMs));
    const noticeMs = containmentMs * NOTICE_SHARE_OF_CONTAINMENT;

    test('a live shell that stays silent until late in its containment and then answers is admitted', async () => {
      const verdict = await runReadinessSite(site, containmentMs, {
        answerAtMs: lateAtMs,
        exitAtMs: null,
      });
      expect({ settled: verdict.settled, message: verdict.message }).toEqual({
        settled: 'ready',
        message: '',
      });
      expect(verdict.atMs).toBeGreaterThanOrEqual(lateAtMs);
      expect(verdict.atMs).toBeLessThan(containmentMs);
      expect(verdict.probesWritten).toEqual(site.probesWritten);
    });

    test('a live shell that has not answered when its containment is spent is refused there as a spent containment, though it would answer later', async () => {
      const verdict = await runReadinessSite(site, containmentMs, {
        answerAtMs: Math.round(containmentMs * (1 + PAST_CONTAINMENT_SHARE / 2)),
        exitAtMs: null,
      });
      expect(verdict.settled).toBe('refused');
      expect(verdict.message).toContain(`${site.label} was not reached inside its`);
      expect(verdict.atMs).toBeGreaterThanOrEqual(containmentMs);
      expect(verdict.atMs).toBeLessThan(containmentMs + noticeMs);
      expect(verdict.probesWritten).toEqual(site.probesWritten);
    });

    test('a shell that exits right after it speaks is refused at its exit', async () => {
      const exitAtMs = lastSpokeAtMs + Math.round(containmentMs * EARLY_SHARE_OF_CONTAINMENT);
      const verdict = await runReadinessSite(site, containmentMs, {
        answerAtMs: null,
        exitAtMs,
      });
      expect(verdict.settled).toBe('refused');
      expect(verdict.message).toContain(`shell failed before ${site.label}: ${SHELL_EXIT}`);
      expect(verdict.atMs).toBeGreaterThanOrEqual(exitAtMs);
      expect(verdict.atMs).toBeLessThan(exitAtMs + noticeMs);
      expect(verdict.probesWritten).toEqual(site.probesWritten);
    });

    test('a shell that exits late in its containment, after a long silence, is refused at its exit and not earlier for the silence', async () => {
      const verdict = await runReadinessSite(site, containmentMs, {
        answerAtMs: null,
        exitAtMs: lateAtMs,
      });
      expect(verdict.settled).toBe('refused');
      expect(verdict.message).toContain(`shell failed before ${site.label}: ${SHELL_EXIT}`);
      expect(verdict.atMs).toBeGreaterThanOrEqual(lateAtMs);
      expect(verdict.atMs).toBeLessThan(lateAtMs + noticeMs);
      expect(verdict.probesWritten).toEqual(site.probesWritten);
    });
  },
);

const EXIT_AFTER_KILL_LABEL = 'exit after kill';

function createShellExitingOn(exitsOn: 'SIGKILL' | 'never'): SpawnPty {
  return (): PtyProcessLike => {
    let exit: (event: { exitCode: number | undefined; signal?: number }) => void = () => undefined;
    return {
      pid: 4244,
      onData(listener) {
        queueMicrotask(() => listener(POSIX_PROMPT));
      },
      onExit(listener) {
        exit = listener;
      },
      write() {},
      resize() {},
      kill(signal) {
        if (exitsOn === 'SIGKILL' && signal === 'SIGKILL') {
          queueMicrotask(() => exit({ exitCode: undefined, signal: 9 }));
        }
      },
      pause() {},
      resume() {},
    };
  };
}

interface ExitAfterKillVerdict {
  settled: 'pending' | 'reached' | 'refused';
  atMs: number;
  message: string;
  exited: boolean;
}

async function waitOutExitAfterKill(
  exitsOn: 'SIGKILL' | 'never',
  containmentMs: number,
): Promise<ExitAfterKillVerdict> {
  vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'performance'] });
  const host = createPtyHostProbe({
    spawn: createShellExitingOn(exitsOn),
    env: { PATH: '/usr/bin', SHELL: '/bin/sh' },
    platform: 'linux',
    shellExists: () => true,
  });
  const first = host.streamOf('c1');
  try {
    host.send({ type: 'create', ptyId: 'c1', cwd: '/tmp', cols: 80, rows: 24 });
    host.send({ type: 'kill', ptyId: 'c1' });
    const startedAt = performance.now();
    const verdict: ExitAfterKillVerdict = {
      settled: 'pending',
      atMs: Number.NaN,
      message: '',
      exited: false,
    };
    void waitForCondition(first, () => host.exitOf('c1') !== null, EXIT_AFTER_KILL_LABEL, {
      backstopAt: startedAt + containmentMs,
    }).then(
      () => {
        verdict.settled = 'reached';
        verdict.atMs = performance.now() - startedAt;
      },
      (error: unknown) => {
        verdict.settled = 'refused';
        verdict.atMs = performance.now() - startedAt;
        verdict.message = error instanceof Error ? error.message : String(error);
      },
    );
    await vi.advanceTimersByTimeAsync(Math.ceil(containmentMs * (1 + NOTICE_SHARE_OF_CONTAINMENT)));
    verdict.exited = host.exitOf('c1') !== null;
    return verdict;
  } finally {
    host.killActive();
  }
}

describe('a wait whose condition is the shell exiting keeps its meaning: the exit is the success, and a shell still alive is the failure', () => {
  test('waiting on the exit of a shell that exits once the host escalates the kill is reached before its containment', async () => {
    const containmentMs = grantWithWholeBudgetLeft('win32');
    const verdict = await waitOutExitAfterKill('SIGKILL', containmentMs);
    expect({ settled: verdict.settled, message: verdict.message }).toEqual({
      settled: 'reached',
      message: '',
    });
    expect(verdict.exited).toBe(true);
    expect(verdict.atMs).toBeLessThan(containmentMs);
  });

  test('waiting on the exit of a shell that outlives every kill is refused when its containment is spent, not before', async () => {
    const containmentMs = grantWithWholeBudgetLeft('win32');
    const verdict = await waitOutExitAfterKill('never', containmentMs);
    expect(verdict.settled).toBe('refused');
    expect(verdict.message).toContain(EXIT_AFTER_KILL_LABEL);
    expect(verdict.exited).toBe(false);
    expect(verdict.atMs).toBeGreaterThanOrEqual(containmentMs);
    expect(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();
      }
    },
  );
});