hurui200320
ee2024046f
fix(plan): upsert action arguments during plan use to avoid UNIQUE constraint violation ( #4197 )
...
## Summary
`agents plan use` crashed with `sqlite3.IntegrityError: UNIQUE constraint failed: action_arguments.action_name, action_arguments.name` when the action had arguments already registered via `action create`. The root cause was `ActionRepository.update()` using SQLAlchemy's relationship `.clear()` + `.append()` pattern, which deferred the DELETE and processed the INSERT first — triggering a UNIQUE constraint violation when the same `(action_name, name)` pair was being re-inserted.
## Approach
Replace the `.clear()` + `.append()` pattern with explicit bulk `sa_delete()` + `session.flush()` before re-inserting child rows for both `action_arguments` and `action_invariants`. After the flush, expire the relationship collections with `session.expire(row, ["arguments_rel", "invariants_rel"])` so SQLAlchemy reloads from the now-empty database state before appending replacements. This avoids stale identity map references and guarantees the DELETE is committed before any INSERT.
## Key Changes
### Bug fix (`src/cleveragents/infrastructure/database/repositories.py`)
- `ActionRepository.update()` now uses `sa_delete(ActionArgumentModel)` and `sa_delete(ActionInvariantModel)` with `synchronize_session=False`, followed by `session.flush()`, before re-inserting child rows.
- Targeted `session.expire(row, ["arguments_rel", "invariants_rel"])` replaces the removed `.clear()` calls to force collection reload.
### Schema parity (`src/cleveragents/infrastructure/database/models.py`)
- Added `UniqueConstraint("action_name", "position")` to `ActionInvariantModel`.
- Added `UniqueConstraint("action_name", "name")`, `CheckConstraint` for `arg_type`, and `CheckConstraint` for `requirement` to `ActionArgumentModel`.
### Alembic migration (`alembic/versions/a5_006_action_invariants_unique_constraint.py`)
- New migration adds all four constraints to both `action_invariants` and `action_arguments` tables.
- Includes deduplication guards and data normalization so the upgrade succeeds on existing databases with invalid or duplicate rows.
- Uses `batch_alter_table` for SQLite compatibility.
### Tests
- **Behave** (`features/plan_use_action_args_integrity.feature`): 6 scenarios covering the core bug path, zero-argument regression, multiple arguments, reusable action double-use, non-reusable action archival with invariants, and direct repository update.
- **Robot** (`robot/plan_use_action_args_integrity.robot`): Integration test mirroring the Behave scenarios via a helper script.
- **Shared factory** (`features/mocks/test_uow_factory.py`): Extracted `build_test_uow()` from both test suites into a single shared module to eliminate duplication (DRY).
### Minor
- Updated `src/cleveragents/domain/repositories/__init__.py` docstring from table format to bullet list (conflict resolution from rebase).
Closes #4174
Reviewed-on: cleveragents/cleveragents-core#4197
Reviewed-by: HAL 9000 <HAL9000@cleverthis.com >
Co-authored-by: Rui Hu <rui.hu@cleverthis.com >
Co-committed-by: Rui Hu <rui.hu@cleverthis.com >
2026-04-09 06:11:39 +00:00
..
2026-04-05 17:53:56 +00:00
2026-04-03 00:29:46 +00:00
2026-03-31 16:01:58 +00:00
2026-03-31 21:47:12 +00:00
2026-04-03 00:29:46 +00:00
2026-04-05 17:53:56 +00:00
2026-04-04 20:38:16 +00:00
2026-04-05 17:53:56 +00:00
2026-04-05 17:53:56 +00:00
2026-04-05 21:10:14 +00:00
2026-04-02 23:53:13 +00:00
2026-04-01 06:16:41 +00:00
2026-04-01 06:16:41 +00:00
2026-04-05 21:26:33 +00:00
2026-03-26 06:19:14 +00:00
2026-03-31 21:47:12 +00:00
2026-03-30 23:40:36 +01:00
2026-04-05 08:12:32 +00:00
2026-04-05 21:19:40 +00:00
2026-04-05 07:49:49 +00:00
2026-04-05 21:19:40 +00:00
2026-03-26 13:41:31 +00:00
2026-04-05 21:19:40 +00:00
2026-04-05 21:19:40 +00:00
2026-04-05 19:48:40 +00:00
2026-03-31 21:47:12 +00:00
2026-03-28 05:27:14 +00:00
2026-03-28 05:27:14 +00:00
2026-04-05 18:18:08 +00:00
2026-03-26 13:41:31 +00:00
2026-03-31 16:01:58 +00:00
2026-03-31 16:01:58 +00:00
2026-04-02 16:53:03 +00:00
2026-03-20 21:22:10 +00:00
2026-04-04 20:38:16 +00:00
2026-03-20 21:22:10 +00:00
2026-03-30 17:06:49 -04:00
2026-03-31 21:47:12 +00:00
2026-03-27 23:53:22 +00:00
2026-04-04 20:38:16 +00:00
2026-04-02 17:00:53 +00:00
2026-04-02 16:59:07 +00:00
2026-03-22 05:01:06 +00:00
2026-03-20 21:22:10 +00:00
2026-04-02 06:21:49 +00:00
2026-03-30 13:18:07 +01:00
2026-03-30 13:18:07 +01:00
2026-04-05 09:46:16 +00:00
2026-03-30 13:18:07 +01:00
2026-03-30 13:18:07 +01:00
2026-04-01 01:46:37 +00:00
2026-04-05 17:55:54 +00:00
2026-03-21 05:30:01 +00:00
2026-04-05 21:13:07 +00:00
2026-04-05 21:13:07 +00:00
2026-03-20 21:22:10 +00:00
2026-03-31 21:47:12 +00:00
2026-03-21 00:29:49 +00:00
2026-03-20 21:22:10 +00:00
2026-03-30 21:39:26 +00:00
2026-03-21 01:14:18 +00:00
2026-03-31 11:03:20 +00:00
2026-03-23 22:22:16 +00:00
2026-04-02 19:09:04 +00:00
2026-04-05 08:39:14 +00:00
2026-04-05 19:48:40 +00:00
2026-04-02 19:09:04 +00:00
2026-04-02 19:09:04 +00:00
2026-03-31 21:47:12 +00:00
2026-04-02 19:09:04 +00:00
2026-04-05 08:39:14 +00:00
2026-04-05 19:48:40 +00:00
2026-03-19 23:30:55 +00:00
2026-03-22 05:01:06 +00:00
2026-03-31 21:47:12 +00:00
2026-03-20 21:22:10 +00:00
2026-04-01 06:16:41 +00:00
2026-04-05 19:48:40 +00:00
2026-04-05 19:48:40 +00:00
2026-03-31 21:47:12 +00:00
2026-03-22 05:01:06 +00:00
2026-04-05 19:48:40 +00:00
2026-03-31 21:47:12 +00:00
2026-03-30 21:48:01 +00:00
2026-03-30 21:48:01 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-04-05 04:33:46 +00:00
2026-04-05 04:33:46 +00:00
2026-03-27 12:57:03 +00:00
2026-03-24 08:46:58 +00:00
2026-03-20 21:22:10 +00:00
2026-04-05 20:56:32 +00:00
2026-03-25 17:49:06 +00:00
2026-03-21 01:40:58 +00:00
2026-03-30 21:39:26 +00:00
2026-03-29 16:23:04 +01:00
2026-03-30 17:06:49 -04:00
2026-03-20 21:22:10 +00:00
2026-03-31 21:47:12 +00:00
2026-04-02 16:59:40 +00:00
2026-03-20 21:22:10 +00:00
2026-04-05 04:09:27 +00:00
2026-04-02 08:31:27 +00:00
2026-04-05 10:17:24 +00:00
2026-04-02 16:58:36 +00:00
2026-03-31 11:03:20 +00:00
2026-04-04 20:38:16 +00:00
2026-03-31 21:47:12 +00:00
2026-03-26 07:52:09 +00:00
2026-04-04 20:38:16 +00:00
2026-03-20 21:22:10 +00:00
2026-03-30 23:40:36 +01:00
2026-03-30 23:40:36 +01:00
2026-03-30 23:40:36 +01:00
2026-04-04 20:38:16 +00:00
2026-03-20 21:22:10 +00:00
2026-03-18 17:16:32 +00:00
2026-03-31 21:47:12 +00:00
2026-03-20 21:22:10 +00:00
2026-03-19 07:53:43 +00:00
2026-03-19 07:53:43 +00:00
2026-04-01 06:16:41 +00:00
2026-03-20 21:22:10 +00:00
2026-04-04 20:38:16 +00:00
2026-03-31 21:47:12 +00:00
2026-04-02 17:08:52 +00:00
2026-04-08 11:02:14 +00:00
2026-03-20 21:22:10 +00:00
2026-04-05 21:06:04 +00:00
2026-03-20 21:22:10 +00:00
2026-03-27 23:53:22 +00:00
2026-04-03 02:01:45 +00:00
2026-04-08 11:02:14 +00:00
2026-04-04 20:38:16 +00:00
2026-03-21 01:40:58 +00:00
2026-04-02 17:07:48 +00:00
2026-04-02 16:53:03 +00:00
2026-04-02 16:59:07 +00:00
2026-03-31 16:01:58 +00:00
2026-03-31 11:31:00 +00:00
2026-03-31 21:47:12 +00:00
2026-03-31 16:01:58 +00:00
2026-03-31 21:47:12 +00:00
2026-03-30 13:19:08 +00:00
2026-04-02 16:58:39 +00:00
2026-04-05 17:49:20 +00:00
2026-03-24 22:01:48 +00:00
2026-03-31 21:47:12 +00:00
2026-03-24 22:01:48 +00:00
2026-03-31 21:47:12 +00:00
2026-03-31 21:47:12 +00:00
2026-04-05 08:29:55 +00:00
2026-03-29 18:57:15 +01:00
2026-04-05 18:20:15 +00:00
2026-04-05 18:20:15 +00:00
2026-04-05 05:46:05 +00:00
2026-04-02 16:59:14 +00:00
2026-04-05 08:26:50 +00:00
2026-03-30 09:21:51 +00:00
2026-03-27 11:10:56 +00:00
2026-04-02 16:58:32 +00:00
2026-03-31 21:47:12 +00:00
2026-03-30 11:51:27 +00:00
2026-03-31 21:47:12 +00:00
2026-03-24 13:01:12 +00:00
2026-04-02 16:52:33 +00:00
2026-04-05 07:06:53 +00:00
2026-03-24 13:01:12 +00:00
2026-03-31 21:47:12 +00:00
2026-04-04 23:48:02 +00:00
2026-04-02 16:52:33 +00:00
2026-03-31 21:47:12 +00:00
2026-04-02 19:09:04 +00:00
2026-04-02 16:53:03 +00:00
2026-04-04 20:38:16 +00:00
2026-04-04 20:38:16 +00:00
2026-03-30 13:18:07 +01:00
2026-03-30 17:48:48 +00:00
2026-03-31 21:47:12 +00:00
2026-03-31 21:47:12 +00:00
2026-04-05 07:48:02 +00:00
2026-03-30 22:42:15 +00:00
2026-03-30 22:42:15 +00:00
2026-03-18 17:16:32 +00:00
2026-03-30 23:40:36 +01:00
2026-04-02 16:58:36 +00:00
2026-03-30 19:52:51 +00:00
2026-03-31 21:47:12 +00:00
2026-03-29 18:57:15 +01:00
2026-04-02 17:08:38 +00:00
2026-04-02 17:07:41 +00:00
2026-04-02 19:09:04 +00:00
2026-04-02 19:09:04 +00:00
2026-04-02 19:09:04 +00:00
2026-04-05 19:44:17 +00:00
2026-04-05 19:46:23 +00:00
2026-04-04 20:38:16 +00:00
2026-04-02 19:09:04 +00:00
2026-03-31 01:49:16 +00:00
2026-04-05 21:11:36 +00:00
2026-04-02 19:09:04 +00:00
2026-03-30 17:06:49 -04:00
2026-03-20 21:22:10 +00:00
2026-04-05 20:46:24 +00:00
2026-04-05 19:48:40 +00:00
2026-03-20 21:22:10 +00:00
2026-04-02 19:09:04 +00:00
2026-04-02 19:09:04 +00:00
2026-03-30 23:40:36 +01:00
2026-03-23 23:33:33 +00:00
2026-03-20 21:22:10 +00:00
2026-03-30 13:18:07 +01:00
2026-03-31 21:47:12 +00:00
2026-04-01 04:27:09 +00:00
2026-03-30 13:18:07 +01:00
2026-04-03 13:38:33 -04:00
2026-04-04 20:38:16 +00:00
2026-03-23 23:33:33 +00:00
2026-03-30 23:40:36 +01:00
2026-04-04 20:38:16 +00:00
2026-04-05 04:38:31 +00:00
2026-03-20 21:22:10 +00:00
2026-04-03 00:01:48 +00:00
2026-04-09 06:11:39 +00:00
2026-04-05 19:48:40 +00:00
2026-03-30 23:40:36 +01:00
2026-04-02 21:30:05 +00:00
2026-03-20 21:22:10 +00:00
2026-04-04 20:38:16 +00:00
2026-03-20 21:22:10 +00:00
2026-04-02 16:59:48 +00:00
2026-03-22 05:01:06 +00:00
2026-03-19 06:53:06 +00:00
2026-04-05 08:22:36 +00:00
2026-03-30 17:12:20 +00:00
2026-03-30 08:42:48 +00:00
2026-03-30 14:33:28 +00:00
2026-03-30 14:33:28 +00:00
2026-03-20 21:22:10 +00:00
2026-03-31 21:47:12 +00:00
2026-04-05 18:00:27 +00:00
2026-03-19 09:30:35 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-03-31 21:47:12 +00:00
2026-03-30 10:18:19 +00:00
2026-03-20 22:28:32 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-03-30 10:18:19 +00:00
2026-03-20 21:22:10 +00:00
2026-03-25 13:05:04 +00:00
2026-03-30 23:40:36 +01:00
2026-03-30 23:40:36 +01:00
2026-03-30 23:40:36 +01:00
2026-03-30 13:18:07 +01:00
2026-03-31 21:47:12 +00:00
2026-04-02 08:31:27 +00:00
2026-04-02 08:31:27 +00:00
2026-03-20 21:22:10 +00:00
2026-04-04 20:38:16 +00:00
2026-04-01 13:02:24 +00:00
2026-04-04 20:38:16 +00:00
2026-04-04 20:38:16 +00:00
2026-03-31 21:47:12 +00:00
2026-04-05 18:13:11 +00:00
2026-04-08 11:02:14 +00:00
2026-04-05 07:29:35 +00:00
2026-04-02 08:31:27 +00:00
2026-03-30 13:44:54 +00:00
2026-03-19 14:03:17 +00:00
2026-04-05 09:03:32 +00:00
2026-03-24 13:21:12 +00:00
2026-03-20 21:22:10 +00:00
2026-04-05 08:04:27 +00:00
2026-03-31 21:47:12 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-03-28 00:57:51 +00:00
2026-04-02 16:53:03 +00:00
2026-03-29 18:57:15 +01:00
2026-03-21 03:14:40 +00:00
2026-03-29 18:57:15 +01:00
2026-03-31 21:47:12 +00:00
2026-03-29 18:57:15 +01:00
2026-04-05 04:37:45 +00:00
2026-03-20 21:22:10 +00:00
2026-03-21 04:46:45 +00:00
2026-04-02 17:00:53 +00:00
2026-03-30 13:18:07 +01:00
2026-03-27 23:53:22 +00:00
2026-04-04 20:38:16 +00:00
2026-04-04 23:08:32 +00:00
2026-03-20 21:22:10 +00:00
2026-04-04 23:08:32 +00:00
2026-03-31 21:47:12 +00:00
2026-04-05 07:28:12 +00:00
2026-04-05 21:12:13 +00:00
2026-04-05 19:48:40 +00:00
2026-04-05 09:38:43 +00:00
2026-04-05 18:19:19 +00:00
2026-04-04 20:38:16 +00:00
2026-03-20 21:22:10 +00:00
2026-04-05 07:15:48 +00:00
2026-04-05 21:11:53 +00:00
2026-04-05 19:48:40 +00:00
2026-04-05 19:48:40 +00:00
2026-04-05 19:48:40 +00:00
2026-03-20 21:22:10 +00:00
2026-04-08 11:02:14 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-03-31 23:57:39 +00:00
2026-04-05 07:18:27 +00:00
2026-03-28 05:27:14 +00:00
2026-03-28 05:27:14 +00:00
2026-04-03 19:00:22 +00:00
2026-04-02 16:50:50 +00:00
2026-03-27 05:58:35 +00:00
2026-04-06 07:55:09 +00:00
2026-04-02 16:51:15 +00:00
2026-03-27 05:58:35 +00:00
2026-04-02 16:51:29 +00:00
2026-04-05 17:52:59 +00:00
2026-03-28 00:30:26 +00:00
2026-03-27 05:58:35 +00:00
2026-03-26 01:02:10 +00:00
2026-03-27 22:53:33 +00:00
2026-03-27 05:58:35 +00:00
2026-04-05 07:39:57 +00:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-04-03 03:58:45 +00:00
2026-04-03 19:03:15 +00:00
2026-03-28 04:15:49 +00:00
2026-04-02 19:09:04 +00:00
2026-04-01 00:40:46 +00:00
2026-03-30 17:06:49 -04:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-04-04 20:38:16 +00:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-03-22 01:54:33 +00:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-03-27 05:58:35 +00:00
2026-04-08 11:02:14 +00:00
2026-04-01 04:27:09 +00:00
2026-03-31 07:42:06 +00:00
2026-04-02 23:53:13 +00:00
2026-04-02 23:59:37 +00:00
2026-04-06 07:55:09 +00:00
2026-03-20 23:51:46 +00:00
2026-04-02 16:53:03 +00:00
2026-04-05 04:19:52 +00:00
2026-04-02 16:53:03 +00:00
2026-04-06 07:55:09 +00:00
2026-04-02 17:07:26 +00:00
2026-04-02 17:07:26 +00:00
2026-04-02 17:07:26 +00:00
2026-03-31 11:31:00 +00:00
2026-04-06 07:55:09 +00:00
2026-03-29 18:57:15 +01:00
2026-04-01 21:51:14 +00:00
2026-04-05 18:08:09 +00:00
2026-04-04 20:38:16 +00:00
2026-04-05 18:08:09 +00:00
2026-04-05 18:09:41 +00:00
2026-04-03 05:56:27 +00:00
2026-04-02 17:08:13 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-03-20 21:22:10 +00:00
2026-04-05 17:52:30 +00:00
2026-04-02 20:48:33 +00:00
2026-03-20 21:22:10 +00:00
2026-04-01 09:23:21 +00:00
2026-04-05 17:50:51 +00:00
2026-04-05 17:50:51 +00:00
2026-04-03 05:56:27 +00:00
2026-04-02 17:09:15 +00:00
2026-04-06 07:55:09 +00:00
2026-04-05 17:52:59 +00:00
2026-03-31 21:47:12 +00:00
2026-04-01 03:30:00 +00:00
2026-04-04 20:38:16 +00:00
2026-03-30 11:47:37 +08:00