Skip to content

Commit a639dfd

Browse files
committed
fix(threads): show named monitor conditions in work status
Monitor status used a generic Durable monitors placeholder, hiding the condition names and the number of outstanding monitors. Project the IDs and labels of active monitors and format their names across clients. Deduplicate tasks shared by live and shell projections, and avoid repeating a single task label in mobile accessibility text. Cover monitor completion and cancellation without changing active-turn presentation. Model: GPT-6-Astra (max) Harness: Codex
1 parent 13e2c6c commit a639dfd

5 files changed

Lines changed: 176 additions & 22 deletions

File tree

‎apps/mobile/src/features/threads/ThreadDetailScreen.tsx‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,12 @@ export const ThreadDetailScreen = memo(function ThreadDetailScreen(props: Thread
476476
return {
477477
kind: "waiting",
478478
label: pendingBackgroundWork.title,
479-
accessibilityLabel: `${pendingBackgroundWork.title}: ${pendingBackgroundWork.items
480-
.map((item) => item.label)
481-
.join(", ")}`,
479+
accessibilityLabel:
480+
pendingBackgroundWork.items.length === 1
481+
? pendingBackgroundWork.title
482+
: `${pendingBackgroundWork.title}: ${pendingBackgroundWork.items
483+
.map((item) => item.label)
484+
.join(", ")}`,
482485
};
483486
}
484487
return null;

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

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4259,6 +4259,129 @@ it.layer(TestLayer)("ProjectionStoreV2", (it) => {
42594259
}),
42604260
);
42614261

