From e2f82f8b174e0431e54ab4f731fa9ffee5762ec7 Mon Sep 17 00:00:00 2001 From: rcourtman Date: Tue, 9 Jun 2026 10:58:38 +0100 Subject: [PATCH] Add discovery freshness to the local/full context formatter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit formatSingleDiscovery (the local/full path, FormatForAIContext) reported a resource's service, access, paths, and ports but not how old the discovery was — so Ollama/local users lacked the recency signal cloud users just got. Emit a "Last Discovered: " header fact when the timestamp is known, completing freshness parity across the cloud-safe and full paths. Omitted when unknown. Test: TestFormatSingleDiscovery_IncludesFreshness (present when set, absent when zero). --- internal/servicediscovery/formatters.go | 6 ++++++ internal/servicediscovery/formatters_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/internal/servicediscovery/formatters.go b/internal/servicediscovery/formatters.go index 5cb23df92..be4c4f134 100644 --- a/internal/servicediscovery/formatters.go +++ b/internal/servicediscovery/formatters.go @@ -118,6 +118,12 @@ func formatSingleDiscovery(d *ResourceDiscovery) string { sb.WriteString(fmt.Sprintf("- **Type:** %s\n", d.ResourceType)) sb.WriteString(fmt.Sprintf("- **Target:** %s\n", firstNonEmpty(d.Hostname, d.TargetID))) + // Freshness so the model (local/full path) can caveat staleness rather than + // presenting a cached scan as current — parity with the cloud-safe path. + if !d.UpdatedAt.IsZero() { + sb.WriteString(fmt.Sprintf("- **Last Discovered:** %s\n", FormatDiscoveryAge(d))) + } + if d.ServiceVersion != "" { sb.WriteString(fmt.Sprintf("- **Version:** %s\n", d.ServiceVersion)) } diff --git a/internal/servicediscovery/formatters_test.go b/internal/servicediscovery/formatters_test.go index d8c0ae9c2..35b421e4d 100644 --- a/internal/servicediscovery/formatters_test.go +++ b/internal/servicediscovery/formatters_test.go @@ -329,3 +329,21 @@ func TestFormatCloudSafeContext(t *testing.T) { t.Errorf("cloud-safe context must report discovery age\n--- output ---\n%s", freshOut) } } + +func TestFormatSingleDiscovery_IncludesFreshness(t *testing.T) { + d := &ResourceDiscovery{ + ID: MakeResourceID(ResourceTypeSystemContainer, "node1", "101"), + ResourceType: ResourceTypeSystemContainer, + ServiceName: "Home Assistant", + } + // No timestamp -> no freshness line (parity with the cloud-safe path). + if strings.Contains(formatSingleDiscovery(d), "Last Discovered:") { + t.Fatalf("expected no freshness line when UpdatedAt is zero") + } + // Known timestamp -> the local/full path reports discovery age. + d.UpdatedAt = time.Now().Add(-3 * time.Hour) + out := formatSingleDiscovery(d) + if !strings.Contains(out, "**Last Discovered:** 3 hours ago") { + t.Fatalf("expected freshness line in single-discovery output, got:\n%s", out) + } +}