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。

24.0 KB
Raw Blame
/**
 * allowlist 单测:守住「默认拒绝」语义 + 关键 channel 准入 / 拒绝边界。
 * 这是远程控制的安全闸门,回归必须显式。
 */
import { describe, it, expect } from 'vitest';
import { TASK_MIGRATION_CHANNEL } from '../taskMigration.js';
import {
  REMOTE_INVOKE_ALLOWLIST,
  REMOTE_REVIEW_EXTERNAL_INPUT_CHANNELS,
  PUSH_FORWARD_ALLOWLIST,
  INVOKE_TIMEOUT_OVERRIDES_MS,
  computeAllowlistHash,
  DL_SUBSCRIBE_CHANNEL,
  DL_UNSUBSCRIBE_CHANNEL,
  DL_MEDIA_FETCH_CHANNEL,
  DL_VOICE_TRANSCRIBE_CHANNEL,
  DL_VOICE_CREDENTIAL_SYNC_CHANNEL,
  DL_VOICE_DICTIONARY_LEARNING_CHANNEL,
  DL_HISTORY_MESSAGES_CHANNEL,
  DL_TELEGRAM_STATUS_CHANNEL,
  DL_TELEGRAM_SET_ONLINE_CHANNEL,
} from '../allowlist.js';
import { SESSION_ACTIVITY_CHANNEL, topicForPush } from '../topics.js';
import {
  REMOTE_RESOURCE_CHANGED_CHANNEL,
  REMOTE_RESOURCE_CHANNELS,
} from '../remoteResources.js';

describe('REMOTE_INVOKE_ALLOWLIST', () => {
  it('allows the public composer projection without exposing the full installed plugin records', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('ghosts:composer-list')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('ghosts:list')).toBe(false);
  });
  it('allows Review start to run on the data-owning Desktop', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:review:start')).toBe(true);
  });

  it('allows the read-only task context-window bounds query but no generic session write', () => {
    // 只读边界查询:控制端据此才能在远程任务上给出「更大」的档位。
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:get-context-window-bounds')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:set-context-window-budget')).toBe(true);
    // 通用写入口不因这条查询放开。
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:sessions:update')).toBe(false);
  });
  it('allows the reduced teammate directory while keeping native configuration local', () => {
    for (const channel of ['local-db:bots:list', 'local-db:bots:get']) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(channel)).toBe(true);
    }
    for (const channel of [
      'local-db:bots:choose-avatar', 'local-db:bots:create', 'local-db:bots:update',
      'local-db:bots:model-chain-settings-set', 'maker:bot-lifecycle:action',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(channel)).toBe(false);
    }
  });

  it('keeps every Review external-input classification inside the remote allowlist', () => {
    for (const channel of REMOTE_REVIEW_EXTERNAL_INPUT_CHANNELS) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(channel)).toBe(true);
    }
    expect(REMOTE_REVIEW_EXTERNAL_INPUT_CHANNELS.has('maker:input:get-projection')).toBe(false);
  });

  it('放行模块中立的远程资源 API,而不是逐功能扩张 channel', () => {
    for (const channel of REMOTE_RESOURCE_CHANNELS) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(channel)).toBe(true);
    }
    expect(PUSH_FORWARD_ALLOWLIST.has(REMOTE_RESOURCE_CHANGED_CHANNEL)).toBe(true);
    expect(topicForPush(REMOTE_RESOURCE_CHANGED_CHANNEL, {
      collectionId: 'teammates',
    })).toBe('sessions');
  });

  it('放行核心会话链路', () => {
    for (const ch of [
      'maker:create-session',
      'maker:send',
      'maker:abort-session',
      'maker:resolve-interaction',
      'maker:get-pending-interactions',
      'maker:input:compact',
      'maker:compact-session',
      'maker:set-model',
      'maker:switch-session-agent',
      'maker:get-session-agent-switch-intent',
      'local-db:sessions:list',
      'local-db:conversations:search',
      DL_HISTORY_MESSAGES_CHANNEL,
      'local-db:messages:list',
      'local-db:messages:around',
      'local-db:messages:around-client-id',
      'maker:message:delete',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
  });

  it('允许远程会话读取被控端 Git / GitHub 上下文', () => {
    for (const ch of [
      'git-context:get-for-session',
      'git-context:pr-refs:list',
      'git-context:pr-status',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
  });

  it('放行订阅价值历史汇总只读聚合(远程会话底部 $ chip 的历史初值查被控端)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:messages:estimatedSessionValue')).toBe(true);
  });

  it('放行 per-session turn 态只读查询(控制端 stall 看门狗核实被控端用)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:session-in-turn')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:any-session-in-turn')).toBe(true);
  });

  it('放行会话未读已读回执(控制端看完会话,清被控端灵动岛 / 角标 / 侧栏未读)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('notification:clear-session-attention')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('agent-island:completion-action')).toBe(true);
    // mark 方向不放行:未读的产生真相只在被控端 main,远程不得凭空标未读。
    expect(REMOTE_INVOKE_ALLOWLIST.has('notification:mark-session-attention')).toBe(false);
  });

  it('放行 M4 完整控制面(scheduler / orca / rewind / 只读 usage)', () => {
    for (const ch of [
      'maker:schedule:create',
      'maker:schedule:get-runtime-state',
      'maker:worker:create',
      'maker:worker:dispatch-ui-assignment',
      'maker:worker:update',
      'maker:session:enable-orca',
      'maker:rewind:commit',
      'maker:fork',
      'maker:usage:today',
      'maker:memory:get',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:orca-workflows:list-workers-by-leads')).toBe(false);
  });

  it('放行 workflow 逐 agent 进度树只读(记录文件真相在被控端 HOME,控制端本机读必落空)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:get-workflow-progress')).toBe(true);
  });

  it('放行后台命令输出尾部只读(.output 文件真相在被控端,控制端本机读必落空)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:background-task:output-tail')).toBe(true);
  });

  it('放行会话后台任务快照只读(任务真身在被控端,后台任务面板挂载水合用)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:session-background-tasks:list')).toBe(true);
  });

  it('放行后台任务停止(单个 / 全部):任务真身在被控端,控制端本机停止只会假成功', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:agent-task:stop')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:session-background-tasks:stop')).toBe(true);
  });

  it('routes durable PI Subagent reads and controls to the data-owning device', () => {
    for (const channel of [
      'local-db:subagent-runs:list',
      'local-db:subagent-runs:detail',
      'local-db:subagent-runs:transcript',
      'maker:pi-subagent:control',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(channel)).toBe(true);
    }
  });

  it('放行会话级完整对等补充(fork-strip / context-usage / 窄口径 patch-meta / Magic 重命名)', () => {
    for (const ch of [
      'maker:fork-strip-encrypted',
      'maker:get-context-usage',
      'local-db:sessions:patch-meta',
      'maker:generate-title',
      'maker:regenerate-title',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
  });

  it('放行 device-link 远程草稿镜像只读读(控制端 seed 被控端当前 New Maker 草稿)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:get-new-maker-defaults')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:get-new-maker-worktree-branch-pref')).toBe(true);
  });

  it('放行 device-link 模型列表 effort/fast 写穿(草稿 + 会话非选中,控制端→被控端)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:apply-new-maker-draft-pref')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:apply-new-maker-worktree-branch-pref')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:set-session-model-pref')).toBe(true);
  });

  it('放行模型单价表只读(控制端模型选择器展示被控端视角单价)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:model-pricing')).toBe(true);
  });

  it('放行用量历史跨设备合并的原始用量行只读读取', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:device-rows')).toBe(true);
    // 本机聚合入口仍只对受信 renderer 开放, 不经隧道暴露。
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:history')).toBe(false);
  });

  it('放行 Codex 官方额度读取与 desktop 绑定的人工 reset offer', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:codex-rate-limits')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:codex-rate-limit-reset')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:codex-reset-auto-use')).toBe(true);
  });

  it('放行 Claude 订阅余量快照只读(远程订阅会话 chip 镜像被控端窗口剩余)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:claude-subscription')).toBe(true);
  });

  it('放行 xAI 订阅周用量只读(被控端 dispatch 拦截执行,不进挂 assert 的 ipcMain)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:usage:xai-subscription')).toBe(true);
  });

  it('放行 cc 默认路由观察值只读(路由真值在被控端 proxy registry,远程形态判定用)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:claude-session-route:get')).toBe(true);
  });

  it('放行被控端项目顺序读写(显示偏好,真相在被控端)', () => {
    // #6916: only the host display-name read is remotely callable; local preferences stay local.
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:project-aliases:list')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:project-aliases:set')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:project-aliases:delete')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('sidebar-settings:get-project-order')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('sidebar-settings:apply-project-order')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('sidebar-settings:set-project-order')).toBe(false);
  });

  it('exposes host project aliases as a read-only snapshot and forwards invalidations', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('sidebar-settings:get-project-aliases')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:project-aliases:list')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:project-aliases:set')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('local-db:project-aliases:delete')).toBe(false);
    expect(PUSH_FORWARD_ALLOWLIST.has('local-db:project-aliases:changed')).toBe(true);
  });

  it('放行 Git safety 只读查询(远程 Codex Rewind 按被控端 snapshot 设置 gate)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:git-safety:get')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:git-safety:set')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:git-safety:reset')).toBe(false);
  });

  it('放行网关 API key presence-only 探测(只回 boolean,不回密钥材料)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('maker:api-key:present')).toBe(true);
    // 真正的密钥读写仍绝不放行(下方「绝不放行」与不变式守卫共同看住)。
    expect(REMOTE_INVOKE_ALLOWLIST.has('api-key:save')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('api-key:get')).toBe(false);
  });

  it('放行本机目录浏览(「添加远程项目」逐级选被控端项目目录:只读枚举 + mkdir -p)', () => {
    for (const ch of ['fs:list-dir', 'fs:stat-path', 'fs:mkdir-p']) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
  });

  it('放行窄口径文本文件预览(只读 + 大小上限 + forbidden/oversize reason)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('text-file:read-preview')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('read-file-for-attachment')).toBe(false);
    // 同理:read-file-bytes 回整个文件的原始字节(PDF 预览用),调用方永远是被控端
    // 本机 renderer,远程控制端不需要、也不得拿到这条通道。
    expect(REMOTE_INVOKE_ALLOWLIST.has('read-file-bytes')).toBe(false);
  });

  it('放行 /goal 远程(goal-host 在被控端,per-session 业务写)', () => {
    for (const ch of [
      'maker:goal:set',
      'maker:goal:clear',
      'maker:goal:get-status',
      'maker:goal:pause',
      'maker:goal:resume',
      'maker:goal:update',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
  });

  it('放行 /learn 远程(learn-host 全流程在被控端,skill 落被控端)', () => {
    for (const ch of [
      'learn:start',
      'learn:list-runs',
      'learn:get-proposal-diff',
      'learn:apply',
      'learn:discard',
      'learn:cancel',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
  });

  it('放行 /cmd 远程(被控端 workingDir 执行,cwd 过 remote-workdir-guard)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('desktop-cmd:run')).toBe(true);
  });

  it('放行远程 worktree(git 探测 / 分支 / 建议名 / 删除预检 + create / 窄补偿回收)', () => {
    for (const ch of [
      'worktree:detect-cwd',
      'worktree:list-branches',
      'worktree:branch-sync-status',
      'worktree:sync-branch',
      'worktree:suggest-name',
      'worktree:create',
      'worktree:discard-precreated',
      'worktree:cancel-precreated',
      'worktree:removal-preview',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(true);
    }
    // 通用删除与 reveal 不放行:discard-precreated 只接受 sessionId + 已登记的
    // 精确 path,或 create 前已持久化且与被控端元数据匹配的 recoveryKey,并由
    // 被控端复核 ownership/dirty/live refs;其余删除仍只在被控端状态变更流程
    // 内部触发。reveal 是本机 shell 副作用(shell.showItemInFolder)。
    expect(REMOTE_INVOKE_ALLOWLIST.has('worktree:reveal')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('worktree:get-for-session')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('worktree:list-all')).toBe(false);
    expect(REMOTE_INVOKE_ALLOWLIST.has('worktree:remove')).toBe(false);
  });

  it('放行订阅控制帧(push 驱动:subscribe / unsubscribe)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('device-link:subscribe')).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has('device-link:unsubscribe')).toBe(true);
    // 常量与字面量一致(契约稳定)
    expect(DL_SUBSCRIBE_CHANNEL).toBe('device-link:subscribe');
    expect(DL_UNSUBSCRIBE_CHANNEL).toBe('device-link:unsubscribe');
  });

  it('放行入方向媒体取件帧(被控端 dispatch 拦截执行;契约 + 能力探测)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('device-link:media:fetch')).toBe(true);
    expect(DL_MEDIA_FETCH_CHANNEL).toBe('device-link:media:fetch');
  });

  it('放行手机语音转写帧(手机上传 OSS,被控端用本机 ASR 配置转写)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('device-link:voice:transcribe')).toBe(true);
    expect(DL_VOICE_TRANSCRIBE_CHANNEL).toBe('device-link:voice:transcribe');
  });

  it('放行手机语音 credential 临时同步帧(仅 voice ASR/refine 专用,非通用 key 读写)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('device-link:voice:credential-sync')).toBe(true);
    expect(DL_VOICE_CREDENTIAL_SYNC_CHANNEL).toBe('device-link:voice:credential-sync');
  });

  it('放行手机语音词典学习 evidence 回写帧(词典仍写在被控桌面)', () => {
    expect(REMOTE_INVOKE_ALLOWLIST.has('device-link:voice:dictionary-learning')).toBe(true);
    expect(DL_VOICE_DICTIONARY_LEARNING_CHANNEL).toBe('device-link:voice:dictionary-learning');
  });

  it('放行个人 Telegram bot 的跨设备上下线(窄口径例外),但本地那条 IPC 仍绝不放行', () => {
    // 放行的是两条 device-link 专用通道:被控端 dispatch 拦截执行, 只切轮询。
    expect(REMOTE_INVOKE_ALLOWLIST.has(DL_TELEGRAM_STATUS_CHANNEL)).toBe(true);
    expect(REMOTE_INVOKE_ALLOWLIST.has(DL_TELEGRAM_SET_ONLINE_CHANNEL)).toBe(true);
    // 本地 IM IPC 一律不得入表:它们在 im/host.ts 统一挂了
    // assertTrustedAppRendererEvent(只认真实 sender), 是有意拦住 IM 凭证/配置面的
    // 闸门。未来若有人图省事把 telegramBot:* 加进来, 这条直接红 —— 尤其
    // disconnect / set-config 会清凭证或换 token, 远程绝不该碰。
    for (const ch of [
      'telegramBot:set-online',
      'telegramBot:disconnect',
      'telegramBot:set-config',
      'telegramBot:get-status',
      'discordBot:set-config',
      'feishuBot:set-config',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(false);
    }
  });

  it('绝不放行:本机副作用 / 全局设置写 / 账号密钥 / 裸写库 / 窗口 UI', () => {
    for (const ch of [
      'shell:open-path',
      'maker:compat-mode:set',
      'maker:lsp-mode:set',
      'maker:memory:set',
      'maker:codex-auth-mode:set',
      'auth:trigger-login',
      'maker:auth:trigger-login',
      'maker:auth:logout',
      'api-key:save',
      'local-db:sessions:create',
      'local-db:sessions:update',
      'local-db:messages:create',
      'maker:execute-desktop-command',
      'maker:open-session-in-new-window',
      'show-open-directory-dialog',
      'window-minimize',
      'page-zoom:in',
    ]) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch)).toBe(false);
    }
  });

  it('不变式守卫:全表扫描,任何命中危险模式的 channel 都不得入表(挡未来误加)', () => {
    // 把「永不放行」的类别从 prose 注释固化成可执行不变式:未来若有人往 allowlist 里
    // 误加 auth:* / shell:* / 裸写库 / 全局设置写 / 本机 UI 副作用等,这条直接红。
    // 模式刻意避开合法项:maker:set-model(per-session 运行时切换)不以 `:set` 结尾,
    // maker:schedule:create(业务 handler)无 `local-db:` 前缀。
    const FORBIDDEN: Array<{ re: RegExp; why: string }> = [
      { re: /^auth:/, why: '顶层账号鉴权' },
      { re: /^shell:/, why: 'shell 副作用' },
      { re: /^(window[-:]|page-zoom|find-in-page)/, why: '窗口 / UI' },
      { re: /api-key|safe-storage/, why: '密钥 / 安全存储' },
      { re: /^local-db:.*:(create|update|delete)$/, why: 'local-db 裸写' },
      // maker:goal:set 是 per-session 业务写(入参带 sessionId、goal-host 按会话管理),
      // 不属「全局设置写」;显式豁免,其余任何 :set 结尾 channel 仍然拦。
      { re: /^(?!maker:goal:set$).*:set$/, why: '全局设置写' },
      { re: /execute-desktop-command|open-session-in-new-window|show-open-directory-dialog/, why: '本机 UI 副作用' },
      { re: /updater|release-notes|session-import|migration/, why: 'updater / 导入 / 迁移' },
    ];
    // 显式豁免:
    //  - `maker:goal:set` 是 per-session 域动作(入参带 sessionId,只影响单个任务的
    //    目标状态机),与 compat-mode:set / memory:set 这类全局设置写不同类,同类的
    //    maker:set-permission-mode 本就放行;仅命名撞上 `:set$` 模式。
    //  - `maker:api-key:present` 是 presence-only 探测:只回 { present: boolean },
    //    不回、也永不扩展为读取密钥材料(handler 见 desktop authHandlers.ts)。密钥类
    //    通用读写(api-key:save/get、safe-storage)仍被本模式看住,禁止再加同前缀通道。
    // 单任务迁移仅允许同账号控制端,通过校验后的快照附件与持久交接状态机执行;
    // 不提供通用导入、调用方指定任意目标路径或裸写库接口。
    const FORBIDDEN_EXEMPT = new Set(['maker:goal:set', 'maker:api-key:present', TASK_MIGRATION_CHANNEL]);
    for (const ch of REMOTE_INVOKE_ALLOWLIST) {
      if (FORBIDDEN_EXEMPT.has(ch)) continue;
      for (const { re, why } of FORBIDDEN) {
        expect(re.test(ch), `${ch} 命中禁止模式(${why}),不得进 REMOTE_INVOKE_ALLOWLIST`).toBe(false);
      }
    }
  });
});

describe('PUSH_FORWARD_ALLOWLIST', () => {
  it('转发事件流 / 交互 / 读模型增量', () => {
    for (const ch of [
      'maker:event',
      'maker:status-changed',
      'maker:interaction-request',
      'maker:interaction-dismissed',
      'maker:auto-permission:fallback',
      'maker:session-credential-switch-applied',
      'maker:session-credential-switch-failed',
      'maker:provider:changed',
      'maker:agents:changed',
      'maker:schedule:event',
      'maker:orca:worker-changed',
      'usage:message-turn-cost',
      'usage:session-spend-changed',
      'usage:session-tokens-changed',
      'usage:claude-subscription-changed',
      'usage:codex-account-changed',
      'usage:codex-provider-account-changed',
      'usage:subscription-provider-account-changed',
      'usage:claude-account-changed',
      'usage:xai-subscription-changed',
      'usage:xai-rate-limit-changed',
      'usage:xai-provider-rate-limit-changed',
      'maker:claude-session-route-changed',
      'local-db:messages:created',
      'local-db:messages:deleted',
      'local-db:session:error-persisted',
      'sidebar-settings:project-order-changed',
      'local-db:project-aliases:changed',
      SESSION_ACTIVITY_CHANNEL,
    ]) {
      expect(PUSH_FORWARD_ALLOWLIST.has(ch)).toBe(true);
    }
  });

  it('转发 device-link 模型列表变更(草稿全量 + 会话非选中)', () => {
    expect(PUSH_FORWARD_ALLOWLIST.has('maker:new-maker-draft:changed')).toBe(true);
    expect(PUSH_FORWARD_ALLOWLIST.has('maker:new-maker-worktree-branch:changed')).toBe(true);
    expect(PUSH_FORWARD_ALLOWLIST.has('maker:session-model-pref:changed')).toBe(true);
  });

  it('转发 /goal 状态变化与 /learn run 状态机流转', () => {
    expect(PUSH_FORWARD_ALLOWLIST.has('maker:goal:status-changed')).toBe(true);
    expect(PUSH_FORWARD_ALLOWLIST.has('learn:event')).toBe(true);
  });

  it('不转发任意未列 channel(防意外泄露本机 UI 事件)', () => {
    expect(PUSH_FORWARD_ALLOWLIST.has('maker:desktop-command-triggered')).toBe(false);
    // 死条目已移除:发射点不 tap、控制端不消费,放白名单只会误导。
    expect(PUSH_FORWARD_ALLOWLIST.has('maker:auth:state-changed')).toBe(false);
  });
});

