Repository navigation
Fix media deletion’s community ownership and share agent discovery - #9670
Conversation
Keep agent ownership scoped to each community through upload evidence and cleanup. Refresh ownership on retries and refuse a changed set before mutations. Co-authored-by: dessert <7d616e7e143e5d408159614dccff1371350a1c1bd36c8de838a45ece5aca0aca@buzz.block.builderlab.xyz> Signed-off-by: Ravneet Arora <rarora@squareup.com>
Keep current direct ownership scoped to communities in every handler. Preserve batch row locks and retry discovery, including profile cleanup of its remaining identities. Co-authored-by: dessert <7d616e7e143e5d408159614dccff1371350a1c1bd36c8de838a45ece5aca0aca@buzz.block.builderlab.xyz> Signed-off-by: Ravneet Arora <rarora@squareup.com>
Co-authored-by: dessert <7d616e7e143e5d408159614dccff1371350a1c1bd36c8de838a45ece5aca0aca@buzz.block.builderlab.xyz> Signed-off-by: Ravneet Arora <rarora@squareup.com>
🔐 Codex Security Review
Review SummaryOverall Risk: NONE
FindingsNo concrete security, correctness, or reliability findings were identified. Notes
Generated by Codex Security Review | |
wpfleger96
left a comment
There was a problem hiding this comment.
🤖 Automated multi-lane review at head 4b038e55: two independent code reviews plus a live run of buzz-admin user-deletions media-batches against a throwaway PostgreSQL and versioned MinIO. Verdict: clear, no blocking findings.
What checked out
- The shared
discoverlookup inagent_ownership.rsreturns the same direct, community-local ownership as the queries it replaces:FOR SHARElocking is still owned by the caller, deactivated profiles still count, and profile cleanup still drops agents removed by earlier batches from its remaining targets. - Media candidate selection, the outside-uploader check and uploader-row deletion all use
(community_id, pubkey)pairs. The flattenedtarget_pubkeyslist is never used as deletion authority.community_idisNOT NULLin bothmedia_uploadersandaudit_log, so the pair filters can't silently drop rows. Storage upload records are checked against their path's community beforecontainsis used. require_current_targetscompares the full pair list, so moving an agent's ownership between communities is detected even when the flattened keys don't change.- Live run with the same agent owned by the deleted human in one community and by another human in a second community:
| Scenario | Result |
|---|---|
--read-only preview |
All 80 tables and all 86 stored versions and delete markers unchanged |
--delete |
24 exclusive versions and 6 in-scope uploader rows removed. The other owner's originals, thumbnails and historical versions all survived |
| Outside evidence from only an uploader row, only an audit entry, or only a historical storage claim | File kept in each case |
| Ownership transferred after discovery, before mutation | Exit 5 with media agent ownership changed, nothing mutated. The retry used the new ownership |
| Storage refuses a delete partway through (retention lock on a historical thumbnail), then ownership swaps before retry | Uploader rows kept for the failed file. The retry removed only the new owner's exclusive files |
Repeat --delete |
0 versions and 0 rows removed |
MINOR (nonblocking)
- The full ownership lookup now runs before every object-version delete. Each check opens its own transaction and reads up to 10,000 rows, so a large job makes one extra query per version. Since the docs already say these checks don't replace stopping writers, one check per file (before its first delete and before the uploader-row removal) would give the same detection for far fewer queries.
combined_targets_share_one_inventory_and_preserve_outside_uploadersno longer pins the recheck count. The exacttarget_callsassertions became>= 1and> 2, so the test would still pass if a recheck were dropped. Asserting the exact count for preview and delete would cover the new checks.- The PR description is out of date. It says "this PR remains a draft", but the PR is marked ready for review.
Preserve original caller limits and media report shape. Match storage claims with a community-key hash set and retain ownership in another community in the retry regression. Signed-off-by: dessert <7d616e7e143e5d408159614dccff1371350a1c1bd36c8de838a45ece5aca0aca@buzz.block.builderlab.xyz>
wpfleger96
left a comment
There was a problem hiding this comment.
🤖 Automated multi-lane review at head fa90c88a. Two independent code reviews plus a live end-to-end run of the media handler. Verdict: no blocking findings. Three non-blocking findings are listed below.
What holds
- Every media decision applies the same rule: the human's uploads are targets in every community, and an agent's uploads are targets only for an exact
(community_id, agent)pair the human currently owns. This covers candidate discovery frommedia_uploadersandmedia_uploadedaudit rows, both database sharing checks, storage upload-claim matching inscan_storage, and uploader-row deletion. Bothcommunity_idcolumns areNOT NULL, so row-pair matching has no null gap. - The content, account, profile and account-scope callers keep their previous limits, ordering, transaction connections and
FOR SHARElocks. Profile cleanup still rechecks the shrinking target set after its own committed batches. - Inventory completes before mutation, and uploader rows are removed only after a file's storage deletions succeed.
- Live run against throwaway PostgreSQL and versioned MinIO, using
buzz-admin user-deletions media-batcheswith--read-only, then--delete, then--deleteagain. The fixture covered 17 file families: conflicting ownership, missing and deactivated profiles, sharing with another human, audit-only and storage-only discovery, outside claims listed before target claims, and outside uploaders present only in historical claim versions. Read-only left all public tables and every storage version unchanged. Delete removed 48 version entries and 8 authorized uploader rows, and every outside uploader row and protected original, thumbnail, sidecar, claim and historical version survived. Repeat delete removed nothing.
Non-blocking findings
- Media discovery has no bound on community/agent pairs (
agent_ownership.rs:52-90). The grouped media query checks only distinct identities (agents + 1). Thearray_aggarrays are then expanded into one(community, key)pair each, with no cap. Those pairs are copied again into the handler'sHashSet(media.rs:167), andpubkeys()clones and sorts all of them on every candidate, shared-check and remove call. Before this change, media discovery held at most 10,000 key strings. With many agents across many communities, memory and bind-parameter size grow with agents × communities. This needs a very large fleet and fails before any mutation, but the bound is easy to restore. Fix: also cap the total pair count. Reusing account scope capture's 10,000-row bound would match that caller, and the distinct-identity check stays as it is. Overflow must abort, never truncate. Compute the flattened distinct keys once per invocation rather than per call, and add a many-communities/few-agents capacity test. - The keep reason in the report is now inaccurate for one case (
media_report.rs:39). A file kept only because the same agent uploaded it in a community where the human does not own it still reports "Uploaded by someone outside the target set". That agent is listed intarget_pubkeys, so the report contradicts itself. Reword the reason to cover outside-community uploads by target agents. discoveraccepts a combination PostgreSQL rejects (agent_ownership.rs:52-74).community: NonewithTargetKeysandlock: truebuildsGROUP BY pubkey ... FOR SHARE, which PostgreSQL rejects at runtime. No caller does this today. Reject that combination up front, so a future locked global caller gets a clear error.
Signed-off-by: dessert <7d616e7e143e5d408159614dccff1371350a1c1bd36c8de838a45ece5aca0aca@buzz.block.builderlab.xyz>
wpfleger96
left a comment
There was a problem hiding this comment.
🤖 Automated review at head 43afcf14. The only change since the last automated review at fa90c88a is the documentation commit "docs(deletion): clarify discovery constraints". Two independent code reviews checked each new statement against the source. Verdict: no blocking findings. Nothing that runs changed, so no new live run was needed.
What checks out
- The
discoverdoc comment is accurate. Withcommunity: NoneandDiscoveryLimit::TargetKeys, the query usesGROUP BY pubkey. It still appendsFOR SHAREwheneverlockis true (agent_ownership.rs:55-78), solock: falseis a precondition the caller must meet. All five production callers meet it. Media is the only grouped caller, and it passeslock: false. Content, profile and account use community-scoped discovery. Account scope capture usesAgentRows. - The operator-guide paragraph is accurate. The limit counts grouped identities before their community arrays are expanded, and the expanded pairs have no cap (
agent_ownership.rs:79-93). The pairs are held in the handler's storage-matchingHashSet(media.rs:167). Every candidate page binds the full arrays. The inventory budget counts only its own collections.
Non-blocking findings
- The pair arrays are sent with more than the candidate-page query.
user_media_shared_hashesand the uploader-row delete also bind the full community and agent-key arrays on each call. The paragraph's conclusion still holds. Naming all three queries would make it complete. - Still open from the review at
fa90c88a: the report's keep reason (media_report.rs:39). Suppose a file is kept only because the same agent uploaded it in a community the human does not own. It still reports "Uploaded by someone outside the target set", but that agent is listed intarget_pubkeys. The operator guide now states this case keeps the file, so the mismatch is easier to see. Rewording the reason to cover uploads in communities outside the human's ownership would fix it.
…ead-dot * origin/main: (34 commits) Fix media deletion’s community ownership and share agent discovery (#9670) buzz-db: drop the remaining relay foreign keys (#9740) perf(mobile): hash upload payloads in bounded worker chunks (#9747) feat(read-state): sync mark read, channel mark unread and thread follows (#9501) Fix shared stars and mutes in mobile and CLI (#9684) fix(mobile): keep active iOS Huddles connected while locked (#9682) fix(mobile): hash upload payloads off the UI isolate (#9736) perf(relay): serve named-only channel access from the access caches (#9735) fix(mobile): stop notification extension sending cookies to Builderlab (#9741) Remove personal workflow data and missed event copies (#9675) fix(relay): rename target log field to target_pubkey (#9739) feat(push-gateway): declarative desired-state schema, one file per table (#8328) test(cli): keep the raw refusal exit test independent of BUZZ_* env (#9733) docs(nip-pm): define pinned messages (#9712) push-gateway: drop the delegation → installation foreign key (#9610) Match desktop unread cues on mobile channel rows (#9715) feat(mobile): refresh Huddle speaking styles and iOS controls (#9716) feat(cli): add raw query and publish escape-hatch commands (#9653) fix(mobile): only say "Community changed" when the community changed (#9156) chore: require relay owner review for the relay and db crates (#9728) ... Signed-off-by: morgmart <98432065+morgmart@users.noreply.github.com> # Conflicts: # mobile/lib/features/activity/activity_page.dart # mobile/lib/features/channels/channel_unread.dart
Summary
Deleting a human must preserve an agent's uploads in communities where someone else owns that agent or its profile is missing. Media cleanup now carries community/key pairs through database and storage discovery, shared-file decisions, and uploader-row deletion.
Media, content, account and profile cleanup share one direct-agent discovery utility. Each caller retains its previous limits and transaction behavior. Media discovers ownership once per invocation, including retries, and uses a hash set for storage matching. The report shape stays unchanged. The operator guide explains that community/agent pairs have no separate cap and sit outside the inventory budget. The discovery helper documents that global, identity-grouped discovery requires
lock: falsebecause PostgreSQL cannot combineGROUP BYwithFOR SHARE.Run a read-only preview first, then keep writers stopped throughout deletion. Reverting the binary restores the old cross-community selection bug; it cannot restore deleted objects.
Related issue
Follow-up to the merged media handler in #9365 and the content/account/profile handlers in #9363, #9364 and #9580. No duplicate of this ownership correction was identified.
Testing
Exercised the rebuilt
buzz-admin user-deletions media-batches --pubkey <fixture-human> --read-only, then--delete, against isolated PostgreSQL and versioned MinIO. Preview changed neither rows nor storage. Exclusive versions and in-scope uploader rows were deleted; shared originals, thumbnails and historical versions survived. All four handler commands agreed before and after a between-invocation ownership change. Failure followed by retry used fresh ownership, and repeat deletion removed zero versions.Compared the production candidate SQL with the pre-PR query on about 50,000 local upload/audit rows. With 9,999 agents, execution took 120–185 ms per 500-row page versus 38–83 ms before community matching. Three runs per query; this excludes planning, Rust preparation and storage operations. The added per-version ownership queries are removed entirely.
Full database/deletion/admin package runs passed (339 tests). The PostgreSQL database/deletion run passed 904 tests; after strengthening the retry fixture, the full deletion PostgreSQL package passed all 45 tests. The retry case retains ownership in another community, and media tests assert exactly one ownership lookup per invocation.
Local
just testcompleted with one failure in the unchanged relay logging testcomposition_log_does_not_leak_issuer_url(expected warning not captured); all other test groups passed. Two pre-push attempts failed in unchanged ACP tests: first a launch timeout, then credential tests using Git 2.40 where 2.46+ is required. All four credential integration tests passed with Git 2.54; the full pre-push retry was stopped at the author’s request to skip local checks and push. Localjust ciwas also stopped before completion. Neither interrupted run is a full pass; local hooks were skipped for publication.The follow-up for the optional review comments changes documentation only. Its statements were checked against the query and all five production callers;
git diff --checkpassed. Runtime suites were not repeated for this follow-up.Human local acceptance remains pending.
Generated with Codex