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.

50.1 KB
Raw Blame
package chat

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/rcourtman/pulse-go-rewrite/internal/agentcapabilities"
	"github.com/rcourtman/pulse-go-rewrite/internal/agentexec"
	"github.com/rcourtman/pulse-go-rewrite/internal/ai/providers"
	"github.com/rcourtman/pulse-go-rewrite/internal/ai/tools"
	"github.com/rcourtman/pulse-go-rewrite/internal/config"
	"github.com/rcourtman/pulse-go-rewrite/internal/models"
	"github.com/rcourtman/pulse-go-rewrite/internal/recovery"
	ur "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
)

type fakeStateProvider struct{}

func (f fakeStateProvider) ReadSnapshot() models.StateSnapshot {
	return models.StateSnapshot{}
}

type fakeAgentServer struct{}

func (f fakeAgentServer) GetConnectedAgents() []agentexec.ConnectedAgent {
	return []agentexec.ConnectedAgent{{AgentID: "agent-1", Hostname: "node-1"}}
}

func (f fakeAgentServer) ExecuteCommand(ctx context.Context, agentID string, cmd agentexec.ExecuteCommandPayload) (*agentexec.CommandResultPayload, error) {
	return &agentexec.CommandResultPayload{Stdout: "ok", ExitCode: 0}, nil
}

type fakeCanonicalReadState struct{}

func (f *fakeCanonicalReadState) VMs() []*ur.VMView                           { return nil }
func (f *fakeCanonicalReadState) Containers() []*ur.ContainerView             { return nil }
func (f *fakeCanonicalReadState) Nodes() []*ur.NodeView                       { return nil }
func (f *fakeCanonicalReadState) Hosts() []*ur.HostView                       { return nil }
func (f *fakeCanonicalReadState) DockerHosts() []*ur.DockerHostView           { return nil }
func (f *fakeCanonicalReadState) DockerContainers() []*ur.DockerContainerView { return nil }
func (f *fakeCanonicalReadState) StoragePools() []*ur.StoragePoolView         { return nil }
func (f *fakeCanonicalReadState) PhysicalDisks() []*ur.PhysicalDiskView       { return nil }
func (f *fakeCanonicalReadState) PBSInstances() []*ur.PBSInstanceView         { return nil }
func (f *fakeCanonicalReadState) PMGInstances() []*ur.PMGInstanceView         { return nil }
func (f *fakeCanonicalReadState) K8sClusters() []*ur.K8sClusterView           { return nil }
func (f *fakeCanonicalReadState) K8sNodes() []*ur.K8sNodeView                 { return nil }
func (f *fakeCanonicalReadState) Pods() []*ur.PodView                         { return nil }
func (f *fakeCanonicalReadState) K8sDeployments() []*ur.K8sDeploymentView     { return nil }
func (f *fakeCanonicalReadState) Workloads() []*ur.WorkloadView               { return nil }
func (f *fakeCanonicalReadState) Infrastructure() []*ur.InfrastructureView    { return nil }

type fakeRecoveryPointsProvider struct {
	points []recovery.RecoveryPoint
}

func (f *fakeRecoveryPointsProvider) ListPoints(_ context.Context, opts recovery.ListPointsOptions) ([]recovery.RecoveryPoint, int, error) {
	filtered := make([]recovery.RecoveryPoint, 0, len(f.points))
	for _, point := range f.points {
		if opts.Provider != "" && point.Provider != opts.Provider {
			continue
		}
		if opts.Kind != "" && point.Kind != opts.Kind {
			continue
		}
		filtered = append(filtered, point)
	}

	total := len(filtered)
	if opts.Limit <= 0 {
		return filtered, total, nil
	}
	page := opts.Page
	if page <= 0 {
		page = 1
	}
	start := (page - 1) * opts.Limit
	if start >= total {
		return []recovery.RecoveryPoint{}, total, nil
	}
	end := start + opts.Limit
	if end > total {
		end = total
	}
	return filtered[start:end], total, nil
}

func toolNameSet(list []providers.Tool) map[string]bool {
	set := make(map[string]bool, len(list))
	for _, tool := range list {
		set[tool.Name] = true
	}
	return set
}

func TestToolsForExecutionMode_InteractiveExposesGovernedTools(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{
		StateProvider: fakeStateProvider{},
		AgentServer:   fakeAgentServer{},
		ReadState:     &fakeCanonicalReadState{},
		ControlLevel:  tools.ControlLevelControlled,
	})

	svc := &Service{
		executor: exec,
		cfg: &config.AIConfig{
			PatrolAnalyzeDocker:  true,
			PatrolAnalyzeStorage: true,
		},
	}

	interactiveTools := svc.toolsForExecutor(nil, false)
	interactiveSet := toolNameSet(interactiveTools)
	if !interactiveSet["pulse_kubernetes"] ||
		!interactiveSet["pulse_storage"] ||
		!interactiveSet["pulse_pmg"] ||
		!interactiveSet["pulse_docker"] {
		t.Fatalf("expected interactive chat to expose all governed tools to the selected model")
	}
	if interactiveSet[pulseQuestionToolName] {
		t.Fatalf("interactive chat must not offer pulse_question: no Pulse client can answer it")
	}
}

func TestService_ExecuteStream_ToolManifestIsModelOwned(t *testing.T) {
	// Tool selection is model-owned: every interactive turn that reaches the
	// selected model carries the same full governed manifest, regardless of
	// prompt wording. These prompts were previously keyword-scoped to
	// text-only or query-only manifests (greetings, exact-reply diagnostics,
	// "before using tools" phrasing, inventory breakdown wording, short
	// lookups) — the model now sees the same tools as diagnostic/action
	// prompts and decides for itself whether to use them.
	tmpDir := t.TempDir()
	store, err := NewSessionStore(tmpDir)
	if err != nil {
		t.Fatalf("failed to create session store: %v", err)
	}

	var toolCounts []int
	provider := &stubServiceProvider{
		streamFn: func(ctx context.Context, req providers.ChatRequest, callback providers.StreamCallback) error {
			toolCounts = append(toolCounts, len(req.Tools))
			callback(providers.StreamEvent{Type: "content", Data: providers.ContentEvent{Text: "ok"}})
			callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{InputTokens: 1, OutputTokens: 1}})
			return nil
		},
	}

	service := NewService(Config{
		AIConfig:      &config.AIConfig{ControlLevel: config.ControlLevelControlled},
		StateProvider: &mockStateProvider{},
		AgentServer:   &mockAgentServer{},
	})
	service.sessions = store
	service.provider = provider
	service.started = true

	prompts := []string{
		"hi",
		"thanks",
		"Reply exactly: PULSE_CHAT_OK",
		"Before using any tools, tell me your plan.",
		"give me a detailed inventory breakdown by node",
		"hows esphome",
		"Alerts count",
		"show me the logs for vm 100",
	}
	for i, prompt := range prompts {
		if err := service.ExecuteStream(context.Background(), ExecuteRequest{
			SessionID: fmt.Sprintf("model-owned-manifest-%d", i),
			Prompt:    prompt,
		}, func(StreamEvent) {}); err != nil {
			t.Fatalf("ExecuteStream(%q) failed: %v", prompt, err)
		}
	}

	if len(toolCounts) != len(prompts) {
		t.Fatalf("toolCounts = %#v, want %d provider calls", toolCounts, len(prompts))
	}
	for i, count := range toolCounts {
		if count == 0 {
			t.Fatalf("prompt %q reached the model with no tools; the manifest must be model-owned", prompts[i])
		}
		if count != toolCounts[0] {
			t.Fatalf("prompt %q got %d tools, want the same governed manifest (%d) for every turn", prompts[i], count, toolCounts[0])
		}
	}
}

func TestAssistantPromptQualifiesForLocalInventoryCount(t *testing.T) {
	// The deterministic count-only inventory shortcut is the single surviving
	// prompt-classified path, and it only answers locally — it never decides
	// what tools the model sees. The gate must stay conservative: any hint of
	// intent beyond a pure count sends the turn to the model.
	tests := []struct {
		prompt string
		want   bool
	}{
		{"how many devices in this", true},
		{"how many vms do i have", true},
		{"container count", true},
		// Qualified counts must reach the model with full tools.
		{"how many vms have errors", false},
		{"how many containers are using high cpu", false},
		// Explicit operator tool intent is the contract escape hatch.
		{"Use read-only tools only. On node delly, count entries in /dev with `ls /dev | wc -l`; answer with the number only.", false},
		// Detail/breakdown prompts are not count-only.
		{"give me a detailed inventory breakdown by node", false},
		// Not inventory counts at all.
		{"hows esphome", false},
		{"hi", false},
		{"Alerts count", false},
	}
	for _, tt := range tests {
		normalized := normalizeAssistantToolRoutingPrompt(tt.prompt)
		if got := assistantPromptQualifiesForLocalInventoryCount(normalized); got != tt.want {
			t.Fatalf("assistantPromptQualifiesForLocalInventoryCount(%q) = %v, want %v", tt.prompt, got, tt.want)
		}
	}
}

func TestToolsForExecutionMode_AutonomousNonPatrolExposesGovernedTools(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{
		StateProvider:      fakeStateProvider{},
		TypedActionPlanner: &gateTestPlanner{},
		AgentServer:        fakeAgentServer{},
		ControlLevel:       tools.ControlLevelControlled,
	})

	svc := &Service{executor: exec}
	toolsList := svc.toolsForExecutor(nil, true)
	set := toolNameSet(toolsList)
	if !set["pulse_storage"] {
		t.Fatalf("expected storage tool to be exposed without prompt keyword routing")
	}
	if !set["pulse_control"] || !set["pulse_docker"] {
		t.Fatalf("expected autonomous non-patrol mode to expose typed planning and read tools")
	}
	if set["pulse_file_edit"] {
		t.Fatal("retired file mutation tool must not be exposed")
	}
	if set[pulseQuestionToolName] {
		t.Fatalf("expected autonomous mode to exclude the interactive clarification tool")
	}
}

func TestBuildSystemPrompt_DoesNotClaimGenericVMControl(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildSystemPrompt()

	if !strings.Contains(prompt, "You are Pulse Assistant, the first-party in-app Pulse Intelligence surface") {
		t.Fatalf("expected system prompt to name the native Assistant surface, got %q", prompt)
	}
	if strings.Contains(prompt, "You are Pulse AI") {
		t.Fatalf("system prompt must not use legacy Pulse AI surface identity, got %q", prompt)
	}
	if strings.Contains(prompt, "Run commands on hosts, containers, and VMs") {
		t.Fatalf("expected system prompt to avoid blanket VM control claim, got %q", prompt)
	}
	if !strings.Contains(prompt, "Not every VM or container supports control") {
		t.Fatalf("expected system prompt to call out read-only resource platforms, got %q", prompt)
	}
	if !strings.Contains(prompt, "Plans shared Pulse resource actions; approval and execution stay on the canonical action lifecycle") {
		t.Fatalf("expected system prompt to describe capability-bound pulse_control usage, got %q", prompt)
	}
	if !strings.Contains(prompt, "A Proxmox VM or LXC reboot is a hypervisor lifecycle action and does not require the QEMU guest agent") {
		t.Fatalf("expected system prompt to distinguish Proxmox lifecycle control from guest-agent operations, got %q", prompt)
	}
	if !strings.Contains(prompt, "Never redirect the user to manual qm or pct commands when Pulse offers the requested lifecycle capability") {
		t.Fatalf("expected system prompt to keep offered Proxmox lifecycle actions on the governed Pulse path, got %q", prompt)
	}
	for _, want := range []string{
		"is an ordering rule, not a boundary",
		"Never present it to the user as a prerequisite, a session or discovery binding, or a limitation.",
		"submit pulse_control for each target",
		"Report a limitation only from a tool result in this turn",
		"never hand the user manual steps for an action Pulse offers",
	} {
		if !strings.Contains(prompt, want) {
			t.Fatalf("expected system prompt to carry the advertised-action contract (#1782) %q", want)
		}
	}
	if !strings.Contains(prompt, "Do not claim that a guest agent or capability is unavailable without current tool evidence") {
		t.Fatalf("expected system prompt to prohibit invented guest-agent limitations, got %q", prompt)
	}
	if !strings.Contains(prompt, "## AVAILABLE TOOL GOVERNANCE") {
		t.Fatalf("expected system prompt to include generated tool governance section, got %q", prompt)
	}
	if !strings.Contains(prompt, "pulse_kubernetes") {
		t.Fatalf("expected system prompt to include the governed Kubernetes tool contract, got %q", prompt)
	}
	if !strings.Contains(prompt, "without decorative symbols") {
		t.Fatalf("expected system prompt to keep Assistant formatting operational, got %q", prompt)
	}
}

func TestBuildSystemPrompt_IncludesProvenanceGuidance(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildSystemPrompt()

	for _, expected := range []string{
		"## GROUNDING & PROVENANCE",
		"source observations, model hypotheses, proposed actions, executed operations and verified outcomes separately",
		"historical evidence until a current observation supports it",
		"Attribute measured facts to their source and observation time",
	} {
		if !strings.Contains(prompt, expected) {
			t.Fatalf("expected provenance guidance %q in system prompt, got %q", expected, prompt)
		}
	}
}

