oficina / equipo Public

wire: drop empty slice F keys below protocol 37

Encode below 37 now always re-encodes a body that can carry a slice F
key, so an empty, zero or null value goes with its key: an older
peer's strict decoder refuses the key whatever its value (Astra
review, task 2 round 1). The same re-encode drops older empty
omitempty keys on those nine body types; other types keep the gap.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a5650332c9a3928d2c58060b161824b2b7fe28eb parent 098407b
2 files changed, +77 −14
  • internal/wire/mac_engineering.go +8 −14
  • internal/wire/mac_engineering_test.go +69 −0
diff --git a/internal/wire/mac_engineering.go b/internal/wire/mac_engineering.go
index a75fb8b0..29015ab4 100644
--- a/internal/wire/mac_engineering.go
+++ b/internal/wire/mac_engineering.go
@@ -292,8 +292,10 @@ func WithoutWorkerConnections(h *Hello, protocol int) bool {
// downgradeMacEngineering takes slice F out of what a peer below
// ProtocolMacEngineering is sent (the new frames never get this far:
// Speaks refuses them): an Engineering transfer and a diff upload bound
-// to a publication are refused; every new field is dropped. It reports
-// whether anything changed.
+// to a publication are refused; every new field is dropped. A body that
+// can carry a slice F key is always re-encoded (it reports true), so the
+// key goes even when its value is empty, zero or null: every new field is
+// omitempty, and an older peer's strict decoder refuses the key itself.
func downgradeMacEngineering(body any, protocol int) (bool, error) {
refuse := func(what string) (bool, error) {
return false, fmt.Errorf("%w: %s at protocol %d", ErrProtocol, what, protocol)
@@ -312,27 +314,19 @@ func downgradeMacEngineering(body any, protocol int) (bool, error) {
return refuse("a publication's diff")
}
case *Hello:
- return WithoutMacEngineering(v), nil
+ WithoutMacEngineering(v)
case *DispatchCommand:
- set := v.XcodeAccess != ""
v.XcodeAccess = ""
- return set, nil
case *StepReport:
- set := v.XcodeAccess != "" || v.SessionID != "" || v.OutcomePhase != ""
v.XcodeAccess, v.SessionID, v.OutcomePhase = "", "", ""
- return set, nil
case *PublishCommand:
- set := v.OutcomePhase != ""
v.OutcomePhase = ""
- return set, nil
case *PublishedReport:
- set := v.ChangeFiles != nil || v.DiffCollection != "" || v.OutcomePhase != ""
v.ChangeFiles, v.DiffCollection, v.OutcomePhase = nil, "", ""
- return set, nil
case *AskCommand:
- set := v.EngAttempt != 0 || v.EngAccount != "" || v.EngSession != ""
v.EngAttempt, v.EngAccount, v.EngSession = 0, "", ""
- return set, nil
+ default:
+ return false, nil
}
- return false, nil
+ return true, nil
}
diff --git a/internal/wire/mac_engineering_test.go b/internal/wire/mac_engineering_test.go
index e69ef70f..0337cd03 100644
--- a/internal/wire/mac_engineering_test.go
+++ b/internal/wire/mac_engineering_test.go
@@ -211,6 +211,75 @@ func TestEncodeDropsMacEngineeringBelow37(t *testing.T) {
}
}
+// A frame may carry a slice F key with an empty, zero or null value (a
+// peer that writes the key without omitempty, or a hand-built frame). Below
+// 37 the key itself must go, not only a set value: an older peer's strict
+// decoder refuses any key it does not know, whatever its value.
+func TestEncodeDropsEmptyMacEngineeringKeysBelow37(t *testing.T) {
+ with := func(name string, at []string, extra map[string]any) wire.Frame {
+ t.Helper()
+ var frame map[string]any
+ if err := json.Unmarshal(wiretest.Fixture(name), &frame); err != nil {
+ t.Fatal(err)
+ }
+ obj := frame["data"].(map[string]any)
+ for _, k := range at {
+ obj = obj[k].(map[string]any)
+ }
+ for k, v := range extra {
+ obj[k] = v
+ }
+ raw, _ := json.Marshal(frame)
+ f, _, err := wire.ParseFrame(raw)
+ if err != nil {
+ t.Fatalf("%s with %v: %v", name, extra, err)
+ }
+ return f
+ }
+ diff, _ := wire.NewFrame("cd", wire.FrameChangeDiff, wire.ChangeDiffUpload{})
+ var diffFrame map[string]any
+ raw, _ := json.Marshal(diff)
+ _ = json.Unmarshal(raw, &diffFrame)
+ diffFrame["data"].(map[string]any)["publication"] = ""
+ raw, _ = json.Marshal(diffFrame)
+ emptyDiff, _, err := wire.ParseFrame(raw)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, c := range []struct {
+ name string
+ frame wire.Frame
+ keys []string
+ }{
+ {"dispatch", with("frames/dispatch-needs.json", nil, map[string]any{"xcode_access": ""}), []string{"xcode_access"}},
+ {"step", with("frames/step-process-group.json", nil, map[string]any{"xcode_access": "", "session_id": "", "outcome_phase": ""}), []string{"xcode_access", "session_id", "outcome_phase"}},
+ {"publish", with("frames/publish.json", nil, map[string]any{"outcome_phase": ""}), []string{"outcome_phase"}},
+ {"published", with("frames/published.json", nil, map[string]any{"change_files": nil, "diff_collection": "", "outcome_phase": ""}), []string{"change_files", "diff_collection", "outcome_phase"}},
+ {"ask", with("frames/ask.json", nil, map[string]any{"eng_attempt": 0, "eng_account": "", "eng_session": ""}), []string{"eng_attempt", "eng_account", "eng_session"}},
+ {"blob", with("frames/blob.json", []string{"transfer"}, map[string]any{"kind": "", "old_base_sha": ""}), []string{"kind", "old_base_sha"}},
+ {"send_pack", with("frames/send_pack.json", []string{"transfer"}, map[string]any{"kind": "", "old_base_sha": ""}), []string{"kind", "old_base_sha"}},
+ {"hello", with("frames/hello-mac-probes.json", []string{"capabilities"}, map[string]any{"engineering": nil}), []string{"engineering"}},
+ {"change_diff", emptyDiff, []string{"publication"}},
+ } {
+ for _, p := range []int{35, 36} {
+ got, err := wire.Encode(c.frame, p)
+ if err != nil {
+ t.Errorf("%s at %d: %v", c.name, p, err)
+ continue
+ }
+ for _, k := range c.keys {
+ if bytes.Contains(got.Data, []byte(`"`+k+`"`)) {
+ t.Errorf("%s at %d kept the empty %s: %s", c.name, p, k, got.Data)
+ }
+ }
+ out, _ := json.Marshal(got)
+ if _, _, err := wire.ParseFrame(out); err != nil {
+ t.Errorf("%s at %d does not parse: %v", c.name, p, err)
+ }
+ }
+ }
+}
+
// A worker's hello carries its connections and seal key only at 37: the
// instance ignores both from a worker below it.
func TestWithoutWorkerConnections(t *testing.T) {