95454afa72
All five defects filed while implementing B18, plus the router flake that predated them. None of this has run on the deployed instance: the service is stopped and /usr/local/bin/orchestra predates every change here. B20 is the one that could silently defeat approvals. The capture revision was UnixNano, so it changed on every read and said nothing about whether the pane had changed; it is now an FNV-1a hash of the pane text, changing iff the text does. The worse half was precedence: capture() preferred the coordinator over a published worker capture, handing Queue a timestamp the owning worker's staleness check could never match, so every federated approval resolved "stale" and the keystroke never happened. Worker captures now win — their existence means a registered worker owns that pane — and capturePane follows the same precedence via Capture.Source rather than guessing. B19 was filed as "federated approvals emit no event", which overstated it: the resolution half already existed, and correctly fires only on an acknowledged worker report. The missing half was the request. Server.action now appends ApprovalRequested at queue time, subject_ref set to the command ID the later resolution carries. If that append fails the queued command is resolved "rejected" — a keystroke that left no audit trail must not run. B21 bounds the command list: resolved commands prune after 30 minutes on both Queue and Commands, pending ones never at any age, since dropping one would discard an operator decision. The persistence half stays open and is recorded as such — captures and commands are still in-memory only. S12 splits ORCHESTRA_NTFY_TOKEN, which was both the secret handed to the ntfy server and a valid inbound credential for the ntfy surface; the latter is now ORCHESTRA_NTFY_SURFACE_TOKEN. Breaking: a deployment relying on the old dual use has no inbound gate until it sets the new variable. S13 deletes the dead auth() copy of the authorization policy. The router flake was in the test, not in assignment. Store.Tasks() ranges a map, and the assertion indexed two separate Tasks() calls, failing whenever the orderings disagreed; instrumenting it showed a valid TaskLeased and a genuinely leased task on every "failing" run. It now snapshots once and asserts that exactly one task is leased, and passes at -count=60. AUDIT.md records what is still not done: the deployed env and binary, the live re-verification B13-B17 has always lacked, and two operational faults found in the journal that block it — all six herdrs are refusing connections, and ntfy delivery is failing 403 on every send. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01535A3Y8RtkAi8wYuWhtkEd
141 lines
4.3 KiB
Go
141 lines
4.3 KiB
Go
package federation
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCursorIsMonotonicAndAuthenticationIsRequired(t *testing.T) {
|
|
r := &Registry{}
|
|
if err := r.Register(Worker{ID: "workpc", Token: "secret"}, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Authenticate("workpc", "wrong"); err != ErrUnauthorized {
|
|
t.Fatalf("got %v", err)
|
|
}
|
|
if err := r.Authenticate("workpc", "secret"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Ack("workpc", 7); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.Ack("workpc", 6); err == nil {
|
|
t.Fatal("backwards cursor accepted")
|
|
}
|
|
if got, _ := r.Cursor("workpc"); got != 7 {
|
|
t.Fatalf("cursor = %d", got)
|
|
}
|
|
}
|
|
|
|
func TestRegisterRequiresAdmitTokenAndOwnToken(t *testing.T) {
|
|
r := &Registry{AdmitToken: "admit-secret"}
|
|
if err := r.Register(Worker{ID: "workpc", Token: "secret"}, "wrong"); err != ErrUnauthorized {
|
|
t.Fatalf("wrong admit token: got %v", err)
|
|
}
|
|
if err := r.Register(Worker{ID: "workpc", Token: "secret"}, "admit-secret"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Re-registering the same id with a different token is a hijack
|
|
// attempt (S10), not a legitimate re-registration, and must be refused
|
|
// even with a valid admit token.
|
|
if err := r.Register(Worker{ID: "workpc", Token: "different"}, "admit-secret"); err != ErrUnauthorized {
|
|
t.Fatalf("hijack with different token: got %v", err)
|
|
}
|
|
// The same worker re-registering with its own token (e.g. after a
|
|
// restart) must still succeed.
|
|
if err := r.Register(Worker{ID: "workpc", Token: "secret"}, "admit-secret"); err != nil {
|
|
t.Fatalf("legitimate re-registration: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestOfflineHookRunsOnceOnTransition(t *testing.T) {
|
|
called := make(chan Worker, 1)
|
|
r := &Registry{TTL: time.Millisecond, OnOffline: func(w Worker) { called <- w }}
|
|
if err := r.Register(Worker{ID: "workpc", Token: "secret"}, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r.mu.Lock()
|
|
w := r.workers["workpc"]
|
|
w.LastSeen = time.Now().Add(-time.Second)
|
|
r.workers["workpc"] = w
|
|
r.mu.Unlock()
|
|
r.Snapshot()
|
|
select {
|
|
case got := <-called:
|
|
if got.ID != "workpc" {
|
|
t.Fatal(got.ID)
|
|
}
|
|
case <-time.After(time.Second):
|
|
t.Fatal("offline hook not called")
|
|
}
|
|
r.Snapshot()
|
|
select {
|
|
case <-called:
|
|
t.Fatal("offline hook called twice")
|
|
case <-time.After(10 * time.Millisecond):
|
|
}
|
|
}
|
|
|
|
func TestCaptureRevisionAndCommandQueue(t *testing.T) {
|
|
r := &Registry{}
|
|
if err := r.Register(Worker{ID: "w", Token: "t"}, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c, err := r.PutCapture("w", Capture{TaskID: "task", PaneID: "pane", Text: "Permission required\n$ ls"})
|
|
if err != nil || c.Revision != 1 {
|
|
t.Fatalf("capture=%#v err=%v", c, err)
|
|
}
|
|
again, err := r.PutCapture("w", Capture{TaskID: "task", PaneID: "pane", Text: c.Text})
|
|
if err != nil || again.Revision != 1 {
|
|
t.Fatalf("same capture=%#v err=%v", again, err)
|
|
}
|
|
cmd, err := r.Queue("w", Command{TaskID: "task", Kind: "grant_approval", PaneID: "pane", CaptureRevision: 1})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
commands, err := r.Commands("w")
|
|
if err != nil || len(commands) != 1 || commands[0].ID != cmd.ID {
|
|
t.Fatalf("commands=%#v err=%v", commands, err)
|
|
}
|
|
if err := r.CompleteCommand("w", cmd.ID, "acknowledged", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
commands, _ = r.Commands("w")
|
|
if len(commands) != 0 {
|
|
t.Fatalf("pending=%#v", commands)
|
|
}
|
|
}
|
|
|
|
func TestResolvedCommandsArePrunedButPendingOnesSurvive(t *testing.T) {
|
|
r := &Registry{}
|
|
if err := r.Register(Worker{ID: "w", Token: "t"}, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old, err := r.Queue("w", Command{TaskID: "task", Kind: "grant_approval", PaneID: "pane", CaptureRevision: 1})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := r.CompleteCommand("w", old.ID, "acknowledged", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pending, err := r.Queue("w", Command{TaskID: "task", Kind: "deny_approval", PaneID: "pane", CaptureRevision: 2})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Age both past the retention window; only the resolved one may go.
|
|
r.mu.Lock()
|
|
for i := range r.commands["w"] {
|
|
r.commands["w"][i].CreatedAt = time.Now().UTC().Add(-2 * CommandRetention)
|
|
}
|
|
r.mu.Unlock()
|
|
if _, err := r.Commands("w"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := r.Command("w", old.ID); ok {
|
|
t.Fatal("resolved command past retention was not pruned")
|
|
}
|
|
if c, ok := r.Command("w", pending.ID); !ok || c.Status != "pending" {
|
|
t.Fatalf("pending command was pruned: %#v ok=%v", c, ok)
|
|
}
|
|
}
|