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): reconcile lost automation delete acknowledgements

david05688efea66c
diff --git a/apps/mobile/src/__tests__/CompanionAutomationSheet.test.tsx b/apps/mobile/src/__tests__/CompanionAutomationSheet.test.tsx
index 9b1460ead0..34d7af4069 100644
--- a/apps/mobile/src/__tests__/CompanionAutomationSheet.test.tsx
+++ b/apps/mobile/src/__tests__/CompanionAutomationSheet.test.tsx
@@ -376,6 +376,91 @@ it.each(['run', 'delete'])('renews a consumed %s action for an explicit retry',
expect(h.invoke.mock.calls[1][2].actionId).not.toBe(h.invoke.mock.calls[0][2].actionId);
});
+async function confirmDelete() {
+ await click('delete');
+ await act(async () => h.alert.mock.calls.at(-1)![2].find((item: any) => item.style === 'destructive').onPress());
+}
+
+it.each(['ios', 'android'])('returns to a fresh list when delete succeeded but its acknowledgement was lost on %s', async platform => {
+ h.platform = platform;
+ await openExisting();
+ h.perform.mockImplementationOnce(async () => {
+ h.existing = false;
+ throw new Error('Response lost');
+ });
+ h.read.mockImplementation((_invoke, _target, ref) => {
+ if (ref.id === 'bot:bot/rule' && !h.existing) {
+ // Device-link IPC transports a bracketed message; also accept structured errors.
+ throw platform === 'ios' ? new Error('[NOT_FOUND] Automation unavailable')
+ : Object.assign(new Error('Automation unavailable'), { code: 'NOT_FOUND' });
+ }
+ return Promise.resolve(resource(ref.id));
+ });
+ await confirmDelete();
+ expect(h.perform).toHaveBeenCalledOnce();
+ expect(h.perform.mock.calls[0][0].actionId).toBe('routine-delete');
+ expect(h.read.mock.calls.at(-1)![2].id).toBe('bot:bot');
+ expect(container.querySelector('input[aria-label="name"]')).toBeNull();
+ expect(container.textContent).toContain('empty');
+ expect(container.textContent).not.toContain('loadFailed');
+ expect(container.textContent).not.toContain('Existing');
+});
+
+it('retains delete reconciliation across a transient read failure and a manual retry', async () => {
+ await openExisting();
+ h.perform.mockRejectedValueOnce(new Error('Response lost'));
+ h.read.mockRejectedValueOnce(new Error('Read unavailable'));
+ await confirmDelete();
+ expect(container.textContent).toContain('Response lost');
+ expect(container.textContent).toContain('loadFailed');
+ expect(input('name').value).toBe('Existing');
+ h.existing = false;
+ h.read.mockRejectedValueOnce(Object.assign(new Error('Automation unavailable'), { code: 'NOT_FOUND' }));
+ await click('retry');
+ expect(h.invoke).toHaveBeenCalledOnce();
+ expect(container.querySelector('input[aria-label="name"]')).toBeNull();
+ expect(container.textContent).toContain('empty');
+});
+
+it('does not discard edits when a missing detail is unrelated to a pending delete', async () => {
+ await openExisting(); await type('hour', '12');
+ h.perform.mockRejectedValueOnce(new Error('Save rejected'));
+ h.read.mockRejectedValueOnce(Object.assign(new Error('Automation unavailable'), { code: 'NOT_FOUND' }));
+ await click('save');
+ expect(input('hour').value).toBe('12');
+ expect(container.textContent).toContain('Save rejected');
+ expect(container.textContent).toContain('loadFailed');
+});
+
+it('reconciles a lost delete response after reconnecting without sending delete again', async () => {
+ await openExisting();
+ let reject!: (error: Error) => void;
+ h.perform.mockImplementationOnce(() => new Promise((_resolve, fail) => { reject = fail; }));
+ await confirmDelete(); await render(false);
+ await act(async () => reject(new Error('Response lost')));
+ expect(input('name').value).toBe('Existing');
+ expect(container.textContent).toContain('Response lost');
+ h.existing = false;
+ h.read.mockRejectedValueOnce(Object.assign(new Error('Automation unavailable'), { code: 'NOT_FOUND' }));
+ await render(true);
+ expect(h.invoke).toHaveBeenCalledOnce();
+ expect(container.querySelector('input[aria-label="name"]')).toBeNull();
+ expect(container.textContent).toContain('empty');
+});
+
+it('keeps edits made after an uncertain delete when a retry finds the detail missing', async () => {
+ await openExisting();
+ h.perform.mockRejectedValueOnce(new Error('Response lost'));
+ h.read.mockRejectedValueOnce(new Error('Read unavailable'));
+ await confirmDelete();
+ await type('hour', '12');
+ h.read.mockRejectedValueOnce(Object.assign(new Error('Automation unavailable'), { code: 'NOT_FOUND' }));
+ await click('retry');
+ expect(h.invoke).toHaveBeenCalledOnce();
+ expect(input('hour').value).toBe('12');
+ expect(container.textContent).toContain('loadFailed');
+});
+
it('keeps a successful save committed when the following run fails', async () => {
await openExisting(); await type('hour', '12');
h.perform.mockImplementationOnce(async request => {
diff --git a/apps/mobile/src/session/CompanionAutomationSheet.tsx b/apps/mobile/src/session/CompanionAutomationSheet.tsx
index 1bfe07cd01..708ebc2f68 100644
--- a/apps/mobile/src/session/CompanionAutomationSheet.tsx
+++ b/apps/mobile/src/session/CompanionAutomationSheet.tsx
@@ -60,12 +60,18 @@ export function CompanionAutomationSheet({ visible, onClose, collectionId, botId
const requestId = useRef(randomUUID());
const seq = useRef(0);
const inFlight = useRef(false);
+ const pendingDelete = useRef(false);
const operationGeneration = useRef(0);
const dirty = draft !== null && JSON.stringify(draft) !== initialDraft;
const dirtyRef = useRef(dirty); dirtyRef.current = dirty;
const identity = `${accountGeneration}:${deviceId}:${collectionId}:${botId}`;
const current = useRef({ identity, visible, selected, online }); current.current = { identity, visible, selected, online };
const valid = (scope: string, page: string | null) => current.current.visible && current.current.identity === scope && current.current.selected === page;
+ const open = useCallback((id: string | null) => {
+ pendingDelete.current = false;
+ setDraft(null); setInitialDraft(''); dirtyRef.current = false; setDetail(null); setResource(null); setError(null); setSelected(id);
+ if (id === 'new') requestId.current = randomUUID();
+ }, []);
const load = useCallback(async (actionError?: string) => {
if (!visible || !botId || !valid(identity, selected)) return;
// An action may settle after connectivity changed; preserve its reason even offline.
@@ -103,19 +109,29 @@ export function CompanionAutomationSheet({ visible, onClose, collectionId, botId
}
}
} else setItems(parseRoutineSummaries(block.data));
+ pendingDelete.current = false;
setResource(result);
} catch (e) {
if (valid(scope, selected) && generation === seq.current) {
+ // A lost delete response may leave no detail to refresh. Only a confirmed
+ // missing resource settles that intent; transient failures remain retryable.
+ const missing = (e as { code?: unknown } | null)?.code === 'NOT_FOUND'
+ || e instanceof Error && e.message.startsWith('[NOT_FOUND]');
+ if (pendingDelete.current && !dirtyRef.current && missing) {
+ open(null);
+ return;
+ }
const unsupported = t('devices.companions.automation.unsupported');
const readError = e instanceof Error && e.message === unsupported ? unsupported : t('devices.resources.loadFailed');
setError(actionError ? `${actionError}\n${readError}` : readError);
}
}
finally { if (valid(scope, selected) && generation === seq.current) setLoading(false); }
- }, [visible, online, botId, identity, invoke, openLink, deviceId, deviceName, collectionId, selected, i18n.language, t]);
+ }, [visible, online, botId, identity, invoke, openLink, deviceId, deviceName, collectionId, selected, i18n.language, t, open]);
useEffect(() => {
setSelected(null); setResource(null); setItems([]); setDetail(null); setDraft(null); setInitialDraft(''); setError(null);
requestId.current = randomUUID();
+ pendingDelete.current = false;
operationGeneration.current++; inFlight.current = false; setBusy(false);
return () => { seq.current++; };
}, [visible, identity]);
@@ -137,10 +153,6 @@ export function CompanionAutomationSheet({ visible, onClose, collectionId, botId
{ text: tr('discard'), style: 'destructive', onPress: () => { if (valid(identity, selected)) action(); } },
]);
};
- const open = (id: string | null) => {
- setDraft(null); setInitialDraft(''); dirtyRef.current = false; setDetail(null); setResource(null); setError(null); setSelected(id);
- if (id === 'new') requestId.current = randomUUID();
- };
const act = async (actionId: string) => {
const capabilityId = getRoutineActionId(resource, actionId);
if (!valid(identity, selected) || inFlight.current || loading || !online || !resource || !capabilityId) return;
@@ -182,6 +194,7 @@ export function CompanionAutomationSheet({ visible, onClose, collectionId, botId
// The host consumes opaque actions even when validation or the response fails.
// Reconcile by reading, never replay a write or renew a creation request ID.
setResource(null);
+ pendingDelete.current = actionId === 'routine-delete';
await load(automationFailure(e, tr));
}
}