refactor(server): move the team runtime into a postponed package (P1.1)
Team mode stays in the repository but is not built or shipped (ADR-0003). - The team server (index.ts, config, db, daemons, plugins, proactive, routes, scheduler, services, ws, drizzle migrations) and its tests move from @waggle/server to packages/team-server (@waggle/team-server, private). It keeps its own copies of the logger and the agent-group executor, so it does not import @waggle/server. @clerk/fastify, bullmq, ioredis, postgres, drizzle-orm, drizzle-kit, cron-parser and @waggle/waggle-dance leave @waggle/server; the launcher drops the same team-only dependencies. - build:packages no longer builds waggle-dance or the worker: nothing in the community edition imports them, and the worker's project reference would otherwise build the team server. The worker now imports from team-server; the team Dockerfile builds both explicitly. - The sidecar no longer starts the team server when DATABASE_URL is set, and loses the team routes (/api/team/*, /api/teams), the WaggleDance routes and bridge (/api/waggle-dance/*), the Team server binding and egress guard, the WebSocket team client, the TeamSync cache and pushes, team audit forwarding, team workspace registration and the team capability-policy lookup in chat. The in-process room signal bus stays: agent groups, fleet runs and external tool runs use it. middleware/audit.ts was dead and is deleted. - @waggle/core loses TeamSync and the teamServer part of WaggleConfig. A teamServer key in config.json is ignored and kept on save, as P0.1 does for the tier keys. Workspaces lose teamServerUrl and teamUserId; teamId and teamRole stay because workspaces from a shared data directory still carry them and the viewer write guards read them. POST /api/workspaces ignores team fields. - The sidecar bundle no longer marks the team packages as external. The agent still references the TeamSync type in orchestrator.ts and pattern-write-back.ts; those files are being rewritten in P1.5 and drop it there. Logs UF-030 and UF-031 (team sync and team policy defects found while removing the code).
This commit is contained in:
46
packages/team-server/src/ws/connection-manager.ts
Normal file
46
packages/team-server/src/ws/connection-manager.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import type { WebSocket } from 'ws';
|
||||
import type { WsServerEvent } from '@waggle/shared';
|
||||
|
||||
export class ConnectionManager {
|
||||
private connections = new Map<string, Map<string, WebSocket>>(); // teamId -> userId -> ws
|
||||
|
||||
add(teamId: string, userId: string, ws: WebSocket): void {
|
||||
if (!this.connections.has(teamId)) {
|
||||
this.connections.set(teamId, new Map());
|
||||
}
|
||||
this.connections.get(teamId)!.set(userId, ws);
|
||||
}
|
||||
|
||||
remove(teamId: string, userId: string): void {
|
||||
this.connections.get(teamId)?.delete(userId);
|
||||
if (this.connections.get(teamId)?.size === 0) {
|
||||
this.connections.delete(teamId);
|
||||
}
|
||||
}
|
||||
|
||||
broadcast(teamId: string, event: WsServerEvent, excludeUserId?: string): void {
|
||||
const team = this.connections.get(teamId);
|
||||
if (!team) return;
|
||||
const data = JSON.stringify(event);
|
||||
for (const [userId, ws] of team) {
|
||||
if (userId !== excludeUserId && ws.readyState === ws.OPEN) {
|
||||
ws.send(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sendTo(teamId: string, userId: string, event: WsServerEvent): void {
|
||||
const ws = this.connections.get(teamId)?.get(userId);
|
||||
if (ws && ws.readyState === ws.OPEN) {
|
||||
ws.send(JSON.stringify(event));
|
||||
}
|
||||
}
|
||||
|
||||
getConnectedUsers(teamId: string): string[] {
|
||||
return Array.from(this.connections.get(teamId)?.keys() ?? []);
|
||||
}
|
||||
|
||||
getTeamCount(): number {
|
||||
return this.connections.size;
|
||||
}
|
||||
}
|
||||
264
packages/team-server/src/ws/gateway.ts
Normal file
264
packages/team-server/src/ws/gateway.ts
Normal file
@@ -0,0 +1,264 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { WsClientEvent, WsServerEvent } from '@waggle/shared';
|
||||
import { createClerkClient, verifyToken } from '@clerk/fastify';
|
||||
import { ConnectionManager } from './connection-manager.js';
|
||||
import { teams, messages, users } from '../db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { TeamService } from '../services/team-service.js';
|
||||
|
||||
export const connectionManager = new ConnectionManager();
|
||||
|
||||
/**
|
||||
* Validate that a string looks like a JWT (3 dot-separated base64url segments).
|
||||
* This is a structural check, not a cryptographic verification.
|
||||
*/
|
||||
function isJwtStructure(token: string): boolean {
|
||||
const parts = token.split('.');
|
||||
if (parts.length !== 3) return false;
|
||||
// Each segment should be non-empty and valid base64url
|
||||
const base64urlRegex = /^[A-Za-z0-9_-]+$/;
|
||||
return parts.every((part) => part.length > 0 && base64urlRegex.test(part));
|
||||
}
|
||||
|
||||
// Overridable verifier for testing — allows tests to swap in a mock
|
||||
export type WsTokenVerifier = (token: string) => Promise<{ sub: string }>;
|
||||
let _wsTokenVerifier: WsTokenVerifier | null = null;
|
||||
|
||||
/** Override the WS token verifier (for testing only) */
|
||||
export function setWsTokenVerifier(verifier: WsTokenVerifier | null): void {
|
||||
_wsTokenVerifier = verifier;
|
||||
}
|
||||
|
||||
export async function wsGateway(fastify: FastifyInstance) {
|
||||
const teamService = new TeamService(fastify.db);
|
||||
|
||||
// Create Clerk client for JWT verification if secret key is available
|
||||
const clerkSecretKey = fastify.config.clerkSecretKey;
|
||||
const clerk = clerkSecretKey
|
||||
? createClerkClient({ secretKey: clerkSecretKey })
|
||||
: null;
|
||||
|
||||
// Hard fail in production if no JWT verification path is configured. Without
|
||||
// either a Clerk client or a test override, the gateway would have no way to
|
||||
// cryptographically verify incoming tokens — the previous fallback (unsigned
|
||||
// JWT decode) is forgeable. NODE_ENV !== 'production' (dev / desktop /
|
||||
// testing) is allowed to start without the key, but connections without a
|
||||
// verifier are rejected at request time below.
|
||||
if (!clerk && !_wsTokenVerifier && process.env.NODE_ENV === 'production') {
|
||||
throw new Error(
|
||||
'[ws-gateway] CLERK_SECRET_KEY required in production. WebSocket gateway ' +
|
||||
'refuses to start without cryptographic JWT verification configured. ' +
|
||||
'Set CLERK_SECRET_KEY in environment or settings.json.',
|
||||
);
|
||||
}
|
||||
if (!clerk && !_wsTokenVerifier) {
|
||||
fastify.log.warn(
|
||||
'[ws-gateway] CLERK_SECRET_KEY absent; WebSocket connections will be rejected. ' +
|
||||
'This is dev/desktop mode — set CLERK_SECRET_KEY for cloud/SaaS deployments.',
|
||||
);
|
||||
}
|
||||
|
||||
fastify.get('/ws', { websocket: true }, (socket, request) => {
|
||||
let userId: string | null = null;
|
||||
let teamId: string | null = null;
|
||||
|
||||
socket.on('message', async (raw: Buffer | string) => {
|
||||
try {
|
||||
const event = JSON.parse(raw.toString()) as WsClientEvent;
|
||||
|
||||
switch (event.type) {
|
||||
case 'authenticate': {
|
||||
const token = event.token;
|
||||
|
||||
// Reject tokens that are not JWTs (plain userIds, random strings)
|
||||
if (!isJwtStructure(token)) {
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Invalid token: must be a valid JWT',
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
let sub: string;
|
||||
|
||||
if (_wsTokenVerifier) {
|
||||
// Test override path
|
||||
const result = await _wsTokenVerifier(token);
|
||||
sub = result.sub;
|
||||
} else if (clerk) {
|
||||
// Production path: full Clerk JWT verification. `clerk` is only
|
||||
// non-null when clerkSecretKey is set, so the secretKey below is
|
||||
// always present here.
|
||||
const payload = await verifyToken(token, { secretKey: clerkSecretKey });
|
||||
sub = payload.sub;
|
||||
} else {
|
||||
// No verifier available — reject. The startup check above ensures
|
||||
// production cannot reach this branch. Dev/desktop without a key
|
||||
// hits this and is rejected at connection time (no silent decode).
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Authentication unavailable: server has no JWT verifier configured',
|
||||
}),
|
||||
);
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Look up internal user by Clerk ID
|
||||
const [user] = await fastify.db
|
||||
.select()
|
||||
.from(users)
|
||||
.where(eq(users.clerkId, sub));
|
||||
|
||||
if (!user) {
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'User not found',
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (teamId && userId) {
|
||||
connectionManager.remove(teamId, userId);
|
||||
}
|
||||
teamId = null;
|
||||
userId = user.id;
|
||||
socket.send(JSON.stringify({ type: 'authenticated', userId }));
|
||||
} catch {
|
||||
socket.send(
|
||||
JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Token verification failed',
|
||||
}),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'join_team': {
|
||||
if (!userId) {
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Not authenticated' }));
|
||||
return;
|
||||
}
|
||||
|
||||
const [team] = await fastify.db
|
||||
.select()
|
||||
.from(teams)
|
||||
.where(eq(teams.slug, event.teamSlug));
|
||||
|
||||
if (!team) {
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Team not found' }));
|
||||
return;
|
||||
}
|
||||
|
||||
const membership = await teamService.getMembership(team.id, userId);
|
||||
if (!membership) {
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Team not found' }));
|
||||
return;
|
||||
}
|
||||
|
||||
// Leave previous team if any
|
||||
if (teamId && userId) {
|
||||
connectionManager.remove(teamId, userId);
|
||||
}
|
||||
|
||||
teamId = team.id;
|
||||
connectionManager.add(teamId, userId, socket);
|
||||
|
||||
// Subscribe to Redis channel for this team
|
||||
const channel = `team:${teamId}:waggle`;
|
||||
try {
|
||||
const existingChannels = await fastify.redisSub.call('PUBSUB', 'CHANNELS', channel) as string[];
|
||||
// Only subscribe if not already subscribed (redisSub is shared)
|
||||
if (!existingChannels || !existingChannels.includes(channel)) {
|
||||
await fastify.redisSub.subscribe(channel);
|
||||
}
|
||||
} catch {
|
||||
// Redis subscribe failure is non-fatal — real-time updates won't work
|
||||
// but join should still succeed
|
||||
}
|
||||
|
||||
socket.send(JSON.stringify({ type: 'joined_team', teamSlug: event.teamSlug }));
|
||||
break;
|
||||
}
|
||||
|
||||
case 'send_message': {
|
||||
if (!userId || !teamId) {
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Not in a team' }));
|
||||
return;
|
||||
}
|
||||
|
||||
// Persist message
|
||||
const [msg] = await fastify.db
|
||||
.insert(messages)
|
||||
.values({
|
||||
teamId,
|
||||
senderId: userId,
|
||||
type: event.messageType,
|
||||
subtype: event.subtype,
|
||||
content: event.content,
|
||||
})
|
||||
.returning();
|
||||
|
||||
// Publish to Redis for cross-process delivery
|
||||
await fastify.redis.publish(
|
||||
`team:${teamId}:waggle`,
|
||||
JSON.stringify(msg),
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Invalid message' }));
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('close', () => {
|
||||
if (teamId && userId) {
|
||||
connectionManager.remove(teamId, userId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Forward Redis pub/sub messages to WebSocket clients
|
||||
fastify.redisSub.on('message', (channel: string, data: string) => {
|
||||
const match = channel.match(/^team:(.+):waggle$/);
|
||||
if (!match) return;
|
||||
const tId = match[1];
|
||||
try {
|
||||
const event: WsServerEvent = { type: 'waggle_message', message: JSON.parse(data) };
|
||||
connectionManager.broadcast(tId, event);
|
||||
} catch {
|
||||
// Ignore malformed messages from Redis
|
||||
}
|
||||
});
|
||||
|
||||
// Subscribe to job progress channels via pattern matching
|
||||
await fastify.redisSub.psubscribe('job:*:progress');
|
||||
|
||||
// Forward job progress events to WebSocket clients
|
||||
fastify.redisSub.on('pmessage', (_pattern: string, channel: string, data: string) => {
|
||||
const jobMatch = channel.match(/^job:(.+):progress$/);
|
||||
if (!jobMatch) return;
|
||||
try {
|
||||
const parsed = JSON.parse(data);
|
||||
const event: WsServerEvent = {
|
||||
type: 'job_progress',
|
||||
jobId: jobMatch[1],
|
||||
progress: parsed,
|
||||
};
|
||||
// Route to the team that owns this job
|
||||
if (parsed.teamId) {
|
||||
connectionManager.broadcast(parsed.teamId, event);
|
||||
}
|
||||
} catch {
|
||||
// Ignore malformed messages from Redis
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user