Skip to content

Commit e6b2460

Browse files
committed
fix(relay): retain re-enable history for active attention
1 parent f6bf4ae commit e6b2460

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const a = EnvironmentId.make("env-a");
2222
const b = EnvironmentId.make("env-b");
2323

2424
describe("Devices", () => {
25-
it.effect("records when each environment is re-enabled for the delivery job lifetime", () =>
25+
it.effect("records when each environment is re-enabled for the activity lifetime", () =>
2626
Effect.gen(function* () {
2727
const db = yield* makeTestRelayDb;
2828
const services = Layer.mergeAll(Devices.layer, LiveActivities.layer).pipe(
@@ -63,13 +63,16 @@ describe("Devices", () => {
6363
yield* at(3_000);
6464
yield* register(undefined);
6565
expect(yield* revealedAt).toEqual({ [a]: 1_000, [b]: 3_000 });
66-
// Entries outlive every job queued before them, then expire.
66+
// Waiting attention outlives queued jobs, so an unchanged refresh must preserve this history.
6767
yield* at(1_000 + 5 * 60_000 + 1);
6868
yield* register([a]);
69+
expect(yield* revealedAt).toEqual({ [a]: 1_000, [b]: 3_000 });
70+
yield* at(1_000 + 24 * 60 * 60_000 + 1);
71+
yield* register([a]);
6972
expect(yield* revealedAt).toEqual({ [b]: 3_000 });
70-
yield* at(400_000);
73+
yield* at(2_000 + 24 * 60 * 60_000);
7174
yield* register([]);
72-
expect(yield* revealedAt).toEqual({ [a]: 400_000 });
75+
expect(yield* revealedAt).toEqual({ [a]: 2_000 + 24 * 60 * 60_000, [b]: 3_000 });
7376
}).pipe(Effect.provide(services));
7477
}).pipe(Effect.provide(NodeServices.layer)),
7578
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { sql } from "drizzle-orm";
1212

1313
import * as RelayDb from "../db.ts";
1414
import { relayLiveActivities, relayMobileDevices } from "../persistence/schema.ts";
15-
import { ANDROID_DELIVERY_JOB_TTL_MS } from "./agentActivityAlerts.ts";
15+
import { MAX_AGENT_ACTIVITY_ROW_TTL_MS } from "./agentActivityPayloads.ts";
1616

1717
export class DeviceRegistrationPersistenceError extends Schema.TaggedError<DeviceRegistrationPersistenceError>()(
1818
"DeviceRegistrationPersistenceError",
@@ -164,7 +164,7 @@ const make = Effect.gen(function* () {
164164
from jsonb_each(
165165
coalesce(${relayMobileDevices.environmentRevealedAt}, '{}'::jsonb)
166166
) as kept
167-
where (kept.value)::bigint >= ${now.epochMilliseconds - ANDROID_DELIVERY_JOB_TTL_MS}
167+
where (kept.value)::bigint >= ${now.epochMilliseconds - MAX_AGENT_ACTIVITY_ROW_TTL_MS}
168168
), '{}'::jsonb)
169169
|| coalesce((
170170
select jsonb_object_agg(revealed.id, ${now.epochMilliseconds}::bigint)

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,37 @@ describe("Android delivery routing", () => {
488488
),
489489
);
490490

491+
it.effect("an unchanged refresh preserves old attention exclusions after the job lifetime", () =>
492+
withRegisteredDevice(({ h, register, target }) =>
493+
Effect.gen(function* () {
494+
const delivery = yield* FcmDeliveries.FcmDeliveries;
495+
const other = { ...secondState, environmentId: EnvironmentId.make("other-env") };
496+
h.current.otherStates = [other];
497+
yield* register(preferences);
498+
yield* delivery.process({ ...h.job, state: null, replay: true });
499+
yield* TestClock.adjust("1 second");
500+
yield* register({ ...preferences, disabledEnvironmentIds: [other.environmentId] });
501+
h.current.otherStates = [{ ...other, phase: "waiting_for_approval" }];
502+
yield* delivery.process({ ...h.job, state: null, replay: true });
503+
yield* TestClock.adjust("1 second");
504+
yield* register(preferences);
505+
yield* TestClock.adjust("6 minutes");
506+
yield* register(preferences);
507+
h.current.state = {
508+
...state,
509+
phase: "waiting_for_input",
510+
updatedAt: DateTime.formatIso(yield* DateTime.now),
511+
};
512+
yield* delivery.enqueue({ target: yield* target, state: h.current.state });
513+
yield* delivery.process(h.queued[0]);
514+
expect(h.sent.filter((message) => message.alert).map((message) => message.data)).toEqual([
515+
expect.objectContaining({ alert_title: state.threadTitle }),
516+
]);
517+
expect(h.marked.at(-1)?.aggregate?.activities).toHaveLength(2);
518+
}),
519+
),
520+
);
521+
491522
const cardsOff = { name: "with activity cards off", cards: false, notificationOnly: false };
492523
const notificationOnly = {
493524
name: "for a notification-only environment",

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function isTerminalPhase(state: RelayAgentActivityState): boolean {
1414

1515
// Dead environments never publish terminal states; expire their rows so activeCount cannot remain inflated.
1616
const RUNNING_AGENT_ACTIVITY_ROW_TTL_MS = 2 * 60 * 60 * 1_000;
17-
const WAITING_AGENT_ACTIVITY_ROW_TTL_MS = 24 * 60 * 60 * 1_000;
17+
export const MAX_AGENT_ACTIVITY_ROW_TTL_MS = 24 * 60 * 60 * 1_000;
1818

1919
export function agentActivityExpiresAt(
2020
state: Pick<RelayAgentActivityState, "phase" | "updatedAt">,
@@ -29,7 +29,7 @@ export function agentActivityExpiresAt(
2929
const ttlMs =
3030
state.phase === "running" || state.phase === "starting"
3131
? RUNNING_AGENT_ACTIVITY_ROW_TTL_MS
32-
: WAITING_AGENT_ACTIVITY_ROW_TTL_MS;
32+
: MAX_AGENT_ACTIVITY_ROW_TTL_MS;
3333
return updatedAtMs + ttlMs;
3434
}
3535

0 commit comments

Comments
 (0)