Skip to content

fix(opencode): collapse plugin-pushed system messages into one - #38671

Closed
matchaxnb wants to merge 2 commits into
anomalyco:devfrom
matchaxnb:fix-system-collapse
Closed

fix(opencode): collapse plugin-pushed system messages into one#38671
matchaxnb wants to merge 2 commits into
anomalyco:devfrom
matchaxnb:fix-system-collapse

Conversation

@matchaxnb

Copy link
Copy Markdown

Issue for this PR

Closes#34321
This supersedes #34322 by @MADEVAL because some further fix was missing (collapse again in the messages)

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When a plugin uses experimental.chat.system.transform to push additional system instructions, the collapse guard system.length > 2 was one too high. The initial array has 1 element and a plugin that pushes 1 entry makes it 2, so 2 > 2 never fired. The result was multiple {role: "system"} messages, which OpenAI-compatible providers reject.

The fix changes the guard from > 2 to > 1. The system[0] === header check still protects against intentional header replacement by plugins.

How did you verify your code works?

Added a test in transform.test.ts that simulates a plugin pushing to system and verifies the prepared messages array has exactly one system message containing the extra instructions. Traced the full code path: system array construction, plugin trigger, collapse guard, and message construction.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Potential Duplicate Found

PR #34322 - fix(opencode): collapse plugin-pushed system messages into one
#34322

Relationship: This is the exact same fix that PR #38671 supersedes. According to the current PR's description, #38671 addresses the same issue as #34322 but with an additional correction (collapse guard fix that was missing in the previous attempt).

Since the current PR explicitly states it "supersedes #34322 by @MADEVAL," these are directly related—#34322 should likely be closed in favor of #38671.

@ondrejch

Copy link
Copy Markdown

Preparing a narrower replacement in #42801.

#38671 merges adjacent system messages for every provider. #42801 only coalesces them for @ai-sdk/openai-compatible, so Anthropic can keep separate system segments for prompt-cache boundaries.

@matchaxnb

Copy link
Copy Markdown
Author

Oka closing, cheers!

Sign up for freeto 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.

Multiple system messages break OpenAI-compatible providers when plugins use system.transform

3 participants

@matchaxnb@ondrejch@MADEVAL