8000 feat(shell): top-bar escalation alert for silently wedged agents by simon-inkie · Pull Request #10 · simon-inkie/inkie-deck · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 170 additions & 7 deletions packages/deck/app/components/AppShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { useEffect, useState } from "react";
import { Outlet, Link, NavLink, useLocation, useParams } from "react-router";
import { Bell, CalendarClock, KanbanSquare, MessageSquare, SquareStack, TerminalSquare, X } from "lucide-react";
import { Bell, CalendarClock, KanbanSquare, MessageSquare, SquareStack, TerminalSquare, TriangleAlert, X } from "lucide-react";
import { Sidebar, type SidebarAgent } from "./Sidebar";
import {
ResizablePanelGroup,
Expand All @@ -31,7 +31,8 @@ import type { MiniCard } from "~/lib/projects.shared";
import { Avatar } from "./ui/avatar";
import { Badge } from "./ui/badge";
import { cn } from "~/lib/utils";
import { formatRelative } from "~/lib/time";
import { formatRelative, formatDurationHm } from "~/lib/time";
import { statusLabel, type EscalatedAgent } from "~/lib/activity.shared";
import { useLiveRevalidate } from "~/lib/use-live-revalidate";
import { useMessageChime } from "~/lib/message-chime";
import {
Expand Down Expand Up @@ -115,9 +116,18 @@ interface AppShellProps {
* not the old duplicate terminal-browsing surface that link used to open.
*/
activityNavOn?: boolean;
/**
* Top-bar escalation: agents `stuck`/`needs-you` for longer than the
* escalation threshold (see escalatedAgents, activity.shared.ts).
* Distinct top-bar element from the needs-you bell, deliberately - the two
* concepts (the operator has something to action vs. an agent has been
* silently wedged for a while) must not blur into one badge count.
* Empty/absent renders nothing, same posture as workCards.
*/
escalated?: EscalatedAgent[];
}

export function AppShell({ agents, needsItems, needsCount, workCards, tmuxOn = true, instanceLabel, modelSelector, cronAdminOn = false, activityNavOn = false }: AppShellProps) {
export function AppShell({ agents, needsItems, needsCount, workCards, tmuxOn = true, instanceLabel, modelSelector, cronAdminOn = false, activityNavOn = false, escalated = [] }: AppShellProps) {
const params = useParams();
const active = params.agent;
const location = useLocation();
Expand Down Expand Up @@ -152,9 +162,53 @@ export function AppShell({ agents, needsItems, needsCount, workCards, tmuxOn = t

return (
<div className="flex h-screen flex-col">
{/* Top bar */}
<header className="flex h-14 shrink-0 items-center justify-between border-b border-border bg-card/60 px-4 backdrop-blur-sm">
<div className="flex min-w-0 items-center gap-4">
{/* Top bar.

WRAPPING, and why the header is not `h-14` any more (TASK-783).
The escalation chip is ~176px of deliberately unshrinkable text
(whitespace-nowrap, and truncating it would throw away the duration,
which is the whole point of the chip). Added to the right-hand
cluster that already carries the files toggle and the bell, it took
the cluster from 132px to 264px, which on a phone left the LEFT group
a box far narrower than its own contents. `min-w-0` lets that box
shrink, but the avatar and the chat/terminal toggle inside it are
`shrink-0`, so they did not shrink with it - they OVERFLOWED their
box and were painted underneath the right-hand cluster. Measured in
Chromium against the real stylesheet (see the e2e layout test): at
375px the toggle overlapped the chip by 58px and the thread title was
crushed to zero width; at 320px the whole toggle sat under the chip
(78px, i.e. all of it) and was therefore unclickable, since the chip
is later in DOM order and paints on top. That is strictly worse than
the bare-icon bug the visible reason set out to fix, and it is the
ONE surface the operator reads on a phone.

So the header wraps: `flex-wrap` plus a `gap-x-2` (the gap is what
makes a `w-full` item exceed the line and start a new one), and
`min-h-14` instead of `h-14` so a second row makes the bar taller
rather than spilling over the content below. `min-h-` and not a
padded `h-`: measured, the height is byte-identical to the old fixed
`h-14` at every width where nothing wraps, INCLUDING the 768-1023
band where the left group is already 56px tall and a vertical
padding would have pushed the bar to 65px for a case that has
nothing to do with escalation.

`flex-1` on this left group is the other half: with a content-sized
basis it is the left group that decides where the line breaks, and a
thread title alone is enough to push the bell onto its own row on a
360px Android. At `basis-0` it never forces a break and simply takes
whatever the line has left, which is also the SPEC's stated priority
order (the thread title yields, the alert does not).

`overflow-hidden` is the backstop for everything this class of bug
can still throw at us: if the left group's contents ever outgrow its
box again they are CLIPPED at its edge instead of being drawn over
the controls to its right. A clipped glyph is visibly wrong; an
invisible overlay that swallows taps is not. Safe against the two
things inside it that escape their box on purpose - the Radix model
dropdown renders through a Portal, and the unread dot is positioned
inside its own link. */}
<header className="flex min-h-14 shrink-0 flex-wrap items-center justify-between gap-x-2 gap-y-1 border-b border-border bg-card/60 px-4 backdrop-blur-sm lg:flex-nowrap lg:gap-y-0">
<div className="flex min-w-0 flex-1 items-center gap-4 overflow-hidden">
<Link
to="/"
aria-label="Inkie Deck home"
Expand Down Expand Up @@ -302,7 +356,28 @@ export function AppShell({ agents, needsItems, needsCount, workCards, tmuxOn = t
) : null}
</div>

<div className="flex items-center gap-1">
{/* Right-hand control cluster. `w-full` below `lg` when - and only
when - the single-agent escalation chip is in here is what buys the
cluster its own row: a full-width flex item cannot share a line
with anything (the header's gap-x pushes it over), so the chip
lands on row two at its natural width and the reason text stays
whole. It is the CLUSTER that drops, not the chip alone, because
the chip lives inside here and moving it out to be a header-level
sibling would either reorder the desktop bar or need an `order`
override, which puts visual order and tab order out of step.

Conditional on `escalated.length === 1` deliberately. With no
escalation this cluster is 84px and with the multi-agent dropdown
it is 132px - both fit inline at 320px with room to spare, so
neither has any business costing a phone a second header row.
`justify-end` keeps the controls hard right on the wrapped row, so
the bar reads the same way it does when it is one line. */}
<div
className={cn(
"flex items-center justify-end gap-1",
escalated.length === 1 ? "w-full lg:w-auto" : undefined
)}
>
{/* File pinner: per-thread Files panel toggle. Chat view only (the
manifest is built from chat messages, not the tmux pane). Pure
URL-state (?files=1) - no shell prop needed. */}
Expand All @@ -329,6 +404,94 @@ export function AppShell({ agents, needsItems, needsCount, workCards, tmuxOn = t
</button>
) : null}

{/* Top-bar escalation alert: distinct icon (TriangleAlert,
StatusPill's own `stuck` glyph) and destructive tint so this
never blurs into the needs-you bell's count - it is a different
claim ("an agent has been silently wedged") not an action queue.
Single escalated agent -> direct click-through to its thread.
Multiple -> a dropdown, same shape as the bell's. No dismiss/
mute in v1 (SPEC): it is a live-computed present/absent state
off current data, not a fired-once event.

TASK-783: the single-agent branch spells the REASON out in
visible text ("quill stuck for 45m") rather than leaving the
glyph bare with the facts only in aria-label. That shape was hit
live - red triangle, click, thread, no idea why it fired or
whether it was worth the interruption. The duration is the
number the interruption gets priced with, so it cannot live
behind a screen reader (or a mouse-only title=, which never
fires on touch); house rule, see StatusPill. The count badge
goes with it: naming the agent makes a badge reading "1" pure
noise. The dropdown branch below keeps its count - a count is
the only summary available there - and is otherwise untouched.
aria-label drops "has been" so the visible label is a contiguous
substring of the accessible name (WCAG 2.5.3 Label in Name),
which also spares screen readers "quill has been needs you for
45m". */}
{escalated.length === 1 ? (
<Link
to={`/threads/${escalated[0]!.agent}`}
data-testid="escalation-alert"
aria-label={`${escalated[0]!.agent} ${statusLabel(escalated[0]!.status)} for ${formatDurationHm(escalated[0]!.durationMs)} - go to thread`}
className="inline-flex min-h-11 items-center gap-1.5 rounded-md px-2 text-destructive transition-colors hover:bg-destructive/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
<TriangleAlert className="size-5 shrink-0" aria-hidden="true" />
{/* Plain text, not label-mono: the top bar's nav idiom
uppercases on 0.18em tracking, which is both shouty for an
agent's name and ~60% wider - width the header does not
have on a phone. The dropdown renders its own reason line
("wedged 45m") as plain text for the same reason. */}
<span
data-testid="escalation-reason"
className="whitespace-nowrap text-xs font-medium"
>
{escalated[0]!.agent} {statusLabel(escalated[0]!.status)} for{" "}
{formatDurationHm(escalated[0]!.durationMs)}
</span>
</Link>
) : escalated.length > 1 ? (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<button
type="button"
data-testid="escalation-alert"
aria-label={`Wedged agents (${escalated.length})`}
className="relative inline-flex min-h-11 min-w-11 items-center justify-center rounded-md text-destructive transition-colors hover:bg-destructive/10 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
>
<TriangleAlert className="size-5" />
<span className="absolute -right-0.5 -top-0.5 inline-flex h-4 min-w-4 items-center justify-center rounded-full bg-destructive px-1 font-mono text-[10px] font-bold leading-none text-destructive-foreground">
{escalated.length}
</span>
</button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-72">
<DropdownMenuLabel>Wedged ({escalated.length})</DropdownMenuLabel>
<DropdownMenuSeparator />
{escalated.map((e) => (
<DropdownMenuItem key={e.agent} asChild>
<Link
to={`/threads/${e.agent}`}
data-testid={`escalation-item-${e.agent}`}
className="flex flex-col items-start gap-0.5"
>
<span className="flex w-full items-center gap-2">
<span className="font-display text-xs font-bold uppercase tracking-tight text-foreground">
{e.agent}
</span>
<span className="label-mono ml-auto text-[10px] text-muted-foreground">
{statusLabel(e.status)}
</span>
</span>
<span className="text-sm text-destructive">
wedged {formatDurationHm(e.durationMs)}
</span>
</Link>
</DropdownMenuItem>
))}
</DropdownMenuContent>
</DropdownMenu>
) : null}

<DropdownMenu>
<DropdownMenuTrigger asChild>
{/* Bell tap target: min 44px per tap-target sweep */}
Expand Down
38 changes: 38 additions & 0 deletions packages/deck/app/lib/activity.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ export function activityFilePath(env: NodeJS.ProcessEnv): string | undefined {
*/
export const ACTIVITY_STALE_AFTER_MS = 10 * 60 * 1000;

// ---------------------------------------------------------------------------
// Top-bar escalation threshold
// ---------------------------------------------------------------------------

/**
* Default duration (ms) an agent must sit `stuck`/`needs-you` before the
* top-bar escalation alert fires (top-bar escalation SPEC).
*
* 30 minutes. NOT SPECIFIED by Alex ("a certain amount of time") - the
* SPEC's own proposed default, flagged there as an assumption to correct
* rather than a blocker: long enough that a normal working pause never
* fires, short enough to have caught the incident that prompted the feature
* (an agent wedged for most of a day) with hours to spare. Overridable
* per-instance via ESCALATION_THRESHOLD_MS, same convention as
* ACTIVITY_STALE_AFTER_MS's would-be override - a single constant either way.
*/
export const ESCALATION_THRESHOLD_MS = 30 * 60 * 1000;

/**
* Resolve the escalation threshold from env, degrading to the default on any
* unset/blank/unparseable/non-positive value rather than throwing or letting
* a bad env var produce a threshold of 0 (which would escalate everything)
* or NaN (which would escalate nothing, silently). Same posture as
* resolveNow (time.ts): a bad override degrades, it never crashes the loader.
*/
export function resolveEscalationThresholdMs(env: NodeJS.ProcessEnv): number {
const raw = env["ESCALATION_THRESHOLD_MS"]?.trim();
if (!raw) return ESCALATION_THRESHOLD_MS;
const parsed = Number(raw);
if (!Number.isFinite(parsed) || parsed <= 0) {
console.warn(
`[activity] ESCALATION_THRESHOLD_MS value "${raw}" is invalid - falling back to default ${ESCALATION_THRESHOLD_MS}ms`
);
return ESCALATION_THRESHOLD_MS;
}
return parsed;
}

// ---------------------------------------------------------------------------
// readActivity
// ---------------------------------------------------------------------------
Expand Down
80 changes: 80 additions & 0 deletions packages/deck/app/lib/activity.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,21 @@ export interface AgentActivity {
worktree: AgentWorktree | null;
recent: RecentCompletion[];
updated: string | null;
/**
* ISO-8601 instant the agent's CURRENT `status` was first observed (top-bar
* escalation SPEC, Part A - a producer field, not written by every sweep).
*
* OPTIONAL, deliberately, unlike every other timestamp on this record: the
* producer half of this feature is newer than the consumer half, so a
* sweep that predates it writes documents with no `statusSince` at all.
* Absence must degrade, never throw - see `escalatedAgents` below, which
* SKIPS an agent entirely when this is absent, computing no duration at
* all. (Caught in review: the first version fell back to `updated`,
* reasoning it as "just started" - wrong the moment the sweep itself goes
* stale, since `updated` then stops advancing and looks like a
* long-running status instead of missing data.)
*/
statusSince?: string;
}

export interface ActivityDocument {
Expand Down Expand Up @@ -382,6 +397,10 @@ export function parseActivityDocument(text: string): ParseActivityResult {
worktree: asWorktree(value["worktree"]),
recent: asRecent(value["recent"]),
updated: str(value["updated"]),
// Coerced to undefined rather than left as a stored null, so a
// document written by a sweep that does not emit this field yet
// leaves the optional property genuinely ABSENT.
statusSince: str(value["statusSince"]) ?? undefined,
});
}
}
Expand Down Expand Up @@ -507,3 +526,64 @@ export function countStatuses(
count: counts.get(s)!,
}));
}

// ---------------------------------------------------------------------------
// Top-bar escalation (the consumer half)
// ---------------------------------------------------------------------------

/** A status the top-bar escalation cares about - see `escalatedAgents`. */
export type EscalatableStatus = "stuck" | "needs-you";

/** One agent that has been stuck/needs-you for at least the threshold. */
export interface EscalatedAgent {
agent: string;
status: EscalatableStatus;
/**
* The instant `status` was entered, straight off the producer field.
* Carried so the UI can show a "since" time without recomputing it from
* `durationMs` and `now`.
*/
statusSince: string;
durationMs: number;
}

/**
* Agents that have been `stuck` or `needs-you` for >= thresholdMs, i.e. the
* top-bar escalation alert's contents (top-bar escalation SPEC, "Design"
* section 1).
*
* Pure: `now` is injected, matching the MC_NOW convention used throughout
* activity.test.ts - no clock read happens in here.
*
* DEGRADE ON MISSING `statusSince`: an agent whose record carries no such
* timestamp is SKIPPED, computing no duration at all. There is deliberately
* no fallback to `updated` (the sweep-tick write time). That fallback reads
* as reasonable - `updated` is usually seconds old, so the gap stands in for
* "just entered this status" - and it only holds while the sweep is healthy.
* If the sweep itself stalls, which is exactly the failure this feature
* exists to notice, `updated` stops advancing and the gap grows without
* bound, firing a FALSE escalation off data staleness at the precise moment
* the alert's own inputs became unreliable.
*
* `waiting-on-teammate`/`waiting-on-machine` are expected, unescalated states
* (StatusPill.tsx's own tint table draws the same line) - an agent waiting on
* a teammate is not the operator's problem by definition.
*/
export function escalatedAgents(
agents: AgentActivity[],
now: Date,
thresholdMs: number
): EscalatedAgent[] {
const out: EscalatedAgent[] = [];
for (const a of agents) {
if (a.status !== "stuck" && a.status !== "needs-you") continue;
const since = a.statusSince;
if (since === undefined) continue; // no statusSince - nothing to escalate on
const sinceMs = new Date(since).getTime();
if (Number.isNaN(sinceMs)) continue;
const durationMs = now.getTime() - sinceMs;
if (durationMs < thresholdMs) continue;
out.push({ agent: a.agent, status: a.status, statusSince: since, durationMs });
}
return out;
}
Loading
Loading
0