mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-26 05:27:06 +00:00
* fix(web): drop "No response requested." assistant messages When Claude Code injects system messages (task notifications, system reminders) as user turns, Claude responds with "No response requested." In the HAPI web UI this appears as a reply to the user's message, making it look like Claude is ignoring their input. Filter these out in isSkippableAgentContent() (catches the fallback path in normalize.ts) and in normalizeAssistantOutput() (catches the primary path). Both checks verify the assistant message contains only the text "No response requested." with no tool calls. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix(web): filter text block instead of dropping entire message Address bot review: dropping the whole normalized record breaks sidechain UUID threading (parentUUID chain orphans). Instead of returning null, suppress only the "No response requested." text block during content extraction. The message record (uuid, parentUUID, usage) is preserved so the tracer's sidechain grouping continues to work. Also remove the isSkippableAgentContent check since we no longer need to drop the message at that layer. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix(web): move "No response requested." filter to reducer layer Address bot review: filtering in normalizeAssistantOutput() produced empty content arrays, breaking traceMessages() which reads uuid and parentUUID from content[0]. Sidechain child messages whose parentUUID pointed to the filtered message became orphaned. Fix: revert the normalizer to always emit the text block (preserving the UUID chain for the tracer), and filter the sentinel text in reducerTimeline.ts where text blocks become visible AgentTextBlocks. At this point tracing is already complete. Also adds reducer-level tests for the filter and updates the normalize test to verify the text block is preserved. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix(web): scope sentinel filter to single-block assistant messages only Address bot review: the previous filter suppressed any text block matching "No response requested.", which could hide legitimate replies. Now the filter only triggers when the message has exactly one content block (msg.content.length === 1) — i.e., the assistant response is purely the sentinel text with no tool calls or reasoning blocks. This prevents false positives while still catching the system-injection auto-reply case. Add test for the multi-block case (text + tool call) to verify the sentinel text is preserved when other content exists. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix(web): add parentUUID structural check to sentinel filter Address bot review: raw text match alone could theoretically suppress a legitimate reply. Add c.parentUUID !== null as a structural guard: - Sentinel auto-replies always follow a prior assistant turn, so their parentUUID is set (pointing to the previous message in the chain). - A first message in a conversation has parentUUID: null and will never be filtered. Combined conditions: msg.content.length === 1 (sole block, no tool calls) AND c.parentUUID !== null (not the first reply) AND exact text match. Add tests for the parentUUID=null escape hatch. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix(web): use injected-turn UUID tracking for sentinel filter Address review: use structural markers instead of broad text matching. 1. Pre-scan collects UUIDs from sidechain content blocks (system- injected user turns). The sentinel filter now only triggers when parentUUID points to one of these known injected turns. 2. Move task-notification event extraction from normalizer to reducer. Previously, task-notifications with summary were normalized as role:'event', losing their uuid. Now they stay as sidechain (uuid preserved for pre-scan), and the reducer extracts the summary as an agent-event block. 3. Remove redundant 'uuid' in c guard (always present on sidechain type). False positive analysis: a legitimate reply is only suppressed when ALL of: (a) sole content block, (b) parentUUID matches a sidechain-injected turn, (c) exact sentinel text. This combination cannot occur for real user-facing content. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix(web): add parentUUID to sidechain content type for tracer linkage The sidechain content block was missing parentUUID, so traceMessages() could not chain system-injected user turns (task notifications, system reminders) inside a Task sidechain back to their parent. This caused later sidechain messages pointing to the injected turn's UUID to become orphaned and disappear from the Task card. Add parentUUID to the sidechain type definition and propagate it from normalizeUserOutput() in both the isSidechain and non-sidechain paths. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * fix(web): handle array-content sidechain user messages to prevent prompt leak Sidechain user messages can arrive with either string content or array content ([{type:'text', text:'...'}]) depending on how Claude Code serialises them. The previous fix only handled the string case, causing intermittent prompt leaks when array format was used. Now normalizeUserOutput extracts text from array-content sidechain messages and emits them as sidechain blocks, so the tracer can match them to their parent Task tool call. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> * test(web): verify parentUUID propagation from assistant output data Add integration tests confirming normalizeAssistantOutput correctly maps data.parentUuid to text block parentUUID (used by the reducer's sentinel detection). Tests cover both present and absent parentUuid. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> --------- Co-authored-by: HAPI <noreply@hapi.run>
299 lines
13 KiB
TypeScript
299 lines
13 KiB
TypeScript
import type { ChatBlock, ToolCallBlock, ToolPermission } from '@/chat/types'
|
|
import type { TracedMessage } from '@/chat/tracer'
|
|
import { createCliOutputBlock, isCliOutputText, mergeCliOutputBlocks } from '@/chat/reducerCliOutput'
|
|
import { parseMessageAsEvent } from '@/chat/reducerEvents'
|
|
import { ensureToolBlock, extractTitleFromChangeTitleInput, isChangeTitleToolName, type PermissionEntry } from '@/chat/reducerTools'
|
|
|
|
export function reduceTimeline(
|
|
messages: TracedMessage[],
|
|
context: {
|
|
permissionsById: Map<string, PermissionEntry>
|
|
groups: Map<string, TracedMessage[]>
|
|
consumedGroupIds: Set<string>
|
|
titleChangesByToolUseId: Map<string, string>
|
|
emittedTitleChangeToolUseIds: Set<string>
|
|
}
|
|
): { blocks: ChatBlock[]; toolBlocksById: Map<string, ToolCallBlock>; hasReadyEvent: boolean } {
|
|
const blocks: ChatBlock[] = []
|
|
const toolBlocksById = new Map<string, ToolCallBlock>()
|
|
let hasReadyEvent = false
|
|
|
|
// Pre-scan: collect UUIDs of system-injected user turns (sidechain
|
|
// prompts, task notifications, system reminders). These are used below
|
|
// to identify sentinel auto-replies ("No response requested.") whose
|
|
// parentUUID points to one of these injected messages.
|
|
const injectedTurnUuids = new Set<string>()
|
|
for (const msg of messages) {
|
|
if (msg.role !== 'agent' || !msg.isSidechain) continue
|
|
for (const c of msg.content) {
|
|
if (c.type === 'sidechain') {
|
|
injectedTurnUuids.add(c.uuid)
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const msg of messages) {
|
|
if (msg.role === 'event') {
|
|
if (msg.content.type === 'ready') {
|
|
hasReadyEvent = true
|
|
continue
|
|
}
|
|
blocks.push({
|
|
kind: 'agent-event',
|
|
id: msg.id,
|
|
createdAt: msg.createdAt,
|
|
event: msg.content,
|
|
meta: msg.meta
|
|
})
|
|
continue
|
|
}
|
|
|
|
const event = parseMessageAsEvent(msg)
|
|
if (event) {
|
|
blocks.push({
|
|
kind: 'agent-event',
|
|
id: msg.id,
|
|
createdAt: msg.createdAt,
|
|
event,
|
|
meta: msg.meta
|
|
})
|
|
continue
|
|
}
|
|
|
|
if (msg.role === 'user') {
|
|
if (isCliOutputText(msg.content.text, msg.meta)) {
|
|
blocks.push(createCliOutputBlock({
|
|
id: msg.id,
|
|
localId: msg.localId,
|
|
createdAt: msg.createdAt,
|
|
text: msg.content.text,
|
|
source: 'user',
|
|
meta: msg.meta
|
|
}))
|
|
continue
|
|
}
|
|
blocks.push({
|
|
kind: 'user-text',
|
|
id: msg.id,
|
|
localId: msg.localId,
|
|
createdAt: msg.createdAt,
|
|
text: msg.content.text,
|
|
attachments: msg.content.attachments,
|
|
status: msg.status,
|
|
originalText: msg.originalText,
|
|
meta: msg.meta
|
|
})
|
|
continue
|
|
}
|
|
|
|
if (msg.role === 'agent') {
|
|
// When the message contains a Task tool_use, Claude often writes the
|
|
// prompt as a text block before the tool_use block. We only want to
|
|
// suppress that exact prompt text — not every text block in the message.
|
|
const taskToolCall = msg.content.find(
|
|
(c) => c.type === 'tool-call' && c.name === 'Task'
|
|
)
|
|
const taskPromptText: string | null = (() => {
|
|
if (!taskToolCall || taskToolCall.type !== 'tool-call') return null
|
|
const input = taskToolCall.input
|
|
if (typeof input === 'object' && input !== null && 'prompt' in input) {
|
|
const p = (input as { prompt: unknown }).prompt
|
|
if (typeof p === 'string') return p
|
|
}
|
|
return null
|
|
})()
|
|
|
|
for (let idx = 0; idx < msg.content.length; idx += 1) {
|
|
const c = msg.content[idx]
|
|
if (c.type === 'text') {
|
|
// Skip "No response requested." — Claude's sentinel auto-response
|
|
// to system-injected messages (task notifications, system reminders).
|
|
//
|
|
// Structural checks to avoid false positives:
|
|
// 1. msg.content.length === 1 — no tool calls or reasoning alongside
|
|
// 2. c.parentUUID points to a known injected turn UUID (collected
|
|
// in pre-scan from sidechain content blocks)
|
|
// 3. Exact text match on the known sentinel phrase
|
|
if (
|
|
msg.content.length === 1 &&
|
|
c.parentUUID !== null &&
|
|
injectedTurnUuids.has(c.parentUUID)
|
|
) {
|
|
const trimmedText = c.text.trim()
|
|
if (trimmedText === 'No response requested.' || trimmedText === 'No response requested') {
|
|
continue
|
|
}
|
|
}
|
|
|
|
// Skip text blocks that are just the Task tool prompt (already shown in tool card)
|
|
if (taskPromptText && c.text.trim() === taskPromptText.trim()) continue
|
|
|
|
if (isCliOutputText(c.text, msg.meta)) {
|
|
blocks.push(createCliOutputBlock({
|
|
id: `${msg.id}:${idx}`,
|
|
localId: msg.localId,
|
|
createdAt: msg.createdAt,
|
|
text: c.text,
|
|
source: 'assistant',
|
|
meta: msg.meta
|
|
}))
|
|
continue
|
|
}
|
|
blocks.push({
|
|
kind: 'agent-text',
|
|
id: `${msg.id}:${idx}`,
|
|
localId: msg.localId,
|
|
createdAt: msg.createdAt,
|
|
text: c.text,
|
|
meta: msg.meta
|
|
})
|
|
continue
|
|
}
|
|
|
|
if (c.type === 'reasoning') {
|
|
blocks.push({
|
|
kind: 'agent-reasoning',
|
|
id: `${msg.id}:${idx}`,
|
|
localId: msg.localId,
|
|
createdAt: msg.createdAt,
|
|
text: c.text,
|
|
meta: msg.meta
|
|
})
|
|
continue
|
|
}
|
|
|
|
if (c.type === 'summary') {
|
|
blocks.push({
|
|
kind: 'agent-event',
|
|
id: `${msg.id}:${idx}`,
|
|
createdAt: msg.createdAt,
|
|
event: { type: 'message', message: c.summary },
|
|
meta: msg.meta
|
|
})
|
|
continue
|
|
}
|
|
|
|
if (c.type === 'tool-call') {
|
|
if (isChangeTitleToolName(c.name)) {
|
|
const title = context.titleChangesByToolUseId.get(c.id) ?? extractTitleFromChangeTitleInput(c.input)
|
|
if (title && !context.emittedTitleChangeToolUseIds.has(c.id)) {
|
|
context.emittedTitleChangeToolUseIds.add(c.id)
|
|
blocks.push({
|
|
kind: 'agent-event',
|
|
id: `${msg.id}:${idx}`,
|
|
createdAt: msg.createdAt,
|
|
event: { type: 'title-changed', title },
|
|
meta: msg.meta
|
|
})
|
|
}
|
|
continue
|
|
}
|
|
|
|
const permission = context.permissionsById.get(c.id)?.permission
|
|
|
|
const block = ensureToolBlock(blocks, toolBlocksById, c.id, {
|
|
createdAt: msg.createdAt,
|
|
localId: msg.localId,
|
|
meta: msg.meta,
|
|
name: c.name,
|
|
input: c.input,
|
|
description: c.description,
|
|
permission
|
|
})
|
|
|
|
if (block.tool.state === 'pending') {
|
|
block.tool.state = 'running'
|
|
block.tool.startedAt = msg.createdAt
|
|
}
|
|
|
|
if (c.name === 'Task' && !context.consumedGroupIds.has(msg.id)) {
|
|
const sidechain = context.groups.get(msg.id) ?? null
|
|
if (sidechain && sidechain.length > 0) {
|
|
context.consumedGroupIds.add(msg.id)
|
|
const child = reduceTimeline(sidechain, context)
|
|
hasReadyEvent = hasReadyEvent || child.hasReadyEvent
|
|
block.children = child.blocks
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
if (c.type === 'tool-result') {
|
|
const title = context.titleChangesByToolUseId.get(c.tool_use_id) ?? null
|
|
if (title) {
|
|
if (!context.emittedTitleChangeToolUseIds.has(c.tool_use_id)) {
|
|
context.emittedTitleChangeToolUseIds.add(c.tool_use_id)
|
|
blocks.push({
|
|
kind: 'agent-event',
|
|
id: `${msg.id}:${idx}`,
|
|
createdAt: msg.createdAt,
|
|
event: { type: 'title-changed', title },
|
|
meta: msg.meta
|
|
})
|
|
}
|
|
continue
|
|
}
|
|
|
|
const permissionEntry = context.permissionsById.get(c.tool_use_id)
|
|
const permissionFromResult = c.permissions ? ({
|
|
id: c.tool_use_id,
|
|
status: c.permissions.result === 'approved' ? 'approved' : 'denied',
|
|
date: c.permissions.date,
|
|
mode: c.permissions.mode,
|
|
allowedTools: c.permissions.allowedTools,
|
|
decision: c.permissions.decision
|
|
} satisfies ToolPermission) : undefined
|
|
|
|
const permission = (() => {
|
|
if (permissionFromResult && permissionEntry?.permission) {
|
|
return {
|
|
...permissionEntry.permission,
|
|
...permissionFromResult,
|
|
allowedTools: permissionFromResult.allowedTools ?? permissionEntry.permission.allowedTools,
|
|
decision: permissionFromResult.decision ?? permissionEntry.permission.decision
|
|
} satisfies ToolPermission
|
|
}
|
|
return permissionFromResult ?? permissionEntry?.permission
|
|
})()
|
|
|
|
const block = ensureToolBlock(blocks, toolBlocksById, c.tool_use_id, {
|
|
createdAt: msg.createdAt,
|
|
localId: msg.localId,
|
|
meta: msg.meta,
|
|
name: permissionEntry?.toolName ?? 'Tool',
|
|
input: permissionEntry?.input ?? null,
|
|
description: null,
|
|
permission
|
|
})
|
|
|
|
block.tool.result = c.content
|
|
block.tool.completedAt = msg.createdAt
|
|
block.tool.state = c.is_error ? 'error' : 'completed'
|
|
continue
|
|
}
|
|
|
|
if (c.type === 'sidechain') {
|
|
// Extract task-notification summaries as visible events
|
|
const trimmedPrompt = c.prompt.trimStart()
|
|
if (trimmedPrompt.startsWith('<task-notification>')) {
|
|
const summary = trimmedPrompt.match(/<summary>([\s\S]*?)<\/summary>/)?.[1]?.trim()
|
|
if (summary) {
|
|
blocks.push({
|
|
kind: 'agent-event',
|
|
id: `${msg.id}:${idx}`,
|
|
createdAt: msg.createdAt,
|
|
event: { type: 'message', message: summary },
|
|
meta: msg.meta
|
|
})
|
|
}
|
|
}
|
|
// Skip rendering prompt text (already in parent Task tool card or not user-visible)
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return { blocks: mergeCliOutputBlocks(blocks), toolBlocksById, hasReadyEvent }
|
|
}
|