From 50401660ef75ee868ff744666acf2f51e9f29e25 Mon Sep 17 00:00:00 2001 From: hailin Date: Sun, 8 Mar 2026 14:49:00 -0700 Subject: [PATCH] fix(dingtalk): exclude removed instances from routing + clear binding on remove Two bugs fixed: 1. findByDingTalkUserId now filters status != 'removed' so a re-bound new instance is not shadowed by an old removed one with the same DingTalk user ID. 2. When an agent is deleted (removed), its dingtalkUserId is cleared so the DingTalk ID is freed for reuse by the next binding. Co-Authored-By: Claude Sonnet 4.6 --- .../infrastructure/repositories/agent-instance.repository.ts | 5 +++-- .../interfaces/rest/controllers/agent-instance.controller.ts | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/services/agent-service/src/infrastructure/repositories/agent-instance.repository.ts b/packages/services/agent-service/src/infrastructure/repositories/agent-instance.repository.ts index 599b0d0..31dcd57 100644 --- a/packages/services/agent-service/src/infrastructure/repositories/agent-instance.repository.ts +++ b/packages/services/agent-service/src/infrastructure/repositories/agent-instance.repository.ts @@ -1,5 +1,5 @@ import { Injectable } from '@nestjs/common'; -import { DataSource, Repository } from 'typeorm'; +import { DataSource, Not, Repository } from 'typeorm'; import { AgentInstance } from '../../domain/entities/agent-instance.entity'; @Injectable() @@ -27,7 +27,8 @@ export class AgentInstanceRepository { } findByDingTalkUserId(dingTalkUserId: string): Promise { - return this.repo.findOne({ where: { dingTalkUserId } as any }); + // Exclude removed instances so a rebound new instance is routed correctly + return this.repo.findOne({ where: { dingTalkUserId, status: Not('removed') } as any }); } save(instance: AgentInstance): Promise { diff --git a/packages/services/agent-service/src/interfaces/rest/controllers/agent-instance.controller.ts b/packages/services/agent-service/src/interfaces/rest/controllers/agent-instance.controller.ts index e185cfe..3f2a005 100644 --- a/packages/services/agent-service/src/interfaces/rest/controllers/agent-instance.controller.ts +++ b/packages/services/agent-service/src/interfaces/rest/controllers/agent-instance.controller.ts @@ -146,6 +146,7 @@ export class AgentInstanceController { if (!inst) throw new NotFoundException(`Instance ${id} not found`); await this.deployService.removeInstance(inst); inst.status = 'removed'; + inst.dingTalkUserId = null as any; // Clear DingTalk binding so the ID can be reused by a new instance await this.instanceRepo.save(inst); return { message: 'Instance removed', id }; }