Skip to content
208 linesCodeBlameRaw
1import { Download, Filter } from "lucide-react";
2import { Form, Link, data } from "react-router";
3
4import type { Route } from "./+types/audit";
5import { page } from "../../lib/meta";
6import { AuditTable } from "../../components/audit";
7import { ButtonLink, EmptyState, Field, Input } from "../../components/ui";
8import { Button } from "../../components/ui/button";
9import { Card } from "../../components/ui/card";
10import { SelectField } from "../../components/ui/select";
11import { type AuditFilters, filterHref, parseFilters, toQuery } from "../../lib/audit";
12import { auditPage, auditRetention } from "../../lib/audit.server";
13import { repos } from "../../lib/services.server";
14import { requireUser, roleIn } from "../../lib/session.server";
15
16const PAGE_SIZE = 100;
17
18/** Actions worth offering in the filter; any other can be typed. */
19const COMMON_ACTIONS = [
20 "git.push",
21 "git.fetch",
22 "create_issue",
23 "add_comment",
24 "record_session",
25 "mark_pull_request_ready",
26 "review_pull_request",
27 "merge_pull_request",
28 "remember",
29 "message_agent",
30];
31
32export function meta({ params, ...args }: Route.MetaArgs) {
33 return page(args, { title: `Audit log · ${params.owner} · g1t` });
34}
35
36export async function loader({ params, context, request }: Route.LoaderArgs) {
37 const viewer = requireUser(context, request);
38 const workspace = params.owner.toLowerCase();
39 const role = roleIn(viewer, workspace);
40 if (!role) throw data("Only members of this workspace can read its audit log.", { status: 404 });
41 const filters = parseFilters(new URL(request.url).searchParams);
42 const { visibility: _, ...query } = toQuery(workspace, { kind: "all" }, filters, PAGE_SIZE);
43 const [found, projects, retention] = await Promise.all([
44 auditPage(viewer, query),
45 repos.list(viewer, { namespace: workspace }).catch(() => []),
46 auditRetention(workspace),
47 ]);
48 return {
49 workspace,
50 role,
51 filters,
52 entries: found?.entries ?? [],
53 next: found?.next ?? null,
54 projects: projects.map((repo) => repo.name),
55 retention,
56 };
57}
58
59function FilterField({
60 label,
61 name,
62 value,
63 placeholder,
64 list,
65}: {
66 label: string;
67 name: keyof AuditFilters;
68 value: string;
69 placeholder?: string;
70 list?: string;
71}) {
72 return (
73 <Field label={label}>
74 <Input name={name} defaultValue={value} placeholder={placeholder} list={list} />
75 </Field>
76 );
77}
78
79/** As tall as the inputs beside it. */
80const SELECT = "h-auto py-2";
81
82export default function WorkspaceAudit({ loaderData }: Route.ComponentProps) {
83 const { workspace, role, filters, entries, next, projects, retention } = loaderData;
84 const base = `/${workspace}/-/audit`;
85 const filtered = Object.entries(filters).some(([key, value]) => key !== "before" && value);
86 const exportHref = (format: "csv" | "json") => filterHref(`${base}/export`, { ...filters, before: "" }) + `${filtered ? "&" : "?"}format=${format}`;
87 return (
88 <div>
89 <p className="max-w-3xl text-sm text-muted">
90 Every action taken with an agent run's credentials, reads included, and every change people and
91 workspace tokens make through the API, MCP and git: who did it, on whose behalf, with which
92 credential, to what, and whether it was allowed. Refusals name the rule that refused them.
93 {role === "owner"
94 ? " As an owner you see the whole workspace."
95 : " As a member you see what was done to the workspace's projects, and what was done by you or on your behalf."}
96 {retention != null &&
97 ` The log goes back ${retention} days, and exports the same; older entries are deleted each day.`}
98 </p>
99
100 <Card asChild className="mt-6 p-4">
101 <Form method="get">
102 <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
103 <FilterField label="Actor" name="actor" value={filters.actor} placeholder="A person, or whom an agent worked for" />
104 <FilterField label="Agent" name="agent" value={filters.agent} placeholder="g1t" />
105 <FilterField label="Action" name="action" value={filters.action} placeholder="git.push" list="audit-actions" />
106 <FilterField label="Project" name="project" value={filters.project} placeholder="Any" list="audit-projects" />
107 <Field label="Outcome">
108 <SelectField
109 key={`outcome-${filters.outcome}`}
110 name="outcome"
111 defaultValue={filters.outcome}
112 className={SELECT}
113 options={[
114 { value: "", label: "Any" },
115 { value: "allowed", label: "Allowed" },
116 { value: "denied", label: "Denied" },
117 ]}
118 />
119 </Field>
120 <Field label="Who">
121 <SelectField
122 key={`kind-${filters.kind}`}
123 name="kind"
124 defaultValue={filters.kind}
125 className={SELECT}
126 options={[
127 { value: "", label: "Anyone" },
128 { value: "agent", label: "Agents" },
129 { value: "person", label: "People" },
130 { value: "workspace", label: "Workspace tokens" },
131 ]}
132 />
133 </Field>
134 <Field label="From">
135 <Input type="date" name="from" defaultValue={filters.from} />
136 </Field>
137 <Field label="To">
138 <Input type="date" name="to" defaultValue={filters.to} />
139 </Field>
140 </div>
141 {filters.run && <input type="hidden" name="run" value={filters.run} />}
142 <datalist id="audit-actions">
143 {COMMON_ACTIONS.map((action) => (
144 <option key={action} value={action} />
145 ))}
146 </datalist>
147 <datalist id="audit-projects">
148 {projects.map((name) => (
149 <option key={name} value={name} />
150 ))}
151 </datalist>
152 <div className="mt-4 flex flex-wrap items-center gap-3">
153 <Button type="submit">
154 <Filter size={14} />
155 Filter
156 </Button>
157 {filtered && (
158 <Link to={base} className="text-sm text-muted hover:text-fg">
159 Clear filters
160 </Link>
161 )}
162 {filters.run && (
163 <span className="font-mono text-xs text-muted">
164 Run {filters.run}
165 </span>
166 )}
167 <span className="grow" />
168 <ButtonLink variant="outline" to={exportHref("csv")} reloadDocument>
169 <Download size={14} />
170 CSV
171 </ButtonLink>
172 <ButtonLink variant="outline" to={exportHref("json")} reloadDocument>
173 <Download size={14} />
174 JSON
175 </ButtonLink>
176 </div>
177 </Form>
178 </Card>
179
180 <div className="mt-6">
181 {entries.length === 0 ? (
182 <EmptyState title={filtered ? "Nothing matches these filters" : "Nothing recorded yet"}>
183 {filtered
184 ? "Try a wider time range, or clear the filters."
185 : "Entries appear as agents work and as people change things through the API, MCP and git."}
186 </EmptyState>
187 ) : (
188 <AuditTable entries={entries} base={base} />
189 )}
190 </div>
191
192 {(next || filters.before) && (
193 <div className="mt-4 flex gap-4 text-sm">
194 {filters.before && (
195 <Link to={filterHref(base, { ...filters, before: "" })} className="text-muted hover:text-fg">
196 Newest
197 </Link>
198 )}
199 {next && (
200 <Link to={filterHref(base, filters, { before: next })} className="text-muted hover:text-fg">
201 Older
202 </Link>
203 )}
204 </div>
205 )}
206 </div>
207 );
208}