Skip to content

Commit 09eb689

Browse files
authored
Merge pull request #5535 from modelcontextprotocol/v2/fix/5526-sequentialthinking-branchid-header-cap
fix(sequentialthinking): count header rows toward the log box's row cap
2 parents 1e0c7ba + 1fde546 commit 09eb689

4 files changed

Lines changed: 137 additions & 24 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-sequential-thinking": patch
3+
---
4+
5+
Count the thought box's header rows toward its 1000-row cap, so a long `branchId` can no longer draw past it on stderr: line breaks in a `branchId` are shown as `\r` and `\n`, and a header too long for the cap is cut with a row saying how many characters it left out (#5526).

‎src/sequentialthinking/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ On Windows, use `cmd /c` to launch `npx`:
134134

135135
To disable logging of thought information set env var: `DISABLE_THOUGHT_LOGGING` to `true`.
136136

137-
Each thought is logged to stderr as a box, one row per line of the thought. The box is at most 100 characters wide, with longer lines (and a long branch ID in the header) wrapped, and draws at most 1000 rows of thought text; past that it ends with a row saying how many lines it left out. Only the log is shortened: the server keeps the whole thought in its history, and the tool result is unchanged.
137+
Each thought is logged to stderr as a box, one row per line of the thought. The box is at most 100 characters wide, with longer lines (and a long branch ID in the header) wrapped, and draws at most 1000 rows of text, header and thought together; past that it ends with a row saying how many lines it left out. A line break in a branch ID is shown in the header as `\r` or `\n`, and a header too long for those rows (a very long branch ID) is cut, with a row saying how many characters it left out. Only the log is shortened: the server keeps the whole thought in its history, and the tool result is unchanged.
138138

139139
To bound memory use, the server retains at most `MAX_THOUGHT_HISTORY` thoughts per connection (a positive integer; default `1000`). Past the cap the oldest thought is dropped, from its branch too, so `thoughtHistoryLength` in the result stops at the cap and a branch whose thoughts have all been dropped is no longer listed in `branches`.
140140

‎src/sequentialthinking/__tests__/thought-logging.test.ts‎

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// "true" (any case), each accepted thought is drawn as a box on stderr, with
33
// a header that differs for plain thoughts, revisions and branches, and one
44
// row per line of the thought, wrapped at MAX_BOX_TEXT_WIDTH and cut off
5-
// after MAX_BOX_ROWS. Thoughts
5+
// once header and thought fill MAX_BOX_ROWS rows. Thoughts
66
// are driven through the in-process client and stderr is captured by spying
77
// on console.error, which also keeps the boxes off the test output. chalk's
88
// colour level depends on the terminal the tests run in, so it is pinned to 0
@@ -232,15 +232,18 @@ ${rows.join("\n")}
232232
});
233233

234234
// #5433: past MAX_BOX_ROWS the box stops drawing and says how many lines
235-
// it left out; the call itself is unaffected.
235+
// it left out; the call itself is unaffected. The header's one row counts
236+
// toward the cap (#5526), so the thought gets the rest.
236237
it("draws a thought with very many lines", async () => {
237238
const lines = Array.from({ length: 200_000 }, (_, i) => `${i % 10}`);
238239
const result = await conn!.think(thought({ thought: lines.join("\n") }));
239240
expect(result.isError).toBeFalsy();
240241
const out = logged()[0].split("\n");
241-
expect(out).toHaveLength(MAX_BOX_ROWS + 1 + 5);
242+
// A leading blank, the borders and separator, MAX_BOX_ROWS rows of
243+
// header and thought, and the summary row.
244+
expect(out).toHaveLength(MAX_BOX_ROWS + 5);
242245
expect(out.at(-2)).toContain(
243-
`… ${200_000 - MAX_BOX_ROWS} more lines not shown`,
246+
`… ${200_000 - (MAX_BOX_ROWS - 1)} more lines not shown`,
244247
);
245248
// The summary is wider than every one-character line and the header,
246249
// so it sets the box's width; it must not stick out of it.
@@ -284,28 +287,33 @@ ${row("short")}
284287
const result = await conn!.think(thought({ thought: text }));
285288
expect(result.isError).toBeFalsy();
286289
const out = logged()[0];
287-
// 200 wrapped rows for the long line, short lines up to the row cap,
288-
// then the summary row; each row is W + 6 characters and a newline.
289-
const shortShown = MAX_BOX_ROWS - n / W;
290-
expect(out.split("\n")).toHaveLength(MAX_BOX_ROWS + 1 + 5);
290+
// The header row, 200 wrapped rows for the long line, short lines up to
291+
// the row cap, then the summary row; each row is W + 6 characters and a
292+
// newline.
293+
const shortShown = MAX_BOX_ROWS - 1 - n / W;
294+
expect(out.split("\n")).toHaveLength(MAX_BOX_ROWS + 5);
291295
expect(out).toContain(`… ${n - 1 - shortShown} more lines not shown`);
292296
expect(out.length).toBeLessThan((MAX_BOX_ROWS + 10) * (W + 7));
293297
});
294298

