From d530df47542b0e85e53f974e8edac69d1c052f61 Mon Sep 17 00:00:00 2001
From: Kris <2040188027@qq.com>
Date: Sat, 28 Nov 2026 12:16:00 +0800
Subject: [PATCH] fix(web): exclude generic template shadows from role
reference counts
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
The role catalog counted nodes from platform generic templates (appended by
CatalogRepo.Workflows) as project role references, so roles in a project with
no SOP showed node counts and could not be deleted — while the backend DELETE
check does not consider generic templates (#1496).
Exclude template shadows from the reference count: a private template copy
only binds in its own project. Real project workflow references still count.
Test covers: generic-only reference shows 'unused' with delete enabled, while
a real same-project workflow reference still counts.
Scope: front-end role catalog count only; no backend/DELETE change.
Coordination: open PR #1248 changes the same count line (to display workflow
names). The changes are complementary — its agentWorkflowUsage helper does not
exclude generic templates. If both land, merge by adding this exclusion inside
agentWorkflowUsage. Part of #1496; does not close the issue.
---
web/app/agents.js | 10 +++++-
webtest/tests/assets-agents.test.mjs | 54 ++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 1 deletion(-)
diff --git a/web/app/agents.js b/web/app/agents.js
index 2a78c75eb..95ecdc8b8 100644
--- a/web/app/agents.js
+++ b/web/app/agents.js
@@ -6,7 +6,15 @@ function renderAgentsLegacy(){
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=new Map(),workflows=state.data.workflows||[],roles=new Map((state.data.agents||[]).map(a=>[a.id,a]));
+ const templateAgentIds=new Set(workflows.filter(w=>w.generic).flatMap(w=>(w.nodes||[]).map(n=>n.agentId?.trim()).filter(Boolean)));
+ workflows.forEach(w=>(w.nodes||[]).forEach(n=>{
+ const id=n.agentId?.trim();if(!id)return;
+ const a=roles.get(id);
+ // Match AgentShadowsTemplateOutside: private template copies only bind in their own project.
+ if(a?.projectId&&!a.shared&&a.projectId!==w.projectId&&templateAgentIds.has(id))return;
+ used.set(id,(used.get(id)||0)+1);
+ }));
$('#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..3d14645cf 100644
--- a/webtest/tests/assets-agents.test.mjs
+++ b/webtest/tests/assets-agents.test.mjs
@@ -207,6 +207,60 @@ test('agent catalog search, presets, validation, persistence, clone, and delete
assert.equal(app.evaluate(`state.data.agents.some(a=>a.id==='agent-copy')`), false);
});
+test('agent usage ignores template shadows and keeps real workflow references', async (t) => {
+ const app = await loadApp();
+ t.after(app.close);
+ app.evaluate(`
+ state.projectFilter='project-1';state.agentScope='project';
+ state.data.agents=[{id:'template-role',projectId:'project-1',name:'Developer'}];
+ state.data.workflows=[
+ {id:'generic-flow',generic:true,nodes:[{agentId:'template-role'}]},
+ {id:'other-flow',projectId:'project-2',nodes:[{agentId:'template-role'}]}
+ ];
+ switchView('agents');renderAgents();
+ `);
+ const usage = () => app.document.querySelector('[data-agent-id="template-role"] .agent-row-uses').textContent;
+ const deleteButton = () => {
+ app.document.querySelector('[data-agent-more="template-role"]').click();
+ return app.document.querySelector('#agentRowMenu [data-agent-act="delete"]');
+ };
+ assert.equal(usage(), '未使用');
+ assert.equal(deleteButton().disabled, false);
+
+ app.evaluate(`
+ state.data.workflows.push({id:'project-flow',projectId:'project-1',nodes:[
+ {agentId:'template-role'},{agentId:'template-role'},{}
+ ]});renderAgents();
+ `);
+ assert.equal(usage(), '2 个节点');
+ assert.equal(deleteButton().disabled, true);
+
+ app.evaluate(`state.data.workflows.pop();renderAgents()`);
+ assert.equal(usage(), '未使用');
+ assert.equal(deleteButton().disabled, false);
+
+ // Shared roles resolve across projects and remain protected from deletion.
+ app.evaluate(`state.data.agents[0].shared=true;renderAgents()`);
+ assert.equal(usage(), '2 个节点');
+ assert.equal(deleteButton().disabled, true);
+
+ // A private role whose ID is not in a template still binds across projects.
+ app.evaluate(`
+ state.data.agents[0].shared=false;
+ state.data.workflows=state.data.workflows.filter(w=>!w.generic);renderAgents();
+ `);
+ assert.equal(usage(), '1 个节点');
+ assert.equal(deleteButton().disabled, true);
+
+ // Platform roles also keep references from generic templates.
+ app.evaluate(`
+ state.data.agents[0].projectId='';state.agentScope='shared';
+ state.data.workflows.push({id:'generic-flow',generic:true,nodes:[{agentId:'template-role'}]});
+ renderAgents();
+ `);
+ assert.equal(usage(), '2 个节点');
+});
+
test('role editor refreshes configured CLIs and preserves an unavailable saved executor', async (t) => {
const app = await loadApp();
t.after(app.close);