mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 16:01:36 +00:00
fix(subagent): harden read-tool overflow guards and sticky reply threading (#19508)
* fix(gateway): avoid premature agent.wait completion on transient errors * fix(agent): preemptively guard tool results against context overflow * fix: harden tool-result context guard and add message_id metadata * fix: use importOriginal in session-key mock to include DEFAULT_ACCOUNT_ID The run.skill-filter test was mocking ../../routing/session-key.js with only buildAgentMainSessionKey and normalizeAgentId, but the module also exports DEFAULT_ACCOUNT_ID which is required transitively by src/web/auth-store.ts. Switch to importOriginal pattern so all real exports are preserved alongside the mocked functions. * pi-runner: guard accumulated tool-result overflow in transformContext * PI runner: compact overflowing tool-result context * Subagent: harden tool-result context recovery * Enhance tool-result context handling by adding support for legacy tool outputs and improving character estimation for message truncation. This includes a new function to create legacy tool results and updates to existing functions to better manage context overflow scenarios. * Enhance iMessage handling by adding reply tag support in send functions and tests. This includes modifications to prepend or rewrite reply tags based on provided replyToId, ensuring proper message formatting for replies. * Enhance message delivery across multiple channels by implementing sticky reply context for chunked messages. This includes preserving reply references in Discord, Telegram, and iMessage, ensuring that follow-up messages maintain their intended reply targets. Additionally, improve handling of reply tags in system prompts and tests to support consistent reply behavior. * Enhance read tool functionality by implementing auto-paging across chunks when no explicit limit is provided, scaling output budget based on model context window. Additionally, add tests for adaptive reading behavior and capped continuation guidance for large outputs. Update related functions to support these features. * Refine tool-result context management by stripping oversized read-tool details payloads during compaction, ensuring repeated read calls do not bypass context limits. Introduce new utility functions for handling truncation content and enhance character estimation for tool results. Add tests to validate the removal of excessive details in context overflow scenarios. * Refine message delivery logic in Matrix and Telegram by introducing a flag to track if a text chunk was sent. This ensures that replies are only marked as delivered when a text chunk has been successfully sent, improving the accuracy of reply handling in both channels. * fix: tighten reply threading coverage and prep fixes (#19508) (thanks @tyler6204)
This commit is contained in:
@@ -146,6 +146,16 @@ describe("buildInboundUserContextPrefix", () => {
|
||||
expect(conversationInfo["sender"]).toBe("+15551234567");
|
||||
});
|
||||
|
||||
it("includes message_id in conversation info", () => {
|
||||
const text = buildInboundUserContextPrefix({
|
||||
ChatType: "direct",
|
||||
MessageSid: " msg-123 ",
|
||||
} as TemplateContext);
|
||||
|
||||
const conversationInfo = parseConversationInfoPayload(text);
|
||||
expect(conversationInfo["message_id"]).toBe("msg-123");
|
||||
});
|
||||
|
||||
it("falls back to SenderId when sender phone is missing", () => {
|
||||
const text = buildInboundUserContextPrefix({
|
||||
ChatType: "direct",
|
||||
|
||||
@@ -61,6 +61,7 @@ export function buildInboundUserContextPrefix(ctx: TemplateContext): string {
|
||||
const isDirect = !chatType || chatType === "direct";
|
||||
|
||||
const conversationInfo = {
|
||||
message_id: safeTrim(ctx.MessageSid),
|
||||
conversation_label: isDirect ? undefined : safeTrim(ctx.ConversationLabel),
|
||||
sender: safeTrim(ctx.SenderE164) ?? safeTrim(ctx.SenderId) ?? safeTrim(ctx.SenderUsername),
|
||||
group_subject: safeTrim(ctx.GroupSubject),
|
||||
|
||||
@@ -644,6 +644,34 @@ describe("createStreamingDirectiveAccumulator", () => {
|
||||
expect(result?.replyToId).toBe("abc-123");
|
||||
expect(result?.replyToTag).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps explicit reply ids sticky across subsequent renderable chunks", () => {
|
||||
const accumulator = createStreamingDirectiveAccumulator();
|
||||
|
||||
expect(accumulator.consume("[[reply_to: abc-123]]")).toBeNull();
|
||||
|
||||
const first = accumulator.consume("test 1");
|
||||
expect(first?.replyToId).toBe("abc-123");
|
||||
expect(first?.replyToTag).toBe(true);
|
||||
|
||||
const second = accumulator.consume("test 2");
|
||||
expect(second?.replyToId).toBe("abc-123");
|
||||
expect(second?.replyToTag).toBe(true);
|
||||
});
|
||||
|
||||
it("clears sticky reply context on reset", () => {
|
||||
const accumulator = createStreamingDirectiveAccumulator();
|
||||
|
||||
expect(accumulator.consume("[[reply_to_current]]")).toBeNull();
|
||||
expect(accumulator.consume("first")?.replyToCurrent).toBe(true);
|
||||
|
||||
accumulator.reset();
|
||||
|
||||
const afterReset = accumulator.consume("second");
|
||||
expect(afterReset?.replyToCurrent).toBe(false);
|
||||
expect(afterReset?.replyToTag).toBe(false);
|
||||
expect(afterReset?.replyToId).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveResponsePrefixTemplate", () => {
|
||||
|
||||
@@ -74,10 +74,12 @@ const hasRenderableContent = (parsed: ReplyDirectiveParseResult): boolean =>
|
||||
export function createStreamingDirectiveAccumulator() {
|
||||
let pendingTail = "";
|
||||
let pendingReply: PendingReplyState = { sawCurrent: false, hasTag: false };
|
||||
let activeReply: PendingReplyState = { sawCurrent: false, hasTag: false };
|
||||
|
||||
const reset = () => {
|
||||
pendingTail = "";
|
||||
pendingReply = { sawCurrent: false, hasTag: false };
|
||||
activeReply = { sawCurrent: false, hasTag: false };
|
||||
};
|
||||
|
||||
const consume = (raw: string, options: ConsumeOptions = {}): ReplyDirectiveParseResult | null => {
|
||||
@@ -95,9 +97,10 @@ export function createStreamingDirectiveAccumulator() {
|
||||
}
|
||||
|
||||
const parsed = parseChunk(combined, { silentToken: options.silentToken });
|
||||
const hasTag = pendingReply.hasTag || parsed.replyToTag;
|
||||
const sawCurrent = pendingReply.sawCurrent || parsed.replyToCurrent;
|
||||
const explicitId = parsed.replyToExplicitId ?? pendingReply.explicitId;
|
||||
const hasTag = activeReply.hasTag || pendingReply.hasTag || parsed.replyToTag;
|
||||
const sawCurrent = activeReply.sawCurrent || pendingReply.sawCurrent || parsed.replyToCurrent;
|
||||
const explicitId =
|
||||
parsed.replyToExplicitId ?? pendingReply.explicitId ?? activeReply.explicitId;
|
||||
|
||||
const combinedResult: ReplyDirectiveParseResult = {
|
||||
...parsed,
|
||||
@@ -117,6 +120,13 @@ export function createStreamingDirectiveAccumulator() {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Keep reply context sticky for the full assistant message so split/newline chunks
|
||||
// stay on the same native reply target until reset() is called for the next message.
|
||||
activeReply = {
|
||||
explicitId,
|
||||
sawCurrent,
|
||||
hasTag,
|
||||
};
|
||||
pendingReply = { sawCurrent: false, hasTag: false };
|
||||
return combinedResult;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user