From 012c4b040349b4d3d2504c67410d311a395a3895 Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:17:34 +0200 Subject: [PATCH 1/2] fix(efficacy): write a trial's refreshed token back while the trial runs A trial's refresh retires the refresh token the account's file holds, and the write-back waited for the trial's end, so the owner's own session was forced to log in mid-trial. A watch now writes a later home token back as soon as it appears, under the same lock and compare; the link is still remade only between trials. Closes #87 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DpT5xPB8YTAMW2Msp4aPTX --- docs/architecture/06-runtime-view.md | 7 +++ tests/efficacy/harness.py | 93 +++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/docs/architecture/06-runtime-view.md b/docs/architecture/06-runtime-view.md index 9e1d3f7..804923d 100644 --- a/docs/architecture/06-runtime-view.md +++ b/docs/architecture/06-runtime-view.md @@ -109,6 +109,13 @@ every trial, so a stopped run keeps every trial it finished. writing back at once cannot leave the older token. The CLI takes no lock, so the account's file is compared through the handle that writes it, and a save that replaces it meanwhile is compared again rather than overwritten. + The refresh also retires the refresh token the account's file holds, so + every other session on the machine holds a dead one until the new token + is back: on 2026-10-30 `hybrid-6` refreshed at 15:05 and the owner's + session was forced to log in at 15:10 (#87). A watch therefore looks at + the home's file every 15 seconds while a trial runs, and writes a later + token back as soon as it appears, under the same lock and compare. The + link itself is still remade only between trials. - **The temporary directory.** Git Bash maps `/tmp` to the machine's temporary directory whatever `TEMP` says, so an entry that appeared there during a trial and that a tool call names is moved into the trial's own. diff --git a/tests/efficacy/harness.py b/tests/efficacy/harness.py index 9ba7704..04a3498 100644 --- a/tests/efficacy/harness.py +++ b/tests/efficacy/harness.py @@ -732,6 +732,48 @@ def credential_lock(source, stale_s=60, poll_s=0.05): pass +# How often a running trial's credentials are looked at. The account's own +# clients go without a live token from a trial's refresh until the next look. +WATCH_EVERY_S = 15 + + +@contextlib.contextmanager +def credential_watch(home, account=None, every_s=WATCH_EVERY_S): + """While inside, write a token the trial refreshed back to the account's + file as soon as it is seen; yields a list that counts the write-backs.""" + source = account or os.path.join(os.path.expanduser("~"), CREDENTIALS) + target = os.path.join(home, CREDENTIALS) + written, stop = [], threading.Event() + + # A trial's refresh retires the refresh token the account's file holds, + # so every other session on the machine holds a dead one until the new + # token is written back. Left to the end of the trial, that gap ran an + # hour and forced a login; the link is still remade only between trials. + def look(): + while not stop.wait(every_s): + try: + if not os.path.exists(target) or not os.path.exists(source) \ + or os.path.samefile(source, target): + continue + with credential_lock(source): + if write_back(source, target): + written.append(time.time()) + print(" credentials: a token the trial refreshed was " + "written back to the account's file", flush=True) + except OSError: + + # The CLI may be mid-save; the next look reads the file whole. + continue + + watcher = threading.Thread(target=look, daemon=True) + watcher.start() + try: + yield written + finally: + stop.set() + watcher.join(every_s + 30) + + def prepare_home(root): """Create the scratch home every trial runs under, and return it. @@ -3000,6 +3042,48 @@ def replaced(data): return checks +def credential_watch_checks(): + """A token a running trial refreshed reaches the account's file before + the trial ends, an older one never does, and the watch stops with it.""" + scratch = os.path.join(tempfile.gettempdir(), + "efficacy-credential-watch-self-test") + remove_tree(scratch) + home = os.path.join(scratch, "home") + account = os.path.join(scratch, "account", ".credentials.json") + os.makedirs(os.path.dirname(account)) + target = os.path.join(home, CREDENTIALS) + + def write(path, expires): + # A new file each time, as the CLI saves a refreshed token. + if os.path.lexists(path): + os.remove(path) + with io.open(path, "w", encoding="utf-8") as handle: + json.dump({"claudeAiOauth": {"expiresAt": expires}}, handle) + + def settle(until, bound_s=10): + deadline = time.time() + bound_s + while not until() and time.time() < deadline: + time.sleep(0.02) + return until() + + write(account, 1000) + refresh_credentials(home, account) + with credential_watch(home, account, every_s=0.05) as written: + write(target, 2000) + reached = settle(lambda: expires_at(account) == 2000) + write(target, 1500) + time.sleep(0.5) + older_kept = expires_at(account) == 2000 + threads = threading.active_count() + stopped = threading.active_count() == threads - 1 + remove_tree(scratch) + return [("a token refreshed mid-trial reaches the account's file " + "while the trial runs", reached and len(written) == 1), + ("an older token seen mid-trial leaves the account's as it was", + older_kept), + ("the watch stops when the trial ends", stopped)] + + def credential_link_checks(): """The scratch home reads the account's live credentials, and gets them back before a trial where they went missing.""" @@ -3386,6 +3470,7 @@ def self_test(): rules before a trial.""" checks = (naming_checks() + environment_checks() + credential_checks() + credential_link_checks() + credential_rotation_checks() + + credential_watch_checks() + outcome_checks() + hang_checks() + resume_checks() + change_checks() + dry_run_checks() + process_checks() @@ -3567,13 +3652,15 @@ def main(argv): def runner(arm, trial): refresh_credentials(home) if options.task == "build": - return run_trial(arm, trial, work, home, options) + with credential_watch(home): + return run_trial(arm, trial, work, home, options) name = trial_name(arm, trial) if name not in builds: raise TrialError("build trial %s has no scorable outcome, so it " "has no change task" % name) - return run_change_trial(arm, trial, work, home, options, - builds[name]) + with credential_watch(home): + return run_change_trial(arm, trial, work, home, options, + builds[name]) def write(): write_run(run_file, started_at, records, options.task, prompt) From 3b1cc920b1b9580b4f4ffbccb46f10491f769e69 Mon Sep 17 00:00:00 2001 From: Branimir Georgiev <66906831+braboj@users.noreply.github.com> Date: Fri, 30 Oct 2026 15:43:37 +0200 Subject: [PATCH 2/2] fix(efficacy): write back a token refreshed after the watch's last look The watch stopped without a final look, so a refresh after it waited for the next trial's re-link, which the last trial of a run, or one before a long wait for the generator, does not reach for hours. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DpT5xPB8YTAMW2Msp4aPTX --- docs/architecture/06-runtime-view.md | 6 ++-- tests/efficacy/harness.py | 43 +++++++++++++++++++--------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/docs/architecture/06-runtime-view.md b/docs/architecture/06-runtime-view.md index 804923d..184a9ee 100644 --- a/docs/architecture/06-runtime-view.md +++ b/docs/architecture/06-runtime-view.md @@ -114,8 +114,10 @@ every trial, so a stopped run keeps every trial it finished. is back: on 2026-10-30 `hybrid-6` refreshed at 15:05 and the owner's session was forced to log in at 15:10 (#87). A watch therefore looks at the home's file every 15 seconds while a trial runs, and writes a later - token back as soon as it appears, under the same lock and compare. The - link itself is still remade only between trials. + token back as soon as it appears, under the same lock and compare, and + looks once more as the trial ends, so a refresh after the last look is not + left for a run's end or a long wait. The link itself is still remade only + between trials. - **The temporary directory.** Git Bash maps `/tmp` to the machine's temporary directory whatever `TEMP` says, so an entry that appeared there during a trial and that a tool call names is moved into the trial's own. diff --git a/tests/efficacy/harness.py b/tests/efficacy/harness.py index 04a3498..9d74489 100644 --- a/tests/efficacy/harness.py +++ b/tests/efficacy/harness.py @@ -749,21 +749,24 @@ def credential_watch(home, account=None, every_s=WATCH_EVERY_S): # so every other session on the machine holds a dead one until the new # token is written back. Left to the end of the trial, that gap ran an # hour and forced a login; the link is still remade only between trials. + def sync(): + try: + if not os.path.exists(target) or not os.path.exists(source) \ + or os.path.samefile(source, target): + return + with credential_lock(source): + if write_back(source, target): + written.append(time.time()) + print(" credentials: a token the trial refreshed was " + "written back to the account's file", flush=True) + except OSError: + + # The CLI may be mid-save; the next look reads the file whole. + pass + def look(): while not stop.wait(every_s): - try: - if not os.path.exists(target) or not os.path.exists(source) \ - or os.path.samefile(source, target): - continue - with credential_lock(source): - if write_back(source, target): - written.append(time.time()) - print(" credentials: a token the trial refreshed was " - "written back to the account's file", flush=True) - except OSError: - - # The CLI may be mid-save; the next look reads the file whole. - continue + sync() watcher = threading.Thread(target=look, daemon=True) watcher.start() @@ -773,6 +776,11 @@ def look(): stop.set() watcher.join(every_s + 30) + # A refresh after the last look would otherwise wait for the next + # trial's re-link, which the last trial of a run, or one followed by + # a long wait for the generator, does not reach for hours. + sync() + def prepare_home(root): """Create the scratch home every trial runs under, and return it. @@ -3076,12 +3084,19 @@ def settle(until, bound_s=10): older_kept = expires_at(account) == 2000 threads = threading.active_count() stopped = threading.active_count() == threads - 1 + + # A refresh after the last look, with no look due before the trial ends. + with credential_watch(home, account, every_s=3600): + write(target, 3000) + last_kept = expires_at(account) == 3000 remove_tree(scratch) return [("a token refreshed mid-trial reaches the account's file " "while the trial runs", reached and len(written) == 1), ("an older token seen mid-trial leaves the account's as it was", older_kept), - ("the watch stops when the trial ends", stopped)] + ("the watch stops when the trial ends", stopped), + ("a token refreshed after the last look is written back as the " + "trial ends", last_kept)] def credential_link_checks():