Implementation Plan: Complete GnuCash Cognitive Accounting
Guiding principles
- Prefer a correct, testable simulated cognitive core that always builds, with optional real OpenCog/ggml backends behind clean adapters.
- Integrate into existing GnuCash engine lifecycle (book open, transaction commit, account tree changes) before UI polish.
- Treat docs/demos as secondary until the engine layer compiles, tests green, and produces auditable accounting value (validation, attention ranking, prediction bounds).
- Keep changes surgical inside
libgnucash/engine first; GUI/report work only after stable APIs.
Phase 0 — Make it build (foundation unblocker)
Goal: Clean compile of gnc-engine with cognitive sources on a stock Linux build (no OpenCog/ggml required).
Work
- Resolve all merge remnants in
gnc-cognitive-accounting.cpp:
- Choose one coherent
GncCognitiveAtomSpace layout (single struct with always-present maps + optional OpenCog handles pointer).
- Ensure
create_hierarchy_link, fund pools, truth values, and constructors exist on all build paths.
- Fix MOSES discover path so
strategy_atom is always defined before OpenCog/ASMOSES branches.
- Fix URE validity function: remove dead code after early
return and unify one algorithm path.
- Fix
gnc-cognitive-scheme.cpp:
- Single valid raw-string Scheme bootstrap.
- Remove orphaned Scheme fragments outside the string.
- Unify messaging types:
- One
GncCognitiveMessage (prefer the module-enum hub in comms, or rename accounting’s string-based message to GncCognitiveAtomMessage).
- Move C++-only APIs (
std::vector receive) out of extern "C" or wrap with opaque C handles.
- CMake hygiene:
- Link
GGML_LIBRARIES / include dirs when HAVE_GGML.
- Ensure config.h / compile defs propagate consistently to engine + tests.
- Add optional demo targets only if they build standalone without polluting core.
- Fix tests to match defined semantics (e.g. balanced tx → confidence in
[0.7, 1.0], not exact 1.0).
- Gate CI: replace
test-cognitive-accounting.sh presence checks with real ctest targets.
Exit criteria: ninja gnc-engine test-cognitive-accounting test-tensor-network succeeds without OpenCog/ggml.
Phase 1 — Solid simulated AtomSpace core
Goal: A deterministic in-process hypergraph that correctly mirrors Chart of Accounts and transactions.
Work
- Atom model
- Explicit atom records: handle, type, name, outgoing set, incoming set, SimpleTV (strength/confidence), attention (STI/LTI/VLTI/wage/rent).
- Link integrity (Evaluation/Inheritance/Member/Implication) with real outgoing handles, not name-only strings.
- Account mapping
gnc_account_to_atomspace: ConceptNode per account; InheritanceLink to type + parent; EvaluationLink for balance snapshots.
- Invalidate/rebuild on rename, reparent, delete (QOF events).
- Transaction mapping
- Execution/Evaluation structures for splits (account, amount, value, commodity).
- Double-entry as Implication/And patterns with TV derived from imbalance magnitude.
- Persistence boundary (design now, implement light)
- Session-scoped AtomSpace is OK initially.
- Cognitive account flags already use KVP — keep that; document non-persistence of STI/LTI unless later serialized.
Exit criteria: Unit tests for hierarchy links, idempotent account atoms, balanced vs imbalanced TVs, and atom graph walk APIs.
Phase 2 — Engine lifecycle integration
Goal: Cognitive layer runs automatically when users do normal accounting, not only from demos.
Work
- Init/shutdown hooks in engine/book lifecycle (
gnc_engine_init / book open-close), guarded by feature flag (e.g. GNC_FEATURE_COGNITIVE_ACCOUNTING or runtime pref).
- QOF event listeners:
- Transaction commit → PLN validate + ECAN update per affected account + optional adaptive learning.
- Account create/edit/destroy → AtomSpace sync.
- Public query API for GUI/reports:
- Last validation score for a transaction
- Attention ranking of accounts
- Predicted balance + uncertainty interval
- Threading policy: start single-threaded on commit path; defer heavy MOSES/clustering to idle/async worker later.
- Performance budget: O(splits) on commit; avoid full-book scans on every edit.
Exit criteria: Opening a book + posting transactions updates cognitive state; shutdown is leak-free under ASAN/tests.
Phase 3 — Real algorithms behind the OpenCog-shaped API
Complete each subsystem as useful accounting behavior, still simulated unless optional libs present.
3A. PLN validation & proofs
- Formalize TV combination (product/revision) and document formula.
- Double-entry: imbalance relative to magnitude → strength; split count, age, account reliability → confidence.
- N-entry: party/account diversity constraints.
- Trial balance proof: walk subtree, assert Σ debits = Σ credits; emit proof atom + structured report object (not just a handle).
- P&L proof: income − expense over period with commodity awareness.
- Multi-currency: convert via pricedb before validation.
3B. ECAN attention
- Global STI/LTI funds with wage on activity, rent on holdings, decay tick (time-based, not only on events).
- Hebbian-style association: co-occurring accounts in splits boost mutual attention.
- API: top-K attended accounts; “stale high-balance” detection.
- Optional OpenCog AttentionBank adapter when
HAVE_OPENCOG_ATTENTION.
3C. MOSES strategy discovery
- Replace placeholder “return same transaction”:
- Featureize historical txs (split count, type pairs, day-of-week, amount buckets).
- Evolutionary or greedy search over categorization/balancing heuristics.
- Output: ranked rule set (atoms + human-readable Scheme/JSON), not silent const_cast.
- Optimize path: suggest missing imbalance split account candidates (never mutate committed txs in place without edit API).
3D. URE prediction
- Historical series per account (balance over time or net flows).
- Simple models first: mean drift + variance → prediction ± bounds.
- Return structure with point estimate, low/high, confidence (extend API beyond bare
gnc_numeric if needed).
- Seasonal hooks later (monthly bills).
3E. Emergence / distributed cognition
- Define measurable patterns (recurring transfer pairs, anomalous spikes, attention cascades).
- Wire comms hub so PLN validation results publish to ECAN/MOSES consumers with typed payloads (atoms or small structs), not
gpointer voids.
Exit criteria: Golden-file tests on a sample book; proofs produce checkable numeric equality; predictions beat naive “current balance” on held-out windows for at least one synthetic series.
Phase 4 — Tensor network completion (Memory / Task / AI / Autonomy)
Goal: Turn the four-node architecture into a real pipeline over financial tensors.
Work
- Feature schema (versioned):
- Transactions: normalized amount, log-magnitude, day, dow, account-type one-hots, commodity id, imbalance flag, split count.
- Accounts: type, depth, child count, balance z-score, STI/LTI, reconcile age.
- Memory node: ring buffer / windowed tensor store; optional mmap cache for large books.
- Task node: DAG of jobs (
encode → cluster → attention → insights) with priorities from ECAN.
- AI node / Cogfluence:
- Replace
sin/cos toys with real clustering (k-means or hierarchical on standardized features).
- Pattern discovery: cluster centroids + silhouette; anomaly scores.
- Insights as structured key/values + AtomSpace Concept/Evaluation links.
- Autonomy node: adjust clustering k, attention decay, and task priorities from recent fitness (validation accuracy, user dismissals later).
- ggml backend: when
HAVE_GGML, build compute graphs for distance/clustering kernels; keep CPU fallback identical numerically within tolerance.
- Bridge tensor insights → AtomSpace (AI node outputs become atoms; Autonomy modulates ECAN funds).
Exit criteria: End-to-end test: N transactions → clusters → insights table → attention redistribution; ggml and fallback agree within ε.
Phase 5 — Scheme / Guile integration (GnuCash-native)
Goal: Use GnuCash’s existing Guile stack rather than OpenCog Scheme-only.
Work
- Expose cognitive C API via SWIG/Guile modules (
(gnucash cognitive)).
- Scheme helpers for reports: validation summary, attention top accounts, predicted balances.
- Keep OpenCog SchemeEval path only when AtomSpace is real; otherwise evaluate through Guile.
- Hypergraph pattern strings become export format + optional BindLink-like DSL, not fake eval.
Exit criteria: Guile REPL can init cognitive core, map a book, and print validation/attention without C demos.
Phase 6 — Product surfaces (UI, import, reports)
Goal: Users can see and act on cognitive output.
Work
- Preferences: enable cognitive engine; attention decay; clustering window.
- Register/transaction UI: validation badge/tooltip (confidence + reason).
- Account tree: optional attention heat or sort-by-attention.
- Reports (HTML/Guile):
- Cognitive Trial Balance / P&L proof report
- Attention & activity report
- Anomaly/cluster report
- Import pipeline (CSV/OFX/QIF): post-import batch validate + suggest categorizations from MOSES patterns.
- Python bindings examples updated for cognitive queries (optional).
Exit criteria: Manual UX path on sample book without running C++ demos.
Phase 7 — Optional real OpenCog / distributed backends
Goal: Adapters, not ifdefs sprinkled through business logic.
Work
CognitiveBackend interface: SimulatedBackend (default), OpenCogBackend, (later) RemoteCogServerBackend.
- Map Gnc atom types ↔ OpenCog types carefully; own handle table.
- PLN/URE/MOSES call real modules only inside backend; simulated remains reference implementation for tests.
- CogServer: optional out-of-process AtomSpace for heavy analysis; never block GUI commit path.
- Packaging: document deps; CI matrix job with
-DWITH_OPENCOG=ON when images exist.
Exit criteria: Same public API passes tests on Simulated and OpenCog backends for a core subset.
Phase 8 — Hardening, security, performance, docs
Work
- Fuzz/property tests: random balanced/unbalanced txs; attention fund conservation invariants.
- Memory: no growth on repeated open/close; message queues bounded.
- Security: no
system/eval of untrusted Scheme from book data; sanitize names in generated Scheme strings.
- Multi-commodity and business features (invoices) cognitive mapping — phase after personal accounting path is solid.
- Rewrite docs to match reality:
- Architecture (what works / optional)
- Developer guide (build flags, adding rules)
- User guide (features + limitations)
- Demote
IMPLEMENTATION_REPORT.md claims; track completion with a real checklist in repo or issues.
- Benchmarks on large books (10k–100k txs).
Recommended delivery milestones
| Milestone | Outcome |
|---|
| M0 | Clean build + green cognitive/tensor unit tests (no external cognitive deps) |
| M1 | AtomSpace mirrors CoA + txs; QOF hooks live behind feature flag |
| M2 | PLN validation + trial balance/P&L proofs with numeric checks |
| M3 | ECAN ranking + URE forecast with uncertainty |
| M4 | Tensor pipeline with real clustering + insights → atoms |
| M5 | Guile/report surface for validation & attention |
| M6 | Import assists + register UI badges |
| M7 | OpenCog backend adapter (optional CI) |
| M8 | Performance, docs truth-up, release-quality flag |
Suggested workstream order (dependency graph)
- Stabilize compile & types (Phase 0)
- Atom model + tests (Phase 1)
- Lifecycle hooks (Phase 2)
- Parallelize after M1:
- PLN/proofs (3A)
- ECAN (3B)
- Tensor features/clustering (Phase 4 core)
- URE + MOSES (3C/3D) using validation fitness from 3A
- Guile/reports (Phase 5–6)
- OpenCog adapter (Phase 7)
- Hardening (Phase 8)
Explicit non-goals for early milestones
- Full AGI / “emergent consciousness” messaging without measurable metrics
- Mutating user transactions silently
- Requiring OpenCog or ggml for basic cognitive features
- Blockchain or multi-agent distributed ledgers
- GPU path before CPU correctness
Immediate next implementation slice (when coding starts)
- Repair
gnc-cognitive-accounting.cpp / gnc-cognitive-scheme.cpp / message type clash.
- Introduce unified
GncCognitiveAtom record + always-on simulated backend.
- Align unit tests with TV semantics and fund accounting invariants.
- Add QOF commit hook behind feature flag calling PLN + ECAN.
- Replace Cogfluence
sin/cos with k-means on a documented feature vector.
That slice turns the project from “documented scaffold” into a shippable cognitive validation layer others can extend.
Implementation Plan: Complete GnuCash Cognitive Accounting
Guiding principles
libgnucash/enginefirst; GUI/report work only after stable APIs.Phase 0 — Make it build (foundation unblocker)
Goal: Clean compile of
gnc-enginewith cognitive sources on a stock Linux build (no OpenCog/ggml required).Work
gnc-cognitive-accounting.cpp:GncCognitiveAtomSpacelayout (single struct with always-present maps + optional OpenCog handles pointer).create_hierarchy_link, fund pools, truth values, and constructors exist on all build paths.strategy_atomis always defined before OpenCog/ASMOSES branches.returnand unify one algorithm path.gnc-cognitive-scheme.cpp:GncCognitiveMessage(prefer the module-enum hub in comms, or rename accounting’s string-based message toGncCognitiveAtomMessage).std::vectorreceive) out ofextern "C"or wrap with opaque C handles.GGML_LIBRARIES/ include dirs whenHAVE_GGML.[0.7, 1.0], not exact1.0).test-cognitive-accounting.shpresence checks with realctesttargets.Exit criteria:
ninja gnc-engine test-cognitive-accounting test-tensor-networksucceeds without OpenCog/ggml.Phase 1 — Solid simulated AtomSpace core
Goal: A deterministic in-process hypergraph that correctly mirrors Chart of Accounts and transactions.
Work
gnc_account_to_atomspace: ConceptNode per account; InheritanceLink to type + parent; EvaluationLink for balance snapshots.Exit criteria: Unit tests for hierarchy links, idempotent account atoms, balanced vs imbalanced TVs, and atom graph walk APIs.
Phase 2 — Engine lifecycle integration
Goal: Cognitive layer runs automatically when users do normal accounting, not only from demos.
Work
gnc_engine_init/ book open-close), guarded by feature flag (e.g.GNC_FEATURE_COGNITIVE_ACCOUNTINGor runtime pref).Exit criteria: Opening a book + posting transactions updates cognitive state; shutdown is leak-free under ASAN/tests.
Phase 3 — Real algorithms behind the OpenCog-shaped API
Complete each subsystem as useful accounting behavior, still simulated unless optional libs present.
3A. PLN validation & proofs
3B. ECAN attention
HAVE_OPENCOG_ATTENTION.3C. MOSES strategy discovery
3D. URE prediction
gnc_numericif needed).3E. Emergence / distributed cognition
gpointervoids.Exit criteria: Golden-file tests on a sample book; proofs produce checkable numeric equality; predictions beat naive “current balance” on held-out windows for at least one synthetic series.
Phase 4 — Tensor network completion (Memory / Task / AI / Autonomy)
Goal: Turn the four-node architecture into a real pipeline over financial tensors.
Work
encode → cluster → attention → insights) with priorities from ECAN.sin/costoys with real clustering (k-means or hierarchical on standardized features).HAVE_GGML, build compute graphs for distance/clustering kernels; keep CPU fallback identical numerically within tolerance.Exit criteria: End-to-end test: N transactions → clusters → insights table → attention redistribution; ggml and fallback agree within ε.
Phase 5 — Scheme / Guile integration (GnuCash-native)
Goal: Use GnuCash’s existing Guile stack rather than OpenCog Scheme-only.
Work
(gnucash cognitive)).Exit criteria: Guile REPL can init cognitive core, map a book, and print validation/attention without C demos.
Phase 6 — Product surfaces (UI, import, reports)
Goal: Users can see and act on cognitive output.
Work
Exit criteria: Manual UX path on sample book without running C++ demos.
Phase 7 — Optional real OpenCog / distributed backends
Goal: Adapters, not ifdefs sprinkled through business logic.
Work
CognitiveBackendinterface: SimulatedBackend (default), OpenCogBackend, (later) RemoteCogServerBackend.-DWITH_OPENCOG=ONwhen images exist.Exit criteria: Same public API passes tests on Simulated and OpenCog backends for a core subset.
Phase 8 — Hardening, security, performance, docs
Work
system/eval of untrusted Scheme from book data; sanitize names in generated Scheme strings.IMPLEMENTATION_REPORT.mdclaims; track completion with a real checklist in repo or issues.Recommended delivery milestones
Suggested workstream order (dependency graph)
Explicit non-goals for early milestones
Immediate next implementation slice (when coding starts)
gnc-cognitive-accounting.cpp/gnc-cognitive-scheme.cpp/ message type clash.GncCognitiveAtomrecord + always-on simulated backend.sin/coswith k-means on a documented feature vector.That slice turns the project from “documented scaffold” into a shippable cognitive validation layer others can extend.