discourse/plugins/discourse-ai/db
Rafael dos Santos Silva 0bc1216009
FIX: Scope ReviewableAiToolAction to target post's topic and category (#39716)
## Summary

`ReviewableAiToolAction` records were being created without `topic_id`
or `category_id`. `Reviewable#created_new!` only copies those fields
when the target is a `Post`, but the target here is `AiToolAction`, so
every record landed unscoped. As a result, `Reviewable.viewable_by`'s
`category_id IS NULL` allowance made these reviewables visible to
**all** moderators — including ones who couldn't access the restricted
category the underlying action targeted.

This brings `ReviewableAiToolAction` in line with how
`ReviewableFlaggedPost` already behaves for the same shape of data:

- Override `created_new!` to read the target action's post and copy
`topic`/`category_id` onto the reviewable, so category moderation groups
route the queue entry to the right reviewers.
- Memoize a `target_post` accessor for reuse by the model and
serializer.
- Backfill migration updates pre-existing rows by joining
`ai_tool_actions → posts → topics`. Idempotent via `topic_id IS NULL`
guard.

This is intentionally a routing/scoping fix only — the serializer is
unchanged. AI agents using approval-required tools are admin-configured
and opt-in to human review, and reviewers need the full post and tool
parameters to make an informed decision; suppressing those would defeat
the human-in-the-loop the admin enabled.

## Test plan

- [x] `bin/rspec
plugins/discourse-ai/spec/models/reviewable_ai_tool_action_spec.rb`
passes (new `#created_new!` cases covering private-category routing and
the no-post fallback)
- [x] `bin/rspec
plugins/discourse-ai/spec/db/migrate/20260504211108_backfill_reviewable_ai_tool_action_scope_spec.rb`
passes (backfill, no-post stays nil, already-scoped rows untouched)
- [x] `bin/lint` clean on all four files
2026-05-05 10:24:19 -03:00
..
fixtures DEV: Add the ability to register an AI module for data explorer (#38891) 2026-04-03 13:59:06 +08:00
migrate FIX: Scope ReviewableAiToolAction to target post's topic and category (#39716) 2026-05-05 10:24:19 -03:00
post_migrate DEV: Update DropToolDetailsFromAiPersonas following table rename flip (#38596) 2026-03-13 19:25:41 +00:00