Files
pipelines/internal/pipeline/control.go
T
alan 34b8b94223 Engine: run findings ledger, findings gate, loop continuation, larger memory, loop lint, retry partial work, resume_session
- Runner-owned findings ledger per run: stages report a # findings section
  (new items and F<n> status changes); stable IDs, persisted (run_loop_state),
  in RunDetail.findings, findings.md, memory.md, inspect.
- type=findings gate stage routes open/clear on the ledger mechanically.
- retry --extra-visits N [--stage KEY] continues an exhausted loop; limits
  on_exhausted=decide parks the run on accept/continue/abort.
- Essential memory cap 20 KB, task never truncated, handoff and open findings
  ahead of the outcome ledger.
- Lint rejects limits.on outcomes on forward edges; warns on ineffective
  limits and resume_session.
- A resumed retry carries the failed visit's output.md and private/ under
  previous-attempt/.
- resume_session=true resumes a Claude stage's previous session on loop visits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 14:22:28 +00:00

112 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package pipeline
import (
"encoding/json"
"fmt"
"io"
"strings"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
func decodeControl(text string, value any) error {
// Control blocks are flat objects. Reject duplicate keys before decoding.
keys := json.NewDecoder(strings.NewReader(text))
if token, err := keys.Token(); err != nil || token != json.Delim('{') {
return fmt.Errorf("expected object")
}
seen := map[string]bool{}
for keys.More() {
token, err := keys.Token()
if err != nil {
return err
}
key, ok := token.(string)
if !ok || seen[key] {
return fmt.Errorf("duplicate key %v", token)
}
seen[key] = true
var raw json.RawMessage
if err := keys.Decode(&raw); err != nil {
return err
}
}
decoder := json.NewDecoder(strings.NewReader(text))
decoder.DisallowUnknownFields()
if err := decoder.Decode(value); err != nil {
return err
}
var extra any
if err := decoder.Decode(&extra); err != io.EOF {
return fmt.Errorf("expected one JSON value")
}
return nil
}
func parseControl(s *Stage, fields map[string]string) error {
// A resources block from a definition created before the feature was removed
// is ignored: it no longer declares scheduling or validation.
delete(fields, "resources")
if text, ok := fields["limits"]; ok {
if err := decodeControl(text, &s.Limits); err != nil || s.Limits == nil {
return fmt.Errorf("limits must be an object with max_visits and optional on outcomes")
}
if s.Limits.MaxVisits < 1 || s.Limits.MaxVisits > 5 {
return fmt.Errorf("max_visits must be from 1 to 5")
}
seen := map[string]bool{}
for _, outcome := range s.Limits.On {
if !identifier.MatchString(outcome) || seen[outcome] {
return fmt.Errorf("invalid/duplicate limit outcome %q", outcome)
}
seen[outcome] = true
}
switch s.Limits.OnExhausted {
case "", "fail", "decide":
default:
return fmt.Errorf("on_exhausted must be fail or decide")
}
if s.Limits.AcceptTo != "" && s.Limits.OnExhausted != "decide" {
return fmt.Errorf("accept_to needs \"on_exhausted\":\"decide\"")
}
delete(fields, "limits")
}
if text, ok := fields["memory"]; ok {
if err := decodeControl(text, &s.Memory); err != nil || len(s.Memory) == 0 || len(s.Memory) > 50 {
return fmt.Errorf("memory must map 1–50 input names to input.FIELD or STAGE.FIELD")
}
for alias, reference := range s.Memory {
owner, field, ok := strings.Cut(strings.TrimSuffix(reference, "?"), ".")
if !identifier.MatchString(alias) || !ok || !identifier.MatchString(owner) || !identifier.MatchString(field) {
return fmt.Errorf("invalid memory reference %q: %q", alias, reference)
}
}
delete(fields, "memory")
}
return nil
}
func validateControl(s Stage, stages map[string]bool) error {
if s.Limits != nil {
for _, outcome := range s.Limits.On {
target, exists := s.Edges[outcome]
if !exists || protocol.IsTerminal(target) {
return fmt.Errorf("stage %s: limit outcome %q must name a nonterminal edge", s.Key, outcome)
}
}
if target := s.Limits.AcceptTo; target != "" && !protocol.IsTerminal(target) && !stages[target] {
return fmt.Errorf("stage %s: accept_to %q is not a stage, #success or #failure", s.Key, target)
}
}
for _, ref := range s.Memory {
owner, _, _ := strings.Cut(ref, ".")
if owner != "input" && !stages[owner] {
return fmt.Errorf("stage %s: unknown memory source %s", s.Key, owner)
}
if owner == s.Key {
return fmt.Errorf("stage %s: self-memory is not allowed; use another stage's explicit handoff", s.Key)
}
}
return nil
}