Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadDetail.ts

605 lines24.3 KB

1import {
2 OrchestrationV2AppThread,
3 OrchestrationV2TurnItem,
4 type OrchestrationV2ThreadProjection,
5 type ScopedThreadRef,
6} from "@t3tools/contracts";
7import * as Option from "effect/Option";
8import * as Schema from "effect/Schema";
9import { AsyncResult, Atom } from "effect/reactivity";
10​
11import {
12 deriveThreadQueueWorkflowState,
13 getUserQueuedThreadRuns,
14 type ThreadQueueWorkflowState,
15} from "./threadWorkflows.ts";
16import { deriveThreadTurnSubagents, type ThreadTurnSubagents } from "./threadSubagents.ts";
17import {
18 deriveUsageLimitNoticeRows,
19 presentRunStop,
20 withoutUsageLimitNoticeRows,
21 type RunStopPresentation,
22 type UsageLimitNoticeRows,
23} from "./threadExecution.ts";
24import {
25 deriveThreadCheckpointSummaries,
26 type ThreadCheckpointSummary,
27} from "./threadCheckpoints.ts";
28import type {
29 EnvironmentThread,
30 EnvironmentThreadStructure,
31 ThreadStructure,
32 ThreadStructureThread,
33 ThreadStructureTurnItem,
34 ThreadStructureVisibleTurnItem,
35} from "./models.ts";
36import { EMPTY_ENVIRONMENT_THREAD_STATE, type EnvironmentThreadState } from "./threadState.ts";
37import {
38 createQuestionHistoryProjector,
39 derivePendingThreadRequests,
40 type PendingThreadRequests,
41} from "./threadRequests.ts";
42import { arrayElementsEqual, parseThreadKey, threadKey } from "./entities.ts";
43import { getProjectionArrayReplacementIndex } from "./orchestrationV2Projection.ts";
44​
45const EMPTY_VISIBLE_TURN_ITEMS: OrchestrationV2ThreadProjection["visibleTurnItems"] = Object.freeze(
46 [],
47);
48const EMPTY_THREAD_STRUCTURE_TURN_ITEMS: ReadonlyArray<ThreadStructureVisibleTurnItem> =
49 Object.freeze([]);
50const EMPTY_CHECKPOINT_SUMMARIES: ReadonlyArray<ThreadCheckpointSummary> = Object.freeze([]);
51​
52/**
53 * Changes when the agent may have changed the workspace, so file views and host images refresh.
54 * File tools are explicit; commands, MCP or dynamic tools and subagents are included because
55 * they can mutate the workspace without reporting the paths they touched.
56 */
57export function deriveWorkspaceMutationId(
58 visibleTurnItems: ReadonlyArray<ThreadStructureVisibleTurnItem>,
59 latestCheckpointCompletedAt: string | null,
60) {
61 let itemId: string | null = null;
62 for (let index = visibleTurnItems.length - 1; index >= 0; index -= 1) {
63 const item = visibleTurnItems[index]?.item;
64 if (!item) continue;
65 if (
66 item.type !== "command_execution" &&
67 item.type !== "file_change" &&
68 item.type !== "dynamic_tool" &&
69 item.type !== "subagent"
70 ) {
71 continue;
72 }
73 if (item.status === "pending" || item.status === "running" || item.status === "waiting") {
74 continue;
75 }
76 itemId = item.id;
77 break;
78 }
79 return itemId === null && latestCheckpointCompletedAt === null
80 ? null
81 : JSON.stringify([itemId, latestCheckpointCompletedAt]);
83​
84type TurnItem = OrchestrationV2ThreadProjection["turnItems"][number];
85type VisibleTurnItem = OrchestrationV2ThreadProjection["visibleTurnItems"][number];
86type AppThread = OrchestrationV2ThreadProjection["thread"];
87​
88// Items are stripped once each: unchanged items keep their identity across
89// structure changes, which the incremental timeline relies on.
90const structureTurnItems = new WeakMap<TurnItem, ThreadStructureTurnItem>();
91const structureVisibleTurnItems = new WeakMap<VisibleTurnItem, ThreadStructureVisibleTurnItem>();
92​
93function toThreadStructureTurnItem(item: TurnItem): ThreadStructureTurnItem {
94 if (item.type !== "assistant_message" && item.type !== "reasoning") return item;
95 let structure = structureTurnItems.get(item);
96 if (structure === undefined) {
97 const { text: _text, updatedAt: _updatedAt, ...rest } = item;
98 structure = rest;
99 structureTurnItems.set(item, structure);
100 }
101 return structure;
103​
104function toThreadStructureVisibleTurnItem(
105 visibleItem: VisibleTurnItem,
106): ThreadStructureVisibleTurnItem {
107 const item = toThreadStructureTurnItem(visibleItem.item);
108 if (item === visibleItem.item) return visibleItem;
109 let structure = structureVisibleTurnItems.get(visibleItem);
110 if (structure === undefined) {
111 structure = { ...visibleItem, item };
112 structureVisibleTurnItems.set(visibleItem, structure);
113 }
114 return structure;
116​
117/** Whether the structure kept every field of this row: true for all but streamed items. */
118export function isCompleteTurnItemRow(row: ThreadStructureVisibleTurnItem): row is VisibleTurnItem {
119 return row.item.type !== "assistant_message" && row.item.type !== "reasoning";
121​
122// The open thread is marked visited several times while a reply streams. Visit
123// state is read from the shell, or from lastVisitedAtAtom while the thread is
124// not in the shell index yet, so a mark is not structure.
125function toThreadStructureThread(thread: AppThread): ThreadStructureThread {
126 const { updatedAt: _updatedAt, lastVisitedAt: _lastVisitedAt, ...structure } = thread;
127 return structure;
129​
130function toThreadStructure(projection: OrchestrationV2ThreadProjection): ThreadStructure {
131 const { updatedAt: _updatedAt, ...structure } = projection;
132 return {
133 ...structure,
134 thread: toThreadStructureThread(projection.thread),
135 turnItems: projection.turnItems.map(toThreadStructureTurnItem),
136 visibleTurnItems: projection.visibleTurnItems.map(toThreadStructureVisibleTurnItem),
137 };
139​
140const sameTurnItem = Schema.toEquivalence(OrchestrationV2TurnItem);
141const sameAppThread = Schema.toEquivalence(OrchestrationV2AppThread);
142​
143// Overlays previous's structure on next, so the fields the strip functions
144// remove are exactly the ones ignored here.
145function sameApartFromStripped<T extends object>(
146 same: (left: T, right: T) => boolean,
147 previous: T,
148 previousStructure: object,
149 next: T,
150): boolean {
151 return (
152 Object.keys(previous).length === Object.keys(next).length &&
153 same(Object.assign({}, next, previousStructure), next)
154 );
156​
157function sameTurnItemStructure(previous: TurnItem, next: TurnItem): boolean {
158 if (previous === next) return true;
159 const structure = toThreadStructureTurnItem(previous);
160 if (previous.id !== next.id || previous.type !== next.type || structure === previous) {
161 return false;
162 }
163 return sameApartFromStripped(sameTurnItem, previous, structure, next);
165​
166function sameProjectionArray<T>(
167 previous: ReadonlyArray<T>,
168 next: ReadonlyArray<T>,
169 same: (previous: T, next: T) => boolean,
170): boolean {
171 if (previous === next) return true;
172 if (previous.length !== next.length) return false;
173 const replacedIndex = getProjectionArrayReplacementIndex(previous, next);
174 if (replacedIndex !== undefined) return same(previous[replacedIndex]!, next[replacedIndex]!);
175 return previous.every((item, index) => same(item, next[index]!));
177​
178function sameVisibleTurnItemStructure(before: VisibleTurnItem, after: VisibleTurnItem): boolean {
179 return (
180 before === after ||
181 (before.position === after.position &&
182 before.visibility === after.visibility &&
183 before.sourceThreadId === after.sourceThreadId &&
184 before.sourceItemId === after.sourceItemId &&
185 sameTurnItemStructure(before.item, after.item))
186 );
188​
189function isStreamingAssistantMessage(visibleItem: VisibleTurnItem): boolean {
190 return visibleItem.item.type === "assistant_message" && visibleItem.item.streaming;
192​
193/** Whether `next` only streams more text into assistant messages that were already streaming. */
194function sameApartFromStreamedAssistantText(
195 previous: OrchestrationV2ThreadProjection["visibleTurnItems"],
196 next: OrchestrationV2ThreadProjection["visibleTurnItems"],
197): boolean {
198 return sameProjectionArray(
199 previous,
200 next,
201 (before, after) =>
202 before === after ||
203 (isStreamingAssistantMessage(before) &&
204 isStreamingAssistantMessage(after) &&
205 sameVisibleTurnItemStructure(before, after)),
206 );
208​
209/**
210 * Whether `previous` and `next` have the same ThreadStructure: they differ
211 * only by text streamed into existing assistant and reasoning items or by a
212 * visit mark. The same items exist, with the same status, and every other
213 * field is unchanged.
214 */
215export function sameThreadStructure(
216 previous: OrchestrationV2ThreadProjection,
217 next: OrchestrationV2ThreadProjection,
218): boolean {
219 if (previous === next) return true;
220 for (const key of Object.keys(next) as Array<keyof OrchestrationV2ThreadProjection>) {
221 if (
222 key === "thread" ||
223 key === "turnItems" ||
224 key === "visibleTurnItems" ||
225 key === "updatedAt"
226 ) {
227 continue;
228 }
229 if (previous[key] !== next[key]) return false;
230 }
231 if (
232 previous.thread !== next.thread &&
233 !sameApartFromStripped(
234 sameAppThread,
235 previous.thread,
236 toThreadStructureThread(previous.thread),
237 next.thread,
238 )
239 ) {
240 return false;
241 }
242 return (
243 sameProjectionArray(previous.turnItems, next.turnItems, sameTurnItemStructure) &&
244 sameProjectionArray(
245 previous.visibleTurnItems,
246 next.visibleTurnItems,
247 sameVisibleTurnItemStructure,
248 )
249 );
251​
252export function createEnvironmentThreadDetailAtoms<E>(
253 threadStateAtom: (
254 environmentId: ScopedThreadRef["environmentId"],
255 threadId: ScopedThreadRef["threadId"],
256 ) => Atom.Atom<AsyncResult.AsyncResult<EnvironmentThreadState, E>>,
257) {
258 const threadStateValueAtomFamily = Atom.family((key: string) => {
259 const ref = parseThreadKey(key);
260 return Atom.make((get) =>
261 AsyncResult.getOrElse(
262 get(threadStateAtom(ref.environmentId, ref.threadId)),
263 () => EMPTY_ENVIRONMENT_THREAD_STATE,
264 ),
265 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-state-value:${key}`));
266 });
267​
268 const threadAtomFamily = Atom.family((key: string) => {
269 const ref = parseThreadKey(key);
270 let previousProjection: OrchestrationV2ThreadProjection | null = null;
271 let previousValue: EnvironmentThread | null = null;
272 return Atom.make((get) => {
273 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
274 if (projection === previousProjection) return previousValue;
275 previousProjection = projection;
276 previousValue = projection === null ? null : { environmentId: ref.environmentId, projection };
277 return previousValue;
278 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread:${key}`));
279 });
280​
281 // Streamed assistant and reasoning text replaces an item several times a
282 // second. Readers of the thread's structure (runs, requests, which items
283 // exist and their status) keep the previous value through those updates.
284 const quietThreadAtomFamily = Atom.family((key: string) => {
285 let previousProjection: OrchestrationV2ThreadProjection | null = null;
286 let previous: EnvironmentThreadStructure | null = null;
287 return Atom.make((get) => {
288 const next = get(threadAtomFamily(key));
289 if (next === null) {
290 previousProjection = null;
291 previous = null;
292 } else if (
293 previousProjection === null ||
294 !sameThreadStructure(previousProjection, next.projection)
295 ) {
296 previous = {
297 environmentId: next.environmentId,
298 projection: toThreadStructure(next.projection),
299 };
300 }
301 // Comparing the latest projection lets consecutive replacements bypass history scans.
302 previousProjection = next?.projection ?? null;
303 return previous;
304 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-quiet-thread:${key}`));
305 });
306​
307 // Read from the structure so streamed text does not rescan runs and turn items.
308 const usageLimitNoticeRowsAtomFamily = Atom.family((key: string) => {
309 let previous: EnvironmentThreadStructure | null = null;
310 let value: UsageLimitNoticeRows | null = null;
311 return Atom.make((get) => {
312 const thread = get(quietThreadAtomFamily(key));
313 if (thread !== previous) {
314 previous = thread;
315 value = thread === null ? null : deriveUsageLimitNoticeRows(thread.projection);
316 }
317 return value;
318 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-usage-limit-notice:${key}`));
319 });
320​
321 // Presentation only: the projection keeps every row for other readers.
322 const visibleTurnItemsAtomFamily = Atom.family((key: string) => {
323 const projectQuestionHistory = createQuestionHistoryProjector();
324 let previousRows: OrchestrationV2ThreadProjection["visibleTurnItems"] | null = null;
325 let previousNotice: UsageLimitNoticeRows | null = null;
326 let previous = EMPTY_VISIBLE_TURN_ITEMS;
327 return Atom.make((get): OrchestrationV2ThreadProjection["visibleTurnItems"] => {
328 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
329 if (projection === null) return EMPTY_VISIBLE_TURN_ITEMS;
330 const rows = projectQuestionHistory(projection);
331 const notice = get(usageLimitNoticeRowsAtomFamily(key));
332 if (rows !== previousRows || notice !== previousNotice) {
333 previousRows = rows;
334 previousNotice = notice;
335 previous = withoutUsageLimitNoticeRows(rows, notice);
336 }
337 return previous;
338 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-visible-turn-items:${key}`));
339 });
340​
341 const quietVisibleTurnItemsAtomFamily = Atom.family((key: string) => {
342 const projectQuestionHistory = createQuestionHistoryProjector<ThreadStructureVisibleTurnItem>();
343 return Atom.make((get) => {
344 const thread = get(quietThreadAtomFamily(key));
345 return thread === null
346 ? EMPTY_THREAD_STRUCTURE_TURN_ITEMS
347 : projectQuestionHistory(thread.projection);
348 }).pipe(
349 Atom.setIdleTTL(0),
350 Atom.withLabel(`environment-quiet-thread-visible-turn-items:${key}`),
351 );
352 });
353​
354 // Holds while only streamed assistant text moves, so the timeline re-renders on structure;
355 // each streaming message reads its text through assistantMessageTextAtom.
356 const timelineVisibleTurnItemsAtomFamily = Atom.family((key: string) => {
357 let previous = EMPTY_VISIBLE_TURN_ITEMS;
358 return Atom.make((get) => {
359 const next = get(visibleTurnItemsAtomFamily(key));
360 if (!sameApartFromStreamedAssistantText(previous, next)) previous = next;
361 return previous;
362 }).pipe(
363 Atom.setIdleTTL(0),
364 Atom.withLabel(`environment-thread-timeline-visible-turn-items:${key}`),
365 );
366 });
367​
368 const assistantMessageTextAtomFamily = Atom.family((key: string) => {
369 const separator = key.lastIndexOf("\u0000");
370 const thread = key.slice(0, separator);
371 const itemId = key.slice(separator + 1);
372 return Atom.make((get) => {
373 const item = get(visibleTurnItemsAtomFamily(thread)).find(
374 (visibleItem) => visibleItem.item.id === itemId,
375 )?.item;
376 return item?.type === "assistant_message" ? item.text : null;
377 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-assistant-message-text:${key}`));
378 });
379​
380 const queueWorkflowAtomFamily = Atom.family((key: string) => {
381 let previous: Pick<
382 OrchestrationV2ThreadProjection,
383 "thread" | "runs" | "messages" | "providerThreads" | "providerSessions" | "providerTurns"
384 > | null = null;
385 let value: ThreadQueueWorkflowState | null = null;
386 return Atom.make((get) => {
387 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
388 if (projection === null) {
389 previous = null;
390 value = null;
391 } else if (
392 previous === null ||
393 projection.thread !== previous.thread ||
394 projection.runs !== previous.runs ||
395 projection.messages !== previous.messages ||
396 projection.providerThreads !== previous.providerThreads ||
397 projection.providerSessions !== previous.providerSessions ||
398 projection.providerTurns !== previous.providerTurns
399 ) {
400 value = deriveThreadQueueWorkflowState(projection);
401 const { thread, runs, messages, providerThreads, providerSessions, providerTurns } =
402 projection;
403 previous = { thread, runs, messages, providerThreads, providerSessions, providerTurns };
404 }
405 return value;
406 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-queue:${key}`));
407 });
408 const runStopAtomFamily = Atom.family((key: string) => {
409 let previous: Pick<OrchestrationV2ThreadProjection, "runs" | "turnItems"> | null = null;
410 let stop: RunStopPresentation | null = null;
411 return Atom.make((get) => {
412 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
413 if (projection === null) {
414 previous = null;
415 stop = null;
416 } else if (
417 projection.runs !== previous?.runs ||
418 projection.turnItems !== previous?.turnItems
419 ) {
420 const { runs, turnItems } = projection;
421 previous = { runs, turnItems };
422 const next = presentRunStop(previous);
423 if (
424 next?.state !== stop?.state ||
425 next?.runId !== stop?.runId ||
426 next?.requestedAt !== stop?.requestedAt ||
427 next?.background !== stop?.background
428 )
429 stop = next;
430 }
431 return stop;
432 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-run-stop:${key}`));
433 });
434 const queuedCountAtomFamily = Atom.family((key: string) => {
435 let previous: Pick<OrchestrationV2ThreadProjection, "runs" | "messages"> | null = null;
436 let count = 0;
437 return Atom.make((get) => {
438 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
439 if (projection === null) {
440 previous = null;
441 count = 0;
442 } else if (projection.runs !== previous?.runs || projection.messages !== previous?.messages) {
443 const { runs, messages } = projection;
444 previous = { runs, messages };
445 count = getUserQueuedThreadRuns(previous).length;
446 }
447 return count;
448 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-queue-count:${key}`));
449 });
450​
451 const turnSubagentsAtomFamily = Atom.family((key: string) => {
452 let previous: Pick<OrchestrationV2ThreadProjection, "runs" | "subagents"> | null = null;
453 let value: ThreadTurnSubagents | null = null;
454 return Atom.make((get) => {
455 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
456 if (projection === null) {
457 previous = null;
458 value = null;
459 } else if (
460 projection.runs !== previous?.runs ||
461 projection.subagents !== previous?.subagents
462 ) {
463 const { runs, subagents } = projection;
464 previous = { runs, subagents };
465 value = deriveThreadTurnSubagents(previous);
466 }
467 return value;
468 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-turn-subagents:${key}`));
469 });
470​
471 const checkpointSummariesAtomFamily = Atom.family((key: string) => {
472 let previous: Pick<OrchestrationV2ThreadProjection, "checkpoints" | "messages"> | null = null;
473 let value = EMPTY_CHECKPOINT_SUMMARIES;
474 return Atom.make((get) => {
475 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
476 if (projection === null) {
477 previous = null;
478 value = EMPTY_CHECKPOINT_SUMMARIES;
479 } else if (
480 projection.checkpoints !== previous?.checkpoints ||
481 projection.messages !== previous?.messages
482 ) {
483 const { checkpoints, messages } = projection;
484 previous = { checkpoints, messages };
485 value = deriveThreadCheckpointSummaries(previous);
486 }
487 return value;
488 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-checkpoint-summaries:${key}`));
489 });
490​
491 const lastVisitedAtAtomFamily = Atom.family((key: string) =>
492 Atom.make(
493 (get) =>
494 Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.thread.lastVisitedAt ?? null,
495 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-last-visited:${key}`)),
496 );
497 const updatedAtAtomFamily = Atom.family((key: string) =>
498 Atom.make(
499 (get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.updatedAt ?? null,
500 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-updated-at:${key}`)),
501 );
502 const worktreePathAtomFamily = Atom.family((key: string) =>
503 Atom.make(
504 (get) =>
505 Option.getOrNull(get(threadStateValueAtomFamily(key)).data)?.thread.worktreePath ?? null,
506 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-worktree:${key}`)),
507 );
508 const pendingRequestsAtomFamily = Atom.family((key: string) => {
509 let previous: Pick<OrchestrationV2ThreadProjection, "runtimeRequests" | "turnItems"> | null =
510 null;
511 let value: PendingThreadRequests | null = null;
512 return Atom.make((get) => {
513 const projection = Option.getOrNull(get(threadStateValueAtomFamily(key)).data);
514 if (projection === null) {
515 previous = null;
516 value = null;
517 return value;
518 }
519 const runtimeRequests = projection.runtimeRequests.filter(
520 (request) => request.status === "pending",
521 );
522 const ids = new Set(runtimeRequests.map((request) => request.id));
523 const turnItems =
524 ids.size === 0
525 ? []
526 : projection.turnItems.filter(
527 (item) =>
528 (item.type === "approval_request" || item.type === "user_input_request") &&
529 ids.has(item.requestId),
530 );
531 if (
532 previous === null ||
533 !arrayElementsEqual(previous.runtimeRequests, runtimeRequests) ||
534 !arrayElementsEqual(previous.turnItems, turnItems)
535 ) {
536 previous = { runtimeRequests, turnItems };
537 value = derivePendingThreadRequests(previous);
538 }
539 return value;
540 }).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-pending-requests:${key}`));
541 });
542​
543 const statusAtomFamily = Atom.family((key: string) =>
544 Atom.make((get) => get(threadStateValueAtomFamily(key)).status).pipe(
545 Atom.setIdleTTL(0),
546 Atom.withLabel(`environment-thread-status:${key}`),
547 ),
548 );
549 const sequenceAtomFamily = Atom.family((key: string) =>
550 Atom.make((get) => get(threadStateValueAtomFamily(key)).sequence).pipe(
551 Atom.setIdleTTL(0),
552 Atom.withLabel(`environment-thread-sequence:${key}`),
553 ),
554 );
555 const errorAtomFamily = Atom.family((key: string) =>
556 Atom.make((get) => Option.getOrNull(get(threadStateValueAtomFamily(key)).error)).pipe(
557 Atom.setIdleTTL(0),
558 Atom.withLabel(`environment-thread-error:${key}`),
559 ),
560 );
561 const historyAtomFamily = Atom.family((key: string) =>
562 Atom.make((get) => get(threadStateValueAtomFamily(key)).history).pipe(
563 Atom.setIdleTTL(0),
564 Atom.withLabel(`environment-thread-history:${key}`),
565 ),
566 );
567​
568 const workspaceMutationIdAtomFamily = Atom.family((key: string) =>
569 Atom.make((get) =>
570 deriveWorkspaceMutationId(
571 get(quietVisibleTurnItemsAtomFamily(key)),
572 get(checkpointSummariesAtomFamily(key)).at(-1)?.completedAt ?? null,
573 ),
574 ).pipe(Atom.setIdleTTL(0), Atom.withLabel(`environment-thread-workspace-mutation-id:${key}`)),
575 );
576​
577 return {
578 worktreePathAtom: (ref: ScopedThreadRef) => worktreePathAtomFamily(threadKey(ref)),
579 workspaceMutationIdAtom: (ref: ScopedThreadRef) =>
580 workspaceMutationIdAtomFamily(threadKey(ref)),
581 updatedAtAtom: (ref: ScopedThreadRef) => updatedAtAtomFamily(threadKey(ref)),
582 lastVisitedAtAtom: (ref: ScopedThreadRef) => lastVisitedAtAtomFamily(threadKey(ref)),
583 pendingRequestsAtom: (ref: ScopedThreadRef) => pendingRequestsAtomFamily(threadKey(ref)),
584 queueWorkflowAtom: (ref: ScopedThreadRef) => queueWorkflowAtomFamily(threadKey(ref)),
585 queuedCountAtom: (ref: ScopedThreadRef) => queuedCountAtomFamily(threadKey(ref)),
586 runStopAtom: (ref: ScopedThreadRef) => runStopAtomFamily(threadKey(ref)),
587 turnSubagentsAtom: (ref: ScopedThreadRef) => turnSubagentsAtomFamily(threadKey(ref)),
588 checkpointSummariesAtom: (ref: ScopedThreadRef) =>
589 checkpointSummariesAtomFamily(threadKey(ref)),
590 stateAtom: (ref: ScopedThreadRef) => threadStateValueAtomFamily(threadKey(ref)),
591 threadAtom: (ref: ScopedThreadRef) => threadAtomFamily(threadKey(ref)),
592 quietThreadAtom: (ref: ScopedThreadRef) => quietThreadAtomFamily(threadKey(ref)),
593 visibleTurnItemsAtom: (ref: ScopedThreadRef) => visibleTurnItemsAtomFamily(threadKey(ref)),
594 quietVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
595 quietVisibleTurnItemsAtomFamily(threadKey(ref)),
596 timelineVisibleTurnItemsAtom: (ref: ScopedThreadRef) =>
597 timelineVisibleTurnItemsAtomFamily(threadKey(ref)),
598 assistantMessageTextAtom: (ref: ScopedThreadRef, itemId: string) =>
599 assistantMessageTextAtomFamily(`${threadKey(ref)}\u0000${itemId}`),
600 statusAtom: (ref: ScopedThreadRef) => statusAtomFamily(threadKey(ref)),
601 sequenceAtom: (ref: ScopedThreadRef) => sequenceAtomFamily(threadKey(ref)),
602 errorAtom: (ref: ScopedThreadRef) => errorAtomFamily(threadKey(ref)),
603 historyAtom: (ref: ScopedThreadRef) => historyAtomFamily(threadKey(ref)),
604 };