ARCH: ввести resumable discovery loop state для clarification follow-up

This commit is contained in:
2026-04-23 11:54:36 +03:00
parent 0d3b33578e
commit e4cab85dd9
11 changed files with 715 additions and 11 deletions
@@ -5,6 +5,13 @@ exports.readAssistantMcpDiscoveryEntityAmbiguityCandidates = readAssistantMcpDis
exports.readAssistantMcpDiscoveryEntityCandidates = readAssistantMcpDiscoveryEntityCandidates;
exports.readAssistantMcpDiscoveryPilotScope = readAssistantMcpDiscoveryPilotScope;
exports.readAssistantMcpDiscoveryRankingNeed = readAssistantMcpDiscoveryRankingNeed;
exports.readAssistantMcpDiscoveryLoopStatus = readAssistantMcpDiscoveryLoopStatus;
exports.readAssistantMcpDiscoveryLoopSelectedChainId = readAssistantMcpDiscoveryLoopSelectedChainId;
exports.readAssistantMcpDiscoveryLoopPendingAxes = readAssistantMcpDiscoveryLoopPendingAxes;
exports.readAssistantMcpDiscoveryLoopProvidedAxes = readAssistantMcpDiscoveryLoopProvidedAxes;
exports.readAssistantMcpDiscoveryLoopAskedDomainFamily = readAssistantMcpDiscoveryLoopAskedDomainFamily;
exports.readAssistantMcpDiscoveryLoopAskedActionFamily = readAssistantMcpDiscoveryLoopAskedActionFamily;
exports.readAssistantMcpDiscoveryLoopUnsupportedFamily = readAssistantMcpDiscoveryLoopUnsupportedFamily;
exports.readAssistantMcpDiscoveryMetadataRouteFamily = readAssistantMcpDiscoveryMetadataRouteFamily;
exports.readAssistantMcpDiscoveryMetadataRouteFamilySelectionBasis = readAssistantMcpDiscoveryMetadataRouteFamilySelectionBasis;
exports.readAssistantMcpDiscoveryMetadataSelectedEntitySet = readAssistantMcpDiscoveryMetadataSelectedEntitySet;
@@ -98,6 +105,9 @@ function readAssistantMcpDiscoveryActionFamily(debug, toNonEmptyString = fallbac
function readAssistantMcpDiscoveryBridge(debug) {
return toRecordObject(readAssistantMcpDiscoveryEntry(debug)?.bridge);
}
function readAssistantMcpDiscoveryLoopState(debug) {
return toRecordObject(readAssistantMcpDiscoveryBridge(debug)?.loop_state);
}
function readAssistantMcpDiscoveryDerivedMetadataSurface(debug) {
const bridge = readAssistantMcpDiscoveryBridge(debug);
const pilot = toRecordObject(bridge?.pilot);
@@ -152,7 +162,37 @@ function readAssistantMcpDiscoveryPilotScope(debug, toNonEmptyString = fallbackT
return toNonEmptyString(pilot?.pilot_scope);
}
function readAssistantMcpDiscoveryRankingNeed(debug, toNonEmptyString = fallbackToNonEmptyString) {
return toNonEmptyString(readAssistantMcpDiscoveryDataNeedGraph(debug)?.ranking_need);
return (toNonEmptyString(readAssistantMcpDiscoveryLoopState(debug)?.ranking_need) ??
toNonEmptyString(readAssistantMcpDiscoveryDataNeedGraph(debug)?.ranking_need));
}
function readAssistantMcpDiscoveryLoopStatus(debug, toNonEmptyString = fallbackToNonEmptyString) {
return toNonEmptyString(readAssistantMcpDiscoveryLoopState(debug)?.loop_status);
}
function readAssistantMcpDiscoveryLoopSelectedChainId(debug, toNonEmptyString = fallbackToNonEmptyString) {
return toNonEmptyString(readAssistantMcpDiscoveryLoopState(debug)?.selected_chain_id);
}
function readAssistantMcpDiscoveryLoopPendingAxes(debug, toNonEmptyString = fallbackToNonEmptyString) {
const values = readAssistantMcpDiscoveryLoopState(debug)?.pending_axes;
if (!Array.isArray(values)) {
return [];
}
return values.map((item) => toNonEmptyString(item)).filter((item) => Boolean(item));
}
function readAssistantMcpDiscoveryLoopProvidedAxes(debug, toNonEmptyString = fallbackToNonEmptyString) {
const values = readAssistantMcpDiscoveryLoopState(debug)?.provided_axes;
if (!Array.isArray(values)) {
return [];
}
return values.map((item) => toNonEmptyString(item)).filter((item) => Boolean(item));
}
function readAssistantMcpDiscoveryLoopAskedDomainFamily(debug, toNonEmptyString = fallbackToNonEmptyString) {
return toNonEmptyString(readAssistantMcpDiscoveryLoopState(debug)?.asked_domain_family);
}
function readAssistantMcpDiscoveryLoopAskedActionFamily(debug, toNonEmptyString = fallbackToNonEmptyString) {
return toNonEmptyString(readAssistantMcpDiscoveryLoopState(debug)?.asked_action_family);
}
function readAssistantMcpDiscoveryLoopUnsupportedFamily(debug, toNonEmptyString = fallbackToNonEmptyString) {
return toNonEmptyString(readAssistantMcpDiscoveryLoopState(debug)?.unsupported_but_understood_family);
}
function readAssistantMcpDiscoveryMetadataRouteFamily(debug, toNonEmptyString = fallbackToNonEmptyString) {
return toNonEmptyString(readAssistantMcpDiscoveryDerivedMetadataSurface(debug)?.downstream_route_family);
@@ -1,11 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ASSISTANT_MCP_DISCOVERY_RUNTIME_BRIDGE_SCHEMA_VERSION = void 0;
exports.ASSISTANT_MCP_DISCOVERY_LOOP_STATE_SCHEMA_VERSION = exports.ASSISTANT_MCP_DISCOVERY_RUNTIME_BRIDGE_SCHEMA_VERSION = void 0;
exports.runAssistantMcpDiscoveryRuntimeBridge = runAssistantMcpDiscoveryRuntimeBridge;
const assistantMcpDiscoveryAnswerAdapter_1 = require("./assistantMcpDiscoveryAnswerAdapter");
const assistantMcpDiscoveryPilotExecutor_1 = require("./assistantMcpDiscoveryPilotExecutor");
const assistantMcpDiscoveryPlanner_1 = require("./assistantMcpDiscoveryPlanner");
exports.ASSISTANT_MCP_DISCOVERY_RUNTIME_BRIDGE_SCHEMA_VERSION = "assistant_mcp_discovery_runtime_bridge_v1";
exports.ASSISTANT_MCP_DISCOVERY_LOOP_STATE_SCHEMA_VERSION = "assistant_mcp_discovery_loop_state_v1";
function normalizeReasonCode(value) {
const normalized = value
.trim()
@@ -48,6 +49,58 @@ function bridgeStatusFor(pilot, draft) {
function businessFactAnswerAllowed(draft) {
return draft.answer_mode === "confirmed_with_bounded_inference" || draft.answer_mode === "bounded_inference_only";
}
function loopStatusFor(bridgeStatus) {
if (bridgeStatus === "needs_clarification") {
return "awaiting_clarification";
}
if (bridgeStatus === "blocked" || bridgeStatus === "unsupported") {
return "blocked";
}
return "ready_for_next_hop";
}
function flattenAxes(pilot, source) {
const result = [];
for (const step of pilot.dry_run.execution_steps) {
if (source === "provided_axes") {
for (const axis of step.provided_axes) {
if (axis && !result.includes(axis)) {
result.push(axis);
}
}
continue;
}
for (const option of step.missing_axis_options) {
for (const axis of option) {
if (axis && !result.includes(axis)) {
result.push(axis);
}
}
}
}
return result;
}
function entityCandidatesFromPlanner(planner) {
const values = planner.discovery_plan.turn_meaning_ref?.explicit_entity_candidates ?? [];
return uniqueStrings(values);
}
function buildLoopState(planner, pilot, bridgeStatus) {
return {
schema_version: exports.ASSISTANT_MCP_DISCOVERY_LOOP_STATE_SCHEMA_VERSION,
policy_owner: "assistantMcpDiscoveryRuntimeBridge",
loop_status: loopStatusFor(bridgeStatus),
selected_chain_id: planner.selected_chain_id,
pilot_scope: pilot.pilot_scope,
asked_domain_family: planner.discovery_plan.turn_meaning_ref?.asked_domain_family ?? null,
asked_action_family: planner.discovery_plan.turn_meaning_ref?.asked_action_family ?? null,
unsupported_but_understood_family: planner.discovery_plan.turn_meaning_ref?.unsupported_but_understood_family ?? null,
ranking_need: planner.data_need_graph?.ranking_need ?? planner.discovery_plan.turn_meaning_ref?.seeded_ranking_need ?? null,
pending_axes: flattenAxes(pilot, "missing_axis_options"),
provided_axes: flattenAxes(pilot, "provided_axes"),
explicit_entity_candidates: entityCandidatesFromPlanner(planner),
explicit_organization_scope: planner.discovery_plan.turn_meaning_ref?.explicit_organization_scope ?? null,
explicit_date_scope: planner.discovery_plan.turn_meaning_ref?.explicit_date_scope ?? null
};
}
async function runAssistantMcpDiscoveryRuntimeBridge(input) {
const planner = (0, assistantMcpDiscoveryPlanner_1.planAssistantMcpDiscovery)({
semanticDataNeed: input.semanticDataNeed,
@@ -58,9 +111,11 @@ async function runAssistantMcpDiscoveryRuntimeBridge(input) {
const pilot = await (0, assistantMcpDiscoveryPilotExecutor_1.executeAssistantMcpDiscoveryPilot)(planner, input.deps);
const answerDraft = (0, assistantMcpDiscoveryAnswerAdapter_1.buildAssistantMcpDiscoveryAnswerDraft)(pilot);
const bridgeStatus = bridgeStatusFor(pilot, answerDraft);
const loopState = buildLoopState(planner, pilot, bridgeStatus);
const reasonCodes = uniqueStrings([...planner.reason_codes, ...pilot.reason_codes, ...answerDraft.reason_codes]);
pushReason(reasonCodes, `runtime_bridge_status_${bridgeStatus}`);
pushReason(reasonCodes, "runtime_bridge_not_wired_to_hot_assistant_answer");
pushReason(reasonCodes, `runtime_bridge_loop_state_${loopState.loop_status}`);
return {
schema_version: exports.ASSISTANT_MCP_DISCOVERY_RUNTIME_BRIDGE_SCHEMA_VERSION,
policy_owner: "assistantMcpDiscoveryRuntimeBridge",
@@ -69,6 +124,7 @@ async function runAssistantMcpDiscoveryRuntimeBridge(input) {
planner,
pilot,
answer_draft: answerDraft,
loop_state: loopState,
user_facing_response_allowed: bridgeStatus !== "blocked",
business_fact_answer_allowed: businessFactAnswerAllowed(answerDraft),
requires_user_clarification: bridgeStatus === "needs_clarification",
@@ -254,14 +254,92 @@ function mapAddressIntentToFollowupMeaning(intent) {
unsupported: null
};
}
function mapLoopClarificationSeedToFollowupMeaning(input) {
if (input.domain || input.action || input.unsupported) {
return {
domain: input.domain,
action: input.action,
unsupported: input.unsupported
};
}
if (input.selectedChainId === "metadata_lane_clarification") {
return {
domain: "metadata",
action: "resolve_next_lane",
unsupported: "metadata_lane_choice_clarification"
};
}
return {
domain: null,
action: null,
unsupported: null
};
}
function pilotScopeFromLoopClarificationSeed(selectedChainId, action) {
if (!selectedChainId) {
return null;
}
if (selectedChainId === "metadata_inspection" ||
selectedChainId === "metadata_lane_clarification" ||
selectedChainId === "catalog_drilldown") {
return "metadata_inspection_v1";
}
if (selectedChainId === "movement_evidence") {
return "counterparty_movement_evidence_query_movements_v1";
}
if (selectedChainId === "document_evidence") {
return "counterparty_document_evidence_query_documents_v1";
}
if (selectedChainId === "lifecycle") {
return "counterparty_lifecycle_query_documents_v1";
}
if (selectedChainId === "entity_resolution") {
return "entity_resolution_search_v1";
}
if (selectedChainId === "value_flow_comparison") {
return "counterparty_bidirectional_value_flow_query_movements_v1";
}
if (selectedChainId === "value_flow_ranking" || selectedChainId === "value_flow") {
if (action === "payout") {
return "counterparty_supplier_payout_query_movements_v1";
}
if (action === "net_value_flow") {
return "counterparty_bidirectional_value_flow_query_movements_v1";
}
return "counterparty_value_flow_query_movements_v1";
}
return null;
}
function collectFollowupDiscoverySeed(followupContext) {
const previousFilters = toRecordObject(followupContext?.previous_filters);
const rootFilters = toRecordObject(followupContext?.root_filters);
const pilotScope = toNonEmptyString(followupContext?.previous_discovery_pilot_scope);
const loopStatus = toNonEmptyString(followupContext?.previous_discovery_loop_status);
const loopSelectedChainId = toNonEmptyString(followupContext?.previous_discovery_loop_selected_chain_id);
const loopPendingAxes = collectEntityCandidates(followupContext?.previous_discovery_loop_pending_axes);
const loopProvidedAxes = collectEntityCandidates(followupContext?.previous_discovery_loop_provided_axes);
const loopAskedDomainFamily = toNonEmptyString(followupContext?.previous_discovery_loop_asked_domain_family);
const loopAskedActionFamily = toNonEmptyString(followupContext?.previous_discovery_loop_asked_action_family);
const loopUnsupportedFamily = toNonEmptyString(followupContext?.previous_discovery_loop_unsupported_family);
const previousIntent = toNonEmptyString(followupContext?.target_intent) ?? toNonEmptyString(followupContext?.previous_intent);
const mapped = mapPilotScopeToFollowupMeaning(pilotScope).domain !== null
? mapPilotScopeToFollowupMeaning(pilotScope)
: mapAddressIntentToFollowupMeaning(previousIntent);
const loopMapped = loopStatus === "awaiting_clarification"
? mapLoopClarificationSeedToFollowupMeaning({
selectedChainId: loopSelectedChainId,
domain: loopAskedDomainFamily,
action: loopAskedActionFamily,
unsupported: loopUnsupportedFamily
})
: {
domain: null,
action: null,
unsupported: null
};
const effectivePilotScope = pilotScope ?? pilotScopeFromLoopClarificationSeed(loopSelectedChainId, loopMapped.action);
const mapped = loopMapped.domain !== null || loopMapped.action !== null || loopMapped.unsupported !== null
? loopMapped
: mapPilotScopeToFollowupMeaning(effectivePilotScope).domain !== null
? mapPilotScopeToFollowupMeaning(effectivePilotScope)
: mapAddressIntentToFollowupMeaning(previousIntent);
const discoveryEntities = collectEntityCandidates(followupContext?.previous_discovery_entity_candidates);
const entityResolutionStatus = toNonEmptyString(followupContext?.previous_discovery_entity_resolution_status);
const entityResolutionAmbiguityCandidates = collectEntityCandidates(followupContext?.previous_discovery_entity_ambiguity_candidates);
@@ -280,10 +358,14 @@ function collectFollowupDiscoverySeed(followupContext) {
const dateScope = collectDateScopeFromFilters(previousFilters) ??
collectDateScopeFromFilters(rootFilters);
return {
pilotScope,
pilotScope: effectivePilotScope,
domain: mapped.domain,
action: mapped.action,
unsupported: mapped.unsupported,
loopStatus,
loopSelectedChainId,
loopPendingAxes,
loopProvidedAxes,
counterparty,
discoveryEntity: ambiguityBlocksImplicitGrounding ? null : discoveryEntities[0] ?? null,
entityResolutionStatus,
@@ -963,6 +1045,7 @@ function buildAssistantMcpDiscoveryTurnInput(input) {
!predecomposeDateScope &&
!rawDateScope &&
followupSeed.dateScope);
const clarificationLoopSeedApplied = Boolean(followupSeed.loopStatus === "awaiting_clarification" && followupSeed.loopSelectedChainId);
const turnMeaning = {
asked_domain_family: lifecycleSignal
? "counterparty_lifecycle"
@@ -1151,6 +1234,9 @@ function buildAssistantMcpDiscoveryTurnInput(input) {
if (followupDiscoverySeedApplicable) {
pushReason(reasonCodes, "mcp_discovery_seeded_from_followup_context");
}
if (clarificationLoopSeedApplied) {
pushReason(reasonCodes, "mcp_discovery_resumed_from_saved_loop_state");
}
if (effectiveMetadataFollowupSeedApplicable) {
pushReason(reasonCodes, "mcp_discovery_metadata_seeded_from_followup_context");
}
@@ -508,6 +508,13 @@ function createAssistantTransitionPolicy(deps) {
const sourceDiscoveryMetadataAmbiguityEntitySets = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryMetadataAmbiguityEntitySets)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryEntityResolutionStatus = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryEntityResolutionStatus)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryEntityCandidates = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryEntityCandidates)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryLoopStatus = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryLoopStatus)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryLoopSelectedChainId = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryLoopSelectedChainId)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryLoopPendingAxes = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryLoopPendingAxes)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryLoopProvidedAxes = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryLoopProvidedAxes)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryLoopAskedDomainFamily = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryLoopAskedDomainFamily)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryLoopAskedActionFamily = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryLoopAskedActionFamily)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryLoopUnsupportedFamily = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryLoopUnsupportedFamily)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryRankingNeed = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryRankingNeed)(carryoverSourceDebug, deps.toNonEmptyString);
const sourceDiscoveryEntityAmbiguityCandidates = (0, assistantContinuityPolicy_1.readAssistantMcpDiscoveryEntityAmbiguityCandidates)(carryoverSourceDebug, deps.toNonEmptyString);
const llmExplicitIntent = deps.toNonEmptyString(llmPreDecomposeMeta?.predecomposeContract?.intent);
@@ -746,6 +753,13 @@ function createAssistantTransitionPolicy(deps) {
previous_discovery_pilot_scope: sourceDiscoveryPilotScope ?? undefined,
previous_discovery_entity_resolution_status: sourceDiscoveryEntityResolutionStatus ?? undefined,
previous_discovery_entity_candidates: sourceDiscoveryEntityCandidates.length > 0 ? sourceDiscoveryEntityCandidates : undefined,
previous_discovery_loop_status: sourceDiscoveryLoopStatus ?? undefined,
previous_discovery_loop_selected_chain_id: sourceDiscoveryLoopSelectedChainId ?? undefined,
previous_discovery_loop_pending_axes: sourceDiscoveryLoopPendingAxes.length > 0 ? sourceDiscoveryLoopPendingAxes : undefined,
previous_discovery_loop_provided_axes: sourceDiscoveryLoopProvidedAxes.length > 0 ? sourceDiscoveryLoopProvidedAxes : undefined,
previous_discovery_loop_asked_domain_family: sourceDiscoveryLoopAskedDomainFamily ?? undefined,
previous_discovery_loop_asked_action_family: sourceDiscoveryLoopAskedActionFamily ?? undefined,
previous_discovery_loop_unsupported_family: sourceDiscoveryLoopUnsupportedFamily ?? undefined,
previous_discovery_ranking_need: sourceDiscoveryRankingNeed ?? undefined,
previous_discovery_entity_ambiguity_candidates: sourceDiscoveryEntityAmbiguityCandidates.length > 0
? sourceDiscoveryEntityAmbiguityCandidates