Repository navigation
Expand file tree
/
Copy pathdaemon.py
More file actions
executable file
·1467 lines (1341 loc) · 56.5 KB
/
Copy pathdaemon.py
File metadata and controls
executable file
·1467 lines (1341 loc) · 56.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""herdr-agent-inbox daemon.
Watches herdr's agent panes and decorates them with inbox metadata:
- pane token/metadata `title`: session title derived from the agent's native
session transcript (first real user prompt), like ChatGPT/Claude chat titles.
- pane token `rank`: inbox tier (blocked=0, done/unread=1, working=2, idle=3,
unknown=4, settled=5) used by an `agent.view.set` sort so the Agents panel
behaves like an inbox: attention on top, settled slides to the bottom.
- pane tokens `age` (session running time) and `since` (time in current state).
- pane token `flag`: "⚑" settled / "●" marked unread.
- workspace tokens `agents` (per-status counts, e.g. "!1 ▸2 ✓1 ⚑1") and
`busy` (longest currently-working stint).
Settle / mark-unread commands arrive on a control socket (see actions.py).
State persists across herdr server restarts keyed by terminal_id.
Stdlib only. One herdr request per connection (the server closes the socket
after each response); only events.subscribe holds a long-lived connection.
"""
import fcntl
import glob
import hashlib
import json
import os
import queue
import re
import socket
import subprocess
import sys
import threading
import time
try:
import tomllib
except ImportError: # < 3.11
tomllib = None
SOURCE = "herdr-agent-inbox"
TICK_SECS = 30.0
DEBOUNCE_SECS = 0.4
TITLE_MAX = 56
STATE_KEEP_SECS = 3 * 24 * 3600 # keep state for vanished terminals 3 days
RANK_BLOCKED = "0"
RANK_ATTENTION = "1" # done (finished, unseen) or manually marked unread
RANK_WORKING = "2"
RANK_IDLE = "3"
RANK_UNKNOWN = "4"
RANK_SETTLED = "5"
FLAG_SETTLED = "⚑" # ⚑
FLAG_UNREAD = "●" # ●
# Agents whose native title we can only find by working directory (herdr
# reports no session ref for them, or they never publish the title). Two live
# panes of the same such agent in one directory are indistinguishable, so we
# skip the lookup rather than risk labelling both with the same name. claude
# and pi are keyed by their own transcript and are never ambiguous.
CWD_KEYED_AGENTS = ("codex", "grok", "cursor", "hermes")
# Bump when title extraction changes so cached titles are re-derived instead
# of surviving forever in state.json.
TITLE_ALGO = 2
VIEW_SORT = [
{"field": {"token": "rank"}, "order": "asc"},
{"field": "state_change_seq", "order": "desc"},
]
def herdr_socket_path():
p = os.environ.get("HERDR_SOCKET_PATH")
if p:
return p
return os.path.expanduser("~/.config/herdr/herdr.sock")
def state_dir():
# Deliberately NOT $HERDR_PLUGIN_STATE_DIR: the daemon may be started from
# a shell (no plugin env) or from a hook (plugin env); deriving the path
# from the session socket keeps every entry point on the same control
# socket and state file.
p = os.path.join(os.path.dirname(herdr_socket_path()), "agent-inbox-state")
os.makedirs(p, mode=0o700, exist_ok=True)
try:
# State holds prompt-derived titles and project paths — private.
# makedirs won't tighten a pre-existing dir, so chmod explicitly
# (this also gates control.sock on platforms where AF_UNIX connect
# is governed by directory permissions).
os.chmod(p, 0o700)
except OSError:
pass
return p
def config_file():
d = os.environ.get("HERDR_PLUGIN_CONFIG_DIR") or os.path.join(
os.path.dirname(herdr_socket_path()), "plugins", "config", "herdr-agent-inbox"
)
return os.path.join(d, "config.toml")
DEFAULT_CONFIG = {
"title_source": "first", # "first" | "last" user prompt
"prefer_agent_title": True, # use the agent's own session name when it has one
"summarize": False, # pipe the prompt through summarize_cmd
"summarize_cmd": "", # shell cmd: prompt on stdin -> title on stdout
"summarize_timeout_secs": 60,
"tab_rename": False, # rename tabs to their agent's session title
"tab_max_chars": 24, # truncate the label to this many characters
"tab_ellipsis": "…", # appended when the title is truncated
"tab_respect_manual": True, # never overwrite a label you set yourself
}
CONFIG_TEMPLATE = """\
# herdr-agent-inbox configuration
[title]
# Prefer the title the AGENT ITSELF gives the session, when it records one
# (claude-code's own session name; pi/codex don't publish one yet). Falls back
# to the prompt-derived title below whenever no native title exists.
prefer_agent_title = true
# Which user prompt seeds the session title when there's no native one:
# "first" — the opening request, like ChatGPT/Claude chat titles
# "last" — the most recent request; re-derived when the agent finishes a turn
source = "first"
# Produce a meaningful title by piping the prompt through a command instead of
# showing the (truncated) raw prompt. The command receives the prompt text on
# stdin and must print a short title on stdout.
summarize = false
summarize_cmd = "claude -p --model claude-haiku-4-5-20251001 'Write a 4-8 word title for this coding-agent session request. Output ONLY the title, nothing else.'"
summarize_timeout_secs = 60
[tab]
# Rename each tab to its agent's session title automatically.
rename_from_agent_title = false
# Truncate the label to this many characters, appending `ellipsis` when cut.
max_chars = 24
ellipsis = "…"
# Never touch a tab you named yourself: a tab is only managed while its label
# is herdr's default (the tab number) or the last label this plugin set.
respect_manual = true
"""
def load_config():
cfg = dict(DEFAULT_CONFIG)
path = config_file()
if not os.path.exists(path):
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w") as f:
f.write(CONFIG_TEMPLATE)
except OSError:
pass
return cfg
if tomllib is None:
return cfg
try:
with open(path, "rb") as f:
data = tomllib.load(f)
title = data.get("title") or {}
src = title.get("source")
if src in ("first", "last"):
cfg["title_source"] = src
if "prefer_agent_title" in title:
cfg["prefer_agent_title"] = bool(title["prefer_agent_title"])
cfg["summarize"] = bool(title.get("summarize", False))
cmd = title.get("summarize_cmd")
if isinstance(cmd, str):
cfg["summarize_cmd"] = cmd
t = title.get("summarize_timeout_secs")
if isinstance(t, int) and 5 <= t <= 600:
cfg["summarize_timeout_secs"] = t
tab = data.get("tab") or {}
cfg["tab_rename"] = bool(tab.get("rename_from_agent_title", False))
n = tab.get("max_chars")
if isinstance(n, int) and 4 <= n <= 200:
cfg["tab_max_chars"] = n
ell = tab.get("ellipsis")
if isinstance(ell, str) and len(ell) <= 4:
cfg["tab_ellipsis"] = ell
cfg["tab_respect_manual"] = bool(tab.get("respect_manual", True))
except (OSError, ValueError) as e:
sys.stderr.write("config load failed: %s\n" % e)
return cfg
class Log:
def __init__(self, path):
self.path = path
self._writes = 0
self._rotate()
def _rotate(self):
try:
if os.path.exists(self.path) and os.path.getsize(self.path) > 1_000_000:
os.replace(self.path, self.path + ".1")
except OSError:
pass
def __call__(self, msg):
line = "%s %s\n" % (time.strftime("%Y-%m-%d %H:%M:%S"), msg)
self._writes += 1
if self._writes % 500 == 0: # long-running daemons rotate too
self._rotate()
try:
with open(self.path, "a") as f:
f.write(line)
except OSError:
pass
def herdr_request(method, params, timeout=10.0):
"""One request on a fresh connection; the server closes it after replying."""
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect(herdr_socket_path())
payload = json.dumps({"id": "inbox", "method": method, "params": params})
s.sendall((payload + "\n").encode())
buf = b""
while not buf.endswith(b"\n"):
chunk = s.recv(1 << 16)
if not chunk:
break
buf += chunk
finally:
s.close()
# Raise RuntimeError (not ValueError) for empty/truncated responses so
# every `except (OSError, RuntimeError)` call site survives a herdr
# restart mid-request.
if not buf:
raise RuntimeError("%s: connection closed without a reply" % method)
try:
resp = json.loads(buf)
except ValueError as e:
raise RuntimeError("%s: bad response: %s" % (method, e))
if "error" in resp:
raise RuntimeError("%s: %s" % (method, resp["error"]))
return resp.get("result") or {}
def fmt_dur(secs):
secs = max(0, int(secs))
if secs < 60:
return "%ds" % secs
if secs < 3600:
return "%dm" % (secs // 60)
if secs < 86400:
h, m = divmod(secs // 60, 60)
return "%dh%02dm" % (h, m) if m else "%dh" % h
d, h = divmod(secs // 3600, 24)
return "%dd%dh" % (d, h) if h else "%dd" % d
# ---------------------------------------------------------------- titles ----
_WS_RE = re.compile(r"\s+")
_TAGLINE_RE = re.compile(r"<[^>]{1,80}>")
def _clean_title(text):
if not text:
return None
# Defense in depth: drop C0/C1 control bytes (ESC, BEL, …) so terminal
# escape sequences from transcript content or LLM output can never ride
# a title, regardless of downstream normalization.
text = re.sub(r"[\x00-\x08\x0b-\x1f\x7f-\x9f]", " ", text)
m = re.search(r"<command-args>(.*?)</command-args>", text, re.S)
if m and m.group(1).strip():
text = m.group(1)
elif re.search(r"<(command-name|local-command-stdout|local-command-caveat)>", text):
# A slash-command record with no real prompt text — not title material.
return None
# Drop system reminders, xml-ish noise, and URLs (they make lousy titles).
text = re.sub(r"<system-reminder>.*?</system-reminder>", " ", text, flags=re.S)
text = _TAGLINE_RE.sub(" ", text)
text = re.sub(r"\(?\bhttps?://\S+\)?", " ", text)
text = _WS_RE.sub(" ", text).strip()
if not text or text.startswith("Caveat:"):
return None
if len(text) < 4:
return None
if len(text) > TITLE_MAX:
cut = text[:TITLE_MAX]
if " " in cut[20:]:
cut = cut[: cut.rfind(" ")]
text = cut.rstrip(" ,;:.") + "…"
return text
def _texts_from_content(content):
out = []
if isinstance(content, str):
out.append(content)
elif isinstance(content, list):
for part in content:
if isinstance(part, dict):
t = part.get("text") or part.get("input_text")
if isinstance(t, str):
out.append(t)
return out
def _user_texts(obj):
"""Find user-authored text in one transcript JSON object (any agent format)."""
if not isinstance(obj, dict):
return []
# claude-code: {"type":"user","message":{"role":"user","content":...}}
# pi: {"type":"message","message":{"role":"user","content":[...]}}
msg = obj.get("message")
if isinstance(msg, dict) and msg.get("role") == "user":
if obj.get("isSidechain"):
return []
return _texts_from_content(msg.get("content"))
# codex rollouts: {"type":"response_item","payload":{"role":"user","content":[...]}}
payload = obj.get("payload")
if isinstance(payload, dict) and payload.get("role") == "user":
return _texts_from_content(payload.get("content"))
if obj.get("role") == "user":
return _texts_from_content(obj.get("content"))
return []
def _scan_lines_for_prompt(lines, want_last):
"""Returns (clean_title, raw_text). For want_last, feed reversed lines."""
summary = None
for line in lines:
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
except ValueError:
continue
if isinstance(obj, dict) and obj.get("type") == "summary" and obj.get("summary"):
summary = summary or _clean_title(str(obj["summary"]))
continue
for text in _user_texts(obj):
if "<system-reminder>" in text and "</system-reminder>" not in text:
continue
title = _clean_title(text)
if title:
raw = _raw_prompt(text)
return (title if want_last else (summary or title)), raw
return summary, None
def _raw_prompt(text):
"""The prompt as summarizer input: unwrapped but untruncated."""
m = re.search(r"<command-args>(.*?)</command-args>", text, re.S)
if m and m.group(1).strip():
text = m.group(1)
text = re.sub(r"<system-reminder>.*?</system-reminder>", " ", text, flags=re.S)
text = _WS_RE.sub(" ", text).strip()
return text[:4000]
def _tail_lines(path, max_bytes=512 * 1024):
size = os.path.getsize(path)
with open(path, "rb") as f:
f.seek(max(0, size - max_bytes))
data = f.read()
lines = data.split(b"\n")
if size > max_bytes and lines:
lines = lines[1:] # drop the partial first line
return [ln.decode("utf-8", "replace") for ln in lines]
def _newest_json_title(candidates, keys, mtime_of=None):
"""(title, mtime) from the most recently touched candidate JSON file.
candidates: iterable of file paths; keys: field names to try in order;
mtime_of: optional callable(dict) -> sortable recency from file content.
"""
best = None
for path in candidates:
try:
stamp = os.path.getmtime(path)
with open(path) as f:
data = json.load(f)
except (OSError, ValueError):
continue
if not isinstance(data, dict):
continue
if mtime_of:
stamp = mtime_of(data) or stamp
title = None
for key in keys:
title = _clean_title(str(data.get(key) or ""))
if title:
break
if title and (best is None or stamp > best[1]):
best = (title, stamp)
return best or (None, 0)
def codex_native_title(cwd):
"""codex keeps a per-thread `title` in ~/.codex/state_5.sqlite.
It is the verbatim first prompt rather than a short generated name, so it
gets truncated like any prompt-derived title — but it is authoritative,
already indexed by cwd, and works for panes with no session ref at all.
"""
if not cwd:
return None
db = os.path.expanduser("~/.codex/state_5.sqlite")
if not os.path.exists(db):
return None
try:
import sqlite3
# Read-only URI so a live codex never blocks us and we never write.
con = sqlite3.connect("file:%s?mode=ro" % db, uri=True, timeout=0.5)
try:
con.execute("PRAGMA query_only = 1")
row = con.execute(
"SELECT title, preview, first_user_message FROM threads "
"WHERE cwd = ? AND archived = 0 "
"ORDER BY COALESCE(updated_at_ms, 0) DESC LIMIT 1",
(cwd,),
).fetchone()
finally:
con.close()
except Exception:
return None
for value in row or ():
title = _clean_title(str(value or ""))
if title:
return title
return None
def _sqlite_scalar(db, sql, params):
"""One read-only scalar query; never blocks or writes the agent's db."""
if not os.path.exists(db):
return None
try:
import sqlite3
con = sqlite3.connect("file:%s?mode=ro" % db, uri=True, timeout=0.5)
try:
con.execute("PRAGMA query_only = 1")
row = con.execute(sql, params).fetchone()
finally:
con.close()
except Exception:
return None
return row
def hermes_native_title(cwd):
"""hermes auto-titles sessions into ~/.hermes/profiles/<p>/state.db
(`sessions.title`, best-effort and only set while NULL). It reports a
session id + lifecycle state to herdr but never the title, so match on
cwd across profiles, preferring a session that is still running."""
if not cwd:
return None
best = None
for db in glob.glob(os.path.expanduser("~/.hermes/profiles/*/state.db")):
row = _sqlite_scalar(
db,
"SELECT title, ended_at IS NULL AS live, COALESCE(started_at, '') "
"FROM sessions WHERE cwd = ? AND COALESCE(archived, 0) = 0 "
"AND title IS NOT NULL AND title != '' "
"ORDER BY live DESC, started_at DESC LIMIT 1",
(cwd,),
)
if not row:
continue
title = _clean_title(str(row[0] or ""))
if title:
rank = (row[1] or 0, row[2] or "")
if best is None or rank > best[1]:
best = (title, rank)
return best[0] if best else None
def grok_native_title(cwd):
"""grok stores `generated_title` per session under a url-encoded cwd dir."""
if not cwd:
return None
import urllib.parse
root = os.path.expanduser(
"~/.grok/sessions/%s" % urllib.parse.quote(cwd, safe="")
)
return _newest_json_title(
glob.glob(os.path.join(root, "*", "summary.json")),
("generated_title", "session_summary"),
)[0]
def cursor_native_title(cwd):
"""cursor-agent stores `title` per chat under md5(cwd)/<chat-uuid>/."""
if not cwd:
return None
digest = hashlib.md5(cwd.encode()).hexdigest()
root = os.path.expanduser("~/.cursor/chats/%s" % digest)
return _newest_json_title(
glob.glob(os.path.join(root, "*", "meta.json")),
("title",),
mtime_of=lambda d: (d.get("updatedAtMs") or 0) / 1000.0 or None,
)[0]
def pi_native_title(path):
"""pi never auto-generates a title, but a name the user sets (`/name`,
`--name`, picker rename, RPC set_session_name) is appended to the same
transcript as {"type":"session_info","name":...}; the latest one wins and
an empty name clears it — matching what pi's own resume picker shows."""
if not path:
return None
try:
size = os.path.getsize(path)
cap = 256 * 1024
while True:
for line in reversed(_tail_lines(path, cap)):
if '"session_info"' not in line:
continue
try:
obj = json.loads(line)
except ValueError:
continue
if obj.get("type") == "session_info" and "name" in obj:
# An empty name is pi's way of clearing it.
return _clean_title(str(obj.get("name") or "")) or None
if cap >= min(size, 32 * 1024 * 1024):
return None
cap *= 4
except OSError:
return None
def native_title_from_transcript(path):
"""claude-code's own name for the session, if it has one.
Two record types, both kept current as the conversation evolves:
{"type":"custom-title","customTitle":...} a name YOU set (/title)
{"type":"ai-title","aiTitle":...} claude's generated title
A custom title wins — it is an explicit choice — so scan from the end and
take the newest of each, preferring custom. This mirrors what claude's
own resume picker displays.
"""
try:
size = os.path.getsize(path)
cap = 256 * 1024
while True:
ai = None
for line in reversed(_tail_lines(path, cap)):
if '"customTitle"' not in line and '"aiTitle"' not in line:
continue
try:
obj = json.loads(line)
except ValueError:
continue
kind = obj.get("type")
if kind == "custom-title":
title = _clean_title(str(obj.get("customTitle") or ""))
if title:
return title # explicit name beats the generated one
elif kind == "ai-title" and ai is None:
ai = _clean_title(str(obj.get("aiTitle") or ""))
if ai:
return ai
if cap >= min(size, 32 * 1024 * 1024):
return None
cap *= 4
except OSError:
return None
def native_title_for(rec, path):
"""The session name the AGENT ITSELF gives this conversation, or None.
Where each agent keeps it (verified 2026-07-28):
claude transcript records {"type":"ai-title","aiTitle":...} ✅
grok ~/.grok/sessions/<urlencoded cwd>/<id>/summary.json
-> generated_title ✅
cursor ~/.cursor/chats/<md5 cwd>/<uuid>/meta.json -> title ✅
codex ~/.codex/state_5.sqlite threads.title — the verbatim
first prompt, used as-is (truncated) ✅
pi no auto-title, but a user-set name lands in the same
transcript as {"type":"session_info","name":...} ✅
hermes auto-titles into ~/.hermes/profiles/*/state.db
sessions.title (reports its id to herdr, never the
title), so read the db directly ✅
codex, grok, cursor and hermes are keyed by cwd (herdr gets no session
ref for the first three, and hermes never reports its title), so the
newest session for that directory wins.
"""
agent = rec.get("agent")
if agent == "claude" and path:
return native_title_from_transcript(path)
if agent == "codex":
return codex_native_title(rec.get("cwd"))
if agent == "pi" and path:
return pi_native_title(path)
if agent == "hermes":
return hermes_native_title(rec.get("cwd"))
if agent == "grok":
return grok_native_title(rec.get("cwd"))
if agent == "cursor":
return cursor_native_title(rec.get("cwd"))
return None
def title_from_transcript(path, source="first", prefer_native=True):
"""(clean_title, raw_prompt) from a session transcript.
A native agent title wins when present (raw is None so it is never sent
to the summarizer — the agent already named it).
source "first": the opening user prompt (preferring claude summary lines).
source "last": the most recent user prompt (tail-scanned).
"""
if prefer_native:
native = native_title_from_transcript(path)
if native:
return native, None
try:
if source == "last":
# Transcript lines can be huge (base64 images in tool results), so
# widen the tail window until a user prompt shows up — but cap the
# read so a promptless multi-hundred-MB transcript can't be
# re-slurped whole on every tick.
size = os.path.getsize(path)
cap = 512 * 1024
while True:
title, raw = _scan_lines_for_prompt(
reversed(_tail_lines(path, cap)), True)
if title or cap >= min(size, 32 * 1024 * 1024):
return title, raw
cap *= 4
with open(path, "r", errors="replace") as f:
head = []
for i, line in enumerate(f):
if i > 400:
break
head.append(line)
return _scan_lines_for_prompt(head, False)
except OSError:
return None, None
def _munge_claude_cwd(cwd):
return re.sub(r"[/.]", "-", cwd)
def resolve_transcript(agent_rec):
"""Locate the native session transcript for an agent pane, if any."""
sess = agent_rec.get("agent_session")
if not isinstance(sess, dict):
return None
kind, value = sess.get("kind"), sess.get("value")
if not value:
return None
if kind == "path":
return value if os.path.exists(value) else None
# A session *id* gets interpolated into paths/globs below — reject
# anything that could traverse or glob outside the intended directories.
if re.search(r"[/\\*?\[\]]|\.\.", value):
return None
if kind == "id":
agent = agent_rec.get("agent")
if agent == "claude":
cwd = agent_rec.get("cwd") or ""
p = os.path.expanduser(
"~/.claude/projects/%s/%s.jsonl" % (_munge_claude_cwd(cwd), value)
)
if os.path.exists(p):
return p
hits = glob.glob(os.path.expanduser("~/.claude/projects/*/%s.jsonl" % value))
return hits[0] if hits else None
if agent == "codex":
hits = glob.glob(
os.path.expanduser("~/.codex/sessions/**/*%s*.jsonl" % value),
recursive=True,
)
return hits[0] if hits else None
return None
# ---------------------------------------------------------------- daemon ----
class InboxDaemon:
def __init__(self):
self.dir = state_dir()
self.log = Log(os.path.join(self.dir, "daemon.log"))
self.state_path = os.path.join(self.dir, "state.json")
self.control_path = os.path.join(self.dir, "control.sock")
self.lock = threading.RLock()
self.dirty = threading.Event()
self.stop = threading.Event()
self.terminals = {} # terminal_id -> persisted per-agent state
self.last_report = {} # terminal_id -> {"title":..., "tokens": {...}}
self.ws_report = {} # workspace_id -> tokens dict last reported
self.pane_to_tid = {} # pane_id -> terminal_id (from last refresh)
self.cfg = load_config()
self.cfg_mtime = self._cfg_mtime()
self.ambiguous = set() # (agent, cwd) pairs with >1 live pane
self.tab_labels = {} # tab_id -> label WE set (never clobber yours)
self.sum_q = queue.Queue()
self.sum_inflight = set() # (tid, hash) queued or running
self.sum_failed = set() # hashes that failed this run; don't retry
self._load()
# -- persistence --
def _load(self):
try:
with open(self.state_path) as f:
data = json.load(f)
self.terminals = data.get("terminals", {})
self.tab_labels = data.get("tab_labels", {}) or {}
except (OSError, ValueError):
self.terminals = {}
def _save(self):
tmp = self.state_path + ".tmp"
try:
with open(tmp, "w") as f:
json.dump({"terminals": self.terminals,
"tab_labels": self.tab_labels}, f)
os.replace(tmp, self.state_path)
except OSError as e:
self.log("state save failed: %s" % e)
# -- inbox model --
@staticmethod
def rank_for(status, settled, unread):
# `settled` outranks `done`: settling is an explicit "I'm finished with
# this thread". Fresh activity clears it (working/blocked transition),
# so anything genuinely new still surfaces.
if status == "blocked":
return RANK_BLOCKED
if unread:
return RANK_ATTENTION
if status == "working":
return RANK_WORKING
if settled:
return RANK_SETTLED
if status == "done":
return RANK_ATTENTION
if status == "idle":
return RANK_IDLE
return RANK_UNKNOWN
def assert_view(self):
try:
herdr_request(
"agent.view.set",
{"source": SOURCE, "label": "Inbox", "sort": VIEW_SORT},
)
self.log("agent view asserted")
except (OSError, RuntimeError) as e:
self.log("agent.view.set failed: %s" % e)
def _cfg_mtime(self):
try:
return os.path.getmtime(config_file())
except OSError:
return 0
def _maybe_reload_config(self):
m = self._cfg_mtime()
if m == self.cfg_mtime:
return
self.cfg_mtime = m
old = self.cfg
self.cfg = load_config()
if (old["title_source"], old["summarize"], old["summarize_cmd"]) != (
self.cfg["title_source"], self.cfg["summarize"], self.cfg["summarize_cmd"]
):
self.log("config changed: source=%s summarize=%s — retitling all"
% (self.cfg["title_source"], self.cfg["summarize"]))
with self.lock:
for st in self.terminals.values():
st["title"] = None
st["title_tried"] = 0
st["sum_hash"] = None
self.sum_failed.clear()
def _archive_chat(self, st, now):
"""Archive st's current chat: into its in-pane history (capped 10,
drives the tree's ⚫ rows) and the durable history.jsonl (drives the
ChatGPT-style history browser, resumable via stored session refs)."""
hist = list(st.get("history") or [])
# A pane archived on "gone" that reappears and closes for real would
# archive the same chat twice — skip unchanged re-archives.
fingerprint = (st.get("sess_ref"), st.get("title"))
if fingerprint == tuple(st.get("_last_archived") or ()):
return hist
if st.get("title"):
st["_last_archived"] = list(fingerprint)
entry = {
"agent": st.get("agent"),
"title": st["title"],
"closed": now,
"first_seen": st.get("first_seen"),
"workspace_id": st.get("ws"),
"workspace": st.get("ws_label"),
"pane_id": st.get("pane_id"),
"cwd": st.get("cwd"),
"sess_kind": st.get("sess_kind"),
"sess_value": st.get("sess_value"),
}
hist.append({"agent": entry["agent"], "title": entry["title"],
"closed": now})
self._append_history(entry)
return hist[-10:]
def _append_history(self, entry):
path = os.path.join(self.dir, "history.jsonl")
try:
if os.path.exists(path) and os.path.getsize(path) > 400_000:
with open(path) as f:
tail = f.readlines()[-1000:]
tmp = path + ".tmp"
with open(tmp, "w") as f:
f.writelines(tail)
os.replace(tmp, path) # atomic — readers never see a partial file
with open(path, "a") as f:
f.write(json.dumps(entry) + "\n")
except OSError as e:
self.log("history append failed: %s" % e)
def _ensure_title(self, rec, st, now):
"""Generate/refresh the session title for one agent record."""
sess = rec.get("agent_session") or {}
sess_ref = "%s:%s" % (sess.get("kind"), sess.get("value"))
if sess.get("value") and st.get("sess_ref") not in (None, sess_ref):
# The pane started a NEW native session — archive the old chat.
st["history"] = self._archive_chat(st, now)
if sess.get("value"):
st["sess_ref"] = sess_ref
st["sess_kind"] = sess.get("kind")
st["sess_value"] = sess.get("value")
sess_key = "%s:%s:%s:%s:%s" % (sess.get("kind"), sess.get("value"),
self.cfg["title_source"],
self.cfg["prefer_agent_title"],
TITLE_ALGO)
fresh = st.get("title_sess") == sess_key
if st.get("title_manual") and st.get("title"):
if fresh:
return
st["title_manual"] = False # new session ref supersedes manual title
path = resolve_transcript(rec)
# The agent's own name for the session is checked EVERY tick, before
# any cached-title shortcut: an agent renames its session as work
# evolves, and a session can gain a name long after it started.
# The ambiguity guard applies ONLY to cwd-keyed agents — claude and pi
# are looked up by their own transcript, so several of them in one
# directory are fine.
cwd_keyed = rec.get("agent") in CWD_KEYED_AGENTS
if self.cfg["prefer_agent_title"] \
and not (cwd_keyed
and (rec.get("agent"), rec.get("cwd")) in self.ambiguous):
native = native_title_for(rec, path)
if native:
st["title_sess"] = sess_key
st["title_native"] = True
st["title_stale"] = False
if native != st.get("title"):
st["title"] = native
self.log("title (agent) %s -> %r" % (rec.get("pane_id"), native))
return
if st.get("title_native"):
# The agent's title went away (cleared) — re-derive our own.
st["title_native"] = False
st["title"] = None
if st.get("title") and fresh and not st.get("title_stale"):
return
# Retry transcripts at most once per tick; they appear shortly after
# the first prompt is sent.
if st.get("title_tried", 0) > now - (TICK_SECS - 1) and fresh and not st.get("title_stale"):
return
st["title_tried"] = now
if not fresh:
st["title_sess"] = sess_key
st["title"] = None
st["sum_hash"] = None
st["title_stale"] = False
if not path:
if not st.get("title"):
st["title"] = None
return
# Native titles were already handled above; derive from prompts only.
title, raw = title_from_transcript(path, self.cfg["title_source"],
prefer_native=False)
if not title:
return
summarize = bool(self.cfg["summarize"] and self.cfg["summarize_cmd"] and raw)
if not summarize:
if title != st.get("title"):
st["title"] = title
self.log("title %s -> %r" % (rec.get("pane_id"), title))
return
h = hashlib.sha1(raw.encode()).hexdigest()[:12]
if st.get("sum_hash") == h and st.get("title"):
return # already summarized this content
if not st.get("title"):
st["title"] = title # heuristic placeholder until the summary lands
tid = rec.get("terminal_id")
if h in self.sum_failed or (tid, h) in self.sum_inflight:
return
self.sum_inflight.add((tid, h))
self.sum_q.put((tid, h, rec.get("agent"), rec.get("cwd"), raw))
def summarize_loop(self):
env = dict(os.environ)
env["PATH"] = env.get("PATH", "") + ":/opt/homebrew/bin:/usr/local/bin"
while not self.stop.is_set():
try:
tid, h, agent, cwd, raw = self.sum_q.get(timeout=1.0)
except queue.Empty:
continue
payload = "Agent: %s\nWorkdir: %s\n\nRequest:\n%s" % (agent, cwd, raw)
title = None
try:
r = subprocess.run(
self.cfg["summarize_cmd"], shell=True, env=env,
input=payload, capture_output=True, text=True,
timeout=self.cfg["summarize_timeout_secs"],
)
if r.returncode == 0:
title = _clean_title(r.stdout.strip().splitlines()[0] if r.stdout.strip() else "")
else:
self.log("summarize_cmd rc=%d: %s" % (r.returncode, (r.stderr or "")[:200]))
except (OSError, subprocess.TimeoutExpired) as e:
self.log("summarize failed: %s" % e)
with self.lock:
self.sum_inflight.discard((tid, h))
st = self.terminals.get(tid)
if title and st:
st["title"] = title
st["sum_hash"] = h
self.log("summary %s -> %r" % (tid, title))
self.dirty.set()
elif not title:
self.sum_failed.add(h)
def refresh(self):
now = time.time()
self._maybe_reload_config()
try:
agents = herdr_request("agent.list", {}).get("agents", [])
except (OSError, RuntimeError, ValueError) as e:
self.log("agent.list failed: %s" % e)
return
ws_labels = {}
try:
for wsr in herdr_request("workspace.list", {}).get("workspaces", []):
ws_labels[wsr.get("workspace_id")] = wsr.get("label")
except (OSError, RuntimeError, ValueError):
pass
pending = []
ws_pending = []
tab_best = {}
seen_pairs = {}
for rec in agents:
key = (rec.get("agent"), rec.get("cwd"))
seen_pairs[key] = seen_pairs.get(key, 0) + 1
self.ambiguous = {k for k, n in seen_pairs.items() if n > 1}
with self.lock:
self.pane_to_tid = {}
seen_tids = set()
ws_agents = {}
for rec in agents:
tid = rec.get("terminal_id")
pane_id = rec.get("pane_id")
if not tid or not pane_id:
continue
seen_tids.add(tid)
self.pane_to_tid[pane_id] = tid
st = self.terminals.get(tid)
if st is None or st.get("agent") != rec.get("agent"):
old = st
st = {
"agent": rec.get("agent"),
"first_seen": now,
"last_status": rec.get("agent_status"),
"last_change": now,
"settled": False,
"unread": False,
"flagged_at": 0,
"title": None,
"title_sess": None,
"title_tried": 0,
}
if old: