htm 0.0.1

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.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/.architecture/decisions/adrs/001-use-postgresql-timescaledb-storage.md +227 -0
  3. data/.architecture/decisions/adrs/002-two-tier-memory-architecture.md +322 -0
  4. data/.architecture/decisions/adrs/003-ollama-default-embedding-provider.md +339 -0
  5. data/.architecture/decisions/adrs/004-multi-robot-shared-memory-hive-mind.md +374 -0
  6. data/.architecture/decisions/adrs/005-rag-based-retrieval-with-hybrid-search.md +443 -0
  7. data/.architecture/decisions/adrs/006-context-assembly-strategies.md +444 -0
  8. data/.architecture/decisions/adrs/007-working-memory-eviction-strategy.md +461 -0
  9. data/.architecture/decisions/adrs/008-robot-identification-system.md +550 -0
  10. data/.architecture/decisions/adrs/009-never-forget-explicit-deletion-only.md +570 -0
  11. data/.architecture/decisions/adrs/010-redis-working-memory-rejected.md +323 -0
  12. data/.architecture/decisions/adrs/011-database-side-embedding-generation-with-pgai.md +585 -0
  13. data/.architecture/decisions/adrs/012-llm-driven-ontology-topic-extraction.md +583 -0
  14. data/.architecture/decisions/adrs/013-activerecord-orm-and-many-to-many-tagging.md +299 -0
  15. data/.architecture/decisions/adrs/014-client-side-embedding-generation-workflow.md +569 -0
  16. data/.architecture/decisions/adrs/015-hierarchical-tag-ontology-and-llm-extraction.md +701 -0
  17. data/.architecture/decisions/adrs/016-async-embedding-and-tag-generation.md +694 -0
  18. data/.architecture/members.yml +144 -0
  19. data/.architecture/reviews/2025-10-29-llm-configuration-and-async-processing-review.md +1137 -0
  20. data/.architecture/reviews/initial-system-analysis.md +330 -0
  21. data/.envrc +32 -0
  22. data/.irbrc +145 -0
  23. data/CHANGELOG.md +150 -0
  24. data/COMMITS.md +196 -0
  25. data/LICENSE +21 -0
  26. data/README.md +1347 -0
  27. data/Rakefile +51 -0
  28. data/SETUP.md +268 -0
  29. data/config/database.yml +67 -0
  30. data/db/migrate/20250101000001_enable_extensions.rb +14 -0
  31. data/db/migrate/20250101000002_create_robots.rb +14 -0
  32. data/db/migrate/20250101000003_create_nodes.rb +42 -0
  33. data/db/migrate/20250101000005_create_tags.rb +38 -0
  34. data/db/migrate/20250101000007_add_node_vector_indexes.rb +30 -0
  35. data/db/schema.sql +473 -0
  36. data/db/seed_data/README.md +100 -0
  37. data/db/seed_data/presidents.md +136 -0
  38. data/db/seed_data/states.md +151 -0
  39. data/db/seeds.rb +208 -0
  40. data/dbdoc/README.md +173 -0
  41. data/dbdoc/public.node_stats.md +48 -0
  42. data/dbdoc/public.node_stats.svg +41 -0
  43. data/dbdoc/public.node_tags.md +40 -0
  44. data/dbdoc/public.node_tags.svg +112 -0
  45. data/dbdoc/public.nodes.md +54 -0
  46. data/dbdoc/public.nodes.svg +118 -0
  47. data/dbdoc/public.nodes_tags.md +39 -0
  48. data/dbdoc/public.nodes_tags.svg +112 -0
  49. data/dbdoc/public.ontology_structure.md +48 -0
  50. data/dbdoc/public.ontology_structure.svg +38 -0
  51. data/dbdoc/public.operations_log.md +42 -0
  52. data/dbdoc/public.operations_log.svg +130 -0
  53. data/dbdoc/public.relationships.md +39 -0
  54. data/dbdoc/public.relationships.svg +41 -0
  55. data/dbdoc/public.robot_activity.md +46 -0
  56. data/dbdoc/public.robot_activity.svg +35 -0
  57. data/dbdoc/public.robots.md +35 -0
  58. data/dbdoc/public.robots.svg +90 -0
  59. data/dbdoc/public.schema_migrations.md +29 -0
  60. data/dbdoc/public.schema_migrations.svg +26 -0
  61. data/dbdoc/public.tags.md +35 -0
  62. data/dbdoc/public.tags.svg +60 -0
  63. data/dbdoc/public.topic_relationships.md +45 -0
  64. data/dbdoc/public.topic_relationships.svg +32 -0
  65. data/dbdoc/schema.json +1437 -0
  66. data/dbdoc/schema.svg +154 -0
  67. data/docs/api/database.md +806 -0
  68. data/docs/api/embedding-service.md +532 -0
  69. data/docs/api/htm.md +797 -0
  70. data/docs/api/index.md +259 -0
  71. data/docs/api/long-term-memory.md +1096 -0
  72. data/docs/api/working-memory.md +665 -0
  73. data/docs/architecture/adrs/001-postgresql-timescaledb.md +314 -0
  74. data/docs/architecture/adrs/002-two-tier-memory.md +411 -0
  75. data/docs/architecture/adrs/003-ollama-embeddings.md +421 -0
  76. data/docs/architecture/adrs/004-hive-mind.md +437 -0
  77. data/docs/architecture/adrs/005-rag-retrieval.md +531 -0
  78. data/docs/architecture/adrs/006-context-assembly.md +496 -0
  79. data/docs/architecture/adrs/007-eviction-strategy.md +645 -0
  80. data/docs/architecture/adrs/008-robot-identification.md +625 -0
  81. data/docs/architecture/adrs/009-never-forget.md +648 -0
  82. data/docs/architecture/adrs/010-redis-working-memory-rejected.md +323 -0
  83. data/docs/architecture/adrs/011-pgai-integration.md +494 -0
  84. data/docs/architecture/adrs/index.md +215 -0
  85. data/docs/architecture/hive-mind.md +736 -0
  86. data/docs/architecture/index.md +351 -0
  87. data/docs/architecture/overview.md +538 -0
  88. data/docs/architecture/two-tier-memory.md +873 -0
  89. data/docs/assets/css/custom.css +83 -0
  90. data/docs/assets/images/htm-core-components.svg +63 -0
  91. data/docs/assets/images/htm-database-schema.svg +93 -0
  92. data/docs/assets/images/htm-hive-mind-architecture.svg +125 -0
  93. data/docs/assets/images/htm-importance-scoring-framework.svg +83 -0
  94. data/docs/assets/images/htm-layered-architecture.svg +71 -0
  95. data/docs/assets/images/htm-long-term-memory-architecture.svg +115 -0
  96. data/docs/assets/images/htm-working-memory-architecture.svg +120 -0
  97. data/docs/assets/images/htm.jpg +0 -0
  98. data/docs/assets/images/htm_demo.gif +0 -0
  99. data/docs/assets/js/mathjax.js +18 -0
  100. data/docs/assets/videos/htm_video.mp4 +0 -0
  101. data/docs/database_rake_tasks.md +322 -0
  102. data/docs/development/contributing.md +787 -0
  103. data/docs/development/index.md +336 -0
  104. data/docs/development/schema.md +596 -0
  105. data/docs/development/setup.md +719 -0
  106. data/docs/development/testing.md +819 -0
  107. data/docs/guides/adding-memories.md +824 -0
  108. data/docs/guides/context-assembly.md +1009 -0
  109. data/docs/guides/getting-started.md +577 -0
  110. data/docs/guides/index.md +118 -0
  111. data/docs/guides/long-term-memory.md +941 -0
  112. data/docs/guides/multi-robot.md +866 -0
  113. data/docs/guides/recalling-memories.md +927 -0
  114. data/docs/guides/search-strategies.md +953 -0
  115. data/docs/guides/working-memory.md +717 -0
  116. data/docs/index.md +214 -0
  117. data/docs/installation.md +477 -0
  118. data/docs/multi_framework_support.md +519 -0
  119. data/docs/quick-start.md +655 -0
  120. data/docs/setup_local_database.md +302 -0
  121. data/docs/using_rake_tasks_in_your_app.md +383 -0
  122. data/examples/basic_usage.rb +93 -0
  123. data/examples/cli_app/README.md +317 -0
  124. data/examples/cli_app/htm_cli.rb +270 -0
  125. data/examples/custom_llm_configuration.rb +183 -0
  126. data/examples/example_app/Rakefile +71 -0
  127. data/examples/example_app/app.rb +206 -0
  128. data/examples/sinatra_app/Gemfile +21 -0
  129. data/examples/sinatra_app/app.rb +335 -0
  130. data/lib/htm/active_record_config.rb +113 -0
  131. data/lib/htm/configuration.rb +342 -0
  132. data/lib/htm/database.rb +594 -0
  133. data/lib/htm/embedding_service.rb +115 -0
  134. data/lib/htm/errors.rb +34 -0
  135. data/lib/htm/job_adapter.rb +154 -0
  136. data/lib/htm/jobs/generate_embedding_job.rb +65 -0
  137. data/lib/htm/jobs/generate_tags_job.rb +82 -0
  138. data/lib/htm/long_term_memory.rb +965 -0
  139. data/lib/htm/models/node.rb +109 -0
  140. data/lib/htm/models/node_tag.rb +33 -0
  141. data/lib/htm/models/robot.rb +52 -0
  142. data/lib/htm/models/tag.rb +76 -0
  143. data/lib/htm/railtie.rb +76 -0
  144. data/lib/htm/sinatra.rb +157 -0
  145. data/lib/htm/tag_service.rb +135 -0
  146. data/lib/htm/tasks.rb +38 -0
  147. data/lib/htm/version.rb +5 -0
  148. data/lib/htm/working_memory.rb +182 -0
  149. data/lib/htm.rb +400 -0
  150. data/lib/tasks/db.rake +19 -0
  151. data/lib/tasks/htm.rake +147 -0
  152. data/lib/tasks/jobs.rake +312 -0
  153. data/mkdocs.yml +190 -0
  154. data/scripts/install_local_database.sh +309 -0
  155. metadata +341 -0
