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。

26.6 KB
Raw Blame
/**
 * makerChatStoreBackgroundTaskReconcile.test.ts
 * ---------------------------------------------------------------------------
 * 后台任务 stale running 对账自愈(终态 agent_task_update 丢失后的收口):
 *   - seedBackgroundTaskSnapshots + staleRunningCandidates:候选集内、仍
 *     running、不在快照中的 claude-code 条目收口为 stopped;快照命中 / 非候选 /
 *     非 claude-code / 已终态条目一律不动;收口后迟到的真实事件仍能覆盖。
 *   - captureRunningClaudeTaskIds:候选集捕获口径。
 *   - initGlobalListeners 的活动熄灭触发:active:false → 延迟拉快照对账;
 *     active:true 取消;远程会话豁免;无 running 条目不发 IPC。
 */

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { setDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';

const logMocks = vi.hoisted(() => ({ warn: vi.fn() }));

vi.mock('@/lib/messageService', () => ({
  list: vi.fn(async () => []),
  create: vi.fn(async () => ({}) as unknown),
  updateContent: vi.fn(async () => ({}) as unknown),
}));

vi.mock('@/lib/sessionService', () => ({
  get: vi.fn(async () => ({
    agentKind: 'cc',
    remoteHostId: null,
    sdkSessionId: null,
    fastMode: false,
    contextTokens: 0,
    contextWindow: 0,
    totalCostUsd: 0,
  })),
  update: vi.fn(async () => ({})),
  touchUserSend: vi.fn(async () => ({})),
}));

vi.mock('@/lib/sessionsBus', () => ({
  emitPatch: vi.fn(),
}));

vi.mock('@/lib/userPromptStore', () => ({
  getUserPrompt: () => '',
}));

vi.mock('@/lib/memorySettingsStore', () => ({
  getMakerMemoryEnabled: () => true,
}));

vi.mock('@/lib/logger', () => ({
  createLogger: () => ({
    debug: vi.fn(),
    info: vi.fn(),
    warn: logMocks.warn,
    error: vi.fn(),
  }),
}));

vi.mock('@/lib/imageRef', () => ({
  parseUserContent: vi.fn((c: string) => ({ text: c, images: [], files: [] })),
  stringifyUserContent: vi.fn((text: string, images = [], files = []) =>
    JSON.stringify({ text, images, files }),
  ),
}));

vi.mock('@/lib/composerDraftStore', () => ({
  saveDraft: vi.fn(),
  setRemoteOptimisticAttachmentUrls: vi.fn(),
  plainTextToTiptapDoc: (s: string) => ({
    type: 'doc',
    content: [{ type: 'paragraph', content: [{ type: 'text', text: s }] }],
  }),
}));

// 远程判定按 id 前缀 + 可动态标记的集合,便于测「远程会话豁免对账」以及
// 「调度后、触发前才识别为远程」的粘滞复查分支。
const transportMocks = vi.hoisted(() => ({ dynamicRemoteIds: new Set<string>() }));
const isMockRemote = (sessionId: string): boolean =>
  sessionId.startsWith('remote-') || transportMocks.dynamicRemoteIds.has(sessionId);

vi.mock('@/lib/makerTransport', () => ({
  makerApiFor: () => ({
    input: {
      stop: vi.fn(async () => ({ queue: [], paused: false })),
      clearSession: vi.fn(async () => ({ queue: [], paused: false })),
    },
    closeSession: vi.fn(async () => undefined),
  }),
  getSessionFor: vi.fn(async () => ({})),
  listMessagesFor: vi.fn(async () => []),
  aroundMessagesFor: vi.fn(async () => []),
  aroundMessagesByClientIdFor: vi.fn(async () => []),
  isRemoteSession: (sessionId: string) => isMockRemote(sessionId),
  isRemoteSessionSticky: (sessionId: string) => isMockRemote(sessionId),
}));

import { makerChatStore } from '@/lib/makerChatStore';

const applyTask = (sessionId: string, data: Record<string, unknown>): void => {
  makerChatStore.__applyStreamEventForTest(sessionId, {
    sessionId,
    type: 'agent_task_update',
    source: 'claude-code',
    data: { provider: 'claude-code', ...data },
  } as CCAgentStreamEvent);
};

describe('seedBackgroundTaskSnapshots stale running 对账', () => {
  it('候选集内不在快照的 running 条目收口为 stopped;别名双键共享同一对象', () => {
    const sid = `rec-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, {
        taskId: 't-stale',
        parentToolUseId: 'tu-stale',
        status: 'running',
        taskType: 'local_agent',
      });
      const candidates = makerChatStore.captureRunningClaudeTaskIds(sid);
      expect(candidates.has('t-stale')).toBe(true);

      makerChatStore.seedBackgroundTaskSnapshots(sid, [], {
        staleRunningCandidates: candidates,
      });

      const tasks = makerChatStore.getSnapshot(sid).taskUpdates;
      expect(tasks?.get('t-stale')?.status).toBe('stopped');
      expect(tasks?.get('tu-stale')?.status).toBe('stopped');
      // 别名键共享同一新对象(isSameAgentTaskAlias 语义不被破坏)
      expect(tasks?.get('t-stale')).toBe(tasks?.get('tu-stale'));
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('快照命中(taskId 或 toolUseId 别名)的候选条目保持 running', () => {
    const sid = `rec2-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't-alive', status: 'running', taskType: 'local_agent' });
      applyTask(sid, {
        taskId: 't-alias',
        parentToolUseId: 'tu-alias',
        status: 'running',
        taskType: 'local_bash',
      });
      const candidates = makerChatStore.captureRunningClaudeTaskIds(sid);

      makerChatStore.seedBackgroundTaskSnapshots(
        sid,
        [
          { taskId: 't-alive' },
          // main 侧快照以 toolUseId 报同一任务(别名命中同样算存活)
          { taskId: 't-alias-renamed', toolUseId: 'tu-alias' },
        ],
        { staleRunningCandidates: candidates },
      );

      const tasks = makerChatStore.getSnapshot(sid).taskUpdates;
      expect(tasks?.get('t-alive')?.status).toBe('running');
      expect(tasks?.get('t-alias')?.status).toBe('running');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('候选集外的 running(请求在飞窗口内新启动)与已终态条目不动;codex 条目不受空快照影响', () => {
    const sid = `rec3-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't-old', status: 'running', taskType: 'local_agent' });
      const candidates = makerChatStore.captureRunningClaudeTaskIds(sid);
      // 候选捕获之后才启动的任务(模拟请求在飞窗口)
      applyTask(sid, { taskId: 't-fresh', status: 'running', taskType: 'local_agent' });
      // 已终态条目
      applyTask(sid, { taskId: 't-done', status: 'completed', taskType: 'local_agent' });
      // codex 任务:快照通道只覆盖 claude-code,空快照对它没有含义
      makerChatStore.__applyStreamEventForTest(sid, {
        sessionId: sid,
        type: 'agent_task_update',
        source: 'codex',
        data: { provider: 'codex', taskId: 't-codex', status: 'running' },
      } as CCAgentStreamEvent);

      makerChatStore.seedBackgroundTaskSnapshots(sid, [], {
        staleRunningCandidates: candidates,
      });

      const tasks = makerChatStore.getSnapshot(sid).taskUpdates;
      expect(tasks?.get('t-old')?.status).toBe('stopped');
      expect(tasks?.get('t-fresh')?.status).toBe('running');
      expect(tasks?.get('t-done')?.status).toBe('completed');
      expect(tasks?.get('t-codex')?.status).toBe('running');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('收口非终局:迟到的真实事件仍能覆盖(终态 → completed / 进度 → running)', () => {
    const sid = `rec4-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't-late', status: 'running', taskType: 'local_agent' });
      applyTask(sid, { taskId: 't-back', status: 'running', taskType: 'local_bash' });
      const candidates = makerChatStore.captureRunningClaudeTaskIds(sid);
      makerChatStore.seedBackgroundTaskSnapshots(sid, [], {
        staleRunningCandidates: candidates,
      });
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t-late')?.status).toBe('stopped');

      // 迟到的 task_notification 终态:stopped → completed(真实结局胜出)
      applyTask(sid, { taskId: 't-late', status: 'completed' });
      // 迟到的 task_progress:任务实际存活,翻回 running(自愈,不误杀)
      applyTask(sid, { taskId: 't-back', status: 'running' });

      const tasks = makerChatStore.getSnapshot(sid).taskUpdates;
      expect(tasks?.get('t-late')?.status).toBe('completed');
      expect(tasks?.get('t-back')?.status).toBe('running');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('captureRunningClaudeTaskIds 只含 running 的 claude-code 任务,按 taskId 去重', () => {
    const sid = `cap-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, {
        taskId: 't-run',
        parentToolUseId: 'tu-run',
        status: 'running',
        taskType: 'local_agent',
      });
      applyTask(sid, { taskId: 't-done', status: 'completed' });
      makerChatStore.__applyStreamEventForTest(sid, {
        sessionId: sid,
        type: 'agent_task_update',
        source: 'codex',
        data: { provider: 'codex', taskId: 't-codex', status: 'running' },
      } as CCAgentStreamEvent);

      const ids = makerChatStore.captureRunningClaudeTaskIds(sid);
      expect([...ids]).toEqual(['t-run']);
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('seed 与对账同次完成:快照新任务补进、stale 候选同时收口', () => {
    const sid = `rec5-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't-stale', status: 'running', taskType: 'local_agent' });
      const candidates = makerChatStore.captureRunningClaudeTaskIds(sid);

      makerChatStore.seedBackgroundTaskSnapshots(
        sid,
        [{ taskId: 't-new', taskType: 'local_bash', title: 'dev server' }],
        { staleRunningCandidates: candidates },
      );

      const tasks = makerChatStore.getSnapshot(sid).taskUpdates;
      expect(tasks?.get('t-new')?.status).toBe('running');
      expect(tasks?.get('t-stale')?.status).toBe('stopped');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });
});

