perf(runtime): drop dead resultIDs map and mid-loop tool-result injection (P2)

This commit is contained in:
sausheong committed 2026-06-13 09:55:58 +08:00
1 parent 86ad622144
commit 7151562d99
2 files changed
+27 -14

No files matched your search

-14
View File
@@ -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,
+27
View File
@@ -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")
}