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(sharing): close shared task on IM session rotation

DavidShen02eb04d8660f
diff --git a/apps/desktop/src/main/localDb/client/__tests__/tx.test.ts b/apps/desktop/src/main/localDb/client/__tests__/tx.test.ts
index 836b836ec2..9811b82e03 100644
--- a/apps/desktop/src/main/localDb/client/__tests__/tx.test.ts
+++ b/apps/desktop/src/main/localDb/client/__tests__/tx.test.ts
@@ -104,6 +104,20 @@ CREATE TABLE sessions (
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
+CREATE TABLE shared_task_events (
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
+ shared_task_id TEXT NOT NULL,
+ session_id TEXT NOT NULL REFERENCES sessions(id) ON DELETE CASCADE,
+ revision INTEGER NOT NULL,
+ kind TEXT NOT NULL,
+ terminal INTEGER NOT NULL,
+ snapshot TEXT,
+ recorded_at INTEGER NOT NULL
+);
+CREATE UNIQUE INDEX shared_task_events_revision_idx
+ ON shared_task_events (shared_task_id, kind, revision);
+CREATE INDEX shared_task_events_session_idx
+ ON shared_task_events (session_id, id);
CREATE TABLE orca_teams (
id TEXT PRIMARY KEY,
lead_session_id TEXT NOT NULL,
@@ -2820,6 +2834,11 @@ describe('db worker tx handlers', () => {
channel, bot_context_id, user_id, scope_key, target_session_id, attached_at
) VALUES ('telegram', 'bot', 'user', '', 'telegram-old', 100)`,
);
+ await client.exec(
+ `INSERT INTO shared_task_events (
+ shared_task_id, session_id, revision, kind, terminal, recorded_at
+ ) VALUES ('shared-old', 'telegram-old', 1, 'authority', 0, 400)`,
+ );
const result = await client.tx('im.rotateSession', {
previousSessionId: 'telegram-old',
@@ -2869,6 +2888,29 @@ describe('db worker tx handlers', () => {
},
]);
await expect(client.query('SELECT * FROM im_bindings')).resolves.toEqual([]);
+ await expect(
+ client.query(
+ `SELECT shared_task_id, session_id, revision, kind, terminal, recorded_at
+ FROM shared_task_events ORDER BY id`,
+ ),
+ ).resolves.toEqual([
+ {
+ shared_task_id: 'shared-old',
+ session_id: 'telegram-old',
+ revision: 1,
+ kind: 'authority',
+ terminal: 0,
+ recorded_at: 400,
+ },
+ {
+ shared_task_id: 'shared-old',
+ session_id: 'telegram-old',
+ revision: 0,
+ kind: 'local-close',
+ terminal: 1,
+ recorded_at: 500,
+ },
+ ]);
});
});
diff --git a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts b/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
index 41d1befa68..d46e0b3cd3 100644
--- a/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
+++ b/apps/desktop/src/main/localDb/worker/opHandlers/tx.ts
@@ -2027,6 +2027,13 @@ function imRotateSession(
now,
now,
);
+ // IM rotation archives the previous task inside this transaction, so hand
+ // its shared-task authority to the journal before the old route disappears.
+ // The runtime will revoke local access and retry the server close from this
+ // durable terminal record, even when the relay is offline.
+ if (previousSessionId !== null) {
+ db.prepare(CLOSE_SHARED_TASKS_FOR_SESSION_SQL).run(now, previousSessionId);
+ }
if (previousSessionId !== null) retirePrevious.run(now, previousSessionId);
if (detachBinding !== null) {
deleteBinding.run(
diff --git a/apps/desktop/src/renderer/features/device-link/SharedTaskButton.tsx b/apps/desktop/src/renderer/features/device-link/SharedTaskButton.tsx
index d4af2cae20..ea89df37a2 100644
--- a/apps/desktop/src/renderer/features/device-link/SharedTaskButton.tsx
+++ b/apps/desktop/src/renderer/features/device-link/SharedTaskButton.tsx
@@ -19,7 +19,7 @@ import { sharedTaskErrorKey } from './sharedTaskCompatibility';
type SharedTaskTab = 'current' | 'owned';
type ConfirmState =
- | { kind: 'remove'; memberId: string; name: string }
+ | { kind: 'remove'; memberId: string; name: string; sharedTaskId: string }
| { kind: 'leave' }
| { kind: 'closeCurrent'; sharedTaskId: string }
| { kind: 'closeAll'; targets: SharedTaskOwnedItem[] };
@@ -146,7 +146,7 @@ export function SharedTaskButton({ session }: { session: Session }) {
switch (state.kind) {
case 'remove':
return { title: t('sharedTask.removeNamedTitle', { name: state.name }), body: t('sharedTask.removeBody'), keep: t('sharedTask.removeKeep'), action: t('sharedTask.remove'),
- run: () => void run(async () => { await host({ action: 'remove', sharedTaskId: detail!.sharedTaskId, memberId: state.memberId }); setConfirm(null); }) };
+ run: () => void run(async () => { await host({ action: 'remove', sharedTaskId: state.sharedTaskId, memberId: state.memberId }); setConfirm(null); }) };
case 'leave':
return { title: t('sharedTask.leaveTitle'), body: t('sharedTask.leaveBody'), keep: t('sharedTask.leaveKeep'), action: t('sharedTask.leave'),
run: () => void run(async () => {
@@ -210,7 +210,7 @@ export function SharedTaskButton({ session }: { session: Session }) {
<span className={avatarClass} aria-hidden>{Array.from(name)[0]}</span>
<div className="min-w-0 flex-1"><div className="break-words text-13 font-medium">{name}</div><div className="text-11 text-[var(--text-secondary)]">{t('sharedTask.roleGuest')}</div></div>
<Button variant="secondary" disabled={busy} className="shrink-0 px-3 text-12 text-[var(--error-fg)]"
- onClick={() => setConfirm({ kind: 'remove', memberId: member.memberId, name })}>{t('sharedTask.removeShort')}</Button>
+ onClick={() => setConfirm({ kind: 'remove', memberId: member.memberId, name, sharedTaskId: detail.sharedTaskId })}>{t('sharedTask.removeShort')}</Button>
</div>;
})}
<div className={noticeClass}><Clock size={16} className="mt-0.5 shrink-0" aria-hidden /><p>{t(session.deviceLinkDeviceId ? 'sharedTask.remoteHostOfflineNote' : 'sharedTask.hostOfflineNote')}</p></div>
diff --git a/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsx b/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsx
index 262401d09f..6a6ea6f2b2 100644
--- a/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsx
+++ b/apps/desktop/src/renderer/features/device-link/__tests__/SharedTaskButton.test.tsx
@@ -219,6 +219,29 @@ it('closes the shared task captured when the current-task confirmation opened',
vi.useRealTimers();
}
});
+it('removes a member from the shared task captured when the confirmation opened', async () => {
+ vi.useFakeTimers();
+ try {
+ let currentDetail: SharedTaskDetail | null = { ...detail, guests: [
+ { memberId: 'guest-1', accountId: 'guest-account', deviceIds: [], version: 1 },
+ ] };
+ const command = vi.fn(async (input: { action: string; sharedTaskId?: string; memberId?: string }) => {
+ if (input.action === 'remove') return { ok: true };
+ return { available: true, detail: currentDetail };
+ });
+ state.host.mockImplementation(command);
+ const body = await openWindow(ownerSession);
+ await act(async () => fireEvent.click(within(body).getByRole('button', { name: 'sharedTask.removeShort' })));
+ currentDetail = { ...detail, sharedTaskId: 'st2', title: 'Task B', guests: [] };
+ await act(async () => vi.advanceTimersByTimeAsync(5_000));
+ await act(async () => fireEvent.click(within(body).getByRole('button', { name: 'sharedTask.remove' })));
+ expect(command.mock.calls.filter(([input]) => input.action === 'remove')).toEqual([
+ [{ action: 'remove', sharedTaskId: 'st1', memberId: 'guest-1' }],
+ ]);
+ } finally {
+ vi.useRealTimers();
+ }
+});
it.each(['account change', 'unmount'])('stops the confirmed batch after %s', async (invalidation) => {
let finish!: (result: SharedTaskCloseResult) => void;
state.account.mockImplementation((command: { action: string }) => command.action === 'owned'