From c6dcfd667135324cd6c2ebbd93fe50b4c69b32b2 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 17 Sep 2026 14:58:14 +0000 Subject: [PATCH] fix(staterpc): never decode a collection field to a nil slice Every client method that decodes a repeated proto field goes through mapValues, which returned nil for nil input. Protobuf decodes an empty repeated field to nil, so an empty collection crossed the wire as JSON null rather than an empty list: the field's type depended on whether it happened to have contents, and the wire path disagreed with the local path, which normalises its own slices. This is the nil-slice class the contract-conformance audit deferred, arriving by the route its own revisit condition names -- a handler that can produce nil. The audit checked the webui handlers (clean, as it said) and did not cover the wire mapping added afterwards, so the regressed field was the new one: ReadTaskLog's components, observed as "components": null from the live dashboard. mapValues now returns []B{} for empty input, which fixes the class once at the mapping helper rather than at each of its 36 call sites -- none of them compares the result to nil, so nothing depended on the old behaviour. TaskLog also normalises components, which is read straight from the proto rather than through mapValues. Red first: the test drives the existing local/grpc matrix and asserts the marshalled JSON, so it fails on a null collection without depending on which path is under test. Both collections are exercised empty -- entries present with components empty, and a log file that exists with no lines -- because the first shape alone leaves the entries side untested. Verified by mutation: restoring the nil return fails it on "entries marshalled to null". --- internal/infrastructure/staterpc/client.go | 10 +- .../staterpc/task_log_nilslice_test.go | 91 +++++++++++++++++++ internal/infrastructure/staterpc/values.go | 13 ++- 3 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 internal/infrastructure/staterpc/task_log_nilslice_test.go diff --git a/internal/infrastructure/staterpc/client.go b/internal/infrastructure/staterpc/client.go index 958cc4d6d..8da36f86f 100644 --- a/internal/infrastructure/staterpc/client.go +++ b/internal/infrastructure/staterpc/client.go @@ -436,12 +436,20 @@ func (c *Client) TaskLog(ctx context.Context, taskID int64, attempt int, q loggi Components: []string{}, }, nil } + // Both collections are normalised: protobuf decodes an empty `repeated` + // field to nil, and a nil slice marshals to JSON null rather than an empty + // list, so the field's type would otherwise depend on whether it happened + // to have contents. mapValues owns the entries side. + components := r.Components + if components == nil { + components = []string{} + } return logging.TaskLogPage{ Entries: mapValues(r.Entries, taskLogEntryValue), Truncated: r.Truncated, File: r.File, Found: true, - Components: r.Components, + Components: components, }, nil } diff --git a/internal/infrastructure/staterpc/task_log_nilslice_test.go b/internal/infrastructure/staterpc/task_log_nilslice_test.go new file mode 100644 index 000000000..52a8e4063 --- /dev/null +++ b/internal/infrastructure/staterpc/task_log_nilslice_test.go @@ -0,0 +1,91 @@ +package staterpc + +import ( + "encoding/json" + "path/filepath" + "testing" + + "github.com/samcharles93/archie-core/internal/logging" + "github.com/samcharles93/archie-core/internal/store" +) + +// A decoded task-log page must never carry a nil slice for a collection field. +// Go marshals a nil slice to JSON null, and null is not an empty list: a +// client that reads the field as an array sees a different type than the same +// route serves when the collection is non-empty. +// +// This is the nil-slice class the contract-conformance audit deferred (its +// named revisit condition is a handler that can produce nil). The local reader +// already normalises both fields; the wire mapping did not, because protobuf +// decodes an empty `repeated` field to nil, so the grpc path regressed what the +// local path got right. Asserting the JSON shape rather than the slice catches +// it on both paths without depending on which one is under test. +func TestTaskLogPageCarriesNoNilCollectionFields(t *testing.T) { + // Both collections must be exercised empty. A log with entries but no + // structured "component" field leaves only Components empty; an attempt + // whose log file exists but holds no lines leaves only Entries empty, and + // that second shape is what catches an empty-slice mapping regressing on + // the entries side. + for _, tc := range []struct { + name string + write bool + }{ + {name: "entries present, components empty", write: true}, + {name: "entries empty, components empty", write: false}, + } { + for _, mode := range []string{"local", "grpc"} { + t.Run(tc.name+"/"+mode, func(t *testing.T) { + ctx := t.Context() + baseDir := filepath.Join(t.TempDir(), "logs", "tasks") + reader := logging.NewTaskRegistry(baseDir, logging.NewFeed(10), logging.TaskSinkOptions{}) + + const taskID = 7 + if err := reader.Open(taskID, 0); err != nil { + t.Fatalf("open sink: %v", err) + } + if tc.write { + // No "component" field at all: Components stays empty while + // Entries does not. + reader.Write(taskID, logging.Entry{Level: "INFO", Message: "starting"}) + } + if err := reader.Close(taskID); err != nil { + t.Fatalf("close sink: %v", err) + } + + var c taskLogContract = reader + if mode == "grpc" { + c = remoteTaskStore(t, store.OpenTest(t), reader) + } + + page, err := c.TaskLog(ctx, taskID, 0, logging.Query{}) + if err != nil { + t.Fatalf("TaskLog: %v", err) + } + if !page.Found { + t.Fatal("Found = false for an attempt whose log file exists") + } + if tc.write && len(page.Entries) != 1 { + t.Fatalf("entries = %d, want the line the sink wrote", len(page.Entries)) + } + if !tc.write && len(page.Entries) != 0 { + t.Fatalf("entries = %d, want none for a log with no lines", len(page.Entries)) + } + + raw, err := json.Marshal(page) + if err != nil { + t.Fatalf("marshal page: %v", err) + } + var decoded map[string]json.RawMessage + if err := json.Unmarshal(raw, &decoded); err != nil { + t.Fatalf("unmarshal page: %v", err) + } + for _, field := range []string{"entries", "components"} { + if got := string(decoded[field]); got == "null" { + t.Errorf("%s marshalled to null, want an empty list: the field's type must not depend on whether it has contents", + field) + } + } + }) + } + } +} diff --git a/internal/infrastructure/staterpc/values.go b/internal/infrastructure/staterpc/values.go index 54cdb0895..91bec136c 100644 --- a/internal/infrastructure/staterpc/values.go +++ b/internal/infrastructure/staterpc/values.go @@ -34,9 +34,18 @@ func timeValue(t *timestamppb.Timestamp) time.Time { return t.AsTime() } +// mapValues maps a slice of proto values to domain values, always returning a +// non-nil result for empty input. +// +// Protobuf decodes an empty `repeated` field to nil, and Go marshals a nil +// slice to JSON null rather than []. Returning nil here made every decoded +// collection field's JSON type depend on whether it happened to have contents, +// and made the wire path disagree with the local path, which normalises its own +// slices. []B{} for empty input keeps the shape stable at every call site +// without making each one remember to guard. func mapValues[A, B any](in []A, f func(A) B) []B { - if in == nil { - return nil + if len(in) == 0 { + return []B{} } out := make([]B, len(in)) for i, v := range in {