From 73850911410001660437428c791681e44720ebc9 Mon Sep 17 00:00:00 2001 From: Khaliq Date: Mon, 16 Nov 2026 14:10:19 -0800 Subject: [PATCH 1/2] feat(sessions): badge sessions with their relaygate group label relaygate's groupLabels policy adds `relaygate: {gid, label}` to readable sessions a labeled group owns (list items, and the catalog via relay-control). Session list rows and the session header show the label as a small info badge; a `group:` search token and a "Group" sidebar section (shown only when some loaded session has a label) filter by it in the browser. Malformed values are ignored. Co-Authored-By: Claude Opus 5.5 --- src/sessions/SessionView.tsx | 2 + src/sessions/SessionsPage.tsx | 23 +++++++-- src/sessions/api.ts | 6 ++- src/sessions/data.ts | 20 ++++++++ src/sessions/group-badge.test.tsx | 83 +++++++++++++++++++++++++++++++ src/sessions/group-badge.tsx | 20 ++++++++ src/sessions/query.ts | 17 ++++++- 7 files changed, 165 insertions(+), 6 deletions(-) create mode 100644 src/sessions/group-badge.test.tsx create mode 100644 src/sessions/group-badge.tsx diff --git a/src/sessions/SessionView.tsx b/src/sessions/SessionView.tsx index aee5da6..1283b02 100644 --- a/src/sessions/SessionView.tsx +++ b/src/sessions/SessionView.tsx @@ -12,6 +12,7 @@ import { import { Button, ButtonLink, buttonClass, Skeleton } from "../shell/ui"; import styles from "./sessions.module.css"; import { HarnessIcon } from "./harness-icon"; +import { GroupBadge } from "./group-badge"; import { WorkspaceViewSlot } from "../shell/view-slots"; import { SignedOut } from "../api/client"; import { useLoad } from "./use-load"; @@ -553,6 +554,7 @@ export function SessionView({ {harnessName(source)} +
diff --git a/src/sessions/SessionsPage.tsx b/src/sessions/SessionsPage.tsx index d3d0a2d..f1ddf1c 100644 --- a/src/sessions/SessionsPage.tsx +++ b/src/sessions/SessionsPage.tsx @@ -19,6 +19,7 @@ import { Copy, CirclePause, FolderGit2, + Tag, Inbox, Network, RefreshCw, @@ -81,6 +82,7 @@ import { Button, buttonClass, Skeleton } from "../shell/ui"; import { WorkspaceViewSlot } from "../shell/view-slots"; import styles from "./sessions.module.css"; import { HarnessIcon } from "./harness-icon"; +import { GroupBadge } from "./group-badge"; import { SessionSearch } from "./SessionSearch"; // Hosted session-table-data.ts: status dot colors and harness icons. @@ -366,7 +368,7 @@ function SidebarFilters({ // Each option's count once per change of rows, query, or clock. const counts = useMemo(() => { const totals = new Map(); - const fields = ["person", "is", "harness", "project"]; + const fields = ["person", "is", "harness", "project", "group"]; for (const field of fields) { const values = [ null, @@ -481,6 +483,19 @@ function SidebarFilters({ icon:
); @@ -1045,9 +1060,11 @@ function SessionList({ - {(row.parentSessionIds.length > 0 || + {(row.group || + row.parentSessionIds.length > 0 || row.childSessionCount > 0) && ( - + + {row.parentSessionIds.length > 0 && Subagent} {row.childSessionCount > 0 && ( diff --git a/src/sessions/api.ts b/src/sessions/api.ts index 5ab6ca1..8106d2f 100644 --- a/src/sessions/api.ts +++ b/src/sessions/api.ts @@ -4,7 +4,7 @@ import { z } from "zod"; import { read, ReadError, SignedOut } from "../api/client"; import { sessionAgentNamesEnabled, sessionShortSummariesEnabled } from "../app-path"; import { markAgentFilterNotReady } from "./agent-readiness"; -import { harnessSources, sessionRow, tools, type Member, type SessionRow } from "./data"; +import { harnessSources, sessionGroup, sessionRow, tools, type Member, type SessionGroup, type SessionRow } from "./data"; import { fromDirectCursor, ReadApiUnavailable, @@ -513,6 +513,8 @@ export type SessionCatalog = { children: SessionEdge[]; commitLinks: Record[]; truncated: boolean; + /** The owning relaygate group's label, when there is one. */ + group: SessionGroup | null; }; function edge(value: Record): SessionEdge | null { @@ -543,6 +545,7 @@ export async function sessionCatalog( children: z.array(record), commitLinks: z.array(record).default([]), truncated: z.boolean().default(false), + relaygate: z.unknown().optional(), }) .parse( await read( @@ -558,6 +561,7 @@ export async function sessionCatalog( .filter((row): row is SessionEdge => !!row), commitLinks: body.commitLinks, truncated: body.truncated, + group: sessionGroup(body.relaygate), }; } diff --git a/src/sessions/data.ts b/src/sessions/data.ts index d7317b8..6dfcdfe 100644 --- a/src/sessions/data.ts +++ b/src/sessions/data.ts @@ -40,8 +40,27 @@ export type SessionRow = { totalInputTokens: number | null; totalOutputTokens: number | null; search: { snippet: string | null; matchedBy: string[] } | null; + /** The owning relaygate group's label, when the deployment's policy gives it one. */ + group: SessionGroup | null; }; +/** + * relaygate's label for a session's owning group (`relaygate: {gid, label}`, + * set only by relaygate from its `groupLabels` policy). Display only. + */ +export type SessionGroup = { gid: number; label: string }; + +const GROUP_LABEL = /^[A-Za-z0-9](?:[A-Za-z0-9 ._-]{0,22}[A-Za-z0-9._-])?$/; + +export function sessionGroup(value: unknown): SessionGroup | null { + if (!isRecord(value)) return null; + const { gid, label } = value; + return typeof gid === "number" && Number.isSafeInteger(gid) && gid >= 0 && + typeof label === "string" && GROUP_LABEL.test(label) + ? { gid, label } + : null; +} + // Colors are brand.css tokens, so they follow the light and dark themes. export const tools: Record = { claude: { label: "Claude Code", color: "var(--harness-claude)" }, @@ -154,6 +173,7 @@ export function sessionRow(value: Record): SessionRow { search: search ? { snippet: text(search.snippet), matchedBy: strings(search.matchedBy) } : null, + group: sessionGroup(value.relaygate), }; } diff --git a/src/sessions/group-badge.test.tsx b/src/sessions/group-badge.test.tsx new file mode 100644 index 0000000..cbd4482 --- /dev/null +++ b/src/sessions/group-badge.test.tsx @@ -0,0 +1,83 @@ +import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { sessionGroup, sessionRow } from "./data"; +import { matchesSessionFilters, sessionSuggestions } from "./query"; +import { SessionView } from "./SessionView"; +import { SessionsPage } from "./SessionsPage"; + +const base = { source: "codex", firstTs: "2026-11-10T10:00:00Z", lastTs: "2026-11-10T11:00:00Z", userIds: [], machineIds: [], eventCount: 1 }; +const pit = { ...base, sessionId: "pit-session-1", relaygate: { gid: 22, label: "pit" } }; +const plain = { ...base, sessionId: "plain-session-1" }; +const empty = { items: [], members: [], nextCursor: null, error: false }; + +function route(catalog: Record = {}) { + vi.stubGlobal("fetch", vi.fn(async (input: string | URL) => { + const url = new URL(String(input), "https://example.test"); + if (url.pathname.endsWith("/sessions/activity")) return Response.json({ days: [] }); + if (url.pathname.endsWith("/sessions/output")) return Response.json({ output: null, unsupported: true }); + if (url.pathname.endsWith("/turns")) return Response.json({ turns: [], nextCursor: null }); + if (url.pathname.endsWith("/timing")) return Response.json({ timing: null }); + if (url.pathname.endsWith("/summary")) return Response.json({ summary: { status: "unsupported" } }); + if (url.pathname.endsWith("/catalog")) + return Response.json({ catalog: null, parents: [], children: [], commitLinks: [], truncated: false, ...catalog }); + if (url.pathname.endsWith("/sessions")) return Response.json({ ...empty, items: [pit, plain] }); + if (url.pathname.includes("/sessions/")) return Response.json({ ...empty, events: [] }); + return Response.json({ error: "not_found" }, { status: 404 }); + })); +} + +beforeEach(() => { + window.__AGENT_RELAY_DASHBOARD_CONFIG__ = { sessionReadApi: false }; + window.history.replaceState(null, "", "/"); +}); +afterEach(() => { vi.unstubAllGlobals(); delete window.__AGENT_RELAY_DASHBOARD_CONFIG__; }); + +it("reads only a well-formed relaygate group label", () => { + expect(sessionRow(pit).group).toEqual({ gid: 22, label: "pit" }); + expect(sessionRow(plain).group).toBeNull(); + for (const bad of [null, "pit", { gid: "22", label: "pit" }, { gid: -1, label: "pit" }, { gid: 22, label: "" }, { gid: 22, label: "pit" }, { gid: 22, label: "x".repeat(25) }]) { + expect(sessionGroup(bad)).toBeNull(); + } +}); + +it("filters rows by group: and offers only labels present on loaded rows", () => { + const rows = [sessionRow(pit), sessionRow(plain)]; + const now = Date.parse("2026-11-16T00:00:00Z"); + expect(rows.filter((row) => matchesSessionFilters(row, "group:PIT", now)).map((row) => row.sessionId)).toEqual(["pit-session-1"]); + expect(rows.filter((row) => matchesSessionFilters(row, "-group:pit", now)).map((row) => row.sessionId)).toEqual(["plain-session-1"]); + expect(sessionSuggestions(rows, [], now).filter((s) => s.field === "group")).toEqual([{ field: "group", value: "pit", title: "pit", section: "Groups" }]); + expect(sessionSuggestions([sessionRow(plain)], [], now).some((s) => s.field === "group")).toBe(false); +}); + +it("badges labeled rows in the session list and filters by group from the sidebar", async () => { + route(); + render( undefined} />); + const row = (await screen.findByRole("link", { name: /pit-session-1/ })).closest("tr")!; + expect(within(row).getByLabelText("Group: pit")).toHaveTextContent("pit"); + const other = screen.getByRole("link", { name: /plain-session-1/ }).closest("tr")!; + expect(within(other).queryByLabelText(/^Group:/)).not.toBeInTheDocument(); + fireEvent.click(within(screen.getByRole("group", { name: "Filter by group" })).getByRole("button", { name: "pit" })); + await waitFor(() => expect(screen.queryByRole("link", { name: /plain-session-1/ })).not.toBeInTheDocument()); + expect(screen.getByRole("link", { name: /pit-session-1/ })).toBeVisible(); +}); + +it("hides the group filter when no session has a label", async () => { + vi.stubGlobal("fetch", vi.fn(async () => Response.json({ ...empty, items: [plain], days: [] }))); + render( undefined} />); + await screen.findByRole("link", { name: /plain-session-1/ }); + expect(screen.queryByRole("group", { name: "Filter by group" })).not.toBeInTheDocument(); +}); + +it("badges the session header from the catalog's relaygate label", async () => { + route({ relaygate: { gid: 22, label: "pit" } }); + render(); + const identity = await screen.findByRole("group", { name: "Session identity" }); + expect(within(identity).getByLabelText("Group: pit")).toHaveTextContent("pit"); +}); + +it("shows no header badge without a label", async () => { + route(); + render(); + const identity = await screen.findByRole("group", { name: "Session identity" }); + expect(within(identity).queryByLabelText(/^Group:/)).not.toBeInTheDocument(); +}); diff --git a/src/sessions/group-badge.tsx b/src/sessions/group-badge.tsx new file mode 100644 index 0000000..c2c3891 --- /dev/null +++ b/src/sessions/group-badge.tsx @@ -0,0 +1,20 @@ +import { Badge } from "../shell/ui"; +import type { SessionGroup } from "./data"; + +/** + * The owning relaygate group's label (for example "pit"), from the + * deployment's policy. Display only: it says which group owns the session. + */ +export function GroupBadge({ group, className }: { group: SessionGroup | null; className?: string }) { + if (!group) return null; + return ( + + {group.label} + + ); +} diff --git a/src/sessions/query.ts b/src/sessions/query.ts index 413b2d3..8ca6dc3 100644 --- a/src/sessions/query.ts +++ b/src/sessions/query.ts @@ -1,5 +1,6 @@ // Search tokens, ported from the hosted dashboard's session-query.ts: -// `harness:`, `project:`, `is:`, `person:`, `updated:`, `created:`, with a +// `harness:`, `project:`, `is:`, `person:`, `updated:`, `created:` (and +// `group:`, a relaygate group label), with a // leading `-` to negate. Free text goes to RelayHistory; tokens filter rows. import type { ListFilters } from "./api"; import { @@ -60,7 +61,7 @@ export function parseSessionQuery(query: string, { agents = true }: QueryOptions continue; } const match = raw.match( - /^(-?)(harness|project|is|person|updated|created):(.*)$/i, + /^(-?)(harness|project|is|person|updated|created|group):(.*)$/i, ); if (!match) { words.push(raw); @@ -158,6 +159,9 @@ export function matchesSessionFilters( case "created": matches = compareDate(row.firstTs, value); break; + case "group": + matches = row.group?.label.toLowerCase() === v; + break; } // An incomplete date is still being typed; don't hide rows for it. if (matches === null) return true; @@ -269,6 +273,15 @@ export function sessionSuggestions( title, section: "Projects", })), + // Only labels relaygate put on loaded rows; no group is offered otherwise. + ...[...new Set(rows.flatMap((row) => (row.group ? [row.group.label] : [])))] + .sort((a, b) => a.localeCompare(b)) + .map((label) => ({ + field: "group", + value: label, + title: label, + section: "Groups", + })), ...members.map((member) => ({ field: "person", value: member.id, From e92d7bfbfb34d633a1c4057c9eada65d704d973c Mon Sep 17 00:00:00 2001 From: Khaliq Date: Mon, 16 Nov 2026 14:15:06 -0800 Subject: [PATCH 2/2] fix(sessions): complete multiword group labels from an unquoted prefix Co-Authored-By: Claude Opus 5.5 --- src/sessions/group-badge.test.tsx | 9 ++++++++- src/sessions/query.ts | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/sessions/group-badge.test.tsx b/src/sessions/group-badge.test.tsx index cbd4482..7c87228 100644 --- a/src/sessions/group-badge.test.tsx +++ b/src/sessions/group-badge.test.tsx @@ -1,7 +1,7 @@ import { fireEvent, render, screen, waitFor, within } from "@testing-library/react"; import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { sessionGroup, sessionRow } from "./data"; -import { matchesSessionFilters, sessionSuggestions } from "./query"; +import { matchesSessionFilters, matchesSuggestionFragment, sessionSuggestions } from "./query"; import { SessionView } from "./SessionView"; import { SessionsPage } from "./SessionsPage"; @@ -49,6 +49,13 @@ it("filters rows by group: and offers only labels present on loaded rows", () => expect(sessionSuggestions([sessionRow(plain)], [], now).some((s) => s.field === "group")).toBe(false); }); +it("completes a multiword group label from an unquoted prefix", () => { + const team = { field: "group", value: "Team Alpha", title: "Team Alpha", section: "Groups" }; + expect(matchesSuggestionFragment(team, "group:Tea")).toBe(true); + expect(matchesSuggestionFragment(team, "group:pit")).toBe(false); + expect(matchesSuggestionFragment(team, "harness:Tea")).toBe(false); +}); + it("badges labeled rows in the session list and filters by group from the sidebar", async () => { route(); render( undefined} />); diff --git a/src/sessions/query.ts b/src/sessions/query.ts index 8ca6dc3..629cfdd 100644 --- a/src/sessions/query.ts +++ b/src/sessions/query.ts @@ -179,13 +179,16 @@ export type SearchSuggestion = { export const suggestionToken = (s: SearchSuggestion) => `${s.field}:${/\s/.test(s.value) ? JSON.stringify(s.value) : s.value}`; -/** Selfhost stores IDs while showing project/person labels in the picker. */ +/** + * Selfhost stores IDs while showing project/person labels in the picker; a + * group label with a space is quoted in its token, so match its parsed value. + */ export function matchesSuggestionFragment( suggestion: SearchSuggestion, fragment: string, ) { const partial = parseSessionQuery(fragment).filters[0]; - if (partial && (suggestion.field === "project" || suggestion.field === "person")) { + if (partial && (suggestion.field === "project" || suggestion.field === "person" || suggestion.field === "group")) { return ( partial.field === suggestion.field && !partial.exclude && [suggestion.value, suggestion.title].some((value) =>