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。

313 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { MAKER_EVENT_BATCH_CHANNEL, SESSION_SYNC_CHANNEL } from '@cindy/device-link';
import { clampLiveRowCreatedAt } from '@/session/messagePaging';
import { MOBILE_TOOL_INPUT_PROJECTION_THRESHOLD_BYTES } from '@/session/messageToolPayloadProjection';
import { remoteSessionStore, resolveSessionWriteDevices, sessionPendingWrites } from '@/session/remoteSessionStore';
import { buildMobileSessionMessageDeepLink, parseSessionDeepLinkUrl, resolveSessionLinkDeviceId } from '@/session/sessionLinks';
import { sessionMenuCopyLink } from '@/session/sessionMenu';
import type { InputProjection, PendingInteraction, RemoteMessage, RemoteSession } from '@/session/types';
import { setJankMetricsEnabled, takeJankMetrics } from '@/debug/jankMetrics';

function session(id: string, patch: Partial<RemoteSession> = {}): RemoteSession {
  return {
    id,
    userId: 'user-1',
    title: id,
    workingDir: '/repo',
    workspaceKind: 'project',
    model: 'claude',
    effort: 'medium',
    permissionMode: 'default',
    fastMode: false,
    status: 'active',
    agentKind: 'cc',
    userSendAt: null,
    createdAt: '2026-01-01T00:00:00.000Z',
    updatedAt: '2026-01-01T00:00:00.000Z',
    ...patch,
  };
}

function message(id: string, sessionId: string): RemoteMessage {
  return {
    id,
    clientId: id,
    sessionId,
    role: 'assistant',
    content: 'hello',
    toolUseId: null,
    agentMeta: null,
    createdAt: '2026-01-01T00:00:00.000Z',
  };
}

// Unit, PR #6863 r4353650385: existing removal coverage retained messages but
// lost the raw metadata read by useRemoteSessionDetail. A preview must hide only
// the visible/Home projections, retaining title, status and messages for details.
it('keeps detail metadata and messages while an archive preview hides only the list row', () => {
  remoteSessionStore.clear();
  const row = session('s1');
  remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [row]);
  remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
  const removed = vi.fn();
  const off = remoteSessionStore.onSessionRemoved(removed);
  try {
    const finishPreview = remoteSessionStore.beginSessionListRemovalPreview('s1');
    expect(remoteSessionStore.getVisibleSessions()).toEqual([]);
    expect(remoteSessionStore.getHomeSessions()).toEqual([]);
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({ id: 's1', title: 's1', status: 'active' });
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(removed).not.toHaveBeenCalled();
    remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [row]);
    expect(remoteSessionStore.getVisibleSessions()).toEqual([]);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { title: 'Saved rename' });
    finishPreview();
    expect(remoteSessionStore.getHomeSessions()[0].title).toBe('Saved rename');
    expect(remoteSessionStore.getMessages('s1')[0].content).toBe('hello');
    const finishConfirmedPreview = remoteSessionStore.beginSessionListRemovalPreview('s1');
    remoteSessionStore.applySessionPatch('dev-1', 's1', { status: 'archived' });
    finishConfirmedPreview();
    expect(remoteSessionStore.getSessions()).toEqual([]);
    expect(remoteSessionStore.getMessages('s1')).toEqual([]);
    expect(removed).toHaveBeenCalledExactlyOnceWith('dev-1', 's1');
  } finally { off(); remoteSessionStore.clear(); }
});

it('finishing an older archive preview keeps a newer preview hidden and cannot revive a removed row', () => {
  remoteSessionStore.clear();
  remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [session('s1')]);
  const older = remoteSessionStore.beginSessionListRemovalPreview('s1');
  const newer = remoteSessionStore.beginSessionListRemovalPreview('s1');
  older();
  older();
  expect(remoteSessionStore.getHomeSessions()).toEqual([]);
  expect(remoteSessionStore.getSessions()).toHaveLength(1);
  remoteSessionStore.applySessionPatch('dev-1', 's1', { status: 'deleted' });
  newer();
  expect(remoteSessionStore.getSessions()).toEqual([]);
  expect(remoteSessionStore.getHomeSessions()).toEqual([]);
  remoteSessionStore.clear();
});

// Unit: side-chat details are omitted by the Host list. A list refresh must
// retain opened details without leaking them into Home, and deletion must still
// remove them. Existing tests only cover ordinary listed task reconciliation.
it.each(['deleted', 'archived'] as const)('retains hidden side-chat details across refresh, then reports %s after removal', status => {
  remoteSessionStore.clear();
  const parent = session('main');
  const child = session('side', { source: 'side-chat', parentSessionId: 'main' });
  remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [parent]);
  remoteSessionStore.upsertDeviceSession('dev-1', 'Computer', child);
  // Black-box regression (2026-10-20): removing the row did not close its sheet.
  // This lifecycle signal must follow removal, and must not fire on reconnect.
  const removed = vi.fn(() => remoteSessionStore.getSessions().map(row => row.id));
  const off = remoteSessionStore.onSessionRemoved(removed);
  try {
    remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [parent]);
    remoteSessionStore.markDeviceOffline('dev-1');
    expect(removed).not.toHaveBeenCalled();
    expect(remoteSessionStore.getSessions().map(row => row.id)).toContain('side');
    expect(remoteSessionStore.getVisibleSessions().map(row => row.id)).toEqual(['main']);
    expect(remoteSessionStore.getHomeSessions().map(row => row.id)).toEqual(['main']);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
      sessionId: 'side', patch: { status },
    });
    expect(removed).toHaveBeenCalledExactlyOnceWith('dev-1', 'side');
    expect(removed.mock.results[0].value).toEqual(['main']);
    remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [parent]);
    expect(remoteSessionStore.getSessions().map(row => row.id)).toEqual(['main']);
  } finally { off(); remoteSessionStore.clear(); }
});

// Unit, #6529: reopening an archived side chat restores a detail omitted by
// sessions:list. Active-only refresh coverage missed this archive → reopen path.
it('keeps reopened archived side-chat details through list refresh until deletion', () => {
  remoteSessionStore.clear();
  const parent = session('main');
  const archived = session('side', { source: 'side-chat', parentSessionId: 'main', status: 'archived' });
  try {
    remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [parent]);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Computer', archived);
    remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [parent]);
    expect(remoteSessionStore.getSessions()).toContainEqual(expect.objectContaining({ id: 'side', status: 'archived' }));
    expect(remoteSessionStore.getVisibleSessions().map(row => row.id)).toEqual(['main']);
    expect(remoteSessionStore.getHomeSessions().map(row => row.id)).toEqual(['main']);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
      sessionId: 'side', patch: { status: 'deleted' },
    });
    remoteSessionStore.setDeviceSessions('dev-1', 'Computer', [parent]);
    expect(remoteSessionStore.getSessions().map(row => row.id)).toEqual(['main']);
  } finally { remoteSessionStore.clear(); }
});

function messageAt(id: string, sessionId: string, createdAt: string): RemoteMessage {
  return { ...message(id, sessionId), createdAt };
}

function pushMakerStatus(sessionId: string, data: Record<string, unknown>, deviceId = 'dev-1'): void {
  remoteSessionStore.applyRemotePush(deviceId, 'maker:event', {
    sessionId,
    event: { type: 'status', data },
  });
}

function pushMakerTaskUpdate(
  sessionId: string,
  taskId: string,
  opts: { source?: 'claude-code' | 'codex' | 'pi'; status?: string; description?: string } = {},
): void {
  remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
    sessionId,
    event: {
      type: 'agent_task_update',
      source: opts.source ?? 'claude-code',
      data: {
        taskId,
        status: opts.status ?? 'running',
        title: taskId,
        ...(opts.description ? { description: opts.description } : {}),
      },
    },
  });
}

function pushMakerText(
  sessionId: string,
  persistId: string | undefined,
  text: string,
  isFinal: boolean,
  agentMeta?: Record<string, unknown>,
): void {
  remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
    sessionId,
    ...(persistId ? { persistId } : {}),
    event: {
      type: 'text',
      data: { text, isFinal },
      ...(agentMeta ? { agentMeta } : {}),
    },
  });
}

function projection(sessionId: string, clientId = 'q-1'): InputProjection {
  return {
    sessionId,
    pendingQueue: [{
      clientId,
      text: 'queued',
      persistedContent: JSON.stringify({ text: 'queued', images: [], files: [] }),
      model: 'claude',
      effort: 'medium',
      permissionMode: 'ask',
      workingDir: '/repo',
      createOpts: {
        agentKind: 'claude-code',
        workingDir: '/repo',
        model: 'claude',
      },
      chatMessage: {
        clientId,
        role: 'user',
        content: 'queued',
        createdAt: '2026-01-01T00:00:00.000Z',
      },
    }],
    steeringQueueClientIds: [],
    queuePaused: true,
    queueExpanded: false,
    queueInteractionLocks: [],
    queueEditLocks: [],
    queueAbortPending: false,
    error: null,
    errorRetryText: null,
    credentialSwitchWait: null,
  };
}

function pending(kind: string, requestId?: string, persistId?: string): PendingInteraction {
  return {
    persistId,
    request: {
      kind,
      ...(requestId ? { requestId } : {}),
    },
  };
}

