diff --git a/runtime/runturn.go b/runtime/runturn.go
index 9fa7fe4..3981d24 100644
--- a/runtime/runturn.go
+++ b/runtime/runturn.go
@@ -3,6 +3,7 @@ package runtime
import (
"context"
"encoding/base64"
+ "encoding/json"
"fmt"
"sort"
"strings"
@@ -153,3 +154,46 @@ func (r *Runtime) turnSlice(startLen int, done bool, reason string, usage *llm.U
}
return TurnResult{Done: done, StopReason: reason, Entries: delta, Usage: usage, Err: err}
}
+
+// sessionThread converts session history into the minimal []Message the
+// KnowledgeGraph ingests, mirroring how Run accumulates its thread: user and
+// assistant messages by role+text, tool calls as "[tool: name]\n"
+// (assistant), tool results as their output or "[error] " (user).
+// Compaction/meta and non-user/assistant message entries are skipped — the
+// thread carries only conversation turns and tool exchanges.
+func sessionThread(history []session.SessionEntry) []Message {
+ var thread []Message
+ for _, e := range history {
+ switch e.Type {
+ case session.EntryTypeMessage:
+ if e.Role != "user" && e.Role != "assistant" {
+ continue // skip system/summary messages
+ }
+ var d session.MessageData
+ if json.Unmarshal(e.Data, &d) != nil {
+ continue
+ }
+ thread = append(thread, Message{Role: e.Role, Content: d.Text})
+ case session.EntryTypeToolCall:
+ var d session.ToolCallData
+ if json.Unmarshal(e.Data, &d) != nil {
+ continue
+ }
+ thread = append(thread, Message{
+ Role: "assistant",
+ Content: fmt.Sprintf("[tool: %s]\n%s", d.Tool, string(d.Input)),
+ })
+ case session.EntryTypeToolResult:
+ var d session.ToolResultData
+ if json.Unmarshal(e.Data, &d) != nil {
+ continue
+ }
+ content := d.Output
+ if d.Error != "" {
+ content = "[error] " + d.Error
+ }
+ thread = append(thread, Message{Role: "user", Content: content})
+ }
+ }
+ return thread
+}
diff --git a/runtime/runturn_kg_test.go b/runtime/runturn_kg_test.go
new file mode 100644
index 0000000..5725f02
--- /dev/null
+++ b/runtime/runturn_kg_test.go
@@ -0,0 +1,44 @@
+package runtime
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/sausheong/harness/session"
+ "github.com/stretchr/testify/require"
+)
+
+func TestSessionThread_RendersEntries(t *testing.T) {
+ sess := session.NewSession("a", "k")
+ sess.Append(session.UserMessageEntry("hello"))
+ sess.Append(session.AssistantMessageEntry("hi there"))
+ sess.Append(session.ToolCallEntry("tc1", "echo", json.RawMessage(`{"x":1}`)))
+ sess.Append(session.ToolResultEntry("tc1", "echoed", "", nil))
+ sess.Append(session.ToolResultEntry("tc2", "", "boom", nil))
+
+ thread := sessionThread(sess.View())
+
+ require.Equal(t, []Message{
+ {Role: "user", Content: "hello"},
+ {Role: "assistant", Content: "hi there"},
+ {Role: "assistant", Content: "[tool: echo]\n{\"x\":1}"},
+ {Role: "user", Content: "echoed"},
+ {Role: "user", Content: "[error] boom"},
+ }, thread)
+}
+
+func TestSessionThread_SkipsCompaction(t *testing.T) {
+ sess := session.NewSession("a", "k")
+ sess.Append(session.UserMessageEntry("hello"))
+ sess.Append(session.AssistantMessageEntry("hi"))
+ // Build the expected from whatever View() returns, minus any compaction/meta.
+ got := sessionThread(sess.View())
+ require.Equal(t, []Message{
+ {Role: "user", Content: "hello"},
+ {Role: "assistant", Content: "hi"},
+ }, got)
+}
+
+func TestSessionThread_Empty(t *testing.T) {
+ require.Nil(t, sessionThread(nil))
+}