CodeNomad/.opencode
Pascal André 226e862270
fix(process-tree): never adopt an orphan through its dead parent's reused PID (#1552)
## Problem

A process keeps the numeric ID of a parent that exited, and Windows
reuses that ID (Raymond Chen, [*Why is CreateToolhelp32Snapshot
returning incorrect parent process
IDs*](https://devblogs.microsoft.com/oldnewthing/20150403-00/?p=44313)).
A tree walk over parent IDs alone adopts unrelated processes. An
application an agent launched through a launcher that exited names the
launcher's PID; once a process being stopped receives that PID, the
application and its children are terminated with it. Observed: a Houdini
session died with exit code 1 (`TerminateProcess(…, 1)`), its `cmd`
parent 0.2 s earlier, two seconds after two `powershell.exe` started.

- `processTreeFromRows` (`electron/main/process-stop.ts`) walked parent
IDs only. The native and CIM Windows captures already filter children
older than their parent, but the shared walk relied on that, and the
Linux `/proc` scan and macOS `ps` listing are not atomic.
- `taskkill /T` walks recorded parent IDs the same way (see e.g.
openai/codex#36258, #58999). It was used by the server's foreign
OpenCode `--version` probe on timeout, by
`ui/src/lib/server-events-lifecycle.test.ts` and by two Windows desktop
fixtures.

## Change

- `packages/server/src/process-tree.ts` (Node built-ins only, shared by
Electron main, the server, tests and tsx-loaded scripts):
- `processTreeRows(rootPid, rows)`: a row is a child only when it
started **no earlier** than the process holding its parent ID; unknown
start times are left out; the first row of a PID wins.
- `stopWindowsProcessTree(rootPid, spawnedBy)`: one CIM listing, the
same walk, refusal when the root ID was reused after the spawn, then
`Stop-Process` through `Process` objects whose creation time is checked
after their handle is bound.
- Electron's `processTreeFromRows` uses `processTreeRows` with Windows
creation ticks, Linux boot clock ticks and macOS `lstart` seconds
(same-second children are kept).
- The foreign probe, the UI test and both desktop fixtures use
`stopWindowsProcessTree` instead of `taskkill /T`.

Checked and unchanged: Tauri (Windows Job Object with kill-on-close,
POSIX process groups: membership inherited at creation, no walk),
Electron POSIX `kill(-pid)` in `shell-environment.ts` (process group),
Electron force termination (already checks each member's creation
identity before `TerminateProcess`).

## Tests

- `process-stop.test.ts`: a recycled-parent table on Windows, Linux and
macOS leaves the orphan and its child out of the capture. **Fails on
`dev`** (`[100, 300, 301, 200, 201]` instead of `[100, 200, 201]`),
passes here; the 34 other tests pass.
- `server/src/process-tree.test.ts`: recycled parent, a cycle through a
recycled ID, unknown start times, duplicate rows, a missing root, reused
root ID refusal, CIM row parsing, a real Windows wrapper and its
descendant stopped.
- `legacy-installation.test.ts` (foreign probe kills the wrapper's
descendant on timeout) and `server-events-lifecycle.test.ts` pass;
server, Electron and UI `tsc` pass.

## Docs

`AGENTS.md` (never `taskkill /T` or a parent-ID-only walk), the desktop
conventions reference and `OPENCODE_RUNTIME_SETUP.md`. No user-visible
behaviour, so no help change.
2026-11-16 19:03:30 +01:00
..
agent Improve message stream caching and virtualization for large sessions 2025-11-26 13:30:20 +00:00
commands Use never release notes format 2026-05-14 10:38:03 +01:00
skills fix(process-tree): never adopt an orphan through its dead parent's reused PID (#1552) 2026-11-16 19:03:30 +01:00