Skip to content

Commit 4731e3b

Browse files
cliffhallclaude
andcommitted
fix(sequentialthinking): no "undefined" in log headers, border sized from visible text
The thought log on stderr printed "revising thought undefined" for a revision with no revisesThought and "ID: undefined" for a branch with no branchId, and sized its border from the coloured header, so chalk's escape codes made the box wider than its text. A revision with no number now reads "Revision N/M" with no suffix, a branch with no id reads "(from thought N)", and the header is measured before its label is coloured. The modelcontextprotocol#5000 pinning tests now assert this behavior. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit 4731e3b

2 files changed

Lines changed: 42 additions & 29 deletions

File tree

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// are driven through the in-process client and stderr is captured by spying
55
// on console.error, which also keeps the boxes off the test output. chalk's
66
// colour level depends on the terminal the tests run in, so it is pinned to 0
7-
// (no colour) around each test; the last test turns colour on to pin how the
8-
// border width is computed from the coloured header.
7+
// (no colour) around each test; the last test turns colour on to check that
8+
// the border width ignores the header's escape codes.
99

1010
import {
1111
describe,
@@ -98,12 +98,11 @@ describe("sequentialthinking thought logging", () => {
9898
]);
9999
});
100100

101-
// KNOWN BUG #5000: the header reads "revising thought undefined" when revisesThought is unset; the fix changes this assertion.
102-
it("prints 'undefined' for a revision with no revisesThought", async () => {
101+
// #5000: with no revisesThought, the "revising thought N" suffix is left out.
102+
it("draws a plain revision header when no revisesThought is given", async () => {
103103
await conn!.think(thought({ thought: "x", isRevision: true }));
104-
expect(logged()).toEqual([
105-
box("🔄 Revision 1/3 (revising thought undefined)", "x"),
106-
]);
104+
expect(logged()).toEqual([box("🔄 Revision 1/3", "x")]);
105+
expect(logged()[0]).not.toContain("undefined");
107106
});
108107

109108
it("draws a branch, naming its origin and id", async () => {
@@ -115,12 +114,11 @@ describe("sequentialthinking thought logging", () => {
115114
]);
116115
});
117116

118-
// KNOWN BUG #5000: a branchFromThought with no branchId draws a Branch header reading "ID: undefined"; the fix changes this assertion.
119-
it("draws a branch header even when no branch is recorded (no branchId)", async () => {
117+
// #5000: with no branchId, the Branch header names the origin and omits the ID.
118+
it("omits the ID from a branch header when no branchId is given", async () => {
120119
await conn!.think(thought({ thought: "fork", branchFromThought: 2 }));
121-
expect(logged()).toEqual([
122-
box("🌿 Branch 1/3 (from thought 2, ID: undefined)", "fork"),
123-
]);
120+
expect(logged()).toEqual([box("🌿 Branch 1/3 (from thought 2)", "fork")]);
121+
expect(logged()[0]).not.toContain("undefined");
124122
});
125123

126124
it("prefers the revision header when a thought is both", async () => {
@@ -166,17 +164,16 @@ describe("sequentialthinking thought logging", () => {
166164
});
167165
});
168166

169-
// KNOWN BUG #5000: the border counts the header's colour escape codes, so the box is drawn wider than its text; the fix changes this assertion.
170-
// The border is sized from header.length, and the header carries chalk's
171-
// escape codes, so when colour is on the box is wider than its visible text.
172-
it("sizes the border from the coloured header, escape codes included", async () => {
167+
// #5000: when colour is on, the header carries chalk's escape codes, but the
168+
// border is sized from the visible text, so the box matches the uncoloured one.
169+
it("sizes the border from the visible header, not its escape codes", async () => {
173170
chalk.level = 1;
174171
conn = await connect({ disableThoughtLogging: undefined });
175172
await conn.think(thought({ thought: "t" }));
176173
const raw = String(errorSpy.mock.calls[0][0]);
174+
const visible = "💭 Thought 1/3";
177175
const coloured = `${chalk.blue("💭 Thought")} 1/3`;
178-
expect(coloured.length).toBeGreaterThan("💭 Thought 1/3".length);
179-
expect(raw).toContain(coloured);
180-
expect(raw).toContain(`┌${"─".repeat(coloured.length + 4)}┐`);
176+
expect(coloured.length).toBeGreaterThan(visible.length);
177+
expect(raw).toBe(box(visible, "t").replace(visible, coloured));
181178
});
182179
});

‎src/sequentialthinking/lib.ts‎

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,38 @@ export class SequentialThinkingServer {
3333
branchId,
3434
} = thoughtData;
3535

36-
let prefix: string;
37-
let context: string;
36+
// The label is coloured only after the header is measured, so the border
37+
// is sized from the visible text, not from chalk's escape codes. A missing
38+
// revisesThought or branchId is left out of the header rather than printed
39+
// as "undefined".
40+
let label: string;
41+
let colour: (text: string) => string;
42+
let context = "";
3843

3944
if (isRevision) {
40-
prefix = chalk.yellow("🔄 Revision");
41-
context = ` (revising thought ${revisesThought})`;
45+
label = "🔄 Revision";
46+
colour = chalk.yellow;
47+
if (revisesThought !== undefined) {
48+
context = ` (revising thought ${revisesThought})`;
49+
}
4250
} else if (branchFromThought) {
43-
prefix = chalk.green("🌿 Branch");
44-
context = ` (from thought ${branchFromThought}, ID: ${branchId})`;
51+
label = "🌿 Branch";
52+
colour = chalk.green;
53+
context =
54+
branchId !== undefined
55+
? ` (from thought ${branchFromThought}, ID: ${branchId})`
56+
: ` (from thought ${branchFromThought})`;
4557
} else {
46-
prefix = chalk.blue("💭 Thought");
47-
context = "";
58+
label = "💭 Thought";
59+
colour = chalk.blue;
4860
}
4961

50-
const header = `${prefix} ${thoughtNumber}/${totalThoughts}${context}`;
51-
const border = "─".repeat(Math.max(header.length, thought.length) + 4);
62+
const counts = ` ${thoughtNumber}/${totalThoughts}${context}`;
63+
const visibleHeader = `${label}${counts}`;
64+
const header = `${colour(label)}${counts}`;
65+
const border = "─".repeat(
66+
Math.max(visibleHeader.length, thought.length) + 4,
67+
);
5268

5369
return `
5470
┌${border}┐

0 commit comments

Comments
 (0)