Skip to content

feat(core): add media usage repair tooling#2007

Merged
khoinguyenpham04 merged 5 commits into
mainfrom
feat/media-usage-repair-cli
Jul 13, 2026
Merged

feat(core): add media usage repair tooling#2007
khoinguyenpham04 merged 5 commits into
mainfrom
feat/media-usage-repair-cli

Conversation

@khoinguyenpham04

@khoinguyenpham04 khoinguyenpham04 commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

What does this PR do?

Adds operator-facing media usage index repair through the CLI and MCP server. Both interfaces support repairing one collection or every collection, reuse the existing repair contracts, preserve structured complete/partial/failed/stale results, and require the appropriate administrative authorization.

Includes CLI and MCP contract coverage, operator documentation, and integration-test setup that builds the workspace once per test run.

In this PR, the operators use this CLI/MCP repair tooling after imports or direct database writes; it makes the media usage index maintainable, with read APIs and the Media Library “Used in” UI still to come.

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

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: OpenCode (GPT-5.6 Sol)

Screenshots / test output

No visual changes.

  • pnpm typecheck
  • pnpm lint
  • Core suite: 4,719 passed, 3 skipped
  • CLI repair contract: 14 passed
  • MCP repair contract: 14 passed
  • MCP error/authorization suite: 14 passed

Copilot AI review requested due to automatic review settings July 13, 2026 08:56
@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0f84101

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

This PR includes changesets to release 16 packages
Name Type
emdash Minor
@emdash-cms/cloudflare Minor
@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 Minor
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
create-emdash Minor
@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

Copy link
Copy Markdown
Contributor

Scope check

This PR changes 783 lines across 14 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.

@cloudflare-workers-and-pages

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
docs 0f84101 Jul 13 2026, 08:58 AM

@cloudflare-workers-and-pages

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 0f84101 Jul 13 2026, 08:59 AM

@cloudflare-workers-and-pages

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 0f84101 Jul 13 2026, 08:59 AM

@pkg-pr-new

pkg-pr-new Bot commented Jul 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: 0f84101

@cloudflare-workers-and-pages

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 0f84101 Jul 13 2026, 09:00 AM

@khoinguyenpham04 khoinguyenpham04 added the bot:review Trigger an emdashbot code review on this PR label Jul 13, 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 is the right change for an approved Discussion: it surfaces the existing media-usage repair handler through the CLI and MCP server, reuses the complete/partial/failed/stale contracts, and ships contract tests plus operator docs. The implementation is clean and the test coverage is good.

I read the diff, the full changed files, the backing handler/route, and the client/MCP wiring. The main concern is authorization consistency: the new CLI docs and the MCP tool both say the repair requires an admin token scope, but the existing REST route they rely on (POST /_emdash/api/admin/media-usage/repair) only checks schema:manage and ignores token scopes. That means an Admin-owned token without the admin scope can still trigger a potentially expensive, write-heavy repair. The route should either be updated to enforce requireScope(locals, "admin") or the docs/MCP contract should be softened.

A couple of smaller suggestions: the integration global setup always runs pnpm build even when artifacts are fresh, and the MCP tool redeclares an input schema that already exists in #api/schemas.js.

No blocking code defects in the changed lines themselves, so the verdict is comment.

Comment thread docs/src/content/docs/reference/cli.mdx

@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 well-scoped operator-facing media usage repair tooling: a media repair-usage CLI command and a media_usage_repair MCP tool, both reusing the existing repair contracts and preserving the structured complete/partial/failed/stale status shapes. It links to the approved Discussion, includes docs and a changeset, and moves the integration/smoke build step into a Vitest globalSetup so the workspace is built once per test run. The approach is sound and fits EmDash’s architecture.

I traced the CLI/MCP wiring against the REST route (/_emdash/api/admin/media-usage/repair) and its schema:manage permission check, and against the auth middleware scope rules (["/_emdash/api/admin", "*", "admin"]). The MCP tool’s admin scope + Role.ADMIN requirement is effectively equivalent for token auth, and session auth is covered by the role check, so the two entry points are consistent in practice.

The one real gap is test wiring: the new MCP contract tests live under tests/integration/mcp/ but vitest.integration.config.ts only includes tests/integration/cli/** and tests/integration/client/**, so those tests will not run in CI’s integration job. They are also not excluded from the main vitest.config.ts, so they may run (and pass) as part of the unit suite instead, which is semantically wrong and duplicates work. The fix is to add the MCP directory to the integration config include and exclude it from the main unit config.

Otherwise the code looks clean: validation, error handling, exit codes, auth checks, and contract coverage are all in order, and I found no SQL, locale-filter, or API-envelope issues introduced by this PR.

Comment thread packages/core/vitest.integration.config.ts

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks solid overall, but look at the bot reviews

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah, I see you have now!

@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 13, 2026
@khoinguyenpham04 khoinguyenpham04 merged commit 6fb52b0 into main Jul 13, 2026
75 checks passed
@khoinguyenpham04 khoinguyenpham04 deleted the feat/media-usage-repair-cli branch July 13, 2026 10:44
@emdashbot emdashbot Bot mentioned this pull request Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ci area/core area/docs bot:review Trigger an emdashbot code review on this PR review/approved Approved; no new commits since size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants