Skip to content

Commit 8fa4702

Browse files
bkudiessCopilot
authored andcommitted
fix(chat): isolate user frames across reset
Keep run-scoped clock-skew floors limited to run-identifiable non-user frames so delayed pre-reset user messages cannot reappear after an exact echo opens the gate. Co-authored-by: Copilot App <[email protected]>
1 parent cff9b25 commit 8fa4702

3 files changed

Lines changed: 228 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,14 @@ internal ChatResetMessageGate EvaluateChatMessage(
286286
}
287287
if (!_awaitingUserMessage.Contains(threadId))
288288
{
289-
return new(
290-
!IsTimestampAcceptedForRun(
289+
var timestampAccepted = role == "user"
290+
? !IsPreResetTimestamp(threadId, timestampMs)
291+
: IsTimestampAcceptedForRun(
291292
threadId,
292293
activeRunId,
293-
timestampMs),
294+
timestampMs);
295+
return new(
296+
!timestampAccepted,
294297
null,
295298
false,
296299
null);

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

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,79 @@ public void AcceptedLifecycleFloor_AllowsSameRunAndRejectsOlderOrCompletedRun()
675675
Assert.True(completed.Drop);
676676
}
677677

678+
[Fact]
679+
public void AcceptedLifecycleFloor_NeverAuthorizesUserRoleFrames()
680+
{
681+
const string marker = "exact local echo";
682+
var now = DateTimeOffset.UtcNow;
683+
var cutoff = now.ToUnixTimeMilliseconds();
684+
var lifecycleTimestamp = cutoff - 5_000;
685+
var state = new ChatResetState();
686+
var generation = state.BeginReset("main", cutoff);
687+
state.RegisterPendingLocalSubmission(
688+
"main",
689+
"submission",
690+
marker,
691+
generation,
692+
state.LifecycleStartSequence,
693+
now);
694+
var start = Lifecycle(
695+
"start",
696+
"current-run",
697+
DateTimeOffset.FromUnixTimeMilliseconds(
698+
lifecycleTimestamp));
699+
Assert.True(state.EvaluateAgentEvent(start, "main").Drop);
700+
var echo = state.EvaluateChatMessage(
701+
"main",
702+
"user",
703+
marker,
704+
lifecycleTimestamp + 1,
705+
hasPendingLocalEcho: false);
706+
Assert.Same(start, echo.OpenedLifecycleStart);
707+
708+
var delayedUser = state.EvaluateChatMessage(
709+
"main",
710+
"user",
711+
"delayed unrelated user",
712+
lifecycleTimestamp + 2,
713+
hasPendingLocalEcho: false,
714+
activeRunId: "current-run");
715+
var delayedApproval = state.EvaluateChatMessage(
716+
"main",
717+
"user",
718+
"/approve abcdef allow-once",
719+
lifecycleTimestamp + 3,
720+
hasPendingLocalEcho: false,
721+
activeRunId: "current-run");
722+
var delayedControl = state.EvaluateChatMessage(
723+
"main",
724+
"user",
725+
"System: Reset session",
726+
lifecycleTimestamp + 4,
727+
hasPendingLocalEcho: false,
728+
activeRunId: "current-run");
729+
var sameRunAssistant = state.EvaluateChatMessage(
730+
"main",
731+
"assistant",
732+
"current response",
733+
lifecycleTimestamp + 5,
734+
hasPendingLocalEcho: false,
735+
activeRunId: "current-run");
736+
var postCutoffUser = state.EvaluateChatMessage(
737+
"main",
738+
"user",
739+
"fresh remote user",
740+
cutoff + 1,
741+
hasPendingLocalEcho: false,
742+
activeRunId: "current-run");
743+
744+
Assert.True(delayedUser.Drop);
745+
Assert.True(delayedApproval.Drop);
746+
Assert.True(delayedControl.Drop);
747+
Assert.False(sameRunAssistant.Drop);
748+
Assert.False(postCutoffUser.Drop);
749+
}
750+
678751
[Fact]
679752
public void AcceptedLifecycleFloor_ResetAndReconnectClearState()
680753
{
@@ -771,7 +844,7 @@ public void RemovePendingSubmission_RemovesOnlyMatchingIdentityAndGeneration()
771844
}
772845

773846
[Fact]
774-
public void MatchedSubmissionEcho_DoesNotConsumeLaterIdenticalRemoteMessage()
847+
public void MatchedSubmissionEcho_DoesNotConsumeLaterIdenticalPostCutoffMessage()
775848
{
776849
var now = DateTimeOffset.UtcNow;
777850
var cutoff = now.ToUnixTimeMilliseconds();
@@ -800,7 +873,7 @@ public void MatchedSubmissionEcho_DoesNotConsumeLaterIdenticalRemoteMessage()
800873
"main",
801874
"user",
802875
"same text",
803-
cutoff - 4_000,
876+
cutoff + 1,
804877
hasPendingLocalEcho: false,
805878
activeRunId: "new-run");
806879

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

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,6 +4122,153 @@ public async Task SessionResetCompletion_FallbackStepUsesExactEchoTimestampFloor
41224122
await provider.DisposeAsync();
41234123
}
41244124

