|
1
|
+ |
defmodule OpenAgents.Memories.ChallengeTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
Disagreement about a system memory: who may record it, what it does to the
|
|
4
|
+ |
derived status, and what it still may not reach.
|
|
5
|
+ |
|
|
6
|
+ |
Five properties carry the weight here.
|
|
7
|
+ |
|
|
8
|
+ |
**A challenge is a record, not an edit.** A reader who finds an admitted
|
|
9
|
+ |
claim wrong writes a row stating the ground. Nothing updates the claim, and a
|
|
10
|
+ |
reversal is a further record — so these tests read the whole history back and
|
|
11
|
+ |
check that every step of an argument survives it.
|
|
12
|
+ |
|
|
13
|
+ |
**Evidence is the distinction recall turns on.** An evidenced challenge
|
|
14
|
+ |
suspends its target; an unevidenced one is recorded and changes nothing. An
|
|
15
|
+ |
empty evidence list is neither: the table refuses it, so these tests insert
|
|
16
|
+ |
around the changeset to prove the refusal is the store's rather than the
|
|
17
|
+ |
write path's.
|
|
18
|
+ |
|
|
19
|
+ |
**Only a steward resolves.** A refutation from an ordinary account is
|
|
20
|
+ |
refused, and the refusal changes nothing and reveals nothing.
|
|
21
|
+ |
|
|
22
|
+ |
**The derivation is order-independent.** The same set of records derives the
|
|
23
|
+ |
same statuses however they arrive, including a refutation dated earlier than
|
|
24
|
+ |
the challenge it resolves. Every permutation is checked, not asserted.
|
|
25
|
+ |
|
|
26
|
+ |
**And the bucket still surfaces to nobody.** MEMORY-001 and MEMORY-010 hold
|
|
27
|
+ |
unchanged: a suspended memory, a challenged one, and an admitted one all
|
|
28
|
+ |
reach the same number of turns, which is none.
|
|
29
|
+ |
"""
|
|
30
|
+ |
use OpenAgents.DataCase, async: true
|
|
31
|
+ |
|
|
32
|
+ |
alias OpenAgents.Memories
|
|
33
|
+ |
alias OpenAgents.Memories.{Admission, Admissions, Memory, Recall}
|
|
34
|
+ |
|
|
35
|
+ |
# The owner account is an operator by definition (`@owner_github_id`), so a
|
|
36
|
+ |
# steward needs no configuration change and these tests stay async.
|
|
37
|
+ |
@owner_github_id 14_167_547
|
|
38
|
+ |
|
|
39
|
+ |
@ground "The receipt this cites was superseded before the claim was written."
|
|
40
|
+ |
|
|
41
|
+ |
defp account(key) do
|
|
42
|
+ |
digest = :crypto.hash(:sha256, key)
|
|
43
|
+ |
github_id = digest |> binary_part(0, 7) |> :binary.decode_unsigned()
|
|
44
|
+ |
|
|
45
|
+ |
upsert(github_id, "chl-" <> (digest |> Base.encode16(case: :lower) |> binary_part(0, 12)))
|
|
46
|
+ |
end
|
|
47
|
+ |
|
|
48
|
+ |
defp steward, do: upsert(@owner_github_id, "AtlantisPleb")
|
|
49
|
+ |
|
|
50
|
+ |
defp upsert(github_id, login) do
|
|
51
|
+ |
{:ok, user} =
|
|
52
|
+ |
OpenAgents.Accounts.upsert_github_user(%{
|
|
53
|
+ |
github_id: github_id,
|
|
54
|
+ |
github_login: login,
|
|
55
|
+ |
github_avatar_url: "https://avatars.githubusercontent.com/u/#{github_id}?v=4"
|
|
56
|
+ |
})
|
|
57
|
+ |
|
|
58
|
+ |
user
|
|
59
|
+ |
end
|
|
60
|
+ |
|
|
61
|
+ |
defp evidence(ref \\ "receipt:4f1c") do
|
|
62
|
+ |
[%{"kind" => "receipt", "ref" => ref, "digest" => "sha256:9ab3"}]
|
|
63
|
+ |
end
|
|
64
|
+ |
|
|
65
|
+ |
defp candidate(overrides) do
|
|
66
|
+ |
Map.merge(
|
|
67
|
+ |
%{
|
|
68
|
+ |
"bucket" => "system",
|
|
69
|
+ |
"slug" => "sys:gateway-402-retired-model",
|
|
70
|
+ |
"body" => "A 402 from the gateway means the default model was retired upstream.",
|
|
71
|
+ |
"tier" => "ledger",
|
|
72
|
+ |
"as_of" => ~D[2026-08-25],
|
|
73
|
+ |
"admission" => "candidate",
|
|
74
|
+ |
"evidence_refs" => evidence()
|
|
75
|
+ |
},
|
|
76
|
+ |
overrides
|
|
77
|
+ |
)
|
|
78
|
+ |
end
|
|
79
|
+ |
|
|
80
|
+ |
# An admitted system memory, which is the only thing a challenge can suspend.
|
|
81
|
+ |
defp admitted(author, slug) do
|
|
82
|
+ |
{:ok, memory} = Memories.create(author, candidate(%{"slug" => slug}))
|
|
83
|
+ |
|
|
84
|
+ |
{:ok, _verdict} =
|
|
85
|
+ |
Admissions.record(steward(), memory.id, %{
|
|
86
|
+ |
"verdict" => "admitted",
|
|
87
|
+ |
"ground" => "The receipt shows the 402 and the retirement together."
|
|
88
|
+ |
})
|
|
89
|
+ |
|
|
90
|
+ |
memory
|
|
91
|
+ |
end
|
|
92
|
+ |
|
|
93
|
+ |
# A record assembled as a struct rather than through a changeset. This is the
|
|
94
|
+ |
# second write path the constraints exist for, and the one that proves the
|
|
95
|
+ |
# refusal belongs to the table.
|
|
96
|
+ |
defp around_the_changeset(fields) do
|
|
97
|
+ |
Repo.insert(struct(Admission, Map.merge(%{memory_bucket: "system"}, fields)))
|
|
98
|
+ |
end
|
|
99
|
+ |
|
|
100
|
+ |
describe "writing a challenge" do
|
|
101
|
+ |
test "any account records one against an admitted claim" do
|
|
102
|
+ |
author = account("write-author")
|
|
103
|
+ |
reader = account("write-reader")
|
|
104
|
+ |
|
|
105
|
+ |
memory = admitted(author, "sys:write-any-account")
|
|
106
|
+ |
|
|
107
|
+ |
assert {:ok, challenge} =
|
|
108
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
109
|
+ |
"ground" => @ground,
|
|
110
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
111
|
+ |
})
|
|
112
|
+ |
|
|
113
|
+ |
assert challenge.role == "challenge"
|
|
114
|
+ |
assert challenge.slug == "chl:" <> memory.id
|
|
115
|
+ |
assert challenge.author_id == reader.id
|
|
116
|
+ |
assert challenge.memory_id == memory.id
|
|
117
|
+ |
assert challenge.verdict == nil
|
|
118
|
+ |
assert challenge.ground == @ground
|
|
119
|
+ |
assert challenge.inserted_at != nil
|
|
120
|
+ |
assert Admission.evidenced_challenge?(challenge)
|
|
121
|
+ |
end
|
|
122
|
+ |
|
|
123
|
+ |
test "the claim's own author may challenge it too" do
|
|
124
|
+ |
author = account("write-self")
|
|
125
|
+ |
memory = admitted(author, "sys:write-self")
|
|
126
|
+ |
|
|
127
|
+ |
assert {:ok, challenge} =
|
|
128
|
+ |
Admissions.challenge(author, memory.id, %{"ground" => @ground})
|
|
129
|
+ |
|
|
130
|
+ |
assert challenge.author_id == author.id
|
|
131
|
+ |
refute Admission.evidenced_challenge?(challenge)
|
|
132
|
+ |
end
|
|
133
|
+ |
|
|
134
|
+ |
test "a challenge needs a ground" do
|
|
135
|
+ |
author = account("write-ground")
|
|
136
|
+ |
memory = admitted(author, "sys:write-ground")
|
|
137
|
+ |
|
|
138
|
+ |
assert {:error, changeset} = Admissions.challenge(author, memory.id, %{})
|
|
139
|
+ |
assert %{ground: _refused} = errors_on(changeset)
|
|
140
|
+ |
end
|
|
141
|
+ |
|
|
142
|
+ |
test "an empty evidence list is refused rather than read as unevidenced" do
|
|
143
|
+ |
author = account("write-empty-evidence")
|
|
144
|
+ |
memory = admitted(author, "sys:write-empty-evidence")
|
|
145
|
+ |
|
|
146
|
+ |
assert {:error, changeset} =
|
|
147
|
+ |
Admissions.challenge(author, memory.id, %{
|
|
148
|
+ |
"ground" => @ground,
|
|
149
|
+ |
"evidence_refs" => []
|
|
150
|
+ |
})
|
|
151
|
+ |
|
|
152
|
+ |
assert %{evidence_refs: _refused} = errors_on(changeset)
|
|
153
|
+ |
end
|
|
154
|
+ |
|
|
155
|
+ |
test "an evidence ref with no digest is refused" do
|
|
156
|
+ |
author = account("write-bad-evidence")
|
|
157
|
+ |
memory = admitted(author, "sys:write-bad-evidence")
|
|
158
|
+ |
|
|
159
|
+ |
assert {:error, changeset} =
|
|
160
|
+ |
Admissions.challenge(author, memory.id, %{
|
|
161
|
+ |
"ground" => @ground,
|
|
162
|
+ |
"evidence_refs" => [%{"kind" => "url", "ref" => "https://openagents.com/"}]
|
|
163
|
+ |
})
|
|
164
|
+ |
|
|
165
|
+ |
assert %{evidence_refs: _refused} = errors_on(changeset)
|
|
166
|
+ |
end
|
|
167
|
+ |
|
|
168
|
+ |
# The composite foreign key, not a read. A memory outside the system bucket
|
|
169
|
+ |
# is not challengeable, and saying so costs no query across an account.
|
|
170
|
+ |
test "a challenge naming a memory outside the system bucket is refused" do
|
|
171
|
+ |
author = account("write-wrong-bucket")
|
|
172
|
+ |
|
|
173
|
+ |
{:ok, plain} = Memories.create(author, %{"body" => "I use pnpm."})
|
|
174
|
+ |
|
|
175
|
+ |
assert {:error, :not_found} =
|
|
176
|
+ |
Admissions.challenge(author, plain.id, %{"ground" => @ground})
|
|
177
|
+ |
|
|
178
|
+ |
assert {:error, :not_found} =
|
|
179
|
+ |
Admissions.challenge(author, Ecto.UUID.generate(), %{"ground" => @ground})
|
|
180
|
+ |
|
|
181
|
+ |
assert {:error, :not_found} =
|
|
182
|
+ |
Admissions.challenge(author, "not-a-uuid", %{"ground" => @ground})
|
|
183
|
+ |
|
|
184
|
+ |
assert Repo.aggregate(Admission, :count) == 0
|
|
185
|
+ |
end
|
|
186
|
+ |
end
|
|
187
|
+ |
|
|
188
|
+ |
describe "writing a refutation" do
|
|
189
|
+ |
test "a steward resolves one challenge" do
|
|
190
|
+ |
author = account("refute-author")
|
|
191
|
+ |
reader = account("refute-reader")
|
|
192
|
+ |
resolving = steward()
|
|
193
|
+ |
|
|
194
|
+ |
memory = admitted(author, "sys:refute-steward")
|
|
195
|
+ |
|
|
196
|
+ |
{:ok, challenge} =
|
|
197
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
198
|
+ |
"ground" => @ground,
|
|
199
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
200
|
+ |
})
|
|
201
|
+ |
|
|
202
|
+ |
assert {:ok, refutation} =
|
|
203
|
+ |
Admissions.refute(resolving, challenge.id, %{
|
|
204
|
+ |
"ground" => "The receipt it cites is current; the supersession named another."
|
|
205
|
+ |
})
|
|
206
|
+ |
|
|
207
|
+ |
assert refutation.role == "refutation"
|
|
208
|
+ |
assert refutation.slug == "ref:" <> challenge.id
|
|
209
|
+ |
assert refutation.challenge_id == challenge.id
|
|
210
|
+ |
assert refutation.memory_id == memory.id
|
|
211
|
+ |
assert refutation.author_id == resolving.id
|
|
212
|
+ |
assert refutation.verdict == nil
|
|
213
|
+ |
end
|
|
214
|
+ |
|
|
215
|
+ |
test "an ordinary account is refused, and the refusal changes nothing" do
|
|
216
|
+ |
author = account("refute-non-steward")
|
|
217
|
+ |
reader = account("refute-non-steward-reader")
|
|
218
|
+ |
|
|
219
|
+ |
memory = admitted(author, "sys:refute-non-steward")
|
|
220
|
+ |
|
|
221
|
+ |
{:ok, challenge} =
|
|
222
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
223
|
+ |
"ground" => @ground,
|
|
224
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
225
|
+ |
})
|
|
226
|
+ |
|
|
227
|
+ |
for account <- [author, reader] do
|
|
228
|
+ |
assert {:error, :steward_required} =
|
|
229
|
+ |
Admissions.refute(account, challenge.id, %{"ground" => "I disagree."})
|
|
230
|
+ |
end
|
|
231
|
+ |
|
|
232
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
233
|
+ |
assert Enum.count(Admissions.list(memory), &(&1.role == "refutation")) == 0
|
|
234
|
+ |
end
|
|
235
|
+ |
|
|
236
|
+ |
# The role check runs before the challenge is read, so a caller with no
|
|
237
|
+ |
# standing cannot tell a real challenge id from an invented one.
|
|
238
|
+ |
test "an account without the role learns nothing about the challenge it named" do
|
|
239
|
+ |
stranger = account("refute-stranger")
|
|
240
|
+ |
|
|
241
|
+ |
assert {:error, :steward_required} =
|
|
242
|
+ |
Admissions.refute(stranger, Ecto.UUID.generate(), %{"ground" => "No."})
|
|
243
|
+ |
|
|
244
|
+ |
assert {:error, :steward_required} =
|
|
245
|
+ |
Admissions.refute(stranger, "not-a-uuid", %{"ground" => "No."})
|
|
246
|
+ |
end
|
|
247
|
+ |
|
|
248
|
+ |
test "a refutation of something that is not a challenge is refused" do
|
|
249
|
+ |
author = account("refute-not-a-challenge")
|
|
250
|
+ |
resolving = steward()
|
|
251
|
+ |
|
|
252
|
+ |
memory = admitted(author, "sys:refute-not-a-challenge")
|
|
253
|
+ |
[verdict] = Admissions.list(memory)
|
|
254
|
+ |
|
|
255
|
+ |
assert {:error, :not_found} =
|
|
256
|
+ |
Admissions.refute(resolving, verdict.id, %{"ground" => "Not a challenge."})
|
|
257
|
+ |
|
|
258
|
+ |
assert {:error, :not_found} =
|
|
259
|
+ |
Admissions.refute(resolving, Ecto.UUID.generate(), %{"ground" => "Nothing."})
|
|
260
|
+ |
|
|
261
|
+ |
assert {:error, :not_found} =
|
|
262
|
+ |
Admissions.refute(resolving, "not-a-uuid", %{"ground" => "Nothing."})
|
|
263
|
+ |
end
|
|
264
|
+ |
|
|
265
|
+ |
test "a refutation needs a ground" do
|
|
266
|
+ |
author = account("refute-ground")
|
|
267
|
+ |
resolving = steward()
|
|
268
|
+ |
|
|
269
|
+ |
memory = admitted(author, "sys:refute-ground")
|
|
270
|
+ |
|
|
271
|
+ |
{:ok, challenge} =
|
|
272
|
+ |
Admissions.challenge(author, memory.id, %{
|
|
273
|
+ |
"ground" => @ground,
|
|
274
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
275
|
+ |
})
|
|
276
|
+ |
|
|
277
|
+ |
assert {:error, changeset} = Admissions.refute(resolving, challenge.id, %{})
|
|
278
|
+ |
assert %{ground: _refused} = errors_on(changeset)
|
|
279
|
+ |
end
|
|
280
|
+ |
end
|
|
281
|
+ |
|
|
282
|
+ |
describe "derived status" do
|
|
283
|
+ |
test "an evidenced challenge suspends an admitted claim" do
|
|
284
|
+ |
author = account("status-suspends")
|
|
285
|
+ |
reader = account("status-suspends-reader")
|
|
286
|
+ |
|
|
287
|
+ |
memory = admitted(author, "sys:status-suspends")
|
|
288
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
289
|
+ |
|
|
290
|
+ |
{:ok, _challenge} =
|
|
291
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
292
|
+ |
"ground" => @ground,
|
|
293
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
294
|
+ |
})
|
|
295
|
+ |
|
|
296
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
297
|
+ |
end
|
|
298
|
+ |
|
|
299
|
+ |
test "an unevidenced challenge is recorded and changes nothing" do
|
|
300
|
+ |
author = account("status-unevidenced")
|
|
301
|
+ |
reader = account("status-unevidenced-reader")
|
|
302
|
+ |
|
|
303
|
+ |
memory = admitted(author, "sys:status-unevidenced")
|
|
304
|
+ |
|
|
305
|
+ |
{:ok, challenge} = Admissions.challenge(reader, memory.id, %{"ground" => @ground})
|
|
306
|
+ |
|
|
307
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
308
|
+ |
assert challenge.id in Enum.map(Admissions.list(memory), & &1.id)
|
|
309
|
+ |
end
|
|
310
|
+ |
|
|
311
|
+ |
test "a challenge against a candidate or a rejected claim changes nothing" do
|
|
312
|
+ |
author = account("status-not-admitted")
|
|
313
|
+ |
reader = account("status-not-admitted-reader")
|
|
314
|
+ |
|
|
315
|
+ |
{:ok, proposed} = Memories.create(author, candidate(%{"slug" => "sys:status-candidate"}))
|
|
316
|
+ |
|
|
317
|
+ |
{:ok, refused} = Memories.create(author, candidate(%{"slug" => "sys:status-rejected"}))
|
|
318
|
+ |
|
|
319
|
+ |
{:ok, _verdict} =
|
|
320
|
+ |
Admissions.record(steward(), refused.id, %{
|
|
321
|
+ |
"verdict" => "rejected",
|
|
322
|
+ |
"ground" => "The digest does not match the receipt it names."
|
|
323
|
+ |
})
|
|
324
|
+ |
|
|
325
|
+ |
for memory <- [proposed, refused] do
|
|
326
|
+ |
{:ok, _challenge} =
|
|
327
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
328
|
+ |
"ground" => @ground,
|
|
329
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
330
|
+ |
})
|
|
331
|
+ |
end
|
|
332
|
+ |
|
|
333
|
+ |
assert Admissions.status(proposed) == "candidate"
|
|
334
|
+ |
assert Admissions.status(refused) == "rejected"
|
|
335
|
+ |
end
|
|
336
|
+ |
|
|
337
|
+ |
test "a refutation restores the claim" do
|
|
338
|
+ |
author = account("status-refuted")
|
|
339
|
+ |
reader = account("status-refuted-reader")
|
|
340
|
+ |
resolving = steward()
|
|
341
|
+ |
|
|
342
|
+ |
memory = admitted(author, "sys:status-refuted")
|
|
343
|
+ |
|
|
344
|
+ |
{:ok, challenge} =
|
|
345
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
346
|
+ |
"ground" => @ground,
|
|
347
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
348
|
+ |
})
|
|
349
|
+ |
|
|
350
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
351
|
+ |
|
|
352
|
+ |
{:ok, _refutation} =
|
|
353
|
+ |
Admissions.refute(resolving, challenge.id, %{"ground" => "The receipt is current."})
|
|
354
|
+ |
|
|
355
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
356
|
+ |
end
|
|
357
|
+ |
|
|
358
|
+ |
test "a refutation resolves the one challenge it names and no other" do
|
|
359
|
+ |
author = account("status-two-challenges")
|
|
360
|
+ |
first = account("status-two-challenges-first")
|
|
361
|
+ |
second = account("status-two-challenges-second")
|
|
362
|
+ |
resolving = steward()
|
|
363
|
+ |
|
|
364
|
+ |
memory = admitted(author, "sys:status-two-challenges")
|
|
365
|
+ |
|
|
366
|
+ |
{:ok, one} =
|
|
367
|
+ |
Admissions.challenge(first, memory.id, %{
|
|
368
|
+ |
"ground" => @ground,
|
|
369
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
370
|
+ |
})
|
|
371
|
+ |
|
|
372
|
+ |
{:ok, two} =
|
|
373
|
+ |
Admissions.challenge(second, memory.id, %{
|
|
374
|
+ |
"ground" => "The claim reverses cause and effect.",
|
|
375
|
+ |
"evidence_refs" => evidence("receipt:0b22")
|
|
376
|
+ |
})
|
|
377
|
+ |
|
|
378
|
+ |
{:ok, _refutation} =
|
|
379
|
+ |
Admissions.refute(resolving, one.id, %{"ground" => "The first ground does not stand."})
|
|
380
|
+ |
|
|
381
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
382
|
+ |
|
|
383
|
+ |
{:ok, _refutation} =
|
|
384
|
+ |
Admissions.refute(resolving, two.id, %{"ground" => "Nor does the second."})
|
|
385
|
+ |
|
|
386
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
387
|
+ |
end
|
|
388
|
+ |
|
|
389
|
+ |
test "a second refutation of the same challenge changes nothing" do
|
|
390
|
+ |
author = account("status-double-refutation")
|
|
391
|
+ |
resolving = steward()
|
|
392
|
+ |
|
|
393
|
+ |
memory = admitted(author, "sys:status-double-refutation")
|
|
394
|
+ |
|
|
395
|
+ |
{:ok, challenge} =
|
|
396
|
+ |
Admissions.challenge(author, memory.id, %{
|
|
397
|
+ |
"ground" => @ground,
|
|
398
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
399
|
+ |
})
|
|
400
|
+ |
|
|
401
|
+ |
{:ok, _first} = Admissions.refute(resolving, challenge.id, %{"ground" => "It stands."})
|
|
402
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
403
|
+ |
|
|
404
|
+ |
{:ok, _second} = Admissions.refute(resolving, challenge.id, %{"ground" => "Still stands."})
|
|
405
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
406
|
+ |
end
|
|
407
|
+ |
|
|
408
|
+ |
# A reversal is a further record, never an edit of one that exists.
|
|
409
|
+ |
test "a new challenge after a refutation suspends the claim again" do
|
|
410
|
+ |
author = account("status-reversal")
|
|
411
|
+ |
reader = account("status-reversal-reader")
|
|
412
|
+ |
resolving = steward()
|
|
413
|
+ |
|
|
414
|
+ |
memory = admitted(author, "sys:status-reversal")
|
|
415
|
+ |
|
|
416
|
+ |
{:ok, first} =
|
|
417
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
418
|
+ |
"ground" => @ground,
|
|
419
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
420
|
+ |
})
|
|
421
|
+ |
|
|
422
|
+ |
{:ok, _refutation} =
|
|
423
|
+ |
Admissions.refute(resolving, first.id, %{"ground" => "The receipt is current."})
|
|
424
|
+ |
|
|
425
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
426
|
+ |
|
|
427
|
+ |
{:ok, _second} =
|
|
428
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
429
|
+ |
"ground" => "The receipt is current and still does not say this.",
|
|
430
|
+ |
"evidence_refs" => evidence("receipt:0c33")
|
|
431
|
+ |
})
|
|
432
|
+ |
|
|
433
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
434
|
+ |
|
|
435
|
+ |
# And the argument reads back whole: nothing was overwritten.
|
|
436
|
+ |
assert Enum.map(Admissions.list(memory), & &1.role) ==
|
|
437
|
+ |
["admission", "challenge", "refutation", "challenge"]
|
|
438
|
+ |
end
|
|
439
|
+ |
|
|
440
|
+ |
test "re-admitting a suspended claim does not resolve the challenge" do
|
|
441
|
+ |
author = account("status-readmit")
|
|
442
|
+ |
reader = account("status-readmit-reader")
|
|
443
|
+ |
admitting = steward()
|
|
444
|
+ |
|
|
445
|
+ |
memory = admitted(author, "sys:status-readmit")
|
|
446
|
+ |
|
|
447
|
+ |
{:ok, _challenge} =
|
|
448
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
449
|
+ |
"ground" => @ground,
|
|
450
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
451
|
+ |
})
|
|
452
|
+ |
|
|
453
|
+ |
{:ok, _verdict} =
|
|
454
|
+ |
Admissions.record(admitting, memory.id, %{
|
|
455
|
+ |
"verdict" => "admitted",
|
|
456
|
+ |
"ground" => "Re-read the receipt and it still holds."
|
|
457
|
+ |
})
|
|
458
|
+ |
|
|
459
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
460
|
+ |
end
|
|
461
|
+ |
|
|
462
|
+ |
test "a memory outside the system bucket has no status, and an unknown id none either" do
|
|
463
|
+ |
author = account("status-other-bucket")
|
|
464
|
+ |
|
|
465
|
+ |
{:ok, plain} = Memories.create(author, %{"body" => "I use pnpm."})
|
|
466
|
+ |
|
|
467
|
+ |
assert Admissions.status(plain) == nil
|
|
468
|
+ |
assert Admissions.status("not-a-uuid") == nil
|
|
469
|
+ |
assert Admissions.status(Ecto.UUID.generate()) == "candidate"
|
|
470
|
+ |
end
|
|
471
|
+ |
end
|
|
472
|
+ |
|
|
473
|
+ |
describe "supersession as the other resolution path" do
|
|
474
|
+ |
test "a steward's correction resolves every open challenge on the target" do
|
|
475
|
+ |
author = account("supersede-resolves")
|
|
476
|
+ |
reader = account("supersede-resolves-reader")
|
|
477
|
+ |
correcting = steward()
|
|
478
|
+ |
|
|
479
|
+ |
memory = admitted(author, "sys:supersede-resolves")
|
|
480
|
+ |
|
|
481
|
+ |
{:ok, challenge} =
|
|
482
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
483
|
+ |
"ground" => @ground,
|
|
484
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
485
|
+ |
})
|
|
486
|
+ |
|
|
487
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
488
|
+ |
|
|
489
|
+ |
assert {:ok, replacement} =
|
|
490
|
+ |
Admissions.supersede(
|
|
491
|
+ |
correcting,
|
|
492
|
+ |
memory.id,
|
|
493
|
+ |
candidate(%{
|
|
494
|
+ |
"slug" => "sys:supersede-resolves",
|
|
495
|
+ |
"body" => "The gateway 402s when the default model is retired."
|
|
496
|
+ |
})
|
|
497
|
+ |
)
|
|
498
|
+ |
|
|
499
|
+ |
assert Repo.get!(Memory, memory.id).superseded_by_id == replacement.id
|
|
500
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
501
|
+ |
|
|
502
|
+ |
# The resolution is a receipt, attributed and dated, not something a
|
|
503
|
+ |
# reader has to infer from the pointer.
|
|
504
|
+ |
assert [resolution] = Enum.filter(Admissions.list(memory), &(&1.role == "refutation"))
|
|
505
|
+ |
assert resolution.challenge_id == challenge.id
|
|
506
|
+ |
assert resolution.author_id == correcting.id
|
|
507
|
+ |
end
|
|
508
|
+ |
|
|
509
|
+ |
test "an author's correction resolves nothing" do
|
|
510
|
+ |
author = account("supersede-author-resolves")
|
|
511
|
+ |
reader = account("supersede-author-resolves-reader")
|
|
512
|
+ |
|
|
513
|
+ |
memory = admitted(author, "sys:supersede-author-resolves")
|
|
514
|
+ |
|
|
515
|
+ |
{:ok, _challenge} =
|
|
516
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
517
|
+ |
"ground" => @ground,
|
|
518
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
519
|
+ |
})
|
|
520
|
+ |
|
|
521
|
+ |
assert {:ok, _replacement} =
|
|
522
|
+ |
Admissions.supersede(
|
|
523
|
+ |
author,
|
|
524
|
+ |
memory.id,
|
|
525
|
+ |
candidate(%{"slug" => "sys:supersede-author-resolves", "body" => "Corrected."})
|
|
526
|
+ |
)
|
|
527
|
+ |
|
|
528
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
529
|
+ |
assert Enum.count(Admissions.list(memory), &(&1.role == "refutation")) == 0
|
|
530
|
+ |
end
|
|
531
|
+ |
|
|
532
|
+ |
test "a correction with nothing challenged writes no resolution" do
|
|
533
|
+ |
author = account("supersede-no-challenges")
|
|
534
|
+ |
correcting = steward()
|
|
535
|
+ |
|
|
536
|
+ |
memory = admitted(author, "sys:supersede-no-challenges")
|
|
537
|
+ |
|
|
538
|
+ |
assert {:ok, _replacement} =
|
|
539
|
+ |
Admissions.supersede(
|
|
540
|
+ |
correcting,
|
|
541
|
+ |
memory.id,
|
|
542
|
+ |
candidate(%{"slug" => "sys:supersede-no-challenges", "body" => "Corrected."})
|
|
543
|
+ |
)
|
|
544
|
+ |
|
|
545
|
+ |
assert Enum.count(Admissions.list(memory), &(&1.role == "refutation")) == 0
|
|
546
|
+ |
end
|
|
547
|
+ |
end
|
|
548
|
+ |
|
|
549
|
+ |
describe "the challenge-flood cap" do
|
|
550
|
+ |
test "the cap is a quarter of the admitted store, rounded up" do
|
|
551
|
+ |
assert Admissions.challenge_share() == 25
|
|
552
|
+ |
assert Admissions.challenge_cap(0) == 0
|
|
553
|
+ |
assert Admissions.challenge_cap(1) == 1
|
|
554
|
+ |
assert Admissions.challenge_cap(4) == 1
|
|
555
|
+ |
assert Admissions.challenge_cap(5) == 2
|
|
556
|
+ |
assert Admissions.challenge_cap(8) == 2
|
|
557
|
+ |
assert Admissions.challenge_cap(100) == 25
|
|
558
|
+ |
end
|
|
559
|
+ |
|
|
560
|
+ |
test "one account suspends at most its share, and the rest queue" do
|
|
561
|
+ |
author = account("cap-author")
|
|
562
|
+ |
prolific = account("cap-prolific")
|
|
563
|
+ |
|
|
564
|
+ |
memories =
|
|
565
|
+ |
for index <- 1..8, do: admitted(author, "sys:cap-#{index}")
|
|
566
|
+ |
|
|
567
|
+ |
# Written latest-first, so the challenges that take effect are chosen by
|
|
568
|
+ |
# the records' own dates rather than by the order they arrived in.
|
|
569
|
+ |
memories
|
|
570
|
+ |
|> Enum.take(4)
|
|
571
|
+ |
|> Enum.with_index()
|
|
572
|
+ |
|> Enum.reverse()
|
|
573
|
+ |
|> Enum.each(fn {memory, index} ->
|
|
574
|
+ |
challenge(prolific, memory, ~U[2026-08-25 12:00:00.000000Z] |> shift(index))
|
|
575
|
+ |
end)
|
|
576
|
+ |
|
|
577
|
+ |
statuses = Admissions.statuses()
|
|
578
|
+ |
suspended = for {id, "suspended"} <- statuses, do: id
|
|
579
|
+ |
|
|
580
|
+ |
assert Enum.sort(suspended) ==
|
|
581
|
+ |
memories |> Enum.take(2) |> Enum.map(& &1.id) |> Enum.sort()
|
|
582
|
+ |
|
|
583
|
+ |
# Equal inputs, equal outcomes.
|
|
584
|
+ |
assert Admissions.statuses() == statuses
|
|
585
|
+ |
end
|
|
586
|
+ |
|
|
587
|
+ |
test "the cap is per account, not across the store" do
|
|
588
|
+ |
author = account("cap-per-account-author")
|
|
589
|
+ |
one = account("cap-per-account-one")
|
|
590
|
+ |
two = account("cap-per-account-two")
|
|
591
|
+ |
|
|
592
|
+ |
memories = for index <- 1..8, do: admitted(author, "sys:cap-account-#{index}")
|
|
593
|
+ |
|
|
594
|
+ |
[first, second, third, fourth | _rest] = memories
|
|
595
|
+ |
|
|
596
|
+ |
challenge(one, first, ~U[2026-08-25 12:00:00.000000Z])
|
|
597
|
+ |
challenge(one, second, ~U[2026-08-25 12:00:01.000000Z])
|
|
598
|
+ |
challenge(one, third, ~U[2026-08-25 12:00:02.000000Z])
|
|
599
|
+ |
challenge(two, fourth, ~U[2026-08-25 12:00:03.000000Z])
|
|
600
|
+ |
|
|
601
|
+ |
suspended = for {id, "suspended"} <- Admissions.statuses(), into: MapSet.new(), do: id
|
|
602
|
+ |
|
|
603
|
+ |
assert MapSet.equal?(suspended, MapSet.new([first.id, second.id, fourth.id]))
|
|
604
|
+ |
end
|
|
605
|
+ |
|
|
606
|
+ |
test "several challenges against one memory spend one slot, not several" do
|
|
607
|
+ |
author = account("cap-same-memory-author")
|
|
608
|
+ |
prolific = account("cap-same-memory-prolific")
|
|
609
|
+ |
|
|
610
|
+ |
memories = for index <- 1..8, do: admitted(author, "sys:cap-same-#{index}")
|
|
611
|
+ |
[first, second | _rest] = memories
|
|
612
|
+ |
|
|
613
|
+ |
challenge(prolific, first, ~U[2026-08-25 12:00:00.000000Z], "receipt:0a11")
|
|
614
|
+ |
challenge(prolific, first, ~U[2026-08-25 12:00:01.000000Z], "receipt:0b22")
|
|
615
|
+ |
challenge(prolific, second, ~U[2026-08-25 12:00:02.000000Z], "receipt:0c33")
|
|
616
|
+ |
|
|
617
|
+ |
suspended = for {id, "suspended"} <- Admissions.statuses(), into: MapSet.new(), do: id
|
|
618
|
+ |
|
|
619
|
+ |
assert MapSet.equal?(suspended, MapSet.new([first.id, second.id]))
|
|
620
|
+ |
end
|
|
621
|
+ |
|
|
622
|
+ |
# A challenge that suspends nothing spends nothing. Otherwise the cheapest
|
|
623
|
+ |
# way past the cap would be to challenge rows nobody admitted.
|
|
624
|
+ |
test "a challenge against a claim that is not admitted spends no budget" do
|
|
625
|
+ |
author = account("cap-unadmitted-author")
|
|
626
|
+ |
prolific = account("cap-unadmitted-prolific")
|
|
627
|
+ |
|
|
628
|
+ |
memories = for index <- 1..8, do: admitted(author, "sys:cap-unadmitted-#{index}")
|
|
629
|
+ |
[first, second | _rest] = memories
|
|
630
|
+ |
|
|
631
|
+ |
{:ok, proposed} =
|
|
632
|
+ |
Memories.create(author, candidate(%{"slug" => "sys:cap-unadmitted-candidate"}))
|
|
633
|
+ |
|
|
634
|
+ |
challenge(prolific, proposed, ~U[2026-08-25 11:00:00.000000Z])
|
|
635
|
+ |
challenge(prolific, first, ~U[2026-08-25 12:00:00.000000Z])
|
|
636
|
+ |
challenge(prolific, second, ~U[2026-08-25 12:00:01.000000Z])
|
|
637
|
+ |
|
|
638
|
+ |
suspended = for {id, "suspended"} <- Admissions.statuses(), into: MapSet.new(), do: id
|
|
639
|
+ |
|
|
640
|
+ |
assert MapSet.equal?(suspended, MapSet.new([first.id, second.id]))
|
|
641
|
+ |
assert Admissions.status(proposed) == "candidate"
|
|
642
|
+ |
end
|
|
643
|
+ |
|
|
644
|
+ |
test "a queued challenge takes effect once an earlier one is refuted" do
|
|
645
|
+ |
author = account("cap-queue-author")
|
|
646
|
+ |
prolific = account("cap-queue-prolific")
|
|
647
|
+ |
resolving = steward()
|
|
648
|
+ |
|
|
649
|
+ |
memories = for index <- 1..4, do: admitted(author, "sys:cap-queue-#{index}")
|
|
650
|
+ |
[first, second | _rest] = memories
|
|
651
|
+ |
|
|
652
|
+ |
one = challenge(prolific, first, ~U[2026-08-25 12:00:00.000000Z])
|
|
653
|
+ |
_two = challenge(prolific, second, ~U[2026-08-25 12:00:01.000000Z])
|
|
654
|
+ |
|
|
655
|
+ |
# Four admitted claims, so the cap is one.
|
|
656
|
+ |
assert Admissions.status(first) == "suspended"
|
|
657
|
+ |
assert Admissions.status(second) == "admitted"
|
|
658
|
+ |
|
|
659
|
+ |
{:ok, _refutation} =
|
|
660
|
+ |
Admissions.refute(resolving, one.id, %{"ground" => "The first ground does not stand."})
|
|
661
|
+ |
|
|
662
|
+ |
assert Admissions.status(first) == "admitted"
|
|
663
|
+ |
assert Admissions.status(second) == "suspended"
|
|
664
|
+ |
end
|
|
665
|
+ |
end
|
|
666
|
+ |
|
|
667
|
+ |
# The point of the whole exercise. A backfill, an import, and a correction all
|
|
668
|
+ |
# break the convenient case where records arrive in the order they happened,
|
|
669
|
+ |
# so the derivation must not depend on it.
|
|
670
|
+ |
describe "order independence" do
|
|
671
|
+ |
test "every permutation of one record set derives the same statuses" do
|
|
672
|
+ |
author = account("order-author")
|
|
673
|
+ |
reader = account("order-reader")
|
|
674
|
+ |
resolving = steward()
|
|
675
|
+ |
|
|
676
|
+ |
{:ok, first} = Memories.create(author, candidate(%{"slug" => "sys:order-first"}))
|
|
677
|
+ |
{:ok, second} = Memories.create(author, candidate(%{"slug" => "sys:order-second"}))
|
|
678
|
+ |
|
|
679
|
+ |
# Timestamps deliberately at odds with any insertion order: the second
|
|
680
|
+ |
# verdict on `first` is dated before nothing in particular, and the
|
|
681
|
+ |
# refutation is dated *earlier* than the challenge it resolves, which is
|
|
682
|
+ |
# the out-of-order case a backfill actually produces.
|
|
683
|
+ |
against_first = challenge_record(reader, first, ~U[2026-08-25 11:00:00.000000Z], evidence())
|
|
684
|
+ |
|
|
685
|
+ |
records = [
|
|
686
|
+ |
verdict_record(resolving, first, "rejected", ~U[2026-08-25 10:00:00.000000Z]),
|
|
687
|
+ |
verdict_record(resolving, first, "admitted", ~U[2026-08-25 10:00:05.000000Z]),
|
|
688
|
+ |
verdict_record(resolving, second, "admitted", ~U[2026-08-25 10:00:02.000000Z]),
|
|
689
|
+ |
against_first,
|
|
690
|
+ |
challenge_record(
|
|
691
|
+ |
reader,
|
|
692
|
+ |
second,
|
|
693
|
+ |
~U[2026-08-25 11:00:01.000000Z],
|
|
694
|
+ |
evidence("receipt:0b22")
|
|
695
|
+ |
),
|
|
696
|
+ |
refutation_record(resolving, first, against_first, ~U[2026-08-25 09:00:00.000000Z])
|
|
697
|
+ |
]
|
|
698
|
+ |
|
|
699
|
+ |
expected = %{first.id => "admitted", second.id => "suspended"}
|
|
700
|
+ |
|
|
701
|
+ |
derived =
|
|
702
|
+ |
for permutation <- permutations(records) do
|
|
703
|
+ |
Repo.delete_all(Admission)
|
|
704
|
+ |
insert_records(permutation)
|
|
705
|
+ |
Admissions.statuses()
|
|
706
|
+ |
end
|
|
707
|
+ |
|
|
708
|
+ |
assert length(derived) == 720
|
|
709
|
+ |
assert Enum.uniq(derived) == [expected]
|
|
710
|
+ |
end
|
|
711
|
+ |
end
|
|
712
|
+ |
|
|
713
|
+ |
describe "the constraints hold around the changeset" do
|
|
714
|
+ |
setup do
|
|
715
|
+ |
author = account("constraint-author")
|
|
716
|
+ |
%{author: author, memory: admitted(author, "sys:constraints")}
|
|
717
|
+ |
end
|
|
718
|
+ |
|
|
719
|
+ |
test "a challenge with an empty evidence list is refused by the database", context do
|
|
720
|
+ |
assert_raise Ecto.ConstraintError, ~r/memory_admissions_shape/, fn ->
|
|
721
|
+ |
around_the_changeset(%{
|
|
722
|
+ |
memory_id: context.memory.id,
|
|
723
|
+ |
author_id: context.author.id,
|
|
724
|
+ |
role: "challenge",
|
|
725
|
+ |
slug: "chl:" <> context.memory.id,
|
|
726
|
+ |
ground: @ground,
|
|
727
|
+ |
evidence_refs: []
|
|
728
|
+ |
})
|
|
729
|
+ |
end
|
|
730
|
+ |
end
|
|
731
|
+ |
|
|
732
|
+ |
test "a challenge whose evidence names no digest is refused", context do
|
|
733
|
+ |
assert_raise Ecto.ConstraintError, ~r/memory_admissions_shape/, fn ->
|
|
734
|
+ |
around_the_changeset(%{
|
|
735
|
+ |
memory_id: context.memory.id,
|
|
736
|
+ |
author_id: context.author.id,
|
|
737
|
+ |
role: "challenge",
|
|
738
|
+ |
slug: "chl:" <> context.memory.id,
|
|
739
|
+ |
ground: @ground,
|
|
740
|
+ |
evidence_refs: [%{"kind" => "url", "ref" => "https://openagents.com/"}]
|
|
741
|
+ |
})
|
|
742
|
+ |
end
|
|
743
|
+ |
end
|
|
744
|
+ |
|
|
745
|
+ |
test "a challenge under the wrong slug is refused", context do
|
|
746
|
+ |
assert_raise Ecto.ConstraintError, ~r/memory_admissions_shape/, fn ->
|
|
747
|
+ |
around_the_changeset(%{
|
|
748
|
+ |
memory_id: context.memory.id,
|
|
749
|
+ |
author_id: context.author.id,
|
|
750
|
+ |
role: "challenge",
|
|
751
|
+ |
slug: "chl:not-this-memory",
|
|
752
|
+ |
ground: @ground
|
|
753
|
+ |
})
|
|
754
|
+ |
end
|
|
755
|
+ |
end
|
|
756
|
+ |
|
|
757
|
+ |
test "an admission carrying evidence is refused", context do
|
|
758
|
+ |
assert_raise Ecto.ConstraintError, ~r/memory_admissions_shape/, fn ->
|
|
759
|
+ |
around_the_changeset(%{
|
|
760
|
+ |
memory_id: context.memory.id,
|
|
761
|
+ |
author_id: context.author.id,
|
|
762
|
+ |
role: "admission",
|
|
763
|
+ |
verdict: "admitted",
|
|
764
|
+ |
slug: "adm:" <> context.memory.id,
|
|
765
|
+ |
ground: @ground,
|
|
766
|
+ |
evidence_refs: evidence()
|
|
767
|
+ |
})
|
|
768
|
+ |
end
|
|
769
|
+ |
end
|
|
770
|
+ |
|
|
771
|
+ |
# `challenge_role` is what lets the foreign key insist that `challenge_id`
|
|
772
|
+ |
# names a challenge. A null there would leave the foreign key unchecked, so
|
|
773
|
+ |
# the shape constraint is what refuses it.
|
|
774
|
+ |
test "a refutation with no challenge_role is refused", context do
|
|
775
|
+ |
challenge = challenge(context.author, context.memory, ~U[2026-08-25 12:00:00.000000Z])
|
|
776
|
+ |
|
|
777
|
+ |
assert_raise Ecto.ConstraintError, ~r/memory_admissions_shape/, fn ->
|
|
778
|
+ |
around_the_changeset(%{
|
|
779
|
+ |
memory_id: context.memory.id,
|
|
780
|
+ |
author_id: context.author.id,
|
|
781
|
+ |
role: "refutation",
|
|
782
|
+ |
challenge_id: challenge.id,
|
|
783
|
+ |
slug: "ref:" <> challenge.id,
|
|
784
|
+ |
ground: "Resolved."
|
|
785
|
+ |
})
|
|
786
|
+ |
end
|
|
787
|
+ |
end
|
|
788
|
+ |
|
|
789
|
+ |
test "a refutation naming an admission record is refused", context do
|
|
790
|
+ |
[verdict] = Enum.filter(Admissions.list(context.memory), &(&1.role == "admission"))
|
|
791
|
+ |
|
|
792
|
+ |
assert_raise Ecto.ConstraintError, ~r/memory_admissions_challenge_fkey/, fn ->
|
|
793
|
+ |
around_the_changeset(%{
|
|
794
|
+ |
memory_id: context.memory.id,
|
|
795
|
+ |
author_id: context.author.id,
|
|
796
|
+ |
role: "refutation",
|
|
797
|
+ |
challenge_id: verdict.id,
|
|
798
|
+ |
challenge_role: "challenge",
|
|
799
|
+ |
slug: "ref:" <> verdict.id,
|
|
800
|
+ |
ground: "Resolved."
|
|
801
|
+ |
})
|
|
802
|
+ |
end
|
|
803
|
+ |
end
|
|
804
|
+ |
|
|
805
|
+ |
test "a refutation naming a challenge against another memory is refused", context do
|
|
806
|
+ |
other = admitted(context.author, "sys:constraints-other")
|
|
807
|
+ |
challenge = challenge(context.author, other, ~U[2026-08-25 12:00:00.000000Z])
|
|
808
|
+ |
|
|
809
|
+ |
assert_raise Ecto.ConstraintError, ~r/memory_admissions_challenge_fkey/, fn ->
|
|
810
|
+ |
around_the_changeset(%{
|
|
811
|
+ |
memory_id: context.memory.id,
|
|
812
|
+ |
author_id: context.author.id,
|
|
813
|
+ |
role: "refutation",
|
|
814
|
+ |
challenge_id: challenge.id,
|
|
815
|
+ |
challenge_role: "challenge",
|
|
816
|
+ |
slug: "ref:" <> challenge.id,
|
|
817
|
+ |
ground: "Resolved."
|
|
818
|
+ |
})
|
|
819
|
+ |
end
|
|
820
|
+ |
end
|
|
821
|
+ |
end
|
|
822
|
+ |
|
|
823
|
+ |
# MEMORY-001 and MEMORY-010, unchanged. Challenge and refutation are about
|
|
824
|
+ |
# what a status derives to, not about what any session sees, and every one of
|
|
825
|
+ |
# these states reaches the same number of turns: none.
|
|
826
|
+ |
describe "the recall boundary" do
|
|
827
|
+ |
test "a suspended, a challenged, and an admitted memory all surface to nobody" do
|
|
828
|
+ |
author = account("recall-author")
|
|
829
|
+ |
reader = account("recall-reader")
|
|
830
|
+ |
|
|
831
|
+ |
suspended = admitted(author, "sys:recall-suspended")
|
|
832
|
+ |
plain = admitted(author, "sys:recall-admitted")
|
|
833
|
+ |
|
|
834
|
+ |
{:ok, _challenge} =
|
|
835
|
+ |
Admissions.challenge(reader, suspended.id, %{
|
|
836
|
+ |
"ground" => @ground,
|
|
837
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
838
|
+ |
})
|
|
839
|
+ |
|
|
840
|
+ |
assert Admissions.status(suspended) == "suspended"
|
|
841
|
+ |
assert Admissions.status(plain) == "admitted"
|
|
842
|
+ |
|
|
843
|
+ |
for account <- [author, reader] do
|
|
844
|
+ |
assert %Recall{memories: []} =
|
|
845
|
+ |
Memories.recall(account, "the inference gateway returned 402")
|
|
846
|
+ |
end
|
|
847
|
+ |
end
|
|
848
|
+ |
|
|
849
|
+ |
test "and a challenge writes nothing into the buckets recall does read" do
|
|
850
|
+ |
author = account("recall-buckets-author")
|
|
851
|
+ |
reader = account("recall-buckets-reader")
|
|
852
|
+ |
|
|
853
|
+ |
memory = admitted(author, "sys:recall-buckets")
|
|
854
|
+ |
|
|
855
|
+ |
{:ok, _challenge} =
|
|
856
|
+ |
Admissions.challenge(reader, memory.id, %{
|
|
857
|
+ |
"ground" => @ground,
|
|
858
|
+ |
"evidence_refs" => evidence("receipt:0a11")
|
|
859
|
+ |
})
|
|
860
|
+ |
|
|
861
|
+ |
{:ok, asked} = Memories.create(reader, %{"body" => "I use pnpm, not npm."})
|
|
862
|
+ |
|
|
863
|
+ |
%Recall{memories: recalled} = Memories.recall(reader, "install the deps")
|
|
864
|
+ |
|
|
865
|
+ |
assert Enum.map(recalled, & &1.id) == [asked.id]
|
|
866
|
+ |
assert Memories.list(reader, bucket: "system") == []
|
|
867
|
+ |
end
|
|
868
|
+ |
end
|
|
869
|
+ |
|
|
870
|
+ |
# ── fixtures ───────────────────────────────────────────────────────────────
|
|
871
|
+ |
|
|
872
|
+ |
defp shift(%DateTime{} = at, seconds), do: DateTime.add(at, seconds, :second)
|
|
873
|
+ |
|
|
874
|
+ |
# A challenge dated deliberately, which is what the cap ranks by.
|
|
875
|
+ |
defp challenge(user, memory, at, ref \\ "receipt:0a11") do
|
|
876
|
+ |
Repo.insert!(challenge_record(user, memory, at, evidence(ref)))
|
|
877
|
+ |
end
|
|
878
|
+ |
|
|
879
|
+ |
defp challenge_record(user, memory, at, refs) do
|
|
880
|
+ |
%Admission{
|
|
881
|
+ |
id: Ecto.UUID.generate(),
|
|
882
|
+ |
memory_id: memory.id,
|
|
883
|
+ |
memory_bucket: "system",
|
|
884
|
+ |
author_id: user.id,
|
|
885
|
+ |
role: "challenge",
|
|
886
|
+ |
slug: "chl:" <> memory.id,
|
|
887
|
+ |
ground: @ground,
|
|
888
|
+ |
evidence_refs: refs,
|
|
889
|
+ |
inserted_at: at
|
|
890
|
+ |
}
|
|
891
|
+ |
end
|
|
892
|
+ |
|
|
893
|
+ |
defp verdict_record(user, memory, verdict, at) do
|
|
894
|
+ |
%Admission{
|
|
895
|
+ |
id: Ecto.UUID.generate(),
|
|
896
|
+ |
memory_id: memory.id,
|
|
897
|
+ |
memory_bucket: "system",
|
|
898
|
+ |
author_id: user.id,
|
|
899
|
+ |
role: "admission",
|
|
900
|
+ |
verdict: verdict,
|
|
901
|
+ |
slug: "adm:" <> memory.id,
|
|
902
|
+ |
ground: "Read the receipt.",
|
|
903
|
+ |
inserted_at: at
|
|
904
|
+ |
}
|
|
905
|
+ |
end
|
|
906
|
+ |
|
|
907
|
+ |
defp refutation_record(user, memory, challenge, at) do
|
|
908
|
+ |
%Admission{
|
|
909
|
+ |
id: Ecto.UUID.generate(),
|
|
910
|
+ |
memory_id: memory.id,
|
|
911
|
+ |
memory_bucket: "system",
|
|
912
|
+ |
author_id: user.id,
|
|
913
|
+ |
role: "refutation",
|
|
914
|
+ |
challenge_id: challenge.id,
|
|
915
|
+ |
challenge_role: "challenge",
|
|
916
|
+ |
slug: "ref:" <> challenge.id,
|
|
917
|
+ |
ground: "The ground does not stand.",
|
|
918
|
+ |
inserted_at: at
|
|
919
|
+ |
}
|
|
920
|
+ |
end
|
|
921
|
+ |
|
|
922
|
+ |
# The foreign key forbids a refutation reaching the table before the
|
|
923
|
+ |
# challenge it names, so a permutation is applied within each group rather
|
|
924
|
+ |
# than across them. That is not a weakening of the property: what the
|
|
925
|
+ |
# derivation must not read is the records' arrival order, and the refutation
|
|
926
|
+ |
# here is *dated* two hours before its challenge, which is the disagreement
|
|
927
|
+ |
# between arrival and date that a backfill actually produces.
|
|
928
|
+ |
defp insert_records(records) do
|
|
929
|
+ |
{independent, dependent} = Enum.split_with(records, &(&1.role != "refutation"))
|
|
930
|
+ |
Enum.each(independent ++ dependent, &Repo.insert!/1)
|
|
931
|
+ |
end
|
|
932
|
+ |
|
|
933
|
+ |
defp permutations([]), do: [[]]
|
|
934
|
+ |
|
|
935
|
+ |
defp permutations(list) do
|
|
936
|
+ |
for element <- list, rest <- permutations(list -- [element]), do: [element | rest]
|
|
937
|
+ |
end
|
|
938
|
+ |
end
|