Commit 1cb31bb
authored
feat: Schedule API (#88)
# Agentex Schedule API - PR Summary
## Overview
This PR introduces a new Schedule API for Agentex that enables
agent-scoped management of Temporal schedules. The feature supports both
cron-based and interval-based scheduling for recurring workflow
executions, designed to power async agents.
## Goal
Enable async agents to programmatically schedule when their pre-defined
workflows run through the Agentex
API. This allows agents to:
- Schedule recurring executions of existing workflows
- Pause/resume schedules based on external conditions
- Trigger immediate workflow executions on-demand
- Monitor schedule status and execution history
## Files Created
### Agentex Backend (`scale-agentex/agentex/`)
| File | Description |
|------|-------------|
| `src/api/routes/schedules.py` | FastAPI router with schedule CRUD
endpoints |
| `src/api/schemas/schedules.py` | Pydantic models for request/response
schemas |
| `src/domain/services/schedule_service.py` | Business logic for
schedule management |
| `src/domain/use_cases/schedules_use_case.py` | Use case layer wrapping
schedule service |
### Files Modified
| File | Changes |
|------|---------|
| `src/adapters/temporal/adapter_temporal.py` | Added Temporal schedule
operations (create, describe, list, pause, unpause, trigger, delete) |
| `src/adapters/temporal/port.py` | Added schedule method signatures to
Temporal port interface |
| `src/api/app.py` | Registered schedules router |
## API Endpoints
All endpoints are scoped under `/agents/{agent_id}/schedules`:
| Endpoint | Method | Description |
|----------|--------|-------------|
| `/agents/{agent_id}/schedules` | `POST` | Create a new schedule |
| `/agents/{agent_id}/schedules` | `GET` | List all schedules for an
agent |
| `/agents/{agent_id}/schedules/{schedule_name}` | `GET` | Get schedule
details |
| `/agents/{agent_id}/schedules/{schedule_name}/pause` | `POST` | Pause
a schedule |
| `/agents/{agent_id}/schedules/{schedule_name}/unpause` | `POST` |
Resume a paused schedule |
| `/agents/{agent_id}/schedules/{schedule_name}/trigger` | `POST` |
Trigger immediate execution |
| `/agents/{agent_id}/schedules/{schedule_name}` | `DELETE` | Delete a
schedule |
## Request/Response Schemas
### CreateScheduleRequest
```json
{
"name": "string (required)",
"workflow_name": "string (required)",
"task_queue": "string (required)",
"cron_expression": "string (optional, e.g., '0 * * * *')",
"interval_seconds": "integer (optional)",
"workflow_params": "object (optional)",
"execution_timeout_seconds": "integer (optional)",
"start_at": "datetime (optional)",
"end_at": "datetime (optional)",
"paused": "boolean (optional, default: false)"
}
```
### ScheduleResponse
```json
{
"schedule_id": "string",
"name": "string",
"agent_id": "string",
"state": "ACTIVE | PAUSED",
"action": {
"workflow_name": "string",
"workflow_id_prefix": "string",
"task_queue": "string",
"workflow_params": "list | null"
},
"spec": {
"cron_expressions": ["string"],
"intervals_seconds": [integer],
"start_at": "datetime | null",
"end_at": "datetime | null"
},
"num_actions_taken": "integer",
"num_actions_missed": "integer",
"next_action_times": ["datetime"],
"last_action_time": "datetime | null",
"created_at": "datetime | null"
}
```
## Bugs Fixed During Development
### 1. Async Iterator Bug in `list_schedules`
**File:** `src/adapters/temporal/adapter_temporal.py:502-503`
**Problem:** The Temporal SDK's `client.list_schedules()` returns a
coroutine that yields an async iterator, but the code was treating it as
a direct async iterator.
**Error:**
```python
'async for' requires an object with __aiter__ method, got coroutine
```
**Fix:**
```python
# Before (incorrect)
async for schedule in self.client.list_schedules(page_size=page_size):
# After (correct)
async for schedule in await self.client.list_schedules(page_size=page_size):
```
### 2. Temporal Payload Serialization Bug
**File:** `src/domain/services/schedule_service.py:268-288`
**Problem:** When fetching schedule details, `action.args` contains raw
Temporal `Payload` protobuf objects that cannot be JSON-serialized by
Pydantic.
**Error:**
```python
Unable to serialize unknown type: <class 'temporalio.api.common.v1.message_pb2.Payload'>
```
**Fix:** Added code to extract and decode payload data to
JSON-serializable format:
```python
if action.args:
try:
workflow_params = []
for arg in action.args:
if hasattr(arg, "data"):
try:
import json
workflow_params.append(json.loads(arg.data.decode("utf-8")))
except (json.JSONDecodeError, UnicodeDecodeError):
workflow_params.append(str(arg.data))
else:
workflow_params.append(str(arg))
except Exception:
workflow_params = None
```
### 3. ScheduleActionResult Attribute Name Bug
**File:** `src/domain/services/schedule_service.py:313-317`
**Problem:** The code was accessing
`info.recent_actions[-1].actual_time`, but `ScheduleActionResult` uses
`started_at` and `scheduled_at` attributes, not `actual_time`.
**Error:**
```Python
'ScheduleActionResult' object has no attribute 'actual_time'
```
**Fix:**
```python
# Before (incorrect)
last_action_time = info.recent_actions[-1].actual_time
# After (correct)
last_action = info.recent_actions[-1]
last_action_time = getattr(last_action, "started_at", None) or getattr(last_action, "scheduled_at", None)
```
### 4. Schedule args double-wrapping Bug
ScheduleActionStartWorkflow expects positional args, not a list. The
args were being passed as `[params]` which became `[[params]]`. Fixed by
unpacking with `*(args if args else [])`.
## Testing Results
All endpoints were tested against a local Docker Compose environment
with an existing agent which was registered locally:
| Test | Result |
|------|--------|
| List schedules (initially empty) | ✅ Pass |
| Create schedule with cron expression | ✅ Pass |
| Create schedule with interval_seconds | ✅ Pass |
| Get schedule details | ✅ Pass |
| Pause schedule | ✅ Pass |
| Unpause schedule | ✅ Pass |
| Trigger schedule immediately | ✅ Pass |
| Delete schedule | ✅ Pass |
| Verify deletion | ✅ Pass |
### Sample Test Commands
```bash
# List schedules
curl -s http://localhost:5003/agents/{agent_id}/schedules
# Create schedule with cron
curl -s -X POST http://localhost:5003/agents/{agent_id}/schedules \
-H "Content-Type: application/json" \
-d '{"name": "hourly-job", "workflow_name": "my-orchestrator", "task_queue": "my-queue", "cron_expression": "0 * * * *"}'
# Create schedule with interval
curl -s -X POST http://localhost:5003/agents/{agent_id}/schedules \
-H "Content-Type: application/json" \
-d '{"name": "interval-job", "workflow_name": "my-orchestrator", "task_queue": "my-queue", "interval_seconds": 3600}'
# Pause/Unpause
curl -s -X POST http://localhost:5003/agents/{agent_id}/schedules/{name}/pause
curl -s -X POST http://localhost:5003/agents/{agent_id}/schedules/{name}/unpause
# Trigger immediately
curl -s -X POST http://localhost:5003/agents/{agent_id}/schedules/{name}/trigger
# Delete
curl -s -X DELETE http://localhost:5003/agents/{agent_id}/schedules/{name}
```
## Architecture Notes
- **Schedule ID Format:** `{agent_id}--{schedule_name}` (using `--` as
separator)
- **Workflow ID Format:** `{schedule_id}-run-{timestamp}`
(auto-generated by Temporal)
- Schedules are stored in Temporal, not in the Agentex database *(!!
Please lmk if this needs to be changed !!)*
- Authorization uses existing agent-scoped authorization middleware
## Future Considerations
1. **SDK Integration:** The agentex-python SDK is Stainless-generated.
Schedule endpoints should be auto-added when the OpenAPI spec is
updated.
2. **Backfill Support:** Temporal supports schedule backfills which
could be exposed as an additional endpoint if needed.1 parent 8ceb59a commit 1cb31bb
9 files changed
Lines changed: 2734 additions & 3 deletions
File tree
- agentex
- src
- adapters/temporal
- api
- routes
- schemas
- domain
- services
- use_cases
- tests/unit
- services
- use_cases
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
7 | 15 | | |
8 | 16 | | |
9 | 17 | | |
10 | 18 | | |
11 | | - | |
12 | | - | |
13 | | - | |
| 19 | + | |
14 | 20 | | |
15 | 21 | | |
16 | 22 | | |
17 | 23 | | |
18 | 24 | | |
19 | 25 | | |
20 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
21 | 30 | | |
22 | 31 | | |
23 | 32 | | |
| |||
335 | 344 | | |
336 | 345 | | |
337 | 346 | | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
338 | 613 | | |
339 | 614 | | |
340 | 615 | | |
| |||
0 commit comments