// ── 活动熄灭触发的延迟自动对账(initGlobalListeners)─────────────────────────

type FanOutCb = (data: unknown, ownerStamp?: unknown) => void;

function makeElectronApiStub(listTasks: ReturnType<typeof vi.fn>) {
  let activityCb: FanOutCb | null = null;
  let messageCb: FanOutCb | null = null;
  let remoteCb: FanOutCb | null = null;
  const fanOut = () => () => () => {};
  const stub = {
    maker: {
      onEvent: fanOut(),
      onStatusChanged: fanOut(),
      onInputProjection: fanOut(),
      onInteractionRequest: fanOut(),
      onInteractionDismissed: fanOut(),
      onSessionBackgroundActivityChanged: (cb: FanOutCb) => {
        activityCb = cb;
        return () => {
          activityCb = null;
        };
      },
      listSessionBackgroundTasks: listTasks,
      input: {
        getProjection: vi.fn(async () => Promise.reject(new Error('n/a in test'))),
      },
    },
    localDb: {
      messages: {
        onCreated: vi.fn((cb: FanOutCb) => {
          messageCb = cb;
          return () => {
            messageCb = null;
          };
        }),
      },
    },
    deviceLink: {
      onRemotePush: vi.fn((cb: FanOutCb) => {
        remoteCb = cb;
        return () => {
          remoteCb = null;
        };
      }),
    },
  };
  return {
    stub,
    emitActivity: (payload: unknown) => activityCb?.(payload),
    emitMessage: (payload: unknown) => messageCb?.(payload),
    emitRemote: (payload: unknown) => remoteCb?.(payload),
  };
}

// #4655: unit coverage through the real listener lifecycle. The previous stub
// omitted optional APIs but never asserted diagnostics. An absent capability
// must be silent; a broken return must warn; supported events must still flow.
const optionalSubscriptions = [
  ['usage', 'onUsageMessageTurnCost', 'usage-message-turn-cost'],
  ['usage', 'onUsageMessageModelMismatch', 'usage-message-model-mismatch'],
  ['ghosts', 'onUserMessageBlocked', 'ghosts-user-message-blocked'],
  ['ghosts', 'onUserMessageRewritten', 'ghosts-user-message-rewritten'],
  ['ghosts', 'onAssistantMessageRewritten', 'ghosts-assistant-message-rewritten'],
  ['ghosts', 'onAssistantMessagePending', 'ghosts-assistant-message-pending'],
  ['ghosts', 'onHookFused', 'ghosts-hook-fused'],
  ['ghosts', 'onNotify', 'ghosts-notify'],
  ['ghosts', 'onPreviewOpen', 'ghosts-preview-open'],
  ['maker', 'onSessionBackgroundActivityChanged', 'session-background-activity-reconcile'],
] as const;

describe('optional IPC subscription lifecycle (#4655)', () => {
  const sid = 'optional-ipc-subscriptions';

  beforeEach(() => {
    vi.useFakeTimers();
    logMocks.warn.mockClear();
    setDataOwnerGeneration('ipc-owner', 1);
  });

  afterEach(() => {
    makerChatStore.__teardownGlobalListeners();
    makerChatStore.purgeSession(sid);
    setDataOwnerGeneration(null, 0);
    delete (globalThis as { window?: unknown }).window;
    vi.useRealTimers();
    vi.clearAllMocks();
  });

  it.each([false, true])(
    'silently skips absent optional APIs (ghost namespace present: %s)',
    (hasGhosts) => {
      const made = makeElectronApiStub(vi.fn());
      Reflect.deleteProperty(made.stub.maker, 'onSessionBackgroundActivityChanged');
      Reflect.deleteProperty(made.stub.maker, 'onEvent');
      const api = { ...made.stub, ...(hasGhosts ? { ghosts: {} } : {}) };
      (globalThis as { window?: unknown }).window = { electronAPI: api };

      makerChatStore.initGlobalListeners();

      const warnings = logMocks.warn.mock.calls.map(([message]) => message);
      for (const [, , label] of optionalSubscriptions) {
        expect(warnings.filter((message) => message.startsWith(`${label}:`))).toEqual([]);
      }
      expect(warnings).toContain('maker-event: subscribe is not available; teardown will no-op');
      expect(api.localDb.messages.onCreated).toHaveBeenCalledTimes(1);
      expect(api.deviceLink.onRemotePush).toHaveBeenCalledTimes(1);
      const message = (id: string) => ({
        id,
        clientId: id,
        sessionId: sid,
        role: 'user',
        content: id,
        createdAt: '2026-10-21T00:00:00.000Z',
      });
      made.emitMessage({ sessionId: sid, message: message('local-message') });
      made.emitRemote({
        channel: 'local-db:messages:created',
        payload: { sessionId: sid, message: message('remote-message') },
      });
      expect(makerChatStore.getSnapshot(sid).messages.map((m) => m.content)).toEqual([
        'local-message',
        'remote-message',
      ]);
    },
  );

  it('preserves receivers, event consumption and owner stamps, and unsubscribes exactly once', () => {
    const made = makeElectronApiStub(vi.fn());
    const api = { ...made.stub, ghosts: {} };
    const callbacks = new Map<string, FanOutCb>();
    const subscriptions = optionalSubscriptions.map(([domain, key, label]) => {
      const receiver = domain === 'usage' ? api : api[domain];
      const unsubscribe = vi.fn();
      const subscribe = vi.fn(function (this: unknown, cb: FanOutCb) {
        expect(this).toBe(receiver);
        callbacks.set(label, cb);
        return unsubscribe;
      });
      Object.assign(receiver, { [key]: subscribe });
      return { subscribe, unsubscribe };
    });
    (globalThis as { window?: unknown }).window = { electronAPI: api };
    makerChatStore.initGlobalListeners();
    makerChatStore.initGlobalListeners();

    const payload = {
      sessionId: sid,
      clientId: 'blocked',
      ghostId: 'test-ghost',
      text: 'original',
    };
    const stamp = { dataOwnerId: 'ipc-owner', ownerGeneration: 1 };
    callbacks.get('ghosts-user-message-blocked')?.(payload, { ...stamp, ownerGeneration: 0 });
    expect(makerChatStore.getSnapshot(sid).messages).toEqual([]);
    callbacks.get('ghosts-user-message-blocked')?.(payload, stamp);
    expect(makerChatStore.getSnapshot(sid).messages[0]?.blockedByGhost?.ghostId).toBe('test-ghost');
    callbacks.get('usage-message-turn-cost')?.({ ...payload, turnCostUsd: 0.25 }, stamp);
    expect(makerChatStore.getSnapshot(sid).messages[0]?.turnCostUsd).toBe(0.25);
    callbacks.get('usage-message-turn-cost')?.(
      { ...payload, turnCostUsd: 9 },
      { ...stamp, ownerGeneration: 0 },
    );
    expect(makerChatStore.getSnapshot(sid).messages[0]?.turnCostUsd).toBe(0.25);

    makerChatStore.__teardownGlobalListeners();
    makerChatStore.__teardownGlobalListeners();
    for (const { subscribe, unsubscribe } of subscriptions) {
      expect(subscribe).toHaveBeenCalledTimes(1);
      expect(unsubscribe).toHaveBeenCalledTimes(1);
    }
    for (const [, , label] of optionalSubscriptions) {
      expect(logMocks.warn).not.toHaveBeenCalledWith(expect.stringContaining(`${label}:`));
    }
  });

  it.each([undefined, 42])(
    'warns for every supported API returning %s instead of unsubscribe',
    (result) => {
      const made = makeElectronApiStub(vi.fn());
      const api = { ...made.stub, ghosts: {} };
      const subscriptions = optionalSubscriptions.map(([domain, key, label]) => {
        const subscribe = vi.fn(() => result);
        Object.assign(domain === 'usage' ? api : api[domain], { [key]: subscribe });
        return { subscribe, label };
      });
      (globalThis as { window?: unknown }).window = { electronAPI: api };

      makerChatStore.initGlobalListeners();
      makerChatStore.__teardownGlobalListeners();
      for (const { subscribe, label } of subscriptions) {
        expect(subscribe).toHaveBeenCalledTimes(1);
        expect(
          logMocks.warn.mock.calls.filter(
            ([message]) => message === `${label}: unsubscribe not returned; teardown will no-op`,
          ),
        ).toHaveLength(1);
      }
    },
  );
});

describe('活动熄灭触发的 stale running 对账', () => {
  let emitActivity: (payload: unknown) => void;
  let listTasks: ReturnType<typeof vi.fn>;

  beforeEach(() => {
    vi.useFakeTimers();
    listTasks = vi.fn(async () => ({ tasks: [] }));
    const made = makeElectronApiStub(listTasks);
    emitActivity = made.emitActivity;
    (globalThis as { window?: unknown }).window = { electronAPI: made.stub };
    makerChatStore.initGlobalListeners();
  });

  afterEach(() => {
    makerChatStore.__teardownGlobalListeners();
    delete (globalThis as { window?: unknown }).window;
    transportMocks.dynamicRemoteIds.clear();
    vi.useRealTimers();
    vi.clearAllMocks();
  });

  it('active:false → 延迟拉快照,stale running 收口为 stopped', async () => {
    const sid = `act-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't1', status: 'running', taskType: 'local_agent' });

      emitActivity({ sessionId: sid, active: false });
      expect(listTasks).not.toHaveBeenCalled();
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).toHaveBeenCalledWith(sid);
      // list promise → seed 的微任务落地
      await vi.advanceTimersByTimeAsync(0);

      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t1')?.status).toBe('stopped');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('首次快照仍含运行中的 Bash 时只安排一次有界重试,随后退出可被收口', async () => {
    const sid = `late-bash-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 'bash-late', status: 'running', taskType: 'local_bash' });
      listTasks
        .mockResolvedValueOnce({ tasks: [{ taskId: 'bash-late', taskType: 'local_bash' }] })
        .mockResolvedValueOnce({ tasks: [] });

      emitActivity({ sessionId: sid, active: false });
      await vi.advanceTimersByTimeAsync(3000);
      await vi.advanceTimersByTimeAsync(0);

      expect(listTasks).toHaveBeenCalledTimes(1);
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('bash-late')?.status).toBe(
        'running',
      );

      // The bounded retry covers the window in which Bash exits after the first
      // snapshot but its terminal event is lost.
      await vi.advanceTimersByTimeAsync(3000);
      await vi.advanceTimersByTimeAsync(0);

      expect(listTasks).toHaveBeenCalledTimes(2);
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('bash-late')?.status).toBe(
        'stopped',
      );

      // A task that remains alive after the retry is not polled forever.
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).toHaveBeenCalledTimes(2);
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('active:true 在快照请求飞行中到达时,丢弃旧响应且不安排重试', async () => {
    const sid = `active-inflight-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't-active', status: 'running', taskType: 'local_bash' });
      let resolveList!: (value: { tasks: unknown[] }) => void;
      listTasks.mockReturnValue(
        new Promise((resolve) => {
          resolveList = resolve;
        }),
      );

      emitActivity({ sessionId: sid, active: false });
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).toHaveBeenCalledTimes(1);

      emitActivity({ sessionId: sid, active: true });
      resolveList({ tasks: [] });
      await vi.advanceTimersByTimeAsync(0);
      await vi.advanceTimersByTimeAsync(3000);

      expect(listTasks).toHaveBeenCalledTimes(1);
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t-active')?.status).toBe(
        'running',
      );
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('purge 在快照请求飞行中到达时,旧响应不会重建会话或安排重试', async () => {
    const sid = `purge-inflight-${Math.random().toString(36).slice(2, 8)}`;
    let resolveList!: (value: { tasks: unknown[] }) => void;
    try {
      applyTask(sid, { taskId: 't-purged', status: 'running', taskType: 'local_bash' });
      listTasks.mockReturnValue(
        new Promise((resolve) => {
          resolveList = resolve;
        }),
      );

      emitActivity({ sessionId: sid, active: false });
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).toHaveBeenCalledTimes(1);

      makerChatStore.purgeSession(sid);
      resolveList({ tasks: [] });
      await vi.advanceTimersByTimeAsync(0);
      await vi.advanceTimersByTimeAsync(3000);

      expect(listTasks).toHaveBeenCalledTimes(1);
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.size ?? 0).toBe(0);
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('到点前 active:true 取消对账;仍在快照中的任务不被收口', async () => {
    const sid = `act2-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't1', status: 'running', taskType: 'local_agent' });

      emitActivity({ sessionId: sid, active: false });
      emitActivity({ sessionId: sid, active: true });
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).not.toHaveBeenCalled();
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t1')?.status).toBe('running');

      // 再次熄灭:这次快照证明任务仍在(main 表有它)→ 保持 running
      listTasks.mockResolvedValueOnce({ tasks: [{ taskId: 't1' }] });
      emitActivity({ sessionId: sid, active: false });
      await vi.advanceTimersByTimeAsync(3000);
      await vi.advanceTimersByTimeAsync(0);
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t1')?.status).toBe('running');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('调度后、触发前才识别为远程(重连窗口误放行)→ 触发沿粘滞复查拦下,不发本机 IPC', async () => {
    const sid = `late-remote-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't1', status: 'running', taskType: 'local_agent' });

      // 调度沿:此刻远程注册表尚未水合,会话被当成本机 → 定时器挂上
      emitActivity({ sessionId: sid, active: false });
      // 触发前:注册表水合,会话被识别为远程
      transportMocks.dynamicRemoteIds.add(sid);
      await vi.advanceTimersByTimeAsync(3000);

      // 触发沿粘滞复查必须拦下:不发本机 IPC,镜像里的 running 不被空快照错误收口
      expect(listTasks).not.toHaveBeenCalled();
      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t1')?.status).toBe('running');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('在飞窗口:快照响应落地前会话被识别为远程 → 丢弃本机快照,镜像 running 不被收口', async () => {
    const sid = `inflight-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(sid, { taskId: 't1', status: 'running', taskType: 'local_agent' });
      let resolveList!: (v: unknown) => void;
      listTasks.mockReturnValue(
        new Promise((r) => {
          resolveList = r;
        }),
      );

      emitActivity({ sessionId: sid, active: false });
      // timer 到点:粘滞复查通过(此刻仍判本机)、候选捕获、请求发出
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).toHaveBeenCalledWith(sid);

      // 请求在飞期间远程注册表完成会话水合,随后本机空表才落地
      transportMocks.dynamicRemoteIds.add(sid);
      resolveList({ tasks: [] });
      await vi.advanceTimersByTimeAsync(0);

      expect(makerChatStore.getSnapshot(sid).taskUpdates?.get('t1')?.status).toBe('running');
    } finally {
      makerChatStore.purgeSession(sid);
    }
  });

  it('远程会话豁免;无 running 条目不发 IPC', async () => {
    const remoteSid = 'remote-act3';
    const idleSid = `act4-${Math.random().toString(36).slice(2, 8)}`;
    try {
      applyTask(remoteSid, { taskId: 't1', status: 'running', taskType: 'local_agent' });
      emitActivity({ sessionId: remoteSid, active: false });
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).not.toHaveBeenCalled();
      expect(makerChatStore.getSnapshot(remoteSid).taskUpdates?.get('t1')?.status).toBe(
        'running',
      );

      // 本机会话但没有 running 条目、也没有唤醒桥接(非 wake 型终态不置位):
      // 调度前粗筛直接跳过。
      applyTask(idleSid, { taskId: 't2', status: 'completed', taskType: 'local_bash' });
      emitActivity({ sessionId: idleSid, active: false });
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).not.toHaveBeenCalled();

      // wake 型终态会置位唤醒桥接(pendingTaskWake):即便没有 running 条目,
      // 粗筛也要放行对账 —— 迟到 / 误投终态泄漏的桥接正是靠这次权威对账收口
      // (收口条件与代际语义见 lib/__tests__/pendingTaskWakeBridgeReconcile.test.ts)。
      applyTask(idleSid, { taskId: 't3', status: 'completed', taskType: 'local_agent' });
      emitActivity({ sessionId: idleSid, active: false });
      await vi.advanceTimersByTimeAsync(3000);
      expect(listTasks).toHaveBeenCalledTimes(1);
      expect(listTasks).toHaveBeenCalledWith(idleSid);
    } finally {
      makerChatStore.purgeSession(remoteSid);
      makerChatStore.purgeSession(idleSid);
    }
  });
});