mirror of
https://github.com/Twigpine/zero.git
synced 2026-11-10 05:01:28 -10:00
* config: add ValidateFile for structured config diagnostics
* doctor: add offsetToLineCol helper for JSON error positions
* doctor: validate config files in config.validation check
* cli: thread config paths into doctor and degrade on bad JSON
* doctor: inline read, single-parse config validation + utf8 col test
- Remove the osReadFile wrapper; call os.ReadFile directly at its one call site
- Add config.ValidateBytes([]byte) so each config file is read and JSON-parsed
once in configValidationCheck instead of twice (once for position, once via
ValidateFile); ValidateFile now reads then delegates to its own unmarshal for
the path-prefixed error message, keeping its public signature unchanged
- Fix the misleading skip comment in configValidationCheck: configFilesCheck
only checks path-string presence; the skip is a defensive guard for the
unreachable case where DefaultResolveOptions passed a non-empty path that
still fails to read
- Add utf8 multibyte case to TestOffsetToLineCol documenting byte-column
semantics (column counts bytes, not runes)
* sandbox: add Policy.MaxAutonomy ceiling defaulting to high
* sandbox: normalize both operands in autonomyAllowed to fail closed
* sandbox: clamp grant-allow and unsafe escalation to policy autonomy ceiling
* sandbox: add RequiredAutonomyForBatch helper with fail-closed mapping
* config: add sandbox.maxAutonomy field to file and resolved config
* config: merge sandbox.maxAutonomy across layers, env, and overrides
* cli: thread configured autonomy ceiling into sandbox engine construction
* cli: surface max_autonomy in sandbox policy formatters
* zerocommands: carry max autonomy in sandbox policy snapshot
* sandbox: default empty policy ceiling to high + reason const
Evaluate now treats an empty Policy.MaxAutonomy (from a directly-constructed
Policy that bypasses DefaultPolicy) as High instead of letting it normalize to
Low, which would silently clamp every Medium/High decision to Prompt. The
ceiling is a no-op unless explicitly configured.
Also extracts the "above policy ceiling" clamp reason into a package const
referenced at both engine clamp sites and the related tests, and adds a
non-vacuous engine test proving the empty-ceiling trap is closed.
* zerogit: add base-ref three-dot diff to changes inspect
* zerogit: cover base-ref backward-compat and real-git diff
* zerogit: propagate base ref through change snapshot
* cli: parse changes --base ref and reject on commit
* cli: thread base ref into changes inspect output and help
* changes: redact base in cli summary + guard --base= + rename test
- redactChangeSummary now redacts the Base field alongside Root/Branch/Commit/etc.
- --base= equals-form rejects leading-dash values via flagValueLooksLikeOption, closing the option-smuggling gap
- TestParseNameStatusRenameAndCopy asserts rename/copy three-field lines use the new/destination path with correct status, and confirms two-field modify is unaffected
* Add ParseDiffStat helper for git diff --stat summaries
* Add usage report aggregation over persisted session usage events
* Add zero usage report command over persisted session usage
* Wire usage command into CLI dispatch and help
* usage: validate --since format, fix redaction comments, add tests
Fix 1 (BLOCKING): parseUsageArgs now validates --since with time.Parse
against YYYY-MM-DD; returns exitUsage + "invalid --since %q: expected
YYYY-MM-DD" for bare strings like "foo", unpadded "2026-6-1", or
wrong-separator "06/01/2026". Valid dates are stored unchanged for the
existing lexical comparison.
Fix 2: Replace false "pre-redaction stat" claims in diffstat.go and
usage.go with accurate comments: the --stat summary line carries no
secret-bearing tokens so parsing the already-redacted DiffStat is safe.
Fix 3: Add three tests to usage_test.go —
- TestRunUsageDaysFilter: stubs deps.now, seeds events on two dates,
asserts --days 3 includes the recent date and excludes the old one.
- TestRunUsageInvalidSince: table-driven; verifies "foo", "2026-6-1",
"06/01/2026" each return exitUsage + validation message, while
"2026-06-01" returns exitSuccess.
- TestRunUsageEmptyStore: zero usage events → exitSuccess, output
contains header and "total" row, no panic.
* doctor: surface type-mismatch offsets and drop colliding flat line/col keys
Probe config validation against config.FileConfig instead of a bare any so
a structurally-valid document with a wrong field type (e.g. maxTurns as a
string) surfaces a *json.UnmarshalTypeError carrying the offset, instead of
losing line/col. Remove the flat top-level details["line"]/["col"] keys
that were overwritten by the last malformed file when several were present;
keep only the unambiguous per-path map entry. Tighten the existing malformed
test to assert concrete per-path line/col and add a type-mismatch test that
exercises the previously-dead UnmarshalTypeError branch.
* sandbox: fail closed on invalid configured autonomy ceiling
An invalid non-empty sandbox.maxAutonomy (e.g. "moderate") previously
survived Resolve (only trimmed, never validated), failed to normalize at the
sandbox bridge, and left the policy unchanged at the default High ceiling.
A typo therefore silently disabled the admin's intended ceiling: fail-open on
a security boundary.
Validate at resolve time (fail loud): Resolve now rejects a non-empty
maxAutonomy that does not normalize, returning a clear error. As defense in
depth, applyConfiguredAutonomyCeiling now clamps an unrecognised non-empty
value to the most restrictive ceiling (low) instead of returning the policy
unchanged, so a bad value can never widen the ceiling even via direct use.
Empty/unset stays valid and keeps the default High ceiling.
Add resolver tests for invalid-fails / valid-resolves and bridge tests for
the fail-closed clamp and valid mappings.
* sandbox: fail closed on malformed autonomy and surface policy resolve errors
Treat a genuinely-invalid request autonomy as the highest tier in the engine
so it exceeds a Medium/Low ceiling and clamps to Prompt instead of being
sanitized to Low and auto-allowing on the grant/unsafe path. Surface
resolveConfig failures in the policy command instead of silently falling back
to the permissive DefaultPolicy, which would misreport trust posture.
* usage: bucket and filter by UTC calendar day, reject empty --session
Normalize RFC3339 timestamps to their UTC calendar date before day bucketing
(report) and before the --since/--days cutoff comparison (CLI) so offset
timestamps land on the correct UTC day and the bucket and cutoff agree;
malformed timestamps fall back to the leading-10 slice. Reject empty
--session/--session-id values with a value-required error matching the --since
validation style.
* doctor: fail on unreadable config files and stop fabricating positions
Surface a present-but-unreadable config path (permissions, is-a-directory) as
a failing per-path validation detail instead of silently skipping it; a
genuinely-missing path stays a skip. Return ok=false for a JSON parse error
that carries no offset so it routes through the no-position ValidateBytes path
rather than fabricating a (1,1) line/col. Assert empty stderr on the JSON
doctor failure path.
* config: make sandbox maxAutonomy resolver tests hermetic
Pass an explicit empty Env to the maxAutonomy Resolve tests so host
environment variables cannot leak in through the nil-Env os.Getenv fallback.
* sandbox: preserve raw autonomy for ceiling check so invalid clamps under default ceiling
Defaulting invalid request.Autonomy to High only clamped under a Medium/Low
ceiling; under the default High ceiling autonomyAllowed(High, High) returned true
and still auto-allowed (fail-open). Keep the raw requested value for the ceiling
check so autonomyAllowed's unknown-tier guard fails it closed under ANY ceiling.
Adds default-High-ceiling regression tests for the unsafe and grant-allow paths.
---------
Co-authored-by: KRATOS <[email protected]>
Co-authored-by: Vasanthdev2004 <[email protected]>
721 lines
22 KiB
Go
721 lines
22 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Gitlawb/zero/internal/redaction"
|
|
"github.com/Gitlawb/zero/internal/selfverify"
|
|
"github.com/Gitlawb/zero/internal/testrunner"
|
|
"github.com/Gitlawb/zero/internal/verify"
|
|
"github.com/Gitlawb/zero/internal/worktrees"
|
|
"github.com/Gitlawb/zero/internal/zerogit"
|
|
)
|
|
|
|
type worktreeCommandOptions struct {
|
|
json bool
|
|
name string
|
|
baseDir string
|
|
cwd string
|
|
}
|
|
|
|
type verifyCommandOptions struct {
|
|
json bool
|
|
cwd string
|
|
only []string
|
|
timeoutMS int
|
|
attempts int
|
|
}
|
|
|
|
type changesCommandOptions struct {
|
|
json bool
|
|
cwd string
|
|
baseRef string
|
|
message string
|
|
dryRun bool
|
|
maxDiffBytes int
|
|
}
|
|
|
|
func runWorktrees(args []string, stdout io.Writer, stderr io.Writer, deps appDeps) int {
|
|
command := "prepare"
|
|
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
|
command = strings.ToLower(strings.TrimSpace(args[0]))
|
|
args = args[1:]
|
|
}
|
|
if command == "help" {
|
|
if err := writeWorktreesHelp(stdout); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
if command != "prepare" {
|
|
return writeExecUsageError(stderr, fmt.Sprintf("unknown worktrees command %q", command))
|
|
}
|
|
options, help, err := parseWorktreeCommandArgs(args)
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
if help {
|
|
if err := writeWorktreesHelp(stdout); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
workspaceRoot, err := resolveWorkspaceRoot(options.cwd, deps)
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
result, err := deps.prepareWorktree(context.Background(), worktrees.Options{
|
|
Cwd: workspaceRoot,
|
|
Name: options.name,
|
|
BaseDir: options.baseDir,
|
|
Now: deps.now,
|
|
})
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
safeResult := redactWorktreeResult(result)
|
|
if options.json {
|
|
if err := writePrettyJSON(stdout, safeResult); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
if _, err := fmt.Fprintln(stdout, formatWorktreeResult(safeResult)); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
|
|
func runVerifyCommand(args []string, stdout io.Writer, stderr io.Writer, deps appDeps) int {
|
|
options, help, err := parseVerifyCommandArgs(args)
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
if help {
|
|
if err := writeVerifyHelp(stdout); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
workspaceRoot, err := resolveWorkspaceRoot(options.cwd, deps)
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
plan, err := deps.detectVerifyPlan(workspaceRoot)
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
if options.attempts > 1 {
|
|
loopReport := deps.runSelfVerify(context.Background(), plan, selfverify.Options{
|
|
RunOptions: verify.RunOptions{
|
|
Only: options.only,
|
|
TimeoutMS: options.timeoutMS,
|
|
Now: deps.now,
|
|
},
|
|
MaxAttempts: options.attempts,
|
|
})
|
|
safeLoopReport := redactVerifyLoopReport(loopReport)
|
|
if options.json {
|
|
if err := writePrettyJSON(stdout, selfverify.SnapshotFromReport(safeLoopReport)); err != nil {
|
|
return exitCrash
|
|
}
|
|
} else if _, err := fmt.Fprintln(stdout, formatVerifyLoopReport(safeLoopReport)); err != nil {
|
|
return exitCrash
|
|
}
|
|
if !loopReport.OK {
|
|
return exitProvider
|
|
}
|
|
return exitSuccess
|
|
}
|
|
report := deps.runVerify(context.Background(), plan, verify.RunOptions{
|
|
Only: options.only,
|
|
TimeoutMS: options.timeoutMS,
|
|
Now: deps.now,
|
|
})
|
|
safeReport := redactVerifyReport(report)
|
|
if options.json {
|
|
if err := writePrettyJSON(stdout, verify.SnapshotFromReport(safeReport)); err != nil {
|
|
return exitCrash
|
|
}
|
|
} else if _, err := fmt.Fprintln(stdout, formatVerifyReport(safeReport)); err != nil {
|
|
return exitCrash
|
|
}
|
|
if !report.OK {
|
|
return exitProvider
|
|
}
|
|
return exitSuccess
|
|
}
|
|
|
|
func runChanges(args []string, stdout io.Writer, stderr io.Writer, deps appDeps) int {
|
|
command := "inspect"
|
|
if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
|
command = strings.ToLower(strings.TrimSpace(args[0]))
|
|
args = args[1:]
|
|
}
|
|
if command == "help" {
|
|
if err := writeChangesHelp(stdout); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
options, help, err := parseChangesArgs(args, command)
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
if help {
|
|
if err := writeChangesHelp(stdout); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
workspaceRoot, err := resolveWorkspaceRoot(options.cwd, deps)
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
switch command {
|
|
case "inspect", "status":
|
|
summary, err := deps.inspectChanges(context.Background(), zerogit.InspectOptions{
|
|
Cwd: workspaceRoot,
|
|
BaseRef: options.baseRef,
|
|
MaxDiffBytes: options.maxDiffBytes,
|
|
})
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
safeSummary := redactChangeSummary(summary)
|
|
if options.json {
|
|
if err := writePrettyJSON(stdout, zerogit.SnapshotFromSummary(safeSummary)); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
if _, err := fmt.Fprintln(stdout, formatChangeSummary(safeSummary)); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
case "commit":
|
|
result, err := deps.commitChanges(context.Background(), zerogit.CommitOptions{
|
|
Cwd: workspaceRoot,
|
|
Message: options.message,
|
|
DryRun: options.dryRun,
|
|
MaxDiffBytes: options.maxDiffBytes,
|
|
})
|
|
if err != nil {
|
|
return writeExecUsageError(stderr, err.Error())
|
|
}
|
|
safeResult := redactCommitResult(result)
|
|
if options.json {
|
|
if err := writePrettyJSON(stdout, safeResult); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
}
|
|
if _, err := fmt.Fprintln(stdout, formatCommitResult(safeResult)); err != nil {
|
|
return exitCrash
|
|
}
|
|
return exitSuccess
|
|
default:
|
|
return writeExecUsageError(stderr, fmt.Sprintf("unknown changes command %q", command))
|
|
}
|
|
}
|
|
|
|
func parseWorktreeCommandArgs(args []string) (worktreeCommandOptions, bool, error) {
|
|
options := worktreeCommandOptions{}
|
|
for index := 0; index < len(args); index++ {
|
|
arg := args[index]
|
|
switch {
|
|
case arg == "-h" || arg == "--help" || arg == "help":
|
|
return options, true, nil
|
|
case arg == "--json":
|
|
options.json = true
|
|
case arg == "--name":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
if err := setWorktreeName(&options, value); err != nil {
|
|
return options, false, err
|
|
}
|
|
index = next
|
|
case strings.HasPrefix(arg, "--name="):
|
|
if err := setWorktreeName(&options, strings.TrimPrefix(arg, "--name=")); err != nil {
|
|
return options, false, err
|
|
}
|
|
case arg == "--dir":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.baseDir = value
|
|
index = next
|
|
case strings.HasPrefix(arg, "--dir="):
|
|
options.baseDir = strings.TrimSpace(strings.TrimPrefix(arg, "--dir="))
|
|
case arg == "-C" || arg == "--cwd":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.cwd = value
|
|
index = next
|
|
case strings.HasPrefix(arg, "--cwd="):
|
|
options.cwd = strings.TrimSpace(strings.TrimPrefix(arg, "--cwd="))
|
|
case strings.HasPrefix(arg, "-"):
|
|
return options, false, execUsageError{fmt.Sprintf("unknown worktrees flag %q", arg)}
|
|
default:
|
|
if err := setWorktreeName(&options, arg); err != nil {
|
|
return options, false, err
|
|
}
|
|
}
|
|
}
|
|
return options, false, nil
|
|
}
|
|
|
|
func setWorktreeName(options *worktreeCommandOptions, value string) error {
|
|
name := strings.TrimSpace(value)
|
|
if name == "" {
|
|
return nil
|
|
}
|
|
if options.name != "" {
|
|
return execUsageError{"worktree name was provided more than once"}
|
|
}
|
|
options.name = name
|
|
return nil
|
|
}
|
|
|
|
func parseVerifyCommandArgs(args []string) (verifyCommandOptions, bool, error) {
|
|
options := verifyCommandOptions{}
|
|
for index := 0; index < len(args); index++ {
|
|
arg := args[index]
|
|
switch {
|
|
case arg == "-h" || arg == "--help" || arg == "help":
|
|
return options, true, nil
|
|
case arg == "--json":
|
|
options.json = true
|
|
case arg == "-C" || arg == "--cwd":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.cwd = value
|
|
index = next
|
|
case strings.HasPrefix(arg, "--cwd="):
|
|
options.cwd = strings.TrimSpace(strings.TrimPrefix(arg, "--cwd="))
|
|
case arg == "--only":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.only = append(options.only, parseToolList(value)...)
|
|
index = next
|
|
case strings.HasPrefix(arg, "--only="):
|
|
options.only = append(options.only, parseToolList(strings.TrimSpace(strings.TrimPrefix(arg, "--only=")))...)
|
|
case arg == "--timeout-ms":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
timeoutMS, err := parsePositiveIntFlag("--timeout-ms", value)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.timeoutMS = timeoutMS
|
|
index = next
|
|
case strings.HasPrefix(arg, "--timeout-ms="):
|
|
timeoutMS, err := parsePositiveIntFlag("--timeout-ms", strings.TrimSpace(strings.TrimPrefix(arg, "--timeout-ms=")))
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.timeoutMS = timeoutMS
|
|
case arg == "--attempts":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
attempts, err := parsePositiveIntFlag("--attempts", value)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.attempts = attempts
|
|
index = next
|
|
case strings.HasPrefix(arg, "--attempts="):
|
|
attempts, err := parsePositiveIntFlag("--attempts", strings.TrimSpace(strings.TrimPrefix(arg, "--attempts=")))
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.attempts = attempts
|
|
case strings.HasPrefix(arg, "-"):
|
|
return options, false, execUsageError{fmt.Sprintf("unknown verify flag %q", arg)}
|
|
default:
|
|
return options, false, execUsageError{fmt.Sprintf("unexpected verify argument %q", arg)}
|
|
}
|
|
}
|
|
return options, false, nil
|
|
}
|
|
|
|
func parseChangesArgs(args []string, command string) (changesCommandOptions, bool, error) {
|
|
options := changesCommandOptions{}
|
|
for index := 0; index < len(args); index++ {
|
|
arg := args[index]
|
|
switch {
|
|
case arg == "-h" || arg == "--help" || arg == "help":
|
|
return options, true, nil
|
|
case arg == "--json":
|
|
options.json = true
|
|
case arg == "--dry-run":
|
|
options.dryRun = true
|
|
case arg == "-C" || arg == "--cwd":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.cwd = value
|
|
index = next
|
|
case strings.HasPrefix(arg, "--cwd="):
|
|
options.cwd = strings.TrimSpace(strings.TrimPrefix(arg, "--cwd="))
|
|
case arg == "--base":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.baseRef = strings.TrimSpace(value)
|
|
index = next
|
|
case strings.HasPrefix(arg, "--base="):
|
|
v := strings.TrimSpace(strings.TrimPrefix(arg, "--base="))
|
|
if v == "" || flagValueLooksLikeOption(v) {
|
|
return options, false, execUsageError{"--base requires a value"}
|
|
}
|
|
options.baseRef = v
|
|
case arg == "-m" || arg == "--message":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.message = value
|
|
index = next
|
|
case strings.HasPrefix(arg, "--message="):
|
|
options.message = strings.TrimSpace(strings.TrimPrefix(arg, "--message="))
|
|
case arg == "--diff-bytes":
|
|
value, next, err := nextFlagValue(args, index, arg)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
maxDiffBytes, err := parsePositiveIntFlag("--diff-bytes", value)
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.maxDiffBytes = maxDiffBytes
|
|
index = next
|
|
case strings.HasPrefix(arg, "--diff-bytes="):
|
|
maxDiffBytes, err := parsePositiveIntFlag("--diff-bytes", strings.TrimSpace(strings.TrimPrefix(arg, "--diff-bytes=")))
|
|
if err != nil {
|
|
return options, false, err
|
|
}
|
|
options.maxDiffBytes = maxDiffBytes
|
|
case strings.HasPrefix(arg, "-"):
|
|
return options, false, execUsageError{fmt.Sprintf("unknown changes flag %q", arg)}
|
|
default:
|
|
return options, false, execUsageError{fmt.Sprintf("unexpected changes argument %q", arg)}
|
|
}
|
|
}
|
|
if command != "commit" && (options.message != "" || options.dryRun) {
|
|
return options, false, execUsageError{"--message and --dry-run are only valid with `zero changes commit`"}
|
|
}
|
|
if command == "commit" && options.baseRef != "" {
|
|
return options, false, execUsageError{"--base is only valid with `zero changes inspect`"}
|
|
}
|
|
return options, false, nil
|
|
}
|
|
|
|
func parsePositiveIntFlag(flag string, value string) (int, error) {
|
|
trimmed := strings.TrimSpace(value)
|
|
if trimmed == "" {
|
|
return 0, execUsageError{flag + " requires a value"}
|
|
}
|
|
number, err := strconv.Atoi(trimmed)
|
|
if err != nil || number <= 0 {
|
|
return 0, execUsageError{fmt.Sprintf("invalid %s %q. Expected a positive integer.", flag, value)}
|
|
}
|
|
return number, nil
|
|
}
|
|
|
|
func redactWorktreeResult(result worktrees.Result) worktrees.Result {
|
|
result.Name = redactCLIString(result.Name)
|
|
result.Path = redactCLIString(result.Path)
|
|
result.RepoRoot = redactCLIString(result.RepoRoot)
|
|
result.SourceBranch = redactCLIString(result.SourceBranch)
|
|
result.SourceCommit = redactCLIString(result.SourceCommit)
|
|
return result
|
|
}
|
|
|
|
func redactVerifyReport(report verify.Report) verify.Report {
|
|
report.Root = redactCLIString(report.Root)
|
|
report.Results = append([]verify.Result{}, report.Results...)
|
|
for index := range report.Results {
|
|
report.Results[index].Stdout = redactCLIString(report.Results[index].Stdout)
|
|
report.Results[index].Stderr = redactCLIString(report.Results[index].Stderr)
|
|
report.Results[index].Error = redactCLIString(report.Results[index].Error)
|
|
if report.Results[index].OutputSummary != nil {
|
|
summary := *report.Results[index].OutputSummary
|
|
summary.Lines = append([]string{}, summary.Lines...)
|
|
for lineIndex := range summary.Lines {
|
|
summary.Lines[lineIndex] = redactCLIString(summary.Lines[lineIndex])
|
|
}
|
|
report.Results[index].OutputSummary = &summary
|
|
}
|
|
if report.Results[index].TestSummary != nil {
|
|
summary := *report.Results[index].TestSummary
|
|
summary.Failures = append([]testrunner.Failure{}, summary.Failures...)
|
|
for failureIndex := range summary.Failures {
|
|
summary.Failures[failureIndex].Name = redactCLIString(summary.Failures[failureIndex].Name)
|
|
summary.Failures[failureIndex].File = redactCLIString(summary.Failures[failureIndex].File)
|
|
summary.Failures[failureIndex].Message = redactCLIString(summary.Failures[failureIndex].Message)
|
|
}
|
|
report.Results[index].TestSummary = &summary
|
|
}
|
|
}
|
|
return report
|
|
}
|
|
|
|
func redactVerifyLoopReport(report selfverify.Report) selfverify.Report {
|
|
report.Root = redactCLIString(report.Root)
|
|
report.Error = redactCLIString(report.Error)
|
|
for index := range report.Attempts {
|
|
report.Attempts[index].Report = redactVerifyReport(report.Attempts[index].Report)
|
|
if report.Attempts[index].Remediation != nil {
|
|
remediation := *report.Attempts[index].Remediation
|
|
remediation.StartedAt = redactCLIString(remediation.StartedAt)
|
|
remediation.EndedAt = redactCLIString(remediation.EndedAt)
|
|
remediation.Message = redactCLIString(remediation.Message)
|
|
remediation.Error = redactCLIString(remediation.Error)
|
|
report.Attempts[index].Remediation = &remediation
|
|
}
|
|
}
|
|
return report
|
|
}
|
|
|
|
func redactChangeSummary(summary zerogit.ChangeSummary) zerogit.ChangeSummary {
|
|
summary.Root = redactCLIString(summary.Root)
|
|
summary.Base = redactCLIString(summary.Base)
|
|
summary.Branch = redactCLIString(summary.Branch)
|
|
summary.Commit = redactCLIString(summary.Commit)
|
|
summary.DiffStat = redactCLIString(summary.DiffStat)
|
|
summary.Diff = redactCLIString(summary.Diff)
|
|
for index := range summary.Files {
|
|
summary.Files[index].Path = redactCLIString(summary.Files[index].Path)
|
|
summary.Files[index].Status = redactCLIString(summary.Files[index].Status)
|
|
}
|
|
return summary
|
|
}
|
|
|
|
func redactCommitResult(result zerogit.CommitResult) zerogit.CommitResult {
|
|
result.Root = redactCLIString(result.Root)
|
|
result.Message = redactCLIString(result.Message)
|
|
result.CommitHash = redactCLIString(result.CommitHash)
|
|
result.Before = redactChangeSummary(result.Before)
|
|
return result
|
|
}
|
|
|
|
func redactCLIString(value string) string {
|
|
// Keep ordinary paths visible; these commands report useful locations.
|
|
// Central redaction still removes secret-looking tokens embedded in paths.
|
|
return redaction.RedactString(value, redaction.Options{})
|
|
}
|
|
|
|
func formatWorktreeResult(result worktrees.Result) string {
|
|
lines := []string{
|
|
"Zero worktree ready",
|
|
"name: " + result.Name,
|
|
"path: " + result.Path,
|
|
"repo: " + result.RepoRoot,
|
|
}
|
|
if result.SourceBranch != "" {
|
|
lines = append(lines, "branch: "+result.SourceBranch)
|
|
}
|
|
if result.SourceCommit != "" {
|
|
lines = append(lines, "commit: "+result.SourceCommit)
|
|
}
|
|
if result.Reused {
|
|
lines = append(lines, "reused: true")
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func formatVerifyReport(report verify.Report) string {
|
|
lines := []string{
|
|
"Zero verification",
|
|
"root: " + report.Root,
|
|
fmt.Sprintf("summary: %d total, %d passed, %d failed, %d errors", report.Summary.Total, report.Summary.Passed, report.Summary.Failed, report.Summary.Errors),
|
|
}
|
|
if len(report.Results) == 0 {
|
|
lines = append(lines, " (no checks detected)")
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
for _, result := range report.Results {
|
|
lines = append(lines, fmt.Sprintf(" [%s] %s - %s", result.Status, result.ID, strings.Join(result.Command, " ")))
|
|
if result.TestSummary != nil {
|
|
lines = append(lines, formatVerifyTestSummary(result.TestSummary))
|
|
for _, failure := range result.TestSummary.Failures {
|
|
if failure.Name == "" {
|
|
continue
|
|
}
|
|
detail := failure.Name
|
|
if failure.File != "" {
|
|
detail += " at " + failure.File
|
|
}
|
|
lines = append(lines, " failure: "+detail)
|
|
}
|
|
}
|
|
if result.Error != "" {
|
|
lines = append(lines, " error: "+result.Error)
|
|
}
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func formatVerifyTestSummary(summary *testrunner.Summary) string {
|
|
line := fmt.Sprintf(" tests: %d total, %d passed, %d failed", summary.Total, summary.Passed, summary.Failed)
|
|
if summary.Skipped > 0 {
|
|
line += fmt.Sprintf(", %d skipped", summary.Skipped)
|
|
}
|
|
return line
|
|
}
|
|
|
|
func formatVerifyLoopReport(report selfverify.Report) string {
|
|
lines := []string{
|
|
"Zero self-verification",
|
|
}
|
|
if report.Root != "" {
|
|
lines = append(lines, "root: "+report.Root)
|
|
}
|
|
lines = append(lines,
|
|
fmt.Sprintf("attempts: %d", len(report.Attempts)),
|
|
"stop: "+string(report.StopReason),
|
|
fmt.Sprintf("summary: %d total, %d passed, %d failed, %d errors", report.Summary.Total, report.Summary.Passed, report.Summary.Failed, report.Summary.Errors),
|
|
)
|
|
for _, attempt := range report.Attempts {
|
|
status := "failed"
|
|
if attempt.Report.OK {
|
|
status = "passed"
|
|
}
|
|
lines = append(lines, fmt.Sprintf(" attempt %d: %s", attempt.Number, status))
|
|
if attempt.Remediation != nil {
|
|
lines = append(lines, " remediation: "+formatRemediation(*attempt.Remediation))
|
|
}
|
|
}
|
|
if report.Error != "" {
|
|
lines = append(lines, "error: "+report.Error)
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func formatRemediation(remediation selfverify.Remediation) string {
|
|
status := "not applied"
|
|
if remediation.Applied {
|
|
status = "applied"
|
|
}
|
|
details := []string{status}
|
|
if remediation.Message != "" {
|
|
details = append(details, remediation.Message)
|
|
}
|
|
if remediation.Error != "" {
|
|
details = append(details, "error: "+remediation.Error)
|
|
}
|
|
return strings.Join(details, " - ")
|
|
}
|
|
|
|
func formatChangeSummary(summary zerogit.ChangeSummary) string {
|
|
lines := []string{
|
|
"Zero changes",
|
|
"root: " + summary.Root,
|
|
fmt.Sprintf("files: %d changed", len(summary.Files)),
|
|
}
|
|
if summary.Branch != "" {
|
|
lines = append(lines, "branch: "+summary.Branch)
|
|
}
|
|
if summary.Base != "" {
|
|
lines = append(lines, "base: "+summary.Base)
|
|
}
|
|
if summary.Commit != "" {
|
|
lines = append(lines, "commit: "+summary.Commit)
|
|
}
|
|
if summary.Clean {
|
|
lines = append(lines, "clean: true")
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
for _, file := range summary.Files {
|
|
lines = append(lines, fmt.Sprintf(" [%s] %s", file.Status, file.Path))
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func formatCommitResult(result zerogit.CommitResult) string {
|
|
lines := []string{
|
|
"Zero changes commit",
|
|
"root: " + result.Root,
|
|
"message: " + result.Message,
|
|
fmt.Sprintf("dry-run: %t", result.DryRun),
|
|
fmt.Sprintf("committed: %t", result.Committed),
|
|
fmt.Sprintf("files: %d changed", len(result.Before.Files)),
|
|
}
|
|
if result.CommitHash != "" {
|
|
lines = append(lines, "commit: "+result.CommitHash)
|
|
}
|
|
return strings.Join(lines, "\n")
|
|
}
|
|
|
|
func writeWorktreesHelp(w io.Writer) error {
|
|
_, err := fmt.Fprint(w, `Usage:
|
|
zero worktrees prepare [flags] [name]
|
|
|
|
Prepares an isolated git worktree for a Zero task.
|
|
|
|
Flags:
|
|
--name <name> Worktree name; defaults to a timestamped task name
|
|
--dir <path> Base directory for Zero worktrees
|
|
-C, --cwd <path> Source repository directory
|
|
--json Print JSON output
|
|
-h, --help Show this help
|
|
`)
|
|
return err
|
|
}
|
|
|
|
func writeVerifyHelp(w io.Writer) error {
|
|
_, err := fmt.Fprint(w, `Usage:
|
|
zero verify [flags]
|
|
|
|
Detects and runs local verification checks for the workspace.
|
|
|
|
Flags:
|
|
-C, --cwd <path> Workspace directory
|
|
--only <ids> Run only matching check ids
|
|
--timeout-ms <n> Per-check timeout in milliseconds
|
|
--attempts <n> Run a bounded self-verification loop
|
|
--json Print JSON output
|
|
-h, --help Show this help
|
|
`)
|
|
return err
|
|
}
|
|
|
|
func writeChangesHelp(w io.Writer) error {
|
|
_, err := fmt.Fprint(w, `Usage:
|
|
zero changes inspect [flags]
|
|
zero changes commit [flags]
|
|
|
|
Inspects local git changes and optionally creates a commit.
|
|
|
|
Flags:
|
|
-C, --cwd <path> Workspace directory
|
|
--base <ref> Diff against <ref>...HEAD instead of the working tree
|
|
--diff-bytes <n> Maximum diff bytes to include
|
|
-m, --message <text> Commit message for `+"`zero changes commit`"+`
|
|
--dry-run Preview commit metadata without mutating git state
|
|
--json Print JSON output
|
|
-h, --help Show this help
|
|
`)
|
|
return err
|
|
}
|