Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(desktop): 第十三轮 review 修复——文件行 phaseIndex 归组;tapdb 二级窗口身份补种

Kafeifeiae85a1a88cd3
diff --git a/apps/desktop/src/main/workflow-progress/__tests__/reader.test.ts b/apps/desktop/src/main/workflow-progress/__tests__/reader.test.ts
index 056442c67f..9d3f4ed4b2 100644
--- a/apps/desktop/src/main/workflow-progress/__tests__/reader.test.ts
+++ b/apps/desktop/src/main/workflow-progress/__tests__/reader.test.ts
@@ -101,6 +101,7 @@ describe('extractWorkflowProgress', () => {
model: 'claude-opus-4-8[1m]',
state: 'done',
phaseTitle: 'Search',
+ phaseIndex: 1,
attempt: 1,
lastToolName: 'StructuredOutput',
lastToolSummary: '核心断言全部实测确认',
@@ -113,6 +114,7 @@ describe('extractWorkflowProgress', () => {
model: 'claude-opus-4-8[1m]',
state: 'running',
phaseTitle: 'Search',
+ phaseIndex: 1,
attempt: 1,
},
]);
@@ -235,7 +237,7 @@ describe('extractWorkflowProgress', () => {
{ type: 'workflow_agent', label: 'verify:auth', state: 'queued', phaseIndex: 0 },
],
});
- expect(p!.agents).toEqual([{ label: 'verify:auth', state: 'queued' }]);
+ expect(p!.agents).toEqual([{ label: 'verify:auth', state: 'queued', phaseIndex: 0 }]);
});
it('returns null when runId is missing or input is not an object', () => {
diff --git a/apps/desktop/src/main/workflow-progress/reader.ts b/apps/desktop/src/main/workflow-progress/reader.ts
index cc2d29bf6f..7ee081f79d 100644
--- a/apps/desktop/src/main/workflow-progress/reader.ts
+++ b/apps/desktop/src/main/workflow-progress/reader.ts
@@ -106,6 +106,7 @@ export function extractWorkflowProgress(record: unknown): WorkflowProgress | nul
if (!label || !state) continue;
const model = asString(e.model);
const phaseTitle = asString(e.phaseTitle);
+ const phaseIndex = asNumber(e.phaseIndex);
const attempt = asNumber(e.attempt);
const lastToolName = asTruncated(e.lastToolName, 200);
const lastToolSummary = asTruncated(e.lastToolSummary, 160);
@@ -118,6 +119,7 @@ export function extractWorkflowProgress(record: unknown): WorkflowProgress | nul
state,
...(model ? { model } : {}),
...(phaseTitle ? { phaseTitle } : {}),
+ ...(phaseIndex != null ? { phaseIndex } : {}),
...(attempt != null ? { attempt } : {}),
...(lastToolName ? { lastToolName } : {}),
...(lastToolSummary ? { lastToolSummary } : {}),
diff --git a/apps/desktop/src/renderer/__tests__/tapdbConsentGate.test.ts b/apps/desktop/src/renderer/__tests__/tapdbConsentGate.test.ts
index 0dd08b5971..39411f0a98 100644
--- a/apps/desktop/src/renderer/__tests__/tapdbConsentGate.test.ts
+++ b/apps/desktop/src/renderer/__tests__/tapdbConsentGate.test.ts
@@ -470,6 +470,23 @@ describe('engagement-driven activity reporting', () => {
expect(tapdb.setUser).toHaveBeenCalledTimes(2);
});
+ it('seeds identity via authInitialize so a secondary window does not misread refresh as login', async () => {
+ // 登录后才打开的二级窗口错过初始广播:补种身份后,半夜 token 刷新广播的
+ // 同一账号不得被当成「身份变化」触发 setUser。
+ installElectronApi(ALLOWED);
+ (window as unknown as { electronAPI: Record<string, unknown> }).electronAPI.authInitialize =
+ vi.fn(async () => ({ isAuthenticated: true, user: { id: 'user-1' } }));
+ const client = await importClient();
+ client.initTapdb();
+ await flush();
+ tapdb.setUser.mockClear();
+
+ vi.setSystemTime(new Date(2026, 6, 29, 3, 0, 0));
+ authListener?.({ isAuthenticated: true, user: { id: 'user-1' } });
+
+ expect(tapdb.setUser).not.toHaveBeenCalled();
+ });
+
it('a token-refresh auth broadcast on a new day does not emit setUser (no interaction)', async () => {
// auth:state-change 也由定时 token 刷新广播:挂机过夜的机器不得凭空产生
// 当日账号活跃 —— 跨天重绑只走交互路径(reportActive)。
diff --git a/apps/desktop/src/renderer/analytics/tapdbClient.ts b/apps/desktop/src/renderer/analytics/tapdbClient.ts
index 483d52cb16..cac4c6e397 100644
--- a/apps/desktop/src/renderer/analytics/tapdbClient.ts
+++ b/apps/desktop/src/renderer/analytics/tapdbClient.ts
@@ -182,6 +182,26 @@ export function initTapdb(): void {
log.error('onAuthStateChange subscription failed (non-fatal)', err);
}
+ // 身份补种:登录**之后**才打开的二级窗口(独立侧栏等)错过了 auth:state-change
+ // 的初始广播,currentUserId 停在 null —— 半夜定时 token 刷新的广播会被误判成
+ // 「身份变化」触发非交互 setUser(假 DAU)。挂完订阅后主动读一次当前身份;
+ // 只在仍未从广播学到身份时写入,不与并发广播竞争,也不触发任何上报。
+ try {
+ const seed = window.electronAPI.authInitialize?.();
+ if (seed) {
+ void seed
+ .then((state) => {
+ if (currentUserId !== null) return;
+ if (state?.isAuthenticated && state.user?.id) currentUserId = state.user.id;
+ })
+ .catch(() => {
+ // 静默:读不到就维持广播驱动的原行为。
+ });
+ }
+ } catch (err) {
+ log.error('auth identity seed failed (non-fatal)', err);
+ }
+
// 交互驱动的活跃信号(见文件头「活跃口径」)。capture 挂在 window 捕获阶段,
// 业务代码的 stopPropagation 挡不住;onEngagedSignal 自带节流与同意闸。
try {
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts
index 9ae9bfbd80..5264ba2286 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/__tests__/workflowProgressModel.test.ts
@@ -410,6 +410,21 @@ describe('buildWorkflowTreeModel', () => {
expect(rows.find((r) => r.key === 'a2')).toMatchObject({ resultPreview: 'second ok', durationMs: 200 });
});
+ it('file-only 路径:只带 phaseIndex 的文件行经顶层 phases[] 反查归组,不坠孤儿区', () => {
+ const file = fileProgress({
+ phases: [{ index: 0, title: 'Scan' }],
+ agents: [
+ fileAgent({ label: 'queued-one', agentId: '', state: 'queued', phaseIndex: 0 }),
+ fileAgent({ label: 'lost', agentId: '', state: 'queued', phaseIndex: 9 }),
+ ],
+ });
+ const model = buildWorkflowTreeModel({ fileProgress: file, taskStatus: 'running' })!;
+ expect(model.groups[0]).toMatchObject({ title: 'Scan' });
+ expect(model.groups[0].agents.map((r) => r.label)).toEqual(['queued-one']);
+ expect(model.groups[1]).toMatchObject({ title: null });
+ expect(model.groups[1].agents.map((r) => r.label)).toEqual(['lost']);
+ });
+
it('entries 已有终态 state 时文件不覆盖(采纳只发生在事件流停在非终态时)', () => {
const entries: WorkflowProgressEntry[] = [
agentEntry(0, { agentId: 'a1', label: 'alpha', phaseTitle: 'P', state: 'error', error: 'boom' }),
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts
index 12bc771969..7228b5d75f 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/background-tasks/workflowProgressModel.ts
@@ -191,7 +191,7 @@ function groupsFromEntries(
const orphans: WorkflowTreeAgentRow[] = [];
// 回填索引一次性预建:条目上限 2000,逐行线性扫 file.agents 是 O(n²),大编队
// 打开详情会卡帧。
- const fileAgentIndex = backfillTerminal && file ? indexFileAgents(file.agents) : null;
+ const fileAgentIndex = backfillTerminal && file ? indexFileAgents(file) : null;
let agentOrdinal = 0;
for (const entry of entries) {
if (entry.type !== 'workflow_agent') continue;
@@ -214,9 +214,30 @@ function groupsFromEntries(
return assembleGroups(byPhase, orphans, detailByTitle);
}
+/** 文件 agent 的有效 phase 标题:phaseTitle 直取;只带 phaseIndex 的排队行
+ * (与事件流同形态)经顶层 phases[] 的 index→title 反查,不坠入孤儿区。 */
+function resolveFilePhaseTitle(
+ agent: WorkflowAgentProgress,
+ titleByIndex: ReadonlyMap<number, string>,
+): string | undefined {
+ return (
+ agent.phaseTitle ??
+ (typeof agent.phaseIndex === 'number' ? titleByIndex.get(agent.phaseIndex) : undefined)
+ );
+}
+
+function collectFilePhaseTitlesByIndex(file: WorkflowProgress): Map<number, string> {
+ const titleByIndex = new Map<number, string>();
+ for (const phase of file.phases) {
+ if (!titleByIndex.has(phase.index)) titleByIndex.set(phase.index, phase.title);
+ }
+ return titleByIndex;
+}
+
/** entries 缺失(重载后)整树退回 wf 文件;文件 state 词表原样保留。 */
function groupsFromFile(file: WorkflowProgress): WorkflowTreeGroup[] {
const detailByTitle = collectPhaseDetails(file);
+ const titleByIndex = collectFilePhaseTitlesByIndex(file);
const byPhase = new Map<string, WorkflowTreeAgentRow[]>();
for (const phase of file.phases) {
if (!byPhase.has(phase.title)) byPhase.set(phase.title, []);
@@ -224,8 +245,9 @@ function groupsFromFile(file: WorkflowProgress): WorkflowTreeGroup[] {
const orphans: WorkflowTreeAgentRow[] = [];
file.agents.forEach((agent, index) => {
const row = rowFromFileAgent(agent, index);
- if (agent.phaseTitle && byPhase.has(agent.phaseTitle)) {
- byPhase.get(agent.phaseTitle)!.push(row);
+ const phaseTitle = resolveFilePhaseTitle(agent, titleByIndex);
+ if (phaseTitle && byPhase.has(phaseTitle)) {
+ byPhase.get(phaseTitle)!.push(row);
} else {
orphans.push(row);
}
@@ -306,12 +328,15 @@ interface FileAgentIndex {
byLabelPhase: Map<string, WorkflowAgentProgress>;
}
-function indexFileAgents(fileAgents: readonly WorkflowAgentProgress[]): FileAgentIndex {
+function indexFileAgents(file: WorkflowProgress): FileAgentIndex {
const byAgentId = new Map<string, WorkflowAgentProgress>();
const byLabelPhase = new Map<string, WorkflowAgentProgress>();
- for (const agent of fileAgents) {
+ // label 键用**解析后的**有效 title(phaseIndex 反查),与事件行解析口径对齐 ——
+ // 否则文件里只带 phaseIndex 的行 key 落空串,永远匹配不上事件行。
+ const titleByIndex = collectFilePhaseTitlesByIndex(file);
+ for (const agent of file.agents) {
if (agent.agentId && !byAgentId.has(agent.agentId)) byAgentId.set(agent.agentId, agent);
- const key = fileAgentKey(agent.label, agent.phaseTitle);
+ const key = fileAgentKey(agent.label, resolveFilePhaseTitle(agent, titleByIndex));
if (!byLabelPhase.has(key)) byLabelPhase.set(key, agent);
}
return { byAgentId, byLabelPhase };
diff --git a/apps/desktop/src/shared/workflow-progress.ts b/apps/desktop/src/shared/workflow-progress.ts
index 9a7431cfea..2ac8d98d46 100644
--- a/apps/desktop/src/shared/workflow-progress.ts
+++ b/apps/desktop/src/shared/workflow-progress.ts
@@ -18,6 +18,8 @@ export interface WorkflowAgentProgress {
state: string;
/** 所属 phase 标题。 */
phaseTitle?: string;
+ /** 所属 phase 序号(条目可能只带 index 不带 title,经顶层 phases[] 反查)。 */
+ phaseIndex?: number;
/** 重试次数(≥1)。 */
attempt?: number;
/** 最近一次工具调用的工具名(如 "Read")。 */