describe('remoteSessionStore', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('lists independent teammate lanes, excludes canonical chat and retains attribution after legacy detail reads', () => {
    const taskTeammate = { id: 'cindy-default', name: 'Cindy' };
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('routine', { source: 'bot', companion: { id: 'cindy-default', role: 'routine' }, taskTeammate }),
      session('channel', { source: 'bot', companion: { id: 'cindy-default', role: 'channel' }, taskTeammate }),
      session('canonical', { source: 'bot', companion: { id: 'cindy-default', role: 'canonical' } }),
      session('thread', { source: 'bot' }),
    ]);
    expect(remoteSessionStore.getHomeSessions().map(row => row.id).sort()).toEqual(['channel', 'routine']);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('routine', { source: 'bot', status: 'archived' }));
    expect(remoteSessionStore.getHomeSessions().find(row => row.id === 'routine')).toMatchObject({
      status: 'archived', companion: { id: 'cindy-default', role: 'routine' }, taskTeammate,
    });
  });

  it('keeps an edited async question across disconnection but removes it on explicit history clearing', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const item: PendingInteraction = { persistId: 'p1',
      request: { kind: 'ask_user_question', requestId: 'q1', delivery: 'async', questions: [{ question: 'Q?' }] } };
    remoteSessionStore.setPendingInteractions('s1', [item]);
    remoteSessionStore.protectAsyncQuestion('s1', item);
    remoteSessionStore.applyRemotePush('dev-1', 'maker:interaction-dismissed', {
      sessionId: 's1', requestId: 'q1', reason: 'resolved', decision: { dismissed: true },
    });
    const visible = remoteSessionStore.getVisiblePendingInteractions('s1');
    expect(remoteSessionStore.getVisiblePendingInteractions('s1')).toBe(visible);
    remoteSessionStore.markDeviceOffline('dev-1');
    expect(remoteSessionStore.getPendingInteractions('s1')).toHaveLength(0);
    expect(remoteSessionStore.getVisiblePendingInteractions('s1')[0]).toMatchObject({ retainedDraft: true });
    remoteSessionStore.invalidateSessionMessageWindow('s1', 'dev-1');
    expect(remoteSessionStore.getVisiblePendingInteractions('s1')).toHaveLength(0);
  });

  it.each(['deleted', 'archived', 'logout'] as const)('drops protected cards on %s', (action) => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.protectAsyncQuestion('s1', { request: {
      kind: 'ask_user_question', requestId: 'q1', delivery: 'async',
    } });
    if (action === 'logout') remoteSessionStore.clear();
    else remoteSessionStore.applySessionPatch('dev-1', 's1', { status: action });
    expect(remoteSessionStore.getVisiblePendingInteractions('s1')).toHaveLength(0);
  });

  it('keeps Make mirrors for transport while excluding them from visible session snapshots', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('make', { source: 'cindy-make' }),
      session('merge', { source: 'cindy-make-merge' }),
      session('ordinary', { source: 'desktop' }),
    ]);
    expect(remoteSessionStore.getSessions()).toHaveLength(3);
    expect(remoteSessionStore.getSessionDeviceId('make')).toBe('dev-1');
    const visible = remoteSessionStore.getVisibleSessions();
    expect(visible.map(item => item.id)).toEqual(['ordinary']);
    const home = remoteSessionStore.getHomeSessions();
    expect(home.map(item => item.id)).toEqual(['ordinary']);
    remoteSessionStore.applySessionPatch('dev-1', 'make', { totalCostUsd: 1 });
    expect(remoteSessionStore.getVisibleSessions()).toBe(visible);
    expect(remoteSessionStore.getHomeSessions()).toBe(home);
    remoteSessionStore.applySessionPatch('dev-1', 'ordinary', { totalCostUsd: 2 });
    expect(remoteSessionStore.getVisibleSessions()[0].totalCostUsd).toBe(2);
    expect(remoteSessionStore.getHomeSessions()).toBe(home);
    remoteSessionStore.applySessionPatch('dev-1', 'ordinary', { source: 'cindy-make-merge' });
    expect(remoteSessionStore.getVisibleSessions()).toEqual([]);
    expect(remoteSessionStore.getHomeSessions()).toEqual([]);
    remoteSessionStore.clear();
    expect(remoteSessionStore.getVisibleSessions()).toEqual([]);
    expect(remoteSessionStore.getHomeSessions()).toEqual([]);
  });

  it.each(['cindy-make', 'cindy-make-merge'])('removes %s from Home when source metadata arrives late', (source) => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('unknown'),
      session('ordinary', { source: 'desktop' }),
    ]);
    const ordinary = remoteSessionStore.getHomeSessions()[1];
    remoteSessionStore.applySessionActivity('dev-1', { sessionId: 'unknown', phase: 'error', attention: true });
    remoteSessionStore.applySessionPatch('dev-1', 'unknown', { source });
    expect(remoteSessionStore.getHomeSessions()).toEqual([ordinary]);
    expect(remoteSessionStore.getHomeSessions()[0]).toBe(ordinary);
    expect(remoteSessionStore.getSessions()).toHaveLength(2);
    expect(remoteSessionStore.getSessionLiveActivity('unknown')).toMatchObject({ phase: 'error', attention: true });
    remoteSessionStore.applySessionActivity('dev-1', { sessionId: 'unknown', phase: 'completed', attention: true });
    expect(remoteSessionStore.getHomeSessions()).toEqual([ordinary]);
  });

  it('releases a large persisted tool input when the matching result is appended', () => {
    remoteSessionStore.setMessages('s1', [{
      ...message('tool-use', 's1'),
      role: 'tool_use',
      toolUseId: 'toolu-1',
      content: {
        input: { payload: 'x'.repeat(MOBILE_TOOL_INPUT_PROJECTION_THRESHOLD_BYTES + 1) },
        toolName: 'WebFetch',
        toolUseId: 'toolu-1',
      },
    }]);
    expect(remoteSessionStore.getMessages('s1')[0].mobileToolInputProjection).toBeUndefined();

    remoteSessionStore.appendMessage('s1', {
      ...message('tool-result', 's1'),
      role: 'tool_result',
      toolUseId: 'toolu-1',
      content: 'finished',
      createdAt: '2026-01-01T00:00:01.000Z',
    });

    expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
      content: { input: null, mobilePayloadProjected: true },
      mobileToolInputProjection: {
        projected: true,
        toolUseMessageId: 'tool-use',
      },
    });
  });

  it('normalizes same-timestamp messages by host rowid', () => {
    const createdAt = '2026-01-01T00:00:00.000Z';
    remoteSessionStore.setMessages('s1', [
      { ...message('a-newer', 's1'), createdAt, rowid: 5 },
      { ...message('z-older', 's1'), createdAt, rowid: 4 },
    ]);

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'z-older',
      'a-newer',
    ]);
  });

  it('preserves arrival order for same-timestamp messages without host rowid', () => {
    const createdAt = '2026-01-01T00:00:00.000Z';
    remoteSessionStore.setMessages('s1', [
      { ...message('z-first', 's1'), createdAt },
      { ...message('a-second', 's1'), createdAt },
    ]);

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'z-first',
      'a-second',
    ]);
  });

  it('stamps sessions with device-link origin and indexes session ids', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);

    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      id: 's1',
      deviceLinkDeviceId: 'dev-1',
      deviceLinkDeviceName: 'Mac',
    });
    expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('dev-1');
  });

  it('mirrors new-maker worktree preference pushes, including explicit false and same-value revisions', () => {
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1')).toEqual({
      enabled: false,
      revision: 0,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:new-maker-draft:changed', {
      claudeCode: { worktreeEnabled: true },
      codex: { worktreeEnabled: true },
    });
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1')).toEqual({
      enabled: true,
      revision: 1,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:new-maker-draft:changed', {
      codex: { worktreeEnabled: false },
    });
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1')).toEqual({
      enabled: false,
      revision: 2,
    });

    // 同值 push 仍可能晚于在途 pull,revision 必须推进让旧响应失去写权。
    remoteSessionStore.applyRemotePush('dev-1', 'maker:new-maker-draft:changed', {
      claudeCode: { worktreeEnabled: false },
    });
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1')).toEqual({
      enabled: false,
      revision: 3,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:new-maker-draft:changed', {
      claudeCode: { worktreeEnabled: 'yes' },
    });
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1').revision).toBe(3);
  });

  it('isolates new-maker worktree preferences by device and removes their revisions with the device', () => {
    remoteSessionStore.setNewMakerWorktreePreference('dev-1', true);
    remoteSessionStore.setNewMakerWorktreePreference('dev-2', false);
    remoteSessionStore.setNewMakerWorktreePreference('dev-2', true);

    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1')).toEqual({
      enabled: true,
      revision: 1,
    });
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-2')).toEqual({
      enabled: true,
      revision: 2,
    });

    remoteSessionStore.removeDevice('dev-1');
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1')).toEqual({
      enabled: false,
      revision: 0,
    });
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-2')).toEqual({
      enabled: true,
      revision: 2,
    });

    remoteSessionStore.clear();
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-2')).toEqual({
      enabled: false,
      revision: 0,
    });
  });

  it('mirrors host-revisioned worktree branches without coupling them to the checkbox', () => {
    remoteSessionStore.setNewMakerWorktreePreference('dev-1', true);

    remoteSessionStore.applyRemotePush(
      'dev-1',
      'maker:new-maker-worktree-branch:changed',
      { baseRepo: '/repo/a', sourceBranch: 'feature/mobile', revision: 1 },
    );
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/a')).toEqual({
      baseRepo: '/repo/a',
      sourceBranch: 'feature/mobile',
      revision: 1,
    });
    expect(remoteSessionStore.getNewMakerWorktreePreference('dev-1')).toEqual({
      enabled: true,
      revision: 1,
    });

    // 同一分支的较新 host snapshot 仍要推进 revision,给在途 pull / apply 回包做 fence。
    remoteSessionStore.applyRemotePush(
      'dev-1',
      'maker:new-maker-worktree-branch:changed',
      { baseRepo: '/repo/a', sourceBranch: 'feature/mobile', revision: 2 },
    );
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/a')?.revision)
      .toBe(2);

    // 更旧 revision 以及同 revision 的冲突值都无权覆盖已接受的宿主快照。
    remoteSessionStore.applyRemotePush(
      'dev-1',
      'maker:new-maker-worktree-branch:changed',
      { baseRepo: '/repo/a', sourceBranch: 'stale', revision: 1 },
    );
    remoteSessionStore.applyRemotePush(
      'dev-1',
      'maker:new-maker-worktree-branch:changed',
      { baseRepo: '/repo/a', sourceBranch: 'conflict', revision: 2 },
    );
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/a')).toEqual({
      baseRepo: '/repo/a',
      sourceBranch: 'feature/mobile',
      revision: 2,
    });
  });

  it('isolates worktree branch snapshots by device and canonical repo, then clears their shards', () => {
    remoteSessionStore.setNewMakerWorktreeBranchPreference('dev-1', {
      baseRepo: '/repo/a', sourceBranch: 'main', revision: 1,
    });
    remoteSessionStore.setNewMakerWorktreeBranchPreference('dev-1', {
      baseRepo: '/repo/b', sourceBranch: 'release', revision: 4,
    });
    remoteSessionStore.setNewMakerWorktreeBranchPreference('dev-2', {
      baseRepo: '/repo/a', sourceBranch: 'develop', revision: 2,
    });

    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/a')?.sourceBranch)
      .toBe('main');
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/b')?.sourceBranch)
      .toBe('release');
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-2', '/repo/a')?.sourceBranch)
      .toBe('develop');
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-2', '/repo/b')).toBeNull();

    remoteSessionStore.removeDevice('dev-1');
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/a')).toBeNull();
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/b')).toBeNull();
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-2', '/repo/a')?.sourceBranch)
      .toBe('develop');

    remoteSessionStore.clear();
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-2', '/repo/a')).toBeNull();
  });

  it('ignores malformed worktree branch pushes without disturbing an accepted snapshot', () => {
    remoteSessionStore.setNewMakerWorktreeBranchPreference('dev-1', {
      baseRepo: '/repo/a', sourceBranch: 'main', revision: 3,
    });
    for (const payload of [
      null,
      { baseRepo: '', sourceBranch: 'release', revision: 4 },
      { baseRepo: '/repo/a', sourceBranch: '', revision: 4 },
      { baseRepo: '/repo/a', sourceBranch: 'release', revision: -1 },
      { baseRepo: '/repo/a', sourceBranch: 'release', revision: 3.5 },
      { baseRepo: '/repo/a', sourceBranch: 'release', revision: '4' },
    ]) {
      remoteSessionStore.applyRemotePush(
        'dev-1',
        'maker:new-maker-worktree-branch:changed',
        payload,
      );
    }
    expect(remoteSessionStore.getNewMakerWorktreeBranchPreference('dev-1', '/repo/a')).toEqual({
      baseRepo: '/repo/a',
      sourceBranch: 'main',
      revision: 3,
    });
  });

  it('mirrors structured session money and legacy USD usage pushes', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);

    // 被控端裸 UPDATE 不发 sessions:patched,这两条(sessions topic)是唯一更新通道。
    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-spend-changed', {
      sessionId: 's1',
      totalCostUsd: 1.23,
    });
    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-tokens-changed', {
      sessionId: 's1',
      totalTokens: 45_000,
    });
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      id: 's1',
      totalCostUsd: 1.23,
      totalTokenUsage: 45_000,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-spend-changed', {
      sessionId: 's1',
      totalMoney: {
        amount: 8.24,
        currency: 'CNY',
        approximate: false,
        kind: 'actual-cost',
      },
    });
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      totalMoney: {
        amount: 8.24,
        currency: 'CNY',
      },
    });

    // 跨设备 payload 防御:NaN / 负数不入镜像。
    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-spend-changed', {
      sessionId: 's1',
      totalCostUsd: Number.NaN,
    });
    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-tokens-changed', {
      sessionId: 's1',
      totalTokens: -1,
    });
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      totalCostUsd: 1.23,
      totalTokenUsage: 45_000,
    });
  });

  it('keeps the cached home projection stable across usage-only refreshes', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const before = remoteSessionStore.getHomeSessions();
    const beforeRow = before[0];
    expect(beforeRow).toBeDefined();
    expect(beforeRow).not.toHaveProperty('totalCostUsd');

    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-spend-changed', {
      sessionId: 's1',
      totalCostUsd: 2.5,
    });
    expect(remoteSessionStore.getHomeSessions()).toBe(before);
    expect(remoteSessionStore.getHomeSessions()[0]).toBe(beforeRow);

    // A later ordinary recompute must still reuse the projection because usage
    // fields are intentionally absent from Home.
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'dev-1', name: 'Mac' }]);
    expect(remoteSessionStore.getHomeSessions()).toBe(before);
    expect(remoteSessionStore.getHomeSessions()[0]).toBe(beforeRow);
    expect(remoteSessionStore.getHomeSessions()[0]).not.toHaveProperty('totalCostUsd');
  });

  it('black-box: repeated device refreshes keep the visible home snapshot stable', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const before = remoteSessionStore.getHomeSessions();
    const beforeRows = [...before];

    for (let index = 0; index < 100; index += 1) {
      remoteSessionStore.setDeviceIdentity([
        { deviceId: 'dev-1', name: 'Mac' },
        { deviceId: `unrelated-${index}`, name: `Other ${index}` },
      ]);
    }

    const after = remoteSessionStore.getHomeSessions();
    expect(after).toBe(before);
    expect(after).toEqual(beforeRows);
    expect(after.every((row) => !Object.prototype.hasOwnProperty.call(row, 'totalCostUsd'))).toBe(true);
    expect(after.every((row) => !Object.prototype.hasOwnProperty.call(row, 'totalMoney'))).toBe(true);
    expect(after.every((row) => !Object.prototype.hasOwnProperty.call(row, 'totalTokenUsage'))).toBe(true);
  });

  it('refreshes Home metadata after unchanged device refreshes and usage-only pushes', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const before = remoteSessionStore.getHomeSessions();
    const homeChanged = vi.fn();
    const unsubscribe = remoteSessionStore.subscribeHomeStatus(homeChanged);
    try {
      remoteSessionStore.applyRemotePush('dev-1', 'usage:session-tokens-changed', { sessionId: 's1', totalTokens: 500 });
      for (let index = 0; index < 10; index += 1) {
        remoteSessionStore.setDeviceIdentity([
          { deviceId: 'dev-1', name: 'Mac' },
          { deviceId: `other-${index}`, name: 'Other' },
        ]);
      }
      expect(remoteSessionStore.getHomeSessions()).toBe(before);
      expect(homeChanged).not.toHaveBeenCalled();
      expect(remoteSessionStore.getSessions()[0].totalTokenUsage).toBe(500);
      expect(remoteSessionStore.getDeviceIdentity()[1].deviceId).toBe('other-9');

      remoteSessionStore.applySessionPatch('dev-1', 's1', { title: 'Renamed' });
      const renamed = remoteSessionStore.getHomeSessions();
      expect(renamed).not.toBe(before);
      expect(renamed[0]).toMatchObject({ id: 's1', title: 'Renamed' });
      expect(renamed[0]).not.toHaveProperty('totalTokenUsage');
      expect(renamed[1]).toBe(before[1]);
      expect(homeChanged).toHaveBeenCalledTimes(1);

      remoteSessionStore.renameDevice('dev-1', 'New Mac');
      expect(remoteSessionStore.getHomeSessions().every((row) => row.deviceLinkDeviceName === 'New Mac')).toBe(true);
      expect(homeChanged).toHaveBeenCalledTimes(2);
    } finally {
      unsubscribe();
    }
  });

  it.each(['remove', 'clear'] as const)('rebuilds Home after %s and reusing a session ID', (action) => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const before = remoteSessionStore.getHomeSessions();
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s2'), session('s1')]);
    expect(remoteSessionStore.getHomeSessions()).toEqual([before[1], before[0]]);
    expect(remoteSessionStore.getHomeSessions()[0]).toBe(before[1]);

    if (action === 'clear') remoteSessionStore.clear();
    else remoteSessionStore.setDeviceSessions('dev-1', 'Mac', []);
    expect(remoteSessionStore.getHomeSessions()).toEqual([]);
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    expect(remoteSessionStore.getHomeSessions()).toHaveLength(1);
    expect(remoteSessionStore.getHomeSessions()[0]).not.toBe(before[0]);
    expect(remoteSessionStore.getHomeSessions()[0]).toMatchObject({ id: 's1', title: 's1' });
    remoteSessionStore.applySessionPatch('dev-1', 's1', { title: 'Restored title' });
    expect(remoteSessionStore.getHomeSessions()[0].title).toBe('Restored title');
  });

  it('keeps metadata patches on the canonical shard and preserves hidden rows and usage projection', () => {
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'current', name: 'Mac' }]);
    remoteSessionStore.setDeviceSessions('stale', 'Mac', [session('s1', { title: 'Stale' })]);
    remoteSessionStore.setDeviceSessions('current', 'Mac', [session('s1'), session('s2')]);
    const other = remoteSessionStore.getHomeSessions()[1];
    const before = remoteSessionStore.getSessions()[0];
    remoteSessionStore.applySessionPatch('stale', 's1', { title: 'Wrong', updatedAt: '2026-02-01T00:00:00Z' });
    expect(remoteSessionStore.getSessions()[0]).toBe(before);
    expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('current');
    const finishPreview = remoteSessionStore.beginSessionListRemovalPreview('s1');
    remoteSessionStore.applySessionPatch('current', 's1', {
      title: 'Renamed', updatedAt: '2026-02-02T00:00:00Z', totalTokenUsage: 500,
    });
    expect(remoteSessionStore.getHomeSessions()).toEqual([other]);
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({ title: 'Renamed', totalTokenUsage: 500 });
    finishPreview();
    expect(remoteSessionStore.getHomeSessions()[0]).toMatchObject({
      title: 'Renamed', updatedAt: '2026-02-02T00:00:00Z', canonicalDeviceId: 'current',
    });
    expect(remoteSessionStore.getHomeSessions()[0]).not.toHaveProperty('totalTokenUsage');
    expect(remoteSessionStore.getHomeSessions()[1]).toBe(other);
  });

  it('removes archived sessions from the active mirror', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { status: 'archived' });

    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s2']);
  });

  it('upserts forked sessions at the top of the controlled device shard', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('forked', { title: 'Forked' }));

    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['forked', 's1', 's2']);
    expect(remoteSessionStore.getSessionDeviceId('forked')).toBe('dev-1');

    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', { title: 'Updated' }));
    expect(remoteSessionStore.getSessions().map((s) => [s.id, s.title])).toEqual([
      ['s1', 'Updated'],
      ['forked', 'Forked'],
      ['s2', 's2'],
    ]);
  });

  it('preserves the main-memory Agent switch intent across SQLite session snapshots', () => {
    const intent = {
      targetAgentKind: 'codex' as const,
      model: 'gpt-5.5',
      providerId: 'openai',
      effort: 'high',
      fastMode: true,
    };
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { agentSwitchIntent: intent });

    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: 'Fresh list' })]);
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      title: 'Fresh list',
      agentSwitchIntent: intent,
    });

    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', { title: 'Fresh detail' }));
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      title: 'Fresh detail',
      agentSwitchIntent: intent,
    });

    remoteSessionStore.applySessionPatch('dev-1', 's1', { agentSwitchIntent: null });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: 'After cancel' })]);
    expect(remoteSessionStore.getSessions()[0].agentSwitchIntent).toBeNull();
  });

  it('applies runtime model projections and preserves them only for legacy snapshots', () => {
    const runtimeBaseline = {
      agentKind: 'codex' as const,
      model: 'gpt-baseline',
      providerId: 'xd',
      effort: 'high',
      fastMode: false,
    };
    const runtimeEffective = {
      agentKind: 'codex' as const,
      model: 'gpt-runtime',
      providerId: 'openai',
      effort: 'xhigh',
      fastMode: true,
    };
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.applySessionPatch('dev-1', 's1', {
      model: runtimeEffective.model,
      providerId: runtimeEffective.providerId,
      effort: runtimeEffective.effort,
      fastMode: runtimeEffective.fastMode,
      runtimeGeneration: 3,
      runtimeBaseline,
      runtimeEffective,
      runtimePending: null,
    });

    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: 'Legacy snapshot' }),
    ]);
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      title: 'Legacy snapshot',
      runtimeGeneration: 3,
      runtimeBaseline,
      runtimeEffective,
      runtimePending: null,
    });

    const settledBaseline = { ...runtimeBaseline, model: 'gpt-user-selected' };
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', {
        model: settledBaseline.model,
        runtimeGeneration: 0,
        runtimeBaseline: settledBaseline,
        runtimeEffective: settledBaseline,
        runtimePending: null,
      }),
    ]);
    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      model: 'gpt-user-selected',
      runtimeGeneration: 0,
      runtimeBaseline: settledBaseline,
      runtimeEffective: settledBaseline,
      runtimePending: null,
    });
  });

  it('clears stale effort for a fixed-strength runtime model', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { effort: 'high' })]);
    remoteSessionStore.applySessionPatch('dev-1', 's1', {
      model: 'fixed-strength-model',
      providerId: 'openai',
      effort: '',
      runtimeEffective: {
        agentKind: 'codex',
        model: 'fixed-strength-model',
        providerId: 'openai',
        effort: null,
        fastMode: false,
      },
    });

    expect(remoteSessionStore.getSessions()[0]).toMatchObject({
      model: 'fixed-strength-model',
      effort: '',
      runtimeEffective: { effort: null },
    });
  });

  it('does not let a draft sentinel snapshot replace an optimistic first-message title', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: '帮我排查登录失败' }),
    ]);
    remoteSessionStore.setPendingTitlePreview('s1', '帮我排查登录失败');
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: 'New Maker' }),
    ]);
    expect(remoteSessionStore.getSessions()[0]?.title).toBe('帮我排查登录失败');

    remoteSessionStore.applySessionPatch('dev-1', 's1', { title: 'New Maker' });
    expect(remoteSessionStore.getSessions()[0]?.title).toBe('帮我排查登录失败');

    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: '登录失败排查' }),
    ]);
    expect(remoteSessionStore.getSessions()[0]?.title).toBe('登录失败排查');
  });

  it('keeps the first-message preview after pendingLocalCreation settles', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: '帮我排查登录失败', pendingLocalCreation: true }),
    ]);
    remoteSessionStore.setPendingTitlePreview('s1', '帮我排查登录失败');
    remoteSessionStore.applySessionPatch('dev-1', 's1', { pendingLocalCreation: false });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: 'New Maker' }),
    ]);
    expect(remoteSessionStore.getSessions()[0]?.title).toBe('帮我排查登录失败');
    expect(remoteSessionStore.getSessions()[0]?.pendingLocalCreation).toBeFalsy();
  });

  it('lets an authoritative New Maker rename through after the preview is cleared', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: '帮我排查登录失败' }),
    ]);
    remoteSessionStore.setPendingTitlePreview('s1', '帮我排查登录失败');
    remoteSessionStore.clearPendingTitlePreview('s1');
    remoteSessionStore.applySessionPatch('dev-1', 's1', { title: 'New Maker' });
    expect(remoteSessionStore.getSessions()[0]?.title).toBe('New Maker');
  });

  it('keeps a title preview across a stale list that temporarily omits the new session', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s-old'),
    ]);
    remoteSessionStore.upsertDeviceSession(
      'dev-1',
      'Mac',
      session('s-new', { title: '帮我排查登录失败', pendingLocalCreation: true }),
    );
    remoteSessionStore.setPendingTitlePreview('s-new', '帮我排查登录失败');
    remoteSessionStore.applySessionPatch('dev-1', 's-new', { pendingLocalCreation: false });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s-old'),
    ]);
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s-old'),
      session('s-new', { title: 'New Maker' }),
    ]);
    expect(remoteSessionStore.getSessions().find((row) => row.id === 's-new')?.title).toBe('帮我排查登录失败');
  });

  it('dedupes an unchanged message push by id or client id', () => {
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    const versionAfterSet = remoteSessionStore.getMessageVersion();
    remoteSessionStore.appendMessage('s1', message('m1', 's1'));

    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(remoteSessionStore.getMessageVersion()).toBe(versionAfterSet);
  });

  it('upserts a changed message push instead of keeping the stale duplicate', () => {
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1',
      message: message('m1', 's1'),
    });
    const versionAfterCreate = remoteSessionStore.getMessageVersion();

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1',
      message: { ...message('m1', 's1'), content: 'updated' },
    });

    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(remoteSessionStore.getMessages('s1')[0].content).toBe('updated');
    expect(remoteSessionStore.getMessageVersion()).toBeGreaterThan(versionAfterCreate);
  });

  it('removes a deleted AI round from the device-link transcript mirror', () => {
    remoteSessionStore.setMessages('s1', [
      message('m1', 's1'),
      message('m2', 's1'),
      message('m3', 's1'),
    ]);
    remoteSessionStore.markSessionMessagesSynced('s1', {
      _count: { messages: 3 },
      updatedAt: '2026-01-01T00:00:00.000Z',
    });
    pushMakerTaskUpdate('s1', 'm1');
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBeGreaterThan(0);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:deleted', {
      sessionId: 's1',
      clientId: 'm1',
      clientIds: ['m1', 'm2'],
    });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual(['m3']);
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(0);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', {
      _count: { messages: 3 },
      updatedAt: '2026-01-01T00:00:00.000Z',
    })).toBe(false);
  });

  it('removes a rejected persisted draft while a newer assistant keeps streaming', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setMessages('s1', [{ ...message('host-a', 's1'), clientId: 'item-a', content: 'Withdrawn' }]);
      pushMakerText('s1', 'item-b', 'Next', false);
      vi.runOnlyPendingTimers();
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:deleted', {
        sessionId: 's1', clientId: 'item-a', clientIds: ['item-a'],
      });
      pushMakerText('s1', undefined, ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toEqual([expect.objectContaining({
        clientId: 'item-b', content: 'Next tail', agentMeta: expect.objectContaining({ isStreaming: true }),
      })]);
    } finally { vi.useRealTimers(); }
  });

  it('removes an orphan task update even when its deleted message is not cached', () => {
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    pushMakerTaskUpdate('s1', 'orphan-task');
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBeGreaterThan(0);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:deleted', {
      sessionId: 's1',
      clientId: 'orphan-task',
      clientIds: ['orphan-task'],
    });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual(['m1']);
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(0);
  });

  it('repairs a missing streaming prefix on reopen and keeps subsequent deltas exactly once', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'live-id', ' suffix', false);
      const snapshot = {
        sessionId: 's1', persistId: 'live-id',
        event: { type: 'text', data: { text: 'prefix suffix', isFinal: false, isFullText: true } },
      };
      remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, snapshot);
      remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, snapshot);
      pushMakerText('s1', 'live-id', ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
      expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
        content: 'prefix suffix tail', agentMeta: { isStreaming: true },
      });
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', message: { ...message('host-id', 's1'), clientId: 'live-id', content: 'prefix suffix tail final' },
      });
      remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, snapshot);
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
      expect(remoteSessionStore.getMessages('s1')[0].content).toBe('prefix suffix tail final');
    } finally { vi.useRealTimers(); }
  });

  it.each([['db-row', 'item-a'], ['db-row', 'host-a'], ['history', 'item-a'], ['history', 'host-a']] as const)('keeps anonymous B when initially unseen old A arrives before its final (%s, %s)', (arrival, finalId) => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', undefined, 'Sure', false);
      vi.runOnlyPendingTimers();
      const newer = remoteSessionStore.getMessages('s1').find((row) => row.clientId?.startsWith('mobile-stream-'))!;
      const older = { ...message('host-a', 's1'), clientId: 'item-a', content: 'Sure, that was the previous reply' };
      if (arrival === 'db-row') {
        remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
          sessionId: 's1', message: older,
        });
      } else {
        remoteSessionStore.setLatestMessageWindow('s1', [older]);
      }
      // An equal DB replay must not retire B before the full correction either.
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', message: older,
      });
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', persistId: finalId, event: {
          type: 'text', data: { text: 'Corrected older reply', isFinal: true, isFullText: true },
        },
      });
      const corrected = { ...older, content: 'Corrected older reply' };
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', message: corrected,
      });
      remoteSessionStore.setLatestMessageWindow('s1', [corrected]);
      pushMakerText('s1', undefined, ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === newer.clientId))
        .toMatchObject({ content: 'Sure tail', agentMeta: { isStreaming: true } });
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === 'item-a')?.content).toBe('Corrected older reply');
    } finally { vi.useRealTimers(); }
  });

  it.each([false, true, 'anonymous', 'anonymous-same-prefix'] as const)('calibrates a late final by identity without taking over a newer stream (newer=%s)', (newer) => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setMessages('s1', [{
        ...message('host-a', 's1'), clientId: 'item-a', content: 'Incorrect long draft',
      }]);
      const anonymous = typeof newer === 'string';
      const nextText = newer === 'anonymous-same-prefix' ? 'Correct' : 'Next';
      if (newer) {
        pushMakerText('s1', anonymous ? undefined : 'item-b', nextText, false);
        vi.runOnlyPendingTimers();
      }
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', persistId: 'item-a', event: {
          type: 'text', data: { text: 'Corrected', isFinal: true, isFullText: true },
        },
      });
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === 'item-a')?.content).toBe('Corrected');
      if (newer) {
        remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
          sessionId: 's1', message: { ...message('host-a', 's1'), clientId: 'item-a', content: 'Corrected' },
        });
        pushMakerText('s1', undefined, ' tail', false);
        vi.runOnlyPendingTimers();
        expect(remoteSessionStore.getMessages('s1').find((row) => anonymous ? row.clientId?.startsWith('mobile-stream-') : row.clientId === 'item-b'))
          .toMatchObject({ content: `${nextText} tail`, agentMeta: { isStreaming: true } });
      }
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(newer ? 2 : 1);
    } finally { vi.useRealTimers(); }
  });

  it.each(['db-row', 'full-final'] as const)('keeps an anonymous stream when an older row reappears outside the current window (%s)', (arrival) => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setMessages('s1', [{ ...message('host-a', 's1'), clientId: 'item-a', content: 'Old draft' }]);
      const prefix = 'A shared introductory sentence';
      pushMakerText('s1', undefined, prefix, false);
      vi.runOnlyPendingTimers();
      const newer = remoteSessionStore.getMessages('s1').find((row) => row.clientId?.startsWith('mobile-stream-'))!;
      remoteSessionStore.setMessages('s1', [newer]);
      if (arrival === 'full-final') {
        remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
          sessionId: 's1', persistId: 'item-a', event: {
            type: 'text', data: { text: `${prefix} from the older reply`, isFinal: true, isFullText: true },
          },
        });
      }
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', message: { ...message('host-a', 's1'), clientId: 'item-a', content: `${prefix} from the older reply` },
      });
      pushMakerText('s1', undefined, ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === newer.clientId)?.content).toBe(`${prefix} tail`);
    } finally { vi.useRealTimers(); }
  });

  it.each([false, true, 'db-first'] as const)('keeps a known older row from claiming a matching anonymous stream through history (cropped=%s)', (cropped) => {
    vi.useFakeTimers();
    try {
      const older = { ...message('host-a', 's1'), clientId: 'item-a', content: 'partial answer from earlier' };
      if (cropped !== 'db-first') remoteSessionStore.setMessages('s1', [older]);
      pushMakerText('s1', undefined, cropped === 'db-first' ? 'partial' : 'partial answer', false);
      vi.runOnlyPendingTimers();
      const newer = remoteSessionStore.getMessages('s1').find((row) => row.clientId?.startsWith('mobile-stream-'))!;
      if (cropped === 'db-first') {
        remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', { sessionId: 's1', message: older });
        pushMakerText('s1', undefined, ' answer', false);
        vi.runOnlyPendingTimers();
      }
      if (cropped) remoteSessionStore.setMessages('s1', [remoteSessionStore.getMessages('s1').find((row) => row.clientId === newer.clientId)!]);
      remoteSessionStore.setLatestMessageWindow('s1', [older]);
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', persistId: 'item-a', event: {
          type: 'text', data: { text: 'Corrected older reply', isFinal: true, isFullText: true },
        },
      });
      pushMakerText('s1', undefined, ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === newer.clientId))
        .toMatchObject({ content: 'partial answer tail', agentMeta: { isStreaming: true } });
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === 'item-a')?.content).toBe('Corrected older reply');
    } finally { vi.useRealTimers(); }
  });

  it.each(['Sure, checked', 'Corrected'])('migrates a short anonymous stream when an identified delta follows its DB row (%s)', (finalText) => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', undefined, 'Sure', false);
      vi.runOnlyPendingTimers();
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', message: { ...message('host-a', 's1'), clientId: 'item-a', content: 'Sure, checking' },
      });
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
      const anonymousId = remoteSessionStore.getMessages('s1').find((row) => row.clientId?.startsWith('mobile-stream-'))!.clientId;
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', persistId: 'item-a', event: {
          type: 'text', data: { text: finalText, isFinal: true, isFullText: true },
        },
      });
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === anonymousId)?.content).toBe('Sure');
      pushMakerText('s1', 'item-a', ', checking', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', persistId: 'item-a', event: {
          type: 'text', data: { text: finalText, isFinal: true, isFullText: true },
        },
      });
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
      pushMakerText('s1', undefined, ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toEqual([expect.objectContaining({
        id: 'host-a', clientId: 'item-a', content: `${finalText} tail`,
      })]);
    } finally { vi.useRealTimers(); }
  });

  // PR #6532: a legacy delta may omit persistId while still carrying the Codex
  // item identity. Its final must merge the temporary bubble without another delta.
  // Unit: applyRemotePush -> observable row count, authoritative content and tail.
  it.each(['Sure, checked', 'Corrected'])('migrates a short unpersisted stream on its own identified final (%s)', (finalText) => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', event: {
          type: 'text', source: 'codex',
          data: { text: 'Sure', isFinal: false, agentMessageId: 'provider-item-a' },
        },
      });
      vi.runOnlyPendingTimers();
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', message: { ...message('host-a', 's1'), clientId: 'item-a', content: 'Sure, checking' },
      });
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', persistId: 'item-a', event: {
          type: 'text', source: 'codex',
          data: { text: finalText, isFinal: true, isFullText: true, agentMessageId: 'provider-item-a' },
        },
      });
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
      pushMakerText('s1', undefined, ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toEqual([expect.objectContaining({
        id: 'host-a', clientId: 'item-a', content: `${finalText} tail`,
      })]);
    } finally { vi.useRealTimers(); }
  });

  it.each(['other-item', 'other-source', 'other-device', 'missing-item', 'mixed-batch', 'later-mixed-batch'] as const)(
    'does not let a final with unproven provider identity claim a short new stream (%s)', (mismatch) => {
      vi.useFakeTimers();
      try {
        remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
          sessionId: 's1', event: { type: 'text', source: 'codex',
            data: { text: 'Sure', isFinal: false, agentMessageId: 'provider-item-b' } },
        });
        if (mismatch === 'later-mixed-batch') vi.runOnlyPendingTimers();
        if (mismatch === 'mixed-batch' || mismatch === 'later-mixed-batch') {
          remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
            sessionId: 's1', event: { type: 'text', source: 'codex',
              data: { text: ' extra', isFinal: false, agentMessageId: 'provider-item-c' } },
          });
        }
        vi.runOnlyPendingTimers();
        const generatedId = remoteSessionStore.getMessages('s1')[0].clientId;
        remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
          sessionId: 's1', message: { ...message('host-a', 's1'), clientId: 'item-a', content: 'Sure, old reply' },
        });
        remoteSessionStore.applyRemotePush(mismatch === 'other-device' ? 'dev-2' : 'dev-1', 'maker:event', {
          sessionId: 's1', persistId: 'item-a', event: {
            type: 'text', source: mismatch === 'other-source' ? 'pi' : 'codex',
            data: { text: 'Corrected old reply', isFinal: true, isFullText: true,
              agentMessageId: mismatch === 'other-item' ? 'provider-item-a'
                : mismatch === 'missing-item' ? undefined : 'provider-item-b' },
          },
        });
        pushMakerText('s1', undefined, ' tail', false);
        vi.runOnlyPendingTimers();
        expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
        expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === generatedId))
          .toMatchObject({ content: mismatch.endsWith('mixed-batch') ? 'Sure extra tail' : 'Sure tail',
            agentMeta: { isStreaming: true } });
        expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId === 'item-a')?.content)
          .toBe('Corrected old reply');
      } finally { vi.useRealTimers(); }
    },
  );

  it('still accepts a legacy final full-text event after its partial row was persisted', () => {
    remoteSessionStore.setMessages('s1', [{
      ...message('host-id', 's1'), clientId: 'legacy-id', content: 'partial',
    }]);
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1', persistId: 'legacy-id', event: {
        type: 'text', data: { text: 'partial completed', isFinal: true, isFullText: true },
      },
    });
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
      id: 'host-id', content: 'partial completed',
    });
  });

  it.each([false, true])('orders the live snapshot by host time when history arrives later (existing=%s)', (existing) => {
    vi.useFakeTimers();
    try {
      const oldTime = '2026-09-05T10:00:00.000Z';
      const userTime = '2026-09-05T11:00:00.000Z';
      const blockTime = '2026-09-05T11:00:01.000Z';
      remoteSessionStore.setMessages('s1', [{ ...message('old', 's1'), createdAt: oldTime }]);
      if (existing) {
        pushMakerText('s1', 'live-id', 'suffix', false);
        vi.runOnlyPendingTimers();
        remoteSessionStore.mergeMessages('s1', [{ ...message('user', 's1'), role: 'user', createdAt: userTime }]);
      }
      remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, {
        sessionId: 's1', persistId: 'live-id', event: {
          type: 'text', data: { text: 'whole suffix', isFinal: false, isFullText: true, createdAt: blockTime },
        },
      });
      remoteSessionStore.mergeMessages('s1', [{ ...message('user', 's1'), role: 'user', createdAt: userTime }]);
      pushMakerText('s1', 'live-id', ' tail', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1').map(m => m.id)).toEqual(['old', 'user', 'live-id']);
      expect(remoteSessionStore.getMessages('s1').at(-1)).toMatchObject({
        content: 'whole suffix tail', createdAt: blockTime, agentMeta: { isStreaming: true },
      });
    } finally { vi.useRealTimers(); }
  });

  it('requests history reconciliation after a dropped push without clearing cached messages', () => {
    const meta = session('s1', { _count: { messages: 1 } });
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    remoteSessionStore.markSessionMessagesSynced('s1', meta);
    remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, { sessionId: 's1', resyncRequired: true });
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', meta)).toBe(false);
    expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(true);
    // An older in-flight history response commits after the dirty notification.
    remoteSessionStore.markSessionMessagesSynced('s1', meta);
    expect(remoteSessionStore.consumePendingRefresh('s1')).toBe(true);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', meta)).toBe(false);
  });

  it('retires a provisional time anchor even when the authoritative snapshot is identical', () => {
    vi.useFakeTimers();
    try {
      const blockTime = '2026-09-05T11:00:01.000Z';
      vi.setSystemTime(new Date(blockTime));
      pushMakerText('s1', 'live-id', 'whole text', false);
      vi.runOnlyPendingTimers();
      const createdAt = remoteSessionStore.getMessages('s1')[0].createdAt;
      remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, {
        sessionId: 's1', persistId: 'live-id', event: {
          type: 'text', data: { text: 'whole text', isFinal: false, isFullText: true, createdAt },
        },
      });
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        updatedAt: '2026-09-05T10:00:00.000Z', userSendAt: '2026-09-05T10:00:00.000Z',
      })]);
      expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
        createdAt, content: 'whole text', agentMeta: { isStreaming: true },
      });
    } finally { vi.useRealTimers(); }
  });

  it('batches maker text deltas into one streaming assistant row', () => {
    vi.useFakeTimers();
    const notify = vi.fn();
    const unsubscribe = remoteSessionStore.subscribe(notify);
    try {
      pushMakerText('s1', 'persist-1', 'hello', false);
      pushMakerText('s1', 'persist-1', ' world', false);

      expect(remoteSessionStore.getMessages('s1')).toHaveLength(0);
      expect(notify).not.toHaveBeenCalled();

      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
      expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
        id: 'persist-1',
        clientId: 'persist-1',
        role: 'assistant',
        content: 'hello world',
        agentMeta: { isStreaming: true },
      });
      expect(notify).toHaveBeenCalledTimes(1);
    } finally {
      unsubscribe();
      vi.useRealTimers();
    }
  });

  it('keeps message structure and home status stable across ordinary text deltas', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.enterSessionMessageDetail('s1');
      remoteSessionStore.setMessages('s1', Array.from({ length: 2_000 }, (_, index) => ({
        ...message(`history-${index}`, 's1'),
        createdAt: new Date(Date.UTC(2026, 0, 1, 0, 0, index)).toISOString(),
      })));
      pushMakerText('s1', 'live-tail', 'first', false);
      vi.runOnlyPendingTimers();

      const firstStructure = remoteSessionStore.getSessionMessageStructureToken('s1');
      const homeStatusBeforeDelta = remoteSessionStore.getHomeStatusVersion();
      const reduceSpy = vi.spyOn(Array.prototype, 'reduce');
      const filterSpy = vi.spyOn(Array.prototype, 'filter');
      try {
        pushMakerText('s1', 'live-tail', ' second', false);
        vi.runOnlyPendingTimers();

        expect(reduceSpy).not.toHaveBeenCalled();
        expect(filterSpy).not.toHaveBeenCalled();
      } finally {
        reduceSpy.mockRestore();
        filterSpy.mockRestore();
      }

      expect(remoteSessionStore.getMessages('s1').at(-1)?.content).toBe('first second');
      expect(remoteSessionStore.getSessionMessageStructureToken('s1')).toBe(firstStructure);
      expect(remoteSessionStore.getSessionMessageStructureChangedIndexes('s1')).toEqual(
        new Set([2_000]),
      );
      expect(remoteSessionStore.getHomeStatusVersion()).toBe(homeStatusBeforeDelta);
      expect(remoteSessionStore.getSessionMessagePreview('s1')).toBe('first second');

      pushMakerText(
        's1',
        'live-tail',
        'first second',
        true,
        { isStreaming: false, streaming: false },
      );
      expect(remoteSessionStore.getSessionMessageStructureToken('s1')).not.toBe(firstStructure);
    } finally {
      vi.useRealTimers();
    }
  });

  it('isolates empty message structure tokens by session', () => {
    const first = remoteSessionStore.getSessionMessageStructureToken('s1');
    expect(remoteSessionStore.getSessionMessageStructureToken('s1')).toBe(first);
    expect(remoteSessionStore.getSessionMessageStructureToken('s2')).not.toBe(first);

    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    expect(remoteSessionStore.getSessionMessageStructureToken('s1')).not.toBe(first);
  });

  it('notifies only the changed session preview subscription for text deltas', () => {
    vi.useFakeTimers();
    const firstPreview = vi.fn();
    const secondPreview = vi.fn();
    const homeStatus = vi.fn();
    const unsubscribeFirst = remoteSessionStore.subscribeSessionMessagePreview('s1', firstPreview);
    const unsubscribeSecond = remoteSessionStore.subscribeSessionMessagePreview('s2', secondPreview);
    const unsubscribeHomeStatus = remoteSessionStore.subscribeHomeStatus(homeStatus);
    try {
      pushMakerText('s1', 'persist-1', 'first', false);
      vi.advanceTimersByTime(32);

      expect(firstPreview).toHaveBeenCalledTimes(1);
      expect(secondPreview).not.toHaveBeenCalled();
      expect(homeStatus).not.toHaveBeenCalled();
    } finally {
      unsubscribeFirst();
      unsubscribeSecond();
      unsubscribeHomeStatus();
      vi.useRealTimers();
    }
  });

  it('keeps the first text flush responsive and batches continuation deltas', () => {
    vi.useFakeTimers();
    const notify = vi.fn();
    const unsubscribe = remoteSessionStore.subscribe(notify);
    try {
      remoteSessionStore.enterSessionMessageDetail('s1');
      pushMakerText('s1', 'persist-1', 'first', false);
      vi.advanceTimersByTime(31);
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(0);
      expect(notify).not.toHaveBeenCalled();

      vi.advanceTimersByTime(1);
      expect(remoteSessionStore.getMessages('s1')[0]?.content).toBe('first');
      expect(notify).toHaveBeenCalledTimes(1);
      notify.mockClear();

      pushMakerText('s1', 'persist-1', ' second', false);
      vi.advanceTimersByTime(40);
      pushMakerText('s1', 'persist-1', ' third', false);
      expect(remoteSessionStore.getMessages('s1')[0]?.content).toBe('first');
      expect(notify).not.toHaveBeenCalled();

      vi.advanceTimersByTime(24);
      expect(remoteSessionStore.getMessages('s1')[0]?.content).toBe('first second third');
      expect(notify).toHaveBeenCalledTimes(1);
    } finally {
      unsubscribe();
      vi.useRealTimers();
    }
  });

  it('batches background continuation deltas more aggressively than visible detail', () => {
    vi.useFakeTimers();
    const notify = vi.fn();
    const unsubscribe = remoteSessionStore.subscribe(notify);
    try {
      pushMakerText('s1', 'persist-1', 'first', false);
      vi.advanceTimersByTime(32);
      notify.mockClear();

      pushMakerText('s1', 'persist-1', ' second', false);
      vi.advanceTimersByTime(64);
      expect(remoteSessionStore.getMessages('s1')[0]?.content).toBe('first');
      expect(notify).not.toHaveBeenCalled();

      vi.advanceTimersByTime(32);
      expect(remoteSessionStore.getMessages('s1')[0]?.content).toBe('first second');
      expect(notify).toHaveBeenCalledTimes(1);
    } finally {
      unsubscribe();
      vi.useRealTimers();
    }
  });

  it('lets a new session first delta accelerate an existing continuation timer', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'first', false);
      vi.advanceTimersByTime(32);
      pushMakerText('s1', 'persist-1', ' continuation', false);

      vi.advanceTimersByTime(20);
      pushMakerText('s2', 'persist-2', 'new session', false);
      vi.advanceTimersByTime(31);
      expect(remoteSessionStore.getMessages('s1')[0]?.content).toBe('first');
      expect(remoteSessionStore.getMessages('s2')).toHaveLength(0);

      vi.advanceTimersByTime(1);
      expect(remoteSessionStore.getMessages('s1')[0]?.content).toBe('first continuation');
      expect(remoteSessionStore.getMessages('s2')[0]?.content).toBe('new session');
    } finally {
      vi.useRealTimers();
    }
  });

  it('reuses the identity index when a streaming assistant is not the tail row', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.enterSessionMessageDetail('s1');
      remoteSessionStore.setMessages('s1', Array.from({ length: 2_000 }, (_, index) => ({
        ...message(`history-${index}`, 's1'),
        createdAt: new Date(Date.UTC(2026, 0, 1, 0, 0, index)).toISOString(),
      })));
      pushMakerText('s1', 'live-before-system-card', 'first', false);
      vi.runOnlyPendingTimers();
      remoteSessionStore.appendLocalSystemCard(
        's1',
        'context',
        { context: 'tail card' },
        new Date('2026-01-02T00:00:00.000Z'),
      );

      // The first non-tail delta builds the identity index. Its replacement inherits that index.
      pushMakerText('s1', 'live-before-system-card', ' second', false);
      vi.runOnlyPendingTimers();
      const findIndexSpy = vi.spyOn(Array.prototype, 'findIndex');
      try {
        pushMakerText('s1', 'live-before-system-card', ' third', false);
        vi.runOnlyPendingTimers();

        expect(findIndexSpy).not.toHaveBeenCalled();
        expect(remoteSessionStore.getMessages('s1').find(
          (item) => item.clientId === 'live-before-system-card',
        )?.content).toBe('first second third');
      } finally {
        findIndexSpy.mockRestore();
      }
    } finally {
      vi.useRealTimers();
    }
  });

  it('clears content-only indexes after append, prepend, final, and reset writes', () => {
    vi.useFakeTimers();
    let sequence = 0;
    const establishContentOnlyDelta = (persistId: string): object => {
      pushMakerText('s1', persistId, 'first', false);
      vi.runOnlyPendingTimers();
      const structureToken = remoteSessionStore.getSessionMessageStructureToken('s1');
      pushMakerText('s1', persistId, ' second', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getSessionMessageStructureToken('s1')).toBe(structureToken);
      expect(remoteSessionStore.getSessionMessageStructureChangedIndexes('s1').size).toBe(1);
      return structureToken;
    };
    const expectStructuralReset = (previousToken: object): void => {
      expect(remoteSessionStore.getSessionMessageStructureToken('s1')).not.toBe(previousToken);
      expect(remoteSessionStore.getSessionMessageStructureChangedIndexes('s1').size).toBe(0);
    };
    const nextMessage = (prefix: string, createdAt: string): RemoteMessage => {
      sequence += 1;
      return messageAt(`${prefix}-${sequence}`, 's1', createdAt);
    };
    try {
      let previousToken = establishContentOnlyDelta('live-before-append');
      remoteSessionStore.appendMessage(
        's1',
        nextMessage('append', '2026-01-02T00:00:00.000Z'),
      );
      expectStructuralReset(previousToken);

      previousToken = establishContentOnlyDelta('live-before-prepend');
      remoteSessionStore.mergeEarlierMessages(
        's1',
        [nextMessage('prepend', '2025-12-31T00:00:00.000Z')],
      );
      expectStructuralReset(previousToken);

      previousToken = establishContentOnlyDelta('live-before-final');
      pushMakerText('s1', 'live-before-final', 'first second', true, {
        isStreaming: false,
        streaming: false,
      });
      expectStructuralReset(previousToken);

      previousToken = establishContentOnlyDelta('live-before-reset');
      remoteSessionStore.setMessages(
        's1',
        [nextMessage('reset', '2026-01-03T00:00:00.000Z')],
      );
      expectStructuralReset(previousToken);
    } finally {
      vi.useRealTimers();
    }
  });

  it('finalizes the previous streaming assistant when the persist id changes', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'commentary-row', 'Inspecting the workspace', false);
      vi.runOnlyPendingTimers();

      pushMakerText('s1', 'final-answer-row', 'The fix is ready', false);
      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([
        {
          clientId: 'commentary-row',
          content: 'Inspecting the workspace',
          agentMeta: null,
        },
        {
          clientId: 'final-answer-row',
          content: 'The fix is ready',
          agentMeta: { isStreaming: true },
        },
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it.each([
    ['without persistId', undefined],
    ['with the same persistId', 'shared-live-assistant'],
  ] as const)('flushes a text delta batch when the transport changes %s', (_label, persistId) => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        ...(persistId ? { persistId } : {}),
        event: {
          type: 'text',
          data: { text: 'Stale reply', isFinal: false },
        },
      });
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        ...(persistId ? { persistId } : {}),
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: false },
        },
      });

      remoteSessionStore.removeDevice('stale-mac');
      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        content: 'Current reply',
        role: 'assistant',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('resets a shared-persist streaming row after both transport batches flush', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale reply', isFinal: false },
        },
      });
      vi.runOnlyPendingTimers();

      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: false },
        },
      });
      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'shared-live-assistant',
        content: 'Current reply',
        role: 'assistant',
      }]);

      remoteSessionStore.removeDevice('stale-mac');

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'shared-live-assistant',
        content: 'Current reply',
        role: 'assistant',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('tracks transport stream assembly independently for interleaved persist ids', () => {
    vi.useFakeTimers();
    try {
      for (const [deviceId, persistId, text] of [
        ['stale-mac', 'assistant-a', 'Stale A'],
        ['stale-mac', 'assistant-b', 'Stale B'],
        ['current-mac', 'assistant-a', 'Current A'],
      ] as const) {
        remoteSessionStore.applyRemotePush(deviceId, 'maker:event', {
          sessionId: 's1',
          persistId,
          event: {
            type: 'text',
            data: { text, isFinal: false },
          },
        });
        vi.runOnlyPendingTimers();
      }

      expect(remoteSessionStore.getMessages('s1').map((item) => ({
        clientId: item.clientId,
        content: item.content,
      })).sort((left, right) => left.clientId.localeCompare(right.clientId))).toEqual([
        { clientId: 'assistant-a', content: 'Current A' },
        { clientId: 'assistant-b', content: 'Stale B' },
      ]);

      remoteSessionStore.removeDevice('stale-mac');

      expect(remoteSessionStore.getMessages('s1').map((item) => ({
        clientId: item.clientId,
        content: item.content,
      }))).toEqual([
        { clientId: 'assistant-a', content: 'Current A' },
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('treats final text as a complete block and clears streaming at done', () => {
    pushMakerText('s1', 'persist-1', 'hello', false);
    pushMakerText('s1', 'persist-1', 'hello world', true, { model: 'claude' });

    expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
      clientId: 'persist-1',
      content: 'hello world',
      agentMeta: { isStreaming: true, model: 'claude' },
    }]);

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'done', data: {} },
    });

    expect(remoteSessionStore.getMessages('s1')[0].agentMeta).toEqual({ model: 'claude' });
  });

  it('appends a fallback-tail final event to the accumulated streaming text', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'already visible ', false);
      vi.runOnlyPendingTimers();

      pushMakerText('s1', 'persist-1', 'recovered tail', true);

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'persist-1',
        content: 'already visible recovered tail',
        agentMeta: { isStreaming: true },
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('emits once when done flushes text and closes the running turn', () => {
    vi.useFakeTimers();
    const notify = vi.fn();
    const unsubscribe = remoteSessionStore.subscribe(notify);
    try {
      pushMakerStatus('s1', { isRunning: true });
      notify.mockClear();

      pushMakerText('s1', 'persist-1', 'complete on done', false);
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1',
        event: { type: 'done', data: {} },
      });

      expect(notify).toHaveBeenCalledTimes(1);
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'persist-1',
        content: 'complete on done',
        agentMeta: null,
      }]);
      expect(remoteSessionStore.isSessionRunning('s1')).toBe(false);
      expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);
    } finally {
      unsubscribe();
      vi.useRealTimers();
    }
  });

  it('replaces the temporary streaming row when the persisted message arrives', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'partial', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1',
        message: {
          id: 'message-1',
          clientId: 'persist-1',
          sessionId: 's1',
          role: 'assistant',
          content: 'partial and complete',
          toolUseId: null,
          agentMeta: { model: 'claude' },
          createdAt: '2026-01-01T00:00:01.000Z',
        },
      });
      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        id: 'message-1',
        clientId: 'persist-1',
        content: 'partial and complete',
        agentMeta: { model: 'claude' },
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('reconciles a generated fallback row when DB create has a new identity', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', undefined, 'partial answer', false);
      vi.runOnlyPendingTimers();

      const temporary = remoteSessionStore.getMessages('s1')[0];
      expect(temporary.clientId).toMatch(/^mobile-stream-/);

      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1',
        message: {
          id: 'persisted-1',
          clientId: 'persisted-1',
          sessionId: 's1',
          role: 'assistant',
          content: 'partial answer and complete',
          toolUseId: null,
          agentMeta: null,
          createdAt: '2026-01-01T00:00:01.000Z',
        },
      });

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        id: 'persisted-1',
        clientId: 'persisted-1',
        content: 'partial answer and complete',
        agentMeta: null,
      }]);
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    } finally {
      vi.useRealTimers();
    }
  });

  it('reconciles a generated fallback row when history sync is the first DB identity', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', undefined, 'partial answer', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.setLatestMessageWindow('s1', [{
        id: 'history-persisted-1',
        clientId: 'history-persisted-1',
        sessionId: 's1',
        role: 'assistant',
        content: 'partial answer and complete',
        toolUseId: null,
        agentMeta: null,
        createdAt: '2026-01-01T00:00:01.000Z',
      }]);

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        id: 'history-persisted-1',
        clientId: 'history-persisted-1',
        content: 'partial answer and complete',
      }]);
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not retire a generated fallback on a short ambiguous prefix', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', undefined, 'Sure', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1',
        message: {
          id: 'old-persisted-2',
          clientId: 'old-persisted-2',
          sessionId: 's1',
          role: 'assistant',
          content: 'Sure, that was the previous turn',
          toolUseId: null,
          agentMeta: null,
          createdAt: '2026-01-01T00:00:01.000Z',
        },
      });

      expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
      expect(remoteSessionStore.getMessages('s1').find((row) => row.clientId.startsWith('mobile-stream-'))).toMatchObject({
        content: 'Sure',
        agentMeta: { isStreaming: true },
      });
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not replace a live fallback row with an unrelated delayed DB message', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', undefined, 'new turn partial', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1',
        message: {
          id: 'old-persisted-1',
          clientId: 'old-persisted-1',
          sessionId: 's1',
          role: 'assistant',
          content: 'old turn answer',
          toolUseId: null,
          agentMeta: null,
          createdAt: '2026-01-01T00:00:01.000Z',
        },
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows).toHaveLength(2);
      expect(rows.find((row) => row.clientId.startsWith('mobile-stream-'))).toMatchObject({
        content: 'new turn partial',
        agentMeta: { isStreaming: true },
      });
      expect(rows.find((row) => row.id === 'old-persisted-1')).toMatchObject({ content: 'old turn answer' });
    } finally {
      vi.useRealTimers();
    }
  });

  it('migrates a fallback streaming row when a later event carries persistId', () => {
    pushMakerText('s1', undefined, 'partial ', false);
    pushMakerText('s1', 'persist-1', 'partial and complete', true);

    expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
      id: 'persist-1',
      clientId: 'persist-1',
      content: 'partial and complete',
      agentMeta: { isStreaming: true },
    }]);
  });

  it('ends the current streaming block at a tool boundary', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'before tool', false);
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1',
        event: { type: 'tool_use', data: { toolUseId: 'tool-1' } },
      });

      expect(remoteSessionStore.getMessages('s1')[0].agentMeta).toBeNull();

      pushMakerText('s1', 'persist-2', 'after tool', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([
        { clientId: 'persist-1', content: 'before tool', agentMeta: null },
        { clientId: 'persist-2', content: 'after tool', agentMeta: { isStreaming: true } },
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('finalizes live text on idle recovery and does not erase it from an empty window', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'still streaming', false);
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:activity', {
        sessionId: 's1',
        phase: 'completed',
        compactDetail: '',
      });
      vi.runOnlyPendingTimers();

      remoteSessionStore.setLatestMessageWindow('s1', []);
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'persist-1',
        content: 'still streaming',
        agentMeta: null,
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('finalizes live text when a reconnect snapshot restores a pending interaction', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'waiting for approval', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.setPendingInteractions(
        's1',
        [pending('permission', 'req-1')],
        { finalizeStreaming: true },
      );

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'persist-1',
        content: 'waiting for approval',
        agentMeta: null,
      }]);
      expect(remoteSessionStore.getPendingInteractions('s1')).toHaveLength(1);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not finalize streaming when the reconnect pending snapshot is empty', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'still generating', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.setPendingInteractions('s1', [], { finalizeStreaming: true });

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'persist-1',
        content: 'still generating',
        agentMeta: { isStreaming: true },
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('matches persisted rows by clientId without content-prefix guesses', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'partial', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('message-1', 's1', '2026-01-01T00:00:01.000Z'),
        clientId: 'persist-1',
        content: 'partial and complete',
        agentMeta: { model: 'claude' },
      }]);

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        id: 'message-1',
        clientId: 'persist-1',
        content: 'partial and complete',
        agentMeta: { model: 'claude' },
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps accumulated text when the final event is device-link truncated', () => {
    pushMakerText('s1', 'persist-1', '前半段', false);
    pushMakerText('s1', 'persist-1', '后半段', false);
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      persistId: 'persist-1',
      event: {
        type: 'text',
        __deviceLinkTruncated: true,
        data: { text: '前半段\n[device-link truncated]', isFinal: true, __deviceLinkTruncated: true },
      },
    });

    expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
      content: '前半段后半段',
      agentMeta: { isStreaming: true },
    });
  });

  it('applies live update_plan snapshots to the persisted task row', () => {
    const initialPlan = {
      ...message('plan-row-1', 's1'),
      role: 'tool_use' as const,
      toolUseId: 'plan:turn-1',
      content: {
        toolUseId: 'plan:turn-1',
        toolName: 'update_plan',
        input: {
          plan: [
            { step: 'Inspect', status: 'in_progress' },
            { step: 'Patch', status: 'pending' },
          ],
        },
      },
    };
    remoteSessionStore.setMessages('s1', [initialPlan]);

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      persistId: 'plan-row-1',
      event: {
        type: 'tool_use',
        data: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: {
            plan: [
              { step: 'Inspect', status: 'completed' },
              { step: 'Patch', status: 'completed' },
            ],
          },
        },
      },
    });

    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
      id: 'plan-row-1',
      toolUseId: 'plan:turn-1',
      content: {
        toolUseId: 'plan:turn-1',
        toolName: 'update_plan',
        input: {
          plan: [
            { step: 'Inspect', status: 'completed' },
            { step: 'Patch', status: 'completed' },
          ],
        },
      },
    });
  });

  it('stamps the turn plan as failed on a codex terminal error, through live-plan overlays', () => {
    // 没有 done 的 codex 终态 error:这一轮的计划行等不到章。手机端要自己补失败
    // 印记,否则全勾完的失败计划按旧数据兜底退场;而印记只写内存行、不写 live
    // 缓存时,overlay 会用旧缓存把 main 随后广播的落库印记盖回去(review P1)。
    const planRow = {
      ...message('plan-row-1', 's1'),
      role: 'tool_use' as const,
      toolUseId: 'plan:turn-err',
      content: {
        toolUseId: 'plan:turn-err',
        toolName: 'update_plan',
        input: { plan: [{ step: 'Ship', status: 'completed' }] },
      },
    };
    remoteSessionStore.setMessages('s1', [planRow]);
    // 先按真实链路让 live 缓存记住这一行(update_plan 推送)。
    remoteSessionStore.applyMakerEvent('s1', {
      type: 'tool_use',
      data: {
        toolUseId: 'plan:turn-err',
        toolName: 'update_plan',
        input: { plan: [{ step: 'Ship', status: 'completed' }] },
      },
    }, 'plan-row-1');

    remoteSessionStore.applyMakerEvent('s1', {
      type: 'error',
      source: 'codex',
      data: { message: 'stream disconnected' },
    });

    expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
      content: {
        turnCompleted: false,
        input: { plan: [{ step: 'Ship', status: 'completed' }] },
      },
    });

    // main 落库前的行重新到达(无印记):overlay 用 live 缓存覆盖 content,
    // 缓存已带印记 → 不回退成"已完成的旧计划"。
    remoteSessionStore.mergeMessages('s1', [planRow]);
    expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
      content: { turnCompleted: false },
    });
  });

  it('coalesces update_plan with streaming finalization into one notification', () => {
    vi.useFakeTimers();
    const notify = vi.fn();
    try {
      remoteSessionStore.setMessages('s1', [{
        ...message('plan-row-1', 's1'),
        role: 'tool_use',
        toolUseId: 'plan:turn-1',
        content: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: { plan: [{ step: 'Inspect', status: 'pending' }] },
        },
      }]);
      pushMakerText('s1', 'assistant-1', 'before plan', false);
      vi.runOnlyPendingTimers();
      const unsubscribe = remoteSessionStore.subscribe(notify);
      try {
        remoteSessionStore.applyMakerEvent('s1', {
          type: 'tool_use',
          data: {
            toolUseId: 'plan:turn-1',
            toolName: 'update_plan',
            input: { plan: [{ step: 'Inspect', status: 'completed' }] },
          },
        }, 'plan-row-1');
      } finally {
        unsubscribe();
      }

      expect(notify).toHaveBeenCalledTimes(1);
      const rows = remoteSessionStore.getMessages('s1');
      expect(rows).toHaveLength(2);
      expect(rows.find((row) => row.clientId === 'assistant-1')).toMatchObject({ agentMeta: null });
      expect(rows.find((row) => row.id === 'plan-row-1')).toMatchObject({
        content: {
          input: { plan: [{ step: 'Inspect', status: 'completed' }] },
        },
      });
    } finally {
      vi.useRealTimers();
    }
  });

  it('falls back to the stable update_plan toolUseId when a live push has no persistId', () => {
    remoteSessionStore.setMessages('s1', [{
      ...message('plan-row-1', 's1'),
      role: 'tool_use',
      toolUseId: 'plan:turn-1',
      content: {
        toolUseId: 'plan:turn-1',
        toolName: 'update_plan',
        input: { plan: [{ step: 'Inspect', status: 'pending' }] },
      },
    }]);

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'tool_use',
        data: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: { plan: [{ step: 'Inspect', status: 'completed' }] },
        },
      },
    });

    expect(remoteSessionStore.getMessages('s1')[0].content).toMatchObject({
      input: { plan: [{ step: 'Inspect', status: 'completed' }] },
    });
  });

  it('keeps the latest live update_plan snapshot when the initial DB row arrives later', () => {
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      persistId: 'plan-row-1',
      event: {
        type: 'tool_use',
        data: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: { plan: [{ step: 'Inspect', status: 'completed' }] },
        },
      },
    });
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(0);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1',
      message: {
        ...message('plan-row-1', 's1'),
        role: 'tool_use',
        toolUseId: 'plan:turn-1',
        content: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: { plan: [{ step: 'Inspect', status: 'pending' }] },
        },
      },
    });

    expect(remoteSessionStore.getMessages('s1')[0].content).toMatchObject({
      input: { plan: [{ step: 'Inspect', status: 'completed' }] },
    });
  });

  it('keeps the honest live snapshot when done precedes the initial plan DB row', () => {
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      persistId: 'plan-row-1',
      event: {
        type: 'tool_use',
        data: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: {
            plan: [
              { step: 'Inspect', status: 'in_progress' },
              { step: 'Patch', status: 'pending' },
            ],
          },
        },
      },
    });
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'done',
        source: 'codex',
        data: {
          raw: { id: 'turn-1', status: 'completed' },
          plan: [
            { step: 'Inspect', status: 'in_progress' },
            { step: 'Patch', status: 'pending' },
          ],
        },
      },
    });

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1',
      message: {
        ...message('plan-row-1', 's1'),
        role: 'tool_use',
        toolUseId: 'plan:turn-1',
        content: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: {
            plan: [
              { step: 'Inspect', status: 'in_progress' },
              { step: 'Patch', status: 'pending' },
            ],
          },
        },
      },
    });

    // 成功收尾封的是生命周期,不改步骤事实:agent 报告什么就显示什么,
    // 晚到的 DB 行也不能把 live 快照拉回更旧的内容。
    expect(remoteSessionStore.getMessages('s1')[0].content).toMatchObject({
      input: {
        plan: [
          { step: 'Inspect', status: 'in_progress' },
          { step: 'Patch', status: 'pending' },
        ],
      },
    });
  });

  it('does not let a delayed message window revert the live plan snapshot', () => {
    const stalePlanRow = {
      ...message('plan-row-1', 's1'),
      role: 'tool_use' as const,
      toolUseId: 'plan:turn-1',
      content: {
        toolUseId: 'plan:turn-1',
        toolName: 'update_plan',
        input: { plan: [{ step: 'Inspect', status: 'in_progress' }] },
      },
    };
    remoteSessionStore.setMessages('s1', [stalePlanRow]);
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      persistId: 'plan-row-1',
      event: {
        type: 'tool_use',
        data: {
          toolUseId: 'plan:turn-1',
          toolName: 'update_plan',
          input: { plan: [{ step: 'Inspect', status: 'in_progress' }] },
        },
      },
    });
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'done',
        source: 'codex',
        data: {
          raw: { id: 'turn-1', status: 'completed' },
          plan: [{ step: 'Inspect', status: 'in_progress' }],
        },
      },
    });

    remoteSessionStore.setLatestMessageWindow('s1', [stalePlanRow]);

    // 步骤保持 agent 实际报告的状态,不因成功收尾被改写成 completed。
    expect(remoteSessionStore.getMessages('s1')[0].content).toMatchObject({
      input: { plan: [{ step: 'Inspect', status: 'in_progress' }] },
    });
  });

  it('finalizes pre-compact streaming rows and de-duplicates the same boundary replay', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setMessages('s1', [{
        ...messageAt('before-compact', 's1', '2026-01-01T00:00:01.000Z'),
        content: { text: 'before', isStreaming: true, streaming: true },
        agentMeta: { isStreaming: true, streaming: true },
      }]);
      vi.setSystemTime(new Date('2026-01-01T00:00:10.000Z'));
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'compact_boundary',
        data: { boundaryId: 'compact-1', trigger: 'auto' },
      });

      const afterBoundary = remoteSessionStore.getMessages('s1');
      expect(afterBoundary).toHaveLength(2);
      expect(afterBoundary[0]).toMatchObject({
        id: 'before-compact',
        agentMeta: { isStreaming: false, streaming: false },
        content: { text: 'before', isStreaming: false, streaming: false },
      });
      expect(afterBoundary[1]).toMatchObject({
        id: 'mobile-system-compact:compact-1',
        systemCardType: 'compact',
        systemCardData: { boundaryId: 'compact-1', trigger: 'auto' },
      });

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('after-compact', 's1', '2026-01-01T00:00:11.000Z'),
        agentMeta: { isStreaming: true },
      });
      const versionBeforeReplay = remoteSessionStore.getMessageVersion();
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'compact_boundary',
        data: { boundaryId: 'compact-1', trigger: 'auto' },
      });

      const afterReplay = remoteSessionStore.getMessages('s1');
      expect(afterReplay).toHaveLength(3);
      expect(afterReplay.find((item) => item.id === 'after-compact')?.agentMeta?.isStreaming).toBe(true);
      expect(remoteSessionStore.getMessageVersion()).toBe(versionBeforeReplay);
    } finally {
      vi.useRealTimers();
    }
  });

  it('de-duplicates a replayed id-less compact boundary before it can end newer work', () => {
    const firstData = { trigger: 'auto', preTokens: 100, postTokens: 20, durationMs: 50 };
    remoteSessionStore.setMessages('s1', [{
      ...messageAt('before-compact', 's1', '2026-01-01T00:00:01.000Z'),
      agentMeta: { isStreaming: true },
    }]);
    remoteSessionStore.applyMakerEvent('s1', { type: 'compact_boundary', data: firstData });
    remoteSessionStore.appendMessage('s1', {
      ...messageAt('after-compact', 's1', '2026-01-01T00:00:02.000Z'),
      agentMeta: { isStreaming: true },
    });
    const versionBeforeReplay = remoteSessionStore.getMessageVersion();

    // 相同数据换 key 顺序,仍应映射到同一个 canonical fallback identity。
    remoteSessionStore.applyMakerEvent('s1', {
      type: 'compact_boundary',
      data: { durationMs: 50, postTokens: 20, preTokens: 100, trigger: 'auto' },
    });

    const afterReplay = remoteSessionStore.getMessages('s1');
    expect(afterReplay.filter((item) => item.systemCardType === 'compact')).toHaveLength(1);
    expect(afterReplay.find((item) => item.id === 'after-compact')?.agentMeta?.isStreaming).toBe(true);
    expect(remoteSessionStore.getMessageVersion()).toBe(versionBeforeReplay);

    remoteSessionStore.applyMakerEvent('s1', {
      type: 'compact_boundary',
      data: { ...firstData, preTokens: 180 },
    });
    const afterDistinctBoundary = remoteSessionStore.getMessages('s1');
    expect(afterDistinctBoundary.filter((item) => item.systemCardType === 'compact')).toHaveLength(2);
    expect(afterDistinctBoundary.find((item) => item.id === 'after-compact')?.agentMeta?.isStreaming).toBe(false);
  });

  it('treats a new compact boundary as the end of the current post-compact activity segment', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setMessages('s1', [{
        ...messageAt('active-1', 's1', '2026-01-01T00:00:01.000Z'),
        agentMeta: { isStreaming: true },
      }]);
      vi.setSystemTime(new Date('2026-01-01T00:00:10.000Z'));
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'compact_boundary',
        data: { boundaryId: 'compact-1' },
      });
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('active-2', 's1', '2026-01-01T00:00:11.000Z'),
        agentMeta: { isStreaming: true },
      });

      vi.setSystemTime(new Date('2026-01-01T00:00:12.000Z'));
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'compact_boundary',
        data: { boundaryId: 'compact-2' },
      });

      const stored = remoteSessionStore.getMessages('s1');
      expect(stored.filter((item) => item.systemCardType === 'compact').map((item) => item.id)).toEqual([
        'mobile-system-compact:compact-1',
        'mobile-system-compact:compact-2',
      ]);
      expect(stored.find((item) => item.id === 'active-2')?.agentMeta?.isStreaming).toBe(false);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not finish a live turn when a background compact_boundary arrives', () => {
    remoteSessionStore.setMessages('s1', [{
      ...messageAt('live-after-idle-compact', 's1', '2026-01-01T00:00:01.000Z'),
      content: { text: '正在回答', isStreaming: true, streaming: true },
      agentMeta: { isStreaming: true, streaming: true },
    }]);
    remoteSessionStore.applyMakerEvent('s1', {
      type: 'compact_boundary',
      turnScope: 'background',
      data: { boundaryId: 'idle-compact', trigger: 'auto' },
    });

    const stored = remoteSessionStore.getMessages('s1');
    expect(stored.find((item) => item.id === 'live-after-idle-compact')).toMatchObject({
      agentMeta: { isStreaming: true, streaming: true },
      content: { text: '正在回答', isStreaming: true, streaming: true },
    });
    expect(stored.at(-1)).toMatchObject({
      id: 'mobile-system-compact:idle-compact',
      systemCardType: 'compact',
    });
  });

  it('does not flip product isRunning for background compact status', () => {
    remoteSessionStore.applyMakerEvent('s1', {
      type: 'status',
      data: { isRunning: true, status: 'Thinking…', tokenUsage: 80 },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').isRunning).toBe(true);
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);

    remoteSessionStore.applyMakerEvent('s1', {
      type: 'status',
      turnScope: 'background',
      data: { isRunning: true, status: 'Compacting context…' },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: true,
      status: 'Compacting context…',
      tokenUsage: 80,
    });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);

    remoteSessionStore.applyMakerEvent('s1', {
      type: 'status',
      turnScope: 'background',
      data: { isRunning: false, status: 'Done' },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: true,
      status: 'Compacting context…',
    });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);
  });

  it('increments message version when searchable message windows change', () => {
    const initialVersion = remoteSessionStore.getMessageVersion();
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    expect(remoteSessionStore.getMessageVersion()).toBeGreaterThan(initialVersion);

    const versionAfterSet = remoteSessionStore.getMessageVersion();
    remoteSessionStore.appendMessage('s1', message('m2', 's1'));
    expect(remoteSessionStore.getMessageVersion()).toBeGreaterThan(versionAfterSet);

    const versionAfterAppend = remoteSessionStore.getMessageVersion();
    remoteSessionStore.removeDevice('dev-1');
    expect(remoteSessionStore.getMessageVersion()).toBeGreaterThan(versionAfterAppend);
  });

  it('uses fresh list metadata after opening history without rewriting the old message mirror', () => {
    const meta = session('s1', { preview: 'old reply', _count: { messages: 1 } });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [meta, session('s2')]);
    remoteSessionStore.setMessages('s1', [{ ...message('m1', 's1'), content: 'old reply' }]);
    remoteSessionStore.markSessionMessagesSynced('s1', meta);
    const oldMirror = remoteSessionStore.getMessages('s1');
    const first = vi.fn();
    const second = vi.fn();
    const offFirst = remoteSessionStore.subscribeSessionMessagePreview('s1', first);
    const offSecond = remoteSessionStore.subscribeSessionMessagePreview('s2', second);
    try {
      // The history-view route commits fresh metadata but keeps persisted
      // history outside this legacy mirror. Returning home must use that metadata.
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', {
        ...meta, preview: 'new reply', updatedAt: '2026-01-01T00:00:02.000Z', _count: { messages: 2 },
      });
      expect(remoteSessionStore.getMessages('s1')).toBe(oldMirror);
      expect(remoteSessionStore.getSessionMessagePreview('s1')).toBe('old reply');
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('new reply');
      expect(remoteSessionStore.getSessionListMessagePreviewIndex(remoteSessionStore.getSessions()).get('s1')).toBe('new reply');
      expect(first).toHaveBeenCalled();
      expect(second).not.toHaveBeenCalled();
    } finally { offFirst(); offSecond(); }
  });

  it('notifies list previews when the same message array becomes synchronized', () => {
    const meta = session('s1', { preview: 'host preview', _count: { messages: 1 } });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [meta]);
    remoteSessionStore.setMessages('s1', [{ ...message('m1', 's1'), content: 'loaded reply' }]);
    expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('host preview');
    const changed = vi.fn();
    const off = remoteSessionStore.subscribeSessionMessagePreview('s1', changed);
    const version = remoteSessionStore.getMessageVersion();
    try {
      remoteSessionStore.markSessionMessagesSynced('s1', meta);
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('loaded reply');
      expect(remoteSessionStore.getMessageVersion()).toBeGreaterThan(version);
      expect(changed).toHaveBeenCalledTimes(1);
      remoteSessionStore.markSessionMessagesSynced('s1', meta);
      expect(changed).toHaveBeenCalledTimes(1);
      remoteSessionStore.applySessionPatch('dev-1', 's1', { preview: 'edited reply' });
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('edited reply');
      // Preview selection must not make the history-loading gate more eager.
      expect(remoteSessionStore.isSessionMessageWindowSynced('s1', meta)).toBe(true);
      remoteSessionStore.applySessionPatch('dev-1', 's1', { _count: { messages: 2 }, preview: 'next reply' });
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('next reply');
    } finally { off(); }
  });

  it('preserves actual live text over metadata but does not trust cached streaming flags', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { preview: 'host preview' })]);
      remoteSessionStore.setMessages('s1', [{ ...message('m1', 's1'), content: 'stale stream', agentMeta: { isStreaming: true } }]);
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('host preview');
      pushMakerText('s1', 'live-tail', 'live reply', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('live reply');
      pushMakerText('s1', 'live-tail', ' continues', false);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('live reply continues');
    } finally { vi.useRealTimers(); }
  });

  it.each(['done', 'status'] as const)('retains received text when %s only finalizes streaming flags', (type) => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { preview: 'old reply' })]);
      pushMakerText('s1', 'live-tail', 'new reply', false);
      vi.runOnlyPendingTimers();
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1', event: { type, data: { isRunning: false } },
      });
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('new reply');
    } finally { vi.useRealTimers(); }
  });

  it('keeps loaded previews for old hosts without preview metadata', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('hello');
  });

  it.each([false, true])('refreshes the preview after missing completion in background (final received: %s)', (finalReceived) => {
    vi.useFakeTimers();
    try {
      const meta = session('s1', { preview: 'earlier reply', _count: { messages: 1 } });
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [meta]);
      pushMakerText('s1', 'live-tail', 'partial reply', finalReceived);
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('partial reply');
      // No final/status push while suspended. The existing foreground loadHome
      // refreshes sessions; it does not download all message mirrors.
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [{
        ...meta, preview: 'completed reply', updatedAt: '2026-01-01T00:00:04.000Z', _count: { messages: 2 },
      }]);
      expect(remoteSessionStore.getSessionMessagePreview('s1')).toBe('partial reply');
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('completed reply');
      expect(remoteSessionStore.getSessionListMessagePreviewIndex(remoteSessionStore.getSessions()).get('s1')).toBe('completed reply');
    } finally { vi.useRealTimers(); }
  });

  it.each(['offline', 'deletion'] as const)('notifies when %s invalidates only message freshness', (reason) => {
    const meta = session('s1', { preview: 'host reply', _count: { messages: 1 } });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [meta]);
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    remoteSessionStore.markSessionMessagesSynced('s1', meta);
    expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('hello');
    const changed = vi.fn();
    const off = remoteSessionStore.subscribeSessionMessagePreview('s1', changed);
    try {
      if (reason === 'offline') remoteSessionStore.markDeviceOffline('dev-1');
      else remoteSessionStore.removeMessages('s1', ['not-in-mirror']);
      expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('host reply');
      expect(changed).toHaveBeenCalled();
    } finally { off(); }
  });

  it('does not resurrect an old cached preview when Host explicitly clears it', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { preview: '' })]);
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe('');
  });

  it.each([null, 'previous user', 'newer reply'])('uses the canonical preview after a draft is retracted: %s', (preview) => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { preview: 'rejected draft' })]);
    remoteSessionStore.setMessages('s1', [{ ...message('draft', 's1'), content: 'rejected draft' }]);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
      sessionId: 's1', patch: { preview },
    });
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:deleted', {
      sessionId: 's1', clientIds: ['draft'],
    });
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(0);
    expect(remoteSessionStore.getSessions().find((row) => row.id === 's1')?.preview).toBe(preview);
    expect(remoteSessionStore.getSessionListMessagePreview('s1')).toBe(preview ?? undefined);
  });

  it('tracks which session metadata the message window has been synced against', () => {
    const meta = session('s1', {
      updatedAt: '2026-01-01T00:00:01.000Z',
      _count: { messages: 2 },
    });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [meta]);

    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', meta)).toBe(false);

    remoteSessionStore.markSessionMessagesSynced('s1', meta);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', meta)).toBe(true);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', {
      ...meta,
      updatedAt: '2026-01-01T00:00:02.000Z',
    })).toBe(false);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', {
      ...meta,
      _count: { messages: 3 },
    })).toBe(false);

    remoteSessionStore.removeDevice('dev-1');
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', meta)).toBe(false);
  });

  it('appends local mobile system cards as transient messages', () => {
    const id = remoteSessionStore.appendLocalSystemCard(
      's1',
      'pwd',
      { workingDir: '/repo' },
      new Date('2026-01-01T00:00:01.000Z'),
    );

    expect(remoteSessionStore.getMessages('s1')).toEqual([
      expect.objectContaining({
        id,
        clientId: id,
        role: 'system',
        systemCardType: 'pwd',
        systemCardData: { workingDir: '/repo' },
        createdAt: '2026-01-01T00:00:01.000Z',
      }),
    ]);
  });

  it('merges reloaded history without dropping live-pushed messages', () => {
    remoteSessionStore.setMessages('s1', [
      messageAt('m1', 's1', '2026-01-01T00:00:01.000Z'),
      messageAt('m3', 's1', '2026-01-01T00:00:03.000Z'),
    ]);

    remoteSessionStore.mergeMessages('s1', [
      messageAt('m1', 's1', '2026-01-01T00:00:01.000Z'),
      messageAt('m2', 's1', '2026-01-01T00:00:02.000Z'),
    ]);

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['m1', 'm2', 'm3']);
  });

  it('replaces a stale cached window when syncing a non-overlapping latest page', () => {
    remoteSessionStore.setMessages('s1', [
      messageAt('old-1', 's1', '2026-01-01T00:00:01.000Z'),
      messageAt('old-2', 's1', '2026-01-01T00:00:02.000Z'),
    ]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
    ]);

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['latest-1', 'latest-2']);
  });

  it('keeps live-pushed tail messages when a latest-page sync resolves late', () => {
    remoteSessionStore.setMessages('s1', [
      messageAt('old-1', 's1', '2026-01-01T00:00:01.000Z'),
      messageAt('live-1', 's1', '2026-01-01T10:00:03.000Z'),
    ]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
    ]);

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'latest-1',
      'latest-2',
      'live-1',
    ]);
  });

  it('keeps a same-timestamp live tail with a larger host rowid', () => {
    const createdAt = '2026-01-01T10:00:02.000Z';
    remoteSessionStore.setMessages('s1', [
      { ...messageAt('old-1', 's1', '2026-01-01T00:00:01.000Z'), rowid: 1 },
      { ...messageAt('live-3', 's1', createdAt), rowid: 3 },
    ]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      { ...messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'), rowid: 1 },
      { ...messageAt('latest-2', 's1', createdAt), rowid: 2 },
    ]);

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'latest-1',
      'latest-2',
      'live-3',
    ]);
  });

  it('keeps a same-timestamp pending live assistant during latest-window reconciliation', () => {
    vi.useFakeTimers();
    try {
      const createdAt = '2026-01-01T10:00:02.000Z';
      pushMakerText('s1', 'live-3', 'still streaming', false);
      vi.runOnlyPendingTimers();
      remoteSessionStore.setMessages('s1', remoteSessionStore.getMessages('s1').map((item) => ({
        ...item,
        createdAt,
      })));

      remoteSessionStore.setLatestMessageWindow('s1', [
        { ...messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'), rowid: 1 },
        { ...messageAt('latest-2', 's1', createdAt), rowid: 2 },
      ]);

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.id)).toEqual([
        'latest-1',
        'live-3',
        'latest-2',
      ]);
      expect(rows.find((item) => item.clientId === 'live-3')).toMatchObject({
        content: 'still streaming',
        agentMeta: { isStreaming: true },
      });
    } finally {
      vi.useRealTimers();
    }
  });

  it('丢弃无法确认相接的更早缓存段:本页上沿之外服务端还有历史时(#1222)', () => {
    // 「有交集」不等于「连续」:交集只说明两段有共同的行,不排除更早那一段与本页之间还隔着
    // 服务端仍有、本地从未加载的行。断连期间漏收几十上百条 push 时就是这样,而漏收的量不大时
    // 两侧时间差很小 —— 时间阈值的空洞检测发现不了,窗口会静默留下孤岛。
    // moreBeyondWindow(本页满页 / 被裁行)为真时,更早的缓存段一律丢弃,窗口保持连续区间。
    // 用冷开缓存入口种入:它刻意不登记「已验证连续」,正是"来源不明"的那类段。
    remoteSessionStore.hydrateMessagesIfEmpty('s1', [
      messageAt('cached-old', 's1', '2026-01-01T00:00:01.000Z'),
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
    ]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'latest-1',
      'latest-2',
    ]);
  });

  it('断流期间漏收超过一页:补读到已验证上界后,整段旧历史保留', () => {
    remoteSessionStore.setMessages('s1', [
      messageAt('old-1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('old-2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.noteLiveStreamInterrupted();
    const latestPage = [
      messageAt('gap-3', 's1', '2026-01-01T09:00:05.000Z'),
      messageAt('gap-4', 's1', '2026-01-01T09:00:06.000Z'),
    ];
    // 本页最旧行晚于已验证上界:直接提交会丢掉 old-*,所以需要先补读。
    expect(remoteSessionStore.latestMessageWindowNeedsBridge('s1', latestPage, true)).toBe(true);
    // 本页不满页(历史到头)、或已验证区间不存在时,补读不改变结果。
    expect(remoteSessionStore.latestMessageWindowNeedsBridge('s1', latestPage, false)).toBe(false);
    expect(remoteSessionStore.latestMessageWindowNeedsBridge('s2', latestPage, true)).toBe(false);

    const bridged = [
      messageAt('old-2', 's1', '2026-01-01T09:00:02.000Z'),
      messageAt('gap-1', 's1', '2026-01-01T09:00:03.000Z'),
      messageAt('gap-2', 's1', '2026-01-01T09:00:04.000Z'),
      ...latestPage,
    ];
    expect(remoteSessionStore.latestMessageWindowNeedsBridge('s1', bridged, true)).toBe(false);
    remoteSessionStore.setLatestMessageWindow('s1', bridged, { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'old-1',
      'old-2',
      'gap-1',
      'gap-2',
      'gap-3',
      'gap-4',
    ]);
  });

  it('漏推提示后只信区间下界:补读覆盖整段才相接,旧历史不被整段丢弃', () => {
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.setMessages('s1', [
      messageAt('old-1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('old-2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    // 订阅内的 push 把上界推过了一条被漏推的 gap-3。
    remoteSessionStore.appendMessage('s1', messageAt('live-4', 's1', '2026-01-01T09:00:04.000Z'));
    remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, { sessionId: 's1', resyncRequired: true });

    const latestPage = [
      messageAt('live-4', 's1', '2026-01-01T09:00:04.000Z'),
      messageAt('new-5', 's1', '2026-01-01T09:00:05.000Z'),
    ];
    // 上界已不可信:即使最新页与 live-4 有交集,也要补读到区间下界。
    expect(remoteSessionStore.latestMessageWindowNeedsBridge('s1', latestPage, true)).toBe(true);
    const bridged = [
      messageAt('old-1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('old-2', 's1', '2026-01-01T09:00:02.000Z'),
      messageAt('gap-3', 's1', '2026-01-01T09:00:03.000Z'),
      ...latestPage,
    ];
    expect(remoteSessionStore.latestMessageWindowNeedsBridge('s1', bridged, true)).toBe(false);
    remoteSessionStore.setLatestMessageWindow('s1', bridged, { moreBeyondWindow: true });
    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'old-1', 'old-2', 'gap-3', 'live-4', 'new-5',
    ]);
  });

  it('漏推提示后没补读到区间下界时,仍按未验证处置', () => {
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.setMessages('s1', [
      messageAt('old-1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('old-2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.appendMessage('s1', messageAt('live-4', 's1', '2026-01-01T09:00:04.000Z'));
    remoteSessionStore.applyRemotePush('dev-1', SESSION_SYNC_CHANNEL, { sessionId: 's1', resyncRequired: true });
    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('gap-3', 's1', '2026-01-01T09:00:03.000Z'),
      messageAt('live-4', 's1', '2026-01-01T09:00:04.000Z'),
    ], { moreBeyondWindow: true });
    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['gap-3', 'live-4']);
  });

  it('来源不明的缓存段没有已验证区间可接,不触发补读', () => {
    remoteSessionStore.hydrateMessagesIfEmpty('s1', [
      messageAt('cached-old', 's1', '2026-01-01T00:00:01.000Z'),
    ]);
    expect(remoteSessionStore.latestMessageWindowNeedsBridge('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
    ], true)).toBe(false);
  });

  it('rejects late paging whose anchor was removed by a concurrent latest-window refresh', () => {
    const old = messageAt('old-anchor', 's1', '2026-01-01T09:00:00.000Z');
    const latest = messageAt('new-anchor', 's1', '2026-01-01T10:00:00.000Z');
    remoteSessionStore.setMessages('s1', [old]);
    remoteSessionStore.setLatestMessageWindow('s1', [latest], { moreBeyondWindow: true });
    expect(remoteSessionStore.mergeEarlierMessages('s1', [
      messageAt('late-old-page', 's1', '2026-01-01T08:00:00.000Z'),
    ], { before: old.id })).toBe(false);
    expect(remoteSessionStore.mergeEarlierMessages('s1', [], { before: old.id })).toBe(false);
    expect(remoteSessionStore.getMessages('s1').map(row => row.id)).toEqual(['new-anchor']);
    // Paging again from the surviving anchor fills the actual gap and may extend coverage.
    expect(remoteSessionStore.mergeEarlierMessages('s1', [old], { before: latest.id })).toBe(true);
    remoteSessionStore.setLatestMessageWindow('s1', [latest], { moreBeyondWindow: true });
    expect(remoteSessionStore.getMessages('s1').map(row => row.id)).toEqual(['old-anchor', 'new-anchor']);
  });

  it('does not join a late disjoint latest response to a newer verified window', () => {
    const latest = messageAt('new-anchor', 's1', '2026-01-01T10:00:00.000Z');
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.setLatestMessageWindow('s1', [latest], { moreBeyondWindow: true });
    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('stale-latest', 's1', '2026-01-01T08:00:00.000Z'),
    ], { moreBeyondWindow: true });
    const tail = messageAt('live-tail', 's1', '2026-01-01T11:00:00.000Z');
    remoteSessionStore.appendMessage('s1', tail);
    remoteSessionStore.setLatestMessageWindow('s1', [tail], { moreBeyondWindow: true });
    expect(remoteSessionStore.getMessages('s1').map(row => row.id)).toEqual(['new-anchor', 'live-tail']);
  });

  it('does not certify a gap when paging before an unverified older island', () => {
    const latest = messageAt('new-anchor', 's1', '2026-01-01T10:00:00.000Z');
    const island = messageAt('island', 's1', '2026-01-01T09:00:00.000Z');
    remoteSessionStore.setMessages('s1', [latest]);
    remoteSessionStore.mergeMessages('s1', [island]);
    remoteSessionStore.mergeEarlierMessages('s1', [
      messageAt('old-page', 's1', '2026-01-01T08:00:00.000Z'),
    ], { before: island.id });
    remoteSessionStore.setLatestMessageWindow('s1', [latest], { moreBeyondWindow: true });
    expect(remoteSessionStore.getMessages('s1').map(row => row.id)).toEqual(['new-anchor']);
  });

  it('用户「加载更早」翻出来的历史在满页重连时保留(已验证连续)', () => {
    // 回归(#1210 review):只凭"最新页满页"就清空更早的行,会把用户一路翻出来的历史与滚动锚点
    // 一起丢掉,而且补齐也不会拉回(裁完窗口里已没有内部跳变可发现)。「加载更早」是沿 before 从
    // 窗口最旧端连续取的,登记进已验证连续区间后必须保住。
    remoteSessionStore.setMessages('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
    ]);
    remoteSessionStore.mergeEarlierMessages('s1', [
      messageAt('earlier-1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('earlier-2', 's1', '2026-01-01T09:30:01.000Z'),
    ]);

    // 断连重连:最新快照恰好满页 → moreBeyondWindow=true,但这些行在已验证区间内。
    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'earlier-1',
      'earlier-2',
      'latest-1',
      'latest-2',
    ]);
  });

  it('断流后先到的一条 push 不能让旧段继续被连续性结论背书(#1210 review)', () => {
    // 只记「下界」时,它断言的是"从下界到**窗口最新端**连续"——而窗口最新端会被断流期间漏收的行
    // 悄悄作废:旧窗 09:00 那两行,掉线期间服务端产出一大段(本端全漏),重连后先到一条尾部 push,
    // 于是"有交集"仅靠这条 push 成立,旧下界却还在背书,窗口重新变成孤岛(而这些行若在半小时内
    // 产生,自动探测也发现不了)。上界显式记下来后,"接不上"就是一次比较。
    remoteSessionStore.setMessages('s1', [
      messageAt('old-1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('old-2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.noteLiveStreamInterrupted();
    remoteSessionStore.appendMessage('s1', messageAt('resumed-tail', 's1', '2026-01-01T10:00:09.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('gap-tail', 's1', '2026-01-01T10:00:08.000Z'),
      messageAt('resumed-tail', 's1', '2026-01-01T10:00:09.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'gap-tail',
      'resumed-tail',
    ]);
  });

  it('订阅未断时实时 push 推进上界,涨过一页后满页刷新仍保留更早的已验证历史', () => {
    // 上一条的反面:订阅已 ACK、又没断流时,推送是顺序且完整的,窗口从下界一直连续到最新那条
    // push。这时最新页只回尾段(会话已涨过一页)不代表更早的行来源不明——收紧过头会在活跃会话里
    // 反复清空历史。
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.setMessages('s1', [
      messageAt('a1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('a2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.appendMessage('s1', messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'));
    remoteSessionStore.appendMessage('s1', messageAt('live-2', 's1', '2026-01-01T09:30:01.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'),
      messageAt('live-2', 's1', '2026-01-01T09:30:01.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'a1',
      'a2',
      'live-1',
      'live-2',
    ]);
  });

  it('断流只作用于被中断的会话(退后台释放的是各自的 session 订阅)', () => {
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.noteLiveStreamAcked('s2');
    remoteSessionStore.setMessages('s1', [messageAt('a1', 's1', '2026-01-01T09:00:01.000Z')]);
    // 另一个会话的订阅被释放,不能顺带作废 s1 的上界。
    remoteSessionStore.noteLiveStreamInterrupted('s2');
    remoteSessionStore.appendMessage('s1', messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['a1', 'live-1']);
  });

  it('最新页与缓存逐行相同时也登记连续性(#1210 review)', () => {
    // 冷开缓存恰好等于服务端最新页是常态。相等早退发生在记账之前时,这次权威响应白来:之后
    // 会话靠 push 涨过一页、再遇一次满页重连刷新,这些**已被权威页确认过**的行会被当成来源不明
    // 全部丢弃,用户当前历史与滚动位置随之消失。
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.hydrateMessagesIfEmpty('s1', [
      messageAt('c1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('c2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('c1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('c2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.appendMessage('s1', messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['c1', 'c2', 'live-1']);
  });

  it('整窗替换与缓存逐行相同时也登记连续性', () => {
    // setMessages 的相等早退同理:两条路径的记账必须一致,否则走哪条入口决定历史保不保得住。
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.hydrateMessagesIfEmpty('s1', [
      messageAt('c1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('c2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.setMessages('s1', [
      messageAt('c1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('c2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.appendMessage('s1', messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['c1', 'c2', 'live-1']);
  });

  it('订阅 ACK 之前落库的权威页,尾部不算可信(#1210 review)', () => {
    // 屏幕侧的 openAndSubscribe / startFocusedTopicSubscription 都是 `void subscribe(...)`,不等
    // ACK 就拉页(订阅只管之后的推送,不该挡数据读),所以"页比订阅先到"是常态。这个空窗里被控端
    // 写下的行既不在这一页、也不会被推过来;若这时仍把尾部标成可信,之后一条 push 就会把上界抬过
    // 那几行,而等尾部涨过一页后最新页已不含它们,事实自检也发现不了 —— 孤岛就此固化下来。
    remoteSessionStore.setMessages('s1', [
      messageAt('a1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('a2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    // 空窗里服务端写了 m81/m82(本端全没收到);订阅生效后才收到更新的这一条。
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.appendMessage('s1', messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'),
    ], { moreBeyondWindow: true });

    // 上界仍是 09:00:02,接不上 09:30 的页 → a1/a2 丢弃,窗口保持连续。
    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['live-1']);
  });

  it.each([
    ['socket 掉线(不带 sessionId)', undefined],
    ['退后台释放 / 离开会话(按 sessionId)', 's1'],
  ])('断流时 ACK 记录一并作废:%s', (_label, interruptedSessionId) => {
    // 断流清掉信任位只管**既有**区间;若 ACK 记录还留着,断线后才落库的在途页(请求在断线前发出、
    // 响应迟到)会重新把尾部标成可信,重连后先到的 push 又把上界抬过漏收的行 —— 绕一圈回到同一个
    // 孤岛。生效与失效必须成对。
    remoteSessionStore.noteLiveStreamAcked('s1');
    remoteSessionStore.setMessages('s1', [
      messageAt('a1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('a2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.noteLiveStreamInterrupted(interruptedSessionId);
    // 断线后才落库的在途页(内容与窗口相同,走相等早退,但记账照做)。
    remoteSessionStore.setMessages('s1', [
      messageAt('a1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('a2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.appendMessage('s1', messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('live-1', 's1', '2026-01-01T09:30:00.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['live-1']);
  });

  it('最新页在已验证区间内带来窗口没有的行时,旧结论作废', () => {
    // 覆盖区间是对服务端事实的断言,可以被更新的权威页推翻(桌面侧改写历史、迟到落库)。
    // 区间内出现窗口没有的行 → 断言本来就是假的,当次按"未知"处置,不能继续背书更早的段。
    remoteSessionStore.setMessages('s1', [
      messageAt('a1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('a3', 's1', '2026-01-01T09:00:03.000Z'),
    ]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('a2', 's1', '2026-01-01T09:00:02.000Z'),
      messageAt('a3', 's1', '2026-01-01T09:00:03.000Z'),
      messageAt('a4', 's1', '2026-01-01T09:00:04.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['a2', 'a3', 'a4']);
  });

  it('断流后到达的 push 不推进上界(本页被裁到只剩那条 push 时,信任位是唯一守卫)', () => {
    // 上界只有在"实时推送链路自它建立以来没断过"时才能被 push 续推:断流期间漏收的行与新到的
    // push 之间可能隔着任意多行,续推等于凭空声明覆盖了它们。
    // 这里刻意把最新页裁到只剩那条 push(device-link payload 超限时的常态,moreBeyondWindow 仍为
    // 真):本页没带来任何"区间内缺失"的行,事实自检无从发现 —— 信任位是这一档唯一的守卫。
    remoteSessionStore.setMessages('s1', [
      messageAt('a1', 's1', '2026-01-01T09:00:01.000Z'),
      messageAt('a2', 's1', '2026-01-01T09:00:02.000Z'),
    ]);
    remoteSessionStore.noteLiveStreamInterrupted('s1');
    // 断线期间服务端还产出了 09:10 / 09:20 等行,本端全漏;重连后先到的是更新的这一条。
    remoteSessionStore.appendMessage('s1', messageAt('resumed', 's1', '2026-01-01T09:30:00.000Z'));

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('resumed', 's1', '2026-01-01T09:30:00.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['resumed']);
  });

  it('rewind / clear 之后连续性结论失效,来源不明的旧段照旧丢弃', () => {
    // 连续性是对"窗口"的结论:rewind 可能删掉中间的行,不能让旧结论继续背书。
    remoteSessionStore.setMessages('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
    ]);
    remoteSessionStore.mergeEarlierMessages('s1', [
      messageAt('earlier-1', 's1', '2026-01-01T09:00:01.000Z'),
    ]);
    // 被控端删除某行 → 窗口连续性结论重置。
    remoteSessionStore.removeMessages('s1', ['latest-1']);

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
      messageAt('latest-3', 's1', '2026-01-01T10:00:03.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'latest-2',
      'latest-3',
    ]);
  });

  it('丢弃更早缓存段时仍保留比本页更新的实时 push 行与本地系统卡', () => {
    // 收紧的只是"更早那一段"这一条判据:尾部的 live push 与没有服务端对应行的本地卡不受影响。
    remoteSessionStore.hydrateMessagesIfEmpty('s1', [
      messageAt('cached-old', 's1', '2026-01-01T00:00:01.000Z'),
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('live-tail', 's1', '2026-01-01T10:00:09.000Z'),
      messageAt('mobile-system-pwd-1', 's1', '2026-01-01T00:00:05.000Z'),
    ]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
    ], { moreBeyondWindow: true });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'mobile-system-pwd-1',
      'latest-1',
      'latest-2',
      'live-tail',
    ]);
  });

  it('preserves loaded older pages when the refreshed latest page overlaps the current window', () => {
    // 未给 moreBeyondWindow(或为 false)= 本页已到会话起点,不存在中间缺口,旧段可信照旧保留。
    remoteSessionStore.setMessages('s1', [
      messageAt('older-1', 's1', '2026-01-01T00:00:01.000Z'),
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
    ]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('latest-1', 's1', '2026-01-01T10:00:01.000Z'),
      messageAt('latest-2', 's1', '2026-01-01T10:00:02.000Z'),
      messageAt('latest-3', 's1', '2026-01-01T10:00:03.000Z'),
    ]);

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual([
      'older-1',
      'latest-1',
      'latest-2',
      'latest-3',
    ]);
  });

  it('keeps in-window complete rows when a payload-limited latest sync sends truncated rows', () => {
    // 会话重开路径走 setLatestMessageWindow:实时 push 已拿到完整内容的行,
    // 不能被帧超限刷新(agentMeta.remoteContentTruncated)的占位串覆盖——
    // 窗口内重叠行必须进入截断保护的比较基准(existingByKey)。
    const full = {
      ...messageAt('m1', 's1', '2026-01-01T10:00:01.000Z'),
      content: '完整的长内容',
    };
    const truncated = {
      ...messageAt('m1', 's1', '2026-01-01T10:00:01.500Z'),
      content: '[remote content truncated: payload too large]',
      agentMeta: { remoteContentTruncated: true, agentTaskStatus: 'failed' as const },
    };
    remoteSessionStore.setMessages('s1', [full]);

    remoteSessionStore.setLatestMessageWindow('s1', [
      truncated,
      messageAt('m2', 's1', '2026-01-01T10:00:02.000Z'),
    ]);

    const rows = remoteSessionStore.getMessages('s1');
    expect(rows.map((item) => item.id)).toEqual(['m1', 'm2']);
    expect(rows[0].content).toBe('完整的长内容');
    expect(rows[0].createdAt).toBe('2026-01-01T10:00:01.500Z');
    expect(rows[0].agentMeta?.agentTaskStatus).toBe('failed');
    expect(rows[0].agentMeta?.remoteContentTruncated).not.toBe(true);
  });

  it('does not emit when a reseed returns the same session list or message window', () => {
    const notify = vi.fn();
    const unsubscribe = remoteSessionStore.subscribe(notify);
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
      notify.mockClear();

      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
      expect(notify).not.toHaveBeenCalled();

      remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
      const versionAfterSet = remoteSessionStore.getMessageVersion();
      notify.mockClear();

      remoteSessionStore.mergeMessages('s1', [message('m1', 's1')]);
      expect(notify).not.toHaveBeenCalled();
      expect(remoteSessionStore.getMessageVersion()).toBe(versionAfterSet);
    } finally {
      unsubscribe();
    }
  });

  it('does not emit when pending interactions or input projection are unchanged', () => {
    const notify = vi.fn();
    const unchangedPending = [pending('permission', 'permission-1')];
    const unchangedProjection = projection('s1');
    const unsubscribe = remoteSessionStore.subscribe(notify);
    try {
      remoteSessionStore.setPendingInteractions('s1', unchangedPending);
      remoteSessionStore.setInputProjection('s1', unchangedProjection);
      notify.mockClear();

      remoteSessionStore.setPendingInteractions('s1', unchangedPending);
      remoteSessionStore.setInputProjection('s1', unchangedProjection);

      expect(notify).not.toHaveBeenCalled();
    } finally {
      unsubscribe();
    }
  });

  it('tracks session running state from active snapshots without clearing other devices', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.setDeviceSessions('dev-2', 'Mac mini', [session('s2')]);

    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{ sessionId: 's1', isTurnRunning: true }]);
    remoteSessionStore.setActiveSessionSnapshots('dev-2', [{ sessionId: 's2', isTurnRunning: true }]);
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    expect(remoteSessionStore.isSessionRunning('s2')).toBe(true);

    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{ sessionId: 's1', isTurnRunning: false }]);
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(false);
    expect(remoteSessionStore.isSessionRunning('s2')).toBe(true);
  });

  it('reconciles a missed error-clear push from an authoritative active activity snapshot', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.applySessionActivity('dev-1', {
      sessionId: 's1', phase: 'error', attention: true,
    });
    expect(remoteSessionStore.getSessionLiveActivity('s1')).toMatchObject({ phase: 'error', attention: true });

    // 旧主机没有活动字段,运行标记本身不足以判定旧错误已读。
    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{ sessionId: 's1', isTurnRunning: true }]);
    expect(remoteSessionStore.getSessionLiveActivity('s1')).toMatchObject({ phase: 'error', attention: true });

    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{
      sessionId: 's1', isTurnRunning: true,
      activityPhase: 'running', activityAttention: false,
    }]);
    expect(remoteSessionStore.getSessionLiveActivity('s1')).toMatchObject({ phase: 'running', attention: false });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);

    remoteSessionStore.applySessionActivity('dev-1', {
      sessionId: 's1', phase: 'running', attention: false, workingPhase: 'testing',
    });
    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{
      sessionId: 's1', isTurnRunning: true,
      activityPhase: 'running', activityAttention: false,
    }]);
    expect(remoteSessionStore.getSessionLiveActivity('s1')?.workingPhase).toBe('testing');
  });

  it('does not let a delayed activity snapshot erase a newer error push', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const epoch = remoteSessionStore.captureActiveSessionSnapshotEpoch();
    remoteSessionStore.applySessionActivity('dev-1', {
      sessionId: 's1', phase: 'error', attention: true,
    });
    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{
      sessionId: 's1', isTurnRunning: false,
      activityPhase: 'completed', activityAttention: false,
    }], epoch);
    expect(remoteSessionStore.getSessionLiveActivity('s1')).toMatchObject({ phase: 'error', attention: true });
  });

  it('clears a cached error when the host explicitly reports no remaining activity', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.applySessionActivity('dev-1', {
      sessionId: 's1', phase: 'error', attention: true,
    });
    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{
      sessionId: 's1', isTurnRunning: false,
      activityPhase: 'idle', activityAttention: false,
    }]);
    expect(remoteSessionStore.getSessionLiveActivity('s1')).toBeNull();
  });

  it('does not treat an absent active-session row as an idle assertion', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
      pushMakerStatus('s1', { isRunning: true });
      pushMakerText('s1', undefined, 'still generating', false);
      vi.runOnlyPendingTimers();

      // This response may have started before the turn and completed after the
      // live push. Absence must not finalize the current streaming row.
      remoteSessionStore.setActiveSessionSnapshots('dev-1', []);

      expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        content: 'still generating',
        agentMeta: { isStreaming: true },
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  // Unit regression: after re-link, the physical shard can remain old while
  // authoritative snapshots arrive from its canonical host. Preserve both paths.
  it.each(['dev-1', 'new-dev-1'])('clears absent runtime state from complete snapshots on %s', (host) => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    if (host !== 'dev-1') remoteSessionStore.setDeviceIdentity([{ deviceId: host, name: 'Mac' }]);
    remoteSessionStore.setDeviceSessions('dev-2', 'Mac mini', [session('s2')]);
    remoteSessionStore.applySessionActivity(host, {
      sessionId: 's1', phase: 'error', attention: true,
    });
    remoteSessionStore.applySessionActivity('dev-2', {
      sessionId: 's2', phase: 'error', attention: true,
    });
    remoteSessionStore.setSessionRunning('s1', true);

    // A legacy host also returns an empty array, without claiming completeness.
    remoteSessionStore.setActiveSessionSnapshots(host, []);
    expect(remoteSessionStore.getSessionLiveActivity('s1')).toMatchObject({ phase: 'error', attention: true });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);

    remoteSessionStore.setActiveSessionSnapshots(host, {
      format: 'active-sessions-v2', sessions: [],
    });

    expect(remoteSessionStore.getSessionLiveActivity('s1')).toBeNull();
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(false);
    expect(remoteSessionStore.getSessionLiveActivity('s2')).toMatchObject({ phase: 'error', attention: true });
  });

  it.each(['dev-1', 'new-dev-1'])('preserves newer activity across delayed complete snapshots on %s', (host) => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    if (host !== 'dev-1') remoteSessionStore.setDeviceIdentity([{ deviceId: host, name: 'Mac' }]);
    const epoch = remoteSessionStore.captureActiveSessionSnapshotEpoch();
    remoteSessionStore.applySessionActivity(host, {
      sessionId: 's1', phase: 'error', attention: true,
    });

    remoteSessionStore.setActiveSessionSnapshots(host, {
      format: 'active-sessions-v2', sessions: [],
    }, epoch);

    expect(remoteSessionStore.getSessionLiveActivity('s1')).toMatchObject({ phase: 'error', attention: true });
  });

  it('clears stale reconnect progress from an active snapshot without erasing newer retry events', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Reconnecting... 1/5', willRetry: true } },
    });

    const currentSnapshotEpoch = remoteSessionStore.captureActiveSessionSnapshotEpoch();
    remoteSessionStore.setActiveSessionSnapshots(
      'dev-1',
      [{ sessionId: 's1', isTurnRunning: true }],
      currentSnapshotEpoch,
    );
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toBeNull();

    const staleSnapshotEpoch = remoteSessionStore.captureActiveSessionSnapshotEpoch();
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Reconnecting... 2/5', willRetry: true } },
    });
    remoteSessionStore.setActiveSessionSnapshots(
      'dev-1',
      [{ sessionId: 's1', isTurnRunning: true }],
      staleSnapshotEpoch,
    );
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toEqual({
      attempt: 2,
      maxAttempts: 5,
    });
  });

  it('projects the overload auto-retry marker as an overload attempt', () => {
    // 上游过载退避与传输层重连共用同一个 attempt 字段, 但必须能分辨: 不认
    // `(auto-retry N/M)` 的话, 整个退避窗口(最长约 30s)手机端只显示笼统的「思考中」
    // (review #844 codex P1)。
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'error',
        data: {
          message:
            'Selected model is at capacity. Please try a different model. (auto-retry 2/4)',
          isTerminal: false,
          willRetry: true,
        },
      },
    });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toEqual({
      attempt: 2,
      maxAttempts: 4,
      kind: 'overload',
    });

    // Claude 侧的 529 走同一后缀。
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'error',
        data: {
          message: 'SDK API request failed: overloaded (HTTP 529) (auto-retry 3/10)',
          isTerminal: false,
          willRetry: true,
        },
      },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toEqual({
      attempt: 3,
      maxAttempts: 10,
      kind: 'overload',
    });

    // 重连仍是老形状(不带 kind), 既有投影与用例不受影响。
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Reconnecting... 1/5', willRetry: true } },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toEqual({
      attempt: 1,
      maxAttempts: 5,
    });
  });

  it('projects terminal 429 retries as rate-limit attempts only with their reason', () => {
    const message =
      'exceeded retry limit, last status: 429 Too Many Requests (rate-limit-retry 1/2)';
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'error',
        data: {
          message,
          reason: 'terminal-rate-limit-retry',
          isTerminal: false,
          willRetry: true,
        },
      },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toEqual({
      attempt: 1,
      maxAttempts: 2,
      kind: 'rate-limit',
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'error',
        data: { message, isTerminal: false, willRetry: true },
      },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toBeNull();

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'error',
        data: {
          message: 'provider failed (auto-retry 1/2)',
          reason: 'terminal-rate-limit-retry',
          isTerminal: false,
          willRetry: true,
        },
      },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toBeNull();
  });

  it('tracks session running state from maker event push boundaries', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-01-01T00:00:10.000Z'));
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'status', data: { isRunning: true } },
    });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: true,
      startedAt: Date.parse('2026-01-01T00:00:10.000Z'),
      tokenUsage: 0,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Reconnecting... 1/5', willRetry: true } },
    });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toEqual({
      attempt: 1,
      maxAttempts: 5,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: {
        type: 'error',
        data: {
          message: 'Reconnecting... 2/5 (stream disconnected before completion)',
          isTerminal: false,
          willRetry: true,
        },
      },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toEqual({
      attempt: 2,
      maxAttempts: 5,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Waiting before retry', willRetry: true } },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toBeNull();

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Reconnecting... 3/5', willRetry: true } },
    });
    pushMakerText('s1', 'persist-reconnected', 'resumed', false);
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toBeNull();

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Reconnecting... 4/5', willRetry: true } },
    });
    remoteSessionStore.applyInteractionRequest('s1', pending('permission', 'permission-1'));
    expect(remoteSessionStore.getSessionRunStatus('s1').reconnectAttempt).toBeNull();

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'error', data: { message: 'Reconnecting... 5/5', willRetry: true } },
    });

    vi.setSystemTime(new Date('2026-01-01T00:00:20.000Z'));
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'status', data: { isRunning: true, status: 'Thinking', tokenUsage: 1200 } },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: true,
      startedAt: Date.parse('2026-01-01T00:00:10.000Z'),
      reconnectAttempt: null,
      status: 'Thinking',
      tokenUsage: 1200,
    });

    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'status', data: { isRunning: false } },
    });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(false);
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: false,
      startedAt: null,
      tokenUsage: 1200,
    });

    vi.setSystemTime(new Date('2026-01-01T00:00:30.000Z'));
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'status', data: { isRunning: true } },
    });
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'done', data: {} },
    });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(false);
    vi.useRealTimers();
  });

  it('keeps live generation fields from the first post-reconnect status', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    pushMakerStatus('s1', {
      isRunning: true,
      outputTokens: 12,
      generationDurationMs: 400,
      generationActive: true,
      generationReliable: true,
    });
    remoteSessionStore.markDeviceOffline('dev-1');
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);

    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{ sessionId: 's1', isTurnRunning: true }]);
    expect(remoteSessionStore.getSessionRunStatus('s1').isRunning).toBe(true);
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);

    pushMakerStatus('s1', {
      isRunning: true,
      status: 'Generating...',
      tokenUsage: 235,
      outputTokens: 40,
      generationDurationMs: 800,
      generationActive: true,
      generationReliable: true,
    });
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: true,
      tokenUsage: 235,
      outputTokens: 40,
      generationDurationMs: 800,
      generationActive: true,
      generationReliable: true,
    });
  });

  it('markDevicesOffline sweeps a wave with a single notification', () => {
    const deviceIds = ['dev-a', 'dev-b', 'dev-c'];
    deviceIds.forEach((id, i) => {
      remoteSessionStore.setDeviceSessions(id, `Mac-${i}`, [session(`s-${id}`)]);
    });
    for (const id of deviceIds) {
      pushMakerStatus(`s-${id}`, {
        isRunning: true,
        outputTokens: 12,
        generationDurationMs: 400,
        generationActive: true,
        generationReliable: true,
      }, id);
      expect(remoteSessionStore.isSessionMakerTurnRunning(`s-${id}`)).toBe(true);
    }
    const notify = vi.fn();
    const unsubscribe = remoteSessionStore.subscribe(notify);

    remoteSessionStore.markDevicesOffline(deviceIds);

    // 整波只 notify 一轮;逐台 markDeviceOffline 会 notify N 轮,叠加 schedule
    // store 的逐台失效后在设备多时击穿 React 嵌套上限(2026-09-10)。
    expect(notify).toHaveBeenCalledTimes(1);
    for (const id of deviceIds) {
      expect(remoteSessionStore.isSessionMakerTurnRunning(`s-${id}`)).toBe(false);
    }

    // 清理已生效:同批重复离线无变化、静默。
    remoteSessionStore.markDevicesOffline(deviceIds);
    expect(notify).toHaveBeenCalledTimes(1);
    unsubscribe();
  });

  it('still zeros leftover live metrics when a new turn starts without them', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    pushMakerStatus('s1', {
      isRunning: true,
      outputTokens: 99,
      generationDurationMs: 5_000,
      generationActive: true,
      generationReliable: false,
    });
    pushMakerStatus('s1', { isRunning: false });
    pushMakerStatus('s1', { isRunning: true, status: 'Thinking' });
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: true,
      outputTokens: 0,
      generationDurationMs: 0,
      generationActive: false,
      generationReliable: true,
    });
  });

  it('clears leftover tok/s when activity restores wide running before the next maker status', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    pushMakerStatus('s1', {
      isRunning: true,
      outputTokens: 40,
      generationDurationMs: 800,
      generationActive: true,
      generationReliable: true,
    });
    pushMakerStatus('s1', { isRunning: false });
    expect(remoteSessionStore.getSessionRunStatus('s1').outputTokens).toBe(40);

    remoteSessionStore.applySessionActivity('dev-1', { sessionId: 's1', phase: 'running' });
    expect(remoteSessionStore.getSessionRunStatus('s1')).toMatchObject({
      isRunning: true,
      outputTokens: 0,
      generationDurationMs: 0,
      generationActive: false,
      generationReliable: true,
    });
  });

  it('carries the host tok/s across statuses without it and clears it for the next turn', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const tokenSpeed = { rate: 42, average: 40, tokens: 120, elapsedMs: 3_000, streaming: true };
    pushMakerStatus('s1', { isRunning: true, status: 'Generating...', tokenSpeed });
    pushMakerStatus('s1', { isRunning: true, status: 'Running tool' });
    expect(remoteSessionStore.getSessionRunStatus('s1').tokenSpeed).toEqual(tokenSpeed);
    // The host withdrew the speed (suspended mid-generation): the stale reading goes too.
    pushMakerStatus('s1', { isRunning: true, generationReliable: false });
    expect(remoteSessionStore.getSessionRunStatus('s1').tokenSpeed).toBeUndefined();
    pushMakerStatus('s1', { isRunning: false });
    remoteSessionStore.applySessionActivity('dev-1', { sessionId: 's1', phase: 'running' });
    expect(remoteSessionStore.getSessionRunStatus('s1').tokenSpeed).toBeUndefined();
    pushMakerStatus('s1', { isRunning: false });
    pushMakerStatus('s1', { isRunning: true, status: 'Thinking' });
    expect(remoteSessionStore.getSessionRunStatus('s1').tokenSpeed).toBeUndefined();
  });

  it('clears leftover task updates on a real turn start, scoped to that session', () => {
    // Turn 1 on s1 spawns a sub-agent, then the turn ends — the live update lingers.
    pushMakerStatus('s1', { isRunning: true });
    pushMakerTaskUpdate('s1', 'task-old');
    pushMakerTaskUpdate('s2', 'task-other-session');
    pushMakerStatus('s1', { isRunning: false });
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(1);

    // A NEW turn on s1 must not resurface turn-1's sub-agent cards; s2 is untouched.
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(0);
    expect(remoteSessionStore.getSessionTaskUpdates('s2').size).toBe(1);

    // Updates pushed within the new turn are kept across same-turn status refreshes.
    pushMakerTaskUpdate('s1', 'task-new');
    pushMakerStatus('s1', { isRunning: true, status: 'Thinking', tokenUsage: 100 });
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(1);
    expect([...remoteSessionStore.getSessionTaskUpdates('s1').values()][0]?.taskId).toBe('task-new');
  });

  it('preserves Pi as the source of agent task updates', () => {
    pushMakerTaskUpdate('s1', 'pi-task', { source: 'pi' });
    expect([...remoteSessionStore.getSessionTaskUpdates('s1').values()][0]?.provider).toBe('pi');
  });

  it('sweeps everything on a side-task start too, recalling the worker on its next update', () => {
    // Leftovers from a finished turn: a claude sub-agent, a completed codex worker, and the
    // still-running collab worker (the side task's own subject).
    pushMakerStatus('s1', { isRunning: true });
    pushMakerTaskUpdate('s1', 'claude-leftover', { source: 'claude-code' });
    pushMakerTaskUpdate('s1', 'codex-done-leftover', { source: 'codex', status: 'completed' });
    pushMakerTaskUpdate('s1', 'collab-worker', { source: 'codex' });
    pushMakerStatus('s1', { isRunning: false });

    // A side-task start flips the maker turn boundary true, which OPENS the orphan render
    // gate — so it must sweep like a real turn start (skipTurnReset is no exemption), or
    // the leftovers would replay immediately. Pre-existing entries all leave the visible
    // map (the running worker is parked, not dropped).
    pushMakerStatus('s1', { isRunning: true, skipTurnReset: true });
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(0);

    // The live side-task worker pushes its next update — post-boundary evidence — and is
    // recalled; the stale leftovers stay gone.
    pushMakerTaskUpdate('s1', 'collab-worker', { source: 'codex' });
    const visible = remoteSessionStore.getSessionTaskUpdates('s1');
    expect([...new Set([...visible.values()].map((u) => u.taskId))]).toEqual(['collab-worker']);
  });

  it('emits when the maker turn boundary changes even if the broad run status did not', () => {
    // The activity stream opened the broad running flag first (the boundary stays closed —
    // idle-recovery paths may close it but only maker status can open it).
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:activity', {
      sessionId: 's1',
      phase: 'running',
      compactDetail: '',
    });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);

    // The maker status then arrives with nothing new for the broad status object — only
    // the turn boundary flips. Subscribers (the orphan render gate) must still be notified.
    const before = remoteSessionStore.getStoreVersion();
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);
    expect(remoteSessionStore.getStoreVersion()).toBeGreaterThan(before);
  });

  it('parks a running codex worker at turn start and recalls it with merged history on its next update', () => {
    // A collab worker lives in its own codex session across turns; its updates land in the
    // MAIN session's map. A stale "missed terminal, stays running" leftover is indistinguishable
    // from an alive-but-quiet worker, so NO pre-existing entry may render in the new turn —
    // only post-boundary updates count as evidence of life (park, don't drop).
    pushMakerStatus('s1', { isRunning: true });
    pushMakerTaskUpdate('s1', 'claude-subagent', { source: 'claude-code' });
    pushMakerTaskUpdate('s1', 'codex-worker', { source: 'codex', description: 'original prompt' });
    pushMakerTaskUpdate('s1', 'codex-worker-done', { source: 'codex', status: 'completed' });
    pushMakerStatus('s1', { isRunning: false });

    // New turn: nothing pre-existing is visible — the exact card the user reported can no
    // longer replay, no matter how fresh the stale entry looks.
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(0);

    // The worker's next update recalls the parked entry and merges its history: the new
    // update carries no description, yet the original prompt survives.
    pushMakerTaskUpdate('s1', 'codex-worker', { source: 'codex' });
    const visible = remoteSessionStore.getSessionTaskUpdates('s1');
    const recalled = [...visible.values()].find((u) => u.taskId === 'codex-worker');
    expect(recalled?.description).toBe('original prompt');
    expect([...new Set([...visible.values()].map((u) => u.taskId))]).toEqual(['codex-worker']);
  });

  it('still clears stale updates when activity pushes marked the session running first', () => {
    // Leftover from a finished turn.
    pushMakerStatus('s1', { isRunning: true });
    pushMakerTaskUpdate('s1', 'task-old');
    pushMakerStatus('s1', { isRunning: false });

    // Reconnect/foreground path: the lightweight activity push (or active-session snapshot)
    // flips the broad running flag before any maker status event arrives.
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:activity', {
      sessionId: 's1',
      phase: 'running',
      compactDetail: '…',
    });
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    // The orphan-render gate must stay closed until the maker turn boundary confirms.
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);

    // First maker status still detects the turn start (its own boundary, not the broad
    // flag) and sweeps the stale entries before the orphan gate opens.
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(0);
  });

  it('discards a parked worker that crossed a second boundary, while a later update still self-heals', () => {
    pushMakerStatus('s1', { isRunning: true });
    pushMakerTaskUpdate('s1', 'codex-worker', { source: 'codex', description: 'original prompt' });
    pushMakerStatus('s1', { isRunning: false });

    // Boundary 1: parked (invisible). Boundary 2: still no post-boundary update → dropped.
    pushMakerStatus('s1', { isRunning: true });
    pushMakerStatus('s1', { isRunning: false });
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.getSessionTaskUpdates('s1').size).toBe(0);

    // If the task somehow pushes again after being dropped, it rebuilds as a fresh entry
    // (self-healing; the parked history is gone, so no merged description).
    pushMakerTaskUpdate('s1', 'codex-worker', { source: 'codex' });
    const rebuilt = [...remoteSessionStore.getSessionTaskUpdates('s1').values()].find((u) => u.taskId === 'codex-worker');
    expect(rebuilt).toBeDefined();
    expect(rebuilt?.description).toBeUndefined();
  });

  it('closes the maker turn gate from authoritative idle recovery paths', () => {
    // Boundary stuck true: the terminal maker event was missed while backgrounded.
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);

    // (a) completed/error activity push closes the gate.
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:activity', {
      sessionId: 's1',
      phase: 'completed',
      compactDetail: '',
    });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);

    // (b) an active-session snapshot without the session closes the gate too.
    remoteSessionStore.setDeviceSessions('dev-1', 'MacBook', [session('s1')]);
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);
    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{ sessionId: 's1', isTurnRunning: false }]);
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);

    // Neither idle path may OPEN the gate — that stays maker-status-only.
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:activity', {
      sessionId: 's1',
      phase: 'running',
      compactDetail: '',
    });
    remoteSessionStore.setActiveSessionSnapshots('dev-1', [{ sessionId: 's1', isTurnRunning: true }]);
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);
  });

  it('closes the maker turn boundary on done and terminal error events', () => {
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'done', data: {} },
    });
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);
  });

  it('keeps terminal prediction eligibility through acknowledgement and clears it for the next run', () => {
    pushMakerStatus('s1', { isRunning: true });
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1', event: { type: 'error', data: { message: 'Failed', isTerminal: true } },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').hasTerminalError).toBe(true);
    remoteSessionStore.applySessionActivity('dev-1', { sessionId: 's1', phase: 'completed', attention: false });
    expect(remoteSessionStore.getSessionRunStatus('s1').hasTerminalError).toBe(true);
    pushMakerStatus('s1', { isRunning: false });
    expect(remoteSessionStore.getSessionRunStatus('s1').hasTerminalError).toBe(true);
    pushMakerStatus('s1', { isRunning: true });
    expect(remoteSessionStore.getSessionRunStatus('s1').hasTerminalError).toBe(false);
  });

  it('recovers a terminal error from activity even without its maker event', () => {
    remoteSessionStore.applySessionActivity('dev-1', { sessionId: 's1', phase: 'error', attention: false });
    expect(remoteSessionStore.getSessionRunStatus('s1').hasTerminalError).toBe(true);
    remoteSessionStore.applySessionActivity('dev-1', { sessionId: 's1', phase: 'running' });
    expect(remoteSessionStore.getSessionRunStatus('s1').hasTerminalError).toBe(false);
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1', event: { type: 'error', data: { message: 'Retrying', isTerminal: false, willRetry: true } },
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').hasTerminalError).toBe(false);
  });

  it.each(['ask_user_question', 'plan_review'])('keeps the product running while %s awaits confirmation across an SDK boundary', (kind) => {
    vi.useFakeTimers();
    try {
      pushMakerStatus('s1', { isRunning: true });
      remoteSessionStore.setPendingInteractions('s1', [{ request: { kind, requestId: 'human-1' } }]);
      pushMakerText('s1', 'persist-1', 'first segment', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1',
        event: {
          type: 'status',
          turnContinuationId: 7,
          data: { isRunning: false, status: 'Done' },
        },
      });
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1',
        event: { type: 'done', turnContinuationId: 7, data: {} },
      });

      expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
      expect(remoteSessionStore.getPendingInteractions('s1')).toHaveLength(1);
      expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(true);
      expect(remoteSessionStore.getSessionRunStatus('s1').startedAt).not.toBeNull();
      expect(remoteSessionStore.getMessages('s1')[0]?.agentMeta?.isStreaming).toBe(true);

      remoteSessionStore.applyRemotePush('dev-1', 'maker:interaction-dismissed', {
        sessionId: 's1', requestId: 'human-1', resolvedAs: 'allow',
      });
      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1',
        event: { type: 'done', data: {} },
      });

      expect(remoteSessionStore.isSessionRunning('s1')).toBe(false);
      expect(remoteSessionStore.getPendingInteractions('s1')).toHaveLength(0);
      expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);
      expect(remoteSessionStore.getMessages('s1')[0]?.agentMeta?.isStreaming).not.toBe(true);
    } finally {
      vi.useRealTimers();
    }
  });

  it('preserves boundary agent metadata when finalizing a streaming row', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'persist-1', 'sub-agent answer', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
        sessionId: 's1',
        event: {
          type: 'done',
          agentMeta: { parentUuid: 'parent-1', uuid: 'child-1' },
          data: {},
        },
      });

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'persist-1',
        content: 'sub-agent answer',
        agentMeta: { parentUuid: 'parent-1', uuid: 'child-1' },
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('stores and clears list-level live activity from the sessions stream', () => {
    vi.useFakeTimers();
    vi.setSystemTime(new Date('2026-01-01T00:00:10.000Z'));

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:activity', {
      sessionId: 's1',
      phase: 'running',
      compactDetail: '正在检查失败测试',
    });

    expect(remoteSessionStore.isSessionRunning('s1')).toBe(true);
    expect(remoteSessionStore.getSessionLiveActivity('s1')).toMatchObject({
      sessionId: 's1',
      phase: 'running',
      compactDetail: '正在检查失败测试',
    });
    expect(remoteSessionStore.getSessionRunStatus('s1').startedAt).toBe(
      Date.parse('2026-01-01T00:00:10.000Z'),
    );

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:activity', {
      sessionId: 's1',
      phase: 'completed',
      compactDetail: '',
    });

    expect(remoteSessionStore.getSessionLiveActivity('s1')).toBeNull();
    expect(remoteSessionStore.isSessionRunning('s1')).toBe(false);
    vi.useRealTimers();
  });

  it('keeps empty snapshots referentially stable for useSyncExternalStore', () => {
    expect(remoteSessionStore.getMessages('missing')).toBe(remoteSessionStore.getMessages('missing'));
    expect(remoteSessionStore.getPendingInteractions('missing')).toBe(
      remoteSessionStore.getPendingInteractions('missing'),
    );
  });

  it('applies realtime session patches from device-link push', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: 'Old' })]);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
      sessionId: 's1',
      patch: { title: 'New' },
    });

    expect(remoteSessionStore.getSessions()[0].title).toBe('New');
  });

  it('fences an older whole-list snapshot after created/patched pushes per device', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: 'Old' })]);
    const dev1Epoch = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-1');
    const dev2Epoch = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-2');

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
      sessionId: 's1',
      patch: { title: 'New' },
    });
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-1', dev1Epoch)).toBe(false);
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-2', dev2Epoch)).toBe(true);

    const afterPatch = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-1');
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:created', { sessionId: 's2' });
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-1', afterPatch)).toBe(false);

    const beforeReset = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-1');
    remoteSessionStore.clear();
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-1', beforeReset)).toBe(false);
  });

  it('fences an older whole-list snapshot after valid session usage pushes', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);

    const beforeSpend = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-1');
    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-spend-changed', {
      sessionId: 's1',
      totalCostUsd: 1.23,
    });
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-1', beforeSpend)).toBe(false);

    const beforeTokens = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-1');
    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-tokens-changed', {
      sessionId: 's1',
      totalTokens: 45_000,
    });
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-1', beforeTokens)).toBe(false);

    const beforeInvalid = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-1');
    remoteSessionStore.applyRemotePush('dev-1', 'usage:session-tokens-changed', {
      sessionId: 's1',
      totalTokens: -1,
    });
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-1', beforeInvalid)).toBe(true);
  });

  it('mirrors goal status pushes per session and clears on null goal', () => {
    const goal = {
      sessionId: 's1',
      status: 'active',
      objective: '修完登录 bug',
      turnsUsed: 2,
      tokensUsed: 1200,
      maxTurns: 20,
      noProgressLimit: 3,
      budgetTokens: null,
      usageResetAt: null,
      startedAt: 1,
      lastReason: null,
    };
    remoteSessionStore.applyRemotePush('dev-1', 'maker:goal:status-changed', {
      sessionId: 's1',
      goal,
    });
    expect(remoteSessionStore.getGoalStatus('s1')).toEqual(goal);
    // 未拉取过的会话是 undefined(unknown),不能压平成 null——见 getGoalStatus 注释。
    expect(remoteSessionStore.getGoalStatus('other')).toBeUndefined();

    remoteSessionStore.applyRemotePush('dev-1', 'maker:goal:status-changed', {
      sessionId: 's1',
      goal: null,
    });
    expect(remoteSessionStore.getGoalStatus('s1')).toBeNull();
  });

  it('requests a reseed when a push cannot be applied from local mirror state', () => {
    const reseed = vi.fn();
    remoteSessionStore.registerReseedHandler('dev-1', reseed);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:created', { sessionId: 's2' });
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
      sessionId: 's3',
      patch: { status: 'active' },
    });

    expect(reseed).toHaveBeenCalledTimes(2);
  });

  it('requests a reseed when unpinning a mirrored session that may have been included only because it was pinned', () => {
    const reseed = vi.fn();
    remoteSessionStore.registerReseedHandler('dev-1', reseed);
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('old-pinned', { pinnedAt: '2026-01-02T00:00:00.000Z' }),
    ]);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
      sessionId: 'old-pinned',
      patch: { pinnedAt: null },
    });

    expect(remoteSessionStore.getSessions()[0].pinnedAt).toBeNull();
    expect(reseed).toHaveBeenCalledTimes(1);
  });

  // Unit regression: unopened side chats are absent from the ordinary mirror.
  // Existing reseed coverage handled creation only, leaving their count stale.
  it.each(['deleted', 'archived'] as const)('invalidates device lists on an unseen child becoming %s', status => {
    for (const withShard of [false, true]) {
      remoteSessionStore.clear();
      if (withShard) remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('parent')]);
      const refresh = vi.fn();
      const otherDevice = vi.fn();
      const off = remoteSessionStore.registerReseedHandler('dev-1', refresh);
      const offOther = remoteSessionStore.registerReseedHandler('dev-2', otherDevice);
      const removed = vi.fn();
      const offRemoved = remoteSessionStore.onSessionRemoved(removed);
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:patched', {
        sessionId: 'unopened-child', patch: { status },
      });
      expect(refresh).toHaveBeenCalledOnce();
      expect(otherDevice).not.toHaveBeenCalled();
      expect(removed).toHaveBeenCalledExactlyOnceWith('dev-1', 'unopened-child');
      expect(remoteSessionStore.getSessions().some(row => row.id === 'unopened-child')).toBe(false);
      off(); offOther(); offRemoved();
    }
  });

  it('fans reseed out to every subscriber and only unregisters the one that left', () => {
    const home = vi.fn();
    const detail = vi.fn();
    const unregisterHome = remoteSessionStore.registerReseedHandler('dev-1', home);
    remoteSessionStore.registerReseedHandler('dev-1', detail);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:created', { sessionId: 's2' });
    expect(home).toHaveBeenCalledTimes(1);
    expect(detail).toHaveBeenCalledTimes(1);

    // Detail screen unmounts; Home must keep reseeding (regression: a shared Map dropped it).
    unregisterHome();
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:sessions:created', { sessionId: 's3' });
    expect(home).toHaveBeenCalledTimes(1);
    expect(detail).toHaveBeenCalledTimes(2);
  });

  it('keeps cached messages but invalidates sync marker and marks pendingRefresh on local-db:session:error-persisted push', () => {
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    remoteSessionStore.markSessionMessagesSynced('s1', { _count: { messages: 1 }, updatedAt: '2026-01-01T00:00:00.000Z' });

    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', { _count: { messages: 1 }, updatedAt: '2026-01-01T00:00:00.000Z' })).toBe(true);
    expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(false);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:session:error-persisted', { sessionId: 's1' });

    // 消息保留(避免空白帧),sync marker 失效,待刷新标记置 true。
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', { _count: { messages: 1 }, updatedAt: '2026-01-01T00:00:00.000Z' })).toBe(false);
    expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(true);
  });

  it('clears messages and sync marker, then marks pendingRefresh when error arrives before initial cache', () => {
    // 未缓存但页面可能正在首次 listMessages:仍需 pendingRefresh 触发下一轮拉取,避免 in-flight 旧响应盖住新 error。
    expect(remoteSessionStore.getMessages('s2')).toHaveLength(0);

    remoteSessionStore.applyRemotePush('dev-1', 'local-db:session:error-persisted', { sessionId: 's2' });

    expect(remoteSessionStore.getMessages('s2')).toHaveLength(0);
    expect(remoteSessionStore.hasPendingRefresh('s2')).toBe(true);
  });

  it('consumePendingRefresh returns true and clears the flag', () => {
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:session:error-persisted', { sessionId: 's1' });

    expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(true);
    expect(remoteSessionStore.consumePendingRefresh('s1')).toBe(true);
    expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(false);
    // 二次消费返回 false。
    expect(remoteSessionStore.consumePendingRefresh('s1')).toBe(false);
  });

  it('applies realtime message and interaction pushes', () => {
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1',
      message: message('m1', 's1'),
    });
    remoteSessionStore.applyRemotePush('dev-1', 'maker:interaction-request', {
      sessionId: 's1',
      request: { kind: 'permission', requestId: 'req-1' },
    });

    expect(remoteSessionStore.getMessages('s1').map((item) => item.id)).toEqual(['m1']);
    expect(remoteSessionStore.getPendingInteractions('s1')).toHaveLength(1);

    remoteSessionStore.applyRemotePush('dev-1', 'maker:interaction-dismissed', {
      sessionId: 's1',
      requestId: 'req-1',
    });

    expect(remoteSessionStore.getPendingInteractions('s1')).toHaveLength(0);
  });

  it('keeps pending interactions sorted by desktop priority and preserves invalid requests', () => {
    remoteSessionStore.setPendingInteractions('s1', [
      pending('issue_confirm', 'issue-1'),
      pending('permission', 'permission-1'),
      pending('permission', 'permission-1'),
      pending('plan_review', 'plan-1'),
      pending('ask_user_question', undefined, 'ask-persist'),
    ]);

    expect(remoteSessionStore.getPendingInteractions('s1').map((item) => item.request.kind)).toEqual([
      'plan_review',
      'permission',
      'ask_user_question',
      'issue_confirm',
    ]);
    expect(remoteSessionStore.getPendingInteractions('s1').map((item) => item.request.requestId ?? item.persistId)).toEqual([
      'plan-1',
      'permission-1',
      'ask-persist',
      'issue-1',
    ]);
  });

  it('does not finalize streaming for a reconnect snapshot containing only a suppressed interaction', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.beginOptimisticInteractionDismiss('s1', 'req-stale');
      remoteSessionStore.settleOptimisticInteractionDismiss('s1', 'req-stale', { kind: 'confirmed' });
      pushMakerText('s1', 'persist-1', 'still generating', false);
      vi.runOnlyPendingTimers();

      remoteSessionStore.setPendingInteractions(
        's1',
        [pending('permission', 'req-stale')],
        { finalizeStreaming: true },
      );

      expect(remoteSessionStore.getPendingInteractions('s1')).toHaveLength(0);
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        content: 'still generating',
        agentMeta: { isStreaming: true },
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('dismisses one pending interaction by requestId without clearing same-session siblings', () => {
    remoteSessionStore.setPendingInteractions('s1', [
      pending('permission', 'permission-1'),
      pending('ask_user_question', 'ask-1'),
      pending('plan_review', 'plan-1'),
    ]);

    remoteSessionStore.dismissInteraction('s1', 'ask-1');

    expect(remoteSessionStore.getPendingInteractions('s1').map((item) => item.request.requestId)).toEqual([
      'plan-1',
      'permission-1',
    ]);
  });

  it('patches existing messages from realtime turn cost pushes', () => {
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);

    remoteSessionStore.applyRemotePush('dev-1', 'usage:message-turn-cost', {
      sessionId: 's1',
      clientId: 'm1',
      turnMoney: {
        amount: 0.29,
        currency: 'CNY',
        approximate: false,
        kind: 'actual-cost',
      },
    });

    expect(remoteSessionStore.getMessages('s1')[0].agentMeta).toMatchObject({
      turnCost: {
        amount: 0.29,
        currency: 'CNY',
      },
      turnCostIsEstimate: false,
    });
  });

  // 无当前 segment 金额、只有整轮累计 + token 明细 = 桌面自动续跑的无价收尾轮。
  // 累计金额必须一起落进 agentMeta,否则操作行会用 token 顶掉这一轮已经花掉的钱
  // (不变量正本见 apps/desktop/src/shared/turnCostPayload.ts)。
  it('keeps the user-round total from usage-only turn cost pushes', () => {
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);

    remoteSessionStore.applyRemotePush('dev-1', 'usage:message-turn-cost', {
      sessionId: 's1',
      clientId: 'm1',
      userTurnMoney: {
        amount: 1.25,
        currency: 'USD',
        approximate: false,
        kind: 'actual-cost',
      },
      userTurnCostUsd: 1.25,
      userTurnCostIsEstimate: true,
      turnUsageDetails: { totalTokens: 2_100_000 },
    });

    const meta = remoteSessionStore.getMessages('s1')[0].agentMeta;
    expect(meta).toMatchObject({
      userTurnCost: { amount: 1.25, currency: 'USD' },
      userTurnCostUsd: 1.25,
      userTurnCostIsEstimate: true,
      turnUsageDetails: { totalTokens: 2_100_000 },
    });
    // 当前 segment 没有报价 → 不写任何 segment 金额字段(不记账)。
    expect(meta?.turnCost).toBeUndefined();
    expect(meta?.turnCostUsd).toBeUndefined();
  });

  it('patches existing messages from realtime model mismatch pushes', () => {
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);

    remoteSessionStore.applyRemotePush('dev-1', 'usage:message-model-mismatch', {
      sessionId: 's1',
      clientId: 'm1',
      modelMismatch: { selected: 'claude-fable-5', actual: 'claude-opus-4-8' },
    });

    expect(remoteSessionStore.getMessages('s1')[0].agentMeta).toMatchObject({
      modelMismatch: { selected: 'claude-fable-5', actual: 'claude-opus-4-8' },
    });

    // 字段不全的 push 一律忽略,不写入半截标记。
    remoteSessionStore.setMessages('s2', [message('m2', 's2')]);
    remoteSessionStore.applyRemotePush('dev-1', 'usage:message-model-mismatch', {
      sessionId: 's2',
      clientId: 'm2',
      modelMismatch: { selected: 'claude-fable-5' },
    });
    expect(remoteSessionStore.getMessages('s2')[0].agentMeta?.modelMismatch).toBeUndefined();
  });

  it('applies input projection push and exposes an empty stable fallback', () => {
    expect(remoteSessionStore.getInputProjection('missing')).toBe(
      remoteSessionStore.getInputProjection('missing'),
    );

    remoteSessionStore.applyRemotePush('dev-1', 'maker:input:projection', projection('s1'));

    expect(remoteSessionStore.getInputProjection('s1')).toMatchObject({
      sessionId: 's1',
      pendingQueue: [{ clientId: 'q-1', text: 'queued' }],
      queuePaused: true,
    });
  });

  it('rejects a late projection query after a newer push and terminal boundary', () => {
    const ownerProjection = {
      ...projection('s1'),
      continuationTurnClientId: 'resume-1',
    };
    const expectedEpoch = remoteSessionStore.captureInputProjectionAuthorityEpoch('s1');
    remoteSessionStore.setInputProjection('s1', ownerProjection);
    const queryEpoch = remoteSessionStore.captureInputProjectionAuthorityEpoch('s1');

    remoteSessionStore.applyRemotePush('dev-1', 'maker:input:projection', {
      ...projection('s1'),
      continuationTurnClientId: null,
    });
    remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
      sessionId: 's1',
      event: { type: 'done', data: {} },
    });

    expect(remoteSessionStore.setInputProjectionIfCurrent('s1', ownerProjection, queryEpoch)).toBe(false);
    expect(remoteSessionStore.getInputProjection('s1').continuationTurnClientId).toBeNull();
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);
    expect(remoteSessionStore.captureInputProjectionAuthorityEpoch('s1')).not.toBe(expectedEpoch);
  });

  it('accepts a projection query result when no newer authority event arrived', () => {
    const expectedEpoch = remoteSessionStore.captureInputProjectionAuthorityEpoch('s1');
    expect(remoteSessionStore.setInputProjectionIfCurrent('s1', projection('s1'), expectedEpoch)).toBe(true);
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue[0]?.clientId).toBe('q-1');
  });

  it('keeps optimistic projection writes out of remote acceptance evidence', () => {
    const local = projection('s1', 'q-local');
    const authorityEpoch = remoteSessionStore.captureInputProjectionAuthorityEpoch('s1');
    const remoteEpoch = remoteSessionStore.captureInputProjectionRemoteEpoch('s1');
    remoteSessionStore.setInputProjectionOptimistically('s1', { ...local, queuePaused: true });
    expect(remoteSessionStore.captureInputProjectionAuthorityEpoch('s1')).not.toBe(authorityEpoch);
    expect(remoteSessionStore.captureInputProjectionRemoteEpoch('s1')).toBe(remoteEpoch);
    expect(remoteSessionStore.hasAuthoritativeQueuedItemSince('s1', 'q-local', remoteEpoch)).toBe(false);
    expect(remoteSessionStore.setInputProjectionIfCurrent('s1', local, authorityEpoch, remoteEpoch)).toBe(false);
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue[0]?.clientId).toBe('q-local');

    remoteSessionStore.setInputProjection('s1', local);
    expect(remoteSessionStore.hasAuthoritativeQueuedItemSince('s1', 'q-local', remoteEpoch)).toBe(true);
  });

  it('records accepted evidence from a stale response without overwriting a newer projection', () => {
    const expectedEpoch = remoteSessionStore.captureInputProjectionAuthorityEpoch('s1');
    const expectedRemoteEpoch = remoteSessionStore.captureInputProjectionRemoteEpoch('s1');
    remoteSessionStore.setInputProjection('s1', projection('s1', 'q-new'));

    expect(remoteSessionStore.setInputProjectionIfCurrent(
      's1',
      projection('s1', 'q-old'),
      expectedEpoch,
      expectedRemoteEpoch,
      'q-accepted',
    )).toBe(false);
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue[0]?.clientId).toBe('q-new');
    expect(remoteSessionStore.hasAuthoritativeQueuedItemSince(
      's1',
      'q-accepted',
      expectedRemoteEpoch,
    )).toBe(true);
  });

  it('settles a local optimistic row when its persisted user message arrives', () => {
    const local = projection('s1', 'q-local');
    const remoteEpoch = remoteSessionStore.captureInputProjectionRemoteEpoch('s1');
    remoteSessionStore.setInputProjectionOptimistically('s1', local);
    remoteSessionStore.appendMessage('s1', message('q-local', 's1'));
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue).toHaveLength(1);

    remoteSessionStore.appendMessage('s1', { ...message('q-local', 's1'), role: 'user' });
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue).toEqual([]);
    expect(remoteSessionStore.hasAuthoritativeQueuedItemSince('s1', 'q-local', remoteEpoch)).toBe(true);
  });

  it('clears a continuation owner at a terminal boundary without a projection clear push', () => {
    const ownerProjection = {
      ...projection('s1'),
      continuationTurnClientId: 'resume-1',
    };
    remoteSessionStore.setInputProjection('s1', ownerProjection);
    remoteSessionStore.setSessionRunning('s1', true);
    const operationEpoch = remoteSessionStore.captureInputProjectionAuthorityEpoch('s1');
    const operationRemoteEpoch = remoteSessionStore.captureInputProjectionRemoteEpoch('s1');

    remoteSessionStore.applyRemotePush('dev-1', 'maker:status-changed', {
      sessionId: 's1',
      status: 'closed',
    });

    expect(remoteSessionStore.getInputProjection('s1').continuationTurnClientId).toBeNull();
    expect(remoteSessionStore.isSessionMakerTurnRunning('s1')).toBe(false);
    expect(remoteSessionStore.setInputProjectionIfCurrent('s1', { ...ownerProjection, pendingQueue: [] }, operationEpoch, operationRemoteEpoch, 'q-1')).toBe(false);
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue[0]?.clientId).toBe('q-1');
    expect(remoteSessionStore.hasAuthoritativeQueuedItemSince('s1', 'q-1', operationRemoteEpoch)).toBe(true);
  });

  it('soft-invalidates an offline device without deleting sessions or messages', () => {
    const meta = session('s1', {
      updatedAt: '2026-01-01T00:00:01.000Z',
      _count: { messages: 1 },
    });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [meta]);
    remoteSessionStore.setDeviceSessions('dev-2', 'Windows', [session('s2')]);
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    pushMakerText('s1', 'live-1', 'streaming', false);
    remoteSessionStore.setMessages('s2', [message('m2', 's2')]);
    remoteSessionStore.markSessionMessagesSynced('s1', meta);
    remoteSessionStore.setPendingInteractions('s1', [{ request: { requestId: 'req-1' } }]);
    remoteSessionStore.setInputProjection('s1', projection('s1'));
    remoteSessionStore.setSessionRunning('s1', true);
    remoteSessionStore.setGoalStatus('s1', { status: 'running' } as never);

    remoteSessionStore.markDeviceOffline('dev-1');

    expect(remoteSessionStore.getSessions().map((item) => item.id).sort()).toEqual(['s1', 's2']);
    expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('dev-1');
    expect(remoteSessionStore.getMessages('s1')).toEqual([
      expect.objectContaining({ id: 'm1' }),
      expect.objectContaining({ content: 'streaming', agentMeta: expect.not.objectContaining({ isStreaming: true }) }),
    ]);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', meta)).toBe(false);
    expect(remoteSessionStore.getPendingInteractions('s1')).toEqual([]);
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue).toEqual([]);
    expect(remoteSessionStore.getSessionRunStatus('s1').isRunning).toBe(false);
    expect(remoteSessionStore.getGoalStatus('s1')).toBeUndefined();
    expect(remoteSessionStore.getMessages('s2')).toHaveLength(1);

    // 重复离线通知幂等,不会清掉保留的 last-known 内容。
    remoteSessionStore.markDeviceOffline('dev-1');
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(2);
  });

  it('preserves a provisional live reply anchor across transient device offline', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:01.000Z',
        updatedAt: '2026-01-01T00:00:01.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'current-live-assistant', 'Current reply', true);

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      }]);

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'current-user',
        'current-live-assistant',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not let a reconnecting newer send claim an older offline provisional reply', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:01.000Z',
        updatedAt: '2026-01-01T00:00:01.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'previous-live-assistant', 'Previous reply', true);

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      }));
      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('previous-user', 's1', '2026-01-01T00:00:01.000Z'),
          role: 'user',
          content: 'Previous question',
        },
        {
          ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Current question',
        },
      ]);

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'previous-user',
        'previous-live-assistant',
        'current-user',
      ]);
      expect(rows.find((item) => item.clientId === 'previous-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:00:01.000Z');
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps per-identity transport ownership across soft offline finalization', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('stale-mac', 'Mac', [session('s1')]);
      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale reply', isFinal: false },
        },
      });
      vi.runOnlyPendingTimers();

      remoteSessionStore.markDeviceOffline('stale-mac');
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: false },
        },
      });
      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'shared-live-assistant',
        content: 'Current reply',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps a pre-metadata offline reply unbound until its older user row arrives', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'previous-live-assistant', 'Previous reply', true);

      // No session list row exists yet, so the offline transition must use the
      // maker event's transport device rather than sessionDeviceIndex.
      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      }));
      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      }]);

      expect(remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'previous-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:10:00.000Z');

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('previous-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Previous question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'previous-user',
        'previous-live-assistant',
        'current-user',
      ]);
      expect(rows.find((item) => item.clientId === 'previous-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:00:01.000Z');
    } finally {
      vi.useRealTimers();
    }
  });

  it('flushes and freezes a pre-metadata text delta when its transport goes offline', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'previous-live-assistant', 'Previous reply', false);

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      }));
      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      }]);

      expect(remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'previous-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:10:00.000Z');

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('previous-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Previous question',
      });
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'previous-user',
        'previous-live-assistant',
        'current-user',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('pairs an older delayed user with the offline-unbound reply before the new round reply', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'previous-live-assistant', 'Previous reply', true);
      remoteSessionStore.markDeviceOffline('dev-1');

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      }));
      vi.setSystemTime(new Date('2026-01-01T00:11:00.000Z'));
      pushMakerText('s1', 'current-live-assistant', 'Current reply', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('previous-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Previous question',
      });
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'previous-user',
        'previous-live-assistant',
        'current-live-assistant',
      ]);
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'previous-user',
        'previous-live-assistant',
        'current-user',
        'current-live-assistant',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('pairs multiple offline-unbound reply cohorts with realtime users in arrival order', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: true },
      });
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: false },
      });
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: true },
      });
      vi.setSystemTime(new Date('2026-01-01T00:11:00.000Z'));
      pushMakerText('s1', 'live-assistant-2', 'Second reply', true);

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      }));
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'First question',
      });
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Second question',
      });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'user-2',
        'live-assistant-2',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('pairs multiple offline-unbound reply cohorts with a reconnect window in arrival order', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: true },
      });
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: false },
      });
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: true },
      });
      vi.setSystemTime(new Date('2026-01-01T00:11:00.000Z'));
      pushMakerText('s1', 'live-assistant-2', 'Second reply', true);

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      }));
      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
          role: 'user',
          content: 'First question',
        },
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
      ], { moreBeyondWindow: false });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'user-2',
        'live-assistant-2',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('leaves offline-unbound cohorts pending when the reconnect window is truncated', () => {
    vi.useFakeTimers();
    try {
      for (const [index, createdAt] of [
        ['1', '2026-01-01T00:10:00.000Z'],
        ['2', '2026-01-01T00:11:00.000Z'],
        ['3', '2026-01-01T00:12:00.000Z'],
      ] as const) {
        remoteSessionStore.applyMakerEvent('s1', {
          type: 'status',
          data: { isRunning: true },
        });
        vi.setSystemTime(new Date(createdAt));
        pushMakerText('s1', `live-assistant-${index}`, `Reply ${index}`, true);
        remoteSessionStore.applyMakerEvent('s1', {
          type: 'status',
          data: { isRunning: false },
        });
      }

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:03.000Z',
        updatedAt: '2026-01-01T00:00:03.000Z',
      }));
      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
        {
          ...messageAt('user-3', 's1', '2026-01-01T00:00:03.000Z'),
          role: 'user',
          content: 'Third question',
        },
      ], { moreBeyondWindow: true });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-2',
        'user-3',
        'live-assistant-1',
        'live-assistant-2',
        'live-assistant-3',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('leaves offline-unbound cohorts pending when the reconnect window has extra users', () => {
    vi.useFakeTimers();
    try {
      for (const [index, createdAt] of [
        ['1', '2026-01-01T00:10:00.000Z'],
        ['2', '2026-01-01T00:11:00.000Z'],
      ] as const) {
        remoteSessionStore.applyMakerEvent('s1', {
          type: 'status',
          data: { isRunning: true },
        });
        vi.setSystemTime(new Date(createdAt));
        pushMakerText('s1', `live-assistant-${index}`, `Reply ${index}`, true);
        remoteSessionStore.applyMakerEvent('s1', {
          type: 'status',
          data: { isRunning: false },
        });
      }

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:03.000Z',
        updatedAt: '2026-01-01T00:00:03.000Z',
      }));
      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
          role: 'user',
          content: 'First question',
        },
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
        {
          ...messageAt('user-3', 's1', '2026-01-01T00:00:03.000Z'),
          role: 'user',
          content: 'Third question',
        },
      ], { moreBeyondWindow: false });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'user-2',
        'user-3',
        'live-assistant-1',
        'live-assistant-2',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('leaves offline-unbound cohorts pending when an equal-sized reconnect window is truncated', () => {
    vi.useFakeTimers();
    try {
      for (const [index, createdAt] of [
        ['1', '2026-01-01T00:10:00.000Z'],
        ['2', '2026-01-01T00:11:00.000Z'],
      ] as const) {
        remoteSessionStore.applyMakerEvent('s1', {
          type: 'status',
          data: { isRunning: true },
        });
        vi.setSystemTime(new Date(createdAt));
        pushMakerText('s1', `live-assistant-${index}`, `Reply ${index}`, true);
        remoteSessionStore.applyMakerEvent('s1', {
          type: 'status',
          data: { isRunning: false },
        });
      }

      remoteSessionStore.markDeviceOffline('dev-1');
      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:03.000Z',
        updatedAt: '2026-01-01T00:00:03.000Z',
      }));
      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
        {
          ...messageAt('user-3', 's1', '2026-01-01T00:00:03.000Z'),
          role: 'user',
          content: 'Third question',
        },
      ], { moreBeyondWindow: true });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-2',
        'user-3',
        'live-assistant-1',
        'live-assistant-2',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('emits when soft offline only clears pending-refresh metadata', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:session:error-persisted', {
      sessionId: 's1',
    });
    expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(true);

    const notify = vi.fn();
    const unsubscribe = remoteSessionStore.subscribe(notify);
    try {
      remoteSessionStore.markDeviceOffline('dev-1');
      expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(false);
      expect(notify).toHaveBeenCalledTimes(1);
    } finally {
      unsubscribe();
    }
  });

  it('removes a device shard with its messages and pending interactions', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.setDeviceSessions('dev-2', 'Windows', [session('s2')]);
    remoteSessionStore.setMessages('s1', [message('m1', 's1')]);
    remoteSessionStore.setMessages('s2', [message('m2', 's2')]);
    remoteSessionStore.setPendingInteractions('s1', [{ request: { requestId: 'req-1' } }]);
    remoteSessionStore.setInputProjection('s1', projection('s1'));

    remoteSessionStore.removeDevice('dev-1');

    expect(remoteSessionStore.getSessions().map((item) => item.id)).toEqual(['s2']);
    expect(remoteSessionStore.getMessages('s1')).toEqual([]);
    expect(remoteSessionStore.getPendingInteractions('s1')).toEqual([]);
    expect(remoteSessionStore.getInputProjection('s1').pendingQueue).toEqual([]);
    expect(remoteSessionStore.getMessages('s2')).toHaveLength(1);
  });

  it('discards a pre-metadata text batch when its transport is hard removed', () => {
    vi.useFakeTimers();
    try {
      pushMakerText('s1', 'live-assistant', 'Stale reply', false);

      remoteSessionStore.removeDevice('dev-1');
      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toEqual([]);
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
      expect(remoteSessionStore.getMessages('s1')).toEqual([]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('removes a flushed pre-metadata reply with its transport-owned anchor', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'Stale reply', true);
      remoteSessionStore.setSessionRunning('s1', true);
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
      expect(remoteSessionStore.getSessionRunStatus('s1').isRunning).toBe(true);

      remoteSessionStore.removeDevice('dev-1');
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:01.000Z',
        updatedAt: '2026-01-01T00:00:01.000Z',
      })]);
      expect(remoteSessionStore.getMessages('s1')).toEqual([]);
      expect(remoteSessionStore.getSessionRunStatus('s1').isRunning).toBe(false);
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps a current shard window when removing a stale transport for the same session id', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'stale-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale reply', isFinal: true },
        },
      });
      remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
      remoteSessionStore.setMessages('s1', [messageAt(
        'current-assistant',
        's1',
        '2026-01-01T00:00:01.000Z',
      )]);

      remoteSessionStore.removeDevice('stale-mac');

      expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('current-mac');
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'current-assistant',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not let an identical transport replay claim a persisted assistant row', () => {
    remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
    remoteSessionStore.setMessages('s1', [message('persisted-assistant', 's1')]);

    remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
      sessionId: 's1',
      persistId: 'persisted-assistant',
      event: {
        type: 'text',
        data: { text: 'hello', isFinal: true },
      },
    });
    remoteSessionStore.removeDevice('stale-mac');

    expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('current-mac');
    expect(remoteSessionStore.getMessages('s1')).toEqual([
      message('persisted-assistant', 's1'),
    ]);
  });

  it('retires pending identity when an identical persisted assistant echo arrives', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:00:00.000Z'));
      pushMakerText('s1', 'persisted-assistant', 'hello', true);
      remoteSessionStore.appendMessage('s1', message('persisted-assistant', 's1'));

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('next-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Next question',
      });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'persisted-assistant',
        'next-user',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps pending identity for an identical live transport replay', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:00:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'hello', true);
      pushMakerText('s1', 'live-assistant', 'hello', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('trigger-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Question',
      });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'trigger-user',
        'live-assistant',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not let a stale transport mutate or claim a persisted assistant row', () => {
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Mac' }]);
    remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
    remoteSessionStore.setMessages('s1', [message('persisted-assistant', 's1')]);

    remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
      sessionId: 's1',
      persistId: 'persisted-assistant',
      event: {
        type: 'text',
        data: { text: 'Stale replay', isFinal: true },
      },
    });
    remoteSessionStore.removeDevice('stale-mac');

    expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('current-mac');
    expect(remoteSessionStore.getMessages('s1')).toEqual([
      message('persisted-assistant', 's1'),
    ]);
  });

  it.each([
    ['before the current transport batch flushes', false],
    ['after the current transport batch flushes', true],
  ] as const)('keeps replacement transport state %s when removing an indexed stale shard', (
    _label,
    flushBeforeRemoval,
  ) => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('stale-mac', 'Mac', [session('s1')]);
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'current-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: false },
        },
      });
      if (flushBeforeRemoval) vi.runOnlyPendingTimers();

      remoteSessionStore.removeDevice('stale-mac');
      vi.runOnlyPendingTimers();

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'current-live-assistant',
        content: 'Current reply',
      }]);

      remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
      expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('current-mac');
      expect(remoteSessionStore.getMessages('s1')).toHaveLength(1);
    } finally {
      vi.useRealTimers();
    }
  });

  it.each([
    ['stale transport writes first', ['stale-mac', 'current-mac']],
    ['stale transport writes last', ['current-mac', 'stale-mac']],
  ] as const)('removes only the stale transport provisional reply when %s', (_label, deviceOrder) => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      for (const deviceId of deviceOrder) {
        const isStale = deviceId === 'stale-mac';
        remoteSessionStore.applyRemotePush(deviceId, 'maker:event', {
          sessionId: 's1',
          persistId: isStale ? 'stale-live-assistant' : 'current-live-assistant',
          event: {
            type: 'text',
            data: {
              text: isStale ? 'Stale reply' : 'Current reply',
              isFinal: true,
            },
          },
        });
      }

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId).sort()).toEqual([
        'current-live-assistant',
        'stale-live-assistant',
      ]);
      remoteSessionStore.removeDevice('stale-mac');

      expect(remoteSessionStore.getSessionDeviceId('s1')).toBe('current-mac');
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'current-live-assistant',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps a provisional reply that is also owned by the current transport', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      for (const deviceId of ['stale-mac', 'current-mac']) {
        remoteSessionStore.applyRemotePush(deviceId, 'maker:event', {
          sessionId: 's1',
          persistId: 'shared-live-assistant',
          event: {
            type: 'text',
            data: { text: 'Shared reply', isFinal: true },
          },
        });
      }

      remoteSessionStore.removeDevice('stale-mac');

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'shared-live-assistant',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not let a stale replay replace the current transport assembly', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Mac' }]);
      remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: true },
        },
      });
      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale replay', isFinal: true },
        },
      });

      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'shared-live-assistant',
        content: 'Current reply',
      }]);
      remoteSessionStore.removeDevice('stale-mac');
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'shared-live-assistant',
        content: 'Current reply',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('does not let a stale transport migrate the current generated assembly', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Mac' }]);
      remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: true },
        },
      });
      const currentClientId = remoteSessionStore.getMessages('s1')[0]?.clientId;

      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'stale-persisted-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale replay', isFinal: true },
        },
      });

      expect(currentClientId).toMatch(/^mobile-stream-/);
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: currentClientId,
        content: 'Current reply',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('keeps current generated ownership across a done boundary before stale replay', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Mac' }]);
      remoteSessionStore.setDeviceSessions('current-mac', 'Mac', [session('s1')]);
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: true },
        },
      });
      const currentClientId = remoteSessionStore.getMessages('s1')[0]?.clientId;
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        event: { type: 'done', data: {} },
      });

      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'stale-persisted-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale replay', isFinal: true },
        },
      });

      expect(currentClientId).toMatch(/^mobile-stream-/);
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: currentClientId,
        content: 'Current reply',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('allows a replacement transport when no authoritative device identity is available', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('stale-mac', 'Mac', [session('s1')]);
      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale reply', isFinal: true },
        },
      });
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: true },
        },
      });

      remoteSessionStore.removeDevice('stale-mac');
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'shared-live-assistant',
        content: 'Current reply',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('lets the canonical current transport replace an indexed stale assembly', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Mac' }]);
      remoteSessionStore.setDeviceSessions('stale-mac', 'Mac', [session('s1')]);
      remoteSessionStore.applyRemotePush('stale-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Stale reply', isFinal: true },
        },
      });
      remoteSessionStore.applyRemotePush('current-mac', 'maker:event', {
        sessionId: 's1',
        persistId: 'shared-live-assistant',
        event: {
          type: 'text',
          data: { text: 'Current reply', isFinal: true },
        },
      });

      remoteSessionStore.removeDevice('stale-mac');
      expect(remoteSessionStore.getMessages('s1')).toMatchObject([{
        clientId: 'shared-live-assistant',
        content: 'Current reply',
      }]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('writes canonicalDeviceId for a stale shard uniquely matching a current device, keeping deviceLinkDeviceId physical', () => {
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Lizi Mac' }]);
    remoteSessionStore.setDeviceSessions('current-mac', 'Lizi Mac', [session('s-current')]);
    remoteSessionStore.setDeviceSessions('stale-mac', 'Lizi Mac', [session('s-stale')]);

    const byId = new Map(remoteSessionStore.getSessions().map((s) => [s.id, s]));
    // 展示维度:两条都认领到 current-mac(详情页按 canonicalDeviceId 过滤时同时可见)。
    expect(byId.get('s-current')?.canonicalDeviceId).toBe('current-mac');
    expect(byId.get('s-stale')?.canonicalDeviceId).toBe('current-mac');
    // 路由维度:deviceLinkDeviceId 保持物理 shard id,sessionDeviceIndex 指向物理 shard(patch 不丢)。
    expect(byId.get('s-stale')?.deviceLinkDeviceId).toBe('stale-mac');
    expect(remoteSessionStore.getSessionDeviceId('s-stale')).toBe('stale-mac');
  });

  it('keeps links generated after re-link routed to the new device after archival removes the lookup', () => {
    const routeDeviceId = 'old-mac';
    remoteSessionStore.setDeviceIdentity([{ deviceId: routeDeviceId, name: 'Mac' }]);
    remoteSessionStore.setDeviceSessions(routeDeviceId, 'Mac', [session('source')]);
    const before = remoteSessionStore.getSessions()[0]!;
    expect(parseSessionDeepLinkUrl(buildMobileSessionMessageDeepLink(
      'source', 'anchor', { session: before, deviceId: routeDeviceId },
    ))?.deviceId).toBe(routeDeviceId);

    remoteSessionStore.setDeviceIdentity([{ deviceId: 'new-mac', name: 'Mac' }]);
    const current = remoteSessionStore.getSessions()[0]!;
    expect(current.canonicalDeviceId).toBe('new-mac');
    expect(current.deviceLinkDeviceId).toBe(routeDeviceId);
    const links = [
      buildMobileSessionMessageDeepLink('source', 'anchor', { session: current, deviceId: routeDeviceId }),
      sessionMenuCopyLink(current),
    ];
    remoteSessionStore.applySessionPatch(routeDeviceId, 'source', { status: 'archived' });
    expect(remoteSessionStore.getSessionDeviceId('source')).toBeUndefined();
    for (const link of links) {
      const target = parseSessionDeepLinkUrl(link)!;
      expect(resolveSessionLinkDeviceId({
        listedDeviceId: remoteSessionStore.getSessionDeviceId(target.sessionId),
        linkedDeviceId: target.deviceId,
        currentDeviceId: 'other-mac',
      })).toBe('new-mac');
    }
  });

  it('dedupes the same session id across stale/current shards, keeping the current shard copy', () => {
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mac', name: 'Lizi Mac' }]);
    // 同一 session id 同时出现在 stale 与 current 两个 shard(re-link 后旧 shard 残留)。
    remoteSessionStore.setDeviceSessions('stale-mac', 'Lizi Mac', [session('dup')]);
    remoteSessionStore.setDeviceSessions('current-mac', 'Lizi Mac', [session('dup')]);

    const rows = remoteSessionStore.getSessions().filter((s) => s.id === 'dup');
    expect(rows).toHaveLength(1); // 去重,不重复渲染
    expect(rows[0]?.deviceLinkDeviceId).toBe('current-mac'); // 保留 current shard 的真身
    expect(remoteSessionStore.getSessionDeviceId('dup')).toBe('current-mac');
  });

  it('does not merge two unknown same-name shards (canonicalDeviceId stays the original id)', () => {
    remoteSessionStore.setDeviceIdentity([]);
    remoteSessionStore.setDeviceSessions('ghost-1', 'MacBook Pro', [session('g1')]);
    remoteSessionStore.setDeviceSessions('ghost-2', 'MacBook Pro', [session('g2')]);

    const byId = new Map(remoteSessionStore.getSessions().map((s) => [s.id, s.canonicalDeviceId]));
    expect(byId.get('g1')).toBe('ghost-1');
    expect(byId.get('g2')).toBe('ghost-2');
  });

  it('leaves canonicalDeviceId equal to the physical shard id before any device identity is injected', () => {
    remoteSessionStore.setDeviceSessions('stale-mac', 'Lizi Mac', [session('s1')]);
    const s = remoteSessionStore.getSessions()[0];
    expect(s?.deviceLinkDeviceId).toBe('stale-mac');
    expect(s?.canonicalDeviceId).toBe('stale-mac');
  });

  it('does not claim when two stale shards share a name matching one current device', () => {
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-mbp', name: 'MacBook Pro' }]);
    remoteSessionStore.setDeviceSessions('stale-1', 'MacBook Pro', [session('a')]);
    remoteSessionStore.setDeviceSessions('stale-2', 'MacBook Pro', [session('b')]);

    const byId = new Map(remoteSessionStore.getSessions().map((s) => [s.id, s.canonicalDeviceId]));
    // stale 侧同名歧义 → 不认领,保留各自物理 id,不并到 current-mbp。
    expect(byId.get('a')).toBe('stale-1');
    expect(byId.get('b')).toBe('stale-2');
  });

  it('ignores placeholder device names when claiming stale shards', () => {
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'current-x', name: 'unknown' }]);
    remoteSessionStore.setDeviceSessions('stale-x', 'unknown', [session('p')]);
    // placeholder 名(unknown)不参与认领,stale-x 保留物理 id。
    expect(remoteSessionStore.getSessions().find((s) => s.id === 'p')?.canonicalDeviceId).toBe('stale-x');
  });
});

