Skip to content

Commit 78d1512

Browse files
perf(memory): Read only the current History version's Turn contexts
Prompt history loaded every turn_context row of the Conversation and dropped older History versions in memory, so its cost grew with every compaction. The event store now reads one type in the current version in SQL.
1 parent c551115 commit 78d1512

5 files changed

Lines changed: 63 additions & 9 deletions

File tree

‎packages/junior/src/chat/conversations/history.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,11 @@ export interface ConversationEventStore {
749749
conversationId: string,
750750
type: KnownConversationEventType,
751751
): Promise<ConversationEvent[]>;
752+
/** Events of one type in the current history version in `seq` order. */
753+
loadCurrentEventsOfType(
754+
conversationId: string,
755+
type: KnownConversationEventType,
756+
): Promise<ConversationEvent[]>;
752757
/**
753758
* Bounded raw event page for observational/debug reads.
754759
* Defaults to the newest matching events when neither bound is set.

‎packages/junior/src/chat/conversations/sql/history.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,33 @@ class SqlConversationEventStore implements ConversationEventStore {
592592
return rows.map(eventFromRow);
593593
}
594594

595+
async loadCurrentEventsOfType(
596+
conversationId: string,
597+
type: KnownConversationEventType,
598+
): Promise<ConversationEvent[]> {
599+
const db = this.executor.db();
600+
// One statement, as in `loadCurrentHistory`. Older versions stay out of
601+
// the read, so its cost does not grow with every compaction.
602+
const currentHistoryVersion = db
603+
.select({
604+
historyVersion: sql<number>`max(${juniorConversationEvents.historyVersion})`,
605+
})
606+
.from(juniorConversationEvents)
607+
.where(eq(juniorConversationEvents.conversationId, conversationId));
608+
const rows = await db
609+
.select()
610+
.from(juniorConversationEvents)
611+
.where(
612+
and(
613+
eq(juniorConversationEvents.conversationId, conversationId),
614+
eq(juniorConversationEvents.historyVersion, currentHistoryVersion),
615+
eq(juniorConversationEvents.type, type),
616+
),
617+
)
618+
.orderBy(asc(juniorConversationEvents.seq));
619+
return rows.map(eventFromRow);
620+
}
621+
595622
async query(
596623
conversationId: string,
597624
query: ConversationEventQuery,

‎packages/junior/src/chat/plugins/prompt-history.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ export async function loadPluginPromptHistory(args: {
1818
limit: 1,
1919
types: ["compaction", "handoff"],
2020
}),
21-
eventStore.loadEventsOfType(args.conversationId, "turn_context"),
21+
eventStore.loadCurrentEventsOfType(args.conversationId, "turn_context"),
2222
]);
2323
const replacement = replacements.events[0];
24-
const historyVersion = replacement?.historyVersion ?? 0;
2524
return {
2625
...(replacement ? { startedAtMs: replacement.createdAtMs } : undefined),
2726
contexts: contextEvents.flatMap((event) =>
28-
event.historyVersion === historyVersion &&
2927
event.data.type === "turn_context" &&
3028
event.data.pluginName === args.pluginName
3129
? [

‎packages/junior/tests/component/plugins/plugin-conversation-events.test.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,15 @@ describe("plugin conversation events", () => {
158158
},
159159
});
160160
const histories: unknown[] = [];
161-
const turnContext = (pluginName: string) => ({
162-
createdAtMs: 1_000,
161+
const turnContext = (pluginName: string, createdAtMs = 1_000) => ({
162+
createdAtMs,
163163
data: {
164164
type: "turn_context" as const,
165-
turnId: "earlier-turn",
165+
turnId: `turn-${createdAtMs}`,
166166
pluginName,
167167
kind: "recall",
168168
version: 1,
169-
content: { memories: [`${pluginName} memory`] },
169+
content: { memories: [`${pluginName} memory ${createdAtMs}`] },
170170
},
171171
});
172172
const { getPluginUserPromptContributions, setPlugins } =
@@ -199,6 +199,9 @@ describe("plugin conversation events", () => {
199199
},
200200
});
201201
await getPluginUserPromptContributions(request);
202+
await eventStore.append(conversationId, [
203+
turnContext("prompt-event-demo", 3_000),
204+
]);
202205
// Another Turn of the same Conversation emits its own event.
203206
await getPluginUserPromptContributions({
204207
...request,
@@ -213,13 +216,24 @@ describe("plugin conversation events", () => {
213216
{
214217
kind: "recall",
215218
version: 1,
216-
content: { memories: ["prompt-event-demo memory"] },
219+
content: { memories: ["prompt-event-demo memory 1000"] },
217220
loadedAtMs: 1_000,
218221
},
219222
],
220223
},
221224
{ startedAtMs: 2_000, contexts: [] },
222-
{ startedAtMs: 2_000, contexts: [] },
225+
// Only the context of the current History version comes back.
226+
{
227+
startedAtMs: 2_000,
228+
contexts: [
229+
{
230+
kind: "recall",
231+
version: 1,
232+
content: { memories: ["prompt-event-demo memory 3000"] },
233+
loadedAtMs: 3_000,
234+
},
235+
],
236+
},
223237
]);
224238
const events = await eventStore.loadHistory(conversationId);
225239
expect(

‎packages/junior/tests/unit/conversations/turn-lifecycle.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ class MemoryConversationEventStore implements ConversationEventStore {
104104
return this.history.filter((event) => event.data.type === type);
105105
}
106106

107+
async loadCurrentEventsOfType(
108+
conversationId: string,
109+
type: string,
110+
): Promise<ConversationEvent[]> {
111+
const version = await this.loadCurrentHistoryVersion();
112+
return (await this.loadEventsOfType(conversationId, type)).filter(
113+
(event) => event.historyVersion === version,
114+
);
115+
}
116+
107117
async query(
108118
_conversationId: string,
109119
query: ConversationEventQuery,

0 commit comments

Comments
 (0)