diff --git a/adapters/local/README.md b/adapters/local/README.md index 556310c6b..9a47c56c0 100644 --- a/adapters/local/README.md +++ b/adapters/local/README.md @@ -22,6 +22,7 @@ adapters/local/driver version # {"driver": "local", ..., "can": [. - **Errors never repeat raw output:** the clone's and every turn's output goes only to the run's owner-only log (`$OPENWIZ_LOCAL_STATE/.log`). A failed clone or first turn answers a fixed reason (not signed in, refused a tool, failed, could not clone), its exit code, and that log's path. - **Runs:** `start` saves the run, then runs its first turn. A run whose first turn began but couldn't be saved is ended, and its error says the outcome is unknown and what to check; it never says retry. `wake` runs another turn on the same ticket, in which the agent edits its own status note. While the run's last turn is still running, `wake` answers `woke: true` and starts no second turn, so one ticket never has two agents. On Linux, `stop` ends every process the run's turns started, whatever their session or environment: a polite signal, then a kill after 10 seconds. Each turn runs under a small supervisor (`driver --turn`) that keeps every process it starts (a Linux subreaper), so a worker in its own session or with a clean environment is still stopped. On macOS, `stop` ends every process it can find, within the gap and the `lsof` fallback above. A wake and a stop on one run take turns under its lock. Several runs at once are independent. - **State:** each run's request, its workspace and each turn's output log, owner-only, in `$OPENWIZ_LOCAL_STATE` (default `~/.local/state/openwiz-local`). +- **Usage limits and overload:** when a turn ends non-zero with a usage-limit or overload message in the last lines of its log (built in for `claude` and `codex`, or via a row's optional `limit` pattern), its supervisor waits out the window (`reset + 15 s + up to 30 s` random jitter, or `60 s` then `120 s` then stop for overload with no reset time) and resumes once with the normal wake prompt on the same harness and model. Resets more than 6 hours away are not waited for; during a wait (or before a >6-hour reset), `wake` refuses under D3 naming the limit and reset time, and `start`'s first-turn check reports a limit the same way. - **On a VPS:** sign in to the harness and to `gh` there, and run the driver there, for example over `ssh`. `schema.json` is byte for byte the kit's `kernel/driver-kit/schema.json`. diff --git a/adapters/local/driver b/adapters/local/driver index 5de276c99..f8d116ca9 100755 --- a/adapters/local/driver +++ b/adapters/local/driver @@ -1,10 +1,12 @@ #!/usr/bin/env python3 import ctypes, fcntl, importlib.machinery, importlib.util, io, json, os, re, secrets, shutil, signal, subprocess, sys, time, uuid +from datetime import datetime, timedelta, timezone import tempfile sys.dont_write_bytecode = True HERE = os.path.dirname(os.path.abspath(__file__)) +SELF = os.path.abspath(__file__) KIT = os.path.join(HERE, '..', '..', 'kernel', 'driver-kit', 'kit.py') STATE = os.path.abspath(os.environ.get('OPENWIZ_LOCAL_STATE') or os.path.expanduser('~/.local/state/openwiz-local')) APPROVALS = os.path.join(STATE, 'approvals.json') @@ -20,7 +22,7 @@ HARNESSES = {'claude': lambda model, mode, prompt: ['-p', prompt, '--model', mod MODE_ROW = '| Harness permission mode |' ROWS = os.path.join(HERE, 'harnesses') ROW_FIELDS = ('harness', 'program', 'start', 'wake', 'session', 'modes', 'env', 'effort', 'login', 'daemon') -OPTIONAL_ROW_FIELDS = ('signed_out', 'login_command', 'login_env', 'precheck', 'stop') +OPTIONAL_ROW_FIELDS = ('signed_out', 'login_command', 'login_env', 'precheck', 'stop', 'limit') PLACEHOLDERS = ('{prompt}', '{model}', '{session}', '{mode}', '{effort}') SESSION = r'[A-Za-z0-9._:-]{1,100}' EFFORT = r'[a-z][a-z0-9-]{0,19}' @@ -48,6 +50,26 @@ STOP_NOTES = [] NO_LSOF = ('driver: lsof could not run or failed, so the driver could not check which processes hold this ' "run's output file open; it ended only the turns' process groups and the processes their " 'supervisors recorded') +LIMIT_TAIL_BYTES = 65536 +LIMIT_TAIL_LINES = 50 +LIMIT_BUFFER_SECONDS = 15 +LIMIT_JITTER_SECONDS = 30 +LIMIT_MAX_WAIT_SECONDS = 6 * 3600 +OVERLOAD_WAITS = (60, 120) +USAGE_LIMIT = re.compile(r'(?i)(?:' + '|'.join(( + r'\b(?:claude(?:\s+ai)?\s+)?usage\s+limit\b', + r"\byou(?:'ve|\s+have)?\s+hit\s+your\s+(?:usage\s+|plan\s+)?limit\b", + r'\b(?:rate|usage)[_\s-]+limit(?:[_\s-]+(?:reached|exceeded))?\b', + r'\btoo\s+many\s+requests\b', + r'\b(?:http\s+|status(?:\s+code)?\s+|api\s+error:?\s*)429\b', +)) + r')') +OVERLOAD = re.compile(r'(?i)(?:' + '|'.join(( + r'\boverloaded_error\b', + r'\b(?:api\s+error:?\s*)?529\b[^\n]*\boverloaded\b', + r'\b(?:server|service|api|provider|model|anthropic|openai)\b[^.\n]{0,40}\boverloaded\b', + r'^[ \t]*(?:[\w .()\[\]-]{0,40}:[ \t]*){0,2}(?:529\s+)?overloaded\b', +)) + r')', re.M) +STOP_SIGNALED = False class Refused(Exception): @@ -237,6 +259,20 @@ def row_commands(row): raise Broken('stop', "it has no {session}, so it could reach other runs' sessions") +def check_limit_pattern(row): + if 'limit' not in row: + return + pattern = row['limit'] + if not plain(pattern): + raise Broken('limit', 'it is not one line with no placeholder and nothing credential-shaped') + try: + matches_empty = re.search(pattern, '', re.I) is not None + except re.error: + raise Broken('limit', 'its pattern does not compile') + if matches_empty: + raise Broken('limit', 'its pattern matches empty output, so it would match any failure') + + def check_row(row, name): if not isinstance(row, dict): raise Broken('fields', 'it is not one JSON object') @@ -291,6 +327,7 @@ def check_row(row, name): raise Broken('start', 'it has {session}, but the session is reported, not chosen') no_credential_arguments(row) row_commands(row) + check_limit_pattern(row) flags = isinstance(effort, dict) and any(isinstance(s, list) for s in effort.values()) if flags and '{effort}' not in row['start']: raise Broken('effort', 'it maps effort values to arguments, but start has no {effort} to pass them') @@ -328,9 +365,11 @@ def program(harness, effort): try: row = read_row(os.path.join(ROWS, harness, 'row.json'), harness) except Broken as e: - raise Refused('%s breaks the harness-row contract (%s: %s); nothing was started' - % (row_file(harness), e.args[0], e.args[1]), - 'fix %s as adapters/local/harness-row.md says, then start again' % row_file(harness)) + error = '%s breaks the harness-row contract (%s: %s); nothing was started' % ( + row_file(harness), e.args[0], e.args[1]) + if len(error) > 300: + error = '%s breaks the harness-row contract (%s); nothing was started' % (row_file(harness), e.args[0]) + raise Refused(error, 'fix %s as adapters/local/harness-row.md says, then start again' % row_file(harness)) if row is None and os.path.isdir(os.path.join(ROWS, harness)): raise Refused('%s is missing, so harness %s has no row; nothing was started' % (row_file(harness), harness), 'add %s as adapters/local/harness-row.md says, then start again' % row_file(harness)) @@ -783,31 +822,357 @@ def descendants(pid): return found +def limit_now(): + return time.time() + + +def limit_jitter(): + return secrets.randbelow(LIMIT_JITTER_SECONDS * 1000 + 1) / 1000.0 + + +def format_reset(ts): + return datetime.fromtimestamp(ts, tz=timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') + + +def limit_file(run): + return os.path.join(STATE, run + '.limit.json') + + +def write_limit_state(run, info): + path = limit_file(run) + try: + fd = os.open(path + '.new', os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) + with os.fdopen(fd, 'w') as f: + json.dump(info, f) + os.replace(path + '.new', path) + except OSError: + pass + + +def read_limit_state(run): + try: + with open(limit_file(run)) as f: + data = json.load(f) + return data if isinstance(data, dict) else None + except (OSError, ValueError): + return None + + +def clear_limit_state(run): + try: + os.remove(limit_file(run)) + except OSError: + pass + + +def turn_tail_lines(run, output_from): + try: + with open(log_path(run), 'rb') as f: + f.seek(0, os.SEEK_END) + size = f.tell() + start = max(output_from or 0, size - LIMIT_TAIL_BYTES) + if start >= size: + return [] + f.seek(start) + text = f.read().decode(errors='replace') + except OSError: + return [] + lines = [l for l in text.splitlines() if l.strip()] + return lines[-LIMIT_TAIL_LINES:] + + +def parse_reset_time(lines, now): + for text in reversed(lines): + m = re.search(r'\b(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}(?::\d{2})?(?:Z|[+-]\d{2}:?\d{2}))\b', text) + if m: + raw = m.group(1).replace('Z', '+00:00') + if re.search(r'[+-]\d{4}$', raw): + raw = raw[:-2] + ':' + raw[-2:] + try: + return datetime.fromisoformat(raw).timestamp() + except ValueError: + pass + m = re.search(r'\b(\d{4}-\d{2}-\d{2})[ T](\d{1,2}):(\d{2})(?::(\d{2}))?\s*(?:UTC|GMT)\b', text, re.I) + if m: + try: + dt = datetime(int(m.group(1)[:4]), int(m.group(1)[5:7]), int(m.group(1)[8:10]), + int(m.group(2)), int(m.group(3)), int(m.group(4) or 0), tzinfo=timezone.utc) + return dt.timestamp() + except ValueError: + pass + m = re.search(r'(?:\||resets?(?:\s+at)?\s+|try\s+again\s+at\s+)(\d{10})\b', text, re.I) + if m and int(m.group(1)) >= 1700000000: + return float(int(m.group(1))) + m = re.search( + r'(?:resets?|try\s+again|retry|wait)\s+(?:in|after)\s+' + r'((?:\d+\s*(?:hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)(?:\s*(?:,|and)?\s*)?)+)', + text, re.I) + if m: + total = 0 + for amount, unit in re.findall(r'(\d+)\s*(hours?|hrs?|h|minutes?|mins?|m|seconds?|secs?|s)', m.group(1), re.I): + u = unit.lower()[0] + total += int(amount) * (3600 if u == 'h' else 60 if u == 'm' else 1) + if total > 0: + return now + float(total) + m = re.search( + r'(?:resets?|try\s+again|available)(?:\s+at)?\s+(?:[A-Z][a-z]{2}\s+\d{1,2},?\s+)?' + r'(\d{1,2})(?::(\d{2}))?\s*(am|pm)?(?:\s*\(?\s*(UTC|GMT)\s*\)?)?\b', + text, re.I) + if m and (m.group(2) is not None or m.group(3) is not None or m.group(4) is not None): + hour, minute, ampm = int(m.group(1)), int(m.group(2) or 0), (m.group(3) or '').lower() + if ampm == 'pm' and hour < 12: + hour += 12 + elif ampm == 'am' and hour == 12: + hour = 0 + if 0 <= hour <= 23 and 0 <= minute <= 59: + base = datetime.fromtimestamp(now, tz=timezone.utc) + cand = base.replace(hour=hour, minute=minute, second=0, microsecond=0) + if cand.timestamp() <= now: + cand = cand + timedelta(days=1) + return cand.timestamp() + return None + + +def detect_limit(lines, row=None, now=None): + if not lines: + return None + now = limit_now() if now is None else now + matched_kind = None + if row and row.get('limit'): + try: + pat = re.compile(row['limit'], re.I) + except re.error: + pat = None + if pat and any(pat.search(l) for l in lines): + matched_kind = 'overload' if any(OVERLOAD.search(l) for l in lines) else 'limit' + if matched_kind is None: + if any(USAGE_LIMIT.search(l) for l in lines): + matched_kind = 'limit' + elif any(OVERLOAD.search(l) for l in lines): + matched_kind = 'overload' + if matched_kind is None: + return None + reset_at = parse_reset_time(lines, now) + if matched_kind == 'overload' and reset_at is not None and any(USAGE_LIMIT.search(l) for l in lines): + matched_kind = 'limit' + return {'kind': matched_kind, 'reset_at': reset_at, + 'too_long': reset_at is not None and (reset_at - now) > LIMIT_MAX_WAIT_SECONDS} + + +def limit_refusal(run, info, from_start=False, code=None): + what = 'provider overload' if info.get('kind') == 'overload' else 'usage limit' + reset_iso = format_reset(info['reset_at']) if info.get('reset_at') is not None else None + wake_iso = format_reset(info['wake_at']) if info.get('wake_at') is not None else None + if from_start: + exit_part = ' (exit %s)' % ('unknown' if code is None else code) + if reset_iso: + err = 'run %s: the first turn hit a %s; resets at %s%s' % (run, what, reset_iso, exit_part) + if info.get('too_long'): + nxt = 'reset is more than 6 hours away; wake or start again after %s' % reset_iso + else: + base = 'wait until after %s, then start again' % reset_iso + full = "%s; %s has the turn's output" % (base, log_path(run)) + nxt = full if len(full) <= 300 else base + else: + err = 'run %s: the first turn hit %s%s' % (run, what, exit_part) + base = 'wait a minute and start again' + full = "%s; %s has the turn's output" % (base, log_path(run)) + nxt = full if len(full) <= 300 else base + return err, nxt + if info.get('waiting'): + if reset_iso: + return ('run %s is waiting out a %s; resets at %s' % (run, what, reset_iso), + 'the run resumes on its own after %s (around %s); no wake needed' % (reset_iso, wake_iso or reset_iso)) + return ('run %s is waiting out %s until %s' % (run, what, wake_iso), + 'the run resumes on its own at %s; no wake needed' % wake_iso) + if info.get('too_long') and reset_iso: + return ('run %s hit a %s; resets at %s (more than 6 hours away)' % (run, what, reset_iso), + 'wake the run after %s' % reset_iso) + return ('run %s hit a %s' % (run, what), 'wait and wake the run again') + + +def limit_stop_requested(run): + if STOP_SIGNALED: + return True + try: + return bool(saved(run).get('stopped')) + except (OSError, ValueError, Refused): + return False + + +def limit_sleep(seconds, run): + end_at = time.monotonic() + max(0.0, seconds) + while time.monotonic() < end_at: + if limit_stop_requested(run): + return False + time.sleep(min(0.2, max(0.01, end_at - time.monotonic()))) + return not limit_stop_requested(run) + + +def check_limit_before_wake(run): + info = read_limit_state(run) + if not info: + return + if info.get('waiting'): + sup, sup_since = info.get('supervisor'), info.get('supervisor_since') + if sup is not None and sup_since is not None and since(sup) == sup_since: + raise Refused(*limit_refusal(run, info, from_start=False)) + clear_limit_state(run) + return + if info.get('too_long') and info.get('reset_at') is not None: + if limit_now() < info['reset_at']: + raise Refused(*limit_refusal(run, info, from_start=False)) + clear_limit_state(run) + + +def record_resumed_turn(run, harness_pid, output_from): + try: + lock_fd = os.open(os.path.join(STATE, run + '.lock'), os.O_RDONLY | os.O_CREAT, 0o600) + try: + fcntl.flock(lock_fd, fcntl.LOCK_EX) + state = saved(run) + turn = {'pid': harness_pid, 'since': since(harness_pid), 'supervisor': os.getpid(), + 'supervisor_since': since(os.getpid()), 'output_from': output_from} + saved(run, dict(state, turns=state['turns'] + [turn])) + finally: + os.close(lock_fd) + except (OSError, ValueError, Refused): + pass + + +def next_resume_after_limit(run, output_from, exit_code, counts): + if exit_code in (None, 0) or limit_stop_requested(run): + return None + try: + state = saved(run) + except (OSError, ValueError, Refused): + return None + if state.get('starting') or state.get('stopped'): + return None + info = detect_limit(turn_tail_lines(run, output_from), state.get('row')) + if info is None: + return None + now = limit_now() + if info['too_long']: + write_limit_state(run, dict(info, waiting=False, supervisor=os.getpid(), supervisor_since=since(os.getpid()))) + return None + if info['reset_at'] is not None: + if counts['limit'] >= 1: + clear_limit_state(run) + return None + counts['limit'] += 1 + wake_at = max(now, info['reset_at']) + LIMIT_BUFFER_SECONDS + limit_jitter() + wait_s = max(0.0, wake_at - now) + elif info['kind'] == 'overload': + if counts['overload'] >= len(OVERLOAD_WAITS): + clear_limit_state(run) + return None + wait_s = float(OVERLOAD_WAITS[counts['overload']]) + counts['overload'] += 1 + wake_at = now + wait_s + else: + if counts['limit'] >= 1: + clear_limit_state(run) + return None + counts['limit'] += 1 + wait_s = float(OVERLOAD_WAITS[0]) + wake_at = now + wait_s + write_limit_state(run, dict(info, waiting=True, wake_at=wake_at, + supervisor=os.getpid(), supervisor_since=since(os.getpid()))) + try: + log = open_log(run) + try: + os.write(log, ('driver: %s; waiting until %s to resume on the same model\n' + % (info['kind'], format_reset(wake_at))).encode()) + finally: + os.close(log) + except OSError: + pass + if not limit_sleep(wait_s, run): + clear_limit_state(run) + return None + clear_limit_state(run) + try: + lock_fd = os.open(os.path.join(STATE, run + '.lock'), os.O_RDONLY | os.O_CREAT, 0o600) + try: + fcntl.flock(lock_fd, fcntl.LOCK_EX) + state = saved(run) + if state.get('stopped'): + return None + if state.get('row') and state.get('session') is None: + state['session'] = reported_session(run, state['row']['session'], state['turns'][0]) + if state['session'] is None: + return None + saved(run, state) + return command(state, True) + finally: + os.close(lock_fd) + except (OSError, ValueError, Refused): + return None + + +def _on_stop_signal(_sig, _frame): + global STOP_SIGNALED + STOP_SIGNALED = True + + def supervise(cmd): # SECURITY: the row's env and effort setting go to the harness only; the supervisor and its process tools keep # the driver's own environment. + run = os.environ['OPENWIZ_RUN'] overlay = json.loads(os.environ.pop('OPENWIZ_TURN_ENV', '{}')) + output_from = int(os.environ.pop('OPENWIZ_OUTPUT_FROM', '0') or '0') if LINUX and ctypes.CDLL(None, use_errno=True).prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0, 0) != 0: print(json.dumps({'error': 'this host cannot keep a turn\'s processes together'}), flush=True) return try: harness = subprocess.Popen(cmd, stdin=subprocess.DEVNULL, stdout=sys.stderr, stderr=sys.stderr, - cwd=work(os.environ['OPENWIZ_RUN']), env=dict(os.environ, **overlay)) + cwd=work(run), env=dict(os.environ, **overlay)) except OSError as e: print(json.dumps({'error': e.strerror}), flush=True) return - signal.signal(signal.SIGTERM, signal.SIG_IGN) + signal.signal(signal.SIGTERM, _on_stop_signal) print(json.dumps({'pid': harness.pid, 'since': since(harness.pid)}), flush=True) os.dup2(os.open(os.devnull, os.O_WRONLY), 1) if not LINUX: os.dup2(os.open(os.devnull, os.O_WRONLY), 2) - return look_after_the_turn_on_macos(os.environ['OPENWIZ_RUN'], harness.pid, log_path(os.environ['OPENWIZ_RUN'])) + counts = {'limit': 0, 'overload': 0} while True: + exit_code = None + if not LINUX: + look_after_the_turn_on_macos(run, harness.pid, log_path(run)) + try: + with open(exit_file(run, harness.pid)) as f: + exit_code = int(f.read()) + except (OSError, ValueError): + exit_code = None + else: + while True: + try: + pid, status = os.wait() + except ChildProcessError: + break + if pid == harness.pid: + exit_code = os.waitstatus_to_exitcode(status) + saved_exit(pid, status, harness.pid) + resumed = next_resume_after_limit(run, output_from, exit_code, counts) + if not resumed: + return + next_cmd, next_overlay = resumed try: - pid, status = os.wait() - except ChildProcessError: + log = open_log(run) + output_from = os.fstat(log).st_size + except OSError: + log, output_from = subprocess.DEVNULL, 0 + try: + harness = subprocess.Popen(next_cmd, stdin=subprocess.DEVNULL, stdout=log, stderr=log, + cwd=work(run), env=dict(os.environ, **next_overlay)) + except OSError: return - saved_exit(pid, status, harness.pid) + finally: + if log != subprocess.DEVNULL: + os.close(log) + record_resumed_turn(run, harness.pid, output_from) def saved_exit(pid, status, harness): @@ -855,8 +1220,9 @@ def launch(state, run, woken): except OSError: log, output_from = subprocess.DEVNULL, 0 try: - sup = subprocess.Popen([sys.executable, '-I', os.path.abspath(__file__), '--turn'] + cmd, - env=dict(os.environ, OPENWIZ_RUN=run, OPENWIZ_TURN_ENV=json.dumps(extra)), + sup = subprocess.Popen([sys.executable, '-I', SELF, '--turn'] + cmd, + env=dict(os.environ, OPENWIZ_RUN=run, OPENWIZ_TURN_ENV=json.dumps(extra), + OPENWIZ_OUTPUT_FROM=str(output_from)), start_new_session=True, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=log) with sup.stdout: @@ -883,7 +1249,7 @@ def running(turn): def end(turns, run): - kept = [(t['supervisor'], t['supervisor_since']) for t in turns if t and t['supervisor_since'] is not None] + kept = list(dict.fromkeys((t['supervisor'], t['supervisor_since']) for t in turns if t and t['supervisor_since'] is not None)) if lsof_failed_in(run, kept): LSOF_COULD_NOT_CHECK.append(run) for sig, wait in ((signal.SIGTERM, GRACE), (signal.SIGKILL, 2)): @@ -893,7 +1259,10 @@ def end(turns, run): if not kept: return if not LINUX: - for p in set(members({s for s, _ in kept}, recorded_by(run, kept), writing_to(log_path(run)))) - sent: + targets = set(members({s for s, _ in kept}, recorded_by(run, kept), writing_to(log_path(run)))) + if not targets: + targets = {s for s, _ in kept} + for p in targets - sent: try: os.kill(p, sig) sent.add(p) @@ -901,13 +1270,15 @@ def end(turns, run): pass time.sleep(0.1) continue + under = set().union(*(descendants(s) for s, _ in kept)) + idle = {s for s, _ in kept if not descendants(s)} pinned = {} - for p in set().union(*(descendants(s) for s, _ in kept)) - sent: + for p in (under | idle) - sent: try: pinned[p] = os.pidfd_open(p) except OSError: pass - under = set().union(*(descendants(s) for s, _ in kept)) + under = set().union(*(descendants(s) for s, _ in kept)) | {s for s, t in kept if since(s) == t and not descendants(s)} for p, fd in pinned.items(): try: if p in under: @@ -972,6 +1343,11 @@ def ended_at_once(run, turn, row=None, precheck_output=None): except OSError: log = '' signed_out = SIGNED_OUT.search(log) or signed_out_by_row(row, turn_output(run, turn)) + if not signed_out and not REFUSAL.search(log) and code != 0: + limit_info = detect_limit(turn_tail_lines(run, turn.get('output_from', 0)), row) + if limit_info: + end([turn], run) + raise Refused(*limit_refusal(run, limit_info, from_start=True, code=code)) why = ('the harness is not signed in' if signed_out else 'the harness refused a tool' if REFUSAL.search(log) else None if code == 0 else 'the harness failed') if why is None: @@ -1089,6 +1465,7 @@ def answer(cmd): os.remove(name) except OSError: pass + clear_limit_state(run) if not kept: raise Refused('run %s has ended, but the driver could not save that it is stopped' % run, WRITABLE + ', then stop it again') @@ -1103,6 +1480,7 @@ def answer(cmd): if state['session'] is None: raise Refused('run %s has no session id from its first turn yet; nothing was started' % run, 'wait until its first turn has printed its session id, then wake again') + check_limit_before_wake(run) busy = state['turns'] and running(state['turns'][-1]) if found or not busy: try: diff --git a/adapters/local/fixtures/fake-harness b/adapters/local/fixtures/fake-harness index 69a54d5b1..ef01455a1 100755 --- a/adapters/local/fixtures/fake-harness +++ b/adapters/local/fixtures/fake-harness @@ -17,6 +17,22 @@ skill = re.search(r'with the ([a-z0-9-]+) skill', prompt).group(1) woken = prompt.startswith('You were woken') if not worker: + if os.environ.get('FAKE_MODELS'): + with open(os.environ['FAKE_MODELS'], 'a') as f: + f.write('%s %s %s\n' % (name, model, 'woken' if woken else 'first')) + step = None + if os.environ.get('FAKE_LIMIT_QUEUE'): + qpath = os.environ['FAKE_LIMIT_QUEUE'] + with open(qpath) as f: + steps = json.load(f) + if steps: + step = steps.pop(0) + with open(qpath + '.new', 'w') as f: + json.dump(steps, f) + os.replace(qpath + '.new', qpath) + if step and step.get('immediate'): + print(step['line'], flush=True) + sys.exit(step.get('exit', 1)) quick = {'refuse': ('Fake harness: permission to use Bash was denied (%s).' % OPAQUE, 0), 'expired': ('Fake harness: OAuth session expired (%s). Please run /login.' % OPAQUE, 0), 'fail': ('Fake harness: it stopped with an error (%s).' % OPAQUE, 3), 'quick': ('Fake harness: nothing to do.', 0)} @@ -38,6 +54,14 @@ if not worker: time.sleep(0.05) while os.environ.get('FAKE_HOLD') and os.path.exists(os.environ['FAKE_HOLD']): time.sleep(0.05) + if step and step.get('exit', 0) != 0: + if step.get('post_note'): + subprocess.run([sys.executable, sys.argv[0], '--worker'] + args, + env=dict(os.environ, FAKE_TURN='0'), stdin=subprocess.DEVNULL, check=True) + child.kill() + child.wait() + print(step['line'], flush=True) + sys.exit(step['exit']) subprocess.Popen([sys.executable, sys.argv[0], '--worker'] + args, start_new_session=True, env=dict(os.environ, FAKE_TURN=str(os.getpid())), stdin=subprocess.DEVNULL) sys.exit(0) diff --git a/adapters/local/harness-row.md b/adapters/local/harness-row.md index 99ad58ce2..11060fd96 100644 --- a/adapters/local/harness-row.md +++ b/adapters/local/harness-row.md @@ -22,6 +22,7 @@ A row is `adapters/local/harnesses//row.json`, read with standard-libra | `login_env` (optional; needs `login_command`) | The NAME of one environment variable the user may set for the CLI instead of signing in, such as `EXAMPLE_CLI_API_KEY`; never its value | | `precheck` (optional) | `{"run": [, ...], "match": ""}`: a read-only command the driver runs before the first turn, such as listing a model's effort levels, and the fixed pattern its output must match for the start to go ahead | | `stop` (optional) | `[, ...]` holding `{session}`: the command the driver runs to end the run's own session before it ends the run's process tree, such as a CLI's interrupt for a session that lives in its own backend | +| `limit` (optional) | One-line case-insensitive regular expression (no placeholders, nothing credential-shaped, must not match empty output) matching a usage-limit or overload message in the last lines of a non-zero turn's log so the supervisor waits and resumes once on the same harness and model | - **Placeholders.** A placeholder fills a whole argument, never part of one (`--model={model}` breaks the row), and appears at most once in a list. `{prompt}`, `{model}` and `{session}` each become one argument. `{mode}` splices the approved mode's fixed arguments, and `{effort}` the requested effort's (section 4). A fixed argument holds no placeholder. - **Start** holds `{prompt}`, `{model}` and `{mode}`. **Wake** holds `{prompt}`, `{mode}` and `{session}`: wake must hold `{session}`. Start holds `{session}` only when the session is chosen. diff --git a/adapters/local/test_driver.py b/adapters/local/test_driver.py index 293ea2c85..fd23639e6 100755 --- a/adapters/local/test_driver.py +++ b/adapters/local/test_driver.py @@ -48,6 +48,28 @@ def broken(*args): ''' +TIMED_DRIVER = '''import importlib.machinery, importlib.util, os, sys, time +sys.dont_write_bytecode = True +spec = importlib.util.spec_from_loader('local', importlib.machinery.SourceFileLoader('local', %r)) +t = importlib.util.module_from_spec(spec) +spec.loader.exec_module(t) +t.SELF = os.path.abspath(__file__) +t.limit_now = lambda: float(os.environ['FAKE_NOW']) +t.limit_jitter = lambda: float(os.environ.get('FAKE_JITTER', '5')) +def fake_sleep(seconds, run): + with open(os.environ['FAKE_WAITS'], 'a') as f: + f.write('%%.1f\\n' %% seconds) + hold = os.environ.get('FAKE_HOLD_WAIT') + while hold and os.path.exists(hold): + if t.limit_stop_requested(run): + return False + time.sleep(0.05) + return not t.limit_stop_requested(run) +t.limit_sleep = fake_sleep +t.main() +''' + + def alive(pid): try: with open('/proc/%d/stat' % pid) as f: @@ -616,5 +638,173 @@ def test_padded_row_labels_count_as_the_same_row(self): self.until(lambda: len(self.logged()) and self.logged()[-1][4] == mode) self.assertEqual([t[4] for t in self.logged()], ['acceptEdits', 'workspace-write']) + def timed_driver(self): + path = os.path.join(self.folder, 'timed-driver') + with open(path, 'w') as f: + f.write('#!%s\n' % sys.executable + TIMED_DRIVER % DRIVER) + os.chmod(path, 0o700) + return path + + def limit_env(self, steps, now=1795460000, hold_wait=None, jitter='5'): + queue = os.path.join(self.folder, 'limit-queue.json') + waits = os.path.join(self.folder, 'waits') + models = os.path.join(self.folder, 'models') + with open(queue, 'w') as f: + json.dump(steps, f) + env = dict(self.env, FAKE_LIMIT_QUEUE=queue, FAKE_NOW=str(now), FAKE_WAITS=waits, + FAKE_MODELS=models, FAKE_JITTER=jitter) + if hold_wait: + env['FAKE_HOLD_WAIT'] = hold_wait + return env, waits, models + + def test_usage_limit_waits_and_resumes_once_on_same_model(self): + reset_iso = '2026-11-23T18:55:20Z' + env, waits, models = self.limit_env([ + {'exit': 1, 'post_note': True, + 'line': 'Claude AI usage limit reached. Resets at %s (%s)' % (reset_iso, OPAQUE)}, + {'exit': 0}, + ]) + prog = self.timed_driver() + code, out = self.start('claude', env=env, program=prog) + self.assertEqual(code, 0, out) + self.until(lambda: len(self.logged()) == 2) + self.until(lambda: 'woke and read the ticket again' in read(self.tracker)) + comments = json.loads(read(self.tracker))['comments'] + self.assertEqual(len(comments), 1, 'status note was duplicated instead of edited') + self.assertIn('model: synthetic-1', comments[0]['body']) + self.assertEqual(read(models).splitlines(), ['claude synthetic-1 first', 'claude synthetic-1 woken']) + self.assertEqual(read(waits).splitlines(), ['140.0']) + self.assertEqual(self.driver('stop', out, env, prog), (0, dict(out, stopped=True))) + + self.tearDown() + self.setUp() + env, waits, models = self.limit_env([ + {'exit': 1, 'post_note': True, 'line': 'Claude AI usage limit reached. Resets at %s' % reset_iso}, + {'exit': 1, 'line': 'Claude AI usage limit reached. Resets at %s' % reset_iso}, + {'exit': 0}, + ]) + prog = self.timed_driver() + code, out = self.start('codex', env=env, program=prog) + self.assertEqual(code, 0, out) + self.until(lambda: len(self.logged()) == 2) + with open(os.path.join(env['OPENWIZ_LOCAL_STATE'], out['run'] + '.json')) as f: + sup = json.load(f)['turns'][0]['supervisor'] + self.until(lambda: not alive(sup)) + self.assertEqual(len(self.logged()), 2) + self.assertEqual(read(models).splitlines(), ['codex synthetic-1 first', 'codex synthetic-1 woken']) + self.assertEqual(read(waits).splitlines(), ['140.0']) + + def test_wake_during_limit_wait_refuses_with_reset_time_and_stop_ends_wait(self): + reset_iso = '2026-11-23T18:55:20Z' + hold = os.path.join(self.folder, 'hold-wait') + open(hold, 'w').close() + env, waits, _ = self.limit_env([ + {'exit': 1, 'post_note': True, + 'line': 'You have hit your usage limit. Try again at %s (%s)' % (reset_iso, OPAQUE)}, + {'exit': 0}, + ], hold_wait=hold) + prog = self.timed_driver() + code, out = self.start('codex', env=env, program=prog) + self.assertEqual(code, 0, out) + self.until(lambda: os.path.exists(waits) and len(read(waits).splitlines()) == 1) + wcode, err = self.driver('wake', out, env, prog) + self.assertEqual(wcode, 1, err) + self.assertIsNone(kit.invalid(err, kit.load_schema()['error']), err) + self.assertIn('usage limit', err['error']) + self.assertIn(reset_iso, err['error']) + self.assertIn('resumes on its own', err['next']) + self.assertNotIn(OPAQUE, json.dumps(err)) + self.assertEqual(len(self.logged()), 1) + self.assertEqual(self.driver('stop', out, env, prog), (0, dict(out, stopped=True))) + self.no_new_turn(1) + + def test_overload_without_reset_time_backs_off_60_120_then_stops(self): + env, waits, models = self.limit_env([ + {'exit': 1, 'post_note': True, 'line': 'API Error: 529 Overloaded (overloaded_error)'}, + {'exit': 1, 'line': 'API Error: 529 Overloaded (overloaded_error)'}, + {'exit': 1, 'line': 'API Error: 529 Overloaded (overloaded_error)'}, + {'exit': 0}, + ]) + prog = self.timed_driver() + code, out = self.start('claude', env=env, program=prog) + self.assertEqual(code, 0, out) + self.until(lambda: len(self.logged()) == 3) + with open(os.path.join(env['OPENWIZ_LOCAL_STATE'], out['run'] + '.json')) as f: + sup = json.load(f)['turns'][0]['supervisor'] + self.until(lambda: not alive(sup)) + self.assertEqual(read(waits).splitlines(), ['60.0', '120.0']) + self.assertEqual(read(models).splitlines(), [ + 'claude synthetic-1 first', 'claude synthetic-1 woken', 'claude synthetic-1 woken']) + + def test_limit_beyond_six_hours_does_not_wait_and_first_turn_reports_limit(self): + far_iso = '2026-11-24T01:53:20Z' + env, waits, _ = self.limit_env([ + {'exit': 1, 'post_note': True, + 'line': 'Claude AI usage limit reached. Resets at %s (%s)' % (far_iso, OPAQUE)}, + {'exit': 0}, + ]) + prog = self.timed_driver() + code, out = self.start('claude', env=env, program=prog) + self.assertEqual(code, 0, out) + with open(os.path.join(env['OPENWIZ_LOCAL_STATE'], out['run'] + '.json')) as f: + sup = json.load(f)['turns'][0]['supervisor'] + self.until(lambda: not alive(sup)) + self.assertFalse(os.path.exists(waits), 'supervisor waited for a reset more than 6 hours away') + self.assertEqual(len(self.logged()), 1) + wcode, err = self.driver('wake', out, env, prog) + self.assertEqual(wcode, 1, err) + self.assertIsNone(kit.invalid(err, kit.load_schema()['error']), err) + self.assertIn(far_iso, err['error']) + self.assertIn(far_iso, err['next']) + self.assertNotIn(OPAQUE, json.dumps(err)) + + near_iso = '2026-11-23T18:55:20Z' + env2, _, _ = self.limit_env([ + {'exit': 1, 'immediate': True, + 'line': 'Claude AI usage limit reached. Resets at %s (%s)' % (near_iso, OPAQUE)}, + ]) + code, err = self.start('claude', env=env2, program=prog) + self.assertEqual(code, 1, err) + self.assertIsNone(kit.invalid(err, kit.load_schema()['error']), err) + self.assertIn('usage limit', err['error']) + self.assertIn(near_iso, err['error']) + self.assertNotIn(OPAQUE, json.dumps(err)) + + code, err = self.start('claude', repository='example/broken') + self.assertEqual(code, 1, err) + self.assertIn('the harness failed (exit 3)', err['error']) + + def test_row_limit_pattern_and_reset_time_formats(self): + import importlib.machinery, importlib.util + spec = importlib.util.spec_from_loader('local', importlib.machinery.SourceFileLoader('local', DRIVER)) + drv = importlib.util.module_from_spec(spec) + spec.loader.exec_module(drv) + now = 1795460000.0 + self.assertTrue(hasattr(drv, 'detect_limit'), 'driver has no detect_limit') + for text, expected_kind, expected_reset in ( + ('Claude AI usage limit reached|1795460120', 'limit', 1795460120.0), + ('Rate limit exceeded. Try again in 2 minutes.', 'limit', now + 120.0), + ('You hit your usage limit; resets at 6:55pm UTC', 'limit', 1795460100.0), + ('API Error: 529 Overloaded', 'overload', None), + ('Ordinary syntax error on line 4', None, None), + ): + with self.subTest(text=text): + info = drv.detect_limit(text.splitlines(), now=now) + if expected_kind is None: + self.assertIsNone(info) + else: + self.assertIsNotNone(info) + self.assertEqual(info['kind'], expected_kind) + self.assertEqual(info['reset_at'], expected_reset) + row = {'harness': 'custom', 'program': 'custom', 'start': ['run', '{prompt}', '{model}', '{mode}'], + 'wake': ['run', '{prompt}', '{mode}', '{session}'], 'session': {'pattern': 's=(.*)'}, + 'modes': {'auto': []}, 'env': {}, 'effort': 'none: fixture', 'login': 'fixture', 'daemon': 'none', + 'limit': r'quota\s+exhausted'} + self.assertEqual(drv.check_row(dict(row), 'custom')['limit'], r'quota\s+exhausted') + info = drv.detect_limit(['quota exhausted; try again in 5 minutes'], row=row, now=now) + self.assertEqual((info['kind'], info['reset_at']), ('limit', now + 300.0)) + with self.assertRaises(drv.Broken): + drv.check_row(dict(row, limit='.*'), 'custom') + if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/changes/3116.md b/changes/3116.md new file mode 100644 index 000000000..e0adadbf1 --- /dev/null +++ b/changes/3116.md @@ -0,0 +1,3 @@ +kind: added + +On synthetic fixtures, the local driver waits out usage limits and provider overloads and resumes once on the same harness and model, while refusing wakes with the reset time during the wait.