describe('maker:session-model-pref:changed push 路由', () => {
  it('路由进 sessionModelMirror(镜像可读),非法 payload 静默忽略', async () => {
    const { makeSessionMirrorAccessors, clearSessionMirror } = await import('@/session/sessionModelMirror');
    const acc = makeSessionMirrorAccessors('s-pref', vi.fn());
    remoteSessionStore.applyRemotePush('dev-1', 'maker:session-model-pref:changed', {
      sessionId: 's-pref',
      agent: 'codex',
      providerId: 'xd',
      model: 'gpt-5.5',
      effort: 'xhigh',
      fast: true,
    });
    expect(acc.getEffort('codex', 'xd', 'gpt-5.5')).toBe('xhigh');
    expect(acc.getFast('codex', 'xd', 'gpt-5.5')).toBe(true);

    remoteSessionStore.applyRemotePush('dev-1', 'maker:session-model-pref:changed', { nope: 1 });
    expect(acc.getEffort('codex', 'xd', 'nope')).toBeUndefined();
    clearSessionMirror('s-pref');
  });
});

describe('setDeviceSessions 对在途乐观创建行(pendingLocalCreation)的保护', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('比建成更早发出的旧列表不含该 id:合成行保留,不从列表消失', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s-old')]);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s-new', { pendingLocalCreation: true }));

    // 首页 in-flight 的 sessions:list 响应此刻返回(发出时被控端还没建这个任务)。
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s-old')]);
    const rows = remoteSessionStore.getSessions();
    expect(rows.map((s) => s.id)).toContain('s-new');
    expect(rows.find((s) => s.id === 's-new')?.pendingLocalCreation).toBe(true);
  });

  it('建成后 enqueue 落定前的新列表含该 id 但无标:合并权威字段并保留禁发标', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', []);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s-new', { pendingLocalCreation: true, title: '本地草稿标题' }));

    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s-new', { title: '权威标题' })]);
    const row = remoteSessionStore.getSessions().find((s) => s.id === 's-new');
    expect(row?.title).toBe('权威标题');
    expect(row?.pendingLocalCreation).toBe(true);
  });

  it('管线清标(pendingLocalCreation: false)后,列表对账恢复正常替换语义', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', []);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s-new', { pendingLocalCreation: true }));
    remoteSessionStore.applySessionPatch('dev-1', 's-new', { pendingLocalCreation: false });

    // 不含该 id 的列表到达:行不再受保护(正常对账,如会话被其它控制端删除)。
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s-old')]);
    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s-old']);
  });
});

