Skip to content

Commit ef6ac8a

Browse files
shanselmanCopilot
andcommitted
fix(chat): surface keyless event drops
Keep dropping inbound chat and agent timeline events that lack a canonical sessionKey, but raise a one-shot visible diagnostic without including dropped payload content. Co-authored-by: Copilot <[email protected]>
1 parent 5a87c99 commit ef6ac8a

8 files changed

Lines changed: 140 additions & 4 deletions

File tree

‎docs/CONNECTION_ARCHITECTURE.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ MigrateFromSettings(...) // one-time legacy migration
6262

6363
The operator client is received through the `OperatorClientChanged` event. The app subscribes to data events (sessions, nodes, usage, config, pairing, models, agents, etc.) and calls request methods for chat, node invocations, and configuration.
6464

65+
### Chat timeline event routing
66+
67+
Inbound chat and agent timeline events must include the gateway's canonical `sessionKey`. The tray client must not synthesize a literal `main` key for keyless inbound events, because that can merge unrelated events into the wrong timeline. When a keyless chat or agent event arrives, the tray drops it and raises a one-shot diagnostic so the protocol issue is visible without exposing the dropped message contents.
68+
6569
## Startup wiring (App.xaml.cs)
6670

6771
```

‎src/OpenClaw.Tray.WinUI/Chat/OpenClawChatDataProvider.cs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public sealed class OpenClawChatDataProvider : IChatDataProvider
9898
// Track recently-sent local user message texts so we can suppress
9999
// SSE echoes while still displaying messages from other clients.
100100
private readonly Dictionary<string, Queue<LocalSentText>> _localSentTexts = new();
101+
private int _keylessEventDiagnosticRaised;
101102
// Threads where we locally initiated the current turn (via SendMessageAsync).
102103
// When lifecycle.start arrives for a thread NOT in this set, we know a remote
103104
// client started the turn and should fetch the user message from history.
@@ -1021,6 +1022,7 @@ private void OnChatMessageReceived(object? sender, ChatMessageInfo message)
10211022
if (string.IsNullOrEmpty(message.SessionKey))
10221023
{
10231024
Logger.Warn($"[ChatProvider] Dropping chat message with empty sessionKey (role={message.Role})");
1025+
RaiseKeylessEventDiagnosticOnce();
10241026
return;
10251027
}
10261028

@@ -1152,6 +1154,7 @@ private void OnAgentEventReceived(object? sender, AgentEventInfo evt)
11521154
if (string.IsNullOrEmpty(evt.SessionKey))
11531155
{
11541156
Logger.Warn($"[ChatProvider] Dropping agent event with empty sessionKey (stream={evt.Stream})");
1157+
RaiseKeylessEventDiagnosticOnce();
11551158
return;
11561159
}
11571160
var threadId = evt.SessionKey;
@@ -1212,6 +1215,34 @@ private void OnAgentEventReceived(object? sender, AgentEventInfo evt)
12121215
ApplyEventAndPublish(threadId, mapped, meta);
12131216
}
12141217

1218+
private void RaiseKeylessEventDiagnosticOnce()
1219+
{
1220+
if (System.Threading.Interlocked.Exchange(ref _keylessEventDiagnosticRaised, 1) != 0)
1221+
return;
1222+
1223+
var threadId = GetKeylessEventDiagnosticThreadId();
1224+
var title = LocalizationHelper.GetString("Chat_Notification_KeylessEventDropped");
1225+
var message = LocalizationHelper.GetString("Chat_Notification_KeylessEventDroppedMessage");
1226+
1227+
RaiseNotification(new ChatProviderNotification(
1228+
ChatProviderNotificationKind.Error,
1229+
threadId ?? string.Empty,
1230+
title,
1231+
message));
1232+
1233+
if (!string.IsNullOrWhiteSpace(threadId))
1234+
ApplyEventAndPublish(threadId, new ChatStatusEvent(message, ChatTone.Warning));
1235+
}
1236+
1237+
private string? GetKeylessEventDiagnosticThreadId()
1238+
{
1239+
lock (_gate)
1240+
{
1241+
return ResolveDefaultThreadIdLocked()
1242+
?? _timelines.Keys.FirstOrDefault(k => !string.IsNullOrWhiteSpace(k));
1243+
}
1244+
}
1245+
12151246
private string? _deferredAbortRunId; // set inside lock when pending abort fires; read outside lock to send RPC
12161247
private int _deferredAbortCount; // how many user messages to force-persist as aborted
12171248

‎src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,6 +2958,12 @@ On your gateway host (Mac/Linux), run:
29582958
<data name="Chat_Notification_ConnectionInterrupted" xml:space="preserve">
29592959
<value>Connection lost — response interrupted.</value>
29602960
</data>
2961+
<data name="Chat_Notification_KeylessEventDropped" xml:space="preserve">
2962+
<value>Gateway timeline event dropped</value>
2963+
</data>
2964+
<data name="Chat_Notification_KeylessEventDroppedMessage" xml:space="preserve">
2965+
<value>The gateway sent a chat timeline event without a session key, so OpenClaw ignored it to avoid mixing timelines. Update or restart the gateway if this continues.</value>
2966+
</data>
29612967
<data name="VoiceSettingsPage_TestVoiceButtonText.Text" xml:space="preserve">
29622968
<value>Test Voice Input</value>
29632969
</data>

‎src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,12 @@ Sur votre hôte passerelle (Mac/Linux), exécutez :
29102910
<data name="Chat_Notification_ConnectionInterrupted" xml:space="preserve">
29112911
<value>Connexion perdue — réponse interrompue.</value>
29122912
</data>
2913+
<data name="Chat_Notification_KeylessEventDropped" xml:space="preserve">
2914+
<value>Événement de chronologie de passerelle ignoré</value>
2915+
</data>
2916+
<data name="Chat_Notification_KeylessEventDroppedMessage" xml:space="preserve">
2917+
<value>La passerelle a envoyé un événement de chronologie de chat sans clé de session ; OpenClaw l'a ignoré pour éviter de mélanger les conversations. Mettez à jour ou redémarrez la passerelle si cela continue.</value>
2918+
</data>
29132919
<data name="VoiceSettingsPage_TestVoiceButtonText.Text" xml:space="preserve">
29142920
<value>Tester la saisie vocale</value>
29152921
</data>

‎src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,6 +2911,12 @@ Voer op uw gateway-host (Mac/Linux) uit:
29112911
<data name="Chat_Notification_ConnectionInterrupted" xml:space="preserve">
29122912
<value>Verbinding verbroken — antwoord onderbroken.</value>
29132913
</data>
2914+
<data name="Chat_Notification_KeylessEventDropped" xml:space="preserve">
2915+
<value>Gateway-tijdlijngebeurtenis genegeerd</value>
2916+
</data>
2917+
<data name="Chat_Notification_KeylessEventDroppedMessage" xml:space="preserve">
2918+
<value>De gateway heeft een chattijdlijngebeurtenis zonder sessiesleutel verzonden, dus OpenClaw heeft deze genegeerd om te voorkomen dat tijdlijnen door elkaar lopen. Werk de gateway bij of start deze opnieuw als dit doorgaat.</value>
2919+
</data>
29142920
<data name="VoiceSettingsPage_TestVoiceButtonText.Text" xml:space="preserve">
29152921
<value>Spraakinvoer testen</value>
29162922
</data>

‎src/OpenClaw.Tray.WinUI/Strings/zh-cn/Resources.resw‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,12 @@
29102910
<data name="Chat_Notification_ConnectionInterrupted" xml:space="preserve">
29112911
<value>连接已断开 — 回复已中断。</value>
29122912
</data>
2913+
<data name="Chat_Notification_KeylessEventDropped" xml:space="preserve">
2914+
<value>已丢弃网关时间线事件</value>
2915+
</data>
2916+
<data name="Chat_Notification_KeylessEventDroppedMessage" xml:space="preserve">
2917+
<value>网关发送了缺少会话键的聊天时间线事件,OpenClaw 已将其忽略,以避免混淆时间线。如果此情况持续,请更新或重启网关。</value>
2918+
</data>
29132919
<data name="VoiceSettingsPage_TestVoiceButtonText.Text" xml:space="preserve">
29142920
<value>测试语音输入</value>
29152921
</data>

‎src/OpenClaw.Tray.WinUI/Strings/zh-tw/Resources.resw‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,6 +2910,12 @@
29102910
<data name="Chat_Notification_ConnectionInterrupted" xml:space="preserve">
29112911
<value>連線已中斷 — 回覆已中斷。</value>
29122912
</data>
2913+
<data name="Chat_Notification_KeylessEventDropped" xml:space="preserve">
2914+
<value>已捨棄閘道時間軸事件</value>
2915+
</data>
2916+
<data name="Chat_Notification_KeylessEventDroppedMessage" xml:space="preserve">
2917+
<value>閘道傳送了缺少工作階段金鑰的聊天時間軸事件,OpenClaw 已將其忽略,以避免混淆時間軸。如果此情況持續,請更新或重新啟動閘道。</value>
2918+
</data>
29132919
<data name="VoiceSettingsPage_TestVoiceButtonText.Text" xml:space="preserve">
29142920
<value>測試語音輸入</value>
29152921
</data>

‎tests/OpenClaw.Tray.Tests/OpenClawChatDataProviderTests.cs‎

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,17 @@ private static (FakeBridge bridge, OpenClawChatDataProvider provider, List<ChatD
19871987
return (bridge, provider, snapshots);
19881988
}
19891989

1990+
private static (FakeBridge bridge, OpenClawChatDataProvider provider, List<ChatDataSnapshot> snapshots, List<ChatProviderNotification> notifications)
1991+
CreateConnectedProviderWithNotifications(string canonicalMainKey = "agent:main:main")
1992+
{
1993+
var (bridge, provider, snapshots, notifications) = CreateProvider();
1994+
bridge.HasHandshakeSnapshot = true;
1995+
bridge.MainSessionKey = canonicalMainKey;
1996+
bridge.RaiseStatus(ConnectionStatus.Connected);
1997+
bridge.RaiseSessions(Array.Empty<SessionInfo>());
1998+
return (bridge, provider, snapshots, notifications);
1999+
}
2000+
19902001
[Fact]
19912002
public async Task SendMessageAsync_FreshInstall_OptimisticEntryKeyedByCanonicalSessionKey()
19922003
{
@@ -2062,10 +2073,9 @@ public async Task ChatEvent_WithEmptySessionKey_IsDropped()
20622073
// of the bug: it would silently route mis-routed events to a synthetic
20632074
// bucket. The fix is to drop the event and log — surfacing protocol
20642075
// bugs instead of papering over them.
2065-
var (bridge, provider, snapshots) = CreateConnectedProvider("agent:main:main");
2076+
var (bridge, provider, snapshots, notifications) = CreateConnectedProviderWithNotifications("agent:main:main");
20662077
await provider.LoadAsync();
20672078
await provider.SendMessageAsync("agent:main:main", "hi");
2068-
var snapshotCountBefore = snapshots.Count;
20692079

20702080
bridge.RaiseChat(new ChatMessageInfo
20712081
{
@@ -2074,9 +2084,70 @@ public async Task ChatEvent_WithEmptySessionKey_IsDropped()
20742084
Text = "echo with no session key — should be dropped"
20752085
});
20762086

2077-
Assert.Equal(snapshotCountBefore, snapshots.Count);
20782087
// And specifically: no synthetic "main" timeline was created.
2079-
Assert.False(snapshots[^1].Timelines.ContainsKey("main"));
2088+
var latest = snapshots[^1];
2089+
var timeline = latest.Timelines["agent:main:main"];
2090+
Assert.False(latest.Timelines.ContainsKey("main"));
2091+
Assert.DoesNotContain(timeline.Entries, e => e.Text.Contains("echo with no session key"));
2092+
Assert.Contains(timeline.Entries, e =>
2093+
e.Kind == ChatTimelineItemKind.Status &&
2094+
e.Tone == ChatTone.Warning &&
2095+
e.Text == "Chat_Notification_KeylessEventDroppedMessage");
2096+
Assert.Single(notifications, n => n.Title == "Chat_Notification_KeylessEventDropped");
2097+
Assert.DoesNotContain(notifications, n =>
2098+
(n.Title?.Contains("echo with no session key") ?? false) ||
2099+
(n.Message?.Contains("echo with no session key") ?? false));
2100+
}
2101+
2102+
[Fact]
2103+
public async Task KeylessEvents_RaiseOnlyOneDiagnostic()
2104+
{
2105+
var (bridge, provider, snapshots, notifications) = CreateConnectedProviderWithNotifications("agent:main:main");
2106+
await provider.LoadAsync();
2107+
2108+
bridge.RaiseChat(new ChatMessageInfo
2109+
{
2110+
SessionKey = "",
2111+
Role = "assistant",
2112+
Text = "first dropped payload"
2113+
});
2114+
bridge.RaiseAgent(MakeAgentEvent("assistant", """{"delta":"second dropped payload"}""", sessionKey: ""));
2115+
bridge.RaiseChat(new ChatMessageInfo
2116+
{
2117+
SessionKey = "",
2118+
Role = "assistant",
2119+
Text = "third dropped payload"
2120+
});
2121+
2122+
Assert.Single(notifications, n => n.Title == "Chat_Notification_KeylessEventDropped");
2123+
Assert.Single(snapshots[^1].Timelines["agent:main:main"].Entries, e =>
2124+
e.Kind == ChatTimelineItemKind.Status &&
2125+
e.Text == "Chat_Notification_KeylessEventDroppedMessage");
2126+
Assert.DoesNotContain(notifications, n =>
2127+
(n.Title?.Contains("dropped payload") ?? false) ||
2128+
(n.Message?.Contains("dropped payload") ?? false));
2129+
}
2130+
2131+
[Fact]
2132+
public async Task AgentEvent_WithEmptySessionKey_IsDroppedAndDiagnosed()
2133+
{
2134+
var (bridge, provider, snapshots, notifications) = CreateConnectedProviderWithNotifications("agent:main:main");
2135+
await provider.LoadAsync();
2136+
2137+
bridge.RaiseAgent(MakeAgentEvent("assistant", """{"delta":"secret agent payload"}""", sessionKey: ""));
2138+
2139+
var latest = snapshots[^1];
2140+
var timeline = latest.Timelines["agent:main:main"];
2141+
Assert.False(latest.Timelines.ContainsKey("main"));
2142+
Assert.DoesNotContain(timeline.Entries, e => e.Text.Contains("secret agent payload"));
2143+
Assert.Contains(timeline.Entries, e =>
2144+
e.Kind == ChatTimelineItemKind.Status &&
2145+
e.Tone == ChatTone.Warning &&
2146+
e.Text == "Chat_Notification_KeylessEventDroppedMessage");
2147+
Assert.Single(notifications, n => n.Title == "Chat_Notification_KeylessEventDropped");
2148+
Assert.DoesNotContain(notifications, n =>
2149+
(n.Title?.Contains("secret agent payload") ?? false) ||
2150+
(n.Message?.Contains("secret agent payload") ?? false));
20802151
}
20812152

20822153
[Fact]

0 commit comments

Comments
 (0)