agy-bar 0.9.1 → 0.9.3

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/src/telemetry.mjs CHANGED
@@ -158,6 +158,19 @@ function pickQuotaSnapshot(quota) {
158
158
  return Object.keys(out).length > 0 ? out : null;
159
159
  }
160
160
 
161
+ export function resolveSessionTitles(payload = {}, prev = null) {
162
+ const conversationTitle = sanitizeText(payload?.conversation_title || '') || prev?.conversationTitle || null;
163
+ // If user explicitly renamed the conversation via /rename (conversation_title changed),
164
+ // clear any older custom tab override automatically.
165
+ let customTitle = prev?.customTitle || null;
166
+ let customTitleBase = prev?.customTitleBase ?? null;
167
+ if (customTitle && conversationTitle && customTitleBase && conversationTitle !== customTitleBase) {
168
+ customTitle = null;
169
+ customTitleBase = null;
170
+ }
171
+ return { conversationTitle, customTitle, customTitleBase };
172
+ }
173
+
161
174
  export function recordTelemetry(
162
175
  payload = {},
163
176
  {
@@ -165,6 +178,7 @@ export function recordTelemetry(
165
178
  git = null,
166
179
  stepTiming = null,
167
180
  prevSession = undefined,
181
+ tabSync = null,
168
182
  baseDir = configDir(homedir()),
169
183
  cacheTtlSec = 300,
170
184
  enableLedger = true,
@@ -376,15 +390,21 @@ export function recordTelemetry(
376
390
  const cacheRemainingSec =
377
391
  lastActivityAt !== null ? Math.max(0, Math.round((cacheTtlMs - (now - lastActivityAt)) / 1000)) : null;
378
392
 
379
- const conversationTitle = sanitizeText(p.conversation_title || '') || prev?.conversationTitle || null;
380
- // If user explicitly renamed the conversation via /rename (conversation_title changed),
381
- // clear any older custom tab override automatically.
382
- let customTitle = prev?.customTitle || null;
383
- let customTitleBase = prev?.customTitleBase ?? null;
384
- if (customTitle && conversationTitle && customTitleBase && conversationTitle !== customTitleBase) {
385
- customTitle = null;
386
- customTitleBase = null;
387
- }
393
+ const { conversationTitle, customTitle, customTitleBase } = resolveSessionTitles(p, prev);
394
+ const tabWritten = Boolean(tabSync?.written && tabSync.title);
395
+ const nextTabTitle = tabWritten ? tabSync.title : prev?.lastTabTitle || null;
396
+ const nextTabHostPid = tabWritten
397
+ ? (tabSync.hostPid ?? prev?.lastTabHostPid ?? null)
398
+ : (prev?.lastTabHostPid ?? null);
399
+ const nextTabTty = tabWritten
400
+ ? (tabSync.tty && tabSync.tty !== 'custom' ? tabSync.tty : prev?.lastTabTty ?? null)
401
+ : (prev?.lastTabTty ?? null);
402
+ const nextTabUpdatedAt = tabWritten ? now : prev?.lastTabUpdatedAt ?? null;
403
+ const nextTabError = tabWritten
404
+ ? null
405
+ : tabSync?.changed && !tabSync.written
406
+ ? tabSync.error || 'write_failed'
407
+ : prev?.lastTabError ?? null;
388
408
 
389
409
  const nextState = {
390
410
  sessionId,
@@ -409,8 +429,11 @@ export function recordTelemetry(
409
429
  conversationTitle,
410
430
  customTitle,
411
431
  customTitleBase,
412
- lastTabTitle: prev?.lastTabTitle || null,
413
- lastTabHostPid: prev?.lastTabHostPid ?? null,
432
+ lastTabTitle: nextTabTitle,
433
+ lastTabHostPid: nextTabHostPid,
434
+ lastTabTty: nextTabTty,
435
+ lastTabUpdatedAt: nextTabUpdatedAt,
436
+ lastTabError: nextTabError,
414
437
  model: modelName,
415
438
  effort: sanitizeText(p.model?.effort || '') || null,
416
439
  usedPct,
@@ -456,12 +479,17 @@ export function recordTelemetry(
456
479
  lastActivityAt,
457
480
  conversationTitle,
458
481
  customTitle,
459
- prevTabTitle: prev?.lastTabTitle || null,
460
- prevTabHostPid: prev?.lastTabHostPid ?? null,
482
+ prevTabTitle: nextTabTitle,
483
+ prevTabHostPid: nextTabHostPid,
484
+ prevTabTty: nextTabTty,
461
485
  };
462
486
  }
463
487
 
464
- export function updateSessionTabTitle(sessionId, tabTitle, { hostPid = null, baseDir = configDir(homedir()) } = {}) {
488
+ export function updateSessionTabTitle(
489
+ sessionId,
490
+ tabTitle,
491
+ { hostPid = null, tty = null, now = Date.now(), baseDir = configDir(homedir()) } = {},
492
+ ) {
465
493
  if (!sessionId) return false;
466
494
  const sFile = join(sessionsDir(baseDir), `${sessionId}.json`);
467
495
  const prev = readSessionFile(sFile);
@@ -469,7 +497,14 @@ export function updateSessionTabTitle(sessionId, tabTitle, { hostPid = null, bas
469
497
  try {
470
498
  writeFileAtomic(
471
499
  sFile,
472
- JSON.stringify({ ...prev, lastTabTitle: tabTitle, ...(hostPid !== null ? { lastTabHostPid: hostPid } : {}) }) + '\n',
500
+ JSON.stringify({
501
+ ...prev,
502
+ lastTabTitle: tabTitle,
503
+ lastTabUpdatedAt: now,
504
+ lastTabError: null,
505
+ ...(hostPid !== null ? { lastTabHostPid: hostPid } : {}),
506
+ ...(tty && tty !== 'custom' ? { lastTabTty: tty } : {}),
507
+ }) + '\n',
473
508
  );
474
509
  return true;
475
510
  } catch {
@@ -519,6 +554,7 @@ export function listSessions({ baseDir = configDir(homedir()), now = Date.now(),
519
554
  if (prune) {
520
555
  try {
521
556
  rmSync(full, { force: true });
557
+ rmSync(full.replace(/\.json$/, '.title'), { force: true });
522
558
  } catch {
523
559
  // Ignore prune errors.
524
560
  }
package/src/title.mjs ADDED
@@ -0,0 +1,136 @@
1
+ import { existsSync, readFileSync, realpathSync, writeFileSync, mkdirSync } from 'node:fs';
2
+ import { homedir } from 'node:os';
3
+ import { dirname, join, resolve } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { sanitizeText } from './ansi.mjs';
6
+ import { configDir, loadConfig } from './config.mjs';
7
+ import { readGitInfo } from './git.mjs';
8
+ import { buildTabTitle } from './tab-title.mjs';
9
+ import { deriveSessionId, peekSessionState, resolveSessionTitles, sessionsDir } from './telemetry.mjs';
10
+
11
+ const NATIVE_TITLE_FRESH_MS = 60_000;
12
+ const NATIVE_TITLE_TOUCH_INTERVAL_MS = 120_000;
13
+
14
+ export function nativeTitleMarkerPath(sessionId, baseDir = configDir(homedir())) {
15
+ return join(sessionsDir(baseDir), `${sessionId}.title`);
16
+ }
17
+
18
+ export function touchNativeTitleMarker(
19
+ sessionId,
20
+ { now = Date.now(), hostPid = process.ppid, baseDir = configDir(homedir()) } = {},
21
+ ) {
22
+ if (!sessionId) return false;
23
+ const markerPath = nativeTitleMarkerPath(sessionId, baseDir);
24
+ try {
25
+ if (existsSync(markerPath)) {
26
+ const [rawTs, rawPid] = readFileSync(markerPath, 'utf8').trim().split(':');
27
+ const prevTs = Number(rawTs);
28
+ const prevPid = Number(rawPid);
29
+ if (
30
+ Number.isFinite(prevTs) &&
31
+ now - prevTs < NATIVE_TITLE_TOUCH_INTERVAL_MS &&
32
+ Number.isFinite(prevPid) &&
33
+ prevPid === hostPid
34
+ ) {
35
+ return true;
36
+ }
37
+ }
38
+ mkdirSync(dirname(markerPath), { recursive: true });
39
+ writeFileSync(markerPath, `${now}:${Number.isFinite(hostPid) ? hostPid : 0}\n`, 'utf8');
40
+ return true;
41
+ } catch {
42
+ return false;
43
+ }
44
+ }
45
+
46
+ export function isNativeTitleActive(
47
+ sessionId,
48
+ { now = Date.now(), hostPid = process.ppid, baseDir = configDir(homedir()) } = {},
49
+ ) {
50
+ if (!sessionId) return false;
51
+ const markerPath = nativeTitleMarkerPath(sessionId, baseDir);
52
+ if (!existsSync(markerPath)) return false;
53
+ try {
54
+ const [rawTs, rawPid] = readFileSync(markerPath, 'utf8').trim().split(':');
55
+ const ts = Number(rawTs);
56
+ const pid = Number(rawPid);
57
+ if (!Number.isFinite(ts) || ts <= 0) return false;
58
+ const ageMs = now - ts;
59
+ if (ageMs < 0) return true;
60
+ if (ageMs < NATIVE_TITLE_FRESH_MS) return true;
61
+ if (ageMs < 24 * 60 * 60 * 1000 && Number.isFinite(pid) && pid > 1 && typeof hostPid === 'number' && pid === hostPid) {
62
+ return true;
63
+ }
64
+ return false;
65
+ } catch {
66
+ return false;
67
+ }
68
+ }
69
+
70
+ export function renderNativeTitle(
71
+ payload = {},
72
+ { config = loadConfig(), prevSession = undefined, baseDir = configDir(homedir()), now = Date.now(), hostPid = process.ppid, touchMarker = true } = {},
73
+ ) {
74
+ if (config.tabTitle === false) return '';
75
+
76
+ const p = payload && typeof payload === 'object' ? payload : {};
77
+ const sessionId = deriveSessionId(p);
78
+ const prev = prevSession !== undefined ? prevSession : peekSessionState(p, { baseDir });
79
+ const { customTitle } = resolveSessionTitles(p, prev);
80
+
81
+ const cwd = p?.workspace?.current_dir || p?.cwd || prev?.cwd || null;
82
+ const needsGit =
83
+ config.tabTitleSource === 'dir' ||
84
+ (!customTitle && !sanitizeText(p?.conversation_title || ''));
85
+ let git = null;
86
+ if (needsGit && cwd) {
87
+ if (prev && prev.cwd === cwd && prev.branch) {
88
+ git = { branch: prev.branch, dirty: Boolean(prev.dirty) };
89
+ } else {
90
+ // Zero-subprocess .git/HEAD lookup only.
91
+ git = readGitInfo(cwd, { checkDirty: false, checkDiff: false });
92
+ }
93
+ }
94
+
95
+ const title = buildTabTitle(p, { config, git, customTitle });
96
+ if (touchMarker) {
97
+ touchNativeTitleMarker(sessionId, { now, hostPid, baseDir });
98
+ }
99
+ return title ? sanitizeText(title).replace(/[\r\n\t]+/g, ' ') : '';
100
+ }
101
+
102
+ export async function runTitleCommand({
103
+ stdin = process.stdin,
104
+ stdout = process.stdout,
105
+ baseDir = configDir(homedir()),
106
+ config = undefined,
107
+ } = {}) {
108
+ let raw = '';
109
+ for await (const chunk of stdin) {
110
+ raw += chunk;
111
+ }
112
+
113
+ try {
114
+ const payload = raw.trim() ? JSON.parse(raw) : {};
115
+ const cfg = config ?? loadConfig();
116
+ const title = renderNativeTitle(payload, { config: cfg, baseDir });
117
+ if (title) {
118
+ stdout.write(`${title}\n`);
119
+ }
120
+ } catch {
121
+ // Never emit error noise into the terminal window title.
122
+ }
123
+ }
124
+
125
+ function isDirectInvocation() {
126
+ if (!process.argv[1]) return false;
127
+ try {
128
+ return realpathSync(process.argv[1]) === realpathSync(fileURLToPath(import.meta.url));
129
+ } catch {
130
+ return resolve(process.argv[1]) === fileURLToPath(import.meta.url);
131
+ }
132
+ }
133
+
134
+ if (isDirectInvocation()) {
135
+ await runTitleCommand();
136
+ }