@lethevimlet/sshift 1.7.3 → 1.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "1.7.3",
3
+ "version": "1.7.5",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -10,10 +10,10 @@
10
10
  * 1. App detection is NON-STICKY: Claude Code is considered present only
11
11
  * while one of its UI signatures is visible in the CURRENT viewport of
12
12
  * the headless terminal ("Claude", the "? for shortcuts" footer hint,
13
- * "esc to interrupt", edit-mode hints). When the user exits back to a
14
- * shell the signatures disappear and, after a few misses, all state
15
- * resets — so a shell prompt can never flash the tab (the old sticky
16
- * flag caused exactly that).
13
+ * "esc to interrupt", the ⎿ tool-result marker, edit-mode hints...).
14
+ * When the user exits back to a shell the signatures disappear and,
15
+ * after a few misses, no NEW working episode can start — so a shell
16
+ * prompt can never arm the engine.
17
17
  *
18
18
  * 2. "Working" is tracked from two signals:
19
19
  * - working indicators visible in the viewport footer (braille
@@ -22,9 +22,8 @@
22
22
  * - sustained agent-driven output: bytes that arrive while the user
23
23
  * has NOT typed recently (echo/UI responses to keystrokes are
24
24
  * ignored via userEchoWindowMs).
25
- * NOTE: "·" is deliberately NOT a spinner char anymore — it appears in
26
- * every status line ("3.2k tokens · esc to interrupt") and made the
27
- * old working-detection fire on ordinary output.
25
+ * NOTE: "·" is deliberately NOT a spinner char — it appears in every
26
+ * status line ("3.2k tokens · esc to interrupt").
28
27
  *
29
28
  * 3. The tab flashes ONLY on the working → idle TRANSITION: a working
30
29
  * episode was observed, the working indicator is gone, and no work
@@ -32,8 +31,29 @@
32
31
  * permission dialog both look like this transition, so no brittle
33
32
  * prompt regexes ("❯", "Do you want", "allow", ...) are needed.
34
33
  *
35
- * 4. User input (keystrokes) stops an active flash immediately and
36
- * suppresses flashing for cooldownMs — the user is already there.
34
+ * 4. User input (keystrokes) ends the current working episode, stops an
35
+ * active flash and suppresses flashing for cooldownMs — the user is
36
+ * already there. A genuinely running agent re-arms the episode with
37
+ * its very next spinner frame, so nothing is lost.
38
+ *
39
+ * Things that made v1.7.3 miss most "waiting for input" events, and how
40
+ * they are handled now:
41
+ *
42
+ * - The viewport was read through getTerminalState(), which serializes
43
+ * the whole 10k-line scrollback and returns NULL past its 1MB guard.
44
+ * Busy agent sessions — exactly the ones worth watching — went blind.
45
+ * Now it reads getTerminalViewport() (visible screen only) and follows
46
+ * the alternate-screen marker when a full-screen TUI is active.
47
+ * - A transition that landed inside the post-keystroke cooldown was
48
+ * dropped and never retried. The engine now keeps a pendingAttention
49
+ * flag and retries on every check until the flash actually goes out.
50
+ * - Losing the app signature (a permission dialog can cover the footer
51
+ * hints) wiped the pending episode. App misses now only stop NEW
52
+ * episodes from starting; an armed one still gets to flash.
53
+ * - A flash the user dismissed by simply looking at the tab left the
54
+ * server stuck in "already flashing", swallowing every later event.
55
+ * Clients now report onSessionFocus, and a fresh transition re-emits
56
+ * the flash anyway.
37
57
  *
38
58
  * There are NO default attention text patterns: strings like "(y/n)",
39
59
  * "allow" or "❯ " routinely appear inside chat prose, code and shell
@@ -57,9 +77,15 @@
57
77
  * mark the session as working (default: 600)
58
78
  * - workWindowMs: number - Rolling window for minWorkBytes (default: 2000)
59
79
  * - appMissLimit: number - Consecutive viewport checks without an app
60
- * signature before state resets (default: 3)
80
+ * signature before new episodes are blocked (default: 3)
61
81
  * - appPatterns: string[] - Regexes replacing the default app signatures
82
+ * - spinnerStaleMs: number - A bare spinner glyph only counts as "working" if
83
+ * output arrived within this window (default: 2000)
84
+ * - workStaleMs: number - A screen with no output at all for this long is
85
+ * never considered working (default: 60000)
62
86
  * - workingPatterns: string[] - Regexes replacing the default working indicators
87
+ * - workingHintPatterns: string[] - Regexes for run hints that count as working
88
+ * even on a quiet screen (default: "esc to interrupt")
63
89
  * - patterns: string[] - Extra attention regexes (footer lines, idle only)
64
90
  * - excludePatterns: string[] - Remove patterns (by source) from all lists
65
91
  */
@@ -78,22 +104,34 @@ class ClaudeAttentionPlugin {
78
104
  minWorkBytes: config.minWorkBytes || 600,
79
105
  workWindowMs: config.workWindowMs || 2000,
80
106
  appMissLimit: config.appMissLimit || 3,
107
+ spinnerStaleMs: config.spinnerStaleMs || 2000,
108
+ workStaleMs: config.workStaleMs || 60000,
81
109
  };
82
110
 
83
111
  // App signatures. Claude Code's welcome box scrolls away in long
84
112
  // sessions, so alongside the case-SENSITIVE "Claude" wordmark (keeps
85
113
  // lowercase shell text like `cd claude-project` from arming the
86
- // engine) we accept the persistent footer hints. A false positive
87
- // here only ARMS the engine; a flash still requires a real
88
- // working → idle transition.
114
+ // engine) we accept the markers that survive on screen: the footer
115
+ // hints, the mode indicators, the ⎿ tool-result glyph and the
116
+ // permission-dialog headline/choices (a dialog can push every other
117
+ // hint off the viewport, which is precisely when we must flash).
118
+ // A false positive here only ARMS the engine; a flash still requires
119
+ // a real working → idle transition.
89
120
  this._appPatterns = this._compileList(
90
121
  config.appPatterns || [
91
122
  'Claude',
92
123
  '\\? for shortcuts',
93
124
  'esc to interrupt',
125
+ 'esc to cancel',
94
126
  'accept edits',
95
127
  'plan mode',
96
128
  'bypass permissions',
129
+ 'shift\\+tab to cycle',
130
+ 'auto-compact',
131
+ '⎿',
132
+ '⏵⏵',
133
+ 'Do you want',
134
+ '❯ \\d+\\.',
97
135
  ],
98
136
  'appPatterns',
99
137
  '' // case-sensitive
@@ -102,16 +140,28 @@ class ClaudeAttentionPlugin {
102
140
  // Working indicators, scanned over the viewport footer only:
103
141
  // - the full braille block (U+2800–U+28FF) covers braille spinners,
104
142
  // - ✢✳✶✻✽∗ are Claude Code's sparkle spinner frames,
143
+ // - ◐◓◑◒◜◝◞◟ cover the arc/circle spinners used by some tools,
105
144
  // - "esc to interrupt" is shown for the whole duration of a run.
106
145
  this._workingPatterns = this._compileList(
107
146
  config.workingPatterns || [
108
147
  '[\\u2800-\\u28FF]',
109
148
  '[✢✳✶✻✽∗]',
149
+ '[◐◓◑◒◜◝◞◟]',
110
150
  'esc to interrupt',
111
151
  ],
112
152
  'workingPatterns'
113
153
  );
114
154
 
155
+ // Text hints that stay on screen for the WHOLE run ("esc to
156
+ // interrupt"). Unlike a spinner glyph these are unambiguous, so they
157
+ // count as "working" even when the screen is completely quiet.
158
+ this._workingHintPatterns = this._compileList(
159
+ config.workingHintPatterns || [
160
+ 'esc to interrupt',
161
+ ],
162
+ 'workingHintPatterns'
163
+ );
164
+
115
165
  // Optional user-supplied attention patterns (none by default).
116
166
  this._attentionPatterns = this._compileList(config.patterns || [], 'patterns');
117
167
 
@@ -121,6 +171,7 @@ class ClaudeAttentionPlugin {
121
171
  const source = new RegExp(p).source;
122
172
  this._appPatterns = this._appPatterns.filter(r => r.source !== source);
123
173
  this._workingPatterns = this._workingPatterns.filter(r => r.source !== source);
174
+ this._workingHintPatterns = this._workingHintPatterns.filter(r => r.source !== source);
124
175
  this._attentionPatterns = this._attentionPatterns.filter(r => r.source !== source);
125
176
  } catch (e) {
126
177
  console.error(`[claude-attention] Invalid exclude pattern "${p}":`, e.message);
@@ -130,6 +181,7 @@ class ClaudeAttentionPlugin {
130
181
  this._sessionState = new Map();
131
182
  this._checkTimers = new Map();
132
183
  this._dataTimers = new Map();
184
+ this._idleTimers = new Map();
133
185
  this._flashing = new Map();
134
186
  }
135
187
 
@@ -148,7 +200,9 @@ class ClaudeAttentionPlugin {
148
200
  this._sessionState.set(sessionId, {
149
201
  appVisible: false, // signature currently on screen
150
202
  appMissStreak: 0, // consecutive checks without a signature
151
- working: false, // inside a working episode
203
+ working: false, // working indicator seen recently
204
+ workEpisode: false, // a working episode is armed (not yet consumed)
205
+ pendingAttention: false, // wants to flash, hasn't managed to yet
152
206
  lastWorkSignalAt: 0, // last time any work signal was seen
153
207
  lastUserInputAt: 0, // last user keystroke
154
208
  lastOutputAt: 0, // last output of any kind
@@ -178,32 +232,58 @@ class ClaudeAttentionPlugin {
178
232
  return false;
179
233
  }
180
234
 
181
- // Extract the current viewport (last `rows` lines) from the serialized
182
- // terminal state, ANSI-stripped, one entry per screen line.
235
+ // Extract the current viewport, ANSI-stripped, one entry per screen line.
236
+ //
237
+ // Prefers the cheap viewport-only serialization; getTerminalState() is a
238
+ // fallback for older hosts (it serializes the entire scrollback and
239
+ // returns null once that exceeds its size guard).
183
240
  _getViewportLines(sessionId) {
184
- const state = this.ctx.getTerminalState(sessionId);
241
+ let state = null;
242
+ if (typeof this.ctx.getTerminalViewport === 'function') {
243
+ state = this.ctx.getTerminalViewport(sessionId);
244
+ }
245
+ if (!state || !state.state) {
246
+ state = this.ctx.getTerminalState(sessionId);
247
+ }
185
248
  if (!state || !state.state) return null;
249
+
186
250
  const rows = state.rows || 24;
187
- const lines = state.state.split('\n');
251
+ let text = state.state;
252
+
253
+ // Full-screen TUIs run on the alternate buffer, which the serializer
254
+ // appends after a `\x1b[?1049h` marker — everything before it is the
255
+ // scrolled-away normal buffer, not what the user is looking at.
256
+ const altMarker = text.lastIndexOf('\x1b[?1049h');
257
+ if (altMarker !== -1) {
258
+ text = text.slice(altMarker);
259
+ }
260
+
261
+ const lines = text.split('\n');
188
262
  const viewport = lines.slice(-rows);
189
263
  return viewport.map(l => this._stripAnsi(l));
190
264
  }
191
265
 
192
- _startFlash(sessionId) {
266
+ _startFlash(sessionId, { force = false } = {}) {
193
267
  const s = this._getSessionState(sessionId);
194
268
  const now = Date.now();
269
+ // Suppressed for now — pendingAttention keeps it queued for a retry
270
+ // instead of dropping the event on the floor.
195
271
  if (now < s.cooldownUntil) return;
196
272
  if (now - s.lastUserInputAt < this.config.userEchoWindowMs) return;
197
- if (this._flashing.get(sessionId)) return;
273
+ // `force` is used for a fresh transition: re-emitting is harmless and
274
+ // recovers from a flash the client cleared without telling us.
275
+ if (!force && this._flashing.get(sessionId)) return;
198
276
  this._flashing.set(sessionId, true);
277
+ s.pendingAttention = false;
199
278
  this.ctx.flashTab(sessionId, { duration: this.config.flashDuration || 0 });
200
279
  }
201
280
 
202
281
  _stopFlash(sessionId, { cooldown = false } = {}) {
282
+ const s = this._getSessionState(sessionId);
203
283
  if (cooldown) {
204
- const s = this._getSessionState(sessionId);
205
284
  s.cooldownUntil = Date.now() + this.config.cooldownMs;
206
285
  }
286
+ s.pendingAttention = false;
207
287
  if (!this._flashing.get(sessionId)) return;
208
288
  this._flashing.delete(sessionId);
209
289
  this.ctx.stopFlashTab(sessionId);
@@ -212,17 +292,30 @@ class ClaudeAttentionPlugin {
212
292
  _resetSession(sessionId) {
213
293
  this._stopFlash(sessionId);
214
294
  this._sessionState.delete(sessionId);
215
- const dataTimer = this._dataTimers.get(sessionId);
216
- if (dataTimer) {
217
- clearTimeout(dataTimer);
218
- this._dataTimers.delete(sessionId);
295
+ this._clearTimer(this._dataTimers, sessionId);
296
+ this._clearTimer(this._idleTimers, sessionId);
297
+ }
298
+
299
+ _clearTimer(map, sessionId) {
300
+ const timer = map.get(sessionId);
301
+ if (timer) {
302
+ clearTimeout(timer);
303
+ map.delete(sessionId);
219
304
  }
220
305
  }
221
306
 
222
- _markWorking(s) {
223
- const now = Date.now();
307
+ _markWorking(s, sessionId) {
224
308
  s.working = true;
225
- s.lastWorkSignalAt = now;
309
+ s.workEpisode = true;
310
+ s.pendingAttention = false;
311
+ s.lastWorkSignalAt = Date.now();
312
+ // Re-evaluate as soon as the idle threshold could be met, so the flash
313
+ // doesn't wait for the next periodic check (up to checkInterval later).
314
+ this._clearTimer(this._idleTimers, sessionId);
315
+ this._idleTimers.set(sessionId, setTimeout(() => {
316
+ this._idleTimers.delete(sessionId);
317
+ this._evaluate(sessionId);
318
+ }, this.config.idleThreshold + 100));
226
319
  }
227
320
 
228
321
  // Core evaluation: reads the live viewport and drives the state machine.
@@ -242,40 +335,69 @@ class ClaudeAttentionPlugin {
242
335
  } else {
243
336
  s.appMissStreak++;
244
337
  if (s.appMissStreak >= this.config.appMissLimit) {
245
- // Claude left the screen (user exited to shell, switched apps).
246
- const wasFlashing = this._flashing.get(sessionId);
338
+ // Claude is off screen: no NEW working episode may start. An
339
+ // already armed episode is kept on purpose — a permission dialog
340
+ // can hide every signature, and an agent that exited on its own
341
+ // still deserves its one flash. Anything the user did to leave
342
+ // the app went through onUserInput, which clears the episode.
247
343
  s.appVisible = false;
248
344
  s.working = false;
249
- if (wasFlashing) this._stopFlash(sessionId);
250
345
  }
251
- return;
252
346
  }
253
347
 
254
- // 2) Working indicator in the footer region.
348
+ // 2) Working indicator in the footer region, in two tiers:
349
+ // - a run hint ("esc to interrupt") is unambiguous and counts even
350
+ // on a completely quiet screen;
351
+ // - a bare spinner glyph only counts while output is actually
352
+ // flowing. A spinner that animates always produces output, so a
353
+ // glyph with a quiet stream is a LEFTOVER (the finished status
354
+ // line pushed up by a dialog, a braille graph, a progress bar).
355
+ // Treating those as "still working" is what kept sessions
356
+ // pinned in the working state and swallowed the flash.
255
357
  const footer = viewport.slice(-this.config.footerLines);
256
- const workingVisible = footer.some(line => this._matchesAny(this._workingPatterns, line));
358
+ const hintVisible = footer.some(line => this._matchesAny(this._workingHintPatterns, line));
359
+ const glyphVisible = !hintVisible &&
360
+ footer.some(line => this._matchesAny(this._workingPatterns, line));
361
+ const spinnerLive = (now - s.lastOutputAt) < this.config.spinnerStaleMs;
362
+ // Last resort against a permanently stuck "working" state: a run that
363
+ // is really in progress keeps repainting its elapsed-time counter, so
364
+ // a screen that has produced NOTHING for workStaleMs is showing a
365
+ // leftover, not a live run.
366
+ const outputStale = (now - s.lastOutputAt) > this.config.workStaleMs;
367
+ const workingVisible = s.appVisible && !outputStale &&
368
+ (hintVisible || (glyphVisible && spinnerLive));
257
369
 
258
370
  if (workingVisible) {
259
- this._markWorking(s);
371
+ const wasFlashing = this._flashing.get(sessionId) || s.pendingAttention;
372
+ this._markWorking(s, sessionId);
260
373
  // Working resumed — anything currently flashing is stale.
261
- if (this._flashing.get(sessionId)) {
262
- this._stopFlash(sessionId, { cooldown: true });
263
- }
374
+ if (wasFlashing) this._stopFlash(sessionId, { cooldown: true });
264
375
  return;
265
376
  }
266
377
 
267
378
  // 3) Idle transition: a working episode ended (indicator gone AND no
268
379
  // work signal for idleThreshold) → the reply finished or a
269
380
  // permission dialog is waiting → flash.
270
- if (s.working && (now - s.lastWorkSignalAt) >= this.config.idleThreshold) {
381
+ if (s.workEpisode && (now - s.lastWorkSignalAt) >= this.config.idleThreshold) {
382
+ s.workEpisode = false;
271
383
  s.working = false;
384
+ s.pendingAttention = true;
385
+ this._startFlash(sessionId, { force: true });
386
+ return;
387
+ }
388
+
389
+ // 4) A transition that was suppressed (cooldown after a keystroke)
390
+ // stays queued and is retried until it goes out or the user shows up.
391
+ if (s.pendingAttention) {
272
392
  this._startFlash(sessionId);
273
393
  return;
274
394
  }
275
395
 
276
- // 4) Optional custom attention patterns (config-supplied only),
396
+ // 5) Optional custom attention patterns (config-supplied only),
277
397
  // evaluated against the footer while idle.
278
- if (!s.working && this._attentionPatterns.length > 0) {
398
+ if (s.appVisible && !s.working && this._attentionPatterns.length > 0) {
399
+ // Level-triggered (the pattern stays on screen), so the dedupe in
400
+ // _startFlash must apply — no force here.
279
401
  if (footer.some(line => this._matchesAny(this._attentionPatterns, line))) {
280
402
  this._startFlash(sessionId);
281
403
  }
@@ -299,10 +421,9 @@ class ClaudeAttentionPlugin {
299
421
  // Cheap inline work signal: spinner/indicator glyphs in the stream
300
422
  // keep the working state fresh without waiting for a viewport check.
301
423
  if (s.appVisible && this._matchesAny(this._workingPatterns, data)) {
302
- this._markWorking(s);
303
- if (this._flashing.get(sessionId)) {
304
- this._stopFlash(sessionId, { cooldown: true });
305
- }
424
+ const wasFlashing = this._flashing.get(sessionId) || s.pendingAttention;
425
+ this._markWorking(s, sessionId);
426
+ if (wasFlashing) this._stopFlash(sessionId, { cooldown: true });
306
427
  return;
307
428
  }
308
429
 
@@ -315,10 +436,9 @@ class ClaudeAttentionPlugin {
315
436
  }
316
437
  s.agentBytes += data.length;
317
438
  if (s.appVisible && s.agentBytes >= this.config.minWorkBytes) {
318
- this._markWorking(s);
319
- if (this._flashing.get(sessionId)) {
320
- this._stopFlash(sessionId, { cooldown: true });
321
- }
439
+ const wasFlashing = this._flashing.get(sessionId) || s.pendingAttention;
440
+ this._markWorking(s, sessionId);
441
+ if (wasFlashing) this._stopFlash(sessionId, { cooldown: true });
322
442
  }
323
443
  }
324
444
 
@@ -330,7 +450,18 @@ class ClaudeAttentionPlugin {
330
450
  onUserInput(sessionId /*, data */) {
331
451
  const s = this._getSessionState(sessionId);
332
452
  s.lastUserInputAt = Date.now();
333
- // The user is here — stop flashing and hold off for a while.
453
+ // The user is here — stop flashing and hold off for a while. The
454
+ // current episode is dropped as well: if the agent really is working,
455
+ // its next spinner frame re-arms it within milliseconds, and if the
456
+ // keystroke was the user quitting to a shell there is nothing left to
457
+ // announce.
458
+ s.workEpisode = false;
459
+ s.working = false;
460
+ this._stopFlash(sessionId, { cooldown: true });
461
+ }
462
+
463
+ // A client reported the user is looking at this tab.
464
+ onSessionFocus(sessionId) {
334
465
  this._stopFlash(sessionId, { cooldown: true });
335
466
  }
336
467
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-attention",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Flashes SSH tabs when Claude Code finishes working and is waiting for you (state-transition detection, no brittle prompt patterns)",
5
5
  "type": "sshift-plugin"
6
6
  }