feat: monorepo consolidation — forge pipeline, MACP protocol, framework plugin, profiles/guides/skills
Some checks failed
ci/woodpecker/push/ci Pipeline failed
ci/woodpecker/pr/ci Pipeline failed

Work packages completed:
- WP1: packages/forge — pipeline runner, stage adapter, board tasks, brief classifier,
  persona loader with project-level overrides. 89 tests, 95.62% coverage.
- WP2: packages/macp — credential resolver, gate runner, event emitter, protocol types.
  65 tests, 96.24% coverage. Full Python-to-TS port preserving all behavior.
- WP3: plugins/mosaic-framework — OC rails injection plugin (before_agent_start +
  subagent_spawning hooks for Mosaic contract enforcement).
- WP4: profiles/ (domains, tech-stacks, workflows), guides/ (17 docs),
  skills/ (5 universal skills), forge pipeline assets (48 markdown files).

Board deliberation: docs/reviews/consolidation-board-memo.md
Brief: briefs/monorepo-consolidation.md

Consolidates mosaic/stack (forge, MACP, bootstrap framework) into mosaic/mosaic-stack.
154 new tests total. Zero Python — all TypeScript/ESM.
This commit is contained in:
Mos (Agent)
2026-03-30 19:43:24 +00:00
parent 40c068fcbc
commit 10689a30d2
123 changed files with 18166 additions and 11 deletions

View File

@@ -0,0 +1,182 @@
import fs from 'node:fs';
import path from 'node:path';
import type { BoardPersona, BoardSynthesis, ForgeTask, PersonaReview } from './types.js';
/**
* Build the brief content for a persona's board evaluation.
*/
export function buildPersonaBrief(brief: string, persona: BoardPersona): string {
return [
`# Board Evaluation: ${persona.name}`,
'',
'## Your Role',
persona.description,
'',
'## Brief Under Review',
brief.trim(),
'',
'## Instructions',
'Evaluate this brief from your perspective. Output a JSON object:',
'{',
` "persona": "${persona.name}",`,
' "verdict": "approve|reject|conditional",',
' "confidence": 0.0-1.0,',
' "concerns": ["..."],',
' "recommendations": ["..."],',
' "key_risks": ["..."]',
'}',
'',
].join('\n');
}
/**
* Write a persona brief to the run directory and return the path.
*/
export function writePersonaBrief(
runDir: string,
baseTaskId: string,
persona: BoardPersona,
brief: string,
): string {
const briefDir = path.join(runDir, '01-board', 'briefs');
fs.mkdirSync(briefDir, { recursive: true });
const briefPath = path.join(briefDir, `${baseTaskId}-${persona.slug}.md`);
fs.writeFileSync(briefPath, buildPersonaBrief(brief, persona), 'utf-8');
return briefPath;
}
/**
* Get the result path for a persona's board review.
*/
export function personaResultPath(runDir: string, taskId: string): string {
return path.join(runDir, '01-board', 'results', `${taskId}.board.json`);
}
/**
* Get the result path for the board synthesis.
*/
export function synthesisResultPath(runDir: string, taskId: string): string {
return path.join(runDir, '01-board', 'results', `${taskId}.board.json`);
}
/**
* Generate one ForgeTask per board persona plus one synthesis task.
*
* Persona tasks run independently (no depends_on).
* The synthesis task depends on all persona tasks with 'all_terminal' policy.
*/
export function generateBoardTasks(
brief: string,
personas: BoardPersona[],
runDir: string,
baseTaskId = 'BOARD',
): ForgeTask[] {
const tasks: ForgeTask[] = [];
const personaTaskIds: string[] = [];
const personaResultPaths: string[] = [];
for (const persona of personas) {
const taskId = `${baseTaskId}-${persona.slug}`;
personaTaskIds.push(taskId);
const briefPath = writePersonaBrief(runDir, baseTaskId, persona, brief);
const resultRelPath = personaResultPath(runDir, taskId);
personaResultPaths.push(resultRelPath);
tasks.push({
id: taskId,
title: `Board review: ${persona.name}`,
description: `Independent board evaluation for ${persona.name}.`,
type: 'review',
dispatch: 'exec',
status: 'pending',
briefPath,
resultPath: resultRelPath,
timeoutSeconds: 120,
qualityGates: ['true'],
metadata: {
personaName: persona.name,
personaSlug: persona.slug,
personaPath: persona.path,
resultOutputPath: resultRelPath,
},
});
}
// Synthesis task — merges all persona reviews
const synthesisId = `${baseTaskId}-SYNTHESIS`;
const synthesisResult = synthesisResultPath(runDir, synthesisId);
tasks.push({
id: synthesisId,
title: 'Board synthesis',
description: 'Merge independent board reviews into a single recommendation.',
type: 'review',
dispatch: 'exec',
status: 'pending',
briefPath: '',
resultPath: synthesisResult,
timeoutSeconds: 120,
dependsOn: personaTaskIds,
dependsOnPolicy: 'all_terminal',
qualityGates: ['true'],
metadata: {
resultOutputPath: synthesisResult,
inputResultPaths: personaResultPaths,
},
});
return tasks;
}
/**
* Merge multiple persona reviews into a board synthesis.
*/
export function synthesizeReviews(reviews: PersonaReview[]): BoardSynthesis {
const verdicts = reviews.map((r) => r.verdict);
let mergedVerdict: PersonaReview['verdict'];
if (verdicts.includes('reject')) {
mergedVerdict = 'reject';
} else if (verdicts.includes('conditional')) {
mergedVerdict = 'conditional';
} else {
mergedVerdict = 'approve';
}
const confidenceValues = reviews.map((r) => r.confidence);
const avgConfidence =
confidenceValues.length > 0
? Math.round((confidenceValues.reduce((a, b) => a + b, 0) / confidenceValues.length) * 1000) /
1000
: 0;
const concerns = unique(reviews.flatMap((r) => r.concerns));
const recommendations = unique(reviews.flatMap((r) => r.recommendations));
const keyRisks = unique(reviews.flatMap((r) => r.keyRisks));
return {
persona: 'Board Synthesis',
verdict: mergedVerdict,
confidence: avgConfidence,
concerns,
recommendations,
keyRisks,
reviews,
};
}
/** Deduplicate while preserving order. */
function unique(items: string[]): string[] {
const seen = new Set<string>();
const result: string[] = [];
for (const item of items) {
if (!seen.has(item)) {
seen.add(item);
result.push(item);
}
}
return result;
}

