From 6096656f7cad694694ce48b5884eedd45eaa0722 Mon Sep 17 00:00:00 2001 From: Longado Date: Wed, 26 Aug 2026 15:38:06 +0800 Subject: [PATCH 1/7] Fix Pi Codex session discovery --- bin/omarchy-agent-usage-codex | 38 ++++- .../shell.d/agent-usage-codex-scanner-test.sh | 148 +++++++++++++++++- 2 files changed, 170 insertions(+), 16 deletions(-) diff --git a/bin/omarchy-agent-usage-codex b/bin/omarchy-agent-usage-codex index 972c164dee96..302357fe488f 100755 --- a/bin/omarchy-agent-usage-codex +++ b/bin/omarchy-agent-usage-codex @@ -135,12 +135,28 @@ def add_usage(day, session_key, model, input_tokens, output_tokens, cache_read, today_tokens_by_model[model] = today_tokens_by_model.get(model, 0) + total -def scan_pi_sessions(): +def pi_session_roots(): + coding_agent_dir = Path(os.environ.get("PI_CODING_AGENT_DIR") or (Path.home() / ".config" / "pi")) roots = [ Path.home() / ".pi" / "agent" / "sessions", Path.home() / ".omp" / "agent" / "sessions", + coding_agent_dir / "sessions", ] + normalized = [] + for root in roots: + try: + root = root.expanduser().resolve() + except (OSError, RuntimeError): + root = root.absolute() + if root not in normalized: + normalized.append(root) + return normalized + + +def scan_pi_sessions(roots=None): rg = find_command("rg") or "rg" + if roots is None: + roots = pi_session_roots() for root in roots: if not root.exists(): continue @@ -343,12 +359,15 @@ def cache_root(): return root -def scan_cache_paths(): +def scan_cache_paths(pi_roots=None): codex_home = Path(os.environ.get("CODEX_HOME") or (Path.home() / ".codex")) db = Path(os.environ.get("XDG_DATA_HOME") or (Path.home() / ".local" / "share")) / "opencode" / "opencode.db" # The digest covers every data path the scan reads: the codex session - # roots, the opencode DB, and (via Path.home()) the pi/omp session roots. - digest = hashlib.sha1((str(Path.home()) + "\n" + str(codex_home) + "\n" + str(db)).encode("utf-8")).hexdigest()[:16] + # roots, the opencode DB, and each supported Pi session root. + if pi_roots is None: + pi_roots = pi_session_roots() + scan_paths = [codex_home, db, *pi_roots] + digest = hashlib.sha1("\n".join(str(path) for path in scan_paths).encode("utf-8")).hexdigest()[:16] root = cache_root() return root / f"codex-scan-{digest}.json", root / f"codex-scan-{digest}.lock" @@ -432,8 +451,10 @@ def local_stats(): } -def run_local_scans(): - scan_pi_sessions() +def run_local_scans(pi_roots=None): + if pi_roots is None: + pi_roots = pi_session_roots() + scan_pi_sessions(pi_roots) scan_native_codex_sessions() complete = scan_opencode_sessions() return local_stats(), complete @@ -455,7 +476,8 @@ def cached_local_stats(max_age): def _cached_local_stats(max_age): - cache_file, lock_file = scan_cache_paths() + pi_roots = pi_session_roots() + cache_file, lock_file = scan_cache_paths(pi_roots) cached = read_cached_stats(cache_file, max_age) if cached is not None: @@ -466,7 +488,7 @@ def _cached_local_stats(max_age): cached = read_cached_stats(cache_file, max_age) if cached is not None: return cached - stats, complete = run_local_scans() + stats, complete = run_local_scans(pi_roots) # An interrupted scan still serves this run, but caching it would # suppress the missing usage for every reader until the cache expires. if complete: diff --git a/test/shell.d/agent-usage-codex-scanner-test.sh b/test/shell.d/agent-usage-codex-scanner-test.sh index 3ce4883cdbfb..b1da46166c11 100644 --- a/test/shell.d/agent-usage-codex-scanner-test.sh +++ b/test/shell.d/agent-usage-codex-scanner-test.sh @@ -5,6 +5,8 @@ source "$(dirname "$0")/base-test.sh" require_command jq require_command python3 +unset PI_CODING_AGENT_DIR XDG_CACHE_HOME + TEST_HOME=$(mktemp -d) trap 'rm -rf "$TEST_HOME"' EXIT @@ -36,7 +38,7 @@ done EOF chmod +x "$TEST_HOME/bin/codex" -timestamp="$(date +%Y-%m-%d)T12:00:00Z" +timestamp="$(date -Iseconds)" session="$TEST_HOME/.codex/sessions/$(date +%Y/%m/%d)/rollout.jsonl" cat >"$session" <"$PI_HOME/.pi/agent/sessions/project/pi.jsonl" <"$PI_HOME/.omp/agent/sessions/project/omp.jsonl" <"$PI_HOME/.config/pi/sessions/project/pi-config.jsonl" <"$PI_HOME/pi-tilde/sessions/project/pi-tilde.jsonl" <"$PI_HOME/pi-custom/sessions/project/pi-custom.jsonl" <"$PI_HOME/pi-other/sessions/project/pi-other.jsonl" <"$PI_HOME/cwd-a/pi-relative/sessions/project/pi-relative.jsonl" <"$PI_HOME/cwd-b/pi-relative/sessions/project/pi-relative.jsonl" < Date: Mon, 28 Sep 2026 20:30:58 +0200 Subject: [PATCH 2/7] Default PI_CODING_AGENT_DIR to Pi's own ~/.pi/agent 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 --- bin/omarchy-agent-usage-codex | 2 +- test/shell.d/agent-usage-codex-scanner-test.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/omarchy-agent-usage-codex b/bin/omarchy-agent-usage-codex index 302357fe488f..572d9b877144 100755 --- a/bin/omarchy-agent-usage-codex +++ b/bin/omarchy-agent-usage-codex @@ -136,7 +136,7 @@ def add_usage(day, session_key, model, input_tokens, output_tokens, cache_read, def pi_session_roots(): - coding_agent_dir = Path(os.environ.get("PI_CODING_AGENT_DIR") or (Path.home() / ".config" / "pi")) + coding_agent_dir = Path(os.environ.get("PI_CODING_AGENT_DIR") or (Path.home() / ".pi" / "agent")) roots = [ Path.home() / ".pi" / "agent" / "sessions", Path.home() / ".omp" / "agent" / "sessions", diff --git a/test/shell.d/agent-usage-codex-scanner-test.sh b/test/shell.d/agent-usage-codex-scanner-test.sh index b1da46166c11..8531b61d0d58 100644 --- a/test/shell.d/agent-usage-codex-scanner-test.sh +++ b/test/shell.d/agent-usage-codex-scanner-test.sh @@ -72,7 +72,7 @@ pass "Codex collector identifies itself with an empty limits list" # Pi and omp can both spend a Codex subscription without creating native # Codex sessions. Their compatible JSONL transcripts must be included, -# including Pi's current default config directory. +# including sessions under a PI_CODING_AGENT_DIR that moves Pi's agent directory. PI_HOME=$(mktemp -d) trap 'rm -rf "$TEST_HOME" "$PI_HOME"' EXIT mkdir -p "$PI_HOME/bin" "$PI_HOME/.pi/agent/sessions/project" "$PI_HOME/.omp/agent/sessions/project" "$PI_HOME/.config/pi/sessions/project" @@ -88,7 +88,7 @@ cat >"$PI_HOME/.config/pi/sessions/project/pi-config.jsonl" < Date: Sat, 3 Oct 2026 05:00:19 +0200 Subject: [PATCH 3/7] Expand PI_CODING_AGENT_DIR as Pi does and prefer its spelling 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 Co-Authored-By: Codex Medium --- bin/omarchy-agent-usage-codex | 20 +++++++++---------- .../shell.d/agent-usage-codex-scanner-test.sh | 20 +++++++++++++++---- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/bin/omarchy-agent-usage-codex b/bin/omarchy-agent-usage-codex index 37d8aa5baec1..d4b862a5876c 100755 --- a/bin/omarchy-agent-usage-codex +++ b/bin/omarchy-agent-usage-codex @@ -240,6 +240,13 @@ def pi_parent_session(path): # sessions rather than double-counting the default one. def pi_session_roots(): roots = [] + # Pi keeps its whole agent tree in PI_CODING_AGENT_DIR when that is set, and + # expands only a leading ~ or ~/ in it, so ~user stays a literal directory. + agent_dir = os.environ.get("PI_CODING_AGENT_DIR") + if agent_dir: + if agent_dir == "~" or agent_dir.startswith("~/"): + agent_dir = str(Path.home()) + agent_dir[1:] + roots.append(Path(os.path.abspath(agent_dir)) / "sessions") for base in (Path.home() / ".pi", Path.home() / ".omp"): roots.append(base / "agent" / "sessions") profiles = base / "profiles" @@ -249,17 +256,8 @@ def pi_session_roots(): except OSError: # No profiles directory, or it is unreadable: the default root stands. pass - # Pi keeps its whole agent tree in PI_CODING_AGENT_DIR when that is set, so - # sessions written that way live nowhere else. - agent_dir = os.environ.get("PI_CODING_AGENT_DIR") - if agent_dir: - try: - roots.append(Path(os.path.abspath(Path(agent_dir).expanduser())) / "sessions") - except RuntimeError: - # ~user for a user that does not exist: there is nothing there to scan. - pass - # Aliased roots are scanned once, but under the path as given: parentSession - # links name sessions by the path pi used, not the resolved one. + # Aliased roots are scanned once, under the first spelling: parentSession + # links name sessions by the path pi used, which is PI_CODING_AGENT_DIR's. unique = [] resolved = set() for root in roots: diff --git a/test/shell.d/agent-usage-codex-scanner-test.sh b/test/shell.d/agent-usage-codex-scanner-test.sh index 604393a94bbd..651f35e3f51a 100644 --- a/test/shell.d/agent-usage-codex-scanner-test.sh +++ b/test/shell.d/agent-usage-codex-scanner-test.sh @@ -192,21 +192,33 @@ result=$(agent_usage "$AGENT_HOME/.config/pi") fail "Codex collector reads usage from PI_CODING_AGENT_DIR" "$result" pass "Codex collector counts usage under PI_CODING_AGENT_DIR" +# Pi run through an alias of its default directory names a fork's parent by the +# alias, so the alias is the spelling scanned, or the fork counts as a session. ln -s "$AGENT_HOME/.pi/agent" "$AGENT_HOME/pi-alias" +cat >"$AGENT_HOME/.pi/agent/sessions/project/pi-fork.jsonl" <"$AGENT_HOME/cwd-tilde/~omarchy-codex-7194/sessions/project/pi-literal.jsonl" <"$AGENT_HOME/pi-custom/sessions/project/pi-custom.jsonl" < Date: Sat, 3 Oct 2026 05:04:28 +0200 Subject: [PATCH 4/7] Match Pi forks to their parents by resolved path 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 Co-Authored-By: Codex Medium --- bin/omarchy-agent-usage-codex | 14 ++++++-------- test/shell.d/agent-usage-codex-scanner-test.sh | 14 ++++++++------ 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bin/omarchy-agent-usage-codex b/bin/omarchy-agent-usage-codex index d4b862a5876c..cab7350c6035 100755 --- a/bin/omarchy-agent-usage-codex +++ b/bin/omarchy-agent-usage-codex @@ -230,7 +230,7 @@ def pi_parent_session(path): parent = header.get("parentSession") if not isinstance(parent, str) or not parent: return None - return os.path.abspath(os.path.expanduser(parent)) + return os.path.realpath(os.path.expanduser(parent)) # `omp --profile=` (and pi's equivalent) relocates the whole agent tree @@ -256,15 +256,11 @@ def pi_session_roots(): except OSError: # No profiles directory, or it is unreadable: the default root stands. pass - # Aliased roots are scanned once, under the first spelling: parentSession - # links name sessions by the path pi used, which is PI_CODING_AGENT_DIR's. + # Aliased roots are scanned once. unique = [] resolved = set() for root in roots: - try: - key = root.resolve() - except (OSError, RuntimeError): - key = root.absolute() + key = os.path.realpath(root) if key not in resolved: resolved.add(key) unique.append(root) @@ -304,7 +300,9 @@ def scan_pi_sessions(roots=None): continue line = event.get("data", {}).get("lines", {}).get("text", "") raw_path = event.get("data", {}).get("path", {}).get("text", "pi-session") - path = os.path.abspath(os.path.expanduser(raw_path)) + # Resolved, like parentSession, so a fork finds its parent whichever + # alias of the agent directory either was written under. + path = os.path.realpath(os.path.expanduser(raw_path)) entry = json.loads(line) except Exception: continue diff --git a/test/shell.d/agent-usage-codex-scanner-test.sh b/test/shell.d/agent-usage-codex-scanner-test.sh index 651f35e3f51a..55718b1d7aa2 100644 --- a/test/shell.d/agent-usage-codex-scanner-test.sh +++ b/test/shell.d/agent-usage-codex-scanner-test.sh @@ -192,18 +192,20 @@ result=$(agent_usage "$AGENT_HOME/.config/pi") fail "Codex collector reads usage from PI_CODING_AGENT_DIR" "$result" pass "Codex collector counts usage under PI_CODING_AGENT_DIR" -# Pi run through an alias of its default directory names a fork's parent by the -# alias, so the alias is the spelling scanned, or the fork counts as a session. +# An alias of the default directory is scanned once, and a fork finds its +# parent whichever of the two spellings it was written under. ln -s "$AGENT_HOME/.pi/agent" "$AGENT_HOME/pi-alias" -cat >"$AGENT_HOME/.pi/agent/sessions/project/pi-fork.jsonl" <"$AGENT_HOME/.pi/agent/sessions/project/pi-fork-${spelling//\//-}.jsonl" < Date: Sat, 3 Oct 2026 22:34:26 +0800 Subject: [PATCH 5/7] test: cover repointed symlinks for Pi and Codex directories --- .../shell.d/agent-usage-codex-scanner-test.sh | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/shell.d/agent-usage-codex-scanner-test.sh b/test/shell.d/agent-usage-codex-scanner-test.sh index 55718b1d7aa2..407e210b03af 100644 --- a/test/shell.d/agent-usage-codex-scanner-test.sh +++ b/test/shell.d/agent-usage-codex-scanner-test.sh @@ -259,6 +259,55 @@ result=$(cd "$AGENT_HOME/cwd-b" && agent_usage pi-relative) fail "Codex collector keys a relative PI_CODING_AGENT_DIR by working directory" "$result" pass "Codex collector keys a relative PI_CODING_AGENT_DIR by working directory" +# A symlinked PI_CODING_AGENT_DIR keeps its own spelling when the link is +# repointed, so the cache has to be keyed by where it resolves to. +mkdir -p "$AGENT_HOME/pi-link-a/sessions/project" "$AGENT_HOME/pi-link-b/sessions/project" +cat >"$AGENT_HOME/pi-link-a/sessions/project/pi-link.jsonl" <"$AGENT_HOME/pi-link-b/sessions/project/pi-link.jsonl" <"$LINK_HOME/codex-$side/sessions/$(date +%Y/%m/%d)/rollout.jsonl" < Date: Sat, 3 Oct 2026 22:34:33 +0800 Subject: [PATCH 6/7] fix: key scan cache by resolved paths 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. --- bin/omarchy-agent-usage-codex | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bin/omarchy-agent-usage-codex b/bin/omarchy-agent-usage-codex index cab7350c6035..99d3e047da2f 100755 --- a/bin/omarchy-agent-usage-codex +++ b/bin/omarchy-agent-usage-codex @@ -563,15 +563,17 @@ def cache_root(): def scan_cache_digest(): codex_home = Path(os.environ.get("CODEX_HOME") or (Path.home() / ".codex")) db = Path(os.environ.get("XDG_DATA_HOME") or (Path.home() / ".local" / "share")) / "opencode" / "opencode.db" - # The digest covers every data path the scan reads: the codex session - # roots, the opencode DB, and (via Path.home()) the pi/omp session roots. - return hashlib.sha1((str(Path.home()) + "\n" + str(codex_home) + "\n" + str(db)).encode("utf-8")).hexdigest()[:16] + # The digest covers the codex session root and the opencode DB; the pi/omp + # session roots are keyed separately in scan_cache_paths. Resolved, so + # repointing a symlinked CODEX_HOME or data dir changes the key. + return hashlib.sha1((str(Path.home()) + "\n" + os.path.realpath(codex_home) + "\n" + os.path.realpath(db)).encode("utf-8")).hexdigest()[:16] def scan_cache_paths(pi_roots): # The pi roots are keyed too: PI_CODING_AGENT_DIR moves them without moving # Path.home(), and a cache from the old roots would hide the new sessions. - digest = hashlib.sha1((scan_cache_digest() + "\n" + "\n".join(str(root) for root in pi_roots)).encode("utf-8")).hexdigest()[:16] + # Resolved, so repointing a symlinked root changes the key too. + digest = hashlib.sha1((scan_cache_digest() + "\n" + "\n".join(os.path.realpath(root) for root in pi_roots)).encode("utf-8")).hexdigest()[:16] root = cache_root() return root / f"codex-scan-{digest}.json", root / f"codex-scan-{digest}.lock" From 04750c7e12457b77407e255296a74716f26b1428 Mon Sep 17 00:00:00 2001 From: Longado Date: Fri, 9 Oct 2026 10:20:32 +0800 Subject: [PATCH 7/7] test: cover per-file cache after CODEX_HOME symlink is repointed --- .../shell.d/agent-usage-codex-scanner-test.sh | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/shell.d/agent-usage-codex-scanner-test.sh b/test/shell.d/agent-usage-codex-scanner-test.sh index 407e210b03af..e3fe63eb8aef 100644 --- a/test/shell.d/agent-usage-codex-scanner-test.sh +++ b/test/shell.d/agent-usage-codex-scanner-test.sh @@ -308,6 +308,32 @@ result=$(codex_home_usage) fail "Codex collector rescans after a CODEX_HOME symlink is repointed" "$result" pass "Codex collector rescans after a CODEX_HOME symlink is repointed" +# The per-file cache is keyed by session path, spelled through the link, so +# only its file name keeps the two homes apart. Same size and mtime on both +# sides make a stale record look current; dropping the aggregate cache leaves +# the per-file cache as the only thing that could replay the old totals. +for side_tokens in c:10 d:20; do + side=${side_tokens%%:*} + tokens=${side_tokens##*:} + mkdir -p "$LINK_HOME/codex-$side/sessions/$(date +%Y/%m/%d)" + touch "$LINK_HOME/codex-$side/auth.json" + cat >"$LINK_HOME/codex-$side/sessions/$(date +%Y/%m/%d)/rollout.jsonl" <