Skip to content
Merged
109 changes: 109 additions & 0 deletions backend/app/agent_runs/running.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"""Runs at work now, described once for every screen that lists them: the Dashboard's Running now and the Agents
pages. A run works on a task (or a chat's round), answers a question, runs a cron or writes a Knowledge Base draft."""

import uuid
from collections.abc import Collection

from sqlalchemy import ColumnElement, Select, func, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import aliased

from app.a2a.models import Question
from app.agent_runs.models import AgentRun, RunStatus
from app.agent_runs.schemas import RunningItem
from app.agents.models import Agent
from app.chats.models import Chat
from app.chats.service import chat_link
from app.crons.models import Cron, CronRun
from app.dashboard.scope import task_link
from app.projects.models import Project
from app.tasks.models import Task
from app.workflow.models import DocKind, DocStatus, Stage, TaskWorkflow, WorkflowDocument


def run_targets(*columns) -> Select:
"""A select over runs with what each works on joined: its task, its question, or its cron run and cron."""
return (
select(*columns).select_from(AgentRun)
.outerjoin(Task, Task.id == AgentRun.task_id)
.outerjoin(Question, Question.id == AgentRun.question_id)
.outerjoin(CronRun, CronRun.id == AgentRun.cron_run_id)
.outerjoin(Cron, Cron.id == CronRun.cron_id)
)


def run_project_id() -> ColumnElement[uuid.UUID]:
"""The project a run works in (over run_targets): its task's, its question's target, or its cron's; None for a
Knowledge Base draft."""
return func.coalesce(Task.project_id, Question.target_project_id, Cron.project_id)


async def _running(
db: AsyncSession, where: ColumnElement[bool], openable: Collection[uuid.UUID] | None
) -> list[tuple[uuid.UUID, RunningItem]]:
"""(agent id, item) for each running run matching `where`, newest started first. `openable`: the projects the
viewer can open (None: all); a run elsewhere keeps its text but loses its link."""
asking_task, asking_project = aliased(Task), aliased(Project)
project_id = run_project_id()
steps_total = (
select(func.jsonb_array_length(WorkflowDocument.steps))
.where(WorkflowDocument.task_id == AgentRun.task_id, WorkflowDocument.kind == DocKind.plan,
WorkflowDocument.status == DocStatus.approved)
.order_by(WorkflowDocument.version.desc()).limit(1).scalar_subquery()
)
stmt = (
run_targets(AgentRun.agent_id, AgentRun.id, Agent.name, AgentRun.started_at, project_id, Project.key,
Task.number, Task.title, Task.chat_id, Task.in_chat, Chat.title, AgentRun.stage, AgentRun.step,
steps_total, TaskWorkflow.review_round, AgentRun.question_id, Question.number,
asking_project.key, Cron.id, Cron.name)
.join(Agent, Agent.id == AgentRun.agent_id)
.outerjoin(Project, Project.id == project_id)
.outerjoin(Chat, Chat.id == Task.chat_id)
.outerjoin(TaskWorkflow, TaskWorkflow.task_id == AgentRun.task_id)
.outerjoin(asking_task, asking_task.id == Question.from_task_id)
.outerjoin(asking_project, asking_project.id == asking_task.project_id)
.where(AgentRun.status == RunStatus.running, where)
.order_by(AgentRun.started_at.desc().nulls_last(), AgentRun.id)
)
out = []
for (agent_id, run_id, name, started, pid, key, number, task_title, chat_id, in_chat, chat_title, stage, step,
total, rounds, question_id, question_number, asked_from, cron_id, cron_name) in (await db.execute(stmt)).all():
title = question_key = link = None
if number is not None and in_chat and chat_id: # a chat's round; added to the board it is a plain task
kind, title, link = "chat", chat_title, chat_link(chat_id)
elif number is not None:
kind, title, link = "task", task_title, task_link(key, number)
elif question_id is not None:
kind, question_key, link = "question", f"{key}-Q{question_number}", f"/questions/{question_id}"
elif cron_id is not None:
kind, title, link = "cron", cron_name, f"/projects/{key}/crons/{cron_id}"
else:
kind = "draft" # only the draft's person can open it
if openable is not None and pid not in openable:
link = None
out.append((agent_id, RunningItem(
run_id=run_id, agent_name=name, started_at=started, kind=kind, title=title,
task_key=f"{key}-{number}" if number is not None else None, question_key=question_key,
stage=stage, step=step if stage == Stage.build else None,
steps_total=total if stage == Stage.build else None,
review_round=rounds if stage == Stage.review else None,
question_from=asked_from if question_id else None,
link=link, chat_id=chat_id if kind == "chat" else None,
)))
return out