describe('INVOKE_TIMEOUT_OVERRIDES_MS', () => {
  it('desktop-cmd:run 隧道超时必须大于被控端执行预算(30s CMD + 5s kill 宽限)', () => {
    // 对撞回归:隧道默认 30s == 被控端命令超时 30s → 慢命令结果被丢弃且看不到 timedOut 语义。
    expect(INVOKE_TIMEOUT_OVERRIDES_MS['desktop-cmd:run']).toBeGreaterThan(35_000);
  });

  it('覆盖表内的 channel 必须都在 REMOTE_INVOKE_ALLOWLIST(不给白名单外通道配超时)', () => {
    for (const ch of Object.keys(INVOKE_TIMEOUT_OVERRIDES_MS)) {
      expect(REMOTE_INVOKE_ALLOWLIST.has(ch), `${ch} 不在 allowlist`).toBe(true);
    }
  });

  it('worktree:create 隧道超时必须大于默认 30s(git worktree add + 选择性 checkout 预算)', () => {
    expect(INVOKE_TIMEOUT_OVERRIDES_MS['worktree:create']).toBeGreaterThan(30_000);
  });

  it('worktree:discard-precreated 可等待同 session 创建锁且不沿用默认 30s', () => {
    expect(INVOKE_TIMEOUT_OVERRIDES_MS['worktree:discard-precreated']).toBeGreaterThan(30_000);
    expect(INVOKE_TIMEOUT_OVERRIDES_MS['worktree:cancel-precreated']).toBeGreaterThan(30_000);
  });

  it('maker:compact-session 隧道超时必须大于 pi 压缩执行预算(10min + 回程余量,不 30s 截断)', () => {
    // 被控端在请求穿过 relay 后才开始跑 PI_COMPACT_TIMEOUT_MS(10min);控制端若只给
    // 相同预算,压缩恰好到上限时会先 INVOKE_TIMEOUT 并被误判为设备无响应(codex P2)。
    // 严格大于 10min,锁住「带余量」的语义,防止回退成无余量的同值。
    expect(INVOKE_TIMEOUT_OVERRIDES_MS['maker:compact-session']).toBeGreaterThan(10 * 60_000);
  });

  it('UI Worker 派单超时必须大于 Lead history gate 的 30s 执行预算', () => {
    expect(
      INVOKE_TIMEOUT_OVERRIDES_MS['maker:worker:dispatch-ui-assignment'],
    ).toBeGreaterThan(30_000);
  });
});

describe('computeAllowlistHash', () => {
  it('稳定且确定(同一 allowlist 多次计算一致)', () => {
    expect(computeAllowlistHash()).toBe(computeAllowlistHash());
    expect(computeAllowlistHash()).toMatch(/^[0-9a-f]{8}$/);
  });
});