2026 03 19 Role System Mongodb Runbook

Documentation for Plans

Role system cleanup — MongoDB runbook

Companion to 2026-03-19-role-system-cleanup-handoff.md.
Purpose: Optional data backfills after deploying schema/code changes. Mongoose does not migrate existing documents automatically.

When to run

Canonical values

Remote monorepo / VM engineering (Vitto)

Suggested mongosh checks

Replace database/collection names with your environment values (often gograb or tenant-specific DBs).

1. Users missing platformRole

// Users with legacy shape only (adjust collection name, e.g. users)
db.users.find(
  { platformRole: { $exists: false } },
  { email: 1, role: 1, roles: 1 }
).limit(20);

Backfill (example): derive from existing roles or role using the same rules as platformRoleFromLegacyRoles in role-derivation.ts, or force users through re-login / profile sync so the API sets platformRole. Avoid blind $set unless you have a verified mapping table.

2. User hierarchy rows — executive and level 0

db.userhierarchies.find({ type: 'executive' }).limit(10);
db.userhierarchies.find({ level: { $lt: 1 } }).limit(10);

New schema allows executive and level min 0 for L0. Existing documents without executive need no change unless you onboard executive-tier rows.

3. AI infrastructure / chat-related userType strings

If you store userType on AI session or infra documents, verify values match the expanded enum in ai-infrastructure.schema.ts (executive, admin, builder, user, guest, plus legacy strings). Find unknown values:

const allowed = [
  'developer', 'space_builder', 'platform_user',
  'executive', 'admin', 'builder', 'user', 'guest',
];
db.ai_sessions.find({ userType: { $nin: allowed } }).limit(20); // rename collection as needed

What not to do

Verification

After backfills, spot-check:

  1. platformRoleFromLegacyRoles(['manager']) → builder (app code, not Mongo).
  2. Executive test user: JWT + User.platformRole = executive, hierarchy level 0 if applicable.
  3. No Vitto/chat errors from unexpected userType in stored AI documents.