Files
deepseek-harness/scripts/persistence-artifacts.ts
T
imccyu 1e24617aad perf(docs): parse each Markdown document once in documentation gates
markdown links parsed anchor targets twice more and probed existence
without a cache; translation pairing reparsed every document after link
normalization and globbed four times; the README model-experience and
limitations checks parsed each README three times; type equivalence
parsed all 1,835 documents; Session format references reparsed each pair
four times. Each gate now parses a document once: links and anchors come
from one tree, pairing reuses the original tree when every rewritten
destination keeps the block structure and falls back to a full reparse
otherwise, the README checks share one tree, a text prefilter skips
documents that cannot hold a source-equivalence fence, and the
persistence renderer passes its trees on. Outputs are byte-identical on
the repository, and seeded faults are reported identically.

Refs #5852
2026-10-12 11:06:53 +08:00

56 lines
2.8 KiB
TypeScript

/** Render complete persistence documentation pairs without Git or file mutation. */
import { readFileSync } from 'node:fs'
import { hasLanguageSwitcher } from './translation-links.ts'
import {
computeTranslationPairingRecord, renderTranslationPairingRecord, translationPairPaths,
} from './translation-pairing-record.ts'
import {
languageSwitcherTargets, parseTranslationMarkdown, parseTranslationPairingManifest,
requiresSourceLanguageSwitcher, translationPairSourcePredicate,
translationStructureDiff, translationStructureSignature,
} from './translation-pairing.ts'
const isTranslationPairSource = translationPairSourcePredicate(parseTranslationPairingManifest(
readFileSync(new URL('./translation-pairing.manifest.json', import.meta.url), 'utf8'),
))
/** One repository-relative generated file and its complete UTF-8 content. */
export interface PersistenceArtifact {
readonly path: string
readonly content: string
}
/**
* Check a pair's code, structure and localized links, then render its three files.
* Link existence remains the Markdown gate's responsibility.
* @param root - checkout root used to resolve relative link identities.
* @param source - repository-relative English document path.
* @param en - complete authored or generated English Markdown.
* @param zh - complete authored or generated Chinese Markdown.
* @returns the documents and their matching consistency sidecar, without writing files.
*/
export function renderPersistencePair(root: string, source: string, en: string, zh: string): PersistenceArtifact[] {
const paths = translationPairPaths(source)
const sourceTree = parseTranslationMarkdown(en)
const zhTree = parseTranslationMarkdown(zh)
const sourceTargets = languageSwitcherTargets(paths.source)
const zhTargets = languageSwitcherTargets(paths.zh)
if (!hasLanguageSwitcher(zhTree, zh, sourceTargets)
|| requiresSourceLanguageSwitcher(source) && !hasLanguageSwitcher(sourceTree, en, zhTargets)) {
throw new Error(`${source}: both authored languages need their counterpart switcher`)
}
const context = { repoRoot: root, isTranslationPairSource, repositoryFileExists: () => true }
const errors = translationStructureDiff(
translationStructureSignature(sourceTree, zhTargets, { ...context, sourcePath: paths.source, markdown: en }),
translationStructureSignature(zhTree, sourceTargets, { ...context, sourcePath: paths.zh, markdown: zh }),
)
if (errors.length > 0) throw new Error(`${source}: bilingual structure mismatch: ${errors.join('; ')}`)
const record = computeTranslationPairingRecord(paths, en, zh, context, { en: sourceTree, zh: zhTree })
return [
{ path: paths.source, content: en },
{ path: paths.zh, content: zh },
{ path: paths.meta, content: renderTranslationPairingRecord(paths, record) },
]
}