"""lane-nudge decides from idle time, running state, cadence status and its own rate limit.""" from importlib.machinery import SourceFileLoader from pathlib import Path import json from datetime import date, timedelta import pytest REPO = Path(__file__).resolve().parents[1] nudge = SourceFileLoader('lane_nudge', str(REPO / 'harness/tools/lane-nudge/lane-nudge')).load_module() NOW = 1_000_000.0 @pytest.mark.parametrize('state', ['retired', 'ended', 'held']) def test_registry_skips_alias_without_a_skip_list_or_cadence_help(tmp_path, monkeypatch, capsys, state): lanes = tmp_path / 'lanes' lanes.mkdir() row = {'status': 'active' if state == 'ended' else state, 'why': 'finished challenge', 'sessions': ['closedb']} if state == 'ended': row['end'] = str(date.today() - timedelta(days=1)) (lanes / 'REGISTRY.json').write_text(json.dumps({'lanes': {'closed': row}})) monkeypatch.setattr(nudge.sys, 'argv', ['lane-nudge', '--lanes', str(lanes), '--skip', '', '--state', str(tmp_path / 'state'), '--log', str(tmp_path / 'log')]) monkeypatch.setattr(nudge, 'cadence', lambda _: ({'closedb': {'status': 'OK'}}, None)) monkeypatch.setattr(nudge, 'panes', lambda *_: [('closedb', '%1', '/work')]) def unexpected(*args, **kwargs): pytest.fail('inactive session reached transcript scan or tmux') monkeypatch.setattr(nudge, 'idle_minutes', unexpected) monkeypatch.setattr(nudge.subprocess, 'run', unexpected) nudge.main() assert 'closedb: skipped (closed: ' + state in capsys.readouterr().out assert 'finished challenge' in (tmp_path / 'log').read_text() def test_retired_session_is_skipped_even_when_cadence_is_unavailable(tmp_path, monkeypatch, capsys): path = tmp_path / 'registry.json' path.write_text(json.dumps({'lanes': {'closed': {'status': 'retired', 'sessions': ['closedb']}}})) monkeypatch.setenv('RH_LANE_REGISTRY', str(path)) monkeypatch.setattr(nudge.sys, 'argv', ['lane-nudge', '--state', str(tmp_path), '--skip', '', '--dry']) monkeypatch.setattr(nudge, 'cadence', lambda _: (None, 'offline')) monkeypatch.setattr(nudge, 'panes', lambda *_: [('closedb', '%1', '/work')]) monkeypatch.setattr(nudge, 'idle_minutes', lambda *_: pytest.fail('retired lane scanned')) nudge.main() assert 'retired' in capsys.readouterr().out def test_quiet_lane_with_nothing_running_is_nudged(): assert nudge.decide(90, False, 'OK', 0, NOW) == 'quiet' def test_cadence_behind_is_named(): assert nudge.decide(61, False, 'STALLED', 0, NOW) == 'STALLED on cadence' def test_running_work_or_recent_turn_is_left_alone(): assert nudge.decide(500, True, 'STALLED', 0, NOW) is None assert nudge.decide(59, False, 'WARN', 0, NOW) is None def test_rate_limited_per_lane(): assert nudge.decide(500, False, 'WARN', NOW - 3600, NOW) is None assert nudge.decide(500, False, 'WARN', NOW - 7201, NOW) == 'WARN on cadence' def test_exempt_and_unconfigured_never_nudged(): assert nudge.decide(10_000, False, 'HELD', 0, NOW) is None # paused by its owner (friends, 11-11) assert nudge.decide(10_000, False, 'EXEMPT', 0, NOW) is None assert nudge.decide(10_000, False, 'UNCONFIGURED', 0, NOW) is None def test_running_pattern_matches_claude_code_status_lines(): assert nudge.RUNNING.search('done 2:41 PM · 1 shell still running') assert nudge.RUNNING.search('Shimmying… (esc to interrupt)') assert not nudge.RUNNING.search('Worked for 3m 15s · done 10:47 AM') def test_a_failed_cadence_read_is_loud_never_all_clear(tmp_path, capsys): """11-08: a bare `rh` missing from launchd's PATH failed every read for 8 h and every lane read as fine.""" cad, error = nudge.cadence(str(tmp_path / 'no-such-rh')) assert cad is None and 'FileNotFoundError' in error fake = tmp_path / 'rh' fake.write_text('#!/bin/sh\necho "Traceback: boom" >&2\nexit 2\n') fake.chmod(0o755) assert nudge.cadence(str(fake)) == (None, 'exit 2, no JSON: Traceback: boom') behind = tmp_path / 'rh-behind' behind.write_text('#!/bin/sh\necho \'[{"owner_session": "a", "status": "STALLED"}]\'\nexit 1\n') behind.chmod(0o755) assert nudge.cadence(str(behind))[0]['a']['status'] == 'STALLED', 'exit 1 means a lane is behind, not a failed read' sent = tmp_path / 'sent.txt' alert = '/bin/sh -c \'echo "$0" >> {}\''.format(sent) for _ in range(3): nudge.report_failure(error, tmp_path, alert, dry=False) assert 'CADENCE READ FAILED' in capsys.readouterr().err assert sent.read_text().count('CADENCE READ FAILED') == 1, 'one push per failure streak' nudge.report_failure(None, tmp_path, alert, dry=False) assert 'recovered' in sent.read_text() and not (tmp_path / 'cadence-failed.json').exists() def _row(kind, ts, **extra): import json return json.dumps(dict(type=kind, timestamp=ts, message={'role': kind, 'content': 'x'}, **extra)) def test_idle_is_measured_from_the_last_real_turn_not_the_file_mtime(tmp_path): """11-08 to 11-11: idle compaction rewrote every lane transcript about hourly, mtime never aged past 60 minutes, and no lane was nudged for three days. Compaction rows and the untimestamped metadata a session rewrites while idle must not count as activity.""" import json import os from datetime import datetime projects = tmp_path / 'projects' pane = '/Users/x/.worktrees/kb/math' d = projects / '-Users-x--worktrees-kb-math' d.mkdir(parents=True) f = d / 's.jsonl' rows = [ _row('user', '2026-11-08T19:00:00.000Z'), _row('assistant', '2026-11-08T19:21:00.000Z'), json.dumps(dict(type='system', subtype='compact_boundary', timestamp='2026-11-09T22:50:39.000Z')), _row('user', '2026-11-09T22:50:39.000Z', isCompactSummary=True), _row('user', '2026-11-09T22:50:40.000Z', isMeta=True), json.dumps(dict(type='last-prompt', lastPrompt='x')), json.dumps(dict(type='bridge-session', lastSequenceNum=9)), ] f.write_text('\n'.join(rows) + '\n') now = datetime.fromisoformat('2026-11-11T11:21:00+00:00').timestamp() os.utime(f, (now - 30, now - 30)) # the file looks fresh: the old check saw 0 min idle idle = nudge.idle_minutes(pane, projects, now) assert idle == int((now - datetime.fromisoformat('2026-11-08T19:21:00+00:00').timestamp()) / 60) assert nudge.decide(idle, False, 'STALLED', 0, now) == 'STALLED on cadence' with open(f, 'a') as fh: # a peer message or task notification is a real turn fh.write(_row('user', '2026-11-11T11:00:00.000Z') + '\n') assert nudge.idle_minutes(pane, projects, now) == 21 def test_last_turn_reads_past_a_tail_window_with_no_turn(tmp_path): import json f = tmp_path / 't.jsonl' filler = json.dumps(dict(type='file-history-snapshot', snapshot='y' * 500)) f.write_text(_row('assistant', '2026-11-10T00:00:00Z') + '\n' + '\n'.join([filler] * 50) + '\n') assert nudge.last_turn(f, chunk=1024) is not None g = tmp_path / 'none.jsonl' g.write_text(filler + '\n') assert nudge.last_turn(g, chunk=64) is None def test_the_nudge_names_the_newest_unrun_digest_idea(): row = {'digest': {'newest_unrun': {'id': 'abc123', 'claim': 'Run one round as teams of ten agents.'}}} assert 'abc123 (Run one round as teams of ten agents.)' in nudge.pickup(row) assert nudge.pickup({'digest': None}) == '' and nudge.pickup({}) == '' and nudge.pickup(None) == '' def test_a_line_separator_inside_the_last_turn_does_not_hide_it(tmp_path): import json f = tmp_path / 't.jsonl' turn = dict(type='assistant', timestamp='2026-11-12T10:00:00Z', message={'role': 'assistant', 'content': 'a
b'}) f.write_text(json.dumps(turn, ensure_ascii=False) + '\n') assert nudge.last_turn(f) is not None def test_a_result_newer_than_the_last_turn_wakes_a_lane_even_with_its_watcher_running(): """11-12: math's review finished at 16:18Z and the lane sat until 21:05Z. Its watcher shell was running (watching the wrong files), and a running shell made the pane look busy, so it was never nudged. A result newer than the last turn overrides that, once per file.""" why = nudge.decide(20, True, 'OK', NOW - 600, NOW, unread='/jobs/math_rev/astra.md') assert why and 'astra.md' in why assert nudge.decide(20, True, 'OK', NOW - 600, NOW, unread='/jobs/math_rev/astra.md', last_output='/jobs/math_rev/astra.md') is None # already told about this one assert nudge.decide(20, True, 'HELD', 0, NOW, unread='/jobs/x/astra.md') is None # a paused lane stays paused assert nudge.decide(20, True, 'OK', 0, NOW) is None # no result: a watcher is busy def test_unread_output_is_the_newest_settled_result_after_the_last_turn(tmp_path): import os now = 1_000_000.0 def make(name, age): f = tmp_path / name f.parent.mkdir(parents=True, exist_ok=True) f.write_text('x') os.utime(f, (now - age, now - age)) return str(f) make('math_a/astra.md', 7200) # before the last turn: already seen want = make('math_b/astra.md', 3000) # after the last turn, settled make('math_c/astra.md', 60) # still being written make('friends_d/astra.md', 3000) # another lane globs = [str(tmp_path / '{lane}_*/astra.md')] assert nudge.unread_output(globs, 'math', last_turn=now - 5000, now=now) == want assert nudge.unread_output(globs, 'math', last_turn=now - 100, now=now) is None def test_a_lane_blocked_on_the_owner_is_not_nudged_but_a_result_still_wakes_it(tmp_path): """friends, 11-13: blocked on seven owner-only decisions with every job done, and nudged three times in a night to keep working on something else.""" import os lane = tmp_path / 'friends' lane.mkdir() assert nudge.blocked_on(lane, NOW) is None (lane / 'BLOCKED').write_text('7 decisions sent to general 05:30 (ricardo channel)\n') os.utime(lane / 'BLOCKED', (NOW - 3600, NOW - 3600)) reason = nudge.blocked_on(lane, NOW) assert reason.startswith('7 decisions') assert nudge.decide(500, False, 'STALLED', 0, NOW, blocked=reason) is None assert nudge.decide(500, False, 'OK', 0, NOW, unread='/jobs/x.last.md', blocked=reason) # a result still wakes it os.utime(lane / 'BLOCKED', (NOW - 13 * 3600, NOW - 13 * 3600)) assert nudge.blocked_on(lane, NOW) is None # lapses: cannot silence forever