perf(server): stop loading message bodies for thread summaries (#9662)
This commit is contained in:
1 parent
560afffdea
commit
8ac5462920
7 files changed
+205
-20
No files matched your search
@@ -2534,7 +2534,7 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("maintains shell summary fields across message and activity streams", () =>
|
||||
it.effect("maintains shell summaries without reading message bodies", () =>
|
||||
Effect.gen(function* () {
|
||||
const projectionPipeline = yield* OrchestrationProjectionPipeline;
|
||||
const eventStore = yield* OrchestrationEventStore;
|
||||
@@ -2742,6 +2742,106 @@ it.layer(BaseTestLayer)("OrchestrationProjectionPipeline", (it) => {
|
||||
updatedAt: "2026-03-01T08:00:05.000Z",
|
||||
},
|
||||
]);
|
||||
|
||||
// Summary refreshes must not decode message bodies or attachment metadata.
|
||||
yield* sql`
|
||||
UPDATE projection_thread_messages
|
||||
SET attachments_json = '{not-json'
|
||||
WHERE thread_id = 'thread-shell-summary'
|
||||
`;
|
||||
yield* sql`
|
||||
INSERT INTO projection_pending_approvals (
|
||||
request_id, thread_id, turn_id, status, decision, created_at, resolved_at
|
||||
) VALUES
|
||||
('summary-pending', 'thread-shell-summary', NULL, 'pending', NULL,
|
||||
'2026-03-01T08:00:06.000Z', NULL),
|
||||
('summary-resolved', 'thread-shell-summary', NULL, 'resolved', 'accept',
|
||||
'2026-03-01T08:00:06.000Z', '2026-03-01T08:00:06.000Z'),
|
||||
('summary-other-thread', 'thread-shell-summary-other', NULL, 'pending', NULL,
|
||||
'2026-03-01T08:00:06.000Z', NULL)
|
||||
`;
|
||||
yield* sql`
|
||||
INSERT INTO projection_thread_proposed_plans (
|
||||
plan_id, thread_id, turn_id, plan_markdown, implemented_at,
|
||||
implementation_thread_id, created_at, updated_at
|
||||
) VALUES (
|
||||
'summary-plan', 'thread-shell-summary', 'turn-shell-summary-1', '# Plan', NULL,
|
||||
NULL, '2026-03-01T08:00:06.000Z', '2026-03-01T08:00:06.000Z'
|
||||
)
|
||||
`;
|
||||
|
||||
const refreshEvents = [
|
||||
{
|
||||
type: "thread.session-set",
|
||||
eventId: EventId.make("evt-shell-summary-7"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: ThreadId.make("thread-shell-summary"),
|
||||
occurredAt: "2026-03-01T08:00:07.000Z",
|
||||
commandId: CommandId.make("cmd-shell-summary-7"),
|
||||
causationEventId: null,
|
||||
correlationId: CorrelationId.make("cmd-shell-summary-7"),
|
||||
metadata: {},
|
||||
payload: {
|
||||
threadId: ThreadId.make("thread-shell-summary"),
|
||||
session: {
|
||||
threadId: ThreadId.make("thread-shell-summary"),
|
||||
status: "ready",
|
||||
providerName: "codex",
|
||||
runtimeMode: "approval-required",
|
||||
activeTurnId: null,
|
||||
lastError: null,
|
||||
updatedAt: "2026-03-01T08:00:07.000Z",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
type: "thread.turn-diff-completed",
|
||||
eventId: EventId.make("evt-shell-summary-8"),
|
||||
aggregateKind: "thread",
|
||||
aggregateId: ThreadId.make("thread-shell-summary"),
|
||||
occurredAt: "2026-03-01T08:00:08.000Z",
|
||||
commandId: CommandId.make("cmd-shell-summary-8"),
|
||||
causationEventId: null,
|
||||
correlationId: CorrelationId.make("cmd-shell-summary-8"),
|
||||
metadata: {},
|
||||
payload: {
|
||||
threadId: ThreadId.make("thread-shell-summary"),
|
||||
turnId: TurnId.make("turn-shell-summary-1"),
|
||||
checkpointTurnCount: 1,
|
||||
checkpointRef: CheckpointRef.make("refs/t3/checkpoints/thread-shell-summary/1"),
|
||||
status: "ready",
|
||||
files: [],
|
||||
assistantMessageId: MessageId.make("message-shell-summary-assistant"),
|
||||
completedAt: "2026-03-01T08:00:08.000Z",
|
||||
},
|
||||
},
|
||||
] satisfies ReadonlyArray<Parameters<typeof eventStore.append>[0]>;
|
||||
|
||||
for (const event of refreshEvents) {
|
||||
yield* appendAndProject(event);
|
||||
const summary = yield* sql<{
|
||||
readonly latestUserMessageAt: string | null;
|
||||
readonly pendingApprovalCount: number;
|
||||
readonly pendingUserInputCount: number;
|
||||
readonly hasActionableProposedPlan: number;
|
||||
}>`
|
||||
SELECT
|
||||
latest_user_message_at AS "latestUserMessageAt",
|
||||
pending_approval_count AS "pendingApprovalCount",
|
||||
pending_user_input_count AS "pendingUserInputCount",
|
||||
has_actionable_proposed_plan AS "hasActionableProposedPlan"
|
||||
FROM projection_threads
|
||||
WHERE thread_id = 'thread-shell-summary'
|
||||
`;
|
||||
assert.deepEqual(summary, [
|
||||
{
|
||||
latestUserMessageAt: "2026-03-01T08:00:02.000Z",
|
||||
pendingApprovalCount: 1,
|
||||
pendingUserInputCount: 1,
|
||||
hasActionableProposedPlan: 1,
|
||||
},
|
||||
]);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
@@ -587,26 +587,14 @@ const makeOrchestrationProjectionPipeline = Effect.fn("makeOrchestrationProjecti
|
||||
return;
|
||||
}
|
||||
|
||||
const [messages, proposedPlans, activities, pendingApprovals] = yield* Effect.all([
|
||||
projectionThreadMessageRepository.listByThreadId({ threadId }),
|
||||
projectionThreadProposedPlanRepository.listByThreadId({ threadId }),
|
||||
projectionThreadActivityRepository.listUserInputLifecycleByThreadId({ threadId }),
|
||||
projectionPendingApprovalRepository.listByThreadId({ threadId }),
|
||||
]);
|
||||
const [latestUserMessageAt, proposedPlans, activities, pendingApprovalCount] =
|
||||
yield* Effect.all([
|
||||
projectionThreadMessageRepository.getLatestUserMessageAt({ threadId }),
|
||||
projectionThreadProposedPlanRepository.listByThreadId({ threadId }),
|
||||
projectionThreadActivityRepository.listUserInputLifecycleByThreadId({ threadId }),
|
||||
projectionPendingApprovalRepository.countPendingByThreadId({ threadId }),
|
||||
]);
|
||||
|
||||
let latestUserMessageAt: string | null = null;
|
||||
for (const message of messages) {
|
||||
if (
|
||||
message.role === "user" &&
|
||||
(latestUserMessageAt === null || message.createdAt > latestUserMessageAt)
|
||||
) {
|
||||
latestUserMessageAt = message.createdAt;
|
||||
}
|
||||
}
|
||||
|
||||
const pendingApprovalCount = pendingApprovals.filter(
|
||||
(approval) => approval.status === "pending",
|
||||
).length;
|
||||
const pendingUserInputCount = derivePendingUserInputCountFromActivities(activities);
|
||||
const hasActionableProposedPlan = deriveHasActionableProposedPlan({
|
||||
latestTurnId: existingRow.value.latestTurnId,
|
||||
|
||||
@@ -2,6 +2,7 @@ import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
import * as SqlSchema from "effect/unstable/sql/SqlSchema";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import { toPersistenceSqlError } from "../Errors.ts";
|
||||
import {
|
||||
@@ -68,6 +69,16 @@ const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
|
||||
`,
|
||||
});
|
||||
|
||||
const countPendingApprovalRows = SqlSchema.findOne({
|
||||
Request: ListProjectionPendingApprovalsInput,
|
||||
Result: Schema.Struct({ count: Schema.Number }),
|
||||
execute: ({ threadId }) => sql`
|
||||
SELECT COUNT(*) AS count
|
||||
FROM projection_pending_approvals
|
||||
WHERE thread_id = ${threadId} AND status = 'pending'
|
||||
`,
|
||||
});
|
||||
|
||||
const getProjectionPendingApprovalRow = SqlSchema.findOneOption({
|
||||
Request: GetProjectionPendingApprovalInput,
|
||||
Result: ProjectionPendingApproval,
|
||||
@@ -116,6 +127,15 @@ const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
|
||||
),
|
||||
);
|
||||
|
||||
const countPendingByThreadId: ProjectionPendingApprovalRepositoryShape["countPendingByThreadId"] =
|
||||
(input) =>
|
||||
countPendingApprovalRows(input).pipe(
|
||||
Effect.mapError(
|
||||
toPersistenceSqlError("ProjectionPendingApprovalRepository.countPendingByThreadId:query"),
|
||||
),
|
||||
Effect.map((row) => row.count),
|
||||
);
|
||||
|
||||
const getByRequestId: ProjectionPendingApprovalRepositoryShape["getByRequestId"] = (input) =>
|
||||
getProjectionPendingApprovalRow(input).pipe(
|
||||
Effect.mapError(
|
||||
@@ -142,6 +162,7 @@ const makeProjectionPendingApprovalRepository = Effect.gen(function* () {
|
||||
return {
|
||||
upsert,
|
||||
listByThreadId,
|
||||
countPendingByThreadId,
|
||||
getByRequestId,
|
||||
deleteByRequestId,
|
||||
deleteByThreadId,
|
||||
|
||||
@@ -12,6 +12,49 @@ const layer = it.layer(
|
||||
);
|
||||
|
||||
layer("ProjectionThreadMessageRepository", (it) => {
|
||||
it.effect("finds the latest user-message time within one thread", () =>
|
||||
Effect.gen(function* () {
|
||||
const repository = yield* ProjectionThreadMessageRepository;
|
||||
const threadId = ThreadId.make("thread-latest-user-message");
|
||||
assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
|
||||
|
||||
const messages = [
|
||||
{ role: "user", createdAt: "2026-02-28T19:05:02.000Z" },
|
||||
{ role: "user", createdAt: "2026-02-28T19:05:01.000Z" },
|
||||
{ role: "assistant", createdAt: "2026-02-28T19:05:03.000Z" },
|
||||
{ role: "system", createdAt: "2026-02-28T19:05:04.000Z" },
|
||||
] as const;
|
||||
for (const [index, message] of messages.entries()) {
|
||||
yield* repository.upsert({
|
||||
messageId: MessageId.make(`latest-user-message-${index}`),
|
||||
threadId,
|
||||
turnId: null,
|
||||
...message,
|
||||
text: "Message body",
|
||||
isStreaming: false,
|
||||
updatedAt: "2026-02-28T19:06:00.000Z",
|
||||
});
|
||||
}
|
||||
yield* repository.upsert({
|
||||
messageId: MessageId.make("latest-user-message-other-thread"),
|
||||
threadId: ThreadId.make("thread-latest-user-message-other"),
|
||||
turnId: null,
|
||||
role: "user",
|
||||
text: "Other thread",
|
||||
isStreaming: false,
|
||||
createdAt: "2026-02-28T19:05:05.000Z",
|
||||
updatedAt: "2026-02-28T19:05:05.000Z",
|
||||
});
|
||||
|
||||
assert.strictEqual(
|
||||
yield* repository.getLatestUserMessageAt({ threadId }),
|
||||
"2026-02-28T19:05:02.000Z",
|
||||
);
|
||||
yield* repository.deleteByThreadId({ threadId });
|
||||
assert.isNull(yield* repository.getLatestUserMessageAt({ threadId }));
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("appends streaming text and applies attachment updates", () =>
|
||||
Effect.gen(function* () {
|
||||
const repository = yield* ProjectionThreadMessageRepository;
|
||||
|
||||
@@ -182,6 +182,18 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
|
||||
`,
|
||||
});
|
||||
|
||||
const getLatestUserMessageAtRow = SqlSchema.findOne({
|
||||
Request: ListProjectionThreadMessagesInput,
|
||||
Result: Schema.Struct({
|
||||
latestUserMessageAt: Schema.NullOr(ProjectionThreadMessage.fields.createdAt),
|
||||
}),
|
||||
execute: ({ threadId }) => sql`
|
||||
SELECT MAX(created_at) AS "latestUserMessageAt"
|
||||
FROM projection_thread_messages
|
||||
WHERE thread_id = ${threadId} AND role = 'user'
|
||||
`,
|
||||
});
|
||||
|
||||
const deleteProjectionThreadMessageRows = SqlSchema.void({
|
||||
Request: DeleteProjectionThreadMessagesInput,
|
||||
execute: ({ threadId }) =>
|
||||
@@ -219,6 +231,16 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
|
||||
Effect.map((rows) => rows.map(toProjectionThreadMessage)),
|
||||
);
|
||||
|
||||
const getLatestUserMessageAt: ProjectionThreadMessageRepositoryShape["getLatestUserMessageAt"] = (
|
||||
input,
|
||||
) =>
|
||||
getLatestUserMessageAtRow(input).pipe(
|
||||
Effect.mapError(
|
||||
toPersistenceSqlError("ProjectionThreadMessageRepository.getLatestUserMessageAt:query"),
|
||||
),
|
||||
Effect.map((row) => row.latestUserMessageAt),
|
||||
);
|
||||
|
||||
const deleteByThreadId: ProjectionThreadMessageRepositoryShape["deleteByThreadId"] = (input) =>
|
||||
deleteProjectionThreadMessageRows(input).pipe(
|
||||
Effect.mapError(
|
||||
@@ -231,6 +253,7 @@ const makeProjectionThreadMessageRepository = Effect.gen(function* () {
|
||||
appendStreaming,
|
||||
getByMessageId,
|
||||
listByThreadId,
|
||||
getLatestUserMessageAt,
|
||||
deleteByThreadId,
|
||||
} satisfies ProjectionThreadMessageRepositoryShape;
|
||||
});
|
||||
|
||||
@@ -69,6 +69,11 @@ export interface ProjectionPendingApprovalRepositoryShape {
|
||||
input: ListProjectionPendingApprovalsInput,
|
||||
) => Effect.Effect<ReadonlyArray<ProjectionPendingApproval>, ProjectionRepositoryError>;
|
||||
|
||||
/** Count pending approvals without loading resolved request history. */
|
||||
readonly countPendingByThreadId: (
|
||||
input: ListProjectionPendingApprovalsInput,
|
||||
) => Effect.Effect<number, ProjectionRepositoryError>;
|
||||
|
||||
/**
|
||||
* Read a pending approval row by request id.
|
||||
*/
|
||||
|
||||
@@ -90,6 +90,11 @@ export interface ProjectionThreadMessageRepositoryShape {
|
||||
input: ListProjectionThreadMessagesInput,
|
||||
) => Effect.Effect<ReadonlyArray<ProjectionThreadMessage>, ProjectionRepositoryError>;
|
||||
|
||||
/** Read the latest user-message timestamp without loading message bodies. */
|
||||
readonly getLatestUserMessageAt: (
|
||||
input: ListProjectionThreadMessagesInput,
|
||||
) => Effect.Effect<ProjectionThreadMessage["createdAt"] | null, ProjectionRepositoryError>;
|
||||
|
||||
/**
|
||||
* Delete projected thread messages by thread.
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user