Skip to content

feat: store topic primary_agent_id in topics table#1171

Open
sh1nj1 wants to merge 3 commits into
mainfrom
feat/topic-primary-agent-db
Open

feat: store topic primary_agent_id in topics table#1171
sh1nj1 wants to merge 3 commits into
mainfrom
feat/topic-primary-agent-db

Conversation

@sh1nj1
Copy link
Copy Markdown
Owner

@sh1nj1 sh1nj1 commented Apr 10, 2026

Summary

  • Add primary_agent_id column directly to topics table instead of relying on OrchestratorPolicy with scope_type: "Topic"
  • Simplify TopicsController, PolicyResolver, AiAgentResolvable, and related code
  • Data migration moves existing topic-scoped primary_agent_id from orchestrator_policies to topics table

Why

Previously, topic-level primary agent was stored in orchestrator_policies table with scope_type: "Topic". When admin updated orchestration YAML, apply_policies! would delete_all policies and re-create from YAML — destroying any topic-scoped settings. Storing directly on the topics table prevents this data loss.

Changes

  • Migration: add_primary_agent_id_to_topics with data migration from orchestrator_policies
  • Topic model: belongs_to :primary_agent directly on model
  • TopicsController: Simplified topic_json to use column directly
  • PolicyResolver: Checks topic.primary_agent_id first, falls back to orchestration config
  • AiAgentResolvable: Simplified to use topic column

Test plan

  • All 1623 tests pass (0 failures, 0 errors)
  • Rubocop passes (796 files, no offenses)
  • Data migration tested for existing orchestrator_policies with scope_type Topic

sh1nj1 added 2 commits April 10, 2026 19:29
…or_policies

Move primary_agent_id from OrchestratorPolicy (scope_type: Topic) to a
direct column on the topics table. This prevents topic-level agent
assignments from being wiped when admin saves global orchestration config
via the YAML editor (which does OrchestratorPolicy.delete_all).

Changes:
- Migration adds primary_agent_id column to topics with data migration
- Topic model uses belongs_to :primary_agent instead of policy lookup
- TopicsController uses includes(:primary_agent) for eager loading
- PolicyResolver checks topic column first, falls back to policy config
- AiAgentResolvable checks topic column directly before policy resolver
- All tests updated to assert on topic.primary_agent_id
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4a4818cd95

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread engines/collavre/app/models/collavre/topic.rb
Comment thread engines/collavre/db/migrate/20260410000000_add_primary_agent_id_to_topics.rb Outdated
Comment thread engines/collavre/db/migrate/20260410000000_add_primary_agent_id_to_topics.rb Outdated
…nostic

- PolicyResolver#arbitration_strategy now returns "primary_first"
  when topic has primary_agent_id, preserving prior behavior where
  set_primary_agent! always implied primary_first routing.
- Rewrite migration from raw SQLite SQL (json_extract, json_object)
  to ActiveRecord queries for PostgreSQL compatibility.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant