Skip to content

fix(persona): place injected mid-conversation system before an assistant turn (#978) - #1116

Closed
dofastted wants to merge 1 commit into
mainfrom
fix/mid-system-position
Closed

dofastted wants to merge 1 commit into
mainfrom
fix/mid-system-position

Conversation

@dofastted

@dofastted dofastted commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

Fixes #978

问题

insertMidConversationSystem 和 persona 的 Environment 注入都把非空 role: system 插在首条 user 之后。首条 user 后面紧跟 directive-only system(content: [] + output_config)或另一条 user 时,非空 system 落在上游不接受的位置:role 'system' must precede an 'assistant' message or end the array。

修复

插入点从锚定的 user 往后找,放到下一条 assistant 之前;后面没有 assistant 就放到末尾。常见的 user → assistant 形状位置不变,缓存前缀不受影响。

验证


Note

Medium Risk
Changes message ordering for persona Environment and mid-conversation system injection on outbound API requests; incorrect placement could still cause 400s or cache-prefix shifts, but behavior is narrowed to comply with upstream rules and is regression-tested.

Overview
Non-empty role: system messages were inserted immediately after the first user turn, which breaks when the next message is another user or a directive-only system row (content: [] + output_config). Upstream requires non-empty system turns to sit right before an assistant or at the end of the array (#978).

This PR adds systemInsertAt, which anchors on the target user index then advances to the index before the next assistant (or messages.length). insertMidConversationSystem and official template Environment injection in applyTemplatePersona both use it instead of a fixed “after user” slot. The usual user → assistant layout is unchanged for cache stability.

Tests cover directive-adjacent layouts, the classic shape, and tail append when no assistant follows.

Reviewed by Cursor Bugbot for commit aca8c6a. Configure here.

…ant turn (#978)

The Environment system was spliced right after the first user. When that
user was followed by a directive-only system row or another user, the
non-empty system ended up in a position upstream rejects
("role 'system' must precede an 'assistant' message or end the array").
Slide the insert point forward to just before the next assistant, or the
end of the array. The common user -> assistant shape is unchanged.
@cursor

cursor Bot commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: 095174ab-f2a9-4fc2-b912-8610e8e0a02b)

@dofastted

Copy link
Copy Markdown
Owner Author

感谢贡献。替代修复 #1162 已合入 main(1881d65f01)。面板 Environment 移到 assistant 边界;相邻已有正文 system 时合并内容,避免原候选仍可能产生连续两个非空 system。 此 PR 不再重复合并。

@dofastted dofastted closed this Nov 16, 2026
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.

[Bug] OpenCode 多轮续聊失败:出站新增 Environment system 与控制轮相邻,触发消息位置校验(Opus 5.5)

1 participant