Skip to content

Commit 686f644

Browse files
committed
fix(agents): report surrogate-safe omitted count
1 parent e8f2506 commit 686f644

2 files changed

Lines changed: 5 additions & 22 deletions

File tree

src/agents/embedded-agent-runner/tool-result-context-guard.test.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,6 @@ function recordMockArg(
179179
return arg as Record<string, unknown>;
180180
}
181181

182-
function hasLoneSurrogate(s: string): boolean {
183-
for (let i = 0; i < s.length; i += 1) {
184-
const c = s.charCodeAt(i);
185-
if (c >= 0xd800 && c <= 0xdbff) {
186-
if (i + 1 >= s.length || s.charCodeAt(i + 1) < 0xdc00 || s.charCodeAt(i + 1) > 0xdfff) {
187-
return true;
188-
}
189-
}
190-
if (c >= 0xdc00 && c <= 0xdfff) {
191-
if (i === 0 || s.charCodeAt(i - 1) < 0xd800 || s.charCodeAt(i - 1) > 0xdbff) {
192-
return true;
193-
}
194-
}
195-
}
196-
return false;
197-
}
198-
199182
describe("formatContextLimitTruncationNotice", () => {
200183
it("formats truncation wording with a count", () => {
201184
expect(formatContextLimitTruncationNotice(123)).toBe(
@@ -358,9 +341,9 @@ describe("installToolResultContextGuard", () => {
358341
1_000,
359342
)) as AgentMessage[];
360343

361-
const truncated = getToolResultText(transformed[0]);
362-
expectOpenClawTruncation(truncated);
363-
expect(hasLoneSurrogate(truncated)).toBe(false);
344+
expect(getToolResultText(transformed[0])).toBe(
345+
"a".repeat(439) + formatContextLimitTruncationNotice(1_002),
346+
);
364347
expect(getToolResultText(contextForNextCall[0])).toBe(text);
365348
});
366349

src/agents/embedded-agent-runner/tool-result-context-guard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ function truncateTextToBudget(text: string, maxChars: number): string {
165165
cutPoint = newline;
166166
}
167167

168-
const omittedChars = text.length - cutPoint;
169-
return truncateUtf16Safe(text, cutPoint) + formatContextLimitTruncationNotice(omittedChars);
168+
const prefix = truncateUtf16Safe(text, cutPoint);
169+
return prefix + formatContextLimitTruncationNotice(text.length - prefix.length);
170170
}
171171

172172
function replaceToolResultText(msg: AgentMessage, text: string): AgentMessage {

0 commit comments

Comments
 (0)