Skip to content

Commit f928e74

Browse files
committed
test(web): add a live daemon-shutdown lane proving zero owned Chrome processes
The unit tests added for #1868 mock the agent-browser CLI call, so they prove teardownSessionResources dispatches `agent-browser close`, not that the managed Chrome fleet actually terminates. Add a second live-E2E scenario to smoke-web-platform.test.ts (same AGENT_DEVICE_WEB_E2E=1 gate as the existing smoke test) that opens a real managed web session, sends the daemon process a real SIGTERM, and asserts zero owned Chrome processes remain within a bounded settle window — kept well under agent-browser's 5-minute idle timer default (left unmodified, unlike the functional smoke test's shortened override) so a pass can only mean the daemon's shutdown teardown actively closed the browser, not that the idle timer coincidentally beat the poll deadline. Also runs the #1781 B1 daemon leak oracle against the same shutdown, wiring it to a web lane as the original issue asked for. I could not fully execute this test in the local sandbox: agent-browser's install step unconditionally fetches Chrome-for-Testing from googlechromelabs.github.io, which this sandbox's network policy blocks (confirmed via the proxy status endpoint, not assumed) even after installing Node 24 and pointing AGENT_BROWSER_EXECUTABLE_PATH at the sandbox's pre-installed Chromium. The repo's own CI already runs AGENT_DEVICE_WEB_E2E=1 with real network access (.github/workflows/ci.yml, "Execute live web smoke" step), which is where this new scenario will actually execute and get validated. Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_011ot5wg8YUEyWRrphs8bMSJ
1 parent 911ce15 commit f928e74

1 file changed

Lines changed: 105 additions & 1 deletion

File tree

‎test/integration/smoke-web-platform.test.ts‎

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import assert from 'node:assert/strict';
2-
import { mkdirSync, writeFileSync } from 'node:fs';
2+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
33
import { createServer, type Server } from 'node:http';
44
import path from 'node:path';
55
import test from 'node:test';
66
import { type CliJsonResult, formatResultDebug, runBuiltCliJson } from './cli-json.ts';
77
import { assertPngDimensions, assertPngFile } from './provider-scenarios/assertions.ts';
88
import { runCleanupWithCoverageReport } from './web-e2e/coverage-report.ts';
9+
import { assertNoDaemonLeaks } from './support/daemon-leak-oracle.ts';
10+
import {
11+
inspectManagedAgentBrowserProcesses,
12+
type AgentBrowserProcessSummary,
13+
} from '../../src/platforms/web/agent-browser-lifecycle.ts';
14+
import { getManagedAgentBrowserStatus } from '../../src/platforms/web/agent-browser-tool.ts';
15+
import { isProcessAlive } from '../../src/utils/host-process.ts';
916

1017
const TEST_NAME = 'live web platform e2e smoke';
18+
const SHUTDOWN_TEST_NAME = 'live web platform e2e daemon-shutdown browser cleanup';
1119
const WEB_E2E_ENABLED = process.env.AGENT_DEVICE_WEB_E2E === '1';
20+
// #1868: SIGTERM must close the managed Chrome fleet well before agent-browser's own idle timer
21+
// (DEFAULT_AGENT_BROWSER_IDLE_TIMEOUT_MS, 5 minutes) would have. Left at its production default
22+
// here (unlike the functional smoke test's shortened override below) so a pass can only mean the
23+
// daemon's shutdown teardown actively closed the browser, not that the idle timer coincidentally
24+
// beat this test's own poll deadline.
25+
const WEB_SHUTDOWN_SETTLE_TIMEOUT_MS = 45_000;
26+
const WEB_SHUTDOWN_SETTLE_POLL_MS = 500;
1227

1328
type StepRecord = {
1429
step: string;
@@ -42,6 +57,95 @@ test(
4257
},
4358
);
4459

