formal-ai 0.177.0

Formal symbolic AI implementation with OpenAI-compatible APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Unknown-prompt reasoning path.
//!
//! This module handles the point where every known rule and specialized
//! handler has declined a prompt. It still records a bounded reasoning trace,
//! tries reachable link-memory and public-cache sources, and only then returns
//! an unknown answer.

use std::sync::OnceLock;

use crate::concepts::{lookup_concept_query, ConceptQuery};
use crate::engine::{stable_id, SymbolicAnswer};
use crate::event_log::EventLog;
use crate::language::Language;
use crate::seed::{self, ConceptRecord};
use crate::solver_handlers::finalize_simple;
use crate::solver_helpers::humanize_url;
use crate::unknown_opener::language_aware_unknown_answer;

#[derive(Debug, Clone, Copy)]
pub struct UnknownReasoningConfig {
    pub questioning_rigor: f32,
    pub offline: bool,
}

pub fn answer_unknown_prompt(
    prompt: &str,
    language: Language,
    log: &mut EventLog,
    config: UnknownReasoningConfig,
) -> SymbolicAnswer {
    let focus = infer_missing_focus(prompt);
    record_initial_unknown_trace(prompt, language, log, focus.as_deref(), config);

    log.append("reasoning:candidate_source", "link_memory".to_owned());
    log.append(
        "reasoning:gather_attempt",
        focus.as_deref().map_or_else(
            || "link_memory:no_focus".to_owned(),
            |value| format!("link_memory:{value}"),
        ),
    );
    if let Some(focus) = focus.as_deref() {
        if let Some(body) = answer_from_link_memory(prompt, focus, log) {
            return finalize_simple(
                prompt,
                log,
                "memory_fact_lookup",
                "response:memory_fact_lookup",
                &body,
                0.85,
            );
        }
    }
    log.append("reasoning:gather_result", "link_memory:miss".to_owned());

    log.append(
        "reasoning:candidate_source",
        "public_knowledge_cache".to_owned(),
    );
    log.append(
        "reasoning:gather_attempt",
        focus.as_deref().map_or_else(
            || "public_knowledge_cache:no_focus".to_owned(),
            |value| format!("public_knowledge_cache:{value}"),
        ),
    );
    if let Some(focus) = focus.as_deref() {
        if let Some(answer) = answer_from_public_knowledge_cache(prompt, focus, language, log) {
            return answer;
        }
    }
    log.append(
        "reasoning:gather_result",
        "public_knowledge_cache:miss".to_owned(),
    );

    log.append(
        "reasoning:candidate_source",
        if config.offline {
            "allowed_external_api:skipped_offline"
        } else {
            "allowed_external_api:no_verified_value"
        }
        .to_owned(),
    );

    if let Some(focus) = focus {
        return answer_unresolved_unknown(prompt, language, log, &focus, config);
    }

    answer_with_legacy_fallback(prompt, language, log)
}

fn record_initial_unknown_trace(
    prompt: &str,
    language: Language,
    log: &mut EventLog,
    focus: Option<&str>,
    config: UnknownReasoningConfig,
) {
    let prompt_state = if prompt.trim().is_empty() {
        "empty_prompt"
    } else {
        "unmatched_prompt"
    };
    log.append(
        "reasoning:known",
        format!(
            "language={} local_search=complete prompt_state={} questioning_rigor={:.2}",
            language.slug(),
            prompt_state,
            config.questioning_rigor.clamp(0.0, 1.0),
        ),
    );
    log.append(
        "reasoning:unknown",
        focus.map_or_else(
            || "missing_focus:no_extractable_terms".to_owned(),
            |value| format!("missing_answer_for:{value}"),
        ),
    );
}

fn answer_unresolved_unknown(
    prompt: &str,
    language: Language,
    log: &mut EventLog,
    focus: &str,
    config: UnknownReasoningConfig,
) -> SymbolicAnswer {
    let body = render_unresolved_unknown(language, focus, config.questioning_rigor);
    finalize_simple(
        prompt,
        log,
        "unknown",
        "response:unknown_reasoning",
        &body,
        0.0,
    )
}

fn answer_with_legacy_fallback(
    prompt: &str,
    language: Language,
    log: &mut EventLog,
) -> SymbolicAnswer {
    let completed_steps = log
        .events()
        .iter()
        .filter(|event| event.kind.starts_with("reasoning:"))
        .count();
    log.append(
        "reasoning:gave_up",
        format!(
            "gave up after {} reasoning steps; legacy_unknown_fallback",
            completed_steps + 1
        ),
    );
    let body = language_aware_unknown_answer(prompt, language);
    finalize_simple(prompt, log, "unknown", "response:unknown", &body, 0.0)
}

fn answer_from_link_memory(prompt: &str, focus: &str, log: &mut EventLog) -> Option<String> {
    let subject = extract_question_subject(prompt).unwrap_or_else(|| focus.to_owned());
    let normalized_subject = normalize_fact_subject(&subject);
    if normalized_subject.is_empty() {
        return None;
    }

    for event in log.events() {
        if !matches!(event.kind, "prior_turn:user" | "prior_turn:assistant") {
            continue;
        }
        if let Some((stored_subject, stored_value)) = extract_memory_fact(&event.payload) {
            let stored_normalized = normalize_fact_subject(&stored_subject);
            if stored_normalized == normalized_subject {
                log.append(
                    "reasoning:gather_result",
                    format!("link_memory:hit:{stored_normalized}"),
                );
                log.append(
                    "cache_hit",
                    format!(
                        "link_memory:{}",
                        stable_id(
                            "memory_fact",
                            &format!("{stored_normalized}={stored_value}")
                        )
                    ),
                );
                return Some(format!(
                    "From link memory: {} is {}.",
                    stored_subject.trim(),
                    stored_value.trim()
                ));
            }
        }
    }
    None
}

fn answer_from_public_knowledge_cache(
    prompt: &str,
    focus: &str,
    language: Language,
    log: &mut EventLog,
) -> Option<SymbolicAnswer> {
    for term in public_concept_candidate_terms(focus) {
        log.append(
            "reasoning:gather_attempt",
            format!("public_knowledge_cache:concept:{term}"),
        );
        let query = ConceptQuery {
            term: term.to_lowercase(),
            context: None,
        };
        let Some(lookup) = lookup_concept_query(&query) else {
            continue;
        };
        let record = lookup.record;
        log.append(
            "reasoning:gather_result",
            format!("public_knowledge_cache:hit:{}", record.slug),
        );
        log.append("concept_lookup:request", term);
        log.append("concept_lookup:hit", record.slug.clone());
        if !record.wikidata.is_empty() {
            log.append("wikidata", record.wikidata.clone());
        }
        let source = concept_source(record, language);
        if !source.is_empty() {
            log.append("source", source.to_owned());
        }
        let body = render_concept_plain(record, language);
        return Some(finalize_simple(
            prompt,
            log,
            "concept_lookup",
            "response:concept_lookup",
            &body,
            0.75,
        ));
    }
    None
}

fn public_concepts() -> &'static [ConceptRecord] {
    static CELL: OnceLock<Vec<ConceptRecord>> = OnceLock::new();
    CELL.get_or_init(seed::concepts).as_slice()
}

fn public_concept_candidate_terms(focus: &str) -> Vec<String> {
    let focus_normalized = normalize_search_surface(focus);
    let mut scored = Vec::new();
    for record in public_concepts() {
        for candidate in concept_candidate_surfaces(record) {
            let normalized = normalize_search_surface(&candidate);
            if normalized.len() < 3 {
                continue;
            }
            let matches = focus_normalized.contains(&normalized) || normalized == focus_normalized;
            if matches {
                scored.push((normalized.len(), candidate));
            }
        }
    }
    scored.sort_by(|left, right| right.0.cmp(&left.0).then_with(|| left.1.cmp(&right.1)));
    let mut out: Vec<String> = Vec::new();
    for (_score, candidate) in scored {
        let normalized = normalize_search_surface(&candidate);
        if !out
            .iter()
            .any(|existing| normalize_search_surface(existing) == normalized)
        {
            out.push(candidate);
        }
    }
    out
}

