Skip to content

g1t/packages/contracts/src/work.ts

1,251 lines44,226 bytesCodeBlame
1import type { CodeownersReport, PullCodeOwners } from "./codeowners";
2import type { User, Viewer } from "./identity";
3import type { PullBranchUpdate, RepoPath } from "./repos";
4import type { Result } from "./result";
5
6/**
7 * The filter on lists of issues and pull requests. An open pull request is
8 * a draft or one ready for review; a closed one was merged or closed
9 * without merging.
10 */
11export type State = "open" | "closed";
12
13/** Why an issue was closed. */
14export type IssueReason = "completed" | "not_planned";
15
16/**
17 * Something that should change in a repository: a bug, a feature, a
18 * question. Opened by a person, an agent or an integration. Pull requests
19 * are made against it; the one that is merged resolves it.
20 *
21 * Issues and pull requests share one sequence of numbers per repository.
22 */
23export type Issue = {
24 id: string;
25 repoId: string;
26 /** Shown as `#12`. */
27 number: number;
28 title: string;
29 /** Markdown. Also what an agent is given to work from. */
30 body: string;
31 labels: string[];
32 state: State;
33 /** Set when closed. */
34 reason: IssueReason | null;
35 /** The number of the pull request whose merge closed this issue. */
36 resolvedBy: number | null;
37 /** Who opened it: a person, an integration, or g1t (`kind` `agent`) for one its agent filed while at work. */
38 author: User;
39 /**
40 * For an issue g1t's agent filed: the person it was working for. They may
41 * manage it as its author could. See `workOwner`.
42 */
43 requestedBy: User | null;
44 /** RFC 3339. */
45 createdAt: string;
46 /** RFC 3339. */
47 updatedAt: string;
48 /** RFC 3339. */
49 closedAt: string | null;
50 /** Pull requests made against this issue, in any state. */
51 pullCount: number;
52 commentCount: number;
53 /** Usernames of the people it is assigned to. */
54 assignees: string[];
55 /** The numbers of the issues that have to be merged before this one is worked on. */
56 blockedBy: number[];
57 /**
58 * Whether a g1t agent takes it as soon as it can: at once, or when what it
59 * is blocked by has merged.
60 */
61 queued: boolean;
62 /**
63 * The agent working on it now: the one behind its newest pull request
64 * that is still in progress in a fork, such as `g1t`.
65 */
66 agent: string | null;
67 /** The milestone it is in, if any. */
68 milestone?: MilestoneRef | null;
69};
70
71/**
72 * A label of a repository: a name, a color and what it means. Issues and
73 * pull requests carry labels by name; names are lowercase.
74 */
75export type Label = {
76 name: string;
77 /** Six hex digits, without `#`. */
78 color: string;
79 description: string;
80 /** How many issues carry it, open or closed. */
81 issues: number;
82 /** How many pull requests carry it, in any state. */
83 pulls: number;
84};
85
86/** A milestone, as an issue or pull request names it. */
87export type MilestoneRef = { number: number; title: string };
88
89/**
90 * A goal, with an optional due date, that issues and pull requests are
91 * gathered under. Its progress is how many of them are closed.
92 */
93export type Milestone = {
94 /** Numbered from 1 in each repository, apart from issues. */
95 number: number;
96 title: string;
97 /** Markdown. */
98 description: string;
99 /** `YYYY-MM-DD`. */
100 dueOn: string | null;
101 state: State;
102 /** Open issues and pull requests in it. */
103 openItems: number;
104 /** Closed issues, and merged or closed pull requests, in it. */
105 closedItems: number;
106 createdAt: string;
107 updatedAt: string;
108 closedAt: string | null;
109};
110
111/** One milestone and what is in it, newest first. */
112export type MilestoneDetail = { milestone: Milestone; issues: Issue[]; pulls: Pull[] };
113
114/** How `setLabels` changes an item's labels. */
115export type LabelChange = "set" | "add" | "remove";
116
117/**
118 * Whose an issue or a pull request is to answer for: whoever asked g1t for
119 * it, or its author. They may change, close and steer it, are never asked to
120 * review it and cannot approve it, and see it as theirs. Mirrors
121 * `Pull::owner` in the Rust contracts.
122 */
123export function workOwner(item: Pick<Pull, "author" | "requestedBy">): User {
124 return item.requestedBy ?? item.author;
125}
126
127/** `draft` is still being worked on; `open` is ready for review. */
128export type PullStatus = "draft" | "open" | "merged" | "closed";
129
130/** Where the agent runs: on g1t's sandboxes, or in someone's own session. */
131export type Runtime = "hosted" | "external";
132
133/**
134 * A proposed change. It is made either in a fork created for it, which is
135 * how agents work, or on a branch pushed to the repository itself.
136 */
137export type Pull = {
138 id: string;
139 repoId: string;
140 /** Shown as `#12`. */
141 number: number;
142 /** The number of the issue this is for, if any. */
143 issue: number | null;
144 title: string;
145 /** Markdown: what changed and why. Set when marked ready. */
146 body: string | null;
147 /** A label for the agent doing the work, e.g. `claude-code`. */
148 agent: string;
149 runtime: Runtime;
150 status: PullStatus;
151 /** The fork holding the change, unless it is on a branch. */
152 fork: RepoPath | null;
153 /** The fork's repository id. */
154 forkRepoId: string | null;
155 /** The branch of the repository holding the change, unless it is in a fork. */
156 branch: string | null;
157 headCommit: string | null;
158 /**
159 * For a merged pull request, what the branch pointed to before the merge.
160 * Comparing against it shows what the pull request changed.
161 */
162 mergeBase: string | null;
163 /** Username of whoever merged it. */
164 mergedBy: string | null;
165 /** RFC 3339. */
166 mergedAt: string | null;
167 /**
168 * Set on a pull request closed because another one for the same issue was
169 * merged: that one's number.
170 */
171 supersededBy: number | null;
172 /**
173 * Where the latest run of the issue's acceptance checks stands, if there
174 * has been one against the current head.
175 */
176 checkStatus: CheckStatus | null;
177 /** The files it changes, as of its latest push. */
178 files: ChangedFile[];
179 /** Usernames of the people it is assigned to. */
180 assignees: string[];
181 /**
182 * Those whose review was asked for: usernames, and `g1t` when a g1t
183 * agent was asked.
184 */
185 reviewers: string[];
186 /**
187 * Teams whose review was asked for, as `workspace/slug`. A team stays here
188 * after review assignment picks people from it, who are in `reviewers`.
189 */
190 teamReviewers?: string[];
191 /** The labels it carries, by name. */
192 labels?: string[];
193 /** The milestone it is in, if any. */
194 milestone?: MilestoneRef | null;
195 /**
196 * The branch it merges into. Lists and `getPull` name it; null only in
197 * what services pass between themselves, for the default branch.
198 */
199 base?: string | null;
200 /** Who opened it: a person, or g1t (`kind` `agent`, username `g1t`) for a change g1t made. */
201 author: User;
202 /**
203 * For a change g1t made: the person who asked for it, by assigning an issue
204 * or handing g1t the work. They answer for it as its author would. See
205 * `workOwner`.
206 */
207 requestedBy: User | null;
208 /** RFC 3339. */
209 createdAt: string;
210 /** RFC 3339. */
211 updatedAt: string;
212 /**
213 * How sure g1t is of a g1t agent's change, from what it can observe, once
214 * the agent has finished it. Absent before then, and on changes g1t is
215 * not seeing through.
216 */
217 confidence?: Confidence | null;
218};
219
220/** How sure g1t is that an agent's change is right. */
221export type ConfidenceLevel = "low" | "medium" | "high";
222
223/**
224 * How sure g1t is of a change an agent made, worked out from what can be
225 * observed: its checks, how often it was sent back, the reviewer agent's
226 * verdict, whether it touched tests, its size, where it reached, how close
227 * it came to its guardrails, and what it asked without an answer. The
228 * agent's own word can only lower it.
229 */
230export type Confidence = {
231 level: ConfidenceLevel;
232 /** A few words each, most telling first: what lowered it, or for `high`, what it rests on. */
233 reasons: string[];
234 /** What the agent said of its own change, if it said. */
235 selfReported: ConfidenceLevel | null;
236 /** What the agent said it was unsure about. */
237 uncertainAbout: string[];
238 /** The agent run it was worked out after. */
239 runId: string | null;
240 /** RFC 3339. */
241 assessedAt: string;
242};
243
244/** What became of the agent when an issue was opened and handed to it in one step. */
245export type AgentStartStatus = "started" | "queued" | "not_started";
246
247/** Whether the agent started, and if not, why and what fixes it. */
248export type AgentStart = {
249 status: AgentStartStatus;
250 /**
251 * Why it did not start: `not_paid`, `trial_used`, `limit`, `paused`,
252 * `issue_cap`, `billing_unavailable` or `no_model`; `waiting` when queued.
253 */
254 code: string | null;
255 /** What happened, in a sentence or two, with what to do. */
256 message: string | null;
257 /** Where the fix is: the workspace's billing or model settings. */
258 fixUrl: string | null;
259};
260
261/** An issue opened and handed to g1t in one step. The issue exists whatever became of the agent. */
262export type Delegated = {
263 issue: Issue;
264 /** The pull request the agent opened, when it started. */
265 pull: Pull | null;
266 agent: AgentStart;
267};
268
269/**
270 * What to put an agent on: an issue's title, and what to do in plain words,
271 * with what done means if you like (a "Definition of done" section). What
272 * has to pass before it merges is the branch's required checks.
273 */
274export type DelegateInput = {
275 title: string;
276 body: string;
277 labels?: string[];
278 /** Deprecated: commands, added to the body under "Definition of done". */
279 checks?: string[];
280};
281
282/** One file a pull request changes, and by how much. */
283export type ChangedFile = { path: string; additions: number; deletions: number };
284
285/**
286 * Another pull request in progress that changes some of the same files. Two
287 * for the same issue are alternatives; two for different issues are heading
288 * for a conflict.
289 */
290export type Overlap = {
291 number: number;
292 title: string;
293 /** The number of the issue the other pull request is for. */
294 issue: number | null;
295 /** The files both change. */
296 paths: string[];
297};
298
299/**
300 * On a pull request, `failed` means the merge queue took it out, until its
301 * head moves. The other states are from runs of commands written on issues,
302 * which g1t no longer runs.
303 */
304export type CheckStatus = "queued" | "running" | "passed" | "failed" | "errored";
305
306/** How one command went, in a run recorded before checks were workflows. */
307export type CheckResult = {
308 command: string;
309 passed: boolean;
310 /** Null when the command was stopped for taking too long. */
311 exitCode: number | null;
312 /** What the command printed; the end of it, when there was a lot. */
313 output: string;
314 durationMs: number;
315};
316
317/**
318 * A record against a pull request's head: the merge queue taking it out,
319 * with why, or an earlier run of commands written on its issue.
320 */
321export type CheckRun = {
322 id: string;
323 /** The commit that was checked. */
324 headCommit: string;
325 status: CheckStatus;
326 results: CheckResult[];
327 /** Why the checks could not be run, when `status` is `errored`. */
328 error: string | null;
329 /** RFC 3339. */
330 createdAt: string;
331 /** RFC 3339. */
332 finishedAt: string | null;
333};
334
335/** A reviewer's decision on a pull request. */
336export type Verdict = "approve" | "request_changes";
337
338/**
339 * A comment on an issue or a pull request. On a pull request it can sit on
340 * one line of the change, and it can carry a reviewer's verdict.
341 */
342export type Comment = {
343 id: string;
344 /**
345 * Something a person or an agent wrote, or something that happened: an
346 * assignment, a review asked for, a close.
347 */
348 kind: "comment" | "event";
349 author: User;
350 /**
351 * Markdown. For an event, what its author did, as the rest of a sentence
352 * that starts with their name: "assigned ana".
353 */
354 body: string;
355 /** The file commented on, for a comment on a line. */
356 path: string | null;
357 /** The line of that file, as numbered after the change. */
358 line: number | null;
359 verdict: Verdict | null;
360 /** RFC 3339. */
361 createdAt: string;
362};
363
364export type NewComment = {
365 /** May be empty when approving. */
366 body: string;
367 path?: string;
368 line?: number;
369 verdict?: Verdict;
370};
371
372/** What a sandbox needs to carry out a check run. */
373export type CheckJob = {
374 runId: string;
375 /** Lets the sandbox, and nothing else, report this run's results. */
376 token: string;
377 commands: string[];
378 /** The repository holding the commit: the fork, or the repository itself. */
379 source: RepoPath;
380 commit: string;
381 /** Who the pull request is for (`workOwner`: whoever asked g1t for it, or its author), and so can read its source. */
382 author: User;
383 /** Username of whoever wrote the checks: the issue's author. */
384 requestedBy: string;
385 repo: RepoPath;
386 number: number;
387};
388
389export type CheckReport = { results?: CheckResult[]; error?: string; skip?: boolean };
390
391export type SessionEntryKind = "prompt" | "message" | "tool_call" | "tool_result" | "note";
392
393/** One step of an agent's session: the "why" behind a pull request's commits. */
394export type SessionEntry = {
395 seq: number;
396 kind: SessionEntryKind;
397 text: string;
398 /** For tool calls and results. */
399 tool: string | null;
400 /** The fork's head commit when this entry was recorded, if known. */
401 commit: string | null;
402 /** RFC 3339. */
403 at: string;
404};
405
406export type NewSessionEntry = Pick<SessionEntry, "kind" | "text"> &
407 Partial<Pick<SessionEntry, "tool" | "commit">>;
408
409export type IssueDetail = {
410 issue: Issue;
411 /** Every pull request made against it, oldest first. */
412 pulls: Pull[];
413 comments: Comment[];
414};
415
416export type PullDetail = {
417 pull: Pull;
418 /** The issue it is for, if any. */
419 issue: Issue | null;
420 comments: Comment[];
421 /** The latest record against its head: the merge queue taking it out. */
422 checks: CheckRun | null;
423 /** Other pull requests in progress that change the same files. */
424 overlaps: Overlap[];
425 /**
426 * Whether the branch it would merge into has moved on without it, so that
427 * it has to catch up before it can merge.
428 */
429 behind: boolean;
430 /** Whether a g1t agent is reviewing it right now. */
431 reviewPending: boolean;
432 /**
433 * Where it stands on its way to being merged, for a pull request g1t is
434 * seeing through. Null on anyone else's.
435 */
436 lifecycle: Lifecycle | null;
437 /**
438 * A merge was asked for while it was behind: g1t is bringing it up to
439 * date and will then land it.
440 */
441 landing: boolean;
442 /** Why g1t stopped working on it, if it did. */
443 stalled: string | null;
444 /** Messages people sent the agent while it worked, oldest first. */
445 messages: AgentMessage[];
446 /** What workflow runs said about its head commit, one per workflow. */
447 statuses?: CommitStatus[];
448 /**
449 * Whether it merges cleanly into the branch it targets, worked out ahead
450 * of time whenever either side moves.
451 */
452 mergeable?: Mergeable;
453 /** When `mergeable` is `conflicting`: the files that conflict. */
454 conflicts?: string[];
455 /** Earlier records like `checks`, newest first, without their output. */
456 earlierChecks?: CheckRun[];
457 /**
458 * The checks the default branch's protection requires, each as it stands
459 * on the head commit. Empty when none are required.
460 */
461 requiredChecks?: RequiredCheck[];
462 /**
463 * Who owns the files it changes (the CODEOWNERS file of the branch it
464 * merges into) and whose approval is still needed. Absent without one.
465 */
466 codeOwners?: PullCodeOwners | null;
467};
468
469/** Where a required check stands on a commit; `expected` when nothing has reported it yet. */
470export type RequiredState = "success" | "failure" | "pending" | "expected";
471
472/** One check a branch's protection requires, as it stands on a commit. */
473export type RequiredCheck = {
474 /** A workflow's name, such as `CI`, or another status's context. */
475 name: string;
476 state: RequiredState;
477 description: string | null;
478 /** Where to see more: the workflow run, for one a workflow reported. */
479 targetUrl: string | null;
480};
481
482/** A check name reported on a repository's commits lately, for choosing required checks. */
483export type SeenCheck = {
484 name: string;
485 /** The events it was reported for, such as `pull_request`; empty for a status that names none. */
486 events: string[];
487 /** RFC 3339. */
488 lastSeen: string;
489};
490
491/**
492 * A status context's check name: `CI / pull_request` is the `CI` check,
493 * reported for a `pull_request` event. A context that does not end in an
494 * event, such as `g1t / deploy`, is its own name. As the work service reads it.
495 */
496export function checkName(context: string): string {
497 const at = context.lastIndexOf(" / ");
498 if (at <= 0) return context;
499 return STATUS_EVENTS.has(context.slice(at + 3)) ? context.slice(0, at) : context;
500}
501
502const STATUS_EVENTS = new Set([
503 "push",
504 "pull_request",
505 "pull_request_target",
506 "pull_request_review",
507 "merge_group",
508 "workflow_dispatch",
509 "workflow_run",
510 "workflow_call",
511 "schedule",
512 "release",
513 "issues",
514 "issue_comment",
515 "repository_dispatch",
516]);
517
518/**
519 * Whether a pull request merges cleanly into the branch it targets:
520 * `unknown` when it was never worked out or could not be, `checking` while
521 * a probe merges the two.
522 */
523export type Mergeable = "clean" | "conflicting" | "unknown" | "checking";
524
525/** What a sandbox needs to find out whether a pull request merges cleanly. */
526export type MergecheckJob = {
527 pullId: string;
528 /** Lets the sandbox, and nothing else, report this probe. */
529 token: string;
530 repo: RepoPath;
531 number: number;
532 defaultBranch: string;
533 /** The default branch's commit to merge into. */
534 base: string;
535 /** The repository holding the change: its fork, or the repository. */
536 source: RepoPath;
537 /** The branch of `source` holding it. */
538 branch: string;
539 /** The change's commit. */
540 head: string;
541 /** Who the pull request is for (`workOwner`: whoever asked g1t for it, or its author), and so can read its source. */
542 author: User;
543};
544
545/** What a workflow run (or another tool) says about a commit. */
546export type CommitStatus = {
547 /** What reported it, such as `CI / push`. */
548 context: string;
549 state: "pending" | "success" | "failure" | "error";
550 description: string | null;
551 targetUrl: string | null;
552 updatedAt: string;
553};
554
555/**
556 * A step on the way from an assigned issue to a pull request that is ready
557 * to merge. g1t takes each one without being asked: `working` (the agent is
558 * making the change), `checking`, `reviewing`, `revising` (the agent is
559 * addressing failed checks or a review), `catching_up` (merging in the
560 * branch it would land on), `answering` (woken to answer another agent),
561 * then `ready` for a person to merge. `needs_you`
562 * means g1t has stopped and a person decides what happens next.
563 */
564export type Stage =
565 | "working"
566 | "checking"
567 | "reviewing"
568 | "revising"
569 | "catching_up"
570 | "answering"
571 | "queued"
572 | "ready"
573 | "needs_you";
574
575export type Lifecycle = {
576 stage: Stage;
577 /** One sentence saying what is happening, or why it stopped. */
578 detail: string;
579 /** How many times the agent has been sent back to revise it. */
580 revisions: number;
581};
582
583/** What the runner needs to carry out a step of a pull request's lifecycle. */
584export type LifecycleJob = {
585 pullId: string;
586 repo: RepoPath;
587 number: number;
588 /** Who the pull request belongs to (`workOwner`: whoever asked g1t for it, or its author). Sandboxes act as them. */
589 author: User;
590 /** The repository holding the change: its fork, or the repository itself. */
591 source: RepoPath;
592 /** The branch of the source holding the change; its default branch when null. */
593 branch: string | null;
594 defaultBranch: string;
595 title: string;
596 description: string;
597 issue: Issue | null;
598 /** For a revision: the failed checks or the review to address. */
599 feedback: string;
600 /** For a revision: which one this is, from 1. */
601 round: number;
602};
603
604/** An agent woken to answer what other agents sent it while it was not at work. */
605export type Wake = { job: LifecycleJob; messages: AgentMessage[] };
606
607/**
608 * `planning` while an agent reads the repository and writes it; `ready` for
609 * a person to read and apply; `failed` if it could not be written;
610 * `applied` once its issues are open.
611 */
612export type PlanStatus = "planning" | "ready" | "failed" | "applied";
613
614/** One issue a plan proposes. */
615export type PlannedIssue = {
616 title: string;
617 /** Markdown: what to change, where, and why. */
618 body: string;
619 labels: string[];
620 /** What is true once it is done, in plain words; added to the issue's body under "Definition of done". */
621 done: string[];
622 /** The files it will most likely change. */
623 files: string[];
624 /**
625 * The positions, counting from 1, of earlier issues in the plan that have
626 * to be merged first.
627 */
628 dependsOn: number[];
629 /** Its number, once the plan has been applied and it was kept. */
630 number: number | null;
631};
632
633/** An outcome someone wrote, and the issues an agent proposes to get there. */
634export type Plan = {
635 id: string;
636 repoId: string;
637 /** The outcome wanted, as written. */
638 brief: string;
639 status: PlanStatus;
640 /** The agent's account of how it split the work. */
641 summary: string;
642 issues: PlannedIssue[];
643 /** Why it could not be written, when `status` is `failed`. */
644 error: string | null;
645 author: User;
646 /** RFC 3339. */
647 createdAt: string;
648 /** RFC 3339. */
649 finishedAt: string | null;
650 /** Once applied: where each issue it opened stands now, in plan order. */
651 progress: IssueProgress[];
652 /** Questions and handoffs between its pull requests' agents, newest first. */
653 exchanges: AgentMessage[];
654};
655
656/** What a sandbox needs to write a plan. */
657export type PlanJob = {
658 planId: string;
659 /** Lets the sandbox, and nothing else, report this plan. */
660 token: string;
661 brief: string;
662 repo: RepoPath;
663};
664
665/** An issue waiting for a g1t agent that can be given one now. */
666export type ReadyIssue = {
667 repo: RepoPath;
668 number: number;
669 /** Who queued it, on whose say-so the agent works. */
670 actor: User;
671};
672
673/**
674 * How a repository wants its pull requests handled. A repository that has
675 * changed nothing has the defaults.
676 */
677export type RepoSettings = {
678 /**
679 * Land a g1t agent's pull request without a person once it is ready:
680 * checks passed and approved as the settings below require.
681 */
682 autoMerge: boolean;
683 /**
684 * The checks that must pass on a pull request's head before it may merge
685 * into the default branch, by name: a workflow's name (`CI`) or another
686 * status's context (`g1t / deploy`). The same for people and agents, and
687 * for the merge queue.
688 */
689 requiredChecks: string[];
690 /**
691 * Refuse to merge a pull request that does not contain the default
692 * branch's latest commits, so that what merges is what was checked. When
693 * off, merging one that is behind brings it up to date first.
694 */
695 requireUpToDate: boolean;
696 /**
697 * How many approving reviews a pull request needs before it may merge. A
698 * reviewer who has since asked for changes blocks it.
699 */
700 requiredApprovals: number;
701 /** Whether a g1t agent's approval counts towards `requiredApprovals`. */
702 countAgentApprovals: boolean;
703 /** Whether someone who may merge can bypass required checks that have not passed. */
704 allowIgnoringChecks: boolean;
705 /** Whether a g1t agent's pull request is reviewed by a second agent unasked. */
706 agentReview: boolean;
707 /**
708 * How many times a g1t agent is sent back to its pull request before a
709 * person is asked instead.
710 */
711 maxRevisions: number;
712 /**
713 * Merge through a queue: pull requests are tested together with those
714 * ahead of them, and only a combination that passed reaches the default
715 * branch.
716 */
717 mergeQueue: boolean;
718 /**
719 * Ask a person before merging a g1t agent's change whose confidence is
720 * low: auto-merge and the merge queue leave it until a person approves it.
721 */
722 holdLowConfidence: boolean;
723 /**
724 * Refuse to merge until the code owners of every file it changes have
725 * approved, as many as each section asks. People only; `g1t` only where
726 * the file names `@g1t`.
727 */
728 requireCodeOwnerReview?: boolean;
729 /** Username of the member who last changed the settings, if anyone has. */
730 updatedBy: string | null;
731 /** RFC 3339. */
732 updatedAt: string | null;
733};
734
735/** The settings a member can change. */
736export type RepoSettingsInput = Omit<RepoSettings, "updatedBy" | "updatedAt">;
737
738/** The next step for a pull request g1t is seeing through, already claimed. */
739export type Advance =
740 | { action: "none" }
741 | { action: "review" | "revise" | "catch_up"; job: LifecycleJob };
742
743/** What a sandbox needs to review a pull request. */
744export type ReviewJob = {
745 runId: string;
746 /** Lets the sandbox, and nothing else, report this review. */
747 token: string;
748 /** The repository holding the commit: the fork, or the repository itself. */
749 source: RepoPath;
750 commit: string;
751 repo: RepoPath;
752 defaultBranch: string;
753 number: number;
754 title: string;
755 description: string;
756 /** The issue the pull request is for, which says what it should achieve. */
757 issue: Issue | null;
758 /** Who the pull request is for (`workOwner`: whoever asked g1t for it, or its author), and so can read its source. */
759 author: User;
760 /**
761 * The files it changes, as of its latest push: how large the change is,
762 * which decides the model that reviews it.
763 */
764 files: ChangedFile[];
765 /**
766 * What among them runs, configures or guards things (CI workflows,
767 * secrets, infrastructure), once each. Any sends the review to the
768 * larger model.
769 */
770 sensitive: string[];
771};
772
773export type OpenIssueInput = {
774 title: string;
775 body: string;
776 labels?: string[];
777 /** Deprecated: commands, added to the body under "Definition of done". */
778 checks?: string[];
779 /** The number of the milestone to put it in. Needs the Triage role. */
780 milestone?: number;
781};
782
783export type UpdateIssueInput = {
784 title?: string;
785 body?: string;
786 labels?: string[];
787 /** Usernames of the people it is assigned to; replaces the whole set. */
788 assignees?: string[];
789 /** The number of the milestone to put it in; 0 takes it out. */
790 milestone?: number;
791};
792
793export type OpenPullInput = {
794 /** The number of the issue this is for. */
795 issue?: number;
796 /** Defaults to the issue's title; required without an issue. */
797 title?: string;
798 /** What changed and why. Usually set later, when a draft is marked ready. */
799 body?: string;
800 /**
801 * A branch of the repository that already holds the change. The pull
802 * request is then ready for review at once and has no fork.
803 */
804 branch?: string;
805 agent: string;
806 runtime: Runtime;
807 /** The branch to merge into: the default branch when left out. */
808 base?: string;
809};
810
811/** One repository's pull requests from `pullsForRepos`, newest first. */
812export type RepoPulls = {
813 repoId: string;
814 /** Draft and open. */
815 open: Pull[];
816 /** Merged and closed. */
817 closed: Pull[];
818};
819
820/** Issues, pull requests, comments and sessions. */
821export interface WorkApi {
822 openIssue(actor: User, repo: RepoPath, input: OpenIssueInput): Promise<Result<Issue>>;
823 /**
824 * Opens an issue to put g1t on at once: refused, with nothing
825 * opened, unless `actor` may put agents to work in `repo`. The runner's
826 * `delegate` calls it, then starts the agent.
827 */
828 delegateIssue(actor: User, repo: RepoPath, input: DelegateInput): Promise<Result<Issue>>;
829 /** Newest first. */
830 listIssues(
831 repo: RepoPath,
832 viewer: Viewer,
833 filter?: { state?: State; label?: string; milestone?: number },
834 ): Promise<Result<Issue[]>>;
835 getIssue(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<IssueDetail>>;
836 /** The author or a member of the workspace may. */
837 updateIssue(actor: User, repo: RepoPath, number: number, input: UpdateIssueInput): Promise<Result<Issue>>;
838 closeIssue(actor: User, repo: RepoPath, number: number, reason?: IssueReason): Promise<Result<Issue>>;
839 reopenIssue(actor: User, repo: RepoPath, number: number): Promise<Result<Issue>>;
840 /** A repository's labels, by name, each with how many issues and pull requests carry it. */
841 listLabels(repo: RepoPath, viewer: Viewer): Promise<Result<Label[]>>;
842 /**
843 * Creates a label, or with `name` changes one; renaming it renames it on
844 * everything that carries it. Needs the Triage role.
845 */
846 saveLabel(
847 actor: User,
848 repo: RepoPath,
849 label: { name?: string; newName?: string; color?: string; description?: string },
850 ): Promise<Result<Label>>;
851 /** Removes a label from the repository and everything carrying it. */
852 deleteLabel(actor: User, repo: RepoPath, name: string): Promise<Result<boolean>>;
853 /** Adds the default labels the repository does not have yet; returns them all. */
854 addDefaultLabels(actor: User, repo: RepoPath): Promise<Result<Label[]>>;
855 /**
856 * The labels of an issue or a pull request, replaced, added to or taken
857 * from. Labels the repository lacks are created for someone with the
858 * Triage role. Returns its labels now.
859 */
860 setLabels(
861 actor: User,
862 repo: RepoPath,
863 number: number,
864 labels: string[],
865 change?: LabelChange,
866 ): Promise<Result<string[]>>;
867 /** Open ones by due date, then closed ones; both unless `state` says. */
868 listMilestones(repo: RepoPath, viewer: Viewer, state?: State): Promise<Result<Milestone[]>>;
869 getMilestone(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<MilestoneDetail>>;
870 /** Creates a milestone, or with `number` changes the fields given. Needs the Triage role. */
871 saveMilestone(
872 actor: User,
873 repo: RepoPath,
874 milestone: { number?: number; title?: string; description?: string; dueOn?: string; state?: State },
875 ): Promise<Result<Milestone>>;
876 deleteMilestone(actor: User, repo: RepoPath, number: number): Promise<Result<boolean>>;
877 /** How many issues and pull requests are open. */
878 counts(repo: RepoPath, viewer: Viewer): Promise<Result<{ issues: number; pulls: number }>>;
879
880 /**
881 * On an issue or a pull request. On a pull request it may name a line of
882 * the change and carry a verdict; nobody can give a verdict on their own.
883 */
884 addComment(actor: User, repo: RepoPath, number: number, comment: NewComment): Promise<Result<Comment>>;
885
886 /**
887 * Always refused now: a pull request's checks are the workflows run on
888 * it. Kept for a runner from before.
889 */
890 startChecks(pullId: string): Promise<Result<CheckJob>>;
891 /**
892 * What a sandbox says about a run from before checks were workflows: so
893 * one still finishing is recorded.
894 */
895 reportChecks(runId: string, token: string, report: CheckReport): Promise<Result<CheckRun>>;
896 /** Begins a review by a g1t agent. For the runner service. */
897 startReview(pullId: string): Promise<Result<ReviewJob>>;
898 /** Records that a review could not be written. For the runner service. */
899 failReview(runId: string, token: string, error: string): Promise<Result<boolean>>;
900 /**
901 * Claims the probe of whether a pull request merges cleanly that a
902 * `pull.mergecheck` event asked for. Refused when it is no longer wanted
903 * or the repository has as many running as it may. For the runner service.
904 */
905 startMergecheck(pullId: string): Promise<Result<MergecheckJob>>;
906 /** Records that a probe could not be carried out. For the runner service. */
907 failMergecheck(pullId: string, token: string, error: string): Promise<Result<Mergeable>>;
908
909 /**
910 * Works out the next step for a pull request g1t is seeing through and,
911 * if there is one to take now, claims it, so that it is taken once
912 * however often this is called. For the runner service.
913 */
914 advance(pullId: string): Promise<Advance>;
915 /** Records that a step could not be carried out, so a person is asked. */
916 stall(pullId: string, reason: string): Promise<boolean>;
917 /** Ids of the open pull requests g1t is seeing through. */
918 managedPulls(repoId?: string): Promise<string[]>;
919
920 /** A repository's merge queue: what is in it, in order, and what recently left. */
921 queue(repo: RepoPath, viewer: Viewer): Promise<Result<QueueView>>;
922 /**
923 * The next batch of combined states to test for a repository, one per
924 * entry; empty while a batch is being tested or nothing waits.
925 */
926 queueBuild(repoId: string): Promise<QueueJob[]>;
927 /** Reports that a combined state could not be built or checked. */
928 failQueue(entryId: string, token: string, error: string): Promise<Result<QueueState>>;
929 /** Sends the agent working on a pull request a message, for its next step. */
930 messageAgent(actor: User, repo: RepoPath, number: number, body: string): Promise<Result<AgentMessage>>;
931 /** Takes a pull request out of the merge queue. Members only. */
932 removeFromQueue(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
933
934 getSettings(repo: RepoPath, viewer: Viewer): Promise<Result<RepoSettings>>;
935 /** The check names reported on the repository's commits in the last 30 days, most recent first. */
936 seenChecks(repo: RepoPath, viewer: Viewer): Promise<Result<SeenCheck[]>>;
937 /** Members of the repository's workspace only. */
938 updateSettings(actor: User, repo: RepoPath, settings: RepoSettingsInput): Promise<Result<RepoSettings>>;
939 /** The CODEOWNERS file at a branch (the default when left out), checked. Needs Read. */
940 codeownersErrors(repo: RepoPath, viewer: Viewer, ref?: string | null): Promise<Result<CodeownersReport>>;
941 /**
942 * What the runner needs to bring a pull request up to date because a
943 * merge of it was asked for. Null if none was.
944 */
945 catchUpJob(pullId: string): Promise<LifecycleJob | null>;
946 /**
947 * Claims a short step for the agent on a pull request to answer the
948 * questions and handoffs it was sent while not at work, and hands them
949 * over, marked read. Null when there is nothing waiting or it cannot
950 * take a step now.
951 */
952 wakeForMessages(pullId: string): Promise<Wake | null>;
953
954 /**
955 * Opens a pull request: a draft with a fork to push to, or, given a
956 * branch, one ready for review.
957 */
958 openPull(actor: User, repo: RepoPath, input: OpenPullInput): Promise<Result<Pull>>;
959 /** Newest first. */
960 listPulls(
961 repo: RepoPath,
962 viewer: Viewer,
963 state?: State,
964 filter?: { label?: string; milestone?: number; base?: string },
965 ): Promise<Result<Pull[]>>;
966 /**
967 * The newest `limit` open and closed pull requests of each repository,
968 * in one call. Repositories the viewer cannot read, and forks, are left
969 * out: ask those with `listPulls`.
970 */
971 pullsForRepos(repoIds: string[], viewer: Viewer, limit: number): Promise<RepoPulls[]>;
972 getPull(repo: RepoPath, number: number, viewer: Viewer): Promise<Result<PullDetail>>;
973 /**
974 * Changes who a pull request is assigned to and whose review is asked
975 * for; each list given replaces the whole set. Asking for `g1t`'s
976 * review does not by itself start one: the runner's `review` does.
977 */
978 updatePull(
979 actor: User,
980 repo: RepoPath,
981 number: number,
982 changes: {
983 assignees?: string[];
984 reviewers?: string[];
985 labels?: string[];
986 /** 0 takes it out of its milestone. */
987 milestone?: number;
988 /** The branch it merges into. Needs the Write role. */
989 base?: string;
990 },
991 ): Promise<Result<Pull>>;
992 /**
993 * Brings a pull request up to date with the default branch in seconds,
994 * without a sandbox, when the two changed different files: the merge
995 * commit is pushed to its branch as `actor`, who must be whoever opened
996 * it (for a fork) or a member (for a branch). Otherwise `needs_agent`, and
997 * nothing is pushed: the runner's `update` is the way on.
998 */
999 catchUpPull(actor: User, repo: RepoPath, number: number): Promise<Result<PullBranchUpdate>>;
1000 /** Marks a draft ready for review and sets its description. */
1001 readyPull(actor: User, repo: RepoPath, number: number, summary: string): Promise<Result<Pull>>;
1002 closePull(actor: User, repo: RepoPath, number: number): Promise<Result<Pull>>;
1003 /**
1004 * Lands the pull request on the repository's default branch. Unless
1005 * `keepIssueOpen`, that resolves the issue it was for: the issue closes
1006 * naming this pull request, and the others still in progress for it close
1007 * as superseded. Only members of the repository's workspace may merge.
1008 *
1009 * If the default branch has moved, the pull request is brought up to date
1010 * first and lands when that is done; it comes back still open, and
1011 * `PullDetail.landing` is true meanwhile. A repository that requires pull
1012 * requests to be up to date refuses instead.
1013 */
1014 mergePull(
1015 actor: User,
1016 repo: RepoPath,
1017 number: number,
1018 options?: {
1019 keepIssueOpen?: boolean;
1020 /** Merge although required checks have not passed, where the repository allows bypassing them. */
1021 ignoreChecks?: boolean;
1022 },
1023 ): Promise<Result<Pull>>;
1024 /**
1025 * Drafts and open pull requests the viewer started, most recently active
1026 * first, each with where it stands if g1t is seeing it through.
1027 */
1028 listActivePulls(
1029 viewer: Viewer,
1030 ): Promise<{ pull: Pull; issue: Issue | null; lifecycle: Lifecycle | null }[]>;
1031 /**
1032 * The issues and pull requests a person opened, a page at a time, only
1033 * on repositories the viewer may read. Not found for no such account.
1034 */
1035 byAuthor(username: string, viewer: Viewer, filter?: AuthoredFilter): Promise<Result<Authored>>;
1036
1037 /**
1038 * Records an outcome to plan for. Members only. For the runner service,
1039 * which starts the sandbox in which an agent writes the plan.
1040 */
1041 startPlan(actor: User, repo: RepoPath, brief: string): Promise<Result<PlanJob>>;
1042 /** Records that a plan could not be written. For the runner service. */
1043 failPlan(planId: string, token: string, error: string): Promise<Result<boolean>>;
1044 /** Members only. */
1045 getPlan(repo: RepoPath, viewer: Viewer, id: string): Promise<Result<Plan>>;
1046 /** Newest first. Members only. */
1047 listPlans(repo: RepoPath, viewer: Viewer): Promise<Result<Plan[]>>;
1048 /**
1049 * Opens a plan's issues, each blocked by the ones it depends on. With
1050 * `assign`, each is queued for a g1t agent. `keep` holds the positions,
1051 * from 1, of the issues to open; all of them when absent. Once.
1052 */
1053 applyPlan(
1054 actor: User,
1055 repo: RepoPath,
1056 id: string,
1057 options?: { assign?: boolean; keep?: number[] },
1058 ): Promise<Result<Plan>>;
1059 /** Asks for a g1t agent to take an issue as soon as it can, or withdraws that. */
1060 queueIssue(actor: User, repo: RepoPath, number: number, queued: boolean): Promise<Result<boolean>>;
1061 /** Issues waiting for a g1t agent that can be given one now. For the runner. */
1062 readyIssues(repoId?: string): Promise<ReadyIssue[]>;
1063
1064 /** Open issues assigned to the viewer, most recently changed first. */
1065 listAssignedIssues(viewer: Viewer): Promise<Issue[]>;
1066
1067 appendSession(actor: User, repo: RepoPath, number: number, entries: NewSessionEntry[]): Promise<Result<{ count: number }>>;
1068 readSession(repo: RepoPath, number: number, viewer: Viewer, afterSeq?: number): Promise<Result<SessionEntry[]>>;
1069}
1070
1071/**
1072 * What to pass `ReposApi.compare` to see what a pull request changes.
1073 *
1074 * A fork is compared as a whole. A branch is compared by name while the
1075 * pull request is open, and by the commit it was merged or closed at
1076 * afterwards, so later pushes to the branch do not change the record.
1077 */
1078export function pullComparison(pull: Pull): {
1079 repoId: string;
1080 base: string | null;
1081 head: string | null;
1082 /** The branch it merges into, which it is compared from. */
1083 baseBranch: string | null;
1084} {
1085 const baseBranch = pull.base ?? null;
1086 if (pull.forkRepoId) return { repoId: pull.forkRepoId, base: pull.mergeBase, head: null, baseBranch };
1087 const settled = pull.status === "merged" || pull.status === "closed";
1088 return {
1089 repoId: pull.repoId,
1090 base: pull.mergeBase,
1091 head: (settled && pull.headCommit) || pull.branch,
1092 baseBranch,
1093 };
1094}
1095
1096
1097/** Where a pull request in a merge queue stands. */
1098/** Where one issue of an applied plan stands. */
1099export type IssueProgress = {
1100 number: number;
1101 title: string;
1102 /**
1103 * `blocked`, `waiting` (for an agent), `open`, a lifecycle stage,
1104 * `landed` or `closed`.
1105 */
1106 state: string;
1107 detail: string;
1108 /** The issues it is waiting on that are still open. */
1109 blockedBy: number[];
1110 pull: number | null;
1111 agent: string | null;
1112};
1113
1114/** A message a person sent an agent at work on a pull request. */
1115export type AgentMessage = {
1116 id: string;
1117 author: string;
1118 body: string;
1119 createdAt: string;
1120 /** When the agent received it; null until then. */
1121 deliveredAt: string | null;
1122 /** `message` from a person; from an agent a `question`, `handoff` or `answer`. */
1123 kind: "message" | "question" | "handoff" | "answer";
1124 /** The pull request whose agent sent it, when an agent did. */
1125 fromNumber: number | null;
1126 /** The pull request it was sent to. */
1127 toNumber: number;
1128 /** The reply to a question or handoff, once there is one. */
1129 answer: string | null;
1130 declined: boolean;
1131 /** For the sending agent: what to expect when the one it asked is not at work. */
1132 hint?: string;
1133};
1134
1135export type QueueState = "waiting" | "testing" | "passed" | "failed" | "landed" | "removed";
1136
1137/** One pull request's place in a merge queue. */
1138export type QueueEntry = {
1139 id: string;
1140 number: number;
1141 title: string;
1142 agent: string;
1143 state: QueueState;
1144 /** The pull requests merged ahead of it in the state being tested, in order. */
1145 ahead: number[];
1146 baseCommit: string | null;
1147 combinedCommit: string | null;
1148 error: string | null;
1149 results: CheckResult[];
1150 /** Username of whoever merged it into the queue: a person, or `g1t`. */
1151 enqueuedBy: string;
1152 /** RFC 3339. */
1153 createdAt: string;
1154 /** RFC 3339. */
1155 finishedAt: string | null;
1156};
1157
1158/** A repository's merge queue: what is in it, in order, and what recently left. */
1159export type QueueView = {
1160 enabled: boolean;
1161 active: QueueEntry[];
1162 /** Newest first. */
1163 recent: QueueEntry[];
1164};
1165
1166export type QueueStackItem = {
1167 number: number;
1168 title: string;
1169 /** The repository holding the change, and its branch. */
1170 source: RepoPath;
1171 branch: string;
1172 commit: string;
1173};
1174
1175/** What a sandbox needs to build and check one combined state. */
1176export type QueueJob = {
1177 entryId: string;
1178 token: string;
1179 repo: RepoPath;
1180 defaultBranch: string;
1181 baseCommit: string;
1182 branch: string;
1183 stack: QueueStackItem[];
1184 /** Always empty: the state is checked by the merge_group workflows run on it. */
1185 checks: string[];
1186 /** Always empty, as `checks`. */
1187 contractChecks: string[];
1188 actor: User;
1189};
1190
1191// --- A person's work ---------------------------------------------------------
1192// Mirrors the `Authored*` types in `crates/contracts/src/work.rs`.
1193
1194export type AuthoredKind = "issue" | "pull";
1195/** `closed` takes in merged pull requests too; `merged` is only those. */
1196export type AuthoredState = "open" | "closed" | "merged";
1197/** `created` is newest first, `updated` most recently changed, `oldest` oldest first. */
1198export type AuthoredSort = "created" | "updated" | "oldest";
1199
1200/** The most items one `byAuthor` page holds. */
1201export const AUTHORED_PAGE = 25;
1202
1203export type AuthoredFilter = {
1204 kind?: AuthoredKind;
1205 state?: AuthoredState;
1206 /** `namespace/name`. */
1207 repo?: string;
1208 sort?: AuthoredSort;
1209 /** The `next` of the page before. */
1210 before?: string;
1211 limit?: number;
1212};
1213
1214export type AuthoredItem = {
1215 kind: AuthoredKind;
1216 repo: RepoPath;
1217 number: number;
1218 title: string;
1219 /** A merged pull request is closed. */
1220 state: State;
1221 /** A pull request's own status; null on an issue. */
1222 status: PullStatus | null;
1223 /** Why an issue was closed. */
1224 reason: IssueReason | null;
1225 draft: boolean;
1226 merged: boolean;
1227 /** RFC 3339. */
1228 createdAt: string;
1229 /** RFC 3339. */
1230 updatedAt: string;
1231 /** RFC 3339. */
1232 mergedAt: string | null;
1233};
1234
1235/** Over every repository the viewer may read, whatever the filters. */
1236export type AuthoredCounts = {
1237 pullsMerged: number;
1238 pullsOpen: number;
1239 pulls: number;
1240 issues: number;
1241 issuesOpen: number;
1242};
1243
1244export type Authored = {
1245 items: AuthoredItem[];
1246 /** Pass as `before` for the next page; null on the last. */
1247 next: string | null;
1248 counts: AuthoredCounts;
1249 /** The repositories they worked in that the viewer may read, most work first. */
1250 repos: { repo: RepoPath; count: number }[];
1251};