Repository navigation
fix(pid-monitor): resolve the process tree on Windows - #691
Merged
Merged
Conversation
`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.
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pid-monitor.tsresolves the AI tool's PID for the dashboard's process-liveness monitor. Both of its readers are POSIX-only, so on Windows they returnedundefinedfor every PID andresolveMonitorPid()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 whatpid-monitorcan see from where it runs:resolvedMonitorPidwas 51136 — thebashbetween the tool and the hook; it is now 38564, the PIDPROBE_TOOLprinted.getParentPid(process.pid)undefined48876(==process.ppid)getProcessComm(process.pid)undefinednode.exeresolveMonitorPid(hookPpid)51136— the launcher38564— the toolRoot cause
/procdoes not exist on Windows —fs.readFileSync('/proc/<pid>/comm')resolves toC:\proc\...and throwsENOENT— and the MSYSpsthat Git Bash ships rejects the BSD flag the fallback uses:So
getParentPid()andgetProcessComm()returnedundefinedfor any input,resolveMonitorPid()hitif (!parent || parent <= 1) breakon its first iteration, andbest— initialised tohookPpid— was returned unchanged. Deciding a shell fromcommnever happened either, sincecommwas alwaysundefined.Why it matters
dashboard.ts:119is the only consumer of that value:and a
process_exitevent is the dashboard's real "session ended" signal:dashboard-collector.ts:1306setsstatus = 'stopped'andstoppedAt = 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 reportsalive_self: truein both runs.What changed
Get-CimInstance Win32_Processquery 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. (wmicwould be 0.47s but is gone from Windows 11 24H2 on;tasklistis 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 -ounchanged elsewhere — so no POSIX call site or reader changed.normalizeComm()drops a.exesuffix and lowercases, because Windows reportsbash.exewhere the skip list storesbash.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 tosrc/utils/powershell.ts. It is the path resolutionhook-dispatch-cli.tsalready relies on — a hook's environment need not carry a usable PATH, or evenSystemRoot— and the new caller needs the same answer, so it is shared rather than copied.Type of Change
Test Plan
npx tsc --noEmitpassesnpm run buildpassesnpx 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)pid-monitor.test.tsentries 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.Three assertions in
src/__tests__/pid-monitor.test.tsfailed on Windows before this change and pass after it, without being weakened:getProcessCommreturns a non-empty string for the current process,getParentPidreturns a number for it, andgetParentPidmatchesprocess.ppid. (Two further assertions in the file already passed on Windows —undefinedfor a non-existent PID — and still do; this change does not touch them.) The fourth failing entry,isProcessAlive(1), becameskipIf(win32): PID 1 is init/systemd, Windows has no PID 1, sokill(1, 0)is legitimatelyESRCHthere. 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/parseWindowsProcessTabletests 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.mdupdate 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 thepsone cannot stand in on Windows.Notes for Reviewers
hook-handlers.ts:547) with a 4.5s budget. If you would rather not pay that inline, the alternative is marking the session-start dashboard handlerbackground— thestopevent already runs that handler that way at:563— but that changes when the host's first event lands, which is a separate decision and I did not want to fold it into a bug fix.ubuntu-latest/macos-latestonly. I ran the whole Windows suite to check this change against it: 78 of 3615 tests fail on Windows today, four of them are these, and this change introduces none. Awindows-latestcell is a far bigger piece of work than this PR — happy to draft it separately if that is welcome.getParentPid/getProcessCommeach pay their own query on Windows, since they query one PID. Nothing in the shipped code calls them in a loop — onlyresolveMonitorPid(), which passes the table in — so I left it at one query per call rather than adding a cache to a monitoring path.