Skip to content

Commit 688025f

Browse files
nicofishmanclaude
andcommitted
fix(relay): keep queued Live Activity updates silent for read threads
The queued aggregate still matches after a visit, so its recomputed alert rang for a thread already read. The card still updates. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 067f140 commit 688025f

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

‎infra/relay/src/agentActivity/ApnsDeliveries.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,64 @@ describe("ApnsDeliveries", () => {
15441544
);
15451545
});
15461546

1547+
it.effect("updates a queued Done Live Activity silently once the thread is read", () => {
1548+
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
1549+
const completedAt = "1970-01-01T00:00:01.000Z";
1550+
const completedAggregate: RelayAgentActivityAggregateState = {
1551+
...aggregate,
1552+
activeCount: 0,
1553+
updatedAt: completedAt,
1554+
activities: [
1555+
{
1556+
...aggregate.activities[0]!,
1557+
phase: "completed",
1558+
status: "Done",
1559+
updatedAt: completedAt,
1560+
},
1561+
],
1562+
};
1563+
const payload = makeApnsDeliveryJobPayload({
1564+
kind: "live_activity_update",
1565+
userId: target.user_id,
1566+
deviceId: target.device_id,
1567+
token: target.activity_push_token ?? "activity-token",
1568+
aggregate: completedAggregate,
1569+
alert: { title: "Thread", body: "Done: Project" },
1570+
createdAt: completedAt,
1571+
expiresAt: "1970-01-01T00:10:00.000Z",
1572+
jobId: "job-update-visited",
1573+
});
1574+
const signed = signApnsDeliveryJob({ secret: config.apnsDeliveryJobSigningSecret, payload });
1575+
const execute = (request: HttpClientRequest.HttpClientRequest) =>
1576+
Effect.sync(() => {
1577+
requests.push(request);
1578+
return HttpClientResponse.fromWeb(request, new Response("", { status: 200 }));
1579+
});
1580+
1581+
return Effect.gen(function* () {
1582+
const deliveries = yield* ApnsDeliveries.ApnsDeliveries;
1583+
yield* deliveries.processSignedJob(signed);
1584+
1585+
expect(requests).toHaveLength(1);
1586+
const body = requests[0]?.body;
1587+
const sent = JSON.parse(
1588+
new TextDecoder().decode(body?._tag === "Uint8Array" ? body.body : undefined),
1589+
);
1590+
expect(sent.aps["content-state"]).toBeDefined();
1591+
expect(sent.aps.alert).toBeUndefined();
1592+
}).pipe(
1593+
Effect.provide(
1594+
layerFor({
1595+
attempts: [],
1596+
currentTargets: [{ ...target, last_aggregate_json: JSON.stringify(aggregate) }],
1597+
activityStates: [{ ...state, phase: "completed", updatedAt: completedAt, visited: true }],
1598+
config: signingConfig,
1599+
execute,
1600+
}),
1601+
),
1602+
);
1603+
});
1604+
15471605
it.effect("skips a queued Done notification after the thread resumes working", () => {
15481606
const attempts: Array<DeliveryAttempts.DeliveryAttemptInput> = [];
15491607
let executeCount = 0;

‎infra/relay/src/agentActivity/ApnsDeliveries.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -730,17 +730,32 @@ export const make = Effect.gen(function* () {
730730
if (alert) {
731731
const preferences = parsePreferences(currentTarget.preferences_json);
732732
const previousAggregate = parseAggregate(currentTarget.last_aggregate_json);
733+
// Reading a thread keeps its phase and updatedAt, so a job queued
734+
// before the visit still updates the card but must not ring for it.
735+
const visitedThreads = new Set(
736+
(yield* activityRows
737+
.listForUser({ userId: input.target.user_id })
738+
.pipe(Effect.orElseSucceed(() => [])))
739+
.filter((row) => row.visited)
740+
.map((row) => `${row.environmentId}\u0000${row.threadId}`),
741+
);
742+
const alertAggregate = aggregate && {
743+
...aggregate,
744+
activities: aggregate.activities.filter(
745+
(row) => !visitedThreads.has(`${row.environmentId}\u0000${row.threadId}`),
746+
),
747+
};
733748
alert =
734-
!preferences?.notificationsEnabled || !aggregate
749+
!preferences?.notificationsEnabled || !alertAggregate
735750
? null
736751
: (alertForAttentionTransition({
737752
previousAggregate,
738-
nextAggregate: aggregate,
753+
nextAggregate: alertAggregate,
739754
preferences,
740755
}) ??
741756
alertForNewlyTerminal({
742757
previousAggregate,
743-
nextAggregate: aggregate,
758+
nextAggregate: alertAggregate,
744759
preferences,
745760
nowMs: now.epochMilliseconds,
746761
includeUnobserved: true,

0 commit comments

Comments
 (0)