mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 17:14:54 +01:00
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - People talk to their agents in chat, and the agent replies in plain
text
> - In a full chat-eval cohort, 47 of 158 passing replies showed raw
UUIDs, error codes, field keys, tool names or server paths
> - Users must not see internal jargon or raw IDs, so these replies are
a product defect, even when the facts are correct
> - Agents copy these terms from API error messages and saved fields;
the chat directive did not tell them to translate
> - This pull request fixes the sources: the chat directive, and API
denial messages that put IDs and policy codes in the human message
> - The benefit is plain replies for every model, while machine codes
stay available in separate fields
## Linked Issues or Issue Description
**What happened?**
Chat replies quote internal names and raw IDs. Examples: run, handoff,
revision, interaction and trigger UUIDs in prose;
`native_session_retry_exhausted`, `board_access_required`,
`review_policy_denied`, `local-board`, `enableCases`; a raw user ID
inside the inbox denial message.
**Expected behavior**
Replies name records the way the person sees them (task identifier,
title or name, with a link) and explain codes in plain words. IDs stay
only in link targets, in commands the person must run, or when the
person asks for an ID.
**Steps to reproduce**
Run the chat-features OpenCode cohort (for example `cases.close-case`,
`routines.webhook-trigger`, `inbox-search.archive-without-grant`) and
read the agent replies.
**Paperclip version or commit**
`master` at f30005440b.
**Agent adapter(s) involved**
Not adapter-specific (core bug). Seen with the Paperclip runner and
OpenCode.
**Deployment mode**
Local dev server (embedded PGlite) and the CI chat-eval runner.
**Installation method**
Source checkout (`pnpm dev`).
## What Changed
- `AGENT_CHAT_DIRECTIVE`: add one rule. Name records by their
identifier, title or name and link them. Do not write raw IDs, internal
codes, field keys, tool names, process names or server paths, even in
inline code. The rule has exceptions for link targets, commands the
person runs, and IDs the person asks for. The rejection rule now asks
for the platform's reason "in plain words".
- Authorization denials (inbox management, membership, responsible user)
no longer put user or agent IDs in the explanation. The `reason` and
`code` values do not change.
- Review-policy denials explain the rule in plain words. The
`review_policy_denied` code and the `policy` and `allowedActor` fields
do not change.
## Verification
- Red/green: `server/src/__tests__/agent-conversations.test.ts`
(directive rule, and it reaches the chat task prompt),
`server/src/__tests__/authorization-service.test.ts` (inbox denials
carry no IDs), `server/src/__tests__/issue-review-policy.test.ts` and
`server/src/services/issue-thread-interaction-resolution.test.ts` (plain
review messages). Each new assertion failed before the fix.
- `pnpm verify` and the server typecheck pass locally.
- A paid OpenCode run on 8 affected chat cases is in progress; results
will be added here.
## Risks
- Low risk. Only human-readable messages and prompt text change. No
client matches the old message text; codes and machine fields stay the
same.
- The directive grows by one paragraph. A model can still copy an ID;
the rule reduces this but cannot prevent it.
## Model Used
- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used in Claude Code
with tool use and code execution.
## 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>
148 lines
5.1 KiB
TypeScript
148 lines
5.1 KiB
TypeScript
import { and, desc, eq, sql } from "drizzle-orm";
|
|
import { activityLog, type Db } from "@paperclipai/db";
|
|
import type { IssueReviewPolicy } from "@paperclipai/shared";
|
|
import { forbidden } from "../errors.js";
|
|
|
|
export interface IssueReviewVerdictActor {
|
|
type: "agent" | "user";
|
|
id: string;
|
|
}
|
|
|
|
export interface IssueReviewRequester extends IssueReviewVerdictActor {
|
|
reviewInteractionId: string | null;
|
|
}
|
|
|
|
interface ReviewPolicyIssue {
|
|
id: string;
|
|
companyId: string;
|
|
reviewPolicy?: IssueReviewPolicy | null;
|
|
createdByAgentId?: string | null;
|
|
createdByUserId?: string | null;
|
|
}
|
|
|
|
export async function resolveIssueReviewRequester(
|
|
db: Db,
|
|
issue: ReviewPolicyIssue,
|
|
): Promise<IssueReviewRequester | null> {
|
|
const transition = await db
|
|
.select({
|
|
actorType: activityLog.actorType,
|
|
actorId: activityLog.actorId,
|
|
details: activityLog.details,
|
|
})
|
|
.from(activityLog)
|
|
.where(and(
|
|
eq(activityLog.companyId, issue.companyId),
|
|
eq(activityLog.entityType, "issue"),
|
|
eq(activityLog.entityId, issue.id),
|
|
eq(activityLog.action, "issue.updated"),
|
|
sql`(
|
|
(
|
|
${activityLog.details} ->> 'status' = 'in_review'
|
|
AND ${activityLog.details} -> '_previous' ->> 'status' IS NOT NULL
|
|
AND ${activityLog.details} -> '_previous' ->> 'status' <> 'in_review'
|
|
)
|
|
OR
|
|
(
|
|
${activityLog.details} -> 'changes' -> 'status' ->> 'to' = 'in_review'
|
|
AND ${activityLog.details} -> 'changes' -> 'status' ->> 'from' IS NOT NULL
|
|
AND ${activityLog.details} -> 'changes' -> 'status' ->> 'from' <> 'in_review'
|
|
)
|
|
)`,
|
|
))
|
|
.orderBy(desc(activityLog.createdAt), desc(activityLog.id))
|
|
.limit(1)
|
|
.then((rows) => rows[0] ?? null);
|
|
|
|
if (transition?.actorType === "agent" || transition?.actorType === "user") {
|
|
const reviewInteractionId = typeof transition.details?.reviewInteractionId === "string"
|
|
? transition.details.reviewInteractionId
|
|
: null;
|
|
return { type: transition.actorType, id: transition.actorId, reviewInteractionId };
|
|
}
|
|
if (issue.createdByAgentId && !issue.createdByUserId) {
|
|
return { type: "agent", id: issue.createdByAgentId, reviewInteractionId: null };
|
|
}
|
|
if (issue.createdByUserId && !issue.createdByAgentId) {
|
|
return { type: "user", id: issue.createdByUserId, reviewInteractionId: null };
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export async function isIssueReviewVerdictInteraction(
|
|
db: Db,
|
|
input: {
|
|
issue: ReviewPolicyIssue;
|
|
interaction: {
|
|
id: string;
|
|
createdByAgentId?: string | null;
|
|
createdByUserId?: string | null;
|
|
};
|
|
},
|
|
): Promise<boolean> {
|
|
const requester = await resolveIssueReviewRequester(db, input.issue);
|
|
if (!requester) return false;
|
|
if (requester.reviewInteractionId && requester.reviewInteractionId !== input.interaction.id) return false;
|
|
// Older review transitions did not persist the interaction binding. In that
|
|
// case, an unattributed confirmation is ambiguous and must fail closed.
|
|
// Confirmations attributed to an unrelated writer remain independently
|
|
// resolvable, while requester-created confirmations inherit the issue policy.
|
|
if (!requester.reviewInteractionId
|
|
&& !input.interaction.createdByAgentId
|
|
&& !input.interaction.createdByUserId) {
|
|
return true;
|
|
}
|
|
return requester.type === "agent"
|
|
? input.interaction.createdByAgentId === requester.id
|
|
: input.interaction.createdByUserId === requester.id;
|
|
}
|
|
|
|
export async function assertIssueReviewVerdictActorAllowed(
|
|
db: Db,
|
|
input: {
|
|
issue: ReviewPolicyIssue;
|
|
actor: IssueReviewVerdictActor;
|
|
reviewPolicy?: IssueReviewPolicy | null;
|
|
},
|
|
): Promise<void> {
|
|
const policy = input.reviewPolicy ?? input.issue.reviewPolicy ?? "anyone";
|
|
if (policy === "anyone") return;
|
|
|
|
if (policy === "human_only") {
|
|
if (input.actor.type === "user") return;
|
|
throw forbidden(
|
|
"Only a signed-in person can approve or reject this review; an agent cannot submit the verdict.",
|
|
{
|
|
code: "review_policy_denied",
|
|
policy,
|
|
allowedActor: "authenticated_user_with_issue_write_access",
|
|
remediation: "Ask a person with access to this task to approve or reject it in Paperclip.",
|
|
},
|
|
);
|
|
}
|
|
|
|
const requester = await resolveIssueReviewRequester(db, input.issue);
|
|
if (!requester) {
|
|
throw forbidden(
|
|
"Someone other than whoever sent this task for review must decide it, and Paperclip could not tell who sent it.",
|
|
{
|
|
code: "review_policy_denied",
|
|
policy,
|
|
allowedActor: "writer_other_than_review_requester",
|
|
remediation: "Move the task out of review and back into review so Paperclip records who sent it, then have someone else submit the verdict.",
|
|
},
|
|
);
|
|
}
|
|
if (requester.type !== input.actor.type || requester.id !== input.actor.id) return;
|
|
|
|
throw forbidden(
|
|
"Whoever sent this task for review cannot also approve or reject it; someone else must submit the verdict.",
|
|
{
|
|
code: "review_policy_denied",
|
|
policy,
|
|
allowedActor: "writer_other_than_review_requester",
|
|
remediation: "Have someone else with access to this task submit the verdict.",
|
|
},
|
|
);
|
|
}
|