magpie icon indicating copy to clipboard operation
magpie copied to clipboard

agent: on Windows, Codex Sync leaves model_catalog_json naming magpie's gone list when the path is spelled with `/` (ownCatalog compares raw strings)

Open laweit-lxy opened this issue 1 month ago • 9 comments

Split out of #4115 (group C, TestCodexSyncMendsGoneCatalog/not_the_provider), with the cause traced and a fix checked.

What happens. A config.toml an older magpie left with model_catalog_json = "C:/Users/<u>/.codex/magpie-models.json" is not recognised as magpie's own list on Windows, so Sync doesn't take the key out once the list is gone. Codex then can't load its config (the case TestCodexSyncMendsGoneCatalog is written for).

Cause. internal/agent/codex.go, ownCatalog (main 8f804396fd, lines 287–295):

if c == catalogRef || c == at.native(catalogPath) {
	return true
}
return at.spell == nil && !filepath.IsAbs(c) && filepath.Clean(filepath.Join(dir, c)) == filepath.Clean(catalogPath)

On Windows catalogRef is the base name and at.native(catalogPath) is the \ spelling, so a /-spelled absolute path matches neither string, and the last line only handles relative paths. filepath.Clean on Windows turns / into \, so cleaning the absolute path too is enough:

-		return at.spell == nil && !filepath.IsAbs(c) && filepath.Clean(filepath.Join(dir, c)) == filepath.Clean(catalogPath)
+		if at.spell != nil {
+			return false
+		}
+		if !filepath.IsAbs(c) {
+			c = filepath.Join(dir, c)
+		}
+		return filepath.Clean(c) == filepath.Clean(catalogPath)

The WSL path (at.spell != nil) is left as it was.

Checked on Windows 11 ARM64, Go 1.26.8, go test -tags nogui ./internal/agent -run '^(TestCodexSyncMendsGoneCatalog|TestCodexOwnCatalogGoneNotPutBack|TestCodex.*Catalog.*)$' -count=1 -v, with the change applied through -overlay (source tree untouched). The ownCatalog function is identical on main 8f804396fd and on the tree tested.

  • before: TestCodexSyncMendsGoneCatalog/not_the_provider FAIL (codex_catalog_gone_test.go:153: config.toml still names C:/Users/.../.codex/magpie-models.json), /the_provider PASS, the other catalog tests PASS
  • after: all 26 PASS, including not_the_provider, TestCodexOwnCatalogGoneNotPutBack and TestCodexNeverNamesGoneCatalog

Not run on macOS/Linux; there filepath.Clean leaves / alone and IsAbs paths already compared equal in the second clause, so nothing should change.

laweit-lxy avatar Nov 01 '26 13:11 laweit-lxy

Confirmed on Windows 11 x64 (the report was ARM64), Go 1.27.3, at main 3e319622: TestCodexSyncMendsGoneCatalog/not_the_provider fails alone while the_provider passes, so the matrix gains the x64 datapoint. Picking this up with the clean-the-absolute-path fix (the WSL spelling untouched) — will open a PR shortly.

Dopetaiga avatar Nov 03 '26 07:11 Dopetaiga

A PR is not open to me here — this repository limits pull requests to collaborators — so the fix comes as a fork branch for pickup, as in #4986:

Branch (rebased on current main): https://github.com/Dopetaiga/magpie/tree/fix/codex-own-catalog-slash-path — head 7648493c8.

  • internal/agent/codex.go: ownCatalog cleans an absolute c too, so a /-spelled absolute path still names the catalog on Windows (#4785). The WSL spelling keeps its own branch.

Verified on Windows 11 x64 (the report was ARM64), Go 1.27.3: TestCodexSyncMendsGoneCatalog/not_the_provider red at main 3e319622 with the_provider green, and with the change the catalog set is green (go test -tags nogui ./internal/agent -run '^(TestCodexSyncMendsGoneCatalog|TestCodexOwnCatalogGoneNotPutBack|TestCodex.*Catalog.*)$' -count=1). The package's other reds are the known ones (#4115's TestT3CodeCodexInstance; TestCodexSignInInKeychain is red on unmodified main here too).