func TestBuildSystemPrompt_CurrentResourceRequiresResourceHandoff(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildSystemPrompt()

	for _, expected := range []string{
		"The placeholder current_resource is valid only when this turn includes Pulse resource context",
		"either from a resource-context handoff or from Pulse backend resource-reference resolution",
		"If no attached resource context is present, do not use target_host=\"current_resource\" or resource_id=\"current_resource\"",
		// Resolve-before-asking: the Assistant must try to identify the target
		// with read-only tools and proceed against a sole plausible match for
		// read-only diagnostics, instead of deflecting every "run X" request
		// back to the user. Placeholder targets remain forbidden.
		"Resolve a missing target yourself before asking",
		"run read-only diagnostics against it and name the target in your answer",
		"Ask for the target only when several plausible targets remain after looking, or when the action changes state",
		"Never guess a target you did not resolve",
		"Do not attempt a tool call with current_resource or another placeholder",
		// Tool-argument recovery: a missing/invalid-argument tool failure is
		// self-recovered (enumerate, then retry), never converted into a user
		// question about internal identifiers a first-run operator cannot know.
		"when a tool call fails because an argument is missing or invalid, recover yourself",
		"Never ask the user for internal identifiers such as resource IDs, UUIDs, or metric keys",
	} {
		if !strings.Contains(prompt, expected) {
			t.Fatalf("expected current_resource boundary %q in system prompt, got %q", expected, prompt)
		}
	}
}

func TestBuildSystemPrompt_IncludesSharedPulseIntelligenceOperatingInstructions(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildSystemPrompt()
	shared := agentcapabilities.BuildPulseAssistantOperatingInstructions()

	if !strings.Contains(prompt, shared) {
		t.Fatalf("expected Assistant system prompt to include shared Pulse Intelligence operating instructions")
	}
	if strings.Contains(prompt, "resources, prompts") || strings.Contains(prompt, "capability metadata as the source of truth") {
		t.Fatalf("Assistant system prompt must not advertise MCP-only operating affordances, got %q", prompt)
	}
}

func TestBuildToolGovernancePromptSection_FallbackUsesCanonicalRegistry(t *testing.T) {
	fallback := fallbackAssistantToolGovernance()
	canonical := tools.CanonicalToolGovernanceForManifestSurface(
		tools.ControlLevelControlled,
		agentcapabilities.CanonicalManifest(),
		agentcapabilities.SurfaceIDPulseAssistant,
	)

	if len(fallback) != len(canonical) {
		t.Fatalf("fallback governance length = %d, want canonical registry length %d", len(fallback), len(canonical))
	}
	for i := range canonical {
		if fallback[i] != canonical[i] {
			t.Fatalf("fallback governance[%d] = %#v, want canonical registry descriptor %#v", i, fallback[i], canonical[i])
		}
	}

	prompt := (&Service{}).buildToolGovernancePromptSection()
	for _, tool := range canonical {
		if !strings.Contains(prompt, tool.Name+": mode="+string(tool.ActionMode)) {
			t.Fatalf("expected fallback prompt to include canonical governance for %s, got %q", tool.Name, prompt)
		}
	}
	if strings.Contains(prompt, "patrol_report_finding") ||
		strings.Contains(prompt, "patrol_resolve_finding") ||
		strings.Contains(prompt, "patrol_get_findings") {
		t.Fatalf("expected Assistant fallback prompt to exclude Patrol-only tools, got %q", prompt)
	}
}

func TestBuildToolGovernancePromptSection_FallbackOfferedNamesUseAssistantAffordances(t *testing.T) {
	fallback := fallbackAssistantToolGovernance()
	names := fallbackAssistantToolGovernanceOfferedNames(fallback)

	// The canonical Assistant surface declares no interactive questions, so
	// the fallback names are exactly the governance names.
	if len(names) != len(fallback) {
		t.Fatalf("fallback offered names length = %d, want governance names only", len(names))
	}
	for i, tool := range fallback {
		if names[i] != tool.Name {
			t.Fatalf("fallback offered names[%d] = %q, want %q", i, names[i], tool.Name)
		}
	}
	for _, name := range names {
		if name == agentcapabilities.PulseQuestionToolName {
			t.Fatalf("fallback offered names must not include pulse_question, got %#v", names)
		}
	}
}

func TestBuildToolGovernancePromptSection_OfferedToolsUseCanonicalFallback(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildToolGovernancePromptSectionForOfferedTools([]providers.Tool{{Name: "pulse_query"}})

	if !strings.Contains(prompt, "pulse_query: mode=read") {
		t.Fatalf("expected offered fallback prompt to use canonical query governance, got %q", prompt)
	}
	if strings.Contains(prompt, "pulse_control:") {
		t.Fatalf("expected offered fallback prompt to exclude non-offered control tools, got %q", prompt)
	}
}

func TestBuildToolGovernancePromptSection_OfferedToolsUseProviderMetadata(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildToolGovernancePromptSectionForOfferedTools([]providers.Tool{
		{
			Name: "pulse_query",
			PulseGovernance: &agentcapabilities.ToolGovernanceDescriptor{
				Name:            "pulse_query",
				Description:     "Discover infrastructure.",
				ActionMode:      agentcapabilities.ActionModeMixed,
				ApprovalPolicy:  agentcapabilities.ApprovalPolicyScopeOnly,
				ApprovalSummary: "metadata-owned approval summary",
				Summary:         "metadata-owned discovery summary",
			},
		},
	})

	if !strings.Contains(prompt, "pulse_query: mode=mixed; approval=scope_only (metadata-owned approval summary); metadata-owned discovery summary") {
		t.Fatalf("expected offered tool metadata to drive governance prompt, got %q", prompt)
	}
	if strings.Contains(prompt, "run uses read-only evidence collection and updates the discovery cache") {
		t.Fatalf("expected offered metadata to avoid executor fallback governance, got %q", prompt)
	}
}

func TestBuildToolGovernancePromptSection_OfferedQuestionToolStaysInteractive(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildToolGovernancePromptSectionForOfferedTools([]providers.Tool{{Name: pulseQuestionToolName}})

	if !strings.Contains(prompt, agentcapabilities.PulseQuestionToolGovernancePromptLine()) {
		t.Fatalf("expected Assistant question tool to keep interactive governance, got %q", prompt)
	}
	if strings.Contains(prompt, "pulse_question: mode=read") {
		t.Fatalf("expected Assistant question tool not to use generic fallback governance, got %q", prompt)
	}
	if strings.Contains(prompt, "No Pulse tools are offered") {
		t.Fatalf("expected question-only manifest to be described as an offered interactive tool, got %q", prompt)
	}
}

func TestBuildToolGovernancePromptSection_NoOfferedToolsUsesSharedNoToolsGuidance(t *testing.T) {
	svc := &Service{}

	prompt := svc.buildToolGovernancePromptSectionForOfferedTools([]providers.Tool{})

	if !strings.Contains(prompt, "No Pulse tools are offered for this turn") {
		t.Fatalf("expected no-tools guidance, got %q", prompt)
	}
	if strings.Contains(prompt, "pulse_question:") {
		t.Fatalf("expected no Assistant question tool when no tools are offered, got %q", prompt)
	}
}

