Skip to content

Commit a8603af

Browse files
t3dotggclaude
andauthored
fix(server): mobile push no longer says "completed" between runs (pingdotgg#24769)
Mobile push said "Thread completed" when a run ended and its continuation started a moment later, then again when the work really finished. Every completion now waits the relay's existing 5s confirm window and is dropped if the thread is live again. A thread that auto-settles during the hold still alerts its completion, then clears its card on a new window. Failed runs publish at once. Follow-up to pingdotgg#24760. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent a96cf08 commit a8603af

2 files changed

Lines changed: 150 additions & 2 deletions

File tree

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ describe("AgentAwarenessRelay", () => {
392392
const terminal = shell({ status });
393393
yield* Ref.set(currentShell, terminal);
394394
yield* relay.publishThread(THREAD_ID);
395+
yield* TestClock.adjust("5 seconds");
396+
yield* relay.drain;
395397
assert.equal(publications[1]?.state?.phase, status);
396398

397399
const now = yield* DateTime.now;
@@ -419,6 +421,113 @@ describe("AgentAwarenessRelay", () => {
419421
}).pipe(Effect.scoped),
420422
);
421423

424+
it.effect("holds a completion through a continuation gap, then publishes it once", () =>
425+
Effect.gen(function* () {
426+
const { relay, currentShell, publications } = yield* makeTestRelay();
427+
yield* relay.publishThread(THREAD_ID);
428+
yield* Ref.set(currentShell, shell({ status: "completed" }));
429+
yield* relay.publishThread(THREAD_ID);
430+
yield* Ref.set(currentShell, shell());
431+
yield* relay.publishThread(THREAD_ID);
432+
yield* TestClock.adjust("5 seconds");
433+
yield* relay.drain;
434+
assert.deepEqual(
435+
publications.map((publication) => publication.state?.phase),
436+
["running"],
437+
);
438+
439+
yield* Ref.set(currentShell, shell({ status: "completed" }));
440+
yield* relay.publishThread(THREAD_ID);
441+
assert.equal(publications.length, 1);
442+
yield* TestClock.adjust("5 seconds");
443+
yield* relay.drain;
444+
assert.deepEqual(
445+
publications.map((publication) => publication.state?.phase),
446+
["running", "completed"],
447+
);
448+
}),
449+
);
450+
451+
it.effect("still publishes a completion when the thread settles during the hold", () =>
452+
Effect.gen(function* () {
453+
const { relay, currentShell, publications } = yield* makeTestRelay();
454+
yield* relay.publishThread(THREAD_ID);
455+
const completed = shell({ status: "completed" });
456+
yield* Ref.set(currentShell, completed);
457+
yield* relay.publishThread(THREAD_ID);
458+
yield* Ref.set(currentShell, {
459+
...completed,
460+
settledOverride: "settled",
461+
settledAt: yield* DateTime.now,
462+
lastVisitedAt: null,
463+
});
464+
yield* relay.publishThread(THREAD_ID);
465+
yield* TestClock.adjust("5 seconds");
466+
yield* relay.drain;
467+
assert.deepEqual(
468+
publications.map((publication) => publication.state?.phase ?? null),
469+
["running", "completed"],
470+
);
471+
472+
yield* TestClock.adjust("5 seconds");
473+
yield* relay.drain;
474+
assert.deepEqual(
475+
publications.map((publication) => publication.state?.phase ?? null),
476+
["running", "completed", null],
477+
);
478+
}),
479+
);
480+
481+
it.effect("drops a held completion when a queued continuation hides the thread", () =>
482+
Effect.gen(function* () {
483+
const { relay, currentShell, publications } = yield* makeTestRelay();
484+
yield* relay.publishThread(THREAD_ID);
485+
yield* Ref.set(currentShell, shell({ status: "completed" }));
486+
yield* relay.publishThread(THREAD_ID);
487+
yield* Ref.set(currentShell, shell({ status: "queued" }));
488+
yield* relay.publishThread(THREAD_ID);
489+
yield* TestClock.adjust("5 seconds");
490+
yield* relay.drain;
491+
assert.notInclude(
492+
publications.map((publication) => publication.state?.phase ?? null),
493+
"completed",
494+
);
495+
}),
496+
);
497+
498+
it.effect("retries a failed completion even after the thread settles", () =>
499+
Effect.gen(function* () {
500+
const { relay, currentShell, publications } = yield* makeTestRelay({
501+
respond: (attempt) =>
502+
attempt === 2
503+
? new Response("unavailable", { status: 503 })
504+
: Response.json({ ok: true, deliveries: [] }),
505+
});
506+
yield* relay.publishThread(THREAD_ID);
507+
const completed = shell({ status: "completed" });
508+
yield* Ref.set(currentShell, completed);
509+
yield* relay.publishThread(THREAD_ID);
510+
yield* TestClock.adjust("5 seconds");
511+
yield* relay.drain;
512+
yield* Ref.set(currentShell, {
513+
...completed,
514+
settledOverride: "settled",
515+
settledAt: yield* DateTime.now,
516+
lastVisitedAt: null,
517+
});
518+
yield* TestClock.adjust("1 second");
519+
yield* relay.drain;
520+
assert.deepEqual(
521+
publications.map((publication) => publication.state?.phase ?? null),
522+
["running", "completed", "completed"],
523+
);
524+
525+
yield* TestClock.adjust("5 seconds");
526+
yield* relay.drain;
527+
assert.equal(publications.at(-1)?.state ?? null, null);
528+
}),
529+
);
530+
422531
it.effect("keeps settled live work until its awaited monitor finishes", () =>
423532
Effect.gen(function* () {
424533
const { relay, currentShell, publications } = yield* makeTestRelay();
@@ -595,6 +704,8 @@ describe("AgentAwarenessRelay", () => {
595704
yield* deliver(
596705
itemEvent("event:held-item:end", item("item:tool", { ...background, status: "completed" })),
597706
);
707+
yield* TestClock.adjust("5 seconds");
708+
yield* relay.drain;
598709
assert.deepEqual(
599710
publications.map((publication) => publication.state?.phase),
600711
["running", "completed"],

‎apps/server/src/relay/AgentAwarenessRelay.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ export const make = Effect.gen(function* () {
411411

412412
// Confirm through the same worker so tombstones cannot race live updates; recovery clears the deadline.
413413
const publishConfirmDeadlines = new Map<ThreadId, number>();
414+
// Threads whose confirmation holds a completion, so a thread that settles during the hold still alerts.
415+
const heldCompletions = new Set<ThreadId>();
414416
let schedulePublishConfirm: (threadId: ThreadId) => Effect.Effect<void> = () => Effect.void;
415417
const publishRetries = new Map<
416418
ThreadId,
@@ -429,6 +431,7 @@ export const make = Effect.gen(function* () {
429431
publishedRelayConfig = undefined;
430432
cachedRelayClient = undefined;
431433
publishConfirmDeadlines.clear();
434+
heldCompletions.clear();
432435
yield* Ref.set(publishedStateByThreadRef, new Map());
433436
});
434437

@@ -542,6 +545,7 @@ export const make = Effect.gen(function* () {
542545
if (publishedStateByThread.get(threadId) === publishIdentity) {
543546
// Clear moot deadlines so later transient nulls cannot skip their deferral window.
544547
publishConfirmDeadlines.delete(threadId);
548+
heldCompletions.delete(threadId);
545549
yield* Effect.logDebug("agent activity publish skipped; projected state unchanged", {
546550
environmentId,
547551
threadId,
@@ -551,12 +555,14 @@ export const make = Effect.gen(function* () {
551555
}
552556

553557
// Confirm tombstones over live state to avoid removing active cards.
554-
// Confirm first-state completion because sessions briefly project completed before their first turn.
558+
// Confirm completion: a run can end moments before its continuation starts, and the relay
559+
// alerts on every move into completed.
555560
const requiresConfirmation =
556561
(snapshot.state === null &&
557562
publishedStateByThread.get(threadId) !== agentAwarenessPublishIdentity(null)) ||
558-
(snapshot.state?.phase === "completed" && !publishedStateByThread.has(threadId));
563+
snapshot.state?.phase === "completed";
559564
if (requiresConfirmation) {
565+
if (snapshot.state?.phase === "completed") heldCompletions.add(threadId);
560566
const nowMs = (yield* DateTime.now).epochMilliseconds;
561567
const deadline = publishConfirmDeadlines.get(threadId);
562568
if (deadline === undefined) {
@@ -581,8 +587,38 @@ export const make = Effect.gen(function* () {
581587
statePhase: snapshot.state?.phase ?? null,
582588
shell: describeThreadShellForAwareness(thread),
583589
});
590+
// Settled during the hold, not resumed: alert the current completion, then clear the card
591+
// on a new window. Only settlement may hide it; queued or silent runs also project to null.
592+
const unsettled =
593+
snapshot.state === null &&
594+
heldCompletions.has(threadId) &&
595+
Option.isSome(thread) &&
596+
thread.value.settledOverride === "settled"
597+
? resolveAgentAwarenessRelayPublishSnapshot({
598+
environmentId,
599+
threadId,
600+
thread: Option.some({ ...thread.value, settledOverride: null }),
601+
project,
602+
})
603+
: null;
604+
if (unsettled?.state?.phase === "completed") {
605+
const completion = unsettled.state;
606+
yield* publishState({
607+
projectId: unsettled.projectId,
608+
state: completion,
609+
reason: unsettled.reason,
610+
}).pipe(withRelayClientTracing);
611+
heldCompletions.delete(threadId);
612+
publishConfirmDeadlines.set(threadId, nowMs + 5_000);
613+
yield* Ref.update(publishedStateByThreadRef, (publishedStates) =>
614+
new Map(publishedStates).set(threadId, agentAwarenessPublishIdentity(completion)),
615+
);
616+
yield* schedulePublishConfirm(threadId);
617+
return;
618+
}
584619
} else {
585620
publishConfirmDeadlines.delete(threadId);
621+
heldCompletions.delete(threadId);
586622
}
587623

588624
if (snapshot.reason === "thread-not-found") {
@@ -604,6 +640,7 @@ export const make = Effect.gen(function* () {
604640
reason: snapshot.reason,
605641
}).pipe(withRelayClientTracing);
606642
publishConfirmDeadlines.delete(threadId);
643+
heldCompletions.delete(threadId);
607644
yield* Ref.update(publishedStateByThreadRef, (publishedStates) => {
608645
const nextPublishedStates = new Map(publishedStates);
609646
if (snapshot.state === null) {

0 commit comments

Comments
 (0)