Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(pi): preserve tree metadata and price overrides
Chriseac03bea7f7b
diff --git a/apps/desktop/src/main/localDb/client/WorkerThreadTransport.ts b/apps/desktop/src/main/localDb/client/WorkerThreadTransport.tsindex 39f5aa9e4c..a25d14b8ab 100644--- a/apps/desktop/src/main/localDb/client/WorkerThreadTransport.ts+++ b/apps/desktop/src/main/localDb/client/WorkerThreadTransport.ts@@ -989,6 +989,23 @@ function mergeTreeUserAttachments(content, source) {return JSON.stringify(merged);}+const TREE_HOST_AGENT_META_KEYS = ['origin', 'autoResume', 'autoResumeInfo'];++function mergeTreeUserAgentMeta(agentMeta, source) {+ if (!source) return agentMeta;+ const previous = parsedTreeObjectJson(source.agent_meta);+ if (!previous) return agentMeta;+ const projected = parsedTreeObjectJson(agentMeta) || {};+ const merged = { ...projected };+ let changed = false;+ for (const key of TREE_HOST_AGENT_META_KEYS) {+ if (!Object.hasOwn(previous, key)) continue;+ merged[key] = previous[key];+ changed = true;+ }+ return changed ? JSON.stringify(merged) : agentMeta;+}+function sessionTreeRehydrate(readyDb, args) {const payload = asRecord(args, 'session.treeRehydrate args');const sessionId = expectString(payload.sessionId, 'sessionId');@@ -1045,6 +1062,7 @@ function sessionTreeRehydrate(readyDb, args) {hideVisible.run(now, sessionId);for (const row of rows) {let content = row.content;+ let agentMeta = row.agentMeta;if (row.role === 'user') {const uuid = treeEntryUuid(row.agentMeta);let source = byClientId.get(row.clientId)@@ -1063,8 +1081,9 @@ function sessionTreeRehydrate(readyDb, args) {}visiblePrefixIndex += 1;content = mergeTreeUserAttachments(row.content, source);+ agentMeta = mergeTreeUserAgentMeta(row.agentMeta, source);}- upsert.run(row.id, row.clientId, sessionId, row.role, content, row.toolUseId, row.agentMeta, row.agentKind, row.createdAt);+ upsert.run(row.id, row.clientId, sessionId, row.role, content, row.toolUseId, agentMeta, row.agentKind, row.createdAt);}readyDb.prepare('UPDATE sessions SET cleared_at = NULL, context_tokens = ?, context_window = ?, updated_at = ? WHERE id = ?').run(contextTokens, contextWindow, now, sessionId);return captured;diff --git a/apps/desktop/src/main/localDb/client/__tests__/tx.test.ts b/apps/desktop/src/main/localDb/client/__tests__/tx.test.tsindex 0c6323fcbb..8c67d4b0c9 100644--- a/apps/desktop/src/main/localDb/client/__tests__/tx.test.ts+++ b/apps/desktop/src/main/localDb/client/__tests__/tx.test.ts@@ -673,13 +673,22 @@ describe('db worker tx handlers', () => {images: [{ url: 'cindy-media://blobs/image-a.webp', name: 'design.webp' }],files: [{ path: '/repo/spec.pdf', name: 'spec.pdf' }],};+ const hostAgentMeta = {+ origin: { kind: 'scheduler', scheduleId: 'schedule-1', runId: 'run-1' },+ autoResume: true,+ autoResumeInfo: { reason: 'capacity', attempt: 2, maxAttempts: 5, sessionTotal: 3 },+ };await client.exec(`INSERT INTO messages(id, client_id, session_id, role, content, agent_meta, agent_kind, created_at, rewind_at)VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,['original-user', 'original-client', 's1', 'user', JSON.stringify(original),- JSON.stringify({ uuid: 'host-message-uuid', piEntryId: 'pi-user-entry' }), 'pi', 123, null,+ JSON.stringify({+ uuid: 'host-message-uuid',+ piEntryId: 'pi-user-entry',+ ...hostAgentMeta,+ }), 'pi', 123, null,],);@@ -697,10 +706,11 @@ describe('db worker tx handlers', () => {});await expect(client.queryOne(- 'SELECT content FROM messages WHERE session_id = ? AND client_id = ?',+ 'SELECT content, agent_meta FROM messages WHERE session_id = ? AND client_id = ?',['s1', 'pi-tree-pi-user-entry-user'],)).resolves.toEqual({content: JSON.stringify({ text: 'Review these assets\n\n[image]', images: original.images, files: original.files }),+ agent_meta: JSON.stringify({ uuid: 'pi-user-entry', ...hostAgentMeta }),});// 先切到不相关的 B 分支:旧活动分支的附件不能被模糊复制过去。@@ -714,9 +724,12 @@ describe('db worker tx handlers', () => {}],});await expect(client.queryOne(- 'SELECT content FROM messages WHERE session_id = ? AND client_id = ?',+ 'SELECT content, agent_meta FROM messages WHERE session_id = ? AND client_id = ?',['s1', 'pi-tree-branch-b-user'],- )).resolves.toEqual({ content: JSON.stringify({ text: 'Different branch' }) });+ )).resolves.toEqual({+ content: JSON.stringify({ text: 'Different branch' }),+ agent_meta: JSON.stringify({ uuid: 'branch-b' }),+ });// B→A 切回时按 entry uuid 复用 A 的历史投影,附件仍在。await client.tx('session.treeRehydrate', {@@ -729,10 +742,11 @@ describe('db worker tx handlers', () => {}],});await expect(client.queryOne(- 'SELECT content FROM messages WHERE session_id = ? AND client_id = ?',+ 'SELECT content, agent_meta FROM messages WHERE session_id = ? AND client_id = ?',['s1', 'pi-tree-pi-user-entry-user'],)).resolves.toEqual({content: JSON.stringify({ text: 'Review these assets\n\n[image]', images: original.images, files: original.files }),+ agent_meta: JSON.stringify({ uuid: 'pi-user-entry', ...hostAgentMeta }),});}, { useInlineWorker });});diff --git a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts b/apps/desktop/src/main/localDb/worker/opHandlers/tx.tsindex 12a203eee5..d02d149b9c 100644--- a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts+++ b/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts@@ -676,6 +676,28 @@ function mergeTreeUserAttachments(content: string, source: TreeAttachmentSourceRreturn JSON.stringify(merged);}+const TREE_HOST_AGENT_META_KEYS = ['origin', 'autoResume', 'autoResumeInfo'] as const;++function mergeTreeUserAgentMeta(+ agentMeta: string | null,+ source: TreeAttachmentSourceRow | null,+): string | null {+ if (!source) return agentMeta;+ const previous = parsedObjectJson(source.agent_meta);+ if (!previous) return agentMeta;+ const projected = parsedObjectJson(agentMeta) ?? {};+ const merged: Record<string, unknown> = { ...projected };+ let changed = false;+ // Pi owns the projected entry uuid; Cindy remains authoritative for delivery metadata that+ // controls scheduler/auto-resume rendering and must survive A→B→A branch reprojection.+ for (const key of TREE_HOST_AGENT_META_KEYS) {+ if (!Object.hasOwn(previous, key)) continue;+ merged[key] = previous[key];+ changed = true;+ }+ return changed ? JSON.stringify(merged) : agentMeta;+}+/** Pi 原生分支切换后,把当前活动路径原子投影成 Cindy 可见消息时间线。 */function sessionTreeRehydrate(db: Database.Database,@@ -754,6 +776,7 @@ function sessionTreeRehydrate(hideVisible.run(now, sessionId);for (const row of rows) {let content = row.content;+ let agentMeta = row.agentMeta;if (row.role === 'user') {const uuid = treeEntryUuid(row.agentMeta);let source = byClientId.get(row.clientId)@@ -774,6 +797,7 @@ function sessionTreeRehydrate(}visiblePrefixIndex += 1;content = mergeTreeUserAttachments(row.content, source);+ agentMeta = mergeTreeUserAgentMeta(row.agentMeta, source);}upsert.run(row.id,@@ -782,7 +806,7 @@ function sessionTreeRehydrate(row.role,content,row.toolUseId,- row.agentMeta,+ agentMeta,row.agentKind,row.createdAt,);diff --git a/apps/desktop/src/main/usage/__tests__/modelPriceOverrideStore.test.ts b/apps/desktop/src/main/usage/__tests__/modelPriceOverrideStore.test.tsindex d58b8e8757..e04c6eb90d 100644--- a/apps/desktop/src/main/usage/__tests__/modelPriceOverrideStore.test.ts+++ b/apps/desktop/src/main/usage/__tests__/modelPriceOverrideStore.test.ts@@ -389,4 +389,32 @@ describe('model price override sparse persistence', () => {}),).toEqual({ version: 1, entries: {} });});++ it('reloads a persisted Pi override instead of discarding it after restart', () => {+ const target = { providerId: 'custom-pi', agent: 'pi' as const, modelId: 'grok-4' };+ const key = __testing.overrideKey(target);+ expect(+ __testing.normalize({+ version: 1,+ entries: {+ [key]: {+ ...target,+ values: { inputPerMtok: 3, outputPerMtok: 15, cacheReadPerMtok: 0.3 },+ baseReference: null,+ updatedAt: '2026-08-02T00:00:00.000Z',+ },+ },+ }),+ ).toEqual({+ version: 1,+ entries: {+ [key]: {+ ...target,+ values: { inputPerMtok: 3, outputPerMtok: 15, cacheReadPerMtok: 0.3 },+ baseReference: null,+ updatedAt: '2026-08-02T00:00:00.000Z',+ },+ },+ });+ });});diff --git a/apps/desktop/src/main/usage/modelPriceOverrideStore.ts b/apps/desktop/src/main/usage/modelPriceOverrideStore.tsindex e12209175f..81bd960f63 100644--- a/apps/desktop/src/main/usage/modelPriceOverrideStore.ts+++ b/apps/desktop/src/main/usage/modelPriceOverrideStore.ts@@ -52,7 +52,7 @@ interface ModelPriceOverridePrefs {const DEFAULTS: ModelPriceOverridePrefs = { version: STORE_VERSION, entries: {} };function isAgent(value: unknown): value is AgentKind {- return value === 'claude-code' || value === 'codex';+ return value === 'claude-code' || value === 'codex' || value === 'pi';}function isCurrency(value: unknown): value is MoneyCurrency {