Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): validate pending agent selection before sending
Chris55f50272ee4a
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsxindex 2c8d6d3b97..1244ae289a 100644--- a/apps/mobile/app/sessions/[sessionId].tsx+++ b/apps/mobile/app/sessions/[sessionId].tsx@@ -6042,10 +6042,15 @@ export default function SessionScreen() {? null: parsedDesktopCommandAtSend;// A known hidden model requires an explicit choice before consuming the draft or enqueueing.- const selectionAtSend = readSessionRowNow() ?? currentSession;+ const sessionAtSend = readSessionRowNow() ?? currentSession;+ const intentAtSend = sessionAtSend.agentSwitchIntent;+ // Cross-agent picks take effect on the next message; validate that chosen target.+ const selectionAtSend = intentAtSend+ ? { agentKind: intentAtSend.targetAgentKind, model: intentAtSend.model, providerId: intentAtSend.providerId }+ : { agentKind: resolveSessionAgentKind(sessionAtSend), model: sessionAtSend.model, providerId: sessionAtSend.providerId };if (!earlyLocalCommand && !earlyDesktopCommand && modelNeedsReselection(getCachedDeviceProviders(deviceId)?.modelVisibilityOverrides,- resolveSessionAgentKind(selectionAtSend), selectionAtSend.model, selectionAtSend.providerId,+ selectionAtSend.agentKind, selectionAtSend.model, selectionAtSend.providerId,)) {setError(t('session.common.modelHiddenReselect', { model: selectionAtSend.model }));setModelSheetOpen(true);diff --git a/apps/mobile/src/__tests__/sessionModelReselection.test.ts b/apps/mobile/src/__tests__/sessionModelReselection.test.tsnew file mode 100644index 0000000000..442653c1f9--- /dev/null+++ b/apps/mobile/src/__tests__/sessionModelReselection.test.ts@@ -0,0 +1,62 @@+import { readFileSync } from 'node:fs';+import { resolve } from 'node:path';+import ts from 'typescript';+import { describe, expect, it } from 'vitest';+import { modelNeedsReselection } from '@/session/modelReselection';++// Execute the screen's actual send selection and condition without mounting unrelated native UI.+const source = ts.createSourceFile('session.tsx', readFileSync(+ resolve(process.cwd(), 'app/sessions/[sessionId].tsx'), 'utf8',+), ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);+const declarations = new Map<string, string>();+let condition = '';+function visit(node: ts.Node) {+ if (ts.isIfStatement(node) && node.expression.getText(source).includes('modelNeedsReselection(')) {+ condition = node.expression.getText(source);+ if (!ts.isBlock(node.parent)) throw new Error('Expected send guard block');+ for (const statement of node.parent.statements) {+ if (!ts.isVariableStatement(statement)) continue;+ for (const declaration of statement.declarationList.declarations) {+ if (declaration.initializer+ && ['sessionAtSend', 'intentAtSend', 'selectionAtSend'].includes(declaration.name.getText(source))) {+ declarations.set(declaration.name.getText(source), declaration.initializer.getText(source));+ }+ }+ }+ }+ ts.forEachChild(node, visit);+}+visit(source);+if (declarations.size !== 3 || !condition) throw new Error('Missing send selection guard');+const runGuard = new Function('modelNeedsReselection', 'bindings', `+ const { readSessionRowNow, currentSession, resolveSessionAgentKind, getCachedDeviceProviders,+ deviceId, earlyLocalCommand, earlyDesktopCommand } = bindings;+ ${[...declarations].map(([name, value]) => `const ${name} = ${value};`).join('\n')}+ return { blocked: ${condition}, model: selectionAtSend.model };+`);++describe('existing task hidden-model send guard', () => {+ const currentSession = { agentKind: 'codex', model: 'old', providerId: 'old-source' };+ const target = { targetAgentKind: 'pi', model: 'next', providerId: 'new-source' };+ const visibility = { 'codex:old-source:old': false, 'pi:new-source:next': true };+ function check(intent: typeof target | null, overrides = visibility, localCommand = false) {+ return runGuard(modelNeedsReselection, {+ // Store is newer than the rendered row: intent is written while the current agent stays unchanged.+ readSessionRowNow: () => ({ ...currentSession, agentSwitchIntent: intent }), currentSession,+ resolveSessionAgentKind: (row: typeof currentSession) => row.agentKind,+ getCachedDeviceProviders: () => ({ modelVisibilityOverrides: overrides }), deviceId: 'device',+ earlyLocalCommand: localCommand, earlyDesktopCommand: false,+ });+ }+ it('allows an enabled cross-agent target despite the old hidden model', () => {+ expect(check(target)).toEqual({ blocked: false, model: 'next' });+ });+ it('rejects a hidden target and explains that target even when the old model is enabled', () => {+ expect(check(target, { 'codex:old-source:old': true, 'pi:new-source:next': false }))+ .toEqual({ blocked: true, model: 'next' });+ });+ it('keeps the original guard without an intent and preserves the local-command exception', () => {+ expect(check(null)).toEqual({ blocked: true, model: 'old' });+ expect(check(null, visibility, true).blocked).toBe(false);+ });+});