|
12 | 12 | * nativeArgs — both are asserted here. |
13 | 13 | */ |
14 | 14 | import { NativeToolCallParser } from "../NativeToolCallParser" |
15 | | -import { looksLikeTextToolCall } from "../TextToolCallParser" |
| 15 | +import { looksLikeTextToolCall, stripMalformedToolCallMarkup } from "../TextToolCallParser" |
16 | 16 | import { |
17 | 17 | applyTextualToolCallRecovery, |
18 | 18 | hasExecutableNativeToolUse, |
@@ -265,4 +265,163 @@ describe("MiMo field-failure scenarios (regression)", () => { |
265 | 265 | expect(tool.nativeArgs.doc).toBe("design") |
266 | 266 | }) |
267 | 267 | }) |
| 268 | + |
| 269 | + describe("Scenario (d): malformed/garbled markup fragments — must never leak to UI", () => { |
| 270 | + /** |
| 271 | + * Field report: model emitted a garbled fragment with no valid function |
| 272 | + * opener, just stray tokens like `=</parameter>` + `</function></tool_call>`. |
| 273 | + * The recovery parser correctly can't extract a valid tool call, but the |
| 274 | + * raw broken XML was displayed to the user. The fix: strip structural |
| 275 | + * tag-shaped tokens even when recovery fails, so the user only sees |
| 276 | + * clean prose. |
| 277 | + */ |
| 278 | + |
| 279 | + it("exact field-report fragment: prose + stray =</parameter> + </function></tool_call>", () => { |
| 280 | + const fragment = |
| 281 | + "All 8 passed. Let me verify the remaining tools and clean up:<tool_call>\n=</parameter>\n</function>\n</tool_call>" |
| 282 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 283 | + expect(stripped).toBe("All 8 passed. Let me verify the remaining tools and clean up:") |
| 284 | + expect(stripped).not.toContain("<tool_call>") |
| 285 | + expect(stripped).not.toContain("</parameter>") |
| 286 | + expect(stripped).not.toContain("</function>") |
| 287 | + expect(stripped).not.toContain("</tool_call>") |
| 288 | + }) |
| 289 | + |
| 290 | + it("applyTextualToolCallRecovery strips malformed fragment when recovery fails", () => { |
| 291 | + const fragment = |
| 292 | + "All 8 passed. Let me verify the remaining tools and clean up:<tool_call>\n=</parameter>\n</function>\n</tool_call>" |
| 293 | + |
| 294 | + const recovered = applyTextualToolCallRecovery({ |
| 295 | + assistantMessage: fragment, |
| 296 | + assistantMessageContent: [{ type: "text", content: fragment, partial: true }] as any[], |
| 297 | + currentStreamingContentIndex: 0, |
| 298 | + }) |
| 299 | + |
| 300 | + // Recovery fails (no valid tools) but markup is stripped |
| 301 | + expect(recovered.recoveredCount).toBe(0) |
| 302 | + expect(recovered.applied).toBe(true) // applied=true because message changed |
| 303 | + expect(recovered.assistantMessage).toBe("All 8 passed. Let me verify the remaining tools and clean up:") |
| 304 | + expect(recovered.assistantMessage).not.toContain("<tool_call>") |
| 305 | + expect(recovered.assistantMessage).not.toContain("</parameter>") |
| 306 | + }) |
| 307 | + |
| 308 | + it("unclosed <tool_call> at end of text", () => { |
| 309 | + const fragment = "Done. Now let me run the tests.<tool_call>" |
| 310 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 311 | + expect(stripped).toBe("Done. Now let me run the tests.") |
| 312 | + }) |
| 313 | + |
| 314 | + it("orphaned </tool_call> closer", () => { |
| 315 | + const fragment = "Finished processing.</tool_call>" |
| 316 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 317 | + expect(stripped).toBe("Finished processing.") |
| 318 | + }) |
| 319 | + |
| 320 | + it("unclosed <function=write_spec> opener", () => { |
| 321 | + const fragment = "Let me write the spec.<function=write_spec>" |
| 322 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 323 | + expect(stripped).toBe("Let me write the spec.") |
| 324 | + }) |
| 325 | + |
| 326 | + it("orphaned </function> closer", () => { |
| 327 | + const fragment = "All done here.</function>" |
| 328 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 329 | + expect(stripped).toBe("All done here.") |
| 330 | + }) |
| 331 | + |
| 332 | + it("unclosed <parameter=doc> opener", () => { |
| 333 | + const fragment = "Setting the doc parameter.<parameter=doc>" |
| 334 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 335 | + expect(stripped).toBe("Setting the doc parameter.") |
| 336 | + }) |
| 337 | + |
| 338 | + it("stray =</parameter> closer", () => { |
| 339 | + const fragment = "Value set.=</parameter>" |
| 340 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 341 | + expect(stripped).toBe("Value set.") |
| 342 | + }) |
| 343 | + |
| 344 | + it("orphaned </parameter> closer", () => { |
| 345 | + const fragment = "Done with params.</parameter>" |
| 346 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 347 | + expect(stripped).toBe("Done with params.") |
| 348 | + }) |
| 349 | + |
| 350 | + it("pure markup fragment (no prose) → empty string", () => { |
| 351 | + const fragment = "<tool_call>\n=</parameter>\n</function>\n</tool_call>" |
| 352 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 353 | + expect(stripped).toBe("") |
| 354 | + }) |
| 355 | + |
| 356 | + it("multiple malformed fragments in one text", () => { |
| 357 | + const fragment = |
| 358 | + "First step done.<tool_call>\n=</parameter>\n</function>\n</tool_call>\nSecond step complete.</tool_call>" |
| 359 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 360 | + expect(stripped).toContain("First step done.") |
| 361 | + expect(stripped).toContain("Second step complete.") |
| 362 | + expect(stripped).not.toContain("<tool_call>") |
| 363 | + expect(stripped).not.toContain("</tool_call>") |
| 364 | + expect(stripped).not.toContain("</function>") |
| 365 | + expect(stripped).not.toContain("</parameter>") |
| 366 | + }) |
| 367 | + |
| 368 | + it("normal prose with technical words is NEVER falsely stripped", () => { |
| 369 | + const prose = |
| 370 | + "The function parameter is set to null. Please call the tool_call function with the correct parameter." |
| 371 | + const stripped = stripMalformedToolCallMarkup(prose) |
| 372 | + expect(stripped).toBe(prose) |
| 373 | + }) |
| 374 | + |
| 375 | + it("prose with angle brackets in code is preserved", () => { |
| 376 | + const prose = "Use `if (a < b && c > d)` for the comparison." |
| 377 | + const stripped = stripMalformedToolCallMarkup(prose) |
| 378 | + expect(stripped).toBe(prose) |
| 379 | + }) |
| 380 | + |
| 381 | + it("prose with HTML-like tags that are NOT tool-call structural is preserved", () => { |
| 382 | + const prose = 'Use the <div> element with class="container" for layout.' |
| 383 | + const stripped = stripMalformedToolCallMarkup(prose) |
| 384 | + expect(stripped).toBe(prose) |
| 385 | + }) |
| 386 | + |
| 387 | + it("well-formed tool call is NOT stripped (recovery handles it, not this path)", () => { |
| 388 | + const wellFormed = |
| 389 | + "Let me read the spec.\n<tool_call>\n<function=read_spec>\n<parameter=doc>design</parameter>\n</function>\n</tool_call>" |
| 390 | + // stripMalformedToolCallMarkup strips it (it's structural markup), but |
| 391 | + // the recovery path should handle it first. This test just documents |
| 392 | + // that the strip function is structural, not semantic. |
| 393 | + const stripped = stripMalformedToolCallMarkup(wellFormed) |
| 394 | + expect(stripped).toBe("Let me read the spec.") |
| 395 | + }) |
| 396 | + |
| 397 | + it("applyTextualToolCallRecovery with well-formed tool call still recovers (not just strips)", () => { |
| 398 | + const wellFormed = |
| 399 | + "Let me read the spec.\n<tool_call>\n<function=read_spec>\n<parameter=doc>design</parameter>\n</function>\n</tool_call>" |
| 400 | + |
| 401 | + const recovered = applyTextualToolCallRecovery({ |
| 402 | + assistantMessage: wellFormed, |
| 403 | + assistantMessageContent: [{ type: "text", content: wellFormed, partial: true }] as any[], |
| 404 | + currentStreamingContentIndex: 0, |
| 405 | + }) |
| 406 | + |
| 407 | + // Recovery succeeds — tool is extracted AND markup is stripped |
| 408 | + expect(recovered.recoveredCount).toBe(1) |
| 409 | + expect(recovered.applied).toBe(true) |
| 410 | + expect(recovered.assistantMessage).toBe("Let me read the spec.") |
| 411 | + const toolBlocks = recovered.assistantMessageContent.filter((b) => b.type === "tool_use") |
| 412 | + expect(toolBlocks).toHaveLength(1) |
| 413 | + expect((toolBlocks[0] as any).name).toBe("read_spec") |
| 414 | + }) |
| 415 | + |
| 416 | + it("empty input returns empty", () => { |
| 417 | + expect(stripMalformedToolCallMarkup("")).toBe("") |
| 418 | + expect(stripMalformedToolCallMarkup(" ")).toBe("") |
| 419 | + }) |
| 420 | + |
| 421 | + it("text with only whitespace between fragments collapses correctly", () => { |
| 422 | + const fragment = "Start.<tool_call>\n\n\n</tool_call>\n\nEnd." |
| 423 | + const stripped = stripMalformedToolCallMarkup(fragment) |
| 424 | + expect(stripped).toBe("Start.\n\nEnd.") |
| 425 | + }) |
| 426 | + }) |
268 | 427 | }) |
0 commit comments