diff --git a/runtime/context.go b/runtime/context.go index b2acad5..d357613 100644 --- a/runtime/context.go +++ b/runtime/context.go @@ -168,17 +168,6 @@ func buildDynamicSystemPromptSuffix(dateLine, cortexContext string) string { // corresponding tool_result in the next user message. Orphaned tool calls // (e.g. from interrupted sessions) get synthetic error results injected. func assembleMessages(history []session.SessionEntry) []llm.Message { - // First pass: collect tool result IDs so we can detect orphaned tool calls. - resultIDs := make(map[string]bool) - for _, entry := range history { - if entry.Type == session.EntryTypeToolResult { - var tr session.ToolResultData - if err := json.Unmarshal(entry.Data, &tr); err == nil { - resultIDs[tr.ToolCallID] = true - } - } - } - var msgs []llm.Message for _, entry := range history { @@ -219,9 +208,6 @@ func assembleMessages(history []session.SessionEntry) []llm.Message { if err := json.Unmarshal(entry.Data, &md); err != nil { continue } - // Before appending a new message, check if the last assistant - // message has orphaned tool calls that need synthetic results. - msgs = injectMissingToolResults(msgs) msg := llm.Message{ Role: entry.Role, Content: md.Text, diff --git a/runtime/context_test.go b/runtime/context_test.go index 84ad8ab..8adba21 100644 --- a/runtime/context_test.go +++ b/runtime/context_test.go @@ -1,12 +1,14 @@ package runtime import ( + jsonpkg "encoding/json" "os" "path/filepath" "strings" "testing" "time" + "github.com/sausheong/harness/session" "github.com/stretchr/testify/require" ) @@ -123,3 +125,28 @@ func TestFormatDateLine(t *testing.T) { }) } } + +func mustJSON(t *testing.T, v any) jsonpkg.RawMessage { + t.Helper() + b, err := jsonpkg.Marshal(v) + require.NoError(t, err) + return b +} + +func TestAssembleMessagesInjectsOrphanedToolResult(t *testing.T) { + history := []session.SessionEntry{ + {Type: session.EntryTypeMessage, Role: "user", Data: mustJSON(t, session.MessageData{Text: "hi"})}, + {Type: session.EntryTypeToolCall, Role: "assistant", Data: mustJSON(t, session.ToolCallData{ + ID: "call_1", Tool: "bash", Input: jsonpkg.RawMessage(`{}`), + })}, + {Type: session.EntryTypeMessage, Role: "user", Data: mustJSON(t, session.MessageData{Text: "still there?"})}, + } + msgs := assembleMessages(history) + var found bool + for _, m := range msgs { + if m.ToolCallID == "call_1" { + found = true + } + } + require.True(t, found, "orphaned tool call must get a synthetic tool result from the terminal pass") +}