Skip to content

Commit eba1c86

Browse files
fix(mobile): initialize widget layouts and clear previous account activity
1 parent 03e0708 commit eba1c86

7 files changed

Lines changed: 155 additions & 171 deletions

File tree

‎apps/mobile/app.config.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,10 @@ const widgetsPlugin: NonNullable<ExpoConfig["plugins"]>[number] = [
188188
description: "Shows the current state of active T3 Code agents.",
189189
// Live Activity companion; there is no Android presentation for it.
190190
android: null,
191-
ios: { supportedFamilies: ["systemSmall", "systemMedium", "accessoryRectangular"] },
191+
ios: {
192+
supportedFamilies: ["systemSmall", "systemMedium", "accessoryRectangular"],
193+
initialLayout: "./src/widgets/AgentActivity.tsx",
194+
},
192195
},
193196
],
194197
},

‎apps/mobile/src/features/agent-awareness/agentLiveActivity.ios.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
import AgentActivity, {
2-
publishAgentActivityWidget,
2+
AgentActivityWidget,
33
type AgentActivityProps,
44
} from "../../widgets/AgentActivity";
55

6-
export { publishAgentActivityWidget };
6+
export function publishAgentActivityWidget(props: AgentActivityProps): boolean {
7+
try {
8+
AgentActivityWidget.updateSnapshot(props);
9+
return true;
10+
} catch {
11+
// Personal-team builds have no widget extension.
12+
return false;
13+
}
14+
}
715

