Skip to content

fix: ephemeral budget gate no longer counts the execution's own pre-created row (#1601) - #1602

Merged
dolho merged 2 commits into
devfrom
feature/1601-ephemeral-self-count
Jul 14, 2026
Merged

dolho merged 2 commits into
devfrom
feature/1601-ephemeral-self-count

Conversation

@vybe

@vybe vybe commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • The /task and scheduler paths pre-create the execution row with status=RUNNING before execute_task → CapacityManager.acquire, so the Filter agents by owner on Dashboard and Agents pages #69 ephemeral admission gate's terminal + active count included the row being admitted: a max_executions=1 ghost denied its first (only) run with 410 ephemeral_budget_exhausted, and every N-budget ghost lost its final run. Found by the live activation smoke required by trinity-enterprise#148.
  • Fix: acquire already knows the execution_id being admitted — thread it through as count_ephemeral_budget_usage(exclude_execution_id=...) and exclude that row from the count. Paths that acquire before creating the row are unaffected (the id isn't in the table yet — exclusion is a no-op). The terminal discard hook keeps the un-excluded count (the just-finished row must count).
  • Concurrency overshoot bound preserved: two racing admissions for the last budget slot each exclude only their own row, so they still see each other and both deny.

Changes

  • src/backend/db/agent_settings/ephemeral.py — optional exclude_execution_id on count_ephemeral_budget_usage
  • src/backend/services/capacity_manager.py — gate passes the admitted execution_id
  • src/backend/database.py — facade delegation carries the kwarg
  • tests/unit/test_69_ephemeral_agents.py — new: DB-layer exclusion (own row / unknown id no-op) + gate-level bug: ephemeral budget gate counts the execution's own pre-created row — max_executions=1 ghosts can never run #1601 scenario (own row admits, racing sibling still denies, id threaded through); existing monkeypatched doubles updated to the new signature

Test plan

  • tests/unit/test_69_ephemeral_agents.py — 42 passed (40 existing + 2 new)
  • Adjacent units: test_capacity_manager.py, test_backlog.py — 101 passed combined
  • Live verification on the local stack: pre-fix, a fresh max_executions=1 ghost 410'd its first /task; post-fix the same flow admits, runs (SMOKE-OK), and hard-discards via the terminal hook within seconds (container removed, ownership purged, audit ephemeral_discard row)

Fixes #1601

🤖 Generated with Claude Code

Eugene Vyborov and others added 2 commits July 14, 2026 09:35
… own pre-created row (#1601)

The /task and scheduler paths pre-create the execution row with
status=RUNNING before execute_task → CapacityManager.acquire, so the #69
admission gate's terminal+active count included the row being admitted:
a max_executions=1 ghost denied its first (only) run with 410
ephemeral_budget_exhausted, and every N-budget ghost lost its final run.

Fix: acquire already knows the execution_id being admitted — thread it
through as count_ephemeral_budget_usage(exclude_execution_id=...) and
exclude that row from the count. Paths that acquire before creating the
row are unaffected (the id isn't in the table yet — exclusion is a no-op).
The terminal discard hook keeps the un-excluded count (the just-finished
row must count). Concurrency overshoot bound preserved: two racing
admissions for the last budget slot each exclude only their own row, so
they still see each other and both deny.

Found by the live activation smoke required by trinity-enterprise#148.
Verified live: a max_executions=1 ghost admits, runs (SMOKE-OK), and
hard-discards via the terminal hook within seconds.

Fixes #1601

Co-Authored-By: Claude Fable 5 <[email protected]>
@dolho

dolho commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Review — LGTM

Traced the logic and the race analysis holds. The root cause (the /task + scheduler pre-created RUNNING row counting against its own budget in the terminal+active gate) is correctly fixed by excluding the admitted execution_id, and the overshoot bound is preserved because each racing admission excludes only its own row → siblings still see each other → both deny for the last slot. Acquire-before-create paths are a clean no-op (id not yet in the table). Tests cover DB-layer exclusion, the gate-level #1601 scenario, and the racing-sibling deny. CI green.

Non-blocking notes:

  1. Implicit invariant worth pinning down: the fix assumes acquire’s execution_id is the same id as the pre-created row. If any pre-create path ever calls acquire with a mismatched (or None) id, the exclusion silently no-ops and the off-by-one returns — with no test to catch it. Holds today; a one-line comment at the pre-create sites (or an assert) would keep it from regressing.
  2. if exclude_execution_id: treats "" as “no exclusion”. Fine given ids are non-empty urlsafe tokens — just flagging the truthiness vs is not None.
  3. The max_executions=1 double-first-run race resolves to both deny (safe, no overshoot) rather than one winning. Reads intentional and correct; noting it since the test asserts the sibling denies but not that at least one ultimately runs.

Nice catch surfacing it via the trinity-enterprise#148 activation smoke.

@dolho
dolho merged commit 486517b into dev Jul 14, 2026
22 checks passed
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.

2 participants