What the agent does
The agent starts from the enqueue. In an app laid out like mjhea0/flask-redis-queue, project/server/main/views.py handles POST /tasks by opening Connection(redis.from_url(current_app.config["REDIS_URL"])), building Queue() with no arguments and calling q.enqueue(create_task, task_type), then answering 202 with the job id; GET /tasks/<task_id> fetches the job with q.fetch_job and returns task.get_status() and task.result, or an error object when the id is unknown. project/server/main/tasks.py is the whole workload: create_task(task_type) sleeps int(task_type) * 10 seconds and returns True, and the form on the home page picks the type. No enqueue passes job_timeout, result_ttl or failure_ttl, and the queue sets no default_timeout, so every job runs under RQ's own default and keeps or drops its result on RQ's own schedule.
Then it reads how the worker and the tests run. manage.py adds a run_worker command that builds Worker(app.config["QUEUES"]) under the same connection and calls worker.work(), and a test command that discovers project/tests; project/server/config.py sets REDIS_URL to redis://redis:6379/0 and QUEUES to ["default"], with DevelopmentConfig and TestingConfig switching WTF_CSRF_ENABLED and TESTING. docker-compose.yml runs web, a worker from the same image, redis:6.2-alpine and an rq-dashboard container; the Dockerfile is python:3.10-alpine with requirements.txt pinning rq==1.10.1, redis==4.1.1, Flask==2.0.2 and Flask-Testing==0.8.1. The existing tests check the two configs and that / answers 200; nothing exercises a job.
The change is in three files. config.py gains RQ_JOB_TIMEOUT, RQ_RESULT_TTL and RQ_FAILURE_TTL, and views.py passes them: the queue is built with default_timeout from the first, and each enqueue carries result_ttl and failure_ttl so finished results expire on a known schedule and failed jobs stay long enough to read. The status route adds a timed_out flag and the last line of task.exc_info when task.is_failed, since RQ 1.10 records the JobTimeoutException the worker raises when it kills the work horse, and the dashboard container shows the same job in its failed registry. create_task is unchanged. A new project/tests/test_tasks.py enqueues create_task with a job_timeout of one second and a type that sleeps longer, runs Worker([queue], connection=…).work(burst=True) against the configured Redis, and asserts the job is_failed and its exc_info names JobTimeoutException; a second test enqueues a type that finishes in time and asserts result is True. Timeouts need the forking Worker, so the test uses it rather than SimpleWorker.
To prove it, the agent installs Redis with apt-get in its sandbox, points REDIS_URL at it through APP_SETTINGS, runs pip install -r requirements.txt on the interpreter .python-version names, and runs python manage.py test with the new tests, pasting the output and the worker's log lines for the killed job. It then starts the app and a worker, posts a slow type to /tasks, and fetches /tasks/<id> after the timeout to show failed, timed_out and the exception line in the JSON.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "claude",
"input": "Add job timeouts to this Flask RQ app. Read project/server/main/views.py, project/server/main/tasks.py, project/server/config.py, project/server/__init__.py, manage.py, project/tests/base.py, project/tests/test_main.py, docker-compose.yml, Dockerfile, requirements.txt and .python-version first. Add RQ_JOB_TIMEOUT, RQ_RESULT_TTL and RQ_FAILURE_TTL to BaseConfig, build the Queue in views.py with default_timeout from the first and pass result_ttl and failure_ttl on enqueue. On GET /tasks/<id> add a timed_out flag and the last line of exc_info when the job is failed. Add project/tests/test_tasks.py: enqueue create_task with job_timeout one second and a type that sleeps longer, run the forking Worker in burst mode against the configured Redis, assert the job is failed and exc_info names JobTimeoutException; and a second test where a fast type returns True. Install Redis in your sandbox, run python manage.py test and paste the output with the worker's log for the killed job, then start the app and a worker, post a slow type, and paste the status JSON after the timeout. Change no task function and no pin.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
- Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
- Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.
What comes back
The pull request changes project/server/config.py and project/server/main/views.py, adds project/tests/test_tasks.py, and leaves tasks.py, the Docker files and the pins alone. Its description shows the test run with the two new tests passing, the worker's log for the job it killed, and the status JSON for a timed-out job with failed, timed_out and the JobTimeoutException line.
The home page and its polling are unchanged; a job that overruns now ends as failed with a readable reason instead of occupying the worker, and results and failures expire on the configured TTLs, so Redis stops accumulating finished jobs. The dashboard shows the timed-out job in its failed registry with the same exception.
Before merging, a reviewer sizes the three settings for their real jobs, since the pull request's values fit this app's sleep-based task, decides whether a timed-out job should be retried or left for a person, and confirms the worker container keeps using the forking Worker, which is what enforces the limit.
Run it on every Linear issue labelled agent
Add an automation to the Project with this prompt and a linear trigger on linear.issue.labeled with the label you reserve for the agent in its labels. Each labelled issue starts one run with the issue as its context, on its own branch; the trigger adds the issue after the prompt and fills in no placeholder, and a change made by an app integration never fires it, so the agent's own comment on the issue starts nothing.
The automation's max_runs_per_hour holds a burst of labels to that many runs an hour: a fire past it is created queued and starts when the hour has room, and one past a second hour's worth is skipped as rate_limited until the issue is labelled again. From your own server, the same run starts with this request and the issue URL in resources, with a request_id that is a UUID v5 of the issue id so a retried start returns the run already begun.
The use case this job follows, with its automation and trigger: Turn a Linear label into a PR.
Why run it on Rush?
A timeout is a keyword argument, but what the worker does when it fires, what the job record holds afterwards and what the status route shows can only be seen by running a worker and letting a job overrun. The agent does that in its own sandbox against a real Redis with the pinned RQ release, so the pull request shows the killed job and the status JSON rather than quoting the documentation.
The transcript keeps the test run, the worker log and the status fetch with their output, so a reviewer reads the evidence from one page. Several Flask services on the same queue pattern can be given limits at once, each run in its own sandbox with its own Redis, and nothing merges until a person reads the diff.
Frequently asked questions
What happens to a job when the timeout fires?
The forking Worker runs each job in a work horse process and kills it when the limit passes, then moves the job to the failed registry with a JobTimeoutException in exc_info. The job's own code gets no chance to clean up, so the limit should sit above the longest honest run, which is why the three settings live in config rather than in the view.
Why do the tests use Worker and not SimpleWorker?
SimpleWorker runs jobs in the worker's own process and cannot enforce a timeout, so a test built on it would pass without proving anything. The forking Worker is also what manage.py run_worker starts, so the test exercises the same path the container does, in burst mode so it returns when the queue is empty.
Why set result and failure TTLs at the same time?
Without them, finished results and failed jobs stay in Redis on RQ's defaults, and a status page that reads them keeps working until they silently vanish. Setting both makes the window explicit: the result TTL says how long /tasks/<id> can report finished, and the failure TTL how long a timed-out job stays readable for someone debugging it.
Does this change the Docker setup?
No. docker-compose.yml, the Dockerfile and the pins are untouched; the new settings have defaults in BaseConfig and can be overridden through the same APP_SETTINGS mechanism the containers already use. The sandbox has no Docker, so the agent runs Redis directly for its proof, which is noted in the pull request.
More RQ jobs
The same job on other stacks
- Add timeout-minutes to Every GitHub Actions Job (GitHub Actions)