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): 校验首次听写选区与 Metro 区域

DavidShenb00f836cce0b
diff --git a/apps/mobile/scripts/lib/sim-whoami.mjs b/apps/mobile/scripts/lib/sim-whoami.mjs
index 2e223a1b3c..3dbb9d10b3 100644
--- a/apps/mobile/scripts/lib/sim-whoami.mjs
+++ b/apps/mobile/scripts/lib/sim-whoami.mjs
@@ -139,6 +139,8 @@ export function resolveSimMetroHandoff({
envChanged = false,
currentSource,
runningSource,
+ currentRegion,
+ runningRegion,
listener,
listenerWorktreeExists = false,
} = {}) {
@@ -158,18 +160,19 @@ export function resolveSimMetroHandoff({
if (listener.isTarget) {
if (runningSource === currentSource) {
- if (envChanged && !takeover) {
+ const regionChanged = currentRegion !== undefined && runningRegion !== currentRegion;
+ if ((envChanged || regionChanged) && !takeover) {
return {
action: 'refuse',
- code: 'target-env-stale',
+ code: regionChanged ? 'target-region-stale' : 'target-env-stale',
lines: [
`✗ 已补/改 apps/mobile/.env,但 ${port} 上的 Metro 是用旧 env 启动的(env 在 bundle 时注入)。`,
' 需要刷新 env 时传 `--takeover` 重起,新 env 才会生效。',
],
};
}
- if (envChanged && takeover) {
- return { action: 'restart', code: 'target-env', lines: [] };
+ if ((envChanged || regionChanged) && takeover) {
+ return { action: 'restart', code: regionChanged ? 'target-region' : 'target-env', lines: [] };
}
return {
action: 'reuse',
diff --git a/apps/mobile/scripts/sim-metro.mjs b/apps/mobile/scripts/sim-metro.mjs
index 44566b2d5f..07ea3426d1 100644
--- a/apps/mobile/scripts/sim-metro.mjs
+++ b/apps/mobile/scripts/sim-metro.mjs
@@ -155,6 +155,7 @@ export function probeMetroOwnership(port, options = {}) {
launcherPid: Number.isInteger(owner.launcherPid) ? owner.launcherPid : owner.pid,
cwd: owner.worktreeRoot ? join(owner.worktreeRoot, 'apps/mobile') : null,
source: owner.source ?? null,
+ region: owner.region ?? null,
};
}
diff --git a/apps/mobile/scripts/sim-rebuild.mjs b/apps/mobile/scripts/sim-rebuild.mjs
index 166ba2a81f..7184e9d002 100644
--- a/apps/mobile/scripts/sim-rebuild.mjs
+++ b/apps/mobile/scripts/sim-rebuild.mjs
@@ -199,6 +199,12 @@ async function ensureMetroOwnershipBeforeLaunch(packageName) {
console.error('Restart Metro with pnpm mobile:sim:start before launching the app.');
return false;
}
+ if (ownership.region !== region) {
+ console.log(`\nNative package installed (${packageName}).`);
+ console.error(`Metro on 8081 uses region ${ownership.region || '(unknown)'}, but this build requested ${region}.`);
+ console.error('Restart Metro with the matching --region before launching the app.');
+ return false;
+ }
return true;
}
diff --git a/apps/mobile/scripts/sim-start.mjs b/apps/mobile/scripts/sim-start.mjs
index 5ef81150b7..f9a04d8635 100644
--- a/apps/mobile/scripts/sim-start.mjs
+++ b/apps/mobile/scripts/sim-start.mjs
@@ -123,6 +123,8 @@ if (portArgs.port === DEFAULT_PORT) {
envChanged,
currentSource: sourceIdentity,
runningSource,
+ currentRegion: process.platform === 'win32' ? region : undefined,
+ runningRegion: process.platform === 'win32' ? ownership?.region : undefined,
listener,
listenerWorktreeExists,
});
@@ -192,6 +194,7 @@ if (portArgs.port === DEFAULT_PORT && Number.isInteger(child.pid)) {
pid: child.pid,
launcherPid: child.pid,
source: sourceIdentity,
+ region,
worktreeRoot,
});
child.once('exit', () => clearMetroOwner(DEFAULT_PORT, child.pid));
diff --git a/apps/mobile/src/__tests__/mobileMetroOwnership.test.ts b/apps/mobile/src/__tests__/mobileMetroOwnership.test.ts
index 5dd993052a..a2a64a6d3f 100644
--- a/apps/mobile/src/__tests__/mobileMetroOwnership.test.ts
+++ b/apps/mobile/src/__tests__/mobileMetroOwnership.test.ts
@@ -22,7 +22,7 @@ function probe(processes: ProcessEntry[], metadata: object | null = owner, liste
describe('Windows Metro owner and listener association', () => {
it('recognizes a listener beneath the pnpm and shell wrappers', () => {
expect(probe([launcher, wrapper, listener])).toEqual({
- pid: '300', launcherPid: 100, cwd: join(worktreeRoot, 'apps/mobile'), source: owner.source,
+ pid: '300', launcherPid: 100, cwd: join(worktreeRoot, 'apps/mobile'), source: owner.source, region: null,
});
});
@@ -81,6 +81,10 @@ describe('Windows Metro owner and listener association', () => {
expect(readOwner).not.toHaveBeenCalled();
expect(processSnapshot).not.toHaveBeenCalled();
});
+
+ it('preserves the recorded Metro region for rebuild gates', () => {
+ expect(probe([launcher, wrapper, listener], { ...owner, region: 'cn' })?.region).toBe('cn');
+ });
});
describe('Windows process snapshot', () => {
diff --git a/apps/mobile/src/__tests__/mobileSimWhoami.test.ts b/apps/mobile/src/__tests__/mobileSimWhoami.test.ts
index ba21a1d2b2..e38770c200 100644
--- a/apps/mobile/src/__tests__/mobileSimWhoami.test.ts
+++ b/apps/mobile/src/__tests__/mobileSimWhoami.test.ts
@@ -105,6 +105,19 @@ describe('mobile:sim takeover and JSON arguments', () => {
platform: 'linux',
})).toEqual({ confirmed: true, worktree: '/Repo', isTarget: false });
});
+
+ it('restarts a target Metro when its persisted region differs', () => {
+ expect(resolveSimMetroHandoff({
+ currentSource: 'branch@commit', runningSource: 'branch@commit',
+ currentRegion: 'global', runningRegion: 'cn',
+ listener: { confirmed: true, isTarget: true }, listenerWorktreeExists: true,
+ })).toMatchObject({ action: 'refuse', code: 'target-region-stale' });
+ expect(resolveSimMetroHandoff({
+ takeover: true, currentSource: 'branch@commit', runningSource: 'branch@commit',
+ currentRegion: 'global', runningRegion: 'cn',
+ listener: { confirmed: true, isTarget: true }, listenerWorktreeExists: true,
+ })).toMatchObject({ action: 'restart', code: 'target-region' });
+ });
});
describe('mobile:sim Metro handoff', () => {
diff --git a/apps/mobile/src/__tests__/mobileVoiceController.test.ts b/apps/mobile/src/__tests__/mobileVoiceController.test.ts
index 8588bc0fef..717b59c2bd 100644
--- a/apps/mobile/src/__tests__/mobileVoiceController.test.ts
+++ b/apps/mobile/src/__tests__/mobileVoiceController.test.ts
@@ -228,6 +228,24 @@ describe('mobileVoiceController', () => {
expect(await session.stop()).toBe('用户刚修改\nraw final');
});
+ it('reports the original range when the first transcript is published after a user move', async () => {
+ let draft = '前后';
+ let firstReplacement: { start: number; end: number; text: string } | undefined;
+ const session = createMobileVoiceControllerSession({
+ credential: credential(), initialDraft: draft, initialSelection: { start: 1, end: 1 },
+ asr: new FakeAsrProvider(), refiner: null,
+ startAudio: async () => async () => undefined,
+ readCurrentDraft: () => draft,
+ onDraftChanged: (text, _selection, replacement) => {
+ draft = text;
+ firstReplacement ??= replacement;
+ },
+ });
+ await session.start();
+ expect(await session.stop()).toBe('前raw final后');
+ expect(firstReplacement).toEqual({ start: 1, end: 1, text: 'raw final' });
+ });
+
it('propagates and localizes empty-transcript failures from stop()', async () => {
const errors: string[] = [];
const states: string[] = [];
diff --git a/apps/mobile/src/__tests__/simRebuildScript.test.ts b/apps/mobile/src/__tests__/simRebuildScript.test.ts
index 11d8feabb4..da2b52c878 100644
--- a/apps/mobile/src/__tests__/simRebuildScript.test.ts
+++ b/apps/mobile/src/__tests__/simRebuildScript.test.ts
@@ -65,6 +65,8 @@ describe('sim-rebuild script invariants', () => {
expect(source).toContain('const ownership = probeMetroOwnership(8081);');
expect(source).toContain('if (!await portInUse(8081)) return true;');
expect(source).toContain('Metro on 8081 is occupied, but its listener PID could not be verified.');
+ expect(source).toContain('Metro on 8081 uses region');
+ expect(source).toContain('ownership.region');
expect(source).toContain("import { resolveJavaRuntimeEnv } from './java-runtime-env.mjs';");
expect(source).toContain('const androidTools = process.platform === \'win32\'');
expect(source).toContain('requireTools: !buildOnly');
diff --git a/apps/mobile/src/session/mobileVoiceController.ts b/apps/mobile/src/session/mobileVoiceController.ts
index b924b64311..78af90507e 100644
--- a/apps/mobile/src/session/mobileVoiceController.ts
+++ b/apps/mobile/src/session/mobileVoiceController.ts
@@ -200,6 +200,7 @@ export function createMobileVoiceControllerSession(
let asrStartError: unknown = null;
let audioFailureError: Error | null = null;
let voiceInsertion: MobileVoiceDraftInsertion | null = null;
+ let voiceInsertionReplacementRange: { start: number; end: number } | null = null;
let publishedVoiceInsertion: MobileVoiceDraftInsertion | null = null;
let voiceInsertionSegmentIds: string[] = [];
let voiceInsertionTouched = false;
@@ -232,8 +233,12 @@ export function createMobileVoiceControllerSession(
pendingDraftToPublish = null;
// The first insertion can change selected atoms even when its text is identical.
if (lastPublishedDraft === draft && (!voiceInsertion || publishedVoiceInsertion)) return;
- const replacement = publishedVoiceInsertion && voiceInsertion
- ? { ...publishedVoiceInsertion, text: voiceInsertion.text }
+ const replacement = voiceInsertion
+ ? publishedVoiceInsertion
+ ? { ...publishedVoiceInsertion, text: voiceInsertion.text }
+ : voiceInsertionReplacementRange
+ ? { ...voiceInsertionReplacementRange, text: voiceInsertion.text }
+ : undefined
: undefined;
lastPublishedDraft = draft;
publishedVoiceInsertion = voiceInsertion ? { ...voiceInsertion } : null;
@@ -344,6 +349,11 @@ export function createMobileVoiceControllerSession(
: appendVoiceTranscriptDraftWithRange(currentDraft, normalized);
if (!result.insertion) return undefined;
voiceInsertion = result.insertion;
+ voiceInsertionReplacementRange = options.initialSelection && currentDraft === baseDraft
+ ? 'atomRange' in options.initialSelection
+ ? null
+ : { start: options.initialSelection.start, end: options.initialSelection.end }
+ : { start: result.insertion.start, end: result.insertion.start };
voiceInsertionSegmentIds = segmentIds;
latestDraft = result.draft;
publishDraftChange(latestDraft, mode);