Diffinery

codician/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

296 lines · 12.5 KBRaw History
1import type { Check } from "../repo/collab.js";
2
3/**
4 * One repository's part of the Today view (the morning view of agent work),
5 * read inside its Durable Object in one call: open pull requests with their
6 * review and check state, and what landed or failed since a point in time.
7 * Who the viewer is doesn't matter here; agentic/today.ts sorts the items
8 * into "needs you", "agents at work" and "since yesterday" per person.
9 */
10
11export type ChecksState = "success" | "failure" | "pending" | "none";
12
13export interface TodayPerson {
14 id: string;
15 login: string;
16 kind: string;
17}
18
19export interface TodayPull {
20 number: number;
21 title: string;
22 author: TodayPerson;
23 head: string;
24 base: string;
25 draft: boolean;
26 requestedReviewers: string[];
27 intent: string | null;
28 createdAt: string;
29 updatedAt: string;
30 /** People whose latest vote is an approval, and whether one of them approved the current head. */
31 approvedBy: string[];
32 approvedHead: boolean;
33 /** People (and agents) whose latest vote asks for changes. */
34 changesRequestedBy: string[];
35 /** Ids of everyone whose latest vote is an approval or a change request. */
36 reviewerIds: string[];
37 checks: ChecksState;
38 failingChecks: string[];
39 autoMerge: boolean;
40 /**
41 * An agent's pull request with auto-merge on that still needs a person to approve its latest commit (merge on
42 * green with approvals kept): it isn't landing until someone does, so Today asks them.
43 */
44 awaitingApproval?: boolean;
45 queued: boolean;
46 /**
47 * What the last mergeability check found, while the base branch and the head are still the commits it saw:
48 * conflicts, or a head that is already part of the base. Null when nobody checked since either moved.
49 * Absent in summaries from repositories still running code from before it existed.
50 */
51 mergeState?: MergeState | null;
52}
53
54export type MergeState = "clean" | "conflicting" | "in_base";
55
56/** At most this many pull requests' mergeability is checked after one summary (RepoDO.todaySummary). */
57export const MERGE_CHECKS_PER_SUMMARY = 5;
58
59/**
60 * An agent's pull request that may be waiting for a person's approval, with no mergeability check for its
61 * current base and head: Today checks it after answering, so a later Today knows whether it conflicts.
62 */
63export const needsMergeCheck = (p: TodayPull): boolean =>
64 p.author.kind === "agent" && p.mergeState === null && !p.draft && (!p.autoMerge || !!p.awaitingApproval) && !p.queued && p.checks !== "failure" && !p.changesRequestedBy.length && !p.approvedHead;
65
66export interface TodayMerged {
67 number: number;
68 title: string;
69 author: TodayPerson;
70 mergedAt: string;
71 mergedBy: string | null;
72}
73
74export interface TodayRelease {
75 tag: string;
76 name: string;
77 author: TodayPerson;
78 publishedAt: string;
79 prerelease: boolean;
80}
81
82export interface TodayRun {
83 id: string;
84 runNumber: number;
85 workflow: string;
86 title: string;
87 ref: string;
88 event: string;
89 actor: TodayPerson;
90 conclusion: string;
91 prNumber: number | null;
92 at: string;
93}
94
95/** A run's jobs waiting on an environment's required reviewers. */
96export interface TodayDeployment {
97 runId: string;
98 runNumber: number;
99 workflow: string;
100 title: string;
101 ref: string;
102 environment: string;
103 /** People listed as the environment's required reviewers. */
104 reviewerIds: string[];
105 /**
106 * Account ids that can't approve because the environment prevents self-review: who started the run
107 * and its earlier attempts. When one of them is an agent, its owner can't either (see classify).
108 */
109 selfBlocked: string[];
110 actor: TodayPerson;
111 since: string;
112}
113
114export interface RepoToday {
115 pulls: TodayPull[];
116 merged: TodayMerged[];
117 releases: TodayRelease[];
118 failedRuns: TodayRun[];
119 /** Absent in summaries from before environment reviewers existed. */
120 deployments?: TodayDeployment[];
121}
122
123/** At most this many open pull requests per repository (most recently updated first). */
124export const MAX_PULLS = 50;
125const MAX_RECENT = 20;
126
127const FAILED = new Set(["failure", "timed_out", "startup_failure", "action_required", "cancelled"]);
128
129/** One state for a commit's checks and statuses: any failure wins, then anything unfinished. */
130export function checksState(checks: Pick<Check, "name" | "status" | "conclusion">[]): { state: ChecksState; failing: string[] } {
131 if (!checks.length) return { state: "none", failing: [] };
132 const failing = checks.filter((c) => c.status === "completed" && FAILED.has(c.conclusion ?? "")).map((c) => c.name);
133 if (failing.length) return { state: "failure", failing: [...new Set(failing)] };
134 return { state: checks.some((c) => c.status !== "completed") ? "pending" : "success", failing: [] };
135}
136
137type PullJoin = {
138 number: number;
139 title: string;
140 author_id: string;
141 author_login: string;
142 author_kind: string;
143 created_at: string;
144 updated_at: string;
145 head_ref: string;
146 head_sha: string;
147 base_ref: string;
148 draft: number;
149 requested_reviewers: string;
150 intent: string | null;
151 auto_merge: string | null;
152 queued: number;
153 merge_state: MergeState | null;
154};
155
156type ReviewRow = { number: number; author_id: string; author_login: string; author_kind: string; state: string; commit_id: string; imported: string | null };
157
158/**
159 * `agentApprovalWaived(base)`: whether what Diffinery lands for agents into `base` needs no approving review
160 * (its rule's "as soon as the checks pass"); otherwise an agent's pull request waits for a person even with auto-merge on.
161 */
162export function repoToday(sql: SqlStorage, checksFor: (sha: string) => Check[], since: string, agentApprovalWaived: (base: string) => boolean = () => false): RepoToday {
163 const rows = sql
164 .exec<PullJoin>(
165 `SELECT i.number, i.title, i.author_id, i.author_login, i.author_kind, i.created_at, i.updated_at,
166 p.head_ref, p.head_sha, p.base_ref, p.draft, p.requested_reviewers, p.intent, p.auto_merge,
167 EXISTS (SELECT 1 FROM merge_queue q WHERE q.number = i.number) AS queued,
168 (SELECT m.state FROM pull_merge_state m JOIN refs b ON b.name = 'refs/heads/' || p.base_ref
169 WHERE m.number = i.number AND m.base_sha = b.oid AND m.head_sha = p.head_sha) AS merge_state
170 FROM issues i JOIN pulls p ON p.number = i.number
171 WHERE i.kind = 'pull' AND i.state = 'open'
172 ORDER BY i.updated_at DESC LIMIT ?`,
173 MAX_PULLS,
174 )
175 .toArray();
176 const numbers = rows.map((r) => r.number);
177 const reviews = numbers.length
178 ? sql
179 .exec<ReviewRow>(
180 `SELECT number, author_id, author_login, author_kind, state, commit_id, imported FROM reviews
181 WHERE number IN (${numbers.map(() => "?").join(",")}) ORDER BY submitted_at`,
182 ...numbers,
183 )
184 .toArray()
185 : [];
186 const pulls = rows.map((r): TodayPull => {
187 // Each reviewer's latest vote, as the merge rules count them: comments don't vote, imported reviews are history.
188 const latest = new Map<string, ReviewRow>();
189 for (const rv of reviews) if (rv.number === r.number && rv.state !== "COMMENTED" && !rv.imported && rv.author_kind !== "github") latest.set(rv.author_id, rv);
190 const votes = [...latest.values()];
191 const approvals = votes.filter((v) => v.state === "APPROVED" && v.author_kind === "user");
192 const checks = checksState(checksFor(r.head_sha));
193 const approvedHead = approvals.some((v) => v.commit_id === r.head_sha);
194 return {
195 number: r.number,
196 title: r.title,
197 author: { id: r.author_id, login: r.author_login, kind: r.author_kind },
198 head: r.head_ref,
199 base: r.base_ref,
200 draft: !!r.draft,
201 requestedReviewers: JSON.parse(r.requested_reviewers) as string[],
202 intent: r.intent,
203 createdAt: r.created_at,
204 updatedAt: r.updated_at,
205 approvedBy: approvals.map((v) => v.author_login),
206 approvedHead,
207 changesRequestedBy: votes.filter((v) => v.state === "CHANGES_REQUESTED").map((v) => v.author_login),
208 reviewerIds: votes.filter((v) => v.state === "APPROVED" || v.state === "CHANGES_REQUESTED").map((v) => v.author_id),
209 checks: checks.state,
210 failingChecks: checks.failing,
211 autoMerge: !!r.auto_merge,
212 awaitingApproval: r.author_kind === "agent" && !!r.auto_merge && !approvedHead && !agentApprovalWaived(r.base_ref),
213 queued: !!r.queued,
214 mergeState: r.merge_state,
215 };
216 });
217
218 const merged = sql
219 .exec<{ number: number; title: string; author_id: string; author_login: string; author_kind: string; merged_at: string; merged_by: string | null }>(
220 `SELECT i.number, i.title, i.author_id, i.author_login, i.author_kind, p.merged_at, p.merged_by
221 FROM pulls p JOIN issues i ON i.number = p.number
222 WHERE p.merged = 1 AND p.merged_at >= ? AND p.imported IS NULL
223 ORDER BY p.merged_at DESC LIMIT ?`,
224 since,
225 MAX_RECENT,
226 )
227 .toArray()
228 .map((m) => ({ number: m.number, title: m.title, author: { id: m.author_id, login: m.author_login, kind: m.author_kind }, mergedAt: m.merged_at, mergedBy: m.merged_by }));
229
230 const releases = sql
231 .exec<{ tag_name: string; name: string; author_id: string; author_login: string; author_kind: string; published_at: string; prerelease: number }>(
232 `SELECT tag_name, name, author_id, author_login, author_kind, published_at, prerelease FROM releases
233 WHERE draft = 0 AND published_at >= ? ORDER BY published_at DESC LIMIT ?`,
234 since,
235 MAX_RECENT,
236 )
237 .toArray()
238 .map((x) => ({ tag: x.tag_name, name: x.name || x.tag_name, author: { id: x.author_id, login: x.author_login, kind: x.author_kind }, publishedAt: x.published_at, prerelease: !!x.prerelease }));
239
240 const failedRuns = sql
241 .exec<{ id: string; run_number: number; workflow_name: string; display_title: string; ref: string; event: string; actor: string; conclusion: string; pr_number: number | null; updated_at: string }>(
242 `SELECT id, run_number, workflow_name, display_title, ref, event, actor, conclusion, pr_number, updated_at FROM runs
243 WHERE status = 'completed' AND conclusion IN ('failure', 'timed_out', 'startup_failure') AND updated_at >= ?
244 ORDER BY updated_at DESC LIMIT ?`,
245 since,
246 MAX_RECENT,
247 )
248 .toArray()
249 .map((x) => {
250 let actor: TodayPerson = { id: "", login: "unknown", kind: "system" };
251 try {
252 const a = JSON.parse(x.actor) as Partial<TodayPerson>;
253 actor = { id: a.id ?? "", login: a.login ?? "unknown", kind: a.kind ?? "user" };
254 } catch {
255 // An unreadable actor shows as unknown.
256 }
257 return { id: x.id, runNumber: x.run_number, workflow: x.workflow_name, title: x.display_title, ref: x.ref, event: x.event, actor, conclusion: x.conclusion, prNumber: x.pr_number, at: x.updated_at };
258 });
259
260 const parsePerson = (json: string): TodayPerson & { onBehalfOf?: string } => {
261 try {
262 const a = JSON.parse(json) as Partial<TodayPerson> & { onBehalfOf?: string };
263 return { id: a.id ?? "", login: a.login ?? "unknown", kind: a.kind ?? "user", onBehalfOf: a.onBehalfOf };
264 } catch {
265 return { id: "", login: "unknown", kind: "system" };
266 }
267 };
268 const deployments = sql
269 .exec<{ run_id: string; environment: string; since: string; run_number: number; workflow_name: string; display_title: string; ref: string; actor: string; prior_actors: string | null; reviewers: string | null; prevent_self_review: number }>(
270 `SELECT j.run_id, e.name AS environment, MIN(j.gate_since) AS since, r.run_number, r.workflow_name, r.display_title, r.ref, r.actor, r.prior_actors, e.reviewers, e.prevent_self_review
271 FROM jobs j JOIN runs r ON r.id = j.run_id JOIN environments e ON e.name = j.environment COLLATE NOCASE
272 WHERE j.status = 'approval' AND e.reviewers IS NOT NULL
273 AND NOT EXISTS (SELECT 1 FROM deployment_reviews d WHERE d.run_id = j.run_id AND d.environment = e.name COLLATE NOCASE)
274 GROUP BY j.run_id, e.name ORDER BY since DESC LIMIT ?`,
275 MAX_RECENT,
276 )
277 .toArray()
278 .map((x): TodayDeployment => {
279 const actor = parsePerson(x.actor);
280 const self = x.prevent_self_review ? [actor.id, ...((x.prior_actors ? JSON.parse(x.prior_actors) : []) as string[])].filter((l): l is string => !!l) : [];
281 return {
282 runId: x.run_id,
283 runNumber: x.run_number,
284 workflow: x.workflow_name,
285 title: x.display_title,
286 ref: x.ref,
287 environment: x.environment,
288 reviewerIds: (JSON.parse(x.reviewers!) as Array<{ id: string }>).map((r) => r.id),
289 selfBlocked: [...new Set(self)],
290 actor: { id: actor.id, login: actor.login, kind: actor.kind },
291 since: x.since,
292 };
293 });
294
295 return { pulls, merged, releases, failedRuns, deployments };
296}