816
export function getAgentLiveActivities() {
917
return AgentActivity.getInstances();

‎apps/mobile/src/features/agent-awareness/liveWidgetActivity.ts‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,16 @@ export function reconcileWidgetActivity(
143143
acknowledgedScope !== undefined &&
144144
sameWidgetEnvironmentScope(acknowledgedScope, [...live.keys()]);
145145
const globalScope = (acknowledgedScope?.length ?? 0) === 0;
146-
const activeCount =
147-
snapshot === null
148-
? null
149-
: exactScope
150-
? localActiveCount + (aggregate?.activeCount ?? 0)
151-
: !globalScope
152-
? null
153-
: live.size === 0
154-
? (aggregate?.activeCount ?? 0)
155-
: relayCountIsComplete
156-
? localActiveCount + remoteRows.filter(isActive).length
157-
: null;
146+
let activeCount: number | null = null;
147+
if (snapshot !== null) {
148+
if (exactScope) {
149+
activeCount = localActiveCount + (aggregate?.activeCount ?? 0);
150+
} else if (globalScope) {
151+
if (live.size === 0) activeCount = aggregate?.activeCount ?? 0;
152+
else if (relayCountIsComplete)
153+
activeCount = localActiveCount + remoteRows.filter(isActive).length;
154+
}
155+
}
158156
const activities = [...localRows, ...remoteRows]
159157
.sort(
160158
(left, right) =>

‎apps/mobile/src/features/agent-awareness/remoteRegistration.test.ts‎

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
571571
const snapshot = excludedEnvironmentIds
572572
? { ...remoteSnapshot, excludedEnvironmentIds }
573573
: remoteSnapshot;
574-
return ordinal === 2 && kind === blockedRead
574+
return ordinal === 1 && kind === blockedRead
575575
? Deferred.succeed(readStarted, undefined).pipe(
576576
Effect.andThen(Deferred.await(finishRead)),
577577
Effect.as(snapshot),
@@ -592,20 +592,9 @@ describe("makeRelayDeviceRegistrationRequest", () => {
592592
isReady: true,
593593
entries: new Map(),
594594
});
595-
expect(backgroundRuntime.pending).toHaveLength(1);
596-
const refresh = backgroundRuntime.pending.shift();
597-
if (!refresh) throw new Error("Expected scope-only widget refresh");
598-
const exit = yield* Effect.exit(
599-
refresh.operation as Effect.Effect<unknown, unknown, ManagedRelay.ManagedRelayClient>,
600-
).pipe(Effect.provide(layer));
601-
refresh.resolve(exit);
602-
expect(Exit.isSuccess(exit)).toBe(true);
603-
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
604-
expect.objectContaining({
605-
activeCount: 1,
606-
activities: remoteSnapshot.aggregate!.activities,
607-
}),
608-
);
595+
// The in-flight read rechecks the scope before publishing; no second
596+
// foreground reconciliation is needed before that first response.
597+
expect(backgroundRuntime.pending).toHaveLength(0);
609598
} else if (transition === "background") {
610599
appStateMock.currentState = "background";
611600
for (const listener of appStateMock.listeners) listener("background");
@@ -622,8 +611,8 @@ describe("makeRelayDeviceRegistrationRequest", () => {
622611
yield* Fiber.join(prime);
623612
if (transition === "scope change") {
624613
expect({ globalReads, scopedReads }).toEqual({
625-
globalReads: 3,
626-
scopedReads: blockedRead === "global" ? 1 : 2,
614+
globalReads: 1,
615+
scopedReads: 1,
627616
});
628617
expect(backgroundRuntime.pending).toHaveLength(0);
629618
expect(widgetMocks.start).toHaveBeenCalledExactlyOnceWith(
@@ -660,7 +649,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
660649
expect(activity.getPushToken).toHaveBeenCalledTimes(1);
661650
expect(backgroundRuntime.pending).toHaveLength(0);
662651
} else {
663-
expect(globalReads).toBe(2);
652+
expect(globalReads).toBe(1);
664653
expect(widgetMocks.start).not.toHaveBeenCalled();
665654
expect(activity.getPushToken).not.toHaveBeenCalled();
666655
expect(widgetBeforeRelease?.activities).toEqual([]);
@@ -739,7 +728,9 @@ describe("makeRelayDeviceRegistrationRequest", () => {
739728
}),
740729
expect.any(Date),
741730
);
742-
expect(publishAgentActivityWidget).not.toHaveBeenCalled();
731+
expect(publishAgentActivityWidget).toHaveBeenCalledExactlyOnceWith(
732+
expect.objectContaining({ activeCount: 0, activities: [] }),
733+
);
743734
});
744735

745736
it("preserves disabled Live Activity preferences in relay registrations", () => {
@@ -1752,7 +1743,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
17521743
yield* runBackgroundOperations();
17531744

17541745
expect(widgetMocks.start).not.toHaveBeenCalled();
1755-
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(1);
1746+
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(2);
17561747
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
17571748
expect.objectContaining({
17581749
activeCount: 1,
@@ -1786,7 +1777,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
17861777
yield* runBackgroundOperations();
17871778

17881779
expect(widgetMocks.start).not.toHaveBeenCalled();
1789-
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(1);
1780+
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(2);
17901781
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
17911782
expect.objectContaining({
17921783
activeCount: 1,
@@ -1868,7 +1859,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
18681859
yield* Deferred.succeed(finishFirstRead, undefined);
18691860
yield* Fiber.join(olderRefresh);
18701861

1871-
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(1);
1862+
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(2);
18721863
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
18731864
expect.objectContaining({
18741865
activities: [expect.objectContaining({ status: "Working" })],
@@ -1929,7 +1920,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
19291920
}),
19301921
expect.any(Date),
19311922
);
1932-
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(1);
1923+
expect(publishAgentActivityWidget).toHaveBeenCalledTimes(2);
19331924
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
19341925
expect.objectContaining({
19351926
activeCount: 0,
@@ -1983,7 +1974,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
19831974
const snapshot = excludedEnvironmentIds
19841975
? { aggregate: null, excludedEnvironmentIds }
19851976
: activeAgentActivitySnapshot;
1986-
return ordinal === 2 && kind === blockedRead
1977+
return ordinal === 1 && kind === blockedRead
19871978
? Deferred.succeed(readStarted, undefined).pipe(
19881979
Effect.andThen(Deferred.await(finishRead)),
19891980
Effect.as(snapshot),
@@ -2016,7 +2007,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
20162007
const readsBeforeTokenResolved = { global: globalReads, scoped: scopedReads };
20172008
finishToken("activity-token");
20182009
pending.resolve(yield* Fiber.join(localRegistration));
2019-
expect(readsBeforeTokenResolved).toEqual({ global: 2, scoped: 2 });
2010+
expect(readsBeforeTokenResolved).toEqual({ global: 1, scoped: 1 });
20202011
expect(startsBeforeTokenResolved).toBe(1);
20212012
expect(activities).toHaveLength(1);
20222013
}).pipe(Effect.scoped),
@@ -2073,11 +2064,9 @@ describe("makeRelayDeviceRegistrationRequest", () => {
20732064
preferencesStarted();
20742065
return preferences;
20752066
});
2076-
let readCount = 0;
2077-
const layer = snapshotRelayLayer(() => {
2078-
readCount++;
2079-
return Effect.succeed(readCount === 1 ? activeAgentActivitySnapshot : { aggregate: null });
2080-
});
2067+
let snapshot: RelayAgentActivitySnapshotResponse = activeAgentActivitySnapshot;
2068+
const readSnapshot = vi.fn(() => Effect.succeed(snapshot));
2069+
const layer = snapshotRelayLayer(readSnapshot);
20812070
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("clerk-token-user-a"));
20822071

20832072
const refresh = yield* refreshActiveLiveActivityRemoteRegistration().pipe(
@@ -2086,10 +2075,12 @@ describe("makeRelayDeviceRegistrationRequest", () => {
20862075
);
20872076
yield* Effect.promise(() => started);
20882077

2078+
expect(readSnapshot).not.toHaveBeenCalled();
2079+
snapshot = { aggregate: null };
20892080
finishPreferences({ liveActivitiesEnabled: true } as Preferences);
20902081
yield* Fiber.join(refresh);
20912082

2092-
expect(readCount).toBe(2);
2083+
expect(readSnapshot).toHaveBeenCalledTimes(1);
20932084
expect(widgetMocks.start).not.toHaveBeenCalled();
20942085
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
20952086
expect.objectContaining({
@@ -2369,7 +2360,7 @@ describe("makeRelayDeviceRegistrationRequest", () => {
23692360
);
23702361
});
23712362
yield* refreshActiveLiveActivityRemoteRegistration().pipe(Effect.provide(layer));
2372-
expect(queries).toEqual([undefined, [liveEnvironmentId], undefined, [liveEnvironmentId]]);
2363+
expect(queries).toEqual([undefined, [liveEnvironmentId]]);
23732364
expect(widgetMocks.start).toHaveBeenCalledWith(
23742365
expect.objectContaining({
23752366
activities: activeAgentActivitySnapshot.aggregate.activities,
@@ -2547,6 +2538,43 @@ describe("makeRelayDeviceRegistrationRequest", () => {
25472538
}),
25482539
);
25492540

2541+
it.effect.each(["account switch", "provider remount"] as const)(
2542+
"clears the prior account's native widget on %s even when the relay is unavailable",
2543+
(transition) =>
2544+
Effect.gen(function* () {
2545+
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("token-a"), "user-a");
2546+
backgroundRuntime.pending.length = 0;
2547+
yield* refreshActiveLiveActivityRemoteRegistration().pipe(
2548+
Effect.provide(snapshotRelayLayer()),
2549+
);
2550+
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
2551+
expect.objectContaining({
2552+
activeCount: 1,
2553+
activities: activeAgentActivitySnapshot.aggregate.activities,
2554+
}),
2555+
);
2556+
if (transition === "provider remount") releaseAgentAwarenessRelayTokenProvider();
2557+
setAgentAwarenessRelayTokenProvider(() => Promise.resolve("token-b"), "user-b");
2558+
backgroundRuntime.pending.length = 0;
2559+
yield* refreshActiveLiveActivityRemoteRegistration().pipe(
2560+
Effect.provide(
2561+
snapshotRelayLayer(() =>
2562+
Effect.fail(
2563+
new ManagedRelay.ManagedRelayRequestFailedError({
2564+
action: "read relay agent activity snapshot",
2565+
transportFailed: true,
2566+
cause: new Error("offline"),
2567+
}),
2568+
),
2569+
),
2570+
),
2571+
);
2572+
expect(publishAgentActivityWidget).toHaveBeenLastCalledWith(
2573+
expect.objectContaining({ activeCount: 0, activities: [] }),
2574+
);
2575+
}),
2576+
);
2577+
25502578
it.effect("publishes freshness changes even when rows and count are unchanged", () =>
25512579
Effect.gen(function* () {
25522580
addLiveEnvironment();

0 commit comments

Comments
 (0)