Files
waggle-os/packages/worker/src/index.ts
shumeiko.it c25efe6c44 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).
2026-10-24 12:17:41 +02:00

132 lines
4.4 KiB
TypeScript

import { Worker } from 'bullmq';
import { resolve } from 'node:path';
import { pathToFileURL } from 'node:url';
import { createDb } from '../../team-server/src/db/connection.js';
import { agentJobs } from '../../team-server/src/db/schema.js';
import { eq } from 'drizzle-orm';
import { JobProcessor, type JobData } from './job-processor.js';
import Redis from 'ioredis';
import { chatHandler } from './handlers/chat-handler.js';
import { taskHandler } from './handlers/task-handler.js';
import { groupHandler } from './handlers/group-handler.js';
import { createCronHandler } from './handlers/cron-handler.js';
import { createWaggleHandler } from './handlers/waggle-handler.js';
import { JobService } from '../../team-server/src/services/job-service.js';
const REDIS_URL = process.env.REDIS_URL ?? 'redis://localhost:6381';
function requireDatabaseUrl(): string {
const url = process.env.DATABASE_URL;
if (!url) throw new Error('DATABASE_URL environment variable is required');
return url;
}
export function createWorker(redisUrl = REDIS_URL, databaseUrl?: string, queueName = 'waggle-jobs') {
const resolvedDbUrl = databaseUrl ?? requireDatabaseUrl();
const db = createDb(resolvedDbUrl);
const processor = new JobProcessor();
const redisPub = new Redis(redisUrl);
const jobService = new JobService(db, redisUrl, queueName);
// Register job handlers
processor.register('chat', chatHandler);
processor.register('task', taskHandler);
processor.register('waggle', createWaggleHandler({
enqueueWorker: async ({ teamId, userId, task, role, context }) => {
const childJob = await jobService.createJob(teamId, userId, 'chat', {
message: task,
role,
...(context ? { context } : {}),
});
return childJob.id;
},
}));
processor.register('group', groupHandler);
processor.register('cron', createCronHandler((job, handlerDb) => processor.process(job, handlerDb)));
const url = new URL(redisUrl);
const worker = new Worker<JobData>(queueName, async (job) => {
// Update status to running
await db.update(agentJobs)
.set({ status: 'running', startedAt: new Date() })
.where(eq(agentJobs.id, job.data.jobId));
try {
const result = await processor.process(job, db);
// Update status to completed
await db.update(agentJobs)
.set({ status: 'completed', completedAt: new Date(), output: result })
.where(eq(agentJobs.id, job.data.jobId));
// Publish progress to Redis (includes teamId for gateway routing)
await redisPub.publish(`job:${job.data.jobId}:progress`, JSON.stringify({
status: 'completed',
output: result,
userId: job.data.userId,
teamId: job.data.teamId,
}));
return result;
} catch (error: unknown) {
const message = error instanceof Error ? error.message : String(error);
await db.update(agentJobs)
.set({ status: 'failed', completedAt: new Date(), output: { error: message } })
.where(eq(agentJobs.id, job.data.jobId));
throw error;
}
}, {
connection: {
host: url.hostname,
port: parseInt(url.port || '6379'),
},
concurrency: 5,
});
// Clean up shared Redis publisher when worker closes
worker.on('closed', () => {
redisPub.quit().catch(() => {});
jobService.close().catch(() => {});
});
return { worker, processor, db, redisPub, jobService };
}
// Start if run directly
export function isDirectModule(entryPath: string | undefined, moduleUrl: string): boolean {
return entryPath !== undefined && pathToFileURL(resolve(entryPath)).href === moduleUrl;
}
type ShutdownSignalTarget = {
once(signal: 'SIGTERM' | 'SIGINT', listener: () => void): unknown;
};
export function installShutdownHandlers(
worker: Pick<Worker, 'close'>,
signalTarget: ShutdownSignalTarget = process,
): () => Promise<void> {
let shutdownPromise: Promise<void> | undefined;
const shutdown = () => {
shutdownPromise ??= worker.close();
return shutdownPromise;
};
signalTarget.once('SIGTERM', shutdown);
signalTarget.once('SIGINT', shutdown);
return shutdown;
}
if (isDirectModule(process.argv[1], import.meta.url)) {
const { worker } = createWorker();
installShutdownHandlers(worker);
console.log('Waggle agent worker started, waiting for jobs...');
worker.on('completed', (job) => {
console.log(`Job ${job.id} completed`);
});
worker.on('failed', (job, err) => {
console.error(`Job ${job?.id} failed:`, err.message);
});
}