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.

Separate finding writes from infrastructure verification

rcourtman090e6fde6408
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index de010ea817..587bd53a5d 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -96,6 +96,14 @@ four-turn model budget: active-finding inspection, report or assessment, one
bounded fallback turn, and a tool-free final summary. A capability-unavailable
read result is terminal for that capability within the run and must not trigger
retries or broad inventory scans.
+Patrol report, assessment, and resolution tools remain governed Pulse-state
+writes for invocation authorization, but accepting one does not represent an
+infrastructure mutation and must not enter or satisfy the infrastructure
+read-after-write FSM. A successful finding-lifecycle write moves the loop to a
+text-only conclusion. When a real infrastructure write does require another
+verification turn, the internal verification constraint is appended to the
+provider conversation as a user-role instruction; the request must never end
+in an assistant prefill that compatible providers reject before verification.
Patrol finding storage must consolidate equivalent active storage-capacity
siblings before they reach browser surfaces: a broader storage risk and a
generic usage/capacity finding for the same normalized storage identity are one
diff --git a/internal/ai/chat/agentic.go b/internal/ai/chat/agentic.go
index 97b8e2c39c..80f153edca 100644
--- a/internal/ai/chat/agentic.go
+++ b/internal/ai/chat/agentic.go
@@ -349,6 +349,39 @@ func isKnownGovernedWriteProgress(toolName string, input map[string]interface{},
}
}
+// Patrol finding lifecycle calls mutate governed Pulse state, so their
+// invocation classification must remain write. They do not mutate
+// infrastructure, however, and therefore must not put the infrastructure FSM
+// into VERIFYING or satisfy verification for a preceding infrastructure write.
+func isPatrolFindingLifecycleWrite(toolName string) bool {
+ switch strings.TrimSpace(toolName) {
+ case agentcapabilities.PatrolReportFindingToolName,
+ agentcapabilities.PatrolAssessFindingToolName,
+ agentcapabilities.PatrolResolveFindingToolName:
+ return true
+ default:
+ return false
+ }
+}
+
+func applySuccessfulToolFSM(fsm *SessionFSM, toolKind ToolKind, toolName string) bool {
+ if fsm == nil {
+ return false
+ }
+ if isPatrolFindingLifecycleWrite(toolName) {
+ return true
+ }
+ fsm.OnToolSuccess(toolKind, toolName)
+ return false
+}
+
+func appendFSMVerificationPrompt(messages []providers.Message, prompt string) []providers.Message {
+ return append(messages, providers.Message{
+ Role: "user",
+ Content: prompt,
+ })
+}
+
// sanitizeProviderStreamErrorForUser turns a raw provider/transport error into a
// clean, human message safe to render in the chat. Upstream errors (especially
// from OpenAI-compatible gateways like OpenRouter) embed raw JSON bodies and
@@ -1199,10 +1232,14 @@ func (a *AgenticLoop) executeWithTools(ctx context.Context, sessionID string, me
verifyTarget,
)
- // Update the last assistant message to include verification constraint
+ // Preserve the existing transcript behavior for the internal
+ // constraint, but also add the missing user-role provider anchor.
+ // Providers that reject assistant prefill require the conversation to
+ // end with user input before they can perform the verification turn.
if len(resultMessages) > 0 {
resultMessages[len(resultMessages)-1].Content = verifyPrompt
}
+ providerMessages = appendFSMVerificationPrompt(providerMessages, verifyPrompt)
// Note: verification constraint is injected into resultMessages above (for the model).
// We intentionally do NOT emit this to the user callback — it's an internal protocol
@@ -1810,7 +1847,18 @@ func (a *AgenticLoop) executeWithTools(ctx context.Context, sessionID string, me
// === FSM STATE TRANSITION: Update FSM after successful tool execution ===
if fsm != nil && !isError {
- fsm.OnToolSuccess(toolKind, tc.Name)
+ findingLifecycleWrite := applySuccessfulToolFSM(fsm, toolKind, tc.Name)
+ if findingLifecycleWrite {
+ // Finding lifecycle persistence is complete when the governed
+ // handler accepts it. It neither changes infrastructure nor proves
+ // verification of a preceding infrastructure change. Conclude on
+ // the next text-only turn.
+ writeCompletedLastTurn = true
+ log.Debug().
+ Str("tool", tc.Name).
+ Str("state", string(fsm.State)).
+ Msg("[AgenticLoop] Patrol finding lifecycle write accepted without infrastructure verification transition")
+ }
if toolKind == ToolKindWrite && fsm.State == StateVerifying {
emitWorkflowState(callback, "verify", "Verifying the write before the Assistant responds.", sessionFSMState(fsm), tc.Name)
}
@@ -1818,7 +1866,7 @@ func (a *AgenticLoop) executeWithTools(ctx context.Context, sessionID string, me
// If we just completed verification (read after write in VERIFYING), transition to READING
// This allows subsequent writes to proceed without being blocked
// CRITICAL: Must call this IMMEDIATELY after OnToolSuccess, not just when model gives final answer
- if fsm.State == StateVerifying && fsm.ReadAfterWrite {
+ if !findingLifecycleWrite && fsm.State == StateVerifying && fsm.ReadAfterWrite {
fsm.CompleteVerification()
log.Debug().
Str("tool", tc.Name).
@@ -1832,7 +1880,7 @@ func (a *AgenticLoop) executeWithTools(ctx context.Context, sessionID string, me
//
// Verification evidence is a structured field in the tool output:
// { "verification": { "ok": true, ... } }
- if toolKind == ToolKindWrite && agentcapabilities.ToolResultHasVerificationOK(resultText) {
+ if !findingLifecycleWrite && toolKind == ToolKindWrite && agentcapabilities.ToolResultHasVerificationOK(resultText) {
fsm.OnToolSuccess(ToolKindRead, "self_verify")
if fsm.State == StateVerifying && fsm.ReadAfterWrite {
fsm.CompleteVerification()
diff --git a/internal/ai/chat/agentic_additional_test.go b/internal/ai/chat/agentic_additional_test.go
index 39bdf0d74a..04633edfd4 100644
--- a/internal/ai/chat/agentic_additional_test.go
+++ b/internal/ai/chat/agentic_additional_test.go
@@ -51,6 +51,68 @@ func TestAgenticLoop_Setters(t *testing.T) {
}
}
+func TestIsPatrolFindingLifecycleWrite(t *testing.T) {
+ for _, toolName := range []string{
+ agentcapabilities.PatrolReportFindingToolName,
+ agentcapabilities.PatrolAssessFindingToolName,
+ agentcapabilities.PatrolResolveFindingToolName,
+ } {
+ if !isPatrolFindingLifecycleWrite(toolName) {
+ t.Fatalf("expected %s to be a Patrol finding lifecycle write", toolName)
+ }
+ if kind := ClassifyToolCall(toolName, nil); kind != ToolKindWrite {
+ t.Fatalf("%s must retain governed write classification, got %s", toolName, kind)
+ }
+ }
+
+ for _, toolName := range []string{
+ agentcapabilities.PatrolGetFindingsToolName,
+ agentcapabilities.PulseControlToolName,
+ agentcapabilities.PulseQueryToolName,
+ } {
+ if isPatrolFindingLifecycleWrite(toolName) {
+ t.Fatalf("did not expect %s to bypass infrastructure verification transition", toolName)
+ }
+ }
+}
+
+func TestApplySuccessfulToolFSM_SeparatesFindingStateFromInfrastructureVerification(t *testing.T) {
+ fsm := NewSessionFSM()
+ fsm.State = StateReading
+ if !applySuccessfulToolFSM(fsm, ToolKindWrite, agentcapabilities.PatrolReportFindingToolName) {
+ t.Fatal("expected accepted Patrol finding report to use the lifecycle path")
+ }
+ if fsm.State != StateReading || fsm.WroteThisEpisode || fsm.ReadAfterWrite {
+ t.Fatalf("finding report changed infrastructure FSM: %+v", fsm)
+ }
+
+ fsm.OnToolSuccess(ToolKindWrite, agentcapabilities.PulseControlToolName)
+ if fsm.State != StateVerifying {
+ t.Fatalf("expected real infrastructure write to require verification, got %s", fsm.State)
+ }
+ if !applySuccessfulToolFSM(fsm, ToolKindWrite, agentcapabilities.PatrolAssessFindingToolName) {
+ t.Fatal("expected accepted Patrol assessment to use the lifecycle path")
+ }
+ if fsm.State != StateVerifying || fsm.ReadAfterWrite {
+ t.Fatalf("finding assessment satisfied or escaped infrastructure verification: %+v", fsm)
+ }
+}
+
+func TestAppendFSMVerificationPrompt_EndsWithUserInstruction(t *testing.T) {
+ messages := []providers.Message{{Role: "assistant", Content: "unverified conclusion"}}
+ got := appendFSMVerificationPrompt(messages, "verify the changed target")
+ if len(got) != 2 {
+ t.Fatalf("verification messages = %d, want 2", len(got))
+ }
+ last := got[len(got)-1]
+ if last.Role != "user" || last.Content != "verify the changed target" {
+ t.Fatalf("verification anchor = %+v, want user-role instruction", last)
+ }
+ if messages[0].Content != "unverified conclusion" {
+ t.Fatalf("helper mutated existing provider history: %+v", messages)
+ }
+}
+
func TestPruneMessagesForModel_Stateless(t *testing.T) {
prev := StatelessContext
StatelessContext = true