Skip to content

Commit b5c5ccc

Browse files
cliffhallBlueX888claude
committed
fix(sequentialthinking): accept any branchId and leave state unchanged on failure
`branches` was a plain object literal, so the lazy-init guard in processThought read an inherited Object.prototype member for ids such as "constructor", "__proto__", "toString" or "hasOwnProperty", skipped the `= []` and then threw on `.push`. The thought had already been appended to thoughtHistory, so the failed call still advanced thoughtHistoryLength. Give `branches` a null prototype, so every string id is an ordinary own key. Object.keys() still lists ordinary ids in the same order. Draw the log box, the work that can still throw, before touching any state, so a failed call leaves thoughtHistory and branches as they were. The #4813 pinning tests now assert the correct behavior and lose their KNOWN BUG markers; a new test injects a failure while drawing the box and checks the history and branch list are unchanged. Ports the null-prototype fix from #4814 by @BlueX888. Closes #4813 Co-authored-by: BlueX888 <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 571e2b3 commit b5c5ccc

4 files changed

Lines changed: 87 additions & 51 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+
A `branchId` that names an `Object.prototype` key, such as `"constructor"` or `"__proto__"`, now creates and lists its branch like any other id instead of failing the call. A call that fails no longer adds its thought to the history first, so `thoughtHistoryLength` counts only thoughts that were accepted.

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

Lines changed: 67 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
// count, the branch list, revisions, totalThoughts auto-adjustment and the
33
// state kept per server instance. Every case drives the tool through the
44
// in-process client and reads only the result's text and structuredContent.
5-
// Includes the #4813 characterization (a branchId that names an
6-
// Object.prototype key); PR #4814 is the fix, so that test changes with it.
5+
// Includes the #4813 regression tests: a branchId that names an
6+
// Object.prototype key is an ordinary branch, and a failed call leaves the
7+
// state unchanged.
78

8-
import { describe, it, expect, beforeEach, afterEach } from "vitest";
9+
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
910
import {
1011
connect,
1112
parseOk,
@@ -277,69 +278,91 @@ describe("sequentialthinking thought processing", () => {
277278
});
278279
});
279280

