Files
orca/src/shared/agent-hook-relay.test.ts
Brennan BensonandNeil ec4687c434 feat(agents): distinguish Claude background monitoring (takes over #14205) (#16201)
* feat(agents): distinguish Claude background monitoring

Adds an optional `workingMode: 'monitoring'` discriminator for a Claude
session whose lead turn finished but which still has background shell tasks
or session crons registered. The wire state stays `working`, so older peers
that never read the field keep rendering Working.

(cherry picked from commit d5d54b4bdd)

Rebased onto current main (554 commits of drift) by Brennan Benson;
conflicts resolved by keeping both sides where main and this branch made
independent additions to the same construct.

* fix(sidebar): keep monitoring status visible

(cherry picked from commit fd6b38654d)

* test(agents): cover Claude monitoring drain

(cherry picked from commit ce4d61ebf8)

* test(mobile): avoid unresolved renderer test type

(cherry picked from commit bbcfa35ff9)

* feat(agents): render Claude monitoring as a static turquoise dot

Replaces the yellow Radio glyph from #14205 with a static dot in a new
--agent-monitoring token (#8abeb7), defined once for light and once for
dark like --workspace-status-done, so the status keeps its identity when
the theme flips. Deliberately a fixed UI value: it never reads terminal
theme state at runtime.

Adds the turn-boundary notification pins. The monitoring predicate and
the turnCompletedAt stamp are computed from the same "lead said done but
the pane resolves to working" expression, so a rename can silently drop
the stamp and kill a completion notification that works today with
nothing else going red.

* revert(agents): restore the yellow Radio glyph for monitoring

Brennan chose #14205's original treatment over the turquoise dot, so the visual
goes back to nwparker's: lucide Radio in text-yellow-500 across the sidebar,
dashboard dot, cmd-j palette and agent-map ring.

Reverts only the visual surface. The turn-boundary notification pins stay — the
monitoring predicate and the turnCompletedAt stamp share an expression, so a
rename can silently drop the stamp and kill a completion that works today with
nothing else going red. The --agent-monitoring token is removed with its last
consumer rather than left dead in main.css.

---------

Co-authored-by: Neil <4138956+nwparker@users.noreply.github.com>
2026-08-24 15:24:32 -07:00

150 lines
5.8 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
AGENT_HOOK_INSTALL_PLUGINS_METHOD,
AGENT_HOOK_NOTIFICATION_METHOD,
AGENT_HOOK_REQUEST_REPLAY_METHOD,
AGENT_HOOK_SHED_FIELDS_KEY,
ORCA_FEATURE_REMOTE_AGENT_HOOKS_ENV,
createShedSubagentsField,
isAgentHookSource,
isRemoteAgentHooksEnabled,
restoreShedStatusFields,
type AgentHookRelayEnvelope
} from './agent-hook-relay'
import type { ParsedAgentStatusPayload } from './agent-status-types'
describe('agent-hook-relay wire shape', () => {
it('encodes/decodes through JSON without losing fields', () => {
const envelope: AgentHookRelayEnvelope = {
source: 'claude',
paneKey: 'tab-1:0',
tabId: 'tab-1',
worktreeId: 'wt-1',
connectionId: null,
env: 'production',
version: '1',
providerPromptId: '11111111-1111-4111-8111-111111111111',
compactTrigger: 'manual',
payload: {
state: 'working',
workingMode: 'monitoring',
prompt: 'roundtrip',
agentType: 'claude'
}
}
const decoded = JSON.parse(JSON.stringify(envelope)) as AgentHookRelayEnvelope
expect(decoded).toEqual(envelope)
expect(decoded.connectionId).toBeNull()
expect(decoded.payload.prompt).toBe('roundtrip')
expect(decoded.payload.workingMode).toBe('monitoring')
})
it('exposes stable JSON-RPC method names', () => {
expect(AGENT_HOOK_NOTIFICATION_METHOD).toBe('agent.hook')
expect(AGENT_HOOK_REQUEST_REPLAY_METHOD).toBe('agent_hook.requestReplay')
expect(AGENT_HOOK_INSTALL_PLUGINS_METHOD).toBe('agent_hook.installPlugins')
})
it('validates hook sources crossing persisted and relay trust boundaries', () => {
expect(isAgentHookSource('claude')).toBe(true)
expect(isAgentHookSource('kimi')).toBe(true)
expect(isAgentHookSource('prime-agent')).toBe(true)
expect(isAgentHookSource('claude\0codex')).toBe(false)
expect(isAgentHookSource('unknown')).toBe(false)
expect(isAgentHookSource({ source: 'claude' })).toBe(false)
})
})
describe('isRemoteAgentHooksEnabled', () => {
it('is on when the env var is absent', () => {
expect(isRemoteAgentHooksEnabled({})).toBe(true)
})
it('is off for empty / "0"', () => {
expect(isRemoteAgentHooksEnabled({ [ORCA_FEATURE_REMOTE_AGENT_HOOKS_ENV]: '' })).toBe(false)
expect(isRemoteAgentHooksEnabled({ [ORCA_FEATURE_REMOTE_AGENT_HOOKS_ENV]: '0' })).toBe(false)
expect(isRemoteAgentHooksEnabled({ [ORCA_FEATURE_REMOTE_AGENT_HOOKS_ENV]: ' ' })).toBe(false)
})
it('is on for any other non-empty value', () => {
expect(isRemoteAgentHooksEnabled({ [ORCA_FEATURE_REMOTE_AGENT_HOOKS_ENV]: '1' })).toBe(true)
expect(isRemoteAgentHooksEnabled({ [ORCA_FEATURE_REMOTE_AGENT_HOOKS_ENV]: 'on' })).toBe(true)
expect(isRemoteAgentHooksEnabled({ [ORCA_FEATURE_REMOTE_AGENT_HOOKS_ENV]: 'true' })).toBe(true)
})
})
describe('restoreShedStatusFields', () => {
const roster = [{ id: 'child-1', agentType: 'reviewer', state: 'working' as const, startedAt: 1 }]
const cached: ParsedAgentStatusPayload = {
state: 'working',
prompt: 'p',
agentType: 'claude',
subagents: roster,
lastAssistantMessage: 'cached message',
interactivePrompt: '{"questions":["old"]}'
}
const shed: ParsedAgentStatusPayload = { state: 'done', prompt: 'p', agentType: 'claude' }
it('restores a matching shed roster, so a done pane is not falsely hibernation-eligible', () => {
const restored = restoreShedStatusFields(shed, [createShedSubagentsField(roster)], cached)
expect(restored.subagents).toEqual(roster)
expect(restored.state).toBe('done')
// Only the named field comes back.
expect(restored.lastAssistantMessage).toBeUndefined()
})
it('does not restore a shed roster across a prompt identity change', () => {
expect(AGENT_HOOK_SHED_FIELDS_KEY).toBe('shedFields')
const nextTurn = { ...shed, prompt: 'next prompt' }
const restored = restoreShedStatusFields(
nextTurn,
['lastAssistantMessage', createShedSubagentsField(roster)],
cached
)
expect(restored.lastAssistantMessage).toBeUndefined()
expect(restored.subagents).toBeUndefined()
expect(restored).toBe(nextTurn)
})
it('does not restore the cached roster when the shed roster digest changed', () => {
const changedRoster = [
{ id: 'child-2', agentType: 'reviewer', state: 'working' as const, startedAt: 2 }
]
const restored = restoreShedStatusFields(
shed,
[createShedSubagentsField(changedRoster)],
cached
)
expect(restored.subagents).toBeUndefined()
expect(restored).toBe(shed)
})
it('never restores interactivePrompt — a stale answerable card is worse than none', () => {
const restored = restoreShedStatusFields(shed, ['interactivePrompt'], cached)
expect(restored.interactivePrompt).toBeUndefined()
expect(restored).toBe(shed)
})
it('leaves an intentional lastAssistantMessage clear intact when the relay shed nothing', () => {
expect(restoreShedStatusFields(shed, undefined, cached)).toBe(shed)
expect(restoreShedStatusFields(shed, [], cached)).toBe(shed)
expect(restoreShedStatusFields(shed, [], cached).lastAssistantMessage).toBeUndefined()
// A hostile/garbled marker must not throw or restore.
expect(restoreShedStatusFields(shed, 'subagents', cached)).toBe(shed)
expect(restoreShedStatusFields(shed, ['subagents'], cached)).toBe(shed)
expect(restoreShedStatusFields(shed, [{ toString: () => 'subagents' }], cached)).toBe(shed)
})
it('does not overwrite a value the envelope still carries', () => {
const fresh: ParsedAgentStatusPayload = { ...shed, lastAssistantMessage: 'fresh' }
expect(
restoreShedStatusFields(fresh, ['lastAssistantMessage'], cached).lastAssistantMessage
).toBe('fresh')
})
it('is a no-op with no cached payload for the pane', () => {
expect(restoreShedStatusFields(shed, ['subagents'], undefined)).toBe(shed)
})
})