Files
claude-code-gnome-extension/lib/abbrev.js
T
av 2565d45bb5 Act on four reviews: state machine, resource bounds, teardown
Four agents reviewed this in parallel -- correctness, GNOME integration,
edge cases, security. Everything below was reproduced before being fixed;
several findings that survived the first reading did not survive a probe
and are not here.

State machine, the two that mattered most. A pending permission prompt was
erased by any subagent bookkeeping event: SubagentStop or the next
PreToolUse recomputed the state from scratch, so a session sat at "working"
with a dialog open and nothing ever raised it again. Blocked now outlives
everything except evidence the question was answered. Separately, the
stale-event guard refused whole events, including the subagent counter's
increments and decrements -- but those are deltas and deltas commute, so a
"+1" that lost a timestamp race left the count short and the batch freed
the session while a subagent was still running. The guard now gates the
state decision only.

Corrupt or hostile state files could wedge the panel or take the hook down
for every session: a non-numeric pid raised inside sweep_dead before the
hook wrote its own file, so one bad byte stopped new sessions appearing at
all. Numbers read back from disk are coerced, one unreadable file no longer
aborts the sweep, and a stored timestamp far in the future -- corruption, or
a clock stepped backwards by NTP -- no longer refuses every later event
forever.

Resource bounds, all in the compositor process. A state file was read whole
with no size check: a symlink to /dev/zero took a test process past 4 GB in
three seconds, which in gnome-shell ends the session. Sizes are checked
before the read, sessions and zellij subprocesses are capped, labels
ellipsize, and cwd and messages are truncated at the hook.

Teardown hung off an overridden destroy(), which only runs when JS calls
it. An actor destroyed any other way -- another extension rebuilding the
panel boxes -- left the timer and the file monitor running against a
disposed actor. It is a destroy signal now. The zellij child is killed
rather than merely abandoned.

The glyph was pinned to physical pixels and rendered half-size on HiDPI;
size comes from the stylesheet, and the foreground colour is normalised by
inspection rather than assuming which colour struct the shell hands back.

Chip labels: non-Latin names all collapsed to "?", because the split
treated every Cyrillic letter as a separator -- notable for a tool whose
own README is Russian. Seniority also ranked by time-in-state rather than
session age, so after a shell restart the older session could take the
digit; the hook now records when the session began.

zellij: a dump ends with new_tab_template and swap_tiled_layout blocks
whose tab lines carry no name, and their panes were being attached to the
last real tab -- which then answered for every unmatched directory,
confidently and wrongly.

install.py no longer widens the mode of a settings.json someone narrowed to
0600, no longer overwrites the pristine .bak on a second run, no longer
replaces a symlink out of a dotfiles repository with a regular file, and
quotes the hook path. The debug log is capped and README now says plainly
that it records prompts verbatim.

Not fixed, deliberately: the panel does push the clock about 70 px left
with three labelled chips, which is inherent to putting them in the centre
box; two different projects abbreviating alike still read as one project
with a digit; GNOME 48 remains unverified for the colour struct and for
St.BoxLayout's vertical property, both flagged rather than guessed at.
2026-08-09 20:20:45 +03:00

77 lines
2.8 KiB
JavaScript

// Three-character chip labels for the panel.
//
// A chip per session only pays off if the label stays put. Two rules do that:
// labels are assigned oldest-session-first, so a session starting now takes the
// suffixed variant rather than pushing one off the label already on screen; and
// a label, once given, belongs to that session until it ends -- even after the
// session it was disambiguated against has closed. A label that moves under
// your hand is worse than one carrying a digit that no longer looks necessary.
//
// Imports nothing, so it runs under plain node or gjs.
const MAX = 3;
/** Split on separators and camelCase humps: "pet-project-server", "outlineMcp". */
function segments(name) {
// Unicode-aware: splitting on [^a-zA-Z0-9] makes every Cyrillic letter a
// separator, so "проект" reduces to nothing and every non-Latin project
// ends up sharing the label "?".
return name
.replace(/(\p{Ll}|\p{N})(\p{Lu})/gu, '$1 $2')
.split(/[^\p{L}\p{N}]+/u)
.filter(Boolean);
}
/** Up to three characters for a project name.
*
* Initials for multi-segment names, first letters for single words. Initials
* matter more than they look: a plain prefix collapses "dev-skills" and
* "dev-conventions" onto the same "dev", which is the exact case this has to
* keep apart.
*/
export function abbreviate(name) {
const parts = segments(String(name ?? ''));
if (!parts.length)
return '?';
const raw = parts.length > 1
? parts.map(p => p[0]).join('')
: parts[0];
return raw.slice(0, MAX).toLowerCase();
}
/** Assign a label to every session, reusing the ones already handed out.
*
* `previous` is the mapping from the last run; pass the returned map back in.
* Sessions absent from `sessions` drop out, which frees their label for reuse.
*/
export function assignChips(sessions, previous = new Map()) {
const labels = new Map();
const taken = new Set();
for (const session of sessions) {
const kept = previous.get(session.sessionId);
if (kept !== undefined) {
labels.set(session.sessionId, kept);
taken.add(kept);
}
}
const fresh = sessions
.filter(s => !labels.has(s.sessionId))
.sort((a, b) => (a.since || 0) - (b.since || 0));
for (const session of fresh) {
const base = abbreviate(session.base);
let label = base;
// Digits eat into the base rather than extending past three characters,
// so every chip stays the same width and the row does not ripple.
for (let n = 2; taken.has(label); n++) {
const suffix = String(n);
label = base.slice(0, Math.max(1, MAX - suffix.length)) + suffix;
}
labels.set(session.sessionId, label);
taken.add(label);
}
return labels;
}