Count each connected agent once in the editor badge (#429)
The Agent badge showed 2 or 3 while one Claude Code session was connected. The relay log against the open TCP connections in the container showed two causes. - A client may drop its event stream while the relay still asks the API about its token. The close was over before the relay listened for it, so the stream stayed in the list and counted for good. The relay now listens before the token check and keeps no stream that is already gone. It listens on the response, not the request. - Claude Code opens three sessions whenever it connects and keeps a stream on one of them. It never ends the others with a DELETE. Any session counted for two minutes after its last request, so the badge stood at 3 after each start. Now a session counts while its stream is open or it called a tool in the last two minutes. - The relay logs session start, stream open and close and each change of the count, with session ids cut to 8 characters. One case is left: after a relay restart Claude Code sometimes repeats its first call on a spare session. The badge then shows 2 for two minutes and drops to 1 on its own. Tests: - relay.test.ts: 27/27, three new. A dropped stream counts out, a stream dropped during the token check never counts, and spare sessions without a tool call do not count. - Counter-checks: without the early close listener the drop test turns red, with the old count two tests turn red. - Live: the badge showed 2 with one open SSE connection before. After the fix it shows "Agent · 1" and stays there after a normal call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b9d1602b94
commit
0dd671ea26
2 files changed
+78
-12
No files matched your search
+24
-7
@@ -223,10 +223,11 @@ export async function startRelay(options) {
|
||||
subscriptions: new Set(),
|
||||
streams: new Set(),
|
||||
seenAt: Date.now(),
|
||||
calledAt: 0,
|
||||
lastSummary: null,
|
||||
};
|
||||
sessions.set(session.id, session);
|
||||
log(`agent session for ${user}`);
|
||||
log(`agent session ${session.id.slice(0, 8)} for ${user}`);
|
||||
tellAgents(key);
|
||||
return session;
|
||||
}
|
||||
@@ -297,6 +298,8 @@ export async function startRelay(options) {
|
||||
* see gets told so with the answer: not every client follows a subscription.
|
||||
*/
|
||||
async function callTool(session, name, args) {
|
||||
session.calledAt = Date.now();
|
||||
tellAgents(session.key);
|
||||
const tab = tabs.get(session.key);
|
||||
if (!tab) return { content: [{ type: 'text', text: NOT_CONNECTED }], isError: true };
|
||||
const seen = session.lastSummary;
|
||||
@@ -317,8 +320,12 @@ export async function startRelay(options) {
|
||||
if (!String(req.headers.accept ?? '').includes('text/event-stream')) {
|
||||
return reply(res, 405, { error: 'Ask for text/event-stream' }, { Allow: 'GET, POST, DELETE' });
|
||||
}
|
||||
// A client may drop the stream while its token is being checked: its close is over by the time
|
||||
// the stream would be kept, and a stream kept then would count an agent that is gone, for good.
|
||||
let dropped = false;
|
||||
res.once('close', () => (dropped = true));
|
||||
const user = await agent(req, res);
|
||||
if (user === null) return;
|
||||
if (user === null || dropped) return;
|
||||
const session = sessionOf(req, res, user);
|
||||
if (!session) return;
|
||||
res.writeHead(200, {
|
||||
@@ -329,9 +336,11 @@ export async function startRelay(options) {
|
||||
});
|
||||
res.write(': open\n\n');
|
||||
session.streams.add(res);
|
||||
log(`stream open on ${session.id.slice(0, 8)}`);
|
||||
tellAgents(session.key);
|
||||
req.on('close', () => {
|
||||
res.on('close', () => {
|
||||
session.streams.delete(res);
|
||||
log(`stream closed on ${session.id.slice(0, 8)}`);
|
||||
tellAgents(session.key);
|
||||
});
|
||||
}
|
||||
@@ -341,6 +350,7 @@ export async function startRelay(options) {
|
||||
if (user === null) return;
|
||||
const session = sessionOf(req, res, user);
|
||||
if (!session) return;
|
||||
log(`agent session ${session.id.slice(0, 8)} ended by the agent`);
|
||||
endSession(session);
|
||||
reply(res, 204);
|
||||
}
|
||||
@@ -360,18 +370,24 @@ export async function startRelay(options) {
|
||||
|
||||
/**
|
||||
* How many agents an account has on, for its tab to show: a session counts while its event stream
|
||||
* is open or it asked something lately. A client that went away without a DELETE leaves its
|
||||
* session behind, and so does a revoked token until it is used again.
|
||||
* is open or it called a tool lately. A client that went away without a DELETE leaves its
|
||||
* session behind, and so does a revoked token until it is used again. Claude Code opens two
|
||||
* spare sessions next to the one it keeps whenever it connects: they never call a tool.
|
||||
*/
|
||||
function tellAgents(key) {
|
||||
const tab = tabs.get(key);
|
||||
if (!tab) return;
|
||||
const now = Date.now();
|
||||
const count = sessionsOf(key).filter(
|
||||
(session) => session.streams.size > 0 || now - session.seenAt < agentRecentMs,
|
||||
(session) => session.streams.size > 0 || now - session.calledAt < agentRecentMs,
|
||||
).length;
|
||||
if (count === tab.agents) return;
|
||||
tab.agents = count;
|
||||
const seen = sessionsOf(key).map(
|
||||
(session) =>
|
||||
`${session.id.slice(0, 8)} streams ${session.streams.size} idle ${Math.round((now - session.seenAt) / 1000)}s`,
|
||||
);
|
||||
log(`agents for ${tab.user}: ${count} [${seen.join('; ')}]`);
|
||||
tab.conn.send(JSON.stringify({ type: 'agents', count }));
|
||||
}
|
||||
|
||||
@@ -617,5 +633,6 @@ function passOn(req, res, backend) {
|
||||
/**
|
||||
* @typedef {{status: number, user?: string, role?: string, error?: string, retryAfter?: string | null}} ApiAnswer
|
||||
* @typedef {{id: string, user: string, key: string, protocolVersion: string, subscriptions: Set<string>,
|
||||
* streams: Set<import('node:http').ServerResponse>, seenAt: number, lastSummary: string | null}} Session
|
||||
* streams: Set<import('node:http').ServerResponse>, seenAt: number, calledAt: number,
|
||||
* lastSummary: string | null}} Session
|
||||
*/
|
||||
Reference in new issue
Block a user