mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-06 15:52:22 +01:00
Merge agent approval preparation recovery into sleep-08-decision-archive-delivery
Propagate the verified agent-origin preparation and dispatch-claim correction while preserving this PR boundary. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
commit
76a4a0b360
4 files changed
+308
-200
No files matched your search
+9
-8
@@ -3,7 +3,7 @@ import { toolActionRequests, toolInvocations, type Db } from "@paperclipai/db";
|
||||
import type { ToolRedactedValueSummary } from "@paperclipai/shared";
|
||||
import { notifyToolReviewChange } from "./tool-review-work-notifications.js";
|
||||
|
||||
export type ToolTestApprovalClaim = { actionRequestId: string; invocationId: string; claimedAt: Date };
|
||||
export type ToolApprovalClaim = { actionRequestId: string; invocationId: string; claimedAt: Date };
|
||||
export type ToolTestOutcome = {
|
||||
status: "succeeded" | "failed" | "timed_out" | "rate_limited";
|
||||
result?: ToolRedactedValueSummary;
|
||||
@@ -11,7 +11,7 @@ export type ToolTestOutcome = {
|
||||
errorMessage?: string;
|
||||
};
|
||||
|
||||
export async function claimToolTestApproval(db: Db, actionRequestId: string, invocationId: string): Promise<ToolTestApprovalClaim | null> {
|
||||
export async function claimToolTestApproval(db: Db, actionRequestId: string, invocationId: string): Promise<ToolApprovalClaim | null> {
|
||||
return db.transaction(async tx => {
|
||||
const [invocation] = await tx.select().from(toolInvocations)
|
||||
.where(eq(toolInvocations.id, invocationId)).for("update");
|
||||
@@ -37,7 +37,7 @@ export async function claimToolTestApproval(db: Db, actionRequestId: string, inv
|
||||
}
|
||||
|
||||
/** Return only this still-undispatched claim to its existing approval queue. */
|
||||
export async function releaseToolTestApproval(db: Db, claim: ToolTestApprovalClaim) {
|
||||
export async function releaseToolApproval(db: Db, claim: ToolApprovalClaim) {
|
||||
return db.transaction(async tx => {
|
||||
const [invocation] = await tx.select().from(toolInvocations)
|
||||
.where(eq(toolInvocations.id, claim.invocationId)).for("update");
|
||||
@@ -54,7 +54,7 @@ export async function releaseToolTestApproval(db: Db, claim: ToolTestApprovalCla
|
||||
});
|
||||
}
|
||||
|
||||
export async function startToolTestInvocation(db: Db, invocationId: string, claim?: ToolTestApprovalClaim) {
|
||||
export async function startToolInvocation(db: Db, invocationId: string, claim?: ToolApprovalClaim) {
|
||||
const now = new Date();
|
||||
if (!claim) {
|
||||
await db.update(toolInvocations).set({ status: "executing", startedAt: now, updatedAt: now })
|
||||
@@ -62,19 +62,20 @@ export async function startToolTestInvocation(db: Db, invocationId: string, clai
|
||||
return true;
|
||||
}
|
||||
return db.transaction(async tx => {
|
||||
const [invocation] = await tx.select({ status: toolInvocations.status }).from(toolInvocations)
|
||||
const [invocation] = await tx.select().from(toolInvocations)
|
||||
.where(eq(toolInvocations.id, invocationId)).for("update");
|
||||
const [request] = await tx.select().from(toolActionRequests)
|
||||
.where(eq(toolActionRequests.id, claim.actionRequestId)).for("update");
|
||||
if (invocation?.status !== "awaiting_approval" || request?.status !== "executing" ||
|
||||
if (invocationId !== claim.invocationId || invocation?.status !== "awaiting_approval" || invocation.startedAt !== null ||
|
||||
request?.status !== "executing" || request.invocationId !== invocationId || request.companyId !== invocation.companyId ||
|
||||
request.updatedAt.getTime() !== claim.claimedAt.getTime()) return false;
|
||||
await tx.update(toolInvocations).set({ status: "executing", startedAt: now, updatedAt: now })
|
||||
await tx.update(toolInvocations).set({ status: "executing", approvalState: "approved", startedAt: now, updatedAt: now })
|
||||
.where(eq(toolInvocations.id, invocationId));
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
export async function persistToolTestOutcome(db: Db, invocationId: string, outcome: ToolTestOutcome, claim?: ToolTestApprovalClaim) {
|
||||
export async function persistToolTestOutcome(db: Db, invocationId: string, outcome: ToolTestOutcome, claim?: ToolApprovalClaim) {
|
||||
const now = new Date();
|
||||
const values = { status: outcome.status, ...(outcome.result ? {
|
||||
resultHash: outcome.result.sha256 ?? null, resultSummary: outcome.result,
|
||||
+154
-185
@@ -1,6 +1,6 @@
|
||||
import { toolReviewProjectionService } from "./tool-review-projection.js";
|
||||
import { toolReviewInterruptionService } from "./tool-review-interruption.js";
|
||||
import { claimToolTestApproval, releaseToolTestApproval, persistToolTestOutcome, startToolTestInvocation, type ToolTestApprovalClaim } from "./tool-test-approval.js";
|
||||
import { claimToolTestApproval, releaseToolApproval, persistToolTestOutcome, startToolInvocation, type ToolApprovalClaim } from "./tool-approval-execution.js";
|
||||
import { approvedToolReviewCompanies, prepareApprovedToolReview, recoverApprovedToolReviews } from "./tool-review-recovery.js";
|
||||
import { toolReviewExpiryService } from "./tool-review-expiry.js";
|
||||
import { notifyToolReviewChange, writeToolReviewChange } from "./tool-review-work-notifications.js";
|
||||
@@ -7424,7 +7424,7 @@ export function createToolGatewayService(
|
||||
matchedPolicyIds: string[];
|
||||
timeoutMs?: number;
|
||||
signal?: AbortSignal;
|
||||
approvedClaim?: ToolTestApprovalClaim;
|
||||
approvedClaim?: ToolApprovalClaim;
|
||||
}): Promise<
|
||||
| { decision: "allowed"; invocationId: string; result: unknown }
|
||||
| {
|
||||
@@ -7433,7 +7433,7 @@ export function createToolGatewayService(
|
||||
error: { message: string; reasonCode: string };
|
||||
}
|
||||
> {
|
||||
if (!(await startToolTestInvocation(db, args.invocationId, args.approvedClaim))) {
|
||||
if (!(await startToolInvocation(db, args.invocationId, args.approvedClaim))) {
|
||||
throw new ToolGatewayHttpError(409, "Tool action execution claim is no longer current", "action_already_consumed");
|
||||
}
|
||||
await writeAudit({
|
||||
@@ -7630,6 +7630,12 @@ export function createToolGatewayService(
|
||||
|
||||
}
|
||||
|
||||
function isApprovalPreparationRejection(error: unknown) {
|
||||
return error instanceof ToolContentValidationError ||
|
||||
error instanceof ToolGatewayHttpError && error.status >= 400 && error.status < 500 &&
|
||||
![408, 425, 429].includes(error.status);
|
||||
}
|
||||
|
||||
async function runApprovedTestInvocation(
|
||||
invocation: typeof toolInvocations.$inferSelect,
|
||||
signedPayload: NonNullable<ReturnType<typeof readSignedToolArgumentsPayload>>,
|
||||
@@ -7671,13 +7677,10 @@ export function createToolGatewayService(
|
||||
companyId: invocation.companyId, agentId, userId, argumentsSummary, reasonCode: "approval_granted",
|
||||
approvedClaim: claim, matchedPolicyIds: invocation.matchedPolicyIds ?? [] };
|
||||
} catch (err) {
|
||||
const rejected = err instanceof ToolContentValidationError ||
|
||||
err instanceof ToolGatewayHttpError && err.status >= 400 && err.status < 500 &&
|
||||
![408, 425, 429].includes(err.status);
|
||||
if (!rejected) {
|
||||
if (!isApprovalPreparationRejection(err)) {
|
||||
// Preparation has not entered runTestToolInvocation. Preserve approval
|
||||
// through dependency failures; the claim owner still verifies no dispatch.
|
||||
await releaseToolTestApproval(db, claim);
|
||||
await releaseToolApproval(db, claim);
|
||||
throw err;
|
||||
}
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
@@ -8032,81 +8035,67 @@ export function createToolGatewayService(
|
||||
);
|
||||
}
|
||||
|
||||
// Terminal-issue expiry revokes pending/approved requests, but a claim that
|
||||
// committed just before the issue closed slips past that revocation. Recheck
|
||||
// the issue after winning the claim so a governed action never runs external
|
||||
// side effects for an issue that is already done or cancelled.
|
||||
const issue = await assertIssueOpenForApprovedAction({
|
||||
claimed,
|
||||
invocation,
|
||||
});
|
||||
|
||||
const signedPayload = readSignedToolArgumentsPayload({
|
||||
signedArguments: claimed.signedArguments,
|
||||
invocationId: invocation.id,
|
||||
toolName: invocation.toolName,
|
||||
signingSecret: options.toolActionSigningSecret,
|
||||
});
|
||||
if (!signedPayload) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"Approved tool action arguments signature is invalid",
|
||||
"signed_arguments_invalid",
|
||||
);
|
||||
await markApprovedActionFailed({
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
claimUpdatedAt: claimed.updatedAt,
|
||||
expectedInvocationStatus: "awaiting_approval",
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
if (signedPayload.executionOnApprove !== true) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"This approval predates execute-on-approve and must remain inert",
|
||||
"legacy_approved_action_inert",
|
||||
);
|
||||
await markApprovedActionFailed({
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
claimUpdatedAt: claimed.updatedAt,
|
||||
expectedInvocationStatus: "awaiting_approval",
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
|
||||
const session: ToolGatewaySession = {
|
||||
id: `approved-action:${claimed.id}`,
|
||||
token: "",
|
||||
companyId: invocation.companyId,
|
||||
agentId: invocation.agentId,
|
||||
runId: invocation.runId,
|
||||
issueId: invocation.issueId,
|
||||
projectId: issue?.projectId ?? null,
|
||||
gatewayId: invocation.gatewayId,
|
||||
gatewayPublicId: invocation.gatewayPublicId,
|
||||
gatewayTokenId: invocation.gatewayTokenId,
|
||||
actorType: "agent",
|
||||
actorId: invocation.agentId,
|
||||
createdAt: new Date(),
|
||||
expiresAt: new Date(Date.now() + DEFAULT_SESSION_TTL_MS),
|
||||
const claim: ToolApprovalClaim = {
|
||||
actionRequestId: claimed.id, invocationId: invocation.id, claimedAt: claimed.updatedAt,
|
||||
};
|
||||
let tool: ToolGatewayDescriptor;
|
||||
let liveApprovalSnapshot: Awaited<
|
||||
ReturnType<typeof connectedRemoteApprovalSnapshot>
|
||||
>;
|
||||
try {
|
||||
const agentId = invocation.agentId;
|
||||
async function prepareExecution() {
|
||||
// Terminal-issue expiry revokes pending/approved requests, but a claim that
|
||||
// committed just before the issue closed slips past that revocation. Recheck
|
||||
// the issue after winning the claim so a governed action never runs external
|
||||
// side effects for an issue that is already done or cancelled.
|
||||
const issue = await assertIssueOpenForApprovedAction({
|
||||
claimed,
|
||||
invocation,
|
||||
});
|
||||
|
||||
const signedPayload = readSignedToolArgumentsPayload({
|
||||
signedArguments: claimed.signedArguments,
|
||||
invocationId: invocation.id,
|
||||
toolName: invocation.toolName,
|
||||
signingSecret: options.toolActionSigningSecret,
|
||||
});
|
||||
if (!signedPayload) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"Approved tool action arguments signature is invalid",
|
||||
"signed_arguments_invalid",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
if (signedPayload.executionOnApprove !== true) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"This approval predates execute-on-approve and must remain inert",
|
||||
"legacy_approved_action_inert",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
|
||||
const session: ToolGatewaySession = {
|
||||
id: `approved-action:${claimed.id}`,
|
||||
token: "",
|
||||
companyId: invocation.companyId,
|
||||
agentId,
|
||||
runId: invocation.runId,
|
||||
issueId: invocation.issueId,
|
||||
projectId: issue?.projectId ?? null,
|
||||
gatewayId: invocation.gatewayId,
|
||||
gatewayPublicId: invocation.gatewayPublicId,
|
||||
gatewayTokenId: invocation.gatewayTokenId,
|
||||
actorType: "agent",
|
||||
actorId: agentId,
|
||||
createdAt: new Date(),
|
||||
expiresAt: new Date(Date.now() + DEFAULT_SESSION_TTL_MS),
|
||||
};
|
||||
await restoreApprovedActionIdentity(
|
||||
session,
|
||||
signedPayload.identityContextId,
|
||||
);
|
||||
session.approvedInvocationId = invocation.id;
|
||||
session.approvedSlackInvocationId = invocation.id;
|
||||
tool = await findToolForSession(session, invocation.toolName);
|
||||
liveApprovalSnapshot = await connectedRemoteApprovalSnapshot(
|
||||
const tool = await findToolForSession(session, invocation.toolName);
|
||||
const liveApprovalSnapshot = await connectedRemoteApprovalSnapshot(
|
||||
session,
|
||||
tool,
|
||||
);
|
||||
@@ -8128,121 +8117,101 @@ export function createToolGatewayService(
|
||||
currentAccess.explanation,
|
||||
currentAccess.reasonCode,
|
||||
);
|
||||
} catch (error) {
|
||||
await markApprovedActionFailed({
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
claimUpdatedAt: claimed.updatedAt,
|
||||
expectedInvocationStatus: "awaiting_approval",
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
if (
|
||||
!approvalSnapshotsMatch(
|
||||
signedPayload.approvalSnapshot,
|
||||
liveApprovalSnapshot,
|
||||
)
|
||||
) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"Approved tool action target changed after review",
|
||||
"approved_tool_target_changed",
|
||||
);
|
||||
await markApprovedActionFailed({
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
claimUpdatedAt: claimed.updatedAt,
|
||||
expectedInvocationStatus: "awaiting_approval",
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
const parameters = signedPayload.arguments;
|
||||
const canonicalArguments = canonicalToolArguments(parameters);
|
||||
if (
|
||||
![
|
||||
toolRequestIdentityHash(parameters, options.toolActionSigningSecret),
|
||||
// Historical approvals used the redacted preview hash. Their signature
|
||||
// still binds the complete canonical arguments checked below.
|
||||
summarizeToolValue(parameters).sha256,
|
||||
].includes(claimed.canonicalArgumentsHash) ||
|
||||
!verifyToolArgumentsSignature({
|
||||
signedArguments: claimed.signedArguments,
|
||||
invocationId: invocation.id,
|
||||
toolName: invocation.toolName,
|
||||
canonicalArguments,
|
||||
approvalSnapshot: signedPayload.approvalSnapshot,
|
||||
executionOnApprove: true,
|
||||
identityContextId: signedPayload.identityContextId,
|
||||
signingSecret: options.toolActionSigningSecret,
|
||||
})
|
||||
) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"Approved tool action arguments do not match reviewed hash",
|
||||
"signed_arguments_mismatch",
|
||||
);
|
||||
await markApprovedActionFailed({
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
claimUpdatedAt: claimed.updatedAt,
|
||||
expectedInvocationStatus: "awaiting_approval",
|
||||
error,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
let managedArgumentsRemainCurrent: boolean;
|
||||
try {
|
||||
managedArgumentsRemainCurrent =
|
||||
if (
|
||||
!approvalSnapshotsMatch(
|
||||
signedPayload.approvalSnapshot,
|
||||
liveApprovalSnapshot,
|
||||
)
|
||||
) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"Approved tool action target changed after review",
|
||||
"approved_tool_target_changed",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
const parameters = signedPayload.arguments;
|
||||
const canonicalArguments = canonicalToolArguments(parameters);
|
||||
if (
|
||||
![
|
||||
toolRequestIdentityHash(parameters, options.toolActionSigningSecret),
|
||||
// Historical approvals used the redacted preview hash. Their signature
|
||||
// still binds the complete canonical arguments checked below.
|
||||
summarizeToolValue(parameters).sha256,
|
||||
].includes(claimed.canonicalArgumentsHash) ||
|
||||
!verifyToolArgumentsSignature({
|
||||
signedArguments: claimed.signedArguments,
|
||||
invocationId: invocation.id,
|
||||
toolName: invocation.toolName,
|
||||
canonicalArguments,
|
||||
approvalSnapshot: signedPayload.approvalSnapshot,
|
||||
executionOnApprove: true,
|
||||
identityContextId: signedPayload.identityContextId,
|
||||
signingSecret: options.toolActionSigningSecret,
|
||||
})
|
||||
) {
|
||||
const error = new ToolGatewayHttpError(
|
||||
409,
|
||||
"Approved tool action arguments do not match reviewed hash",
|
||||
"signed_arguments_mismatch",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
const managedArgumentsRemainCurrent =
|
||||
await approvedManagedArgumentsRemainCurrent(session, tool, parameters);
|
||||
if (!managedArgumentsRemainCurrent) {
|
||||
throw await expireApprovedActionForManagedArgumentDrift({
|
||||
companyId: claimed.companyId,
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
toolName: invocation.toolName,
|
||||
ownsExecutingClaim: true,
|
||||
});
|
||||
}
|
||||
|
||||
const argumentsSummary = (await validateToolContent({
|
||||
value: parameters,
|
||||
direction: "arguments",
|
||||
sensitiveMode: "redact",
|
||||
promptInjectionMode: "ignore",
|
||||
})).summary;
|
||||
// Final recheck at the last DB write before dispatch: tool and snapshot
|
||||
// resolution above involve network calls, so re-verify the issue is still
|
||||
// open now that only the provider call remains. A close that commits after
|
||||
// this read has raced an execution that was approved, claimed, and verified
|
||||
// while the issue was open; that instant is irreducible for an external
|
||||
// side effect gated by DB state (holding a DB lock across a remote provider
|
||||
// call is not an option), and the accepted linearization is that the
|
||||
// execution wins — its result still lands on the expired card via
|
||||
// reflectToolActionInteractionLifecycle.
|
||||
await assertIssueOpenForApprovedAction({ claimed, invocation });
|
||||
return { session, tool, parameters, argumentsSummary };
|
||||
}
|
||||
let prepared: Awaited<ReturnType<typeof prepareExecution>>;
|
||||
try {
|
||||
prepared = await prepareExecution();
|
||||
} catch (error) {
|
||||
await markApprovedActionFailed({
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
claimUpdatedAt: claimed.updatedAt,
|
||||
expectedInvocationStatus: "awaiting_approval",
|
||||
error,
|
||||
});
|
||||
if (isApprovalPreparationRejection(error)) {
|
||||
await markApprovedActionFailed({
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
claimUpdatedAt: claimed.updatedAt,
|
||||
expectedInvocationStatus: "awaiting_approval",
|
||||
error,
|
||||
});
|
||||
} else {
|
||||
await releaseToolApproval(db, claim);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
if (!managedArgumentsRemainCurrent) {
|
||||
throw await expireApprovedActionForManagedArgumentDrift({
|
||||
companyId: claimed.companyId,
|
||||
actionRequestId: claimed.id,
|
||||
invocationId: invocation.id,
|
||||
toolName: invocation.toolName,
|
||||
ownsExecutingClaim: true,
|
||||
});
|
||||
}
|
||||
|
||||
const argumentsSummary = (await validateToolContent({
|
||||
value: parameters,
|
||||
direction: "arguments",
|
||||
sensitiveMode: "redact",
|
||||
promptInjectionMode: "ignore",
|
||||
})).summary;
|
||||
// Final recheck at the last DB write before dispatch: tool and snapshot
|
||||
// resolution above involve network calls, so re-verify the issue is still
|
||||
// open now that only the provider call remains. A close that commits after
|
||||
// this read has raced an execution that was approved, claimed, and verified
|
||||
// while the issue was open; that instant is irreducible for an external
|
||||
// side effect gated by DB state (holding a DB lock across a remote provider
|
||||
// call is not an option), and the accepted linearization is that the
|
||||
// execution wins — its result still lands on the expired card via
|
||||
// reflectToolActionInteractionLifecycle.
|
||||
await assertIssueOpenForApprovedAction({ claimed, invocation });
|
||||
const { session, tool, parameters, argumentsSummary } = prepared;
|
||||
|
||||
const startedAt = Date.now();
|
||||
await db
|
||||
.update(toolInvocations)
|
||||
.set({
|
||||
status: "executing",
|
||||
approvalState: "approved",
|
||||
startedAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
})
|
||||
.where(eq(toolInvocations.id, invocation.id));
|
||||
// Keep dispatch and outcome persistence outside the preparation retry
|
||||
// boundary. Only the still-current claim may record the provider start.
|
||||
if (!(await startToolInvocation(db, invocation.id, claim))) {
|
||||
throw new ToolGatewayHttpError(409, "Tool action execution claim is no longer current", "action_already_consumed");
|
||||
}
|
||||
await reflectToolActionInteractionLifecycle({
|
||||
actionRequestId: claimed.id,
|
||||
status: "executing",
|
||||
|
||||
Reference in new issue
Block a user