Files
hermes-agent/apps/desktop/e2e/run-tmp.ts
Teknium 29d13bd134 fix(desktop-e2e): stop the gateways Playwright runs leave behind, including a killed run's
Desktop dials its backend through `hermes gateway ensure`, a detached
`gateway run` that outlives the app on purpose. The main suite's sandbox
cleanup only closed the app and removed the sandbox, so EVERY spec launch,
clean runs included, left its gateway running (~300 MB each), and the orphan
rewrote its sandbox HERMES_HOME right after the rmSync, so the run's pw-*
root reappeared. A SIGKILLed runner (agent/CI timeout) additionally left its
Electron main process.

Three layers, each covering a teardown the previous one cannot reach:
- per sandbox: the main suite's cleanup now stops the daemons its
  gateway.lock files record (the core lane's existing helper, moved to the
  shared e2e/run-tmp-reaper.ts; every platform);
- per run, at runner exit: every process whose environment names this run's
  root (a fixture that never returned, profile gateways off the lock homes);
- per host, at the next runner's start: every process left under a run root
  whose recorded runner (pid + kernel start time) is dead.
The run-level layers read /proc (Linux) and only signal a process carrying
both HERMES_E2E_RUN_TMP=<root> and a HERMES_HOME under that root; a root
whose runner is alive or unrecorded is never touched.
2026-10-25 07:53:33 -07:00

40 lines
1.6 KiB
TypeScript

/**
* One temp root per Playwright run, removed when the runner exits.
*
* Playwright (artifact and stack-trace dirs), the Electron app, its backend
* and every sandbox mkdtemp under os.tmpdir(), and a run that fails or times
* out in setup leaves all of it behind (a clean run of two specs left eight
* `playwright-tracing-*` dirs). Workers and the apps they launch inherit
* TMPDIR, so everything lands here; a SIGKILLed run leaves one dir, not dozens.
*
* The root also names the run's backends (`HERMES_E2E_RUN_TMP` in their
* environment): the exit hook stops them before removing it, and the next
* runner stops the ones a killed run left (./run-tmp-reaper). Canonical, so a
* backend's resolved HERMES_HOME still lies under it behind a symlinked tmpdir.
*
* Config modules load in the runner and again in each worker; only the first
* (runner) load creates the root.
*/
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { reapAbandonedRuns, reapRunBackends, recordRunOwner } from './run-tmp-reaper'
if (!process.env.HERMES_E2E_RUN_TMP) {
for (const abandoned of reapAbandonedRuns()) {
console.warn(`[e2e] stopped the backends a killed run left under ${abandoned}`)
}
const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), 'pw-')))
recordRunOwner(root)
process.env.HERMES_E2E_RUN_TMP = root
process.env.TMPDIR = root
process.env.TEMP = root
process.env.TMP = root
process.on('exit', () => {
reapRunBackends(candidate => candidate === root)
fs.rmSync(root, { recursive: true, force: true })
})
}