2026-04-04 13:38:34 -05:00
|
|
|
// @vitest-environment jsdom
|
|
|
|
|
|
|
|
|
|
import { act } from "react";
|
|
|
|
|
import { createRoot } from "react-dom/client";
|
|
|
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
|
|
import type { Issue, RoutineListItem } from "@paperclipai/shared";
|
|
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
|
import { Routines, buildRoutineGroups } from "./Routines";
|
|
|
|
|
|
|
|
|
|
let currentSearch = "";
|
|
|
|
|
|
|
|
|
|
const navigateMock = vi.fn();
|
|
|
|
|
const routinesListMock = vi.fn<(companyId: string) => Promise<RoutineListItem[]>>();
|
|
|
|
|
const issuesListMock = vi.fn<(companyId: string, filters?: Record<string, unknown>) => Promise<Issue[]>>();
|
|
|
|
|
const issuesListRenderMock = vi.fn(({ issues }: { issues: Issue[] }) => (
|
|
|
|
|
<div data-testid="issues-list">{issues.map((issue) => issue.title).join(", ")}</div>
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/lib/router", () => ({
|
|
|
|
|
useNavigate: () => navigateMock,
|
|
|
|
|
useLocation: () => ({ pathname: "/routines", search: currentSearch ? `?${currentSearch}` : "", hash: "" }),
|
|
|
|
|
useSearchParams: () => [new URLSearchParams(currentSearch), vi.fn()],
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../context/CompanyContext", () => ({
|
|
|
|
|
useCompany: () => ({ selectedCompanyId: "company-1" }),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../context/BreadcrumbContext", () => ({
|
|
|
|
|
useBreadcrumbs: () => ({ setBreadcrumbs: vi.fn() }),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../context/ToastContext", () => ({
|
[codex] Harden execution reliability and heartbeat tooling (#3679)
## Thinking Path
> - Paperclip orchestrates AI agents for zero-human companies
> - Reliable execution depends on heartbeat routing, issue lifecycle
semantics, telemetry, and a fast enough local verification loop to keep
regressions visible
> - The remaining commits on this branch were mostly server/runtime
correctness fixes plus test and documentation follow-ups in that area
> - Those changes are logically separate from the UI-focused
issue-detail and workspace/navigation branches even when they touch
overlapping issue APIs
> - This pull request groups the execution reliability, heartbeat,
telemetry, and tooling changes into one standalone branch
> - The benefit is a focused review of the control-plane correctness
work, including the follow-up fix that restored the implicit
comment-reopen helpers after branch splitting
## What Changed
- Hardened issue/heartbeat execution behavior, including self-review
stage skipping, deferred mention wakes during active execution, stranded
execution recovery, active-run scoping, assignee resolution, and
blocked-to-todo wake resumption
- Reduced noisy polling/logging overhead by trimming issue run payloads,
compacting persisted run logs, silencing high-volume request logs, and
capping heartbeat-run queries in dashboard/inbox surfaces
- Expanded telemetry and status semantics with adapter/model fields on
task completion plus clearer status guidance in docs/onboarding material
- Updated test infrastructure and verification defaults with faster
route-test module isolation, cheaper default `pnpm test`, e2e isolation
from local state, and repo verification follow-ups
- Included docs/release housekeeping from the branch and added a small
follow-up commit restoring the implicit comment-reopen helpers that were
dropped during branch reconstruction
## Verification
- `pnpm vitest run
server/src/__tests__/issue-comment-reopen-routes.test.ts
server/src/__tests__/issue-telemetry-routes.test.ts`
- `pnpm vitest run server/src/__tests__/http-log-policy.test.ts
server/src/__tests__/heartbeat-run-log.test.ts
server/src/__tests__/health.test.ts`
- `server/src/__tests__/activity-service.test.ts`,
`server/src/__tests__/heartbeat-comment-wake-batching.test.ts`, and
`server/src/__tests__/heartbeat-process-recovery.test.ts` were attempted
on this host but the embedded Postgres harness reported
init-script/data-dir problems and skipped or failed to start, so they
are noted as environment-limited
## Risks
- Medium: this branch changes core issue/heartbeat routing and
reopen/wakeup behavior, so regressions would affect agent execution flow
rather than isolated UI polish
- Because it also updates verification infrastructure, reviewers should
pay attention to whether the new tests are asserting the right failure
modes and not just reshaping harness behavior
## Model Used
- OpenAI Codex coding agent (GPT-5-class runtime in Codex CLI; exact
deployed model ID is not exposed in this environment), reasoning
enabled, tool use and local code execution enabled
## 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)
- [ ] 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
- [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>
2026-04-14 13:34:52 -05:00
|
|
|
useToastActions: () => ({ pushToast: vi.fn() }),
|
2026-04-04 13:38:34 -05:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../api/routines", () => ({
|
|
|
|
|
routinesApi: {
|
|
|
|
|
list: (companyId: string) => routinesListMock(companyId),
|
|
|
|
|
create: vi.fn(),
|
|
|
|
|
update: vi.fn(),
|
|
|
|
|
run: vi.fn(),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../api/issues", () => ({
|
|
|
|
|
issuesApi: {
|
|
|
|
|
list: (companyId: string, filters?: Record<string, unknown>) => issuesListMock(companyId, filters),
|
|
|
|
|
update: vi.fn(),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../api/agents", () => ({
|
|
|
|
|
agentsApi: {
|
|
|
|
|
list: vi.fn(async () => [
|
|
|
|
|
{
|
|
|
|
|
id: "agent-1",
|
|
|
|
|
companyId: "company-1",
|
|
|
|
|
name: "Agent One",
|
|
|
|
|
role: "engineer",
|
|
|
|
|
title: null,
|
|
|
|
|
status: "active",
|
|
|
|
|
reportsTo: null,
|
|
|
|
|
capabilities: null,
|
|
|
|
|
adapterType: "process",
|
|
|
|
|
adapterConfig: {},
|
|
|
|
|
contextMode: "thin",
|
|
|
|
|
budgetMonthlyCents: 0,
|
|
|
|
|
spentMonthlyCents: 0,
|
|
|
|
|
lastHeartbeatAt: null,
|
|
|
|
|
icon: "code",
|
|
|
|
|
metadata: null,
|
|
|
|
|
createdAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
updatedAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
urlKey: "agent-one",
|
|
|
|
|
pauseReason: null,
|
|
|
|
|
pausedAt: null,
|
|
|
|
|
permissions: null,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "agent-2",
|
|
|
|
|
companyId: "company-1",
|
|
|
|
|
name: "Agent Two",
|
|
|
|
|
role: "engineer",
|
|
|
|
|
title: null,
|
|
|
|
|
status: "active",
|
|
|
|
|
reportsTo: null,
|
|
|
|
|
capabilities: null,
|
|
|
|
|
adapterType: "process",
|
|
|
|
|
adapterConfig: {},
|
|
|
|
|
contextMode: "thin",
|
|
|
|
|
budgetMonthlyCents: 0,
|
|
|
|
|
spentMonthlyCents: 0,
|
|
|
|
|
lastHeartbeatAt: null,
|
|
|
|
|
icon: "code",
|
|
|
|
|
metadata: null,
|
|
|
|
|
createdAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
updatedAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
urlKey: "agent-two",
|
|
|
|
|
pauseReason: null,
|
|
|
|
|
pausedAt: null,
|
|
|
|
|
permissions: null,
|
|
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../api/projects", () => ({
|
|
|
|
|
projectsApi: {
|
|
|
|
|
list: vi.fn(async () => [
|
|
|
|
|
{
|
|
|
|
|
id: "project-1",
|
|
|
|
|
companyId: "company-1",
|
|
|
|
|
urlKey: "project-alpha",
|
|
|
|
|
goalId: null,
|
|
|
|
|
goalIds: [],
|
|
|
|
|
goals: [],
|
|
|
|
|
name: "Project Alpha",
|
|
|
|
|
description: null,
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
leadAgentId: null,
|
|
|
|
|
targetDate: null,
|
|
|
|
|
color: "#22c55e",
|
|
|
|
|
pauseReason: null,
|
|
|
|
|
pausedAt: null,
|
|
|
|
|
archivedAt: null,
|
|
|
|
|
executionWorkspacePolicy: null,
|
|
|
|
|
codebase: null,
|
|
|
|
|
workspaces: [],
|
|
|
|
|
primaryWorkspace: null,
|
|
|
|
|
createdAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
updatedAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "project-2",
|
|
|
|
|
companyId: "company-1",
|
|
|
|
|
urlKey: "project-beta",
|
|
|
|
|
goalId: null,
|
|
|
|
|
goalIds: [],
|
|
|
|
|
goals: [],
|
|
|
|
|
name: "Project Beta",
|
|
|
|
|
description: null,
|
|
|
|
|
status: "in_progress",
|
|
|
|
|
leadAgentId: null,
|
|
|
|
|
targetDate: null,
|
|
|
|
|
color: "#38bdf8",
|
|
|
|
|
pauseReason: null,
|
|
|
|
|
pausedAt: null,
|
|
|
|
|
archivedAt: null,
|
|
|
|
|
executionWorkspacePolicy: null,
|
|
|
|
|
codebase: null,
|
|
|
|
|
workspaces: [],
|
|
|
|
|
primaryWorkspace: null,
|
|
|
|
|
createdAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
updatedAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
},
|
|
|
|
|
]),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../api/instanceSettings", () => ({
|
|
|
|
|
instanceSettingsApi: {
|
|
|
|
|
getExperimental: vi.fn(async () => ({ enableIsolatedWorkspaces: false })),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../api/heartbeats", () => ({
|
|
|
|
|
heartbeatsApi: {
|
|
|
|
|
liveRunsForCompany: vi.fn(async () => []),
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../components/IssuesList", () => ({
|
|
|
|
|
IssuesList: (props: { issues: Issue[] }) => issuesListRenderMock(props),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../components/PageTabBar", () => ({
|
|
|
|
|
PageTabBar: ({ items }: { items: Array<{ label: string }> }) => (
|
|
|
|
|
<div>{items.map((item) => item.label).join(", ")}</div>
|
|
|
|
|
),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("@/components/ui/tabs", () => ({
|
|
|
|
|
Tabs: ({ children }: { children: unknown }) => <div>{children as never}</div>,
|
|
|
|
|
TabsContent: ({ children }: { children: unknown }) => <div>{children as never}</div>,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../components/MarkdownEditor", () => ({
|
|
|
|
|
MarkdownEditor: () => <div />,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../components/InlineEntitySelector", () => ({
|
|
|
|
|
InlineEntitySelector: () => <button type="button">selector</button>,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../components/RoutineRunVariablesDialog", () => ({
|
|
|
|
|
RoutineRunVariablesDialog: () => null,
|
|
|
|
|
routineRunNeedsConfiguration: () => false,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../components/RoutineVariablesEditor", () => ({
|
|
|
|
|
RoutineVariablesEditor: () => null,
|
|
|
|
|
RoutineVariablesHint: () => null,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
vi.mock("../components/AgentIconPicker", () => ({
|
|
|
|
|
AgentIcon: () => <span data-testid="agent-icon" />,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
|
|
|
|
|
|
|
|
|
|
function createRoutine(overrides: Partial<RoutineListItem>): RoutineListItem {
|
|
|
|
|
return {
|
|
|
|
|
id: "routine-1",
|
|
|
|
|
companyId: "company-1",
|
|
|
|
|
projectId: "project-1",
|
|
|
|
|
goalId: null,
|
|
|
|
|
parentIssueId: null,
|
|
|
|
|
title: "Routine title",
|
|
|
|
|
description: null,
|
|
|
|
|
assigneeAgentId: "agent-1",
|
|
|
|
|
priority: "medium",
|
|
|
|
|
status: "active",
|
|
|
|
|
concurrencyPolicy: "coalesce_if_active",
|
|
|
|
|
catchUpPolicy: "skip_missed",
|
|
|
|
|
variables: [],
|
|
|
|
|
createdByAgentId: null,
|
|
|
|
|
createdByUserId: null,
|
|
|
|
|
updatedByAgentId: null,
|
|
|
|
|
updatedByUserId: null,
|
|
|
|
|
lastTriggeredAt: null,
|
|
|
|
|
lastEnqueuedAt: null,
|
|
|
|
|
createdAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
updatedAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
triggers: [],
|
|
|
|
|
lastRun: null,
|
|
|
|
|
activeIssue: null,
|
|
|
|
|
...overrides,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createIssue(overrides: Partial<Issue> = {}): Issue {
|
|
|
|
|
return {
|
|
|
|
|
id: "issue-1",
|
|
|
|
|
identifier: "PAP-1000",
|
|
|
|
|
companyId: "company-1",
|
|
|
|
|
projectId: "project-1",
|
|
|
|
|
projectWorkspaceId: null,
|
|
|
|
|
goalId: null,
|
|
|
|
|
parentId: null,
|
|
|
|
|
title: "Routine execution issue",
|
|
|
|
|
description: null,
|
|
|
|
|
status: "todo",
|
|
|
|
|
priority: "medium",
|
|
|
|
|
assigneeAgentId: "agent-1",
|
|
|
|
|
assigneeUserId: null,
|
|
|
|
|
createdByAgentId: null,
|
|
|
|
|
createdByUserId: null,
|
|
|
|
|
issueNumber: 1000,
|
|
|
|
|
originKind: "routine_execution",
|
|
|
|
|
originId: "routine-1",
|
|
|
|
|
originRunId: null,
|
|
|
|
|
requestDepth: 0,
|
|
|
|
|
billingCode: null,
|
|
|
|
|
assigneeAdapterOverrides: null,
|
|
|
|
|
executionWorkspaceId: null,
|
|
|
|
|
executionWorkspacePreference: null,
|
|
|
|
|
executionWorkspaceSettings: null,
|
|
|
|
|
checkoutRunId: null,
|
|
|
|
|
executionRunId: null,
|
|
|
|
|
executionAgentNameKey: null,
|
|
|
|
|
executionLockedAt: null,
|
|
|
|
|
startedAt: null,
|
|
|
|
|
completedAt: null,
|
|
|
|
|
cancelledAt: null,
|
|
|
|
|
hiddenAt: null,
|
|
|
|
|
createdAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
updatedAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
labels: [],
|
|
|
|
|
labelIds: [],
|
|
|
|
|
myLastTouchAt: null,
|
|
|
|
|
lastExternalCommentAt: null,
|
|
|
|
|
lastActivityAt: new Date("2026-04-01T00:00:00.000Z"),
|
|
|
|
|
isUnreadForMe: false,
|
|
|
|
|
...overrides,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function flush() {
|
|
|
|
|
await Promise.resolve();
|
|
|
|
|
await Promise.resolve();
|
|
|
|
|
await new Promise((resolve) => window.setTimeout(resolve, 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("Routines page", () => {
|
|
|
|
|
let container: HTMLDivElement;
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
container = document.createElement("div");
|
|
|
|
|
document.body.appendChild(container);
|
|
|
|
|
currentSearch = "";
|
|
|
|
|
navigateMock.mockReset();
|
|
|
|
|
routinesListMock.mockReset();
|
|
|
|
|
issuesListMock.mockReset();
|
|
|
|
|
issuesListRenderMock.mockClear();
|
|
|
|
|
localStorage.clear();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
container.remove();
|
|
|
|
|
document.body.innerHTML = "";
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("groups routines by project using project names for the section labels", () => {
|
|
|
|
|
const groups = buildRoutineGroups(
|
|
|
|
|
[
|
|
|
|
|
createRoutine({ id: "routine-1", title: "Morning sync", projectId: "project-1" }),
|
|
|
|
|
createRoutine({ id: "routine-2", title: "Weekly digest", projectId: "project-2", assigneeAgentId: "agent-2" }),
|
|
|
|
|
],
|
|
|
|
|
"project",
|
|
|
|
|
new Map([
|
|
|
|
|
["project-1", { name: "Project Alpha" }],
|
|
|
|
|
["project-2", { name: "Project Beta" }],
|
|
|
|
|
]),
|
|
|
|
|
new Map([
|
|
|
|
|
["agent-1", { name: "Agent One" }],
|
|
|
|
|
["agent-2", { name: "Agent Two" }],
|
|
|
|
|
]),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(groups.map((group) => group.label)).toEqual(["Project Alpha", "Project Beta"]);
|
|
|
|
|
expect(groups[0]?.items.map((item) => item.title)).toEqual(["Morning sync"]);
|
|
|
|
|
expect(groups[1]?.items.map((item) => item.title)).toEqual(["Weekly digest"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("shows recent runs through the issues list scoped to routine execution issues", async () => {
|
|
|
|
|
currentSearch = "tab=runs";
|
|
|
|
|
routinesListMock.mockResolvedValue([createRoutine({ id: "routine-1" })]);
|
|
|
|
|
issuesListMock.mockResolvedValue([
|
|
|
|
|
createIssue({ id: "issue-1", title: "Routine execution A" }),
|
|
|
|
|
createIssue({ id: "issue-2", title: "Routine execution B", identifier: "PAP-1001", issueNumber: 1001 }),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const root = createRoot(container);
|
|
|
|
|
const queryClient = new QueryClient({
|
|
|
|
|
defaultOptions: {
|
|
|
|
|
queries: { retry: false },
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
root.render(
|
|
|
|
|
<QueryClientProvider client={queryClient}>
|
|
|
|
|
<Routines />
|
|
|
|
|
</QueryClientProvider>,
|
|
|
|
|
);
|
|
|
|
|
await flush();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(issuesListMock).toHaveBeenCalledWith("company-1", { originKind: "routine_execution" });
|
|
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
|
root.unmount();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|