Intelligence

Artifacts

Browse the repository, read documents, and manage the governance folders. Source, runtime, and infrastructure are read-only.

Repository
README.md
CONSTITUTION_COMPLIANCE_AUDIT_V1.mdREADME.md
repositories/aaf-holdings/hq01/lib/workers/templates.ts
3.9 KB
import fs from "node:fs";
import path from "node:path";
import { TEMPLATES_ROOT } from "./config";
import type { WorkerTemplate } from "./types";

/**
 * Worker template registry. Seeded once with the initial templates; thereafter
 * read from the filesystem. Templates are blueprints — versioned definitions
 * from which temporary workers instantiate.
 */

function nowIso(): string {
  return new Date().toISOString();
}
function templateJson(id: string): string {
  return path.join(TEMPLATES_ROOT, id, "worker-template.json");
}

const COMMON = {
  default_model: "claude-sonnet-4-6",
  default_permissions: "acceptEdits + scoped tool allowlist (no arbitrary shell)",
  default_workspace_policy: "runtime-confined: writes only inside the assignment workspace",
  default_report_format: "report.md with M4 sections (Summary, Files Changed, Recommendations, Doctrine Candidates, Next Pass)",
  status: "Active" as const,
  version: "1",
};

/** The initial templates (seed). Adding one here is additive. */
const SEED: Omit<WorkerTemplate, "created_at" | "updated_at">[] = [
  {
    id: "backend", name: "Backend Worker", department: "engineering", executive_owner: "agent-z",
    description: "Implements server-side code, APIs, schemas, and integrations.",
    default_temperature: 0.2, default_tools: ["Read", "Write", "Edit", "Bash", "Grep"], ...COMMON,
  },
  {
    id: "frontend", name: "Frontend Worker", department: "engineering", executive_owner: "agent-z",
    description: "Implements UI, components, and client behavior.",
    default_temperature: 0.3, default_tools: ["Read", "Write", "Edit", "Bash", "Grep"], ...COMMON,
  },
  {
    id: "qa", name: "QA Worker", department: "engineering", executive_owner: "agent-z",
    description: "Verifies behavior, runs tests, and reports defects.",
    default_temperature: 0.1, default_tools: ["Read", "Bash", "Grep"], ...COMMON,
  },
  {
    id: "documentation", name: "Documentation Worker", department: "engineering", executive_owner: "agent-z",
    description: "Generates and updates documentation from truth.",
    default_temperature: 0.3, default_tools: ["Read", "Write", "Edit"], ...COMMON,
  },
  {
    id: "research", name: "Research Worker", department: "engineering", executive_owner: "agent-z",
    description: "Investigates options, tools, and approaches; produces findings.",
    default_temperature: 0.4, default_tools: ["Read", "WebFetch", "Grep"], ...COMMON,
  },
  {
    id: "devops", name: "DevOps Worker", department: "engineering", executive_owner: "agent-z",
    description: "Builds and operates infrastructure, CI/CD, and deployments.",
    default_temperature: 0.2, default_tools: ["Read", "Write", "Edit", "Bash"], ...COMMON,
  },
];

function writeTemplate(t: WorkerTemplate): void {
  const file = templateJson(t.id);
  fs.mkdirSync(path.dirname(file), { recursive: true });
  const tmp = `${file}.tmp`;
  fs.writeFileSync(tmp, JSON.stringify(t, null, 2) + "\n", "utf8");
  fs.renameSync(tmp, file);
}

function readTemplate(id: string): WorkerTemplate | null {
  try {
    return JSON.parse(fs.readFileSync(templateJson(id), "utf8")) as WorkerTemplate;
  } catch {
    return null;
  }
}

/** List templates, seeding any missing ones from SEED (additive). */
export function listTemplates(): WorkerTemplate[] {
  const created = nowIso();
  for (const seed of SEED) {
    if (!readTemplate(seed.id)) {
      writeTemplate({ ...seed, created_at: created, updated_at: created });
    }
  }
  let names: string[] = [];
  try {
    names = fs
      .readdirSync(TEMPLATES_ROOT, { withFileTypes: true })
      .filter((e) => e.isDirectory())
      .map((e) => e.name);
  } catch {
    names = [];
  }
  return names
    .map(readTemplate)
    .filter((t): t is WorkerTemplate => Boolean(t))
    .sort((a, b) => a.name.localeCompare(b.name));
}

export function getTemplate(id: string): WorkerTemplate | null {
  listTemplates(); // ensure seeded
  return readTemplate(id);
}

root · /srv/aaf