Skip to content

Commit d2de615

Browse files
calebedenCopilot
andauthored
fix(chat): show checkpoint details for compacted history (#1091)
Co-authored-by: Copilot App <[email protected]>
1 parent ca8fb4a commit d2de615

18 files changed

Lines changed: 1001 additions & 279 deletions

‎.editorconfig‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ dotnet_diagnostic.REACTOR_DIALOG_001.severity = none
166166
[src/OpenClaw.Tray.WinUI/Windows/**/*.cs]
167167
dotnet_diagnostic.REACTOR_DIALOG_001.severity = none
168168

169+
# Shared XAML/Reactor workflow targets the invoking surface's XamlRoot.
170+
[src/OpenClaw.Tray.WinUI/Dialogs/SessionCheckpointDialogCoordinator.cs]
171+
dotnet_diagnostic.REACTOR_DIALOG_001.severity = none
172+
169173
[src/OpenClaw.Tray.WinUI/Chat/ReactorChatHostExtensions.cs]
170174
dotnet_diagnostic.REACTOR_DIALOG_001.severity = none
171175

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,54 @@
11
using System.Globalization;
2+
using OpenClaw.Chat;
23

34
namespace OpenClawTray.Chat;
45

56
internal sealed record ChatCompactionPresentation(
67
string Title,
78
string Detail,
9+
string ActionLabel,
810
string AutomationName);
911

1012
internal static class ChatCompactionPresenter
1113
{
14+
public static ChatCompactionPresentation? TryCreateForEntry(
15+
ChatTimelineItem entry,
16+
IReadOnlyDictionary<string, ChatEntryMetadata>? entryMetadata,
17+
string? title = null,
18+
string? detail = null,
19+
string? actionLabel = null)
20+
{
21+
if (entry.Kind != ChatTimelineItemKind.Status
22+
|| entryMetadata?.TryGetValue(entry.Id, out var metadata) != true
23+
|| metadata is null
24+
|| !string.Equals(metadata.OpenClawKind, "compaction", StringComparison.OrdinalIgnoreCase))
25+
{
26+
return null;
27+
}
28+
29+
title ??= "COMPACTED HISTORY";
30+
detail ??= "The compacted transcript is preserved as a checkpoint. " +
31+
"Open session checkpoints to branch or restore from that compacted view.";
32+
actionLabel ??= "Open checkpoints";
33+
return new ChatCompactionPresentation(
34+
title,
35+
detail,
36+
actionLabel,
37+
$"{title}. {detail}");
38+
}
39+
1240
public static ChatCompactionPresentation Create(
1341
long? tokensBefore,
1442
long? tokensAfter,
1543
string? title = null,
1644
string? metricsFormat = null,
17-
string? fallbackDetail = null)
45+
string? fallbackDetail = null,
46+
string? actionLabel = null)
1847
{
1948
title ??= "Context compacted";
2049
var detail = BuildDetail(tokensBefore, tokensAfter, metricsFormat, fallbackDetail);
21-
return new ChatCompactionPresentation(title, detail, $"{title}. {detail}");
50+
actionLabel ??= "Open checkpoints";
51+
return new ChatCompactionPresentation(title, detail, actionLabel, $"{title}. {detail}");
2252
}
2353

2454
private static string BuildDetail(

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

Lines changed: 92 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public sealed class OpenClawChatDataProvider : IChatDataProvider
118118
private readonly HashSet<string> _historyLoaded = new(); // sessionKey
119119
private readonly HashSet<string> _historyInFlight = new(); // sessionKey
120120
private readonly HashSet<string> _authoritativeHistoryReloadPending = new(); // sessionKey
121+
private readonly HashSet<string> _replacementHistoryReloadPending = new(); // sessionKey
122+
private readonly Dictionary<string, long> _historyReplacementVersions = new(); // sessionKey -> replacement generation
121123
private CancellationTokenSource _historyGenerationCancellation = new();
122124
private long _historyConnectionVersion;
123125
private readonly Dictionary<string, Task> _pendingModelPatches = new(); // sessionKey -> in-flight model set/clear
@@ -1008,17 +1010,52 @@ public async Task StopResponseAsync(string threadId, CancellationToken cancellat
10081010
public Task LoadHistoryAsync(string threadId, bool force = false, CancellationToken cancellationToken = default, bool authoritative = false)
10091011
=> LoadHistoryCoreAsync(threadId, force, cancellationToken, expectedConnectionVersion: null, authoritative: authoritative);
10101012

1013+
internal Task ReplaceHistoryAfterCheckpointRestoreAsync(
1014+
string threadId,
1015+
CancellationToken cancellationToken = default)
1016+
{
1017+
ArgumentException.ThrowIfNullOrWhiteSpace(threadId);
1018+
1019+
ChatDataSnapshot snapshot;
1020+
lock (_gate)
1021+
{
1022+
if (_disposed)
1023+
return Task.CompletedTask;
1024+
1025+
_historyReplacementVersions[threadId] = GetHistoryReplacementVersionLocked(threadId) + 1;
1026+
_timelines[threadId] = ChatTimelineState.Initial();
1027+
_entryMeta.Remove(threadId);
1028+
_historyLoaded.Remove(threadId);
1029+
_historyRetryCount.Remove(threadId);
1030+
_authoritativeHistoryReloadPending.Remove(threadId);
1031+
_replacementHistoryReloadPending.Remove(threadId);
1032+
snapshot = BuildSnapshotLocked();
1033+
}
1034+
1035+
Publish(snapshot);
1036+
return LoadHistoryCoreAsync(
1037+
threadId,
1038+
force: true,
1039+
cancellationToken,
1040+
expectedConnectionVersion: null,
1041+
authoritative: false,
1042+
replacementReload: true);
1043+
}
1044+
10111045
private async Task LoadHistoryCoreAsync(
10121046
string threadId,
10131047
bool force,
10141048
CancellationToken cancellationToken,
10151049
long? expectedConnectionVersion,
1016-
bool authoritative = false)
1050+
long? expectedHistoryReplacementVersion = null,
1051+
bool authoritative = false,
1052+
bool replacementReload = false)
10171053
{
10181054
cancellationToken.ThrowIfCancellationRequested();
10191055
if (string.IsNullOrEmpty(threadId)) return;
10201056

10211057
long requestResetVersion;
1058+
long requestHistoryReplacementVersion;
10221059
long requestConnectionVersion;
10231060
CancellationToken generationCancellationToken;
10241061
CancellationTokenSource requestCancellation;
@@ -1030,14 +1067,22 @@ private async Task LoadHistoryCoreAsync(
10301067
{
10311068
return;
10321069
}
1070+
if (expectedHistoryReplacementVersion is { } expectedReplacement &&
1071+
GetHistoryReplacementVersionLocked(threadId) != expectedReplacement)
1072+
{
1073+
return;
1074+
}
10331075
if (!force && _historyLoaded.Contains(threadId)) return;
10341076
if (!_historyInFlight.Add(threadId))
10351077
{
1036-
if (authoritative)
1078+
if (replacementReload)
1079+
_replacementHistoryReloadPending.Add(threadId);
1080+
else if (authoritative)
10371081
_authoritativeHistoryReloadPending.Add(threadId);
10381082
return;
10391083
}
10401084
requestResetVersion = GetResetVersionLocked(threadId);
1085+
requestHistoryReplacementVersion = GetHistoryReplacementVersionLocked(threadId);
10411086
requestConnectionVersion = _historyConnectionVersion;
10421087
generationCancellationToken = _historyGenerationCancellation.Token;
10431088
requestCancellation = CancellationTokenSource.CreateLinkedTokenSource(
@@ -1062,7 +1107,8 @@ private async Task LoadHistoryCoreAsync(
10621107
lock (_gate)
10631108
{
10641109
if (_historyConnectionVersion != requestConnectionVersion ||
1065-
GetResetVersionLocked(threadId) != requestResetVersion)
1110+
GetResetVersionLocked(threadId) != requestResetVersion ||
1111+
GetHistoryReplacementVersionLocked(threadId) != requestHistoryReplacementVersion)
10661112
{
10671113
Logger.Info($"[ChatHistory] Ignoring stale history for thread '{threadId}'");
10681114
historyOutcome = ChatTelemetryOutcome.Canceled;
@@ -1515,7 +1561,9 @@ ChatTimelineState ApplyAndCaptureMeta(ChatTimelineState s, ChatEvent e, ChatEntr
15151561
bool shouldRetry;
15161562
lock (_gate)
15171563
{
1518-
if (_disposed || _historyConnectionVersion != requestConnectionVersion)
1564+
if (_disposed ||
1565+
_historyConnectionVersion != requestConnectionVersion ||
1566+
GetHistoryReplacementVersionLocked(threadId) != requestHistoryReplacementVersion)
15191567
{
15201568
historyOutcome = ChatTelemetryOutcome.Canceled;
15211569
return;
@@ -1525,7 +1573,7 @@ ChatTimelineState ApplyAndCaptureMeta(ChatTimelineState s, ChatEvent e, ChatEntr
15251573
historyException = ex;
15261574
_historyRetryCount.TryGetValue(threadId, out var retries);
15271575
shouldRetry = _status == ConnectionStatus.Connected
1528-
&& (authoritative || !_historyLoaded.Contains(threadId))
1576+
&& (replacementReload || authoritative || !_historyLoaded.Contains(threadId))
15291577
&& retries < MaxHistoryRetries;
15301578
if (shouldRetry)
15311579
_historyRetryCount[threadId] = retries + 1;
@@ -1534,7 +1582,9 @@ ChatTimelineState ApplyAndCaptureMeta(ChatTimelineState s, ChatEvent e, ChatEntr
15341582
_historyFailureReservedForTesting?.Invoke();
15351583
lock (_gate)
15361584
{
1537-
if (_disposed || _historyConnectionVersion != requestConnectionVersion)
1585+
if (_disposed ||
1586+
_historyConnectionVersion != requestConnectionVersion ||
1587+
GetHistoryReplacementVersionLocked(threadId) != requestHistoryReplacementVersion)
15381588
{
15391589
historyOutcome = ChatTelemetryOutcome.Canceled;
15401590
historyException = null;
@@ -1549,11 +1599,15 @@ ChatTimelineState ApplyAndCaptureMeta(ChatTimelineState s, ChatEvent e, ChatEntr
15491599
threadId,
15501600
LocalizationHelper.GetString("Chat_Notification_LoadHistoryFailed"),
15511601
ex.Message),
1552-
requestConnectionVersion);
1602+
threadId,
1603+
requestConnectionVersion,
1604+
requestHistoryReplacementVersion);
15531605

15541606
lock (_gate)
15551607
{
1556-
if (_disposed || _historyConnectionVersion != requestConnectionVersion)
1608+
if (_disposed ||
1609+
_historyConnectionVersion != requestConnectionVersion ||
1610+
GetHistoryReplacementVersionLocked(threadId) != requestHistoryReplacementVersion)
15571611
{
15581612
historyOutcome = ChatTelemetryOutcome.Canceled;
15591613
historyException = null;
@@ -1576,28 +1630,44 @@ await LoadHistoryCoreAsync(
15761630
force: true,
15771631
CancellationToken.None,
15781632
expectedConnectionVersion: requestConnectionVersion,
1579-
authoritative: authoritative);
1633+
expectedHistoryReplacementVersion: requestHistoryReplacementVersion,
1634+
authoritative: authoritative,
1635+
replacementReload: replacementReload);
15801636
}));
15811637
}
15821638
}
15831639
finally
15841640
{
1641+
bool rerunReplacement;
15851642
bool rerunAuthoritative;
15861643
lock (_gate)
15871644
{
15881645
if (_historyConnectionVersion == requestConnectionVersion)
15891646
{
15901647
_historyInFlight.Remove(threadId);
1591-
rerunAuthoritative = _authoritativeHistoryReloadPending.Remove(threadId);
1648+
rerunReplacement = _replacementHistoryReloadPending.Remove(threadId);
1649+
rerunAuthoritative = !rerunReplacement
1650+
&& _authoritativeHistoryReloadPending.Remove(threadId);
15921651
}
15931652
else
15941653
{
1595-
// Generation advance owns clearing pending authoritative state.
1654+
// Generation advance owns clearing pending reload state.
1655+
rerunReplacement = false;
15961656
rerunAuthoritative = false;
15971657
}
15981658
}
15991659
_telemetry.FinishHistoryLoad(historyOperation, historyOutcome, historyException);
1600-
if (rerunAuthoritative)
1660+
if (rerunReplacement)
1661+
{
1662+
_ = LoadHistoryCoreAsync(
1663+
threadId,
1664+
force: true,
1665+
CancellationToken.None,
1666+
expectedConnectionVersion: requestConnectionVersion,
1667+
authoritative: false,
1668+
replacementReload: true);
1669+
}
1670+
else if (rerunAuthoritative)
16011671
_ = LoadHistoryAsync(threadId, force: true, authoritative: true);
16021672
}
16031673
}
@@ -1660,15 +1730,19 @@ void Deliver()
16601730

16611731
private void RaiseHistoryNotificationIfCurrent(
16621732
ChatProviderNotification notification,
1663-
long requestConnectionVersion)
1733+
string threadId,
1734+
long requestConnectionVersion,
1735+
long requestHistoryReplacementVersion)
16641736
{
16651737
var args = new ChatProviderNotificationEventArgs(notification);
16661738

16671739
void Deliver()
16681740
{
16691741
lock (_gate)
16701742
{
1671-
if (_disposed || _historyConnectionVersion != requestConnectionVersion)
1743+
if (_disposed ||
1744+
_historyConnectionVersion != requestConnectionVersion ||
1745+
GetHistoryReplacementVersionLocked(threadId) != requestHistoryReplacementVersion)
16721746
return;
16731747
}
16741748

@@ -2175,6 +2249,7 @@ private CancellationTokenSource AdvanceHistoryGenerationLocked(bool clearLoaded)
21752249
_historyInFlight.Clear();
21762250
_historyRetryCount.Clear();
21772251
_authoritativeHistoryReloadPending.Clear();
2252+
_replacementHistoryReloadPending.Clear();
21782253
if (clearLoaded)
21792254
_historyLoaded.Clear();
21802255
return previousCancellation;
@@ -5216,6 +5291,9 @@ private readonly record struct ResetClearPersistence(
52165291
private long GetResetVersionLocked(string threadId) =>
52175292
_resetVersions.TryGetValue(threadId, out var version) ? version : 0;
52185293

5294+
private long GetHistoryReplacementVersionLocked(string threadId) =>
5295+
_historyReplacementVersions.TryGetValue(threadId, out var version) ? version : 0;
5296+
52195297
private long GetHistoryRevisionLocked(string threadId) =>
52205298
_historyRevisions.TryGetValue(threadId, out var revision) ? revision : 0;
52215299

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public sealed record OpenClawReactorChatRootProps(
3030
Func<CancellationToken, Action?, Task<string?>>? OnVoiceRequest = null,
3131
Action? OnAttachClick = null,
3232
Action? OnSettingsClick = null,
33+
Action<string>? OnOpenCheckpoints = null,
3334
Action<bool>? OnSpeakerMuteChanged = null,
3435
Func<string, string?, Task<bool>>? ConfirmResetAsync = null,
3536
bool InitialMuted = false,
@@ -319,6 +320,7 @@ void SelectThread(string threadId)
319320
timelineProps,
320321
onSuggestionPicked,
321322
firstSendInFlight,
323+
OnOpenCheckpoints: props.OnOpenCheckpoints,
322324
HistoryRevision: historyRevision));
323325
var composerElement = effectiveThread is null
324326
? Empty()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static MountedReactorChat MountReactorChat(
3636
Func<CancellationToken, Action?, Task<string?>>? onVoiceRequest = null,
3737
Action? onAttachClick = null,
3838
Action? onSettingsClick = null,
39+
Action<string>? onOpenCheckpoints = null,
3940
Action<bool>? onSpeakerMuteChanged = null,
4041
bool initialMuted = false,
4142
bool isCompact = false)
@@ -81,6 +82,7 @@ async Task<bool> ConfirmResetAsync(string sessionKey, string? displayName)
8182
onVoiceRequest,
8283
onAttachClick,
8384
onSettingsClick,
85+
onOpenCheckpoints,
8486
onSpeakerMuteChanged,
8587
ConfirmResetAsync,
8688
initialMuted,

0 commit comments

Comments
 (0)