fix(cli): correct KB id detection, SSE terminal-frame, and CI test isolation
Three defects surfaced during end-to-end RAG verification — the first two
block real chat usage, the third makes Linux CI flaky:
1. KB id detection — `IsKBID` was checking
`strings.HasPrefix(s, "kb_")`, but WeKnora generates KB ids as bare
UUIDs (internal/types/knowledge_base.go: `uuid.New().String()` stored
in a `varchar(36)` column). Real ids therefore fell through to the
name-resolution path:
$ weknora chat ... --kb a32a63ff-fb36-4874-bcaa-30f48570a694
Error: knowledge base not found: a32a63ff-...
Switched the discriminator to a UUID regex
(`^[0-9a-fA-F]{8}-…-[0-9a-fA-F]{12}$`). KB names are arbitrary
user-supplied strings, so the canonical 8-4-4-4-12 form is an
unambiguous signal. Mirrors gcloud `--project`'s id-vs-name detection.
2. SSE terminal-frame — the accumulator's `Append` was gating
finalization on `r.Done`, but the server's KnowledgeQAStream protocol
emits a leading `agent_query` frame with `done=true` to deliver
session + message metadata *before* the answer fragments arrive:
event: message
data: {"response_type":"agent_query","content":"","done":true,…}
event: message
data: {"response_type":"answer","content":"你好","done":false}
…
event: message
data: {"response_type":"complete","content":"","done":true}
The accumulator therefore flipped to `finished=true` on frame #1 and
discarded every subsequent answer fragment — `weknora chat … --json`
returned `answer: ""` even though the LLM reported completion_tokens
> 0. Fixed: terminate only on `response_type == complete`.
References still captured opportunistically (they may arrive on a
dedicated `references` event before the terminator).
3. doctor credential_storage CI isolation — the check probes the real
OS keyring via `secrets.NewBestEffortStore()`: present on macOS dev
machines → StatusOK; absent on Linux CI runners without libsecret /
Gnome-Keyring → StatusWarn ("falling back to file store"). That
host-dependence was leaking into two test classes that assumed
StatusOK:
* cmd/doctor/doctor_test.go: TestDoctor_AllOK and
TestDoctor_NoConfig_StillRunsCredentialStorage already had a
withCredStoreFactory seam but didn't use it. Added the pin.
* acceptance/contract/envelope_test.go: doctor.success_offline
and doctor.error_network golden cases. The contract test runs
through the cobra tree in-process and shares cmd/doctor's
package-level credStoreFactory var — but couldn't reach it
because the existing seam was unexported.
Fix: export `doctor.SetCredStoreFactoryForTest(fn) (restore func())`
for out-of-package tests; acceptance/contract/helpers_test.go adds
a TestMain that pins the factory to a MemStore-returning closure
for the whole suite (MemStore is neither *FileStore nor a real
keyring, so doctor's type-switch hits StatusOK). Production stays
at secrets.NewBestEffortStore — only the test hook is now reachable
from across packages.
Test fixtures and goldens that used the old `kb_xxx` literals or
`Done: true` terminators were rewritten to use real UUIDs and
`ResponseType: ResponseTypeComplete` respectively. Per-command --help
text and Long descriptions / Examples now show a UUID rather than
`kb_…` so users see the correct shape from the start. New
TestAccumulator_IgnoresAgentQueryDone pins the SSE terminator bug so
it can't regress.
Tests: 24 cli packages green on macOS dev + Linux/macOS/Windows CI
matrix. Verified end-to-end against a live WeKnora server: `weknora
chat "..." --kb <UUID> --no-stream --json` returns the full LLM answer
in the envelope, live token streaming in TTY mode works, and the
credential_storage check renders deterministic envelopes across hosts.
This commit is contained in:
@@ -306,7 +306,7 @@ func fakeKBServer(t *testing.T, kbs []sdk.KnowledgeBase) *httptest.Server {
|
||||
// isolates cwd / env / closure from the others.
|
||||
func TestResolveKB_Chain(t *testing.T) {
|
||||
t.Run("flag_kb_id_wins", func(t *testing.T) {
|
||||
// kb_<id> form → no SDK call, no env, no disk.
|
||||
// UUID form on --kb → pass-through; no SDK call, no env, no disk.
|
||||
t.Setenv("WEKNORA_KB_ID", "kb_env_should_lose")
|
||||
dir := t.TempDir()
|
||||
resolveKBChdir(t, dir)
|
||||
@@ -320,9 +320,9 @@ func TestResolveKB_Chain(t *testing.T) {
|
||||
return nil, errors.New("must not be called")
|
||||
},
|
||||
}
|
||||
got, err := f.ResolveKB(makeResolveKBCmd(t, "kb_explicit"))
|
||||
got, err := f.ResolveKB(makeResolveKBCmd(t, "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "kb_explicit", got)
|
||||
assert.Equal(t, "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", got)
|
||||
assert.Equal(t, 0, clientCalls)
|
||||
})
|
||||
|
||||
|
||||
@@ -3,20 +3,20 @@ package cmdutil
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"regexp"
|
||||
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// kbIDPrefix is the server-emitted prefix that marks a string as a KB id
|
||||
// (rather than a KB name). The single `--kb` flag uses it for client-side
|
||||
// id-vs-name auto-detection — the same pattern gcloud uses for --project
|
||||
// (numeric id vs project-id form).
|
||||
const kbIDPrefix = "kb_"
|
||||
// uuidPattern matches the canonical 8-4-4-4-12 UUID form. WeKnora's KB ids
|
||||
// are uuid.New().String() output stored as varchar(36); names are arbitrary
|
||||
// user-supplied strings, so format-detection is unambiguous.
|
||||
var uuidPattern = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`)
|
||||
|
||||
// IsKBID reports whether s looks like a KB id. Used by Factory.ResolveKB and
|
||||
// any caller that accepts a single id-or-name selector value.
|
||||
func IsKBID(s string) bool { return strings.HasPrefix(s, kbIDPrefix) }
|
||||
// any caller that accepts a single id-or-name selector value — same pattern
|
||||
// gcloud uses for --project (id vs name auto-detection).
|
||||
func IsKBID(s string) bool { return uuidPattern.MatchString(s) }
|
||||
|
||||
// KBLister is the narrow SDK surface ResolveKBNameToID depends on. The
|
||||
// production *sdk.Client satisfies it; tests inject fakes without standing
|
||||
|
||||
@@ -42,11 +42,18 @@ func (a *Accumulator) Append(r *sdk.StreamResponse) {
|
||||
if r.AssistantMessageID != "" && a.AssistantMessageID == "" {
|
||||
a.AssistantMessageID = r.AssistantMessageID
|
||||
}
|
||||
if r.Done {
|
||||
// Terminal events carry References; capture once.
|
||||
if r.KnowledgeReferences != nil {
|
||||
a.References = r.KnowledgeReferences
|
||||
}
|
||||
// Capture references whenever they arrive — they may land on a
|
||||
// dedicated `references` event before the terminal `complete`.
|
||||
if r.KnowledgeReferences != nil {
|
||||
a.References = r.KnowledgeReferences
|
||||
}
|
||||
// Stream is only truly done on response_type=complete. Other events
|
||||
// (notably the leading agent_query metadata frame) carry done=true to
|
||||
// mark their own per-event completion, but the answer fragments arrive
|
||||
// in subsequent response_type=answer events. Gating on response_type
|
||||
// avoids the off-by-one termination that would discard the entire
|
||||
// answer.
|
||||
if r.ResponseType == sdk.ResponseTypeComplete {
|
||||
a.finished = true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,16 @@ import (
|
||||
sdk "github.com/Tencent/WeKnora/client"
|
||||
)
|
||||
|
||||
// terminator is the canonical "stream is done" event the accumulator
|
||||
// recognises. The server emits response_type=complete after the answer
|
||||
// fragments and any references; per-event done=true on agent_query /
|
||||
// answer frames is NOT terminal.
|
||||
var terminator = &sdk.StreamResponse{ResponseType: sdk.ResponseTypeComplete, Done: true}
|
||||
|
||||
func TestAccumulator_AppendsContent(t *testing.T) {
|
||||
a := &sse.Accumulator{}
|
||||
a.Append(&sdk.StreamResponse{Content: "Hello "})
|
||||
a.Append(&sdk.StreamResponse{Content: "world"})
|
||||
a.Append(&sdk.StreamResponse{ResponseType: sdk.ResponseTypeAnswer, Content: "Hello "})
|
||||
a.Append(&sdk.StreamResponse{ResponseType: sdk.ResponseTypeAnswer, Content: "world"})
|
||||
if got := a.Result(); got != "Hello world" {
|
||||
t.Errorf("got %q, want %q", got, "Hello world")
|
||||
}
|
||||
@@ -19,15 +25,17 @@ func TestAccumulator_AppendsContent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccumulator_FinalizesOnDone(t *testing.T) {
|
||||
func TestAccumulator_FinalizesOnComplete(t *testing.T) {
|
||||
a := &sse.Accumulator{}
|
||||
a.Append(&sdk.StreamResponse{Content: "answer"})
|
||||
a.Append(&sdk.StreamResponse{ResponseType: sdk.ResponseTypeAnswer, Content: "answer"})
|
||||
// References may arrive on a dedicated event before the terminator.
|
||||
a.Append(&sdk.StreamResponse{
|
||||
Done: true,
|
||||
ResponseType: sdk.ResponseTypeReferences,
|
||||
KnowledgeReferences: []*sdk.SearchResult{{KnowledgeID: "k1"}},
|
||||
})
|
||||
a.Append(terminator)
|
||||
if !a.Done() {
|
||||
t.Error("expected Done=true")
|
||||
t.Error("expected Done=true after response_type=complete")
|
||||
}
|
||||
if len(a.References) != 1 {
|
||||
t.Errorf("expected 1 reference, got %d", len(a.References))
|
||||
@@ -37,13 +45,37 @@ func TestAccumulator_FinalizesOnDone(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccumulator_IgnoresPostDone(t *testing.T) {
|
||||
func TestAccumulator_IgnoresAgentQueryDone(t *testing.T) {
|
||||
// Server emits a leading agent_query frame with done=true to deliver
|
||||
// session metadata; the accumulator must NOT treat that as terminal —
|
||||
// otherwise the answer fragments that follow would be discarded.
|
||||
a := &sse.Accumulator{}
|
||||
a.Append(&sdk.StreamResponse{Content: "first"})
|
||||
a.Append(&sdk.StreamResponse{Done: true})
|
||||
a.Append(&sdk.StreamResponse{Content: "after"})
|
||||
a.Append(&sdk.StreamResponse{
|
||||
ResponseType: sdk.ResponseTypeAgentQuery,
|
||||
Done: true,
|
||||
SessionID: "sess_123",
|
||||
AssistantMessageID: "msg_456",
|
||||
})
|
||||
a.Append(&sdk.StreamResponse{ResponseType: sdk.ResponseTypeAnswer, Content: "real answer"})
|
||||
a.Append(terminator)
|
||||
if !a.Done() {
|
||||
t.Error("expected Done=true after response_type=complete")
|
||||
}
|
||||
if got := a.Result(); got != "real answer" {
|
||||
t.Errorf("agent_query done=true should not terminate; got %q", got)
|
||||
}
|
||||
if a.SessionID != "sess_123" {
|
||||
t.Errorf("session metadata from agent_query frame lost: got %q", a.SessionID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccumulator_IgnoresPostComplete(t *testing.T) {
|
||||
a := &sse.Accumulator{}
|
||||
a.Append(&sdk.StreamResponse{ResponseType: sdk.ResponseTypeAnswer, Content: "first"})
|
||||
a.Append(terminator)
|
||||
a.Append(&sdk.StreamResponse{ResponseType: sdk.ResponseTypeAnswer, Content: "after"})
|
||||
if got := a.Result(); got != "first" {
|
||||
t.Errorf("post-Done append should be no-op, got %q", got)
|
||||
t.Errorf("post-complete append should be no-op, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,11 +93,12 @@ func TestAccumulator_NilSafe(t *testing.T) {
|
||||
func TestAccumulator_CapturesSessionMetadata(t *testing.T) {
|
||||
a := &sse.Accumulator{}
|
||||
a.Append(&sdk.StreamResponse{
|
||||
ResponseType: sdk.ResponseTypeAnswer,
|
||||
SessionID: "sess_123",
|
||||
AssistantMessageID: "msg_456",
|
||||
Content: "hi",
|
||||
})
|
||||
a.Append(&sdk.StreamResponse{Done: true})
|
||||
a.Append(terminator)
|
||||
if a.SessionID != "sess_123" {
|
||||
t.Errorf("SessionID: got %q", a.SessionID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user