Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(cli): reject invalid jq syntax before polling (#4258)
Rudy Mizrahi Celeklia9d01d015446
diff --git a/cli/cmd/doc/wait_jq_validation_test.go b/cli/cmd/doc/wait_jq_validation_test.gonew file mode 100644index 000000000..24dfdd857--- /dev/null+++ b/cli/cmd/doc/wait_jq_validation_test.go@@ -0,0 +1,78 @@+package doc++import (+ "encoding/json"+ "errors"+ "net/http"+ "net/http/httptest"+ "sync/atomic"+ "testing"++ "github.com/Tencent/WeKnora/cli/internal/cmdutil"+ "github.com/Tencent/WeKnora/cli/internal/iostreams"+ sdk "github.com/Tencent/WeKnora/client"+)++func TestDocWait_JQSyntaxCheckedBeforeClient(t *testing.T) {+ for _, expression := range []string{"[", ".data.completed[]"} {+ t.Run(expression, func(t *testing.T) {+ out, _ := iostreams.SetForTest(t)+ var clientCalls, requests atomic.Int32+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ requests.Add(1)+ w.Header().Set("Content-Type", "application/json")+ _ = json.NewEncoder(w).Encode(map[string]any{+ "success": true,+ "data": map[string]any{"id": "doc-1", "parse_status": "completed"},+ })+ }))+ t.Cleanup(server.Close)+ f := &cmdutil.Factory{Client: func() (*sdk.Client, error) {+ clientCalls.Add(1)+ return sdk.NewClient(server.URL), nil+ }}+ root := withRootHarnessDoc(NewCmdWait(f),+ "doc-1", "--timeout", "2s", "--format", "json", "--jq", expression)+ err := root.Execute()+ if expression == "[" {+ var typed *cmdutil.Error+ if !errors.As(err, &typed) || typed.Code != cmdutil.CodeInputInvalidArgument {+ t.Fatalf("error=%v, want input.invalid_argument", err)+ }+ if clientCalls.Load() != 0 || requests.Load() != 0 || out.Len() != 0 {+ t.Errorf("invalid syntax performed work: client=%d requests=%d stdout=%q",+ clientCalls.Load(), requests.Load(), out.String())+ }+ return+ }+ if err != nil {+ t.Fatal(err)+ }+ if clientCalls.Load() != 1 || requests.Load() != 1 || out.String() != "doc-1\n" {+ t.Errorf("valid projection client=%d requests=%d stdout=%q",+ clientCalls.Load(), requests.Load(), out.String())+ }+ })+ }+}++func TestDocWait_JQFormatPrecedenceBeforePolling(t *testing.T) {+ for _, expression := range []string{"[", ".data.completed[]"} {+ t.Run(expression, func(t *testing.T) {+ t.Setenv("WEKNORA_FORMAT", "text")+ out, _ := iostreams.SetForTest(t)+ var clientCalls int+ f := &cmdutil.Factory{Client: func() (*sdk.Client, error) {+ clientCalls+++ return nil, errors.New("client should not be constructed")+ }}+ root := withRootHarnessDoc(NewCmdWait(f), "doc-1", "--jq", expression)+ err := root.Execute()+ var flagError *cmdutil.FlagError+ if !errors.As(err, &flagError) || cmdutil.ExitCode(err) != 2 || clientCalls != 0 || out.Len() != 0 {+ t.Errorf("text/jq error=%v exit=%d client=%d stdout=%q",+ err, cmdutil.ExitCode(err), clientCalls, out.String())+ }+ })+ }+}diff --git a/cli/internal/cmdutil/format.go b/cli/internal/cmdutil/format.goindex 6119f30f6..a80d64db7 100644--- a/cli/internal/cmdutil/format.go+++ b/cli/internal/cmdutil/format.go@@ -8,6 +8,7 @@ import ("sort""strings"+ "github.com/itchyny/gojq""github.com/spf13/cobra""github.com/Tencent/WeKnora/cli/internal/format"@@ -85,12 +86,21 @@ func CheckFormatFlag(cmd *cobra.Command) (*FormatOptions, error) {if f := cmd.Flags().Lookup("jq"); f != nil {fopts.JQ = f.Value.String()}- // --jq only meaningful for JSON-shaped output. Reject the explicit- // `--format text --jq ...` combination; the `--jq` with --format unset- // case is handled by ResolveDefault.- if fopts.JQ != "" && fopts.Mode == FormatText {+ // --jq only makes sense for JSON-shaped output. Validate the effective+ // mode before syntax so text selected by the environment has the same+ // format error priority as an explicit text flag.+ effectiveMode := fopts.Mode+ if effectiveMode == "" {+ effectiveMode = FormatMode(os.Getenv("WEKNORA_FORMAT"))+ }+ if fopts.JQ != "" && effectiveMode == FormatText {return nil, NewFlagError(errors.New("--jq requires --format json|ndjson"))}+ if fopts.JQ != "" {+ if _, err := gojq.Parse(fopts.JQ); err != nil {+ return nil, mapJQError(&format.JQError{Err: fmt.Errorf("jq parse: %w", err)})+ }+ }return fopts, nil}