Skip to content

Fix Pi Codex session discovery - #8345

Open
Longado wants to merge 8 commits into
omacom:quattrofrom
Longado:codex/fix-pi-codex-session-path
Open

Longado wants to merge 8 commits into
omacom:quattrofrom
Longado:codex/fix-pi-codex-session-path

Conversation

@Longado

@Longado Longado commented Aug 26, 2026

Copy link
Copy Markdown

Problem

The Agents panel only scans the legacy Pi session directory, so current Pi sessions under ~/.config/pi are missing from Codex usage totals.

Change

  • Scan PI_CODING_AGENT_DIR/sessions, defaulting to ~/.config/pi/sessions, while retaining legacy Pi and OMP roots.
  • Normalize and deduplicate effective roots so aliases are counted once.
  • Include the effective roots in the cache identity and use one root snapshot for both cache selection and scanning.

Tests

  • Bash 5/Linux: bash test/shell.d/agent-usage-codex-scanner-test.sh
  • Bash 5/Linux: bash ./test/cli
  • git diff --check

Fixes #7194

Copilot AI balanced review requested due to automatic review settings August 26, 2026 07:39

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Codex usage discovery for current Pi session locations while preserving legacy roots.

Changes:

  • Adds normalized, deduplicated Pi session roots.
  • Includes effective roots in cache identity and scanning.
  • Expands scanner coverage for custom paths and aliases.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.

File Description
bin/omarchy-agent-usage-codex Adds Pi root discovery, normalization, and cache isolation.
test/shell.d/agent-usage-codex-scanner-test.sh Tests supported roots, aliases, custom paths, and caching.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
Pi documents PI_CODING_AGENT_DIR as defaulting to ~/.pi/agent and has never written to ~/.config/pi on its own; sessions land there only when the variable points there. Falling back to ~/.config/pi scanned a directory Pi does not use, and the test called it Pi's default. The test now sets the variable to that directory, the layout the report describes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Sep 28, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed and tested on a disposable Omarchy worker. The fix works, and I pushed one small correction to your branch.

