oficina / memoria Public

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

Plain git — no account needed to clone.

Download

Download this file

1package memoria
2
3import "time"
4
5// Models is every model the schema generator manages. Keep it in step
6// with the structs below: `rastrillo migration generate` reads it to work
7// out what the database should look like, and `rastrillo migration check`
8// fails CI when it and the migrations disagree.
9var Models = []any{
10 &Document{},
11 &Chunk{},
12 &Connector{},
13 &SyncRun{},
14 &SourcePage{},
15 &SiblingFeed{},
16 &SiblingTally{},
17 &FactReport{},
18 &AnswerFeedback{},
19}
20
21// Document is one thing memoria holds.
22//
23// What is plaintext here is deliberate and matches Sheets' §2.2: a
24// coordinate is not content. Source, ExternalID, Revision and the
25// timestamps are how the app finds and reconciles a row, and sealing them
26// would make the store unqueryable while protecting nothing an attacker
27// with the bucket could not infer from row counts anyway. The spec's §3.2
28// lists them as known leaks rather than pretending otherwise.
29//
30// Title and Body are content and are sealed.
31type Document struct {
32 ID int64
33 // Source names where this came from — "upload" or "paste" in slice 0.
34 // Sibling sources arrive in slice 1 and carry the app's name.
35 Source string `gorm:"index;not null"`
36 // ExternalID is the id in the source system, STABLE ACROSS EDITS —
37 // a file path, a Notion page id, a sibling's item id. Keyed
38 // (seal.Fingerprint) so it is not a plaintext-derived oracle.
39 //
40 // It used to be a hash of the body, which made every edit a new
41 // document: the old one stayed indexed under the same title, and
42 // Revision could only ever increment for a byte-identical re-upload.
43 // The staleness story the spec leans on was unreachable for the case
44 // it names. Found by a code review of this branch.
45 ExternalID string `gorm:"index;not null"`
46 // BodyPrint is a keyed fingerprint of the body — the change
47 // detector. Keyed for the same reason ExternalID is.
48 BodyPrint string `gorm:"not null;default:''"`
49 // Revision increments on every change to the body, and on a local paste
50 // note's title change so full-note optimistic updates cannot lose a rename. **This is
51 // memoria's own counter and it is not what S2 is asked about.**
52 //
53 // The model said the authorization re-check was keyed on it. That is
54 // wrong for sibling content and would have been a quiet
55 // misauthorization: the revision S2 rules on is a token the *sibling*
56 // issued, memoria never invents one, and two apps counting
57 // independently agree only by accident. What this number is for is
58 // memoria-owned content under §4.2, where memoria is the app of
59 // record and its own count is the truth — and for §5.2's provenance,
60 // where a derived artefact built from revision 1 must not be served
61 // after revision 2 lands. Sibling documents carry SiblingRevision
62 // below and this counts their local edits alongside it.
63 Revision int `gorm:"not null;default:1"`
64 // SiblingItemID is the app of record's own id for this document,
65 // sealed and reversible (seal.PurposeItemID) because it goes back out
66 // on the wire in every S2 question and S3 fetch.
67 //
68 // It cannot be recovered from ExternalID, which is a keyed
69 // fingerprint — deliberately one-way, so the bucket does not confirm
70 // which documents a team holds. A one-way id is right for finding a
71 // row and useless for asking somebody about it, so both are here.
72 //
73 // Empty for memoria-owned content (§4.2), which has no app of record
74 // to ask.
75 SiblingItemID []byte
76 // SiblingRevision is the revision token the sibling issued for the
77 // body memoria holds. **Never compared, only matched for equality**
78 // (§4.1): STALE and AHEAD are the sibling's verdict on its own log,
79 // not memoria's arithmetic on two numbers, and a sibling restored to
80 // an older revision must not be second-guessed by preferring the
81 // larger one.
82 //
83 // int64 because that is what the contract module types it as. §4.1's
84 // prose calls a revision an opaque token that need not be numeric;
85 // the shipped wire shape is narrower than that sentence, and the
86 // substantive rule — equality only, never ordering — is what memoria
87 // actually depends on and is unaffected.
88 //
89 // Plaintext, like Revision, and for the same reason §3.2 gives: a
90 // coordinate is not content, and the count of edits is already
91 // inferable from row history.
92 SiblingRevision int64 `gorm:"not null;default:0"`
93 // Pending marks a sibling document held from before a CURSOR_RESET
94 // (suite doctrine §6.6, revision 11): soft-dropped, and hard-retired
95 // when the re-read reaches the tip unless the re-read lists it again.
96 // Hiding is not this column's job — the whole source is withheld while
97 // its SiblingFeed is Reconciling — so it is read only by retirement.
98 Pending bool `gorm:"not null;default:false"`
99 // SealedTitle and SealedBody are sealed under distinct HKDF purposes.
100 SealedTitle []byte `gorm:"not null"`
101 SealedBody []byte `gorm:"not null"`
102 // SealedPath is the document's ancestry in its source, outermost
103 // first — "Handbook › Onboarding" for a page nested two deep. Empty
104 // for uploads and paste, which have no ancestry.
105 //
106 // Sealed, and under its own purpose: a path is content. It is
107 // indexed in its own low-weighted column rather than folded into
108 // the heading trail, for the reason index.PathWeight gives.
109 SealedPath []byte
110 // Bytes is the plaintext length, kept so the UI can say how big a
111 // document is without opening it. A size is already inferable from
112 // the sealed length, so this leaks nothing new.
113 Bytes int
114 CreatedAt time.Time
115 UpdatedAt time.Time
116
117 Chunks []Chunk `gorm:"constraint:OnDelete:CASCADE"`
118}
119
120// Chunk is one retrievable passage of a Document.
121//
122// The searchable form of this text lives only in memory (internal/memoria/index):
123// FTS5 keeps term postings in plaintext, so an on-disk index would put
124// readable passages in the replicated file. These rows are the durable
125// copy and the index is rebuilt from them.
126type Chunk struct {
127 ID int64
128 DocumentID int64 `gorm:"index;not null"`
129 // Ordinal is the passage's position in its document, from 0 —
130 // a coordinate, plaintext, and what a citation points at.
131 Ordinal int `gorm:"not null"`
132 // SealedHeading is the heading trail ("FY2024 › Revenue"). Sealed:
133 // a heading is content, and Sheets §2.1 seals titles for the same
134 // reason.
135 SealedHeading []byte
136 SealedText []byte `gorm:"not null"`
137 CreatedAt time.Time
138}
139
140// SiblingFeed is memoria's position in one sibling's change feed.
141//
142// One row per configured app of record, created on first poll.
143type SiblingFeed struct {
144 ID int64
145 // App is the sibling's configured name, which is also the Source on
146 // every document it owns. The unique index is what makes "one
147 // position per app" a property of the schema rather than of the code
148 // that writes it.
149 App string `gorm:"uniqueIndex;not null"`
150 // SealedCursor is the sibling's own opaque cursor, sealed
151 // (seal.PurposeCursor) because memoria does not know what is in it.
152 //
153 // **Written only after a page has been fully applied.** §5.1 and gate
154 // 7: a cursor moved past a page nobody applied skips changes the
155 // sibling will never offer again, and the sibling has no way to know
156 // it happened. Every failure path in Poll returns before this column
157 // is touched.
158 SealedCursor []byte
159 // PolledAt is when the last poll DRAINED THE FEED — not when one
160 // started, and not when one got partway.
161 //
162 // It is what the sources page WILL say the corpus is current as of —
163 // the page is not built yet, and saying "is" would be claiming a
164 // reader today sees something they do not. What is true today is the
165 // column's rule: a poll that failed halfway, or stopped short, must
166 // not move it. §8.1: "a failed or stale S1 → the page states the
167 // corpus is current as of a time, rather than implying freshness".
168 PolledAt time.Time
169 // CountingSince is when this instance began keeping breaker counters
170 // for this sibling.
171 //
172 // **It is what makes "cannot determine" distinguishable from "no
173 // anomaly".** Without it, a fresh instance and a sibling that has
174 // genuinely refused nobody look identical: both have no refusals in
175 // the window and nobody in the baseline. §8.1 is explicit that this
176 // is the one place where mistaking the two would silently disable a
177 // guard rather than mis-render a page, so the instance records when
178 // it started looking and the page says so when the answer is younger
179 // than the question.
180 CountingSince time.Time
181 // Reconciling marks a source whose sibling has been restored behind
182 // memoria (§4.1's post-restore rule 1): its chunks leave the ranking
183 // entirely until a re-announce completes, so the other sources fill
184 // the page to its full depth instead of it rendering nearly empty.
185 //
186 // Set by a CURSOR_RESET (siblingsync.go cursorReset, suite doctrine
187 // §6.6) and cleared when the re-read reaches the tip; AHEAD, which this
188 // column was first added for, is still not on the wire (§13). Durable
189 // because the hibernation that is certain to happen in the middle of a
190 // re-read must not end it.
191 Reconciling bool `gorm:"not null;default:false"`
192 CreatedAt time.Time
193 UpdatedAt time.Time
194}
195
196// SiblingTally is §8.1's breaker counter: for one sibling, one asker and
197// one UTC day, how many passages that person was allowed to see and how
198// many they were refused.
199//
200// **The only per-person record memoria keeps.** §3.2's leak list carries
201// it. Two integers a day is the smallest thing that answers the question
202// the breaker asks, and the section is explicit that a per-refusal log
203// would answer it too and is not built.
204//
205// Why durable rather than in memory: memoria hibernates (§3.5), so an
206// in-memory window resets on every wake, and a breaker that can never
207// accumulate seven days of baseline is a breaker that is *off* — reading,
208// on the owner's page, as "no anomaly". That is the failure this whole
209// section exists to prevent, arriving through the storage choice.
210type SiblingTally struct {
211 ID int64
212 // App, AskerPrint and Day are the key. Unique together, so "one row
213 // per person per sibling per day" is a property of the schema rather
214 // than of the code that writes it.
215 App string `gorm:"uniqueIndex:idx_tally;not null"`
216 // AskerPrint is a KEYED, one-way fingerprint of the asker
217 // (seal.PurposeAsker). The predicate counts distinct people and
218 // compares a person against themselves across two periods; it never
219 // needs the address back, and a reversible id here would make this
220 // table a roster of who reads what.
221 AskerPrint string `gorm:"uniqueIndex:idx_tally;not null"`
222 // Day is the UTC date, "2006-01-02". Plaintext: it is the retention
223 // key and the window key, and sealing it would make the table
224 // unqueryable while hiding nothing — the row's existence already says
225 // somebody asked that day.
226 Day string `gorm:"uniqueIndex:idx_tally;index;not null"`
227 // SealedCounts holds the two integers, sealed together under
228 // seal.PurposeTally.
229 //
230 // One blob rather than two columns because they are read and written
231 // as a pair and never independently, and because a sealed integer
232 // cannot be incremented in SQL either way — the write is a
233 // read-modify-write in a transaction whichever shape this takes.
234 SealedCounts []byte `gorm:"not null"`
235 CreatedAt time.Time
236 UpdatedAt time.Time
237}
238
239// FactReport is one correction (§8.10): a person said which candidate
240// was right, or that none was, and why.
241//
242// **It holds no passage text.** The shown and chosen candidates are
243// coordinates — document id, revision, chunk id, span offsets — sealed
244// with the person's own query and note. Following a coordinate goes
245// through the document page's own §4.1 check, so a report cannot show
246// anybody a passage they may not read, and a deleted, revised or
247// withheld document leaves a coordinate that no longer resolves for
248// them, which is all it ever was (review, 2026-10-26).
249type FactReport struct {
250 ID int64
251 // ReporterPrint is a KEYED fingerprint of the reporter's address
252 // (seal.PurposeReporter): enough to list a person's own reports, and
253 // not their address. Its own purpose, so it cannot be joined against
254 // the breaker's asker prints.
255 ReporterPrint string `gorm:"index;not null"`
256 // SealedReport is the report's JSON, sealed under seal.PurposeReport.
257 SealedReport []byte `gorm:"not null"`
258 CreatedAt time.Time
259}
260
261// AnswerFeedback is one 👍 or 👎 on an AI answer (§8.11, Paul 2026-10-27:
262// "Record for review"). Like FactReport it holds coordinates and the
263// person's own words and never passage text — an AI answer IS passage
264// text, so its wording is not kept.
265type AnswerFeedback struct {
266 ID int64
267 // ReporterPrint is the same keyed print FactReport uses
268 // (seal.PurposeReporter), so a person's reports and feedback list
269 // together, and nobody else's.
270 ReporterPrint string `gorm:"index;not null"`
271 // Verdict is "up" or "down". Not content: two values, chosen by the
272 // person.
273 Verdict string `gorm:"not null"`
274 // SealedFeedback is the JSON of feedbackPayload, sealed under
275 // seal.PurposeFeedback.
276 SealedFeedback []byte `gorm:"not null"`
277 CreatedAt time.Time
278}
279