From 8f8d75cd06f3427b2cd68cdeef1345899f3f83a2 Mon Sep 17 00:00:00 2001 From: sausheong Date: Tue, 9 Jun 2026 23:39:26 +0800 Subject: [PATCH] feat(runtime): KG ingest on RunTurn exchange completion --- runtime/runturn.go | 7 ++++++ runtime/runturn_kg_test.go | 51 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/runtime/runturn.go b/runtime/runturn.go index 8ea4165..41ee493 100644 --- a/runtime/runturn.go +++ b/runtime/runturn.go @@ -137,6 +137,13 @@ func (r *Runtime) RunTurn(ctx context.Context, userMsg string, images []llm.Imag } if len(toolCalls) == 0 { + // Exchange complete: ingest the full thread once. Background + best-effort + // (the KG spawns its own bounded goroutine), so it never delays the turn. + // context.Background() because the request ctx may be cancelling and the + // ingest goroutine deliberately outlives the turn. + if r.KG != nil { + r.KG.Ingest(context.Background(), sessionThread(r.Session.View())) + } emit(AgentEvent{Type: EventDone, Usage: lastUsage}) return r.turnSlice(startLen, true, "completed", lastUsage, nil), nil } diff --git a/runtime/runturn_kg_test.go b/runtime/runturn_kg_test.go index 6421a74..ffcdf65 100644 --- a/runtime/runturn_kg_test.go +++ b/runtime/runturn_kg_test.go @@ -163,3 +163,54 @@ func TestRunTurn_RecallTimeoutInjectsNothing(t *testing.T) { require.Less(t, time.Since(start), 1500*time.Millisecond, "recall must be bounded ~800ms") require.Len(t, captured, 1, "timed-out recall injects nothing") } + +func TestRunTurn_IngestOnCompletion(t *testing.T) { + llmProvider := &scriptedStreamLLM{events: []scriptedStreamEvent{ + {typ: llm.EventTextDelta, text: "done"}, + {typ: llm.EventDone}, + }} + kg := &fakeKG{} + rt := &Runtime{ + LLM: llmProvider, Tools: newEchoRegistry(), Session: session.NewSession("a", "k"), + AgentID: "a", Model: "test", StaticSystemPrompt: "S", KG: kg, + } + res, err := rt.RunTurn(context.Background(), "remember I like tea", nil, nil) + require.NoError(t, err) + require.True(t, res.Done) + require.Equal(t, 1, kg.ingestCalls, "ingest fires once on the completing round") + require.NotEmpty(t, kg.ingestThreads[0], "ingest gets a non-empty thread") + require.Equal(t, "user", kg.ingestThreads[0][0].Role) + require.Equal(t, "remember I like tea", kg.ingestThreads[0][0].Content) +} + +func TestRunTurn_NoIngestOnToolRound(t *testing.T) { + tc := &llm.ToolCall{ID: "tc_1", Name: "echo", Input: json.RawMessage(`{"x":1}`)} + llmProvider := &scriptedStreamLLM{events: []scriptedStreamEvent{ + {typ: llm.EventToolCallStart, toolCall: tc}, + {typ: llm.EventToolCallDone, toolCall: tc}, + {typ: llm.EventDone}, + }} + kg := &fakeKG{} + rt := &Runtime{ + LLM: llmProvider, Tools: newEchoRegistry(), Session: session.NewSession("a", "k"), + AgentID: "a", Model: "test", StaticSystemPrompt: "S", KG: kg, + } + res, err := rt.RunTurn(context.Background(), "use echo", nil, nil) + require.NoError(t, err) + require.False(t, res.Done, "tool round is not terminal") + require.Equal(t, 0, kg.ingestCalls, "no ingest on a round that produced tool calls") +} + +func TestRunTurn_NoIngestWhenKGNil(t *testing.T) { + llmProvider := &scriptedStreamLLM{events: []scriptedStreamEvent{ + {typ: llm.EventTextDelta, text: "done"}, + {typ: llm.EventDone}, + }} + rt := &Runtime{ + LLM: llmProvider, Tools: newEchoRegistry(), Session: session.NewSession("a", "k"), + AgentID: "a", Model: "test", StaticSystemPrompt: "S", KG: nil, + } + res, err := rt.RunTurn(context.Background(), "hi", nil, nil) + require.NoError(t, err) + require.True(t, res.Done) +}