mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-15 18:56:05 +01:00
## Thinking Path > - Paperclip helps people manage AI agents and their work. > - The inbox collects requests that need a human decision. > - Teams need to know who owns each request and who may act on it. > - A shared close operation must differ from hiding a card for one person. > - This pull request adds personal routing and durable outcomes to the existing attention service. > - People can claim or transfer work and inspect what another person did for them. > - Domain permissions still decide who may resolve each request. ## Linked Issues or Issue Description **Subsystem affected** Server, shared contracts, and database. This is the routing foundation for a stacked inbox UI change. **Problem or motivation** A company-wide attention feed does not give teams a clear personal queue. Archive, withdrawal, transfer, and decisions by another person need distinct scopes and records. **Proposed solution** Resolve a request audience from its addressee, task participants, and configured owners. Provide personal, unassigned, company, all-assignee, and archived views. Add claim, transfer, personal archive and snooze, shared withdrawal, receipts, and personal history. **Alternatives considered** A single shared inbox leaves ownership and personal hiding ambiguous. The service keeps one canonical request and projects its audience instead of copying requests for each person. **Roadmap alignment** This extends the existing Multiple Human Users and Agent Reviews and Approvals capabilities in ROADMAP.md. **Additional context** Refs #19363 and #19520 for the current inbox navigation and confirmation replacement rules. Refs #20268 for related actionable-request work. That PR filters actionability; this stack adds audience routing and personal outcomes. This is part 1 of a two-PR stack. The inbox UI PR #20961 targets this branch. ## What Changed - Add nullable company and project decision owners and a company budget owner. - Add audience views and personal badge counts to the canonical attention projections. - Add claim, transfer, withdrawal, personal archive, snooze, and restore operations with permission checks and activity records. Lock addressed transfers to the current addressee or routing administrators, and validate the recipient against canonical resolution policies. - Add receipts and scoped history for work handled by another person. - Keep closed-task requests and replaced confirmations out of current cards. - Add an idempotent migration and a concurrent receipt-history index. - Update typed fixtures so the existing UI remains compatible with the new contracts. ## Verification - Server layered typechecking passes, including all three test-tree partitions. - Seven routing and interaction-resolution suites pass all 249 tests against a dedicated external PostgreSQL test database on `4caecc21d8`. Six new regressions failed before the repair; they cover takeover followed by approval, legitimate delegation, excluded creators/review requesters, and unauthorized claims. - Prior focused verification covers private approval history and receipts, transfer visibility, audience escalation, withdrawal governance, hidden-card counts, per-recipient confirmation replacement, and SDK source contracts. - A synthetic three-person company passed 37 routing and lifecycle acceptance checks. The UI stack adds browser acceptance for the shared controls. - The complete [GitHub Actions CI matrix](https://github.com/paperclipai/paperclip/actions/runs/43134997331) passes on `4caecc21d891677323a7335f23b1a015cce24c13` (Linux/Node 24: typecheck, build, general and serialized tests, Runner support, browser shards, and aggregate gates). Greptile is 5/5 on that exact head with no open findings. The fresh security scan passes after explicitly rechecking the addressee-transfer repair. - `pnpm verify` reports exit 2 because a CI-global script changed; full CI supplies that broader coverage. Embedded PostgreSQL could not start on this Mac because shared-memory slots were exhausted, so local database checks use a separate external test database. Linux CI covers the embedded setup. ## Risks - Personal badge counts change to the viewer's assigned requests. Accessible feed totals and task membership keep their existing scope. - The migration adds nullable owner fields and a dismissal fingerprint. Its index runs outside a transaction. The migration is classified and rerunnable. - Routing changes the addressee used by resolution authorization. The current addressee or a routing administrator must authorize an addressed transfer; the recipient must satisfy writer access, canonical resolver exclusions, and task review policy under row locks. - The shared validator export barrel crosses 1,000 lines because it exports the new attention schemas. Schema implementation stays in the dedicated attention validator module. ## Model Used OpenAI GPT-6 through Codex. Used repository tools, code execution, and browser checks. This session does not expose a more specific runtime model ID or its context-window size. ## 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 described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal Paperclip ticket id - [x] I have run focused tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes (OpenAPI contracts and docs/api/attention.md) - [x] I have considered and documented any risks above - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Scott Tong <scott@scottsmbpm5max.lan> Co-authored-by: Paperclip <noreply@paperclip.ing>
547 lines
21 KiB
TypeScript
547 lines
21 KiB
TypeScript
import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js";
|
|
import { randomUUID } from "node:crypto";
|
|
import express from "express";
|
|
import request from "supertest";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { afterAll, afterEach, beforeAll, describe, expect, it } from "vitest";
|
|
import {
|
|
activityLog,
|
|
agents,
|
|
approvals,
|
|
companies,
|
|
createDb, applyPendingMigrations,
|
|
decisionQueueItems,
|
|
decisionQueues,
|
|
decisionTriage,
|
|
decisionTriageEvents,
|
|
issueThreadInteractions,
|
|
issueWorkProducts,
|
|
issues,
|
|
} from "@paperclipai/db";
|
|
import type { AttentionItem } from "@paperclipai/shared";
|
|
import { DEFAULT_DECISION_SHELF_DAYS } from "../services/decision-retention.js";
|
|
import {
|
|
getEmbeddedPostgresTestSupport,
|
|
startEmbeddedPostgresTestDatabase,
|
|
} from "./helpers/embedded-postgres.js";
|
|
import { errorHandler } from "../middleware/index.js";
|
|
import { decisionQueueRoutes } from "../routes/decision-queues.js";
|
|
import { decisionQueueService } from "../services/decision-queues.js";
|
|
|
|
const external = resolveExternalTestDatabaseUrl("PAPERCLIP_DECISION_QUEUES_TEST_DATABASE_URL");
|
|
const embeddedPostgresSupport = external ? { supported: true, reason: undefined } : await getEmbeddedPostgresTestSupport();
|
|
const describeEmbeddedPostgres = embeddedPostgresSupport.supported ? describe : describe.skip;
|
|
|
|
if (!embeddedPostgresSupport.supported) {
|
|
console.warn(
|
|
`Skipping embedded Postgres decision queue tests on this host: ${embeddedPostgresSupport.reason ?? "unsupported environment"}`,
|
|
);
|
|
}
|
|
|
|
describeEmbeddedPostgres("decision queue routes", () => {
|
|
let db!: ReturnType<typeof createDb>;
|
|
let tempDb: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>> | null = null;
|
|
|
|
beforeAll(async () => {
|
|
if (external) await applyPendingMigrations(external);
|
|
else tempDb = await startEmbeddedPostgresTestDatabase("paperclip-decision-queues-");
|
|
db = createDb(external ?? tempDb!.connectionString);
|
|
}, 30_000);
|
|
|
|
afterEach(async () => {
|
|
await db.delete(decisionTriageEvents);
|
|
await db.delete(decisionQueueItems);
|
|
await db.delete(decisionTriage);
|
|
await db.delete(decisionQueues);
|
|
await db.delete(activityLog);
|
|
await db.delete(issueThreadInteractions);
|
|
await db.delete(issueWorkProducts);
|
|
await db.delete(approvals);
|
|
await db.delete(issues);
|
|
await db.delete(agents);
|
|
await db.delete(companies);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await db?.$client.end({ timeout: 1 });
|
|
await tempDb?.cleanup();
|
|
});
|
|
|
|
async function seed() {
|
|
const companyId = randomUUID();
|
|
const agentId = randomUUID();
|
|
const issueId = randomUUID();
|
|
const interactionId = randomUUID();
|
|
const approvalId = randomUUID();
|
|
await db.insert(companies).values({
|
|
id: companyId,
|
|
name: "Decision Queue Co",
|
|
issuePrefix: "DQC",
|
|
});
|
|
await db.insert(agents).values({
|
|
id: agentId,
|
|
companyId,
|
|
name: "Prioritizer",
|
|
role: "engineer",
|
|
status: "idle",
|
|
adapterType: "codex_local",
|
|
adapterConfig: {},
|
|
runtimeConfig: {},
|
|
permissions: {},
|
|
});
|
|
await db.insert(issues).values({
|
|
id: issueId,
|
|
companyId,
|
|
identifier: "DQC-1",
|
|
title: "Review the rollout",
|
|
status: "in_review",
|
|
assigneeAgentId: agentId,
|
|
});
|
|
await db.insert(issueThreadInteractions).values({
|
|
id: interactionId,
|
|
companyId,
|
|
issueId,
|
|
kind: "ask_user_questions",
|
|
status: "pending",
|
|
payload: { version: 1, questions: [] } as never,
|
|
});
|
|
await db.insert(approvals).values({
|
|
id: approvalId,
|
|
companyId,
|
|
type: "request_board_approval",
|
|
status: "pending",
|
|
payload: {},
|
|
});
|
|
return { companyId, agentId, issueId, interactionId, approvalId };
|
|
}
|
|
|
|
function app(actor: Record<string, unknown>) {
|
|
const testApp = express();
|
|
testApp.use(express.json());
|
|
testApp.use((req, _res, next) => {
|
|
(req as any).actor = actor;
|
|
next();
|
|
});
|
|
testApp.use("/api", decisionQueueRoutes(db));
|
|
testApp.use(errorHandler);
|
|
return testApp;
|
|
}
|
|
|
|
function boardActor(companyId: string, userId = "board-user") {
|
|
return {
|
|
type: "board",
|
|
source: "local_implicit",
|
|
userId,
|
|
companyIds: [companyId],
|
|
isInstanceAdmin: false,
|
|
};
|
|
}
|
|
|
|
function agentActor(companyId: string, agentId: string) {
|
|
return {
|
|
type: "agent",
|
|
source: "agent_key",
|
|
companyId,
|
|
agentId,
|
|
keyId: null,
|
|
keyScope: { kind: "standard" },
|
|
runId: null,
|
|
};
|
|
}
|
|
|
|
it("creates idempotently, patches, lists by updated time, and audits queue mutations", async () => {
|
|
const { companyId } = await seed();
|
|
const board = boardActor(companyId);
|
|
const first = await request(app(board)).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "launches",
|
|
title: "Launches",
|
|
description: "Ship decisions",
|
|
}).expect(201);
|
|
const repeated = await request(app(board)).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "launches",
|
|
title: "Ignored duplicate title",
|
|
}).expect(200);
|
|
|
|
expect(repeated.body.id).toBe(first.body.id);
|
|
expect(repeated.body.title).toBe("Launches");
|
|
expect(repeated.body.itemCount).toBe(0);
|
|
|
|
const patched = await request(app(board))
|
|
.patch(`/api/companies/${companyId}/decision-queues/launches`)
|
|
.send({ title: "Launch desk", retentionDays: 45 })
|
|
.expect(200);
|
|
expect(patched.body).toMatchObject({ title: "Launch desk", retentionDays: 45 });
|
|
|
|
await request(app(board)).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "older",
|
|
title: "Older",
|
|
}).expect(201);
|
|
const listed = await request(app(board)).get(`/api/companies/${companyId}/decision-queues`).expect(200);
|
|
expect(listed.body.map((queue: { key: string }) => queue.key)).toEqual(["older", "launches"]);
|
|
const seedRules = await request(app(board))
|
|
.get(`/api/companies/${companyId}/decision-queue-seed-rules`)
|
|
.expect(200);
|
|
expect(seedRules.body.map((seed: { key: string }) => seed.key)).toEqual(["prs", "plans", "questions"]);
|
|
|
|
const events = await db.select().from(decisionTriageEvents)
|
|
.where(eq(decisionTriageEvents.queueId, first.body.id));
|
|
expect(events.map((event) => event.action)).toEqual(["queue.created", "queue.updated"]);
|
|
expect(events.every((event) => event.actorUserId === "board-user")).toBe(true);
|
|
});
|
|
|
|
it("adds and removes three attention source kinds and hides board-only membership from agents", async () => {
|
|
const { companyId, agentId, issueId, interactionId, approvalId } = await seed();
|
|
const board = boardActor(companyId);
|
|
await request(app(board)).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "triage",
|
|
title: "Triage",
|
|
}).expect(201);
|
|
|
|
for (const source of [
|
|
{ sourceKind: "approval", sourceId: approvalId },
|
|
{ sourceKind: "issue_thread_interaction", sourceId: interactionId },
|
|
{ sourceKind: "review", sourceId: issueId },
|
|
]) {
|
|
await request(app(board))
|
|
.post(`/api/companies/${companyId}/decision-queues/triage/items`)
|
|
.send(source)
|
|
.expect(201);
|
|
}
|
|
|
|
const items = await request(app(board))
|
|
.get(`/api/companies/${companyId}/decision-queues/triage/items`)
|
|
.expect(200);
|
|
expect(items.body).toHaveLength(3);
|
|
const repeated = await request(app(board)).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "triage",
|
|
title: "Ignored duplicate title",
|
|
}).expect(200);
|
|
expect(repeated.body.itemCount).toBe(3);
|
|
|
|
const agentList = await request(app(agentActor(companyId, agentId)))
|
|
.get(`/api/companies/${companyId}/decision-queues`)
|
|
.expect(200);
|
|
expect(agentList.body[0].itemCount).toBe(2);
|
|
const agentItems = await request(app(agentActor(companyId, agentId)))
|
|
.get(`/api/companies/${companyId}/decision-queues/triage/items`)
|
|
.expect(200);
|
|
expect(agentItems.body.map((item: { sourceKind: string }) => item.sourceKind).sort()).toEqual([
|
|
"issue_thread_interaction",
|
|
"review",
|
|
]);
|
|
await request(app(agentActor(companyId, agentId)))
|
|
.delete(`/api/companies/${companyId}/decision-queues/triage/items/approval/${approvalId}`)
|
|
.expect(404);
|
|
|
|
await request(app(board))
|
|
.delete(`/api/companies/${companyId}/decision-queues/triage/items/review/${issueId}`)
|
|
.send({ reason: "Handled in the release review" })
|
|
.expect(200);
|
|
expect(await db.select().from(decisionQueueItems).where(and(
|
|
eq(decisionQueueItems.companyId, companyId),
|
|
eq(decisionQueueItems.sourceKind, "review"),
|
|
))).toHaveLength(0);
|
|
const removalAudit = await db.select().from(activityLog).where(and(
|
|
eq(activityLog.companyId, companyId),
|
|
eq(activityLog.action, "decision_queue_item.removed"),
|
|
)).then((rows) => rows[0]);
|
|
expect(removalAudit?.details).toMatchObject({
|
|
sourceKind: "review",
|
|
sourceId: issueId,
|
|
reason: "Handled in the release review",
|
|
});
|
|
const removalEvent = await db.select().from(decisionTriageEvents).where(and(
|
|
eq(decisionTriageEvents.companyId, companyId),
|
|
eq(decisionTriageEvents.sourceKind, "review"),
|
|
eq(decisionTriageEvents.sourceId, issueId),
|
|
eq(decisionTriageEvents.action, "queue_item.removed"),
|
|
)).then((rows) => rows[0]);
|
|
expect(removalEvent?.details).toEqual({ reason: "Handled in the release review" });
|
|
|
|
await db.delete(approvals).where(eq(approvals.id, approvalId));
|
|
await request(app(board))
|
|
.delete(`/api/companies/${companyId}/decision-queues/triage/items/approval/${approvalId}`)
|
|
.expect(200);
|
|
expect(await db.select().from(decisionQueueItems).where(and(
|
|
eq(decisionQueueItems.companyId, companyId),
|
|
eq(decisionQueueItems.sourceKind, "approval"),
|
|
))).toHaveLength(0);
|
|
});
|
|
|
|
it("materializes data-backed starter queues from plan, question, and pull-request signals", async () => {
|
|
const { companyId, issueId, interactionId } = await seed();
|
|
await db.insert(issueWorkProducts).values({
|
|
companyId,
|
|
issueId,
|
|
type: "pull_request",
|
|
provider: "github",
|
|
title: "PR 42",
|
|
status: "open",
|
|
});
|
|
|
|
function attentionItem(input: {
|
|
sourceKind: AttentionItem["sourceKind"];
|
|
sourceId: string;
|
|
subjectKind: AttentionItem["subject"]["kind"];
|
|
metadata?: Record<string, unknown>;
|
|
issueId?: string;
|
|
}): AttentionItem {
|
|
return {
|
|
id: `${input.sourceKind}:${input.sourceId}`,
|
|
companyId,
|
|
sourceKind: input.sourceKind,
|
|
subject: {
|
|
kind: input.subjectKind,
|
|
id: input.sourceId,
|
|
companyId,
|
|
title: "Seed candidate",
|
|
identifier: null,
|
|
status: "pending",
|
|
href: null,
|
|
metadata: input.metadata,
|
|
},
|
|
whyNow: "test",
|
|
audience: { kind: "pool", userId: null, reason: "none" },
|
|
stale: null,
|
|
required: false,
|
|
changeFingerprint: "fixture.candidate",
|
|
closeModes: ["archive", "snooze"],
|
|
obligationKey: null,
|
|
obligationLeadId: null,
|
|
decisionVerbs: [],
|
|
inlineResolvable: true,
|
|
entryRule: "test",
|
|
exitRule: "test",
|
|
dedupKey: `${input.sourceKind}:${input.sourceId}`,
|
|
dismissalKey: `${input.sourceKind}:${input.sourceId}`,
|
|
dismissal: null,
|
|
severity: "medium",
|
|
rank: 1,
|
|
activityAt: new Date().toISOString(),
|
|
createdAt: new Date().toISOString(),
|
|
updatedAt: new Date().toISOString(),
|
|
relatedIssue: input.issueId ? {
|
|
kind: "issue",
|
|
id: input.issueId,
|
|
companyId,
|
|
title: "Issue",
|
|
identifier: "DQC-1",
|
|
status: "in_review",
|
|
href: null,
|
|
} : null,
|
|
project: null,
|
|
workspace: null,
|
|
expiresAt: null,
|
|
ruleKey: null,
|
|
originAgentName: null,
|
|
queues: [],
|
|
shelf: false,
|
|
retentionDays: DEFAULT_DECISION_SHELF_DAYS,
|
|
keep: false,
|
|
archivedAt: null,
|
|
retentionVersion: 0,
|
|
decideBy: null,
|
|
decideByAttribution: null,
|
|
snoozedUntil: null,
|
|
detail: null,
|
|
trainingExampleId: null,
|
|
};
|
|
}
|
|
|
|
const planId = randomUUID();
|
|
const candidates = [
|
|
attentionItem({ sourceKind: "review", sourceId: issueId, subjectKind: "issue", issueId }),
|
|
attentionItem({
|
|
sourceKind: "issue_thread_interaction",
|
|
sourceId: planId,
|
|
subjectKind: "interaction",
|
|
metadata: { kind: "request_confirmation", isPlanTarget: true, issueId },
|
|
issueId,
|
|
}),
|
|
attentionItem({
|
|
sourceKind: "issue_thread_interaction",
|
|
sourceId: interactionId,
|
|
subjectKind: "interaction",
|
|
metadata: { kind: "ask_user_questions", issueId },
|
|
issueId,
|
|
}),
|
|
];
|
|
await decisionQueueService(db).materializeSeededQueues(companyId, candidates);
|
|
|
|
const queues = await db.select().from(decisionQueues).where(eq(decisionQueues.companyId, companyId));
|
|
expect(queues.map((queue) => queue.key).sort()).toEqual(["plans", "prs", "questions"]);
|
|
expect(queues.every((queue) => queue.seedRulesEnabled && queue.seedRules.length === 1)).toBe(true);
|
|
const seededItems = await db.select({ queueId: decisionQueueItems.queueId })
|
|
.from(decisionQueueItems)
|
|
.where(eq(decisionQueueItems.companyId, companyId));
|
|
expect(seededItems).toHaveLength(5);
|
|
const prQueue = queues.find((queue) => queue.key === "prs")!;
|
|
expect(seededItems.filter((item) => item.queueId === prQueue.id)).toHaveLength(3);
|
|
|
|
const firstEvents = await db.select().from(decisionTriageEvents)
|
|
.where(eq(decisionTriageEvents.companyId, companyId));
|
|
const firstActivity = await db.select().from(activityLog).where(eq(activityLog.companyId, companyId));
|
|
const firstUpdatedAt = new Map(queues.map((queue) => [queue.key, queue.updatedAt.toISOString()]));
|
|
await decisionQueueService(db).materializeSeededQueues(companyId, candidates);
|
|
const unchangedQueues = await db.select().from(decisionQueues).where(eq(decisionQueues.companyId, companyId));
|
|
expect(unchangedQueues.every((queue) => queue.updatedAt.toISOString() === firstUpdatedAt.get(queue.key))).toBe(true);
|
|
expect(await db.select().from(decisionTriageEvents).where(eq(decisionTriageEvents.companyId, companyId)))
|
|
.toHaveLength(firstEvents.length);
|
|
expect(await db.select().from(activityLog).where(eq(activityLog.companyId, companyId)))
|
|
.toHaveLength(firstActivity.length);
|
|
|
|
const questionsQueue = queues.find((queue) => queue.key === "questions")!;
|
|
await db.update(decisionQueues).set({ seedRulesEnabled: false }).where(eq(decisionQueues.id, questionsQueue.id));
|
|
await decisionQueueService(db).materializeSeededQueues(companyId, [attentionItem({
|
|
sourceKind: "issue_thread_interaction",
|
|
sourceId: randomUUID(),
|
|
subjectKind: "interaction",
|
|
metadata: { kind: "ask_user_questions", issueId },
|
|
issueId,
|
|
})]);
|
|
expect(await db.select().from(decisionQueueItems).where(eq(decisionQueueItems.queueId, questionsQueue.id)))
|
|
.toHaveLength(1);
|
|
});
|
|
|
|
it("batches repeated seed checks and records only new memberships across concurrent reads", async () => {
|
|
const { companyId } = await seed();
|
|
const service = decisionQueueService(db);
|
|
const items = Array.from({ length: 501 }, () => ({ sourceKind: "issue_thread_interaction" as const,
|
|
sourceId: randomUUID(), issueId: null, interactionKind: "ask_user_questions" }));
|
|
await service.materializeSeedSources(companyId, items.slice(0, 2));
|
|
await Promise.all([
|
|
service.materializeSeedSources(companyId, [...items, items[0]!]),
|
|
service.materializeSeedSources(companyId, items),
|
|
]);
|
|
const memberships = await db.select().from(decisionQueueItems).where(eq(decisionQueueItems.companyId, companyId));
|
|
expect(new Set(memberships.map(item => item.sourceId))).toEqual(new Set(items.map(item => item.sourceId)));
|
|
expect(memberships).toHaveLength(items.length);
|
|
const events = await db.select().from(decisionTriageEvents).where(and(
|
|
eq(decisionTriageEvents.companyId, companyId), eq(decisionTriageEvents.action, "queue_item.seeded")));
|
|
const activity = await db.select().from(activityLog).where(and(
|
|
eq(activityLog.companyId, companyId), eq(activityLog.action, "decision_queue_item.seeded")));
|
|
expect(events).toHaveLength(items.length);
|
|
expect(activity).toHaveLength(items.length);
|
|
const queries: string[] = [];
|
|
const previousDebug = db.$client.options.debug;
|
|
try {
|
|
db.$client.options.debug = (_connection, text) => queries.push(text);
|
|
await service.materializeSeedSources(companyId, items);
|
|
} finally { db.$client.options.debug = previousDebug; }
|
|
expect(queries.filter(query => query.startsWith('insert into "decision_queue_items"')).length).toBeLessThan(10);
|
|
expect(await db.select().from(decisionTriageEvents).where(and(
|
|
eq(decisionTriageEvents.companyId, companyId), eq(decisionTriageEvents.action, "queue_item.seeded")))).toHaveLength(items.length);
|
|
});
|
|
|
|
it("records agent decide-by, preserves override history, and exposes the board override attribution", async () => {
|
|
const { companyId, agentId, issueId } = await seed();
|
|
const agent = agentActor(companyId, agentId);
|
|
const board = boardActor(companyId, "override-user");
|
|
|
|
const agentSet = await request(app(agent))
|
|
.put(`/api/companies/${companyId}/decision-triage/review/${issueId}`)
|
|
.send({ decideBy: "today", snoozedUntil: "2026-08-03T12:00:00.000Z" })
|
|
.expect(200);
|
|
expect(agentSet.body).toMatchObject({
|
|
decideBy: "today",
|
|
setByType: "agent",
|
|
setByAgentId: agentId,
|
|
version: 1,
|
|
});
|
|
|
|
const overridden = await request(app(board))
|
|
.put(`/api/companies/${companyId}/decision-triage/review/${issueId}`)
|
|
.send({ decideBy: "2026-08-08", snoozedUntil: null })
|
|
.expect(200);
|
|
expect(overridden.body).toMatchObject({
|
|
decideBy: "2026-08-08",
|
|
setByType: "user",
|
|
setByUserId: "override-user",
|
|
version: 2,
|
|
});
|
|
|
|
const events = await db.select().from(decisionTriageEvents).where(and(
|
|
eq(decisionTriageEvents.companyId, companyId),
|
|
eq(decisionTriageEvents.sourceKind, "review"),
|
|
eq(decisionTriageEvents.sourceId, issueId),
|
|
));
|
|
expect(events).toHaveLength(2);
|
|
expect(events[0]?.actorAgentId).toBe(agentId);
|
|
expect(events[1]?.actorUserId).toBe("override-user");
|
|
expect(events[1]?.details).toMatchObject({ previousDecideBy: "today", decideBy: "2026-08-08" });
|
|
|
|
const current = await db.select().from(decisionTriage).where(eq(decisionTriage.sourceId, issueId));
|
|
expect(current[0]?.setByUserId).toBe("override-user");
|
|
});
|
|
|
|
it("serializes concurrent partial triage updates without losing state or reusing a version", async () => {
|
|
const { companyId, issueId } = await seed();
|
|
const board = boardActor(companyId);
|
|
const [decideByResult, snoozeResult] = await Promise.all([
|
|
request(app(board))
|
|
.put(`/api/companies/${companyId}/decision-triage/review/${issueId}`)
|
|
.send({ decideBy: "today" }),
|
|
request(app(board))
|
|
.put(`/api/companies/${companyId}/decision-triage/review/${issueId}`)
|
|
.send({ snoozedUntil: "2026-08-03T12:00:00.000Z" }),
|
|
]);
|
|
|
|
expect(decideByResult.status).toBe(200);
|
|
expect(snoozeResult.status).toBe(200);
|
|
expect([decideByResult.body.version, snoozeResult.body.version].sort()).toEqual([1, 2]);
|
|
|
|
const current = await request(app(board))
|
|
.get(`/api/companies/${companyId}/decision-triage/review/${issueId}`)
|
|
.expect(200);
|
|
expect(current.body).toMatchObject({
|
|
decideBy: "today",
|
|
snoozedUntil: "2026-08-03T12:00:00.000Z",
|
|
version: 2,
|
|
});
|
|
const events = await db.select().from(decisionTriageEvents).where(and(
|
|
eq(decisionTriageEvents.companyId, companyId),
|
|
eq(decisionTriageEvents.sourceKind, "review"),
|
|
eq(decisionTriageEvents.sourceId, issueId),
|
|
));
|
|
expect(events.map((event) => event.details.version).sort()).toEqual([1, 2]);
|
|
});
|
|
|
|
it("rejects task-bridge JWTs and returns the same 404 for missing and unauthorized sources", async () => {
|
|
const { companyId, agentId, approvalId } = await seed();
|
|
const bridge = {
|
|
...agentActor(companyId, agentId),
|
|
source: "agent_jwt",
|
|
keyId: randomUUID(),
|
|
keyScope: { kind: "task_bridge", parentIssueId: randomUUID() },
|
|
};
|
|
await request(app(bridge)).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "blocked",
|
|
title: "Blocked",
|
|
}).expect(403);
|
|
const skillTest = {
|
|
...agentActor(companyId, agentId),
|
|
source: "agent_jwt",
|
|
keyScope: { kind: "skill_test", issueId: randomUUID() },
|
|
};
|
|
await request(app(skillTest)).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "skill-test-blocked",
|
|
title: "Skill test blocked",
|
|
}).expect(403);
|
|
|
|
await request(app(boardActor(companyId))).post(`/api/companies/${companyId}/decision-queues`).send({
|
|
key: "secure",
|
|
title: "Secure",
|
|
}).expect(201);
|
|
const standardAgent = app(agentActor(companyId, agentId));
|
|
const unauthorized = await request(standardAgent)
|
|
.post(`/api/companies/${companyId}/decision-queues/secure/items`)
|
|
.send({ sourceKind: "approval", sourceId: approvalId })
|
|
.expect(404);
|
|
const missing = await request(standardAgent)
|
|
.post(`/api/companies/${companyId}/decision-queues/secure/items`)
|
|
.send({ sourceKind: "approval", sourceId: randomUUID() })
|
|
.expect(404);
|
|
expect(unauthorized.body).toEqual(missing.body);
|
|
});
|
|
});
|