Files
PaperClipAI/ui/src/components/RunnerInspector.test.tsx
T
a3313f6944 fix(ui): complete read recovery and enforce query error rendering (#18822)
## Thinking Path

> - Paperclip lets people manage agent work through the board.
> - The board reads task, agent, settings, plugin, and run data from the
server.
> - A deploy can interrupt those reads after content is already visible.
> - Local error branches can replace that content with an error or show
raw codes.
> - This pull request moves the remaining reads to the shared recovery
behavior.
> - The query-error guard prevents new local error patterns from
returning.

## Linked Issues or Issue Description

**What happened?**
Loaded lists, settings, task panels, and manual reads could clear their
content or show errors during a server restart. Some reads also disabled
the shared retry policy.

**Expected behavior**
Keep loaded content during a temporary failure. Show the shared
connection banner. Retry reads after recovery. Replace content after a
real access denial or missing resource, with readable copy.

**Steps to reproduce**
Open a task, settings page, or run log. Return 503 with
`tenant_app_unavailable` from the API. Restore the API and check that
content refreshes. Repeat with 403 and 404 to check the failure state.

Related merged work: Refs #15754, Refs #15772, Refs #17960. This change
covers remaining reads and enforcement. Browser persistence and the
outage browser suite follow separately.

## What Changed

- Repair an existing PostgreSQL deadline fixture at UTC boundaries. A
daily cap defers a heartbeat until both its original interval and the
next UTC day allow it. Keep the counted run in its seeded day and cover
noon, just before midnight and the first second after midnight with
explicit clocks and real PostgreSQL. Production scheduling is unchanged.

- Integrate the saved list, settings, shell, task, and manual-read
recovery changes against current master. Preserve the current task,
Inbox, and run-log structure.
- Add `useResilientFetch` for component-local reads. Preserve loaded
data, cancel stale requests, and retry through the shared connectivity
policy. Keep browser and pairing capabilities in memory.
- Retain run log chunks across stream changes and catch up after
recovery without duplicate chunks or stalled requests.
- Keep genuine authorization and missing-resource failures visible.
Translate known single-word codes such as `forbidden`. Show known skills
and disconnected-app failures before sibling loading states. Keep Retry
bound to its failed request.
- Make the query-error guard enforce by default and run it in the CI
policy job. Document the narrow exceptions and the read recovery
contract.
- Invalidate pending OAuth discovery when the operator leaves setup or
its context changes. Keep reserved-popup cleanup bound to the current
attempt.

## Verification

- Current read head `09d1db29dea` is merge-ready: all 59 check runs are
completed (54 successful, three skipped, two neutral), the legacy Snyk
status is successful, and both required `ci / verify` and `ci / e2e`
gates pass. Greptile Apex reviewed this exact head at 5/5 with no
unresolved threads; the branch has no merge conflicts. The current CI
run is `41748339602`.
- Current read head `09d1db29dea` includes master `683fa4725e9`. The
latest change touches only the existing server deadline test fixture.
Application source remains the previously verified `86376fa751e`
revision: all 60 affected skills/disconnected-app tests and both
application UI typechecks passed, along with token gates and the
enforcing query-error guard. Those UI suites were not repeated for this
test-only repair.
- The fixture repair passes all seven deadline integration cases and the
complete server typecheck, including the server test layer. The old
assertion deterministically fails at 23:59:06.888 UTC; its
one-second-old run also fails the first-second-of-day probe. The
corrected fixture passes noon and both midnight probes. The permanent
three-case coverage uses seeded 2099 clocks.
- Twelve new regressions fail against the previous implementation. Both
skills variants and disconnected-app pages show either request's 403 or
429 while the sibling pauses or remains fetching. Permission failures
stay visible. Retry stays usable for its failed request and recovers
independently. An initial test-fixture query-key typo was corrected
before the passing run. The finding is replied to and resolved. Greptile
reviewed prior application revision `86376fa751e` at 5/5 with no new
actionable findings or unresolved threads. The current test-only head
has now received its own Apex 5/5 review with no unresolved threads, and
its CI is successful as recorded above.
- The preceding OAuth fix passed all 237 setup tests on both application
branches. Its ten regressions prove that Back, Cancel, unmount, or
context changes retire initial discovery without creating a connection,
caching old identities, navigating an abandoned popup, or closing a
newer attempt.
- Earlier master integration `e992f770339` passed all 309 tests across
eight affected Slack, recovery, retry, submission and task files, plus
UI typecheck, token gates, enforcing query-error scan and seven guard
tests. The integration preserves Slack workspace reconnect and shared
recovery, readable execution errors, source-inspection company scoping,
and current submission behavior. Newly inherited retained-file recovery
controls keep cached data through transient failures and hide it after
definitive denial.
- The preceding cache head `97c0bc3966f` passed 1,038 focused
cache/write/editor/account/task/API tests. All 22 built-UI
outage/write/loading cases passed in 4.7 minutes on exact browser head
`b7deaaee5da`, with strict checking of all three specs. The latest
skills/app rendering fix is covered by its affected component suites;
those broader cases were not repeated for this separate read path.
- Historical broader UI, CLI and build results and unresolved aggregate
server/DB/adapter failures remain in the PR discussion. Interrupted CI
runs were audited and recorded separately; they are not counted as
passes. Controlled local outages do not prove hosted authentication or a
real Cloud restart/deploy. Real hosted acceptance remains required after
merge and deployment.

Commands for the latest fix:

```sh
pnpm --filter @paperclipai/ui exec vitest run src/pages/CompanySkills.test.tsx src/pages/apps/AppNotConnected.test.tsx
pnpm --filter @paperclipai/ui typecheck
pnpm check:token-gates
node scripts/check-query-error-rendering.mjs --enforce --summary
```

## Risks

This migration touches many read surfaces. The main risks are initial
loading, request cancellation, retained authorization state, and run-log
paging. The tests cover those states. There are no server, schema,
dependency, or API contract changes. The follow-up browser suite and
hosted deploy acceptance are required before release acceptance.

Recovered commits retain their original authors. If squash-merging,
preserve `Co-Authored-By: Bender (Fable) <bender@agents.paperclip.ing>`
and the Paperclip trailer.

## Model Used

OpenAI `gpt-6.1-sol` through Codex, with xhigh reasoning effort, tool
use, and code execution. The context window size is not exposed. Saved
source commits keep their original authorship.

## 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 described the issue in-PR following the bug report template
- [x] I have not referenced internal/instance-local Paperclip issues or
links
- [x] My branch name describes the change and contains no internal
ticket id
- [x] The affected local tests pass; broader limitations are recorded
above
- [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 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: devinfoley <139239+devinfoley@users.noreply.github.com>
Co-authored-by: Paperclip <noreply@paperclip.ing>
Co-authored-by: Bender (Fable) <bender@agents.paperclip.ing>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
2026-11-04 14:07:18 -08:00

848 lines
29 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.
// @vitest-environment jsdom
import { createElement, type ReactNode } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { ApiError } from "@/api/client";
import { ConnectivityProvider, createConnectivityStore, type ConnectivityStore } from "@/lib/connectivity";
import { RunnerInspector } from "./RunnerInspector";
const accessMock = vi.hoisted(() => vi.fn());
const eventsMock = vi.hoisted(() => vi.fn());
const traceMock = vi.hoisted(() => vi.fn());
const revealMock = vi.hoisted(() => vi.fn());
const downloadMock = vi.hoisted(() => vi.fn());
const deleteMock = vi.hoisted(() => vi.fn());
vi.mock("@/api/access", () => ({
accessApi: { getCurrentBoardAccess: accessMock },
}));
vi.mock("@/api/heartbeats", () => ({
heartbeatsApi: {
events: eventsMock,
providerTrace: traceMock,
revealProviderTraceFrame: revealMock,
downloadProviderTrace: downloadMock,
deleteProviderTrace: deleteMock,
},
}));
vi.mock("@/components/ui/sheet", () => ({
Sheet: ({ open, children }: { open: boolean; children: ReactNode }) =>
open ? createElement("div", null, children) : null,
SheetContent: ({ children }: { children: ReactNode }) =>
createElement("div", null, children),
SheetDescription: ({ children }: { children: ReactNode }) =>
createElement("p", null, children),
SheetHeader: ({ children }: { children: ReactNode }) =>
createElement("header", null, children),
SheetTitle: ({ children }: { children: ReactNode }) =>
createElement("h2", null, children),
}));
vi.mock("@/components/ui/select", () => ({
Select: ({ children }: { children: ReactNode }) =>
createElement("div", null, children),
SelectContent: ({ children }: { children: ReactNode }) =>
createElement("div", null, children),
SelectItem: ({ children }: { children: ReactNode }) =>
createElement("span", null, children),
SelectTrigger: ({ children }: { children: ReactNode }) =>
createElement("button", { type: "button" }, children),
SelectValue: ({ placeholder }: { placeholder?: string }) =>
createElement("span", null, placeholder),
}));
vi.mock("@/components/ui/scroll-area", () => ({
ScrollArea: ({ children }: { children: ReactNode }) =>
createElement("div", null, children),
}));
(
globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;
async function flush() {
for (let index = 0; index < 5; index += 1) {
await Promise.resolve();
await new Promise((resolve) => setTimeout(resolve, 0));
flushSync(() => {});
}
}
function traceInspection(runId: string, marker: string) {
return {
trace: {
id: `trace-${runId}`,
runId,
companyId: "company-1",
status: "complete",
provider: "codex",
frameCount: 1,
byteCount: 31,
digest: `sha256:${"a".repeat(64)}`,
reason: null,
requestedBy: "local-admin",
createdAt: "2026-08-22T12:00:00.000Z",
expiresAt: "2026-08-23T12:00:00.000Z",
deletedAt: null,
schema: "paperclip.provider_trace_metadata.v1",
},
entries: [
{
kind: "frame",
frameId: 1,
direction: "provider_to_client",
byteLength: 31,
parsed: { method: "item/completed", marker },
withheldPaths: ["secret"],
},
],
};
}
function deferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((next) => {
resolve = next;
});
return { promise, resolve };
}
describe("RunnerInspector", () => {
let container: HTMLDivElement;
let root: Root;
beforeEach(() => {
window.history.replaceState(null, "", "/");
container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
accessMock.mockResolvedValue({
source: "local_implicit",
isInstanceAdmin: false,
});
eventsMock.mockResolvedValue([]);
traceMock.mockResolvedValue({ trace: null, entries: [] });
});
afterEach(() => {
flushSync(() => root.unmount());
container.remove();
vi.restoreAllMocks();
vi.clearAllMocks();
});
it("keeps canonical inspection available when raw capture was disabled", async () => {
const rerun = vi.fn();
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
onRerunWithTrace={rerun}
/>,
),
);
await flush();
expect(container.textContent).toContain(
"Correlate exact provider traffic with every interpretation stage",
);
expect(container.textContent).toContain(
"Raw provider capture was off for this run.",
);
flushSync(() =>
Array.from(container.querySelectorAll("button"))
.find((button) => button.textContent?.trim() === "Overview")
?.click(),
);
await flush();
expect(container.textContent).toContain("Raw capture off");
expect(container.textContent).not.toContain("Expired");
const rerunButton = Array.from(container.querySelectorAll("button")).find(
(button) => button.textContent?.includes("Re-run with provider trace"),
);
flushSync(() => rerunButton?.click());
expect(rerun).toHaveBeenCalledOnce();
});
it("shows redacted frames by default and warns before exact reveal", async () => {
traceMock.mockResolvedValue({
trace: {
id: "trace-1",
runId: "run-1",
companyId: "company-1",
status: "complete",
provider: "codex",
frameCount: 1,
byteCount: 31,
digest: `sha256:${"a".repeat(64)}`,
reason: null,
requestedBy: "local-admin",
createdAt: "2026-08-22T12:00:00.000Z",
expiresAt: "2026-08-23T12:00:00.000Z",
deletedAt: null,
schema: "paperclip.provider_trace_metadata.v1",
},
entries: [
{
kind: "frame",
frameId: 1,
direction: "provider_to_client",
byteLength: 31,
parsed: { method: "item/completed", authorization: "[withheld]" },
withheldPaths: ["authorization"],
},
],
});
revealMock.mockResolvedValue({
schema: "paperclip.provider_trace_frame.v1",
frameId: 1,
timestamp: "1",
direction: "provider_to_client",
transport: "stdio_jsonl",
provider: "codex",
byteLength: 31,
digest: `sha256:${"b".repeat(64)}`,
rawBase64: btoa(JSON.stringify({ method: "item/completed" })),
});
const confirm = vi.spyOn(window, "confirm").mockReturnValue(true);
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
await flush();
expect(container.textContent).toContain("Withheld paths: authorization");
expect(container.textContent).not.toContain("rawBase64");
const reveal = Array.from(container.querySelectorAll("button")).find(
(button) => button.textContent?.includes("Reveal exact frame"),
);
flushSync(() => reveal?.click());
await flush();
expect(confirm).toHaveBeenCalledWith(
expect.stringContaining("may contain prompts"),
);
expect(revealMock).toHaveBeenCalledWith("run-1", 1);
});
it("does not reuse an exact frame after switching runs with the same frame id", async () => {
traceMock.mockImplementation(async (requestedRunId: string) =>
traceInspection(requestedRunId, `${requestedRunId}-redacted`),
);
revealMock.mockResolvedValue({
schema: "paperclip.provider_trace_frame.v1",
frameId: 1,
timestamp: "1",
direction: "provider_to_client",
transport: "stdio_jsonl",
provider: "codex",
byteLength: 31,
digest: `sha256:${"b".repeat(64)}`,
rawBase64: btoa(JSON.stringify({ secret: "run-one-secret" })),
});
vi.spyOn(window, "confirm").mockReturnValue(true);
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
await flush();
const reveal = Array.from(container.querySelectorAll("button")).find(
(button) => button.textContent?.includes("Reveal exact frame"),
);
flushSync(() => reveal?.click());
await flush();
expect(container.textContent).toContain("run-one-secret");
flushSync(() =>
root.render(
<RunnerInspector
runId="run-2"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
expect(container.textContent).not.toContain("run-one-secret");
await flush();
expect(container.textContent).toContain("run-2-redacted");
expect(container.textContent).not.toContain("run-one-secret");
});
it("clears exact frames when raw-trace access is revoked while open", async () => {
traceMock.mockResolvedValue(traceInspection("run-1", "run-1-redacted"));
revealMock.mockResolvedValue({
schema: "paperclip.provider_trace_frame.v1",
frameId: 1,
timestamp: "1",
direction: "provider_to_client",
transport: "stdio_jsonl",
provider: "codex",
byteLength: 31,
digest: `sha256:${"b".repeat(64)}`,
rawBase64: btoa(JSON.stringify({ secret: "revoked-secret" })),
});
vi.spyOn(window, "confirm").mockReturnValue(true);
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
await flush();
const reveal = Array.from(container.querySelectorAll("button")).find(
(button) => button.textContent?.includes("Reveal exact frame"),
);
flushSync(() => reveal?.click());
await flush();
expect(container.textContent).toContain("revoked-secret");
accessMock.mockResolvedValue({
source: "session",
isInstanceAdmin: false,
});
window.dispatchEvent(new Event("focus"));
await flush();
expect(accessMock.mock.calls.length).toBeGreaterThan(1);
expect(container.textContent).toContain(
"Raw provider traces require an instance administrator.",
);
expect(container.textContent).not.toContain("Reveal exact frame");
expect(container.textContent).not.toContain("revoked-secret");
});
it("does not let a stale initial access check override a newer denial", async () => {
const pendingInitialAccess = deferred<{
source: "local_implicit";
isInstanceAdmin: false;
}>();
accessMock
.mockReturnValueOnce(pendingInitialAccess.promise)
.mockResolvedValue({ source: "session", isInstanceAdmin: false });
traceMock.mockResolvedValue(traceInspection("run-1", "stale-secret"));
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
window.dispatchEvent(new Event("focus"));
await flush();
expect(container.textContent).toContain(
"Raw provider traces require an instance administrator.",
);
pendingInitialAccess.resolve({
source: "local_implicit",
isInstanceAdmin: false,
});
await flush();
expect(traceMock).not.toHaveBeenCalled();
expect(container.textContent).not.toContain("Reveal exact frame");
expect(container.textContent).not.toContain("stale-secret");
});
it("discards in-flight exact reads and disables privileged controls during deletion", async () => {
traceMock.mockResolvedValue(traceInspection("run-1", "run-1-redacted"));
const pendingReveal = deferred<{
schema: string;
frameId: number;
timestamp: string;
direction: string;
transport: string;
provider: string;
byteLength: number;
digest: string;
rawBase64: string;
}>();
const pendingDownload = deferred<Blob>();
const pendingDelete = deferred<void>();
const pendingPostDeleteAccess = deferred<{
source: "local_implicit";
isInstanceAdmin: false;
}>();
revealMock.mockReturnValue(pendingReveal.promise);
downloadMock.mockReturnValue(pendingDownload.promise);
deleteMock.mockReturnValue(pendingDelete.promise);
vi.spyOn(window, "confirm").mockReturnValue(true);
const anchorClick = vi
.spyOn(HTMLAnchorElement.prototype, "click")
.mockImplementation(() => undefined);
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
await flush();
const buttons = () => Array.from(container.querySelectorAll("button"));
flushSync(() =>
buttons()
.find((button) => button.textContent?.includes("Reveal exact frame"))
?.click(),
);
flushSync(() =>
buttons()
.find((button) => button.textContent?.includes("Download exact trace"))
?.click(),
);
await Promise.resolve();
expect(revealMock).toHaveBeenCalledWith("run-1", 1);
expect(downloadMock).toHaveBeenCalledWith("run-1");
flushSync(() =>
buttons()
.find((button) => button.textContent?.includes("Delete trace"))
?.click(),
);
expect(deleteMock).toHaveBeenCalledWith("run-1");
expect(container.textContent).not.toContain("Reveal exact frame");
expect(container.textContent).not.toContain("Download exact trace");
expect(container.textContent).not.toContain("Delete trace");
pendingReveal.resolve({
schema: "paperclip.provider_trace_frame.v1",
frameId: 1,
timestamp: "1",
direction: "provider_to_client",
transport: "stdio_jsonl",
provider: "codex",
byteLength: 31,
digest: `sha256:${"b".repeat(64)}`,
rawBase64: btoa(JSON.stringify({ secret: "late-secret" })),
});
pendingDownload.resolve(new Blob(["late raw trace"]));
await flush();
expect(container.textContent).not.toContain("late-secret");
expect(anchorClick).not.toHaveBeenCalled();
accessMock
.mockReturnValueOnce(pendingPostDeleteAccess.promise)
.mockResolvedValue({ source: "session", isInstanceAdmin: false });
pendingDelete.resolve();
await Promise.resolve();
window.dispatchEvent(new Event("focus"));
await flush();
expect(container.textContent).toContain(
"Raw provider traces require an instance administrator.",
);
pendingPostDeleteAccess.resolve({
source: "local_implicit",
isInstanceAdmin: false,
});
await flush();
expect(container.textContent).toContain(
"Raw provider traces require an instance administrator.",
);
expect(container.textContent).not.toContain("late-secret");
expect(anchorClick).not.toHaveBeenCalled();
});
it("keeps client and provider JSON-RPC id spaces separate when grouping operations", async () => {
eventsMock.mockResolvedValue([{
id: 41,
companyId: "company-1",
runId: "run-1",
agentId: "agent-1",
seq: 9,
eventType: "run.result.proposed",
stream: "stdout",
level: "info",
color: null,
message: null,
payload: { prpEvent: { sourceEventId: "runner:run-1:9", payload: {} } },
createdAt: "2026-08-22T12:00:03.000Z",
}]);
traceMock.mockResolvedValue({
trace: null,
entries: [
{ kind: "frame", frameId: 1, timestamp: "1", direction: "client_to_provider", parsed: { id: 1, method: "initialize" } },
{ kind: "frame", frameId: 2, timestamp: "2", direction: "provider_to_client", parsed: { id: 1, result: {} } },
{ kind: "frame", frameId: 3, timestamp: "3", direction: "provider_to_client", parsed: { id: 1, method: "item/tool/call", params: { callId: "finish-1", tool: "paperclip_finish" } } },
{ kind: "frame", frameId: 4, timestamp: "4", direction: "client_to_provider", parsed: { id: 1, result: { success: true } } },
{ kind: "interpretation", frameId: 3, stage: "typescript_codex_driver_normalization", disposition: "mapped", emittedEventIds: ["runner:run-1:9"], ruleId: "codex_driver.normalize.item/tool/call" },
],
});
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
await flush();
expect(container.textContent).toContain("frames 1–2 · 0 PRP events");
expect(container.textContent).toContain("frames 3–4 · 1 PRP event");
expect(container.textContent).not.toContain("frames 1–4");
});
it("correlates a Codex web search through every stage to canonical PRP and presentation", async () => {
eventsMock.mockResolvedValue([
{
id: 91,
companyId: "company-1",
runId: "run-1",
agentId: "agent-1",
seq: 42,
eventType: "research.completed",
stream: "stdout",
level: "info",
color: null,
message: null,
payload: {
prpEvent: {
eventType: "research.completed",
sourceEventId: "event_runner_000001",
sourceSequence: 42,
payload: { query: "best bbq sauce", status: "completed" },
},
},
createdAt: "2026-08-22T12:00:01.000Z",
},
]);
traceMock.mockResolvedValue({
trace: {
id: "trace-1",
runId: "run-1",
companyId: "company-1",
status: "complete",
provider: "codex",
frameCount: 1,
byteCount: 512,
digest: `sha256:${"a".repeat(64)}`,
reason: null,
requestedBy: "local-admin",
createdAt: "2026-08-22T12:00:00.000Z",
expiresAt: "2026-08-23T12:00:00.000Z",
deletedAt: null,
schema: "paperclip.provider_trace_metadata.v1",
},
entries: [
{
kind: "frame",
frameId: 27,
timestamp: "1787400001000",
direction: "provider_to_client",
byteLength: 512,
digest: `sha256:${"b".repeat(64)}`,
parsed: {
method: "item/completed",
params: {
item: {
id: "search-1",
type: "webSearch",
query: "best bbq sauce",
results: [{ title: "Sauce guide", url: "https://example.com" }],
},
},
},
withheldPaths: [],
},
{
kind: "interpretation",
frameId: 27,
debugChannel: "rust_native",
debugSequence: 1,
stage: "rust_jsonrpc_parse",
ruleId: "codex.notification",
disposition: "mapped",
emittedEventIds: [],
droppedFields: [],
reason: "Parsed Codex notification",
},
{
kind: "interpretation",
frameId: 27,
debugChannel: "rust_native",
debugSequence: 2,
stage: "rust_durable_normalization",
ruleId: "provider.notification.known",
disposition: "mapped",
emittedEventIds: ["event_runner_000001"],
droppedFields: ["params.item.results"],
fieldMappings: [
{
inputPath: "params.item.query",
outputPath: "payload.query",
action: "copied",
reason: "Preserved the search query",
},
{
inputPath: "params.item.results",
action: "dropped",
reason: "Raw provider results are not part of this semantic event",
},
],
reason: "Normalized provider web search",
},
{
kind: "interpretation",
frameId: 27,
debugChannel: "typescript_runnerd_rehydration",
debugSequence: 1,
stage: "typescript_runnerd_rehydration",
ruleId: "runnerd.rehydrate.research.completed",
disposition: "mapped",
emittedEventIds: ["event_runner_000001"],
droppedFields: [],
reason: "Rehydrated canonical event",
},
{
kind: "interpretation",
frameId: 27,
debugChannel: "typescript_runnerd_rehydration",
debugSequence: 2,
stage: "typescript_codex_driver_normalization",
ruleId: "codex_driver.normalize.item/completed",
disposition: "mapped",
emittedEventIds: ["event_runner_000001"],
droppedFields: [],
reason: "Emitted canonical PRP event",
},
],
});
flushSync(() =>
root.render(
<RunnerInspector
runId="run-1"
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>,
),
);
await flush();
expect(container.textContent).toContain("webSearch");
expect(container.textContent).toContain("rust_jsonrpc_parse");
expect(container.textContent).toContain("rust_durable_normalization");
expect(container.textContent).toContain("typescript_runnerd_rehydration");
expect(container.textContent).toContain("typescript_codex_driver_normalization");
expect(container.textContent).toContain("params.item.results");
expect(container.textContent).toContain("payload.query");
expect(container.textContent).toContain("research.completed");
expect(container.textContent).toContain("Production surface preview");
});
});
describe("RunnerInspector disconnect handling", () => {
let container: HTMLDivElement;
let root: Root;
let store: ConnectivityStore;
let probe: ReturnType<typeof vi.fn<() => Promise<{ reachable: boolean; retryAfterMs: number | null }>>>;
const unavailable = () => new ApiError("Paperclip is restarting.", 503, { error: "tenant_app_unavailable" });
const event = (runId: string, seq: number) => ({
id: seq,
companyId: "company-1",
runId,
agentId: "agent-1",
seq,
eventType: "run.result.proposed",
stream: "stdout",
level: "info",
color: null,
message: null,
payload: { prpEvent: { sourceEventId: `runner:${runId}:${seq}`, payload: {} } },
createdAt: "2026-08-22T12:00:03.000Z",
});
function render(runId = "run-1") {
flushSync(() =>
root.render(
<ConnectivityProvider store={store}>
<RunnerInspector
runId={runId}
run={{ status: "succeeded", resultJson: null }}
open
onOpenChange={vi.fn()}
/>
</ConnectivityProvider>,
),
);
}
beforeEach(() => {
window.history.replaceState(null, "", "/");
container = document.createElement("div");
document.body.appendChild(container);
root = createRoot(container);
probe = vi.fn(async () => ({ reachable: true, retryAfterMs: null }));
store = createConnectivityStore({ probe: probe as never, browserOnline: true });
accessMock.mockResolvedValue({ source: "local_implicit", isInstanceAdmin: false });
eventsMock.mockResolvedValue([]);
traceMock.mockResolvedValue({ trace: null, entries: [] });
});
afterEach(() => {
flushSync(() => root.unmount());
container.remove();
store.dispose();
vi.restoreAllMocks();
vi.clearAllMocks();
});
it("does not grant trace controls when a recovery read starts during deletion", async () => {
traceMock.mockResolvedValue(traceInspection("run-1", "retained-redacted-frame"));
const pendingDelete = deferred<void>();
deleteMock.mockReturnValue(pendingDelete.promise);
vi.spyOn(window, "confirm").mockReturnValue(true);
render(); await flush();
expect(container.textContent).toContain("Reveal exact frame");
const traceReads = traceMock.mock.calls.length;
const eventReads = eventsMock.mock.calls.length;
flushSync(() => [...container.querySelectorAll("button")].find(button => button.textContent?.includes("Delete trace"))!.click());
expect(deleteMock).toHaveBeenCalledWith("run-1");
probe.mockResolvedValue({ reachable: false, retryAfterMs: null });
store.reportError(unavailable()); await flush();
probe.mockResolvedValue({ reachable: true, retryAfterMs: null });
store.probeNow(); await flush();
expect(store.getSnapshot().status).toBe("online");
expect(eventsMock.mock.calls.length).toBeGreaterThan(eventReads);
expect(traceMock).toHaveBeenCalledTimes(traceReads);
expect(container.textContent).not.toContain("Reveal exact frame");
expect(container.textContent).not.toContain("Download exact trace");
expect(container.textContent).not.toContain("Delete trace");
pendingDelete.resolve(); await flush();
expect(container.textContent).not.toContain("retained-redacted-frame");
});
it("keeps the loaded inspection on screen when the refetch after an outage fails transiently", async () => {
eventsMock.mockResolvedValue([event("run-1", 9)]);
traceMock.mockResolvedValue(traceInspection("run-1", "run-1-redacted"));
render();
await flush();
expect(container.textContent).toContain("run-1-redacted");
probe.mockResolvedValue({ reachable: false, retryAfterMs: null });
store.reportError(unavailable());
await flush();
expect(store.getSnapshot().status).toBe("reconnecting");
expect(container.textContent).toContain("run-1-redacted");
expect(container.querySelector('[data-query-view="error"]')).toBeNull();
// Recovery refetches; a transient failure of that refetch retries quietly.
eventsMock.mockRejectedValue(new ApiError("Too many requests", 429, { error: "rate_limited" }));
probe.mockResolvedValue({ reachable: true, retryAfterMs: null });
store.probeNow();
await flush();
expect(store.getSnapshot().status).toBe("online");
expect(container.textContent).toContain("run-1-redacted");
expect(container.textContent).toContain("Reveal exact frame");
expect(container.querySelector('[data-query-view="error"]')).toBeNull();
expect(container.textContent).not.toContain("rate_limited");
});
it("waits quietly for a first load during an outage and fills in on recovery", async () => {
probe.mockResolvedValue({ reachable: false, retryAfterMs: null });
store.reportError(unavailable());
await flush();
expect(store.getSnapshot().status).toBe("reconnecting");
eventsMock.mockResolvedValue([event("run-1", 9)]);
render();
await flush();
expect(accessMock).not.toHaveBeenCalledWith(); // the initial load is paused, not failing
expect(eventsMock).not.toHaveBeenCalled();
expect(container.textContent).toContain("Loading run pipeline…");
expect(container.querySelector('[data-query-view="error"]')).toBeNull();
probe.mockResolvedValue({ reachable: true, retryAfterMs: null });
store.probeNow();
await flush();
expect(store.getSnapshot().status).toBe("online");
expect(eventsMock).toHaveBeenCalledWith("run-1", 0, 1_000);
expect(container.textContent).not.toContain("Loading run pipeline…");
expect(container.textContent).toContain("1 PRP event");
});
it("shows readable copy for a real failure, never raw access, and retries on request", async () => {
traceMock.mockRejectedValueOnce(new ApiError("stale_revision", 409, { error: "stale_revision" }));
render();
await flush();
const alert = container.querySelector('[data-query-view="error"]');
expect(alert).not.toBeNull();
expect(alert?.textContent).toContain("Couldn't load the runner inspection");
expect(alert?.textContent).not.toContain("stale_revision");
expect(container.textContent).not.toContain("Reveal exact frame");
expect(container.textContent).not.toContain("Download exact trace");
traceMock.mockResolvedValue(traceInspection("run-1", "run-1-redacted"));
flushSync(() => alert?.querySelector("button")?.click());
await flush();
expect(container.querySelector('[data-query-view="error"]')).toBeNull();
expect(container.textContent).toContain("run-1-redacted");
});
it("leaves out Retry and keeps raw controls hidden when access is forbidden", async () => {
accessMock.mockRejectedValue(new ApiError("Forbidden", 403, { error: "Forbidden" }));
render();
await flush();
const alert = container.querySelector('[data-query-view="error"]');
expect(alert?.textContent).toContain("Forbidden");
expect(alert?.querySelector("button")).toBeNull();
expect(traceMock).not.toHaveBeenCalled();
expect(container.textContent).not.toContain("Reveal exact frame");
});
it("ignores a load that finishes after the run changed", async () => {
const pendingFirst = deferred<ReturnType<typeof event>[]>();
eventsMock.mockImplementation(async (runId: string) =>
runId === "run-1" ? pendingFirst.promise : [event(runId, 2)],
);
traceMock.mockImplementation(async (runId: string) => traceInspection(runId, `${runId}-redacted`));
render("run-1");
await flush();
expect(container.textContent).toContain("Loading run pipeline…");
render("run-2");
await flush();
expect(container.textContent).toContain("run-2-redacted");
pendingFirst.resolve([event("run-1", 1), event("run-1", 2), event("run-1", 3)]);
await flush();
expect(container.textContent).toContain("run-2-redacted");
expect(container.textContent).not.toContain("run-1-redacted");
expect(container.textContent).toContain("runner:run-2:2");
expect(container.textContent).not.toContain("runner:run-1:");
});
});