|
1
|
+ |
defmodule OpenAgents.Memories.SystemMemoryTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
The system bucket: what it refuses, what a status is derived from, who may
|
|
4
|
+ |
admit, who may correct, and what it is still not allowed to reach.
|
|
5
|
+ |
|
|
6
|
+ |
Four properties carry the weight here, and each of them is the kind that
|
|
7
|
+ |
holds until somebody adds a second route to the same table.
|
|
8
|
+ |
|
|
9
|
+ |
An evidence-free candidate is refused **by the database**, so these tests
|
|
10
|
+ |
insert around the changeset rather than through it. A validation refused only
|
|
11
|
+ |
in `changeset/2` is an application filter, and MEMORY-004's discipline is
|
|
12
|
+ |
that a boundary is a predicate the store enforces.
|
|
13
|
+ |
|
|
14
|
+ |
Status is derived from the admission records, never read from the author's
|
|
15
|
+ |
own field, so an author who writes `admitted` on their own row is still
|
|
16
|
+ |
proposing.
|
|
17
|
+ |
|
|
18
|
+ |
Only a steward admits, and only the author or a steward corrects. A caller
|
|
19
|
+ |
with no standing is refused without learning anything about the row.
|
|
20
|
+ |
|
|
21
|
+ |
And the bucket surfaces to nobody. A system memory that is stored and
|
|
22
|
+ |
admitted but recalled by no session is the shippable state; recall reaching
|
|
23
|
+ |
it would be cross-account recall, which MEMORY-001 and MEMORY-010 forbid.
|
|
24
|
+ |
"""
|
|
25
|
+ |
use OpenAgents.DataCase, async: true
|
|
26
|
+ |
|
|
27
|
+ |
alias OpenAgents.Memories
|
|
28
|
+ |
alias OpenAgents.Memories.{Admission, Admissions, Memory, Recall}
|
|
29
|
+ |
|
|
30
|
+ |
# The owner account is an operator by definition (`@owner_github_id`), so a
|
|
31
|
+ |
# steward needs no configuration change and these tests stay async.
|
|
32
|
+ |
@owner_github_id 14_167_547
|
|
33
|
+ |
|
|
34
|
+ |
defp account(key) do
|
|
35
|
+ |
digest = :crypto.hash(:sha256, key)
|
|
36
|
+ |
github_id = digest |> binary_part(0, 7) |> :binary.decode_unsigned()
|
|
37
|
+ |
|
|
38
|
+ |
upsert(github_id, "sysmem-" <> (digest |> Base.encode16(case: :lower) |> binary_part(0, 12)))
|
|
39
|
+ |
end
|
|
40
|
+ |
|
|
41
|
+ |
defp steward, do: upsert(@owner_github_id, "AtlantisPleb")
|
|
42
|
+ |
|
|
43
|
+ |
defp upsert(github_id, login) do
|
|
44
|
+ |
{:ok, user} =
|
|
45
|
+ |
OpenAgents.Accounts.upsert_github_user(%{
|
|
46
|
+ |
github_id: github_id,
|
|
47
|
+ |
github_login: login,
|
|
48
|
+ |
github_avatar_url: "https://avatars.githubusercontent.com/u/#{github_id}?v=4"
|
|
49
|
+ |
})
|
|
50
|
+ |
|
|
51
|
+ |
user
|
|
52
|
+ |
end
|
|
53
|
+ |
|
|
54
|
+ |
defp evidence do
|
|
55
|
+ |
[%{"kind" => "receipt", "ref" => "receipt:4f1c", "digest" => "sha256:9ab3"}]
|
|
56
|
+ |
end
|
|
57
|
+ |
|
|
58
|
+ |
defp candidate(overrides \\ %{}) do
|
|
59
|
+ |
Map.merge(
|
|
60
|
+ |
%{
|
|
61
|
+ |
"bucket" => "system",
|
|
62
|
+ |
"slug" => "sys:gateway-402-retired-model",
|
|
63
|
+ |
"body" =>
|
|
64
|
+ |
"A 402 from the inference gateway usually means the default model was " <>
|
|
65
|
+ |
"retired upstream. Check gateway status before bisecting local lanes.",
|
|
66
|
+ |
"entity" => "inference-gateway",
|
|
67
|
+ |
"tier" => "ledger",
|
|
68
|
+ |
"as_of" => ~D[2026-08-25],
|
|
69
|
+ |
"admission" => "candidate",
|
|
70
|
+ |
"evidence_refs" => evidence()
|
|
71
|
+ |
},
|
|
72
|
+ |
overrides
|
|
73
|
+ |
)
|
|
74
|
+ |
end
|
|
75
|
+ |
|
|
76
|
+ |
# A row assembled as a struct rather than through `changeset/2`. This is the
|
|
77
|
+ |
# second route the constraint exists for.
|
|
78
|
+ |
defp around_the_changeset(user, overrides) do
|
|
79
|
+ |
fields =
|
|
80
|
+ |
Map.merge(
|
|
81
|
+ |
%{
|
|
82
|
+ |
user_id: user.id,
|
|
83
|
+ |
bucket: "system",
|
|
84
|
+ |
body: "Written around the write path.",
|
|
85
|
+ |
slug: "sys:around-the-write-path",
|
|
86
|
+ |
tier: "ledger",
|
|
87
|
+ |
as_of: ~D[2026-08-25],
|
|
88
|
+ |
admission: "candidate",
|
|
89
|
+ |
evidence_refs: evidence()
|
|
90
|
+ |
},
|
|
91
|
+ |
overrides
|
|
92
|
+ |
)
|
|
93
|
+ |
|
|
94
|
+ |
Repo.insert(struct(Memory, fields))
|
|
95
|
+ |
end
|
|
96
|
+ |
|
|
97
|
+ |
describe "the system fields" do
|
|
98
|
+ |
test "round-trip on a memory row" do
|
|
99
|
+ |
author = account("system-round-trip")
|
|
100
|
+ |
|
|
101
|
+ |
assert {:ok, memory} = Memories.create(author, candidate())
|
|
102
|
+ |
|
|
103
|
+ |
reread = Repo.get!(Memory, memory.id)
|
|
104
|
+ |
|
|
105
|
+ |
assert reread.bucket == "system"
|
|
106
|
+ |
assert reread.slug == "sys:gateway-402-retired-model"
|
|
107
|
+ |
assert reread.entity == "inference-gateway"
|
|
108
|
+ |
assert reread.tier == "ledger"
|
|
109
|
+ |
assert reread.as_of == ~D[2026-08-25]
|
|
110
|
+ |
assert reread.admission == "candidate"
|
|
111
|
+ |
|
|
112
|
+ |
assert reread.evidence_refs == [
|
|
113
|
+ |
%{"kind" => "receipt", "ref" => "receipt:4f1c", "digest" => "sha256:9ab3"}
|
|
114
|
+ |
]
|
|
115
|
+ |
end
|
|
116
|
+ |
|
|
117
|
+ |
# `as_of` dates the claim and `inserted_at` orders the chain, so a claim
|
|
118
|
+ |
# observed true last year is still written today and still reads as dated.
|
|
119
|
+ |
test "as_of is the claim's date, not the row's" do
|
|
120
|
+ |
author = account("system-as-of")
|
|
121
|
+ |
|
|
122
|
+ |
{:ok, memory} = Memories.create(author, candidate(%{"as_of" => ~D[2025-01-09]}))
|
|
123
|
+ |
|
|
124
|
+ |
assert memory.as_of == ~D[2025-01-09]
|
|
125
|
+ |
assert DateTime.to_date(memory.inserted_at) != memory.as_of
|
|
126
|
+ |
end
|
|
127
|
+ |
|
|
128
|
+ |
test "a system memory needs a slug, a tier, a date, and an admission" do
|
|
129
|
+ |
author = account("system-required")
|
|
130
|
+ |
|
|
131
|
+ |
for {field, key} <- [{"slug", :slug}, {"tier", :tier}, {"as_of", :as_of}] do
|
|
132
|
+ |
assert {:error, changeset} = Memories.create(author, Map.put(candidate(), field, nil))
|
|
133
|
+ |
assert Map.has_key?(errors_on(changeset), key)
|
|
134
|
+ |
end
|
|
135
|
+ |
|
|
136
|
+ |
assert {:error, changeset} = Memories.create(author, Map.put(candidate(), "admission", nil))
|
|
137
|
+ |
assert Map.has_key?(errors_on(changeset), :admission)
|
|
138
|
+ |
end
|
|
139
|
+ |
|
|
140
|
+ |
test "the slug carries the sys: prefix" do
|
|
141
|
+ |
author = account("system-slug")
|
|
142
|
+ |
|
|
143
|
+ |
assert {:error, changeset} =
|
|
144
|
+ |
Memories.create(author, candidate(%{"slug" => "gateway-402"}))
|
|
145
|
+ |
|
|
146
|
+ |
assert %{slug: _refused} = errors_on(changeset)
|
|
147
|
+ |
end
|
|
148
|
+ |
|
|
149
|
+ |
test "a user or learned row carries none of them" do
|
|
150
|
+ |
author = account("system-fields-elsewhere")
|
|
151
|
+ |
|
|
152
|
+ |
assert {:error, changeset} =
|
|
153
|
+ |
Memories.create(author, %{
|
|
154
|
+ |
"body" => "I use pnpm.",
|
|
155
|
+ |
"bucket" => "user",
|
|
156
|
+ |
"tier" => "ledger"
|
|
157
|
+ |
})
|
|
158
|
+ |
|
|
159
|
+ |
assert %{tier: _refused} = errors_on(changeset)
|
|
160
|
+ |
|
|
161
|
+ |
assert {:error, changeset} =
|
|
162
|
+ |
Memories.create(author, %{
|
|
163
|
+ |
"body" => "The migration runs first.",
|
|
164
|
+ |
"bucket" => "learned",
|
|
165
|
+ |
"evidence_refs" => evidence()
|
|
166
|
+ |
})
|
|
167
|
+ |
|
|
168
|
+ |
assert %{evidence_refs: _refused} = errors_on(changeset)
|
|
169
|
+ |
end
|
|
170
|
+ |
|
|
171
|
+ |
test "an ordinary memory is unaffected" do
|
|
172
|
+ |
author = account("system-unaffected")
|
|
173
|
+ |
|
|
174
|
+ |
assert {:ok, plain} = Memories.create(author, %{"body" => "I use pnpm, not npm."})
|
|
175
|
+ |
|
|
176
|
+ |
assert plain.bucket == "user"
|
|
177
|
+ |
assert plain.slug == nil
|
|
178
|
+ |
assert plain.tier == nil
|
|
179
|
+ |
assert plain.as_of == nil
|
|
180
|
+ |
assert plain.admission == nil
|
|
181
|
+ |
assert plain.evidence_refs == nil
|
|
182
|
+ |
end
|
|
183
|
+ |
end
|
|
184
|
+ |
|
|
185
|
+ |
describe "evidence is unrepresentable when absent" do
|
|
186
|
+ |
test "the write path refuses a candidate with no evidence" do
|
|
187
|
+ |
author = account("evidence-write-path")
|
|
188
|
+ |
|
|
189
|
+ |
assert {:error, changeset} =
|
|
190
|
+ |
Memories.create(author, candidate(%{"evidence_refs" => []}))
|
|
191
|
+ |
|
|
192
|
+ |
assert %{evidence_refs: _refused} = errors_on(changeset)
|
|
193
|
+ |
|
|
194
|
+ |
assert {:error, changeset} =
|
|
195
|
+ |
Memories.create(author, candidate(%{"evidence_refs" => nil}))
|
|
196
|
+ |
|
|
197
|
+ |
assert %{evidence_refs: _refused} = errors_on(changeset)
|
|
198
|
+ |
end
|
|
199
|
+ |
|
|
200
|
+ |
test "the write path refuses an evidence ref missing its digest or its kind" do
|
|
201
|
+ |
author = account("evidence-shape")
|
|
202
|
+ |
|
|
203
|
+ |
for broken <- [
|
|
204
|
+ |
%{"kind" => "receipt", "ref" => "receipt:4f1c"},
|
|
205
|
+ |
%{"kind" => "rumour", "ref" => "receipt:4f1c", "digest" => "sha256:9ab3"},
|
|
206
|
+ |
%{"kind" => "url", "ref" => "", "digest" => "sha256:9ab3"}
|
|
207
|
+ |
] do
|
|
208
|
+ |
assert {:error, changeset} =
|
|
209
|
+ |
Memories.create(author, candidate(%{"evidence_refs" => [broken]}))
|
|
210
|
+ |
|
|
211
|
+ |
assert %{evidence_refs: _refused} = errors_on(changeset)
|
|
212
|
+ |
end
|
|
213
|
+ |
end
|
|
214
|
+ |
|
|
215
|
+ |
# The point of the whole exercise: not "the changeset refuses it" but "the
|
|
216
|
+ |
# table has no row shaped like that", so a second write path cannot reopen
|
|
217
|
+ |
# the hole.
|
|
218
|
+ |
test "the database refuses an evidence-free candidate inserted around the changeset" do
|
|
219
|
+ |
author = account("evidence-constraint")
|
|
220
|
+ |
|
|
221
|
+ |
assert_raise Ecto.ConstraintError, ~r/memories_system_shape/, fn ->
|
|
222
|
+ |
around_the_changeset(author, %{evidence_refs: []})
|
|
223
|
+ |
end
|
|
224
|
+ |
|
|
225
|
+ |
assert_raise Ecto.ConstraintError, ~r/memories_system_shape/, fn ->
|
|
226
|
+ |
around_the_changeset(author, %{evidence_refs: nil})
|
|
227
|
+ |
end
|
|
228
|
+ |
end
|
|
229
|
+ |
|
|
230
|
+ |
test "the database refuses an evidence ref that names no digest" do
|
|
231
|
+ |
author = account("evidence-constraint-shape")
|
|
232
|
+ |
|
|
233
|
+ |
assert_raise Ecto.ConstraintError, ~r/memories_system_shape/, fn ->
|
|
234
|
+ |
around_the_changeset(author, %{
|
|
235
|
+ |
evidence_refs: [%{"kind" => "url", "ref" => "https://openagents.com/"}]
|
|
236
|
+ |
})
|
|
237
|
+ |
end
|
|
238
|
+ |
end
|
|
239
|
+ |
|
|
240
|
+ |
test "the database refuses a tier below ledger, around the changeset and through it" do
|
|
241
|
+ |
author = account("tier-constraint")
|
|
242
|
+ |
|
|
243
|
+ |
assert {:error, changeset} = Memories.create(author, candidate(%{"tier" => "pulse"}))
|
|
244
|
+ |
assert %{tier: _refused} = errors_on(changeset)
|
|
245
|
+ |
|
|
246
|
+ |
assert_raise Ecto.ConstraintError, ~r/memories_system_shape/, fn ->
|
|
247
|
+ |
around_the_changeset(author, %{tier: "dark"})
|
|
248
|
+ |
end
|
|
249
|
+ |
end
|
|
250
|
+ |
|
|
251
|
+ |
test "the database refuses system fields on a learned row" do
|
|
252
|
+ |
author = account("bucket-constraint")
|
|
253
|
+ |
|
|
254
|
+ |
assert_raise Ecto.ConstraintError, ~r/memories_system_shape/, fn ->
|
|
255
|
+ |
around_the_changeset(author, %{bucket: "learned"})
|
|
256
|
+ |
end
|
|
257
|
+ |
end
|
|
258
|
+ |
end
|
|
259
|
+ |
|
|
260
|
+ |
describe "derived admission status" do
|
|
261
|
+ |
test "a candidate with no record behind it is a candidate" do
|
|
262
|
+ |
author = account("status-candidate")
|
|
263
|
+ |
|
|
264
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
265
|
+ |
|
|
266
|
+ |
assert Admissions.status(memory) == "candidate"
|
|
267
|
+ |
end
|
|
268
|
+ |
|
|
269
|
+ |
test "an author's self-claimed admitted still reads as a candidate" do
|
|
270
|
+ |
author = account("status-self-claimed")
|
|
271
|
+ |
|
|
272
|
+ |
{:ok, memory} = Memories.create(author, candidate(%{"admission" => "admitted"}))
|
|
273
|
+ |
|
|
274
|
+ |
assert memory.admission == "admitted"
|
|
275
|
+ |
assert Admissions.status(memory) == "candidate"
|
|
276
|
+ |
end
|
|
277
|
+ |
|
|
278
|
+ |
test "a steward's verdict is what the status is read from" do
|
|
279
|
+ |
author = account("status-admitted")
|
|
280
|
+ |
admitting = steward()
|
|
281
|
+ |
|
|
282
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
283
|
+ |
|
|
284
|
+ |
assert {:ok, record} =
|
|
285
|
+ |
Admissions.record(admitting, memory.id, %{
|
|
286
|
+ |
"verdict" => "admitted",
|
|
287
|
+ |
"ground" => "The receipt shows the 402 and the retirement together."
|
|
288
|
+ |
})
|
|
289
|
+ |
|
|
290
|
+ |
assert record.slug == "adm:" <> memory.id
|
|
291
|
+ |
assert record.role == "admission"
|
|
292
|
+ |
assert record.steward_id == admitting.id
|
|
293
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
294
|
+ |
end
|
|
295
|
+ |
|
|
296
|
+ |
test "a rejection reads as rejected, and a later verdict replaces an earlier one" do
|
|
297
|
+ |
author = account("status-rejected")
|
|
298
|
+ |
admitting = steward()
|
|
299
|
+ |
|
|
300
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
301
|
+ |
|
|
302
|
+ |
{:ok, _rejected} =
|
|
303
|
+ |
Admissions.record(admitting, memory.id, %{
|
|
304
|
+ |
"verdict" => "rejected",
|
|
305
|
+ |
"ground" => "The digest does not match the receipt it names."
|
|
306
|
+ |
})
|
|
307
|
+ |
|
|
308
|
+ |
assert Admissions.status(memory) == "rejected"
|
|
309
|
+ |
|
|
310
|
+ |
{:ok, _admitted} =
|
|
311
|
+ |
Admissions.record(admitting, memory.id, %{
|
|
312
|
+ |
"verdict" => "admitted",
|
|
313
|
+ |
"ground" => "The author re-cited the receipt and the digest matches."
|
|
314
|
+ |
})
|
|
315
|
+ |
|
|
316
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
317
|
+ |
|
|
318
|
+ |
# Append-only: the reversal is a second record, not an edit of the first.
|
|
319
|
+ |
assert Enum.map(Admissions.list(memory), & &1.verdict) == ["rejected", "admitted"]
|
|
320
|
+ |
end
|
|
321
|
+ |
|
|
322
|
+ |
test "a memory outside the system bucket has no admission status" do
|
|
323
|
+ |
author = account("status-other-bucket")
|
|
324
|
+ |
|
|
325
|
+ |
{:ok, plain} = Memories.create(author, %{"body" => "I use pnpm."})
|
|
326
|
+ |
|
|
327
|
+ |
assert Admissions.status(plain) == nil
|
|
328
|
+ |
end
|
|
329
|
+ |
end
|
|
330
|
+ |
|
|
331
|
+ |
describe "only a steward admits" do
|
|
332
|
+ |
test "an ordinary account is refused" do
|
|
333
|
+ |
author = account("admit-non-steward")
|
|
334
|
+ |
other = account("admit-non-steward-other")
|
|
335
|
+ |
|
|
336
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
337
|
+ |
|
|
338
|
+ |
assert {:error, :steward_required} =
|
|
339
|
+ |
Admissions.record(other, memory.id, %{
|
|
340
|
+ |
"verdict" => "admitted",
|
|
341
|
+ |
"ground" => "I say so."
|
|
342
|
+ |
})
|
|
343
|
+ |
|
|
344
|
+ |
# And the refusal changes nothing.
|
|
345
|
+ |
assert Admissions.status(memory) == "candidate"
|
|
346
|
+ |
assert Admissions.list(memory) == []
|
|
347
|
+ |
end
|
|
348
|
+ |
|
|
349
|
+ |
test "the author cannot admit their own candidate" do
|
|
350
|
+ |
author = account("admit-self")
|
|
351
|
+ |
|
|
352
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
353
|
+ |
|
|
354
|
+ |
assert {:error, :steward_required} =
|
|
355
|
+ |
Admissions.record(author, memory.id, %{
|
|
356
|
+ |
"verdict" => "admitted",
|
|
357
|
+ |
"ground" => "It is true."
|
|
358
|
+ |
})
|
|
359
|
+ |
end
|
|
360
|
+ |
|
|
361
|
+ |
test "a record needs a verdict and a ground" do
|
|
362
|
+ |
author = account("admit-shape")
|
|
363
|
+ |
admitting = steward()
|
|
364
|
+ |
|
|
365
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
366
|
+ |
|
|
367
|
+ |
assert {:error, changeset} =
|
|
368
|
+ |
Admissions.record(admitting, memory.id, %{"verdict" => "maybe", "ground" => "Hm."})
|
|
369
|
+ |
|
|
370
|
+ |
assert %{verdict: _refused} = errors_on(changeset)
|
|
371
|
+ |
|
|
372
|
+ |
assert {:error, changeset} =
|
|
373
|
+ |
Admissions.record(admitting, memory.id, %{"verdict" => "admitted"})
|
|
374
|
+ |
|
|
375
|
+ |
assert %{ground: _refused} = errors_on(changeset)
|
|
376
|
+ |
end
|
|
377
|
+ |
|
|
378
|
+ |
# The composite foreign key, not a read. A candidate outside the system
|
|
379
|
+ |
# bucket is not admissible, and saying so costs no query across an account.
|
|
380
|
+ |
test "a record naming a memory outside the system bucket is refused" do
|
|
381
|
+ |
author = account("admit-wrong-bucket")
|
|
382
|
+ |
admitting = steward()
|
|
383
|
+ |
|
|
384
|
+ |
{:ok, plain} = Memories.create(author, %{"body" => "I use pnpm."})
|
|
385
|
+ |
|
|
386
|
+ |
assert {:error, :not_found} =
|
|
387
|
+ |
Admissions.record(admitting, plain.id, %{
|
|
388
|
+ |
"verdict" => "admitted",
|
|
389
|
+ |
"ground" => "Not a system memory."
|
|
390
|
+ |
})
|
|
391
|
+ |
|
|
392
|
+ |
assert Repo.aggregate(Admission, :count) == 0
|
|
393
|
+ |
end
|
|
394
|
+ |
|
|
395
|
+ |
test "a record naming no memory at all is refused" do
|
|
396
|
+ |
admitting = steward()
|
|
397
|
+ |
|
|
398
|
+ |
assert {:error, :not_found} =
|
|
399
|
+ |
Admissions.record(admitting, "not-a-uuid", %{
|
|
400
|
+ |
"verdict" => "admitted",
|
|
401
|
+ |
"ground" => "Nothing."
|
|
402
|
+ |
})
|
|
403
|
+ |
|
|
404
|
+ |
assert {:error, :not_found} =
|
|
405
|
+ |
Admissions.record(admitting, Ecto.UUID.generate(), %{
|
|
406
|
+ |
"verdict" => "admitted",
|
|
407
|
+ |
"ground" => "Nothing."
|
|
408
|
+ |
})
|
|
409
|
+ |
end
|
|
410
|
+ |
end
|
|
411
|
+ |
|
|
412
|
+ |
describe "supersession on a system slug" do
|
|
413
|
+ |
test "the author corrects their own claim" do
|
|
414
|
+ |
author = account("supersede-author")
|
|
415
|
+ |
|
|
416
|
+ |
{:ok, wrong} = Memories.create(author, candidate())
|
|
417
|
+ |
|
|
418
|
+ |
assert {:ok, right} =
|
|
419
|
+ |
Admissions.supersede(
|
|
420
|
+ |
author,
|
|
421
|
+ |
wrong.id,
|
|
422
|
+ |
candidate(%{"body" => "A 402 is the retired-model signal. Check status first."})
|
|
423
|
+ |
)
|
|
424
|
+ |
|
|
425
|
+ |
assert Repo.get!(Memory, wrong.id).superseded_by_id == right.id
|
|
426
|
+ |
assert right.user_id == author.id
|
|
427
|
+ |
end
|
|
428
|
+ |
|
|
429
|
+ |
test "a steward corrects another account's claim" do
|
|
430
|
+ |
author = account("supersede-steward")
|
|
431
|
+ |
correcting = steward()
|
|
432
|
+ |
|
|
433
|
+ |
{:ok, wrong} = Memories.create(author, candidate())
|
|
434
|
+ |
|
|
435
|
+ |
assert {:ok, right} =
|
|
436
|
+ |
Admissions.supersede(
|
|
437
|
+ |
correcting,
|
|
438
|
+ |
wrong.id,
|
|
439
|
+ |
candidate(%{"body" => "The gateway 402s when the default model is retired."})
|
|
440
|
+ |
)
|
|
441
|
+ |
|
|
442
|
+ |
assert Repo.get!(Memory, wrong.id).superseded_by_id == right.id
|
|
443
|
+ |
assert right.user_id == correcting.id
|
|
444
|
+ |
end
|
|
445
|
+ |
|
|
446
|
+ |
test "anyone else is refused, and the row is untouched" do
|
|
447
|
+ |
author = account("supersede-stranger")
|
|
448
|
+ |
stranger = account("supersede-stranger-other")
|
|
449
|
+ |
|
|
450
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
451
|
+ |
|
|
452
|
+ |
assert {:error, :not_supersedable} =
|
|
453
|
+ |
Admissions.supersede(stranger, memory.id, candidate(%{"body" => "Mine now."}))
|
|
454
|
+ |
|
|
455
|
+ |
assert Repo.get!(Memory, memory.id).superseded_by_id == nil
|
|
456
|
+ |
|
|
457
|
+ |
# The refusal is a rollback, not a half-write: the replacement the
|
|
458
|
+ |
# stranger proposed does not survive it.
|
|
459
|
+ |
assert Memories.list(stranger, bucket: "system") == []
|
|
460
|
+ |
end
|
|
461
|
+ |
|
|
462
|
+ |
test "an already superseded claim is not superseded twice" do
|
|
463
|
+ |
author = account("supersede-twice")
|
|
464
|
+ |
|
|
465
|
+ |
{:ok, first} = Memories.create(author, candidate())
|
|
466
|
+ |
{:ok, _second} = Admissions.supersede(author, first.id, candidate(%{"body" => "Second."}))
|
|
467
|
+ |
|
|
468
|
+ |
assert {:error, :not_supersedable} =
|
|
469
|
+ |
Admissions.supersede(author, first.id, candidate(%{"body" => "Third."}))
|
|
470
|
+ |
end
|
|
471
|
+ |
|
|
472
|
+ |
test "an unreadable target is refused rather than raised" do
|
|
473
|
+ |
author = account("supersede-unreadable")
|
|
474
|
+ |
|
|
475
|
+ |
assert {:error, :not_supersedable} =
|
|
476
|
+ |
Admissions.supersede(author, "not-a-uuid", candidate())
|
|
477
|
+ |
end
|
|
478
|
+ |
end
|
|
479
|
+ |
|
|
480
|
+ |
# MEMORY-001 and MEMORY-010. The whole bucket is stored and admitted, and
|
|
481
|
+ |
# surfaced to nobody. Widening this is a privacy decision that belongs to the
|
|
482
|
+ |
# recall issue, and these assertions are what make it a decision rather than
|
|
483
|
+ |
# a side effect.
|
|
484
|
+ |
describe "the recall boundary" do
|
|
485
|
+ |
test "an admitted system memory never reaches another account's turn" do
|
|
486
|
+ |
author = account("recall-boundary-author")
|
|
487
|
+ |
reader = account("recall-boundary-reader")
|
|
488
|
+ |
admitting = steward()
|
|
489
|
+ |
|
|
490
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
491
|
+ |
|
|
492
|
+ |
{:ok, _record} =
|
|
493
|
+ |
Admissions.record(admitting, memory.id, %{
|
|
494
|
+ |
"verdict" => "admitted",
|
|
495
|
+ |
"ground" => "The receipt supports the claim."
|
|
496
|
+ |
})
|
|
497
|
+ |
|
|
498
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
499
|
+ |
|
|
500
|
+ |
assert %Recall{memories: []} =
|
|
501
|
+ |
Memories.recall(reader, "the inference gateway returned 402")
|
|
502
|
+ |
end
|
|
503
|
+ |
|
|
504
|
+ |
test "nor its own author's turn" do
|
|
505
|
+ |
author = account("recall-boundary-own")
|
|
506
|
+ |
admitting = steward()
|
|
507
|
+ |
|
|
508
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
509
|
+ |
|
|
510
|
+ |
{:ok, _record} =
|
|
511
|
+ |
Admissions.record(admitting, memory.id, %{
|
|
512
|
+ |
"verdict" => "admitted",
|
|
513
|
+ |
"ground" => "The receipt supports the claim."
|
|
514
|
+ |
})
|
|
515
|
+ |
|
|
516
|
+ |
%Recall{memories: recalled} =
|
|
517
|
+ |
Memories.recall(author, "the inference gateway returned 402")
|
|
518
|
+ |
|
|
519
|
+ |
assert recalled == []
|
|
520
|
+ |
end
|
|
521
|
+ |
|
|
522
|
+ |
test "and it does not crowd out the buckets recall does read" do
|
|
523
|
+ |
author = account("recall-boundary-mixed")
|
|
524
|
+ |
|
|
525
|
+ |
{:ok, _system} = Memories.create(author, candidate())
|
|
526
|
+ |
{:ok, asked} = Memories.create(author, %{"body" => "I use pnpm, not npm."})
|
|
527
|
+ |
|
|
528
|
+ |
%Recall{memories: recalled, dropped: dropped} =
|
|
529
|
+ |
Memories.recall(author, "install the deps")
|
|
530
|
+ |
|
|
531
|
+ |
assert Enum.map(recalled, & &1.id) == [asked.id]
|
|
532
|
+ |
assert dropped == 0
|
|
533
|
+ |
end
|
|
534
|
+ |
|
|
535
|
+ |
test "the author still reads their own system memories through the store" do
|
|
536
|
+ |
author = account("recall-boundary-list")
|
|
537
|
+ |
|
|
538
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
539
|
+ |
|
|
540
|
+ |
assert Enum.map(Memories.list(author, bucket: "system"), & &1.id) == [memory.id]
|
|
541
|
+ |
assert {:ok, %Memory{id: id}} = Memories.fetch(author, memory.id)
|
|
542
|
+ |
assert id == memory.id
|
|
543
|
+ |
end
|
|
544
|
+ |
|
|
545
|
+ |
test "and another account's system memory is absent rather than forbidden" do
|
|
546
|
+ |
author = account("recall-boundary-fetch")
|
|
547
|
+ |
reader = account("recall-boundary-fetch-other")
|
|
548
|
+ |
|
|
549
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
550
|
+ |
|
|
551
|
+ |
assert {:error, :not_found} = Memories.fetch(reader, memory.id)
|
|
552
|
+ |
assert Memories.list(reader, bucket: "system") == []
|
|
553
|
+ |
end
|
|
554
|
+ |
end
|
|
555
|
+ |
end
|