describe('setDeviceSessions 在途元数据写保护(sessionPendingWrites)', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('在途字段不被旧快照冲掉,其余字段照常吃快照(review P1:重连全量对账)', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: '旧名' })]);
    const release = sessionPendingWrites.track('s1', ['title']);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { title: '新名' });
    // 重连全量拉到旧快照:title 仍是旧名,但 updatedAt 已推进
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { title: '旧名', updatedAt: '2026-01-02T00:00:00.000Z' }),
    ]);
    const row = remoteSessionStore.getSessions().find((s) => s.id === 's1');
    expect(row?.title).toBe('新名');
    expect(row?.updatedAt).toBe('2026-01-02T00:00:00.000Z');
    // overlay 藏掉了与本地不同的权威快照值 → 留差异痕,由写的结局 consume 触发 reseed
    expect(sessionPendingWrites.consumeMaskedPush('s1', ['title'])).toBe(true);
    release();
  });

  it('乐观移出的行(删除/归档在途)不随旧快照复活;release 后恢复正常对账', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const release = sessionPendingWrites.track('s1', ['status']);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { status: 'deleted' });
    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s2']);
    // 重连全量拉到旧快照(被控端还没处理删除,s1 仍在):不得复活
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s2']);
    // 写 settle 后,后续快照按正常语义对账(若被控端确认删除,新快照不含 s1)
    release();
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s2')]);
    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s2']);
  });

  it('restore 在途把行加回列表:旧快照缺失该行时不抹掉,写 settle 后恢复正常对账(review P2)', () => {
    // 归档会话 s1 不在活跃列表;详情页 restore:乐观 status=active 把行加回
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s2')]);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', { status: 'active' }));
    const release = sessionPendingWrites.track('s1', ['status']);
    // 重连全量拉到旧快照(被控端未处理 restore,s1 仍归档不在列表):不得抹掉乐观行
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s2')]);
    expect(remoteSessionStore.getSessions().map((s) => s.id).sort()).toEqual(['s1', 's2']);
    // 写 settle 后正常对账:后续快照不含 s1(如 restore 失败回滚)则移除
    release();
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s2')]);
    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s2']);
  });

  it('无在途写时全量对账语义不变', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: 'A' })]);
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: 'B' })]);
    expect(remoteSessionStore.getSessions().find((s) => s.id === 's1')?.title).toBe('B');
  });
});

