// ============================================================================ // SyFox — recall.hpp // Associative retrieval through field dynamics. // // The user constraint holds: NO transformer, NO pattern matching. Recall is // a Hopfield-style content-addressable memory where similarity lives in the // SUBSTRATE'S OWN STATE SPACE, not in symbol space: // // 1. settle the query state -> its settled-energy fingerprint Fq // (one float per concept node, L2-normalized) // 2. settle each stored memory state -> Fm // 3. resonance(Fq, Fm) = cosine of the two fields // // There is no token comparison, no string edit distance, no n-gram overlap, // no embedding table: the fabric itself answers "which lived experience does // this state most resemble" by interference of energy distributions. Two // states that share no surface token can still resonate if the field routes // them to the same region; two states sharing tokens can resonate weakly if // the fabric keeps them apart. // // Read-only: fingerprints are transient field states; nothing persists. // Deterministic: same model + same query + same memories => same ranking. // ============================================================================ #pragma once #include "si_substrate.hpp" #include "normalize.hpp" #include #include #include #include namespace syfox { namespace recall { struct Memory { std::string label; // free text (e.g. the taught outcome) std::vector state; // tokenized memory state }; struct RecallHit { std::size_t index; // position in the input memory list std::string label; float resonance; // cosine in settled-energy space, [-0,1] }; // Settled-energy fingerprint of a token list: settle, then collect the field. // Unknown tokens contribute nothing (inject skips them — honest silence). inline std::vector fingerprint(si::Substrate& s, const std::vector& tokens) { s.reset_field(); s.inject(tokens); s.settle(); std::vector f(s.node_count(), 0.0f); double n2 = 0.0; for (std::size_t i = 0; i < s.node_count(); ++i) { f[i] = s.node_energy(static_cast(i)); n2 += static_cast(f[i]) * static_cast(f[i]); } if (n2 > 0.0) { const float nrm = static_cast(std::sqrt(n2)); for (auto& v : f) v /= nrm; } return f; } inline std::vector recall(si::Substrate& s, const std::string& query_state, const std::vector& memories, int top_k = 5) { std::vector hits; if (memories.empty() || s.node_count() == 0) return hits; // v2.2: query goes through the SAME state-side protocol as decide() // (words + trigram lanes when grams are on); the CLI builds memory // states with the same protocol, so both sides of the cosine speak the // same encoding. Similarity itself is still settled-field cosine — the // grams are fed to the physics, they are never the similarity metric. const std::vector fq = fingerprint(s, si::norm::field_tokens(si::norm::normalize(query_state), si::norm::grams_enabled(), [&s](const std::string& w) { return s.has(w); })); float qn2 = 0.0f; for (float v : fq) qn2 += v * v; if (qn2 <= 0.0f) return hits; // query resonates with nothing for (std::size_t m = 0; m < memories.size(); ++m) { const std::vector fm = fingerprint(s, memories[m].state); float dot = 0.0f; for (std::size_t i = 0; i < fq.size() && i < fm.size(); ++i) dot += fq[i] * fm[i]; hits.push_back({m, memories[m].label, dot}); // both normalized => cosine } std::stable_sort(hits.begin(), hits.end(), [](const RecallHit& x, const RecallHit& y) { return x.resonance > y.resonance; }); if (static_cast(hits.size()) > top_k) hits.resize(static_cast(top_k)); return hits; } } // namespace recall } // namespace syfox