Files
PaperClipAI/server/src/__tests__/routine-suggestions.test.ts
T
43b2d8be22 feat(dashboard): add an experimental areas and responsibilities dashboard (#22069)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The dashboard is the first page a person sees in a company
> - The classic dashboard shows agents, runs, costs and recent tasks
> - It does not show what the person is responsible for, or if that work
goes well
> - People think in areas, such as Fundraising or Customers, and in the
things they own in each area
> - This pull request adds a dashboard built on areas and
responsibilities, behind an experimental setting that is off by default
> - The benefit is that a person sees what they own, how it is going,
and what agents do for it, on one page

## Linked Issues or Issue Description

No public issue exists for this work. The description follows the
feature request template.

**Subsystem affected**

Dashboard, navigation, the task conversation, routines, and the instance
experimental settings.

**Problem or motivation**

The classic dashboard counts agents and tasks. It does not say what a
person owns or if it goes well. People who run a company with agents
want to see their areas, the responsibilities in each area, the bar each
one must meet, and the agents and routines that serve it.

**Proposed solution**

A person owns responsibilities. Each responsibility belongs to an area
and has a bar, a number to reach. Projects, tasks and routines link to a
responsibility, and the page shows its status from those links. An agent
can interview the person and draft the dashboard. The person accepts or
declines each change, and any change can be undone from a saved version.

**Alternatives considered**

Add the same data to the classic dashboard. That page is a fixed set of
cards for company activity, so a second, person-centred page is clearer.
The setting lets a person pick one of the two.

**Roadmap alignment**

This is an experimental feature behind `enableResponsibilities`. It is
off by default and changes nothing for a person who leaves it off.

## What Changed

- Add the setting "Areas & Responsibilities dashboard"
(`enableResponsibilities`, off by default). On, Dashboard in the sidebar
opens the new page, and the classic dashboard link is not shown in the
sidebars or the command palette. Off, nothing changes.
- Add tables and migrations 0492 to 0497 for areas, responsibilities,
links to work, metrics, status reports, saved dashboard versions, and
model-call records for summaries.
- Add the API: areas, responsibilities, links, bars, metrics, status
reports, summaries, runs, automation, and the dashboard conversation
with versions and undo. Each route checks company access and writes to
the activity log.
- Add four task conversations with an agent: about a bar, about an
automation, about a routine, and about the whole dashboard. The agent
proposes a change. Nothing applies until a person accepts it.
- Add standing checks: routines that an agent carries out on its
heartbeat and reports.
- Add the Dashboard page: search, counts, areas that open and close, a
primary New area button, and a message box at the bottom of the page.
Add the responsibility page with its bar, metrics, work and routines.
- Add the Paperclip skill sections and runner API entries that let an
agent use these routes. The runner skill inventory is regenerated (166
to 174 rows).
- Keep the new runner API tests in their own file so
`runner-api.test.ts` stays at 985 lines.
- Onboarding is not changed. The onboarding changes that build on this
work are a separate pull request.

## Verification

- Type checks pass for `packages/db`, `packages/shared`, `server` (all
layers) and `ui`.
- `pnpm --filter @paperclipai/db check:migrations` passes. A new
`drizzle-kit generate` run produces no further change.
- Tests for every changed file pass: 26 server test files (3,009 tests),
116 UI test files (1,991 tests) and 28 shared test files (432 tests).
- The runner capability inventory checks pass.
- I started the app on a local database that already held earlier data.
Master's new migrations and these migrations applied without error. With
the setting on, the Dashboard and a responsibility page loaded with no
console or server errors. The More menu and the command palette show one
Dashboard.
- I did not run `pnpm verify`, the full local suite, or the browser
suites. Required CI covers them.
- The agent conversations are tested with simulated agents. No real
model has run them.

## Risks

- Medium. The change is large, and the setting is off by default. With
it off, the new routes refuse requests and the UI is unchanged.
- Migrations only add tables and columns. Two replace a CHECK
constraint, and one builds an index `CONCURRENTLY`. These two have the
`online_compatible` override with a reason in the file, and the second
one is listed in `MIGRATIONS_WITH_TOP_LEVEL_STATEMENTS`.
- Master can add migrations before this merges. The migrations then need
new numbers.
- `responsibilities.ts` (1,663 lines), `responsibility-summaries.ts`
(1,239 lines) and one Storybook fixture file (1,305 lines) are new files
over 1,000 lines. They each keep one cohesive unit together: the service
for one table family, the model-call path for summaries, and the
fixtures that the stories share. No existing file crosses 1,000 lines.

> 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

- Anthropic Claude: Claude Fable 5.2 (`claude-fable-5-2`) wrote most of
the change, with Claude Sonnet 5.5 (`claude-sonnet-5-5`) for the final
steps. Both used tool use, code execution and browser checks.

## 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 either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [ ] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [ ] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Scott Tong <scott@scottsmbpm5max.lan>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-17 20:45:44 -08:00

612 lines
32 KiB
TypeScript

import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js";
import { randomUUID } from "node:crypto";
import express from "express";
import request from "supertest";
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
import { and, eq } from "drizzle-orm";
import {
activityLog,
agents,
companies,
companyMemberships,
createDb,
issueThreadInteractions,
issues,
projects,
responsibilities,
responsibilityLinks,
routineTriggers,
routines,
} from "@paperclipai/db";
import { ROUTINE_FROM_TASK_ORIGIN_KIND } from "@paperclipai/shared";
import { errorHandler } from "../middleware/index.js";
import { routineSuggestionRoutes } from "../routes/routine-suggestions.js";
import { instanceSettingsService } from "../services/instance-settings.js";
import { issueThreadInteractionService } from "../services/issue-thread-interactions.js";
import { routineSuggestionService, type RoutineSuggestionResponsibilityFiling } from "../services/routine-suggestions.js";
import {
getEmbeddedPostgresTestSupport,
startEmbeddedPostgresTestDatabase,
} from "./helpers/embedded-postgres.js";
const externalDatabaseUrl = resolveExternalTestDatabaseUrl("PAPERCLIP_ROUTINE_SUGGESTIONS_TEST_DATABASE_URL");
const embeddedPostgresSupport = externalDatabaseUrl ? { supported: true } : await getEmbeddedPostgresTestSupport();
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
type Db = ReturnType<typeof createDb>;
type Actor = Express.Request["actor"];
const OWNER = "board-user";
function boardActor(): Actor {
return { type: "board", userId: OWNER, source: "local_implicit", isInstanceAdmin: true };
}
function agentActor(companyId: string, agentId: string): Actor {
return { type: "agent", companyId, agentId, source: "agent_jwt" };
}
const WEEKLY_OFFER = {
title: "Weekly competitor pricing summary",
instructions: "Check the three competitors' pricing pages and summarize what changed since last week.",
message: "This looks like something you will want every week.",
cadence: { frequency: "weekly", dayOfWeek: "monday", time: "09:00" },
};
describeEmbeddedPostgres("routine suggestions", () => {
let db!: Db;
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
let filedUnder: string[] = [];
let fakeFiling: RoutineSuggestionResponsibilityFiling;
beforeAll(async () => {
if (!externalDatabaseUrl) tempDb = await startEmbeddedPostgresTestDatabase("paperclip-routine-suggestions-");
db = createDb(externalDatabaseUrl ?? tempDb!.connectionString, { maxConnections: 6 });
}, 30_000);
afterAll(async () => {
await db?.$client.end({ timeout: 1 });
await tempDb?.cleanup();
});
beforeEach(async () => {
filedUnder = [];
fakeFiling = {
responsibilitiesForIssue: async () => [],
fileRoutine: async () => null,
};
await instanceSettingsService(db).updateExperimental({ enableResponsibilities: true });
});
/** `filing: "module"` uses the real responsibilities module instead of the fake. */
function createApp(actor: Actor, filing: RoutineSuggestionResponsibilityFiling | "module" = fakeFiling) {
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
req.actor = actor;
next();
});
app.use("/api", routineSuggestionRoutes(db, { filing: filing === "module" ? undefined : filing }));
app.use(errorHandler);
return app;
}
async function seed(input: {
status?: string;
originKind?: string;
parentId?: string | null;
withProject?: boolean;
} = {}) {
const company = await db.insert(companies)
.values({ name: "Routine Co", issuePrefix: `S${randomUUID().replace(/[^A-Z0-9]/gi, "").slice(0, 6).toUpperCase()}` })
.returning().then((rows) => rows[0]!);
await db.insert(companyMemberships).values({
companyId: company.id, principalType: "user", principalId: OWNER, status: "active", membershipRole: "owner",
});
const agent = await db.insert(agents).values({
companyId: company.id, name: "Analyst", role: "general", status: "idle",
adapterType: "codex_local", adapterConfig: { model: "gpt-5.4" },
}).returning().then((rows) => rows[0]!);
const project = input.withProject === false ? null
: await db.insert(projects).values({ companyId: company.id, name: "Market watch" }).returning().then((rows) => rows[0]!);
const issue = await db.insert(issues).values({
companyId: company.id,
projectId: project?.id ?? null,
title: "Summarize competitor pricing",
status: input.status ?? "done",
priority: "medium",
assigneeAgentId: agent.id,
originKind: input.originKind ?? "manual",
parentId: input.parentId ?? null,
createdByUserId: OWNER,
}).returning().then((rows) => rows[0]!);
return { company, agent, project, issue };
}
async function addTask(companyId: string, agentId: string, values: { originKind?: string; parentId?: string | null; status?: string }) {
return db.insert(issues).values({
companyId,
title: "Related task",
status: values.status ?? "done",
priority: "medium",
assigneeAgentId: agentId,
originKind: values.originKind ?? "manual",
parentId: values.parentId ?? null,
}).returning().then((rows) => rows[0]!);
}
async function offer(seeded: { company: { id: string }; agent: { id: string }; issue: { id: string } }, body: object = WEEKLY_OFFER) {
return request(createApp(agentActor(seeded.company.id, seeded.agent.id)))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions`).send(body);
}
it("is not available while the responsibilities experiment is off", async () => {
await instanceSettingsService(db).updateExperimental({ enableResponsibilities: false });
const seeded = await seed();
const res = await offer(seeded);
expect(res.status).toBe(404);
expect(await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.issueId, seeded.issue.id))).toEqual([]);
});
it("lets the assigned agent offer once on a finished task, with a server-derived cadence", async () => {
const seeded = await seed();
const res = await offer(seeded);
expect(res.status).toBe(201);
expect(res.body).toMatchObject({
kind: "suggest_routine",
status: "pending",
continuationPolicy: "none",
effectiveResolverPolicy: "human_only",
createdByAgentId: seeded.agent.id,
title: 'Repeat "Weekly competitor pricing summary"?',
summary: "Every Monday at 9 AM",
payload: {
cadenceLabel: "Every Monday at 9 AM",
cronExpression: "0 9 * * 1",
assigneeAgentId: seeded.agent.id,
projectId: seeded.project!.id,
message: "This looks like something you will want every week.",
},
});
// The offer stays open on the done task instead of reading as expired.
const listed = await issueThreadInteractionService(db).listForIssue(seeded.issue.id);
expect(listed.map((item) => [item.kind, item.status])).toEqual([["suggest_routine", "pending"]]);
// Closing paths that expire a done task's cards leave the offer alone.
await issueThreadInteractionService(db).expirePendingInteractionsForTerminalIssue(
{ id: seeded.issue.id, companyId: seeded.company.id, status: "done" },
);
expect((await issueThreadInteractionService(db).listForIssue(seeded.issue.id))[0]!.status).toBe("pending");
const again = await offer(seeded, { ...WEEKLY_OFFER, title: "Something else" });
expect(again.status).toBe(409);
expect(again.body.details?.code ?? again.body.code).toBe("routine_suggestion_exists");
const [activity] = await db.select().from(activityLog).where(and(
eq(activityLog.companyId, seeded.company.id),
eq(activityLog.action, "issue.thread_interaction_created"),
));
expect(activity?.details).toMatchObject({ interactionKind: "suggest_routine" });
});
it("never offers for a cancelled task", async () => {
const seeded = await seed({ status: "cancelled" });
const res = await offer(seeded);
expect(res.status).toBe(409);
expect(JSON.stringify(res.body)).toContain("routine_suggestion_task_not_done");
});
it("holds an offer made before the task is done until it finishes successfully", async () => {
// A native agent's completion settles after its turn, so its offer can
// arrive while the task is still open.
const seeded = await seed({ status: "in_progress" });
const offered = await offer(seeded);
expect(offered.status).toBe(201);
const interactions = issueThreadInteractionService(db);
expect(await interactions.listForIssue(seeded.issue.id)).toEqual([]);
const early = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(early.status).toBe(409);
expect(await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).toHaveLength(0);
for (const status of ["in_review", "blocked"]) {
await db.update(issues).set({ status }).where(eq(issues.id, seeded.issue.id));
expect(await interactions.listForIssue(seeded.issue.id), status).toEqual([]);
}
await db.update(issues).set({ status: "done" }).where(eq(issues.id, seeded.issue.id));
expect((await interactions.listForIssue(seeded.issue.id)).map((item) => item.status)).toEqual(["pending"]);
const accepted = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(accepted.status).toBe(200);
});
it("refuses work a routine created, including its subtasks", async () => {
const seeded = await seed({ originKind: "routine_execution" });
expect((await offer(seeded)).status).toBe(422);
const child = await addTask(seeded.company.id, seeded.agent.id, { parentId: seeded.issue.id });
const res = await offer({ ...seeded, issue: child });
expect(res.status).toBe(422);
expect(JSON.stringify(res.body)).toContain("routine");
});
it("refuses the onboarding first task and its subtasks", async () => {
const seeded = await seed({ originKind: "onboarding_first_task" });
expect((await offer(seeded)).status).toBe(422);
const child = await addTask(seeded.company.id, seeded.agent.id, { parentId: seeded.issue.id });
const grandchild = await addTask(seeded.company.id, seeded.agent.id, { parentId: child.id });
expect((await offer({ ...seeded, issue: grandchild })).status).toBe(422);
});
it("only lets the task's own agent make the offer", async () => {
const seeded = await seed();
const other = await db.insert(agents).values({
companyId: seeded.company.id, name: "Other", role: "general", status: "idle",
adapterType: "codex_local", adapterConfig: {},
}).returning().then((rows) => rows[0]!);
expect((await offer({ ...seeded, agent: other })).status).toBe(403);
const asBoard = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions`).send(WEEKLY_OFFER);
expect(asBoard.status).toBe(403);
});
it("rejects incomplete cadences and template placeholders", async () => {
const seeded = await seed();
expect((await offer(seeded, { ...WEEKLY_OFFER, cadence: { frequency: "weekly", time: "09:00" } })).status).toBe(400);
expect((await offer(seeded, { ...WEEKLY_OFFER, cadence: { frequency: "monthly", dayOfMonth: 31, time: "09:00" } })).status).toBe(400);
expect((await offer(seeded, { ...WEEKLY_OFFER, instructions: "Summarize {{topic}}" })).status).toBe(400);
expect((await offer(seeded)).status).toBe(201);
});
it("accepting creates a scheduled routine with the task's agent and project, filed under the task's responsibilities", async () => {
const seeded = await seed();
const offered = await offer(seeded);
fakeFiling = {
responsibilitiesForIssue: async (companyId, issueId) => {
expect([companyId, issueId]).toEqual([seeded.company.id, seeded.issue.id]);
return [{ id: "resp-a" }, { id: "resp-b" }];
},
fileRoutine: async (responsibilityId) => {
filedUnder.push(responsibilityId);
return { linkId: `link-${responsibilityId}`, created: true };
},
};
// Agents cannot accept their own offer.
const asAgent = await request(createApp(agentActor(seeded.company.id, seeded.agent.id)))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(asAgent.status).toBe(403);
const res = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`)
.send({ timezone: "America/Chicago" });
expect(res.status).toBe(200);
const routineId = res.body.routine.id as string;
expect(res.body.interaction).toMatchObject({
status: "accepted",
resolvedByUserId: OWNER,
result: {
outcome: "accepted",
routineId,
routineTitle: "Weekly competitor pricing summary",
cadenceLabel: "Every Monday at 9 AM",
timezone: "America/Chicago",
responsibilityIds: ["resp-a", "resp-b"],
},
});
expect(filedUnder).toEqual(["resp-a", "resp-b"]);
const [routine] = await db.select().from(routines).where(eq(routines.id, routineId));
expect(routine).toMatchObject({
companyId: seeded.company.id,
assigneeAgentId: seeded.agent.id,
projectId: seeded.project!.id,
title: "Weekly competitor pricing summary",
status: "active",
responsibleUserId: OWNER,
});
const triggers = await db.select().from(routineTriggers).where(eq(routineTriggers.routineId, routineId));
expect(triggers).toHaveLength(1);
expect(triggers[0]).toMatchObject({
kind: "schedule", cronExpression: "0 9 * * 1", timezone: "America/Chicago", enabled: true, label: "Every Monday at 9 AM",
});
const actions = (await db.select({ action: activityLog.action }).from(activityLog)
.where(eq(activityLog.companyId, seeded.company.id))).map((row) => row.action);
expect(actions).toEqual(expect.arrayContaining([
"routine.created", "routine.trigger_created", "responsibility.linked", "issue.thread_interaction_accepted",
]));
// The offer is settled: a second accept makes nothing new.
const again = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(again.status).toBe(409);
expect(await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).toHaveLength(1);
});
it("files the routine under every responsibility the task is under, directly or through its project", async () => {
const seeded = await seed();
const [direct, viaProject, letGo] = await db.insert(responsibilities).values([
{ companyId: seeded.company.id, ownerUserId: OWNER, title: "Pricing", status: "active" },
{ companyId: seeded.company.id, ownerUserId: OWNER, title: "Market", status: "active" },
{ companyId: seeded.company.id, ownerUserId: OWNER, title: "Old", status: "let_go" },
]).returning();
await db.insert(responsibilityLinks).values([
{ companyId: seeded.company.id, responsibilityId: direct!.id, issueId: seeded.issue.id },
{ companyId: seeded.company.id, responsibilityId: viaProject!.id, projectId: seeded.project!.id },
{ companyId: seeded.company.id, responsibilityId: letGo!.id, issueId: seeded.issue.id },
]);
const offered = await offer(seeded);
// No injected filing: use the responsibilities module.
const res = await request(createApp(boardActor(), "module"))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({ timezone: "UTC" });
expect(res.status).toBe(200);
const routineId = res.body.routine.id as string;
const links = await db.select({ responsibilityId: responsibilityLinks.responsibilityId })
.from(responsibilityLinks).where(eq(responsibilityLinks.routineId, routineId));
expect(links.map((link) => link.responsibilityId).sort()).toEqual([direct!.id, viaProject!.id].sort());
expect([...res.body.interaction.result.responsibilityIds].sort()).toEqual([direct!.id, viaProject!.id].sort());
});
it("leaves a loose task's routine loose", async () => {
const seeded = await seed({ withProject: false });
const offered = await offer(seeded);
const res = await request(createApp(boardActor(), "module"))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(res.status).toBe(200);
expect(res.body.routine.projectId).toBeNull();
expect(res.body.interaction.result).toMatchObject({ timezone: "UTC", responsibilityIds: [] });
});
it("reopens the offer when the routine cannot be created", async () => {
const seeded = await seed();
const offered = await offer(seeded);
await db.update(agents).set({ status: "terminated" }).where(eq(agents.id, seeded.agent.id));
const res = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(res.status).toBeGreaterThanOrEqual(400);
const [row] = await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, offered.body.id));
expect(row).toMatchObject({ status: "pending", result: null });
expect(await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).toHaveLength(0);
});
async function strandAccept(offerId: string, extra: Record<string, unknown> = {}) {
const claimedAt = new Date(Date.now() - 5 * 60_000);
await db.update(issueThreadInteractions).set({
status: "accepted", result: { version: 1, outcome: "accepted", claimId: "interrupted-claim", ...extra } as never,
resolvedByUserId: OWNER, resolvedAt: claimedAt, updatedAt: claimedAt,
}).where(eq(issueThreadInteractions.id, offerId));
}
it("finishes an accept that a restart interrupted, archiving only the routine it had made", async () => {
const seeded = await seed();
const offered = await offer(seeded);
const [stray] = await db.insert(routines).values({
companyId: seeded.company.id, title: "Weekly competitor pricing summary", assigneeAgentId: seeded.agent.id,
originKind: ROUTINE_FROM_TASK_ORIGIN_KIND, originId: seeded.issue.id,
}).returning();
await strandAccept(offered.body.id, { pendingRoutineId: stray!.id });
const res = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({ timezone: "UTC" });
expect(res.status).toBe(200);
expect(res.body.interaction.result.routineId).toBe(res.body.routine.id);
const live = (await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).filter((row) => row.status !== "archived");
expect(live.map((row) => row.id)).toEqual([res.body.routine.id]);
expect((await db.select().from(routines).where(eq(routines.id, stray!.id)))[0]).toMatchObject({ status: "archived" });
});
it("leaves a routine the person made meanwhile alone, and closes the interrupted offer", async () => {
const seeded = await seed();
const offered = await offer(seeded);
await strandAccept(offered.body.id);
const [theirs] = await db.insert(routines).values({
companyId: seeded.company.id, title: "Their own schedule", assigneeAgentId: seeded.agent.id,
originKind: ROUTINE_FROM_TASK_ORIGIN_KIND, originId: seeded.issue.id,
}).returning();
const res = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({ timezone: "UTC" });
expect(res.status).toBe(409);
const all = await db.select().from(routines).where(eq(routines.companyId, seeded.company.id));
expect(all.map((row) => [row.id, row.status])).toEqual([[theirs!.id, "active"]]);
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, offered.body.id)))[0])
.toMatchObject({ status: "expired" });
});
it("stops a slow accept from changing a card another accept took over", async () => {
const seeded = await seed();
const offered = await offer(seeded);
await strandAccept(offered.body.id);
const res = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({ timezone: "UTC" });
expect(res.status).toBe(200);
const interactions = issueThreadInteractionService(db);
const issueRef = { id: seeded.issue.id, companyId: seeded.company.id };
expect(await interactions.noteAcceptClaimRoutine(issueRef, offered.body.id, "interrupted-claim", randomUUID())).toBe(false);
await expect(interactions.recordAcceptedRoutineSuggestion(issueRef, offered.body.id, "interrupted-claim", {
routineId: randomUUID(), routineTitle: "Old", cadenceLabel: "Every Monday at 9 AM", timezone: "UTC", responsibilityIds: [],
})).rejects.toThrow();
await interactions.releaseRoutineSuggestionClaim(issueRef, offered.body.id, "interrupted-claim");
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, offered.body.id)))[0])
.toMatchObject({ status: "accepted", result: expect.objectContaining({ routineId: res.body.routine.id }) });
});
it("keeps a saved acceptance and its routine when later work fails", async () => {
const seeded = await seed();
const offered = await offer(seeded);
const real = issueThreadInteractionService(db);
const failing = {
...real,
recordAcceptedRoutineSuggestion: async (...args: Parameters<typeof real.recordAcceptedRoutineSuggestion>) => {
await real.recordAcceptedRoutineSuggestion(...args);
throw new Error("Could not refresh the task");
},
};
const svc = routineSuggestionService(db, { filing: fakeFiling, interactions: failing });
await expect(svc.accept({ id: seeded.issue.id, companyId: seeded.company.id }, offered.body.id, { timezone: "UTC" }, { userId: OWNER }))
.rejects.toThrow("Could not refresh the task");
const all = await db.select().from(routines).where(eq(routines.companyId, seeded.company.id));
expect(all.map((row) => row.status)).toEqual(["active"]);
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, offered.body.id)))[0])
.toMatchObject({ status: "accepted", result: expect.objectContaining({ routineId: all[0]!.id }) });
});
it("does not take over an accept that may still be running", async () => {
const seeded = await seed();
const offered = await offer(seeded);
const claimedAt = new Date(Date.now() - 10_000);
await db.update(issueThreadInteractions).set({
status: "accepted", result: { version: 1, outcome: "accepted", claimId: "running-claim" } as never, resolvedByUserId: OWNER, resolvedAt: claimedAt, updatedAt: claimedAt,
}).where(eq(issueThreadInteractions.id, offered.body.id));
const res = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({ timezone: "UTC" });
expect(res.status).toBe(409);
expect(await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).toHaveLength(0);
});
it("never turns a private task into a routine", async () => {
const seeded = await seed();
const offered = await offer(seeded);
await db.update(issues).set({ visibility: "private", privacyRootIssueId: seeded.issue.id, responsibleUserId: OWNER })
.where(eq(issues.id, seeded.issue.id));
const accepted = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({ timezone: "UTC" });
expect(accepted.status).toBe(422);
const repeated = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/repeat-as-routine`)
.send({ title: "Weekly", cadence: { frequency: "weekly", dayOfWeek: "monday", time: "09:00" }, assigneeAgentId: seeded.agent.id, timezone: "UTC" });
expect(repeated.status).toBe(422);
expect(await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).toHaveLength(0);
expect((await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, offered.body.id)))[0])
.toMatchObject({ status: "pending" });
});
it("declining is quiet and final", async () => {
const seeded = await seed();
const offered = await offer(seeded);
const res = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/decline`).send({});
expect(res.status).toBe(200);
expect(res.body).toMatchObject({ status: "rejected", result: { outcome: "declined" } });
expect(await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).toHaveLength(0);
const accept = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(accept.status).toBe(409);
expect((await offer(seeded)).status).toBe(409);
});
describe("a person turns a task into a routine", () => {
const BODY = { title: "Weekly competitor pricing summary", cadence: { frequency: "monthly", dayOfMonth: 3, time: "08:30" }, timezone: "Europe/Berlin" };
function repeat(seeded: { issue: { id: string } }, body: object, actor: Actor = boardActor(), filing: RoutineSuggestionResponsibilityFiling | "module" = "module") {
return request(createApp(actor, filing)).post(`/api/issues/${seeded.issue.id}/repeat-as-routine`).send(body);
}
it("creates an active scheduled routine with the chosen agent, records the task, and files it under each responsibility", async () => {
const seeded = await seed({ status: "in_progress" });
const [direct, viaProject] = await db.insert(responsibilities).values([
{ companyId: seeded.company.id, ownerUserId: OWNER, title: "Pricing", status: "active" },
{ companyId: seeded.company.id, ownerUserId: OWNER, title: "Market", status: "active" },
]).returning();
await db.insert(responsibilityLinks).values([
{ companyId: seeded.company.id, responsibilityId: direct!.id, issueId: seeded.issue.id },
{ companyId: seeded.company.id, responsibilityId: viaProject!.id, projectId: seeded.project!.id },
]);
const res = await repeat(seeded, { ...BODY, assigneeAgentId: seeded.agent.id });
expect(res.status).toBe(201);
expect(res.body).toMatchObject({
cadenceLabel: "On the 3rd of every month at 8:30 AM",
timezone: "Europe/Berlin",
acceptedOfferId: null,
routine: { title: "Weekly competitor pricing summary", assigneeAgentId: seeded.agent.id, projectId: seeded.project!.id, status: "active" },
});
expect([...res.body.responsibilityIds].sort()).toEqual([direct!.id, viaProject!.id].sort());
const [routine] = await db.select().from(routines).where(eq(routines.id, res.body.routine.id));
expect(routine).toMatchObject({ originKind: "repeated_task", originId: seeded.issue.id, responsibleUserId: OWNER });
expect(routine!.description).toContain("Repeats");
const triggers = await db.select().from(routineTriggers).where(eq(routineTriggers.routineId, routine!.id));
expect(triggers).toHaveLength(1);
expect(triggers[0]).toMatchObject({ kind: "schedule", cronExpression: "30 8 3 * *", timezone: "Europe/Berlin", label: "On the 3rd of every month at 8:30 AM", enabled: true });
const links = await db.select({ responsibilityId: responsibilityLinks.responsibilityId })
.from(responsibilityLinks).where(eq(responsibilityLinks.routineId, routine!.id));
expect(links.map((link) => link.responsibilityId).sort()).toEqual([direct!.id, viaProject!.id].sort());
// The task itself is left as it is.
const [task] = await db.select().from(issues).where(eq(issues.id, seeded.issue.id));
expect(task).toMatchObject({ status: "in_progress", title: "Summarize competitor pricing" });
const actions = (await db.select({ action: activityLog.action }).from(activityLog)
.where(eq(activityLog.companyId, seeded.company.id))).map((row) => row.action);
expect(actions).toEqual(expect.arrayContaining(["routine.created", "routine.trigger_created", "responsibility.linked"]));
});
it("makes one routine from a double submit", async () => {
const seeded = await seed();
const body = { ...BODY, assigneeAgentId: seeded.agent.id };
const [first, second] = await Promise.all([repeat(seeded, body, boardActor(), fakeFiling), repeat(seeded, body, boardActor(), fakeFiling)]);
expect([first.status, second.status].sort()).toEqual([201, 409]);
const refused = first.status === 409 ? first : second;
expect(JSON.stringify(refused.body)).toContain("routine_origin_exists");
expect(await db.select().from(routines).where(eq(routines.companyId, seeded.company.id))).toHaveLength(1);
// Once that routine is archived, the task can be repeated again.
await db.update(routines).set({ status: "archived" }).where(eq(routines.companyId, seeded.company.id));
expect((await repeat(seeded, body, boardActor(), fakeFiling)).status).toBe(201);
});
it("settles the agent's pending offer on the task with the person's routine", async () => {
const seeded = await seed();
const offered = await offer(seeded);
const res = await repeat(seeded, { ...BODY, assigneeAgentId: seeded.agent.id }, boardActor(), fakeFiling);
expect(res.status).toBe(201);
expect(res.body.acceptedOfferId).toBe(offered.body.id);
const [row] = await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, offered.body.id));
expect(row).toMatchObject({ status: "accepted", result: { outcome: "accepted", routineId: res.body.routine.id, cadenceLabel: "On the 3rd of every month at 8:30 AM" } });
// The offer's instructions become what each run does.
const [routine] = await db.select().from(routines).where(eq(routines.id, res.body.routine.id));
expect(routine!.description).toBe(WEEKLY_OFFER.instructions);
});
it("refuses work a routine created, the onboarding task, bar conversations, cancelled tasks, agents and the flag being off", async () => {
const ok = (seeded: { agent: { id: string } }) => ({ ...BODY, assigneeAgentId: seeded.agent.id });
const routineMade = await seed({ originKind: "routine_execution" });
expect((await repeat(routineMade, ok(routineMade))).status).toBe(422);
const child = await addTask(routineMade.company.id, routineMade.agent.id, { parentId: routineMade.issue.id });
expect((await repeat({ issue: child }, ok(routineMade))).status).toBe(422);
const onboarding = await seed({ originKind: "onboarding_first_task" });
expect((await repeat(onboarding, ok(onboarding))).status).toBe(422);
const bar = await seed({ originKind: "responsibility_bar" });
const barRes = await repeat(bar, ok(bar));
expect(barRes.status).toBe(422);
expect(JSON.stringify(barRes.body)).toContain("routine_from_task_bar_conversation");
const cancelled = await seed({ status: "cancelled" });
expect((await repeat(cancelled, ok(cancelled))).status).toBe(409);
const plain = await seed();
expect((await repeat(plain, ok(plain), agentActor(plain.company.id, plain.agent.id))).status).toBe(403);
expect((await repeat(plain, { ...ok(plain), cadence: { frequency: "weekly", time: "09:00" } })).status).toBe(400);
await instanceSettingsService(db).updateExperimental({ enableResponsibilities: false });
expect((await repeat(plain, ok(plain))).status).toBe(404);
expect(await db.select().from(routines).where(eq(routines.companyId, plain.company.id))).toHaveLength(0);
});
});
it("withdraws the offer when the task is cancelled", async () => {
const seeded = await seed();
const offered = await offer(seeded);
await db.update(issues).set({ status: "cancelled" }).where(eq(issues.id, seeded.issue.id));
await issueThreadInteractionService(db).expirePendingInteractionsForTerminalIssue(
{ id: seeded.issue.id, companyId: seeded.company.id, status: "cancelled" },
);
const [row] = await db.select().from(issueThreadInteractions).where(eq(issueThreadInteractions.id, offered.body.id));
expect(row!.status).toBe("expired");
const accept = await request(createApp(boardActor()))
.post(`/api/issues/${seeded.issue.id}/routine-suggestions/${offered.body.id}/accept`).send({});
expect(accept.status).toBe(409);
});
});