diff --git a/src/plugin-render.ts b/src/plugin-render.ts index 636aca2043..703207d39f 100644 --- a/src/plugin-render.ts +++ b/src/plugin-render.ts @@ -99,15 +99,17 @@ export async function handlePluginRender(req: http.IncomingMessage, res: http.Se const coveredSeqs = new Set(); const anchors: { pos: number; msg: BiliMessage }[] = []; for (const block of activeBlocks) { - const blockSeqs: number[] = []; + let maxCover: number | undefined; for (const id of block.effectiveMessageIds) { const seq = idToSeq.get(id); - if (seq !== undefined) blockSeqs.push(seq); + if (seq !== undefined) { + coveredSeqs.add(seq); + maxCover = maxCover === undefined ? seq : Math.max(maxCover, seq); + } } - if (blockSeqs.length === 0) continue; - for (const s of blockSeqs) coveredSeqs.add(s); - const maxCover = Math.max(...blockSeqs); - const pos = viewRows.findIndex((r) => r.seq > maxCover); + if (maxCover === undefined) continue; + const lastCoveredSeq = maxCover; + const pos = viewRows.findIndex((r) => r.seq > lastCoveredSeq); // #4732: pos === -1 means the fold reaches the END of the window — the // covered rows are suppressed below, so the anchor MUST still land // (at the tail) or the rendered prefix silently drops the whole fold diff --git a/tests/plugin-render-endpoint.test.ts b/tests/plugin-render-endpoint.test.ts index 7c3d696ecf..345803d0f7 100644 --- a/tests/plugin-render-endpoint.test.ts +++ b/tests/plugin-render-endpoint.test.ts @@ -17,7 +17,8 @@ import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _resetPluginStateForTest } from "../src/plugin.ts"; -import { _resetSessionsForTest } from "../src/session.ts"; +import { getSession, _resetSessionsForTest } from "../src/session.ts"; +import type { MessageRow } from "../src/storage/store.ts"; import { _setForTest } from "../src/registry.ts"; import { resetToolRingForTest } from "../src/tool-ring.ts"; import { __resetIncrementalLaneForTests } from "../src/incremental-lane.js"; @@ -263,3 +264,35 @@ test("S5 guard rails: poison msgids hard-fail; count mismatch degrades silently" await h.close(); } }); + +test("render handles a fold covering 150000 rows without a function-argument limit", async (t) => { + const h = await harness(true); + try { + const count = 150_000; + const conv = "large-render"; + let rows: MessageRow[] = Array.from({ length: count + 1 }, (_, i) => ({ + seq: i * 2, msgId: `raw-${i}`, ref: null, refNum: null, role: "user", kind: "message", + content: JSON.stringify({ id: `raw-${i}`, role: "user", contentType: "text", text: i === count ? "uncovered tail" : `covered ${i}` }), + })); + t.mock.method(h.store, "unifiedTailViewRows", () => rows); + t.mock.method(h.store, "unifiedTailSeqs", () => rows.map((row) => row.seq)); + const session = getSession(conv); + (session.state.blocks as unknown[]).push({ + blockId: "large", active: true, + effectiveMessageIds: [...rows.slice(0, count).map((row) => row.msgId).reverse(), "outside-view"], + }); + const rendered = await h.render({ conversationId: conv, untilSeq: count + 1 }); + assert.equal(rendered.status, 200, JSON.stringify(rendered.body)); + const output = rendered.body as { appliedSeq: number; messages: { content: { type: string; text?: string }[] }[] }; + assert.equal(output.appliedSeq, count + 1); + assert.deepEqual(output.messages.flatMap((message) => message.content.map((part) => part.text)), ["", "uncovered tail"]); + + rows = [{ ...rows[count]!, seq: 0 }]; + const unresolved = await h.render({ conversationId: conv, untilSeq: 1 }); + assert.equal(unresolved.status, 200); + const unchanged = unresolved.body as { messages: { content: { text?: string }[] }[] }; + assert.deepEqual(unchanged.messages.flatMap((message) => message.content.map((part) => part.text)), ["uncovered tail"]); + } finally { + await h.close(); + } +});