func TestToolsForExecutionMode_RecoveryOnlyKeepsStorage(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{
		RecoveryPointsProvider: &fakeRecoveryPointsProvider{points: []recovery.RecoveryPoint{{
			ID:       "pve-snapshot:before-upgrade",
			Provider: recovery.ProviderProxmoxPVE,
			Kind:     recovery.KindSnapshot,
			Mode:     recovery.ModeLocal,
			Outcome:  recovery.OutcomeSuccess,
		}}},
		ControlLevel: tools.ControlLevelControlled,
	})

	svc := &Service{executor: exec}
	toolsList := svc.toolsForExecutor(nil, false)
	set := toolNameSet(toolsList)
	if !set["pulse_storage"] {
		t.Fatalf("expected storage tool to be kept when recovery points are the only storage source")
	}
}

func TestToolsForExecutionMode_PatrolScopeUsesConfigNotPrompt(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{
		StateProvider: fakeStateProvider{},
		AgentServer:   fakeAgentServer{},
		ControlLevel:  tools.ControlLevelControlled,
	})

	svc := &Service{
		executor: exec,
		cfg: &config.AIConfig{
			PatrolAnalyzeDocker:  false,
			PatrolAnalyzeStorage: false,
		},
	}

	exec.ApplyExecutionProfile(tools.ProfilePatrolDetection)
	filtered := svc.toolsForExecutor(exec, false)
	set := toolNameSet(filtered)

	if set["pulse_docker"] {
		t.Fatalf("expected pulse_docker to follow disabled Patrol subsystem scope")
	}
	if set["pulse_storage"] {
		t.Fatalf("expected pulse_storage to follow disabled Patrol subsystem scope")
	}
	if !set["pulse_query"] {
		t.Fatalf("expected core read/query tools to remain available to the Patrol model")
	}
}

func testRunCommandToolDefinition() tools.Tool {
	return tools.Tool{
		Name: agentcapabilities.PulseRunCommandToolName,
		InputSchema: tools.InputSchema{Properties: map[string]tools.PropertySchema{
			"command":     {Type: "string"},
			"run_on_host": {Type: "boolean"},
			"target_host": {Type: "string"},
		}},
	}
}

func TestExecuteCommand_SuccessAndExitCode(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
	svc := &Service{executor: exec}

	output, code, err := svc.ExecuteCommand(context.Background(), "uptime", "")
	if err == nil || code != 1 {
		t.Fatalf("expected retired command error with exit code 1, got output=%q code=%d err=%v", output, code, err)
	}
	if !strings.Contains(output, "retired and denied") {
		t.Fatalf("expected retirement output, got: %s", output)
	}
}

func TestExecuteCommand_ErrorAndApprovalPaths(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
	svc := &Service{executor: exec}

	_, code, err := svc.ExecuteCommand(context.Background(), "uptime", "")
	if err == nil || code != 1 {
		t.Fatalf("expected error with exit code 1")
	}

}

func TestExecuteCommandUsesSharedResultTextProjection(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
	svc := &Service{executor: exec}
	output, code, err := svc.ExecuteCommand(context.Background(), "uptime", "")
	if err == nil || code != 1 {
		t.Fatalf("expected retired command denial, output=%q code=%d err=%v", output, code, err)
	}
	if !strings.Contains(output, "retired and denied") {
		t.Fatalf("output = %q, want retirement response", output)
	}
}

func TestExecuteAssistantTool_ErrorsAndSuccess(t *testing.T) {
	// Registry entries are append-only, so each handler scenario runs on
	// a fresh executor instead of swapping the handler in place.
	newService := func(handler func(ctx context.Context, exec *tools.PulseToolExecutor, args map[string]interface{}) (tools.CallToolResult, error)) *Service {
		exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
		exec.RegisterTool(tools.RegisteredTool{
			Invocation: tools.StaticInvocation(agentcapabilities.ToolCallKindRead, agentcapabilities.MutationNone),
			Definition: tools.Tool{Name: "test_tool"},
			Handler:    handler,
		})
		return &Service{executor: exec}
	}

	svc := newService(func(ctx context.Context, exec *tools.PulseToolExecutor, args map[string]interface{}) (tools.CallToolResult, error) {
		return tools.NewErrorResult(context.DeadlineExceeded), nil
	})
	if _, err := svc.ExecuteAssistantTool(context.Background(), "test_tool", map[string]interface{}{}); err == nil {
		t.Fatalf("expected tool error")
	}

	svc = newService(func(ctx context.Context, exec *tools.PulseToolExecutor, args map[string]interface{}) (tools.CallToolResult, error) {
		return tools.NewTextResult("POLICY_BLOCKED: nope"), nil
	})
	if _, err := svc.ExecuteAssistantTool(context.Background(), "test_tool", map[string]interface{}{}); err == nil {
		t.Fatalf("expected policy blocked error")
	}

	svc = newService(func(ctx context.Context, exec *tools.PulseToolExecutor, args map[string]interface{}) (tools.CallToolResult, error) {
		return tools.NewTextResult("ok"), nil
	})
	output, err := svc.ExecuteAssistantTool(context.Background(), "test_tool", map[string]interface{}{})
	if err != nil || output != "ok" {
		t.Fatalf("expected success, got output=%q err=%v", output, err)
	}
}

func TestExecuteAssistantToolUsesSharedResultTextProjection(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
	exec.RegisterTool(tools.RegisteredTool{
		Invocation: tools.StaticInvocation(agentcapabilities.ToolCallKindRead, agentcapabilities.MutationNone),
		Definition: tools.Tool{Name: "test_tool"},
		Handler: func(ctx context.Context, exec *tools.PulseToolExecutor, args map[string]interface{}) (tools.CallToolResult, error) {
			return tools.CallToolResult{
				Content: []tools.Content{
					{Type: "text", Text: "first"},
					{Type: "resource", URI: "file://ignored"},
					{Type: "text"},
					{Type: "text", Text: "second"},
				},
			}, nil
		},
	})

	svc := &Service{executor: exec}
	output, err := svc.ExecuteAssistantTool(context.Background(), "test_tool", map[string]interface{}{})
	if err != nil {
		t.Fatalf("ExecuteAssistantTool: %v", err)
	}
	if output != "first\nsecond" {
		t.Fatalf("output = %q, want shared result text projection", output)
	}
}

