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
+54
-5
@@ -19,17 +19,20 @@ let running = false;
|
||||
let tickets: Map<string, string>;
|
||||
let addresses: string[];
|
||||
let throttled: boolean;
|
||||
/** How long the fake API takes to know a token. */
|
||||
let verifyMs: number;
|
||||
const open: WebSocket[] = [];
|
||||
|
||||
async function start(extra: Partial<Parameters<typeof startRelay>[0]> = {}): Promise<void> {
|
||||
if (running) await relay.close();
|
||||
relay = await startRelay({
|
||||
api: {
|
||||
verifyToken: (token: string, address: string): Promise<ApiAnswer> => {
|
||||
verifyToken: async (token: string, address: string): Promise<ApiAnswer> => {
|
||||
addresses.push(address);
|
||||
if (throttled) return Promise.resolve({ status: 429, retryAfter: '600', error: 'Too many' });
|
||||
if (verifyMs > 0) await new Promise((resolve) => setTimeout(resolve, verifyMs));
|
||||
if (throttled) return { status: 429, retryAfter: '600', error: 'Too many' };
|
||||
const user = TOKENS[token];
|
||||
return Promise.resolve(user ? { status: 200, user } : { status: 401 });
|
||||
return user ? { status: 200, user } : { status: 401 };
|
||||
},
|
||||
redeemTicket: (ticket: string): Promise<ApiAnswer> => {
|
||||
const user = tickets.get(ticket);
|
||||
@@ -48,6 +51,7 @@ beforeEach(async () => {
|
||||
tickets = new Map();
|
||||
addresses = [];
|
||||
throttled = false;
|
||||
verifyMs = 0;
|
||||
await start();
|
||||
});
|
||||
|
||||
@@ -414,14 +418,59 @@ describe('tabs', () => {
|
||||
|
||||
it('learn how many agents their account has on, and an agent gone quiet counts out', async () => {
|
||||
await start({ agentRecentMs: 150, pingMs: 50 });
|
||||
await initialize('fwa_alice');
|
||||
await call('fwa_alice', await initialize('fwa_alice'), 'get_view');
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
const tab = await onTab('alice');
|
||||
expect((await tab.next('agents')).count).toBe(0);
|
||||
await initialize('fwa_alice');
|
||||
await call('fwa_alice', await initialize('fwa_alice'), 'get_view');
|
||||
expect((await tab.next('agents')).count).toBe(1);
|
||||
expect((await tab.next('agents')).count).toBe(0);
|
||||
});
|
||||
|
||||
it('do not count the spare sessions a client opens and never calls a tool in', async () => {
|
||||
const tab = await onTab('alice');
|
||||
expect((await tab.next('agents')).count).toBe(0);
|
||||
await initialize('fwa_alice');
|
||||
await initialize('fwa_alice');
|
||||
await call('fwa_alice', await initialize('fwa_alice'), 'get_view');
|
||||
expect((await tab.next('agents')).count).toBe(1);
|
||||
expect(tab.received.filter((message) => message.type === 'agents')).toEqual([]);
|
||||
});
|
||||
|
||||
it('count out when the event stream of an agent drops, without a DELETE', async () => {
|
||||
await start({ agentRecentMs: 150, pingMs: 50 });
|
||||
const tab = await onTab('alice');
|
||||
expect((await tab.next('agents')).count).toBe(0);
|
||||
const session = await initialize('fwa_alice');
|
||||
const gone = new AbortController();
|
||||
const stream = await fetch(url(), {
|
||||
headers: { Authorization: 'Bearer fwa_alice', 'Mcp-Session-Id': session, Accept: 'text/event-stream' },
|
||||
signal: gone.signal,
|
||||
});
|
||||
expect(stream.status).toBe(200);
|
||||
expect((await tab.next('agents')).count).toBe(1);
|
||||
await new Promise((resolve) => setTimeout(resolve, 400));
|
||||
expect(tab.received.filter((message) => message.type === 'agents')).toEqual([]);
|
||||
gone.abort();
|
||||
expect((await tab.next('agents')).count).toBe(0);
|
||||
});
|
||||
|
||||
it('do not count an agent that drops its event stream while its token is still being checked', async () => {
|
||||
const tab = await onTab('alice');
|
||||
expect((await tab.next('agents')).count).toBe(0);
|
||||
const session = await initialize('fwa_alice');
|
||||
verifyMs = 100;
|
||||
const gone = new AbortController();
|
||||
const stream = fetch(url(), {
|
||||
headers: { Authorization: 'Bearer fwa_alice', 'Mcp-Session-Id': session, Accept: 'text/event-stream' },
|
||||
signal: gone.signal,
|
||||
}).catch(() => null);
|
||||
await new Promise((resolve) => setTimeout(resolve, 30));
|
||||
gone.abort();
|
||||
await stream;
|
||||
await new Promise((resolve) => setTimeout(resolve, 200));
|
||||
expect(tab.received.filter((message) => message.type === 'agents')).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('the view', () => {
|
||||
|
||||
Reference in new issue
Block a user