Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): track Metro environment fingerprint
DavidShene232909f4408
diff --git a/apps/mobile/scripts/lib/sim-whoami.mjs b/apps/mobile/scripts/lib/sim-whoami.mjsindex 3dbb9d10b3..827801725c 100644--- a/apps/mobile/scripts/lib/sim-whoami.mjs+++ b/apps/mobile/scripts/lib/sim-whoami.mjs@@ -141,6 +141,8 @@ export function resolveSimMetroHandoff({runningSource,currentRegion,runningRegion,+ currentEnvFingerprint,+ runningEnvFingerprint,listener,listenerWorktreeExists = false,} = {}) {@@ -161,7 +163,9 @@ export function resolveSimMetroHandoff({if (listener.isTarget) {if (runningSource === currentSource) {const regionChanged = currentRegion !== undefined && runningRegion !== currentRegion;- if ((envChanged || regionChanged) && !takeover) {+ const environmentChanged = currentEnvFingerprint !== undefined+ && runningEnvFingerprint !== currentEnvFingerprint;+ if ((envChanged || regionChanged || environmentChanged) && !takeover) {return {action: 'refuse',code: regionChanged ? 'target-region-stale' : 'target-env-stale',@@ -171,7 +175,7 @@ export function resolveSimMetroHandoff({],};}- if ((envChanged || regionChanged) && takeover) {+ if ((envChanged || regionChanged || environmentChanged) && takeover) {return { action: 'restart', code: regionChanged ? 'target-region' : 'target-env', lines: [] };}return {diff --git a/apps/mobile/scripts/sim-metro.mjs b/apps/mobile/scripts/sim-metro.mjsindex 07ea3426d1..3fa94b56f6 100644--- a/apps/mobile/scripts/sim-metro.mjs+++ b/apps/mobile/scripts/sim-metro.mjs@@ -51,6 +51,21 @@ export function listenerPid(port) {const metroOwnerDir = join(tmpdir(), 'cindy-metro-owners');+/**+ * Hash the public bundle environment and local config inputs without persisting+ * their values in the owner file. The owner only needs to answer whether the+ * running Metro was started with the same inputs as the next invocation.+ */+export function metroEnvironmentFingerprint({ env = {}, files = {} } = {}) {+ const ordered = (value) => Object.fromEntries(+ Object.entries(value).sort(([left], [right]) => left.localeCompare(right)),+ );+ return createHash('sha256')+ .update(JSON.stringify({ env: ordered(env), files: ordered(files) }))+ .digest('hex')+ .slice(0, 16);+}+function metroOwnerPath(port) {return join(metroOwnerDir, `port-${port}.json`);}@@ -143,7 +158,19 @@ export function probeMetroOwnership(port, options = {}) {const pid = (options.listenerPid ?? listenerPid)(port);if (!pid) return null;if ((options.platform ?? process.platform) !== 'win32') {- return { pid, cwd: cwdOfPid(pid), source: gitSourceOfPid(pid) };+ const cwd = cwdOfPid(pid);+ const source = gitSourceOfPid(pid);+ const owner = (options.readOwner ?? readMetroOwner)(port);+ const worktreeRoot = cwd ? path.resolve(cwd, '../..') : null;+ const ownerRoot = owner?.worktreeRoot ? path.resolve(owner.worktreeRoot) : null;+ return {+ pid,+ cwd,+ source,+ envFingerprint: ownerRoot && ownerRoot === worktreeRoot && owner?.source === source+ ? owner.envFingerprint ?? null+ : null,+ };}const owner = (options.readOwner ?? readMetroOwner)(port);if (!owner || !windowsOwnerOwnsListener(owner, pid,@@ -156,6 +183,7 @@ export function probeMetroOwnership(port, options = {}) {cwd: owner.worktreeRoot ? join(owner.worktreeRoot, 'apps/mobile') : null,source: owner.source ?? null,region: owner.region ?? null,+ envFingerprint: owner.envFingerprint ?? null,};}diff --git a/apps/mobile/scripts/sim-start.mjs b/apps/mobile/scripts/sim-start.mjsindex f9a04d8635..c2636ef6b9 100644--- a/apps/mobile/scripts/sim-start.mjs+++ b/apps/mobile/scripts/sim-start.mjs@@ -26,7 +26,7 @@// pnpm mobile:sim:start -- --no-emulator # Windows 只启动 Metroimport { execFileSync, spawn } from 'node:child_process';-import { existsSync } from 'node:fs';+import { existsSync, readFileSync } from 'node:fs';import { dirname, resolve } from 'node:path';import { fileURLToPath } from 'node:url';import { mobileClientBundleEnv } from '../../../scripts/shared/client-endpoint-build-env.mjs';@@ -57,6 +57,7 @@ import {clearMetroOwner,gitSourceIdentity,isMetroPid,+ metroEnvironmentFingerprint,portInUse,probeMetroOwnership,terminateMetro,@@ -84,6 +85,13 @@ const buildEnv = withLocalMobileRegionConfig(const envResult = ensureMobileEnv({ mobileDir, authRegion: region, endpointEnv: buildEnv });console.log(formatMobileEnvStatus(envResult, worktreeRoot));const envChanged = envResult.created || envResult.addedKeys.length > 0;+const envFingerprint = metroEnvironmentFingerprint({+ env: buildEnv,+ files: {+ '.env': readFileSync(envResult.envPath, 'utf8'),+ 'scripts/self-host-regions.json': readFileSync(localConfigResult.configPath, 'utf8'),+ },+});function git(args) {try {@@ -125,6 +133,8 @@ if (portArgs.port === DEFAULT_PORT) {runningSource,currentRegion: process.platform === 'win32' ? region : undefined,runningRegion: process.platform === 'win32' ? ownership?.region : undefined,+ currentEnvFingerprint: envFingerprint,+ runningEnvFingerprint: ownership?.envFingerprint,listener,listenerWorktreeExists,});@@ -195,6 +205,7 @@ if (portArgs.port === DEFAULT_PORT && Number.isInteger(child.pid)) {launcherPid: child.pid,source: sourceIdentity,region,+ envFingerprint,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.tsindex a2a64a6d3f..2d9c208fb2 100644--- a/apps/mobile/src/__tests__/mobileMetroOwnership.test.ts+++ b/apps/mobile/src/__tests__/mobileMetroOwnership.test.ts@@ -1,10 +1,11 @@import { join } from 'node:path';import { describe, expect, it, vi } from 'vitest';-import { probeMetroOwnership, windowsProcessSnapshot } from '../../scripts/sim-metro.mjs';+import { metroEnvironmentFingerprint, probeMetroOwnership, windowsProcessSnapshot } from '../../scripts/sim-metro.mjs';import { classifySimMetroListener, resolveSimMetroHandoff } from '../../scripts/lib/sim-whoami.mjs';const worktreeRoot = join('/worktrees', 'metro-owner');-const owner = { pid: 100, launcherPid: 100, worktreeRoot, source: 'branch@commit', recordedAtMs: 1500 };+const owner = { pid: 100, launcherPid: 100, worktreeRoot, source: 'branch@commit', recordedAtMs: 1500,+ envFingerprint: 'env-1' };type ProcessEntry = { pid: number; parentPid: number; startedAtMs: number };const launcher = { pid: 100, parentPid: 1, startedAtMs: 1000 };const wrapper = { pid: 200, parentPid: 100, startedAtMs: 1100 };@@ -22,7 +23,8 @@ function probe(processes: ProcessEntry[], metadata: object | null = owner, listedescribe('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, region: null,+ pid: '300', launcherPid: 100, cwd: join(worktreeRoot, 'apps/mobile'), source: owner.source,+ region: null, envFingerprint: owner.envFingerprint,});});@@ -85,6 +87,32 @@ describe('Windows Metro owner and listener association', () => {it('preserves the recorded Metro region for rebuild gates', () => {expect(probe([launcher, wrapper, listener], { ...owner, region: 'cn' })?.region).toBe('cn');});++ it('preserves the recorded environment fingerprint for reuse gates', () => {+ expect(probe([launcher, wrapper, listener], { ...owner, envFingerprint: 'env-2' })?.envFingerprint)+ .toBe('env-2');+ });+});++describe('Metro environment fingerprint', () => {+ it('is stable for reordered inputs and changes when local config changes', () => {+ const first = metroEnvironmentFingerprint({+ env: { REGION: 'cn', MANIFEST: 'https://one.invalid' },+ files: { '.env': 'REGION=cn\n', 'scripts/self-host-regions.json': '{"cn":1}' },+ });+ expect(metroEnvironmentFingerprint({+ env: { MANIFEST: 'https://one.invalid', REGION: 'cn' },+ files: { 'scripts/self-host-regions.json': '{"cn":1}', '.env': 'REGION=cn\n' },+ })).toBe(first);+ expect(metroEnvironmentFingerprint({+ env: { REGION: 'cn', MANIFEST: 'https://two.invalid' },+ files: { '.env': 'REGION=cn\n', 'scripts/self-host-regions.json': '{"cn":1}' },+ })).not.toBe(first);+ expect(metroEnvironmentFingerprint({+ env: { REGION: 'cn', MANIFEST: 'https://one.invalid' },+ files: { '.env': 'REGION=cn\n', 'scripts/self-host-regions.json': '{"cn":2}' },+ })).not.toBe(first);+ });});describe('Windows process snapshot', () => {diff --git a/apps/mobile/src/__tests__/mobileSimWhoami.test.ts b/apps/mobile/src/__tests__/mobileSimWhoami.test.tsindex e38770c200..e6a0ab3e2a 100644--- a/apps/mobile/src/__tests__/mobileSimWhoami.test.ts+++ b/apps/mobile/src/__tests__/mobileSimWhoami.test.ts@@ -118,6 +118,19 @@ describe('mobile:sim takeover and JSON arguments', () => {listener: { confirmed: true, isTarget: true }, listenerWorktreeExists: true,})).toMatchObject({ action: 'restart', code: 'target-region' });});++ it('restarts a target Metro when its persisted environment fingerprint differs', () => {+ expect(resolveSimMetroHandoff({+ currentSource: 'branch@commit', runningSource: 'branch@commit',+ currentEnvFingerprint: 'env-new', runningEnvFingerprint: 'env-old',+ listener: { confirmed: true, isTarget: true }, listenerWorktreeExists: true,+ })).toMatchObject({ action: 'refuse', code: 'target-env-stale' });+ expect(resolveSimMetroHandoff({+ takeover: true, currentSource: 'branch@commit', runningSource: 'branch@commit',+ currentEnvFingerprint: 'env-new', runningEnvFingerprint: 'env-old',+ listener: { confirmed: true, isTarget: true }, listenerWorktreeExists: true,+ })).toMatchObject({ action: 'restart', code: 'target-env' });+ });});describe('mobile:sim Metro handoff', () => {