|
4 | 4 | * |
5 | 5 | * States (priority order): |
6 | 6 | * Listening — SSE connected + last_activity is msg_wait (⏳) |
7 | | - * Working — SSE connected + NOT msg_wait + activity within 60s (⚡) |
8 | | - * Idle — SSE connected + NOT msg_wait + activity older than 60s (🔌) |
| 7 | + * Working — SSE connected + last_activity is msg_received/msg_post (⚡) |
| 8 | + * (no timeout — agent may be on a long task) |
| 9 | + * Idle — SSE connected + never entered message loop (🔌) |
9 | 10 | * (SSE open but agent stopped responding — may be processing |
10 | 11 | * a long task or is a stale connection) |
11 | 12 | * Offline — no SSE + heartbeat expired (⚫) |
12 | 13 | * |
13 | 14 | * For stdio agents (no SSE): fall back to heartbeat-based detection. |
14 | 15 | * stdio agents will show Listening when is_online=true, Offline otherwise. |
15 | 16 | */ |
16 | | - const WORKING_TIMEOUT_S = 60; |
17 | | - |
18 | 17 | function getAgentState(agent) { |
19 | 18 | if (!agent) return "Offline"; |
20 | 19 |
|
21 | 20 | if (agent.is_sse_connected) { |
22 | 21 | if (agent.last_activity === "msg_wait") return "Listening"; |
23 | | - // Check how long ago the last activity was |
24 | | - const lastActivityTime = agent.last_activity_time ? new Date(agent.last_activity_time) : null; |
25 | | - if (lastActivityTime) { |
26 | | - const elapsedS = (Date.now() - lastActivityTime.getTime()) / 1000; |
27 | | - if (elapsedS > WORKING_TIMEOUT_S) return "Idle"; |
| 22 | + // msg_received: msg_wait just delivered a message, agent is processing. |
| 23 | + // msg_post: agent just posted a reply, still in working cycle. |
| 24 | + // No timeout — agent may be doing a long task (editing code, reasoning, etc.) |
| 25 | + // SSE disconnect is the only reliable signal that work has truly stopped. |
| 26 | + if (agent.last_activity === "msg_received" || agent.last_activity === "msg_post") { |
| 27 | + return "Working"; |
28 | 28 | } |
29 | | - return "Working"; |
| 29 | + // Any other activity (registered, heartbeat, resume, etc.) with SSE open: |
| 30 | + // agent is connected but hasn't entered the message loop yet → Idle. |
| 31 | + return "Idle"; |
30 | 32 | } |
31 | 33 |
|
32 | 34 | // stdio or truly offline — rely on heartbeat |
|
0 commit comments