Skip to content
This repository was archived by the owner on Oct 10, 2026. It is now read-only.

Commit 5fcdefd

Browse files
t3dotggclaude
andauthored
perf(server): trim stale context-window rows and drop dead replay RPC (pingdotgg#4791)
Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 38a6e3c commit 5fcdefd

6 files changed

Lines changed: 179 additions & 184 deletions

File tree

‎apps/server/src/orchestration/ActivityPayloadProjection.ts‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,62 @@ export function projectActivityPayload(
201201
};
202202
}
203203

204+
/**
205+
* Matches the validity rule in the web client's
206+
* `deriveLatestContextWindowSnapshot`: rows without a finite, non-negative
207+
* `usedTokens` are skipped during its backward walk, so they must not shadow
208+
* an earlier resolvable row here.
209+
*/
210+
function isResolvableContextWindowActivity(activity: OrchestrationThreadActivity): boolean {
211+
if (activity.kind !== "context-window.updated") {
212+
return false;
213+
}
214+
const payload = asRecord(activity.payload);
215+
const usedTokens = payload?.usedTokens;
216+
return typeof usedTokens === "number" && Number.isFinite(usedTokens) && usedTokens >= 0;
217+
}
218+
219+
/**
220+
* Drops all but the last resolvable context-window activity per turn from a
221+
* snapshot. Clients only ever read the latest usage value (walking the array
222+
* backwards), so shipping the full history — often thousands of rows on long
223+
* threads — buys nothing. Retention is per turn rather than per thread because
224+
* a live `thread.reverted` makes the client discard whole turns; keeping each
225+
* turn's latest row means the meter can still resolve a value from the turns
226+
* that survive. Malformed rows pass through untouched rather than shadowing a
227+
* valid earlier row. Live `thread.activity-appended` events are untouched:
228+
* newer updates still stream through and supersede the retained rows on the
229+
* client.
230+
*/
231+
function dropStaleContextWindowActivities(
232+
activities: ReadonlyArray<OrchestrationThreadActivity>,
233+
): ReadonlyArray<OrchestrationThreadActivity> {
234+
const latestIndexByTurn = new Map<string | null, number>();
235+
for (let index = 0; index < activities.length; index += 1) {
236+
if (isResolvableContextWindowActivity(activities[index]!)) {
237+
latestIndexByTurn.set(activities[index]!.turnId, index);
238+
}
239+
}
240+
if (latestIndexByTurn.size === 0) {
241+
return activities;
242+
}
243+
return activities.filter(
244+
(activity, index) =>
245+
!isResolvableContextWindowActivity(activity) ||
246+
latestIndexByTurn.get(activity.turnId) === index,
247+
);
248+
}
249+
204250
export function projectThreadDetailSnapshot(
205251
snapshot: OrchestrationThreadDetailSnapshot,
206252
): OrchestrationThreadDetailSnapshot {
207253
return {
208254
...snapshot,
209255
thread: {
210256
...snapshot.thread,
211-
activities: snapshot.thread.activities.map(projectActivityPayload),
257+
activities: dropStaleContextWindowActivities(snapshot.thread.activities).map(
258+
projectActivityPayload,
259+
),
212260
},
213261
};
214262
}

‎apps/server/src/server.test.ts‎

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -5690,15 +5690,6 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
56905690
),
56915691
);
56925692
assert.equal(fullDiffResult.diff, "full-diff");
5693-
5694-
const replayResult = yield* Effect.scoped(
5695-
withWsRpcClient(wsUrl, (client) =>
5696-
client[ORCHESTRATION_WS_METHODS.replayEvents]({
5697-
fromSequenceExclusive: 0,
5698-
}),
5699-
),
5700-
);
5701-
assert.deepEqual(replayResult, []);
57025693
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
57035694
);
57045695

@@ -6361,73 +6352,6 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
63616352
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
63626353
);
63636354

