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): retry speaker state after storage read failures

DavidShen59ad66e89486
diff --git a/apps/mobile/src/__tests__/mobileSpeakerFilter.test.ts b/apps/mobile/src/__tests__/mobileSpeakerFilter.test.ts
index 5e4aee625a..7c74dc7fd4 100644
--- a/apps/mobile/src/__tests__/mobileSpeakerFilter.test.ts
+++ b/apps/mobile/src/__tests__/mobileSpeakerFilter.test.ts
@@ -362,6 +362,82 @@ describe('mobile speaker filter', () => {
expect(loadSpeakerModels).not.toHaveBeenCalled(); // State only; native load stays lazy.
});
+ it.each(['preferences', 'voiceprint'] as const)('retries a transient %s read on the next recording', async source => {
+ installModels();
+ const key = accountVaultKey('global', 'user-a');
+ storeProfile(key);
+ const prefsKey = service.__testing.prefsKey(key);
+ asyncStore.set(prefsKey, JSON.stringify({ version: 1, enabled: true, strictnessOverride: 'strict' }));
+ const storage = vi.mocked(source === 'preferences' ? AsyncStorage.getItem : getSecureItem);
+ const read = storage.getMockImplementation()!;
+ const failedKey = source === 'preferences' ? prefsKey : service.__testing.profileKey(key);
+ storage.mockImplementation(async name => {
+ if (name === failedKey) throw new Error('storage temporarily unavailable');
+ return read(name);
+ });
+ try {
+ setMobileAuthOwner('user-a', 'global');
+ expect(await service.createMobileSpeakerGate()).toBeUndefined();
+ expect(service.getMobileSpeakerFilterSnapshot()).toMatchObject({ hydrated: false, enabled: false, enrolled: false });
+ } finally {
+ storage.mockImplementation(read);
+ }
+
+ // No process reset or account switch: the next recording must read again.
+ expect(await service.createMobileSpeakerGate()).toBeInstanceOf(SpeakerGate);
+ expect(service.getMobileSpeakerFilterSnapshot()).toMatchObject({
+ hydrated: true, enabled: true, enrolled: true, strictness: 'strict', strictnessCustomized: true,
+ });
+ expect(AsyncStorage.setItem).not.toHaveBeenCalled();
+ expect(setSecureItem).not.toHaveBeenCalled();
+ expect(FileSystem.createDownloadResumable).not.toHaveBeenCalled();
+ });
+
+ it.each(['enabled', 'strictness'] as const)('rejects changes to %s until saved state can be read', async setting => {
+ installModels();
+ const key = accountVaultKey('global', 'user-a');
+ const prefsKey = service.__testing.prefsKey(key);
+ const saved = JSON.stringify({ version: 1, enabled: true, strictnessOverride: 'strict' });
+ asyncStore.set(prefsKey, saved);
+ const read = vi.mocked(AsyncStorage.getItem).getMockImplementation()!;
+ vi.mocked(AsyncStorage.getItem).mockImplementation(async name => {
+ if (name === prefsKey) throw new Error('storage temporarily unavailable');
+ return read(name);
+ });
+ const edit = () => setting === 'enabled'
+ ? service.setMobileSpeakerFilterEnabled(false)
+ : service.setMobileSpeakerFilterStrictness('relaxed');
+ try {
+ setMobileAuthOwner('user-a', 'global');
+ await expect(edit()).rejects.toThrow();
+ expect(service.getMobileSpeakerFilterSnapshot()).toMatchObject({ hydrated: false, enabled: false, strictnessCustomized: false });
+ expect(AsyncStorage.setItem).not.toHaveBeenCalled();
+ expect(asyncStore.get(prefsKey)).toBe(saved);
+ } finally {
+ vi.mocked(AsyncStorage.getItem).mockImplementation(read);
+ }
+
+ await edit();
+ expect(JSON.parse(asyncStore.get(prefsKey)!)).toEqual({
+ version: 1,
+ enabled: setting !== 'enabled',
+ strictnessOverride: setting === 'enabled' ? 'strict' : 'relaxed',
+ });
+ });
+
+ it.each(['{', 'null'])('uses defaults for unreadable JSON (%s) without retrying storage', async raw => {
+ const key = accountVaultKey('global', 'user-a');
+ asyncStore.set(service.__testing.prefsKey(key), raw);
+ secureStore.set(service.__testing.profileKey(key), raw);
+ await signIn('user-a');
+ expect(service.getMobileSpeakerFilterSnapshot()).toMatchObject({ hydrated: true, enabled: false, enrolled: false });
+ vi.mocked(AsyncStorage.getItem).mockClear();
+ vi.mocked(getSecureItem).mockClear();
+ await service.hydrateMobileSpeakerFilter();
+ expect(AsyncStorage.getItem).not.toHaveBeenCalled();
+ expect(getSecureItem).not.toHaveBeenCalled();
+ });
+
it('does not give an old startup the next account\'s gate', async () => {
installModels();
for (const key of [accountVaultKey('global', 'user-a'), accountVaultKey('global', 'user-b')]) {
diff --git a/apps/mobile/src/session/mobileSpeakerFilter.ts b/apps/mobile/src/session/mobileSpeakerFilter.ts
index e019240ce4..8a8e4da87b 100644
--- a/apps/mobile/src/session/mobileSpeakerFilter.ts
+++ b/apps/mobile/src/session/mobileSpeakerFilter.ts
@@ -189,9 +189,11 @@ export function hydrateMobileSpeakerFilter(): Promise<void> {
}
async function readPrefs(accountKey: string): Promise<{ enabled: boolean; strictnessOverride?: SpeakerGateStrictness }> {
+ // Storage failures must reach hydration so the next call retries. Only a
+ // missing or invalid record should count as successfully loaded defaults.
+ const raw = await AsyncStorage.getItem(prefsKey(accountKey));
+ if (!raw) return { enabled: false };
try {
- const raw = await AsyncStorage.getItem(prefsKey(accountKey));
- if (!raw) return { enabled: false };
const parsed = JSON.parse(raw) as Partial<StoredPrefs>;
return {
enabled: parsed.enabled === true,
@@ -228,9 +230,9 @@ function restoreSavedPrefs(): void {
}
async function readProfile(accountKey: string): Promise<Profile | null> {
+ const raw = await getSecureItem(profileKey(accountKey));
+ if (!raw) return null;
try {
- const raw = await getSecureItem(profileKey(accountKey));
- if (!raw) return null;
const stored = JSON.parse(raw) as Partial<StoredProfile>;
// A voiceprint from another embedding model cannot be compared: re-enroll.
if (stored.version !== 1 || stored.modelSha256 !== speakerModelAsset('embedding').sha256) return null;
@@ -247,6 +249,7 @@ export async function setMobileSpeakerFilterEnabled(enabled: boolean): Promise<v
const owner = getMobileAuthOwner();
await hydrateMobileSpeakerFilter();
if (!owner.accountKey || !isMobileAuthOwnerCurrent(owner)) return;
+ if (hydratedFor !== owner.accountKey) throw new Error('speaker preferences not hydrated');
const revision = ++prefsRevision;
update({ enabled });
try {
@@ -267,6 +270,7 @@ export async function setMobileSpeakerFilterStrictness(strictness: SpeakerGateSt
const owner = getMobileAuthOwner();
await hydrateMobileSpeakerFilter();
if (!owner.accountKey || !isMobileAuthOwnerCurrent(owner)) return;
+ if (hydratedFor !== owner.accountKey) throw new Error('speaker preferences not hydrated');
const revision = ++prefsRevision;
update(
strictness === null