From 2566b36c29bc43a9177664ff4b468bf1f1863108 Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 1 Oct 2026 03:57:32 +0800 Subject: [PATCH] fix: contain dsh-native attach/recovery chains within their own test (#1797) The windows-matrix flake in "#983 maybeRetry self-heals a base-less register after a failed respawn" had two timing failures sharing one mechanism: fire-and-forget verify chains outliving the test that started them. - suites calling apply() tore down their mock proxy while the attach chain was still probing; on loaded runners the probe then sees a dead target, falls back to an unstubbed real bootstrap(), and the child's late tail clobbered the shared module-global register.base/toolsReady mid-window of a later test ("proxy is down" at execute / "plugin headers stamped" timeout). - the #983 self-heal test raced its own initial chain with a fixed 50ms sleep; a slow dead-port probe left the base set when the first headersFor ran, parking retryAt 10s ahead of the 5s wait cap. Fix at the decision point instead of widening sleeps: - src/agent/dsh-native.ts: track in-flight attach/recovery flights (pendingChains + trackChain at the apply() state.ready sites and the maybeRetry respawn branch); expose _settleNativeForTest() awaiting them plus a macrotask flush. Behavior-neutral outside tests. - tests/dsh-native.test.ts: afterEach drains pending chains (universal backstop); command-behavior suites settle before closing their mock proxies (removes the fallback-to-real-bootstrap window); the #983 test waits for initial-chain settlement event-driven instead of sleeping 50ms; new regression test pins the slow-dead-port-probe shape by delaying :1 fetches 80ms in-test (failed deterministically pre-fix at >=30ms delay). Verified: typecheck + build + full suite (3470 tests, 0 fail) + slow loopback sweep SLOW_MS=30/50/100/150 x2 (green; pre-fix failed at 30+) + standalone repro delay sweep 0..200ms x2 (pass; pre-fix failed at 30+). --- src/agent/dsh-native.ts | 26 +++++++++++++++++++++++--- tests/dsh-native.test.ts | 8 ++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/agent/dsh-native.ts b/src/agent/dsh-native.ts index eaffb3b279..7794227d6a 100644 --- a/src/agent/dsh-native.ts +++ b/src/agent/dsh-native.ts @@ -138,6 +138,19 @@ type RegisterState = { base: string | undefined; toolsReady: boolean; dead: bool const register: RegisterState = { base: undefined, toolsReady: false, dead: false, retryAt: 0, pending: undefined }; +// #1797: attach/recovery chains still in flight after apply() returned — a late +// chain would clobber the shared register mid-run of whatever executes next. +const pendingChains = new Set>(); + +function trackChain(p: Promise): Promise { + pendingChains.add(p); + void p.then( + () => { pendingChains.delete(p); }, + () => { pendingChains.delete(p); }, + ); + return p; +} + // #1772: once-per-process — the web-profile compaction caveat is logged a // single time even though apply() may run again after context re-arming. let webProfileWarned = false; @@ -444,7 +457,7 @@ function maybeRetry(ctx: PluginContext): void { const respawn = state.respawn; if (respawn === undefined) return; register.retryAt = Date.now() + RETRY_INTERVAL_MS; - void respawn() + void trackChain(respawn()) .then((origin) => { if (origin === undefined) return; register.base = origin; @@ -695,7 +708,7 @@ export function apply(ctx: PluginContext): void { console.warn(line); }); }; - state.ready = start(); + state.ready = trackChain(start()); } else { state.ready = Promise.resolve(undefined); } @@ -708,7 +721,7 @@ export function apply(ctx: PluginContext): void { register.base = undefined; register.toolsReady = false; }; - state.ready = start(); + state.ready = trackChain(start()); } // #1158 L2: a refusal sends model traffic DIRECT. First refusal per @@ -882,6 +895,13 @@ export function _resetRegisterForTest(base: string | undefined): void { modelInfo.refreshing = false; } +/** Test hook (#1797): resolve once every in-flight attach/recovery chain has + * settled, plus one macrotask so their .then state-writes have run. */ +export function _settleNativeForTest(): Promise { + const all = [...pendingChains]; + return Promise.all(all).catch(() => undefined).then(() => new Promise((resolve) => setTimeout(resolve, 0))); +} + export function _stateHeadersForTest(): ((url: string) => Record | undefined) | undefined { return state.headersFor; } diff --git a/tests/dsh-native.test.ts b/tests/dsh-native.test.ts index 64ffd40a9c..97dca587a1 100644 --- a/tests/dsh-native.test.ts +++ b/tests/dsh-native.test.ts @@ -1,13 +1,17 @@ -import test from "node:test"; +import test, { afterEach } from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import http from "node:http"; import { pathToFileURL } from "node:url"; -import { apply, planNativeDsh, shouldBootstrapNativeDsh, persistClientEvent, _resetRegisterForTest, _setSpawnForTest, _stateHeadersForTest, _stateRespawnForTest, _stateTakeoverGateForTest, _stateToolsReadyForTest, _noteRoutedForTest, _resetRoutedForTest, _resetWebProfileWarningForTest } from "../src/agent/dsh-native.ts"; +import { apply, planNativeDsh, shouldBootstrapNativeDsh, persistClientEvent, _resetRegisterForTest, _setSpawnForTest, _settleNativeForTest, _stateHeadersForTest, _stateRespawnForTest, _stateTakeoverGateForTest, _stateToolsReadyForTest, _noteRoutedForTest, _resetRoutedForTest, _resetWebProfileWarningForTest } from "../src/agent/dsh-native.ts"; import { rmrf } from "./tmp-rm.ts"; +// #1797: drain ALL in-flight attach/recovery chains after each test — defense-in-depth +// behind #1798's per-suite settles, which only await the latest apply()'s gate. +afterEach(() => { void _settleNativeForTest(); }); + // #1365: legacy dead-attach suites see no routed traffic, so without this pin // each would sit out the FULL routed-evidence grace window (default 5s, // native-intercept observeRoutedOrigin) before the spawn fallback — keep the