Skip to content

Commit e25e063

Browse files
committed
perf(server): skip unused parent data when forking threads
1 parent 15fe283 commit e25e063

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4250,11 +4250,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
42504250
const sourceProjection = yield* projectionStore
42514251
.getThreadRecords(command.sourceThreadId, [
42524252
"runs",
4253-
"checkpoints",
4253+
...(command.sourcePoint.type === "checkpoint" ? (["checkpoints"] as const) : []),
42544254
"providerThreads",
42554255
"providerTurns",
42564256
"attempts",
4257-
"contextTransfers",
42584257
])
42594258
.pipe(
42604259
Effect.mapError(

‎apps/server/src/orchestration-v2/ThreadFork.execution.test.ts‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { assert, it } from "@effect/vitest";
22
import {
33
CheckpointId,
4+
CheckpointRef,
5+
CheckpointScopeId,
46
CommandId,
57
EventId,
68
MessageId,
@@ -17,6 +19,7 @@ import {
1719
} from "@t3tools/contracts";
1820
import * as DateTime from "effect/DateTime";
1921
import * as Effect from "effect/Effect";
22+
import * as Statement from "effect/sql/Statement";
2023
import * as TestClock from "effect/testing/TestClock";
2124

2225
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -865,6 +868,61 @@ const askSideChat = (threadId: ThreadId, commandId: string) =>
865868
});
866869
});
867870

871+
it.effect("resolves an explicit checkpoint fork to its source run", () =>
872+
Effect.gen(function* () {
873+
const orchestrator = yield* Orchestrator.OrchestratorV2;
874+
const eventSink = yield* EventSink.EventSinkV2;
875+
const now = yield* DateTime.now;
876+
const parentId = ThreadId.make("checkpoint-fork-parent");
877+
const childId = ThreadId.make("checkpoint-fork-child");
878+
const runId = RunId.make("checkpoint-fork-run");
879+
const checkpointId = CheckpointId.make("checkpoint-fork-checkpoint");
880+
yield* createParent(parentId);
881+
yield* writeParentRun({
882+
threadId: parentId,
883+
runId,
884+
ordinal: 1,
885+
status: "completed",
886+
text: "Work",
887+
});
888+
yield* eventSink.write({
889+
events: [
890+
{
891+
id: EventId.make("checkpoint-fork-captured"),
892+
type: "checkpoint.captured",
893+
threadId: parentId,
894+
occurredAt: now,
895+
payload: {
896+
id: checkpointId,
897+
threadId: parentId,
898+
scopeId: CheckpointScopeId.make("checkpoint-fork-scope"),
899+
runId,
900+
nodeId: NodeId.make("checkpoint-fork-node"),
901+
parentCheckpointId: null,
902+
ordinalWithinScope: 1,
903+
appRunOrdinal: 1,
904+
ref: CheckpointRef.make("refs/t3/checkpoint-fork/1"),
905+
status: "ready",
906+
files: [],
907+
capturedAt: now,
908+
},
909+
},
910+
],
911+
});
912+
yield* orchestrator.dispatch({
913+
type: "thread.fork",
914+
commandId: CommandId.make("checkpoint-fork"),
915+
sourceThreadId: parentId,
916+
targetThreadId: childId,
917+
sourcePoint: { type: "checkpoint", checkpointId },
918+
createdBy: "user",
919+
creationSource: "mcp",
920+
});
921+
const child = yield* orchestrator.getThreadShell(childId);
922+
assert.deepEqual(child?.forkedFrom, { type: "run", threadId: parentId, runId });
923+
}).pipe(Effect.provide(forkCases[0]!.layer)),
924+
);
925+
868926
it.effect("a side chat of an idle parent still starts with the framing note", () =>
869927
Effect.gen(function* () {
870928
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -880,7 +938,22 @@ it.effect("a side chat of an idle parent still starts with the framing note", ()
880938
stable: true,
881939
});
882940

883-
yield* forkSideChat("side-chat-idle-fork", parentId, sideChatId);
941+
const reads: Array<string> = [];
942+
const recordReads: Statement.Transformer = (statement) => {
943+
const [query] = statement.compile();
944+
if (/^\s*SELECT/i.test(query)) reads.push(query);
945+
return Effect.succeed(statement);
946+
};
947+
yield* forkSideChat("side-chat-idle-fork", parentId, sideChatId).pipe(
948+
Effect.provideService(Statement.CurrentTransformer, recordReads),
949+
);
950+
assert.isEmpty(
951+
reads.filter(
952+
(query) =>
953+
query.includes("FROM orchestration_v2_projection_checkpoints") ||
954+
query.includes("FROM orchestration_v2_projection_context_transfers"),
955+
),
956+
);
884957
yield* askSideChat(sideChatId, "side-chat-idle-first-message");
885958

886959
const started = yield* orchestrator.getThreadProjection(sideChatId);

0 commit comments

Comments
 (0)