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

Fixes Anthropic prefill 400s by stripping trailing assistant messages in packages/pi requests. Also tightens sanitization and error guards, and adds tests plus a bun test script.

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

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

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
…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
@iceteaSA iceteaSA force-pushed the fix/pi-improvements branch from b659ab4 to 768e897 Compare May 22, 2026 17:08
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