Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 44 additions & 42 deletions apps/server/src/relay/AgentAwarenessRelay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ describe("AgentAwarenessRelay", () => {
}),
);

it.effect("still publishes a completion when the thread settles during the hold", () =>
it.effect("clears instead of alerting when the thread settles during the completion hold", () =>
Effect.gen(function* () {
const { relay, currentShell, publications } = yield* makeTestRelay();
yield* relay.publishThread(THREAD_ID);
Expand All @@ -808,14 +808,14 @@ describe("AgentAwarenessRelay", () => {
yield* relay.drain;
assert.deepEqual(
publications.map((publication) => publication.state?.phase ?? null),
["running", "completed"],
["running", null],
);

yield* TestClock.adjust("5 seconds");
yield* relay.drain;
assert.deepEqual(
publications.map((publication) => publication.state?.phase ?? null),
["running", "completed", null],
["running", null],
);
}),
);
Expand Down Expand Up @@ -888,43 +888,45 @@ describe("AgentAwarenessRelay", () => {
}),
);

it.effect("alerts a completion settled during its hold after an earlier tombstone", () =>
Effect.gen(function* () {
yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)));
const { relay, currentShell, publications } = yield* makeTestRelay();
const settledAt = DateTime.makeUnsafe(NOW);
yield* Ref.set(currentShell, {
...shell({ status: "completed" }),
settledOverride: "settled",
settledAt,
lastVisitedAt: null,
});
yield* relay.publishThread(THREAD_ID);
yield* TestClock.adjust("5 seconds");
yield* relay.drain;
it.effect(
"does not alert when a completion settles during its hold after an earlier tombstone",
() =>
Effect.gen(function* () {
yield* TestClock.setTime(DateTime.toEpochMillis(DateTime.makeUnsafe(NOW)));
const { relay, currentShell, publications } = yield* makeTestRelay();
const settledAt = DateTime.makeUnsafe(NOW);
yield* Ref.set(currentShell, {
...shell({ status: "completed" }),
settledOverride: "settled",
settledAt,
lastVisitedAt: null,
});
yield* relay.publishThread(THREAD_ID);
yield* TestClock.adjust("5 seconds");
yield* relay.drain;

const completed = shell({
status: "completed",
latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:00:06.000Z"),
});
yield* Ref.set(currentShell, completed);
yield* relay.publishThread(THREAD_ID);
yield* Ref.set(currentShell, {
...completed,
settledOverride: "settled",
settledAt,
lastVisitedAt: null,
});
yield* relay.publishThread(THREAD_ID);
yield* TestClock.adjust("5 seconds");
yield* relay.drain;
yield* TestClock.adjust("5 seconds");
yield* relay.drain;
assert.deepEqual(
publications.map((publication) => publication.state?.phase ?? null),
[null, "completed", null],
);
}),
const completed = shell({
status: "completed",
latestRunCompletedAt: DateTime.makeUnsafe("2026-09-04T12:00:06.000Z"),
});
yield* Ref.set(currentShell, completed);
yield* relay.publishThread(THREAD_ID);
yield* Ref.set(currentShell, {
...completed,
settledOverride: "settled",
settledAt,
lastVisitedAt: null,
});
yield* relay.publishThread(THREAD_ID);
yield* TestClock.adjust("5 seconds");
yield* relay.drain;
yield* TestClock.adjust("5 seconds");
yield* relay.drain;
assert.deepEqual(
publications.map((publication) => publication.state?.phase ?? null),
[null],
);
}),
);

it.effect("drops a held completion when a queued continuation hides the thread", () =>
Expand All @@ -944,7 +946,7 @@ describe("AgentAwarenessRelay", () => {
}),
);

it.effect("retries a failed completion even after the thread settles", () =>
it.effect("clears instead of retrying a failed completion after the thread settles", () =>
Effect.gen(function* () {
const { relay, currentShell, publications } = yield* makeTestRelay({
respond: (attempt) =>
Expand All @@ -968,12 +970,12 @@ describe("AgentAwarenessRelay", () => {
yield* relay.drain;
assert.deepEqual(
publications.map((publication) => publication.state?.phase ?? null),
["running", "completed", "completed"],
["running", "completed", null],
);

yield* TestClock.adjust("5 seconds");
yield* relay.drain;
assert.equal(publications.at(-1)?.state ?? null, null);
assert.equal(publications.length, 3);
}),
);

Expand Down
42 changes: 1 addition & 41 deletions apps/server/src/relay/AgentAwarenessRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,6 @@ export const make = Effect.gen(function* () {

// Confirm through the same worker so tombstones cannot race live updates; recovery clears the deadline.
const publishConfirmDeadlines = new Map<ThreadId, number>();
// Threads whose confirmation holds a completion, so a thread that settles during the hold still alerts.
const heldCompletions = new Set<ThreadId>();
let schedulePublishConfirm: (threadId: ThreadId) => Effect.Effect<void> = () => Effect.void;
const publishRetries = new Map<
ThreadId,
Expand All @@ -499,7 +497,6 @@ export const make = Effect.gen(function* () {
publishedRelayConfig = undefined;
cachedRelayClient = undefined;
publishConfirmDeadlines.clear();
heldCompletions.clear();
yield* Ref.set(publishedStateByThreadRef, new Map());
});

Expand Down Expand Up @@ -611,14 +608,9 @@ export const make = Effect.gen(function* () {
) {
if (Option.isNone(thread) || !terminalWorkSinceStart(thread.value, startedAt)) return;
}
if (
publishedStateByThread.get(threadId) === publishIdentity &&
// A held completion settled after an earlier clear still has to alert.
!(snapshot.state === null && heldCompletions.has(threadId))
) {
if (publishedStateByThread.get(threadId) === publishIdentity) {
// Clear moot deadlines so later transient nulls cannot skip their deferral window.
publishConfirmDeadlines.delete(threadId);
heldCompletions.delete(threadId);
yield* Effect.logDebug("agent activity publish skipped; projected state unchanged", {
environmentId,
threadId,
Expand All @@ -632,7 +624,6 @@ export const make = Effect.gen(function* () {
// alerts on every move into completed.
const requiresConfirmation = snapshot.state === null || snapshot.state?.phase === "completed";
if (requiresConfirmation) {
if (snapshot.state?.phase === "completed") heldCompletions.add(threadId);
const nowMs = (yield* DateTime.now).epochMilliseconds;
const deadline = publishConfirmDeadlines.get(threadId);
if (deadline === undefined) {
Expand All @@ -657,38 +648,8 @@ export const make = Effect.gen(function* () {
statePhase: snapshot.state?.phase ?? null,
shell: describeThreadShellForAwareness(thread),
});
// Settled during the hold, not resumed: alert the current completion, then clear the card
// on a new window. Only settlement may hide it; queued or silent runs also project to null.
const unsettled =
snapshot.state === null &&
heldCompletions.has(threadId) &&
Option.isSome(thread) &&
thread.value.settledOverride === "settled"
? resolveAgentAwarenessRelayPublishSnapshot({
environmentId,
thread: Option.some({ ...thread.value, settledOverride: null }),
project,
pendingSubagentRequest: null,
})
: null;
if (unsettled?.state?.phase === "completed") {
const completion = unsettled.state;
yield* publishState({
projectId: unsettled.projectId,
state: completion,
reason: unsettled.reason,
}).pipe(withRelayClientTracing);
heldCompletions.delete(threadId);
publishConfirmDeadlines.set(threadId, nowMs + 5_000);
yield* Ref.update(publishedStateByThreadRef, (publishedStates) =>
new Map(publishedStates).set(threadId, agentAwarenessPublishIdentity(completion)),
);
yield* schedulePublishConfirm(threadId);
return;
}
} else {
publishConfirmDeadlines.delete(threadId);
heldCompletions.delete(threadId);
}

if (snapshot.reason === "thread-not-found") {
Expand Down Expand Up @@ -721,7 +682,6 @@ export const make = Effect.gen(function* () {
childAlertByThread.set(threadId, { requestId: childRequestId, updatedAt: state.updatedAt });
}
publishConfirmDeadlines.delete(threadId);
heldCompletions.delete(threadId);
yield* Ref.update(publishedStateByThreadRef, (publishedStates) =>
new Map(publishedStates).set(threadId, publishIdentity),
);
Expand Down
Loading