htm 0.0.32 → 0.1.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/.architecture/decisions/adrs/005-rag-based-retrieval-with-hybrid-search.md +4 -0
- data/.architecture/decisions/adrs/017-bitemporal-event-time.md +78 -0
- data/.architecture/decisions/adrs/018-memory-timelines-and-node-links.md +64 -0
- data/.architecture/decisions/adrs/019-epistemic-status.md +66 -0
- data/.claude.json +11 -0
- data/.envrc +9 -7
- data/.irbrc +14 -14
- data/.reek.yml +104 -0
- data/.rubocop.yml +0 -11
- data/.ruby-version +1 -0
- data/CHANGELOG.md +160 -98
- data/README.md +22 -17
- data/SETUP.md +49 -46
- data/_typos.toml +17 -0
- data/bin/htm +18 -0
- data/db/migrate/00010_convert_embedding_to_sparsevec.rb +47 -0
- data/db/migrate/00011_strip_frontmatter_from_chunks.rb +66 -0
- data/db/migrate/00012_add_event_time_to_nodes.rb +110 -0
- data/db/migrate/00013_create_node_links.rb +158 -0
- data/db/migrate/00014_add_epistemic_status.rb +110 -0
- data/db/schema.sql +238 -7
- data/db/seed_data/README.md +11 -11
- data/db/seeds.rb +11 -11
- data/docs/api/database.md +8 -8
- data/docs/api/embedding-service.md +138 -262
- data/docs/api/htm.md +128 -12
- data/docs/api/index.md +8 -8
- data/docs/api/long-term-memory.md +8 -19
- data/docs/api/working-memory.md +34 -46
- data/docs/api/yard/HTM/Admin/Console.md +53 -0
- data/docs/api/yard/HTM/Admin/DatabaseCommands/Console.md +38 -0
- data/docs/api/yard/HTM/Admin/DatabaseCommands.md +127 -0
- data/docs/api/yard/HTM/Admin/EnrichCommands.md +85 -0
- data/docs/api/yard/HTM/Admin/Error.md +9 -0
- data/docs/api/yard/HTM/Admin/FileCommands/Console.md +50 -0
- data/docs/api/yard/HTM/Admin/FileCommands.md +83 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/Base.md +17 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/Edit.md +74 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/Search.md +77 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/TagSearch.md +46 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands/WorkingMemory.md +36 -0
- data/docs/api/yard/HTM/Admin/MemoryCommands.md +67 -0
- data/docs/api/yard/HTM/Admin/TagCommands/Console.md +48 -0
- data/docs/api/yard/HTM/Admin/TagCommands.md +105 -0
- data/docs/api/yard/HTM/Admin/TagGraph.md +149 -0
- data/docs/api/yard/HTM/Admin/YardDocs.md +60 -0
- data/docs/api/yard/HTM/Admin.md +102 -0
- data/docs/api/yard/HTM/AuthorizationError.md +6 -6
- data/docs/api/yard/HTM/CLI/ConfigFile.md +40 -0
- data/docs/api/yard/HTM/CLI/Db.md +63 -0
- data/docs/api/yard/HTM/CLI/Enrich.md +39 -0
- data/docs/api/yard/HTM/CLI/ErrorSuggestion.md +56 -0
- data/docs/api/yard/HTM/CLI/Files.md +36 -0
- data/docs/api/yard/HTM/CLI/Group/ClassMethods.md +11 -0
- data/docs/api/yard/HTM/CLI/Group.md +13 -0
- data/docs/api/yard/HTM/CLI/MemoryCommand/ClassMethods.md +20 -0
- data/docs/api/yard/HTM/CLI/MemoryCommand.md +9 -0
- data/docs/api/yard/HTM/CLI/Robots.md +31 -0
- data/docs/api/yard/HTM/CLI/Support.md +9 -0
- data/docs/api/yard/HTM/CLI/Tags.md +57 -0
- data/docs/api/yard/HTM/CLI/WorkingMemory.md +24 -0
- data/docs/api/yard/HTM/CLI.md +84 -0
- data/docs/api/yard/HTM/CircuitBreaker/Guarded.md +29 -0
- data/docs/api/yard/HTM/CircuitBreaker/Settings.md +21 -0
- data/docs/api/yard/HTM/CircuitBreaker/Status.md +42 -0
- data/docs/api/yard/HTM/CircuitBreaker.md +54 -49
- data/docs/api/yard/HTM/CircuitBreakerOpenError.md +9 -9
- data/docs/api/yard/HTM/Config/Builder.md +87 -0
- data/docs/api/yard/HTM/Config/Coercion.md +59 -0
- data/docs/api/yard/HTM/Config/Database.md +102 -0
- data/docs/api/yard/HTM/Config/Readers.md +30 -0
- data/docs/api/yard/HTM/Config/RubyLlmSettings.md +32 -0
- data/docs/api/yard/HTM/Config/Validator.md +59 -0
- data/docs/api/yard/HTM/Config.md +224 -105
- data/docs/api/yard/HTM/ConfigurationError.md +20 -0
- data/docs/api/yard/HTM/Confirmation.md +22 -0
- data/docs/api/yard/HTM/Database/Connection.md +26 -0
- data/docs/api/yard/HTM/Database/Migrations.md +32 -0
- data/docs/api/yard/HTM/Database/Report.md +26 -0
- data/docs/api/yard/HTM/Database/SchemaFile.md +52 -0
- data/docs/api/yard/HTM/Database/Teardown.md +15 -0
- data/docs/api/yard/HTM/Database.md +58 -70
- data/docs/api/yard/HTM/DatabaseDocs.md +67 -0
- data/docs/api/yard/HTM/DatabaseError.md +6 -6
- data/docs/api/yard/HTM/Deletion.md +32 -0
- data/docs/api/yard/HTM/EmbeddingError.md +6 -6
- data/docs/api/yard/HTM/EmbeddingService.md +37 -38
- data/docs/api/yard/HTM/Epistemics.md +135 -0
- data/docs/api/yard/HTM/Error.md +6 -6
- data/docs/api/yard/HTM/EventTime.md +110 -0
- data/docs/api/yard/HTM/FileMemory.md +76 -0
- data/docs/api/yard/HTM/InputValidator.md +82 -0
- data/docs/api/yard/HTM/JobAdapter/Job.md +26 -0
- data/docs/api/yard/HTM/JobAdapter.md +35 -16
- data/docs/api/yard/HTM/Jobs/FailureLogging.md +14 -0
- data/docs/api/yard/HTM/Jobs/GenerateEmbeddingJob.md +16 -0
- data/docs/api/yard/HTM/Jobs/GeneratePropositionsJob.md +17 -0
- data/docs/api/yard/HTM/Jobs/GenerateRelationshipsJob.md +26 -0
- data/docs/api/yard/HTM/Jobs/GenerateTagsJob.md +17 -0
- data/docs/api/yard/HTM/Jobs/LlmCallTimer.md +58 -0
- data/docs/api/yard/HTM/Jobs/NodeLookup.md +14 -0
- data/docs/api/yard/HTM/Jobs.md +13 -0
- data/docs/api/yard/HTM/Loaders/ChunkSync/Chunk.md +22 -0
- data/docs/api/yard/HTM/Loaders/ChunkSync.md +50 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker/Coalescer.md +24 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker/Packer.md +20 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker/Splitter.md +18 -0
- data/docs/api/yard/HTM/Loaders/MarkdownChunker.md +83 -0
- data/docs/api/yard/HTM/Loaders/MarkdownLoader/Document.md +43 -0
- data/docs/api/yard/HTM/Loaders/MarkdownLoader.md +62 -0
- data/docs/api/yard/HTM/Loaders.md +5 -0
- data/docs/api/yard/HTM/LongTermMemory/FulltextSearch.md +36 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfMerger.md +25 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfRows.md +30 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfSql/Query.md +79 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfSql/Source.md +45 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/RrfSql.md +11 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch/TagDepth.md +52 -0
- data/docs/api/yard/HTM/LongTermMemory/HybridSearch.md +52 -0
- data/docs/api/yard/HTM/LongTermMemory/NodeOperations.md +95 -0
- data/docs/api/yard/HTM/LongTermMemory/RelevanceScorer/HierarchicalJaccard.md +58 -0
- data/docs/api/yard/HTM/LongTermMemory/RelevanceScorer/TagSearch.md +27 -0
- data/docs/api/yard/HTM/LongTermMemory/RelevanceScorer.md +81 -0
- data/docs/api/yard/HTM/LongTermMemory/RobotOperations.md +22 -0
- data/docs/api/yard/HTM/LongTermMemory/TagOperations/ExpiringValue.md +24 -0
- data/docs/api/yard/HTM/LongTermMemory/TagOperations/TagMatchSql.md +40 -0
- data/docs/api/yard/HTM/LongTermMemory/TagOperations.md +129 -0
- data/docs/api/yard/HTM/LongTermMemory/VectorSearch.md +30 -0
- data/docs/api/yard/HTM/LongTermMemory.md +279 -31
- data/docs/api/yard/HTM/MCP/AssembleContextTool.md +16 -0
- data/docs/api/yard/HTM/MCP/ClearGroupWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ClearWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ConfigResource.md +12 -0
- data/docs/api/yard/HTM/MCP/ConfirmTool.md +12 -0
- data/docs/api/yard/HTM/MCP/CreateGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/EnrichPendingTool.md +16 -0
- data/docs/api/yard/HTM/MCP/ExtractPropositionsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/FailoverTool.md +12 -0
- data/docs/api/yard/HTM/MCP/FileInfoTool.md +13 -0
- data/docs/api/yard/HTM/MCP/FileSourcesResource.md +12 -0
- data/docs/api/yard/HTM/MCP/FileSupport.md +7 -0
- data/docs/api/yard/HTM/MCP/FindByTopicTool.md +12 -0
- data/docs/api/yard/HTM/MCP/FirstMentionTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ForgetContentTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ForgetTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetGroupStatusTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetGroupWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetNodeTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetRobotTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GetWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GroupRecallTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GroupRememberTool.md +12 -0
- data/docs/api/yard/HTM/MCP/GroupSession.md +23 -0
- data/docs/api/yard/HTM/MCP/JoinGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/LeaveGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListDeletedTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListFilesTool.md +13 -0
- data/docs/api/yard/HTM/MCP/ListGroupsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListRobotsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ListTagsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/LoadDirectoryTool.md +13 -0
- data/docs/api/yard/HTM/MCP/LoadFileTool.md +13 -0
- data/docs/api/yard/HTM/MCP/MemoryStatsResource.md +13 -0
- data/docs/api/yard/HTM/MCP/PopularTagsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/PromoteRobotTool.md +12 -0
- data/docs/api/yard/HTM/MCP/PurgeDeletedTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RecallTool.md +16 -0
- data/docs/api/yard/HTM/MCP/RecentMemoriesResource.md +13 -0
- data/docs/api/yard/HTM/MCP/RelatedNodesTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RememberTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RestoreTool.md +12 -0
- data/docs/api/yard/HTM/MCP/RobotGroupsResource.md +13 -0
- data/docs/api/yard/HTM/MCP/RobotTeamsStatusTool.md +13 -0
- data/docs/api/yard/HTM/MCP/RunRobotTeamsTool.md +13 -0
- data/docs/api/yard/HTM/MCP/SearchByTagsTool.md +16 -0
- data/docs/api/yard/HTM/MCP/SearchTagsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/Server.md +37 -0
- data/docs/api/yard/HTM/MCP/Session.md +32 -0
- data/docs/api/yard/HTM/MCP/SetRobotTool.md +12 -0
- data/docs/api/yard/HTM/MCP/ShutdownGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/StartupPromptResource.md +16 -0
- data/docs/api/yard/HTM/MCP/StatsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/SyncFilesTool.md +13 -0
- data/docs/api/yard/HTM/MCP/SyncGroupTool.md +12 -0
- data/docs/api/yard/HTM/MCP/TagHierarchyResource.md +16 -0
- data/docs/api/yard/HTM/MCP/TagNodeTool.md +12 -0
- data/docs/api/yard/HTM/MCP/TagTreeTool.md +22 -0
- data/docs/api/yard/HTM/MCP/TeamNames.md +7 -0
- data/docs/api/yard/HTM/MCP/TimelineTool.md +12 -0
- data/docs/api/yard/HTM/MCP/Tool.md +24 -0
- data/docs/api/yard/HTM/MCP/ToolSupport.md +17 -0
- data/docs/api/yard/HTM/MCP/TopicRelationshipsTool.md +12 -0
- data/docs/api/yard/HTM/MCP/TransferWorkingMemoryTool.md +12 -0
- data/docs/api/yard/HTM/MCP/UnloadFileTool.md +13 -0
- data/docs/api/yard/HTM/MCP/UntagNodeTool.md +12 -0
- data/docs/api/yard/HTM/MCP.md +30 -0
- data/docs/api/yard/HTM/Migration.md +44 -0
- data/docs/api/yard/HTM/Models/FileSource.md +66 -0
- data/docs/api/yard/HTM/Models/Node/Scopes.md +74 -0
- data/docs/api/yard/HTM/Models/Node/SoftDeletion.md +22 -0
- data/docs/api/yard/HTM/Models/Node/VectorSimilarity.md +28 -0
- data/docs/api/yard/HTM/Models/Node.md +113 -0
- data/docs/api/yard/HTM/Models/NodeLink.md +65 -0
- data/docs/api/yard/HTM/Models/NodeRelationship.md +31 -0
- data/docs/api/yard/HTM/Models/NodeTag.md +38 -0
- data/docs/api/yard/HTM/Models/Robot.md +50 -0
- data/docs/api/yard/HTM/Models/RobotNode.md +35 -0
- data/docs/api/yard/HTM/Models/Tag/Scopes.md +32 -0
- data/docs/api/yard/HTM/Models/Tag.md +112 -0
- data/docs/api/yard/HTM/Models.md +5 -0
- data/docs/api/yard/HTM/NodeEnrichment.md +36 -0
- data/docs/api/yard/HTM/NodeView.md +113 -0
- data/docs/api/yard/HTM/NotFoundError.md +6 -5
- data/docs/api/yard/HTM/Observability.md +58 -40
- data/docs/api/yard/HTM/PropositionError.md +6 -6
- data/docs/api/yard/HTM/PropositionService.md +63 -29
- data/docs/api/yard/HTM/QueryCache/KeyNormalizer.md +25 -0
- data/docs/api/yard/HTM/QueryCache/NullStore.md +19 -0
- data/docs/api/yard/HTM/QueryCache/Store.md +43 -0
- data/docs/api/yard/HTM/QueryCache.md +41 -46
- data/docs/api/yard/HTM/QueryTimeoutError.md +9 -8
- data/docs/api/yard/HTM/Railtie.md +11 -7
- data/docs/api/yard/HTM/RecallQuery.md +111 -0
- data/docs/api/yard/HTM/ResourceExhaustedError.md +6 -6
- data/docs/api/yard/HTM/RobotGroup/CacheSync.md +35 -0
- data/docs/api/yard/HTM/RobotGroup/Membership.md +96 -0
- data/docs/api/yard/HTM/RobotGroup/TransferOptions.md +14 -0
- data/docs/api/yard/HTM/RobotGroup/WorkingMemoryLedger.md +65 -0
- data/docs/api/yard/HTM/RobotGroup.md +116 -360
- data/docs/api/yard/HTM/Robots/Actions/TagMove.md +31 -0
- data/docs/api/yard/HTM/Robots/Actions.md +153 -0
- data/docs/api/yard/HTM/Robots/Neighbors.md +20 -0
- data/docs/api/yard/HTM/Robots/Supervisor/Idler.md +26 -0
- data/docs/api/yard/HTM/Robots/Supervisor/Roster.md +26 -0
- data/docs/api/yard/HTM/Robots/Supervisor.md +42 -0
- data/docs/api/yard/HTM/Robots/Team.md +60 -0
- data/docs/api/yard/HTM/Robots/Teams/Archivist.md +43 -0
- data/docs/api/yard/HTM/Robots/Teams/Consolidator.md +29 -0
- data/docs/api/yard/HTM/Robots/Teams/ContradictionDetector.md +33 -0
- data/docs/api/yard/HTM/Robots/Teams/TagCurator.md +37 -0
- data/docs/api/yard/HTM/Robots/Teams.md +5 -0
- data/docs/api/yard/HTM/Robots/Tools/ArchiveNodes.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/Base.md +16 -0
- data/docs/api/yard/HTM/Robots/Tools/MergeNodes.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/MergeTags.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/Pass.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/RecordConflict.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools/SupersedeNode.md +12 -0
- data/docs/api/yard/HTM/Robots/Tools.md +14 -0
- data/docs/api/yard/HTM/Robots.md +72 -0
- data/docs/api/yard/HTM/SearchFilters.md +82 -0
- data/docs/api/yard/HTM/SequelConfig.md +97 -0
- data/docs/api/yard/HTM/Settings.md +39 -0
- data/docs/api/yard/HTM/Sinatra/Helpers.md +37 -0
- data/docs/api/yard/HTM/Sinatra/Middleware.md +29 -0
- data/docs/api/yard/HTM/Sinatra.md +44 -0
- data/docs/api/yard/HTM/SqlBuilder/Sparsevec.md +69 -0
- data/docs/api/yard/HTM/SqlBuilder.md +62 -75
- data/docs/api/yard/HTM/StartupPrompt.md +45 -0
- data/docs/api/yard/HTM/TagError.md +6 -6
- data/docs/api/yard/HTM/TagService.md +100 -37
- data/docs/api/yard/HTM/TagTree/Branch.md +43 -0
- data/docs/api/yard/HTM/TagTree/SvgRenderer/Layout.md +51 -0
- data/docs/api/yard/HTM/TagTree/SvgRenderer.md +25 -0
- data/docs/api/yard/HTM/TagTree.md +65 -0
- data/docs/api/yard/HTM/Telemetry/NullInstrument.md +12 -9
- data/docs/api/yard/HTM/Telemetry/NullMeter.md +17 -9
- data/docs/api/yard/HTM/Telemetry.md +53 -50
- data/docs/api/yard/HTM/Timeframe/Result.md +13 -16
- data/docs/api/yard/HTM/Timeframe.md +17 -20
- data/docs/api/yard/HTM/TimeframeExtractor/Patterns.md +18 -0
- data/docs/api/yard/HTM/TimeframeExtractor/Result.md +13 -16
- data/docs/api/yard/HTM/TimeframeExtractor.md +27 -16
- data/docs/api/yard/HTM/Timeline.md +113 -0
- data/docs/api/yard/HTM/ValidationError.md +6 -5
- data/docs/api/yard/HTM/Workflows/RememberWorkflow/NodeState.md +73 -0
- data/docs/api/yard/HTM/Workflows/RememberWorkflow.md +80 -0
- data/docs/api/yard/HTM/Workflows.md +5 -0
- data/docs/api/yard/HTM/WorkingMemory/Entry.md +67 -0
- data/docs/api/yard/HTM/WorkingMemory/Store.md +54 -0
- data/docs/api/yard/HTM/WorkingMemory.md +113 -87
- data/docs/api/yard/HTM/WorkingMemoryChannel/Subscribers.md +28 -0
- data/docs/api/yard/HTM/WorkingMemoryChannel.md +108 -102
- data/docs/api/yard/HTM.md +483 -5
- data/docs/api/yard/README.md +211 -0
- data/docs/api/yard/Sinatra/Base.md +30 -0
- data/docs/api/yard/Sinatra.md +7 -0
- data/docs/api/yard/index.csv +1834 -181
- data/docs/api/yard-reference.md +228 -9
- data/docs/architecture/adrs/005-rag-retrieval.md +3 -0
- data/docs/architecture/adrs/index.md +1 -1
- data/docs/architecture/index.md +2 -2
- data/docs/architecture/overview.md +3 -3
- data/docs/assets/images/project-structure.svg +2 -2
- data/docs/database/INDEX.md +113 -0
- data/docs/database/README.md +60 -176
- data/docs/database/file_sources.md +51 -0
- data/docs/database/file_sources.svg +120 -0
- data/docs/database/naming-convention.md +12 -12
- data/docs/database/node_links.md +65 -0
- data/docs/database/node_links.svg +171 -0
- data/docs/database/node_relationships.md +62 -0
- data/docs/database/node_relationships.svg +124 -0
- data/docs/database/node_tags.md +50 -0
- data/docs/database/node_tags.svg +125 -0
- data/docs/database/nodes.md +120 -0
- data/docs/database/nodes.svg +394 -0
- data/docs/database/robot_nodes.md +57 -0
- data/docs/database/robot_nodes.svg +155 -0
- data/docs/database/robots.md +40 -0
- data/docs/database/robots.svg +131 -0
- data/docs/database/schema.svg +384 -275
- data/docs/database/schema_migrations.md +19 -0
- data/docs/database/schema_migrations.svg +31 -0
- data/docs/database/tags.md +47 -0
- data/docs/database/tags.svg +90 -0
- data/docs/development/asgard-tasks.md +114 -0
- data/docs/development/contributing.md +6 -6
- data/docs/development/index.md +15 -17
- data/docs/development/schema.md +43 -14
- data/docs/development/setup.md +60 -70
- data/docs/development/testing.md +57 -65
- data/docs/examples/basic-usage.md +9 -9
- data/docs/examples/config-files.md +8 -8
- data/docs/examples/file-loading.md +12 -12
- data/docs/examples/index.md +15 -8
- data/docs/examples/llm-configuration.md +13 -9
- data/docs/examples/mcp-client.md +61 -59
- data/docs/examples/rails-integration.md +5 -4
- data/docs/examples/sinatra-integration.md +5 -4
- data/docs/examples/standalone-app.md +17 -16
- data/docs/getting-started/index.md +3 -3
- data/docs/getting-started/installation.md +76 -83
- data/docs/getting-started/quick-start.md +24 -22
- data/docs/guides/adding-memories.md +14 -3
- data/docs/guides/cli.md +458 -0
- data/docs/guides/configuration.md +72 -37
- data/docs/guides/epistemic-status.md +138 -0
- data/docs/guides/event-time.md +106 -0
- data/docs/guides/file-loading.md +26 -22
- data/docs/guides/getting-started.md +21 -17
- data/docs/guides/long-term-memory.md +1 -1
- data/docs/guides/mcp-server.md +416 -173
- data/docs/guides/propositions.md +18 -29
- data/docs/guides/recalling-memories.md +22 -7
- data/docs/guides/robot-teams.md +96 -0
- data/docs/guides/search-strategies.md +10 -7
- data/docs/guides/tags.md +19 -19
- data/docs/guides/timeline.md +106 -0
- data/docs/index.md +7 -7
- data/docs/llms.txt +35 -0
- data/docs/multi_framework_support.md +13 -13
- data/docs/robots/robot-groups.md +9 -9
- data/docs/robots/two-tier-memory.md +13 -26
- data/docs/setup_local_database.md +33 -31
- data/examples/00_create_examples_db.rb +20 -7
- data/examples/01_basic_usage.rb +20 -18
- data/examples/02_config_file_example/README.md +13 -12
- data/examples/02_config_file_example/config/htm.local.yml +8 -8
- data/examples/02_config_file_example/show_config.rb +3 -4
- data/examples/03_custom_llm_configuration.rb +12 -12
- data/examples/04_file_loader_usage.rb +15 -21
- data/examples/05_timeframe_demo.rb +282 -234
- data/examples/06_example_app/.loki +36 -0
- data/examples/06_example_app/app.rb +21 -33
- data/examples/07_cli_app/README.md +18 -18
- data/examples/07_cli_app/htm_cli.rb +25 -19
- data/examples/08_sinatra_app/Gemfile +1 -0
- data/examples/08_sinatra_app/Gemfile.lock +101 -67
- data/examples/08_sinatra_app/app.rb +64 -62
- data/examples/09_mcp_client.rb +92 -119
- data/examples/10_telemetry/README.md +3 -3
- data/examples/10_telemetry/SETUP_README.md +2 -2
- data/examples/10_telemetry/demo.rb +5 -5
- data/examples/11_robot_groups/README.md +5 -4
- data/examples/11_robot_groups/lib/robot_worker.rb +0 -5
- data/examples/11_robot_groups/multi_process.rb +5 -4
- data/examples/11_robot_groups/same_process.rb +18 -17
- data/examples/12_rails_app/Gemfile +1 -1
- data/examples/12_rails_app/Gemfile.lock +146 -97
- data/examples/12_rails_app/README.md +21 -17
- data/examples/12_rails_app/app/controllers/chats_controller.rb +33 -63
- data/examples/12_rails_app/app/controllers/memories_controller.rb +7 -1
- data/examples/12_rails_app/app/controllers/messages_controller.rb +21 -58
- data/examples/12_rails_app/app/controllers/search_controller.rb +7 -2
- data/examples/12_rails_app/app/models/chat.rb +0 -33
- data/examples/12_rails_app/app/models/message.rb +0 -2
- data/examples/12_rails_app/app/views/chats/show.html.erb +2 -2
- data/examples/12_rails_app/app/views/files/new.html.erb +1 -1
- data/examples/12_rails_app/app/views/memories/_memory_card.html.erb +1 -1
- data/examples/12_rails_app/app/views/memories/new.html.erb +12 -0
- data/examples/12_rails_app/app/views/memories/show.html.erb +13 -0
- data/examples/12_rails_app/app/views/search/index.html.erb +18 -2
- data/examples/12_rails_app/config/initializers/ruby_llm.rb +11 -15
- data/examples/12_rails_app/db/migrate/20261205061932_create_ruby_llm_records.rb +160 -0
- data/examples/12_rails_app/db/migrate/20261205061933_create_chats.rb +13 -0
- data/examples/12_rails_app/db/migrate/20261205061934_create_messages.rb +23 -0
- data/examples/12_rails_app/db/schema.rb +163 -20
- data/examples/README.md +53 -44
- data/examples/examples_helper.rb +29 -5
- data/htm.gemspec +68 -0
- data/lib/htm/admin/database_commands.rb +454 -0
- data/lib/htm/admin/enrich_commands.rb +209 -0
- data/lib/htm/admin/file_commands.rb +323 -0
- data/lib/htm/admin/memory_commands/edit.rb +157 -0
- data/lib/htm/admin/memory_commands/search.rb +178 -0
- data/lib/htm/admin/memory_commands/tag_search.rb +98 -0
- data/lib/htm/admin/memory_commands/text.rb +164 -0
- data/lib/htm/admin/memory_commands/working_memory.rb +71 -0
- data/lib/htm/admin/memory_commands.rb +179 -0
- data/lib/htm/admin/tag_commands.rb +352 -0
- data/lib/htm/admin/tag_graph.rb +290 -0
- data/lib/htm/admin/templates/tag_graph.html +295 -0
- data/lib/htm/admin/yard_docs.rb +206 -0
- data/lib/htm/admin.rb +253 -0
- data/lib/htm/circuit_breaker.rb +142 -68
- data/lib/htm/class_methods.rb +136 -0
- data/lib/htm/cli/config_file.rb +86 -0
- data/lib/htm/cli/db.rb +77 -0
- data/lib/htm/cli/enrich.rb +44 -0
- data/lib/htm/cli/environment.rb +74 -0
- data/lib/htm/cli/error_suggestion.rb +97 -0
- data/lib/htm/cli/files.rb +41 -0
- data/lib/htm/cli/group.rb +32 -0
- data/lib/htm/cli/memory.rb +320 -0
- data/lib/htm/cli/memory_command.rb +71 -0
- data/lib/htm/cli/robots.rb +71 -0
- data/lib/htm/cli/support.rb +46 -0
- data/lib/htm/cli/tags.rb +103 -0
- data/lib/htm/cli/working_memory.rb +41 -0
- data/lib/htm/cli.rb +75 -0
- data/lib/htm/config/builder.rb +99 -68
- data/lib/htm/config/coercion.rb +91 -0
- data/lib/htm/config/database.rb +81 -52
- data/lib/htm/config/defaults.yml +52 -16
- data/lib/htm/config/readers.rb +85 -0
- data/lib/htm/config/ruby_llm_settings.rb +58 -0
- data/lib/htm/config/validator.rb +45 -26
- data/lib/htm/config.rb +112 -297
- data/lib/htm/database.rb +305 -396
- data/lib/htm/database_docs.rb +149 -0
- data/lib/htm/embedding_service.rb +26 -66
- data/lib/htm/epistemics.rb +254 -0
- data/lib/htm/errors.rb +4 -4
- data/lib/htm/event_time.rb +201 -0
- data/lib/htm/file_memory.rb +106 -0
- data/lib/htm/input_validator.rb +158 -0
- data/lib/htm/integrations/sinatra.rb +43 -31
- data/lib/htm/job_adapter.rb +78 -83
- data/lib/htm/jobs/failure_logging.rb +49 -0
- data/lib/htm/jobs/generate_embedding_job.rb +21 -28
- data/lib/htm/jobs/generate_propositions_job.rb +56 -28
- data/lib/htm/jobs/generate_relationships_job.rb +28 -24
- data/lib/htm/jobs/generate_tags_job.rb +32 -37
- data/lib/htm/jobs/llm_call_timer.rb +53 -0
- data/lib/htm/jobs/node_lookup.rb +27 -0
- data/lib/htm/knowledge_memory.rb +153 -0
- data/lib/htm/loaders/chunk_sync.rb +168 -0
- data/lib/htm/loaders/markdown_chunker.rb +207 -36
- data/lib/htm/loaders/markdown_loader.rb +76 -120
- data/lib/htm/long_term_memory/fulltext_search.rb +15 -36
- data/lib/htm/long_term_memory/hybrid_search.rb +471 -544
- data/lib/htm/long_term_memory/node_operations.rb +110 -81
- data/lib/htm/long_term_memory/relevance_scorer.rb +232 -237
- data/lib/htm/long_term_memory/tag_operations.rb +201 -130
- data/lib/htm/long_term_memory/vector_search.rb +33 -61
- data/lib/htm/long_term_memory.rb +5 -17
- data/lib/htm/mcp/file_tools.rb +190 -0
- data/lib/htm/mcp/group_tools.rb +223 -233
- data/lib/htm/mcp/node_tools.rb +350 -0
- data/lib/htm/mcp/resources.rb +102 -28
- data/lib/htm/mcp/robot_team_tools.rb +119 -0
- data/lib/htm/mcp/server.rb +26 -14
- data/lib/htm/mcp/tag_tools.rb +130 -0
- data/lib/htm/mcp/tool.rb +40 -0
- data/lib/htm/mcp/tool_support.rb +88 -0
- data/lib/htm/mcp/tools.rb +232 -283
- data/lib/htm/models/file_source.rb +35 -20
- data/lib/htm/models/node.rb +287 -193
- data/lib/htm/models/node_link.rb +97 -0
- data/lib/htm/models/node_relationship.rb +3 -2
- data/lib/htm/models/node_tag.rb +22 -2
- data/lib/htm/models/robot.rb +4 -8
- data/lib/htm/models/robot_node.rb +5 -2
- data/lib/htm/models/tag.rb +53 -207
- data/lib/htm/node_enrichment.rb +75 -0
- data/lib/htm/node_view.rb +295 -0
- data/lib/htm/observability.rb +114 -98
- data/lib/htm/proposition_service.rb +38 -74
- data/lib/htm/query_cache.rb +145 -91
- data/lib/htm/railtie.rb +3 -7
- data/lib/htm/request_options.rb +149 -0
- data/lib/htm/robot_group/cache_sync.rb +69 -0
- data/lib/htm/robot_group/membership.rb +147 -0
- data/lib/htm/robot_group/working_memory_ledger.rb +111 -0
- data/lib/htm/robot_group.rb +128 -521
- data/lib/htm/robots/actions.rb +371 -0
- data/lib/htm/robots/neighbors.rb +63 -0
- data/lib/htm/robots/node_brief.rb +49 -0
- data/lib/htm/robots/supervisor.rb +140 -0
- data/lib/htm/robots/team.rb +124 -0
- data/lib/htm/robots/teams/archivist.rb +88 -0
- data/lib/htm/robots/teams/consolidator.rb +52 -0
- data/lib/htm/robots/teams/contradiction_detector.rb +67 -0
- data/lib/htm/robots/teams/tag_curator.rb +75 -0
- data/lib/htm/robots/tools.rb +127 -0
- data/lib/htm/robots.rb +125 -0
- data/lib/htm/search_filters.rb +116 -0
- data/lib/htm/sequel_config.rb +95 -59
- data/lib/htm/sql_builder.rb +162 -53
- data/lib/htm/startup_prompt.rb +73 -0
- data/lib/htm/tag_service.rb +104 -109
- data/lib/htm/tag_tree.rb +206 -0
- data/lib/htm/telemetry.rb +15 -16
- data/lib/htm/timeframe.rb +27 -9
- data/lib/htm/timeframe_extractor.rb +77 -55
- data/lib/htm/timeline.rb +230 -0
- data/lib/htm/version.rb +1 -1
- data/lib/htm/workflows/remember_workflow.rb +145 -136
- data/lib/htm/working_memory/entry.rb +53 -0
- data/lib/htm/working_memory/store.rb +97 -0
- data/lib/htm/working_memory.rb +137 -139
- data/lib/htm/working_memory_channel.rb +90 -40
- data/lib/htm.rb +177 -501
- data/mkdocs.yml +100 -23
- data/scripts/install_local_database.sh +7 -6
- metadata +1032 -594
- data/.tbls.yml +0 -31
- data/Rakefile +0 -240
- data/bin/htm_mcp +0 -27
- data/docs/api/yard/HTM/ActiveRecordConfig.md +0 -29
- data/docs/api/yard/HTM/ConfigSection.md +0 -28
- data/docs/database/public.file_sources.md +0 -42
- data/docs/database/public.file_sources.svg +0 -219
- data/docs/database/public.node_stats.md +0 -48
- data/docs/database/public.node_stats.svg +0 -41
- data/docs/database/public.node_tags.md +0 -41
- data/docs/database/public.node_tags.svg +0 -251
- data/docs/database/public.nodes.md +0 -61
- data/docs/database/public.nodes.svg +0 -283
- data/docs/database/public.nodes_tags.md +0 -39
- data/docs/database/public.nodes_tags.svg +0 -112
- data/docs/database/public.ontology_structure.md +0 -48
- data/docs/database/public.ontology_structure.svg +0 -38
- data/docs/database/public.operations_log.md +0 -42
- data/docs/database/public.operations_log.svg +0 -130
- data/docs/database/public.relationships.md +0 -39
- data/docs/database/public.relationships.svg +0 -41
- data/docs/database/public.robot_activity.md +0 -46
- data/docs/database/public.robot_activity.svg +0 -35
- data/docs/database/public.robot_nodes.md +0 -48
- data/docs/database/public.robot_nodes.svg +0 -251
- data/docs/database/public.robots.md +0 -34
- data/docs/database/public.robots.svg +0 -165
- data/docs/database/public.schema_migrations.md +0 -29
- data/docs/database/public.schema_migrations.svg +0 -26
- data/docs/database/public.tags.md +0 -38
- data/docs/database/public.tags.svg +0 -147
- data/docs/database/public.topic_relationships.md +0 -45
- data/docs/database/public.topic_relationships.svg +0 -32
- data/docs/database/schema.json +0 -1814
- data/docs/database_rake_tasks.md +0 -345
- data/docs/development/rake-tasks.md +0 -1113
- data/docs/using_rake_tasks_in_your_app.md +0 -383
- data/examples/06_example_app/Rakefile +0 -71
- data/examples/12_rails_app/app/models/model.rb +0 -5
- data/examples/12_rails_app/app/models/tool_call.rb +0 -5
- data/examples/12_rails_app/db/migrate/20250124000001_create_ruby_llm_tables.rb +0 -34
- data/examples/12_rails_app/db/migrate/20250124000002_create_models_table.rb +0 -28
- data/lib/htm/mcp/cli.rb +0 -583
- data/lib/htm/tasks.rb +0 -79
- data/lib/tasks/db.rake +0 -15
- data/lib/tasks/doc.rake +0 -301
- data/lib/tasks/files.rake +0 -285
- data/lib/tasks/htm.rake +0 -703
- data/lib/tasks/jobs.rake +0 -429
- data/lib/tasks/tags.rake +0 -184
- data/scripts/backfill_parent_tags.rb +0 -376
- data/scripts/normalize_plural_tags.rb +0 -335
data/lib/htm/robot_group.rb
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
|
-
# examples/robot_groups/lib/htm/robot_group.rb
|
|
2
1
|
# frozen_string_literal: true
|
|
3
2
|
|
|
3
|
+
require 'forwardable'
|
|
4
|
+
require_relative 'robot_group/membership'
|
|
5
|
+
require_relative 'robot_group/working_memory_ledger'
|
|
6
|
+
require_relative 'robot_group/cache_sync'
|
|
7
|
+
|
|
4
8
|
class HTM
|
|
5
9
|
# Coordinates multiple robots with shared working memory and automatic failover.
|
|
6
10
|
#
|
|
@@ -38,18 +42,67 @@ class HTM
|
|
|
38
42
|
# @see HTM::WorkingMemoryChannel Low-level pub/sub mechanism
|
|
39
43
|
#
|
|
40
44
|
class RobotGroup
|
|
45
|
+
extend Forwardable
|
|
46
|
+
|
|
41
47
|
# Name of the robot group
|
|
42
48
|
# @return [String]
|
|
43
49
|
attr_reader :name
|
|
44
50
|
|
|
45
|
-
# Maximum token budget for working memory
|
|
46
|
-
# @return [Integer]
|
|
47
|
-
attr_reader :max_tokens
|
|
48
|
-
|
|
49
51
|
# The pub/sub channel used for real-time synchronization
|
|
50
52
|
# @return [HTM::WorkingMemoryChannel]
|
|
51
53
|
attr_reader :channel
|
|
52
54
|
|
|
55
|
+
# @!method max_tokens
|
|
56
|
+
# Maximum token budget for working memory
|
|
57
|
+
# @return [Integer]
|
|
58
|
+
# @!method member?(robot_name)
|
|
59
|
+
# @return [Boolean] true if robot_name is an active or passive member
|
|
60
|
+
# @!method active?(robot_name)
|
|
61
|
+
# @return [Boolean] true if robot_name is an active member
|
|
62
|
+
# @!method passive?(robot_name)
|
|
63
|
+
# @return [Boolean] true if robot_name is a passive member
|
|
64
|
+
# @!method member_ids
|
|
65
|
+
# @return [Array<Integer>] Database IDs of all members
|
|
66
|
+
# @!method active_robot_names
|
|
67
|
+
# @return [Array<String>] Names of the active members
|
|
68
|
+
# @!method passive_robot_names
|
|
69
|
+
# @return [Array<String>] Names of the passive members
|
|
70
|
+
# @!method remove(robot_name)
|
|
71
|
+
# Removes a robot from the group and clears its working memory flags.
|
|
72
|
+
# Does nothing if robot_name is not a member.
|
|
73
|
+
# @return [Integer, nil]
|
|
74
|
+
# @!method promote(robot_name)
|
|
75
|
+
# Promotes a passive robot to active status.
|
|
76
|
+
# @raise [ArgumentError] if robot_name is not a passive member
|
|
77
|
+
# @!method demote(robot_name)
|
|
78
|
+
# Demotes an active robot to passive status.
|
|
79
|
+
# @raise [ArgumentError] if robot_name is not active or is the last active robot
|
|
80
|
+
# @!method failover!
|
|
81
|
+
# Promotes the first passive robot to active.
|
|
82
|
+
# @return [String] Name of the promoted robot
|
|
83
|
+
# @raise [RuntimeError] if no passive robots are available
|
|
84
|
+
def_delegators :@members, :max_tokens, :member?, :active?, :passive?, :member_ids,
|
|
85
|
+
:active_robot_names, :passive_robot_names, :remove, :promote, :demote, :failover!
|
|
86
|
+
|
|
87
|
+
# @!method working_memory_contents
|
|
88
|
+
# Nodes in any member's working memory, newest first.
|
|
89
|
+
# @return [Sequel::Dataset]
|
|
90
|
+
# @!method in_sync?
|
|
91
|
+
# @return [Boolean] true when every member's working memory holds the same nodes
|
|
92
|
+
def_delegator :ledger, :contents, :working_memory_contents
|
|
93
|
+
def_delegator :ledger, :in_sync?
|
|
94
|
+
|
|
95
|
+
# @!method member(robot_name)
|
|
96
|
+
# A member's HTM instance, for its in-memory working memory.
|
|
97
|
+
# @param robot_name [String]
|
|
98
|
+
# @return [HTM]
|
|
99
|
+
# @raise [ArgumentError] if robot_name is not a member
|
|
100
|
+
def_delegator :@members, :fetch!, :member
|
|
101
|
+
|
|
102
|
+
# @!method sync_stats
|
|
103
|
+
# @return [Hash] Copy of { nodes_synced:, evictions_synced: }
|
|
104
|
+
def_delegator :@sync, :stats, :sync_stats
|
|
105
|
+
|
|
53
106
|
# Creates a new robot group with optional initial members.
|
|
54
107
|
#
|
|
55
108
|
# Initializes the group, sets up the PostgreSQL pub/sub channel for real-time
|
|
@@ -59,7 +112,7 @@ class HTM
|
|
|
59
112
|
# @param active [Array<String>] Names of robots to add as active members
|
|
60
113
|
# @param passive [Array<String>] Names of robots to add as passive (standby) members
|
|
61
114
|
# @param max_tokens [Integer] Maximum token budget for shared working memory
|
|
62
|
-
# @param db_config [Hash
|
|
115
|
+
# @param db_config [Hash] PostgreSQL connection config (defaults to HTM::Database.default_config)
|
|
63
116
|
#
|
|
64
117
|
# @example Create a group with one active and one passive robot
|
|
65
118
|
# group = HTM::RobotGroup.new(
|
|
@@ -74,27 +127,18 @@ class HTM
|
|
|
74
127
|
# group.add_active('agent-1')
|
|
75
128
|
# group.add_passive('agent-2')
|
|
76
129
|
#
|
|
77
|
-
def initialize(name:, active: [], passive: [], max_tokens: 4000, db_config:
|
|
78
|
-
@name
|
|
79
|
-
@
|
|
80
|
-
@
|
|
81
|
-
@
|
|
82
|
-
@sync_stats = { nodes_synced: 0, evictions_synced: 0 }
|
|
83
|
-
@mutex = Mutex.new
|
|
84
|
-
|
|
85
|
-
# Setup pub/sub channel for real-time sync
|
|
86
|
-
@db_config = db_config || HTM::Database.default_config
|
|
87
|
-
@channel = HTM::WorkingMemoryChannel.new(name, @db_config)
|
|
130
|
+
def initialize(name:, active: [], passive: [], max_tokens: 4000, db_config: HTM::Database.default_config)
|
|
131
|
+
@name = name
|
|
132
|
+
@members = Membership.new(max_tokens: max_tokens)
|
|
133
|
+
@sync = CacheSync.new(@members)
|
|
134
|
+
@channel = HTM::WorkingMemoryChannel.new(name, db_config)
|
|
88
135
|
|
|
89
|
-
#
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
# Start listening for notifications
|
|
136
|
+
# Apply other robots' working memory changes to our members' caches
|
|
137
|
+
@channel.on_change { |event, node_id, origin_robot_id| @sync.handle(event, node_id, origin_robot_id) }
|
|
93
138
|
@channel.start_listening
|
|
94
139
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
passive.each { |robot_name| add_passive(robot_name) }
|
|
140
|
+
active.each { add_active(it) }
|
|
141
|
+
passive.each { add_passive(it) }
|
|
98
142
|
end
|
|
99
143
|
|
|
100
144
|
# Shuts down the group by stopping the listener thread.
|
|
@@ -104,15 +148,10 @@ class HTM
|
|
|
104
148
|
#
|
|
105
149
|
# @return [void]
|
|
106
150
|
#
|
|
107
|
-
# @example
|
|
108
|
-
# group.shutdown
|
|
109
|
-
#
|
|
110
151
|
def shutdown
|
|
111
152
|
@channel.stop_listening
|
|
112
153
|
end
|
|
113
154
|
|
|
114
|
-
# @!group Membership Management
|
|
115
|
-
|
|
116
155
|
# Adds a robot as an active member of the group.
|
|
117
156
|
#
|
|
118
157
|
# Active robots can add memories and respond to queries. The new robot
|
|
@@ -122,182 +161,18 @@ class HTM
|
|
|
122
161
|
# @return [Integer] The robot's database ID
|
|
123
162
|
# @raise [ArgumentError] if robot_name is already a member
|
|
124
163
|
#
|
|
125
|
-
|
|
126
|
-
# robot_id = group.add_active('new-agent')
|
|
127
|
-
# puts "Added robot with ID: #{robot_id}"
|
|
128
|
-
#
|
|
129
|
-
def add_active(robot_name)
|
|
130
|
-
raise ArgumentError, "#{robot_name} is already a member" if member?(robot_name)
|
|
131
|
-
|
|
132
|
-
htm = HTM.new(robot_name: robot_name, working_memory_size: @max_tokens)
|
|
133
|
-
@active_robots[robot_name] = htm
|
|
134
|
-
|
|
135
|
-
# Sync existing shared working memory to new member
|
|
136
|
-
sync_robot(robot_name) if member_ids.length > 1
|
|
137
|
-
|
|
138
|
-
htm.robot_id
|
|
139
|
-
end
|
|
164
|
+
def add_active(robot_name) = enrolled(@members.add_active(robot_name))
|
|
140
165
|
|
|
141
166
|
# Adds a robot as a passive (standby) member of the group.
|
|
142
167
|
#
|
|
143
|
-
# Passive robots maintain synchronized working memory but don't
|
|
144
|
-
#
|
|
168
|
+
# Passive robots maintain synchronized working memory but don't handle
|
|
169
|
+
# queries directly. They serve as hot standbys for failover.
|
|
145
170
|
#
|
|
146
171
|
# @param robot_name [String] Unique name for the robot
|
|
147
172
|
# @return [Integer] The robot's database ID
|
|
148
173
|
# @raise [ArgumentError] if robot_name is already a member
|
|
149
174
|
#
|
|
150
|
-
|
|
151
|
-
# robot_id = group.add_passive('standby-agent')
|
|
152
|
-
#
|
|
153
|
-
def add_passive(robot_name)
|
|
154
|
-
raise ArgumentError, "#{robot_name} is already a member" if member?(robot_name)
|
|
155
|
-
|
|
156
|
-
htm = HTM.new(robot_name: robot_name, working_memory_size: @max_tokens)
|
|
157
|
-
@passive_robots[robot_name] = htm
|
|
158
|
-
|
|
159
|
-
# Sync existing shared working memory to new member
|
|
160
|
-
sync_robot(robot_name) if member_ids.length > 1
|
|
161
|
-
|
|
162
|
-
htm.robot_id
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
# Removes a robot from the group.
|
|
166
|
-
#
|
|
167
|
-
# Clears the robot's working memory flags in the database. The robot can
|
|
168
|
-
# be either active or passive.
|
|
169
|
-
#
|
|
170
|
-
# @param robot_name [String] Name of the robot to remove
|
|
171
|
-
# @return [void]
|
|
172
|
-
#
|
|
173
|
-
# @example
|
|
174
|
-
# group.remove('departing-agent')
|
|
175
|
-
#
|
|
176
|
-
def remove(robot_name)
|
|
177
|
-
htm = @active_robots.delete(robot_name) || @passive_robots.delete(robot_name)
|
|
178
|
-
return unless htm
|
|
179
|
-
|
|
180
|
-
# Clear working memory flags for this robot
|
|
181
|
-
HTM::Models::RobotNode
|
|
182
|
-
.where(robot_id: htm.robot_id, working_memory: true)
|
|
183
|
-
.update(working_memory: false)
|
|
184
|
-
end
|
|
185
|
-
|
|
186
|
-
# Promotes a passive robot to active status.
|
|
187
|
-
#
|
|
188
|
-
# The robot retains its synchronized working memory and becomes eligible
|
|
189
|
-
# to handle queries and add memories.
|
|
190
|
-
#
|
|
191
|
-
# @param robot_name [String] Name of the passive robot to promote
|
|
192
|
-
# @return [void]
|
|
193
|
-
# @raise [ArgumentError] if robot_name is not a passive member
|
|
194
|
-
#
|
|
195
|
-
# @example
|
|
196
|
-
# group.promote('standby-agent')
|
|
197
|
-
# group.active?('standby-agent') # => true
|
|
198
|
-
#
|
|
199
|
-
def promote(robot_name)
|
|
200
|
-
raise ArgumentError, "#{robot_name} is not a passive member" unless passive?(robot_name)
|
|
201
|
-
|
|
202
|
-
htm = @passive_robots.delete(robot_name)
|
|
203
|
-
@active_robots[robot_name] = htm
|
|
204
|
-
end
|
|
205
|
-
|
|
206
|
-
# Demotes an active robot to passive status.
|
|
207
|
-
#
|
|
208
|
-
# The robot retains its working memory but stops handling queries.
|
|
209
|
-
# Cannot demote the last active robot.
|
|
210
|
-
#
|
|
211
|
-
# @param robot_name [String] Name of the active robot to demote
|
|
212
|
-
# @return [void]
|
|
213
|
-
# @raise [ArgumentError] if robot_name is not an active member
|
|
214
|
-
# @raise [ArgumentError] if this is the last active robot
|
|
215
|
-
#
|
|
216
|
-
# @example
|
|
217
|
-
# group.demote('primary-agent')
|
|
218
|
-
# group.passive?('primary-agent') # => true
|
|
219
|
-
#
|
|
220
|
-
def demote(robot_name)
|
|
221
|
-
raise ArgumentError, "#{robot_name} is not an active member" unless active?(robot_name)
|
|
222
|
-
raise ArgumentError, 'Cannot demote last active robot' if @active_robots.length == 1
|
|
223
|
-
|
|
224
|
-
htm = @active_robots.delete(robot_name)
|
|
225
|
-
@passive_robots[robot_name] = htm
|
|
226
|
-
end
|
|
227
|
-
|
|
228
|
-
# Checks if a robot is a member of this group.
|
|
229
|
-
#
|
|
230
|
-
# @param robot_name [String] Name of the robot to check
|
|
231
|
-
# @return [Boolean] true if the robot is an active or passive member
|
|
232
|
-
#
|
|
233
|
-
# @example
|
|
234
|
-
# group.member?('agent-1') # => true
|
|
235
|
-
# group.member?('unknown') # => false
|
|
236
|
-
#
|
|
237
|
-
def member?(robot_name)
|
|
238
|
-
@active_robots.key?(robot_name) || @passive_robots.key?(robot_name)
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
# Checks if a robot is an active member of this group.
|
|
242
|
-
#
|
|
243
|
-
# @param robot_name [String] Name of the robot to check
|
|
244
|
-
# @return [Boolean] true if the robot is an active member
|
|
245
|
-
#
|
|
246
|
-
# @example
|
|
247
|
-
# group.active?('primary-agent') # => true
|
|
248
|
-
#
|
|
249
|
-
def active?(robot_name)
|
|
250
|
-
@active_robots.key?(robot_name)
|
|
251
|
-
end
|
|
252
|
-
|
|
253
|
-
# Checks if a robot is a passive member of this group.
|
|
254
|
-
#
|
|
255
|
-
# @param robot_name [String] Name of the robot to check
|
|
256
|
-
# @return [Boolean] true if the robot is a passive member
|
|
257
|
-
#
|
|
258
|
-
# @example
|
|
259
|
-
# group.passive?('standby-agent') # => true
|
|
260
|
-
#
|
|
261
|
-
def passive?(robot_name)
|
|
262
|
-
@passive_robots.key?(robot_name)
|
|
263
|
-
end
|
|
264
|
-
|
|
265
|
-
# Returns database IDs of all group members.
|
|
266
|
-
#
|
|
267
|
-
# @return [Array<Integer>] Array of robot IDs (both active and passive)
|
|
268
|
-
#
|
|
269
|
-
# @example
|
|
270
|
-
# group.member_ids # => [1, 2, 3]
|
|
271
|
-
#
|
|
272
|
-
def member_ids
|
|
273
|
-
all_robots.values.map(&:robot_id)
|
|
274
|
-
end
|
|
275
|
-
|
|
276
|
-
# Returns names of all active robots.
|
|
277
|
-
#
|
|
278
|
-
# @return [Array<String>] Array of active robot names
|
|
279
|
-
#
|
|
280
|
-
# @example
|
|
281
|
-
# group.active_robot_names # => ['primary-agent', 'secondary-agent']
|
|
282
|
-
#
|
|
283
|
-
def active_robot_names
|
|
284
|
-
@active_robots.keys
|
|
285
|
-
end
|
|
286
|
-
|
|
287
|
-
# Returns names of all passive robots.
|
|
288
|
-
#
|
|
289
|
-
# @return [Array<String>] Array of passive robot names
|
|
290
|
-
#
|
|
291
|
-
# @example
|
|
292
|
-
# group.passive_robot_names # => ['standby-agent']
|
|
293
|
-
#
|
|
294
|
-
def passive_robot_names
|
|
295
|
-
@passive_robots.keys
|
|
296
|
-
end
|
|
297
|
-
|
|
298
|
-
# @!endgroup
|
|
299
|
-
|
|
300
|
-
# @!group Shared Working Memory Operations
|
|
175
|
+
def add_passive(robot_name) = enrolled(@members.add_passive(robot_name))
|
|
301
176
|
|
|
302
177
|
# Adds content to shared working memory for all group members.
|
|
303
178
|
#
|
|
@@ -307,80 +182,34 @@ class HTM
|
|
|
307
182
|
#
|
|
308
183
|
# @param content [String] The content to remember
|
|
309
184
|
# @param originator [String, nil] Name of the robot creating the memory (optional)
|
|
310
|
-
# @param options [Hash] Additional options passed to HTM#remember
|
|
311
185
|
# @return [Integer] The node ID of the created memory
|
|
312
186
|
# @raise [RuntimeError] if no active robots exist in the group
|
|
313
187
|
#
|
|
314
|
-
# @example Add memory with default originator
|
|
315
|
-
# node_id = group.remember('Customer prefers morning appointments.')
|
|
316
|
-
#
|
|
317
188
|
# @example Add memory with specific originator
|
|
318
|
-
# node_id = group.remember(
|
|
319
|
-
# 'Escalated to billing department.',
|
|
320
|
-
# originator: 'agent-2'
|
|
321
|
-
# )
|
|
189
|
+
# node_id = group.remember('Escalated to billing department.', originator: 'agent-2')
|
|
322
190
|
#
|
|
323
191
|
def remember(content, originator: nil, **)
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
primary = if originator && all_robots[originator]
|
|
328
|
-
all_robots[originator]
|
|
329
|
-
else
|
|
330
|
-
@active_robots.values.first
|
|
331
|
-
end
|
|
332
|
-
|
|
333
|
-
node_id = primary.remember(content, **)
|
|
334
|
-
|
|
335
|
-
# Sync to database (robot_nodes table) for all other members
|
|
336
|
-
sync_node_to_members(node_id, exclude: primary.robot_id)
|
|
192
|
+
primary = @members.primary(originator)
|
|
193
|
+
node_id = primary.remember(content, **)
|
|
194
|
+
robot_id = primary.robot_id
|
|
337
195
|
|
|
338
|
-
#
|
|
339
|
-
|
|
196
|
+
# Sync to other members' working memory in the database, then notify
|
|
197
|
+
# their in-memory caches
|
|
198
|
+
ledger.share(node_id, except: robot_id)
|
|
199
|
+
@channel.notify(:added, node_id: node_id, robot_id: robot_id)
|
|
340
200
|
|
|
341
201
|
node_id
|
|
342
202
|
end
|
|
343
203
|
|
|
344
|
-
# Recalls memories
|
|
345
|
-
#
|
|
346
|
-
# Uses the first active robot to perform the query against the shared
|
|
347
|
-
# working memory context.
|
|
204
|
+
# Recalls memories using the first active robot (all members share the
|
|
205
|
+
# same working memory).
|
|
348
206
|
#
|
|
349
207
|
# @param query [String] The search query
|
|
350
|
-
# @
|
|
351
|
-
# @option options [Integer] :limit Maximum number of results
|
|
352
|
-
# @option options [Symbol] :strategy Search strategy (:vector, :fulltext, :hybrid)
|
|
353
|
-
# @return [Array] Array of matching memories
|
|
208
|
+
# @return [Array] Matching memories (format depends on options)
|
|
354
209
|
# @raise [RuntimeError] if no active robots exist in the group
|
|
355
210
|
#
|
|
356
|
-
# @example
|
|
357
|
-
# results = group.recall('billing issue', limit: 5, strategy: :fulltext)
|
|
358
|
-
#
|
|
359
211
|
def recall(query, **)
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
primary = @active_robots.values.first
|
|
363
|
-
primary.recall(query, **)
|
|
364
|
-
end
|
|
365
|
-
|
|
366
|
-
# Returns all nodes currently in shared working memory.
|
|
367
|
-
#
|
|
368
|
-
# Queries the database for the union of all members' working memory,
|
|
369
|
-
# returning nodes sorted by creation date (newest first).
|
|
370
|
-
#
|
|
371
|
-
# @return [Sequel::Dataset] Collection of nodes
|
|
372
|
-
#
|
|
373
|
-
# @example
|
|
374
|
-
# nodes = group.working_memory_contents
|
|
375
|
-
# nodes.each { |n| puts n.content }
|
|
376
|
-
#
|
|
377
|
-
def working_memory_contents
|
|
378
|
-
node_ids = HTM::Models::RobotNode
|
|
379
|
-
.where(robot_id: member_ids, working_memory: true)
|
|
380
|
-
.distinct
|
|
381
|
-
.select_map(:node_id)
|
|
382
|
-
|
|
383
|
-
HTM::Models::Node.where(id: node_ids).order(Sequel.desc(:created_at))
|
|
212
|
+
@members.primary.recall(query, **)
|
|
384
213
|
end
|
|
385
214
|
|
|
386
215
|
# Clears shared working memory for all group members.
|
|
@@ -390,133 +219,52 @@ class HTM
|
|
|
390
219
|
#
|
|
391
220
|
# @return [Integer] Number of robot_node records updated
|
|
392
221
|
#
|
|
393
|
-
# @example
|
|
394
|
-
# cleared_count = group.clear_working_memory
|
|
395
|
-
# puts "Cleared #{cleared_count} working memory entries"
|
|
396
|
-
#
|
|
397
222
|
def clear_working_memory
|
|
398
|
-
count =
|
|
399
|
-
.where(robot_id: member_ids, working_memory: true)
|
|
400
|
-
.update(working_memory: false)
|
|
223
|
+
count = ledger.clear
|
|
401
224
|
|
|
402
|
-
|
|
403
|
-
primary = @active_robots.values.first || @passive_robots.values.first
|
|
225
|
+
primary = @members.first_member
|
|
404
226
|
return 0 unless primary
|
|
405
227
|
|
|
406
228
|
primary.clear_working_memory
|
|
407
|
-
|
|
408
|
-
# Notify all listeners (will clear other in-memory caches via callback)
|
|
409
229
|
@channel.notify(:cleared, node_id: nil, robot_id: primary.robot_id)
|
|
410
230
|
|
|
411
231
|
count
|
|
412
232
|
end
|
|
413
233
|
|
|
414
|
-
# @!endgroup
|
|
415
|
-
|
|
416
|
-
# @!group Synchronization
|
|
417
|
-
|
|
418
234
|
# Synchronizes a specific robot to match the group's shared working memory.
|
|
419
235
|
#
|
|
420
236
|
# Copies working memory flags from other members to the specified robot,
|
|
421
|
-
#
|
|
237
|
+
# then loads those nodes into its in-memory working memory, ensuring it
|
|
238
|
+
# has access to all shared context.
|
|
422
239
|
#
|
|
423
240
|
# @param robot_name [String] Name of the robot to synchronize
|
|
424
|
-
# @return [Integer] Number of nodes
|
|
241
|
+
# @return [Integer] Number of nodes newly flagged in the database
|
|
425
242
|
# @raise [ArgumentError] if robot_name is not a member
|
|
426
243
|
#
|
|
427
|
-
# @example
|
|
428
|
-
# synced = group.sync_robot('new-agent')
|
|
429
|
-
# puts "Synchronized #{synced} nodes"
|
|
430
|
-
#
|
|
431
244
|
def sync_robot(robot_name)
|
|
432
|
-
htm
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
shared_node_ids = HTM::Models::RobotNode
|
|
437
|
-
.where(robot_id: member_ids, working_memory: true)
|
|
438
|
-
.exclude(robot_id: htm.robot_id)
|
|
439
|
-
.distinct
|
|
440
|
-
.select_map(:node_id)
|
|
441
|
-
|
|
442
|
-
synced = 0
|
|
443
|
-
shared_node_ids.each do |node_id|
|
|
444
|
-
# Create or update robot_node with working_memory=true
|
|
445
|
-
robot_node = HTM::Models::RobotNode.first(
|
|
446
|
-
robot_id: htm.robot_id,
|
|
447
|
-
node_id: node_id
|
|
448
|
-
)
|
|
449
|
-
robot_node ||= HTM::Models::RobotNode.new(
|
|
450
|
-
robot_id: htm.robot_id,
|
|
451
|
-
node_id: node_id
|
|
452
|
-
)
|
|
453
|
-
next if robot_node.working_memory
|
|
454
|
-
|
|
455
|
-
robot_node.working_memory = true
|
|
456
|
-
robot_node.save
|
|
457
|
-
synced += 1
|
|
458
|
-
end
|
|
459
|
-
|
|
460
|
-
synced
|
|
245
|
+
htm = @members.fetch!(robot_name)
|
|
246
|
+
count = ledger.sync_robot(htm.robot_id)
|
|
247
|
+
self.class.refill(htm)
|
|
248
|
+
count
|
|
461
249
|
end
|
|
462
250
|
|
|
463
|
-
#
|
|
251
|
+
# Loads the nodes the database marks as in a member's working memory
|
|
252
|
+
# into its in-memory working memory (within its token budget)
|
|
464
253
|
#
|
|
465
|
-
#
|
|
254
|
+
# @param htm [HTM] A member's HTM instance
|
|
255
|
+
# @return [Integer] Nodes now in its working memory
|
|
256
|
+
def self.refill(htm) = htm.working_memory.restore(htm.robot_id)
|
|
257
|
+
|
|
258
|
+
# Synchronizes all members to a consistent state.
|
|
466
259
|
#
|
|
467
260
|
# @return [Hash] Sync results with :synced_nodes and :members_updated counts
|
|
468
261
|
#
|
|
469
|
-
# @example
|
|
470
|
-
# result = group.sync_all
|
|
471
|
-
# puts "Synced #{result[:synced_nodes]} nodes to #{result[:members_updated]} members"
|
|
472
|
-
#
|
|
473
262
|
def sync_all
|
|
474
|
-
|
|
475
|
-
total_synced = 0
|
|
476
|
-
|
|
477
|
-
all_robots.each_key do |robot_name|
|
|
478
|
-
synced = sync_robot(robot_name)
|
|
479
|
-
if synced.positive?
|
|
480
|
-
members_updated += 1
|
|
481
|
-
total_synced += synced
|
|
482
|
-
end
|
|
483
|
-
end
|
|
263
|
+
counts = @members.all.keys.map { sync_robot(it) }.select(&:positive?)
|
|
484
264
|
|
|
485
|
-
{ synced_nodes:
|
|
265
|
+
{ synced_nodes: counts.sum, members_updated: counts.size }
|
|
486
266
|
end
|
|
487
267
|
|
|
488
|
-
# Checks if all members have identical working memory.
|
|
489
|
-
#
|
|
490
|
-
# Compares the set of working memory node IDs across all members.
|
|
491
|
-
#
|
|
492
|
-
# @return [Boolean] true if all members have the same working memory nodes
|
|
493
|
-
#
|
|
494
|
-
# @example
|
|
495
|
-
# if group.in_sync?
|
|
496
|
-
# puts "All robots synchronized"
|
|
497
|
-
# else
|
|
498
|
-
# group.sync_all
|
|
499
|
-
# end
|
|
500
|
-
#
|
|
501
|
-
def in_sync?
|
|
502
|
-
return true if member_ids.length <= 1
|
|
503
|
-
|
|
504
|
-
# Get working memory node_ids for each robot
|
|
505
|
-
working_memories = member_ids.map do |robot_id|
|
|
506
|
-
HTM::Models::RobotNode
|
|
507
|
-
.where(robot_id: robot_id, working_memory: true)
|
|
508
|
-
.select_map(:node_id)
|
|
509
|
-
.sort
|
|
510
|
-
end
|
|
511
|
-
|
|
512
|
-
# All should be identical
|
|
513
|
-
working_memories.uniq.length == 1
|
|
514
|
-
end
|
|
515
|
-
|
|
516
|
-
# @!endgroup
|
|
517
|
-
|
|
518
|
-
# @!group Failover
|
|
519
|
-
|
|
520
268
|
# Transfers working memory from one robot to another.
|
|
521
269
|
#
|
|
522
270
|
# Copies all working memory node references from the source robot to the
|
|
@@ -524,85 +272,25 @@ class HTM
|
|
|
524
272
|
#
|
|
525
273
|
# @param from_robot [String] Name of the source robot
|
|
526
274
|
# @param to_robot [String] Name of the destination robot
|
|
527
|
-
# @param clear_source [Boolean]
|
|
275
|
+
# @param clear_source [Boolean] Clear the source robot's working memory afterwards
|
|
276
|
+
# @option options [Boolean] :clear_source Whether to clear source's working memory after transfer (default: true)
|
|
528
277
|
# @return [Integer] Number of nodes transferred
|
|
529
278
|
# @raise [ArgumentError] if either robot is not a member
|
|
530
279
|
#
|
|
531
|
-
# @example Transfer with source clearing
|
|
532
|
-
# transferred = group.transfer_working_memory('failing-agent', 'backup-agent')
|
|
533
|
-
#
|
|
534
280
|
# @example Transfer without clearing source
|
|
535
|
-
# transferred = group.transfer_working_memory(
|
|
536
|
-
# 'agent-1', 'agent-2',
|
|
537
|
-
# clear_source: false
|
|
538
|
-
# )
|
|
281
|
+
# transferred = group.transfer_working_memory('agent-1', 'agent-2', clear_source: false)
|
|
539
282
|
#
|
|
540
283
|
def transfer_working_memory(from_robot, to_robot, clear_source: true)
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
raise ArgumentError, "#{from_robot} is not a member" unless from_htm
|
|
545
|
-
raise ArgumentError, "#{to_robot} is not a member" unless to_htm
|
|
546
|
-
|
|
547
|
-
# Get source's working memory nodes
|
|
548
|
-
source_node_ids = HTM::Models::RobotNode
|
|
549
|
-
.where(robot_id: from_htm.robot_id, working_memory: true)
|
|
550
|
-
.select_map(:node_id)
|
|
551
|
-
|
|
552
|
-
transferred = 0
|
|
553
|
-
source_node_ids.each do |node_id|
|
|
554
|
-
robot_node = HTM::Models::RobotNode.first(
|
|
555
|
-
robot_id: to_htm.robot_id,
|
|
556
|
-
node_id: node_id
|
|
557
|
-
)
|
|
558
|
-
robot_node ||= HTM::Models::RobotNode.new(
|
|
559
|
-
robot_id: to_htm.robot_id,
|
|
560
|
-
node_id: node_id
|
|
561
|
-
)
|
|
562
|
-
robot_node.working_memory = true
|
|
563
|
-
robot_node.save
|
|
564
|
-
transferred += 1
|
|
565
|
-
end
|
|
566
|
-
|
|
567
|
-
# Clear source's working memory if requested
|
|
568
|
-
if clear_source
|
|
569
|
-
HTM::Models::RobotNode
|
|
570
|
-
.where(robot_id: from_htm.robot_id, working_memory: true)
|
|
571
|
-
.update(working_memory: false)
|
|
572
|
-
end
|
|
284
|
+
options = TransferOptions.new(clear_source:)
|
|
285
|
+
from = @members.fetch!(from_robot)
|
|
286
|
+
to = @members.fetch!(to_robot)
|
|
573
287
|
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
#
|
|
579
|
-
# Promotes the first passive robot to active status. The promoted robot
|
|
580
|
-
# already has synchronized working memory and can immediately handle requests.
|
|
581
|
-
#
|
|
582
|
-
# @return [String] Name of the promoted robot
|
|
583
|
-
# @raise [RuntimeError] if no passive robots are available
|
|
584
|
-
#
|
|
585
|
-
# @example
|
|
586
|
-
# promoted = group.failover!
|
|
587
|
-
# puts "#{promoted} is now active"
|
|
588
|
-
#
|
|
589
|
-
def failover!
|
|
590
|
-
raise 'No passive robots available for failover' if @passive_robots.empty?
|
|
591
|
-
|
|
592
|
-
# Get first passive robot
|
|
593
|
-
standby_name = @passive_robots.keys.first
|
|
594
|
-
|
|
595
|
-
# Promote it
|
|
596
|
-
promote(standby_name)
|
|
597
|
-
|
|
598
|
-
puts " ⚡ Failover: #{standby_name} promoted to active"
|
|
599
|
-
standby_name
|
|
288
|
+
count = ledger.transfer(from.robot_id, to.robot_id, options)
|
|
289
|
+
self.class.refill(to)
|
|
290
|
+
from.working_memory.clear if clear_source
|
|
291
|
+
count
|
|
600
292
|
end
|
|
601
293
|
|
|
602
|
-
# @!endgroup
|
|
603
|
-
|
|
604
|
-
# @!group Status & Health
|
|
605
|
-
|
|
606
294
|
# Returns comprehensive status information about the group.
|
|
607
295
|
#
|
|
608
296
|
# @return [Hash] Status hash with the following keys:
|
|
@@ -616,118 +304,37 @@ class HTM
|
|
|
616
304
|
# @option return [Float] :token_utilization Ratio of used to max tokens (0.0-1.0)
|
|
617
305
|
# @option return [Boolean] :in_sync Whether all members are synchronized
|
|
618
306
|
#
|
|
619
|
-
# @example
|
|
620
|
-
# status = group.status
|
|
621
|
-
# puts "Group: #{status[:name]}"
|
|
622
|
-
# puts "Active: #{status[:active].join(', ')}"
|
|
623
|
-
# puts "Utilization: #{(status[:token_utilization] * 100).round(1)}%"
|
|
624
|
-
#
|
|
625
307
|
def status
|
|
626
|
-
|
|
627
|
-
|
|
308
|
+
contents = working_memory_contents
|
|
309
|
+
tokens = contents.sum { it.token_count || 0 }
|
|
310
|
+
max_tokens = @members.max_tokens
|
|
628
311
|
|
|
629
312
|
{
|
|
630
313
|
name: @name,
|
|
631
314
|
active: active_robot_names,
|
|
632
315
|
passive: passive_robot_names,
|
|
633
316
|
total_members: member_ids.length,
|
|
634
|
-
working_memory_nodes:
|
|
635
|
-
working_memory_tokens:
|
|
636
|
-
max_tokens:
|
|
637
|
-
token_utilization:
|
|
317
|
+
working_memory_nodes: contents.count,
|
|
318
|
+
working_memory_tokens: tokens,
|
|
319
|
+
max_tokens: max_tokens,
|
|
320
|
+
token_utilization: max_tokens.positive? ? (tokens.to_f / max_tokens).round(2) : 0,
|
|
638
321
|
in_sync: in_sync?
|
|
639
322
|
}
|
|
640
323
|
end
|
|
641
324
|
|
|
642
|
-
# Returns statistics about real-time synchronization.
|
|
643
|
-
#
|
|
644
|
-
# @return [Hash] Stats hash with :nodes_synced and :evictions_synced counts
|
|
645
|
-
#
|
|
646
|
-
# @example
|
|
647
|
-
# stats = group.sync_stats
|
|
648
|
-
# puts "Nodes synced: #{stats[:nodes_synced]}"
|
|
649
|
-
# puts "Evictions synced: #{stats[:evictions_synced]}"
|
|
650
|
-
#
|
|
651
|
-
def sync_stats
|
|
652
|
-
@mutex.synchronize { @sync_stats.dup }
|
|
653
|
-
end
|
|
654
|
-
|
|
655
|
-
# @!endgroup
|
|
656
|
-
|
|
657
325
|
private
|
|
658
326
|
|
|
659
|
-
def
|
|
660
|
-
@active_robots.merge(@passive_robots)
|
|
661
|
-
end
|
|
662
|
-
|
|
663
|
-
def setup_sync_listener
|
|
664
|
-
@channel.on_change do |event, node_id, origin_robot_id|
|
|
665
|
-
handle_sync_notification(event, node_id, origin_robot_id)
|
|
666
|
-
end
|
|
667
|
-
end
|
|
668
|
-
|
|
669
|
-
def handle_sync_notification(event, node_id, origin_robot_id)
|
|
670
|
-
@mutex.synchronize do
|
|
671
|
-
case event
|
|
672
|
-
when :added
|
|
673
|
-
sync_node_to_in_memory_caches(node_id, origin_robot_id)
|
|
674
|
-
@sync_stats[:nodes_synced] += 1
|
|
675
|
-
when :evicted
|
|
676
|
-
evict_from_in_memory_caches(node_id, origin_robot_id)
|
|
677
|
-
@sync_stats[:evictions_synced] += 1
|
|
678
|
-
when :cleared
|
|
679
|
-
clear_all_in_memory_caches(origin_robot_id)
|
|
680
|
-
end
|
|
681
|
-
end
|
|
682
|
-
end
|
|
683
|
-
|
|
684
|
-
def sync_node_to_in_memory_caches(node_id, origin_robot_id)
|
|
685
|
-
node = HTM::Models::Node.first(id: node_id)
|
|
686
|
-
return unless node
|
|
687
|
-
|
|
688
|
-
all_robots.each_value do |htm|
|
|
689
|
-
next if htm.robot_id == origin_robot_id
|
|
690
|
-
|
|
691
|
-
htm.working_memory.add_from_sync(
|
|
692
|
-
id: node.id,
|
|
693
|
-
content: node.content,
|
|
694
|
-
token_count: node.token_count || 0,
|
|
695
|
-
created_at: node.created_at
|
|
696
|
-
)
|
|
697
|
-
end
|
|
698
|
-
end
|
|
699
|
-
|
|
700
|
-
def evict_from_in_memory_caches(node_id, origin_robot_id)
|
|
701
|
-
all_robots.each_value do |htm|
|
|
702
|
-
next if htm.robot_id == origin_robot_id
|
|
703
|
-
|
|
704
|
-
htm.working_memory.remove_from_sync(node_id)
|
|
705
|
-
end
|
|
706
|
-
end
|
|
707
|
-
|
|
708
|
-
def clear_all_in_memory_caches(origin_robot_id)
|
|
709
|
-
all_robots.each_value do |htm|
|
|
710
|
-
next if htm.robot_id == origin_robot_id
|
|
711
|
-
|
|
712
|
-
htm.working_memory.clear_from_sync
|
|
713
|
-
end
|
|
714
|
-
end
|
|
327
|
+
def ledger = WorkingMemoryLedger.new(@members)
|
|
715
328
|
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
)
|
|
724
|
-
robot_node ||= HTM::Models::RobotNode.new(
|
|
725
|
-
robot_id: robot_id,
|
|
726
|
-
node_id: node_id
|
|
727
|
-
)
|
|
728
|
-
robot_node.working_memory = true
|
|
729
|
-
robot_node.save
|
|
329
|
+
# Brings a new member up to date with the group, in the database and in
|
|
330
|
+
# its in-memory working memory, and returns its robot id
|
|
331
|
+
def enrolled(htm)
|
|
332
|
+
robot_id = htm.robot_id
|
|
333
|
+
if member_ids.length > 1
|
|
334
|
+
ledger.sync_robot(robot_id)
|
|
335
|
+
self.class.refill(htm)
|
|
730
336
|
end
|
|
337
|
+
robot_id
|
|
731
338
|
end
|
|
732
339
|
end
|
|
733
340
|
end
|