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 探测进入「连接恢复中」,不再永久停在「检测环境中…」 (#4050)

fico-hub2b4bf591e4db
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index 5ab537ba47..c6cf705364 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -311,6 +311,7 @@ import {
isValidWorktreeBranchPreferenceSnapshot,
isWorktreeChannelNotAllowedError,
parseWorktreeCreateResult,
+ initialWorktreeProbeEligibility,
resolveWorktreeEligibility,
shouldAcceptWorktreeBranchListResult,
shouldBlockNewSessionCreateForWorktree,
@@ -2286,9 +2287,16 @@ export default function NewRemoteSessionScreen() {
};
let cancelled = false;
let retryTimer: ReturnType<typeof setTimeout> | null = null;
- const probingEligibility: NewSessionWorktreeEligibility = { status: 'probing' };
- worktreeEligibilityRef.current = probingEligibility;
- setWorktreeProbe({ target, eligibility: probingEligibility });
+ // 起始状态由纯函数决定(#4046):设备与目录都已选、但链路不在 online(如完全断网)
+ // 时直接进 recovering 而不是停在 probing —— 下面的提前返回不会进 catch,停在
+ // probing 就是永久的「检测环境中…」。
+ const initialEligibility: NewSessionWorktreeEligibility = initialWorktreeProbeEligibility({
+ hasDevice: Boolean(selectedDeviceId),
+ hasWorkingDir: cwd.length > 0,
+ online: deviceLinkStatus === 'online',
+ });
+ worktreeEligibilityRef.current = initialEligibility;
+ setWorktreeProbe({ target, eligibility: initialEligibility });
// Relay 离线时不把预期的 NOT_CONNECTED 固化成 detect-failed;online /
// connectionEpoch / presenceVersion 变化后自动重探,覆盖手机重连和工作端
// 重新上线两种路径。
diff --git a/apps/mobile/src/__tests__/newSession.test.ts b/apps/mobile/src/__tests__/newSession.test.ts
index 04242d6373..d3fd5eed33 100644
--- a/apps/mobile/src/__tests__/newSession.test.ts
+++ b/apps/mobile/src/__tests__/newSession.test.ts
@@ -2561,3 +2561,38 @@ describe('resolveStartedDowngradeOrCommit —— started 落账后设备切换
expect(restoreStarted).toHaveBeenCalledTimes(1);
});
});
+
+describe('new.tsx worktree 探测 effect 的离线起始态(#4046,源码契约)', () => {
+ const source = readTextLf(resolve(__dirname, '..', '..', 'app', 'sessions', 'new.tsx'), 'utf8');
+
+ it('起始 eligibility 经 initialWorktreeProbeEligibility 决定,不再硬编码 probing 后直接提前返回', () => {
+ expect(source).toContain('initialWorktreeProbeEligibility({');
+ expect(source).toContain("online: deviceLinkStatus === 'online',");
+ expect(source).toContain('worktreeEligibilityRef.current = initialEligibility;');
+ expect(source).toContain('setWorktreeProbe({ target, eligibility: initialEligibility });');
+ expect(source).not.toContain("const probingEligibility: NewSessionWorktreeEligibility = { status: 'probing' };");
+ // 提前返回条件与 fail-closed 语义不变:非 online 不发探测请求。
+ expect(source).toContain("if (!selectedDeviceId || !cwd || deviceLinkStatus !== 'online') return undefined;");
+ });
+
+ it('离线起始态的恢复依赖 effect 依赖数组:链路 / 连接代次 / presence / 定时重探任一变化即重跑', () => {
+ // recovering 不是终态,靠 effect 重跑回到探测;这几项漏掉任何一个,断网恢复后都会卡在
+ // 「连接恢复中」。锁住依赖数组片段,补上 initialWorktreeProbeEligibility 单测覆盖不到的那一段。
+ const effectStart = source.indexOf('initialWorktreeProbeEligibility({');
+ expect(effectStart).toBeGreaterThan(0);
+ const depsStart = source.indexOf('}, [', effectStart);
+ const depsEnd = source.indexOf(']);', depsStart);
+ const deps = source.slice(depsStart, depsEnd);
+ for (const dep of [
+ 'connectionEpoch',
+ 'deviceLinkStatus',
+ 'presenceVersion',
+ 'worktreeDetectRetryNonce',
+ 'selectedDeviceId',
+ 'draft.workspaceKind',
+ 'draft.workingDir',
+ ]) {
+ expect(deps, `effect deps 应包含 ${dep}`).toContain(dep);
+ }
+ });
+});
diff --git a/apps/mobile/src/__tests__/newSessionWorktree.test.ts b/apps/mobile/src/__tests__/newSessionWorktree.test.ts
index bf178ae92d..6f563fd0ea 100644
--- a/apps/mobile/src/__tests__/newSessionWorktree.test.ts
+++ b/apps/mobile/src/__tests__/newSessionWorktree.test.ts
@@ -16,6 +16,7 @@ import {
classifyWorktreePreferenceSeed,
fallbackWorktreeName,
formatWorktreeCreateFailure,
+ initialWorktreeProbeEligibility,
isExactRemoteSessionClaimed,
isValidWorktreeBranchPreferenceSnapshot,
isWorktreeChannelNotAllowedError,
@@ -357,6 +358,38 @@ describe('worktreeEligibilityFromError', () => {
});
});
+describe('initialWorktreeProbeEligibility(#4046 完全断网时探测起始态)', () => {
+ it('设备 + 目录都已选但链路不在 online → recovering(自动重试文案,不停在检测中)', () => {
+ expect(initialWorktreeProbeEligibility({ hasDevice: true, hasWorkingDir: true, online: false }))
+ .toEqual({ status: 'recovering' });
+ });
+
+ it('设备或目录未选全 → probing(探测尚未开始,不把输入未完整误报成网络错误)', () => {
+ expect(initialWorktreeProbeEligibility({ hasDevice: false, hasWorkingDir: true, online: false }))
+ .toEqual({ status: 'probing' });
+ expect(initialWorktreeProbeEligibility({ hasDevice: true, hasWorkingDir: false, online: false }))
+ .toEqual({ status: 'probing' });
+ expect(initialWorktreeProbeEligibility({ hasDevice: false, hasWorkingDir: false, online: true }))
+ .toEqual({ status: 'probing' });
+ });
+
+ it('三者齐备 → probing,由后续请求结果 / 抛错归并覆盖', () => {
+ expect(initialWorktreeProbeEligibility({ hasDevice: true, hasWorkingDir: true, online: true }))
+ .toEqual({ status: 'probing' });
+ });
+
+ it('recovering 起始态与探测抛错的 recovering 走同一文案,且创建门禁保持 fail-closed', () => {
+ const offline = initialWorktreeProbeEligibility({ hasDevice: true, hasWorkingDir: true, online: false });
+ expect(worktreeEligibilityCaptionKey(offline)).toBe('session.new.worktreeRecovering');
+ expect(shouldBlockNewSessionCreateForWorktree({
+ applicable: true,
+ enabled: true,
+ eligibility: offline,
+ preferenceSaving: false,
+ })).toBe(true);
+ });
+});
+
describe('seedWorktreeEnabled', () => {
it('只接受工作端明确 boolean;缺字段/异常形状不覆盖当前镜像', () => {
expect(seedWorktreeEnabled({ worktreeEnabled: true })).toBe(true);
diff --git a/apps/mobile/src/session/newSessionWorktree.ts b/apps/mobile/src/session/newSessionWorktree.ts
index 49f5484278..e976056976 100644
--- a/apps/mobile/src/session/newSessionWorktree.ts
+++ b/apps/mobile/src/session/newSessionWorktree.ts
@@ -246,6 +246,24 @@ export function worktreeEligibilityFromError(error: unknown): NewSessionWorktree
return { status: 'detect-failed' };
}
+/**
+ * 探测 effect 的**起始**状态(#4046):
+ * - 设备或目录还没选全 → probing(探测尚未开始,UI 不暴露该行);
+ * - 设备与目录都在、但链路不在 online → recovering(「连接恢复中,正在自动重试…」)。
+ * 完全断网时 effect 会在发请求前直接返回,永远进不了 catch,若仍停在 probing 就是
+ * 永久的「检测环境中…」;online / connectionEpoch / presenceVersion 变化即重探。
+ * - 三者齐备 → probing,随后由请求结果 / 抛错归并覆盖。
+ * 创建门禁不变:probing / recovering 都 fail-closed(shouldBlockNewSessionCreateForWorktree)。
+ */
+export function initialWorktreeProbeEligibility(input: {
+ hasDevice: boolean;
+ hasWorkingDir: boolean;
+ online: boolean;
+}): NewSessionWorktreeEligibility {
+ if (input.hasDevice && input.hasWorkingDir && !input.online) return { status: 'recovering' };
+ return { status: 'probing' };
+}
+
/**
* 读取工作端 get-new-maker-defaults 的明确偏好。缺字段/形状异常返回 null,
* 由控制端保留当前镜像;全新设备的镜像自身默认未勾选。