Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/OpenClaw.Tray.WinUI/Chat/ChatLifecycleCommandDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,20 @@ internal static class ChatLifecycleSelectionPolicy
{
public static string? RetainPendingForSelection(
string? pendingSelectedId,
string? selectedId) =>
pendingSelectedId is not null &&
string? selectedId,
bool selectedMaterialized = false) =>
!selectedMaterialized && pendingSelectedId is not null &&
string.Equals(pendingSelectedId, selectedId, StringComparison.Ordinal)
? pendingSelectedId
: null;

public static bool IsComposeOnlyWelcomeEligible(
string? threadId,
string? pendingSelectedId,
bool hasRealThreads) =>
!hasRealThreads || (pendingSelectedId is not null &&
string.Equals(threadId, pendingSelectedId, StringComparison.Ordinal));

public static bool ShouldFallback(
string staleSelectedId,
string? pendingSelectedId,
Expand Down
10 changes: 7 additions & 3 deletions src/OpenClaw.Tray.WinUI/Chat/OpenClawReactorChatRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ public override Element Render()
string.Equals(thread.Id, fallbackId, StringComparison.Ordinal));
}

// A synthetic compose-only thread does not acknowledge session materialization.
_pendingSelectedThreadId = ChatLifecycleSelectionPolicy.RetainPendingForSelection(
_pendingSelectedThreadId,
selectedId,
selectedMaterializedThread is not null);
var effectiveThread = selectedMaterializedThread ?? CreateComposeOnlyThread(props.Provider, snapshot);
if (effectiveThread is { } selected && string.Equals(_pendingSelectedThreadId, selected.Id, StringComparison.Ordinal))
_pendingSelectedThreadId = null;

var connectionState = ToConnectionState(snapshot.ConnectionStatus);
var isGatewayConnected = string.Equals(connectionState, "connected", StringComparison.Ordinal);
Expand Down Expand Up @@ -187,7 +190,8 @@ public override Element Render()
var welcomeEligible = isEmptyConversation
&& isGatewayConnected
&& (
(isComposeOnly && !hasRealThreads)
(isComposeOnly && ChatLifecycleSelectionPolicy.IsComposeOnlyWelcomeEligible(
effectiveThread?.Id, _pendingSelectedThreadId, hasRealThreads))
|| (!isComposeOnly && timeline.HistoryLoaded));
var welcomeEligibilityKey = welcomeEligible
? $"{effectiveThread?.Id}|{isComposeOnly}|{timeline.HistoryLoaded}|{hasRealThreads}"
Expand Down
32 changes: 32 additions & 0 deletions tests/OpenClaw.Tray.Tests/ChatComposerControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,38 @@ public async Task SendAsync_NewCommand_HandsCanonicalSessionKeyToBoundSelection(
Assert.Equal(0, port.SendMessageCallCount);
}

[Fact]
public async Task SendAsync_AfterNew_FirstMessageTargetsPendingComposeOnlySession()
{
var (vm, controller, port, _) = MakeController();
vm.SetDraft("/new");
port.ExecuteLifecycleGate = new TaskCompletionSource<ChatLifecycleCommandResult>();
port.ExecuteLifecycleGate.SetResult(new ChatLifecycleCommandResult(
ChatLifecycleCommandKind.New, Succeeded: true, NewSessionKey: "new-session-key"));
string? selected = null;
controller.BindSelectionHandoff(key => selected = key);
Assert.True(await controller.SendAsync());
Assert.Equal("new-session-key", selected);

string? pending = selected;
for (var render = 0; render < 3; render++)
{
pending = ChatLifecycleSelectionPolicy.RetainPendingForSelection(
pending, selected, selectedMaterialized: false);
Assert.Equal(selected, pending);
Assert.False(ChatLifecycleSelectionPolicy.ShouldFallback(selected!, pending, "session-1"));
// The root projects its effective compose-only thread into inputs
// while the provider's real thread list still contains only Main.
vm.ApplyInputs(MakeInputs(revision: render + 2, thread: MakeThread(pending!)));
}

vm.SetDraft("first message");
Assert.True(await controller.SendAsync());
Assert.Equal(1, port.SendMessageCallCount);
Assert.Equal("new-session-key", port.LastSendMessageCall!.Value.ThreadId);
Assert.Equal("first message", port.LastSendMessageCall.Value.Message);
}

[Fact]
public async Task SendAsync_Compact_UsesQueuePathNotLifecycleExecute()
{
Expand Down
53 changes: 53 additions & 0 deletions tests/OpenClaw.Tray.Tests/ChatLifecycleSelectionPolicyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using OpenClawTray.Chat;

namespace OpenClaw.Tray.Tests;

public sealed class ChatLifecycleSelectionPolicyTests
{
[Fact]
public void PendingNewSession_SurvivesRepeatedStaleSnapshotsUntilMaterialization()
{
const string selected = "agent:main:new-session";
string? pending = selected;

// Main is the only real thread throughout these renders. The selected
// thread is synthetic and must remain usable until history catches up.
for (var render = 0; render < 3; render++)
{
Assert.False(ChatLifecycleSelectionPolicy.ShouldFallback(selected, pending, "main"));
pending = ChatLifecycleSelectionPolicy.RetainPendingForSelection(
pending, selected, selectedMaterialized: false);
Assert.Equal(selected, pending);
Assert.True(ChatLifecycleSelectionPolicy.IsComposeOnlyWelcomeEligible(
selected, pending, hasRealThreads: true));
}

pending = ChatLifecycleSelectionPolicy.RetainPendingForSelection(
pending, selected, selectedMaterialized: true);
Assert.Null(pending);
Assert.True(ChatLifecycleSelectionPolicy.ShouldFallback(selected, pending, "main"));
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public void ExplicitNavigationAway_RetiresPreviousPendingSelection(bool materialized)
{
Assert.Null(ChatLifecycleSelectionPolicy.RetainPendingForSelection(
"new-session", "main", materialized));
Assert.False(ChatLifecycleSelectionPolicy.ShouldFallback("main", null, "main"));
}

[Theory]
[InlineData(null, false, true)]
[InlineData(null, true, false)]
[InlineData("other-session", false, true)]
[InlineData("other-session", true, false)]
[InlineData("compose-session", true, true)]
public void ComposeOnlyWelcome_PreservesUnrelatedSessionBehavior(
string? pending, bool hasRealThreads, bool expected)
{
Assert.Equal(expected, ChatLifecycleSelectionPolicy.IsComposeOnlyWelcomeEligible(
"compose-session", pending, hasRealThreads));
}
}