АРЧ АП11 - Добавить state transition owner для assistant runtime
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
type FinalizeAssistantAddressTurnInput
|
||||
} from "./assistantAddressTurnFinalizeRuntimeAdapter";
|
||||
import { attachAssistantRuntimeContractShadow } from "./assistantRuntimeContractResolver";
|
||||
import { attachAssistantStateTransition } from "./assistantStateTransitionRuntimeAdapter";
|
||||
import { attachAssistantTruthAnswerPolicy } from "./assistantTruthAnswerPolicyRuntimeAdapter";
|
||||
|
||||
export interface RunAssistantAddressLaneResponseRuntimeInput<ResponseType = AssistantMessageResponsePayload> {
|
||||
@@ -256,6 +257,10 @@ export function runAssistantAddressLaneResponseRuntime<ResponseType = AssistantM
|
||||
addressRuntimeMeta: input.llmPreDecomposeMeta,
|
||||
replyType: normalizeAddressReplyType(input.addressLane.reply_type)
|
||||
});
|
||||
const debugWithStateTransition = attachAssistantStateTransition(debugWithTruthAnswerPolicy, {
|
||||
addressRuntimeMeta: input.llmPreDecomposeMeta,
|
||||
replyType: normalizeAddressReplyType(input.addressLane.reply_type)
|
||||
});
|
||||
const finalization = finalizeAddressTurnSafe({
|
||||
sessionId: input.sessionId,
|
||||
userMessage: input.userMessage,
|
||||
@@ -263,7 +268,7 @@ export function runAssistantAddressLaneResponseRuntime<ResponseType = AssistantM
|
||||
assistantReply: safeAddressReply,
|
||||
replyType: normalizeAddressReplyType(input.addressLane.reply_type),
|
||||
addressLaneDebug: normalizeAddressLaneDebug(input.addressLane.debug),
|
||||
debug: debugWithTruthAnswerPolicy,
|
||||
debug: debugWithStateTransition,
|
||||
carryoverMeta: normalizeCarryoverMeta(input.carryoverMeta),
|
||||
llmPreDecomposeMeta: normalizeLlmPreDecomposeMeta(input.llmPreDecomposeMeta),
|
||||
appendItem: input.appendItem,
|
||||
@@ -276,6 +281,6 @@ export function runAssistantAddressLaneResponseRuntime<ResponseType = AssistantM
|
||||
|
||||
return {
|
||||
response: finalization.response as ResponseType,
|
||||
debug: debugWithTruthAnswerPolicy
|
||||
debug: debugWithStateTransition
|
||||
};
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import type {
|
||||
TemporalGuardAudit
|
||||
} from "./assistantRuntimeGuards";
|
||||
import { attachAssistantRuntimeContractShadow } from "./assistantRuntimeContractResolver";
|
||||
import { attachAssistantStateTransition } from "./assistantStateTransitionRuntimeAdapter";
|
||||
import { attachAssistantTruthAnswerPolicy } from "./assistantTruthAnswerPolicyRuntimeAdapter";
|
||||
import { buildStage4AnswerContractAuditV1 } from "./assistantStage4AnswerContractAudit";
|
||||
|
||||
@@ -179,7 +180,13 @@ export function buildDeepAnalysisDebugPayload(input: DeepAnalysisDebugPayloadInp
|
||||
addressRuntimeMeta: input.addressRuntimeMetaForDeep as unknown as Record<string, unknown> | null | undefined,
|
||||
groundingStatus: input.groundingCheck.status
|
||||
});
|
||||
return attachAssistantTruthAnswerPolicy(debugWithRuntimeContracts, {
|
||||
const debugWithTruthAnswerPolicy = attachAssistantTruthAnswerPolicy(debugWithRuntimeContracts, {
|
||||
addressRuntimeMeta: input.addressRuntimeMetaForDeep as unknown as Record<string, unknown> | null | undefined,
|
||||
groundingStatus: input.groundingCheck.status,
|
||||
coverageReport: input.coverageReport as unknown as Record<string, unknown>,
|
||||
replyType: "deep_analysis"
|
||||
});
|
||||
return attachAssistantStateTransition(debugWithTruthAnswerPolicy, {
|
||||
addressRuntimeMeta: input.addressRuntimeMetaForDeep as unknown as Record<string, unknown> | null | undefined,
|
||||
groundingStatus: input.groundingCheck.status,
|
||||
coverageReport: input.coverageReport as unknown as Record<string, unknown>,
|
||||
|
||||
@@ -0,0 +1,331 @@
|
||||
import {
|
||||
ASSISTANT_STATE_TRANSITION_RUNTIME_SCHEMA_VERSION,
|
||||
type AssistantCarryoverDepth,
|
||||
type AssistantCoverageGateState,
|
||||
type AssistantRuntimeContractShadowDecision,
|
||||
type AssistantStateTransitionApplicationStatus,
|
||||
type AssistantStateTransitionRuntimeContract,
|
||||
type AssistantTruthAnswerPolicyRuntimeContract,
|
||||
type AssistantTransitionClassId
|
||||
} from "../types/assistantRuntimeContracts";
|
||||
import { resolveAssistantRuntimeContractShadow } from "./assistantRuntimeContractResolver";
|
||||
import { getAssistantTransitionContract } from "./assistantRuntimeContractRegistry";
|
||||
import { resolveAssistantTruthAnswerPolicyRuntime } from "./assistantTruthAnswerPolicyRuntimeAdapter";
|
||||
|
||||
export interface ResolveAssistantStateTransitionRuntimeInput {
|
||||
addressDebug?: Record<string, unknown> | null;
|
||||
addressRuntimeMeta?: Record<string, unknown> | null;
|
||||
groundingStatus?: unknown;
|
||||
coverageReport?: Record<string, unknown> | null;
|
||||
replyType?: unknown;
|
||||
runtimeContractShadow?: AssistantRuntimeContractShadowDecision | null;
|
||||
truthAnswerPolicy?: AssistantTruthAnswerPolicyRuntimeContract | null;
|
||||
}
|
||||
|
||||
export interface AssistantStateTransitionRuntimeFields {
|
||||
assistant_state_transition_v1: AssistantStateTransitionRuntimeContract;
|
||||
state_transition_contract: AssistantStateTransitionRuntimeContract;
|
||||
state_transition_id: AssistantTransitionClassId | null;
|
||||
state_transition_status: AssistantStateTransitionApplicationStatus;
|
||||
effective_carryover_depth: AssistantCarryoverDepth;
|
||||
}
|
||||
|
||||
function toRecordObject(value: unknown): Record<string, unknown> | null {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
return null;
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function toNonEmptyString(value: unknown): string | null {
|
||||
if (value === null || value === undefined) {
|
||||
return null;
|
||||
}
|
||||
const text = String(value).trim();
|
||||
return text.length > 0 ? text : null;
|
||||
}
|
||||
|
||||
function uniqueStrings(values: string[]): string[] {
|
||||
return Array.from(new Set(values.filter((item) => item.trim().length > 0)));
|
||||
}
|
||||
|
||||
function baseStateActions(): AssistantStateTransitionRuntimeContract["state_actions"] {
|
||||
return {
|
||||
living_mode_state: "none",
|
||||
root_frame_state: "none",
|
||||
selected_object_frame_state: "none",
|
||||
meta_frame_state: "none",
|
||||
clarification_state: "none",
|
||||
coverage_gate_state: "none",
|
||||
answer_context_state: "none"
|
||||
};
|
||||
}
|
||||
|
||||
function stateActionsFor(transitionId: AssistantTransitionClassId | null): AssistantStateTransitionRuntimeContract["state_actions"] {
|
||||
const actions = baseStateActions();
|
||||
if (!transitionId) {
|
||||
return actions;
|
||||
}
|
||||
actions.living_mode_state = "update";
|
||||
actions.coverage_gate_state = "create";
|
||||
|
||||
if (transitionId === "T1") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "create",
|
||||
selected_object_frame_state: "clear",
|
||||
meta_frame_state: "clear",
|
||||
clarification_state: "clear",
|
||||
answer_context_state: "create"
|
||||
};
|
||||
}
|
||||
if (transitionId === "T2") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "update",
|
||||
selected_object_frame_state: "clear",
|
||||
meta_frame_state: "clear",
|
||||
clarification_state: "clear",
|
||||
answer_context_state: "create"
|
||||
};
|
||||
}
|
||||
if (transitionId === "T3") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "preserve",
|
||||
selected_object_frame_state: "create",
|
||||
meta_frame_state: "clear",
|
||||
clarification_state: "clear",
|
||||
answer_context_state: "create"
|
||||
};
|
||||
}
|
||||
if (transitionId === "T4" || transitionId === "T5") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "preserve",
|
||||
selected_object_frame_state: "reuse",
|
||||
meta_frame_state: "clear",
|
||||
clarification_state: "clear",
|
||||
answer_context_state: "create"
|
||||
};
|
||||
}
|
||||
if (transitionId === "T6") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "preserve",
|
||||
selected_object_frame_state: "clear",
|
||||
meta_frame_state: "clear",
|
||||
clarification_state: "clear",
|
||||
answer_context_state: "create"
|
||||
};
|
||||
}
|
||||
if (transitionId === "T7") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "preserve",
|
||||
selected_object_frame_state: "preserve",
|
||||
meta_frame_state: "none",
|
||||
clarification_state: "update",
|
||||
answer_context_state: "none"
|
||||
};
|
||||
}
|
||||
if (transitionId === "T8") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "preserve",
|
||||
selected_object_frame_state: "preserve",
|
||||
meta_frame_state: "create",
|
||||
clarification_state: "none",
|
||||
answer_context_state: "reuse"
|
||||
};
|
||||
}
|
||||
if (transitionId === "T9") {
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "preserve",
|
||||
selected_object_frame_state: "preserve",
|
||||
meta_frame_state: "create",
|
||||
clarification_state: "none",
|
||||
coverage_gate_state: "preserve",
|
||||
answer_context_state: "reuse"
|
||||
};
|
||||
}
|
||||
return {
|
||||
...actions,
|
||||
root_frame_state: "none",
|
||||
selected_object_frame_state: "clear",
|
||||
meta_frame_state: "none",
|
||||
clarification_state: "none",
|
||||
coverage_gate_state: "block",
|
||||
answer_context_state: "none"
|
||||
};
|
||||
}
|
||||
|
||||
function applicationStatusFor(input: {
|
||||
transitionId: AssistantTransitionClassId | null;
|
||||
coverageGateState: AssistantCoverageGateState;
|
||||
}): AssistantStateTransitionApplicationStatus {
|
||||
if (!input.transitionId) {
|
||||
return "unresolved";
|
||||
}
|
||||
if (input.coverageGateState.truth_mode === "clarification_required") {
|
||||
return "clarification_required";
|
||||
}
|
||||
if (input.transitionId === "T10" || input.coverageGateState.coverage_status === "blocked" || input.coverageGateState.truth_mode === "unsupported") {
|
||||
return "blocked";
|
||||
}
|
||||
return "applied";
|
||||
}
|
||||
|
||||
function effectiveCarryoverDepthFor(input: {
|
||||
declared: AssistantCarryoverDepth | null;
|
||||
truthGate: AssistantCarryoverDepth;
|
||||
status: AssistantStateTransitionApplicationStatus;
|
||||
}): AssistantCarryoverDepth {
|
||||
if (input.status === "unresolved" || input.status === "blocked") {
|
||||
return "none";
|
||||
}
|
||||
if (!input.declared) {
|
||||
return input.truthGate;
|
||||
}
|
||||
if (input.status === "clarification_required") {
|
||||
return input.declared === "full" ? "full" : input.truthGate;
|
||||
}
|
||||
if (input.truthGate === "none") {
|
||||
return "none";
|
||||
}
|
||||
if (input.declared === "full") {
|
||||
return input.truthGate;
|
||||
}
|
||||
if (input.truthGate === "full") {
|
||||
return input.declared;
|
||||
}
|
||||
return input.declared === input.truthGate ? input.declared : "none";
|
||||
}
|
||||
|
||||
function resolveShadow(
|
||||
input: ResolveAssistantStateTransitionRuntimeInput,
|
||||
debug: Record<string, unknown>
|
||||
): AssistantRuntimeContractShadowDecision {
|
||||
return (
|
||||
input.runtimeContractShadow ??
|
||||
(toRecordObject(debug.assistant_runtime_contract_v1) as AssistantRuntimeContractShadowDecision | null) ??
|
||||
resolveAssistantRuntimeContractShadow({
|
||||
addressDebug: debug,
|
||||
addressRuntimeMeta: input.addressRuntimeMeta,
|
||||
groundingStatus: input.groundingStatus
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function resolveTruthPolicy(
|
||||
input: ResolveAssistantStateTransitionRuntimeInput,
|
||||
debug: Record<string, unknown>
|
||||
): AssistantTruthAnswerPolicyRuntimeContract {
|
||||
return (
|
||||
input.truthAnswerPolicy ??
|
||||
(toRecordObject(debug.assistant_truth_answer_policy_v1) as AssistantTruthAnswerPolicyRuntimeContract | null) ??
|
||||
resolveAssistantTruthAnswerPolicyRuntime({
|
||||
addressDebug: debug,
|
||||
addressRuntimeMeta: input.addressRuntimeMeta,
|
||||
groundingStatus: input.groundingStatus,
|
||||
coverageReport: input.coverageReport,
|
||||
replyType: input.replyType
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
function reasonCodesFor(input: {
|
||||
shadow: AssistantRuntimeContractShadowDecision;
|
||||
truthPolicy: AssistantTruthAnswerPolicyRuntimeContract;
|
||||
transitionId: AssistantTransitionClassId | null;
|
||||
applicationStatus: AssistantStateTransitionApplicationStatus;
|
||||
}): string[] {
|
||||
return uniqueStrings([
|
||||
`transition_status_${input.applicationStatus}`,
|
||||
...(input.transitionId ? [`transition_${input.transitionId}`] : ["transition_unresolved"]),
|
||||
...input.shadow.transition_contract_reason,
|
||||
...input.shadow.capability_contract_reason,
|
||||
...input.truthPolicy.truth_gate.reason_codes
|
||||
]).slice(0, 40);
|
||||
}
|
||||
|
||||
export function resolveAssistantStateTransitionRuntime(
|
||||
input: ResolveAssistantStateTransitionRuntimeInput
|
||||
): AssistantStateTransitionRuntimeContract {
|
||||
const debug = toRecordObject(input.addressDebug) ?? {};
|
||||
const shadow = resolveShadow(input, debug);
|
||||
const truthPolicy = resolveTruthPolicy(input, debug);
|
||||
const transitionId = shadow.transition_contract_id;
|
||||
const transition = transitionId ? getAssistantTransitionContract(transitionId) : null;
|
||||
const status = applicationStatusFor({
|
||||
transitionId,
|
||||
coverageGateState: truthPolicy.truth_gate
|
||||
});
|
||||
const effectiveCarryoverDepth = effectiveCarryoverDepthFor({
|
||||
declared: transition?.allowed_carryover_depth ?? null,
|
||||
truthGate: truthPolicy.truth_gate.carryover_eligibility,
|
||||
status
|
||||
});
|
||||
const forbiddenCarryover = uniqueStrings([
|
||||
...(transition?.forbidden_carryover ?? []),
|
||||
...(status === "blocked" ? ["blocked_as_confirmed_factual_answer"] : []),
|
||||
...(effectiveCarryoverDepth === "none" ? ["implicit_followup_reuse"] : [])
|
||||
]);
|
||||
|
||||
return {
|
||||
schema_version: ASSISTANT_STATE_TRANSITION_RUNTIME_SCHEMA_VERSION,
|
||||
state_owner: "assistantStateTransitionRuntimeAdapter",
|
||||
transition_id: transitionId,
|
||||
transition_title: transition?.title ?? shadow.transition_contract_title,
|
||||
application_status: status,
|
||||
declared_carryover_depth: transition?.allowed_carryover_depth ?? null,
|
||||
truth_gate_carryover_depth: truthPolicy.truth_gate.carryover_eligibility,
|
||||
effective_carryover_depth: effectiveCarryoverDepth,
|
||||
required_prior_state: transition?.required_prior_state ?? [],
|
||||
expected_answer_mode: transition?.expected_answer_mode ?? null,
|
||||
state_mutations: transition?.state_mutations ?? [],
|
||||
forbidden_carryover: forbiddenCarryover,
|
||||
state_actions: stateActionsFor(transitionId),
|
||||
coverage_gate_state: {
|
||||
coverage_status: truthPolicy.truth_gate.coverage_status,
|
||||
evidence_grade: truthPolicy.truth_gate.evidence_grade,
|
||||
grounding_status: truthPolicy.truth_gate.grounding_status,
|
||||
truth_mode: truthPolicy.truth_gate.truth_mode,
|
||||
carryover_eligibility: truthPolicy.truth_gate.carryover_eligibility,
|
||||
reason_codes: truthPolicy.truth_gate.reason_codes
|
||||
},
|
||||
reason_codes: reasonCodesFor({
|
||||
shadow,
|
||||
truthPolicy,
|
||||
transitionId,
|
||||
applicationStatus: status
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
export function buildAssistantStateTransitionRuntimeFields(
|
||||
input: ResolveAssistantStateTransitionRuntimeInput
|
||||
): AssistantStateTransitionRuntimeFields {
|
||||
const transition = resolveAssistantStateTransitionRuntime(input);
|
||||
return {
|
||||
assistant_state_transition_v1: transition,
|
||||
state_transition_contract: transition,
|
||||
state_transition_id: transition.transition_id,
|
||||
state_transition_status: transition.application_status,
|
||||
effective_carryover_depth: transition.effective_carryover_depth
|
||||
};
|
||||
}
|
||||
|
||||
export function attachAssistantStateTransition<T extends Record<string, unknown>>(
|
||||
debugPayload: T,
|
||||
input: Omit<ResolveAssistantStateTransitionRuntimeInput, "addressDebug">
|
||||
): T & AssistantStateTransitionRuntimeFields {
|
||||
return {
|
||||
...debugPayload,
|
||||
...buildAssistantStateTransitionRuntimeFields({
|
||||
...input,
|
||||
addressDebug: debugPayload
|
||||
})
|
||||
};
|
||||
}
|
||||
@@ -20,6 +20,8 @@ import type { AddressNavigationState } from "./addressNavigation";
|
||||
import type {
|
||||
AssistantAnswerShapeKind,
|
||||
AssistantRuntimeContractShadowDecision,
|
||||
AssistantStateTransitionApplicationStatus,
|
||||
AssistantStateTransitionRuntimeContract,
|
||||
AssistantTruthAnswerPolicyRuntimeContract,
|
||||
AssistantTruthMode,
|
||||
AssistantTransitionClassId
|
||||
@@ -463,6 +465,11 @@ export interface AssistantDebugPayload {
|
||||
truth_mode?: AssistantTruthMode;
|
||||
carryover_eligibility?: AssistantTruthAnswerPolicyRuntimeContract["truth_gate"]["carryover_eligibility"];
|
||||
answer_shape?: AssistantAnswerShapeKind;
|
||||
assistant_state_transition_v1?: AssistantStateTransitionRuntimeContract;
|
||||
state_transition_contract?: AssistantStateTransitionRuntimeContract;
|
||||
state_transition_id?: AssistantTransitionClassId | null;
|
||||
state_transition_status?: AssistantStateTransitionApplicationStatus;
|
||||
effective_carryover_depth?: AssistantStateTransitionRuntimeContract["effective_carryover_depth"];
|
||||
execution_lane?: "address_query" | "deep_analysis";
|
||||
llm_decomposition_applied?: boolean;
|
||||
llm_decomposition_attempted?: boolean;
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { AddressIntent } from "./addressQuery";
|
||||
|
||||
export const ASSISTANT_RUNTIME_CONTRACTS_SCHEMA_VERSION = "assistant_runtime_contracts_v1" as const;
|
||||
export const ASSISTANT_TRUTH_ANSWER_POLICY_RUNTIME_SCHEMA_VERSION = "assistant_truth_answer_policy_runtime_v1" as const;
|
||||
export const ASSISTANT_STATE_TRANSITION_RUNTIME_SCHEMA_VERSION = "assistant_state_transition_runtime_v1" as const;
|
||||
|
||||
export type AssistantLivingMode = "address_data" | "assistant_data_scope" | "chat" | "meta_followup" | "clarification";
|
||||
export type AssistantFrameStatus = "active" | "suspended" | "closed" | "blocked";
|
||||
@@ -36,6 +37,8 @@ export type AssistantAnswerShapeKind =
|
||||
| "blocked_no_answer"
|
||||
| "unknown";
|
||||
export type AssistantAnswerShapeReplyType = "factual" | "partial_coverage" | "deep_analysis" | "unknown";
|
||||
export type AssistantStateTransitionApplicationStatus = "applied" | "clarification_required" | "blocked" | "unresolved";
|
||||
export type AssistantStateFrameAction = "create" | "update" | "preserve" | "reuse" | "clear" | "block" | "none";
|
||||
|
||||
export interface AssistantDateScopeState {
|
||||
as_of_date: string | null;
|
||||
@@ -115,6 +118,32 @@ export interface AssistantTruthAnswerPolicyRuntimeContract {
|
||||
answer_shape: AssistantAnswerShapeRuntimeContract;
|
||||
}
|
||||
|
||||
export interface AssistantStateTransitionRuntimeContract {
|
||||
schema_version: typeof ASSISTANT_STATE_TRANSITION_RUNTIME_SCHEMA_VERSION;
|
||||
state_owner: "assistantStateTransitionRuntimeAdapter";
|
||||
transition_id: AssistantTransitionClassId | null;
|
||||
transition_title: string | null;
|
||||
application_status: AssistantStateTransitionApplicationStatus;
|
||||
declared_carryover_depth: AssistantCarryoverDepth | null;
|
||||
truth_gate_carryover_depth: AssistantCarryoverDepth;
|
||||
effective_carryover_depth: AssistantCarryoverDepth;
|
||||
required_prior_state: AssistantStateSlice[];
|
||||
expected_answer_mode: AssistantAnswerMode | null;
|
||||
state_mutations: string[];
|
||||
forbidden_carryover: string[];
|
||||
state_actions: {
|
||||
living_mode_state: AssistantStateFrameAction;
|
||||
root_frame_state: AssistantStateFrameAction;
|
||||
selected_object_frame_state: AssistantStateFrameAction;
|
||||
meta_frame_state: AssistantStateFrameAction;
|
||||
clarification_state: AssistantStateFrameAction;
|
||||
coverage_gate_state: AssistantStateFrameAction;
|
||||
answer_context_state: AssistantStateFrameAction;
|
||||
};
|
||||
coverage_gate_state: AssistantCoverageGateState;
|
||||
reason_codes: string[];
|
||||
}
|
||||
|
||||
export interface AssistantSessionAggregateState {
|
||||
schema_version: typeof ASSISTANT_RUNTIME_CONTRACTS_SCHEMA_VERSION;
|
||||
living_mode_state: {
|
||||
|
||||
Reference in New Issue
Block a user