Files
paperclip/ui/src/pages/InstanceExperimentalSettings.tsx
T
Devin Foley a904effb96 Add experimental newest-first issue thread (#5455)
## Thinking Path

> - Paperclip orchestrates AI agents for zero-human companies, so issue
threads are a core operator surface for reviewing work.
> - The issue detail page is the place where humans read agent messages,
user comments, and execution context together.
> - That thread originally rendered oldest-first, which made recent
activity harder to see during active review.
> - Reversing the thread order changes navigation expectations,
timestamp placement, and the "Jump to latest" affordance, so the UI
behavior needed to move as a coherent set.
> - Because this is a visible core-product behavior shift, it also
needed a safe rollout path instead of becoming the default immediately.
> - This pull request adds the newest-first issue thread behavior behind
an Experimental setting, updates the thread UI to match that mode, and
keeps the legacy oldest-first experience unchanged by default.
> - The benefit is that reviewers can opt into a more recent-first issue
workflow without forcing a global behavior change on every Paperclip
instance.

## What Changed

- Reversed issue thread rendering so the newest comments and messages
appear first when the experiment is enabled.
- Moved the plain comment timestamp into the card header in newest-first
mode and kept the legacy timestamp placement for oldest-first mode.
- Moved the `Jump to latest` control to the bottom of the thread in
newest-first mode while leaving the existing top placement for the
legacy mode.
- Added the `Enable Newest-First Issue Thread` experimental instance
setting and wired issue detail to read that toggle.
- Added regression coverage for thread order, timestamp placement,
jump-button placement, and the issue-detail experiment toggle behavior.

## Verification

- `pnpm -r typecheck`
- `pnpm test:run`
- `pnpm build`
- Focused checks that also passed during issue review:
- `pnpm vitest run src/components/IssueChatThread.test.tsx
src/pages/IssueDetail.test.tsx` in `ui/`
- `pnpm vitest run src/__tests__/instance-settings-routes.test.ts` in
`server/`
- Manual review path:
- Enable `Instance Settings > Experimental > Enable Newest-First Issue
Thread`
- Open an issue with comments/messages and confirm newest activity
renders first, timestamps move into the header, and `Jump to latest`
sits below the thread
- Disable the experiment and confirm the legacy oldest-first behavior
returns

## Risks

- Low risk: the behavioral change is gated behind an instance-level
experimental toggle and defaults off.
- The main regression risk is thread navigation drift between the two
modes, especially around anchor scrolling and the `Jump to latest`
affordance.
- There is some UI coupling between issue-detail query state and
experimental settings fetches, so future changes in that area should
keep both modes covered.
- Screenshots are not attached in this PR body; verification is
described with automated coverage and manual steps instead.

> I checked [`ROADMAP.md`](ROADMAP.md). This is a scoped issue-thread UX
improvement and rollout gate, not a duplicate of a roadmap-level planned
core feature.

## Model Used

- OpenAI Codex via the local `codex_local` Paperclip adapter,
GPT-5-based coding agent with terminal tool use and local code execution
in this repository worktree.

## 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 run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] If this change affects the UI, I have included before/after
screenshots
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [x] I will address all Greptile and reviewer comments before
requesting merge
2026-05-07 16:45:12 -07:00

436 lines
17 KiB
TypeScript

import { useEffect, useState } from "react";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Clock, FlaskConical, Play, Search } from "lucide-react";
import type {
IssueGraphLivenessAutoRecoveryPreview,
PatchInstanceExperimentalSettings,
} from "@paperclipai/shared";
import { instanceSettingsApi } from "@/api/instanceSettings";
import { useBreadcrumbs } from "../context/BreadcrumbContext";
import { queryKeys } from "../lib/queryKeys";
import { ToggleSwitch } from "@/components/ui/toggle-switch";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
function issueHref(identifier: string | null, issueId: string) {
if (!identifier) return `/issues/${issueId}`;
const prefix = identifier.split("-")[0] || "PAP";
return `/${prefix}/issues/${identifier}`;
}
function formatRecoveryState(state: string) {
return state.replace(/_/g, " ");
}
function RecoveryPreviewDialog({
preview,
open,
onOpenChange,
onEnableOnly,
onEnableAndRun,
isPending,
}: {
preview: IssueGraphLivenessAutoRecoveryPreview | null;
open: boolean;
onOpenChange: (open: boolean) => void;
onEnableOnly: () => void;
onEnableAndRun: () => void;
isPending: boolean;
}) {
const count = preview?.recoverableFindings ?? 0;
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-3xl">
<DialogHeader>
<DialogTitle>Confirm auto-recovery</DialogTitle>
<DialogDescription>
{preview
? `${count} recovery ${count === 1 ? "task" : "tasks"} match the last ${preview.lookbackHours} hours.`
: "Checking recovery candidates before enabling."}
</DialogDescription>
</DialogHeader>
<div className="max-h-[min(28rem,65vh)] space-y-3 overflow-y-auto pr-1">
{preview && preview.items.length === 0 ? (
<div className="rounded-md border border-border bg-muted/30 px-3 py-4 text-sm text-muted-foreground">
No recovery tasks would be created right now. Auto-recovery can still run for future liveness incidents in
this window.
</div>
) : null}
{preview?.items.map((item) => (
<div key={item.incidentKey} className="rounded-md border border-border bg-card px-3 py-3">
<div className="flex flex-wrap items-center gap-2">
<a
href={issueHref(item.identifier, item.issueId)}
className="text-sm font-medium text-primary underline-offset-2 hover:underline"
>
{item.identifier ?? item.issueId}
</a>
<span className="rounded-sm bg-muted px-1.5 py-0.5 text-xs text-muted-foreground">
{formatRecoveryState(item.state)}
</span>
</div>
<p className="mt-1 text-sm text-foreground">{item.title}</p>
<p className="mt-1 text-xs text-muted-foreground">{item.reason}</p>
<div className="mt-2 text-xs text-muted-foreground">
Recovery target:{" "}
<a
href={issueHref(item.recoveryIdentifier, item.recoveryIssueId)}
className="text-primary underline-offset-2 hover:underline"
>
{item.recoveryIdentifier ?? item.recoveryIssueId}
</a>
</div>
</div>
))}
</div>
{preview && preview.skippedOutsideLookback > 0 ? (
<p className="text-xs text-muted-foreground">
{preview.skippedOutsideLookback} current{" "}
{preview.skippedOutsideLookback === 1 ? "finding is" : "findings are"} outside the configured lookback and
will not be touched.
</p>
) : null}
<DialogFooter>
<Button variant="outline" onClick={() => onOpenChange(false)} disabled={isPending}>
Cancel
</Button>
<Button variant="outline" onClick={onEnableOnly} disabled={isPending || !preview}>
Enable only
</Button>
<Button onClick={onEnableAndRun} disabled={isPending || !preview}>
{count > 0 ? `Enable and create ${count}` : "Enable"}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
export function InstanceExperimentalSettings() {
const { setBreadcrumbs } = useBreadcrumbs();
const queryClient = useQueryClient();
const [actionError, setActionError] = useState<string | null>(null);
const [lookbackHoursDraft, setLookbackHoursDraft] = useState("24");
const [previewDialogOpen, setPreviewDialogOpen] = useState(false);
const [pendingPreview, setPendingPreview] = useState<IssueGraphLivenessAutoRecoveryPreview | null>(null);
useEffect(() => {
setBreadcrumbs([
{ label: "Instance Settings" },
{ label: "Experimental" },
]);
}, [setBreadcrumbs]);
const experimentalQuery = useQuery({
queryKey: queryKeys.instance.experimentalSettings,
queryFn: () => instanceSettingsApi.getExperimental(),
});
const toggleMutation = useMutation({
mutationFn: async (patch: PatchInstanceExperimentalSettings) =>
instanceSettingsApi.updateExperimental(patch),
onSuccess: async () => {
setActionError(null);
await Promise.all([
queryClient.invalidateQueries({ queryKey: queryKeys.instance.experimentalSettings }),
queryClient.invalidateQueries({ queryKey: queryKeys.health }),
]);
},
onError: (error) => {
setActionError(error instanceof Error ? error.message : "Failed to update experimental settings.");
},
});
const previewMutation = useMutation({
mutationFn: async (lookbackHours: number) =>
instanceSettingsApi.previewIssueGraphLivenessAutoRecovery({ lookbackHours }),
onSuccess: (preview) => {
setActionError(null);
setPendingPreview(preview);
setPreviewDialogOpen(true);
},
onError: (error) => {
setActionError(error instanceof Error ? error.message : "Failed to preview recovery tasks.");
},
});
const runRecoveryMutation = useMutation({
mutationFn: async (lookbackHours: number) =>
instanceSettingsApi.runIssueGraphLivenessAutoRecovery({ lookbackHours }),
onSuccess: async () => {
setActionError(null);
setPreviewDialogOpen(false);
await Promise.all([
queryClient.invalidateQueries({ queryKey: queryKeys.instance.experimentalSettings }),
queryClient.invalidateQueries({ queryKey: queryKeys.health }),
]);
},
onError: (error) => {
setActionError(error instanceof Error ? error.message : "Failed to create recovery tasks.");
},
});
useEffect(() => {
const next = experimentalQuery.data?.issueGraphLivenessAutoRecoveryLookbackHours;
if (typeof next === "number") {
setLookbackHoursDraft(String(next));
}
}, [experimentalQuery.data?.issueGraphLivenessAutoRecoveryLookbackHours]);
if (experimentalQuery.isLoading) {
return <div className="text-sm text-muted-foreground">Loading experimental settings...</div>;
}
if (experimentalQuery.error) {
return (
<div className="text-sm text-destructive">
{experimentalQuery.error instanceof Error
? experimentalQuery.error.message
: "Failed to load experimental settings."}
</div>
);
}
const enableEnvironments = experimentalQuery.data?.enableEnvironments === true;
const enableIsolatedWorkspaces = experimentalQuery.data?.enableIsolatedWorkspaces === true;
const enableNewestFirstIssueThread = experimentalQuery.data?.enableNewestFirstIssueThread === true;
const autoRestartDevServerWhenIdle = experimentalQuery.data?.autoRestartDevServerWhenIdle === true;
const enableIssueGraphLivenessAutoRecovery =
experimentalQuery.data?.enableIssueGraphLivenessAutoRecovery === true;
const lookbackHours =
experimentalQuery.data?.issueGraphLivenessAutoRecoveryLookbackHours ?? 24;
const parsedLookbackHours = Number.parseInt(lookbackHoursDraft, 10);
const lookbackHoursIsValid =
Number.isInteger(parsedLookbackHours) && parsedLookbackHours >= 1 && parsedLookbackHours <= 720;
const recoveryActionPending =
toggleMutation.isPending || previewMutation.isPending || runRecoveryMutation.isPending;
function previewForEnable() {
if (!lookbackHoursIsValid) {
setActionError("Lookback hours must be a whole number from 1 to 720.");
return;
}
previewMutation.mutate(parsedLookbackHours);
}
function enableOnly() {
if (!lookbackHoursIsValid) return;
toggleMutation.mutate({
enableIssueGraphLivenessAutoRecovery: true,
issueGraphLivenessAutoRecoveryLookbackHours: parsedLookbackHours,
}, {
onSuccess: () => setPreviewDialogOpen(false),
});
}
function enableAndRun() {
if (!lookbackHoursIsValid) return;
toggleMutation.mutate({
enableIssueGraphLivenessAutoRecovery: true,
issueGraphLivenessAutoRecoveryLookbackHours: parsedLookbackHours,
}, {
onSuccess: () => runRecoveryMutation.mutate(parsedLookbackHours),
});
}
return (
<div className="max-w-4xl space-y-6">
<div className="space-y-2">
<div className="flex items-center gap-2">
<FlaskConical className="h-5 w-5 text-muted-foreground" />
<h1 className="text-lg font-semibold">Experimental</h1>
</div>
<p className="text-sm text-muted-foreground">
Opt into features that are still being evaluated before they become default behavior.
</p>
</div>
{actionError && (
<div className="rounded-md border border-destructive/40 bg-destructive/5 px-3 py-2 text-sm text-destructive">
{actionError}
</div>
)}
<section className="rounded-xl border border-border bg-card p-5">
<div className="flex items-start justify-between gap-4">
<div className="space-y-1.5">
<h2 className="text-sm font-semibold">Enable Environments</h2>
<p className="max-w-2xl text-sm text-muted-foreground">
Show environment management in company settings and allow project and agent environment assignment
controls.
</p>
</div>
<ToggleSwitch
checked={enableEnvironments}
onCheckedChange={() => toggleMutation.mutate({ enableEnvironments: !enableEnvironments })}
disabled={toggleMutation.isPending}
aria-label="Toggle environments experimental setting"
/>
</div>
</section>
<section className="rounded-xl border border-border bg-card p-5">
<div className="flex items-start justify-between gap-4">
<div className="space-y-1.5">
<h2 className="text-sm font-semibold">Enable Isolated Workspaces</h2>
<p className="max-w-2xl text-sm text-muted-foreground">
Show execution workspace controls in project configuration and allow isolated workspace behavior for new
and existing issue runs.
</p>
</div>
<ToggleSwitch
checked={enableIsolatedWorkspaces}
onCheckedChange={() => toggleMutation.mutate({ enableIsolatedWorkspaces: !enableIsolatedWorkspaces })}
disabled={toggleMutation.isPending}
aria-label="Toggle isolated workspaces experimental setting"
/>
</div>
</section>
<section className="rounded-xl border border-border bg-card p-5">
<div className="flex items-start justify-between gap-4">
<div className="space-y-1.5">
<h2 className="text-sm font-semibold">Enable Newest-First Issue Thread</h2>
<p className="max-w-2xl text-sm text-muted-foreground">
Show issue comments and messages with the newest activity first, move the jump control to the bottom of
the page, and surface plain comment timestamps in the header area.
</p>
</div>
<ToggleSwitch
checked={enableNewestFirstIssueThread}
onCheckedChange={() =>
toggleMutation.mutate({ enableNewestFirstIssueThread: !enableNewestFirstIssueThread })}
disabled={toggleMutation.isPending}
aria-label="Toggle newest-first issue thread experimental setting"
/>
</div>
</section>
<section className="rounded-xl border border-border bg-card p-5">
<div className="flex items-start justify-between gap-4">
<div className="space-y-1.5">
<h2 className="text-sm font-semibold">Auto-Restart Dev Server When Idle</h2>
<p className="max-w-2xl text-sm text-muted-foreground">
In `pnpm dev:once`, wait for all queued and running local agent runs to finish, then restart the server
automatically when backend changes or migrations make the current boot stale.
</p>
</div>
<ToggleSwitch
checked={autoRestartDevServerWhenIdle}
onCheckedChange={() => toggleMutation.mutate({ autoRestartDevServerWhenIdle: !autoRestartDevServerWhenIdle })}
disabled={toggleMutation.isPending}
aria-label="Toggle guarded dev-server auto-restart"
/>
</div>
</section>
<section className="rounded-xl border border-border bg-card p-5">
<div className="flex flex-col gap-5">
<div className="flex items-start justify-between gap-4">
<div className="space-y-1.5">
<h2 className="text-sm font-semibold">Auto-Create Issue Recovery Tasks</h2>
<p className="max-w-2xl text-sm text-muted-foreground">
Let the heartbeat scheduler create recovery issues for issue dependency chains found inside the
configured lookback window.
</p>
</div>
<ToggleSwitch
checked={enableIssueGraphLivenessAutoRecovery}
onCheckedChange={() => {
if (enableIssueGraphLivenessAutoRecovery) {
toggleMutation.mutate({ enableIssueGraphLivenessAutoRecovery: false });
return;
}
previewForEnable();
}}
disabled={recoveryActionPending}
aria-label="Toggle issue graph liveness auto-recovery"
/>
</div>
<div className="grid gap-3 sm:grid-cols-[minmax(10rem,14rem)_1fr] sm:items-end">
<label className="space-y-1.5">
<span className="flex items-center gap-1.5 text-xs font-medium text-muted-foreground">
<Clock className="h-3.5 w-3.5" />
Lookback hours
</span>
<Input
type="number"
min={1}
max={720}
step={1}
value={lookbackHoursDraft}
onChange={(event) => setLookbackHoursDraft(event.target.value)}
aria-invalid={!lookbackHoursIsValid}
/>
</label>
<div className="flex flex-wrap gap-2">
<Button
variant="outline"
onClick={() => {
if (!lookbackHoursIsValid) {
setActionError("Lookback hours must be a whole number from 1 to 720.");
return;
}
toggleMutation.mutate({
issueGraphLivenessAutoRecoveryLookbackHours: parsedLookbackHours,
});
}}
disabled={recoveryActionPending || parsedLookbackHours === lookbackHours}
>
Save hours
</Button>
<Button
variant="outline"
onClick={previewForEnable}
disabled={recoveryActionPending}
>
<Search className="h-4 w-4" />
Preview
</Button>
<Button
onClick={() => {
if (!lookbackHoursIsValid) {
setActionError("Lookback hours must be a whole number from 1 to 720.");
return;
}
runRecoveryMutation.mutate(parsedLookbackHours);
}}
disabled={recoveryActionPending || !enableIssueGraphLivenessAutoRecovery}
>
<Play className="h-4 w-4" />
Run now
</Button>
</div>
</div>
<p className="text-xs text-muted-foreground">
Current window: last {lookbackHours} {lookbackHours === 1 ? "hour" : "hours"}.
</p>
</div>
</section>
<RecoveryPreviewDialog
open={previewDialogOpen}
onOpenChange={setPreviewDialogOpen}
preview={pendingPreview}
onEnableOnly={enableOnly}
onEnableAndRun={enableAndRun}
isPending={recoveryActionPending}
/>
</div>
);
}