Skip to content
TypeScript

fix(web): silence notifications when stopping a watch (#22615)

Yash Singh4741dc563f5d
diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts
index 89731e10f2a..3fb7c152578 100644
--- a/apps/server/src/orchestration-v2/ProjectionStore.ts
+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts
@@ -1058,6 +1058,7 @@ type ShellThreadRow = {
readonly latest_run_requested_at: string | null;
readonly latest_run_started_at: string | null;
readonly latest_run_completed_at: string | null;
+ readonly latest_run_stop_requested: number;
readonly active_run_id: string | null;
readonly active_run_status: string | null;
readonly activity_run_status: string | null;
@@ -1068,6 +1069,7 @@ type ShellThreadRow = {
readonly blocking_run_requested_at: string | null;
readonly blocking_run_started_at: string | null;
readonly blocking_run_completed_at: string | null;
+ readonly blocking_run_stop_requested: number;
readonly blocking_failure_payload_json: string | null;
readonly pending_request_payload_json: string | null;
readonly pending_secret_request_payload_json: string | null;
@@ -1598,6 +1600,11 @@ export function threadShellFromProjection(
latestRunRequestedAt: latestRun?.requestedAt ?? null,
latestRunStartedAt: latestRun?.startedAt ?? null,
latestRunCompletedAt: latestRun?.completedAt ?? null,
+ latestRunStopRequested:
+ latestRun != null &&
+ projection.turnItems.some(
+ (item) => item.runId === latestRun.id && item.type === "run_interrupt_request",
+ ),
activeRunId: activeRun?.id ?? null,
blockedOnThreadWait: deriveBlockedOnThreadWait({ activeRun, turnItems: projection.turnItems }),
activityRunStatus: activityRun?.status ?? null,
@@ -1697,6 +1704,7 @@ type ShellThreadState = {
readonly latestRunRequestedAt: DateTime.Utc | null;
readonly latestRunStartedAt: DateTime.Utc | null;
readonly latestRunCompletedAt: DateTime.Utc | null;
+ readonly latestRunStopRequested: boolean;
readonly activeRunId: RunId | null;
readonly blockedOnThreadWait: boolean;
readonly activityRunStatus: ShellActivityRunStatus | null;
@@ -1850,6 +1858,7 @@ function shellFromState(input: {
latestRunRequestedAt: input.state.latestRunRequestedAt,
latestRunStartedAt: input.state.latestRunStartedAt,
latestRunCompletedAt: input.state.latestRunCompletedAt,
+ latestRunStopRequested: input.state.latestRunStopRequested,
activeRunId: input.state.activeRunId,
blockedOnThreadWait: input.state.blockedOnThreadWait,
activityRunStatus: input.state.activityRunStatus,
@@ -5638,6 +5647,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
presented.requested_at AS latest_run_requested_at,
json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at,
presented.completed_at AS latest_run_completed_at,
+ EXISTS (
+ SELECT 1 FROM orchestration_v2_projection_turn_items stop_request
+ WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id
+ AND stop_request.type = 'run_interrupt_request'
+ ) AS latest_run_stop_requested,
active.run_id AS active_run_id,
active.status AS active_run_status,
(
@@ -5688,6 +5702,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
blocked.requested_at AS blocking_run_requested_at,
json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at,
blocked.completed_at AS blocking_run_completed_at,
+ EXISTS (
+ SELECT 1 FROM orchestration_v2_projection_turn_items stop_request
+ WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id
+ AND stop_request.type = 'run_interrupt_request'
+ ) AS blocking_run_stop_requested,
(
SELECT item.payload_json
FROM orchestration_v2_projection_turn_items item
@@ -6160,6 +6179,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
row.latest_run_completed_at === null
? null
: DateTime.makeUnsafe(row.latest_run_completed_at);
+ let latestRunStopRequested = row.latest_run_stop_requested === 1;
if (row.blocking_run_id !== null && row.blocking_failure_payload_json !== null) {
const blockingFailure = yield* decodeTurnItemPayload(
row.blocking_failure_payload_json,
@@ -6172,6 +6192,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
if (blocksQueue && blockingFailure !== null) {
terminalFailureItem = blockingFailure;
latestRunId = RunId.make(row.blocking_run_id);
+ latestRunStopRequested = row.blocking_run_stop_requested === 1;
latestRunStatus = "failed";
latestRunRequestedAt =
row.blocking_run_requested_at === null
@@ -6211,6 +6232,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
latestRunRequestedAt,
latestRunStartedAt,
latestRunCompletedAt,
+ latestRunStopRequested,
activeRunId: row.active_run_id === null ? null : RunId.make(row.active_run_id),
blockedOnThreadWait: deriveBlockedOnThreadWait({
activeRun:
diff --git a/apps/server/src/orchestration-v2/ThreadStop.test.ts b/apps/server/src/orchestration-v2/ThreadStop.test.ts
index 74da4d674d7..e041b5628bb 100644
--- a/apps/server/src/orchestration-v2/ThreadStop.test.ts
+++ b/apps/server/src/orchestration-v2/ThreadStop.test.ts
@@ -10,6 +10,7 @@ import {
ProviderDriverKind,
ProviderInstanceId,
ProviderSessionId,
+ ProviderThreadId,
ProviderTurnId,
RuntimeRequestId,
ScheduledTaskId,
@@ -726,6 +727,62 @@ it.effect(
}).pipe(Effect.provide(layerTest)),
);
+it.effect.each([false, true])(
+ "a completed watch records whether it ended through Stop (%s)",
+ (stopped) =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const projections = yield* ProjectionStore.ProjectionStoreV2;
+ const threadId = ThreadId.make(`thread:watch-completion:${stopped}`);
+ yield* createWatchingThread(threadId, 7);
+ yield* send(threadId, "watch", "start_immediately");
+ const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
+ const now = yield* DateTime.now;
+ const completedRun = {
+ ...run,
+ providerThreadId: run.providerThreadId ?? ProviderThreadId.make("provider-thread:watch"),
+ status: "completed" as const,
+ startedAt: now,
+ completedAt: now,
+ };
+ yield* projections.apply({
+ id: EventId.make("event:watch-completed"),
+ type: "run.updated",
+ threadId,
+ occurredAt: now,
+ payload: completedRun,
+ });
+ assert.isFalse((yield* projections.getThreadShell(threadId))?.latestRunStopRequested);
+
+ yield* orchestrator.dispatch(
+ stopped
+ ? { type: "thread.stop", commandId: CommandId.make("stop-watch"), threadId }
+ : {
+ type: "thread.pull-request.watch",
+ commandId: CommandId.make("finish-watch"),
+ threadId,
+ ...pullRequest(7),
+ watching: false,
+ },
+ );
+ const shell = yield* projections.getThreadShell(threadId);
+ const snapshot = (yield* projections.getShellSnapshot()).threads.find(
+ (thread) => thread.id === threadId,
+ );
+ const memoryShell = ProjectionStore.threadShellFromProjection(
+ yield* orchestrator.getThreadProjection(threadId),
+ );
+ for (const result of [shell, snapshot, memoryShell]) {
+ assert.equal(result?.status, "completed");
+ assert.isEmpty(result?.pendingBackgroundTasks ?? []);
+ assert.equal(result?.latestRunStopRequested, stopped);
+ }
+
+ yield* send(threadId, "next", "start_immediately");
+ assert.isFalse((yield* projections.getThreadShell(threadId))?.latestRunStopRequested);
+ }).pipe(Effect.provide(layerTest)),
+);
+
it.effect("a run Stop reached cannot delegate or start a watch, even after it ends", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;
diff --git a/apps/web/src/components/ThreadNotificationCoordinator.tsx b/apps/web/src/components/ThreadNotificationCoordinator.tsx
index 9e4e7d79150..c9355fc7f82 100644
--- a/apps/web/src/components/ThreadNotificationCoordinator.tsx
+++ b/apps/web/src/components/ThreadNotificationCoordinator.tsx
@@ -154,7 +154,9 @@ function EnvironmentNotifications({
const kind =
attention && attention !== prior.attention
? "input"
- : completion !== null && (prior.completion === null || completion > prior.completion)
+ : !rawThread.latestRunStopRequested &&
+ completion !== null &&
+ (prior.completion === null || completion > prior.completion)
? "completion"
: null;
if (!kind) continue;
diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts
index a60f7b7c513..eecc7110a1f 100644
--- a/packages/contracts/src/orchestrationV2.ts
+++ b/packages/contracts/src/orchestrationV2.ts
@@ -1825,6 +1825,7 @@ export const OrchestrationV2ThreadShell = Schema.Struct({
latestRunRequestedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
latestRunStartedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
latestRunCompletedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
+ latestRunStopRequested: Schema.optional(Schema.Boolean),
activeRunId: Schema.NullOr(RunId),
activityRunStartedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
activityRunStatus: Schema.optional(
diff --git a/packages/shared/src/agentAwareness.test.ts b/packages/shared/src/agentAwareness.test.ts
index 7216f1defb3..8e60d263ff6 100644
--- a/packages/shared/src/agentAwareness.test.ts
+++ b/packages/shared/src/agentAwareness.test.ts
@@ -24,6 +24,7 @@ describe("projectThreadAwarenessV2", () => {
Pick<
OrchestrationV2ThreadShell,
| "activityRunStatus"
+ | "latestRunStopRequested"
| "status"
| "pendingBackgroundTasks"
| "pendingRuntimeRequest"
@@ -47,6 +48,21 @@ describe("projectThreadAwarenessV2", () => {
...overrides,
});
+ it("does not announce completion after a manual stop", () => {
+ const input = {
+ environmentId: "env-1" as EnvironmentId,
+ project,
+ thread: v2Thread({ status: "completed" }),
+ };
+ expect(projectThreadAwarenessV2(input)?.phase).toBe("completed");
+ expect(
+ projectThreadAwarenessV2({
+ ...input,
+ thread: { ...input.thread, latestRunStopRequested: true },
+ }),
+ ).toBeNull();
+ });
+
it.each(["running", "completed", "failed"] as const)(
"does not publish %s subagent activity",
(status) => {
diff --git a/packages/shared/src/agentAwareness.ts b/packages/shared/src/agentAwareness.ts
index 1c22deb9ff5..df912b4bb0f 100644
--- a/packages/shared/src/agentAwareness.ts
+++ b/packages/shared/src/agentAwareness.ts
@@ -45,6 +45,7 @@ export interface ProjectThreadAwarenessV2Input {
| "activityRunStatus"
| "id"
| "lineage"
+ | "latestRunStopRequested"
| "modelSelection"
| "pendingBackgroundTasks"
| "pendingRuntimeRequest"
@@ -64,6 +65,7 @@ export function projectThreadAwarenessV2(
if (phase === null) {
return null;
}
+ if (phase === "completed" && thread.latestRunStopRequested === true) return null;
// Resolve the phase first so live or waiting work stays visible after settlement.
if (
thread.settledOverride === "settled" &&