|
1
|
+ |
defmodule OpenAgents.Issues.CompletionClaims do
|
|
2
|
+ |
@moduledoc """
|
|
3
|
+ |
Store what an attempt claims about an issue, grade it, and close the issue
|
|
4
|
+ |
only when a narrow rule says the claim entails the outcome.
|
|
5
|
+ |
|
|
6
|
+ |
## The gap this closes, and the gap it refuses to close
|
|
7
|
+ |
|
|
8
|
+ |
`#148` bound an issue to the receipts that evaluated its commit. That answers
|
|
9
|
+ |
"what evaluated this?" and it does not answer "is this done?". A green build
|
|
10
|
+ |
on a commit whose message names an issue proves the tree compiles. A
|
|
11
|
+ |
successful deployment proves bytes reached a host. Neither entails that the
|
|
12
|
+ |
outcome the issue asked for was produced, and a rule that closed an issue on
|
|
13
|
+ |
either would file a receipt saying work was verified when nothing verified
|
|
14
|
+ |
it.
|
|
15
|
+ |
|
|
16
|
+ |
So no receipt closes an issue here. What closes an issue is a *claim*: an
|
|
17
|
+ |
assertion that names, for each of the issue's acceptance criteria, the
|
|
18
|
+ |
evidence that satisfied it. Mapping intent to evidence is the one judgment no
|
|
19
|
+ |
record can make on its own, which is why it is claimed rather than derived —
|
|
20
|
+ |
and why the rest is derived rather than claimed.
|
|
21
|
+ |
|
|
22
|
+ |
## What the caller supplies, and what it cannot
|
|
23
|
+ |
|
|
24
|
+ |
The caller supplies exactly one thing: the criterion-to-evidence mapping, and
|
|
25
|
+ |
optionally the false-green classes it names against its own result, which can
|
|
26
|
+ |
only make the verdict worse. Everything else is read from records:
|
|
27
|
+ |
|
|
28
|
+ |
* the **issue's** four required sections, parsed from its body;
|
|
29
|
+ |
* the **attempt's** five binding fields — issue number, repository,
|
|
30
|
+ |
authority, budget, revision — from `forge_assignments` and the
|
|
31
|
+ |
`budget_snapshot` on its `work_jobs` row, never from the request, so the
|
|
32
|
+ |
attempt cannot claim to be bound to an issue it did not run against;
|
|
33
|
+ |
* the **verifier**, which is the published check result the evidence
|
|
34
|
+ |
resolves to. It is admitted because publishing a check result is already
|
|
35
|
+ |
authority-gated (`OpenAgents.Deployments.Authority`), and it is
|
|
36
|
+ |
independent of the producer unless the same user both requested the
|
|
37
|
+ |
attempt and published the check;
|
|
38
|
+ |
* the **falsifier**, which is the observation that would have made the same
|
|
39
|
+ |
claim red: the same check name, on the same commit and the same artifact
|
|
40
|
+ |
digest, reporting `failed`. `deployment_check_results` keys on exactly
|
|
41
|
+ |
that identity, so the falsifying observation is a row that can exist.
|
|
42
|
+ |
|
|
43
|
+ |
Producer-verifier separation is always required on this path. An agent
|
|
44
|
+ |
grading its own work is the hazard the whole contract exists for, so it is
|
|
45
|
+ |
not a knob.
|
|
46
|
+ |
|
|
47
|
+ |
## The closure rule
|
|
48
|
+ |
|
|
49
|
+ |
An issue closes automatically only when every one of these holds:
|
|
50
|
+ |
|
|
51
|
+ |
1. The repository opted in twice: `agents_enabled` and
|
|
52
|
+ |
`verified_closing_enabled` (`OpenAgents.Issues.ClosurePolicy`). Both
|
|
53
|
+ |
default false, and an absent policy row means the same as both false.
|
|
54
|
+ |
2. `OpenAgents.AcceptedOutcome.evaluate/1` graded the claim `accepted`.
|
|
55
|
+ |
3. Every acceptance criterion names an `issue_evidence` edge that is a
|
|
56
|
+ |
**qualification** receipt for **this issue** at **this exact revision**
|
|
57
|
+ |
whose status is `succeeded`. An edge in any other family, for any other
|
|
58
|
+ |
revision, or for any other issue satisfies nothing, so the criterion is
|
|
59
|
+ |
unevidenced and the claim is `incomplete`.
|
|
60
|
+ |
4. No evidence edge for this issue at this revision carries a failing
|
|
61
|
+ |
terminal word in any family. A revision that qualified and then failed
|
|
62
|
+ |
to build is contradicted, and a person decides.
|
|
63
|
+ |
5. The issue is open. This path never reopens and never changes a closed
|
|
64
|
+ |
issue's state.
|
|
65
|
+ |
|
|
66
|
+ |
### Why only qualification
|
|
67
|
+ |
|
|
68
|
+ |
`deployment_check_results` is the only family whose row is a *verdict about
|
|
69
|
+ |
named bytes*: identity is `{repository, name, commit, artifact digest}`, so a
|
|
70
|
+ |
green result cannot be replayed onto bytes it never examined, and the
|
|
71
|
+ |
publisher is an authorized principal that is not the attempt. The other three
|
|
72
|
+ |
record something real and something else:
|
|
73
|
+ |
|
|
74
|
+ |
* **push** — that the forge received the bytes. It carries no outcome at
|
|
75
|
+ |
all; its `result` is null.
|
|
76
|
+ |
* **build** — that the tree compiles at that commit. Necessary for anything
|
|
77
|
+ |
to work; sufficient for nothing to be done.
|
|
78
|
+ |
* **deployment** — that an artifact reached an environment. An operational
|
|
79
|
+ |
fact about placement, not an observation of behavior.
|
|
80
|
+ |
|
|
81
|
+ |
All four still *record*: they appear on the issue's evidence chain, they are
|
|
82
|
+ |
what a person reads, and a failing one in any family blocks the automatic
|
|
83
|
+ |
close by rule 4. Recording is the broad half; closing is the narrow half.
|
|
84
|
+ |
|
|
85
|
+ |
## When a later receipt disagrees
|
|
86
|
+ |
|
|
87
|
+ |
`note_evidence/1` runs where evidence is written. A failing edge arriving for
|
|
88
|
+ |
an issue and revision an accepted claim already closed stamps
|
|
89
|
+ |
`contradicted_at` and names the edge. It does **not** reopen the issue:
|
|
90
|
+ |
reopening on a later signal is a separate policy with its own failure modes
|
|
91
|
+ |
(`ISSUE-001` draws the same line for reverts). What it does is stop the
|
|
92
|
+ |
closure reading as uncontested, and rule 4 stops any further close on that
|
|
93
|
+ |
revision.
|
|
94
|
+ |
|
|
95
|
+ |
## One closer, not two
|
|
96
|
+ |
|
|
97
|
+ |
`OpenAgents.Issues.ClosingReferences` stays the trailer path and is untouched:
|
|
98
|
+ |
a person wrote `Closes #N`, the pusher's write authority was checked, and the
|
|
99
|
+ |
commit was reachable from the default branch. That is a human assertion and it
|
|
100
|
+ |
stays attributed to that person. This path closes only issues that are still
|
|
101
|
+ |
open, so a trailer close that already happened is recorded here as a claim
|
|
102
|
+ |
and moves nothing. Neither path reads commit prose —
|
|
103
|
+ |
`OpenAgents.Forge.CommitReferences` is still the only reader of that.
|
|
104
|
+ |
|
|
105
|
+ |
A reader tells the two apart by the record, not by the issue: a person's close
|
|
106
|
+ |
leaves an `issue_closing_references` row with a `closed_by_user_id`; this one
|
|
107
|
+ |
leaves an `issue_completion_claims` row whose `closed_by_actor` is the system
|
|
108
|
+ |
principal `system:accepted-outcome` and never a user id.
|
|
109
|
+ |
"""
|
|
110
|
+ |
|
|
111
|
+ |
import Ecto.Query
|
|
112
|
+ |
|
|
113
|
+ |
require Logger
|
|
114
|
+ |
|
|
115
|
+ |
alias OpenAgents.AcceptedOutcome
|
|
116
|
+ |
alias OpenAgents.Deployments.CheckResult
|
|
117
|
+ |
alias OpenAgents.Forge.Assignment
|
|
118
|
+ |
alias OpenAgents.Issues
|
|
119
|
+ |
alias OpenAgents.Issues.{ClosurePolicy, CompletionClaim, EvidenceEntry, Issue}
|
|
120
|
+ |
alias OpenAgents.Repo
|
|
121
|
+ |
alias OpenAgents.Repositories.Repository
|
|
122
|
+ |
alias OpenAgents.Work.Job
|
|
123
|
+ |
|
|
124
|
+ |
# The one actor an automatic close is attributed to. It is not a user id and
|
|
125
|
+ |
# cannot become one, which is the whole distinction between this close and a
|
|
126
|
+ |
# person's.
|
|
127
|
+ |
@closing_actor "system:accepted-outcome"
|
|
128
|
+ |
|
|
129
|
+ |
# The one family whose receipt can satisfy an acceptance criterion. See the
|
|
130
|
+ |
# moduledoc: the other three record, they do not qualify.
|
|
131
|
+ |
@closing_family "qualification"
|
|
132
|
+ |
|
|
133
|
+ |
# A qualification receipt satisfies a criterion only with this status.
|
|
134
|
+ |
# `pending` is not a verdict, and `failed` is the opposite of one.
|
|
135
|
+ |
@qualifying_result "succeeded"
|
|
136
|
+ |
|
|
137
|
+ |
# Each family's own words for "this did not work", read straight from the
|
|
138
|
+ |
# schemas that own them: `forge_builds.status`, `forge_deploys.result`,
|
|
139
|
+ |
# `deployment_runs.state`, and `deployment_check_results.status`. A push
|
|
140
|
+ |
# receipt has no result, so it can never contradict.
|
|
141
|
+ |
@failing_results %{
|
|
142
|
+ |
"push" => [],
|
|
143
|
+ |
"build" => ~w(failed expired),
|
|
144
|
+ |
"deployment" => ~w(failed reverted needs_rolling_replace cancelled superseded),
|
|
145
|
+ |
"qualification" => ~w(failed)
|
|
146
|
+ |
}
|
|
147
|
+ |
|
|
148
|
+ |
# The issue-body headings the accepted-outcome contract requires. The match is
|
|
149
|
+ |
# on the heading text, after the semantic question — "is this claim gradeable
|
|
150
|
+ |
# at all" — has already been settled by the policy read.
|
|
151
|
+ |
@section_headings %{
|
|
152
|
+ |
problem: ["problem"],
|
|
153
|
+ |
scope: ["scope"],
|
|
154
|
+ |
acceptance_criteria: ["acceptance criteria"],
|
|
155
|
+ |
success_metrics: ["success metrics"]
|
|
156
|
+ |
}
|
|
157
|
+ |
|
|
158
|
+ |
@type verdict :: {:ok, CompletionClaim.t()} | {:error, atom()}
|
|
159
|
+ |
|
|
160
|
+ |
@doc "The system principal an automatic close is attributed to."
|
|
161
|
+ |
@spec closing_actor() :: String.t()
|
|
162
|
+ |
def closing_actor, do: @closing_actor
|
|
163
|
+ |
|
|
164
|
+ |
@doc "The one receipt family whose evidence can satisfy an acceptance criterion."
|
|
165
|
+ |
@spec closing_family() :: String.t()
|
|
166
|
+ |
def closing_family, do: @closing_family
|
|
167
|
+ |
|
|
168
|
+ |
@doc "The receipt families that can record evidence but never close an issue."
|
|
169
|
+ |
@spec recording_only_families() :: [String.t()]
|
|
170
|
+ |
def recording_only_families,
|
|
171
|
+ |
do: Enum.reject(EvidenceEntry.families(), &(&1 == @closing_family))
|
|
172
|
+ |
|
|
173
|
+ |
# ── policy ──────────────────────────────────────────────────────────────
|
|
174
|
+ |
|
|
175
|
+ |
@doc """
|
|
176
|
+ |
One repository's closure policy, defaulted rather than absent.
|
|
177
|
+ |
|
|
178
|
+ |
An unconfigured repository reads as both flags false, which is the same
|
|
179
|
+ |
answer the stored row with both flags false gives. Nothing distinguishes
|
|
180
|
+ |
silence from a deliberate no, because nothing should.
|
|
181
|
+ |
"""
|
|
182
|
+ |
@spec policy(Repository.t() | binary()) :: ClosurePolicy.t()
|
|
183
|
+ |
def policy(%Repository{id: id}), do: policy(id)
|
|
184
|
+ |
|
|
185
|
+ |
def policy(repository_id) when is_binary(repository_id) do
|
|
186
|
+ |
Repo.one(from p in ClosurePolicy, where: p.repository_id == ^repository_id) ||
|
|
187
|
+ |
%ClosurePolicy{
|
|
188
|
+ |
repository_id: repository_id,
|
|
189
|
+ |
agents_enabled: false,
|
|
190
|
+ |
verified_closing_enabled: false
|
|
191
|
+ |
}
|
|
192
|
+ |
end
|
|
193
|
+ |
|
|
194
|
+ |
@doc "Set one repository's closure policy, attributed to the user who set it."
|
|
195
|
+ |
@spec set_policy(Repository.t(), map(), OpenAgents.Accounts.User.t() | nil) ::
|
|
196
|
+ |
{:ok, ClosurePolicy.t()} | {:error, Ecto.Changeset.t()}
|
|
197
|
+ |
def set_policy(%Repository{id: repository_id}, attrs, actor \\ nil) do
|
|
198
|
+ |
existing =
|
|
199
|
+ |
Repo.one(from p in ClosurePolicy, where: p.repository_id == ^repository_id) ||
|
|
200
|
+ |
%ClosurePolicy{}
|
|
201
|
+ |
|
|
202
|
+ |
attrs =
|
|
203
|
+ |
attrs
|
|
204
|
+ |
|> Map.new(fn {key, value} -> {to_string(key), value} end)
|
|
205
|
+ |
|> Map.put("repository_id", repository_id)
|
|
206
|
+ |
|> Map.put("updated_by_user_id", actor && actor.id)
|
|
207
|
+ |
|
|
208
|
+ |
existing |> ClosurePolicy.changeset(attrs) |> Repo.insert_or_update()
|
|
209
|
+ |
end
|
|
210
|
+ |
|
|
211
|
+ |
# ── the claim ───────────────────────────────────────────────────────────
|
|
212
|
+ |
|
|
213
|
+ |
@doc """
|
|
214
|
+ |
Grade and store one completion claim for a finished attempt.
|
|
215
|
+ |
|
|
216
|
+ |
`author` is `:agent` or `:human`. A human's claim is `not_applicable` —
|
|
217
|
+ |
people close their own issues through the ordinary path, and the contract
|
|
218
|
+ |
gates agent-authored claims only.
|
|
219
|
+ |
|
|
220
|
+ |
`attrs` carries `:evidence`, a list of `%{criterion: binary, evidence_id:
|
|
221
|
+ |
binary}`, and optionally `:false_green_classes`. Nothing else in `attrs` is
|
|
222
|
+ |
read: the attempt's binding, the verifier, and the falsifier are derived from
|
|
223
|
+ |
records so that a claim cannot assert its own authority.
|
|
224
|
+ |
|
|
225
|
+ |
Returns the stored claim. A non-accepted verdict is stored too — a typed
|
|
226
|
+ |
refusal on the record is the point, not silence.
|
|
227
|
+ |
"""
|
|
228
|
+ |
@spec submit(Assignment.t(), :agent | :human, map()) :: verdict()
|
|
229
|
+ |
def submit(%Assignment{} = assignment, author, attrs \\ %{})
|
|
230
|
+ |
when author in [:agent, :human] do
|
|
231
|
+ |
with {:ok, assignment} <- terminal_attempt(assignment),
|
|
232
|
+ |
{:ok, repository} <- fetch(Repository, assignment.repository_id),
|
|
233
|
+ |
{:ok, issue} <- fetch_issue(assignment.issue_id) do
|
|
234
|
+ |
revision = String.downcase(assignment.terminal_commit)
|
|
235
|
+ |
policy = policy(repository)
|
|
236
|
+ |
references = evidence_references(issue, revision, attrs)
|
|
237
|
+ |
|
|
238
|
+ |
claim =
|
|
239
|
+ |
build_claim(
|
|
240
|
+ |
author,
|
|
241
|
+ |
policy,
|
|
242
|
+ |
repository,
|
|
243
|
+ |
issue,
|
|
244
|
+ |
assignment,
|
|
245
|
+ |
revision,
|
|
246
|
+ |
references,
|
|
247
|
+ |
attrs
|
|
248
|
+ |
)
|
|
249
|
+ |
|
|
250
|
+ |
persist(repository, issue, assignment, revision, AcceptedOutcome.evaluate(claim), policy)
|
|
251
|
+ |
end
|
|
252
|
+ |
end
|
|
253
|
+ |
|
|
254
|
+ |
@doc "The claims recorded against one issue, oldest first."
|
|
255
|
+ |
@spec for_issue(Issue.t() | integer()) :: [map()]
|
|
256
|
+ |
def for_issue(%Issue{id: id}), do: for_issue(id)
|
|
257
|
+ |
|
|
258
|
+ |
def for_issue(issue_id) when is_integer(issue_id) do
|
|
259
|
+ |
CompletionClaim
|
|
260
|
+ |
|> where([claim], claim.issue_id == ^issue_id)
|
|
261
|
+ |
|> order_by([claim], asc: claim.inserted_at, asc: claim.id)
|
|
262
|
+ |
|> Repo.all()
|
|
263
|
+ |
|> Enum.map(&summary/1)
|
|
264
|
+ |
end
|
|
265
|
+ |
|
|
266
|
+ |
@doc """
|
|
267
|
+ |
Claims for a whole page of issues, keyed by issue id.
|
|
268
|
+ |
|
|
269
|
+ |
One query for the page, the way `OpenAgents.Issues.Evidence.for_issues/1`
|
|
270
|
+ |
reads evidence, so listing issues does not cost one query per row.
|
|
271
|
+ |
"""
|
|
272
|
+ |
@spec for_issues([Issue.t()]) :: %{integer() => [map()]}
|
|
273
|
+ |
def for_issues(issues) when is_list(issues) do
|
|
274
|
+ |
ids = Enum.map(issues, & &1.id)
|
|
275
|
+ |
base = Map.new(ids, &{&1, []})
|
|
276
|
+ |
|
|
277
|
+ |
CompletionClaim
|
|
278
|
+ |
|> where([claim], claim.issue_id in ^ids)
|
|
279
|
+ |
|> order_by([claim], asc: claim.inserted_at, asc: claim.id)
|
|
280
|
+ |
|> Repo.all()
|
|
281
|
+ |
|> Enum.reduce(base, fn claim, acc ->
|
|
282
|
+ |
Map.update(acc, claim.issue_id, [summary(claim)], &(&1 ++ [summary(claim)]))
|
|
283
|
+ |
end)
|
|
284
|
+ |
end
|
|
285
|
+ |
|
|
286
|
+ |
@doc """
|
|
287
|
+ |
The bounded projection of one stored claim.
|
|
288
|
+ |
|
|
289
|
+ |
It carries the verdict, the typed reasons, the criterion names, the public
|
|
290
|
+ |
receipt references, and the close. It never carries a prompt, a log, a
|
|
291
|
+ |
private repository name, or a private receipt reference, because the criteria
|
|
292
|
+ |
it stores were already reduced to that shape by
|
|
293
|
+ |
`OpenAgents.AcceptedOutcome.public_projection/1` before the row was written.
|
|
294
|
+ |
"""
|
|
295
|
+ |
@spec summary(CompletionClaim.t()) :: map()
|
|
296
|
+ |
def summary(%CompletionClaim{} = claim) do
|
|
297
|
+ |
%{
|
|
298
|
+ |
id: claim.id,
|
|
299
|
+ |
revision: claim.revision,
|
|
300
|
+ |
state: claim.state,
|
|
301
|
+ |
reasons: claim.reasons,
|
|
302
|
+ |
criteria: claim.criteria,
|
|
303
|
+ |
verifier: claim.verifier,
|
|
304
|
+ |
falsifier: claim.falsifier,
|
|
305
|
+ |
closed: claim.closed,
|
|
306
|
+ |
closed_at: claim.closed_at,
|
|
307
|
+ |
closed_by_actor: claim.closed_by_actor,
|
|
308
|
+ |
contradicted_at: claim.contradicted_at,
|
|
309
|
+ |
contradiction_reason: claim.contradiction_reason,
|
|
310
|
+ |
recorded_at: claim.inserted_at
|
|
311
|
+ |
}
|
|
312
|
+ |
end
|
|
313
|
+ |
|
|
314
|
+ |
@doc """
|
|
315
|
+ |
Mark the accepted claims one new evidence edge contradicts.
|
|
316
|
+ |
|
|
317
|
+ |
Called where evidence is written. A failing edge for an issue and revision an
|
|
318
|
+ |
accepted claim rested on stamps the claim; nothing else moves, and the issue
|
|
319
|
+ |
is never reopened. Never raises and never fails the receipt that produced it.
|
|
320
|
+ |
"""
|
|
321
|
+ |
@spec note_evidence(EvidenceEntry.t()) :: :ok
|
|
322
|
+ |
def note_evidence(%EvidenceEntry{} = entry) do
|
|
323
|
+ |
if failing?(entry) do
|
|
324
|
+ |
now = DateTime.utc_now()
|
|
325
|
+ |
|
|
326
|
+ |
{_count, _rows} =
|
|
327
|
+ |
CompletionClaim
|
|
328
|
+ |
|> where([claim], claim.issue_id == ^entry.issue_id)
|
|
329
|
+ |
|> where([claim], claim.revision == ^String.downcase(entry.commit_sha))
|
|
330
|
+ |
|> where([claim], claim.state == "accepted" and is_nil(claim.contradicted_at))
|
|
331
|
+ |
|> Repo.update_all(
|
|
332
|
+ |
set: [
|
|
333
|
+ |
contradicted_at: now,
|
|
334
|
+ |
contradicted_by_evidence_id: entry.id,
|
|
335
|
+ |
contradiction_reason: "#{entry.family}:#{entry.result}",
|
|
336
|
+ |
updated_at: now
|
|
337
|
+ |
]
|
|
338
|
+ |
)
|
|
339
|
+ |
|
|
340
|
+ |
:ok
|
|
341
|
+ |
else
|
|
342
|
+ |
:ok
|
|
343
|
+ |
end
|
|
344
|
+ |
end
|
|
345
|
+ |
|
|
346
|
+ |
def note_evidence(_entry), do: :ok
|
|
347
|
+ |
|
|
348
|
+ |
@doc "Whether one evidence edge carries its family's word for failure."
|
|
349
|
+ |
@spec failing?(EvidenceEntry.t()) :: boolean()
|
|
350
|
+ |
def failing?(%EvidenceEntry{family: family, result: result}) when is_binary(result) do
|
|
351
|
+ |
result in Map.get(@failing_results, family, [])
|
|
352
|
+ |
end
|
|
353
|
+ |
|
|
354
|
+ |
def failing?(%EvidenceEntry{}), do: false
|
|
355
|
+ |
|
|
356
|
+ |
# ── building the claim from records ─────────────────────────────────────
|
|
357
|
+ |
|
|
358
|
+ |
defp build_claim(author, policy, repository, issue, assignment, revision, references, attrs) do
|
|
359
|
+ |
%{
|
|
360
|
+ |
actor: if(author == :agent, do: :agent, else: :human),
|
|
361
|
+ |
agents_enabled: policy.agents_enabled,
|
|
362
|
+ |
issue: %{
|
|
363
|
+ |
number: issue.number,
|
|
364
|
+ |
repository: "#{repository.owner}/#{repository.name}",
|
|
365
|
+ |
sections: sections(issue.body)
|
|
366
|
+ |
},
|
|
367
|
+ |
attempt: %{
|
|
368
|
+ |
issue_number: issue.number,
|
|
369
|
+ |
repository: "#{repository.owner}/#{repository.name}",
|
|
370
|
+ |
authority: authority(assignment),
|
|
371
|
+ |
budget: budget(assignment),
|
|
372
|
+ |
revision: revision
|
|
373
|
+ |
},
|
|
374
|
+ |
verification: %{
|
|
375
|
+ |
verifier: verifier(references, assignment),
|
|
376
|
+ |
falsifier: falsifier(references),
|
|
377
|
+ |
terminal_result: terminal_result(references),
|
|
378
|
+ |
separation_required: true,
|
|
379
|
+ |
false_green_classes: named_false_greens(attrs)
|
|
380
|
+ |
},
|
|
381
|
+ |
evidence:
|
|
382
|
+ |
Enum.map(references, fn reference ->
|
|
383
|
+ |
%{
|
|
384
|
+ |
criterion: reference.criterion,
|
|
385
|
+ |
receipt: reference.receipt,
|
|
386
|
+ |
visibility: reference.visibility
|
|
387
|
+ |
}
|
|
388
|
+ |
end)
|
|
389
|
+ |
}
|
|
390
|
+ |
end
|
|
391
|
+ |
|
|
392
|
+ |
# The five attempt fields the contract binds come from records, never from
|
|
393
|
+ |
# the request. Two of them are the issue and the repository, which is why
|
|
394
|
+ |
# `attempt_not_bound_to_issue` cannot fire on this path: an attempt that ran
|
|
395
|
+ |
# against another issue is a different `forge_assignments` row and produces a
|
|
396
|
+ |
# different claim.
|
|
397
|
+ |
defp authority(%Assignment{requesting_principal: %{"type" => type, "id" => id}})
|
|
398
|
+ |
when is_binary(type) and is_binary(id),
|
|
399
|
+ |
do: "#{type}:#{id}"
|
|
400
|
+ |
|
|
401
|
+ |
defp authority(%Assignment{}), do: nil
|
|
402
|
+ |
|
|
403
|
+ |
defp budget(%Assignment{work_job_id: nil}), do: nil
|
|
404
|
+ |
|
|
405
|
+ |
defp budget(%Assignment{work_job_id: job_id}) do
|
|
406
|
+ |
case Repo.one(from job in Job, where: job.id == ^job_id, select: job.budget_snapshot) do
|
|
407
|
+ |
snapshot when is_map(snapshot) and map_size(snapshot) > 0 -> snapshot
|
|
408
|
+ |
_absent -> nil
|
|
409
|
+ |
end
|
|
410
|
+ |
end
|
|
411
|
+ |
|
|
412
|
+ |
# Only edges this issue already owns, at this exact revision, in the one
|
|
413
|
+ |
# family that qualifies, with the one status that qualifies. A reference to
|
|
414
|
+ |
# anything else resolves to no receipt, so the criterion it names is
|
|
415
|
+ |
# unevidenced and the claim is `incomplete` — which is the contract's own way
|
|
416
|
+ |
# of saying "a build is not a verdict".
|
|
417
|
+ |
defp evidence_references(%Issue{} = issue, revision, attrs) do
|
|
418
|
+ |
requested =
|
|
419
|
+ |
attrs
|
|
420
|
+ |
|> Map.get(:evidence, Map.get(attrs, "evidence", []))
|
|
421
|
+ |
|> List.wrap()
|
|
422
|
+ |
|> Enum.map(&normalize_reference/1)
|
|
423
|
+ |
|> Enum.reject(&is_nil/1)
|
|
424
|
+ |
|
|
425
|
+ |
ids = requested |> Enum.map(& &1.evidence_id) |> Enum.reject(&is_nil/1)
|
|
426
|
+ |
|
|
427
|
+ |
qualifying =
|
|
428
|
+ |
if ids == [] do
|
|
429
|
+ |
%{}
|
|
430
|
+ |
else
|
|
431
|
+ |
EvidenceEntry
|
|
432
|
+ |
|> where([entry], entry.id in ^ids)
|
|
433
|
+ |
|> where([entry], entry.issue_id == ^issue.id)
|
|
434
|
+ |
|> where([entry], entry.commit_sha == ^revision)
|
|
435
|
+ |
|> where([entry], entry.family == ^@closing_family)
|
|
436
|
+ |
|> where([entry], entry.result == ^@qualifying_result)
|
|
437
|
+ |
|> Repo.all()
|
|
438
|
+ |
|> Map.new(&{&1.id, &1})
|
|
439
|
+ |
end
|
|
440
|
+ |
|
|
441
|
+ |
Enum.map(requested, fn reference ->
|
|
442
|
+ |
entry = Map.get(qualifying, reference.evidence_id)
|
|
443
|
+ |
|
|
444
|
+ |
%{
|
|
445
|
+ |
criterion: reference.criterion,
|
|
446
|
+ |
entry: entry,
|
|
447
|
+ |
receipt: entry && entry.id,
|
|
448
|
+ |
visibility: visibility(issue)
|
|
449
|
+ |
}
|
|
450
|
+ |
end)
|
|
451
|
+ |
end
|
|
452
|
+ |
|
|
453
|
+ |
defp normalize_reference(%{} = reference) do
|
|
454
|
+ |
criterion = reference[:criterion] || reference["criterion"]
|
|
455
|
+ |
evidence_id = reference[:evidence_id] || reference["evidence_id"]
|
|
456
|
+ |
|
|
457
|
+ |
case {present(criterion), cast_id(evidence_id)} do
|
|
458
|
+ |
{nil, _id} -> nil
|
|
459
|
+ |
{criterion, id} -> %{criterion: criterion, evidence_id: id}
|
|
460
|
+ |
end
|
|
461
|
+ |
end
|
|
462
|
+ |
|
|
463
|
+ |
defp normalize_reference(_reference), do: nil
|
|
464
|
+ |
|
|
465
|
+ |
defp cast_id(value) do
|
|
466
|
+ |
case Ecto.UUID.cast(value) do
|
|
467
|
+ |
{:ok, id} -> id
|
|
468
|
+ |
:error -> nil
|
|
469
|
+ |
end
|
|
470
|
+ |
end
|
|
471
|
+ |
|
|
472
|
+ |
defp present(value) when is_binary(value) do
|
|
473
|
+ |
case String.trim(value) do
|
|
474
|
+ |
"" -> nil
|
|
475
|
+ |
trimmed -> trimmed
|
|
476
|
+ |
end
|
|
477
|
+ |
end
|
|
478
|
+ |
|
|
479
|
+ |
defp present(_value), do: nil
|
|
480
|
+ |
|
|
481
|
+ |
# A public repository's receipt references are publishable; a private
|
|
482
|
+ |
# repository's are not, and the projection renders them as `:private` without
|
|
483
|
+ |
# the reference. The visibility is read from the repository rather than from
|
|
484
|
+ |
# the caller for the same reason everything else here is.
|
|
485
|
+ |
defp visibility(%Issue{repository_id: repository_id}) do
|
|
486
|
+ |
case Repo.one(from r in Repository, where: r.id == ^repository_id, select: r.visibility) do
|
|
487
|
+ |
"public" -> :public
|
|
488
|
+ |
_private_or_absent -> :private
|
|
489
|
+ |
end
|
|
490
|
+ |
end
|
|
491
|
+ |
|
|
492
|
+ |
# The verifier is the check result the evidence resolves to, never a name the
|
|
493
|
+ |
# caller supplied. It is admitted because publishing a check result is
|
|
494
|
+ |
# already authority-gated; it is independent unless the same user both
|
|
495
|
+ |
# requested the attempt and published the check.
|
|
496
|
+ |
defp verifier(references, %Assignment{} = assignment) do
|
|
497
|
+ |
case check_results(references) do
|
|
498
|
+ |
[] ->
|
|
499
|
+ |
%{}
|
|
500
|
+ |
|
|
501
|
+ |
results ->
|
|
502
|
+ |
%{
|
|
503
|
+ |
id: results |> Enum.map(& &1.name) |> Enum.uniq() |> Enum.sort() |> Enum.join(","),
|
|
504
|
+ |
admitted: true,
|
|
505
|
+ |
independent_of_producer: Enum.all?(results, &independent?(&1, assignment))
|
|
506
|
+ |
}
|
|
507
|
+ |
end
|
|
508
|
+ |
end
|
|
509
|
+ |
|
|
510
|
+ |
defp independent?(%CheckResult{published_by_user_id: nil}, %Assignment{}), do: true
|
|
511
|
+ |
|
|
512
|
+ |
defp independent?(%CheckResult{published_by_user_id: publisher}, %Assignment{} = assignment) do
|
|
513
|
+ |
authority(assignment) != "user:#{publisher}"
|
|
514
|
+ |
end
|
|
515
|
+ |
|
|
516
|
+ |
# What observation would have made this red: the same check name, on the same
|
|
517
|
+ |
# commit and the same artifact digest, reporting `failed`.
|
|
518
|
+ |
# `deployment_check_results_identity_index` keys on exactly that tuple, so the
|
|
519
|
+ |
# falsifying observation is a row that can exist rather than a sentence.
|
|
520
|
+ |
defp falsifier(references) do
|
|
521
|
+ |
case check_results(references) do
|
|
522
|
+ |
[] ->
|
|
523
|
+ |
nil
|
|
524
|
+ |
|
|
525
|
+ |
results ->
|
|
526
|
+ |
results
|
|
527
|
+ |
|> Enum.map(&"deployment_check_results:#{&1.name}@#{&1.artifact_digest}=failed")
|
|
528
|
+ |
|> Enum.uniq()
|
|
529
|
+ |
|> Enum.sort()
|
|
530
|
+ |
|> Enum.join(" ")
|
|
531
|
+ |
|> String.slice(0, 500)
|
|
532
|
+ |
end
|
|
533
|
+ |
end
|
|
534
|
+ |
|
|
535
|
+ |
defp terminal_result(references) do
|
|
536
|
+ |
entries = references |> Enum.map(& &1.entry) |> Enum.reject(&is_nil/1)
|
|
537
|
+ |
|
|
538
|
+ |
if entries != [] and Enum.all?(entries, &(&1.result == @qualifying_result)),
|
|
539
|
+ |
do: :passed,
|
|
540
|
+ |
else: :failed
|
|
541
|
+ |
end
|
|
542
|
+ |
|
|
543
|
+ |
defp check_results(references) do
|
|
544
|
+ |
ids =
|
|
545
|
+ |
references
|
|
546
|
+ |
|> Enum.map(& &1.entry)
|
|
547
|
+ |
|> Enum.reject(&is_nil/1)
|
|
548
|
+ |
|> Enum.map(& &1.receipt_id)
|
|
549
|
+ |
|> Enum.uniq()
|
|
550
|
+ |
|
|
551
|
+ |
if ids == [], do: [], else: Repo.all(from r in CheckResult, where: r.id in ^ids)
|
|
552
|
+ |
end
|
|
553
|
+ |
|
|
554
|
+ |
# Self-reported, and deliberately the one thing a caller may add: naming a
|
|
555
|
+ |
# false-green class against your own result can only make the verdict worse.
|
|
556
|
+ |
defp named_false_greens(attrs) do
|
|
557
|
+ |
attrs
|
|
558
|
+ |
|> Map.get(:false_green_classes, Map.get(attrs, "false_green_classes", []))
|
|
559
|
+ |
|> List.wrap()
|
|
560
|
+ |
|> Enum.map(&to_string/1)
|
|
561
|
+ |
|> Enum.filter(&(&1 in AcceptedOutcome.false_green_classes()))
|
|
562
|
+ |
end
|
|
563
|
+ |
|
|
564
|
+ |
# ── issue sections ──────────────────────────────────────────────────────
|
|
565
|
+ |
|
|
566
|
+ |
# Deterministic parsing of a bounded field, after the question of whether to
|
|
567
|
+ |
# grade at all has been answered by the stored policy. It reads Markdown ATX
|
|
568
|
+ |
# headings and nothing else: a section is present when its heading exists and
|
|
569
|
+ |
# some non-blank line follows it before the next heading.
|
|
570
|
+ |
defp sections(body) when is_binary(body) do
|
|
571
|
+ |
lines = String.split(body, ~r/\r?\n/)
|
|
572
|
+ |
|
|
573
|
+ |
@section_headings
|
|
574
|
+ |
|> Enum.reduce(%{}, fn {section, headings}, acc ->
|
|
575
|
+ |
Map.put(acc, section, section_body(lines, headings))
|
|
576
|
+ |
end)
|
|
577
|
+ |
end
|
|
578
|
+ |
|
|
579
|
+ |
defp sections(_body), do: %{}
|
|
580
|
+ |
|
|
581
|
+ |
defp section_body(lines, headings) do
|
|
582
|
+ |
lines
|
|
583
|
+ |
|> Enum.drop_while(&(not heading_matching?(&1, headings)))
|
|
584
|
+ |
|> Enum.drop(1)
|
|
585
|
+ |
|> Enum.take_while(&(not heading?(&1)))
|
|
586
|
+ |
|> Enum.map(&strip_marker/1)
|
|
587
|
+ |
|> Enum.reject(&(&1 == ""))
|
|
588
|
+ |
|> case do
|
|
589
|
+ |
[] -> nil
|
|
590
|
+ |
content -> content
|
|
591
|
+ |
end
|
|
592
|
+ |
end
|
|
593
|
+ |
|
|
594
|
+ |
# A criterion is the sentence, not the bullet that carries it. An acceptance
|
|
595
|
+ |
# criterion written `- [ ] the thing` and the same criterion named in a claim
|
|
596
|
+ |
# have to be the same string, or every criterion would read as unevidenced
|
|
597
|
+ |
# for a reason that is about Markdown rather than about the work.
|
|
598
|
+ |
defp strip_marker(line) do
|
|
599
|
+ |
line
|
|
600
|
+ |
|> String.trim()
|
|
601
|
+ |
|> String.replace(~r/\A(?:[-*+]|\d+\.)\s+/, "")
|
|
602
|
+ |
|> String.replace(~r/\A\[[ xX]\]\s+/, "")
|
|
603
|
+ |
|> String.trim()
|
|
604
|
+ |
end
|
|
605
|
+ |
|
|
606
|
+ |
defp heading?(line), do: Regex.match?(~r/\A\s{0,3}\#{1,6}\s+/, line)
|
|
607
|
+ |
|
|
608
|
+ |
defp heading_matching?(line, headings) do
|
|
609
|
+ |
case Regex.run(~r/\A\s{0,3}\#{1,6}\s+(.+?)\s*\#*\s*\z/, line) do
|
|
610
|
+ |
[_line, text] -> normalize_heading(text) in headings
|
|
611
|
+ |
nil -> false
|
|
612
|
+ |
end
|
|
613
|
+ |
end
|
|
614
|
+ |
|
|
615
|
+ |
defp normalize_heading(text) do
|
|
616
|
+ |
text
|
|
617
|
+ |
|> String.downcase()
|
|
618
|
+ |
|> String.replace(~r/[^a-z0-9]+/, " ")
|
|
619
|
+ |
|> String.trim()
|
|
620
|
+ |
end
|
|
621
|
+ |
|
|
622
|
+ |
# ── storing the verdict, and the close ──────────────────────────────────
|
|
623
|
+ |
|
|
624
|
+ |
defp persist(repository, issue, assignment, revision, evaluation, policy) do
|
|
625
|
+ |
projection = AcceptedOutcome.public_projection(evaluation)
|
|
626
|
+ |
{state, reasons} = verdict(evaluation)
|
|
627
|
+ |
withheld = if state == "accepted", do: closure_block(issue, revision, policy), else: nil
|
|
628
|
+ |
close? = state == "accepted" and is_nil(withheld) and issue.state == "open"
|
|
629
|
+ |
now = DateTime.utc_now()
|
|
630
|
+ |
|
|
631
|
+ |
attrs = %{
|
|
632
|
+ |
"repository_id" => repository.id,
|
|
633
|
+ |
"issue_id" => issue.id,
|
|
634
|
+ |
"assignment_id" => assignment.id,
|
|
635
|
+ |
"revision" => revision,
|
|
636
|
+ |
"state" => state,
|
|
637
|
+ |
"reasons" => reasons ++ List.wrap(withheld),
|
|
638
|
+ |
"criteria" => criteria(projection),
|
|
639
|
+ |
"verifier" => verifier_id(evaluation),
|
|
640
|
+ |
"falsifier" => falsifier_of(evaluation),
|
|
641
|
+ |
"closed" => close?,
|
|
642
|
+ |
"closed_at" => if(close?, do: now),
|
|
643
|
+ |
"closed_by_actor" => if(close?, do: @closing_actor)
|
|
644
|
+ |
}
|
|
645
|
+ |
|
|
646
|
+ |
Repo.transaction(fn ->
|
|
647
|
+ |
claim =
|
|
648
|
+ |
case existing(issue, assignment, revision) do
|
|
649
|
+ |
nil -> %CompletionClaim{}
|
|
650
|
+ |
%CompletionClaim{} = found -> found
|
|
651
|
+ |
end
|
|
652
|
+ |
|> CompletionClaim.changeset(attrs)
|
|
653
|
+ |
|> Repo.insert_or_update()
|
|
654
|
+ |
|
|
655
|
+ |
case claim do
|
|
656
|
+ |
{:ok, stored} -> if close?, do: close(issue, stored), else: stored
|
|
657
|
+ |
{:error, changeset} -> Repo.rollback(changeset)
|
|
658
|
+ |
end
|
|
659
|
+ |
end)
|
|
660
|
+ |
|> case do
|
|
661
|
+ |
{:ok, %CompletionClaim{} = stored} ->
|
|
662
|
+ |
{:ok, stored}
|
|
663
|
+ |
|
|
664
|
+ |
{:error, reason} ->
|
|
665
|
+ |
Logger.warning(
|
|
666
|
+ |
"issue_completion_claim_failed issue=#{issue.number} code=#{OpenAgents.OperationalLog.code(reason)}"
|
|
667
|
+ |
)
|
|
668
|
+ |
|
|
669
|
+ |
{:error, :claim_not_recorded}
|
|
670
|
+ |
end
|
|
671
|
+ |
end
|
|
672
|
+ |
|
|
673
|
+ |
# The close and the record land together. A failure anywhere rolls both back,
|
|
674
|
+ |
# so a claim can never say it closed an issue that stayed open, which is the
|
|
675
|
+ |
# same discipline `ClosingReferences` holds for the trailer path.
|
|
676
|
+ |
defp close(%Issue{} = issue, %CompletionClaim{} = claim) do
|
|
677
|
+ |
case Issues.update_issue(issue, %{"state" => "closed", "state_reason" => "completed"}, nil) do
|
|
678
|
+ |
{:ok, _closed} -> claim
|
|
679
|
+ |
{:error, reason} -> Repo.rollback(reason)
|
|
680
|
+ |
end
|
|
681
|
+ |
end
|
|
682
|
+ |
|
|
683
|
+ |
# Rule 4. An accepted claim on a revision that also carries a failing receipt
|
|
684
|
+ |
# in any family records the verdict and withholds the close, because two
|
|
685
|
+ |
# receipts about the same bytes disagree and no rule here is entitled to pick
|
|
686
|
+ |
# one.
|
|
687
|
+ |
defp closure_block(%Issue{} = issue, revision, %ClosurePolicy{} = policy) do
|
|
688
|
+ |
cond do
|
|
689
|
+ |
not policy.verified_closing_enabled ->
|
|
690
|
+ |
"closure_withheld:repository_has_not_opted_in"
|
|
691
|
+ |
|
|
692
|
+ |
contradicted?(issue, revision) ->
|
|
693
|
+ |
"closure_withheld:contradicting_evidence"
|
|
694
|
+ |
|
|
695
|
+ |
issue.state != "open" ->
|
|
696
|
+ |
"closure_withheld:issue_already_closed"
|
|
697
|
+ |
|
|
698
|
+ |
true ->
|
|
699
|
+ |
nil
|
|
700
|
+ |
end
|
|
701
|
+ |
end
|
|
702
|
+ |
|
|
703
|
+ |
defp contradicted?(%Issue{id: issue_id}, revision) do
|
|
704
|
+ |
EvidenceEntry
|
|
705
|
+ |
|> where([entry], entry.issue_id == ^issue_id and entry.commit_sha == ^revision)
|
|
706
|
+ |
|> Repo.all()
|
|
707
|
+ |
|> Enum.any?(&failing?/1)
|
|
708
|
+ |
end
|
|
709
|
+ |
|
|
710
|
+ |
defp existing(%Issue{id: issue_id}, %Assignment{id: assignment_id}, revision) do
|
|
711
|
+ |
Repo.one(
|
|
712
|
+ |
from claim in CompletionClaim,
|
|
713
|
+ |
where:
|
|
714
|
+ |
claim.issue_id == ^issue_id and claim.assignment_id == ^assignment_id and
|
|
715
|
+ |
claim.revision == ^revision
|
|
716
|
+ |
)
|
|
717
|
+ |
end
|
|
718
|
+ |
|
|
719
|
+ |
defp verdict({:accepted, _outcome}), do: {"accepted", []}
|
|
720
|
+ |
defp verdict({:not_applicable, exemption}), do: {"not_applicable", ["#{exemption}"]}
|
|
721
|
+ |
|
|
722
|
+ |
defp verdict({:not_accepted, type, reasons}),
|
|
723
|
+ |
do: {"#{type}", Enum.map(reasons, &render_reason/1)}
|
|
724
|
+ |
|
|
725
|
+ |
defp render_reason({key, detail}) when is_list(detail),
|
|
726
|
+ |
do: "#{key}:#{Enum.join(Enum.map(detail, &to_string/1), ",")}"
|
|
727
|
+ |
|
|
728
|
+ |
defp render_reason({key, detail}), do: "#{key}:#{detail}"
|
|
729
|
+ |
defp render_reason(reason), do: to_string(reason)
|
|
730
|
+ |
|
|
731
|
+ |
defp criteria(%{criteria: criteria}) when is_list(criteria) do
|
|
732
|
+ |
Enum.map(criteria, fn item ->
|
|
733
|
+ |
%{
|
|
734
|
+ |
"criterion" => to_string(item.criterion),
|
|
735
|
+ |
"evidence" => if(item.evidence == :private, do: nil, else: to_string(item.evidence)),
|
|
736
|
+ |
"visibility" => if(item.evidence == :private, do: "private", else: "public")
|
|
737
|
+ |
}
|
|
738
|
+ |
end)
|
|
739
|
+ |
end
|
|
740
|
+ |
|
|
741
|
+ |
defp criteria(_projection), do: []
|
|
742
|
+ |
|
|
743
|
+ |
defp verifier_id({:accepted, outcome}),
|
|
744
|
+ |
do: outcome.verifier && String.slice(outcome.verifier, 0, 200)
|
|
745
|
+ |
|
|
746
|
+ |
defp verifier_id(_evaluation), do: nil
|
|
747
|
+ |
|
|
748
|
+ |
defp falsifier_of({:accepted, outcome}), do: outcome.falsifier
|
|
749
|
+ |
defp falsifier_of(_evaluation), do: nil
|
|
750
|
+ |
|
|
751
|
+ |
# ── loading ─────────────────────────────────────────────────────────────
|
|
752
|
+ |
|
|
753
|
+ |
defp terminal_attempt(%Assignment{id: id}) do
|
|
754
|
+ |
case Repo.get(Assignment, id) do
|
|
755
|
+ |
%Assignment{terminal_commit: commit} = assignment when is_binary(commit) ->
|
|
756
|
+ |
if Assignment.terminal?(assignment),
|
|
757
|
+ |
do: {:ok, assignment},
|
|
758
|
+ |
else: {:error, :claim_attempt_not_terminal}
|
|
759
|
+ |
|
|
760
|
+ |
%Assignment{} ->
|
|
761
|
+ |
{:error, :claim_attempt_has_no_revision}
|
|
762
|
+ |
|
|
763
|
+ |
nil ->
|
|
764
|
+ |
{:error, :claim_attempt_not_found}
|
|
765
|
+ |
end
|
|
766
|
+ |
end
|
|
767
|
+ |
|
|
768
|
+ |
defp fetch(schema, id) do
|
|
769
|
+ |
case id && Repo.get(schema, id) do
|
|
770
|
+ |
nil -> {:error, :claim_attempt_not_found}
|
|
771
|
+ |
record -> {:ok, record}
|
|
772
|
+ |
end
|
|
773
|
+ |
end
|
|
774
|
+ |
|
|
775
|
+ |
defp fetch_issue(nil), do: {:error, :claim_attempt_not_found}
|
|
776
|
+ |
|
|
777
|
+ |
defp fetch_issue(issue_id) do
|
|
778
|
+ |
case Repo.get(Issue, issue_id) do
|
|
779
|
+ |
nil -> {:error, :claim_attempt_not_found}
|
|
780
|
+ |
%Issue{} = issue -> {:ok, issue}
|
|
781
|
+ |
end
|
|
782
|
+ |
end
|
|
783
|
+ |
end
|