Skip to content

refactor(db): typed parameter objects for the four widest writers (#1482) - #3042

Merged
dolho merged 1 commit into
devfrom
refactor/1482-facade-param-objects
Sep 29, 2026
Merged

dolho merged 1 commit into
devfrom
refactor/1482-facade-param-objects

Conversation

@dolho

@dolho dolho commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #1482

What

The four widest DB writers now each take one typed parameter object instead of 10–20 keyword args:

Writer New signature
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 and kw_only, and each field name matches its column name.

Why

  • Each parameter list was declared twice, in the database.py facade and in the db module.
  • update_execution_status forwarded its arguments positionally. Reordering the parameters in either layer would write one field into another column, and nothing would fail.
  • Adding a column meant editing both layers. That is the EXEC-023 bug class: the facade silently dropped claude_session_id.
  • Now the facade passes the object straight through, so a new column is a single field.

Behaviour: unchanged

  • The CAS contract is the same:
    • SUCCESS is blocked on CANCELLED.
    • Non-success writes are blocked on terminal rows.
    • claim_token is still a WHERE precondition.
  • retry_count and turn_integrity still mean "leave the column alone" when they are None.
  • claim_token stays a keyword-only argument. It is a precondition on the write, not a value the row records.
  • src/scheduler/database.py has its own writer and is not touched.

Callers and tests

Verification

  • I ran the full unit suite (pytest unit -m "not slow" -n auto --dist loadfile) on untouched dev and on this branch:
    • dev: 15 failed / 18547 passed.
    • This branch: 15 failed / 18566 passed.
  • The 15 failures are the same set on both runs. They are all IPv4-mapped IPv6 SSRF/origin cases that depend on the environment, and none of them touch this change.

Docs

  • architecture/backend.md has a new db/write_params.py row.
  • The stale signature snippets in feature-flows/tasks-tab.md and persistent-chat-tracking.md are refreshed.

🤖 Generated with Claude Code

)

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 AndriiPasternak31 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. [nit] tests/unit/test_1482_write_params.py:173 and :197: the "every field is persisted" tests pass None for source_agent_name (schedule source) and for error (execution result). Those tests exist to catch a field that gets dropped between the object and its column. For a field whose input is None, though, a dropped field reads back as NULL, which is exactly what the test expects, so that field is unguarded. error is caught by accident today, but only because test_none_retry_count_and_turn_integrity_leave_the_columns_alone writes error="boom". I confirmed that by mutating "error": r.error to "error": None in db/schedules/executions.py: only that other test went red. Fix: give every field a distinct non-None value in the values dicts. error="e" still works with status="success".

  2. [nit] docs/memory/feature-flows/execution-termination.md:246 and :256 still show literal Python db.update_execution_status(execution_id, status="failed", error=...). With this PR that line raises TypeError, 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 to result=ExecutionResult(error=...), the same way tasks-tab.md was 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 touched src/backend files matches the merge base (ebc2bfb4f). An AST sweep of all of src/backend finds no caller still using the old keywords. The only other hits are src/scheduler/service.py, which calls the scheduler's own writer and is correctly left alone. origin/dev has 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_id is still kept off ScheduleExecution.
  • ruff F821/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 in docker/backend/Dockerfile, so write_params.py ships in the image (prod-image-smoke is 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_calls with compact_metadata makes it go red;
    • NULLing error makes it go red, but only through the retry-count test (see finding 1).
  • gh pr checks 3042: all green (pytest head seed 12345, schema-parity, pg-migrations, CodeQL, journey-smoke, prod-image-smoke).

@dolho
dolho merged commit fe31df6 into dev Sep 29, 2026
25 checks passed
dolho added a commit that referenced this pull request Sep 29, 2026
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]>
@dolho

dolho commented Sep 29, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @AndriiPasternak31, I addressed both nits in 735f213:

  1. tests/unit/test_1482_write_params.py: the "every field is persisted" values dicts now use distinct non-None values for every field (source_agent_name="sched-caller", error="e"), so a field that gets dropped now fails the test.
  2. docs/memory/feature-flows/execution-termination.md: the two literal calls now use result=ExecutionResult(error=...), which matches the new signature.

Verification: pytest unit/test_1482_write_params.py gives 19 passed, both with pytest-randomly and without it. I then mutated "error": r.error to None in db/schedules/executions.py. With that change, test_every_result_field_is_persisted fails on its own. I reverted the mutation afterwards.

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