package wire_test import ( "bytes" "encoding/json" "errors" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // Mixed versions (the scheduler at 14): at 14 every scheduler frame and // field goes out whole; at 13 the release and priority frames, a held // report and a preference with an end are refused (an older runner is // refused anything that spends, never handed it), and a dispatch loses // its priority, deadline and release; what 13 already had is unchanged. func TestEncodeBelowProtocol14DropsScheduler(t *testing.T) { if wire.ProtocolScheduler != wire.Protocol14 || wire.ProtocolCurrent != wire.ProtocolDesign { t.Fatalf("scheduler %d, current %d", wire.ProtocolScheduler, wire.ProtocolCurrent) } by := time.Date(2026, 11, 20, 17, 0, 0, 0, time.UTC) wake := time.Date(2026, 11, 13, 10, 0, 0, 0, time.UTC) plain := wire.DispatchCommand{Person: "p", TaskID: "tk", StepID: "s", Attempt: 1, Specialist: wire.SpecialistEngineering, Findings: []wire.Finding{}} urgent := plain urgent.Priority, urgent.By, urgent.Release = wire.PriorityUrgent, &by, "rl_1" dispatch, _ := wire.NewFrame("d", wire.FrameDispatch, urgent) release, _ := wire.NewFrame("r", wire.FrameRelease, wire.ReleaseCommand{ID: "rl_1", Person: "p", TaskID: "tk", StepID: "s", StepRevision: 1}) priority, _ := wire.NewFrame("p", wire.FramePriority, wire.PriorityCommand{Person: "p", TaskID: "tk", Priority: wire.PriorityUrgent, Revision: 2}) 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}}) until, _ := wire.NewFrame("u", wire.FramePreference, wire.PreferenceCommand{Person: "p", Provider: "codex", Revision: 3, Until: &by}) forever, _ := wire.NewFrame("f", wire.FramePreference, wire.PreferenceCommand{Person: "p", Provider: "codex", Revision: 3}) started, _ := wire.NewFrame("s", wire.FrameStep, wire.StepReport{TaskID: "tk", StepID: "s", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}}) for _, f := range []wire.Frame{dispatch, release, priority, held, until, forever, started} { if same, err := wire.Encode(f, wire.Protocol14); err != nil || !bytes.Equal(same.Data, f.Data) { t.Fatalf("%s changed at 14: %v", f.Type, err) } } for _, f := range []wire.Frame{release, priority, held, until} { if _, err := wire.Encode(f, wire.Protocol13); !errors.Is(err, wire.ErrProtocol) { t.Fatalf("%s %s encoded at 13: %v", f.Type, f.Data, err) } } for _, f := range []wire.Frame{forever, started} { if same, err := wire.Encode(f, wire.Protocol13); err != nil || !bytes.Equal(same.Data, f.Data) { t.Fatalf("%s changed at 13: %v", f.Type, err) } } got, err := wire.Encode(dispatch, wire.Protocol13) want, _ := wire.NewFrame("d", wire.FrameDispatch, plain) if err != nil || !bytes.Equal(got.Data, want.Data) { t.Fatalf("dispatch at 13: %v\n got %s\nwant %s", err, got.Data, want.Data) } // What a protocol 13 peer reads strictly: no scheduler field survives. for _, p := range []int{wire.Protocol13, wire.Protocol12, wire.Protocol1} { got, err := wire.Encode(dispatch, p) if err != nil { t.Fatal(err) } for _, field := range []string{`"priority"`, `"by"`, `"release"`} { if strings.Contains(string(got.Data), field) { t.Fatalf("%s reached protocol %d: %s", field, p, got.Data) } } } } // Frozen protocol 13 schemas: the JSON keys each body had at protocol // 13 (main at 379fe0d, before the scheduler), written out so that a // field added later cannot slip into them. var ( 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") 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") frozen13Preference = keySet("person provider revision") frozen13Record = keySet("repo title brief separate kind") frozen13Start = keySet("task brief") ) func keySet(s string) map[string]bool { out := map[string]bool{} for _, k := range strings.Fields(s) { out[k] = true } return out } // frozen13 strictly reads raw against a frozen schema: every key one it // had at protocol 13. func frozen13(t *testing.T, what string, raw []byte, schema map[string]bool) { t.Helper() var m map[string]json.RawMessage if err := json.Unmarshal(raw, &m); err != nil { t.Fatalf("%s: %v", what, err) } for k := range m { if !schema[k] { t.Fatalf("%s carries %q, which a protocol 13 peer refuses: %s", what, k, raw) } } } // What is still sent below 14 decodes strictly against the frozen // protocol 13 schemas: a dispatch, a preference, a step report, and // record_task's and start_task's arguments. func TestEncodeBelowProtocol14AgainstFrozenSchemas(t *testing.T) { by := time.Date(2026, 11, 20, 17, 0, 0, 0, time.UTC) 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}) pref, _ := wire.NewFrame("p", wire.FramePreference, wire.PreferenceCommand{Person: "p", Provider: "codex", Revision: 2}) step, _ := wire.NewFrame("s", wire.FrameStep, wire.StepReport{TaskID: "tk", StepID: "s", Attempt: 1, State: wire.StepRunning, Findings: []wire.Finding{}}) 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"}`)}) 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"}`)}) for _, c := range []struct { what string f wire.Frame schema map[string]bool args bool }{{"dispatch", dispatch, frozen13Dispatch, false}, {"preference", pref, frozen13Preference, false}, {"step", step, frozen13Step, false}, {"record_task", record, frozen13Record, true}, {"start_task", start, frozen13Start, true}} { old, err := wire.Encode(c.f, wire.Protocol13) if err != nil { t.Fatalf("%s: %v", c.what, err) } raw := []byte(old.Data) if c.args { var call wire.ToolCall if err := wire.Strict(old.Data, &call); err != nil { t.Fatal(err) } raw = call.Args } frozen13(t, c.what, raw, c.schema) } } // The scheduler's frames are durable and decode strictly. func TestSchedulerFramesStrict(t *testing.T) { for _, kind := range []string{wire.FrameRelease, wire.FramePriority} { if !(wire.Frame{Type: kind}).Durable() { t.Fatalf("%s is not durable", kind) } } bad, _ := json.Marshal(map[string]any{"id": "r", "person": "p", "task_id": "t", "step_id": "s", "step_revision": 1, "api_key": "x"}) if _, err := (wire.Frame{ID: "r", Type: wire.FrameRelease, Data: bad}).Decode(); err == nil { t.Fatal("an unknown field in a release decoded") } for _, p := range []string{wire.PriorityUrgent, wire.PriorityNormal, wire.PriorityLow} { if !wire.ValidPriority(p) { t.Fatalf("%s invalid", p) } } if wire.ValidPriority("") || wire.ValidPriority("high") || wire.CoordinatorPriority(wire.PriorityUrgent) || !wire.CoordinatorPriority(wire.PriorityLow) { t.Fatal("priority values") } }