View File

@@ -0,0 +1,102 @@
import { STAGE_SEQUENCE, STRATEGIC_KEYWORDS, TECHNICAL_KEYWORDS } from './constants.js';
import type { BriefClass, ClassSource } from './types.js';
const VALID_CLASSES: ReadonlySet<string> = new Set<BriefClass>([
'strategic',
'technical',
'hotfix',
]);
/**
* Auto-classify a brief based on keyword analysis.
* Returns 'strategic' if strategic keywords dominate,
* 'technical' if any technical keywords are found,
* otherwise defaults to 'strategic' (full pipeline).
*/
export function classifyBrief(text: string): BriefClass {
const lower = text.toLowerCase();
let strategicHits = 0;
let technicalHits = 0;
for (const kw of STRATEGIC_KEYWORDS) {
if (lower.includes(kw)) strategicHits++;
}
for (const kw of TECHNICAL_KEYWORDS) {
if (lower.includes(kw)) technicalHits++;
}
if (strategicHits > technicalHits) return 'strategic';
if (technicalHits > 0) return 'technical';
return 'strategic';
}
/**
* Parse YAML frontmatter from a brief.
* Supports simple `key: value` pairs via regex (no YAML dependency).
*/
export function parseBriefFrontmatter(text: string): Record<string, string> {
const match = text.match(/^---\s*\n([\s\S]*?)\n---\s*\n/);
if (!match?.[1]) return {};
const result: Record<string, string> = {};
for (const line of match[1].split('\n')) {
const km = line.trim().match(/^(\w[\w-]*)\s*:\s*(.+)$/);
if (km?.[1] && km[2]) {
result[km[1]] = km[2].trim().replace(/^["']|["']$/g, '');
}
}
return result;
}
/**
* Determine brief class from all sources with priority:
* CLI flag > frontmatter > auto-classify.
*/
export function determineBriefClass(
text: string,
cliClass?: string,
): { briefClass: BriefClass; classSource: ClassSource } {
if (cliClass && VALID_CLASSES.has(cliClass)) {
return { briefClass: cliClass as BriefClass, classSource: 'cli' };
}
const fm = parseBriefFrontmatter(text);
if (fm['class'] && VALID_CLASSES.has(fm['class'])) {
return { briefClass: fm['class'] as BriefClass, classSource: 'frontmatter' };
}
return { briefClass: classifyBrief(text), classSource: 'auto' };
}
/**
* Build the stage list based on brief classification.
* - strategic: full pipeline (all stages)
* - technical: skip board (01-board)
* - hotfix: skip board + brief analyzer
*
* forceBoard re-adds the board stage regardless of class.
*/
export function stagesForClass(briefClass: BriefClass, forceBoard = false): string[] {
const stages = ['00-intake', '00b-discovery'];
if (briefClass === 'strategic' || forceBoard) {
stages.push('01-board');
}
if (briefClass === 'strategic' || briefClass === 'technical' || forceBoard) {
stages.push('01b-brief-analyzer');
}
stages.push(
'02-planning-1',
'03-planning-2',
'04-planning-3',
'05-coding',
'06-review',
'07-remediate',
'08-test',
'09-deploy',
);
// Maintain canonical order
return stages.filter((s) => STAGE_SEQUENCE.includes(s));
}

View File

@@ -0,0 +1,208 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import type { StageSpec } from './types.js';
/** Package root resolved via import.meta.url — works regardless of install location. */
export const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
/** Pipeline asset directory (stages, agents, rails, gates, templates). */
export const PIPELINE_DIR = path.join(PACKAGE_ROOT, 'pipeline');
/** Stage specifications — defines every pipeline stage. */
export const STAGE_SPECS: Record<string, StageSpec> = {
'00-intake': {
number: '00',
title: 'Forge Intake',
dispatch: 'exec',
type: 'research',
gate: 'none',
promptFile: '00-intake.md',
qualityGates: [],
},
'00b-discovery': {
number: '00b',
title: 'Forge Discovery',
dispatch: 'exec',
type: 'research',
gate: 'discovery-complete',
promptFile: '00b-discovery.md',
qualityGates: ['true'],
},
'01-board': {
number: '01',
title: 'Forge Board Review',
dispatch: 'exec',
type: 'review',
gate: 'board-approval',
promptFile: '01-board.md',
qualityGates: [{ type: 'ci-pipeline', command: 'board-approval (via board-tasks)' }],
},
'01b-brief-analyzer': {
number: '01b',
title: 'Forge Brief Analyzer',
dispatch: 'exec',
type: 'research',
gate: 'brief-analysis-complete',
promptFile: '01-board.md',
qualityGates: ['true'],
},
'02-planning-1': {
number: '02',
title: 'Forge Planning 1',
dispatch: 'exec',
type: 'research',
gate: 'architecture-approval',
promptFile: '02-planning-1-architecture.md',
qualityGates: ['true'],
},
'03-planning-2': {
number: '03',
title: 'Forge Planning 2',
dispatch: 'exec',
type: 'research',
gate: 'implementation-approval',
promptFile: '03-planning-2-implementation.md',
qualityGates: ['true'],
},
'04-planning-3': {
number: '04',
title: 'Forge Planning 3',
dispatch: 'exec',
type: 'research',
gate: 'decomposition-approval',
promptFile: '04-planning-3-decomposition.md',
qualityGates: ['true'],
},
'05-coding': {
number: '05',
title: 'Forge Coding',
dispatch: 'yolo',
type: 'coding',
gate: 'lint-build-test',
promptFile: '05-coding.md',
qualityGates: ['pnpm lint', 'pnpm build', 'pnpm test'],
},
'06-review': {
number: '06',
title: 'Forge Review',
dispatch: 'exec',
type: 'review',
gate: 'review-pass',
promptFile: '06-review.md',
qualityGates: [
{
type: 'ai-review',
command:
'echo \'{"summary":"review-pass","verdict":"approve","findings":[],"stats":{"blockers":0,"should_fix":0,"suggestions":0}}\'',
},
],
},
'07-remediate': {
number: '07',
title: 'Forge Remediation',
dispatch: 'yolo',
type: 'coding',
gate: 're-review',
promptFile: '07-remediate.md',
qualityGates: ['true'],
},
'08-test': {
number: '08',
title: 'Forge Test Validation',
dispatch: 'exec',
type: 'review',
gate: 'tests-green',
promptFile: '08-test.md',
qualityGates: ['pnpm test'],
},
'09-deploy': {
number: '09',
title: 'Forge Deploy',
dispatch: 'exec',
type: 'deploy',
gate: 'deploy-verification',
promptFile: '09-deploy.md',
qualityGates: [{ type: 'ci-pipeline', command: 'deploy-verification' }],
},
};
/** Ordered stage sequence — full pipeline. */
export const STAGE_SEQUENCE = [
'00-intake',
'00b-discovery',
'01-board',
'01b-brief-analyzer',
'02-planning-1',
'03-planning-2',
'04-planning-3',
'05-coding',
'06-review',
'07-remediate',
'08-test',
'09-deploy',
];
/** Per-stage timeout in seconds. */
export const STAGE_TIMEOUTS: Record<string, number> = {
'00-intake': 120,
'00b-discovery': 300,
'01-board': 120,
'01b-brief-analyzer': 300,
'02-planning-1': 600,
'03-planning-2': 600,
'04-planning-3': 600,
'05-coding': 3600,
'06-review': 600,
'07-remediate': 3600,
'08-test': 600,
'09-deploy': 600,
};
/** Human-readable labels per stage. */
export const STAGE_LABELS: Record<string, string> = {
'00-intake': 'INTAKE',
'00b-discovery': 'DISCOVERY',
'01-board': 'BOARD',
'01b-brief-analyzer': 'BRIEF ANALYZER',
'02-planning-1': 'PLANNING 1',
'03-planning-2': 'PLANNING 2',
'04-planning-3': 'PLANNING 3',
'05-coding': 'CODING',
'06-review': 'REVIEW',
'07-remediate': 'REMEDIATE',
'08-test': 'TEST',
'09-deploy': 'DEPLOY',
};
/** Keywords that indicate a strategic brief. */
export const STRATEGIC_KEYWORDS = new Set([
'security',
'pricing',
'architecture',
'integration',
'budget',
'strategy',
'compliance',
'migration',
'partnership',
'launch',
]);
/** Keywords that indicate a technical brief. */
export const TECHNICAL_KEYWORDS = new Set([
'bugfix',
'bug',
'refactor',
'ui',
'style',
'tweak',
'typo',
'lint',
'cleanup',
'rename',
'hotfix',
'patch',
'css',
'format',
]);

View File

@@ -0,0 +1,82 @@
// Types
export type {
StageDispatch,
StageType,
StageSpec,
BriefClass,
ClassSource,
StageStatus,
RunManifest,
ForgeTaskStatus,
ForgeTask,
TaskExecutor,
BoardPersona,
PersonaReview,
BoardSynthesis,
ForgeConfig,
PipelineOptions,
PipelineResult,
} from './types.js';
// Constants
export {
PACKAGE_ROOT,
PIPELINE_DIR,
STAGE_SPECS,
STAGE_SEQUENCE,
STAGE_TIMEOUTS,
STAGE_LABELS,
STRATEGIC_KEYWORDS,
TECHNICAL_KEYWORDS,
} from './constants.js';
// Brief classifier
export {
classifyBrief,
parseBriefFrontmatter,
determineBriefClass,
stagesForClass,
} from './brief-classifier.js';
// Persona loader
export {
slugify,
personaNameFromMarkdown,
loadBoardPersonas,
loadPersonaOverrides,
loadForgeConfig,
getEffectivePersonas,
} from './persona-loader.js';
// Stage adapter
export {
stageTaskId,
stageDir,
stageBriefPath,
stageResultPath,
loadStagePrompt,
buildStageBrief,
writeStageBrief,
mapStageToTask,
} from './stage-adapter.js';
// Board tasks
export {
buildPersonaBrief,
writePersonaBrief,
personaResultPath,
synthesisResultPath,
generateBoardTasks,
synthesizeReviews,
} from './board-tasks.js';
// Pipeline runner
export {
generateRunId,
saveManifest,
loadManifest,
selectStages,
runPipeline,
resumePipeline,
getPipelineStatus,
} from './pipeline-runner.js';

View File

@@ -0,0 +1,153 @@
import fs from 'node:fs';
import path from 'node:path';
import { PIPELINE_DIR } from './constants.js';
import type { BoardPersona, ForgeConfig } from './types.js';
/** Board agents directory within the pipeline assets. */
const BOARD_AGENTS_DIR = path.join(PIPELINE_DIR, 'agents', 'board');
/**
* Convert a string to a URL-safe slug.
*/
export function slugify(value: string): string {
const slug = value
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-+|-+$/g, '');
return slug || 'persona';
}
/**
* Extract persona name from the first heading line in markdown.
* Strips trailing em-dash or hyphen-separated subtitle.
*/
export function personaNameFromMarkdown(markdown: string, fallback: string): string {
const firstLine = markdown.trim().split('\n')[0] ?? fallback;
let heading = firstLine.replace(/^#+\s*/, '').trim();
if (heading.includes('—')) {
heading = heading.split('—')[0]!.trim();
} else if (heading.includes('-')) {
heading = heading.split('-')[0]!.trim();
}
return heading || fallback;
}
/**
* Load board personas from the pipeline assets directory.
* Returns sorted list of persona definitions.
*/
export function loadBoardPersonas(boardDir: string = BOARD_AGENTS_DIR): BoardPersona[] {
if (!fs.existsSync(boardDir)) return [];
const files = fs
.readdirSync(boardDir)
.filter((f) => f.endsWith('.md'))
.sort();
return files.map((file) => {
const filePath = path.join(boardDir, file);
const content = fs.readFileSync(filePath, 'utf-8').trim();
const stem = path.basename(file, '.md');
return {
name: personaNameFromMarkdown(content, stem.toUpperCase()),
slug: slugify(stem),
description: content,
path: path.relative(PIPELINE_DIR, filePath),
};
});
}
/**
* Load project-level persona overrides from {projectRoot}/.forge/personas/.
* Returns a map of slug → override content.
*/
export function loadPersonaOverrides(projectRoot: string): Record<string, string> {
const overridesDir = path.join(projectRoot, '.forge', 'personas');
if (!fs.existsSync(overridesDir)) return {};
const result: Record<string, string> = {};
const files = fs.readdirSync(overridesDir).filter((f) => f.endsWith('.md'));
for (const file of files) {
const slug = slugify(path.basename(file, '.md'));
result[slug] = fs.readFileSync(path.join(overridesDir, file), 'utf-8').trim();
}
return result;
}
/**
* Load project-level Forge config from {projectRoot}/.forge/config.yaml.
* Parses simple YAML key-value pairs via regex (no YAML dependency).
*/
export function loadForgeConfig(projectRoot: string): ForgeConfig {
const configPath = path.join(projectRoot, '.forge', 'config.yaml');
if (!fs.existsSync(configPath)) return {};
const text = fs.readFileSync(configPath, 'utf-8');
const config: ForgeConfig = {};
// Parse simple list values under board: and specialists: sections
const boardAdditional = parseYamlList(text, 'additionalMembers');
const boardSkip = parseYamlList(text, 'skipMembers');
const specialistsInclude = parseYamlList(text, 'alwaysInclude');
if (boardAdditional.length > 0 || boardSkip.length > 0) {
config.board = {};
if (boardAdditional.length > 0) config.board.additionalMembers = boardAdditional;
if (boardSkip.length > 0) config.board.skipMembers = boardSkip;
}
if (specialistsInclude.length > 0) {
config.specialists = { alwaysInclude: specialistsInclude };
}
return config;
}
/**
* Parse a simple YAML list under a given key name.
*/
function parseYamlList(text: string, key: string): string[] {
const pattern = new RegExp(`${key}:\\s*\\n((?:\\s+-\\s+.+\\n?)*)`, 'm');
const match = text.match(pattern);
if (!match?.[1]) return [];
return match[1]
.split('\n')
.map((line) => line.trim().replace(/^-\s+/, '').trim())
.filter(Boolean);
}
/**
* Get effective board personas after applying project overrides and config.
*
* - Base personas loaded from pipeline/agents/board/
* - Project overrides from {projectRoot}/.forge/personas/ APPENDED to base
* - Config skipMembers removes personas; additionalMembers adds custom paths
*/
export function getEffectivePersonas(projectRoot: string, boardDir?: string): BoardPersona[] {
let personas = loadBoardPersonas(boardDir);
const overrides = loadPersonaOverrides(projectRoot);
const config = loadForgeConfig(projectRoot);
// Apply overrides — append project content to base persona description
personas = personas.map((p) => {
const override = overrides[p.slug];
if (override) {
return { ...p, description: `${p.description}\n\n${override}` };
}
return p;
});
// Apply config: skip members
if (config.board?.skipMembers?.length) {
const skip = new Set(config.board.skipMembers.map((s) => slugify(s)));
personas = personas.filter((p) => !skip.has(p.slug));
}
return personas;
}

View File

@@ -0,0 +1,348 @@
import fs from 'node:fs';
import path from 'node:path';
import { STAGE_SEQUENCE } from './constants.js';
import { determineBriefClass, stagesForClass } from './brief-classifier.js';
import { mapStageToTask } from './stage-adapter.js';
import type {
ForgeTask,
PipelineOptions,
PipelineResult,
RunManifest,
StageStatus,
TaskExecutor,
} from './types.js';
/**
* Generate a timestamp-based run ID.
*/
export function generateRunId(): string {
const now = new Date();
const pad = (n: number, w = 2) => String(n).padStart(w, '0');
return [
now.getUTCFullYear(),
pad(now.getUTCMonth() + 1),
pad(now.getUTCDate()),
'-',
pad(now.getUTCHours()),
pad(now.getUTCMinutes()),
pad(now.getUTCSeconds()),
].join('');
}
/**
* Get the ISO timestamp for now.
*/
function nowISO(): string {
return new Date().toISOString();
}
/**
* Create and persist a run manifest.
*/
function createManifest(opts: {
runId: string;
briefPath: string;
codebase: string;
briefClass: RunManifest['briefClass'];
classSource: RunManifest['classSource'];
forceBoard: boolean;
runDir: string;
}): RunManifest {
const ts = nowISO();
const manifest: RunManifest = {
runId: opts.runId,
brief: opts.briefPath,
codebase: opts.codebase,
briefClass: opts.briefClass,
classSource: opts.classSource,
forceBoard: opts.forceBoard,
createdAt: ts,
updatedAt: ts,
currentStage: '',
status: 'in_progress',
stages: {},
};
saveManifest(opts.runDir, manifest);
return manifest;
}
/**
* Save a manifest to disk.
*/
export function saveManifest(runDir: string, manifest: RunManifest): void {
manifest.updatedAt = nowISO();
const manifestPath = path.join(runDir, 'manifest.json');
fs.mkdirSync(path.dirname(manifestPath), { recursive: true });
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + '\n', 'utf-8');
}
/**
* Load a manifest from disk.
*/
export function loadManifest(runDir: string): RunManifest {
const manifestPath = path.join(runDir, 'manifest.json');
if (!fs.existsSync(manifestPath)) {
throw new Error(`manifest.json not found: ${manifestPath}`);
}
return JSON.parse(fs.readFileSync(manifestPath, 'utf-8')) as RunManifest;
}
/**
* Select and validate stages, optionally skipping to a specific stage.
*/
export function selectStages(stages?: string[], skipTo?: string): string[] {
const selected = stages ?? [...STAGE_SEQUENCE];
const unknown = selected.filter((s) => !STAGE_SEQUENCE.includes(s));
if (unknown.length > 0) {
throw new Error(`Unknown Forge stages requested: ${unknown.join(', ')}`);
}
if (!skipTo) return selected;
if (!selected.includes(skipTo)) {
throw new Error(`skip_to stage '${skipTo}' is not present in the selected stage list`);
}
const skipIndex = selected.indexOf(skipTo);
return selected.slice(skipIndex);
}
/**
* Run the Forge pipeline.
*
* 1. Classify the brief
* 2. Generate a run ID and create run directory
* 3. Map stages to tasks and submit to TaskExecutor
* 4. Track manifest with stage statuses
* 5. Return pipeline result
*/
export async function runPipeline(
briefPath: string,
projectRoot: string,
options: PipelineOptions,
): Promise<PipelineResult> {
const resolvedRoot = path.resolve(projectRoot);
const resolvedBrief = path.resolve(briefPath);
const briefContent = fs.readFileSync(resolvedBrief, 'utf-8');
// Classify brief
const { briefClass, classSource } = determineBriefClass(briefContent, options.briefClass);
// Determine stages
const classStages = options.stages ?? stagesForClass(briefClass, options.forceBoard);
const selectedStages = selectStages(classStages, options.skipTo);
// Create run directory
const runId = generateRunId();
const runDir = path.join(resolvedRoot, '.forge', 'runs', runId);
fs.mkdirSync(runDir, { recursive: true });
// Create manifest
const manifest = createManifest({
runId,
briefPath: resolvedBrief,
codebase: options.codebase ?? '',
briefClass,
classSource,
forceBoard: options.forceBoard ?? false,
runDir,
});
// Map stages to tasks
const tasks: ForgeTask[] = [];
for (let i = 0; i < selectedStages.length; i++) {
const stageName = selectedStages[i]!;
const task = mapStageToTask({
stageName,
briefContent,
projectRoot: resolvedRoot,
runId,
runDir,
});
// Override dependency chain for selected (possibly filtered) stages
if (i > 0) {
task.dependsOn = [tasks[i - 1]!.id];
} else {
delete task.dependsOn;
}
tasks.push(task);
}
// Execute stages
const { executor } = options;
for (let i = 0; i < tasks.length; i++) {
const task = tasks[i]!;
const stageName = selectedStages[i]!;
// Update manifest: stage in progress
manifest.currentStage = stageName;
manifest.stages[stageName] = {
status: 'in_progress',
startedAt: nowISO(),
};
saveManifest(runDir, manifest);
try {
await executor.submitTask(task);
const result = await executor.waitForCompletion(task.id, task.timeoutSeconds * 1000);
// Update manifest: stage completed or failed
const stageStatus: StageStatus = {
status: result.status === 'completed' ? 'passed' : 'failed',
startedAt: manifest.stages[stageName]!.startedAt,
completedAt: nowISO(),
};
manifest.stages[stageName] = stageStatus;
if (result.status !== 'completed') {
manifest.status = 'failed';
saveManifest(runDir, manifest);
throw new Error(`Stage ${stageName} failed with status: ${result.status}`);
}
saveManifest(runDir, manifest);
} catch (error) {
if (!manifest.stages[stageName]?.completedAt) {
manifest.stages[stageName] = {
status: 'failed',
startedAt: manifest.stages[stageName]?.startedAt,
completedAt: nowISO(),
};
}
manifest.status = 'failed';
saveManifest(runDir, manifest);
throw error;
}
}
// All stages passed
manifest.status = 'completed';
saveManifest(runDir, manifest);
return {
runId,
briefPath: resolvedBrief,
projectRoot: resolvedRoot,
runDir,
taskIds: tasks.map((t) => t.id),
stages: selectedStages,
manifest,
};
}
/**
* Resume a pipeline from the last incomplete stage.
*/
export async function resumePipeline(
runDir: string,
executor: TaskExecutor,
): Promise<PipelineResult> {
const manifest = loadManifest(runDir);
const resolvedRoot = path.dirname(path.dirname(path.dirname(runDir))); // .forge/runs/{id} → project root
const briefContent = fs.readFileSync(manifest.brief, 'utf-8');
const allStages = stagesForClass(manifest.briefClass, manifest.forceBoard);
// Find first non-passed stage
const resumeFrom = allStages.find((s) => manifest.stages[s]?.status !== 'passed');
if (!resumeFrom) {
manifest.status = 'completed';
saveManifest(runDir, manifest);
return {
runId: manifest.runId,
briefPath: manifest.brief,
projectRoot: resolvedRoot,
runDir,
taskIds: [],
stages: allStages,
manifest,
};
}
const remainingStages = selectStages(allStages, resumeFrom);
manifest.status = 'in_progress';
const tasks: ForgeTask[] = [];
for (let i = 0; i < remainingStages.length; i++) {
const stageName = remainingStages[i]!;
const task = mapStageToTask({
stageName,
briefContent,
projectRoot: resolvedRoot,
runId: manifest.runId,
runDir,
});
if (i > 0) {
task.dependsOn = [tasks[i - 1]!.id];
} else {
delete task.dependsOn;
}
tasks.push(task);
}
for (let i = 0; i < tasks.length; i++) {
const task = tasks[i]!;
const stageName = remainingStages[i]!;
manifest.currentStage = stageName;
manifest.stages[stageName] = {
status: 'in_progress',
startedAt: nowISO(),
};
saveManifest(runDir, manifest);
try {
await executor.submitTask(task);
const result = await executor.waitForCompletion(task.id, task.timeoutSeconds * 1000);
manifest.stages[stageName] = {
status: result.status === 'completed' ? 'passed' : 'failed',
startedAt: manifest.stages[stageName]!.startedAt,
completedAt: nowISO(),
};
if (result.status !== 'completed') {
manifest.status = 'failed';
saveManifest(runDir, manifest);
throw new Error(`Stage ${stageName} failed with status: ${result.status}`);
}
saveManifest(runDir, manifest);
} catch (error) {
if (!manifest.stages[stageName]?.completedAt) {
manifest.stages[stageName] = {
status: 'failed',
startedAt: manifest.stages[stageName]?.startedAt,
completedAt: nowISO(),
};
}
manifest.status = 'failed';
saveManifest(runDir, manifest);
throw error;
}
}
manifest.status = 'completed';
saveManifest(runDir, manifest);
return {
runId: manifest.runId,
briefPath: manifest.brief,
projectRoot: resolvedRoot,
runDir,
taskIds: tasks.map((t) => t.id),
stages: remainingStages,
manifest,
};
}
/**
* Get the status of a pipeline run.
*/
export function getPipelineStatus(runDir: string): RunManifest {
return loadManifest(runDir);
}

View File

@@ -0,0 +1,169 @@
import fs from 'node:fs';
import path from 'node:path';
import { PIPELINE_DIR, STAGE_SEQUENCE, STAGE_SPECS, STAGE_TIMEOUTS } from './constants.js';
import type { ForgeTask } from './types.js';
/**
* Generate a deterministic task ID for a stage within a run.
*/
export function stageTaskId(runId: string, stageName: string): string {
const spec = STAGE_SPECS[stageName];
if (!spec) throw new Error(`Unknown Forge stage: ${stageName}`);
return `FORGE-${runId}-${spec.number}`;
}
/**
* Get the directory for a stage's artifacts within a run.
*/
export function stageDir(runDir: string, stageName: string): string {
return path.join(runDir, stageName);
}
/**
* Get the brief path for a stage within a run.
*/
export function stageBriefPath(runDir: string, stageName: string): string {
return path.join(stageDir(runDir, stageName), 'brief.md');
}
/**
* Get the result path for a stage within a run.
*/
export function stageResultPath(runDir: string, stageName: string): string {
return path.join(stageDir(runDir, stageName), 'result.json');
}
/**
* Load a stage prompt from the pipeline assets.
*/
export function loadStagePrompt(promptFile: string): string {
const promptPath = path.join(PIPELINE_DIR, 'stages', promptFile);
return fs.readFileSync(promptPath, 'utf-8').trim();
}
/**
* Build the brief content for a stage, combining source brief with stage definition.
*/
export function buildStageBrief(opts: {
stageName: string;
stagePrompt: string;
briefContent: string;
projectRoot: string;
runId: string;
runDir: string;
}): string {
return [
`# Forge Pipeline Stage: ${opts.stageName}`,
'',
`Run ID: ${opts.runId}`,
`Project Root: ${opts.projectRoot}`,
'',
'## Source Brief',
opts.briefContent.trim(),
'',
`Read previous stage results from ${opts.runDir}/ before proceeding.`,
'',
'## Stage Definition',
opts.stagePrompt,
'',
].join('\n');
}
/**
* Write the stage brief to disk and return the path.
*/
export function writeStageBrief(opts: {
stageName: string;
briefContent: string;
projectRoot: string;
runId: string;
runDir: string;
}): string {
const spec = STAGE_SPECS[opts.stageName];
if (!spec) throw new Error(`Unknown Forge stage: ${opts.stageName}`);
const briefPath = stageBriefPath(opts.runDir, opts.stageName);
fs.mkdirSync(path.dirname(briefPath), { recursive: true });
const stagePrompt = loadStagePrompt(spec.promptFile);
const content = buildStageBrief({
stageName: opts.stageName,
stagePrompt,
briefContent: opts.briefContent,
projectRoot: opts.projectRoot,
runId: opts.runId,
runDir: opts.runDir,
});
fs.writeFileSync(briefPath, content, 'utf-8');
return briefPath;
}
/**
* Convert a Forge stage into a ForgeTask ready for submission to a TaskExecutor.
*/
export function mapStageToTask(opts: {
stageName: string;
briefContent: string;
projectRoot: string;
runId: string;
runDir: string;
}): ForgeTask {
const { stageName, briefContent, projectRoot, runId, runDir } = opts;
const spec = STAGE_SPECS[stageName];
if (!spec) throw new Error(`Unknown Forge stage: ${stageName}`);
const timeout = STAGE_TIMEOUTS[stageName];
if (timeout === undefined) {
throw new Error(`Missing stage timeout for Forge stage: ${stageName}`);
}
const briefPath = writeStageBrief({
stageName,
briefContent,
projectRoot,
runId,
runDir,
});
const resultPath = stageResultPath(runDir, stageName);
const taskId = stageTaskId(runId, stageName);
const promptPath = path.join(PIPELINE_DIR, 'stages', spec.promptFile);
const task: ForgeTask = {
id: taskId,
title: spec.title,
description: `Forge stage ${stageName} via MACP`,
status: 'pending',
dispatch: spec.dispatch,
type: spec.type,
briefPath: path.resolve(briefPath),
resultPath: path.resolve(resultPath),
timeoutSeconds: timeout,
qualityGates: [...spec.qualityGates],
metadata: {
runId,
runDir,
stageName,
stageNumber: spec.number,
gate: spec.gate,
promptPath: path.resolve(promptPath),
resultOutputPath: path.resolve(resultPath),
},
};
// Build dependency chain from stage sequence
const stageIndex = STAGE_SEQUENCE.indexOf(stageName);
if (stageIndex > 0) {
const prevStage = STAGE_SEQUENCE[stageIndex - 1]!;
task.dependsOn = [stageTaskId(runId, prevStage)];
}
// exec dispatch stages get a worktree reference
if (spec.dispatch === 'exec') {
task.worktree = path.resolve(projectRoot);
}
return task;
}

137
packages/forge/src/types.ts Normal file
View File

@@ -0,0 +1,137 @@
import type { GateEntry, TaskResult } from '@mosaic/macp';
/** Stage dispatch mode. */
export type StageDispatch = 'exec' | 'yolo' | 'pi';
/** Stage type — determines agent selection and gate requirements. */
export type StageType = 'research' | 'review' | 'coding' | 'deploy';
/** Stage specification — defines a single pipeline stage. */
export interface StageSpec {
number: string;
title: string;
dispatch: StageDispatch;
type: StageType;
gate: string;
promptFile: string;
qualityGates: (string | GateEntry)[];
}
/** Brief classification. */
export type BriefClass = 'strategic' | 'technical' | 'hotfix';
/** How the brief class was determined. */
export type ClassSource = 'cli' | 'frontmatter' | 'auto';
/** Per-stage status within a run manifest. */
export interface StageStatus {
status: 'pending' | 'in_progress' | 'passed' | 'failed';
startedAt?: string;
completedAt?: string;
}
/** Run manifest — persisted to disk as manifest.json. */
export interface RunManifest {
runId: string;
brief: string;
codebase: string;
briefClass: BriefClass;
classSource: ClassSource;
forceBoard: boolean;
createdAt: string;
updatedAt: string;
currentStage: string;
status: 'in_progress' | 'completed' | 'failed' | 'interrupted' | 'rejected';
stages: Record<string, StageStatus>;
}
/** Task status for the executor. */
export type ForgeTaskStatus =
| 'pending'
| 'running'
| 'completed'
| 'failed'
| 'gated'
| 'escalated';
/** Task submitted to a TaskExecutor. */
export interface ForgeTask {
id: string;
title: string;
description: string;
status: ForgeTaskStatus;
type: StageType;
dispatch: StageDispatch;
briefPath: string;
resultPath: string;
timeoutSeconds: number;
qualityGates: (string | GateEntry)[];
worktree?: string;
command?: string;
dependsOn?: string[];
dependsOnPolicy?: 'all' | 'any' | 'all_terminal';
metadata: Record<string, unknown>;
}
/** Abstract task executor — decouples from packages/coord. */
export interface TaskExecutor {
submitTask(task: ForgeTask): Promise<void>;
waitForCompletion(taskId: string, timeoutMs: number): Promise<TaskResult>;
getTaskStatus(taskId: string): Promise<ForgeTaskStatus>;
}
/** Board persona loaded from markdown. */
export interface BoardPersona {
name: string;
slug: string;
description: string;
path: string;
}
/** Board review result from a single persona. */
export interface PersonaReview {
persona: string;
verdict: 'approve' | 'reject' | 'conditional';
confidence: number;
concerns: string[];
recommendations: string[];
keyRisks: string[];
}
/** Board synthesis result merging all persona reviews. */
export interface BoardSynthesis extends PersonaReview {
reviews: PersonaReview[];
}
/** Project-level Forge configuration (.forge/config.yaml). */
export interface ForgeConfig {
board?: {
additionalMembers?: string[];
skipMembers?: string[];
};
specialists?: {
alwaysInclude?: string[];
};
}
/** Options for running a pipeline. */
export interface PipelineOptions {
briefClass?: BriefClass;
forceBoard?: boolean;
codebase?: string;
stages?: string[];
skipTo?: string;
dryRun?: boolean;
executor: TaskExecutor;
}
/** Pipeline run result. */
export interface PipelineResult {
runId: string;
briefPath: string;
projectRoot: string;
runDir: string;
taskIds: string[];
stages: string[];
manifest: RunManifest;
}