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

threadInbox.ts

264 lines9.1 KB

1// @effect-diagnostics globalTimers:off - Shelf placement timers run in web and native clients, outside an Effect runtime.
2import { threadRuntimeIsActive, type EnvironmentThreadShell } from "./models.ts";
3import { toSortableTimestamp } from "./threadSort.ts";
4import { effectiveSnoozed } from "./threadSettled.ts";
5​
6type WorkingThreadInput = Pick<
7 EnvironmentThreadShell,
8 | "hasActionableProposedPlan"
9 | "hasPendingApprovals"
10 | "hasPendingUserInput"
11 | "pendingSubagentRequest"
12 | "interactionMode"
13 | "latestRun"
14 | "runtime"
15>;
16​
17export function isThreadWorking(thread: WorkingThreadInput): boolean {
18 if (thread.hasPendingApprovals || thread.hasPendingUserInput) return false;
19 if (thread.pendingSubagentRequest != null) return false;
20 if (!threadRuntimeIsActive(thread.runtime) && thread.runtime?.status !== "idle") return false;
21 const run = thread.latestRun;
22 const runSettled =
23 run !== null &&
24 run.status !== "preparing" &&
25 run.status !== "queued" &&
26 run.status !== "starting" &&
27 run.status !== "running" &&
28 run.status !== "waiting" &&
29 thread.runtime?.activeRunId !== run.runId;
30 return !(thread.interactionMode === "plan" && thread.hasActionableProposedPlan && runSettled);
32​
33/**
34 * A thread its own client is launching reads as preparing until its first run lands.
35 * The server commits the thread before its run, so the list would otherwise place it twice.
36 */
37export function asLaunchingThread<T extends EnvironmentThreadShell>(thread: T): T {
38 if (thread.latestRun !== null || thread.runtime !== null) return thread;
39 return {
40 ...thread,
41 runtime: {
42 status: "preparing",
43 activeRunId: null,
44 providerInstanceId: thread.providerInstanceId,
45 providerName: null,
46 lastError: null,
47 updatedAt: thread.updatedAt,
48 },
49 };
51​
52type InboxThreadInput = Pick<
53 EnvironmentThreadShell,
54 "id" | "environmentId" | "createdAt" | "unsettledAt" | "latestRun"
55>;
56​
57export function sortInboxThreadsByReturn<T extends InboxThreadInput>(
58 threads: readonly T[],
59 observedReturnAt?: (thread: T) => number | undefined,
60): T[] {
61 const timestamps = new Map(
62 threads.map((thread) => [
63 thread,
64 Math.max(
65 toSortableTimestamp(thread.createdAt) ?? 0,
66 toSortableTimestamp(thread.unsettledAt ?? undefined) ?? 0,
67 toSortableTimestamp(thread.latestRun?.requestedAt ?? undefined) ?? 0,
68 toSortableTimestamp(thread.latestRun?.completedAt ?? undefined) ?? 0,
69 observedReturnAt?.(thread) ?? 0,
70 ),
71 ]),
72 );
73 return sortNewestFirst(threads, timestamps);
75​
76type WorkingSortInput = Pick<
77 EnvironmentThreadShell,
78 "id" | "environmentId" | "createdAt" | "latestRun" | "latestUserAuthoredMessageAt"
79>;
80​
81export function sortWorkingThreadsBySend<T extends WorkingSortInput>(threads: readonly T[]): T[] {
82 const timestamps = new Map(
83 threads.map((thread) => [
84 thread,
85 Math.max(
86 toSortableTimestamp(thread.createdAt) ?? 0,
87 toSortableTimestamp(
88 (thread.latestUserAuthoredMessageAt === undefined
89 ? thread.latestRun?.requestedAt
90 : thread.latestUserAuthoredMessageAt) ?? undefined,
91 ) ?? 0,
92 ),
93 ]),
94 );
95 return sortNewestFirst(threads, timestamps);
97​
98function sortNewestFirst<T extends Pick<EnvironmentThreadShell, "id" | "environmentId">>(
99 threads: readonly T[],
100 timestamps: ReadonlyMap<T, number>,
101): T[] {
102 return [...threads].sort(
103 (left, right) =>
104 timestamps.get(right)! - timestamps.get(left)! ||
105 left.id.localeCompare(right.id) ||
106 left.environmentId.localeCompare(right.environmentId),
107 );
109​
110type TrackedInboxThread = WorkingThreadInput &
111 InboxThreadInput &
112 Partial<
113 Pick<
114 EnvironmentThreadShell,
115 | "archivedAt"
116 | "deletedAt"
117 | "pinnedAt"
118 | "settledOverride"
119 | "settledAt"
120 | "snoozedAt"
121 | "snoozedUntil"
122 | "activeOrderKey"
123 | "pendingGuestMessageCount"
124 >
125 >;
126​
127type ThreadKeyInput = Pick<EnvironmentThreadShell, "environmentId" | "id">;
128​
129/**
130 * How long a thread that stopped working keeps reading as working. An orchestrator
131 * run can end moments before its continuation starts, so clients wait this long
132 * before treating the thread as done.
133 */
134const WORKING_EXIT_GRACE_MS = 2_000;
135​
136/**
137 * Keep one at module scope so placement survives routes that unmount the list. Call
138 * `observe` when the shells or the snapshot version change, or with null while the beta
139 * is off. Its result stays fixed, so a list built from it can be cached.
140 */
141export function createInboxReturnTracker() {
142 const keyOf = (thread: ThreadKeyInput) => `${thread.environmentId}:${thread.id}`;
143 const states = new Map<
144 string,
145 { working: boolean; deadline: number | null; placement: string | null }
146 >();
147 const returns = new Map<string, number>();
148 const listeners = new Set<() => void>();
149 let timer: ReturnType<typeof setTimeout> | undefined;
150 let version = 0;
151 const now = () => {
152 // @effect-diagnostics-next-line globalDate:off - The grace deadline is wall-clock time shared by web and native clients.
153 return Date.now();
154 };
155 const schedule = () => {
156 clearTimeout(timer);
157 timer = undefined;
158 let next = Infinity;
159 for (const state of states.values()) {
160 if (state.deadline !== null) next = Math.min(next, state.deadline);
161 }
162 if (next === Infinity) return;
163 timer = setTimeout(
164 () => {
165 const at = now();
166 for (const [key, state] of states) {
167 if (state.deadline !== null && state.deadline <= at) {
168 state.working = false;
169 state.deadline = null;
170 returns.set(key, at);
171 }
172 }
173 schedule();
174 version++;
175 for (const listener of listeners) listener();
176 },
177 Math.max(0, next - now()),
178 );
179 };
180 return {
181 subscribe(listener: () => void) {
182 listeners.add(listener);
183 return () => {
184 listeners.delete(listener);
185 };
186 },
187 getSnapshot: () => version,
188 observe(threads: readonly TrackedInboxThread[] | null): {
189 readonly isWorking: (thread: WorkingThreadInput & ThreadKeyInput) => boolean;
190 readonly returnedAt: (thread: ThreadKeyInput) => number | undefined;
191 } {
192 if (threads === null) {
193 clearTimeout(timer);
194 timer = undefined;
195 states.clear();
196 returns.clear();
197 return { isWorking: isThreadWorking, returnedAt: () => undefined };
198 }
199 const present = new Set<string>();
200 const at = now();
201 // @effect-diagnostics-next-line globalDate:off - Snooze checks compare wall-clock ISO times.
202 const observedAt = new Date(at).toISOString();
203 for (const thread of threads) {
204 const key = keyOf(thread);
205 present.add(key);
206 const previous = states.get(key);
207 const actual = isThreadWorking(thread);
208 const placement =
209 actual || previous?.working
210 ? JSON.stringify([
211 thread.archivedAt,
212 thread.deletedAt,
213 thread.pinnedAt,
214 thread.settledOverride,
215 thread.settledAt,
216 thread.snoozedAt,
217 thread.snoozedUntil,
218 thread.activeOrderKey,
219 ])
220 : null;
221 const actionable =
222 thread.hasPendingApprovals ||
223 thread.hasPendingUserInput ||
224 thread.pendingSubagentRequest != null ||
225 (thread.pendingGuestMessageCount ?? 0) > 0 ||
226 (thread.interactionMode === "plan" && thread.hasActionableProposedPlan) ||
227 thread.runtime?.lastError != null ||
228 thread.latestRun?.status === "failed";
229 const moved = previous !== undefined && previous.placement !== placement;
230 const excluded =
231 thread.archivedAt != null ||
232 thread.deletedAt != null ||
233 thread.pinnedAt != null ||
234 thread.settledOverride === "settled" ||
235 effectiveSnoozed(thread, { now: observedAt });
236 // Only shelf placement is held: execution state and attention requests stay authoritative.
237 const deadline =
238 !actual && previous?.working && !actionable && !moved && !excluded
239 ? (previous.deadline ?? at + WORKING_EXIT_GRACE_MS)
240 : null;
241 const working = actual || (deadline !== null && deadline > at);
242 if (previous?.working && !working) returns.set(key, at);
243 states.set(key, { working, deadline: working ? deadline : null, placement });
244 }
245 for (const key of states.keys()) {
246 if (!present.has(key)) {
247 states.delete(key);
248 returns.delete(key);
249 }
250 }
251 schedule();
252 const observedWorking = new Map<string, boolean>();
253 states.forEach((state, key) => observedWorking.set(key, state.working));
254 const observedReturns = new Map(returns);
255 return {
256 isWorking: (thread) => observedWorking.get(keyOf(thread)) ?? isThreadWorking(thread),
257 returnedAt: (thread) => observedReturns.get(keyOf(thread)),
258 };
259 },
260 isWorking: (thread: WorkingThreadInput & ThreadKeyInput) =>
261 states.get(keyOf(thread))?.working ?? isThreadWorking(thread),
262 returnedAt: (thread: ThreadKeyInput) => returns.get(keyOf(thread)),
263 };