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