Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
Merge origin/main into claude/session-worktree-option-4346fc
Kafeifei1db3196d6eed
diff --cc apps/desktop/src/main/maker-ipc/sessionCreateHandler.tsindex 503cb57081,24724de483..ff661c9405--- a/apps/desktop/src/main/maker-ipc/sessionCreateHandler.ts+++ b/apps/desktop/src/main/maker-ipc/sessionCreateHandler.ts@@@ -62,52 -70,53 +75,60 @@@ export function registerMakerSessionCredeps.warnStderr,);- let bootstrapped: Awaited<ReturnType<typeof deps.bootstrapSession>>;- try {- bootstrapped = await deps.bootstrapSession(o);- } catch (err) {- if (isCredentialModeSwitchBusyError(err)) {- // 独立 code:新建会话撞上凭证切换忙(别的会话在跑),不是"本会话在跑"。- // renderer 据此走 ipcError.CREDENTIAL_SWITCH_BUSY 专属文案。- throwIpcError('CREDENTIAL_SWITCH_BUSY', err.message);+ const run = async () => {+ let bootstrapped: Awaited<ReturnType<typeof deps.bootstrapSession>>;+ try {+ bootstrapped = await deps.bootstrapSession(o);+ } catch (err) {+ if (isCredentialModeSwitchBusyError(err)) {+ // 独立 code:新建会话撞上凭证切换忙(别的会话在跑),不是"本会话在跑"。+ // renderer 据此走 ipcError.CREDENTIAL_SWITCH_BUSY 专属文案。+ throwIpcError('CREDENTIAL_SWITCH_BUSY', err.message);+ }++ // 远端 Claude 路由 materialization 错误([REMOTE_*] 前缀)映射到独立 code,++ // renderer 按 code 给可操作文案(连接订阅 / 换来源 / 稍后重试),不吞成通用++ // 「创建会话失败」。++ const remoteRouteCode = remoteRouteErrorCode(err);++ if (remoteRouteCode) {++ const msg = err instanceof Error ? err.message : String(err);++ throwIpcError(remoteRouteCode, msg);++ }+ throw err;}- // 远端 Claude 路由 materialization 错误([REMOTE_*] 前缀)映射到独立 code,- // renderer 按 code 给可操作文案(连接订阅 / 换来源 / 稍后重试),不吞成通用- // 「创建会话失败」。- const remoteRouteCode = remoteRouteErrorCode(err);- if (remoteRouteCode) {- const msg = err instanceof Error ? err.message : String(err);- throwIpcError(remoteRouteCode, msg);- }- throw err;- }- const { session, didInjectOrcaInstructions, didInjectProjectContext } = bootstrapped;+ const { session, didInjectOrcaInstructions, didInjectProjectContext } = bootstrapped;- deps.logCreateSession({- agentKind: o.agentKind,- model: o.model,- fastMode: o.fastMode ?? 'default',- workDir: o.workingDir,- providedId: !!o.id,- usedOrcaInstructions: didInjectOrcaInstructions,- usedProjectContext: didInjectProjectContext,- extraDirsCount: o.extraDirs?.length ?? 0,- });+ deps.logCreateSession({+ agentKind: o.agentKind,+ model: o.model,+ fastMode: o.fastMode ?? 'default',+ workDir: o.workingDir,+ providedId: !!o.id,+ usedOrcaInstructions: didInjectOrcaInstructions,+ usedProjectContext: didInjectProjectContext,+ extraDirsCount: o.extraDirs?.length ?? 0,+ });- if (o.orcaRole === 'lead') {- await deps.markOrcaRoleIfNeeded(session.id, o.orcaRole);- }- deps.markKnownNonOrcaIfApplicable(session.id, o);- if (o.orcaRole === 'worker' && !o.resumeSessionId) {- deps.sendWorkerReadyMessage(session);- }- deps.broadcastSessionCreated(session.id);+ if (o.orcaRole === 'lead') {+ await deps.markOrcaRoleIfNeeded(session.id, o.orcaRole);+ }+ deps.markKnownNonOrcaIfApplicable(session.id, o);+ if (o.orcaRole === 'worker' && !o.resumeSessionId) {+ deps.sendWorkerReadyMessage(session);+ }+ deps.broadcastSessionCreated(session.id);- return {- sessionId: session.id,- agentKind: session.agentKind,- workDir: session.workDir,- capabilities: session.capabilities,- usedProjectContext: didInjectProjectContext,+ return {+ sessionId: session.id,+ agentKind: session.agentKind,+ workDir: session.workDir,+ capabilities: session.capabilities,+ usedProjectContext: didInjectProjectContext,+ };};++ const explicitSessionId = typeof o.id === 'string' && o.id ? o.id : null;+ return explicitSessionId && deps.withSessionLock+ ? deps.withSessionLock(explicitSessionId, run)+ : run();});}diff --cc apps/desktop/src/renderer/__tests__/newMakerProjectPicker.test.tsindex adcbfcfcc6,585b6d3f1e..dd652faf2e--- a/apps/desktop/src/renderer/__tests__/newMakerProjectPicker.test.ts+++ b/apps/desktop/src/renderer/__tests__/newMakerProjectPicker.test.ts@@@ -12,46 -12,59 +12,61 @@@ import { resolve } from 'node:path'import { describe, expect, it } from 'vitest';- const newMakerDraftRouteSource = readFileSync(- resolve(__dirname, '..', 'features', 'cc-agent', 'NewMakerDraftRoute.tsx'),- 'utf8',- );+ // 本仓 core.autocrlf=true 时 Windows checkout 出来的源文件是 CRLF,而下面的快照断言+ // 统一按 LF 书写;读入时归一化行尾(CRLF 与孤立 CR 都归一为 LF),让断言不绑定+ // checkout 端的行尾配置。+ const readSource = (...segments: string[]): string =>+ readFileSync(resolve(__dirname, '..', ...segments), 'utf8').replace(/\r\n?/g, '\n');- const worktreeChipsSource = readFileSync(- resolve(__dirname, '..', 'components', 'new-chat', 'WorktreeChipsRow.tsx'),- 'utf8',- );+ const newMakerDraftRouteSource = readSource('features', 'cc-agent', 'NewMakerDraftRoute.tsx');- const branchPickSource = readFileSync(- resolve(__dirname, '..', 'components', 'new-chat', 'branchPick.ts'),- 'utf8',- );+ const worktreeChipsSource = readSource('components', 'new-chat', 'WorktreeChipsRow.tsx');- const folderPickerPopoverSource = readFileSync(- resolve(__dirname, '..', 'components', 'new-chat', 'FolderPickerPopover.tsx'),- 'utf8',- );+ const folderPickerPopoverSource = readSource('components', 'new-chat', 'FolderPickerPopover.tsx');- const addRemoteProjectDialogSource = readFileSync(- resolve(__dirname, '..', 'components', 'new-chat', 'AddRemoteProjectDialog.tsx'),- 'utf8',+ const addRemoteProjectDialogSource = readSource(+ 'components',+ 'new-chat',+ 'AddRemoteProjectDialog.tsx',);- const projectPickerOptionsHookSource = readFileSync(- resolve(__dirname, '..', 'hooks', 'useProjectPickerOptions.ts'),- 'utf8',- );+ const projectPickerOptionsHookSource = readSource('hooks', 'useProjectPickerOptions.ts');++ const deviceLinkProjectsHookSource = readSource('hooks', 'useDeviceLinkProjects.ts');++ const remoteSessionHandoffSource = readSource('features', 'cc-agent', 'remoteSessionHandoff.ts');++ const deviceSwitcherPillSource = readSource('components', 'new-chat', 'DeviceSwitcherPill.tsx');- const scheduleFormDialogSource = readFileSync(- resolve(__dirname, '..', 'features', 'scheduler', 'components', 'ScheduleFormDialog.tsx'),- 'utf8',+ const controllableDevicesHookSource = readSource('hooks', 'useControllableDevices.ts');++ const deviceLinkRemoteProjectsSource = readSource(+ 'features',+ 'device-link',+ 'useDeviceLinkRemoteProjects.ts',);- const scheduleChipsSource = readFileSync(- resolve(__dirname, '..', 'features', 'scheduler', 'components', 'ScheduleChips.tsx'),- 'utf8',++const branchPickSource = readSource('components', 'new-chat', 'branchPick.ts');+++ const deviceProvidersHookSource = readSource('hooks', 'useDeviceProviders.ts');++ const agentCapabilitiesHookSource = readSource('hooks', 'useAgentCapabilities.ts');++ const scheduleFormDialogSource = readSource(+ 'features',+ 'scheduler',+ 'components',+ 'ScheduleFormDialog.tsx',);+ const scheduleChipsSource = readSource('features', 'scheduler', 'components', 'ScheduleChips.tsx');++ const newGoalDialogSource = readSource('components', 'new-chat', 'NewGoalDialog.tsx');++ const extraDirsButtonSource = readSource('components', 'new-chat', 'ExtraDirsButton.tsx');++ const sidebarUpperSource = readSource('features', 'cc-agent', 'CCAgentSidebarUpper.tsx');+describe('Shared create project picker', () => {it('builds project options from the persistent recent_workdirs table, not from live sessions', () => {// 0031 起创建页草稿的"项目"下拉脱离 sessions 列表,改读 recent_workdirs@@@ -183,30 -161,9 +198,36 @@@expect(newMakerDraftRouteSource).toContain('if (wd && !isRemoteProjectDraft)');// device-link 草稿的 git 探测经隧道在被控端执行(本机 git 对远程路径必然误报);// SSH(worktreeDisabled)仍不探测。+ expect(worktreeChipsSource).toContain('deviceLinkReconnectEpoch,');+ expect(newMakerDraftRouteSource).toContain(+ 'deviceLinkReconnectEpoch={remoteDraftRefreshEpoch}',+ );expect(worktreeChipsSource).toContain(- 'useDetectCwd(worktreeDisabled ? null : (cwd ?? null), deviceLinkDeviceId)',+ "sourceBranch || branches.current || 'HEAD'",);+ expect(worktreeChipsSource).not.toContain(+ "sourceBranch || branches.current || 'main'",+ );+ });++ it('invalidates worktree probe-derived fields when the selected project changes', () => {+ const start = newMakerDraftRouteSource.indexOf('const handleWorkingDirChange');+ const end = newMakerDraftRouteSource.indexOf(+ '// ─── 新草稿入场',+ start,+ );+ const handler = newMakerDraftRouteSource.slice(start, end);++ const actionStart = newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback(');++ const actionEnd = newMakerDraftRouteSource.indexOf(++ '// 弹窗确认添加后的落点',++ actionStart,++ );++ const action = newMakerDraftRouteSource.slice(actionStart, actionEnd);+- expect(handler).toContain('if (dir !== effectiveWorkingDir)');- expect(handler).toContain('setWtBaseRepo(null)');- expect(handler).toContain("setWtSourceBranch('')");- expect(handler).toContain("setWtName('')");++ expect(handler).toContain('applyDraftTarget({');++ expect(action).toContain('if (deviceChanged || workingDirChanged)');++ expect(action).toContain('setWtBaseRepo(null)');++ expect(action).toContain("setWtSourceBranch('')");});it('wires the remote project entry into the CREATE AGENT mode-pill picker', () => {@@@ -225,9 -188,1038 +252,1040 @@@expect(addRemoteProjectDialogSource).toContain('sourceGroupSsh');expect(addRemoteProjectDialogSource).toContain('sourceGroupDevice');expect(addRemoteProjectDialogSource).not.toContain('res.hosts.filter((h) => h.autoConnect)');- // 归属一致:device-link 建会话参数走纯函数 buildDeviceLinkCreateArgs(workspaceKind:'project'),- // 行为由 deviceLinkCreateArgs.test.ts 断言;此处锁「route 确实经该纯函数」,防有人再内联错 workspaceKind。- expect(newMakerDraftRouteSource).toContain('buildDeviceLinkCreateArgs({');+ // 归属一致:device-link 建会话参数只经 resolveDeviceLinkSubmission 组装(它内部转调+ // buildDeviceLinkCreateArgs 派生 workspaceKind),行为由 deviceLinkCreateArgs.test.ts 断言;+ // 此处锁「route 确实经那个唯一入口」,防有人再内联错 workspaceKind 或绕过来源校准。+ expect(newMakerDraftRouteSource).toContain('resolveDeviceLinkSubmission({');+ // 组件不得直接调底层组装函数 —— 那样就绕开了来源校准这一步(第 25 轮缺陷的形状)。+ expect(newMakerDraftRouteSource).not.toContain('buildDeviceLinkCreateArgs({');+ });++ // #807:设备切换 pill。三条产品裁决写进源码断言,防后续重构悄悄改掉。+ it('wires the device switcher pill and keeps it invisible without paired devices', () => {+ expect(newMakerDraftRouteSource).toContain(+ 'const { devices: selectableDevices, loaded: selectableDevicesLoaded } = useSelectableDevices();',+ );+ expect(newMakerDraftRouteSource).toContain('<DeviceSwitcherPill');+ // 没有对端设备 → 组件自己返回 null,只有本机的用户看不到任何新增控件。+ expect(deviceSwitcherPillSource).toContain('if (devices.length === 0) return null');+ // 离线设备列出但禁用 —— 掉线时从列表消失会让用户以为配对丢了。+ expect(deviceSwitcherPillSource).toContain('disabled={!device.online}');+ // 换设备后停在这台设备的「对话」:上一台的项目路径在新机器上基本不存在,+ // 留着会让用户以为项目跟过来了、发送时才在被控端 path guard 上失败。+ expect(newMakerDraftRouteSource).toContain('const handleDeviceChange = useCallback(');+ expect(newMakerDraftRouteSource).toContain(+ 'applyDraftTarget({ deviceId, deviceName, workingDir: null });',+ );+ });++ // #807:跨设备纯对话。放宽后「选了设备」单独成立即可整套走对端(能力/provider/创建同口径),+ // 修掉「模型列表来自对端、会话却建在本机」的不一致。+ it('treats a picked device alone as a device-link draft so cross-device dialogues work', () => {+ expect(newMakerDraftRouteSource).toContain(+ 'const isDeviceLinkDraft = effectiveDeviceLinkDeviceId != null',+ );+ expect(newMakerDraftRouteSource).toContain(+ 'if (isDeviceLinkDraft && effectiveDeviceLinkDeviceId) {',+ );+ // 远程纯对话没有 repo:即使 wtEnabled 残留 true 也必须跳过 worktree 分支。- expect(newMakerDraftRouteSource).toContain('if (effectiveWorkingDir && wt.enabled) {');++ expect(newMakerDraftRouteSource).toContain(++ 'if (effectiveWorkingDir && wt.enabled && wt.baseRepo) {',++ );+ });++ // #807 review 修复:新建目标必须与普通发送同口径 —— 远程纯对话下不能因为缺 workingDir 就抛错。+ it('lets goal creation accept a device-only draft (same shape as normal send)', () => {+ expect(newMakerDraftRouteSource).not.toContain(+ 'if (!effectiveDeviceLinkDeviceId || !effectiveWorkingDir) {',+ );+ expect(newMakerDraftRouteSource).toContain('if (!effectiveDeviceLinkDeviceId) {');+ expect(newMakerDraftRouteSource).toContain('workingDir: effectiveWorkingDir ?? undefined,');+ });++ // #807 review 修复:换工作区必须显式回传当前设备。store 的不变量是「改 workingDir 又不带+ // 设备字段就清设备」,不显式带会让选「对话」/换项目把设备悄悄清回本机。+ it('carries the current device when only the workspace changes', () => {+ // 工作区 picker 原样回传当前设备 → 转移动作判出 deviceChanged=false,只处理「换项目」那一半。+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleWorkingDirChange = useCallback('),+ );+ const body = handler.slice(0, handler.indexOf(' [applyDraftTarget'));+ expect(body).toContain('deviceId: draft.deviceLinkDeviceId,');+ expect(body).toContain('workingDir: dir,');+ // 而那个动作**总是**显式带上设备字段,所以这条不变量对四条路径一次性成立,+ // 不再依赖每个调用方各自记得拼一个 keepDevice。+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ expect(action).toContain('deviceLinkDeviceId: req.deviceId,');+ expect(action).toContain('deviceLinkDeviceName: req.deviceName,');+ });++ // #807 review 修复:设备真正从可选列表消失时把草稿收敛回本机,避免显示与实际目标不一致。+ it('falls back to local when the selected device is no longer selectable', () => {+ expect(newMakerDraftRouteSource).toContain(+ 'if (selectableDevices.some((d) => d.deviceId === effectiveDeviceLinkDeviceId)) return;',+ );+ // 判据是「拉到过权威快照」而非「列表非空」—— 详见 distinguishes a loaded-empty… 用例。+ expect(newMakerDraftRouteSource).toContain('if (!selectableDevicesLoaded) return;');+ });++ // #807 review 修复:远程删除失败 + 权威回读也失败时,必须把行放回去,不留幻影删除。+ it('restores the optimistically removed row when both remove and reload fail', () => {+ expect(deviceLinkProjectsHookSource).toContain('const restored = removedRow;');+ expect(deviceLinkProjectsHookSource).toContain('removedIndex');+ });++ // #807 review 第二轮:远程设备语境下「选择其他项目文件夹」不能开本机原生目录对话框 ——+ // 选出来的是控制端路径,配上远程 deviceId 发送时要么被 path guard 拒,要么在对端一个+ // 毫不相关的同名目录里建会话。+ it('routes folder browsing through the selected device instead of the local native picker', () => {+ expect(folderPickerPopoverSource).toContain('if (deviceScope) {');+ expect(folderPickerPopoverSource).toContain('onAddRemoteProject?.(deviceScope.deviceId);');+ });++ /**+ * 2026-07-30 用户真机反馈:选定远程设备后不该再出现「添加远程项目」。+ *+ * 不只是文案冗余 —— 那一项调 `onAddRemoteProject()` **不带 deviceId**,弹窗会让用户从头重选+ * 目标,于是可以从设备 A 的语境里点一个叫「添加远程项目」的入口、选到设备 B,等于绕过设备 pill+ * 改掉了设备这一级维度,而 #807 方案 B 的前提正是「设备由设备 pill 独占」。+ * 同一语境下「选择其他项目文件夹」已经承担了浏览对端文件夹的职责(见上一条用例)。+ */+ it('hides the add-remote-project entry once a device is selected', () => {+ // 条件里必须有 !deviceScope —— 这是这条行为的全部实现。+ expect(folderPickerPopoverSource).toContain(+ '{isProjectPicker && onAddRemoteProject && !deviceScope && (',+ );+ // 那一项就是不带设备身份的那次调用;它只应出现在本机语境。+ expect(folderPickerPopoverSource).toContain('onAddRemoteProject();');+ // 空态入口不受影响:仍然渲染,且**带**设备身份(否则又变成一个能换设备的入口)。+ const emptyState = folderPickerPopoverSource.slice(+ folderPickerPopoverSource.indexOf('deviceScope && onAddRemoteProject ? ('),+ );+ expect(emptyState.slice(0, emptyState.indexOf(') : ('))).toContain(+ 'onAddRemoteProject(deviceScope.deviceId);',+ );+ // 上层仍要在已选设备时下发 onAddRemoteProject —— 入口 1 与空态入口都靠它,+ // 只是那个 Globe 项不再渲染。别顺手把这个 gate 一起收掉。+ expect(newMakerDraftRouteSource).toContain(+ 'hasAnyRemoteTarget || folderPickerDeviceScope ? handleOpenRemoteProject : undefined',+ );+ });++ // #807 review 第二轮:空列表必须区分「还没拉到」与「拉到了确实没有」。唯一对端被解除配对时+ // 列表会合法变空,若按「非空」gate,回落永远不触发,草稿会永久指着一台已消失的设备。+ it('distinguishes a loaded-empty device snapshot from a not-yet-loaded one', () => {+ expect(controllableDevicesHookSource).toContain(+ 'export function useSelectableDevices(): { devices: SelectableDevice[]; loaded: boolean }',+ );+ // 拉取失败(device-link 不可用)的空不作数,不得据此清掉用户选定的设备。+ expect(controllableDevicesHookSource).toContain('setLoaded(false);');+ expect(newMakerDraftRouteSource).toContain('if (!selectableDevicesLoaded) return;');+ expect(newMakerDraftRouteSource).not.toContain('if (selectableDevices.length === 0) return;');+ });++ // #807 review 第二轮:换机器 = 换文件系统,@file/@dir chip 指的是上一台机器的路径。+ it('strips filesystem mention chips when switching devices', () => {+ expect(newMakerDraftRouteSource).toContain(+ 'const composerDraft = getComposerDraft(NEW_MAKER_DRAFT_KEY);',+ );+ expect(newMakerDraftRouteSource).toContain('text: stripLocalMentionChips(composerDraft.text),');+ });++ // #807 review 第三轮:点已选中的那一行只是确认当前选择,不能有副作用 —— 否则用户点一下+ // 就静默丢掉已选项目和部分已写好的消息(mention chip 被剥、workingDir/extraDirs 被清)。+ it('ignores reselecting the current device before touching either draft store', () => {+ expect(newMakerDraftRouteSource).toContain(+ 'if (deviceId === (effectiveDeviceLinkDeviceId ?? null)) return;',+ );+ });++ // #807 review 第三轮:并发删除时恢复不能按 requestId gate —— 删除按钮不禁用,快速删两行会让+ // 第二次重写共享 requestIdRef,第一次的恢复被跳过,那一行会一直从选择器消失(对端其实还在)。+ it('restores concurrently removed rows without gating on the shared request id', () => {+ const tail = deviceLinkProjectsHookSource.slice(+ deviceLinkProjectsHookSource.indexOf('const restored = removedRow;'),+ );+ expect(tail).not.toContain('requestIdRef.current !== requestId');+ // 靠插回前的存在性检查保证幂等,而不是靠版本号。+ expect(deviceLinkProjectsHookSource).toContain(+ 'if (current.some((row) => row.path === restored.path)) return;',+ );+ });++ // #807 review 第十三 / 二十三轮:「标着 B 的 A 的项目」这条最初靠「切设备时立刻清空 rows」挡,+ // 但那依赖 effect 先于渲染 —— passive effect 在 paint 之后才跑,于是仍有一帧会把 A 的路径包成+ // 属于 B 的可点击选项(Greptile 抓到)。现在改成结构性保证:行连同归属设备一起存,memo 只在+ // 归属相符时输出。逐帧行为断言在 deviceLinkProjectsConcurrency.test.tsx。+ it('binds loaded rows to their owning device so a mismatch cannot be rendered', () => {+ expect(deviceLinkProjectsHookSource).toContain('const [loaded, setLoaded] = useState<{');+ // 写入必须申明归属 —— 没有「只改 rows 不改归属」的调用形态。+ expect(deviceLinkProjectsHookSource).toContain(+ 'const commitRows = useCallback((ownerDeviceId: string | null, next: ExistingRemoteProject[])',+ );+ // memo 的归属守卫:这是把 deviceId 绑进状态的唯一目的。+ expect(deviceLinkProjectsHookSource).toContain('deviceId && loaded.deviceId === deviceId');+ // 归属没对上时仍算加载中,避免切设备那一帧闪「没有项目」空态。+ expect(deviceLinkProjectsHookSource).toContain(+ 'const effectiveLoading = loading || (deviceId != null && loaded.deviceId !== deviceId);',+ );+ });++ // #807 review 第十四轮:恢复路径不能依赖 React 的调度时机。`setRows(updater)` 的 updater+ // 只在 React 处理更新时才跑,而「删除失败 + 回读失败」的恢复要在两次 await 之间就拿到被移除+ // 的行 —— 从 updater 副作用取值可能读到 undefined(Copilot review),`if (!restored) return`+ // 于是把恢复整个跳过,幻影删除又回来了。改由同步镜像 rowsRef 供数,写入统一走 commitRows。+ it('captures the removed row from a synchronous mirror, not a setState updater side effect', () => {+ expect(deviceLinkProjectsHookSource).toContain('const loadedRef = useRef<{');+ expect(deviceLinkProjectsHookSource).toContain(+ 'loadedRef.current = { deviceId: ownerDeviceId, rows: next };',+ );+ // 读镜像时同时校验归属:归属不符说明当前显示的已是别的设备的行,乐观移除会改错列表。+ expect(deviceLinkProjectsHookSource).toContain(+ 'const before = loadedRef.current.deviceId === target.deviceId ? loadedRef.current.rows : [];',+ );+ // 被移除的行与位置都从镜像同步算出,不再靠 updater 的副作用赋值。+ expect(deviceLinkProjectsHookSource).toContain(+ 'const removedIndex = before.findIndex((row) => row.path === option.path);',+ );+ // 状态只由 commitRows 写(唯一一处 `setLoaded(loadedRef.current)`)。留任何一个裸 setLoaded+ // 就还有一条镜像与状态不同步、或归属未申明的路。+ const bareSet =+ deviceLinkProjectsHookSource.match(/setLoaded\((?!loadedRef\.current\))/g) ?? [];+ expect(bareSet.length).toBe(0);+ });++ // #807 review 第十五轮:picker 换项目也必须作废 worktree 三态。baseRepo 由 WorktreeChipsRow+ // 经 detect-cwd 异步回填(远程还要走隧道),回填前发送会把 worktree 建到上一个 repo;+ // sourceBranch 只在为空时才自动填充,用户在 A 上显式选的分支会一直跟到 B。浏览器路径早就重置+ // 了,picker 路径漏了 —— 而 picker 才是 #807 之后换项目的主路径。+ it('invalidates worktree state when the project picker switches workspaces', () => {+ // 换工作区 = 换 repo,worktree 三态必须一起作废;但只在路径真的变了时 —— 重选当前项目不该+ // 把用户刚打开的 worktree 开关又关掉。这条判据原先只补进了工作区 picker,设备域浏览器那条+ // 路径漏了(它无条件重置);收敛之后两条都由同一个条件驱动。+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ const wt = action.slice(action.indexOf('if (deviceChanged || workingDirChanged) {'));+ const block = wt.slice(0, wt.indexOf(' }'));+ expect(block).toContain('setWtEnabled(false);');+ expect(block).toContain('setWtBaseRepo(null);');+ expect(block).toContain("setWtSourceBranch('');");+ });++ // #807 review 第十四轮:compact 模式下按钮只剩图标 + 状态点,不渲染设备名 —— aria-label 只报+ // 「设备」的话读屏用户无从得知当前选的是哪台机器。+ it('announces the selected device in the switcher aria-label', () => {+ expect(deviceSwitcherPillSource).toContain(+ "const triggerLabel = `${t('newChat.deviceSwitcher.label')}: ${label}`;",+ );+ expect(deviceSwitcherPillSource).toContain('aria-label={triggerLabel}');+ });++ // #807 review 第十四轮:注释与实现必须一致 —— 早前几轮把「指名设备不在目标里就留空」改对了,+ // 但 JSDoc 还写着 falls back to the first available target,会误导后续维护者改回静默换机器。+ it('documents that an explicitly requested device never falls back', () => {+ expect(addRemoteProjectDialogSource).not.toContain('falls back to the first available target');+ expect(addRemoteProjectDialogSource).toContain('**指名了就只认这一台**');+ });++ // #807 review 第十三轮:同一台机器上换项目不得重置运行配置与引用目录 —— 上一轮只 gate 了+ // mention chip,dlSel 与 extraDirs 仍被无条件打回默认值,用户选的远程模型/来源/权限和加好的+ // 引用目录会静默丢失。+ it('preserves runtime selection and extra dirs when browsing the same device', () => {+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ // dlSel 只在换设备时重种;同机保留用户选择(下一条断言它还会按新能力重校)。+ expect(action).toContain('if (deviceChanged) {\n setDlSel(\n');+ // extraDirs 只在换设备、或进入「对话」时清 —— 同机换项目那些目录仍然有效,+ // 不传则 store 保持原值。+ expect(action).toContain(+ '...(deviceChanged || req.workingDir == null ? { extraDirs: [] } : {}),',+ );+ // 但 worktree 三态照常重置 —— 换项目就是换 repo。+ expect(action).toContain('if (deviceChanged || workingDirChanged) {');+ });++ // #807 review 第十二轮:in-flight 保护要覆盖**工作区** pill,不只是设备 pill —— 否则用户点了+ // Send 还能从远程项目 X 切到 Y,会话建在 X 里、刚选的 Y 又被 create 后的重置清掉。+ it('disables and guards workspace switching while a send is in flight', () => {+ // 两个 pill 都要禁用。+ expect(+ (newMakerDraftRouteSource.match(/disabled=\{wtCreating \|\| sendInFlight\}/g) ?? []).length,+ ).toBe(2);+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleModePickerSelect = useCallback('),+ );+ expect(handler.slice(0, handler.indexOf('handleWorkingDirChange('))).toContain(+ 'if (sendInFlightRef.current) return;',+ );+ });++ /**+ * ⚠️ 这条的结论在第 29 轮被**推翻**了,保留原委作为记录。+ *+ * 第十二轮我判断「同一台机器上换项目不该剥 mention chip —— 文件系统没变」,并据此加了 gate。+ * 那个判断建立在一个错误前提上:以为 chip 存绝对路径。实际上 ChatInput 把 file/dir chip 的+ * `path` 存成**项目相对**路径(`attrs.path = item.relPath`,源码注释原文「for files/dirs we+ * stash the relative path as-is」),agent chip 是 `.claude/agents/<name>.md`,同样相对。+ *+ * 于是解析基准是 workingDir 而不是文件系统:同机从项目 X 换到 Y,`@src/foo.ts` 会解析到 Y 里的+ * 同名文件。这比换设备更隐蔽 —— `src/index.ts`、`.claude/agents/reviewer.md` 这类路径在同机两个+ * 项目间恰好都存在的概率相当高,agent 于是读到毫不相关的内容而没有任何报错。+ *+ * 现在 chip 按 `deviceChanged || workingDirChanged` 剥;而路径型**附件**(存绝对路径)仍然只在+ * 换设备时丢 —— 原先两件事共用一个条件,所以必然有一边是错的。+ */+ it('strips project-relative mention chips whenever the project changes, not just the device', () => {+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ expect(action).toContain(+ 'if (deviceChanged || workingDirChanged) stripProjectRelativeMentions();',+ );+ // 附件是另一个条件 —— 别再把这两件事合回一个 gate。+ expect(action).toContain('if (deviceChanged) dropPathBackedAttachments();');+ // 拆分后的两个函数各自绑住自己的解析基准,注释里写明了理由。+ expect(newMakerDraftRouteSource).toContain('const stripProjectRelativeMentions = useCallback(');+ expect(newMakerDraftRouteSource).toContain('const dropPathBackedAttachments = useCallback(');+ // 旧的合并式 helper 必须消失,否则又会有人按单一条件调它。断言声明与调用两种形态,+ // 而不是裸名字 —— 拆分后的函数注释里会提到这个旧名作为历史记录。+ expect(newMakerDraftRouteSource).not.toContain('const cleanupCrossFilesystemDraftContext');+ expect(newMakerDraftRouteSource).not.toContain('cleanupCrossFilesystemDraftContext()');+ });++ // #807 review 第十二轮:pending 删除集合按设备分层,否则 A 上未结束的 /x 会被当成 B 的待删除项,+ // B 上同名 /x 会被权威列表错误过滤掉。+ it('scopes pending removals per device', () => {+ expect(deviceLinkProjectsHookSource).toContain('useRef<Map<string, Set<string>>>(new Map())');+ expect(deviceLinkProjectsHookSource).toContain(+ 'pendingRemovalsRef.current.get(target.deviceId)',+ );+ });++ // #807 review 第十一轮:并发删除时,失败删除的权威回读不能复活另一个仍在飞的乐观删除+ // (B 被复活后,它真的成功时成功路径不再更新状态,于是 B 一直显示到重开 picker)。+ it('preserves other in-flight deletions when a failed removal reloads', () => {+ expect(deviceLinkProjectsHookSource).toContain('pendingRemovalsRef');+ expect(deviceLinkProjectsHookSource).toContain('devicePending.add(option.path);');+ // 减去其它 pending,但不含自己 —— 这次删除失败了,真相里有它就该显示回来。+ expect(deviceLinkProjectsHookSource).toContain('(path) => path !== option.path,');+ // finally 必须清除,否则一次异常会让那条 path 永久被过滤掉。+ expect(deviceLinkProjectsHookSource).toContain('set?.delete(option.path);');+ });++ // #807 review 第十一轮:调用方指名了设备时,弹窗不得回落到别的目标 —— 被指名的那台离线时+ // targets 里没有它,静默落到 targets[0](可能是 SSH 主机或另一台设备)会把草稿切到意外的机器。+ it('never falls back to a different target when a device was explicitly requested', () => {+ expect(addRemoteProjectDialogSource).toContain('if (preferredKey) {');+ expect(addRemoteProjectDialogSource).toContain(+ 'setSelectedKey(targets.some((target) => target.key === preferredKey) ? preferredKey : null);',+ );+ });++ // #807 review 第十轮:两个创建 guard 必须把 remoteDraftState.loaded 一起看。换设备时我们把它+ // 打回未加载(防上一台默认值串台),而 capabilities/providers 若已缓存则那两个 loading 立刻为+ // false —— 只看它们会在 maker:get-new-maker-defaults 回来前放行,提交 capability 兜底值而不是+ // 该设备保存的草稿值,会话建出来后晚到的响应也修不回去。+ it('waits for remote defaults before allowing send or goal creation', () => {+ const guards =+ newMakerDraftRouteSource.match(+ /capabilitiesLoading \|\| deviceProvidersLoading \|\| !remoteDraftState\.loaded/g,+ ) ?? [];+ expect(guards.length).toBe(2);+ expect(newMakerDraftRouteSource).not.toContain(+ 'if (isDeviceLinkDraft && (capabilitiesLoading || deviceProvidersLoading)) return false;',+ );+ });++ // #807 review 第九轮:设备列表刷新要按请求序号丢弃过期响应。首次加载与两个监听会并发调+ // refresh,REST 响应可能乱序 —— 更早的 listDevices 晚到会把新的权威快照覆盖掉,把刚被解除配对+ // 的设备连同 loaded=true 一起写回来,于是回落认为目标仍有效、picker 也允许再次选中它。+ it('discards superseded device-list refreshes', () => {+ const hook = controllableDevicesHookSource.slice(+ controllableDevicesHookSource.indexOf('export function useSelectableDevices()'),+ );+ const body = hook.slice(0, hook.indexOf('export function useControllableDevices()'));+ expect(body).toContain('const requestId = requestIdRef.current + 1;');+ // 成功与失败两条路径都要 gate,否则过期的失败会误把 loaded 打回 false。+ expect(+ (body.match(/requestIdRef\.current !== requestId/g) ?? []).length,+ ).toBeGreaterThanOrEqual(2);+ });++ // #807 review 第八轮:两处对称性缺口,都是前几轮修复的直接后果。+ it('resets worktree state during the automatic local fallback too', () => {+ // 远程项目开过 worktree、设备随后被解除配对 → wtEnabled/wtBaseRepo 残留 → 下一次本机发送会进+ // worktree 分支,拿上一台设备的仓库路径去建。+ //+ // 回落路径历史上漏得最多(chip、附件、worktree 三态各漏过一次),而且它原先还**隐式**依赖+ // seed effect 的 !isDeviceLinkDraft 分支去清远程运行配置 —— 能跑,但没人能一眼看出为什么。+ // 现在它只是「转移到 本机 + 对话」,所有连带清理由同一个动作按 deviceChanged 推导。+ const effect = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('selected device is no longer selectable'),+ );+ const body = effect.slice(0, effect.indexOf(' }, ['));+ expect(body).toContain(+ 'applyDraftTarget({ deviceId: null, deviceName: null, workingDir: null });',+ );+ // 依赖数组要带上那个动作,否则闭包吃旧的 draft.workingDir / attachments。+ expect(effect.slice(effect.indexOf(' }, ['), effect.indexOf(' }, [') + 200)).toContain(+ 'applyDraftTarget',+ );+ });++ it('gates the failed-delete restoration by current device as well', () => {+ // 上一轮为修并发删除去掉了 requestId gate,但没补设备 gate:请求在飞时切到别的设备,+ // 会把 A 的行插进 B 的列表并被标成属于 B —— 选中它就把 A 的路径发给 B。+ const restore = deviceLinkProjectsHookSource.slice(+ deviceLinkProjectsHookSource.indexOf('const restored = removedRow;'),+ );+ expect(restore.slice(0, restore.indexOf('commitRows('))).toContain(+ 'if (currentDeviceIdRef.current !== target.deviceId) return;',+ );+ });++ // #807 review 第六轮:发送在途时不能换设备 —— 那次调用的闭包持有旧设备,draft 却切到新设备,+ // 结果会话建在旧设备上并导航过去,同时把刚选的新设备上下文重置掉。+ it('rejects and disables device switching while a send is in flight', () => {+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleDeviceChange = useCallback('),+ );+ // ref 而非 state:必须即时可读,不能等下一次渲染。+ expect(handler.slice(0, handler.indexOf('patchDraft('))).toContain(+ 'if (sendInFlightRef.current) return;',+ );+ // 同时用同步的 state 禁用 pill(ref 变化不触发渲染)。+ expect(newMakerDraftRouteSource).toContain('disabled={wtCreating || sendInFlight}');+ // ref 与 state 必须一起改,所以赋值统一走 helper。+ expect(newMakerDraftRouteSource).toContain('const markSendInFlight = useCallback(');+ expect(newMakerDraftRouteSource).not.toContain('sendInFlightRef.current = true;');+ });++ // #807 review 第五轮:换设备必须同步失效上一台的远程默认值快照,否则 seed effect 会拿旧+ // capabilities/defaults 种下新设备的 dlSel 并把它记成「已 seed」,新设备真值到达后又被 guard+ // 挡住重种 —— composer 于是向新设备提交上一台的 model / provider / permission。+ it('invalidates the previous device remote-default snapshots before switching', () => {+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ const body = action.slice(0, action.indexOf(' patchDraft({'));+ // 没有 inline 快照(设备 pill / 回落两条路径)且换了设备 → 打回未加载,交给 seed effect。+ const fallback = body.slice(body.indexOf('} else if (deviceChanged) {'));+ expect(fallback).toContain('setDlSel(null);');+ expect(fallback).toContain('dlSeedKeyRef.current = null;');+ expect(fallback).toContain('setRemoteDraftState({ loaded: false, value: null });');+ // 有 inline 快照(设备域浏览器)则当场 seed,并置 skip flag 避免 effect 再拉一次覆盖掉。+ expect(body).toContain('skipDefaultsRefetchRef.current = true;');+ });++ // #807 review 第四轮:四个死角,都是前几轮修复留下的。+ it('never opens the local picker in a remote scope, even without onAddRemoteProject', () => {+ // 上层按 hasAnyRemoteTarget 下发 onAddRemoteProject,而选中的对端离线且是唯一远程目标时+ // 那个 gate 会变 false —— 判据必须只看 deviceScope,否则又落回本机原生对话框。+ expect(folderPickerPopoverSource).toContain('if (deviceScope) {');+ expect(folderPickerPopoverSource).toContain('onAddRemoteProject?.(deviceScope.deviceId);');+ // 已选定设备时无条件下发入口(设备离线也要能浏览它)。+ expect(newMakerDraftRouteSource).toContain(+ 'hasAnyRemoteTarget || folderPickerDeviceScope ? handleOpenRemoteProject : undefined',+ );+ });++ it('strips remote mention chips during the automatic local fallback too', () => {+ // 回落时草稿里对着远程机器建的 @file/@dir 必须剥掉,否则下一次本机发送被当成本机路径送进去。+ // 这条以前要求回落 effect **自己**调清理(它绕过了两个显式 handler);现在它走同一个转移动作,+ // 于是「回落 = deviceChanged」这一个事实就同时保证了 chip 与附件都被处理 —— 不必再逐条盯。+ const effect = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('selected device is no longer selectable'),+ );+ expect(effect.slice(0, effect.indexOf(' }, ['))).toContain(+ 'applyDraftTarget({ deviceId: null',+ );+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ expect(action).toContain(+ 'if (deviceChanged || workingDirChanged) stripProjectRelativeMentions();',+ );+ });++ it('keeps the last known device rows when listDevices fails', () => {+ // 清空会造成死角:选了远程设备后一次瞬时失败就让 pill 返回 null,而回落 effect 又(正确地)+ // 因为空不权威而不动草稿 —— 草稿仍指着那台设备,UI 上却没有控件能切回本机。+ const catchBlock = controllableDevicesHookSource.slice(+ controllableDevicesHookSource.indexOf('**保留上次已知的设备行**'),+ );+ const untilEnd = catchBlock.slice(0, catchBlock.indexOf('};'));+ expect(untilEnd).toContain('setLoaded(false);');+ expect(untilEnd).not.toContain('setDevices(');+ });++ it('gates the post-delete authoritative reload on device identity, not the shared request id', () => {+ // requestIdRef 被 effect 取数与每次删除共享,快速删两行会让第一次成功的回读被丢弃。+ expect(deviceLinkProjectsHookSource).toContain(+ 'if (currentDeviceIdRef.current !== target.deviceId) return;',+ );+ });++ // #807:设备 popover 宽度自适应内容 + 上限截断(2026-07-29 用户裁决)。+ // 不写死固定宽(会无理由地比 trigger 宽),也不绑 trigger 宽度(trigger 只有 80–200px 且随+ // 设备名浮动,绑上去会把设备名 + 状态点 + 离线副文案全挤没)。行内 truncate 负责有限展现。+ it('sizes the device popover to its content with an upper bound, truncating long names', () => {+ expect(deviceSwitcherPillSource).toContain('w-auto min-w-[200px] max-w-[320px]');+ // 截断链路:可收缩的 body + 名字/副文案 truncate + 图标与 check 不参与收缩。+ expect(deviceSwitcherPillSource).toContain('flex min-w-0 flex-1 flex-col items-start');+ expect(deviceSwitcherPillSource).toContain('min-w-0 truncate text-sm font-medium');+ });++ // #807 review 第三轮:能力缓存命中时必须清掉上一目标遗留的 loading —— 漏了会让+ // capabilitiesLoading 永久为 true,而创建页的 send / goal guard 正是看它。+ it('clears inherited loading state when the capability cache hits', () => {+ const cachedBranch = agentCapabilitiesHookSource.slice(+ agentCapabilitiesHookSource.indexOf(+ 'const cached = cache.get(cacheKey(agentKind, deviceId));',+ ),+ );+ const untilReturn = cachedBranch.slice(0, cachedBranch.indexOf('return;'));+ expect(untilReturn).toContain('setLoading(false);');+ expect(untilReturn).toContain('setError(null);');+ });++ // #807 review 第十四轮:被控端 maker:create-session 一返回 sessionId 就是**提交点**。原来两处+ // 远程分支手写 invoke('local-db:sessions:list') + setDeviceSessions 做镜像回流,隧道一抖(被控端+ // DB 刚启动未就绪 / 链路瞬断 / 超时)就抛 —— handleSend 落进外层 catch 报「创建失败」、+ // handleCreateGoal 让 NewGoalDialog 内联报错并保持打开,两者都会让用户重试,于是对端多出第二个+ // 会话、第一个空着永久滞留。回流必须走 refreshRemoteDeviceSessions:它不抛(瞬态退避重试、+ // 永久错误返回 'gave-up'),且认 snapshot epoch、有界快照按 merge 落库。+ it('routes post-create mirror refresh through the non-throwing shared helper', () => {+ // 回流本体现在住在 commitRemoteSessionHandoff 里(见下一条断言:两条路径都只调它),+ // 且是 fire-and-forget —— 见 does not block the handoff on the mirror refresh 那条用例。+ expect(remoteSessionHandoffSource).toContain(+ 'void refreshRemoteDeviceSessions(p.deviceId, p.deviceName)',+ );+ // 手写回流必须彻底消失,否则提交点后仍有可抛的一步。+ expect(newMakerDraftRouteSource).not.toContain("'local-db:sessions:list'");+ expect(newMakerDraftRouteSource).not.toContain('setDeviceSessions(');+ // 组件也不该再自己 import 回流函数 —— 它只经 handoff 使用。+ expect(newMakerDraftRouteSource).not.toContain('refreshRemoteDeviceSessions');+ });++ // #807 review 第十七轮:归属必须在**回流之前**登记。回流失败(gave-up / superseded)时镜像里+ // 没有这条会话,getSessionDeviceId 返回 undefined,makerApiFor / goalApiFor 就把首条消息与+ // setGoal 发给本机 maker。行为契约本身由 remoteProjectsStore.test.ts 覆盖,这里只钉接线顺序。+ it('pins the remote session origin before refreshing the mirror', () => {+ // 顺序不变量现在只有一处可改。原来这条断言在组件源码上数「pin 出现 2 次」并逐处校验顺序 ——+ // 那锁的是**重复本身**:两处逐字重复的代码,漏改一处不会有任何编译或测试信号,而这三条+ // 不变量恰好各自都曾只在一条路径上被修好过(第 14 / 17 / 20 轮)。收敛之后改锁两件事:+ // ① handoff 内部三步顺序正确;② 两条创建路径都只经 handoff(见下一条)。+ const pinAt = remoteSessionHandoffSource.indexOf(+ 'remoteProjectsStore.pinSessionOrigin(p.deviceId, p.remoteSessionId)',+ );+ const rowAt = remoteSessionHandoffSource.indexOf('remoteProjectsStore.mergeDeviceSessions(');+ const refreshAt = remoteSessionHandoffSource.indexOf('void refreshRemoteDeviceSessions(');+ expect(pinAt).toBeGreaterThan(-1);+ // 钉子必须在临时行之前,临时行必须在触发回流之前。+ expect(rowAt).toBeGreaterThan(pinAt);+ expect(refreshAt).toBeGreaterThan(rowAt);+ });++ /**+ * #807 review 第 33 轮 P1:回流**不得**挡在 setPending / navigate 前面。+ *+ * refreshRemoteDeviceSessions 对瞬态错误退避重试,窗口最长约 6.75 秒。原来 handoff `await` 它才+ * 返回,于是这段时间里应用被关掉 → 对端**已经**有了新会话,而用户的首条消息(或目标弹窗里刚写的+ * 内容)还没被 setPending / setPendingGoal 记录下来 → 重开再试就在对端建出第二个会话,第一个空着+ * 滞留;建目标那条还会连同只存在于弹窗内存里的编辑一起丢。+ *+ * 交接本来就不需要等权威快照 —— 临时行(带对端真正分配的 workDir)已经足够让 SessionView 的+ * delayed-create 完成 consumePending。所以 handoff 改为同步返回、回流 fire-and-forget。+ */+ it('does not block the handoff on the mirror refresh', () => {+ // 同步签名:没有 async / 不返回 Promise。+ expect(remoteSessionHandoffSource).toContain(+ 'export function commitRemoteSessionHandoff(p: RemoteSessionHandoffParams): void {',+ );+ expect(remoteSessionHandoffSource).not.toContain(+ 'export async function commitRemoteSessionHandoff',+ );+ // 回流不被 await。+ expect(remoteSessionHandoffSource).not.toContain('await refreshRemoteDeviceSessions(');+ // 两处调用点都不得 await 它 —— await 一个同步函数不报错,但会把「不要等」这个意图悄悄改回去。+ expect(newMakerDraftRouteSource).not.toContain('await commitRemoteSessionHandoff(');+ // 而 setPending / setPendingGoal 必须在各自的 handoff 之后仍然发生(交接本体没被搬走)。+ const sendPart = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf("logTag: 'draft send'"),+ );+ expect(sendPart.slice(0, sendPart.indexOf('navigate('))).toContain(+ 'setPending(remoteSessionId',+ );+ const goalPart = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf("logTag: 'draft goal'"),+ );+ expect(goalPart.slice(0, goalPart.indexOf('navigate('))).toContain(+ 'setPendingGoal(remoteSessionId',+ );+ });++ // 替代原先「两处各自都得有 pin + merge + refresh」的三组计数断言。锁的东西没减少反而更强:+ // 那三条不变量的**内容**由上一条断言在 handoff 里钉死,这里只需要保证没有哪条创建路径绕过它。+ // 将来第三条远程创建路径(例如侧边栏直建)漏调 handoff 时,这条会直接失败。+ it('routes every remote create path through the shared handoff', () => {+ const handoffCalls =+ newMakerDraftRouteSource.match(/\n\s+commitRemoteSessionHandoff\(\{/g) ?? [];+ expect(handoffCalls.length).toBe(2);+ // 组件不得自己碰这三步中的任何一步 —— 那就等于又开了一条绕过不变量的路。+ expect(newMakerDraftRouteSource).not.toContain('pinSessionOrigin(');+ expect(newMakerDraftRouteSource).not.toContain('mergeDeviceSessions(');+ expect(newMakerDraftRouteSource).not.toContain('buildProvisionalRemoteSession(');+ // 两处都得把实际提交的 args 交给 handoff:临时行按它组装,不各自再推一遍+ // model / permission / workspaceKind。+ expect((newMakerDraftRouteSource.match(/\n\s+createArgs,\n/g) ?? []).length).toBe(2);+ // workDir 取 create 响应(纯对话的运行目录由对端分配,控制端猜不到)。+ expect((newMakerDraftRouteSource.match(/workDir: created\?\.workDir,/g) ?? []).length).toBe(2);+ });++ // #807 review 第十七轮:handleSend 的第一个 await 是协同策略重取,它在上锁之前 —— 期间两个+ // pill 仍可点,而本次调用的闭包持有旧设备 / 旧工作区,会话会建在旧目标上。+ it('takes the in-flight lock before the first await in handleSend', () => {+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleSend = useCallback('),+ );+ const lockAt = handler.indexOf('markSendInFlight(true);');+ const refreshAt = handler.indexOf('await collabPolicy.refresh()');+ expect(lockAt).toBeGreaterThan(-1);+ expect(refreshAt).toBeGreaterThan(lockAt);+ // finally 解锁:早退路径不必各自记得解锁(锁到真正上锁点之间是纯同步代码)。+ expect(handler.slice(lockAt, refreshAt + 400)).toContain('markSendInFlight(false);');+ });++ // #807 review 第十七轮:DESIGN.md §5 只允许 8px / 12px / 9999px 三档,6px 是明文禁止的中间值。+ // 这里取 pill —— §5 把 8px 限定为「小到无法戴 pill 的交互件」,24×24 图标按钮戴 pill 就是正圆。+ it('keeps the row remove affordance on an allowed radius tier', () => {+ expect(folderPickerPopoverSource).not.toContain('rounded-[6px]');+ expect(folderPickerPopoverSource).toContain(+ 'flex h-6 w-6 shrink-0 items-center justify-center rounded-full',+ );+ });++ // #807 review 第十七轮:换设备要一并丢掉**路径型**附件。attachment-path-passthrough 之后非图片+ // 附件只把 path 透传给模型,而 rehomeDraftAttachments 只重整图片 —— 上一台机器的路径会随首条+ // 消息发到新设备,读不到、或读到同路径下一个毫不相关的文件。+ it('drops path-backed attachments on every device switch', () => {+ const helper = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const dropPathBackedAttachments = useCallback('),+ );+ const body = helper.slice(0, helper.indexOf('}, ['));+ expect(body).toContain(".filter((f) => f.category !== 'image')");+ expect(body).toContain('attachmentState.removeFile(f.id)');+ // 图片不动:它们走 xdt-image:// 缓存,不依赖对端文件系统。+ expect(body).toContain("t('newChat.deviceSwitcher.attachmentsDropped'");+ // chip 剥离在**另一个**函数里(不同的解析基准,不同的触发条件)。+ expect(body).not.toContain('stripLocalMentionChips');+ expect(newMakerDraftRouteSource).toContain(+ 'const stripProjectRelativeMentions = useCallback(() => {',+ );+ // 全文恰好两处非图片过滤,各有明确分工:① 这个换设备时的同步清理;② 第二十四轮加的不变量+ // 收敛 effect(兜住在途摄入等所有入口)。出现第三处就说明又有人在某条路径上手写了一份。+ expect(+ (newMakerDraftRouteSource.match(/\.filter\(\(f\) => f\.category !== 'image'\)/g) ?? [])+ .length,+ ).toBe(2);+ expect(newMakerDraftRouteSource).toContain('「远程草稿绝不携带控制端路径附件」的**收敛器**');+ });++ /**+ * ─── 草稿运行目标的转移:两条主不变量 ────────────────────────────────────────+ *+ * 这两条替代了先前十余条按「路径 × 状态」逐格 pin 的断言。那种写法锁的是矩阵的每一格,+ * 而矩阵本身就是缺陷来源:4 条转移路径 × 9 处连带状态,漏掉一格既不会编译失败也不会有测试+ * 变红,#807 的 review 里约十轮都在补格子(切设备漏 worktree 三态、同机换项目误重置运行配置、+ * 指向设备前忘了作废快照、回落路径两样清理都漏、picker 换项目不作废 worktree……)。+ *+ * 收敛之后只需要锁两件事:① 四条路径都**只声明目标**,不自己做副作用;② 每处连带状态绑对了+ * 它真正依赖的那一半(设备 / 项目)。第五条路径出现时,①会直接失败,而②保证它自动是对的。+ */+ it('routes every draft-target transition through the single action', () => {+ // 四条路径:设备 pill、设备域浏览器选项目、工作区 picker、所选设备失效后的自动回落。+ // 声明本身是 `= useCallback(` 不匹配这个模式,所以数出来的就是调用点。+ const calls = newMakerDraftRouteSource.match(/applyDraftTarget\(\{/g) ?? [];+ expect(calls.length).toBe(4);+ // 组件里不得再有任何一处手写这些副作用 —— 手写一处就等于又开了一条绕过推导的路。+ // patchDraft 仍可出现(入场清 extraDirs、发送后复位),但不得再带设备字段。+ expect(newMakerDraftRouteSource).not.toContain('deviceLinkDeviceId: deviceId,');+ expect(newMakerDraftRouteSource).not.toContain('deviceLinkDeviceId: target.deviceId,');+ expect(newMakerDraftRouteSource).not.toContain('deviceLinkDeviceId: null,');+ // worktree 三态与三个 evict 只能出现在那个动作里(各 1 处)。+ // 注:`setRemoteDraftState({ loaded: false, … })` 不在此列 —— defaults effect 自己的早返回与+ // 重拉前重置也用它,那是它自身的正常逻辑,不是转移路径的重复实现。+ for (const marker of [+ 'setWtEnabled(false);',+ 'setWtBaseRepo(null);',+ 'evictDeviceCapabilities(',+ 'evictDeviceProviders(',+ 'evictDeviceGitSafetySettings(',+ ]) {+ const n = newMakerDraftRouteSource.split(marker).length - 1;+ expect({ marker, n }).toEqual({ marker, n: 1 });+ }+ });++ it('binds each connected state to the half of the target it actually depends on', () => {+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ const body = action.slice(0, action.indexOf(' patchDraft({'));+ // 变化判据本身。+ expect(body).toContain('const deviceChanged = req.deviceId !== prevDeviceId;');+ expect(body).toContain('const workingDirChanged = req.workingDir !== draft.workingDir;');+ // mention chip 存**项目相对**路径 → 设备或项目任一变化都要剥(第 29 轮 P1)。+ expect(body).toContain(+ 'if (deviceChanged || workingDirChanged) stripProjectRelativeMentions();',+ );+ // 路径型附件存**绝对**路径 → 只有换设备才失效,同机换项目不该丢用户的附件。+ expect(body).toContain('if (deviceChanged) dropPathBackedAttachments();');+ // 三个无 TTL 快照:绑「指向一台**新**设备」或「用户主动重新验证了这台设备」,+ // **不是**「指向设备就作废」—— 详见下面 does not evict… 那条用例的机制说明。+ expect(body).toContain('if (req.deviceId && (deviceChanged || req.remoteSnapshot)) {');+ expect(body).toContain('evictDeviceCapabilities(req.deviceId);');+ // worktree 三态绑 (设备, 项目) 二元组 = 绑 repo。+ expect(body).toContain('if (deviceChanged || workingDirChanged) {');+ // 判据读 draft.workingDir,必须在依赖数组里,否则闭包比的是上一次渲染的值。+ const deps = action.slice(action.indexOf(' [', action.indexOf('patchDraft({')));+ expect(deps.slice(0, deps.indexOf(' );'))).toContain('draft.workingDir,');+ });++ // #807 review 第十九轮:被控端能力 / 供应商 / Git safety 快照是「拉一次、无 TTL、只在设备下线+ // 才 evict」的,设备一直在线期间装了新模型或改了供应商,控制端不会知道 —— 切回它时 hook 的+ // effect 虽因 deviceId 变化重跑,却直接命中旧缓存,composer 会向它提交已不支持的 model /+ // provider。**每一条**把草稿指向某台被控设备的路径都必须先 evict。+ it('evicts the target device snapshots on every path that points the draft at a device', () => {+ // 本路由的转移路径共用一处 evict;条件见下一条用例(不是「指向设备就作废」)。+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ const evictBlock = action.slice(+ action.indexOf('if (req.deviceId && (deviceChanged || req.remoteSnapshot)) {'),+ );+ expect(evictBlock.slice(0, evictBlock.indexOf(' }'))).toContain(+ 'evictDeviceCapabilities(req.deviceId);',+ );+ expect(evictBlock.slice(0, evictBlock.indexOf(' }'))).toContain(+ 'evictDeviceProviders(req.deviceId);',+ );+ expect(evictBlock.slice(0, evictBlock.indexOf(' }'))).toContain(+ 'evictDeviceGitSafetySettings(req.deviceId);',+ );+ // 设备域浏览器那条额外 prefetch(它允许「目标就是当前设备」,deps 不变 → hook effect 不重跑,+ // 只有 subscriber 能送新数据),且必须排在转移动作**之后** —— 否则刚 prefetch 的又被 evict 掉。+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleRemoteProjectAdded = useCallback('),+ );+ const applyAt = handler.indexOf('applyDraftTarget({');+ const prefetchAt = handler.indexOf('prefetchDeviceCapabilities(target.deviceId)');+ expect(applyAt).toBeGreaterThan(-1);+ expect(prefetchAt).toBeGreaterThan(applyAt);+ // 侧边栏点远程项目进草稿是**另一个组件**的路径,不经本路由的转移动作,自己 evict + prefetch。+ // ⚠️ 这里原来只断言那三行 evict 存在 —— 于是它**保护住了**第 32 轮那个 P1(见下一条用例)。+ expect(sidebarUpperSource).toContain('evictDeviceCapabilities(targetDeviceId);');+ expect(sidebarUpperSource).toContain('evictDeviceProviders(targetDeviceId);');+ expect(sidebarUpperSource).toContain('evictDeviceGitSafetySettings(targetDeviceId);');+ });++ /**+ * #807 review 第 32 轮 P1 —— 与第 30 轮**同一个根因的第三处**,而且我上一轮的测试恰好把它锁住了:+ * 那条断言只要求侧边栏那三行 evict 存在,于是「无条件 evict」被写成了不变量去保护。+ *+ * 规则的完整表述应该是:**capabilities / providers 的 evict 必须配对一次会真正重取的动作** ——+ * 它们 notify `{ status: 'loading' }` 把已挂载的 hook 推进加载态,而 fetch effect 的 deps 是+ * `[agentKind, deviceId]`。配对方式有两种,按 deviceId 是否变化选:+ * · 换设备 → effect 自己会重跑(applyDraftTarget 走这条);+ * · deviceId 可能不变 → 必须显式 prefetch(设备域浏览器与侧边栏这两条走这条)。+ *+ * 本仓早有正确范例:useDeviceLinkRemoteProjects 处理 `maker:provider:changed` push 时就是+ * evict 紧跟 prefetch。反之,「设备撤销 / 断开 / 禁用 / hook 卸载」那几处 evict **不需要**配对 ——+ * 那台设备已经不可用,没有「永久 loading」的受害者。+ */+ it('pairs every eviction of a still-usable device with a refetch', () => {+ // ① 侧边栏「+ 新建」:deviceId 可能与当前草稿相同 → 必须显式 prefetch 三个。+ const handler = sidebarUpperSource.slice(+ sidebarUpperSource.indexOf('const handleCreateInProject = useCallback('),+ );+ const body = handler.slice(0, handler.indexOf('navigate('));+ for (const call of [+ 'prefetchDeviceCapabilities(targetDeviceId)',+ 'prefetchDeviceProviders(targetDeviceId)',+ 'prefetchDeviceGitSafetySettings(targetDeviceId)',+ ]) {+ expect(body).toContain(call);+ }+ // prefetch 必须在 evict 之后,否则刚取回的又被作废。+ expect(body.indexOf('prefetchDeviceCapabilities')).toBeGreaterThan(+ body.indexOf('evictDeviceCapabilities'),+ );+ // ② 设备域浏览器:同样可能是同一台设备,三个都要 prefetch(gitSafety 少了会让 Rewind 入口隐藏)。+ const added = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleRemoteProjectAdded = useCallback('),+ );+ const addedHead = added.slice(0, added.indexOf('return;'));+ expect(addedHead).toContain('prefetchDeviceCapabilities(target.deviceId)');+ expect(addedHead).toContain('prefetchDeviceProviders(target.deviceId)');+ expect(addedHead).toContain('prefetchDeviceGitSafetySettings(target.deviceId)');+ // ③ 「设备已不可用」类的 evict 不需要配对 —— 那几处刻意不 prefetch,别被这条规则误改。+ // 这里只锁「本仓存在那个正确范例」,它是这条规则的出处。+ expect(deviceLinkRemoteProjectsSource).toContain('evictDeviceProviders(push.deviceId);');+ expect(deviceLinkRemoteProjectsSource).toContain(+ 'void prefetchDeviceProviders(push.deviceId);',+ );+ });++ /**+ * #807 review 第 30 轮 P1 —— 这是**上一轮收敛时我自己引入的回归**,而全量门禁 54 PASS 没抓到:+ * 那时的断言只锁「有没有 evict」,不锁**触发条件**。+ *+ * 机制:evict 不是幂等清理,而是一次有副作用的状态转移 —— `evictDeviceCapabilities` /+ * `evictDeviceProviders` 都会 notify `{ status: 'loading' }`(为了让已挂载的 hook 立刻知道旧+ * 快照失效,否则 provider 新快照先到时会拿旧 capabilities 算 fallback 并覆盖用户偏好)。+ * 它必须有配对的 fetch 才能收敛,而两个 hook 的 effect deps 是 `[agentKind, deviceId]`、+ * **不含项目** —— 于是同一台设备上换个项目时,evict 之后没有任何东西会去重拉:+ * `capabilitiesLoading` 永久为真,send / New Goal 的三重 gate 永久拒绝创建,用户必须切设备+ * 或重进路由才能恢复。功能完全阻塞。+ *+ * 所以这条同时钉住上游那个**事实**(evict 会 notify loading)与下游那条**规则**(因此需要配对+ * fetch):任何一端被改动都会在这里失败,而不是等到用户发不出消息。+ */+ it('does not evict same-device snapshots when only the workspace changes', () => {+ // 上游事实:evict 会把已挂载的 hook 推进 loading 态。+ expect(agentCapabilitiesHookSource).toContain(+ "notifyRemoteCapabilities(deviceId, agentKind, { status: 'loading' });",+ );+ expect(deviceProvidersHookSource).toContain(+ "notifyDeviceProviders(deviceId, { status: 'loading' });",+ );+ // 上游事实:fetch effect 只按 (agentKind, deviceId) 重跑,换项目不会触发它。+ expect(agentCapabilitiesHookSource).toContain('}, [agentKind, deviceId]);');+ expect(deviceProvidersHookSource).toContain('}, [deviceId]);');+ // 下游规则:因此只在「换了设备」或「调用方已带来新快照并会紧接着 prefetch」时才 evict。+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ expect(action).toContain('if (req.deviceId && (deviceChanged || req.remoteSnapshot)) {');+ // 工作区 picker 这条路径既不换设备也不带快照 → 必然不 evict。+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleWorkingDirChange = useCallback('),+ );+ const body = handler.slice(0, handler.indexOf(' [applyDraftTarget'));+ expect(body).not.toContain('remoteSnapshot');+ expect(body).not.toContain('evictDevice');+ });++ // #807 review 第二十轮:归属钉子只解决路由,首条消息的交接还要求 SessionView 拿到一条会话行+ // (delayed-create effect 要 `session` 非空且 workingDir 非空)。回流失败、尤其老被控端永久+ // 拿不到 sessions:list 时永远不会有权威快照,那条消息就永久不发,而草稿已经清掉。+ it('seeds a provisional session row so the first message can hand off without the snapshot', () => {+ // 补行这一步同样搬进了 handoff(两条路径都走它,见上)。这里锁它的三条细节:+ // workDir 必须取 create 响应,不能拿草稿的 workingDir 顶替 —— 纯对话的运行目录由对端分配。+ expect(remoteSessionHandoffSource).toContain('workDir: p.workDir,');+ // 缺 workDir 时跳过补行,而不是编一个目录:delayed-create 的门槛正是它非空。+ expect(remoteSessionHandoffSource).toContain('if (p.workDir) {');+ // 用 merge 而非 set:不能把该设备已缓存的其它会话冲掉。+ expect(remoteSessionHandoffSource).toContain('remoteProjectsStore.mergeDeviceSessions(');+ expect(remoteSessionHandoffSource).not.toContain('setDeviceSessions(');+ // 临时行按**实际提交的** args 组装,不再推一遍 model / permission / workspaceKind。+ expect(remoteSessionHandoffSource).toContain('args: p.createArgs,');+ });++ // #807 review 第二十二轮:换设备时的清理只管「切换那一刻已在托盘里」的附件,**先选设备、之后+ // 再拖进来**的路径型附件照样会把控制端绝对路径发到对端。两者一起才是「远程草稿绝不携带控制端+ // 路径附件」这条不变量。+ it('refuses path-backed attachments added after a remote device is selected', () => {+ const guard = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const guardedAttachmentState = useMemo('),+ );+ const body = guard.slice(0, guard.indexOf('}, ['));+ // 本机草稿零开销:直接返回原对象,不包装。+ expect(body).toContain('if (!isDeviceLinkDraft) return attachmentState;');+ // 判据必须与下游**同口径**(第 29 轮 P1):useAttachments 的分类完全不看 MIME —— 先按扩展名+ // categorizeFile,认不出来才 peekFileHeader 按魔数推断。原来这里用 `f.type.startsWith('image/')`,+ // 于是 Electron 给空 / 通用 File.type 时(某些平台与拖拽源如此,重命名过的图片更是必然),+ // 一张下游明明能识别的图片会被拦掉,而且只在远程草稿下如此,用户切回本机就能加。+ // 断言旧代码形态而非裸片段 —— 上面那段注释里会逐字提到它作为历史记录。+ expect(body).not.toContain("incoming.filter((f) => f.type.startsWith('image/'))");+ expect(body).toContain('const ext = extractExt(f.name);');+ expect(body).toContain(+ 'const category = ext ? categorizeFile(ext) : categorizeByFilename(f.name);',+ );+ // 认不出类别时**放行**,交给下游的文件头推断 + 收敛器兜底 —— 闸门宁可放过,绝不误拒。+ expect(body).toContain('if (!category) return false;');+ expect(body).toContain("return category !== 'image';");+ expect(body).toContain("t('newChat.deviceSwitcher.attachmentsRemoteUnsupported'");+ // 三个入口(ChatInput + 本路由的拖拽 + 延迟分类的拖拽)都必须走闸门,不能有一个直连原对象。+ expect(newMakerDraftRouteSource).toContain('attachmentState={guardedAttachmentState}');+ expect(+ (newMakerDraftRouteSource.match(/guardedAttachmentState\.addFiles\(/g) ?? []).length,+ ).toBe(2);+ // 唯一一处直连原对象的 addFiles 必须是闸门内部那次(放行的那批);此外一处都不许有。+ expect(+ (newMakerDraftRouteSource.match(/(?<!guarded)attachmentState\.addFiles\(/g) ?? []).length,+ ).toBe(1);+ expect(body).toContain('await attachmentState.addFiles(passed);');+ });++ // #807 review 第二十二轮:ExtraDirsButton 开的是控制端原生目录对话框,选出来的本机路径发到对端+ // 会被静默丢掉、或撞上对端同名的无关目录 —— chip 显示的并不是真实授予的上下文。+ it('hides the reference-directory picker on remote drafts', () => {+ expect(newMakerDraftRouteSource).toContain(+ 'onExtraDirsChange={isDeviceLinkDraft ? undefined : handleExtraDirsChange}',+ );+ // ExtraDirsButton 的契约:没有 onChange 就不渲染引用目录段(其余菜单项不受影响)。+ expect(extraDirsButtonSource).toContain(+ '未提供时只显示目标、计划模式或 Plugin 入口,不显示引用目录段',+ );+ });++ // #807 review 第二十四轮:`useAttachments.addFiles` 对未知扩展名要先 await peekFileHeader,附件是+ // IPC 回来后才进 state 的 —— 本机草稿下拖入 → 期间切到远程 → 切换清理找不到它 → IPC 回来后被追加,+ // 且那次调用握的是切换前的真 addFiles,绕过闸门。按入口逐个堵已经漏了三次,改成维护不变量。+ it('converges the no-controller-path invariant regardless of which entry added the file', () => {+ const effect = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('「远程草稿绝不携带控制端路径附件」的**收敛器**'),+ );+ const body = effect.slice(0, effect.indexOf('}, ['));+ expect(body).toContain('if (!isDeviceLinkDraft) return;');+ expect(body).toContain(".filter((f) => f.category !== 'image')");+ expect(body).toContain('attachmentState.removeFile(f.id)');+ // 依赖 attachments 本身(而非整个 attachmentState 对象)才能在附件晚到时重跑。+ expect(effect.slice(effect.indexOf('}, ['), effect.indexOf('}, [') + 160)).toContain(+ 'attachmentState.attachments',+ );+ });++ // #807 review 第二十四轮:handleCreateGoal 必须整段持在途锁。上一轮我以为「模态遮罩挡住 pill」就+ // 够了 —— 只考虑了指针输入,AlertDialog 默认拦外部点击但 Esc 照样能关。+ it('holds the in-flight lock for the whole goal creation', () => {+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleCreateGoal = useCallback('),+ );+ const head = handler.slice(0, handler.indexOf('let policyEnabled'));+ expect(head).toContain('markSendInFlight(true);');+ expect(head).toContain('try {');+ // finally 释放,覆盖所有 throw / 早退。+ expect(handler.slice(0, handler.indexOf('\n },'))).toContain('markSendInFlight(false);');+ // 弹窗侧:saving 期间不许 Esc 关掉(别让 UI 假装取消了)。+ expect(newGoalDialogSource).toContain('if (saving) event.preventDefault();');+ });++ /**+ * #807 review 第 31 轮 P1:上一处的锁本身是对的,但**早退方式**错了 —— `return` 会被+ * NewGoalDialog 当成成功。+ *+ * 与第 30 轮那条 evict 同一个模式:同时钉住上游那个事实与下游那条规则,任何一端被改动都会+ * 在这里失败,而不是等到用户的目标文案被静默丢掉。+ */+ it('rejects duplicate goal creation instead of resolving as success', () => {+ // 上游事实:save() 把 `await onCreate(...)` 正常 resolve 一律当成成功 —— 紧接着清 composer+ // 并关掉弹窗;只有抛出才会走 catch 内联显示原因、保住用户已写的 objective。+ const save = newGoalDialogSource.slice(+ newGoalDialogSource.indexOf('const save = async () => {'),+ );+ const saveBody = save.slice(0, save.indexOf('\n };'));+ expect(saveBody).toContain('await onCreate(trimmed, limits);');+ expect(saveBody).toContain('onCreated?.();');+ expect(saveBody).toContain('onOpenChange(false);');+ expect(saveBody).toContain('} catch (err) {');+ // 下游规则:锁被占用时必须抛,不能 return。+ const handler = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleCreateGoal = useCallback('),+ );+ const guard = handler.slice(0, handler.indexOf('markSendInFlight(true);'));+ expect(guard).toContain("throw new Error(t('goal.newGoalDialog.busy'));");+ expect(guard).not.toContain('if (sendInFlightRef.current) return;');+ // 抛出必须发生在**上锁之前**,否则 finally 会把仍在跑的那次操作的锁解掉。+ expect(guard.indexOf('throw new Error')).toBeGreaterThan(+ guard.indexOf('if (sendInFlightRef.current)'),+ );+ // 发送路径相反:它返回 false 让 ChatInput 保留草稿(那是它的既定契约,不要跟着改成 throw)。+ const send = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const handleSend = useCallback('),+ );+ expect(send.slice(0, send.indexOf('if (effectiveCollab.enabled'))).toContain(+ 'if (sendInFlightRef.current) return false;',+ );+ });++ // #807 review 第二十五轮:上一轮把「指名设备离线就不回落到别的目标」改对了,但受控 select 的+ // value="" 没有对应 option —— 浏览器会去显示第一个真实 option,而 selectedTarget 仍是 null,+ // 「添加」保持 disabled;只有一个备选目标时点那个已显示的项也不产生 change,弹窗就此卡死。+ it('gives the unselected target state a real placeholder option and a reason', () => {+ expect(addRemoteProjectDialogSource).toContain('{selectedKey === null && (');+ expect(addRemoteProjectDialogSource).toContain('<option value="" disabled>');+ expect(addRemoteProjectDialogSource).toContain(+ "t('newChat.addRemoteProject.selectTargetPlaceholder')",+ );+ // 未选中的原因也要说出来,否则用户只看到一个空下拉。+ expect(addRemoteProjectDialogSource).toContain('const requestedDeviceUnavailable =');+ expect(addRemoteProjectDialogSource).toContain(+ "t('newChat.addRemoteProject.requestedDeviceUnavailable')",+ );+ });++ // #807 review 第二十七轮:同机换项目保留用户已选是对的(第十三轮加的),但这条路径同时把+ // dlSeedKeyRef 记成「该设备已 seed」,后续 capabilities 更新不会再重种 —— 被控端此间删掉了用户+ // 选中的模型 / 不再支持某个 permission 时,失效值会一直留在草稿里:发送被 gate 拦住变成「点了+ // 没反应」,「新建目标」更糟,直接把失效值提交给 maker:create-session。+ it('revalidates a preserved remote selection against the freshly fetched capabilities', () => {+ const action = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const applyDraftTarget = useCallback('),+ );+ const body = action.slice(0, action.indexOf(' patchDraft({'));+ // 同设备分支不能什么都不做 —— 上面刚把 seedKey 记成「已 seed」,后续 capabilities 更新不会+ // 再重种,失效值会一直留着:发送被 gate 拦住变成「点了没反应」,建目标更糟,会直接提交出去。+ expect(body).toContain('} else {');+ expect(body).toContain('setDlSel((prev) =>');+ // 复用既有纯函数做 clamp,不另写一套;把用户当前选择当 remoteDraft 传进去。+ expect(body).toContain('model: prev.model,');+ expect(body).toContain('permissionMode: prev.permissionMode,');+ // 三处调用:换设备重种、同设备按 prev 校准、prev 为空时退回正常 seed。+ expect((body.match(/resolveDeviceLinkDraftDefaults\(/g) ?? []).length).toBeGreaterThanOrEqual(+ 3,+ );+ });++ // #807 review 第二十七轮:设备菜单行原来只有 hover / disabled 两态,且 outline-none 去掉了浏览器+ // 默认焦点圈 —— 键盘走这个菜单时完全看不出焦点落在哪一行。+ it('shows a token-backed focus ring on device menu rows', () => {+ const row = deviceSwitcherPillSource.slice(+ deviceSwitcherPillSource.indexOf('function DeviceRow('),+ );+ expect(row).toContain(+ 'focus-visible:ring-2 focus-visible:ring-[var(--create-agent-focus-ring)]',+ );+ // 与 pill trigger 同 token(同一控件的键盘表现应一致)。+ expect(deviceSwitcherPillSource).toContain(+ 'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--create-agent-focus-ring)]',+ );+ });++ // #807 review 第二十八轮:本机分支早就用 effectiveSourceIdForModel 校准过来源,device-link 分支+ // 却原样透传 dlSel.providerId。普通发送不受影响(ChatInput 内部会重算),但「新建目标」是直接拿+ // 这个值提交给 maker:create-session 的 —— 被控端把该来源断开后,会把未认证来源写进+ // sessions.provider_id,新目标起不来。校准放在**派生处**,一次覆盖所有消费点。+ it('clamps the device-link provider through the shared resolver, not just the local branch', () => {+ const derive = newMakerDraftRouteSource.slice(+ newMakerDraftRouteSource.indexOf('const chatInitialProviderId = useMemo<string | null>('),+ );+ const body = derive.slice(0, derive.indexOf('}, ['));+ // 本机分支行为不变。+ expect(body).toContain('if (!isDeviceLinkDraft) return localProviderIdForDraft;');+ // 远程分支按**被控端**目录 + 草稿当前模型复算,用与 main 同源的解析函数。+ expect(body).toContain('effectiveSourceIdForModel(');+ expect(body).toContain('deviceProviders,');+ expect(body).toContain('draftInitialModel,');+ // 反向防回退:不能再出现原样透传。+ expect(newMakerDraftRouteSource).not.toContain(+ 'isDeviceLinkDraft\n ? (deviceLinkInitial?.providerId ?? null)',+ );});it('keeps recent-folder storage out of project-option selection', () => {diff --cc apps/desktop/src/renderer/__tests__/newMakerWorktreeSend.test.tsindex 5a31b7548f,c62a235730..03a1b4eb9a--- a/apps/desktop/src/renderer/__tests__/newMakerWorktreeSend.test.ts+++ b/apps/desktop/src/renderer/__tests__/newMakerWorktreeSend.test.ts@@@ -51,87 -50,6 +51,86 @@@ describe('NewMakerDraftRoute worktree sexpect(source).not.toContain('patchDraft({ workingDir: newDir })');});+ it('uses the current checkout as the safe source when branch discovery is not ready', () => {+ expect(source.match(/sourceBranch: wt\.sourceBranch\.trim\(\) \|\| 'HEAD'/g)).toHaveLength(2);+ expect(source).not.toContain("sourceBranch: wt.sourceBranch.trim() || 'main'");+ });+- it('treats remote session creation as committed before the ancillary list refresh', () => {++ it('treats remote session creation as committed before the shared non-blocking handoff', () => {+ const remoteSessionId = source.indexOf('const remoteSessionId = presetSessionId');- const commitPoint = source.indexOf('maker:create-session 回包是事务提交点', remoteSessionId);- const listRefresh = source.indexOf("'local-db:sessions:list'", commitPoint);- const refreshCatch = source.indexOf(- '[remote draft send] session created but list refresh failed; requesting reseed',- listRefresh,++ const commitPoint = source.indexOf('remoteSessionId 到手就是**提交点**', remoteSessionId);++ const handoff = source.indexOf(++ 'commitRemoteSessionHandoff({',++ commitPoint,+ );- const pendingHandoff = source.indexOf('setPending(remoteSessionId', refreshCatch);++ const pendingHandoff = source.indexOf('setPending(remoteSessionId', handoff);++ expect(remoteSessionId).toBeGreaterThan(-1);+ expect(commitPoint).toBeGreaterThan(remoteSessionId);- expect(listRefresh).toBeGreaterThan(commitPoint);- expect(refreshCatch).toBeGreaterThan(listRefresh);- expect(pendingHandoff).toBeGreaterThan(refreshCatch);++ expect(handoff).toBeGreaterThan(commitPoint);++ expect(pendingHandoff).toBeGreaterThan(handoff);++ expect(source).not.toContain("'local-db:sessions:list'");+ });++ it('settles an older remote cleanup obligation before creating another worktree', () => {+ const remoteBranch = source.indexOf(- 'if (isDeviceLinkDraft && effectiveDeviceLinkDeviceId && effectiveWorkingDir) {',++ 'if (isDeviceLinkDraft && effectiveDeviceLinkDeviceId) {',+ );+ const recovery = source.indexOf(+ 'await recoverPendingRemotePrecreatedWorktrees({',+ remoteBranch,+ );+ const retainedGuard = source.indexOf('!recovery.storageReadable', recovery);+ const reservationGuard = source.indexOf(+ 'if (!registerPendingRemotePrecreatedWorktree(reservation))',+ retainedGuard,+ );+ const worktreeCreate = source.indexOf("'worktree:create'", retainedGuard);+ const ledgerRegistration = source.indexOf(+ 'createRemoteSessionWithPrecreatedWorktree({',+ worktreeCreate,+ );++ expect(recovery).toBeGreaterThan(remoteBranch);+ expect(source.slice(recovery, worktreeCreate)).toContain(+ '!recovery.storageReadable || recovery.retained > 0',+ );+ expect(retainedGuard).toBeGreaterThan(recovery);+ expect(reservationGuard).toBeGreaterThan(retainedGuard);+ expect(worktreeCreate).toBeGreaterThan(reservationGuard);+ expect(worktreeCreate).toBeGreaterThan(retainedGuard);+ expect(source.slice(worktreeCreate, worktreeCreate + 420)).toContain(+ 'recoveryKey,',+ );+ expect(ledgerRegistration).toBeGreaterThan(worktreeCreate);+ expect(source.slice(ledgerRegistration, ledgerRegistration + 220)).toContain(+ 'deviceId,',+ );+ });++ it('retries remote draft defaults after the relay or selected workstation reconnects', () => {+ const epochHook = source.indexOf(+ 'useDeviceLinkReconnectEpoch(',+ );+ const defaultsFetch = source.indexOf(+ "'maker:get-new-maker-defaults'",+ epochHook,+ );+ const effectDependencies = source.indexOf(+ 'remoteDraftRefreshEpoch,',+ defaultsFetch,+ );+ const transientPreserve = source.indexOf(+ 'value: unsupported ? null : previous.value',+ defaultsFetch,+ );++ expect(epochHook).toBeGreaterThan(-1);+ expect(defaultsFetch).toBeGreaterThan(epochHook);+ expect(effectDependencies).toBeGreaterThan(defaultsFetch);+ expect(transientPreserve).toBeGreaterThan(defaultsFetch);+ });+it('does not auto-send if the prepared session is no longer active', () => {const worktreeCreate = source.indexOf('window.electronAPI.worktreeCreate');const latestSession = source.indexOf('const latestSession = await sessionService.get(newSession.id)', worktreeCreate);diff --cc apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsxindex a19cde6e52,12c5d153f8..151d1e2104--- a/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx+++ b/apps/desktop/src/renderer/features/cc-agent/NewMakerDraftRoute.tsx@@@ -1546,12 -2000,13 +2076,17 @@@ export function NewMakerDraftRoute()// workingDir 为空就是 standalone dialogue;main 端按 workspaceKind='dialogue'// 自动分配运行目录。项目不是必填项,只是同一创建页里的可切换上下文。const selectedWorkingDir = effectiveWorkingDir?.trim() || undefined;+ // 发送语义以用户按下 Send 的那一帧为准。鉴权检查期间项目/分支仍可能被 UI+ // 改动;若异步块稍后再读 live ref,会把旧 workingDir 与新 baseRepo 拼成一次+ // 混合目标创建。这里与 selectedWorkingDir 同步快照,保证整笔创建目标一致。+ const selectedWorktree = { ...wtRef.current };- sendInFlightRef.current = true;+ markSendInFlight(true);+ // 已登记乐观标题预览的会话 id。交接链路(rehomeDraftAttachments / setPending /+ // navigate)在登记之后抛错时,消息没被交出去、权威标题永不回流,预览必须在下面的+ // catch 里撤回,否则空会话会跨列表刷新一直显示一句**没发出去**的话+ // (PR #1031 review P1;worktree 与 goal 两条路径各有自己的撤回点)。+ let optimisticTitleSessionId: string | null = null;void (async () => {try {// device-link:远程草稿就绪态以被控端为准(传 deviceId 走隧道查被控端 maker:agent:status);@@@ -1570,36 -2025,29 +2105,39 @@@// 所以顺序反过来 —— 先同步等被控端建好 worktree 拿到路径,再以该路径 + 预生成// sessionId 建会话;sessionId 两步共用,被控端 close-session 时才能按// worktreeStore 绑定回收 worktree。- if (isDeviceLinkDraft && effectiveDeviceLinkDeviceId && effectiveWorkingDir) {+ // #807:不再要求 effectiveWorkingDir —— 选了设备就走对端建会话。没有项目目录时+ // resolveDeviceLinkSubmission 会把 workspaceKind 派生成 'dialogue',被控端自行分配+ // 运行目录(隧道侧 path guard 对「缺 workingDir」本来就是放行的,不放宽任何边界)。+ if (isDeviceLinkDraft && effectiveDeviceLinkDeviceId) {const deviceId = effectiveDeviceLinkDeviceId;const deviceName = effectiveDeviceLinkDeviceName ?? deviceId;- const wt = wtRef.current;- let remoteWorkingDir = effectiveWorkingDir ?? undefined;+ const invokeRemote = (channel: string, args: unknown[]) =>+ window.electronAPI.deviceLink.invoke(deviceId, channel, args);+ const wt = selectedWorktree;- let remoteWorkingDir = effectiveWorkingDir;++ let remoteWorkingDir = effectiveWorkingDir?.trim() || undefined;let presetSessionId: string | undefined;- // worktree 是项目专属能力:远程纯对话没有 repo,即使 wtEnabled 还残留着上一个- // 项目草稿的 true(切「对话」不会重置它),也必须跳过,否则会拿 null baseRepo- // 撞进 worktreeMissingRepo 报错、把一次正常的对话创建拦掉。- if (effectiveWorkingDir && wt.enabled) {- // baseRepo 来自被控端 worktree:detect-cwd 的 repoRoot(hooks 已按 deviceId 路由)。- const baseRepo = wt.baseRepo;- if (!baseRepo) {- showWorktreeError({- kind: 'unknown',- message: t('ccAgent.draft.worktreeMissingRepo'),- hint: t('ccAgent.draft.worktreeRepoHint'),- });- return;+ let precreatedWorktree: {+ path: string;+ recoveryKey: string;+ createdAt: number;+ } | undefined;+ // 生效条件 = 勾选 && baseRepo 已就绪(被控端 detect-cwd 的 repoRoot)。+ // 勾选但环境不合格 / 探测未回时**静默按普通方式启动**,不报错不改勾选记忆+ // ——状态只属于用户,合格性只影响这一次是否真的走 worktree(2026-07-29 裁决)。- if (wt.enabled && wt.baseRepo) {++ if (effectiveWorkingDir && wt.enabled && wt.baseRepo) {+ // 上次两步创建若在 create / probe / discard 都断线后失败,本地账本仍+ // 承担 cleanup obligation。新建下一份前先恢复;无法确认回收/认领时+ // 硬挡本次 worktree:create,避免每次重试生成一个新的受管目录。+ const recovery = await recoverPendingRemotePrecreatedWorktrees({+ deviceId,+ invoke: invokeRemote,+ });+ // 账本不可读时磁盘上是否还有旧 obligation 未知,也必须 fail+ // closed;否则一次暂时的 localStorage 故障会绕过同设备串行回收。+ if (!recovery.storageReadable || recovery.retained > 0) {+ throw new RemotePrecreatedWorktreeCleanupPendingError();}+ const baseRepo = wt.baseRepo;let name = wt.name.trim();if (!name) name = `auto-${Date.now().toString(36).slice(-6)}`;presetSessionId = makeDraftSessionId();@@@ -1675,29 -2099,34 +2212,41 @@@// 非 worktree 流程两者保持原值 / 缺省。id: presetSessionId,workingDir: remoteWorkingDir,- model,- effort,- permissionMode,- fastMode: effectiveFastMode,extraDirs: effectiveExtraDirs,- // 草稿选定的来源(被控端供应商;null=跟随默认路由)。被控端 create 时落 sessions.provider_id,- // 使新远程会话首个请求即按所选来源路由(P2)。- providerId,+ // 候选值 = ChatInput 回传的实时值(用户此刻在界面上看到的那一组)。来源校准与 args+ // 组装都在 resolveDeviceLinkSubmission 里,与「新建目标」共用同一份规则 —— 那两条+ // 路径各自推导曾长出过只在其中一条上复现的缺陷(见该函数注释)。+ candidate: {+ model,+ effort,+ permissionMode,+ fastMode: effectiveFastMode,+ providerId,+ },+ deviceProviders,+ capabilityAgentKind,});- const createResult = await window.electronAPI.deviceLink.invoke(- deviceId,- 'maker:create-session',- // workspaceKind 由 workingDir 派生 + agentKind 归一 + 来源经共享解析器校准,- // 全部发生在 resolveDeviceLinkSubmission 里。- // extraDirs 一并透传(与本地 create 对齐):被控端 bootstrapSession 按 set-extra-dirs- // 同款 validateExtraDirs 校验后只存通过的子集,控制端镜像随被控端真相回流。- [createArgs],- );- const created = createResult as { sessionId?: string; workDir?: string } | null;- const remoteSessionId = created?.sessionId;++ let created: { sessionId?: string; workDir?: string } | null = null;+ const remoteSessionId = presetSessionId && precreatedWorktree+ ? await createRemoteSessionWithPrecreatedWorktree({+ deviceId,+ sessionId: presetSessionId,+ path: precreatedWorktree.path,+ recoveryKey: precreatedWorktree.recoveryKey,+ createdAt: precreatedWorktree.createdAt,- createArgs: remoteCreateArgs,++ createArgs: createArgs,+ invoke: invokeRemote,+ })- : ((- await invokeRemote('maker:create-session', [remoteCreateArgs])- ) as { sessionId?: string } | null)?.sessionId;++ : (created = (await invokeRemote('maker:create-session', [createArgs])) as {++ sessionId?: string;++ workDir?: string;++ } | null)?.sessionId;++ if (presetSessionId && precreatedWorktree) {++ created = { sessionId: remoteSessionId, workDir: remoteWorkingDir };++ }if (!remoteSessionId) {+ // device-link 创建失败:错误来自被控端 RPC,与 useCCSessions().error 无关,+ // 不应读 state 里的 REMOTE_* code(copilot review #1035)。toast.error(t('ccAgent.draft.createSessionFailed'));return;}@@@ -2069,13 -2515,10 +2629,15 @@@});} catch (err) {log.error('[draft send]', err);+ // 交接失败 → 撤回乐观标题预览(理由见上面 optimisticTitleSessionId 的注释)。+ if (optimisticTitleSessionId) emitAutoTitlePreviewCleared(optimisticTitleSessionId);toast.error(- getRemoteWorkingDirErrorMessage(err, t) ?? t('ccAgent.draft.createSessionFailed'),+ isRemotePrecreatedWorktreeCleanupPendingError(err)+ ? t('ccAgent.draft.remoteWorktreeCleanupPending')+ : (+ getRemoteWorkingDirErrorMessage(err, t)+ ?? t('ccAgent.draft.createSessionFailed')+ ),);} finally {setWtCreating(false);diff --cc apps/desktop/src/renderer/i18n/locales/en/common.jsonindex 44100af216,86bd4977bb..c888aadcab--- a/apps/desktop/src/renderer/i18n/locales/en/common.json+++ b/apps/desktop/src/renderer/i18n/locales/en/common.json@@@ -5316,10 -5609,15 +5612,14 @@@},"draft": {"createSessionFailed": "Failed to create session, please retry",+ "remoteWorktreeCleanupPending": "The previous remote worktree is still awaiting cleanup. Reconnect the device and try again.",+ "remoteProviderUpdating": "Provider credentials are being updated — retry in a moment",+ "remoteProviderUnsupported": "This provider is not supported in SSH remote sessions — pick another source",+ "remoteNativeOauthUnavailable": "Claude.ai subscription is not connected — connect it first or pick a gateway model","deviceStillLoading": "Remote device is still loading, please wait",- "worktreeMissingRepo": "Cannot create worktree: missing repository root for the current directory",- "worktreeRepoHint": "Try re-selecting the working directory and wait for the folder badge to refresh, then retry","sessionInactive": "Session is no longer active",- "remoteProjectBanner": "New session in {{project}} on {{device}}"+ "remoteProjectBanner": "New session in {{project}} on {{device}}",+ "remoteDialogueBanner": "New dialogue on {{device}} (no project)"},"jumpSession": {"emptyId": "Enter a sessionId to jump to",diff --cc apps/desktop/src/renderer/i18n/locales/ja/common.jsonindex 011a50f5fb,507d9cfd96..d68d0b50ba--- a/apps/desktop/src/renderer/i18n/locales/ja/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ja/common.json@@@ -5306,10 -5599,15 +5602,14 @@@},"draft": {"createSessionFailed": "セッションの作成に失敗しました。再試行してください",+ "remoteWorktreeCleanupPending": "前回のリモート worktree はまだクリーンアップ待ちです。デバイスを再接続してから再試行してください。",+ "remoteProviderUpdating": "プロバイダーの認証情報を更新中です。しばらくしてから再試行してください",+ "remoteProviderUnsupported": "このプロバイダーは SSH リモートセッションに対応していません。別のソースを選択してください",+ "remoteNativeOauthUnavailable": "Claude.ai サブスクリプションが未接続です。先に接続するか、ゲートウェイモデルを選択してください","deviceStillLoading": "リモートデバイスの読み込み中です。しばらくお待ちください",- "worktreeMissingRepo": "worktree を作成できません:現在のディレクトリのリポジトリルートがありません",- "worktreeRepoHint": "作業ディレクトリを再選択し、右下のフォルダ表示が更新されてから再試行してください","sessionInactive": "セッションはすでにアクティブではありません",- "remoteProjectBanner": "{{device}} の {{project}} で新しいセッションを作成"+ "remoteProjectBanner": "{{device}} の {{project}} で新しいセッションを作成",+ "remoteDialogueBanner": "{{device}} で新しい対話(プロジェクトなし)"},"jumpSession": {"emptyId": "移動先の sessionId を入力してください",diff --cc apps/desktop/src/renderer/i18n/locales/ko/common.jsonindex bcdc25c66d,7c761f403f..7cb0c121e8--- a/apps/desktop/src/renderer/i18n/locales/ko/common.json+++ b/apps/desktop/src/renderer/i18n/locales/ko/common.json@@@ -5306,10 -5599,15 +5602,14 @@@},"draft": {"createSessionFailed": "세션 생성에 실패했습니다. 다시 시도하세요",+ "remoteWorktreeCleanupPending": "이전 원격 worktree가 아직 정리 대기 중입니다. 기기를 다시 연결한 후 재시도하세요.",+ "remoteProviderUpdating": "제공자 자격 증명을 업데이트하는 중입니다. 잠시 후 다시 시도하세요",+ "remoteProviderUnsupported": "이 제공자는 SSH 원격 세션을 지원하지 않습니다. 다른 소스를 선택하세요",+ "remoteNativeOauthUnavailable": "Claude.ai 구독이 연결되지 않았습니다. 먼저 연결하거나 게이트웨이 모델을 선택하세요","deviceStillLoading": "원격 기기가 아직 로딩 중입니다. 잠시 기다려 주세요",- "worktreeMissingRepo": "worktree를 만들 수 없음: 현재 디렉터리의 리포지토리 루트가 없습니다",- "worktreeRepoHint": "작업 디렉터리를 다시 선택하고 우측 하단의 폴더 표시가 갱신된 후 다시 시도하세요","sessionInactive": "세션이 더 이상 활성 상태가 아닙니다",- "remoteProjectBanner": "{{device}}의 {{project}}에서 새 세션 만들기"+ "remoteProjectBanner": "{{device}}의 {{project}}에서 새 세션 만들기",+ "remoteDialogueBanner": "{{device}}에서 새 대화(프로젝트 없음)"},"jumpSession": {"emptyId": "이동할 sessionId를 입력하세요",diff --cc apps/desktop/src/renderer/i18n/locales/zh-CN/common.jsonindex 3821c366a1,4c10df564a..0f03f8f54b--- a/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json+++ b/apps/desktop/src/renderer/i18n/locales/zh-CN/common.json@@@ -5306,10 -5599,15 +5602,14 @@@},"draft": {"createSessionFailed": "创建对话失败,请重试",+ "remoteWorktreeCleanupPending": "上一次远程 worktree 仍待回收,请重新连接设备后再试。",+ "remoteProviderUpdating": "供应商凭证正在更新,请稍后重试",+ "remoteProviderUnsupported": "该供应商不支持 SSH 远程会话,请更换来源",+ "remoteNativeOauthUnavailable": "未连接 Claude.ai 订阅,请先连接或选择网关模型","deviceStillLoading": "远程设备仍在加载中,请稍候",- "worktreeMissingRepo": "无法开 worktree:缺少当前目录的仓库根",- "worktreeRepoHint": "可尝试重新选一次工作目录,等右下角文件夹标识刷新后再试","sessionInactive": "对话已不再处于活跃状态",- "remoteProjectBanner": "在 {{device}} 的 {{project}} 中新建对话"+ "remoteProjectBanner": "在 {{device}} 的 {{project}} 中新建对话",+ "remoteDialogueBanner": "在 {{device}} 上新建对话(不绑定项目)"},"jumpSession": {"emptyId": "请输入要跳转的对话 ID",diff --cc apps/mobile/app/sessions/[sessionId].tsxindex 31b0c6d547,dc06827630..73515cd4b2--- a/apps/mobile/app/sessions/[sessionId].tsx+++ b/apps/mobile/app/sessions/[sessionId].tsx@@@ -142,7 -140,7 +143,8 @@@ import { ImageLightbox } from '@/sessioimport { pickWriteFields, retryPatchWhileLatest, writeGuardFields } from '@/session/swipeRowRegistry';import {dismissNewSessionCreation,+ prepareNewSessionCreationForEdit,+ getNewSessionCreationTask,retryNewSessionCreation,shouldBlockSessionSync,stashNewSessionDraftForEdit,@@@ -1876,10 -2019,9 +2068,10 @@@ export default function SessionScreen(const renderComposerToolbar = () => (<>{renderComposerAttachmentButton()}+ {renderSessionPermissionButton()}{planModeOn ? (<PlanModeChip- disabled={!canUseComposer || controlBusy}+ disabled={!canUseComposer || controlBusy || sessionSettingsLocked}onExit={() => togglePlanMode(false)}testID="session.planModeChip"/>@@@ -2829,32 -3024,39 +3075,62 @@@style: 'cancel',onPress: () => {if (!creationTask) return;- // 创建期间发出的后续消息必须一起带回新建页(review P1):它们此刻在 outbox 里,- // 而下一行 dismiss 会连同合成会话行一起删掉——会话页 unmount 时 cleanup 会把它们- // 写进那个已经不存在的会话的草稿,用户在新建页看不到、也再也找不回来。- // 新建页只有一个首条消息输入框,所以按序拼成文本、附件按 id 去重合并后一起 stash。- // 上传任务保不住:本页(连同 upload controller)马上就要销毁,只能取消- // 并回收,再把「没能带回多少」告知用户(review P1:静默丢等于偷走内容)。- const { items: followUps, cancelledUploadCount } = takeOutboxForSession(sessionId, 'cancel');- if (followUps.length > 0) {- const { merged, dropped } = mergeAttachmentsWithinLimit(- creationTask.attachments,- followUps.flatMap(outboxItemAttachments),- );- // 装不下的中转对象在这里回收:草稿带不走它们,留着就是没人认领的垃圾。- discardRecoveredAttachments(dropped);- const unrecoveredCount = dropped.length + cancelledUploadCount;- stashNewSessionDraftForEdit(creationTask, {- draft: {- ...creationTask.draft,- firstMessage: [- creationTask.draft.firstMessage,- ...followUps.map(outboxItemDraftText),- ].filter(Boolean).join('\n\n'),- },- attachments: merged,- notice: unrecoveredCount > 0- ? t('session.screen.attachmentsNotCarriedBack', { count: unrecoveredCount })- : null,+ void prepareNewSessionCreationForEdit(sessionId)+ .then((prepared) => {+ if (!prepared) return;- stashNewSessionDraftForEdit(prepared);++ // 创建期间发出的后续消息必须一起带回新建页(review P1):它们此刻在 outbox 里,++ // 而下一行 dismiss 会连同合成会话行一起删掉——会话页 unmount 时 cleanup 会把它们++ // 写进那个已经不存在的会话的草稿,用户在新建页看不到、也再也找不回来。++ // 新建页只有一个首条消息输入框,所以按序拼成文本、附件按 id 去重合并后一起 stash。++ // 上传任务保不住:本页(连同 upload controller)马上要销毁,只能取消++ // 并回收,再把「没能带回多少」告知用户(review P1:静默丢等于偷走内容)。++ const { items: followUps, cancelledUploadCount } = takeOutboxForSession(sessionId, 'cancel');++ if (followUps.length > 0) {++ const { merged, dropped } = mergeAttachmentsWithinLimit(++ prepared.attachments,++ followUps.flatMap(outboxItemAttachments),++ );++ // 装不下的中转对象在这里回收:草稿带不走它们,留着就是没人认领的垃圾。++ discardRecoveredAttachments(dropped);++ const unrecoveredCount = dropped.length + cancelledUploadCount;++ stashNewSessionDraftForEdit(prepared, {++ draft: {++ ...prepared.draft,++ firstMessage: [++ prepared.draft.firstMessage,++ ...followUps.map(outboxItemDraftText),++ ].filter(Boolean).join('\n\n'),++ },++ attachments: merged,++ notice: unrecoveredCount > 0++ ? t('session.screen.attachmentsNotCarriedBack', { count: unrecoveredCount })++ : null,++ });++ } else {++ stashNewSessionDraftForEdit(prepared);++ }+ dismissNewSessionCreation(sessionId, { removeSyntheticRow: true });+ router.replace({ pathname: '/sessions/new', params: { deviceId, deviceName } });+ })+ .catch((err: unknown) => {+ // 补偿回收失败时保留 task + 合成行,绝不先跳回表单再创建第二个孤儿+ // worktree。用户可取消等待连接恢复,或直接复用同 sessionId 重试创建。+ const cleanupError = formatRemoteError(err);+ setError(t('session.screen.createFailedNotice', { message: cleanupError }));+ Alert.alert(t('session.screen.createFailedTitle'), cleanupError, [+ {+ text: t('session.common.cancel'),+ style: 'cancel',+ },+ {+ text: t('session.screen.retry'),+ onPress: () => {+ setError(null);+ retryNewSessionCreation(sessionId);+ },+ },+ ]);});- } else {- stashNewSessionDraftForEdit(creationTask);- }- dismissNewSessionCreation(sessionId, { removeSyntheticRow: true });- router.replace({ pathname: '/sessions/new', params: { deviceId, deviceName } });},},{diff --cc apps/mobile/src/__tests__/newSessionCreation.test.tsindex 7827ecea41,2d3e4a6777..fecae4e927--- a/apps/mobile/src/__tests__/newSessionCreation.test.ts+++ b/apps/mobile/src/__tests__/newSessionCreation.test.ts@@@ -382,146 -356,4 +383,177 @@@ describe('newSessionCreation pipeline'expect(remoteSessionStore.getSessions().find((s) => s.id === 's1')).toBeUndefined();expect(remoteSessionStore.getInputProjection('s1').pendingQueue).toHaveLength(0);});++ it('create-failed 返回编辑前回收预创建 worktree,并把原项目目录放回草稿', async () => {+ const maker = makeMaker({+ createSession: vi.fn(async () => {+ throw new Error('INVALID_PARAMS: cannot create session');+ }),+ });+ const params = makeParams('s13', maker, {+ draft: {+ ...DRAFT,+ workingDir: '/repo/.cindy-worktrees/auto-one',+ },+ precreatedWorktree: {+ path: '/repo/.cindy-worktrees/auto-one',+ recoveryKey: 'recovery-key-1234567890',+ originalWorkingDir: '/repo',+ },+ });+ startNewSessionCreation(params);+ await flushPipeline();++ const failed = getNewSessionCreationTask('s13');+ expect(failed?.status).toBe('create-failed');++ const prepared = await prepareNewSessionCreationForEdit('s13');+ expect(prepared).not.toBeNull();+ expect(params.transport.openLink).toHaveBeenCalledWith('dev-1');+ expect(maker.worktree.discardPrecreated).toHaveBeenCalledWith({+ sessionId: 's13',+ path: '/repo/.cindy-worktrees/auto-one',+ });++ stashNewSessionDraftForEdit(prepared!);+ expect(drainStashedNewSessionDraft()?.draft.workingDir).toBe('/repo');+ });+++ it('带回创建期间后续消息时仍恢复预创建 worktree 的原项目目录', async () => {++ const task = {++ ...makeParams('s13-override', makeMaker(), {++ draft: {++ ...DRAFT,++ workingDir: '/repo/.cindy-worktrees/auto-override',++ },++ precreatedWorktree: {++ path: '/repo/.cindy-worktrees/auto-override',++ recoveryKey: 'recovery-key-override-123456',++ originalWorkingDir: '/repo',++ },++ }),++ status: 'create-failed' as const,++ error: 'create failed',++ firstMessageClientId: 'client-override',++ };++++ stashNewSessionDraftForEdit(task, {++ draft: {++ ...task.draft,++ firstMessage: 'hello world\n\nfollow up',++ },++ });++++ expect(drainStashedNewSessionDraft()?.draft).toMatchObject({++ workingDir: '/repo',++ firstMessage: 'hello world\n\nfollow up',++ });++ });+++ it('create-failed cleanup preserves the task when the new channel fails', async () => {+ const maker = makeMaker({+ createSession: vi.fn(async () => {+ throw new Error('INVALID_PARAMS: cannot create session');+ }),+ worktree: {+ discardPrecreated: vi.fn(async () => {+ throw Object.assign(new Error('registered path mismatch'), {+ code: 'PERMISSION_DENIED',+ });+ }),+ },+ });+ startNewSessionCreation(makeParams('s14', maker, {+ draft: {+ ...DRAFT,+ workingDir: '/repo/.cindy-worktrees/auto-two',+ },+ precreatedWorktree: {+ path: '/repo/.cindy-worktrees/auto-two',+ recoveryKey: 'recovery-key-1234567890',+ originalWorkingDir: '/repo',+ },+ }));+ await flushPipeline();++ await expect(+ prepareNewSessionCreationForEdit('s14'),+ ).rejects.toMatchObject({ code: 'PERMISSION_DENIED' });+ expect(getNewSessionCreationTask('s14')?.status).toBe('create-failed');+ });++ it('old desktop without discard-precreated falls back to startup orphan reconciliation', async () => {+ const maker = makeMaker({+ createSession: vi.fn(async () => {+ throw new Error('INVALID_PARAMS: cannot create session');+ }),+ worktree: {+ discardPrecreated: vi.fn(async () => {+ throw Object.assign(new Error('channel not allowed remotely'), {+ code: 'CHANNEL_NOT_ALLOWED',+ });+ }),+ },+ });+ startNewSessionCreation(makeParams('s15', maker, {+ draft: {+ ...DRAFT,+ workingDir: '/repo/.cindy-worktrees/auto-three',+ },+ precreatedWorktree: {+ path: '/repo/.cindy-worktrees/auto-three',+ recoveryKey: 'recovery-key-1234567890',+ originalWorkingDir: '/repo',+ },+ }));+ await flushPipeline();++ await expect(+ prepareNewSessionCreationForEdit('s15'),+ ).resolves.toMatchObject({+ sessionId: 's15',+ status: 'create-failed',+ });+ expect(maker.worktree.discardPrecreated).toHaveBeenCalledTimes(1);+ });++ it('回收发现会话已认领时重新对账真实会话,不把用户困在 create-failed', async () => {+ const worktreePath = '/repo/.cindy-worktrees/auto-four';+ const claimedSession = sessionFromCreateResult(+ { sessionId: 's16', workDir: worktreePath },+ DRAFT,+ );+ const maker = makeMaker({+ createSession: vi.fn(async () => {+ throw new Error('INVALID_PARAMS: create response lost');+ }),+ getSession: vi.fn(async () => claimedSession),+ worktree: {+ discardPrecreated: vi.fn(async () => {+ throw Object.assign(new Error('session already owns worktree'), {+ code: 'PRECONDITION_FAILED',+ });+ }),+ },+ });+ startNewSessionCreation(makeParams('s16', maker, {+ draft: { ...DRAFT, workingDir: worktreePath },+ precreatedWorktree: {+ path: worktreePath,+ recoveryKey: 'recovery-key-1234567890',+ originalWorkingDir: '/repo',+ },+ }));+ await flushPipeline();+ expect(getNewSessionCreationTask('s16')?.status).toBe('create-failed');++ await expect(prepareNewSessionCreationForEdit('s16')).resolves.toBeNull();+ expect(getNewSessionCreationTask('s16')?.status).toBe('enqueue-failed');+ expect(remoteSessionStore.getSessions().find((s) => s.id === 's16')).toMatchObject({+ id: 's16',+ workingDir: worktreePath,+ pendingLocalCreation: false,+ });+ expect(remoteSessionStore.getInputProjection('s16').pendingQueue).toHaveLength(0);+ });});diff --cc apps/mobile/src/i18n/locales/en/session.jsonindex 65c3f1daae,b938ccf8c9..e8a7aa225d--- a/apps/mobile/src/i18n/locales/en/session.json+++ b/apps/mobile/src/i18n/locales/en/session.json@@@ -152,16 -155,7 +155,16 @@@"previewAttachments": "Attachments: {{num}}","previewReadyTitle": "Ready to create and send","previewNotReadyTitle": "Not ready to create yet",- "previewReadySubtitle": "Confirming creates the session on the controlled computer and queues the first message.",- "previewReadySubtitle": "Confirming creates the session on the controlled device and queues the first message."++ "previewReadySubtitle": "Confirming creates the session on the controlled device and queues the first message.",+ "useWorktree": "Use worktree",+ "worktreeDetecting": "Detecting environment…",+ "worktreeGitMissing": "git not detected — install it first",+ "worktreeNotGitRepo": "Current directory is not a git repository",+ "worktreeAlreadyInWorktree": "Already inside a worktree, nesting is not allowed",+ "worktreeDetectFailed": "Could not check this directory — reselect it later and try again",+ "worktreeCreateFailed": "Failed to create worktree: {{message}}",+ "worktreeRecoveryStateFailed": "Could not save the worktree recovery state. Try again.",+ "worktreeCleanupPending": "The previous worktree is still awaiting cleanup. Reconnect the device and try again."},"menu": {"chipPinned": "Pinned",diff --cc apps/mobile/src/i18n/locales/ja/session.jsonindex 5025f2e65f,1d5034c072..2b75d989a9--- a/apps/mobile/src/i18n/locales/ja/session.json+++ b/apps/mobile/src/i18n/locales/ja/session.json@@@ -152,16 -155,7 +155,16 @@@"previewAttachments": "添付:{{num}} 件","previewReadyTitle": "作成して送信する準備ができました","previewNotReadyTitle": "まだ作成できません",- "previewReadySubtitle": "確認すると、操作対象のパソコンにセッションを作成し、最初のメッセージをキューに追加します。",- "previewReadySubtitle": "確認すると、操作対象のデバイスにセッションを作成し、最初のメッセージをキューに追加します。"++ "previewReadySubtitle": "確認すると、操作対象のデバイスにセッションを作成し、最初のメッセージをキューに追加します。",+ "useWorktree": "worktree を使用",+ "worktreeDetecting": "環境を検出中…",+ "worktreeGitMissing": "git が検出されません。先にインストールしてください",+ "worktreeNotGitRepo": "現在のディレクトリは git リポジトリではありません",+ "worktreeAlreadyInWorktree": "すでに worktree 内です。ネストは禁止です",+ "worktreeDetectFailed": "ディレクトリ環境を確認できません。後でディレクトリを選び直してもう一度お試しください",+ "worktreeCreateFailed": "worktree の作成に失敗しました:{{message}}",+ "worktreeRecoveryStateFailed": "worktree の復旧状態を保存できませんでした。もう一度お試しください。",+ "worktreeCleanupPending": "前回の worktree はまだクリーンアップ待ちです。デバイスを再接続してから再試行してください。"},"menu": {"chipPinned": "ピン留め済み",diff --cc apps/mobile/src/i18n/locales/ko/session.jsonindex 96c5df7e97,b245d16b8a..700239d637--- a/apps/mobile/src/i18n/locales/ko/session.json+++ b/apps/mobile/src/i18n/locales/ko/session.json@@@ -152,16 -155,7 +155,16 @@@"previewAttachments": "첨부: {{num}}개","previewReadyTitle": "만들고 보낼 준비 완료","previewNotReadyTitle": "아직 만들 수 없습니다",- "previewReadySubtitle": "확인하면 제어 컴퓨터에 세션을 만들고 첫 메시지를 대기열에 추가합니다.",- "previewReadySubtitle": "확인하면 제어 기기에 세션을 만들고 첫 메시지를 대기열에 추가합니다."++ "previewReadySubtitle": "확인하면 제어 기기에 세션을 만들고 첫 메시지를 대기열에 추가합니다.",+ "useWorktree": "worktree 사용",+ "worktreeDetecting": "환경 감지 중…",+ "worktreeGitMissing": "git이 감지되지 않습니다. 먼저 설치하세요",+ "worktreeNotGitRepo": "현재 디렉터리는 git 리포지토리가 아닙니다",+ "worktreeAlreadyInWorktree": "이미 worktree 안에 있어 중첩이 금지됩니다",+ "worktreeDetectFailed": "디렉터리 환경을 확인할 수 없습니다. 나중에 디렉터리를 다시 선택해 시도하세요",+ "worktreeCreateFailed": "worktree 생성 실패: {{message}}",+ "worktreeRecoveryStateFailed": "worktree 복구 상태를 저장할 수 없습니다. 다시 시도하세요.",+ "worktreeCleanupPending": "이전 worktree가 아직 정리 대기 중입니다. 기기를 다시 연결한 후 재시도하세요."},"menu": {"chipPinned": "고정됨",diff --cc apps/mobile/src/i18n/locales/zh-CN/session.jsonindex a7ded6cdd0,1ec5b93bd4..ac9183adc6--- a/apps/mobile/src/i18n/locales/zh-CN/session.json+++ b/apps/mobile/src/i18n/locales/zh-CN/session.json@@@ -152,16 -155,7 +155,16 @@@"previewAttachments": "附件:{{num}} 个","previewReadyTitle": "准备创建并发送","previewNotReadyTitle": "还不能创建",- "previewReadySubtitle": "确认后会在被控电脑创建对话,并把首条消息加入队列。",- "previewReadySubtitle": "确认后会在被控设备创建对话,并把首条消息加入队列。"++ "previewReadySubtitle": "确认后会在被控设备创建对话,并把首条消息加入队列。",+ "useWorktree": "使用 worktree",+ "worktreeDetecting": "检测环境中…",+ "worktreeGitMissing": "未检测到 git,请先安装",+ "worktreeNotGitRepo": "当前目录不是 git 仓库",+ "worktreeAlreadyInWorktree": "已在 worktree 中,禁止嵌套",+ "worktreeDetectFailed": "无法检测目录环境,可稍后重选目录再试",+ "worktreeCreateFailed": "worktree 创建失败:{{message}}",+ "worktreeRecoveryStateFailed": "无法保存 worktree 恢复状态,请重试。",+ "worktreeCleanupPending": "上一次 worktree 仍待回收,请重新连接设备后再试。"},"menu": {"chipPinned": "已置顶",diff --cc apps/mobile/src/session/newSessionCreation.tsindex a60857228e,d1ce7ecc01..028413449f--- a/apps/mobile/src/session/newSessionCreation.ts+++ b/apps/mobile/src/session/newSessionCreation.ts@@@ -426,17 -320,24 +434,35 @@@ export interface StashedNewSessionDraflet stashedDraft: StashedNewSessionDraft | null = null;- export function stashNewSessionDraftForEdit(task: NewSessionCreationTask): void {+ /**+ * override 用于把「创建期间攒下的后续消息」并进带回新建页的草稿:那些消息此刻在会话页的+ * outbox 里,而合成会话行紧接着就被删掉,不并进来就再也找不回(review P1)。+ */+ export function stashNewSessionDraftForEdit(+ task: NewSessionCreationTask,+ override?: {+ draft?: NewSessionDraft;+ attachments?: readonly RemoteSerializedAttachment[];+ notice?: string | null;+ },+ ): void {++ const baseDraft = task.precreatedWorktree++ ? {++ ...task.draft,++ workingDir: task.precreatedWorktree.originalWorkingDir,++ }++ : task.draft;stashedDraft = {deviceId: task.deviceId,deviceName: task.deviceName,- draft: task.precreatedWorktree- draft: override?.draft ?? task.draft,++ draft: override?.draft+ ? {- ...task.draft,- workingDir: task.precreatedWorktree.originalWorkingDir,++ ...override.draft,++ workingDir: baseDraft.workingDir,+ }- : task.draft,- attachments: task.attachments,++ : baseDraft,+ attachments: override?.attachments ?? task.attachments,+ notice: override?.notice ?? null,};}