moving
This commit is contained in:
69
packages/worker/src/execution-policy.ts
Normal file
69
packages/worker/src/execution-policy.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import {
|
||||
createSystemTools,
|
||||
PermissionManager,
|
||||
type ToolDefinition,
|
||||
} from '@waggle/agent';
|
||||
|
||||
const SAFE_TEAM_ID = /^[A-Za-z0-9](?:[A-Za-z0-9_-]{0,127})$/;
|
||||
|
||||
export const READ_ONLY_WORKER_SYSTEM_PROMPT = [
|
||||
'You are a Waggle AI agent running as a non-interactive read-only worker.',
|
||||
'You may inspect the tenant workspace with read_file, search_files, and search_content, and use web_search or web_fetch for research.',
|
||||
'You cannot run shell commands, execute code, or write, edit, or delete files.',
|
||||
'If a task requires changes, return a proposed patch or exact instructions for a human-approved interactive run.',
|
||||
'Be helpful, concise, and proactive.',
|
||||
].join('\n');
|
||||
|
||||
export interface WorkerExecutionContext {
|
||||
workspaceDir: string;
|
||||
tools: ToolDefinition[];
|
||||
systemPrompt: string;
|
||||
}
|
||||
|
||||
function isContained(parent: string, candidate: string): boolean {
|
||||
const relative = path.relative(parent, candidate);
|
||||
return relative === '' || (
|
||||
!path.isAbsolute(relative)
|
||||
&& relative !== '..'
|
||||
&& !relative.startsWith(`..${path.sep}`)
|
||||
);
|
||||
}
|
||||
|
||||
function canonicalChild(parent: string, childName: string): string {
|
||||
const child = path.join(parent, childName);
|
||||
fs.mkdirSync(child, { recursive: true });
|
||||
const canonical = fs.realpathSync.native(child);
|
||||
if (!isContained(parent, canonical)) {
|
||||
throw new Error(`Worker tenant path escapes WAGGLE_DATA_DIR: ${childName}`);
|
||||
}
|
||||
if (path.relative(child, canonical) !== '') {
|
||||
throw new Error(`Worker tenant path must not traverse a link or junction: ${childName}`);
|
||||
}
|
||||
return canonical;
|
||||
}
|
||||
|
||||
export function createWorkerExecutionContext(teamId: string): WorkerExecutionContext {
|
||||
const configuredDataDir = process.env.WAGGLE_DATA_DIR;
|
||||
if (!configuredDataDir?.trim()) {
|
||||
throw new Error('WAGGLE_DATA_DIR is required for non-interactive worker execution');
|
||||
}
|
||||
if (typeof teamId !== 'string' || !SAFE_TEAM_ID.test(teamId)) {
|
||||
throw new Error('Invalid teamId for non-interactive worker execution');
|
||||
}
|
||||
|
||||
const dataDir = path.resolve(configuredDataDir);
|
||||
fs.mkdirSync(dataDir, { recursive: true });
|
||||
const canonicalDataDir = fs.realpathSync.native(dataDir);
|
||||
const teamsDir = canonicalChild(canonicalDataDir, 'teams');
|
||||
const teamDir = canonicalChild(teamsDir, teamId);
|
||||
const workspaceDir = canonicalChild(teamDir, 'files');
|
||||
const tools = PermissionManager.sandbox().filterTools(createSystemTools(workspaceDir));
|
||||
|
||||
return {
|
||||
workspaceDir,
|
||||
tools,
|
||||
systemPrompt: READ_ONLY_WORKER_SYSTEM_PROMPT,
|
||||
};
|
||||
}
|
||||
@@ -1,25 +1,18 @@
|
||||
import type { Job } from 'bullmq';
|
||||
import type { JobData } from '../job-processor.js';
|
||||
import type { Db } from '../../../server/src/db/connection.js';
|
||||
import { runAgentLoop, createSystemTools } from '@waggle/agent';
|
||||
import { runAgentLoop } from '@waggle/agent';
|
||||
import { createWorkerExecutionContext } from '../execution-policy.js';
|
||||
|
||||
const LITELLM_URL = process.env.LITELLM_URL ?? 'http://localhost:4000/v1';
|
||||
const LITELLM_API_KEY = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev';
|
||||
const DEFAULT_MODEL = process.env.DEFAULT_MODEL ?? 'claude-sonnet';
|
||||
|
||||
export async function chatHandler(job: Job<JobData>, _db: Db): Promise<Record<string, unknown>> {
|
||||
const { userId, input } = job.data;
|
||||
const { teamId, userId, input } = job.data;
|
||||
const message = (input as Record<string, unknown>).message as string ?? '';
|
||||
const model = (input as Record<string, unknown>).model as string ?? DEFAULT_MODEL;
|
||||
const workspaceDir = (input as Record<string, unknown>).workspaceDir as string ?? process.cwd();
|
||||
|
||||
const tools = createSystemTools(workspaceDir);
|
||||
|
||||
const systemPrompt = [
|
||||
'You are a Waggle AI agent. You help users with tasks using your tools.',
|
||||
'Use system tools (bash, read_file, write_file, edit_file, search_files, search_content) to interact with the workspace.',
|
||||
'Be helpful, concise, and proactive.',
|
||||
].join('\n');
|
||||
const { systemPrompt, tools } = createWorkerExecutionContext(teamId);
|
||||
|
||||
const result = await runAgentLoop({
|
||||
litellmUrl: LITELLM_URL,
|
||||
|
||||
@@ -6,13 +6,14 @@ import { eq } from 'drizzle-orm';
|
||||
import { executeParallel, type ExecutionDeps } from '../execution/parallel.js';
|
||||
import { executeSequential } from '../execution/sequential.js';
|
||||
import { executeCoordinator } from '../execution/coordinator.js';
|
||||
import { runAgentLoop, createSystemTools } from '@waggle/agent';
|
||||
import { runAgentLoop } from '@waggle/agent';
|
||||
import { createWorkerExecutionContext } from '../execution-policy.js';
|
||||
|
||||
const LITELLM_URL = process.env.LITELLM_URL ?? 'http://localhost:4000/v1';
|
||||
const LITELLM_API_KEY = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev';
|
||||
|
||||
export async function groupHandler(job: Job<JobData>, db: Db): Promise<Record<string, unknown>> {
|
||||
const { input } = job.data;
|
||||
const { teamId, userId, input } = job.data;
|
||||
const groupId = (input as Record<string, unknown>).groupId as string;
|
||||
const taskInput = (input as Record<string, unknown>).taskInput as Record<string, unknown> ?? {};
|
||||
|
||||
@@ -24,7 +25,7 @@ export async function groupHandler(job: Job<JobData>, db: Db): Promise<Record<st
|
||||
const [group] = await db.select().from(agentGroups)
|
||||
.where(eq(agentGroups.id, groupId));
|
||||
|
||||
if (!group) {
|
||||
if (!group || group.userId !== userId) {
|
||||
throw new Error(`Agent group not found: ${groupId}`);
|
||||
}
|
||||
|
||||
@@ -40,27 +41,29 @@ export async function groupHandler(job: Job<JobData>, db: Db): Promise<Record<st
|
||||
if (members.length === 0) {
|
||||
throw new Error(`Agent group ${groupId} has no members`);
|
||||
}
|
||||
if (members.some(({ agent }) => agent.userId !== group.userId)) {
|
||||
throw new Error(`Agent group not found: ${groupId}`);
|
||||
}
|
||||
|
||||
const executionContext = createWorkerExecutionContext(teamId);
|
||||
|
||||
// Sort by execution order
|
||||
members.sort((a: typeof members[number], b: typeof members[number]) => a.member.executionOrder - b.member.executionOrder);
|
||||
|
||||
// Build execution deps — wires strategies to real runAgentLoop
|
||||
const workspaceDir = (taskInput.workspaceDir as string) ?? process.cwd();
|
||||
const allTools = createSystemTools(workspaceDir);
|
||||
|
||||
const deps: ExecutionDeps = {
|
||||
runAgent: async (config) => runAgentLoop({
|
||||
litellmUrl: LITELLM_URL,
|
||||
litellmApiKey: LITELLM_API_KEY,
|
||||
model: config.model,
|
||||
systemPrompt: config.systemPrompt,
|
||||
systemPrompt: `${executionContext.systemPrompt}\n\n${config.systemPrompt}`,
|
||||
tools: config.tools,
|
||||
messages: config.messages,
|
||||
maxTurns: config.maxTurns ?? 10,
|
||||
}),
|
||||
resolveTools: (toolNames) => {
|
||||
if (toolNames.length === 0) return allTools; // No filter = all tools
|
||||
return allTools.filter(t => toolNames.includes(t.name));
|
||||
if (toolNames.length === 0) return executionContext.tools; // No filter = all safe tools
|
||||
return executionContext.tools.filter(t => toolNames.includes(t.name));
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ import type { JobData } from '../job-processor.js';
|
||||
import type { Db } from '../../../server/src/db/connection.js';
|
||||
import { tasks } from '../../../server/src/db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { runAgentLoop, createSystemTools } from '@waggle/agent';
|
||||
import { runAgentLoop } from '@waggle/agent';
|
||||
import { createWorkerExecutionContext } from '../execution-policy.js';
|
||||
|
||||
const LITELLM_URL = process.env.LITELLM_URL ?? 'http://localhost:4000/v1';
|
||||
const LITELLM_API_KEY = process.env.LITELLM_API_KEY ?? process.env.LITELLM_MASTER_KEY ?? 'sk-waggle-dev';
|
||||
@@ -34,6 +35,8 @@ export async function taskHandler(job: Job<JobData>, db: Db): Promise<Record<str
|
||||
throw new Error(`Task ${taskId} does not belong to team ${teamId}`);
|
||||
}
|
||||
|
||||
const executionContext = createWorkerExecutionContext(teamId);
|
||||
|
||||
// Mark task as in-progress
|
||||
await db.update(tasks)
|
||||
.set({ status: 'in_progress', assignedTo: userId, updatedAt: new Date() })
|
||||
@@ -41,27 +44,25 @@ export async function taskHandler(job: Job<JobData>, db: Db): Promise<Record<str
|
||||
|
||||
// Build prompt from task context
|
||||
const systemPrompt = [
|
||||
executionContext.systemPrompt,
|
||||
'',
|
||||
'You are a Waggle AI agent executing an assigned task.',
|
||||
'Complete the task described below. Be thorough but concise.',
|
||||
'Use system tools (bash, read_file, write_file, edit_file, search_files, search_content) to interact with the workspace.',
|
||||
'',
|
||||
`Task: ${task.title}`,
|
||||
task.description ? `Details: ${task.description}` : '',
|
||||
task.priority ? `Priority: ${task.priority}` : '',
|
||||
].filter(Boolean).join('\n');
|
||||
|
||||
const workspaceDir = (input as Record<string, unknown>).workspaceDir as string ?? process.cwd();
|
||||
const model = (input as Record<string, unknown>).model as string ?? DEFAULT_MODEL;
|
||||
|
||||
try {
|
||||
const tools = createSystemTools(workspaceDir);
|
||||
|
||||
const result = await runAgentLoop({
|
||||
litellmUrl: LITELLM_URL,
|
||||
litellmApiKey: LITELLM_API_KEY,
|
||||
model,
|
||||
systemPrompt,
|
||||
tools,
|
||||
tools: executionContext.tools,
|
||||
messages: [{ role: 'user', content: `Execute this task: ${task.title}${task.description ? '\n\n' + task.description : ''}` }],
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { Worker } from 'bullmq';
|
||||
import { resolve } from 'node:path';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { createDb } from '../../server/src/db/connection.js';
|
||||
import { agentJobs } from '../../server/src/db/schema.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
@@ -91,9 +93,32 @@ export function createWorker(redisUrl = REDIS_URL, databaseUrl?: string, queueNa
|
||||
}
|
||||
|
||||
// Start if run directly
|
||||
const isDirectRun = process.argv[1]?.replace(/\\/g, '/').includes('worker/src/index');
|
||||
if (isDirectRun) {
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user