func TestAgenticLoopUsesSharedProviderToolResultConstruction(t *testing.T) {
	src, err := os.ReadFile("agentic.go")
	if err != nil {
		t.Fatalf("read agentic.go: %v", err)
	}
	text := string(src)

	for _, fragment := range []string{
		"agentcapabilities.ParseApprovalRequiredToolMarkerData(resultText)",
		"inputWithApproval = agentcapabilities.WithApprovalArgument(inputWithApproval, approvalData.ApprovalID)",
		"agentcapabilities.NewProviderToolResultFromToolResult(tc.ID, result)",
		"projection := newProviderToolResultContextProjection(tc.ID, resultText, isError)",
		"ToolResult: &projection.Transcript",
		"ToolResult: &projection.Model",
	} {
		if !strings.Contains(text, fragment) {
			t.Fatalf("agentic loop must use shared provider tool-result construction; missing %s", fragment)
		}
	}
	if strings.Contains(text, "resultText = FormatToolResult(result)") {
		t.Fatalf("agentic loop must not flatten tool results into provider context locally")
	}
	if !strings.Contains(text, "toolCalls = agentcapabilities.NormalizeProviderToolCallsForExecution(data.ToolCalls)") {
		t.Fatalf("agentic loop must normalize provider-emitted tool calls through the shared tools/call projection before execution")
	}
	if strings.Contains(text, "toolCalls = data.ToolCalls") {
		t.Fatalf("agentic loop must not execute raw provider tool-call names or inputs")
	}
	for _, forbidden := range []string{
		"agentcapabilities.ApprovalRequiredToolMarkerPayloadJSON(resultText)",
		"var approvalData struct",
		"\n\t\t\t\t\t\t\tagentcapabilities.WithApprovalArgument(inputWithApproval, approvalData.ApprovalID)",
	} {
		if strings.Contains(text, forbidden) {
			t.Fatalf("agentic loop must use shared approval marker/replay helpers correctly; found %s", forbidden)
		}
	}
	for _, forbidden := range []string{
		"ToolResult: &providers.ToolResult{",
		"ToolResult: &ToolResult{",
		"agentcapabilities.NewProviderToolResultFromMCP(tc.ID, result)",
		"modelResultText := truncateToolResultForModel(resultText)",
	} {
		if strings.Contains(text, forbidden) {
			t.Fatalf("agentic loop must not assemble provider tool-result structs locally; found %s", forbidden)
		}
	}
}

func TestExecuteAssistantTool_PulseStorageSnapshotsToleratesMalformedRecoveryMetadata(t *testing.T) {
	completedAt := time.Date(2026, 2, 24, 10, 30, 0, 0, time.UTC)
	svc := &Service{
		executor: tools.NewPulseToolExecutor(tools.ExecutorConfig{
			StateProvider: fakeStateProvider{},
			ReadState:     &fakeCanonicalReadState{},
			RecoveryPointsProvider: &fakeRecoveryPointsProvider{points: []recovery.RecoveryPoint{
				{
					ID:       "pve-snapshot:snap-100-before-upgrade",
					Provider: recovery.ProviderProxmoxPVE,
					Kind:     recovery.KindSnapshot,
					Mode:     recovery.ModeLocal,
					Outcome:  recovery.OutcomeSuccess,
					SubjectRef: &recovery.ExternalRef{
						Type:      "vm",
						Name:      "100",
						ID:        "100",
						Namespace: "pve1",
						Class:     "node1",
					},
					Display: &recovery.RecoveryPointDisplay{
						SubjectLabel:   "100",
						ItemType:       "vm",
						ClusterLabel:   "pve1",
						NodeHostLabel:  "node1",
						EntityIDLabel:  "100",
						DetailsSummary: "before-upgrade",
					},
					CompletedAt: &completedAt,
				},
			}},
		}),
	}

	output, err := svc.ExecuteAssistantTool(context.Background(), "pulse_storage", map[string]interface{}{
		"type":     "snapshots",
		"guest_id": "100",
		"instance": "pve1",
	})
	if err != nil {
		t.Fatalf("ExecuteAssistantTool(pulse_storage snapshots): %v", err)
	}

	var resp tools.SnapshotsResponse
	if err := json.Unmarshal([]byte(output), &resp); err != nil {
		t.Fatalf("unmarshal snapshots response: %v\noutput=%s", err, output)
	}
	if resp.Total != 1 || resp.Filtered != 1 || len(resp.Snapshots) != 1 {
		t.Fatalf("unexpected snapshots counts: total=%d filtered=%d len=%d", resp.Total, resp.Filtered, len(resp.Snapshots))
	}
	if resp.Snapshots[0].VMID != 100 || resp.Snapshots[0].Instance != "pve1" || resp.Snapshots[0].Node != "node1" {
		t.Fatalf("unexpected snapshot identity: %#v", resp.Snapshots[0])
	}
	if resp.Snapshots[0].Type != "vm" || resp.Snapshots[0].SnapshotName != "before-upgrade" {
		t.Fatalf("unexpected snapshot canonical fields: %#v", resp.Snapshots[0])
	}
}

func TestExecuteAssistantTool_PulseStorageBackupTasksToleratesMalformedRecoveryMetadata(t *testing.T) {
	startedAt := time.Date(2026, 2, 24, 11, 0, 0, 0, time.UTC)
	completedAt := time.Date(2026, 2, 24, 11, 15, 0, 0, time.UTC)
	svc := &Service{
		executor: tools.NewPulseToolExecutor(tools.ExecutorConfig{
			StateProvider: fakeStateProvider{},
			ReadState:     &fakeCanonicalReadState{},
			RecoveryPointsProvider: &fakeRecoveryPointsProvider{points: []recovery.RecoveryPoint{
				{
					ID:       "pve-task:task-101-backup",
					Provider: recovery.ProviderProxmoxPVE,
					Kind:     recovery.KindBackup,
					Mode:     recovery.ModeLocal,
					Outcome:  recovery.OutcomeSuccess,
					SubjectRef: &recovery.ExternalRef{
						Type:      "vm",
						Name:      "101",
						ID:        "101",
						Namespace: "pve1",
						Class:     "node1",
					},
					Display: &recovery.RecoveryPointDisplay{
						SubjectLabel:   "101",
						ItemType:       "vm",
						ClusterLabel:   "pve1",
						NodeHostLabel:  "node1",
						EntityIDLabel:  "101",
						DetailsSummary: "completed successfully",
					},
					StartedAt:   &startedAt,
					CompletedAt: &completedAt,
				},
			}},
		}),
	}

	output, err := svc.ExecuteAssistantTool(context.Background(), "pulse_storage", map[string]interface{}{
		"type":     "backup_tasks",
		"guest_id": "101",
		"instance": "pve1",
		"status":   "OK",
	})
	if err != nil {
		t.Fatalf("ExecuteAssistantTool(pulse_storage backup_tasks): %v", err)
	}

	var resp tools.BackupTasksListResponse
	if err := json.Unmarshal([]byte(output), &resp); err != nil {
		t.Fatalf("unmarshal backup tasks response: %v\noutput=%s", err, output)
	}
	if resp.Total != 1 || resp.Filtered != 1 || len(resp.Tasks) != 1 {
		t.Fatalf("unexpected backup task counts: total=%d filtered=%d len=%d", resp.Total, resp.Filtered, len(resp.Tasks))
	}
	if resp.Tasks[0].VMID != 101 || resp.Tasks[0].Instance != "pve1" || resp.Tasks[0].Node != "node1" {
		t.Fatalf("unexpected backup task identity: %#v", resp.Tasks[0])
	}
	if resp.Tasks[0].Type != "vm" || resp.Tasks[0].Status != "OK" {
		t.Fatalf("unexpected backup task canonical fields: %#v", resp.Tasks[0])
	}
}

func TestService_StartInitializesActionAuditStore(t *testing.T) {
	svc := NewService(Config{
		AIConfig: &config.AIConfig{
			ChatModel: "mock:model",
		},
		DataDir: t.TempDir(),
		OrgID:   "org-a",
	})
	svc.providerFactory = func(modelStr string) (providers.StreamingProvider, error) {
		return &mockStreamingProvider{}, nil
	}

	if err := svc.Start(context.Background()); err != nil {
		t.Fatalf("start service: %v", err)
	}
	if svc.actionAuditStore == nil {
		t.Fatalf("expected action audit store to be initialized")
	}
	if svc.executor == nil {
		t.Fatalf("expected executor to be initialized")
	}
	if svc.executor.GetActionAuditStore() == nil {
		t.Fatalf("expected executor action audit store to be set")
	}

	if err := svc.Stop(context.Background()); err != nil {
		t.Fatalf("stop service: %v", err)
	}
	if svc.actionAuditStore != nil {
		t.Fatalf("expected action audit store to be cleared on stop")
	}
}

func TestExecutionProfilePromptModes(t *testing.T) {
	loop := &AgenticLoop{baseSystemPrompt: "base"}

	prompt := loop.getSystemPrompt()
	if !strings.Contains(prompt, "EXECUTION MODE: Controlled") {
		t.Fatalf("default interactive profile should describe controlled mode, got %q", prompt)
	}

	loop.SetAutonomousMode(true)
	prompt = loop.getSystemPrompt()
	if !strings.Contains(prompt, "EXECUTION MODE: Autonomous") {
		t.Fatalf("interactive autonomous should describe autonomous mode, got %q", prompt)
	}

	// Patrol profiles own their mode text and never claim controlled or
	// autonomous execution, regardless of inherited autonomy flags.
	loop.SetExecutionProfile(tools.ProfilePatrolDetection)
	prompt = loop.getSystemPrompt()
	if !strings.Contains(prompt, "EXECUTION MODE: Patrol detection") ||
		strings.Contains(prompt, "EXECUTION MODE: Autonomous") ||
		strings.Contains(prompt, "EXECUTION MODE: Controlled") {
		t.Fatalf("detection profile prompt wrong: %q", prompt)
	}

	loop.SetExecutionProfile(tools.ProfilePatrolInvestigation)
	prompt = loop.getSystemPrompt()
	if !strings.Contains(prompt, "EXECUTION MODE: Patrol investigation") {
		t.Fatalf("investigation profile prompt wrong: %q", prompt)
	}
	if strings.Contains(prompt, "EXECUTION MODE: Autonomous") ||
		strings.Contains(prompt, "EXECUTION MODE: Controlled") {
		t.Fatalf("investigation inherited interactive execution authority: %q", prompt)
	}
}

func TestToolsForExecutorNeverOffersQuestionTool(t *testing.T) {
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{
		ControlLevel:  tools.ControlLevelControlled,
		StateProvider: &mockStateProvider{},
		AgentServer:   &mockAgentServer{},
	})
	svc := &Service{executor: exec, cfg: &config.AIConfig{}}

	interactive := svc.toolsForExecutor(nil, false)
	if toolNameSet(interactive)[pulseQuestionToolName] {
		t.Fatal("interactive manifest must not offer pulse_question: no Pulse client can answer it")
	}

	detection := exec.Clone()
	detection.ApplyExecutionProfile(tools.ProfilePatrolDetection)
	detectionTools := svc.toolsForExecutor(detection, false)
	if toolNameSet(detectionTools)[pulseQuestionToolName] {
		t.Fatal("detection profile must hide pulse_question")
	}

	investigation := exec.Clone()
	investigation.ApplyExecutionProfile(tools.ProfilePatrolInvestigation)
	investigationTools := svc.toolsForExecutor(investigation, false)
	set := toolNameSet(investigationTools)
	if set[pulseQuestionToolName] {
		t.Fatal("investigation profile must hide pulse_question")
	}
	if set["patrol_report_finding"] || set["pulse_control"] || set["pulse_file_edit"] {
		t.Fatalf("investigation manifest must not offer mutation tools, got %v", set)
	}
	if !set["pulse_query"] || !set["pulse_read"] {
		t.Fatalf("investigation manifest must keep read tools, got %v", set)
	}
}

func TestNonInteractiveProfileBlocksQuestionPersistsPairAndContinues(t *testing.T) {
	turn := 0
	provider := &stubStreamingProvider{}
	provider.chatStream = func(ctx context.Context, req providers.ChatRequest, callback providers.StreamCallback) error {
		turn++
		switch turn {
		case 1:
			// The model fabricates an interactive question alongside a
			// legitimate read in the same provider turn.
			callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{
				ToolCalls: []providers.ToolCall{
					{ID: "q-1", Name: pulseQuestionToolName, Input: map[string]interface{}{"question": "which host?"}},
					{ID: "r-1", Name: "pulse_query", Input: map[string]interface{}{"action": "health"}},
				},
			}})
		default:
			callback(providers.StreamEvent{Type: "content", Data: providers.ContentEvent{Text: "diagnosis complete"}})
			callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{}})
		}
		return nil
	}

	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{
		StateProvider: &mockStateProvider{},
		AgentServer:   &mockAgentServer{},
	})
	exec.ApplyExecutionProfile(tools.ProfilePatrolDetection)

	loop := NewAgenticLoop(provider, exec, "base prompt")
	loop.SetExecutionProfile(tools.ProfilePatrolDetection)

	var sawWaiting bool
	messages, err := loop.ExecuteWithTools(
		context.Background(),
		"session-noninteractive",
		[]Message{{Role: "user", Content: "investigate"}},
		nil,
		func(event StreamEvent) {
			if strings.Contains(string(event.Data), "Waiting for your answer") {
				sawWaiting = true
			}
		},
	)
	if err != nil {
		t.Fatalf("ExecuteWithTools: %v", err)
	}
	if sawWaiting {
		t.Fatal("non-interactive profile must not emit a waiting-for-answer event")
	}
	if turn < 2 {
		t.Fatalf("inference must continue after the blocked question, provider turns = %d", turn)
	}

	var questionCallSeen, questionResultSeen, queryResultSeen, finalTextSeen bool
	for _, msg := range messages {
		for _, tc := range msg.ToolCalls {
			if tc.Name == pulseQuestionToolName {
				questionCallSeen = true
			}
		}
		if msg.ToolResult != nil {
			switch msg.ToolResult.ToolUseID {
			case "q-1":
				questionResultSeen = true
				if !msg.ToolResult.IsError || !strings.Contains(msg.ToolResult.Content, "non-interactive") {
					t.Fatalf("question result must be a non-interactive error, got %+v", msg.ToolResult)
				}
			case "r-1":
				queryResultSeen = true
			}
		}
		if msg.Role == "assistant" && strings.Contains(msg.Content, "diagnosis complete") {
			finalTextSeen = true
		}
	}
	if !questionCallSeen {
		t.Fatal("persisted transcript must retain the question tool call")
	}
	if !questionResultSeen {
		t.Fatal("persisted transcript must pair the question call with its refusal result")
	}
	if !queryResultSeen {
		t.Fatal("sibling tool call from the same provider turn must still execute")
	}
	if !finalTextSeen {
		t.Fatal("loop must continue to the model's final answer after the blocked question")
	}
}

