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
@@ -91,6 +91,9 @@ const THREAD_DETAIL_ACTIVITY_LIMIT = 500;
|
||||
// Snapshot payloads are decoded and projected in small sequential batches so
|
||||
// one client read does not retain the raw payloads for the full activity window.
|
||||
const THREAD_DETAIL_ACTIVITY_PAYLOAD_BATCH_SIZE = 25;
|
||||
// SQLite trim defaults to spaces. Match the whitespace removed by String.trim.
|
||||
const MESSAGE_TRIM_WHITESPACE =
|
||||
"\t\n\v\f\r \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff";
|
||||
const ProjectionProjectDbRowSchema = ProjectionProject.mapFields(
|
||||
Struct.assign({
|
||||
defaultModelSelection: Schema.NullOr(Schema.fromJsonString(ModelSelection)),
|
||||
@@ -105,6 +108,9 @@ const ProjectionThreadMessageDbRowSchema = ProjectionThreadMessage.mapFields(
|
||||
attachments: Schema.NullOr(Schema.fromJsonString(Schema.Array(ChatAttachment))),
|
||||
}),
|
||||
);
|
||||
const ProjectionTurnStartMessageDbRowSchema = ProjectionThreadMessageDbRowSchema.mapFields(
|
||||
Struct.assign({ hasOtherUserMessages: Schema.Number }),
|
||||
);
|
||||
const ProjectionThreadProposedPlanDbRowSchema = ProjectionThreadProposedPlan;
|
||||
const ProjectionThreadDbRowSchema = ProjectionThread.mapFields(
|
||||
Struct.assign({
|
||||
@@ -180,6 +186,10 @@ const ProjectionImportedAgentSessionSourcesRowSchema = Schema.Struct({
|
||||
const ThreadIdLookupInput = Schema.Struct({
|
||||
threadId: ThreadId,
|
||||
});
|
||||
const TurnStartMessageLookupInput = Schema.Struct({
|
||||
threadId: ThreadId,
|
||||
messageId: MessageId,
|
||||
});
|
||||
const ThreadActivityKindsLookupInput = Schema.Struct({
|
||||
threadId: ThreadId,
|
||||
activityKinds: Schema.Array(Schema.String),
|
||||
@@ -1117,6 +1127,37 @@ const makeProjectionSnapshotQuery = Effect.gen(function* () {
|
||||
),
|
||||
});
|
||||
|
||||
const getTurnStartMessageRow = SqlSchema.findOneOption({
|
||||
Request: TurnStartMessageLookupInput,
|
||||
Result: ProjectionTurnStartMessageDbRowSchema,
|
||||
execute: ({ threadId, messageId }) => sql`
|
||||
SELECT
|
||||
message_id AS "messageId",
|
||||
thread_id AS "threadId",
|
||||
turn_id AS "turnId",
|
||||
role,
|
||||
text,
|
||||
attachments_json AS "attachments",
|
||||
is_streaming AS "isStreaming",
|
||||
created_at AS "createdAt",
|
||||
updated_at AS "updatedAt",
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM projection_thread_messages AS other
|
||||
WHERE other.thread_id = ${threadId}
|
||||
AND other.message_id != ${messageId}
|
||||
AND other.role = 'user'
|
||||
AND (
|
||||
LOWER(TRIM(other.text, ${MESSAGE_TRIM_WHITESPACE})) != '/compact'
|
||||
OR COALESCE(json_array_length(other.attachments_json), 0) > 0
|
||||
)
|
||||
) AS "hasOtherUserMessages"
|
||||
FROM projection_thread_messages
|
||||
WHERE thread_id = ${threadId} AND message_id = ${messageId}
|
||||
LIMIT 1
|
||||
`,
|
||||
});
|
||||
|
||||
const listThreadMessageRowsByThread = SqlSchema.findAll({
|
||||
Request: ThreadIdLookupInput,
|
||||
Result: ProjectionThreadMessageDbRowSchema,
|
||||
@@ -2887,6 +2928,32 @@ pending_approval_requests AS (
|
||||
}));
|
||||
});
|
||||
|
||||
const getTurnStartMessage: ProjectionSnapshotQueryShape["getTurnStartMessage"] = Effect.fn(
|
||||
"ProjectionSnapshotQuery.getTurnStartMessage",
|
||||
)(function* (input) {
|
||||
const message = yield* getTurnStartMessageRow(input).pipe(
|
||||
Effect.mapError(
|
||||
toPersistenceSqlOrDecodeError(
|
||||
"ProjectionSnapshotQuery.getTurnStartMessage:query",
|
||||
"ProjectionSnapshotQuery.getTurnStartMessage:decodeRow",
|
||||
),
|
||||
),
|
||||
);
|
||||
return Option.map(message, (row) => ({
|
||||
message: {
|
||||
id: row.messageId,
|
||||
role: row.role,
|
||||
text: row.text,
|
||||
turnId: row.turnId,
|
||||
streaming: row.isStreaming === 1,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
...(row.attachments !== null ? { attachments: row.attachments } : {}),
|
||||
},
|
||||
hasOtherUserMessages: row.hasOtherUserMessages === 1,
|
||||
}));
|
||||
});
|
||||
|
||||
// Contiguous turn range bounding a windowed detail read; undefined loads the
|
||||
// full thread. Resolved from a window request inside the snapshot
|
||||
// transaction (see getThreadDetailSnapshot).
|
||||
@@ -3326,6 +3393,7 @@ pending_approval_requests AS (
|
||||
getFullThreadDiffContext,
|
||||
getThreadShellById,
|
||||
getThreadRuntimeContext,
|
||||
getTurnStartMessage,
|
||||
getThreadDetailById,
|
||||
getThreadDetailSnapshot,
|
||||
} satisfies ProjectionSnapshotQueryShape;
|
||||
|
||||
Reference in new issue
Block a user