Skip to content

Harden media usage repair behavior#1850

Merged
khoinguyenpham04 merged 8 commits into
mainfrom
media-usage-repair-hardening-main
Jul 7, 2026
Merged

Harden media usage repair behavior#1850
khoinguyenpham04 merged 8 commits into
mainfrom
media-usage-repair-hardening-main

Conversation

@khoinguyenpham04

@khoinguyenpham04 khoinguyenpham04 commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Hardens collection-scope content media usage repair before the operator entry point ships.

  • Preserves valid columns usage when draft_overlay snapshot loading fails.
  • Treats content rows deleted during repair as repair conflicts/skips instead of source failures.
  • Preserves fresher runtime usage during guarded repair deletes and final status races.
  • Adds regression coverage for missing draft revisions, malformed repeater metadata, no-media scopes, concurrent writes/deletes, and stale status races.

Related: Discussion #1503.
Builds on merged repair foundation PR #1846.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: GPT-5.5 via OpenCode

Screenshots / test output

No screenshots; backend repair hardening only.

Verified on media-usage-repair-hardening-main:

pnpm format
pnpm lint
pnpm typecheck
pnpm --dir packages/core exec vitest run tests/integration/database/media-usage-repository.test.ts tests/integration/database/media-usage-content-repair.test.ts tests/integration/database/media-usage-content-refresh.test.ts tests/integration/database/media-usage-content-snapshots.test.ts  # 85 passed
pnpm lint:json | jq '.diagnostics | length'  # 0
pnpm changeset status --since origin/main  # emdash patch bump found
second-opinion review cycle 3  # No findings.

Try this PR

Open a fresh playground →

A full working EmDash site, deployed from this branch. Each visit gets its own session-scoped sandbox: no login needed and no shared state. Try the admin, edit content, hit the public site.

Tracks media-usage-repair-hardening-main. Updated automatically when the playground redeploys.

Copilot AI review requested due to automatic review settings July 6, 2026 22:10
@changeset-bot

changeset-bot Bot commented Jul 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 6266fe8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions github-actions Bot added review/awaiting-author Reviewed; waiting on the author to respond area/core size/XL labels Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Scope check

This PR changes 1,043 lines across 6 files. Large PRs are harder to review and more likely to be closed without review.

If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs.

See CONTRIBUTING.md for contribution guidelines.

@pkg-pr-new

pkg-pr-new Bot commented Jul 6, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1850

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1850

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1850

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1850

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1850

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1850

emdash

npm i https://pkg.pr.new/emdash@1850

create-emdash

npm i https://pkg.pr.new/create-emdash@1850

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1850

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1850

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1850

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1850

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1850

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1850

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1850

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1850

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1850

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1850

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1850

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1850

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1850

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1850

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1850

commit: 6266fe8

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 6, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-playground 6266fe8 Jul 06 2026, 10:17 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 6, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-do 6266fe8 Jul 06 2026, 10:19 PM

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 6, 2026

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-cache 6266fe8 Jul 06 2026, 10:20 PM

@github-actions github-actions Bot added cla: signed review/needs-rereview Author pushed changes since the last review and removed review/awaiting-author Reviewed; waiting on the author to respond labels Jul 6, 2026
@khoinguyenpham04 khoinguyenpham04 self-assigned this Jul 6, 2026

@emdashbot emdashbot Bot 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 PR hardens the collection-scope media usage repair logic ahead of the operator entry point. The approach is sound: it layers generation-guarded deletes, partial-snapshot preservation, conflict-versus-failure classification, and final-status CAS behavior on top of the existing CAS-based repository, which fits EmDash's architecture well.

I reviewed the full diff and the changed source files:

  • Repository deletes (media-usage.ts): Switching deleteSourceIfCurrent, deleteSourceIfMatching, and deleteSourceIfMatchingContentAbsent to remove only the generation that won the source CAS is correct. Older/unmatched generations become invisible to current-usage queries and are reclaimed by the existing orphan/stale/abandoned cleanup sweepers. This prevents a guarded repair delete from wiping out a fresher concurrent write. The deleteSourceKeys unguarded bulk path is left alone, which is right.
  • Repair conflict handling (content-repair.ts): Treating rows deleted mid-repair as missingContentIds / conflicts rather than source failures is the right semantics. repairedContentIds neatly removes those IDs from the before/after scan comparison, avoiding a spurious additional conflict. Refactoring repairSnapshotSources and the new partial-snapshot failure path correctly preserve a valid columns snapshot even when draft_overlay loading fails. markRepairConflict with ??= keeps more specific error codes from being overwritten by later conflict markers.
  • Snapshot partial failures (content-snapshots.ts): Returning the already-built columns snapshot along with the failing draft source is a clean way to let repair make forward progress.
  • Status logic: The updated determineRepairStatus correctly maps {failed, skipped} × {trusted progress} to complete/partial/failed, matching the new regression tests.
  • Tests: Good coverage of missing drafts, malformed repeater metadata, no-media scopes, concurrent inserts/deletes, stale status races, and generation-preserved deletes. The trigger-based race simulations are dialect-aware.
  • Conventions: SQL uses parameterized sql.ref; no new admin UI strings; no migrations; no API routes. The only conventional gap is the absent changeset, which the author notes is intentional and will be added with Patch 4C when the operator surface ships.

No logic bugs, regressions, or security issues found. The code is well-scoped, well-tested, and consistent with the surrounding media-usage subsystem.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview Author pushed changes since the last review labels Jul 6, 2026
@khoinguyenpham04 khoinguyenpham04 requested a review from ascorbic July 6, 2026 22:27
@khoinguyenpham04 khoinguyenpham04 added this to the 1.0 milestone Jul 7, 2026
@khoinguyenpham04 khoinguyenpham04 merged commit b92807f into main Jul 7, 2026
52 checks passed
@khoinguyenpham04 khoinguyenpham04 deleted the media-usage-repair-hardening-main branch July 7, 2026 09:00
@emdashbot emdashbot Bot mentioned this pull request Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants