smart_brain 0.1.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/MEMPAL_GUIDE.md +1074 -0
- data/README.en.md +206 -173
- data/README.md +593 -173
- data/config/brain.yml +69 -1
- data/conversation_demo.rb +438 -438
- data/db/migrate/002_turn_events_payload.sql +9 -0
- data/db/migrate/003_tiers_and_lifecycle.sql +28 -0
- data/db/migrate/004_kg_edges.sql +30 -0
- data/db/migrate/005_domains_and_memory_scopes.sql +163 -0
- data/docs/coding_todo.md +139 -0
- data/docs/context_package.md +220 -0
- data/docs/evidence_pack.md +190 -0
- data/docs/gap_vs_mempal.md +161 -0
- data/docs/installation.md +198 -0
- data/docs/mcp.md +93 -0
- data/docs/media_memory_schema.md +271 -0
- data/docs/memory_types.md +278 -0
- data/docs/multi_scope_memory_refactor_plan.md +483 -0
- data/docs/multi_scope_migration.md +65 -0
- data/docs/policies.md +308 -0
- data/docs/retrieval_plan.md +233 -0
- data/docs/smartbrain_design.md +299 -0
- data/docs/user_guide.md +547 -0
- data/example.rb +91 -91
- data/examples/01_memory_basic.rb +57 -0
- data/examples/02_governance.rb +63 -0
- data/examples/03_postgres_persistence.rb +63 -0
- data/examples/04_ollama_llm.rb +69 -0
- data/examples/05_smart_rag_integration.rb +79 -0
- data/examples/06_multi_scope_memory.rb +50 -0
- data/examples/07_media_memory.rb +53 -0
- data/examples/README.md +49 -0
- data/exe/smart_brain +168 -0
- data/lib/smart_brain/adapters/smart_rag/direct_client.rb +57 -5
- data/lib/smart_brain/adapters/smart_rag/http_client.rb +118 -5
- data/lib/smart_brain/adapters/smart_rag/http_transport.rb +138 -0
- data/lib/smart_brain/adapters/smart_rag/media_metadata_extractor.rb +255 -0
- data/lib/smart_brain/adapters/smart_rag/null_client.rb +44 -2
- data/lib/smart_brain/adapters/smart_rag/scope_filter.rb +60 -0
- data/lib/smart_brain/configuration.rb +60 -0
- data/lib/smart_brain/consolidator/working_summary.rb +80 -12
- data/lib/smart_brain/context_composer/composer.rb +40 -3
- data/lib/smart_brain/contracts/retrieval_plan.rb +10 -0
- data/lib/smart_brain/contracts/scope_context.rb +46 -0
- data/lib/smart_brain/contracts/scope_ref.rb +25 -0
- data/lib/smart_brain/db.rb +109 -0
- data/lib/smart_brain/event_store/in_memory.rb +6 -2
- data/lib/smart_brain/event_store/postgres.rb +199 -0
- data/lib/smart_brain/fusion/merger.rb +31 -2
- data/lib/smart_brain/governance/briefing.rb +146 -0
- data/lib/smart_brain/governance/fact_check.rb +110 -0
- data/lib/smart_brain/governance/knowledge_graph.rb +60 -0
- data/lib/smart_brain/governance/lifecycle.rb +225 -0
- data/lib/smart_brain/governance/tiers.rb +60 -0
- data/lib/smart_brain/memory_extractor/extractor.rb +25 -7
- data/lib/smart_brain/memory_store/in_memory.rb +202 -17
- data/lib/smart_brain/memory_store/postgres.rb +500 -0
- data/lib/smart_brain/model_provider/base.rb +87 -0
- data/lib/smart_brain/model_provider/factory.rb +49 -0
- data/lib/smart_brain/model_provider/ollama.rb +60 -0
- data/lib/smart_brain/model_provider/openai.rb +60 -0
- data/lib/smart_brain/model_provider/stub.rb +26 -0
- data/lib/smart_brain/model_provider.rb +7 -0
- data/lib/smart_brain/observability/tracker.rb +39 -1
- data/lib/smart_brain/retrievers/exact_retriever.rb +6 -0
- data/lib/smart_brain/retrievers/memory_retriever.rb +59 -5
- data/lib/smart_brain/runtime.rb +306 -16
- data/lib/smart_brain/scopes/conflict_resolver.rb +67 -0
- data/lib/smart_brain/scopes/registry.rb +133 -0
- data/lib/smart_brain/scopes/resolver.rb +32 -0
- data/lib/smart_brain/server/http_app.rb +143 -0
- data/lib/smart_brain/server/mcp_server.rb +385 -0
- data/lib/smart_brain/server/service.rb +129 -0
- data/lib/smart_brain/support/levenshtein.rb +35 -0
- data/lib/smart_brain/version.rb +5 -5
- data/lib/smart_brain.rb +93 -35
- metadata +100 -54
data/README.en.md
CHANGED
|
@@ -1,173 +1,206 @@
|
|
|
1
|
-
# SmartBrain
|
|
2
|
-
|
|
3
|
-
SmartBrain is an Agent Memory Runtime and Context Composer.
|
|
4
|
-
|
|
5
|
-
Core responsibilities:
|
|
6
|
-
- `commit_turn`: persist event truth and structured memory
|
|
7
|
-
- `compose_context`: build minimal, sufficient context per turn
|
|
8
|
-
- integrate with SmartRAG: SmartBrain handles conversation memory, SmartRAG handles resource retrieval
|
|
9
|
-
|
|
10
|
-
## Current Progress
|
|
11
|
-
|
|
12
|
-
The repository now includes a runnable v0.1 flow with:
|
|
13
|
-
- `commit_turn` / `compose_context` end-to-end pipeline
|
|
14
|
-
- retention / consolidation / retrieval / composition policies
|
|
15
|
-
- retrievers: exact + relational
|
|
16
|
-
- fusion: dedupe, rule-based rerank, diversity, budget truncation
|
|
17
|
-
- SmartRAG adapters: `NullClient`, `HttpClient`, `DirectClient`
|
|
18
|
-
- traceability via `request_id` / `plan_id` / `context_id`
|
|
19
|
-
- RSpec coverage (unit + integration + regression)
|
|
20
|
-
|
|
21
|
-
## Project Layout
|
|
22
|
-
|
|
23
|
-
- `lib/smart_brain.rb`: public API
|
|
24
|
-
- `lib/smart_brain/runtime.rb`: runtime orchestration
|
|
25
|
-
- `lib/smart_brain/contracts/`: RetrievalPlan / EvidencePack / ContextPackage validation
|
|
26
|
-
- `lib/smart_brain/observability/`: logs and metrics
|
|
27
|
-
- `lib/smart_brain/event_store/`: event storage (in-memory currently)
|
|
28
|
-
- `lib/smart_brain/memory_store/`: memory storage (in-memory currently)
|
|
29
|
-
- `lib/smart_brain/retrievers/`: exact/relational retrievers
|
|
30
|
-
- `lib/smart_brain/fusion/`: multi-source fusion
|
|
31
|
-
- `lib/smart_brain/context_composer/`: context assembly
|
|
32
|
-
- `lib/smart_brain/adapters/smart_rag/`: SmartRAG adapters
|
|
33
|
-
- `config/brain.yml`: policy config
|
|
34
|
-
- `example.rb`: SmartBrain + SmartAgent + SmartPrompt + SmartRAG demo
|
|
35
|
-
- `docs/`: design and protocol documents
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
bundle config set --local
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
{ role: '
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
puts context
|
|
77
|
-
puts context.dig(:debug, :trace, :
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
SmartBrain.
|
|
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
|
-
```bash
|
|
165
|
-
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
1
|
+
# SmartBrain
|
|
2
|
+
|
|
3
|
+
SmartBrain is an Agent Memory Runtime and Context Composer.
|
|
4
|
+
|
|
5
|
+
Core responsibilities:
|
|
6
|
+
- `commit_turn`: persist event truth and structured memory
|
|
7
|
+
- `compose_context`: build minimal, sufficient context per turn
|
|
8
|
+
- integrate with SmartRAG: SmartBrain handles conversation memory, SmartRAG handles resource retrieval
|
|
9
|
+
|
|
10
|
+
## Current Progress
|
|
11
|
+
|
|
12
|
+
The repository now includes a runnable v0.1 flow with:
|
|
13
|
+
- `commit_turn` / `compose_context` end-to-end pipeline
|
|
14
|
+
- retention / consolidation / retrieval / composition policies
|
|
15
|
+
- retrievers: exact + relational
|
|
16
|
+
- fusion: dedupe, rule-based rerank, diversity, budget truncation
|
|
17
|
+
- SmartRAG adapters: `NullClient`, `HttpClient`, `DirectClient`
|
|
18
|
+
- traceability via `request_id` / `plan_id` / `context_id`
|
|
19
|
+
- RSpec coverage (unit + integration + regression)
|
|
20
|
+
|
|
21
|
+
## Project Layout
|
|
22
|
+
|
|
23
|
+
- `lib/smart_brain.rb`: public API
|
|
24
|
+
- `lib/smart_brain/runtime.rb`: runtime orchestration
|
|
25
|
+
- `lib/smart_brain/contracts/`: RetrievalPlan / EvidencePack / ContextPackage validation
|
|
26
|
+
- `lib/smart_brain/observability/`: logs and metrics
|
|
27
|
+
- `lib/smart_brain/event_store/`: event storage (in-memory currently)
|
|
28
|
+
- `lib/smart_brain/memory_store/`: memory storage (in-memory currently)
|
|
29
|
+
- `lib/smart_brain/retrievers/`: exact/relational retrievers
|
|
30
|
+
- `lib/smart_brain/fusion/`: multi-source fusion
|
|
31
|
+
- `lib/smart_brain/context_composer/`: context assembly
|
|
32
|
+
- `lib/smart_brain/adapters/smart_rag/`: SmartRAG adapters
|
|
33
|
+
- `config/brain.yml`: policy config
|
|
34
|
+
- `example.rb`: SmartBrain + SmartAgent + SmartPrompt + SmartRAG demo
|
|
35
|
+
- `docs/`: design and protocol documents
|
|
36
|
+
- `docs/media_memory_schema.md`: media-memory contract, production controls, and SmartRAG migration requirements (Chinese)
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
bundle install
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
If you hit permission/shared-gem issues:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
bundle config set --local path 'vendor/bundle'
|
|
48
|
+
bundle config set --local disable_shared_gems 'true'
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Quick Start (SmartBrain Only)
|
|
52
|
+
|
|
53
|
+
```ruby
|
|
54
|
+
require_relative 'lib/smart_brain'
|
|
55
|
+
|
|
56
|
+
SmartBrain.configure
|
|
57
|
+
|
|
58
|
+
SmartBrain.commit_turn(
|
|
59
|
+
session_id: 'demo',
|
|
60
|
+
turn_events: {
|
|
61
|
+
messages: [
|
|
62
|
+
{ role: 'user', content: 'Remember this: default DB is Postgres.' },
|
|
63
|
+
{ role: 'assistant', content: 'Saved.' }
|
|
64
|
+
],
|
|
65
|
+
decisions: [
|
|
66
|
+
{ key: 'decision:smartbrain:storage', decision: 'Use Postgres by default' }
|
|
67
|
+
]
|
|
68
|
+
}
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
context = SmartBrain.compose_context(
|
|
72
|
+
session_id: 'demo',
|
|
73
|
+
user_message: 'Continue and summarize key points'
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
puts context[:context_id]
|
|
77
|
+
puts context.dig(:debug, :trace, :request_id)
|
|
78
|
+
puts context.dig(:debug, :trace, :plan_id)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## SmartRAG Integration Options
|
|
82
|
+
|
|
83
|
+
### 1) NullClient (default)
|
|
84
|
+
|
|
85
|
+
If no SmartRAG client is injected, resource evidence is empty.
|
|
86
|
+
|
|
87
|
+
### 2) HttpClient
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
transport = lambda do |plan, timeout_seconds:|
|
|
91
|
+
{
|
|
92
|
+
plan_id: 'p1',
|
|
93
|
+
supports_language_filter: true,
|
|
94
|
+
evidences: []
|
|
95
|
+
}
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
client = SmartBrain::Adapters::SmartRag::HttpClient.new(transport: transport, timeout_seconds: 2)
|
|
99
|
+
SmartBrain.configure(smart_rag_client: client)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
For the built-in SmartRAG HTTP API, configure the remote endpoint and Bearer token directly:
|
|
103
|
+
|
|
104
|
+
```ruby
|
|
105
|
+
client = SmartBrain::Adapters::SmartRag::HttpClient.for_url(
|
|
106
|
+
base_url: 'http://127.0.0.1:9393',
|
|
107
|
+
timeout_seconds: 30,
|
|
108
|
+
headers: { 'Authorization' => "Bearer #{ENV.fetch('SMARTRAG_TOKEN')}" }
|
|
109
|
+
)
|
|
110
|
+
SmartBrain.configure(smart_rag_client: client)
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 3) DirectClient (used in `example.rb`)
|
|
114
|
+
|
|
115
|
+
```ruby
|
|
116
|
+
require 'smart_rag'
|
|
117
|
+
require_relative 'lib/smart_brain/adapters/smart_rag/direct_client'
|
|
118
|
+
|
|
119
|
+
rag_config = SmartRAG::Config.load(ENV.fetch('SMARTRAG_CONFIG_PATH'))
|
|
120
|
+
rag = SmartRAG::SmartRAG.new(rag_config)
|
|
121
|
+
client = SmartBrain::Adapters::SmartRag::DirectClient.new(rag: rag)
|
|
122
|
+
|
|
123
|
+
SmartBrain.configure(smart_rag_client: client)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
## Media Queue Production Contract
|
|
127
|
+
|
|
128
|
+
SmartBrain delegates image, audio, and video persistence to SmartRAG. Before deploying the current queue code, migrate the **SmartRAG database** through `017_add_media_job_request_fingerprint`; running `smart_brain migrate` does not apply SmartRAG migrations. Migrations 015 and 016 add leases, shared S3/MinIO object lifecycle, staging protection, and principal-owned documents. Migration 017 backfills and requires a canonical SHA-256 request fingerprint.
|
|
129
|
+
|
|
130
|
+
Use a stable idempotency key for asynchronous retries:
|
|
131
|
+
|
|
132
|
+
```ruby
|
|
133
|
+
job = SmartBrain.enqueue_media(
|
|
134
|
+
source: '/data/product-demo.mp4',
|
|
135
|
+
options: { media_type: 'video', idempotency_key: 'product-demo-v1' }
|
|
136
|
+
)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
For the same authenticated principal, repeating the same operation, source, and canonicalized options returns the original job with `deduplicated: true`. Reusing the key with a different payload returns SmartRAG HTTP `409` and `code: "idempotency_conflict"`. Keep both the original key and payload for network retries; generate a new key when the business payload changes.
|
|
140
|
+
|
|
141
|
+
`HttpClient` converts a non-2xx response into a SmartBrain result with `status: "failed"`; an idempotency conflict is visible in `warnings` as `SmartRAG HTTP 409`. `DirectClient` propagates `SmartRAG::Core::MediaJobQueue::IdempotencyConflict` to the caller.
|
|
142
|
+
|
|
143
|
+
Authenticated SmartRAG retrieval uses defense in depth: it pushes the principal's PostgreSQL-owned document IDs into search and rechecks returned candidates before emitting evidence. Invalid Bearer tokens return 401. For multi-instance workers, configure a shared S3 or MinIO endpoint, bucket, and prefix; retained failed jobs protect their staging objects until pruning.
|
|
144
|
+
|
|
145
|
+
SmartRAG's opt-in real integration suites verify MinIO cross-instance storage and deletion, asynchronous worker materialization, failed-job GC protection, Bearer-token tenant isolation, HTTP 409 behavior, and concurrent idempotency races. See the SmartRAG README section "Real storage and isolation verification" for environment variables and commands.
|
|
146
|
+
|
|
147
|
+
## `example.rb` (Updated)
|
|
148
|
+
|
|
149
|
+
The example demonstrates the real loop:
|
|
150
|
+
1. SmartBrain `compose_context`
|
|
151
|
+
2. SmartAgent calls SmartPrompt worker via `call_worker`
|
|
152
|
+
3. SmartBrain `commit_turn`
|
|
153
|
+
4. prints `evidence(memory/resource)` so you can verify SmartRAG participation
|
|
154
|
+
|
|
155
|
+
Files used by the demo:
|
|
156
|
+
- `config/example_agent.yml`
|
|
157
|
+
- `config/example_llm.yml`
|
|
158
|
+
- `agents/brain_assistant.rb`
|
|
159
|
+
- `workers/brain_assistant.rb`
|
|
160
|
+
- `templates/brain_assistant.erb`
|
|
161
|
+
|
|
162
|
+
Run:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
bundle exec ruby example.rb
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Core API
|
|
169
|
+
|
|
170
|
+
### `SmartBrain.configure(config_path: nil, smart_rag_client: nil, clock: -> { Time.now.utc })`
|
|
171
|
+
Initialize runtime and optionally inject a SmartRAG client.
|
|
172
|
+
|
|
173
|
+
### `SmartBrain.commit_turn(session_id:, turn_events:)`
|
|
174
|
+
Persist events, extract memory, resolve conflicts, update summary.
|
|
175
|
+
|
|
176
|
+
### `SmartBrain.compose_context(session_id:, user_message:, agent_state: {})`
|
|
177
|
+
Build a `ContextPackage` with planning and fused evidence.
|
|
178
|
+
|
|
179
|
+
### `SmartBrain.diagnostics`
|
|
180
|
+
Return observability snapshot for compose/commit logs and metrics.
|
|
181
|
+
|
|
182
|
+
## Test
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
rspec
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Troubleshooting
|
|
189
|
+
|
|
190
|
+
### 1) `cannot load such file -- sequel/extensions/pgvector`
|
|
191
|
+
`example.rb` already applies compatibility handling (`Sequel.extension 'pgvector'` and strips `database.extensions` from DB connect config).
|
|
192
|
+
|
|
193
|
+
### 2) `Config file not found: config/llm_config.yml`
|
|
194
|
+
`example.rb` injects `config_path: ./config/example_llm.yml` for SmartRAG EmbeddingService startup.
|
|
195
|
+
|
|
196
|
+
### 3) `ruby-lsp: not found`
|
|
197
|
+
```bash
|
|
198
|
+
gem install --user-install ruby-lsp debug
|
|
199
|
+
```
|
|
200
|
+
and ensure user gem `bin` is in `PATH`.
|
|
201
|
+
|
|
202
|
+
## Roadmap
|
|
203
|
+
|
|
204
|
+
- migrate EventStore/MemoryStore from in-memory to Postgres-backed implementations
|
|
205
|
+
- integrate real reranker/embedding models
|
|
206
|
+
- improve SmartRAG ingest pipeline and cross-session evaluation tooling
|