mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-12 14:19:00 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Agents can submit complaints and suggestions as commentary, and
people with commentary access read them on the Commentary page
> - When a person asks an agent in chat what agents have been
complaining about, the agent cannot read commentary and gets "Board
access required"
> - That response does not say where a person can see it, so the agent
can name the feed but cannot link it
> - Product rule: an agent cannot read other agents' commentary unless
it holds `commentary:view`, which is off by default; it may read its own
> - This pull request implements that rule and adds a remediation to the
denial naming the human page for the agent's own company
> - The benefit is a clear permission boundary and a working link for
the person instead of a dead end
## Linked Issues or Issue Description
**What happened?**
In the `activity.agent-feedback` chat eval, the agent correctly said it
could not read agent commentary and that only the person could open it,
but it gave no link, because the denial named no page.
**Expected behavior**
An agent denied a commentary read learns which page a person with
commentary access can open.
**Steps to reproduce**
1. As an agent, call `GET /api/companies/{companyId}/agent-commentary`.
2. Before this change, the 403 body is only `{ "error": "Board access
required" }`.
## What Changed
- `server/src/routes/agent-commentary.ts`:
- An agent with the `commentary:view` grant reads all commentary its
task access allows. The grant is not among the default agent grants.
- Without the grant, an agent may read its own entries: `?agentId=<its
own id>` on the list, or its own entry by ID.
- Without the grant, listing all commentary, filtering by another agent,
or reading another agent's entry returns 403 "Board access required".
The response's `remediation` names `/<issuePrefix>/activity/commentary`
for the agent's own company.
- A request for another company still fails on company access first,
with no page path.
- Board reads are unchanged: `commentary:view` for the user, plus task
read conditions.
- `agent-commentary.integration.test.ts`: one test covers the full-list
and other-agent denials with the page path, own-list and own-entry
reads, the cross-company denial, the grant allowing a full list, and
that `commentary:view` is not a default agent grant.
## Verification
- `npx vitest run src/__tests__/agent-commentary.integration.test.ts` in
`server`: 21 tests passed.
- The new test fails against the previous route.
- Negative control: with the previous route, the new test fails.
- Related commentary tests: `agent-commentary-reader`,
`agent-commentary-helper`, `commentary-permission-migration` passed.
`agent-commentary-skills` fails the same way with and without this
change (a missing skill file in a temporary directory locally).
- The chat eval rerun with this change is recorded in #19614.
## Risks
- Agents can now read their own commentary when they ask for it
explicitly; before, every agent read was denied.
- The denial's status code and message are unchanged; a `remediation`
string is added for same-company agents.
- There is no UI yet for granting `commentary:view` to an agent.
- An agent learns the company's issue prefix, which it already sees in
task identifiers.
> 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
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, with tool use and code
execution in Claude Code.
## 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
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
🤖 Generated with [Claude Code](https://claude.com/claude-code)
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
87 lines
4.5 KiB
TypeScript
87 lines
4.5 KiB
TypeScript
import { Router, type Request } from "express";
|
|
import { companies, type Db } from "@paperclipai/db";
|
|
import { eq } from "drizzle-orm";
|
|
import { agentCommentaryQuerySchema, isUuidLike } from "@paperclipai/shared";
|
|
import { badRequest, forbidden, notFound } from "../errors.js";
|
|
import { submitAgentCommentary } from "../services/agent-commentary.js";
|
|
import { agentCommentaryReader } from "../services/agent-commentary-reader.js";
|
|
import { issueReadSqlCondition } from "../services/authorization.js";
|
|
import { accessService } from "../services/access.js";
|
|
import { assertBoard, assertCompanyAccess } from "./authz.js";
|
|
|
|
export function agentCommentaryRoutes(db: Db) {
|
|
const router = Router();
|
|
const reader = agentCommentaryReader(db);
|
|
const access = accessService(db);
|
|
|
|
// Agents can submit commentary. They read only their own entries unless they
|
|
// hold commentary:view, which no agent has by default. A denial names the
|
|
// human page so the agent can send the person somewhere useful.
|
|
async function denyAgentRead(companyId: string): Promise<never> {
|
|
const [company] = await db.select({ issuePrefix: companies.issuePrefix }).from(companies).where(eq(companies.id, companyId)).limit(1);
|
|
throw forbidden("Board access required", company ? {
|
|
remediation: `Agents read only their own commentary unless granted commentary:view. People with commentary access can read it on the Commentary page; give them this link: [Commentary](/${company.issuePrefix}/activity/commentary).`,
|
|
} : undefined);
|
|
}
|
|
|
|
async function agentCanReadAll(req: Request, companyId: string) {
|
|
return req.actor.type === "agent" && !!req.actor.agentId
|
|
&& await access.hasPermission(companyId, "agent", req.actor.agentId, "commentary:view");
|
|
}
|
|
|
|
async function authorizeBoardRead(req: Request, companyId: string) {
|
|
assertBoard(req);
|
|
if (req.actor.source !== "local_implicit" && !req.actor.isInstanceAdmin
|
|
&& (!req.actor.userId || !await access.canUser(companyId, req.actor.userId, "commentary:view"))) {
|
|
throw forbidden("Missing permission: commentary:view");
|
|
}
|
|
return issueReadSqlCondition(db, req.actor);
|
|
}
|
|
|
|
router.get("/companies/:companyId/agent-commentary", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
const query = agentCommentaryQuerySchema.safeParse(req.query);
|
|
if (!query.success) throw badRequest("Invalid agent commentary filters", query.error.issues);
|
|
if (req.actor.type === "agent") {
|
|
// Without the grant, only an explicit request for its own entries is allowed.
|
|
if (!await agentCanReadAll(req, companyId) && query.data.agentId !== req.actor.agentId) await denyAgentRead(companyId);
|
|
res.json(await reader.list(companyId, query.data, await issueReadSqlCondition(db, req.actor)));
|
|
return;
|
|
}
|
|
if (req.actor.type !== "board") await denyAgentRead(companyId);
|
|
res.json(await reader.list(companyId, query.data, await authorizeBoardRead(req, companyId)));
|
|
});
|
|
|
|
router.get("/companies/:companyId/agent-commentary/:id", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
const id = req.params.id as string;
|
|
if (req.actor.type === "agent") {
|
|
const entry = isUuidLike(id) ? await reader.get(companyId, id, await issueReadSqlCondition(db, req.actor)) : null;
|
|
if (!await agentCanReadAll(req, companyId) && entry?.agent.id !== req.actor.agentId) await denyAgentRead(companyId);
|
|
if (!entry) throw notFound("Commentary not found");
|
|
res.json(entry);
|
|
return;
|
|
}
|
|
if (req.actor.type !== "board") await denyAgentRead(companyId);
|
|
const readCondition = await authorizeBoardRead(req, companyId);
|
|
if (!isUuidLike(id)) throw notFound("Commentary not found");
|
|
const entry = await reader.get(companyId, id, readCondition);
|
|
if (!entry) throw notFound("Commentary not found");
|
|
res.json(entry);
|
|
});
|
|
router.post("/companies/:companyId/agent-commentary", async (req, res) => {
|
|
const companyId = req.params.companyId as string;
|
|
assertCompanyAccess(req, companyId);
|
|
if (req.actor.type !== "agent" || !req.actor.agentId || !req.actor.runId) {
|
|
throw forbidden("Feedback requires an authenticated agent run");
|
|
}
|
|
const result = await submitAgentCommentary(db, {
|
|
companyId, agentId: req.actor.agentId, runId: req.actor.runId, agentApiKeyId: req.actor.keyId,
|
|
}, req.body);
|
|
res.status(result.replayed ? 200 : 201).json(result);
|
|
});
|
|
return router;
|
|
}
|