4125+
[Fact]
4126+
public async Task SessionResetCompletion_RunFloorDoesNotAdmitPreResetUserFrames()
4127+
{
4128+
const string marker = "user floor marker";
4129+
const string delayedUser = "delayed unrelated user";
4130+
const string freshUser = "fresh remote user";
4131+
const string response = "same-run terminal";
4132+
using var activities = new ChatActivityCollector();
4133+
var (bridge, provider, snapshots, _) =
4134+
CreateProvider(new[] { MainSession() });
4135+
bridge.SendResults.Enqueue(new ChatSendResult());
4136+
await provider.LoadAsync();
4137+
var lifecycleTimestamp = DateTimeOffset.UtcNow
4138+
.AddSeconds(-5)
4139+
.ToUnixTimeMilliseconds();
4140+
bridge.RaiseSessionCommandCompleted(new SessionCommandResult
4141+
{
4142+
Method = "sessions.reset",
4143+
Ok = true,
4144+
Key = "main"
4145+
});
4146+
await provider.SendMessageAsync("main", marker);
4147+
4148+
var start = MakeAgentEvent(
4149+
"lifecycle",
4150+
"""{"phase":"start"}""",
4151+
runId: "current-run");
4152+
start.Ts = lifecycleTimestamp;
4153+
bridge.RaiseAgent(start);
4154+
bridge.RaiseChat(new ChatMessageInfo
4155+
{
4156+
SessionKey = "main",
4157+
Role = "user",
4158+
Text = marker,
4159+
Ts = lifecycleTimestamp + 1,
4160+
OpenClawId = "marker-user",
4161+
OpenClawSeq = 1,
4162+
});
4163+
bridge.RaiseChat(new ChatMessageInfo
4164+
{
4165+
SessionKey = "main",
4166+
Role = "user",
4167+
Text = delayedUser,
4168+
Ts = lifecycleTimestamp + 2,
4169+
OpenClawId = "delayed-user",
4170+
OpenClawSeq = 2,
4171+
});
4172+
var sameRunFrame = MakeAgentEvent(
4173+
"assistant",
4174+
"""{"delta":"same-run partial"}""",
4175+
runId: "current-run");
4176+
sameRunFrame.Ts = lifecycleTimestamp + 3;
4177+
bridge.RaiseAgent(sameRunFrame);
4178+
bridge.RaiseChat(new ChatMessageInfo
4179+
{
4180+
SessionKey = "main",
4181+
Role = "user",
4182+
Text = freshUser,
4183+
Ts = DateTimeOffset.UtcNow.AddSeconds(1)
4184+
.ToUnixTimeMilliseconds(),
4185+
OpenClawId = "fresh-user",
4186+
OpenClawSeq = 3,
4187+
});
4188+
bridge.RaiseChat(new ChatMessageInfo
4189+
{
4190+
SessionKey = "main",
4191+
Role = "assistant",
4192+
State = "final",
4193+
Text = response,
4194+
Ts = lifecycleTimestamp + 4,
4195+
OpenClawId = "assistant-final",
4196+
OpenClawSeq = 4,
4197+
});
4198+
4199+
var terminal = snapshots[^1].Timelines["main"];
4200+
Assert.False(terminal.TurnActive);
4201+
Assert.Single(
4202+
terminal.Entries,
4203+
entry => entry.Kind == ChatTimelineItemKind.User &&
4204+
entry.Text == marker);
4205+
Assert.DoesNotContain(
4206+
terminal.Entries,
4207+
entry => entry.Text == delayedUser);
4208+
Assert.Contains(
4209+
terminal.Entries,
4210+
entry => entry.Kind == ChatTimelineItemKind.User &&
4211+
entry.Text == freshUser);
4212+
Assert.Contains(
4213+
terminal.Entries,
4214+
entry => entry.Kind == ChatTimelineItemKind.Assistant &&
4215+
entry.Text == response);
4216+
Assert.Single(
4217+
activities.Stopped,
4218+
activity =>
4219+
activity.OperationName ==
4220+
ChatTelemetryTracker.TurnSpanName);
4221+
4222+
bridge.HistoryBehavior = _ => Task.FromResult(new ChatHistoryInfo
4223+
{
4224+
SessionKey = "main",
4225+
Messages =
4226+
[
4227+
new ChatMessageInfo
4228+
{
4229+
SessionKey = "main",
4230+
Role = "user",
4231+
Text = marker,
4232+
Ts = lifecycleTimestamp + 1,
4233+
OpenClawId = "marker-user",
4234+
OpenClawSeq = 1,
4235+
},
4236+
new ChatMessageInfo
4237+
{
4238+
SessionKey = "main",
4239+
Role = "assistant",
4240+
State = "final",
4241+
Text = response,
4242+
Ts = lifecycleTimestamp + 4,
4243+
OpenClawId = "assistant-final",
4244+
OpenClawSeq = 4,
4245+
},
4246+
]
4247+
});
4248+
await provider.LoadHistoryAsync(
4249+
"main",
4250+
force: true,
4251+
authoritative: true);
4252+
var replay = snapshots[^1].Timelines["main"];
4253+
Assert.Single(
4254+
replay.Entries,
4255+
entry => entry.Kind == ChatTimelineItemKind.User &&
4256+
entry.Text == marker);
4257+
Assert.DoesNotContain(
4258+
replay.Entries,
4259+
entry => entry.Text == delayedUser);
4260+
Assert.Single(
4261+
replay.Entries,
4262+
entry => entry.Kind == ChatTimelineItemKind.Assistant &&
4263+
entry.Text == response);
4264+
Assert.Single(
4265+
activities.Stopped,
4266+
activity =>
4267+
activity.OperationName ==
4268+
ChatTelemetryTracker.TurnSpanName);
4269+
await provider.DisposeAsync();
4270+
}
4271+
41254272
[Fact]
41264273
public async Task SessionResetCompletion_ExactEchoSelectsNewestBufferedLifecycle()
41274274
{

0 commit comments

Comments
 (0)