Skip to content

fix(state): a listing's order comes from the state, not from Go's map seed (#865) - #871

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/865-list-order
Sep 15, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/865-list-order

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

MemoryStateManager.List ranged the namespace map and returned without sorting, and
StateManager's doc comment stated no ordering contract, so no caller was wrong to assume none. Go
randomizes map iteration per process, so every operation that renders those keys in order answered
differently from one run to the next — and two identical calls in one run could differ. That is a
correctness defect against the replay guarantee, not untidiness: a recorded run cannot be replayed
byte-for-byte if a response body reorders itself between two reads of unchanged state.

The sort goes in the one non-test implementation, and the interface documents the guarantee, rather
than at the 119 call sites. The project had already ruled on this five times, at five individual
sites, each with its own written rationale and each leaving every other caller exposed: CFN stack
tags (#764), aws:TagKeys, CreateSnapshots' snapshotSet, DeleteSnapshot's image-ID tie-break,
and #862's four tag merge helpers. computeStateHash was deterministic only by accident, because
Snapshot marshals a map and encoding/json sorts map keys on the way out.

Verified operation by operation, the affected set is S3 ListBuckets, ListMultipartUploads and
ListObjectVersions; four ELBv2 describes; five RDS describes; and fifteen EC2 describes. Worst are
the ones paging over the keys with a cursor — RDS's Marker, ELBv2's Marker/PageSize — where an
unstable order can omit or repeat a resource across pages rather than merely reorder them.

Two operations needed more than the shared sort

ListMultipartUploads was violating a documented order. API_ListMultipartUploads publishes a
"Sorting of multipart uploads in response" section: ascending object key, then ascending initiation
time among uploads sharing a key. It now sorts on the initiation instant rather than its RFC 3339
rendering, with upload ID as a last-resort tie-break — substrate's own, because two uploads of one
key can share an instant under a controlled clock where they could not under a real one, and
key-marker's description admits an upload "for a key equal to the key-marker" only when its
"upload ID [is] lexicographically greater than the specified upload-id-marker".

DescribeInstances was only half-fixed by the source sort. It buckets instances into a
map[string]*reservationItem and ranges that map to build reservationSet, so the shared sort made
the instances within a reservation deterministic while leaving the reservations themselves in map
order. It now sorts by reservation ID. AWS documents no order there, so that is substrate's reading.

ListObjectVersions needed no code change: its keys are lexicographic from List and its versions
newest-first from the prepend-built stored list. It gains a doc comment recording that, and the
NextKeyMarker/CommonPrefixes cursor semantics that make the key order load-bearing.

Two premises in #865 were wrong, and are corrected on the record

Recorded in a comment on the issue
rather than left for this diff to contradict:

  • RGTA GetResources was never affected, though state: MemoryStateManager.List returns keys in Go map order, so 111 call sites answer differently run to run #865 called it the worst case. It sorts its
    entire scan by ARN before paginating (tagging_plugin.go:110-113), and all seventeen scan*
    helpers feed that one sort. The planned ResourcesPerPage=1 regression test was dropped rather
    than written, because it would have passed before this change. EC2 DescribeTags likewise —
    it sorts by resource ID, then type, then key. Both sorts stay and are still load-bearing, because
    neither wants the order of the state keys their records were read from.
  • "111 of 119 sites have no sort. nearby" is not a publishable number. It came from a proximity
    heuristic that overstates twice over — the sort can live in the caller (which is exactly how both
    cases above were missed), and most List callers are single-key lookups or first-match mutations
    whose order never reaches a response body. A verified per-operation list replaces it, in the code
    comment and in the docs. Roughly sixty sites in IAM, ElastiCache, API Gateway, CloudWatch, Lambda
    and SSM were not individually audited; they inherit the sort either way, and are claimed neither as
    fixed nor as unaffected.

Doc-comment staleness the contract change surfaced (#867)

Changing an interface contract made two in-code comments false, and one was already false:

  • ec2SortTagDescriptions asserted "StateManager.List documents no ordering either" — stale the
    moment types.go changed. Replaced with why its own sort is still needed: DescribeTags spans
    every resource type at once, and a tag's state key does not sort by resource ID.
  • cfnRegionalStampKinds' header described ECS's service and task definition as blocked by their
    writer. v0.115.0 fixed that; what they now lack is a table entry, which is a different reason.
  • docs/services.md contradicted itself — one passage described tagging behaviour v0.115.0 removed
    ("a TagResource on an ECS task definition answers 200 and writes nothing"), another enumerated
    the reachable set correctly.

Provenance

  • Documented by AWS: API_ListMultipartUploads' key-then-initiation-time sort.
  • Implied by the operation's own cursor: ListObjectVersions' KeyMarker, RDS's Marker,
    ELBv2's Marker/PageSize. AWS states no order in prose for any of them.
  • Substrate's reading, stated as such: lexicographic order everywhere AWS documents none —
    API_ListBuckets says nothing at all about bucket order, nor does API_DescribeRules — and
    ascending reservation ID for reservationSet. Both rest on the replay guarantee and on the five
    in-tree precedents.

Verification

docs/services.md gains a cross-cutting section, "The order a listing returns its members in",
recording the contract, the three provenance tiers with AWS's own language quoted, the verified
affected list, and the two never-affected listings.

The new emulator/list_order_test.go covers six cases through the wire rather than reading state
back, since the defect is what a caller observes. Every test either seeds its input in the reverse
of the expected output or asserts two successive calls agree byte-for-byte, so map order cannot be
mistaken for the contract holding. Their negativity was checked directly: with both sorts disabled,
all five order assertions fail on essentially every run.

Full gate green — gofmt/build/vet, golangci-lint (0 issues), make test and
GOTOOLCHAIN=go1.26.6 make test, coverage (83.9% emulator / 83.3% total, at or above the
83.9%/83.2% baseline), the four docs checks, the docs site build, and the e2e suite. Sorting List
changed observable order across the tree and broke zero tests — the risk this change carried —
so nothing was silently depending on map order.

Compatibility

Every affected listing's member order changes. An assertion that happened to pass on Go's map order
may now fail, and a paginated RDS or ELBv2 walk stops omitting and repeating. ListMultipartUploads
now matches its documented sort, and DescribeInstances' reservationSet arrives in ascending
reservation ID.

Closes #865.
Closes #867.

… seed (#865)

MemoryStateManager.List ranged the namespace map and returned without sorting,
and StateManager's doc comment stated no ordering contract, so no caller was
wrong to assume none. Go randomizes map iteration per process, so every
operation that renders those keys in order answered differently from one run to
the next — and two identical calls in one run could differ.

The sort goes in the one non-test implementation, and the interface documents
the guarantee, rather than at the 119 call sites. The same defect had already
been fixed five separate times at five individual sites (#764, aws:TagKeys,
CreateSnapshots' snapshotSet, DeleteSnapshot's image-ID tie-break, and #862's
four tag merge helpers), each with its own rationale and each leaving every
other caller exposed.

Verified operation by operation, the affected set is S3 ListBuckets,
ListMultipartUploads and ListObjectVersions; four ELBv2 describes; five RDS
describes; and fifteen EC2 describes. Worst are the ones paging over the keys
with a cursor — RDS's Marker, ELBv2's Marker/PageSize — where an unstable order
can omit or repeat a resource across pages rather than merely reorder them.

Two operations needed more than the shared sort:

- ListMultipartUploads violated a documented order. API_ListMultipartUploads
  publishes ascending object key, then ascending initiation time among uploads
  sharing a key. It now sorts on the initiation instant rather than its RFC 3339
  rendering, with upload ID as a last-resort tie-break — substrate's own, because
  two uploads of one key can share an instant under a controlled clock.
- DescribeInstances buckets instances into a map[string]*reservationItem and
  ranges it to build reservationSet, which the source sort cannot reach. It now
  sorts by reservation ID; AWS documents no order there, so that is substrate's
  reading.

RGTA GetResources and EC2 DescribeTags were never affected — both already sort
before paginating — and their sorts stay, because neither wants the order of the
state keys their records were read from. Both are corrected on #865, whose body
named GetResources as the worst case.

docs/services.md gains a cross-cutting section recording the guarantee with its
three provenance tiers: documented (ListMultipartUploads), cursor-implied
(ListObjectVersions, RDS's Marker, ELBv2's Marker/PageSize), and substrate's
reading (ListBuckets, DescribeRules, reservationSet).

Changing the interface contract made two in-code comments false, and both are
fixed here: ec2SortTagDescriptions asserted List documents no ordering, and
cfnRegionalStampKinds still described ECS's service and task definition as
blocked by their writer, which v0.115.0 fixed. The same self-contradiction in
docs/services.md — one passage describing tagging behaviour v0.115.0 removed,
another enumerating the reachable set correctly — is corrected too (#867).

Closes #865.
Closes #867.
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/s3_plugin.go 95.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 436a292 into main Sep 15, 2026
17 checks passed
@scttfrdmn
scttfrdmn deleted the fix/865-list-order branch September 15, 2026 03:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant