@wopr-network/platform-core 1.16.0 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/dist/auth/tenant-access.test.js +2 -0
  2. package/dist/billing/crypto/evm/__tests__/config.test.js +10 -0
  3. package/dist/billing/crypto/evm/config.js +12 -0
  4. package/dist/billing/crypto/evm/types.d.ts +1 -1
  5. package/dist/db/schema/organization-members.d.ts +34 -0
  6. package/dist/db/schema/organization-members.js +2 -0
  7. package/dist/tenancy/org-member-repository.d.ts +12 -0
  8. package/dist/tenancy/org-member-repository.js +14 -0
  9. package/dist/tenancy/org-service.d.ts +10 -1
  10. package/dist/tenancy/org-service.js +39 -0
  11. package/dist/tenancy/org-service.test.js +219 -0
  12. package/dist/trpc/index.d.ts +1 -1
  13. package/dist/trpc/index.js +1 -1
  14. package/dist/trpc/init.d.ts +7 -0
  15. package/dist/trpc/init.js +16 -1
  16. package/dist/trpc/init.test.js +39 -1
  17. package/docs/superpowers/specs/2026-03-14-fleet-auto-update-design.md +300 -0
  18. package/docs/superpowers/specs/2026-03-14-paperclip-org-integration-design.md +359 -0
  19. package/docs/superpowers/specs/2026-03-14-role-permissions-design.md +346 -0
  20. package/drizzle/migrations/0006_invite_acceptance.sql +2 -0
  21. package/drizzle/migrations/meta/_journal.json +7 -0
  22. package/package.json +1 -1
  23. package/src/auth/tenant-access.test.ts +2 -0
  24. package/src/billing/crypto/evm/__tests__/config.test.ts +12 -0
  25. package/src/billing/crypto/evm/config.ts +13 -1
  26. package/src/billing/crypto/evm/types.ts +1 -1
  27. package/src/db/schema/organization-members.ts +2 -0
  28. package/src/tenancy/org-member-repository.ts +20 -0
  29. package/src/tenancy/org-service.test.ts +260 -0
  30. package/src/tenancy/org-service.ts +42 -1
  31. package/src/trpc/index.ts +1 -0
  32. package/src/trpc/init.test.ts +48 -0
  33. package/src/trpc/init.ts +18 -1
@@ -0,0 +1,300 @@
1
+ # Fleet Auto-Update with Rolling Waves
2
+
3
+ **Date:** 2026-03-14
4
+ **Status:** Draft
5
+ **Repos:** platform-core, paperclip, paperclip-platform, paperclip-platform-ui
6
+
7
+ ## Problem
8
+
9
+ Upstream Paperclip changes land nightly via `upstream-sync.mjs`, which rebases our fork and creates a PR. After manual review and merge, `docker-managed.yml` auto-builds and pushes `ghcr.io/wopr-network/paperclip:managed`. But existing running containers never receive the update. New containers get `:managed` on first pull; old containers are stuck on whatever digest they were created with.
10
+
11
+ platform-core has `ImagePoller` and `ContainerUpdater` classes that are fully implemented and tested but **not wired into the application lifecycle**.
12
+
13
+ ## Design
14
+
15
+ ### Pipeline Overview
16
+
17
+ ```
18
+ paperclipai/paperclip (upstream)
19
+ | nightly 06:00 UTC
20
+ upstream-sync.mjs (rebase + hostedMode guards + changelog generation)
21
+ | creates PR
22
+ human reviews & merges PR
23
+ | push to master
24
+ docker-managed.yml (auto-build)
25
+ | pushes ghcr.io/wopr-network/paperclip:managed
26
+ ImagePoller detects new digest
27
+ | groups bots by tenant
28
+ RolloutOrchestrator executes strategy
29
+ | per-bot update sequence
30
+ ContainerUpdater (snapshot + pull + recreate + health check + rollback)
31
+ ```
32
+
33
+ ### Human Gate
34
+
35
+ The only human checkpoint is **reviewing and merging the upstream sync PR**. Everything downstream is automatic.
36
+
37
+ ### 1. Changelog Generation (changes to `paperclip/scripts/upstream-sync.mjs`)
38
+
39
+ After rebase and hostedMode gap scanning, the sync agent generates two changelogs:
40
+
41
+ **Internal changelog** (`changelogs/internal/YYYY-MM-DD.md`):
42
+ - Full developer-facing diff summary
43
+ - What upstream changed, what guards were added, conflicts resolved
44
+ - For PR review purposes
45
+
46
+ **User-facing changelog** (`changelogs/user-facing/YYYY-MM-DD.json`):
47
+ - Structured format: `{ version, date, sections: [{ title: "New" | "Improved" | "Fixed", items: string[] }] }`
48
+ - Filtered through hosted-mode exclusion list — silently drops anything related to: adapters, model selection, thinking effort, runtime/heartbeat config, provider API keys, CLI, deployment modes, infrastructure, self-hosting
49
+ - Same `HOSTED_MODE_CONTEXT` that drives the guard scanner drives the changelog filter
50
+
51
+ Both files are committed in the sync PR. The user-facing JSON is copied into the Docker image during build (add `COPY changelogs/user-facing/ /app/changelogs/` to `Dockerfile.managed`). If the image exists, its changelog exists.
52
+
53
+ **Changelog retrieval:** After pulling a new image (before starting the update sequence), extract the changelog:
54
+
55
+ ```bash
56
+ docker run --rm ghcr.io/wopr-network/paperclip:managed cat /app/changelogs/latest.json
57
+ ```
58
+
59
+ The extracted JSON is stored in the fleet event payload for email and UI consumption. The `latest.json` symlink always points to the most recent changelog file.
60
+
61
+ ### 2. Image Detection (wire existing code in `platform-core`)
62
+
63
+ Changes to `src/fleet/services.ts`:
64
+
65
+ - Add `ImagePoller` and `ContainerUpdater` singletons
66
+ - `initFleet()` starts the poller and wires `poller.onUpdateAvailable` to `RolloutOrchestrator`
67
+ - ImagePoller already handles poll intervals per release channel (canary=5m, staging=15m, stable=30m)
68
+
69
+ ### 3. Rollout Orchestrator (new: `src/fleet/rollout-orchestrator.ts`)
70
+
71
+ GoF Strategy pattern. The orchestrator is the context; strategies are interchangeable.
72
+
73
+ **`IRolloutStrategy` interface:**
74
+
75
+ ```typescript
76
+ interface IRolloutStrategy {
77
+ /** Select next batch from remaining bots */
78
+ nextBatch(remaining: BotProfile[]): BotProfile[];
79
+ /** Milliseconds to wait between waves */
80
+ pauseDuration(): number;
81
+ /** What to do when a single bot update fails */
82
+ onBotFailure(botId: string, error: Error, attempt: number): "abort" | "skip" | "retry";
83
+ /** Max retries per bot before skip/abort */
84
+ maxRetries(): number;
85
+ /** Health check timeout per bot (ms) */
86
+ healthCheckTimeout(): number;
87
+ }
88
+ ```
89
+
90
+ **Concrete strategies:**
91
+
92
+ | Strategy | Batch | Pause | Failure | Use Case |
93
+ |----------|-------|-------|---------|----------|
94
+ | `RollingWaveStrategy` | configurable % | configurable | abort on N+ failures | Default for auto-update |
95
+ | `SingleBotStrategy` | 1 bot | N/A | report | Manual per-bot update button |
96
+ | `ImmediateStrategy` | all | 0 | skip | Emergency hotfix |
97
+
98
+ Strategy selection is **admin-controlled only** — users never see this.
99
+
100
+ **Orchestrator flow:**
101
+
102
+ ```
103
+ 1. Group update-eligible bots by tenant
104
+ 2. For each tenant:
105
+ a. Check tenant update mode (auto/manual)
106
+ b. If manual: mark bots as "update available", send notification, stop
107
+ c. If auto: check if current time is within tenant's preferred window
108
+ d. Select strategy (from admin config)
109
+ e. Execute waves:
110
+ - batch = strategy.nextBatch(remaining)
111
+ - for each bot in batch: ContainerUpdater.updateBot()
112
+ - if any failure: strategy.onBotFailure() → abort/skip/retry
113
+ - sleep(strategy.pauseDuration())
114
+ - repeat until remaining is empty
115
+ 3. Send notification emails with changelog
116
+ ```
117
+
118
+ ### 4. Update Sequence Per Bot (major rework of `ContainerUpdater`)
119
+
120
+ Nuclear rollback — image AND volumes roll back together.
121
+
122
+ **Volume Snapshot Mechanism (new: `VolumeSnapshotManager`):**
123
+
124
+ The existing `SnapshotManager` operates on filesystem paths, not Docker named volumes. A new `VolumeSnapshotManager` is needed that snapshots Docker named volumes using a temporary container:
125
+
126
+ ```bash
127
+ # Snapshot a named volume to a tar archive:
128
+ docker run --rm -v <volume-name>:/source -v <backup-dir>:/backup alpine \
129
+ tar cf /backup/<volume-name>-<timestamp>.tar -C /source .
130
+
131
+ # Restore a named volume from a tar archive:
132
+ docker run --rm -v <volume-name>:/target -v <backup-dir>:/backup alpine \
133
+ sh -c "rm -rf /target/* && tar xf /backup/<volume-name>-<timestamp>.tar -C /target"
134
+ ```
135
+
136
+ This is a new class (`src/fleet/volume-snapshot-manager.ts`), not a modification of the existing `SnapshotManager`.
137
+
138
+ **Update sequence:**
139
+
140
+ ```
141
+ 1. Snapshot /data and /paperclip volumes (via VolumeSnapshotManager)
142
+ 2. Record previous image digest (already implemented)
143
+ 3. Pull new image
144
+ 4. Stop container
145
+ 5. Recreate container with new image (named volumes remount automatically)
146
+ 6. Start container (PAPERCLIP_MIGRATION_AUTO_APPLY=true runs Drizzle migrations on boot)
147
+ 7. Health check: HTTP GET http://container:3100/health, expect {"status":"ok"}
148
+ - Timeout: 120s (increased from current 60s to allow for Drizzle migration time)
149
+ - Poll interval: 5s
150
+ 8a. HEALTHY:
151
+ - Delete volume snapshots
152
+ - Emit fleet event: bot.updated
153
+ - Record new digest
154
+ 8b. UNHEALTHY:
155
+ - Stop container
156
+ - Restore volume snapshots from step 1 (via VolumeSnapshotManager)
157
+ - Recreate container with OLD image (digest-pinned to prevent re-pulling new)
158
+ - Start container
159
+ - Verify old container is healthy
160
+ - Emit fleet event: bot.update_failed
161
+ - Report to orchestrator (abort/skip/retry per strategy)
162
+ ```
163
+
164
+ **Health check upgrade:** Replace `node -e 'process.exit(0)'` in `createContainer()` with:
165
+
166
+ ```typescript
167
+ Healthcheck: {
168
+ // Use node+fetch instead of curl — Paperclip's base image (node:lts-trixie-slim)
169
+ // may not have curl installed.
170
+ Test: ["CMD-SHELL", "node -e \"fetch('http://localhost:3100/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))\""],
171
+ Interval: 30_000_000_000,
172
+ Timeout: 10_000_000_000,
173
+ Retries: 3,
174
+ StartPeriod: 60_000_000_000, // 60s for Drizzle migrations on boot
175
+ }
176
+ ```
177
+
178
+ **Note:** `HEALTH_CHECK_TIMEOUT_MS` in `ContainerUpdater` must be increased from 60,000 to 120,000 to match the spec's 120s timeout.
179
+
180
+ ### 5. Tenant Update Config
181
+
182
+ Stored per-tenant (moves to per-org when org support ships — see `2026-03-14-paperclip-org-integration-design.md`).
183
+
184
+ ```typescript
185
+ interface TenantUpdateConfig {
186
+ /** "auto" = rolling wave in preferred window; "manual" = badge + button */
187
+ mode: "auto" | "manual";
188
+ /** Hour of day (UTC) for auto-update window. Only used when mode=auto. */
189
+ preferredHourUtc: number; // 0-23, default 3
190
+ }
191
+ ```
192
+
193
+ Default for new tenants: `{ mode: "manual", preferredHourUtc: 3 }`.
194
+
195
+ **Repository interface** (follows the `IFooRepository` pattern used throughout platform-core):
196
+
197
+ ```typescript
198
+ export interface ITenantUpdateConfigRepository {
199
+ get(tenantId: string): Promise<TenantUpdateConfig | null>;
200
+ upsert(tenantId: string, config: TenantUpdateConfig): Promise<void>;
201
+ listAutoEnabled(): Promise<Array<{ tenantId: string; config: TenantUpdateConfig }>>;
202
+ }
203
+ ```
204
+
205
+ `DrizzleTenantUpdateConfigRepository` implements this against a `tenant_update_configs` table with columns `(tenant_id TEXT PK, mode TEXT, preferred_hour_utc INTEGER, updated_at BIGINT)`.
206
+
207
+ **Audit logging:** All config changes (mode switch, hour change) are logged via `logger.info("Tenant update config changed", { tenantId, oldConfig, newConfig, actorUserId })`. Admin-triggered updates via the `/admin/updates` route include the actor in the log entry.
208
+
209
+ Admin panel can override per-tenant or set global defaults.
210
+
211
+ **Precedence: tenant config overrides per-bot `updatePolicy`.** The existing `BotProfile.updatePolicy` field (per-bot: `on-push`, `nightly`, `manual`, `cron:*`) is superseded by `TenantUpdateConfig` for hosted deployments. The `RolloutOrchestrator` reads tenant config, not bot-level policy. `ImagePoller.shouldAutoUpdate()` is refactored to always return `false` — the poller's only job is to detect new digests and notify the orchestrator, which makes the auto/manual decision based on tenant config.
212
+
213
+ `ImagePoller.isNightlyWindow()` (hardcoded 03:00-03:30 UTC) is superseded by the orchestrator's per-tenant `preferredHourUtc` window check. The poller's nightly logic becomes a no-op.
214
+
215
+ Per-bot `updatePolicy` is preserved in the schema for self-hosted (non-platform) deployments where there is no tenant config.
216
+
217
+ ### 6. Admin Controls
218
+
219
+ Admin panel (platform-core admin routes, not user-facing):
220
+
221
+ - **Global update mode**: auto / manual / paused (pause halts all rollouts fleet-wide)
222
+ - **Strategy config**: batch %, pause duration, failure threshold
223
+ - **Default update window**: hour UTC
224
+ - **Per-tenant overrides**: mode, window
225
+ - **Manual triggers**: "roll out now" for a specific image digest
226
+ - **Rollout status dashboard**: which bots updated, which failed, which pending
227
+
228
+ ### 7. User-Facing Experience
229
+
230
+ **Auto mode (tenant doesn't know or care):**
231
+ - Updates happen silently during configured window
232
+ - Email after: "Your Paperclip was updated. Here's what's new: [changelog]"
233
+ - Brief downtime during container restart (seconds)
234
+
235
+ **Manual mode:**
236
+ - Email when update available: "A new update is available for your Paperclip. [changelog]"
237
+ - In-app: badge on bot in UI indicating update available
238
+ - Click "Update" → modal shows user-facing changelog with "Update Now" / "Later" buttons
239
+ - "Update Now" triggers `SingleBotStrategy` immediately
240
+ - Email after: "Your Paperclip was updated. Here's what's new: [changelog]"
241
+
242
+ **Both modes:**
243
+ - Admin email on rollback failure
244
+ - Fleet event log for audit
245
+
246
+ ### 8. Image Allowlist
247
+
248
+ `FLEET_IMAGE_ALLOWLIST` already allows `ghcr.io/wopr-network/` — covers both WOPR and Paperclip images. Future brands add their prefix.
249
+
250
+ ## Files to Create/Modify
251
+
252
+ ### platform-core
253
+
254
+ | File | Action | Description |
255
+ |------|--------|-------------|
256
+ | `src/fleet/rollout-orchestrator.ts` | Create | Strategy pattern orchestrator |
257
+ | `src/fleet/rollout-strategies.ts` | Create | RollingWave, SingleBot, Immediate strategies |
258
+ | `src/fleet/services.ts` | Modify | Wire ImagePoller + ContainerUpdater + RolloutOrchestrator into initFleet() |
259
+ | `src/fleet/updater.ts` | Major rework | Add volume snapshot/restore lifecycle, replace FleetManager delegation with direct Docker operations for atomic update, upgrade health check from Docker HEALTHCHECK polling to HTTP GET, increase timeout from 60s to 120s |
260
+ | `src/fleet/volume-snapshot-manager.ts` | Create | Snapshot and restore Docker named volumes using temporary alpine containers |
261
+ | `src/fleet/fleet-manager.ts` | Modify | Upgrade HEALTHCHECK in createContainer() to use node+fetch instead of node -e |
262
+ | `src/fleet/image-poller.ts` | Modify | Wire onUpdateAvailable to orchestrator instead of direct updater |
263
+ | `src/db/schema/tenant-update-config.ts` | Create | Drizzle schema for tenant update preferences |
264
+ | `src/api/routes/admin-updates.ts` | Create | Admin API for update management |
265
+ | `src/fleet/update-notifier.ts` | Create | Email notifications for updates |
266
+
267
+ ### paperclip
268
+
269
+ | File | Action | Description |
270
+ |------|--------|-------------|
271
+ | `scripts/upstream-sync.mjs` | Modify | Add changelog generation step |
272
+ | `Dockerfile.managed` | Modify | COPY changelogs into image |
273
+ | `changelogs/` | Create | Directory for generated changelogs |
274
+
275
+ ### paperclip-platform-ui
276
+
277
+ | File | Action | Description |
278
+ |------|--------|-------------|
279
+ | Update modal component | Create | Shows changelog, "Update Now" / "Later" |
280
+ | Bot card badge | Modify | Show "Update Available" indicator |
281
+
282
+ ## Dependencies
283
+
284
+ - **Implementation work required:**
285
+ - `ImagePoller` and `ContainerUpdater` classes exist and are tested, but have no singleton getters in `services.ts` and are not imported or wired. Docker instance injection needs to be plumbed through.
286
+ - `ContainerUpdater` needs significant enhancement: volume snapshot/restore integration with `SnapshotManager`, HTTP-based health checks (replacing `node -e`), increased timeout from 60s to 120s for migration time.
287
+ - `RolloutOrchestrator` and strategies are entirely new code.
288
+ - `SnapshotManager` exists in `src/backup/` but has no integration with `ContainerUpdater`.
289
+ - **Future:** Org support (see `2026-03-14-paperclip-org-integration-design.md`) — update config moves from tenant to org level after org integration ships
290
+ - **Future:** Cron policy implementation in ImagePoller (currently stubbed)
291
+
292
+ ## Risks
293
+
294
+ | Risk | Mitigation |
295
+ |------|------------|
296
+ | Bad upstream migration corrupts data | Nuclear rollback: volume snapshot restored alongside image rollback |
297
+ | Upstream pushes breaking change | Human gate at sync PR review catches this before any image is built |
298
+ | Rolling wave takes too long | ImmediateStrategy available for emergency hotfixes |
299
+ | Health check passes but app is subtly broken | `/health` endpoint queries DB, so migration failures surface. Consider adding deeper health checks later. |
300
+ | Volume snapshots consume disk | Snapshots deleted after successful update. Failed rollbacks alert admin for manual cleanup. |
@@ -0,0 +1,359 @@
1
+ # Paperclip Platform Org Integration
2
+
3
+ **Date:** 2026-03-14
4
+ **Status:** Draft
5
+ **Repos:** paperclip, paperclip-platform, paperclip-platform-ui, platform-core, platform-ui-core
6
+
7
+ ## Problem
8
+
9
+ Paperclip (the bot) has a full multi-user org model: companies, memberships, invites, roles, permissions, org charts. The hosted platform layer (paperclip-platform + paperclip-platform-ui) also has an org model: tenants, organization_members, organization_invites.
10
+
11
+ Today the managed Paperclip image runs in `local_trusted` mode, which hardcodes every request as `userId: "local-board"` with instance admin privileges. There is no user identity inside the bot — everyone is the same person.
12
+
13
+ The platform is the front door: it handles auth, billing, and access. Users should manage their team exclusively through the platform. Paperclip's native invite/member UI should be hidden in hosted mode.
14
+
15
+ When a user is invited to a platform org, they should be able to use the Paperclip instance immediately — no second signup, no separate invite flow inside the bot.
16
+
17
+ ## Design
18
+
19
+ ### Architecture
20
+
21
+ ```
22
+ Platform (front door)
23
+ ├─ Auth (better-auth sessions)
24
+ ├─ Org management (invite, roles, billing)
25
+ ├─ Proxy (routes requests to Paperclip container)
26
+ │ └─ Injects: x-platform-user-id, x-platform-user-email, x-platform-user-name
27
+ └─ Provisioning (syncs membership changes into Paperclip)
28
+ └─ Calls /internal/add-member, /internal/remove-member
29
+
30
+ Paperclip (the bot, hosted_proxy mode)
31
+ ├─ actorMiddleware reads proxy headers → resolves user identity
32
+ ├─ Company/invite/member UI hidden in hostedMode
33
+ └─ All org management delegated to platform
34
+ ```
35
+
36
+ ### 1. New Deployment Mode: `hosted_proxy`
37
+
38
+ **File:** `paperclip/server/src/middleware/auth.ts`
39
+
40
+ Add a new branch in `actorMiddleware` for `hosted_proxy` deployment mode. Instead of hardcoding `local-board`, read identity from trusted proxy headers:
41
+
42
+ ```typescript
43
+ if (opts.deploymentMode === "hosted_proxy") {
44
+ const userId = req.header("x-platform-user-id");
45
+ const userEmail = req.header("x-platform-user-email");
46
+ const userName = req.header("x-platform-user-name");
47
+
48
+ if (userId) {
49
+ const [roleRow, memberships] = await Promise.all([
50
+ db.select({ id: instanceUserRoles.id })
51
+ .from(instanceUserRoles)
52
+ .where(and(
53
+ eq(instanceUserRoles.userId, userId),
54
+ eq(instanceUserRoles.role, "instance_admin")
55
+ ))
56
+ .then(rows => rows[0] ?? null),
57
+ db.select({ companyId: companyMemberships.companyId })
58
+ .from(companyMemberships)
59
+ .where(and(
60
+ eq(companyMemberships.principalType, "user"),
61
+ eq(companyMemberships.principalId, userId),
62
+ eq(companyMemberships.status, "active"),
63
+ )),
64
+ ]);
65
+
66
+ req.actor = {
67
+ type: "board",
68
+ userId,
69
+ companyIds: memberships.map(r => r.companyId),
70
+ isInstanceAdmin: Boolean(roleRow),
71
+ source: "hosted_proxy",
72
+ };
73
+ } else {
74
+ // No user header = reject (proxy should always inject)
75
+ req.actor = { type: "none", source: "none" };
76
+ }
77
+ }
78
+ ```
79
+
80
+ **Dockerfile.managed change:** `PAPERCLIP_DEPLOYMENT_MODE=hosted_proxy` (was `local_trusted`)
81
+
82
+ **Security:** The container is never exposed to the internet. Only the platform proxy can reach it. Trusting headers from the proxy is safe — same pattern as any reverse proxy auth.
83
+
84
+ ### 2. Provision Endpoints for Member Management
85
+
86
+ **File:** `paperclip/server/src/routes/provision.ts` (extend existing adapter)
87
+
88
+ The provision adapter already has `ensureUser()` and `grantAccess()`. Add new operations to the adapter interface:
89
+
90
+ **Role mapping (platform → Paperclip):**
91
+
92
+ | Platform Role | Paperclip Role | Instance Admin | Notes |
93
+ |--------------|----------------|----------------|-------|
94
+ | `owner` | `owner` | Yes | Full control |
95
+ | `admin` | `owner` | Yes | Same as owner inside Paperclip — admin distinction is platform-level (billing, org settings) |
96
+ | `member` | `member` | No | Can view/create issues, interact with agents |
97
+
98
+ **Add member:**
99
+ ```typescript
100
+ async addMember(companyId: string, user: AdminUser, role: "owner" | "admin" | "member") {
101
+ await this.ensureUser(user);
102
+ const paperclipRole = role === "member" ? "member" : "owner";
103
+ await access.ensureMembership(companyId, "user", user.id, paperclipRole, "active");
104
+ if (paperclipRole === "owner") {
105
+ await access.promoteInstanceAdmin(user.id);
106
+ }
107
+ }
108
+ ```
109
+
110
+ **Remove member:**
111
+ ```typescript
112
+ async removeMember(companyId: string, userId: string) {
113
+ // Remove company membership
114
+ await access.removeMembership(companyId, "user", userId);
115
+ // Demote instance admin if no longer owner of any company
116
+ const remaining = await access.listUserCompanyAccess(userId);
117
+ if (remaining.length === 0) {
118
+ await access.demoteInstanceAdmin(userId);
119
+ }
120
+ }
121
+ ```
122
+
123
+ **Change role:**
124
+ ```typescript
125
+ async changeRole(companyId: string, userId: string, role: "owner" | "admin" | "member") {
126
+ const paperclipRole = role === "member" ? "member" : "owner";
127
+ await access.ensureMembership(companyId, "user", userId, paperclipRole, "active");
128
+ if (paperclipRole === "owner") {
129
+ await access.promoteInstanceAdmin(userId);
130
+ } else {
131
+ // Demotion: remove instance admin if user is no longer owner of any company
132
+ const remaining = await access.listUserCompanyAccess(userId);
133
+ const isOwnerOfAny = remaining.some(c => c.role === "owner");
134
+ if (!isOwnerOfAny) {
135
+ await access.demoteInstanceAdmin(userId);
136
+ }
137
+ }
138
+ }
139
+ ```
140
+
141
+ These map to new provision-server routes:
142
+ - `POST /internal/members/add` — `{ companyId, user: { id, email, name }, role }`
143
+ - `POST /internal/members/remove` — `{ companyId, userId }`
144
+ - `POST /internal/members/change-role` — `{ companyId, userId, role }`
145
+
146
+ **All provision endpoints are idempotent.** Repeated calls with the same parameters are no-ops (`ensureUser` checks for existing records, `ensureMembership` upserts).
147
+
148
+ All authenticated via `PROVISION_SECRET` bearer token (brand-agnostic; replaces the WOPR-specific `WOPR_PROVISION_SECRET` naming).
149
+
150
+ ### 3. Platform Triggers Provisioning on Org Changes
151
+
152
+ **File:** `paperclip-platform/src/trpc/routers/org.ts`
153
+
154
+ When org membership changes happen in the platform, call the Paperclip instance's provision API:
155
+
156
+ **Invite accepted → add member** (triggered from `acceptInvite()` in org.ts — see Section 5):
157
+ ```
158
+ Platform: user accepts org invite
159
+ → acceptInvite() adds user to organization_members
160
+ → acceptInvite() resolves tenant's Paperclip instance
161
+ → POST instance:3100/internal/members/add
162
+ { companyId: <paperclip company id>, user: { id, email, name }, role: "member" }
163
+ ```
164
+
165
+ **Member removed → remove member:**
166
+ ```
167
+ Platform: admin removes member from org
168
+ → platform removes from organization_members
169
+ → POST instance:3100/internal/members/remove
170
+ { companyId: <paperclip company id>, userId }
171
+ ```
172
+
173
+ **Role changed → change role:**
174
+ ```
175
+ Platform: admin changes member role
176
+ → platform updates organization_members
177
+ → POST instance:3100/internal/members/change-role
178
+ { companyId: <paperclip company id>, userId, role }
179
+ ```
180
+
181
+ **Mapping:** Platform org → Paperclip company. The `companyId` is stored during initial provisioning (already returned by `createTenant()`). Platform stores this mapping in the tenant record.
182
+
183
+ ### 4. Hide Company/Invite/Member UI in Hosted Mode
184
+
185
+ **File:** `paperclip/scripts/upstream-sync.mjs`
186
+
187
+ Expand the `infraKeywords` list in `scanForHostedModeGaps()` to include org management patterns:
188
+
189
+ ```javascript
190
+ const infraKeywords = [
191
+ // Existing adapter/model keywords...
192
+ "adapterType", "AdapterType", /* ... */
193
+
194
+ // NEW: Org management keywords (hidden in hosted mode)
195
+ "CompanySettings",
196
+ "CompanySwitcher",
197
+ "InviteLanding",
198
+ "createInvite",
199
+ "inviteLink",
200
+ "joinRequest",
201
+ "companyMemberships",
202
+ "boardClaim",
203
+ "BoardClaim",
204
+ "manageMembers",
205
+ "instanceSettings",
206
+ ];
207
+ ```
208
+
209
+ **Specific UI changes needed in Paperclip:**
210
+
211
+ | File | Action |
212
+ |------|--------|
213
+ | `ui/src/pages/CompanySettings.tsx` | Hide invite creation, member management sections in hostedMode |
214
+ | `ui/src/components/CompanySwitcher.tsx` | Hide "Manage Companies" link, hide "Company Settings" link in hostedMode |
215
+ | `ui/src/pages/Companies.tsx` | Hide create/delete company in hostedMode (single company, platform-managed) |
216
+ | `ui/src/pages/InviteLanding.tsx` | Redirect to platform in hostedMode |
217
+ | `ui/src/pages/BoardClaim.tsx` | Redirect to platform in hostedMode |
218
+ | `ui/src/components/Layout.tsx` | Hide any "Invite" buttons in hostedMode |
219
+
220
+ **What stays visible:** The Org page (org chart, agent hierarchy) and agent management stay visible — those are product features, not org admin. Users can still see who's on the team and what agents are doing.
221
+
222
+ ### 5. Platform Org Gaps to Fix
223
+
224
+ These are existing stubs/gaps in paperclip-platform that need implementation:
225
+
226
+ **`listMyOrganizations()` (org.ts):**
227
+ Currently returns empty. Implement: query `organization_members` for user's memberships, return list of orgs.
228
+
229
+ **Invite acceptance flow:**
230
+ `inviteMember()` creates the invite, but there's no `acceptInvite()` endpoint. Implement:
231
+ 1. Validate token, check not expired/revoked
232
+ 2. Create user account if needed (signup during accept)
233
+ 3. Add to `organization_members`
234
+ 4. Call Paperclip provision API to add member (triggers the flow described in Section 3)
235
+ 5. Mark invite as accepted
236
+
237
+ **`listMyOrganizations()`:** Currently returns empty in org.ts. Implement by adding `listOrgsForUser(userId)` to platform-core's `OrgService`, then calling it from the tRPC route.
238
+
239
+ **Org switcher in platform UI:**
240
+ `x-tenant-id` header mechanism exists but isn't exposed in the frontend. Add org switcher component that:
241
+ - Lists user's orgs
242
+ - Switches `x-tenant-id` header for subsequent API calls
243
+ - Persists selection to localStorage
244
+
245
+ **Member list population:**
246
+ Org member list loads but doesn't populate user name/email. Join `organization_members` with user table to get display info.
247
+
248
+ ### 6. Proxy Header Injection
249
+
250
+ **File:** `paperclip-platform/src/proxy/tenant-proxy.ts`
251
+
252
+ The platform proxy already routes requests to Paperclip containers. Add header injection:
253
+
254
+ ```typescript
255
+ // Before proxying to the Paperclip container:
256
+ proxyReq.setHeader("x-platform-user-id", ctx.user.id);
257
+ proxyReq.setHeader("x-platform-user-email", ctx.user.email);
258
+ proxyReq.setHeader("x-platform-user-name", ctx.user.name ?? "");
259
+ ```
260
+
261
+ Strip these headers from incoming external requests to prevent spoofing:
262
+ ```typescript
263
+ // On incoming request, before auth:
264
+ req.headers.delete("x-platform-user-id");
265
+ req.headers.delete("x-platform-user-email");
266
+ req.headers.delete("x-platform-user-name");
267
+ ```
268
+
269
+ ## User Experience
270
+
271
+ ### Inviting a Teammate
272
+
273
+ 1. User goes to Platform → Settings → Team
274
+ 2. Clicks "Invite Member"
275
+ 3. Enters email, selects role (admin/member)
276
+ 4. Invitee receives email with signup/accept link
277
+ 5. Invitee creates platform account (or logs in if existing)
278
+ 6. Platform adds them to org + provisions into Paperclip instance
279
+ 7. Invitee logs in → sees the Paperclip workspace with full access
280
+
281
+ ### Day-to-Day Usage
282
+
283
+ - User logs into platform
284
+ - Platform authenticates, resolves org context
285
+ - User clicks into their Paperclip instance
286
+ - Every request carries their identity via proxy headers
287
+ - Paperclip shows their issues, their agents, their activity
288
+ - Team members see the same company but actions are attributed to the right person
289
+
290
+ ### What Users Never See
291
+
292
+ - Paperclip's native invite flow
293
+ - Paperclip's company management
294
+ - Paperclip's member management
295
+ - Any awareness that two systems exist
296
+
297
+ ## Files to Create/Modify
298
+
299
+ ### paperclip (the bot)
300
+
301
+ | File | Action | Description |
302
+ |------|--------|-------------|
303
+ | `server/src/middleware/auth.ts` | Modify | Add `hosted_proxy` deployment mode branch |
304
+ | `server/src/routes/provision.ts` | Modify | Add addMember, removeMember, changeRole to adapter |
305
+ | `Dockerfile.managed` | Modify | Change PAPERCLIP_DEPLOYMENT_MODE to hosted_proxy |
306
+ | `scripts/upstream-sync.mjs` | Modify | Expand infraKeywords for org management patterns |
307
+ | `ui/src/pages/CompanySettings.tsx` | Modify | Add hostedMode guards for invite/member sections |
308
+ | `ui/src/components/CompanySwitcher.tsx` | Modify | Hide management links in hostedMode |
309
+ | `ui/src/pages/Companies.tsx` | Modify | Hide create/delete in hostedMode |
310
+ | `ui/src/pages/InviteLanding.tsx` | Modify | Redirect to platform in hostedMode |
311
+ | `ui/src/pages/BoardClaim.tsx` | Modify | Redirect to platform in hostedMode |
312
+ | `packages/shared/src/types.ts` | Modify | Add "hosted_proxy" to DeploymentMode union |
313
+
314
+ ### paperclip-platform
315
+
316
+ | File | Action | Description |
317
+ |------|--------|-------------|
318
+ | `src/trpc/routers/org.ts` | Modify | Implement listMyOrganizations, acceptInvite, wire provisioning calls |
319
+ | `src/proxy/tenant-proxy.ts` | Modify | Inject + strip x-platform-user-* headers |
320
+ | `src/fleet/provision-client.ts` | Modify | Add addMember, removeMember, changeRole methods |
321
+
322
+ ### platform-ui-core
323
+
324
+ | File | Action | Description |
325
+ |------|--------|-------------|
326
+ | Org switcher component | Create | Switch between orgs, set x-tenant-id |
327
+ | Invite acceptance page | Create | Accept invite → create account → join org |
328
+ | Member list | Modify | Populate user name/email from joined query |
329
+
330
+ ### platform-core
331
+
332
+ | File | Action | Description |
333
+ |------|--------|-------------|
334
+ | `src/tenancy/org-service.ts` | Modify | Add listOrgsForUser(userId), acceptInvite(token) |
335
+ | provision-server package | Modify | Add member management routes to protocol |
336
+
337
+ ## Dependencies
338
+
339
+ - **Blocks:** Fleet auto-update org-level config (currently tenant-level, moves to org-level after this ships)
340
+ - **Requires:** provision-server package update for new member management routes
341
+ - **Requires:** `@paperclipai/shared` types update for `hosted_proxy` deployment mode
342
+
343
+ ## Risks
344
+
345
+ | Risk | Mitigation |
346
+ |------|------------|
347
+ | Upstream adds new invite/member UI patterns | Upstream sync scanner catches them via expanded keyword list |
348
+ | Provisioning call fails when adding member | Retry with backoff. Member shows in platform but can't access Paperclip until sync succeeds. Show "provisioning" state in UI. |
349
+ | Header spoofing | Strip x-platform-user-* headers from external requests before auth. Container not exposed to internet. |
350
+ | Paperclip upstream changes auth middleware | Our `hosted_proxy` branch is isolated — rebase conflict is localized to one function |
351
+ | User removed from platform but not from Paperclip | removeMember provision call is synchronous with platform removal. If it fails, retry with backoff. Future work: add periodic reconciliation job that compares platform org members with Paperclip company members and fixes drift. |
352
+ | Company ID mapping lost | Store Paperclip companyId in tenant record during initial provisioning. Already returned by createTenant(). |
353
+
354
+ ## Future Considerations
355
+
356
+ - **SSO/SAML:** Platform handles SSO, provisions users into Paperclip on first login via SAML callback
357
+ - **Fine-grained permissions:** Platform roles (owner/admin/member) map to Paperclip roles. Could extend to map to Paperclip's `principalPermissionGrants` for granular control.
358
+ - **Multi-instance orgs:** An org could have multiple Paperclip instances (staging/prod). Provisioning calls go to all instances.
359
+ - **Audit trail:** Platform logs all membership changes. Paperclip logs activity via `onProvisioned` callback. Both sides have audit coverage.