Add federation worker and canonical handoffs

This commit is contained in:
kami
2026-07-28 16:17:18 +04:00
parent 58793a5aa3
commit 2cecbc4015
22 changed files with 1429 additions and 108 deletions
+48 -16
View File
@@ -11,6 +11,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"time"
)
@@ -36,6 +37,9 @@ func RenderTaskFile(t domain.Task) []byte {
if t.Parent != "" {
fmt.Fprintf(&b, "- Parent: %s\n", t.Parent)
}
if strings.TrimSpace(t.Description) != "" {
fmt.Fprintf(&b, "\n## Instructions\n\n%s\n", t.Description)
}
b.WriteString("\nThis file is immutable for the lifetime of the task (§6.2) — its hash is\ncarried in every handoff and re-verified on every pickup. Do not edit it.\n")
return []byte(b.String())
}
@@ -99,21 +103,14 @@ type Meta struct {
RotationIndex int `json:"rotation_index"`
}
type Handoff struct {
Meta Meta `json:"meta"`
Anchor Anchor `json:"anchor"`
Goal string `json:"goal"`
DoneWhen []string `json:"done_when"`
Completed []Completed `json:"completed"`
Remaining []string `json:"remaining"`
Action string `json:"action"`
Command string `json:"command"`
Files []string `json:"files"`
Invariants []string `json:"invariants"`
DeadEnds []DeadEnd `json:"dead_ends"`
OpenQuestions []string `json:"open_questions"`
Build string `json:"build"`
Test string `json:"test"`
LastResult Result `json:"last_result"`
Meta Meta `json:"meta"`
Anchor Anchor `json:"anchor"`
Remaining []string `json:"remaining"`
Action string `json:"action"`
Command string `json:"command"`
DeadEnds []DeadEnd `json:"dead_ends"`
OpenQuestions []string `json:"open_questions"`
Learned []string `json:"learned"`
}
type Result struct {
Command string `json:"command"`
@@ -123,13 +120,32 @@ type Result struct {
var reasons = map[string]bool{"threshold": true, "milestone": true, "thrash": true, "manual": true}
const maxAuthoredLine = 200
var circularAction = regexp.MustCompile(`(?i)handoff|report\.md|^continue the task`)
var circularCommand = regexp.MustCompile(`(?i)\.orchestra-handoff|handoff-report|report\.md`)
func (h Handoff) Validate() error {
if strings.TrimSpace(h.Meta.ID) == "" || !reasons[h.Meta.Reason] || h.Meta.RotationIndex < 0 {
return errors.New("invalid handoff meta")
}
if len(h.Anchor.GitSHA) != 40 || h.Anchor.Branch == "" || strings.TrimSpace(h.Goal) == "" || len(h.DoneWhen) == 0 || strings.TrimSpace(h.Action) == "" || strings.TrimSpace(h.Command) == "" {
if len(h.Anchor.GitSHA) != 40 || h.Anchor.Branch == "" || strings.TrimSpace(h.Action) == "" {
return errors.New("invalid handoff required fields")
}
if circularAction.MatchString(h.Action) {
return errors.New("invalid handoff action: must name concrete next work, not a handoff")
}
if err := validateAuthoredLine(h.Action); err != nil {
return err
}
if circularCommand.MatchString(h.Command) {
return errors.New("invalid handoff command: must not point to a handoff or report")
}
for _, item := range append(append([]string{}, h.Remaining...), append(h.OpenQuestions, h.Learned...)...) {
if err := validateAuthoredLine(item); err != nil {
return err
}
}
for _, d := range h.Anchor.Dirty {
if filepath.IsAbs(d.Path) || d.Path == "" || len(d.SHA256) != 64 {
return errors.New("invalid dirty anchor")
@@ -139,6 +155,22 @@ func (h Handoff) Validate() error {
if strings.TrimSpace(d.Tried) == "" || strings.TrimSpace(d.WhyFailed) == "" {
return errors.New("invalid dead end")
}
if err := validateAuthoredLine(d.Tried); err != nil {
return err
}
if err := validateAuthoredLine(d.WhyFailed); err != nil {
return err
}
}
return nil
}
func validateAuthoredLine(s string) error {
if strings.TrimSpace(s) == "" {
return errors.New("invalid handoff authored field: empty item")
}
if strings.Contains(s, "\n#") || len(s) > maxAuthoredLine {
return errors.New("invalid handoff authored field: prose smuggled into list")
}
return nil
}