Skip to content

Commit de997b4

Browse files
committed
fix(relay): prioritize waiting agents before limiting activity rows
1 parent f66dcce commit de997b4

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,41 @@ describe("Android delivery routing", () => {
564564
}).pipe(Effect.provide(h.layer));
565565
});
566566

567+
for (const phase of ["waiting_for_approval", "waiting_for_input"] as const) {
568+
it.effect(`keeps ${phase} visible when five newer agents are running`, () => {
569+
const h = harness();
570+
const running = Array.from({ length: 5 }, (_, index) => ({
571+
...state,
572+
threadId: ThreadId.make(`running-${index}`),
573+
updatedAt: "1970-01-01T00:00:01.000Z",
574+
}));
575+
const waiting: RelayAgentActivityState = {
576+
...state,
577+
threadId: ThreadId.make("waiting"),
578+
threadTitle: "Needs your response",
579+
phase,
580+
deepLink: "/threads/env/waiting",
581+
};
582+
h.current.state = running[0]!;
583+
h.current.otherStates = [...running.slice(1), waiting];
584+
h.current.target.last_aggregate_json = encodeJson(aggregateFor(running));
585+
return Effect.gen(function* () {
586+
yield* TestClock.adjust("1 second");
587+
const delivery = yield* FcmDeliveries;
588+
yield* delivery.process({ ...h.job, state: waiting });
589+
expect(h.sent).toHaveLength(1);
590+
expect(h.sent[0]?.alert).toBe(true);
591+
expect(h.sent[0]?.data.activity_title).toBe("6 active agents · 1 needs attention");
592+
expect(h.sent[0]?.data.activity_line_0).toContain("\tNeeds your response\tProject");
593+
expect(h.sent[0]?.data.activity_path).toBe(waiting.deepLink);
594+
expect(h.sent[0]?.data.alert_path).toBe(waiting.deepLink);
595+
expect(
596+
Object.keys(h.sent[0]!.data).filter((key) => key.startsWith("activity_line_")),
597+
).toHaveLength(5);
598+
}).pipe(Effect.provide(h.layer));
599+
});
600+
}
601+
567602
it("shows five rows with attention then failure first, including project and status", () => {
568603
const aggregate = aggregateFor([
569604
state,

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ export function makeAggregateState(input: {
8080
readonly terminalState: RelayAgentActivityState | null;
8181
readonly nowMs: number;
8282
}): RelayAgentActivityAggregateState | null {
83-
const activeStates = input.activeStates.filter(
84-
(state) => !isTerminalPhase(state) && !isExpiredAgentActivityState(state, input.nowMs),
85-
);
83+
const needsAttention = (state: RelayAgentActivityState) =>
84+
state.phase === "waiting_for_approval" || state.phase === "waiting_for_input";
85+
// Select attention rows before applying the display cap. Sorting the final
86+
// payload cannot recover a waiting agent displaced by newer running agents.
87+
const activeStates = input.activeStates
88+
.filter((state) => !isTerminalPhase(state) && !isExpiredAgentActivityState(state, input.nowMs))
89+
.sort((a, b) => Number(needsAttention(b)) - Number(needsAttention(a)));
8690
if (activeStates.length === 0) {
8791
if (input.terminalState !== null) {
8892
return terminalAggregateState(input.terminalState);

0 commit comments

Comments
 (0)