{"record":{"id":"0987731cd2c49c86","repo":"iflytek/astron-agent","slug":"model-delete-failed-apply-agent","errorCode":"MODEL_DELETE_FAILED_APPLY_AGENT","errorMessage":"BusinessException(ResponseEnum.MODEL_DELETE_FAILED_APPLY_AGENT)","messagePattern":"BusinessException\\(ResponseEnum\\.MODEL_DELETE_FAILED_APPLY_AGENT\\)","errorType":"error_code","errorClass":"BusinessException","httpStatus":null,"severity":"error","filePath":"console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/service/model/ModelService.java","lineNumber":1054,"sourceCode":"    }\n\n    @Transactional(rollbackFor = Exception.class)\n    public ApiResult checkAndDelete(Long modelId, HttpServletRequest request) {\n        String uid = UserInfoManagerHandler.getUserId();\n        Model model = this.getById(modelId);\n        if (model == null) {\n            throw new BusinessException(ResponseEnum.MODEL_NOT_EXIST);\n        }\n        if (!model.getUid().equals(uid)) {\n            log.warn(\"Unauthorized deletion, uid={}, modelId={}\", uid, modelId);\n            throw new BusinessException(ResponseEnum.EXCEED_AUTHORITY);\n        }\n\n        checkWorkflowReference(uid, model);\n\n        Integer modelCount = sparkBotMapper.checkDomainIsUsage(uid, model.getDomain());\n        if (modelCount != null && modelCount > 0) {\n            throw new BusinessException(ResponseEnum.MODEL_DELETE_FAILED_APPLY_AGENT);\n        }\n\n        boolean result;\n        if (Objects.equals(model.getType(), 1)) {\n            result = this.removeById(modelId);\n        } else {\n            result = this.removeById(modelId) && modelHandler.deleteModel(model.getRemark());\n        }\n        return ApiResult.success(result);\n    }\n\n    /**\n     * Check if model is used by workflow applications\n     *\n     * @param uid\n     * @param model\n     */\n    private void checkWorkflowReference(String uid, Model model) {","sourceCodeStart":1036,"sourceCodeEnd":1072,"githubUrl":"https://github.com/iflytek/astron-agent/blob/5e758547a83371a5a4b29dadf4ac03e8dd527635/console/backend/toolkit/src/main/java/com/iflytek/astron/console/toolkit/service/model/ModelService.java#L1036-L1072","documentation":"Thrown by checkAndDelete when the model is still referenced by agent applications: sparkBotMapper.checkDomainIsUsage(uid, model.getDomain()) returns a positive count. The platform refuses to delete a model whose domain/serviceId is bound to at least one Spark bot/agent, preventing broken agents.","triggerScenarios":"Deleting a model whose domain is used as the LLM domain by one or more agents belonging to the same uid; typically a widely-used default model or one bound to a production agent.","commonSituations":"Cleaning up old fine-tuned models still attached to live agents; shared default domain reused across many bots; forgetting to re-point agents before deletion.","solutions":["Find and update the agents using this model's domain, re-pointing them to another model, then retry deletion.","Temporarily unbind/disconnect the agents from the model domain.","Choose a different domain/serviceId if the model entry is being replaced rather than removed.","If the reference is stale, verify checkDomainIsUsage results and clean up orphaned agent bindings."],"exampleFix":"// before\ndeleteModel(modelId); // throws MODEL_DELETE_FAILED_APPLY_AGENT\n// after\nconst agents = await listAgentsUsingDomain(model.domain);\nawait Promise.all(agents.map(a => updateAgentModel(a.id, replacementModelId)));\ndeleteModel(modelId);","handlingStrategy":"try-catch","validationCode":"Integer usage = sparkBotMapper.checkDomainIsUsage(uid, model.getDomain());\nboolean safeToDelete = model != null && (usage == null || usage == 0);","typeGuard":"if (modelCount != null && modelCount > 0) { /* blocked: model in use by agents */ }","tryCatchPattern":"try { modelService.checkAndDelete(modelId, request); }\ncatch (BusinessException e) {\n  if (ResponseEnum.MODEL_DELETE_FAILED_APPLY_AGENT.equals(e.getResponseEnum())) {\n    promptUserToRebindAgents(model.getDomain());\n    return;\n  }\n  throw e;\n}","preventionTips":["Check agent bindings for the model domain before offering delete","Provide a re-point/rebind workflow in the UI","Audit shared default domains that many agents reference"],"tags":["java","delete","reference-conflict","agent"],"backgroundTag":"invalid-state-transition","analyzedSha":"5e758547a83371a5a4b29dadf4ac03e8dd527635","analyzedAt":"2026-09-12T08:03:51.356Z","contentChangedAt":"2026-09-12T08:03:51.356Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}