Skip to content

Commit df364f3

Browse files
committed
perf(server): skip plan bodies in thread summaries
1 parent a465772 commit df364f3

5 files changed

Lines changed: 289 additions & 41 deletions

File tree

‎apps/server/src/orchestration/Layers/ProjectionPipeline.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,7 +2725,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
27252725
}),
27262726
);
27272727

2728-
it.effect("maintains shell summaries without reading message bodies", () =>
2728+
it.effect("maintains shell summaries without decoding message or plan bodies", () =>
27292729
Effect.gen(function* () {
27302730
const projectionPipeline = yield* OrchestrationProjectionPipeline;
27312731
const eventStore = yield* OrchestrationEventStore;
@@ -2951,12 +2951,13 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
29512951
('summary-other-thread', 'thread-shell-summary-other', NULL, 'pending', NULL,
29522952
'2026-03-01T08:00:06.000Z', NULL)
29532953
`;
2954+
// Empty markdown must not be decoded when the shell only needs plan status.
29542955
yield* sql`
29552956
INSERT INTO projection_thread_proposed_plans (
29562957
plan_id, thread_id, turn_id, plan_markdown, implemented_at,
29572958
implementation_thread_id, created_at, updated_at
29582959
) VALUES (
2959-
'summary-plan', 'thread-shell-summary', 'turn-shell-summary-1', '# Plan', NULL,
2960+
'summary-plan', 'thread-shell-summary', 'turn-shell-summary-1', '', NULL,
29602961
NULL, '2026-03-01T08:00:06.000Z', '2026-03-01T08:00:06.000Z'
29612962
)
29622963
`;

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

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -141,33 +141,6 @@ function shouldRefreshThreadShellSummary(event: OrchestrationEvent): boolean {
141141
}
142142
}
143143

144-
function deriveHasActionableProposedPlan(input: {
145-
readonly latestTurnId: string | null;
146-
readonly proposedPlans: ReadonlyArray<ProjectionThreadProposedPlan>;
147-
}): boolean {
148-
const sorted = [...input.proposedPlans].toSorted(
149-
(left, right) =>
150-
left.updatedAt.localeCompare(right.updatedAt) || left.planId.localeCompare(right.planId),
151-
);
152-
153-
let latestForTurn: ProjectionThreadProposedPlan | null = null;
154-
if (input.latestTurnId !== null) {
155-
for (let index = sorted.length - 1; index >= 0; index -= 1) {
156-
const plan = sorted[index];
157-
if (plan?.turnId === input.latestTurnId) {
158-
latestForTurn = plan;
159-
break;
160-
}
161-
}
162-
}
163-
if (latestForTurn !== null) {
164-
return latestForTurn.implementedAt === null;
165-
}
166-
167-
const latestPlan = sorted.at(-1) ?? null;
168-
return latestPlan !== null && latestPlan.implementedAt === null;
169-
}
170-
171144
function retainProjectionMessagesAfterRevert(
172145
messages: ReadonlyArray<ProjectionThreadMessage>,
173146
turns: ReadonlyArray<ProjectionTurn>,
@@ -539,18 +512,20 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
539512
return;
540513
}
541514

542-
const [latestUserMessageAt, proposedPlans, pendingUserInputCount, pendingApprovalCount] =
543-
yield* Effect.all([
544-
projectionThreadMessageRepository.getLatestUserMessageAt({ threadId }),
545-
projectionThreadProposedPlanRepository.listByThreadId({ threadId }),
546-
projectionThreadActivityRepository.countPendingUserInputsByThreadId({ threadId }),
547-
projectionPendingApprovalRepository.countPendingByThreadId({ threadId }),
548-
]);
549-
550-
const hasActionableProposedPlan = deriveHasActionableProposedPlan({
551-
latestTurnId: existingRow.value.latestTurnId,
552-
proposedPlans,
553-
});
515+
const [
516+
latestUserMessageAt,
517+
hasActionableProposedPlan,
518+
pendingUserInputCount,
519+
pendingApprovalCount,
520+
] = yield* Effect.all([
521+
projectionThreadMessageRepository.getLatestUserMessageAt({ threadId }),
522+
projectionThreadProposedPlanRepository.hasActionableByThreadId({
523+
threadId,
524+
latestTurnId: existingRow.value.latestTurnId,
525+
}),
526+
projectionThreadActivityRepository.countPendingUserInputsByThreadId({ threadId }),
527+
projectionPendingApprovalRepository.countPendingByThreadId({ threadId }),
528+
]);
554529

555530
yield* projectionThreadRepository.upsert({
556531
...existingRow.value,

‎apps/server/src/persistence/Layers/ProjectionRepositories.test.ts‎

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
EventId,
44
ProjectId,
55
ThreadId,
6+
TurnId,
67
ProviderInstanceId,
78
} from "@t3tools/contracts";
89
import { legacyStaleRequestFailureDetails } from "@t3tools/shared/requestActivity";
@@ -11,20 +12,24 @@ import * as Effect from "effect/Effect";
1112
import * as Layer from "effect/Layer";
1213
import * as Option from "effect/Option";
1314
import * as SqlClient from "effect/unstable/sql/SqlClient";
15+
import * as Statement from "effect/unstable/sql/Statement";
1416

1517
import { SqlitePersistenceMemory } from "./Sqlite.ts";
1618
import { ProjectionProjectRepositoryLive } from "./ProjectionProjects.ts";
1719
import { ProjectionThreadRepositoryLive } from "./ProjectionThreads.ts";
1820
import { ProjectionThreadActivityRepositoryLive } from "./ProjectionThreadActivities.ts";
21+
import { ProjectionThreadProposedPlanRepositoryLive } from "./ProjectionThreadProposedPlans.ts";
1922
import { ProjectionProjectRepository } from "../Services/ProjectionProjects.ts";
2023
import { ProjectionThreadRepository } from "../Services/ProjectionThreads.ts";
2124
import { ProjectionThreadActivityRepository } from "../Services/ProjectionThreadActivities.ts";
25+
import { ProjectionThreadProposedPlanRepository } from "../Services/ProjectionThreadProposedPlans.ts";
2226

2327
const projectionRepositoriesLayer = it.layer(
2428
Layer.mergeAll(
2529
ProjectionProjectRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
2630
ProjectionThreadRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
2731
ProjectionThreadActivityRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
32+
ProjectionThreadProposedPlanRepositoryLive.pipe(Layer.provideMerge(SqlitePersistenceMemory)),
2833
SqlitePersistenceMemory,
2934
),
3035
);
@@ -203,6 +208,211 @@ projectionRepositoriesLayer("Projection repositories", (it) => {
203208
}),
204209
);
205210

211+
it.effect("selects the latest-turn plan before checking implementation status", () =>
212+
Effect.gen(function* () {
213+
const plans = yield* ProjectionThreadProposedPlanRepository;
214+
const threadId = ThreadId.make("thread-plan-status");
215+
const latestTurnId = TurnId.make("turn-plan-status-current");
216+
const firstPlan = {
217+
planId: "plan-status-first",
218+
threadId,
219+
turnId: latestTurnId,
220+
planMarkdown: "# First plan",
221+
implementedAt: null,
222+
implementationThreadId: null,
223+
createdAt: "2026-03-24T00:00:01.000Z",
224+
updatedAt: "2026-03-24T00:00:01.000Z",
225+
};
226+
yield* plans.upsert(firstPlan);
227+
yield* plans.upsert({
228+
...firstPlan,
229+
planId: "plan-status-implemented",
230+
implementedAt: "2026-03-24T00:00:02.000Z",
231+
createdAt: "2026-03-24T00:00:02.000Z",
232+
updatedAt: "2026-03-24T00:00:02.000Z",
233+
});
234+
yield* plans.upsert({
235+
...firstPlan,
236+
planId: "plan-status-other-turn",
237+
turnId: TurnId.make("turn-plan-status-old"),
238+
updatedAt: "2026-03-24T00:00:10.000Z",
239+
});
240+
241+
assert.isFalse(yield* plans.hasActionableByThreadId({ threadId, latestTurnId }));
242+
assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null }));
243+
244+
yield* plans.upsert({ ...firstPlan, updatedAt: "2026-03-24T00:00:03.000Z" });
245+
assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId }));
246+
}),
247+
);
248+
249+
it.effect("falls back within the thread when the latest turn has no plan", () =>
250+
Effect.gen(function* () {
251+
const plans = yield* ProjectionThreadProposedPlanRepository;
252+
const threadId = ThreadId.make("thread-plan-fallback");
253+
const latestTurnId = TurnId.make("turn-plan-fallback-missing");
254+
assert.isFalse(yield* plans.hasActionableByThreadId({ threadId, latestTurnId }));
255+
assert.isFalse(yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null }));
256+
257+
const firstPlan = {
258+
planId: "plan-fallback-without-turn",
259+
threadId,
260+
turnId: null,
261+
planMarkdown: "# Old plan",
262+
implementedAt: "2026-03-24T00:00:01.000Z",
263+
implementationThreadId: null,
264+
createdAt: "2026-03-24T00:00:01.000Z",
265+
updatedAt: "2026-03-24T00:00:01.000Z",
266+
};
267+
yield* plans.upsert(firstPlan);
268+
yield* plans.upsert({
269+
...firstPlan,
270+
planId: "plan-fallback-with-turn",
271+
turnId: TurnId.make("turn-plan-fallback-old"),
272+
implementedAt: null,
273+
updatedAt: "2026-03-24T00:00:02.000Z",
274+
});
275+
yield* plans.upsert({
276+
...firstPlan,
277+
planId: "plan-fallback-other-thread",
278+
threadId: ThreadId.make("thread-plan-fallback-other"),
279+
turnId: latestTurnId,
280+
updatedAt: "2026-03-24T00:00:03.000Z",
281+
});
282+
283+
assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId }));
284+
assert.isTrue(yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null }));
285+
}),
286+
);
287+
288+
it.effect("preserves locale ordering and stable ties when selecting plan status", () =>
289+
Effect.gen(function* () {
290+
const plans = yield* ProjectionThreadProposedPlanRepository;
291+
const timestamp = "2026-03-24T00:00:00.000Z";
292+
const cases = [
293+
{
294+
name: "mixed-case-ids",
295+
expected: "plan-A".localeCompare("plan-a") > 0,
296+
rows: [
297+
{
298+
planId: "plan-a",
299+
implementedAt: timestamp,
300+
createdAt: timestamp,
301+
updatedAt: timestamp,
302+
},
303+
{ planId: "plan-A", implementedAt: null, createdAt: timestamp, updatedAt: timestamp },
304+
],
305+
},
306+
{
307+
name: "equivalent-ids",
308+
expected: true,
309+
rows: [
310+
{
311+
planId: "plan-\u00e9",
312+
implementedAt: timestamp,
313+
createdAt: timestamp,
314+
updatedAt: timestamp,
315+
},
316+
{
317+
planId: "plan-e\u0301",
318+
implementedAt: null,
319+
createdAt: "2026-03-24T00:00:01.000Z",
320+
updatedAt: timestamp,
321+
},
322+
],
323+
},
324+
{
325+
name: "timestamp-formats",
326+
expected: "2026-03-24T00:00:00+00:00".localeCompare("2026-03-24T00:00:00-01:00") > 0,
327+
rows: [
328+
{
329+
planId: "plan-minus",
330+
implementedAt: timestamp,
331+
createdAt: timestamp,
332+
updatedAt: "2026-03-24T00:00:00-01:00",
333+
},
334+
{
335+
planId: "plan-plus",
336+
implementedAt: null,
337+
createdAt: timestamp,
338+
updatedAt: "2026-03-24T00:00:00+00:00",
339+
},
340+
],
341+
},
342+
];
343+
for (const testCase of cases) {
344+
const threadId = ThreadId.make(`thread-plan-order-${testCase.name}`);
345+
const latestTurnId = TurnId.make(`turn-plan-order-${testCase.name}`);
346+
for (const plan of testCase.rows) {
347+
yield* plans.upsert({
348+
...plan,
349+
planId: `${testCase.name}-${plan.planId}`,
350+
threadId,
351+
turnId: latestTurnId,
352+
planMarkdown: "# Plan",
353+
implementationThreadId: null,
354+
});
355+
}
356+
assert.strictEqual(
357+
yield* plans.hasActionableByThreadId({ threadId, latestTurnId }),
358+
testCase.expected,
359+
testCase.name,
360+
);
361+
assert.strictEqual(
362+
yield* plans.hasActionableByThreadId({ threadId, latestTurnId: null }),
363+
testCase.expected,
364+
testCase.name,
365+
);
366+
}
367+
}),
368+
);
369+
370+
it.effect("returns only current-turn status metadata when old plans have large bodies", () =>
371+
Effect.gen(function* () {
372+
const plans = yield* ProjectionThreadProposedPlanRepository;
373+
const sql = yield* SqlClient.SqlClient;
374+
const threadId = ThreadId.make("thread-plan-metadata");
375+
const latestTurnId = TurnId.make("turn-plan-metadata-current");
376+
const updatedAt = "2026-03-24T00:00:00.000Z";
377+
yield* sql`
378+
WITH RECURSIVE history(n) AS (
379+
SELECT 1 UNION ALL SELECT n + 1 FROM history WHERE n < 256
380+
)
381+
INSERT INTO projection_thread_proposed_plans (
382+
plan_id, thread_id, turn_id, plan_markdown, implemented_at,
383+
implementation_thread_id, created_at, updated_at
384+
)
385+
SELECT 'plan-metadata-old-' || n, ${threadId}, 'turn-plan-metadata-old',
386+
${"# Old plan\n".repeat(1024)}, ${updatedAt}, NULL, ${updatedAt}, ${updatedAt}
387+
FROM history
388+
`;
389+
yield* plans.upsert({
390+
planId: "plan-metadata-current",
391+
threadId,
392+
turnId: latestTurnId,
393+
planMarkdown: "# Current plan",
394+
implementedAt: null,
395+
implementationThreadId: null,
396+
createdAt: updatedAt,
397+
updatedAt,
398+
});
399+
const statements: Array<Statement.Statement<unknown>> = [];
400+
const actionable = yield* plans.hasActionableByThreadId({ threadId, latestTurnId }).pipe(
401+
Effect.provideService(Statement.CurrentTransformer, (statement) => {
402+
statements.push(statement);
403+
return Effect.succeed(statement);
404+
}),
405+
);
406+
assert.isTrue(actionable);
407+
assert.strictEqual(statements.length, 1);
408+
const statement = statements[0];
409+
if (statement === undefined) return yield* Effect.die("Expected a plan status query.");
410+
assert.deepEqual(yield* statement, [
411+
{ planId: "plan-metadata-current", implementedAt: null, updatedAt },
412+
]);
413+
}),
414+
);
415+
206416
it.effect("stores SQL NULL for missing project model options", () =>
207417
Effect.gen(function* () {
208418
const projects = yield* ProjectionProjectRepository;

0 commit comments

Comments
 (0)