6364-
it.effect("enriches replayed project events with repository identity metadata", () =>
6365-
Effect.gen(function* () {
6366-
const repositoryIdentity = {
6367-
canonicalKey: "github.com/t3tools/t3code",
6368-
locator: {
6369-
source: "git-remote" as const,
6370-
remoteName: "origin",
6371-
remoteUrl: "[email protected]:T3Tools/t3code.git",
6372-
},
6373-
displayName: "T3Tools/t3code",
6374-
provider: "github",
6375-
owner: "T3Tools",
6376-
name: "t3code",
6377-
};
6378-
6379-
yield* buildAppUnderTest({
6380-
layers: {
6381-
orchestrationEngine: {
6382-
readEvents: (_fromSequenceExclusive) =>
6383-
Stream.make({
6384-
sequence: 1,
6385-
eventId: EventId.make("event-1"),
6386-
aggregateKind: "project",
6387-
aggregateId: defaultProjectId,
6388-
occurredAt: "2026-04-05T00:00:00.000Z",
6389-
commandId: null,
6390-
causationEventId: null,
6391-
correlationId: null,
6392-
metadata: {},
6393-
type: "project.created",
6394-
payload: {
6395-
projectId: defaultProjectId,
6396-
title: "Default Project",
6397-
workspaceRoot: "/tmp/default-project",
6398-
defaultModelSelection,
6399-
scripts: [],
6400-
createdAt: "2026-04-05T00:00:00.000Z",
6401-
updatedAt: "2026-04-05T00:00:00.000Z",
6402-
},
6403-
} satisfies Extract<OrchestrationEvent, { type: "project.created" }>),
6404-
},
6405-
repositoryIdentityResolver: {
6406-
resolve: () => Effect.succeed(repositoryIdentity),
6407-
},
6408-
},
6409-
});
6410-
6411-
const wsUrl = yield* getWsServerUrl("/ws");
6412-
const replayResult = yield* Effect.scoped(
6413-
withWsRpcClient(wsUrl, (client) =>
6414-
client[ORCHESTRATION_WS_METHODS.replayEvents]({
6415-
fromSequenceExclusive: 0,
6416-
}),
6417-
),
6418-
);
6419-
6420-
const replayedEvent = replayResult[0];
6421-
assert.equal(replayedEvent?.type, "project.created");
6422-
assert.deepEqual(
6423-
replayedEvent && replayedEvent.type === "project.created"
6424-
? replayedEvent.payload.repositoryIdentity
6425-
: null,
6426-
repositoryIdentity,
6427-
);
6428-
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
6429-
);
6430-
64316355
it.effect("stops the provider session and closes thread terminals after archive", () =>
64326356
Effect.gen(function* () {
64336357
const threadId = ThreadId.make("thread-archive");

‎apps/server/src/ws.ts‎

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import {
4646
ProjectWriteFileError,
4747
RelayClientInstallFailedError,
4848
type RelayClientInstallProgressEvent,
49-
OrchestrationReplayEventsError,
5049
type FilesystemBrowseFailure,
5150
FilesystemBrowseError,
5251
AssetWorkspaceContextNotFoundError,
@@ -60,7 +59,6 @@ import {
6059
WS_METHODS,
6160
WsRpcGroup,
6261
} from "@t3tools/contracts";
63-
import { clamp } from "effect/Number";
6462
import { HttpRouter, HttpServerRequest, HttpServerRespondable } from "effect/unstable/http";
6563
import { RpcSerialization, RpcServer } from "effect/unstable/rpc";
6664

@@ -99,7 +97,6 @@ import * as VcsProvisioningService from "./vcs/VcsProvisioningService.ts";
9997
import * as GitWorkflowService from "./git/GitWorkflowService.ts";
10098
import * as ReviewService from "./review/ReviewService.ts";
10199
import * as ProjectSetupScriptRunner from "./project/ProjectSetupScriptRunner.ts";
102-
import * as RepositoryIdentityResolver from "./project/RepositoryIdentityResolver.ts";
103100
import * as ServerEnvironment from "./environment/ServerEnvironment.ts";
104101
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
105102
import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts";
@@ -302,7 +299,6 @@ const RPC_REQUIRED_SCOPE = new Map<string, AuthEnvironmentScope>([
302299
[ORCHESTRATION_WS_METHODS.dispatchCommand, AuthOrchestrationOperateScope],
303300
[ORCHESTRATION_WS_METHODS.getTurnDiff, AuthOrchestrationReadScope],
304301
[ORCHESTRATION_WS_METHODS.getFullThreadDiff, AuthOrchestrationReadScope],
305-
[ORCHESTRATION_WS_METHODS.replayEvents, AuthOrchestrationReadScope],
306302
[ORCHESTRATION_WS_METHODS.subscribeShell, AuthOrchestrationReadScope],
307303
[ORCHESTRATION_WS_METHODS.getArchivedShellSnapshot, AuthOrchestrationReadScope],
308304
[ORCHESTRATION_WS_METHODS.subscribeThread, AuthOrchestrationReadScope],
@@ -441,8 +437,6 @@ const makeWsRpcLayer = (
441437
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
442438
const workspaceFileSystem = yield* WorkspaceFileSystem.WorkspaceFileSystem;
443439
const projectSetupScriptRunner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
444-
const repositoryIdentityResolver =
445-
yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
446440
const serverEnvironment = yield* ServerEnvironment.ServerEnvironment;
447441
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
448442
const sourceControlDiscovery = yield* SourceControlDiscovery.SourceControlDiscovery;
@@ -592,53 +586,6 @@ const makeWsRpcLayer = (
592586
});
593587
};
594588

595-
const enrichProjectEvent = (
596-
event: OrchestrationEvent,
597-
): Effect.Effect<OrchestrationEvent, never, never> => {
598-
switch (event.type) {
599-
case "project.created":
600-
return repositoryIdentityResolver.resolve(event.payload.workspaceRoot).pipe(
601-
Effect.map((repositoryIdentity) => ({
602-
...event,
603-
payload: {
604-
...event.payload,
605-
repositoryIdentity,
606-
},
607-
})),
608-
);
609-
case "project.meta-updated":
610-
return Effect.gen(function* () {
611-
const workspaceRoot =
612-
event.payload.workspaceRoot ??
613-
Option.match(
614-
yield* projectionSnapshotQuery.getProjectShellById(event.payload.projectId),
615-
{
616-
onNone: () => null,
617-
onSome: (project) => project.workspaceRoot,
618-
},
619-
) ??
620-
null;
621-
if (workspaceRoot === null) {
622-
return event;
623-
}
624-
625-
const repositoryIdentity = yield* repositoryIdentityResolver.resolve(workspaceRoot);
626-
return {
627-
...event,
628-
payload: {
629-
...event.payload,
630-
repositoryIdentity,
631-
},
632-
} satisfies OrchestrationEvent;
633-
}).pipe(Effect.orElseSucceed(() => event));
634-
default:
635-
return Effect.succeed(event);
636-
}
637-
};
638-
639-
const enrichOrchestrationEvents = (events: ReadonlyArray<OrchestrationEvent>) =>
640-
Effect.forEach(events, enrichProjectEvent, { concurrency: 4 });
641-
642589
const toShellStreamEvent = (
643590
event: OrchestrationEvent,
644591
): Effect.Effect<Option.Option<OrchestrationShellStreamEvent>, never, never> => {
@@ -1215,30 +1162,6 @@ const makeWsRpcLayer = (
12151162
),
12161163
{ "rpc.aggregate": "orchestration" },
12171164
),
1218-
[ORCHESTRATION_WS_METHODS.replayEvents]: (input) =>
1219-
observeRpcEffect(
1220-
ORCHESTRATION_WS_METHODS.replayEvents,
1221-
Stream.runCollect(
1222-
orchestrationEngine.readEvents(
1223-
clamp(input.fromSequenceExclusive, {
1224-
maximum: Number.MAX_SAFE_INTEGER,
1225-
minimum: 0,
1226-
}),
1227-
),
1228-
).pipe(
1229-
Effect.map((events) => Array.from(events)),
1230-
Effect.flatMap(enrichOrchestrationEvents),
1231-
Effect.map((events) => events.map(projectActivityEvent)),
1232-
Effect.mapError(
1233-
(cause) =>
1234-
new OrchestrationReplayEventsError({
1235-
message: "Failed to replay orchestration events",
1236-
cause,
1237-
}),
1238-
),
1239-
),
1240-
{ "rpc.aggregate": "orchestration" },
1241-
),
12421165
[ORCHESTRATION_WS_METHODS.subscribeShell]: (input) =>
12431166
observeRpcStreamEffect(
12441167
ORCHESTRATION_WS_METHODS.subscribeShell,

0 commit comments

Comments
 (0)