describe('upsertDeviceSession 在途元数据写保护(sessionPendingWrites)', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('归档在途、行已被乐观移出:单条读回的旧行不插回;release 后恢复正常 upsert', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const release = sessionPendingWrites.track('s1', ['status']);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { status: 'archived' });
    // 会话页 getSession 慢回包:被控端还没处理归档,带回旧 status
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', { status: 'active' }));
    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s2']);
    // 失败回滚先 release 再整行插回:不被自己的在途登记挡掉
    release();
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1'));
    expect(remoteSessionStore.getSessions().map((s) => s.id).sort()).toEqual(['s1', 's2']);
  });

  it('行仍在本地:在途字段保留本地乐观值并留差异痕,其余字段照常吃读回', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { title: '旧名' })]);
    const release = sessionPendingWrites.track('s1', ['title']);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { title: '新名' });
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
      title: '旧名',
      updatedAt: '2026-01-02T00:00:00.000Z',
    }));
    const row = remoteSessionStore.getSessions().find((s) => s.id === 's1');
    expect(row?.title).toBe('新名');
    expect(row?.updatedAt).toBe('2026-01-02T00:00:00.000Z');
    expect(sessionPendingWrites.consumeMaskedPush('s1', ['title'])).toBe(true);
    release();
  });

  it('只有非 status 字段在途时,本地缺行的读回照常插入', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', []);
    const release = sessionPendingWrites.track('s1', ['title']);
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1'));
    expect(remoteSessionStore.getSessions().map((s) => s.id)).toEqual(['s1']);
    release();
  });
});

