fix(auto-reply): move volatile inbound flags out of system metadata

Co-authored-by: aidiffuser <aidiffuser@users.noreply.github.com>
This commit is contained in:
Peter Steinberger
2026-02-23 19:05:57 +00:00
parent cf38339f25
commit 31e4c21b67
3 changed files with 49 additions and 11 deletions

View File

@@ -16,9 +16,9 @@ export function buildInboundMetaSystemPrompt(ctx: TemplateContext): string {
// Keep system metadata strictly free of attacker-controlled strings (sender names, group subjects, etc.).
// Those belong in the user-role "untrusted context" blocks.
// Per-message identifiers (message_id, reply_to_id, sender_id) are also excluded here: they change
// on every turn and would bust prefix-based prompt caches on local model providers. They are
// included in the user-role conversation info block via buildInboundUserContextPrefix() instead.
// Per-message identifiers and dynamic flags are also excluded here: they change on turns/replies
// and would bust prefix-based prompt caches on providers that use stable system prefixes.
// They are included in the user-role conversation info block instead.
// Resolve channel identity: prefer explicit channel, then surface, then provider.
// For webchat/Hub Chat sessions (when Surface is 'webchat' or undefined with no real channel),
@@ -43,14 +43,6 @@ export function buildInboundMetaSystemPrompt(ctx: TemplateContext): string {
provider: safeTrim(ctx.Provider),
surface: safeTrim(ctx.Surface),
chat_type: chatType ?? (isDirect ? "direct" : undefined),
flags: {
is_group_chat: !isDirect ? true : undefined,
was_mentioned: ctx.WasMentioned === true ? true : undefined,
has_reply_context: Boolean(ctx.ReplyToBody),
has_forwarded_context: Boolean(ctx.ForwardedFrom),
has_thread_starter: Boolean(safeTrim(ctx.ThreadStarterBody)),
history_count: Array.isArray(ctx.InboundHistory) ? ctx.InboundHistory.length : 0,
},
};
// Keep the instructions local to the payload so the meaning survives prompt overrides.
@@ -92,7 +84,15 @@ export function buildInboundUserContextPrefix(ctx: TemplateContext): string {
group_space: safeTrim(ctx.GroupSpace),
thread_label: safeTrim(ctx.ThreadLabel),
is_forum: ctx.IsForum === true ? true : undefined,
is_group_chat: !isDirect ? true : undefined,
was_mentioned: ctx.WasMentioned === true ? true : undefined,
has_reply_context: ctx.ReplyToBody ? true : undefined,
has_forwarded_context: ctx.ForwardedFrom ? true : undefined,
has_thread_starter: safeTrim(ctx.ThreadStarterBody) ? true : undefined,
history_count:
Array.isArray(ctx.InboundHistory) && ctx.InboundHistory.length > 0
? ctx.InboundHistory.length
: undefined,
};
if (Object.values(conversationInfo).some((v) => v !== undefined)) {
blocks.push(