Dopetaiga avatar Nov 03 '26 08:11 Dopetaiga

take

ggbdpq avatar Nov 12 '26 11:11 ggbdpq

Verified @Dopetaiga's fix (7648493c8 on their fork) on current main (d0a4e0be6): it applies as is, the diff is exactly the shape @laweit-lxy traced in the issue body, and the full internal/agent suite shows no new failures. Posting a verification of their commit rather than a competing patch — the commit below is Dopetaiga's unchanged, replayed on main.

Why the fix is right (checked branch by branch against the old one-liner):

  • WSL (at.spell != nil): old returned false through the at.spell == nil conjunct; new returns false early. Same.
  • Relative c: filepath.Join(dir, c) then Clean both sides, as before. Same.
  • Absolute c on non-Windows: unreachable with a match — an exact match is caught by the c == at.native(catalogPath) line above, and Clean on unix changes nothing, so different absolute paths still don't match. No behavior change.
  • Absolute c spelled with / on Windows — the issue's case: old fed it through filepath.Join(dir, c), which prefixes dir onto an absolute path, so the comparison could never hit; new skips the join and filepath.Clean turns the slashes into \, so it names the catalog. Fixed.

Beyond Sync's mending, the same predicate gates the put-back (codex.go:1657), the keys list (:732) and the drift message "Codex's model_catalog_json is no longer magpie's list" (:2205) — so configs left by pre-cd217c174 magpies (which wrote /-spelled absolute paths, per #816's history) also stop triggering false drift rows once this lands.

Siblings: rg -n "model_catalog_json" internal/ --glob '*.go' (non-test) → the only same-shape predicate is deskCatalog (internal/agent/codex.go:435), which compares raw strings the same way and also mishandles an absolute /-spelled c. Leaving it: it was born on 2026-10-27 (add927e60) with the base-name write convention already in place, so magpie never wrote a /-spelled absolute desktop path — there is no legacy population like #4785's, and a hand-written one is the only way to reach it. Worth a follow-up only if a real config shows up.

