Skip to content

fix(pi): strip trailing assistant messages, improve sanitize and error guards#32

Open
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:fix/pi-improvements
Open

fix(pi): strip trailing assistant messages, improve sanitize and error guards#32
iceteaSA wants to merge 1 commit into
cortexkit:mainfrom
iceteaSA:fix/pi-improvements

Conversation

@iceteaSA
Copy link
Copy Markdown
Contributor

@iceteaSA iceteaSA commented May 21, 2026

Improvements to pi/convert.ts beyond what was merged in #17:

  • Strip trailing assistant messages from converted requests to prevent Anthropic prefill 400 errors on some models
  • Unicode regex (/gu flag) for spec-compliant surrogate pair replacement
  • Simplified sanitizeToolId with cleaner length check (removes redundant guard)
  • Improved error content guard catches empty string case (content === '') that upstream misses
  • Comprehensive test coverage for all conversion logic (21 tests)

Summary by cubic

Prevents Anthropic prefill 400s by stripping trailing assistant messages in packages/pi conversions. Also improves sanitization and error handling, plus adds full tests and a test script.

  • Bug Fixes
    • Strip trailing assistant messages from converted requests to avoid prefill errors on some Anthropic models.
    • Use Unicode /gu flag in surrogate replacement for spec compliance.
    • Simplify sanitizeToolId with deterministic allowed chars and a clean length check.
    • Guard tool_result with is_error=true and empty content (including empty string) by inserting "Error".
    • Add test script (bun test src/tests) and 21 tests covering conversion logic.

Written for commit b659ab4. Summary will update on new commits. Review in cubic

…r guards

- Strip trailing assistant-role messages from converted requests to prevent
  Anthropic prefill 400 errors on some models
- Add Unicode flag to surrogate regex for spec compliance
- Simplify sanitizeToolId with cleaner length check
- Improve error content guard to catch empty string case
- Add comprehensive test coverage for all pi conversion logic
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 3 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread packages/pi/src/tests/convert.test.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant