Skip to content

Commit f99b6a4

Browse files
authored
fix(reply): dedupe room-event prompt envelope and make ambient turns cache-stable
Room-event turns now send the persisted attributed chat line as the active model body, so the turn replays byte-identically and the provider prefix cache survives ambient traffic. The room-event context block keeps only the marker, room backlog, and reply-policy directive: the duplicate marker, bare inbound_event_kind line, and Current event copy of the chatter are gone, and the dead Current event goal anchor is removed from inbound-meta. Net -18 production LOC; quiet-room behavior unchanged (message tool still required for visible replies).
1 parent d8147f8 commit f99b6a4

4 files changed

Lines changed: 39 additions & 60 deletions

File tree

‎src/auto-reply/reply/get-reply-run.media-only.test.ts‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function createGatewayDrainingError(): Error {
161161
}
162162

163163
const ROOM_EVENT_MESSAGE_TOOL_DIRECTIVE =
164-
"Treat this as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way.";
164+
"Treat the current message as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way.";
165165

166166
function createInboundBody<T extends string>(body: T) {
167167
return { Body: body, RawBody: body, CommandBody: body };
@@ -2285,9 +2285,9 @@ describe("runPreparedReply media-only handling", () => {
22852285
);
22862286

22872287
const call = requireLastRunReplyAgentCall();
2288-
expect(call?.commandBody).toBe("[OpenClaw room event]");
2288+
expect(call?.commandBody).toBe("#35676 Keśava: No wtf");
22892289
expect(call?.transcriptCommandBody).toBe("#35676 Keśava: No wtf");
2290-
expect(call?.followupRun.prompt).toBe("[OpenClaw room event]");
2290+
expect(call?.followupRun.prompt).toBe("#35676 Keśava: No wtf");
22912291
expect(call?.followupRun.transcriptPrompt).toBe("#35676 Keśava: No wtf");
22922292
expect(call?.followupRun.currentInboundEventKind).toBe("room_event");
22932293
expect(call?.followupRun.currentInboundAudio).toBe(true);
@@ -2334,9 +2334,7 @@ describe("runPreparedReply media-only handling", () => {
23342334
ROOM_EVENT_MESSAGE_TOOL_DIRECTIVE,
23352335
);
23362336
expect(call?.followupRun.currentInboundContext?.text).not.toContain("visible_reply_contract:");
2337-
expect(call?.followupRun.currentInboundContext?.text).toContain(
2338-
"Current event:\n#35676 Keśava: No wtf",
2339-
);
2337+
expect(call?.followupRun.currentInboundContext?.text).not.toContain("Current event:");
23402338
});
23412339

23422340
it("queues active room events as followups instead of steering fake prompts", async () => {
@@ -2378,10 +2376,10 @@ describe("runPreparedReply media-only handling", () => {
23782376
expect(call.shouldFollowup).toBe(true);
23792377
expect(call.isActive).toBe(true);
23802378
expect(call.resolvedQueue.mode).toBe("steer");
2381-
expect(call.followupRun.prompt).toBe("[OpenClaw room event]");
2379+
expect(call.followupRun.prompt).toBe("#992 Alice: ambient");
23822380
expect(call.followupRun.currentInboundEventKind).toBe("room_event");
23832381
expect(call.followupRun.abortSignal).toBe(abortController.signal);
2384-
expect(call.followupRun.currentInboundContext?.text).toContain("Current event:");
2382+
expect(call.followupRun.currentInboundContext?.text).toContain("Room context:");
23852383
});
23862384

23872385
it("uses queued followup abort ownership instead of borrowed active-lane abort ownership", async () => {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ function refreshActiveGoalContextText(params: {
7373
return retained.join("\n\n");
7474
}
7575
if (insertionIndex === undefined) {
76-
const anchorIndex = retained.findLastIndex(
77-
(block) => block.startsWith("Current message:") || block.startsWith("Current event:"),
78-
);
76+
const anchorIndex = retained.findLastIndex((block) => block.startsWith("Current message:"));
7977
insertionIndex = anchorIndex >= 0 ? anchorIndex : retained.length;
8078
}
8179
retained.splice(Math.min(insertionIndex, retained.length), 0, params.activeGoalContext);

‎src/auto-reply/reply/prompt-prelude.test.ts‎

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,16 @@ describe("buildReplyPromptEnvelope", () => {
153153
sourceReplyDeliveryMode: "message_tool_only",
154154
});
155155

156-
expect(envelope.prefixedCommandBody).toBe("[OpenClaw room event]");
157-
expect(envelope.queuedBody).toBe("[OpenClaw room event]");
156+
// The active room-event prompt is the attributed transcript row itself, so
157+
// the turn replays byte-identically as history instead of swapping a
158+
// placeholder marker for the chat line on the next request.
159+
expect(envelope.prefixedCommandBody).toBe("#35676 Keśava: No wtf");
160+
expect(envelope.queuedBody).toBe("#35676 Keśava: No wtf");
158161
expect(envelope.transcriptCommandBody).toBe("#35676 Keśava: No wtf");
162+
expect(envelope.queuedBody).toBe(envelope.transcriptCommandBody);
159163
expect(envelope.currentInboundContext?.text).toBe(
160164
[
161165
"[OpenClaw room event]",
162-
"inbound_event_kind: room_event",
163166
[
164167
"Room context:",
165168
"Conversation info:",
@@ -171,23 +174,24 @@ describe("buildReplyPromptEnvelope", () => {
171174
"#35674 Other: I wish I could enjoy 5.5",
172175
"#35675 User ->#35674: Are you fr fr",
173176
].join("\n"),
174-
"Current event:\n#35676 Keśava: No wtf",
175-
"Treat this as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way.",
177+
"Treat the current message as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way.",
176178
].join("\n\n"),
177179
);
180+
// Each room-event fact appears exactly once per request: kind lives in the
181+
// Conversation info JSON, the event line lives in the user turn body.
182+
expect(envelope.currentInboundContext?.text).not.toContain("inbound_event_kind: room_event\n");
183+
expect(envelope.currentInboundContext?.text).not.toContain("Current event:");
178184
expect(envelope.currentInboundContext?.resumableText).toBe(
179185
[
180186
"[OpenClaw room event]",
181-
"inbound_event_kind: room_event",
182187
[
183188
"Room context:",
184189
"Conversation info:",
185190
"```json",
186191
JSON.stringify({ message_id: "35676", inbound_event_kind: "room_event" }, null, 2),
187192
"```",
188193
].join("\n"),
189-
"Current event:\n#35676 Keśava: No wtf",
190-
"Treat this as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way.",
194+
"Treat the current message as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way.",
191195
].join("\n\n"),
192196
);
193197
expect(envelope.currentInboundContext?.resumableText).not.toContain(
@@ -220,9 +224,8 @@ describe("buildReplyPromptEnvelope", () => {
220224
});
221225

222226
expect(envelope.transcriptCommandBody).toBe(ambientTranscriptBody);
223-
expect(envelope.currentInboundContext?.text).toContain(
224-
`Current event:\n${ambientTranscriptBody}`,
225-
);
227+
expect(envelope.queuedBody).toBe(ambientTranscriptBody);
228+
expect(envelope.currentInboundContext?.text).not.toContain(ambientTranscriptBody);
226229
});
227230

228231
it("uses the raw current body for room-event current event text", () => {
@@ -251,19 +254,15 @@ describe("buildReplyPromptEnvelope", () => {
251254

252255
expect(envelope.currentInboundContext?.text).toContain("Room context:");
253256
expect(envelope.currentInboundContext?.text).toContain("Alice: old context");
257+
expect(envelope.queuedBody).toBe("#2002 Bob: current note");
254258
expect(envelope.currentInboundContext?.text).toContain(
255-
"Current event:\n#2002 Bob: current note",
256-
);
257-
expect(envelope.currentInboundContext?.text).toContain(
258-
"Treat this as observed room activity. Default: no reply; most room events need no response from you. Reply only when you are directly addressed or have concrete value to add.",
259+
"Treat the current message as observed room activity. Default: no reply; most room events need no response from you. Reply only when you are directly addressed or have concrete value to add.",
259260
);
260261
expect(envelope.currentInboundContext?.text).not.toContain("message(action=send)");
261262
expect(envelope.currentInboundContext?.text).not.toContain(
262263
"your final text here stays private",
263264
);
264-
expect(envelope.currentInboundContext?.text).not.toContain(
265-
"Current event:\n#2002 Bob: [Chat history]",
266-
);
265+
expect(envelope.queuedBody).not.toContain("[Chat history]");
267266
});
268267

269268
it("keeps media-only notes in ordinary user request transcripts", () => {

‎src/auto-reply/reply/prompt-prelude.ts‎

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ function resolvePerTurnDeliveryDirective(params: {
159159
}): string | undefined {
160160
if (params.inboundEventKind === "room_event") {
161161
return params.sourceReplyDeliveryMode === "message_tool_only"
162-
? "Treat this as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way."
163-
: "Treat this as observed room activity. Default: no reply; most room events need no response from you. Reply only when you are directly addressed or have concrete value to add.";
162+
? "Treat the current message as observed room activity. Default: no reply; most room events need no response from you. Send a visible reply via message(action=send) only when you are directly addressed or have concrete value to add; your final text here stays private either way."
163+
: "Treat the current message as observed room activity. Default: no reply; most room events need no response from you. Reply only when you are directly addressed or have concrete value to add.";
164164
}
165165
if (
166166
params.inboundEventKind === "user_request" &&
@@ -171,19 +171,13 @@ function resolvePerTurnDeliveryDirective(params: {
171171
return undefined;
172172
}
173173

174+
// The current event itself is the user turn body; the context block carries
175+
// only the marker, the room backlog, and the reply-policy directive so no
176+
// fact is stated twice in one request.
174177
function buildRoomEventContext(params: ReplyPromptEnvelopeBaseParams, roomContext: string): string {
175-
const roomEventBody = resolveRoomEventTranscriptBody(params);
176178
const roomContextBlock = roomContext.trim() ? `Room context:\n${roomContext.trim()}` : "";
177179
const deliveryDirective = resolvePerTurnDeliveryDirective(params);
178-
return [
179-
"[OpenClaw room event]",
180-
"inbound_event_kind: room_event",
181-
roomContextBlock,
182-
`Current event:\n${roomEventBody}`,
183-
deliveryDirective,
184-
]
185-
.filter(Boolean)
186-
.join("\n\n");
180+
return [ROOM_EVENT_PROMPT, roomContextBlock, deliveryDirective].filter(Boolean).join("\n\n");
187181
}
188182

189183
function buildResumableRoomContext(roomContext: string): string {
@@ -203,17 +197,12 @@ export function buildReplyPromptEnvelopeBase(
203197
const softResetTail = params.softResetTail?.trim() ?? "";
204198
const isRoomEvent = params.inboundEventKind === "room_event";
205199
const inboundUserContext = params.inboundUserContext.trim();
206-
const roomEventContext = buildRoomEventContext(params, inboundUserContext);
207200
const resumableRoomEventContext = isRoomEvent
208201
? buildRoomEventContext(params, buildResumableRoomContext(inboundUserContext))
209202
: undefined;
210-
const userRequestDeliveryDirective = resolvePerTurnDeliveryDirective({
211-
inboundEventKind: params.inboundEventKind,
212-
sourceReplyDeliveryMode: params.sourceReplyDeliveryMode,
213-
});
214203
const currentInboundContextText = isRoomEvent
215-
? roomEventContext
216-
: [inboundUserContext, userRequestDeliveryDirective].filter(Boolean).join("\n\n");
204+
? buildRoomEventContext(params, inboundUserContext)
205+
: [inboundUserContext, resolvePerTurnDeliveryDirective(params)].filter(Boolean).join("\n\n");
217206
const resetModelBody = params.isBareSessionReset
218207
? [
219208
params.inboundUserContext,
@@ -226,22 +215,17 @@ export function buildReplyPromptEnvelopeBase(
226215
.filter(Boolean)
227216
.join("\n\n")
228217
: params.baseBody;
229-
const effectiveBaseBody = isRoomEvent
230-
? ROOM_EVENT_PROMPT
231-
: params.hasUserBody
232-
? resetModelBody
233-
: MEDIA_ONLY_USER_TEXT;
234-
// Room-event transcript rows are plain chat lines; replay treats them as
235-
// conversation, while the OpenClaw marker remains current-turn context only.
218+
// Room-event turns and their transcript rows share one attributed chat line
219+
// so the active turn replays byte-identically as history; the room-event
220+
// marker and directive stay current-turn context only.
221+
const roomEventBody = isRoomEvent ? resolveRoomEventTranscriptBody(params) : undefined;
222+
const effectiveBaseBody =
223+
roomEventBody ?? (params.hasUserBody ? resetModelBody : MEDIA_ONLY_USER_TEXT);
236224
const transcriptBody = params.isHeartbeat
237225
? HEARTBEAT_TRANSCRIPT_PROMPT
238226
: params.isBareSessionReset
239227
? softResetTail || `[OpenClaw session ${params.startupAction}]`
240-
: isRoomEvent
241-
? resolveRoomEventTranscriptBody(params)
242-
: params.hasUserBody
243-
? params.baseBody
244-
: MEDIA_ONLY_USER_TEXT;
228+
: (roomEventBody ?? (params.hasUserBody ? params.baseBody : MEDIA_ONLY_USER_TEXT));
245229
const currentInboundContext: CurrentInboundPromptContext | undefined =
246230
!params.isBareSessionReset && currentInboundContextText
247231
? {

0 commit comments

Comments
 (0)