mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-29 23:56:43 +01:00
fix(server): serialize interaction review verdicts
Lock the issue before accepting or rejecting review confirmations, reauthorize against the current policy, and cover concurrent policy tightening. Co-Authored-By: Codex <noreply@openai.com>
This commit is contained in:
1 parent
3526b82e2b
commit
edb8083538
4 files changed
+311
-54
No files matched your search
@@ -20,6 +20,7 @@ import type {
|
||||
CancelIssueThreadInteraction,
|
||||
CreateIssueThreadInteraction,
|
||||
InteractionResolverGovernance,
|
||||
IssueReviewPolicy,
|
||||
IssueThreadInteraction,
|
||||
IssueThreadInteractionKind,
|
||||
IssueThreadInteractionResolverPolicy,
|
||||
@@ -59,6 +60,10 @@ import { conflict, forbidden, notFound, unprocessable } from "../errors.js";
|
||||
import { getTelemetryClient } from "../telemetry.js";
|
||||
import { logActivity } from "./activity-log.js";
|
||||
import { evaluateAgentInvokabilityFromDb } from "./agent-invokability.js";
|
||||
import {
|
||||
assertIssueReviewVerdictActorAllowed,
|
||||
isIssueReviewVerdictInteraction,
|
||||
} from "./issue-review-policy.js";
|
||||
import { issueService, runWorkspaceIsFinalized } from "./issues.js";
|
||||
import {
|
||||
createPullRequestMergeStateResolver,
|
||||
@@ -286,8 +291,48 @@ type IssueResolutionContext = {
|
||||
status: string;
|
||||
assigneeAgentId: string | null;
|
||||
assigneeUserId: string | null;
|
||||
reviewPolicy: IssueReviewPolicy | null;
|
||||
createdByAgentId: string | null;
|
||||
createdByUserId: string | null;
|
||||
};
|
||||
|
||||
async function assertRequestConfirmationResolutionAllowedUnderLock(
|
||||
tx: Db,
|
||||
issue: IssueResolutionContext,
|
||||
interaction: IssueThreadInteractionRow,
|
||||
actor: InteractionActor,
|
||||
) {
|
||||
if (isTerminalIssueStatus(issue.status)) {
|
||||
throw conflict("Interaction is no longer actionable because the issue is closed");
|
||||
}
|
||||
|
||||
const isReviewVerdict = issue.status === "in_review"
|
||||
&& isRequestConfirmationLikeKind(interaction.kind)
|
||||
&& await isIssueReviewVerdictInteraction(tx, { issue, interaction });
|
||||
|
||||
if (!isReviewVerdict) {
|
||||
assertAgentResolutionAllowed(interaction, {
|
||||
...actor,
|
||||
reviewVerdictAuthorized: false,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (actor.agentId) assertAgentInteractionActorAllowed(interaction, actor);
|
||||
const verdictActor = actor.agentId
|
||||
? { type: "agent" as const, id: actor.agentId }
|
||||
: actor.userId
|
||||
? { type: "user" as const, id: actor.userId }
|
||||
: null;
|
||||
if (!verdictActor) {
|
||||
throw forbidden("A review verdict requires an authenticated agent or user");
|
||||
}
|
||||
await assertIssueReviewVerdictActorAllowed(tx, {
|
||||
issue,
|
||||
actor: verdictActor,
|
||||
});
|
||||
}
|
||||
|
||||
const REQUEST_CONFIRMATION_INTERACTION_KINDS = [
|
||||
"request_confirmation",
|
||||
"request_checkbox_confirmation",
|
||||
@@ -1330,17 +1375,62 @@ export function issueThreadInteractionService(db: Db, opts: IssueThreadInteracti
|
||||
return { interaction: expired, continuationIssue: null };
|
||||
}
|
||||
|
||||
const interaction = hydrateInteraction(args.current);
|
||||
const selectedOptionIds =
|
||||
interaction.kind === "request_checkbox_confirmation"
|
||||
const now = new Date();
|
||||
const result = await db.transaction(async (tx) => {
|
||||
// Lock the issue before claiming the interaction. Policy mutations and
|
||||
// review transitions use the same issue-row lock, so the authoritative
|
||||
// review policy and requester are stable through the verdict write.
|
||||
const issueContext = await tx
|
||||
.select({
|
||||
id: issues.id,
|
||||
companyId: issues.companyId,
|
||||
status: issues.status,
|
||||
assigneeAgentId: issues.assigneeAgentId,
|
||||
assigneeUserId: issues.assigneeUserId,
|
||||
reviewPolicy: issues.reviewPolicy,
|
||||
createdByAgentId: issues.createdByAgentId,
|
||||
createdByUserId: issues.createdByUserId,
|
||||
})
|
||||
.from(issues)
|
||||
.where(eq(issues.id, args.issue.id))
|
||||
.for("update")
|
||||
.then((rows: IssueResolutionContext[]) => rows[0] ?? null);
|
||||
|
||||
if (!issueContext || issueContext.companyId !== args.issue.companyId) {
|
||||
throw notFound("Issue not found");
|
||||
}
|
||||
|
||||
const lockedCurrent = await tx
|
||||
.select()
|
||||
.from(issueThreadInteractions)
|
||||
.where(eq(issueThreadInteractions.id, args.current.id))
|
||||
.for("update")
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (
|
||||
!lockedCurrent
|
||||
|| lockedCurrent.companyId !== args.issue.companyId
|
||||
|| lockedCurrent.issueId !== args.issue.id
|
||||
) {
|
||||
throw notFound("Interaction not found");
|
||||
}
|
||||
if (lockedCurrent.status !== "pending") {
|
||||
throw conflict("Interaction has already been resolved");
|
||||
}
|
||||
await assertRequestConfirmationResolutionAllowedUnderLock(
|
||||
tx as unknown as Db,
|
||||
issueContext,
|
||||
lockedCurrent,
|
||||
args.actor,
|
||||
);
|
||||
|
||||
const interaction = hydrateInteraction(lockedCurrent);
|
||||
const selectedOptionIds = interaction.kind === "request_checkbox_confirmation"
|
||||
? resolveSelectedCheckboxConfirmationOptions({
|
||||
interaction,
|
||||
selectedOptionIds: args.input.selectedOptionIds,
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const now = new Date();
|
||||
const result = await db.transaction(async (tx) => {
|
||||
const [updated] = await tx
|
||||
.update(issueThreadInteractions)
|
||||
.set({
|
||||
@@ -1357,7 +1447,7 @@ export function issueThreadInteractionService(db: Db, opts: IssueThreadInteracti
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(and(
|
||||
eq(issueThreadInteractions.id, args.current.id),
|
||||
eq(issueThreadInteractions.id, lockedCurrent.id),
|
||||
eq(issueThreadInteractions.status, "pending"),
|
||||
))
|
||||
.returning();
|
||||
@@ -1366,32 +1456,16 @@ export function issueThreadInteractionService(db: Db, opts: IssueThreadInteracti
|
||||
throw conflict("Interaction has already been resolved");
|
||||
}
|
||||
|
||||
const issueContext = await tx
|
||||
.select({
|
||||
id: issues.id,
|
||||
companyId: issues.companyId,
|
||||
status: issues.status,
|
||||
assigneeAgentId: issues.assigneeAgentId,
|
||||
assigneeUserId: issues.assigneeUserId,
|
||||
})
|
||||
.from(issues)
|
||||
.where(eq(issues.id, args.issue.id))
|
||||
.then((rows: IssueResolutionContext[]) => rows[0] ?? null);
|
||||
|
||||
if (!issueContext || issueContext.companyId !== args.issue.companyId) {
|
||||
throw notFound("Issue not found");
|
||||
}
|
||||
|
||||
let continuationIssue: IssueWakeTarget | null = null;
|
||||
if (shouldReturnAcceptedConfirmationToCreatorAgent({
|
||||
issue: issueContext,
|
||||
current: args.current,
|
||||
current: lockedCurrent,
|
||||
actor: args.actor,
|
||||
})) {
|
||||
const returnStatus = issueContext.status === "blocked" ? "blocked" : "todo";
|
||||
const returnedIssue = await issueService(db).update(args.issue.id, {
|
||||
status: returnStatus,
|
||||
assigneeAgentId: args.current.createdByAgentId,
|
||||
assigneeAgentId: lockedCurrent.createdByAgentId,
|
||||
assigneeUserId: null,
|
||||
actorAgentId: args.actor.agentId ?? null,
|
||||
actorUserId: args.actor.userId ?? null,
|
||||
@@ -1420,12 +1494,12 @@ export function issueThreadInteractionService(db: Db, opts: IssueThreadInteracti
|
||||
entityType: "issue",
|
||||
entityId: args.issue.id,
|
||||
details: {
|
||||
interactionId: args.current.id,
|
||||
interactionKind: args.current.kind,
|
||||
interactionId: lockedCurrent.id,
|
||||
interactionKind: lockedCurrent.kind,
|
||||
interactionStatus: "accepted",
|
||||
resolutionActorKind: "system",
|
||||
requestedResolverPolicy: args.current.requestedResolverPolicy,
|
||||
effectiveResolverPolicy: args.current.effectiveResolverPolicy,
|
||||
requestedResolverPolicy: lockedCurrent.requestedResolverPolicy,
|
||||
effectiveResolverPolicy: lockedCurrent.effectiveResolverPolicy,
|
||||
...(args.actor.resolutionDetails ?? {}),
|
||||
},
|
||||
});
|
||||
@@ -1461,31 +1535,77 @@ export function issueThreadInteractionService(db: Db, opts: IssueThreadInteracti
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const [updated] = await db
|
||||
.update(issueThreadInteractions)
|
||||
.set({
|
||||
status: "rejected",
|
||||
result: {
|
||||
version: 1,
|
||||
outcome: "rejected",
|
||||
reason: reason || null,
|
||||
},
|
||||
resolvedByAgentId: args.actor.agentId ?? null,
|
||||
resolvedByRunId: args.actor.runId ?? null,
|
||||
resolvedByUserId: args.actor.userId ?? null,
|
||||
resolvedAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(and(
|
||||
eq(issueThreadInteractions.id, args.current.id),
|
||||
eq(issueThreadInteractions.status, "pending"),
|
||||
))
|
||||
.returning();
|
||||
const updated = await db.transaction(async (tx) => {
|
||||
const issueContext = await tx
|
||||
.select({
|
||||
id: issues.id,
|
||||
companyId: issues.companyId,
|
||||
status: issues.status,
|
||||
assigneeAgentId: issues.assigneeAgentId,
|
||||
assigneeUserId: issues.assigneeUserId,
|
||||
reviewPolicy: issues.reviewPolicy,
|
||||
createdByAgentId: issues.createdByAgentId,
|
||||
createdByUserId: issues.createdByUserId,
|
||||
})
|
||||
.from(issues)
|
||||
.where(eq(issues.id, args.issue.id))
|
||||
.for("update")
|
||||
.then((rows: IssueResolutionContext[]) => rows[0] ?? null);
|
||||
if (!issueContext || issueContext.companyId !== args.issue.companyId) {
|
||||
throw notFound("Issue not found");
|
||||
}
|
||||
|
||||
const lockedCurrent = await tx
|
||||
.select()
|
||||
.from(issueThreadInteractions)
|
||||
.where(eq(issueThreadInteractions.id, args.current.id))
|
||||
.for("update")
|
||||
.then((rows) => rows[0] ?? null);
|
||||
if (
|
||||
!lockedCurrent
|
||||
|| lockedCurrent.companyId !== args.issue.companyId
|
||||
|| lockedCurrent.issueId !== args.issue.id
|
||||
) {
|
||||
throw notFound("Interaction not found");
|
||||
}
|
||||
if (lockedCurrent.status !== "pending") {
|
||||
throw conflict("Interaction has already been resolved");
|
||||
}
|
||||
await assertRequestConfirmationResolutionAllowedUnderLock(
|
||||
tx as unknown as Db,
|
||||
issueContext,
|
||||
lockedCurrent,
|
||||
args.actor,
|
||||
);
|
||||
|
||||
const [resolved] = await tx
|
||||
.update(issueThreadInteractions)
|
||||
.set({
|
||||
status: "rejected",
|
||||
result: {
|
||||
version: 1,
|
||||
outcome: "rejected",
|
||||
reason: reason || null,
|
||||
},
|
||||
resolvedByAgentId: args.actor.agentId ?? null,
|
||||
resolvedByRunId: args.actor.runId ?? null,
|
||||
resolvedByUserId: args.actor.userId ?? null,
|
||||
resolvedAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(and(
|
||||
eq(issueThreadInteractions.id, lockedCurrent.id),
|
||||
eq(issueThreadInteractions.status, "pending"),
|
||||
))
|
||||
.returning();
|
||||
|
||||
if (!resolved) {
|
||||
throw conflict("Interaction has already been resolved");
|
||||
}
|
||||
await touchIssue(tx, args.issue.id);
|
||||
return resolved;
|
||||
});
|
||||
|
||||
if (!updated) {
|
||||
throw conflict("Interaction has already been resolved");
|
||||
}
|
||||
await touchIssue(db, args.issue.id);
|
||||
const rejected = hydrateInteraction(updated);
|
||||
await emitInteractionResolvedTelemetry(db, rejected);
|
||||
return rejected;
|
||||
|
||||
Reference in new issue
Block a user