async def running_items(db: AsyncSession, where: ColumnElement[bool]) -> list[RunningItem]:
"""The running runs matching `where` (a condition over run_targets), newest started first."""
return [item for _, item in await _running(db, where, None)]


async def running_by_agent(
db: AsyncSession, agent_ids: Collection[uuid.UUID], openable: Collection[uuid.UUID] | None
) -> dict[uuid.UUID, list[RunningItem]]:
"""Each agent's running runs, drafts and cron runs too, newest started first; agents with none are left out."""
by_agent: dict[uuid.UUID, list[RunningItem]] = {}
if agent_ids:
for agent_id, item in await _running(db, AgentRun.agent_id.in_(set(agent_ids)), openable):
by_agent.setdefault(agent_id, []).append(item)
return by_agent
23 changes: 23 additions & 0 deletions backend/app/agent_runs/schemas.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import uuid
from datetime import datetime
from typing import Literal

from pydantic import BaseModel

from app.agent_runs.models import RunPolicy, RunStatus
from app.agents.models import AgentProgram
from app.workflow.models import Stage

RunKind = Literal["task", "chat", "question", "cron", "draft"]


class RunAgentOut(BaseModel):
Expand All @@ -26,3 +30,22 @@ class RunOut(BaseModel):
created_at: datetime
started_at: datetime | None
finished_at: datetime | None


class RunningItem(BaseModel):
"""A run at work now, as the Dashboard's Running now and the Agents pages list it."""

run_id: uuid.UUID
agent_name: str
started_at: datetime | None
kind: RunKind
title: str | None = None # the task's title, the chat's title or the cron's name
task_key: str | None
question_key: str | None = None # answering runs: <KEY>-Q<n>
stage: Stage | None
step: int | None
steps_total: int | None
review_round: int | None
question_from: str | None # answering runs: the asking task's project key
link: str | None # None: a Knowledge Base draft, or a run in a project the viewer cannot open
chat_id: uuid.UUID | None = None
4 changes: 3 additions & 1 deletion backend/app/agents/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pydantic import AfterValidator, BaseModel, Field

from app.agent_runs.schemas import RunningItem
from app.agents.models import AgentProgram, AnswerMode
from app.auth.schemas import UserOut
from app.core.text import NoNulStr
Expand Down Expand Up @@ -99,6 +100,7 @@ class AgentOut(BaseModel):
owner: UserOut
status: str # busy: every one of its max_concurrent_runs slots is taken
running: int = 0 # its running runs; above max_concurrent_runs after the owner lowered it
runs: list[RunningItem] = [] # owner and admins only: what it works on now, newest first
problems: list[str] = []
bindings: list[BindingOut]
runner: RunnerInfo | None = None # None while the owner's runner is offline
Expand All @@ -114,4 +116,4 @@ class ProjectAgentOut(AgentOut):


class AdminAgentOut(AgentOut):
current_run: dict | None = None
"""An agent on Admin → Agents (every agent in the workspace)."""
42 changes: 8 additions & 34 deletions backend/app/agents/service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re
import uuid
from datetime import UTC, datetime

from fastapi import HTTPException, status
from sqlalchemy import ColumnElement, func, or_, select, update
Expand Down Expand Up @@ -32,7 +31,6 @@
WORKFLOW_PROBLEM,
registry,
)
from app.tasks.models import Task
from app.teams.models import TeamMember
from app.teams.service import user_team_ids
from app.workflow.models import TaskWorkflow, WorkflowStatus
Expand Down Expand Up @@ -246,7 +244,8 @@ async def publish_agents_updated(

async def _base_fields(db: AsyncSession, agents: list[Agent], viewer: User) -> list[dict]:
"""Common AgentOut fields. Bindings are limited to projects the viewer can see;
local_path only goes to the owner."""
local_path only goes to the owner. Runs go to the owner and admins only, and a run in a project the viewer
cannot open has no link."""
if not agents:
return []
owners = {
Expand All @@ -265,6 +264,10 @@ async def _base_fields(db: AsyncSession, agents: list[Agent], viewer: User) -> l
statuses = await agent_statuses(db, list(owner_of), {a.id: a.max_concurrent_runs for a in agents})
running = await running_counts(db, list(owner_of))
allowed = await accessible_project_ids(db, viewer)
from app.agent_runs.running import running_by_agent # local: app.chats.service, which it uses, imports this module

private = [a.id for a in agents if a.owner_id == viewer.id or viewer.role == Role.admin]
runs = await running_by_agent(db, private, allowed)
rows = await db.execute(
select(AgentBinding, Project.key)
.join(Project, Project.id == AgentBinding.project_id)
Expand Down Expand Up @@ -292,6 +295,7 @@ async def _base_fields(db: AsyncSession, agents: list[Agent], viewer: User) -> l
"owner": UserOut.model_validate(owners[a.owner_id]),
"status": statuses[a.id],
"running": running.get(a.id, 0),
"runs": runs.get(a.id, []),
"problems": agent_problems(
a.id, a.owner_id, viewer, bool(a.model or a.effort), a.program, stages=bool(a.stage_models)
),
Expand Down Expand Up @@ -330,35 +334,5 @@ async def project_agents_out(
]


def _newest(running: dict, agent_id: uuid.UUID, run: dict) -> None:
"""Keep the most recently started run per agent (a run without a start time counts as oldest)."""
oldest = datetime.min.replace(tzinfo=UTC)
current = running.get(agent_id)
if current is None or (run["started_at"] or oldest) > (current["started_at"] or oldest):
running[agent_id] = run


async def admin_agents_out(db: AsyncSession, agents: list[Agent], viewer: User) -> list[AdminAgentOut]:
fields = await _base_fields(db, agents, viewer)
from app.a2a.models import Question # local: a2a imports this module

ids = [a.id for a in agents]
running: dict = {}
# ADM-3: what each agent works on now, a task or (§6.6) a question it answers; the newest run wins
for agent_id, run_id, pkey, num, run_status, started_at in await db.execute(
select(AgentRun.agent_id, AgentRun.id, Project.key, Task.number, AgentRun.status, AgentRun.started_at)
.join(Task, Task.id == AgentRun.task_id)
.join(Project, Project.id == Task.project_id)
.where(AgentRun.agent_id.in_(ids), AgentRun.status == RunStatus.running)
):
_newest(running, agent_id, {"id": run_id, "kind": "task", "task_key": f"{pkey}-{num}", "status": run_status,
"started_at": started_at})
for agent_id, run_id, pkey, num, run_status, started_at in await db.execute(
select(AgentRun.agent_id, AgentRun.id, Project.key, Question.number, AgentRun.status, AgentRun.started_at)
.join(Question, Question.id == AgentRun.question_id)
.join(Project, Project.id == Question.target_project_id)
.where(AgentRun.agent_id.in_(ids), AgentRun.status == RunStatus.running)
):
_newest(running, agent_id, {"id": run_id, "kind": "question", "task_key": f"{pkey}-Q{num}",
"status": run_status, "started_at": started_at})
return [AdminAgentOut(**f, current_run=running.get(f["id"])) for f in fields]
return [AdminAgentOut(**f) for f in await _base_fields(db, agents, viewer)]
63 changes: 14 additions & 49 deletions backend/app/dashboard/agents.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
"""Agents and their runs: who is working, idle or offline, what is running and queued, and failed runs. A run is in
scope through its task's project, or (a run answering another project's question) the question's target project."""
scope through its project: its task's, the question's target project (a run answering another project's question),
or its cron's."""

from datetime import UTC, datetime, timedelta

from sqlalchemy import exists, func, select
from sqlalchemy import ColumnElement, and_, exists, func, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import aliased

from app.a2a.models import Question
from app.agent_runs.models import AgentRun, RunStatus
from app.agent_runs.running import run_project_id, run_targets, running_items
from app.agents.models import Agent, AgentBinding
from app.agents.service import agent_statuses, runner_info
from app.chats.service import chat_link
from app.dashboard.schemas import AgentItem, Queued, QueuedItem, RunningItem
from app.dashboard.scope import Scope, task_link
from app.projects.models import Project
from app.tasks.models import Task
from app.workflow.models import DocKind, DocStatus, Stage, TaskWorkflow, WorkflowDocument

QUEUED_SHOWN = 5
_ORDER = {"busy": 0, "busy_elsewhere": 1, "online": 2, "offline": 3}


def _in_scope(scope: Scope) -> ColumnElement[bool]:
"""Runs in scope, over run_targets: through their project (a task's, a question's target or a cron's)."""
# a Knowledge Base draft's turns are its person's own: not on anyone's dashboard
cond = and_(AgentRun.draft_id.is_(None), scope.in_projects(run_project_id()))
return and_(cond, AgentRun.agent_id.in_(scope.agent_ids)) if scope.name == "mine" else cond


def _runs(scope: Scope, *columns):
"""A select over runs in scope, with the run's task (if any) and question (if any) joined."""
stmt = (
select(*columns).select_from(AgentRun)
.outerjoin(Task, Task.id == AgentRun.task_id).outerjoin(Question, Question.id == AgentRun.question_id)
# a Knowledge Base draft's turns are its person's own: not on anyone's dashboard
.where(AgentRun.draft_id.is_(None), scope.in_projects(func.coalesce(Task.project_id, Question.target_project_id)))
)
if scope.name == "mine":
stmt = stmt.where(AgentRun.agent_id.in_(scope.agent_ids))
return stmt
"""A select over runs in scope, with the run's task, question and cron joined."""
return run_targets(*columns).where(_in_scope(scope))


async def agents(db: AsyncSession, scope: Scope) -> list[AgentItem]:
Expand Down Expand Up @@ -67,39 +64,7 @@ async def agents(db: AsyncSession, scope: Scope) -> list[AgentItem]:


async def running(db: AsyncSession, scope: Scope) -> list[RunningItem]:
asking_task, asking_project = aliased(Task), aliased(Project)
steps_total = (
select(func.jsonb_array_length(WorkflowDocument.steps))
.where(WorkflowDocument.task_id == AgentRun.task_id, WorkflowDocument.kind == DocKind.plan,
WorkflowDocument.status == DocStatus.approved)
.order_by(WorkflowDocument.version.desc()).limit(1).scalar_subquery()
)
stmt = (
_runs(scope, AgentRun.id, Agent.name, AgentRun.started_at, Project.key, Task.number, AgentRun.stage,
AgentRun.step, steps_total, TaskWorkflow.review_round, AgentRun.question_id, asking_project.key,
Task.chat_id, Task.in_chat)
.join(Agent, Agent.id == AgentRun.agent_id)
.outerjoin(Project, Project.id == Task.project_id)
.outerjoin(TaskWorkflow, TaskWorkflow.task_id == AgentRun.task_id)
.outerjoin(asking_task, asking_task.id == Question.from_task_id)
.outerjoin(asking_project, asking_project.id == asking_task.project_id)
.where(AgentRun.status == RunStatus.running)
.order_by(AgentRun.started_at.desc().nulls_last())
)
out = []
for run_id, name, started, key, number, stage, step, total, rounds, question_id, asked_from, chat_id, in_chat in (
await db.execute(stmt)
).all():
out.append(RunningItem(
run_id=run_id, agent_name=name, started_at=started, task_key=f"{key}-{number}" if key else None,
stage=stage, step=step if stage == Stage.build else None,
steps_total=total if stage == Stage.build else None,
review_round=rounds if stage == Stage.review else None,
question_from=asked_from if question_id else None,
link=(chat_link(chat_id) if in_chat else task_link(key, number)) if key else f"/questions/{question_id}",
chat_id=chat_id if in_chat else None,
))
return out
return await running_items(db, _in_scope(scope))


async def queued(db: AsyncSession, scope: Scope) -> Queued:
Expand Down
15 changes: 1 addition & 14 deletions backend/app/dashboard/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pydantic import BaseModel

from app.agent_runs.schemas import RunningItem
from app.agents.schemas import RunnerInfo
from app.workflow.models import Stage

Expand Down Expand Up @@ -53,20 +54,6 @@ class MyTask(BaseModel):
link: str


class RunningItem(BaseModel):
run_id: uuid.UUID
agent_name: str
started_at: datetime | None
task_key: str | None
stage: Stage | None
step: int | None
steps_total: int | None
review_round: int | None
question_from: str | None # answering runs: the asking task's project key
link: str
chat_id: uuid.UUID | None = None


class QueuedItem(BaseModel):
task_key: str | None
stage: Stage | None
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/agent_runs/test_audit_and_cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ async def test_admin_agents_shows_answering_run(client, client_factory, db):
finally:
_cleanup(m1)
[row] = [r for r in rows if r["id"] == a1["id"]]
assert row["current_run"]["kind"] == "question" and row["current_run"]["task_key"] == "QBB-Q1"
assert [(r["kind"], r["question_key"]) for r in row["runs"]] == [("question", "QBB-Q1")]
Loading
Loading