|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +import { resendAgentActivity } from '../src/agentActivityResend.js'; |
| 4 | +import { AgentStateStore } from '../src/agentStateStore.js'; |
| 5 | +import type { AgentState } from '../src/types.js'; |
| 6 | + |
| 7 | +function createTestAgent(overrides: Partial<AgentState> = {}): AgentState { |
| 8 | + return { |
| 9 | + id: 0, |
| 10 | + sessionId: 'test-session', |
| 11 | + isExternal: false, |
| 12 | + projectDir: '/test', |
| 13 | + jsonlFile: '/test/session.jsonl', |
| 14 | + fileOffset: 0, |
| 15 | + lineBuffer: '', |
| 16 | + activeToolIds: new Set(), |
| 17 | + activeToolStatuses: new Map(), |
| 18 | + activeToolNames: new Map(), |
| 19 | + activeSubagentToolIds: new Map(), |
| 20 | + activeSubagentToolNames: new Map(), |
| 21 | + backgroundAgentToolIds: new Set(), |
| 22 | + isWaiting: false, |
| 23 | + permissionSent: false, |
| 24 | + hadToolsInTurn: false, |
| 25 | + lastDataAt: 0, |
| 26 | + linesProcessed: 0, |
| 27 | + seenUnknownRecordTypes: new Set(), |
| 28 | + hookDelivered: false, |
| 29 | + contextTokens: 0, |
| 30 | + maxContextTokens: 200_000, |
| 31 | + ...overrides, |
| 32 | + } as AgentState; |
| 33 | +} |
| 34 | + |
| 35 | +describe('resendAgentActivity', () => { |
| 36 | + it('sends messages in order: team info, tools, waiting, context', () => { |
| 37 | + const store = new AgentStateStore(); |
| 38 | + store.set( |
| 39 | + 1, |
| 40 | + createTestAgent({ |
| 41 | + id: 1, |
| 42 | + teamName: 'test-team', |
| 43 | + activeToolStatuses: new Map([['tool-1', 'Running']]), |
| 44 | + activeToolNames: new Map([['tool-1', 'Bash']]), |
| 45 | + isWaiting: true, |
| 46 | + contextTokens: 50_000, |
| 47 | + }), |
| 48 | + ); |
| 49 | + const sent: Array<Record<string, unknown>> = []; |
| 50 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 51 | + |
| 52 | + expect(sent.map((m) => m.type)).toEqual([ |
| 53 | + 'agentTeamInfo', |
| 54 | + 'agentToolStart', |
| 55 | + 'agentStatus', |
| 56 | + 'agentContextUsage', |
| 57 | + ]); |
| 58 | + }); |
| 59 | + |
| 60 | + it('includes basic fields for regular tools', () => { |
| 61 | + const store = new AgentStateStore(); |
| 62 | + store.set( |
| 63 | + 1, |
| 64 | + createTestAgent({ |
| 65 | + id: 1, |
| 66 | + activeToolStatuses: new Map([['tool-1', 'Running']]), |
| 67 | + activeToolNames: new Map([['tool-1', 'Bash']]), |
| 68 | + }), |
| 69 | + ); |
| 70 | + const sent: Array<Record<string, unknown>> = []; |
| 71 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 72 | + |
| 73 | + expect(sent).toHaveLength(1); |
| 74 | + expect(sent[0]).toEqual({ |
| 75 | + type: 'agentToolStart', |
| 76 | + id: 1, |
| 77 | + toolId: 'tool-1', |
| 78 | + status: 'Running', |
| 79 | + toolName: 'Bash', |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it('handles background tools with flags and promoted skip logic', () => { |
| 84 | + const store = new AgentStateStore(); |
| 85 | + // Lead with three background tools: unnamed, named spawn, and promoted |
| 86 | + store.set( |
| 87 | + 1, |
| 88 | + createTestAgent({ |
| 89 | + id: 1, |
| 90 | + // A teamed lead: the webview routes agentToolStart by the parent's |
| 91 | + // teamName, so the team message has to land before the tool replays or |
| 92 | + // a background spawn is routed as if the lead had no team. |
| 93 | + teamName: 'test-team', |
| 94 | + activeToolStatuses: new Map([ |
| 95 | + ['bg-unnamed', 'Subtask: Unnamed'], |
| 96 | + ['bg-named', 'Subtask: Named'], |
| 97 | + ['bg-promoted', 'Subtask: Promoted'], |
| 98 | + ]), |
| 99 | + activeToolNames: new Map([ |
| 100 | + ['bg-unnamed', 'Agent'], |
| 101 | + ['bg-named', 'Agent'], |
| 102 | + ['bg-promoted', 'Agent'], |
| 103 | + ]), |
| 104 | + backgroundAgentToolIds: new Set(['bg-unnamed', 'bg-named', 'bg-promoted']), |
| 105 | + teammateSpawnToolIds: new Set(['bg-named']), |
| 106 | + }), |
| 107 | + ); |
| 108 | + // Promoted teammate (should cause bg-promoted to be skipped) |
| 109 | + store.set( |
| 110 | + 2, |
| 111 | + createTestAgent({ |
| 112 | + id: 2, |
| 113 | + leadAgentId: 1, |
| 114 | + spawnToolUseId: 'bg-promoted', |
| 115 | + agentName: 'promoted-agent', |
| 116 | + }), |
| 117 | + ); |
| 118 | + const sent: Array<Record<string, unknown>> = []; |
| 119 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 120 | + |
| 121 | + const toolStarts = sent.filter((m) => m.type === 'agentToolStart'); |
| 122 | + expect(toolStarts).toHaveLength(2); // unnamed + named, NOT promoted |
| 123 | + |
| 124 | + // Team info first: the webview reads the parent's teamName to decide whether |
| 125 | + // a background agentToolStart becomes a Subtask sub-character, so a replay |
| 126 | + // that arrives before it is routed against stale team state. |
| 127 | + const teamIdx = sent.findIndex((m) => m.type === 'agentTeamInfo' && m.id === 1); |
| 128 | + const firstBgToolIdx = sent.findIndex((m) => m.type === 'agentToolStart' && m.id === 1); |
| 129 | + expect(teamIdx).toBeGreaterThanOrEqual(0); |
| 130 | + expect(teamIdx).toBeLessThan(firstBgToolIdx); |
| 131 | + |
| 132 | + // Unnamed: runInBackground=true, no isTeammateSpawn. toolName is required — |
| 133 | + // without it the webview cannot recreate the Subtask after agentToolsClear. |
| 134 | + const unnamed = toolStarts.find((t) => t.toolId === 'bg-unnamed'); |
| 135 | + expect(unnamed).toMatchObject({ runInBackground: true, toolName: 'Agent' }); |
| 136 | + expect(unnamed?.isTeammateSpawn).toBeUndefined(); |
| 137 | + |
| 138 | + // Named: runInBackground=true, isTeammateSpawn=true, toolName present. |
| 139 | + const named = toolStarts.find((t) => t.toolId === 'bg-named'); |
| 140 | + expect(named).toMatchObject({ |
| 141 | + runInBackground: true, |
| 142 | + isTeammateSpawn: true, |
| 143 | + toolName: 'Agent', |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + it('sends team info for any team field trigger, omits when none present', () => { |
| 148 | + // teamName trigger |
| 149 | + let store = new AgentStateStore(); |
| 150 | + store.set(1, createTestAgent({ id: 1, teamName: 'my-team' })); |
| 151 | + let sent: Array<Record<string, unknown>> = []; |
| 152 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 153 | + expect(sent).toHaveLength(1); |
| 154 | + expect(sent[0]).toMatchObject({ type: 'agentTeamInfo', id: 1, teamName: 'my-team' }); |
| 155 | + |
| 156 | + // agentName trigger (derived team) |
| 157 | + store = new AgentStateStore(); |
| 158 | + store.set(2, createTestAgent({ id: 2, agentName: 'worker-1', leadAgentId: 1 })); |
| 159 | + sent = []; |
| 160 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 161 | + expect(sent).toHaveLength(1); |
| 162 | + expect(sent[0]).toMatchObject({ type: 'agentTeamInfo', id: 2, agentName: 'worker-1' }); |
| 163 | + |
| 164 | + // isTeamLead trigger |
| 165 | + store = new AgentStateStore(); |
| 166 | + store.set(3, createTestAgent({ id: 3, isTeamLead: true })); |
| 167 | + sent = []; |
| 168 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 169 | + expect(sent).toHaveLength(1); |
| 170 | + expect(sent[0]).toMatchObject({ type: 'agentTeamInfo', id: 3, isTeamLead: true }); |
| 171 | + |
| 172 | + // No team fields: no message |
| 173 | + store = new AgentStateStore(); |
| 174 | + store.set(4, createTestAgent({ id: 4 })); |
| 175 | + sent = []; |
| 176 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 177 | + expect(sent.filter((m) => m.type === 'agentTeamInfo')).toHaveLength(0); |
| 178 | + }); |
| 179 | + |
| 180 | + it('sends waiting status only when agent is waiting', () => { |
| 181 | + let store = new AgentStateStore(); |
| 182 | + store.set(1, createTestAgent({ id: 1, isWaiting: true })); |
| 183 | + let sent: Array<Record<string, unknown>> = []; |
| 184 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 185 | + expect(sent).toEqual([{ type: 'agentStatus', id: 1, status: 'waiting' }]); |
| 186 | + |
| 187 | + store = new AgentStateStore(); |
| 188 | + store.set(2, createTestAgent({ id: 2, isWaiting: false })); |
| 189 | + sent = []; |
| 190 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 191 | + expect(sent).toHaveLength(0); |
| 192 | + }); |
| 193 | + |
| 194 | + it('sends context usage only when agent has tokens', () => { |
| 195 | + let store = new AgentStateStore(); |
| 196 | + store.set(1, createTestAgent({ id: 1, contextTokens: 50_000, maxContextTokens: 200_000 })); |
| 197 | + let sent: Array<Record<string, unknown>> = []; |
| 198 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 199 | + expect(sent).toEqual([ |
| 200 | + { type: 'agentContextUsage', id: 1, contextTokens: 50_000, maxContextTokens: 200_000 }, |
| 201 | + ]); |
| 202 | + |
| 203 | + store = new AgentStateStore(); |
| 204 | + store.set(2, createTestAgent({ id: 2, contextTokens: 0, maxContextTokens: 200_000 })); |
| 205 | + sent = []; |
| 206 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 207 | + expect(sent).toHaveLength(0); |
| 208 | + }); |
| 209 | + |
| 210 | + it('handles edge cases: empty store and multiple agents', () => { |
| 211 | + // Empty store |
| 212 | + let store = new AgentStateStore(); |
| 213 | + let sent: Array<Record<string, unknown>> = []; |
| 214 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 215 | + expect(sent).toHaveLength(0); |
| 216 | + |
| 217 | + // Multiple agents with different activity |
| 218 | + store = new AgentStateStore(); |
| 219 | + store.set( |
| 220 | + 1, |
| 221 | + createTestAgent({ |
| 222 | + id: 1, |
| 223 | + activeToolStatuses: new Map([['tool-1', 'Running']]), |
| 224 | + activeToolNames: new Map([['tool-1', 'Bash']]), |
| 225 | + }), |
| 226 | + ); |
| 227 | + store.set(2, createTestAgent({ id: 2, isWaiting: true })); |
| 228 | + sent = []; |
| 229 | + resendAgentActivity((msg) => sent.push(msg), store); |
| 230 | + expect(sent).toHaveLength(2); |
| 231 | + expect(sent[0]).toMatchObject({ type: 'agentToolStart', id: 1 }); |
| 232 | + expect(sent[1]).toMatchObject({ type: 'agentStatus', id: 2, status: 'waiting' }); |
| 233 | + }); |
| 234 | +}); |
0 commit comments