refactor(telegram): fold reset boundary lookup

This commit is contained in:
Ayaan Zaidi
2026-06-02 21:31:22 +05:30
parent d76f2c0c3b
commit a9f014e9df
2 changed files with 51 additions and 20 deletions

View File

@@ -760,6 +760,45 @@ describe("telegram message cache", () => {
expect(context.map((entry) => entry.node.body)).not.toContain(staleInstruction);
});
it("uses the current reset command as the session boundary", async () => {
const cache = createTelegramMessageCache();
const chat = { id: 7, type: "group", title: "Ops" } as const;
await cache.record({
accountId: "default",
chatId: 7,
msg: {
chat,
message_id: 100,
date: 1736380800,
text: "stale context",
from: { id: 100, is_bot: false, first_name: "Requester" },
} as Message,
});
await cache.record({
accountId: "default",
chatId: 7,
msg: {
chat,
message_id: 101,
date: 1736380860,
text: "/new",
from: { id: 101, is_bot: false, first_name: "Requester" },
} as Message,
});
const context = await buildTelegramConversationContext({
cache,
accountId: "default",
chatId: 7,
messageId: "101",
replyChainNodes: [],
recentLimit: 10,
replyTargetWindowSize: 1,
});
expect(context).toEqual([]);
});
it("does not select messages before the persisted session start when the reset command is absent", async () => {
const cache = createTelegramMessageCache();
const beforeSession = Date.parse("2026-05-10T12:40:00.000Z");

View File

@@ -54,7 +54,7 @@ export type TelegramMessageCache = {
before: number;
after: number;
}) => Promise<TelegramCachedMessageNode[]>;
latestBeforeMatching: (params: {
latestMatchingAtOrBefore: (params: {
accountId: string;
chatId: string | number;
messageId?: string;
@@ -719,7 +719,7 @@ export function createTelegramMessageCache(params?: {
targetIndex + Math.max(0, after) + 1,
);
},
latestBeforeMatching: async ({ accountId, chatId, messageId, threadId, matches }) => {
latestMatchingAtOrBefore: async ({ accountId, chatId, messageId, threadId, matches }) => {
if (!messageId) {
return null;
}
@@ -744,7 +744,7 @@ export function createTelegramMessageCache(params?: {
continue;
}
const entryId = parseSafeMessageId(entry.messageId);
if (entryId === undefined || entryId >= targetId || !matches(entry)) {
if (entryId === undefined || entryId > targetId || !matches(entry)) {
continue;
}
if (!latest || compareCachedMessageNodes(entry, latest) > 0) {
@@ -830,23 +830,15 @@ async function resolveSessionBoundaryNode(params: {
if (!params.messageId) {
return undefined;
}
const { messageId } = params;
const latestBefore = await params.cache.latestBeforeMatching({
accountId: params.accountId,
chatId: params.chatId,
messageId,
...(params.threadId !== undefined ? { threadId: params.threadId } : {}),
matches: isSessionBoundaryCommandNode,
});
const current = await params.cache.get({
accountId: params.accountId,
chatId: params.chatId,
messageId,
});
if (current && isSessionBoundaryCommandNode(current)) {
return current;
}
return latestBefore ?? undefined;
return (
(await params.cache.latestMatchingAtOrBefore({
accountId: params.accountId,
chatId: params.chatId,
messageId: params.messageId,
...(params.threadId !== undefined ? { threadId: params.threadId } : {}),
matches: isSessionBoundaryCommandNode,
})) ?? undefined
);
}
export async function buildTelegramReplyChain(params: {