Skip to content

Commit ebfd183

Browse files
PR Batch Testerclaude
andcommitted
fix(web): background announcements follow each run's ending and stay quiet while snoozed
Interrupted runs now say Stopped instead of Done, marking a thread unread no longer replays its ending, and snoozed threads stay quiet until their timer, like their sidebar row. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent df01193 commit ebfd183

4 files changed

Lines changed: 153 additions & 75 deletions

File tree

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
import { useAtomValue } from "@effect/atom-react";
22
import { scopeThreadRef, scopedThreadKey } from "@t3tools/client-runtime/environment";
3+
import { effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
34
import { useEffect, useRef } from "react";
45

56
import { useComposerDraftStore } from "../composerDraftStore";
67
import { useThreadRouteTarget } from "../hooks/useThreadRouteTarget";
78
import { useQuietThreadShells } from "../state/entities";
89
import { liveShellEnvironmentIdsAtom } from "../state/shell";
910
import { resolveActiveThreadRouteRef } from "../threadRoutes";
10-
import { useUiStateStore } from "../uiStateStore";
1111
import {
12-
type BackgroundThreadEntry,
1312
type BackgroundThreadMemory,
13+
type BackgroundThreadSnapshot,
1414
nextBackgroundThreadAnnouncement,
15-
resolveBackgroundThreadStatus,
15+
resolveBackgroundThreadRequest,
1616
} from "./backgroundThreadAnnouncement";
17-
import { resolveThreadLastVisitedAt } from "./Sidebar.logic";
1817

1918
/**
2019
* Speaks status changes for threads other than the open one. It lives outside
@@ -23,7 +22,6 @@ import { resolveThreadLastVisitedAt } from "./Sidebar.logic";
2322
export function BackgroundThreadAnnouncer() {
2423
const threads = useQuietThreadShells();
2524
const liveEnvironmentIds = useAtomValue(liveShellEnvironmentIdsAtom);
26-
const localLastVisitedAtById = useUiStateStore((state) => state.threadLastVisitedAtById);
2725
const routeTarget = useThreadRouteTarget();
2826
const routeDraftThread = useComposerDraftStore((store) =>
2927
routeTarget?.kind === "draft" ? store.getDraftSession(routeTarget.draftId) : null,
@@ -34,25 +32,25 @@ export function BackgroundThreadAnnouncer() {
3432
const memoryRef = useRef<BackgroundThreadMemory>(new Map());
3533

3634
useEffect(() => {
37-
const entries: BackgroundThreadEntry[] = [];
35+
const now = new Date().toISOString();
36+
const snapshots: BackgroundThreadSnapshot[] = [];
3837
for (const thread of threads) {
3938
// Subagents have no row of their own; their requests surface on the parent.
4039
if (thread.archivedAt !== null || thread.lineage.relationshipToParent === "subagent")
4140
continue;
4241
if (!liveEnvironmentIds.has(thread.environmentId)) continue;
43-
const key = scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id));
44-
entries.push({
45-
key,
42+
snapshots.push({
43+
key: scopedThreadKey(scopeThreadRef(thread.environmentId, thread.id)),
4644
title: thread.title,
47-
status: resolveBackgroundThreadStatus(
48-
thread,
49-
resolveThreadLastVisitedAt(thread.lastVisitedAt, localLastVisitedAtById[key]),
50-
),
45+
request: resolveBackgroundThreadRequest(thread),
46+
runId: thread.latestRun?.runId ?? null,
47+
runStatus: thread.latestRun?.status ?? null,
48+
snoozed: effectiveSnoozed(thread, { now }),
5149
});
5250
}
5351
const { memory, announcement } = nextBackgroundThreadAnnouncement(
5452
memoryRef.current,
55-
entries,
53+
snapshots,
5654
openThreadKey,
5755
);
5856
memoryRef.current = memory;
@@ -61,7 +59,7 @@ export function BackgroundThreadAnnouncer() {
6159
// Screen readers ignore a write that leaves the text unchanged, so a repeat
6260
// gets a trailing non-breaking space to count as new.
6361
region.textContent = region.textContent === announcement ? `${announcement} ` : announcement;
64-
}, [threads, liveEnvironmentIds, localLastVisitedAtById, openThreadKey]);
62+
}, [threads, liveEnvironmentIds, openThreadKey]);
6563

6664
return <div ref={regionRef} role="status" aria-live="polite" className="sr-only" />;
6765
}
Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
1+
import { RunId } from "@t3tools/contracts";
12
import { describe, expect, it } from "vite-plus/test";
23

34
import {
4-
type BackgroundThreadEntry,
55
type BackgroundThreadMemory,
6+
type BackgroundThreadSnapshot,
67
nextBackgroundThreadAnnouncement,
8+
resolveBackgroundThreadRequest,
79
} from "./backgroundThreadAnnouncement";
810

9-
const login = (status: BackgroundThreadEntry["status"]): BackgroundThreadEntry => ({
10-
key: "env:login",
11-
title: "Fix login redirect",
12-
status,
13-
});
14-
const docs = (status: BackgroundThreadEntry["status"]): BackgroundThreadEntry => ({
15-
key: "env:docs",
16-
title: "Update docs",
17-
status,
18-
});
11+
const firstRun = RunId.make("run-1");
12+
const secondRun = RunId.make("run-2");
13+
14+
function thread(
15+
key: string,
16+
title: string,
17+
state: Partial<Omit<BackgroundThreadSnapshot, "key" | "title">> = {},
18+
): BackgroundThreadSnapshot {
19+
return {
20+
key,
21+
title,
22+
request: null,
23+
runId: firstRun,
24+
runStatus: "running",
25+
snoozed: false,
26+
...state,
27+
};
28+
}
29+
const login = (state?: Partial<BackgroundThreadSnapshot>) =>
30+
thread("env:login", "Fix login redirect", state);
31+
const docs = (state?: Partial<BackgroundThreadSnapshot>) =>
32+
thread("env:docs", "Update docs", state);
1933

2034
function speak(
2135
frames: ReadonlyArray<{
22-
readonly threads: ReadonlyArray<BackgroundThreadEntry>;
36+
readonly threads: ReadonlyArray<BackgroundThreadSnapshot>;
2337
readonly open?: string;
2438
}>,
2539
) {
@@ -37,51 +51,98 @@ describe("nextBackgroundThreadAnnouncement", () => {
3751
it("names the thread with the same words the open thread uses", () => {
3852
expect(
3953
speak([
40-
{ threads: [login(null)] },
41-
{ threads: [login("approval")] },
42-
{ threads: [login(null)] },
43-
{ threads: [login("input")] },
44-
{ threads: [login("done")] },
45-
{ threads: [login(null)] },
46-
{ threads: [login("failed")] },
54+
{ threads: [login()] },
55+
{ threads: [login({ request: "approval" })] },
56+
{ threads: [login()] },
57+
{ threads: [login({ request: "input" })] },
58+
{ threads: [login({ runStatus: "completed" })] },
59+
{ threads: [login({ runId: secondRun, runStatus: "failed" })] },
60+
{ threads: [login({ runId: RunId.make("run-3"), runStatus: "interrupted" })] },
4761
]),
4862
).toEqual([
4963
"Needs approval: Fix login redirect",
5064
"Needs your input: Fix login redirect",
5165
"Done: Fix login redirect",
5266
"Failed: Fix login redirect",
67+
"Stopped: Fix login redirect",
5368
]);
5469
});
5570

71+
it("announces each run's ending once", () => {
72+
expect(
73+
speak([
74+
{ threads: [login()] },
75+
{ threads: [login({ runStatus: "completed" })] },
76+
// Marking the thread unread or renaming it changes nothing about the run.
77+
{ threads: [login({ runStatus: "completed", title: "Fix the login redirect" })] },
78+
// The next run can finish between two updates, so it never looks busy here.
79+
{ threads: [login({ runId: secondRun, runStatus: "completed" })] },
80+
{ threads: [login({ runId: RunId.make("run-3"), runStatus: "rolled_back" })] },
81+
]),
82+
).toEqual(["Done: Fix login redirect", "Done: Fix login redirect"]);
83+
});
84+
5685
it("stays quiet for threads it sees for the first time, like on launch or reconnect", () => {
5786
expect(
5887
speak([
59-
{ threads: [login("done"), docs("approval")] },
60-
{ threads: [login("done"), docs("approval")] },
88+
{ threads: [login({ runStatus: "completed" }), docs({ request: "approval" })] },
89+
{ threads: [login({ runStatus: "completed" }), docs({ request: "approval" })] },
6190
// The environment drops out while it resyncs, then comes back changed.
6291
{ threads: [] },
63-
{ threads: [login("failed"), docs("done")] },
92+
{
93+
threads: [
94+
login({ runId: secondRun, runStatus: "failed" }),
95+
docs({ runStatus: "completed" }),
96+
],
97+
},
6498
]),
6599
).toEqual([]);
66100
});
67101

68102
it("leaves the open thread to its own announcer", () => {
69103
expect(
70104
speak([
71-
{ threads: [login(null), docs(null)], open: "env:login" },
72-
{ threads: [login("done"), docs(null)], open: "env:login" },
73-
// Switching away from a thread doesn't replay its status.
74-
{ threads: [login("done"), docs(null)], open: "env:docs" },
105+
{ threads: [login(), docs()], open: "env:login" },
106+
{ threads: [login({ runStatus: "completed" }), docs()], open: "env:login" },
107+
// Switching away from a thread doesn't replay its ending.
108+
{ threads: [login({ runStatus: "completed" }), docs()], open: "env:docs" },
109+
{
110+
threads: [login({ runStatus: "completed" }), docs({ request: "input" })],
111+
open: "env:login",
112+
},
113+
]),
114+
).toEqual(["Needs your input: Update docs"]);
115+
});
116+
117+
it("keeps a snoozed thread quiet, even after it wakes", () => {
118+
expect(
119+
speak([
120+
{ threads: [login({ snoozed: true })] },
121+
{ threads: [login({ snoozed: true, runStatus: "completed" })] },
122+
{ threads: [login({ runStatus: "completed" })] },
75123
]),
76124
).toEqual([]);
77125
});
78126

79127
it("speaks threads that change together in one announcement", () => {
80128
expect(
81129
speak([
82-
{ threads: [login(null), docs(null)] },
83-
{ threads: [login("done"), docs("approval")] },
130+
{ threads: [login(), docs()] },
131+
{ threads: [login({ runStatus: "completed" }), docs({ request: "approval" })] },
84132
]),
85133
).toEqual(["Done: Fix login redirect. Needs approval: Update docs"]);
86134
});
87135
});
136+
137+
describe("resolveBackgroundThreadRequest", () => {
138+
it("raises a subagent's request on the thread that owns its row", () => {
139+
const quiet = { hasPendingApprovals: false, hasPendingUserInput: false };
140+
expect(resolveBackgroundThreadRequest({ ...quiet, pendingSubagentRequest: "approval" })).toBe(
141+
"approval",
142+
);
143+
expect(resolveBackgroundThreadRequest({ ...quiet, pendingSubagentRequest: "user_input" })).toBe(
144+
"input",
145+
);
146+
expect(resolveBackgroundThreadRequest({ ...quiet, pendingSubagentRequest: null })).toBeNull();
147+
});
148+
});

‎apps/web/src/components/backgroundThreadAnnouncement.ts‎

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,69 @@
1+
import type { OrchestrationV2RunStatus, RunId } from "@t3tools/contracts";
2+
13
import type { SidebarThreadSummary } from "../types";
2-
import { THREAD_STATUS_TEXT } from "./chat/threadActivityAnnouncement";
3-
import { hasUnseenCompletion, resolveSidebarThreadStatus } from "./Sidebar.logic";
4+
import { runOutcomeText, THREAD_STATUS_TEXT } from "./chat/threadActivityAnnouncement";
45

5-
export type BackgroundThreadStatus = keyof typeof THREAD_STATUS_TEXT;
6+
export interface BackgroundThreadSnapshot {
7+
readonly key: string;
8+
readonly title: string;
9+
readonly request: "approval" | "input" | null;
10+
readonly runId: RunId | null;
11+
readonly runStatus: OrchestrationV2RunStatus | null;
12+
/** A snoozed thread stays hidden until its timer, so it stays quiet too. */
13+
readonly snoozed: boolean;
14+
}
615

