@@ -7326,6 +7326,97 @@ public async Task LoadHistoryAsync_StructuredToolBlocks_CorrelatesInputAndOutput
73267326 Assert.Equal("/workspace", entry.ToolOutput);
73277327 }
73287328
7329+ [Fact]
7330+ public async Task LoadHistoryAsync_StringEncodedToolArguments_PreservesSafeInput()
7331+ {
7332+ var (bridge, provider, snapshots, _) = CreateProvider(new[] { MainSession() });
7333+ bridge.HistoryBehavior = _ => Task.FromResult(new ChatHistoryInfo
7334+ {
7335+ SessionKey = "main",
7336+ Messages =
7337+ [
7338+ new ChatMessageInfo
7339+ {
7340+ Role = "assistant",
7341+ State = "final",
7342+ Ts = 1,
7343+ ToolContent =
7344+ [
7345+ new ChatToolContentInfo
7346+ {
7347+ Kind = ChatToolContentKind.Call,
7348+ CallId = "call-1",
7349+ ToolName = "exec",
7350+ Args = JsonSerializer.SerializeToElement(
7351+ """{"command":"pwd","workdir":"/workspace"}"""),
7352+ },
7353+ ],
7354+ },
7355+ ],
7356+ });
7357+
7358+ await provider.LoadHistoryAsync("main");
7359+
7360+ var entry = Assert.Single(snapshots[^1].Timelines["main"].Entries);
7361+ Assert.Equal("pwd", entry.ToolArgs?["command"]?.GetValue<string>());
7362+ Assert.False(entry.ToolArgs?.ContainsKey("workdir"));
7363+ }
7364+
7365+ [Fact]
7366+ public async Task LoadHistoryAsync_AfterRestart_ReplaysSanitizedArgumentsWithoutLocalPersistence()
7367+ {
7368+ using var tempDir = new TempDirectory();
7369+ var cachePath = Path.Combine(tempDir.DirectoryPath, "tool-metadata.json");
7370+ static ChatHistoryInfo History() => new()
7371+ {
7372+ SessionKey = "main",
7373+ Messages =
7374+ [
7375+ new ChatMessageInfo
7376+ {
7377+ Role = "assistant",
7378+ State = "final",
7379+ Ts = 1,
7380+ ToolContent =
7381+ [
7382+ new ChatToolContentInfo
7383+ {
7384+ Kind = ChatToolContentKind.Call,
7385+ CallId = "call-1",
7386+ ToolName = "exec",
7387+ Args = JsonSerializer.SerializeToElement(
7388+ """{"command":"curl https://example.test --token abcdef1234567890ghij","workdir":"C:\\private"}"""),
7389+ },
7390+ ],
7391+ },
7392+ ],
7393+ };
7394+
7395+ var first = CreateProvider(
7396+ new[] { MainSession() },
7397+ toolMetaCachePath: cachePath);
7398+ first.bridge.HistoryBehavior = _ => Task.FromResult(History());
7399+ await first.provider.LoadHistoryAsync("main");
7400+ await first.provider.DisposeAsync();
7401+
7402+ var second = CreateProvider(
7403+ new[] { MainSession() },
7404+ toolMetaCachePath: cachePath);
7405+ second.bridge.HistoryBehavior = _ => Task.FromResult(History());
7406+ await second.provider.LoadHistoryAsync("main");
7407+
7408+ var entry = Assert.Single(second.snapshots[^1].Timelines["main"].Entries);
7409+ var command = entry.ToolArgs?["command"]?.GetValue<string>();
7410+ Assert.NotNull(command);
7411+ Assert.DoesNotContain(
7412+ "abcdef1234567890ghij",
7413+ command,
7414+ StringComparison.Ordinal);
7415+ Assert.False(entry.ToolArgs?.ContainsKey("workdir"));
7416+ Assert.False(File.Exists(cachePath));
7417+ await second.provider.DisposeAsync();
7418+ }
7419+
73297420 [Theory]
73307421 [InlineData("toolResult")]
73317422 [InlineData("tool_result")]
0 commit comments