Quick Fix: don't call a job interrupted while a rebuild reloads systemd

A rebuild restarts the shell mid-job, and the restarted shell asked
systemd whether the job's unit was alive while the user manager was
reloading. The failed query read as "stopped", so the chat showed
"interrupted" and stopped polling, and the real result never appeared.
A job now counts as dead only when systemd says its unit stopped and its
worker process is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mxandClaude Opus 5.5 committed 2026-11-26 00:19:37 +01:00
1 parent 3d976b5961
commit f35dbee430
2 files changed
+40 -4

No files matched your search

+26 -4
View File
@@ -123,8 +123,26 @@ def new_id():
# Units ----------------------------------------------------------------------- # Units -----------------------------------------------------------------------
def unit_active(id_): def unit_active(id_):
r = subprocess.run(['systemctl', '--user', 'is-active', f'{UNIT}-{id_}.service'], capture_output=True, text=True) """False only when systemd says the unit has stopped. While a rebuild
return r.stdout.strip() in ('active', 'activating', 'deactivating') reloads the user manager the query can fail; that counts as running."""
try:
r = subprocess.run(['systemctl', '--user', 'is-active', f'{UNIT}-{id_}.service'],
capture_output=True, text=True, timeout=10)
except (OSError, subprocess.TimeoutExpired):
return True
return r.stdout.strip() not in ('inactive', 'failed')
def alive(pid):
if not isinstance(pid, int) or pid <= 0:
return False
try:
os.kill(pid, 0)
except ProcessLookupError:
return False
except PermissionError:
return True
return True
def start_unit(id_, action): def start_unit(id_, action):
@@ -471,6 +489,8 @@ def system_generation():
def worker(id_): def worker(id_):
with locked():
save_state(pid=os.getpid())
session = load_session(id_) session = load_session(id_)
options = session['options'] options = session['options']
run = session['messages'][-1]['run'] run = session['messages'][-1]['run']
@@ -494,7 +514,7 @@ def worker(id_):
session = load_session(id_) session = load_session(id_)
session['messages'][-1]['provider'] = provider session['messages'][-1]['provider'] = provider
save_session(session) save_session(session)
save_state(current=id_, running=True, seen=False) save_state(current=id_, running=True, seen=False, pid=os.getpid())
record = snapshot(run, options) record = snapshot(run, options)
prompt = transcript(session, provider) + prompt prompt = transcript(session, provider) + prompt
command, env, text = build_command(provider, session, prompt, options, last_file) command, env, text = build_command(provider, session, prompt, options, last_file)
@@ -536,6 +556,8 @@ def worker(id_):
def undo_worker(id_): def undo_worker(id_):
with locked():
save_state(pid=os.getpid())
session = load_session(id_) session = load_session(id_)
run = session['messages'][-1]['undoes'] run = session['messages'][-1]['undoes']
generation = system_generation() generation = system_generation()
@@ -581,7 +603,7 @@ def recover():
if not state.get('running') or not state.get('current'): if not state.get('running') or not state.get('current'):
return return
id_ = state['current'] id_ = state['current']
if time.time() - state.get('started', 0) < 5 or unit_active(id_): if time.time() - state.get('started', 0) < 5 or alive(state.get('pid')) or unit_active(id_):
return return
with locked(): with locked():
if not read_state().get('running'): if not read_state().get('running'):
+14
View File
@@ -129,5 +129,19 @@ class QuickFix(unittest.TestCase):
self.assertFalse(state['seen']) self.assertFalse(state['seen'])
self.assertEqual(state['session']['messages'][-1]['status'], 'failed') self.assertEqual(state['session']['messages'][-1]['status'], 'failed')
def test_a_failed_unit_query_is_not_a_dead_job(self):
# During a rebuild systemctl may not answer; the job is still running.
request = {'op': 'send', 'text': 'x', 'provider': 'claude', 'options': {}}
with patch.object(quickfix, 'start_unit'):
quickfix.handle(request)
quickfix.save_state(started=0)
failing = subprocess.CompletedProcess([], 1, '', 'Failed to connect to bus')
with patch.object(quickfix.subprocess, 'run', return_value=failing):
self.assertTrue(quickfix.handle({'op': 'state'})['running'])
quickfix.save_state(pid=os.getpid())
stopped = subprocess.CompletedProcess([], 3, 'inactive\n', '')
with patch.object(quickfix.subprocess, 'run', return_value=stopped):
self.assertTrue(quickfix.handle({'op': 'state'})['running']) # worker process alive
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()