fn concept_candidate_surfaces(record: &ConceptRecord) -> Vec<String> {
    let mut out = vec![record.term.clone(), record.slug.replace("concept_", "")];
    out.extend(record.aliases.iter().cloned());
    for localized in &record.localized {
        if !localized.term.is_empty() {
            out.push(localized.term.clone());
        }
        out.extend(localized.aliases.iter().cloned());
    }
    out
}

fn render_concept_plain(record: &ConceptRecord, language: Language) -> String {
    let localized = record.localized_for(language.slug());
    let term = localized
        .map(|loc| loc.term.as_str())
        .filter(|value| !value.is_empty())
        .unwrap_or(record.term.as_str());
    let summary = localized
        .map(|loc| loc.summary.as_str())
        .filter(|value| !value.is_empty())
        .unwrap_or(record.summary.as_str());
    let source = concept_source(record, language);
    let source_kind = localized
        .map(|loc| loc.source_kind.as_str())
        .filter(|value| !value.is_empty())
        .unwrap_or(record.source_kind.as_str());
    format!(
        "{term} ({category}): {summary}\n\nSource: {source} ({source_kind}).",
        category = record.category,
        source = render_source_link(source),
    )
}

fn concept_source(record: &ConceptRecord, language: Language) -> &str {
    record
        .localized_for(language.slug())
        .map(|loc| loc.source.as_str())
        .filter(|value| !value.is_empty())
        .unwrap_or(record.source.as_str())
}

fn render_source_link(source: &str) -> String {
    let human = humanize_url(source);
    if human == source {
        source.to_owned()
    } else {
        format!("[{human}]({source})")
    }
}

fn render_unresolved_unknown(language: Language, focus: &str, questioning_rigor: f32) -> String {
    let focus = focus.trim();
    let base = match language {
        Language::Russian => format!(
            "Я не смог определить `{focus}` по локальной памяти Links Notation, \
             кэшу публичных знаний или доступным источникам."
        ),
        Language::Hindi => format!(
            "मैं `{focus}` को local Links Notation memory, public-knowledge cache, \
             या उपलब्ध sources से निर्धारित नहीं कर सका."
        ),
        Language::Chinese => {
            format!("我无法从本地 Links Notation 记忆、公共知识缓存或可用来源确定 `{focus}`。")
        }
        Language::Unknown => format!(
            "I detected an unsupported language and am falling back to English. I could not \
             determine `{focus}` from local Links Notation memory, cached public knowledge, \
             or the source cache, and cannot infer a verified answer."
        ),
        Language::English => format!(
            "I could not determine `{focus}` from local Links Notation memory, cached public \
             knowledge, or the source cache, and cannot infer a verified answer."
        ),
    };
    let hint = unknown_extension_hint(language);
    if questioning_rigor >= 0.5 {
        let question = match language {
            Language::Russian => "Какой один источник или факт нужно использовать?",
            Language::Hindi => "कौन सा एक source या missing fact इस्तेमाल करूँ?",
            Language::Chinese => "应该使用哪一个来源或缺失事实?",
            _ => "Which one source or missing fact should I use?",
        };
        format!("{base} {question}\n\n{hint}")
    } else {
        format!("{base} I recorded the failed gather attempts in the trace.\n\n{hint}")
    }
}

const fn unknown_extension_hint(language: Language) -> &'static str {
    match language {
        Language::Russian => {
            "Если после reasoning всё ещё нужен общий факт или правило Links Notation, \
             используйте Report issue с trace. Для этого диалога можно экспортировать память \
             или обучить правило: `List behavior rules`, `Show behavior rule unknown`, \
             `When I say ... answer ...`."
        }
        Language::Hindi => {
            "अगर reasoning के बाद भी shared Links Notation seed fact या rule चाहिए, तो trace \
             के साथ Report issue उपयोग करें. इस dialog को टिकाऊ बनाने के लिए memory export \
             करें या `When I say ... answer ...` सिखाएँ; routes देखने के लिए `List behavior \
             rules` और `Show behavior rule unknown`."
        }
        Language::Chinese => {
            "如果 reasoning 之后仍需要共享的 Links Notation seed 事实或规则,请带上 trace 使用 \
             Report issue。要让当前对话可持久化,可以 export memory,或用 `When I say ... \
             answer ...` 教一条规则;也可查看 `List behavior rules` 和 `Show behavior rule \
             unknown`。"
        }
        _ => {
            "If reasoning still cannot resolve this and a shared Links Notation seed fact or \
             rule is needed, use Report issue with the trace. To keep a dialog-local rule \
             durable, export memory or teach it with `When I say ... answer ...`; inspect routes \
             with `List behavior rules` and `Show behavior rule unknown`."
        }
    }
}

