| 1 | package wire_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | ) |
| 13 | |
| 14 | // Mixed versions (the scheduler at 14): at 14 every scheduler frame and |
| 15 | // field goes out whole; at 13 the release and priority frames, a held |
| 16 | // report and a preference with an end are refused (an older runner is |
| 17 | // refused anything that spends, never handed it), and a dispatch loses |
| 18 | // its priority, deadline and release; what 13 already had is unchanged. |
| 19 | func TestEncodeBelowProtocol14DropsScheduler(t *testing.T) { |
| 20 | if wire.ProtocolScheduler != wire.Protocol14 || wire.ProtocolCurrent != wire.ProtocolDesign { |
| 21 | t.Fatalf("scheduler %d, current %d", wire.ProtocolScheduler, wire.ProtocolCurrent) |
| 22 | } |
| 23 | by := time.Date(2026, 11, 20, 17, 0, 0, 0, time.UTC) |
| 24 | wake := time.Date(2026, 11, 13, 10, 0, 0, 0, time.UTC) |
| 25 | plain := wire.DispatchCommand{Person: "p", TaskID: "tk", StepID: "s", Attempt: 1, Specialist: wire.SpecialistEngineering, Findings: []wire.Finding{}} |
| 26 | urgent := plain |
| 27 | urgent.Priority, urgent.By, urgent.Release = wire.PriorityUrgent, &by, "rl_1" |
| 28 | dispatch, _ := wire.NewFrame("d", wire.FrameDispatch, urgent) |
| 29 | release, _ := wire.NewFrame("r", wire.FrameRelease, wire.ReleaseCommand{ID: "rl_1", Person: "p", TaskID: "tk", StepID: "s", StepRevision: 1}) |
| 30 | priority, _ := wire.NewFrame("p", wire.FramePriority, wire.PriorityCommand{Person: "p", TaskID: "tk", Priority: wire.PriorityUrgent, Revision: 2}) |
| 31 | held, _ := wire.NewFrame("h", wire.FrameStep, wire.StepReport{TaskID: "tk", StepID: "s", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}, Hold: &wire.StepHold{Reason: wire.HoldHeadroom, Connection: "claude-a", Provider: "claude", WakeAt: &wake, StepRevision: 1}}) |
| 32 | until, _ := wire.NewFrame("u", wire.FramePreference, wire.PreferenceCommand{Person: "p", Provider: "codex", Revision: 3, Until: &by}) |
| 33 | forever, _ := wire.NewFrame("f", wire.FramePreference, wire.PreferenceCommand{Person: "p", Provider: "codex", Revision: 3}) |
| 34 | started, _ := wire.NewFrame("s", wire.FrameStep, wire.StepReport{TaskID: "tk", StepID: "s", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}}) |
| 35 | |
| 36 | for _, f := range []wire.Frame{dispatch, release, priority, held, until, forever, started} { |
| 37 | if same, err := wire.Encode(f, wire.Protocol14); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 38 | t.Fatalf("%s changed at 14: %v", f.Type, err) |
| 39 | } |
| 40 | } |
| 41 | for _, f := range []wire.Frame{release, priority, held, until} { |
| 42 | if _, err := wire.Encode(f, wire.Protocol13); !errors.Is(err, wire.ErrProtocol) { |
| 43 | t.Fatalf("%s %s encoded at 13: %v", f.Type, f.Data, err) |
| 44 | } |
| 45 | } |
| 46 | for _, f := range []wire.Frame{forever, started} { |
| 47 | if same, err := wire.Encode(f, wire.Protocol13); err != nil || !bytes.Equal(same.Data, f.Data) { |
| 48 | t.Fatalf("%s changed at 13: %v", f.Type, err) |
| 49 | } |
| 50 | } |
| 51 | got, err := wire.Encode(dispatch, wire.Protocol13) |
| 52 | want, _ := wire.NewFrame("d", wire.FrameDispatch, plain) |
| 53 | if err != nil || !bytes.Equal(got.Data, want.Data) { |
| 54 | t.Fatalf("dispatch at 13: %v\n got %s\nwant %s", err, got.Data, want.Data) |
| 55 | } |
| 56 | // What a protocol 13 peer reads strictly: no scheduler field survives. |
| 57 | for _, p := range []int{wire.Protocol13, wire.Protocol12, wire.Protocol1} { |
| 58 | got, err := wire.Encode(dispatch, p) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | for _, field := range []string{`"priority"`, `"by"`, `"release"`} { |
| 63 | if strings.Contains(string(got.Data), field) { |
| 64 | t.Fatalf("%s reached protocol %d: %s", field, p, got.Data) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // Frozen protocol 13 schemas: the JSON keys each body had at protocol |
| 71 | // 13 (main at 379fe0d, before the scheduler), written out so that a |
| 72 | // field added later cannot slip into them. |
| 73 | var ( |
| 74 | frozen13Dispatch = keySet("person task_id step_id attempt specialist repo branch base_sha title brief findings candidate_sha republish from_attempt avoid_account round feedback rebase from_base needs runner_id revert_sha hosted_checks unsandboxed research research_note") |
| 75 | frozen13Step = keySet("task_id step_id attempt state reason base_sha candidate_sha pushed termination summary headline log_tail family account verdict review_label findings checks gate exit_code output askable merge_sha into strategy usage switched termination_kind have phase apple checks_sandbox change merge_source merge_parents hosted has_preview preview research") |
| 76 | frozen13Preference = keySet("person provider revision") |
| 77 | frozen13Record = keySet("repo title brief separate kind") |
| 78 | frozen13Start = keySet("task brief") |
| 79 | ) |
| 80 | |
| 81 | func keySet(s string) map[string]bool { |
| 82 | out := map[string]bool{} |
| 83 | for _, k := range strings.Fields(s) { |
| 84 | out[k] = true |
| 85 | } |
| 86 | return out |
| 87 | } |
| 88 | |
| 89 | // frozen13 strictly reads raw against a frozen schema: every key one it |
| 90 | // had at protocol 13. |
| 91 | func frozen13(t *testing.T, what string, raw []byte, schema map[string]bool) { |
| 92 | t.Helper() |
| 93 | var m map[string]json.RawMessage |
| 94 | if err := json.Unmarshal(raw, &m); err != nil { |
| 95 | t.Fatalf("%s: %v", what, err) |
| 96 | } |
| 97 | for k := range m { |
| 98 | if !schema[k] { |
| 99 | t.Fatalf("%s carries %q, which a protocol 13 peer refuses: %s", what, k, raw) |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // What is still sent below 14 decodes strictly against the frozen |
| 105 | // protocol 13 schemas: a dispatch, a preference, a step report, and |
| 106 | // record_task's and start_task's arguments. |
| 107 | func TestEncodeBelowProtocol14AgainstFrozenSchemas(t *testing.T) { |
| 108 | by := time.Date(2026, 11, 20, 17, 0, 0, 0, time.UTC) |
| 109 | dispatch, _ := wire.NewFrame("d", wire.FrameDispatch, wire.DispatchCommand{Person: "p", TaskID: "tk", StepID: "s", Attempt: 1, Specialist: wire.SpecialistEngineering, Findings: []wire.Finding{}, Priority: wire.PriorityUrgent, By: &by, Release: "rl", PriorityRevision: 3, Housekeeping: true}) |
| 110 | pref, _ := wire.NewFrame("p", wire.FramePreference, wire.PreferenceCommand{Person: "p", Provider: "codex", Revision: 2}) |
| 111 | step, _ := wire.NewFrame("s", wire.FrameStep, wire.StepReport{TaskID: "tk", StepID: "s", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}}) |
| 112 | record, _ := wire.NewFrame("r", wire.FrameToolCall, wire.ToolCall{CallID: "c", TurnID: "t", Attempt: 1, Name: wire.ToolRecordTask, Args: json.RawMessage(`{"brief":"b","by":"2026-11-20","kind":"change","priority":"low","repo":"r","separate":false,"title":"x"}`)}) |
| 113 | start, _ := wire.NewFrame("t", wire.FrameToolCall, wire.ToolCall{CallID: "c2", TurnID: "t", Attempt: 1, Name: wire.ToolStartTask, Args: json.RawMessage(`{"brief":"go","by":"2026-11-20","priority":"normal","task":"tk"}`)}) |
| 114 | for _, c := range []struct { |
| 115 | what string |
| 116 | f wire.Frame |
| 117 | schema map[string]bool |
| 118 | args bool |
| 119 | }{{"dispatch", dispatch, frozen13Dispatch, false}, {"preference", pref, frozen13Preference, false}, {"step", step, frozen13Step, false}, {"record_task", record, frozen13Record, true}, {"start_task", start, frozen13Start, true}} { |
| 120 | old, err := wire.Encode(c.f, wire.Protocol13) |
| 121 | if err != nil { |
| 122 | t.Fatalf("%s: %v", c.what, err) |
| 123 | } |
| 124 | raw := []byte(old.Data) |
| 125 | if c.args { |
| 126 | var call wire.ToolCall |
| 127 | if err := wire.Strict(old.Data, &call); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | raw = call.Args |
| 131 | } |
| 132 | frozen13(t, c.what, raw, c.schema) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // The scheduler's frames are durable and decode strictly. |
| 137 | func TestSchedulerFramesStrict(t *testing.T) { |
| 138 | for _, kind := range []string{wire.FrameRelease, wire.FramePriority} { |
| 139 | if !(wire.Frame{Type: kind}).Durable() { |
| 140 | t.Fatalf("%s is not durable", kind) |
| 141 | } |
| 142 | } |
| 143 | bad, _ := json.Marshal(map[string]any{"id": "r", "person": "p", "task_id": "t", "step_id": "s", "step_revision": 1, "api_key": "x"}) |
| 144 | if _, err := (wire.Frame{ID: "r", Type: wire.FrameRelease, Data: bad}).Decode(); err == nil { |
| 145 | t.Fatal("an unknown field in a release decoded") |
| 146 | } |
| 147 | for _, p := range []string{wire.PriorityUrgent, wire.PriorityNormal, wire.PriorityLow} { |
| 148 | if !wire.ValidPriority(p) { |
| 149 | t.Fatalf("%s invalid", p) |
| 150 | } |
| 151 | } |
| 152 | if wire.ValidPriority("") || wire.ValidPriority("high") || wire.CoordinatorPriority(wire.PriorityUrgent) || !wire.CoordinatorPriority(wire.PriorityLow) { |
| 153 | t.Fatal("priority values") |
| 154 | } |
| 155 | } |
| 156 | |