feat(cli): v0.9 contract harmonization
Make the agent-facing contract uniform across the whole surface: - cobra parse errors (unknown flag, arg-count) emit the JSON envelope by default, not bare prose — via a single DefaultFormatMode shared by the early resolver and ResolveDefault. - a malformed --jq is input.invalid_argument (exit 5), not internal.error. - destructive-confirmation messages name the real operation (edit/remove, not a hardcoded "delete"). - `doc wait` emits one envelope (Silent failure, no contradictory stderr); completed marshals as [] not null. - WEKNORA_AGENT_HELP covers every leaf command, enforced by a coverage drift guard; a sibling guard enforces --dry-run on every mutation. - `search docs`/`search chunks` resolve --kb through the same flag→env→project-link chain as `doc list`/`chat` (no longer a hard --kb requirement); a linked dir or WEKNORA_KB_ID now works without --kb, and an unresolved KB is local.kb_id_required (exit 1). The destructive `doc delete --all` keeps its explicit-only rule. - exit-code and batch/wait ok-semantics documented in the shared skill, README, and AGENTS.md.
This commit is contained in:
@@ -46,17 +46,27 @@ func filterObjectKeys(objRaw json.RawMessage, fields []string) (json.RawMessage,
|
||||
return json.Marshal(dst)
|
||||
}
|
||||
|
||||
// JQError marks a failure attributable to the user-supplied --jq expression
|
||||
// (parse or evaluation), as opposed to an internal serialization fault. The
|
||||
// cmdutil layer (which can't be imported here without a cycle) maps it to
|
||||
// input.invalid_argument so agents fix the expression rather than treating a
|
||||
// bad --jq as a CLI bug (internal.error / exit 1).
|
||||
type JQError struct{ Err error }
|
||||
|
||||
func (e *JQError) Error() string { return e.Err.Error() }
|
||||
func (e *JQError) Unwrap() error { return e.Err }
|
||||
|
||||
// writeJQ evaluates expr against raw and writes each result line by line to w.
|
||||
// String results render without quotes (so `--jq '.name'` yields shell-friendly
|
||||
// bare strings); non-string results use encoding/json.
|
||||
//
|
||||
// Returns input.invalid_argument-shaped errors via plain errors.New + fmt;
|
||||
// the caller is responsible for wrapping with cmdutil.NewError if it wants
|
||||
// the typed code.
|
||||
// Expression-attributable failures (parse / eval) are wrapped in *JQError so
|
||||
// the caller can map them to a typed input.invalid_argument code. A failure to
|
||||
// parse `raw` itself is internal (our own envelope JSON) and stays plain.
|
||||
func writeJQ(w io.Writer, raw []byte, expr string) error {
|
||||
query, err := gojq.Parse(expr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("jq parse: %w", err)
|
||||
return &JQError{Err: fmt.Errorf("jq parse: %w", err)}
|
||||
}
|
||||
var input any
|
||||
if err := json.Unmarshal(raw, &input); err != nil {
|
||||
@@ -69,7 +79,7 @@ func writeJQ(w io.Writer, raw []byte, expr string) error {
|
||||
return nil
|
||||
}
|
||||
if e, ok := v.(error); ok {
|
||||
return fmt.Errorf("jq eval: %w", e)
|
||||
return &JQError{Err: fmt.Errorf("jq eval: %w", e)}
|
||||
}
|
||||
if s, ok := v.(string); ok {
|
||||
if _, err := fmt.Fprintln(w, s); err != nil {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package format_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/cli/internal/format"
|
||||
)
|
||||
|
||||
// A malformed --jq expression must surface as *format.JQError so the cmdutil
|
||||
// layer can map it to a typed input.invalid_argument (exit 5) instead of the
|
||||
// unclassified internal.error bucket (exit 1). Regression for the bad-jq
|
||||
// misclassification.
|
||||
func TestWriteJSONFiltered_BadJQExpr_IsJQError(t *testing.T) {
|
||||
for _, expr := range []string{".data[", "this is not jq"} {
|
||||
buf := &bytes.Buffer{}
|
||||
err := format.WriteJSONFiltered(buf, map[string]any{"data": []any{}}, nil, expr)
|
||||
if err == nil {
|
||||
t.Fatalf("expr %q: expected error, got nil", expr)
|
||||
}
|
||||
var jqe *format.JQError
|
||||
if !errors.As(err, &jqe) {
|
||||
t.Errorf("expr %q: expected *format.JQError, got %T: %v", expr, err, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A jq eval failure (valid parse, runtime error) is also expression-attributable
|
||||
// and must be a *JQError.
|
||||
func TestWriteJSONFiltered_JQEvalError_IsJQError(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
// ".foo.bar" against a non-object .foo raises an eval error.
|
||||
err := format.WriteJSONFiltered(buf, map[string]any{"foo": 5}, nil, ".foo.bar")
|
||||
if err == nil {
|
||||
t.Fatal("expected eval error, got nil")
|
||||
}
|
||||
var jqe *format.JQError
|
||||
if !errors.As(err, &jqe) {
|
||||
t.Errorf("expected *format.JQError, got %T: %v", err, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A valid expression must NOT error.
|
||||
func TestWriteJSONFiltered_ValidJQ_NoError(t *testing.T) {
|
||||
buf := &bytes.Buffer{}
|
||||
if err := format.WriteJSONFiltered(buf, map[string]any{"data": []any{"a", "b"}}, nil, ".data[]"); err != nil {
|
||||
t.Fatalf("valid jq errored: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user