7-
/** The statuses worth interrupting someone for: the ones a sidebar row flags. */
8-
export function resolveBackgroundThreadStatus(
9-
thread: SidebarThreadSummary,
10-
lastVisitedAt: string | undefined,
11-
): BackgroundThreadStatus | null {
12-
const status = resolveSidebarThreadStatus(thread);
13-
if (status === "approval" || status === "input") return status;
14-
if (status === "failed" || status === "limited") return "failed";
15-
if (status === "ready" && hasUnseenCompletion({ ...thread, lastVisitedAt })) return "done";
16+
export function resolveBackgroundThreadRequest(
17+
thread: Pick<
18+
SidebarThreadSummary,
19+
"hasPendingApprovals" | "hasPendingUserInput" | "pendingSubagentRequest"
20+
>,
21+
): BackgroundThreadSnapshot["request"] {
22+
if (thread.hasPendingApprovals || thread.pendingSubagentRequest === "approval") {
23+
return "approval";
24+
}
25+
if (thread.hasPendingUserInput || thread.pendingSubagentRequest === "user_input") {
26+
return "input";
27+
}
1628
return null;
1729
}
1830

19-
export interface BackgroundThreadEntry {
20-
readonly key: string;
21-
readonly title: string;
22-
readonly status: BackgroundThreadStatus | null;
31+
interface BackgroundThreadMemoryEntry {
32+
readonly request: BackgroundThreadSnapshot["request"];
33+
/** The last run whose ending is known, so each run's ending is announced once. */
34+
readonly settledRunId: RunId | null;
2335
}
2436

25-
export type BackgroundThreadMemory = ReadonlyMap<string, BackgroundThreadStatus | null>;
37+
export type BackgroundThreadMemory = ReadonlyMap<string, BackgroundThreadMemoryEntry>;
2638

2739
/**
2840
* Decides what a screen reader hears when a thread other than the open one
29-
* changes status. `threads` holds only threads whose environment is live, so a
30-
* thread is silent the first time it's seen: opening the app or reconnecting
31-
* takes a baseline instead of reading out every thread.
41+
* asks for something or ends a run. `threads` holds only threads whose
42+
* environment is live, so a thread is silent the first time it's seen:
43+
* opening the app or reconnecting takes a baseline instead of reading out
44+
* every thread.
3245
*/
3346
export function nextBackgroundThreadAnnouncement(
3447
previous: BackgroundThreadMemory,
35-
threads: ReadonlyArray<BackgroundThreadEntry>,
48+
threads: ReadonlyArray<BackgroundThreadSnapshot>,
3649
openThreadKey: string | null,
3750
): { readonly memory: BackgroundThreadMemory; readonly announcement: string | null } {
38-
const memory = new Map<string, BackgroundThreadStatus | null>();
51+
const memory = new Map<string, BackgroundThreadMemoryEntry>();
3952
const announcements: string[] = [];
4053
for (const thread of threads) {
41-
memory.set(thread.key, thread.status);
42-
if (
43-
thread.status !== null &&
44-
thread.key !== openThreadKey &&
45-
previous.has(thread.key) &&
46-
previous.get(thread.key) !== thread.status
47-
) {
48-
announcements.push(`${THREAD_STATUS_TEXT[thread.status]}: ${thread.title}`);
54+
const before = previous.get(thread.key);
55+
const outcome = runOutcomeText(thread.runStatus);
56+
const settled = outcome !== null || thread.runStatus === "rolled_back";
57+
memory.set(thread.key, {
58+
request: thread.request,
59+
settledRunId: settled ? thread.runId : (before?.settledRunId ?? null),
60+
});
61+
if (before === undefined || thread.key === openThreadKey || thread.snoozed) continue;
62+
if (thread.request !== null && thread.request !== before.request) {
63+
announcements.push(`${THREAD_STATUS_TEXT[thread.request]}: ${thread.title}`);
64+
}
65+
if (outcome !== null && thread.runId !== before.settledRunId) {
66+
announcements.push(`${outcome}: ${thread.title}`);
4967
}
5068
}
5169
return {

‎apps/web/src/components/chat/threadActivityAnnouncement.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const THREAD_STATUS_TEXT = {
88
input: "Needs your input",
99
done: "Done",
1010
failed: "Failed",
11+
stopped: "Stopped",
1112
} as const;
1213

1314
export interface ThreadActivitySnapshot {
@@ -44,15 +45,15 @@ const STARTING_RUN_STATUSES: ReadonlySet<OrchestrationV2RunStatus | null> = new
4445
"starting",
4546
]);
4647

47-
function runOutcomeText(status: OrchestrationV2RunStatus | null): string | null {
48+
export function runOutcomeText(status: OrchestrationV2RunStatus | null): string | null {
4849
switch (status) {
4950
case "completed":
5051
return THREAD_STATUS_TEXT.done;
5152
case "failed":
5253
return THREAD_STATUS_TEXT.failed;
5354
case "interrupted":
5455
case "cancelled":
55-
return "Stopped";
56+
return THREAD_STATUS_TEXT.stopped;
5657
default:
5758
return null;
5859
}

0 commit comments

Comments
 (0)