fix(cli): stop peer pings from waking or broadcasting to unrelated sessions

ping_peer resumed any inactive target by default, and list_peers returned
the whole namespace (including offline sessions) with no machine or cwd, so
agents re-woke finished collaborators and broadcast exclusive-work notices
to every listed peer.

- ping_peer refuses inactive targets unless wake=true (CLI --wake, exit 5);
  spawn_peer keeps waking its own fresh child.
- list_peers lists active sessions only (hub GET /api/sessions?active=true)
  unless includeInactive (CLI --list --all); rows show busy/idle, machine,
  cwd, and same-machine/same-cwd marks relative to the caller.
- inspect_peer reports the peer's machine.
- Prompts and tool descriptions: message only task-related peers, inspect
  unfamiliar peers first, never broadcast, never wake for FYI/status.
This commit is contained in:
weishu
2026-11-05 11:28:40 +08:00
parent 7688a4f13a
commit 5a5731f938
14 changed files with 387 additions and 57 deletions
@@ -295,6 +295,7 @@ describe('formatInspectPeerReport', () => {
thinking: false,
flavor: 'cursor',
path: '/tmp/x',
machine: 'studio',
lifecycleState: null,
updatedAt: 1_700_000_000_000,
messages: [
+76 -11
View File
@@ -195,6 +195,7 @@ describe('pingPeer', () => {
sessionIdPrefix: 'aaaaaaaa',
message: 'wake up',
deliveryMode,
wake: true,
accessToken: 'tok',
apiUrl: 'http://hub.test',
waitActiveSecs: 10,
@@ -265,6 +266,7 @@ describe('pingPeer', () => {
const result = await pingPeer({
sessionIdPrefix: 'bbbbbbbb',
message: 'still here?',
wake: true,
accessToken: 'tok',
apiUrl: 'http://hub.test',
waitActiveSecs: 10,
@@ -505,15 +507,44 @@ describe('pingPeer', () => {
await expect(pingPeer({
sessionIdPrefix: 'deadbeef',
message: 'nudge',
wake: true,
accessToken: 'tok',
apiUrl: 'http://hub.test',
http: http as never
})).rejects.toMatchObject({ code: 'resume_failed' })
})
it.each([
['list snapshot inactive', false],
['stale active list snapshot', true]
] as const)('refuses an inactive target without wake (%s) and never resumes or sends', async (_label, listedActive) => {
const sessionId = 'cafecafe-1111-1111-1111-111111111111'
const http = createHttpMock({
post: (url) => {
if (url.endsWith('/api/auth')) {
return { status: 200, data: { token: 'jwt' } }
}
throw new Error(`unexpected POST ${url}`)
},
get: (url) => url.endsWith('/api/sessions')
? { status: 200, data: { sessions: [{ id: sessionId, active: listedActive, metadata: { name: 'Old peer' } }] } }
: { status: 200, data: { session: { id: sessionId, active: false, metadata: { name: 'Old peer' } } } }
})
await expect(pingPeer({
sessionIdPrefix: 'cafecafe',
message: 'fyi: done',
accessToken: 'tok',
apiUrl: 'http://hub.test',
http: http as never
})).rejects.toMatchObject({ code: 'inactive', message: expect.stringContaining('no message sent') })
expect(http.post.mock.calls.map(([url]) => url)).toEqual(['http://hub.test/api/auth'])
})
it('maps exit codes', () => {
expect(exitCodeForPingPeerError(new PingPeerError('bad_args', 'x'))).toBe(2)
expect(exitCodeForPingPeerError(new PingPeerError('resume_failed', 'x'))).toBe(3)
expect(exitCodeForPingPeerError(new PingPeerError('inactive', 'x'))).toBe(5)
expect(exitCodeForPingPeerError(new PingPeerError('timeout', 'x'))).toBe(4)
expect(exitCodeForPingPeerError(new PingPeerError('send_failed', 'x'))).toBe(4)
})
@@ -637,7 +668,7 @@ describe('pingPeer delivery modes', () => {
}
})
const result = await pingPeer({
sessionIdPrefix: sessionId, message: 'update', deliveryMode: 'auto',
sessionIdPrefix: sessionId, message: 'update', deliveryMode: 'auto', wake: true,
apiUrl: 'http://hub.test', accessToken: 'tok', http: http as never
})
expect(result.resumed).toBe(true)
@@ -792,16 +823,36 @@ describe('formatPeerSessionsList', () => {
{
id: 'bbbbbbbb-2222-2222-2222-222222222222',
active: true,
thinking: true,
updatedAt: 200,
metadata: { name: 'Fresh', flavor: 'cursor' }
machineLabel: 'studio',
metadata: { name: 'Fresh', flavor: 'cursor', path: '/repo/app', machineId: 'm-1' }
}
])
], { includeInactive: true })
const lines = text.split('\n')
expect(lines[0]).toContain('bbbbbbbb-2222-2222-2222-222222222222')
expect(lines[0]).toContain('active=true')
expect(lines[0]).toContain('busy')
expect(lines[0]).toContain('flavor=cursor')
expect(lines[0]).toContain('machine=studio')
expect(lines[0]).toContain('cwd=/repo/app')
expect(lines[0]).toContain('Fresh')
expect(lines[1]).toContain('aaaaaaaa-1111-1111-1111-111111111111')
expect(lines[1]).toContain('inactive')
expect(lines.at(-1)).toMatch(/not a recipient list/)
})
it('marks peers sharing the caller machine and cwd', async () => {
const { formatPeerSessionsList } = await import('./pingPeer')
const selfId = 'cccccccc-3333-3333-3333-333333333333'
const text = formatPeerSessionsList([
{ id: selfId, active: true, updatedAt: 3, metadata: { machineId: 'm-1', path: '/repo' } },
{ id: 'aaaaaaaa-1111-1111-1111-111111111111', active: true, updatedAt: 2, metadata: { name: 'Same', machineId: 'm-1', path: '/repo' } },
{ id: 'bbbbbbbb-2222-2222-2222-222222222222', active: true, updatedAt: 1, metadata: { name: 'Elsewhere', machineId: 'm-2', path: '/repo' } }
], { excludeSessionId: selfId })
const lines = text.split('\n')
expect(lines[0]).toContain('[same-machine, same-cwd]')
expect(lines[1]).not.toContain('same-')
expect(text).not.toContain(selfId)
})
it('emits full session ids so colliding 8-char prefixes stay resolvable', async () => {
@@ -822,7 +873,7 @@ describe('formatPeerSessionsList', () => {
])
expect(text).toContain('aaaaaaaa-1111-1111-1111-111111111111')
expect(text).toContain('aaaaaaaa-2222-2222-2222-222222222222')
expect(text.split('\n')).toHaveLength(2)
expect(text.split('\n')).toHaveLength(3)
})
it('uses summary.text when name is unset and collapses multiline titles', async () => {
@@ -840,7 +891,7 @@ describe('formatPeerSessionsList', () => {
expect(resolvePeerSessionLabel(session)).toBe('Peer with tabs')
const text = formatPeerSessionsList([session])
expect(text).toContain('Peer with tabs')
expect(text.split('\n')).toHaveLength(1)
expect(text.split('\n')).toHaveLength(2)
expect(text).not.toContain('(unnamed)')
})
@@ -865,7 +916,8 @@ describe('formatPeerSessionsList', () => {
it('respects maxRows and empty list', async () => {
const { formatPeerSessionsList } = await import('./pingPeer')
expect(formatPeerSessionsList([])).toContain('No peer sessions')
expect(formatPeerSessionsList([])).toContain('No active peer sessions')
expect(formatPeerSessionsList([], { includeInactive: true })).toContain('No peer sessions')
const many = Array.from({ length: 5 }, (_, i) => ({
id: `${String(i).padStart(8, '0')}-0000-0000-0000-000000000000`,
active: true,
@@ -873,7 +925,7 @@ describe('formatPeerSessionsList', () => {
metadata: { name: `S${i}`, flavor: 'pi' as string | null }
}))
const text = formatPeerSessionsList(many, { maxRows: 2 })
expect(text.split('\n').filter((l) => l.includes('active='))).toHaveLength(2)
expect(text.split('\n').filter((l) => l.includes('flavor='))).toHaveLength(2)
})
it('omits the calling session even when it is newest', async () => {
@@ -993,19 +1045,32 @@ describe('listSessions query params', () => {
listParams.push(config?.params)
return {
status: 200,
data: { sessions: [{ id: sessionId, active: true, updatedAt: 1, metadata: { name: 'Z' } }] }
data: { sessions: [{ id: sessionId, active: true, updatedAt: 1, metadata: { name: 'Z', machineId: 'machine-1234' } }] }
}
}
if (url.endsWith('/api/machines')) {
return { status: 200, data: { machines: [{ id: 'machine-1234', metadata: { host: 'studio.local' } }] } }
}
throw new Error(`unexpected GET ${url}`)
}
})
await listPeerSessions({
const listed = await listPeerSessions({
apiUrl: 'http://hub.test',
accessToken: 'tok',
http: listHttp as never,
limit: 40
})
expect(listParams[0]).toEqual({ limit: 40, order: 'updatedAt' })
expect(listParams[0]).toEqual({ limit: 40, order: 'updatedAt', active: 'true' })
expect(listed[0]?.machineLabel).toBe('studio.local')
await listPeerSessions({
apiUrl: 'http://hub.test',
accessToken: 'tok',
http: listHttp as never,
limit: 40,
includeInactive: true
})
expect(listParams[1]).toEqual({ limit: 40, order: 'updatedAt' })
const pingHttp = createHttpMock({
post: (url, body) => {
+122 -15
View File
@@ -1,5 +1,5 @@
/**
* Resume-if-inactive + wait-active + POST /api/sessions/:id/messages,
* Opt-in resume (wake) + wait-active + POST /api/sessions/:id/messages,
* plus read-only inspectPeer (GET session + messages, never resume)
* and setPeerPermissionMode (POST /api/sessions/:id/permission-mode).
*
@@ -31,6 +31,7 @@ export type PingPeerErrorCode =
| 'auth_failed'
| 'not_found'
| 'ambiguous'
| 'inactive'
| 'resume_failed'
| 'timeout'
| 'send_failed'
@@ -70,6 +71,7 @@ export type PingPeerSessionSummary = {
name?: string
flavor?: string | null
path?: string | null
machineId?: string | null
lifecycleState?: string | null
piSessionId?: string
cursorSessionId?: string | null
@@ -85,6 +87,11 @@ export type PingPeerOptions = {
sessionId?: string
message: string
deliveryMode?: PeerDeliveryMode
/**
* Resume an inactive target before sending. Default false: inactive
* targets are refused so stale/unrelated peers are not relaunched.
*/
wake?: boolean
waitActiveSecs?: number
/**
* Fresh spawn: poll until active instead of POST /resume on the first
@@ -132,6 +139,13 @@ export type ListPeerSessionsOptions = {
limit?: number
/** Hub sort before limit truncation. Peer discovery defaults to newest updatedAt. */
order?: 'updatedAt'
/** Include inactive sessions (default false: hub filters to active before limit). */
includeInactive?: boolean
}
/** Peer list row: hub session summary plus the resolved machine label. */
export type PeerSessionListEntry = PingPeerSessionSummary & {
machineLabel?: string
}
const DEFAULT_WAIT_ACTIVE_SECS = 60
@@ -253,7 +267,7 @@ async function listSessions(
apiUrl: string,
jwt: string,
http: AxiosInstance,
options: { limit?: number; order?: 'updatedAt' } = {}
options: { limit?: number; order?: 'updatedAt'; activeOnly?: boolean } = {}
): Promise<PingPeerSessionSummary[]> {
const params: Record<string, string | number> = {}
if (options.limit !== undefined) {
@@ -262,6 +276,9 @@ async function listSessions(
if (options.order !== undefined) {
params.order = options.order
}
if (options.activeOnly) {
params.active = 'true'
}
const response = await http.get(
`${apiUrl}/api/sessions`,
{
@@ -467,25 +484,81 @@ async function sendMessage(
throw new PingPeerError('send_failed', `send failed: ${detail}`)
}
/**
* Best-effort machineId → hostname map from GET /api/machines (online
* machines only). Labels are decoration; failures leave rows unlabeled.
*/
async function fetchMachineLabels(
apiUrl: string,
jwt: string,
http: AxiosInstance
): Promise<Map<string, string>> {
const labels = new Map<string, string>()
try {
const response = await http.get(`${apiUrl}/api/machines`, {
headers: authHeaders(jwt),
timeout: 10_000,
validateStatus: () => true
})
if (response.status < 200 || response.status >= 300) {
return labels
}
const machines = Array.isArray(response.data?.machines) ? response.data.machines : []
for (const machine of machines) {
if (!isObject(machine) || typeof machine.id !== 'string') continue
const metadata = isObject(machine.metadata) ? machine.metadata : null
const label = typeof metadata?.displayName === 'string' && metadata.displayName.trim()
? metadata.displayName.trim()
: typeof metadata?.host === 'string' && metadata.host.trim()
? metadata.host.trim()
: null
if (label) {
labels.set(machine.id, label)
}
}
} catch {
// Unlabeled rows still carry the machine id prefix.
}
return labels
}
function resolveMachineLabel(machineId: string | null | undefined, labels: Map<string, string>): string | undefined {
if (!machineId) return undefined
return labels.get(machineId) ?? machineId.slice(0, 8)
}
export async function listPeerSessions(
options: ListPeerSessionsOptions = {}
): Promise<PingPeerSessionSummary[]> {
): Promise<PeerSessionListEntry[]> {
assertPeerToolsEnabled()
const apiUrl = resolveApiUrl(options.apiUrl)
const accessToken = resolveAccessToken(options.accessToken)
const http = options.http ?? axios
const jwt = await exchangeJwt(apiUrl, accessToken, http)
return listSessions(apiUrl, jwt, http, {
limit: options.limit ?? 200,
order: options.order ?? 'updatedAt'
})
const [sessions, machineLabels] = await Promise.all([
listSessions(apiUrl, jwt, http, {
limit: options.limit ?? 200,
order: options.order ?? 'updatedAt',
activeOnly: !options.includeInactive
}),
fetchMachineLabels(apiUrl, jwt, http)
])
return sessions.map((session) => ({
...session,
machineLabel: resolveMachineLabel(session.metadata?.machineId, machineLabels)
}))
}
export type FormatPeerSessionsListOptions = {
/** Max rows to print (default 30). */
maxRows?: number
/** Omit this session id (the caller) from the shortlist. */
/**
* Omit this session id (the caller) from the shortlist. When the caller
* row is present, peers on its machine / cwd are marked for conflict triage.
*/
excludeSessionId?: string
/** The list was fetched with inactive sessions included. */
includeInactive?: boolean
/**
* When the fetch was intentionally bounded, signal overflow without claiming
* an exact omitted count from the sample.
@@ -525,27 +598,43 @@ export function resolvePeerSessionLabel(session: PingPeerSessionSummary): string
: collapsed
}
const PEER_LIST_FOOTER =
'Directory, not a recipient list: message only peers tied to your task; ' +
'inspect_peer an unfamiliar peer before messaging it, and never broadcast.'
/**
* Human/agent-readable shortlist for MCP `list_peers` and `hapi ping-peer --list`.
* Newest `updatedAt` first. Same hub/namespace as the caller credentials.
*/
export function formatPeerSessionsList(
sessions: PingPeerSessionSummary[],
sessions: PeerSessionListEntry[],
options: FormatPeerSessionsListOptions = {}
): string {
const maxRows = options.maxRows ?? 30
const excludeId = options.excludeSessionId?.trim() ?? ''
const caller = excludeId ? sessions.find((session) => session.id === excludeId) : undefined
const filtered = excludeId
? sessions.filter((session) => session.id !== excludeId)
: sessions
if (filtered.length === 0) {
return 'No peer sessions found on this hub/namespace.'
return options.includeInactive
? 'No peer sessions found on this hub/namespace.'
: 'No active peer sessions found on this hub/namespace.'
}
const callerMachineId = caller?.metadata?.machineId ?? null
const callerPath = caller?.metadata?.path ?? null
const sorted = [...filtered].sort((a, b) => (b.updatedAt ?? 0) - (a.updatedAt ?? 0))
const rows = sorted.slice(0, Math.max(1, maxRows)).map((session) => {
const flavor = session.metadata?.flavor ?? '?'
const meta = session.metadata
const flavor = meta?.flavor ?? '?'
const name = resolvePeerSessionLabel(session)
return ` ${session.id} active=${session.active} flavor=${flavor} ${name}`
const state = session.active ? (session.thinking ? 'busy' : 'idle') : 'inactive'
const sameMachine = Boolean(callerMachineId) && meta?.machineId === callerMachineId
const machine = session.machineLabel ?? meta?.machineId?.slice(0, 8) ?? '?'
const sameCwd = sameMachine && Boolean(callerPath) && meta?.path === callerPath
const marks = [sameMachine ? 'same-machine' : null, sameCwd ? 'same-cwd' : null].filter(Boolean)
return ` ${session.id} ${state} flavor=${flavor} machine=${machine} cwd=${meta?.path ?? '?'}`
+ `${marks.length > 0 ? ` [${marks.join(', ')}]` : ''} ${name}`
})
const omitted = sorted.length - rows.length
if (options.hasMore) {
@@ -553,12 +642,13 @@ export function formatPeerSessionsList(
} else if (omitted > 0) {
rows.push(` … ${omitted} more (narrow with inspect_peer / ping_peer by id)`)
}
rows.push(PEER_LIST_FOOTER)
return rows.join('\n')
}
function getSteeringUnavailableReason(session: PingPeerSessionSummary): string | null {
if (!session.active) {
return 'Steering requires an active turn; target is inactive, no message sent. Use queue to resume it'
return 'Steering requires an active turn; target is inactive, no message sent. Use queue with wake to resume it'
}
if (!isSteeringSupportedForSession(session.metadata)) {
return 'Steering is only supported for Claude, Pi, Codex, and Cursor ACP sessions; no message sent'
@@ -618,7 +708,15 @@ export async function pingPeer(options: PingPeerOptions): Promise<PingPeerResult
return session
}
if (requestedDeliveryMode === 'steer') {
throw new PingPeerError('bad_args', 'Steering requires an active turn; target is inactive, no message sent. Use queue to resume it')
throw new PingPeerError('bad_args', 'Steering requires an active turn; target is inactive, no message sent. Use queue with wake to resume it')
}
if (!options.wake) {
throw new PingPeerError(
'inactive',
`target ${matched.id} is inactive; no message sent. `
+ 'Do not wake peers for FYI, status, or done notes. '
+ 'Pass wake=true (CLI: --wake) only when the user asked or the handed-off work requires this peer to act.'
)
}
onProgress?.(progressMessage)
await resumeSession(apiUrl, jwt, matched.id, http)
@@ -685,6 +783,8 @@ export function exitCodeForPingPeerError(error: PingPeerError): number {
return 2
case 'resume_failed':
return 3
case 'inactive':
return 5
case 'timeout':
case 'send_failed':
case 'apply_failed':
@@ -719,6 +819,8 @@ export type InspectPeerResult = {
thinking: boolean
flavor: string | null
path: string | null
/** Hostname (or machine id prefix) the peer runs on. */
machine: string | null
lifecycleState: string | null
updatedAt: number | null
messages: InspectPeerMessage[]
@@ -830,7 +932,10 @@ export async function inspectPeer(options: InspectPeerOptions): Promise<InspectP
const matched = resolveSessionByPrefix(sessions, prefix)
const live = await getSession(apiUrl, jwt, matched.id, http)
const meta = live.metadata ?? matched.metadata ?? null
const messages = await fetchSessionMessages(apiUrl, jwt, matched.id, messageLimit, http)
const [messages, machineLabels] = await Promise.all([
fetchSessionMessages(apiUrl, jwt, matched.id, messageLimit, http),
fetchMachineLabels(apiUrl, jwt, http)
])
return {
sessionId: matched.id,
@@ -839,6 +944,7 @@ export async function inspectPeer(options: InspectPeerOptions): Promise<InspectP
thinking: Boolean(live.thinking),
flavor: typeof meta?.flavor === 'string' ? meta.flavor : null,
path: typeof meta?.path === 'string' ? meta.path : null,
machine: resolveMachineLabel(meta?.machineId, machineLabels) ?? null,
lifecycleState: typeof meta?.lifecycleState === 'string' ? meta.lifecycleState : null,
updatedAt: typeof live.updatedAt === 'number'
? live.updatedAt
@@ -859,6 +965,7 @@ export function formatInspectPeerReport(result: InspectPeerResult): string {
`active: ${result.active}`,
`thinking: ${result.thinking}`,
`lifecycle: ${result.lifecycleState ?? '(none)'}`,
`machine: ${result.machine ?? '(unknown)'}`,
`cwd: ${result.path ?? '(unknown)'}`,
`updatedAt: ${result.updatedAt ?? '(unknown)'}`,
`messages (text snippets, newest page): ${result.messages.length}`
+5 -1
View File
@@ -681,6 +681,8 @@ export async function spawnPeer(options: SpawnPeerOptions): Promise<SpawnPeerRes
message,
waitActiveSecs,
waitForInitialActive: true,
// The remit targets a session this call just created.
wake: true,
apiUrl,
accessToken,
http,
@@ -734,7 +736,9 @@ export async function spawnPeer(options: SpawnPeerOptions): Promise<SpawnPeerRes
throw new SpawnPeerError(deliveryError.code, `${deliveryError.message}; ${leaveRunning}`)
}
if (deliveryError instanceof PingPeerError) {
throw new SpawnPeerError(deliveryError.code, `${deliveryError.message}; ${leaveRunning}`)
// Remit delivery passes wake, so 'inactive' only means the child never stayed up.
const code = deliveryError.code === 'inactive' ? 'send_failed' : deliveryError.code
throw new SpawnPeerError(code, `${deliveryError.message}; ${leaveRunning}`)
}
throw new SpawnPeerError(
'send_failed',