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.
This commit is contained in:
+13
-1
@@ -146,6 +146,18 @@ func envOr(key, value string) string {
|
|||||||
return value
|
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.
|
// parse allows flags before or after positional arguments without reinterpreting values.
|
||||||
func parse(f *flag.FlagSet, args []string) error {
|
func parse(f *flag.FlagSet, args []string) error {
|
||||||
var flags, positionals []string
|
var flags, positionals []string
|
||||||
@@ -809,7 +821,7 @@ func (a App) Run(ctx context.Context, args []string) error {
|
|||||||
return a.output(r.RunDetail)
|
return a.output(r.RunDetail)
|
||||||
}
|
}
|
||||||
line := fmt.Sprintf("Decided run #%d: %s · %s", r.RunDetail.ID, option, r.RunDetail.Status)
|
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
|
line += " · feedback: " + feedback
|
||||||
}
|
}
|
||||||
_, err = fmt.Fprintln(a.Out, line)
|
_, err = fmt.Fprintln(a.Out, line)
|
||||||
|
|||||||
@@ -79,6 +79,20 @@ func TestCLIDecideWaitingHumanRun(t *testing.T) {
|
|||||||
t.Fatalf("derived verdict missing the decision: %+v", gate.Verdict)
|
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 {
|
if _, err := run("help", "decide"); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,
|
// stays in the output only - never the step diagnostic - so a deliberate,
|
||||||
// successful decision is not presented as a stop or error; the derived
|
// successful decision is not presented as a stop or error; the derived
|
||||||
// verdict reads the feedback as the step's reason.
|
// 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)
|
encoded, err := json.Marshal(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return protocol.RunStep{}, false, err
|
return protocol.RunStep{}, false, err
|
||||||
|
|||||||
@@ -37,6 +37,14 @@ const (
|
|||||||
WaitingOptionsKey = "options"
|
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
|
// NewWaitingEvidence builds the self-contained evidence a human stage persists
|
||||||
// at wait time: the operator instructions and the ordered options to choose.
|
// at wait time: the operator instructions and the ordered options to choose.
|
||||||
func NewWaitingEvidence(prompt string, options []string) map[string]any {
|
func NewWaitingEvidence(prompt string, options []string) map[string]any {
|
||||||
@@ -72,6 +80,15 @@ func WaitingOptions(output map[string]any) []string {
|
|||||||
return nil
|
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 {
|
type SessionEvidence struct {
|
||||||
Sequence int `json:"sequence"`
|
Sequence int `json:"sequence"`
|
||||||
SessionID string `json:"session_id"`
|
SessionID string `json:"session_id"`
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ func TestPiEnvironmentCarriesArtifactsDirectory(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestPiEnvironmentOmitsEmptyArtifactsDirectory(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", "") {
|
for _, entry := range piEnvironment("/tmp/ref", "") {
|
||||||
if strings.HasPrefix(entry, ArtifactsEnv+"=") {
|
if strings.HasPrefix(entry, ArtifactsEnv+"=") {
|
||||||
t.Fatalf("unexpected artifacts entry %q", entry)
|
t.Fatalf("unexpected artifacts entry %q", entry)
|
||||||
|
|||||||
+14
-2
@@ -312,9 +312,21 @@ func (c *PiClient) Generate(ctx context.Context, config ModelConfig, prompt stri
|
|||||||
}
|
}
|
||||||
|
|
||||||
// piEnvironment builds the spawned Pi process environment, including the
|
// 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 {
|
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_POLICY="+filepath.Join(reference, "policy.json"),
|
||||||
"PIPELINES_PI_LEASE="+filepath.Join(reference, "lease"),
|
"PIPELINES_PI_LEASE="+filepath.Join(reference, "lease"),
|
||||||
"PIPELINES_PI_STOPPED="+filepath.Join(reference, "quiescent"))
|
"PIPELINES_PI_STOPPED="+filepath.Join(reference, "quiescent"))
|
||||||
|
|||||||
Reference in New Issue
Block a user