{"record":{"id":"6f22fba7e1f71ff9","repo":"bytedance/deer-flow","slug":"system-already-initialized","errorCode":"system_already_initialized","errorMessage":"System already initialized","messagePattern":"System already initialized","errorType":"http","errorClass":"HTTPException","httpStatus":409,"severity":"warning","filePath":"backend/app/gateway/routers/auth.py","lineNumber":543,"sourceCode":"    password: str = Field(..., min_length=8)\n    remember_me: bool = True\n\n    _strong_password = field_validator(\"password\")(classmethod(lambda cls, v: _validate_strong_password(v)))\n\n\n@router.post(\"/initialize\", response_model=UserResponse, status_code=status.HTTP_201_CREATED)\nasync def initialize_admin(request: Request, response: Response, body: InitializeAdminRequest):\n    \"\"\"Create the first admin account on initial system setup.\n\n    Only callable when no admin exists. Returns 409 Conflict if an admin\n    already exists.\n\n    On success, the admin account is created with ``needs_setup=False`` and\n    the session cookie is set.\n    \"\"\"\n    admin_count = await get_local_provider().count_admin_users()\n    if admin_count > 0:\n        raise HTTPException(\n            status_code=status.HTTP_409_CONFLICT,\n            detail=AuthErrorResponse(code=AuthErrorCode.SYSTEM_ALREADY_INITIALIZED, message=\"System already initialized\").model_dump(),\n        )\n\n    try:\n        user = await get_local_provider().create_user(email=body.email, password=body.password, system_role=\"admin\", needs_setup=False)\n    except ValueError:\n        admin_count = await get_local_provider().count_admin_users()\n        if admin_count == 0:\n            raise HTTPException(\n                status_code=status.HTTP_400_BAD_REQUEST,\n                detail=AuthErrorResponse(code=AuthErrorCode.EMAIL_ALREADY_EXISTS, message=\"Email already registered\").model_dump(),\n            )\n        raise HTTPException(\n            status_code=status.HTTP_409_CONFLICT,\n            detail=AuthErrorResponse(code=AuthErrorCode.SYSTEM_ALREADY_INITIALIZED, message=\"System already initialized\").model_dump(),\n        )\n","sourceCodeStart":525,"sourceCodeEnd":561,"githubUrl":"https://github.com/bytedance/deer-flow/blob/1dd6ba1acb03700589994b0366c5d1c7d05e2eff/backend/app/gateway/routers/auth.py#L525-L561","documentation":"409 from POST /api/auth/initialize: count_admin_users() returned > 0, so the first-admin bootstrap endpoint refuses to run — the system already has an admin and initialize is a once-only operation. Subsequent admins must be created by an authenticated admin.","triggerScenarios":"Calling /initialize a second time (page reload + resubmit, or scripted setup re-run) after an admin already exists.","commonSituations":"Setup wizard double-submits; operators re-running bootstrap automation against an already-initialized instance; replayed HTTP requests.","solutions":["Log in as the existing admin instead of re-initializing","To start over on a throwaway instance, wipe the user store (delete the DB/users file) so admin count returns to 0","Make setup scripts idempotent: treat 409 SYSTEM_ALREADY_INITIALIZED as success"],"exampleFix":"# before\nresp = post(\"/api/auth/initialize\", json=admin_payload)\nresp.raise_for_status()\n\n# after\nresp = post(\"/api/auth/initialize\", json=admin_payload)\nif resp.status_code == 409:\n    log.info(\"already initialized; skipping bootstrap\")\nelse:\n    resp.raise_for_status()","handlingStrategy":"try-catch","validationCode":"const admins = await countAdmins(); // if exposed\nif (admins > 0) skipBootstrap();","typeGuard":null,"tryCatchPattern":"try { await initializeAdmin(payload); } catch (e) { if (e.status === 409 && e.body?.code === 'system_already_initialized') { log.info('bootstrap already done'); return; } throw e; }","preventionTips":["Treat 409 SYSTEM_ALREADY_INITIALIZED as the idempotent-success signal in bootstrap scripts","Guard setup UIs against double-submit of the initialize form"],"tags":["auth","http-409","bootstrap","admin"],"backgroundTag":null,"analyzedSha":"1dd6ba1acb03700589994b0366c5d1c7d05e2eff","analyzedAt":"2026-08-14T21:20:34.804Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}