perf(agent-status): validate hook payloads without the JSON round trip (#10752)
This commit is contained in:
+147
-174
@@ -20,7 +20,6 @@ import { isAbsolute, join } from 'node:path'
|
||||
import {
|
||||
AGENT_MODEL_MAX_LENGTH,
|
||||
normalizeAgentStatusPayload,
|
||||
parseAgentStatusPayload,
|
||||
type AgentStatusState,
|
||||
type AgentSubagentSnapshot,
|
||||
type ParsedAgentStatusPayload
|
||||
@@ -2684,20 +2683,18 @@ function normalizeDevinEvent(
|
||||
const interrupted =
|
||||
eventName === 'Stop' && hookPayload['is_interrupt'] === true ? true : undefined
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('devin', eventName)
|
||||
}),
|
||||
agentType: 'devin',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('devin', eventName)
|
||||
}),
|
||||
agentType: 'devin',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
}
|
||||
|
||||
// Why: Kimi's auto-allowed AskUserQuestion emits PreToolUse (not PermissionRequest) while awaiting an answer; treat as waiting so the UI shows the attention icon, not a spinner.
|
||||
@@ -2744,19 +2741,17 @@ function normalizeKimiEvent(
|
||||
const interrupted =
|
||||
eventName === 'Stop' && hookPayload['is_interrupt'] === true ? true : undefined
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('kimi', eventName)
|
||||
}),
|
||||
agentType: 'kimi',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('kimi', eventName)
|
||||
}),
|
||||
agentType: 'kimi',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeGeminiEvent(
|
||||
@@ -2789,19 +2784,17 @@ function normalizeGeminiEvent(
|
||||
{ resetOnNewTurn: isNewTurnEvent('gemini', eventName) }
|
||||
)
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('gemini', eventName)
|
||||
}),
|
||||
agentType: 'gemini',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('gemini', eventName)
|
||||
}),
|
||||
agentType: 'gemini',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function isAntigravityFeedbackTool(toolName: string | undefined): boolean {
|
||||
@@ -2863,19 +2856,17 @@ function normalizeAntigravityEvent(
|
||||
{ resetOnNewTurn: resetsTurn }
|
||||
)
|
||||
|
||||
const payload = parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: resetsTurn
|
||||
}),
|
||||
agentType: 'antigravity',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
const payload = normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: resetsTurn
|
||||
}),
|
||||
agentType: 'antigravity',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
// Why: Antigravity can emit Stop with fullyIdle=false between tool steps; only a fully idle Stop is terminal, else the sidebar bounces done -> working and ignores later tool updates.
|
||||
if (eventName === 'Stop' && !stopStillBusy && transcriptPath) {
|
||||
state.antigravityCompletedTranscriptByPaneKey.set(paneKey, transcriptPath)
|
||||
@@ -2941,21 +2932,19 @@ function normalizeAmpEvent(
|
||||
(eventName === 'agent.end' && !state.lastPromptByPaneKey.has(ampCacheKey))
|
||||
const ampPromptText = explicitPrompt ?? (canUseMessageAsPrompt ? promptText : '')
|
||||
|
||||
const normalized = parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
// Why: Amp tool/result events may use `message` for tool output; only lifecycle events may treat it as the turn prompt.
|
||||
prompt: resolvePrompt(state, ampCacheKey, ampPromptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('amp', eventName)
|
||||
}),
|
||||
agentType: 'amp',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
)
|
||||
const normalized = normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
// Why: Amp tool/result events may use `message` for tool output; only lifecycle events may treat it as the turn prompt.
|
||||
prompt: resolvePrompt(state, ampCacheKey, ampPromptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('amp', eventName)
|
||||
}),
|
||||
agentType: 'amp',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
if (normalized && eventName === 'agent.end') {
|
||||
state.ampCompletedCacheKeys.add(ampCacheKey)
|
||||
}
|
||||
@@ -3335,19 +3324,17 @@ function normalizeOpenCodeFamilyEvent(
|
||||
{ resetOnNewTurn: isNewTurnEvent(source, eventName) }
|
||||
)
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent(source, eventName)
|
||||
}),
|
||||
agentType: source,
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent(source, eventName)
|
||||
}),
|
||||
agentType: source,
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeCursorEvent(
|
||||
@@ -3395,20 +3382,18 @@ function normalizeCursorEvent(
|
||||
? true
|
||||
: undefined
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('cursor', eventName)
|
||||
}),
|
||||
agentType: 'cursor',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('cursor', eventName)
|
||||
}),
|
||||
agentType: 'cursor',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage,
|
||||
interrupted
|
||||
})
|
||||
}
|
||||
|
||||
// Why: Copilot PermissionRequest fires before allow/ask/deny (stays working); ask_user and notification prompts are the real blocked signals.
|
||||
@@ -3458,19 +3443,17 @@ function normalizeCopilotEvent(
|
||||
|
||||
const effectivePrompt = normalizedEventName === 'Notification' ? '' : promptText
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: isNewTurnEvent('copilot', normalizedEventName)
|
||||
}),
|
||||
agentType: 'copilot',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: isNewTurnEvent('copilot', normalizedEventName)
|
||||
}),
|
||||
agentType: 'copilot',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function normalizePiCompatibleEvent(
|
||||
@@ -3517,19 +3500,17 @@ function normalizePiCompatibleEvent(
|
||||
{ resetOnNewTurn: isNewTurnEvent(agentType, eventName) }
|
||||
)
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent(agentType, eventName)
|
||||
}),
|
||||
agentType,
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent(agentType, eventName)
|
||||
}),
|
||||
agentType,
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeDroidEvent(
|
||||
@@ -3584,19 +3565,17 @@ function normalizeDroidEvent(
|
||||
// Why: Droid Notification.message is status text, not the prompt; '' keeps resolvePrompt's cached UserPromptSubmit value.
|
||||
const effectivePrompt = eventName === 'Notification' ? '' : promptText
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: isNewTurnEvent('droid', eventName)
|
||||
}),
|
||||
agentType: 'droid',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: isNewTurnEvent('droid', eventName)
|
||||
}),
|
||||
agentType: 'droid',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeCommandCodeEvent(
|
||||
@@ -3623,19 +3602,17 @@ function normalizeCommandCodeEvent(
|
||||
{ resetOnNewTurn: isNewTurnEvent('command-code', eventName) }
|
||||
)
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('command-code', eventName)
|
||||
}),
|
||||
agentType: 'command-code',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('command-code', eventName)
|
||||
}),
|
||||
agentType: 'command-code',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeGrokEvent(
|
||||
@@ -3709,19 +3686,17 @@ function normalizeGrokEvent(
|
||||
? ''
|
||||
: stripGrokUserQueryWrapper(promptText)
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: isNewTurnEvent('grok', eventName)
|
||||
}),
|
||||
agentType: 'grok',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, effectivePrompt, {
|
||||
resetOnNewTurn: isNewTurnEvent('grok', eventName)
|
||||
}),
|
||||
agentType: 'grok',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function normalizeHermesEvent(
|
||||
@@ -3758,19 +3733,17 @@ function normalizeHermesEvent(
|
||||
{ resetOnNewTurn: isNewTurnEvent('hermes', eventName) }
|
||||
)
|
||||
|
||||
return parseAgentStatusPayload(
|
||||
JSON.stringify({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('hermes', eventName)
|
||||
}),
|
||||
agentType: 'hermes',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
)
|
||||
return normalizeAgentStatusPayload({
|
||||
state: stateName,
|
||||
prompt: resolvePrompt(state, paneKey, promptText, {
|
||||
resetOnNewTurn: isNewTurnEvent('hermes', eventName)
|
||||
}),
|
||||
agentType: 'hermes',
|
||||
toolName: snapshot.toolName,
|
||||
toolInput: snapshot.toolInput,
|
||||
interactivePrompt: snapshot.interactivePrompt,
|
||||
lastAssistantMessage: snapshot.lastAssistantMessage
|
||||
})
|
||||
}
|
||||
|
||||
function readStringField(record: Record<string, unknown>, key: string): string | undefined {
|
||||
|
||||
@@ -509,3 +509,77 @@ describe('agentSubagentsEqual', () => {
|
||||
expect(agentSubagentsEqual([snapshot], [snapshot, { ...snapshot, id: 'b' }])).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
// Why: the per-source hook normalizers construct these literals and validate them
|
||||
// directly. This pins that the direct path stays identical to the JSON round trip
|
||||
// they used to take, including where stringify would have altered the payload.
|
||||
describe('normalizeAgentStatusPayload matches the JSON round trip', () => {
|
||||
const CASES: Record<string, unknown>[] = [
|
||||
{ state: 'working', prompt: 'p', agentType: 'grok', toolName: 'sh', toolInput: 'ls' },
|
||||
{ state: 'done', prompt: '', agentType: 'devin', interrupted: true },
|
||||
// stringify DROPS undefined-valued keys; the direct path passes them through
|
||||
{
|
||||
state: 'working',
|
||||
prompt: 'p',
|
||||
agentType: 'cursor',
|
||||
toolName: undefined,
|
||||
toolInput: undefined,
|
||||
interactivePrompt: undefined,
|
||||
lastAssistantMessage: undefined,
|
||||
interrupted: undefined
|
||||
},
|
||||
// raw JSON inside a field exercises the structure scanner's in-string path
|
||||
{
|
||||
state: 'working',
|
||||
prompt: 'p',
|
||||
agentType: 'copilot',
|
||||
interactivePrompt: JSON.stringify({ q: 'pick {one}', options: ['a', 'b'] })
|
||||
},
|
||||
{
|
||||
state: 'working',
|
||||
prompt: 'a\r\nb c',
|
||||
agentType: 'gemini',
|
||||
lastAssistantMessage: 'emoji \u{1f389} \u65e5\u672c\u8a9e\r\n\r\n\r\nmulti'
|
||||
},
|
||||
{ state: 'working', prompt: 'p', agentType: 'amp', lastAssistantMessage: 'x'.repeat(50_000) },
|
||||
{ state: 'done', prompt: 'p', agentType: 'hermes', toolName: '', toolInput: '' },
|
||||
{
|
||||
state: 'working',
|
||||
prompt: 'p',
|
||||
agentType: 'droid',
|
||||
toolInput: '{"nested":{"deep":{"deeper":[1,2,3]}}}'
|
||||
},
|
||||
{
|
||||
state: 'working',
|
||||
prompt: 'p',
|
||||
agentType: 'kimi',
|
||||
lastAssistantMessage: '"escaped" quotes and \\ backslashes'
|
||||
},
|
||||
{ state: 'working', prompt: 'p', agentType: 'opencode' },
|
||||
{ state: 'done', prompt: 'p', agentType: 'antigravity', interrupted: false },
|
||||
{ state: 'working', prompt: 'p', agentType: 'pi', toolName: 'x'.repeat(9000) },
|
||||
{ state: 'working', prompt: 'x'.repeat(9000), agentType: 'omp' },
|
||||
{
|
||||
state: 'working',
|
||||
prompt: 'p',
|
||||
agentType: 'command-code',
|
||||
lastAssistantMessage: 'tail with \u001b[0m escape codes'
|
||||
},
|
||||
// a lone surrogate is the case where stringify and a raw read could diverge
|
||||
{ state: 'working', prompt: 'p', agentType: 'grok', lastAssistantMessage: 'lone \ud800 pair' }
|
||||
]
|
||||
|
||||
it('produces identical output for every normalizer literal shape', () => {
|
||||
for (const [index, payload] of CASES.entries()) {
|
||||
expect({
|
||||
index,
|
||||
agent: payload.agentType,
|
||||
value: normalizeAgentStatusPayload(payload)
|
||||
}).toEqual({
|
||||
index,
|
||||
agent: payload.agentType,
|
||||
value: parseAgentStatusPayload(JSON.stringify(payload))
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user