Skip to content

Commit a5c60fa

Browse files
cliffhallclaude
andcommitted
fix(sequentialthinking): count header rows toward the log box's row cap
A long branchId wraps onto header rows that MAX_BOX_ROWS did not count, so it could draw past the cap #5433 set for the thought's rows. The header's rows now count toward the cap, and a header too long for it is cut, its last row saying how many characters it left out. Closes #5526 Signed-off-by: cliffhall <cliff@futurescale.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a9558c5 commit a5c60fa

4 files changed

Lines changed: 107 additions & 23 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, and cut a header too long for the cap 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 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: 71 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,57 @@ ${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 header too long for every row is cut, so a huge branchId logs
369+
// no more than the cap; its last row says how much was left out.
370+
it("cuts a header longer than the row cap", async () => {
371+
const branchId = "b".repeat(MAX_BOX_ROWS * W);
372+
const header = `🌿 Branch 1/3 (from thought 1, ID: ${branchId})`;
373+
const result = await conn!.think(
374+
thought({ thought: "t", branchFromThought: 1, branchId }),
375+
);
376+
expect(result.isError).toBeFalsy();
377+
const out = logged()[0];
378+
const rows = out.split("\n");
379+
// A leading blank, the top border, MAX_BOX_ROWS header rows, the
380+
// separator, the thought's summary row and the bottom border.
381+
expect(rows).toHaveLength(MAX_BOX_ROWS + 5);
382+
const shown = (MAX_BOX_ROWS - 1) * W;
383+
expect(
384+
rows
385+
.slice(2, MAX_BOX_ROWS + 1)
386+
.map((r) => r.slice(2, 2 + W))
387+
.join(""),
388+
).toBe(header.slice(0, shown));
389+
expect(rows[MAX_BOX_ROWS + 1]).toBe(
390+
row(`… ${header.length - shown} more characters not shown`),
391+
);
392+
expect(rows[MAX_BOX_ROWS + 2]).toBe(`├${border}┤`);
393+
expect(rows.at(-2)).toBe(row("… 1 more line not shown"));
394+
expect(new Set(rows.slice(1).map((r) => r.length))).toEqual(
395+
new Set([W + 6]),
396+
);
397+
expect(out.length).toBeLessThan((MAX_BOX_ROWS + 10) * (W + 7));
398+
});
399+
341400
it("never splits an emoji across rows", async () => {
342401
// The emoji's two UTF-16 halves would straddle the cut at W.
343402
const text = `${"a".repeat(W - 1)}😀tail`;

‎src/sequentialthinking/lib.ts‎

Lines changed: 30 additions & 10 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

@@ -158,15 +161,32 @@ export class SequentialThinkingServer {
158161
);
159162
// A header longer than the cap (a long branchId) wraps like a thought
160163
// 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.
164+
// #5526: its rows count toward MAX_BOX_ROWS. Only one character more than
165+
// the cap's rows can hold is wrapped, so a huge branchId costs no more
166+
// than the cap, and more than MAX_BOX_ROWS rows means the header does not
167+
// fit. It then keeps MAX_BOX_ROWS - 1 rows and a last one saying how many
168+
// characters it left out: always more than one row's worth. Such a header
169+
// is longer than the box is wide, so wrapWidth is MAX_BOX_TEXT_WIDTH and
170+
// that row fits it.
171+
const headerTexts = wrapRow(
172+
visibleHeader.slice(0, MAX_BOX_ROWS * wrapWidth + 1),
173+
wrapWidth,
174+
);
175+
if (headerTexts.length > MAX_BOX_ROWS) {
176+
headerTexts.length = MAX_BOX_ROWS - 1;
177+
const shown = headerTexts.reduce((n, text) => n + text.length, 0);
178+
headerTexts.push(
179+
`… ${visibleHeader.length - shown} more characters not shown`,
180+
);
181+
}
182+
// Thought rows fill what the header left of MAX_BOX_ROWS; the lines left
183+
// over are counted, not drawn, so a huge thought costs no more than the cap.
164184
const texts: string[] = [];
165185
// Lines drawn in full; a line cut off by the cap counts as hidden.
166186
let whole = 0;
167187
for (const line of lines) {
168188
const wrapped = wrapRow(line, wrapWidth);
169-
const room = MAX_BOX_ROWS - texts.length;
189+
const room = MAX_BOX_ROWS - headerTexts.length - texts.length;
170190
texts.push(...wrapped.slice(0, room));
171191
if (wrapped.length > room) break;
172192
whole++;

0 commit comments

Comments
 (0)