Этап 4 / Волна 19.1 выравнивание живого контура для слоя адресного сбора доказательной базы

This commit is contained in:
2026-03-29 08:33:54 +03:00
parent 60d8b96a14
commit d461cedf35
19 changed files with 2389 additions and 66 deletions
+3 -1
View File
@@ -845,7 +845,9 @@ function collectDateLikeSpans(text) {
const spans = [];
const patterns = [
/\b\d{1,2}[./-]\d{1,2}[./-]\d{2,4}\b/g,
/\b20\d{2}[./-](?:0[1-9]|1[0-2])[./-](?:0[1-9]|[12]\d|3[01])\b/g
/\b20\d{2}(?:[./-](?:0[1-9]|1[0-2]))(?:[./-](?:0[1-9]|[12]\d|3[01]))?\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])\s+(?:январ[ьяе]|феврал[ьяе]|март[ае]?|апрел[ьяе]|ма[йея]|июн[ьяе]?|июл[ьяе]?|август[ае]?|сентябр[ьяе]?|октябр[ьяе]?|ноябр[ьяе]?|декабр[ьяе]?|january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+20\d{2})?\b/giu,
/\b(?:январ[ьяе]|феврал[ьяе]|март[ае]?|апрел[ьяе]|ма[йея]|июн[ьяе]?|июл[ьяе]?|август[ае]?|сентябр[ьяе]?|октябр[ьяе]?|ноябр[ьяе]?|декабр[ьяе]?|january|february|march|april|may|june|july|august|september|october|november|december)\s+20\d{2}\b/giu
];
for (const pattern of patterns) {
let match = null;
+227 -15
View File
@@ -102,14 +102,66 @@ function accountPrefix(value) {
const match = token.match(/^(\d{2})/);
return match ? match[1] : null;
}
function extractAccountsFromText(text) {
function collectDateLikeSpans(text) {
const spans = [];
const patterns = [
/\b20\d{2}(?:[-/.](?:0?[1-9]|1[0-2]))(?:[-/.](?:0?[1-9]|[12]\d|3[01]))?\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:\d{2}|\d{4})\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])\s+(?:январ[ьяе]|феврал[ьяе]|март[ае]?|апрел[ьяе]|ма[йея]|июн[ьяе]?|июл[ьяе]?|август[ае]?|сентябр[ьяе]?|октябр[ьяе]?|ноябр[ьяе]?|декабр[ьяе]?|january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+20\d{2})?\b/giu
];
for (const pattern of patterns) {
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
}
return spans;
}
function collectAmountLikeSpans(text) {
const spans = [];
const patterns = [/\b\d{1,3}(?:[ \u00A0]\d{3})+(?:[.,]\d{2})?\b/g, /\b\d+[.,]\d{2}\b/g];
for (const pattern of patterns) {
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
}
return spans;
}
function collectPercentLikeSpans(text) {
const spans = [];
const pattern = /\b\d{1,3}(?:[.,]\d+)?\s*%/g;
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
return spans;
}
function intersectsSpan(start, end, spans) {
return spans.some((span) => start < span.end && end > span.start);
}
function extractAccountsFromTextDetailed(text, options) {
const lower = String(text ?? "").toLowerCase();
const accounts = new Set();
const contextualPattern = /(?:\b(?:СЃС‡(?:Рµ|С‘)С‚(?:Р°|Сѓ|РѕРј|РѕРІ)?|account|schet)\b\s*(?:в„–|#|:)?\s*)(\d{2}(?:\.\d{2})?)/giu;
const dateSpans = collectDateLikeSpans(lower);
const amountSpans = collectAmountLikeSpans(lower);
const percentSpans = collectPercentLikeSpans(lower);
const blockedSpans = [...dateSpans, ...amountSpans, ...percentSpans];
const contextualPattern = /(?:\b(?:счет(?:а|у|ом|ов)?|сч\.?|account(?:s)?|schet(?:a|u|om|ov)?)\b\s*(?:№|#|:)?\s*)(\d{2}(?:\.\d{2})?)/giu;
let contextualMatch = null;
while ((contextualMatch = contextualPattern.exec(lower)) !== null) {
const token = String(contextualMatch[1] ?? "").trim();
if (token) {
const prefix = token.match(/^(\d{2})/)?.[1] ?? null;
if (token && prefix && KNOWN_ACCOUNT_PREFIXES.has(prefix)) {
accounts.add(token);
}
}
@@ -118,36 +170,130 @@ function extractAccountsFromText(text) {
while ((pairMatch = pairPattern.exec(lower)) !== null) {
const left = String(pairMatch[1] ?? "").trim();
const right = String(pairMatch[2] ?? "").trim();
if (left)
const leftPrefix = left.match(/^(\d{2})/)?.[1] ?? null;
const rightPrefix = right.match(/^(\d{2})/)?.[1] ?? null;
if (left && leftPrefix && KNOWN_ACCOUNT_PREFIXES.has(leftPrefix))
accounts.add(left);
if (right)
if (right && rightPrefix && KNOWN_ACCOUNT_PREFIXES.has(rightPrefix))
accounts.add(right);
}
const genericAccountPattern = /\b(\d{2}(?:\.\d{2})?)\b/g;
let genericMatch = null;
const classifiedNumericTokens = [];
const rejectedAsNonAccounts = new Set();
while ((genericMatch = genericAccountPattern.exec(lower)) !== null) {
const token = String(genericMatch[1] ?? "").trim();
const start = genericMatch.index;
const end = start + token.length;
const prefix = token.match(/^(\d{2})/)?.[1] ?? null;
if (options?.forceAccountContext === true && prefix && KNOWN_ACCOUNT_PREFIXES.has(prefix)) {
accounts.add(token);
classifiedNumericTokens.push({
token,
classification: "account_token"
});
continue;
}
if (intersectsSpan(start, end, dateSpans)) {
classifiedNumericTokens.push({
token,
classification: "date_token"
});
rejectedAsNonAccounts.add(token);
continue;
}
if (intersectsSpan(start, end, amountSpans)) {
classifiedNumericTokens.push({
token,
classification: "amount_token"
});
rejectedAsNonAccounts.add(token);
continue;
}
if (intersectsSpan(start, end, percentSpans)) {
classifiedNumericTokens.push({
token,
classification: "percent_token"
});
rejectedAsNonAccounts.add(token);
continue;
}
if (!prefix || !KNOWN_ACCOUNT_PREFIXES.has(prefix)) {
classifiedNumericTokens.push({
token,
classification: "other_numeric"
});
rejectedAsNonAccounts.add(token);
continue;
}
accounts.add(token);
classifiedNumericTokens.push({
token,
classification: "account_token"
});
}
return Array.from(accounts);
const rawNumericTokens = uniqueStrings((lower.match(/\b\d{1,4}(?:[.,]\d{1,4})?\b/g) ?? []).map((item) => String(item)));
for (const token of accounts) {
if (!classifiedNumericTokens.some((item) => item.token === token && item.classification === "account_token")) {
classifiedNumericTokens.push({
token,
classification: "account_token"
});
}
}
// Numeric tokens hidden behind blocked spans still need explicit audit markers.
const blockedMatchPattern = /\b\d{2}(?:\.\d{2})?\b/g;
let blockedMatch = null;
while ((blockedMatch = blockedMatchPattern.exec(lower)) !== null) {
const token = String(blockedMatch[0] ?? "").trim();
const start = blockedMatch.index;
const end = start + token.length;
if (!intersectsSpan(start, end, blockedSpans)) {
continue;
}
if (classifiedNumericTokens.some((item) => item.token === token)) {
continue;
}
const classification = intersectsSpan(start, end, dateSpans)
? "date_token"
: intersectsSpan(start, end, amountSpans)
? "amount_token"
: "percent_token";
classifiedNumericTokens.push({
token,
classification
});
rejectedAsNonAccounts.add(token);
}
return {
resolved_account_anchors: Array.from(accounts),
raw_numeric_tokens: rawNumericTokens,
classified_numeric_tokens: classifiedNumericTokens,
rejected_as_non_accounts: Array.from(rejectedAsNonAccounts)
};
}
function extractAccountsFromUnknown(value) {
function extractAccountsFromText(text) {
return extractAccountsFromTextDetailed(text).resolved_account_anchors;
}
function extractAccountsFromUnknown(value, pathKey = "") {
if (Array.isArray(value)) {
return uniqueStrings(value.flatMap((item) => extractAccountsFromUnknown(item)));
return uniqueStrings(value.flatMap((item) => extractAccountsFromUnknown(item, pathKey)));
}
if (value && typeof value === "object") {
return uniqueStrings(Object.values(value).flatMap((item) => extractAccountsFromUnknown(item)));
return uniqueStrings(Object.entries(value).flatMap(([key, item]) => extractAccountsFromUnknown(item, `${pathKey}.${String(key).toLowerCase()}`)));
}
if (typeof value !== "string" && typeof value !== "number") {
return [];
}
const text = String(value);
const matches = text.match(/\b\d{2}(?:\.\d{2})?\b/g) ?? [];
return uniqueStrings(matches);
const contextPath = String(pathKey ?? "").toLowerCase();
if (contextPath.length > 0 &&
!/(?:account|счет|сч|debit|credit|дт|кт|konto|subkonto|analytics|context)/iu.test(contextPath)) {
return [];
}
const forceAccountContext = /(?:account|счет|сч|debit|credit|дт|кт|konto|subkonto|analytics|context)/iu.test(contextPath);
return extractAccountsFromTextDetailed(String(value), {
forceAccountContext
}).resolved_account_anchors;
}
function normalizeTwoDigits(value) {
return String(value).padStart(2, "0");
@@ -360,16 +506,51 @@ function resolveJulyAnchor(rawText) {
applyGuard: true
};
}
function inferPrimaryWindowFromAnchor(anchor) {
const raw = String(anchor ?? "").trim();
if (/^\d{4}-\d{2}$/.test(raw)) {
return {
from: `${raw}-01`,
to: `${raw}-31`,
granularity: "month"
};
}
const normalized = normalizeEvidenceDate(raw);
if (!normalized) {
return null;
}
if (/^\d{4}-\d{2}-\d{2}$/.test(normalized)) {
return {
from: normalized,
to: normalized,
granularity: "day"
};
}
const month = normalized.slice(0, 7);
if (!/^\d{4}-\d{2}$/.test(month)) {
return null;
}
return {
from: `${month}-01`,
to: `${month}-31`,
granularity: "month"
};
}
function resolveTemporalGuard(input) {
const rawAnchorText = collectRawTemporalAnchorText(input.userMessage, input.companyAnchors);
const julyAnchor = resolveJulyAnchor(rawAnchorText);
const normalizedAnchor = normalizedAnchorFromFragments(input.normalized);
const reasonCodes = [];
if (!julyAnchor.applyGuard) {
const resolvedWindow = inferPrimaryWindowFromAnchor(normalizedAnchor.value);
return {
raw_time_anchor: julyAnchor.raw,
raw_time_scope: normalizedAnchor.value,
resolved_time_anchor: normalizedAnchor.value,
resolved_primary_period: resolvedWindow,
temporal_alignment_status: normalizedAnchor.value ? "aligned" : "conflicting",
temporal_resolution_source: normalizedAnchor.source,
temporal_guard_basis: normalizedAnchor.value ? "raw_time_scope_unlocked" : "none",
temporal_guard_applied: false,
temporal_guard_outcome: "passed",
primary_period_window: null,
@@ -377,24 +558,31 @@ function resolveTemporalGuard(input) {
controlled_temporal_expansion_enabled: false,
context_expansion_reasons_allowed: ["prehistory", "carryover", "post_period_closure", "long_running_contract_context"],
normalized_anchor_drift_detected: false,
reason_codes: []
reason_codes: normalizedAnchor.value ? [] : ["missing_resolved_primary_period"]
};
}
let outcome = "passed";
let normalizedAnchorDriftDetected = false;
let temporalAlignmentStatus = "aligned";
if (normalizedAnchor.value && julyAnchor.window && !isPeriodWithinWindow(normalizedAnchor.value, julyAnchor.window)) {
normalizedAnchorDriftDetected = true;
temporalAlignmentStatus = "corrected";
reasonCodes.push("normalized_anchor_out_of_primary_window_overridden");
}
else if (!normalizedAnchor.value && !julyAnchor.resolved) {
outcome = "ambiguous_limited";
temporalAlignmentStatus = "conflicting";
reasonCodes.push("missing_time_anchor_under_snapshot_lock");
}
const allowedContextWindow = buildAllowedContextWindow(julyAnchor.window);
return {
raw_time_anchor: julyAnchor.raw,
raw_time_scope: normalizedAnchor.value,
resolved_time_anchor: julyAnchor.resolved ?? normalizedAnchor.value,
resolved_primary_period: julyAnchor.window,
temporal_alignment_status: temporalAlignmentStatus,
temporal_resolution_source: julyAnchor.source,
temporal_guard_basis: julyAnchor.window ? "resolved_primary_period" : "none",
temporal_guard_applied: true,
temporal_guard_outcome: outcome,
primary_period_window: julyAnchor.window,
@@ -428,7 +616,8 @@ function applyTemporalHintToExecutionPlan(executionPlan, temporal) {
}
function resolveDomainPolarityGuard(input) {
const lower = String(input.userMessage ?? "").toLowerCase();
const accounts = uniqueStrings([...(input.companyAnchors?.accounts ?? []), ...extractAccountsFromText(lower)]);
const accountExtraction = extractAccountsFromTextDetailed(lower);
const accounts = uniqueStrings([...(input.companyAnchors?.accounts ?? []), ...accountExtraction.resolved_account_anchors]);
const prefixes = new Set(accounts.map((item) => accountPrefix(item)).filter((item) => Boolean(item)));
const settlementSignal = input.focusDomainHint === "settlements_60_62" ||
prefixes.has("60") ||
@@ -444,6 +633,10 @@ function resolveDomainPolarityGuard(input) {
supplier_score: 0,
customer_score: 0,
account_scope: accounts,
raw_numeric_tokens: accountExtraction.raw_numeric_tokens,
classified_numeric_tokens: accountExtraction.classified_numeric_tokens,
rejected_as_non_accounts: accountExtraction.rejected_as_non_accounts,
resolved_account_anchors: accounts,
rejected_problem_units: 0,
rejected_evidence: 0,
critical_contradiction: false,
@@ -471,6 +664,10 @@ function resolveDomainPolarityGuard(input) {
supplier_score: supplierScore,
customer_score: customerScore,
account_scope: accounts,
raw_numeric_tokens: accountExtraction.raw_numeric_tokens,
classified_numeric_tokens: accountExtraction.classified_numeric_tokens,
rejected_as_non_accounts: accountExtraction.rejected_as_non_accounts,
resolved_account_anchors: accounts,
rejected_problem_units: 0,
rejected_evidence: 0,
critical_contradiction: unresolved,
@@ -959,6 +1156,11 @@ function applyEvidenceAdmissibilityGate(input) {
}
function evaluateGroundedAnswerEligibility(input) {
const temporalPassed = input.temporal.temporal_guard_outcome === "passed";
const eligibilityTimeBasis = input.temporal.temporal_guard_basis;
const scopeValues = Array.isArray(input.businessScopeResolved) ? input.businessScopeResolved : [];
const hasCompanyScope = scopeValues.includes("company_specific_accounting");
const hasOnlyGenericScope = scopeValues.length > 0 && scopeValues.every((item) => String(item ?? "").trim() === "generic_accounting");
const businessScopePassed = scopeValues.length === 0 ? true : hasCompanyScope || !hasOnlyGenericScope;
const polarityPassed = !input.polarity.applied || input.polarity.outcome === "passed" || input.polarity.outcome === "not_applicable";
const claimAnchorResolutionRate = input.claimAnchors ? Number(input.claimAnchors.claim_anchor_resolution_rate ?? 0) : null;
const missingRequiredAnchors = input.claimAnchors ? Number(input.claimAnchors.missing_anchors?.length ?? 0) : 0;
@@ -972,6 +1174,7 @@ function evaluateGroundedAnswerEligibility(input) {
? true
: Number(input.targetedEvidenceHitRate) > 0;
const eligible = temporalPassed &&
businessScopePassed &&
polarityPassed &&
claimAnchorsPassed &&
admissibleEvidenceCount > 0 &&
@@ -984,6 +1187,9 @@ function evaluateGroundedAnswerEligibility(input) {
if (!polarityPassed) {
reasonCodes.push(`polarity_guard_${input.polarity.outcome}`);
}
if (!businessScopePassed) {
reasonCodes.push("business_scope_generic_unresolved");
}
if (!claimAnchorsPassed) {
reasonCodes.push("claim_anchor_coverage_insufficient");
}
@@ -999,6 +1205,8 @@ function evaluateGroundedAnswerEligibility(input) {
return {
eligible,
temporal_passed: temporalPassed,
eligibility_time_basis: eligibilityTimeBasis,
business_scope_passed: businessScopePassed,
polarity_passed: polarityPassed,
claim_anchors_passed: claimAnchorsPassed,
claim_anchor_resolution_rate: claimAnchorResolutionRate,
@@ -1014,7 +1222,10 @@ function applyEligibilityToGroundingCheck(groundingCheck, eligibility) {
if (eligibility.eligible) {
return groundingCheck;
}
const status = eligibility.admissible_evidence_count <= 0 || !eligibility.temporal_passed || !eligibility.claim_anchors_passed
const status = eligibility.admissible_evidence_count <= 0 ||
!eligibility.temporal_passed ||
!eligibility.claim_anchors_passed ||
!eligibility.business_scope_passed
? "no_grounded_answer"
: "partial";
const reasonMap = {
@@ -1022,6 +1233,7 @@ function applyEligibilityToGroundingCheck(groundingCheck, eligibility) {
critical_domain_or_account_contradiction: "Есть критическое противоречие по domain/account scope.",
temporal_guard_failed_out_of_snapshot_window: "Temporal anchor вышел за окно company snapshot (июль 2020).",
temporal_guard_ambiguous_limited: "Temporal anchor не разрешен надежно в пределах company snapshot.",
business_scope_generic_unresolved: "Business scope остался generic и не подтвержден как company-specific для доказательного ответа.",
polarity_guard_limited_unresolved_polarity: "Не удалось надежно определить supplier/customer polarity.",
polarity_guard_blocked_conflict: "Обнаружен конфликт supplier/customer polarity в retrieval-контуре.",
claim_anchor_coverage_insufficient: "Недостаточно покрытия required anchors для claim-bound grounding.",
+150 -12
View File
@@ -120,6 +120,83 @@ function extractExecutionState(normalized) {
};
});
}
function collectBusinessScopesFromNormalized(normalized) {
const scopes = [];
for (const item of extractFragments(normalized)) {
if (!item || typeof item !== "object") {
continue;
}
const scope = String(item.business_scope ?? "").trim();
if (scope) {
scopes.push(scope);
}
}
return Array.from(new Set(scopes));
}
function hasJuly2020SnapshotSignal(userMessage, companyAnchors) {
const lower = String(userMessage ?? "").toLowerCase();
if (/(?:\b2020[-/.]0?7\b|\bиюл[ьяе]?\b(?:\s+20\d{2})?|\bjuly\b(?:\s+20\d{2})?)/i.test(lower)) {
return true;
}
const periods = Array.isArray(companyAnchors?.periods) ? companyAnchors.periods : [];
const dates = Array.isArray(companyAnchors?.dates) ? companyAnchors.dates : [];
return [...periods, ...dates].some((item) => /2020[-/.]0?7|июл|july/i.test(String(item ?? "").toLowerCase()));
}
function hasP0DomainSignal(userMessage, companyAnchors) {
if (inferP0DomainFromMessage(userMessage)) {
return true;
}
const accounts = Array.isArray(companyAnchors?.accounts) ? companyAnchors.accounts : [];
if (accounts.some((item) => /^(?:01|02|08|19|20|21|23|25|26|28|29|44|51|60|62|68|76|97)(?:\.|$)/.test(String(item ?? "").trim()))) {
return true;
}
return /(?:ндс|vat|рбп|deferred|амортиз|supplier|customer|settlement|month\s*close|закрыти[ея]\s+месяц|поставщ|покупат)/i.test(String(userMessage ?? "").toLowerCase());
}
function resolveBusinessScopeAlignment(input) {
const rawScopes = collectBusinessScopesFromNormalized(input.normalized);
const needsCompanyGrounding = hasJuly2020SnapshotSignal(input.userMessage, input.companyAnchors) && hasP0DomainSignal(input.userMessage, input.companyAnchors);
const reasons = [];
if (needsCompanyGrounding) {
reasons.push("july_2020_snapshot_p0_signal");
}
if (!input.routeSummary || input.routeSummary.mode !== "deterministic_v2" || !needsCompanyGrounding) {
return {
business_scope_raw: rawScopes,
business_scope_resolved: rawScopes,
company_grounding_applied: false,
scope_resolution_reason: reasons,
route_summary_resolved: input.routeSummary
};
}
let changed = false;
const decisions = input.routeSummary.decisions.map((decision) => {
const scopeValue = String(decision.business_scope ?? "").trim();
if (scopeValue !== "generic_accounting" && scopeValue !== "unclear") {
return decision;
}
changed = true;
return {
...decision,
business_scope: "company_specific_accounting"
};
});
const resolvedSummary = changed
? {
...input.routeSummary,
decisions
}
: input.routeSummary;
const resolvedScopes = changed
? Array.from(new Set(decisions.map((decision) => String(decision.business_scope ?? "").trim()).filter(Boolean)))
: rawScopes;
return {
business_scope_raw: rawScopes,
business_scope_resolved: resolvedScopes,
company_grounding_applied: changed,
scope_resolution_reason: changed ? [...reasons, "generic_or_unclear_to_company_specific_override"] : reasons,
route_summary_resolved: resolvedSummary
};
}
function escapeRegex(value) {
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
@@ -179,8 +256,9 @@ function extractDiscardedIntentSegments(normalized) {
function collectDateSpans(text) {
const spans = [];
const datePatterns = [
/\b20\d{2}[-/.](?:0[1-9]|1[0-2])(?:[-/.](?:0[1-9]|[12]\d|3[01]))?\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:\d{2}|\d{4})\b/g
/\b20\d{2}(?:[-/.](?:0?[1-9]|1[0-2]))(?:[-/.](?:0?[1-9]|[12]\d|3[01]))?\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:\d{2}|\d{4})\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])\s+(?:январ[ьяе]|феврал[ьяе]|март[ае]?|апрел[ьяе]|ма[йея]|июн[ьяе]?|июл[ьяе]?|август[ае]?|сентябр[ьяе]?|октябр[ьяе]?|ноябр[ьяе]?|декабр[ьяе]?|january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+20\d{2})?\b/giu
];
for (const datePattern of datePatterns) {
let match = null;
@@ -193,6 +271,32 @@ function collectDateSpans(text) {
}
return spans;
}
function collectAmountSpans(text) {
const spans = [];
const amountPatterns = [/\b\d{1,3}(?:[ \u00A0]\d{3})+(?:[.,]\d{2})?\b/g, /\b\d+[.,]\d{2}\b/g];
for (const amountPattern of amountPatterns) {
let match = null;
while ((match = amountPattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
}
return spans;
}
function collectPercentSpans(text) {
const spans = [];
const percentPattern = /\b\d{1,3}(?:[.,]\d+)?\s*%/g;
let match = null;
while ((match = percentPattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
return spans;
}
function intersectsAnySpan(start, end, spans) {
return spans.some((span) => start < span.end && end > span.start);
}
@@ -268,7 +372,7 @@ function extractAccountTokens(text) {
if (explicitAccounts.size > 0) {
return Array.from(explicitAccounts);
}
const spans = collectDateSpans(lower);
const spans = [...collectDateSpans(lower), ...collectAmountSpans(lower), ...collectPercentSpans(lower)];
const hasAccountingLexeme = /(?:\bсчет(?:а|у|ом|ов)?\b|\bсч\.?\b|\baccount(?:s)?\b|\bschet(?:a|u|om|ov)?\b|оплат|расчет|аванс|долг|settlement|payment)/iu.test(lower);
if (!hasAccountingLexeme) {
return [];
@@ -884,7 +988,7 @@ function extractNormalizedPeriodLiteral(text) {
}
function extractFollowupAccountAnchorsLoose(text) {
const lower = String(text ?? "").toLowerCase();
const spans = collectDateSpans(lower);
const spans = [...collectDateSpans(lower), ...collectAmountSpans(lower), ...collectPercentSpans(lower)];
const anchors = [];
const followupAccountPattern = /\b(?:01|02|08|19|20|21|23|25|26|28|29|44|51|60|62|68|76|97)(?:\.\d{2})?\b/g;
let match = null;
@@ -1230,6 +1334,13 @@ class AssistantService {
};
const normalized = await this.normalizerService.normalize(normalizePayload);
const companyAnchors = (0, companyAnchorResolver_1.resolveCompanyAnchors)(userMessage);
const businessScopeResolution = resolveBusinessScopeAlignment({
userMessage,
companyAnchors,
normalized: normalized.normalized,
routeSummary: normalized.route_hint_summary
});
const resolvedRouteSummary = businessScopeResolution.route_summary_resolved;
const inferredDomainByMessage = inferP0DomainFromMessage(userMessage);
const focusDomainForGuards = inferredDomainByMessage === "settlements_60_62" ||
inferredDomainByMessage === "vat_document_register_book" ||
@@ -1252,8 +1363,8 @@ class AssistantService {
focusDomainHint: focusDomainForGuards,
primaryPeriod: temporalGuard.primary_period_window
});
const requirementExtraction = extractRequirements(normalized.route_hint_summary, normalized.normalized, userMessage);
let executionPlan = toExecutionPlan(normalized.route_hint_summary, normalized.normalized, userMessage, requirementExtraction.byFragment);
const requirementExtraction = extractRequirements(resolvedRouteSummary, normalized.normalized, userMessage);
let executionPlan = toExecutionPlan(resolvedRouteSummary, normalized.normalized, userMessage, requirementExtraction.byFragment);
executionPlan = (0, assistantRuntimeGuards_1.applyTemporalHintToExecutionPlan)(executionPlan, temporalGuard);
executionPlan = (0, assistantRuntimeGuards_1.applyPolarityHintToExecutionPlan)(executionPlan, domainPolarityGuardInitial);
const retrievalCalls = [];
@@ -1343,7 +1454,8 @@ class AssistantService {
polarity: polarityGuardResult.audit,
evidence: evidenceGateResult.audit,
claimAnchors: claimAnchorAudit,
targetedEvidenceHitRate: targetedEvidenceResult.audit.targeted_evidence_hit_rate
targetedEvidenceHitRate: targetedEvidenceResult.audit.targeted_evidence_hit_rate,
businessScopeResolved: businessScopeResolution.business_scope_resolved
});
const groundingCheck = (0, assistantRuntimeGuards_1.applyEligibilityToGroundingCheck)(groundingCheckBase, groundedAnswerEligibilityGuard);
const focusDomainHint = followupBinding.usage?.applied
@@ -1355,7 +1467,7 @@ class AssistantService {
const normalizationPeriodExplicit = hasExplicitPeriodAnchorFromNormalized(normalized.normalized) || hasPeriodInCompanyAnchors;
const composition = (0, answerComposer_1.composeAssistantAnswer)({
userMessage,
routeSummary: normalized.route_hint_summary,
routeSummary: resolvedRouteSummary,
retrievalResults,
requirements: coverageEvaluation.requirements,
coverageReport: coverageEvaluation.coverage,
@@ -1389,7 +1501,7 @@ class AssistantService {
timestamp: new Date().toISOString(),
questionId: userItem.message_id,
userMessage,
routeSummary: normalized.route_hint_summary,
routeSummary: resolvedRouteSummary,
requirements: coverageEvaluation.requirements,
coverageReport: coverageEvaluation.coverage,
retrievalResults,
@@ -1405,11 +1517,11 @@ class AssistantService {
prompt_version: normalized.prompt_version,
schema_version: normalized.schema_version,
fallback_type: composition.fallback_type,
route_summary: normalized.route_hint_summary,
route_summary: resolvedRouteSummary,
fragments: extractFragments(normalized.normalized),
requirements_extracted: coverageEvaluation.requirements,
coverage_report: coverageEvaluation.coverage,
routes: toDebugRoutes(normalized.route_hint_summary),
routes: toDebugRoutes(resolvedRouteSummary),
retrieval_status: retrievalResults.map((item) => ({
fragment_id: item.fragment_id,
requirement_ids: item.requirement_ids,
@@ -1422,16 +1534,29 @@ class AssistantService {
dropped_intent_segments: extractDiscardedIntentSegments(normalized.normalized),
question_type_class: questionTypeClass,
company_anchors: companyAnchors,
business_scope_raw: businessScopeResolution.business_scope_raw,
business_scope_resolved: businessScopeResolution.business_scope_resolved,
company_grounding_applied: businessScopeResolution.company_grounding_applied,
scope_resolution_reason: businessScopeResolution.scope_resolution_reason,
raw_time_anchor: temporalGuard.raw_time_anchor,
raw_time_scope: temporalGuard.raw_time_scope,
resolved_time_anchor: temporalGuard.resolved_time_anchor,
resolved_primary_period: temporalGuard.resolved_primary_period,
temporal_alignment_status: temporalGuard.temporal_alignment_status,
temporal_resolution_source: temporalGuard.temporal_resolution_source,
temporal_guard_basis: temporalGuard.temporal_guard_basis,
temporal_guard_applied: temporalGuard.temporal_guard_applied,
temporal_guard_outcome: temporalGuard.temporal_guard_outcome,
temporal_guard: temporalGuard,
raw_numeric_tokens: polarityGuardResult.audit.raw_numeric_tokens,
classified_numeric_tokens: polarityGuardResult.audit.classified_numeric_tokens,
rejected_as_non_accounts: polarityGuardResult.audit.rejected_as_non_accounts,
resolved_account_anchors: polarityGuardResult.audit.resolved_account_anchors,
domain_polarity_guard: polarityGuardResult.audit,
claim_anchor_audit: claimAnchorAudit,
targeted_evidence_acquisition: targetedEvidenceResult.audit,
evidence_admissibility_gate: evidenceGateResult.audit,
eligibility_time_basis: groundedAnswerEligibilityGuard.eligibility_time_basis,
grounded_answer_eligibility_guard: groundedAnswerEligibilityGuard,
...(followupBinding.usage ? { followup_state_usage: followupBinding.usage } : {}),
problem_centric_answer_applied: composition.problem_centric_answer_applied ?? false,
@@ -1476,7 +1601,7 @@ class AssistantService {
normalizer_output: normalized.normalized,
execution_plan: executionPlan,
resolved_execution_state: extractExecutionState(normalized.normalized),
routes: toDebugRoutes(normalized.route_hint_summary),
routes: toDebugRoutes(resolvedRouteSummary),
retrieval_calls: retrievalCalls,
retrieval_results_raw: retrievalResultsRaw,
retrieval_results_normalized: retrievalResults,
@@ -1498,16 +1623,29 @@ class AssistantService {
dropped_intent_segments: extractDiscardedIntentSegments(normalized.normalized),
question_type_class: questionTypeClass,
company_anchors: companyAnchors,
business_scope_raw: businessScopeResolution.business_scope_raw,
business_scope_resolved: businessScopeResolution.business_scope_resolved,
company_grounding_applied: businessScopeResolution.company_grounding_applied,
scope_resolution_reason: businessScopeResolution.scope_resolution_reason,
raw_time_anchor: temporalGuard.raw_time_anchor,
raw_time_scope: temporalGuard.raw_time_scope,
resolved_time_anchor: temporalGuard.resolved_time_anchor,
resolved_primary_period: temporalGuard.resolved_primary_period,
temporal_alignment_status: temporalGuard.temporal_alignment_status,
temporal_resolution_source: temporalGuard.temporal_resolution_source,
temporal_guard_basis: temporalGuard.temporal_guard_basis,
temporal_guard_applied: temporalGuard.temporal_guard_applied,
temporal_guard_outcome: temporalGuard.temporal_guard_outcome,
temporal_guard: temporalGuard,
raw_numeric_tokens: polarityGuardResult.audit.raw_numeric_tokens,
classified_numeric_tokens: polarityGuardResult.audit.classified_numeric_tokens,
rejected_as_non_accounts: polarityGuardResult.audit.rejected_as_non_accounts,
resolved_account_anchors: polarityGuardResult.audit.resolved_account_anchors,
domain_polarity_guard: polarityGuardResult.audit,
claim_anchor_audit: claimAnchorAudit,
targeted_evidence_acquisition: targetedEvidenceResult.audit,
evidence_admissibility_gate: evidenceGateResult.audit,
eligibility_time_basis: groundedAnswerEligibilityGuard.eligibility_time_basis,
grounded_answer_eligibility_guard: groundedAnswerEligibilityGuard,
...(followupBinding.usage ? { followup_state_usage: followupBinding.usage } : {}),
problem_centric_answer_applied: composition.problem_centric_answer_applied ?? false,
+139 -1
View File
@@ -11,8 +11,146 @@ function uniqueStrings(values) {
function capStrings(values, max) {
return uniqueStrings(values).slice(0, max);
}
const INVESTIGATION_ACCOUNT_PREFIXES = new Set([
"01",
"02",
"07",
"08",
"10",
"13",
"19",
"20",
"21",
"23",
"25",
"26",
"28",
"29",
"41",
"43",
"44",
"45",
"50",
"51",
"52",
"55",
"57",
"58",
"60",
"62",
"66",
"67",
"68",
"69",
"70",
"71",
"73",
"76",
"90",
"91",
"94",
"96",
"97"
]);
function collectDateLikeSpans(text) {
const spans = [];
const patterns = [
/\b20\d{2}(?:[-/.](?:0?[1-9]|1[0-2]))(?:[-/.](?:0?[1-9]|[12]\d|3[01]))?\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:\d{2}|\d{4})\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])\s+(?:январ[ьяе]|феврал[ьяе]|март[ае]?|апрел[ьяе]|ма[йея]|июн[ьяе]?|июл[ьяе]?|август[ае]?|сентябр[ьяе]?|октябр[ьяе]?|ноябр[ьяе]?|декабр[ьяе]?|january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+20\d{2})?\b/giu
];
for (const pattern of patterns) {
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
}
return spans;
}
function collectAmountLikeSpans(text) {
const spans = [];
const patterns = [/\b\d{1,3}(?:[ \u00A0]\d{3})+(?:[.,]\d{2})?\b/g, /\b\d+[.,]\d{2}\b/g];
for (const pattern of patterns) {
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
}
return spans;
}
function collectPercentLikeSpans(text) {
const spans = [];
const pattern = /\b\d{1,3}(?:[.,]\d+)?\s*%/g;
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
return spans;
}
function intersectsSpan(start, end, spans) {
return spans.some((span) => start < span.end && end > span.start);
}
function hasAccountContextAround(text, start, end) {
const left = text.slice(Math.max(0, start - 32), start);
const right = text.slice(end, Math.min(text.length, end + 32));
return /(?:счет|сч\.?|account|schet|оплат|расчет|расч[её]т|аванс|зачет|зач[её]т|ндс|закрыт|провод|постав|покуп|settlement|payment|vat|close|supplier|customer)/iu.test(`${left} ${right}`);
}
function detectAccounts(text) {
return capStrings(text.match(/\b\d{2}(?:\.\d{2})?\b/g) ?? [], stage1Contracts_1.INVESTIGATION_MAX_PRIMARY_ACCOUNTS);
const lower = String(text ?? "").toLowerCase();
const blockedSpans = [...collectDateLikeSpans(lower), ...collectAmountLikeSpans(lower), ...collectPercentLikeSpans(lower)];
const hasAccountingLexeme = /(?:\bсчет(?:а|у|ом|ов)?\b|\bсч\.?\b|\baccount(?:s)?\b|\bschet(?:a|u|om|ov)?\b|оплат|расчет|расч[её]т|аванс|долг|settlement|payment|supplier|customer|ндс|vat|рбп|deferred|амортиз)/iu.test(lower);
const accounts = new Set();
const contextualPattern = /(?:\b(?:счет(?:а|у|ом|ов)?|сч\.?|account(?:s)?|schet(?:a|u|om|ov)?)\b)\s*(?:№|#|:)?\s*(\d{2}(?:\.\d{1,2})?)/giu;
let contextualMatch = null;
while ((contextualMatch = contextualPattern.exec(lower)) !== null) {
const token = String(contextualMatch[1] ?? "").trim();
const prefix = token.match(/^(\d{2})/)?.[1] ?? null;
if (prefix && INVESTIGATION_ACCOUNT_PREFIXES.has(prefix)) {
accounts.add(token);
}
}
const pairPattern = /\b(\d{2}\.\d{1,2})\s*\/\s*(\d{2}\.\d{1,2})\b/g;
let pairMatch = null;
while ((pairMatch = pairPattern.exec(lower)) !== null) {
const left = String(pairMatch[1] ?? "").trim();
const right = String(pairMatch[2] ?? "").trim();
const leftPrefix = left.match(/^(\d{2})/)?.[1] ?? null;
const rightPrefix = right.match(/^(\d{2})/)?.[1] ?? null;
if (leftPrefix && INVESTIGATION_ACCOUNT_PREFIXES.has(leftPrefix)) {
accounts.add(left);
}
if (rightPrefix && INVESTIGATION_ACCOUNT_PREFIXES.has(rightPrefix)) {
accounts.add(right);
}
}
const genericPattern = /\b\d{2}(?:\.\d{1,2})?\b/g;
let genericMatch = null;
while ((genericMatch = genericPattern.exec(lower)) !== null) {
const token = String(genericMatch[0] ?? "").trim();
const start = genericMatch.index;
const end = start + token.length;
if (intersectsSpan(start, end, blockedSpans)) {
continue;
}
const prefix = token.match(/^(\d{2})/)?.[1] ?? null;
if (!prefix || !INVESTIGATION_ACCOUNT_PREFIXES.has(prefix)) {
continue;
}
if (!hasAccountingLexeme && !hasAccountContextAround(lower, start, end)) {
continue;
}
accounts.add(token);
}
return capStrings(Array.from(accounts), stage1Contracts_1.INVESTIGATION_MAX_PRIMARY_ACCOUNTS);
}
function detectPeriod(text) {
const monthly = text.match(/\b(20\d{2})[-/.](0[1-9]|1[0-2])\b/);
+147 -4
View File
@@ -60,9 +60,41 @@ function computeRetryMaxOutputTokens(current, rawModelResponse) {
}
function collectDateSpans(text) {
const spans = [];
const datePattern = /\b20\d{2}[-/.](?:0[1-9]|1[0-2])(?:[-/.](?:0[1-9]|[12]\d|3[01]))?\b/g;
const patterns = [
/\b20\d{2}(?:[-/.](?:0?[1-9]|1[0-2]))(?:[-/.](?:0?[1-9]|[12]\d|3[01]))?\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])[./-](?:0?[1-9]|1[0-2])[./-](?:\d{2}|\d{4})\b/g,
/\b(?:0?[1-9]|[12]\d|3[01])\s+(?:январ[ьяе]|феврал[ьяе]|март[ае]?|апрел[ьяе]|ма[йея]|июн[ьяе]?|июл[ьяе]?|август[ае]?|сентябр[ьяе]?|октябр[ьяе]?|ноябр[ьяе]?|декабр[ьяе]?|january|february|march|april|may|june|july|august|september|october|november|december)(?:\s+20\d{2})?\b/giu
];
for (const pattern of patterns) {
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
}
return spans;
}
function collectAmountSpans(text) {
const spans = [];
const patterns = [/\b\d{1,3}(?:[ \u00A0]\d{3})+(?:[.,]\d{2})?\b/g, /\b\d+[.,]\d{2}\b/g];
for (const pattern of patterns) {
let match = null;
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
});
}
}
return spans;
}
function collectPercentSpans(text) {
const spans = [];
const pattern = /\b\d{1,3}(?:[.,]\d+)?\s*%/g;
let match = null;
while ((match = datePattern.exec(text)) !== null) {
while ((match = pattern.exec(text)) !== null) {
spans.push({
start: match.index,
end: match.index + match[0].length
@@ -75,18 +107,67 @@ function intersectsAnySpan(start, end, spans) {
}
function extractAccounts(text) {
const lower = String(text ?? "").toLowerCase();
const knownPrefixes = new Set([
"01",
"02",
"07",
"08",
"10",
"13",
"19",
"20",
"21",
"23",
"25",
"26",
"28",
"29",
"41",
"43",
"44",
"45",
"50",
"51",
"52",
"55",
"57",
"58",
"60",
"62",
"66",
"67",
"68",
"69",
"70",
"71",
"73",
"76",
"90",
"91",
"94",
"96",
"97"
]);
const explicitAccounts = new Set();
const contextualPattern = /(?:\bсч(?:е|ё)т(?:а|у|ом|ов)?\b|\bсч\.?\b|\baccount(?:s)?\b|\bschet(?:a|u|om|ov)?\b)\s*(?:№|#|:)?\s*(\d{2}(?:\.\d{2})?)/giu;
let contextual = null;
while ((contextual = contextualPattern.exec(lower)) !== null) {
if (contextual[1]) {
explicitAccounts.add(contextual[1]);
const token = String(contextual[1]).trim();
const prefix = token.match(/^(\d{2})/)?.[1] ?? null;
if (prefix && knownPrefixes.has(prefix)) {
explicitAccounts.add(token);
}
}
}
if (explicitAccounts.size > 0) {
return Array.from(explicitAccounts);
}
const spans = collectDateSpans(lower);
const spans = [...collectDateSpans(lower), ...collectAmountSpans(lower), ...collectPercentSpans(lower)];
const hasAccountingLexeme = /(?:\bсчет(?:а|у|ом|ов)?\b|\bсч\.?\b|\baccount(?:s)?\b|\bschet(?:a|u|om|ov)?\b|оплат|расчет|расч[её]т|аванс|долг|settlement|payment|supplier|customer|ндс|vat|амортиз|рбп|deferred)/iu.test(lower);
if (!hasAccountingLexeme) {
return [];
}
const extracted = [];
const genericPattern = /\b\d{2}(?:\.\d{2})?\b/g;
let generic = null;
@@ -97,6 +178,10 @@ function extractAccounts(text) {
if (intersectsAnySpan(start, end, spans)) {
continue;
}
const prefix = value.match(/^(\d{2})/)?.[1] ?? null;
if (!prefix || !knownPrefixes.has(prefix)) {
continue;
}
extracted.push(value);
}
return Array.from(new Set(extracted));
@@ -424,6 +509,58 @@ function routeCanBeSelected(fragment) {
}
return hasBusinessNodeSignals(fragment);
}
function hasJuly2020SnapshotSignal(userMessage, sessionContext) {
const text = String(userMessage ?? "").toLowerCase();
const contextPeriod = String(sessionContext?.period_hint ?? "").toLowerCase();
const businessContext = String(sessionContext?.business_context ?? "").toLowerCase();
if (/(?:\b2020[-/.]0?7\b|\bиюл[ьяе]?\b(?:\s+20\d{2})?|\bjuly\b(?:\s+20\d{2})?)/i.test(text)) {
return true;
}
return /2020[-/.]0?7|июл|july/.test(`${contextPeriod} ${businessContext}`);
}
function hasP0SignalForCompanyScope(userMessage) {
const lower = String(userMessage ?? "").toLowerCase();
return /(?:\b(?:01|02|08|19|20|21|23|25|26|28|29|44|51|60|62|68|76|97)(?:\.\d{1,2})?\b|ндс|vat|supplier|customer|settlement|month\s*close|рбп|deferred|закрыти[ея]\s+месяц|амортиз|поставщ|покупат)/i.test(lower);
}
function applyCompanyScopeResolutionV2(candidate, userMessage, sessionContext) {
if (!candidate || typeof candidate !== "object") {
return candidate;
}
const source = candidate;
if (!Array.isArray(source.fragments)) {
return candidate;
}
const forceCompanyScope = hasJuly2020SnapshotSignal(userMessage, sessionContext) && hasP0SignalForCompanyScope(userMessage);
if (!forceCompanyScope) {
return candidate;
}
let changed = false;
const fragments = source.fragments.map((fragment) => {
if (!fragment || typeof fragment !== "object") {
return fragment;
}
const value = fragment;
if (value.domain_relevance !== "in_scope") {
return fragment;
}
const scopeValue = String(value.business_scope ?? "").trim();
if (scopeValue !== "generic_accounting" && scopeValue !== "unclear") {
return fragment;
}
changed = true;
return {
...value,
business_scope: "company_specific_accounting"
};
});
if (!changed) {
return candidate;
}
return {
...source,
fragments
};
}
function dedupeSoftAssumptions(input) {
return Array.from(new Set(input));
}
@@ -787,6 +924,9 @@ class NormalizerService {
let validation = { passed: false, errors: ["NO_VALIDATION"] };
try {
normalizedCandidate = safeJsonParse(outputText);
if (schemaVersion !== "v1") {
normalizedCandidate = applyCompanyScopeResolutionV2(normalizedCandidate, payload.userQuestion, payload.context);
}
if (schemaVersion === "v2_0_2") {
normalizedCandidate = applyExecutionStatePolicyV202(normalizedCandidate, payload.userQuestion, payload.context);
}
@@ -831,6 +971,9 @@ class NormalizerService {
usage = retry.usage;
try {
normalizedCandidate = safeJsonParse(outputText);
if (schemaVersion !== "v1") {
normalizedCandidate = applyCompanyScopeResolutionV2(normalizedCandidate, payload.userQuestion, payload.context);
}
if (schemaVersion === "v2_0_2") {
normalizedCandidate = applyExecutionStatePolicyV202(normalizedCandidate, payload.userQuestion, payload.context);
}