perf(server): avoid full thread reads on turn start (#10108)
This commit is contained in:
1 parent
281b92b488
commit
17490c0a00
12 files changed
+291
-8
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
type AgentSessionImportSource,
|
||||
ChatAttachment,
|
||||
CheckpointRef,
|
||||
EventId,
|
||||
MessageId,
|
||||
@@ -12,6 +13,7 @@ import { assert, it } from "@effect/vitest";
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as SqlClient from "effect/unstable/sql/SqlClient";
|
||||
|
||||
@@ -31,6 +33,9 @@ const asTurnId = (value: string): TurnId => TurnId.make(value);
|
||||
const asMessageId = (value: string): MessageId => MessageId.make(value);
|
||||
const asEventId = (value: string): EventId => EventId.make(value);
|
||||
const asCheckpointRef = (value: string): CheckpointRef => CheckpointRef.make(value);
|
||||
const encodeChatAttachments = Schema.encodeEffect(
|
||||
Schema.fromJsonString(Schema.Array(ChatAttachment)),
|
||||
);
|
||||
|
||||
const projectionSnapshotLayer = it.layer(
|
||||
OrchestrationProjectionSnapshotQueryLive.pipe(
|
||||
@@ -598,6 +603,142 @@ projectionSnapshotLayer("ProjectionSnapshotQuery", (it) => {
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("reads one turn-start message without decoding unrelated history", () =>
|
||||
Effect.gen(function* () {
|
||||
const query = yield* ProjectionSnapshotQuery;
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const threadId = ThreadId.make("thread-turn-start-read");
|
||||
const messageId = MessageId.make("message-turn-start-read");
|
||||
const createdAt = "2026-09-05T00:00:00.000Z";
|
||||
const attachments = [
|
||||
{
|
||||
type: "file" as const,
|
||||
id: "notes",
|
||||
name: "notes.txt",
|
||||
mimeType: "text/plain",
|
||||
sizeBytes: 8,
|
||||
},
|
||||
];
|
||||
const attachmentsJson = yield* encodeChatAttachments(attachments);
|
||||
yield* sql`
|
||||
WITH RECURSIVE history(n) AS (
|
||||
VALUES (1) UNION ALL SELECT n + 1 FROM history WHERE n < 2000
|
||||
)
|
||||
INSERT INTO projection_thread_messages (
|
||||
message_id, thread_id, turn_id, role, text, attachments_json,
|
||||
is_streaming, created_at, updated_at
|
||||
)
|
||||
SELECT 'turn-start-history:' || n, ${threadId}, 'old-turn:' || n, 'assistant',
|
||||
'Unrelated assistant output', 'not-json', 0, ${createdAt}, ${createdAt}
|
||||
FROM history
|
||||
`;
|
||||
yield* sql`
|
||||
INSERT INTO projection_thread_messages (
|
||||
message_id, thread_id, role, text, attachments_json, is_streaming, created_at, updated_at
|
||||
) VALUES (${messageId}, ${threadId}, 'user', 'Read these notes',
|
||||
${attachmentsJson}, 0, ${createdAt}, ${createdAt})
|
||||
`;
|
||||
yield* sql`
|
||||
INSERT INTO projection_thread_messages (
|
||||
message_id, thread_id, role, text, attachments_json, is_streaming, created_at, updated_at
|
||||
) VALUES ('turn-start-unrelated-user', 'thread-turn-start-unrelated', 'user', 'Unrelated prompt',
|
||||
'not-json', 0, ${createdAt}, ${createdAt})
|
||||
`;
|
||||
|
||||
const counter = makeSqlStatementCounter();
|
||||
const context = yield* query
|
||||
.getTurnStartMessage({ threadId, messageId })
|
||||
.pipe(Effect.withTracer(counter.tracer));
|
||||
assert.equal(counter.count(), 1);
|
||||
assert.deepEqual(
|
||||
context,
|
||||
Option.some({
|
||||
message: {
|
||||
id: messageId,
|
||||
role: "user",
|
||||
text: "Read these notes",
|
||||
turnId: null,
|
||||
streaming: false,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
attachments,
|
||||
},
|
||||
hasOtherUserMessages: false,
|
||||
}),
|
||||
);
|
||||
assert.equal(
|
||||
(yield* query.getTurnStartMessage({
|
||||
threadId: ThreadId.make("thread-turn-start-unrelated"),
|
||||
messageId,
|
||||
}))._tag,
|
||||
"None",
|
||||
);
|
||||
assert.equal(
|
||||
(yield* query.getTurnStartMessage({ threadId, messageId: MessageId.make("missing") }))._tag,
|
||||
"None",
|
||||
);
|
||||
}).pipe(
|
||||
Effect.ensuring(
|
||||
Effect.gen(function* () {
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
yield* sql`
|
||||
DELETE FROM projection_thread_messages
|
||||
WHERE thread_id IN ('thread-turn-start-read', 'thread-turn-start-unrelated')
|
||||
`;
|
||||
}).pipe(Effect.orDie),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("keeps compaction and queued-message eligibility in the turn-start query", () =>
|
||||
Effect.gen(function* () {
|
||||
const query = yield* ProjectionSnapshotQuery;
|
||||
const sql = yield* SqlClient.SqlClient;
|
||||
const threadId = ThreadId.make("thread-turn-start-eligibility");
|
||||
const messageId = MessageId.make("message-turn-start-eligibility");
|
||||
const createdAt = "2026-09-05T00:00:00.000Z";
|
||||
yield* sql`
|
||||
INSERT INTO projection_thread_messages (
|
||||
message_id, thread_id, role, text, is_streaming, created_at, updated_at
|
||||
) VALUES (${messageId}, ${threadId}, 'user', 'Start a turn', 0, ${createdAt}, ${createdAt})
|
||||
`;
|
||||
yield* sql`
|
||||
INSERT INTO projection_thread_messages (
|
||||
message_id, thread_id, role, text, attachments_json, is_streaming, created_at, updated_at
|
||||
) VALUES ('turn-start-other-user', ${threadId}, 'user', '/compact', NULL, 0,
|
||||
'2026-09-05T00:00:01.000Z', '2026-09-05T00:00:01.000Z')
|
||||
`;
|
||||
|
||||
for (const { text, attachments, hasOtherUserMessages } of [
|
||||
{ text: "/compact", attachments: null, hasOtherUserMessages: false },
|
||||
{
|
||||
text: "\t\n\r /CoMpAcT\u00a0\u2028\ufeff",
|
||||
attachments: "[ ]",
|
||||
hasOtherUserMessages: false,
|
||||
},
|
||||
{ text: "/compact keep recent errors", attachments: "[]", hasOtherUserMessages: true },
|
||||
{ text: "", attachments: null, hasOtherUserMessages: true },
|
||||
{ text: "Queued prompt", attachments: null, hasOtherUserMessages: true },
|
||||
{
|
||||
text: "/compact",
|
||||
attachments:
|
||||
'[{"type":"file","id":"notes","name":"notes.txt","mimeType":"text/plain","sizeBytes":8}]',
|
||||
hasOtherUserMessages: true,
|
||||
},
|
||||
]) {
|
||||
yield* sql`
|
||||
UPDATE projection_thread_messages SET text = ${text}, attachments_json = ${attachments}
|
||||
WHERE message_id = 'turn-start-other-user'
|
||||
`;
|
||||
const context = yield* query.getTurnStartMessage({ threadId, messageId });
|
||||
assert.equal(context._tag, "Some");
|
||||
if (context._tag === "Some") {
|
||||
assert.equal(context.value.hasOtherUserMessages, hasOtherUserMessages);
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("keeps archived threads out of the main shell snapshot", () =>
|
||||
Effect.gen(function* () {
|
||||
const snapshotQuery = yield* ProjectionSnapshotQuery;
|
||||
|
||||
Reference in new issue
Block a user