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(device-link): 词典快照 push 在控制链路关闭后仍可送达

Dash95044d19cc2d
diff --git a/apps/mobile/src/__tests__/mobileVoiceDictionaryView.test.ts b/apps/mobile/src/__tests__/mobileVoiceDictionaryView.test.ts
index 17594e752a..fc9cf1960a 100644
--- a/apps/mobile/src/__tests__/mobileVoiceDictionaryView.test.ts
+++ b/apps/mobile/src/__tests__/mobileVoiceDictionaryView.test.ts
@@ -192,6 +192,24 @@ describe('新鲜度判定', () => {
expect(views.map((view) => view.text)).toEqual(['bar']);
});
+ it('跨桌面并列时不比各自主机的 emittedAt', () => {
+ const views = buildMobileVoiceDictionaryEntryViews([
+ {
+ entries: [{ text: '旧且时钟快' }],
+ fetchedAt: 1_000,
+ stateVector: { a: '0000000100.0000.a' },
+ emittedAt: 9_999_999,
+ },
+ {
+ entries: [{ text: '新到达' }],
+ fetchedAt: 9_000,
+ stateVector: { b: '0000000101.0000.b' },
+ emittedAt: 1,
+ },
+ ]);
+ expect(views.map((view) => view.text)).toEqual(['新到达']);
+ });
+
it('最大 HLC 更大但并不包含对方时,不能因此被当成完整答案', () => {
// 这正是只比最大时间戳会犯的错:B 的 HLC 更大,但它没有 A 的词。
const views = buildMobileVoiceDictionaryEntryViews([
diff --git a/apps/mobile/src/session/mobileVoiceDictionaryCache.ts b/apps/mobile/src/session/mobileVoiceDictionaryCache.ts
index 25776f7128..359afd62e8 100644
--- a/apps/mobile/src/session/mobileVoiceDictionaryCache.ts
+++ b/apps/mobile/src/session/mobileVoiceDictionaryCache.ts
@@ -17,7 +17,7 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { hlcNodeId, isCanonicalHlc } from '@cindy/voice-input-core';
import { listMobileVoiceHistoryHosts } from '@/session/mobileVoiceHistoryStore';
-import { isFresherMobileVoiceDictionarySnapshot } from '@/session/mobileVoiceDictionaryView';
+import { isFresherSameHostMobileVoiceDictionarySnapshot } from '@/session/mobileVoiceDictionaryView';
import type {
MobileVoiceCredentialSyncDictionaryEntry,
MobileVoiceDictionarySnapshotResult,
@@ -272,11 +272,9 @@ function shouldAcceptIncomingSnapshot(
current: CachedDictionary,
next: CachedDictionary,
): boolean {
- // 同一 host 只接受更新鲜的快照。新鲜度唯一判据是版本向量包含关系,
- // 并列或都没有向量时才比到达时间。同步关闭的空投影必须自带当时的
- // stateVector,才能清掉同一代缓存;无向量的空表不能覆盖已证明自己
- // 更新的快照。
- return isFresherMobileVoiceDictionarySnapshot(next, current);
+ // 同一 host 只接受更新鲜的快照。先比版本向量,并列时比桌面 emittedAt。
+ // 跨桌面展示不走这条,避免各自主机时钟互比。
+ return isFresherSameHostMobileVoiceDictionarySnapshot(next, current);
}
/**
diff --git a/apps/mobile/src/session/mobileVoiceDictionaryView.ts b/apps/mobile/src/session/mobileVoiceDictionaryView.ts
index 185015ae3f..83dd6b8300 100644
--- a/apps/mobile/src/session/mobileVoiceDictionaryView.ts
+++ b/apps/mobile/src/session/mobileVoiceDictionaryView.ts
@@ -126,19 +126,38 @@ export interface MobileVoiceDictionarySnapshot {
export function isFresherMobileVoiceDictionarySnapshot(
candidate: MobileVoiceDictionarySnapshot,
best: MobileVoiceDictionarySnapshot,
+): boolean {
+ return compareSnapshotFreshness(candidate, best, (left, right) => left.fetchedAt > right.fetchedAt);
+}
+
+/** 同一 host 入站防倒灌:并列时比桌面发出时间,不用手机到达时间。 */
+export function isFresherSameHostMobileVoiceDictionarySnapshot(
+ candidate: MobileVoiceDictionarySnapshot,
+ best: MobileVoiceDictionarySnapshot,
+): boolean {
+ return compareSnapshotFreshness(candidate, best, isLaterByEmittedAt);
+}
+
+function compareSnapshotFreshness(
+ candidate: MobileVoiceDictionarySnapshot,
+ best: MobileVoiceDictionarySnapshot,
+ isLater: (
+ candidate: MobileVoiceDictionarySnapshot,
+ best: MobileVoiceDictionarySnapshot,
+ ) => boolean,
): boolean {
if (candidate.stateVector && best.stateVector) {
const candidateDominates = dominates(candidate.stateVector, best.stateVector);
const bestDominates = dominates(best.stateVector, candidate.stateVector);
if (candidateDominates !== bestDominates) return candidateDominates;
- return isLaterSnapshot(candidate, best);
+ return isLater(candidate, best);
}
if (candidate.stateVector) return true;
if (best.stateVector) return false;
- return isLaterSnapshot(candidate, best);
+ return isLater(candidate, best);
}
-function isLaterSnapshot(
+function isLaterByEmittedAt(
candidate: MobileVoiceDictionarySnapshot,
best: MobileVoiceDictionarySnapshot,
): boolean {
diff --git a/packages/device-link/src/__tests__/client.test.ts b/packages/device-link/src/__tests__/client.test.ts
index 6f007acba1..67fe31d669 100644
--- a/packages/device-link/src/__tests__/client.test.ts
+++ b/packages/device-link/src/__tests__/client.test.ts
@@ -1579,6 +1579,53 @@ describe('DeviceLinkClient', () => {
h.client.stop();
});
+ it('显式 close 后词典只读快照 push 仍走 unlinked legacy,不报 LINK_NOT_OPEN', async () => {
+ const h = makeHarness({ timing: { pingIntervalMs: 1_000 } });
+ h.client.start();
+ await tick();
+ h.current().ack();
+
+ const open = h.client.openLink(
+ 'dev-b',
+ { controllerName: 'Test', protocolVersion: 1, appVersion: '1' },
+ 100,
+ );
+ const sentOpen = h.current().sent.find((env) => env.kind === 'link-open')!;
+ h.current().push({
+ v: PROTOCOL_VERSION,
+ kind: 'link-accept',
+ id: sentOpen.id,
+ src: 'dev-b',
+ payload: {
+ appVersion: '1',
+ allowlistHash: 'hash',
+ capabilities: [DEVICE_LINK_CAPABILITY_RELIABLE_TRANSPORT],
+ transportStreamId: 'remote-stream',
+ },
+ });
+ await open;
+ h.client.closeLink('dev-b', 'user');
+
+ expect(() => h.client.sendPush('dev-b', 'maker:event', { text: 'blocked' })).toThrow(
+ expect.objectContaining({ code: 'LINK_NOT_OPEN' }),
+ );
+ const sentBefore = h.current().sent.length;
+ expect(() => h.client.sendPush(
+ 'dev-b',
+ 'device-link:voice:dictionary:snapshot',
+ { ok: true, entries: [] },
+ )).not.toThrow();
+ const sent = h.current().sent.at(-1)!;
+ expect(h.current().sent.length).toBeGreaterThan(sentBefore);
+ expect(sent).toMatchObject({
+ kind: 'push',
+ dst: 'dev-b',
+ payload: { channel: 'device-link:voice:dictionary:snapshot' },
+ });
+ expect(parseTransportPayload(sent.payload)).toBeNull();
+ h.client.stop();
+ });
+
it('显式 close 后只放行已接收的 legacy listing invoke-result 回程', async () => {
const h = makeHarness({ timing: { pingIntervalMs: 1_000 } });
h.client.start();
diff --git a/packages/device-link/src/client.ts b/packages/device-link/src/client.ts
index b12ffbee65..d7f2ab7666 100644
--- a/packages/device-link/src/client.ts
+++ b/packages/device-link/src/client.ts
@@ -3297,12 +3297,16 @@ const UNLINKED_LEGACY_INVOKE_CHANNELS = new Set([
'local-db:sessions:interrupted-pending',
'maker:git-safety:get',
]);
+const UNLINKED_LEGACY_PUSH_CHANNELS = new Set([
+ 'device-link:voice:dictionary:snapshot',
+]);
function isUnlinkedLegacyEnvelope(env: Envelope): boolean {
- if (env.kind !== 'invoke') return false;
- const payload = env.payload as Partial<InvokePayload> | undefined;
- return typeof payload?.channel === 'string'
- && UNLINKED_LEGACY_INVOKE_CHANNELS.has(payload.channel);
+ const payload = env.payload as { channel?: unknown } | undefined;
+ if (typeof payload?.channel !== 'string') return false;
+ if (env.kind === 'invoke') return UNLINKED_LEGACY_INVOKE_CHANNELS.has(payload.channel);
+ if (env.kind === 'push') return UNLINKED_LEGACY_PUSH_CHANNELS.has(payload.channel);
+ return false;
}
function isLegacyBusinessFrame(kind: Envelope['kind']): boolean {