mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-04 04:29:01 +01:00
Address review findings on PR #23621: - Enforce quarantine redaction (body, title, changeSummary) on non-Muse list_document_revisions (including historical low-trust revisions), get_task_context acceptedPlan/sourcePlanApproval, and document titles on list_documents/read_document. - Introduce isPositivelyLowTrustReaderForIssue so unknown or agent-mismatched reader runs fail closed (redact) instead of failing open as low-trust readers. - Normalize createdByRunId in documentService.upsertIssueDocument before resolving sourceTrust so stale run IDs on standard-trust writes do not falsely quarantine. - Resolve and persist sourceTrust in documentService.restoreIssueDocumentRevision when an agent restores a revision (including restoring a low-trust historical revision). - Add regression tests in server/src/__tests__/source-trust.test.ts.
208 lines
6.4 KiB
TypeScript
208 lines
6.4 KiB
TypeScript
import { and, eq } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { agents, heartbeatRuns, projects } from "@paperclipai/db";
|
|
import {
|
|
LOW_TRUST_REVIEW_PRESET,
|
|
type SourceTrustMetadata,
|
|
} from "@paperclipai/shared";
|
|
import { forbidden } from "../errors.js";
|
|
import { readObject } from "../lib/objects.js";
|
|
import { resolveCoreTrustPreset } from "./trust-preset-resolver.js";
|
|
|
|
export const LOW_TRUST_QUARANTINED_BODY =
|
|
"[Quarantined low-trust output omitted from higher-trust agent context. A trusted reviewer can inspect and promote a sanitized artifact.]";
|
|
|
|
export type SourceTrustActor = {
|
|
actorType: "agent" | "user";
|
|
actorId: string;
|
|
agentId: string | null;
|
|
runId: string | null;
|
|
};
|
|
|
|
export type SourceTrustIssueContext = {
|
|
id: string;
|
|
companyId: string;
|
|
projectId?: string | null;
|
|
executionPolicy?: unknown;
|
|
};
|
|
|
|
export function isLowTrustQuarantined(sourceTrust: SourceTrustMetadata | null | undefined): boolean {
|
|
return sourceTrust?.preset === LOW_TRUST_REVIEW_PRESET && sourceTrust.disposition === "quarantined";
|
|
}
|
|
|
|
export function redactQuarantinedBodyForHigherTrust<T extends { body?: string | null; sourceTrust?: SourceTrustMetadata | null }>(
|
|
value: T,
|
|
): T {
|
|
if (!isLowTrustQuarantined(value.sourceTrust)) return value;
|
|
return {
|
|
...value,
|
|
body: LOW_TRUST_QUARANTINED_BODY,
|
|
} as T;
|
|
}
|
|
|
|
export function sanitizeQuarantinedCommentForHigherTrust<
|
|
T extends {
|
|
body: string;
|
|
presentation?: unknown;
|
|
metadata?: unknown;
|
|
sourceTrust?: SourceTrustMetadata | null;
|
|
},
|
|
>(comment: T): T {
|
|
if (!isLowTrustQuarantined(comment.sourceTrust)) return comment;
|
|
return {
|
|
...comment,
|
|
body: LOW_TRUST_QUARANTINED_BODY,
|
|
presentation: null,
|
|
metadata: null,
|
|
};
|
|
}
|
|
|
|
export function buildLowTrustSourceTrust(input: {
|
|
issueId: string;
|
|
runId?: string | null;
|
|
agentId?: string | null;
|
|
}): SourceTrustMetadata {
|
|
return {
|
|
preset: LOW_TRUST_REVIEW_PRESET,
|
|
disposition: "quarantined",
|
|
sourceIssueId: input.issueId,
|
|
sourceRunId: input.runId ?? null,
|
|
sourceAgentId: input.agentId ?? null,
|
|
};
|
|
}
|
|
|
|
export function buildPromotedSourceTrust(input: {
|
|
sourceIssueId: string;
|
|
sourceArtifactKind: "comment" | "document" | "work_product" | "issue";
|
|
sourceArtifactId: string;
|
|
promotedByActorType: "agent" | "user" | "system";
|
|
promotedByActorId: string;
|
|
promotedAt?: Date;
|
|
}): SourceTrustMetadata {
|
|
return {
|
|
preset: LOW_TRUST_REVIEW_PRESET,
|
|
disposition: "promoted",
|
|
sourceIssueId: input.sourceIssueId,
|
|
promotedFrom: {
|
|
artifactKind: input.sourceArtifactKind,
|
|
artifactId: input.sourceArtifactId,
|
|
issueId: input.sourceIssueId,
|
|
},
|
|
promotedByActorType: input.promotedByActorType,
|
|
promotedByActorId: input.promotedByActorId,
|
|
promotedAt: (input.promotedAt ?? new Date()).toISOString(),
|
|
};
|
|
}
|
|
|
|
export type ActorSourceTrustResolution =
|
|
| { kind: "standard"; sourceTrust: null }
|
|
| { kind: "low_trust_review"; sourceTrust: SourceTrustMetadata }
|
|
| { kind: "unverified_run"; sourceTrust: SourceTrustMetadata };
|
|
|
|
export async function resolveActorSourceTrustStateForIssue(input: {
|
|
db: Db;
|
|
issue: SourceTrustIssueContext;
|
|
actor: SourceTrustActor;
|
|
}): Promise<ActorSourceTrustResolution> {
|
|
if (input.actor.actorType !== "agent" || !input.actor.agentId) {
|
|
return { kind: "standard", sourceTrust: null };
|
|
}
|
|
|
|
const [agent, project, run] = await Promise.all([
|
|
input.db
|
|
.select({
|
|
companyId: agents.companyId,
|
|
permissions: agents.permissions,
|
|
})
|
|
.from(agents)
|
|
.where(and(eq(agents.id, input.actor.agentId), eq(agents.companyId, input.issue.companyId)))
|
|
.then((rows) => rows[0] ?? null),
|
|
input.issue.projectId
|
|
? input.db
|
|
.select({
|
|
companyId: projects.companyId,
|
|
executionWorkspacePolicy: projects.executionWorkspacePolicy,
|
|
})
|
|
.from(projects)
|
|
.where(and(eq(projects.id, input.issue.projectId), eq(projects.companyId, input.issue.companyId)))
|
|
.then((rows) => rows[0] ?? null)
|
|
: Promise.resolve(null),
|
|
input.actor.runId
|
|
? input.db
|
|
.select({
|
|
companyId: heartbeatRuns.companyId,
|
|
agentId: heartbeatRuns.agentId,
|
|
contextSnapshot: heartbeatRuns.contextSnapshot,
|
|
})
|
|
.from(heartbeatRuns)
|
|
.where(and(eq(heartbeatRuns.id, input.actor.runId), eq(heartbeatRuns.companyId, input.issue.companyId)))
|
|
.then((rows) => rows[0] ?? null)
|
|
: Promise.resolve(null),
|
|
]);
|
|
|
|
if (input.actor.runId && (!run || run.agentId !== input.actor.agentId)) {
|
|
// Fail closed on writes: an unknown or mismatched run cannot prove higher trust, so tag the write as quarantined.
|
|
// Callers on read paths use `kind` via `isPositivelyLowTrustReaderForIssue` so unverifiable runs also fail closed.
|
|
return {
|
|
kind: "unverified_run",
|
|
sourceTrust: buildLowTrustSourceTrust({
|
|
issueId: input.issue.id,
|
|
runId: input.actor.runId,
|
|
agentId: input.actor.agentId,
|
|
}),
|
|
};
|
|
}
|
|
|
|
const runContext = readObject(run?.contextSnapshot);
|
|
const runExecutionPolicy = readObject(runContext?.executionPolicy);
|
|
|
|
const resolution = resolveCoreTrustPreset({
|
|
companyId: input.issue.companyId,
|
|
agent,
|
|
project,
|
|
issue: {
|
|
companyId: input.issue.companyId,
|
|
executionPolicy: input.issue.executionPolicy,
|
|
},
|
|
run: run
|
|
? {
|
|
companyId: run.companyId,
|
|
executionPolicy: runExecutionPolicy,
|
|
}
|
|
: null,
|
|
});
|
|
|
|
if (resolution.kind === "denied") {
|
|
throw forbidden(resolution.detail);
|
|
}
|
|
if (resolution.kind !== "low_trust_review") {
|
|
return { kind: "standard", sourceTrust: null };
|
|
}
|
|
return {
|
|
kind: "low_trust_review",
|
|
sourceTrust: buildLowTrustSourceTrust({
|
|
issueId: input.issue.id,
|
|
runId: input.actor.runId,
|
|
agentId: input.actor.agentId,
|
|
}),
|
|
};
|
|
}
|
|
|
|
export async function resolveActorSourceTrustForIssue(input: {
|
|
db: Db;
|
|
issue: SourceTrustIssueContext;
|
|
actor: SourceTrustActor;
|
|
}): Promise<SourceTrustMetadata | null> {
|
|
const resolved = await resolveActorSourceTrustStateForIssue(input);
|
|
return resolved.sourceTrust;
|
|
}
|
|
|
|
export async function isPositivelyLowTrustReaderForIssue(input: {
|
|
db: Db;
|
|
issue: SourceTrustIssueContext;
|
|
actor: SourceTrustActor;
|
|
}): Promise<boolean> {
|
|
const resolved = await resolveActorSourceTrustStateForIssue(input);
|
|
return resolved.kind === "low_trust_review";
|
|
}
|