mirror of
https://github.com/davidkaya/aryx.git
synced 2026-08-06 19:58:43 +02:00
refactor: finalize backend streaming ownership
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -847,6 +847,53 @@ public sealed class CopilotTurnExecutionStateTests
|
||||
Assert.Equal("agent-1", evt.AgentId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ObserveSessionEvent_AssistantMessage_FinalizesTranscriptAndSuppressesLateDeltas()
|
||||
{
|
||||
RunTurnCommandDto command = CreateCommand();
|
||||
CopilotTurnExecutionState state = new(command);
|
||||
|
||||
Assert.True(state.TryAppendDelta("msg-final", "Primary", "Draft response", out TranscriptSegment streamed));
|
||||
Assert.False(streamed.IsFinalized);
|
||||
|
||||
state.ObserveSessionEvent(
|
||||
command.Workflow.GetAgentNodes()[0],
|
||||
SessionEvent.FromJson(
|
||||
"""
|
||||
{
|
||||
"type": "assistant.message",
|
||||
"data": {
|
||||
"messageId": "msg-final",
|
||||
"content": "Provider final response"
|
||||
},
|
||||
"id": "12121212-1212-1212-1212-121212121212",
|
||||
"timestamp": "2026-03-27T00:00:00Z"
|
||||
}
|
||||
"""));
|
||||
|
||||
TurnDeltaEventDto correction = Assert.Single(state.DrainPendingEvents().OfType<TurnDeltaEventDto>());
|
||||
Assert.Equal("msg-final", correction.MessageId);
|
||||
Assert.Equal("Primary", correction.AuthorName);
|
||||
Assert.Equal(string.Empty, correction.ContentDelta);
|
||||
Assert.Equal("Provider final response", correction.Content);
|
||||
|
||||
Assert.False(state.TryAppendDelta("msg-final", "Primary", " late delta", out TranscriptSegment unchanged));
|
||||
Assert.True(unchanged.IsFinalized);
|
||||
Assert.Equal("Provider final response", unchanged.Content);
|
||||
|
||||
ChatMessage output = new(ChatRole.Assistant, "Workflow wording")
|
||||
{
|
||||
MessageId = "msg-final",
|
||||
AuthorName = "assistant",
|
||||
};
|
||||
|
||||
state.UpdateCompletedMessages([output], []);
|
||||
ChatMessageDto completed = Assert.Single(state.FinalizeCompletedMessages());
|
||||
Assert.Equal("msg-final", completed.Id);
|
||||
Assert.Equal("Primary", completed.AuthorName);
|
||||
Assert.Equal("Provider final response", completed.Content);
|
||||
}
|
||||
|
||||
private static SessionEvent CreateHookStartEvent()
|
||||
{
|
||||
return SessionEvent.FromJson(
|
||||
|
||||
@@ -451,6 +451,50 @@ public sealed class CopilotWorkflowRunnerTests
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectCompletedMessages_UsesExactMessageIdMatchBeforeFallbackHeuristics()
|
||||
{
|
||||
RunTurnCommandDto command = CreateCommand(
|
||||
"group-chat",
|
||||
CreateAgent(id: "agent-group-writer", name: "Writer"),
|
||||
CreateAgent(id: "agent-group-reviewer", name: "Reviewer"));
|
||||
|
||||
IReadOnlyList<ChatMessageDto> messages = WorkflowTranscriptProjector.ProjectCompletedMessagesFromSegments(
|
||||
command,
|
||||
[
|
||||
new ChatMessage(ChatRole.Assistant, "Revised draft with cleaner wording.")
|
||||
{
|
||||
AuthorName = "assistant",
|
||||
MessageId = "msg-writer-2",
|
||||
},
|
||||
new ChatMessage(ChatRole.Assistant, "Review feedback with cleaner wording.")
|
||||
{
|
||||
AuthorName = "assistant",
|
||||
MessageId = "msg-reviewer-1",
|
||||
},
|
||||
],
|
||||
[
|
||||
new TranscriptSegment("msg-writer-1", "Writer", "Initial draft."),
|
||||
new TranscriptSegment("msg-reviewer-1", "Reviewer", "Review feedback with draft wording."),
|
||||
new TranscriptSegment("msg-writer-2", "Writer", "Revised draft with draft wording."),
|
||||
]);
|
||||
|
||||
Assert.Collection(
|
||||
messages,
|
||||
writer =>
|
||||
{
|
||||
Assert.Equal("msg-writer-2", writer.Id);
|
||||
Assert.Equal("Writer", writer.AuthorName);
|
||||
Assert.Equal("Revised draft with cleaner wording.", writer.Content);
|
||||
},
|
||||
reviewer =>
|
||||
{
|
||||
Assert.Equal("msg-reviewer-1", reviewer.Id);
|
||||
Assert.Equal("Reviewer", reviewer.AuthorName);
|
||||
Assert.Equal("Review feedback with cleaner wording.", reviewer.Content);
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectCompletedMessages_UsesFallbackAgentForGenericAssistantOutput()
|
||||
{
|
||||
@@ -531,6 +575,36 @@ public sealed class CopilotWorkflowRunnerTests
|
||||
Assert.Equal("Done — GoogleClient.cs now contains the requested class with cleaned formatting.", message.Content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectCompletedMessages_PrefersFinalizedSegmentContentOverWorkflowOutput()
|
||||
{
|
||||
RunTurnCommandDto command = CreateCommand(
|
||||
"single",
|
||||
CreateAgent(id: "agent-single-primary", name: "Primary Agent"));
|
||||
|
||||
IReadOnlyList<ChatMessageDto> messages = WorkflowTranscriptProjector.ProjectCompletedMessagesFromSegments(
|
||||
command,
|
||||
[
|
||||
new ChatMessage(ChatRole.Assistant, "Workflow wording that arrived later.")
|
||||
{
|
||||
AuthorName = "assistant",
|
||||
MessageId = "msg-1",
|
||||
},
|
||||
],
|
||||
[
|
||||
new TranscriptSegment(
|
||||
"msg-1",
|
||||
"Primary Agent",
|
||||
"Provider final wording that should win.",
|
||||
IsFinalized: true),
|
||||
]);
|
||||
|
||||
ChatMessageDto message = Assert.Single(messages);
|
||||
Assert.Equal("msg-1", message.Id);
|
||||
Assert.Equal("Primary Agent", message.AuthorName);
|
||||
Assert.Equal("Provider final wording that should win.", message.Content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectCompletedMessages_DropsBlankAssistantOutputMessages()
|
||||
{
|
||||
@@ -602,7 +676,22 @@ public sealed class CopilotWorkflowRunnerTests
|
||||
Assert.Equal("msg-2", second.MessageId);
|
||||
Assert.Equal("Implementer", second.AuthorName);
|
||||
Assert.Equal("B", second.Content);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StreamingTranscriptBuffer_IgnoresLateDeltasAfterFinalSnapshot()
|
||||
{
|
||||
StreamingTranscriptBuffer buffer = new();
|
||||
|
||||
buffer.AppendDelta("msg-1", "Architect", "Draft");
|
||||
Assert.True(buffer.TryApplySnapshot("msg-1", "Architect", "Final", out TranscriptSegment finalized));
|
||||
Assert.True(finalized.IsFinalized);
|
||||
Assert.Equal("Final", finalized.Content);
|
||||
|
||||
Assert.False(buffer.TryAppendDelta("msg-1", "Architect", " late delta", out TranscriptSegment unchanged));
|
||||
Assert.True(unchanged.IsFinalized);
|
||||
Assert.Equal("Final", unchanged.Content);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user