Skip to content

Commit 63c1d4d

Browse files
cliffhallclaude
andcommitted
fix(sequentialthinking): write the log box before recording the thought
Copilot round 1 on #5036: console.error ran after thoughtHistory and branches were updated, inside the same try, so a failing write still returned isError with the thought recorded. Draw and write the box first, then record the thought. The state-unchanged test now covers a failure in both drawing and writing the box. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent b5c5ccc commit 63c1d4d

2 files changed

Lines changed: 61 additions & 41 deletions

File tree

‎src/sequentialthinking/__tests__/thinking.test.ts‎

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -330,38 +330,60 @@ describe("sequentialthinking thought processing", () => {
330330
});
331331
});
332332

333-
it("leaves the history and branches unchanged when a call fails", async () => {
334-
const logging = await connect({ disableThoughtLogging: undefined });
335-
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
336-
try {
337-
await logging.think(thought({ branchFromThought: 1, branchId: "alt" }));
338-
// Drawing the box is the work that can still throw; make it fail once.
339-
vi.spyOn(String.prototype, "padEnd").mockImplementationOnce(() => {
340-
throw new Error("boom");
341-
});
342-
const failed = await logging.think(
343-
thought({ thoughtNumber: 2, branchFromThought: 1, branchId: "new" }),
344-
);
345-
expect(failed.isError).toBe(true);
346-
expect(JSON.parse(textOf(failed))).toEqual({
347-
error: "boom",
348-
status: "failed",
349-
});
350-
expect(
351-
parseOk(await logging.think(thought({ thoughtNumber: 3 }))),
352-
).toEqual({
353-
thoughtNumber: 3,
354-
totalThoughts: 3,
355-
nextThoughtNeeded: true,
356-
branches: ["alt"],
357-
thoughtHistoryLength: 2,
358-
});
359-
} finally {
360-
vi.restoreAllMocks();
361-
errorSpy.mockRestore();
362-
await logging.close();
363-
}
364-
});
333+
// Drawing the box and writing it to stderr are the steps that can still
334+
// throw; make each fail once and check nothing was recorded.
335+
it.each([
336+
[
337+
"drawing",
338+
() =>
339+
vi.spyOn(String.prototype, "padEnd").mockImplementationOnce(() => {
340+
throw new Error("boom");
341+
}),
342+
],
343+
[
344+
"writing",
345+
() =>
346+
vi.spyOn(console, "error").mockImplementationOnce(() => {
347+
throw new Error("boom");
348+
}),
349+
],
350+
])(
351+
"leaves the history and branches unchanged when %s the log box fails",
352+
async (_step, fail) => {
353+
const logging = await connect({ disableThoughtLogging: undefined });
354+
vi.spyOn(console, "error").mockImplementation(() => {});
355+
try {
356+
await logging.think(
357+
thought({ branchFromThought: 1, branchId: "alt" }),
358+
);
359+
fail();
360+
const failed = await logging.think(
361+
thought({
362+
thoughtNumber: 2,
363+
branchFromThought: 1,
364+
branchId: "new",
365+
}),
366+
);
367+
expect(failed.isError).toBe(true);
368+
expect(JSON.parse(textOf(failed))).toEqual({
369+
error: "boom",
370+
status: "failed",
371+
});
372+
expect(
373+
parseOk(await logging.think(thought({ thoughtNumber: 3 }))),
374+
).toEqual({
375+
thoughtNumber: 3,
376+
totalThoughts: 3,
377+
nextThoughtNeeded: true,
378+
branches: ["alt"],
379+
thoughtHistoryLength: 2,
380+
});
381+
} finally {
382+
vi.restoreAllMocks();
383+
await logging.close();
384+
}
385+
},
386+
);
365387

366388
it("is harmless when branchFromThought is absent, since no branch is recorded", async () => {
367389
expect(

‎src/sequentialthinking/lib.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,13 @@ export class SequentialThinkingServer {
7171
input.totalThoughts = input.thoughtNumber;
7272
}
7373

74-
// Do the work that can throw before touching any state, so a failed
75-
// call leaves thoughtHistory and branches as they were.
76-
const formattedThought = this.disableThoughtLogging
77-
? undefined
78-
: this.formatThought(input);
74+
// Log before touching any state: drawing and writing the box are the
75+
// steps that can throw, so a failed call leaves thoughtHistory and
76+
// branches as they were.
77+
if (!this.disableThoughtLogging) {
78+
const formattedThought = this.formatThought(input);
79+
console.error(formattedThought);
80+
}
7981

8082
this.thoughtHistory.push(input);
8183

@@ -86,10 +88,6 @@ export class SequentialThinkingServer {
8688
this.branches[input.branchId].push(input);
8789
}
8890

89-
if (formattedThought !== undefined) {
90-
console.error(formattedThought);
91-
}
92-
9391
return {
9492
content: [
9593
{

0 commit comments

Comments
 (0)