From f35070594f82aa7a1a2a198c08ff355ccc4d2ea2 Mon Sep 17 00:00:00 2001 From: sausheong Date: Tue, 9 Jun 2026 23:32:30 +0800 Subject: [PATCH] test(runtime): strengthen sessionThread compaction-skip + undecodable coverage --- runtime/runturn.go | 2 ++ runtime/runturn_kg_test.go | 24 ++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/runtime/runturn.go b/runtime/runturn.go index 3981d24..9b4e66d 100644 --- a/runtime/runturn.go +++ b/runtime/runturn.go @@ -161,6 +161,8 @@ func (r *Runtime) turnSlice(startLen int, done bool, reason string, usage *llm.U // (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. +// On the runtime's per-turn serve path each call sees one turn's fresh session, +// so a compaction summary's absence here is intentional, not a gap. func sessionThread(history []session.SessionEntry) []Message { var thread []Message for _, e := range history { diff --git a/runtime/runturn_kg_test.go b/runtime/runturn_kg_test.go index 5725f02..bec0cd5 100644 --- a/runtime/runturn_kg_test.go +++ b/runtime/runturn_kg_test.go @@ -29,16 +29,36 @@ func TestSessionThread_RendersEntries(t *testing.T) { func TestSessionThread_SkipsCompaction(t *testing.T) { sess := session.NewSession("a", "k") + // Compaction is appended FIRST so the entries that follow remain on the + // View() walk-back path. View() stops its back-walk at the most recent + // compaction (inclusive), so appending user/assistant AFTER it yields + // View() == [compaction, user, assistant] — the compaction is genuinely + // present in the slice handed to sessionThread, exercising the skip path. + // (Appending compaction last would make it the leaf, and View() would + // return only [compaction], never reaching the messages.) + sess.Append(session.CompactionEntry("summary", "", "", "m", 0, 0, 0)) 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()) + + view := sess.View() + require.Equal(t, session.EntryTypeCompaction, view[0].Type, "View() must include the compaction entry to exercise the skip") + + got := sessionThread(view) require.Equal(t, []Message{ {Role: "user", Content: "hello"}, {Role: "assistant", Content: "hi"}, }, got) } +func TestSessionThread_SkipsUndecodableEntry(t *testing.T) { + history := []session.SessionEntry{ + {Type: session.EntryTypeToolCall, Data: []byte("not json")}, + session.UserMessageEntry("after"), + } + got := sessionThread(history) + require.Equal(t, []Message{{Role: "user", Content: "after"}}, got) +} + func TestSessionThread_Empty(t *testing.T) { require.Nil(t, sessionThread(nil)) }