Repository navigation
Feat/issue 298/preserve context - #326
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
RunTurn can panic on nil inputs and can commit an assistant message even if the chat implementation returns a non-assistant role, potentially diverging session history from the returned response.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Implements a small orchestration boundary for multi-turn chat by introducing a turn-level helper that constructs outbound history and commits messages to the session only on success, aligning with Issue #298’s “preserve context across turns” and “no fabricated assistant responses on failure” requirements.
Changes:
- Replaces ad-hoc prompt/session mutation in the CLI path with
conversation.RunTurn. - Adds
conversation.ChatFuncandconversation.RunTurnto centralize turn orchestration and commit semantics. - Adds unit tests asserting correct outbound message ordering across two turns and ensuring failures/cancellation don’t mutate session history.
File summaries
| File | Description |
|---|---|
| main.go | Uses conversation.RunTurn and shared conversation.ChatFunc instead of mutating session directly. |
| internal/conversation/turn.go | Introduces RunTurn orchestration helper that builds outbound history and commits user+assistant on success. |
| internal/conversation/turn_test.go | Adds multi-turn and failure/cancellation tests for outbound message sequence and session immutability on errors. |
Review details
- Files reviewed: 3/3 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| import ( | ||
| "context" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // ChatFunc requests one assistant response for an ordered message history. | ||
| type ChatFunc func(context.Context, string, []Message) (Message, error) | ||
|
|
||
| // RunTurn sends a user prompt with the session history and commits the | ||
| // user-assistant exchange only when chat succeeds. Any returned error leaves | ||
| // the session unchanged. | ||
| func RunTurn(ctx context.Context, session *Session, model string, prompt string, chat ChatFunc) (Message, error) { | ||
| messages := session.Messages() | ||
| messages = append(messages, Message{Role: RoleUser, Content: prompt}) | ||
|
|
||
| response, err := chat(ctx, model, messages) | ||
| if err != nil { | ||
| return Message{}, fmt.Errorf("run conversation turn: %w", err) | ||
| } | ||
|
|
||
| session.AppendUser(prompt) | ||
| session.AppendAssistant(response.Content) | ||
|
|
||
| return response, nil | ||
| } |
Closes #298