ARCH: удержать year-switch после contracts pivot
This commit is contained in:
+65
-4
@@ -1839,6 +1839,49 @@ function applyFutureDatedRowsGuard(rows, intent, referenceDate) {
|
||||
droppedCount
|
||||
};
|
||||
}
|
||||
function applyExplicitPeriodWindowFilter(rows, filters) {
|
||||
const periodFrom = typeof filters.period_from === "string" ? filters.period_from.trim() : "";
|
||||
const periodTo = typeof filters.period_to === "string" ? filters.period_to.trim() : "";
|
||||
if (!periodFrom && !periodTo) {
|
||||
return {
|
||||
rows,
|
||||
droppedCount: 0,
|
||||
applied: false
|
||||
};
|
||||
}
|
||||
const periodFromTs = periodFrom ? parseIsoDateUtcTimestamp(periodFrom) : null;
|
||||
const periodToTs = periodTo ? parseIsoDateUtcTimestamp(periodTo) : null;
|
||||
if ((periodFrom && periodFromTs === null) || (periodTo && periodToTs === null)) {
|
||||
return {
|
||||
rows,
|
||||
droppedCount: 0,
|
||||
applied: false
|
||||
};
|
||||
}
|
||||
const keptRows = [];
|
||||
let droppedCount = 0;
|
||||
for (const row of rows) {
|
||||
const rowTs = parseIsoDateUtcTimestamp(row.period);
|
||||
if (rowTs === null) {
|
||||
droppedCount += 1;
|
||||
continue;
|
||||
}
|
||||
if (periodFromTs !== null && rowTs < periodFromTs) {
|
||||
droppedCount += 1;
|
||||
continue;
|
||||
}
|
||||
if (periodToTs !== null && rowTs > periodToTs) {
|
||||
droppedCount += 1;
|
||||
continue;
|
||||
}
|
||||
keptRows.push(row);
|
||||
}
|
||||
return {
|
||||
rows: keptRows,
|
||||
droppedCount,
|
||||
applied: true
|
||||
};
|
||||
}
|
||||
function hasExplicitPeriodWindow(filters) {
|
||||
return ((typeof filters.period_from === "string" && filters.period_from.trim().length > 0) ||
|
||||
(typeof filters.period_to === "string" && filters.period_to.trim().length > 0));
|
||||
@@ -1855,6 +1898,7 @@ function canAutoBroadenPeriodWindow(intent, filters) {
|
||||
}
|
||||
return (intent === "list_documents_by_counterparty" ||
|
||||
intent === "bank_operations_by_counterparty" ||
|
||||
intent === "list_contracts_by_counterparty" ||
|
||||
intent === "list_documents_by_contract" ||
|
||||
intent === "bank_operations_by_contract" ||
|
||||
intent === "inventory_purchase_provenance_for_item" ||
|
||||
@@ -3327,7 +3371,8 @@ class AddressQueryService {
|
||||
});
|
||||
let anchorFilter = applyAddressFilters(normalizedRows, filtersForMatching);
|
||||
let filterByAnchors = anchorFilter.rows;
|
||||
let filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, filterByAnchors);
|
||||
let explicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(filterByAnchors, executionFilters);
|
||||
let filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, explicitPeriodWindowFilter.rows);
|
||||
let filteredRowsFutureGuard = applyFutureDatedRowsGuard(filteredRowsBeforeFutureGuard, intent.intent, futureGuardReferenceDate);
|
||||
let filteredRows = filteredRowsFutureGuard.rows;
|
||||
let organizationWarehouseRecoveryApplied = false;
|
||||
@@ -3369,7 +3414,8 @@ class AddressQueryService {
|
||||
}
|
||||
anchorFilter = applyAddressFilters(normalizedRows, filtersForMatching);
|
||||
filterByAnchors = anchorFilter.rows;
|
||||
filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, filterByAnchors);
|
||||
explicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(filterByAnchors, executionFilters);
|
||||
filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, explicitPeriodWindowFilter.rows);
|
||||
filteredRowsFutureGuard = applyFutureDatedRowsGuard(filteredRowsBeforeFutureGuard, intent.intent, futureGuardReferenceDate);
|
||||
filteredRows = filteredRowsFutureGuard.rows;
|
||||
organizationWarehouseRecoveryApplied = filteredRows.length > 0;
|
||||
@@ -3382,6 +3428,19 @@ class AddressQueryService {
|
||||
baseReasons.push("future_rows_excluded_from_response");
|
||||
}
|
||||
}
|
||||
if (explicitPeriodWindowFilter.applied) {
|
||||
if (!baseReasons.includes("explicit_period_window_post_filter_applied")) {
|
||||
baseReasons.push("explicit_period_window_post_filter_applied");
|
||||
}
|
||||
if (explicitPeriodWindowFilter.droppedCount > 0) {
|
||||
if (!filters.warnings.includes("rows_outside_explicit_period_window_excluded")) {
|
||||
filters.warnings.push("rows_outside_explicit_period_window_excluded");
|
||||
}
|
||||
if (!baseReasons.includes("rows_outside_explicit_period_window_excluded")) {
|
||||
baseReasons.push("rows_outside_explicit_period_window_excluded");
|
||||
}
|
||||
}
|
||||
}
|
||||
const rowDiagnostics = deriveRowStageDiagnostics(mcp.raw_rows, normalizedRows.length, normalizedRows.length);
|
||||
const stageStatus = deriveMcpStageStatus({
|
||||
rawRowsReceived: mcp.raw_rows.length,
|
||||
@@ -3758,7 +3817,8 @@ class AddressQueryService {
|
||||
});
|
||||
const expandedAnchorFilter = applyAddressFilters(expandedNormalizedRows, expandedFiltersForMatching);
|
||||
const expandedRowsByAnchor = expandedAnchorFilter.rows;
|
||||
const expandedFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, expandedRowsByAnchor);
|
||||
const expandedExplicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(expandedRowsByAnchor, expandedLimitFilters);
|
||||
const expandedFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, expandedExplicitPeriodWindowFilter.rows);
|
||||
const expandedFutureGuard = applyFutureDatedRowsGuard(expandedFilteredRowsBeforeFutureGuard, intent.intent, resolveFutureGuardReferenceDate(analysisDate, expandedLimitFilters));
|
||||
const expandedFilteredRows = expandedFutureGuard.rows;
|
||||
if (expandedFutureGuard.droppedCount > 0) {
|
||||
@@ -3981,7 +4041,8 @@ class AddressQueryService {
|
||||
});
|
||||
const historicalAnchorFilter = applyAddressFilters(historicalNormalizedRows, historicalFiltersForMatching);
|
||||
const historicalRowsByAnchor = historicalAnchorFilter.rows;
|
||||
const historicalFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, historicalRowsByAnchor);
|
||||
const historicalExplicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(historicalRowsByAnchor, historicalFilters);
|
||||
const historicalFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, historicalExplicitPeriodWindowFilter.rows);
|
||||
const historicalFutureGuard = applyFutureDatedRowsGuard(historicalFilteredRowsBeforeFutureGuard, intent.intent, resolveFutureGuardReferenceDate(analysisDate, historicalFilters));
|
||||
const historicalFilteredRows = historicalFutureGuard.rows;
|
||||
if (historicalFutureGuard.droppedCount > 0) {
|
||||
|
||||
@@ -871,7 +871,7 @@ const BASE_RECIPES = [
|
||||
intent: "list_contracts_by_counterparty",
|
||||
purpose: "List contracts by counterparty from contract catalog",
|
||||
required_filters: ["counterparty"],
|
||||
optional_filters: ["limit", "sort"],
|
||||
optional_filters: ["period_from", "period_to", "as_of_date", "organization", "limit", "sort"],
|
||||
default_limit: 300,
|
||||
account_scope_mode: "preferred",
|
||||
query_template: "contracts_by_counterparty_profile"
|
||||
|
||||
@@ -2274,6 +2274,56 @@ function applyFutureDatedRowsGuard(
|
||||
};
|
||||
}
|
||||
|
||||
function applyExplicitPeriodWindowFilter(
|
||||
rows: NormalizedAddressRow[],
|
||||
filters: AddressFilterSet
|
||||
): { rows: NormalizedAddressRow[]; droppedCount: number; applied: boolean } {
|
||||
const periodFrom = typeof filters.period_from === "string" ? filters.period_from.trim() : "";
|
||||
const periodTo = typeof filters.period_to === "string" ? filters.period_to.trim() : "";
|
||||
if (!periodFrom && !periodTo) {
|
||||
return {
|
||||
rows,
|
||||
droppedCount: 0,
|
||||
applied: false
|
||||
};
|
||||
}
|
||||
|
||||
const periodFromTs = periodFrom ? parseIsoDateUtcTimestamp(periodFrom) : null;
|
||||
const periodToTs = periodTo ? parseIsoDateUtcTimestamp(periodTo) : null;
|
||||
if ((periodFrom && periodFromTs === null) || (periodTo && periodToTs === null)) {
|
||||
return {
|
||||
rows,
|
||||
droppedCount: 0,
|
||||
applied: false
|
||||
};
|
||||
}
|
||||
|
||||
const keptRows: NormalizedAddressRow[] = [];
|
||||
let droppedCount = 0;
|
||||
for (const row of rows) {
|
||||
const rowTs = parseIsoDateUtcTimestamp(row.period);
|
||||
if (rowTs === null) {
|
||||
droppedCount += 1;
|
||||
continue;
|
||||
}
|
||||
if (periodFromTs !== null && rowTs < periodFromTs) {
|
||||
droppedCount += 1;
|
||||
continue;
|
||||
}
|
||||
if (periodToTs !== null && rowTs > periodToTs) {
|
||||
droppedCount += 1;
|
||||
continue;
|
||||
}
|
||||
keptRows.push(row);
|
||||
}
|
||||
|
||||
return {
|
||||
rows: keptRows,
|
||||
droppedCount,
|
||||
applied: true
|
||||
};
|
||||
}
|
||||
|
||||
function hasExplicitPeriodWindow(filters: AddressFilterSet): boolean {
|
||||
return (
|
||||
(typeof filters.period_from === "string" && filters.period_from.trim().length > 0) ||
|
||||
@@ -2295,6 +2345,7 @@ function canAutoBroadenPeriodWindow(intent: AddressIntent, filters: AddressFilte
|
||||
return (
|
||||
intent === "list_documents_by_counterparty" ||
|
||||
intent === "bank_operations_by_counterparty" ||
|
||||
intent === "list_contracts_by_counterparty" ||
|
||||
intent === "list_documents_by_contract" ||
|
||||
intent === "bank_operations_by_contract" ||
|
||||
intent === "inventory_purchase_provenance_for_item" ||
|
||||
@@ -4082,7 +4133,8 @@ export class AddressQueryService {
|
||||
});
|
||||
let anchorFilter = applyAddressFilters(normalizedRows, filtersForMatching);
|
||||
let filterByAnchors = anchorFilter.rows;
|
||||
let filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, filterByAnchors);
|
||||
let explicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(filterByAnchors, executionFilters);
|
||||
let filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, explicitPeriodWindowFilter.rows);
|
||||
let filteredRowsFutureGuard = applyFutureDatedRowsGuard(
|
||||
filteredRowsBeforeFutureGuard,
|
||||
intent.intent,
|
||||
@@ -4130,7 +4182,8 @@ export class AddressQueryService {
|
||||
}
|
||||
anchorFilter = applyAddressFilters(normalizedRows, filtersForMatching);
|
||||
filterByAnchors = anchorFilter.rows;
|
||||
filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, filterByAnchors);
|
||||
explicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(filterByAnchors, executionFilters);
|
||||
filteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, explicitPeriodWindowFilter.rows);
|
||||
filteredRowsFutureGuard = applyFutureDatedRowsGuard(
|
||||
filteredRowsBeforeFutureGuard,
|
||||
intent.intent,
|
||||
@@ -4147,6 +4200,19 @@ export class AddressQueryService {
|
||||
baseReasons.push("future_rows_excluded_from_response");
|
||||
}
|
||||
}
|
||||
if (explicitPeriodWindowFilter.applied) {
|
||||
if (!baseReasons.includes("explicit_period_window_post_filter_applied")) {
|
||||
baseReasons.push("explicit_period_window_post_filter_applied");
|
||||
}
|
||||
if (explicitPeriodWindowFilter.droppedCount > 0) {
|
||||
if (!filters.warnings.includes("rows_outside_explicit_period_window_excluded")) {
|
||||
filters.warnings.push("rows_outside_explicit_period_window_excluded");
|
||||
}
|
||||
if (!baseReasons.includes("rows_outside_explicit_period_window_excluded")) {
|
||||
baseReasons.push("rows_outside_explicit_period_window_excluded");
|
||||
}
|
||||
}
|
||||
}
|
||||
const rowDiagnostics = deriveRowStageDiagnostics(mcp.raw_rows, normalizedRows.length, normalizedRows.length);
|
||||
const stageStatus = deriveMcpStageStatus({
|
||||
rawRowsReceived: mcp.raw_rows.length,
|
||||
@@ -4599,7 +4665,14 @@ export class AddressQueryService {
|
||||
});
|
||||
const expandedAnchorFilter = applyAddressFilters(expandedNormalizedRows, expandedFiltersForMatching);
|
||||
const expandedRowsByAnchor = expandedAnchorFilter.rows;
|
||||
const expandedFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, expandedRowsByAnchor);
|
||||
const expandedExplicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(
|
||||
expandedRowsByAnchor,
|
||||
expandedLimitFilters
|
||||
);
|
||||
const expandedFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(
|
||||
intent.intent,
|
||||
expandedExplicitPeriodWindowFilter.rows
|
||||
);
|
||||
const expandedFutureGuard = applyFutureDatedRowsGuard(
|
||||
expandedFilteredRowsBeforeFutureGuard,
|
||||
intent.intent,
|
||||
@@ -4859,7 +4932,14 @@ export class AddressQueryService {
|
||||
});
|
||||
const historicalAnchorFilter = applyAddressFilters(historicalNormalizedRows, historicalFiltersForMatching);
|
||||
const historicalRowsByAnchor = historicalAnchorFilter.rows;
|
||||
const historicalFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(intent.intent, historicalRowsByAnchor);
|
||||
const historicalExplicitPeriodWindowFilter = applyExplicitPeriodWindowFilter(
|
||||
historicalRowsByAnchor,
|
||||
historicalFilters
|
||||
);
|
||||
const historicalFilteredRowsBeforeFutureGuard = applyIntentSpecificFilter(
|
||||
intent.intent,
|
||||
historicalExplicitPeriodWindowFilter.rows
|
||||
);
|
||||
const historicalFutureGuard = applyFutureDatedRowsGuard(
|
||||
historicalFilteredRowsBeforeFutureGuard,
|
||||
intent.intent,
|
||||
|
||||
@@ -895,7 +895,7 @@ const BASE_RECIPES: AddressRecipeDefinition[] = [
|
||||
intent: "list_contracts_by_counterparty",
|
||||
purpose: "List contracts by counterparty from contract catalog",
|
||||
required_filters: ["counterparty"],
|
||||
optional_filters: ["limit", "sort"],
|
||||
optional_filters: ["period_from", "period_to", "as_of_date", "organization", "limit", "sort"],
|
||||
default_limit: 300,
|
||||
account_scope_mode: "preferred",
|
||||
query_template: "contracts_by_counterparty_profile"
|
||||
|
||||
@@ -4080,6 +4080,28 @@ describe("address query limited taxonomy and stage diagnostics", { timeout: 1500
|
||||
});
|
||||
});
|
||||
|
||||
it("auto-broadens out-of-window period after contracts pivot and keeps requested year in the reply", async () => {
|
||||
const service = new AddressQueryService();
|
||||
const seed = await service.tryHandle("покажи договора все по жуковке 51");
|
||||
expect(seed?.handled).toBe(true);
|
||||
expect(seed?.debug.detected_intent).toBe("list_contracts_by_counterparty");
|
||||
|
||||
const result = await service.tryHandle("Р° Р·Р° 2021?", {
|
||||
followupContext: {
|
||||
previous_intent: (seed?.debug.detected_intent as any) ?? "list_contracts_by_counterparty",
|
||||
previous_filters: seed?.debug.extracted_filters,
|
||||
previous_anchor_type: (seed?.debug.anchor_type as any) ?? "counterparty",
|
||||
previous_anchor_value: seed?.debug.anchor_value_resolved ?? seed?.debug.anchor_value_raw ?? null
|
||||
}
|
||||
});
|
||||
|
||||
expect(result?.handled).toBe(true);
|
||||
expect(result?.debug.detected_intent).toBe("list_contracts_by_counterparty");
|
||||
expect(result?.debug.selected_recipe).toBe("address_contracts_by_counterparty_v1");
|
||||
expect(result?.debug.limitations).toContain("period_window_auto_broadened_to_available_data");
|
||||
expect(String(result?.reply_text ?? "")).toContain("2021");
|
||||
});
|
||||
|
||||
describe("address decompose stage follow-up carryover", () => {
|
||||
it("promotes selected-object supplier slang follow-up into inventory provenance with inherited date context", () => {
|
||||
const result = runAddressDecomposeStage('По выбранному объекту "Столешница 600*3050*26 дуб ниагара": кто это поставил нам', {
|
||||
|
||||
Reference in New Issue
Block a user