Repository navigation
refactor(db): typed parameter objects for the four widest writers (#1482) - #3042
Conversation
) update_execution_status, create_task_execution, create_schedule_execution and add_chat_message took 10-20 parameters, declared twice (database.py facade + db module) and, for update_execution_status, forwarded POSITIONALLY — so a reorder in either layer would write one field into another column with nothing failing, and a new column had to be added and forwarded in both places (the EXEC-023 class: the facade silently dropped claude_session_id). - db/write_params.py: frozen, kw_only ExecutionSource / TaskExecutionFields / ExecutionResult / ChatMessageFields; field names = column names. - The facade passes the object straight through; claim_token stays a keyword-only CAS precondition, not a recorded value. - Behaviour unchanged: CAS gating, the None-means-leave-alone rule for retry_count / turn_integrity, and every persisted column. - All src callers rewritten (comments kept); test fakes and mock assertions read through tests/unit/_write_params.py (flat_kwargs / object_keywords). - src/scheduler/database.py keeps its own writer — out of scope. Co-Authored-By: Claude Opus 5.5 <[email protected]>
AndriiPasternak31
left a comment
There was a problem hiding this comment.
This refactor is clean and I couldn't find a behaviour change. I compared every call site of the four writers against the merge base, with arguments flattened out of the new objects, and all 42 calls in the 13 touched source files pass exactly the same fields with the same expressions. The CAS contract, the rule that None leaves retry_count and turn_integrity alone, and the bool return are all intact. The findings below are two small nits, and neither blocks the merge.
-
[nit]
tests/unit/test_1482_write_params.py:173and:197: the "every field is persisted" tests passNoneforsource_agent_name(schedule source) and forerror(execution result). Those tests exist to catch a field that gets dropped between the object and its column. For a field whose input isNone, though, a dropped field reads back as NULL, which is exactly what the test expects, so that field is unguarded.erroris caught by accident today, but only becausetest_none_retry_count_and_turn_integrity_leave_the_columns_alonewriteserror="boom". I confirmed that by mutating"error": r.errorto"error": Noneindb/schedules/executions.py: only that other test went red. Fix: give every field a distinct non-None value in thevaluesdicts.error="e"still works withstatus="success". -
[nit]
docs/memory/feature-flows/execution-termination.md:246and:256still show literal Pythondb.update_execution_status(execution_id, status="failed", error=...). With this PR that line raisesTypeError, so anyone who copies it gets a broken call. The other flows (execution-queue.md:409,chat-turn-cancellation.md:105,workspace-model-choice.md:206) use shorthand call notation and are fine as they are. Fix: change those two lines toresult=ExecutionResult(error=...), the same waytasks-tab.mdwas refreshed.
For the record, nothing needs to change here: the facade's create_schedule_execution now forwards model_used. The old facade silently dropped that argument even though the db layer accepted it, so this closes the same EXEC-023 gap. No current caller passes it.
What I checked
- Argument equivalence: I wrote an AST script that maps each old positional argument to its name and inlines the fields of
ExecutionResult/TaskExecutionFields/ExecutionSource/ChatMessageFields. Every call in all 13 touchedsrc/backendfiles matches the merge base (ebc2bfb4f). An AST sweep of all ofsrc/backendfinds no caller still using the old keywords. The only other hits aresrc/scheduler/service.py, which calls the scheduler's own writer and is correctly left alone.origin/devhas no new commits past the merge base, so no new callers have landed since. db/schedules/executions.py:asdict(...)gives the same column set on insert as before.fan_out_task_idis still kept offScheduleExecution.ruffF821/F841/F811/F401 on every touched source file gives an identical finding set before and after the PR, so there are no undefined names left over from the local renames.db/is copied whole indocker/backend/Dockerfile, sowrite_params.pyships in the image (prod-image-smokeis green too).- Tests, Python 3.13, pytest-randomly seeds 12345 and 99999:
- all 42 touched unit test files: 1017 passed, 3 skipped on both seeds;
- the 28 untouched unit files that reference these writers: 496 passed on both seeds.
- Mutations run against
test_1482_write_params.py:- swapping
tool_callswithcompact_metadatamakes it go red; - NULLing
errormakes it go red, but only through the retry-count test (see finding 1).
- swapping
gh pr checks 3042: all green (pytest head seed 12345, schema-parity, pg-migrations, CodeQL, journey-smoke, prod-image-smoke).
Address review nits on #3042: the "every field is persisted" tests now give source_agent_name and error distinct non-None values so a dropped field fails, and execution-termination.md shows the ExecutionResult call signature. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks @AndriiPasternak31, I addressed both nits in 735f213:
Verification: |
Fixes #1482
What
The four widest DB writers now each take one typed parameter object instead of 10–20 keyword args:
update_execution_status(execution_id, status, result: ExecutionResult = None, *, claim_token=None)create_task_execution(agent_name, message, triggered_by="manual", fields: TaskExecutionFields = None)create_schedule_execution(schedule_id, agent_name, message, triggered_by="schedule", source: ExecutionSource = None)add_chat_message(session_id, agent_name, user_id, user_email, role, content, fields: ChatMessageFields = None)The objects live in the new
src/backend/db/write_params.py. They are frozen andkw_only, and each field name matches its column name.Why
database.pyfacade and in the db module.update_execution_statusforwarded its arguments positionally. Reordering the parameters in either layer would write one field into another column, and nothing would fail.claude_session_id.Behaviour: unchanged
claim_tokenis still a WHERE precondition.retry_countandturn_integritystill mean "leave the column alone" when they areNone.claim_tokenstays a keyword-only argument. It is a precondition on the write, not a value the row records.src/scheduler/database.pyhas its own writer and is not touched.Callers and tests
src/backend. There are no callers in the enterprise submodule.tests/unit/_write_params.pyadds two functions:flat_kwargs(call)reads a mocked call's fields.object_keywords(node)does the same job for the AST source guards (bug: MCP-triggered chat executions fail with 'Silent launch failure' (regression / parallel codepath of #279) #686, bug(voice): agents tell Workspace clients the surface is text-only — portal TTS is never advertised, and the channel/portal voice gates disagree #2157, Workspace report-back can't fire on the synchronous chat path — the pre-created row has no session binding #2426, ent#457), which now look inside the object constructor.tests/unit/test_1482_write_params.pyhas 19 tests covering:kw_only;retry_countandturn_integrityare preserved whenNone;claim_tokengating;Verification
pytest unit -m "not slow" -n auto --dist loadfile) on untoucheddevand on this branch:dev: 15 failed / 18547 passed.Docs
architecture/backend.mdhas a newdb/write_params.pyrow.feature-flows/tasks-tab.mdandpersistent-chat-tracking.mdare refreshed.🤖 Generated with Claude Code