Skip to content

fix(opencode): collapse system prompt fragments into a single system message - #2

Merged
cyrilmhansen merged 1 commit into
devfrom
fix/system-prompt-normalization
Mar 26, 2026
Merged

cyrilmhansen merged 1 commit into
devfrom
fix/system-prompt-normalization

Conversation

@cyrilmhansen

Copy link
Copy Markdown
Owner

Branch:

fix/system-prompt-normalization

Title:

fix(opencode): collapse system prompt fragments into a single system message

Body:

## Summary

This normalizes the final system prompt into a single system message before provider request construction.

## Problem

`LLM.stream()` builds a `system: string[]` from:
- provider/agent prompt
- `input.system`
- `user.system`
- plugin mutations via `experimental.chat.system.transform`

That array was then mapped to multiple separate `role: "system"` messages for most providers.

Some strict backends/templates are sensitive to this representation, and multiple system messages can cause prompt
handling failures.

## Fix

Collapse the final `system` fragments into a single newline-joined system string before building provider messages.

This keeps all system content while avoiding multiple system-message entries in the final request.

## Why this is safe

- No system content is removed
- Only the representation changes
- Plugin modifications still apply before normalization
- Reduces provider/template incompatibility for strict backends

## Tests

Added unit tests for:
- collapsing multiple fragments into one system prompt
- dropping empty fragments
- returning `[]` when all fragments are empty

## Verification

Validated locally with:
- `bun typecheck`
- `bun test test/session/llm-system.test.ts`

Related to:
- anomalyco/opencode#15059

@cyrilmhansen

Copy link
Copy Markdown
Owner Author

diagnostic, implementation, test, review by Codex/gpt-5.4

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