Class: Collavre::OrchestratorPolicy

Inherits:
ApplicationRecord show all
Defined in:
app/models/collavre/orchestrator_policy.rb

Overview

OrchestratorPolicy stores configuration for agent orchestration behavior.

Table: orchestrator_policies

Scope types:

  • nil (global): Default policy applied everywhere

  • “Creative”: Policy for a specific creative/workspace

  • “Topic”: Policy for a specific topic/conversation

  • “User”: Policy for a specific agent

Policy types:

  • matching: How agents are matched to messages

  • arbitration: How to select responders from candidates (floor control)

  • scheduling: When/how to execute agent jobs (resource management)

Example configs:

Arbitration policy (primary_first):
{
  "strategy": "primary_first",
  "max_responders": 1,
  "primary_agent_id": 123
}

Arbitration policy (round_robin):
{
  "strategy": "round_robin",
  "max_responders": 1
}

Constant Summary collapse

SCOPE_TYPES =

Scope types

%w[Creative Topic User].freeze
POLICY_TYPES =

Policy types

%w[matching arbitration scheduling stuck_detection collaboration].freeze
ARBITRATION_STRATEGIES =

Arbitration strategies

%w[all primary_first round_robin bid].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for_agent(agent_id, policy_type:) ⇒ Object

Find policies for a specific agent



97
98
99
100
101
102
# File 'app/models/collavre/orchestrator_policy.rb', line 97

def for_agent(agent_id, policy_type:)
  enabled
    .for_type(policy_type)
    .where(scope_type: "User", scope_id: agent_id)
    .by_priority
end

.for_context(context, policy_type:) ⇒ Object

Find policies applicable to a given context Returns policies in priority order (global < creative < topic < agent)



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/collavre/orchestrator_policy.rb', line 64

def for_context(context, policy_type:)
  policies = enabled.for_type(policy_type).by_priority

  applicable = []

  # Global policies
  applicable.concat(policies.global.to_a)

  # Creative-level policies
  if context["creative"].present?
    creative_id = context["creative"]["id"]
    applicable.concat(
      policies.where(scope_type: "Creative", scope_id: creative_id).to_a
    )
  end

  # Topic-level policies
  if context["topic"].present?
    topic_id = context["topic"]["id"]
    applicable.concat(
      policies.where(scope_type: "Topic", scope_id: topic_id).to_a
    )
  end

  # Agent-level policies are handled separately per-agent

  # Note: We don't sort by priority globally here because scope hierarchy
  # (global < creative < topic) takes precedence. Each scope level is
  # already sorted by priority from the query.
  applicable
end

Instance Method Details

#arbitration_strategyObject

Get the arbitration strategy



118
119
120
121
122
# File 'app/models/collavre/orchestrator_policy.rb', line 118

def arbitration_strategy
  return nil unless policy_type == "arbitration"

  config_value("strategy", default: "all")
end

#config_value(key, default: nil) ⇒ Object

Get a config value with default



108
109
110
# File 'app/models/collavre/orchestrator_policy.rb', line 108

def config_value(key, default: nil)
  config[key.to_s] || default
end

#global?Boolean

Check if this is a global policy

Returns:

  • (Boolean)


113
114
115
# File 'app/models/collavre/orchestrator_policy.rb', line 113

def global?
  scope_type.nil? && scope_id.nil?
end