describe('resolveSessionWriteDevices', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('乐观 patch / 回滚落行所在的物理 shard,出网走规范 id 或调用方指定的路由设备', () => {
    remoteSessionStore.setDeviceSessions('dev-old', 'Mac', [session('s1')]);
    const row = { ...remoteSessionStore.getSessions().find((s) => s.id === 's1')!, canonicalDeviceId: 'dev-new' };
    expect(resolveSessionWriteDevices('s1', row)).toEqual({ rpcDeviceId: 'dev-new', shardId: 'dev-old' });
    // 会话页:出网沿用路由设备,shard 仍是物理 shard(不是路由 id)
    expect(resolveSessionWriteDevices('s1', row, 'dev-route')).toEqual({ rpcDeviceId: 'dev-route', shardId: 'dev-old' });
    // 调用方没有会话对象时按 store 索引解析
    expect(resolveSessionWriteDevices('s1', null, 'dev-route')).toEqual({ rpcDeviceId: 'dev-route', shardId: 'dev-old' });
    expect(resolveSessionWriteDevices('missing', null)).toBeNull();
    expect(resolveSessionWriteDevices('missing', null, 'dev-route')).toEqual({ rpcDeviceId: 'dev-route', shardId: 'dev-route' });
  });
});

describe('引用调和(2026-07-18 首页重渲染风暴修复)', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('setDeviceSessions:单会话变化时,其余会话保留旧对象引用', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2'), session('s3')]);
    const before = remoteSessionStore.getSessions();
    const s1Before = before.find((s) => s.id === 's1');
    const s3Before = before.find((s) => s.id === 's3');
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1'),
      session('s2', { updatedAt: '2026-01-02T00:00:00.000Z' }),
      session('s3'),
    ]);
    const after = remoteSessionStore.getSessions();
    expect(after).not.toBe(before);
    expect(after.find((s) => s.id === 's1')).toBe(s1Before);
    expect(after.find((s) => s.id === 's3')).toBe(s3Before);
    expect(after.find((s) => s.id === 's2')).not.toBe(before.find((s) => s.id === 's2'));
  });

  it('upsert 置顶重排:数组换新但内容未变的会话对象引用保留', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const before = remoteSessionStore.getSessions();
    const s1Before = before.find((s) => s.id === 's1');
    const s2Before = before.find((s) => s.id === 's2');
    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s2'));
    const after = remoteSessionStore.getSessions();
    expect(after[0]).toBe(s2Before);
    expect(after.find((s) => s.id === 's1')).toBe(s1Before);
  });

  it('设备身份归一化下,与会话内容无关的重算保留 mergedSessions 数组引用', () => {
    // 归一化分支每轮都会 {...session, canonicalDeviceId} 重铸对象,调和必须能吸收它,
    // 否则 useSyncExternalStore 快照逐 emit 换新引用,消费屏全量重渲染(风暴根因)。
    remoteSessionStore.setDeviceIdentity([{ deviceId: 'dev-1', name: 'Mac' }]);
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const before = remoteSessionStore.getSessions();
    remoteSessionStore.setDeviceIdentity([
      { deviceId: 'dev-1', name: 'Mac' },
      { deviceId: 'dev-9', name: 'Other' },
    ]);
    expect(remoteSessionStore.getSessions()).toBe(before);
  });

  it('跨设备:另一设备 shard 变化不换本设备会话的对象引用', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const s1Before = remoteSessionStore.getSessions().find((s) => s.id === 's1');
    remoteSessionStore.setDeviceSessions('dev-2', 'Win', [session('w1')]);
    expect(remoteSessionStore.getSessions().find((s) => s.id === 's1')).toBe(s1Before);
    remoteSessionStore.removeDevice('dev-2');
    expect(remoteSessionStore.getSessions().find((s) => s.id === 's1')).toBe(s1Before);
  });

  it('风暴不变量:消息/运行态/活动高频 churn + 内容等价重算下,会话列表快照引用零漂移', () => {
    // 桌面端流式输出 = appendMessage / status / activity 事件以每秒多条的频率灌入,
    // 其间还夹杂设备表更新等触发 recomputeSessions 的内容等价重算(归一化分支每轮
    // 都会重铸对象,必须被引用调和吸收)。这些都不改变会话列表内容,getSessions()
    // 快照必须保持同一引用——它是 useRemoteSessions 消费屏(首页/设备详情)不被
    // 无关 emit 惊动的结构保证,重构后回归验证的核心断言(2026-07-18 风暴修复;
    // 每轮 setDeviceIdentity 在修复前的 store 上会真实打红本用例)。
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1'), session('s2')]);
    const snapshot = remoteSessionStore.getSessions();
    for (let i = 0; i < 50; i += 1) {
      remoteSessionStore.appendMessage('s1', {
        id: `m${i}`,
        clientId: `m${i}`,
        sessionId: 's1',
        role: 'assistant',
        content: `chunk ${i}`,
        toolUseId: null,
        agentMeta: null,
        createdAt: `2026-01-01T00:00:${String(i).padStart(2, '0')}.000Z`,
      });
      remoteSessionStore.applyMakerEvent('s1', { type: 'status', data: { isRunning: true, tokenUsage: i + 1 } });
      remoteSessionStore.applySessionActivity('dev-1', {
        sessionId: 's1',
        phase: 'running',
        compactDetail: `step ${i}`,
      });
      // 每轮设备表变化(无关设备增补)强制走一次 recomputeSessions:会话内容等价,
      // 归一化重铸的对象必须被调和吸收,快照引用不许漂移。
      remoteSessionStore.setDeviceIdentity([
        { deviceId: 'dev-1', name: 'Mac' },
        { deviceId: `ghost-${i}`, name: `G${i}` },
      ]);
    }
    expect(remoteSessionStore.getSessions()).toBe(snapshot);
  });
});

describe('maker:event 微批拆包(CONTROLLER_CAPABILITY_MAKER_EVENT_BATCH_V1)', () => {
  it('批内事件按序走与逐帧完全相同的路径:流式增量拼接结果一致', () => {
    vi.useFakeTimers();
    try {
      // 逐帧基线
      pushMakerText('s-single', 'p-1', 'hello', false);
      pushMakerText('s-single', 'p-1', ' world', false);
      vi.runOnlyPendingTimers();
      const single = remoteSessionStore.getMessages('s-single');

      // 同样两条事件,这次由被控端合并成一帧微批下发
      remoteSessionStore.applyRemotePush('dev-1', MAKER_EVENT_BATCH_CHANNEL, {
        sessionId: 's-batch',
        events: [
          { sessionId: 's-batch', persistId: 'p-1', event: { type: 'text', data: { text: 'hello', isFinal: false } } },
          { sessionId: 's-batch', persistId: 'p-1', event: { type: 'text', data: { text: ' world', isFinal: false } } },
        ],
      });
      vi.runOnlyPendingTimers();
      const batched = remoteSessionStore.getMessages('s-batch');

      expect(batched).toHaveLength(1);
      expect(batched[0]).toMatchObject({ id: 'p-1', role: 'assistant', content: 'hello world' });
      // 与逐帧结果逐字段一致(仅 sessionId 不同)
      expect(batched[0]!.content).toBe(single[0]!.content);
    } finally {
      vi.useRealTimers();
    }
  });

  it('形状不符的批帧整体忽略;批内单条坏事件只跳过该条', () => {
    vi.useFakeTimers();
    try {
      // 缺 events / events 为空 / 非数组:整帧忽略,不抛
      for (const bad of [
        { sessionId: 's-bad' },
        { sessionId: 's-bad', events: [] },
        { sessionId: 's-bad', events: 'nope' },
        { events: [{}] },
        null,
      ]) {
        expect(() =>
          remoteSessionStore.applyRemotePush('dev-1', MAKER_EVENT_BATCH_CHANNEL, bad),
        ).not.toThrow();
      }
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s-bad')).toHaveLength(0);

      // 批内混入坏条目:好的照常生效
      remoteSessionStore.applyRemotePush('dev-1', MAKER_EVENT_BATCH_CHANNEL, {
        sessionId: 's-mixed',
        events: [
          'not-an-object',
          { sessionId: 's-mixed', persistId: 'p-9', event: { type: 'text', data: { text: 'ok', isFinal: true } } },
        ],
      });
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s-mixed')).toHaveLength(1);
      expect(remoteSessionStore.getMessages('s-mixed')[0]).toMatchObject({ content: 'ok' });
    } finally {
      vi.useRealTimers();
    }
  });

  it('批内 sessionId 与顶层不一致的条目被丢弃:不绕过 topic 隔离', () => {
    // topic 路由只按**顶层** sessionId,批内混入其它会话的事件会把本端未订阅的
    // 会话数据投进来(坏帧/恶意帧场景)。fail-closed 跳过该条,不整批丢。
    vi.useFakeTimers();
    try {
      remoteSessionStore.applyRemotePush('dev-1', MAKER_EVENT_BATCH_CHANNEL, {
        sessionId: 's-own',
        events: [
          { sessionId: 's-other', persistId: 'p-x', event: { type: 'text', data: { text: 'leak', isFinal: true } } },
          { sessionId: 's-own', persistId: 'p-y', event: { type: 'text', data: { text: 'mine', isFinal: true } } },
        ],
      });
      vi.runOnlyPendingTimers();
      expect(remoteSessionStore.getMessages('s-other')).toHaveLength(0);
      expect(remoteSessionStore.getMessages('s-own')).toHaveLength(1);
      expect(remoteSessionStore.getMessages('s-own')[0]).toMatchObject({ content: 'mine' });
    } finally {
      vi.useRealTimers();
    }
  });
});

describe('任务消息内存治理', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('补读结果区分已接受的相同窗口与失效代际或过旧窗口', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
    const first = remoteSessionStore.enterSessionMessageDetail('s1');
    const latest = [messageAt('latest', 's1', '2026-09-08T00:00:00.000Z')];
    expect(remoteSessionStore.setLatestMessageWindow('s1', latest, { authority: first })).toBe(true);
    expect(remoteSessionStore.setLatestMessageWindow('s1', latest, { authority: first })).toBe(true);
    expect(remoteSessionStore.setLatestMessageWindow('s1', [
      messageAt('old', 's1', '2026-09-07T00:00:00.000Z'),
    ], { authority: first })).toBe(false);
    remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', first);
    remoteSessionStore.enterSessionMessageDetail('s1');
    expect(remoteSessionStore.setLatestMessageWindow('s1', latest, { authority: first })).toBe(false);
    expect(remoteSessionStore.getMessages('s1').map((row) => row.id)).toEqual(['latest']);
  });

  const manyMessages = (sessionId: string, count: number): RemoteMessage[] =>
    Array.from({ length: count }, (_, index) => messageAt(
      `${sessionId}-m-${index}`,
      sessionId,
      new Date(Date.UTC(2026, 0, 1, 0, 0, index)).toISOString(),
    ));

  const flushReclaim = async (): Promise<void> => {
    await Promise.resolve();
    await Promise.resolve();
  };

  it('只按 source=scheduler 分类,标题与 source 缺失均保守按 regular', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('schedule', { source: 'scheduler', title: '普通标题' }),
      session('legacy-title', { title: '[Schedule] 旧标题' }),
      session('bound', { source: 'user', title: '被定时器绑定' }),
    ]);

    expect(remoteSessionStore.getSessionRetention('schedule')).toBe('schedule');
    expect(remoteSessionStore.getSessionRetention('legacy-title')).toBe('regular');
    expect(remoteSessionStore.getSessionRetention('bound')).toBe('regular');
  });

  it('keeps retention lookup current across patch, shard replacement, removal and ID reuse', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.applySessionPatch('dev-1', 's1', { source: 'scheduler' });
    remoteSessionStore.applySessionPatch('dev-1', 's1', { totalCostUsd: 5 });
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
    remoteSessionStore.applySessionPatch('dev-1', 's1', { source: 'scheduler' });
    remoteSessionStore.removeDevice('dev-1');
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
    remoteSessionStore.setDeviceSessions('dev-2', 'PC', [session('s1', { source: 'scheduler' })]);
    remoteSessionStore.clear();
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
    remoteSessionStore.setDeviceSessions('dev-2', 'PC', [session('s1')]);
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
  });

  it('旧详情代际的读取在 blur→refocus 后不能覆盖新窗口', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
    const first = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', [message('first', 's1')], { authority: first });
    remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', first);
    const second = remoteSessionStore.enterSessionMessageDetail('s1');

    remoteSessionStore.setMessages('s1', [message('stale', 's1')], { authority: first });
    expect(remoteSessionStore.getMessages('s1').map((row) => row.id)).toEqual(['first']);

    remoteSessionStore.setMessages('s1', [message('fresh', 's1')], { authority: second });
    expect(remoteSessionStore.getMessages('s1').map((row) => row.id)).toEqual(['fresh']);
  });

  it('从未打开的 regular 可更新全局镜像,首次进入或离场后不再接受无 authority 补读', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const unenteredAuthority = remoteSessionStore.captureUnenteredSessionMessageAuthority('s1');
    expect(remoteSessionStore.hasSessionMessageDetailEntered('s1')).toBe(false);
    expect(remoteSessionStore.canCommitUnenteredSessionMessageWindow(unenteredAuthority, 'dev-1')).toBe(true);
    expect(remoteSessionStore.canCommitUnenteredSessionMessageWindow(unenteredAuthority, 'dev-2')).toBe(false);

    remoteSessionStore.setLatestMessageWindow('s1', [message('global-mirror', 's1')]);
    expect(remoteSessionStore.getMessages('s1').map((row) => row.id)).toEqual(['global-mirror']);

    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    expect(remoteSessionStore.hasSessionMessageDetailEntered('s1')).toBe(true);
    expect(remoteSessionStore.canCommitUnenteredSessionMessageWindow(unenteredAuthority, 'dev-1')).toBe(false);
    remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
    remoteSessionStore.setLatestMessageWindow('s1', [message('stale-reconnect', 's1')]);

    expect(remoteSessionStore.getMessages('s1').some((row) => row.id === 'stale-reconnect')).toBe(false);
  });

  it('从未打开的 schedule 不接受无 authority 的重连补读', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [
      session('s1', { source: 'scheduler' }),
    ]);

    const unenteredAuthority = remoteSessionStore.captureUnenteredSessionMessageAuthority('s1');
    expect(remoteSessionStore.hasSessionMessageDetailEntered('s1')).toBe(false);
    expect(remoteSessionStore.canCommitUnenteredSessionMessageWindow(unenteredAuthority, 'dev-1')).toBe(false);
    remoteSessionStore.setLatestMessageWindow('s1', [message('schedule-reconnect', 's1')]);
    expect(remoteSessionStore.getMessages('s1')).toEqual([]);
  });

  it('clear 后同设备同任务重建也拒绝 reset 前的未进入详情读取', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const beforeReset = remoteSessionStore.captureUnenteredSessionMessageAuthority('s1');

    remoteSessionStore.clear();
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);

    expect(remoteSessionStore.canCommitUnenteredSessionMessageWindow(beforeReset, 'dev-1')).toBe(false);
    const afterReset = remoteSessionStore.captureUnenteredSessionMessageAuthority('s1');
    expect(remoteSessionStore.canCommitUnenteredSessionMessageWindow(afterReset, 'dev-1')).toBe(true);
  });

  it('打开过的 schedule 离场后与 regular 一致:压回单窗而不是清空', async () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
    remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });

    remoteSessionStore.leaveSessionMessageDetail('s1', 'app-background', authority);
    await flushReclaim();
    const rows = remoteSessionStore.getMessages('s1');
    expect(rows).toHaveLength(80);
    expect(rows.at(-1)?.id).toBe('s1-m-99');
  });

  it.each(['deleted', 'archived'] as const)('%s 后同 id 重新出现的 schedule 不沿用旧的已打开标记', (status) => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
    remoteSessionStore.enterSessionMessageDetail('s1');
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');

    remoteSessionStore.applySessionPatch('dev-1', 's1', { status });
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
  });

  it('移除设备后同 id 重新出现的 schedule 不沿用旧的已打开标记', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
    remoteSessionStore.enterSessionMessageDetail('s1');

    remoteSessionStore.removeDevice('dev-1');
    remoteSessionStore.setDeviceSessions('dev-2', 'PC', [session('s1', { source: 'scheduler' })]);
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
  });

  it('从未打开的 schedule 不驻留推送来的完整正文', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', { source: 'scheduler' })]);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1',
      message: message('unopened-push', 's1'),
    });
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('schedule');
    expect(remoteSessionStore.getMessages('s1')).toEqual([]);
  });

  it('regular 离场后旧订阅 push 与尚未 flush 的流式批次都不能复活窗口', async () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
      const authority = remoteSessionStore.enterSessionMessageDetail('s1');
      remoteSessionStore.setMessages('s1', manyMessages('s1', 2), { authority });
      pushMakerText('s1', 'stream-1', 'queued delta', false);

      remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
      const nextAuthority = remoteSessionStore.enterSessionMessageDetail('s1');
      vi.runOnlyPendingTimers();
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1',
        message: message('late-subscription-push', 's1'),
      });

      expect(remoteSessionStore.isSessionMessageAuthorityCurrent(nextAuthority)).toBe(true);
      expect(remoteSessionStore.getMessages('s1').map((row) => row.id)).toEqual([
        's1-m-0',
        'late-subscription-push',
        's1-m-1',
      ]);
      // 上面的 push 是在新代际可见期间到达,应该保留;旧代际排队的 delta 不得出现。
      expect(remoteSessionStore.getMessages('s1').some((row) => row.id === 'stream-1')).toBe(false);

      remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', nextAuthority);
      await flushReclaim();
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1',
        message: message('push-after-leave', 's1'),
      });
      expect(remoteSessionStore.getMessages('s1').some((row) => row.id === 'push-after-leave')).toBe(false);
    } finally {
      vi.useRealTimers();
    }
  });

  it('页面卸载后本地工作排空仍会完成 deferred reclaim', async () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const work = remoteSessionStore.acquireSessionMessageWork('s1', true);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });
    remoteSessionStore.leaveSessionMessageDetail('s1', 'session-switch', authority);

    await flushReclaim();
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(100);
    work.release();
    await flushReclaim();
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(80);
  });

  it('pending queue 暂缓回收,queue 排空后由 store 主动补回收', async () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });
    remoteSessionStore.setInputProjection('s1', projection('s1'));
    remoteSessionStore.leaveSessionMessageDetail('s1', 'app-background', authority);

    await flushReclaim();
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(100);
    remoteSessionStore.setInputProjection('s1', {
      ...projection('s1'),
      pendingQueue: [],
    });
    await flushReclaim();
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(80);
  });

  it('regular 失焦压回单窗,同时保留窗口外的本地系统卡', async () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    const localCard = {
      ...messageAt('mobile-system-pwd-old', 's1', '2025-12-31T23:59:59.000Z'),
      role: 'system' as const,
    };
    remoteSessionStore.setMessages('s1', [localCard, ...manyMessages('s1', 100)], { authority });
    remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
    await flushReclaim();

    const rows = remoteSessionStore.getMessages('s1');
    expect(rows).toHaveLength(80);
    expect(rows.some((row) => row.id === localCard.id)).toBe(true);
    expect(rows.at(-1)?.id).toBe('s1-m-99');
  });

  it('后台运行中的 regular 任务持续收到列表推送,内存窗口不超过一页', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.setSessionRunning('s1', true);
    const pushed = manyMessages('s1', 300);
    for (const row of pushed) {
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', listMessage: true, message: row,
      });
    }
    const rows = remoteSessionStore.getMessages('s1');
    expect(rows).toHaveLength(80);
    expect(rows.at(-1)?.id).toBe('s1-m-299');
  });

  it('后台单窗上限只裁可重取的行,保留本地系统卡', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.setSessionRunning('s1', true);
    const localCard = {
      ...messageAt('mobile-system-local-only', 's1', '2025-12-31T23:59:59.000Z'),
      role: 'system' as const,
    };
    remoteSessionStore.setMessages('s1', [localCard]);
    for (const row of manyMessages('s1', 200)) {
      remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
        sessionId: 's1', listMessage: true, message: row,
      });
    }
    const rows = remoteSessionStore.getMessages('s1');
    expect(rows).toHaveLength(80);
    expect(rows.some((row) => row.id === localCard.id)).toBe(true);
    expect(rows.at(-1)?.id).toBe('s1-m-199');
  });

  it('正在看的 regular 任务不受后台单窗上限影响', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    remoteSessionStore.setSessionRunning('s1', true);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', manyMessages('s1', 120), { authority });
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1', listMessage: true, message: messageAt('s1-new', 's1', '2026-01-01T01:00:00.000Z'),
    });
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(121);
  });

  it('regular 全局 LRU 不淘汰当前详情,且总量压回约 800 条', () => {
    // 后台任务各自封顶一页(80),要 11 个以上后台任务才会触发全局淘汰。
    const sessions = Array.from({ length: 13 }, (_, index) => session(`s${index}`));
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', sessions);
    remoteSessionStore.enterSessionMessageDetail('s0');
    for (const item of sessions) {
      remoteSessionStore.setMessages(item.id, manyMessages(item.id, 100));
    }

    const total = sessions.reduce(
      (sum, item) => sum + remoteSessionStore.getMessages(item.id).length,
      0,
    );
    expect(total).toBeLessThanOrEqual(800);
    expect(remoteSessionStore.getMessages('s0')).toHaveLength(100);
    expect(sessions.slice(1).some((item) => remoteSessionStore.getMessages(item.id).length === 0)).toBe(true);
  });

  it('notifies an evicted session preview subscriber when another session exceeds the LRU budget', () => {
    const sessions = Array.from({ length: 13 }, (_, index) => session(`s${index}`));
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', sessions);
    const previewChanged = vi.fn();
    const unsubscribe = remoteSessionStore.subscribeSessionMessagePreview('s0', previewChanged);
    try {
      remoteSessionStore.setMessages('s0', manyMessages('s0', 80));
      expect(remoteSessionStore.getSessionMessagePreview('s0')).toBeDefined();
      previewChanged.mockClear();

      for (const item of sessions.slice(1)) {
        remoteSessionStore.setMessages(item.id, manyMessages(item.id, 80));
      }

      expect(remoteSessionStore.getMessages('s0')).toEqual([]);
      expect(previewChanged).toHaveBeenCalledTimes(1);
      expect(remoteSessionStore.getSessionMessagePreview('s0')).toBeUndefined();
    } finally {
      unsubscribe();
    }
  });

  it('regular 字节 LRU 会计入深层容器中的大字符串', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s0'), session('s1')]);
    const currentAuthority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', [message('current', 's1')], { authority: currentAuthority });

    // 同一字符串引用复用四次,避免测试本身额外分配 72 MiB;逻辑 payload 序列化后
    // 仍会产生四份内容。旧 depth=3 截断会在 chunks 外层直接按 64 bytes 低估。
    const chunk = 'x'.repeat(9 * 1024 * 1024);
    const deepPayload = {
      level1: {
        level2: {
          level3: {
            chunks: [chunk, chunk, chunk, chunk],
          },
        },
      },
    };
    remoteSessionStore.setMessages('s0', [{
      ...message('deep', 's0'),
      content: deepPayload,
    }]);

    expect(remoteSessionStore.getMessages('s0')).toEqual([]);
    expect(remoteSessionStore.getMessages('s1').map((row) => row.id)).toEqual(['current']);
  });

  it('regular LRU 只淘汰可重取正文,不丢尚未落盘的本地系统卡', () => {
    const sessions = Array.from({ length: 13 }, (_, index) => session(`s${index}`));
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', sessions);
    const localCard = {
      ...messageAt('mobile-system-local-only', 's0', '2025-12-31T23:59:59.000Z'),
      role: 'system' as const,
    };
    remoteSessionStore.setMessages('s0', [localCard, ...manyMessages('s0', 79)]);
    for (const item of sessions.slice(1)) {
      remoteSessionStore.setMessages(item.id, manyMessages(item.id, 80));
    }

    expect(remoteSessionStore.getMessages('s0')).toHaveLength(80);
    expect(remoteSessionStore.getMessages('s0').some((row) => row.id === localCard.id)).toBe(true);
  });

  it('regular 离场释放详情投影并拒绝离场前启动的旧投影查询', async () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', [message('m1', 's1')], { authority });
    remoteSessionStore.setInputProjection('s1', {
      ...projection('s1'),
      pendingQueue: [],
    });
    const queryEpoch = remoteSessionStore.captureInputProjectionAuthorityEpoch('s1');

    remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
    await flushReclaim();

    expect(remoteSessionStore.getInputProjection('s1').pendingQueue).toEqual([]);
    expect(remoteSessionStore.setInputProjectionIfCurrent(
      's1',
      projection('s1'),
      queryEpoch,
    )).toBe(false);
  });

  it('source 晚到为 scheduler:已打开的详情仍按 regular 驻留', async () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', manyMessages('s1', 100), { authority });

    remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', { source: 'scheduler' }));
    expect(remoteSessionStore.getSessionRetention('s1')).toBe('regular');
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(100);

    remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
    await flushReclaim();
    expect(remoteSessionStore.getMessages('s1')).toHaveLength(80);
  });

  it('归档会话会撤销旧 authority,并拒绝迟到 push 复活正文', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', [message('before-archive', 's1')], { authority });

    remoteSessionStore.applySessionPatch('dev-1', 's1', { status: 'archived' });
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', {
      sessionId: 's1',
      message: message('late-after-archive', 's1'),
    });

    expect(remoteSessionStore.isSessionMessageAuthorityCurrent(authority)).toBe(false);
    expect(remoteSessionStore.getMessages('s1')).toEqual([]);
  });

  it('显式失效 rewind 窗口会清正文、sync marker 并登记刷新', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1')]);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.setMessages('s1', [message('before-rewind', 's1')], { authority });
    const row = remoteSessionStore.getSessions().find((item) => item.id === 's1')!;
    remoteSessionStore.markSessionMessagesSynced('s1', row);

    remoteSessionStore.invalidateSessionMessageWindow('s1', 'dev-1');

    expect(remoteSessionStore.getMessages('s1')).toEqual([]);
    expect(remoteSessionStore.isSessionMessageWindowSynced('s1', row)).toBe(false);
    expect(remoteSessionStore.hasPendingRefresh('s1')).toBe(true);
    expect(remoteSessionStore.isSessionMessageAuthorityCurrent(authority)).toBe(true);
  });
});

