TencentCloud/TencentDB-Agent-Memory · error · MetadataError

already_initialized

already_initialized

Error message

system already has users; init-admin requires empty database

What it means

MetadataError code 'already_initialized' from initAdminUser means the database is not empty, so the one-time bootstrap of the initial admin cannot proceed. init-admin is designed to run exactly once, on an empty user table. This protects against overwriting or racing bootstrap flows.

Source

Thrown at MemoryCore/src/metadata/service/metadata-service.ts:380

  private async assertTeamQuota(): Promise<void> {
    const count = await this.store.countTeams();
    const limit = this._configParams
      ? await this._configParams.getEffectiveInt("quota", "max_teams_per_instance")
      : this.quota.maxTeamsPerInstance;
    if (count >= limit) {
      throw new MetadataError(
        "team_limit_exceeded",
        `team limit ${limit} reached for instance ${this.instanceId} (current: ${count})`,
      );
    }
  }

  // ============================================================
  // User(含 user_key 生成/刷新)
  // ============================================================
  async initAdminUser(input: InitAdminInput): Promise<InitAdminResult> {
    if ((await this.store.countUsers()) > 0) {
      throw new MetadataError("already_initialized", "system already has users; init-admin requires empty database");
    }
    if ((await this.store.countSystemAdmins()) > 0) {
      throw new MetadataError("already_initialized", "system_admin already exists");
    }

    // 核心操作:创建 admin 用户
    const created = await this.createUserWithType(
      { username: input.username, default_key_value: input.user_key },
      "system_admin",
    );

    // 辅助操作:自动创建默认 Team 和 Agent(失败不阻塞核心流程)
    try {
      const team = await this.createTeam({
        name: DEFAULT_TEAM_NAME,
        description: DEFAULT_TEAM_DESCRIPTION,
        owner_user_id: created.user_id,
      });

View on GitHub (pinned to 3efcd317b8)

Solutions

  1. Skip initAdminUser if users already exist — check countUsers() first or treat this error as 'already done'
  2. Run init-admin against a clean/empty database (fresh volume or wiped metadata store)
  3. If re-bootstrap is truly required, clear all users first, then call initAdminUser

Example fix

// before
await service.initAdminUser({ username: 'admin', user_key: key }); // throws already_initialized
// after
if ((await service.getUserById('admin')) === null) {
  await service.initAdminUser({ username: 'admin', user_key: key });
}
Defensive patterns

Strategy: try-catch

Validate before calling

const existingUsers = await store.countUsers();
if (existingUsers > 0) return; // already bootstrapped, skip init-admin

Try / catch

try {
  await svc.initAdminUser(input);
} catch (e) {
  if (e instanceof MetadataError && e.code === 'already_initialized') {
    // normal on re-run; treat as success
  } else throw e;
}

Prevention

When it happens

Trigger: Calling initAdminUser when store.countUsers() > 0 — i.e. any user (not just an admin) already exists.

Common situations: Re-running an init-admin migration/startup script against an already-bootstrapped database; pointing a fresh init script at a shared/prod database instead of a clean one; leftover test users in a dev DB.


AI-assisted analysis of TencentCloud/TencentDB-Agent-Memory@3efcd317b8 (2026-09-01). Data as JSON: /api/errors/3c6b7dce447a371e. Report an issue: GitHub.