oficina / suite Public

Client decoders (revision 11): CURSOR_RESET, strict S2 rows, a real end-of-input check

- DecodeS1 returns ErrCursorReset for exactly 409 {"code":"CURSOR_RESET"}
  (CursorResetStatus, CursorResetBody); any other 409 stays a plain failure.
  Fable H1(b).
- DecodeS2 refuses fields outside (item_id, revision, result), in rows and
  beside results: the whole call is UNKNOWN. Fable M5.
- rejectAmbiguousJSON ends on io.EOF from one more Token, not Decoder.More,
  which let `{...}]` through DecodeS1 and DecodeS2. Fable L6.

Mutation-tested; three added to mutations.py (all 12 caught).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 4b876e169df488b70f35ef69c2d3b7ec3ce26890 parent c38e3ad
6 files changed, +138 −4
  • mutations.py +12 −0
  • s1.go +26 −0
  • s1_test.go +61 −1
  • s2.go +11 −2
  • s2_test.go +22 −0
  • strictjson.go +6 −1
diff --git a/mutations.py b/mutations.py
index c9993f2..3b1dbbc 100644
--- a/mutations.py
+++ b/mutations.py
@@ -41,6 +41,18 @@ MUTATIONS = [
"\tif err := rejectAmbiguousJSON(body); err != nil {\n\t\treturn allUnknown(len(asked))\n\t}",
"\tif false {\n\t\treturn allUnknown(len(asked))\n\t}"),
+ ("r11 L6: Decoder.More back as the end-of-input check", "strictjson.go",
+ "\tif _, err := dec.Token(); err != io.EOF {",
+ "\tif _ = io.EOF; dec.More() {"),
+
+ ("r11 M5: S2 result rows may carry fields outside the contract", "s2.go",
+ "\tdec.DisallowUnknownFields()\n\tvar parsed s2Body",
+ "\tvar parsed s2Body"),
+
+ ("r11 H1b: any status carrying the reset body is a reset", "s1.go",
+ "status == CursorResetStatus && bytes.Equal",
+ "true && bytes.Equal"),
+
("9: hostname labels are not validated", "transport.go",
"\t\tif err := validLabel(label); err != nil {",
"\t\tif false {\n\t\t\t_ = label\n\t\tif err := error(nil); err != nil {"),
diff --git a/s1.go b/s1.go
index ba5c3d2..32a602e 100644
--- a/s1.go
+++ b/s1.go
@@ -3,6 +3,7 @@ package suite
import (
"bytes"
"encoding/json"
+ "errors"
"fmt"
"time"
)
@@ -30,9 +31,34 @@ type Feed struct {
Rows []FeedRow `json:"rows"`
}
+// CursorResetStatus and CursorResetBody are S1's one answer for a cursor the
+// producer's log does not recognise — a log restored from a backup, or a
+// cursor some other log issued (doctrine §6.6, revision 11). 409 because the
+// request conflicts with the producer's state and is not the consumer's
+// syntax error; the body is fixed, with no other field, so it carries no
+// position, no count and nothing about the log.
+const (
+ CursorResetStatus = 409
+ CursorResetBody = `{"code":"CURSOR_RESET"}`
+)
+
+// ErrCursorReset is DecodeS1's answer to exactly CursorResetStatus with
+// exactly CursorResetBody: "your cursor names no position in my log". The
+// consumer drops every item it holds from that producer and its cursor,
+// reads from "" again, and retires whatever that complete re-read does not
+// list as if it were gone (doctrine §6.6). Any other 409 is a plain failure,
+// because a reset destroys a consumer's whole copy and must not be read into
+// a body that merely resembles one.
+var ErrCursorReset = errors.New("suite: S1 cursor reset: the producer's log holds no such position")
+
// DecodeS1 reads a change-feed page, refusing anything the contract does not
// allow on the wire.
func DecodeS1(status int, body []byte) (Feed, error) {
+ if status == CursorResetStatus && bytes.Equal(bytes.TrimSpace(body), []byte(CursorResetBody)) {
+ // Whitespace around the object is the only latitude: a trailing
+ // newline is what json.Encoder writes, and it changes no meaning.
+ return Feed{}, ErrCursorReset
+ }
if status < 200 || status > 299 {
// Not an empty feed. An empty feed means "nothing changed", and
// returning that for a failed call advances a consumer's cursor past
diff --git a/s1_test.go b/s1_test.go
index 075b573..8560d65 100644
--- a/s1_test.go
+++ b/s1_test.go
@@ -1,6 +1,9 @@
package suite
-import "testing"
+import (
+ "errors"
+ "testing"
+)
// S1 rows are (item_id, revision, gone, touched_at) and nothing else
// (doctrine §6). Draft 1 of memoria's feed carried a visible-to-team field and
@@ -128,3 +131,60 @@ func TestS1NonSuccessIsAnError(t *testing.T) {
t.Fatal("got nil error for a 503, want a refusal")
}
}
+
+// A cursor the producer's log does not recognise is answered with one fixed
+// 409, and only that exact answer is a reset (doctrine §6.6, revision 11).
+// A reset makes a consumer drop its whole copy of the producer, so a body
+// that merely resembles one — a field more, another code, a second document,
+// the right body under another status — is a plain failure, never a reset.
+//
+// MUTATION-TESTED 2026-11-01, each reverted: the status check removed (any
+// status with the body is a reset) → "the body under 200" and "under 503";
+// the body compared with bytes.Contains instead of equality → "a field
+// more"; the reset check disabled, so a 409 falls to the non-2xx failure →
+// "exact".
+func TestS1CursorReset(t *testing.T) {
+ for name, body := range map[string]string{
+ "exact": `{"code":"CURSOR_RESET"}`,
+ "with json.Encoder's newline": `{"code":"CURSOR_RESET"}` + "\n",
+ } {
+ if _, err := DecodeS1(409, []byte(body)); !errors.Is(err, ErrCursorReset) {
+ t.Errorf("%s: %v, want ErrCursorReset", name, err)
+ }
+ }
+ for name, c := range map[string]struct {
+ status int
+ body string
+ }{
+ "a field more": {409, `{"code":"CURSOR_RESET","at":"c-3"}`},
+ "another code": {409, `{"code":"CURSOR_STALE"}`},
+ "an empty 409": {409, ``},
+ "two documents": {409, `{"code":"CURSOR_RESET"}{}`},
+ "a stray bracket": {409, `{"code":"CURSOR_RESET"}]`},
+ "the body under 200": {200, `{"code":"CURSOR_RESET"}`},
+ "under 503": {503, `{"code":"CURSOR_RESET"}`},
+ } {
+ _, err := DecodeS1(c.status, []byte(c.body))
+ if err == nil || errors.Is(err, ErrCursorReset) {
+ t.Errorf("%s: %v, want a plain failure", name, err)
+ }
+ }
+}
+
+// Trailing content is a real end-of-input check. Decoder.More answers false
+// in front of a stray ']' or '}', so a complete page followed by either was
+// accepted as one document (Fable, review of c38e3ad, L6).
+//
+// MUTATION-TESTED 2026-11-01, reverted: dec.More() restored as the end check
+// in rejectAmbiguousJSON → "]" and "}" accepted.
+func TestS1TrailingDelimiterIsRefused(t *testing.T) {
+ for _, junk := range []string{"]", "}", " ]", "[]", "0"} {
+ body := `{"cursor":"c-1","rows":[]}` + junk
+ if _, err := DecodeS1(200, []byte(body)); err == nil {
+ t.Errorf("accepted %q after a page", junk)
+ }
+ }
+ if _, err := DecodeS1(200, []byte(`{"cursor":"c-1","rows":[]}`+" \n\t")); err != nil {
+ t.Errorf("trailing whitespace is not content: %v", err)
+ }
+}
diff --git a/s2.go b/s2.go
index 0ad865c..fcb24d5 100644
--- a/s2.go
+++ b/s2.go
@@ -1,6 +1,9 @@
package suite
-import "encoding/json"
+import (
+ "bytes"
+ "encoding/json"
+)
// Authorization is what S2 answers for one item, for one named asker.
//
@@ -53,8 +56,14 @@ func DecodeS2(asked []ItemRevision, status int, body []byte) []Authorization {
if err := rejectAmbiguousJSON(body); err != nil {
return allUnknown(len(asked))
}
+ // Strict, as S1 is: a result row is (item_id, revision, result) and
+ // nothing else. A row carrying a title or a passage is a sibling leaking
+ // into a decoder that would otherwise drop the field unseen; it is not an
+ // answer (Fable, review of c38e3ad, M5).
+ dec := json.NewDecoder(bytes.NewReader(body))
+ dec.DisallowUnknownFields()
var parsed s2Body
- if err := json.Unmarshal(body, &parsed); err != nil {
+ if err := dec.Decode(&parsed); err != nil {
return allUnknown(len(asked))
}
diff --git a/s2_test.go b/s2_test.go
index f7c7e04..7ac3ded 100644
--- a/s2_test.go
+++ b/s2_test.go
@@ -223,3 +223,25 @@ func TestS2DecodesAFullBatch(t *testing.T) {
assertResults(t, got, want)
}
+
+// A result row is (item_id, revision, result) and nothing more. A row
+// carrying a title is a sibling leaking an item's name into a decoder that
+// would have dropped the field unseen; it is not an answer, and the whole
+// call is UNKNOWN (Fable, review of c38e3ad, M5).
+//
+// MUTATION-TESTED 2026-11-01, each reverted: DisallowUnknownFields removed
+// → "a title in a row" and "a field beside results"; dec.More() restored
+// as rejectAmbiguousJSON's end check → "a stray brace".
+func TestS2RefusesFieldsOutsideTheContract(t *testing.T) {
+ asked := []ItemRevision{{ItemID: "doc-1", Revision: 7}}
+ for name, body := range map[string]string{
+ "a title in a row": `{"results":[{"item_id":"doc-1","revision":7,"result":"VISIBLE","title":"Q3 plan"}]}`,
+ "a field beside results": `{"results":[{"item_id":"doc-1","revision":7,"result":"VISIBLE"}],"note":"x"}`,
+ "a stray brace": `{"results":[{"item_id":"doc-1","revision":7,"result":"VISIBLE"}]}}`,
+ "a stray bracket": `{"results":[{"item_id":"doc-1","revision":7,"result":"VISIBLE"}]}]`,
+ } {
+ t.Run(name, func(t *testing.T) {
+ assertResults(t, DecodeS2(asked, 200, []byte(body)), []Authorization{UNKNOWN})
+ })
+ }
+}
diff --git a/strictjson.go b/strictjson.go
index a14cbdd..ed1db42 100644
--- a/strictjson.go
+++ b/strictjson.go
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
+ "io"
"strings"
)
@@ -28,7 +29,11 @@ func rejectAmbiguousJSON(data []byte) error {
}
// Anything after the first complete value means the body is two documents
// wearing one Content-Length, and which one is "the" answer is a guess.
- if dec.More() {
+ // The end is io.EOF from one more Token, never Decoder.More: More answers
+ // false in front of a stray ']' or '}', so `{...}]` passed as one
+ // document (Fable, review of c38e3ad, L6; the server package had already
+ // learned this from Astra, 2026-11-01).
+ if _, err := dec.Token(); err != io.EOF {
return fmt.Errorf("suite: trailing content after the JSON document")
}
return nil