htm 0.0.1 → 0.0.10

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 (184) hide show
  1. checksums.yaml +4 -4
  2. data/.aigcm_msg +1 -0
  3. data/.architecture/reviews/comprehensive-codebase-review.md +577 -0
  4. data/.claude/settings.local.json +92 -0
  5. data/.envrc +1 -0
  6. data/.irbrc +283 -80
  7. data/.tbls.yml +31 -0
  8. data/CHANGELOG.md +314 -16
  9. data/CLAUDE.md +603 -0
  10. data/README.md +76 -5
  11. data/Rakefile +5 -0
  12. data/SETUP.md +132 -101
  13. data/db/migrate/{20250101000001_enable_extensions.rb → 00001_enable_extensions.rb} +0 -1
  14. data/db/migrate/00002_create_robots.rb +11 -0
  15. data/db/migrate/00003_create_file_sources.rb +20 -0
  16. data/db/migrate/00004_create_nodes.rb +65 -0
  17. data/db/migrate/00005_create_tags.rb +13 -0
  18. data/db/migrate/00006_create_node_tags.rb +18 -0
  19. data/db/migrate/00007_create_robot_nodes.rb +26 -0
  20. data/db/migrate/00009_add_working_memory_to_robot_nodes.rb +12 -0
  21. data/db/schema.sql +390 -36
  22. data/docs/api/database.md +19 -232
  23. data/docs/api/embedding-service.md +1 -7
  24. data/docs/api/htm.md +305 -364
  25. data/docs/api/index.md +1 -7
  26. data/docs/api/long-term-memory.md +342 -590
  27. data/docs/api/yard/HTM/ActiveRecordConfig.md +23 -0
  28. data/docs/api/yard/HTM/AuthorizationError.md +11 -0
  29. data/docs/api/yard/HTM/CircuitBreaker.md +92 -0
  30. data/docs/api/yard/HTM/CircuitBreakerOpenError.md +34 -0
  31. data/docs/api/yard/HTM/Configuration.md +175 -0
  32. data/docs/api/yard/HTM/Database.md +99 -0
  33. data/docs/api/yard/HTM/DatabaseError.md +14 -0
  34. data/docs/api/yard/HTM/EmbeddingError.md +18 -0
  35. data/docs/api/yard/HTM/EmbeddingService.md +58 -0
  36. data/docs/api/yard/HTM/Error.md +11 -0
  37. data/docs/api/yard/HTM/JobAdapter.md +39 -0
  38. data/docs/api/yard/HTM/LongTermMemory.md +342 -0
  39. data/docs/api/yard/HTM/NotFoundError.md +17 -0
  40. data/docs/api/yard/HTM/Observability.md +107 -0
  41. data/docs/api/yard/HTM/QueryTimeoutError.md +19 -0
  42. data/docs/api/yard/HTM/Railtie.md +27 -0
  43. data/docs/api/yard/HTM/ResourceExhaustedError.md +13 -0
  44. data/docs/api/yard/HTM/TagError.md +18 -0
  45. data/docs/api/yard/HTM/TagService.md +67 -0
  46. data/docs/api/yard/HTM/Timeframe/Result.md +24 -0
  47. data/docs/api/yard/HTM/Timeframe.md +40 -0
  48. data/docs/api/yard/HTM/TimeframeExtractor/Result.md +24 -0
  49. data/docs/api/yard/HTM/TimeframeExtractor.md +45 -0
  50. data/docs/api/yard/HTM/ValidationError.md +20 -0
  51. data/docs/api/yard/HTM/WorkingMemory.md +131 -0
  52. data/docs/api/yard/HTM.md +80 -0
  53. data/docs/api/yard/index.csv +179 -0
  54. data/docs/api/yard-reference.md +51 -0
  55. data/docs/architecture/adrs/001-postgresql-timescaledb.md +1 -1
  56. data/docs/architecture/adrs/003-ollama-embeddings.md +1 -1
  57. data/docs/architecture/adrs/010-redis-working-memory-rejected.md +2 -27
  58. data/docs/architecture/adrs/index.md +2 -13
  59. data/docs/architecture/hive-mind.md +165 -166
  60. data/docs/architecture/index.md +2 -2
  61. data/docs/architecture/overview.md +5 -171
  62. data/docs/architecture/two-tier-memory.md +1 -35
  63. data/docs/assets/images/adr-010-current-architecture.svg +37 -0
  64. data/docs/assets/images/adr-010-proposed-architecture.svg +48 -0
  65. data/docs/assets/images/adr-dependency-tree.svg +93 -0
  66. data/docs/assets/images/class-hierarchy.svg +55 -0
  67. data/docs/assets/images/exception-hierarchy.svg +45 -0
  68. data/docs/assets/images/htm-architecture-overview.svg +83 -0
  69. data/docs/assets/images/htm-complete-memory-flow.svg +160 -0
  70. data/docs/assets/images/htm-context-assembly-flow.svg +148 -0
  71. data/docs/assets/images/htm-eviction-process.svg +141 -0
  72. data/docs/assets/images/htm-memory-addition-flow.svg +138 -0
  73. data/docs/assets/images/htm-memory-recall-flow.svg +152 -0
  74. data/docs/assets/images/htm-node-states.svg +123 -0
  75. data/docs/assets/images/project-structure.svg +78 -0
  76. data/docs/assets/images/test-directory-structure.svg +38 -0
  77. data/{dbdoc → docs/database}/README.md +127 -125
  78. data/docs/database/public.file_sources.md +42 -0
  79. data/docs/database/public.file_sources.svg +211 -0
  80. data/{dbdoc → docs/database}/public.node_tags.md +7 -8
  81. data/docs/database/public.node_tags.svg +239 -0
  82. data/{dbdoc → docs/database}/public.nodes.md +22 -17
  83. data/docs/database/public.nodes.svg +271 -0
  84. data/docs/database/public.robot_nodes.md +46 -0
  85. data/docs/database/public.robot_nodes.svg +243 -0
  86. data/{dbdoc → docs/database}/public.robots.md +2 -3
  87. data/docs/database/public.robots.svg +161 -0
  88. data/docs/database/public.tags.svg +139 -0
  89. data/{dbdoc → docs/database}/schema.json +941 -630
  90. data/docs/database/schema.svg +282 -0
  91. data/docs/development/index.md +1 -29
  92. data/docs/development/schema.md +134 -309
  93. data/docs/development/testing.md +1 -9
  94. data/docs/getting-started/index.md +47 -0
  95. data/docs/{installation.md → getting-started/installation.md} +2 -2
  96. data/docs/{quick-start.md → getting-started/quick-start.md} +5 -5
  97. data/docs/guides/adding-memories.md +295 -643
  98. data/docs/guides/recalling-memories.md +36 -1
  99. data/docs/guides/search-strategies.md +85 -51
  100. data/docs/images/htm-er-diagram.svg +156 -0
  101. data/docs/index.md +16 -31
  102. data/docs/multi_framework_support.md +4 -4
  103. data/examples/README.md +280 -0
  104. data/examples/basic_usage.rb +18 -16
  105. data/examples/cli_app/htm_cli.rb +146 -8
  106. data/examples/cli_app/temp.log +93 -0
  107. data/examples/custom_llm_configuration.rb +1 -2
  108. data/examples/example_app/app.rb +11 -14
  109. data/examples/file_loader_usage.rb +177 -0
  110. data/examples/robot_groups/lib/robot_group.rb +419 -0
  111. data/examples/robot_groups/lib/working_memory_channel.rb +140 -0
  112. data/examples/robot_groups/multi_process.rb +286 -0
  113. data/examples/robot_groups/robot_worker.rb +136 -0
  114. data/examples/robot_groups/same_process.rb +229 -0
  115. data/examples/sinatra_app/Gemfile +1 -0
  116. data/examples/sinatra_app/Gemfile.lock +166 -0
  117. data/examples/sinatra_app/app.rb +219 -24
  118. data/examples/timeframe_demo.rb +276 -0
  119. data/lib/htm/active_record_config.rb +10 -3
  120. data/lib/htm/circuit_breaker.rb +202 -0
  121. data/lib/htm/configuration.rb +313 -80
  122. data/lib/htm/database.rb +67 -36
  123. data/lib/htm/embedding_service.rb +39 -2
  124. data/lib/htm/errors.rb +131 -11
  125. data/lib/htm/{sinatra.rb → integrations/sinatra.rb} +87 -12
  126. data/lib/htm/job_adapter.rb +10 -3
  127. data/lib/htm/jobs/generate_embedding_job.rb +5 -4
  128. data/lib/htm/jobs/generate_tags_job.rb +4 -0
  129. data/lib/htm/loaders/markdown_loader.rb +263 -0
  130. data/lib/htm/loaders/paragraph_chunker.rb +112 -0
  131. data/lib/htm/long_term_memory.rb +601 -321
  132. data/lib/htm/models/file_source.rb +99 -0
  133. data/lib/htm/models/node.rb +116 -12
  134. data/lib/htm/models/robot.rb +53 -4
  135. data/lib/htm/models/robot_node.rb +51 -0
  136. data/lib/htm/models/tag.rb +302 -0
  137. data/lib/htm/observability.rb +395 -0
  138. data/lib/htm/tag_service.rb +60 -3
  139. data/lib/htm/tasks.rb +29 -0
  140. data/lib/htm/timeframe.rb +194 -0
  141. data/lib/htm/timeframe_extractor.rb +307 -0
  142. data/lib/htm/version.rb +1 -1
  143. data/lib/htm/working_memory.rb +165 -70
  144. data/lib/htm.rb +352 -133
  145. data/lib/tasks/doc.rake +300 -0
  146. data/lib/tasks/files.rake +299 -0
  147. data/lib/tasks/htm.rake +188 -2
  148. data/lib/tasks/jobs.rake +10 -12
  149. data/lib/tasks/tags.rake +194 -0
  150. data/mkdocs.yml +91 -9
  151. data/notes/ARCHITECTURE_REVIEW.md +1167 -0
  152. data/notes/IMPLEMENTATION_SUMMARY.md +606 -0
  153. data/notes/MULTI_FRAMEWORK_IMPLEMENTATION.md +451 -0
  154. data/notes/next_steps.md +100 -0
  155. data/notes/plan.md +627 -0
  156. data/notes/tag_ontology_enhancement_ideas.md +222 -0
  157. data/notes/timescaledb_removal_summary.md +200 -0
  158. metadata +177 -37
  159. data/db/migrate/20250101000002_create_robots.rb +0 -14
  160. data/db/migrate/20250101000003_create_nodes.rb +0 -42
  161. data/db/migrate/20250101000005_create_tags.rb +0 -38
  162. data/db/migrate/20250101000007_add_node_vector_indexes.rb +0 -30
  163. data/dbdoc/public.node_tags.svg +0 -112
  164. data/dbdoc/public.nodes.svg +0 -118
  165. data/dbdoc/public.robots.svg +0 -90
  166. data/dbdoc/public.tags.svg +0 -60
  167. data/dbdoc/schema.svg +0 -154
  168. data/{dbdoc → docs/database}/public.node_stats.md +0 -0
  169. data/{dbdoc → docs/database}/public.node_stats.svg +0 -0
  170. data/{dbdoc → docs/database}/public.nodes_tags.md +0 -0
  171. data/{dbdoc → docs/database}/public.nodes_tags.svg +0 -0
  172. data/{dbdoc → docs/database}/public.ontology_structure.md +0 -0
  173. data/{dbdoc → docs/database}/public.ontology_structure.svg +0 -0
  174. data/{dbdoc → docs/database}/public.operations_log.md +0 -0
  175. data/{dbdoc → docs/database}/public.operations_log.svg +0 -0
  176. data/{dbdoc → docs/database}/public.relationships.md +0 -0
  177. data/{dbdoc → docs/database}/public.relationships.svg +0 -0
  178. data/{dbdoc → docs/database}/public.robot_activity.md +0 -0
  179. data/{dbdoc → docs/database}/public.robot_activity.svg +0 -0
  180. data/{dbdoc → docs/database}/public.schema_migrations.md +0 -0
  181. data/{dbdoc → docs/database}/public.schema_migrations.svg +0 -0
  182. data/{dbdoc → docs/database}/public.tags.md +3 -3
  183. /data/{dbdoc → docs/database}/public.topic_relationships.md +0 -0
  184. /data/{dbdoc → docs/database}/public.topic_relationships.svg +0 -0
data/docs/api/database.md CHANGED
@@ -264,241 +264,27 @@ htm = HTM.new(db_config: HTM::Database.default_config)
264
264
 
265
265
  ## Database Schema
266
266
 
267
- ### Tables
267
+ For detailed database schema documentation, see:
268
268
 
269
- #### `nodes`
269
+ - **[Database Schema Documentation](../development/schema.md)** - Query patterns, optimization tips, and best practices
270
+ - **[Database Tables Overview](../database/README.md)** - Auto-generated table reference with ER diagram
270
271
 
271
- Primary memory storage table (hypertable partitioned by `created_at`).
272
+ ### Quick Reference
272
273
 
273
- ```sql
274
- CREATE TABLE nodes (
275
- id SERIAL PRIMARY KEY,
276
- key TEXT UNIQUE NOT NULL,
277
- value TEXT NOT NULL,
278
- type TEXT,
279
- category TEXT,
280
- importance REAL DEFAULT 1.0,
281
- token_count INTEGER DEFAULT 0,
282
- robot_id TEXT NOT NULL REFERENCES robots(id),
283
- embedding vector(1536),
284
- created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
285
- last_accessed TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
286
- in_working_memory BOOLEAN DEFAULT TRUE,
287
- evicted_at TIMESTAMPTZ
288
- );
289
-
290
- -- Indexes
291
- CREATE UNIQUE INDEX idx_nodes_key ON nodes(key);
292
- CREATE INDEX idx_nodes_created_at ON nodes(created_at DESC);
293
- CREATE INDEX idx_nodes_robot_id ON nodes(robot_id);
294
- CREATE INDEX idx_nodes_type ON nodes(type);
295
- CREATE INDEX idx_nodes_embedding ON nodes USING ivfflat (embedding vector_cosine_ops);
296
- CREATE INDEX idx_nodes_value_fts ON nodes USING GIN (to_tsvector('english', value));
297
- ```
298
-
299
- #### `relationships`
300
-
301
- Node relationship graph.
302
-
303
- ```sql
304
- CREATE TABLE relationships (
305
- id SERIAL PRIMARY KEY,
306
- from_node_id INTEGER NOT NULL REFERENCES nodes(id) ON DELETE CASCADE,
307
- to_node_id INTEGER NOT NULL REFERENCES nodes(id) ON DELETE CASCADE,
308
- relationship_type TEXT,
309
- strength REAL DEFAULT 1.0,
310
- created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
311
- UNIQUE(from_node_id, to_node_id, relationship_type)
312
- );
313
-
314
- -- Indexes
315
- CREATE INDEX idx_relationships_from ON relationships(from_node_id);
316
- CREATE INDEX idx_relationships_to ON relationships(to_node_id);
317
- ```
318
-
319
- #### `tags`
320
-
321
- Flexible tagging system.
322
-
323
- ```sql
324
- CREATE TABLE tags (
325
- id SERIAL PRIMARY KEY,
326
- node_id INTEGER NOT NULL REFERENCES nodes(id) ON DELETE CASCADE,
327
- tag TEXT NOT NULL,
328
- created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
329
- UNIQUE(node_id, tag)
330
- );
331
-
332
- -- Indexes
333
- CREATE INDEX idx_tags_node_id ON tags(node_id);
334
- CREATE INDEX idx_tags_tag ON tags(tag);
335
- ```
336
-
337
- #### `robots`
338
-
339
- Robot registry for multi-robot tracking.
340
-
341
- ```sql
342
- CREATE TABLE robots (
343
- id TEXT PRIMARY KEY,
344
- name TEXT NOT NULL,
345
- created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
346
- last_active TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
347
- );
348
- ```
349
-
350
- #### `operations_log`
351
-
352
- Audit log for all operations (hypertable partitioned by `timestamp`).
353
-
354
- ```sql
355
- CREATE TABLE operations_log (
356
- id SERIAL,
357
- operation TEXT NOT NULL,
358
- node_id INTEGER REFERENCES nodes(id) ON DELETE SET NULL,
359
- robot_id TEXT NOT NULL REFERENCES robots(id),
360
- timestamp TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
361
- details JSONB
362
- );
363
-
364
- -- Indexes
365
- CREATE INDEX idx_operations_log_timestamp ON operations_log(timestamp DESC);
366
- CREATE INDEX idx_operations_log_robot_id ON operations_log(robot_id);
367
- CREATE INDEX idx_operations_log_operation ON operations_log(operation);
368
- ```
369
-
370
- ### Views
371
-
372
- #### `node_stats`
373
-
374
- Aggregate statistics by node type.
375
-
376
- ```sql
377
- CREATE VIEW node_stats AS
378
- SELECT
379
- type,
380
- COUNT(*) as count,
381
- AVG(importance) as avg_importance,
382
- MIN(created_at) as oldest,
383
- MAX(created_at) as newest
384
- FROM nodes
385
- GROUP BY type;
386
- ```
387
-
388
- #### `robot_activity`
389
-
390
- Robot activity summary.
391
-
392
- ```sql
393
- CREATE VIEW robot_activity AS
394
- SELECT
395
- id,
396
- name,
397
- last_active,
398
- (SELECT COUNT(*) FROM nodes WHERE robot_id = robots.id) as node_count
399
- FROM robots
400
- ORDER BY last_active DESC;
401
- ```
402
-
403
- ---
404
-
405
- ## TimescaleDB Hypertables
406
-
407
- ### `nodes` Hypertable
408
-
409
- ```sql
410
- SELECT create_hypertable('nodes', 'created_at',
411
- if_not_exists => TRUE,
412
- migrate_data => TRUE
413
- );
414
- ```
415
-
416
- **Partitioning**: By `created_at` timestamp
417
-
418
- **Chunk Interval**: 7 days (default)
419
-
420
- **Compression**:
421
-
422
- ```sql
423
- ALTER TABLE nodes SET (
424
- timescaledb.compress,
425
- timescaledb.compress_segmentby = 'robot_id,type'
426
- );
427
-
428
- SELECT add_compression_policy('nodes', INTERVAL '30 days',
429
- if_not_exists => TRUE
430
- );
431
- ```
432
-
433
- - Automatically compresses chunks older than 30 days
434
- - Segments by `robot_id` and `type` for efficient queries
435
- - Reduces storage by ~90% for old data
436
-
437
- ### `operations_log` Hypertable
438
-
439
- ```sql
440
- SELECT create_hypertable('operations_log', 'timestamp',
441
- if_not_exists => TRUE,
442
- migrate_data => TRUE
443
- );
444
- ```
445
-
446
- **Partitioning**: By `timestamp`
447
-
448
- **Chunk Interval**: 1 day (default)
449
-
450
- **Benefits**:
451
-
452
- - Fast time-range queries
453
- - Automatic data retention policies (can be added)
454
- - Optimized for append-only workload
455
-
456
- ---
457
-
458
- ## Required PostgreSQL Extensions
459
-
460
- ### TimescaleDB
461
-
462
- Time-series database extension.
463
-
464
- ```sql
465
- CREATE EXTENSION IF NOT EXISTS timescaledb;
466
- ```
467
-
468
- **Features Used**:
469
-
470
- - Hypertables for time-series optimization
471
- - Automatic chunking and partitioning
472
- - Compression policies
473
- - Continuous aggregates (planned)
474
-
475
- ### pgvector
476
-
477
- Vector similarity search.
478
-
479
- ```sql
480
- CREATE EXTENSION IF NOT EXISTS vector;
481
- ```
482
-
483
- **Features Used**:
484
-
485
- - `vector(1536)` data type for embeddings
486
- - Cosine similarity operator `<=>`
487
- - IVFFlat index for approximate nearest neighbor
488
-
489
- ### pg_trgm
490
-
491
- Trigram-based text search.
492
-
493
- ```sql
494
- CREATE EXTENSION IF NOT EXISTS pg_trgm;
495
- ```
274
+ | Table | Purpose |
275
+ |-------|---------|
276
+ | [robots](../database/public.robots.md) | Robot registry for multi-robot tracking |
277
+ | [nodes](../database/public.nodes.md) | Primary memory storage with vector embeddings |
278
+ | [tags](../database/public.tags.md) | Hierarchical tag names for categorization |
279
+ | [robot_nodes](../database/public.robot_nodes.md) | Robot-to-node associations (hive mind, working memory) |
280
+ | [node_tags](../database/public.node_tags.md) | Node-to-tag associations |
496
281
 
