NODEDC_1C/llm_normalizer/backend/tests/assistantTruthAnswerPolicyR...

181 lines
7.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
attachAssistantTruthAnswerPolicy,
resolveAssistantTruthAnswerPolicyRuntime
} from "../src/services/assistantTruthAnswerPolicyRuntimeAdapter";
describe("assistant truth answer policy runtime adapter", () => {
it("emits confirmed truth gate and factual answer shape for grounded exact results", () => {
const policy = resolveAssistantTruthAnswerPolicyRuntime({
addressDebug: {
capability_id: "confirmed_inventory_on_hand_as_of_date",
detected_intent: "inventory_on_hand_as_of_date",
rows_matched: 3,
route_expectation_status: "matched",
evidence_strength: "strong"
},
groundingStatus: "grounded",
replyType: "factual"
});
expect(policy.truth_gate).toEqual(
expect.objectContaining({
schema_version: "assistant_truth_answer_policy_runtime_v1",
policy_owner: "assistantTruthAnswerPolicyRuntimeAdapter",
coverage_status: "full",
grounding_status: "grounded",
truth_mode: "confirmed",
evidence_grade: "strong",
source_truth_gate_status: "full_confirmed",
carryover_eligibility: "root_only",
blocked_or_limited_explanation: null
})
);
expect(policy.answer_shape).toEqual(
expect.objectContaining({
answer_shape: "confirmed_factual",
reply_type: "factual",
capability_contract_id: "confirmed_inventory_on_hand_as_of_date",
transition_contract_id: "T1",
may_state_confirmed_facts: true,
must_include_limitation: false,
may_power_followup: true,
required_sections: ["direct_answer", "evidence_basis"],
downgrade_only: true
})
);
});
it("keeps temporal/contextual limits as limited answer shape with explicit reason codes", () => {
const policy = resolveAssistantTruthAnswerPolicyRuntime({
addressDebug: {
capability_id: "confirmed_inventory_on_hand_as_of_date",
temporal_guard_outcome: "ambiguous_limited",
rows_matched: 2,
limitations: ["period_window_auto_broadened_to_available_data"]
},
groundingStatus: "partial",
replyType: "partial_coverage"
});
expect(policy.truth_gate.coverage_status).toBe("partial");
expect(policy.truth_gate.truth_mode).toBe("limited");
expect(policy.truth_gate.source_truth_gate_status).toBe("limited_temporal_or_contextual");
expect(policy.truth_gate.blocked_or_limited_explanation).toBe("temporal_or_contextual_limit");
expect(policy.truth_gate.reason_codes).toContain("period_window_auto_broadened_to_available_data");
expect(policy.answer_shape.answer_shape).toBe("limited_with_reason");
expect(policy.answer_shape.must_include_limitation).toBe(true);
expect(policy.answer_shape.required_sections).toEqual(["direct_answer", "evidence_window", "limitations"]);
});
it("blocks route expectation failures and prevents follow-up carryover", () => {
const policy = resolveAssistantTruthAnswerPolicyRuntime({
addressDebug: {
capability_id: "confirmed_inventory_on_hand_as_of_date",
route_expectation_status: "mismatch",
route_expectation_reason: "expected_confirmed_balance_route"
},
groundingStatus: "route_mismatch_blocked",
replyType: "partial_coverage"
});
expect(policy.truth_gate.coverage_status).toBe("blocked");
expect(policy.truth_gate.grounding_status).toBe("route_mismatch_blocked");
expect(policy.truth_gate.truth_mode).toBe("unsupported");
expect(policy.truth_gate.carryover_eligibility).toBe("none");
expect(policy.truth_gate.reason_codes).toContain("expected_confirmed_balance_route");
expect(policy.answer_shape.answer_shape).toBe("blocked_no_answer");
expect(policy.answer_shape.may_state_confirmed_facts).toBe(false);
expect(policy.answer_shape.may_power_followup).toBe(false);
});
it("honors explicit exact-lane truth gate contracts for factual negative answers", () => {
const policy = resolveAssistantTruthAnswerPolicyRuntime({
addressDebug: {
capability_id: "inventory_counterparty_item_flow",
rows_matched: 0,
address_coverage_evidence_v1: {
schema_version: "address_coverage_evidence_v1",
policy_owner: "addressCoverageEvidencePolicy",
requested_result_mode: "confirmed_balance",
result_mode: "confirmed_balance",
evidence_strength: "medium",
balance_confirmed: true,
as_of_date_basis: "period_end",
coverage_status: "full",
evidence_basis: "exact_negative",
reason_codes: ["evidence_basis_exact_negative"]
},
address_truth_gate_v1: {
schema_version: "address_truth_gate_v1",
policy_owner: "addressTruthGatePolicy",
truth_gate_status: "full_confirmed",
carryover_eligibility: "root_only",
limited_reason_category: "empty_match",
runtime_readiness: "LIVE_QUERYABLE_WITH_LIMITS",
reason_codes: ["counterparty_item_flow_exact_negative_response"],
blocked_or_limited_explanation: null
}
},
replyType: "factual"
});
expect(policy.truth_gate.coverage_status).toBe("full");
expect(policy.truth_gate.grounding_status).toBe("grounded");
expect(policy.truth_gate.truth_mode).toBe("confirmed");
expect(policy.truth_gate.evidence_grade).toBe("medium");
expect(policy.truth_gate.carryover_eligibility).toBe("root_only");
expect(policy.truth_gate.reason_codes).toContain("counterparty_item_flow_exact_negative_response");
expect(policy.truth_gate.reason_codes).toContain("evidence_basis_exact_negative");
expect(policy.answer_shape.answer_shape).toBe("confirmed_factual");
expect(policy.answer_shape.may_power_followup).toBe(true);
});
it("keeps explicit temporal-limited factual answers limited in the truth contract", () => {
const policy = resolveAssistantTruthAnswerPolicyRuntime({
addressDebug: {
capability_id: "inventory_inventory_purchase_provenance_for_item",
rows_matched: 2,
address_truth_gate_v1: {
schema_version: "address_truth_gate_v1",
policy_owner: "addressTruthGatePolicy",
truth_gate_status: "limited_temporal_or_contextual",
carryover_eligibility: "object_only",
limited_reason_category: null,
runtime_readiness: "LIVE_QUERYABLE_WITH_LIMITS",
reason_codes: ["period_window_auto_broadened_to_available_data"],
blocked_or_limited_explanation: "temporal_or_contextual_limit"
}
},
replyType: "factual"
});
expect(policy.truth_gate.coverage_status).toBe("partial");
expect(policy.truth_gate.truth_mode).toBe("limited");
expect(policy.truth_gate.carryover_eligibility).toBe("object_only");
expect(policy.truth_gate.blocked_or_limited_explanation).toBe("temporal_or_contextual_limit");
expect(policy.answer_shape.answer_shape).toBe("limited_with_reason");
expect(policy.answer_shape.must_include_limitation).toBe(true);
});
it("attaches top-level debug fields without hiding the nested contract", () => {
const debug = attachAssistantTruthAnswerPolicy(
{
capability_id: "inventory_inventory_purchase_provenance_for_item",
missing_required_filters: ["item"],
limited_reason_category: "missing_anchor"
},
{
replyType: "partial_coverage"
}
);
expect(debug.assistant_truth_answer_policy_v1.schema_version).toBe("assistant_truth_answer_policy_runtime_v1");
expect(debug.coverage_gate_contract.truth_mode).toBe("clarification_required");
expect(debug.answer_shape_contract.answer_shape).toBe("clarification_required");
expect(debug.truth_mode).toBe("clarification_required");
expect(debug.carryover_eligibility).toBe("none");
expect(debug.answer_shape).toBe("clarification_required");
});
});