Skip to content

Commit 4e193be

Browse files
committed
Respect configured inbound history cap
1 parent abffed8 commit 4e193be

5 files changed

Lines changed: 48 additions & 3 deletions

File tree

‎extensions/discord/src/monitor/message-handler.context.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ export async function buildDiscordMessageProcessContext(params: {
310310
Body: combinedBody,
311311
BodyForAgent: preflightAudioTranscript ?? baseText ?? text,
312312
InboundHistory: inboundHistory,
313+
InboundHistoryLimit: historyLimit,
313314
RawBody: preflightAudioTranscript ?? baseText,
314315
CommandBody: preflightAudioTranscript ?? baseText,
315316
...(preflightAudioTranscript !== undefined ? { Transcript: preflightAudioTranscript } : {}),

‎src/auto-reply/reply/get-reply-run.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,10 @@ export async function runPreparedReply(
609609
? (bareResetPromptState?.prompt ?? "")
610610
: stripPromptThinkingDirectives(baseBody);
611611
const envelopeOptions = resolveEnvelopeFormatOptions(cfg);
612+
const inboundHistoryLimit =
613+
typeof sessionCtx.InboundHistoryLimit === "number"
614+
? sessionCtx.InboundHistoryLimit
615+
: cfg.messages?.groupChat?.historyLimit;
612616
const inboundUserContext = buildInboundUserContextPrefix(
613617
isNewSession
614618
? {
@@ -619,6 +623,7 @@ export async function runPreparedReply(
619623
}
620624
: { ...sessionCtx, ThreadStarterBody: undefined },
621625
envelopeOptions,
626+
{ historyLimit: inboundHistoryLimit },
622627
);
623628
const baseBodyForPrompt = isBareSessionReset
624629
? [

‎src/auto-reply/reply/inbound-meta.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,4 +686,28 @@ describe("buildInboundUserContextPrefix", () => {
686686
expect(history[0]?.["body"]).toBe("body-5");
687687
expect(history.at(-1)?.["body"]).toBe("body-24");
688688
});
689+
690+
it("uses the configured inbound history cap when provided", () => {
691+
const text = buildInboundUserContextPrefix(
692+
{
693+
ChatType: "group",
694+
InboundHistory: Array.from({ length: 25 }, (_, index) => ({
695+
sender: `sender-${index}`,
696+
body: `body-${index}`,
697+
timestamp: index,
698+
})),
699+
} as TemplateContext,
700+
undefined,
701+
{ historyLimit: 25 },
702+
);
703+
704+
const conversationInfo = parseConversationInfoPayload(text);
705+
expect(conversationInfo["history_count"]).toBe(25);
706+
expect(conversationInfo["history_truncated"]).toBeUndefined();
707+
708+
const history = parseHistoryPayload(text);
709+
expect(history).toHaveLength(25);
710+
expect(history[0]?.["body"]).toBe("body-0");
711+
expect(history.at(-1)?.["body"]).toBe("body-24");
712+
});
689713
});

‎src/auto-reply/reply/inbound-meta.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { formatEnvelopeTimestamp } from "../envelope.js";
1010
import type { TemplateContext } from "../templating.js";
1111

1212
const MAX_UNTRUSTED_JSON_STRING_CHARS = 2_000;
13-
const MAX_UNTRUSTED_HISTORY_ENTRIES = 20;
13+
const DEFAULT_UNTRUSTED_HISTORY_ENTRIES = 20;
14+
15+
type InboundUserContextPrefixOptions = {
16+
historyLimit?: number;
17+
};
1418

1519
function stripNullBytes(value: string): string {
1620
return value.replaceAll("\u0000", "");
@@ -44,6 +48,13 @@ function truncateUntrustedJsonString(value: string): string {
4448
return `${truncateUtf16Safe(value, Math.max(0, MAX_UNTRUSTED_JSON_STRING_CHARS - 14)).trimEnd()}…[truncated]`;
4549
}
4650

51+
function resolveUntrustedHistoryEntryLimit(value: unknown): number {
52+
if (typeof value !== "number" || !Number.isFinite(value)) {
53+
return DEFAULT_UNTRUSTED_HISTORY_ENTRIES;
54+
}
55+
return Math.max(0, Math.floor(value));
56+
}
57+
4758
function sanitizeUntrustedJsonValue(value: unknown): unknown {
4859
if (typeof value === "string") {
4960
return neutralizeMarkdownFences(truncateUntrustedJsonString(value));
@@ -178,6 +189,7 @@ export function buildInboundMetaSystemPrompt(
178189
export function buildInboundUserContextPrefix(
179190
ctx: TemplateContext,
180191
envelope?: EnvelopeFormatOptions,
192+
options?: InboundUserContextPrefixOptions,
181193
): string {
182194
const blocks: string[] = [];
183195
const chatType = normalizeChatType(ctx.ChatType);
@@ -193,7 +205,8 @@ export function buildInboundUserContextPrefix(
193205
const resolvedMessageId = messageId ?? messageIdFull;
194206
const timestampStr = formatConversationTimestamp(ctx.Timestamp, envelope);
195207
const inboundHistory = Array.isArray(ctx.InboundHistory) ? ctx.InboundHistory : [];
196-
const boundedHistory = inboundHistory.slice(-MAX_UNTRUSTED_HISTORY_ENTRIES);
208+
const historyLimit = resolveUntrustedHistoryEntryLimit(options?.historyLimit);
209+
const boundedHistory = historyLimit > 0 ? inboundHistory.slice(-historyLimit) : [];
197210

198211
// Keep volatile conversation/message identifiers in the user-role block so the system
199212
// prompt stays byte-stable across task-scoped sessions and reply turns.
@@ -231,7 +244,7 @@ export function buildInboundUserContextPrefix(
231244
has_forwarded_context: normalizePromptMetadataString(ctx.ForwardedFrom) ? true : undefined,
232245
has_thread_starter: sanitizePromptBody(ctx.ThreadStarterBody) ? true : undefined,
233246
history_count: boundedHistory.length > 0 ? boundedHistory.length : undefined,
234-
history_truncated: inboundHistory.length > MAX_UNTRUSTED_HISTORY_ENTRIES ? true : undefined,
247+
history_truncated: inboundHistory.length > historyLimit ? true : undefined,
235248
};
236249
if (Object.values(conversationInfo).some((v) => v !== undefined)) {
237250
blocks.push(

‎src/auto-reply/templating.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export type MsgContext = {
4444
body: string;
4545
timestamp?: number;
4646
}>;
47+
/** Maximum number of InboundHistory entries to serialize into prompt metadata. */
48+
InboundHistoryLimit?: number;
4749
/**
4850
* @deprecated Use CommandBody.
4951
*

0 commit comments

Comments
 (0)