magpie icon indicating copy to clipboard operation
magpie copied to clipboard

sessions, usage: a Codex folder whose sessions folder is a junction (Windows, .codex on another drive) lists no Codex session or call at all

Open JasonBuildAI opened this issue 1 month ago • 4 comments

A Codex folder whose sessions folder is a link — which is what the Codex CLI itself leaves on Windows when .codex is kept on another drive — reads as no sessions at all: the Sessions page lists no Codex session and Usage lists no Codex call, token or cost.

Impact

.codex on another drive is the normal Windows layout: the Codex CLI reaches its folders through NTFS junctions. On this machine %USERPROFILE%\.codex holds 22 junctions, sessions and archived_sessions among them:

> dir /AL %USERPROFILE%\.codex
2026/09/15  11:29    <JUNCTION>     archived_sessions [\??\D:\CDriveData\C_\Users\...\.codex\archived_sessions]
2026/09/15  11:29    <JUNCTION>     sessions          [\??\D:\CDriveData\C_\Users\...\.codex\sessions]

internal/sessions/sessions.go's codexFilesIn hands filepath.Join(dir, folder) to filepath.WalkDir, which only lstat()s the root it is given. To lstat a junction is not a directory, so WalkDir calls back once for the root itself and never descends. Every rollout is missed. The same user's .codex copied to a plain folder reads fine, so nothing else in the Codex path is involved.

Reproduction

Windows 11, upstream main d0fcb8bc, .codex on another drive as above.

  1. magpie usage all — 26,563 calls, and no Codex row at all:
125M tokens all time · 26563 calls · no price (26550 calls unpriced)
  agents
  DeepSeek Harness  71% 89M     18370 calls no price
  WorkBuddy         20% 25M     4998 calls no price
  ZCode              8% 10M     2925 calls no price
  OpenCode           1% 1.1M    207 calls  no price
  Claude Code        0% 33.8K   63 calls   no price
  1. The same .codex copied to a plain folder: CODEX_HOME=<copy> magpie usage all — 69,935 Codex calls.

  2. Why, with stdlib only (the walk of each folder, and whether the root is a directory to each call):

C:\Users\Jason\.codex\sessions
  lstat: mode=?rw-rw-rw- isdir=false      <- what WalkDir looks at
  stat:  mode=drwxrwxrwx isdir=true
  WalkDir entries seen: 1
D:\...\_m9_cx_home\.codex\sessions        <- the same data, no link
  lstat: mode=drwxrwxrwx isdir=true
  WalkDir entries seen: 252

codexFilesIn is the one agent reader here that neither resolves its root nor accepts a link entry: ccFiles lists through readDirectory, whose os.Stat follows a link, and hermes.go/manage.go check ModeSymlink on their entries.

Expected

The rollouts behind the link are listed, as they are when the same folder is not a link: the Codex session on the Sessions page, and its calls and tokens in Usage and the ledger.

Fix

Follow the link before walking. internal/proc.RealPath already exists for this — it is documented as "where the file at p really is, its links followed... filepath.EvalSymlinks leaves junctions as they are (Go 1.23)" and was written for Codex's own install.ps1 junctions. filepath.EvalSymlinks is not enough here, as it does not resolve a junction either.

JasonBuildAI avatar Oct 14 '26 16:10 JasonBuildAI

Patch ready and pushed to the fork, branch fix/codex-sessions-junction-mg7x3 (commit 4402641c, on top of 2a99e7a4; 2 files, 64 insertions). Opening a pull request from here to this repository is refused by the API for this account (POST /repos/yetone/magpie/pulls -> 404 Not Found; GraphQL createPullRequest -> FORBIDDEN "does not have the correct permissions"), so the diff is on that branch rather than in a PR. Files: internal/sessions/sessions.go (+10), internal/sessions/codex_link_test.go (+54). The same refusal happens for every account in this batch, and no pull request has been opened on this repository since 2026-10-08.

JasonBuildAI avatar Oct 14 '26 16:10 JasonBuildAI

Updated: the fix now also keeps the path shape right, and the branch is fix/codex-sessions-junction-mg7x3 at 270f2535 (fork JasonBuildAI/magpie, on main 2a99e7a4; 2 files, +97/-1).