497
- **Features Used**:
282
+ ### Required Extensions
498
283
 
499
- - Full-text search with fuzzy matching
500
- - GIN indexes for fast text queries
501
- - Similarity ranking
284
+ | Extension | Purpose |
285
+ |-----------|---------|
286
+ | `pgvector` | Vector similarity search with HNSW indexes |
287
+ | `pg_trgm` | Trigram-based fuzzy text matching |
502
288
 
503
289
  ---
504
290
 
@@ -801,6 +587,7 @@ end
801
587
 
802
588
  - [HTM API](htm.md) - Main class that uses Database config
803
589
  - [LongTermMemory API](long-term-memory.md) - Uses database for storage
804
- - [Database Schema](../development/schema.md) - Complete schema documentation
805
- - [TimescaleDB Documentation](https://docs.timescale.com/) - Hypertable features
590
+ - [Database Schema](../development/schema.md) - Query patterns, optimization tips, and best practices
591
+ - [Database Tables](../database/README.md) - Auto-generated table reference with ER diagram
806
592
  - [pgvector Documentation](https://github.com/pgvector/pgvector) - Vector search
593
+ - [pg_trgm Documentation](https://www.postgresql.org/docs/current/pgtrgm.html) - Trigram fuzzy matching
@@ -353,13 +353,7 @@ system("ollama pull nomic-embed-text")
353
353
 
354
354
  ### Exception Types
355
355
 
356
- ```ruby
357
- HTM::EmbeddingError
358
- ├─ "Ollama connection failed"
359
- ├─ "OpenAI API error: ..."
360
- ├─ "Invalid model: ..."
361
- └─ "Empty text provided"
362
- ```
356
+ ![HTM Exception Hierarchy](../assets/images/exception-hierarchy.svg)
363
357
 
364
358
  ---
365
359