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.

Hide Assistant reasoning from message history

rcourtman064cd4bc8035
diff --git a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
index 2df2427e34..f30da3a5ae 100644
--- a/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
+++ b/docs/release-control/v6/internal/subsystems/agent-lifecycle.md
@@ -213,7 +213,12 @@ lifecycle evidence only; lifecycle surfaces must not treat approval as
implicit command execution or define a parallel execution handoff. Assistant
handoffs that recover a live Patrol approval by finding ID are still AI/runtime
review context only; agent lifecycle surfaces must not treat that recovered
-approval reference as an agent command grant or host-execution shortcut. When a
+approval reference as an agent command grant or host-execution shortcut.
+Assistant session-history reads from the shared AI endpoints are likewise
+API-owned browser-safe projections: lifecycle surfaces, MCP adapters, and
+agents must not depend on `reasoning_content`, raw `pulse_*` / `patrol_*`
+tool-call prose, token accounting text, or assistant transcript prose as an
+agent command grant, install-token disclosure, or host-execution shortcut. When a
planned resource capability is actually executed from an agent-lifecycle
surface, that handoff must route through `POST /api/actions/{id}/execute` so
the API-owned action audit records `executing` before dispatch and the
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 60b6190677..da9cdc1a8f 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -107,7 +107,13 @@ runtime cost control, and shared AI transport surfaces.
Restored Assistant sessions must hydrate saved assistant content and
persisted tool calls into the same transcript event shape used by live
streams so switching sessions does not hide prior tool evidence or collapse
- the resumed conversation into a text-only transcript.
+ the resumed conversation into a text-only transcript. Saved Assistant
+ message history is part of that same output contract: service reads and
+ `GET /api/ai/sessions/{id}/messages` must return a client-safe projection
+ that normalizes collection fields, strips `reasoning_content`, and sanitizes
+ stored assistant prose before the browser restores a transcript. Provider
+ reasoning may remain in runtime-only history for model continuity, but it
+ must not serialize through public session history or resumed drawer state.
The empty Assistant drawer may surface recent non-empty sessions as direct
resume actions using the backend session list already owned by the drawer;
it must not create a parallel recent-chat store or product-authored prompt
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index 9d0838cf87..7e2c3780b5 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -425,6 +425,15 @@ payload shape change when the portal presents compact client rows.
stored rich handoff envelope with a partial metadata-only envelope, and the
handler must not ask the browser to resend resource context that the chat
runtime can rehydrate from the stored selected-resource envelope.
+ Assistant message-history payloads are part of the same browser-safe
+ projection boundary: `GET /api/ai/sessions/{id}/messages` must encode only
+ messages passed through `chat.Message.ClientSafe()`. Collection fields stay
+ stable for clients, but hidden provider `reasoning_content`, raw
+ `pulse_*` / `patrol_*` tool-call prose, DSML/XML/function-call envelopes,
+ JSON tool-call text, token accounting strings, and provider thinking text
+ must not serialize into API history. Runtime history may retain provider
+ reasoning for model continuity, but restored drawer state must expose only
+ visible assistant prose plus governed structured tool evidence.
Chat stream events are generated from `internal/ai/chat` payload structs
into `frontend-modern/src/api/generated/aiChatEvents.ts`; that generated
union must not include the retired `explore_status` pre-pass event. Runtime
diff --git a/docs/release-control/v6/internal/subsystems/storage-recovery.md b/docs/release-control/v6/internal/subsystems/storage-recovery.md
index 6a18673961..b8e8c19c6d 100644
--- a/docs/release-control/v6/internal/subsystems/storage-recovery.md
+++ b/docs/release-control/v6/internal/subsystems/storage-recovery.md
@@ -636,6 +636,12 @@ recovery scope, or a storage/recovery-owned secret source.
their Patrol conclusion and bounded evidence as investigation context, but
must not reinterpret the briefing as backup recency, restore support,
Patrol-authored remediation guidance, or storage-local action authority.
+ Saved Assistant message history exposed by the shared AI endpoints follows
+ that same adjacent boundary. Storage and recovery surfaces may consume only
+ the API-owned client-safe transcript projection; hidden provider reasoning,
+ raw `pulse_*` / `patrol_*` tool-call prose, token accounting text, and
+ provider thinking text are not recovery evidence, backup freshness, restore
+ eligibility, storage-local approval state, or recovery execution authority.
That same adjacent `internal/api/` boundary still carries Patrol-run
execution identity. Storage and recovery may observe shared Patrol
transport through `internal/api/chat_service_adapter.go`, but they must not
diff --git a/internal/ai/chat/service.go b/internal/ai/chat/service.go
index d1d9ce4a4a..edd96a65bb 100644
--- a/internal/ai/chat/service.go
+++ b/internal/ai/chat/service.go
@@ -2422,7 +2422,14 @@ func (s *Service) GetMessages(ctx context.Context, sessionID string) ([]Message,
return nil, fmt.Errorf("service not started")
}
- return sessions.GetMessages(sessionID)
+ messages, err := sessions.GetMessages(sessionID)
+ if err != nil {
+ return nil, err
+ }
+ for i := range messages {
+ messages[i] = messages[i].ClientSafe()
+ }
+ return messages, nil
}
// GetModelHandoffFindingID returns the session-scoped finding reference used to
diff --git a/internal/ai/chat/service_test.go b/internal/ai/chat/service_test.go
index ce9c2fb13e..c0fc7c866e 100644
--- a/internal/ai/chat/service_test.go
+++ b/internal/ai/chat/service_test.go
@@ -285,6 +285,19 @@ func TestService_SessionWrappers(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, msgs) // Initially empty
+ require.NoError(t, service.sessions.AddMessage(session.ID, Message{
+ Role: "assistant",
+ Content: "I will inspect the device nodes.\npulse_read(target_host=\"current_resource\", command=\"ls /dev | wc -l\")",
+ ReasoningContent: "We need to inspect the user's prompt before answering.",
+ }))
+ msgs, err = service.GetMessages(ctx, session.ID)
+ require.NoError(t, err)
+ require.Len(t, msgs, 1)
+ assert.Equal(t, "I will inspect the device nodes.", msgs[0].Content)
+ assert.Empty(t, msgs[0].ReasoningContent)
+ assert.NotContains(t, msgs[0].Content, "pulse_read")
+ assert.NotContains(t, msgs[0].Content, "target_host")
+
// List Sessions
list, err := service.ListSessions(ctx)
require.NoError(t, err)
diff --git a/internal/ai/chat/types.go b/internal/ai/chat/types.go
index ad9371fc8d..eb59f111af 100644
--- a/internal/ai/chat/types.go
+++ b/internal/ai/chat/types.go
@@ -67,6 +67,18 @@ func (m Message) NormalizeCollections() Message {
return m
}
+// ClientSafe returns the browser/API-facing view of a stored chat message.
+// Runtime history may retain provider reasoning for model continuity, but
+// user-facing history must not serialize hidden reasoning or raw tool-call text.
+func (m Message) ClientSafe() Message {
+ m = m.NormalizeCollections()
+ if strings.EqualFold(m.Role, "assistant") {
+ m.Content = cleanToolCallArtifacts(m.Content)
+ }
+ m.ReasoningContent = ""
+ return m
+}
+
// ToolCall represents a tool invocation
type ToolCall struct {
ID string `json:"id"`
diff --git a/internal/api/ai_handler.go b/internal/api/ai_handler.go
index 68e89a1fdc..1ea5345e3d 100644
--- a/internal/api/ai_handler.go
+++ b/internal/api/ai_handler.go
@@ -2620,7 +2620,7 @@ func (h *AIHandler) HandleMessages(w http.ResponseWriter, r *http.Request, sessi
}
}
for i := range messages {
- messages[i] = messages[i].NormalizeCollections()
+ messages[i] = messages[i].ClientSafe()
}
w.Header().Set("Content-Type", "application/json")
diff --git a/internal/api/ai_handler_test.go b/internal/api/ai_handler_test.go
index faf6590bbb..0a4bb1099e 100644
--- a/internal/api/ai_handler_test.go
+++ b/internal/api/ai_handler_test.go
@@ -459,7 +459,14 @@ func TestHandleMessages(t *testing.T) {
h.defaultService = mockSvc
mockSvc.On("IsRunning").Return(true)
- messages := []chat.Message{{Role: "user", Content: "hello"}}
+ messages := []chat.Message{
+ {Role: "user", Content: "hello"},
+ {
+ Role: "assistant",
+ Content: "I will inspect the device nodes.\npulse_read(target_host=\"current_resource\", command=\"lsblk\")",
+ ReasoningContent: "We need to inspect the user's prompt before answering.",
+ },
+ }
mockSvc.On("GetMessages", mock.Anything, "s1").Return(messages, nil)
req := httptest.NewRequest("GET", "/api/ai/sessions/s1/messages", nil)
@@ -469,6 +476,11 @@ func TestHandleMessages(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)
assert.Contains(t, w.Body.String(), `"tool_calls":[]`)
+ assert.Contains(t, w.Body.String(), "I will inspect the device nodes.")
+ assert.NotContains(t, w.Body.String(), "reasoning_content")
+ assert.NotContains(t, w.Body.String(), "inspect the user's prompt")
+ assert.NotContains(t, w.Body.String(), "pulse_read")
+ assert.NotContains(t, w.Body.String(), "target_host")
}
func TestHandleChat_NotRunning(t *testing.T) {
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index c5280ab77a..6ca077d9aa 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -573,6 +573,56 @@ func TestContract_AssistantFindingContextUsesModelOnlyHandoff(t *testing.T) {
}
}
+func TestContract_AssistantMessageHistoryUsesClientSafeProjection(t *testing.T) {
+ handlerSource, err := os.ReadFile(filepath.Clean("ai_handler.go"))
+ if err != nil {
+ t.Fatalf("read ai_handler.go: %v", err)
+ }
+ serviceSource, err := os.ReadFile(filepath.Clean("../ai/chat/service.go"))
+ if err != nil {
+ t.Fatalf("read chat service: %v", err)
+ }
+ typesSource, err := os.ReadFile(filepath.Clean("../ai/chat/types.go"))
+ if err != nil {
+ t.Fatalf("read chat types: %v", err)
+ }
+
+ handlerText := string(handlerSource)
+ for _, required := range []string{
+ "func (h *AIHandler) HandleMessages(w http.ResponseWriter, r *http.Request, sessionID string)",
+ "messages[i] = messages[i].ClientSafe()",
+ "json.NewEncoder(w).Encode(messages)",
+ } {
+ if !strings.Contains(handlerText, required) {
+ t.Fatalf("Assistant message-history handler must encode client-safe transcript projection: missing %q", required)
+ }
+ }
+
+ serviceText := string(serviceSource)
+ for _, required := range []string{
+ "func (s *Service) GetMessages(ctx context.Context, sessionID string) ([]Message, error)",
+ "messages[i] = messages[i].ClientSafe()",
+ } {
+ if !strings.Contains(serviceText, required) {
+ t.Fatalf("chat service message-history reads must return client-safe transcript projection: missing %q", required)
+ }
+ }
+
+ typesText := string(typesSource)
+ for _, required := range []string{
+ "func (m Message) ClientSafe() Message",
+ "m = m.NormalizeCollections()",
+ `if strings.EqualFold(m.Role, "assistant")`,
+ "m.Content = cleanToolCallArtifacts(m.Content)",
+ `m.ReasoningContent = ""`,
+ "return m",
+ } {
+ if !strings.Contains(typesText, required) {
+ t.Fatalf("chat message client-safe projection must strip hidden reasoning and raw tool-call prose: missing %q", required)
+ }
+ }
+}
+
func TestContract_InvestigateAlertIsApprovalBound(t *testing.T) {
source, err := os.ReadFile(filepath.Clean("ai_handlers.go"))
if err != nil {