4262+
it.effect(
4263+
"projects each outstanding monitor by name across snapshots and live shell updates",
4264+
() =>
4265+
Effect.gen(function* () {
4266+
const store = yield* ProjectionStore.ProjectionStoreV2;
4267+
const sql = yield* SqlClient.SqlClient;
4268+
const threadId = ThreadId.make("thread:monitor-labels");
4269+
const now = yield* DateTime.now;
4270+
const nowIso = DateTime.formatIso(now);
4271+
yield* store.apply({
4272+
id: EventId.make("event:monitor-labels:thread"),
4273+
type: "thread.created",
4274+
threadId,
4275+
occurredAt: now,
4276+
payload: {
4277+
createdBy: "user",
4278+
creationSource: "web",
4279+
id: threadId,
4280+
projectId: ProjectId.make("project:monitor-labels"),
4281+
title: "Watch the build",
4282+
providerInstanceId,
4283+
modelSelection,
4284+
runtimeMode: "full-access",
4285+
interactionMode: "default",
4286+
branch: null,
4287+
worktreePath: null,
4288+
activeProviderThreadId: null,
4289+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId },
4290+
forkedFrom: null,
4291+
createdAt: now,
4292+
updatedAt: now,
4293+
archivedAt: null,
4294+
settledOverride: null,
4295+
settledAt: null,
4296+
lastVisitedAt: null,
4297+
deletedAt: null,
4298+
},
4299+
});
4300+
for (const [id, status, label] of [
4301+
["a-build", "active", 'Build "release" finishes'],
4302+
["b-checks", "triggered", "PR checks pass"],
4303+
["c-cancelled", "cancelled", "Old wait"],
4304+
["d-delivered", "delivered", "Finished wait"],
4305+
["e-failed", "failed", "Failed wait"],
4306+
]) {
4307+
yield* sql`
4308+
INSERT INTO thread_monitors (
4309+
monitor_id, thread_id, label, condition_type, continuation_mode,
4310+
status, created_at, updated_at
4311+
) VALUES (${id}, ${threadId}, ${label}, 'signal', 'record-only',
4312+
${status}, ${nowIso}, ${nowIso})
4313+
`;
4314+
}
4315+
const expected = [
4316+
{
4317+
taskId: "thread-monitor:a-build",
4318+
kind: "monitor" as const,
4319+
description: 'Build "release" finishes',
4320+
},
4321+
{
4322+
taskId: "thread-monitor:b-checks",
4323+
kind: "monitor" as const,
4324+
description: "PR checks pass",
4325+
},
4326+
];
4327+
const snapshot = (yield* store.getShellSnapshot()).threads.find((t) => t.id === threadId);
4328+
const live = yield* store.getThreadShell(threadId);
4329+
assert.deepEqual(snapshot?.pendingBackgroundTasks, expected);
4330+
assert.deepEqual(live?.pendingBackgroundTasks, expected);
4331+
assert.equal(live?.backgroundLiveness, "monitoring");
4332+
4333+
const runId = RunId.make("run:monitor-labels");
4334+
yield* store.apply({
4335+
id: EventId.make("event:monitor-labels:run"),
4336+
type: "run.created",
4337+
threadId,
4338+
runId,
4339+
driver,
4340+
providerInstanceId,
4341+
occurredAt: now,
4342+
payload: {
4343+
id: runId,
4344+
threadId,
4345+
ordinal: 1,
4346+
providerInstanceId,
4347+
modelSelection,
4348+
providerThreadId: null,
4349+
userMessageId: MessageId.make("message:monitor-labels"),
4350+
rootNodeId: null,
4351+
activeAttemptId: null,
4352+
status: "running",
4353+
requestedAt: now,
4354+
startedAt: now,
4355+
completedAt: null,
4356+
checkpointId: null,
4357+
contextHandoffId: null,
4358+
},
4359+
});
4360+
const working = yield* store.getThreadShell(threadId);
4361+
assert.deepEqual(working?.pendingBackgroundTasks, []);
4362+
assert.equal(working?.backgroundLiveness, "monitoring");
4363+
4364+
const activeRun = (yield* store.getThreadProjection(threadId)).runs[0]!;
4365+
yield* store.apply({
4366+
id: EventId.make("event:monitor-labels:completed"),
4367+
type: "run.updated",
4368+
threadId,
4369+
runId,
4370+
occurredAt: now,
4371+
payload: { ...activeRun, status: "completed", completedAt: now },
4372+
});
4373+
yield* sql`UPDATE thread_monitors SET status = 'cancelled'
4374+
WHERE monitor_id = 'a-build'`;
4375+
const remaining = yield* store.getThreadShell(threadId);
4376+
assert.deepEqual(remaining?.pendingBackgroundTasks, expected.slice(1));
4377+
yield* sql`UPDATE thread_monitors SET status = 'delivered'
4378+
WHERE monitor_id = 'b-checks'`;
4379+
const finished = yield* store.getThreadShell(threadId);
4380+
assert.deepEqual(finished?.pendingBackgroundTasks, []);
4381+
assert.equal(finished?.backgroundLiveness, null);
4382+
}),
4383+
);
4384+
42624385
it.effect("dates a completion released by held background work at the release", () =>
42634386
Effect.gen(function* () {
42644387
const store = yield* ProjectionStore.ProjectionStoreV2;

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
OrchestrationV2ContextTransferJson as OrchestrationV2ContextTransferJsonSchema,
4040
OrchestrationV2ConversationMessageJson as OrchestrationV2ConversationMessageJsonSchema,
4141
OrchestrationV2ExecutionNodeJson as OrchestrationV2ExecutionNodeJsonSchema,
42+
OrchestrationV2PendingBackgroundTask,
4243
OrchestrationV2PlanArtifact as OrchestrationV2PlanArtifactSchema,
4344
OrchestrationV2ProviderSessionJson as OrchestrationV2ProviderSessionJsonSchema,
4445
OrchestrationV2ProviderThreadJson as OrchestrationV2ProviderThreadJsonSchema,
@@ -889,7 +890,7 @@ type PayloadRow = {
889890
};
890891

891892
type ShellThreadRow = {
892-
readonly has_active_monitors: number;
893+
readonly active_monitors_json: string;
893894
readonly thread_id: string;
894895
readonly payload_json: string;
895896
readonly forked_from_run_source_thread_id: string | null;
@@ -994,6 +995,9 @@ const encodeContextTransferPayload = Schema.encodeEffect(
994995
const decodeThreadPayload = Schema.decodeUnknownEffect(
995996
Schema.fromJsonString(OrchestrationV2AppThreadJsonSchema),
996997
);
998+
const decodeMonitorTasks = Schema.decodeUnknownEffect(
999+
Schema.fromJsonString(Schema.Array(OrchestrationV2PendingBackgroundTask)),
1000+
);
9971001
const decodeRunPayload = Schema.decodeUnknownEffect(
9981002
Schema.fromJsonString(OrchestrationV2RunJsonSchema),
9991003
);
@@ -1620,17 +1624,7 @@ function shellFromState(input: {
16201624
latestVisibleMessage: null,
16211625
latestUserMessageAt: input.state.latestUserMessageAt,
16221626
hasActionableProposedPlan: input.state.hasActionableProposedPlan,
1623-
pendingBackgroundTasks:
1624-
input.state.backgroundLiveness === "monitoring" && input.state.activeRunId === null
1625-
? [
1626-
...(input.state.pendingBackgroundTasks ?? []),
1627-
{
1628-
taskId: `thread-monitors:${input.state.thread.id}`,
1629-
kind: "monitor",
1630-
description: "Durable monitors",
1631-
},
1632-
]
1633-
: input.state.pendingBackgroundTasks,
1627+
pendingBackgroundTasks: input.state.pendingBackgroundTasks,
16341628
backgroundLiveness: input.state.backgroundLiveness ?? null,
16351629
providerInstanceHistory: input.state.providerInstanceHistory,
16361630
itemCount: input.state.itemCount,
@@ -4768,10 +4762,15 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
47684762
) =>
47694763
sql<ShellThreadRow>`
47704764
SELECT
4771-
EXISTS (
4772-
SELECT 1 FROM thread_monitors m
4765+
(
4766+
SELECT json_group_array(json_object(
4767+
'taskId', 'thread-monitor:' || m.monitor_id,
4768+
'kind', 'monitor',
4769+
'description', m.label
4770+
) ORDER BY m.created_at, m.monitor_id)
4771+
FROM thread_monitors m
47734772
WHERE m.thread_id = t.thread_id AND m.status IN ('active', 'triggered')
4774-
) AS has_active_monitors,
4773+
) AS active_monitors_json,
47754774
t.thread_id,
47764775
t.payload_json,
47774776
CASE
@@ -5250,6 +5249,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52505249
: DateTime.makeUnsafe(row.blocking_run_completed_at);
52515250
}
52525251
}
5252+
const monitorTasks = yield* decodeMonitorTasks(row.active_monitors_json);
52535253
const pendingBackgroundTasks = [
52545254
...derivePendingBackgroundWork({
52555255
latestRun:
@@ -5265,6 +5265,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52655265
activeProviderThreadId: thread.activeProviderThreadId,
52665266
hasActiveRun: row.active_run_id !== null,
52675267
}),
5268+
...(row.active_run_id === null ? monitorTasks : []),
52685269
];
52695270
return {
52705271
thread,
@@ -5295,7 +5296,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52955296
? null
52965297
: DateTime.makeUnsafe(row.latest_user_message_at),
52975298
hasActionableProposedPlan: row.has_actionable_proposed_plan === 1,
5298-
backgroundLiveness: row.has_active_monitors === 1 ? "monitoring" : null,
5299+
backgroundLiveness: monitorTasks.length > 0 ? "monitoring" : null,
52995300
pendingBackgroundTasks,
53005301
providerInstanceHistory: providerInstanceHistoryForShell({
53015302
threadId: thread.id,

‎packages/client-runtime/src/state/threadExecution.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,11 +546,27 @@ describe("presentPendingBackgroundWork", () => {
546546
])?.title,
547547
).toBe("Waiting on subagent Review src/math.ts");
548548
expect(presentPendingBackgroundWork([{ taskId: "a", kind: "monitor" }])?.title).toBe(
549-
"Waiting on a monitor",
549+
"Monitoring",
550550
);
551551
expect(presentPendingBackgroundWork([])).toBeNull();
552552
});
553553

554+
it("names one monitor and counts multiple conditions without losing their labels", () => {
555+
const tasks = [
556+
{ taskId: "build", kind: "monitor" as const, description: "Build finishes" },
557+
{ taskId: "checks", kind: "monitor" as const, description: "PR checks pass" },
558+
];
559+
expect(presentPendingBackgroundWork(tasks.slice(0, 1))?.title).toBe(
560+
"Monitoring: Build finishes",
561+
);
562+
const presentation = presentPendingBackgroundWork([...tasks, ...tasks]);
563+
expect(presentation?.title).toBe("Monitoring 2 conditions");
564+
expect(presentation?.items.map((item) => item.label)).toEqual([
565+
"Build finishes",
566+
"PR checks pass",
567+
]);
568+
});
569+
554570
// A command left running, such as a dev server, does not wake the agent.
555571
it("says only commands are running, not waited on", () => {
556572
expect(

‎packages/client-runtime/src/state/threadExecution.ts‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,10 @@ export function presentPendingBackgroundWork(
324324
tasks: ReadonlyArray<OrchestrationV2PendingBackgroundTask>,
325325
): PendingBackgroundWorkPresentation | null {
326326
if (tasks.length === 0) return null;
327-
const waiting = backgroundWorkHoldsCompletion(tasks);
328-
const items = tasks
327+
// The shell and live provider projection can report the same background task.
328+
const uniqueTasks = [...new Map(tasks.map((task) => [task.taskId, task])).values()];
329+
const waiting = backgroundWorkHoldsCompletion(uniqueTasks);
330+
const items = uniqueTasks
329331
.map((task): PendingBackgroundWorkItem => {
330332
const description = task.description?.trim();
331333
const label =
@@ -348,6 +350,15 @@ export function presentPendingBackgroundWork(
348350
BACKGROUND_WORK_KINDS[left.kind].order - BACKGROUND_WORK_KINDS[right.kind].order,
349351
);
350352
const [only] = items;
353+
if (items.every((item) => item.kind === "monitor")) {
354+
const title =
355+
items.length === 1 && only !== undefined
356+
? only.label === "monitor"
357+
? "Monitoring"
358+
: `Monitoring: ${only.label}`
359+
: `Monitoring ${items.length} conditions`;
360+
return { title, items, waiting };
361+
}
351362
if (items.length === 1 && only !== undefined) {
352363
const noun = BACKGROUND_WORK_KINDS[only.kind].singular;
353364
const named = only.label !== noun;

0 commit comments

Comments
 (0)