package release import ( "encoding/json" "errors" "os" "os/exec" "path/filepath" "testing" "time" ) type historyCrashStore struct { *healthyRetryStore boundary string } func (s *historyCrashStore) Write(name string, raw []byte) error { var r historyRecord if err := json.Unmarshal(raw, &r); err != nil { return err } reached := r.Attempt != nil && r.Attempt.ID == "crash-attempt" if s.boundary == "settled" { reached = r.Attempt == nil && len(r.Refusals.Values) == 1 } if reached && s.boundary == "intent-visible" { s.fail = 1 } err := s.healthyRetryStore.Write(name, raw) if reached { if err != nil && !(s.boundary == "intent-visible" && errors.Is(err, s.fault)) { return err } os.Exit(73) } return err } func TestPlan14RootHistoryProcessCrash(t *testing.T) { b := HistoryBinding{Host: proofHost, Account: "bab", App: "console"} a := HistoryAttempt{ID: "crash-attempt", Version: "new", Source: "record-etag", Started: time.Date(2026, 10, 13, 0, 0, 0, 0, time.UTC)} if boundary := os.Getenv("PLAN14_HISTORY_CRASH"); boundary != "" { dir := os.Getenv("PLAN14_HISTORY_DIR") disk := &healthyRetryStore{dir: dir, fault: errors.New("injected directory sync failure")} h := &RootHistory{Store: &historyCrashStore{healthyRetryStore: disk, boundary: boundary}} if boundary == "settled" { if err := h.Settle(b, a.ID, &Refusal{Host: b.Host, App: b.App, Version: a.Version, RefusedAt: a.Started, Reason: "failed"}); err != nil { t.Fatal(err) } } else if err := h.Begin(b, a); err != nil { t.Fatal(err) } t.Fatal("crash boundary not reached") } for _, boundary := range []string{"intent-durable", "intent-visible", "settled"} { t.Run(boundary, func(t *testing.T) { h, s, b := rootHistoryFixture(t) if boundary == "settled" { if err := h.Begin(b, a); err != nil { t.Fatal(err) } } cmd := exec.Command(os.Args[0], "-test.run=^TestPlan14RootHistoryProcessCrash$") cmd.Dir = t.TempDir() cmd.Env = append(os.Environ(), "PLAN14_HISTORY_CRASH="+boundary, "PLAN14_HISTORY_DIR="+s.dir) out, err := cmd.CombinedOutput() var exit *exec.ExitError if !errors.As(err, &exit) || exit.ExitCode() != 73 { t.Fatalf("child: %v\n%s", err, out) } h = &RootHistory{Store: &healthyRetryStore{dir: s.dir}} r, err := h.read(b) if err != nil { t.Fatal(err) } if boundary == "settled" { if r.Attempt != nil || len(r.Refusals.Values) != 1 { t.Fatalf("lost completed refusal: %+v", r) } } else { if r.Attempt == nil || *r.Attempt != a { t.Fatalf("lost pending attempt: %+v", r) } blocked, err := h.Blocks(b, &ChannelPointer{Version: "other"}) if err != nil || blocked { t.Fatal("unknown outcome blocked a different signed recovery target") } if err := h.Begin(b, HistoryAttempt{ID: "another", Version: "other", Source: a.Source, Started: a.Started}); err == nil { t.Fatal("crashed intent allowed an unjournalled forward attempt") } if _, err := h.Refusals(b); err == nil { t.Fatal("crashed intent granted mint completeness") } } if _, err := os.Stat(filepath.Join(s.dir, historyKey(b))); err != nil { t.Fatal(err) } }) } }