script-mode task execution — database-backed tasks with direct script runner

Tasks now live in the database (mode: script or agentic). Script-mode tasks
bypass the agent entirely — the implementation is materialized to a temp file
and executed directly, with stdout/stderr streamed to the UI via WebSocket.

Includes convert-to-mp3 as the first native script task.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-08 16:07:59 +00:00
co-authored by Claude Opus 4.6
parent 1159978187
commit f32f427972
15 changed files with 3238 additions and 209 deletions
+255
View File
@@ -0,0 +1,255 @@
import type { ServerWebSocket } from 'bun';
import { join } from 'node:path';
import { mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
import { getTaskByDirName } from 'officerdb';
import { getHomeDirForRole, DATA_PATH } from '../../data-path';
import { buildSandboxPrefix, buildRunuserSuffix } from '../../sidecar/sandbox';
type WSData = {
userId: number;
email: string;
username: string;
role: string;
sandboxed: boolean;
};
type RunMessage = {
type: 'run';
taskDirName: string;
inputs: Record<string, string>;
cwd?: string;
};
type StopMessage = {
type: 'stop';
};
type ClientMessage = RunMessage | StopMessage;
type OutMessage =
| { type: 'started'; taskName: string }
| { type: 'stdout'; data: string }
| { type: 'stderr'; data: string }
| { type: 'exit'; code: number }
| { type: 'error'; message: string };
// Active processes per WebSocket
const activeProcs = new WeakMap<ServerWebSocket<WSData>, { proc: ReturnType<typeof Bun.spawn>; kill: () => void }>();
import { tmpdir } from 'node:os';
function send(ws: ServerWebSocket<WSData>, msg: OutMessage) {
if (ws.readyState === 1) ws.send(JSON.stringify(msg));
}
function getRunner(language: string): string[] {
switch (language) {
case 'bash': return ['bash'];
case 'python': return ['python3'];
case 'typescript': return ['bun', 'run'];
case 'javascript': return ['node'];
default: return ['bash'];
}
}
function getFileName(language: string): string {
switch (language) {
case 'bash': return 'run.sh';
case 'python': return 'run.py';
case 'typescript': return 'index.ts';
case 'javascript': return 'index.js';
default: return 'run.sh';
}
}
// Write implementation to a temp file for execution, cleaned up after
function materializeScript(language: string, implementation: string): string {
const dir = join(tmpdir(), `officer-task-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(dir, { recursive: true });
const fileName = getFileName(language);
const filePath = join(dir, fileName);
writeFileSync(filePath, implementation);
chmodSync(filePath, 0o755);
return filePath;
}
function buildInputEnv(inputs: Record<string, string>): Record<string, string> {
const env: Record<string, string> = {};
for (const [key, value] of Object.entries(inputs)) {
env[`INPUT_${key.toUpperCase()}`] = value;
}
return env;
}
function buildArgs(inputs: Record<string, string>, argsOrder?: string[] | null): string[] {
if (!argsOrder || argsOrder.length === 0) return [];
return argsOrder.map((name) => inputs[name] ?? '');
}
async function handleRun(ws: ServerWebSocket<WSData>, msg: RunMessage) {
const { email, role, sandboxed, userId } = ws.data;
// Resolve task from database
const task = await getTaskByDirName(msg.taskDirName, userId);
if (!task) {
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
return;
}
if (task.mode !== 'script') {
send(ws, { type: 'error', message: 'Task is not a script-mode task' });
return;
}
if (!task.implementation) {
send(ws, { type: 'error', message: `Task ${msg.taskDirName} has no implementation` });
return;
}
const language = task.language ?? 'bash';
// Write script to temp dir for execution
const scriptPath = materializeScript(language, task.implementation);
// Build env vars from inputs
const inputEnv = buildInputEnv(msg.inputs);
// Build positional args
const positionalArgs = buildArgs(msg.inputs, task.args);
// Build the command
const runner = getRunner(language);
const cmd = [...runner, scriptPath, ...positionalArgs];
// Resolve cwd
const homeDir = getHomeDirForRole(email, role);
const cwd = msg.cwd ?? homeDir;
let spawnCmd: string[];
let spawnEnv: Record<string, string>;
let spawnCwd: string;
if (sandboxed) {
const prefix = buildSandboxPrefix(email);
const suffix = buildRunuserSuffix();
// Translate paths in inputs and args: DATA_PATH/{email}/... → /data/...
const userDataPrefix = join(DATA_PATH, email);
const translatePath = (v: string) => v.startsWith(userDataPrefix) ? '/data' + v.slice(userDataPrefix.length) : v;
const envArgs: string[] = [];
for (const [key, value] of Object.entries(inputEnv)) {
envArgs.push('--setenv', key, translatePath(value));
}
// Translate positional args too
const sandboxCmd = cmd.map((arg) => translatePath(arg));
// Script is in /tmp which is a tmpfs inside bwrap — need to bind-mount the host tmp dir
const scriptDir = join(scriptPath, '..');
const extraMounts = ['--ro-bind', scriptDir, scriptDir];
spawnCmd = [...prefix, ...extraMounts, ...envArgs, ...suffix, ...sandboxCmd];
spawnEnv = {};
spawnCwd = '/';
} else {
spawnCmd = cmd;
spawnEnv = { ...process.env as Record<string, string>, ...inputEnv };
spawnCwd = cwd;
}
const cleanup = () => {
try { rmSync(join(scriptPath, '..'), { recursive: true, force: true }); } catch { /* best effort */ }
};
send(ws, { type: 'started', taskName: task.name });
try {
const proc = Bun.spawn(spawnCmd, {
cwd: spawnCwd,
env: spawnEnv,
stdout: 'pipe',
stderr: 'pipe',
});
activeProcs.set(ws, {
proc,
kill: () => {
try { proc.kill(); } catch { /* already dead */ }
},
});
const stdoutReader = proc.stdout.getReader();
const stderrReader = proc.stderr.getReader();
const decoder = new TextDecoder();
const readStream = async (reader: ReadableStreamDefaultReader<Uint8Array>, type: 'stdout' | 'stderr') => {
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
send(ws, { type, data: decoder.decode(value) });
}
} catch {
// stream closed
}
};
const [, , exitCode] = await Promise.all([
readStream(stdoutReader, 'stdout'),
readStream(stderrReader, 'stderr'),
proc.exited,
]);
activeProcs.delete(ws);
cleanup();
send(ws, { type: 'exit', code: exitCode });
} catch (err) {
activeProcs.delete(ws);
cleanup();
send(ws, { type: 'error', message: `Failed to spawn: ${err instanceof Error ? err.message : String(err)}` });
}
}
export function open(_ws: ServerWebSocket<WSData>) {
// nothing to do
}
export function message(ws: ServerWebSocket<WSData>, raw: string | Buffer) {
const data = typeof raw === 'string' ? raw : raw.toString();
try {
const msg = JSON.parse(data) as ClientMessage;
if (msg.type === 'run') {
handleRun(ws, msg);
} else if (msg.type === 'stop') {
const active = activeProcs.get(ws);
if (active) {
active.kill();
activeProcs.delete(ws);
send(ws, { type: 'exit', code: -1 });
}
}
} catch {
send(ws, { type: 'error', message: 'Failed to parse message' });
}
}
export function close(ws: ServerWebSocket<WSData>) {
const active = activeProcs.get(ws);
if (active) {
active.kill();
activeProcs.delete(ws);
}
}
export const taskRunnerWebsocket = {
open,
message,
close,
drain() {},
};
+49 -194
View File
@@ -1,77 +1,8 @@
import { createRouter } from '../../create-router';
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { getNativeTasksDir, getGlobalTasksDir, getUserTasksDir } from '../../data-path';
import { getTasksForUser, getTaskByDirName, getTaskById, createTask, updateTask, deleteTask } from 'officerdb';
type TriggerConfig = { type: 'file'; extensions: string[] } | { type: 'directory' };
type Frontmatter = {
name: string;
description: string;
triggers: TriggerConfig[];
};
export function parseFrontmatter(raw: string): { frontmatter: Frontmatter; body: string; rawYaml: string } {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
if (!match) return { frontmatter: { name: '', description: '', triggers: [] }, body: raw, rawYaml: '' };
const yaml = match[1]!;
const body = match[2]!;
const name = yaml.match(/^name:\s*(.+)$/m)?.[1]?.trim() ?? '';
const description = yaml.match(/^description:\s*(.+)$/m)?.[1]?.trim() ?? '';
const triggers: TriggerConfig[] = [];
const triggerMatch = yaml.match(/^trigger:\s*\n((?:[ \t]+.+\n?)*)/m);
if (triggerMatch) {
const items = triggerMatch[1]!.split(/(?=^\s+-\s*type:)/m);
for (const item of items) {
const type = item.match(/type:\s*(.+)/)?.[1]?.trim();
if (type === 'directory') {
triggers.push({ type: 'directory' });
} else if (type === 'file') {
const extBlock = item.match(/extensions:\s*\n((?:\s+-\s*.+\n?)*)/);
const extensions = extBlock ? [...extBlock[1]!.matchAll(/^\s+-\s*(.+)$/gm)].map((m) => m[1]!.trim()) : [];
if (extensions.length > 0) triggers.push({ type: 'file', extensions });
}
}
}
return { frontmatter: { name, description, triggers }, body, rawYaml: yaml };
}
export async function readTaskDirs(dir: string): Promise<Map<string, string>> {
const result = new Map<string, string>();
try {
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const taskFile = join(dir, entry.name, 'TASK.md');
if (await Bun.file(taskFile).exists()) {
result.set(entry.name, taskFile);
}
}
} catch {
// directory doesn't exist yet
}
return result;
}
type Scope = 'native' | 'global' | 'user';
function resolveScope(dirName: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): Scope {
if (user.has(dirName)) return 'user';
if (global.has(dirName)) return 'global';
return 'native';
}
function resolveFile(name: string, native: Map<string, string>, global: Map<string, string>, user: Map<string, string>): { filePath: string; scope: Scope } | null {
if (user.has(name)) return { filePath: user.get(name)!, scope: 'user' };
if (global.has(name)) return { filePath: global.get(name)!, scope: 'global' };
if (native.has(name)) return { filePath: native.get(name)!, scope: 'native' };
return null;
}
function isPrivileged(role: string) {
return role === 'Super Admin';
}
@@ -80,29 +11,18 @@ export const tasksRouter = createRouter();
tasksRouter.get('/', async (ctx) => {
const user = ctx.get('user');
const nativeTasks = await readTaskDirs(getNativeTasksDir());
const globalTasks = await readTaskDirs(getGlobalTasksDir());
const userTasks = await readTaskDirs(getUserTasksDir(user.email));
const rows = await getTasksForUser(user.id);
const merged = new Map(nativeTasks);
for (const [name, path] of globalTasks) merged.set(name, path);
for (const [name, path] of userTasks) merged.set(name, path);
const tasks = await Promise.all(
Array.from(merged.entries()).map(async ([dirName, filePath]) => {
const raw = await Bun.file(filePath).text();
const { frontmatter } = parseFrontmatter(raw);
const scope = resolveScope(dirName, nativeTasks, globalTasks, userTasks);
return {
dirName,
name: frontmatter.name || dirName,
description: frontmatter.description,
scope,
triggers: frontmatter.triggers,
filePath,
};
}),
);
const tasks = rows.map((row) => ({
id: row.id,
dirName: row.dirName,
name: row.name,
description: row.description,
scope: row.scope,
triggers: (row.trigger as TriggerConfig[]) ?? [],
mode: row.mode ?? 'agentic',
userId: row.userId,
}));
return ctx.json(tasks);
});
@@ -111,127 +31,62 @@ tasksRouter.get('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTasks = await readTaskDirs(getNativeTasksDir());
const globalTasks = await readTaskDirs(getGlobalTasksDir());
const userTasks = await readTaskDirs(getUserTasksDir(user.email));
const resolved = resolveFile(name, nativeTasks, globalTasks, userTasks);
if (!resolved) return ctx.text('Not found', 404);
const raw = await Bun.file(resolved.filePath).text();
const { frontmatter, body, rawYaml } = parseFrontmatter(raw);
const chatMeta = join(dirname(resolved.filePath), 'chat', 'meta.json');
const chatSessionId = await Bun.file(chatMeta).json().then((m: { id: string }) => m.id).catch(() => null);
const task = await getTaskByDirName(name, user.id);
if (!task) return ctx.text('Not found', 404);
return ctx.json({
name: frontmatter.name || name,
description: frontmatter.description,
scope: resolved.scope,
body,
rawFrontmatter: rawYaml,
filePath: resolved.filePath,
chatSessionId,
id: task.id,
dirName: task.dirName,
name: task.name,
description: task.description,
scope: task.scope,
mode: task.mode,
language: task.language,
body: task.body,
implementation: task.implementation,
inputs: task.inputs,
args: task.args,
trigger: task.trigger,
version: task.version,
userId: task.userId,
});
});
tasksRouter.get('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTasks = await readTaskDirs(getNativeTasksDir());
const globalTasks = await readTaskDirs(getGlobalTasksDir());
const userTasks = await readTaskDirs(getUserTasksDir(user.email));
const resolved = resolveFile(name, nativeTasks, globalTasks, userTasks);
if (!resolved) return ctx.text('Not found', 404);
const chatDir = join(dirname(resolved.filePath), 'chat');
const sessionId = await Bun.file(join(chatDir, 'meta.json')).json().then((m: { id: string }) => m.id).catch(() => null);
const messages = await Bun.file(join(chatDir, 'messages.json')).json().catch(() => []);
return ctx.json({ sessionId, messages });
});
tasksRouter.put('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTasks = await readTaskDirs(getNativeTasksDir());
const globalTasks = await readTaskDirs(getGlobalTasksDir());
const userTasks = await readTaskDirs(getUserTasksDir(user.email));
const resolved = resolveFile(name, nativeTasks, globalTasks, userTasks);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
const { sessionId, messages } = await ctx.req.json<{ sessionId: string; messages: unknown[] }>();
await mkdir(chatDir, { recursive: true });
await Bun.write(join(chatDir, 'messages.json'), JSON.stringify(messages));
if (sessionId) await Bun.write(join(chatDir, 'meta.json'), JSON.stringify({ id: sessionId }));
return ctx.json({ ok: true });
});
tasksRouter.delete('/:name/chat', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTasks = await readTaskDirs(getNativeTasksDir());
const globalTasks = await readTaskDirs(getGlobalTasksDir());
const userTasks = await readTaskDirs(getUserTasksDir(user.email));
const resolved = resolveFile(name, nativeTasks, globalTasks, userTasks);
if (!resolved) return ctx.text('Not found', 404);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
const chatDir = join(dirname(resolved.filePath), 'chat');
await rm(chatDir, { recursive: true, force: true });
return ctx.json({ ok: true });
});
tasksRouter.post('/', async (ctx) => {
const user = ctx.get('user');
const { name } = await ctx.req.json<{ name: string }>();
if (!name?.trim()) return ctx.text('Name is required', 400);
const body = await ctx.req.json<{ name: string; description?: string; mode?: string; language?: string }>();
const dirName = name.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
if (!body.name?.trim()) return ctx.text('Name is required', 400);
const dirName = body.name.trim().toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '');
if (!dirName) return ctx.text('Invalid name', 400);
const targetDir = isPrivileged(user.role) ? getGlobalTasksDir() : getUserTasksDir(user.email);
const scope: Scope = isPrivileged(user.role) ? 'global' : 'user';
const scope = isPrivileged(user.role) ? 'global' : 'user';
const dir = join(targetDir, dirName);
const filePath = join(dir, 'TASK.md');
const task = await createTask({
scope,
userId: user.id,
dirName,
name: body.name.trim(),
description: body.description ?? null,
mode: body.mode ?? 'agentic',
language: body.language ?? null,
});
if (await Bun.file(filePath).exists()) {
return ctx.text('Task already exists', 409);
}
await mkdir(dir, { recursive: true });
await Bun.write(filePath, `---\nname: ${name.trim()}\ndescription: \n---\n`);
return ctx.json({ name: name.trim(), dirName, filePath, scope });
return ctx.json(task);
});
tasksRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const nativeTasks = await readTaskDirs(getNativeTasksDir());
const globalTasks = await readTaskDirs(getGlobalTasksDir());
const userTasks = await readTaskDirs(getUserTasksDir(user.email));
const task = await getTaskByDirName(name, user.id);
if (!task) return ctx.text('Not found', 404);
const resolved = resolveFile(name, nativeTasks, globalTasks, userTasks);
if (!resolved) return ctx.text('Not found', 404);
// Only owner or Super Admin can delete
if (task.scope === 'native') return ctx.text('Cannot delete native tasks', 403);
if (task.userId !== user.id && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
if (resolved.scope !== 'user' && !isPrivileged(user.role)) return ctx.text('Forbidden', 403);
await rm(dirname(resolved.filePath), { recursive: true });
await deleteTask(task.id);
return ctx.json({ ok: true });
});