Skip to content

dkg-cli agent-setup fails when admin user already exists in database #6

@jaksm

Description

@jaksm

The dkg-cli agent-setup command fails when an admin user already exists in the database, preventing successful completion of the agent setup
process.

Steps to Reproduce

  1. Run dkg-cli agent-setup successfully (creating the default admin user)
  2. Attempt to run dkg-cli agent-setup again (e.g., to reconfigure settings)
  3. Setup fails with error related to LLM provider being undefined

Expected Behavior

According to the official documentation, the admin user
(admin@example.com) should be created during setup. The setup script should handle the scenario where an admin user already exists by either:

  • Skipping user creation if the user already exists
  • Providing an option to update existing credentials
  • Showing a clear message that the user exists and continuing with setup

Actual Behavior

The setup script attempts to create an admin user unconditionally. When a user with admin@example.com already exists, the script encounters an
error and fails to complete.

Root Cause

Looking at apps/agent/src/server/helpers.ts:90-98, the createUser function throws an error if a user with the provided email already exists:

await db
  .select()
  .from(users)
  .where(eq(users.email, email))
  .then((r) => {
    if (r.length > 0) {
      throw new Error(`User with email ${email} already exists.`);
    }
  });

The setup script at apps/agent/src/server/scripts/setup.ts:178-186 calls createUser without checking if the admin user already exists.

Suggested Fix

Modify the setup script to check if the admin user exists before attempting creation:

// Check if admin user already exists
const existingAdmin = await db
  .select()
  .from(users)
  .where(eq(users.email, "admin@example.com"))
  .then((r) => r[0]);

if (existingAdmin) {
  console.log("Admin user already exists, skipping creation...");
  console.log(`Email: admin@example.com`);
} else {
  console.log("Creating admin user...");
  const userId = await createUser(
    db,
    {
      email: "admin@example.com",
      password: "admin123",
    },
    ["mcp", "llm", "blob", "scope123"],
  );
  // ... rest of the output
}

Environment

  • OS: macOS (Darwin 24.6.0)
  • Node version: [your version]
  • DKG Node: Latest from main branch

Workaround

Manually delete the database file and .env files before running dkg-cli agent-setup again.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions