历史文档:2026-09 虚拟项目重构已替代本文的运行模型。当前行为以虚拟项目设计为准。分组表已由分组视图恢复为运行态,新增
task_group_admissions记录首次归组和创建 placement;workspace bootstrap 不再执行。CLI 拥有会话事实,Host 拥有项目与组织态。下文只作历史设计记录。
~/.zcode/v2/tasks-index.sqlite is the app/host-side index for task list queries. ZCode Agent owns core session content and runtime state; the sqlite index owns fast list metadata and product shell state.
The index is not a replacement for ZCode Protocol session snapshots.
tasks-index.sqlite 还容纳 Automation 与 Off-Peak 表。三个 Repo 在开放读写前
复用 runTasksDatabaseMigrations;库级 tasks_schema_migration 对已登记迁移只读校验
checksum;有待应用迁移时通过 BEGIN IMMEDIATE 串行复查并应用,失败回滚且不记成功。
0001_adopt_task_schema 接管八张已有表、历史补列和索引;
0002_provider_selection 收口已裁决的 Automation 转换。之后只追加新迁移,
不得修改已发布 SQL、冻结转换实现或其语义版本。checksum 覆盖声明式 SQL/列清单及
转换语义版本,不使用会被打包器改写的函数文本。
修复原因:R3 的 Controller/Source Repo 自行建表、补列,绕过了
tasks_schema_migration 的 checksum 和事务门禁。结构升级统一由
runTasksDatabaseMigrations 执行,按调用用途选择迁移集合:
base 保持既有 0001–0003;不创建 Controller/Source 专属表。controller-remote 应用 0004_controller_remote_identity,接管
controller_remote_identity_migrations;已有审计记录保留。source-server 应用 0005_source_server_workspace_schema,接管 alias、合并
ledger、conflicts 的当前结构及旧 v1/v2/v3 升级。旧
workspace_migration_schema_version=3 仅保留给旧二进制兼容,正式执行记录以
tasks_schema_migration 为准;接管时遇到未来旧格式版本明确拒绝。三个集合共用同一个物理库的账本和 BEGIN IMMEDIATE 执行器,可按任意初始化顺序
应用各自集合,不在只读取 Source 管理信息时触发无关业务表/模型选择转换。
所有新迁移兼容「表已存在但正式账本未登记」;既有 migration/checksum 不变,失败时
结构与记账一起回滚,不手工补成功记录。校验和必须覆盖冻结 SQL 与结构转换语义。
dc16292fa1 将迁移入口接入每次 Controller 重连与 Source workspace 解析后,已登记
迁移也会申请写锁,绕过原有无待迁移数据的只读路径。真实 WAL 库中另一连接持有写锁时,
父版本约 1–2 ms 完成,目标版本等待约 5 秒后报 database is locked。
修复边界是 App 库迁移执行器与 Source session identity 迁移入口:
BEGIN IMMEDIATE 后重新读取账本;并发初始化由该事务串行化,
结构和成功记录原子提交。checksum 不符仍明确失败,不以锁错误掩盖。Agent 完整启动迁移继续持有原有库级事务;这次只改变重开时的锁获取条件, 不改变 workspace identity、合并计划、UI/RPC 或桌面 continuous / 手机 replayable 边界。
重连 / Source 查询 → 只读账本与 checksum
├─ 全部已登记 → 返回
└─ 存在缺项 → 写锁 → 锁内复查 → 结构升级 + 记账 → 提交
回归以 remoteSchemaMigrations.test.ts 的真实 SQLite 双连接覆盖:已迁移 Controller
无源行重连、Source 管理查询、已有 alias 的 session 解析在另一连接持有 WAL 写锁时均成功;
成功账本/业务审计不变,错误 checksum 仍拒绝。继续执行新库并发初始化和故障回滚用例。
已有远程重连 E2E 操作合同保持不变;按远程矩阵 R3A-P-02,迁移锁分支用真实存储集成
覆盖,不复制各数据库状态的 Electron 用例。
2026-09-15 修复验证:新增三例先在 dc16292fa1 全部复现 database is locked,
再修改入口;App/Source 六个测试文件 60 项、Agent 三个存储/迁移文件 57 项全部通过。
根类型检查、Lint(57 个既有警告,0 错误)、架构检查(0 违规)通过。未运行桌面/手机
实机 E2E 或 Windows/Linux 回归;证据限于 macOS Node 24.14.0 的真实 SQLite 集成。
Controller / Source 待应用迁移 → 同库事务锁 → 结构升级 + 成功账本
↓ 提交后
workspace 数据迁移(仍有独立备份、进度、恢复记录)
CLI db.sqlite 的 zcode_database_identity 由共享的
0024_session_database_identity 定义纳入 schema_migration。Agent 初始化与
Source 合并前接管复用同一 SQL、checksum 和应用原语;Source 只应用该独立附加表迁移,
不代跑 Agent 的其他迁移。建表不生成或覆盖 identity;其值仍由冻结合并计划管理。
旧备份接管也走相同门禁,已登记但结构损坏时明确失败,不重新建表伪装恢复。
未发布迁移的编号按 CLI 清单统一分配:0023_session_soft_delete_outcome 后接
0024_session_database_identity,两者引入版本均为 CLI 0.16.9。迁移测试检查完整
ID、四位数字编号唯一及清单按编号递增,防止不同分支合并后重复占号。此次仅修正
identity 的编号,SQL 与 checksum 不变,不新增旧 ID 别名。开发库先停止旧写入进程并
备份,再在单个事务内核对 ID、checksum 和旧版本,修正 identity ID 及误填的
soft-delete 3.12.0;保留执行时间和业务数据,重复运行不重放迁移。
验证覆盖真实 SQLite 的新库、存量表接管、v2 缺列、重复打开、checksum 不符、 故障回滚、多连接初始化,以及 Agent/Source 两种先后顺序。只改存储结构初始化, 不改 UI/RPC、workspace key、合并裁决或桌面 continuous / 手机 replayable 链路。
b7881b4c2fe08e5f2b50d89db3e9dea96521c795。真实临时 SQLite 中先复现
Controller/Source/Agent 缺少正式记账、Source 不受账本故障注入约束;修复后通过。
追加「旧版本标记缺失但 generation 已存在」的失败用例,确认接管不能清空冻结代际。pnpm typecheck、pnpm lint、pnpm architecture:check --changed 及改动文件格式
检查通过;Agent adapters npm run typecheck 在重建过期 contracts dist 后通过。npm test -- --run:120 文件中 11 失败,1750 项中
87 失败、1659 通过、4 跳过;另有 config suite 缺失模块。它不是本次专项通过记录。
用临时 Vitest loader 恢复 HEAD 的两个迁移模块,5 个关联失败文件仍复现 51 项失败
(模型 options fixture、签名配置、MCP 环境和缺失 catalog 模块)。旧产物导致的
runtime replay 失败在重建后已消失;全量负载下的 session 并发超时在聚焦重跑通过。
其余扩展测试失败未全部完成基线归因,未扩大修复。npm run lint 仍被既有多文件 max-lines 超限阻止;包含原有
900 多行的 migration 定义文件。本次未修改旧 SQL 以拆分/规避该门禁。未操作真实用户数据库,未做桌面/手机实机或 Windows/Linux 联合回归;本次没有 UI/RPC 交互变更,已有远程交互 E2E 的操作步骤不变。Agent schema 发布前需按
回退本次代码时保留新增表/账本和旧兼容标记;不删 schema 行,不重跑已完成的数据合并。 数据迁移故障仍使用原有备份及业务恢复入口,不以手工补成功记录解除阻塞。
任意 Repo 首次打开 → 库级账本/事务 → 所有表就绪 → Repo 正常读写
其他 Repo / 其他进程 → 同一账本校验 → 跳过已应用版本
旧列及时间戳不被 Provider 转换覆盖;正常 Reader/派发只使用新列。
Automation 的 JSON null 表示明确使用默认配置,SQL NULL 表示配置缺失,需要重选。
回滚产生的新旧格式记录不通过重复执行既有迁移追赶,内容仍须可列出、打开。
Off-Peak 缺失 Provider 的旧选择不查询当前账号补猜,不改变 Ticket 或读取时补迁;
缺配置只影响后续执行资格。TaskIndex 既有关联清理与投影维护不属于 Provider 转换。
ZCodeTaskMeta snapshot in meta_json for list rendering.workspaceKey = workspaceIdentity?.trim() || workspacePath.The tasks table is keyed by (workspace_key, task_id).
Core columns:
workspace_keyworkspace_pathworkspace_identitytask_idtitletask_statusprovidermodemodelmigration_sourceforked_from_task_idcreated_atupdated_atunread_atlast_unread_atpinnedarchiveddeletedtitle_overriddensearchable_textmeta_jsonIndexes cover active workspace list queries, pinned list queries, archived list queries, deleted list queries, recent global timeline, and bounded text search.
(workspace_key, task_id) to avoid concurrent metadata overwrites.unread_at is the current unread membership/CAS marker. last_unread_at is an internal
allocation watermark that survives clearing unread_at; repository writes allocate each new
unread marker above that watermark so same-millisecond writes and clock rollback cannot reuse
a stale compare-and-clear version.unread_at is authoritative over the copy in meta_json. Writes that do not explicitly mutate
unread state must preserve the current row value at the SQLite conflict boundary, so a stale
snapshot from another Host cannot roll back a newer marker.meta_json is parsed with zcodeTaskMetaSchema; invalid rows fall back to scalar columns and log a warning.meta_json values with
task_id and workspace_path. Because (workspace_key, task_id) is the actual query/primary-key boundary,
a valid remote workspace_key is authoritative over a missing or stale workspace_identity projection;
non-remote rows use the identity column only when it equals workspace_key. This keeps list queries and
sessions-index joins on the same isolation key without ever falling back to a remote path.searchable_text is capped so long tasks cannot grow the sqlite file without bound.workspaceIdentity for remote workspaces; workspacePath remains the execution/display path.sessions-index / workspace-config ingestion subscriptions attach only after an explicit
workspace operation has made that exact workspaceKey runtime available. Their subscribe and retry paths use
an existing-only policy, so a stopped runtime returns to dormant state instead of being started by observation.workspaceIdentity?.trim() || workspacePath. An available event for one SSH,
WSL, or Docker identity must not attach observers belonging to another identity with the same execution path.sessions-index snapshot is a silent reconciliation boundary: non-draft sessions seed
only missing task rows, existing product-shell state is never overwritten, and no per-row list-change broadcast
is emitted. This keeps late attachment/reconnect safe without replaying historical terminal events or causing a
startup broadcast storm.ZCode Agent session state remains authoritative for messages, model/runtime settings, permission requests, stream events, and active turn state.
The task index may cache enough metadata for list rendering, but it must not become the source of truth for session content. Missing rows are seeded from the authoritative sessions-index summary during initial reconciliation; opening, resuming, or completing a task later enriches that row from the full session snapshot (including model and searchable message text).
Host task-index 回源(resyncTaskIndexRowFromAgent:phase.completed/error、meta.titleUpdated、
session.became-visible)调用 session/read 时带 contentProfile: "index":
task-index syncer ──session/read {runtimePolicy: existing-only, contentProfile: "index"}──▶ CLI
│ 先按 full 构建完整 snapshot(语义不变)
│ 再只剥离大载荷字段(不增删 message/part)
task-index syncer ◀── 同结构 snapshot(几十 KB~数百 KB,而非 15 MB)──┘
└─ buildMetaFromSnapshot / searchableText / hasUserVisibleContent:结果与 full 逐字节一致
elideSessionSnapshotForIndex,@zcode/shared):tool part 的 state.input → {}、
state.output/raw/error → ""、state.metadata → {};reasoning part 的 text → "";
file part 的 data: URL → data:,。message 数量与顺序、info、所有 part 的 type/id/metadata、
text/compaction/timeline/subagent 等其它 part 全部原样保留。messageLimit:它保留尾部 N 条,而 searchable text 取开头 200K 字符、标题回退取
第一条真实用户消息、可见性判断看全部消息,截尾会改变索引结果。session/read 调用方不得传 index,拿到的 snapshot
也不得回写到 UI/conversation 状态。缺省或 full 行为不变。task_status is a product task state, not a direct mirror of session.status.
session.status=running|waiting|paused maps to task_status=running when the snapshot still has active runtime.session.status=error|completed maps directly to the same terminal task status.session.status=idle only means the agent engine has no active turn. If the latest user-visible assistant message has a completed timestamp and is not a tool-call continuation boundary, the task status is completed.projection.lastError takes precedence over a previously completed assistant message, because provider failures can happen before a new assistant message is persisted.All snapshot-to-task-status callers must use the shared projection helper from @zcode/shared. UI projection, service adapter, and sqlite index sync must not carry separate copies of this logic, otherwise switching workspaces can restore a task from a different terminal-status interpretation than the one used while streaming.
packages/services/src/session/taskIndexRepo.tspackages/services/src/session/zcodeTaskService.tspackages/services/src/zcode-agent/zcodeTaskIndexSyncer.tspackages/shared/src/zcode-task-types.tsdocs/ui/pinned-task-storage.md