Skip to content

Commit b718ef7

Browse files
committed
Add reciprocal finish_reason validation and clarify session restore scope
- Override stop→tool_calls when tool calls are present (symmetric with the existing tool_calls→null guard) - Expand comment explaining why session restore scopes to parent messages only
1 parent 6403dfa commit b718ef7

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

pkg/runtime/runtime.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,11 @@ func (r *LocalRuntime) EmitStartupInfo(ctx context.Context, sess *session.Sessio
865865

866866
// Reconstruct LastMessage from the parent session's last assistant
867867
// message so that FinishReason (and other per-message fields) are
868-
// available on session restore. We iterate sess.Messages directly
869-
// (not GetAllMessages) to avoid picking up sub-agent messages.
868+
// available on session restore. We intentionally iterate
869+
// sess.Messages (not GetAllMessages) so the result reflects the
870+
// parent agent's state: this event carries the parent session_id,
871+
// and sub-agents emit their own token_usage events with their own
872+
// session_id during live streaming.
870873
for i := len(sess.Messages) - 1; i >= 0; i-- {
871874
item := &sess.Messages[i]
872875
if !item.IsMessage() || item.Message.Message.Role != chat.MessageRoleAssistant {

pkg/runtime/streaming.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,12 @@ func (r *LocalRuntime) handleStream(ctx context.Context, stream chat.MessageStre
219219
finishReason = chat.FinishReasonNull
220220
}
221221
}
222-
// Guard against a provider claiming tool_calls when no calls arrived.
223-
if finishReason == chat.FinishReasonToolCalls && len(toolCalls) == 0 {
222+
// Ensure finish reason agrees with the actual stream output.
223+
switch {
224+
case finishReason == chat.FinishReasonToolCalls && len(toolCalls) == 0:
224225
finishReason = chat.FinishReasonNull
226+
case finishReason == chat.FinishReasonStop && len(toolCalls) > 0:
227+
finishReason = chat.FinishReasonToolCalls
225228
}
226229

227230
return streamResult{

0 commit comments

Comments
 (0)