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): 完善听写停止和 Windows 模拟器环境

DavidShen875a1bf55c58
diff --git a/apps/mobile/app/sessions/new.tsx b/apps/mobile/app/sessions/new.tsx
index cae80a7e0e..1d6dd6b8a9 100644
--- a/apps/mobile/app/sessions/new.tsx
+++ b/apps/mobile/app/sessions/new.tsx
@@ -5888,7 +5888,9 @@ export default function NewRemoteSessionScreen() {
}}
onChangeText={setFirstMessageDraft}
onSelectionChange={(event) => {
- if (!voiceRecordingActiveRef.current && !voiceStopInFlightRef.current) {
+ // finishVoiceRecording marks recording inactive before awaiting
+ // ASR/refinement teardown, so native cursor edits remain user-owned.
+ if (!voiceRecordingActiveRef.current) {
const selection = event.nativeEvent.selection;
firstMessageSelectionRef.current = selection;
setFirstMessageSelection(selection);
diff --git a/apps/mobile/scripts/java-runtime-env.mjs b/apps/mobile/scripts/java-runtime-env.mjs
index dbddbd7d79..7f93c593f3 100644
--- a/apps/mobile/scripts/java-runtime-env.mjs
+++ b/apps/mobile/scripts/java-runtime-env.mjs
@@ -34,6 +34,8 @@ export function javaRuntimeDetail(env = resolveJavaRuntimeEnv()) {
function javaHomeCandidates(env) {
return [
env.JAVA_HOME,
+ env.ANDROID_STUDIO_JDK,
+ ...(process.platform === 'win32' ? windowsAndroidStudioJavaHomes(env) : []),
macJavaHome('17'),
homebrewOpenJdk17Home('/opt/homebrew/opt/openjdk@17'),
homebrewOpenJdk17Home('/usr/local/opt/openjdk@17'),
@@ -41,6 +43,15 @@ function javaHomeCandidates(env) {
].filter(uniqueTruthy);
}
+function windowsAndroidStudioJavaHomes(env) {
+ return [
+ env.ANDROID_STUDIO_HOME ? join(env.ANDROID_STUDIO_HOME, 'jbr') : null,
+ env.ProgramFiles ? join(env.ProgramFiles, 'Android', 'Android Studio', 'jbr') : null,
+ env['ProgramFiles(x86)'] ? join(env['ProgramFiles(x86)'], 'Android', 'Android Studio', 'jbr') : null,
+ env.LOCALAPPDATA ? join(env.LOCALAPPDATA, 'Programs', 'Android Studio', 'jbr') : null,
+ ];
+}
+
function macJavaHome(version) {
if (process.platform !== 'darwin') return null;
const result = spawnSync('/usr/libexec/java_home', ['-v', version], { encoding: 'utf8' });
diff --git a/apps/mobile/scripts/sim-metro.mjs b/apps/mobile/scripts/sim-metro.mjs
index 2d813a8a17..a2b16a9e27 100644
--- a/apps/mobile/scripts/sim-metro.mjs
+++ b/apps/mobile/scripts/sim-metro.mjs
@@ -103,7 +103,12 @@ export function probeMetroOwnership(port) {
if (!owner || !Number.isInteger(owner.pid) || !ownerProcessAlive) {
return { pid, cwd: null, source: null };
}
- return { pid, cwd: owner.worktreeRoot ?? null, source: owner.source ?? null };
+ return {
+ pid,
+ launcherPid: Number.isInteger(owner.launcherPid) ? owner.launcherPid : owner.pid,
+ cwd: owner.worktreeRoot ?? null,
+ source: owner.source ?? null,
+ };
}
// 进程工作目录(用 cwd 判 worktree,而非解析命令行 —— 命令行常是 `pnpm exec expo`、取不到
@@ -182,12 +187,29 @@ export function isDedicatedMetroProcessGroup(entries, expectedWorktree) {
export async function terminateMetro(pid, options = {}) {
const run = options.execFile ?? execFileSync;
const wait = options.wait ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
- const isAlive = options.isAlive ?? (() => Boolean(commandOfPid(pid)));
+ const platform = options.platform ?? process.platform;
+ const isAlive = options.isAlive ?? (() => (
+ platform === 'win32' ? processAlive(pid) : Boolean(commandOfPid(pid))
+ ));
const signal = options.signal ?? 'TERM';
const timeoutMs = options.timeoutMs ?? 5000;
const pollMs = options.pollMs ?? 100;
if (!Number.isFinite(pollMs) || pollMs <= 0) throw new Error('Metro 终止轮询间隔必须大于 0');
+ if (platform === 'win32') {
+ try {
+ run('taskkill', ['/PID', String(pid), '/T', '/F'], { stdio: 'ignore', windowsHide: true });
+ } catch {
+ if (!isAlive()) return true;
+ return false;
+ }
+ for (let waitedMs = 0; waitedMs < timeoutMs; waitedMs += pollMs) {
+ if (!isAlive()) return true;
+ await wait(pollMs);
+ }
+ return !isAlive();
+ }
+
const groupId = options.groupId !== undefined ? options.groupId : processGroupOfPid(pid);
const currentGroupId = options.currentGroupId !== undefined
? options.currentGroupId
diff --git a/apps/mobile/scripts/sim-rebuild.mjs b/apps/mobile/scripts/sim-rebuild.mjs
index c3df8bdde6..c459931ffd 100644
--- a/apps/mobile/scripts/sim-rebuild.mjs
+++ b/apps/mobile/scripts/sim-rebuild.mjs
@@ -70,7 +70,7 @@ import {
formatMobileLocalConfigStatus,
} from './lib/mobile-local-config.mjs';
import { podInstallBounded } from './sim-pod-install.mjs';
-import { ensureWindowsAndroidEmulator } from './lib/android-simulator.mjs';
+import { ensureWindowsAndroidEmulator, resolveAndroidSdkTools } from './lib/android-simulator.mjs';
import { resolveJavaRuntimeEnv } from './java-runtime-env.mjs';
import {
cwdOfPid,
@@ -196,11 +196,18 @@ function ensureMetroOwnershipBeforeLaunch(packageName) {
}
async function rebuildAndroidSimulator() {
+ const androidTools = process.platform === 'win32'
+ ? resolveAndroidSdkTools({ env: devProcessEnv, platform: process.platform })
+ : null;
const emulator = buildOnly
- ? { serial: null, adb: null }
+ ? { serial: null, adb: null, sdkRoot: androidTools?.sdkRoot }
: await ensureWindowsAndroidEmulator({ port: 8081 });
+ const sdkRoot = emulator.sdkRoot ?? androidTools?.sdkRoot;
const androidDir = join(mobileDir, 'android');
- const javaEnv = resolveJavaRuntimeEnv(devProcessEnv);
+ const javaEnv = resolveJavaRuntimeEnv({
+ ...devProcessEnv,
+ ...(sdkRoot ? { ANDROID_SDK_ROOT: sdkRoot, ANDROID_HOME: sdkRoot } : {}),
+ });
console.log('› Android expo prebuild (debug development client)');
runPnpm(['exec', 'expo', 'prebuild', '--platform', 'android', '--no-install']);
diff --git a/apps/mobile/scripts/sim-start.mjs b/apps/mobile/scripts/sim-start.mjs
index ae375fbb70..5ef81150b7 100644
--- a/apps/mobile/scripts/sim-start.mjs
+++ b/apps/mobile/scripts/sim-start.mjs
@@ -136,11 +136,14 @@ if (portArgs.port === DEFAULT_PORT) {
process.exit(1);
}
- if (!pid || !isMetroPid(pid)) {
+ const confirmedMetro = process.platform === 'win32'
+ ? Boolean(ownership?.cwd && ownership?.source)
+ : Boolean(pid && isMetroPid(pid));
+ if (!pid || !confirmedMetro) {
console.error(`✗ ${DEFAULT_PORT} 上的进程不是可确认的 Metro,拒绝接管。`);
process.exit(1);
}
- const stopped = await terminateMetro(pid, { worktreeRoot: listener.worktree });
+ const stopped = await terminateMetro(ownership?.launcherPid ?? pid, { worktreeRoot: listener.worktree });
if (!stopped) {
console.error(`✗ 无法在限定时间内停止旧 Metro(pid=${pid}),拒绝继续。`);
process.exit(1);
diff --git a/apps/mobile/src/__tests__/mobileSimSource.test.ts b/apps/mobile/src/__tests__/mobileSimSource.test.ts
index 85baa6e398..5aba2976fc 100644
--- a/apps/mobile/src/__tests__/mobileSimSource.test.ts
+++ b/apps/mobile/src/__tests__/mobileSimSource.test.ts
@@ -88,6 +88,7 @@ describe('mobile simulator Metro takeover', () => {
const run = vi.fn();
let alive = true;
const stopped = await terminateMetro(123, {
+ platform: 'darwin',
execFile: run,
groupId: '456',
currentGroupId: '789',
@@ -105,9 +106,30 @@ describe('mobile simulator Metro takeover', () => {
expect(run).toHaveBeenCalledWith('kill', ['-TERM', '-456']);
});
+ it('uses taskkill tree termination for a confirmed Windows launcher', async () => {
+ const run = vi.fn();
+ let alive = true;
+ const stopped = await terminateMetro(123, {
+ platform: 'win32',
+ execFile: run,
+ isAlive: () => alive,
+ wait: async () => { alive = false; },
+ timeoutMs: 100,
+ pollMs: 10,
+ });
+
+ expect(stopped).toBe(true);
+ expect(run).toHaveBeenCalledWith(
+ 'taskkill',
+ ['/PID', '123', '/T', '/F'],
+ { stdio: 'ignore', windowsHide: true },
+ );
+ });
+
it('falls back to the listener PID when the group is unavailable', async () => {
const run = vi.fn();
const stopped = await terminateMetro(123, {
+ platform: 'darwin',
execFile: run,
groupId: null,
currentGroupId: '789',
@@ -121,6 +143,7 @@ describe('mobile simulator Metro takeover', () => {
it('falls back to the listener PID when the current process group is unknown', async () => {
const run = vi.fn();
const stopped = await terminateMetro(123, {
+ platform: 'darwin',
execFile: run,
groupId: '456',
currentGroupId: null,
@@ -135,6 +158,7 @@ describe('mobile simulator Metro takeover', () => {
it('falls back to the listener PID for a process group with unrelated members', async () => {
const run = vi.fn();
const stopped = await terminateMetro(123, {
+ platform: 'darwin',
execFile: run,
groupId: '456',
currentGroupId: '789',
@@ -149,6 +173,7 @@ describe('mobile simulator Metro takeover', () => {
it('returns success when the listener exits before kill', async () => {
const run = vi.fn(() => { throw new Error('ESRCH'); });
const stopped = await terminateMetro(123, {
+ platform: 'darwin',
execFile: run,
groupId: null,
isAlive: () => false,
@@ -163,6 +188,7 @@ describe('mobile simulator Metro takeover', () => {
it('times out instead of claiming a process was stopped', async () => {
const stopped = await terminateMetro(123, {
+ platform: 'darwin',
execFile: vi.fn(),
groupId: null,
isAlive: () => true,
diff --git a/apps/mobile/src/__tests__/nativeE2eEnvironment.test.ts b/apps/mobile/src/__tests__/nativeE2eEnvironment.test.ts
index af224044e7..1ff3ffb85a 100644
--- a/apps/mobile/src/__tests__/nativeE2eEnvironment.test.ts
+++ b/apps/mobile/src/__tests__/nativeE2eEnvironment.test.ts
@@ -10,6 +10,8 @@ describe('native e2e environment', () => {
const runner = readFileSync(resolve(process.cwd(), 'scripts/maestro-e2e.mjs'), 'utf8');
expect(helper).toContain('const MIN_JAVA_MAJOR = 17');
+ expect(helper).toContain('env.ANDROID_STUDIO_JDK');
+ expect(helper).toContain("env.ProgramFiles, 'Android', 'Android Studio', 'jbr'");
expect(helper).toContain("brew', ['--prefix', 'openjdk@17']");
expect(helper).toContain("'/usr/libexec/java_home', ['-v', version]");
expect(helper).toContain('JAVA_HOME: javaHome');
diff --git a/apps/mobile/src/__tests__/newSession.test.ts b/apps/mobile/src/__tests__/newSession.test.ts
index 0ec53fd2f4..857d6ccfe4 100644
--- a/apps/mobile/src/__tests__/newSession.test.ts
+++ b/apps/mobile/src/__tests__/newSession.test.ts
@@ -1758,6 +1758,8 @@ describe('new session composer surface', () => {
expect(newSource).toContain('const voiceStartupInFlightRef = useRef(false);');
expect(newSource).toContain('const voicePermissionRequestInFlightRef = useRef(false);');
expect(newSource).toContain('const voiceStopInFlightRef = useRef(false);');
+ expect(newSource).toContain('if (!voiceRecordingActiveRef.current) {');
+ expect(newSource).not.toContain('if (!voiceRecordingActiveRef.current && !voiceStopInFlightRef.current) {');
expect(newSource).toContain('const voiceStartupSeqRef = useRef(0);');
expect(newSource).toContain('|| voiceStopInFlightRef.current');
expect(newSource).toContain('resolveMobileVoiceRecordingPermission({');
diff --git a/apps/mobile/src/__tests__/simRebuildScript.test.ts b/apps/mobile/src/__tests__/simRebuildScript.test.ts
index b5892e2d5b..aca55d6dc1 100644
--- a/apps/mobile/src/__tests__/simRebuildScript.test.ts
+++ b/apps/mobile/src/__tests__/simRebuildScript.test.ts
@@ -60,10 +60,12 @@ describe('sim-rebuild script invariants', () => {
});
it('uses the Android debug client on Windows without invoking xcrun', () => {
- expect(source).toContain("import { ensureWindowsAndroidEmulator } from './lib/android-simulator.mjs';");
+ expect(source).toContain("import { ensureWindowsAndroidEmulator, resolveAndroidSdkTools } from './lib/android-simulator.mjs';");
expect(source).toContain("probeMetroOwnership");
expect(source).toContain('const ownership = probeMetroOwnership(8081);');
expect(source).toContain("import { resolveJavaRuntimeEnv } from './java-runtime-env.mjs';");
+ expect(source).toContain('const androidTools = process.platform === \'win32\'');
+ expect(source).toContain('ANDROID_SDK_ROOT: sdkRoot, ANDROID_HOME: sdkRoot');
expect(source).toContain("if (process.platform === 'win32') {");
expect(source).toContain('await rebuildAndroidSimulator();');
expect(source).toContain("'--platform', 'android', '--no-install'");