g1t/packages/contracts/src/events.ts

255 lines11,278 bytesCodeBlame
1/**
2 * Every state change in g1t is published as an event. Services react to
3 * each other through events rather than direct calls, and the same stream
4 * feeds timelines, webhooks and workflows.
5 *
6 * The envelope follows CloudEvents: `type` says what happened, `subject`
7 * says to what, `data` is the type-specific payload.
8 */
9
10import type { RepoRole } from "./access";
11import type { Confidence, Verdict } from "./work";
12
13/** The payload of the `repo.collaborator_*` events. */
14export type RepoCollaboratorData = {
15 repoId: string;
16 namespace: string;
17 name: string;
18 username: string;
19 role: RepoRole | null;
20 previousRole: RepoRole | null;
21};
22export type EventPayloads = {
23 "repo.created": { repoId: string; namespace: string; name: string; isPrivate: boolean };
24 "repo.forked": { repoId: string; sourceRepoId: string; pullId: string };
25 /**
26 * A repository's description, topics or visibility changed.
27 * `visibilityChanged` says whether it went public or private, which
28 * `repo.visibility_changed` also announces on its own.
29 */
30 "repo.updated": { repoId: string; namespace: string; name: string; isPrivate: boolean; visibilityChanged: boolean };
31 "repo.visibility_changed": { repoId: string; isPrivate: boolean };
32 /**
33 * A repository's name changed within its workspace `namespace`, from
34 * `from` to `to`, keeping its id. A path change like `repo.transferred`:
35 * services move rows kept under its path to its *current* path (see
36 * `repoMove`, `currentMovedPath`).
37 */
38 "repo.renamed": { repoId: string; namespace: string; from: string; to: string };
39 /**
40 * A repository was deleted. It is hidden and git refuses it, but it can
41 * be restored until `purgeAfter`: services stop what runs for it and hide
42 * it, and keep their rows until `repo.purged`.
43 */
44 "repo.deleted": { repoId: string; namespace: string; name: string; isPrivate: boolean; purgeAfter: string };
45 /** A deleted repository is back, as it was. Services start again what they stopped. */
46 "repo.restored": { repoId: string; namespace: string; name: string; isPrivate: boolean };
47 /**
48 * A deleted repository is gone for good, its git data with it. Services
49 * drop every row they keep for it, except a workspace's history (ledgers,
50 * invoices, the audit log).
51 */
52 "repo.purged": { repoId: string; namespace: string; name: string };
53 /**
54 * A repository was archived (read-only: pushes, merges, agents and
55 * workflows refused; issues and pull requests locked; deployments keep
56 * serving), or unarchived.
57 */
58 "repo.archived": { repoId: string; namespace: string; name: string; archived: true };
59 "repo.unarchived": { repoId: string; namespace: string; name: string; archived: false };
60 /** The default branch is now `to`; `renamed` when `from` was renamed to it. */
61 "repo.default_branch_changed": { repoId: string; from: string; to: string; renamed: boolean };
62 /** A branch was renamed. Pull requests from it follow. */
63 "branch.renamed": { repoId: string; from: string; to: string; defaultBranch: boolean };
64 /** An account was made, or changed what its profile shows. Ask identity for the profile. */
65 "user.updated": { username: string };
66 /** A workspace was made, or its name, description or icon changed. */
67 "workspace.updated": { workspaceId: string; slug: string };
68 /** Someone, or g1t staff, made an invite. Never the code or the address. */
69 "invite.created": { inviteId: string; inviterId: string | null; workspaceId: string | null; bound: boolean };
70 /** An invite was used: by a new account, or by an account joining a workspace. */
71 "invite.redeemed": {
72 inviteId: string;
73 userId: string;
74 inviterId: string | null;
75 workspaceId: string | null;
76 createdAccount: boolean;
77 };
78 /** Someone asked for access while registration is invite-only. The address is not in the event. */
79 "waitlist.requested": { entryId: string };
80 /**
81 * One branch moved by a push. `ref` is the full ref, `after` the commit it
82 * points to now, and `defaultBranch` whether it is the default branch.
83 */
84 "git.push": { repoId: string; ref: string; after: string; defaultBranch: boolean };
85 /**
86 * `author` is who opened it: g1t, for one its agent filed while at work,
87 * with `requestedBy` the person it was working for. Every issue and pull
88 * request event carries both.
89 */
90 "issue.opened": {
91 issueId: string;
92 repoId: string;
93 number: number;
94 title: string;
95 author?: { id: string; username: string };
96 requestedBy?: { id: string; username: string };
97 };
98 "issue.updated": { issueId: string; repoId: string; number: number };
99 /** The people an issue is assigned to changed; `assignees` is the new set. */
100 "issue.assigned": { issueId: string; repoId: string; number: number; assignees: string[] };
101 /** `resolvedBy` is the number of the pull request whose merge closed it. */
102 "issue.closed": {
103 issueId: string;
104 repoId: string;
105 number: number;
106 reason: "completed" | "not_planned";
107 resolvedBy?: number;
108 };
109 "issue.reopened": { issueId: string; repoId: string; number: number };
110 /**
111 * `issue` is the number of the issue the pull request is for. `author` is
112 * who opened it: g1t, for a change g1t made, with `requestedBy` the person
113 * who asked for it.
114 */
115 "pull.opened": {
116 pullId: string;
117 repoId: string;
118 number: number;
119 issue?: number;
120 agent: string;
121 author?: { id: string; username: string };
122 requestedBy?: { id: string; username: string };
123 };
124 /** `confidence`, on a g1t agent's change once g1t has worked it out, is on every pull request event. */
125 "pull.ready": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
126 /** A push moved the head of a pull request that is ready for review. */
127 "pull.updated": { pullId: string; repoId: string; number: number; issue?: number; commit: string; confidence?: Confidence };
128 /** A merge was asked for while the pull request was behind; it has to catch up first. */
129 "pull.merge_requested": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
130 "pull.closed": { pullId: string; repoId: string; number: number; issue?: number; confidence?: Confidence };
131 /**
132 * The pull request's head or its target moved and the files both changed
133 * overlap: a sandbox should find out whether it still merges cleanly.
134 * `commit` is its head.
135 */
136 "pull.mergecheck": { pullId: string; repoId: string; number: number; issue?: number; commit: string };
137 /** Whether the pull request merges cleanly was settled. */
138 "pull.mergeability": { pullId: string; repoId: string; number: number; issue?: number };
139 /** Another agent asked the agent on a pull request, which was not at work, a question or handed it work. */
140 "agent.asked": { pullId: string; repoId: string; number: number; issue?: number };
141 "pull.merged": { pullId: string; repoId: string; number: number; issue?: number; commit: string; confidence?: Confidence };
142 /** A run of the acceptance checks finished. `commit` is what was checked. */
143 "checks.completed": {
144 pullId: string;
145 repoId: string;
146 number: number;
147 status: "passed" | "failed" | "errored";
148 commit: string;
149 };
150 /** A g1t agent finished reviewing a pull request; no verdict if it could not. */
151 "review.completed": {
152 pullId: string;
153 repoId: string;
154 number: number;
155 verdict?: "approve" | "request_changes";
156 };
157 /** A person was given a role on one repository directly, had it changed, or lost it. `role` is null once removed. */
158 "repo.collaborator_added": RepoCollaboratorData;
159 "repo.collaborator_removed": RepoCollaboratorData;
160 "repo.collaborator_role_changed": RepoCollaboratorData;
161 /** A repository's merge queue gained, lost or settled an entry. */
162 "queue.changed": { repoId: string };
163 /** `number` is the issue or pull request commented on. */
164 "comment.created": {
165 commentId: string;
166 repoId: string;
167 number: number;
168 /** Set when the comment is on a pull request. */
169 pullId?: string;
170 /** Set when the comment is a review. */
171 verdict?: Verdict;
172 };
173 "session.appended": { pullId: string; repoId: string; number: number; count: number };
174 /**
175 * A workspace's slug changed from `from` to `to`. Services that store a
176 * slug move their rows to the workspace's *current* slug (see
177 * `currentWorkspaceSlug`), so a repeated or late delivery after a second
178 * rename still lands in the right place.
179 */
180 "workspace.renamed": { workspaceId: string; from: string; to: string };
181 /**
182 * A repository moved from workspace `from` to `to`, keeping its id and
183 * name. Services that store its path or its workspace's slug move those
184 * rows to its *current* path (ask repos `path_by_id`), so a repeated or
185 * late delivery after a second transfer still lands in the right place.
186 */
187 "repo.transferred": { repoId: string; name: string; from: string; to: string };
188 /**
189 * A workspace is gone. Services drop what they keep for it alone; ledgers,
190 * invoices and the audit log stay under its slug, which is never reused.
191 */
192 "workspace.deleted": { workspaceId: string; slug: string };
193 /**
194 * A memory was added, changed, reviewed or forgotten. No text: ask the
195 * work service for it by id. `repoId` is the project's, or null for the
196 * workspace's memory. `status` is `deleted` once forgotten.
197 */
198 "memory.changed": { memoryId: string; workspace: string; status: "candidate" | "kept" | "dismissed" | "deleted" };
199 /**
200 * A sandbox was stopped because it looked like it was mining: CPU pinned
201 * with little I/O and no progress, or a miner seen by name. For g1t's
202 * staff, in sudo; published with no `repoId` so it never reaches a
203 * repository's timeline or webhooks. `metrics` is what the sandbox
204 * measured (`Verdict` in crates/runner abuse.rs), or null if it could
205 * not say.
206 */
207 "abuse.flagged": {
208 workspace: string;
209 repo: string | null;
210 /** The agent run, when the sandbox had one. */
211 run: string | null;
212 /** What the sandbox was for: agent, checks, queue, actions, deploy... */
213 kind: string;
214 sandbox: string;
215 metrics: Record<string, unknown> | null;
216 };
217};
218
219export type EventType = keyof EventPayloads;
220
221export type G1tEvent<T extends EventType = EventType> = {
222 [K in T]: {
223 id: string;
224 type: K;
225 /** The service that published it. */
226 source: string;
227 /** RFC 3339. */
228 time: string;
229 /** The repo the event concerns, used to scope timelines and deliveries. */
230 repoId: string | null;
231 /** The user or agent that caused it, if any. */
232 actor: string | null;
233 data: EventPayloads[K];
234 };
235}[T];
236
237/** What a publisher supplies; the bus fills in `id` and `time`. */
238export type NewEvent<T extends EventType = EventType> = {
239 [K in T]: Omit<G1tEvent<K>, "id" | "time">;
240}[T];
241
242export type EventQuery = {
243 repoId?: string;
244 types?: EventType[];
245 /** Return events older than this event id. */
246 before?: string;
247 limit?: number;
248};
249
250/** The event bus and its durable log. */
251export interface EventsApi {
252 publish(events: NewEvent[]): Promise<void>;
253 /** Newest first. */
254 list(query: EventQuery): Promise<G1tEvent[]>;
255}