From 98bbc2deacc94e050d2d25f2e2a78e95f2c416e9 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 17:25:48 +0100 Subject: [PATCH] Report recorded decision evidence and harden the Pi artifacts env Refinements on top of the human decision stage (a6aff47): - `pipelines decide` now reports the feedback the server actually stored for the chosen option, so a repeated (idempotent) decision shows the recorded evidence instead of the ignored request text. A repeated decision is covered by a new assertion that also checks the step output does not change. - The decision option/feedback output keys are defined once in protocol (DecisionOptionKey/DecisionFeedbackKey) with a DecisionEvidence reader, and the store and CLI use them instead of bare string literals. - piEnvironment drops any inherited PIPELINES_ARTIFACTS_DIR before spawning Pi, so the artifactsDir argument is authoritative and an ambient directory (for example Pipelines' own when a stage runs inside another stage) never leaks into a child that was given none. Covered by a test with an ambient value set. --- internal/cli/app.go | 14 +++++++++++++- internal/cli/decide_test.go | 14 ++++++++++++++ internal/pipeline/runs.go | 2 +- internal/protocol/runs.go | 17 +++++++++++++++++ internal/stage/artifacts_test.go | 4 ++++ internal/stage/pi.go | 16 ++++++++++++++-- 6 files changed, 63 insertions(+), 4 deletions(-) diff --git a/internal/cli/app.go b/internal/cli/app.go index d3ea5df8..ce92d259 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -146,6 +146,18 @@ func envOr(key, value string) string { return value } +// recordedFeedback returns the feedback the server actually stored for the +// decided step that carries the chosen option, so a repeated (idempotent) +// decision reports the recorded evidence rather than the ignored request text. +func recordedFeedback(detail *protocol.RunDetail, option string) string { + for i := len(detail.Steps) - 1; i >= 0; i-- { + if chosen, feedback := protocol.DecisionEvidence(detail.Steps[i].Output); chosen == option { + return strings.TrimSpace(feedback) + } + } + return "" +} + // parse allows flags before or after positional arguments without reinterpreting values. func parse(f *flag.FlagSet, args []string) error { var flags, positionals []string @@ -809,7 +821,7 @@ func (a App) Run(ctx context.Context, args []string) error { return a.output(r.RunDetail) } line := fmt.Sprintf("Decided run #%d: %s · %s", r.RunDetail.ID, option, r.RunDetail.Status) - if feedback := strings.TrimSpace(o.feedback); feedback != "" { + if feedback := recordedFeedback(r.RunDetail, option); feedback != "" { line += " · feedback: " + feedback } _, err = fmt.Fprintln(a.Out, line) diff --git a/internal/cli/decide_test.go b/internal/cli/decide_test.go index 57011aac..98d1d7af 100644 --- a/internal/cli/decide_test.go +++ b/internal/cli/decide_test.go @@ -79,6 +79,20 @@ func TestCLIDecideWaitingHumanRun(t *testing.T) { t.Fatalf("derived verdict missing the decision: %+v", gate.Verdict) } + // A repeated decision must not double-apply and must report the recorded + // evidence, not the ignored request text. + repeat, err := run("decide", strconv.FormatInt(runID, 10), "gate", "--option", "go", "--feedback", "again") + if err != nil { + t.Fatal(err) + } + if strings.Contains(repeat, "again") || !strings.Contains(repeat, "approved") { + t.Fatalf("repeated decision reported the request, not the recorded evidence: %q", repeat) + } + after := waitFor("succeeded") + if len(after.Steps) != 1 || after.Steps[0].Output["feedback"] != "approved" { + t.Fatalf("repeated decision changed the recorded evidence: %+v", after.Steps) + } + if _, err := run("help", "decide"); err != nil { t.Fatal(err) } diff --git a/internal/pipeline/runs.go b/internal/pipeline/runs.go index 59cf476d..3b6d4887 100644 --- a/internal/pipeline/runs.go +++ b/internal/pipeline/runs.go @@ -689,7 +689,7 @@ JOIN pipeline_versions v ON v.pipeline_id=r.pipeline_id AND v.version=r.version // stays in the output only - never the step diagnostic - so a deliberate, // successful decision is not presented as a stop or error; the derived // verdict reads the feedback as the step's reason. - output := map[string]any{"option": option, "feedback": feedback} + output := map[string]any{protocol.DecisionOptionKey: option, protocol.DecisionFeedbackKey: feedback} encoded, err := json.Marshal(output) if err != nil { return protocol.RunStep{}, false, err diff --git a/internal/protocol/runs.go b/internal/protocol/runs.go index f9ee7e27..02ab595c 100644 --- a/internal/protocol/runs.go +++ b/internal/protocol/runs.go @@ -37,6 +37,14 @@ const ( WaitingOptionsKey = "options" ) +// Decision evidence keys. A decided human step records the operator's chosen +// option and optional feedback in its output, so the choice stays visible in +// inspect and the TUI. They are the one definition of those keys. +const ( + DecisionOptionKey = "option" + DecisionFeedbackKey = "feedback" +) + // NewWaitingEvidence builds the self-contained evidence a human stage persists // at wait time: the operator instructions and the ordered options to choose. func NewWaitingEvidence(prompt string, options []string) map[string]any { @@ -72,6 +80,15 @@ func WaitingOptions(output map[string]any) []string { return nil } +// DecisionEvidence reads the recorded choice and feedback from a decided human +// step's output, or empty strings when none were recorded. It is the read side +// of DecisionOptionKey/DecisionFeedbackKey. +func DecisionEvidence(output map[string]any) (option, feedback string) { + option, _ = output[DecisionOptionKey].(string) + feedback, _ = output[DecisionFeedbackKey].(string) + return option, feedback +} + type SessionEvidence struct { Sequence int `json:"sequence"` SessionID string `json:"session_id"` diff --git a/internal/stage/artifacts_test.go b/internal/stage/artifacts_test.go index a30ca152..01652724 100644 --- a/internal/stage/artifacts_test.go +++ b/internal/stage/artifacts_test.go @@ -17,6 +17,10 @@ func TestPiEnvironmentCarriesArtifactsDirectory(t *testing.T) { } func TestPiEnvironmentOmitsEmptyArtifactsDirectory(t *testing.T) { + // An ambient artifacts directory (for example Pipelines' own when a stage + // runs inside another stage) must not leak into a Pi child that was given + // none: the argument is authoritative. + t.Setenv(ArtifactsEnv, "/ambient/artifacts/leak") for _, entry := range piEnvironment("/tmp/ref", "") { if strings.HasPrefix(entry, ArtifactsEnv+"=") { t.Fatalf("unexpected artifacts entry %q", entry) diff --git a/internal/stage/pi.go b/internal/stage/pi.go index 78f38c2e..c418bb57 100644 --- a/internal/stage/pi.go +++ b/internal/stage/pi.go @@ -312,9 +312,21 @@ func (c *PiClient) Generate(ctx context.Context, config ModelConfig, prompt stri } // piEnvironment builds the spawned Pi process environment, including the -// per-stage artifacts directory when the runtime provided one. +// per-stage artifacts directory when the runtime provided one. Any +// PIPELINES_ARTIFACTS_DIR inherited from the process that spawns Pi is dropped +// first: the artifactsDir argument is authoritative, so an absent directory is +// never silently replaced by an ambient one (for example Pipelines' own +// PIPELINES_ARTIFACTS_DIR when a stage runs inside another stage). func piEnvironment(reference, artifactsDir string) []string { - env := append(os.Environ(), "PI_OFFLINE=1", + inherited := os.Environ() + env := make([]string, 0, len(inherited)+4) + for _, entry := range inherited { + if strings.HasPrefix(entry, ArtifactsEnv+"=") { + continue + } + env = append(env, entry) + } + env = append(env, "PI_OFFLINE=1", "PIPELINES_PI_POLICY="+filepath.Join(reference, "policy.json"), "PIPELINES_PI_LEASE="+filepath.Join(reference, "lease"), "PIPELINES_PI_STOPPED="+filepath.Join(reference, "quiescent"))