Skip to content

Read a review again before the wait deadline instead of sleeping it out - #155

Open
Ryzuuuu wants to merge 2 commits into
prooflinedev:mainfrom
Ryzuuuu:fix/wait-final-read-before-deadline
Open

Ryzuuuu wants to merge 2 commits into
prooflinedev:mainfrom
Ryzuuuu:fix/wait-final-read-before-deadline

Conversation

@Ryzuuuu

@Ryzuuuu Ryzuuuu commented Dec 2, 2026

Copy link
Copy Markdown

Problem

wait_for_review sleeps min(interval, remaining) between reads, with the interval doubling from 5 s up to 60 s. Once the next interval is longer than the time left, that sleep ends exactly at the deadline. The loop then exits, because remaining() > 0 is false, and returns TIMED_OUT without another read. The last interval of every wait that doesn't settle earlier is slept out unobserved.

With the default timeout=300, the reads are at 0, 5, 15, 35, 75, 135, 195 and 255 s, followed by 45 s of sleep. A review that completes at 260 s is reported as TIMED_OUT, although it finished inside the window the caller asked for. With timeout=30, the blind tail is 15 of the 30 s. TIMED_OUT is what CI jobs branch on, so this turns a finished review into a failed or retried job.

AsyncProoflineClient.wait_for_review has the same loop.

Fix

One helper, _poll_delay(interval, budget), now picks each sleep for both clients:

  • If the next read lands more than 5 s before the deadline, sleep the back-off interval as before.
  • If it would land in the last 5 s or after the deadline, sleep until 5 s before the deadline and read once more. If that read is still unsettled, the remaining ~5 s are slept out, so a wait that doesn't settle still ends at its deadline.
  • If moving the read would put it less than 5 s after the previous one, keep min(interval, budget) unchanged.

Notes on the choices:

  • The read needs time to finish, because limit_timeout caps it at the remaining budget and it usually reconnects after a sleep longer than the 5 s keep-alive. A read at the deadline itself can't start. A moved read that doesn't finish ends exactly as before.
  • The client never reads more often than every 5 s (its first interval), and each wait makes at most one extra request.
  • For any budget under 10 s the result is exactly min(interval, budget). Short waits, timeout=0, and the Retry-After-at-deadline paths are unchanged.
  • The existing timeout=260 back-off test passes unmodified: its final gap is 5 s, so the read stays where it was.

The README's waiting section and the wait_for_review docstring describe the new last read.

Verification

Tests

  • test_wait_reads_again_before_the_deadline_instead_of_sleeping_it_out (sync and async): the review completes at 260 s into the default 300 s wait. Before: TIMED_OUT. After: COMPLETED at 295 s, with sleeps [5, 10, 20, 40, 60, 60, 60, 40] and 9 requests.
  • test_wait_still_lasts_its_timeout_after_the_last_read (sync and async): an unfinished review with timeout=120 is read at 0, 5, 15, 35, 75 and 115 s and returns TIMED_OUT at exactly 120 s with the latest record.
  • test_poll_delay_moves_only_a_read_the_deadline_would_skip: six interval/budget cases covering each branch and its boundaries.

The two wait tests fail on main and pass on this branch.

End to end, real time

A loopback HTTP server answers RUNNING until 20 s, then COMPLETED. ProoflineClient.wait_for_review(..., timeout=30) runs against it with real time and the real transport:

main                                   this branch
t= 0.0s RUNNING                        t= 0.0s RUNNING
t= 5.0s RUNNING                        t= 5.0s RUNNING
t=15.1s RUNNING                        t=15.0s RUNNING
Outcome: TIMED_OUT after 30.0s         t=25.0s COMPLETED
                                       Outcome: COMPLETED after 25.0s
Script to reproduce

Run from the repository root with the dev dependencies installed (python demo_wait.py):

import json, sys, threading, time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path

sys.path.insert(0, str(Path.cwd()))
from proofline import ProoflineClient
from tests.fixtures import ORG, REVIEW, review

started = time.monotonic()

class Handler(BaseHTTPRequestHandler):
    protocol_version = "HTTP/1.1"
    def log_message(self, *args): pass
    def do_GET(self):
        elapsed = time.monotonic() - started
        status = "COMPLETED" if elapsed >= 20 else "RUNNING"
        print(f"t={elapsed:5.1f}s {status}")
        body = json.dumps(review(status)).encode()
        self.send_response(200)
        self.send_header("Content-Type", "application/json")
        self.send_header("Content-Length", str(len(body)))
        self.end_headers()
        self.wfile.write(body)

server = ThreadingHTTPServer(("127.0.0.1", 0), Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()
with ProoflineClient("demo-token", base_url=f"http://127.0.0.1:{server.server_address[1]}") as client:
    started = time.monotonic()
    outcome = client.wait_for_review(ORG, REVIEW, timeout=30)
print(outcome.status, f"after {time.monotonic() - started:.1f}s")

Checks

python scripts/check.py runs in this PR's CI on Linux.

Locally on Windows 11 (Python 3.13.12, pinned dev dependencies): ruff check, ruff format --check, mypy proofline scripts, scripts/contracts.py and pytest (244 passed) all pass. scripts/generate.py --check fails on Windows on main as well (#87); CI runs it on Linux.

Follow-up

The TypeScript SDK's waitForReview uses the same sleep(min(interval, deadline - now)) pattern and has the same blind tail.

wait_for_review slept min(interval, remaining) between reads, so once the
backed-off interval outgrew the time left, the last sleep ended at the
deadline and the wait returned TIMED_OUT without reading again. With the
default 300 s timeout the last read was at 255 s, so a review completing
at 260 s was reported as timed out.

Move a read the deadline would skip to 5 s before the deadline, unless
that is under 5 s after the previous read. A wait that does not settle
still ends at its deadline.
…stays

The wait_for_review docstring said only reads the deadline would skip move
to five seconds before it. _poll_delay also moves a read that would land in
the last five seconds, and leaves a read in place when moving it would come
under five seconds after the previous one. Match the README wording.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant