Files
PaperClipAI/ui/src/lib/interaction-resolution-error.ts
T
DottaandPaperclip 10d0555189 fix(interactions): authorize resolvers consistently (#11376)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work.
> - Issue interactions give agents and people a structured decision
record.
> - Resolver routes used different authorization rules.
> - Some routes blocked valid agents, including task watchdogs with
normal issue access.
> - The API did not show who could resolve a pending interaction.
> - This pull request gives every interaction kind one resolver policy
evaluator.
> - The benefit is a clear decision path with consistent governance and
company isolation.

## Linked Issues or Issue Description

Fixes: #8087

Refs: #7403

Related PR: #11082 proposes board-only confirmation rules. This change
keeps human-only review as an explicit policy.

**What happened?**

Agents could create issue interactions. Some resolver routes still
required board access.

This left valid agent confirmations pending. Task watchdogs could see
the same problem without board identity.

**Expected behavior**

Every interaction kind must use one resolver policy contract.

The contract must support `anyone`, `not_creator`, and `human_only`. It
must also apply all normal governance controls.

**Steps to reproduce**

1. Create a `request_confirmation` interaction as an agent.
2. Resolve it with another authorized agent.
3. Observe the board-only denial.

**Paperclip version or commit**

The problem exists on `master` before this change.

**Deployment mode**

Local development with `pnpm dev`.

## What Changed

- Add canonical policies for `anyone`, `not_creator`, and `human_only`.
- Use one server evaluator for every interaction kind.
- Apply named addressees, company limits, review rules, and task
watchdog scope.
- Charge cross-issue resolutions to the existing per-run action limit.
- Return the effective resolver audience in attention and interaction
data.
- Show the audience, governance choices, and denial reasons in the board
UI.
- Add telemetry, API documents, product documents, and regression
fixtures.
- Add migration provenance for safe legacy behavior.
- Make migration `0218` safe for complete replays and partial prior
runs.

## Product Rules

- An interaction records a response. It does not grant authority for the
next action.
- `anyone` lets any authorized issue participant respond.
- `not_creator` requires a responder other than the interaction creator.
- `human_only` requires an authorized person.
- A named addressee, company policy, or governed action can narrow the
audience.
- These controls cannot widen the audience.
- A task watchdog uses the same rules as an ordinary agent.
- A task watchdog does not receive board authority.
- An agent resolution on another issue uses the shared cross-issue
action limit.
- Legacy pending interactions keep their earlier restrictions.
- The UI shows the effective audience and a permanent denial reason.

## Verification

- `pnpm --filter @paperclipai/db check:migrations`
- `pnpm --filter @paperclipai/db typecheck`
- `pnpm exec vitest run
packages/db/src/issue-thread-interaction-resolver-policy-migration.test.ts`
- The focused PostgreSQL test applies migration `0218` twice.
- The test also completes a partial prior run and preserves existing
provenance.
- The latest GitHub head has 29 successful checks.
- The opt-in Storybook visual check skipped as expected.
- Greptile reports 5/5 with no open comments.

## Risks

- New interaction writes use `anyone` by default.
- Callers must select `not_creator` or `human_only` when they need
stricter review.
- Legacy pending interactions keep the old creator and human
restrictions.
- Migration `0218` fills only missing provenance fields during recovery.
- Cross-issue resolutions can reach the existing action limit.
- The shared evaluator affects every interaction kind.
- Route, service, database, shared contract, and UI tests cover these
rules.

> This work matches the Agent Reviews and Approvals direction in
`ROADMAP.md`. It does not duplicate a planned item.

## Model Used

OpenAI Codex, GPT-5. The runtime does not expose the exact deployment ID
or context window.

The agent used reasoning, repository tools, shell commands, and test
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 linked public issues or described the issue with the
required labels
- [x] I have not referenced internal Paperclip issues or links
- [x] My branch name describes the change and contains no internal
ticket id
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation
- [x] I have considered and documented the risks
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open comments
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: Paperclip <noreply@paperclip.ing>
2026-08-16 13:46:50 -05:00

164 lines
6.0 KiB
TypeScript

/**
* Plain-language copy for a *failed* issue-thread interaction resolution
* (PAP-17287).
*
* The resolution routes deny with a specific reason — human-only, creator
* excluded, addressee mismatch, a governed action, an already-resolved card.
* Collapsing all of that to `Try again` tells an operator to repeat an action
* that will never succeed, so this module keeps the server's explanation and,
* for an audience denial, follows it with who *can* respond.
*
* Presentation only. Authority is the server's: nothing here decides whether an
* action is permitted, it only explains the answer the server already gave.
*/
import type { InteractionAudienceDescription } from "./interaction-audience";
/**
* Denials that mean "you are outside this card's resolver audience". Mirrors the
* codes the server's audience evaluator can return
* (`server/src/services/issue-thread-interaction-resolution.ts`), plus the
* scope denial raised before it.
*/
export const INTERACTION_AUDIENCE_DENIAL_CODES = [
"interaction_human_only",
"interaction_creator_excluded",
"interaction_addressee_mismatch",
"interaction_governed_action_denied",
"interaction_run_attribution_required",
"interaction_scope_denied",
] as const;
/**
* Denials that are permanent for a different reason: the card moved on. Retrying
* cannot help, so these lose the retry prompt too.
*/
const INTERACTION_SETTLED_CODES = [
"interaction_not_found",
"interaction_already_resolved",
"interaction_superseded",
"interaction_stale_target",
"interaction_issue_closed",
] as const;
export type InteractionResolutionFailureKind = "audience_denied" | "settled" | "transient";
export interface InteractionResolutionFailure {
kind: InteractionResolutionFailureKind;
/** Copy for the inline error region. Never invites a retry that cannot work. */
message: string;
/** Server-provided denial code, when the response carried one. */
code: string | null;
}
function record(value: unknown): Record<string, unknown> | null {
return value && typeof value === "object" && !Array.isArray(value)
? value as Record<string, unknown>
: null;
}
/** The server's `code`, from either the top-level field or the details object. */
export function interactionResolutionErrorCode(error: unknown): string | null {
const body = record(record(error)?.body);
if (!body) return null;
if (typeof body.code === "string" && body.code) return body.code;
const details = record(body.details);
return typeof details?.code === "string" && details.code ? details.code : null;
}
function errorStatus(error: unknown): number | null {
const status = record(error)?.status;
return typeof status === "number" ? status : null;
}
/** The server's human-readable reason, preserved verbatim apart from punctuation. */
function serverReason(error: unknown): string | null {
const body = record(record(error)?.body);
const fromBody = typeof body?.error === "string" ? body.error.trim() : "";
const fromError = error instanceof Error ? error.message.trim() : "";
const reason = fromBody || fromError;
if (!reason) return null;
// The API writes reasons as bare clauses ("This interaction is human-only").
return /[.!?]$/.test(reason) ? reason : `${reason}.`;
}
export function isInteractionAudienceDenial(error: unknown): boolean {
const code = interactionResolutionErrorCode(error);
if (code) return (INTERACTION_AUDIENCE_DENIAL_CODES as readonly string[]).includes(code);
// A 403 with no code is still an authorization refusal, not a hiccup.
return errorStatus(error) === 403;
}
/** The audience of the card being resolved, as far as this client knows it. */
export type InteractionResolutionAudience = Pick<
InteractionAudienceDescription,
"shortSummary" | "isOpen"
>;
/**
* Classify a rejected resolution and produce the copy to show inline.
*
* `audience` is the effective audience of the card being resolved; when the
* failure is an audience denial *and* that audience is narrower than the open
* default, its short form is appended so the reader learns who can respond
* instead of being told to retry.
*/
export function describeInteractionResolutionFailure(
error: unknown,
audience?: InteractionResolutionAudience | null,
): InteractionResolutionFailure {
const code = interactionResolutionErrorCode(error);
const reason = serverReason(error);
if (isInteractionAudienceDenial(error)) {
// Only a *narrowed* audience has a responder worth naming. Appending the
// open-default clause to a refusal produces copy that refutes itself —
// "You are not in this card's resolver audience. Anyone can respond." —
// which happens whenever the snapshot the client holds is wider than the
// policy the server just enforced (PAP-17289).
const responder = audience && !audience.isOpen && audience.shortSummary
? `${audience.shortSummary}.`
: null;
// Without a code the server has told us only that this is forbidden, not
// *why*. Restate the status; do not invent a resolver-audience cause it
// never claimed.
const coded = code !== null
&& (INTERACTION_AUDIENCE_DENIAL_CODES as readonly string[]).includes(code);
return {
kind: "audience_denied",
code,
message: [
reason
?? (coded
? "You are not in this card's resolver audience."
: "You do not have permission to respond to this card."),
responder,
]
.filter(Boolean)
.join(" "),
};
}
if (code && (INTERACTION_SETTLED_CODES as readonly string[]).includes(code)) {
return {
kind: "settled",
code,
message: reason ?? "This request is no longer waiting for a decision.",
};
}
return {
kind: "transient",
code,
message: reason ? `${reason} Try again.` : "Couldn't submit. Try again.",
};
}
/** Convenience for the many call sites that only need the sentence. */
export function interactionResolutionErrorMessage(
error: unknown,
audience?: InteractionResolutionAudience | null,
): string {
return describeInteractionResolutionFailure(error, audience).message;
}