@@ -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
0 commit comments