Skip to content

A local plugin for image-heavy chats in Codex, Claude Code and other agents: earlier images stop going out with every message, and the model fetches the one it needs by its ID, also after compaction.

fix(windows): 数据目录挪出 AppData,避免被微软商店应用的转存拆成几份

chipfighterfdb56c513977
diff --git a/AGENTS.md b/AGENTS.md
index f9832bb..873775c 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -12,7 +12,7 @@ Codex 上下文素材管理器(暂名):让用户逐轮决定,Codex 任
## 已确认的规矩
- 原始对话记录和素材文件始终只读。
-- 真实数据不进仓库:从真实会话和用户素材里提取的内容只能放在本机的 `local/`(已被 git 忽略)或工具的数据目录 `%LOCALAPPDATA%\codex-attachment-manager`,任何时候都不提交。
+- 真实数据不进仓库:从真实会话和用户素材里提取的内容只能放在本机的 `local/`(已被 git 忽略)或工具的数据目录(Windows 是 `%USERPROFILE%\.codex-attachment-manager`),任何时候都不提交。
- 插件本体在 `plugin/`(`plugin/src` 是运行代码),测试和实验脚本在 `spike/`,一行命令用的安装、卸载脚本在 `scripts/`。改动运行代码后,要重新运行 `cam install`,Codex 缓存里的插件副本才会更新;不要让已安装的配置指向不存在的文件。
- `scripts/*.ps1` 只用 ASCII 字符:Windows PowerShell 5.1 用 `irm … | iex` 下载时按 Latin-1 解码,中文会乱码。
- 仓库里的文档和代码不写用户本机的具体路径;通用位置(如 `~/.codex`、`%LOCALAPPDATA%`)可以写。
diff --git a/README.md b/README.md
index 84e92b5..077db64 100644
--- a/README.md
+++ b/README.md
@@ -111,7 +111,7 @@ macOS、Linux 上,插件被移除或关掉后,引擎会自己恢复直连,
## 数据和隐私
-- 所有数据只留在本机的数据目录:Windows 是 `%LOCALAPPDATA%\codex-attachment-manager`,macOS 是 `~/Library/Application Support/codex-attachment-manager`,Linux 是 `~/.local/share/codex-attachment-manager`。里面有:
+- 所有数据只留在本机的数据目录:Windows 是 `%USERPROFILE%\.codex-attachment-manager`,macOS 是 `~/Library/Application Support/codex-attachment-manager`,Linux 是 `~/.local/share/codex-attachment-manager`。里面有:
- 勾选记录;
- 请求统计:只记大小和数量,不记内容;
- 日志:不记对话内容和凭据;
diff --git a/docs/plan.md b/docs/plan.md
index 59e21bc..6ddb6f7 100644
--- a/docs/plan.md
+++ b/docs/plan.md
@@ -340,7 +340,7 @@ P1 通过后,再按用户的节奏一步步探索改写。
- **启动**:
- `.mcp.json` 用 `cmd.exe /d /s /c call ./scripts/launch.cmd ./src/plugin-server.ts` 启动插件服务,`launch.cmd` 优先用 Codex 自带的 Node(v0.1 起改成三个平台通用的 `./scripts/launch`,见第 14 节);
- 根目录的 `cam.cmd` 用同一个脚本运行命令行,用户不用另装 Node。
-- **数据目录**:`%LOCALAPPDATA%\codex-attachment-manager`。插件目录会在更新时被 Codex 替换,而且不往 `~/.codex` 里写。
+- **数据目录**:`%LOCALAPPDATA%\codex-attachment-manager`(v0.1-15 起改为 `%USERPROFILE%\.codex-attachment-manager`,见第 17 节)。插件目录会在更新时被 Codex 替换,而且不往 `~/.codex` 里写。
- **安装顺序**:
1. 检查和用户自己设置的冲突,然后备份 config.toml;
2. 登记市场,装插件(每次都重装,让 Codex 的副本和仓库一致);
@@ -482,3 +482,20 @@ P1 通过后,再按用户的节奏一步步探索改写。
- **测试**:环境变量 `CAM_LANG=zh|en` 可以固定语言(测试、开发用)。断言中文措辞的测试都固定成中文:第一次推到 CI 时,这些测试在英文系统上全部失败,而且在本机也会随记下的语言变化。
- **已知代价**:同一个任务中途切换 Codex 的语言,说明文字会变,模型那边缓存的上下文要重算一次。
- **验证**:`v01-placeholder-selftest.ts --lang en` 用同样的五轮对话检查英文说明:模型不收回之前的回答,被问到没说过的细节时回复“need IMG-xxx”。
+
+## 17. Windows 的数据目录(v0.1-15)
+
+### 已查证的事实
+
+- Codex 桌面版和 Claude 桌面版都是微软商店的 MSIX 应用。按 Windows 的规则,这类应用启动的程序在 AppData 下新建的文件,会被转存到该应用的私有目录 `%LOCALAPPDATA%\Packages\<应用>\LocalCache\Local\…`,只有这个应用里的程序能在原路径看到它们;修改 AppData 里已有的文件则不受影响。
+- 用户 2026-09-25 的机器上实测:插件服务和它启动的引擎写的勾选记录、请求统计、语言记录都在 Codex 的私有目录里,有 6 份勾选记录只在那里;在 Claude 里跑的自测写的日志在 Claude 的私有目录里。在 Codex 外面运行的 `cam status` 看不到这些文件。
+- 后果:一行命令安装时在 Codex 外面启动的引擎,如果在用户重启 Codex 后继续被使用(重启得快时会这样),它看不到面板新写的勾选,取消的图会照样发出去,而且没有提示。
+- 用户目录本身不会被转存;Codex 自己的 `~\.codex` 就放在那里。
+
+### 设计
+
+- Windows 的数据目录改为 `%USERPROFILE%\.codex-attachment-manager`。macOS、Linux 不变。
+- 迁移(`migrate-data.ts`):插件服务、引擎和命令行在碰数据目录之前,先检查一次;没迁移过,就从旧目录和每个应用私有目录里的副本,把勾选记录、面板切换记录、请求统计、语言记录和 config 备份复制过来,同名文件取最新的一份,并保留原来的修改时间。日志和缓存不搬,旧目录原样保留。
+ - Codex 会同时启动好几个插件服务:用一个锁文件保证只有一个在复制,其他的等它完成;锁文件超过一分钟没动,就当作复制的进程已经中断,接手重做。
+ - 设了 `CAM_DATA_DIR`(测试、开发)时不迁移。
+- `uninstall.ps1` 的配置备份也改放到新目录。
diff --git a/docs/spec.md b/docs/spec.md
index 3b21922..b52334c 100644
--- a/docs/spec.md
+++ b/docs/spec.md
@@ -98,7 +98,7 @@
- 官方 app-server 在创建和运行任务时自己产生的正常写入;
- 为接入本地代理和面板服务,工具可以修改 `config.toml` 里由它自己管理、带标记的设置(用户 2026-09-24 授权)。改之前先备份,能一键恢复,其他设置一律不动。只在这些时候改:用户运行安装或卸载命令;用户在面板里点“启用”或“停用”;插件被移除或关掉、又没先停用时,引擎把 Codex 恢复直连(v0.1);
- 为让生图和改图也能连到本地代理,工具可以在 `~/.codex/.env` 里写一段带标记的 `NO_PROXY`,只含本机地址,并保留用户自己的条目(用户 2026-09-24 同意)。`.env` 可能含密钥,工具不复制、不输出它,只增删自己的那一段;文件里只剩这一段时,恢复时连文件一起删掉。
-- 真实数据不进仓库:从真实会话和用户素材里提取的图片、记录片段、日志和备份,只能放在本机,开发时放在 `local/` 目录(已被 git 忽略),装好的插件放在数据目录(Windows 是 `%LOCALAPPDATA%\codex-attachment-manager`,macOS 是 `~/Library/Application Support/codex-attachment-manager`,Linux 是 `~/.local/share/codex-attachment-manager`);任何时候都不提交。仓库里的样例数据只能是合成的或脱敏的。
+- 真实数据不进仓库:从真实会话和用户素材里提取的图片、记录片段、日志和备份,只能放在本机,开发时放在 `local/` 目录(已被 git 忽略),装好的插件放在数据目录(Windows 是 `%USERPROFILE%\.codex-attachment-manager`,macOS 是 `~/Library/Application Support/codex-attachment-manager`,Linux 是 `~/.local/share/codex-attachment-manager`);任何时候都不提交。仓库里的样例数据只能是合成的或脱敏的。Windows 的数据目录 2026-09-25 从 `%LOCALAPPDATA%` 下挪出来(用户同意):Codex、Claude 这类微软商店应用启动的程序在 AppData 里新建的文件,会被转存到各应用的私有目录,插件服务、引擎和命令行看到的数据就不一样了。
- 仓库里的文档和代码不写用户本机的具体路径(例如个人项目文件夹)。`~/.codex`、`%LOCALAPPDATA%` 这类通用位置可以写。本项目以后会开源。
- 不改写原生任务的历史记录。
- 代理只替换用户取消的图片,连同 Codex 附在它前后的路径标签;有图片被省略时,另外插入一条“上下文管理说明”开发者消息(用户 2026-09-25 要求告诉模型原因,见第 1 节“占位符”)。请求里的其他内容原样转发。
diff --git a/docs/tasks.md b/docs/tasks.md
index 12de58d..38b2ebc 100644
--- a/docs/tasks.md
+++ b/docs/tasks.md
@@ -73,7 +73,10 @@
- 面板跟随 Codex 的界面语言;发给模型的说明也跟着换,中文原样不动;
- 插件页英文为主、附一段中文;标签页标题跟着语言;命令行按系统语言;
- 英文自测:模型不收回回答;被问到没说过的细节时回复“need IMG-001”,没有编造。
- - 待用户重装后确认:Codex 会不会把界面语言告诉面板(`cam status` 看来源)。
+ - 用户 2026-09-25 实测通过:面板跟着 Codex 切到英文;Codex 确实把界面语言告诉了面板(记录里的来源是 codex)。当时 `cam status` 看不到这份记录,原因见 v0.1-15。
+- [x] **v0.1-15 Windows 的数据目录挪出 AppData**(用户 2026-09-25 同意;`paths.ts`、`migrate-data.ts`、`uninstall.ps1`;设计见 plan.md 第 17 节;证据:3 个单元测试)
+ - 起因:收尾检查语言记录时发现,Codex、Claude 启动的程序写的数据被 Windows 转存到了各自的私有目录,插件服务、引擎和命令行看到的数据不一样。
+ - 数据目录改为 `%USERPROFILE%\.codex-attachment-manager`;第一次启动时把旧目录和各私有副本里的数据合并过来,同名文件取最新的。
- [x] **v0.1-7 许可证和 CI**(用户 2026-09-25 定:MIT,并加上 CI):`LICENSE`(插件目录里也放一份);`.github/workflows/ci.yml` 在 Windows、macOS、Linux 上跑单元测试;问题反馈模板,方便 macOS、Linux 用户在 GitHub 上报问题。
- 待定:Windows 上不先停用就移除插件,Codex 会断连,只能靠卸载命令恢复;要不要用计划任务让引擎不依赖插件启动,见 spec.md 第 5 节。
diff --git a/plugin/src/cam.ts b/plugin/src/cam.ts
index d7c02fb..36b05c0 100644
--- a/plugin/src/cam.ts
+++ b/plugin/src/cam.ts
@@ -22,6 +22,7 @@ import { buildOf, DEFAULT_PORT, engineHealth, ensureEngine } from "./engine.ts";
import { isEntryPoint } from "./entry.ts";
import { storedLang, systemLang } from "./language.ts";
import { say, type MessageKey } from "./messages.ts";
+import { migrateDataOnce } from "./migrate-data.ts";
import { MARKETPLACE, PLUGIN, PLUGIN_ID, planInstall, pluginStatus } from "./install.ts";
import { applySelection, loadPanelState, type PanelState } from "./panel-state.ts";
import { dataDir, selectionDirOf } from "./paths.ts";
@@ -135,6 +136,7 @@ function migrateSelections(): number {
}
async function main(): Promise<void> {
+ migrateDataOnce(); // v0.1-15: Windows moved the data folder
const [command, threadId, ...ids] = process.argv.slice(2);
// The same port the plugin's MCP server uses: --port, else CAM_ENGINE_PORT, else the default.
const port = Number(process.argv.includes("--port") ? process.argv[process.argv.indexOf("--port") + 1] : process.env.CAM_ENGINE_PORT ?? DEFAULT_PORT);
diff --git a/plugin/src/migrate-data.ts b/plugin/src/migrate-data.ts
new file mode 100644
index 0000000..aefe182
--- /dev/null
+++ b/plugin/src/migrate-data.ts
@@ -0,0 +1,82 @@
+// Purpose: v0.1-15 — on Windows the tool's data moved from %LOCALAPPDATA%\codex-attachment-manager to
+// %USERPROFILE%\.codex-attachment-manager (paths.ts says why). The old data may be split over several folders: the old
+// folder itself, and a private copy for each Microsoft Store app whose programs wrote there
+// (%LOCALAPPDATA%\Packages\<app>\LocalCache\Local\codex-attachment-manager). Once, before anything reads the new
+// folder, this copies the selections, the panels' task switches, the request statistics, the remembered language and
+// the config backups from all of them, the newest copy of each file winning. Logs and caches stay behind, and the old
+// folders are left as they are.
+// Input: LOCALAPPDATA and the new data folder. Output: copied files; <data dir>/migrated.json.
+
+import { copyFileSync, existsSync, mkdirSync, readdirSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs";
+import { dirname, join, resolve } from "node:path";
+import { dataDir } from "./paths.ts";
+
+const NAME = "codex-attachment-manager";
+const KEPT_FOLDERS = ["selection", "bindings", join("state", "requests"), "config-backup"];
+const KEPT_FILES = ["language.json"];
+
+export function oldWindowsFolders(localAppData: string | undefined): string[] {
+ if (!localAppData) return [];
+ const folders = [join(localAppData, NAME)];
+ let apps: string[] = [];
+ try { apps = readdirSync(join(localAppData, "Packages")); } catch { /* no Store apps */ }
+ for (const app of apps) folders.push(join(localAppData, "Packages", app, "LocalCache", "Local", NAME));
+ return folders.filter((folder) => existsSync(folder));
+}
+
+function filesIn(folder: string, sub: string): string[] {
+ try {
+ return readdirSync(join(folder, sub), { withFileTypes: true }).filter((entry) => entry.isFile()).map((entry) => join(sub, entry.name));
+ } catch { return []; }
+}
+
+const pause = (ms: number) => Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
+
+// Codex starts several plugin services at once: one of them copies, the others wait for it (but not for one that
+// died halfway: a lock older than a minute is taken over).
+export function migrateWindowsData(target = dataDir(), localAppData = process.env.LOCALAPPDATA, waitMs = 5000): { copied: number; sources: string[] } | null {
+ const done = join(target, "migrated.json");
+ if (existsSync(done)) return null;
+ mkdirSync(target, { recursive: true });
+ const lock = join(target, ".migrating");
+ const take = () => { try { writeFileSync(lock, String(process.pid), { flag: "wx" }); return true; } catch { return false; } };
+ if (!take()) {
+ for (let waited = 0; waited < waitMs && !existsSync(done); waited += 100) pause(100);
+ if (existsSync(done)) return null;
+ try { if (Date.now() - statSync(lock).mtimeMs < 60_000) return null; } catch { /* gone meanwhile */ }
+ rmSync(lock, { force: true });
+ if (!take()) return null;
+ }
+ try {
+ const sources = oldWindowsFolders(localAppData).filter((folder) => resolve(folder) !== resolve(target));
+ const newest = new Map<string, { from: string; mtimeMs: number }>();
+ for (const folder of sources) {
+ const files = [...KEPT_FOLDERS.flatMap((sub) => filesIn(folder, sub)), ...KEPT_FILES.filter((file) => existsSync(join(folder, file)))];
+ for (const file of files) {
+ const { mtimeMs } = statSync(join(folder, file));
+ if ((newest.get(file)?.mtimeMs ?? -1) < mtimeMs) newest.set(file, { from: join(folder, file), mtimeMs });
+ }
+ }
+ let copied = 0;
+ for (const [file, { from, mtimeMs }] of newest) {
+ const to = join(target, file);
+ if (existsSync(to) && statSync(to).mtimeMs >= mtimeMs) continue;
+ mkdirSync(dirname(to), { recursive: true });
+ copyFileSync(from, to);
+ // Kept, so a later "newest wins" between copies still means something.
+ utimesSync(to, new Date(), new Date(mtimeMs));
+ copied++;
+ }
+ writeFileSync(done, JSON.stringify({ at: new Date().toISOString(), sources, copied }, null, 2));
+ return { copied, sources };
+ } finally {
+ rmSync(lock, { force: true });
+ }
+}
+
+// Called by every entry point (plugin service, engine, command line) before it touches the data folder. Only Windows
+// had the old folder; CAM_DATA_DIR (tests, development) means a folder of its own.
+export function migrateDataOnce(): void {
+ if (process.platform !== "win32" || process.env.CAM_DATA_DIR?.trim()) return;
+ try { migrateWindowsData(); } catch { /* best effort: the new folder works without the old data */ }
+}
diff --git a/plugin/src/paths.ts b/plugin/src/paths.ts
index 64a65ac..e2352ee 100644
--- a/plugin/src/paths.ts
+++ b/plugin/src/paths.ts
@@ -1,9 +1,14 @@
// Purpose: one place for where the tool keeps its own data (selection state, request statistics, logs, backups; since
// v0.1-10 which task a panel was switched to, and v0.1-11 pixel fingerprints — never image content).
-// Input: CAM_DATA_DIR when set; otherwise a per-user folder: %LOCALAPPDATA%\codex-attachment-manager on Windows,
+// Input: CAM_DATA_DIR when set; otherwise a per-user folder: %USERPROFILE%\.codex-attachment-manager on Windows,
// ~/Library/Application Support/codex-attachment-manager on macOS, $XDG_DATA_HOME (or ~/.local/share)/
// codex-attachment-manager on Linux. Never inside the plugin folder, which Codex replaces on every update, and never
// under ~/.codex.
+// v0.1-15: Windows used %LOCALAPPDATA%\codex-attachment-manager until then. Codex and Claude are Microsoft Store (MSIX)
+// apps, and new files that programs they start write under AppData go to each app's private folder, so the plugin
+// service, the engine and the command line could each see different data there (a selection made in the panel was
+// missing for an engine started outside Codex). The profile folder is not redirected; Codex keeps ~\.codex there too.
+// migrate-data.ts brings the old data over.
// Output: absolute directory paths. Nothing here writes anything.
import { homedir } from "node:os";
@@ -12,7 +17,7 @@ import { join } from "node:path";
export function dataDir(env: NodeJS.ProcessEnv = process.env, platform: NodeJS.Platform = process.platform, home = homedir()): string {
const chosen = env.CAM_DATA_DIR?.trim();
if (chosen) return chosen;
- if (platform === "win32") return join(env.LOCALAPPDATA || join(home, "AppData", "Local"), "codex-attachment-manager");
+ if (platform === "win32") return join(home, ".codex-attachment-manager");
if (platform === "darwin") return join(home, "Library", "Application Support", "codex-attachment-manager");
return join(env.XDG_DATA_HOME || join(home, ".local", "share"), "codex-attachment-manager");
}
diff --git a/plugin/src/plugin-server.ts b/plugin/src/plugin-server.ts
index 6233e97..3182fd1 100644
--- a/plugin/src/plugin-server.ts
+++ b/plugin/src/plugin-server.ts
@@ -24,6 +24,7 @@ import { DEFAULT_PORT, ensureEngine } from "./engine.ts";
import { isEntryPoint } from "./entry.ts";
import { currentLang, langOf, rememberLang, type Lang } from "./language.ts";
import { say } from "./messages.ts";
+import { migrateDataOnce } from "./migrate-data.ts";
import { applySelection, imageFor, loadPanelState, type PanelState } from "./panel-state.ts";
import { dataDir, pixelCacheDirOf } from "./paths.ts";
import { connectDirectly, useEngine, usesEngine } from "./setup.ts";
@@ -190,6 +191,7 @@ export function callTool(name: string, args: Json, meta: Json | undefined, sessi
}
function main(): void {
+ migrateDataOnce(); // v0.1-15: Windows moved the data folder
const send = (message: Json) => process.stdout.write(`${JSON.stringify({ jsonrpc: "2.0", ...message })}\n`);
let engineState = "";
const supervise = async () => {
diff --git a/plugin/src/proxy.ts b/plugin/src/proxy.ts
index 7e13ae8..41f2aa3 100644
--- a/plugin/src/proxy.ts
+++ b/plugin/src/proxy.ts
@@ -25,6 +25,7 @@ import { buildOf, DEFAULT_PORT, ENGINE_SERVICE, engineHealth, versionOf, watchFo
import { findImages, type ImageRef } from "./images.ts";
import { isEntryPoint } from "./entry.ts";
import { currentLang } from "./language.ts";
+import { migrateDataOnce } from "./migrate-data.ts";
import { dataDir, pixelCacheDirOf, proxyLogDirOf } from "./paths.ts";
import { recordRequest } from "./request-stats.ts";
import { rewriteItems, type Described } from "./rewrite.ts";
@@ -225,6 +226,7 @@ export function rewriteBody(original: Buffer, encoding: string | undefined, thre
}
async function main(): Promise<void> {
+ migrateDataOnce(); // v0.1-15: Windows moved the data folder
const portIndex = process.argv.indexOf("--port");
const port = Number(portIndex >= 0 ? process.argv[portIndex + 1] : DEFAULT_PORT);
// One engine per machine: a second start leaves the running one alone.
diff --git a/scripts/uninstall.ps1 b/scripts/uninstall.ps1
index fe49b61..63925aa 100644
--- a/scripts/uninstall.ps1
+++ b/scripts/uninstall.ps1
@@ -49,7 +49,7 @@
}
$codexHome = if ($env:CODEX_HOME) { $env:CODEX_HOME } else { Join-Path $HOME '.codex' }
- $dataDir = if ($env:CAM_DATA_DIR) { $env:CAM_DATA_DIR } else { Join-Path $env:LOCALAPPDATA $name }
+ $dataDir = if ($env:CAM_DATA_DIR) { $env:CAM_DATA_DIR } else { Join-Path $env:USERPROFILE ('.' + $name) }
$config = Join-Path $codexHome 'config.toml'
$dotenv = Join-Path $codexHome '.env'
diff --git a/spike/src/migrate-data.test.ts b/spike/src/migrate-data.test.ts
new file mode 100644
index 0000000..788631d
--- /dev/null
+++ b/spike/src/migrate-data.test.ts
@@ -0,0 +1,78 @@
+// Purpose: v0.1-15 — Windows moves the tool's data out of AppData: the old folder and every Store app's private copy
+// are merged into the new folder once, the newest copy of each file winning; logs stay behind; several processes
+// starting at once copy only once, and a lock left by a crashed one is taken over.
+// Input: synthetic folders in a temporary LOCALAPPDATA; output: Node test assertions only.
+
+import assert from "node:assert/strict";
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, statSync, utimesSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { dirname, join } from "node:path";
+import test from "node:test";
+import { migrateWindowsData, oldWindowsFolders } from "../../plugin/src/migrate-data.ts";
+
+const NAME = "codex-attachment-manager";
+
+function put(file: string, text: string, minutesAgo: number): void {
+ mkdirSync(dirname(file), { recursive: true });
+ writeFileSync(file, text);
+ const at = new Date(Date.now() - minutesAgo * 60_000);
+ utimesSync(file, at, at);
+}
+
+function layout() {
+ const local = mkdtempSync(join(tmpdir(), "cam-localappdata-"));
+ const old = join(local, NAME);
+ const codex = join(local, "Packages", "OpenAI.Codex_x", "LocalCache", "Local", NAME);
+ const claude = join(local, "Packages", "Claude_y", "LocalCache", "Local", NAME);
+ mkdirSync(join(local, "Packages", "Other_z", "LocalCache", "Local"), { recursive: true });
+ put(join(old, "selection", "A.json"), "old A", 30);
+ put(join(old, "state", "requests", "R.json"), "R", 20);
+ put(join(old, "language.json"), "old language", 40);
+ put(join(old, "plugin-server.jsonl"), "a log", 1);
+ put(join(codex, "selection", "A.json"), "codex A", 10);
+ put(join(codex, "selection", "B.json"), "B", 12);
+ put(join(codex, "bindings", "X.json"), "X", 15);
+ put(join(codex, "language.json"), "codex language", 5);
+ put(join(codex, "cache", "pixels", "ab", "ab01"), "fingerprint", 5);
+ put(join(claude, "selection", "C.json"), "C", 8);
+ put(join(claude, "config-backup", "config.toml.2026-09-25T10-00-00.000Z"), "backup", 60);
+ return { local, old, codex, claude, target: join(mkdtempSync(join(tmpdir(), "cam-profile-")), `.${NAME}`) };
+}
+
+test("the old folder and every Store app's private copy are merged once, the newest copy of each file winning", () => {
+ const { local, old, codex, claude, target } = layout();
+ assert.deepEqual(oldWindowsFolders(local).sort(), [old, claude, codex].sort(), "only folders that exist");
+ const result = migrateWindowsData(target, local);
+ assert.equal(result?.copied, 7);
+ const read = (file: string) => readFileSync(join(target, file), "utf8");
+ assert.deepEqual([read("selection/A.json"), read("selection/B.json"), read("selection/C.json"), read("bindings/X.json"), read("state/requests/R.json"), read("language.json")],
+ ["codex A", "B", "C", "X", "R", "codex language"]);
+ assert.equal(read("config-backup/config.toml.2026-09-25T10-00-00.000Z"), "backup");
+ assert.equal(existsSync(join(target, "plugin-server.jsonl")), false, "logs stay behind");
+ assert.equal(existsSync(join(target, "cache")), false, "caches are rebuilt");
+ assert.ok(Math.abs(statSync(join(target, "selection", "A.json")).mtimeMs - statSync(join(codex, "selection", "A.json")).mtimeMs) < 2000, "the copy keeps its time");
+ assert.ok(existsSync(join(target, "migrated.json")));
+ assert.equal(migrateWindowsData(target, local), null, "once only");
+ assert.equal(readFileSync(join(old, "selection", "A.json"), "utf8"), "old A", "the old folders are left as they are");
+});
+
+test("while another process copies, the others wait for it; a lock left by a crashed one is taken over", () => {
+ const busy = layout();
+ mkdirSync(busy.target, { recursive: true });
+ writeFileSync(join(busy.target, ".migrating"), "1234");
+ assert.equal(migrateWindowsData(busy.target, busy.local, 200), null, "a fresh lock: someone else is copying");
+ assert.equal(existsSync(join(busy.target, "migrated.json")), false);
+ const stale = layout();
+ mkdirSync(stale.target, { recursive: true });
+ put(join(stale.target, ".migrating"), "1234", 2);
+ assert.equal(migrateWindowsData(stale.target, stale.local, 200)?.copied, 7);
+ assert.equal(existsSync(join(stale.target, ".migrating")), false);
+});
+
+test("without old folders there is nothing to copy, and the new folder is marked anyway", () => {
+ const target = join(mkdtempSync(join(tmpdir(), "cam-profile-")), `.${NAME}`);
+ const empty = mkdtempSync(join(tmpdir(), "cam-localappdata-"));
+ assert.deepEqual(oldWindowsFolders(""), []);
+ assert.deepEqual(migrateWindowsData(target, empty), { copied: 0, sources: [] });
+ assert.equal(migrateWindowsData(target, empty), null);
+});
diff --git a/spike/src/platform.test.ts b/spike/src/platform.test.ts
index f537618..3493995 100644
--- a/spike/src/platform.test.ts
+++ b/spike/src/platform.test.ts
@@ -12,7 +12,7 @@ import { dataDir } from "../../plugin/src/paths.ts";
const home = join("/", "home", "someone");
test("the data folder follows each platform's convention, and CAM_DATA_DIR wins", () => {
- assert.equal(dataDir({ LOCALAPPDATA: join("C:", "Users", "someone", "AppData", "Local") }, "win32", home), join("C:", "Users", "someone", "AppData", "Local", "codex-attachment-manager"));
+ assert.equal(dataDir({ LOCALAPPDATA: join("C:", "Users", "someone", "AppData", "Local") }, "win32", home), join(home, ".codex-attachment-manager"), "not under AppData, which Store apps redirect");
assert.equal(dataDir({}, "darwin", home), join(home, "Library", "Application Support", "codex-attachment-manager"));
assert.equal(dataDir({}, "linux", home), join(home, ".local", "share", "codex-attachment-manager"));
assert.equal(dataDir({ XDG_DATA_HOME: join(home, "data") }, "linux", home), join(home, "data", "codex-attachment-manager"));