Skip to content

feat(admin): transfer_brand_ownership tool for brand registry - #3159

Merged
bokelley merged 2 commits into
mainfrom
claude/issue-3152-transfer-brand-ownership
Apr 25, 2026
Merged

bokelley merged 2 commits into
mainfrom
claude/issue-3152-transfer-brand-ownership

Conversation

@bokelley

@bokelley bokelley commented Apr 25, 2026 •

Copy link
Copy Markdown
Contributor

Refs #3152

Summary

Adds transfer_brand_ownership to Addie's admin tool panel. Admins can now move a brand domain from one WorkOS org to another via a single tool call, with a full-row revision snapshot written to brand_revisions before the ownership update. Eliminates direct database surgery for acquisitions, org renames, and "original uploader left" cases.

What ships in this PR:

  • BrandDatabase.transferBrandOwnership(domain, newOrgId, reason, editor) — SELECT * FOR UPDATE → revision INSERT → UPDATE workos_organization_id → COMMIT
  • transfer_brand_ownership admin tool in ADMIN_TOOLS + handler in createAdminToolHandlers
  • --empty changeset (server-only, no protocol impact)

What is deferred (needs more design — see issue):

  • claim_disputed_brand member tool — experts agree this should be a dispute_type: 'brand_ownership' variant on the existing escalation schema, not a standalone MCP tool or new service
  • Soft-claim model — blocked on two issues: (1) deleteHostedBrand clears workos_organization_id but not brand_manifest, so an incoming org would inherit the prior org's brand data silently; (2) generateVerificationToken is orphaned (no caller) — domain verification is currently pointer-only and can't safely gate a takeover

Non-breaking justification

Adds a new admin tool; no public schema or API surface changed. Existing callers unaffected.

Pre-PR review

  • code-reviewer: approved — blocker fixed (changed SELECT id, workos_organization_id → SELECT * so revision snapshot captures full row; also added editor.name passthrough and normalized domain.toLowerCase() to one call)
  • internal-tools-strategist: approved — tool description is clear, old_org_id in response is warranted, reason min-length bumped to 20 chars

Nits (not fixed, noted for follow-up):

  • Optional escalation_id input field would let Addie pass the ticket ref directly into the revision edit_summary rather than holding it across two tool calls
  • reason.length < 20 is still permissive; could tighten further if abuse observed in practice

Session: https://claude.ai/code/session_01XmWfbQQkuuX3DB8VpukZsu

Adds an admin-only Addie tool that moves a brand domain from one
WorkOS org to another, writing a full-row revision snapshot before
updating workos_organization_id. Eliminates direct database surgery
for acquisitions, org renames, and "original uploader left" cases.

Refs #3152

https://claude.ai/code/session_01XmWfbQQkuuX3DB8VpukZsu
Security/code review on #3159 surfaced four real items, all addressed:

* Tool added to TOOL_SETS.admin.tools so the router actually surfaces
  it to Sonnet. Without this, the handler was registered but functionally
  unreachable through routed paths — same bug class as #2998.
* new_org_id validated against `^org_[A-Z0-9]{20,}$` so a typo can't
  silently orphan a brand under a non-existent org id (no FK to catch it
  — workos_organization_id is a WorkOS string, not a local row).
* reason capped at 1000 chars after the floor check so a prompt-injected
  long reason can't bloat the brand_revisions audit trail to MB.
* Existence check on new_org_id against organizations table inside the
  transaction; rejects unowned brands (claim path, not transfer) and
  rejects same-org transfers; canonicalization moved into the DB method
  so future callers can't pass raw `Foo.COM` and miss the row.

Return signature tightened: oldOrgId is now `string` (non-null) since
unowned brands are rejected upstream.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley marked this pull request as ready for review April 25, 2026 16:20
@bokelley
bokelley merged commit 621eb49 into main Apr 25, 2026
14 checks passed
@bokelley
bokelley deleted the claude/issue-3152-transfer-brand-ownership branch April 25, 2026 16:20
bokelley added a commit that referenced this pull request Apr 25, 2026
…n relinquish (#3168)

* fix(brand-registry): file escalation on cross-org dispute + clear manifest on relinquish

Two follow-ons to the transfer-tool work in #3159:

- Brand-ownership disputes route through escalations now. The bare 403
  from updateBrandIdentity becomes a category=needs_human_action ticket
  with the caller org, owner org, and brand domain in addie_context.
  Web route returns 409+escalation_id; chat tool replies "we filed it"
  with the ticket number. Admins resolve via the existing
  transfer_brand_ownership tool.

- Relinquishing a hosted brand now clears brand_manifest. Before, only
  ownership fields were cleared, so a later soft-claim silently
  inherited the prior org's logos, colors, agents, and narrative copy.
  The row resets to a clean discovered state.

BrandIdentityError gained a `code` discriminator and `meta` payload so
callers can tell cross_org_ownership apart from validation errors.

Closes the second part of #3152. Soft-claim with verified-domain
takeover is still parked — pointer-only domain verification isn't
strong enough to gate provenance changes without a real
challenge/response.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Address review feedback on dispute escalation flow

Three small items from code-reviewer on #3168, all addressed:

* Chat false-promise on escalation failure. The previous code told the
  user "they'll resolve it and follow up" even when createEscalation
  rejected — a confidently false promise. Now branches on the null
  return: ticket-id message on success, "please email support" on
  silent failure. Web side now also flips its message text based on
  whether the escalation actually filed.
* Tightened BrandIdentityError meta typing. Switched from
  Record<string, unknown> to a per-code interface (BrandIdentityErrorMetaByCode).
  Added an isCrossOrgOwnership() type guard so catch sites narrow both
  the discriminator and the meta payload — err.meta.brandDomain is now
  typed as string instead of unknown. Catches typos at compile time.
* Caller email + display name added to addie_context so admins reading
  the escalation know who's asking without joining against a user table.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Flag-for-adoption + sensitive_topic categorization on dispute escalations

Replaces the hard-reset behavior in #3168's first commit with the
flag-for-adoption design adtech-product-expert recommended in review.

* Migration 430 adds manifest_orphaned + prior_owner_org_id columns to
  brands.
* deleteHostedBrand now sets manifest_orphaned=true and stashes the
  prior owner instead of clearing the brand_manifest. Paired with
  is_public=false so public reads still don't surface the orphaned
  identity, and resolveBrand in member-profiles.ts skips orphaned rows.
* updateBrandIdentity gains an adoptPriorManifest flag. When the
  claimed brand is orphaned: default = clear and start fresh (safe
  against silent inheritance); opt-in = keep prior manifest and merge
  new claimant's logo/color over it (acquisition/handoff case). Either
  branch clears the orphan flag and reasserts is_public=true. Plumbed
  through to the PUT /brand-identity body as adopt_prior_manifest and
  surfaced in the response as adopted_prior_manifest.

Escalation framing changes per adtech-product-expert review:
* category switched from needs_human_action to sensitive_topic — these
  are governance/legal decisions, not routine triage.
* Summary and addie_context rephrased to not presume bad faith. The
  dispute could equally be an acquisition, naming overlap, or someone
  backfilling on behalf of the registered org.

Self-service "publish brand.json to auto-resolve" hint deferred until
that path actually exists — filed as #3176.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

* Address review feedback: close orphan-leak vectors, force explicit decision

Security review surfaced two H findings: the verify-by-pointer route
bypassed the orphan logic (claimed-but-orphan = new owner with prior
manifest), and two unauthenticated registry-resolve routes leaked the
orphaned manifest before any claim. Code review surfaced a misleading
result field. Adtech-product review pushed for explicit-decision-required
on claim time.

Changes:

* claimOrphanedHostedBrand new DB method that atomically asserts
  ownership, marks verified, clears the orphan flag, and (default)
  resets the prior manifest. Verify-by-pointer route now uses this when
  hosted.manifest_orphaned is true. Without this, an org publishing a
  brand.json pointer at a relinquished domain became the verified owner
  with the prior org's logos still attached.

* Public read paths now filter orphaned + non-public:
  - /api/brands/resolve and /api/brands/resolve/bulk
  - /api/me/community-profile/:slug brand resolution in http.ts
  - /api/referral/:code referrer logo lookup

* HOSTED_BRAND_COLUMNS includes manifest_orphaned + prior_owner_org_id
  so every getHostedBrandByDomain caller can branch on orphan state.
  HostedBrand interface gained the matching optional fields.

* brand-feeds getBrandForEdit rejects orphaned brands so feed/property
  edits can't write into the prior owner's manifest fields during the
  pre-claim window.

* updateBrandIdentity now requires the orphan-adoption decision to be
  explicit. When manifest_orphaned and adoptPriorManifest is undefined,
  throws BrandIdentityError(409, 'orphan_manifest_decision_required')
  with the prior owner's org id in meta. Web route returns 409 with
  prior_owner_org_id in the body; chat tool replies with the choice
  question and prompts the user to re-run with adopt_prior_manifest
  set explicitly.

* Result fields renamed: adoptedOrphanedManifest (which fired even on
  clear) split into claimedOrphanedBrand + keptPriorManifest. Web
  response now exposes claimed_orphaned_brand + kept_prior_manifest.

* Cross-org dispute escalation summary now names the incumbent org
  too — "Brand ownership review: {domain} — claim by {claimant} vs
  current owner {incumbent}" — so admins triaging the queue see both
  parties at a glance.

* update_company_logo tool gains an adopt_prior_manifest input so
  members can opt in via chat after the orphan-decision question.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants