Skip to content

Commit eb84f0c

Browse files
perf(client): resume completed shells and deduplicate repeated links
Co-authored-by: maria-rcks <[email protected]>
1 parent 776ec70 commit eb84f0c

9 files changed

Lines changed: 499 additions & 154 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
OrchestrationV2ThreadShell as ThreadShellSchema,
1414
} from "@t3tools/contracts";
1515
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
16+
import { stableStringify } from "@t3tools/shared/relaySigning";
1617
import * as Clock from "effect/Clock";
1718
import * as Effect from "effect/Effect";
1819
import * as Schema from "effect/Schema";
@@ -33,6 +34,24 @@ export function buildActiveShellSnapshot(input: {
3334
};
3435
}
3536

37+
export function deduplicateShellPullRequests(
38+
snapshot: OrchestrationV2ShellSnapshot,
39+
): OrchestrationV2ShellSnapshot {
40+
const firstRowByLinks = new Map<string, number>();
41+
const threads = snapshot.threads.map((thread, index) => {
42+
if (thread.pullRequests === undefined || thread.pullRequests.length === 0) return thread;
43+
const key = stableStringify(thread.pullRequests);
44+
const reference = firstRowByLinks.get(key);
45+
if (reference === undefined) {
46+
firstRowByLinks.set(key, index);
47+
return thread;
48+
}
49+
const { pullRequests: _links, ...row } = thread;
50+
return { ...row, pullRequestReference: reference };
51+
});
52+
return { ...snapshot, threads };
53+
}
54+
3655
/**
3756
* One transaction covers the reads so they agree; the threads are decoded after it commits,
3857
* because the server shares one SQLite connection and decoding a large shell takes longer than

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

Lines changed: 136 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
ORCHESTRATION_PROTOCOL_HEADER,
1010
ORCHESTRATION_PROTOCOL_VERSION_TEXT,
1111
OrchestrationV2AppThread,
12+
OrchestrationV2ShellSnapshot,
13+
OrchestrationV2ShellSnapshotJson,
14+
type ThreadPullRequestLink,
1215
OrchestrationV2TurnItem,
1316
OrchestrationV2ThreadTranscript,
1417
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
@@ -41,13 +44,21 @@ import * as ThreadManagementService from "./ThreadManagementService.ts";
4144
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
4245
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
4346

44-
class TranscriptTestApi extends HttpApi.make("environment").add(
47+
class OrchestrationTestApi extends HttpApi.make("environment").add(
4548
EnvironmentHttpApi.groups.orchestration,
4649
) {}
4750
const decodeTranscript = Schema.decodeUnknownEffect(
4851
Schema.toCodecJson(OrchestrationV2ThreadTranscript),
4952
);
5053

54+
const decodeShellSnapshot = Schema.decodeUnknownEffect(
55+
Schema.toCodecJson(OrchestrationV2ShellSnapshot),
56+
);
57+
58+
const decodeShellSnapshotWire = Schema.decodeUnknownEffect(
59+
Schema.toEncoded(OrchestrationV2ShellSnapshotJson),
60+
);
61+
5162
const now = DateTime.makeUnsafe("2026-09-29T00:00:00.000Z");
5263
const environmentId = EnvironmentId.make("source-بيئة");
5364
const output = "Full tool output. ".repeat(4_000);
@@ -96,27 +107,69 @@ const items = Schema.decodeUnknownSync(Schema.Array(OrchestrationV2TurnItem))(
96107
})),
97108
);
98109

99-
it.effect("exports full history with size, scope, and missing-thread checks", () =>
100-
Effect.gen(function* () {
101-
let allowed = true;
102-
const runtime = ProviderReplayHarness.layerWithRegistry(
103-
{ name: "thread-transcript" },
104-
ProviderAdapterRegistry.layerFromAdapters([]),
105-
{ runEffectWorker: false },
106-
);
107-
const services = yield* Layer.build(
108-
Layer.mergeAll(
109-
runtime,
110-
ThreadManagementService.layer.pipe(
111-
Layer.provide(
112-
Layer.mergeAll(runtime, CommandReceiptStore.layer, ServerSettings.layerTest({})),
110+
const makeHttpHarness = Effect.fnUntraced(function* (allowed = () => true) {
111+
const runtime = ProviderReplayHarness.layerWithRegistry(
112+
{ name: "orchestration-http" },
113+
ProviderAdapterRegistry.layerFromAdapters([]),
114+
{ runEffectWorker: false },
115+
);
116+
const services = yield* Layer.build(
117+
Layer.mergeAll(
118+
runtime,
119+
ThreadManagementService.layer.pipe(
120+
Layer.provide(
121+
Layer.mergeAll(runtime, CommandReceiptStore.layer, ServerSettings.layerTest({})),
122+
),
123+
),
124+
OrchestrationEventStore.layer,
125+
ProjectStore.layer,
126+
).pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
127+
);
128+
const eventSink = Context.get(services, EventSinkV2);
129+
const routes = HttpApiBuilder.layer(OrchestrationTestApi).pipe(
130+
Layer.provide(OrchestrationHttp.layer),
131+
Layer.provide(
132+
Layer.succeed(ServerEnvironment.ServerEnvironmentIdentity, {
133+
getEnvironmentId: Effect.succeed(environmentId),
134+
}),
135+
),
136+
Layer.provide(
137+
Layer.succeed(EnvironmentAuthenticatedAuth, (effect) =>
138+
Effect.suspend(() =>
139+
effect.pipe(
140+
Effect.provideService(EnvironmentAuthenticatedPrincipal, {
141+
sessionId: AuthSessionId.make("test-session"),
142+
subject: "test",
143+
method: "bearer-access-token",
144+
scopes: new Set<AuthEnvironmentScope>(allowed() ? ["orchestration:read"] : []),
145+
}),
113146
),
114147
),
115-
OrchestrationEventStore.layer,
116-
ProjectStore.layer,
117-
).pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
148+
),
149+
),
150+
Layer.provide(Layer.succeedContext(services)),
151+
Layer.provide(Layer.mock(ProjectEnrichmentService)({})),
152+
Layer.provide(HttpPlatform.layer),
153+
Layer.provide(Etag.layerWeak),
154+
Layer.provide(NodeServices.layer),
155+
);
156+
const { handler, dispose } = HttpRouter.toWebHandler(routes, { disableLogger: true });
157+
yield* Effect.addFinalizer(() => Effect.promise(dispose));
158+
const request = (path: string) =>
159+
Effect.promise(() =>
160+
handler(
161+
new Request(`http://localhost${path}`, {
162+
headers: { [ORCHESTRATION_PROTOCOL_HEADER]: ORCHESTRATION_PROTOCOL_VERSION_TEXT },
163+
}),
164+
),
118165
);
119-
const eventSink = Context.get(services, EventSinkV2);
166+
return { services, eventSink, request };
167+
});
168+
169+
it.effect("exports full history with size, scope, and missing-thread checks", () =>
170+
Effect.gen(function* () {
171+
let allowed = true;
172+
const { services, eventSink, request } = yield* makeHttpHarness(() => allowed);
120173
yield* eventSink.write({
121174
events: [
122175
{
@@ -128,43 +181,8 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
128181
},
129182
],
130183
});
131-
const routes = HttpApiBuilder.layer(TranscriptTestApi).pipe(
132-
Layer.provide(OrchestrationHttp.layer),
133-
Layer.provide(
134-
Layer.succeed(ServerEnvironment.ServerEnvironmentIdentity, {
135-
getEnvironmentId: Effect.succeed(environmentId),
136-
}),
137-
),
138-
Layer.provide(
139-
Layer.succeed(EnvironmentAuthenticatedAuth, (effect) =>
140-
Effect.suspend(() =>
141-
effect.pipe(
142-
Effect.provideService(EnvironmentAuthenticatedPrincipal, {
143-
sessionId: AuthSessionId.make("test-session"),
144-
subject: "test",
145-
method: "bearer-access-token",
146-
scopes: new Set<AuthEnvironmentScope>(allowed ? ["orchestration:read"] : []),
147-
}),
148-
),
149-
),
150-
),
151-
),
152-
Layer.provide(Layer.succeedContext(services)),
153-
Layer.provide(Layer.mock(ProjectEnrichmentService)({})),
154-
Layer.provide(HttpPlatform.layer),
155-
Layer.provide(Etag.layerWeak),
156-
Layer.provide(NodeServices.layer),
157-
);
158-
const { handler, dispose } = HttpRouter.toWebHandler(routes, { disableLogger: true });
159-
yield* Effect.addFinalizer(() => Effect.promise(dispose));
160184
const read = (threadId: ThreadId) =>
161-
Effect.promise(() =>
162-
handler(
163-
new Request(`http://localhost/api/orchestration/threads/${threadId}/transcript`, {
164-
headers: { [ORCHESTRATION_PROTOCOL_HEADER]: ORCHESTRATION_PROTOCOL_VERSION_TEXT },
165-
}),
166-
),
167-
);
185+
request(`/api/orchestration/threads/${threadId}/transcript`);
168186

169187
const emptyResponse = yield* read(thread.id);
170188
expect(emptyResponse.status).toBe(200);
@@ -242,3 +260,66 @@ it.effect("exports full history with size, scope, and missing-thread checks", ()
242260
expect((yield* read(thread.id)).status).toBe(403);
243261
}),
244262
);
263+
264+
it.effect("deduplicates equal pull request lists only in opted-in HTTP shell snapshots", () =>
265+
Effect.gen(function* () {
266+
const { eventSink, request } = yield* makeHttpHarness();
267+
const link = (number: number): ThreadPullRequestLink => ({
268+
host: "github.com",
269+
repository: "pingdotgg/t3code",
270+
number,
271+
url: `https://github.com/pingdotgg/t3code/pull/${number}`,
272+
source: "agent",
273+
linkedAt: "2026-09-29T00:00:00.000Z",
274+
snapshot: null,
275+
stack: null,
276+
});
277+
const lists = [[link(1), link(2)], [link(1), link(2)], [link(1)]];
278+
yield* eventSink.write({
279+
events: lists.map((pullRequests, index) => {
280+
const id = ThreadId.make(`shell-${index}`);
281+
return {
282+
id: EventId.make(`create-${id}`),
283+
type: "thread.created" as const,
284+
threadId: id,
285+
occurredAt: now,
286+
payload: { ...thread, id, pullRequests },
287+
};
288+
}),
289+
});
290+
291+
const defaultResponse = yield* request("/api/orchestration/shell");
292+
expect(defaultResponse.status).toBe(200);
293+
const json = yield* Effect.promise(() => defaultResponse.json());
294+
const full = yield* decodeShellSnapshotWire(json);
295+
const decoded = yield* decodeShellSnapshot(json);
296+
expect(decoded.threads).toHaveLength(3);
297+
for (const [index, links] of lists.entries()) {
298+
expect(decoded.threads.find((row) => row.id === `shell-${index}`)?.pullRequests).toEqual(
299+
links,
300+
);
301+
}
302+
expect(full.threads.every((row) => !("pullRequestReference" in row))).toBe(true);
303+
304+
const response = yield* request("/api/orchestration/shell?deduplicatePullRequests=true");
305+
expect(response.status).toBe(200);
306+
const compact = yield* Effect.promise(() => response.json());
307+
const duplicateIndices = decoded.threads.flatMap((row, index) =>
308+
row.id === "shell-0" || row.id === "shell-1" ? [index] : [],
309+
);
310+
const [first, duplicate] = duplicateIndices;
311+
expect(first).toBeDefined();
312+
expect(duplicate).toBeDefined();
313+
expect(compact).toEqual({
314+
...full,
315+
threads: full.threads.map((row, index) => {
316+
if (index !== duplicate) return row;
317+
const { pullRequests: _links, ...rest } = row;
318+
return { ...rest, pullRequestReference: first };
319+
}),
320+
});
321+
const disabled = yield* request("/api/orchestration/shell?deduplicatePullRequests=false");
322+
expect(disabled.status).toBe(200);
323+
expect(yield* Effect.promise(() => disabled.json())).toEqual(full);
324+
}),
325+
);

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ import {
3030
} from "./threadHistoryPaging.ts";
3131
import * as ThreadManagementService from "./ThreadManagementService.ts";
3232
import * as ProjectStore from "./ProjectStore.ts";
33-
import { buildActiveShellSnapshot, loadShellSnapshotParts } from "./ShellStream.ts";
33+
import {
34+
buildActiveShellSnapshot,
35+
deduplicateShellPullRequests,
36+
loadShellSnapshotParts,
37+
} from "./ShellStream.ts";
3438
import { boundedSnapshotResponseFields } from "./ThreadStream.ts";
3539
import { projectThreadProjectionForWire } from "./WireProjection.ts";
3640

@@ -75,7 +79,9 @@ export const layer = HttpApiBuilder.group(
7579
),
7680
);
7781

78-
const loadShellSnapshot = Effect.fn("http.orchestration.loadShellSnapshot")(function* () {
82+
const loadShellSnapshot = Effect.fn("http.orchestration.loadShellSnapshot")(function* (
83+
deduplicatePullRequests: boolean,
84+
) {
7985
const base = buildActiveShellSnapshot(
8086
yield* loadShellSnapshotParts({
8187
sql,
@@ -85,7 +91,8 @@ export const layer = HttpApiBuilder.group(
8591
}),
8692
);
8793
const projects = yield* enrichProjectShells(base.projects);
88-
return { ...base, projects };
94+
const snapshot = { ...base, projects };
95+
return deduplicatePullRequests ? deduplicateShellPullRequests(snapshot) : snapshot;
8996
});
9097

9198
const loadThreadSnapshot = Effect.fn("http.orchestration.loadThreadSnapshot")(function* (
@@ -154,7 +161,7 @@ export const layer = HttpApiBuilder.group(
154161
Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
155162
yield* annotateEnvironmentRequest(args.endpoint.name);
156163
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
157-
return yield* loadShellSnapshot().pipe(
164+
return yield* loadShellSnapshot(args.query.deduplicatePullRequests === "true").pipe(
158165
traceLocalHandlerWork,
159166
Effect.catch((cause) =>
160167
failEnvironmentInternal("orchestration_snapshot_failed", cause),

0 commit comments

Comments
 (0)