60+
// #1868: teardownSessionResources gained a best-effort web-close step so a daemon shutdown (or
61+
// an expired-session reap) tells agent-browser to close its Chrome fleet immediately instead of
62+
// leaving it for agent-browser's own multi-minute idle timer. The unit tests around
63+
// teardownSessionResources and teardownDaemonSessionForShutdown mock the agent-browser CLI call
64+
// and prove only that it gets dispatched with the right arguments; this live lane is the one
65+
// place that proves the whole chain — a real managed browser, a real daemon process, a real
66+
// SIGTERM — actually ends with zero owned Chrome processes, not just an issued close command.
67+
test(
68+
SHUTDOWN_TEST_NAME,
69+
{
70+
skip: WEB_E2E_ENABLED
71+
? false
72+
: 'Set AGENT_DEVICE_WEB_E2E=1 to run the managed web backend smoke.',
73+
},
74+
async () => {
75+
await runWebShutdownSmoke(await createWebSmokeContext());
76+
},
77+
);
78+
79+
async function runWebShutdownSmoke(context: WebSmokeContext): Promise<void> {
80+
try {
81+
await runStep(context, 'set up managed web backend', ['web', 'setup', '--json']);
82+
await runStep(context, 'open local fixture', ['open', context.url, ...context.common]);
83+
84+
const stateDir = context.env.AGENT_DEVICE_STATE_DIR;
85+
assert.ok(stateDir, 'expected the smoke context to configure a state dir');
86+
const status = getManagedAgentBrowserStatus({ stateDir });
87+
88+
const before = await inspectManagedAgentBrowserProcesses(status);
89+
assert.ok(
90+
before.count > 0,
91+
`expected the managed browser fleet to be running after open, found none: ${formatProcessSummary(before)}`,
92+
);
93+
94+
const daemonPid = readDaemonPid(stateDir);
95+
assert.equal(isProcessAlive(daemonPid), true, 'expected a live daemon before SIGTERM');
96+
97+
// The scenario #1868 is about: SIGTERM the daemon directly, the way an operator or an
98+
// orchestrator shutting the container down would, rather than going through `close`.
99+
process.kill(daemonPid, 'SIGTERM');
100+
101+
const after = await settleManagedBrowserProcesses(status);
102+
assert.equal(
103+
after.count,
104+
0,
105+
`expected zero owned Chrome processes after daemon shutdown, found: ${formatProcessSummary(after)}`,
106+
);
107+
assert.equal(
108+
isProcessAlive(daemonPid),
109+
false,
110+
'expected the daemon process itself to have exited after SIGTERM',
111+
);
112+
113+
// #1781 B1: the daemon leak oracle this fix unblocks wiring to a web lane — no stray
114+
// state-dir residue either, on top of the process-level proof above.
115+
await assertNoDaemonLeaks({ stateDir, daemonPids: [daemonPid], phase: 'after-shutdown' });
116+
} finally {
117+
await closeServer(context.server);
118+
}
119+
}
120+
121+
function readDaemonPid(stateDir: string): number {
122+
const info = JSON.parse(readFileSync(path.join(stateDir, 'daemon.json'), 'utf8')) as {
123+
pid?: number;
124+
};
125+
assert.equal(typeof info.pid, 'number', `daemon.json has no pid: ${JSON.stringify(info)}`);
126+
return info.pid as number;
127+
}
128+
129+
async function settleManagedBrowserProcesses(
130+
status: Parameters<typeof inspectManagedAgentBrowserProcesses>[0],
131+
): Promise<AgentBrowserProcessSummary> {
132+
const deadline = Date.now() + WEB_SHUTDOWN_SETTLE_TIMEOUT_MS;
133+
let summary = await inspectManagedAgentBrowserProcesses(status);
134+
while (summary.count > 0 && Date.now() < deadline) {
135+
await new Promise((resolve) => setTimeout(resolve, WEB_SHUTDOWN_SETTLE_POLL_MS));
136+
summary = await inspectManagedAgentBrowserProcesses(status);
137+
}
138+
return summary;
139+
}
140+
141+
function formatProcessSummary(summary: AgentBrowserProcessSummary): string {
142+
return JSON.stringify({
143+
count: summary.count,
144+
pids: summary.pids,
145+
reasons: summary.processes.map((match) => match.reason),
146+
});
147+
}
148+
45149
async function runWebSmoke(context: WebSmokeContext): Promise<void> {
46150
let opened = false;
47151

0 commit comments

Comments
 (0)