Reproduction. A temporary home with a Codex session under $PI_CODING_AGENT_DIR/sessions counts 0 tokens on quattro, 43 on this branch, and 0 again with the new root taken out of pi_session_roots(). It is counted whether the variable points at ~/.config/pi (the layout in #7194) or at ~/.config/pi/agent. With the variable unset, ~/.pi/agent/sessions is still counted once, not twice.

Pushed: b04d6e5. When PI_CODING_AGENT_DIR was unset, the branch fell back to ~/.config/pi, and the test described that as Pi's current default. Pi documents the default as ~/.pi/agent (docs/environment-variables.md in Pi), and Pi only writes to ~/.config/pi when the variable points there. The fallback is now ~/.pi/agent, which deduplicates against the legacy root. The first Pi test now sets PI_CODING_AGENT_DIR to ~/.config/pi explicitly. Every other assertion in your test is unchanged.

Tests, on the worker at b04d6e5: agent-usage-codex-scanner-test.sh 28/28, the other four agent-usage-* suites all green, and ./test/cli green.

Proportionality. The collector change is the size the problem needs. The roots have to be deduplicated, or setting the variable to Pi's default would count every session twice. The cache key has to include the roots, or changing the variable would serve the old scan until the cache expires.

Not in this PR: scan_pi_usage() in bin/omarchy-agent-usage-claude hardcodes the same two roots, so an Anthropic subscription used through Pi with PI_CODING_AGENT_DIR set is still missing from the Claude tab, as the comment on #7194 points out. That belongs in its own pull request. Related open work on the same function: #9546 (Pi/OMP profile roots), #8602 (fork deduplication) and #13209 (--no-ignore when $HOME is a git checkout). #10330 is an unrequested enhancement that moves Pi into its own collector, and it also reads this variable. None of them is the same fix, but whichever lands second will conflict in pi_session_roots().

Second opinion: none. The configured reviewer (Codex Medium) answered a ping but refused this review because its daily budget was spent. This review is one model's reasoning (Claude Opus 5.5), with no second opinion. It waits on that second opinion at this head, then on the maintainer.

@omarchybot omarchybot removed the verified Omarchy Triage has verified that this issue is ready for final review label Oct 3, 2026
omarchybot and others added 3 commits October 3, 2026 04:54
quattro now finds Pi and omp profile roots and keeps a per-file cache for native sessions, which both touch the code this branch changed. PI_CODING_AGENT_DIR becomes one more root beside the profiles, added only when set. Aliased roots are deduplicated by their resolved path but scanned under the path as given, because parentSession links name sessions by the path pi used and fork deduplication matches on it. The pi roots are hashed into the aggregate scan cache key only, so the per-file cache of native sessions survives a change of the variable.

The variable's tests move to a fixture home of their own, so quattro's fork, profile and collision fixtures keep their totals.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pi expands only a leading ~ or ~/ in PI_CODING_AGENT_DIR and leaves ~name as a literal relative directory, so expanduser() sent ~name to another user's home or dropped it. And when the variable aliases a default root, Pi names a fork's parent by the variable's spelling: scanning the default spelling instead missed the parent and counted the fork as its own session. The variable's root now comes first, so deduplication keeps its spelling.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
A PI_CODING_AGENT_DIR that aliases a default root is scanned under one spelling, while a fork's parentSession carries whichever spelling Pi used when it was written, which differs for sessions forked before the variable was set. Comparing the strings left those forks without a parent, so each counted as a session of its own. Session paths and parent references are both resolved now, so either spelling finds the parent.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
@omarchybot omarchybot added the verified Omarchy Triage has verified that this issue is ready for final review label Oct 3, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

This branch had started conflicting with quattro, so I merged quattro into it and pushed three commits (head is now d6df764). Codex Medium reviewed it this time, over three rounds; the last round, on d6df764, found nothing.

The merge (7da230e). quattro has since gained Pi and omp profile roots (<base>/profiles/<name>/agent/sessions) and a per-file cache for native Codex sessions, and both touch the functions this branch changed. $PI_CODING_AGENT_DIR/sessions is now one more root alongside the default and profile roots, added only when the variable is set. Aliased roots are scanned once. The Pi roots are hashed into the key of the aggregate scan cache, so changing the variable triggers a rescan. They stay out of the per-file cache key, because that cache holds only native Codex sessions. Your tests for the variable now live in a fixture home of their own, so the totals in quattro's fork, profile and ID-collision fixtures are unchanged.

Two fixes from the second opinion:

  • ~name (5358692). Pi expands only ~ and ~/ in PI_CODING_AGENT_DIR (normalizePath in Pi's utils/paths.ts) and treats ~name as a literal relative directory. Python's expanduser() instead looked up a user called name, which reads another account's home if that user exists and drops the root if not. The collector now expands the same two forms Pi does. The test now checks that sessions in a literal ~name directory are counted, rather than checking that the root is skipped.
  • Forks under an aliased root (d6df764). If PI_CODING_AGENT_DIR is a symlink to ~/.pi/agent, a fork's parentSession can use either spelling, depending on whether the variable was set when the fork was written. The parent lookup compared path strings, so a fork recorded under the other spelling did not find its parent and was counted as a session of its own. Session paths and parentSession are now both resolved before they are compared. The test writes forks with both spellings and expects one session.

Reproduction, on a disposable Omarchy worker. With PI_CODING_AGENT_DIR pointing at a directory holding a Codex session, the collector counts 19 tokens on quattro (only the default root), 62 at d6df764, and 19 again with the new root disabled. The alias test fails on the commit before d6df764.

Tests, on the worker at d6df764: agent-usage-codex-scanner-test.sh 61/61, and every other agent-usage-* and agent-account* suite green. ./test/cli stops at vscode generated theme references current theme file, and it stops at the same test, after the same 91 passes, on unmodified quattro on the same worker. That failure is not caused by this PR, but it means the suite was not fully green, so this is verified but not yet marked ready.

Reviewed by Claude Opus 5.5, with Codex Medium as the second opinion. Its two findings above went beyond my own review; its last round was clean. It waits on the maintainer.

@greptile-apps

greptile-apps Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no outstanding finding or new actionable issue was identified.

Summary

The PR discovers sessions under PI_CODING_AGENT_DIR, deduplicates aliased Pi roots, and keys cached scans to their resolved locations.

  • The latest change adds a regression test for repointing a symlinked CODEX_HOME when per-file cache records could otherwise appear reusable.

Reviews (3) · Last reviewed commit: "test: cover per-file cache after CODEX_H..." · Reviewed by Greptile

Comment thread bin/omarchy-agent-usage-codex Resolved
A symlinked PI_CODING_AGENT_DIR or CODEX_HOME that was repointed kept its own
spelling, so the cache key stayed the same and the old directory's totals were
reused. Key by the resolved path instead.
@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge and removed verified Omarchy Triage has verified that this issue is ready for final review labels Oct 3, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed your two new commits (f04d577 and 22fa2f4) on a disposable Omarchy worker. Nothing found, and nothing pushed.

What they fix. The aggregate scan cache was keyed by the spelling of PI_CODING_AGENT_DIR and CODEX_HOME, so repointing a symlink kept the same key and served the old directory's totals until the cache expired. Keying by the resolved path fixes that. Because scan_cache_digest() also names the per-file cache, a repointed CODEX_HOME no longer reuses records written for the old directory either. For anyone whose paths were not already canonical, the key changes once and the next refresh rescans.

Reproduction. Your new tests pass at 22fa2f4. They fail at d6df764, the head before your commits, and they fail again with either os.path.realpath call taken back out of the cache keys, each time at the matching symlink test. The original fix still holds: on quattro, a session under PI_CODING_AGENT_DIR is still not counted.

Tests, on the worker. At 22fa2f4, agent-usage-codex-scanner-test.sh passes 63/63. This branch is 89 commits behind quattro, and on its own ./test/cli stops at vscode generated theme references current theme file, a test quattro has since fixed. Merged onto quattro (982290f), ./test/cli passes 117/117, and every agent-usage-* and agent-account* suite is green.

Second opinion. Reviewed by Claude Opus 5.5, with Codex Medium as the second opinion. It found no defects and agreed that the change is correct and the right size. Its independence is not guaranteed. It points out that the tests prove the aggregate cache is invalidated, but not the per-file cache. That is a gap in coverage, not a defect.

This waits on the maintainer.

@Longado

Longado commented Oct 9, 2026

Copy link
Copy Markdown
Author

Added a test (04750c7) for the per-file cache gap mentioned in the last review: with the aggregate cache dropped, repointing CODEX_HOME to a directory whose session file has the same size and mtime no longer replays the old directory's per-file records. It fails if the per-file cache name stops depending on the resolved CODEX_HOME; the existing symlink test still passes in that case, so it did not cover this. Test-only change.

Disclosure: drafted with an AI assistant, reviewed and posted by me.

@omarchybot omarchybot removed verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Oct 9, 2026
@omarchybot omarchybot added verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Oct 9, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Re-reviewed the new commit, 04750c7 (test: cover per-file cache after CODEX_HOME symlink is repointed). Nothing found.

The new test does what it says. Both fixture sessions are the same size, touch -r gives them the same mtime, and they share one path through the link, so a per-file cache keyed by the unresolved CODEX_HOME would replay the first home's totals. I checked that on a worker: with only file_cache_path changed to key on the unresolved path, exactly this test fails and every other test in the file passes. With the head merged onto current quattro (c352b62), the Codex scanner suite passes 64/64, ./test/cli passes 117/117, and every agent-usage-* and agent-account-* shell suite is green, all on a worker.

Codex Medium reviewed the commit as a second opinion and agreed that it is sound and proportionate; it ran with access to this session's notes, so its independence is not guaranteed. It noted that a run crossing midnight between creating the fixtures and touch -r could make the test pass without testing anything. The rest of this file builds its fixture paths with date the same way, so I left that as it is.

This is waiting on the maintainer.

@omarchybot omarchybot removed ready Good to merge verified Omarchy Triage has verified that this issue is ready for final review labels Oct 25, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator

Re-checked after quattro moved. This pull request now conflicts, and quattro already fixes #7194 another way, so I have not ported it.

What happened on quattro. #14040 (80e9460, merged 24 October) moved Pi session discovery out of bin/omarchy-agent-usage-codex into the shared shell/plugins/agents/collectors.py. Its pi_session_roots() reads PI_CODING_AGENT_DIR/sessions (expanding ~) in addition to ~/.pi and ~/.omp and their profiles. It also reads PI_CODING_AGENT_SESSION_DIR and a sessionDir in settings.json. canonical_roots() deduplicates roots by realpath. Codex's scan cache is keyed by those resolved roots (scan_sources() in bin/omarchy-agent-usage-codex). The Claude collector uses the same function, so the gap in the Claude tab mentioned on #7194 is closed too. Porting this branch would mean rewriting it against the shared collector, and nothing in it is needed to fix #7194.

Checked on a disposable Omarchy worker. I ran a reproduction using the layout from #7194, with a Pi session under $PI_CODING_AGENT_DIR/sessions next to one in ~/.pi/agent/sessions. On current quattro (d367a56), all five cases pass:

  • with the variable set to $HOME/.config/pi, both sessions are counted
  • with it set to a literal ~/.config/pi, both are still counted
  • an alias of the default tree is counted once
  • changing the variable rescans instead of serving the cached scan
  • repointing a symlink the variable names also rescans

On this pull request's old base (a85e29a), four of the five fail: the moved session is never counted. On quattro, the shared, Pi-scan, partition and Codex scanner suites and ./test/cli all pass on the worker.

What this branch does that quattro doesn't. None of these is a bug in using Pi as the issue describes:

  • Cache keys use the unresolved path. quattro builds its cache keys from CODEX_HOME and the opencode database path without resolving symlinks. After a symlinked CODEX_HOME is repointed, the aggregate scan can serve the old totals for up to 20 seconds (900 with --limits-only). The per-file rollout cache can replay an old record only when the new home holds a file at the same relative path with the same size and mtime.
  • ~name is treated as a user's home. quattro passes the variable through Path.expanduser(), so PI_CODING_AGENT_DIR=~name becomes that user's home. Pi treats it as a literal directory. If no user has that name, expanduser() raises RuntimeError. scan_sources() runs before the cache's error handling, so that would stop the Codex collector. This is a small bug in the shared collector and belongs in a pull request of its own.

Second opinion. Codex Medium and muse-wonder via pi (thinking high) both reviewed this. Both agreed that quattro already fixes #7194 and that there is no reason to port the branch. Both reviewers could read this session's notes, so their agreement is not guaranteed to be independent. Codex Medium found the ~name crash, and I confirmed it in the source. It also rated the per-file cache replay as a real but potentially lasting edge case; muse-wonder considered it too contrived to matter.

This is waiting on the maintainer. My recommendation is to close it as already fixed by #14040, and #7194 with it. Thanks @Longado for the report and the fix, and for the symlink cases your tests found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Agents panel misses Codex usage from Pi sessions stored under ~/.config/pi

3 participants