|
1
|
+ |
defmodule OpenAgents.Issues.TraceDisclosureTest do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
`#10`: the deliberate ATIF visibility policy for an issue.
|
|
4
|
+ |
|
|
5
|
+ |
The decision under test is a refusal as much as a disclosure: **an issue
|
|
6
|
+ |
publishes that a trajectory exists and never publishes one.** So the first
|
|
7
|
+ |
property here is that no rung returns a step, and the rest is the two gates
|
|
8
|
+ |
that decide whether even the existence is disclosed.
|
|
9
|
+ |
|
|
10
|
+ |
Every tier assertion runs on a **public** repository, where
|
|
11
|
+ |
`Repositories.readable_by/2` admits everybody and the two gates are the only
|
|
12
|
+ |
things between an anonymous reader and a digest. A private repository would
|
|
13
|
+ |
have proved the gates worked while the repository gate did the work — so the
|
|
14
|
+ |
last test does exactly that case separately, and asserts that repository
|
|
15
|
+ |
authority is still stronger than any consent.
|
|
16
|
+ |
"""
|
|
17
|
+ |
use OpenAgents.DataCase, async: true
|
|
18
|
+ |
|
|
19
|
+ |
import OpenAgents.AccountsFixtures
|
|
20
|
+ |
|
|
21
|
+ |
alias OpenAgents.Box.ConversationBox
|
|
22
|
+ |
alias OpenAgents.Conversations
|
|
23
|
+ |
alias OpenAgents.Forge.{Assignment, Assignments}
|
|
24
|
+ |
alias OpenAgents.Issues
|
|
25
|
+ |
alias OpenAgents.Issues.{Activity, TraceDisclosure}
|
|
26
|
+ |
alias OpenAgents.Repo
|
|
27
|
+ |
alias OpenAgents.Repositories
|
|
28
|
+ |
alias OpenAgents.Traces
|
|
29
|
+ |
alias OpenAgents.Transparency.WorkDisclosure
|
|
30
|
+ |
|
|
31
|
+ |
@document %{
|
|
32
|
+ |
"schema_version" => "ATIF-v1.7",
|
|
33
|
+ |
"session_id" => "s1",
|
|
34
|
+ |
"steps" => [
|
|
35
|
+ |
%{"step_id" => 1, "role" => "user", "message" => "the private prompt"},
|
|
36
|
+ |
%{"step_id" => 2, "role" => "agent", "message" => "the private answer"},
|
|
37
|
+ |
%{"step_id" => 3, "role" => "agent", "message" => "and a third"}
|
|
38
|
+ |
]
|
|
39
|
+ |
}
|
|
40
|
+ |
|
|
41
|
+ |
setup do
|
|
42
|
+ |
owner = repository_user_fixture("trace-owner")
|
|
43
|
+ |
member = repository_user_fixture("trace-member")
|
|
44
|
+ |
stranger = repository_user_fixture("trace-stranger")
|
|
45
|
+ |
|
|
46
|
+ |
repository = repository_with_member_fixture(owner, %{visibility: "public"}, "owner")
|
|
47
|
+ |
{:ok, _} = Repositories.add_member(repository, member, "maintainer")
|
|
48
|
+ |
|
|
49
|
+ |
{:ok, issue} = Issues.create_issue(repository, %{title: "Record the trajectory"})
|
|
50
|
+ |
attempt = admit(owner, repository, issue)
|
|
51
|
+ |
|
|
52
|
+ |
%{
|
|
53
|
+ |
owner: owner,
|
|
54
|
+ |
member: member,
|
|
55
|
+ |
stranger: stranger,
|
|
56
|
+ |
repository: repository,
|
|
57
|
+ |
issue: issue,
|
|
58
|
+ |
attempt: attempt
|
|
59
|
+ |
}
|
|
60
|
+ |
end
|
|
61
|
+ |
|
|
62
|
+ |
describe "no rung publishes the trajectory" do
|
|
63
|
+ |
test "the document is absent at every tier, for every reader", context do
|
|
64
|
+ |
trace = upload(context.owner, context.attempt, "glass")
|
|
65
|
+ |
|
|
66
|
+ |
for reader <- [nil, context.stranger, context.member, context.owner] do
|
|
67
|
+ |
viewer = WorkDisclosure.viewer(context.repository, reader)
|
|
68
|
+ |
projection = TraceDisclosure.project(trace, context.attempt, viewer)
|
|
69
|
+ |
|
|
70
|
+ |
refute is_nil(projection),
|
|
71
|
+ |
"expected a projection for #{inspect(reader && reader.github_login)}"
|
|
72
|
+ |
|
|
73
|
+ |
refute Map.has_key?(projection, :document)
|
|
74
|
+ |
refute Map.has_key?(projection, :steps)
|
|
75
|
+ |
|
|
76
|
+ |
# Nothing anywhere in the projection restates a step, however nested.
|
|
77
|
+ |
refute inspect(projection) =~ "the private prompt"
|
|
78
|
+ |
refute inspect(projection) =~ "the private answer"
|
|
79
|
+ |
end
|
|
80
|
+ |
end
|
|
81
|
+ |
|
|
82
|
+ |
test "the schedule itself refuses the document, not only this projection" do
|
|
83
|
+ |
refute :document in WorkDisclosure.fields_at(:trace, :glass)
|
|
84
|
+ |
assert WorkDisclosure.tier_for(:trace, :document) == nil
|
|
85
|
+ |
end
|
|
86
|
+ |
|
|
87
|
+ |
test "an operator gets the shape and not the steps", context do
|
|
88
|
+ |
operator = admin_user()
|
|
89
|
+ |
trace = upload(context.owner, context.attempt, "glass")
|
|
90
|
+ |
viewer = WorkDisclosure.viewer(context.repository, operator)
|
|
91
|
+ |
|
|
92
|
+ |
projection = TraceDisclosure.project(trace, context.attempt, viewer)
|
|
93
|
+ |
|
|
94
|
+ |
assert projection.step_count == 3
|
|
95
|
+ |
refute Map.has_key?(projection, :document)
|
|
96
|
+ |
end
|
|
97
|
+ |
end
|
|
98
|
+ |
|
|
99
|
+ |
describe "consent is a gate, and it defaults to withholding" do
|
|
100
|
+ |
test "a trace stored with no visibility is dark and invisible", context do
|
|
101
|
+ |
trace = upload(context.owner, context.attempt, nil)
|
|
102
|
+ |
|
|
103
|
+ |
assert trace.visibility == "dark"
|
|
104
|
+ |
|
|
105
|
+ |
for reader <- [nil, context.stranger, context.member, context.owner] do
|
|
106
|
+ |
viewer = WorkDisclosure.viewer(context.repository, reader)
|
|
107
|
+ |
assert TraceDisclosure.project(trace, context.attempt, viewer) == nil
|
|
108
|
+ |
end
|
|
109
|
+ |
end
|
|
110
|
+ |
|
|
111
|
+ |
test "a dark trace is absent rather than an empty shell", context do
|
|
112
|
+ |
trace = upload(context.owner, context.attempt, "dark")
|
|
113
|
+ |
viewer = WorkDisclosure.viewer(context.repository, context.owner)
|
|
114
|
+ |
|
|
115
|
+ |
assert TraceDisclosure.project(trace, context.attempt, viewer) == nil
|
|
116
|
+ |
assert TraceDisclosure.for_attempts([context.attempt], viewer) == []
|
|
117
|
+ |
end
|
|
118
|
+ |
|
|
119
|
+ |
test "consent at pulse discloses the shape and withholds the digest", context do
|
|
120
|
+ |
trace = upload(context.owner, context.attempt, "pulse")
|
|
121
|
+ |
viewer = WorkDisclosure.viewer(context.repository, context.member)
|
|
122
|
+ |
|
|
123
|
+ |
projection = TraceDisclosure.project(trace, context.attempt, viewer)
|
|
124
|
+ |
|
|
125
|
+ |
assert projection.tier == :pulse
|
|
126
|
+ |
assert projection.schema_version == "ATIF-v1.7"
|
|
127
|
+ |
assert projection.step_count == 3
|
|
128
|
+ |
assert projection.recorded_at
|
|
129
|
+ |
refute Map.has_key?(projection, :digest)
|
|
130
|
+ |
refute Map.has_key?(projection, :byte_size)
|
|
131
|
+ |
end
|
|
132
|
+ |
|
|
133
|
+ |
test "consent at ledger adds the digest, which is the only field that travels",
|
|
134
|
+ |
context do
|
|
135
|
+ |
trace = upload(context.owner, context.attempt, "ledger")
|
|
136
|
+ |
viewer = WorkDisclosure.viewer(context.repository, context.member)
|
|
137
|
+ |
|
|
138
|
+ |
projection = TraceDisclosure.project(trace, context.attempt, viewer)
|
|
139
|
+ |
|
|
140
|
+ |
assert projection.tier == :ledger
|
|
141
|
+ |
assert projection.digest == trace.digest
|
|
142
|
+ |
assert projection.byte_size == trace.byte_size
|
|
143
|
+ |
end
|
|
144
|
+ |
|
|
145
|
+ |
test "consent is a ceiling the viewer's own rung cannot raise", context do
|
|
146
|
+ |
trace = upload(context.owner, context.attempt, "pulse")
|
|
147
|
+ |
|
|
148
|
+ |
# An operator reaches `glass` on everything else about this attempt.
|
|
149
|
+ |
viewer = WorkDisclosure.viewer(context.repository, admin_user())
|
|
150
|
+ |
|
|
151
|
+ |
assert TraceDisclosure.effective_tier(trace, context.attempt, viewer) == :pulse
|
|
152
|
+ |
end
|
|
153
|
+ |
end
|
|
154
|
+ |
|
|
155
|
+ |
describe "repository access is the other gate, and it is the stronger one" do
|
|
156
|
+ |
test "a reader who cannot read the repository sees no trace, however wide the consent",
|
|
157
|
+ |
context do
|
|
158
|
+ |
trace = upload(context.owner, context.attempt, "glass")
|
|
159
|
+ |
go_private(context.repository)
|
|
160
|
+ |
|
|
161
|
+ |
# The activity read is where repository authority is applied, so the
|
|
162
|
+ |
# assertion belongs at that seam rather than at the projection.
|
|
163
|
+ |
activity = Activity.for_issue(context.issue, context.stranger)
|
|
164
|
+ |
|
|
165
|
+ |
assert activity.traces == []
|
|
166
|
+ |
assert trace.visibility == "glass"
|
|
167
|
+ |
end
|
|
168
|
+ |
|
|
169
|
+ |
test "a member of that private repository still sees the shape", context do
|
|
170
|
+ |
_trace = upload(context.owner, context.attempt, "ledger")
|
|
171
|
+ |
go_private(context.repository)
|
|
172
|
+ |
|
|
173
|
+ |
activity = Activity.for_issue(context.issue, context.member)
|
|
174
|
+ |
|
|
175
|
+ |
assert [projection] = activity.traces
|
|
176
|
+ |
assert projection.assignment_id == context.attempt.id
|
|
177
|
+ |
assert projection.step_count == 3
|
|
178
|
+ |
refute Map.has_key?(projection, :document)
|
|
179
|
+ |
end
|
|
180
|
+ |
|
|
181
|
+ |
test "an anonymous reader of a public repository gets pulse and no digest", context do
|
|
182
|
+ |
_trace = upload(context.owner, context.attempt, "ledger")
|
|
183
|
+ |
|
|
184
|
+ |
activity = Activity.for_issue(context.issue, nil)
|
|
185
|
+ |
|
|
186
|
+ |
assert [projection] = activity.traces
|
|
187
|
+ |
assert projection.tier == :pulse
|
|
188
|
+ |
refute Map.has_key?(projection, :digest)
|
|
189
|
+ |
end
|
|
190
|
+ |
end
|
|
191
|
+ |
|
|
192
|
+ |
describe "binding a trace to an attempt is checked, not believed" do
|
|
193
|
+ |
test "the requesting account may bind", context do
|
|
194
|
+ |
assert {:ok, trace, :created} =
|
|
195
|
+ |
Traces.store(context.owner, @document, assignment_id: context.attempt.id)
|
|
196
|
+ |
|
|
197
|
+ |
assert trace.assignment_id == context.attempt.id
|
|
198
|
+ |
end
|
|
199
|
+ |
|
|
200
|
+ |
test "another account may not, and is refused rather than silently unbound", context do
|
|
201
|
+ |
assert {:error, :trace_assignment_forbidden} =
|
|
202
|
+ |
Traces.store(context.stranger, @document, assignment_id: context.attempt.id)
|
|
203
|
+ |
end
|
|
204
|
+ |
|
|
205
|
+ |
test "an attempt that does not exist is refused", context do
|
|
206
|
+ |
assert {:error, :trace_assignment_forbidden} =
|
|
207
|
+ |
Traces.store(context.owner, @document, assignment_id: Ecto.UUID.generate())
|
|
208
|
+ |
end
|
|
209
|
+ |
|
|
210
|
+ |
test "a malformed identifier is refused rather than raising", context do
|
|
211
|
+ |
assert {:error, :trace_assignment_forbidden} =
|
|
212
|
+ |
Traces.store(context.owner, @document, assignment_id: "not-a-uuid")
|
|
213
|
+ |
end
|
|
214
|
+ |
|
|
215
|
+ |
test "an unbound upload is still the ordinary case", context do
|
|
216
|
+ |
assert {:ok, trace, :created} = Traces.store(context.owner, @document)
|
|
217
|
+ |
assert is_nil(trace.assignment_id)
|
|
218
|
+ |
assert Activity.for_issue(context.issue, context.owner).traces == []
|
|
219
|
+ |
end
|
|
220
|
+ |
end
|
|
221
|
+ |
|
|
222
|
+ |
describe "a malformed document does not break the page" do
|
|
223
|
+ |
test "a document with no steps reports zero rather than raising", context do
|
|
224
|
+ |
{:ok, trace, :created} =
|
|
225
|
+ |
Traces.store(context.owner, %{"schema_version" => "ATIF-v1.7"},
|
|
226
|
+ |
assignment_id: context.attempt.id,
|
|
227
|
+ |
visibility: "pulse"
|
|
228
|
+ |
)
|
|
229
|
+ |
|
|
230
|
+ |
viewer = WorkDisclosure.viewer(context.repository, context.member)
|
|
231
|
+ |
projection = TraceDisclosure.project(trace, context.attempt, viewer)
|
|
232
|
+ |
|
|
233
|
+ |
assert projection.step_count == 0
|
|
234
|
+ |
end
|
|
235
|
+ |
end
|
|
236
|
+ |
|
|
237
|
+ |
defp go_private(repository) do
|
|
238
|
+ |
{1, _} =
|
|
239
|
+ |
Repo.update_all(
|
|
240
|
+ |
Ecto.Query.from(r in OpenAgents.Repositories.Repository, where: r.id == ^repository.id),
|
|
241
|
+ |
set: [visibility: "private"]
|
|
242
|
+ |
)
|
|
243
|
+ |
|
|
244
|
+ |
:ok
|
|
245
|
+ |
end
|
|
246
|
+ |
|
|
247
|
+ |
defp upload(user, attempt, visibility) do
|
|
248
|
+ |
options =
|
|
249
|
+ |
[assignment_id: attempt.id] ++
|
|
250
|
+ |
if visibility, do: [visibility: visibility], else: []
|
|
251
|
+ |
|
|
252
|
+ |
# A distinct document per upload, because `store/3` deduplicates on the
|
|
253
|
+ |
# canonical bytes per account.
|
|
254
|
+ |
document = Map.put(@document, "session_id", "s#{System.unique_integer([:positive])}")
|
|
255
|
+ |
|
|
256
|
+ |
{:ok, trace, _} = Traces.store(user, document, options)
|
|
257
|
+ |
trace
|
|
258
|
+ |
end
|
|
259
|
+ |
|
|
260
|
+ |
# The run never starts in a test. The assignment is committed by
|
|
261
|
+ |
# `persist_assignment/7` before `start_target/7` is reached, so the row this
|
|
262
|
+ |
# returns is the row the production path writes.
|
|
263
|
+ |
defp admit(owner, repository, issue) do
|
|
264
|
+ |
{:ok, conversation} = Conversations.ensure_conversation(owner)
|
|
265
|
+ |
|
|
266
|
+ |
{:ok, box} =
|
|
267
|
+ |
%ConversationBox{}
|
|
268
|
+ |
|> ConversationBox.changeset(%{
|
|
269
|
+ |
conversation_id: conversation.id,
|
|
270
|
+ |
box_id: "bx_trace_#{System.unique_integer([:positive])}",
|
|
271
|
+ |
state: "ready",
|
|
272
|
+ |
setup_status: "done"
|
|
273
|
+ |
})
|
|
274
|
+ |
|> Repo.insert()
|
|
275
|
+ |
|
|
276
|
+ |
_ =
|
|
277
|
+ |
try do
|
|
278
|
+ |
Assignments.create(%{
|
|
279
|
+ |
"target_kind" => "box",
|
|
280
|
+ |
"box_id" => box.box_id,
|
|
281
|
+ |
"conversation_id" => conversation.id,
|
|
282
|
+ |
"repository_id" => repository.id,
|
|
283
|
+ |
"issue_number" => issue.number,
|
|
284
|
+ |
"branch" => "agent/issue-#{issue.number}",
|
|
285
|
+ |
"requesting_user" => owner,
|
|
286
|
+ |
"requesting_principal" => owner
|
|
287
|
+ |
})
|
|
288
|
+ |
rescue
|
|
289
|
+ |
error -> {:error, error}
|
|
290
|
+ |
end
|
|
291
|
+ |
|
|
292
|
+ |
Assignment
|
|
293
|
+ |
|> Repo.get_by!(issue_id: issue.id)
|
|
294
|
+ |
|> Repo.preload([:artifact_link, :work_job])
|
|
295
|
+ |
end
|
|
296
|
+ |
|
|
297
|
+ |
# The operator identity is the one `OpenAgents.Accounts.admin?/1` admits, so
|
|
298
|
+ |
# this needs no global state and stays safe to run concurrently.
|
|
299
|
+ |
defp admin_user do
|
|
300
|
+ |
{:ok, user} =
|
|
301
|
+ |
OpenAgents.Accounts.upsert_github_user(%{
|
|
302
|
+ |
github_id: 14_167_547,
|
|
303
|
+ |
github_login: "trace-operator",
|
|
304
|
+ |
github_avatar_url: "https://avatars.githubusercontent.com/u/14167547?v=4"
|
|
305
|
+ |
})
|
|
306
|
+ |
|
|
307
|
+ |
true = OpenAgents.Accounts.admin?(user)
|
|
308
|
+ |
user
|
|
309
|
+ |
end
|
|
310
|
+ |
end
|