Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/agent/dsh-native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Promise<unknown>>();

function trackChain<T>(p: Promise<T>): Promise<T> {
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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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<void> {
const all = [...pendingChains];
return Promise.all(all).catch(() => undefined).then(() => new Promise((resolve) => setTimeout(resolve, 0)));
}

export function _stateHeadersForTest(): ((url: string) => Record<string, string> | undefined) | undefined {
return state.headersFor;
}
Expand Down
8 changes: 6 additions & 2 deletions tests/dsh-native.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading