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): 新建任务不再卡在默认 Sonnet 4.6 (#5340)

Dash3b57300e86a1
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index 665d848ed5..c497b929b2 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -179,14 +179,18 @@ import {
mergeSlashCommands,
} from '@/session/composerPalette';
import {
+ type StoredAgentRestoreState,
DEFAULT_NEW_SESSION_DRAFT,
NEW_SESSION_AGENT_OPTIONS,
availableNewSessionAgentOptions,
+ canResolveStoredAgentRuntime,
defaultPermissionModeForNewSessionAgent,
buildRemoteCreateSessionOptions,
buildRecentWorkspaceOptions,
filterRemoteDirectoryEntries,
isCurrentRemoteBrowseRequest,
+ isStoredAgentRestorePending,
+ nextStoredAgentRestoreStep,
normalizeRemoteDirectoryDrives,
shouldRetryRemoteBrowseDrives,
normalizeCreateSessionResult,
@@ -946,7 +950,8 @@ export default function NewRemoteSessionScreen() {
const atLoadSeqRef = useRef(0);
const initialWorkspaceKeyRef = useRef<string | null>(null);
const appliedDefaultDeviceKeyRef = useRef<string | null>(null);
- const appliedStoredAgentRef = useRef<NewSessionAgentKind | null>(null);
+ // 上次 agent 的恢复进度(唯一写入者:下面的恢复 effect)。见 nextStoredAgentRestoreStep。
+ const storedAgentRestoreRef = useRef<StoredAgentRestoreState | null>(null);
// 权限记忆只在偏好加载后恢复一次(之后由用户选择 / 切 agent 驱动),防止重复覆盖。
const appliedPermissionMemoryRef = useRef(false);
const userTouchedDeviceRef = useRef(false);
@@ -1153,11 +1158,66 @@ export default function NewRemoteSessionScreen() {
const storedAgentKind = newSessionPreferences?.agentKind;
if (!newSessionPreferencesLoaded || !storedAgentKind) return;
if (userTouchedRuntimeRef.current) return;
- if (appliedStoredAgentRef.current === storedAgentKind) return;
- const expectedDeviceId = preferredDefaultDevice?.deviceId ?? '';
- if (expectedDeviceId && selectedDeviceId !== expectedDeviceId) return;
- if (selectedDeviceId && deviceProviders.loading && deviceProviders.providers.length === 0) return;
- appliedStoredAgentRef.current = storedAgentKind;
+ const restored = storedAgentRestoreRef.current;
+ if (!isStoredAgentRestorePending({
+ storedAgentKind,
+ appliedStoredAgentKind: restored?.phase === 'done' ? restored.agentKind : null,
+ expectedDeviceId: preferredDefaultDevice?.deviceId ?? '',
+ selectedDeviceId,
+ })) return;
+ // agent 偏好一到就恢复;模型要等目录或该 agent 的最近任务,否则只能落到内置兜底模型。
+ const step = nextStoredAgentRestoreStep({
+ storedAgentKind,
+ restored,
+ modelReady: canResolveStoredAgentRuntime({
+ agentKind: storedAgentKind,
+ sessions,
+ deviceId: selectedDeviceId,
+ catalogReady: deviceProviders.ready,
+ providersUnsupported: deviceProviders.unsupported,
+ }),
+ });
+ if (!step) return;
+ storedAgentRestoreRef.current = { agentKind: storedAgentKind, phase: step === 'agent' ? 'agent' : 'done' };
+ // 现场按最新目录算该 agent 的默认运行配置(rows 与 ready 必须同一代,codex review P2)。
+ const resolveStoredRuntime = (currentEffort: string) => {
+ const rowsNow = flattenProviderSections(
+ buildMobileModelSections({
+ providers: deviceProvidersRef.current.providers,
+ agentKind: storedAgentKind,
+ visibilityOverrides: deviceProvidersRef.current.modelVisibilityOverrides,
+ }).sections,
+ );
+ const next = pickAgentDefaultRuntime({
+ agentKind: storedAgentKind,
+ sessions,
+ deviceId: selectedDeviceId || undefined,
+ modelRows: rowsNow,
+ currentEffort,
+ catalogReady: catalogReadyRef.current,
+ });
+ return {
+ model: next.model,
+ effort: next.effort,
+ providerId: next.providerId,
+ // fast 按 (agent, 来源, 模型) 记忆恢复,无记忆置 false;恢复前过与手动选行
+ // 同款的 fastEditable 门控(codex review P2:目录/能力变化后不得恢复出
+ // UI 显示关、实际发 true 的矛盾态)。agent 级门控只认目标 agent 的缓存
+ // 能力表(codex review P1:此刻闭包里的 capabilities 属于切换前 agent 或
+ // 为 null);目标 caps 未就绪 → false,由延迟恢复 effect 就绪后补评。
+ fastMode: next.providerId
+ && isFastRestorable(next.agentKind, next.providerId, next.model, rowsNow, targetAgentHasFast(selectedDeviceId, next.agentKind))
+ ? (draftMemory.getFast(next.agentKind, next.providerId, next.model) ?? false)
+ : false,
+ };
+ };
+ if (step === 'model') {
+ // agent 与权限已在第一步恢复;只补模型,不动 agent 与权限(期间用户改过权限/计划模式不被覆盖)。
+ setDraft((current) => (current.agentKind === storedAgentKind
+ ? { ...current, ...resolveStoredRuntime(current.effort) }
+ : current));
+ return;
+ }
// 该路径同时负责恢复 agent 权限,下面的通用权限记忆 effect 不再重复弹框。
appliedPermissionMemoryRef.current = true;
if (selectedDeviceId) autoDefaultDeviceRef.current = selectedDeviceId;
@@ -1177,50 +1237,21 @@ export default function NewRemoteSessionScreen() {
if (deviceAtTrigger !== selectedDeviceRef.current) return;
// 确认期间用户又切了 agent / 手动选了模型 → 旧回调不得覆盖新选择。
if (seqAtTrigger !== runtimeActionSeqRef.current) return;
- setDraft((current) => {
- // rows 与 ready 必须同一代(codex review P2):提交时用最新目录现场重建,
- // 不用触发时捕获的旧 rows。
- const rowsNow = flattenProviderSections(
- buildMobileModelSections({
- providers: deviceProvidersRef.current.providers,
- agentKind: storedAgentKind,
- visibilityOverrides: deviceProvidersRef.current.modelVisibilityOverrides,
- }).sections,
- );
- const next = pickAgentDefaultRuntime({
- agentKind: storedAgentKind,
- sessions,
- deviceId: selectedDeviceId || undefined,
- modelRows: rowsNow,
- currentEffort: current.effort,
- catalogReady: catalogReadyRef.current,
- });
- return {
- ...current,
- agentKind: next.agentKind,
- model: next.model,
- effort: next.effort,
- // 上次明确选择过的权限直接沿用;内置默认若升级到 Full access 仍需确认。
- permissionMode: confirmed ? nextPermissionMode : current.permissionMode,
- providerId: next.providerId,
- // fast 按 (agent, 来源, 模型) 记忆恢复,无记忆置 false;恢复前过与手动选行
- // 同款的 fastEditable 门控(codex review P2:目录/能力变化后不得恢复出
- // UI 显示关、实际发 true 的矛盾态)。agent 级门控只认目标 agent 的缓存
- // 能力表(codex review P1:此刻闭包里的 capabilities 属于切换前 agent 或
- // 为 null);目标 caps 未就绪 → false,由延迟恢复 effect 就绪后补评。
- fastMode: next.providerId
- && isFastRestorable(next.agentKind, next.providerId, next.model, rowsNow, targetAgentHasFast(selectedDeviceId, next.agentKind))
- ? (draftMemory.getFast(next.agentKind, next.providerId, next.model) ?? false)
- : false,
- };
- });
+ setDraft((current) => ({
+ ...current,
+ ...resolveStoredRuntime(current.effort),
+ agentKind: storedAgentKind,
+ // 上次明确选择过的权限直接沿用;内置默认若升级到 Full access 仍需确认。
+ permissionMode: confirmed ? nextPermissionMode : current.permissionMode,
+ }));
})();
return () => {
cancelled = true;
};
}, [
draft.permissionMode,
- deviceProviders.loading,
+ deviceProviders.ready,
+ deviceProviders.unsupported,
deviceProviders.providers,
deviceProviders.modelVisibilityOverrides,
newSessionPreferences,
diff --git a/apps/mobile/src/__tests__/newSession.test.ts b/apps/mobile/src/__tests__/newSession.test.ts
index 8a98c53577..e4306a9040 100644
--- a/apps/mobile/src/__tests__/newSession.test.ts
+++ b/apps/mobile/src/__tests__/newSession.test.ts
@@ -8,6 +8,10 @@ import {
DEFAULT_NEW_SESSION_DRAFT,
NEW_SESSION_AGENT_OPTIONS,
availableNewSessionAgentOptions,
+ canResolveStoredAgentRuntime,
+ isStoredAgentRestorePending,
+ nextStoredAgentRestoreStep,
+ type StoredAgentRestoreState,
buildNewSessionCreatePreview,
buildRecentWorkspaceOptions,
buildRemoteCreateSessionOptions,
@@ -324,6 +328,128 @@ describe('resolveSubmitGuardCatalog —— 提交终检目录取信(代际安全
});
});
+describe('stored agent restore gating', () => {
+ const recentOpus = remoteSession('recent', {
+ deviceLinkDeviceId: 'mac',
+ model: 'claude-opus-5-5',
+ effort: 'high',
+ });
+
+ it('waits while neither the catalog nor a recent task for the agent is available', () => {
+ // 冷启动:目录未就绪、任务列表还没到 —— 此刻恢复只能落到内置 Sonnet 4.6。
+ expect(canResolveStoredAgentRuntime({
+ agentKind: 'claude-code',
+ sessions: [],
+ deviceId: 'mac',
+ catalogReady: false,
+ providersUnsupported: false,
+ })).toBe(false);
+ expect(pickAgentDefaultRuntime({
+ agentKind: 'claude-code',
+ sessions: [],
+ modelRows: [],
+ currentEffort: 'medium',
+ deviceId: 'mac',
+ catalogReady: false,
+ })).toMatchObject({ model: 'claude-sonnet-4-6', effort: 'medium' });
+ });
+
+ it('proceeds once a recent task of the stored agent exists on the device', () => {
+ expect(canResolveStoredAgentRuntime({
+ agentKind: 'claude-code',
+ sessions: [recentOpus],
+ deviceId: 'mac',
+ catalogReady: false,
+ providersUnsupported: false,
+ })).toBe(true);
+ // 其他设备或其他 agent 的任务不算。
+ expect(canResolveStoredAgentRuntime({
+ agentKind: 'codex',
+ sessions: [recentOpus],
+ deviceId: 'mac',
+ catalogReady: false,
+ providersUnsupported: false,
+ })).toBe(false);
+ expect(canResolveStoredAgentRuntime({
+ agentKind: 'claude-code',
+ sessions: [recentOpus],
+ deviceId: 'studio',
+ catalogReady: false,
+ providersUnsupported: false,
+ })).toBe(false);
+ });
+
+ it('proceeds once the catalog is ready or explicitly unsupported, or without a device', () => {
+ const base = { agentKind: 'claude-code' as const, sessions: [], deviceId: 'mac' };
+ expect(canResolveStoredAgentRuntime({ ...base, catalogReady: true, providersUnsupported: false })).toBe(true);
+ expect(canResolveStoredAgentRuntime({ ...base, catalogReady: false, providersUnsupported: true })).toBe(true);
+ expect(canResolveStoredAgentRuntime({ ...base, deviceId: '', catalogReady: false, providersUnsupported: false })).toBe(true);
+ });
+
+ it('reports a pending restore only for an unapplied stored agent on the target device', () => {
+ const base = { expectedDeviceId: 'mac', selectedDeviceId: 'mac' };
+ expect(isStoredAgentRestorePending({ ...base, storedAgentKind: 'claude-code', appliedStoredAgentKind: null })).toBe(true);
+ expect(isStoredAgentRestorePending({ ...base, storedAgentKind: 'claude-code', appliedStoredAgentKind: 'claude-code' })).toBe(false);
+ expect(isStoredAgentRestorePending({ ...base, storedAgentKind: null, appliedStoredAgentKind: null })).toBe(false);
+ // 显式路由到别的设备时不恢复,自动默认照常工作。
+ expect(isStoredAgentRestorePending({
+ storedAgentKind: 'claude-code',
+ appliedStoredAgentKind: null,
+ expectedDeviceId: 'mac',
+ selectedDeviceId: 'studio',
+ })).toBe(false);
+ expect(isStoredAgentRestorePending({
+ storedAgentKind: 'codex',
+ appliedStoredAgentKind: null,
+ expectedDeviceId: '',
+ selectedDeviceId: 'studio',
+ })).toBe(true);
+ });
+
+ it('restores the remembered agent immediately and fills its model exactly once when data arrives (codex P1)', () => {
+ // 事件序列:偏好到 → 数据未到 → 数据到 → 之后再有数据变化。
+ let restored: StoredAgentRestoreState | null = null;
+ const apply = (modelReady: boolean) => {
+ const step = nextStoredAgentRestoreStep({ storedAgentKind: 'codex', restored, modelReady });
+ if (step) restored = { agentKind: 'codex', phase: step === 'agent' ? 'agent' : 'done' };
+ return step;
+ };
+ // 目录拉不到、也没有 Codex 最近任务:仍先恢复 agent(旧补丁在这里返回不恢复,草稿停在 Claude)。
+ expect(apply(false)).toBe('agent');
+ expect(apply(false)).toBeNull();
+ expect(apply(true)).toBe('model');
+ expect(apply(true)).toBeNull();
+ expect(apply(false)).toBeNull();
+ });
+
+ it('restores agent and model together when data is already there, and restarts for a different remembered agent', () => {
+ expect(nextStoredAgentRestoreStep({ storedAgentKind: 'pi', restored: null, modelReady: true })).toBe('full');
+ expect(nextStoredAgentRestoreStep({
+ storedAgentKind: 'pi',
+ restored: { agentKind: 'codex', phase: 'done' },
+ modelReady: false,
+ })).toBe('agent');
+ });
+
+ it('gates the stored agent restore on real data and leaves the auto-default unblocked', () => {
+ const source = readTextLf(resolve(process.cwd(), 'app/sessions/new.tsx'), 'utf8');
+ const storedStart = source.indexOf('const storedAgentKind = newSessionPreferences?.agentKind;');
+ const autoStart = source.indexOf('const result = resolveNewSessionAutoDefault({');
+ const storedSource = source.slice(storedStart, source.indexOf('storedAgentRestoreRef.current = {', storedStart));
+ const autoGuardSource = source.slice(source.lastIndexOf('useEffect(() => {', autoStart), autoStart);
+ expect(storedSource).toContain('isStoredAgentRestorePending({');
+ expect(storedSource).toContain('canResolveStoredAgentRuntime({');
+ expect(storedSource).toContain('nextStoredAgentRestoreStep({');
+ // model 步只补模型,不写 agent / 权限。
+ const modelStep = source.slice(source.indexOf("if (step === 'model') {"), source.indexOf('const storedPermissionMode', storedStart));
+ expect(modelStep).not.toContain('permissionMode');
+ expect(modelStep).toContain('current.agentKind === storedAgentKind');
+ expect(storedSource).not.toContain('deviceProviders.loading');
+ // 恢复待落定时不阻断「跟随最近任务」(Greptile P1)。
+ expect(autoGuardSource).not.toContain('isStoredAgentRestorePending(');
+ });
+});
+
describe('pickAgentDefaultRuntime', () => {
it('follows the target agent\'s most recent session model + effort (reconciled)', () => {
const runtime = pickAgentDefaultRuntime({
diff --git a/apps/mobile/src/session/newSession.ts b/apps/mobile/src/session/newSession.ts
index 9c18be684b..d9af748b63 100644
--- a/apps/mobile/src/session/newSession.ts
+++ b/apps/mobile/src/session/newSession.ts
@@ -761,6 +761,69 @@ export function pickAgentDefaultRuntime(args: {
return { agentKind, model, effort, providerId };
}
+/**
+ * 「恢复上次选过的 agent」是否仍待落定:有记忆的 agent、模型尚未落定,且当前设备就是恢复
+ * 目标(没有偏好设备 = 不限设备)。
+ */
+export function isStoredAgentRestorePending(input: {
+ storedAgentKind: NewSessionAgentKind | null | undefined;
+ appliedStoredAgentKind: NewSessionAgentKind | null;
+ expectedDeviceId: string;
+ selectedDeviceId: string;
+}): boolean {
+ if (!input.storedAgentKind) return false;
+ if (input.appliedStoredAgentKind === input.storedAgentKind) return false;
+ return !input.expectedDeviceId || input.selectedDeviceId === input.expectedDeviceId;
+}
+
+/** 上次 agent 的恢复进度:agent = 已恢复 agent 与权限、模型仍是占位;done = 模型也已落定。 */
+export interface StoredAgentRestoreState {
+ agentKind: NewSessionAgentKind;
+ phase: 'agent' | 'done';
+}
+
+/**
+ * 恢复上次 agent 的下一步。不变量:agent 不依赖任何数据,偏好一到就恢复;只有模型要等
+ * 目录或该 agent 的最近任务(canResolveStoredAgentRuntime),且只补一次。
+ * - 'full':数据已够,agent + 权限 + 模型一次恢复;
+ * - 'agent':数据未到,先恢复 agent + 权限,模型用占位——否则目录一直拉不到时草稿停在
+ * 别的 agent,创建会建错 agent 并把它存成新偏好;
+ * - 'model':agent 已恢复、数据刚到,只补 model / effort / providerId,不动 agent 与权限;
+ * - null:已落定,或 agent 已恢复但数据仍未到。
+ */
+export function nextStoredAgentRestoreStep(input: {
+ storedAgentKind: NewSessionAgentKind;
+ restored: StoredAgentRestoreState | null;
+ modelReady: boolean;
+}): 'full' | 'agent' | 'model' | null {
+ const restored = input.restored?.agentKind === input.storedAgentKind ? input.restored : null;
+ if (restored?.phase === 'done') return null;
+ if (restored?.phase === 'agent') return input.modelReady ? 'model' : null;
+ return input.modelReady ? 'full' : 'agent';
+}
+
+/**
+ * 恢复上次的 agent 时,是否已有足够数据选模型(pickAgentDefaultRuntime 的输入):
+ * 供应商目录已就绪 / 被控端明确不支持目录,或该设备上已有这个 agent 的最近任务。
+ * 两者都没有时 pickAgentDefaultRuntime 只能落到内置兜底模型(Claude 为 Sonnet 4.6),
+ * 这时只先恢复 agent(见 nextStoredAgentRestoreStep),模型等数据到了再补。
+ * 不能用 `loading === false` 代替:useDeviceProviders 的 loading 初值就是 false,拉取失败后也是。
+ */
+export function canResolveStoredAgentRuntime(input: {
+ agentKind: NewSessionAgentKind;
+ sessions: readonly RemoteSession[];
+ deviceId: string;
+ catalogReady: boolean;
+ providersUnsupported: boolean;
+}): boolean {
+ if (!input.deviceId) return true;
+ if (input.catalogReady || input.providersUnsupported) return true;
+ return pickMostRecentSessionRuntime(input.sessions, {
+ deviceId: input.deviceId,
+ agentKind: input.agentKind,
+ }) !== null;
+}
+
/**
* 新建对话「自动默认运行配置」effect 的决策核心(纯函数,从 new.tsx 那个 effect 内联逻辑抽出,便于单测)。
* 返回 null = 本次不动 draft(已手动选过 / 无 selectedDevice / 该设备已应用过 / modelRows 未就绪且无 recent);