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:
@@ -77,7 +77,7 @@ answer back. By default a fresh session is created on first invocation; pass
|
||||
Modes:
|
||||
TTY (default): live token streaming + reference footer
|
||||
Pipe / --no-stream / --json: buffered, emitted once on completion`,
|
||||
Example: ` weknora chat "What is RRF?" --kb kb_123
|
||||
Example: ` weknora chat "What is RRF?" --kb a32a63ff-fb36-4874-bcaa-30f48570a694
|
||||
weknora chat "Summarise this design doc" --kb my-kb --json
|
||||
weknora chat "Continue?" --session-id sess_abc`,
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
@@ -98,7 +98,7 @@ Modes:
|
||||
return runChat(c.Context(), opts, cli)
|
||||
},
|
||||
}
|
||||
cmd.Flags().String("kb", "", "Knowledge base id (kb_…) or name (overrides project link / env)")
|
||||
cmd.Flags().String("kb", "", "Knowledge base UUID or name (overrides project link / env)")
|
||||
cmd.Flags().StringVar(&opts.SessionID, "session-id", "", "Continue an existing chat session (skip auto-create)")
|
||||
cmd.Flags().BoolVar(&opts.NoStream, "no-stream", false, "Buffer the full answer before printing (forces accumulate mode)")
|
||||
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Emit a single JSON envelope (implies --no-stream)")
|
||||
|
||||
+12
-10
@@ -64,11 +64,12 @@ func TestChat_StreamMode(t *testing.T) {
|
||||
out, errBuf := iostreams.SetForTestWithTTY(t)
|
||||
svc := &fakeChatService{
|
||||
streamEvents: []*sdk.StreamResponse{
|
||||
{Content: "Hello "},
|
||||
{Content: "world"},
|
||||
{Done: true, KnowledgeReferences: []*sdk.SearchResult{
|
||||
{ResponseType: sdk.ResponseTypeAnswer, Content: "Hello "},
|
||||
{ResponseType: sdk.ResponseTypeAnswer, Content: "world"},
|
||||
{ResponseType: sdk.ResponseTypeReferences, KnowledgeReferences: []*sdk.SearchResult{
|
||||
{KnowledgeID: "k1", KnowledgeTitle: "Doc One", Score: 0.42},
|
||||
}},
|
||||
{ResponseType: sdk.ResponseTypeComplete, Done: true},
|
||||
},
|
||||
}
|
||||
opts := &Options{Query: "hi", KBID: "kb_1"}
|
||||
@@ -106,7 +107,8 @@ func TestChat_JSONMode(t *testing.T) {
|
||||
streamEvents: []*sdk.StreamResponse{
|
||||
{Content: "answer body"},
|
||||
{AssistantMessageID: "msg_99"},
|
||||
{Done: true, KnowledgeReferences: []*sdk.SearchResult{{KnowledgeID: "k1"}}},
|
||||
{ResponseType: sdk.ResponseTypeReferences, KnowledgeReferences: []*sdk.SearchResult{{KnowledgeID: "k1"}}},
|
||||
{ResponseType: sdk.ResponseTypeComplete, Done: true},
|
||||
},
|
||||
}
|
||||
opts := &Options{Query: "q", KBID: "kb_42", JSONOut: true}
|
||||
@@ -166,9 +168,9 @@ func TestChat_NoStreamFlag(t *testing.T) {
|
||||
var written string
|
||||
svc := &fakeChatService{
|
||||
streamEvents: []*sdk.StreamResponse{
|
||||
{Content: "buffered "},
|
||||
{Content: "answer"},
|
||||
{Done: true},
|
||||
{ResponseType: sdk.ResponseTypeAnswer, Content: "buffered "},
|
||||
{ResponseType: sdk.ResponseTypeAnswer, Content: "answer"},
|
||||
{ResponseType: sdk.ResponseTypeComplete, Done: true},
|
||||
},
|
||||
}
|
||||
opts := &Options{Query: "q", KBID: "kb", NoStream: true}
|
||||
@@ -186,8 +188,8 @@ func TestChat_NonTTY_AccumulateMode(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
svc := &fakeChatService{
|
||||
streamEvents: []*sdk.StreamResponse{
|
||||
{Content: "piped"},
|
||||
{Done: true},
|
||||
{ResponseType: sdk.ResponseTypeAnswer, Content: "piped"},
|
||||
{ResponseType: sdk.ResponseTypeComplete, Done: true},
|
||||
},
|
||||
}
|
||||
opts := &Options{Query: "q", KBID: "kb"}
|
||||
@@ -202,7 +204,7 @@ func TestChat_NonTTY_AccumulateMode(t *testing.T) {
|
||||
func TestChat_SessionIDProvided(t *testing.T) {
|
||||
_, errBuf := iostreams.SetForTestWithTTY(t)
|
||||
svc := &fakeChatService{
|
||||
streamEvents: []*sdk.StreamResponse{{Done: true}},
|
||||
streamEvents: []*sdk.StreamResponse{{ResponseType: sdk.ResponseTypeComplete, Done: true}},
|
||||
}
|
||||
opts := &Options{Query: "q", KBID: "kb", SessionID: "sess_existing"}
|
||||
if err := runChat(context.Background(), opts, svc); err != nil {
|
||||
|
||||
+6
-6
@@ -52,14 +52,14 @@ func NewCmdList(f *cmdutil.Factory) *cobra.Command {
|
||||
Long: `Lists documents (uploaded files / web pages / inline text) in the
|
||||
resolved knowledge base. KB resolution follows the standard 4-level chain:
|
||||
--kb flag > WEKNORA_KB_ID env > .weknora/project.yaml > error. The --kb
|
||||
flag accepts either a kb_<id> or a name (resolved via list).
|
||||
flag accepts either a KB UUID (passed through) or a name (resolved via list).
|
||||
|
||||
Default sort is updated_at desc so the most recent uploads surface first;
|
||||
backend storage order is not guaranteed and varies between deployments.`,
|
||||
Example: ` weknora doc list # uses project link / env
|
||||
weknora doc list --kb kb_abc # explicit id
|
||||
weknora doc list --kb my-kb # resolved by name
|
||||
weknora doc list --page 2 --json # paginated envelope output`,
|
||||
Example: ` weknora doc list # uses project link / env
|
||||
weknora doc list --kb a32a63ff-fb36-4874-bcaa-30f48570a694 # explicit UUID
|
||||
weknora doc list --kb my-kb # resolved by name
|
||||
weknora doc list --page 2 --json # paginated envelope output`,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(c *cobra.Command, _ []string) error {
|
||||
kbID, err := f.ResolveKB(c)
|
||||
@@ -75,7 +75,7 @@ backend storage order is not guaranteed and varies between deployments.`,
|
||||
}
|
||||
// --kb is read by Factory.ResolveKB; declare it here so cobra parses the
|
||||
// value into the command's flag set.
|
||||
cmd.Flags().String("kb", "", "Knowledge base id (kb_…) or name (overrides env / project link)")
|
||||
cmd.Flags().String("kb", "", "Knowledge base UUID or name (overrides env / project link)")
|
||||
cmd.Flags().IntVar(&opts.Page, "page", 1, "Page number (1-based)")
|
||||
cmd.Flags().IntVar(&opts.PageSize, "page-size", 20, "Items per page")
|
||||
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
||||
|
||||
@@ -47,7 +47,7 @@ func NewCmdUpload(f *cmdutil.Factory) *cobra.Command {
|
||||
Long: `Uploads a file (PDF / DOCX / Markdown / TXT / etc.) to the resolved
|
||||
knowledge base. KB resolution follows the standard 4-level chain:
|
||||
--kb flag > WEKNORA_KB_ID env > .weknora/project.yaml > error. The --kb
|
||||
flag accepts either a kb_<id> or a name (resolved via list).
|
||||
flag accepts either a KB UUID (passed through) or a name (resolved via list).
|
||||
|
||||
Pass --name to override the recorded file name (useful when the local file
|
||||
has a generic name like "report.pdf" but you want to surface it as e.g.
|
||||
@@ -56,7 +56,7 @@ has a generic name like "report.pdf" but you want to surface it as e.g.
|
||||
v0.2 ships single-file upload only; --recursive / --glob and progress UI are
|
||||
planned for v0.3.`,
|
||||
Example: ` weknora doc upload report.pdf
|
||||
weknora doc upload notes.md --kb kb_abc
|
||||
weknora doc upload notes.md --kb a32a63ff-fb36-4874-bcaa-30f48570a694
|
||||
weknora doc upload notes.md --kb my-kb
|
||||
weknora doc upload q3.pdf --name "Q3 Marketing Report.pdf"`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
@@ -80,7 +80,7 @@ planned for v0.3.`,
|
||||
return runUpload(c.Context(), opts, cli, kbID, path)
|
||||
},
|
||||
}
|
||||
cmd.Flags().String("kb", "", "Knowledge base id (kb_…) or name (overrides env / project link)")
|
||||
cmd.Flags().String("kb", "", "Knowledge base UUID or name (overrides env / project link)")
|
||||
cmd.Flags().StringVar(&opts.Name, "name", "", "Custom file name to record (defaults to base name)")
|
||||
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
||||
agent.SetAgentHelp(cmd, "Uploads one local file to the resolved KB. Refuses non-regular files (dir / symlink). Returns data: Knowledge object with id, parse_status. Pass --kb outside a linked project.")
|
||||
|
||||
@@ -240,6 +240,25 @@ func fillVersionCheck(c *Check, info *compat.Info, cliVer string, fromCache bool
|
||||
// at secrets.NewBestEffortStore.
|
||||
var credStoreFactory = func() (secrets.Store, error) { return secrets.NewBestEffortStore() }
|
||||
|
||||
// SetCredStoreFactoryForTest overrides the credential-storage factory used by
|
||||
// runChecks and returns a cleanup function that restores the previous value.
|
||||
// Exported so out-of-package tests (notably cli/acceptance/contract) can
|
||||
// pin the credential_storage outcome — otherwise the check probes the real
|
||||
// OS keyring, which is present on macOS dev machines but not on Linux CI
|
||||
// runners without libsecret, producing host-dependent test flakes.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// restore := doctor.SetCredStoreFactoryForTest(func() (secrets.Store, error) {
|
||||
// return secrets.NewMemStore(), nil // not *FileStore → StatusOK
|
||||
// })
|
||||
// defer restore()
|
||||
func SetCredStoreFactoryForTest(fn func() (secrets.Store, error)) (restore func()) {
|
||||
saved := credStoreFactory
|
||||
credStoreFactory = fn
|
||||
return func() { credStoreFactory = saved }
|
||||
}
|
||||
|
||||
// fillCredentialStorageCheck distinguishes the three terminal states the
|
||||
// secrets layer can produce:
|
||||
//
|
||||
|
||||
@@ -57,6 +57,12 @@ func goodUserResp() *sdk.CurrentUserResponse {
|
||||
func TestDoctor_AllOK(t *testing.T) {
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
// Pin credential_storage to ok so this test doesn't probe the host OS
|
||||
// keyring — macOS dev machines have Keychain, Linux CI without libsecret
|
||||
// would otherwise warn and break the AllPassed assertion.
|
||||
withCredStoreFactory(t, func() (secrets.Store, error) {
|
||||
return secrets.NewMemStore(), nil
|
||||
})
|
||||
svc := &fakeServices{
|
||||
systemInfo: &sdk.SystemInfo{Version: "1.0.0"},
|
||||
userResp: goodUserResp(),
|
||||
@@ -173,6 +179,11 @@ func TestDoctor_NoConfig_StillRunsCredentialStorage(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
_, _ = iostreams.SetForTest(t)
|
||||
// Pin credential_storage to ok regardless of host keyring presence
|
||||
// (Linux CI without libsecret would otherwise warn).
|
||||
withCredStoreFactory(t, func() (secrets.Store, error) {
|
||||
return secrets.NewMemStore(), nil
|
||||
})
|
||||
|
||||
f := cmdutil.New()
|
||||
svc, err := buildServices(f)
|
||||
|
||||
@@ -25,7 +25,7 @@ import (
|
||||
// Options captures `weknora link` flags.
|
||||
type Options struct {
|
||||
Context string // --context: record a specific context instead of the active one
|
||||
KB string // --kb: kb_<id> or name; empty triggers interactive prompt on TTY
|
||||
KB string // --kb: KB UUID or name; empty triggers interactive prompt on TTY
|
||||
JSONOut bool // --json
|
||||
}
|
||||
|
||||
@@ -54,16 +54,16 @@ any existing link — re-run to switch.
|
||||
|
||||
AI agents: link writes to the user's working directory. Only run it when the
|
||||
user explicitly asked to bind this directory; don't run it as a side effect.`,
|
||||
Example: ` weknora link --kb kb_abc # explicit id
|
||||
weknora link --kb engineering # name → id
|
||||
weknora link # interactive (TTY)`,
|
||||
Example: ` weknora link --kb a32a63ff-fb36-4874-bcaa-30f48570a694 # explicit UUID
|
||||
weknora link --kb engineering # name → id
|
||||
weknora link # interactive (TTY)`,
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(c *cobra.Command, _ []string) error {
|
||||
return runLink(c.Context(), opts, f)
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringVar(&opts.Context, "context", "", "Context to record in the link (defaults to active context)")
|
||||
cmd.Flags().StringVar(&opts.KB, "kb", "", "Knowledge base id (kb_…) or name; omit on a TTY for interactive prompt")
|
||||
cmd.Flags().StringVar(&opts.KB, "kb", "", "Knowledge base UUID or name; omit on a TTY for interactive prompt")
|
||||
cmd.Flags().BoolVar(&opts.JSONOut, "json", false, "Output JSON envelope")
|
||||
agent.SetAgentHelp(cmd, "Writes .weknora/project.yaml binding cwd to a KB. Pass --kb (id or name) for non-interactive use. Always overwrites.")
|
||||
return cmd
|
||||
|
||||
@@ -64,13 +64,13 @@ func TestLink_ByID(t *testing.T) {
|
||||
out, _ := iostreams.SetForTest(t)
|
||||
|
||||
f := newFactory("default", nil)
|
||||
opts := &Options{KB: "kb_xxx"}
|
||||
opts := &Options{KB: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa"}
|
||||
require.NoError(t, runLink(context.Background(), opts, f))
|
||||
|
||||
linkPath := filepath.Join(dir, ".weknora", "project.yaml")
|
||||
p, err := projectlink.Load(linkPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "kb_xxx", p.KBID)
|
||||
assert.Equal(t, "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa", p.KBID)
|
||||
assert.Equal(t, "default", p.Context)
|
||||
assert.Contains(t, out.String(), "✓")
|
||||
}
|
||||
@@ -118,16 +118,16 @@ func TestLink_OverwritesExisting(t *testing.T) {
|
||||
// Pre-existing link.
|
||||
linkPath := filepath.Join(dir, ".weknora", "project.yaml")
|
||||
require.NoError(t, projectlink.Save(linkPath, &projectlink.Project{
|
||||
Context: "default", KBID: "kb_old",
|
||||
Context: "default", KBID: "11111111-1111-4111-8111-111111111111",
|
||||
}))
|
||||
|
||||
f := newFactory("default", nil)
|
||||
opts := &Options{KB: "kb_new"}
|
||||
opts := &Options{KB: "22222222-2222-4222-8222-222222222222"}
|
||||
require.NoError(t, runLink(context.Background(), opts, f))
|
||||
|
||||
p, err := projectlink.Load(linkPath)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "kb_new", p.KBID, "link should overwrite silently")
|
||||
assert.Equal(t, "22222222-2222-4222-8222-222222222222", p.KBID, "link should overwrite silently")
|
||||
}
|
||||
|
||||
// TestLink_NonInteractive_NoKB exercises the non-TTY-without-flag error path.
|
||||
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
// Options is the runtime configuration of one search invocation.
|
||||
type Options struct {
|
||||
Query string
|
||||
KB string // raw --kb flag value (kb_<id> or name)
|
||||
KB string // raw --kb flag value (UUID or name)
|
||||
KBID string // resolved id, populated by RunE before HybridSearch
|
||||
TopK int
|
||||
VectorThreshold float64
|
||||
@@ -36,7 +36,7 @@ type Service interface {
|
||||
|
||||
// NewCmdSearch builds `weknora search "<query>" --kb <id-or-name>`.
|
||||
//
|
||||
// The single `--kb` flag accepts either a `kb_…` id (passed through) or a
|
||||
// The single `--kb` flag accepts either a KB UUID (passed through) or a
|
||||
// name (resolved via ListKnowledgeBases). Mirrors gcloud `--project`'s
|
||||
// id-or-name auto-detection — the only mainstream pattern that collapses
|
||||
// the two forms onto one flag.
|
||||
@@ -70,7 +70,7 @@ func NewCmdSearch(f *cmdutil.Factory) *cobra.Command {
|
||||
return runSearch(c.Context(), opts, cli)
|
||||
},
|
||||
}
|
||||
cmd.Flags().StringVar(&opts.KB, "kb", "", "Knowledge base id (kb_…) or name")
|
||||
cmd.Flags().StringVar(&opts.KB, "kb", "", "Knowledge base UUID or name")
|
||||
cmd.Flags().IntVar(&opts.TopK, "top-k", 8, "Maximum results to return")
|
||||
cmd.Flags().Float64Var(&opts.VectorThreshold, "vector-threshold", 0, "Vector retrieval similarity floor (per-channel, pre-fusion); 0 = no filter")
|
||||
cmd.Flags().Float64Var(&opts.KeywordThreshold, "keyword-threshold", 0, "Keyword retrieval score floor (per-channel, pre-fusion); 0 = no filter")
|
||||
|
||||
Reference in New Issue
Block a user