Skip to content

fix(pid-monitor): resolve the process tree on Windows - #691

Merged
jeff-r2026 merged 1 commit into
Tencent:mainfrom
yimi528:fix/pid-monitor-windows
Sep 21, 2026
Merged

jeff-r2026 merged 1 commit into
Tencent:mainfrom
yimi528:fix/pid-monitor-windows

Conversation

@yimi528

@yimi528 yimi528 commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

pid-monitor.ts resolves the AI tool's PID for the dashboard's process-liveness monitor. Both of its readers are POSIX-only, so on Windows they returned undefined for every PID and resolveMonitorPid() fell out of its walk on the first level, handing back the hook's own parent — the launcher, not the tool.

Measured on Windows 11 Home (build 22631) / Node v24.11.0 unless stated, chain tool (node) → bash.exe → hook (node), which is the shape a Windows hook launch produces. The hook prints what pid-monitor can see from where it runs:

======== before (origin/main) ========
PROBE_TOOL {"toolPid":14632}
PROBE_SHELL 4388
PROBE_HOOK {"hookPid":49288,"hookPpid":51136,"resolvedMonitorPid":51136,"alive_self":true}
            ^ parentPid_of_self and comm_of_self absent — they are undefined

======== after ========
PROBE_TOOL {"toolPid":38564}
PROBE_SHELL 4394
PROBE_HOOK {"hookPid":43188,"hookPpid":48876,"parentPid_of_self":48876,
            "comm_of_self":"node.exe","resolvedMonitorPid":38564,"alive_self":true}

resolvedMonitorPid was 51136 — the bash between the tool and the hook; it is now 38564, the PID PROBE_TOOL printed.

before after
getParentPid(process.pid) undefined 48876 (== process.ppid)
getProcessComm(process.pid) undefined node.exe
resolveMonitorPid(hookPpid) 51136 — the launcher 38564 — the tool

Root cause

/proc does not exist on Windows — fs.readFileSync('/proc/<pid>/comm') resolves to C:\proc\... and throws ENOENT — and the MSYS ps that Git Bash ships rejects the BSD flag the fallback uses:

$ ps -o ppid= -p $$
ps: unknown option -- o
Try `ps --help' for more information.

$ ps --help
Usage: ps [-aefls] [-u UID] [-p PID]

So getParentPid() and getProcessComm() returned undefined for any input, resolveMonitorPid() hit if (!parent || parent <= 1) break on its first iteration, and best — initialised to hookPpid — was returned unchanged. Deciding a shell from comm never happened either, since comm was always undefined.

Why it matters

dashboard.ts:119 is the only consumer of that value:

if (!isProcessAlive(session.monitorPid)) { ... appendEvent(process_exit) ... }

and a process_exit event is the dashboard's real "session ended" signal: dashboard-collector.ts:1306 sets status = 'stopped' and stoppedAt = event.timestamp, and a stopped session is dropped from the list 30s later. A launcher exits with the hook, so on Windows that signal was tied to the launcher's lifetime — the window the module exists to cover was the one it could not see.

isProcessAlive() is not part of this: kill(pid, 0) already behaves on Windows, which is why the probe reports alive_self: true in both runs.

What changed

  • A Windows reader. One Get-CimInstance Win32_Process query returns the whole table. The walk needs up to five ancestors, so a per-pid PowerShell start would cost more than the whole answer — measured, best of three: full table 1.19s, a filtered single-PID query 1.25s, and walking five levels in PowerShell with one query per level 2.69s. (wmic would be 0.47s but is gone from Windows 11 24H2 on; tasklist is 0.57s and carries no PPID at all.)
  • walkToNonShell() is the existing loop, parameterised on two accessors. resolveMonitorPid() picks the readers — the table on Windows, /proc + ps -o unchanged elsewhere — so no POSIX call site or reader changed.
  • normalizeComm() drops a .exe suffix and lowercases, because Windows reports bash.exe where the skip list stores bash.
  • The query is bounded at 3s, ~2.4x the idle measurement (PowerShell's own startup is 0.59s of it). The session-start collector is a foreground hook with a 4.5s budget (hook-handlers.ts:547, FOREGROUND_HOOK_TIMEOUT_MS = 4_500), so the bound leaves room for the rest of the handler and a hung WMI provider degrades the walk to its fallback instead of delaying the host past its timeout.
  • windowsPowerShell() moves to src/utils/powershell.ts. It is the path resolution hook-dispatch-cli.ts already relies on — a hook's environment need not carry a usable PATH, or even SystemRoot — and the new caller needs the same answer, so it is shared rather than copied.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Test Plan

  • npx tsc --noEmit passes
  • npm run build passes
  • npx vitest run src/__tests__/pid-monitor.test.ts — 17 tests, 16 passed / 1 skipped on Windows (the skipped one is the POSIX-only PID 1 case)
  • The same file on Node 22.22.2 — a version the CI matrix pins — to rule out a Node-24-only result: 16 passed / 1 skipped
  • Full unit suite before/after on the same machine: 78 failed → 74 failed, the difference being the four pid-monitor.test.ts entries below (three fixed, one now skipped instead of failing). No test that passed before fails now, and no new failure appeared in the before/after set difference.
  • Real-machine record above (the part CI cannot run)

Three assertions in src/__tests__/pid-monitor.test.ts failed on Windows before this change and pass after it, without being weakened: getProcessComm returns a non-empty string for the current process, getParentPid returns a number for it, and getParentPid matches process.ppid. (Two further assertions in the file already passed on Windows — undefined for a non-existent PID — and still do; this change does not touch them.) The fourth failing entry, isProcessAlive(1), became skipIf(win32): PID 1 is init/systemd, Windows has no PID 1, so kill(1, 0) is legitimately ESRCH there. That expectation was POSIX-only rather than a gap in the port, and skipping it is the narrowest way to say so.

The new walkToNonShell / parseWindowsProcessTable tests run on every platform on purpose: CI has no Windows runner, so the parsing and the walk are the only parts of the Windows path a POSIX job can pin, and the real query is covered by the record above.

Docs

No user-facing CLI surface changed, so no README / *.zh-CN.md update is needed. The module header comment claimed "Cross-platform: tries Linux /proc first, falls back to ps(1)" — that claim was the misleading part, and it now names the three readers and says why the ps one cannot stand in on Windows.

Notes for Reviewers

`getParentPid()` and `getProcessComm()` read Linux /proc and fall back to
`ps -o`. Windows has neither: /proc does not exist, and the MSYS `ps` Git Bash
ships rejects `-o`. Both therefore returned undefined for every PID,
`resolveMonitorPid()` broke out of its walk on the first level, and the `best`
it returned was the hook's own parent — the launcher, not the AI tool.

`dashboard.ts` hands that value to `isProcessAlive()`, and a false answer
becomes a `process_exit` event, i.e. `status = 'stopped'`: on Windows the exit
signal was tied to the launcher's lifetime instead of the tool's.

Windows now reads the table from one Win32_Process query — the walk needs up to
five ancestors, so a per-pid PowerShell start would cost more than the whole
answer — and the walk itself moved into `walkToNonShell()`, leaving the POSIX
readers exactly as they were. `windowsPowerShell()` moves to
`src/utils/powershell.ts`, shared with the hook dispatcher that already needed
the same path resolution.
@jeff-r2026 jeff-r2026 self-assigned this Sep 21, 2026
@Tencent Tencent deleted a comment from github-actions Bot Sep 21, 2026
@jeff-r2026
jeff-r2026 merged commit 1f52967 into Tencent:main Sep 21, 2026
10 checks passed
kongdayan added a commit to kongdayan/teamai-cli that referenced this pull request Sep 21, 2026
Squash-rebased onto the latest upstream/main to resolve the PR's merge
conflict (main gained Tencent#693/Tencent#685/Tencent#694/Tencent#691/Tencent#681/Tencent#680/Tencent#666 since this branch
forked). This combines all commits from the PR into one, applied cleanly on
top of the new base — no functional changes from the previously reviewed
state.

The only real conflict was in src/__tests__/uninstall.test.ts, where diff3
split a test mid-body because of the repeated `});` boilerplate around it;
resolved by keeping both sides' new tests intact, in full.
jeff-r2026 pushed a commit that referenced this pull request Sep 23, 2026
* feat(pi): add Pi Coding Agent integration

Squash-rebased onto the latest upstream/main to resolve the PR's merge
conflict (main gained #693/#685/#694/#691/#681/#680/#666 since this branch
forked). This combines all commits from the PR into one, applied cleanly on
top of the new base — no functional changes from the previously reviewed
state.

The only real conflict was in src/__tests__/uninstall.test.ts, where diff3
split a test mid-body because of the repeated `});` boilerplate around it;
resolved by keeping both sides' new tests intact, in full.

* fix(pi): gate agent-hook files on the same per-slug marker ownership check

applyPiAgentHook()/removePiAgentHook() wrote and deleted teamai-agent-<slug>.ts
purely by path, with no ownership check — the same class of bug already
fixed for the main teamai-hooks.ts file, but never extended to the per-slug
HTTP agent-hook files. A user-authored file at that conventional path could
be silently overwritten on sync or deleted on uninstall.

Adds hasPiAgentHook(slug), mirroring hasPiHooks: injection now skips (with a
warning) instead of overwriting a same-named file without the
`[teamai] agent hook [<slug>]` marker, and removal skips instead of
deleting one. uninstall.ts's discovery scan now derives each file's slug and
checks the same marker before scheduling it for removal, instead of
matching by filename prefix alone.

* fix(pi): fail install_hook_rule instead of silently acking a skipped Pi agent hook

applyPiAgentHook warned and returned normally when the requested event has no
Pi equivalent or a same-named extension file exists without the TeamAI
marker. The caller in local-agent.ts wrote the manifest entry and acked
success regardless, so the server and local state believed the hook was
installed even though the file was never touched. Throw in both cases so the
existing install_hook_rule error path acks failure instead.

Also document the known limitation (shared with the OMP adapter) that a
scoped Pi uninstall is not durable across multiple projects on the same
machine, since the extension is one machine-wide file and hook dispatch has
no per-project exclusion check.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
`getParentPid()` and `getProcessComm()` read Linux /proc and fall back to
`ps -o`. Windows has neither: /proc does not exist, and the MSYS `ps` Git Bash
ships rejects `-o`. Both therefore returned undefined for every PID,
`resolveMonitorPid()` broke out of its walk on the first level, and the `best`
it returned was the hook's own parent — the launcher, not the AI tool.

`dashboard.ts` hands that value to `isProcessAlive()`, and a false answer
becomes a `process_exit` event, i.e. `status = 'stopped'`: on Windows the exit
signal was tied to the launcher's lifetime instead of the tool's.

Windows now reads the table from one Win32_Process query — the walk needs up to
five ancestors, so a per-pid PowerShell start would cost more than the whole
answer — and the walk itself moved into `walkToNonShell()`, leaving the POSIX
readers exactly as they were. `windowsPowerShell()` moves to
`src/utils/powershell.ts`, shared with the hook dispatcher that already needed
the same path resolution.
Walternater pushed a commit to Walternater/teamai-cli that referenced this pull request Nov 2, 2026
* feat(pi): add Pi Coding Agent integration

Squash-rebased onto the latest upstream/main to resolve the PR's merge
conflict (main gained Tencent#693/Tencent#685/Tencent#694/Tencent#691/Tencent#681/Tencent#680/Tencent#666 since this branch
forked). This combines all commits from the PR into one, applied cleanly on
top of the new base — no functional changes from the previously reviewed
state.

The only real conflict was in src/__tests__/uninstall.test.ts, where diff3
split a test mid-body because of the repeated `});` boilerplate around it;
resolved by keeping both sides' new tests intact, in full.

* fix(pi): gate agent-hook files on the same per-slug marker ownership check

applyPiAgentHook()/removePiAgentHook() wrote and deleted teamai-agent-<slug>.ts
purely by path, with no ownership check — the same class of bug already
fixed for the main teamai-hooks.ts file, but never extended to the per-slug
HTTP agent-hook files. A user-authored file at that conventional path could
be silently overwritten on sync or deleted on uninstall.

Adds hasPiAgentHook(slug), mirroring hasPiHooks: injection now skips (with a
warning) instead of overwriting a same-named file without the
`[teamai] agent hook [<slug>]` marker, and removal skips instead of
deleting one. uninstall.ts's discovery scan now derives each file's slug and
checks the same marker before scheduling it for removal, instead of
matching by filename prefix alone.

* fix(pi): fail install_hook_rule instead of silently acking a skipped Pi agent hook

applyPiAgentHook warned and returned normally when the requested event has no
Pi equivalent or a same-named extension file exists without the TeamAI
marker. The caller in local-agent.ts wrote the manifest entry and acked
success regardless, so the server and local state believed the hook was
installed even though the file was never touched. Throw in both cases so the
existing install_hook_rule error path acks failure instead.

Also document the known limitation (shared with the OMP adapter) that a
scoped Pi uninstall is not durable across multiple projects on the same
machine, since the extension is one machine-wide file and hook dispatch has
no per-project exclusion check.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants