Skip to content

Fix media deletion’s community ownership and share agent discovery - #9670

Merged
ravarora2 merged 5 commits into
mainfrom
rarora/media-community-ownership
Nov 19, 2026
Merged

ravarora2 merged 5 commits into
mainfrom
rarora/media-community-ownership

Conversation

@ravarora2

@ravarora2 ravarora2 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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: false because PostgreSQL cannot combine GROUP BY with FOR 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 test completed with one failure in the unchanged relay logging test composition_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. Local just ci was 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 --check passed. Runtime suites were not repeated for this follow-up.

Human local acceptance remains pending.

Generated with Codex

ravarora2 and others added 3 commits November 18, 2026 12:34
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>
@ravarora2
ravarora2 marked this pull request as ready for review November 18, 2026 19:54
@ravarora2
ravarora2 requested a review from a team as a code owner November 18, 2026 19:54
@ravarora2
ravarora2 deployed to codex-review November 18, 2026 19:55 — with GitHub Actions Active
@github-actions github-actions Bot added the codex-security-review-current The posted Codex security review matches its recorded range. label Nov 18, 2026
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

🔐 Codex Security Review

Note: This is an automated, security-focused review generated by Codex.
Use it as a supplement to human review; false positives are possible.

Scope

  • Exact PR diff: 7bff21e513fdab311d625278c8379d2bb9ed9ef1...43afcf1494420c11255da7202ab9bee3f91290d2
  • Model: gpt-6.1-sol

💡 Click "edited" above to see earlier reviews for this PR.


Review Summary

Overall Risk: NONE

No concrete security, correctness, or reliability regressions found in the authorized PR range.

Findings

No concrete security, correctness, or reliability findings were identified.

Notes

  • Read-only source review; builds and tests were not run.
  • Media deletion relies on the documented requirement that writers remain stopped throughout cleanup.

Generated by Codex Security Review |
Requested by: @ravarora2 |
Workflow run

@wpfleger96 wpfleger96 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 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 discover lookup in agent_ownership.rs returns the same direct, community-local ownership as the queries it replaces: FOR SHARE locking 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 flattened target_pubkeys list is never used as deletion authority. community_id is NOT NULL in both media_uploaders and audit_log, so the pair filters can't silently drop rows. Storage upload records are checked against their path's community before contains is used.
  • require_current_targets compares 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)

  1. 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.
  2. combined_targets_share_one_inventory_and_preserve_outside_uploaders no longer pins the recheck count. The exact target_calls assertions became >= 1 and > 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.
  3. 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>
@github-actions github-actions Bot removed the codex-security-review-current The posted Codex security review matches its recorded range. label Nov 18, 2026
@ravarora2
ravarora2 deployed to codex-review November 18, 2026 23:25 — with GitHub Actions Active
@ravarora2 ravarora2 changed the title Fix community ownership across deletion handlers Fix media deletion’s community ownership and share agent discovery Nov 18, 2026
@github-actions github-actions Bot added the codex-security-review-current The posted Codex security review matches its recorded range. label Nov 18, 2026

@wpfleger96 wpfleger96 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 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 from media_uploaders and media_uploaded audit rows, both database sharing checks, storage upload-claim matching in scan_storage, and uploader-row deletion. Both community_id columns are NOT 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 SHARE locks. 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-batches with --read-only, then --delete, then --delete again. 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

  1. Media discovery has no bound on community/agent pairs (agent_ownership.rs:52-90). The grouped media query checks only distinct identities (agents + 1). The array_agg arrays are then expanded into one (community, key) pair each, with no cap. Those pairs are copied again into the handler's HashSet (media.rs:167), and pubkeys() 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.
  2. 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 in target_pubkeys, so the report contradicts itself. Reword the reason to cover outside-community uploads by target agents.
  3. discover accepts a combination PostgreSQL rejects (agent_ownership.rs:52-74). community: None with TargetKeys and lock: true builds GROUP 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>
@github-actions github-actions Bot removed the codex-security-review-current The posted Codex security review matches its recorded range. label Nov 19, 2026
@ravarora2
ravarora2 deployed to codex-review November 19, 2026 19:53 — with GitHub Actions Active
@github-actions github-actions Bot added the codex-security-review-current The posted Codex security review matches its recorded range. label Nov 19, 2026

@wpfleger96 wpfleger96 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🤖 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 discover doc comment is accurate. With community: None and DiscoveryLimit::TargetKeys, the query uses GROUP BY pubkey. It still appends FOR SHARE whenever lock is true (agent_ownership.rs:55-78), so lock: false is a precondition the caller must meet. All five production callers meet it. Media is the only grouped caller, and it passes lock: false. Content, profile and account use community-scoped discovery. Account scope capture uses AgentRows.
  • 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-matching HashSet (media.rs:167). Every candidate page binds the full arrays. The inventory budget counts only its own collections.

Non-blocking findings

  1. The pair arrays are sent with more than the candidate-page query. user_media_shared_hashes and 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.
  2. 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 in target_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.

@ravarora2
ravarora2 merged commit 69ea273 into main Nov 19, 2026
91 checks passed
@ravarora2
ravarora2 deleted the rarora/media-community-ownership branch November 19, 2026 23:23
morgmart added a commit that referenced this pull request Nov 20, 2026
…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

This branch was successfully deployed

1 active deployment
codex-review — 43afcf14 Deployed Nov 19, 2026 by ravarora2 via Run Codex Security Review #16451
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex-security-review-current The posted Codex security review matches its recorded range.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants