Skip to main content

qualia_client_core/api/
inference_policy.rs

1//! Privileged native configuration commands. Deliberately not exposed as an
2//! unauthenticated HTTP/PoetHost/MCP signing endpoint. The embedding native UI
3//! must authorize the human command, as it does other identity/profile changes.
4use qualia_core_db::llm_agent::governed::POLICY_COLD_BUDGET_BYTES;
5
6fn now() -> Result<u64, String> {
7    std::time::SystemTime::now()
8        .duration_since(std::time::UNIX_EPOCH)
9        .map(|d| d.as_secs())
10        .map_err(|_| "Host clock precedes Unix epoch".into())
11}
12fn status(value: qualia_core_db::llm_agent::governed::PolicyStatus) -> serde_json::Value {
13    serde_json::json!({"installed":value.installed,"generation":value.generation,
14        "serial":value.serial,"policy_digest":value.digest})
15}
16/// Deliberate policy installation; absence never selects permissive defaults.
17pub fn install_inference_policy(
18    rules_json: String,
19    expires_at: u64,
20    expected_generation: u64,
21) -> Result<serde_json::Value, String> {
22    let now = now()?;
23    crate::inference_policy::install_policy(
24        &rules_json,
25        now,
26        expires_at,
27        now,
28        expected_generation,
29        POLICY_COLD_BUDGET_BYTES,
30    )
31    .map(status)
32}
33pub fn get_inference_policy_status() -> Result<serde_json::Value, String> {
34    crate::inference_policy::load_policy_status().map(status)
35}
36pub fn install_native_inference_policy(
37    rules_json: String,
38    observations: qualia_core_db::llm_agent::governed::evidence::ObservationRequirements,
39    expires_at: u64,
40    expected_generation: u64,
41) -> Result<serde_json::Value, String> {
42    crate::inference_policy::install_evidence_policy(
43        &rules_json,
44        observations,
45        now()?,
46        expires_at,
47        expected_generation,
48        POLICY_COLD_BUDGET_BYTES,
49    )
50    .map(status)
51}
52
53/// Local host command connecting an already logged-in actor to PoET's existing
54/// token-authenticated HTTP transport. No signing API is exposed remotely.
55pub fn bind_poet_inference_actor(
56    actor: &crate::inference_policy::InferenceActorSession,
57    preparation: qualia_core_db::llm_agent::native_request::NativePreparationSpec,
58) -> Result<(), String> {
59    crate::inference_policy::bind_poet_inference_session(actor, preparation)
60}
61
62/// Explicit delegation to the native MCP service; never remotely callable.
63pub fn bind_mcp_inference_actor(
64    actor: &crate::inference_policy::InferenceActorSession,
65    preparation: qualia_core_db::llm_agent::native_request::NativePreparationSpec,
66) -> Result<(), String> {
67    crate::inference_policy::bind_mcp_inference_session(actor, preparation)
68}
69
70/// Obtain the retained context consumed by an embedding's native session API.
71/// In particular the CLI must inject it after loading its selected model.
72pub fn get_native_inference_session(
73    actor: &crate::inference_policy::InferenceActorSession,
74    preparation: qualia_core_db::llm_agent::native_request::NativePreparationSpec,
75) -> Result<std::sync::Arc<qualia_core_db::llm_agent::native_request::NativeProductSession>, String> {
76    crate::inference_policy::native_product_session(actor, preparation)
77}
78pub fn revoke_inference_policy(expected_generation: u64) -> Result<serde_json::Value, String> {
79    crate::inference_policy::revoke_policy(expected_generation, POLICY_COLD_BUDGET_BYTES)
80        .map(status)
81}
82
83/// Native operator command: choose project trust and actual signed admission.
84/// These typed inputs are not accepted from chat, tools, HTTP, or remote signers.
85pub fn configure_inference_host(
86    configuration: crate::inference_policy::InferenceHostConfiguration,
87    admission: qualia_core_db::poet_host::native_authority::SignedAdmission,
88    document: qualia_cooperative_core::authoring::DocumentRevision,
89) -> Result<(), String> {
90    crate::inference_policy::configure_inference_authority(
91        configuration,
92        admission,
93        document,
94        crate::inference_policy::CONFIGURATION_BUDGET_BYTES,
95    )
96}
97/// Explicit local key creation, separate from enrollment and policy installation.
98pub fn create_inference_actor_key(label: String) -> Result<[u8; 32], String> {
99    crate::inference_policy::create_inference_actor_signer(
100        &label,
101        crate::inference_policy::CONFIGURATION_BUDGET_BYTES,
102    )
103}
104pub fn login_inference_actor(
105    subject: String,
106    selected: crate::inference_policy::SelectedInferenceIdentifier,
107    signer_label: String,
108    lifetime_seconds: u64,
109) -> Result<crate::inference_policy::InferenceActorSession, String> {
110    crate::inference_policy::login_inference_actor(
111        &subject,
112        selected,
113        &signer_label,
114        lifetime_seconds,
115        crate::inference_policy::CONFIGURATION_BUDGET_BYTES,
116    )
117}
118/// Login and connect that authenticated actor to a specific local chat. The
119/// native embedding supplies retained source leases and explicit input budgets.
120pub fn login_chat_inference_actor(
121    chat_id: String,
122    subject: String,
123    selected: crate::inference_policy::SelectedInferenceIdentifier,
124    signer_label: String,
125    lifetime_seconds: u64,
126    preparation: qualia_core_db::llm_agent::native_request::NativePreparationSpec,
127) -> Result<crate::inference_policy::InferenceActorSession, String> {
128    let actor = login_inference_actor(subject, selected, signer_label, lifetime_seconds)?;
129    if let Err(error) =
130        crate::inference_policy::bind_chat_inference_session(&chat_id, &actor, preparation)
131    {
132        let _ = actor.logout();
133        return Err(error);
134    }
135    Ok(actor)
136}
137
138pub fn get_inference_actor_scope(
139    actor: &crate::inference_policy::InferenceActorSession,
140) -> Result<qualia_core_db::llm_agent::governed::ActorPolicyScope, String> {
141    actor.policy_scope()
142}
143pub fn logout_inference_actor(
144    actor: &crate::inference_policy::InferenceActorSession,
145) -> Result<(), String> {
146    actor.logout()
147}
148pub fn disconnect_inference_controller() {
149    crate::inference_policy::disconnect_inference_controller();
150}
151pub fn lock_inference_controller_vault() -> Result<(), String> {
152    crate::inference_policy::lock_inference_controller_vault()
153}
154pub fn replace_inference_actor_admission(
155    admission: qualia_core_db::poet_host::native_authority::SignedAdmission,
156) -> Result<(), String> {
157    crate::inference_policy::replace_inference_admission(
158        admission,
159        crate::inference_policy::CONFIGURATION_BUDGET_BYTES,
160    )
161}