paperclip/ui/src/components/RoutineHistoryTab.test.tsx

379 lines
12 KiB
TypeScript
Raw Normal View History

Add routine revision history and restore flow (#5285) ## Thinking Path > - Paperclip is the control plane for autonomous AI companies. > - Routines are the scheduled/recurring work surface that keeps a company operating without manual kicks. > - Operators need routine edits to be auditable and recoverable, especially when routines control assignments, prompts, triggers, and webhook secrets. > - Documents already have revision-style safety, but routines did not have equivalent history or restore semantics. > - This pull request adds append-only routine revisions across the database, shared contracts, server routes, and board UI. > - The benefit is safer routine iteration: users can inspect history, compare changes, restore older definitions, and avoid overwriting newer edits. ## What Changed - Added `routine_revisions` storage, latest revision pointers on routines, shared types, validators, and API docs for routine revision history. - Added server service/route support for listing routine revisions, conflict-aware routine saves, and append-only restore operations. - Added a History tab on routine detail with revision preview, structured change summaries, description line diffs, dirty-edit blocking, restore confirmation, and restored webhook secret surfacing. - Extracted the line diff helper from `DocumentDiffModal` into `ui/src/lib/line-diff.ts` for reuse. - Rebased the branch onto current `public-gh/master` and renumbered the routine revision migration to `0077_unusual_karnak` after upstream `0076_useful_elektra`. - Made the `0077` routine revision migration idempotent so installs that already applied the branch-local `0076_unusual_karnak` can safely advance. - Updated the plugin SDK test harness routine fixture with the new revision fields required by the shared `Routine` contract. ## Verification - `pnpm --filter @paperclipai/db run check:migrations` passed. - `pnpm exec vitest run --project @paperclipai/shared packages/shared/src/validators/routine.test.ts` passed. - `pnpm exec vitest run --project @paperclipai/ui ui/src/lib/line-diff.test.ts ui/src/components/RoutineHistoryTab.test.tsx ui/src/lib/workspace-routines.test.ts ui/src/pages/Routines.test.tsx` passed. - `pnpm exec vitest run --project @paperclipai/server server/src/__tests__/routines-service.test.ts --pool=forks --poolOptions.forks.isolate=true` passed. - `pnpm exec vitest run --project @paperclipai/server server/src/__tests__/routines-routes.test.ts --pool=forks --poolOptions.forks.isolate=true` passed. - `pnpm --filter @paperclipai/plugin-sdk typecheck` passed after updating the SDK test harness fixture. - `pnpm --filter @paperclipai/plugin-sdk build` passed; this refreshed local generated SDK output needed by plugin example typechecks. - `pnpm -r typecheck` passed. ## Risks - Medium migration risk: this adds routine revision storage and backfills existing routines. The migration is ordered after upstream `0076` and uses `IF NOT EXISTS` / duplicate-object guards to tolerate earlier branch-local migration application. - Restore behavior intentionally appends a new revision instead of mutating history; callers expecting an in-place rollback need to follow the new latest revision pointer. - Restoring webhook triggers recreates webhook secret material, so users must copy newly surfaced secrets after restore. - Conflict-aware saves now reject stale routine edits when the client sends an older `baseRevisionId`. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex, GPT-5-based coding agent, with shell/tool use in a local git worktree. Exact context-window size is not exposed in this runtime. ## 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 - [x] If this change affects the UI, I have included before/after screenshots - [x] 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 Screenshots: not attached in this draft PR; the new UI flow is covered by component tests listed above. --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-05-05 11:54:52 -05:00
// @vitest-environment jsdom
import { act } from "react";
import type { ComponentProps } from "react";
import { createRoot } from "react-dom/client";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import type {
Routine,
RoutineRevision,
RoutineRevisionSnapshotV1,
} from "@paperclipai/shared";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { RoutineHistoryTab } from "./RoutineHistoryTab";
const mockRoutinesApi = vi.hoisted(() => ({
listRevisions: vi.fn(),
restoreRevision: vi.fn(),
}));
vi.mock("../api/routines", async () => {
const actual = await vi.importActual<Record<string, unknown>>("../api/routines");
return {
...actual,
routinesApi: {
...((actual as { routinesApi?: Record<string, unknown> }).routinesApi ?? {}),
...mockRoutinesApi,
},
};
});
vi.mock("./MarkdownBody", () => ({
MarkdownBody: ({ children }: { children: string }) => <div>{children}</div>,
}));
vi.mock("@/components/ui/dialog", () => ({
Dialog: ({ open, children }: { open: boolean; children: React.ReactNode }) =>
open ? <div data-testid="dialog">{children}</div> : null,
DialogContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
DialogHeader: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
DialogTitle: ({ children }: { children: React.ReactNode }) => <h2>{children}</h2>,
DialogDescription: ({ children }: { children: React.ReactNode }) => <p>{children}</p>,
DialogFooter: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}));
vi.mock("@/components/ui/button", () => ({
Button: ({ children, onClick, type = "button", disabled, ...props }: ComponentProps<"button">) => (
<button type={type} onClick={onClick} disabled={disabled} {...props}>
{children}
</button>
),
}));
vi.mock("@/components/ui/input", () => ({
Input: (props: ComponentProps<"input">) => <input {...props} />,
}));
vi.mock("@/components/ui/label", () => ({
Label: ({ children, htmlFor }: { children: React.ReactNode; htmlFor?: string }) => (
<label htmlFor={htmlFor}>{children}</label>
),
}));
vi.mock("@/components/ui/skeleton", () => ({
Skeleton: (props: ComponentProps<"div">) => <div data-testid="skeleton" {...props} />,
}));
const toastSpy = vi.fn();
vi.mock("../context/ToastContext", () => ({
useToastActions: () => ({ pushToast: toastSpy }),
}));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
async function flush() {
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 0));
});
}
function snapshotV1(overrides?: Partial<RoutineRevisionSnapshotV1["routine"]>): RoutineRevisionSnapshotV1 {
return {
version: 1,
routine: {
id: "routine-1",
companyId: "company-1",
projectId: null,
goalId: null,
parentIssueId: null,
title: "Daily standup digest",
description: "Summarize standup notes",
assigneeAgentId: null,
priority: "medium",
status: "active",
concurrencyPolicy: "coalesce_if_active",
catchUpPolicy: "skip_missed",
variables: [],
...overrides,
},
triggers: [],
};
}
function createRevision(overrides: Partial<RoutineRevision> = {}): RoutineRevision {
return {
id: overrides.id ?? "revision-1",
companyId: "company-1",
routineId: "routine-1",
revisionNumber: overrides.revisionNumber ?? 1,
title: "Daily standup digest",
description: "Summarize standup notes",
snapshot: overrides.snapshot ?? snapshotV1(),
changeSummary: null,
restoredFromRevisionId: null,
createdByAgentId: null,
createdByUserId: "user-1",
createdByRunId: null,
createdAt: new Date("2026-05-01T12:00:00.000Z"),
...overrides,
};
}
function createRoutine(overrides: Partial<Routine> = {}): Routine {
return {
id: "routine-1",
companyId: "company-1",
projectId: null,
goalId: null,
parentIssueId: null,
title: "Daily standup digest",
description: "Summarize standup notes",
assigneeAgentId: null,
priority: "medium",
status: "active",
concurrencyPolicy: "coalesce_if_active",
catchUpPolicy: "skip_missed",
variables: [],
latestRevisionId: "revision-2",
latestRevisionNumber: 2,
createdByAgentId: null,
createdByUserId: "user-1",
updatedByAgentId: null,
updatedByUserId: "user-1",
lastTriggeredAt: null,
lastEnqueuedAt: null,
createdAt: new Date("2026-05-01T11:00:00.000Z"),
updatedAt: new Date("2026-05-04T12:00:00.000Z"),
...overrides,
};
}
function makeQueryClient() {
return new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
});
}
describe("RoutineHistoryTab", () => {
let container: HTMLDivElement;
beforeEach(() => {
container = document.createElement("div");
document.body.appendChild(container);
vi.clearAllMocks();
toastSpy.mockReset();
});
afterEach(() => {
container.remove();
});
async function render(props: Partial<Parameters<typeof RoutineHistoryTab>[0]> = {}) {
const root = createRoot(container);
const queryClient = makeQueryClient();
const routine = props.routine ?? createRoutine();
await act(async () => {
root.render(
<QueryClientProvider client={queryClient}>
<RoutineHistoryTab
routine={routine}
isEditDirty={false}
dirtyFields={[]}
onDiscardEdits={() => {}}
onSaveEdits={() => {}}
agents={new Map()}
projects={new Map()}
onRestoreSecretMaterials={() => {}}
{...props}
/>
</QueryClientProvider>,
);
});
await flush();
return root;
}
it("shows the empty state when only the bootstrap revision exists", async () => {
mockRoutinesApi.listRevisions.mockResolvedValue([
createRevision({ id: "revision-1", revisionNumber: 1 }),
]);
await render({
routine: createRoutine({ latestRevisionId: "revision-1", latestRevisionNumber: 1 }),
});
expect(container.textContent).toContain("No edits yet");
expect(container.textContent).toContain("Revision 1 is the only history");
});
it("renders the revision list with current and historical pills", async () => {
const current = createRevision({
id: "revision-2",
revisionNumber: 2,
changeSummary: "Updated routine",
});
const old = createRevision({
id: "revision-1",
revisionNumber: 1,
changeSummary: "Created routine",
});
mockRoutinesApi.listRevisions.mockResolvedValue([current, old]);
await render();
expect(container.textContent).toContain("rev 2");
expect(container.textContent).toContain("rev 1");
expect(container.textContent).toContain("Current");
});
it("shows the historical-preview banner with append-only copy when previewing an old revision", async () => {
const current = createRevision({
id: "revision-2",
revisionNumber: 2,
changeSummary: "Updated routine",
});
const old = createRevision({
id: "revision-1",
revisionNumber: 1,
snapshot: snapshotV1({ status: "paused" }),
changeSummary: "Created routine",
});
mockRoutinesApi.listRevisions.mockResolvedValue([current, old]);
await render();
const oldRow = container.querySelector(
"[data-testid='revision-row-1']",
) as HTMLButtonElement | null;
expect(oldRow).not.toBeNull();
await act(async () => {
oldRow?.click();
});
await flush();
expect(container.textContent).toContain("Viewing revision 1 (read-only)");
expect(container.textContent).toContain(
"Restoring this revision creates a new revision 3 with the same content. History stays append-only.",
);
expect(container.textContent).toContain("Status");
expect(container.textContent).toContain("paused");
expect(container.textContent).toContain("Restore as new revision");
});
it("blocks historical preview and surfaces the conflict banner when local edits are dirty", async () => {
const current = createRevision({ id: "revision-2", revisionNumber: 2 });
const old = createRevision({ id: "revision-1", revisionNumber: 1 });
mockRoutinesApi.listRevisions.mockResolvedValue([current, old]);
await render({
isEditDirty: true,
dirtyFields: [{ key: "description", label: "the description" }],
});
expect(container.textContent).toContain("Unsaved routine edits");
const oldRow = container.querySelector(
"[data-testid='revision-row-1']",
) as HTMLButtonElement | null;
expect(oldRow?.disabled).toBe(true);
});
it("calls restoreRevision and surfaces a success toast after confirming restore", async () => {
const current = createRevision({ id: "revision-2", revisionNumber: 2 });
const old = createRevision({ id: "revision-1", revisionNumber: 1 });
mockRoutinesApi.listRevisions.mockResolvedValue([current, old]);
mockRoutinesApi.restoreRevision.mockResolvedValue({
routine: createRoutine({ latestRevisionId: "revision-3", latestRevisionNumber: 3 }),
revision: createRevision({
id: "revision-3",
revisionNumber: 3,
restoredFromRevisionId: "revision-1",
}),
restoredFromRevisionId: "revision-1",
restoredFromRevisionNumber: 1,
secretMaterials: [],
});
await render();
const oldRow = container.querySelector(
"[data-testid='revision-row-1']",
) as HTMLButtonElement | null;
await act(async () => {
oldRow?.click();
});
await flush();
const restoreButtons = Array.from(container.querySelectorAll("button")).filter(
(button) => button.textContent === "Restore as new revision",
);
expect(restoreButtons.length).toBeGreaterThan(0);
await act(async () => {
restoreButtons[0].click();
});
await flush();
expect(container.querySelector("[data-testid='dialog']")).not.toBeNull();
fix(ui): improve routine properties panel and history UX (#5703) ## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies. > - Routines are the recurring-work surface where operators configure schedules, executions, activity, and revision history. > - The routine detail view uses a contextual right properties panel for triggers, runs, activity, and history. > - That panel was too cramped for routine workflows: the routine header could collapse at constrained widths, and revision previews/comparisons were trying to live inside the same narrow panel. > - This pull request makes the routine properties panel wider and responsive without changing the default panel behavior for other pages. > - It also moves routine revision viewing and comparison into focused dialogs so history stays usable instead of rendering dense revision content inside the right panel. > - The benefit is a cleaner routine workflow: triggers remain scannable, the main routine stays readable, and revisions can be inspected, compared, and restored without fighting the sidebar width. ## What Changed - Added optional per-panel layout options for storage key, default width, min/max width, and compact viewport behavior. - Set the routine properties panel to use its own 400px default width and persistence key, while compacting to 320px on narrower viewports. - Made the shared resizable sidebar support right-side panes, custom width bounds, compact max width, and keyboard resizing. - Fixed the routine detail header so title text and action controls remain readable beside the properties panel at constrained widths. - Reworked routine history so selecting a revision opens a read-only snapshot dialog instead of trying to render the whole revision inside the right panel. - Added a side-by-side current-vs-selected revision comparison dialog with clearer diff markers for structured fields, triggers, and variables. - Added focused tests for the resizable pane and routine history behavior. ## Verification - `pnpm vitest run ui/src/components/RoutineHistoryTab.test.tsx ui/src/components/ResizableSidebarPane.test.tsx` - `pnpm --filter @paperclipai/ui typecheck` - `pnpm -r typecheck` - `git diff --check` - Browser E2E in TestCo at `http://localhost:3100/TES/dashboard`: - created and edited a routine - added, edited, toggled, and deleted schedule triggers - paused automation - ran the routine and stopped the live run - verified runs, activity, history, snapshot dialog, compare mode, restore confirmation, routine list, recent runs, row actions, panel close/reopen, and constrained-width layout ### Screenshots #### Trigger Panel Width | Before | After | | --- | --- | | <img width="1741" height="1289" alt="triggers-before" src="https://github.com/user-attachments/assets/2a391769-c355-4219-8da3-d1ea18698430" /> | <img width="1742" height="1288" alt="triggers-after" src="https://github.com/user-attachments/assets/9e818978-283c-49a3-9401-879be550c67b" /> | #### History Panel Before, selecting a revision attempted to show dense revision content inside the already narrow right panel. After, history remains a compact list and revision details open separately. | Before | After | | --- | --- | | <img width="1739" height="1289" alt="history-before" src="https://github.com/user-attachments/assets/eaea4f3d-bb65-4af6-b67f-3ba3026fe0c9" /> | <img width="1741" height="1290" alt="history-after" src="https://github.com/user-attachments/assets/4c139238-8494-4438-89e1-4277d05bc3aa" /> | #### Revision Snapshot The selected revision now opens in a dedicated read-only dialog instead of crowding the properties panel. <img width="1740" height="1289" alt="revision-single" src="https://github.com/user-attachments/assets/f930f50f-7016-434b-bd81-d8d97304c528" /> #### Revision Compare Historical revisions can be compared side-by-side with the current revision, including changed structured fields and trigger differences. <img width="1740" height="1287" alt="revision-compare" src="https://github.com/user-attachments/assets/5640201e-de4f-446b-8941-1b0f140c56d7" /> ## Risks - Low to moderate UI risk: the shared resizable pane API gained optional layout parameters, but existing callers keep the previous defaults. - Routine history now uses dialogs for revision viewing and comparison, so reviewers should confirm the new workflow feels right for restore and compare. - Routine panel width now persists under a routine-specific key, so previous global properties panel width preferences do not carry into routines. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex, GPT-5 coding agent in Codex Desktop, tool-enabled with local shell, git, and in-app browser automation. Context window size was not exposed in this session. ## 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 - [x] If this change affects the UI, I have included before/after screenshots - [x] 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-11 16:37:30 +02:00
expect(container.textContent).not.toContain("Viewing revision 1 (read-only)");
expect(container.textContent).toContain("Restore revision 1?");
Add routine revision history and restore flow (#5285) ## Thinking Path > - Paperclip is the control plane for autonomous AI companies. > - Routines are the scheduled/recurring work surface that keeps a company operating without manual kicks. > - Operators need routine edits to be auditable and recoverable, especially when routines control assignments, prompts, triggers, and webhook secrets. > - Documents already have revision-style safety, but routines did not have equivalent history or restore semantics. > - This pull request adds append-only routine revisions across the database, shared contracts, server routes, and board UI. > - The benefit is safer routine iteration: users can inspect history, compare changes, restore older definitions, and avoid overwriting newer edits. ## What Changed - Added `routine_revisions` storage, latest revision pointers on routines, shared types, validators, and API docs for routine revision history. - Added server service/route support for listing routine revisions, conflict-aware routine saves, and append-only restore operations. - Added a History tab on routine detail with revision preview, structured change summaries, description line diffs, dirty-edit blocking, restore confirmation, and restored webhook secret surfacing. - Extracted the line diff helper from `DocumentDiffModal` into `ui/src/lib/line-diff.ts` for reuse. - Rebased the branch onto current `public-gh/master` and renumbered the routine revision migration to `0077_unusual_karnak` after upstream `0076_useful_elektra`. - Made the `0077` routine revision migration idempotent so installs that already applied the branch-local `0076_unusual_karnak` can safely advance. - Updated the plugin SDK test harness routine fixture with the new revision fields required by the shared `Routine` contract. ## Verification - `pnpm --filter @paperclipai/db run check:migrations` passed. - `pnpm exec vitest run --project @paperclipai/shared packages/shared/src/validators/routine.test.ts` passed. - `pnpm exec vitest run --project @paperclipai/ui ui/src/lib/line-diff.test.ts ui/src/components/RoutineHistoryTab.test.tsx ui/src/lib/workspace-routines.test.ts ui/src/pages/Routines.test.tsx` passed. - `pnpm exec vitest run --project @paperclipai/server server/src/__tests__/routines-service.test.ts --pool=forks --poolOptions.forks.isolate=true` passed. - `pnpm exec vitest run --project @paperclipai/server server/src/__tests__/routines-routes.test.ts --pool=forks --poolOptions.forks.isolate=true` passed. - `pnpm --filter @paperclipai/plugin-sdk typecheck` passed after updating the SDK test harness fixture. - `pnpm --filter @paperclipai/plugin-sdk build` passed; this refreshed local generated SDK output needed by plugin example typechecks. - `pnpm -r typecheck` passed. ## Risks - Medium migration risk: this adds routine revision storage and backfills existing routines. The migration is ordered after upstream `0076` and uses `IF NOT EXISTS` / duplicate-object guards to tolerate earlier branch-local migration application. - Restore behavior intentionally appends a new revision instead of mutating history; callers expecting an in-place rollback need to follow the new latest revision pointer. - Restoring webhook triggers recreates webhook secret material, so users must copy newly surfaced secrets after restore. - Conflict-aware saves now reject stale routine edits when the client sends an older `baseRevisionId`. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex, GPT-5-based coding agent, with shell/tool use in a local git worktree. Exact context-window size is not exposed in this runtime. ## 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 - [x] If this change affects the UI, I have included before/after screenshots - [x] 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 Screenshots: not attached in this draft PR; the new UI flow is covered by component tests listed above. --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-05-05 11:54:52 -05:00
const confirmButtons = Array.from(container.querySelectorAll("button")).filter((b) =>
(b.textContent ?? "").includes("Restore as revision 3"),
);
expect(confirmButtons.length).toBeGreaterThan(0);
await act(async () => {
confirmButtons[0].click();
});
await flush();
expect(mockRoutinesApi.restoreRevision).toHaveBeenCalledWith(
"routine-1",
"revision-1",
{ changeSummary: null },
);
expect(toastSpy).toHaveBeenCalled();
const successCall = toastSpy.mock.calls.find(
(call) => call[0]?.title === "Restored revision 1 as revision 3",
);
expect(successCall).toBeTruthy();
});
it("invokes onRestored with the restore response so the editor can rehydrate (PAP-3588)", async () => {
const current = createRevision({ id: "revision-2", revisionNumber: 2 });
const old = createRevision({
id: "revision-1",
revisionNumber: 1,
snapshot: snapshotV1({ description: "Original description" }),
});
mockRoutinesApi.listRevisions.mockResolvedValue([current, old]);
const restoredRoutine = createRoutine({
description: "Original description",
latestRevisionId: "revision-3",
latestRevisionNumber: 3,
});
mockRoutinesApi.restoreRevision.mockResolvedValue({
routine: restoredRoutine,
revision: createRevision({
id: "revision-3",
revisionNumber: 3,
restoredFromRevisionId: "revision-1",
}),
restoredFromRevisionId: "revision-1",
restoredFromRevisionNumber: 1,
secretMaterials: [],
});
const onRestored = vi.fn();
await render({ onRestored });
const oldRow = container.querySelector(
"[data-testid='revision-row-1']",
) as HTMLButtonElement | null;
await act(async () => {
oldRow?.click();
});
await flush();
const restoreButtons = Array.from(container.querySelectorAll("button")).filter(
(button) => button.textContent === "Restore as new revision",
);
await act(async () => {
restoreButtons[0].click();
});
await flush();
const confirmButtons = Array.from(container.querySelectorAll("button")).filter((b) =>
(b.textContent ?? "").includes("Restore as revision 3"),
);
await act(async () => {
confirmButtons[0].click();
});
await flush();
expect(onRestored).toHaveBeenCalledTimes(1);
const [response] = onRestored.mock.calls[0];
expect(response.routine).toEqual(restoredRoutine);
expect(response.revision.id).toBe("revision-3");
});
});