mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-21 00:05:49 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agents can currently perform many mutations directly, while humans often need a durable review point before cross-issue or destructive actions occur > - Existing approvals and issue-thread interactions do not provide a standalone, reusable object for presenting options, collecting typed inputs, detecting stale targets, and auditing effect execution > - The control plane therefore needs a first-class propose mode that separates an agent's recommendation from the governed mutation it may cause > - This pull request adds Decisions v1 across the database, shared contracts, server execution and telemetry, agent skill guidance, and operator UI > - The benefit is that agents can propose multi-option actions safely while operators get explicit provenance, fail-closed execution, per-effect results, and a focused attention workflow ## Linked Issues or Issue Description ### Subsystem affected Cross-cutting: `packages/db`, `packages/shared`, `server`, and `ui`. ### Problem or motivation Agents need a governed way to propose consequential work without immediately mutating issues, especially when one choice can affect several issue trees. Existing approvals and issue-thread interactions do not provide a standalone object with typed options, target snapshots, effect-level authorization, expiration, execution outcomes, and reusable attention-feed presentation. ### Proposed solution Add first-class Decisions that store options and typed inputs, surface open proposals in the operator attention feed, validate target freshness and the origin-agent/operator authorization intersection at decision time, execute a bounded set of auditable effects, and retain terminal outcomes. Decisions v1 supports comments, status and assignee changes, follow-up issue creation, blocker resolution, and issue-tree cancellation, plus bundle grouping, expiration/dismissal, rule-key telemetry, and agent-facing API guidance. ### Alternatives considered - Extend approvals with arbitrary effects: rejected because approvals represent governed yes/no actions and would become an unsafe generic mutation envelope. - Model every proposal as an issue-thread interaction: rejected because decisions can span several targets and need independent lifecycle, telemetry, idempotency, and effect results. - Let agents perform the mutation and ask for retrospective review: rejected because it removes the pre-execution governance boundary this feature is meant to provide. ### Roadmap alignment Aligns with `ROADMAP.md` sections **Agent Reviews and Approvals**, **Enforced Outcomes**, **MCP Tool Gateway & Apps (governed tool access)**, and **Activity History** by making explicit decisions, authorization gates, auditable execution, and terminal outcomes first-class control-plane objects. ### Additional context This does not replace existing approvals or issue-thread interactions, and it does not add an unrestricted generic mutation effect. ## What Changed - Added company-scoped decision, option, target, and effect-execution schema plus migration and shared TypeScript/Zod contracts. - Added decision routes and services for propose, list/get, decide, dismiss, cancel, target freshness checks, authorization intersection, idempotency, activity logging, and execution auditing. - Added rule-key decision telemetry and attention-feed metadata so open decisions are visible and measurable. - Added agent skill documentation for proposing and resolving decisions through the Paperclip API. - Added the Decisions UI: API client, query keys, inline attention resolver, bundle grouping, target-issue strip, terminal history, destructive confirmation, and per-effect result rendering. - Added server service coverage, DecisionCard state tests, and Storybook stories for the supported visual states. ## Verification - `pnpm -r typecheck` — passed. - `pnpm test:run` — 2,876 passed, 1 skipped, with one unrelated cross-suite cleanup-order failure in `heartbeat-responsible-user-invariant.test.ts`; the failing file passes in isolation (`6/6`). - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/heartbeat-responsible-user-invariant.test.ts` — passed. - `pnpm --filter @paperclipai/ui exec vitest run src/components/DecisionCard.test.tsx` — passed (`9/9`). - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/authz-existence-oracle-guard.test.ts src/__tests__/openapi-routes.test.ts` — passed (`5/5`). - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/decisions-service.test.ts` — passed (`16/16`). - `pnpm --filter paperclipai exec vitest run src/__tests__/company-import-export-e2e.test.ts` — passed (`1/1`). - `pnpm --filter @paperclipai/server typecheck` and `pnpm --filter paperclipai typecheck` — passed. - `pnpm build` — passed. - Rebased-head focused suite — passed (`6` files, `88` tests): shared decision contracts, Decisions service, OpenAPI routes, startup feedback export, DecisionCard states, and attention helpers. The follow-up stale-secondary-target regression passes in the DecisionCard suite (`10/10`). - Rebased-head scoped typechecks — passed for `@paperclipai/shared`, `@paperclipai/db`, `@paperclipai/server`, and `@paperclipai/ui`. - Rebased-head migration numbering and safety checks — passed after renumbering the additive migration to `0193` and making it replay-safe for environments that applied the earlier feature-branch number. - `pnpm check:token-gates` — passed with all gates clean. - GitHub PR workflow and Greptile review for `1f9f7645882d05dfdd9c99377c03a1f53f20e8be` — running after the stale-secondary-target fix and PR metadata refresh on July 27, 2026. - `pnpm --filter @paperclipai/ui build-storybook` exposes an existing Storybook version mismatch (`storybook` 10.4.6 vs `@storybook/addon-docs` 10.5.0); Decisions stories were validated with the docs addon temporarily disabled and the tracked config remains unchanged. ## Risks - **Migration:** Adds replay-safe migration `0193`; migration numbering and safety checks pass. The new tables and indexes are additive. - **Authorization:** Effect execution intersects the proposing agent's permissions with the responsible user context and fails closed; mistakes could reject a valid proposal rather than silently over-authorize it. - **Concurrency:** Target snapshots and idempotency keys protect against stale or duplicate execution, but reviewers should focus on mixed-effect partial outcomes and retry behavior. - **UI:** Decisions are integrated into the existing attention feed rather than a separate navigation surface, reducing routing risk but increasing the importance of attention-item metadata compatibility. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - OpenAI Codex CLI using `gpt-5.6-sol` for final PR preparation, review fixes, and verification; repository tools and code execution were enabled, and context-window size is not exposed in this runtime. - Anthropic Claude Opus 4.8 with 1M context assisted with the Decisions UI implementation, as recorded in the relevant commits. ## 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 - [x] All Paperclip CI gates are green - [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
200 lines
7.4 KiB
TypeScript
200 lines
7.4 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { and, eq } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import { activityLog, agentApiKeys, companies, heartbeatRuns, issues } from "@paperclipai/db";
|
|
import { isUuidLike, PLUGIN_EVENT_TYPES, type PluginEventType } from "@paperclipai/shared";
|
|
import type { PluginEvent } from "@paperclipai/plugin-sdk";
|
|
import { publishLiveEvent } from "./live-events.js";
|
|
import { redactCurrentUserValue } from "../log-redaction.js";
|
|
import { sanitizeRecord } from "../redaction.js";
|
|
import { logger } from "../middleware/logger.js";
|
|
import type { PluginEventBus } from "./plugin-event-bus.js";
|
|
import { instanceSettingsService } from "./instance-settings.js";
|
|
|
|
const PLUGIN_EVENT_SET: ReadonlySet<string> = new Set(PLUGIN_EVENT_TYPES);
|
|
const ACTIVITY_ACTION_TO_PLUGIN_EVENT: Readonly<Record<string, PluginEventType>> = {
|
|
issue_comment_added: "issue.comment.created",
|
|
issue_comment_created: "issue.comment.created",
|
|
issue_document_created: "issue.document.created",
|
|
issue_document_updated: "issue.document.updated",
|
|
issue_document_deleted: "issue.document.deleted",
|
|
issue_blockers_updated: "issue.relations.updated",
|
|
approval_approved: "approval.decided",
|
|
approval_rejected: "approval.decided",
|
|
approval_revision_requested: "approval.decided",
|
|
budget_soft_threshold_crossed: "budget.incident.opened",
|
|
budget_hard_threshold_crossed: "budget.incident.opened",
|
|
budget_incident_resolved: "budget.incident.resolved",
|
|
};
|
|
|
|
let _pluginEventBus: PluginEventBus | null = null;
|
|
|
|
/** Wire the plugin event bus so domain events are forwarded to plugins. */
|
|
export function setPluginEventBus(bus: PluginEventBus): void {
|
|
if (_pluginEventBus) {
|
|
logger.warn("setPluginEventBus called more than once, replacing existing bus");
|
|
}
|
|
_pluginEventBus = bus;
|
|
}
|
|
|
|
function eventTypeForActivityAction(action: string): PluginEventType | null {
|
|
if (PLUGIN_EVENT_SET.has(action)) return action as PluginEventType;
|
|
return ACTIVITY_ACTION_TO_PLUGIN_EVENT[action.replaceAll(".", "_")] ?? null;
|
|
}
|
|
|
|
export function publishPluginDomainEvent(event: PluginEvent): void {
|
|
if (!_pluginEventBus) return;
|
|
void _pluginEventBus.emit(event).then(({ errors }) => {
|
|
for (const { pluginId, error } of errors) {
|
|
logger.warn({ pluginId, eventType: event.eventType, err: error }, "plugin event handler failed");
|
|
}
|
|
}).catch(() => {});
|
|
}
|
|
|
|
export interface LogActivityInput {
|
|
companyId: string;
|
|
actorType: "agent" | "user" | "system" | "plugin";
|
|
actorId: string;
|
|
action: string;
|
|
entityType: string;
|
|
entityId: string;
|
|
agentId?: string | null;
|
|
runId?: string | null;
|
|
agentApiKeyId?: string | null;
|
|
issueId?: string | null;
|
|
details?: Record<string, unknown> | null;
|
|
responsibleUserIdOverride?: string | null;
|
|
}
|
|
|
|
export async function createActivityDetailsRedactor(db: Db) {
|
|
const currentUserRedactionOptions = {
|
|
enabled: (await instanceSettingsService(db).getGeneral()).censorUsernameInLogs,
|
|
};
|
|
return (details: Record<string, unknown> | null) => (
|
|
details ? redactCurrentUserValue(sanitizeRecord(details), currentUserRedactionOptions) : null
|
|
);
|
|
}
|
|
|
|
export async function redactActivityDetails(db: Db, details: Record<string, unknown> | null) {
|
|
if (!details) return null;
|
|
return (await createActivityDetailsRedactor(db))(details);
|
|
}
|
|
|
|
function readNonEmptyString(value: unknown) {
|
|
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
}
|
|
|
|
export async function resolveResponsibleUserIdForActivity(db: Db, input: LogActivityInput) {
|
|
if (input.responsibleUserIdOverride !== undefined) {
|
|
return readNonEmptyString(input.responsibleUserIdOverride);
|
|
}
|
|
if (input.actorType === "user") return readNonEmptyString(input.actorId);
|
|
|
|
const runId = readNonEmptyString(input.runId);
|
|
if (runId && isUuidLike(runId)) {
|
|
const run = await db
|
|
.select({ responsibleUserId: heartbeatRuns.responsibleUserId })
|
|
.from(heartbeatRuns)
|
|
.where(and(eq(heartbeatRuns.companyId, input.companyId), eq(heartbeatRuns.id, runId)))
|
|
.then((rows) => rows[0] ?? null);
|
|
const runResponsibleUserId = readNonEmptyString(run?.responsibleUserId);
|
|
if (runResponsibleUserId) return runResponsibleUserId;
|
|
}
|
|
|
|
const issueIdCandidate = readNonEmptyString(input.issueId)
|
|
?? (input.entityType === "issue" ? readNonEmptyString(input.entityId) : null);
|
|
const issueId = isUuidLike(issueIdCandidate) ? issueIdCandidate : null;
|
|
if (issueId) {
|
|
const issue = await db
|
|
.select({
|
|
responsibleUserId: issues.responsibleUserId,
|
|
createdByUserId: issues.createdByUserId,
|
|
})
|
|
.from(issues)
|
|
.where(and(eq(issues.companyId, input.companyId), eq(issues.id, issueId)))
|
|
.then((rows) => rows[0] ?? null);
|
|
const issueResponsibleUserId = readNonEmptyString(issue?.responsibleUserId)
|
|
?? readNonEmptyString(issue?.createdByUserId);
|
|
if (issueResponsibleUserId) return issueResponsibleUserId;
|
|
}
|
|
|
|
const agentApiKeyId = readNonEmptyString(input.agentApiKeyId);
|
|
const agentId = readNonEmptyString(input.agentId);
|
|
if (agentApiKeyId && isUuidLike(agentApiKeyId)) {
|
|
const apiKey = await db
|
|
.select({ responsibleUserId: agentApiKeys.responsibleUserId })
|
|
.from(agentApiKeys)
|
|
.where(and(
|
|
eq(agentApiKeys.companyId, input.companyId),
|
|
eq(agentApiKeys.id, agentApiKeyId),
|
|
...(agentId && isUuidLike(agentId) ? [eq(agentApiKeys.agentId, agentId)] : []),
|
|
))
|
|
.then((rows) => rows[0] ?? null);
|
|
const apiKeyResponsibleUserId = readNonEmptyString(apiKey?.responsibleUserId);
|
|
if (apiKeyResponsibleUserId) return apiKeyResponsibleUserId;
|
|
}
|
|
|
|
const company = await db
|
|
.select({ defaultResponsibleUserId: companies.defaultResponsibleUserId })
|
|
.from(companies)
|
|
.where(eq(companies.id, input.companyId))
|
|
.then((rows) => rows[0] ?? null);
|
|
return readNonEmptyString(company?.defaultResponsibleUserId);
|
|
}
|
|
|
|
export async function logActivity(db: Db, input: LogActivityInput) {
|
|
const redactedDetails = await redactActivityDetails(db, input.details ?? null);
|
|
const responsibleUserId = await resolveResponsibleUserIdForActivity(db, input);
|
|
const [activity] = await db.insert(activityLog).values({
|
|
companyId: input.companyId,
|
|
actorType: input.actorType,
|
|
actorId: input.actorId,
|
|
action: input.action,
|
|
entityType: input.entityType,
|
|
entityId: input.entityId,
|
|
agentId: input.agentId ?? null,
|
|
runId: input.runId ?? null,
|
|
responsibleUserId,
|
|
details: redactedDetails,
|
|
}).returning({ id: activityLog.id });
|
|
|
|
publishLiveEvent({
|
|
companyId: input.companyId,
|
|
type: "activity.logged",
|
|
payload: {
|
|
actorType: input.actorType,
|
|
actorId: input.actorId,
|
|
action: input.action,
|
|
entityType: input.entityType,
|
|
entityId: input.entityId,
|
|
agentId: input.agentId ?? null,
|
|
runId: input.runId ?? null,
|
|
responsibleUserId,
|
|
details: redactedDetails,
|
|
},
|
|
});
|
|
|
|
const pluginEventType = eventTypeForActivityAction(input.action);
|
|
if (pluginEventType) {
|
|
const event: PluginEvent = {
|
|
eventId: randomUUID(),
|
|
eventType: pluginEventType,
|
|
occurredAt: new Date().toISOString(),
|
|
actorId: input.actorId,
|
|
actorType: input.actorType,
|
|
entityId: input.entityId,
|
|
entityType: input.entityType,
|
|
companyId: input.companyId,
|
|
payload: {
|
|
...redactedDetails,
|
|
agentId: input.agentId ?? null,
|
|
runId: input.runId ?? null,
|
|
responsibleUserId,
|
|
},
|
|
};
|
|
publishPluginDomainEvent(event);
|
|
}
|
|
|
|
return activity;
|
|
}
|