280-
// #4813: branches is a plain object, so a branchId that names an inherited
281-
// Object.prototype member reads back that member (truthy, not an array) and
282-
// `.push` throws. The call fails with the TypeError's message as an
283-
// isError result, after the thought was already appended to the history, so
284-
// the failed call still advances thoughtHistoryLength. PR #4814 fixes this;
285-
// these assertions change with it.
281+
// #4813: a branchId is an opaque string, so one that names an
282+
// Object.prototype member is tracked like any other id, and a call that
283+
// fails leaves thoughtHistory and branches as they were.
286284
describe("#4813: branchId colliding with an Object.prototype key", () => {
287-
// KNOWN BUG #4813: pins current (wrong) behavior; the fix changes this assertion.
288285
it.each(["constructor", "__proto__", "toString", "hasOwnProperty"])(
289-
"fails the call for branchId %j",
286+
"creates and lists the branch for branchId %j",
290287
async (branchId) => {
291-
const result = await conn.think(
292-
thought({ branchFromThought: 1, branchId }),
293-
);
294-
expect(result).toEqual({
295-
content: [
296-
{
297-
type: "text",
298-
text: JSON.stringify(
299-
{
300-
error: "this.branches[input.branchId].push is not a function",
301-
status: "failed",
302-
},
303-
null,
304-
2,
305-
),
306-
},
307-
],
308-
isError: true,
288+
expect(
289+
parseOk(
290+
await conn.think(thought({ branchFromThought: 1, branchId })),
291+
),
292+
).toEqual({
293+
thoughtNumber: 1,
294+
totalThoughts: 3,
295+
nextThoughtNeeded: true,
296+
branches: [branchId],
297+
thoughtHistoryLength: 1,
309298
});
310299
},
311300
);
312301

313-
// KNOWN BUG #4813: pins current (wrong) behavior; the fix changes this assertion.
314-
it("still counts the failed thought in the history and does not list the branch", async () => {
302+
it("counts the thought once and lists the branch alongside ordinary ones", async () => {
315303
await conn.think(thought({ branchFromThought: 1, branchId: "alt" }));
316-
const failed = await conn.think(
317-
thought({
318-
thoughtNumber: 2,
319-
branchFromThought: 1,
320-
branchId: "constructor",
321-
}),
304+
parseOk(
305+
await conn.think(
306+
thought({
307+
thoughtNumber: 2,
308+
branchFromThought: 1,
309+
branchId: "constructor",
310+
}),
311+
),
322312
);
323-
expect(failed.isError).toBe(true);
324-
expect(JSON.parse(textOf(failed))).toEqual({
325-
error: "this.branches[input.branchId].push is not a function",
326-
status: "failed",
327-
});
328313
expect(
329314
parseOk(
330315
await conn.think(
331-
thought({ thoughtNumber: 3, nextThoughtNeeded: false }),
316+
thought({
317+
thoughtNumber: 3,
318+
nextThoughtNeeded: false,
319+
branchFromThought: 1,
320+
branchId: "constructor",
321+
}),
332322
),
333323
),
334324
).toEqual({
335325
thoughtNumber: 3,
336326
totalThoughts: 3,
337327
nextThoughtNeeded: false,
338-
branches: ["alt"],
328+
branches: ["alt", "constructor"],
339329
thoughtHistoryLength: 3,
340330
});
341331
});
342332

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+
});
365+
343366
it("is harmless when branchFromThought is absent, since no branch is recorded", async () => {
344367
expect(
345368
parseOk(await conn.think(thought({ branchId: "constructor" })))

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,14 @@ describe("sequentialthinking thought logging", () => {
156156
expect(logged()).toEqual([]);
157157
});
158158

159-
// KNOWN BUG #4813: pins current (wrong) behavior; the fix changes this assertion.
160-
// #4813: the push throws before the box is drawn.
161-
it("logs nothing for a #4813 branch-id collision", async () => {
159+
// #4813: a branchId that names an Object.prototype key is an ordinary branch.
160+
it("draws the Branch box for a branchId that names an Object.prototype key", async () => {
162161
await conn!.think(
163162
thought({ branchFromThought: 1, branchId: "constructor" }),
164163
);
165-
expect(logged()).toEqual([]);
164+
expect(logged()).toEqual([
165+
box("🌿 Branch 1/3 (from thought 1, ID: constructor)", "a thought"),
166+
]);
166167
});
167168
});
168169

‎src/sequentialthinking/lib.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export interface ThoughtData {
1414

1515
export class SequentialThinkingServer {
1616
private thoughtHistory: ThoughtData[] = [];
17-
private branches: Record<string, ThoughtData[]> = {};
17+
// Null prototype, so any string branchId (even "constructor" or "__proto__")
18+
// is an ordinary own key rather than an inherited Object.prototype member.
19+
private branches: Record<string, ThoughtData[]> = Object.create(null);
1820
private disableThoughtLogging: boolean;
1921

2022
constructor() {
@@ -69,6 +71,12 @@ export class SequentialThinkingServer {
6971
input.totalThoughts = input.thoughtNumber;
7072
}
7173

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);
79+
7280
this.thoughtHistory.push(input);
7381

7482
if (input.branchFromThought && input.branchId) {
@@ -78,8 +86,7 @@ export class SequentialThinkingServer {
7886
this.branches[input.branchId].push(input);
7987
}
8088

81-
if (!this.disableThoughtLogging) {
82-
const formattedThought = this.formatThought(input);
89+
if (formattedThought !== undefined) {
8390
console.error(formattedThought);
8491
}
8592

0 commit comments

Comments
 (0)