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

@@ -18,6 +18,7 @@ import { log as auditLog } from "../audit.js";
import { checkWorkerHealth, scanOrphanedLabels, fetchGatewaySessions, type SessionLookup } from "./health.js";
import { projectTick } from "./tick.js";
import { createProvider } from "../providers/index.js";
import { getAllRoleIds } from "../roles/index.js";
// ---------------------------------------------------------------------------
// Types
@@ -306,13 +307,13 @@ async function performHealthPass(
const { provider } = await createProvider({ repo: project.repo });
let fixedCount = 0;
for (const role of ["dev", "qa", "architect"] as const) {
for (const role of getAllRoleIds()) {
// Check worker health (session liveness, label consistency, etc)
const healthFixes = await checkWorkerHealth({
workspaceDir,
groupId,
project,
role,
role: role as any,
sessions,
autoFix: true,
provider,
@@ -324,7 +325,7 @@ async function performHealthPass(
workspaceDir,
groupId,
project,
role,
role: role as any,
autoFix: true,
provider,
});

View File

@@ -11,7 +11,7 @@ import { createProvider } from "../providers/index.js";
import { selectLevel } from "../model-selector.js";
import { getWorker, getSessionForLevel, readProjects } from "../projects.js";
import { dispatchTask } from "../dispatch.js";
import { DEV_LEVELS, QA_LEVELS, ARCHITECT_LEVELS, isDevLevel, levelRole } from "../tiers.js";
import { getAllRoleIds, getLevelsForRole, getAllLevels, roleForLevel } from "../roles/index.js";
import {
DEFAULT_WORKFLOW,
getQueueLabels,
@@ -48,19 +48,18 @@ export const PRIORITY_ORDER: StateLabel[] = getAllQueueLabels(DEFAULT_WORKFLOW);
export function detectLevelFromLabels(labels: string[]): string | null {
const lower = labels.map((l) => l.toLowerCase());
// Match role.level labels (e.g., "dev.senior", "qa.reviewer")
// Match role.level labels (e.g., "dev.senior", "qa.reviewer", "architect.opus")
for (const l of lower) {
const dot = l.indexOf(".");
if (dot === -1) continue;
const role = l.slice(0, dot);
const level = l.slice(dot + 1);
if (role === "dev" && (DEV_LEVELS as readonly string[]).includes(level)) return level;
if (role === "qa" && (QA_LEVELS as readonly string[]).includes(level)) return level;
if (role === "architect" && (ARCHITECT_LEVELS as readonly string[]).includes(level)) return level;
const roleLevels = getLevelsForRole(role);
if (roleLevels.includes(level)) return level;
}
// Fallback: plain level name
const all = [...DEV_LEVELS, ...QA_LEVELS, ...ARCHITECT_LEVELS] as readonly string[];
const all = getAllLevels();
return all.find((l) => lower.includes(l)) ?? null;
}
@@ -165,7 +164,7 @@ export async function projectTick(opts: {
const provider = opts.provider ?? (await createProvider({ repo: project.repo })).provider;
const roleExecution = project.roleExecution ?? "parallel";
const roles: Role[] = targetRole ? [targetRole] : ["dev", "qa", "architect"];
const roles: Role[] = targetRole ? [targetRole] : getAllRoleIds() as Role[];
const pickups: TickAction[] = [];
const skipped: TickResult["skipped"] = [];
@@ -187,8 +186,8 @@ export async function projectTick(opts: {
continue;
}
// Check sequential role execution: any other role must be inactive
const otherRoles = (["dev", "qa", "architect"] as const).filter(r => r !== role);
if (roleExecution === "sequential" && otherRoles.some(r => getWorker(fresh, r).active)) {
const otherRoles = getAllRoleIds().filter(r => r !== role);
if (roleExecution === "sequential" && otherRoles.some(r => getWorker(fresh, r as any).active)) {
skipped.push({ role, reason: "Sequential: other role active" });
continue;
}
@@ -247,7 +246,7 @@ export async function projectTick(opts: {
function resolveLevelForIssue(issue: Issue, role: Role): string {
const labelLevel = detectLevelFromLabels(issue.labels);
if (labelLevel) {
const labelRole = levelRole(labelLevel);
const labelRole = roleForLevel(labelLevel);
// If label level belongs to a different role, use heuristic for correct role
if (labelRole && labelRole !== role) return selectLevel(issue.title, issue.description ?? "", role).level;
return labelLevel;