Skip to content

Commit 72f81b7

Browse files
bkudiessCopilot
andcommitted
fix(chat): match structured history cache by call ID
Co-authored-by: Copilot App <[email protected]>
1 parent 338f675 commit 72f81b7

4 files changed

Lines changed: 368 additions & 32 deletions

File tree

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ internal static (OpenClawChatDataProvider Provider, Bridge GatewayBridge) Create
2727
return CreateCore(isolatedDataDirectory, post);
2828
}
2929

30+
internal static OpenClawChatDataProvider CreateForTesting(
31+
string isolatedDataDirectory,
32+
Func<string, string?> environmentLookup)
33+
{
34+
ValidateIsolationGate(isolatedDataDirectory, environmentLookup);
35+
return CreateCore(isolatedDataDirectory, post: null).Provider;
36+
}
37+
38+
internal static (OpenClawChatDataProvider Provider, Bridge GatewayBridge) CreateWithBridgeForTesting(
39+
string isolatedDataDirectory,
40+
Func<string, string?> environmentLookup)
41+
{
42+
ValidateIsolationGate(isolatedDataDirectory, environmentLookup);
43+
return CreateCore(isolatedDataDirectory, post: null);
44+
}
45+
3046
private static (OpenClawChatDataProvider Provider, Bridge GatewayBridge) CreateCore(
3147
string isolatedDataDirectory,
3248
Action<Action>? post)
@@ -75,15 +91,22 @@ private static (OpenClawChatDataProvider Provider, Bridge GatewayBridge) CreateC
7591
}
7692

7793
private static void ValidateIsolationGate(string isolatedDataDirectory)
94+
=> ValidateIsolationGate(
95+
isolatedDataDirectory,
96+
Environment.GetEnvironmentVariable);
97+
98+
private static void ValidateIsolationGate(
99+
string isolatedDataDirectory,
100+
Func<string, string?> environmentLookup)
78101
{
79102
var configuredDataDirectory =
80-
Environment.GetEnvironmentVariable("OPENCLAW_TRAY_DATA_DIR");
103+
environmentLookup("OPENCLAW_TRAY_DATA_DIR");
81104
if (!string.Equals(
82-
Environment.GetEnvironmentVariable("OPENCLAW_ACCESSIBILITY_TEST_CHAT"),
105+
environmentLookup("OPENCLAW_ACCESSIBILITY_TEST_CHAT"),
83106
"1",
84107
StringComparison.Ordinal)
85108
|| !string.Equals(
86-
Environment.GetEnvironmentVariable("OPENCLAW_ACCESSIBILITY_TEST_CHAT_FIXTURE"),
109+
environmentLookup("OPENCLAW_ACCESSIBILITY_TEST_CHAT_FIXTURE"),
87110
FixtureName,
88111
StringComparison.Ordinal)
89112
|| string.IsNullOrWhiteSpace(configuredDataDirectory)

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

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,14 +1438,18 @@ string AllocateSyntheticToolCallId()
14381438
{
14391439
if (toolBlock.Kind == ChatToolContentKind.Call)
14401440
{
1441-
_ = TryMatchCachedTool(cachedTools, msg.Ts);
14421441
var args = ConvertToolArgs(toolBlock.Args);
14431442
var callId = toolBlock.CallId;
14441443
if (string.IsNullOrWhiteSpace(callId))
14451444
{
1445+
_ = TryMatchCachedTool(cachedTools, msg.Ts);
14461446
callId = AllocateSyntheticToolCallId();
14471447
pendingUnkeyedToolCalls.Enqueue(callId);
14481448
}
1449+
else
1450+
{
1451+
_ = TryMatchCachedToolByCallId(cachedTools, callId);
1452+
}
14491453
rebuilt = ApplyAndCaptureMeta(
14501454
rebuilt,
14511455
new ChatToolStartEvent(
@@ -1458,36 +1462,40 @@ string AllocateSyntheticToolCallId()
14581462
else
14591463
{
14601464
var callId = toolBlock.CallId;
1461-
if (string.IsNullOrWhiteSpace(callId))
1465+
var hasVerifiedCallId = !string.IsNullOrWhiteSpace(callId);
1466+
if (!hasVerifiedCallId)
14621467
{
14631468
callId = pendingUnkeyedToolCalls.Count > 0
14641469
? pendingUnkeyedToolCalls.Dequeue()
14651470
: AllocateSyntheticToolCallId();
14661471
}
1472+
var resolvedCallId = callId!;
14671473

14681474
var correlationKey = new ChatToolCorrelationKey(
14691475
RunId: null,
14701476
LegacyTurn: rebuilt.ToolLegacyTurn,
1471-
ToolCallId: callId);
1477+
ToolCallId: resolvedCallId);
14721478
if (!rebuilt.ActiveToolCalls.ContainsKey(correlationKey))
14731479
{
1474-
var cached = TryMatchCachedTool(cachedTools, msg.Ts);
1480+
var cached = hasVerifiedCallId
1481+
? TryMatchCachedToolByCallId(cachedTools, resolvedCallId)
1482+
: TryMatchCachedTool(cachedTools, msg.Ts);
14751483
var toolName = cached?.ToolName ?? toolBlock.ToolName;
14761484
rebuilt = ApplyAndCaptureMeta(
14771485
rebuilt,
14781486
new ChatToolStartEvent(
14791487
cached?.Label ?? toolName,
14801488
toolName,
1481-
ToolCallId: callId),
1489+
ToolCallId: resolvedCallId),
14821490
msgMeta);
14831491
}
14841492

14851493
var output = NativeToolProjector.TruncateToolOutput(toolBlock.Text ?? string.Empty);
14861494
rebuilt = ApplyAndCaptureMeta(
14871495
rebuilt,
14881496
toolBlock.IsError
1489-
? new ChatToolErrorEvent(output, callId)
1490-
: new ChatToolOutputEvent(output, callId),
1497+
? new ChatToolErrorEvent(output, resolvedCallId)
1498+
: new ChatToolOutputEvent(output, resolvedCallId),
14911499
msgMeta);
14921500
}
14931501
}
@@ -7332,16 +7340,37 @@ private static void MergeCachedToolMeta(CachedToolMeta existing, CachedToolMeta
73327340
if (historyTsMs > 0 && candidate.Ts > 0 && candidate.Ts > historyTsMs + 300_000)
73337341
return null; // cached entry is >5 min after this history entry — not a match
73347342

7335-
var match = cache.Dequeue();
7336-
var identity = NativeToolProjector.CanonicalizeToolIdentity(
7337-
NormalizeCachedDisplayText(match.ToolName),
7338-
match.IdentityStrength);
7339-
match.ToolName = identity.Name;
7340-
match.IdentityStrength = identity.Strength;
7341-
match.Label = NativeToolProjector.SanitizeToolDisplayValue(
7342-
NormalizeCachedDisplayText(match.Label));
7343-
match.ToolArgs = NormalizeCachedToolArgs(match.ToolArgs);
7344-
return match;
7343+
return CloneCachedToolMeta(cache.Dequeue());
7344+
}
7345+
7346+
/// <summary>
7347+
/// Consume the earliest cached entry with the exact structured call ID while
7348+
/// preserving the relative order of every unmatched entry.
7349+
/// </summary>
7350+
internal static CachedToolMeta? TryMatchCachedToolByCallId(
7351+
Queue<CachedToolMeta>? cache,
7352+
string? toolCallId)
7353+
{
7354+
if (cache is null || cache.Count == 0 || string.IsNullOrWhiteSpace(toolCallId))
7355+
return null;
7356+
7357+
CachedToolMeta? match = null;
7358+
var entryCount = cache.Count;
7359+
for (var index = 0; index < entryCount; index++)
7360+
{
7361+
var candidate = cache.Dequeue();
7362+
if (match is null
7363+
&& !string.IsNullOrWhiteSpace(candidate.ToolCallId)
7364+
&& string.Equals(candidate.ToolCallId, toolCallId, StringComparison.Ordinal))
7365+
{
7366+
match = candidate;
7367+
continue;
7368+
}
7369+
7370+
cache.Enqueue(candidate);
7371+
}
7372+
7373+
return match is null ? null : CloneCachedToolMeta(match);
73457374
}
73467375

73477376
private static JsonObject? NormalizeCachedToolArgs(JsonObject? args)

0 commit comments

Comments
 (0)