|
1
|
+ |
defmodule OpenAgents.Memories.SystemRecallTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
The one recall path that crosses an account boundary: what it takes to turn
|
|
4
|
+ |
it on, what it lets through when it is on, and what it changes when it is
|
|
5
|
+ |
off.
|
|
6
|
+ |
|
|
7
|
+ |
Four properties carry the weight.
|
|
8
|
+ |
|
|
9
|
+ |
**Off is off, and off is the default.** The flag is `false` in
|
|
10
|
+ |
`config/config.exs` and declared `false` in the production and staging
|
|
11
|
+ |
profiles, and with it off an account's own recall is not merely similar to
|
|
12
|
+ |
what it was before this bucket had a recall path — it is byte for byte the
|
|
13
|
+ |
same. That is proved by comparison here rather than asserted: the same
|
|
14
|
+ |
account's recall is captured with no system store behind it, an admitted
|
|
15
|
+ |
system store is then written, and the two results are compared whole.
|
|
16
|
+ |
|
|
17
|
+ |
**Eligibility is derived, never claimed.** A row surfaces only when the
|
|
18
|
+ |
admission records derive `admitted` for it. A candidate, a rejected row, a
|
|
19
|
+ |
suspended row, a superseded row, and a row whose own `admission` column says
|
|
20
|
+ |
`admitted` with no steward behind it all reach nobody.
|
|
21
|
+ |
|
|
22
|
+ |
**The caps bound volume, not truth.** One account that wrote most of the
|
|
23
|
+ |
admitted store still fills at most its quarter of a message's pool and at
|
|
24
|
+ |
most one line of a note.
|
|
25
|
+ |
|
|
26
|
+ |
**It is deterministic.** Equal inputs give equal notes, every time.
|
|
27
|
+ |
"""
|
|
28
|
+ |
use OpenAgents.DataCase, async: false
|
|
29
|
+ |
|
|
30
|
+ |
alias OpenAgents.Memories
|
|
31
|
+ |
alias OpenAgents.Memories.{Admissions, Memory, Note, Recall, SystemRecall}
|
|
32
|
+ |
|
|
33
|
+ |
# The owner account is an operator by definition, so a steward needs no
|
|
34
|
+ |
# configuration change.
|
|
35
|
+ |
@owner_github_id 14_167_547
|
|
36
|
+ |
|
|
37
|
+ |
setup do
|
|
38
|
+ |
original = Application.get_env(:openagents, :memory_recall)
|
|
39
|
+ |
on_exit(fn -> Application.put_env(:openagents, :memory_recall, original) end)
|
|
40
|
+ |
:ok
|
|
41
|
+ |
end
|
|
42
|
+ |
|
|
43
|
+ |
defp surfacing(enabled?) do
|
|
44
|
+ |
settings =
|
|
45
|
+ |
:openagents
|
|
46
|
+ |
|> Application.get_env(:memory_recall, [])
|
|
47
|
+ |
|> Keyword.put(:system_bucket_enabled, enabled?)
|
|
48
|
+ |
|
|
49
|
+ |
Application.put_env(:openagents, :memory_recall, settings)
|
|
50
|
+ |
end
|
|
51
|
+ |
|
|
52
|
+ |
defp account(key) do
|
|
53
|
+ |
digest = :crypto.hash(:sha256, key)
|
|
54
|
+ |
github_id = digest |> binary_part(0, 7) |> :binary.decode_unsigned()
|
|
55
|
+ |
|
|
56
|
+ |
upsert(github_id, "sysrec-" <> (digest |> Base.encode16(case: :lower) |> binary_part(0, 12)))
|
|
57
|
+ |
end
|
|
58
|
+ |
|
|
59
|
+ |
defp steward, do: upsert(@owner_github_id, "AtlantisPleb")
|
|
60
|
+ |
|
|
61
|
+ |
defp upsert(github_id, login) do
|
|
62
|
+ |
{:ok, user} =
|
|
63
|
+ |
OpenAgents.Accounts.upsert_github_user(%{
|
|
64
|
+ |
github_id: github_id,
|
|
65
|
+ |
github_login: login,
|
|
66
|
+ |
github_avatar_url: "https://avatars.githubusercontent.com/u/#{github_id}?v=4"
|
|
67
|
+ |
})
|
|
68
|
+ |
|
|
69
|
+ |
user
|
|
70
|
+ |
end
|
|
71
|
+ |
|
|
72
|
+ |
defp evidence do
|
|
73
|
+ |
[%{"kind" => "receipt", "ref" => "receipt:4f1c", "digest" => "sha256:9ab3"}]
|
|
74
|
+ |
end
|
|
75
|
+ |
|
|
76
|
+ |
defp candidate(overrides \\ %{}) do
|
|
77
|
+ |
Map.merge(
|
|
78
|
+ |
%{
|
|
79
|
+ |
"bucket" => "system",
|
|
80
|
+ |
"slug" => "sys:gateway-402-retired-model",
|
|
81
|
+ |
"body" =>
|
|
82
|
+ |
"A 402 from the inference gateway usually means the default model was " <>
|
|
83
|
+ |
"retired upstream. Check gateway status before bisecting local lanes.",
|
|
84
|
+ |
"entity" => "inference-gateway",
|
|
85
|
+ |
"tier" => "ledger",
|
|
86
|
+ |
"as_of" => ~D[2026-08-25],
|
|
87
|
+ |
"admission" => "candidate",
|
|
88
|
+ |
"evidence_refs" => evidence()
|
|
89
|
+ |
},
|
|
90
|
+ |
overrides
|
|
91
|
+ |
)
|
|
92
|
+ |
end
|
|
93
|
+ |
|
|
94
|
+ |
# A row assembled as a struct rather than through `changeset/2`, so the tier
|
|
95
|
+ |
# floor is proved against the table rather than against the validation.
|
|
96
|
+ |
defp around_the_changeset(user, overrides) do
|
|
97
|
+ |
fields =
|
|
98
|
+ |
Map.merge(
|
|
99
|
+ |
%{
|
|
100
|
+ |
user_id: user.id,
|
|
101
|
+ |
bucket: "system",
|
|
102
|
+ |
body: "Written around the write path.",
|
|
103
|
+ |
slug: "sys:around-the-write-path",
|
|
104
|
+ |
tier: "ledger",
|
|
105
|
+ |
as_of: ~D[2026-08-25],
|
|
106
|
+ |
admission: "candidate",
|
|
107
|
+ |
evidence_refs: evidence()
|
|
108
|
+ |
},
|
|
109
|
+ |
overrides
|
|
110
|
+ |
)
|
|
111
|
+ |
|
|
112
|
+ |
Repo.insert(struct(Memory, fields))
|
|
113
|
+ |
end
|
|
114
|
+ |
|
|
115
|
+ |
# Written, then judged. The verdict is a steward's record, which is the only
|
|
116
|
+ |
# thing `status/1` reads.
|
|
117
|
+ |
defp admitted(author, overrides \\ %{}) do
|
|
118
|
+ |
{:ok, memory} = Memories.create(author, candidate(overrides))
|
|
119
|
+ |
|
|
120
|
+ |
{:ok, _record} =
|
|
121
|
+ |
Admissions.record(steward(), memory.id, %{
|
|
122
|
+ |
"verdict" => "admitted",
|
|
123
|
+ |
"ground" => "The receipt supports the claim."
|
|
124
|
+ |
})
|
|
125
|
+ |
|
|
126
|
+ |
memory
|
|
127
|
+ |
end
|
|
128
|
+ |
|
|
129
|
+ |
@turn "the inference gateway returned 402 on the default model"
|
|
130
|
+ |
|
|
131
|
+ |
describe "the flag, off" do
|
|
132
|
+ |
test "is the default this repository ships" do
|
|
133
|
+ |
refute SystemRecall.enabled?()
|
|
134
|
+ |
end
|
|
135
|
+ |
|
|
136
|
+ |
test "surfaces an admitted system memory to nobody, including its author" do
|
|
137
|
+ |
author = account("off-author")
|
|
138
|
+ |
reader = account("off-reader")
|
|
139
|
+ |
|
|
140
|
+ |
memory = admitted(author)
|
|
141
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
142
|
+ |
|
|
143
|
+ |
assert %Recall{memories: []} = Memories.recall(reader, @turn)
|
|
144
|
+ |
assert %Recall{memories: []} = Memories.recall(author, @turn)
|
|
145
|
+ |
end
|
|
146
|
+ |
|
|
147
|
+ |
test "issues no query for the bucket at all" do
|
|
148
|
+ |
assert SystemRecall.pool(@turn) == []
|
|
149
|
+ |
end
|
|
150
|
+ |
|
|
151
|
+ |
test "leaves user and learned recall byte for byte what it was" do
|
|
152
|
+ |
reader = account("off-identical")
|
|
153
|
+ |
author = account("off-identical-author")
|
|
154
|
+ |
|
|
155
|
+ |
{:ok, _asked} = Memories.create(reader, %{"body" => "I use pnpm, not npm."})
|
|
156
|
+ |
|
|
157
|
+ |
{:ok, _learned} =
|
|
158
|
+ |
Memories.create(reader, %{
|
|
159
|
+ |
"body" => "The inference gateway returned 402 during the last deploy.",
|
|
160
|
+ |
"bucket" => "learned"
|
|
161
|
+ |
})
|
|
162
|
+ |
|
|
163
|
+ |
before = Memories.recall(reader, @turn)
|
|
164
|
+ |
before_note = Note.render(before)
|
|
165
|
+ |
|
|
166
|
+ |
# An admitted store, written by another account and by this one, is now
|
|
167
|
+ |
# behind the same query.
|
|
168
|
+ |
_theirs = admitted(author)
|
|
169
|
+ |
_mine = admitted(reader, %{"slug" => "sys:precommit-installs-the-push-guard"})
|
|
170
|
+ |
|
|
171
|
+ |
after_store = Memories.recall(reader, @turn)
|
|
172
|
+ |
|
|
173
|
+ |
assert after_store == before
|
|
174
|
+ |
assert Note.render(after_store) == before_note
|
|
175
|
+ |
refute before_note == nil
|
|
176
|
+ |
end
|
|
177
|
+ |
end
|
|
178
|
+ |
|
|
179
|
+ |
describe "the flag, on" do
|
|
180
|
+ |
setup do
|
|
181
|
+ |
surfacing(true)
|
|
182
|
+ |
:ok
|
|
183
|
+ |
end
|
|
184
|
+ |
|
|
185
|
+ |
test "an admitted ledger memory reaches an account that did not write it" do
|
|
186
|
+ |
author = account("on-author")
|
|
187
|
+ |
reader = account("on-reader")
|
|
188
|
+ |
|
|
189
|
+ |
memory = admitted(author)
|
|
190
|
+ |
|
|
191
|
+ |
%Recall{memories: recalled} = Memories.recall(reader, @turn)
|
|
192
|
+ |
|
|
193
|
+ |
assert Enum.map(recalled, & &1.id) == [memory.id]
|
|
194
|
+ |
end
|
|
195
|
+ |
|
|
196
|
+ |
test "and reaches its own author too" do
|
|
197
|
+ |
author = account("on-own-author")
|
|
198
|
+ |
|
|
199
|
+ |
memory = admitted(author)
|
|
200
|
+ |
|
|
201
|
+ |
%Recall{memories: recalled} = Memories.recall(author, @turn)
|
|
202
|
+ |
|
|
203
|
+ |
assert Enum.map(recalled, & &1.id) == [memory.id]
|
|
204
|
+ |
end
|
|
205
|
+ |
|
|
206
|
+ |
test "a candidate with no verdict behind it reaches nobody" do
|
|
207
|
+ |
author = account("on-candidate")
|
|
208
|
+ |
reader = account("on-candidate-reader")
|
|
209
|
+ |
|
|
210
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
211
|
+ |
assert Admissions.status(memory) == "candidate"
|
|
212
|
+ |
|
|
213
|
+ |
assert %Recall{memories: []} = Memories.recall(reader, @turn)
|
|
214
|
+ |
end
|
|
215
|
+ |
|
|
216
|
+ |
test "nor does a row that claims admission for itself" do
|
|
217
|
+ |
author = account("on-self-claimed")
|
|
218
|
+ |
reader = account("on-self-claimed-reader")
|
|
219
|
+ |
|
|
220
|
+ |
{:ok, memory} = Memories.create(author, candidate(%{"admission" => "admitted"}))
|
|
221
|
+ |
|
|
222
|
+ |
assert memory.admission == "admitted"
|
|
223
|
+ |
assert Admissions.status(memory) == "candidate"
|
|
224
|
+ |
assert %Recall{memories: []} = Memories.recall(reader, @turn)
|
|
225
|
+ |
end
|
|
226
|
+ |
|
|
227
|
+ |
test "a rejected memory reaches nobody" do
|
|
228
|
+ |
author = account("on-rejected")
|
|
229
|
+ |
reader = account("on-rejected-reader")
|
|
230
|
+ |
|
|
231
|
+ |
{:ok, memory} = Memories.create(author, candidate())
|
|
232
|
+ |
|
|
233
|
+ |
{:ok, _record} =
|
|
234
|
+ |
Admissions.record(steward(), memory.id, %{
|
|
235
|
+ |
"verdict" => "rejected",
|
|
236
|
+ |
"ground" => "The receipt does not say this."
|
|
237
|
+ |
})
|
|
238
|
+ |
|
|
239
|
+ |
assert Admissions.status(memory) == "rejected"
|
|
240
|
+ |
assert %Recall{memories: []} = Memories.recall(reader, @turn)
|
|
241
|
+ |
end
|
|
242
|
+ |
|
|
243
|
+ |
test "a suspended memory leaves recall until the challenge is resolved" do
|
|
244
|
+ |
author = account("on-suspended")
|
|
245
|
+ |
reader = account("on-suspended-reader")
|
|
246
|
+ |
challenger = account("on-suspended-challenger")
|
|
247
|
+ |
|
|
248
|
+ |
memory = admitted(author)
|
|
249
|
+ |
|
|
250
|
+ |
{:ok, challenge} =
|
|
251
|
+ |
Admissions.challenge(challenger, memory.id, %{
|
|
252
|
+ |
"ground" => "The gateway returns 402 for an expired credential too.",
|
|
253
|
+ |
"evidence_refs" => [
|
|
254
|
+ |
%{"kind" => "url", "ref" => "https://example.test/status", "digest" => "sha256:1c2d"}
|
|
255
|
+ |
]
|
|
256
|
+ |
})
|
|
257
|
+ |
|
|
258
|
+ |
assert Admissions.status(memory) == "suspended"
|
|
259
|
+ |
assert %Recall{memories: []} = Memories.recall(reader, @turn)
|
|
260
|
+ |
|
|
261
|
+ |
{:ok, _refutation} =
|
|
262
|
+ |
Admissions.refute(steward(), challenge.id, %{
|
|
263
|
+ |
"ground" => "The receipt distinguishes the two cases."
|
|
264
|
+ |
})
|
|
265
|
+ |
|
|
266
|
+ |
assert Admissions.status(memory) == "admitted"
|
|
267
|
+ |
%Recall{memories: restored} = Memories.recall(reader, @turn)
|
|
268
|
+ |
assert Enum.map(restored, & &1.id) == [memory.id]
|
|
269
|
+ |
end
|
|
270
|
+ |
|
|
271
|
+ |
test "a superseded memory reaches nobody; its replacement does" do
|
|
272
|
+ |
author = account("on-superseded")
|
|
273
|
+ |
reader = account("on-superseded-reader")
|
|
274
|
+ |
|
|
275
|
+ |
memory = admitted(author)
|
|
276
|
+ |
|
|
277
|
+ |
{:ok, replacement} =
|
|
278
|
+ |
Admissions.supersede(
|
|
279
|
+ |
author,
|
|
280
|
+ |
memory.id,
|
|
281
|
+ |
candidate(%{"body" => "A 402 means the credential expired. Check the key first."})
|
|
282
|
+ |
)
|
|
283
|
+ |
|
|
284
|
+ |
{:ok, _record} =
|
|
285
|
+ |
Admissions.record(steward(), replacement.id, %{
|
|
286
|
+ |
"verdict" => "admitted",
|
|
287
|
+ |
"ground" => "The corrected claim is supported."
|
|
288
|
+ |
})
|
|
289
|
+ |
|
|
290
|
+ |
%Recall{memories: recalled} = Memories.recall(reader, @turn)
|
|
291
|
+ |
|
|
292
|
+ |
assert Enum.map(recalled, & &1.id) == [replacement.id]
|
|
293
|
+ |
end
|
|
294
|
+ |
|
|
295
|
+ |
# The tier floor is enforced in three places and reachable through none of
|
|
296
|
+ |
# them. `dark` and `pulse` are refused by the changeset, refused by
|
|
297
|
+ |
# `memories_system_shape` when a caller writes around it, and named as a
|
|
298
|
+ |
# predicate in the eligibility read so the floor does not depend on the
|
|
299
|
+ |
# table alone.
|
|
300
|
+ |
test "a sub-ledger tier cannot be written, and the read names the floor anyway" do
|
|
301
|
+ |
author = account("on-dark-tier")
|
|
302
|
+ |
|
|
303
|
+ |
assert {:error, changeset} = Memories.create(author, candidate(%{"tier" => "dark"}))
|
|
304
|
+ |
assert %{tier: ["is invalid"]} = errors_on(changeset)
|
|
305
|
+ |
|
|
306
|
+ |
assert_raise Ecto.ConstraintError, ~r/memories_system_shape/, fn ->
|
|
307
|
+ |
around_the_changeset(author, %{tier: "pulse"})
|
|
308
|
+ |
end
|
|
309
|
+ |
|
|
310
|
+ |
assert SystemRecall.tiers() == ~w(ledger glass)
|
|
311
|
+ |
end
|
|
312
|
+ |
|
|
313
|
+ |
test "an empty store recalls nothing rather than failing the turn" do
|
|
314
|
+ |
reader = account("on-empty")
|
|
315
|
+ |
|
|
316
|
+ |
assert %Recall{memories: [], dropped: 0} = Memories.recall(reader, @turn)
|
|
317
|
+ |
end
|
|
318
|
+ |
end
|
|
319
|
+ |
|
|
320
|
+ |
describe "the note" do
|
|
321
|
+ |
setup do
|
|
322
|
+ |
surfacing(true)
|
|
323
|
+ |
:ok
|
|
324
|
+ |
end
|
|
325
|
+ |
|
|
326
|
+ |
test "names the bucket, the claim date, and the derived status" do
|
|
327
|
+ |
author = account("note-author")
|
|
328
|
+ |
reader = account("note-reader")
|
|
329
|
+ |
|
|
330
|
+ |
_memory =
|
|
331
|
+ |
admitted(author, %{"body" => "Check gateway status first.", "as_of" => ~D[2026-08-25]})
|
|
332
|
+ |
|
|
333
|
+ |
note = reader |> Memories.recall(@turn) |> Note.render()
|
|
334
|
+ |
|
|
335
|
+ |
assert note ==
|
|
336
|
+ |
"[From memory: (system, as of 2026-08-25, admitted)] Check gateway status first."
|
|
337
|
+ |
end
|
|
338
|
+ |
|
|
339
|
+ |
test "reads differently from the account's own memories in the same note" do
|
|
340
|
+ |
reader = account("note-mixed")
|
|
341
|
+ |
author = account("note-mixed-author")
|
|
342
|
+ |
|
|
343
|
+ |
{:ok, _asked} = Memories.create(reader, %{"body" => "I use pnpm, not npm."})
|
|
344
|
+ |
_memory = admitted(author, %{"body" => "Check gateway status first."})
|
|
345
|
+ |
|
|
346
|
+ |
note = reader |> Memories.recall(@turn) |> Note.render()
|
|
347
|
+ |
|
|
348
|
+ |
assert [own, network] = String.split(note, "\n")
|
|
349
|
+ |
assert own =~ ~r/^\[From memory: user, /
|
|
350
|
+ |
|
|
351
|
+ |
assert network ==
|
|
352
|
+ |
"[From memory: (system, as of 2026-08-25, admitted)] Check gateway status first."
|
|
353
|
+ |
end
|
|
354
|
+ |
|
|
355
|
+ |
test "prints the status the records derived rather than the column the author wrote" do
|
|
356
|
+ |
author = account("note-derived")
|
|
357
|
+ |
reader = account("note-derived-reader")
|
|
358
|
+ |
|
|
359
|
+ |
_memory =
|
|
360
|
+ |
admitted(author, %{"admission" => "rejected", "body" => "Check gateway status first."})
|
|
361
|
+ |
|
|
362
|
+ |
note = reader |> Memories.recall(@turn) |> Note.render()
|
|
363
|
+ |
|
|
364
|
+ |
assert note =~ "admitted)"
|
|
365
|
+ |
refute note =~ "rejected"
|
|
366
|
+ |
end
|
|
367
|
+ |
|
|
368
|
+ |
test "spends the account's character budget rather than one of its own" do
|
|
369
|
+ |
reader = account("note-budget")
|
|
370
|
+ |
author = account("note-budget-author")
|
|
371
|
+ |
|
|
372
|
+ |
{:ok, _asked} = Memories.create(reader, %{"body" => String.duplicate("a", 60)})
|
|
373
|
+ |
_memory = admitted(author, %{"body" => String.duplicate("b", 60)})
|
|
374
|
+ |
|
|
375
|
+ |
%Recall{memories: recalled} =
|
|
376
|
+ |
Memories.recall(reader, @turn, maximum_attached_characters: 80)
|
|
377
|
+ |
|
|
378
|
+ |
assert length(recalled) == 1
|
|
379
|
+ |
assert hd(recalled).bucket == "user"
|
|
380
|
+ |
end
|
|
381
|
+ |
end
|
|
382
|
+ |
|
|
383
|
+ |
describe "the caps" do
|
|
384
|
+ |
setup do
|
|
385
|
+ |
surfacing(true)
|
|
386
|
+ |
:ok
|
|
387
|
+ |
end
|
|
388
|
+ |
|
|
389
|
+ |
test "hold the pool to a quarter of its slots per account" do
|
|
390
|
+ |
prolific = account("cap-prolific")
|
|
391
|
+ |
other = account("cap-other")
|
|
392
|
+ |
|
|
393
|
+ |
for index <- 1..8 do
|
|
394
|
+ |
admitted(prolific, %{
|
|
395
|
+ |
"slug" => "sys:prolific-#{index}",
|
|
396
|
+ |
"body" => "Gateway claim number #{index} about the inference gateway."
|
|
397
|
+ |
})
|
|
398
|
+ |
end
|
|
399
|
+ |
|
|
400
|
+ |
only = admitted(other, %{"slug" => "sys:other-1", "body" => "One claim about the gateway."})
|
|
401
|
+ |
|
|
402
|
+ |
pool = SystemRecall.pool(@turn)
|
|
403
|
+ |
|
|
404
|
+ |
# Nine eligible rows, so the cap is three per account, rounded up.
|
|
405
|
+ |
assert SystemRecall.pool_cap(9) == 3
|
|
406
|
+ |
assert Enum.count(pool, &(&1.user_id == prolific.id)) == 3
|
|
407
|
+ |
assert only.id in Enum.map(pool, & &1.id)
|
|
408
|
+ |
end
|
|
409
|
+ |
|
|
410
|
+ |
test "hold a note to one memory per account and two in all" do
|
|
411
|
+ |
prolific = account("note-cap-prolific")
|
|
412
|
+ |
second = account("note-cap-second")
|
|
413
|
+ |
third = account("note-cap-third")
|
|
414
|
+ |
reader = account("note-cap-reader")
|
|
415
|
+ |
|
|
416
|
+ |
for index <- 1..4 do
|
|
417
|
+ |
admitted(prolific, %{
|
|
418
|
+ |
"slug" => "sys:note-prolific-#{index}",
|
|
419
|
+ |
"body" => "Gateway claim number #{index} about the inference gateway."
|
|
420
|
+ |
})
|
|
421
|
+ |
end
|
|
422
|
+ |
|
|
423
|
+ |
admitted(second, %{
|
|
424
|
+ |
"slug" => "sys:note-second",
|
|
425
|
+ |
"body" => "A second account's gateway claim."
|
|
426
|
+ |
})
|
|
427
|
+ |
|
|
428
|
+ |
admitted(third, %{"slug" => "sys:note-third", "body" => "A third account's gateway claim."})
|
|
429
|
+ |
|
|
430
|
+ |
%Recall{memories: recalled} = Memories.recall(reader, @turn)
|
|
431
|
+ |
|
|
432
|
+ |
assert length(recalled) == 2
|
|
433
|
+ |
|
|
434
|
+ |
authors = Enum.map(recalled, & &1.user_id)
|
|
435
|
+ |
assert authors == Enum.uniq(authors)
|
|
436
|
+ |
assert Enum.count(recalled, &(&1.user_id == prolific.id)) <= 1
|
|
437
|
+ |
end
|
|
438
|
+ |
|
|
439
|
+ |
test "leave a single-account store recallable rather than empty" do
|
|
440
|
+ |
author = account("cap-single")
|
|
441
|
+ |
reader = account("cap-single-reader")
|
|
442
|
+ |
|
|
443
|
+ |
for index <- 1..4 do
|
|
444
|
+ |
admitted(author, %{
|
|
445
|
+ |
"slug" => "sys:single-#{index}",
|
|
446
|
+ |
"body" => "Gateway claim number #{index} about the inference gateway."
|
|
447
|
+ |
})
|
|
448
|
+ |
end
|
|
449
|
+ |
|
|
450
|
+ |
%Recall{memories: recalled} = Memories.recall(reader, @turn)
|
|
451
|
+ |
|
|
452
|
+ |
assert length(recalled) == 1
|
|
453
|
+ |
end
|
|
454
|
+ |
|
|
455
|
+ |
test "do not report what they excluded" do
|
|
456
|
+ |
prolific = account("cap-silent")
|
|
457
|
+ |
reader = account("cap-silent-reader")
|
|
458
|
+ |
|
|
459
|
+ |
for index <- 1..4 do
|
|
460
|
+ |
admitted(prolific, %{
|
|
461
|
+ |
"slug" => "sys:silent-#{index}",
|
|
462
|
+ |
"body" => "Gateway claim number #{index} about the inference gateway."
|
|
463
|
+ |
})
|
|
464
|
+ |
end
|
|
465
|
+ |
|
|
466
|
+ |
%Recall{dropped: dropped} = Memories.recall(reader, @turn)
|
|
467
|
+ |
|
|
468
|
+ |
assert dropped == 0
|
|
469
|
+ |
end
|
|
470
|
+ |
|
|
471
|
+ |
test "round up so a pool of one is not emptied by its own cap" do
|
|
472
|
+ |
assert SystemRecall.pool_cap(0) == 0
|
|
473
|
+ |
assert SystemRecall.pool_cap(1) == 1
|
|
474
|
+ |
assert SystemRecall.pool_cap(4) == 1
|
|
475
|
+ |
assert SystemRecall.pool_cap(5) == 2
|
|
476
|
+ |
assert SystemRecall.pool_cap(8) == 2
|
|
477
|
+ |
end
|
|
478
|
+ |
end
|
|
479
|
+ |
|
|
480
|
+ |
describe "determinism" do
|
|
481
|
+ |
setup do
|
|
482
|
+ |
surfacing(true)
|
|
483
|
+ |
:ok
|
|
484
|
+ |
end
|
|
485
|
+ |
|
|
486
|
+ |
test "equal inputs give equal pools and equal notes" do
|
|
487
|
+ |
first = account("det-first")
|
|
488
|
+ |
second = account("det-second")
|
|
489
|
+ |
reader = account("det-reader")
|
|
490
|
+ |
|
|
491
|
+ |
for index <- 1..3 do
|
|
492
|
+ |
admitted(first, %{
|
|
493
|
+ |
"slug" => "sys:det-first-#{index}",
|
|
494
|
+ |
"body" => "First account's gateway claim number #{index}."
|
|
495
|
+ |
})
|
|
496
|
+ |
|
|
497
|
+ |
admitted(second, %{
|
|
498
|
+ |
"slug" => "sys:det-second-#{index}",
|
|
499
|
+ |
"body" => "Second account's gateway claim number #{index}."
|
|
500
|
+ |
})
|
|
501
|
+ |
end
|
|
502
|
+ |
|
|
503
|
+ |
pools = for _repeat <- 1..5, do: Enum.map(SystemRecall.pool(@turn), & &1.id)
|
|
504
|
+ |
notes = for _repeat <- 1..5, do: reader |> Memories.recall(@turn) |> Note.render()
|
|
505
|
+ |
|
|
506
|
+ |
assert Enum.uniq(pools) == [hd(pools)]
|
|
507
|
+ |
assert Enum.uniq(notes) == [hd(notes)]
|
|
508
|
+ |
end
|
|
509
|
+ |
end
|
|
510
|
+ |
|
|
511
|
+ |
# MEMORY-004 and MEMORY-001's amendment. The eligibility filter is what
|
|
512
|
+ |
# replaces the scope predicate for this bucket, so it has to be a predicate:
|
|
513
|
+ |
# written into the query, not applied to what the query returned.
|
|
514
|
+ |
describe "MEMORY-004" do
|
|
515
|
+ |
@source "lib/openagents/memories/system_recall.ex"
|
|
516
|
+ |
|
|
517
|
+ |
test "the eligibility filter is written into the query" do
|
|
518
|
+ |
query = @source |> queries() |> List.first()
|
|
519
|
+ |
|
|
520
|
+ |
assert query, "no query rooted at `Memory` found in #{@source}"
|
|
521
|
+ |
|
|
522
|
+ |
for predicate <- ~w(bucket superseded_by_id tier id)a do
|
|
523
|
+ |
assert names?(query, predicate),
|
|
524
|
+ |
"""
|
|
525
|
+ |
The eligibility read in #{@source} does not name `#{predicate}`.
|
|
526
|
+ |
MEMORY-001's amendment replaces the account predicate with this
|
|
527
|
+ |
filter, so every part of it belongs in the query.
|
|
528
|
+ |
|
|
529
|
+ |
#{Macro.to_string(query)}
|
|
530
|
+ |
"""
|
|
531
|
+ |
end
|
|
532
|
+ |
end
|
|
533
|
+ |
|
|
534
|
+ |
test "and it names no account" do
|
|
535
|
+ |
for query <- queries(@source) do
|
|
536
|
+ |
refute names?(query, :user_id),
|
|
537
|
+ |
"""
|
|
538
|
+ |
A query in #{@source} names `user_id`. This module holds the
|
|
539
|
+ |
reads that deliberately do not; an account-scoped read belongs
|
|
540
|
+ |
in `OpenAgents.Memories`.
|
|
541
|
+ |
"""
|
|
542
|
+ |
end
|
|
543
|
+ |
end
|
|
544
|
+ |
|
|
545
|
+ |
defp queries(path) do
|
|
546
|
+ |
path
|
|
547
|
+ |
|> File.read!()
|
|
548
|
+ |
|> Code.string_to_quoted!()
|
|
549
|
+ |
|> Macro.prewalker()
|
|
550
|
+ |
|> Enum.filter(fn
|
|
551
|
+ |
{:from, _meta, [{:in, _, [_binding, {:__aliases__, _, [:Memory]}]} | _]} -> true
|
|
552
|
+ |
_other -> false
|
|
553
|
+ |
end)
|
|
554
|
+ |
end
|
|
555
|
+ |
|
|
556
|
+ |
defp names?(query, field) do
|
|
557
|
+ |
query
|
|
558
|
+ |
|> Macro.prewalker()
|
|
559
|
+ |
|> Enum.any?(fn
|
|
560
|
+ |
{{:., _, [_, ^field]}, _, _} -> true
|
|
561
|
+ |
_other -> false
|
|
562
|
+ |
end)
|
|
563
|
+ |
end
|
|
564
|
+ |
end
|
|
565
|
+ |
|
|
566
|
+ |
# The bucket still belongs to the account that wrote it for every purpose but
|
|
567
|
+ |
# recall. Reading a network claim in a turn does not make it listable,
|
|
568
|
+ |
# fetchable, or correctable by the reader.
|
|
569
|
+ |
describe "the write boundary is unmoved" do
|
|
570
|
+ |
setup do
|
|
571
|
+ |
surfacing(true)
|
|
572
|
+ |
:ok
|
|
573
|
+ |
end
|
|
574
|
+ |
|
|
575
|
+ |
test "a reader who recalls a system memory still cannot read it through the store" do
|
|
576
|
+ |
author = account("boundary-author")
|
|
577
|
+ |
reader = account("boundary-reader")
|
|
578
|
+ |
|
|
579
|
+ |
memory = admitted(author)
|
|
580
|
+ |
|
|
581
|
+ |
%Recall{memories: recalled} = Memories.recall(reader, @turn)
|
|
582
|
+ |
assert Enum.map(recalled, & &1.id) == [memory.id]
|
|
583
|
+ |
|
|
584
|
+ |
assert {:error, :not_found} = Memories.fetch(reader, memory.id)
|
|
585
|
+ |
assert Memories.list(reader, bucket: "system") == []
|
|
586
|
+ |
assert {:error, :not_supersedable} = Admissions.supersede(reader, memory.id, candidate())
|
|
587
|
+ |
end
|
|
588
|
+ |
|
|
589
|
+ |
test "and the account's own buckets stay account-scoped with the flag on" do
|
|
590
|
+ |
reader = account("boundary-scoped")
|
|
591
|
+ |
other = account("boundary-scoped-other")
|
|
592
|
+ |
|
|
593
|
+ |
{:ok, _theirs} = Memories.create(other, %{"body" => "Deploy with yarn."})
|
|
594
|
+ |
|
|
595
|
+ |
assert %Recall{memories: []} = Memories.recall(reader, "deploy with yarn")
|
|
596
|
+ |
end
|
|
597
|
+ |
end
|
|
598
|
+ |
end
|