Skip to content

bug: ephemeral budget gate counts the execution's own pre-created row — max_executions=1 ghosts can never run #1601

Closed

Description

@vybe

Symptom

Live smoke of ghost activation (trinity-enterprise#148, entitled build): a fresh ephemeral agent created with max_executions: 1 refuses its first /task with 410 ephemeral_budget_exhausted. Both smoke attempts produced failed rows with ephemeral_exhausted: ghost agent budget spent (budget_exhausted) — the ghost never executed anything:

('CD-MAFwB6...', 'failed', 'manual', 'ephemeral_exhausted: ghost agent budget spent (budget_exhausted)', ...)

Root cause

The /task router (routers/chat.py::execute_parallel_task) pre-creates the execution row with status=RUNNING (db.create_task_execution) before execute_task → CapacityManager.acquire. The #69 admission gate then counts terminal + active (count_ephemeral_budget_usage, active = queued/running/pending_retry) — which includes the execution's own pre-created row. For max_executions=1: 0 terminal + 1 active(self) ≥ 1 → deny. Every pre-created-row path (task, scheduler) loses the ghost's final budgeted execution; max_executions=1 ghosts are unusable.

The active-count itself is correct in intent (bounds concurrent check-then-act overshoot) — the bug is only that the row being admitted counts against itself.

Fix

acquire already knows the execution_id being admitted. Thread it into the gate:

  • count_ephemeral_budget_usage(agent_name, exclude_execution_id=None) — exclude that row id from the count when given.
  • Gate in capacity_manager.acquire passes exclude_execution_id=execution_id.
  • The terminal discard hook keeps the un-excluded count (the just-finished row must count).

Paths that acquire before creating the row are unaffected (the id isn't in the table yet — exclusion is a no-op).

Acceptance criteria

  • A max_executions=1 ghost admits and runs its first execution, then hard-discards at the terminal hook
  • A ghost at terminal ≥ max_executions still denies admission (410)
  • A concurrent second admission (another active row, budget 1) still denies
  • Unit coverage in tests/unit/test_69_ephemeral_agents.py

Found by the live smoke required by trinity-enterprise#148 (activation of #69 / PR #1580).

Activity

  1. added
    priority-p1
    type-bug
    theme-reliability
    complexity-low
    on Jul 14, 2026
  2. vybe commented on Jul 14, 2026

    @vybe
    ContributorAuthor

    /claim

  3. added
    status-in-progress
    on Jul 14, 2026
  4. github-actions commented on Jul 14, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/1601-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #1601
  5. added a commit that references this issue on Jul 14, 2026
  6. added
    status-in-dev
    and removed
    status-in-progress
    on Jul 14, 2026
  7. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

complexity-lowpriority-p1theme-reliabilitytype-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions