perf(runtime): drop dead resultIDs map and mid-loop tool-result injection (P2)
This commit is contained in:
1 parent
86ad622144
commit
7151562d99
2 files changed
+27
-14
No files matched your search
@@ -168,17 +168,6 @@ func buildDynamicSystemPromptSuffix(dateLine, cortexContext string) string {
|
|||||||
// corresponding tool_result in the next user message. Orphaned tool calls
|
// corresponding tool_result in the next user message. Orphaned tool calls
|
||||||
// (e.g. from interrupted sessions) get synthetic error results injected.
|
// (e.g. from interrupted sessions) get synthetic error results injected.
|
||||||
func assembleMessages(history []session.SessionEntry) []llm.Message {
|
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
|
var msgs []llm.Message
|
||||||
|
|
||||||
for _, entry := range history {
|
for _, entry := range history {
|
||||||
@@ -219,9 +208,6 @@ func assembleMessages(history []session.SessionEntry) []llm.Message {
|
|||||||
if err := json.Unmarshal(entry.Data, &md); err != nil {
|
if err := json.Unmarshal(entry.Data, &md); err != nil {
|
||||||
continue
|
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{
|
msg := llm.Message{
|
||||||
Role: entry.Role,
|
Role: entry.Role,
|
||||||
Content: md.Text,
|
Content: md.Text,
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
package runtime
|
package runtime
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
jsonpkg "encoding/json"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/sausheong/harness/session"
|
||||||
"github.com/stretchr/testify/require"
|
"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")
|
||||||
|
}
|
||||||
Reference in new issue
Block a user