Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Please choose versions by [Semantic Versioning](http://semver.org/).
* MINOR version when you add functionality in a backwards-compatible manner, and
* PATCH version when you make backwards-compatible bug fixes.

## Unreleased

- fix: refuse `--force` on `task complete` and `goal complete` when stdin is not a terminal, so an agent running without a terminal cannot bypass the closing check with it; the extracted task-complete helper now prints warnings in both branches and wraps the session-mode error

## v0.198.0

- feat: `task approve` writes `reapproved_at` on its re-approval branch, so a re-approval is observable and timestamped where the preserved `approved_at` made it indistinguishable from a row that was never re-approved
Expand Down
9 changes: 4 additions & 5 deletions integration/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1987,7 +1987,7 @@ This is my task.
cleanup()
})

It("completes with --force despite incomplete subtasks and without close-out fields", func() {
It("refuses --force when stdin is not a terminal and leaves the task unchanged", func() {
cmd := exec.Command(
binPath,
"--config", configPath,
Expand All @@ -1996,14 +1996,13 @@ This is my task.
)
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
Eventually(session).Should(gexec.Exit(0))
Eventually(session).Should(gexec.Exit(1))
Expect(session.Err).To(gbytes.Say("stdin is not a terminal"))

taskPath := filepath.Join(vaultPath, "Tasks", "my-task.md")
content, err := os.ReadFile(taskPath) //#nosec G304 -- test file
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(ContainSubstring("status: completed"))
Expect(string(content)).NotTo(ContainSubstring("aborted_reason:"))
Expect(string(content)).NotTo(ContainSubstring("gate_successor:"))
Expect(string(content)).To(Equal("---\nstatus: in_progress\n---\n# My Task\n\n- [ ] not done\n"))
})
})
})
Expand Down
59 changes: 51 additions & 8 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
Expand Down Expand Up @@ -248,6 +249,9 @@ func createCompleteCommand(
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
taskName := args[0]
if err := requireForceFromTerminal(ctx, force); err != nil {
return err
}
vaults, err := getVaults(ctx, configLoader, vaultName)
if err != nil {
return errors.Wrap(ctx, err, "get vaults")
Expand Down Expand Up @@ -278,14 +282,7 @@ func createCompleteCommand(
return result, err
}
if !OutputFormat(*outputFormat).IsJSON() {
if result.Reason != "" {
fmt.Printf("⚠️ Cannot complete: %s\n", result.Reason)
} else {
fmt.Printf("✅ Task completed: %s\n", result.Name)
}
for _, w := range result.Warnings {
fmt.Printf("⚠️ %s\n", w)
}
printTaskCompleteResult(os.Stdout, result)
}
return result, nil
},
Expand All @@ -300,6 +297,21 @@ func createCompleteCommand(
return cmd
}

// printTaskCompleteResult writes the human-readable outcome of a task
// completion. runMutation handles the JSON case, so this is plain mode only.
// Warnings print in both the refusal and the success branch: the refusal
// branch used to return early and drop them.
func printTaskCompleteResult(w io.Writer, result ops.MutationResult) {
if result.Reason != "" {
fmt.Fprintf(w, "⚠️ Cannot complete: %s\n", result.Reason)
} else {
fmt.Fprintf(w, "✅ Task completed: %s\n", result.Name)
}
for _, warning := range result.Warnings {
fmt.Fprintf(w, "⚠️ %s\n", warning)
}
}

func createDeferCommand(
ctx context.Context,
configLoader *config.Loader,
Expand Down Expand Up @@ -534,6 +546,34 @@ func resolveSessionMode(ctx context.Context, mode string) (bool, error) {
}
}

// requireForceFromTerminal refuses --force when stdin is not a terminal.
// A forced complete overrides the closing check, so it must be confirmable
// by a human at a terminal; an agent piping input, or a process running with
// stdin redirected, must not be able to take the override.
func requireForceFromTerminal(ctx context.Context, force bool) error {
if !force {
return nil
}
interactive, err := resolveSessionMode(ctx, "auto")
if err != nil {
return errors.Wrap(ctx, err, "resolve session mode")
}
return requireTTYForForce(ctx, force, interactive)
}

// requireTTYForForce returns an error when force is set and isTTY is false.
// It is separate from requireForceFromTerminal so both branches are
// unit-testable without allocating a pty.
func requireTTYForForce(ctx context.Context, force bool, isTTY bool) error {
if !force || isTTY {
return nil
}
return errors.Errorf(
ctx,
"--force requires an interactive terminal: stdin is not a terminal, so the override cannot be confirmed by a human; drop --force or re-run from a terminal",
)
}

func createTaskListCommand(
ctx context.Context,
configLoader *config.Loader,
Expand Down Expand Up @@ -1483,6 +1523,9 @@ func createGoalCompleteCommand(
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
goalName := args[0]
if err := requireForceFromTerminal(ctx, force); err != nil {
return err
}
vaults, err := getVaults(ctx, configLoader, vaultName)
if err != nil {
return errors.Wrap(ctx, err, "get vaults")
Expand Down
14 changes: 14 additions & 0 deletions pkg/cli/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cli

import (
"context"
"io"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -48,6 +49,13 @@ func FormatRollupWeeklyPlainForTest(result ops.RollupWeeklyResult) string {
return formatRollupWeeklyPlain(result)
}

// PrintTaskCompleteResultForTest exposes printTaskCompleteResult with an
// explicit writer, so both output branches are testable without swapping
// os.Stdout.
func PrintTaskCompleteResultForTest(w io.Writer, result ops.MutationResult) {
printTaskCompleteResult(w, result)
}

// GetWatchVaultsForTest exposes getWatchVaults for testing.
func GetWatchVaultsForTest(
ctx context.Context,
Expand All @@ -57,6 +65,12 @@ func GetWatchVaultsForTest(
return getWatchVaults(ctx, configLoader, vaultName)
}

// RequireTTYForForceForTest exposes requireTTYForForce with an explicit TTY
// result, so both branches are tested without a pty.
func RequireTTYForForceForTest(ctx context.Context, force bool, isTTY bool) error {
return requireTTYForForce(ctx, force, isTTY)
}

// ParseOpenAnswersForTest exposes parseOpenAnswers for testing, so the flag
// grammar is pinned without spawning a binary.
func ParseOpenAnswersForTest(
Expand Down
116 changes: 116 additions & 0 deletions pkg/cli/force_tty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2026 Benjamin Borbe All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cli_test

import (
"bytes"
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"

"github.com/bborbe/vault-cli/pkg/cli"
"github.com/bborbe/vault-cli/pkg/ops"
)

// findSubcommand walks the built command tree to the named top-level command
// and then to the named subcommand.
func findSubcommand(top, sub string) *cobra.Command {
for _, candidate := range cli.NewRootCommand(context.Background()).Commands() {
if candidate.Name() != top {
continue
}
for _, nested := range candidate.Commands() {
if nested.Name() == sub {
return nested
}
}
}
return nil
}

var _ = Describe("force requires a terminal", func() {
Context("requireTTYForForce", func() {
It("refuses a forced complete when stdin is not a terminal", func() {
err := cli.RequireTTYForForceForTest(context.Background(), true, false)
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stdin is not a terminal"))
})

It("accepts a forced complete at a terminal", func() {
Expect(cli.RequireTTYForForceForTest(context.Background(), true, true)).
To(Succeed())
})

It("never gates a non-forced complete", func() {
Expect(cli.RequireTTYForForceForTest(context.Background(), false, false)).
To(Succeed())
})
})

Context("through cli.Run", func() {
It("refuses task complete --force when stdin is not a terminal", func() {
err := cli.Run(context.Background(), []string{"task", "complete", "Some Task", "--force"})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stdin is not a terminal"))
})

It("refuses goal complete --force when stdin is not a terminal", func() {
err := cli.Run(context.Background(), []string{"goal", "complete", "Some Goal", "--force"})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("stdin is not a terminal"))
})

It("does not gate a non-forced task complete", func() {
err := cli.Run(context.Background(), []string{"task", "complete", "Some Task"})
Expect(err).To(HaveOccurred(), "no task store is reachable, so this must fail before any TTY gate")
Expect(err.Error()).NotTo(ContainSubstring("stdin is not a terminal"))
})
})

Context("printTaskCompleteResult", func() {
It("prints warnings in the refusal branch", func() {
var buf bytes.Buffer
cli.PrintTaskCompleteResultForTest(&buf, ops.MutationResult{
Reason: "3 subtasks are incomplete",
Warnings: []string{"first warning", "second warning"},
})
Expect(buf.String()).To(ContainSubstring("Cannot complete: 3 subtasks are incomplete"))
Expect(buf.String()).To(ContainSubstring("first warning"))
Expect(buf.String()).To(ContainSubstring("second warning"))
})

It("prints warnings in the success branch", func() {
var buf bytes.Buffer
cli.PrintTaskCompleteResultForTest(&buf, ops.MutationResult{
Name: "Some Task",
Warnings: []string{"a warning"},
})
Expect(buf.String()).To(ContainSubstring("Task completed: Some Task"))
Expect(buf.String()).To(ContainSubstring("a warning"))
})
})

Context("flag surface", func() {
It("does not expose --force on topic complete", func() {
sub := findSubcommand("topic", "complete")
Expect(sub).NotTo(BeNil(), "the root command must expose `topic complete`")
Expect(sub.Flags().Lookup("force")).To(BeNil())
})

It("exposes --force on task complete", func() {
sub := findSubcommand("task", "complete")
Expect(sub).NotTo(BeNil(), "the root command must expose `task complete`")
Expect(sub.Flags().Lookup("force")).NotTo(BeNil())
})

It("exposes --force on goal complete", func() {
sub := findSubcommand("goal", "complete")
Expect(sub).NotTo(BeNil(), "the root command must expose `goal complete`")
Expect(sub.Flags().Lookup("force")).NotTo(BeNil())
})
})
})
4 changes: 2 additions & 2 deletions prompts/completed/283-lint-orphan-goal-block-scope.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
status: committing
status: completed
summary: 'Scoped ORPHAN_GOAL detection to the goals: block with line-bounded regexes, unescaped YAML single-quote doubling in goal names, added four regression contexts, and a CHANGELOG entry.'
execution_id: vault-cli-orphan-goal-exec-283-lint-orphan-goal-block-scope
dark-factory-version: v0.199.1
created: "2026-11-17T18:45:00Z"
queued: "2026-11-17T18:18:01Z"
started: "2026-11-17T20:44:20Z"
completed: "2026-11-17T20:58:11Z"
completed: "2026-11-18T19:52:38Z"
---

# Scope ORPHAN_GOAL detection to the `goals:` block
Expand Down
Loading
Loading