fix(cli): preserve agent update flags in retry_argv

When agent update hit exit-10 confirmation, retry_argv dropped
--add-kb/--remove-kb/--system-prompt-file/--config-file, so re-running
after approval could not reproduce the original update.

Expand pflag SliceValue flags as repeated --flag value pairs in
BuildRetryArgv, and include the missing agent update flags in the
allow list. Fixes #2597.
main
jiahao6635 2026-08-07 16:26:45 +08:00 committed by lyingbug
parent 4cb9ed6f3b
commit 8ec5388655
4 changed files with 113 additions and 10 deletions

View File

@ -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.

View File

@ -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)
}

View File

@ -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")
}

View File

@ -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)
}
}
}