Skip to content

Commit 56b6615

Browse files
RusiruSadathanacodex
authored andcommitted
perf(orchestration): bound in-memory read model and client per-thread state (Issue pingdotgg#4178)
Replays PR pingdotgg#4176 on current main and preserves the connection catch-up path from pingdotgg#4177. Replace the array-backed command model with HashMap/HashSet state, evict deleted thread bodies, prune released VCS and browser caches, and clear per-thread client state. Adds focused coverage for read-model invariants, deletion behavior, client cleanup, and VCS cache eviction after the final subscriber releases. Co-authored-by: codex <[email protected]>
1 parent 5d34f9f commit 56b6615

17 files changed

Lines changed: 679 additions & 149 deletions

‎apps/server/src/orchestration/Layers/OrchestrationEngine.ts‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import type {
2-
OrchestrationEvent,
3-
OrchestrationReadModel,
4-
ProjectId,
5-
ThreadId,
6-
} from "@t3tools/contracts";
1+
import type { OrchestrationEvent, ProjectId, ThreadId } from "@t3tools/contracts";
72
import { OrchestrationCommand } from "@t3tools/contracts";
83
import * as Cause from "effect/Cause";
94
import * as Clock from "effect/Clock";
@@ -13,6 +8,7 @@ import * as Deferred from "effect/Deferred";
138
import * as Duration from "effect/Duration";
149
import * as Effect from "effect/Effect";
1510
import * as Exit from "effect/Exit";
11+
import * as HashMap from "effect/HashMap";
1612
import * as Layer from "effect/Layer";
1713
import * as Metric from "effect/Metric";
1814
import * as Option from "effect/Option";
@@ -37,8 +33,13 @@ import {
3733
type OrchestrationDispatchError,
3834
type OrchestrationProjectorDecodeError,
3935
} from "../Errors.ts";
36+
import {
37+
createEmptyCommandReadModel,
38+
fromWireReadModel,
39+
type CommandReadModel,
40+
} from "../commandReadModel.ts";
4041
import { decideOrchestrationCommand } from "../decider.ts";
41-
import { createEmptyReadModel, projectEvent } from "../projector.ts";
42+
import { projectEvent } from "../projector.ts";
4243
import { OrchestrationProjectionPipeline } from "../Services/ProjectionPipeline.ts";
4344
import { ProjectionSnapshotQuery } from "../Services/ProjectionSnapshotQuery.ts";
4445
import {
@@ -85,15 +86,15 @@ const makeOrchestrationEngine = Effect.gen(function* () {
8586
const crypto = yield* Crypto.Crypto;
8687

8788
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
88-
let commandReadModel = createEmptyReadModel(yield* nowIso);
89+
let commandReadModel: CommandReadModel = createEmptyCommandReadModel(yield* nowIso);
8990

9091
const commandQueue = yield* Queue.unbounded<CommandEnvelope>();
9192
const eventPubSub = yield* PubSub.unbounded<OrchestrationEvent>();
9293

9394
const projectEventsOntoReadModel = (
94-
baseReadModel: OrchestrationReadModel,
95+
baseReadModel: CommandReadModel,
9596
events: ReadonlyArray<OrchestrationEvent>,
96-
): Effect.Effect<OrchestrationReadModel, OrchestrationProjectorDecodeError, never> =>
97+
): Effect.Effect<CommandReadModel, OrchestrationProjectorDecodeError, never> =>
9798
Effect.gen(function* () {
9899
let nextReadModel = baseReadModel;
99100
for (const event of events) {
@@ -298,12 +299,21 @@ const makeOrchestrationEngine = Effect.gen(function* () {
298299
};
299300

300301
yield* projectionPipeline.bootstrap;
301-
commandReadModel = yield* projectionSnapshotQuery.getCommandReadModel();
302+
// Seed the in-memory command model from the DB projection. Deleted threads
303+
// are dropped so the model starts consistent with the projector's eviction
304+
// policy (see commandReadModel.ts / the `thread.deleted` projector branch).
305+
commandReadModel = fromWireReadModel(yield* projectionSnapshotQuery.getCommandReadModel(), {
306+
dropDeletedThreads: true,
307+
});
302308

303309
const worker = Effect.forever(Queue.take(commandQueue).pipe(Effect.flatMap(processEnvelope)));
304310
yield* Effect.forkScoped(worker);
305311
yield* Effect.logDebug("orchestration engine started").pipe(
306-
Effect.annotateLogs({ sequence: commandReadModel.snapshotSequence }),
312+
Effect.annotateLogs({
313+
sequence: commandReadModel.snapshotSequence,
314+
threadCount: HashMap.size(commandReadModel.threads),
315+
projectCount: HashMap.size(commandReadModel.projects),
316+
}),
307317
);
308318

309319
const readEvents: OrchestrationEngineShape["readEvents"] = (fromSequenceExclusive, limit) =>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "@t3tools/contracts";
1212
import * as Effect from "effect/Effect";
1313

14+
import { fromWireReadModel } from "./commandReadModel.ts";
1415
import {
1516
findThreadById,
1617
listThreadsByProjectId,
@@ -21,7 +22,7 @@ import {
2122

2223
const now = "2026-01-01T00:00:00.000Z";
2324

24-
const readModel: OrchestrationReadModel = {
25+
const wireReadModel: OrchestrationReadModel = {
2526
snapshotSequence: 2,
2627
updatedAt: now,
2728
projects: [
@@ -102,6 +103,8 @@ const readModel: OrchestrationReadModel = {
102103
],
103104
};
104105

106+
const readModel = fromWireReadModel(wireReadModel, { dropDeletedThreads: false });
107+
105108
const messageSendCommand: OrchestrationCommand = {
106109
type: "thread.turn.start",
107110
commandId: CommandId.make("cmd-1"),

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

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,34 @@
11
import type {
22
OrchestrationCommand,
33
OrchestrationProject,
4-
OrchestrationReadModel,
54
OrchestrationThread,
65
ProjectId,
76
ThreadId,
87
} from "@t3tools/contracts";
98
import { normalizeProjectPathForComparison } from "@t3tools/shared/path";
109
import * as Effect from "effect/Effect";
10+
import * as HashMap from "effect/HashMap";
1111

12+
import {
13+
findProjectById,
14+
findThreadById,
15+
isThreadDeleted,
16+
listThreadsByProjectId,
17+
type CommandReadModel,
18+
} from "./commandReadModel.ts";
1219
import { OrchestrationCommandInvariantError } from "./Errors.ts";
1320

21+
export { findProjectById, findThreadById, listThreadsByProjectId };
22+
1423
function invariantError(commandType: string, detail: string): OrchestrationCommandInvariantError {
1524
return new OrchestrationCommandInvariantError({
1625
commandType,
1726
detail,
1827
});
1928
}
2029

21-
export function findThreadById(
22-
readModel: OrchestrationReadModel,
23-
threadId: ThreadId,
24-
): OrchestrationThread | undefined {
25-
return readModel.threads.find((thread) => thread.id === threadId);
26-
}
27-
28-
export function findProjectById(
29-
readModel: OrchestrationReadModel,
30-
projectId: ProjectId,
31-
): OrchestrationProject | undefined {
32-
return readModel.projects.find((project) => project.id === projectId);
33-
}
34-
35-
export function listThreadsByProjectId(
36-
readModel: OrchestrationReadModel,
37-
projectId: ProjectId,
38-
): ReadonlyArray<OrchestrationThread> {
39-
return readModel.threads.filter((thread) => thread.projectId === projectId);
40-
}
41-
4230
export function requireProject(input: {
43-
readonly readModel: OrchestrationReadModel;
31+
readonly readModel: CommandReadModel;
4432
readonly command: OrchestrationCommand;
4533
readonly projectId: ProjectId;
4634
}): Effect.Effect<OrchestrationProject, OrchestrationCommandInvariantError> {
@@ -57,7 +45,7 @@ export function requireProject(input: {
5745
}
5846

5947
export function requireProjectAbsent(input: {
60-
readonly readModel: OrchestrationReadModel;
48+
readonly readModel: CommandReadModel;
6149
readonly command: OrchestrationCommand;
6250
readonly projectId: ProjectId;
6351
}): Effect.Effect<void, OrchestrationCommandInvariantError> {
@@ -73,18 +61,23 @@ export function requireProjectAbsent(input: {
7361
}
7462

7563
export function requireActiveProjectWorkspaceRootAbsent(input: {
76-
readonly readModel: OrchestrationReadModel;
64+
readonly readModel: CommandReadModel;
7765
readonly command: OrchestrationCommand;
7866
readonly workspaceRoot: string;
7967
readonly exceptProjectId?: ProjectId;
8068
}): Effect.Effect<void, OrchestrationCommandInvariantError> {
8169
const normalizedWorkspaceRoot = normalizeProjectPathForComparison(input.workspaceRoot);
82-
const existingProject = input.readModel.projects.find(
83-
(project) =>
70+
let existingProject: OrchestrationProject | undefined;
71+
for (const project of HashMap.values(input.readModel.projects)) {
72+
if (
8473
project.deletedAt === null &&
8574
normalizeProjectPathForComparison(project.workspaceRoot) === normalizedWorkspaceRoot &&
86-
project.id !== input.exceptProjectId,
87-
);
75+
project.id !== input.exceptProjectId
76+
) {
77+
existingProject = project;
78+
break;
79+
}
80+
}
8881
if (existingProject === undefined) {
8982
return Effect.void;
9083
}
@@ -97,7 +90,7 @@ export function requireActiveProjectWorkspaceRootAbsent(input: {
9790
}
9891

9992
export function requireThread(input: {
100-
readonly readModel: OrchestrationReadModel;
93+
readonly readModel: CommandReadModel;
10194
readonly command: OrchestrationCommand;
10295
readonly threadId: ThreadId;
10396
}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
@@ -114,7 +107,7 @@ export function requireThread(input: {
114107
}
115108

116109
export function requireThreadArchived(input: {
117-
readonly readModel: OrchestrationReadModel;
110+
readonly readModel: CommandReadModel;
118111
readonly command: OrchestrationCommand;
119112
readonly threadId: ThreadId;
120113
}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
@@ -133,7 +126,7 @@ export function requireThreadArchived(input: {
133126
}
134127

135128
export function requireThreadNotArchived(input: {
136-
readonly readModel: OrchestrationReadModel;
129+
readonly readModel: CommandReadModel;
137130
readonly command: OrchestrationCommand;
138131
readonly threadId: ThreadId;
139132
}): Effect.Effect<OrchestrationThread, OrchestrationCommandInvariantError> {
@@ -152,11 +145,16 @@ export function requireThreadNotArchived(input: {
152145
}
153146

154147
export function requireThreadAbsent(input: {
155-
readonly readModel: OrchestrationReadModel;
148+
readonly readModel: CommandReadModel;
156149
readonly command: OrchestrationCommand;
157150
readonly threadId: ThreadId;
158151
}): Effect.Effect<void, OrchestrationCommandInvariantError> {
159-
if (!findThreadById(input.readModel, input.threadId)) {
152+
// A deleted thread is evicted from `threads` but its id is retained in
153+
// `deletedThreadIds`, so reject re-using a live OR previously-deleted id.
154+
if (
155+
!findThreadById(input.readModel, input.threadId) &&
156+
!isThreadDeleted(input.readModel, input.threadId)
157+
) {
160158
return Effect.void;
161159
}
162160
return Effect.fail(
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import {
2+
DEFAULT_PROVIDER_INTERACTION_MODE,
3+
ProjectId,
4+
ProviderInstanceId,
5+
ThreadId,
6+
type OrchestrationReadModel,
7+
type OrchestrationThread,
8+
} from "@t3tools/contracts";
9+
import * as HashMap from "effect/HashMap";
10+
import { describe, expect, it } from "vite-plus/test";
11+
12+
import {
13+
createEmptyCommandReadModel,
14+
findProjectById,
15+
findThreadById,
16+
fromWireReadModel,
17+
isThreadDeleted,
18+
listThreadsByProjectId,
19+
} from "./commandReadModel.ts";
20+
21+
const now = "2026-01-01T00:00:00.000Z";
22+
23+
function makeThread(
24+
id: string,
25+
projectId: string,
26+
overrides?: Partial<OrchestrationThread>,
27+
): OrchestrationThread {
28+
return {
29+
id: ThreadId.make(id),
30+
projectId: ProjectId.make(projectId),
31+
title: id,
32+
modelSelection: {
33+
instanceId: ProviderInstanceId.make("codex"),
34+
model: "gpt-5-codex",
35+
},
36+
interactionMode: DEFAULT_PROVIDER_INTERACTION_MODE,
37+
runtimeMode: "full-access",
38+
branch: null,
39+
worktreePath: null,
40+
createdAt: now,
41+
updatedAt: now,
42+
archivedAt: null,
43+
latestTurn: null,
44+
messages: [],
45+
session: null,
46+
activities: [],
47+
proposedPlans: [],
48+
checkpoints: [],
49+
deletedAt: null,
50+
...overrides,
51+
};
52+
}
53+
54+
const wireReadModel: OrchestrationReadModel = {
55+
snapshotSequence: 5,
56+
updatedAt: now,
57+
projects: [
58+
{
59+
id: ProjectId.make("project-a"),
60+
title: "Project A",
61+
workspaceRoot: "/tmp/project-a",
62+
defaultModelSelection: {
63+
instanceId: ProviderInstanceId.make("codex"),
64+
model: "gpt-5-codex",
65+
},
66+
scripts: [],
67+
createdAt: now,
68+
updatedAt: now,
69+
deletedAt: null,
70+
},
71+
],
72+
threads: [
73+
makeThread("thread-live", "project-a"),
74+
makeThread("thread-archived", "project-a", { archivedAt: now }),
75+
makeThread("thread-deleted", "project-a", { deletedAt: now }),
76+
makeThread("thread-other", "project-b"),
77+
],
78+
};
79+
80+
describe("commandReadModel", () => {
81+
it("creates an empty model", () => {
82+
const model = createEmptyCommandReadModel(now);
83+
expect(model.snapshotSequence).toBe(0);
84+
expect(HashMap.size(model.threads)).toBe(0);
85+
expect(HashMap.size(model.projects)).toBe(0);
86+
expect(model.updatedAt).toBe(now);
87+
});
88+
89+
it("seeds from the wire model and drops deleted threads by default", () => {
90+
const model = fromWireReadModel(wireReadModel);
91+
expect(model.snapshotSequence).toBe(5);
92+
// deleted thread is evicted; archived + live + other retained
93+
expect(HashMap.size(model.threads)).toBe(3);
94+
expect(HashMap.has(model.threads, ThreadId.make("thread-deleted"))).toBe(false);
95+
expect(HashMap.has(model.threads, ThreadId.make("thread-archived"))).toBe(true);
96+
expect(HashMap.has(model.threads, ThreadId.make("thread-live"))).toBe(true);
97+
expect(HashMap.size(model.projects)).toBe(1);
98+
// The evicted deleted thread's id is retained so the create-twice invariant
99+
// survives a restart.
100+
expect(isThreadDeleted(model, ThreadId.make("thread-deleted"))).toBe(true);
101+
expect(isThreadDeleted(model, ThreadId.make("thread-live"))).toBe(false);
102+
expect(isThreadDeleted(model, ThreadId.make("thread-archived"))).toBe(false);
103+
});
104+
105+
it("retains deleted threads when dropDeletedThreads is false", () => {
106+
const model = fromWireReadModel(wireReadModel, { dropDeletedThreads: false });
107+
expect(HashMap.size(model.threads)).toBe(4);
108+
expect(HashMap.has(model.threads, ThreadId.make("thread-deleted"))).toBe(true);
109+
});
110+
111+
it("finds threads and projects by id with O(1) lookups", () => {
112+
const model = fromWireReadModel(wireReadModel);
113+
expect(findThreadById(model, ThreadId.make("thread-live"))?.projectId).toBe("project-a");
114+
expect(findThreadById(model, ThreadId.make("thread-deleted"))).toBeUndefined();
115+
expect(findThreadById(model, ThreadId.make("missing"))).toBeUndefined();
116+
expect(findProjectById(model, ProjectId.make("project-a"))?.title).toBe("Project A");
117+
expect(findProjectById(model, ProjectId.make("missing"))).toBeUndefined();
118+
});
119+
120+
it("lists threads by project id", () => {
121+
const model = fromWireReadModel(wireReadModel);
122+
const ids = listThreadsByProjectId(model, ProjectId.make("project-a"))
123+
.map((thread) => thread.id)
124+
.toSorted();
125+
expect(ids).toEqual([ThreadId.make("thread-archived"), ThreadId.make("thread-live")]);
126+
expect(listThreadsByProjectId(model, ProjectId.make("project-b")).map((t) => t.id)).toEqual([
127+
ThreadId.make("thread-other"),
128+
]);
129+
});
130+
});

0 commit comments

Comments
 (0)