Skip to content

Count pi usage from PI_CODING_AGENT_DIR and PI_CODING_AGENT_SESSION_DIR - #14550

Open
omarchybot wants to merge 1 commit into
quattrofrom
fix/issue-7194
Open

omarchybot wants to merge 1 commit into
quattrofrom
fix/issue-7194

Conversation

@omarchybot

Copy link
Copy Markdown
Collaborator

The Agents panel drops Codex and Claude usage from pi whenever pi keeps its sessions anywhere but ~/.pi/agent/sessions. pi_session_roots() in both bin/omarchy-agent-usage-codex and bin/omarchy-agent-usage-claude builds its roots from ~/.pi and ~/.omp alone, so it never looks where pi actually writes once PI_CODING_AGENT_DIR (pi's agent directory, sessions under <dir>/sessions) or PI_CODING_AGENT_SESSION_DIR (the session directory itself) is set. The Fireworks collector already honours PI_CODING_AGENT_DIR for pi's auth.json; the two session scanners did not.

Both collectors now add those two roots, expanding a leading ~ as pi does. Every root is canonicalised before scanning, and the Codex collector resolves session paths and parentSession through realpath, so an override that reaches a default root through a symlink is scanned under one spelling: its usage is counted once, and a fork whose parent is spelled through the symlink still inherits from it rather than counting as a session of its own.

The collectors read the environment they are started with, which for the panel is the desktop session's. A variable exported only in a shell rc reaches omarchy-agent-usage-update run from a terminal but not the panel; set it in a file under ~/.config/uwsm/env.d/ to reach both. pi's sessionDir setting in settings.json is not read here: it can live in project settings and take relative paths, which needs its own discovery rules.

The scanner tests now unset both variables so a developer's own pi sessions never leak into a run, and cover both overrides, a literal ~, and a symlinked override reaching the default root, for each collector.

Fixes #7194

Reviewed by Claude Opus 5.5 and Codex Medium.

Both collectors scanned only ~/.pi and ~/.omp, so a pi whose agent tree or sessions were moved by its documented overrides had its Codex and Claude usage dropped from the Agents panel. Roots are canonicalised, with Codex session and parentSession paths through realpath, so an override aliasing a default root through a symlink is counted once and forks still find their parent.

Fixes #7194

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
@greptile-apps

greptile-apps Bot commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Expands agent usage scanner to read environment variable overrides.

The PR appears safe to merge.

What we checked:

  • Sessions counted twice: Both collectors skip repeated messages across roots. Their session counts also use the same path for files reached through the tested directory alias.
  • Fork loses its parent: Codex resolves both the scanned file path and parentSession through realpath. The parent lookup therefore uses the same spelling.

Summary

The Claude and Codex collectors now find pi sessions through PI_CODING_AGENT_DIR and PI_CODING_AGENT_SESSION_DIR.

  • Both expand ~ and resolve session roots.
  • Codex also resolves file paths and parentSession so symlink spellings agree.
  • Tests isolate the environment and cover custom directories, overlapping roots, and Codex fork ownership.

No actionable issues were found. Tests were inspected but not run.

Reviews (1) · Last reviewed commit: "Count pi usage from PI_CODING_AGENT_DIR ..." · Reviewed by Greptile

@jandrusk

jandrusk commented Oct 8, 2026

Copy link
Copy Markdown

Heads up: #8345 ("Fix Pi Codex session discovery") is still open for the same issue (#7194) and was updated yesterday. This PR also covers the Claude collector and the symlink/realpath dedupe, so it looks like the more complete of the two. It probably makes sense to consolidate on one, and fold anything from #8345 worth keeping into it.

dhh added a commit that referenced this pull request Oct 15, 2026
The Claude and Codex collectors scanned only ~/.pi and ~/.omp, so a pi whose agent tree or sessions were moved by its documented overrides had its usage dropped from the panel. Roots are canonicalised, with Codex session and parentSession paths through realpath, so an override aliasing a default root through a symlink is counted once and forks still find their parent. A root nested inside another is dropped, since the outer one already reads it, and the Codex scan cache is keyed by the overrides too.

Builds on #14550.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: Omarchybot <[email protected]>
@omarchybot omarchybot added bug Something isn't working verified Omarchy Triage has verified that this issue is ready for final review ready Good to merge labels Oct 16, 2026
@omarchybot

Copy link
Copy Markdown
Collaborator Author

Reviewed at 6773da1, and found nothing wrong.

Checked on a disposable Omarchy worker: the new override tests in test/shell.d/agent-usage-claude-scanner-test.sh and agent-usage-codex-scanner-test.sh fail with both collectors put back to their base versions (Claude counts 5 tokens and 1 session instead of 17 and 3; Codex counts 5 instead of 17). They pass at this head, and again with the head merged onto current quattro (51bfd1d), which has since changed the Codex scanner test and merges cleanly. ./test/cli passed on the worker. Read against pi's documentation: PI_CODING_AGENT_DIR moves the agent directory with sessions under <dir>/sessions and expands a leading ~, and PI_CODING_AGENT_SESSION_DIR sets the session directory itself, which is what both collectors now scan.

Codex Medium reviewed it as a second opinion, alongside Claude Opus 5.5, and found no defects. It checked overlapping and nested roots, symlinked aliases and fork-parent matching through realpath, and confirmed the expected counts in the tests. It agreed with the conclusion here; its independence is not guaranteed.

The fix closes #7194. It is labelled verified and ready, and waits 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 Author

Re-checked after quattro moved. This pull request now conflicts in both collectors, and quattro already fixes #7194 the same way, so I have not ported it. It no longer carries verified or ready.

What changed on quattro. #14040 (80e9460) moved pi session discovery out of the two collectors into the shared shell/plugins/agents/collectors.py. Both collectors now read their roots from it: bin/omarchy-agent-usage-claude at line 322 and bin/omarchy-agent-usage-codex at line 219. Everything this branch adds is now in the shared module:

  • PI_CODING_AGENT_DIR (with its /sessions) and PI_CODING_AGENT_SESSION_DIR are both read, and a leading ~ is expanded (pi_session_roots(), lines 658–668).
  • Roots are deduplicated by realpath, and a root nested inside another is dropped (canonical_roots(), lines 672–677).
  • A fork's parentSession is resolved through realpath (line 698), as are the scanned paths.
  • The resolved roots are part of the Codex scan cache key, so changing an override triggers a rescan.

quattro also reads pi and omp profile trees and an absolute sessionDir from pi's settings, which this branch did not.

Checked on a disposable Omarchy worker. I ran this pull request's two scenarios through both collectors on current quattro (bbfdfdd). Both pass. With PI_CODING_AGENT_DIR=~/.config/pi and PI_CODING_AGENT_SESSION_DIR set, both collectors count 17 tokens across 3 sessions. With a symlinked override that reaches the default root, and a fork whose parent is recorded through the symlink, they count 5 tokens and 1 session. With the two override lines removed from pi_session_roots(), the first scenario fails again in both collectors: only 5 tokens and 1 session are counted. On quattro, the worker also passed ./test/cli, the shared suite, the pi-scan suite and both scanner suites.

Second opinion. Codex Medium and muse-wonder via pi (thinking high) both reviewed this from the code. Both agreed that quattro already does everything this branch does, and that it should not be ported. They could read this session's notes, so their agreement is not guaranteed to be independent. Both found the same small gap: quattro's tests set the two variables only to absolute paths, so nothing tests the ~ expansion for them. The worker run above shows the expansion works. A one-line test in agent-usage-shared-test.sh would cover it, as a separate change.

This waits on the maintainer. My recommendation is to close it as already fixed by #14040, and to close #7194 with it. #8345, which fixes the same issue, carries the same recommendation.

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

2 participants