diff --git a/internal/repo/generic_store_test.go b/internal/repo/generic_store_test.go
index 3e1dc6ac6..92aa85d83 100644
--- a/internal/repo/generic_store_test.go
+++ b/internal/repo/generic_store_test.go
@@ -177,3 +177,37 @@ func TestPublishGenericWorkflow(t *testing.T) {
t.Fatalf("publish = %+v", published)
}
}
+
+func TestWorkflowNamesUsingAgentSkipsGenericTemplateShadows(t *testing.T) {
+ s := testStoreWithGenericTemplates(t)
+ template, ok := s.GenericWorkflowByID(GenericBackendDeliveryWorkflowID)
+ if !ok || len(template.Nodes) == 0 {
+ t.Fatal("backend template has no node")
+ }
+ roleID := template.Nodes[0].AgentID
+ if _, ok := s.GenericAgentByID(roleID); !ok {
+ t.Fatalf("template role %q missing", roleID)
+ }
+ // Adopting a template clones its roles into a project and often keeps the
+ // original id. Generic templates and other projects' LOOPs run the built-in
+ // role, so they are not references to the project clone.
+ projectRole := domain.AgentDefinition{ID: roleID, Name: "本项目角色", ProjectID: "project-1", ExecutorID: "codebuddy"}
+ if err := s.PutAgent(projectRole); err != nil {
+ t.Fatal(err)
+ }
+ if names := s.WorkflowNamesUsingAgent(projectRole); len(names) != 0 {
+ t.Fatalf("shadowed project role reported as referenced: %v", names)
+ }
+ own := domain.WorkflowDefinition{ID: "project-flow", Name: "本项目交付", ProjectID: "project-1", Status: "published",
+ Nodes: []domain.WorkflowNode{{ID: "work", AgentID: roleID}}}
+ if err := s.PutWorkflow(own); err != nil {
+ t.Fatal(err)
+ }
+ if names := s.WorkflowNamesUsingAgent(projectRole); len(names) != 1 || names[0] != "本项目交付" {
+ t.Fatalf("own-project reference missing: %v", names)
+ }
+ // A platform role with the same id resolves in every LOOP.
+ if names := s.WorkflowNamesUsingAgent(domain.AgentDefinition{ID: roleID, Name: "平台角色"}); len(names) < 2 {
+ t.Fatalf("platform role lost template references: %v", names)
+ }
+}
diff --git a/web/app/agents.js b/web/app/agents.js
index 2a78c75eb..a61e6f6be 100644
--- a/web/app/agents.js
+++ b/web/app/agents.js
@@ -3,10 +3,29 @@ function renderAgentsLegacy(){
list.querySelectorAll('.agent-card').forEach(el=>el.onclick=()=>selectAgent(state.data.agents.find(a=>a.id===el.dataset.id)));
if(!state.selectedAgent&&state.data.agents[0])selectAgent(state.data.agents[0],false);
}
+// Mirror CatalogRepo.AgentShadowsTemplateOutside: a project role that reuses a
+// generic template role id only resolves to LOOPs in its own project. Nodes in
+// the templates (and in other projects) run the built-in role instead, so they
+// must not inflate the usage count or block deletion.
+function agentUsageCounts(workflows,agents){
+ const templateRoleIds=new Set();
+ (workflows||[]).forEach(w=>{if(w.generic)(w.nodes||[]).forEach(n=>{if(n.agentId)templateRoleIds.add(n.agentId)})});
+ const used=new Map();
+ (agents||[]).forEach(a=>{
+ const shadowed=!!a.projectId&&!a.shared&&templateRoleIds.has(a.id);
+ let count=0;
+ (workflows||[]).forEach(w=>{
+ if(shadowed&&w.projectId!==a.projectId)return;
+ (w.nodes||[]).forEach(n=>{if(n.agentId===a.id)count++});
+ });
+ used.set(a.id,count);
+ });
+ return used;
+}
function renderAgents(){
const list=$('#agentList');if(!list)return;renderCatalogScopes();
const query=($('#agentSearch')?.value||'').trim().toLowerCase(),agents=scopedAgents().filter(a=>!query||`${a.name} ${a.id} ${a.description} ${(a.rules||[]).join(' ')} ${(a.skills||[]).join(' ')}`.toLowerCase().includes(query));
- const used=new Map();(state.data.workflows||[]).forEach(w=>(w.nodes||[]).forEach(n=>{if(n.agentId)used.set(n.agentId,(used.get(n.agentId)||0)+1)}));
+ const used=agentUsageCounts(state.data?.workflows,state.data?.agents);
$('#agentCatalogStats').innerHTML=`${agents.length} 个${state.agentScope==='shared'?'共享':'项目'}角色${agents.reduce((n,a)=>n+(a.skills?.length||0),0)} 项技能绑定${agents.reduce((n,a)=>n+(a.mcpServers?.length||0),0)} 个 MCP 连接`;
const empty=state.agentScope==='shared'?'还没有共享给当前项目的角色。':'当前项目还没有角色。可以新建,或从共享角色复制后改。';
closeAgentRowMenu();
diff --git a/webtest/tests/assets-agents.test.mjs b/webtest/tests/assets-agents.test.mjs
index 23480625f..32349ba56 100644
--- a/webtest/tests/assets-agents.test.mjs
+++ b/webtest/tests/assets-agents.test.mjs
@@ -462,6 +462,42 @@ test('adding an artifact keeps unsaved loop and review settings of the role', as
assert.equal(saved.artifactOutputs[0].path, 'plan.md');
});
+test('generic LOOP templates do not count as references to project roles', async (t) => {
+ const app = await loadApp();
+ t.after(app.close);
+ app.evaluate(`
+ state.projectFilter='project-1';
+ state.agentScope='project';
+ state.data.agents=[{id:'agent-1',projectId:'project-1',shared:false,name:'Developer',description:'Implements changes',executorId:'runner-1'}];
+ state.data.workflows=[{id:'generic-backend',name:'Common backend delivery',generic:true,projectId:'',nodes:[
+ {id:'n1',agentId:'agent-1'},{id:'n2',agentId:'agent-1'}]}];
+ switchView('agents');renderAgents();
+ `);
+ assert.match(app.document.querySelector('#agentList .agent-row-uses').textContent, /未使用/);
+ assert.equal(app.evaluate(`agentDeleteBlockReason(state.data.agents[0])`), '');
+
+ app.evaluate(`
+ state.data.workflows.push({id:'flow-1',name:'Delivery Flow',projectId:'project-1',nodes:[{id:'n3',agentId:'agent-1'}]});
+ renderAgents();
+ `);
+ assert.match(app.document.querySelector('#agentList .agent-row-uses').textContent, /1 个节点/);
+ assert.equal(app.evaluate(`agentDeleteBlockReason(state.data.agents[0])`), '角色仍被流程引用,请先解除引用');
+});
+
+test('agent usage matches the server shadow rule for shared roles and foreign projects', async (t) => {
+ const app = await loadApp();
+ t.after(app.close);
+ const count = (agent, workflows) => app.evaluate(`agentUsageCounts(${JSON.stringify(workflows)},[${JSON.stringify(agent)}]).get('agent-1')`);
+ const template = {id:'generic-backend',name:'Common backend delivery',generic:true,projectId:'',nodes:[{agentId:'agent-1'}]};
+ const foreign = {id:'foreign-flow',name:'Other project flow',projectId:'project-2',nodes:[{agentId:'agent-1'}]};
+ const own = {id:'own-flow',name:'Own flow',projectId:'project-1',nodes:[{agentId:'agent-1'}]};
+ const projectRole = {id:'agent-1',projectId:'project-1',shared:false};
+ assert.equal(count(projectRole, [template, foreign, own]), 1, 'a shadowed project role only counts its own LOOPs');
+ assert.equal(count({...projectRole, projectId:''}, [template, foreign, own]), 3, 'platform roles resolve in every LOOP');
+ assert.equal(count({...projectRole, shared:true}, [template, foreign, own]), 3, 'shared roles resolve in every LOOP');
+ assert.equal(count(projectRole, [foreign, own]), 2, 'roles without a template twin still count foreign LOOPs');
+});
+
test('platform roles are read-only for project members and can be copied', async (t) => {
const app = await loadApp();
t.after(app.close);