From f054012c44526a930e5b87c7a013bda761a0a78b Mon Sep 17 00:00:00 2001 From: rcourtman Date: Sun, 21 Dec 2025 23:13:47 +0000 Subject: [PATCH] fix: include VMID in AI context to prevent incorrect references The LLM was confusing VMIDs because they weren't included in the context. Now the formatted context shows: ### Container: ollama (VMID 200) on minipc This prevents the AI from referencing the wrong VMID when generating findings and recommendations. --- internal/ai/context/builder.go | 2 ++ internal/ai/context/formatter.go | 7 ++++++- internal/ai/context/formatter_test.go | 1 + internal/ai/context/types.go | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/ai/context/builder.go b/internal/ai/context/builder.go index 913cf5f..8549834 100644 --- a/internal/ai/context/builder.go +++ b/internal/ai/context/builder.go @@ -112,6 +112,7 @@ func (b *Builder) BuildForInfrastructure(state models.StateSnapshot) *Infrastruc trends := b.computeGuestTrends(vm.ID) resourceCtx := FormatGuestForContext( vm.ID, vm.Name, vm.Node, "vm", vm.Status, + vm.VMID, vm.CPU, vm.Memory.Usage, vm.Disk.Usage, vm.Uptime, vm.LastBackup, trends, ) @@ -137,6 +138,7 @@ func (b *Builder) BuildForInfrastructure(state models.StateSnapshot) *Infrastruc resourceCtx := FormatGuestForContext( ct.ID, ct.Name, ct.Node, containerType, ct.Status, + ct.VMID, ct.CPU, ct.Memory.Usage, ct.Disk.Usage, ct.Uptime, ct.LastBackup, trends, ) diff --git a/internal/ai/context/formatter.go b/internal/ai/context/formatter.go index 18d189e..4e91cd4 100644 --- a/internal/ai/context/formatter.go +++ b/internal/ai/context/formatter.go @@ -15,8 +15,11 @@ func FormatResourceContext(ctx ResourceContext) string { // Header with resource identity typeLabel := formatResourceType(ctx.ResourceType) sb.WriteString(fmt.Sprintf("### %s: %s", typeLabel, ctx.ResourceName)) + if ctx.VMID > 0 { + sb.WriteString(fmt.Sprintf(" (VMID %d)", ctx.VMID)) + } if ctx.Node != "" && ctx.ResourceType != "node" { - sb.WriteString(fmt.Sprintf(" (on %s)", ctx.Node)) + sb.WriteString(fmt.Sprintf(" on %s", ctx.Node)) } sb.WriteString("\n") @@ -471,6 +474,7 @@ func FormatNodeForContext(node models.Node, trends map[string]Trend) ResourceCon // Note: cpu is 0-1 ratio from Proxmox API, memUsage and diskUsage are already 0-100 percentages func FormatGuestForContext( id, name, node, guestType, status string, + vmid int, cpu, memUsage, diskUsage float64, uptime int64, lastBackup time.Time, @@ -481,6 +485,7 @@ func FormatGuestForContext( ResourceType: guestType, ResourceName: name, Node: node, + VMID: vmid, CurrentCPU: cpu * 100, // Convert from 0-1 to percentage CurrentMemory: memUsage, // Already 0-100 percentage from Memory.Usage CurrentDisk: diskUsage, // Already 0-100 percentage from Disk.Usage diff --git a/internal/ai/context/formatter_test.go b/internal/ai/context/formatter_test.go index 67d35fd..2dc105a 100644 --- a/internal/ai/context/formatter_test.go +++ b/internal/ai/context/formatter_test.go @@ -385,6 +385,7 @@ func TestFormatGuestForContext(t *testing.T) { "pve-1", "vm", "running", + 100, // VMID 0.35, // CPU (0-1) 65.0, // Memory (0-100) 45.0, // Disk (0-100) diff --git a/internal/ai/context/types.go b/internal/ai/context/types.go index 36ceef1..d49e5b2 100644 --- a/internal/ai/context/types.go +++ b/internal/ai/context/types.go @@ -116,6 +116,7 @@ type ResourceContext struct { ResourceType string // "node", "vm", "container", "oci_container", "storage", "docker_host" ResourceName string Node string // Parent node (for guests) + VMID int // Proxmox VMID for VMs/containers (0 if not applicable) // Current state (point-in-time) CurrentCPU float64