АРЧ АП11 - Архитектура после регресса: Архитектура: централизовать anchor и temporal carryover в continuity policy и протянуть их в transition hot path

This commit is contained in:
2026-04-18 21:08:01 +03:00
parent d29fbba214
commit cf17404925
10 changed files with 348 additions and 204 deletions
@@ -33,6 +33,17 @@ export interface AssistantAddressDebugContextFacts {
scopedDate: string | null;
}
export interface AssistantAddressDebugTemporalScope {
asOfDate: string | null;
periodFrom: string | null;
periodTo: string | null;
}
export interface AssistantAddressDebugAnchorContext {
anchorType: string | null;
anchorValue: string | null;
}
export interface AssistantOrganizationAuthorityInput {
sessionItems?: unknown[];
sessionKnownOrganizations?: unknown[];
@@ -124,6 +135,68 @@ export function readAddressDebugScopedDate(debug: Record<string, unknown> | null
);
}
export function readAddressDebugTemporalScope(
debug: Record<string, unknown> | null,
toNonEmptyString: (value: unknown) => string | null = fallbackToNonEmptyString
): AssistantAddressDebugTemporalScope {
const extractedFilters = readAddressDebugFilters(debug);
const rootFrameContext = toRecordObject(debug?.address_root_frame_context);
return {
asOfDate: toNonEmptyString(extractedFilters?.as_of_date) ?? toNonEmptyString(rootFrameContext?.as_of_date),
periodFrom: toNonEmptyString(extractedFilters?.period_from) ?? toNonEmptyString(rootFrameContext?.period_from),
periodTo: toNonEmptyString(extractedFilters?.period_to) ?? toNonEmptyString(rootFrameContext?.period_to)
};
}
export function resolveAddressDebugAnchorContext(
debug: Record<string, unknown> | null,
toNonEmptyString: (value: unknown) => string | null = fallbackToNonEmptyString
): AssistantAddressDebugAnchorContext {
const explicitAnchorType = toNonEmptyString(debug?.anchor_type);
const explicitAnchorValue =
toNonEmptyString(debug?.anchor_value_resolved) ?? toNonEmptyString(debug?.anchor_value_raw);
if (explicitAnchorType || explicitAnchorValue) {
return {
anchorType: explicitAnchorType,
anchorValue: explicitAnchorValue
};
}
const extractedFilters = readAddressDebugFilters(debug);
const item = toNonEmptyString(extractedFilters?.item);
if (item) {
return {
anchorType: "item",
anchorValue: item
};
}
const counterparty = toNonEmptyString(extractedFilters?.counterparty);
if (counterparty) {
return {
anchorType: "counterparty",
anchorValue: counterparty
};
}
const account = toNonEmptyString(extractedFilters?.account);
if (account) {
return {
anchorType: "account",
anchorValue: account
};
}
const contract = toNonEmptyString(extractedFilters?.contract);
if (contract) {
return {
anchorType: "contract",
anchorValue: contract
};
}
return {
anchorType: null,
anchorValue: null
};
}
export function resolveAddressDebugContextFacts(
debug: Record<string, unknown> | null,
toNonEmptyString: (value: unknown) => string | null = fallbackToNonEmptyString
@@ -3,7 +3,7 @@ import {
buildInventoryHistoryCapabilityFollowupReply as buildInventoryHistoryCapabilityFollowupReplyFromPolicy,
resolveAssistantLivingChatMemoryContext
} from "./assistantMemoryRecapPolicy";
import { resolveAssistantOrganizationAuthority } from "./assistantContinuityPolicy";
import { formatIsoDateForReply, resolveAssistantOrganizationAuthority } from "./assistantContinuityPolicy";
export interface AssistantLivingChatSessionScopeInput {
knownOrganizations?: unknown[];
@@ -66,77 +66,6 @@ export interface AssistantLivingChatRuntimeOutput {
debug: Record<string, unknown> | null;
}
function formatIsoDateForReply(value: unknown): string | null {
const source = String(value ?? "").trim();
const match = source.match(/^(\d{4})-(\d{2})-(\d{2})$/);
if (!match) {
return null;
}
return `${match[3]}.${match[2]}.${match[1]}`;
}
function findLastGroundedInventoryAddressDebug(items: unknown[]): Record<string, unknown> | null {
if (!Array.isArray(items)) {
return null;
}
for (let index = items.length - 1; index >= 0; index -= 1) {
const item = items[index] as { role?: string; debug?: Record<string, unknown> } | null;
if (!item || item.role !== "assistant" || !item.debug || typeof item.debug !== "object") {
continue;
}
const debug = item.debug;
const answerGroundingCheck =
debug.answer_grounding_check && typeof debug.answer_grounding_check === "object"
? (debug.answer_grounding_check as Record<string, unknown>)
: null;
const groundingStatus = String(answerGroundingCheck?.status ?? "");
const detectedIntent = String(debug.detected_intent ?? "");
const capabilityId = String(debug.capability_id ?? "");
const rootFrameContext =
debug.address_root_frame_context && typeof debug.address_root_frame_context === "object"
? (debug.address_root_frame_context as Record<string, unknown>)
: null;
const rootIntent = String(rootFrameContext?.root_intent ?? "");
const isInventoryContext =
detectedIntent === "inventory_on_hand_as_of_date" ||
capabilityId === "confirmed_inventory_on_hand_as_of_date" ||
rootIntent === "inventory_on_hand_as_of_date";
if (groundingStatus === "grounded" && isInventoryContext) {
return debug;
}
}
return null;
}
function findLastAddressDebugWithItem(items: unknown[]): Record<string, unknown> | null {
if (!Array.isArray(items)) {
return null;
}
for (let index = items.length - 1; index >= 0; index -= 1) {
const item = items[index] as { role?: string; debug?: Record<string, unknown> } | null;
if (!item || item.role !== "assistant" || !item.debug || typeof item.debug !== "object") {
continue;
}
const debug = item.debug;
if (String(debug.execution_lane ?? "") !== "address_query") {
continue;
}
const extractedFilters =
debug.extracted_filters && typeof debug.extracted_filters === "object"
? (debug.extracted_filters as Record<string, unknown>)
: null;
const itemLabel =
String(extractedFilters?.item ?? "").trim() ||
(String(debug.anchor_type ?? "") === "item"
? String(debug.anchor_value_resolved ?? debug.anchor_value_raw ?? "").trim()
: "");
if (itemLabel) {
return debug;
}
}
return null;
}
function hasPriorAssistantTurn(items: unknown[]): boolean {
if (!Array.isArray(items)) {
return false;
@@ -148,22 +77,6 @@ function buildDeterministicSmalltalkLeadReply(): string {
return "\u041f\u0440\u0438\u0432\u0435\u0442! \u0412\u0441\u0451 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e.";
}
function findLastAddressDebug(items: unknown[]): Record<string, unknown> | null {
if (!Array.isArray(items)) {
return null;
}
for (let index = items.length - 1; index >= 0; index -= 1) {
const item = items[index] as { role?: string; debug?: Record<string, unknown> } | null;
if (!item || item.role !== "assistant" || !item.debug || typeof item.debug !== "object") {
continue;
}
if (String(item.debug.execution_lane ?? "") === "address_query") {
return item.debug;
}
}
return null;
}
function buildInventoryHistoryCapabilityFollowupReply(input: {
organization: string | null;
addressDebug: Record<string, unknown> | null;
@@ -3,6 +3,8 @@ import {
buildInventoryRootFrameFromAddressDebug,
readAddressDebugFilters,
readAddressDebugItem,
readAddressDebugTemporalScope,
resolveAddressDebugAnchorContext,
resolveAssistantOrganizationAuthority
} from "./assistantContinuityPolicy";
@@ -422,6 +424,10 @@ export function createAssistantTransitionPolicy(deps) {
mergeKnownOrganizations: deps.mergeKnownOrganizations
});
const continuitySnapshot = organizationAuthority.continuitySnapshot;
const continuityTemporalScope = readAddressDebugTemporalScope(
continuitySnapshot.lastGroundedAddressDebug,
deps.toNonEmptyString
);
const organizationClarificationCandidates = Array.isArray(organizationAuthority.organizationClarificationCandidates)
? organizationAuthority.organizationClarificationCandidates
: [];
@@ -652,14 +658,9 @@ export function createAssistantTransitionPolicy(deps) {
followupSelectionMode = "switch_to_suggested_intent";
}
}
let previousAnchorType = deps.toNonEmptyString(previousAddressDebug.anchor_type);
let previousAnchor =
deps.toNonEmptyString(previousAddressDebug.anchor_value_resolved) ??
deps.toNonEmptyString(previousAddressDebug.anchor_value_raw) ??
deps.readAddressFilterString(previousAddressDebug, "item") ??
deps.readAddressFilterString(previousAddressDebug, "counterparty") ??
deps.readAddressFilterString(previousAddressDebug, "account") ??
deps.readAddressFilterString(previousAddressDebug, "contract");
const previousAnchorContext = resolveAddressDebugAnchorContext(previousAddressDebug, deps.toNonEmptyString);
let previousAnchorType = previousAnchorContext.anchorType;
let previousAnchor = previousAnchorContext.anchorValue;
const navigationSessionContext =
addressNavigationState && typeof addressNavigationState === "object"
? addressNavigationState.session_context && typeof addressNavigationState.session_context === "object"
@@ -851,11 +852,9 @@ export function createAssistantTransitionPolicy(deps) {
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.as_of_date) &&
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.as_of_date
continuityTemporalScope.asOfDate
) {
previousFilters.as_of_date = deps.toNonEmptyString(
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.as_of_date
);
previousFilters.as_of_date = continuityTemporalScope.asOfDate;
}
if (
shouldBackfillPreviousDateScopeFromNavigation &&
@@ -867,11 +866,9 @@ export function createAssistantTransitionPolicy(deps) {
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_from) &&
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_from
continuityTemporalScope.periodFrom
) {
previousFilters.period_from = deps.toNonEmptyString(
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_from
);
previousFilters.period_from = continuityTemporalScope.periodFrom;
}
if (
shouldBackfillPreviousDateScopeFromNavigation &&
@@ -883,11 +880,9 @@ export function createAssistantTransitionPolicy(deps) {
if (
shouldBackfillPreviousDateScopeFromNavigation &&
!deps.toNonEmptyString(previousFilters.period_to) &&
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_to
continuityTemporalScope.periodTo
) {
previousFilters.period_to = deps.toNonEmptyString(
readAddressDebugFilters(continuitySnapshot.lastGroundedAddressDebug)?.period_to
);
previousFilters.period_to = continuityTemporalScope.periodTo;
}
const rootContextOnlyPivot = Boolean(
(deps.isInventorySelectedObjectIntent(sourceIntentHint) || currentFrameKind === "inventory_drilldown") &&