Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t.sh
1,585 linesCodeBlameRaw
1import type { ChecksApi } from "./checks";
2import type { CodeownersReport, PullCodeOwners } from "./codeowners";
3import type { G1tEvent } from "./events";
4import type { User, Viewer } from "./identity";
5import type { PullBranchUpdate, RepoPath, UpdateMethod } from "./repos";
6import type { MergeMethod } from "./rules";
7import type { Result } from "./result";
8import type { MergeRules, RulesApi } from "./rules";
9
10/**
11 * The filter on lists of issues and pull requests. An open pull request is
12 * a draft or one ready for review; a closed one was merged or closed
13 * without merging.
14 */
15export type State = "open" | "closed";
16
17/** Why an issue was closed. */
18export type IssueReason = "completed" | "not_planned";
19
20/**
21 * Something that should change in a repository: a bug, a feature, a
22 * question. Opened by a person, an agent or an integration. Pull requests
23 * are made against it; the one that is merged resolves it.
24 *
25 * Issues and pull requests share one sequence of numbers per repository.
26 */
27export type Issue = {
28 id: string;
29 repoId: string;
30 /** Shown as `#12`. */
31 number: number;
32 title: string;
33 /** Markdown. Also what an agent is given to work from. */
34 body: string;
35 labels: string[];
36 state: State;
37 /** Set when closed. */
38 reason: IssueReason | null;
39 /** The number of the pull request whose merge closed this issue. */
40 resolvedBy: number | null;
41 /** The commit pushed to the default branch whose message closed this issue (`Closes #12`), when a commit rather than a pull request closed it. */
42 resolvedCommit: string | null;
43 /** Who pushed that commit, by username. */
44 resolvedPushedBy: string | null;
45 /** Who opened it: a person, an integration, or g1t (`kind` `agent`) for one its agent filed while at work. */
46 author: User;
47 /**
48 * For an issue g1t's agent filed: the person it was working for. They may
49 * manage it as its author could. See `workOwner`.
50 */
51 requestedBy: User | null;
52 /** RFC 3339. */
53 createdAt: string;
54 /** RFC 3339. */
55 updatedAt: string;
56 /** RFC 3339. */
57 closedAt: string | null;
58 /** Pull requests made against this issue, in any state. */
59 pullCount: number;
60 commentCount: number;
61 /** Usernames of the people it is assigned to. */
62 assignees: string[];
63 /** The numbers of the issues that have to be merged before this one is worked on. */
64 blockedBy: number[];
65 /**
66 * The agent working on it now: the one behind its newest pull request
67 * that is still in progress in a fork, such as `g1t`.
68 */
69 agent: string | null;
70 /** The milestone it is in, if any. */
71 milestone?: MilestoneRef | null;
72};
73
74/**
75 * A label of a repository: a name, a color and what it means. Issues and
76 * pull requests carry labels by name; names are lowercase.
77 */
78export type Label = {
79 name: string;
80 /** Six hex digits, without `#`. */
81 color: string;
82 description: string;
83 /** How many issues carry it, open or closed. */
84 issues: number;
85 /** How many pull requests carry it, in any state. */
86 pulls: number;
87};
88
89/** A milestone, as an issue or pull request names it. */
90/** An issue a commit closed, as the commit page lists it. */
91export type ClosedIssue = { number: number; title: string };
92
93export type MilestoneRef = { number: number; title: string };
94
95/**
96 * A goal, with an optional due date, that issues and pull requests are
97 * gathered under. Its progress is how many of them are closed.
98 */
99export type Milestone = {
100 /** Numbered from 1 in each repository, apart from issues. */
101 number: number;
102 title: string;
103 /** Markdown. */
104 description: string;
105 /** `YYYY-MM-DD`. */
106 dueOn: string | null;
107 state: State;
108 /** Open issues and pull requests in it. */
109 openItems: number;
110 /** Closed issues, and merged or closed pull requests, in it. */
111 closedItems: number;
112 createdAt: string;
113 updatedAt: string;
114 closedAt: string | null;
115};
116
117/** One milestone and what is in it, newest first. */
118export type MilestoneDetail = { milestone: Milestone; issues: Issue[]; pulls: Pull[] };
119
120/** How `setLabels` changes an item's labels. */
121export type LabelChange = "set" | "add" | "remove";
122
123/**
124 * Whose an issue or a pull request is to answer for: whoever asked g1t for
125 * it, or its author. They may change, close and steer it, are never asked to
126 * review it and cannot approve it, and see it as theirs. Mirrors
127 * `Pull::owner` in the Rust contracts.
128 */
129export function workOwner(item: Pick<Pull, "author" | "requestedBy">): User {
130 return item.requestedBy ?? item.author;
131}
132
133/** `draft` is still being worked on; `open` is ready for review. */
134export type PullStatus = "draft" | "open" | "merged" | "closed";
135
136/** Where the agent runs: on g1t's sandboxes, or in someone's own session. */
137export type Runtime = "hosted" | "external";
138
139/**
140 * A proposed change. It is made either in a fork created for it, which is
141 * how agents work, on a branch pushed to the repository itself, or on a
142 * branch of someone's fork of it (`headRepo`).
143 */
144export type Pull = {
145 id: string;
146 repoId: string;
147 /** Shown as `#12`. */
148 number: number;
149 /** The number of the issue this is for, if any. */
150 issue: number | null;
151 title: string;
152 /** Markdown: what changed and why. Set when marked ready. */
153 body: string | null;
154 /** A label for the agent doing the work, e.g. `claude-code`. */
155 agent: string;
156 runtime: Runtime;
157 status: PullStatus;
158 /** The fork holding the change, unless it is on a branch. */
159 fork: RepoPath | null;
160 /** The fork's repository id. */
161 forkRepoId: string | null;
162 /**
163 * The branch holding the change, unless it is in a working copy (`fork`):
164 * of `headRepo` when set, else of the repository.
165 */
166 branch: string | null;
167 /**
168 * A fork of the repository (its `parentId`, directly or through other
169 * forks) whose `branch` holds the change, when it was opened from one.
170 * Null for a branch of the repository itself and for a working copy.
171 */
172 headRepo?: RepoPath | null;
173 /** `headRepo`'s id. */
174 headRepoId?: string | null;
175 headCommit: string | null;
176 /**
177 * For a merged pull request, what the branch pointed to before the merge.
178 * Comparing against it shows what the pull request changed.
179 */
180 mergeBase: string | null;
181 /** Username of whoever merged it. */
182 mergedBy: string | null;
183 /** RFC 3339. */
184 mergedAt: string | null;
185 /** How it merged, once merged. */
186 mergeMethod?: MergeMethod | null;
187 /** Where its merge left the base: the merge or squash commit, the last rebased one, or its head. */
188 mergeCommit?: string | null;
189 /** Merge it on its own once everything it needs is met, as a person asked. */
190 autoMerge?: AutoMerge | null;
191 /**
192 * The issues it closes when it merges into the default branch: its issue and
193 * those its title and description name after a closing keyword (`Fixes #12`).
194 */
195 closes?: number[];
196 /** Its branch was deleted after it merged or closed, and can be restored at `headCommit`. */
197 headBranchDeleted?: boolean;
198 /**
199 * Set on a pull request closed because another one for the same issue was
200 * merged: that one's number.
201 */
202 supersededBy: number | null;
203 /**
204 * Where the latest run of the issue's acceptance checks stands, if there
205 * has been one against the current head.
206 */
207 checkStatus: CheckStatus | null;
208 /** The files it changes, as of its latest push. */
209 files: ChangedFile[];
210 /** Usernames of the people it is assigned to. */
211 assignees: string[];
212 /**
213 * Those whose review was asked for: usernames, and `g1t` when a g1t
214 * agent was asked.
215 */
216 reviewers: string[];
217 /**
218 * Teams whose review was asked for, as `workspace/slug`. A team stays here
219 * after review assignment picks people from it, who are in `reviewers`.
220 */
221 teamReviewers?: string[];
222 /** The labels it carries, by name. */
223 labels?: string[];
224 /** The milestone it is in, if any. */
225 milestone?: MilestoneRef | null;
226 /**
227 * The branch it merges into. Lists and `getPull` name it; null only in
228 * what services pass between themselves, for the default branch.
229 */
230 base?: string | null;
231 /** Who opened it: a person, or g1t (`kind` `agent`, username `g1t`) for a change g1t made. */
232 author: User;
233 /**
234 * For a change g1t made: the person who asked for it, by assigning an issue
235 * or handing g1t the work. They answer for it as its author would. See
236 * `workOwner`.
237 */
238 requestedBy: User | null;
239 /** RFC 3339. */
240 createdAt: string;
241 /** RFC 3339. */
242 updatedAt: string;
243 /** Who last moved its head (a user id), and when; absent until a push after rulesets arrived. */
244 headPushedBy?: string;
245 headPushedAt?: string;
246};
247
248/** One file a pull request changes, and by how much. */
249export type ChangedFile = { path: string; additions: number; deletions: number };
250
251/**
252 * Another pull request in progress that changes some of the same files. Two
253 * for the same issue are alternatives; two for different issues are heading
254 * for a conflict.
255 */
256export type Overlap = {
257 number: number;
258 title: string;
259 /** The number of the issue the other pull request is for. */
260 issue: number | null;
261 /** The files both change. */
262 paths: string[];
263};
264
265/**
266 * On a pull request, `failed` means the merge queue took it out, until its
267 * head moves. The other states are from runs of commands written on issues,
268 * which g1t no longer runs.
269 */
270export type CheckStatus = "queued" | "running" | "passed" | "failed" | "errored";
271
272/** How one command went, in a run recorded before checks were workflows. */
273export type CheckResult = {
274 command: string;
275 passed: boolean;
276 /** Null when the command was stopped for taking too long. */
277 exitCode: number | null;
278 /** What the command printed; the end of it, when there was a lot. */
279 output: string;
280 durationMs: number;
281};
282
283/**
284 * A record against a pull request's head: the merge queue taking it out,
285 * with why, or an earlier run of commands written on its issue.
286 */
287export type CheckRun = {
288 id: string;
289 /** The commit that was checked. */
290 headCommit: string;
291 status: CheckStatus;
292 results: CheckResult[];
293 /** Why the checks could not be run, when `status` is `errored`. */
294 error: string | null;
295 /** RFC 3339. */
296 createdAt: string;
297 /** RFC 3339. */
298 finishedAt: string | null;
299};
300
301/** A reviewer's decision on a pull request. */
302export type Verdict = "approve" | "request_changes";
303
304/**
305 * A comment on an issue or a pull request. On a pull request it can sit on
306 * one line of the change, and it can carry a reviewer's verdict.
307 */
308export type Comment = {
309 id: string;
310 /**
311 * Something a person or an agent wrote, or something that happened: an
312 * assignment, a review asked for, a close.
313 */
314 kind: "comment" | "event";
315 author: User;
316 /**
317 * Markdown. For an event, what its author did, as the rest of a sentence
318 * that starts with their name: "assigned ana".
319 */
320 body: string;
321 /** The file commented on, for a comment on a line. */
322 path: string | null;
323 /** The line of that file, as numbered after the change. */
324 line: number | null;
325 verdict: Verdict | null;
326 /** RFC 3339. */
327 createdAt: string;
328 /** When its text was last edited, RFC 3339; absent if it never was. */
329 editedAt?: string;
330 /**
331 * Set when one of the workspace's agents wrote it, as itself. `author`
332 * is then the agent (its id, its handle as `username`, kind `agent`):
333 * show it by `agent.displayName` with an Agent badge, never as a person.
334 */
335 agent?: AgentRef;
336 /**
337 * Who the agent acted for, whose access capped it. Set with `agent`, and
338 * when g1t (`author`) wrote it, or did what an event notes, in a session
339 * someone started: show "on behalf of @them".
340 */
341 actingFor?: User;
342 /**
343 * An agent's review. Its `verdict` (none for a review that only
344 * comments) is shown but advisory: it never counts toward required
345 * approvals or code owners, and never blocks a merge.
346 */
347 advisory?: boolean;
348 /** How people (and g1t) reacted to it. Absent when nobody has. */
349 reactions?: ReactionGroup[];
350};
351
352/** An emoji reaction: the same eight as everywhere else people react to code. */
353export type ReactionContent = "+1" | "-1" | "laugh" | "hooray" | "confused" | "heart" | "rocket" | "eyes";
354
355/** The reactions, in the order they are offered, with the emoji each shows. */
356export const REACTIONS: { content: ReactionContent; emoji: string; label: string }[] = [
357 { content: "+1", emoji: "👍", label: "Thumbs up" },
358 { content: "-1", emoji: "👎", label: "Thumbs down" },
359 { content: "laugh", emoji: "😄", label: "Laugh" },
360 { content: "hooray", emoji: "🎉", label: "Hooray" },
361 { content: "confused", emoji: "😕", label: "Confused" },
362 { content: "heart", emoji: "❤️", label: "Heart" },
363 { content: "rocket", emoji: "🚀", label: "Rocket" },
364 { content: "eyes", emoji: "👀", label: "Eyes" },
365];
366
367/** Everyone who reacted one way to the same thing, first to react first; `g1t` for g1t. */
368export type ReactionGroup = {
369 content: ReactionContent;
370 count: number;
371 users: string[];
372};
373
374/**
375 * One of a workspace's own agents, as a comment or review it wrote before
376 * g1t's agents were removed shows it: Margo (@margo), with the face drawn
377 * from `avatarSeed`. Kept so those comments still render.
378 */
379export type AgentRef = {
380 id: string;
381 handle: string;
382 displayName: string;
383 avatarSeed: string;
384};
385
386export type NewComment = {
387 /** May be empty when approving. */
388 body: string;
389 path?: string;
390 line?: number;
391 verdict?: Verdict;
392};
393
394export type CheckReport = { results?: CheckResult[]; error?: string; skip?: boolean };
395
396export type SessionEntryKind = "prompt" | "message" | "tool_call" | "tool_result" | "note" | "skill";
397
398/** One step of an agent's session: the "why" behind a pull request's commits. */
399export type SessionEntry = {
400 seq: number;
401 kind: SessionEntryKind;
402 text: string;
403 /** For tool calls and results. */
404 tool: string | null;
405 /** The fork's head commit when this entry was recorded, if known. */
406 commit: string | null;
407 /** RFC 3339. */
408 at: string;
409};
410
411export type NewSessionEntry = Pick<SessionEntry, "kind" | "text"> &
412 Partial<Pick<SessionEntry, "tool" | "commit">>;
413
414export type IssueDetail = {
415 issue: Issue;
416 /** Every pull request made against it, oldest first. */
417 pulls: Pull[];
418 comments: Comment[];
419 /** How people (and g1t) reacted to its description. */
420 reactions: ReactionGroup[];
421};
422
423export type PullDetail = {
424 pull: Pull;
425 /** The issue it is for, if any. */
426 issue: Issue | null;
427 comments: Comment[];
428 /** How people (and g1t) reacted to its description. */
429 reactions: ReactionGroup[];
430 /** The latest record against its head: the merge queue taking it out. */
431 checks: CheckRun | null;
432 /** Other pull requests in progress that change the same files. */
433 overlaps: Overlap[];
434 /**
435 * Whether the branch it would merge into has moved on without it, so that
436 * it has to catch up before it can merge.
437 */
438 behind: boolean;
439 /**
440 * Where it stands on its way to being merged, for a pull request g1t is
441 * seeing through. Null on anyone else's.
442 */
443 lifecycle: Lifecycle | null;
444 /**
445 * A merge was asked for while it was behind: g1t is bringing it up to
446 * date and will then land it.
447 */
448 landing: boolean;
449 /** Why g1t stopped working on it, if it did. */
450 stalled: string | null;
451 /** What workflow runs said about its head commit, one per workflow. */
452 statuses?: CommitStatus[];
453 /**
454 * Whether it merges cleanly into the branch it targets, worked out ahead
455 * of time whenever either side moves.
456 */
457 mergeable?: Mergeable;
458 /** When `mergeable` is `conflicting`: the files that conflict. */
459 conflicts?: string[];
460 /** Earlier records like `checks`, newest first, without their output. */
461 earlierChecks?: CheckRun[];
462 /**
463 * The checks the default branch's protection requires, each as it stands
464 * on the head commit. Empty when none are required.
465 */
466 requiredChecks?: RequiredCheck[];
467 /**
468 * Who owns the files it changes (the CODEOWNERS file of the branch it
469 * merges into) and whose approval is still needed. Absent without one.
470 */
471 codeOwners?: PullCodeOwners | null;
472 /**
473 * The rules of the branch it merges into it does not meet yet, for whoever
474 * is looking: what refuses the merge, what they may bypass, and what rulesets
475 * in evaluate would refuse. Absent once it is closed or merged.
476 */
477 rules?: MergeRules | null;
478};
479
480/** Where a required check stands on a commit; `expected` when nothing has reported it yet. */
481export type RequiredState = "success" | "failure" | "pending" | "expected";
482
483/** One check a branch's protection requires, as it stands on a commit. */
484export type RequiredCheck = {
485 /** A workflow's name, such as `CI`, or another status's context. */
486 name: string;
487 state: RequiredState;
488 description: string | null;
489 /** Where to see more: the workflow run, for one a workflow reported. */
490 targetUrl: string | null;
491};
492
493/** A check name reported on a repository's commits lately, for choosing required checks. */
494export type SeenCheck = {
495 name: string;
496 /** The events it was reported for, such as `pull_request`; empty for a status that names none. */
497 events: string[];
498 /** RFC 3339. */
499 lastSeen: string;
500};
501
502/**
503 * A status context's check name: `CI / pull_request` is the `CI` check,
504 * reported for a `pull_request` event. A context that does not end in an
505 * event, such as `g1t / deploy`, is its own name. As the work service reads it.
506 */
507export function checkName(context: string): string {
508 const at = context.lastIndexOf(" / ");
509 if (at <= 0) return context;
510 return STATUS_EVENTS.has(context.slice(at + 3)) ? context.slice(0, at) : context;
511}
512
513const STATUS_EVENTS = new Set([
514 "push",
515 "pull_request",
516 "pull_request_target",
517 "pull_request_review",
518 "merge_group",
519 "workflow_dispatch",
520 "workflow_run",
521 "workflow_call",
522 "schedule",
523 "release",
524 "issues",
525 "issue_comment",
526 "repository_dispatch",
527]);
528
529/**
530 * Whether a pull request merges cleanly into the branch it targets:
531 * `unknown` when it was never worked out or could not be, `checking` while
532 * a probe merges the two.
533 */
534export type Mergeable = "clean" | "conflicting" | "unknown" | "checking";
535
536/** What a sandbox needs to find out whether a pull request merges cleanly. */
537export type MergecheckJob = {
538 pullId: string;
539 /** Lets the sandbox, and nothing else, report this probe. */
540 token: string;
541 repo: RepoPath;
542 number: number;
543 defaultBranch: string;
544 /** The default branch's commit to merge into. */
545 base: string;
546 /** The repository holding the change: its fork, or the repository. */
547 source: RepoPath;
548 /** The branch of `source` holding it. */
549 branch: string;
550 /** The change's commit. */
551 head: string;
552 /** Who the pull request is for (`workOwner`: whoever asked g1t for it, or its author), and so can read its source. */
553 author: User;
554};
555
556/** What a workflow run (or another tool) says about a commit. */
557export type CommitStatus = {
558 /** What reported it, such as `CI / push`. */
559 context: string;
560 state: "pending" | "success" | "failure" | "error";
561 description: string | null;
562 targetUrl: string | null;
563 updatedAt: string;
564 /** The integration that reported it: `actions`, `deployments`, `security`, `g1t` or `api`. */
565 source?: string;
566 /** Set on the status a check run stands as: the check run's id. */
567 checkRunId?: string;
568};
569
570/**
571 * A step on the way from an assigned issue to a pull request that is ready
572 * to merge. g1t takes each one without being asked: `working` (the agent is
573 * making the change), `checking`, `reviewing`, `revising` (the agent is
574 * addressing failed checks or a review), `catching_up` (merging in the
575 * branch it would land on), `answering` (woken to answer another agent),
576 * then `ready` for a person to merge. `needs_you`
577 * means g1t has stopped and a person decides what happens next.
578 */
579export type Stage =
580 | "working"
581 | "checking"
582 | "reviewing"
583 | "revising"
584 | "catching_up"
585 | "answering"
586 | "queued"
587 | "ready"
588 | "needs_you";
589
590export type Lifecycle = {
591 stage: Stage;
592 /** One sentence saying what is happening, or why it stopped. */
593 detail: string;
594 /** How many times the agent has been sent back to revise it. */
595 revisions: number;
596};
597
598/**
599 * How a repository wants its pull requests handled. A repository that has
600 * changed nothing has the defaults.
601 */
602export type RepoSettings = {
603 /**
604 * Land a g1t agent's pull request without a person once it is ready:
605 * checks passed and approved as the settings below require.
606 */
607 autoMerge: boolean;
608 /**
609 * The checks that must pass on a pull request's head before it may merge
610 * into the default branch, by name: a workflow's name (`CI`) or another
611 * status's context (`g1t / deploy`). The same for people and agents, and
612 * for the merge queue.
613 */
614 requiredChecks: string[];
615 /**
616 * Refuse to merge a pull request that does not contain the default
617 * branch's latest commits, so that what merges is what was checked. When
618 * off, merging one that is behind brings it up to date first.
619 */
620 requireUpToDate: boolean;
621 /**
622 * How many approving reviews a pull request needs before it may merge. A
623 * reviewer who has since asked for changes blocks it.
624 */
625 requiredApprovals: number;
626 /** Whether a g1t agent's approval counts towards `requiredApprovals`. */
627 countAgentApprovals: boolean;
628 /** Whether someone who may merge can bypass required checks that have not passed. */
629 allowIgnoringChecks: boolean;
630 /**
631 * Merge through a queue: pull requests are tested together with those
632 * ahead of them, and only a combination that passed reaches the default
633 * branch.
634 */
635 mergeQueue: boolean;
636 /**
637 * Refuse to merge until the code owners of every file it changes have
638 * approved, as many as each section asks. People only; `g1t` only where
639 * the file names `@g1t`.
640 */
641 requireCodeOwnerReview?: boolean;
642 /**
643 * Whether issues can be opened in it: null for the default, which is on
644 * for a repository and off for a fork of one. Existing issues stay
645 * readable either way.
646 */
647 hasIssues?: boolean | null;
648 /** Whether issues can be opened in it now: `hasIssues`, or its default. */
649 issuesEnabled?: boolean;
650 /** Which merge methods pull requests may use (at least one); rules can narrow them per branch. */
651 allowMergeCommit: boolean;
652 allowSquashMerge: boolean;
653 allowRebaseMerge: boolean;
654 allowFastForward: boolean;
655 /** The method offered first, and used when none is named. */
656 defaultMergeMethod: MergeMethod;
657 /** What merge and squash commits say unless their merger writes it. */
658 mergeCommitMessage: CommitMessageDefault;
659 squashCommitMessage: CommitMessageDefault;
660 /**
661 * For `custom`: templates whose `{title}`, `{number}`, `{body}`, `{branch}`,
662 * `{head_repo}`, `{base}`, `{author}`, `{commits}`, `{issues}` and
663 * `{co_authors}` are filled in.
664 */
665 mergeCommitTemplate: string | null;
666 squashCommitTemplate: string | null;
667 /** Offer to bring a pull request up to date whenever its base has new commits. */
668 suggestUpdatingBranches: boolean;
669 /** Let people turn on auto-merge for a pull request. */
670 allowAutoMerge: boolean;
671 /** Delete a pull request's branch once it merges (restorable from it). */
672 deleteBranchOnMerge: boolean;
673 /** Commits made on g1t.sh carry a `Signed-off-by:` trailer for whoever made them. */
674 requireWebCommitSignOff: boolean;
675 /** Let anyone who can read it comment on its commits. */
676 allowCommitComments: boolean;
677 /** Close the issues a pull request names when it merges into the default branch. */
678 autoCloseIssues: boolean;
679 /** Username of the member who last changed the settings, if anyone has. */
680 updatedBy: string | null;
681 /** RFC 3339. */
682 updatedAt: string | null;
683};
684
685/** What a merge or squash commit's message says by default (`pull_title_and_commits` is squash only). */
686export type CommitMessageDefault = "default" | "pull_title" | "pull_title_and_description" | "pull_title_and_commits" | "custom";
687
688/** A person's request that a pull request merge on its own once it may. */
689export type AutoMerge = {
690 method: MergeMethod;
691 commitTitle: string | null;
692 commitMessage: string | null;
693 /** Username of who asked. */
694 enabledBy: string;
695 /** RFC 3339. */
696 enabledAt: string;
697};
698
699/** A comment on a commit, or on one line (`path`, `line`) of a file it changed. */
700export type CommitComment = {
701 id: string;
702 repoId: string;
703 /** The commit's full hash. */
704 commit: string;
705 path: string | null;
706 line: number | null;
707 /** Username. */
708 author: string;
709 /** Markdown. */
710 body: string;
711 /** RFC 3339. */
712 createdAt: string;
713 updatedAt: string | null;
714};
715
716/** How a pull request may merge now, for whoever is looking (`mergeChoices`). */
717export type MergeChoices = {
718 /** The methods it may merge by: the repository's, as the rules for its base allow. In the order offered. */
719 methods: MergeMethod[];
720 /** The one offered first: the repository's default when it may be used, else the first of `methods`. */
721 defaultMethod: MergeMethod | null;
722 /** Each of `methods`, with its default commit message and whether it can be used now. */
723 options: MergeChoice[];
724 /** Its base has commits it does not have. */
725 behind: boolean;
726 /** It contains its base's head, so it can fast-forward as it is. */
727 canFastForward: boolean;
728 /** Offer to bring it up to date: it is behind, and the repository suggests (or requires) it. */
729 suggestUpdate: boolean;
730 /** The repository requires it to be up to date before it merges. */
731 requireUpToDate: boolean;
732 /** It merges through the merge queue, which lands it as tested whatever method is chosen. */
733 mergeQueue: boolean;
734 /** The viewer may turn on auto-merge for it. */
735 autoMergeAllowed: boolean;
736 /** Whether "delete the branch" starts ticked. */
737 deleteBranchDefault: boolean;
738 /** Its branch can be deleted once it merges (never a working copy's). */
739 canDeleteBranch: boolean;
740 /** Commits made merging it carry a `Signed-off-by:` trailer for the merger. */
741 signOff: boolean;
742};
743
744/** One way a pull request may merge. */
745export type MergeChoice = {
746 method: MergeMethod;
747 /** For `merge` and `squash`: the commit's default title and the rest of its message. */
748 commitTitle: string | null;
749 commitMessage: string | null;
750 /** Why it cannot merge this way now, if it cannot. */
751 unavailable: string | null;
752};
753
754/** How a pull request merges, as the merger chose. */
755export type MergeOptions = {
756 keepIssueOpen?: boolean;
757 /** Merge although required checks have not passed, where the rule requiring them allows it. */
758 ignoreChecks?: boolean;
759 /** Merge past rules a ruleset lets the actor bypass. Recorded as a bypass. */
760 bypassRules?: boolean;
761 /** The repository's default when absent. */
762 mergeMethod?: MergeMethod;
763 /** For `merge` and `squash`: the commit's title and the rest of its message. */
764 commitTitle?: string;
765 commitMessage?: string;
766 /** Refuse if its head is no longer this commit. */
767 expectedHead?: string;
768 /** Delete its branch once merged; the repository's setting when absent. */
769 deleteBranch?: boolean;
770};
771
772/** The settings a member can change. */
773export type RepoSettingsInput = Omit<RepoSettings, "updatedBy" | "updatedAt" | "issuesEnabled">;
774
775/**
776 * What the runner is to do next for a pull request: always nothing now,
777 * since no step of a pull request is carried out in a sandbox any more.
778 */
779export type Advance = { action: "none" };
780
781export type OpenIssueInput = {
782 title: string;
783 body: string;
784 labels?: string[];
785 /** Deprecated: commands, added to the body under "Definition of done". */
786 checks?: string[];
787 /** The number of the milestone to put it in. Needs the Triage role. */
788 milestone?: number;
789};
790
791export type UpdateIssueInput = {
792 title?: string;
793 body?: string;
794 labels?: string[];
795 /** Usernames of the people it is assigned to; replaces the whole set. */
796 assignees?: string[];
797 /** The number of the milestone to put it in; 0 takes it out. */
798 milestone?: number;
799 /** The issues of the same repository that have to land first; replaces the whole set. */
800 blocked_by?: number[];
801};
802
803export type OpenPullInput = {
804 /** The number of the issue this is for. */
805 issue?: number;
806 /** Defaults to the issue's title; required without an issue. */
807 title?: string;
808 /** What changed and why. Usually set later, when a draft is marked ready. */
809 body?: string;
810 /**
811 * A branch of the repository that already holds the change. The pull
812 * request is then ready for review at once and has no fork.
813 */
814 branch?: string;
815 /**
816 * A fork of the repository (directly or through other forks) whose
817 * `branch` holds the change. Needs `branch`.
818 */
819 headRepo?: RepoPath;
820 agent: string;
821 runtime: Runtime;
822 /** The branch to merge into: the default branch when left out. */
823 base?: string;
824 /**
825 * Opened from a branch as a draft, still being worked on: it can't merge
826 * until it is marked ready.
827 */
828 draft?: boolean;
829};
830
831/** One repository's pull requests from `pullsForRepos`, newest first. */
832export type RepoPulls = {
833 repoId: string;
834 /** Draft and open. */
835 open: Pull[];
836 /** Merged and closed. */
837 closed: Pull[];
838};
839
840/** Issues, pull requests, comments and sessions. */
841export interface WorkApi extends RulesApi, ChecksApi, CoordinationApi {
842 openIssue(actor: User, repo: RepoPath, input: OpenIssueInput): Promise<Result<Issue>>;
843 /** Newest first. */
844 listIssues(
845 repo: RepoPath,
846 viewer: Viewer,
847 filter?: { state?: State; label?: string; milestone?: number },
848 ): Promise<Result<Issue[]>>;
849 getIssue(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<IssueDetail>>;
850 /** The issues of the repository, of those the viewer may read, that this commit's message closed when it was pushed to the default branch. */
851 closedByCommit(repo: RepoPath, commit: string, viewer: Viewer): Promise<Result<ClosedIssue[]>>;
852 /** The author or a member of the workspace may. */
853 updateIssue(actor: User, repo: RepoPath, number: number, input: UpdateIssueInput): Promise<Result<Issue>>;
854 closeIssue(actor: User, repo: RepoPath, number: number, reason?: IssueReason): Promise<Result<Issue>>;
855 reopenIssue(actor: User, repo: RepoPath, number: number): Promise<Result<Issue>>;
856 /** A repository's labels, by name, each with how many issues and pull requests carry it. */
857 listLabels(repo: RepoPath, viewer: Viewer): Promise<Result<Label[]>>;
858 /**
859 * Creates a label, or with `name` changes one; renaming it renames it on
860 * everything that carries it. Needs the Triage role.
861 */
862 saveLabel(
863 actor: User,
864 repo: RepoPath,
865 label: { name?: string; newName?: string; color?: string; description?: string },
866 ): Promise<Result<Label>>;
867 /** Removes a label from the repository and everything carrying it. */
868 deleteLabel(actor: User, repo: RepoPath, name: string): Promise<Result<boolean>>;
869 /** Adds the default labels the repository does not have yet; returns them all. */
870 addDefaultLabels(actor: User, repo: RepoPath): Promise<Result<Label[]>>;
871 /**
872 * The labels of an issue or a pull request, replaced, added to or taken
873 * from. Labels the repository lacks are created for someone with the
874 * Triage role. Returns its labels now.
875 */
876 setLabels(
877 actor: User,
878 repo: RepoPath,
879 number: number,
880 labels: string[],
881 change?: LabelChange,
882 ): Promise<Result<string[]>>;
883 /** Open ones by due date, then closed ones; both unless `state` says. */
884 listMilestones(repo: RepoPath, viewer: Viewer, state?: State): Promise<Result<Milestone[]>>;
885 getMilestone(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<MilestoneDetail>>;
886 /** Creates a milestone, or with `number` changes the fields given. Needs the Triage role. */
887 saveMilestone(
888 actor: User,
889 repo: RepoPath,
890 milestone: { number?: number; title?: string; description?: string; dueOn?: string; state?: State },
891 ): Promise<Result<Milestone>>;
892 deleteMilestone(actor: User, repo: RepoPath, number: number): Promise<Result<boolean>>;
893 /** How many issues and pull requests are open. */
894 counts(repo: RepoPath, viewer: Viewer): Promise<Result<{ issues: number; pulls: number }>>;
895
896 /**
897 * On an issue or a pull request. On a pull request it may name a line of
898 * the change and carry a verdict; nobody can give a verdict on their own.
899 */
900 addComment(actor: User, repo: RepoPath, number: number, comment: NewComment): Promise<Result<Comment>>;
901 /**
902 * Changes a comment's text. Its author may, and so may anyone with the
903 * Maintain role or higher; notes of what happened cannot be changed.
904 */
905 editComment(actor: User, repo: RepoPath, commentId: string, body: string): Promise<Result<Comment>>;
906 /**
907 * Deletes a comment: its author, or anyone with the Maintain role or
908 * higher. A review that gave a verdict cannot be deleted, only edited.
909 */
910 deleteComment(actor: User, repo: RepoPath, commentId: string): Promise<Result<boolean>>;
911 /**
912 * Reacts to comment `commentId` on issue or pull request `number`, or to
913 * its description without one; with `remove`, takes it back. Anyone who
914 * can read the repository may. What it has now.
915 */
916 react(
917 actor: User,
918 repo: RepoPath,
919 number: number,
920 reaction: { commentId?: string; content: ReactionContent; remove?: boolean },
921 ): Promise<Result<ReactionGroup[]>>;
922
923 /**
924 * What a sandbox says about a run from before checks were workflows: so
925 * one still finishing is recorded.
926 */
927 reportChecks(runId: string, token: string, report: CheckReport): Promise<Result<CheckRun>>;
928 /**
929 * Claims the probe of whether a pull request merges cleanly that a
930 * `pull.mergecheck` event asked for. Refused when it is no longer wanted
931 * or the repository has as many running as it may. For the runner service.
932 */
933 startMergecheck(pullId: string): Promise<Result<MergecheckJob>>;
934 /** Records that a probe could not be carried out. For the runner service. */
935 failMergecheck(pullId: string, token: string, error: string): Promise<Result<Mergeable>>;
936
937 /**
938 * Works out the next step for a pull request g1t is seeing through and,
939 * if there is one to take now, claims it, so that it is taken once
940 * however often this is called. For the runner service.
941 */
942 advance(pullId: string): Promise<Advance>;
943 /**
944 * Ids of the open pull requests to move along: those g1t merges by itself
945 * or someone set to auto-merge; with `repoId`, also every open one whose
946 * lifecycle is remembered, to bring it up to date after a merge there.
947 */
948 managedPulls(repoId?: string): Promise<string[]>;
949
950 /** A repository's merge queue: what is in it, in order, and what recently left. */
951 queue(repo: RepoPath, viewer: Viewer): Promise<Result<QueueView>>;
952 /**
953 * The next batch of combined states to test for a repository, one per
954 * entry; empty while a batch is being tested or nothing waits.
955 */
956 queueBuild(repoId: string): Promise<QueueJob[]>;
957 /** Reports that a combined state could not be built or checked. */
958 failQueue(entryId: string, token: string, error: string): Promise<Result<QueueState>>;
959 /** Takes a pull request out of the merge queue. Members only. */
960 removeFromQueue(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
961
962 getSettings(repo: RepoPath, viewer: Viewer): Promise<Result<RepoSettings>>;
963 /** The check names reported on the repository's commits in the last 30 days, most recent first. */
964 seenChecks(repo: RepoPath, viewer: Viewer): Promise<Result<SeenCheck[]>>;
965 /** Members of the repository's workspace only. */
966 updateSettings(actor: User, repo: RepoPath, settings: RepoSettingsInput): Promise<Result<RepoSettings>>;
967 /** The CODEOWNERS file at a branch (the default when left out), checked. Needs Read. */
968 codeownersErrors(repo: RepoPath, viewer: Viewer, ref?: string | null): Promise<Result<CodeownersReport>>;
969
970 /**
971 * Opens a pull request: a draft with a fork to push to, or, given a
972 * branch, one ready for review.
973 */
974 openPull(actor: User, repo: RepoPath, input: OpenPullInput): Promise<Result<Pull>>;
975 /** Newest first. */
976 listPulls(
977 repo: RepoPath,
978 viewer: Viewer,
979 state?: State,
980 filter?: { label?: string; milestone?: number; base?: string },
981 ): Promise<Result<Pull[]>>;
982 /**
983 * Delivery for up to 100 repositories over UTC days `from` through
984 * `until`, over their whole history. Repositories the viewer cannot read,
985 * and forks, are left out of `projects`.
986 */
987 deliveryInsights(repoIds: string[], viewer: Viewer, from: string, until: string): Promise<DeliveryInsights>;
988 /**
989 * The newest `limit` open and closed pull requests of each repository,
990 * in one call. Repositories the viewer cannot read, and forks, are left
991 * out: ask those with `listPulls`.
992 */
993 pullsForRepos(repoIds: string[], viewer: Viewer, limit: number): Promise<RepoPulls[]>;
994 getPull(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<PullDetail>>;
995 /**
996 * Changes who a pull request is assigned to and whose review is asked
997 * for; each list given replaces the whole set. Asking for `g1t`'s
998 * review does not by itself start one: the runner's `review` does.
999 */
1000 updatePull(
1001 actor: User,
1002 repo: RepoPath,
1003 number: number,
1004 changes: {
1005 assignees?: string[];
1006 reviewers?: string[];
1007 labels?: string[];
1008 /** 0 takes it out of its milestone. */
1009 milestone?: number;
1010 /** The branch it merges into. Needs the Write role. */
1011 base?: string;
1012 },
1013 ): Promise<Result<Pull>>;
1014 /**
1015 * Brings a pull request up to date with the default branch in seconds,
1016 * without a sandbox, when the two changed different files: the merge
1017 * commit is pushed to its branch as `actor`, who must be whoever opened
1018 * it (for a fork) or a member (for a branch). Otherwise `needs_agent`, and
1019 * nothing is pushed: the runner's `update` is the way on.
1020 */
1021 catchUpPull(
1022 actor: User,
1023 repo: RepoPath,
1024 number: number,
1025 options?: { updateMethod?: UpdateMethod; expectedHead?: string },
1026 ): Promise<Result<PullBranchUpdate>>;
1027 /**
1028 * Turns a pull request's auto-merge on (with how to merge) or off. On needs
1029 * Write, since it merges as whoever turned it on; its author may also turn
1030 * it off. Refused where the repository does not allow auto-merge or the
1031 * method. One that may merge already merges at once.
1032 */
1033 setAutoMerge(
1034 actor: User,
1035 repo: RepoPath,
1036 number: number,
1037 enabled: boolean,
1038 options?: { mergeMethod?: MergeMethod; commitTitle?: string; commitMessage?: string },
1039 ): Promise<Result<Pull>>;
1040 /** How a pull request may merge now: methods, default messages, auto-merge and branch deletion. */
1041 mergeChoices(viewer: Viewer, repo: RepoPath, number: number): Promise<Result<MergeChoices>>;
1042 /** Deletes a merged or closed pull request's branch (Push on where it is). */
1043 deleteHeadBranch(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
1044 /** Makes a deleted pull request branch again at its head. */
1045 restoreHeadBranch(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
1046 /** A commit's comments, oldest first; with no commit, the repository's, newest first, 30 a page. */
1047 commitComments(viewer: Viewer, repo: RepoPath, commit?: string | null, page?: number): Promise<Result<CommitComment[]>>;
1048 /** Comments on a commit, or on one line of a file it changed. Needs Read, where commit comments are on. */
1049 addCommitComment(
1050 actor: User,
1051 repo: RepoPath,
1052 commit: string,
1053 body: string,
1054 at?: { path: string; line: number },
1055 ): Promise<Result<CommitComment>>;
1056 editCommitComment(actor: User, repo: RepoPath, id: string, body: string): Promise<Result<CommitComment>>;
1057 deleteCommitComment(actor: User, repo: RepoPath, id: string): Promise<Result<boolean>>;
1058 /** Marks a draft ready for review and sets its description. */
1059 readyPull(actor: User, repo: RepoPath, number: number, summary: string): Promise<Result<Pull>>;
1060 closePull(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
1061 /** Opens a closed pull request again, as the draft it was if it was closed as one. Never a merged one. */
1062 reopenPull(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
1063 /** Turns an open pull request back into a draft; it leaves the merge queue. */
1064 convertPullToDraft(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
1065 /**
1066 * Lands the pull request on the repository's default branch. Unless
1067 * `keepIssueOpen`, that resolves the issue it was for: the issue closes
1068 * naming this pull request, and the others still in progress for it close
1069 * as superseded. Only members of the repository's workspace may merge.
1070 *
1071 * If the default branch has moved, the pull request is brought up to date
1072 * first and lands when that is done; it comes back still open, and
1073 * `PullDetail.landing` is true meanwhile. A repository that requires pull
1074 * requests to be up to date refuses instead.
1075 */
1076 mergePull(actor: User, repo: RepoPath, number: number, options?: MergeOptions): Promise<Result<Pull>>;
1077 /**
1078 * Drafts and open pull requests the viewer started, most recently active
1079 * first, each with where it stands if g1t is seeing it through.
1080 */
1081 listActivePulls(
1082 viewer: Viewer,
1083 ): Promise<{ pull: Pull; issue: Issue | null; lifecycle: Lifecycle | null }[]>;
1084 /**
1085 * The issues and pull requests a person opened, a page at a time, only
1086 * on repositories the viewer may read. Not found for no such account.
1087 */
1088 byAuthor(username: string, viewer: Viewer, filter?: AuthoredFilter): Promise<Result<Authored>>;
1089 /**
1090 * What a person did each day of the last year (issues and pull requests
1091 * opened, reviews given), only on repositories the viewer may read: the
1092 * calendar on their profile. Not found for no such account.
1093 */
1094 contributions(username: string, viewer: Viewer): Promise<Result<Contributions>>;
1095
1096
1097
1098 appendSession(actor: User, repo: RepoPath, number: number, entries: NewSessionEntry[]): Promise<Result<{ count: number }>>;
1099 readSession(repo: RepoPath, number: number, viewer: Viewer, afterSeq?: number): Promise<Result<SessionEntry[]>>;
1100}
1101
1102/**
1103 * What to pass `ReposApi.compare` to see what a pull request changes.
1104 *
1105 * A working copy is compared as a whole. A branch, of the repository or of
1106 * a fork of it, is compared by name while the pull request is open, and by
1107 * the commit it was merged or closed at afterwards, so later pushes to the
1108 * branch do not change the record. A fork's branch is compared from the
1109 * repository it merges into (`targetId`).
1110 */
1111export function pullComparison(pull: Pull): {
1112 repoId: string;
1113 base: string | null;
1114 head: string | null;
1115 /** The branch it merges into, which it is compared from. */
1116 baseBranch: string | null;
1117 /** For a fork's branch: the repository it merges into. */
1118 targetId: string | null;
1119} {
1120 const baseBranch = pull.base ?? null;
1121 if (pull.forkRepoId) return { repoId: pull.forkRepoId, base: pull.mergeBase, head: null, baseBranch, targetId: null };
1122 const settled = pull.status === "merged" || pull.status === "closed";
1123 return {
1124 repoId: pull.headRepoId ?? pull.repoId,
1125 base: pull.mergeBase,
1126 // A fork's branch by the commit it was last seen at, which the repository
1127 // it merges into holds too, so those who read that repository can read it.
1128 head: ((settled || pull.headRepoId) && pull.headCommit) || pull.branch,
1129 baseBranch,
1130 targetId: pull.headRepoId ? pull.repoId : null,
1131 };
1132}
1133
1134/** The id of the repository holding a pull request's change: its working copy, its fork, or the repository. */
1135export function pullSourceId(pull: Pull): string {
1136 return pull.forkRepoId ?? pull.headRepoId ?? pull.repoId;
1137}
1138
1139
1140export type QueueState = "waiting" | "testing" | "passed" | "failed" | "landed" | "removed";
1141
1142/** One pull request's place in a merge queue. */
1143export type QueueEntry = {
1144 id: string;
1145 number: number;
1146 title: string;
1147 agent: string;
1148 state: QueueState;
1149 /** The pull requests merged ahead of it in the state being tested, in order. */
1150 ahead: number[];
1151 baseCommit: string | null;
1152 combinedCommit: string | null;
1153 error: string | null;
1154 results: CheckResult[];
1155 /** Username of whoever merged it into the queue: a person, or `g1t`. */
1156 enqueuedBy: string;
1157 /** RFC 3339. */
1158 createdAt: string;
1159 /** RFC 3339. */
1160 finishedAt: string | null;
1161};
1162
1163/** A repository's merge queue: what is in it, in order, and what recently left. */
1164export type QueueView = {
1165 enabled: boolean;
1166 active: QueueEntry[];
1167 /** Newest first. */
1168 recent: QueueEntry[];
1169};
1170
1171export type QueueStackItem = {
1172 number: number;
1173 title: string;
1174 /** The repository holding the change, and its branch. */
1175 source: RepoPath;
1176 branch: string;
1177 commit: string;
1178 /** How it lands: the state is built with its commits made this way. */
1179 method?: MergeMethod;
1180 /** For merge and squash: the commit's message. */
1181 message?: string;
1182 /** For squash: who the commit is by. */
1183 author?: User;
1184};
1185
1186/** What a sandbox needs to build and check one combined state. */
1187export type QueueJob = {
1188 entryId: string;
1189 token: string;
1190 repo: RepoPath;
1191 defaultBranch: string;
1192 baseCommit: string;
1193 branch: string;
1194 stack: QueueStackItem[];
1195 /** Always empty: the state is checked by the merge_group workflows run on it. */
1196 checks: string[];
1197 /** Always empty, as `checks`. */
1198 contractChecks: string[];
1199 actor: User;
1200};
1201
1202// --- A person's work ---------------------------------------------------------
1203// Mirrors the `Authored*` types in `crates/contracts/src/work.rs`.
1204
1205export type AuthoredKind = "issue" | "pull";
1206/** `closed` takes in merged pull requests too; `merged` is only those. */
1207export type AuthoredState = "open" | "closed" | "merged";
1208/** `created` is newest first, `updated` most recently changed, `oldest` oldest first. */
1209export type AuthoredSort = "created" | "updated" | "oldest";
1210
1211/** The most items one `byAuthor` page holds. */
1212export const AUTHORED_PAGE = 25;
1213
1214export type AuthoredFilter = {
1215 kind?: AuthoredKind;
1216 state?: AuthoredState;
1217 /** `namespace/name`. */
1218 repo?: string;
1219 sort?: AuthoredSort;
1220 /** The `next` of the page before. */
1221 before?: string;
1222 limit?: number;
1223};
1224
1225export type AuthoredItem = {
1226 kind: AuthoredKind;
1227 repo: RepoPath;
1228 number: number;
1229 title: string;
1230 /** A merged pull request is closed. */
1231 state: State;
1232 /** A pull request's own status; null on an issue. */
1233 status: PullStatus | null;
1234 /** Why an issue was closed. */
1235 reason: IssueReason | null;
1236 draft: boolean;
1237 merged: boolean;
1238 /** RFC 3339. */
1239 createdAt: string;
1240 /** RFC 3339. */
1241 updatedAt: string;
1242 /** RFC 3339. */
1243 mergedAt: string | null;
1244};
1245
1246/** Over every repository the viewer may read, whatever the filters. */
1247export type AuthoredCounts = {
1248 pullsMerged: number;
1249 pullsOpen: number;
1250 pulls: number;
1251 issues: number;
1252 issuesOpen: number;
1253};
1254
1255export type Authored = {
1256 items: AuthoredItem[];
1257 /** Pass as `before` for the next page; null on the last. */
1258 next: string | null;
1259 counts: AuthoredCounts;
1260 /** The repositories they worked in that the viewer may read, most work first. */
1261 repos: { repo: RepoPath; count: number }[];
1262};
1263
1264// Mirrors `Contributions` in `crates/contracts/src/work.rs`.
1265
1266/** How many days `contributions` covers: today and the 364 before it. */
1267export const CONTRIBUTION_DAYS = 365;
1268
1269/** One day with something on it; days with nothing are left out. */
1270export type ContributionDay = {
1271 /** `YYYY-MM-DD`, UTC. */
1272 date: string;
1273 /** Everything that day, commits included. */
1274 count: number;
1275 /** How many of `count` are commits they pushed to a default branch. */
1276 commits?: number;
1277};
1278
1279/** A person's year, as far as the viewer may see. */
1280export type Contributions = {
1281 /** Oldest first. */
1282 days: ContributionDay[];
1283 total: number;
1284 /** The first day counted, `YYYY-MM-DD`; the last is today (UTC). */
1285 from: string;
1286};
1287
1288// --- Coordinating work: sessions, claims and dependencies -----------------
1289// Mirrors `crates/contracts/src/work/coordination.rs`.
1290
1291/** The harness a session runs in. */
1292export type Harness = "claude_code" | "claude_managed_agents" | "codex" | "cursor" | "copilot" | "other";
1293
1294/** A session as a mark on something it made. */
1295export type SessionRef = { id: string; harness: Harness };
1296
1297/** The sessions tied to one pull request (`pullSessions`). */
1298export type PullSessions = { number: number; sessions: SessionRef[] };
1299
1300/** Active: heard from in 15 minutes; idle: not since; ended: ended, or quiet for 2 hours. */
1301export type SessionState = "active" | "idle" | "ended";
1302
1303/** One run of someone's own harness on a repository. */
1304export type WorkSession = {
1305 /** `ses_…`. */
1306 id: string;
1307 repoId: string;
1308 issue: number | null;
1309 pull: number | null;
1310 branch: string | null;
1311 actor: { id: string; username: string };
1312 harness: Harness;
1313 harnessSessionId: string | null;
1314 title: string | null;
1315 state: SessionState;
1316 headCommit: string | null;
1317 startedAt: string;
1318 lastSeenAt: string;
1319 endedAt: string | null;
1320 /** What the harness said, or `timed_out`. */
1321 endReason: string | null;
1322 /** For a subagent: the session that started it. */
1323 parentId?: string | null;
1324 /** For a subagent: its kind (Explore, security-reviewer). */
1325 agentType?: string | null;
1326 /** For a subagent: the task it was given, in a few words. */
1327 task?: string | null;
1328 /** For a subagent: the prompt its parent gave it. */
1329 prompt?: string | null;
1330 /** What it reported when it finished: a subagent's final message. */
1331 result?: string | null;
1332 model?: string | null;
1333 /** How many subagents it started. */
1334 subagents?: number;
1335 /** In a list, for one with no title: the start of the first prompt it recorded. */
1336 firstPrompt?: string | null;
1337};
1338
1339/** What kind of thing a session touched. */
1340export type TouchKind = "file" | "commit" | "branch" | "pull" | "issue";
1341
1342/** Something a session touched, kept once per session. */
1343export type SessionTouch = { sessionId: string; kind: TouchKind; value: string; at: string };
1344
1345/** How a session was tied to an issue or pull request. */
1346export type LinkVia = "attached" | "claim" | "branch" | "commit" | "trailer" | "opened" | "ref";
1347
1348/** An issue or pull request a session, or one of its subagents, worked on. */
1349export type SessionLink = {
1350 kind: "issue" | "pull";
1351 number: number;
1352 title: string;
1353 /** The issue's state or the pull request's status. */
1354 state: string;
1355 via: LinkVia[];
1356 sessionIds: string[];
1357};
1358
1359/** A skill a session used, by name. */
1360export type SessionSkill = { sessionId: string; name: string; uses: number };
1361
1362export type ClaimState = "active" | "released" | "expired" | "handed_off";
1363
1364/** An advisory, expiring "I'm working on #N". Never a lock. */
1365export type Claim = {
1366 /** `clm_…`. */
1367 id: string;
1368 repoId: string;
1369 issue: number;
1370 sessionId: string | null;
1371 actor: { id: string; username: string };
1372 harness: Harness | null;
1373 note: string | null;
1374 state: ClaimState;
1375 createdAt: string;
1376 expiresAt: string;
1377 endedAt: string | null;
1378 endReason: string | null;
1379 /** For a hand-off: who it was handed to; null for anyone. */
1380 handoffTo: string | null;
1381};
1382
1383/** `session.started` and `session.ended`. */
1384export type SessionEventData = { repoId: string; number?: number; issue?: number; session: WorkSession };
1385
1386/** `work.claimed`, `work.released`, `work.claim_expired` and `work.handoff_requested`. */
1387export type ClaimEventData = {
1388 repoId: string;
1389 number: number;
1390 issue: number;
1391 claim: Claim;
1392 others?: Claim[];
1393 tookOver?: string;
1394 reason?: string;
1395 from?: string;
1396 to?: string;
1397 note?: string;
1398};
1399
1400/** `dependency.changed`. */
1401export type DependencyChangedData = {
1402 repoId: string;
1403 number: number;
1404 issue: number;
1405 blockedBy: number[];
1406 added: number[];
1407 removed: number[];
1408 cleared: number[];
1409 remaining: number[];
1410 mergedPull?: number;
1411};
1412
1413/** `overlap.detected` between two pull requests, sent once for each side of a new overlap. */
1414export type PullOverlapData = {
1415 repoId: string;
1416 number: number;
1417 pull: number;
1418 otherPull: number;
1419 files: string[];
1420 issue?: number;
1421 otherIssue?: number;
1422};
1423
1424/** `overlap.detected` between two sessions that touched the same files (siblings included), once for each side. */
1425export type SessionOverlapData = {
1426 repoId: string;
1427 number?: number;
1428 sessionId: string;
1429 otherSessionId: string;
1430 otherActor: string;
1431 files: string[];
1432 issue?: number;
1433 otherIssue?: number;
1434};
1435
1436/** `overlap.detected`: between pull requests, or between sessions (with `sessionId`). */
1437export type OverlapDetectedData = PullOverlapData | SessionOverlapData;
1438
1439/** `session.progressed`: a session recorded more (at most every few seconds). */
1440export type SessionProgressedData = { repoId: string; number?: number; sessionId: string; rootId?: string };
1441
1442/** An issue in brief. */
1443export type IssueBrief = { number: number; title: string; state: State };
1444
1445/** A pull request in brief. */
1446export type PullBrief = { number: number; title: string; status: PullStatus };
1447
1448/** An issue another is blocked by, and where it stands. */
1449export type Blocker = {
1450 number: number;
1451 title: string;
1452 state: State;
1453 /** Active claims on it. */
1454 claims: Claim[];
1455 pulls: PullBrief[];
1456};
1457
1458/** A pull request's place in the merge queue; `position` 1 is the front. */
1459export type QueuePlace = { state: QueueState; position: number };
1460
1461/** One of an issue's pull requests, with where it stands. */
1462export type WorkPull = {
1463 number: number;
1464 title: string;
1465 status: PullStatus;
1466 branch: string | null;
1467 headCommit: string | null;
1468 author: { id: string; username: string };
1469 /** `failed` when the merge queue took it out, until its head moves. */
1470 checkStatus: CheckStatus | null;
1471 requiredChecks: RequiredCheck[];
1472 mergeable: Mergeable;
1473 /** Other pull requests in progress changing the same files. */
1474 overlaps: Overlap[];
1475 /** Its place in the merge queue, while it is in it. */
1476 queue: QueuePlace | null;
1477};
1478
1479// --- Decision records (crates/contracts/src/decisions.rs) ------------------
1480
1481export type DecisionKind =
1482 | "proposal"
1483 | "claim"
1484 | "integration_failure"
1485 | "integration_passed"
1486 | "fix"
1487 | "approval"
1488 | "merge"
1489 | "note";
1490
1491/** Claimed: someone said so. Tested: a check or the merge queue proved it. Approved: a person approved it. */
1492export type DecisionConfidence = "claimed" | "tested" | "approved";
1493
1494/** A structured reason kept on an issue or pull request, with how sure it is. */
1495export type Decision = {
1496 /** `dec_…`. */
1497 id: string;
1498 issue: number | null;
1499 pull: number | null;
1500 sessionId: string | null;
1501 /** Username of who recorded it; null when g1t did. */
1502 actor: string | null;
1503 kind: DecisionKind;
1504 confidence: DecisionConfidence;
1505 summary: string;
1506 /**
1507 * What it rests on, kept as recorded, in snake_case: `commit`, `check`
1508 * (`name`, `conclusion`, `url`), `combination`, `other_pulls`,
1509 * `suspects`, `bisected`, `merge_queue`.
1510 */
1511 evidence: Record<string, unknown>;
1512 /** RFC 3339. */
1513 createdAt: string;
1514};
1515
1516/** Everything about an issue an agent needs to pick it up: `get_work`. */
1517export type WorkContext = {
1518 issue: Issue;
1519 /** The items under its "Definition of done" heading. */
1520 definitionOfDone: string[];
1521 /** The checks every pull request into the default branch must pass. */
1522 requiredChecks: string[];
1523 /** Active claims on it. */
1524 claims: Claim[];
1525 /** Sessions on it or its pull requests, most recently heard from first. */
1526 sessions: WorkSession[];
1527 pulls: WorkPull[];
1528 blockedBy: Blocker[];
1529 /** The issues it blocks. */
1530 blocks: IssueBrief[];
1531 /** On it and its pull requests, newest first. */
1532 decisions: Decision[];
1533 /** The latest `integration_failure` decision among them. */
1534 lastIntegrationFailure: Decision | null;
1535 /** Its latest coordination events, newest first. */
1536 recent: G1tEvent[];
1537};
1538
1539/** A harness session and what it recorded. */
1540export type WorkSessionDetail = {
1541 /** Pending repository coordination events; reading does not acknowledge them. */
1542 updates?: { id: string; kind: string; data: unknown; at: string }[];
1543 session: WorkSession;
1544 entries: SessionEntry[];
1545 /** Its subagents and theirs, in the order they started. */
1546 children?: WorkSession[];
1547 /** What it and its subagents touched. */
1548 touched?: SessionTouch[];
1549 /** The issues and pull requests it and its subagents worked on. */
1550 links?: SessionLink[];
1551 skills?: SessionSkill[];
1552 /** For a subagent: the session at the top of its tree. */
1553 root?: WorkSession | null;
1554};
1555
1556/** Reading coordination: work service methods (services/work/src/coordination, decisions.rs). */
1557export interface CoordinationApi {
1558 getWork(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<WorkContext>>;
1559 /** Newest first; active ones unless `state` says otherwise. */
1560 listClaims(repo: RepoPath, viewer: Viewer, filter?: { state?: ClaimState; issue?: number }): Promise<Result<Claim[]>>;
1561 /** Most recently heard from first. */
1562 listWorkSessions(
1563 repo: RepoPath,
1564 viewer: Viewer,
1565 filter?: { state?: SessionState; issue?: number; pull?: number; path?: string; subagents?: boolean },
1566 ): Promise<Result<WorkSession[]>>;
1567 getWorkSession(repo: RepoPath, id: string, viewer: Viewer, afterSeq?: number): Promise<Result<WorkSessionDetail>>;
1568 /**
1569 * The harness sessions tied to some pull requests (at most 100), any way
1570 * a session is tied to one; a subagent's as the session that started it.
1571 * Only pull requests with any are listed.
1572 */
1573 pullSessions(repo: RepoPath, viewer: Viewer, numbers: number[]): Promise<Result<PullSessions[]>>;
1574 /** An issue's (with its pull requests') or a pull request's, oldest first. */
1575 listDecisions(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<Decision[]>>;
1576}
1577
1578/**
1579 * Delivery counts cover the requested UTC days; waiting counts are current.
1580 * `checking` and `failed` count each open pull request once, by its head's
1581 * checks (statuses, check runs and g1t Actions jobs); one with none is in neither.
1582 */
1583export type DeliveryMetrics = { repoId: string; opened: number; merged: number; active: number; unreviewed: number; checking: number; failed: number; queued: number; leadSeconds: number; reviewSeconds: number; reviewed: number };
1584/** `checksUnavailable`: g1t Actions could not be read, so `checking` and `failed` are not known. */
1585export type DeliveryInsights = { projects: DeliveryMetrics[]; days: { day: string; merged: number }[]; checksUnavailable: boolean };