Skip to content

Commit 4741dc5

Browse files
authored
fix(web): silence notifications when stopping a watch (pingdotgg#22615)
1 parent c22ace2 commit 4741dc5

6 files changed

Lines changed: 101 additions & 1 deletion

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ type ShellThreadRow = {
10581058
readonly latest_run_requested_at: string | null;
10591059
readonly latest_run_started_at: string | null;
10601060
readonly latest_run_completed_at: string | null;
1061+
readonly latest_run_stop_requested: number;
10611062
readonly active_run_id: string | null;
10621063
readonly active_run_status: string | null;
10631064
readonly activity_run_status: string | null;
@@ -1068,6 +1069,7 @@ type ShellThreadRow = {
10681069
readonly blocking_run_requested_at: string | null;
10691070
readonly blocking_run_started_at: string | null;
10701071
readonly blocking_run_completed_at: string | null;
1072+
readonly blocking_run_stop_requested: number;
10711073
readonly blocking_failure_payload_json: string | null;
10721074
readonly pending_request_payload_json: string | null;
10731075
readonly pending_secret_request_payload_json: string | null;
@@ -1598,6 +1600,11 @@ export function threadShellFromProjection(
15981600
latestRunRequestedAt: latestRun?.requestedAt ?? null,
15991601
latestRunStartedAt: latestRun?.startedAt ?? null,
16001602
latestRunCompletedAt: latestRun?.completedAt ?? null,
1603+
latestRunStopRequested:
1604+
latestRun != null &&
1605+
projection.turnItems.some(
1606+
(item) => item.runId === latestRun.id && item.type === "run_interrupt_request",
1607+
),
16011608
activeRunId: activeRun?.id ?? null,
16021609
blockedOnThreadWait: deriveBlockedOnThreadWait({ activeRun, turnItems: projection.turnItems }),
16031610
activityRunStatus: activityRun?.status ?? null,
@@ -1697,6 +1704,7 @@ type ShellThreadState = {
16971704
readonly latestRunRequestedAt: DateTime.Utc | null;
16981705
readonly latestRunStartedAt: DateTime.Utc | null;
16991706
readonly latestRunCompletedAt: DateTime.Utc | null;
1707+
readonly latestRunStopRequested: boolean;
17001708
readonly activeRunId: RunId | null;
17011709
readonly blockedOnThreadWait: boolean;
17021710
readonly activityRunStatus: ShellActivityRunStatus | null;
@@ -1850,6 +1858,7 @@ function shellFromState(input: {
18501858
latestRunRequestedAt: input.state.latestRunRequestedAt,
18511859
latestRunStartedAt: input.state.latestRunStartedAt,
18521860
latestRunCompletedAt: input.state.latestRunCompletedAt,
1861+
latestRunStopRequested: input.state.latestRunStopRequested,
18531862
activeRunId: input.state.activeRunId,
18541863
blockedOnThreadWait: input.state.blockedOnThreadWait,
18551864
activityRunStatus: input.state.activityRunStatus,
@@ -5638,6 +5647,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56385647
presented.requested_at AS latest_run_requested_at,
56395648
json_extract(presented.payload_json, '$.startedAt') AS latest_run_started_at,
56405649
presented.completed_at AS latest_run_completed_at,
5650+
EXISTS (
5651+
SELECT 1 FROM orchestration_v2_projection_turn_items stop_request
5652+
WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = presented.run_id
5653+
AND stop_request.type = 'run_interrupt_request'
5654+
) AS latest_run_stop_requested,
56415655
active.run_id AS active_run_id,
56425656
active.status AS active_run_status,
56435657
(
@@ -5688,6 +5702,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
56885702
blocked.requested_at AS blocking_run_requested_at,
56895703
json_extract(blocked.payload_json, '$.startedAt') AS blocking_run_started_at,
56905704
blocked.completed_at AS blocking_run_completed_at,
5705+
EXISTS (
5706+
SELECT 1 FROM orchestration_v2_projection_turn_items stop_request
5707+
WHERE stop_request.thread_id = t.thread_id AND stop_request.run_id = blocked.run_id
5708+
AND stop_request.type = 'run_interrupt_request'
5709+
) AS blocking_run_stop_requested,
56915710
(
56925711
SELECT item.payload_json
56935712
FROM orchestration_v2_projection_turn_items item
@@ -6160,6 +6179,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
61606179
row.latest_run_completed_at === null
61616180
? null
61626181
: DateTime.makeUnsafe(row.latest_run_completed_at);
6182+
let latestRunStopRequested = row.latest_run_stop_requested === 1;
61636183
if (row.blocking_run_id !== null && row.blocking_failure_payload_json !== null) {
61646184
const blockingFailure = yield* decodeTurnItemPayload(
61656185
row.blocking_failure_payload_json,
@@ -6172,6 +6192,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
61726192
if (blocksQueue && blockingFailure !== null) {
61736193
terminalFailureItem = blockingFailure;
61746194
latestRunId = RunId.make(row.blocking_run_id);
6195+
latestRunStopRequested = row.blocking_run_stop_requested === 1;
61756196
latestRunStatus = "failed";
61766197
latestRunRequestedAt =
61776198
row.blocking_run_requested_at === null
@@ -6211,6 +6232,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
62116232
latestRunRequestedAt,
62126233
latestRunStartedAt,
62136234
latestRunCompletedAt,
6235+
latestRunStopRequested,
62146236
activeRunId: row.active_run_id === null ? null : RunId.make(row.active_run_id),
62156237
blockedOnThreadWait: deriveBlockedOnThreadWait({
62166238
activeRun:

‎apps/server/src/orchestration-v2/ThreadStop.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ProviderDriverKind,
1111
ProviderInstanceId,
1212
ProviderSessionId,
13+
ProviderThreadId,
1314
ProviderTurnId,
1415
RuntimeRequestId,
1516
ScheduledTaskId,
@@ -726,6 +727,62 @@ it.effect(
726727
}).pipe(Effect.provide(layerTest)),
727728
);
728729

730+
it.effect.each([false, true])(
731+
"a completed watch records whether it ended through Stop (%s)",
732+
(stopped) =>
733+
Effect.gen(function* () {
734+
const orchestrator = yield* Orchestrator.OrchestratorV2;
735+
const projections = yield* ProjectionStore.ProjectionStoreV2;
736+
const threadId = ThreadId.make(`thread:watch-completion:${stopped}`);
737+
yield* createWatchingThread(threadId, 7);
738+
yield* send(threadId, "watch", "start_immediately");
739+
const run = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!;
740+
const now = yield* DateTime.now;
741+
const completedRun = {
742+
...run,
743+
providerThreadId: run.providerThreadId ?? ProviderThreadId.make("provider-thread:watch"),
744+
status: "completed" as const,
745+
startedAt: now,
746+
completedAt: now,
747+
};
748+
yield* projections.apply({
749+
id: EventId.make("event:watch-completed"),
750+
type: "run.updated",
751+
threadId,
752+
occurredAt: now,
753+
payload: completedRun,
754+
});
755+
assert.isFalse((yield* projections.getThreadShell(threadId))?.latestRunStopRequested);
756+
757+
yield* orchestrator.dispatch(
758+
stopped
759+
? { type: "thread.stop", commandId: CommandId.make("stop-watch"), threadId }
760+
: {
761+
type: "thread.pull-request.watch",
762+
commandId: CommandId.make("finish-watch"),
763+
threadId,
764+
...pullRequest(7),
765+
watching: false,
766+
},
767+
);
768+
const shell = yield* projections.getThreadShell(threadId);
769+
const snapshot = (yield* projections.getShellSnapshot()).threads.find(
770+
(thread) => thread.id === threadId,
771+
);
772+
const memoryShell = ProjectionStore.threadShellFromProjection(
773+
yield* orchestrator.getThreadProjection(threadId),
774+
);
775+
for (const result of [shell, snapshot, memoryShell]) {
776+
assert.equal(result?.status, "completed");
777+
assert.isEmpty(result?.pendingBackgroundTasks ?? []);
778+
assert.equal(result?.latestRunStopRequested, stopped);
779+
}
780+
781+
yield* send(threadId, "next", "start_immediately");
782+
assert.isFalse((yield* projections.getThreadShell(threadId))?.latestRunStopRequested);
783+
}).pipe(Effect.provide(layerTest)),
784+
);
785+
729786
it.effect("a run Stop reached cannot delegate or start a watch, even after it ends", () =>
730787
Effect.gen(function* () {
731788
const orchestrator = yield* Orchestrator.OrchestratorV2;

‎apps/web/src/components/ThreadNotificationCoordinator.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ function EnvironmentNotifications({
154154
const kind =
155155
attention && attention !== prior.attention
156156
? "input"
157-
: completion !== null && (prior.completion === null || completion > prior.completion)
157+
: !rawThread.latestRunStopRequested &&
158+
completion !== null &&
159+
(prior.completion === null || completion > prior.completion)
158160
? "completion"
159161
: null;
160162
if (!kind) continue;

‎packages/contracts/src/orchestrationV2.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,7 @@ export const OrchestrationV2ThreadShell = Schema.Struct({
18251825
latestRunRequestedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
18261826
latestRunStartedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
18271827
latestRunCompletedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
1828+
latestRunStopRequested: Schema.optional(Schema.Boolean),
18281829
activeRunId: Schema.NullOr(RunId),
18291830
activityRunStartedAt: Schema.optional(Schema.NullOr(Schema.DateTimeUtc)),
18301831
activityRunStatus: Schema.optional(

‎packages/shared/src/agentAwareness.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ describe("projectThreadAwarenessV2", () => {
2424
Pick<
2525
OrchestrationV2ThreadShell,
2626
| "activityRunStatus"
27+
| "latestRunStopRequested"
2728
| "status"
2829
| "pendingBackgroundTasks"
2930
| "pendingRuntimeRequest"
@@ -47,6 +48,21 @@ describe("projectThreadAwarenessV2", () => {
4748
...overrides,
4849
});
4950

51+
it("does not announce completion after a manual stop", () => {
52+
const input = {
53+
environmentId: "env-1" as EnvironmentId,
54+
project,
55+
thread: v2Thread({ status: "completed" }),
56+
};
57+
expect(projectThreadAwarenessV2(input)?.phase).toBe("completed");
58+
expect(
59+
projectThreadAwarenessV2({
60+
...input,
61+
thread: { ...input.thread, latestRunStopRequested: true },
62+
}),
63+
).toBeNull();
64+
});
65+
5066
it.each(["running", "completed", "failed"] as const)(
5167
"does not publish %s subagent activity",
5268
(status) => {

‎packages/shared/src/agentAwareness.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export interface ProjectThreadAwarenessV2Input {
4545
| "activityRunStatus"
4646
| "id"
4747
| "lineage"
48+
| "latestRunStopRequested"
4849
| "modelSelection"
4950
| "pendingBackgroundTasks"
5051
| "pendingRuntimeRequest"
@@ -64,6 +65,7 @@ export function projectThreadAwarenessV2(
6465
if (phase === null) {
6566
return null;
6667
}
68+
if (phase === "completed" && thread.latestRunStopRequested === true) return null;
6769
// Resolve the phase first so live or waiting work stays visible after settlement.
6870
if (
6971
thread.settledOverride === "settled" &&

0 commit comments

Comments
 (0)