Commit 24a070d
fix(session-log): walk ancestor PIDs to resolve correct session log (#598)
* fix(session-log): walk ancestor PIDs to find Claude Code session metadata
When plannotator is invoked from a slash command's `!` bang, the direct
parent process is an intermediate bash shell spawned by the Bash tool —
not Claude Code itself. The old resolveSessionLogByPpid() only checked
process.ppid, so it always missed the session metadata file and fell back
to mtime-based selection, which picks the wrong log when multiple sessions
exist for the same project.
New resolution ladder (four tiers):
1. Ancestor-PID walk: call `ps -o ppid=` repeatedly from process.ppid up
to 8 hops, checking ~/.claude/sessions/<pid>.json at each hop.
Deterministic — no guessing, matches exact session every time.
2. Cwd-scan: read every ~/.claude/sessions/*.json, filter by cwd, pick
most recent startedAt. Handles cases where ps is unavailable.
3. CWD slug mtime (legacy): existing behavior, fragile with multiple
sessions.
4. Ancestor directory walk: handles cd-deeper-into-subdirectory cases.
Adds getAncestorPids (injectable getParent for testing), resolveSessionLogByAncestorPids,
and resolveSessionLogByCwdScan. Exports SessionMetadata and accepts
projectsDirOverride on findSessionLogsForCwd for test isolation.
17 new tests cover: edge cases for getAncestorPids (cycles, maxHops,
self-loops), resolveSessionLogByAncestorPids (finds correct session among
multiple, skips missing logs, falls back when no metadata matches), and
resolveSessionLogByCwdScan (picks newest startedAt, ignores mismatched
cwd, handles missing sessions dir).
Closes #458
* refactor(session-log): simplify resolver and findSessionLogs for clarity
* fix(session-log): add Windows support to ancestor-PID resolver
Tier 1 used `ps`, which doesn't exist on Windows. Multiple Claude Code
sessions in the same repo remained broken on Windows because tier 2
(cwd scan) can't disambiguate when both sessions share the same cwd.
- Replace per-hop `ps` calls with a single process-table snapshot (`ps`
on Unix, PowerShell Get-CimInstance on Windows), cached across the
walk. One spawn instead of up to eight; faster on both platforms.
- Export pure-function parsers (parseProcessTablePs,
parseProcessTableCsv) so the Windows path is unit-testable without
a Windows runner.
- Normalize cwd comparison in resolveSessionLogByCwdScan: Windows is
case-insensitive and processes may report drive letters in either
case, so fold slashes and lowercase before comparing.
Windows viability confirmed empirically: ~/.claude/sessions/<pid>.json
exists with the expected schema on Windows 11 + Claude Code 2.1.116.
For provenance purposes, this commit was AI assisted.
---------
Co-authored-by: Michael Ramos <mdramos8@gmail.com>1 parent 53d2246 commit 24a070d
3 files changed
Lines changed: 681 additions & 34 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
84 | | - | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
85 | 92 | | |
86 | 93 | | |
87 | 94 | | |
| |||
652 | 659 | | |
653 | 660 | | |
654 | 661 | | |
655 | | - | |
656 | | - | |
657 | | - | |
658 | | - | |
659 | | - | |
660 | | - | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
661 | 674 | | |
662 | 675 | | |
663 | 676 | | |
| |||
677 | 690 | | |
678 | 691 | | |
679 | 692 | | |
680 | | - | |
681 | | - | |
682 | | - | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
683 | 700 | | |
684 | | - | |
685 | | - | |
| 701 | + | |
| 702 | + | |
686 | 703 | | |
687 | | - | |
688 | | - | |
| 704 | + | |
| 705 | + | |
689 | 706 | | |
690 | 707 | | |
691 | 708 | | |
| |||
0 commit comments