Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions agentflow/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,26 @@ def create_app(*, store: RunStore | None = None, orchestrator: Orchestrator | No

@app.get("/", response_class=HTMLResponse)
async def index(request: Request) -> HTMLResponse:
try:
example = _load_default_web_example()
except Exception as exc:
# Prevent 500 error on the home page if the bundled example fails
example = f"# Error loading default example pipeline:\n# {exc}"

return templates.TemplateResponse(
"index.html",
{"request": request, "example": _load_default_web_example(), "base_dir": os.getcwd()},
request=request,
name="index.html",
context={"example": example, "base_dir": os.getcwd()},
)

@app.get("/api/examples/default")
async def default_example() -> JSONResponse:
return JSONResponse({"example": _load_default_web_example(), "base_dir": os.getcwd()})
try:
example = _load_default_web_example()
except Exception as exc:
example = f"# Error loading default example pipeline:\n# {exc}"

return JSONResponse({"example": example, "base_dir": os.getcwd()})

@app.post("/api/runs/validate")
async def validate_run(request: Request) -> JSONResponse:
Expand Down
16 changes: 13 additions & 3 deletions agentflow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,25 @@ def _get_run_or_exit(store: object, run_id: str, *, runs_dir: str) -> object:

def _run_pipeline(pipeline: object, runs_dir: str, max_concurrent_runs: int, output: RunOutputFormat) -> None:
store, orchestrator = _build_runtime(runs_dir, max_concurrent_runs)
run_id: str | None = None

async def _run() -> None:
nonlocal run_id
run_record = await orchestrator.submit(pipeline)
completed = await orchestrator.wait(run_record.id, timeout=None)
run_dir = store.run_dir(run_record.id) if hasattr(store, "run_dir") else None
run_id = run_record.id
completed = await orchestrator.wait(run_id, timeout=None)
run_dir = store.run_dir(run_id) if hasattr(store, "run_dir") else None
_echo_run_result(completed, output=output, run_dir=run_dir)
raise typer.Exit(code=0 if _status_value(completed.status) == "completed" else 1)

asyncio.run(_run())
try:
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.

This still exits before cancellation is fully finalized. orchestrator.cancel(run_id) only moves an active run to cancelling; the final transition to cancelled happens later in the background run loop. Because that loop runs on a daemon thread, the process can terminate first and leave run.json stuck in cancelling with no finished_at.

I reproduced this by starting a slow run and sending Ctrl+C. Please wait for cancellation to settle before exiting, or finalize the run synchronously here.

asyncio.run(_run())
except KeyboardInterrupt:
if run_id is not None:
# Bridging Ctrl+C to the same cancellation logic used by 'agentflow cancel'
asyncio.run(orchestrator.cancel(run_id))
typer.echo(f"\nRun {run_id} cancelled.", err=True)
raise typer.Exit(code=130) # Standard SIGINT exit code


def _run_pipeline_path(path: str, runs_dir: str, max_concurrent_runs: int, output: RunOutputFormat) -> None:
Expand Down
8 changes: 8 additions & 0 deletions agentflow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def json_dumps(data: Any) -> str:


def render_template(template_text: str, context: dict[str, Any]) -> str:
if not isinstance(template_text, str):
# If template_text is not a string (e.g. a dict), return its JSON representation
# or string representation instead of letting Jinja2 crash with TypeError.
try:
return json.dumps(template_text, ensure_ascii=False)
except (TypeError, ValueError):
return str(template_text)

template = _TEMPLATE_ENV.from_string(template_text)
return template.render(**context)

Expand Down