Show Claude Code session status in the GNOME panel
Answers one question at a glance: is any session waiting for me, and
which one. With several sessions open the cost is not knowing what each
is doing, it is noticing that one stopped an hour ago.
Claude Code hooks write one JSON file per session under
~/.local/state/claude-code-status; the extension watches the directory
with Gio.FileMonitor, so nothing polls and there is no daemon.
Two distinctions carry the design:
* blocked (permission prompt) is kept apart from waiting (turn done).
Merged, a finished task looks as urgent as a stuck one, which is
exactly the judgement the indicator exists to make.
* the panel names the oldest session in the top state, not the latest.
The session you forget is the one that has been waiting longest.
PostToolUse is registered although it looks redundant: it is the only
event that fires after a permission is granted, so without it a session
stays blocked in the panel for the rest of the turn. It writes only on
an actual state change, so the usual case costs no I/O.
Stop and SessionEnd are synchronous, unlike the rest. Both fire as the
process is about to go quiet, and an async hook racing that exit gets
killed before it writes -- claude -p left a session pinned at busy.
Concurrent hooks for one session serialise on an flock plus a timestamp
guard; tests/test-hook.sh covers each separately, because the burst test
passes on the timestamp guard alone.
Sessions running in zellij are located by tab name rather than by path,
matched through dump-layout on the working directory. The dump carries
no pane ids, so ZELLIJ_PANE_ID cannot be used; rows that do not resolve
stay inert instead of pretending a click does something.
lib/sessions.js deliberately imports nothing from the shell resource
namespace, which lets the riskiest logic -- liveness, ordering, partial
reads, monitoring -- run under plain gjs in tests/test-sessions.js.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/gjs -m
|
||||
// Exercises SessionStore against real files, under real GJS/GIO.
|
||||
//
|
||||
// sessions.js deliberately imports nothing from resource:///org/gnome/shell,
|
||||
// which is what makes this runnable outside the compositor -- the part of the
|
||||
// extension most likely to be wrong (liveness, ordering, monitoring, partial
|
||||
// reads) is also the part that can be tested for real.
|
||||
//
|
||||
// Run: gjs -m tests/test-sessions.js
|
||||
|
||||
import GLib from 'gi://GLib';
|
||||
import Gio from 'gi://Gio';
|
||||
|
||||
import { SessionStore, stateRank } from '../lib/sessions.js';
|
||||
|
||||
const DIR = GLib.build_filenamev([GLib.get_tmp_dir(), `ccs-test-${GLib.random_int()}`]);
|
||||
GLib.setenv('XDG_STATE_HOME', DIR, true);
|
||||
const STATE = GLib.build_filenamev([DIR, 'claude-code-status']);
|
||||
GLib.mkdir_with_parents(STATE, 0o755);
|
||||
|
||||
let failures = 0;
|
||||
function check(name, condition, detail = '') {
|
||||
const mark = condition ? 'ok ' : 'FAIL';
|
||||
if (!condition)
|
||||
failures++;
|
||||
print(`${mark} ${name}${detail ? ` (${detail})` : ''}`);
|
||||
}
|
||||
|
||||
const now = GLib.get_real_time() / 1e6;
|
||||
function write(id, state, cwd, agoSeconds, pid) {
|
||||
const payload = {
|
||||
session_id: id, state, cwd, since: now - agoSeconds,
|
||||
event_ts: now, pid, event: 'test', notification_type: '',
|
||||
message: '', zellij_session: 'ztest', zellij_pane: '1', transcript: '',
|
||||
};
|
||||
GLib.file_set_contents(
|
||||
GLib.build_filenamev([STATE, `${id}.json`]), JSON.stringify(payload));
|
||||
}
|
||||
|
||||
// A process that exists for the duration of the test, and one that does not.
|
||||
const livePid = new TextDecoder().decode(
|
||||
GLib.file_get_contents('/proc/self/stat')[1]).split(' ')[0];
|
||||
const deadPid = 4194303; // above the default pid_max, so it cannot exist
|
||||
|
||||
write('s-busy', 'busy', '/home/u/proj-busy', 30, livePid);
|
||||
write('s-blocked', 'blocked', '/home/u/proj-blocked', 10, livePid);
|
||||
write('s-wait-new', 'waiting', '/home/u/proj-new', 60, livePid);
|
||||
write('s-wait-old', 'waiting', '/home/u/proj-old', 3600, livePid);
|
||||
write('s-dead', 'waiting', '/home/u/proj-dead', 5, deadPid);
|
||||
write('s-idle', 'idle', '/home/u/proj-idle', 5, livePid);
|
||||
GLib.file_set_contents(GLib.build_filenamev([STATE, 'notes.txt']), 'ignored');
|
||||
GLib.file_set_contents(GLib.build_filenamev([STATE, 'half.json']), '{"broken');
|
||||
|
||||
const loop = new GLib.MainLoop(null, false);
|
||||
const store = new SessionStore();
|
||||
let round = 0;
|
||||
|
||||
store.connect('changed', () => {
|
||||
round++;
|
||||
const s = store.sessions;
|
||||
|
||||
if (round === 1) {
|
||||
check('ranks order blocked before waiting before busy',
|
||||
stateRank('blocked') < stateRank('waiting') &&
|
||||
stateRank('waiting') < stateRank('busy'));
|
||||
|
||||
check('dead session dropped', !s.some(x => x.sessionId === 's-dead'));
|
||||
check('truncated file skipped, others survive', s.length === 5,
|
||||
`got ${s.length}: ${s.map(x => x.sessionId).join(',')}`);
|
||||
check('non-json ignored', !s.some(x => x.sessionId.includes('notes')));
|
||||
|
||||
check('blocked sorts first', s[0]?.sessionId === 's-blocked', s[0]?.sessionId);
|
||||
check('longest wait precedes newer wait',
|
||||
s[1]?.sessionId === 's-wait-old' && s[2]?.sessionId === 's-wait-new',
|
||||
`${s[1]?.sessionId}, ${s[2]?.sessionId}`);
|
||||
check('busy after waiting', s[3]?.sessionId === 's-busy', s[3]?.sessionId);
|
||||
check('idle last', s[4]?.sessionId === 's-idle', s[4]?.sessionId);
|
||||
|
||||
check('dead session file removed from disk',
|
||||
!GLib.file_test(GLib.build_filenamev([STATE, 's-dead.json']), GLib.FileTest.EXISTS));
|
||||
|
||||
// A hook writing a new session must reach the panel without polling.
|
||||
write('s-fresh', 'blocked', '/home/u/proj-fresh', 1, livePid);
|
||||
return;
|
||||
}
|
||||
|
||||
if (s.some(x => x.sessionId === 's-fresh')) {
|
||||
check('directory monitor picked up a new session', true);
|
||||
check('new blocked session takes the top slot',
|
||||
s[0].state === 'blocked', s[0].sessionId);
|
||||
store.destroy();
|
||||
loop.quit();
|
||||
}
|
||||
});
|
||||
|
||||
store.start();
|
||||
|
||||
GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 10, () => {
|
||||
check('finished before timeout', false, 'monitor never fired');
|
||||
loop.quit();
|
||||
return GLib.SOURCE_REMOVE;
|
||||
});
|
||||
|
||||
loop.run();
|
||||
|
||||
Gio.File.new_for_path(DIR).trash_async?.(GLib.PRIORITY_LOW, null, null);
|
||||
GLib.spawn_command_line_sync(`rm -rf ${DIR}`);
|
||||
|
||||
print(failures ? `\n${failures} failure(s)` : '\nall passed');
|
||||
imports.system.exit(failures ? 1 : 0);
|
||||
Reference in New Issue
Block a user