Skip to content

Add media usage repair foundation - #1846

Merged
khoinguyenpham04 merged 7 commits into
mainfrom
media-usage-repair-rebuild
Jul 6, 2026
Merged

khoinguyenpham04 merged 7 commits into
mainfrom
media-usage-repair-rebuild

Conversation

@khoinguyenpham04

@khoinguyenpham04 khoinguyenpham04 commented Jul 6, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds the internal media usage repair foundation for future Used In workflows.

This PR adds repository CAS/status primitives, a deterministic collection scanner, and a collection-level repair service foundation that can rebuild content-media usage rows and prove complete coverage for the happy path. It also tightens in-process delete/repair serialization and avoids repeated field-discovery queries during a repair run.

Out of scope for this PR: admin REST/client entry points, CLI, MCP, and admin UI. Those are planned follow-up PRs.

Closes: n/a

Related Discussion: #1503

Type of change

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

Checklist

i18n note: n/a, this PR adds no admin UI or user-facing strings.

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: OpenCode with GPT-5.5; second-opinion review with Claude Opus 4.8

Screenshots / test output

No screenshots; no UI changes.

Verified:

pnpm --filter emdash 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
pnpm typecheck
pnpm format
pnpm lint
pnpm lint:json | jq '.diagnostics | length'

Targeted tests passed: 58 tests.

Final lint-json diagnostic count: 0.


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-rebuild. Updated automatically when the playground redeploys.

Copilot AI review requested due to automatic review settings July 6, 2026 15:00

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.

@changeset-bot

changeset-bot Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5f71cde

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

@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,777 lines across 7 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@1846

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: 5f71cde

@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 5f71cde Jul 06 2026, 03:49 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-playground 5f71cde Jul 06 2026, 03:47 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 5f71cde Jul 06 2026, 03:46 PM

@khoinguyenpham04
khoinguyenpham04 requested a review from ascorbic July 6, 2026 15:07

@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 adds the internal media-usage repair foundation described in Discussion #1503: repository CAS/status primitives, a deterministic collection scanner, and a collection-level repair service. It stays within scope (no admin UI/REST/CLI) and includes tests.

What I checked:

  • SQL safety: dynamic table names are built from validateIdentifier-checked collection slugs and passed through sql.ref(); user-supplied IDs are query parameters. No raw interpolation risk.
  • Concurrency: the in-process collection lock is now shared between repair and delete, and the repository-level CAS guard (sourceMatchExpression) is narrow enough to catch the fields that actually change during concurrent writes (current_generation, source_completeness, updated_at, source metadata, revision_id, and attempt timestamps).
  • Status finalization: beginIndexStatusRepair/finalizeIndexStatusRepairIfRunning use the cursor as a run token and only finalize when status = 'running' and cursor = runToken, which prevents stale runs from clobbering newer ones.
  • Locale/i18n: the scanner intentionally reads every content row (all locales) and each row is loaded by primary key; source keys carry locale, so this is consistent with the row-per-locale model rather than a missing-locale-filter bug.
  • Tests: new integration tests cover happy path, empty collection, deleted rows, concurrent insert detection, orphan cleanup, and missing-collection short-circuit; repository tests cover the new CAS methods and guarded status transitions.
  • Changeset is present and appropriate for an internal foundation change.

I found no correctness bugs or AGENTS.md violations. The code is careful with identifier validation, parameterized queries, and CAS semantics. A few low-risk performance/architecture suggestions are noted below, but none block merge.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/awaiting-author Reviewed; waiting on the author to respond labels Jul 6, 2026
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/approved Approved; no new commits since labels Jul 6, 2026

@ascorbic ascorbic 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.

Thanks!

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Jul 6, 2026
@github-actions github-actions Bot added the review/needs-rereview New commits since the last review label Jul 6, 2026
@github-actions github-actions Bot removed the review/approved Approved; no new commits since label Jul 6, 2026
@khoinguyenpham04
khoinguyenpham04 merged commit 7d16d95 into main Jul 6, 2026
46 checks passed
@khoinguyenpham04
khoinguyenpham04 deleted the media-usage-repair-rebuild branch July 6, 2026 15:53
@emdashbot emdashbot Bot mentioned this pull request Jul 6, 2026
@khoinguyenpham04 khoinguyenpham04 mentioned this pull request Jul 6, 2026
11 of 18 tasks
@khoinguyenpham04 khoinguyenpham04 added this to the 1.0 milestone 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