g1t/packages/contracts/src/work.ts

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