Files
PaperClipAI/server/src/services/attention-audience.test.ts
T
8818402e7a feat(inbox): route requests to people and record personal outcomes (#20959)
## 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>
2026-11-11 17:49:39 -08:00

267 lines
12 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
audienceMatchesView,
escalateStaleAudience,
resolveAttentionAudience,
type AttentionAudienceInput,
type IssueAudienceContext,
} from "./attention.js";
const EMPTY_CTX: IssueAudienceContext = {
conversationUserId: null,
assigneeUserId: null,
instigatorUserId: null,
};
function input(overrides: Partial<AttentionAudienceInput> = {}): AttentionAudienceInput {
return { issueId: "issue-1", ...overrides };
}
function ctx(overrides: Partial<IssueAudienceContext> = {}): IssueAudienceContext {
return { ...EMPTY_CTX, ...overrides };
}
describe("resolveAttentionAudience — audience steps (PAP-749 plan §1, Rule 2)", () => {
it("routes an explicit user addressee to that user (step 1)", () => {
expect(resolveAttentionAudience(input({ addresseeUserId: "scott" }), ctx({ instigatorUserId: "dotta" })))
.toEqual({ kind: "user", userId: "scott", reason: "addressee" });
});
it("routes an approval to its requester", () => {
expect(resolveAttentionAudience(input({ requestedByUserId: "scott" }), null))
.toEqual({ kind: "user", userId: "scott", reason: "requester" });
});
it("routes a blocker/recovery card to its named user owner", () => {
expect(resolveAttentionAudience(input({ ownerUserId: "scott" }), null))
.toEqual({ kind: "user", userId: "scott", reason: "assignee" });
});
it("routes a chat card to the conversation user (step 2)", () => {
expect(resolveAttentionAudience(input(), ctx({ conversationUserId: "scott" })))
.toEqual({ kind: "user", userId: "scott", reason: "conversation" });
});
it("routes a review to its current human participant (step 3)", () => {
expect(resolveAttentionAudience(input({ reviewParticipantUserId: "scott" }), ctx({ assigneeUserId: "dotta" })))
.toEqual({ kind: "user", userId: "scott", reason: "reviewer" });
});
it("routes to the human assignee when no participant is named (step 3)", () => {
expect(resolveAttentionAudience(input(), ctx({ assigneeUserId: "scott" })))
.toEqual({ kind: "user", userId: "scott", reason: "assignee" });
});
it("routes to the instigator of the originating human work (step 4)", () => {
expect(resolveAttentionAudience(input(), ctx({ instigatorUserId: "scott" })))
.toEqual({ kind: "user", userId: "scott", reason: "instigator" });
});
it("sends company-operations alerts to the Company view", () => {
expect(resolveAttentionAudience(input({ issueId: null, companyScoped: true }), null))
.toEqual({ kind: "company", userId: null, reason: "none" });
});
it("sends a card with no owner to the claimable Unassigned pool (step 6)", () => {
expect(resolveAttentionAudience(input(), EMPTY_CTX))
.toEqual({ kind: "pool", userId: null, reason: "none" });
});
});
describe("resolveAttentionAudience — precedence", () => {
it("an explicit addressee beats every chain signal", () => {
expect(resolveAttentionAudience(
input({ addresseeUserId: "scott", requestedByUserId: "dotta", reviewParticipantUserId: "riley" }),
ctx({ conversationUserId: "dotta", assigneeUserId: "riley", instigatorUserId: "dotta" }),
)).toMatchObject({ userId: "scott", reason: "addressee" });
});
it("the conversation user beats a reviewer, assignee, and instigator", () => {
expect(resolveAttentionAudience(
input({ reviewParticipantUserId: "riley" }),
ctx({ conversationUserId: "scott", assigneeUserId: "riley", instigatorUserId: "dotta" }),
)).toMatchObject({ userId: "scott", reason: "conversation" });
});
it("a reviewer beats the assignee and the instigator", () => {
expect(resolveAttentionAudience(
input({ reviewParticipantUserId: "scott" }),
ctx({ assigneeUserId: "riley", instigatorUserId: "dotta" }),
)).toMatchObject({ userId: "scott", reason: "reviewer" });
});
it("the assignee beats the instigator", () => {
expect(resolveAttentionAudience(
input(),
ctx({ assigneeUserId: "scott", instigatorUserId: "dotta" }),
)).toMatchObject({ userId: "scott", reason: "assignee" });
});
});
describe("resolveAttentionAudience — default decision owner (PAP-749 task 1d-lite, step 5)", () => {
it("routes an ownerless card to the company default decision owner", () => {
expect(resolveAttentionAudience(input(), EMPTY_CTX, { defaultDecisionOwnerUserId: "scott" }))
.toEqual({ kind: "user", userId: "scott", reason: "escalation_owner" });
});
it("falls back to Unassigned when no default decision owner is set", () => {
expect(resolveAttentionAudience(input(), EMPTY_CTX, { defaultDecisionOwnerUserId: null }))
.toEqual({ kind: "pool", userId: null, reason: "none" });
// Omitting the config entirely behaves the same as an unset owner.
expect(resolveAttentionAudience(input(), EMPTY_CTX))
.toEqual({ kind: "pool", userId: null, reason: "none" });
});
it("does not override an earlier chain step with the default owner", () => {
// A card that already resolves to a person keeps that person; the default
// owner only catches cards that would otherwise fall through to the pool.
expect(resolveAttentionAudience(
input({ addresseeUserId: "dotta" }),
ctx({ instigatorUserId: "riley" }),
{ defaultDecisionOwnerUserId: "scott" },
)).toEqual({ kind: "user", userId: "dotta", reason: "addressee" });
expect(resolveAttentionAudience(
input(),
ctx({ instigatorUserId: "riley" }),
{ defaultDecisionOwnerUserId: "scott" },
)).toEqual({ kind: "user", userId: "riley", reason: "instigator" });
});
it("never pulls a company-operations alert onto the default owner", () => {
expect(resolveAttentionAudience(
input({ issueId: null, companyScoped: true }),
null,
{ defaultDecisionOwnerUserId: "scott" },
)).toEqual({ kind: "company", userId: null, reason: "none" });
});
it("the default owner's card lands in that user's `mine` view, not Unassigned", () => {
const audience = resolveAttentionAudience(input(), EMPTY_CTX, { defaultDecisionOwnerUserId: "scott" });
expect(audienceMatchesView(audience, "mine", "scott")).toBe(true);
expect(audienceMatchesView(audience, "unassigned", "scott")).toBe(false);
expect(audienceMatchesView(audience, "company", "scott")).toBe(false);
});
});
describe("resolveAttentionAudience — project and budget owners (PAP-749 task 1d)", () => {
it("routes an ownerless card to its project's decision owner before the company default", () => {
expect(resolveAttentionAudience(input(), ctx({ projectOwnerUserId: "riley" }), { defaultDecisionOwnerUserId: "scott" }))
.toEqual({ kind: "user", userId: "riley", reason: "project_owner" });
});
it("keeps earlier chain steps ahead of the project owner", () => {
expect(resolveAttentionAudience(input(), ctx({ instigatorUserId: "dotta", projectOwnerUserId: "riley" })))
.toEqual({ kind: "user", userId: "dotta", reason: "instigator" });
});
it("falls back to the default decision owner, then Unassigned, without a project owner", () => {
expect(resolveAttentionAudience(input(), ctx({ projectOwnerUserId: null }), { defaultDecisionOwnerUserId: "scott" }))
.toEqual({ kind: "user", userId: "scott", reason: "escalation_owner" });
expect(resolveAttentionAudience(input(), ctx({ projectOwnerUserId: null })))
.toEqual({ kind: "pool", userId: null, reason: "none" });
});
it("gives a budget hard stop to the budget owner and keeps it in Company", () => {
const audience = resolveAttentionAudience(
input({ issueId: null, companyScoped: true, escalatesToBudgetOwner: true }),
null,
{ budgetOwnerUserId: "scott", defaultDecisionOwnerUserId: "dotta" },
);
expect(audience).toEqual({ kind: "user", userId: "scott", reason: "budget_owner", alsoInCompany: true });
expect(audienceMatchesView(audience, "mine", "scott")).toBe(true);
expect(audienceMatchesView(audience, "mine", "dotta")).toBe(false);
// Critical incidents always show in Company, for every operator (Rule 3).
expect(audienceMatchesView(audience, "company", "scott")).toBe(true);
expect(audienceMatchesView(audience, "company", "dotta")).toBe(true);
expect(audienceMatchesView(audience, "unassigned", "dotta")).toBe(false);
});
it("leaves a budget hard stop in Company only when no budget owner is set", () => {
expect(resolveAttentionAudience(
input({ issueId: null, companyScoped: true, escalatesToBudgetOwner: true }),
null,
{ defaultDecisionOwnerUserId: "dotta" },
)).toEqual({ kind: "company", userId: null, reason: "none" });
});
it("never gives other company alerts to the budget owner", () => {
expect(resolveAttentionAudience(
input({ issueId: null, companyScoped: true }),
null,
{ budgetOwnerUserId: "scott" },
)).toEqual({ kind: "company", userId: null, reason: "none" });
});
it("escalates a required card waiting 30 days to its project owner, then the default owner", () => {
const now = Date.parse("2026-10-30T00:00:00Z");
const since = new Date(now - 31 * 86_400_000);
const audience = { kind: "user" as const, userId: "dotta", reason: "requester" as const };
expect(escalateStaleAudience({
audience, since, required: true, now,
companyConfig: { defaultDecisionOwnerUserId: "scott" },
ctx: ctx({ projectOwnerUserId: "riley" }),
})).toEqual({ kind: "user", userId: "riley", reason: "project_owner" });
expect(escalateStaleAudience({
audience, since, required: true, now,
companyConfig: { defaultDecisionOwnerUserId: "scott" },
ctx: ctx(),
})).toEqual({ kind: "user", userId: "scott", reason: "escalation_owner" });
expect(escalateStaleAudience({ audience, since, required: true, now, ctx: ctx() }))
.toEqual({ kind: "company", userId: null, reason: "none" });
});
});
describe("resolveAttentionAudience — forbidden routing signals", () => {
// `responsibleUserId`, the routine creator, the connected account, the agent
// owner, and a manual routine starter are deliberately not inputs, so a card
// whose only human signal would be one of them resolves to Unassigned, never
// to a person (PAP-749 plan §1, Rule 2).
it("does not route a card that has no addressee/owner/participant/instigator", () => {
expect(resolveAttentionAudience(input(), EMPTY_CTX))
.toEqual({ kind: "pool", userId: null, reason: "none" });
});
it("ignores a falsy owner/addressee rather than inventing a user", () => {
expect(resolveAttentionAudience(
input({ addresseeUserId: null, requestedByUserId: null, ownerUserId: null }),
EMPTY_CTX,
)).toEqual({ kind: "pool", userId: null, reason: "none" });
});
});
describe("audienceMatchesView (PAP-749 plan §1, Rule 3)", () => {
const userAudience = { kind: "user" as const, userId: "scott", reason: "addressee" as const };
const otherUserAudience = { kind: "user" as const, userId: "dotta", reason: "instigator" as const };
const poolAudience = { kind: "pool" as const, userId: null, reason: "none" as const };
const companyAudience = { kind: "company" as const, userId: null, reason: "none" as const };
it("`all` keeps every audience (the pre-PAP-749 superset)", () => {
for (const audience of [userAudience, otherUserAudience, poolAudience, companyAudience]) {
expect(audienceMatchesView(audience, "all", "scott")).toBe(true);
}
});
it("`mine` keeps only the requesting user's cards", () => {
expect(audienceMatchesView(userAudience, "mine", "scott")).toBe(true);
expect(audienceMatchesView(otherUserAudience, "mine", "scott")).toBe(false);
expect(audienceMatchesView(poolAudience, "mine", "scott")).toBe(false);
expect(audienceMatchesView(companyAudience, "mine", "scott")).toBe(false);
});
it("`mine` keeps nothing when the requesting user is unknown", () => {
expect(audienceMatchesView(userAudience, "mine", null)).toBe(false);
});
it("`unassigned` keeps only pool cards", () => {
expect(audienceMatchesView(poolAudience, "unassigned", "scott")).toBe(true);
expect(audienceMatchesView(userAudience, "unassigned", "scott")).toBe(false);
expect(audienceMatchesView(companyAudience, "unassigned", "scott")).toBe(false);
});
it("`company` keeps only company-operations cards", () => {
expect(audienceMatchesView(companyAudience, "company", "scott")).toBe(true);
expect(audienceMatchesView(poolAudience, "company", "scott")).toBe(false);
expect(audienceMatchesView(userAudience, "company", "scott")).toBe(false);
});
});