Problem
The backend standards (.github/skills/backend-standards/SKILL.md, "Who commits") give each request one Unit of Work, from get_unit_of_work, and each worker job one, from create_unit_of_work(). Four places build their own instead.
Over the request's session, both in api/deps.py:
build_sandbox_file_bridge: UnitOfWork(db). Recording the files a provider's code produced now runs here too, through SandboxFileBridge.copy_provider_files, so it shares this Unit of Work.
build_sandbox_container_registry: UnitOfWork(db).
Over a worker session, each opening create_session() and then building a UnitOfWork by hand:
run_file_sweeper in services/files/file_sweeper.py;
run_sandbox_container_sweeper in services/code_execution/container_sweeper.py.
A UnitOfWork counts its open blocks on the object itself. So a second one over the same session does not see a block the first has open, and the rule that an inner block joins the outer one no longer holds between them. RequestContext has no Unit of Work to hand on today: it carries the session as db.
Proposal
Put the request's Unit of Work on RequestContext, and pass it to the file bridge and to the sandbox container registry. Have both sweepers use create_unit_of_work(). That also removes the need to trace whether a block is open when this code runs.
Acceptance
Part of #1470.
Problem
The backend standards (
.github/skills/backend-standards/SKILL.md, "Who commits") give each request one Unit of Work, fromget_unit_of_work, and each worker job one, fromcreate_unit_of_work(). Four places build their own instead.Over the request's session, both in
api/deps.py:build_sandbox_file_bridge:UnitOfWork(db). Recording the files a provider's code produced now runs here too, throughSandboxFileBridge.copy_provider_files, so it shares this Unit of Work.build_sandbox_container_registry:UnitOfWork(db).Over a worker session, each opening
create_session()and then building aUnitOfWorkby hand:run_file_sweeperinservices/files/file_sweeper.py;run_sandbox_container_sweeperinservices/code_execution/container_sweeper.py.A
UnitOfWorkcounts its open blocks on the object itself. So a second one over the same session does not see a block the first has open, and the rule that an inner block joins the outer one no longer holds between them.RequestContexthas no Unit of Work to hand on today: it carries the session asdb.Proposal
Put the request's Unit of Work on
RequestContext, and pass it to the file bridge and to the sandbox container registry. Have both sweepers usecreate_unit_of_work(). That also removes the need to trace whether a block is open when this code runs.Acceptance
src/gateway/, outsidecore/unit_of_work.py, onlyget_unit_of_workconstructs aUnitOfWork.Part of #1470.