mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 02:30:43 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The Mine inbox shows work that needs the current user. > - Failed-run rows used the latest run for every agent in the company. > - A failure from another user therefore appeared in Mine and its badge. > - Run list responses also omitted the responsible user needed to filter these rows. > - This pull request uses run ownership for personal failure routing. > - Users see their own failures and can still inspect company failures in All. ## Linked Issues or Issue Description **What happened?** An agent run started for one user failed. Its row and failure badge appeared in another user's Mine inbox. **Expected behavior** Mine and its badge include failed runs for the current responsible user. Other users' failures remain available in All and run details. **Steps to reproduce** 1. Use a company with two human users. 2. Create a failed or timed-out run attributed to the first user. 3. Open Mine as the second user. Before this fix, the failed run appears there and increases the badge. **Paperclip version or commit** Reproduced in regression tests on master at `24beb0057`. **Deployment mode** Authenticated deployment with multiple users. Tests also cover the local single-user board. Related prior work: #933 addressed inbox dismissal and badge consistency. No duplicate ownership fix was found. ## What Changed - Return `responsibleUserId` in normal and summary run lists. - Share one ownership rule across both inbox versions and client/server badges. - Select the latest run per agent before applying the ownership filter. This prevents old failures from resurfacing on shared agents. - Keep unattributed historical failures in the local board's Mine view. Hide them from authenticated users with no matching owner. - Keep company health alerts outside the personal badge, consistent with the client. - Document the routing contract and add page, badge, and database regression coverage. ## Verification - Red: the new badge cases failed with three company failures instead of one personal failure; eight Mine page cases failed across both inbox versions. - Green: 113 focused tests pass in `ui/src/lib/inbox.test.ts`, `ui/src/pages/Inbox.test.tsx`, `server/src/__tests__/heartbeat-list.test.ts`, and `server/src/__tests__/inbox-dismissals.test.ts`. - `pnpm check:token-gates` passes. - Agent calls on behalf of a user have two additional red-to-green API regressions. - Full `pnpm -r typecheck` and `pnpm build` pass. Server typecheck also passes after the agent-call fix. - All CI test shards and browser tests pass on `243bfa681`. The duplicate local `pnpm test:run` was stopped after the CI test lanes completed; it did not finish locally. ## Risks - Authenticated users no longer receive unattributed legacy failures in Mine. Those failures remain visible in All. - The server badge no longer counts company health alerts, matching the existing client badge. - No migration, run state, retry behavior, or company access rules change. ## Model Used - OpenAI GPT-6 through Codex, with reasoning, terminal execution, and browser tools. The exact deployment variant and context window size are not exposed in this session. ## 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 - [x] I have updated relevant documentation to reflect my changes - [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: Paperclip <noreply@paperclip.ing>
93 lines
3.5 KiB
TypeScript
93 lines
3.5 KiB
TypeScript
import { Router } from "express";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { and, eq } from "drizzle-orm";
|
|
import { inboxDismissals, joinRequests } from "@paperclipai/db";
|
|
import { sidebarBadgeService } from "../services/sidebar-badges.js";
|
|
import { accessService } from "../services/access.js";
|
|
import { collapseDuplicatePendingHumanJoinRequests } from "../lib/join-request-dedupe.js";
|
|
import { assertCompanyAccess } from "./authz.js";
|
|
|
|
function buildDismissedAtByKey(
|
|
dismissals: Array<{ itemKey: string; kind: string; dismissedAt: Date | string; snoozedUntil: Date | string | null }>,
|
|
): Map<string, number> {
|
|
const now = Date.now();
|
|
const entries: Array<[string, number]> = [];
|
|
for (const dismissal of dismissals) {
|
|
if (dismissal.kind === "snooze") {
|
|
const snoozedUntil = dismissal.snoozedUntil ? new Date(dismissal.snoozedUntil).getTime() : 0;
|
|
if (Number.isFinite(snoozedUntil) && snoozedUntil > now) entries.push([dismissal.itemKey, Number.MAX_SAFE_INTEGER]);
|
|
continue;
|
|
}
|
|
entries.push([dismissal.itemKey, new Date(dismissal.dismissedAt).getTime()]);
|
|
}
|
|
return new Map(entries);
|
|
}
|
|
|
|
export function sidebarBadgeRoutes(db: Db) {
|
|
const router = Router();
|
|
const svc = sidebarBadgeService(db);
|
|
const access = accessService(db);
|
|
|
|
router.get("/companies/:companyId/sidebar-badges", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
let canApproveJoins = false;
|
|
if (req.actor.type === "board") {
|
|
canApproveJoins =
|
|
req.actor.source === "local_implicit" ||
|
|
Boolean(req.actor.isInstanceAdmin) ||
|
|
(await access.canUser(companyId, req.actor.userId, "joins:approve"));
|
|
} else if (req.actor.type === "agent" && req.actor.agentId) {
|
|
canApproveJoins = await access.hasPermission(companyId, "agent", req.actor.agentId, "joins:approve");
|
|
}
|
|
|
|
const visibleJoinRequests = canApproveJoins
|
|
? collapseDuplicatePendingHumanJoinRequests(
|
|
await db
|
|
.select({
|
|
id: joinRequests.id,
|
|
requestType: joinRequests.requestType,
|
|
status: joinRequests.status,
|
|
requestingUserId: joinRequests.requestingUserId,
|
|
requestEmailSnapshot: joinRequests.requestEmailSnapshot,
|
|
updatedAt: joinRequests.updatedAt,
|
|
createdAt: joinRequests.createdAt,
|
|
})
|
|
.from(joinRequests)
|
|
.where(and(eq(joinRequests.companyId, companyId), eq(joinRequests.status, "pending_approval")))
|
|
).map(({ id, updatedAt, createdAt }) => ({
|
|
id,
|
|
updatedAt,
|
|
createdAt,
|
|
}))
|
|
: [];
|
|
|
|
const dismissedAtByKey =
|
|
req.actor.type === "board" && req.actor.userId
|
|
? await db
|
|
.select({
|
|
itemKey: inboxDismissals.itemKey,
|
|
kind: inboxDismissals.kind,
|
|
dismissedAt: inboxDismissals.dismissedAt,
|
|
snoozedUntil: inboxDismissals.snoozedUntil,
|
|
})
|
|
.from(inboxDismissals)
|
|
.where(and(eq(inboxDismissals.companyId, companyId), eq(inboxDismissals.userId, req.actor.userId)))
|
|
.then(buildDismissedAtByKey)
|
|
: new Map<string, number>();
|
|
|
|
// Company health alerts belong in All, not the personal inbox badge.
|
|
const badges = await svc.get(companyId, {
|
|
currentUserId: req.actor.type === "board"
|
|
? req.actor.userId ?? null
|
|
: req.actor.onBehalfOfUserId ?? null,
|
|
dismissals: dismissedAtByKey,
|
|
joinRequests: visibleJoinRequests,
|
|
});
|
|
|
|
res.json(badges);
|
|
});
|
|
|
|
return router;
|
|
}
|