Quick Fix: reopen after a restart only when its own rebuild caused it

The chat reopened on every shell start while a stale "unseen" result was
left over. Now the helper checks the job's unit for a running rebuild
(zcli, nixos-rebuild, nh, home-manager) and only then reopens; other
restarts keep following a running job quietly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mxandClaude Opus 5.5 committed 2026-11-28 01:09:35 +01:00
1 parent f35dbee430
commit 94530d3bd3
4 files changed
+84 -11

No files matched your search

+40 -3
View File
@@ -133,6 +133,36 @@ def unit_active(id_):
return r.stdout.strip() not in ('inactive', 'failed')
REBUILDERS = {'zcli', 'nixos-rebuild', 'nixos-rebuild-ng', 'nh', 'home-manager', 'switch-to-configuration'}
def rebuilding(id_):
"""True when the job's unit is running a system rebuild, i.e. the rebuild
that restarted the shell was started by this job."""
try:
group = subprocess.run(['systemctl', '--user', 'show', f'{UNIT}-{id_}.service', '-p', 'ControlGroup', '--value'],
capture_output=True, text=True, timeout=10).stdout.strip()
except (OSError, subprocess.TimeoutExpired):
return False
root = Path('/sys/fs/cgroup') / group.lstrip('/')
if not group or not root.is_dir():
return False
for procs in root.rglob('cgroup.procs'):
try:
pids = procs.read_text().split()
except OSError:
continue
for pid in pids:
try:
argv = Path(f'/proc/{pid}/cmdline').read_bytes().split(b'\0')
except OSError:
continue
# Scripts run as `bash /nix/store/...-zcli/bin/zcli rebuild`.
if {os.path.basename(a.decode(errors='replace')) for a in argv[:3] if a} & REBUILDERS:
return True
return False
def alive(pid):
if not isinstance(pid, int) or pid <= 0:
return False
@@ -625,7 +655,7 @@ def begin(session, request_messages, action):
session['running'] = True
session['updated'] = int(time.time())
save_session(session)
save_state(current=session['id'], running=True, seen=False, started=int(time.time()))
save_state(current=session['id'], running=True, seen=False, rebuilt=False, started=int(time.time()))
try:
start_unit(session['id'], action)
except (OSError, ValueError) as error:
@@ -637,9 +667,16 @@ def handle(request):
op = request.get('op')
recover()
if op == 'state':
state = read_state()
# Called when the shell starts. Remember if the running job is the one
# rebuilding (and so restarting) the shell: only then does it reopen.
with locked():
state = read_state()
if state.get('running') and state.get('current') and not state.get('rebuilt') and rebuilding(state['current']):
save_state(rebuilt=True)
state['rebuilt'] = True
session = read_json(session_path(state['current'])) if state.get('current') else None
return {'session': session, 'running': bool(state.get('running')), 'seen': bool(state.get('seen', True))}
return {'session': session, 'running': bool(state.get('running')), 'seen': bool(state.get('seen', True)),
'rebuilt': bool(state.get('rebuilt'))}
if op == 'send':
text = str(request.get('text') or '').strip()
if not text or len(text) > 8000: