Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): harden CindyDev server env bundling
DavidShen991db9e30678
diff --git a/apps/mobile/.env.example b/apps/mobile/.env.exampleindex c58d794077..e12fe16be7 100644--- a/apps/mobile/.env.example+++ b/apps/mobile/.env.example@@ -30,6 +30,9 @@ EXPO_PUBLIC_XDT_MOBILE_VOICE_LITELLM_BASE_URL=# EXPO_PUBLIC_APP_VARIANT 构建变体标识(beta/prod/自建)# EXPO_PUBLIC_BETA_DEV beta 开发模式门控# EXPO_PUBLIC_XDT_OTA_SELFHOST 是否自建变体(=1)+# EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL+# CindyDev 运行时切换用的 CN Release 清单基址;由构建 helper+# 从 config/endpoint.json 派生,不要在本地 .env 手填# EXPO_PUBLIC_DESKTOP_VERSION 自建线配对的桌面产品线版本号# EXPO_PUBLIC_XDT_GIT_BRANCH 构建注入的 git 分支(调试展示)# EXPO_PUBLIC_XDT_GIT_COMMIT 构建注入的 git commit(调试展示)diff --git a/apps/mobile/app.config.js b/apps/mobile/app.config.jsindex 1a09be2aeb..f07934b3a3 100644--- a/apps/mobile/app.config.js+++ b/apps/mobile/app.config.js@@ -221,6 +221,15 @@ module.exports = (context = {}) => {for (const [key, value] of Object.entries(mobileBundleEnv)) {if (!process.env[key]?.trim()) process.env[key] = value;}+ // 该值只允许由 CindyDev 构建从仓内 CN 清单派生。Dev 构建覆盖 runner+ // 残留值,CN / Global 构建主动清除,确保正式包不会误烘焙 Dev-only 配置。+ if (region === 'dev') {+ process.env.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL =+ mobileBundleEnv.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL;+ } else {+ delete process.env+ .EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL;+ }// xdtProductionEnv 是既有 Expo config / runtime fingerprint 的一部分。对端清单// 基址只需通过上面的 EXPO_PUBLIC_* 环境变量进入 Metro bundle;不要把它追加到// extra,否则本次纯 JS 登录路由会无意要求一次冷构建,并切断旧 runtime 的 OTA 链。diff --git a/apps/mobile/scripts/build-android.mjs b/apps/mobile/scripts/build-android.mjsindex b4d7163aee..2ad3bf4069 100644--- a/apps/mobile/scripts/build-android.mjs+++ b/apps/mobile/scripts/build-android.mjs@@ -79,7 +79,10 @@ import {readEmbeddedRuntimeVersionFromAab,readEmbeddedRuntimeVersionFromApk,} from './lib/embedded-runtime.mjs';-import { mobileClientBundleEnv } from '../../../scripts/shared/client-endpoint-build-env.mjs';+import {+ mobileClientBundleEnv,+ mobileClientBundleProcessEnv,+} from '../../../scripts/shared/client-endpoint-build-env.mjs';import { SELF_HOST_REGIONS, loadSelfHostRegions, missingSelfHostBakeFields, stripSelfHostRegionEnv } from './lib/self-host-region.mjs';const MOBILE_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..');@@ -90,8 +93,7 @@ function log(msg) { console.error(msg); }// self-host 变体的构建环境(与原发布线同源),注入 versionCode。function selfhostEnv(region, versionCode, desktopVersion) {const env = {- ...process.env,- ...mobileClientBundleEnv({ authRegion: region.authRegion }),+ ...mobileClientBundleProcessEnv({ authRegion: region.authRegion }),EXPO_PUBLIC_XDT_OTA_SELFHOST: '1',XDT_ANDROID_VERSION_CODE: String(versionCode),};diff --git a/apps/mobile/scripts/build-ios.mjs b/apps/mobile/scripts/build-ios.mjsindex 9e6cc2db06..532d64f36a 100644--- a/apps/mobile/scripts/build-ios.mjs+++ b/apps/mobile/scripts/build-ios.mjs@@ -62,7 +62,10 @@ import {import { buildExportOptionsPlist, resolveIosSigningEnv } from './lib/ios-local.mjs';import { clearBundlerCache } from './lib/bundler-cache.mjs';import { readEmbeddedRuntimeVersionFromIpa } from './lib/embedded-runtime.mjs';-import { mobileClientBundleEnv } from '../../../scripts/shared/client-endpoint-build-env.mjs';+import {+ mobileClientBundleEnv,+ mobileClientBundleProcessEnv,+} from '../../../scripts/shared/client-endpoint-build-env.mjs';import { SELF_HOST_REGIONS, loadSelfHostRegions, missingSelfHostBakeFields, stripSelfHostRegionEnv } from './lib/self-host-region.mjs';const MOBILE_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '..');@@ -73,8 +76,7 @@ function log(msg) { console.error(msg); }// self-host 变体的构建环境(与原发布线同源:prebuild/fingerprint 与安装包一致)。function selfhostEnv(region, desktopVersion) {const env = {- ...process.env,- ...mobileClientBundleEnv({ authRegion: region.authRegion }),+ ...mobileClientBundleProcessEnv({ authRegion: region.authRegion }),EXPO_PUBLIC_XDT_OTA_SELFHOST: '1',};// 防止本机 shell / 旧 .env 残留变量混入构建;真实地址只认 config/endpoint*.json。diff --git a/apps/mobile/src/__tests__/nativeAppConfig.test.ts b/apps/mobile/src/__tests__/nativeAppConfig.test.tsindex f9ea53bfaf..59bf9c77c2 100644--- a/apps/mobile/src/__tests__/nativeAppConfig.test.ts+++ b/apps/mobile/src/__tests__/nativeAppConfig.test.ts@@ -120,6 +120,8 @@ describe('mobile native app config', () => {);process.env.EXPO_PUBLIC_CINDY_AUTH_REGION = 'cn';+ process.env.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL =+ 'https://stale-release.example.invalid/app';const cn = buildConfig({ config: appJson.expo });expect(process.env.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL,@@ -129,6 +131,8 @@ describe('mobile native app config', () => {);process.env.EXPO_PUBLIC_CINDY_AUTH_REGION = 'global';+ process.env.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL =+ 'https://stale-release.example.invalid/app';const global = buildConfig({ config: appJson.expo });expect(process.env.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL,diff --git a/scripts/__tests__/client-endpoint-build-env.test.mjs b/scripts/__tests__/client-endpoint-build-env.test.mjsindex 29d78e8db8..491cbbf6a0 100644--- a/scripts/__tests__/client-endpoint-build-env.test.mjs+++ b/scripts/__tests__/client-endpoint-build-env.test.mjs@@ -9,6 +9,7 @@ import {loadEndpointManifestBaseUrl,loadPeerEndpointManifestBaseUrl,mobileClientBundleEnv,+ mobileClientBundleProcessEnv,mobileClientBuildEnv,} from '../shared/client-endpoint-build-env.mjs';import { resolveReleaseCdnBaseUrl } from '../shared/release-env.mjs';@@ -47,12 +48,52 @@ test('desktop/mobile 构建从 region 清单的 cdnBaseUrl 生成自举环境变EXPO_PUBLIC_ENDPOINT_MANIFEST_BASE_URL: 'https://hotfix-global.example.invalid/app',EXPO_PUBLIC_ENDPOINT_MANIFEST_PEER_BASE_URL: 'https://hotfix-cn.example.invalid/app',});+ assert.deepEqual(mobileClientBundleEnv({ authRegion: 'dev', repoRoot }), {+ EXPO_PUBLIC_CINDY_AUTH_REGION: 'dev',+ EXPO_PUBLIC_ENDPOINT_MANIFEST_BASE_URL: 'https://hotfix-dev.example.invalid/app',+ EXPO_PUBLIC_ENDPOINT_MANIFEST_PEER_BASE_URL: 'https://hotfix-global.example.invalid/app',+ EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL:+ 'https://hotfix-cn.example.invalid/app',+ });assert.equal(loadPeerEndpointManifestBaseUrl({ authRegion: 'cn', repoRoot }),'https://hotfix-global.example.invalid/app',);});+test('Mobile bundling 进程环境只在 CindyDev 保留 Release 清单基址', () => {+ const repoRoot = writeRepoFixtures();+ const staleBaseEnv = {+ KEEP_ME: '1',+ EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL:+ 'https://stale-release.example.invalid/app',+ };++ const cn = mobileClientBundleProcessEnv({+ authRegion: 'cn',+ baseEnv: staleBaseEnv,+ repoRoot,+ });+ assert.equal(cn.KEEP_ME, '1');+ assert.equal(+ Object.hasOwn(+ cn,+ 'EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL',+ ),+ false,+ );++ const dev = mobileClientBundleProcessEnv({+ authRegion: 'dev',+ baseEnv: staleBaseEnv,+ repoRoot,+ });+ assert.equal(+ dev.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL,+ 'https://hotfix-cn.example.invalid/app',+ );+});+test('端点清单自举基址缺失、非法协议或携带凭据时 fail closed', () => {const repoRoot = writeRepoFixtures();const cnPath = path.join(repoRoot, 'config', 'endpoint.json');@@ -93,5 +134,9 @@ function writeRepoFixtures() {path.join(configDir, 'endpoint.global.json'),JSON.stringify({ schemaVersion: 1, cdnBaseUrl: 'https://hotfix-global.example.invalid/app/' }),);+ fs.writeFileSync(+ path.join(configDir, 'endpoint.dev.json'),+ JSON.stringify({ schemaVersion: 1, cdnBaseUrl: 'https://hotfix-dev.example.invalid/app/' }),+ );return repoRoot;}diff --git a/scripts/shared/client-endpoint-build-env.mjs b/scripts/shared/client-endpoint-build-env.mjsindex 4b26278f50..99124c6a93 100644--- a/scripts/shared/client-endpoint-build-env.mjs+++ b/scripts/shared/client-endpoint-build-env.mjs@@ -121,16 +121,44 @@ export function mobileClientBuildEnv({ authRegion, repoRoot } = {}) {}/**- * Mobile JS bundle 额外需要对端区域清单基址。与 mobileClientBuildEnv 分开,- * 避免把这个纯 JS 变量加入 app.config 的既有 Expo extra / runtime fingerprint。+ * Mobile JS bundle 额外需要对端区域清单基址;CindyDev 还需要 CN Release+ * 清单基址供内部运行时切换。与 mobileClientBuildEnv 分开,避免把这些纯 JS+ * 变量加入 app.config 的既有 Expo extra / runtime fingerprint。*/export function mobileClientBundleEnv(options = {}) {const buildEnv = mobileClientBuildEnv(options);+ const authRegion = buildEnv.EXPO_PUBLIC_CINDY_AUTH_REGION;return {...buildEnv,EXPO_PUBLIC_ENDPOINT_MANIFEST_PEER_BASE_URL: loadPeerEndpointManifestBaseUrl({- authRegion: buildEnv.EXPO_PUBLIC_CINDY_AUTH_REGION,+ authRegion,repoRoot: options.repoRoot,}),+ ...(authRegion === 'dev'+ ? {+ EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL:+ loadEndpointManifestBaseUrl({+ authRegion: 'cn',+ repoRoot: options.repoRoot,+ }),+ }+ : {}),};}++/**+ * 合并最终传给 Mobile bundling 子进程的环境。正式构建必须主动删除 runner+ * 可能残留的 CindyDev Release 清单变量,不能只依赖 app.config.js 在子进程内清理。+ */+export function mobileClientBundleProcessEnv({+ authRegion,+ baseEnv = process.env,+ repoRoot,+} = {}) {+ const bundleEnv = mobileClientBundleEnv({ authRegion, repoRoot });+ const env = { ...baseEnv, ...bundleEnv };+ if (bundleEnv.EXPO_PUBLIC_CINDY_AUTH_REGION !== 'dev') {+ delete env.EXPO_PUBLIC_CINDY_DEV_RELEASE_ENDPOINT_MANIFEST_BASE_URL;+ }+ return env;+}