295299
it("counts a line the row cap cuts off as not shown", async () => {
296-
const text = `${"s\n".repeat(MAX_BOX_ROWS - 1)}${"L".repeat(2 * W)}`;
300+
// The header row and these short lines leave one row for the long one.
301+
const text = `${"s\n".repeat(MAX_BOX_ROWS - 2)}${"L".repeat(2 * W)}`;
297302
await conn!.think(thought({ thought: text }));
298303
const rows = logged()[0].split("\n");
299-
expect(rows).toHaveLength(MAX_BOX_ROWS + 1 + 5);
304+
expect(rows).toHaveLength(MAX_BOX_ROWS + 5);
300305
expect(rows.at(-3)).toBe(row("L".repeat(W)));
301306
expect(rows.at(-2)).toBe(row("… 1 more line not shown"));
302307
});
303308

304309
it("draws a thought that exactly fills the cap with no summary row", async () => {
305-
const text = Array.from({ length: MAX_BOX_ROWS }, () => "s").join("\n");
310+
// With the header's one row, these lines fill the cap exactly.
311+
const text = Array.from({ length: MAX_BOX_ROWS - 1 }, () => "s").join(
312+
"\n",
313+
);
306314
await conn!.think(thought({ thought: text }));
307315
const rows = logged()[0].split("\n");
308-
expect(rows).toHaveLength(MAX_BOX_ROWS + 5);
316+
expect(rows).toHaveLength(MAX_BOX_ROWS + 4);
309317
expect(logged()[0]).not.toContain("not shown");
310318
});
311319

@@ -338,6 +346,81 @@ ${row("short")}
338346
expect(out.length).toBeLessThan((lines + 10) * (W + 7));
339347
});
340348

