Explorar el Código

fix: run paragraph shape detection on final content before persist

Previously paragraph drift detection only ran on the initial draft.
Now it also runs on finalContent (after normalize + auto-revise)
right before saveChapter, catching fragmentation introduced by
intermediate pipeline stages.

Addresses #90
Ma hace 7 meses
padre
commit
7a4a486698
Se han modificado 1 ficheros con 28 adiciones y 0 borrados
  1. 28 0
      packages/core/src/pipeline/runner.ts

+ 28 - 0
packages/core/src/pipeline/runner.ts

@@ -1176,6 +1176,34 @@ export class PipelineRunner {
       throw new Error(`State validation failed for chapter ${chapterNumber}: ${reason}`);
     }
 
+    // 4.2 Final paragraph shape check on persisted content (post-normalize, post-revise)
+    {
+      const { detectParagraphLengthDrift } = await import("../agents/post-write-validator.js");
+      const chapDir = join(bookDir, "chapters");
+      const recentFiles = (await readdir(chapDir).catch(() => [] as string[]))
+        .filter((f) => f.endsWith(".md") && /^\d{4}/.test(f))
+        .sort()
+        .slice(-5);
+      const recentContent = (await Promise.all(
+        recentFiles.map((f) => readFile(join(chapDir, f), "utf-8").catch(() => "")),
+      )).join("\n\n");
+      const paragraphIssues = detectParagraphLengthDrift(finalContent, recentContent, pipelineLang);
+      if (paragraphIssues.length > 0) {
+        for (const issue of paragraphIssues) {
+          this.config.logger?.warn(`[paragraph] ${issue.description}`);
+        }
+        auditResult = {
+          ...auditResult,
+          issues: [...auditResult.issues, ...paragraphIssues.map((v) => ({
+            severity: v.severity as "warning",
+            category: "paragraph-shape",
+            description: v.description,
+            suggestion: v.description,
+          }))],
+        };
+      }
+    }
+
     await writer.saveChapter(bookDir, persistenceOutput, gp.numericalSystem, pipelineLang);
     await writer.saveNewTruthFiles(bookDir, persistenceOutput, pipelineLang);
     await this.syncLegacyStructuredStateFromMarkdown(bookDir, chapterNumber, persistenceOutput);