describe('device-clock live row clamp (applyRemoteTextEvent createdAt, cross-clock-domain sort fix)', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('clampLiveRowCreatedAt: 无既有基准时原样返回设备时间', () => {
    expect(clampLiveRowCreatedAt('2026-01-01T00:00:05.000Z', undefined))
      .toBe('2026-01-01T00:00:05.000Z');
  });

  it('clampLiveRowCreatedAt: 设备时间领先于既有基准 → 锚定既有基准,不让快设备时钟支配后续主机行', () => {
    expect(clampLiveRowCreatedAt('2026-01-01T00:00:05.000Z', '2026-01-01T00:00:01.000Z'))
      .toBe('2026-01-01T00:00:01.000Z');
  });

  it('clampLiveRowCreatedAt: 设备时间与既有基准相同 → 原样返回(打平,交给 compareMessageOrder 的 rowid/到达序兜底)', () => {
    expect(clampLiveRowCreatedAt('2026-01-01T00:00:05.000Z', '2026-01-01T00:00:05.000Z'))
      .toBe('2026-01-01T00:00:05.000Z');
  });

  it('clampLiveRowCreatedAt: 设备时间落后于既有基准 → 钳制为既有基准本身,不发明 +1ms', () => {
    expect(clampLiveRowCreatedAt('2026-01-01T00:00:01.000Z', '2026-01-01T00:00:05.000Z'))
      .toBe('2026-01-01T00:00:05.000Z');
  });

  it('设备时钟落后会话已知最新行时,新建的 live 行不再被排到该行之前(跨时钟域错位的修复现场)', () => {
    vi.useFakeTimers();
    try {
      // 会话里已有一条 createdAt 更新的行(可能是刚持久化的用户消息,也可能是更早一次
      // 已经落定的 live 行),随后设备本地时钟给出的「现在」比它更旧 —— 这正是本 bug 的
      // 跨时钟域场景(不论具体是设备落后还是设备超前,症状同源:新行的设备戳不保证
      // ≥ 会话已知最新行,可能被排到它前面,`getMessages` 尾部就不是最新内容)。
      remoteSessionStore.setLatestMessageWindow('s1', [
        messageAt('user-sent', 's1', '2026-01-01T00:10:00.000Z'),
      ]);
      vi.setSystemTime(new Date('2026-01-01T00:05:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'streaming reply', true);

      const rows = remoteSessionStore.getMessages('s1');
      // 钳制后,新 live 行的 createdAt 被拉到「已知最新行」自身(打平),稳定排序下
      // 仍落在其后 —— 不再被排到 user-sent 前面(修复前会因为设备戳更早而插到它前面,
      // 尾部就会显示旧内容而不是刚发生的这条)。
      expect(rows.map((item) => item.clientId)).toEqual(['user-sent', 'live-assistant']);
      expect(rows.find((item) => item.clientId === 'live-assistant')?.createdAt)
        .toBe('2026-01-01T00:10:00.000Z');
    } finally {
      vi.useRealTimers();
    }
  });

  it('设备时钟快于主机时,后续主机持久化消息仍能排到旧 live 行之后', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('previous-user', 's1', '2026-01-01T00:00:00.000Z'),
          role: 'user',
        },
      ]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'stale-live-assistant', 'previous streaming reply', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('new-user', 's1', '2026-01-01T00:05:00.000Z'),
        role: 'user',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'previous-user',
        'stale-live-assistant',
        'new-user',
      ]);
      expect(rows.find((item) => item.clientId === 'stale-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:00:00.000Z');
    } finally {
      vi.useRealTimers();
    }
  });

  it('会话 userSendAt 已越过旧 user 尾行时,当前 live 回复仍等待本轮 user push 重锚', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      })]);
      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('previous-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Previous question',
      }]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'current-live-assistant', 'Current reply', true);

      expect(remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'current-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:00:02.000Z');

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'previous-user',
        'current-user',
        'current-live-assistant',
      ]);
      expect(rows.slice(1).map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:02.000Z',
        '2026-01-01T00:00:02.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('空消息窗先用会话 userSendAt 临时锚定短 live 行,再由权威消息完成重锚', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:00.000Z',
        updatedAt: '2026-01-01T00:00:00.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', undefined, 'OK', true);

      expect(remoteSessionStore.getMessages('s1')[0]?.createdAt)
        .toBe('2026-01-01T00:00:00.000Z');

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('persisted-short', 's1', '2026-01-01T00:00:01.000Z'),
        content: 'OK',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows).toHaveLength(2);
      expect(rows[0]?.clientId).toMatch(/^mobile-stream-/);
      expect(rows[0]?.createdAt).toBe('2026-01-01T00:00:01.000Z');
      expect(rows[1]?.clientId).toBe('persisted-short');
    } finally {
      vi.useRealTimers();
    }
  });

  it('首个短 live 行早于会话元数据时,元数据到达后把设备时间重锚到主机时间域', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', undefined, 'OK', true);

      expect(remoteSessionStore.getMessages('s1')[0]?.createdAt)
        .toBe('2026-01-01T00:10:00.000Z');

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:00.000Z',
        updatedAt: '2026-01-01T00:00:00.000Z',
      }));

      expect(remoteSessionStore.getMessages('s1')[0]?.createdAt)
        .toBe('2026-01-01T00:00:00.000Z');

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('persisted-short', 's1', '2026-01-01T00:00:01.000Z'),
        content: 'OK',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows).toHaveLength(2);
      expect(rows[0]?.clientId).toMatch(/^mobile-stream-/);
      expect(rows[0]?.createdAt).toBe('2026-01-01T00:00:01.000Z');
      expect(rows[1]?.clientId).toBe('persisted-short');
    } finally {
      vi.useRealTimers();
    }
  });

  it('旧会话快照只做临时重锚,后续权威 user push 仍会恢复问题先于 live 回复', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'OK', true);

      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:00.000Z',
        updatedAt: '2026-01-01T00:00:00.000Z',
      })]);

      expect(remoteSessionStore.getMessages('s1')[0]?.createdAt)
        .toBe('2026-01-01T00:00:00.000Z');

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('persisted-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'persisted-user',
        'live-assistant',
      ]);
      expect(rows.map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('只有旧会话快照水位时,随后创建的 live 行也会等待权威消息重锚', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:00.000Z',
        updatedAt: '2026-01-01T00:00:00.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'OK', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('persisted-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'persisted-user',
        'live-assistant',
      ]);
      expect(rows.map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('发送前发起的旧消息窗口迟到时不消费 live 行待重锚身份', () => {
    vi.useFakeTimers();
    try {
      // 此时发送前的 history 请求已经发出,但本地窗口尚未拿到任何主机时间水位。
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'Current reply', true);

      // 这份窗口在发送前已经开始读取,返回时不含本轮 user 行。它可以临时把 live
      // 行拉回主机时间域,但不能消费待重锚身份;否则后续权威 user push 无法恢复顺序。
      remoteSessionStore.setLatestMessageWindow('s1', [
        messageAt('previous-assistant', 's1', '2026-01-01T00:00:00.000Z'),
      ]);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('current-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Current question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'previous-assistant',
        'current-user',
        'live-assistant',
      ]);
      expect(rows.slice(1).map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('非空会话的旧 assistant 尾行不能让当前 live 回复失去待重锚资格', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setLatestMessageWindow('s1', [
        messageAt('previous-assistant', 's1', '2026-01-01T00:00:00.000Z'),
      ]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'current-live-assistant', 'Current reply', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('current-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Current question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'previous-assistant',
        'current-user',
        'current-live-assistant',
      ]);
      expect(rows.slice(1).map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('连续两轮 provisional live 回复按 user push 顺序逐条完成重锚', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);
      vi.setSystemTime(new Date('2026-01-01T00:11:00.000Z'));
      pushMakerText('s1', 'live-assistant-2', 'Second reply', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'First question',
      });
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Second question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'user-2',
        'live-assistant-2',
      ]);
      expect(rows.map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:02.000Z',
        '2026-01-01T00:00:02.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('同一发送轮次的多条 live 回复按原顺序整体移到 user 行之后', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:01.000Z',
        updatedAt: '2026-01-01T00:00:01.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply block', true);
      pushMakerText('s1', 'live-assistant-2', 'Second reply block', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Question',
      });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'live-assistant-2',
      ]);
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Next question',
      });
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'live-assistant-2',
        'user-2',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('元数据前同一 maker turn 的多条 live 回复由首个 user 行整体消费', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: true },
      });
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply block', true);
      pushMakerText('s1', 'live-assistant-2', 'Second reply block', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Question',
      });
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'live-assistant-2',
      ]);

      remoteSessionStore.applyMakerEvent('s1', {
        type: 'status',
        data: { isRunning: false },
      });
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Next question',
      });
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'live-assistant-2',
        'user-2',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('延迟的非 user push 不会消费当前轮 live 回复的待重锚身份', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'current-live-assistant', 'Current reply', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('delayed-previous-assistant', 's1', '2026-01-01T00:00:01.000Z'),
        content: 'Delayed previous reply',
      });
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'delayed-previous-assistant',
        'current-user',
        'current-live-assistant',
      ]);
      expect(rows.slice(1).map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:02.000Z',
        '2026-01-01T00:00:02.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('早于本轮发送时间的 user push 不会消费当前轮 live 回复的待重锚身份', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'current-live-assistant', 'Current reply', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('delayed-previous-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Previous question',
      });
      expect(remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'current-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:00:02.000Z');
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'delayed-previous-user',
        'current-user',
        'current-live-assistant',
      ]);
      expect(rows.slice(1).map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:02.000Z',
        '2026-01-01T00:00:02.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('匹配本轮发送时间的 user 最新窗口会完成配对,下一轮 user 不再认领旧回复', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:01.000Z',
        updatedAt: '2026-01-01T00:00:01.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);

      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'First question',
      }]);
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Second question',
      });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'user-2',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('一次权威窗口带回多轮 user 时,按窗口顺序逐条配对 provisional 回复', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);
      vi.setSystemTime(new Date('2026-01-01T00:11:00.000Z'));
      pushMakerText('s1', 'live-assistant-2', 'Second reply', true);

      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
          role: 'user',
          content: 'First question',
        },
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
      ]);

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'user-2',
        'live-assistant-2',
      ]);
      expect(rows.map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:02.000Z',
        '2026-01-01T00:00:02.000Z',
      ]);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-3', 's1', '2026-01-01T00:00:03.000Z'),
        role: 'user',
        content: 'Third question',
      });
      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'user-1',
        'live-assistant-1',
        'user-2',
        'live-assistant-2',
        'user-3',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('截断权威窗口只配对窗口内对应的最新 provisional 回复', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:03.000Z',
        updatedAt: '2026-01-01T00:00:03.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);
      pushMakerText('s1', 'live-assistant-2', 'Second reply', true);
      pushMakerText('s1', 'live-assistant-3', 'Third reply', true);

      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
        {
          ...messageAt('user-3', 's1', '2026-01-01T00:00:03.000Z'),
          role: 'user',
          content: 'Third question',
        },
      ]);

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:04.000Z',
        updatedAt: '2026-01-01T00:00:04.000Z',
      }));
      pushMakerText('s1', 'live-assistant-4', 'Fourth reply', true);
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-4', 's1', '2026-01-01T00:00:04.000Z'),
        role: 'user',
        content: 'Fourth question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      const rowIds = rows.map((item) => item.clientId);
      expect(rowIds.indexOf('live-assistant-2')).toBe(rowIds.indexOf('user-2') + 1);
      expect(rowIds.indexOf('live-assistant-3')).toBe(rowIds.indexOf('user-3') + 1);
      expect(rowIds.indexOf('live-assistant-4')).toBe(rowIds.indexOf('user-4') + 1);
      expect(rowIds.indexOf('live-assistant-1')).toBeLessThan(rowIds.indexOf('user-4'));
      expect(rows.find((item) => item.clientId === 'live-assistant-2')?.createdAt)
        .toBe('2026-01-01T00:00:02.000Z');
      expect(rows.find((item) => item.clientId === 'live-assistant-3')?.createdAt)
        .toBe('2026-01-01T00:00:03.000Z');
      expect(rows.find((item) => item.clientId === 'live-assistant-4')?.createdAt)
        .toBe('2026-01-01T00:00:04.000Z');
    } finally {
      vi.useRealTimers();
    }
  });

  it('截断窗口中的中间 user 边界不会认领更旧轮次的 pending 回复', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:01.000Z',
        updatedAt: '2026-01-01T00:00:01.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      }));
      pushMakerText('s1', 'live-assistant-2', 'Second reply', true);
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Second question',
      });

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:03.000Z',
        updatedAt: '2026-01-01T00:00:03.000Z',
      }));
      pushMakerText('s1', 'live-assistant-3', 'Third reply', true);
      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
        {
          ...messageAt('user-3', 's1', '2026-01-01T00:00:03.000Z'),
          role: 'user',
          content: 'Third question',
        },
      ]);

      const truncatedRows = remoteSessionStore.getMessages('s1');
      expect(truncatedRows.find((item) => item.clientId === 'live-assistant-1')?.createdAt)
        .toBe('2026-01-01T00:00:01.000Z');
      expect(truncatedRows.map((item) => item.clientId).indexOf('live-assistant-1'))
        .toBeLessThan(truncatedRows.map((item) => item.clientId).indexOf('user-2'));

      remoteSessionStore.setLatestMessageWindow('s1', [
        {
          ...messageAt('user-1', 's1', '2026-01-01T00:00:01.000Z'),
          role: 'user',
          content: 'First question',
        },
        {
          ...messageAt('user-2', 's1', '2026-01-01T00:00:02.000Z'),
          role: 'user',
          content: 'Second question',
        },
        {
          ...messageAt('user-3', 's1', '2026-01-01T00:00:03.000Z'),
          role: 'user',
          content: 'Third question',
        },
      ]);
      const completeRowIds = remoteSessionStore.getMessages('s1').map((item) => item.clientId);
      expect(completeRowIds.indexOf('live-assistant-1'))
        .toBe(completeRowIds.indexOf('user-1') + 1);
      expect(remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'live-assistant-1')?.createdAt)
        .toBe('2026-01-01T00:00:01.000Z');
    } finally {
      vi.useRealTimers();
    }
  });

  it('早于本轮发送时间的 user 最新窗口不改写当前 live 锚点,仍等待当前 user push 完成配对', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setDeviceSessions('dev-1', 'Mac', [session('s1', {
        userSendAt: '2026-01-01T00:00:02.000Z',
        updatedAt: '2026-01-01T00:00:02.000Z',
      })]);
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'current-live-assistant', 'Current reply', true);

      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('previous-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Previous question',
      }]);
      expect(remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'current-live-assistant')?.createdAt)
        .toBe('2026-01-01T00:00:02.000Z');
      remoteSessionStore.appendMessage('s1', {
        ...messageAt('current-user', 's1', '2026-01-01T00:00:02.000Z'),
        role: 'user',
        content: 'Current question',
      });

      expect(remoteSessionStore.getMessages('s1').map((item) => item.clientId)).toEqual([
        'previous-user',
        'current-user',
        'current-live-assistant',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('多帧 live 行在会话元数据到达前持续保留待重锚标记', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', undefined, 'O', false);
      vi.runOnlyPendingTimers();
      const provisionalCreatedAt = remoteSessionStore.getMessages('s1')[0]?.createdAt;
      pushMakerText('s1', undefined, 'K', true);

      expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
        content: 'OK',
        createdAt: provisionalCreatedAt,
      });

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:00.000Z',
        updatedAt: '2026-01-01T00:00:00.000Z',
      }));

      expect(remoteSessionStore.getMessages('s1')[0]).toMatchObject({
        content: 'OK',
        createdAt: '2026-01-01T00:00:00.000Z',
      });
    } finally {
      vi.useRealTimers();
    }
  });

  it('多条 distinct live 行在首个主机水位到达前都保持待重锚', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', 'live-assistant-1', 'First reply', true);
      vi.setSystemTime(new Date('2026-01-01T00:11:00.000Z'));
      pushMakerText('s1', 'live-assistant-2', 'Second reply', true);

      expect(remoteSessionStore.getMessages('s1').map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:10:00.000Z',
        '2026-01-01T00:11:00.000Z',
      ]);

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:00.000Z',
        updatedAt: '2026-01-01T00:00:00.000Z',
      }));

      expect(remoteSessionStore.getMessages('s1').map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:00.000Z',
        '2026-01-01T00:00:00.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('本地 system card 不会被当成首条 live 行的主机时间水位', () => {
    vi.useFakeTimers();
    try {
      const localCardId = remoteSessionStore.appendLocalSystemCard(
        's1',
        'status',
        {},
        new Date('2026-01-01T00:10:00.000Z'),
      );
      vi.setSystemTime(new Date('2026-01-01T00:11:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'Reply after local card', true);

      expect(remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'live-assistant')?.createdAt)
        .toBe('2026-01-01T00:11:00.000Z');

      remoteSessionStore.upsertDeviceSession('dev-1', 'Mac', session('s1', {
        userSendAt: '2026-01-01T00:00:00.000Z',
        updatedAt: '2026-01-01T00:00:00.000Z',
      }));

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.find((item) => item.id === localCardId)?.createdAt)
        .toBe('2026-01-01T00:10:00.000Z');
      expect(rows.find((item) => item.clientId === 'live-assistant')?.createdAt)
        .toBe('2026-01-01T00:00:00.000Z');
    } finally {
      vi.useRealTimers();
    }
  });

  it('主机持久化消息早于会话元数据时,也会收口临时 live 行的设备时间', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', undefined, 'OK', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('persisted-short', 's1', '2026-01-01T00:00:01.000Z'),
        content: 'OK',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows).toHaveLength(2);
      expect(rows[0]?.clientId).toMatch(/^mobile-stream-/);
      expect(rows[0]?.createdAt).toBe('2026-01-01T00:00:01.000Z');
      expect(rows[1]?.clientId).toBe('persisted-short');
    } finally {
      vi.useRealTimers();
    }
  });

  it('首个主机 user push 在插入后再重锚,保持问题先于 live 回复', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', undefined, 'OK', true);

      remoteSessionStore.appendMessage('s1', {
        ...messageAt('persisted-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Question',
      });

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'persisted-user',
        expect.stringMatching(/^mobile-stream-/),
      ]);
      expect(rows.map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('权威最新消息窗口先于会话元数据到达时也会重锚临时 live 行', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', undefined, 'OK', true);

      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('persisted-user', 's1', '2026-01-01T00:00:01.000Z'),
        role: 'user',
        content: 'Question',
      }]);

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows.map((item) => item.clientId)).toEqual([
        'persisted-user',
        expect.stringMatching(/^mobile-stream-/),
      ]);
      expect(rows.map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('权威 assistant 最新窗口重锚后仍让持久化行占据尾部', () => {
    vi.useFakeTimers();
    try {
      vi.setSystemTime(new Date('2026-01-01T00:10:00.000Z'));
      pushMakerText('s1', undefined, 'OK', true);

      remoteSessionStore.setLatestMessageWindow('s1', [{
        ...messageAt('persisted-short', 's1', '2026-01-01T00:00:01.000Z'),
        content: 'OK',
      }]);

      const rows = remoteSessionStore.getMessages('s1');
      expect(rows).toHaveLength(2);
      expect(rows[0]?.clientId).toMatch(/^mobile-stream-/);
      expect(rows[1]?.clientId).toBe('persisted-short');
      expect(rows.map((item) => item.createdAt)).toEqual([
        '2026-01-01T00:00:01.000Z',
        '2026-01-01T00:00:01.000Z',
      ]);
    } finally {
      vi.useRealTimers();
    }
  });

  it('流式增量的首个 delta 落定 createdAt 后,后续 delta 沿用同一戳(不重复取设备时间/不重新钳制)', () => {
    vi.useFakeTimers();
    try {
      remoteSessionStore.setLatestMessageWindow('s1', [
        messageAt('user-sent', 's1', '2026-01-01T00:10:00.000Z'),
      ]);
      vi.setSystemTime(new Date('2026-01-01T00:05:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'partial ', false);
      // 非 final 的增量先进 pendingTextDeltaBatches,由防抖定时器统一落定
      // (见 remoteSessionStore.ts scheduleTextDeltaFlush/flushPendingTextDeltas)。
      vi.runOnlyPendingTimers();
      const firstStamp = remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'live-assistant')?.createdAt;
      expect(firstStamp).toBe('2026-01-01T00:10:00.000Z');

      vi.setSystemTime(new Date('2026-01-01T00:20:00.000Z'));
      pushMakerText('s1', 'live-assistant', 'and more', false);
      vi.runOnlyPendingTimers();
      const secondStamp = remoteSessionStore.getMessages('s1')
        .find((item) => item.clientId === 'live-assistant')?.createdAt;
      // 已存在行的 createdAt 保持不变(见 remoteSessionStore.ts applyRemoteTextEvent
      // 对应分支的注释),不会因为设备时间继续前进而被重新戳一次。
      expect(secondStamp).toBe(firstStamp);
    } finally {
      vi.useRealTimers();
    }
  });
});

describe('task tag source isolation', () => {
  beforeEach(() => remoteSessionStore.clear());
  it('invalidates a first list response before a shard exists, only for the source device', () => {
    const epoch = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-a');
    const otherEpoch = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-b');
    remoteSessionStore.applyRemotePush('dev-a', 'local-db:task-tags:changed', { tags: [] });
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-a', epoch)).toBe(false);
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-b', otherEpoch)).toBe(true);
    const replacementEpoch = remoteSessionStore.captureDeviceSessionListMutationEpoch('dev-a');
    expect(remoteSessionStore.isDeviceSessionListMutationEpochCurrent('dev-a', replacementEpoch)).toBe(true);
  });
  it('projects rename and deletion only into the source computer task memberships', () => {
    const tag = {
      id: 'default:red',
      name: 'Red',
      color: 'red' as const,
      favoriteOrder: 0,
      revision: 1,
    };
    remoteSessionStore.setDeviceSessions('dev-a', 'A', [session('a', { tags: [tag] })]);
    remoteSessionStore.setDeviceSessions('dev-b', 'B', [session('b', { tags: [tag] })]);
    const renamed = { ...tag, name: 'Work', revision: 2 };
    remoteSessionStore.applyRemotePush('dev-a', 'local-db:task-tags:changed', {
      tags: [renamed],
    });
    expect(remoteSessionStore.getSessions().find((row) => row.id === 'a')?.tags).toEqual([renamed]);
    expect(remoteSessionStore.getSessions().find((row) => row.id === 'b')?.tags).toEqual([tag]);
    remoteSessionStore.applyRemotePush('dev-a', 'local-db:task-tags:changed', {
      tags: [],
    });
    expect(remoteSessionStore.getSessions().find((row) => row.id === 'a')?.tags).toEqual([]);
    expect(remoteSessionStore.getSessions().find((row) => row.id === 'b')?.tags).toEqual([tag]);
  });
});

describe('list message reuse', () => {
  beforeEach(() => remoteSessionStore.clear());
  it('keeps explicitly prefetched prose after leaving a task, while stale detail pushes remain blocked', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Host', [session('s1')]);
    const authority = remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.leaveSessionMessageDetail('s1', 'detail-blur', authority);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', { sessionId: 's1', message: message('stale', 's1') });
    const content = '完整正文'.repeat(2000);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', { sessionId: 's1', listMessage: true,
      message: { ...message('prefetched', 's1'), content } });
    expect(remoteSessionStore.getMessages('s1').map(row => row.id)).toEqual(['prefetched']);
    expect(remoteSessionStore.getMessages('s1')[0].content).toBe(content);
    remoteSessionStore.enterSessionMessageDetail('s1');
    expect(remoteSessionStore.getMessages('s1')[0].content).toBe(content);
  });

  it('merges the disk window under list-only rows when the task opens', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Host', [session('s1')]);
    const at = (id: string, createdAt: string) => messageAt(id, 's1', createdAt);
    remoteSessionStore.applyRemotePush('dev-1', 'local-db:messages:created', { sessionId: 's1', listMessage: true,
      message: at('prefetched', '2026-01-01T00:00:09.000Z') });
    remoteSessionStore.enterSessionMessageDetail('s1');
    remoteSessionStore.hydrateMessagesIfEmpty('s1', [
      at('older', '2026-01-01T00:00:01.000Z'), { ...at('prefetched', '2026-01-01T00:00:09.000Z'), content: 'cached copy' },
    ]);
    expect(remoteSessionStore.getMessages('s1').map(row => [row.id, row.content])).toEqual([
      ['older', 'hello'], ['prefetched', 'hello'],
    ]);
    // Only a list-only mirror merges; a loaded window keeps the existing "if empty" rule.
    remoteSessionStore.hydrateMessagesIfEmpty('s1', [at('ignored', '2026-01-01T00:00:00.000Z')]);
    expect(remoteSessionStore.getMessages('s1').map(row => row.id)).toEqual(['older', 'prefetched']);
  });

  it('does not let another device seed the same task ID', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Host', [session('s1')]);
    remoteSessionStore.applyRemotePush('other', 'local-db:messages:created', { sessionId: 's1', listMessage: true, message: message('wrong', 's1') });
    expect(remoteSessionStore.getMessages('s1')).toEqual([]);
  });
});

describe('jank metrics store size', () => {
  beforeEach(() => remoteSessionStore.clear());

  it('counts full-store, home status and per-task preview subscribers separately', () => {
    remoteSessionStore.setDeviceSessions('dev-1', 'Host', [session('s1'), session('s2')]);
    remoteSessionStore.setMessages('s1', [message('m1', 's1'), message('m2', 's1')]);
    const before = takeJankMetrics().store!;
    const unsubscribe = [
      remoteSessionStore.subscribe(() => undefined),
      remoteSessionStore.subscribeHomeStatus(() => undefined),
      remoteSessionStore.subscribeHomeStatus(() => undefined),
      remoteSessionStore.subscribeSessionMessagePreview('s1', () => undefined),
      remoteSessionStore.subscribeSessionMessagePreview('s2', () => undefined),
      remoteSessionStore.subscribeSessionMessagePreview('s2', () => undefined),
    ];
    setJankMetricsEnabled(true);
    try {
      const store = takeJankMetrics().store!;
      expect(store).toMatchObject({ sessionCount: 1, messageCount: 2, maxSessionMessageCount: 2 });
      expect(store.subscriberCount - before.subscriberCount).toBe(1);
      expect(store.homeStatusSubscriberCount - before.homeStatusSubscriberCount).toBe(2);
      expect(store.previewSubscriberCount - before.previewSubscriberCount).toBe(3);
    } finally {
      setJankMetricsEnabled(false);
      for (const off of unsubscribe) off();
    }
  });
});