Skip to content

feat: Auto-assign subscription to new agents (round-robin) (#74) - #164

Closed
AndriiPasternak31 wants to merge 4 commits into
Abilityai:mainfrom
AndriiPasternak31:feature/74-auto-assign-subscription
Closed

AndriiPasternak31 wants to merge 4 commits into
Abilityai:mainfrom
AndriiPasternak31:feature/74-auto-assign-subscription

Conversation

@AndriiPasternak31

Copy link
Copy Markdown
Contributor

Summary

  • New agents are automatically assigned to the subscription with fewest agents (round-robin, alphabetical tie-break)
  • Rate-limited subscriptions are skipped during auto-assignment
  • Falls back to platform API key if no subscriptions exist or token decryption fails
  • System agents (trinity-system) unaffected — separate creation path

Changes

  • src/backend/db/subscriptions.py — Added get_least_used_subscription() method
  • src/backend/database.py — Added delegation method
  • src/backend/services/agent_service/crud.py — Auto-assign in create_agent_internal(): lookup before container creation, DB persist after register_agent_owner()
  • tests/test_subscriptions.py — 4 new tests (no-subs fallback, auto-assign, round-robin distribution, alphabetical tie-break)
  • docs/memory/changelog.md — Changelog entry
  • docs/memory/requirements.md — New requirement §20.3a
  • docs/memory/feature-flows/subscription-management.md — Auto-Assign section with data flow diagram

Test Plan

  • New tests pass: pytest tests/test_subscriptions.py::TestSubscriptionAutoAssign -v
  • Existing tests unaffected: pytest tests/test_subscriptions.py -v
  • Manual: Create agent with 0 subscriptions → uses API key
  • Manual: Register subscription → create agent → auth shows "subscription"
  • Manual: 2 subs + 3 agents → round-robin distribution

Closes #74

🤖 Generated with Claude Code

AndriiPasternak31 and others added 4 commits March 25, 2026 01:21
…ai#74)

When a new agent is created, Trinity now automatically assigns a subscription
using round-robin distribution (fewest agents first, alphabetical tie-break).
Falls back to platform API key if no subscriptions exist or token decryption
fails. System agents are unaffected (separate creation path).

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
get_least_used_subscription() now checks is_subscription_rate_limited()
for each candidate, matching the guard in select_best_alternative_subscription().
Prevents auto-assigning a rate-limited subscription to a new agent.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Updated subscription-management.md with Auto-Assign on Creation section
documenting the round-robin logic, data flow, and edge cases.
Added entry to feature-flows.md index.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
vybe added a commit that referenced this pull request Mar 25, 2026
New agents are automatically assigned to the subscription with fewest agents
(round-robin, alphabetical tie-break). Rate-limited subscriptions are skipped.
Falls back to platform API key if no subscriptions exist or token decryption fails.

- Added get_least_used_subscription() DB method
- Auto-assign logic in create_agent_internal() with graceful fallback
- 4 new tests covering no-subs, auto-assign, round-robin, tie-break

Closes #74

Co-Authored-By: andrii.pasternak <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@vybe

vybe commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Merged manually via squash to main (fork PR with changelog conflict). Commit: d5864d5

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.

Auto-assign subscription to new agents (round-robin)

2 participants