ARCH: добавить policy gate ответа MCP discovery
This commit is contained in:
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.runAssistantLivingChatRuntime = runAssistantLivingChatRuntime;
|
||||
const assistantMemoryRecapPolicy_1 = require("./assistantMemoryRecapPolicy");
|
||||
const assistantContinuityPolicy_1 = require("./assistantContinuityPolicy");
|
||||
const assistantMcpDiscoveryDebugAttachment_1 = require("./assistantMcpDiscoveryDebugAttachment");
|
||||
const assistantMcpDiscoveryResponsePolicy_1 = require("./assistantMcpDiscoveryResponsePolicy");
|
||||
function hasPriorAssistantTurn(items) {
|
||||
if (!Array.isArray(items)) {
|
||||
return false;
|
||||
@@ -228,6 +230,17 @@ async function runAssistantLivingChatRuntime(input) {
|
||||
debug: null
|
||||
};
|
||||
}
|
||||
const mcpDiscoveryResponsePolicy = (0, assistantMcpDiscoveryResponsePolicy_1.applyAssistantMcpDiscoveryResponsePolicy)({
|
||||
currentReply: chatText,
|
||||
currentReplySource: livingChatSource,
|
||||
livingChatSource,
|
||||
modeDecisionReason: input.modeDecision?.reason ?? null,
|
||||
addressRuntimeMeta
|
||||
});
|
||||
if (mcpDiscoveryResponsePolicy.applied) {
|
||||
chatText = mcpDiscoveryResponsePolicy.reply_text;
|
||||
livingChatSource = mcpDiscoveryResponsePolicy.reply_source;
|
||||
}
|
||||
const predecomposeContract = addressRuntimeMeta.predecomposeContract && typeof addressRuntimeMeta.predecomposeContract === "object"
|
||||
? addressRuntimeMeta.predecomposeContract
|
||||
: null;
|
||||
@@ -245,6 +258,9 @@ async function runAssistantLivingChatRuntime(input) {
|
||||
living_router_mode: input.modeDecision?.mode ?? "chat",
|
||||
living_router_reason: input.modeDecision?.reason ?? "living_chat_signal_detected",
|
||||
living_chat_response_source: livingChatSource,
|
||||
mcp_discovery_response_policy_v1: mcpDiscoveryResponsePolicy,
|
||||
mcp_discovery_response_candidate_v1: mcpDiscoveryResponsePolicy.candidate,
|
||||
mcp_discovery_response_applied: mcpDiscoveryResponsePolicy.applied,
|
||||
living_chat_script_guard_applied: livingChatScriptGuardApplied,
|
||||
living_chat_script_guard_reason: livingChatScriptGuardReason,
|
||||
living_chat_grounding_guard_applied: livingChatGroundingGuardApplied,
|
||||
@@ -278,6 +294,6 @@ async function runAssistantLivingChatRuntime(input) {
|
||||
return {
|
||||
handled: true,
|
||||
chatText,
|
||||
debug
|
||||
debug: (0, assistantMcpDiscoveryDebugAttachment_1.attachAssistantMcpDiscoveryDebug)(debug, { addressRuntimeMeta })
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION = void 0;
|
||||
exports.applyAssistantMcpDiscoveryResponsePolicy = applyAssistantMcpDiscoveryResponsePolicy;
|
||||
const assistantMcpDiscoveryResponseCandidate_1 = require("./assistantMcpDiscoveryResponseCandidate");
|
||||
exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION = "assistant_mcp_discovery_response_policy_v1";
|
||||
const ALLOWED_CANDIDATE_STATUSES = new Set([
|
||||
"ready_for_guarded_use",
|
||||
"checked_sources_only_candidate",
|
||||
"clarification_candidate"
|
||||
]);
|
||||
function toRecordObject(value) {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
function toNonEmptyString(value) {
|
||||
if (value === null || value === undefined) {
|
||||
return null;
|
||||
}
|
||||
const text = String(value).trim();
|
||||
return text.length > 0 ? text : null;
|
||||
}
|
||||
function normalizeReasonCode(value) {
|
||||
const normalized = value
|
||||
.trim()
|
||||
.replace(/[^\p{L}\p{N}_.:-]+/gu, "_")
|
||||
.replace(/^_+|_+$/g, "")
|
||||
.toLowerCase();
|
||||
return normalized.length > 0 ? normalized.slice(0, 120) : null;
|
||||
}
|
||||
function pushReason(target, value) {
|
||||
const normalized = normalizeReasonCode(value);
|
||||
if (normalized && !target.includes(normalized)) {
|
||||
target.push(normalized);
|
||||
}
|
||||
}
|
||||
function hasInternalMechanics(value) {
|
||||
const text = value.toLowerCase();
|
||||
return (text.includes("query_documents") ||
|
||||
text.includes("query_movements") ||
|
||||
text.includes("primitive") ||
|
||||
text.includes("runtime_") ||
|
||||
text.includes("planner_") ||
|
||||
text.includes("catalog_") ||
|
||||
text.includes("select "));
|
||||
}
|
||||
function isMcpDiscoveryEntryPointContract(value) {
|
||||
const record = toRecordObject(value);
|
||||
return (record?.schema_version === "assistant_mcp_discovery_runtime_entry_point_v1" &&
|
||||
record?.policy_owner === "assistantMcpDiscoveryRuntimeEntryPoint");
|
||||
}
|
||||
function resolveEntryPoint(input) {
|
||||
if (isMcpDiscoveryEntryPointContract(input.entryPoint)) {
|
||||
return input.entryPoint;
|
||||
}
|
||||
const runtimeMetaEntryPoint = input.addressRuntimeMeta?.mcpDiscoveryRuntimeEntryPoint ??
|
||||
input.addressRuntimeMeta?.assistantMcpDiscoveryRuntimeEntryPoint ??
|
||||
input.addressRuntimeMeta?.assistant_mcp_discovery_entry_point_v1;
|
||||
return isMcpDiscoveryEntryPointContract(runtimeMetaEntryPoint) ? runtimeMetaEntryPoint : null;
|
||||
}
|
||||
function isUnsupportedCurrentTurnBoundary(input) {
|
||||
return (input.modeDecisionReason === "unsupported_current_turn_meaning_boundary" ||
|
||||
input.livingChatSource === "deterministic_unsupported_current_turn_boundary" ||
|
||||
input.currentReplySource === "deterministic_unsupported_current_turn_boundary");
|
||||
}
|
||||
function applyAssistantMcpDiscoveryResponsePolicy(input) {
|
||||
const currentReply = String(input.currentReply ?? "");
|
||||
const currentReplySource = toNonEmptyString(input.currentReplySource) ?? toNonEmptyString(input.livingChatSource) ?? "unknown";
|
||||
const entryPoint = resolveEntryPoint(input);
|
||||
const candidate = (0, assistantMcpDiscoveryResponseCandidate_1.buildAssistantMcpDiscoveryResponseCandidate)(entryPoint);
|
||||
const reasonCodes = [...candidate.reason_codes];
|
||||
if (!entryPoint) {
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_no_entry_point");
|
||||
}
|
||||
if (!isUnsupportedCurrentTurnBoundary(input)) {
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_not_unsupported_boundary");
|
||||
}
|
||||
if (!ALLOWED_CANDIDATE_STATUSES.has(candidate.candidate_status)) {
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_status_not_allowed");
|
||||
}
|
||||
if (!candidate.eligible_for_future_hot_runtime) {
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_not_eligible");
|
||||
}
|
||||
if (!toNonEmptyString(candidate.reply_text)) {
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_missing_reply_text");
|
||||
}
|
||||
if (candidate.reply_text && hasInternalMechanics(candidate.reply_text)) {
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_contains_internal_mechanics");
|
||||
}
|
||||
const canApply = Boolean(entryPoint) &&
|
||||
isUnsupportedCurrentTurnBoundary(input) &&
|
||||
ALLOWED_CANDIDATE_STATUSES.has(candidate.candidate_status) &&
|
||||
candidate.eligible_for_future_hot_runtime &&
|
||||
Boolean(toNonEmptyString(candidate.reply_text)) &&
|
||||
!hasInternalMechanics(String(candidate.reply_text ?? ""));
|
||||
if (!canApply) {
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_kept_current_reply");
|
||||
return {
|
||||
schema_version: exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION,
|
||||
policy_owner: "assistantMcpDiscoveryResponsePolicy",
|
||||
decision: "keep_current_reply",
|
||||
applied: false,
|
||||
reply_text: currentReply,
|
||||
reply_source: currentReplySource,
|
||||
candidate,
|
||||
reason_codes: reasonCodes
|
||||
};
|
||||
}
|
||||
pushReason(reasonCodes, "mcp_discovery_response_policy_candidate_applied");
|
||||
return {
|
||||
schema_version: exports.ASSISTANT_MCP_DISCOVERY_RESPONSE_POLICY_SCHEMA_VERSION,
|
||||
policy_owner: "assistantMcpDiscoveryResponsePolicy",
|
||||
decision: "apply_candidate",
|
||||
applied: true,
|
||||
reply_text: String(candidate.reply_text),
|
||||
reply_source: "mcp_discovery_response_candidate_guarded",
|
||||
candidate,
|
||||
reason_codes: reasonCodes
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user