| 1 | package suite |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // FeedRow is one row of the S1 change feed, and it is the whole row. |
| 12 | // |
| 13 | // Doctrine §6: (item_id, revision, gone, touched_at) — no title, no content, |
| 14 | // no visibility flag. memoria's draft 1 carried a visible-to-team field and it |
| 15 | // was a leak: `visible=false, gone=false` told a consumer that a guest-only |
| 16 | // item existed, at what revision and when it was touched, which is what S3's |
| 17 | // "404, never 403" is there to hide. Since revision 10 (Paul, 2026-11-01) |
| 18 | // the feed carries every live item and `gone` means deleted; a row still |
| 19 | // carries nothing but these four fields, and who may open an item is S2's. |
| 20 | type FeedRow struct { |
| 21 | ItemID string `json:"item_id"` |
| 22 | Revision int64 `json:"revision"` |
| 23 | Gone bool `json:"gone"` |
| 24 | TouchedAt time.Time `json:"touched_at"` |
| 25 | } |
| 26 | |
| 27 | // Feed is one page of the change feed. The cursor is opaque: a position in the |
| 28 | // sibling's own event log, issued by the sibling and stored per consumer. |
| 29 | type Feed struct { |
| 30 | Cursor string `json:"cursor"` |
| 31 | Rows []FeedRow `json:"rows"` |
| 32 | } |
| 33 | |
| 34 | // CursorResetStatus and CursorResetBody are S1's one answer for a cursor the |
| 35 | // producer's log does not recognise — a log restored from a backup, or a |
| 36 | // cursor some other log issued (doctrine §6.6, revision 11). 409 because the |
| 37 | // request conflicts with the producer's state and is not the consumer's |
| 38 | // syntax error; the body is fixed, with no other field, so it carries no |
| 39 | // position, no count and nothing about the log. |
| 40 | const ( |
| 41 | CursorResetStatus = 409 |
| 42 | CursorResetBody = `{"code":"CURSOR_RESET"}` |
| 43 | ) |
| 44 | |
| 45 | // ErrCursorReset is DecodeS1's answer to exactly CursorResetStatus with |
| 46 | // exactly CursorResetBody: "your cursor names no position in my log". The |
| 47 | // consumer hides every item it holds from that producer on every surface |
| 48 | // and marks it pending, drops its cursor, reads from "" again, and retires |
| 49 | // as gone whatever that complete re-read does not list (doctrine §6.6). One |
| 50 | // reset per re-read: a second before the re-read reaches the tip is a |
| 51 | // failure of that producer, not another reset, and its items stay withheld. |
| 52 | // Any other 409 is a plain failure, |
| 53 | // because a reset destroys a consumer's whole copy and must not be read into |
| 54 | // a body that merely resembles one. |
| 55 | var ErrCursorReset = errors.New("suite: S1 cursor reset: the producer's log holds no such position") |
| 56 | |
| 57 | // DecodeS1 reads a change-feed page, refusing anything the contract does not |
| 58 | // allow on the wire. |
| 59 | func DecodeS1(status int, body []byte) (Feed, error) { |
| 60 | if status == CursorResetStatus && bytes.Equal(bytes.TrimSpace(body), []byte(CursorResetBody)) { |
| 61 | // Whitespace around the object is the only latitude: a trailing |
| 62 | // newline is what json.Encoder writes, and it changes no meaning. |
| 63 | return Feed{}, ErrCursorReset |
| 64 | } |
| 65 | if status < 200 || status > 299 { |
| 66 | // Not an empty feed. An empty feed means "nothing changed", and |
| 67 | // returning that for a failed call advances a consumer's cursor past |
| 68 | // changes it never saw. |
| 69 | return Feed{}, fmt.Errorf("suite: S1 answered %d", status) |
| 70 | } |
| 71 | |
| 72 | // A duplicated key is resolved silently by encoding/json, and a duplicated |
| 73 | // `gone` would lose a tombstone. Trailing content is a second document |
| 74 | // riding along unread, since Decode reads one value and stops. |
| 75 | if err := rejectAmbiguousJSON(body); err != nil { |
| 76 | return Feed{}, fmt.Errorf("suite: S1 body is ambiguous: %w", err) |
| 77 | } |
| 78 | |
| 79 | dec := json.NewDecoder(bytes.NewReader(body)) |
| 80 | // A row carrying more than the four fields is either a sibling leaking or |
| 81 | // a sibling running a different contract. Ignoring the extra field would |
| 82 | // let either go unnoticed until somebody grepped the store. |
| 83 | dec.DisallowUnknownFields() |
| 84 | |
| 85 | // Pointers so a missing field and an explicit null are both visible. Left |
| 86 | // to the plain types, either one decodes to a zero value and the row |
| 87 | // arrives looking like a real item at revision 0 that is present rather |
| 88 | // than gone. |
| 89 | var wire struct { |
| 90 | Cursor *string `json:"cursor"` |
| 91 | Rows *[]struct { |
| 92 | ItemID *string `json:"item_id"` |
| 93 | Revision *int64 `json:"revision"` |
| 94 | Gone *bool `json:"gone"` |
| 95 | TouchedAt *time.Time `json:"touched_at"` |
| 96 | } `json:"rows"` |
| 97 | } |
| 98 | if err := dec.Decode(&wire); err != nil { |
| 99 | return Feed{}, fmt.Errorf("suite: S1 body outside the contract: %w", err) |
| 100 | } |
| 101 | |
| 102 | if wire.Cursor == nil || *wire.Cursor == "" { |
| 103 | // Without a cursor a consumer cannot advance, and committing the empty |
| 104 | // one loses its place in the sibling's log. |
| 105 | return Feed{}, fmt.Errorf("suite: S1 answered without a cursor") |
| 106 | } |
| 107 | if wire.Rows == nil { |
| 108 | return Feed{}, fmt.Errorf("suite: S1 answered without a rows array") |
| 109 | } |
| 110 | |
| 111 | feed := Feed{Cursor: *wire.Cursor, Rows: make([]FeedRow, 0, len(*wire.Rows))} |
| 112 | for i, r := range *wire.Rows { |
| 113 | switch { |
| 114 | case r.ItemID == nil || *r.ItemID == "": |
| 115 | return Feed{}, fmt.Errorf("suite: S1 row %d has no item_id", i) |
| 116 | case r.Revision == nil: |
| 117 | return Feed{}, fmt.Errorf("suite: S1 row %d has no revision", i) |
| 118 | case r.Gone == nil: |
| 119 | return Feed{}, fmt.Errorf("suite: S1 row %d has no gone", i) |
| 120 | case r.TouchedAt == nil: |
| 121 | return Feed{}, fmt.Errorf("suite: S1 row %d has no touched_at", i) |
| 122 | } |
| 123 | feed.Rows = append(feed.Rows, FeedRow{ |
| 124 | ItemID: *r.ItemID, |
| 125 | Revision: *r.Revision, |
| 126 | Gone: *r.Gone, |
| 127 | TouchedAt: *r.TouchedAt, |
| 128 | }) |
| 129 | } |
| 130 | return feed, nil |
| 131 | } |
| 132 | |