data/docs/api/index.md ADDED
@@ -0,0 +1,259 @@
1
+ # API Reference
2
+
3
+ Complete API documentation for HTM (Hierarchical Temporary Memory).
4
+
5
+ ## Overview
6
+
7
+ HTM is a two-tier intelligent memory management system for LLM-based robots:
8
+
9
+ - **Working Memory**: Token-limited active context for immediate LLM use
10
+ - **Long-term Memory**: Durable PostgreSQL storage with RAG-based retrieval
11
+
12
+ ## Class Hierarchy
13
+
14
+ ```
15
+ HTM (main class)
16
+ ├── HTM::WorkingMemory (token-limited active context)
17
+ ├── HTM::LongTermMemory (PostgreSQL backend)
18
+ ├── HTM::EmbeddingService (vector embeddings)
19
+ └── HTM::Database (schema setup and configuration)
20
+ ```
21
+
22
+ ## Class Diagram
23
+
24
+ <svg viewBox="0 0 800 600" xmlns="http://www.w3.org/2000/svg" style="background: transparent;">
25
+ <defs>
26
+ <style>
27
+ .class-box { fill: #1e1e1e; stroke: #4a9eff; stroke-width: 2; }
28
+ .class-title { fill: #4a9eff; font-family: monospace; font-size: 14px; font-weight: bold; }
29
+ .class-method { fill: #cccccc; font-family: monospace; font-size: 11px; }
30
+ .arrow { stroke: #4a9eff; stroke-width: 2; fill: none; marker-end: url(#arrowhead); }
31
+ .label { fill: #888888; font-family: sans-serif; font-size: 10px; }
32
+ </style>
33
+ <marker id="arrowhead" markerWidth="10" markerHeight="10" refX="9" refY="3" orient="auto">
34
+ <polygon points="0 0, 10 3, 0 6" fill="#4a9eff" />
35
+ </marker>
36
+ </defs>
37
+
38
+ <!-- HTM main class -->
39
+ <rect x="300" y="20" width="200" height="180" class="class-box" rx="5"/>
40
+ <text x="400" y="45" text-anchor="middle" class="class-title">HTM</text>
41
+ <line x1="310" y1="55" x2="490" y2="55" stroke="#4a9eff" stroke-width="1"/>
42
+ <text x="310" y="75" class="class-method">+ add_node()</text>
43
+ <text x="310" y="90" class="class-method">+ recall()</text>
44
+ <text x="310" y="105" class="class-method">+ retrieve()</text>
45
+ <text x="310" y="120" class="class-method">+ forget()</text>
46
+ <text x="310" y="135" class="class-method">+ create_context()</text>
47
+ <text x="310" y="150" class="class-method">+ memory_stats()</text>
48
+ <text x="310" y="165" class="class-method">+ which_robot_said()</text>
49
+ <text x="310" y="180" class="class-method">+ conversation_timeline()</text>
50
+
51
+ <!-- WorkingMemory -->
52
+ <rect x="50" y="250" width="180" height="150" class="class-box" rx="5"/>
53
+ <text x="140" y="275" text-anchor="middle" class="class-title">WorkingMemory</text>
54
+ <line x1="60" y1="285" x2="220" y2="285" stroke="#4a9eff" stroke-width="1"/>
55
+ <text x="60" y="305" class="class-method">+ add()</text>
56
+ <text x="60" y="320" class="class-method">+ remove()</text>
57
+ <text x="60" y="335" class="class-method">+ has_space?()</text>
58
+ <text x="60" y="350" class="class-method">+ evict_to_make_space()</text>
59
+ <text x="60" y="365" class="class-method">+ assemble_context()</text>
60
+ <text x="60" y="380" class="class-method">+ token_count()</text>
61
+
62
+ <!-- LongTermMemory -->
63
+ <rect x="260" y="250" width="180" height="180" class="class-box" rx="5"/>
64
+ <text x="350" y="275" text-anchor="middle" class="class-title">LongTermMemory</text>
65
+ <line x1="270" y1="285" x2="430" y2="285" stroke="#4a9eff" stroke-width="1"/>
66
+ <text x="270" y="305" class="class-method">+ add()</text>
67
+ <text x="270" y="320" class="class-method">+ retrieve()</text>
68
+ <text x="270" y="335" class="class-method">+ search()</text>
69
+ <text x="270" y="350" class="class-method">+ search_fulltext()</text>
70
+ <text x="270" y="365" class="class-method">+ search_hybrid()</text>
71
+ <text x="270" y="380" class="class-method">+ add_relationship()</text>
72
+ <text x="270" y="395" class="class-method">+ add_tag()</text>
73
+ <text x="270" y="410" class="class-method">+ stats()</text>
74
+
75
+ <!-- EmbeddingService -->
76
+ <rect x="470" y="250" width="180" height="120" class="class-box" rx="5"/>
77
+ <text x="560" y="275" text-anchor="middle" class="class-title">EmbeddingService</text>
78
+ <line x1="480" y1="285" x2="640" y2="285" stroke="#4a9eff" stroke-width="1"/>
79
+ <text x="480" y="305" class="class-method">+ embed()</text>
80
+ <text x="480" y="320" class="class-method">+ count_tokens()</text>
81
+ <text x="480" y="335" class="class-method">- embed_ollama()</text>
82
+ <text x="480" y="350" class="class-method">- embed_openai()</text>
83
+
84
+ <!-- Database -->
85
+ <rect x="300" y="470" width="200" height="90" class="class-box" rx="5"/>
86
+ <text x="400" y="495" text-anchor="middle" class="class-title">Database</text>
87
+ <line x1="310" y1="505" x2="490" y2="505" stroke="#4a9eff" stroke-width="1"/>
88
+ <text x="310" y="525" class="class-method">+ setup()</text>
89
+ <text x="310" y="540" class="class-method">+ default_config()</text>
90
+
91
+ <!-- Arrows -->
92
+ <path d="M 400 200 L 140 250" class="arrow"/>
93
+ <text x="250" y="220" class="label">uses</text>
94
+
95
+ <path d="M 400 200 L 350 250" class="arrow"/>
96
+ <text x="370" y="220" class="label">uses</text>
97
+
98
+ <path d="M 400 200 L 560 250" class="arrow"/>
99
+ <text x="480" y="220" class="label">uses</text>
100
+
101
+ <path d="M 400 200 L 400 470" class="arrow"/>
102
+ <text x="410" y="340" class="label">config</text>
103
+ </svg>
104
+
105
+ ## Quick Reference
106
+
107
+ ### Core Classes
108
+
109
+ | Class | Purpose | Key Methods |
110
+ |-------|---------|-------------|
111
+ | [HTM](htm.md) | Main interface for memory management | `add_node`, `recall`, `retrieve`, `forget`, `create_context` |
112
+ | [WorkingMemory](working-memory.md) | Token-limited active context | `add`, `evict_to_make_space`, `assemble_context` |
113
+ | [LongTermMemory](long-term-memory.md) | Persistent PostgreSQL storage | `add`, `search`, `search_fulltext`, `search_hybrid` |
114
+ | [EmbeddingService](embedding-service.md) | Vector embedding generation | `embed`, `count_tokens` |
115
+ | [Database](database.md) | Schema setup and configuration | `setup`, `default_config` |
116
+
117
+ ### Common Usage Patterns
118
+
119
+ #### Basic Memory Operations
120
+
121
+ ```ruby
122
+ # Initialize HTM
123
+ htm = HTM.new(robot_name: "Assistant")
124
+
125
+ # Add memories
126
+ htm.add_node("fact_001", "PostgreSQL is our database",
127
+ type: :fact, importance: 7.0, tags: ["database"])
128
+
129
+ # Recall memories
130
+ memories = htm.recall(timeframe: "last week", topic: "PostgreSQL")
131
+
132
+ # Create LLM context
133
+ context = htm.create_context(strategy: :balanced)
134
+ ```
135
+
136
+ #### Multi-Robot Collaboration
137
+
138
+ ```ruby
139
+ # Find who discussed a topic
140
+ robots = htm.which_robot_said("deployment")
141
+ # => {"robot-123" => 5, "robot-456" => 3}
142
+
143
+ # Get conversation timeline
144
+ timeline = htm.conversation_timeline("deployment", limit: 20)
145
+ ```
146
+
147
+ #### Advanced Retrieval
148
+
149
+ ```ruby
150
+ # Vector similarity search
151
+ memories = htm.recall(
152
+ timeframe: "last 30 days",
153
+ topic: "API design decisions",
154
+ strategy: :vector,
155
+ limit: 10
156
+ )
157
+
158
+ # Hybrid search (fulltext + vector)
159
+ memories = htm.recall(
160
+ timeframe: "this month",
161
+ topic: "security vulnerabilities",
162
+ strategy: :hybrid,
163
+ limit: 20
164
+ )
165
+ ```
166
+
167
+ #### Memory Management
168
+
169
+ ```ruby
170
+ # Get statistics
171
+ stats = htm.memory_stats
172
+ # => { total_nodes: 1234, working_memory: { current_tokens: 45000, ... }, ... }
173
+
174
+ # Explicitly forget
175
+ htm.forget("temp_note", confirm: :confirmed)
176
+ ```
177
+
178
+ ## Search Strategies
179
+
180
+ HTM supports three search strategies for `recall`:
181
+
182
+ | Strategy | Description | Use Case |
183
+ |----------|-------------|----------|
184
+ | `:vector` | Semantic similarity using embeddings | Find conceptually related content |
185
+ | `:fulltext` | PostgreSQL full-text search | Find exact terms and phrases |
186
+ | `:hybrid` | Combines fulltext + vector | Best of both worlds - accurate and semantic |
187
+
188
+ ## Memory Types
189
+
190
+ When adding nodes, you can specify a type:
191
+
192
+ | Type | Purpose |
193
+ |------|---------|
194
+ | `:fact` | Factual information |
195
+ | `:context` | Contextual background |
196
+ | `:code` | Code snippets |
197
+ | `:preference` | User preferences |
198
+ | `:decision` | Architectural decisions |
199
+ | `:question` | Questions and answers |
200
+
201
+ ## Context Assembly Strategies
202
+
203
+ When creating context with `create_context`:
204
+
205
+ | Strategy | Behavior |
206
+ |----------|----------|
207
+ | `:recent` | Most recently accessed first |
208
+ | `:important` | Highest importance scores first |
209
+ | `:balanced` | Weighted by importance and recency |
210
+
211
+ ## API Documentation
212
+
213
+ - [HTM](htm.md) - Main class reference
214
+ - [WorkingMemory](working-memory.md) - Active context management
215
+ - [LongTermMemory](long-term-memory.md) - Persistent storage
216
+ - [EmbeddingService](embedding-service.md) - Vector embeddings
217
+ - [Database](database.md) - Schema and configuration
218
+
219
+ ## Error Handling
220
+
221
+ HTM raises standard Ruby exceptions:
222
+
223
+ ```ruby
224
+ # ArgumentError for invalid parameters
225
+ htm.forget("key") # Raises: Must pass confirm: :confirmed
226
+
227
+ # PG::Error for database issues
228
+ htm.add_node("key", "value") # May raise PG connection errors
229
+
230
+ # Invalid timeframe
231
+ htm.recall(timeframe: "invalid") # Raises ArgumentError
232
+ ```
233
+
234
+ ## Thread Safety
235
+
236
+ HTM is **not thread-safe** by default. Each instance maintains its own working memory state. For multi-threaded applications:
237
+
238
+ - Use separate HTM instances per thread
239
+ - Or implement external synchronization
240
+ - Database connections are created per operation (safe)
241
+
242
+ ## Performance Considerations
243
+
244
+ - **Working Memory**: O(n) for eviction, O(1) for add/remove
245
+ - **Vector Search**: O(log n) with proper indexing
246
+ - **Fulltext Search**: O(log n) with GIN indexes
247
+ - **Hybrid Search**: Combines both overhead
248
+
249
+ For large memory stores (>100K nodes):
250
+
251
+ - Use hybrid search with appropriate `prefilter_limit`
252
+ - Consider time-based partitioning (automatic with TimescaleDB)
253
+ - Enable compression for old data (configured in schema)
254
+
255
+ ## Next Steps
256
+
257
+ - See [HTM class reference](htm.md) for detailed API
258
+ - Review [examples](https://github.com/madbomber/htm/tree/main/examples) for common patterns
259
+ - Check [database schema](../development/schema.md) for advanced queries