Scheduler¶
Proportional credit-weight task scheduling.
Proportional fair-share scheduler for assigning tasks to idle agents.
Uses a purely deterministic algorithm -- zero LLM calls. Every token budget is spent on agent work, not on deciding which work to do.
The scheduling algorithm runs in two phases each time an idle agent needs a task:
-
Min-task guarantee -- Projects that have completed zero tasks in the current scheduling window are prioritized first. This ensures every active project gets at least one task assigned before proportional allocation kicks in.
-
Deficit-based proportional allocation -- Among projects that already have at least one completion, the scheduler picks the project whose actual token usage ratio is furthest below its target ratio (derived from
credit_weight). This gradually converges each project toward its fair share of total agent time.
Both phases respect per-project concurrency limits (max_concurrent_agents),
per-project / global budget caps, and workspace availability (a project with
all workspaces locked cannot receive new assignments even if it has quota).
Key design properties:
- Pure function — the scheduler takes a snapshot (
SchedulerState) and returns actions with zero side effects, zero LLM calls, and zero I/O. - Starvation-free —
min_task_guaranteeensures every active project eventually receives at least one task per scheduling window. - Convergent — deficit-based proportional allocation gradually steers each project toward its fair share; short-term imbalances self-correct over multiple scheduling rounds.
Concrete example of deficit-based scheduling::
Projects: A (weight=3), B (weight=1)
Total weight = 4 → target ratios: A=75%, B=25%
Current window usage: A=1000 tokens, B=500 tokens
Total tokens = 1500 → actual ratios: A=66.7%, B=33.3%
Deficits: A = 66.7% - 75% = -8.3% (under-served)
B = 33.3% - 25% = +8.3% (over-served)
→ Project A sorts first because its deficit is more negative.
→ The scheduler assigns A's highest-priority READY task next.
Over multiple rounds, this converges: A will keep getting priority
until its actual usage ratio approaches 75%.
Time complexity: O(A × P × log P) per cycle, where A = idle agents and P = active projects. Both are typically small (<10), so scheduling is effectively instant.
Integration with the orchestrator:
The orchestrator's ``_schedule()`` method builds a ``SchedulerState``
snapshot from DB queries each cycle, passes it to ``Scheduler.schedule()``,
and receives back a list of ``AssignAction`` objects. The orchestrator
then launches background asyncio tasks for each assignment.
See ``src/orchestrator.py::_schedule()`` for snapshot construction.
See ``specs/scheduler-and-budget.md`` for the full specification.
Classes¶
AssignAction
dataclass
¶
A scheduling decision: assign one specific task to one specific agent.
This is the output type of the scheduler -- a list of these actions is returned each scheduling round, one per idle agent that received work. The orchestrator is responsible for actually executing the assignment (updating the database, starting the agent process, etc.).
SchedulerState
dataclass
¶
SchedulerState(projects: list[Project], tasks: list[Task], agents: list[Agent], project_token_usage: dict[str, int], project_active_agent_counts: dict[str, int], tasks_completed_in_window: dict[str, int], project_available_workspaces: dict[str, int] = dict(), workspace_locks: dict[str, str | None] = dict(), global_budget: int | None = None, global_tokens_used: int = 0)
A snapshot of all system state the scheduler needs to make decisions.
The scheduler is a pure function: given a SchedulerState, it returns a list of AssignActions with no side effects. This stateless/functional design makes the algorithm easy to test and reason about -- the orchestrator builds this snapshot each tick, and the scheduler never touches the database or any external resource.
All "window" fields (token usage, completed counts) are scoped to the
rolling_window_hours configured in the scheduling config. The
rolling window creates a "forgetting" mechanism: old usage ages out,
so a project that was over-served yesterday can still receive fair
allocation today. The orchestrator computes these from DB queries
filtered by time.time() - window_hours * 3600.
Scheduler ¶
Functions¶
schedule
staticmethod
¶
Assign READY tasks to idle agents using proportional fair-share.
Algorithm steps:
1. Bail out early if the global token budget is exhausted.
2. Collect idle agents and group READY tasks by project.
3. For each idle agent (in order), rank active projects by:
a. Min-task guarantee -- projects with zero completions in the
window sort first (phase 1).
b. Deficit -- among the rest, the project whose actual token
usage is furthest below its credit_weight share sorts
first (phase 2).
4. Walk the ranked project list; skip any project that has hit its
budget cap or concurrency limit. Pick the highest-priority
READY task from the first eligible project.
5. Record the assignment and move to the next idle agent.
Returns a list of :class:AssignAction -- one per agent that was
matched with a task. May be empty if no work can be assigned.
Source code in src/scheduler.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |