unify agent items into a flat file-based store, drop the marketplace

Replace the marketplace service dependency and the native/global/user
scope tiers with a single external directory ($OFFICER_ITEMS_DIR) holding
skills, tools, tasks, processes and extensions as plain files.

- tasks move from Postgres to TASK.md files (new file-backed task layer);
  task editing now works, which the DB path never supported
- skills/tools/processes collapse into one shared file router (single dir)
- remove the marketplace client (sync-marketplace/sync-version) and the
  boot-time sync; pi-bridge/pi-manager/sandbox point at the flat store
- drop the dead tasks + vestigial skills/tools/processes/extensions +
  item_chats tables (migration 0004)
- one-time migration script exports DB tasks and consolidates disk items

Migration verified: all 6 tasks round-trip through the runtime parser
identically to their DB rows (pipeline steps, triggers, script impls and
agentic bodies all intact).

NOTE: not yet functionally tested end-to-end — every item (each task mode,
tool, skill, extension) still needs to be run/exercised in the app before
this is trusted. To be done manually.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 00:39:17 +00:00
co-authored by Claude Opus 4.8
parent 5fd3d4faac
commit f3492512ba
29 changed files with 2366 additions and 1357 deletions
+5 -4
View File
@@ -3,7 +3,8 @@ import { randomUUID } from 'crypto';
import { readdirSync, existsSync, mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { getTaskByDirName, getUserSettings } from 'officerdb';
import { getUserSettings } from 'officerdb';
import { getTaskByDirName } from './task-files';
import { getHomeDirForRole, getHomeDir } from '../../data-path';
import { resolveBaseCwd } from '../pi/websocket';
import { SANDBOX_HOME } from '../../sidecar/sandbox';
@@ -471,7 +472,7 @@ export type ExecutePipelineParams = {
};
export async function executePipeline({ userId, email, username, role, taskDirName, inputs, cwd, model: modelOverride, startAt, abortSignal, emit }: ExecutePipelineParams): Promise<void> {
const pipelineTask = await getTaskByDirName(taskDirName, userId);
const pipelineTask = await getTaskByDirName(taskDirName);
if (!pipelineTask) {
emit({ type: 'error', message: `Task not found: ${taskDirName}` });
return;
@@ -514,7 +515,7 @@ export async function executePipeline({ userId, email, username, role, taskDirNa
const step = config.steps[stepIdx]!;
const stepTask = await getTaskByDirName(step.task, userId);
const stepTask = await getTaskByDirName(step.task);
if (!stepTask) {
emit({ type: 'error', message: `Step task not found: ${step.task}` });
return;
@@ -648,7 +649,7 @@ export async function message(ws: ServerWebSocket<WSData>, raw: string | Buffer)
const { userId, email, username, role } = ws.data;
// Resolve task name for the DB record
const task = await getTaskByDirName(msg.taskDirName, userId);
const task = await getTaskByDirName(msg.taskDirName);
if (!task) {
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
return;
+4 -4
View File
@@ -1,7 +1,7 @@
import type { ServerWebSocket } from 'bun';
import { join, isAbsolute } from 'node:path';
import { mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
import { getTaskByDirName } from 'officerdb';
import { getTaskByDirName } from './task-files';
import { getHomeDirForRole, DATA_PATH } from '../../data-path';
import { buildSandboxPrefix, buildRunuserSuffix } from '../../sidecar/sandbox';
@@ -90,10 +90,10 @@ function buildArgs(inputs: Record<string, string>, argsOrder?: string[] | null):
}
async function handleRun(ws: ServerWebSocket<WSData>, msg: RunMessage) {
const { email, role, sandboxed, userId } = ws.data;
const { email, role, sandboxed } = ws.data;
// Resolve task from database
const task = await getTaskByDirName(msg.taskDirName, userId);
// Resolve task from the file-backed store
const task = await getTaskByDirName(msg.taskDirName);
if (!task) {
send(ws, { type: 'error', message: `Task not found: ${msg.taskDirName}` });
return;
+247
View File
@@ -0,0 +1,247 @@
import { readdir, mkdir, rm } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { itemsDir } from '../../data-path';
// File-backed task store. Every task is a directory under $OFFICER_ITEMS_DIR/tasks/<dirName>/ with a
// TASK.md (metadata + prose body) and, for script-mode tasks, a sibling implementation file. There are
// no scope tiers and no database — the directory name is the task's identity.
const YAML = (Bun as unknown as { YAML: { parse(input: string): unknown } }).YAML;
export type TaskFrontmatter = {
name: string;
description: string | null;
version: number;
mode: string;
language: string | null;
args: string[] | null;
tags: string[] | null;
tools: string[] | null;
skills: string[] | null;
inputs: unknown;
outputs: unknown;
dependencies: unknown;
config: unknown;
trigger: unknown;
};
export type TaskRecord = TaskFrontmatter & {
dirName: string;
body: string;
implementation: string | null;
filePath: string;
};
export type TaskSummary = {
dirName: string;
name: string;
description: string | null;
mode: string;
version: number;
trigger: unknown;
};
// Frontmatter keys, in write order. These mirror the old `tasks` table columns 1:1.
const FM_KEYS = [
'name',
'description',
'version',
'mode',
'language',
'args',
'tags',
'tools',
'skills',
'inputs',
'outputs',
'dependencies',
'config',
'trigger',
] as const;
// Script implementation lives in a sibling file named by language, matching the marketplace registry.
const IMPL_FILE: Record<string, string> = {
bash: 'run.sh',
python: 'run.py',
typescript: 'index.ts',
javascript: 'index.js',
};
const implFileName = (language: string | null) => IMPL_FILE[language ?? 'bash'] ?? 'run.sh';
const tasksRoot = () => itemsDir('tasks');
const taskDir = (dirName: string) => join(tasksRoot(), dirName);
const taskFile = (dirName: string) => join(taskDir(dirName), 'TASK.md');
const asArray = (v: unknown): string[] | null => (Array.isArray(v) ? v.map(String) : null);
export const slugify = (name: string) =>
name
.trim()
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^a-z0-9-]/g, '');
function parseTaskMd(raw: string): { fm: TaskFrontmatter; body: string } {
const match = raw.match(/^---\n([\s\S]*?)\n---\n?([\s\S]*)$/);
const body = match ? match[2]! : raw;
const yaml = match ? match[1]! : '';
let parsed: Record<string, unknown> = {};
if (yaml.trim()) {
try {
parsed = (YAML.parse(yaml) as Record<string, unknown>) ?? {};
} catch {
parsed = {};
}
}
const versionRaw = parsed.version;
const version = typeof versionRaw === 'number' ? versionRaw : Number(versionRaw) || 1;
return {
fm: {
name: parsed.name == null ? '' : String(parsed.name),
description: parsed.description == null ? null : String(parsed.description),
version,
mode: parsed.mode == null ? 'agentic' : String(parsed.mode),
language: parsed.language == null ? null : String(parsed.language),
args: asArray(parsed.args),
tags: asArray(parsed.tags),
tools: asArray(parsed.tools),
skills: asArray(parsed.skills),
inputs: parsed.inputs ?? null,
outputs: parsed.outputs ?? null,
dependencies: parsed.dependencies ?? null,
config: parsed.config ?? null,
trigger: parsed.trigger ?? null,
},
body,
};
}
// Serialize as JSON-flow YAML: every value is JSON.stringify'd, which is valid YAML and round-trips
// losslessly. Bun.YAML.parse also accepts hand-written multi-line YAML, so files stay editable.
function buildTaskMd(fm: Partial<TaskFrontmatter>, body: string): string {
const lines: string[] = ['---'];
for (const key of FM_KEYS) {
const val = (fm as Record<string, unknown>)[key];
if (val === undefined || val === null) continue;
if (Array.isArray(val) && val.length === 0) continue;
lines.push(`${key}: ${JSON.stringify(val)}`);
}
lines.push('---', '');
lines.push(body ?? '');
return lines.join('\n');
}
async function readImplementation(dirName: string, language: string | null): Promise<string | null> {
const implPath = join(taskDir(dirName), implFileName(language));
const file = Bun.file(implPath);
return (await file.exists()) ? file.text() : null;
}
export async function listTasks(): Promise<TaskSummary[]> {
let entries;
try {
entries = await readdir(tasksRoot(), { withFileTypes: true });
} catch {
return [];
}
const summaries: TaskSummary[] = [];
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const file = Bun.file(taskFile(entry.name));
if (!(await file.exists())) continue;
const { fm } = parseTaskMd(await file.text());
summaries.push({
dirName: entry.name,
name: fm.name || entry.name,
description: fm.description,
mode: fm.mode,
version: fm.version,
trigger: fm.trigger ?? [],
});
}
summaries.sort((a, b) => a.name.localeCompare(b.name));
return summaries;
}
export async function getTaskByDirName(dirName: string): Promise<TaskRecord | null> {
const file = Bun.file(taskFile(dirName));
if (!(await file.exists())) return null;
const { fm, body } = parseTaskMd(await file.text());
const implementation = await readImplementation(dirName, fm.language);
return { ...fm, dirName, body, implementation, filePath: taskFile(dirName) };
}
type CreateTaskInput = {
name: string;
description?: string | null;
mode?: string;
language?: string | null;
};
export async function createTask(input: CreateTaskInput): Promise<TaskSummary & { filePath: string }> {
const name = input.name.trim();
const dirName = slugify(name);
if (!dirName) throw new Error('Invalid name');
if (await Bun.file(taskFile(dirName)).exists()) throw new Error('Task already exists');
const mode = input.mode ?? 'agentic';
const language = input.language ?? null;
await mkdir(taskDir(dirName), { recursive: true });
await Bun.write(
taskFile(dirName),
buildTaskMd({ name, description: input.description ?? null, version: 1, mode, language }, ''),
);
if (mode === 'script') await Bun.write(join(taskDir(dirName), implFileName(language)), '');
return {
dirName,
name,
description: input.description ?? null,
mode,
version: 1,
trigger: [],
filePath: taskFile(dirName),
};
}
export async function updateTask(
dirName: string,
patch: Partial<TaskFrontmatter> & { body?: string; implementation?: string | null },
): Promise<TaskRecord | null> {
const existing = await getTaskByDirName(dirName);
if (!existing) return null;
const merged: TaskFrontmatter = { ...existing, ...patch };
const body = patch.body ?? existing.body;
await Bun.write(taskFile(dirName), buildTaskMd(merged, body));
if (patch.implementation !== undefined && patch.implementation !== null) {
await Bun.write(join(taskDir(dirName), implFileName(merged.language)), patch.implementation);
}
return getTaskByDirName(dirName);
}
export async function deleteTask(dirName: string): Promise<void> {
await rm(taskDir(dirName), { recursive: true, force: true });
}
// Write a full task record to disk (used by the one-time DB→files migration). Overwrites any
// existing task with the same dirName so callers can apply their own precedence via write order.
export async function importTask(
dirName: string,
record: Partial<TaskFrontmatter> & { body?: string; implementation?: string | null },
): Promise<void> {
await mkdir(taskDir(dirName), { recursive: true });
await Bun.write(taskFile(dirName), buildTaskMd(record, record.body ?? ''));
if (record.implementation != null && record.implementation !== '') {
await Bun.write(join(taskDir(dirName), implFileName(record.language ?? null)), record.implementation);
}
}
+19 -40
View File
@@ -1,45 +1,34 @@
import { createRouter } from '../../create-router';
import { getTasksForUser, getTaskByDirName, getTaskById, createTask, updateTask, deleteTask } from 'officerdb';
import { listTasks, getTaskByDirName, createTask, deleteTask } from './task-files';
type TriggerConfig = { type: 'file'; extensions: string[] } | { type: 'directory' };
function isPrivileged(role: string) {
return role === 'Super Admin';
}
export const tasksRouter = createRouter();
tasksRouter.get('/', async (ctx) => {
const user = ctx.get('user');
const rows = await getTasksForUser(user.id);
const summaries = await listTasks();
const tasks = rows.map((row) => ({
id: row.id,
const tasks = summaries.map((row) => ({
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);
});
tasksRouter.get('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const task = await getTaskByDirName(name, user.id);
const task = await getTaskByDirName(name);
if (!task) return ctx.text('Not found', 404);
return ctx.json({
id: task.id,
dirName: task.dirName,
name: task.name,
description: task.description,
scope: task.scope,
mode: task.mode,
language: task.language,
body: task.body,
@@ -49,45 +38,35 @@ tasksRouter.get('/:name', async (ctx) => {
trigger: task.trigger,
config: task.config,
version: task.version,
userId: task.userId,
filePath: task.filePath,
});
});
tasksRouter.post('/', async (ctx) => {
const user = ctx.get('user');
const body = await ctx.req.json<{ name: string; description?: string; mode?: string; language?: string }>();
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 scope = isPrivileged(user.role) ? 'global' : 'user';
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,
});
return ctx.json(task);
try {
const task = await createTask({
name: body.name,
description: body.description ?? null,
mode: body.mode ?? 'agentic',
language: body.language ?? null,
});
return ctx.json(task);
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to create task';
return ctx.text(message, message === 'Task already exists' ? 409 : 400);
}
});
tasksRouter.delete('/:name', async (ctx) => {
const user = ctx.get('user');
const name = ctx.req.param('name');
const task = await getTaskByDirName(name, user.id);
const task = await getTaskByDirName(name);
if (!task) 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);
await deleteTask(task.id);
await deleteTask(task.dirName);
return ctx.json({ ok: true });
});