feat(judgement,approvals,ui): action classes are records with a level, history and reset; the precedent guard reads the class level (#2003)

This commit is contained in:
sam committed 2026-11-16 04:35:12 +11:00
1 parent 5712c9c4e6
commit a91df7ec1f
15 files changed
+900 -16

No files matched your search

+1
View File
@@ -34,6 +34,7 @@ function signOut() {
<nav v-if="signedIn" class="flex flex-1 items-center gap-3 text-label">
<RouterLink to="/composer" active-class="text-foreground" class="text-muted-foreground">Composer</RouterLink>
<RouterLink to="/runs" active-class="text-foreground" class="text-muted-foreground">Runs</RouterLink>
<RouterLink to="/classes" active-class="text-foreground" class="text-muted-foreground">Classes</RouterLink>
<RouterLink to="/settings" active-class="text-foreground" class="text-muted-foreground">Settings</RouterLink>
<Button variant="ghost" size="xs" class="ml-auto" @click="signOut">Sign out</Button>
</nav>
+156
View File
@@ -0,0 +1,156 @@
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from "vue";
import { useRouter } from "vue-router";
import PageHeader from "@/base/PageHeader.vue";
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { ReadError } from "@/components/ui/read-error";
import { Section } from "@/components/ui/section";
import { Spinner } from "@/components/ui/spinner";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { Timestamp } from "@/components/ui/timestamp";
import { type ClassDetail, classDetail, resetClass, Unauthorized } from "./api";
import { className, levelLabel } from "./state";
/** One action class: its level, and every decision made in it. */
const props = defineProps<{ id: string }>();
const emit = defineEmits<{ unauthorized: [] }>();
const router = useRouter();
const detail = ref<ClassDetail>();
const loading = ref(true);
const error = ref("");
const confirming = ref(false);
const resetting = ref(false);
let controller = new AbortController();
async function load() {
controller.abort();
controller = new AbortController();
const signal = controller.signal;
loading.value = true;
error.value = "";
try {
const got = await classDetail(props.id, signal);
if (!signal.aborted) detail.value = got;
} catch (err) {
fail(err, signal);
} finally {
if (!signal.aborted) loading.value = false;
}
}
async function reset() {
resetting.value = true;
error.value = "";
try {
await resetClass(props.id);
await load();
} catch (err) {
fail(err, controller.signal);
} finally {
resetting.value = false;
}
}
function fail(err: unknown, signal: AbortSignal) {
if (signal.aborted) return;
if (err instanceof Unauthorized) emit("unauthorized");
error.value = err instanceof Error ? err.message : String(err);
}
watch(() => props.id, load, { immediate: true });
onBeforeUnmount(() => controller.abort());
</script>
<template>
<div>
<Spinner v-if="loading && !detail" />
<ReadError v-else-if="error && !detail" :message="error" retry-label="Retry" @retry="load" />
<template v-else-if="detail">
<PageHeader :title="className(detail)">
<Badge variant="outline">{{ levelLabel(detail.level) }}</Badge>
<Button variant="outline" size="sm" :disabled="resetting || detail.level === detail.default" @click="confirming = true">
Reset to {{ levelLabel(detail.default).toLowerCase() }}
</Button>
</PageHeader>
<ReadError v-if="error" :message="error" retry-label="Retry" @retry="load" />
<Section title="History">
<Empty v-if="detail.history.length === 0">
<EmptyHeader><EmptyTitle>No decisions yet</EmptyTitle></EmptyHeader>
</Empty>
<Table v-else>
<TableHeader>
<TableRow>
<TableHead>Target</TableHead>
<TableHead>Level</TableHead>
<TableHead>Approval</TableHead>
<TableHead>When</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow
v-for="h in detail.history"
:key="h.decision_id"
class="cursor-pointer"
tabindex="0"
@click="router.push({ name: 'run', params: { id: h.run_id } })"
@keydown.enter="router.push({ name: 'run', params: { id: h.run_id } })"
>
<TableCell>{{ h.target }}</TableCell>
<TableCell>{{ h.refused_by ? `Refused by ${h.refused_by}` : levelLabel(h.level) }}</TableCell>
<TableCell>{{ h.approval }}<template v-if="h.answered_by"> by {{ h.answered_by }}</template></TableCell>
<TableCell><Timestamp :at="h.at" /></TableCell>
</TableRow>
</TableBody>
</Table>
</Section>
<Section v-if="detail.resets.length > 0" title="Resets" class="mt-4">
<Table>
<TableHeader>
<TableRow>
<TableHead>From</TableHead>
<TableHead>To</TableHead>
<TableHead>By</TableHead>
<TableHead>When</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="r in detail.resets" :key="r.at">
<TableCell>{{ levelLabel(r.from) }}</TableCell>
<TableCell>{{ levelLabel(r.to) }}</TableCell>
<TableCell>{{ r.by }}</TableCell>
<TableCell><Timestamp :at="r.at" /></TableCell>
</TableRow>
</TableBody>
</Table>
</Section>
<AlertDialog :open="confirming" @update:open="(v: boolean) => (confirming = v)">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Reset class?</AlertDialogTitle>
<AlertDialogDescription>
{{ className(detail) }} returns to {{ levelLabel(detail.default).toLowerCase() }}. Its history stays.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction @click="reset">Reset</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>
</div>
</template>
+74
View File
@@ -0,0 +1,74 @@
<script setup lang="ts">
import { onBeforeUnmount, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import PageHeader from "@/base/PageHeader.vue";
import { Badge } from "@/components/ui/badge";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { ReadError } from "@/components/ui/read-error";
import { Spinner } from "@/components/ui/spinner";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
import { Timestamp } from "@/components/ui/timestamp";
import { type ActionClass, listClasses, Unauthorized } from "./api";
import { levelLabel } from "./state";
const emit = defineEmits<{ unauthorized: [] }>();
const router = useRouter();
const classes = ref<ActionClass[]>([]);
const loading = ref(true);
const error = ref("");
const controller = new AbortController();
async function load() {
loading.value = true;
error.value = "";
try {
classes.value = await listClasses(controller.signal);
} catch (err) {
if (controller.signal.aborted) return;
if (err instanceof Unauthorized) emit("unauthorized");
error.value = err instanceof Error ? err.message : String(err);
} finally {
loading.value = false;
}
}
const open = (id: string) => router.push({ name: "class", params: { id } });
onMounted(load);
onBeforeUnmount(() => controller.abort());
</script>
<template>
<div>
<PageHeader title="Classes" />
<Spinner v-if="loading" />
<ReadError v-else-if="error" :message="error" retry-label="Retry" @retry="load" />
<Empty v-else-if="classes.length === 0">
<EmptyHeader><EmptyTitle>No action classes yet</EmptyTitle></EmptyHeader>
</Empty>
<Table v-else>
<TableHeader>
<TableRow>
<TableHead>Action</TableHead>
<TableHead>Target</TableHead>
<TableHead>Environment</TableHead>
<TableHead>Level</TableHead>
<TableHead>Decisions</TableHead>
<TableHead>Last</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="c in classes" :key="c.id" class="cursor-pointer" tabindex="0" @click="open(c.id)" @keydown.enter="open(c.id)">
<TableCell>{{ c.connector }} {{ c.action }}</TableCell>
<TableCell>{{ c.target_kind }}</TableCell>
<TableCell>{{ c.environment }}</TableCell>
<TableCell><Badge variant="outline">{{ levelLabel(c.level) }}</Badge></TableCell>
<TableCell>{{ c.decisions }}</TableCell>
<TableCell><Timestamp v-if="c.last_decided_at" :at="c.last_decided_at" /></TableCell>
</TableRow>
</TableBody>
</Table>
</div>
</template>
+48
View File
@@ -174,6 +174,54 @@ export async function answerApproval(id: string, approved: boolean): Promise<voi
throw new Error(body.error ?? `${res.status} ${res.statusText}`);
}
}
export type Level = "act_quietly" | "act_and_tell" | "propose_and_wait" | "escalate" | "never";
export interface ActionClass {
id: string;
connector: string;
action: string;
target_kind: string;
environment: string;
level: Level;
default: Level;
decisions: number;
last_decided_at?: string;
reset_at?: string;
}
export interface HistoryEntry {
decision_id: string;
run_id: string;
target: string;
level: Level;
refused_by?: string;
approval?: "pending" | "approved" | "denied" | "expired";
answered_by?: string;
at: string;
}
export interface ClassDetail extends ActionClass {
history: HistoryEntry[];
resets: { from: Level; to: Level; by: string; at: string }[];
}
export const listClasses = (signal?: AbortSignal) => get<ActionClass[]>("/api/classes", signal);
export const classDetail = (id: string, signal?: AbortSignal) =>
get<ClassDetail>(`/api/classes/${encodeURIComponent(id)}`, signal);
/** Returns an action class to its default level; its history stays. */
export async function resetClass(id: string): Promise<void> {
const res = await fetch(`/api/classes/${encodeURIComponent(id)}/reset`, {
method: "POST",
headers: { Authorization: `Bearer ${token()}` },
});
if (res.status === 401) throw new Unauthorized("The admin token was refused.");
if (!res.ok) {
const body = (await res.json().catch(() => ({}))) as { error?: string };
throw new Error(body.error ?? `${res.status} ${res.statusText}`);
}
}
export const listRuns = (signal?: AbortSignal) => get<Run[]>("/api/runs", signal);
export const runTrail = (id: string, signal?: AbortSignal) => get<Trail>(`/api/runs/${encodeURIComponent(id)}`, signal);
+2
View File
@@ -6,6 +6,8 @@ export default createRouter({
{ path: "/", redirect: "/composer" },
{ path: "/composer", name: "composer", component: () => import("./ComposerView.vue") },
{ path: "/runs", name: "runs", component: () => import("./RunsView.vue") },
{ path: "/classes", name: "classes", component: () => import("./ClassesView.vue") },
{ path: "/classes/:id", name: "class", component: () => import("./ClassView.vue"), props: true },
{ path: "/settings", name: "settings", component: () => import("./SettingsView.vue") },
{ path: "/runs/:id", name: "run", component: () => import("./RunView.vue"), props: true },
],
+15 -1
View File
@@ -1,4 +1,4 @@
import type { Run } from "./api";
import type { Level, Run } from "./api";
/** The StatusDot tone for a run's state. */
export function runTone(state: Run["state"]): "ok" | "warn" | "danger" | "info" | "idle" {
@@ -16,3 +16,17 @@ export function runTone(state: Run["state"]): "ok" | "warn" | "danger" | "info"
return "idle";
}
}
const levelLabels: Record<Level, string> = {
act_quietly: "Act quietly",
act_and_tell: "Act and tell",
propose_and_wait: "Propose and wait",
escalate: "Escalate",
never: "Never",
};
export const levelLabel = (level: Level): string => levelLabels[level];
/** The class's own names, as one line. */
export const className = (c: { connector: string; action: string; target_kind: string; environment: string }): string =>
`${c.connector} ${c.action} on ${c.target_kind} in ${c.environment || "no environment"}`;