Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(cli): preserve agent update flags in retry_argv

jiahao66358ec53886554d
diff --git a/cli/cmd/agent/edit.go b/cli/cmd/agent/edit.go
index dcd1cc91b..40a9fb783 100644
--- a/cli/cmd/agent/edit.go
+++ b/cli/cmd/agent/edit.go
@@ -217,11 +217,12 @@ func NewCmdEdit(f *cmdutil.Factory) *cobra.Command {
}
yes, _ := cmd.Flags().GetBool("yes")
// Build the retry command from the flags the user actually passed.
- // --add-kb/--remove-kb/--system-prompt-file/--config-file are excluded
- // (multi-value / file-based — a precise single argv is impractical).
+ // Include list-shaped and file-path flags so exit-10 retry_argv reproduces
+ // the original update (BuildRetryArgv expands StringSlice as repeats).
retryCmd := cmdutil.BuildRetryArgv(cmd, []string{"weknora", "agent", "update", opts.AgentID},
- "name", "description", "model", "system-prompt", "agent-mode",
- "rerank-model", "temperature", "kb-selection-mode", "format")
+ "name", "description", "model", "system-prompt", "system-prompt-file",
+ "agent-mode", "rerank-model", "temperature", "add-kb", "remove-kb",
+ "kb-selection-mode", "config-file", "format")
if err := cmdutil.ConfirmWrite(f.Prompter(), yes, fopts.WantsJSON(), "update", "agent", opts.AgentID, "agent.update", retryCmd); err != nil {
return err
}
@@ -284,7 +285,6 @@ func NewCmdEdit(f *cmdutil.Factory) *cobra.Command {
return cmd
}
-
// editHasAnyFlag reports whether opts carries at least one surgical update
// signal. Required-flag validation lives in runEdit (not PreRunE) so unit
// tests can invoke runEdit with a hand-built EditOptions directly.
diff --git a/cli/cmd/agent/edit_test.go b/cli/cmd/agent/edit_test.go
index b76881d66..67592b96a 100644
--- a/cli/cmd/agent/edit_test.go
+++ b/cli/cmd/agent/edit_test.go
@@ -324,3 +324,32 @@ func TestAgentEdit_RequiresConfirmation(t *testing.T) {
assert.Contains(t, ce.RetryArgv, "-y")
assert.Contains(t, ce.RetryArgv, "ag_abc")
}
+
+// TestAgentEdit_RetryArgvPreservesAddKB is a regression for #2597: exit-10
+// retry_argv must keep --add-kb / --remove-kb / --config-file so re-running
+// after human approval reproduces the original update.
+func TestAgentEdit_RetryArgvPreservesAddKB(t *testing.T) {
+ iostreams.SetForTest(t) // non-TTY
+ f := &cmdutil.Factory{
+ Client: func() (*sdk.Client, error) { return nil, nil },
+ Prompter: func() prompt.Prompter { return prompt.AgentPrompter{} },
+ }
+ // Avoid --config-file / --system-prompt-file here: PreRunE opens paths and
+ // would fail before ConfirmWrite. Those flags still go through the same
+ // BuildRetryArgv scalar path covered in cmdutil tests.
+ root := withRootHarnessAgent(NewCmdEdit(f),
+ "ag_abc", "--add-kb", "kb_new", "--remove-kb", "kb_old", "--format", "json")
+ err := root.Execute()
+ require.Error(t, err)
+ var ce *cmdutil.Error
+ require.ErrorAs(t, err, &ce)
+ assert.Equal(t, cmdutil.CodeInputConfirmationRequired, ce.Code)
+ // pflag Visit is lexicographical among changed flags.
+ assert.Equal(t, []string{
+ "weknora", "agent", "update", "ag_abc",
+ "--add-kb", "kb_new",
+ "--format", "json",
+ "--remove-kb", "kb_old",
+ "-y",
+ }, ce.RetryArgv)
+}
diff --git a/cli/internal/cmdutil/confirm.go b/cli/internal/cmdutil/confirm.go
index 732432048..7473906f9 100644
--- a/cli/internal/cmdutil/confirm.go
+++ b/cli/internal/cmdutil/confirm.go
@@ -15,9 +15,13 @@ import (
// BuildRetryArgv assembles the directly-executable retry argv for a
// confirmation-gated command: head (e.g. []string{"weknora","kb","update",id})
// followed by each *changed* flag named in allow (as "--name", "value") in
-// pflag's visit order, plus a trailing "-y". Flags not in allow — multi-value
-// or file-based ones where a precise single argv is impractical — are skipped;
-// the human reconstructs those. Replaces the per-command build*RetryCmd helpers.
+// pflag's visit order, plus a trailing "-y".
+//
+// Slice-typed flags (StringSlice / StringArray) expand to repeated
+// "--name", "value" pairs so the argv is re-executable by shells and agents.
+// Scalar flags use f.Value.String(). Flags not in allow are skipped (callers
+// still exclude secrets / stdin-only values such as --api-key-stdin).
+// Replaces the per-command build*RetryCmd helpers.
func BuildRetryArgv(c *cobra.Command, head []string, allow ...string) []string {
allowed := make(map[string]struct{}, len(allow))
for _, a := range allow {
@@ -25,9 +29,18 @@ func BuildRetryArgv(c *cobra.Command, head []string, allow ...string) []string {
}
parts := append([]string{}, head...)
c.Flags().Visit(func(f *pflag.Flag) {
- if _, ok := allowed[f.Name]; ok {
- parts = append(parts, "--"+f.Name, f.Value.String())
+ if _, ok := allowed[f.Name]; !ok {
+ return
+ }
+ // Expand multi-value flags as repeated --flag value pairs so retry_argv
+ // is directly executable (pflag's String() returns "[a,b]", which is not).
+ if sv, ok := f.Value.(pflag.SliceValue); ok {
+ for _, v := range sv.GetSlice() {
+ parts = append(parts, "--"+f.Name, v)
+ }
+ return
}
+ parts = append(parts, "--"+f.Name, f.Value.String())
})
return append(parts, "-y")
}
diff --git a/cli/internal/cmdutil/confirm_test.go b/cli/internal/cmdutil/confirm_test.go
index ac16a6217..48f32b7f9 100644
--- a/cli/internal/cmdutil/confirm_test.go
+++ b/cli/internal/cmdutil/confirm_test.go
@@ -4,6 +4,8 @@ import (
"strings"
"testing"
+ "github.com/spf13/cobra"
+
"github.com/Tencent/WeKnora/cli/internal/cmdutil"
"github.com/Tencent/WeKnora/cli/internal/iostreams"
"github.com/Tencent/WeKnora/cli/internal/testutil"
@@ -53,3 +55,62 @@ func TestConfirmDestructiveBatch_VerbMatchesOperation(t *testing.T) {
t.Errorf("unexpected batch message: %q", err.Error())
}
}
+
+func TestBuildRetryArgv_ScalarsAndSlices(t *testing.T) {
+ cmd := &cobra.Command{Use: "update"}
+ var name string
+ var addKBs []string
+ var format string
+ cmd.Flags().StringVar(&name, "name", "", "")
+ cmd.Flags().StringSliceVar(&addKBs, "add-kb", nil, "")
+ cmd.Flags().StringVar(&format, "format", "", "")
+ if err := cmd.Flags().Parse([]string{
+ "--add-kb", "kb_new",
+ "--add-kb", "kb_old",
+ "--name", "Renamed",
+ "--format", "json",
+ }); err != nil {
+ t.Fatal(err)
+ }
+
+ got := cmdutil.BuildRetryArgv(cmd, []string{"weknora", "agent", "update", "ag_abc"},
+ "name", "add-kb", "format")
+ // Visit order is lexicographical among changed flags.
+ want := []string{
+ "weknora", "agent", "update", "ag_abc",
+ "--add-kb", "kb_new",
+ "--add-kb", "kb_old",
+ "--format", "json",
+ "--name", "Renamed",
+ "-y",
+ }
+ if len(got) != len(want) {
+ t.Fatalf("len got=%d want=%d\ngot=%v\nwant=%v", len(got), len(want), got, want)
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Fatalf("idx %d: got %q want %q\nfull got=%v", i, got[i], want[i], got)
+ }
+ }
+}
+
+func TestBuildRetryArgv_SkipsFlagsNotInAllow(t *testing.T) {
+ cmd := &cobra.Command{Use: "update"}
+ var name, secret string
+ cmd.Flags().StringVar(&name, "name", "", "")
+ cmd.Flags().StringVar(&secret, "api-key-stdin", "", "")
+ if err := cmd.Flags().Parse([]string{"--name", "x", "--api-key-stdin", "-"}); err != nil {
+ t.Fatal(err)
+ }
+
+ got := cmdutil.BuildRetryArgv(cmd, []string{"weknora", "model", "update", "m1"}, "name", "format")
+ want := []string{"weknora", "model", "update", "m1", "--name", "x", "-y"}
+ if len(got) != len(want) {
+ t.Fatalf("len got=%d want=%d\ngot=%v\nwant=%v", len(got), len(want), got, want)
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Fatalf("idx %d: got %q want %q\nfull got=%v", i, got[i], want[i], got)
+ }
+ }
+}