diff --git a/plugin/scripts/codex-hook.js b/plugin/scripts/codex-hook.js index c034b82..f2b83ca 100644 --- a/plugin/scripts/codex-hook.js +++ b/plugin/scripts/codex-hook.js @@ -148,10 +148,37 @@ function stablePluginRootForStrayCopy(root) { return null; } +function cleanupStrayPluginCopy(root) { + if (!isReflexioStrayPluginCopy(root)) return; + const rootReal = realDir(root); + if (!rootReal) return; + try { + fs.rmSync(rootReal, { recursive: true, force: true }); + appendLog("backend.log", `[claude-smart] removed stray plugin copy under ~/.reflexio (${rootReal})`); + } catch (err) { + appendLog("backend.log", `[claude-smart] failed to remove stray plugin copy ${rootReal}: ${err && err.message ? err.message : err}`); + return; + } + + // Remove now-empty host-created wrapper folders (for example + // ~/.reflexio/CUsersAliceRepo/) without touching real Reflexio state dirs. + let parent = path.dirname(rootReal); + const reflexioReal = realDir(REFLEXIO_DIR); + while (reflexioReal && isInsideDir(reflexioReal, parent)) { + try { + fs.rmdirSync(parent); + } catch { + break; + } + parent = path.dirname(parent); + } +} + function stablePluginRoot(root) { const stable = stablePluginRootForStrayCopy(root); if (stable) { appendLog("backend.log", `[claude-smart] redirecting stray plugin copy under ~/.reflexio (${root}) to stable root ${stable}`); + cleanupStrayPluginCopy(root); return stable; } return root; diff --git a/tests/test_install_scripts.py b/tests/test_install_scripts.py index a4d8816..9d2cf8d 100644 --- a/tests/test_install_scripts.py +++ b/tests/test_install_scripts.py @@ -2579,7 +2579,8 @@ def test_codex_hook_redirects_reflexio_stray_copy_to_stable_root( assert (reflexio / "plugin-root").resolve() == stable_root assert json.loads(result.stdout) == {"continue": True} assert not (reflexio / "plugin-root").resolve() == stray_root - assert "redirecting stray plugin copy" in ( + assert not stray_root.exists() + assert "removed stray plugin copy" in ( tmp_path / ".claude-smart" / "backend.log" ).read_text()