Skip to content

Commit 3988315

Browse files
authored
perf(sessions): update existing transcript watermarks directly (#133403)
1 parent 272527e commit 3988315

1 file changed

Lines changed: 29 additions & 34 deletions

File tree

‎src/config/sessions/session-transcript-index.ts‎

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,30 +163,28 @@ function writeWatermark(
163163
sessionId: string,
164164
watermark: SessionTranscriptProjectionState,
165165
now: number,
166+
updateExisting = false,
166167
): SessionTranscriptProjectionState {
168+
const kysely = getIndexKysely(db);
169+
const values = {
170+
active_event_count: watermark.activeEventCount,
171+
active_message_count: watermark.activeMessageCount,
172+
indexed_seq: watermark.indexedSeq,
173+
leaf_event_id: watermark.leafEventId,
174+
needs_rebuild: watermark.needsRebuild ? 1 : 0,
175+
updated_at: now,
176+
};
167177
executeSqliteQuerySync(
168178
db,
169-
getIndexKysely(db)
170-
.insertInto("session_transcript_index_state")
171-
.values({
172-
session_id: sessionId,
173-
active_event_count: watermark.activeEventCount,
174-
active_message_count: watermark.activeMessageCount,
175-
indexed_seq: watermark.indexedSeq,
176-
leaf_event_id: watermark.leafEventId,
177-
needs_rebuild: watermark.needsRebuild ? 1 : 0,
178-
updated_at: now,
179-
})
180-
.onConflict((conflict) =>
181-
conflict.column("session_id").doUpdateSet({
182-
active_event_count: watermark.activeEventCount,
183-
active_message_count: watermark.activeMessageCount,
184-
indexed_seq: watermark.indexedSeq,
185-
leaf_event_id: watermark.leafEventId,
186-
needs_rebuild: watermark.needsRebuild ? 1 : 0,
187-
updated_at: now,
188-
}),
189-
),
179+
updateExisting
180+
? kysely
181+
.updateTable("session_transcript_index_state")
182+
.set(values)
183+
.where("session_id", "=", sessionId)
184+
: kysely
185+
.insertInto("session_transcript_index_state")
186+
.values({ session_id: sessionId, ...values })
187+
.onConflict((conflict) => conflict.column("session_id").doUpdateSet(values)),
190188
);
191189
return watermark;
192190
}
@@ -266,13 +264,7 @@ export function createTranscriptIndexAppenderInTransaction(
266264
// transcripts): stay unindexed until reconcile rebuilds the session.
267265
return true;
268266
}
269-
watermark = applyForwardIndex(db, sessionId, params, {
270-
activeEventCount: 0,
271-
activeMessageCount: 0,
272-
indexedSeq: -1,
273-
leafEventId: null,
274-
needsRebuild: false,
275-
});
267+
watermark = applyForwardIndex(db, sessionId, params, watermark);
276268
return false;
277269
}
278270
if (watermark.needsRebuild) {
@@ -328,7 +320,7 @@ function applyForwardIndex(
328320
db: DatabaseSync,
329321
sessionId: string,
330322
params: TranscriptIndexAppend,
331-
watermark: SessionTranscriptProjectionState,
323+
watermark: SessionTranscriptProjectionState | undefined,
332324
): SessionTranscriptProjectionState {
333325
const entry = extractTranscriptIndexEntry(params.event, params.createdAt);
334326
if (entry) {
@@ -338,10 +330,10 @@ function applyForwardIndex(
338330
const projectsMessage = projectsActiveEvent && hasTranscriptMessage(params.event);
339331
if (projectsActiveEvent) {
340332
insertActiveEventRow(db, {
341-
activePosition: watermark.activeEventCount,
333+
activePosition: watermark?.activeEventCount ?? 0,
342334
contextEligible: transcriptEventContextEligibility(params.event),
343335
eventSeq: params.seq,
344-
messagePosition: projectsMessage ? watermark.activeMessageCount : null,
336+
messagePosition: projectsMessage ? (watermark?.activeMessageCount ?? 0) : null,
345337
sessionId,
346338
});
347339
}
@@ -353,13 +345,16 @@ function applyForwardIndex(
353345
db,
354346
sessionId,
355347
{
356-
activeEventCount: watermark.activeEventCount + (projectsActiveEvent ? 1 : 0),
357-
activeMessageCount: watermark.activeMessageCount + (projectsMessage ? 1 : 0),
348+
activeEventCount: (watermark?.activeEventCount ?? 0) + (projectsActiveEvent ? 1 : 0),
349+
activeMessageCount: (watermark?.activeMessageCount ?? 0) + (projectsMessage ? 1 : 0),
358350
indexedSeq: params.seq,
359-
leafEventId: advancesLeaf ? params.eventId : watermark.leafEventId,
351+
leafEventId: advancesLeaf ? params.eventId : (watermark?.leafEventId ?? null),
360352
needsRebuild: false,
361353
},
362354
params.createdAt,
355+
// The synchronous appender owns this row until its batch ends; initialization
356+
// still upserts, while subsequent events avoid repeated conflict resolution.
357+
watermark !== undefined,
363358
);
364359
}
365360

0 commit comments

Comments
 (0)