oficina / memoria Public

Clone
git clone https://amadan.net/oficina/memoria

Plain git — no account needed to clone.

Download

Download this file

1package sibling
2
3import (
4 "fmt"
5 "sort"
6
7 "amadan.net/oficina/suite"
8)
9
10// Disposition is what memoria does with one candidate passage once its app
11// of record has ruled on it. §4.1's table, as a type.
12//
13// The table has five rows and **exactly one of them is silent.** Every
14// other row produces a line on the page, because the alternative is a
15// shorter answer that looks like a complete one — the plausible empty list,
16// which §4.1 says is why each value exists separately at all. A
17// synchronisation condition is not an authorization decision and must not
18// share its value; the same sentence, one layer down, says it must not
19// share its silence either.
20type Disposition uint8
21
22const (
23 // Withhold is the zero value **on purpose**, and this is the load-bearing
24 // line in the file.
25 //
26 // Go gives an unset field, a missing map entry and a
27 // forgotten-to-be-assigned variable the zero value, and here that means
28 // the safest and loudest outcome rather than the most disclosing one. If
29 // Render were iota's first constant, every one of those accidents would
30 // speak a passage nobody authorised — the exact failure `requireSession`
31 // was rewritten to fail closed for, arriving through a different door.
32 //
33 // It is loud as well as closed: an outcome nobody assigned is not the
34 // one value permitted to say nothing.
35 Withhold Disposition = iota
36
37 // Render: VISIBLE. This person may read this revision.
38 Render
39
40 // OmitSilently: NOT_VISIBLE. The only value that may be silent, and
41 // correct — a notice here would report the existence of a passage this
42 // person may not have, which is the same oracle S3's "404, never 403"
43 // and §4.2's indistinguishability were both built to deny.
44 OmitSilently
45
46 // WithholdAndRefetch: STALE. memoria is behind; the item exists and is
47 // readable and refetching fixes it. Not a denial.
48 WithholdAndRefetch
49
50 // WithholdAndReannounce: a source being re-read. memoria's rule()
51 // produces it for every passage from a source whose feed is Reconciling
52 // after a CURSOR_RESET (suite doctrine §6.6). No S2 answer maps to it:
53 // AHEAD is still not on the wire, and aheadNotYetOnTheWire below is the
54 // tripwire for the day it is.
55 WithholdAndReannounce
56
57 // numDispositions is one past the last, so a test can iterate the
58 // whole set instead of a list of the ones it remembered.
59 //
60 // It exists because a test claimed to assert something "about the
61 // whole set" while ranging a literal of five values: adding a sixth
62 // constant left it green and the new value silent. A set a test
63 // enumerates by hand is a set that stops being enumerated.
64 numDispositions
65)
66
67// Renders reports whether the passage's text may be spoken.
68func (d Disposition) Renders() bool { return d == Render }
69
70// Notices reports whether the page must say something was withheld.
71//
72// Not the negation of Renders: `OmitSilently` neither renders nor notices,
73// and it is the only disposition that does neither.
74//
75// **Written as the two exceptions rather than as a list of the loud
76// values, and that is the whole point.** The list version enumerated
77// Withhold, WithholdAndRefetch and WithholdAndReannounce, so a sixth
78// constant added to the block below was born silent — it rendered
79// nothing and said nothing, a second value permitted to hide a passage
80// without admitting to it. A review found it by appending one constant
81// and touching nothing else, which is precisely the edit
82// TestAheadIsStillAShippingLag instructs the next person to make when the
83// module ships AHEAD.
84//
85// Written this way, a new disposition is loud until somebody deliberately
86// makes it quiet. That is the direction the default should point.
87func (d Disposition) Notices() bool {
88 return d != Render && d != OmitSilently
89}
90
91// Decide maps one S2 answer to what memoria does about it. §4.1's table.
92//
93// **Total, with the default on the safe side.** An answer this client does
94// not recognise withholds loudly; it never falls through to omission. Gate
95// 6's mutation is exactly this — a sixth value `PERHAPS` in the fixture,
96// which must go red rather than be quietly dropped — and the reason is that
97// omission is the one outcome a reader cannot tell from an honest absence.
98//
99// The module folds any unrecognised wire word to UNKNOWN before this is
100// reached (`known`, suite/s2.go), so in practice the default arm is a
101// second line of the same defence rather than the only one. Both are here
102// because the module's rule is the module's to change.
103func Decide(a suite.Authorization) Disposition {
104 switch a {
105 case suite.VISIBLE:
106 return Render
107 case suite.NOT_VISIBLE:
108 return OmitSilently
109 case suite.STALE:
110 return WithholdAndRefetch
111 case suite.UNKNOWN:
112 return Withhold
113 }
114 return Withhold
115}
116
117// aheadNotYetOnTheWire is the fifth row of §4.1's table, and it is not a
118// constant this client can switch on.
119//
120// The module this repository pins carries four values. `AHEAD` is adopted —
121// suite doctrine revision 10, settled 2026-09-06 on memoria's own argument
122// — and lands on a branch that has not shipped, held by suite CI being
123// unconfigured (§13). Until it does, a sibling answering "AHEAD" is folded
124// to UNKNOWN by the module and withheld loudly here: **safe, and lossy.**
125// What is lost is the marking for re-announce, which is the collapse the
126// fifth value was cut to prevent.
127//
128// This constant exists so a test can assert the lag is still a lag. See
129// TestAheadIsStillAShippingLag: it goes red on the day the module gains the
130// value, which is the day `Decide` owes it an arm and this file owes the
131// paragraph above a deletion. A shipping dependency that nothing checks is
132// a shipping dependency nobody notices has arrived.
133const aheadNotYetOnTheWire = suite.Authorization("AHEAD")
134
135// Outcome accumulates one page's loud withholdings, per source app.
136//
137// **It is told an app and a disposition, and never an item.** §6.5 says the
138// page names what is missing and from which app; it does not name what was
139// missed, and the way to guarantee that is to give this type no way to
140// learn it. A parameter that is not there cannot reach a log line, a notice
141// or a metric label — which is the same argument gate 4 makes about
142// content in logs, made structurally instead of by grep.
143//
144// Counts are carried for the withholding that names a number and **not**
145// for reconciling.
146//
147// §8.1 gives counted copy for one value — *"Sheets did not answer; 7
148// results withheld."* — and this said "the two dispositions §8.1's copy
149// counts", which §8.1 does not support: there is no STALE line there. The
150// refetch line's count is this file's own choice, made for the same reason
151// and said plainly here rather than borrowed from a section that does not
152// say it.
153//
154// Reconciling carries no count because §4.1's post-restore rule 1 requires
155// one line for the source rather than one per withheld passage: a source
156// being reconciled leaves the ranking wholesale, so the number of passages
157// it would have contributed describes a page that was never built.
158type Outcome struct {
159 counts map[noticeKey]int
160 order []noticeKey
161}
162
163type noticeKey struct {
164 app string
165 kind Disposition
166}
167
168// Record files one ruling against the page. Dispositions that render or
169// omit silently are no-ops here, which is what "silent" means.
170func (o *Outcome) Record(app string, d Disposition) {
171 if !d.Notices() {
172 return
173 }
174 if o.counts == nil {
175 o.counts = make(map[noticeKey]int)
176 }
177 k := noticeKey{app: app, kind: d}
178 if _, seen := o.counts[k]; !seen {
179 o.order = append(o.order, k)
180 }
181 o.counts[k]++
182}
183
184// Lines renders the page's withholding notices in the order they were
185// recorded, so a page is stable across reloads.
186//
187// That is **candidate order, not arrival order** — the caller records
188// after the fan-in, and the ordering the reader gets is the ordering of
189// the results the lines are about. This comment said "the order the
190// sources were first heard from", which describes ordering by which
191// sibling answered first: the one thing a concurrent fan-out must not let
192// the page depend on.
193func (o *Outcome) Lines() []string {
194 out := make([]string, 0, len(o.order))
195 for _, k := range o.order {
196 n := o.counts[k]
197 switch k.kind {
198 case Withhold:
199 out = append(out, fmt.Sprintf("%s did not answer; %s withheld.",
200 k.app, results(n)))
201 case WithholdAndRefetch:
202 out = append(out, fmt.Sprintf("%s has moved on from %s; refetching.",
203 k.app, results(n)))
204 case WithholdAndReannounce:
205 // No count, deliberately. Rule 1 of §4.1's post-restore
206 // reconciliation: the source leaves the ranking entirely, so the
207 // number of passages it would have contributed is not a number
208 // about this page.
209 out = append(out, fmt.Sprintf(
210 "%s is being re-read after a restore; its results are not on this page.",
211 k.app))
212 }
213 }
214 return out
215}
216
217// SourcesToReannounce lists the apps §4.1 requires memoria to mark for a
218// full re-announce. **It never lists an app to delete from**: `AHEAD` is
219// not a reason to remove anything, and the only thing that ever retires a
220// phantom chunk is a re-announce that reached its closing marker.
221func (o *Outcome) SourcesToReannounce() []string {
222 var out []string
223 for _, k := range o.order {
224 if k.kind == WithholdAndReannounce {
225 out = append(out, k.app)
226 }
227 }
228 sort.Strings(out)
229 return out
230}
231
232func results(n int) string {
233 if n == 1 {
234 return "1 result"
235 }
236 return fmt.Sprintf("%d results", n)
237}
238