@devries-cameron20 / t3code Mirror · read-only / commits / 8bf4822f5e
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 12:00 UTC

perf(server): sidebar shell loads 3x faster and startup thread counts 80x faster (#27106)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 29, 2026 at 13:18 UTC 8bf4822f5eb5b24f3129960ce771daacc5c01a44
17 files changed +436 -154
apps/server/integration/transferBudgetV2.integration.test.ts
+1
@@ -117,6 +117,7 @@ const layerManagement = Layer.unwrap(
117 .pipe(Effect.orDie),
118 getThreadShell: (id) => projections.getThreadShell(id).pipe(Effect.orDie),
119 getShellSnapshot: (options) => projections.getShellSnapshot(options).pipe(Effect.orDie),
120 + listActiveShellIndex: () => projections.listActiveShellIndex().pipe(Effect.orDie),
121 listActiveSubagentThreadIds: () =>
122 projections.listActiveSubagentThreadIds().pipe(Effect.orDie),
123 readShellSnapshot: (options) =>
apps/server/src/orchestration-v2/Orchestrator.ts
+12
@@ -141,6 +141,7 @@ import {
141 type ProjectionRecords,
142 type ProjectionCheckpointContext,
143 type OlderSettledThreadsListing,
144 + type ActiveShellIndexEntry,
145 type OlderSettledThreadsQuery,
146 type ShellSnapshotOptions,
147 } from "./ProjectionStore.ts";
@@ -392,6 +393,11 @@ export interface OrchestratorV2Shape {
393 ReadonlyArray<ThreadId>,
394 OrchestratorV2Error
395 >;
396 + readonly countThreads: () => Effect.Effect<number, OrchestratorV2Error>;
397 + readonly listActiveShellIndex: () => Effect.Effect<
398 + ReadonlyArray<ActiveShellIndexEntry>,
399 + OrchestratorV2Error
400 + >;
401 readonly listOlderSettledThreads: (
402 query: OlderSettledThreadsQuery,
403 ) => Effect.Effect<OlderSettledThreadsListing, OrchestratorV2Error>;
@@ -13434,6 +13440,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13440 .pipe(Effect.mapError(shellProjectionError));
13441 },
13442 ),
13443 + countThreads: Effect.fn("OrchestratorV2.countThreads")(function () {
13444 + return projectionStore.countThreads().pipe(Effect.mapError(shellProjectionError));
13445 + }),
13446 + listActiveShellIndex: Effect.fn("OrchestratorV2.listActiveShellIndex")(function () {
13447 + return projectionStore.listActiveShellIndex().pipe(Effect.mapError(shellProjectionError));
13448 + }),
13449 listOlderSettledThreads: Effect.fn("OrchestratorV2.listOlderSettledThreads")(function (query) {
13450 return projectionStore
13451 .listOlderSettledThreads(query)
apps/server/src/orchestration-v2/ProjectionStore.test.ts
+34
@@ -26,6 +26,7 @@ import {
26 ThreadId,
27 TurnItemId,
28 type ThreadPullRequestLink,
29 + isLiveSubagentShell,
30 } from "@t3tools/contracts";
31 import * as DateTime from "effect/DateTime";
32 import * as Effect from "effect/Effect";
@@ -76,6 +77,33 @@ const traceSqlStatements = () => {
77 return { statements, tracer };
78 };
79
80 +/** The shell index flags every live shell and matches its shells on the fields subscriber trims read. */
81 +const assertShellIndexCoversShells = Effect.fnUntraced(function* () {
82 + const store = yield* ProjectionStore.ProjectionStoreV2;
83 + const shells = (yield* store.getShellSnapshot({ location: "active" })).threads;
84 + const index = yield* store.listActiveShellIndex();
85 + const iso = (value: DateTime.Utc | null | undefined) =>
86 + value == null ? null : DateTime.formatIso(value);
87 + const trimmed = (
88 + row: Pick<
89 + ProjectionStore.ActiveShellIndexEntry,
90 + "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pinnedAt" | "snoozedUntil"
91 + >,
92 + ) => ({
93 + id: row.id,
94 + projectId: row.projectId,
95 + lineage: row.lineage,
96 + settledOverride: row.settledOverride,
97 + settledAt: iso(row.settledAt),
98 + pinnedAt: iso(row.pinnedAt),
99 + snoozedUntil: iso(row.snoozedUntil),
100 + });
101 + assert.deepEqual(index.map(trimmed), shells.map(trimmed));
102 + shells.forEach((shell, position) => {
103 + if (isLiveSubagentShell(shell)) assert.isTrue(index[position]?.mayBeLive, shell.id);
104 + });
105 +});
106 +
107 const addRolledBackRecoveryCandidate = Effect.fn("addRolledBackRecoveryCandidate")(function* (
108 suffix: string,
109 ) {
@@ -272,6 +300,7 @@ it.effect("SQL shells flag only an active turn blocked solely on T3 thread waits
300 assert.equal(shell?.status, "running");
301 assert.equal(shell?.blockedOnThreadWait ?? false, expected);
302 }
303 + yield* assertShellIndexCoversShells();
304 });
305 const wait = {
306 ...itemBase,
@@ -531,6 +560,7 @@ it.effect("SQL shells use the latest subagent owner for duplicate provider-threa
560 const listed = (yield* store.getShellSnapshot()).threads.find(
561 (thread) => thread.id === targetThreadId,
562 );
563 + yield* assertShellIndexCoversShells();
564 for (const shell of [yield* store.getThreadShell(targetThreadId), listed]) {
565 assert.equal(shell?.providerInstanceId, otherInstanceId);
566 assert.equal(shell?.activeRunId, null);
@@ -1336,6 +1366,7 @@ it.effect("SQL list older settled threads below a cursor", () =>
1366 const next = yield* list({ settledAt: DateTime.formatIso(at(4)), threadId: id("b") }, 10);
1367 assert.deepEqual(next.threadIds, [id("c"), id("d"), id("snoozed")]);
1368 assert.deepEqual(yield* list(before, 25, []), { threadIds: [], counts: [] });
1369 + yield* assertShellIndexCoversShells();
1370 const scoped = yield* list(before, 0, [ProjectId.make("project:b")]);
1371 assert.deepEqual(scoped, {
1372 threadIds: [],
@@ -3798,6 +3829,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
3829 const sqlShell = (yield* projectionStore.getShellSnapshot()).threads.find(
3830 (row) => row.id === threadId,
3831 )!;
3832 + yield* assertShellIndexCoversShells();
3833 for (const questionShell of [
3834 sqlShell,
3835 ProjectionStore.threadShellFromProjection(projection),
@@ -4860,6 +4892,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
4892 SET status = 'running', payload_json = json_set(payload_json, '$.status', 'running')
4893 WHERE run_id = ${runId}
4894 `;
4895 + yield* assertShellIndexCoversShells();
4896 const secretOnlyShell = (yield* projectionStore.getShellSnapshot()).threads.find(
4897 (thread) => thread.id === threadId,
4898 );
@@ -6466,6 +6499,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
6499 (candidate) => candidate.id === threadId,
6500 );
6501 assert.deepEqual(listed?.pendingBackgroundTasks, shell?.pendingBackgroundTasks);
6502 + yield* assertShellIndexCoversShells();
6503 return shell && projectThreadAwarenessV2({ environmentId, project, thread: shell })?.phase;
6504 });
6505
apps/server/src/orchestration-v2/ProjectionStore.ts
+110
@@ -393,6 +393,15 @@ export interface ShellSnapshotOptions {
393 readonly threadIds?: ReadonlyArray<ThreadId>;
394 }
395
396 +/** What a shell subscriber's trims read from an active thread, without building its shell. */
397 +export interface ActiveShellIndexEntry extends Pick<
398 + OrchestrationV2ThreadShell,
399 + "id" | "projectId" | "lineage" | "settledOverride" | "settledAt" | "pinnedAt" | "snoozedUntil"
400 +> {
401 + /** False only when no run, request, work item, or watch can make the shell live. */
402 + readonly mayBeLive: boolean;
403 +}
404 +
405 export interface OlderSettledThreadsQuery {
406 readonly before: OrchestrationV2OlderSettledCursor;
407 readonly projectIds?: ReadonlyArray<ProjectId> | undefined;
@@ -450,6 +459,13 @@ export interface ProjectionStoreV2Shape {
459 ReadonlyArray<ThreadId>,
460 ProjectionStoreV2Error
461 >;
462 + /** Threads that are not deleted, archived ones included. */
463 + readonly countThreads: () => Effect.Effect<number, ProjectionStoreV2Error>;
464 + /** Every active thread, in shell order. */
465 + readonly listActiveShellIndex: () => Effect.Effect<
466 + ReadonlyArray<ActiveShellIndexEntry>,
467 + ProjectionStoreV2Error
468 + >;
469 /** Older settled thread ids below a cursor, newest settle first, and per-project counts. */
470 readonly listOlderSettledThreads: (
471 query: OlderSettledThreadsQuery,
@@ -6614,6 +6630,98 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6630 Effect.mapError(shellSnapshotReadError),
6631 );
6632
6633 + const countThreads = Effect.fn("ProjectionStore.countThreads")(function* () {
6634 + const rows = yield* sql<{ readonly count: number }>`
6635 + SELECT COUNT(*) AS count FROM orchestration_v2_projection_threads WHERE deleted_at IS NULL
6636 + `;
6637 + return rows[0]?.count ?? 0;
6638 + }, Effect.mapError(shellSnapshotReadError));
6639 +
6640 + const listActiveShellIndex = Effect.fn("ProjectionStore.listActiveShellIndex")(function* () {
6641 + const rows = yield* sql<{
6642 + readonly thread_id: string;
6643 + readonly project_id: string;
6644 + readonly relationship_to_parent: string | null;
6645 + readonly parent_thread_id: string | null;
6646 + readonly root_thread_id: string;
6647 + readonly settled_override: string | null;
6648 + readonly settled_at: string | null;
6649 + readonly pinned_at: string | null;
6650 + readonly snoozed_until: string | null;
6651 + readonly may_be_live: number;
6652 + }>`
6653 + SELECT
6654 + t.thread_id,
6655 + t.project_id,
6656 + json_extract(t.payload_json, '$.lineage.relationshipToParent') AS relationship_to_parent,
6657 + json_extract(t.payload_json, '$.lineage.parentThreadId') AS parent_thread_id,
6658 + json_extract(t.payload_json, '$.lineage.rootThreadId') AS root_thread_id,
6659 + json_extract(t.payload_json, '$.settledOverride') AS settled_override,
6660 + json_extract(t.payload_json, '$.settledAt') AS settled_at,
6661 + json_extract(t.payload_json, '$.pinnedAt') AS pinned_at,
6662 + json_extract(t.payload_json, '$.snoozedUntil') AS snoozed_until,
6663 + -- A superset of isLiveSubagentShell: the sources of its run status, pending request,
6664 + -- and pending background work.
6665 + (
6666 + EXISTS (
6667 + SELECT 1 FROM orchestration_v2_projection_runs r
6668 + WHERE r.thread_id = t.thread_id
6669 + AND r.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')
6670 + )
6671 + OR EXISTS (
6672 + SELECT 1 FROM orchestration_v2_projection_runtime_requests request
6673 + WHERE request.thread_id = t.thread_id AND request.status = 'pending'
6674 + )
6675 + -- Same predicates as the shell's pending item and link reads, for their partial indexes.
6676 + OR EXISTS (
6677 + SELECT 1 FROM orchestration_v2_projection_turn_items item
6678 + WHERE item.thread_id = t.thread_id
6679 + AND item.type IN ('command_execution', 'dynamic_tool', 'subagent')
6680 + AND item.status NOT IN ('completed', 'interrupted', 'failed', 'cancelled')
6681 + )
6682 + OR EXISTS (
6683 + SELECT 1 FROM orchestration_v2_projection_turn_items link_request
6684 + WHERE link_request.thread_id = t.thread_id
6685 + AND link_request.type = 'link_request' AND link_request.status = 'waiting'
6686 + )
6687 + OR EXISTS (
6688 + SELECT 1 FROM orchestration_v2_projection_provider_threads provider_thread
6689 + WHERE provider_thread.thread_id = t.thread_id
6690 + AND json_array_length(provider_thread.payload_json, '$.pendingBackgroundTasks') > 0
6691 + )
6692 + OR EXISTS (
6693 + SELECT 1 FROM json_each(t.payload_json, '$.pullRequests') link
6694 + WHERE json_type(link.value, '$.watch') IS NOT NULL
6695 + )
6696 + ) AS may_be_live
6697 + FROM orchestration_v2_projection_threads t
6698 + WHERE t.deleted_at IS NULL
6699 + AND json_extract(t.payload_json, '$.archivedAt') IS NULL
6700 + ORDER BY t.updated_at ASC, t.thread_id ASC
6701 + `;
6702 + return rows.map((row): ActiveShellIndexEntry => ({
6703 + id: ThreadId.make(row.thread_id),
6704 + projectId: ProjectId.make(row.project_id),
6705 + lineage: {
6706 + parentThreadId:
6707 + row.parent_thread_id === null ? null : ThreadId.make(row.parent_thread_id),
6708 + relationshipToParent:
6709 + row.relationship_to_parent === "fork" || row.relationship_to_parent === "subagent"
6710 + ? row.relationship_to_parent
6711 + : null,
6712 + rootThreadId: ThreadId.make(row.root_thread_id),
6713 + },
6714 + settledOverride:
6715 + row.settled_override === "settled" || row.settled_override === "active"
6716 + ? row.settled_override
6717 + : null,
6718 + settledAt: row.settled_at === null ? null : DateTime.makeUnsafe(row.settled_at),
6719 + pinnedAt: row.pinned_at === null ? null : DateTime.makeUnsafe(row.pinned_at),
6720 + snoozedUntil: row.snoozed_until === null ? null : DateTime.makeUnsafe(row.snoozed_until),
6721 + mayBeLive: row.may_be_live === 1,
6722 + }));
6723 + }, Effect.mapError(shellSnapshotReadError));
6724 +
6725 const findGuestShareThreadId = Effect.fn("ProjectionStore.findGuestShareThreadId")(function* (
6726 shareId: GuestShareId,
6727 ) {
@@ -6768,6 +6876,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6876 apply,
6877 getShellSnapshot: readOnReader(getShellSnapshot),
6878 listActiveSubagentThreadIds: readOnReader(listActiveSubagentThreadIds),
6879 + countThreads: readOnReader(countThreads),
6880 + listActiveShellIndex: readOnReader(listActiveShellIndex),
6881 readShellSnapshot: readOnReader(readShellSnapshot),
6882 listOlderSettledThreads: readOnReader(listOlderSettledThreads),
6883 getThreadShell: readOnReader(getThreadShell),
apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts
+2
@@ -213,6 +213,8 @@ it.effect(
213 getOpenRemoteDelegatedTasks: Effect.die("unused getOpenRemoteDelegatedTasks"),
214 getShellSnapshot: () => Effect.die("unused getShellSnapshot"),
215 listActiveSubagentThreadIds: () => Effect.die("unused listActiveSubagentThreadIds"),
216 + countThreads: () => Effect.die("unused countThreads"),
217 + listActiveShellIndex: () => Effect.die("unused listActiveShellIndex"),
218 listOlderSettledThreads: () => Effect.die("unused listOlderSettledThreads"),
219 readShellSnapshot: () => Effect.die("unused readShellSnapshot"),
220 getThreadShell: () => Effect.die("unused getThreadShell"),
apps/server/src/orchestration-v2/ShellStream.ts
+203 -84
@@ -31,6 +31,7 @@ import type * as SqlClient from "effect/sql/SqlClient";
31 import * as Stream from "effect/Stream";
32
33 import { bufferLatestLiveStream } from "./LiveStreamBudget.ts";
34 +import type { ActiveShellIndexEntry } from "./ProjectionStore.ts";
35
36 type ShellDelta = Extract<OrchestrationV2ShellStreamItem, { readonly sequence: number }>;
37
@@ -56,7 +57,7 @@ export const bufferShellLiveStream = <E, R>(
57 limits,
58 );
59
59 -export function buildActiveShellSnapshot(input: {
60 +function buildActiveShellSnapshot(input: {
61 readonly projects: ReadonlyArray<OrchestrationProjectShell>;
62 readonly threads: OrchestrationV2ThreadShellSnapshot;
63 readonly snapshotSequence: number;
@@ -70,26 +71,119 @@ export function buildActiveShellSnapshot(input: {
71 };
72 }
73
74 +type ShellTrimRow = Pick<
75 + OrchestrationV2ThreadShell,
76 + "id" | "projectId" | "lineage" | "settledOverride" | "settledAt"
77 +>;
78 +
79 /**
74 - * Idle subagent children have no sidebar row, so clients that opt in read them from their thread
75 - * family instead. A child stays while it is live or the parent of another child, so the request
76 - * and working walks over shell rows never meet a gap in a nested chain.
80 + * What a subscriber's trims read across every active row. Computed from full shells or from the
81 + * store's index, so a snapshot read only for the rows it sends trims the same way.
82 */
78 -export function omitIdleSubagentShells(
79 - threads: ReadonlyArray<OrchestrationV2ThreadShell>,
80 -): ReadonlyArray<OrchestrationV2ThreadShell> {
81 - const parentIds = new Set<string>();
82 - for (const thread of threads) {
83 - if (thread.lineage.relationshipToParent === "subagent" && thread.lineage.parentThreadId) {
84 - parentIds.add(thread.lineage.parentThreadId);
83 +interface ShellTrimFacts {
84 + readonly subagentParentIds: ReadonlySet<string>;
85 + readonly subagentThreadIds: Set<string>;
86 + /** Undefined when no thread is settled, so none is older. */
87 + readonly olderSettledBeforeMs: number | undefined;
88 + readonly olderSettledCounts: ReadonlyArray<OrchestrationV2OlderSettledCount>;
89 +}
90 +
91 +const NEWEST_SETTLED_KEPT = 25;
92 +
93 +function shellTrimFacts(rows: ReadonlyArray<ShellTrimRow>): ShellTrimFacts {
94 + const subagentParentIds = new Set<string>();
95 + const subagentThreadIds = new Set<string>();
96 + const settledTimes: Array<number> = [];
97 + for (const row of rows) {
98 + if (row.lineage.relationshipToParent === "subagent") {
99 + subagentThreadIds.add(row.id);
100 + if (row.lineage.parentThreadId) subagentParentIds.add(row.lineage.parentThreadId);
101 + } else if (row.settledOverride === "settled" && row.settledAt !== null) {
102 + settledTimes.push(DateTime.toEpochMillis(row.settledAt));
103 + }
104 + }
105 + settledTimes.sort((left, right) => right - left);
106 + // With fewer than 25, the oldest one is the cut, so nothing is older.
107 + const olderSettledBeforeMs = settledTimes[NEWEST_SETTLED_KEPT - 1] ?? settledTimes.at(-1);
108 + const counts = new Map<ProjectId, number>();
109 + if (olderSettledBeforeMs !== undefined) {
110 + for (const row of rows) {
111 + if (isOlderSettledThreadShell(row, olderSettledBeforeMs)) {
112 + counts.set(row.projectId, (counts.get(row.projectId) ?? 0) + 1);
113 + }
114 }
115 }
116 + return {
117 + subagentParentIds,
118 + subagentThreadIds,
119 + olderSettledBeforeMs,
120 + olderSettledCounts: Array.from(counts, ([projectId, count]) => ({ projectId, count })),
121 + };
122 +}
123 +
124 +function subagentAncestorIds(threads: ReadonlyArray<Pick<OrchestrationV2ThreadShell, "lineage">>) {
125 + const ancestorIds = new Set<string>();
126 + for (const thread of threads) {
127 + if (thread.lineage.relationshipToParent !== "subagent") continue;
128 + if (thread.lineage.parentThreadId !== null) ancestorIds.add(thread.lineage.parentThreadId);
129 + ancestorIds.add(thread.lineage.rootThreadId);
130 + }
131 + return ancestorIds;
132 +}
133 +
134 +function omitIdleSubagentShellsWith(
135 + threads: ReadonlyArray<OrchestrationV2ThreadShell>,
136 + facts: Pick<ShellTrimFacts, "subagentParentIds">,
137 +): ReadonlyArray<OrchestrationV2ThreadShell> {
138 return threads.filter(
139 (thread) =>
140 thread.lineage.relationshipToParent !== "subagent" ||
141 isLiveSubagentShell(thread) ||
91 - parentIds.has(thread.id),
142 + facts.subagentParentIds.has(thread.id),
143 + );
144 +}
145 +
146 +function omitOlderSettledShellsWith(
147 + input: {
148 + readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
149 + readonly snapshotSequence: number;
150 + readonly now: DateTime.Utc;
151 + },
152 + facts: Pick<ShellTrimFacts, "olderSettledBeforeMs" | "olderSettledCounts">,
153 +): {
154 + readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
155 + readonly olderSettled: OrchestrationV2OlderSettledThreads;
156 +} {
157 + const nowMs = DateTime.toEpochMillis(input.now);
158 + const beforeMs = facts.olderSettledBeforeMs ?? nowMs;
159 + const ancestorIds = subagentAncestorIds(input.threads);
160 + const threads = input.threads.filter(
161 + (thread) =>
162 + !isOlderSettledThreadShell(thread, beforeMs) ||
163 + thread.pinnedAt != null ||
164 + (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs) ||
165 + isLiveSubagentShell(thread) ||
166 + ancestorIds.has(thread.id),
167 );
168 + return {
169 + threads,
170 + olderSettled: {
171 + before: DateTime.formatIso(DateTime.makeUnsafe(beforeMs)),
172 + asOfSequence: input.snapshotSequence,
173 + counts: facts.olderSettledCounts,
174 + },
175 + };
176 +}
177 +
178 +/**
179 + * Idle subagent children have no sidebar row, so clients that opt in read them from their thread
180 + * family instead. A child stays while it is live or the parent of another child, so the request
181 + * and working walks over shell rows never meet a gap in a nested chain.
182 + */
183 +export function omitIdleSubagentShells(
184 + threads: ReadonlyArray<OrchestrationV2ThreadShell>,
185 +): ReadonlyArray<OrchestrationV2ThreadShell> {
186 + return omitIdleSubagentShellsWith(threads, shellTrimFacts(threads));
187 }
188
189 /**
@@ -103,18 +197,13 @@ export function activeShellRowsForSubscriber(
197 readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
198 readonly subagentThreadIds: Set<string>;
199 } {
200 + const facts = shellTrimFacts(threads);
201 return {
107 - threads: omitIdleSubagents ? omitIdleSubagentShells(threads) : threads,
108 - subagentThreadIds: new Set(
109 - threads
110 - .filter((thread) => thread.lineage.relationshipToParent === "subagent")
111 - .map((thread) => thread.id),
112 - ),
202 + threads: omitIdleSubagents ? omitIdleSubagentShellsWith(threads, facts) : threads,
203 + subagentThreadIds: facts.subagentThreadIds,
204 };
205 }
206
116 -const NEWEST_SETTLED_KEPT = 25;
117 -
207 /**
208 * Opted-in snapshots keep the 25 newest settled top-level threads (and ties) and count the rest per
209 * project in `olderSettled`; clients page them with `getOlderSettledThreads`. Older rows stay while
@@ -128,77 +217,107 @@ export function omitOlderSettledShells(input: {
217 readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
218 readonly olderSettled: OrchestrationV2OlderSettledThreads;
219 } {
131 - const nowMs = DateTime.toEpochMillis(input.now);
132 - const settledTimes = input.threads
133 - .flatMap((thread) =>
134 - thread.lineage.relationshipToParent !== "subagent" &&
135 - thread.settledOverride === "settled" &&
136 - thread.settledAt !== null
137 - ? [DateTime.toEpochMillis(thread.settledAt)]
220 + return omitOlderSettledShellsWith(input, shellTrimFacts(input.threads));
221 +}
222 +
223 +interface ShellSubscriberOptions {
224 + readonly omitIdleSubagents: boolean;
225 + readonly omitOlderSettled: boolean;
226 +}
227 +
228 +function trimShellSnapshot(
229 + base: OrchestrationV2ShellSnapshot,
230 + facts: ShellTrimFacts,
231 + options: ShellSubscriberOptions & { readonly now: DateTime.Utc },
232 +): OrchestrationV2ShellSnapshot {
233 + const threads = options.omitIdleSubagents
234 + ? omitIdleSubagentShellsWith(base.threads, facts)
235 + : base.threads;
236 + if (!options.omitOlderSettled) return { ...base, threads };
237 + const older = omitOlderSettledShellsWith(
238 + { threads, snapshotSequence: base.snapshotSequence, now: options.now },
239 + facts,
240 + );
241 + return { ...base, threads: older.threads, olderSettled: older.olderSettled };
242 +}
243 +
244 +/**
245 + * The threads whose shells a trimmed snapshot can contain: a superset of what the trims keep, so
246 + * trimming the shells read for these ids gives the snapshot trimming every shell would.
247 + */
248 +function shellSubscriberThreadIds(
249 + index: ReadonlyArray<ActiveShellIndexEntry>,
250 + options: ShellSubscriberOptions,
251 +): ReadonlyArray<ThreadId> {
252 + const facts = shellTrimFacts(index);
253 + const subagents = index.filter(
254 + (entry) =>
255 + entry.lineage.relationshipToParent === "subagent" &&
256 + (!options.omitIdleSubagents || entry.mayBeLive || facts.subagentParentIds.has(entry.id)),
257 + );
258 + const ancestorIds = subagentAncestorIds(subagents);
259 + const keptSubagentIds = new Set(subagents.map((entry) => entry.id));
260 + const beforeMs = facts.olderSettledBeforeMs;
261 + return index.flatMap((entry) =>
262 + entry.lineage.relationshipToParent === "subagent"
263 + ? keptSubagentIds.has(entry.id)
264 + ? [entry.id]
265 + : []
266 + : !options.omitOlderSettled ||
267 + beforeMs === undefined ||
268 + !isOlderSettledThreadShell(entry, beforeMs) ||
269 + entry.pinnedAt != null ||
270 + entry.snoozedUntil != null ||
271 + entry.mayBeLive ||
272 + ancestorIds.has(entry.id)
273 + ? [entry.id]
274 : [],
139 - )
140 - .sort((left, right) => right - left);
141 - // With fewer than 25, the oldest one is the cut, so nothing is older.
142 - const beforeMs = settledTimes[NEWEST_SETTLED_KEPT - 1] ?? settledTimes.at(-1) ?? nowMs;
143 - const ancestorIds = new Set<string>();
144 - for (const thread of input.threads) {
145 - if (thread.lineage.relationshipToParent !== "subagent") continue;
146 - if (thread.lineage.parentThreadId !== null) ancestorIds.add(thread.lineage.parentThreadId);
147 - ancestorIds.add(thread.lineage.rootThreadId);
148 - }
149 - const counts = new Map<ProjectId, number>();
150 - const threads = input.threads.filter((thread) => {
151 - if (!isOlderSettledThreadShell(thread, beforeMs)) return true;
152 - counts.set(thread.projectId, (counts.get(thread.projectId) ?? 0) + 1);
153 - return (
154 - thread.pinnedAt != null ||
155 - (thread.snoozedUntil != null && DateTime.toEpochMillis(thread.snoozedUntil) > nowMs) ||
156 - isLiveSubagentShell(thread) ||
157 - ancestorIds.has(thread.id)
158 - );
159 - });
160 - return {
161 - threads,
162 - olderSettled: {
163 - before: DateTime.formatIso(DateTime.makeUnsafe(beforeMs)),
164 - asOfSequence: input.snapshotSequence,
165 - counts: Array.from(counts, ([projectId, count]) => ({ projectId, count })),
166 - },
167 - };
275 + );
276 }
277
278 /**
171 - * The snapshot a shell subscriber gets for its opt-ins, and the subagent ids whose transcript
172 - * events its stream drops (from every row, so an omitted child that wakes keeps that filter).
279 + * A subscriber's active shell snapshot, read like `loadShellSnapshotParts`. With an opt-in, the
280 + * store's index decides which shells to read, so threads the trims drop are never built.
281 */
174 -export function shellSnapshotForSubscriber(
175 - base: OrchestrationV2ShellSnapshot,
176 - options: {
177 - readonly omitIdleSubagents: boolean;
178 - readonly omitOlderSettled: boolean;
179 - readonly now: DateTime.Utc;
180 - },
181 -): {
182 - readonly snapshot: OrchestrationV2ShellSnapshot;
183 - readonly subagentThreadIds: Set<string>;
184 -} {
185 - const rows = activeShellRowsForSubscriber(base.threads, options.omitIdleSubagents);
186 - if (!options.omitOlderSettled) {
282 +export const loadSubscriberShellSnapshot = Effect.fn("ShellStream.loadSubscriberShellSnapshot")(
283 + function* <E1, E2, E3, E4, E5>(input: {
284 + readonly sql: SqlClient.SqlClient;
285 + readonly listShellIndex: Effect.Effect<ReadonlyArray<ActiveShellIndexEntry>, E1>;
286 + readonly readThreads: (
287 + threadIds: ReadonlyArray<ThreadId> | undefined,
288 + ) => Effect.Effect<Effect.Effect<OrchestrationV2ThreadShellSnapshot, E3>, E2>;
289 + readonly listProjects: Effect.Effect<ReadonlyArray<OrchestrationProjectShell>, E4>;
290 + readonly latestSequence: Effect.Effect<number, E5>;
291 + readonly options: ShellSubscriberOptions;
292 + }) {
293 + const trims = input.options.omitIdleSubagents || input.options.omitOlderSettled;
294 + const read = yield* input.sql
295 + .withTransaction(
296 + Effect.gen(function* () {
297 + const index = trims ? yield* input.listShellIndex : null;
298 + return {
299 + index,
300 + decodeThreads: yield* input.readThreads(
301 + index === null ? undefined : shellSubscriberThreadIds(index, input.options),
302 + ),
303 + projects: yield* input.listProjects,
304 + snapshotSequence: yield* input.latestSequence,
305 + };
306 + }),
307 + )
308 + .pipe(NodeSqliteClient.readOnly);
309 + const base = buildActiveShellSnapshot({
310 + projects: read.projects,
311 + threads: yield* read.decodeThreads,
312 + snapshotSequence: read.snapshotSequence,
313 + });
314 + const facts = shellTrimFacts(read.index ?? base.threads);
315 return {
188 - snapshot: { ...base, threads: rows.threads },
189 - subagentThreadIds: rows.subagentThreadIds,
316 + snapshot: trimShellSnapshot(base, facts, { ...input.options, now: yield* DateTime.now }),
317 + subagentThreadIds: facts.subagentThreadIds,
318 };
191 - }
192 - const older = omitOlderSettledShells({
193 - threads: rows.threads,
194 - snapshotSequence: base.snapshotSequence,
195 - now: options.now,
196 - });
197 - return {
198 - snapshot: { ...base, threads: older.threads, olderSettled: older.olderSettled },
199 - subagentThreadIds: rows.subagentThreadIds,
200 - };
201 -}
319 + },
320 +);
321
322 /**
323 * One transaction covers the reads so they agree; the threads are decoded after it commits,
apps/server/src/orchestration-v2/ThreadLaunchService.ts
+2 -5
@@ -514,12 +514,9 @@ const make = Effect.gen(function* () {
514 // Update parents first so children inherit names; reread each pass for newly updated descendants.
515 const visited = new Set([threadId]);
516 while (true) {
517 - const snapshot = yield* threads.getShellSnapshot();
517 + const snapshot = yield* threads.getShellSnapshot({ worktreePath: worktreeCwd });
518 const sharing = [...snapshot.threads, ...snapshot.archivedThreads].filter(
519 - (thread) =>
520 - !visited.has(thread.id) &&
521 - thread.worktreePath === worktreeCwd &&
522 - thread.branch === oldBranch,
519 + (thread) => !visited.has(thread.id) && thread.branch === oldBranch,
520 );
521 if (sharing.length === 0) break;
522 for (const thread of sharing) {
apps/server/src/orchestration-v2/ThreadManagementService.ts
+10 -3
@@ -394,10 +394,15 @@ export interface ThreadManagementServiceShape {
394 ProjectionRecords<K>,
395 ThreadManagementProjectionLoadError | ThreadManagementThreadNotFoundError
396 >;
397 - readonly getShellSnapshot: (options?: {
398 - readonly location?: "active" | "archive";
399 - }) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
397 + readonly getShellSnapshot: (
398 + options?: Pick<
399 + ShellSnapshotOptions,
400 + "location" | "projectId" | "unsettledOnly" | "worktreePath" | "threadIds"
401 + >,
402 + ) => Effect.Effect<OrchestrationV2ThreadShellSnapshot, Orchestrator.OrchestratorV2Error>;
403 readonly listActiveSubagentThreadIds: Orchestrator.OrchestratorV2["Service"]["listActiveSubagentThreadIds"];
404 + readonly countThreads: Orchestrator.OrchestratorV2["Service"]["countThreads"];
405 + readonly listActiveShellIndex: Orchestrator.OrchestratorV2["Service"]["listActiveShellIndex"];
406 readonly listOlderSettledThreads: Orchestrator.OrchestratorV2["Service"]["listOlderSettledThreads"];
407 readonly readShellSnapshot: (
408 options?: Pick<ShellSnapshotOptions, "location" | "familyOf" | "worktreePath" | "threadIds">,
@@ -1333,6 +1338,8 @@ const make = Effect.gen(function* () {
1338 getProjectThreadRecords,
1339 getShellSnapshot: orchestrator.getShellSnapshot,
1340 listActiveSubagentThreadIds: orchestrator.listActiveSubagentThreadIds,
1341 + countThreads: orchestrator.countThreads,
1342 + listActiveShellIndex: orchestrator.listActiveShellIndex,
1343 listOlderSettledThreads: orchestrator.listOlderSettledThreads,
1344 readShellSnapshot: orchestrator.readShellSnapshot,
1345 getThreadShell: orchestrator.getThreadShell,
apps/server/src/orchestration-v2/http.ts
+13 -18
@@ -3,7 +3,6 @@ import {
3 EnvironmentHttpApi,
4 type OrchestrationProjectShell,
5 } from "@t3tools/contracts";
6 -import * as DateTime from "effect/DateTime";
6 import * as Effect from "effect/Effect";
7 import * as Result from "effect/Result";
8 import * as Predicate from "effect/Predicate";
@@ -32,11 +31,7 @@ import {
31 } from "./threadHistoryPaging.ts";
32 import * as ThreadManagementService from "./ThreadManagementService.ts";
33 import * as ProjectStore from "./ProjectStore.ts";
35 -import {
36 - buildActiveShellSnapshot,
37 - loadShellSnapshotParts,
38 - shellSnapshotForSubscriber,
39 -} from "./ShellStream.ts";
34 +import { loadSubscriberShellSnapshot } from "./ShellStream.ts";
35 import { boundedSnapshotResponseFields } from "./ThreadStream.ts";
36 import { projectThreadProjectionForWire } from "./WireProjection.ts";
37
@@ -84,19 +79,19 @@ export const layer = HttpApiBuilder.group(
79 readonly omitIdleSubagents: boolean;
80 readonly omitOlderSettled: boolean;
81 }) {
87 - const base = buildActiveShellSnapshot(
88 - yield* loadShellSnapshotParts({
89 - sql,
90 - readThreads: threadManagement.readShellSnapshot({ location: "active" }),
91 - listProjects: projectStore.listShells(),
92 - latestSequence: applicationEvents.latestApplicationSequence,
93 - }),
94 - );
95 - const { snapshot } = shellSnapshotForSubscriber(base, {
96 - ...options,
97 - now: yield* DateTime.now,
82 + const { snapshot } = yield* loadSubscriberShellSnapshot({
83 + sql,
84 + listShellIndex: threadManagement.listActiveShellIndex(),
85 + readThreads: (threadIds) =>
86 + threadManagement.readShellSnapshot({
87 + location: "active",
88 + ...(threadIds === undefined ? {} : { threadIds }),
89 + }),
90 + listProjects: projectStore.listShells(),
91 + latestSequence: applicationEvents.latestApplicationSequence,
92 + options,
93 });
99 - const projects = yield* enrichProjectShells(base.projects);
94 + const projects = yield* enrichProjectShells(snapshot.projects);
95 return { ...snapshot, projects };
96 },
97 );
apps/server/src/project/ProjectService.ts
+2 -4
@@ -457,15 +457,13 @@ export const make = Effect.gen(function* () {
457 ) {
458 const { projectId } = input;
459 const snapshot = yield* threadProjections
460 - .getShellSnapshot()
460 + .getShellSnapshot({ projectId })
461 .pipe(
462 Effect.mapError(
463 (cause) => new ProjectOperationError({ operation: "list-threads", projectId, cause }),
464 ),
465 );
466 - const projectThreads = [...snapshot.threads, ...snapshot.archivedThreads].filter(
467 - (thread) => thread.projectId === projectId,
468 - );
466 + const projectThreads = [...snapshot.threads, ...snapshot.archivedThreads];
467 if (projectThreads.length > 0 && input.force !== true) {
468 return yield* new ProjectNotEmptyError({ projectId });
469 }
apps/server/src/relay/AgentAwarenessRelay.test.ts
+2
@@ -190,6 +190,8 @@ const makeTestRelay = Effect.fnUntraced(function* (
190 return { schemaVersion: 2, snapshotSequence: 1, threads: [], archivedThreads: [] };
191 }),
192 listActiveSubagentThreadIds: unused,
193 + countThreads: unused,
194 + listActiveShellIndex: () => Effect.succeed([]),
195 listOlderSettledThreads: unused,
196 readShellSnapshot: unused,
197 ensureLegacyTranscript: unused,
apps/server/src/relay/AgentAwarenessRelay.ts
+9 -4
@@ -726,10 +726,15 @@ export const make = Effect.gen(function* () {
726 return "unlinked" as const;
727 }
728 const environmentId = yield* serverEnvironment.getEnvironmentId;
729 - const [projectSnapshot, shellSnapshot] = yield* Effect.all([
730 - projects.snapshot,
731 - threads.getShellSnapshot(),
732 - ]);
729 + const projectSnapshot = yield* projects.snapshot;
730 + // Subagents never publish awareness, and they are most threads.
731 + const topLevelThreadIds = (yield* threads.listActiveShellIndex()).flatMap((thread) =>
732 + thread.lineage.relationshipToParent === "subagent" ? [] : [thread.id],
733 + );
734 + const shellSnapshot = yield* threads.getShellSnapshot({
735 + location: "active",
736 + threadIds: topLevelThreadIds,
737 + });
738 const activeThreadIds = resolveAgentAwarenessRelayActiveThreadIds({
739 environmentId,
740 startedAt,
apps/server/src/serverRuntimeStartup.autoPull.test.ts
+1 -7
@@ -141,13 +141,7 @@ it.effect("parks automatic pull until activation without delaying command readin
141 }),
142 Layer.mock(ProjectService.ProjectService)({ snapshot: Effect.succeed(snapshot) }),
143 Layer.mock(ThreadManagement.ThreadManagementService)({
144 - getShellSnapshot: () =>
145 - Effect.succeed({
146 - schemaVersion: 1,
147 - snapshotSequence: 0,
148 - threads: [],
149 - archivedThreads: [],
150 - }),
144 + countThreads: () => Effect.succeed(0),
145 }),
146 Layer.mock(ThreadLaunch.ThreadLaunchService)({}),
147 Layer.mock(ServiceLauncherClient.ServiceLauncherClient)({
apps/server/src/serverRuntimeStartup.test.ts
+2 -7
@@ -176,13 +176,8 @@ it.effect.each([
176 }),
177 }),
178 Layer.mock(ThreadManagement.ThreadManagementService)({
179 - getShellSnapshot: () =>
180 - Effect.succeed({
181 - threads: [],
182 - archivedThreads: [],
183 - schemaVersion: 1,
184 - snapshotSequence: 0,
185 - }),
179 + countThreads: () => Effect.succeed(0),
180 + listActiveShellIndex: () => Effect.succeed([]),
181 }),
182 Layer.mock(ThreadLaunch.ThreadLaunchService)({
183 launch: (input) => {
apps/server/src/serverRuntimeStartup.ts
+4 -5
@@ -153,11 +153,11 @@ const recordStartupHeartbeat = Effect.gen(function* () {
153
154 const { threadCount, projectCount } = yield* Effect.all({
155 projects: projects.snapshot,
156 - threads: threads.getShellSnapshot(),
156 + threadCount: threads.countThreads(),
157 }).pipe(
158 - Effect.map(({ projects: projectSnapshot, threads: shellSnapshot }) => ({
158 + Effect.map(({ projects: projectSnapshot, threadCount }) => ({
159 projectCount: projectSnapshot.projects.length,
160 - threadCount: shellSnapshot.threads.length + shellSnapshot.archivedThreads.length,
160 + threadCount,
161 })),
162 Effect.catch((cause) =>
163 Effect.logWarning("failed to gather V2 startup counts for telemetry", {
@@ -279,8 +279,7 @@ const resolveAutoBootstrapWelcomeTargets = Effect.gen(function* () {
279 title: path.basename(serverConfig.cwd) || "project",
280 workspaceRoot: serverConfig.cwd,
281 });
282 - const shell = yield* threads.getShellSnapshot();
283 - const existingThread = shell.threads.find(
282 + const existingThread = (yield* threads.listActiveShellIndex()).find(
283 (thread) =>
284 thread.projectId === project.id && thread.lineage.relationshipToParent !== "subagent",
285 );
apps/server/src/ws.test.ts
+13 -2
@@ -412,12 +412,23 @@ it.effect("shell snapshots omit idle children and older settled threads only on
412 Effect.flatMap((stream) => stream.pipe(Stream.take(1), Stream.runCollect)),
413 Effect.map(([item]) => (item?.kind === "snapshot" ? item.snapshot : null)),
414 Effect.provideService(ThreadManagement.ThreadManagementService, {
415 - readShellSnapshot: () =>
415 + listActiveShellIndex: () =>
416 + Effect.succeed(
417 + threads.map((thread) => ({
418 + ...thread,
419 + pinnedAt: null,
420 + snoozedUntil: null,
421 + mayBeLive: thread.activityRunStatus !== null,
422 + })),
423 + ),
424 + readShellSnapshot: (options?: { readonly threadIds?: ReadonlyArray<string> }) =>
425 Effect.succeed(
426 Effect.succeed({
427 schemaVersion: 1,
428 snapshotSequence: 0,
420 - threads,
429 + threads: threads.filter(
430 + (thread) => options?.threadIds?.includes(thread.id) ?? true,
431 + ),
432 archivedThreads: [],
433 }),
434 ),
apps/server/src/ws.ts
+16 -15
@@ -124,7 +124,6 @@ import * as ScheduledTasks from "./scheduledTasks/ScheduledTaskService.ts";
124 import * as SecretRequests from "./secrets/SecretRequests.ts";
125 import * as GuestShareService from "./guest/GuestShareService.ts";
126 import {
127 - buildActiveShellSnapshot,
127 bufferShellLiveStream,
128 coalesceShellApplicationEvents,
129 composeShellStreamWithEnrichment,
@@ -134,7 +133,7 @@ import {
133 shellStreamItemFromEnrichmentRefresh,
134 shellStreamItemFromThreadShell,
135 shellStreamItemsFromInitialSnapshot,
137 - shellSnapshotForSubscriber,
136 + loadSubscriberShellSnapshot,
137 shellStreamItemsFromResumeSnapshot,
138 shouldPublishShellApplicationEvent,
139 skipUnchangedThreadShells,
@@ -1010,21 +1009,23 @@ export const subscribeOrchestrationV2Shell = Effect.fn("ws.orchestrationV2.subsc
1009 },
1010 );
1011 const loadSnapshot = Effect.fn("ws.orchestrationV2.loadShellSnapshot")(function* () {
1013 - const base = buildActiveShellSnapshot(
1014 - yield* loadShellSnapshotParts({
1015 - sql,
1016 - readThreads: threadManagement.readShellSnapshot({ location: "active" }),
1017 - listProjects: projects.listShells(),
1018 - latestSequence: applicationEvents.latestApplicationSequence,
1019 - }),
1020 - );
1021 - const forSubscriber = shellSnapshotForSubscriber(base, {
1022 - omitIdleSubagents: input.omitIdleSubagents === true,
1023 - omitOlderSettled: input.omitOlderSettled === true,
1024 - now: yield* DateTime.now,
1012 + const forSubscriber = yield* loadSubscriberShellSnapshot({
1013 + sql,
1014 + listShellIndex: threadManagement.listActiveShellIndex(),
1015 + readThreads: (threadIds) =>
1016 + threadManagement.readShellSnapshot({
1017 + location: "active",
1018 + ...(threadIds === undefined ? {} : { threadIds }),
1019 + }),
1020 + listProjects: projects.listShells(),
1021 + latestSequence: applicationEvents.latestApplicationSequence,
1022 + options: {
1023 + omitIdleSubagents: input.omitIdleSubagents === true,
1024 + omitOlderSettled: input.omitOlderSettled === true,
1025 + },
1026 });
1027 subagentThreadIds = forSubscriber.subagentThreadIds;
1027 - const enriched = yield* enrichProjectShells(base.projects);
1028 + const enriched = yield* enrichProjectShells(forSubscriber.snapshot.projects);
1029 return {
1030 snapshot: {
1031 ...forSubscriber.snapshot,