[codex] Add issue monitor liveness controls (#4988)

## Thinking Path

> - Paperclip is a control plane for autonomous AI companies where work
must stay observable, governable, and recoverable.
> - The task/heartbeat subsystem owns agent execution continuity, issue
state transitions, and visible recovery behavior.
> - Waiting on an external service is not the same as being blocked when
the assignee still owns a future check.
> - The gap was that agents had no first-class one-shot monitor state
for external-service waits, so recovery could look stalled or require ad
hoc comments.
> - This pull request adds bounded issue monitors that can wake the
owner, clear exhausted waits, and produce explicit recovery behavior.
> - It also surfaces monitor status in the board UI and documents when
to use monitors versus `blocked`.
> - The benefit is clearer liveness semantics for asynchronous waits
without weakening single-assignee task ownership.

## What Changed

- Added issue monitor fields, shared types, validators, constants, and
an idempotent `0075` migration for scheduled monitor state.
- Added server-side monitor scheduling, dispatch, recovery bounds,
activity logging, and external-ref redaction.
- Added board/agent route coverage for monitor permissions and child
monitor scheduling.
- Added issue detail/property UI for monitor state, a monitor activity
card, and Storybook stories for review surfaces.
- Documented monitor semantics and recovery policy behavior in
`doc/execution-semantics.md`.
- Addressed Greptile review feedback by preserving monitor state in
skipped-stage builders and making board monitor saves send `scheduledBy:
"board"`.

## Verification

- `pnpm install --frozen-lockfile`
- `pnpm run preflight:workspace-links && pnpm exec vitest run
server/src/__tests__/issue-execution-policy-routes.test.ts
server/src/__tests__/issue-execution-policy.test.ts
server/src/__tests__/issue-monitor-scheduler.test.ts
server/src/__tests__/recovery-classifiers.test.ts
ui/src/components/IssueMonitorActivityCard.test.tsx
ui/src/components/IssueProperties.test.tsx
ui/src/lib/activity-format.test.ts`
- First run passed 5 files and failed to collect 2 server suites because
the worktree was missing the optional `acpx/runtime` dependency.
- After `pnpm install --frozen-lockfile`, reran the 2 failed suites
successfully.
- `pnpm exec vitest run
server/src/__tests__/issue-monitor-scheduler.test.ts
server/src/__tests__/recovery-classifiers.test.ts`
- `pnpm --filter @paperclipai/shared typecheck && pnpm --filter
@paperclipai/db typecheck && pnpm --filter @paperclipai/server typecheck
&& pnpm --filter @paperclipai/ui typecheck`
- `pnpm exec vitest run
server/src/__tests__/issue-execution-policy.test.ts
ui/src/components/IssueProperties.test.tsx`
- `pnpm --filter @paperclipai/server typecheck && pnpm --filter
@paperclipai/ui typecheck`
- `pnpm exec vitest run
ui/src/components/IssueMonitorActivityCard.test.tsx
ui/src/components/IssueProperties.test.tsx`
- `pnpm --filter @paperclipai/ui typecheck`
- Storybook screenshot captured from
`http://127.0.0.1:6006/iframe.html?viewMode=story&id=product-issue-monitor-surfaces--monitor-surfaces`
with Playwright.

## Screenshots

![Issue monitor Storybook
surfaces](https://raw.githubusercontent.com/paperclipai/paperclip/PAP-2945-when-a-task-is-waiting-for-an-_external-service_-what-state-should-it-be-in-and-what-recovery-method-could-it-h/docs/pr-screenshots/pap-2945/monitor-surfaces.png)

## Risks

- Medium: this changes heartbeat recovery behavior for scheduled
external-service waits, so regressions could affect wake timing or
recovery issue creation.
- Migration risk is reduced by using `IF NOT EXISTS` for the new issue
monitor columns and index.
- External monitor references are treated as secret-adjacent and are
intentionally omitted from visible activity/wake payloads.

> 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 with repository tool use and terminal
execution.

## 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 or Storybook review surfaces
- [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

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Dotta 2026-05-03 08:58:53 -05:00 committed by GitHub
parent 76f09c8eb6
commit 57229d0f24
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 19324 additions and 20 deletions

View file

@ -0,0 +1,196 @@
// @vitest-environment jsdom
import { act } from "react";
import { createRoot } from "react-dom/client";
import type { Issue } from "@paperclipai/shared";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { IssueMonitorActivityCard } from "./IssueMonitorActivityCard";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
function createIssue(overrides: Partial<Issue> = {}): Issue {
return {
id: "issue-1",
companyId: "company-1",
projectId: null,
projectWorkspaceId: null,
goalId: null,
parentId: null,
title: "Watch deploy",
description: null,
status: "in_progress",
priority: "medium",
assigneeAgentId: "agent-1",
assigneeUserId: null,
checkoutRunId: null,
executionRunId: null,
executionAgentNameKey: null,
executionLockedAt: null,
createdByAgentId: null,
createdByUserId: "local-board",
issueNumber: 1,
identifier: "PAP-1",
requestDepth: 0,
billingCode: null,
assigneeAdapterOverrides: null,
executionPolicy: {
mode: "normal",
commentRequired: true,
stages: [],
monitor: {
nextCheckAt: "2026-04-11T12:30:00.000Z",
notes: "Check deployment health",
scheduledBy: "board",
},
},
executionState: {
status: "idle",
currentStageId: null,
currentStageIndex: null,
currentStageType: null,
currentParticipant: null,
returnAssignee: null,
reviewRequest: null,
completedStageIds: [],
lastDecisionId: null,
lastDecisionOutcome: null,
monitor: {
status: "scheduled",
nextCheckAt: "2026-04-11T12:30:00.000Z",
lastTriggeredAt: null,
attemptCount: 0,
notes: "Check deployment health",
scheduledBy: "board",
clearedAt: null,
clearReason: null,
},
},
monitorNextCheckAt: new Date("2026-04-11T12:30:00.000Z"),
monitorLastTriggeredAt: null,
monitorAttemptCount: 0,
monitorNotes: "Check deployment health",
monitorScheduledBy: "board",
executionWorkspaceId: null,
executionWorkspacePreference: null,
executionWorkspaceSettings: null,
startedAt: null,
completedAt: null,
cancelledAt: null,
hiddenAt: null,
createdAt: new Date("2026-04-11T10:00:00.000Z"),
updatedAt: new Date("2026-04-11T10:00:00.000Z"),
...overrides,
};
}
describe("IssueMonitorActivityCard", () => {
let container: HTMLDivElement;
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-04-11T12:00:00.000Z"));
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
vi.useRealTimers();
container.remove();
});
it("renders the scheduled monitor details and check-now action", () => {
const onCheckNow = vi.fn();
const root = createRoot(container);
act(() => {
root.render(<IssueMonitorActivityCard issue={createIssue()} onCheckNow={onCheckNow} />);
});
expect(container.textContent).toContain("Monitor scheduled");
expect(container.textContent).toContain("Next check");
expect(container.textContent).toContain("in 30m");
expect(container.textContent).toContain("Check deployment health");
const button = Array.from(container.querySelectorAll("button")).find((candidate) =>
candidate.textContent?.includes("Check now"),
);
expect(button).toBeTruthy();
act(() => {
button?.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
expect(onCheckNow).toHaveBeenCalledTimes(1);
act(() => root.unmount());
});
it("does not render external references from monitor metadata", () => {
const root = createRoot(container);
act(() => {
root.render(
<IssueMonitorActivityCard
issue={createIssue({
executionPolicy: {
mode: "normal",
commentRequired: true,
stages: [],
monitor: {
nextCheckAt: "2026-04-11T12:30:00.000Z",
notes: "Check deployment health",
scheduledBy: "board",
serviceName: "Deploy provider",
externalRef: "https://provider.example/deploy/123?token=secret",
},
},
})}
/>,
);
});
expect(container.textContent).toContain("Deploy provider");
expect(container.textContent).not.toContain("provider.example");
expect(container.textContent).not.toContain("token=secret");
act(() => root.unmount());
});
it("renders nothing when the issue has no scheduled monitor", () => {
const root = createRoot(container);
act(() => {
root.render(
<IssueMonitorActivityCard
issue={createIssue({
executionPolicy: {
mode: "normal",
commentRequired: true,
stages: [],
},
executionState: {
status: "idle",
currentStageId: null,
currentStageIndex: null,
currentStageType: null,
currentParticipant: null,
returnAssignee: null,
reviewRequest: null,
completedStageIds: [],
lastDecisionId: null,
lastDecisionOutcome: null,
monitor: null,
},
monitorNextCheckAt: null,
monitorNotes: null,
})}
/>,
);
});
expect(container.textContent).toBe("");
act(() => root.unmount());
});
});

View file

@ -0,0 +1,71 @@
import type { Issue } from "@paperclipai/shared";
import { Button } from "@/components/ui/button";
import { formatMonitorOffset } from "@/lib/issue-monitor";
import { formatDateTime } from "@/lib/utils";
function resolveScheduledMonitor(issue: Issue) {
const nextCheckAt =
issue.monitorNextCheckAt?.toISOString() ??
issue.executionPolicy?.monitor?.nextCheckAt ??
issue.executionState?.monitor?.nextCheckAt ??
null;
if (!nextCheckAt) return null;
return {
nextCheckAt,
notes: issue.executionPolicy?.monitor?.notes ?? issue.monitorNotes ?? issue.executionState?.monitor?.notes ?? null,
attemptCount: issue.monitorAttemptCount ?? issue.executionState?.monitor?.attemptCount ?? 0,
serviceName: issue.executionPolicy?.monitor?.serviceName ?? issue.executionState?.monitor?.serviceName ?? null,
};
}
interface IssueMonitorActivityCardProps {
issue: Issue;
onCheckNow?: (() => void) | null;
checkingNow?: boolean;
}
export function IssueMonitorActivityCard({
issue,
onCheckNow = null,
checkingNow = false,
}: IssueMonitorActivityCardProps) {
const monitor = resolveScheduledMonitor(issue);
if (!monitor) return null;
return (
<div className="mb-3 rounded-lg border border-border bg-muted/30 px-3 py-2">
<div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
<div className="min-w-0">
<div className="text-sm font-medium text-foreground">Monitor scheduled</div>
<div className="text-xs text-muted-foreground">
Next check {formatDateTime(monitor.nextCheckAt)} ({formatMonitorOffset(monitor.nextCheckAt)})
</div>
{monitor.notes ? (
<div className="mt-1 text-xs text-muted-foreground">{monitor.notes}</div>
) : null}
{monitor.serviceName ? (
<div className="mt-1 text-xs text-muted-foreground">
{monitor.serviceName}
</div>
) : null}
{monitor.attemptCount > 0 ? (
<div className="mt-1 text-xs text-muted-foreground">Attempt {monitor.attemptCount}</div>
) : null}
</div>
{onCheckNow ? (
<Button
type="button"
variant="outline"
size="sm"
className="shrink-0 shadow-none"
onClick={onCheckNow}
disabled={checkingNow}
>
{checkingNow ? "Checking..." : "Check now"}
</Button>
) : null}
</div>
</div>
);
}

View file

@ -969,4 +969,84 @@ describe("IssueProperties", () => {
act(() => root.unmount());
});
it("renders monitor controls and clears an existing monitor", async () => {
const onUpdate = vi.fn();
const root = renderProperties(container, {
issue: createIssue({
status: "in_progress",
assigneeAgentId: "agent-1",
executionPolicy: createExecutionPolicy({
monitor: {
nextCheckAt: "2026-04-11T12:30:00.000Z",
notes: "Check deployment",
scheduledBy: "board",
},
}),
executionState: createExecutionState({
status: "idle",
currentStageId: null,
currentStageIndex: null,
currentStageType: null,
currentParticipant: null,
returnAssignee: null,
lastDecisionOutcome: null,
monitor: {
status: "scheduled",
nextCheckAt: "2026-04-11T12:30:00.000Z",
lastTriggeredAt: null,
attemptCount: 0,
notes: "Check deployment",
scheduledBy: "board",
clearedAt: null,
clearReason: null,
},
}),
}),
childIssues: [],
onUpdate,
inline: true,
});
await flush();
expect(container.textContent).toContain("Monitor");
expect(container.textContent).toContain("Next check");
expect(container.querySelector('input[type="datetime-local"]')).toBeNull();
expect(container.querySelector('input[placeholder="What should the agent re-check?"]')).toBeNull();
const monitorTrigger = Array.from(container.querySelectorAll("button"))
.find((button) => button.textContent?.includes("Next check"));
expect(monitorTrigger).not.toBeUndefined();
await act(async () => {
monitorTrigger!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
await flush();
const inputs = Array.from(container.querySelectorAll("input"));
const datetimeInput = inputs.find((input) => input.getAttribute("type") === "datetime-local");
const textInput = inputs.find((input) => input.getAttribute("placeholder") === "What should the agent re-check?");
const clearButton = Array.from(container.querySelectorAll("button"))
.find((button) => button.textContent?.includes("Clear"));
expect(datetimeInput).toBeTruthy();
expect(textInput).toBeTruthy();
expect(clearButton).toBeTruthy();
expect(datetimeInput!.value).toBeTruthy();
expect(textInput!.value).toBe("Check deployment");
act(() => {
clearButton!.dispatchEvent(new MouseEvent("click", { bubbles: true }));
});
expect(onUpdate).toHaveBeenCalledWith({
executionPolicy: {
mode: "normal",
commentRequired: true,
stages: [],
},
});
act(() => root.unmount());
});
});

View file

@ -25,6 +25,7 @@ import { getRecentProjectIds, trackRecentProject } from "../lib/recent-projects"
import { orderItemsBySelectedAndRecent } from "../lib/recent-selections";
import { formatAssigneeUserLabel } from "../lib/assignees";
import { buildExecutionPolicy, stageParticipantValues } from "../lib/issue-execution-policy";
import { formatMonitorOffset } from "../lib/issue-monitor";
import { StatusIcon } from "./StatusIcon";
import { PriorityIcon } from "./PriorityIcon";
import { Identity } from "./Identity";
@ -33,7 +34,7 @@ import { formatDate, cn, projectUrl } from "../lib/utils";
import { timeAgo } from "../lib/timeAgo";
import { Separator } from "@/components/ui/separator";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
import { User, Hexagon, ArrowUpRight, Tag, Plus, GitBranch, FolderOpen, Check, ExternalLink } from "lucide-react";
import { User, Hexagon, ArrowUpRight, Tag, Plus, GitBranch, FolderOpen, Check, ExternalLink, Clock } from "lucide-react";
import { AgentIcon } from "./AgentIconPicker";
function TruncatedCopyable({ value, icon: Icon }: { value: string; icon: React.ComponentType<{ className?: string }> }) {
@ -118,6 +119,14 @@ function issuesWorkspaceFilterHref(workspaceId: string) {
return `/issues?${params.toString()}`;
}
function toDateTimeLocalValue(value: string | null | undefined) {
if (!value) return "";
const date = new Date(value);
if (Number.isNaN(date.getTime())) return "";
const offsetMs = date.getTimezoneOffset() * 60_000;
return new Date(date.getTime() - offsetMs).toISOString().slice(0, 16);
}
interface IssuePropertiesProps {
issue: Issue;
childIssues?: Issue[];
@ -219,10 +228,14 @@ export function IssueProperties({
const [reviewerSearch, setReviewerSearch] = useState("");
const [approversOpen, setApproversOpen] = useState(false);
const [approverSearch, setApproverSearch] = useState("");
const [monitorOpen, setMonitorOpen] = useState(false);
const [labelsOpen, setLabelsOpen] = useState(false);
const [labelSearch, setLabelSearch] = useState("");
const [newLabelName, setNewLabelName] = useState("");
const [newLabelColor, setNewLabelColor] = useState("#6366f1");
const [monitorAtInput, setMonitorAtInput] = useState(() => toDateTimeLocalValue(issue.executionPolicy?.monitor?.nextCheckAt));
const [monitorNotesInput, setMonitorNotesInput] = useState(issue.executionPolicy?.monitor?.notes ?? "");
const [monitorServiceInput, setMonitorServiceInput] = useState(issue.executionPolicy?.monitor?.serviceName ?? "");
const { data: session } = useQuery({
queryKey: queryKeys.auth.session,
@ -459,6 +472,145 @@ export function IssueProperties({
}
return `${stageLabel} pending${participantLabel ? ` with ${participantLabel}` : ""}`;
})();
useEffect(() => {
setMonitorAtInput(toDateTimeLocalValue(issue.executionPolicy?.monitor?.nextCheckAt));
setMonitorNotesInput(issue.executionPolicy?.monitor?.notes ?? "");
setMonitorServiceInput(issue.executionPolicy?.monitor?.serviceName ?? "");
}, [
issue.executionPolicy?.monitor?.nextCheckAt,
issue.executionPolicy?.monitor?.notes,
issue.executionPolicy?.monitor?.serviceName,
]);
const updateMonitor = (nextMonitor: Issue["executionPolicy"] extends infer T
? T extends { monitor?: infer M | null } | null | undefined
? M | null
: never
: never) => {
const basePolicy = buildExecutionPolicy({
existingPolicy: issue.executionPolicy ?? null,
reviewerValues,
approverValues,
});
if (!basePolicy && !nextMonitor) {
onUpdate({ executionPolicy: null });
return;
}
onUpdate({
executionPolicy: {
mode: basePolicy?.mode ?? issue.executionPolicy?.mode ?? "normal",
commentRequired: true,
stages: basePolicy?.stages ?? [],
...(nextMonitor ? { monitor: nextMonitor } : {}),
},
});
};
const saveMonitor = () => {
if (!monitorAtInput) return;
const nextCheckAt = new Date(monitorAtInput);
if (Number.isNaN(nextCheckAt.getTime())) return;
const serviceName = monitorServiceInput.trim() || null;
updateMonitor({
nextCheckAt: nextCheckAt.toISOString(),
notes: monitorNotesInput.trim() || null,
scheduledBy: "board",
kind: serviceName ? "external_service" : null,
serviceName,
externalRef: null,
});
setMonitorOpen(false);
};
const clearMonitor = () => {
updateMonitor(null);
setMonitorOpen(false);
};
const currentMonitorLabel = (() => {
if (issue.executionPolicy?.monitor?.nextCheckAt) {
return `Next check ${formatDate(new Date(issue.executionPolicy.monitor.nextCheckAt))}`;
}
if (issue.executionState?.monitor?.status === "cleared") {
return "Cleared";
}
if (issue.monitorLastTriggeredAt) {
return `Last triggered ${timeAgo(issue.monitorLastTriggeredAt)}`;
}
return "Not scheduled";
})();
const monitorNextCheckAt = issue.executionPolicy?.monitor?.nextCheckAt ?? null;
const monitorTrigger = (
<span className="inline-flex min-w-0 flex-wrap items-center gap-x-1.5 gap-y-0.5">
{monitorNextCheckAt ? (
<Clock className="mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground" aria-hidden="true" />
) : null}
<span
className={cn(
"min-w-0 text-sm break-words",
monitorNextCheckAt ? "text-foreground" : "text-muted-foreground",
)}
title={monitorNextCheckAt ? currentMonitorLabel : undefined}
>
{monitorNextCheckAt ? `Next check ${formatMonitorOffset(monitorNextCheckAt)}` : currentMonitorLabel}
</span>
{monitorNextCheckAt ? (
<span className="text-xs text-muted-foreground" title={currentMonitorLabel}>
{formatDate(new Date(monitorNextCheckAt))}
</span>
) : null}
</span>
);
const monitorAttemptBadge = issue.monitorAttemptCount && issue.monitorAttemptCount > 0 ? (
<span className="text-xs text-muted-foreground">
Attempt {issue.monitorAttemptCount}
</span>
) : null;
const monitorContent = (
<div className="flex w-full flex-col gap-2">
<div className="flex flex-col gap-2 md:flex-row">
<input
type="datetime-local"
className="rounded-md border border-border bg-transparent px-2 py-1 text-xs"
value={monitorAtInput}
onChange={(e) => setMonitorAtInput(e.target.value)}
/>
<input
type="text"
className="min-w-0 flex-1 rounded-md border border-border bg-transparent px-2 py-1 text-xs"
placeholder="What should the agent re-check?"
value={monitorNotesInput}
onChange={(e) => setMonitorNotesInput(e.target.value)}
/>
</div>
<div className="flex flex-col gap-2 md:flex-row">
<input
type="text"
className="min-w-0 flex-1 rounded-md border border-border bg-transparent px-2 py-1 text-xs"
placeholder="External service"
value={monitorServiceInput}
onChange={(e) => setMonitorServiceInput(e.target.value)}
/>
<div className="flex items-center gap-2">
<button
type="button"
className="inline-flex items-center rounded-full border border-border px-2 py-0.5 text-xs text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground disabled:opacity-50"
disabled={!monitorAtInput}
onClick={saveMonitor}
>
Schedule
</button>
{issue.executionPolicy?.monitor ? (
<button
type="button"
className="inline-flex items-center rounded-full border border-border px-2 py-0.5 text-xs text-muted-foreground transition-colors hover:bg-accent/50 hover:text-foreground"
onClick={clearMonitor}
>
Clear
</button>
) : null}
</div>
</div>
</div>
);
const selectedIssueLabels = useMemo(() => {
const selectedIds = issue.labelIds ?? [];
if (selectedIds.length === 0) return issue.labels ?? [];
@ -1248,6 +1400,19 @@ export function IssueProperties({
</PropertyRow>
)}
<PropertyPicker
inline={inline}
label="Monitor"
open={monitorOpen}
onOpenChange={setMonitorOpen}
triggerContent={monitorTrigger}
triggerClassName="min-w-0 max-w-full"
popoverClassName={cn("max-w-full", inline ? "w-full" : "w-80 sm:w-[32rem]")}
extra={monitorAttemptBadge}
>
{monitorContent}
</PropertyPicker>
{issue.requestDepth > 0 && (
<PropertyRow label="Depth">
<span className="text-sm font-mono">{issue.requestDepth}</span>