349+
// #5526: a long branchId's header rows count toward MAX_BOX_ROWS, so they
350+
// cannot add rows past the cap the thought's rows are held to.
351+
it("counts a wrapped header's rows toward the row cap", async () => {
352+
const branchId = "b".repeat(2 * W);
353+
const header = `🌿 Branch 1/3 (from thought 1, ID: ${branchId})`;
354+
const headerRows = Math.ceil(header.length / W);
355+
expect(headerRows).toBeGreaterThan(1);
356+
await conn!.think(
357+
thought({
358+
thought: Array.from({ length: MAX_BOX_ROWS }, () => "s").join("\n"),
359+
branchFromThought: 1,
360+
branchId,
361+
}),
362+
);
363+
const rows = logged()[0].split("\n");
364+
expect(rows).toHaveLength(MAX_BOX_ROWS + 5);
365+
expect(rows.at(-2)).toBe(row(`… ${headerRows} more lines not shown`));
366+
});
367+
368+
// #5526: a line ending in a branchId would start a line of output the
369+
// row cap never counted; the header shows it escaped instead.
370+
it("escapes line endings in a branchId so the header stays on its rows", async () => {
371+
const branchId = "x\r\ny\rz\n".repeat(500);
372+
const escaped = "x\\r\\ny\\rz\\n".repeat(500);
373+
const header = `🌿 Branch 1/3 (from thought 1, ID: ${escaped})`;
374+
await conn!.think(
375+
thought({ thought: "t", branchFromThought: 1, branchId }),
376+
);
377+
const out = logged()[0];
378+
expect(out).not.toContain("\r");
379+
const rows = out.split("\n");
380+
const headerRows = Math.ceil(header.length / W);
381+
// A leading blank, the borders and separator, the header rows and the
382+
// thought's one row.
383+
expect(rows).toHaveLength(headerRows + 5);
384+
expect(
385+
rows
386+
.slice(2, 2 + headerRows)
387+
.map((r) => r.slice(2, 2 + W).trimEnd())
388+
.join(""),
389+
).toBe(header);
390+
});
391+
392+
// #5526: a header too long for every row is cut, so a huge branchId logs
393+
// no more than the cap; its last row says how much was left out.
394+
it("cuts a header longer than the row cap", async () => {
395+
const branchId = "b".repeat(MAX_BOX_ROWS * W);
396+
const header = `🌿 Branch 1/3 (from thought 1, ID: ${branchId})`;
397+
const result = await conn!.think(
398+
thought({ thought: "t", branchFromThought: 1, branchId }),
399+
);
400+
expect(result.isError).toBeFalsy();
401+
const out = logged()[0];
402+
const rows = out.split("\n");
403+
// A leading blank, the top border, MAX_BOX_ROWS header rows, the
404+
// separator, the thought's summary row and the bottom border.
405+
expect(rows).toHaveLength(MAX_BOX_ROWS + 5);
406+
const shown = (MAX_BOX_ROWS - 1) * W;
407+
expect(
408+
rows
409+
.slice(2, MAX_BOX_ROWS + 1)
410+
.map((r) => r.slice(2, 2 + W))
411+
.join(""),
412+
).toBe(header.slice(0, shown));
413+
expect(rows[MAX_BOX_ROWS + 1]).toBe(
414+
row(`… ${header.length - shown} more characters not shown`),
415+
);
416+
expect(rows[MAX_BOX_ROWS + 2]).toBe(`├${border}┤`);
417+
expect(rows.at(-2)).toBe(row("… 1 more line not shown"));
418+
expect(new Set(rows.slice(1).map((r) => r.length))).toEqual(
419+
new Set([W + 6]),
420+
);
421+
expect(out.length).toBeLessThan((MAX_BOX_ROWS + 10) * (W + 7));
422+
});
423+
341424
it("never splits an emoji across rows", async () => {
342425
// The emoji's two UTF-16 halves would straddle the cut at W.
343426
const text = `${"a".repeat(W - 1)}😀tail`;

‎src/sequentialthinking/lib.ts‎

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ export interface ThoughtData {
2222
export const MAX_BOX_TEXT_WIDTH = 100;
2323

2424
/**
25-
* The most rows of thought text a box draws (#5433). Each row is padded to
26-
* the box's width, so a thought of many short lines would still log about a
27-
* hundred times its own size; past this many rows the box ends with one row
28-
* saying how many lines it left out. Header rows (a long branchId) are not
29-
* counted. Only stderr is cut: the thought kept in thoughtHistory is whole,
30-
* and the tool result, which carries no thought text, is unchanged.
25+
* The most rows of text a box draws, header and thought together (#5433,
26+
* #5526). Each row is padded to the box's width, so a thought of many short
27+
* lines would still log about a hundred times its own size; past this many
28+
* rows the box ends with one row saying how many lines it left out. The
29+
* header's rows count too, so a long branchId, which wraps onto rows of its
30+
* own, cannot draw past the cap: a header too long for every row is cut, its
31+
* last row saying how many characters it left out. Only stderr is cut: the
32+
* thought kept in thoughtHistory is whole, and the tool result, which carries
33+
* no thought text, is unchanged.
3134
*/
3235
export const MAX_BOX_ROWS = 1000;
3336

@@ -130,9 +133,14 @@ export class SequentialThinkingServer {
130133
} else if (branchFromThought) {
131134
label = "🌿 Branch";
132135
colour = chalk.green;
136+
// #5526: a line ending in the branchId is shown as its escape, \r or
137+
// \n, so the header stays on rows the box draws and MAX_BOX_ROWS counts.
133138
context =
134139
branchId !== undefined
135-
? ` (from thought ${branchFromThought}, ID: ${branchId})`
140+
? ` (from thought ${branchFromThought}, ID: ${branchId.replace(
141+
/[\r\n]/g,
142+
(eol) => (eol === "\r" ? "\\r" : "\\n"),
143+
)})`
136144
: ` (from thought ${branchFromThought})`;
137145
} else {
138146
label = "💭 Thought";
@@ -158,15 +166,32 @@ export class SequentialThinkingServer {
158166
);
159167
// A header longer than the cap (a long branchId) wraps like a thought
160168
// line; only its first row, which starts with the label, is coloured.
161-
const headerTexts = wrapRow(visibleHeader, wrapWidth);
162-
// Rows are drawn up to MAX_BOX_ROWS; the lines left over are counted, not
163-
// drawn, so a huge thought costs no more than the cap.
169+
// #5526: its rows count toward MAX_BOX_ROWS. Only one character more than
170+
// the cap's rows can hold is wrapped, so a huge branchId costs no more
171+
// than the cap, and more than MAX_BOX_ROWS rows means the header does not
172+
// fit. It then keeps MAX_BOX_ROWS - 1 rows and a last one saying how many
173+
// characters it left out: always more than one row's worth. Such a header
174+
// is longer than the box is wide, so wrapWidth is MAX_BOX_TEXT_WIDTH and
175+
// that row fits it.
176+
const headerTexts = wrapRow(
177+
visibleHeader.slice(0, MAX_BOX_ROWS * wrapWidth + 1),
178+
wrapWidth,
179+
);
180+
if (headerTexts.length > MAX_BOX_ROWS) {
181+
headerTexts.length = MAX_BOX_ROWS - 1;
182+
const shown = headerTexts.reduce((n, text) => n + text.length, 0);
183+
headerTexts.push(
184+
`… ${visibleHeader.length - shown} more characters not shown`,
185+
);
186+
}
187+
// Thought rows fill what the header left of MAX_BOX_ROWS; the lines left
188+
// over are counted, not drawn, so a huge thought costs no more than the cap.
164189
const texts: string[] = [];
165190
// Lines drawn in full; a line cut off by the cap counts as hidden.
166191
let whole = 0;
167192
for (const line of lines) {
168193
const wrapped = wrapRow(line, wrapWidth);
169-
const room = MAX_BOX_ROWS - texts.length;
194+
const room = MAX_BOX_ROWS - headerTexts.length - texts.length;
170195
texts.push(...wrapped.slice(0, room));
171196
if (wrapped.length > room) break;
172197
whole++;

0 commit comments

Comments
 (0)