feat(prdy): add PrdService as the single PRD authority surface (#1275)

- PrdService owns create/read/update/link/import/export; wizard and package
  CLI become prompt layers over the service (no second writer path)
- PRD documents gain a content version and a missions linkage array persisted
  in the YAML authority store (survives restart); linkage writes do not bump
  the content version
- exportMarkdown renders a labeled generated view (id + version + do-not-edit
  header) that no code path reads back; the store loads .yaml/.yml only
- importDocument validates structure with zod before anything else, persists
  valid imports as draft (validity is not approval), and refuses conflicts
  with a typed PrdImportConflictError carrying a proposed successor; the
  original document stays byte-identical until acceptSuccessor
- raw store writers (createPrd/savePrd) are no longer exported from the
  package entry point
This commit is contained in:
fargo
2026-08-17 16:46:00 -05:00
parent 8199261caa
commit e291bfb837
7 changed files with 1065 additions and 48 deletions
+37 -1
View File
@@ -17,17 +17,49 @@ const prdSectionSchema = z.object({
fields: z.record(z.string(), z.string()),
});
const prdMissionLinkageSchema = z.object({
missionId: z.string().min(1),
missionVersion: z.string().min(1),
prdVersion: z.number().int().min(1),
requirementIds: z.array(z.string()),
linkedAt: z.string().datetime(),
});
const prdDocumentSchema = z.object({
id: z.string().min(1),
title: z.string().min(1),
status: z.enum(['draft', 'review', 'approved', 'archived']),
projectPath: z.string().min(1),
template: z.string().min(1),
// Defaults keep documents written by older prdy versions loadable.
version: z.number().int().min(1).default(1),
sections: z.array(prdSectionSchema),
missions: z.array(prdMissionLinkageSchema).default([]),
createdAt: z.string().datetime(),
updatedAt: z.string().datetime(),
});
/** YAML timestamp scalars are parsed as Date by some emitters — normalize to ISO strings. */
function coerceTimestamps(value: unknown): unknown {
if (value instanceof Date) {
return value.toISOString();
}
if (Array.isArray(value)) {
return value.map(coerceTimestamps);
}
if (typeof value === 'object' && value !== null) {
return Object.fromEntries(
Object.entries(value).map(([key, entry]) => [key, coerceTimestamps(entry)]),
);
}
return value;
}
/** Validate an unknown value as a PRD document (throws zod errors on failure). */
export function parsePrdDocument(value: unknown): PrdDocument {
return prdDocumentSchema.parse(coerceTimestamps(value)) as PrdDocument;
}
function expandHome(projectPath: string): string {
if (!projectPath.startsWith('~')) {
return projectPath;
@@ -74,6 +106,8 @@ function prdDirectory(projectPath: string): string {
return path.join(projectPath, PRD_DIRECTORY);
}
export { prdDirectory };
function prdFilePath(projectPath: string, id: string): string {
return path.join(prdDirectory(projectPath), `${id}.yaml`);
}
@@ -113,11 +147,13 @@ export async function createPrd(options: CreatePrdOptions): Promise<PrdDocument>
status: 'draft',
projectPath: resolvedProjectPath,
template: template.id,
version: 1,
sections: template.sections.map((section) => ({
id: section.id,
title: section.title,
fields: Object.fromEntries(section.fields.map((field) => [field, ''])),
})),
missions: [],
createdAt: now,
updatedAt: now,
};
@@ -190,7 +226,7 @@ export async function listPrds(projectPath: string): Promise<PrdDocument[]> {
throw new Error(`Failed to parse PRD file ${filePath}: ${String(error)}`);
}
const document = prdDocumentSchema.parse(parsed);
const document = parsePrdDocument(parsed);
documents.push(document);
}