Skip to content

armgit / cindy

Public Imported from GitHub
0

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

fix(mobile): 新建任务页 worktree 开关检测后再出现,不再引起界面跳动 (#5794)

Dasha29a05f8d584
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index 9ecbec0b80..a3f8fb1ed1 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -379,6 +379,7 @@ import {
resolveWorktreeEligibility,
shouldAcceptWorktreeBranchListResult,
shouldBlockNewSessionCreateForWorktree,
+ settleWorktreeEligibilityForDisplay,
shouldShowWorktreeToggle,
worktreeEligibilityForTarget,
worktreeEligibilityCaptionKey,
@@ -386,6 +387,7 @@ import {
worktreeSourceBranchFromPreference,
type NewSessionWorktreeEligibility,
type NewSessionWorktreeProbeSnapshot,
+ type NewSessionWorktreeSettledSnapshot,
} from '@/session/newSessionWorktree';
import { mobileAgentLabel, mobileAgentVendor } from '@/session/sessionAgentSwitch';
import { MobileModelIconMark } from '@/session/MobileProviderMark';
@@ -766,6 +768,7 @@ export default function NewRemoteSessionScreen() {
const worktreeBranchRenderedRef = useRef({ key: '', sourceBranch: '' });
const worktreeBranchTargetRef = useRef({ deviceId: '', workingDir: '' });
const worktreeEligibilityRef = useRef<NewSessionWorktreeEligibility>({ status: 'probing' });
+ const worktreeSettledRef = useRef<NewSessionWorktreeSettledSnapshot | null>(null);
const worktreeSourceBranchRef = useRef('HEAD');
const worktreeSeedSeqRef = useRef(0);
const [worktreeSeedRetryNonce, setWorktreeSeedRetryNonce] = useState(0);
@@ -1645,12 +1648,21 @@ export default function NewRemoteSessionScreen() {
: [],
[worktreeBranchList, worktreeBranchListMatchesTarget],
);
- // 老被控端通常隐藏开关;若工作端旧镜像仍为 ON,则保留显式关闭入口,避免
- // fail-closed 后既不能创建、也不能解除状态。
+ // 开关行按「已有定论」显示:首次探测完成前不出现,重连重探沿用上次定论不闪。
+ const worktreeSettled = settleWorktreeEligibilityForDisplay(
+ worktreeEligibility,
+ worktreeTarget,
+ worktreeSettledRef.current,
+ );
+ // 只在提交后记录「已展示的定论」:被放弃的并发 render 不能污染它。
+ useLayoutEffect(() => {
+ worktreeSettledRef.current = worktreeSettled;
+ }, [worktreeSettled]);
const worktreeRowVisible = shouldShowWorktreeToggle({
workspaceKind: draft.workspaceKind,
workingDir: draft.workingDir,
eligibility: worktreeEligibility,
+ settled: worktreeSettled?.eligibility ?? null,
enabled: worktreeEnabled,
});
const worktreePreferenceSaving =
@@ -1700,7 +1712,11 @@ export default function NewRemoteSessionScreen() {
// 2026-09-11: show this draft's explicit choice, otherwise the last host mirror.
// Eligibility failures still cannot silently downgrade an enabled worktree.
const worktreeChecked = worktreeEnabled;
- const worktreeCaptionKey = worktreeEligibilityCaptionKey(worktreeEligibility);
+ // caption 会多占一行、把上方选择器顶起,只在它解释「已勾选但创建被拦」时显示:
+ // 探测中不显示;未勾选时创建不受资格影响,恢复中 / 探测失败也不显示。
+ const worktreeCaptionKey = worktreeEnabled && worktreeEligibility.status !== 'probing'
+ ? worktreeEligibilityCaptionKey(worktreeEligibility)
+ : null;
const worktreeCreateBlocked = shouldBlockNewSessionCreateForWorktree({
applicable: worktreeApplicable,
enabled: worktreeEnabled,
@@ -1716,7 +1732,6 @@ export default function NewRemoteSessionScreen() {
));
const worktreeControlCaptionKey = worktreeCaptionKey
?? (worktreePreferenceAuthorityUnknown ? 'session.new.worktreeSettingsSyncFailed' : null)
- ?? (worktreePreferenceSaving ? 'session.new.worktreeSettingsSaving' : null)
?? (worktreeBranchPreferenceError ? 'session.new.worktreeBranchSyncFailed' : null);
const resolveWorktreeCreateErrorKey = useCallback(() => (
worktreeEligibilityCaptionKey(worktreeEligibilityRef.current)
@@ -6292,7 +6307,11 @@ export default function NewRemoteSessionScreen() {
</Text>
) : null}
</View>
- ) : null}
+ ) : (
+ // 开关行不显示时占住同样高度:检测结果回来后开关出现,上方选择器
+ // 也不会被顶起;同时作为选择器与输入框之间的留白。
+ <View style={styles.worktreeSlotPlaceholder} testID="newSession.worktreeSlot" />
+ )}
</View>
{!nativeSelectionSheet && browseOpen ? (
@@ -7430,6 +7449,9 @@ const makeStyles = (colors: ThemeColors) => StyleSheet.create({
worktreeToggleWrap: {
gap: spacing.xs,
},
+ worktreeSlotPlaceholder: {
+ height: 44,
+ },
worktreeControlTouchWrap: {
alignItems: 'center',
alignSelf: 'flex-start',
diff --git a/apps/mobile/src/__tests__/newSessionWorktree.test.ts b/apps/mobile/src/__tests__/newSessionWorktree.test.ts
index 6f563fd0ea..86ea97c939 100644
--- a/apps/mobile/src/__tests__/newSessionWorktree.test.ts
+++ b/apps/mobile/src/__tests__/newSessionWorktree.test.ts
@@ -32,7 +32,9 @@ import {
worktreeEligibilityFromError,
worktreeSourceBranchFromPreference,
worktreeSourceBranchForTarget,
+ settleWorktreeEligibilityForDisplay,
type NewSessionWorktreeEligibility,
+ type NewSessionWorktreeSettledEligibility,
} from '@/session/newSessionWorktree';
const DETECT_OK: MobileWorktreeDetectCwdResult = {
@@ -480,37 +482,71 @@ describe('shouldShowWorktreeToggle / caption key', () => {
status: 'eligible', baseRepo: '/repo', sourceBranch: 'main',
};
- it('project + 已选目录才显示;unsupported 仅在旧 ON 镜像需要退出入口时显示', () => {
- expect(shouldShowWorktreeToggle({ workspaceKind: 'project', workingDir: '/repo', eligibility: eligible, enabled: false })).toBe(true);
- expect(shouldShowWorktreeToggle({ workspaceKind: 'dialogue', workingDir: '/repo', eligibility: eligible, enabled: true })).toBe(false);
- expect(shouldShowWorktreeToggle({ workspaceKind: 'project', workingDir: ' ', eligibility: eligible, enabled: true })).toBe(false);
- expect(shouldShowWorktreeToggle({ workspaceKind: 'project', workingDir: '/repo', eligibility: { status: 'unsupported' }, enabled: false })).toBe(false);
- expect(shouldShowWorktreeToggle({ workspaceKind: 'project', workingDir: '/repo', eligibility: { status: 'unsupported' }, enabled: true })).toBe(true);
- expect(shouldShowWorktreeToggle({ workspaceKind: 'project', workingDir: '/repo', eligibility: { status: 'probing' }, enabled: false })).toBe(true);
+ const show = (
+ eligibility: NewSessionWorktreeEligibility,
+ enabled: boolean,
+ settled: NewSessionWorktreeSettledEligibility | null = eligibility.status === 'eligible'
+ || eligibility.status === 'ineligible'
+ || eligibility.status === 'unsupported'
+ ? eligibility
+ : null,
+ workspace: { workspaceKind: 'project' | 'dialogue'; workingDir: string } = {
+ workspaceKind: 'project',
+ workingDir: '/repo',
+ },
+ ) => shouldShowWorktreeToggle({ ...workspace, eligibility, settled, enabled });
+
+ it('project + 已选目录且确认可用才显示;unsupported 仅在旧 ON 镜像需要退出入口时显示', () => {
+ expect(show(eligible, false)).toBe(true);
+ expect(show(eligible, true, eligible, { workspaceKind: 'dialogue', workingDir: '/repo' })).toBe(false);
+ expect(show(eligible, true, eligible, { workspaceKind: 'project', workingDir: ' ' })).toBe(false);
+ expect(show({ status: 'unsupported' }, false)).toBe(false);
+ expect(show({ status: 'unsupported' }, true)).toBe(true);
+ });
+
+ it('首次探测中不显示开关行,勾选记忆为 ON 也一样(2026-10-11 裁决)', () => {
+ expect(show({ status: 'probing' }, false)).toBe(false);
+ expect(show({ status: 'probing' }, true)).toBe(false);
+ });
+
+ it('同目标重探沿用上次定论:已显示的开关行不因重连闪掉', () => {
+ expect(show({ status: 'probing' }, false, eligible)).toBe(true);
+ expect(show({ status: 'recovering' }, false, eligible)).toBe(true);
+ expect(show({ status: 'probing' }, true, { status: 'ineligible', reason: 'notGitRepo' })).toBe(false);
+ });
+
+ it('已勾选时实时恢复中 / 探测失败优先于沿用的旧 ineligible:创建被拦就必须露出关闭入口', () => {
+ const notGit: NewSessionWorktreeSettledEligibility = { status: 'ineligible', reason: 'notGitRepo' };
+ for (const live of [{ status: 'recovering' }, { status: 'detect-failed' }] as const) {
+ expect(shouldBlockNewSessionCreateForWorktree({
+ applicable: true, enabled: true, eligibility: live, preferenceSaving: false,
+ })).toBe(true);
+ expect(show(live, true, notGit)).toBe(true);
+ expect(show(live, false, notGit)).toBe(false);
+ }
});
it('确认不合格(ineligible)时开关行隐藏,即使记忆为 ON(2026-08-07 裁决)', () => {
for (const reason of ['gitMissing', 'notGitRepo', 'alreadyInWorktree'] as const) {
- expect(shouldShowWorktreeToggle({
- workspaceKind: 'project',
- workingDir: '/repo',
- eligibility: { status: 'ineligible', reason },
- enabled: true,
- })).toBe(false);
- expect(shouldShowWorktreeToggle({
- workspaceKind: 'project',
- workingDir: '/repo',
- eligibility: { status: 'ineligible', reason },
- enabled: false,
- })).toBe(false);
+ expect(show({ status: 'ineligible', reason }, true)).toBe(false);
+ expect(show({ status: 'ineligible', reason }, false)).toBe(false);
}
- // 探测失败 ≠ 确认不合格:已 ON 时保持显示(fail closed 需要可见入口)。
- expect(shouldShowWorktreeToggle({
- workspaceKind: 'project',
- workingDir: '/repo',
- eligibility: { status: 'detect-failed' },
- enabled: true,
- })).toBe(true);
+ // 探测失败 / 恢复中 ≠ 确认不合格:已 ON 时显示(fail closed 需要可见原因与关闭入口)。
+ expect(show({ status: 'detect-failed' }, true)).toBe(true);
+ expect(show({ status: 'recovering' }, true)).toBe(true);
+ // 未勾选时不拦创建,无需提前露出开关。
+ expect(show({ status: 'detect-failed' }, false)).toBe(false);
+ expect(show({ status: 'recovering' }, false)).toBe(false);
+ });
+
+ it('settleWorktreeEligibilityForDisplay:有定论即更新,探测中只沿用同设备 + 目录的旧定论', () => {
+ const target = { deviceId: 'd1', workingDir: '/repo' };
+ const settled = settleWorktreeEligibilityForDisplay(eligible, target, null);
+ expect(settled).toEqual({ target, eligibility: eligible });
+ expect(settleWorktreeEligibilityForDisplay({ status: 'probing' }, target, settled)).toBe(settled);
+ expect(settleWorktreeEligibilityForDisplay({ status: 'detect-failed' }, target, settled)).toBe(settled);
+ expect(settleWorktreeEligibilityForDisplay({ status: 'probing' }, { deviceId: 'd1', workingDir: '/other' }, settled)).toBeNull();
+ expect(settleWorktreeEligibilityForDisplay({ status: 'probing' }, { deviceId: 'd2', workingDir: '/repo' }, settled)).toBeNull();
});
it('caption key 覆盖探测中 / 三种不合格原因 / 探测失败;eligible 无 caption', () => {
diff --git a/apps/mobile/src/i18n/locales/en/session.json b/apps/mobile/src/i18n/locales/en/session.json
index a2707176f1..9c9d366782 100644
--- a/apps/mobile/src/i18n/locales/en/session.json
+++ b/apps/mobile/src/i18n/locales/en/session.json
@@ -375,7 +375,6 @@
"worktreeBranchesLoading": "Loading branches…",
"worktreeBranchSaving": "Syncing the selected branch…",
"worktreeBranchSyncFailed": "Could not confirm the saved source branch — retry the branch selection",
- "worktreeSettingsSaving": "Syncing the worktree setting…",
"worktreeSettingsSyncFailed": "Could not save the worktree default. You can still use this selection.",
"worktreeBranchesRetry": "Could not load branches. Tap to retry",
"worktreeDetecting": "Detecting environment…",
diff --git a/apps/mobile/src/i18n/locales/ja/session.json b/apps/mobile/src/i18n/locales/ja/session.json
index bcdab9d524..95768a4e46 100644
--- a/apps/mobile/src/i18n/locales/ja/session.json
+++ b/apps/mobile/src/i18n/locales/ja/session.json
@@ -375,7 +375,6 @@
"worktreeBranchesLoading": "ブランチを読み込み中…",
"worktreeBranchSaving": "選択したブランチを同期中…",
"worktreeBranchSyncFailed": "保存済みのソースブランチを確認できません。ブランチを再選択してください",
- "worktreeSettingsSaving": "worktree 設定を同期中…",
"worktreeSettingsSyncFailed": "worktree の既定設定を保存できませんでした。現在の選択で続行できます。",
"worktreeBranchesRetry": "ブランチを読み込めません。タップして再試行",
"worktreeDetecting": "環境を検出中…",
diff --git a/apps/mobile/src/i18n/locales/ko/session.json b/apps/mobile/src/i18n/locales/ko/session.json
index bfb12726ab..609b925264 100644
--- a/apps/mobile/src/i18n/locales/ko/session.json
+++ b/apps/mobile/src/i18n/locales/ko/session.json
@@ -371,7 +371,6 @@
"worktreeBranchesLoading": "브랜치 불러오는 중…",
"worktreeBranchSaving": "선택한 브랜치 동기화 중…",
"worktreeBranchSyncFailed": "저장된 소스 브랜치를 확인할 수 없습니다. 브랜치를 다시 선택해 주세요",
- "worktreeSettingsSaving": "worktree 설정 동기화 중…",
"worktreeSettingsSyncFailed": "worktree 기본 설정을 저장하지 못했습니다. 현재 선택으로 계속할 수 있습니다.",
"worktreeBranchesRetry": "브랜치를 불러오지 못했습니다. 탭하여 다시 시도",
"worktreeDetecting": "환경 감지 중…",
diff --git a/apps/mobile/src/i18n/locales/zh-CN/session.json b/apps/mobile/src/i18n/locales/zh-CN/session.json
index cbf1525edb..0320d482a2 100644
--- a/apps/mobile/src/i18n/locales/zh-CN/session.json
+++ b/apps/mobile/src/i18n/locales/zh-CN/session.json
@@ -371,7 +371,6 @@
"worktreeBranchesLoading": "正在读取分支…",
"worktreeBranchSaving": "正在同步所选分支…",
"worktreeBranchSyncFailed": "无法确认已保存的源分支,请重新选择分支",
- "worktreeSettingsSaving": "正在同步 worktree 设置…",
"worktreeSettingsSyncFailed": "未能保存 worktree 默认设置,仍可按当前选择继续",
"worktreeBranchesRetry": "无法读取分支,点按重试",
"worktreeDetecting": "检测环境中…",
diff --git a/apps/mobile/src/i18n/locales/zh-TW/session.json b/apps/mobile/src/i18n/locales/zh-TW/session.json
index d1d2bb0a3b..aeec798f46 100644
--- a/apps/mobile/src/i18n/locales/zh-TW/session.json
+++ b/apps/mobile/src/i18n/locales/zh-TW/session.json
@@ -375,7 +375,6 @@
"worktreeBranchesLoading": "正在讀取分支…",
"worktreeBranchSaving": "正在同步所選分支…",
"worktreeBranchSyncFailed": "無法確認已儲存的源分支,請重新選擇分支",
- "worktreeSettingsSaving": "正在同步 worktree 設定…",
"worktreeSettingsSyncFailed": "無法儲存 worktree 預設設定,仍可依目前選擇繼續",
"worktreeBranchesRetry": "無法讀取分支,點按重試",
"worktreeDetecting": "檢測環境中…",
diff --git a/apps/mobile/src/session/newSessionWorktree.ts b/apps/mobile/src/session/newSessionWorktree.ts
index e976056976..716caa4c98 100644
--- a/apps/mobile/src/session/newSessionWorktree.ts
+++ b/apps/mobile/src/session/newSessionWorktree.ts
@@ -25,7 +25,7 @@ export type NewSessionWorktreeIneligibleReason =
/**
* worktree 开关的资格状态:
- * - probing:探测中(目录/设备刚变化,结果未回)→ 开关禁用 + 「检测环境中…」;
+ * - probing:探测中(目录/设备刚变化,结果未回)→ 首次探测不显示开关行,重探沿用上次定论;
* - recovering:瞬时断连/超时,页面会自动重试 → 开关禁用 + 恢复连接 caption;
* - eligible:可用,携带 create 所需的 baseRepo(repoRoot)与 sourceBranch(当前分支);
* - ineligible:环境不满足 → 开关禁用 + 原因 caption;
@@ -345,25 +345,71 @@ export function isWorktreeChannelNotAllowedError(error: unknown): boolean {
return remoteErrorText(error).includes('CHANNEL_NOT_ALLOWED');
}
+/** 已有定论的资格(探测中 / 恢复中 / 探测失败都不算定论)。 */
+export type NewSessionWorktreeSettledEligibility = Extract<
+ NewSessionWorktreeEligibility,
+ { status: 'eligible' | 'ineligible' | 'unsupported' }
+>;
+
+/** 某个设备 + 目录最近一次有定论的探测结果;不含连接代次,重连重探时仍可沿用。 */
+export interface NewSessionWorktreeSettledSnapshot {
+ target: NewSessionWorktreeBranchTarget;
+ eligibility: NewSessionWorktreeSettledEligibility;
+}
+
+function isSettledWorktreeEligibility(
+ eligibility: NewSessionWorktreeEligibility,
+): eligibility is NewSessionWorktreeSettledEligibility {
+ return eligibility.status === 'eligible'
+ || eligibility.status === 'ineligible'
+ || eligibility.status === 'unsupported';
+}
+
/**
- * 开关行是否显示:正常情况下是 project + workingDir + 通道可用;若老端不支持但
- * 镜像仍为 ON,保留已勾选的关闭入口,不能把用户锁在 fail-closed 状态。
- *
- * 2026-08-07 用户裁决(对齐桌面 WorktreeChipsRow):勾选记忆只对具备资格的目录
- * 生效——探测**成功**且确认不合格(ineligible:非 git / 无 git / 已在 worktree 内)
- * 时开关行隐藏、创建按普通会话放行,记忆保留;probing / detect-failed 不算确认,
- * 维持显示 + fail closed,一次断连不能把用户要求的隔离静默降级。
+ * 开关行展示用的定论:当前探测已有定论就用当前的;还没有时(重连 / presence 变化
+ * 触发的重探、恢复中重试)沿用同设备 + 目录上一次定论,避免开关行闪掉再出现。
+ * 只决定显示,不参与创建门禁——门禁仍按实时资格 fail closed。
+ */
+export function settleWorktreeEligibilityForDisplay(
+ eligibility: NewSessionWorktreeEligibility,
+ target: NewSessionWorktreeBranchTarget,
+ previous: NewSessionWorktreeSettledSnapshot | null,
+): NewSessionWorktreeSettledSnapshot | null {
+ if (isSettledWorktreeEligibility(eligibility)) return { target, eligibility };
+ if (
+ previous
+ && previous.target.deviceId === target.deviceId
+ && previous.target.workingDir.trim() === target.workingDir.trim()
+ ) {
+ return previous;
+ }
+ return null;
+}
+
+/**
+ * 开关行是否显示(2026-10-11 用户裁决:宁可检测出来再出现,也不要先出现再消失):
+ * - 必须是 project + 已选目录;
+ * - 定论为 eligible 才显示;从未有定论(首次探测中)不显示;
+ * - 确认不合格(ineligible)隐藏,创建按普通会话放行(2026-08-07 裁决);
+ * - 已勾选但创建被拦住时(老端 unsupported、恢复中、探测失败)必须显示,
+ * 让用户看到原因并能关掉,不把用户锁在 fail-closed 状态。实时恢复中 / 探测失败
+ * 优先于沿用的旧定论(含旧 ineligible),因为创建门禁按实时资格拦截。
*/
export function shouldShowWorktreeToggle(input: {
workspaceKind: 'project' | 'dialogue';
workingDir: string;
+ /** 实时资格。 */
eligibility: NewSessionWorktreeEligibility;
+ /** settleWorktreeEligibilityForDisplay 的结果。 */
+ settled: NewSessionWorktreeSettledEligibility | null;
enabled: boolean;
}): boolean {
- return input.workspaceKind === 'project'
- && input.workingDir.trim().length > 0
- && input.eligibility.status !== 'ineligible'
- && (input.eligibility.status !== 'unsupported' || input.enabled);
+ if (input.workspaceKind !== 'project' || input.workingDir.trim().length === 0) return false;
+ if (input.settled?.status === 'eligible') return true;
+ if (!input.enabled) return false;
+ return input.eligibility.status === 'recovering'
+ || input.eligibility.status === 'detect-failed'
+ || input.settled?.status === 'unsupported';
}
/**