Skip to content

Commit b203eb3

Browse files
t3dotggclaude
andcommitted
fix(server): a completion that settles during the hold still alerts
Auto-settle on merge can settle a thread within milliseconds of its run ending, so the 5s re-check saw a hidden thread and dropped the held completion. A held completion is now dropped only when the thread is live again. When the thread settled instead, the completion publishes first and the card clears on a new tombstone window. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 999929a commit b203eb3

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,36 @@ describe("AgentAwarenessRelay", () => {
448448
}),
449449
);
450450

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+
451481
it.effect("keeps settled live work until its awaited monitor finishes", () =>
452482
Effect.gen(function* () {
453483
const { relay, currentShell, publications } = yield* makeTestRelay();

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ 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+
// The completion a confirmation holds, so a thread that settles during the hold still alerts.
415+
const heldCompletions = new Map<
416+
ThreadId,
417+
ReturnType<typeof resolveAgentAwarenessRelayPublishSnapshot>
418+
>();
414419
let schedulePublishConfirm: (threadId: ThreadId) => Effect.Effect<void> = () => Effect.void;
415420
const publishRetries = new Map<
416421
ThreadId,
@@ -542,6 +547,7 @@ export const make = Effect.gen(function* () {
542547
if (publishedStateByThread.get(threadId) === publishIdentity) {
543548
// Clear moot deadlines so later transient nulls cannot skip their deferral window.
544549
publishConfirmDeadlines.delete(threadId);
550+
heldCompletions.delete(threadId);
545551
yield* Effect.logDebug("agent activity publish skipped; projected state unchanged", {
546552
environmentId,
547553
threadId,
@@ -558,6 +564,7 @@ export const make = Effect.gen(function* () {
558564
publishedStateByThread.get(threadId) !== agentAwarenessPublishIdentity(null)) ||
559565
snapshot.state?.phase === "completed";
560566
if (requiresConfirmation) {
567+
if (snapshot.state?.phase === "completed") heldCompletions.set(threadId, snapshot);
561568
const nowMs = (yield* DateTime.now).epochMilliseconds;
562569
const deadline = publishConfirmDeadlines.get(threadId);
563570
if (deadline === undefined) {
@@ -582,9 +589,26 @@ export const make = Effect.gen(function* () {
582589
statePhase: snapshot.state?.phase ?? null,
583590
shell: describeThreadShellForAwareness(thread),
584591
});
592+
const held = heldCompletions.get(threadId);
593+
// Settled during the hold, not resumed: alert the completion, then clear the card on a new window.
594+
if (snapshot.state === null && snapshot.reason === "snapshot" && held?.state != null) {
595+
yield* publishState({
596+
projectId: held.projectId,
597+
state: held.state,
598+
reason: held.reason,
599+
}).pipe(withRelayClientTracing);
600+
heldCompletions.delete(threadId);
601+
publishConfirmDeadlines.set(threadId, nowMs + 5_000);
602+
yield* Ref.update(publishedStateByThreadRef, (publishedStates) =>
603+
new Map(publishedStates).set(threadId, agentAwarenessPublishIdentity(held.state)),
604+
);
605+
yield* schedulePublishConfirm(threadId);
606+
return;
607+
}
585608
} else {
586609
publishConfirmDeadlines.delete(threadId);
587610
}
611+
heldCompletions.delete(threadId);
588612

589613
if (snapshot.reason === "thread-not-found") {
590614
yield* Effect.logDebug("publishing agent activity tombstone; thread not found", {

0 commit comments

Comments
 (0)