mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-28 12:04:41 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Operators need first-party agent capabilities for repeatable company work, not just manually created one-off agents. > - Built-in agents need to behave like normal company-scoped agents while preserving approval gates, permissions, budgets, and audit trails. > - Reflection and coaching work also needs bundled instructions, skill content, and a routine so the feature can be installed and reset predictably. > - The API, database, UI, portability, and tests all need to agree on the built-in lifecycle from not provisioned through setup, approval, ready, paused, and reset. > - This pull request adds built-in agent provisioning and the Reflection Coach bundle end-to-end. > - The benefit is a safer first-party path for Paperclip-managed agents without bypassing the same governance model used for operator-created agents. ## Linked Issues or Issue Description No public GitHub issue was found for this exact built-in agent and Reflection Coach bundle work. Problem/motivation: - Paperclip did not have a first-party built-in agent lifecycle for product-owned agents. - Bundled agent resources such as default instructions, skills, and routines needed managed ownership and reset semantics. - Approval-gated companies needed built-in setup to preserve requested adapter, budget, manager, and permission state through board approval. - The board UI needed clear built-in badges, setup affordances, readiness state, and bundle status without exposing secrets. Proposed solution: - Add a company-scoped built-in agent registry, provisioning/reset/reconcile/status APIs, and Reflection Coach bundled resources. - Track bundled managed resources in the database with idempotent migration behavior. - Reuse existing agent approval, authorization, budget, and activity-log paths instead of creating a bypass. - Add UI setup, badges, gates, bundle panels, and route coverage for built-in agents. Duplicate search: - Searched GitHub PRs for `built-in agents Reflection Coach repo:paperclipai/paperclip`; only this PR was returned. - Searched GitHub issues for the same query; no public issues were returned. ## What Changed - Added built-in agent definitions, lifecycle state derivation, provisioning, reset, reconcile, status, and routine-control routes. - Added the `built_in_managed_resources` migration and schema exports for bundled instructions, skill, and routine ownership. - Added the Reflection Coach built-in bundle with default instructions, skill catalog content, routine template, default permissions, and managed-resource drift handling. - Added approval-aware provisioning behavior that preserves requested adapter config, budgets, manager assignment, and built-in permissions through hire approval. - Added authorization and mutation gates for built-in agent and skill changes, including consented Reflection Coach change paths. - Added UI surfaces for built-in agent setup, roster/detail badges, readiness gates, bundle status, routine controls, and route filtering. - Added company import/export and validator coverage for built-in managed resources and low-trust/red-team presets. - Addressed Greptile follow-ups for pending approval reconciliation, consent-gate error propagation, config-read authorization fallback, approval-path manager preservation, and non-model adapter provisioning. ## Verification Local verification: - `git diff --check public/master..HEAD` passed. - `pnpm check:token-gates` passed with all gates clean. - `pnpm exec vitest run ui/src/components/ConfigureBuiltInAgentModal.test.tsx` passed: 1 file, 4 tests. - `pnpm exec vitest run ui/src/components/EntityRow.test.tsx ui/src/pages/Agents.test.tsx ui/src/components/BuiltInAgentGate.test.tsx ui/src/components/ConfigureBuiltInAgentModal.test.tsx ui/src/components/BuiltInBundlePanel.test.tsx ui/src/pages/InstanceExperimentalSettings.test.tsx ui/src/pages/Routines.test.tsx` passed: 7 files, 64 tests. - `pnpm --filter @paperclipai/server exec vitest run src/__tests__/built-in-agents.test.ts src/__tests__/authorization-service.test.ts src/__tests__/company-skills-routes.test.ts` passed: 3 files, 91 tests. - `pnpm --filter @paperclipai/db check:migrations` passed. - `pnpm -r typecheck` passed after the rebase; `pnpm --filter ui typecheck` passed after the final UI review fix. Remote verification on latest head `1c61f693a4ec881d739022b0e75a8ca8bf8c2cd8`: - Merge state: `CLEAN`. - Greptile: `5/5`, zero unresolved Greptile threads. - PR check rollup: all checks successful, neutral, or skipped as expected. - Passing gates include Build, Typecheck + Release Registry, all server shards, all workspace shards, all serialized server suites, e2e, Canary Dry Run, policy, review, verify, Socket, Superagent, and Snyk. ## Risks - This adds a new managed-resource table and migration; the migration uses idempotent create/add/index guards and passed migration safety checks. - Built-in agent provisioning touches approval and authorization paths; tests cover pending approval preservation, stale retry rejection, consent gates, and config-read fallback behavior. - Reflection Coach creates managed instructions, skill, and routine resources; drift/reset behavior is covered by service tests and redacted API responses. - Non-model adapter setup now provisions a `needs_setup` built-in row before command/endpoint fields are complete; this matches the server lifecycle and is covered by the setup modal regression test. ## Model Used OpenAI Codex coding agent based on GPT-5. Exact hosted model ID, context-window size, and reasoning-mode labels are not exposed in this runtime; tool use, shell execution, GitHub CLI/API access, and local code editing were enabled. ## 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>
233 lines
8.2 KiB
TypeScript
233 lines
8.2 KiB
TypeScript
import type { Db } from "@paperclipai/db";
|
|
import { issueThreadInteractions } from "@paperclipai/db";
|
|
import { and, desc, eq, or, sql } from "drizzle-orm";
|
|
import type { RequestConfirmationPayload, RequestConfirmationResult } from "@paperclipai/shared";
|
|
import { forbidden } from "../errors.js";
|
|
|
|
export const AGENT_PROFILE_CHANGE_CONSENT_FIELDS = ["name", "role", "title", "capabilities"] as const;
|
|
|
|
type ConsumedRequestConfirmationResult = RequestConfirmationResult & {
|
|
consumedAt?: string | null;
|
|
consumedByRunId?: string | null;
|
|
};
|
|
|
|
export function agentInstructionsChangeTargetKey(agentId: string) {
|
|
return `agent:${agentId}:instructions`;
|
|
}
|
|
|
|
export function agentProfileChangeTargetKey(agentId: string) {
|
|
return `agent:${agentId}:profile`;
|
|
}
|
|
|
|
export function skillChangeTargetKey(skillId: string) {
|
|
return `skill:${skillId}`;
|
|
}
|
|
|
|
export function skillSlugChangeTargetKey(slug: string) {
|
|
return `skill-slug:${slug}`;
|
|
}
|
|
|
|
export function skillImportChangeTargetKey(source: string) {
|
|
return `skill-import:${source}`;
|
|
}
|
|
|
|
export function skillsScanProjectsChangeTargetKey() {
|
|
return "skills:scan-projects";
|
|
}
|
|
|
|
export function touchesAgentProfileChangeConsentFields(patchData: Record<string, unknown>) {
|
|
return AGENT_PROFILE_CHANGE_CONSENT_FIELDS.some((key) =>
|
|
Object.prototype.hasOwnProperty.call(patchData, key),
|
|
);
|
|
}
|
|
|
|
function readNonEmptyString(value: unknown) {
|
|
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
|
}
|
|
|
|
function payloadHasDisplayedDiff(payload: RequestConfirmationPayload) {
|
|
const details = readNonEmptyString(payload.detailsMarkdown);
|
|
if (!details) return false;
|
|
if (/```diff\b/i.test(details)) return true;
|
|
return /(^|\n)[+-][^\n]+/.test(details);
|
|
}
|
|
|
|
function requestConfirmationResultConsumed(result: RequestConfirmationResult | null) {
|
|
const consumed = result as ConsumedRequestConfirmationResult | null;
|
|
return Boolean(readNonEmptyString(consumed?.consumedByRunId) || readNonEmptyString(consumed?.consumedAt));
|
|
}
|
|
|
|
function markRequestConfirmationResultConsumed(
|
|
result: RequestConfirmationResult,
|
|
actorRunId: string,
|
|
consumedAt: Date,
|
|
): ConsumedRequestConfirmationResult {
|
|
return {
|
|
...result,
|
|
consumedAt: consumedAt.toISOString(),
|
|
consumedByRunId: actorRunId,
|
|
};
|
|
}
|
|
|
|
function legacyTargetKeysFor(targetKey: string) {
|
|
if (targetKey.startsWith("agent:") && targetKey.endsWith(":instructions")) {
|
|
const agentId = targetKey.slice("agent:".length, -":instructions".length);
|
|
if (agentId) return [`reflection-coach:agent-instructions:${agentId}`];
|
|
}
|
|
if (targetKey.startsWith("agent:") && targetKey.endsWith(":profile")) {
|
|
const agentId = targetKey.slice("agent:".length, -":profile".length);
|
|
if (agentId) return [`reflection-coach:agent-description:${agentId}`];
|
|
}
|
|
if (targetKey.startsWith("skill:")) {
|
|
const skillId = targetKey.slice("skill:".length);
|
|
if (skillId) return [`reflection-coach:company-skill:${skillId}`];
|
|
}
|
|
if (targetKey.startsWith("skill-slug:")) {
|
|
const slug = targetKey.slice("skill-slug:".length);
|
|
if (slug) return [`reflection-coach:company-skill-slug:${slug}`];
|
|
}
|
|
if (targetKey.startsWith("skill-import:")) {
|
|
const source = targetKey.slice("skill-import:".length);
|
|
if (source) {
|
|
return [
|
|
`reflection-coach:company-skill-import:${source}`,
|
|
`reflection-coach:company-skill-catalog:${source}`,
|
|
];
|
|
}
|
|
}
|
|
if (targetKey === "skills:scan-projects") {
|
|
return ["reflection-coach:company-skills:scan-projects"];
|
|
}
|
|
return [];
|
|
}
|
|
|
|
function expandTargetKeysForLegacyCompatibility(targetKeys: string[]) {
|
|
const expanded = new Set<string>();
|
|
for (const targetKey of targetKeys) {
|
|
expanded.add(targetKey);
|
|
for (const legacyTargetKey of legacyTargetKeysFor(targetKey)) {
|
|
expanded.add(legacyTargetKey);
|
|
}
|
|
}
|
|
return [...expanded];
|
|
}
|
|
|
|
export function changeConsentGateService(db: Db) {
|
|
return {
|
|
assertConsented: async (input: {
|
|
companyId: string;
|
|
actorAgentId: string | null | undefined;
|
|
actorRunId: string | null | undefined;
|
|
targetKeys: string[];
|
|
}): Promise<boolean> => {
|
|
const actorAgentId = readNonEmptyString(input.actorAgentId);
|
|
if (!actorAgentId) return false;
|
|
|
|
const actorRunId = readNonEmptyString(input.actorRunId);
|
|
if (!actorRunId) {
|
|
throw forbidden("Reflection Coach mutations require a run id", {
|
|
code: "reflection_coach_mutation_run_id_required",
|
|
});
|
|
}
|
|
|
|
const targetKeys = [...new Set(input.targetKeys.map(readNonEmptyString).filter((key): key is string => Boolean(key)))];
|
|
if (targetKeys.length === 0) {
|
|
throw forbidden("Reflection Coach mutation target is not gateable", {
|
|
code: "reflection_coach_mutation_target_required",
|
|
});
|
|
}
|
|
const queryTargetKeys = expandTargetKeysForLegacyCompatibility(targetKeys);
|
|
|
|
const targetKeyPredicate = or(
|
|
...queryTargetKeys.map((targetKey) =>
|
|
sql`${issueThreadInteractions.payload}->'target'->>'key' = ${targetKey}`,
|
|
),
|
|
);
|
|
|
|
const rows = await db
|
|
.select({
|
|
id: issueThreadInteractions.id,
|
|
sourceRunId: issueThreadInteractions.sourceRunId,
|
|
payload: issueThreadInteractions.payload,
|
|
result: issueThreadInteractions.result,
|
|
})
|
|
.from(issueThreadInteractions)
|
|
.where(and(
|
|
eq(issueThreadInteractions.companyId, input.companyId),
|
|
eq(issueThreadInteractions.createdByAgentId, actorAgentId),
|
|
eq(issueThreadInteractions.kind, "request_confirmation"),
|
|
eq(issueThreadInteractions.status, "accepted"),
|
|
targetKeyPredicate,
|
|
))
|
|
.orderBy(desc(issueThreadInteractions.resolvedAt), desc(issueThreadInteractions.createdAt))
|
|
.limit(10);
|
|
|
|
const accepted = rows.find((row) => {
|
|
const payload = row.payload as RequestConfirmationPayload;
|
|
const result = row.result as RequestConfirmationResult | null;
|
|
return payload.target?.type === "custom"
|
|
&& queryTargetKeys.includes(payload.target.key)
|
|
&& result?.outcome === "accepted"
|
|
&& !requestConfirmationResultConsumed(result)
|
|
&& payloadHasDisplayedDiff(payload)
|
|
&& Boolean(row.sourceRunId)
|
|
&& row.sourceRunId !== actorRunId;
|
|
});
|
|
|
|
if (!accepted) {
|
|
throw forbidden(
|
|
"Reflection Coach mutations require an accepted request_confirmation with a displayed diff for this target, "
|
|
+ "created in a previous run and not already consumed.",
|
|
{
|
|
code: "reflection_coach_mutation_gate_required",
|
|
targetKeys,
|
|
},
|
|
);
|
|
}
|
|
|
|
const acceptedResult = accepted.result as RequestConfirmationResult | null;
|
|
if (!acceptedResult) {
|
|
throw forbidden(
|
|
"Reflection Coach mutations require an accepted request_confirmation with a displayed diff for this target, "
|
|
+ "created in a previous run and not already consumed.",
|
|
{
|
|
code: "reflection_coach_mutation_gate_required",
|
|
targetKeys,
|
|
},
|
|
);
|
|
}
|
|
|
|
const now = new Date();
|
|
const [consumed] = await db
|
|
.update(issueThreadInteractions)
|
|
.set({
|
|
result: markRequestConfirmationResultConsumed(acceptedResult, actorRunId, now),
|
|
updatedAt: now,
|
|
})
|
|
.where(and(
|
|
eq(issueThreadInteractions.id, accepted.id),
|
|
eq(issueThreadInteractions.companyId, input.companyId),
|
|
eq(issueThreadInteractions.createdByAgentId, actorAgentId),
|
|
eq(issueThreadInteractions.kind, "request_confirmation"),
|
|
eq(issueThreadInteractions.status, "accepted"),
|
|
sql`${issueThreadInteractions.result}->>'outcome' = 'accepted'`,
|
|
sql`coalesce(${issueThreadInteractions.result}->>'consumedByRunId', ${issueThreadInteractions.result}->>'consumedAt') is null`,
|
|
))
|
|
.returning({ id: issueThreadInteractions.id });
|
|
|
|
if (!consumed) {
|
|
throw forbidden(
|
|
"Reflection Coach mutations require an accepted request_confirmation with a displayed diff for this target, "
|
|
+ "created in a previous run and not already consumed.",
|
|
{
|
|
code: "reflection_coach_mutation_gate_required",
|
|
targetKeys,
|
|
},
|
|
);
|
|
}
|
|
|
|
return true;
|
|
},
|
|
};
|
|
}
|