open-agreements
Version:
Open-source legal template filling CLI and library
151 lines • 7.12 kB
JavaScript
import { readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
import AdmZip from 'adm-zip';
import { parseReplacementKey } from './replacement-keys.js';
import { extractAllText } from './verifier.js';
import { resolveSelectorContracts } from '../selectors/index.js';
const SHORT_PLACEHOLDER_MAX = 80;
/**
* Resolve every selector field's occurrence locators against the SOURCE DOCX
* (safe-docx parse path) and report drift: any unresolved occurrence or failed
* assertion means an upstream change broke a selector. This is the standing
* "did the form change under us?" gate; it runs at canary time, not fill time.
*/
export async function checkSelectorDrift(sourcePath, manifests) {
if (manifests.length === 0) {
return { unresolved_selector_fields: [], assertion_failures: [] };
}
const { fields } = await resolveSelectorContracts(sourcePath, manifests);
const unresolved = [];
const assertionFailures = [];
for (const field of fields) {
if (field.unresolved)
unresolved.push(field.field_id);
for (const failure of field.assertionFailures) {
assertionFailures.push(`${field.field_id}[occ ${failure.occurrenceIndex}] ${failure.kind}: ${failure.detail ?? 'mismatch'}`);
}
}
return {
unresolved_selector_fields: [...new Set(unresolved)].sort(),
assertion_failures: assertionFailures.sort(),
};
}
function extractDocumentParagraphs(docxPath) {
const zip = new AdmZip(docxPath);
const entry = zip.getEntry('word/document.xml');
if (!entry)
return [];
const xml = entry.getData().toString('utf-8');
const paragraphs = [];
const paraRegex = /<w:p[\s>][\s\S]*?<\/w:p>/g;
let paraMatch = null;
while ((paraMatch = paraRegex.exec(xml)) !== null) {
const paraXml = paraMatch[0];
const parts = [];
const tRegex = /<w:t[^>]*>([^<]*)<\/w:t>/g;
let tMatch = null;
while ((tMatch = tRegex.exec(paraXml)) !== null) {
parts.push(tMatch[1]);
}
const text = parts.join('').trimStart();
if (text.length > 0) {
paragraphs.push(text);
}
}
return paragraphs;
}
function extractBracketTokens(text) {
return [...text.matchAll(/\[([^\]]+)\]/g)].map((m) => m[1]);
}
export function computeSourceStructureSignature(docxPath) {
const paragraphs = extractDocumentParagraphs(docxPath);
const allBracketTokens = paragraphs.flatMap((paragraph) => extractBracketTokens(paragraph));
const uniqueShortPlaceholders = [...new Set(allBracketTokens
.filter((token) => token.length <= SHORT_PLACEHOLDER_MAX)
.map((token) => `[${token}]`))].sort();
const longClauseCount = allBracketTokens.filter((token) => token.length > SHORT_PLACEHOLDER_MAX).length;
const headingLikeCount = paragraphs.filter((p) => /^\[[A-Z][^\]]*$/.test(p)).length;
return {
paragraph_count: paragraphs.length,
heading_like_count: headingLikeCount,
short_placeholder_count: uniqueShortPlaceholders.length,
long_clause_count: longClauseCount,
unique_short_placeholders: uniqueShortPlaceholders,
};
}
function computeSha256Hex(path) {
return createHash('sha256').update(readFileSync(path)).digest('hex');
}
function uniqueSorted(values) {
return [...new Set(values)].sort();
}
function normalizeAnchorWhitespace(value) {
return value.replaceAll('\u00a0', ' ').replace(/\s+/g, ' ');
}
export function checkFieldSelectorSourceDrift(input) {
const { fieldSelectorId, sourcePath, metadata, replacements, normalizeConfig, selectorDrift } = input;
const paragraphs = extractDocumentParagraphs(sourcePath);
const documentText = paragraphs.join('\n');
const normalizedDocumentText = normalizeAnchorWhitespace(extractAllText(sourcePath));
const actualSha = computeSha256Hex(sourcePath);
const hashMatch = metadata.source_sha256 ? metadata.source_sha256 === actualSha : true;
const replacementGroups = new Map();
for (const [key, value] of Object.entries(replacements)) {
const searchText = parseReplacementKey(key, value).searchText;
if (!replacementGroups.has(value)) {
replacementGroups.set(value, new Set());
}
replacementGroups.get(value).add(searchText);
}
const missingReplacementGroups = [];
for (const anchors of replacementGroups.values()) {
const anchorList = [...anchors].sort((a, b) => a.length - b.length || a.localeCompare(b));
const hasAnyAnchor = anchorList.some((anchor) => normalizedDocumentText.includes(normalizeAnchorWhitespace(anchor)));
if (!hasAnyAnchor) {
missingReplacementGroups.push(anchorList[0]);
}
}
const missingNormalizeHeadingAnchors = uniqueSorted((normalizeConfig?.paragraph_rules ?? [])
.filter((rule) => rule.source_drift?.stage !== 'post_transform')
.filter((rule) => !rule.ignore_heading)
.filter((rule) => {
const headingAnchors = [rule.section_heading, ...(rule.section_heading_any ?? [])];
return !headingAnchors.some((anchor) => documentText.includes(anchor));
})
.map((rule) => rule.id));
const missingNormalizeParagraphAnchors = uniqueSorted((normalizeConfig?.paragraph_rules ?? [])
.filter((rule) => rule.source_drift?.stage !== 'post_transform')
.map((rule) => rule.paragraph_contains)
.filter((anchor) => !documentText.includes(anchor)));
const missingNormalizeParagraphEndAnchors = uniqueSorted((normalizeConfig?.paragraph_rules ?? [])
.filter((rule) => rule.source_drift?.stage !== 'post_transform')
.map((rule) => rule.paragraph_end_contains)
.filter((anchor) => typeof anchor === 'string' && anchor.length > 0)
.filter((anchor) => !documentText.includes(anchor)));
const diff = {
missing_replacement_anchor_groups: uniqueSorted(missingReplacementGroups),
missing_normalize_heading_anchors: missingNormalizeHeadingAnchors,
missing_normalize_paragraph_anchors: missingNormalizeParagraphAnchors,
missing_normalize_paragraph_end_anchors: missingNormalizeParagraphEndAnchors,
unresolved_selector_fields: selectorDrift?.unresolved_selector_fields ?? [],
assertion_failures: selectorDrift?.assertion_failures ?? [],
};
const ok = hashMatch
&& diff.missing_replacement_anchor_groups.length === 0
&& diff.missing_normalize_heading_anchors.length === 0
&& diff.missing_normalize_paragraph_anchors.length === 0
&& diff.missing_normalize_paragraph_end_anchors.length === 0
&& diff.unresolved_selector_fields.length === 0
&& diff.assertion_failures.length === 0;
return {
field_selector_id: fieldSelectorId,
source_path: sourcePath,
expected_sha256: metadata.source_sha256,
actual_sha256: actualSha,
hash_match: hashMatch,
structure: computeSourceStructureSignature(sourcePath),
diff,
ok,
};
}
//# sourceMappingURL=source-drift.js.map