fix: scope history injection to pending-only

This commit is contained in:
Peter Steinberger
2026-01-16 23:52:14 +00:00
parent 56ed5cc2d9
commit e31251293b
21 changed files with 278 additions and 175 deletions

View File

@@ -106,7 +106,7 @@ describe("web auto-reply", () => {
vi.useRealTimers();
});
it("supports always-on group activation with silent token and preserves history", async () => {
it("supports always-on group activation with silent token and clears pending history", async () => {
const sendMedia = vi.fn();
const reply = vi.fn().mockResolvedValue(undefined);
const sendComposing = vi.fn();
@@ -180,9 +180,9 @@ describe("web auto-reply", () => {
expect(resolver).toHaveBeenCalledTimes(2);
const payload = resolver.mock.calls[1][0];
expect(payload.Body).toContain("Chat messages since your last reply");
expect(payload.Body).toContain("Alice (+111): first");
expect(payload.Body).toContain("[message_id: g-always-1]");
expect(payload.Body).not.toContain("Chat messages since your last reply");
expect(payload.Body).not.toContain("Alice (+111): first");
expect(payload.Body).not.toContain("[message_id: g-always-1]");
expect(payload.Body).toContain("Bob: second");
expect(reply).toHaveBeenCalledTimes(1);

View File

@@ -77,17 +77,15 @@ export async function maybeBroadcastMessage(params: {
}
};
let didSendReply = false;
if (strategy === "sequential") {
for (const agentId of broadcastAgents) {
if (await processForAgent(agentId)) didSendReply = true;
await processForAgent(agentId);
}
} else {
const results = await Promise.allSettled(broadcastAgents.map(processForAgent));
didSendReply = results.some((result) => result.status === "fulfilled" && result.value);
await Promise.allSettled(broadcastAgents.map(processForAgent));
}
if (params.msg.chatType === "group" && didSendReply) {
if (params.msg.chatType === "group") {
params.groupHistories.set(params.groupHistoryKey, []);
}

View File

@@ -6,6 +6,7 @@ import { resolveMentionGating } from "../../../channels/mention-gating.js";
import type { MentionConfig } from "../mentions.js";
import { buildMentionConfig, debugMention, resolveOwnerList } from "../mentions.js";
import type { WebInboundMsg } from "../types.js";
import { recordPendingHistoryEntry } from "../../../auto-reply/reply/history.js";
import { stripMentionsForCommand } from "./commands.js";
import { resolveGroupActivationFor, resolveGroupPolicyFor } from "./group-activation.js";
import { noteGroupMember } from "./group-members.js";
@@ -65,26 +66,27 @@ export function applyGroupGating(params: {
if (activationCommand.hasCommand && !owner) {
params.logVerbose(`Ignoring /activation from non-owner in group ${params.conversationId}`);
if (params.groupHistoryLimit > 0) {
const sender =
params.msg.senderName && params.msg.senderE164
? `${params.msg.senderName} (${params.msg.senderE164})`
: (params.msg.senderName ?? params.msg.senderE164 ?? "Unknown");
recordPendingHistoryEntry({
historyMap: params.groupHistories,
historyKey: params.groupHistoryKey,
limit: params.groupHistoryLimit,
entry: {
sender,
body: params.msg.body,
timestamp: params.msg.timestamp,
id: params.msg.id,
senderJid: params.msg.senderJid,
},
});
}
return { shouldProcess: false };
}
if (!shouldBypassMention) {
const history = params.groupHistories.get(params.groupHistoryKey) ?? [];
const sender =
params.msg.senderName && params.msg.senderE164
? `${params.msg.senderName} (${params.msg.senderE164})`
: (params.msg.senderName ?? params.msg.senderE164 ?? "Unknown");
history.push({
sender,
body: params.msg.body,
timestamp: params.msg.timestamp,
id: params.msg.id,
senderJid: params.msg.senderJid,
});
while (history.length > params.groupHistoryLimit) history.shift();
params.groupHistories.set(params.groupHistoryKey, history);
}
const mentionDebug = debugMention(params.msg, mentionConfig, params.authDir);
params.replyLogger.debug(
{
@@ -124,6 +126,24 @@ export function applyGroupGating(params: {
params.logVerbose(
`Group message stored for context (no mention detected) in ${params.conversationId}: ${params.msg.body}`,
);
if (params.groupHistoryLimit > 0) {
const sender =
params.msg.senderName && params.msg.senderE164
? `${params.msg.senderName} (${params.msg.senderE164})`
: (params.msg.senderName ?? params.msg.senderE164 ?? "Unknown");
recordPendingHistoryEntry({
historyMap: params.groupHistories,
historyKey: params.groupHistoryKey,
limit: params.groupHistoryLimit,
entry: {
sender,
body: params.msg.body,
timestamp: params.msg.timestamp,
id: params.msg.id,
senderJid: params.msg.senderJid,
},
});
}
return { shouldProcess: false };
}

View File

@@ -76,10 +76,9 @@ export async function processMessage(params: {
if (params.msg.chatType === "group") {
const history = params.groupHistory ?? params.groupHistories.get(params.groupHistoryKey) ?? [];
const historyWithoutCurrent = history.length > 0 ? history.slice(0, -1) : [];
if (historyWithoutCurrent.length > 0) {
if (history.length > 0) {
const lineBreak = "\\n";
const historyText = historyWithoutCurrent
const historyText = history
.map((m) => {
const bodyWithId = m.id ? `${m.body}\n[message_id: ${m.id}]` : m.body;
return formatAgentEnvelope({
@@ -299,14 +298,14 @@ export async function processMessage(params: {
});
if (!queuedFinal) {
if (shouldClearGroupHistory && didSendReply) {
if (shouldClearGroupHistory) {
params.groupHistories.set(params.groupHistoryKey, []);
}
logVerbose("Skipping auto-reply: silent token or no text/media returned from resolver");
return false;
}
if (shouldClearGroupHistory && didSendReply) {
if (shouldClearGroupHistory) {
params.groupHistories.set(params.groupHistoryKey, []);
}