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): 核对请求绕缓存 + 阻断目标缺 version 时不放行

guyong8a0b5f11afc0
diff --git a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
index 525928260b..27c148ea31 100644
--- a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
+++ b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
@@ -67,6 +67,11 @@ describe('强更阻断闸门', () => {
expect(recheck).toContain("return 'error'");
// 仍强更时必须刷新 target:服务端可能只修正了坏掉的安装地址。
expect(recheck).toContain('deps.onStillForced(evaluation.target, startRevision)');
+ // 放行决定不能读缓存:/latest 是可变指针,minVersion 会被原地改(set-mobile-min-version),
+ // 同 version 的边缘旧副本证明不了新鲜度,所以核对这条请求必须绕缓存。
+ const recheckHook = read('src/update/useForcedUpdateRecheck.ts');
+ expect(recheckHook).toContain('fetchLatestRelease(');
+ expect(recheckHook).toMatch(/isCanary,\n\s*(\/\/[^\n]*\n\s*)*true,/);
// 在途期间若有更新的观察写入 store,本次旧结论必须作废(compare-and-set)。
expect(recheck).toContain('const startRevision = deps.getRevision?.();');
expect(recheck).toContain('deps.onCleared(startRevision)');
diff --git a/apps/mobile/src/update/fetchLatestRelease.test.ts b/apps/mobile/src/update/fetchLatestRelease.test.ts
index 00bfdf4463..d25639027f 100644
--- a/apps/mobile/src/update/fetchLatestRelease.test.ts
+++ b/apps/mobile/src/update/fetchLatestRelease.test.ts
@@ -37,6 +37,24 @@ describe('fetchLatestRelease —— 区分"无更新"与"连不上"', () => {
);
});
+ it('noCache=true → 追加 cache-buster + no-cache 头(放行决定不能读边缘旧记录)', async () => {
+ const fetchMock = vi.fn(async () => resp(200, { runtimeVersion: 'rtv1' }));
+ vi.stubGlobal('fetch', fetchMock);
+ await fetchLatestRelease('ios', 8000, BASE, false, true);
+ const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
+ expect(url).toMatch(new RegExp(`^${BASE}/latest\\?platform=ios&t=\\d+$`));
+ expect(init.headers).toMatchObject({ 'cache-control': 'no-cache' });
+ });
+
+ it('默认不绕缓存:常规路径的 URL 与请求头保持旧契约', async () => {
+ const fetchMock = vi.fn(async () => resp(200, { runtimeVersion: 'rtv1' }));
+ vi.stubGlobal('fetch', fetchMock);
+ await fetchLatestRelease('ios', 8000, BASE);
+ const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
+ expect(url).toBe(`${BASE}/latest?platform=ios`);
+ expect(init.headers).toEqual({ accept: 'application/json' });
+ });
+
it('404(服务端确认暂无记录)→ null(= 无更新)', async () => {
vi.stubGlobal('fetch', vi.fn(async () => resp(404)));
await expect(fetchLatestRelease('ios', 8000, BASE)).resolves.toBeNull();
diff --git a/apps/mobile/src/update/fetchLatestRelease.ts b/apps/mobile/src/update/fetchLatestRelease.ts
index 4e4f7f3431..a70a35cb52 100644
--- a/apps/mobile/src/update/fetchLatestRelease.ts
+++ b/apps/mobile/src/update/fetchLatestRelease.ts
@@ -11,21 +11,29 @@ const DEFAULT_TIMEOUT_MS = 8000;
* 否则手动检查会误报"已是最新")。
* baseUrl 可注入,便于单测。
* @param platform 目标平台(默认 ios)
+ * @param noCache 绕开缓存(cache-buster + no-cache 头)。`/latest` 背后是**可变指针**,
+ * 边缘缓存可能返回旧记录 —— 判定"是否还需要强更"这类**放行**决定不能建立在旧记录上
+ * (发布链侧读同一指针的 fetchJsonPointer 一直是这么做的)。默认 false:启动 / resume /
+ * 设置页三条常规路径的缓存行为保持不变,它们读到旧记录最多是少提示一次更新。
*/
export async function fetchLatestRelease(
platform = 'ios',
timeoutMs = DEFAULT_TIMEOUT_MS,
baseUrl = OTA_SERVER_BASE_URL,
isCanary = false,
+ noCache = false,
): Promise<unknown | null> {
if (!baseUrl) return null; // 非自建变体,无自托管服务
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const channelQuery = isCanary ? '&channel=canary' : '';
- const res = await fetch(`${baseUrl}/latest?platform=${encodeURIComponent(platform)}${channelQuery}`, {
+ const cacheQuery = noCache ? `&t=${Date.now()}` : '';
+ const res = await fetch(`${baseUrl}/latest?platform=${encodeURIComponent(platform)}${channelQuery}${cacheQuery}`, {
signal: controller.signal,
- headers: { accept: 'application/json' },
+ headers: noCache
+ ? { accept: 'application/json', 'cache-control': 'no-cache' }
+ : { accept: 'application/json' },
});
if (res.status === 404) return null; // 服务端确认暂无记录 = 无更新
if (!res.ok) throw new Error(i18n.t('update.latestRequestFailed', { status: res.status })); // 5xx 等服务异常,不能当成"无更新"
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.test.ts b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
index 786de1c284..47c45be4b2 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.test.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
@@ -183,6 +183,17 @@ describe('createForcedUpdateRechecker 解除判定', () => {
expect(deps.onStillForced).not.toHaveBeenCalled();
});
+ it('阻断目标本身缺 version → 证明不了新鲜度,维持阻断', async () => {
+ // 触发阻断的那条记录可能没写 version(parseLatestRelease 容许空串),此时任何
+ // 带正常 version 的旧记录都会"看起来更新",不能据此放行。
+ const deps = makeDeps({
+ fetchLatest: vi.fn(async () => latestRecord({ version: '1.5.0', minVersion: undefined })),
+ getHeldTarget: () => ({ version: '' }),
+ });
+ await expect(runOnce(deps)).resolves.toBe('error');
+ expect(deps.onCleared).not.toHaveBeenCalled();
+ });
+
it('记录缺 version → 证明不了新鲜度,维持阻断', async () => {
const deps = makeDeps({
fetchLatest: vi.fn(async () => latestRecord({ version: '', minVersion: undefined })),
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.ts b/apps/mobile/src/update/forcedUpdateRecheck.ts
index b951b552ff..8860345f94 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.ts
@@ -121,8 +121,10 @@ export function createForcedUpdateRechecker(
// 新鲜度门:读到的记录不得比正在阻断的目标更旧。可变指针 + 无 cache-buster 的请求
// 会撞上 CDN 边缘的旧记录,那条记录没有 minVersion → 会把仍需强更的用户放出去。
// 记录缺 version(parseLatestRelease 容许空串)同样证明不了新鲜度,一并挡掉。
+ // held.version 为空(触发阻断的那条记录本身缺 version)时同样比不出新鲜度,一并挡掉。
const held = deps.getHeldTarget?.();
- if (held && (!record.version || compareVersions(record.version, held.version) < 0)) {
+ if (held && (!record.version || !held.version
+ || compareVersions(record.version, held.version) < 0)) {
return 'error';
}
const evaluation = evaluateBundleUpdate({
diff --git a/apps/mobile/src/update/useForcedUpdateRecheck.ts b/apps/mobile/src/update/useForcedUpdateRecheck.ts
index 183ffc87f3..12e9c7fd07 100644
--- a/apps/mobile/src/update/useForcedUpdateRecheck.ts
+++ b/apps/mobile/src/update/useForcedUpdateRecheck.ts
@@ -27,6 +27,9 @@ export function useForcedUpdateRecheck(isCanary = isCanaryChannel()): void {
undefined,
undefined,
isCanary,
+ // 绕开缓存:同一条记录被原地改过 minVersion 时,"同 version"证明不了新鲜度
+ // (set-mobile-min-version 就是原地改)。**放行**用户的决定不能读边缘旧记录。
+ true,
),
getCurrentRuntimeVersion: () => Updates.runtimeVersion,
getCurrentVersion: () => Constants.expoConfig?.version ?? null,