Skip to content

fix(host): 读取 Desktop 新的 SQLite 全局状态,Web 侧栏的项目分区与归属不再停在旧快照 / read Desktop's SQLite global state - #1202

Merged
iruriQ merged 1 commit into
BytePioneer-AI:mainfrom
sduhantao-maker:fix/desktop-global-state-sqlite
Nov 26, 2026
Merged

iruriQ merged 1 commit into
BytePioneer-AI:mainfrom
sduhantao-maker:fix/desktop-global-state-sqlite

Conversation

@sduhantao-maker

Copy link
Copy Markdown
Contributor

摘要

当前 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:
    • 会话的项目归属
    • 未选项目标记
    • 旧项目 ID 的别名
    • Desktop 自定义分区中的项目(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。
    • 其他 scope(例如 electron-owner-atoms:<owner>)各自还原为一个对象,并覆盖同名的顶层键。真实数据库里这些同名顶层键的值是 {}。
  • 以下两种情况读取旧 JSON,行为与之前相同:
    • SQLite 文件不存在。
    • user_version 为 0,即 Desktop 尚未完成迁移,此时 JSON 仍是有效数据。
  • 其他版本号、读取失败或文件损坏时明确报错,不回退到可能已经过期的 JSON。
  • 读取过程:
    • 不创建、迁移或写入任何文件。
    • 忙等待 3 秒,与 Desktop 相同。
    • 版本号和全部行在同一个读事务中取得。
  • 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 个通过。新用例覆盖四种情况:

    • JSON 已过期、SQLite 较新时,以下内容都取自 SQLite:分区项目(通过只存在于 SQLite 的旧 ID 别名解析)、会话归属、未选项目标记。
    • 远程项目在 SQLite 存在时取自 SQLite。
    • user_version 为 0 时读取 JSON。
    • 版本 2 时报错,即使存在有效的 JSON。
  • 去掉两个调用方的改动后,有 3 个新用例失败。user_version 为 0 的用例在旧代码下本来就会通过,它保护的是新代码的兼容路径。

  • 改动文件 prettier --check 和 node tools/check-boundaries.mjs 通过,eslint 没有报错。

  • 本机 Desktop 状态,修改前(JSON)与修改后(SQLite)只读对比如下:

    项目 旧 JSON SQLite
    分区 1 中的项目 6 7
    分区 2 中的项目 7 7
    会话项目归属 239 245
    未选项目的会话 171 174
    旧项目 ID 别名 21 23

    修改后的 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

修复前 Project 2 显示在「工作区」,修复后显示在 Section A 中

这是示意图,不是 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.json to CODEX_HOME/.codex-global-state.sqlite. One transaction does the migration: it creates the table settings(scope, key, value), imports the old JSON and sets user_version to 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 official project/list, but these still come from the JSON:
    • Thread project assignments
    • projectless markers
    • legacy project ID aliases
    • the projects in Desktop's custom sections (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() to desktop-control, shared by both places:

  • When .codex-global-state.sqlite exists and user_version is 1, it reads the database read-only and rebuilds the old JSON shape the way Desktop does:
    • global rows become top-level keys.
    • atom rows go under electron-persisted-atom-state.
    • Every other scope (such as 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 {}.
  • It reads the old JSON in two cases, with unchanged behavior:
    • the SQLite file does not exist;
    • user_version is 0, meaning Desktop has not finished migrating, so the JSON is still current.
  • Any other version, a read failure or a damaged file fails clearly, with no fallback to a JSON that may be stale.
  • How it reads:
    • It never creates, migrates or writes any file.
    • It waits up to 3 seconds on a busy database, as Desktop does.
    • It takes the version and all rows in one read transaction.
  • node:sqlite is loaded dynamically, following the antigravity adapter.
  • Downstream parsing is unchanged.
  • docs/architecture/host-client-channel.md and docs/product/standalone-web.md describe 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:

    • With a stale JSON and a newer SQLite, all of these come from SQLite: section projects (resolved through a legacy ID alias that exists only in SQLite), Thread assignments and projectless markers.
    • Remote projects come from SQLite when it exists.
    • user_version 0 reads the JSON.
    • Version 2 fails even when a valid JSON exists.
  • Without the change to the two callers, 3 of the new cases fail. The user_version 0 case also passes on the old code; it guards the new code's compatibility path.

  • prettier --check and node tools/check-boundaries.mjs pass, and eslint reports no errors on the changed files.

  • A read-only comparison of the local Desktop state before (JSON) and after (SQLite):

    Item Old JSON SQLite
    Projects in section 1 6 7
    Projects in section 2 7 7
    Thread project assignments 239 245
    Projectless Threads 171 174
    Legacy project ID aliases 21 23

    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:sqlite is synchronous. While Desktop is committing a write, a read waits up to 3 seconds (with an artificially held exclusive lock, it returned SQLITE_BUSY after 3.12 seconds). Desktop usually holds the lock for only a few milliseconds per write. Windows is not tested.

UI Demo

Before: Project 2 shows under 工作区; after: it shows in Section A

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.

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.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • 功能更新
    • 桌面项目与工作区元数据现在优先从 SQLite 全局状态读取;数据库不存在或版本为 0 时,回退读取旧 JSON 文件。
    • 对于不受支持的数据库版本或损坏的数据,读取会报错;两种状态文件均不存在时返回空结果。
  • 文档
    • 更新了共享 Host 和兼容目录的项目元数据读取说明。

新增桌面全局状态读取器,优先从 SQLite 读取状态,并在数据库不存在或版本为 0 时回退到旧 JSON。远程项目元数据和原生工作区快照改用该读取器,相关文档和测试也已更新。

Changes: 桌面全局状态读取迁移

Layer / File(s) Summary
统一读取器与数据源规则
packages/desktop-control/src/desktop-global-state.ts, packages/desktop-control/src/index.ts, tests/fixtures/desktop-global-state.ts, docs/architecture/host-client-channel.md, docs/product/standalone-web.md
新增 readDesktopGlobalState,读取并解析 SQLite 设置;数据库不存在或版本为 0 时读取旧 JSON。文件需为普通文件且不超过 32 MiB。文档更新了相同的数据源规则。
远程项目元数据读取
packages/desktop-control/src/remote-project-metadata.ts, packages/desktop-control/test/remote-project-metadata.test.ts
远程项目元数据改用统一读取器。测试覆盖 JSON 和 SQLite 两种来源,并断言返回相同的远程项目及线程分配。
工作区快照读取
packages/host-runtime/src/native-workspace-snapshot.ts, packages/host-runtime/test/projectless-workspace.test.ts
原生工作区快照改用统一读取器。测试覆盖迁移后 SQLite 读取、版本 0 的 JSON 回退,以及版本 2 的错误。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Workspace as 工作区快照
  participant Reader as readDesktopGlobalState
  participant SQLite as 全局状态 SQLite
  participant JSON as 旧版 JSON
  Workspace->>Reader: 请求全局状态
  Reader->>SQLite: 读取数据库版本和设置行
  alt 数据库不存在或版本为 0
    Reader->>JSON: 读取旧版状态
    JSON-->>Reader: 返回 JSON 状态
  else 数据库版本为 1
    SQLite-->>Reader: 返回设置行
  end
  Reader-->>Workspace: 返回状态对象或错误
Loading

Suggested reviewers: mouzhi, bytepioneer-ai

Merge Risk: Minimal · up to b478e

Workspace snapshots and remote project metadata now read Desktop's current SQLite state instead of a possibly stale JSON snapshot. Unsupported or damaged databases fail visibly rather than silently falling back to old data. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题描述了 PR 的主要改动:让 Host 读取 Desktop 新的 SQLite 全局状态(desktop-control 新增 readDesktopGlobalState,并由工作区快照与远程项目元数据共用)。标题中的“Web 侧栏的项目分区与归属不再停在旧快照”也对应了修复的实际效果。标题较长,但与变更集直接相关。
Description check Passed 描述与变更集高度相关。它说明了 Desktop 从 JSON 迁移到 SQLite 的背景,列出了 readNativeWorkspaceSnapshot 与 readRemoteProjectMetadata 两个调用方,说明新增的 readDesktopGlobalState 读取规则(版本 0 回退 JSON、其他版本报错),并给出测试结果与已知限制。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0e50291 and b478e5a.

Files selected for processing: 9

  • docs/architecture/host-client-channel.md
  • docs/product/standalone-web.md
  • packages/desktop-control/src/desktop-global-state.ts
  • packages/desktop-control/src/index.ts
  • packages/desktop-control/src/remote-project-metadata.ts
  • packages/desktop-control/test/remote-project-metadata.test.ts
  • packages/host-runtime/src/native-workspace-snapshot.ts
  • packages/host-runtime/test/projectless-workspace.test.ts
  • tests/fixtures/desktop-global-state.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

Recent review details

Context from checks skipped due to timeout: 8

  • GitHub Check: CodeRabbit
  • GitHub Check: Check macos-26
  • GitHub Check: Web Host tests
  • GitHub Check: Check ubuntu-22.04
  • GitHub Check: Static checks
  • GitHub Check: Check windows-latest
  • GitHub Check: Check Linux ARM64
  • GitHub Check: Vitest
Additional context used

Code guidelines: 2

CLAUDE.md — auto-discovered
AGENTS.md — auto-discovered
Path-based instructions: 2

Source excerpt: Host Runtime loads installed plugins through public contracts, not direct imports of concrete Adapter packages.

CodeRabbit inference engine (CLAUDE.md)

Files:

  • packages/host-runtime/src/native-workspace-snapshot.ts
  • packages/host-runtime/test/projectless-workspace.test.ts
Source excerpt: Use package public exports for cross-package dependencies.

CodeRabbit inference engine (AGENTS.md)

Files:

  • packages/desktop-control/src/desktop-global-state.ts
  • packages/desktop-control/src/index.ts
  • packages/desktop-control/src/remote-project-metadata.ts
  • packages/desktop-control/test/remote-project-metadata.test.ts
  • packages/host-runtime/src/native-workspace-snapshot.ts
  • packages/host-runtime/test/projectless-workspace.test.ts
Learnings: 1

Common learnings

Learnt from: CR
Repo: BytePioneer-AI/codex-host PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-10-31T06:53:42.652Z
Learning: Source excerpt:
# Repository Guidelines

## Testing & Completion

- A change is complete only when implementation, contracts, tests, and affected documentation agree.
Learnt from: CR
Repo: BytePioneer-AI/codex-host PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-11-23T12:41:24.916Z
Learning: Source excerpt:
# Repository Guidelines

## Testing & Completion

- A change is complete only when implementation, contracts, tests, and affected documentation agree.
Learnt from: CR
Repo: BytePioneer-AI/codex-host PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-11-20T16:03:15.386Z
Learning: Source excerpt:
# Repository Guidelines

## Testing & Completion

- For core logic or major feature changes, update the corresponding feature document in `docs/`, creating one only when needed. Describe current behavior and key constraints; prefer maintaining an existing document over adding a change report. Minor fixes and local refactors need no documentation changes.
Learnt from: CR
Repo: BytePioneer-AI/codex-host PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-11-20T16:03:11.275Z
Learning: Source excerpt:
# Repository Guidelines

## Testing & Completion

- For core logic or major feature changes, update the corresponding feature document in `docs/`, creating one only when needed. Describe current behavior and key constraints; prefer maintaining an existing document over adding a change report. Minor fixes and local refactors need no documentation changes.
LanguageTool

docs/architecture/host-client-channel.md

[uncategorized] ~42-~42: 您的意思是“"不"写”?
Context: ...顶列表的事实源;完整成员与顺序通过 pins/list 查询。不建立预置顶、补写队列或 Renderer 回退;原生能力不可用时明确失败,未知写入结果不自动重...

(BU)

docs/product/standalone-web.md

[uncategorized] ~474-~474: 您的意思是“"不"齐”?
Context: ...会话的准确全量计数。旧 Host 沿用 CDP 读取项目元数据。按实际项目根目录补齐导航条目和项目名称:每个根目录只对应一个条目,作为某个项目首个根目录的目录显示...

(BU)

OpenGrep (1.33.1)

tests/fixtures/desktop-global-state.ts

[ERROR] 17-17: A nonliteral argument is passed to exec/execSync. Identify the receiver: Node child_process methods execute shell commands, while regular-expression and database methods have different contracts. For shell execution, prefer execFile or spawn with an argument array.

(coderabbit.command-injection.exec-js)

Additional comments: 1

packages/desktop-control/src/desktop-global-state.ts: 1

45-45: Functional Correctness

The concern is conditional on the supported Desktop writer using WAL mode. The repository does not establish that premise. Its only WAL-related code and tests concern Antigravity, Cursor, and Hermes databases, not .codex-global-state.sqlite. The Desktop fixture uses default journal mode. The architecture and product documents describe a read-only source with no WAL requirement. The Node.js excerpt does not establish that a read-only open of a WAL database creates -wal or -shm files. The comment does not identify a claim-critical fact that the repository shows, so no change to the reader can be justified from this evidence. Its suggested check, the writer's journal mode, is an external fact that the inspected sources do not resolve.

@iruriQ
iruriQ merged commit 62c9bb7 into BytePioneer-AI:main Nov 26, 2026
8 checks passed
@iruriQ

iruriQ commented Nov 26, 2026

Copy link
Copy Markdown
Collaborator

感谢贡献!我们已在本机真实 Codex Desktop SQLite 全局状态(对比已冻结在 11-24 的旧 JSON,SQLite 成功读出 11-26 最新的项目、归属与无项目会话,WAL/DELETE 模式均无锁冲突或残留文件)和隔离 Web 环境下完成实测,10/10 本地项目按 project-order 正常渲染且零控制台报错,CI 8/8 通过,现已合并。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants