@@ -44,3 +44,53 @@ func TestBuildContextSnapshotIncludesModelProviderAndThinking(t *testing.T) {
4444 t .Fatalf ("messages len = %d, want 1" , len (snapshot .Messages ))
4545 }
4646}
47+
48+ func TestBuildSnapshotRepairsMissingToolResult (t * testing.T ) {
49+ t .Parallel ()
50+
51+ dir := t .TempDir ()
52+ store , err := create (dir , "/workspace/project" )
53+ if err != nil {
54+ t .Fatalf ("create store: %v" , err )
55+ }
56+ defer store .Close ()
57+
58+ assistant := agentcore.Message {
59+ Role : agentcore .RoleAssistant ,
60+ Content : []agentcore.ContentBlock {
61+ agentcore .ToolCallBlock (agentcore.ToolCall {
62+ ID : "toolu_123" ,
63+ Name : "read" ,
64+ Args : []byte (`{"path":"README.md"}` ),
65+ }),
66+ },
67+ }
68+ if err := store .AppendMessage (assistant ); err != nil {
69+ t .Fatalf ("append assistant message: %v" , err )
70+ }
71+ if err := store .AppendMessage (agentcore .UserMsg ("继续" )); err != nil {
72+ t .Fatalf ("append user message: %v" , err )
73+ }
74+
75+ snapshot , err := store .BuildSnapshot ()
76+ if err != nil {
77+ t .Fatalf ("build context: %v" , err )
78+ }
79+ if len (snapshot .Messages ) != 3 {
80+ t .Fatalf ("messages len = %d, want 3" , len (snapshot .Messages ))
81+ }
82+
83+ toolMsg , ok := snapshot .Messages [1 ].(agentcore.Message )
84+ if ! ok {
85+ t .Fatalf ("snapshot message[1] type = %T, want agentcore.Message" , snapshot .Messages [1 ])
86+ }
87+ if toolMsg .Role != agentcore .RoleTool {
88+ t .Fatalf ("message[1] role = %s, want %s" , toolMsg .Role , agentcore .RoleTool )
89+ }
90+ if got := toolMsg .Metadata ["tool_call_id" ]; got != "toolu_123" {
91+ t .Fatalf ("tool_call_id = %v, want toolu_123" , got )
92+ }
93+ if got := toolMsg .Metadata ["is_error" ]; got != true {
94+ t .Fatalf ("is_error = %v, want true" , got )
95+ }
96+ }
0 commit comments