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): respect task host and pending interactions

Chris579723d39337
diff --git a/apps/mobile/src/__tests__/CompanionMessageCard.test.tsx b/apps/mobile/src/__tests__/CompanionMessageCard.test.tsx
index 2fe53d6561..f2a91da287 100644
--- a/apps/mobile/src/__tests__/CompanionMessageCard.test.tsx
+++ b/apps/mobile/src/__tests__/CompanionMessageCard.test.tsx
@@ -243,6 +243,46 @@ it('does not borrow another computer\'s task identity or activity for the same i
expect(h.invoke).not.toHaveBeenCalledWith('home', 'local-db:sessions:get', ['child']);
});
+// Reverse collision ordering: the card owns the cached row, then another host
+// sends live signals for that ID. Assert the production card's visible status.
+it.each(['running', 'completed'] as const)('keeps the owning computer\'s %s state despite another host\'s pushes', async (phase) => {
+ remoteSessionStore.setDeviceSessions('home', 'Mac', [{ id: 'child', title: 'Own task', status: 'active' } as RemoteSession]);
+ remoteSessionStore.setDeviceSessions('other', 'Other Mac', [{ id: 'child', title: 'Other task', status: 'active' } as RemoteSession]);
+ remoteSessionStore.applyRemotePush('home', SESSION_ACTIVITY_CHANNEL, { sessionId: 'child', phase, attention: true });
+ h.invoke.mockRejectedValue(new Error('read failed'));
+ await render();
+ await act(async () => {
+ remoteSessionStore.applyRemotePush('other', SESSION_ACTIVITY_CHANNEL, {
+ sessionId: 'child', phase: phase === 'running' ? 'completed' : 'needs-interaction', attention: true,
+ });
+ remoteSessionStore.applyRemotePush('other', 'maker:event', {
+ sessionId: 'child', event: { type: phase === 'running' ? 'done' : 'status', data: { isRunning: true } },
+ });
+ });
+ expect(node.textContent).toContain(`devices.companions.status.${phase}`);
+ expect(node.textContent).not.toContain('devices.companions.status.waiting');
+ expect(openEntry()?.disabled).toBe(false);
+});
+
+// A missing activity relay must not hide an interaction already known to the list.
+it('shows waiting for pending interactions without activity and clears it after dismissal', async () => {
+ remoteSessionStore.setDeviceSessions('home', 'Mac', [{ id: 'child', title: 'Own task', status: 'active' } as RemoteSession]);
+ h.invoke.mockRejectedValue(new Error('read failed'));
+ await render();
+ await act(async () => remoteSessionStore.applyRemotePush('home', 'maker:interaction-request', {
+ sessionId: 'child', request: { kind: 'permission', requestId: 'permission-1' },
+ }));
+ expect(node.textContent).toContain('devices.companions.status.waiting');
+ await act(async () => remoteSessionStore.applyRemotePush('other', 'maker:interaction-dismissed', {
+ sessionId: 'child', requestId: 'permission-1',
+ }));
+ expect(node.textContent).toContain('devices.companions.status.waiting');
+ await act(async () => remoteSessionStore.applyRemotePush('home', 'maker:interaction-dismissed', {
+ sessionId: 'child', requestId: 'permission-1',
+ }));
+ expect(node.textContent).not.toContain('devices.companions.status.waiting');
+});
+
it('keeps the task card when the host omits delegations or sends a broken status', async () => {
h.invoke.mockResolvedValue({ ok: true });
await render();
diff --git a/apps/mobile/src/__tests__/remoteSessionStore.test.ts b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
index 90cd671aab..b2e3ef22c1 100644
--- a/apps/mobile/src/__tests__/remoteSessionStore.test.ts
+++ b/apps/mobile/src/__tests__/remoteSessionStore.test.ts
@@ -45,8 +45,8 @@ function messageAt(id: string, sessionId: string, createdAt: string): RemoteMess
return { ...message(id, sessionId), createdAt };
}
-function pushMakerStatus(sessionId: string, data: Record<string, unknown>): void {
- remoteSessionStore.applyRemotePush('dev-1', 'maker:event', {
+function pushMakerStatus(sessionId: string, data: Record<string, unknown>, deviceId = 'dev-1'): void {
+ remoteSessionStore.applyRemotePush(deviceId, 'maker:event', {
sessionId,
event: { type: 'status', data },
});
@@ -3393,7 +3393,8 @@ describe('remoteSessionStore', () => {
generationDurationMs: 400,
generationActive: true,
generationReliable: true,
- });
+ }, id);
+ expect(remoteSessionStore.isSessionMakerTurnRunning(`s-${id}`)).toBe(true);
}
const notify = vi.fn();
const unsubscribe = remoteSessionStore.subscribe(notify);
diff --git a/apps/mobile/src/session/CompanionMessageCard.tsx b/apps/mobile/src/session/CompanionMessageCard.tsx
index 5238958f55..fd6a558895 100644
--- a/apps/mobile/src/session/CompanionMessageCard.tsx
+++ b/apps/mobile/src/session/CompanionMessageCard.tsx
@@ -200,7 +200,7 @@ export function CompanionTaskCard({
// Without either live evidence or a delegation snapshot, keep the unknown/loading state.
const resolved = value !== null || error || !online;
const status =
- task.activity?.phase === 'needs-interaction' || row?.status === 'waiting' ? 'waiting'
+ task.waiting || task.activity?.phase === 'needs-interaction' || row?.status === 'waiting' ? 'waiting'
: task.running || row?.status === 'running' ? 'running'
: task.activity?.phase === 'error' ? 'failed'
: task.activity?.phase === 'completed' ? 'completed'
diff --git a/apps/mobile/src/session/remoteSessionStore.ts b/apps/mobile/src/session/remoteSessionStore.ts
index 0693106441..b1c2744210 100644
--- a/apps/mobile/src/session/remoteSessionStore.ts
+++ b/apps/mobile/src/session/remoteSessionStore.ts
@@ -1074,6 +1074,13 @@ function listMessageWriteAllowed(sessionId: string, deviceId?: string): boolean
&& retentionForSession(sessionId) === 'regular';
}
+/** Match live state to the same selected host as the merged task row. */
+function sessionStateWriteAllowed(sessionId: string, deviceId?: string): boolean {
+ const indexedDeviceId = sessionDeviceIndex.get(sessionId);
+ return !deviceId || !indexedDeviceId || indexedDeviceId === deviceId
+ || mergedSessionById.get(sessionId)?.canonicalDeviceId === deviceId;
+}
+
function normalizeWindowForRetention(
sessionId: string,
list: readonly RemoteMessage[],
@@ -4352,8 +4359,7 @@ export const remoteSessionStore = {
if (!isRecord(item)) continue;
const sessionId = readString(item, 'sessionId');
if (sessionId && typeof item.isTurnRunning === 'boolean') {
- const indexedDeviceId = sessionDeviceIndex.get(sessionId);
- if (indexedDeviceId && indexedDeviceId !== deviceId) continue;
+ if (!sessionStateWriteAllowed(sessionId, deviceId)) continue;
snapshotStates.set(sessionId, {
running: item.isTurnRunning,
activityPhase: readString(item, 'activityPhase'),
@@ -4681,7 +4687,7 @@ export const remoteSessionStore = {
if (channel === 'maker:status-changed' && isRecord(payload)) {
const sessionId = readString(payload, 'sessionId');
const status = readString(payload, 'status');
- if (sessionId && status === 'closed') this.setSessionRunning(sessionId, false);
+ if (sessionId && status === 'closed' && sessionStateWriteAllowed(sessionId, deviceId)) this.setSessionRunning(sessionId, false);
return;
}
if (channel === 'usage:message-turn-cost' && isRecord(payload)) {
@@ -4780,7 +4786,7 @@ export const remoteSessionStore = {
if (channel === 'maker:interaction-request' && isRecord(payload)) {
const sessionId = readString(payload, 'sessionId');
const request = isRecord(payload.request) ? payload.request : null;
- if (sessionId && request) {
+ if (sessionId && request && sessionStateWriteAllowed(sessionId, deviceId)) {
this.applyInteractionRequest(sessionId, {
request: request as PendingInteraction['request'],
persistId: readString(payload, 'persistId') ?? undefined,
@@ -4791,7 +4797,7 @@ export const remoteSessionStore = {
if (channel === 'maker:interaction-dismissed' && isRecord(payload)) {
const sessionId = readString(payload, 'sessionId');
const requestId = readString(payload, 'requestId');
- if (sessionId && requestId) {
+ if (sessionId && requestId && sessionStateWriteAllowed(sessionId, deviceId)) {
if (payload.reason === 'resolved' && !(isRecord(payload.decision) && payload.decision.dismissed === true)) {
this.confirmProtectedQuestion(sessionId, requestId);
}
@@ -4827,11 +4833,12 @@ export const remoteSessionStore = {
emit();
},
- applySessionActivity(_deviceId: string, payload: unknown): void {
+ applySessionActivity(deviceId: string, payload: unknown): void {
if (!isRecord(payload)) return;
const sessionId = readString(payload, 'sessionId');
const phase = readString(payload, 'phase');
- if (!sessionId || !isRemoteSessionLiveActivityPhase(phase)) return;
+ if (!sessionId || !isRemoteSessionLiveActivityPhase(phase)
+ || !sessionStateWriteAllowed(sessionId, deviceId)) return;
markSessionMakerActivity(sessionId);
const compactDetail = typeof payload.compactDetail === 'string' ? payload.compactDetail : '';
let changed = false;
@@ -4895,8 +4902,12 @@ export const remoteSessionStore = {
deviceId?: string,
fromList = false,
): void {
- markSessionMakerActivity(sessionId);
const type = readString(event, 'type');
+ // Text has its own transport replacement/retention rules; only fence the
+ // runtime signals here, so reconnecting transports keep their existing data path.
+ if ((type === 'status' || type === 'done' || type === 'error')
+ && !sessionStateWriteAllowed(sessionId, deviceId)) return;
+ markSessionMakerActivity(sessionId);
const reconnectCleared = type !== null && type !== 'error' && type !== 'done'
? clearSessionReconnectAttempt(sessionId)
: false;
diff --git a/apps/mobile/src/session/useCompanionTaskSession.ts b/apps/mobile/src/session/useCompanionTaskSession.ts
index e484162b9e..8b6c041e16 100644
--- a/apps/mobile/src/session/useCompanionTaskSession.ts
+++ b/apps/mobile/src/session/useCompanionTaskSession.ts
@@ -30,5 +30,8 @@ export function useCompanionTaskSession(deviceId: string, sessionId: string | nu
const running = useSyncExternalStore(remoteSessionStore.subscribeHomeStatus, () =>
readable && remoteSessionStore.isSessionRunning(sessionId!),
);
- return { session, activity, running, originConflict, refresh: query.refresh };
+ const waiting = useSyncExternalStore(remoteSessionStore.subscribeHomeStatus, () =>
+ readable && remoteSessionStore.getPendingInteractions(sessionId!).length > 0,
+ );
+ return { session, activity, running, waiting, originConflict, refresh: query.refresh };
}