Skip to content

Commit 9a1cdf3

Browse files
cliffhallclaude
andcommitted
fix(sequentialthinking): draw a multi-line thought one row per line
formatThought padded the whole thought as one string, so a thought with line breaks drew its earlier lines outside the frame, put the right border after the last line only, and sized the box from the whole string. Each line (split on \r\n, \n or a lone \r) is now a padded row of its own, and the box is as wide as the longest line or the header. Closes #5205 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 446be48 commit 9a1cdf3

3 files changed

Lines changed: 85 additions & 9 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+
Draw a multi-line thought one row per line in the stderr thought box, sized to the longest line, instead of as one broken row (#5205)

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

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// The server's other observable output: unless DISABLE_THOUGHT_LOGGING is
22
// "true" (any case), each accepted thought is drawn as a box on stderr, with
3-
// a header that differs for plain thoughts, revisions and branches. Thoughts
3+
// a header that differs for plain thoughts, revisions and branches, and one
4+
// row per line of the thought. Thoughts
45
// are driven through the in-process client and stderr is captured by spying
56
// on console.error, which also keeps the boxes off the test output. chalk's
67
// colour level depends on the terminal the tests run in, so it is pinned to 0
@@ -41,14 +42,20 @@ describe("sequentialthinking thought logging", () => {
4142
return errorSpy.mock.calls.map((args) => String(args[0]));
4243
}
4344

44-
/** The box the server draws, for an uncoloured header. */
45-
function box(header: string, text: string): string {
46-
const border = "─".repeat(Math.max(header.length, text.length) + 4);
45+
/**
46+
* The box the server draws, for an uncoloured header: one row per line of
47+
* the thought (given as an array), sized to the longest row.
48+
*/
49+
function box(header: string, text: string | string[]): string {
50+
const lines = typeof text === "string" ? [text] : text;
51+
const width = Math.max(header.length, ...lines.map((l) => l.length));
52+
const border = "─".repeat(width + 4);
53+
const rows = lines.map((l) => `│ ${l.padEnd(border.length - 2)} │`);
4754
return `
4855
┌${border}┐
4956
│ ${header.padEnd(border.length - 2)} │
5057
├${border}┤
51-
│ ${text.padEnd(border.length - 2)} │
58+
${rows.join("\n")}
5259
└${border}┘`;
5360
}
5461

@@ -178,6 +185,58 @@ describe("sequentialthinking thought logging", () => {
178185
expect(new Set(rows.map((row) => row.length)).size).toBe(1);
179186
});
180187

188+
// #5205: a thought with line breaks is drawn one row per line, the box as
189+
// wide as its longest line, whichever line ending the client sent.
190+
describe("a multi-line thought", () => {
191+
beforeEach(async () => {
192+
conn = await connect({ disableThoughtLogging: undefined });
193+
});
194+
195+
it.each([
196+
["\\n", "\n"],
197+
["\\r\\n", "\r\n"],
198+
["a lone \\r", "\r"],
199+
])("draws one row per line split by %s", async (_l, eol) => {
200+
const lines = [
201+
"first line",
202+
"a much longer second line of the thought",
203+
"3",
204+
];
205+
await conn!.think(thought({ thought: lines.join(eol) }));
206+
expect(logged()).toEqual([box("💭 Thought 1/3", lines)]);
207+
expect(logged()[0]).not.toContain("\r");
208+
});
209+
210+
it("sizes the box from the longest line, not the whole thought", async () => {
211+
await conn!.think(thought({ thought: "ab\ncd\nef" }));
212+
// Shorter than the header, so the header sets the width.
213+
expect(logged()).toEqual([box("💭 Thought 1/3", ["ab", "cd", "ef"])]);
214+
});
215+
216+
it("keeps blank lines as blank rows", async () => {
217+
await conn!.think(thought({ thought: "top\n\nbottom\n" }));
218+
expect(logged()).toEqual([
219+
box("💭 Thought 1/3", ["top", "", "bottom", ""]),
220+
]);
221+
});
222+
223+
it("draws every row the same width", async () => {
224+
await conn!.think(
225+
thought({ thought: "short\na considerably longer line here\nmid" }),
226+
);
227+
const rows = logged()[0].split("\n").slice(1);
228+
expect(rows).toHaveLength(7);
229+
expect(new Set(rows.map((row) => row.length)).size).toBe(1);
230+
});
231+
232+
it("draws a thought with very many lines", async () => {
233+
const lines = Array.from({ length: 200_000 }, (_, i) => `${i % 10}`);
234+
const result = await conn!.think(thought({ thought: lines.join("\n") }));
235+
expect(result.isError).toBeFalsy();
236+
expect(logged()[0].split("\n")).toHaveLength(200_000 + 5);
237+
});
238+
});
239+
181240
// #5000: when colour is on, the header carries chalk's escape codes, but the
182241
// border is sized from the visible text, so the box matches the uncoloured one.
183242
it("sizes the border from the visible header, not its escape codes", async () => {

‎src/sequentialthinking/lib.ts‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,32 @@ export class SequentialThinkingServer {
6464

6565
const counts = ` ${thoughtNumber}/${totalThoughts}${context}`;
6666
const visibleHeader = `${label}${counts}`;
67-
const border = "─".repeat(
68-
Math.max(visibleHeader.length, thought.length) + 4,
67+
// #5205: each line of the thought is a row of its own, and the box is as
68+
// wide as the longest of them (or the header). Any line ending (\r\n, \n
69+
// or a lone \r) ends a row: a \r left inside a row would send the cursor
70+
// back to the left border and draw the rest of the row over it.
71+
const lines = thought.split(/\r\n|\r|\n/);
72+
// A reduce rather than Math.max(...lengths): spreading one argument per
73+
// line would overflow the call stack on a thought with very many lines.
74+
const width = lines.reduce(
75+
(widest, line) => Math.max(widest, line.length),
76+
visibleHeader.length,
6977
);
70-
// The header row is padded to the border's width like the thought row, by
78+
const border = "─".repeat(width + 4);
79+
// The header row is padded to the border's width like the thought rows, by
7180
// the visible text's length. Each label's emoji is one surrogate pair, so
7281
// its length of 2 is also its display width in a terminal.
7382
const padding = " ".repeat(border.length - 2 - visibleHeader.length);
7483
const header = `${colour(label)}${counts}${padding}`;
84+
const rows = lines
85+
.map((line) => `│ ${line.padEnd(border.length - 2)} │`)
86+
.join("\n");
7587

7688
return `
7789
┌${border}┐
7890
│ ${header} │
7991
├${border}┤
80-
│ ${thought.padEnd(border.length - 2)} │
92+
${rows}
8193
└${border}┘`;
8294
}
8395

0 commit comments

Comments
 (0)