fn infer_missing_focus(prompt: &str) -> Option<String> {
    let trimmed = clean_focus(prompt);
    if trimmed.is_empty() {
        return None;
    }
    if let Some(about) = tail_after_marker(trimmed, " about ") {
        return Some(clean_focus(about).to_owned());
    }
    if let Some(subject) = tail_after_marker(trimmed, " definitions of ") {
        return Some(clean_focus(subject).to_owned());
    }
    if let Some(subject) = tail_after_marker(trimmed, " definition of ") {
        return Some(clean_focus(subject).to_owned());
    }
    if let Some(subject) = extract_question_subject(trimmed) {
        return Some(subject);
    }
    Some(trimmed.to_owned())
}

fn extract_question_subject(prompt: &str) -> Option<String> {
    let trimmed = clean_focus(prompt);
    let lower = trimmed.to_lowercase();
    for prefix in [
        "what is the ",
        "what's the ",
        "what is ",
        "what's ",
        "who is ",
        "who was ",
        "where is ",
        "how should ",
        "how do i ",
        "how can i ",
    ] {
        if let Some(rest) = lower.strip_prefix(prefix) {
            let start = trimmed.len() - rest.len();
            let body = clean_focus(&trimmed[start..]);
            if !body.is_empty() {
                return Some(body.to_owned());
            }
        }
    }
    None
}

fn tail_after_marker<'a>(value: &'a str, marker: &str) -> Option<&'a str> {
    let lower = value.to_lowercase();
    let index = lower.rfind(marker)?;
    Some(&value[index + marker.len()..])
}

fn extract_memory_fact(text: &str) -> Option<(String, String)> {
    for sentence in text.split(['.', '!', '?', '\n']) {
        let statement = sentence
            .trim()
            .strip_prefix("Remember that ")
            .or_else(|| sentence.trim().strip_prefix("remember that "))
            .unwrap_or_else(|| sentence.trim())
            .trim();
        if statement.is_empty() {
            continue;
        }
        if let Some((subject, value)) = split_fact_statement(statement) {
            return Some((subject, value));
        }
    }
    None
}

fn split_fact_statement(statement: &str) -> Option<(String, String)> {
    for separator in [" is ", " = ", ": "] {
        if let Some(index) = statement.find(separator) {
            let subject = statement[..index].trim();
            let value = statement[index + separator.len()..].trim();
            if !subject.is_empty() && !value.is_empty() {
                return Some((subject.to_owned(), value.to_owned()));
            }
        }
    }
    None
}

fn clean_focus(value: &str) -> &str {
    value
        .trim()
        .trim_matches(['"', '\'', '`', '', '', '', '', '«', '»'])
        .trim_end_matches(['?', '', '.', '!', ',', ';', ':'])
        .trim()
}

fn normalize_fact_subject(value: &str) -> String {
    let cleaned = clean_focus(value).to_lowercase();
    let stripped = cleaned
        .strip_prefix("the ")
        .or_else(|| cleaned.strip_prefix("a "))
        .or_else(|| cleaned.strip_prefix("an "))
        .unwrap_or(cleaned.as_str());
    normalize_search_surface(stripped)
}

fn normalize_search_surface(value: &str) -> String {
    clean_focus(value)
        .chars()
        .flat_map(char::to_lowercase)
        .filter(|character| character.is_alphanumeric() || character.is_whitespace())
        .collect::<String>()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}