func TestInvestigationLoopRedactsProposalParamsEverywhereDurable(t *testing.T) {
	const conclusion = "### Investigation Summary\nRestart proposed.\n\n### Root Cause\nThe cause is unknown. The earlier dependency rationale is unconfirmed.\n\n### Affected Resources\n`vm:42`.\n\n### Recommendation\nRestart pending.\n\n### Conclusion\nNEEDS_ATTENTION: pending."
	turn := 0
	provider := &stubStreamingProvider{}
	provider.chatStream = func(ctx context.Context, req providers.ChatRequest, callback providers.StreamCallback) error {
		turn++
		if turn == 1 {
			callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{
				ToolCalls: []providers.ToolCall{{
					ID:   "p-1",
					Name: agentcapabilities.PatrolProposeActionToolName,
					Input: map[string]interface{}{
						"resource_id":        "vm:42",
						"causal_resource_id": "vm:dependency",
						"capability_name":    "restart",
						"params":             map[string]interface{}{"mode": "graceful"},
						"reason":             "vm:dependency is stopped, stalling the web tier",
					},
				}},
			}})
			return nil
		}
		if len(req.Tools) == 0 {
			t.Fatal("planning acceptance disabled further investigation tools")
		}
		// The provider continuation must still see the raw params from
		// its own prior call.
		for _, msg := range req.Messages {
			for _, tc := range msg.ToolCalls {
				if tc.Name == agentcapabilities.PatrolProposeActionToolName {
					params, ok := tc.Input["params"].(map[string]interface{})
					if !ok || params["mode"] != "graceful" {
						t.Errorf("provider continuation lost raw proposal params: %#v", tc.Input["params"])
					}
				}
			}
		}
		callback(providers.StreamEvent{Type: "content", Data: providers.ContentEvent{Text: conclusion}})
		callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{}})
		return nil
	}

	capture := tools.NewProposalCapture(
		tools.ProposalIdentity{ProposalID: "prop-9", FindingID: "f-9", InvestigationID: "inv-9"},
		func(ctx context.Context, resourceID string) ([]ur.ResourceCapability, error) {
			return []ur.ResourceCapability{{
				Name: "restart",
				Params: []ur.CapabilityParam{
					{Name: "mode", Type: "string", Required: true, Enum: []string{"graceful", "force"}},
				},
			}}, nil
		})
	capture.SetPlanner(func(context.Context, tools.CapturedProposal) (ur.ActionAuditRecord, error) {
		return ur.ActionAuditRecord{ID: "act_persisted", State: ur.ActionStatePending}, nil
	})
	exec := tools.NewPulseToolExecutor(tools.ExecutorConfig{})
	exec.ApplyExecutionProfile(tools.ProfilePatrolInvestigation)
	exec.SetProposalCapture(capture)

	loop := NewAgenticLoop(provider, exec, "base prompt")
	loop.SetExecutionProfile(tools.ProfilePatrolInvestigation)

	var streamedRawParam bool
	var streamedConclusion strings.Builder
	messages, err := loop.ExecuteWithTools(
		context.Background(),
		"session-investigation",
		[]Message{{Role: "user", Content: "investigate finding f-9"}},
		[]providers.Tool{{Name: agentcapabilities.PatrolProposeActionToolName}, {Name: agentcapabilities.PulseQueryToolName}},
		func(event StreamEvent) {
			if event.Type == "content" {
				var data ContentData
				if err := json.Unmarshal(event.Data, &data); err != nil {
					t.Errorf("decode content: %v", err)
				}
				streamedConclusion.WriteString(data.Text)
			}
			if strings.Contains(string(event.Data), "graceful") {
				streamedRawParam = true
			}
		},
	)
	if err != nil {
		t.Fatalf("ExecuteWithTools: %v", err)
	}
	if streamedRawParam {
		t.Fatal("stream events must never expose proposal parameter values")
	}

	if streamedConclusion.String() != conclusion {
		t.Fatalf("stream changed the model's uncertain diagnosis: %q", streamedConclusion.String())
	}
	var durableConclusion strings.Builder
	for _, msg := range messages {
		if msg.Role == "assistant" {
			durableConclusion.WriteString(msg.Content)
		}
	}
	if durableConclusion.String() != conclusion {
		t.Fatalf("transcript changed the model's uncertain diagnosis: %q", durableConclusion.String())
	}

	// The durable transcript keeps the call but with redacted params.
	var sawCall bool
	for _, msg := range messages {
		for _, tc := range msg.ToolCalls {
			if tc.Name != agentcapabilities.PatrolProposeActionToolName {
				continue
			}
			sawCall = true
			if tc.Input["params"] != agentcapabilities.RedactedProposalParamsMarker {
				t.Fatalf("transcript tool call must carry redacted params, got %#v", tc.Input["params"])
			}
		}
	}
	if !sawCall {
		t.Fatal("transcript must retain the proposal tool call")
	}

	// The structured capture holds the validated raw values.
	proposal, outcomeErr := capture.Outcome()
	if outcomeErr != nil {
		t.Fatalf("capture outcome: %v", outcomeErr)
	}
	if proposal == nil || proposal.Params["mode"] != "graceful" || proposal.Identity.FindingID != "f-9" || proposal.InvocationID != "p-1" {
		t.Fatalf("captured proposal = %#v", proposal)
	}
	if proposal.CausalResourceID != "vm:dependency" {
		t.Fatalf("captured causal resource = %q, want vm:dependency", proposal.CausalResourceID)
	}
}

