Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mcps): keep UTF-16 surrogate pairs intact across readResult pages

Zhao Dengfeng4b373d98a3b1
diff --git a/packages/lizi-mcps/src/browser/browserMcpServer.test.ts b/packages/lizi-mcps/src/browser/browserMcpServer.test.ts
index 07ceafa6e7..c6e0bfbb28 100644
--- a/packages/lizi-mcps/src/browser/browserMcpServer.test.ts
+++ b/packages/lizi-mcps/src/browser/browserMcpServer.test.ts
@@ -335,6 +335,28 @@ describe('browser experience cache', () => {
} finally { await h.cleanup(); }
});
+ it('never splits a UTF-16 surrogate pair across readResult pages, even at maxChars 1', async () => {
+ const original = { ok: true, action: 'act' as const, data: { targetId: 'tab', result: '😀'.repeat(9_000) } };
+ const h = await makeHarness({ getRuntime: () => ({ call: async () => original }) });
+ const loneSurrogate = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/;
+ try {
+ const id = (await invoke(h, { action: 'act', targetId: 'tab', request: { kind: 'evaluate', fn: '() => []' } })).fullResult.id;
+ // 奇数页长让边界落在代理对中间;每一页都必须是合法 Unicode,拼接后逐字节还原。
+ let offset: number | null = 0;
+ let text = '';
+ while (offset !== null) {
+ const chunk = await invoke(h, { action: 'readResult', resultId: id, offset, maxChars: 12_001 });
+ expect(chunk.text).not.toMatch(loneSurrogate);
+ text += chunk.text; offset = chunk.nextOffset;
+ }
+ expect(JSON.parse(text)).toEqual(original);
+ // maxChars=1 落在代理对上时整对返回并前进,不会卡在原地。
+ const emoji = JSON.stringify(original).indexOf('😀');
+ expect(await invoke(h, { action: 'readResult', resultId: id, offset: emoji, maxChars: 1 }))
+ .toMatchObject({ text: '😀', nextOffset: emoji + 2 });
+ } finally { await h.cleanup(); }
+ });
+
it('evicts old originals and preserves success with advisory warnings or failed experience storage', async () => {
let big = true;
const h = await makeHarness({
diff --git a/packages/lizi-mcps/src/browser/result-cache.ts b/packages/lizi-mcps/src/browser/result-cache.ts
index 3e42b2e608..64afb78950 100644
--- a/packages/lizi-mcps/src/browser/result-cache.ts
+++ b/packages/lizi-mcps/src/browser/result-cache.ts
@@ -64,7 +64,10 @@ export class BrowserResultCache {
message: 'offset 超出原文长度',
chars: text.length,
};
- const end = Math.min(text.length, offset + maxChars);
+ let end = Math.min(text.length, offset + maxChars);
+ // 不把 UTF-16 代理对切到两页:退回到代理对之前;页内只剩高位代理时整对带上,保证分页前进。
+ if (end < text.length && isHighSurrogate(text.charCodeAt(end - 1)) && isLowSurrogate(text.charCodeAt(end)))
+ end += end - 1 > offset ? -1 : 1;
return {
ok: true,
action: 'readResult',
@@ -77,6 +80,14 @@ export class BrowserResultCache {
}
}
+function isHighSurrogate(code: number): boolean {
+ return code >= 0xd800 && code <= 0xdbff;
+}
+
+function isLowSurrogate(code: number): boolean {
+ return code >= 0xdc00 && code <= 0xdfff;
+}
+
/** Deterministic structural samples; counts make omissions explicit. */
function sample(value: unknown, depth: number, budget: { remaining: number }): unknown {
if (budget.remaining <= 0) return '[omitted]';