Check Result
git am on d0a4e0be6 applies; tree identical to the branch head (e72a9d76d52fc286)
GOOS=windows go build/vet ./internal/agent/ clean
GOOS=linux go build -tags nogui ./internal/agent/ clean
`go test ./internal/agent/ -run 'TestCodex.*Catalog TestCodexSyncMendsGoneCatalog
go test ./internal/agent/ -count=1 -timeout 25m (macOS) only TestMothX fails — fails identically on the parent (7199afbb6) run alone; this host's known red (local proxy)
gofmt -l internal/agent/ clean

The red lives only on Windows: on unix filepath.ToSlash is the native spelling, so TestCodexSyncMendsGoneCatalog/not_the_provider cannot see the bug — the thread's real-machine runs are the red evidence (laweit-lxy on Windows 11 ARM64 at 8f804396fd, Dopetaiga on Windows 11 x64 at 3e319622, both not_the_provider FAIL, all green with the change). 2b22425b4's run sheet also lists #4785 among the failures yetone's own Windows 11 box sees at origin/main.


Source: reported in #4785 (split out of #4115's Windows test matrix) with the cause traced by the reporter; confirmed on a second machine and arch by Dopetaiga.

Red without the fix: not reproducible on my host (macOS; the failing predicate needs runtime.GOOS == "windows" — compile-time). The red is the thread's own data: config.toml still names C:/Users/.../.codex/magpie-models.json after Sync on Windows 11, both ARM64 and x64, at two main SHAs.

Siblings: deskCatalog left unchanged, reasons above. mark.go:28 (strings.Contains(value, "magpie")) is spelling-agnostic. import_codex.go opens the path as Codex wrote it — no string comparison against magpie's own path.

When the read fails: an unreadable config.toml fails GetTOMLTop as before — untouched. theirListGone (a user's own list, file missing) still takes precedence in the put-back path (:1657), unchanged.

Not verified: a real Windows machine (my host is macOS); Codex's actual reload of a mended config.toml on Windows.

Attribution: verification written with GLM-5.3-Flash (ZCode); the commit is Dopetaiga's unchanged — please keep them as the author or add Co-authored-by: Dopetaiga <[email protected]>. The root-cause analysis in the issue body is @laweit-lxy's, and the diff implements it as written.

Branch with the commit: hunt-20261112-t1-4785 (head d349bc957ab14aaa, on main d0a4e0be6).

Patch:

patch: agent: ownCatalog cleans an absolute /-spelled catalog path on Windows (#4785) — git am applies it as is; SHA-256 first 16: 371fc9c249e9f450

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Dopetaiga <[email protected]>
Date: Tue, 3 Nov 2026 14:59:50 +0800
Subject: [PATCH] agent: ownCatalog cleans an absolute /-spelled catalog path
 on Windows (#4785)

A config.toml naming the catalog with an absolute /-spelled path was
not recognised as magpie's own: the raw string matched neither the
base-name ref nor the \ spelling, so Sync left the key in and Codex
could not load its config once the list was gone. Clean the absolute
path too; on Windows that turns its slashes into the native ones.
The WSL spelling keeps its own branch.
---
 internal/agent/codex.go | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/internal/agent/codex.go b/internal/agent/codex.go
index f923feed3..833887104 100644
--- a/internal/agent/codex.go
+++ b/internal/agent/codex.go
@@ -294,7 +294,15 @@ func codexIn(at place) *Agent {
 		if c == catalogRef || c == at.native(catalogPath) {
 			return true
 		}
-		return at.spell == nil && !filepath.IsAbs(c) && filepath.Clean(filepath.Join(dir, c)) == filepath.Clean(catalogPath)
+		if at.spell != nil {
+			return false
+		}
+		// an absolute c spelled with / on Windows: Clean turns its slashes
+		// into \, so it still names the catalog there (#4785)
+		if !filepath.IsAbs(c) {
+			c = filepath.Join(dir, c)
+		}
+		return filepath.Clean(c) == filepath.Clean(catalogPath)
 	}
 	// ownList is magpie's model list for Codex as written to catalogPath:
 	// what the user added by hand to the list there (a key magpie doesn't
-- 
2.50.1 (Apple Git-155)


ggbdpq avatar Nov 12 '26 12:11 ggbdpq

Re-verified at current main (eb1b83000) — the branch was moved to today's tip and every claim below was re-run on the new head.

Branch: hunt-20261112-t1-4785, head 38a7e7e3f992eec4, patch sha256[:16] 719447a6508d0554, the commit is @Dopetaiga's unchanged (Author: Dopetaiga kept).

Element Result
Rebase onto current main clean, no conflicts; git range-diff: the commit is = — zero semantic drift
git am replay applies on a clean eb1b83000 checkout; replayed tree byte-identical to the branch head
Tests full ./internal/agent ok except TestMothX — fails on a clean eb1b83000 checkout too (this host's known proxy red, not from this change); TestCodexSyncMendsGoneCatalog targeted green; gofmt clean, go vet ./internal/agent/ clean; go build -tags nogui ok on linux/amd64, windows/amd64, darwin/arm64
Ablation the bug is invisible on macOS (filepath.ToSlash is the native spelling there): with codex.go reverted to main, TestCodexSyncMendsGoneCatalog passes here too — the red lives only on Windows, and the thread's on-machine runs stay the red evidence (laweit-lxy Windows 11 ARM64 at 8f804396fd, Dopetaiga Windows 11 x64 at 3e319622: not_the_provider FAIL without the change, all green with it)
Sibling faces ownCatalog has 8 call sites on the tip (codex.go — the config mender, the provider branches, the keys list, the drift path), all routing through this one predicate, so they all get the fix; deskCatalog still compares raw strings the same way (no legacy /-spelled population — unchanged follow-up candidate)
Evidence carried the Windows-only red is the reporters' on-machine evidence above; everything buildable locally was re-run on the new head

Not verified (unchanged from the post above): a real Windows machine (my host is macOS); Codex's actual reload of a mended config.toml on Windows.

Attribution: the commit is @Dopetaiga's unchanged — please keep them as the author or add Co-authored-by: Dopetaiga <[email protected]>. This verification post was written with GLM-5.3-Flash (ZCode).

ggbdpq avatar Nov 14 '26 01:11 ggbdpq

Landed in v0.1.4130: @Dopetaiga's f12a1f79d (authorship kept; @ggbdpq verified it, and @laweit-lxy traced the cause), plus my 6cc70b2b3 for the sibling.

  • ownCatalog cleans an absolute catalog path, so a /-spelled one names magpie's list on Windows.
  • deskCatalog (the Codex app's list) had the same raw-string shape and now cleans the path the same way. New test: TestCodexDesktopListSlashSpelled.

Run on Windows 11 x64 (-tags nogui) for the catalog tests:

  • With main's codex.go, TestCodexSyncMendsGoneCatalog/not_the_provider and the new test are red.
  • With Dopetaiga's commit alone, only the new test is red.
  • At the head, all are ok.

Leaving this open for @laweit-lxy to confirm on ARM64.

yetone avatar Nov 14 '26 05:11 yetone

Confirmed on Windows 11 ARM64 (build 26200, VM), Go 1.27.4 windows/arm64.

go test -tags nogui ./internal/agent -run '^(TestCodex.*Catalog.*|TestCodexDesktopListSlashSpelled)$' -count=1 -v

Tree Result
v0.1.4130, with internal/agent/codex.go from f12a1f79d~1 TestCodexSyncMendsGoneCatalog/not_the_provider and TestCodexDesktopListSlashSpelled FAIL (config.toml still names the /-spelled catalog)
v0.1.4130, with codex.go from f12a1f79d only TestCodexDesktopListSlashSpelled FAILs
v0.1.4130 as released all pass, no skips
v0.1.4994 (5099be8b0) all pass, no skips

This matches your x64 run. I didn't separately check that the Codex app or CLI reloads a mended config.toml. The fix works on ARM64 as far as these tests go, so this looks good to close from my side. Thanks, all!

laweit-lxy avatar Nov 28 '26 08:11 laweit-lxy

这条我不确定 —— 引用的提交方向不同,想请你判(2026-11-28)。

提交 6cc70b2b3「agent: deskCatalog cleans an absolute /-spelled path too」

提交做的是「把绝对路径的 / 写法也清掉」,而这条报的是「Windows 上 Codex Sync 会留下 model_catalog_json」 —— 前者是路径归一化,后者是键没被清掉。

请判一句:Windows 上 Codex Sync 之后,model_catalog_json 现在还留着吗? 留着的话,我按「谁该删这个键」那条链读一遍(我此前在 codex.go:342 附近看过 unnameGoneList)。

L0NE-6 avatar Nov 28 '26 12:11 L0NE-6

@L0NE-6 现在不会留下了。这个键之所以没被删,是因为 ownCatalog 用原始字符串比较路径,认不出 / 写法的绝对路径是 magpie 自己的清单,Sync 就不删它。所以统一路径写法正是修复本身。主修复是 f12a1f79d,6cc70b2b3 是给 deskCatalog 补的同类修复。TestCodexSyncMendsGoneCatalog/not_the_provider 检查的就是 Sync 后 config.toml 里不再有这个键,我在 ARM64 上修复前失败、v0.1.4130 和 v0.1.4994 都通过(见上一条)。

laweit-lxy avatar Nov 29 '26 02:11 laweit-lxy