Details have changed since the note above. codexFilesIn now walks what proc.RealPath resolves the link to, but still reports each rollout at its path under the Codex folder. A rollout's path is read back to the Codex folder for its config.toml and state_*.sqlite (codexHomeOf, the provider a session reads as, MoveCodexProvider), and the link's target holds neither: on this machine C:\Users\Jason\.codex has config.toml and a state_*.sqlite, while D:\CDriveData\C_\Users\黄江南\.codex has neither. Naming rollouts under the target would make every Codex session read as a provider the user is not on, and a move offered on one would write a file Codex does not read. TestCodexRolloutsBehindALink now pins that too: same rollout keys and sizes, same session count, a rollout that reads back as the fixture's Codex folder, and every session's provider read from the config.toml written there.

End to end on this machine, same .codex, two binaries built from this tree, one from 2a99e7a4 alone and one from this branch:

magpie usage all           2a99e7a4: 125M tokens · 26563 calls, no Codex row among the agents
magpie usage all           this branch: 381M tokens · 98736 calls, Codex 72173 of them (67%, 257M)
magpie sessions --limit 500  rows whose agent is Codex: 0 (483 sessions) versus 436 (600 Codex sessions in 1083)

Without the resolver, the test fails with codex_link_test.go:65: rollouts behind a link: map[], want map[codex:01a0bdc9-…:833]; with the walk left at the link's target it fails with codex_link_test.go:71: a rollout behind a link reads as …\002, want …\001\codex; with the change it passes. gofmt -l on the two files is empty, go vet and go build pass for windows, linux/nogui and linux, go test -tags nogui -count=1 -run TestCodex ./internal/sessions/ is ok (11s) and the whole package is ok (82s) when TMP/TEMP point at a fresh folder (TestCursorEditorCopiedOncePerChange otherwise trips on a %TEMP%\magpie-cursor-fresh that TestCursorLeftCopiesSwept leaves behind, since os.TempDir() ignores TMPDIR on Windows; it fails the same way without this change).

Opening a pull request from here to this repository is still refused by the API for this account (createPullRequest FORBIDDEN; POST /repos/yetone/magpie/pulls 404), so the diff stays on that branch. The full write-up, ready to paste as a PR body, is at the same branch's commit body.

AI disclosure: this change was drafted with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 14 '26 17:10 JasonBuildAI

Thanks @JasonBuildAI. Your change is in as 85e84d625, with your authorship kept.

Cause. filepath.WalkDir lstats the root it is given. A junction doesn't read as a folder that way, so a junctioned ~/.codex/sessions walked nothing. The root is now resolved with proc.RealPath, and each rollout is still named under the Codex folder, so the config and provider lookups keep working.

Sibling fix (d92cc5b84). Two more agents walked their root the same way and missed everything behind a link:

  • WorkBuddy's (and CodeBuddy Code's) projects folder;
  • MiniMax Code's sessions folder.

The follow-and-rename is now one helper, walkLinked, used by all three agents.

What was run:

  • TestCodexRolloutsBehindALink and the new TestBuddyProjectsBehindALink. Each fails with the resolver or the rename taken out, and both pass on macOS (symlink) and on the Windows box (mklink /J).
  • go test -tags nogui ./... on macOS: every package ok.
  • go vet on darwin, linux and windows: clean.

This ships in the next release after v0.1.1231. I'll leave this open until you confirm Sessions and Usage show your Codex history.

yetone avatar Oct 14 '26 19:10 yetone

Confirmed end to end on this machine - the same one, %USERPROFILE%\.codex holding the junctions sessions and archived_sessions to D:. magpie built from main (82f48f09), Go 1.26.8:

> magpie usage all
388M tokens all time · 101505 calls · no price
  Codex             68% 263M    74738 calls no price ttft 12.0 s · 4 tok/s

> magpie sessions --limit 500
459 of the 500 rows are Codex (before the fix: 0 of 483)

Before the fix the same command listed no Codex row at all among the agents, as in the report. The unit tests pass on main too:

--- PASS: TestCodexRolloutsBehindALink (0.29s)
--- PASS: TestBuddyProjectsBehindALink (0.32s)
ok  github.com/yetone/magpie/internal/sessions  0.714s

So Sessions and Usage show my Codex history. Closing. 85e84d625 with the sibling fix d92cc5b84 is on main but not in v0.1.1242, so this ships in the next release; my branch fix/codex-sessions-junction-mg7x3 (270f2535) is what landed and stays unmerged.

AI disclosure: this verification was produced with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 14 '26 21:10 JasonBuildAI