refactor: Centralize role types and configuration (#190)

Creates a single source of truth for all worker roles via lib/roles/.

## New: lib/roles/

- **registry.ts** — All role definitions (dev, qa, architect) with
  levels, models, emoji, completion results, session key patterns
- **types.ts** — RoleConfig interface
- **selectors.ts** — Query helpers: getRole(), getLevelsForRole(),
  resolveModel(), isValidResult(), roleForLevel(), etc.
- **index.ts** — Barrel exports

## Migrated Files

- **lib/tiers.ts** — Now delegates to registry (backward compat kept)
- **lib/dispatch.ts** — Uses registry for emoji resolution
- **lib/bootstrap-hook.ts** — Uses registry for session key pattern
- **lib/services/tick.ts** — Uses registry for level detection
- **lib/services/heartbeat.ts** — Uses registry for role iteration
- **lib/tools/health.ts** — Uses registry for role iteration
- **lib/tools/work-start.ts** — Uses registry for role enum
- **lib/tools/work-finish.ts** — Uses registry for result validation
- **lib/tools/project-register.ts** — Uses registry for level lists

## Key Benefits

- Adding a new role = add entry to registry.ts (single file)
- No more scattered role unions ("dev" | "qa" | "architect")
- Type-safe role/level/result validation from registry
- Session key pattern auto-generated from registry
- All 64 tests passing (22 new registry tests + 42 existing)
This commit is contained in:
Lauren ten Hoor
2026-02-14 17:15:54 +08:00
parent a547127d19
commit be8e0f4db1
15 changed files with 589 additions and 109 deletions

View File

@@ -10,6 +10,7 @@
import fs from "node:fs/promises";
import path from "node:path";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { getSessionKeyRolePattern } from "./roles/index.js";
/**
* Parse a DevClaw subagent session key to extract project name and role.
@@ -23,11 +24,12 @@ import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
*/
export function parseDevClawSessionKey(
sessionKey: string,
): { projectName: string; role: "dev" | "qa" | "architect" } | null {
// Match `:subagent:` prefix, then capture everything up to the last `-dev-`, `-qa-`, or `-architect-`
const match = sessionKey.match(/:subagent:(.+)-(dev|qa|architect)-[^-]+$/);
): { projectName: string; role: string } | null {
// Match `:subagent:` prefix, then capture project name and role (derived from registry)
const rolePattern = getSessionKeyRolePattern();
const match = sessionKey.match(new RegExp(`:subagent:(.+)-(${rolePattern})-[^-]+$`));
if (!match) return null;
return { projectName: match[1], role: match[2] as "dev" | "qa" | "architect" };
return { projectName: match[1], role: match[2] };
}
/**
@@ -40,7 +42,7 @@ export function parseDevClawSessionKey(
export async function loadRoleInstructions(
workspaceDir: string,
projectName: string,
role: "dev" | "qa" | "architect",
role: string,
): Promise<string> {
const projectFile = path.join(workspaceDir, "projects", "roles", projectName, `${role}.md`);
try {