Repository navigation
fix(host): 读取 Desktop 新的 SQLite 全局状态,Web 侧栏的项目分区与归属不再停在旧快照 / read Desktop's SQLite global state - #1202
Conversation
Desktop leaves its JSON snapshot behind after migration. Read the active SQLite store for workspace and remote project metadata. Use the legacy JSON when the database is absent or still at version 0.
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
|
感谢贡献!我们已在本机真实 Codex Desktop SQLite 全局状态(对比已冻结在 11-24 的旧 JSON,SQLite 成功读出 11-26 最新的项目、归属与无项目会话,WAL/DELETE 模式均无锁冲突或残留文件)和隔离 Web 环境下完成实测,10/10 本地项目按 |
摘要
当前 Desktop(本机 26.1120)把全局状态从
CODEX_HOME/.codex-global-state.json迁到了CODEX_HOME/.codex-global-state.sqlite。迁移在同一个事务里完成三件事:建表settings(scope, key, value),导入旧 JSON,把user_version设为 1。此后 Desktop 只写 SQLite,旧 JSON 留在原处,不再更新。codexhost 有两处仍然只读这份 JSON:
readNativeWorkspaceSnapshot()(codexhost/workspace/read)。项目列表已经来自官方project/list,但以下内容仍取自 JSON:electron-owner-atoms:<owner>→sidebar-custom-sections-v3)readRemoteProjectMetadata():读取远程项目及其会话归属。于是 Web 停在迁移那一刻的快照。迁移后新建、在 Desktop 中放进自定义分区的项目,Web 把它显示在「工作区」里;迁移后在 Desktop 中移动过的会话,Web 也不会更新。
这个 PR 在
desktop-control中新增readDesktopGlobalState(),上述两处共用:.codex-global-state.sqlite存在且user_version为 1 时,以只读方式读取,并按 Desktop 的规则还原成原来的 JSON 结构:global行还原为顶层键。atom行归入electron-persisted-atom-state。electron-owner-atoms:<owner>)各自还原为一个对象,并覆盖同名的顶层键。真实数据库里这些同名顶层键的值是{}。user_version为 0,即 Desktop 尚未完成迁移,此时 JSON 仍是有效数据。node:sqlite改为动态加载,沿用 antigravity 适配器的做法。docs/architecture/host-client-channel.md和docs/product/standalone-web.md更新了数据来源的说明。关联 Issue
无 / N/A
测试
基于
main(0e5029124),macOS 27.2.1(arm64),Node 26.0.0;Host 运行在 Node 24 上。npx tsc -b packages/desktop-control packages/host-runtime:通过。npx vitest run --config tests/vitest.config.js packages/host-runtime/test/projectless-workspace.test.ts packages/desktop-control/test/remote-project-metadata.test.ts:12 个通过。新用例覆盖四种情况:user_version为 0 时读取 JSON。去掉两个调用方的改动后,有 3 个新用例失败。
user_version为 0 的用例在旧代码下本来就会通过,它保护的是新代码的兼容路径。改动文件
prettier --check和node tools/check-boundaries.mjs通过,eslint没有报错。本机 Desktop 状态,修改前(JSON)与修改后(SQLite)只读对比如下:
修改后的
readExternalWorkspaceSnapshot()读取真实数据库(约 1,800 行、2.3 MB)一次约 14 ms。分区 1 多出的那个项目,在 Desktop 中也位于该分区。实机:修改前,用无头 Chrome 打开本机 Web(已安装的 0.27.0),该项目显示在「工作区」,不在它在 Desktop 中所属的分区。修改后没有在真实 Web 中验证,因为没有启动隔离的 Host 和 Web。
已知限制:
node:sqlite是同步接口。Desktop 正在提交写入时,读取最多等待 3 秒(人为持有排它锁时,3.12 秒后返回SQLITE_BUSY)。Desktop 每次写入通常只占锁几毫秒。Windows 未测试。UI Demo
这是示意图,不是 Web 实机截图。做法是用修改前后的读取函数处理同一组合成文件(一份过期的 JSON 和一份较新的 SQLite),再把分区结果画成简化的侧栏。Project 2 只在 SQLite 中属于 Section A:修复前它显示在「工作区」,修复后显示在 Section A 中。
Summary
The current Desktop (26.1120 on this machine) moved its global state from
CODEX_HOME/.codex-global-state.jsontoCODEX_HOME/.codex-global-state.sqlite. One transaction does the migration: it creates the tablesettings(scope, key, value), imports the old JSON and setsuser_versionto 1. After that, Desktop writes only SQLite. The old JSON stays where it is and is no longer updated.Two places in codexhost still read only that JSON:
readNativeWorkspaceSnapshot()(codexhost/workspace/read). The project list already comes from the officialproject/list, but these still come from the JSON:electron-owner-atoms:<owner>→sidebar-custom-sections-v3)readRemoteProjectMetadata(): remote projects and their Thread assignments.As a result, the Web is frozen at the moment of migration. A project created after migration and placed in a Desktop custom section shows under "工作区" in the Web, and Threads moved in Desktop after migration are not updated in the Web either.
This PR adds
readDesktopGlobalState()todesktop-control, shared by both places:.codex-global-state.sqliteexists anduser_versionis 1, it reads the database read-only and rebuilds the old JSON shape the way Desktop does:globalrows become top-level keys.atomrows go underelectron-persisted-atom-state.electron-owner-atoms:<owner>) becomes one object and overrides the top-level key of the same name. In the real database, those same-named top-level keys hold{}.user_versionis 0, meaning Desktop has not finished migrating, so the JSON is still current.node:sqliteis loaded dynamically, following the antigravity adapter.docs/architecture/host-client-channel.mdanddocs/product/standalone-web.mddescribe the new data source.Related issues
N/A
Test plan
On
main(0e50291), macOS 27.2.1 (arm64), Node 26.0.0; the Host runs on Node 24.npx tsc -b packages/desktop-control packages/host-runtime: passes.npx vitest run --config tests/vitest.config.js packages/host-runtime/test/projectless-workspace.test.ts packages/desktop-control/test/remote-project-metadata.test.ts: 12 passed. The new cases cover four situations:user_version0 reads the JSON.Without the change to the two callers, 3 of the new cases fail. The
user_version0 case also passes on the old code; it guards the new code's compatibility path.prettier --checkandnode tools/check-boundaries.mjspass, andeslintreports no errors on the changed files.A read-only comparison of the local Desktop state before (JSON) and after (SQLite):
The changed
readExternalWorkspaceSnapshot()reads the real database (about 1,800 rows, 2.3 MB) in about 14 ms. The extra project in section 1 is also in that section in Desktop.Real machine: before the change, headless Chrome on the local Web (installed 0.27.0) showed that project under "工作区" instead of the section it belongs to in Desktop. After the change, this was not checked in a real Web, because no isolated Host and Web were started.
Known limits:
node:sqliteis synchronous. While Desktop is committing a write, a read waits up to 3 seconds (with an artificially held exclusive lock, it returnedSQLITE_BUSYafter 3.12 seconds). Desktop usually holds the lock for only a few milliseconds per write. Windows is not tested.UI Demo
This is a schematic, not a screenshot of a running Web page. The old and new readers processed the same synthetic files (a stale JSON and a newer SQLite), and the resulting sections were drawn as a simplified sidebar. Project 2 belongs to Section A only in SQLite: before the fix it shows under "工作区", and after the fix it shows in Section A.