mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-14 00:26:28 +01:00
## Thinking Path > - Paperclip manages work for AI-agent companies. > - Chat connections deliver provider messages and user answers. > - Slack requires a response within its original three-second deadline. > - Forms and their delivery receipts must survive retries and tenant restarts. > - This change uses the existing connector queue and durable form owners. > - Current authorization, exact receipt checks and normal ACK rules stay in force. > - The tenant can settle useful work while keeping the connection enabled. ## Linked Issues or Issue Description Refs #19019 and #19348. Companion Cloud PR: paperclipai/paperclip-cloud#1250. ## What Changed - Preserve form grants, sessions, encrypted evidence, terminal receipts and company-authorized Retry. - Check initial form enrollment before the provider send. Keep a typed preparation wait only while an exact current enrollment obligation exists. - Refund the attempt only when the claimed attempt and credential lease still match and provider invocation has not started. Possible or accepted sends retain uncertain-delivery custody. An ordinary error with the same text still uses the normal five-attempt limit. - Notify the existing publication worker when normal enrollment commits. Reconstruct pending work from durable state after a lost reply or owner restart. - Page settled history before proving idle safety. Keep unfinished, malformed and foreign receipts visible beyond the first 1,000 rows. - Keep Slack callback, answer, receipt and ACK rules in their provider owners. Support body-bound Cloud idle capabilities when a complete catalog exceeds the header budget. - Add focused regression coverage, an enabled quiet/reactivation recipe and explicit fixture port checks. - Keep SDK evidence tests tied to a normally built reviewed reference, with a separate strict refusal for actual unreviewed CI outputs. ## Verification App head: `6284d6881b5dc38a9ee149bcbde35e69a67dd8ca`, including actual master `dff8bed007e01c32f3826144b893ec80603d1335`. Companion Cloud head: `f83b38187dc7e6b2e219fe865dc16d268cc66fdb`, including actual Cloud master `b1dcc3421a91efffee631a61536cf48f357c22fc`. Cloud #1250 is merged at `1d07d81f23d4627ab7b6dcf49176b4128a2f8f9b` after exact-head passing checks and complete Greptile 5/5. Current exact-head CI typecheck, build, tests and aggregate verification are green; the complete edited Greptile review is 5/5 and all 13 review threads and nested comment pages are resolved. - Both latest enrollment findings were reproduced against the preceding product. The pure readiness control returned no wait for the first enrollment. The real-PostgreSQL control observed a provider call while that enrollment was still staged. - The repaired source passes three whole files: 21 readiness controls, 80 real-PostgreSQL milestone controls and 37 tenant-actor middleware controls. All 138 pass with zero excluded or TODO cases. The SQL case reconstructs the worker through seven waits, preserves the attempt budget, recovers a controlled lost enrollment reply, and commits one provider-port call and one exact grant. Replaying the selected attempt does not duplicate them. Authorization, SDK and enrollment-response ports are controlled in that SQL case. - The same files cover ordinary five-attempt exhaustion, accepted and possible sends, stale ownership, malformed bindings, attention states and current actor restrictions. One synthetic mismatched-binding parameter uses generation 2 against current generation 1; it is not evidence for an older-generation control. - The normal server-package typecheck passes at the measured source. It runs the normal Runner TypeScript, protocol, SDK and declaration prerequisites and all seven main App programs. The separate declaration configuration is also recorded. A worker union-narrowing error was found, fixed with an explicit callable/null branch, and verified by that normal compiler. No cast or test relaxation was used. - The normal merge of current master preserves all 78 owned paths and 15,783 unrelated master entries and modes. The three measured test files and their changed feature paths are unchanged. New upstream DB code adds only the normal nonunique concurrent native-session index and its migration classification. Shared and adapter-utils changes alter unrelated prompt or denial wording. The 138-case proof carries for those unchanged behaviors; it is not reported as a rerun after the merge. The normal Shared/SDK ensure, DB build, adapter-utils build and complete server-package check also pass at the current merged head. Their fresh inventory contains 8,143 outputs; canonical SQL and journal copies match. This build did not apply the new index migration. - Earlier real-PostgreSQL retained-history coverage reproduced the missing proof with 1,005 settled rows. Its repair retains the existing two-second/64-query inspection budget, current endpoint bound and exact owner snapshot. The 46 source-bound controls cover unfinished and invalid tails, exact cross-page action references, Retry, rollback and lost receipt responses. - Earlier enabled quiet/reactivation coverage produced ordinary useful work, kept configuration enabled, accepted fresh signed work plus a duplicate, and produced exactly one second useful reply. Its two App-worker windows lasted at least 30 seconds with zero measured PG writes, connection or outbound activity after settlement. That evidence stays tied to its measured revision. A missing worker EXIT marker limits that earlier proof; parent evidence does not replace it. - Earlier three-second callback, terminal ACK, current-question race and complete body-size controls remain source-bound. The 500-owner/1,000-binding body measured 313,875 bytes with a 618-byte JWT; signature tampering failed closed. - Final managed-provider acceptance now passes all five fresh Node boots: A/A, A/AB, B/A rejection, B/AB recovery, B/B final. Two independently configured, challenged, verified, linked and enrolled Slack identities exercise ACK and retirement debt separately. Normal provider factory/materialization delivers the configuration through a controlled external stack-provider boundary. The registered opening returns HTTP 200 in 21 ms within the original 3-second deadline. Two answers persist, normal ACK is accepted, exact retirement and lost acceptance/GC replies recover, and the original canonical snapshot remains unchanged without duplicate provider sends or submissions. - The separate complete committed-preflight case passes. A committed `update_attempted` response is lost before SDK entry; normal service-owner reconstruction after the original lease/deadline recovers the same useful update and one answer without a second trigger. This reconstructs service owners within one App process; the separate five fresh Node boots cover process restart and managed signing-key rotation. - Assertion-time PG socket reports balance 15 opens/15 closes across the five boots and 5/5 in preflight, peak 2 and zero live sockets or unexpected attempts. Worker terminal EXIT markers were missing and are not replaced by parent EXIT markers. These are domain/recovery and assertion-time connection proofs, not a new whole-worker terminal quiet measurement. All known owners/groups closed; independent SQL audits were empty; only successful fixture databases were removed. Earlier failed fixture databases/logs remain retained. No product guard, deadline or assertion was weakened. Exact runtime source and normal outputs are unchanged by the three test files in the current follow-up, so these source-bound proofs carry without repeating those journeys. - The preceding App CI ran 11,312 support cases: 11,289 passed, three SDK-evidence cases failed, and 20 external cases skipped. Normal CI dependency setup resolved six stale Slack patch-hash references and produced an unreviewed lock. The public SDK proof correctly refused that lock; the tests incorrectly assumed the CI output was a reviewed reference. The three-file test repair keeps the protected lock policy, source/compiler/parser/Zod pins, scanner and redaction rules unchanged. It builds an owned reviewed reference using the existing normal Shared/SDK producers for the positive seal test, while a separate negative requires actual unreviewed-build privacy refusal and no retained SDK bytes. It does not relabel the CI build as certified or admit a new lock hash. - All four whole SDK support files pass locally: public type 30, evidence 16, public-input synchronizer 8 and lock preparation 10, totaling 64 with zero excluded/TODO cases in 28.73 seconds. The genuine fresh reference build/positive seal and separate invalidated-reference privacy refusal passed within their original 120-second bounds. Original installed dependencies, SDK outputs/cache and runtime source stayed unchanged; normal global setup produced only its own Runner JavaScript outputs. PostgreSQL was denied throughout, all four worker markers were observed, and the owned process group/private test root closed. The local canonical-lock negative mutates only its own reference; fresh remote CI also passes the actual resolved-CI-lock branch: all 16 evidence and 30 public-type tests pass, including the genuine reviewed-reference positive and the strict actual-unreviewed-build refusal. The upstream synchronizer file runs its current 10 cases in CI. - The first complete CI attempt on this head also exposed two separate 30-second timeouts: one real reporter-artifact test and the `agent-test-environment` module-loading hook. Full original logs remain preserved. Their exact test, sanitizer and evidence source blobs are unchanged from the preceding head; inspection found no deterministic setup cause. After the original run finished, one retry of only the failed jobs passed on the same synthetic merge `168851e210671ebe97e7a778e8cf891807e80502`: Runner support reports 11,304 passes and 20 external skips; serialized shard 6 reports all 395 passes, including all 67 route bodies. No source, timeout or assertion changed. This proves recovery; the original causes remain unexplained. - The final pre-merge tree audit uses actual master `17136a6c25ef6cb7c816a29140a4c48c2c75fb31` (refreshed after the earlier `85dec046` audit). The expected normal squash preserves all 81 owned paths and 15,816 unrelated master entries and modes. Only OpenAPI has edits from both sides, and its complete content composition preserves the new project route and token help alongside this PR's API entries. The shared inbound selection changes preserve the same candidate set; new concurrent indexes, Native/workspace behavior and shutdown changes remain upstream behavior. The scoped form proof uses process agents and does not qualify the new Native/sandbox branches. The latest question-schema change still accepts the explicit version-1 payload exercised here. The last master delta also changes managed AI/Native credential refresh and writeback; those branches are outside the measured process-agent form paths. Existing 8,143 local outputs remain tied to their measured revision; no new whole-tree output equality or deployed suspension is claimed. - Reproduce the public path with `doc/testing/slack-cloud-qualification.md`, the companion Cloud checkout, Node 24/22 and fresh owned fixture databases. Aggregate local `pnpm -r typecheck`, `pnpm test:run` and `pnpm build` were not rerun as one command set. Current exact-head required CI and the complete Greptile 5/5 review now pass; the full edited summaries and all paginated threads were read. Local worker quiet does not prove deployed host or Neon suspension. Photon and Discord remain separate work; this PR completes the described Slack slice. ## Risks Use compatible App and Cloud readers before transferring custody. Signatures prove authenticity; current authorization and exact ownership still control execution. Preserve old targets, secrets and terminal facts until their normal owners release them. Keep the managed seal identity stable while custody remains. The required Slack adapter patch remains pinned. Upstream's concurrent index uses the normal top-level migration path. ## Model Used OpenAI `gpt-6.1-sol` implemented and tested the changes. A GPT-6-based Codex managing agent reviewed and integrated them. The host does not expose the managing agent's exact model ID or context-window sizes. The agents used code execution and test tools. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used, its version and tool capabilities - [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 linked public related issues or PRs - [x] I have not referenced internal or instance-local Paperclip issues or links - [x] My branch name describes the change and contains no internal Paperclip ticket id - [x] I have run the local tests listed above and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation - [x] I have considered and documented risks - [x] All Paperclip CI gates are green for the current head - [x] Greptile is 5/5 with no open findings for the current head - [x] I will address all reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
594 lines
28 KiB
TypeScript
594 lines
28 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import express, { type Request } from "express";
|
|
import request from "supertest";
|
|
import { and, eq } from "drizzle-orm";
|
|
import type { Db } from "@paperclipai/db";
|
|
import {
|
|
authUsers,
|
|
companies,
|
|
companyMemberships,
|
|
instanceSettings,
|
|
instanceUserRoles,
|
|
principalPermissionGrants,
|
|
} from "@paperclipai/db";
|
|
import { actorMiddleware, cloudActorHeaderSourceFromHeaders, resolveCloudTenantActor } from "./auth.js";
|
|
import { idleAdmissionMiddleware, trackIdleRequestHandlers } from "./idle-admission.js";
|
|
import { idleWorkSnapshot, startTaskDrain, stopTaskDrain } from "../services/task-admission.js";
|
|
import { readIdleSleepSafety } from "../services/idle-sleep-safety.js";
|
|
|
|
// Minimal fake Drizzle Db: records every table passed to .insert() / .delete() and
|
|
// supports the chained call shapes used by resolveCloudTenantActor (values /
|
|
// onConflictDo* / returning().then() / delete().where()), plus the
|
|
// select().from(table).where() reads: instanceSettings for the owner-elevation
|
|
// flag resolution through instanceSettingsService, and companyMemberships for
|
|
// the user's own membership rows (rows configurable via membershipQueryRows,
|
|
// where-conditions captured in selectWheres). The chain is awaitable so
|
|
// directly-awaited statements resolve.
|
|
function createFakeDb(options: {
|
|
membershipRow?: { companyId: string; membershipRole: string; status: string };
|
|
membershipQueryRows?: Array<{ companyId: string; membershipRole: string | null; status: string }>;
|
|
settingsRow?: Record<string, unknown> | null;
|
|
selectThrows?: boolean;
|
|
beforeWrite?: (table: unknown) => Promise<void>;
|
|
beforeRead?: (table: unknown) => Promise<void>;
|
|
unchangedMembership?: boolean;
|
|
} = {}) {
|
|
const settingsRow =
|
|
options.settingsRow === undefined
|
|
? {
|
|
id: "00000000-0000-0000-0000-000000000001",
|
|
singletonKey: "default",
|
|
defaultEnvironmentId: null,
|
|
general: {},
|
|
experimental: {},
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
}
|
|
: options.settingsRow;
|
|
const insertedTables: unknown[] = [];
|
|
const deletedTables: unknown[] = [];
|
|
const selectWheres: Array<{ table: unknown; condition: unknown; locked?: boolean }> = [];
|
|
function writeChain(table: unknown) {
|
|
let values: unknown;
|
|
const chain: Record<string, unknown> = {};
|
|
chain.values = (value: unknown) => { values = value; return chain; };
|
|
chain.onConflictDoUpdate = () => chain;
|
|
chain.onConflictDoNothing = () => chain;
|
|
chain.where = () => chain;
|
|
chain.returning = async () => {
|
|
await options.beforeWrite?.(table);
|
|
return options.unchangedMembership && table === companyMemberships ? [] : [options.membershipRow ?? values];
|
|
};
|
|
chain.then = (resolve: (v: unknown) => unknown, reject?: (error: unknown) => unknown) =>
|
|
Promise.resolve().then(() => options.beforeWrite?.(table)).then(resolve, reject);
|
|
return chain;
|
|
}
|
|
const db = {
|
|
transaction: async (work: (tx: Db) => Promise<unknown>) => work(db),
|
|
insert: (table: unknown) => {
|
|
insertedTables.push(table);
|
|
return writeChain(table);
|
|
},
|
|
delete: (table: unknown) => {
|
|
deletedTables.push(table);
|
|
return writeChain(table);
|
|
},
|
|
select: () => {
|
|
return {
|
|
from: (table: unknown) => ({
|
|
// Pending Muse bindings use a joined read; this fixture has none.
|
|
innerJoin() { return this; },
|
|
where: (condition: unknown) => {
|
|
// Only settings lookup is degradable. Membership locking and
|
|
// permission seeding must still work in the fail-closed flag test.
|
|
if (options.selectThrows && table === instanceSettings) {
|
|
throw new Error("select unavailable");
|
|
}
|
|
const selected: (typeof selectWheres)[number] = { table, condition };
|
|
selectWheres.push(selected);
|
|
const rows =
|
|
table === instanceSettings && settingsRow
|
|
? [settingsRow]
|
|
: table === companyMemberships
|
|
? (options.membershipQueryRows ?? [])
|
|
: [];
|
|
let locked = false;
|
|
const result = {
|
|
orderBy: () => ({ limit: () => result }),
|
|
for: () => { locked = true; selected.locked = true; return result; },
|
|
then: (resolve: (v: unknown) => unknown, reject?: (error: unknown) => unknown) => {
|
|
// The optional cross-company read can fail independently of
|
|
// the primary membership transaction, which must succeed.
|
|
const query = options.selectThrows && table === companyMemberships && !locked
|
|
? Promise.reject(new Error("select unavailable"))
|
|
: Promise.resolve(rows);
|
|
return Promise.resolve().then(() => options.beforeRead?.(table)).then(() => query).then(resolve, reject);
|
|
},
|
|
};
|
|
return result;
|
|
},
|
|
}),
|
|
};
|
|
},
|
|
} as unknown as Db;
|
|
return { db, insertedTables, deletedTables, selectWheres };
|
|
}
|
|
|
|
function settingsRowWith(experimental: Record<string, unknown>) {
|
|
return {
|
|
id: "00000000-0000-0000-0000-000000000001",
|
|
singletonKey: "default",
|
|
defaultEnvironmentId: null,
|
|
general: {},
|
|
experimental,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
};
|
|
}
|
|
|
|
function fakeReq(headers: Record<string, string>): Request {
|
|
const lower: Record<string, string> = {};
|
|
for (const [k, v] of Object.entries(headers)) lower[k.toLowerCase()] = v;
|
|
return { header: (name: string) => lower[name.toLowerCase()] } as unknown as Request;
|
|
}
|
|
|
|
const VALID_HEADERS = {
|
|
"x-paperclip-cloud-tenant-token": "test-server-token",
|
|
"x-paperclip-cloud-user-id": "user-123",
|
|
"x-paperclip-cloud-user-email": "Owner@Example.com",
|
|
"x-paperclip-cloud-stack-id": "stack-abc",
|
|
"x-paperclip-cloud-stack-role": "owner",
|
|
};
|
|
|
|
const MANAGED_CONFIG_FLAG_ON = JSON.stringify({
|
|
v: 1,
|
|
mode: "cloud",
|
|
catalogVersion: "test",
|
|
features: { enableOwnerInstanceAdmin: true },
|
|
plugins: { autoInstall: [] },
|
|
});
|
|
|
|
const MANAGED_CONFIG_FLAG_OFF = JSON.stringify({
|
|
v: 1,
|
|
mode: "cloud",
|
|
catalogVersion: "test",
|
|
features: { enableOwnerInstanceAdmin: false },
|
|
plugins: { autoInstall: [] },
|
|
});
|
|
|
|
describe("resolveCloudTenantActor (shared-pool hardening)", () => {
|
|
beforeEach(() => {
|
|
process.env.PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN = "test-server-token";
|
|
});
|
|
afterEach(() => {
|
|
stopTaskDrain();
|
|
delete process.env.PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN;
|
|
delete process.env.PAPERCLIP_MANAGED_CONFIG;
|
|
});
|
|
|
|
it("does not grant instance admin by default (flag off)", async () => {
|
|
const { db, insertedTables } = createFakeDb();
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor).not.toBeNull();
|
|
expect(actor!.isInstanceAdmin).toBe(false);
|
|
expect(insertedTables).not.toContain(instanceUserRoles);
|
|
});
|
|
|
|
it("stays scoped to exactly the stack's company when the user has no other membership rows", async () => {
|
|
const { db } = createFakeDb();
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor!.companyIds).toHaveLength(1);
|
|
expect(actor!.memberships).toHaveLength(1);
|
|
expect(actor?.memberships?.[0]?.companyId).toBe(actor?.companyIds?.[0]);
|
|
expect(actor?.memberships?.[0]?.membershipRole).toBe("owner");
|
|
expect(actor!.source).toBe("cloud_tenant");
|
|
});
|
|
|
|
it("settles its concurrent reads before a failed purge leaves authentication", async () => {
|
|
let settingsReadStarted = false;
|
|
let settingsReadSettled = false;
|
|
const { db } = createFakeDb({
|
|
beforeWrite: async (table) => {
|
|
if (table === instanceUserRoles) throw new Error("purge failed");
|
|
},
|
|
beforeRead: async (table) => {
|
|
if (table !== instanceSettings) return;
|
|
settingsReadStarted = true;
|
|
await new Promise((resolve) => setTimeout(resolve, 50));
|
|
settingsReadSettled = true;
|
|
},
|
|
});
|
|
await expect(resolveCloudTenantActor(db, fakeReq(VALID_HEADERS))).rejects.toThrow("purge failed");
|
|
// No started read may still be running once the error surfaces.
|
|
expect(settingsReadStarted).toBe(true);
|
|
expect(settingsReadSettled).toBe(true);
|
|
});
|
|
|
|
it("purges stale instance_admin rows left by pre-hardening deployments", async () => {
|
|
const { db, deletedTables } = createFakeDb();
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor).not.toBeNull();
|
|
expect(deletedTables).toContain(instanceUserRoles);
|
|
});
|
|
|
|
it("still upserts the user, company, and membership", async () => {
|
|
const { db, insertedTables } = createFakeDb();
|
|
await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(insertedTables).toContain(authUsers);
|
|
expect(insertedTables).toContain(companies);
|
|
expect(insertedTables).toContain(companyMemberships);
|
|
});
|
|
|
|
it.each(["/api/health", "/api/instance/task-drain", "/api/instance/task-drain/status"])("counts unfinished authentication on exempt %s requests", async (path) => {
|
|
let enterWrite!: () => void, finishWrite!: () => void;
|
|
const entered = new Promise<void>(resolve => { enterWrite = resolve; });
|
|
const pending = new Promise<void>(resolve => { finishWrite = resolve; });
|
|
const { db, insertedTables } = createFakeDb({ beforeWrite: async () => { enterWrite(); await pending; } });
|
|
const server = express();
|
|
server.use(idleAdmissionMiddleware);
|
|
server.use(actorMiddleware(db, { deploymentMode: "authenticated", resolveSession: async () => null }));
|
|
const hold = startTaskDrain({ purpose: "idle", ttlMs: 60_000 });
|
|
const emptyDb = { transaction: async (run: (tx: unknown) => Promise<unknown>) =>
|
|
run({ execute: async () => [{ blocked: false }] }) } as unknown as Db;
|
|
server.get("/api/health", (_req, res) => res.sendStatus(204));
|
|
server.get("/api/instance/task-drain", async (_req, res) => {
|
|
res.json(await readIdleSleepSafety(emptyDb, () => ({ ...hold, draining: true, activeRuns: 0, pendingWakes: 0 }),
|
|
Date.now, hold.ownerId, async () => "none"));
|
|
});
|
|
server.post("/api/instance/task-drain/status", (_req, res) => res.sendStatus(204));
|
|
trackIdleRequestHandlers(server);
|
|
const accepted = (path.endsWith("/status") ? request(server).post(path) : request(server).get(path)).set(VALID_HEADERS).then(response => response);
|
|
try {
|
|
await entered;
|
|
expect(idleWorkSnapshot().active).toBeGreaterThan(0);
|
|
const during = await request(server).get("/api/instance/task-drain").expect(200);
|
|
expect(during.body.backgroundWork).toBe("unknown");
|
|
} finally {
|
|
finishWrite();
|
|
await accepted;
|
|
}
|
|
expect(insertedTables).toEqual(expect.arrayContaining([authUsers, companies, companyMemberships]));
|
|
expect(idleWorkSnapshot().active).toBe(0);
|
|
const after = await request(server).get("/api/instance/task-drain").expect(200);
|
|
expect(after.body.backgroundWork).toBe("none");
|
|
});
|
|
|
|
it("releases failed control authentication and preserves Express error handling", async () => {
|
|
const { db } = createFakeDb({ beforeWrite: async () => { throw new Error("fixture write failed"); } });
|
|
const server = express();
|
|
server.use(idleAdmissionMiddleware);
|
|
server.use(actorMiddleware(db, { deploymentMode: "authenticated", resolveSession: async () => null }));
|
|
server.get("/api/health", (_req, res) => res.sendStatus(204));
|
|
server.use((_err: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => res.sendStatus(500));
|
|
trackIdleRequestHandlers(server);
|
|
await request(server).get("/api/health").set(VALID_HEADERS).expect(500);
|
|
expect(idleWorkSnapshot().active).toBe(0);
|
|
await request(server).get("/api/health").expect(204);
|
|
});
|
|
|
|
it("resyncs an A to B to A context transition inside the debounce window", async () => {
|
|
const { db, insertedTables } = createFakeDb();
|
|
const contextA = VALID_HEADERS;
|
|
const contextB = { ...VALID_HEADERS, "x-paperclip-cloud-stack-role": "member" };
|
|
|
|
await resolveCloudTenantActor(db, fakeReq(contextA));
|
|
await resolveCloudTenantActor(db, fakeReq(contextB));
|
|
await resolveCloudTenantActor(db, fakeReq(contextA));
|
|
|
|
expect(insertedTables.filter((table) => table === authUsers)).toHaveLength(3);
|
|
expect(insertedTables.filter((table) => table === companyMemberships)).toHaveLength(3);
|
|
});
|
|
|
|
it("coalesces concurrent identical syncs while keeping authority reads and stale-admin purges per request", async () => {
|
|
let release!: () => void;
|
|
let entered!: () => void;
|
|
const blocked = new Promise<void>(resolve => { release = resolve; });
|
|
const started = new Promise<void>(resolve => { entered = resolve; });
|
|
const { db, insertedTables, deletedTables, selectWheres } = createFakeDb({
|
|
beforeWrite: async table => { if (table === authUsers) { entered(); await blocked; } },
|
|
});
|
|
const requests = Array.from({ length: 20 }, () => resolveCloudTenantActor(db, fakeReq(VALID_HEADERS)));
|
|
await started;
|
|
expect(insertedTables.filter(table => table === authUsers)).toHaveLength(1);
|
|
release();
|
|
const actors = await Promise.all(requests);
|
|
expect(insertedTables.filter(table => table === companyMemberships)).toHaveLength(1);
|
|
expect(deletedTables.filter(table => table === instanceUserRoles)).toHaveLength(20);
|
|
expect(selectWheres.filter(entry => entry.table === instanceSettings)).toHaveLength(20);
|
|
expect(selectWheres.filter(entry => entry.table === companyMemberships && !entry.locked)).toHaveLength(20);
|
|
expect(selectWheres.filter(entry => entry.table === companyMemberships && entry.locked)).toHaveLength(1);
|
|
expect(actors.every(actor => actor?.memberships?.[0]?.membershipRole === "owner")).toBe(true);
|
|
});
|
|
|
|
it("serializes concurrent A to B to A without sharing the wrong role", async () => {
|
|
let release!: () => void;
|
|
let entered!: () => void;
|
|
const blocked = new Promise<void>(resolve => { release = resolve; });
|
|
const started = new Promise<void>(resolve => { entered = resolve; });
|
|
let writes = 0;
|
|
const { db, insertedTables } = createFakeDb({ beforeWrite: async table => {
|
|
if (table === authUsers && ++writes === 1) { entered(); await blocked; }
|
|
} });
|
|
const first = resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
await started;
|
|
const second = resolveCloudTenantActor(db, fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": "member" }));
|
|
const third = resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
release();
|
|
const actors = await Promise.all([first, second, third]);
|
|
expect(actors.map(actor => actor?.memberships?.[0]?.membershipRole)).toEqual(["owner", "member", "owner"]);
|
|
expect(insertedTables.filter(table => table === companyMemberships)).toHaveLength(3);
|
|
});
|
|
|
|
it("does not block another user behind a slow user's synchronization", async () => {
|
|
let release!: () => void;
|
|
let entered!: () => void;
|
|
const blocked = new Promise<void>(resolve => { release = resolve; });
|
|
const started = new Promise<void>(resolve => { entered = resolve; });
|
|
let firstWrite = true;
|
|
const { db } = createFakeDb({ beforeWrite: async table => {
|
|
if (table === authUsers && firstWrite) { firstWrite = false; entered(); await blocked; }
|
|
} });
|
|
const slow = resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
await started;
|
|
try {
|
|
const other = await resolveCloudTenantActor(db, fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-user-id": "other-user" }));
|
|
expect(other?.userId).toBe("other-user");
|
|
} finally {
|
|
release();
|
|
await slow;
|
|
}
|
|
});
|
|
|
|
it("does not debounce a failed partial context transition or its repair", async () => {
|
|
let fail = false;
|
|
const { db, insertedTables } = createFakeDb({ beforeWrite: async table => {
|
|
if (fail && table === companyMemberships) throw new Error("partial sync failed");
|
|
} });
|
|
await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
fail = true;
|
|
await expect(resolveCloudTenantActor(db, fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": "member" })))
|
|
.rejects.toThrow("partial sync failed");
|
|
fail = false;
|
|
const repaired = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(repaired?.memberships?.[0]?.membershipRole).toBe("owner");
|
|
expect(insertedTables.filter(table => table === companyMemberships)).toHaveLength(3);
|
|
});
|
|
|
|
it("retries a shared transient failure and completes default grants once", async () => {
|
|
let failed = false;
|
|
const { db, insertedTables } = createFakeDb({ beforeWrite: async table => {
|
|
if (!failed && table === companyMemberships) {
|
|
failed = true;
|
|
throw Object.assign(new Error("synthetic connection loss"), { code: "CONNECTION_CLOSED" });
|
|
}
|
|
} });
|
|
const actors = await Promise.all(Array.from({ length: 10 }, () => resolveCloudTenantActor(db, fakeReq(VALID_HEADERS))));
|
|
expect(actors.every(actor => actor?.memberships?.[0]?.membershipRole === "owner")).toBe(true);
|
|
expect(insertedTables.filter(table => table === companyMemberships)).toHaveLength(2);
|
|
expect(insertedTables.filter(table => table === principalPermissionGrants)).toHaveLength(1);
|
|
});
|
|
|
|
it("keeps default-grant repair and the input role when a guarded upsert returns no row", async () => {
|
|
const { db, insertedTables } = createFakeDb({ unchangedMembership: true });
|
|
const actor = await resolveCloudTenantActor(db, fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": "member" }));
|
|
expect(actor?.memberships?.[0]?.membershipRole).toBe("member");
|
|
expect(insertedTables).toContain(principalPermissionGrants);
|
|
});
|
|
|
|
it("does not share completed syncs across databases or companies", async () => {
|
|
const first = createFakeDb();
|
|
const second = createFakeDb();
|
|
await Promise.all([resolveCloudTenantActor(first.db, fakeReq(VALID_HEADERS)), resolveCloudTenantActor(second.db, fakeReq(VALID_HEADERS))]);
|
|
await resolveCloudTenantActor(first.db, fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-id": "stack-other" }));
|
|
expect(first.insertedTables.filter(table => table === companyMemberships)).toHaveLength(2);
|
|
expect(second.insertedTables.filter(table => table === companyMemberships)).toHaveLength(1);
|
|
});
|
|
|
|
it("resyncs when the requested company name changes", async () => {
|
|
const { db, insertedTables } = createFakeDb();
|
|
await resolveCloudTenantActor(db, fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-paperclip-company-name": "First" }));
|
|
await resolveCloudTenantActor(db, fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-paperclip-company-name": "Second" }));
|
|
expect(insertedTables.filter(table => table === companyMemberships)).toHaveLength(2);
|
|
});
|
|
|
|
it("resumes membership and grant repair after the existing five-second debounce expires", async () => {
|
|
const now = Date.now();
|
|
const clock = vi.spyOn(Date, "now").mockReturnValue(now);
|
|
try {
|
|
const { db, insertedTables } = createFakeDb();
|
|
await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(insertedTables.filter(table => table === companyMemberships)).toHaveLength(1);
|
|
clock.mockReturnValue(now + 5_001);
|
|
await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(insertedTables.filter(table => table === companyMemberships)).toHaveLength(2);
|
|
expect(insertedTables.filter(table => table === principalPermissionGrants)).toHaveLength(2);
|
|
} finally { clock.mockRestore(); }
|
|
});
|
|
|
|
it("returns null when the server token is unset", async () => {
|
|
delete process.env.PAPERCLIP_CLOUD_TENANT_SERVER_TOKEN;
|
|
const { db } = createFakeDb();
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor).toBeNull();
|
|
});
|
|
|
|
it("resolves identically from a raw upgrade-request header map via the shim", async () => {
|
|
// Websocket upgrades hand us IncomingMessage.headers (lowercased keys,
|
|
// possibly string[] values), not an Express Request. The shim must feed
|
|
// resolveCloudTenantActor the same way Express header() does.
|
|
const { db } = createFakeDb();
|
|
const rawHeaders: Record<string, string | string[] | undefined> = {};
|
|
for (const [k, v] of Object.entries(VALID_HEADERS)) rawHeaders[k.toLowerCase()] = v;
|
|
rawHeaders["x-paperclip-cloud-user-name"] = ["Cloud Owner", "ignored-duplicate"];
|
|
const actor = await resolveCloudTenantActor(db, cloudActorHeaderSourceFromHeaders(rawHeaders));
|
|
expect(actor).not.toBeNull();
|
|
expect(actor!.userId).toBe("user-123");
|
|
expect(actor!.userName).toBe("Cloud Owner");
|
|
expect(actor!.companyIds).toHaveLength(1);
|
|
});
|
|
|
|
it("maps a non-owner stack role through to the membership without elevating", async () => {
|
|
const { db } = createFakeDb({
|
|
membershipRow: { companyId: "company-y", membershipRole: "member", status: "active" },
|
|
});
|
|
const actor = await resolveCloudTenantActor(
|
|
db,
|
|
fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": "member" }),
|
|
);
|
|
expect(actor!.isInstanceAdmin).toBe(false);
|
|
expect(actor?.memberships?.[0]?.membershipRole).toBe("member");
|
|
});
|
|
|
|
it.each(["admin", "member", "support"])(
|
|
"grants experimental settings access only to the admin stack role (%s)",
|
|
async (stackRole) => {
|
|
const { db, insertedTables } = createFakeDb();
|
|
const actor = await resolveCloudTenantActor(
|
|
db,
|
|
fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": stackRole }),
|
|
);
|
|
expect(actor?.canManageExperimentalSettings).toBe(stackRole === "admin");
|
|
expect(actor?.isInstanceAdmin).toBe(false);
|
|
expect(insertedTables).not.toContain(instanceUserRoles);
|
|
},
|
|
);
|
|
|
|
it("removes experimental settings access on the next request after a stack admin is demoted", async () => {
|
|
const { db } = createFakeDb();
|
|
const admin = await resolveCloudTenantActor(
|
|
db,
|
|
fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": "admin" }),
|
|
);
|
|
const member = await resolveCloudTenantActor(
|
|
db,
|
|
fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": "member" }),
|
|
);
|
|
expect(admin?.canManageExperimentalSettings).toBe(true);
|
|
expect(member?.canManageExperimentalSettings).toBe(false);
|
|
});
|
|
|
|
describe("company membership union", () => {
|
|
// The primary company id is derived from the stack id; resolve it once
|
|
// through the same code path instead of duplicating the hash here.
|
|
async function resolvePrimaryCompanyId() {
|
|
const { db } = createFakeDb();
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
return actor!.companyIds![0]!;
|
|
}
|
|
|
|
it("unions the pinned primary with the user's own active membership rows, primary first and deduped", async () => {
|
|
const primaryCompanyId = await resolvePrimaryCompanyId();
|
|
const { db } = createFakeDb({
|
|
membershipQueryRows: [
|
|
// The user's own row for the primary company comes back from the
|
|
// query too — it must not appear twice.
|
|
{ companyId: primaryCompanyId, membershipRole: "owner", status: "active" },
|
|
{ companyId: "company-imported", membershipRole: "member", status: "active" },
|
|
],
|
|
});
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor!.companyIds).toEqual([primaryCompanyId, "company-imported"]);
|
|
expect(actor!.memberships).toEqual([
|
|
{ companyId: primaryCompanyId, membershipRole: "owner", status: "active" },
|
|
{ companyId: "company-imported", membershipRole: "member", status: "active" },
|
|
]);
|
|
});
|
|
|
|
it("loads memberships with the session path's exact own-user active-status filter", async () => {
|
|
const { db, selectWheres } = createFakeDb();
|
|
await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
const membershipSelects = selectWheres.filter((entry) => entry.table === companyMemberships);
|
|
expect(membershipSelects).toHaveLength(2);
|
|
// One read locks the primary membership before its upsert; the other,
|
|
// which runs concurrently, loads the user's active memberships for
|
|
// request authorization. Rows for other users and rows in any
|
|
// non-active status are excluded by the query itself: the filter binds
|
|
// exactly this user id and the "active" status — the same condition
|
|
// the session path applies.
|
|
const authorizationReads = membershipSelects.filter((entry) => !entry.locked);
|
|
expect(authorizationReads).toHaveLength(1);
|
|
expect(authorizationReads[0]!.condition).toEqual(
|
|
and(
|
|
eq(companyMemberships.principalType, "user"),
|
|
eq(companyMemberships.principalId, "user-123"),
|
|
eq(companyMemberships.status, "active"),
|
|
),
|
|
);
|
|
});
|
|
|
|
it("degrades to the primary company when the membership read fails", async () => {
|
|
const { db } = createFakeDb({ selectThrows: true });
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor).not.toBeNull();
|
|
expect(actor!.companyIds).toHaveLength(1);
|
|
expect(actor!.memberships).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe("owner instance-admin elevation (enableOwnerInstanceAdmin)", () => {
|
|
it("elevates the owner while the flag is enabled, still without any role row", async () => {
|
|
const { db, insertedTables, deletedTables } = createFakeDb({
|
|
settingsRow: settingsRowWith({ enableOwnerInstanceAdmin: true }),
|
|
});
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor!.isInstanceAdmin).toBe(true);
|
|
expect(actor!.source).toBe("cloud_tenant");
|
|
// Elevation is computed, never persisted: no instance_user_roles insert,
|
|
// and the stale-row purge still runs on every authentication.
|
|
expect(insertedTables).not.toContain(instanceUserRoles);
|
|
expect(deletedTables).toContain(instanceUserRoles);
|
|
});
|
|
|
|
it("does not elevate the owner while the flag is disabled", async () => {
|
|
const { db } = createFakeDb({
|
|
settingsRow: settingsRowWith({ enableOwnerInstanceAdmin: false }),
|
|
});
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor!.isInstanceAdmin).toBe(false);
|
|
});
|
|
|
|
it.each(["member", "admin", "support"] as const)(
|
|
"never elevates the %s stack role even with the flag enabled",
|
|
async (stackRole) => {
|
|
const { db } = createFakeDb({
|
|
membershipRow: { companyId: "company-y", membershipRole: "member", status: "active" },
|
|
settingsRow: settingsRowWith({ enableOwnerInstanceAdmin: true }),
|
|
});
|
|
const actor = await resolveCloudTenantActor(
|
|
db,
|
|
fakeReq({ ...VALID_HEADERS, "x-paperclip-cloud-stack-role": stackRole }),
|
|
);
|
|
expect(actor).not.toBeNull();
|
|
expect(actor!.isInstanceAdmin).toBe(false);
|
|
},
|
|
);
|
|
|
|
it("resolves the flag through the managed overlay: overlay on elevates over a DB value of off", async () => {
|
|
process.env.PAPERCLIP_MANAGED_CONFIG = MANAGED_CONFIG_FLAG_ON;
|
|
const { db } = createFakeDb({
|
|
settingsRow: settingsRowWith({ enableOwnerInstanceAdmin: false }),
|
|
});
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor!.isInstanceAdmin).toBe(true);
|
|
});
|
|
|
|
it("resolves the flag through the managed overlay: overlay off wins over a DB value of on", async () => {
|
|
process.env.PAPERCLIP_MANAGED_CONFIG = MANAGED_CONFIG_FLAG_OFF;
|
|
const { db } = createFakeDb({
|
|
settingsRow: settingsRowWith({ enableOwnerInstanceAdmin: true }),
|
|
});
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor!.isInstanceAdmin).toBe(false);
|
|
});
|
|
|
|
it("fails closed when the settings read errors: actor resolves without elevation", async () => {
|
|
const { db, deletedTables } = createFakeDb({ selectThrows: true });
|
|
const actor = await resolveCloudTenantActor(db, fakeReq(VALID_HEADERS));
|
|
expect(actor).not.toBeNull();
|
|
expect(actor!.isInstanceAdmin).toBe(false);
|
|
expect(deletedTables).toContain(instanceUserRoles);
|
|
});
|
|
});
|
|
});
|