func TestInvestigationServicePreservesUncertainDiagnosisAfterProposal(t *testing.T) {
	const conclusion = "### Root Cause\nUnknown. A restart may restore service, but the earlier dependency explanation is unconfirmed.\n\n### Recommendation\nProposal pending approval. No action has executed."
	store, err := NewSessionStore(t.TempDir())
	if err != nil {
		t.Fatal(err)
	}
	turn := 0
	provider := &stubStreamingProvider{}
	provider.chatStream = func(ctx context.Context, req providers.ChatRequest, callback providers.StreamCallback) error {
		turn++
		switch turn {
		case 1:
			callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{ToolCalls: []providers.ToolCall{{
				ID: "evidence-1", Name: agentcapabilities.PulseQueryToolName,
				Input: map[string]interface{}{"action": "health"},
			}}}})
		case 2:
			callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{ToolCalls: []providers.ToolCall{{
				ID: "proposal-1", Name: agentcapabilities.PatrolProposeActionToolName,
				Input: map[string]interface{}{"resource_id": "vm:42", "causal_resource_id": "vm:dependency", "capability_name": "restart", "reason": "The dependency may have stopped."},
			}}}})
		default:
			callback(providers.StreamEvent{Type: "content", Data: providers.ContentEvent{Text: conclusion}})
			callback(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{}})
		}
		return nil
	}
	service := &Service{
		started: true, sessions: store,
		executor:              tools.NewPulseToolExecutor(tools.ExecutorConfig{StateProvider: &mockStateProvider{}}),
		cfg:                   &config.AIConfig{PatrolModel: "mock:model", ControlLevel: config.ControlLevelReadOnly},
		patrolProviderFactory: func(string) (providers.StreamingProvider, error) { return provider, nil },
	}
	var streamed strings.Builder
	result, err := service.ExecuteInvestigationStream(context.Background(), InvestigationRunRequest{
		SessionID: "uncertain-investigation", Prompt: "Investigate the issue", SystemPrompt: "Investigate using current evidence.",
		MaxTurns: 5, MaxEvidenceCalls: 3, ResourceType: "vm",
		Identity: tools.ProposalIdentity{ProposalID: "request-1", FindingID: "finding-1", InvestigationID: "investigation-1"},
		Planner: func(_ context.Context, p tools.CapturedProposal) (ur.ActionAuditRecord, error) {
			if len(p.Identity.EvidenceIDs) != 1 || p.Identity.EvidenceIDs[0] != "evidence-1" {
				t.Fatalf("planning lost prior completed observation: %+v", p.Identity)
			}
			return ur.ActionAuditRecord{ID: "act_investigation", State: ur.ActionStatePending}, nil
		},
		Catalog: func(context.Context, string) ([]ur.ResourceCapability, error) {
			return []ur.ResourceCapability{{Name: "restart"}}, nil
		},
	}, func(event StreamEvent) {
		if event.Type == "content" {
			var data ContentData
			if err := json.Unmarshal(event.Data, &data); err != nil {
				t.Errorf("content decode: %v", err)
			}
			streamed.WriteString(data.Text)
		}
	})
	if err != nil {
		t.Fatal(err)
	}
	if result.Proposal == nil || result.Proposal.CausalResourceID != "vm:dependency" {
		t.Fatalf("proposal attribution lost: %+v", result.Proposal)
	}
	if result.Content != conclusion || streamed.String() != conclusion {
		t.Fatalf("service promoted proposal rationale into diagnosis: result=%q stream=%q", result.Content, streamed.String())
	}
	messages, err := store.GetMessages("uncertain-investigation")
	if err != nil {
		t.Fatal(err)
	}
	var persisted strings.Builder
	for _, msg := range messages {
		if msg.Role == "assistant" {
			persisted.WriteString(msg.Content)
		}
	}
	if persisted.String() != conclusion {
		t.Fatalf("stored diagnosis mutated: %q", persisted.String())
	}
}

func TestProposalRawInputOverrideNeverLeaksThroughProgressEvents(t *testing.T) {
	var events []string
	callback := func(event StreamEvent) { events = append(events, string(event.Data)) }

	// Providers stream the accumulating raw tool-call JSON as a
	// RawInput override on progress events; for exposure-restricted
	// tools that override must be discarded, not substituted back in
	// after projection.
	secret := `{"params":{"join_token":"super-secret-value"}}`
	emitToolProgressEventWithRawInput(callback, "p-1",
		agentcapabilities.PatrolProposeActionToolName,
		map[string]interface{}{
			"resource_id": "vm:42",
			"params":      map[string]interface{}{"join_token": "super-secret-value"},
		},
		secret, "streaming", "Receiving tool call.")

	if len(events) != 1 {
		t.Fatalf("expected one event, got %d", len(events))
	}
	if strings.Contains(events[0], "super-secret-value") {
		t.Fatalf("progress event leaked raw proposal params: %s", events[0])
	}
	if !strings.Contains(events[0], agentcapabilities.RedactedProposalParamsMarker) {
		t.Fatalf("progress event should carry the projected form, got %s", events[0])
	}

	// Unrestricted tools keep the provider's raw override.
	events = nil
	emitToolProgressEventWithRawInput(callback, "q-1", "pulse_query",
		map[string]interface{}{"action": "health"},
		`{"action":"health","extra":"raw-override"}`, "streaming", "Receiving tool call.")
	if len(events) != 1 {
		t.Fatalf("expected one event, got %d", len(events))
	}
	if !strings.Contains(events[0], "raw-override") {
		t.Fatalf("unrestricted tools should keep the raw override, got %s", events[0])
	}
}

func TestInvestigationRunRequiresIdentityBeforeStarting(t *testing.T) {
	svc := &Service{}
	_, err := svc.ExecuteInvestigationStream(context.Background(), InvestigationRunRequest{
		Prompt:   "investigate",
		Identity: tools.ProposalIdentity{FindingID: "f-1"}, // missing investigation ID
	}, func(StreamEvent) {})
	if err == nil || !strings.Contains(err.Error(), "identity") {
		t.Fatalf("expected identity precondition error, got %v", err)
	}
}

func TestAssistantRuntimeProjectsLegacyLevelAfterResolver(t *testing.T) {
	for _, saved := range []string{config.ControlLevelReadOnly, config.ControlLevelControlled, config.ControlLevelAutonomous, "unknown"} {
		for _, resolved := range []string{"", config.ControlLevelReadOnly, config.ControlLevelControlled, config.ControlLevelAutonomous} {
			t.Run(saved+"/resolver-"+resolved, func(t *testing.T) {
				cfg := &config.AIConfig{ControlLevel: saved}
				var resolver func(*config.AIConfig) string
				if resolved != "" {
					resolver = func(*config.AIConfig) string { return resolved }
				}
				service := NewService(Config{AIConfig: cfg, ControlLevelResolver: resolver})
				selected := saved
				if resolved != "" {
					selected = resolved
				}
				want := tools.ControlLevelReadOnly
				if selected == config.ControlLevelControlled || selected == config.ControlLevelAutonomous {
					want = tools.ControlLevelControlled
				}
				if got := service.effectiveControlLevelLocked(); got != want {
					t.Fatalf("interactive resolved level %s; want %s", got, want)
				}
				if cfg.ControlLevel != saved {
					t.Fatal("runtime projection rewrote saved config")
				}
				if service.isAutonomousModeEnabled() {
					t.Fatal("saved Assistant preference removed interactive question tools")
				}
				// A request flag may tighten but never widen the projected level.
				for _, flag := range []*bool{nil, boolPtrProjection(false), boolPtrProjection(true)} {
					if got := controlLevelForRequestAutonomousMode(service.effectiveControlLevelLocked(), flag); got != want {
						t.Fatalf("request flag widened level: %s", got)
					}
				}
			})
		}
	}
}
func boolPtrProjection(v bool) *bool { return &v }