Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(ai): deduplicate Docker host 3-way chain in mention picker (#1252)

rcourtman5457b0460843
diff --git a/frontend-modern/src/components/AI/Chat/__tests__/mentionResources.test.ts b/frontend-modern/src/components/AI/Chat/__tests__/mentionResources.test.ts
index 42ab4bfcd1..0da2b56217 100644
--- a/frontend-modern/src/components/AI/Chat/__tests__/mentionResources.test.ts
+++ b/frontend-modern/src/components/AI/Chat/__tests__/mentionResources.test.ts
@@ -216,6 +216,57 @@ describe('mentionResources', () => {
expect(ctOrHost[0].name).toBe('my-ct');
});
+ it('deduplicates VM + DockerHost + Host agent 3-way chain into a single entry (#1252)', () => {
+ // Production scenario: a VM runs a host agent with Docker.
+ // Three entities share overlapping aliases forming a chain:
+ // VM (vm-backend-id:pve01-100) <-- Host agent (linkedVmId + id) --> DockerHost (agentId)
+ // Before the union-merge fix, only the first alias match was merged,
+ // leaving the other as a duplicate entry.
+ const state = {
+ nodes: [],
+ vms: [
+ {
+ id: 'pve01-100',
+ vmid: 100,
+ name: 'docker-vm',
+ node: 'pve01',
+ instance: 'pve-instance-1',
+ status: 'running',
+ },
+ ],
+ containers: [],
+ dockerHosts: [
+ {
+ id: 'docker-host-abc',
+ agentId: 'agent-in-vm',
+ hostname: 'docker-vm',
+ displayName: 'docker-vm',
+ status: 'online',
+ lastSeen: Date.now(),
+ containers: [],
+ },
+ ],
+ hosts: [
+ {
+ id: 'agent-in-vm',
+ hostname: 'docker-vm',
+ displayName: 'docker-vm',
+ status: 'online',
+ lastSeen: Date.now(),
+ linkedVmId: 'pve01-100',
+ memory: { total: 8, used: 4, free: 4, usage: 50 },
+ },
+ ],
+ } as unknown as State;
+
+ const resources = buildMentionResources(state);
+ const relevant = resources.filter(
+ (r) => r.type === 'vm' || r.type === 'host',
+ );
+ expect(relevant).toHaveLength(1);
+ expect(relevant[0].name).toBe('docker-vm');
+ });
+
it('deduplicates cluster node mentions from multiple instances and keeps the healthiest status', () => {
const state = {
nodes: [
diff --git a/frontend-modern/src/components/AI/Chat/mentionResources.ts b/frontend-modern/src/components/AI/Chat/mentionResources.ts
index f1bfd7dbbb..c7cf8ad872 100644
--- a/frontend-modern/src/components/AI/Chat/mentionResources.ts
+++ b/frontend-modern/src/components/AI/Chat/mentionResources.ts
@@ -73,37 +73,64 @@ function upsertMentionResource(
aliases: string[] = [],
): void {
const normalizedPrimary = normalizeKeyPart(resource.id);
- let key = normalizedPrimary ? `id:${normalizedPrimary}` : '';
+ const primaryAlias = normalizedPrimary ? `id:${normalizedPrimary}` : '';
- for (const rawAlias of aliases) {
+ // Collect ALL existing keys that any alias points to (union-find style).
+ // This fixes the 3-way chain bug where a host agent bridges a VM and a
+ // DockerHost but only the first match was merged (#1252).
+ const matchedKeys: string[] = [];
+ let firstUnmatchedAlias = '';
+
+ const allAliases = [primaryAlias, ...aliases];
+ for (const rawAlias of allAliases) {
const alias = normalizeKeyPart(rawAlias);
if (!alias) continue;
const existingKey = aliasToKey.get(alias);
if (existingKey) {
- key = existingKey;
- break;
- }
- if (!key) {
- key = alias;
+ if (!matchedKeys.includes(existingKey)) {
+ matchedKeys.push(existingKey);
+ }
+ } else if (!firstUnmatchedAlias) {
+ firstUnmatchedAlias = alias;
}
}
- if (!key) {
- key = `fallback:${normalizeKeyPart(resource.type)}:${normalizeKeyPart(resource.name)}`;
+ // Pick the canonical key: first matched key, or first unmatched alias, or fallback.
+ const canonicalKey =
+ matchedKeys[0] ||
+ firstUnmatchedAlias ||
+ `fallback:${normalizeKeyPart(resource.type)}:${normalizeKeyPart(resource.name)}`;
+
+ // Merge all other matched keys' resources into the canonical winner.
+ for (let i = 1; i < matchedKeys.length; i++) {
+ const loserKey = matchedKeys[i];
+ const loserResource = byKey.get(loserKey);
+ if (loserResource) {
+ const current = byKey.get(canonicalKey);
+ byKey.set(canonicalKey, current ? preferMentionResource(current, loserResource) : loserResource);
+ byKey.delete(loserKey);
+ }
+ // Re-point all aliases that referenced the loser to the winner.
+ for (const [alias, key] of aliasToKey) {
+ if (key === loserKey) {
+ aliasToKey.set(alias, canonicalKey);
+ }
+ }
}
- const existing = byKey.get(key);
+ // Merge the incoming resource.
+ const existing = byKey.get(canonicalKey);
if (existing) {
- byKey.set(key, preferMentionResource(existing, resource));
+ byKey.set(canonicalKey, preferMentionResource(existing, resource));
} else {
- byKey.set(key, resource);
+ byKey.set(canonicalKey, resource);
}
- const allAliases = [normalizedPrimary ? `id:${normalizedPrimary}` : '', ...aliases];
+ // Register all aliases to point at the canonical key.
for (const rawAlias of allAliases) {
const alias = normalizeKeyPart(rawAlias);
if (!alias) continue;
- aliasToKey.set(alias, key);
+ aliasToKey.set(alias, canonicalKey);
}
}