import * as fs from 'node:fs'; import * as path from 'node:path'; import { getSchemaDir, resolveSchema, listSchemasWithInfo } from './resolver.js'; import { ArtifactGraph } from './graph.js'; import { detectCompleted } from './state.js'; import { isSpecsArtifactPath, resolveArtifactOutputPath, resolveArtifactOutputs, } from './outputs.js'; import { formatUnknownChangeMetadataKeysMessage, readChangeMetadata, readUnknownChangeMetadataKeys, resolveSchemaForChange, } from '../../utils/change-metadata.js'; import { FileSystemUtils } from '../../utils/file-system.js'; import { buildActionContext, buildNextSteps, summarizePlanningHome, type ActionContext, type PlanningHomeSummary, } from '../change-status-policy.js'; import { readProjectConfig, validateConfigRules, type ProjectConfig } from '../project-config.js'; import type { ReferenceIndexEntry } from '../references.js'; import type { PlanningHome } from '../planning-home.js'; import type { ChangeMetadata } from '../change-metadata/index.js'; import type { Artifact, CompletedSet } from './types.js'; // Session-level cache for validation warnings (avoid repeating same warnings) const shownWarnings = new Set(); /** * Error thrown when loading a template fails. */ export class TemplateLoadError extends Error { constructor( message: string, public readonly templatePath: string ) { super(message); this.name = 'TemplateLoadError'; } } /** * Change context containing graph, completion state, and metadata. */ export interface ChangeContext { /** The artifact dependency graph */ graph: ArtifactGraph; /** Set of completed artifact IDs */ completed: CompletedSet; /** Schema name being used */ schemaName: string; /** Change name */ changeName: string; /** Path to the change directory */ changeDir: string; /** Project root directory */ projectRoot: string; /** Resolved planning home for this change */ planningHome?: PlanningHome; /** Parsed change metadata, when present */ metadata?: ChangeMetadata; /** Non-fatal metadata diagnostics for text and JSON command surfaces */ warnings?: string[]; /** * Artifact IDs counted as complete only because the change declares * skip_specs, not because their files exist. Kept separate so status can * render them as skipped rather than done. */ skippedArtifacts?: Set; } export interface LoadChangeContextOptions { changeDir?: string; planningHome?: PlanningHome; /** Pre-read project config; suppresses schema resolution's fallback config read. */ projectConfig?: ProjectConfig | null; } /** * Enriched instructions for creating an artifact. */ export interface ArtifactInstructions { /** Change name */ changeName: string; /** Artifact ID */ artifactId: string; /** Schema name */ schemaName: string; /** Full path to change directory */ changeDir: string; /** Resolved planning home for this change */ planningHome?: PlanningHomeSummary; /** Output path pattern (e.g., "proposal.md") */ outputPath: string; /** Absolute output path or glob pattern resolved under the change directory */ resolvedOutputPath: string; /** Existing concrete output files for this artifact */ existingOutputPaths: string[]; /** Artifact description */ description: string; /** Guidance on how to create this artifact (from schema instruction field) */ instruction: string | undefined; /** Project context from config (constraints/background for AI, not to be included in output) */ context: string | undefined; /** Artifact-specific rules from config (constraints for AI, not to be included in output) */ rules: string[] | undefined; /** Referenced-store index (read-only upstream context; omitted when no references are declared) */ references?: ReferenceIndexEntry[]; /** Template content (structure to follow - this IS the output format) */ template: string; /** Dependencies with completion status and paths */ dependencies: DependencyInfo[]; /** Artifacts that become available after completing this one */ unlocks: string[]; /** True when the change declares skip_specs and this artifact is skipped */ skipped?: boolean; /** Present only when skipped: tells the consumer not to create the artifact */ warning?: string; /** Non-fatal metadata diagnostics */ warnings?: string[]; } /** * Warning attached to instructions for an artifact skipped via skip_specs. * Carried in the JSON payload too, so agents driving the CLI with --json see * the same do-not-create signal as the text output. */ export const SKIP_SPECS_INSTRUCTIONS_WARNING = 'This change declares skip_specs: true in .openspec.yaml (no spec-level behavior changes), so this artifact is skipped.\n' + 'Do not create spec files - they will conflict with that marker. If requirements now change, remove skip_specs from .openspec.yaml and rerun this command.'; /** * Dependency information including path and description. */ export interface DependencyInfo { /** Artifact ID */ id: string; /** Whether the dependency is completed */ done: boolean; /** Relative output path of the dependency (e.g., "proposal.md") */ path: string; /** Description of the dependency artifact */ description: string; /** True when the dependency is satisfied via skip_specs - no files exist to read */ skipped?: boolean; } /** * Status of a single artifact in the workflow. */ export interface ArtifactStatus { /** Artifact ID */ id: string; /** Output path pattern */ outputPath: string; /** Status: done, skipped (via skip_specs), ready, or blocked */ status: 'done' | 'skipped' | 'ready' | 'blocked'; /** Artifact IDs this artifact directly requires (its `requires` edges). * Present for every status so callers can compute the transitive required * set even when the artifact is already `done` (file-existence status does * not imply its dependencies exist). */ requires: string[]; /** Missing dependencies (only for blocked) */ missingDeps?: string[]; } /** * Formatted change status. */ export interface ChangeStatus { /** Change name */ changeName: string; /** Schema name */ schemaName: string; /** Planning home facts (generated skills derive the archive dir * from planningHome.changesDir - a published agent contract). */ planningHome?: PlanningHomeSummary; /** Full path to the change root */ changeRoot: string; /** Absolute artifact path details keyed by artifact ID */ artifactPaths: Record; /** Plain-language next steps for users and agents */ nextSteps: string[]; /** Machine-readable action constraints for agents */ actionContext: ActionContext; /** Whether all planning artifacts are complete */ isPlanningComplete: boolean; /** Compatibility alias for isPlanningComplete */ isComplete: boolean; /** Artifact IDs required before apply phase (from schema's apply.requires) */ applyRequires: string[]; /** Status of each artifact */ artifacts: ArtifactStatus[]; /** Non-fatal metadata diagnostics */ warnings?: string[]; } export interface ArtifactPathSummary { outputPath: string; resolvedOutputPath: string; existingOutputPaths: string[]; } /** * Loads a template from a schema's templates directory. * * @param schemaName - Schema name (e.g., "spec-driven") * @param templatePath - Relative path within the templates directory (e.g., "proposal.md") * @param projectRoot - Optional project root for project-local schema resolution * @returns The template content * @throws TemplateLoadError if the template cannot be loaded */ export function loadTemplate( schemaName: string, templatePath: string, projectRoot?: string ): string { const schemaDir = getSchemaDir(schemaName, projectRoot); if (!schemaDir) { throw new TemplateLoadError( `Schema '${schemaName}' not found`, templatePath ); } const templatesDir = path.join(schemaDir, 'templates'); const templatePathOnDisk = path.join(templatesDir, templatePath); try { FileSystemUtils.assertPathWithin(templatesDir, templatePathOnDisk); } catch (error) { throw new TemplateLoadError( error instanceof Error ? error.message : String(error), templatePathOnDisk ); } if (!fs.existsSync(templatePathOnDisk)) { throw new TemplateLoadError( `Template not found: ${templatePathOnDisk}`, templatePathOnDisk ); } const fullPath = FileSystemUtils.canonicalizeExistingPath(templatePathOnDisk); try { return fs.readFileSync(fullPath, 'utf-8'); } catch (err) { const ioError = err instanceof Error ? err : new Error(String(err)); throw new TemplateLoadError( `Failed to read template: ${ioError.message}`, fullPath ); } } /** * Loads change context combining graph and completion state. * * Schema resolution order: * 1. Explicit schemaName parameter (if provided) * 2. Schema from .openspec.yaml metadata (if exists in change directory) * 3. Default 'spec-driven' * * @param projectRoot - Project root directory * @param changeName - Change name * @param schemaName - Optional schema name override. If not provided, auto-detected from metadata. * @returns Change context with graph, completed set, and metadata */ export function loadChangeContext( projectRoot: string, changeName: string, schemaName?: string, options: LoadChangeContextOptions = {} ): ChangeContext { const changeDir = FileSystemUtils.canonicalizeExistingPath( options.changeDir ?? path.join(projectRoot, 'openspec', 'changes', changeName) ); const metadata = readChangeMetadata(changeDir, projectRoot) ?? undefined; const unknownMetadataKeys = readUnknownChangeMetadataKeys(changeDir); const warnings = unknownMetadataKeys.length > 0 ? [formatUnknownChangeMetadataKeysMessage(unknownMetadataKeys)] : []; const resolvedSchemaName = resolveSchemaForChange(changeDir, schemaName, projectRoot, { metadata: metadata ?? null, projectConfig: options.projectConfig, }); const schema = resolveSchema(resolvedSchemaName, projectRoot); const graph = ArtifactGraph.fromSchema(schema); const completed = detectCompleted(graph, changeDir); // A change that declares skip_specs has no spec deltas by design, so // artifacts generating into specs/ count as complete; otherwise the graph // would block their dependents (e.g. tasks) on files that must not exist. // Tracked separately so status renders them as skipped, not done. const skippedArtifacts = new Set(); if (metadata?.skip_specs) { for (const artifact of graph.getAllArtifacts()) { if (isSpecsArtifactPath(artifact.generates) && !completed.has(artifact.id)) { completed.add(artifact.id); skippedArtifacts.add(artifact.id); } } } return { graph, completed, schemaName: resolvedSchemaName, changeName, changeDir, projectRoot, ...(options.planningHome ? { planningHome: options.planningHome } : {}), ...(metadata ? { metadata } : {}), ...(warnings.length > 0 ? { warnings } : {}), ...(skippedArtifacts.size > 0 ? { skippedArtifacts } : {}), }; } /** * Generates enriched instructions for creating an artifact. * * Instruction injection order: * 1. - Project context from config (if present) * 2. - Artifact-specific rules from config (if present) * 3.