mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-10-21 16:27:18 +02:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - Chat connectors give each agent a customer-owned bot and task-backed conversations. > - Manual Slack setup requires app creation and copying durable credentials. > - Operators need a shorter setup that an assisting agent can use safely. > - This pull request creates the app through Slack's Manifest API and installs it through OAuth. > - Durable registration state supports recovery without creating another app. > - A four-screen wizard, automatic avatar upload, and OAuth account linking reduce setup work. > - Connector settings and per-turn tool guidance support daily use after installation. ## Linked Issues or Issue Description **Subsystem affected** Native Slack bot setup, company secret storage, chat connector management, and agent tool guidance. **Problem or motivation** New Slack bots require manual app creation and copying a signing secret and bot token. Interrupted setup can create duplicate apps. The setup and management screens contain unnecessary controls. Agents also need guidance for native questions, files, thread replies, and governed Slack actions. **Proposed solution** Use a temporary app-configuration access token to create a customer-owned app. Save durable secrets in the vault. Bind OAuth to the initiating actor, company, endpoint, registration revision, scopes, and configured origins. Preserve manual and existing-app recovery. Link the installing user's account, send a welcome DM, and advance from saved server evidence. Keep request-URL recovery instructions available if automatic connection detection waits. **Alternatives considered** The Slack CLI adds installation requirements. Socket Mode changes transport. A shared Paperclip-owned app changes app ownership. These alternatives are outside this change. **Roadmap alignment** This extends existing chat connectors and secrets capabilities. Related public work: #14037 and #13954 cover Slack MCP prerequisites and user OAuth. No duplicate bot-registration PR was found. ## What Changed - Share one reviewed manifest builder between automatic registration and manual setup. - Add replay-safe migration 0318 and company-bound registration state with vault references and uncertain-creation recovery. - Add registration, installation, callback, and resume APIs with short-lived, single-use OAuth state. - Save installation credentials before downstream checks and preserve bot identity constraints. - Reduce automatic setup to four screens. Keep advanced app details, manual recovery, and existing-app setup. - Upload the agent avatar with the Paperclip dark background. Link the OAuth installer's account and send setup DMs. - Show agent and connector-owner avatars. Simplify settings, access, and conversation screens. - Discover joined Slack channels and enable them by default. Start a task from a bare mention and admit same-thread follow-ups. - Refresh Slack tool guidance each turn. Add native-form, file, approval, and delivery regressions plus manual model probe definitions and sanitized acceptance records. - Update deployment/database docs, OpenAPI, redaction, removal cleanup, production Storybook stories, and provider browser tests. - Merge current master and move the registration migration after its latest migration without rewriting published commits. The completed Slack success view intentionally has a single centered **Done** action and no **Save & exit**, as explicitly requested by the product owner. `DESIGN.md` records this exception; unfinished setup steps retain the aligned wizard footer. ## Verification - Passed after the master merge: repository typecheck, full build, Storybook build, design-token gates, module-boundary gates, and migration generation. - Passed: all 352 focused Slack deterministic tests and all 14 affected provider browser tests. Browser tests use controlled provider fixtures and a separate throwaway instance. - Passed on current head `c5d01e0e2`: the complete GitHub test matrix (general server, chat, all workspaces, serialized server, and Runner), all eight browser shards, typecheck/release registry, build, canary dry run, security checks, and policy gates. There are 52 passing checks and no pending or failing checks. - Greptile completed on the exact current head with 5/5 and no actionable findings or open review threads. - Local repair verification passed 93 focused tests, including same-app reinstall after revocation and rejection of consent started before revocation, the AgentMail browser journey, and repository typecheck. Local build and Storybook build also passed. The redundant local full-suite rerun was stopped after the complete current-head CI matrix passed. - Real Slack setup and agent replies were exercised in the authorized isolated test drive during the setup iteration. - The ten additional model probes were attempted with legacy `codex_local`, `gpt-5.6-sol`: five passed, two failed, and three were partly verified. Native runtime is not qualified. See `server/src/services/connectors/slack/evals/2026-10-08-acceptance.md` for evidence and limits. - Passing model probes cover native forms, downloaded file bytes, bare mentions with thread replies, explicit posts/reactions, and saved approval denial. - The controlled uncertain-write probe found wrong delivery-check IDs. The canvas fallback attempt used an invented tool name. Search pagination/native search, a private-source denied-tool receipt, and distinct board/webhook origins remain unqualified. Reviewer path: enable Chat connectors, start Slack chat setup, select an agent, enter an app-configuration access token, and approve Slack installation. Send a message to the bot and confirm that setup advances to success. Inspect settings and allowed channels. See `doc/connections/SLACK-AUTOMATIC-SETUP.md` for deployment and recovery. ## Risks - Slack app creation has no provider idempotency guarantee. A timeout after dispatch stays uncertain until the operator checks Slack. - OAuth needs a stable public HTTPS board origin. Webhook ingress may use a separate configured HTTPS origin. Workspace policy can delay installation. - Migration 0318 can replay safely on instances that applied the earlier development migration. - OAuth installation now links the installer to the initiating Paperclip user. Identity checks and company access rules still apply. - Joined channels now enable bot responses by default. Linked-user authorization and per-action approval rules still apply. - Model behavior has the documented delivery-check and canvas fallback failures. A passing CI run does not establish that every model probe passed. - Removing the connection does not delete the customer's Slack app. No new first-party telemetry is added. ## Model Used OpenAI Codex, GPT-6 family, with reasoning, repository tools, code execution, and browser verification. The runtime does not expose a more specific authoring model ID or context-window size. The live bot probes used OpenAI `gpt-5.6-sol` through `codex_local` in legacy mode. ## 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>
182 lines
12 KiB
TypeScript
182 lines
12 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
import os from "node:os";
|
|
import { Readable } from "node:stream";
|
|
import { S3Client, PutObjectCommand, HeadObjectCommand, GetObjectCommand, DeleteObjectCommand } from "@aws-sdk/client-s3";
|
|
import { createS3StorageProvider } from "../storage/s3-provider.js";
|
|
import path from "node:path";
|
|
import express from "express";
|
|
import type { Server } from "node:http";
|
|
import sharp from "sharp";
|
|
import { AGENT_PALETTE_IDS, appearanceForPalette, PAPERCLIP_DARK_AVATAR_BACKGROUND } from "@paperclipai/shared";
|
|
import { createLocalDiskStorageProvider } from "../storage/local-disk-provider.js";
|
|
import { createAgentAvatarService, avatarCacheKey, type AgentAvatarRequest } from "../services/agent-avatars.js";
|
|
import { createAgentAvatarPool } from "../services/agent-avatar-pool.js";
|
|
import { agentAvatarRoutes } from "../routes/agent-avatars.js";
|
|
|
|
const request: AgentAvatarRequest = { appearance: appearanceForPalette("arctic-blue"), size: 24, scale: 2, pose: "rest", muted: false };
|
|
const cleanups: Array<() => Promise<unknown>> = [];
|
|
afterEach(async () => { await Promise.all(cleanups.splice(0).map(fn => fn())); });
|
|
async function consume(stream: Readable) {
|
|
for await (const _ of stream) { /* Finish reads before deleting their cache files. */ }
|
|
}
|
|
async function storage() {
|
|
const dir = await mkdtemp(path.join(os.tmpdir(), "agent-avatar-test-"));
|
|
cleanups.push(() => rm(dir, { recursive: true, force: true }));
|
|
return createLocalDiskStorageProvider(dir);
|
|
}
|
|
async function serve(service: ReturnType<typeof createAgentAvatarService>) {
|
|
const app = express(); app.use("/api", agentAvatarRoutes(service).router);
|
|
const server = await new Promise<Server>(resolve => { const running = app.listen(0, "127.0.0.1", () => resolve(running)); });
|
|
cleanups.push(() => new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve())));
|
|
return `http://127.0.0.1:${(server.address() as { port: number }).port}/api/agent-avatars/cap-v1/arctic-blue/rest.png`;
|
|
}
|
|
describe("on-demand agent avatars", () => {
|
|
it("coalesces cold requests, survives restarts and regenerates deleted cache entries", async () => {
|
|
const provider = await storage();
|
|
const render = vi.fn(async () => Buffer.from("png-bytes"));
|
|
const service = createAgentAvatarService(provider, render);
|
|
const results = await Promise.all(Array.from({ length: 10 }, () => service.get(request)));
|
|
expect(render).toHaveBeenCalledTimes(1);
|
|
expect(new Set(results.map(result => result.etag)).size).toBe(1);
|
|
await Promise.all(results.map(async result => { for await (const _ of result.stream) { /* consume */ } }));
|
|
await consume((await createAgentAvatarService(provider, render).get(request)).stream);
|
|
expect(render).toHaveBeenCalledTimes(1);
|
|
await provider.deleteObject({ objectKey: avatarCacheKey(request) });
|
|
await consume((await service.get(request)).stream);
|
|
expect(render).toHaveBeenCalledTimes(2);
|
|
});
|
|
it("limits cold keys per client while admitting warm hits, joiners and other clients", async () => {
|
|
let release!: () => void;
|
|
const blocked = new Promise<void>(resolve => { release = resolve; });
|
|
const render = vi.fn(async () => { await blocked; return Buffer.from("png"); });
|
|
const provider = await storage();
|
|
const service = createAgentAvatarService(provider, render);
|
|
const keys = AGENT_PALETTE_IDS.flatMap(palette => ([16, 20, 24] as const).map(size => ({ ...request, appearance: appearanceForPalette(palette), size })));
|
|
const pending = keys.slice(0, 32).map(key => service.get(key, "one"));
|
|
try {
|
|
await vi.waitFor(() => expect(render).toHaveBeenCalledTimes(32));
|
|
await expect(service.get(keys[32], "one")).rejects.toThrow("Too many cold avatar requests");
|
|
pending.push(service.get(keys[0], "one")); // Same cold key is free.
|
|
pending.push(service.get(keys[32], "two")); // Another client still has room.
|
|
await vi.waitFor(() => expect(render).toHaveBeenCalledTimes(33));
|
|
} finally { release(); }
|
|
for (const result of await Promise.all(pending)) await consume(result.stream);
|
|
await consume((await service.get(keys[0], "one")).stream);
|
|
expect(render).toHaveBeenCalledTimes(33);
|
|
await consume((await service.get(keys[33], "one")).stream); // Completed renders release slots.
|
|
expect(render).toHaveBeenCalledTimes(34);
|
|
});
|
|
it("returns retryable admission errors without caching them", async () => {
|
|
const service = createAgentAvatarService(await storage(), async () => Buffer.from("png"));
|
|
const { AvatarAdmissionError } = await import("../services/agent-avatars.js");
|
|
vi.spyOn(service, "get").mockRejectedValueOnce(new AvatarAdmissionError(12));
|
|
const url = await serve(service);
|
|
const denied = await fetch(url);
|
|
expect(denied.status).toBe(429);
|
|
expect(denied.headers.get("retry-after")).toBe("12");
|
|
expect(denied.headers.get("cache-control")).toBe("no-store");
|
|
await denied.text();
|
|
const retry = await fetch(url); expect(retry.status).toBe(200); await retry.arrayBuffer();
|
|
});
|
|
it("uses the configured S3 prefix and reuses bytes across service instances", async () => {
|
|
const objects = new Map<string, Buffer>();
|
|
const send = vi.spyOn(S3Client.prototype, "send").mockImplementation(async (command: any) => {
|
|
const key = command.input.Key as string;
|
|
expect(command.input.Bucket).toBe("avatar-test");
|
|
expect(key).toMatch(/^paperclip\/generated-agent-avatars\/cap-v1\//);
|
|
if (command instanceof PutObjectCommand) { objects.set(key, Buffer.from(command.input.Body as Uint8Array)); return {}; }
|
|
if (command instanceof DeleteObjectCommand) { objects.delete(key); return {}; }
|
|
const bytes = objects.get(key);
|
|
if (!bytes) throw Object.assign(new Error("missing"), { name: "NoSuchKey" });
|
|
if (command instanceof HeadObjectCommand) return { ContentLength: bytes.length };
|
|
if (command instanceof GetObjectCommand) return { Body: Readable.from(bytes), ContentLength: bytes.length };
|
|
throw new Error("Unexpected S3 command");
|
|
});
|
|
try {
|
|
const provider = createS3StorageProvider({ bucket: "avatar-test", region: "us-east-1", prefix: "paperclip" });
|
|
const render = vi.fn(async () => Buffer.from("s3-avatar"));
|
|
const first = await createAgentAvatarService(provider, render).get(request);
|
|
const second = await createAgentAvatarService(provider, render).get(request);
|
|
expect(second.etag).toEqual(first.etag);
|
|
const firstBytes: Buffer[] = [], secondBytes: Buffer[] = [];
|
|
for await (const chunk of first.stream) firstBytes.push(Buffer.from(chunk));
|
|
for await (const chunk of second.stream) secondBytes.push(Buffer.from(chunk));
|
|
expect(Buffer.concat(secondBytes)).toEqual(Buffer.concat(firstBytes));
|
|
expect(render).toHaveBeenCalledTimes(1);
|
|
await provider.deleteObject({ objectKey: avatarCacheKey(request) });
|
|
await consume((await createAgentAvatarService(provider, render).get(request)).stream);
|
|
expect(render).toHaveBeenCalledTimes(2);
|
|
} finally { send.mockRestore(); }
|
|
});
|
|
it("renders a PNG in an isolated worker without DOM or WebGL", async () => {
|
|
const pool = createAgentAvatarPool(1); cleanups.push(() => pool.close());
|
|
const png = await pool.render(request);
|
|
expect(await sharp(png).metadata()).toMatchObject({ width: 48, height: 48, format: "png", hasAlpha: true });
|
|
}, 20_000);
|
|
it("preserves transparent background pixels in ordinary 512px avatars", async () => {
|
|
const pool = createAgentAvatarPool(1); cleanups.push(() => pool.close());
|
|
const png = await pool.render({ ...request, size: 512, scale: 1 });
|
|
const { data, info } = await sharp(png).raw().toBuffer({ resolveWithObject: true });
|
|
expect(info).toMatchObject({ width: 512, height: 512, channels: 4 });
|
|
for (const [x, y] of [[0, 0], [511, 0], [0, 511], [511, 511]]) {
|
|
expect(data[(y * info.width + x) * info.channels + 3]).toBe(0);
|
|
}
|
|
expect(data[(256 * info.width + 256) * info.channels + 3]).toBe(255);
|
|
}, 20_000);
|
|
it("serves Slack exports on the Paperclip dark-mode color in a separate cache", async () => {
|
|
const css = await readFile(new URL("../../../ui/src/index.css", import.meta.url), "utf8");
|
|
const lightness = Number(css.match(/\.dark\s*\{\s*--background:\s*oklch\(([\d.]+) 0 0\)/)?.[1]);
|
|
// Neutral OKLCH converts to equal sRGB channels; PNG rounds to 8-bit color.
|
|
const linear = lightness ** 3;
|
|
const channel = Math.round((linear <= 0.0031308 ? 12.92 * linear : 1.055 * linear ** (1 / 2.4) - 0.055) * 255);
|
|
expect(PAPERCLIP_DARK_AVATAR_BACKGROUND).toBe(`#${channel.toString(16).padStart(2, "0").repeat(3)}`);
|
|
const service = createAgentAvatarService(await storage()); cleanups.push(() => service.close());
|
|
const url = await serve(service);
|
|
const clear = await fetch(url + "?size=512&scale=1");
|
|
const dark = await fetch(url + "?size=512&scale=1&background=paperclip-dark");
|
|
expect(dark.status).toBe(200);
|
|
expect(dark.headers.get("etag")).not.toBe(clear.headers.get("etag"));
|
|
const clearPixels = await sharp(Buffer.from(await clear.arrayBuffer())).ensureAlpha().raw().toBuffer();
|
|
const darkPixels = await sharp(Buffer.from(await dark.arrayBuffer())).ensureAlpha().raw().toBuffer();
|
|
expect(Array.from(clearPixels.subarray(0, 4))).toEqual([0, 0, 0, 0]);
|
|
expect(Array.from(darkPixels.subarray(0, 4))).toEqual([channel, channel, channel, 255]);
|
|
expect(avatarCacheKey({ ...request, background: "paperclip-dark" })).not.toBe(avatarCacheKey(request));
|
|
const invalid = await fetch(url + "?background=white");
|
|
expect(invalid.status).toBe(400); await invalid.text();
|
|
}, 20_000);
|
|
it("serves public images with content ETags and validates the finite request space", async () => {
|
|
const render = vi.fn(async () => Buffer.from("png-bytes"));
|
|
const url = await serve(createAgentAvatarService(await storage(), render));
|
|
const first = await fetch(url + "?size=24&scale=2");
|
|
expect(first.status).toBe(200);
|
|
expect(first.headers.get("content-type")).toContain("image/png");
|
|
expect(first.headers.get("cache-control")).toContain("immutable");
|
|
await first.arrayBuffer();
|
|
const second = await fetch(url + "?size=24&scale=2", { headers: { "If-None-Match": first.headers.get("etag")! } });
|
|
expect(second.status).toBe(304);
|
|
for (const suffix of ["?size=99999", "?scale=3", "?color=red", "?size=24&size=32"]) {
|
|
const invalid = await fetch(url + suffix); expect(invalid.status).toBe(400); await invalid.text();
|
|
}
|
|
expect(render).toHaveBeenCalledTimes(1);
|
|
});
|
|
it("settles stream disposal on a 304 even if the cached file disappears during open", async () => {
|
|
const service = createAgentAvatarService(await storage(), async () => Buffer.from("png"));
|
|
const stream = new Readable({
|
|
read() {},
|
|
destroy(_error, callback) { setImmediate(() => callback(new Error("cached file removed during open"))); },
|
|
});
|
|
vi.spyOn(service, "get").mockResolvedValueOnce({ stream, byteSize: 3, etag: '"cached"' });
|
|
const url = await serve(service);
|
|
const response = await fetch(url, { headers: { "If-None-Match": '"cached"' } });
|
|
expect(response.status).toBe(304);
|
|
expect(stream.closed).toBe(true);
|
|
});
|
|
it("does not cache rendering failures and permits retry", async () => {
|
|
const render = vi.fn().mockRejectedValueOnce(new Error("unavailable")).mockResolvedValue(Buffer.from("png"));
|
|
const url = await serve(createAgentAvatarService(await storage(), render));
|
|
const first = await fetch(url); expect(first.status).toBe(503); expect(first.headers.get("cache-control")).toBe("no-store"); await first.text();
|
|
const retry = await fetch(url); expect(retry.status).toBe(200); await retry.arrayBuffer();
|
|
});
|
|
});
|