|
2 | 2 | // count, the branch list, revisions, totalThoughts auto-adjustment and the |
3 | 3 | // state kept per server instance. Every case drives the tool through the |
4 | 4 | // 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. |
7 | 8 |
|
8 | | -import { describe, it, expect, beforeEach, afterEach } from "vitest"; |
| 9 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
9 | 10 | import { |
10 | 11 | connect, |
11 | 12 | parseOk, |
@@ -277,69 +278,91 @@ describe("sequentialthinking thought processing", () => { |
277 | 278 | }); |
278 | 279 | }); |
279 | 280 |
|
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. |
286 | 284 | describe("#4813: branchId colliding with an Object.prototype key", () => { |
287 | | - // KNOWN BUG #4813: pins current (wrong) behavior; the fix changes this assertion. |
288 | 285 | it.each(["constructor", "__proto__", "toString", "hasOwnProperty"])( |
289 | | - "fails the call for branchId %j", |
| 286 | + "creates and lists the branch for branchId %j", |
290 | 287 | 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, |
309 | 298 | }); |
310 | 299 | }, |
311 | 300 | ); |
312 | 301 |
|
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 () => { |
315 | 303 | 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 | + ), |
322 | 312 | ); |
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 | | - }); |
328 | 313 | expect( |
329 | 314 | parseOk( |
330 | 315 | await conn.think( |
331 | | - thought({ thoughtNumber: 3, nextThoughtNeeded: false }), |
| 316 | + thought({ |
| 317 | + thoughtNumber: 3, |
| 318 | + nextThoughtNeeded: false, |
| 319 | + branchFromThought: 1, |
| 320 | + branchId: "constructor", |
| 321 | + }), |
332 | 322 | ), |
333 | 323 | ), |
334 | 324 | ).toEqual({ |
335 | 325 | thoughtNumber: 3, |
336 | 326 | totalThoughts: 3, |
337 | 327 | nextThoughtNeeded: false, |
338 | | - branches: ["alt"], |
| 328 | + branches: ["alt", "constructor"], |
339 | 329 | thoughtHistoryLength: 3, |
340 | 330 | }); |
341 | 331 | }); |
342 | 332 |
|
| 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 | + |
343 | 366 | it("is harmless when branchFromThought is absent, since no branch is recorded", async () => { |
344 | 367 | expect( |
345 | 368 | parseOk(await conn.think(thought({ branchId: "constructor" }))) |
|
0 commit comments