Files
PaperClipAI/ui/src/components/RunnerInspector.tsx
T
DottaandPaperclip 889947c238 feat: add experimental native chat connectors (#13038)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - People also ask agents for work in their existing chat tools.
> - Each external conversation needs one task and a current authorized
source.
> - Retries, Stop, and provider failures must not duplicate work or
expose private data.
> - The first chat PR establishes the opt-in provider and data
contracts.
> - This PR adds experimental channel integration and its durable
control plane.
> - Users can request work from connected channels and inspect delivery
in Paperclip.

## Linked Issues or Issue Description

Refs #13100 and #13092. This is the second of exactly two chat PRs.
Foundation #13100 is merged and changed 143 files. Runner prerequisite
#13092 is also merged. This PR changes 400 files against master, below
the 500-file review limit. It contains no wireframe images or HTML
galleries.

## What Changed

- Add native Slack, GitHub, Microsoft Teams, Telegram, and Discord chat
connections. Keep chat disabled unless the operator enables experimental
chat connectors. Preserve the production GitHub tool connection and its
normal setup path.
- Bind each provider bot identity to one immutable Paperclip agent. Bind
each admitted external conversation to one task. Paperclip owns tasks,
runs, permissions, and audit records.
- Add durable admission, per-conversation queues, questions, task
controls, progress, final replies, images, files, and delivery receipts.
Board comments remain internal unless explicitly sent to the channel.
- Check current identity, provider reach, resource access, credentials,
runtime generation, and exact source before provider effects. Keep
private responses private. Never send raw reasoning, private logs,
credentials, or tool arguments.
- Hold uncertain sends for explicit audited resolution. Make Board
Send-to-channel atomic and idempotent. Keep reconnect and setup
credentials in Paperclip secret storage.
- Preserve current native-runner authority across retries, lost
acknowledgements, and recovery. Keep immutable input and completion
contracts separate from newer user input. Receipt reconciliation cannot
launch a provider.
- Reconcile chat close/new ordering and provider-effect lock order.
Audit resource access changes in the same transaction. Submit only the
selected resource from each UI toggle so stale pages cannot undo
unrelated access changes.
- Drain Codex stdout before certifying process exit. Bound the drain
with the existing shutdown grace. Preserve observed terminal authority
without treating an undrained process as successful or reusable.
- Incorporate master `018ca5da` with its ACP Stop, mobile task layout,
runner packaging, and official lock changes. Preserve dedicated
chat-answer continuations in both directions when ordinary queued
comments are adopted after Stop.
- Fence late adapter readiness behind an earlier Stop for the same run.
Preserve verified cleanup for registered adapters. Handle single Stop,
agent pause, duplicate Stops, and failure release without creating a
false cancellation receipt.
- Incorporate master's `6dd48cad4` wake-queue extraction. Preserve exact
failed-chat retry authorization and lineage, retired question-source
suppression, and the block on generic recovery that would discard the
admitted source. Fresh deferred input retains its separate promotion
path.
- Incorporate master `2a05b5ed3` and its queue-admission extraction,
simplified transaction ports, and separate runner CI job. Preserve exact
durable receipts, actor separation, and dedicated-answer isolation
through the new module. A failed receipt insert rolls back the
accompanying deferred-wake merge.

## Verification

Current head: `afe19299d06253cb628eb398e91d1200ea9f412a`, incorporating
master `2a05b5ed3457ea33efd6895520447d1d97fe98d8`. The conflicts are
resolved. This successor fixes two test-harness boundaries exposed by
CI: per-case route-module preparation and actual durable-save completion
before intentional runner termination. Production code and all existing
test/turn deadlines are unchanged. [Exact-head Greptile
review](https://github.com/paperclipai/paperclip/pull/13038#issuecomment-5587250594)
is **5/5**, completed September 10 at 13:20:55 UTC, with no actionable
findings or open review threads. [Fresh exact-head
CI](https://github.com/paperclipai/paperclip/actions/runs/34481724341)
passes **all 24 jobs**, including Build and both required aggregates.
Normal exact-head guarded merge was attempted and rejected by the
remaining branch approval policy: CODEOWNER review is required and no
human approval is present. Normal **squash auto-merge is enabled** as of
September 10 at 13:36:26 UTC. Requested CODEOWNERS have been notified;
no approval bypass or self-approval was used. Earlier-head results below
remain historical evidence, not qualification of this successor.

- Final exact-head Linux evidence: 995/995 chat integration cases; 36/36
agent-skills routes; 35/35 runner live-session cases, including real
process kill/resume; 1948 runner Vitest cases with three existing
benchmark/platform guards; 870/870 API-authority cases; and 104 browser
cases with four existing optional skips. Rust, conformance/replay, full
repository build, typecheck, canary, all server/workspace shards, and
both required aggregates pass with normal CI concurrency. Earlier failed
attempts remain recorded below.

- Latest test-only qualification: 141/141
route/permissions/authentication cases pass in separate cold forks, with
plain server types and independent review clear. The real-runner suite
passes 35/35, with plain runner types and independent review clear. A
controlled premature-save acknowledgement fails as expected; matching
ownership/effect/process evidence, rejected saves, real turn outcome,
test abort, and pre-kill liveness are covered. No local reproduction of
the original CI scheduling failure is claimed. The preceding [CI
run](https://github.com/paperclipai/paperclip/actions/runs/34479680858)
passes 21/24 jobs, including all 995 Linux chat cases and browser
aggregate (104 passed, four existing optional skips); only Build, the
skills serialized shard, and the required verification aggregate fail.
Its exact-head Greptile review was 5/5. Both failed job logs are
retained.

- Final fixture qualification: all eight focused Discord cases and all
995 chat integration cases pass. The exact modal statement/PID is
observed before taking the real connection lock; the test then proves
its actual blocking relationship before mutation. Original SQL
execution, provider behavior, negative assertions, and 1s/15s timeouts
remain unchanged. Independent review is clear and test/production hashes
remain frozen. The preceding [CI
attempt](https://github.com/paperclipai/paperclip/actions/runs/34477184777)
passed 22 jobs, including Build/runner, typecheck, canary, all other
test shards, and browser aggregate (104 passed, four existing optional
skips); the two fixture failures and failed verification aggregate
remain recorded, not relabeled as a pass.

- Current queue-module composition: 308/308 recovery/batching/queue/Stop
tests; 995/995 full chat integration; 89/89 module tests, including real
PostgreSQL receipt-insert rollback; 24/24 workflow/module-boundary
tests; plain server and UI types. All four actual local process/ACP
browser paths pass in 1.4 minutes. Fresh databases, no skips or retries,
stable reviewed source hashes. The initial boundary failure is retained;
its no-op service wrapper was removed without changing recovery context
or weakening the check. An exploratory standalone test-directory
typecheck fails because its new upstream transformation config is not a
standalone typechecking project; standard CI/build does not invoke it,
and no configuration was weakened to suppress those diagnostics.

- The preceding head `e02a63d462ce5d47433b0aeb632bb6fd20aab1ba` passed
[all 24 CI
jobs](https://github.com/paperclipai/paperclip/actions/runs/34436462958)
and exact-head Greptile review at 5/5. Required CODEOWNER review
prevented its normal merge before master advanced again.

- Final extracted-module composition: 307/307 recovery, batching, queue
and Stop-control tests; 995/995 full chat integration; 49/49 module
tests including eight PostgreSQL adapter cases; and 19/19 issue-update
tests. Plain server types pass. All four actual local process/ACP
browser paths pass in 1.3 minutes. Fresh databases, no skips or retries
in these cohorts, frozen source hashes, and independent review clear.

- The preceding head `3e4e1c1c` passes [all PR CI
jobs](https://github.com/paperclipai/paperclip/actions/runs/34415826820),
including Build and required `ci / verify` and `ci / e2e`. Both the
original Rust failure and the previously load-sensitive lineage fixture
pass with unchanged Linux concurrency. Master advanced afterward and
required this reconciliation.
- Final master composition: 448/448 focused UI tests, 186/186 adapter
tests, 24/24 queue/control tests, and 11/11 packaging tests. Plain UI,
server, shared, and adapter types pass. Token gates and diff checks
pass. Independent server and UI reviews are clear.
- Stop-registration regression: both real-service cases fail against
exact `a95` source and pass with the fix. The full corrected
recovery/control suite passes 265/265. Duplicate-owner and failed-Stop
controls also pass. Plain server types pass. The readiness barrier
prevents provider startup without adding an acknowledgment to an already
terminal run.
- Final qualification strengthens terminal-field equality and repeats
both affected cases successfully on a fresh database. All four actual
local process/ACP browser paths pass again in 1.3 minutes, without skips
or retries. The final screenshot shows Cancelled, a paused subtree,
retained input, and no error toast.
- Two new actual-service regressions fail before the merge fix. They
prove that queued-comment adoption could consume a dedicated chat answer
or add unrelated input to that answer. The fixed four-case cohort
passes, including ordinary upstream continuation and adapter Stop
controls. Full recovery passes 257/257. All four actual local
process/ACP Stop browser flows pass in 1.4 minutes, without skips or
retries, on a fresh database.
- The unchanged runner artifact was qualified with 171/171 transport
tests, 870/870 API-authority tests, conformance 1/1, and replay 11/11.
Six controlled reader tests prove the exit/drain repair. Its local
serial Rust workspace passed 546 top-level cases plus two invoked
helpers; the later passing Linux CI supplies default-concurrency
evidence.
- Prior exact-source full chat integration passes 995/995. Settings
regressions cover concurrent stale pages, 501 destinations, pending
state, rejected updates, and explicit retry. These deterministic tests
do not prove live provider behavior.
- Retained failed attempts and their causes are in the [qualification
log](https://github.com/paperclipai/paperclip/blob/afe19299d06253cb628eb398e91d1200ea9f412a/doc/plans/chat-adapters/2026-09-08-chat-queue-and-webhook-repair.md).
The first merge adapter run timed out while macOS slept for 290 seconds.
Its unchanged repeat passed with a temporary sleep guard. No assertion,
deadline, or CI gate was weakened.

Review commands include `pnpm --filter @paperclipai/server exec vitest
run src/__tests__/heartbeat-process-recovery.test.ts
src/__tests__/issue-queued-comments-routes.test.ts` and `pnpm exec
playwright test --config tests/e2e/playwright.config.ts
tests/e2e/acp-stop-continuation.spec.ts`. Database suites require fresh
disposable databases. See the [browser
runbook](https://github.com/paperclipai/paperclip/blob/afe19299d06253cb628eb398e91d1200ea9f412a/doc/plans/chat-adapters/2026-09-04-chat-adapters-browser-e2e-runbook.md)
for provider setup and separate live acceptance steps.

## Risks

- This remains experimental. Deterministic tests and bounded live
evidence do not establish every provider feature, tenant, permission
layout, or media shape. Teams work-tenant qualification is still open.
- Failed and uncertain provider effects remain visible and can require
operator action. A transport receipt does not prove recipient
visibility.
- Native controller and runner artifacts must remain compatible.
Preserve lease ownership, terminal authority, source binding, and
quarantine during future changes.
- Access and audit rows commit together, but activity notifications
remain best-effort. This is not a new durable event outbox.
- The PR operation does not deploy a live server, replace its runner, or
change provider permissions. Remaining live qualification is documented
in the [temporary
handoff](https://github.com/paperclipai/paperclip/blob/afe19299d06253cb628eb398e91d1200ea9f412a/doc/plans/chat-adapters/2026-09-08-open-qualification-followups.md).

## Model Used

OpenAI Codex assisted with implementation, tool execution, testing, and
review. The work records `gpt-6-astra` assistance. The environment does
not report a context-window size. No private reasoning traces are
included.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-09-10 10:06:45 -05:00

1236 lines
62 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import type {
HeartbeatRunEvent,
ProviderTraceFieldMapping,
ProviderTraceFrame,
} from "@paperclipai/shared";
import {
ArrowRight,
Braces,
Check,
ChevronDown,
ChevronRight,
CircleOff,
Copy,
Download,
Eye,
EyeOff,
FileJson2,
Layers3,
RefreshCw,
Search,
ShieldAlert,
Trash2,
} from "lucide-react";
import { heartbeatsApi, type ProviderTraceInspection } from "@/api/heartbeats";
import { accessApi } from "@/api/access";
import { parsePaperclipRunnerStdoutLine } from "@/adapters/paperclip-runner";
import { TaskChatProtocolCard } from "@/components/task-chat/TaskChatProtocolCard";
import type { TaskChatProtocolItem } from "@/components/task-chat/task-chat-model";
import { transcriptToTaskChatItems } from "@/components/task-chat/transcript-adapter";
import { TASK_PROTOCOL_EVENT_SURFACE_REGISTRY } from "@/components/task-chat/task-protocol-surfaces";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { ScrollArea } from "@/components/ui/scroll-area";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import {
Sheet,
SheetContent,
SheetDescription,
SheetHeader,
SheetTitle,
} from "@/components/ui/sheet";
import { cn } from "@/lib/utils";
import { copyTextToClipboard } from "@/lib/clipboard";
type TraceEntry = Record<string, unknown>;
type InspectorView = "overview" | "pipeline" | "trace";
type RawTraceAccess = {
runId: string;
allowed: boolean;
epoch: number;
phase: "granted" | "denied" | "deleting";
};
type TraceOperation = {
key: string;
frames: TraceEntry[];
interpretations: TraceEntry[];
events: HeartbeatRunEvent[];
title: string;
subtitle: string;
itemType: string;
nativeMethods: string[];
directions: string[];
dispositions: string[];
prpTypes: string[];
visible: boolean;
timestamp: number;
};
const VIEW_OPTIONS: Array<{
value: InspectorView;
label: string;
icon: typeof Layers3;
}> = [
{ value: "overview", label: "Overview", icon: Layers3 },
{ value: "pipeline", label: "Pipeline", icon: ArrowRight },
{ value: "trace", label: "Exact trace", icon: FileJson2 },
];
const RAW_TRACE_ACCESS_REVALIDATION_MS = 1_000;
function record(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
function text(value: unknown): string {
return typeof value === "string" ? value : "";
}
function revealedFrameKey(runId: string, frameId: number): string {
return `${runId}:${frameId}`;
}
function scalar(value: unknown): string {
if (typeof value === "string") return value;
if (typeof value === "number" || typeof value === "boolean") return String(value);
return "";
}
function unique(values: string[]) {
return [...new Set(values.filter(Boolean))];
}
function formatBytes(value: number) {
if (value < 1_024) return `${value} B`;
if (value < 1_024 * 1_024) return `${(value / 1_024).toFixed(1)} KiB`;
return `${(value / (1_024 * 1_024)).toFixed(1)} MiB`;
}
function downloadBlob(blob: Blob, filename: string) {
const href = URL.createObjectURL(blob);
const anchor = document.createElement("a");
anchor.href = href;
anchor.download = filename;
anchor.click();
URL.revokeObjectURL(href);
}
function decodeExactFrame(rawBase64: string): unknown {
const decoded = atob(rawBase64);
try {
return JSON.parse(decoded);
} catch {
return decoded;
}
}
function statusVariant(status: string | undefined) {
if (status === "complete") return "secondary" as const;
if (status === "incomplete" || status === "truncated")
return "destructive" as const;
return "outline" as const;
}
function traceBadgeLabel(status: string, expiresAt: string | Date) {
if (status === "capturing") return "Raw trace enabled";
if (status === "incomplete") return "Incomplete";
if (status === "truncated") return "Truncated";
if (status === "deleted") return "Deleted";
if (status === "expired") return "Expired";
const remainingMs = new Date(expiresAt).getTime() - Date.now();
if (!Number.isFinite(remainingMs) || remainingMs <= 0) return "Expired";
const hours = Math.max(1, Math.ceil(remainingMs / (60 * 60 * 1_000)));
return `Expires in ${hours}h`;
}
function frameParsed(entry: TraceEntry) {
return record(entry.parsed);
}
function frameMethod(entry: TraceEntry) {
const parsed = frameParsed(entry);
return text(parsed.method) || text(record(parsed.params).method);
}
function frameItem(entry: TraceEntry) {
const parsed = frameParsed(entry);
const params = record(parsed.params);
return record(params.item ?? parsed.item);
}
function frameItemType(entry: TraceEntry) {
const parsed = frameParsed(entry);
const params = record(parsed.params);
return text(frameItem(entry).type) || text(params.itemType) || text(parsed.type);
}
function eventPrp(event: HeartbeatRunEvent) {
const payload = record(event.payload);
const prpEvent = record(payload.prpEvent);
return Object.keys(prpEvent).length > 0 ? prpEvent : payload;
}
function eventSourceId(event: HeartbeatRunEvent) {
return text(eventPrp(event).sourceEventId);
}
function eventTimestamp(event: HeartbeatRunEvent) {
const value = new Date(event.createdAt).getTime();
return Number.isFinite(value) ? value : Number.MAX_SAFE_INTEGER;
}
function frameTimestamp(frame: TraceEntry) {
const raw = scalar(frame.timestamp);
const numeric = Number(raw);
if (Number.isFinite(numeric)) {
return numeric < 10_000_000_000 ? numeric * 1_000 : numeric;
}
const parsed = Date.parse(raw);
return Number.isFinite(parsed) ? parsed : Number.MAX_SAFE_INTEGER;
}
function visibilityDecision(event: HeartbeatRunEvent) {
const registration = TASK_PROTOCOL_EVENT_SURFACE_REGISTRY[event.eventType];
if (!registration) {
return {
visible: false,
surface: "run_debug",
container: "Runner Inspector",
state: "hidden",
action: "none",
reasonCode: "presentation_surface_unregistered",
reason: "No production surface registration exists for this event type.",
};
}
return {
visible: registration.disposition !== "debug-only",
surface: registration.surface,
container:
registration.disposition === "inline"
? "primary run turn"
: "collapsed activity",
state: registration.disposition,
action:
registration.disposition === "inline"
? "render"
: registration.disposition === "folded"
? "fold"
: "operator only",
reasonCode: `presentation_${registration.disposition}`,
reason: registration.rationale,
};
}
function interpretationEventIds(entry: TraceEntry) {
return Array.isArray(entry.emittedEventIds)
? entry.emittedEventIds.map(String)
: [];
}
function frameCorrelationTokens(frame: TraceEntry, interpretations: TraceEntry[]) {
const parsed = frameParsed(frame);
const params = record(parsed.params);
const item = frameItem(frame);
const tokens = interpretations.flatMap(interpretationEventIds).map((id) => `event:${id}`);
const rpcId = scalar(parsed.id);
if (rpcId) {
const direction = text(frame.direction);
const isRequest = Boolean(frameMethod(frame));
const requestOrigin = isRequest
? direction === "client_to_provider" ? "client" : "provider"
: direction === "client_to_provider" ? "provider" : "client";
tokens.push(`rpc:${requestOrigin}:${rpcId}`);
}
for (const id of [item.id, params.itemId, params.callId, parsed.itemId]) {
const value = scalar(id);
if (value) tokens.push(`item:${value}`);
}
return unique(tokens);
}
function buildOperations(
frames: TraceEntry[],
interpretations: TraceEntry[],
events: HeartbeatRunEvent[],
): TraceOperation[] {
const sortedFrames = [...frames].sort(
(left, right) => Number(left.frameId) - Number(right.frameId),
);
const frameById = new Map(sortedFrames.map((frame) => [Number(frame.frameId), frame]));
const parent = new Map([...frameById.keys()].map((id) => [id, id]));
const find = (id: number): number => {
const current = parent.get(id) ?? id;
if (current === id) return id;
const root = find(current);
parent.set(id, root);
return root;
};
const union = (left: number, right: number) => {
const leftRoot = find(left);
const rightRoot = find(right);
if (leftRoot !== rightRoot) parent.set(rightRoot, leftRoot);
};
const tokenOwner = new Map<string, number>();
for (const frame of sortedFrames) {
const frameId = Number(frame.frameId);
const stages = interpretations.filter((entry) => Number(entry.frameId) === frameId);
for (const token of frameCorrelationTokens(frame, stages)) {
const owner = tokenOwner.get(token);
if (owner === undefined) tokenOwner.set(token, frameId);
else union(owner, frameId);
}
}
const groupedFrames = new Map<number, TraceEntry[]>();
for (const frame of sortedFrames) {
const root = find(Number(frame.frameId));
groupedFrames.set(root, [...(groupedFrames.get(root) ?? []), frame]);
}
const claimedEvents = new Set<number>();
const operations: TraceOperation[] = [];
for (const groupFrames of groupedFrames.values()) {
const frameIds = new Set(groupFrames.map((frame) => Number(frame.frameId)));
const stages = interpretations.filter((entry) => frameIds.has(Number(entry.frameId)));
const emittedIds = new Set(stages.flatMap(interpretationEventIds));
const groupEvents = events.filter((event) => emittedIds.has(eventSourceId(event)));
groupEvents.forEach((event) => claimedEvents.add(event.id));
const methods = unique(groupFrames.map(frameMethod));
const itemTypes = unique(groupFrames.map(frameItemType));
const title = itemTypes.at(-1) || methods.at(-1) || text(groupFrames[0]?.direction) || "Provider frame";
const firstFrame = groupFrames[0];
const lastFrame = groupFrames.at(-1);
const range = firstFrame === lastFrame
? `frame ${firstFrame?.frameId}`
: `frames ${firstFrame?.frameId}–${lastFrame?.frameId}`;
operations.push({
key: `frames:${firstFrame?.frameId}`,
frames: groupFrames,
interpretations: stages,
events: groupEvents,
title,
subtitle: `${range} · ${groupEvents.length} PRP event${groupEvents.length === 1 ? "" : "s"}`,
itemType: itemTypes.at(-1) ?? "",
nativeMethods: methods,
directions: unique(groupFrames.map((frame) => text(frame.direction))),
dispositions: unique(stages.map((entry) => text(entry.disposition))),
prpTypes: unique(groupEvents.map((event) => event.eventType)),
visible: groupEvents.some((event) => visibilityDecision(event).visible),
timestamp: Math.min(...groupFrames.map(frameTimestamp)),
});
}
for (const event of events) {
if (claimedEvents.has(event.id)) continue;
const decision = visibilityDecision(event);
operations.push({
key: `event:${event.id}`,
frames: [],
interpretations: [],
events: [event],
title: event.eventType,
subtitle: `PRP ${eventSourceId(event) || `event ${event.seq}`} · no raw correlation`,
itemType: "",
nativeMethods: [],
directions: [],
dispositions: [],
prpTypes: [event.eventType],
visible: decision.visible,
timestamp: eventTimestamp(event),
});
}
return operations.sort((left, right) => left.timestamp - right.timestamp);
}
async function loadAllRunEvents(runId: string) {
const events: HeartbeatRunEvent[] = [];
let afterSeq = 0;
for (;;) {
const page = await heartbeatsApi.events(runId, afterSeq, 1_000);
events.push(...page);
if (page.length < 1_000) return events;
const nextSeq = page.at(-1)?.seq ?? afterSeq;
if (nextSeq <= afterSeq) return events;
afterSeq = nextSeq;
}
}
function jsonMatches(value: unknown, query: string): boolean {
if (!query) return true;
try {
return JSON.stringify(value).toLowerCase().includes(query.toLowerCase());
} catch {
return String(value).toLowerCase().includes(query.toLowerCase());
}
}
function JsonPrimitive({ value }: { value: unknown }) {
if (typeof value === "string") return <span className="text-primary">&quot;{value}&quot;</span>;
if (typeof value === "number") return <span className="text-muted-foreground">{value}</span>;
if (typeof value === "boolean") return <span className="text-secondary-foreground">{String(value)}</span>;
if (value === null) return <span className="text-muted-foreground">null</span>;
return <span>{String(value)}</span>;
}
function JsonNode({
label,
value,
path,
depth,
query,
}: {
label?: string;
value: unknown;
path: string;
depth: number;
query: string;
}) {
const expandable = value !== null && typeof value === "object";
const entries = Array.isArray(value)
? value.map((child, index) => [String(index), child] as const)
: Object.entries(record(value));
const large = entries.length > 8;
const [expanded, setExpanded] = useState(depth < 1 && !large);
const forcedOpen = Boolean(query) && jsonMatches(value, query);
const isOpen = expandable && (expanded || forcedOpen);
const visibleEntries = query
? entries.filter(([key, child]) =>
key.toLowerCase().includes(query.toLowerCase()) || jsonMatches(child, query),
)
: entries;
const copyValue = () => {
const serialized = typeof value === "string" ? value : JSON.stringify(value, null, 2);
void copyTextToClipboard(serialized ?? String(value));
};
return (
<div className={cn(depth > 0 && "border-l border-border/60 pl-3")}>
<div className="group flex min-h-6 items-start gap-1 font-mono text-xs leading-6">
{expandable ? (
<button
type="button"
className="mt-1 rounded p-0.5 text-muted-foreground hover:bg-muted hover:text-foreground"
onClick={() => setExpanded(!isOpen)}
aria-label={isOpen ? "Collapse JSON value" : "Expand JSON value"}
>
{isOpen ? <ChevronDown className="h-3 w-3" /> : <ChevronRight className="h-3 w-3" />}
</button>
) : (
<span className="w-4" />
)}
{label !== undefined ? <span className="text-primary">{label}:</span> : null}
{expandable ? (
<span className="text-muted-foreground">
{Array.isArray(value) ? `[${entries.length}]` : `{${entries.length}}`}
</span>
) : (
<JsonPrimitive value={value} />
)}
<span className="ml-auto hidden items-center gap-0.5 group-hover:flex">
<button
type="button"
title="Copy JSON path"
className="rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground"
onClick={() => void copyTextToClipboard(path)}
>
<Braces className="h-3 w-3" />
</button>
<button
type="button"
title="Copy value"
className="rounded p-1 text-muted-foreground hover:bg-muted hover:text-foreground"
onClick={copyValue}
>
<Copy className="h-3 w-3" />
</button>
</span>
</div>
{isOpen ? (
<div>
{visibleEntries.map(([key, child]) => (
<JsonNode
key={`${path}.${key}`}
label={key}
value={child}
path={Array.isArray(value) ? `${path}[${key}]` : `${path}.${key}`}
depth={depth + 1}
query={query}
/>
))}
{query && visibleEntries.length === 0 ? (
<p className="pl-5 text-xs text-muted-foreground">No fields match.</p>
) : null}
</div>
) : null}
</div>
);
}
function JsonExplorer({ value, label = "Search this JSON" }: { value: unknown; label?: string }) {
const [query, setQuery] = useState("");
return (
<div className="overflow-hidden rounded-md border border-border bg-muted/20">
<div className="relative border-b border-border bg-background/60">
<Search className="absolute left-2.5 top-2.5 h-3.5 w-3.5 text-muted-foreground" />
<Input
value={query}
onChange={(event) => setQuery(event.target.value)}
placeholder={label}
className="h-8 border-0 bg-transparent pl-8 text-xs shadow-none focus-visible:ring-0"
/>
</div>
<div className="max-h-80 overflow-auto p-3">
<JsonNode value={value} path="$" depth={0} query={query.trim()} />
</div>
</div>
);
}
function fieldMappings(entry: TraceEntry): ProviderTraceFieldMapping[] {
const explicit = Array.isArray(entry.fieldMappings)
? entry.fieldMappings
.map(record)
.filter((mapping) => text(mapping.action))
.map((mapping) => ({
inputPath: text(mapping.inputPath) || undefined,
outputPath: text(mapping.outputPath) || undefined,
action: text(mapping.action) as ProviderTraceFieldMapping["action"],
reason: text(mapping.reason) || undefined,
}))
: [];
const knownDrops = new Set(explicit.filter((mapping) => mapping.action === "dropped").map((mapping) => mapping.inputPath));
const legacy = Array.isArray(entry.droppedFields)
? entry.droppedFields
.map(String)
.filter((path) => !knownDrops.has(path))
.map((path) => ({
inputPath: path,
action: "dropped" as const,
reason: text(entry.reason) || "Field was not carried into the next stage",
}))
: [];
return [...explicit, ...legacy];
}
function mappingTone(action: ProviderTraceFieldMapping["action"]) {
if (action === "dropped" || action === "redacted") return "border-destructive/30 bg-destructive/10 text-destructive";
if (action === "derived") return "border-secondary bg-secondary text-secondary-foreground";
return "border-primary/30 bg-primary/10 text-primary";
}
function InterpretationStage({ entry, last }: { entry: TraceEntry; last: boolean }) {
const mappings = fieldMappings(entry);
return (
<div className="relative grid grid-cols-(--gtc-runner-inspector-stage) gap-3">
<div className="relative flex justify-center">
{!last ? <span className="absolute bottom-(--sz-neg-1_25rem) top-4 w-px bg-border" /> : null}
<span className={cn(
"relative mt-1 h-3 w-3 rounded-full border-2 bg-background",
entry.disposition === "rejected" ? "border-destructive" : entry.disposition === "ignored" ? "border-muted-foreground" : "border-primary",
)} />
</div>
<div className="mb-4 overflow-hidden rounded-lg border border-border bg-card">
<div className="flex flex-wrap items-center gap-2 border-b border-border/70 px-3 py-2">
<code className="text-xs font-semibold">{text(entry.stage)}</code>
<Badge variant="outline" className="capitalize">{text(entry.disposition)}</Badge>
<code className="ml-auto text-(length:--text-nano) text-muted-foreground">{text(entry.ruleId)}</code>
</div>
<p className="px-3 py-2 text-xs text-muted-foreground">{text(entry.reason)}</p>
{mappings.length > 0 ? (
<div className="overflow-x-auto border-t border-border/70">
<table className="w-full min-w-(--sz-36rem) text-left text-xs">
<thead className="bg-muted/30 text-muted-foreground">
<tr>
<th className="px-3 py-1.5 font-medium">Action</th>
<th className="px-3 py-1.5 font-medium">Provider path</th>
<th className="px-3 py-1.5 font-medium">Output path</th>
<th className="px-3 py-1.5 font-medium">Reason</th>
</tr>
</thead>
<tbody>
{mappings.map((mapping, index) => (
<tr key={`${mapping.inputPath}:${mapping.outputPath}:${index}`} className="border-t border-border/50 align-top">
<td className="px-3 py-2">
<span className={cn("rounded border px-1.5 py-0.5 text-(length:--text-nano)", mappingTone(mapping.action))}>{mapping.action}</span>
</td>
<td className="px-3 py-2 font-mono">{mapping.inputPath ?? "—"}</td>
<td className="px-3 py-2 font-mono">{mapping.outputPath ?? "—"}</td>
<td className="max-w-xs px-3 py-2 text-muted-foreground">{mapping.reason ?? "—"}</td>
</tr>
))}
</tbody>
</table>
</div>
) : (
<p className="border-t border-border/70 px-3 py-2 text-(length:--text-nano) text-muted-foreground">No field-level mapping was recorded at this stage.</p>
)}
</div>
</div>
);
}
function typedPrpFields(event: HeartbeatRunEvent) {
const prp = eventPrp(event);
const payload = record(prp.payload);
const item = record(payload.item);
return [
["Event type", event.eventType],
["Source event", text(prp.sourceEventId)],
["Sequence", scalar(prp.sourceSequence) || String(event.seq)],
["Item type", text(item.type) || text(payload.kind)],
["Status", text(payload.status) || text(item.status)],
["Query", text(payload.query) || text(item.query)],
].filter(([, value]) => Boolean(value));
}
function ProductionSurfacePreview({ event, runId }: { event: HeartbeatRunEvent; runId: string }) {
const prp = eventPrp(event);
const ts = new Date(event.createdAt).toISOString();
const entries = parsePaperclipRunnerStdoutLine(
JSON.stringify({ type: "paperclip.prp.event", event: prp }),
ts,
);
const item = transcriptToTaskChatItems(entries, {
runId,
agentName: "Runner",
running: false,
}).find((candidate): candidate is TaskChatProtocolItem => candidate.kind === "protocol");
if (item) return <TaskChatProtocolCard item={item} />;
const decision = visibilityDecision(event);
const VisibleIcon = decision.visible ? Eye : EyeOff;
return (
<div className="rounded-md border border-border bg-card/60 px-3 py-2.5">
<div className="flex items-start gap-2">
<VisibleIcon className="mt-0.5 h-4 w-4 text-muted-foreground" />
<div className="min-w-0">
<p className="text-sm font-medium">{event.eventType}</p>
<p className="mt-0.5 text-xs text-muted-foreground">
Production surface: {decision.surface} · {decision.action} in {decision.container}
</p>
</div>
</div>
</div>
);
}
function StatCard({ label, value, detail }: { label: string; value: string | number; detail?: string }) {
return (
<div className="rounded-lg border border-border bg-card px-4 py-3">
<p className="text-(length:--text-nano) font-medium uppercase tracking-wide text-muted-foreground">{label}</p>
<p className="mt-1 text-2xl font-semibold tracking-tight">{value}</p>
{detail ? <p className="mt-1 text-xs text-muted-foreground">{detail}</p> : null}
</div>
);
}
export function RunnerInspector({
runId,
run,
open,
onOpenChange,
onRerunWithTrace,
}: {
runId: string;
run?: { resultJson: Record<string, unknown> | null; status: string } | null;
open: boolean;
onOpenChange: (open: boolean) => void;
onRerunWithTrace?: () => void;
}) {
const [inspection, setInspection] = useState<ProviderTraceInspection | null>(null);
const [events, setEvents] = useState<HeartbeatRunEvent[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const [view, setView] = useState<InspectorView>("pipeline");
const [selectedKey, setSelectedKey] = useState<string | null>(null);
const [selectedFrameId, setSelectedFrameId] = useState<number | null>(null);
const [query, setQuery] = useState("");
const [direction, setDirection] = useState("all");
const [nativeMethod, setNativeMethod] = useState("all");
const [disposition, setDisposition] = useState("all");
const [prpType, setPrpType] = useState("all");
const [visibility, setVisibility] = useState("all");
const [revealed, setRevealed] = useState<Record<string, ProviderTraceFrame>>({});
const [rawTraceAccess, setRawTraceAccess] = useState<RawTraceAccess | null>(null);
const rawTraceAccessRef = useRef<RawTraceAccess | null>(null);
const rawTraceAccessEpochRef = useRef(0);
const canInspectRaw =
open && rawTraceAccess?.runId === runId ? rawTraceAccess.allowed : null;
useLayoutEffect(() => {
rawTraceAccessEpochRef.current += 1;
rawTraceAccessRef.current = null;
setRawTraceAccess(null);
setInspection(null);
setEvents([]);
setLoading(open);
setError(null);
setSelectedKey(null);
setSelectedFrameId(null);
setRevealed({});
}, [open, runId]);
const denyRawTraceAccess = useCallback(() => {
const currentAccess = rawTraceAccessRef.current;
if (
currentAccess?.runId === runId &&
currentAccess.phase === "denied"
) {
return;
}
rawTraceAccessEpochRef.current += 1;
const deniedAccess: RawTraceAccess = {
runId,
allowed: false,
epoch: rawTraceAccessEpochRef.current,
phase: "denied",
};
rawTraceAccessRef.current = deniedAccess;
setRawTraceAccess(deniedAccess);
setInspection({ trace: null, entries: [] });
setLoading(false);
setError(null);
setSelectedKey(null);
setSelectedFrameId(null);
setRevealed({});
}, [runId]);
useEffect(() => {
if (!open) return;
const params = new URLSearchParams(window.location.search);
const requestedView = params.get("traceView");
if (requestedView === "overview" || requestedView === "pipeline" || requestedView === "trace") setView(requestedView);
const requestedOperation = params.get("traceOperation");
if (requestedOperation) setSelectedKey(requestedOperation);
}, [open]);
useEffect(() => {
if (!open) return;
let active = true;
const loadEpoch = rawTraceAccessEpochRef.current;
setLoading(true);
setError(null);
Promise.all([accessApi.getCurrentBoardAccess(), loadAllRunEvents(runId)])
.then(async ([boardAccess, nextEvents]) => {
if (!active || rawTraceAccessEpochRef.current !== loadEpoch) return;
const canRaw = boardAccess.source === "local_implicit" || boardAccess.isInstanceAdmin;
const access: RawTraceAccess = {
runId,
allowed: canRaw,
epoch: rawTraceAccessEpochRef.current,
phase: canRaw ? "granted" : "denied",
};
rawTraceAccessRef.current = access;
setRawTraceAccess(access);
if (!canRaw) setRevealed({});
const trace = canRaw
? await heartbeatsApi.providerTrace(runId)
: ({ trace: null, entries: [] } satisfies ProviderTraceInspection);
const currentAccess = rawTraceAccessRef.current;
if (
!active ||
currentAccess?.runId !== runId ||
currentAccess.epoch !== loadEpoch ||
currentAccess.allowed !== canRaw
) {
return;
}
setInspection(trace);
setEvents(nextEvents);
})
.catch((cause) => {
if (!active || rawTraceAccessEpochRef.current !== loadEpoch) return;
setError(cause instanceof Error ? cause.message : "Runner inspection failed");
})
.finally(() => {
if (active && rawTraceAccessEpochRef.current === loadEpoch) {
setLoading(false);
}
});
return () => {
active = false;
};
}, [open, runId]);
useEffect(() => {
if (!open) return;
let active = true;
const revalidate = async () => {
try {
const boardAccess = await accessApi.getCurrentBoardAccess();
if (!active) return;
const canRaw =
boardAccess.source === "local_implicit" ||
boardAccess.isInstanceAdmin;
if (!canRaw) denyRawTraceAccess();
} catch {
if (active) denyRawTraceAccess();
}
};
const onFocus = () => void revalidate();
const onVisibilityChange = () => {
if (document.visibilityState === "visible") void revalidate();
};
const interval = window.setInterval(
() => void revalidate(),
RAW_TRACE_ACCESS_REVALIDATION_MS,
);
window.addEventListener("focus", onFocus);
document.addEventListener("visibilitychange", onVisibilityChange);
return () => {
active = false;
window.clearInterval(interval);
window.removeEventListener("focus", onFocus);
document.removeEventListener("visibilitychange", onVisibilityChange);
};
}, [denyRawTraceAccess, open]);
const entries = inspection?.entries ?? [];
const frames = useMemo(() => entries.filter((entry) => entry.kind === "frame"), [entries]);
const interpretations = useMemo(() => entries.filter((entry) => entry.kind === "interpretation"), [entries]);
const capturedProviders = unique(frames.map((frame) => text(frame.provider)));
const operations = useMemo(
() => buildOperations(frames, interpretations, events),
[events, frames, interpretations],
);
const filteredOperations = useMemo(
() => operations.filter((operation) => {
if (query.trim() && !jsonMatches(operation, query.trim())) return false;
if (direction !== "all" && !operation.directions.includes(direction)) return false;
if (nativeMethod !== "all" && !operation.nativeMethods.includes(nativeMethod)) return false;
if (disposition !== "all" && !operation.dispositions.includes(disposition)) return false;
if (prpType !== "all" && !operation.prpTypes.includes(prpType)) return false;
if (visibility === "visible" && !operation.visible) return false;
if (visibility === "hidden" && operation.visible) return false;
return true;
}),
[direction, disposition, nativeMethod, operations, prpType, query, visibility],
);
useEffect(() => {
if (!filteredOperations.length) return;
if (!selectedKey || !filteredOperations.some((operation) => operation.key === selectedKey)) {
const preferred =
filteredOperations.find(
(operation) => operation.itemType && operation.events.length > 0,
) ??
filteredOperations.find(
(operation) => operation.frames.length > 0 && operation.events.length > 0,
) ??
filteredOperations[0]!;
setSelectedKey(preferred.key);
}
}, [filteredOperations, selectedKey]);
const selectedOperation = filteredOperations.find((operation) => operation.key === selectedKey) ?? filteredOperations[0] ?? null;
useEffect(() => {
if (!selectedOperation) return;
if (!selectedOperation.frames.some((frame) => Number(frame.frameId) === selectedFrameId)) {
const preferred = [...selectedOperation.frames].reverse().find((frame) => frameMethod(frame).includes("completed")) ?? selectedOperation.frames.at(-1);
setSelectedFrameId(preferred ? Number(preferred.frameId) : null);
}
}, [selectedFrameId, selectedOperation]);
useEffect(() => {
if (!open || !selectedOperation) return;
const params = new URLSearchParams(window.location.search);
params.set("inspectRun", runId);
params.set("traceView", view);
params.set("traceOperation", selectedOperation.key);
const next = `${window.location.pathname}?${params.toString()}${window.location.hash}`;
window.history.replaceState(window.history.state, "", next);
}, [open, runId, selectedOperation, view]);
const selectedFrame = selectedOperation?.frames.find((frame) => Number(frame.frameId) === selectedFrameId) ?? selectedOperation?.frames.at(-1) ?? null;
const revealedFrame =
canInspectRaw === true && selectedFrame
? (revealed[
revealedFrameKey(runId, Number(selectedFrame.frameId))
] ?? null)
: null;
const selectedInterpretations = selectedFrame
? selectedOperation?.interpretations.filter((entry) => Number(entry.frameId) === Number(selectedFrame.frameId)) ?? []
: selectedOperation?.interpretations ?? [];
const selectedEvents = selectedOperation?.events ?? [];
const runResultJson = record(run?.resultJson);
const presentationDecision = record(runResultJson.presentationDecision);
const verificationCaveats = Array.isArray(runResultJson.verificationCaveats)
? runResultJson.verificationCaveats
: [];
const ignoredAttentionRequests = Array.isArray(runResultJson.ignoredAttentionRequests)
? runResultJson.ignoredAttentionRequests
: [];
const visibleEventCount = events.filter((event) => visibilityDecision(event).visible).length;
const ignoredCount = interpretations.filter((entry) => entry.disposition === "ignored").length;
const dispositionCounts = interpretations.reduce<Record<string, number>>((counts, entry) => {
const key = text(entry.disposition) || "unknown";
counts[key] = (counts[key] ?? 0) + 1;
return counts;
}, {});
const selectOperation = (operation: TraceOperation) => {
setSelectedKey(operation.key);
setView("pipeline");
};
const reveal = async (frameId: number) => {
if (canInspectRaw !== true) return;
const requestedAccess = rawTraceAccessRef.current;
if (requestedAccess?.runId !== runId || requestedAccess.allowed !== true) {
return;
}
if (!window.confirm("This exact provider frame may contain prompts, tool arguments, secrets, or reasoning. Reveal it now?")) return;
const requestedRunId = runId;
const frame = await heartbeatsApi.revealProviderTraceFrame(
requestedRunId,
frameId,
);
const currentAccess = rawTraceAccessRef.current;
if (
currentAccess?.runId !== requestedRunId ||
currentAccess.allowed !== true ||
currentAccess.epoch !== requestedAccess.epoch
) {
return;
}
setRevealed((current) => ({
...current,
[revealedFrameKey(requestedRunId, frameId)]: frame,
}));
};
const downloadTrace = async () => {
if (canInspectRaw !== true) return;
const requestedAccess = rawTraceAccessRef.current;
if (requestedAccess?.runId !== runId || requestedAccess.allowed !== true) {
return;
}
if (!window.confirm("Download the exact raw trace? It may contain sensitive prompts, tool arguments, and provider-only fields.")) return;
const blob = await heartbeatsApi.downloadProviderTrace(runId);
const currentAccess = rawTraceAccessRef.current;
if (
currentAccess?.runId !== runId ||
currentAccess.allowed !== true ||
currentAccess.epoch !== requestedAccess.epoch
) {
return;
}
downloadBlob(blob, `provider-trace-${runId}.ndjson`);
};
const deleteTrace = async () => {
if (canInspectRaw !== true) return;
const currentAccess = rawTraceAccessRef.current;
if (currentAccess?.runId !== runId || currentAccess.allowed !== true) {
return;
}
if (!window.confirm("Delete this raw trace immediately? This cannot be undone.")) return;
rawTraceAccessEpochRef.current += 1;
const deletionAccess: RawTraceAccess = {
...currentAccess,
allowed: false,
epoch: rawTraceAccessEpochRef.current,
phase: "deleting",
};
rawTraceAccessRef.current = deletionAccess;
setRawTraceAccess(deletionAccess);
setRevealed({});
try {
await heartbeatsApi.deleteProviderTrace(runId);
} catch (cause) {
if (rawTraceAccessRef.current?.epoch === deletionAccess.epoch) {
setError(
cause instanceof Error ? cause.message : "Raw trace deletion failed",
);
}
return;
}
if (rawTraceAccessRef.current?.epoch !== deletionAccess.epoch) return;
let boardAccess;
try {
boardAccess = await accessApi.getCurrentBoardAccess();
} catch {
if (rawTraceAccessRef.current?.epoch === deletionAccess.epoch) {
denyRawTraceAccess();
}
return;
}
if (rawTraceAccessRef.current?.epoch !== deletionAccess.epoch) return;
const canRaw =
boardAccess.source === "local_implicit" || boardAccess.isInstanceAdmin;
if (!canRaw) {
denyRawTraceAccess();
return;
}
rawTraceAccessEpochRef.current += 1;
const restoredAccess: RawTraceAccess = {
runId,
allowed: true,
epoch: rawTraceAccessEpochRef.current,
phase: "granted",
};
rawTraceAccessRef.current = restoredAccess;
setRawTraceAccess(restoredAccess);
setInspection({ trace: null, entries: [] });
setSelectedKey(null);
setSelectedFrameId(null);
};
const handleOpenChange = (nextOpen: boolean) => {
if (!nextOpen) {
rawTraceAccessEpochRef.current += 1;
rawTraceAccessRef.current = null;
setRawTraceAccess(null);
setInspection(null);
setEvents([]);
setSelectedKey(null);
setSelectedFrameId(null);
setRevealed({});
const params = new URLSearchParams(window.location.search);
params.delete("inspectRun");
params.delete("traceView");
params.delete("traceOperation");
const suffix = params.toString();
window.history.replaceState(
window.history.state,
"",
`${window.location.pathname}${suffix ? `?${suffix}` : ""}${window.location.hash}`,
);
}
onOpenChange(nextOpen);
};
return (
<Sheet open={open} onOpenChange={handleOpenChange}>
<SheetContent
className="w-(--sz-calc-43) max-w-none gap-0 p-0 sm:max-w-none"
aria-describedby="runner-inspector-description"
>
<SheetHeader className="border-b border-border pr-12">
<div className="flex flex-wrap items-center gap-2">
<SheetTitle>Runner Inspector</SheetTitle>
{inspection?.trace ? (
<>
<Badge variant={statusVariant(inspection.trace.status)}>
{traceBadgeLabel(inspection.trace.status, inspection.trace.expiresAt)}
</Badge>
<Badge variant="outline" className="font-mono">
{capturedProviders.join(", ") || inspection.trace.provider}
</Badge>
</>
) : null}
</div>
<SheetDescription id="runner-inspector-description">
Correlate exact provider traffic with every interpretation stage, canonical PRP event, and production surface.
</SheetDescription>
<div className="flex gap-1 pt-1" role="tablist" aria-label="Runner inspector views">
{VIEW_OPTIONS.map((option) => {
const Icon = option.icon;
return (
<button
key={option.value}
type="button"
role="tab"
aria-selected={view === option.value}
onClick={() => setView(option.value)}
className={cn(
"inline-flex items-center gap-1.5 border-b-2 px-3 py-2 text-sm font-medium transition-colors",
view === option.value ? "border-foreground text-foreground" : "border-transparent text-muted-foreground hover:text-foreground",
)}
>
<Icon className="h-4 w-4" /> {option.label}
</button>
);
})}
</div>
</SheetHeader>
<div className="flex min-h-0 flex-1 flex-col">
{error ? <div className="m-4 rounded-md border border-destructive/40 bg-destructive/5 p-3 text-sm text-destructive">{error}</div> : null}
{!loading && !error && canInspectRaw === false ? (
<div className="m-4 rounded-md border border-border bg-muted/20 p-4">
<p className="font-medium">Raw provider traces require an instance administrator.</p>
<p className="mt-1 text-sm text-muted-foreground">Canonical PRP events and presentation decisions remain inspectable below.</p>
</div>
) : null}
{!loading && !error && canInspectRaw === true && !inspection?.trace ? (
<div className="m-4 flex items-start justify-between gap-4 rounded-md border border-border bg-muted/20 p-4">
<div>
<p className="font-medium">Raw provider capture was off for this run.</p>
<p className="mt-1 text-sm text-muted-foreground">Canonical PRP events and persisted presentation decisions remain available below.</p>
</div>
{onRerunWithTrace && canInspectRaw === true ? (
<Button size="sm" onClick={onRerunWithTrace}><RefreshCw className="mr-1.5 h-4 w-4" />Re-run with provider trace</Button>
) : null}
</div>
) : null}
{view === "overview" ? (
<ScrollArea className="min-h-0 flex-1">
<div className="space-y-6 p-5">
{loading ? <p className="text-sm text-muted-foreground">Loading run pipeline…</p> : null}
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6">
<StatCard label="Provider" value={capturedProviders.join(", ") || inspection?.trace?.provider || "PRP only"} detail={inspection?.trace ? `captured through ${inspection.trace.provider}` : "capture was off"} />
<StatCard label="Raw frames" value={frames.length} detail={inspection?.trace ? formatBytes(inspection.trace.byteCount) : "no exact bytes"} />
<StatCard label="Operations" value={operations.length} detail="correlated groups" />
<StatCard label="PRP events" value={events.length} detail={`${visibleEventCount} visible`} />
<StatCard label="Mappings" value={interpretations.length} detail={`${ignoredCount} ignored`} />
<StatCard label="Run status" value={run?.status ?? "unknown"} detail={inspection?.trace ? traceBadgeLabel(inspection.trace.status, inspection.trace.expiresAt) : "Raw capture off"} />
</div>
<div className="grid gap-4 lg:grid-cols-2">
<section className="rounded-lg border border-border bg-card p-4">
<h3 className="text-sm font-semibold">Interpretation outcomes</h3>
<div className="mt-3 space-y-2">
{Object.entries(dispositionCounts).length ? Object.entries(dispositionCounts).map(([label, count]) => (
<div key={label} className="flex items-center gap-3 text-sm">
<Badge variant="outline" className="w-24 justify-center capitalize">{label}</Badge>
<div className="h-2 flex-1 overflow-hidden rounded-full bg-muted"><div className="h-full rounded-full bg-primary" style={{ width: `${Math.max(4, (count / Math.max(1, interpretations.length)) * 100)}%` }} /></div>
<span className="w-8 text-right font-mono text-xs">{count}</span>
</div>
)) : <p className="text-sm text-muted-foreground">No interpretation records were persisted.</p>}
</div>
</section>
<section className="rounded-lg border border-border bg-card p-4">
<h3 className="text-sm font-semibold">Recent correlated operations</h3>
<div className="mt-2 divide-y divide-border/70">
{operations.slice(-6).reverse().map((operation) => (
<button key={operation.key} type="button" onClick={() => selectOperation(operation)} className="flex w-full items-center gap-3 py-2 text-left hover:text-primary">
<span className={cn("h-2 w-2 rounded-full", operation.events.length ? "bg-primary" : "bg-muted-foreground")} />
<span className="min-w-0 flex-1"><span className="block truncate text-sm font-medium">{operation.title}</span><span className="block truncate text-xs text-muted-foreground">{operation.subtitle}</span></span>
<ArrowRight className="h-4 w-4 text-muted-foreground" />
</button>
))}
</div>
</section>
</div>
<section className="rounded-lg border border-border bg-accent/30 p-4">
<div className="flex gap-3"><ShieldAlert className="mt-0.5 h-5 w-5 shrink-0 text-accent-foreground" /><div><h3 className="text-sm font-semibold">Sensitive debug data</h3><p className="mt-1 text-sm text-muted-foreground">Parsed frames are redacted on the server. Exact reveals and downloads are administrator-only, warned, audited, and automatically expire.</p></div></div>
</section>
</div>
</ScrollArea>
) : null}
{view === "pipeline" ? (
<>
<div className="grid gap-2 border-b border-border p-3 sm:grid-cols-2 xl:grid-cols-(--gtc-runner-inspector-filters)">
<div className="relative"><Search className="absolute left-2.5 top-2.5 h-4 w-4 text-muted-foreground" /><Input className="pl-8" value={query} onChange={(event) => setQuery(event.target.value)} placeholder="Search operations, fields, and events" /></div>
<Select value={direction} onValueChange={setDirection}><SelectTrigger><SelectValue placeholder="Direction" /></SelectTrigger><SelectContent><SelectItem value="all">All directions</SelectItem><SelectItem value="client_to_provider">Client → provider</SelectItem><SelectItem value="provider_to_client">Provider → client</SelectItem><SelectItem value="provider_stderr">Provider stderr</SelectItem></SelectContent></Select>
<Select value={nativeMethod} onValueChange={setNativeMethod}><SelectTrigger><SelectValue placeholder="Native method" /></SelectTrigger><SelectContent><SelectItem value="all">All native methods</SelectItem>{unique(frames.map(frameMethod)).sort().map((value) => <SelectItem key={value} value={value}>{value}</SelectItem>)}</SelectContent></Select>
<Select value={disposition} onValueChange={setDisposition}><SelectTrigger><SelectValue placeholder="Mapping" /></SelectTrigger><SelectContent><SelectItem value="all">All mappings</SelectItem>{["mapped", "generic", "ignored", "rejected", "operator_only"].map((value) => <SelectItem key={value} value={value}>{value}</SelectItem>)}</SelectContent></Select>
<Select value={prpType} onValueChange={setPrpType}><SelectTrigger><SelectValue placeholder="PRP type" /></SelectTrigger><SelectContent><SelectItem value="all">All PRP types</SelectItem>{unique(events.map((event) => event.eventType)).sort().map((value) => <SelectItem key={value} value={value}>{value}</SelectItem>)}</SelectContent></Select>
<Select value={visibility} onValueChange={setVisibility}><SelectTrigger><SelectValue placeholder="Visibility" /></SelectTrigger><SelectContent><SelectItem value="all">Visible + hidden</SelectItem><SelectItem value="visible">Visible</SelectItem><SelectItem value="hidden">Hidden</SelectItem></SelectContent></Select>
</div>
<div className="grid min-h-0 flex-1 lg:grid-cols-(--gtc-runner-inspector-pipeline)">
<ScrollArea className="border-r border-border">
<div className="p-2">
{loading ? <p className="p-3 text-sm text-muted-foreground">Loading run pipeline…</p> : null}
{!loading && filteredOperations.length === 0 ? <p className="p-3 text-sm text-muted-foreground">No operations match these filters.</p> : null}
{filteredOperations.map((operation) => (
<button key={operation.key} type="button" onClick={() => setSelectedKey(operation.key)} className="mb-1 w-full rounded-lg border border-transparent px-3 py-2.5 text-left hover:bg-muted/50 data-[selected=true]:border-border data-[selected=true]:bg-muted" data-selected={selectedOperation?.key === operation.key}>
<span className="flex items-center gap-2"><span className={cn("h-2 w-2 shrink-0 rounded-full", operation.events.length ? operation.visible ? "bg-primary" : "bg-secondary-foreground" : "bg-muted-foreground")} role="img" aria-label={operation.events.length ? operation.visible ? "Visible production event" : "Hidden production event" : "No production event"} /><span className="min-w-0 flex-1 truncate text-sm font-medium">{operation.title}</span>{operation.frames.length > 1 ? <Badge variant="outline" className="px-1.5 text-(length:--text-nano)">{operation.frames.length} frames</Badge> : null}</span>
<span className="mt-1 block truncate pl-4 text-(length:--text-nano) text-muted-foreground">{operation.subtitle}</span>
<span className="mt-1.5 flex flex-wrap gap-1 pl-4">{operation.prpTypes.slice(0, 3).map((type) => <span key={type} className="rounded bg-background px-1.5 py-0.5 font-mono text-(length:--text-nano) text-muted-foreground">{type}</span>)}</span>
</button>
))}
</div>
</ScrollArea>
<ScrollArea>
{selectedOperation ? (
<div className="space-y-6 p-5">
<div className="flex flex-wrap items-center gap-2 rounded-lg border border-border bg-muted/20 px-4 py-3">
<strong className="text-sm">{selectedOperation.title}</strong><ArrowRight className="h-3.5 w-3.5 text-muted-foreground" /><span className="text-xs text-muted-foreground">{selectedOperation.frames.length} raw frame{selectedOperation.frames.length === 1 ? "" : "s"}</span><ArrowRight className="h-3.5 w-3.5 text-muted-foreground" /><span className="text-xs text-muted-foreground">{selectedOperation.interpretations.length} mapping stage{selectedOperation.interpretations.length === 1 ? "" : "s"}</span><ArrowRight className="h-3.5 w-3.5 text-muted-foreground" /><span className="text-xs text-muted-foreground">{selectedEvents.length} PRP event{selectedEvents.length === 1 ? "" : "s"}</span>
</div>
<section>
<div className="mb-2 flex flex-wrap items-center gap-2"><span className="flex h-6 w-6 items-center justify-center rounded-full bg-accent text-xs font-semibold text-accent-foreground">1</span><h3 className="text-sm font-semibold">Provider frames</h3><span className="text-xs text-muted-foreground">server-redacted by default</span></div>
{selectedOperation.frames.length > 1 ? <div className="mb-2 flex flex-wrap gap-1">{selectedOperation.frames.map((frame) => <button key={Number(frame.frameId)} type="button" onClick={() => setSelectedFrameId(Number(frame.frameId))} className={cn("rounded-md border px-2 py-1 font-mono text-xs", Number(frame.frameId) === Number(selectedFrame?.frameId) ? "border-primary/50 bg-primary/10 text-foreground" : "border-border text-muted-foreground hover:bg-muted")}>#{String(frame.frameId)} {frameMethod(frame) || text(frame.direction)}</button>)}</div> : null}
{selectedFrame ? (
<div className="space-y-2">
<div className="flex flex-wrap items-center gap-2 text-xs text-muted-foreground"><Badge variant="outline">frame {String(selectedFrame.frameId)}</Badge><span>{text(selectedFrame.direction).replaceAll("_", " ")}</span><span>{formatBytes(Number(selectedFrame.byteLength) || 0)}</span><code className="truncate">{text(selectedFrame.digest)}</code></div>
<JsonExplorer value={selectedFrame.parsed} />
{Array.isArray(selectedFrame.withheldPaths) && selectedFrame.withheldPaths.length > 0 ? <div className="flex items-start gap-2 rounded-md border border-border bg-accent/30 px-3 py-2 text-xs text-muted-foreground"><ShieldAlert className="mt-0.5 h-3.5 w-3.5 shrink-0 text-accent-foreground" /><span>Withheld paths: {selectedFrame.withheldPaths.join(", ")}</span></div> : null}
{canInspectRaw === true ? <Button size="sm" variant="outline" onClick={() => void reveal(Number(selectedFrame.frameId))}><Eye className="mr-1.5 h-4 w-4" />Reveal exact frame</Button> : null}
{revealedFrame ? <div className="rounded-md border border-destructive/30 p-2"><p className="mb-2 flex items-center gap-1.5 text-xs font-medium text-destructive"><ShieldAlert className="h-3.5 w-3.5" />Exact unredacted frame</p><JsonExplorer value={decodeExactFrame(revealedFrame.rawBase64)} label="Search exact frame" /></div> : null}
</div>
) : <p className="text-sm text-muted-foreground">This PRP event has no recoverable raw frame.</p>}
</section>
<section>
<div className="mb-3 flex items-center gap-2"><span className="flex h-6 w-6 items-center justify-center rounded-full bg-primary/10 text-xs font-semibold text-primary">2</span><h3 className="text-sm font-semibold">Interpretation stages</h3></div>
{selectedInterpretations.length ? selectedInterpretations.map((entry, index) => <InterpretationStage key={`${entry.debugChannel}:${entry.debugSequence}:${index}`} entry={entry} last={index === selectedInterpretations.length - 1} />) : <p className="text-sm text-muted-foreground">No interpretation stage was recorded for this frame.</p>}
</section>
<section>
<div className="mb-3 flex items-center gap-2"><span className="flex h-6 w-6 items-center justify-center rounded-full bg-secondary text-xs font-semibold text-secondary-foreground">3</span><h3 className="text-sm font-semibold">Canonical PRP events</h3></div>
{selectedEvents.length ? selectedEvents.map((event) => (
<div key={event.id} className="mb-3 overflow-hidden rounded-lg border border-border bg-card">
<div className="flex flex-wrap items-center gap-2 border-b border-border/70 px-3 py-2"><code className="text-xs font-semibold">{event.eventType}</code><Badge variant="outline">seq {event.seq}</Badge><button type="button" className="ml-auto text-xs text-primary underline-offset-4 hover:underline" onClick={() => { setVisibility(visibilityDecision(event).visible ? "visible" : "hidden"); setSelectedKey(selectedOperation.key); }}>Why isn’t this visible?</button></div>
<dl className="grid gap-x-4 gap-y-1 px-3 py-2 text-xs sm:grid-cols-(--gtc-runner-inspector-fields)">{typedPrpFields(event).map(([label, value]) => <div key={label} className="contents"><dt className="text-muted-foreground">{label}</dt><dd className="min-w-0 break-words font-mono">{value}</dd></div>)}</dl>
<details className="border-t border-border/70"><summary className="flex cursor-pointer list-none items-center gap-1 px-3 py-2 text-xs font-medium text-muted-foreground"><ChevronDown className="h-3.5 w-3.5" />Canonical event JSON</summary><div className="p-3 pt-0"><JsonExplorer value={eventPrp(event)} /></div></details>
</div>
)) : <p className="text-sm text-muted-foreground">This provider operation emitted no canonical PRP events.</p>}
</section>
<section>
<div className="mb-3 flex items-center gap-2"><span className="flex h-6 w-6 items-center justify-center rounded-full bg-muted text-xs font-semibold text-foreground">4</span><h3 className="text-sm font-semibold">Production presentation</h3></div>
{selectedEvents.length ? selectedEvents.map((event) => {
const decision = visibilityDecision(event);
return <div key={event.id} className="mb-4 grid gap-3 xl:grid-cols-(--gtc-runner-inspector-presentation)"><dl className="grid grid-cols-(--gtc-runner-inspector-fields) gap-x-3 gap-y-1 rounded-lg border border-border p-3 text-sm"><dt className="text-muted-foreground">Visible</dt><dd className="flex items-center gap-1.5">{decision.visible ? <Check className="h-3.5 w-3.5 text-primary" /> : <CircleOff className="h-3.5 w-3.5 text-muted-foreground" />}{decision.visible ? "yes" : "no"}</dd><dt className="text-muted-foreground">Surface</dt><dd>{decision.surface}</dd><dt className="text-muted-foreground">Container</dt><dd>{decision.container}</dd><dt className="text-muted-foreground">State</dt><dd>{decision.state}</dd><dt className="text-muted-foreground">Action</dt><dd>{decision.action}</dd><dt className="text-muted-foreground">Reason</dt><dd>{decision.reason}</dd><dt className="text-muted-foreground">Reason code</dt><dd><code className="text-xs">{decision.reasonCode}</code></dd></dl><div><p className="mb-2 text-(length:--text-nano) font-medium uppercase tracking-wide text-muted-foreground">Production surface preview</p><ProductionSurfacePreview event={event} runId={runId} /></div></div>;
}) : <p className="text-sm text-muted-foreground">No presentation surface was emitted for this operation.</p>}
{Object.keys(presentationDecision).length > 0 ? <details className="rounded-lg border border-border"><summary className="cursor-pointer px-3 py-2 text-xs font-medium text-muted-foreground">Resolved final response decision</summary><div className="p-3 pt-0"><JsonExplorer value={presentationDecision} /></div></details> : null}
{verificationCaveats.length > 0 || ignoredAttentionRequests.length > 0 ? (
<div className="mt-3 rounded-lg border border-border bg-accent/30 p-3">
<p className="text-xs font-semibold text-accent-foreground">Semantic finalization lineage</p>
<p className="mt-1 text-xs text-muted-foreground">
Provider-native transport and model-authored tool arguments are separate layers. PRP normalized the model payload, then server policy chose the issue disposition.
</p>
{ignoredAttentionRequests.some((candidate) => record(candidate).sourceKind === "environment_constraint") ? (
<p className="mt-2 text-xs">
<code>environment_constraint</code> was model-authored tool payload data, not a Codex app-server event. It was normalized into a non-blocking verification caveat.
</p>
) : null}
<dl className="mt-2 grid grid-cols-(--gtc-runner-inspector-fields) gap-x-3 gap-y-1 text-xs">
<dt className="text-muted-foreground">Policy</dt><dd><code>{text(runResultJson.finalizationPolicyVersion) || "unknown"}</code></dd>
<dt className="text-muted-foreground">Decision reason</dt><dd><code>{text(runResultJson.finalizationReasonCode) || "unknown"}</code></dd>
</dl>
<details className="mt-2"><summary className="cursor-pointer text-xs font-medium text-muted-foreground">Normalized caveats and ignored requests</summary><div className="mt-2"><JsonExplorer value={{ verificationCaveats, ignoredAttentionRequests }} /></div></details>
</div>
) : null}
</section>
</div>
) : <div className="p-5 text-sm text-muted-foreground">Select a correlated operation to inspect its pipeline.</div>}
</ScrollArea>
</div>
</>
) : null}
{view === "trace" ? (
<div className="grid min-h-0 flex-1 md:grid-cols-(--gtc-runner-inspector-trace)">
<ScrollArea className="border-r border-border"><div className="p-2">{frames.length ? [...frames].sort((left, right) => Number(left.frameId) - Number(right.frameId)).map((frame) => <button key={Number(frame.frameId)} type="button" onClick={() => { const operation = operations.find((candidate) => candidate.frames.some((candidateFrame) => Number(candidateFrame.frameId) === Number(frame.frameId))); if (operation) setSelectedKey(operation.key); setSelectedFrameId(Number(frame.frameId)); }} className={cn("mb-1 w-full rounded-md border px-3 py-2 text-left", Number(frame.frameId) === Number(selectedFrame?.frameId) ? "border-primary/40 bg-primary/10" : "border-transparent hover:bg-muted/50")}><span className="flex items-center gap-2 text-sm font-medium"><span className="font-mono text-xs text-muted-foreground">#{String(frame.frameId)}</span><span className="truncate">{frameMethod(frame) || text(frame.direction)}</span></span><span className="mt-1 block text-(length:--text-nano) text-muted-foreground">{text(frame.direction).replaceAll("_", " ")} · {formatBytes(Number(frame.byteLength) || 0)}</span></button>) : <p className="p-3 text-sm text-muted-foreground">No raw frames were captured for this run.</p>}</div></ScrollArea>
<ScrollArea><div className="space-y-3 p-5">{selectedFrame ? <><div className="flex flex-wrap items-center gap-2"><h3 className="text-sm font-semibold">Exact trace frame #{String(selectedFrame.frameId)}</h3><Badge variant="outline">{frameMethod(selectedFrame) || text(selectedFrame.direction)}</Badge></div><JsonExplorer value={selectedFrame.parsed} />{canInspectRaw === true ? <Button size="sm" variant="outline" onClick={() => void reveal(Number(selectedFrame.frameId))}><Eye className="mr-1.5 h-4 w-4" />Reveal exact frame</Button> : null}{revealedFrame ? <JsonExplorer value={decodeExactFrame(revealedFrame.rawBase64)} label="Search exact frame" /> : null}</> : <p className="text-sm text-muted-foreground">Select a frame from the chronological trace.</p>}</div></ScrollArea>
</div>
) : null}
{canInspectRaw === true && inspection?.trace?.runId === runId ? (
<div className="flex flex-wrap items-center justify-between gap-2 border-t border-border p-3">
<p className="text-xs text-muted-foreground">{inspection.trace.frameCount} frames · {formatBytes(inspection.trace.byteCount)} · expires {new Date(inspection.trace.expiresAt).toLocaleString()}</p>
<div className="flex gap-2"><Button size="sm" variant="outline" onClick={() => void downloadTrace()}><Download className="mr-1.5 h-4 w-4" />Download exact trace</Button><Button size="sm" variant="destructive" onClick={() => void deleteTrace()}><Trash2 className="mr-1.5 h-4 w-4" />Delete trace</Button></div>
</div>
) : null}
</div>
</SheetContent>
</Sheet>
);
}