Skip to content

The agent that grows with you

test(desktop): core e2e sandboxes stop the gateway they booted

Teknium7d1df0071e1a
diff --git a/apps/desktop/e2e/core/harness.ts b/apps/desktop/e2e/core/harness.ts
index 8cae46b5a20..e2e8911ec91 100644
--- a/apps/desktop/e2e/core/harness.ts
+++ b/apps/desktop/e2e/core/harness.ts
@@ -71,6 +71,10 @@ export function createCoreSandbox(label: string): CoreSandbox {
hermesHome,
userDataDir,
cleanup: () => {
+ // Attach-mode Desktop never stops the gateway it dialled, so without this
+ // every spec left its sandbox `gateway run` behind (~20 per suite run).
+ stopSandboxGatewayDaemons(root, hermesHome)
+
if (!process.env.HERMES_E2E_CORE_KEEP) {
fs.rmSync(root, { recursive: true, force: true })
}
@@ -78,6 +82,69 @@ export function createCoreSandbox(label: string): CoreSandbox {
}
}
+function sleepSync(ms: number): void {
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms)
+}
+
+function processHomeUnder(pid: number, root: string): boolean {
+ const info = readProc(pid)
+
+ return Boolean(info?.cmdline) && info!.environ.split('\0').some(entry => entry.startsWith(`HERMES_HOME=${root}`))
+}
+
+/**
+ * SIGTERM (then SIGKILL after 15 s) every daemon recorded in a sandbox
+ * `gateway.lock` (launch home and each profile home). The pid is only
+ * signalled while its environment still names this sandbox, so a recycled pid
+ * is never touched.
+ */
+function stopSandboxGatewayDaemons(root: string, hermesHome: string): void {
+ const profilesDir = path.join(hermesHome, 'profiles')
+
+ const homes = [
+ hermesHome,
+ ...(fs.existsSync(profilesDir) ? fs.readdirSync(profilesDir).map(name => path.join(profilesDir, name)) : [])
+ ]
+
+ const pids = new Set<number>()
+
+ for (const home of homes) {
+ try {
+ const pid = Number(JSON.parse(fs.readFileSync(path.join(home, 'gateway.lock'), 'utf8'))?.pid)
+
+ if (Number.isInteger(pid) && pid > 0 && processHomeUnder(pid, root)) {
+ pids.add(pid)
+ }
+ } catch {
+ // No lock (remote-only spec, never booted) or an empty one: nothing to stop.
+ }
+ }
+
+ const signal = (sig: NodeJS.Signals) => {
+ for (const pid of pids) {
+ try {
+ process.kill(pid, sig)
+ } catch {
+ pids.delete(pid)
+ }
+ }
+ }
+
+ signal('SIGTERM')
+
+ for (const deadline = Date.now() + 15_000; pids.size && Date.now() < deadline; ) {
+ sleepSync(100)
+
+ for (const pid of [...pids]) {
+ if (!processHomeUnder(pid, root)) {
+ pids.delete(pid)
+ }
+ }
+ }
+
+ signal('SIGKILL')
+}
+
/**
* Sandbox config: only the scripted provider. The external tirith scanner is
* off: with none on PATH the backend downloads it from GitHub on the first