forked from farhoodlabs/paperclip
3d15798c22
## Thinking Path > - Paperclip orchestrates AI agents for zero-human companies > - The external adapter plugin system (#2218) lets adapters ship as npm modules loaded via `server/src/adapters/plugin-loader.ts`; since #4296 merged, each `ServerAdapterModule` can declare `sessionManagement` (`supportsSessionResume`, `nativeContextManagement`, `defaultSessionCompaction`) and have it preserved through the init-time load via the new `resolveExternalAdapterRegistration` helper > - #4296 fixed the init-time IIFE path at `server/src/adapters/registry.ts:363-369` but noted that the hot-install path at `server/src/routes/adapters.ts:174 registerWithSessionManagement` still unconditionally overwrites module-provided `sessionManagement` during `POST /api/adapters/install` > - Practical impact today: an external adapter installed via the API needs a Paperclip restart before its declared `sessionManagement` takes effect — the IIFE runs on next boot and preserves it, but until then the hot-install overwrite wins > - This PR closes that parity gap: `registerWithSessionManagement` delegates to the same `resolveExternalAdapterRegistration` helper introduced by #4296, unifying both load paths behind one resolver > - The benefit is consistent behaviour between cold-start and hot-install: no "install then restart" ritual; declared `sessionManagement` on an external module is honoured the moment `POST /api/adapters/install` returns 201 ## What Changed - `server/src/routes/adapters.ts`: `registerWithSessionManagement` delegates to the exported `resolveExternalAdapterRegistration` helper (added in #4296). Honours module-provided `sessionManagement` first, falls back to host registry lookup, defaults `undefined`. Updated the section comment to document the parity-with-IIFE intent. - `server/src/routes/adapters.ts`: dropped the now-unused `getAdapterSessionManagement` import. - `server/src/adapters/registry.ts`: updated the JSDoc on `resolveExternalAdapterRegistration` — previously said "Exported for unit tests; runtime callers use the IIFE below", now says the helper is used by both the init-time IIFE and the hot-install path in `routes/adapters.ts`. Addresses Greptile C1. - `server/src/__tests__/adapter-routes.test.ts`: new integration test — installs a mocked external adapter module carrying a non-trivial `sessionManagement` declaration and asserts `findServerAdapter(type).sessionManagement` preserves it after `POST /api/adapters/install` returns 201. - `server/src/__tests__/adapter-routes.test.ts`: added `findServerAdapter` to the shared test-scope variable set so the new test can inspect post-install registry state. ## Verification Targeted test runs from a clean tree on `fix/external-session-management-hot-install` (rebased onto current `upstream/master` now that #4296 has merged): - `pnpm test server/src/__tests__/adapter-routes.test.ts` — 6 passed (new test + 5 pre-existing) - `pnpm test server/src/__tests__/adapter-registry.test.ts` — 15 passed (ensures the IIFE path from #4296 continues to behave correctly) - `pnpm -w run test` full workspace suite — 1923 passed / 1 skipped (unrelated skip) End-to-end smoke on file: [`@superbiche/cline-paperclip-adapter@0.1.1`](https://www.npmjs.com/package/@superbiche/cline-paperclip-adapter) and [`@superbiche/qwen-paperclip-adapter@0.1.1`](https://www.npmjs.com/package/@superbiche/qwen-paperclip-adapter), both public on npm, both declare `sessionManagement`. With this PR in place, the "restart after install" step disappears — the declared compaction policy is active immediately after the install response. ## Risks - Low risk. The change replaces an inline mutation with a call to a helper that already has dedicated unit coverage (#4296 added three tests for `resolveExternalAdapterRegistration` covering module-provided, registry-fallback, and undefined paths). Behaviour is a strict superset of the prior path — externals that did not declare `sessionManagement` continue to get the hardcoded-registry lookup; externals that did declare it now have those values preserved instead of overwritten. - No migration impact. The stored plugin records (`~/.paperclip/adapter-plugins.json`) are unchanged. Existing hot-installed adapters behave correctly before and after. - No behavioural change for builtin adapters; they hit `registerServerAdapter` directly and never flow through `registerWithSessionManagement`. ## Model Used - Provider and model: Claude (Anthropic) via Claude Code - Model ID: `claude-opus-4-7` (1M context) - Reasoning mode: standard (no extended thinking on this PR) - Tool use: yes — file edits, subprocess invocations for builds/tests/git via the Claude Code harness ## 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 run tests locally and they pass - [x] I have added or updated tests where applicable - [x] If this change affects the UI, I have included before/after screenshots (N/A — server-only change) - [x] I have updated relevant documentation to reflect my changes (the JSDoc on `resolveExternalAdapterRegistration` and the section comment above `registerWithSessionManagement` now document the parity-with-IIFE intent) - [x] I have considered and documented any risks above - [x] I will address all Greptile and reviewer comments before requesting merge
282 lines
11 KiB
TypeScript
282 lines
11 KiB
TypeScript
import express from "express";
|
|
import request from "supertest";
|
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
import { vi } from "vitest";
|
|
import type { ServerAdapterModule } from "../adapters/index.js";
|
|
|
|
const mockAdapterPluginStore = vi.hoisted(() => ({
|
|
listAdapterPlugins: vi.fn(),
|
|
addAdapterPlugin: vi.fn(),
|
|
removeAdapterPlugin: vi.fn(),
|
|
getAdapterPluginByType: vi.fn(),
|
|
getAdapterPluginsDir: vi.fn(),
|
|
getDisabledAdapterTypes: vi.fn(),
|
|
setAdapterDisabled: vi.fn(),
|
|
}));
|
|
|
|
const mockPluginLoader = vi.hoisted(() => ({
|
|
buildExternalAdapters: vi.fn(),
|
|
loadExternalAdapterPackage: vi.fn(),
|
|
getUiParserSource: vi.fn(),
|
|
getOrExtractUiParserSource: vi.fn(),
|
|
reloadExternalAdapter: vi.fn(),
|
|
}));
|
|
|
|
const overridingConfigSchemaAdapter: ServerAdapterModule = {
|
|
type: "claude_local",
|
|
execute: async () => ({ exitCode: 0, signal: null, timedOut: false }),
|
|
testEnvironment: async () => ({
|
|
adapterType: "claude_local",
|
|
status: "pass",
|
|
checks: [],
|
|
testedAt: new Date(0).toISOString(),
|
|
}),
|
|
getConfigSchema: async () => ({
|
|
version: 1,
|
|
fields: [
|
|
{
|
|
key: "mode",
|
|
type: "text",
|
|
label: "Mode",
|
|
},
|
|
],
|
|
}),
|
|
};
|
|
|
|
let registerServerAdapter: typeof import("../adapters/registry.js").registerServerAdapter;
|
|
let unregisterServerAdapter: typeof import("../adapters/registry.js").unregisterServerAdapter;
|
|
let findServerAdapter: typeof import("../adapters/registry.js").findServerAdapter;
|
|
let setOverridePaused: typeof import("../adapters/registry.js").setOverridePaused;
|
|
let adapterRoutes: typeof import("../routes/adapters.js").adapterRoutes;
|
|
let errorHandler: typeof import("../middleware/index.js").errorHandler;
|
|
|
|
function registerModuleMocks() {
|
|
vi.doMock("node:child_process", async () => vi.importActual("node:child_process"));
|
|
vi.doMock("../adapters/plugin-loader.js", () => mockPluginLoader);
|
|
vi.doMock("../services/adapter-plugin-store.js", () => mockAdapterPluginStore);
|
|
vi.doMock("../routes/adapters.js", async () => vi.importActual("../routes/adapters.js"));
|
|
vi.doMock("../routes/authz.js", async () => vi.importActual("../routes/authz.js"));
|
|
vi.doMock("../middleware/index.js", async () => vi.importActual("../middleware/index.js"));
|
|
}
|
|
|
|
function createApp(actorOverrides: Partial<Express.Request["actor"]> = {}) {
|
|
const app = express();
|
|
app.use(express.json());
|
|
app.use((req, _res, next) => {
|
|
(req as any).actor = {
|
|
type: "board",
|
|
userId: "local-board",
|
|
companyIds: [],
|
|
source: "local_implicit",
|
|
isInstanceAdmin: false,
|
|
...actorOverrides,
|
|
};
|
|
next();
|
|
});
|
|
app.use("/api", adapterRoutes());
|
|
app.use(errorHandler);
|
|
return app;
|
|
}
|
|
|
|
describe("adapter routes", () => {
|
|
beforeEach(async () => {
|
|
vi.resetModules();
|
|
vi.doUnmock("node:child_process");
|
|
vi.doUnmock("../adapters/registry.js");
|
|
vi.doUnmock("../adapters/plugin-loader.js");
|
|
vi.doUnmock("../services/adapter-plugin-store.js");
|
|
vi.doUnmock("../routes/adapters.js");
|
|
vi.doUnmock("../routes/authz.js");
|
|
vi.doUnmock("../middleware/index.js");
|
|
registerModuleMocks();
|
|
mockAdapterPluginStore.listAdapterPlugins.mockReturnValue([]);
|
|
mockAdapterPluginStore.addAdapterPlugin.mockResolvedValue(undefined);
|
|
mockAdapterPluginStore.removeAdapterPlugin.mockReturnValue(false);
|
|
mockAdapterPluginStore.getAdapterPluginByType.mockReturnValue(undefined);
|
|
mockAdapterPluginStore.getAdapterPluginsDir.mockReturnValue("/tmp/paperclip-adapter-routes-test");
|
|
mockAdapterPluginStore.getDisabledAdapterTypes.mockReturnValue([]);
|
|
mockAdapterPluginStore.setAdapterDisabled.mockReturnValue(false);
|
|
mockPluginLoader.buildExternalAdapters.mockResolvedValue([]);
|
|
mockPluginLoader.loadExternalAdapterPackage.mockResolvedValue(null);
|
|
mockPluginLoader.getUiParserSource.mockResolvedValue(null);
|
|
mockPluginLoader.getOrExtractUiParserSource.mockResolvedValue(null);
|
|
mockPluginLoader.reloadExternalAdapter.mockResolvedValue(null);
|
|
const [registry, routes, middleware] = await Promise.all([
|
|
vi.importActual<typeof import("../adapters/registry.js")>("../adapters/registry.js"),
|
|
import("../routes/adapters.js"),
|
|
import("../middleware/index.js"),
|
|
]);
|
|
registerServerAdapter = registry.registerServerAdapter;
|
|
unregisterServerAdapter = registry.unregisterServerAdapter;
|
|
findServerAdapter = registry.findServerAdapter;
|
|
setOverridePaused = registry.setOverridePaused;
|
|
adapterRoutes = routes.adapterRoutes;
|
|
errorHandler = middleware.errorHandler;
|
|
setOverridePaused("claude_local", false);
|
|
unregisterServerAdapter("claude_local");
|
|
registerServerAdapter(overridingConfigSchemaAdapter);
|
|
});
|
|
|
|
afterEach(() => {
|
|
setOverridePaused("claude_local", false);
|
|
unregisterServerAdapter("claude_local");
|
|
});
|
|
|
|
it("GET /api/adapters includes capabilities object for each adapter", async () => {
|
|
const app = createApp();
|
|
|
|
const res = await request(app).get("/api/adapters");
|
|
expect(res.status).toBe(200);
|
|
const adapters = Array.isArray(res.body) ? res.body : JSON.parse(res.text);
|
|
expect(Array.isArray(adapters)).toBe(true);
|
|
expect(adapters.length).toBeGreaterThan(0);
|
|
|
|
// Every adapter should have a capabilities object
|
|
for (const adapter of adapters) {
|
|
expect(adapter.capabilities).toBeDefined();
|
|
expect(typeof adapter.capabilities.supportsInstructionsBundle).toBe("boolean");
|
|
expect(typeof adapter.capabilities.supportsSkills).toBe("boolean");
|
|
expect(typeof adapter.capabilities.supportsLocalAgentJwt).toBe("boolean");
|
|
expect(typeof adapter.capabilities.requiresMaterializedRuntimeSkills).toBe("boolean");
|
|
}
|
|
});
|
|
|
|
it("GET /api/adapters returns correct capabilities for built-in adapters", async () => {
|
|
const app = createApp();
|
|
|
|
const res = await request(app).get("/api/adapters");
|
|
expect(res.status).toBe(200);
|
|
|
|
// codex_local has instructions bundle + skills + jwt, no materialized skills
|
|
// (claude_local is overridden by beforeEach, so check codex_local instead)
|
|
const codexLocal = res.body.find((a: any) => a.type === "codex_local");
|
|
expect(codexLocal).toBeDefined();
|
|
expect(codexLocal.capabilities).toMatchObject({
|
|
supportsInstructionsBundle: true,
|
|
supportsSkills: true,
|
|
supportsLocalAgentJwt: true,
|
|
requiresMaterializedRuntimeSkills: false,
|
|
});
|
|
|
|
// process adapter should have no local capabilities
|
|
const processAdapter = res.body.find((a: any) => a.type === "process");
|
|
expect(processAdapter).toBeDefined();
|
|
expect(processAdapter.capabilities).toMatchObject({
|
|
supportsInstructionsBundle: false,
|
|
supportsSkills: false,
|
|
supportsLocalAgentJwt: false,
|
|
requiresMaterializedRuntimeSkills: false,
|
|
});
|
|
|
|
// cursor adapter should require materialized runtime skills
|
|
const cursorAdapter = res.body.find((a: any) => a.type === "cursor");
|
|
expect(cursorAdapter).toBeDefined();
|
|
expect(cursorAdapter.capabilities.requiresMaterializedRuntimeSkills).toBe(true);
|
|
expect(cursorAdapter.capabilities.supportsInstructionsBundle).toBe(true);
|
|
|
|
// hermes_local currently supports skills + local JWT, but not the managed
|
|
// instructions bundle flow because the bundled adapter does not consume
|
|
// instructionsFilePath at runtime.
|
|
const hermesAdapter = res.body.find((a: any) => a.type === "hermes_local");
|
|
expect(hermesAdapter).toBeDefined();
|
|
expect(hermesAdapter.capabilities).toMatchObject({
|
|
supportsInstructionsBundle: false,
|
|
supportsSkills: true,
|
|
supportsLocalAgentJwt: true,
|
|
requiresMaterializedRuntimeSkills: false,
|
|
});
|
|
});
|
|
|
|
it("GET /api/adapters derives supportsSkills from listSkills/syncSkills presence", async () => {
|
|
const app = createApp();
|
|
|
|
const res = await request(app).get("/api/adapters");
|
|
expect(res.status).toBe(200);
|
|
|
|
// http adapter has no listSkills/syncSkills
|
|
const httpAdapter = res.body.find((a: any) => a.type === "http");
|
|
expect(httpAdapter).toBeDefined();
|
|
expect(httpAdapter.capabilities.supportsSkills).toBe(false);
|
|
|
|
// codex_local has listSkills/syncSkills
|
|
const codexLocal = res.body.find((a: any) => a.type === "codex_local");
|
|
expect(codexLocal).toBeDefined();
|
|
expect(codexLocal.capabilities.supportsSkills).toBe(true);
|
|
});
|
|
|
|
it("uses the active adapter when resolving config schema for a paused builtin override", async () => {
|
|
const app = createApp();
|
|
|
|
const active = await request(app).get("/api/adapters/claude_local/config-schema");
|
|
expect(active.status, JSON.stringify(active.body)).toBe(200);
|
|
expect(active.body).toMatchObject({
|
|
fields: [{ key: "mode" }],
|
|
});
|
|
|
|
const paused = await request(app)
|
|
.patch("/api/adapters/claude_local/override")
|
|
.send({ paused: true });
|
|
expect(paused.status, JSON.stringify(paused.body)).toBe(200);
|
|
|
|
const builtin = await request(app).get("/api/adapters/claude_local/config-schema");
|
|
expect([200, 404], JSON.stringify(builtin.body)).toContain(builtin.status);
|
|
expect(builtin.body).not.toMatchObject({
|
|
fields: [{ key: "mode" }],
|
|
});
|
|
});
|
|
|
|
it("rejects signed-in users without org access", async () => {
|
|
const app = createApp({
|
|
userId: "outsider-1",
|
|
source: "session",
|
|
companyIds: [],
|
|
memberships: [],
|
|
isInstanceAdmin: false,
|
|
});
|
|
|
|
const res = await request(app).get("/api/adapters/claude_local/config-schema");
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(403);
|
|
});
|
|
|
|
it("POST /api/adapters/install preserves module-provided sessionManagement (hot-install parity with init-time IIFE)", async () => {
|
|
const HOT_INSTALL_TYPE = "hot_install_session_test";
|
|
const declaredSessionManagement = {
|
|
supportsSessionResume: true,
|
|
nativeContextManagement: "confirmed" as const,
|
|
defaultSessionCompaction: {
|
|
enabled: true,
|
|
maxSessionRuns: 10,
|
|
maxRawInputTokens: 100_000,
|
|
maxSessionAgeHours: 24,
|
|
},
|
|
};
|
|
const externalModule: ServerAdapterModule = {
|
|
type: HOT_INSTALL_TYPE,
|
|
execute: async () => ({ exitCode: 0, signal: null, timedOut: false }),
|
|
testEnvironment: async () => ({
|
|
adapterType: HOT_INSTALL_TYPE,
|
|
status: "pass",
|
|
checks: [],
|
|
testedAt: new Date(0).toISOString(),
|
|
}),
|
|
sessionManagement: declaredSessionManagement,
|
|
};
|
|
mockPluginLoader.loadExternalAdapterPackage.mockResolvedValue(externalModule);
|
|
|
|
const app = createApp({ isInstanceAdmin: true });
|
|
const res = await request(app)
|
|
.post("/api/adapters/install")
|
|
.send({ packageName: "/tmp/fake-hot-install-adapter", isLocalPath: true });
|
|
|
|
expect(res.status, JSON.stringify(res.body)).toBe(201);
|
|
expect(res.body.type).toBe(HOT_INSTALL_TYPE);
|
|
|
|
const registered = findServerAdapter(HOT_INSTALL_TYPE);
|
|
expect(registered).not.toBeNull();
|
|
expect(registered?.sessionManagement).toEqual(declaredSessionManagement);
|
|
|
|
unregisterServerAdapter(HOT_INSTALL_TYPE);
|
|
});
|
|
});
|