fix(host_agent): deploy/provision 500 — double-begin on session
CI / lint (push) Successful in 2s
CI / unit (push) Successful in 9s
CI / integration (push) Successful in 2m21s
CI / publish (push) Successful in 8s

fetch_scope_targets() runs a SELECT that autobegins the session transaction
(SQLAlchemy 2.0), so the following `async with db.begin()` raised
"A transaction is already begun on this Session" → 500 on both
/plugins/host_agent/deploy and /provision.

Mint registrations directly into the autobegun transaction, build the
inventory while the ORM objects are still live, then await db.commit().
Escaped CI because unit tests use a mock db_sessionmaker; no integration
test exercises these routes (follow-up noted in Scribe issue #884).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-16 22:53:13 -04:00
parent 17c9c875e4
commit cfe6b4c25f
+14 -10
View File
@@ -731,17 +731,19 @@ async def deploy_via_ansible():
targets = await fetch_scope_targets(db, scope)
if not targets:
return _error(400, "no_targets", "No Ansible targets in that scope")
# The read above autobegins the session transaction (SQLAlchemy 2.0), so
# mint into it directly and commit — a nested db.begin() would double-begin.
tokens: dict[str, str] = {}
async with db.begin():
for t in targets:
host = await _ensure_host_for_target(db, t)
tokens[t.name] = await _mint_registration_token(db, host)
inv = generate_inventory(targets)
for t in targets:
host = await _ensure_host_for_target(db, t)
tokens[t.name] = await _mint_registration_token(db, host)
inv = generate_inventory(targets) # built while ORM objects are still live
for name, tok in tokens.items():
hv = inv["_meta"]["hostvars"].setdefault(name, {})
hv["steward_url"] = url
hv["steward_token"] = tok
inventory_content = _json.dumps(inv)
await db.commit()
actor_role = UserRole(session.get("user_role", "viewer"))
run, _source, err = await invoke_capability(
@@ -805,12 +807,13 @@ async def provision_via_ansible():
targets = await fetch_scope_targets(db, scope)
if not targets:
return _error(400, "no_targets", "No Ansible targets in that scope")
# The read above autobegins the session transaction (SQLAlchemy 2.0), so
# mint into it directly and commit — a nested db.begin() would double-begin.
tokens: dict[str, str] = {}
async with db.begin():
for t in targets:
host = await _ensure_host_for_target(db, t)
tokens[t.name] = await _mint_registration_token(db, host)
inv = generate_inventory(targets)
for t in targets:
host = await _ensure_host_for_target(db, t)
tokens[t.name] = await _mint_registration_token(db, host)
inv = generate_inventory(targets) # built while ORM objects are still live
for name, tok in tokens.items():
hv = inv["_meta"]["hostvars"].setdefault(name, {})
hv["steward_url"] = url
@@ -821,6 +824,7 @@ async def provision_via_ansible():
hv["steward_pubkey"] = pubkey
hv["steward_user"] = steward_user
inventory_content = _json.dumps(inv)
await db.commit()
actor_role = UserRole(session.get("user_role", "viewer"))
run, _source, err = await invoke_capability(