Skip to content

fix(pull): clamp the turn limit at claim so a healthy turn never outlives its lease (#2846) - #2941

Merged
vybe merged 2 commits into
devfrom
feature/2846-clamp-turn-timeout
Sep 22, 2026
Merged

vybe merged 2 commits into
devfrom
feature/2846-clamp-turn-timeout

Conversation

@obasilakis

@obasilakis obasilakis commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • A pulled turn's limit came from the timeout copied onto its row at enqueue; its lease came from the agent's timeout at claim. If the agent's timeout was lowered while the job waited, or the row had no timeout (worker default 900s), a healthy turn could outlive its lease and be re-delivered to a second worker while still running.
  • claim_next_task now reads the agent timeout once and uses it for both the lease and the turn limit. The turn always ends inside its lease.
  • This replaces the lease-renewal heartbeat the design specified and never built. With the clamp, a heartbeat has nothing to do for a healthy turn, and for a hung turn it would keep the lease alive forever.
  • When the clamp shortens a limit it is logged, and a timeout failure on that job says so: (this job asked for 1800s but the agent's timeout is 600s; raise the agent's timeout to give it longer).

Changes

  • src/backend/services/pull_coordination_service.py: clamp in claim_next_task (_turn_limit), best-effort note on timeout failures in apply_task_result (_shortened_note, never blocks the terminal write).
  • src/backend/services/lease_reaper_service.py: docstring states when an expired lease means a dead worker.
  • src/backend/routers/agent_config.py: PUT /timeout docs: applies to new runs; queued pull jobs are cut to it when they start.
  • docs/planning/TARGET_ARCHITECTURE.md (§Recovery), docs/planning/MESSAGE_ENVELOPE_SCHEMA.md (§3.5, OPEN-4): heartbeat replaced by the clamp.
  • docs/memory/architecture/backend.md: catalog line.

Behaviour change on pilot agents only: a legacy schedule whose timeout exceeds the agent's (created before the schedule ceiling existed) now stops at the agent's timeout instead of being re-delivered mid-run; the failure text names the setting to raise.

Test Plan

  • TestClaimTurnLimit in tests/unit/test_1081_pull_endpoints.py: over-cap, absent and within-cap timeouts; turn ends inside the lease; timeout note present/absent; note failure never blocks the terminal write.
  • Pull/lease/cleanup/canary/timeout-related unit tests: 1278 passed.
  • Full unit suite: the only failures (23 in the affected files run alone) reproduce identically on a clean origin/dev checkout (local Python 3.11 IPv6/SSRF, portal uploads, skill assign, workflow trigger parity); none touch this change.
  • PostgreSQL variant of the new tests, run locally against PostgreSQL 16 (no CI job runs this file on PG): 12 passed (6 tests × SQLite + PG).

Fixes #2846

🤖 Generated with Claude Code

https://claude.ai/code/session_01UNS51H87HDMvtBVAZPiekJ

obasilakis and others added 2 commits September 21, 2026 20:42
…ives its lease (#2846)

A pulled turn ran with the timeout copied onto its row at enqueue, while its
lease was sized from the agent's timeout at claim. When the agent's timeout
was lowered while the job waited, or the row carried no timeout (worker
default 900s), a healthy turn could run past its lease and the reaper would
hand the same execution to a second worker while the first still ran.

claim_next_task now reads the agent timeout once and uses it for both the
lease and the turn limit (absent -> the agent timeout). A clamped limit is
logged, and a timeout failure on a clamped job names the shortened limit so
the operator knows which setting to change.

This replaces the lease-renewal heartbeat the design specified and never
built: with the clamp a heartbeat has nothing to do for a healthy turn, and
for a hung turn it would keep the lease alive forever. TARGET_ARCHITECTURE,
MESSAGE_ENVELOPE_SCHEMA (section 3.5, OPEN-4), the reaper docstring and the
timeout endpoint's docs are updated to match.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01UNS51H87HDMvtBVAZPiekJ
…on (#2846)

The timeout note compared against the agent's current timeout, so a timeout
lowered mid-run produced a false "shortened" claim; it now states the two
numbers instead. The reaper docstring names the runtimes that enforce their
own turn limit and the single-process assumption replicas would break.
TARGET_ARCHITECTURE's open-question list no longer mentions lease renewal.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01UNS51H87HDMvtBVAZPiekJ
@vybe

vybe commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

merge-train 2026-09-22: riding this train. Validated lane B (/validate-pr READY, /review READY). Nothing pushed to this branch. Both reviewers traced the clamp to the three places it has to agree — the lease stamped at claim, the reaper (which reads only lease_expires_at), and the task_overrides.timeout_seconds the pull worker feeds to execute_headless — and found it consistent by construction from the single cap read. Two informational residuals, already stated in your reaper docstring: Gemini checks the limit only on a new stdout line, so a silent Gemini turn can still outlive its lease; and _shortened_note compares against the agent's current timeout rather than the claim-time cap. Neither blocks.

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge-train: batch validated on train/20260922-1340 (train PR #2954, full suite green)

@vybe
vybe merged commit 756b0fb into dev Sep 22, 2026
28 of 29 checks passed
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.

2 participants