Skip to content
Merged
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
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,9 @@ the start of the entry's title.
starts and ends every run (`workflow/agents/delegate.py`), each at its own
end, so of two parallel delegations the faster ends first. Each
manager records its runs by conversation (`BaseWorkflowManager.agent_runs`,
provisional: `records(ref)`, `record(ref, id)`; a record has the brief,
provisional: `records(ref)`, `record(ref, id)`, `number_from(ref, counts)`
to number a conversation's instances past the ones its store keeps; a
record has the brief,
`started_by`, `mode` and, for a cancelled run, `cancelled_by`); a run a
stopped or failed turn leaves open is recorded `interrupted`.
`get_active_run()` (exported
Expand Down Expand Up @@ -1541,6 +1543,23 @@ the start of the entry's title.

### Changed

- **On ADK, a conversation's sub-agent transcripts are deleted with it, and no
run reuses one.** Each sub-agent run's transcript is a child
session of its conversation (app `<app_name>--agents`, id `<conversation
id>/<instance>`), which no conversation list, `/resume` picker or
`--continue` shows. Deleting the conversation deletes them, events and
all, whoever deletes it: `delete_session`, a `SessionReader`
(`/sessions --delete` on another backend's conversation), `/new` on the
memory store, and a conversation already gone leaves none behind
(`adk/sessions.py::delete_children`). Instances are numbered per manager,
so each turn's first run is numbered past the instances the
conversation's store keeps (`BaseWorkflowManager._stored_instances`,
asked once per conversation per turn; `AgentRuns.number_from`), whoever
ran them: a manager before a restart or a switch of backend or store, or
another process that had the conversation between two turns.
`researcher-2` follows a stored `researcher-1`, whose transcript stays as
it was. A run whose instance the store keeps all the same fails rather
than replace it.
- **One `task` tool on both backends, which takes a brief.** **Breaking:**
ADK offered one tool per sub-agent (`researcher(request=...)`) and Deep
Agents its own `task(description=, subagent_type=)`; now every agent
Expand Down
14 changes: 7 additions & 7 deletions CLAUDE.md

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1351,11 +1351,12 @@ reviewer = AgentConfig(name="reviewer", prompt="Check what was said.",
agent built from a config is offered `transfer_to_agent`; each sub-agent
type runs on a runner of its own, each run on a child session (app
`<app_name>--agents`, id `<conversation id>/<instance>`), which
`/sessions` never lists; an inheriting run's child session starts with
the conversation's events. **On Deep Agents**, each sub-agent's graph runs
apart from its parent's, in a task of its own; an inheriting run starts
from the conversation's messages, which its own summarization compacts
when they are too large for its model.
`/sessions` never lists and which is deleted with its conversation; an
inheriting run's child session starts with the conversation's events.
**On Deep Agents**, each sub-agent's graph runs apart from its parent's,
in a task of its own; an inheriting run starts from the conversation's
messages, which its own summarization compacts when they are too large
for its model.
- **The limits in 0.7.0.** `inherit_context` is for sub-agents only, and
only for the main agent's own sub-agents that have none of their own. A
coordinator cannot have a tool of its own named `task`. Each raises
Expand Down
31 changes: 22 additions & 9 deletions src/agentic_cli/workflow/adk/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
child_app,
child_session_id,
make_session_service,
stored_instances,
turn_state,
)
from agentic_cli.workflow.adk.task_tool import SHARED_KEYS, TaskTool
Expand Down Expand Up @@ -1479,6 +1480,8 @@ async def _run_agent(self, run: AgentRun) -> RunAnswer:
RunFailed: Its request no longer fits its model's window
(the overflow handler's answer).
ValueError: Its child session's id is longer than ADK stores.
AlreadyExistsError: The store keeps its instance already (a
stored transcript is never replaced).
Exception: Its model call failed: the provider's error, out of
ADK's plugin wrapper (``_plugin_cause``).
"""
Expand Down Expand Up @@ -1540,23 +1543,33 @@ async def _child_session(
) -> None:
"""Make ``run``'s child session: the state it starts from, and an inheriting run's history.

An instance's session left by an earlier process (instances are
numbered per manager, so a new one numbers ``researcher-1`` again)
is replaced: nothing reads it.
Its instance is new to the conversation's store: instances are
numbered past the ones it keeps (``_stored_instances``). One it
keeps all the same is never replaced: ADK refuses to make it
(``AlreadyExistsError``), and the run fails with that.
"""
service = self._session_service
stale = await service.get_session(
app_name=app, user_id=user_id, session_id=session_id,
config=GetSessionConfig(num_recent_events=0),
)
if stale is not None:
await service.delete_session(app_name=app, user_id=user_id, session_id=session_id)
session = await service.create_session(
app_name=app, user_id=user_id, session_id=session_id, state=dict(run.state)
)
for event in run.history or ():
await service.append_event(session, event)

async def _stored_instances(self, conversation: SessionRef) -> dict[str, int]:
"""The highest instance number of each type ``conversation``'s child sessions keep.

The base's hook (``Delegations`` asks it once per conversation per
turn): another manager's runs (before a restart or a switch of
backend or store, or another process's between two turns) are in
the store, and this one numbers past them.
"""
if self._session_service is None:
return {}
return await stored_instances(
self._session_service, conversation.app_name, conversation.user_id,
conversation.session_id,
)

def _report_raw_log(self) -> None:
"""Report what the raw LLM log holds (a sub-agent run's calls) to the turn's live events.

Expand Down
93 changes: 84 additions & 9 deletions src/agentic_cli/workflow/adk/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
``child_session_id(conversation, instance)`` (``<conversation>/<instance>``),
the conversation's user. Under its own app name it never lists as a
conversation. ADK keys a session by at most ``MAX_KEY`` characters, so a
longer id is refused before anything is stored.
longer id is refused before anything is stored. A conversation's children
are deleted with it (``delete_children``: ``ServiceSessions.delete``, the
manager's and its reader's), and each turn numbers its instances past the
ones they keep (``stored_instances``), so none is ever another run's.
"""

from __future__ import annotations
Expand Down Expand Up @@ -82,6 +85,76 @@ def child_session_id(conversation: str, instance: str) -> str:
return session_id


async def _children(
service: "BaseSessionService", app_name: str, user_id: str, conversation: str
) -> list[str]:
"""The ids of ``conversation``'s child sessions, ``user_id``'s, in
``app_name``'s child app.

Each is ``<conversation>/<instance>``, an instance id holding no ``/``:
a conversation whose own id goes on past a ``/`` (``s/x``) has children
of its own, which are not ``s``'s. Read with ADK's public listing, which
returns each session without its events.
"""
prefix = f"{conversation}/"
listed = await service.list_sessions(app_name=child_app(app_name), user_id=user_id)
return [
session.id for session in getattr(listed, "sessions", None) or ()
if session.id.startswith(prefix) and "/" not in session.id[len(prefix):]
]


async def delete_children(
service: "BaseSessionService", app_name: str, user_id: str, conversation: str
) -> int:
"""Delete ``conversation``'s child sessions: its sub-agent instances' transcripts.

Every session of ``app_name``'s child app (``child_app``) that is
``user_id``'s and ``conversation``'s (``<conversation>/<instance>``);
their events go with them (ADK's session store deletes a session's
events with it). What a conversation's delete leaves of it otherwise.

Args:
service: The session service the conversation is in.
app_name: The conversation's app (not the child app).
user_id: The conversation's user.
conversation: The conversation's id.

Returns:
How many it deleted.
"""
children = await _children(service, app_name, user_id, conversation)
for session_id in children:
await service.delete_session(
app_name=child_app(app_name), user_id=user_id, session_id=session_id
)
return len(children)


async def stored_instances(
service: "BaseSessionService", app_name: str, user_id: str, conversation: str
) -> dict[str, int]:
"""The highest instance number of each agent type ``conversation``'s child sessions keep.

Read from their ids (``<conversation>/<type>-<n>``, ``AgentRuns``'
instance ids): what another manager ran there (before a restart, or in
another process between two turns), which this one numbers past. An id
that names no such instance is left out.

Args:
service: The session service the conversation is in.
app_name: The conversation's app (not the child app).
user_id: The conversation's user.
conversation: The conversation's id.
"""
counts: dict[str, int] = {}
for session_id in await _children(service, app_name, user_id, conversation):
agent, _, number = session_id.rpartition("/")[2].rpartition("-")
if agent and number.isascii() and number.isdigit():
counts[agent] = max(counts.get(agent, 0), int(number))
return counts


def typed_prompts(events: list["Event"]) -> list[str]:
"""The texts the user typed, oldest first."""
prompts: list[str] = []
Expand Down Expand Up @@ -227,24 +300,26 @@ async def row_of(session_id: str) -> SessionInfo | None:
return rows

async def delete(self, ref: SessionRef) -> bool:
"""Delete ``ref``'s session; False if there was none.
"""Delete ``ref``'s session and its sub-agents' transcripts; False if there was none.

ADK's own delete answers nothing, for an unknown id too, so the
session is looked up first, without its events
(``num_recent_events=0``).
(``num_recent_events=0``). Its child sessions (``delete_children``)
are deleted after it, whatever the look found: a conversation
already gone may have left some. The answer is the conversation's.
"""
from google.adk.sessions.base_session_service import GetSessionConfig

stored = await self._service.get_session(
app_name=ref.app_name, user_id=ref.user_id, session_id=ref.session_id,
config=GetSessionConfig(num_recent_events=0),
)
if stored is None:
return False
await self._service.delete_session(
app_name=ref.app_name, user_id=ref.user_id, session_id=ref.session_id,
)
return True
if stored is not None:
await self._service.delete_session(
app_name=ref.app_name, user_id=ref.user_id, session_id=ref.session_id,
)
await delete_children(self._service, ref.app_name, ref.user_id, ref.session_id)
return stored is not None


def session_db_url(settings: Any, store: str) -> str | None:
Expand Down
59 changes: 57 additions & 2 deletions src/agentic_cli/workflow/agents/delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
a brief that cannot start a run is the call's error result, and nothing
else happens (no run, no record, no event);
- names the run (``AgentRuns.run_for``: its instance ``<type>-<n>`` and its run
``<instance>#1``, under the caller's run) and reports its ``AGENT_RUN``
``started``: the title, the call, the brief, who started it, ``blocking``;
``<instance>#1``, under the caller's run; a turn's first run is numbered
past the instances its conversation's store keeps, which ``stored`` says
once per conversation per turn) and reports its ``AGENT_RUN``
``started``: the title, the call, the brief, who started it,
``blocking``;
- runs it through the backend's hook (``BaseWorkflowManager._run_agent``) in
an asyncio task of its own, in which the run is the active run
(``get_active_run``) and no tool call is (``get_active_tool_call``): the
Expand Down Expand Up @@ -65,6 +68,7 @@
from agentic_cli.workflow.sessions import (
RunRef,
SessionRef,
current_turn_scope,
get_active_turn,
set_active_run,
set_active_tool_call,
Expand Down Expand Up @@ -172,6 +176,9 @@ class Delegated:


RunHook = Callable[[AgentRun], Awaitable[RunAnswer]]
#: How many instances of each type a conversation's store already keeps, by
#: type: the highest instance number (``BaseWorkflowManager._stored_instances``).
StoredInstances = Callable[[SessionRef], Awaitable[Mapping[str, int]]]


class RunFailed(Exception):
Expand Down Expand Up @@ -201,6 +208,12 @@ class Delegations:
hook: Runs one run (``BaseWorkflowManager._run_agent``).
ledgers: The manager's usage ledgers: a run's end carries what it
used, by model. None: an end carries no usage.
stored: How many instances of each type a conversation's store
already keeps (``BaseWorkflowManager._stored_instances``), asked
once per conversation per turn, before the turn's first run is
named, so what another manager ran there (before a restart, or
in another process between two turns) is numbered past. None:
every conversation is numbered from 1.
"""

def __init__(
Expand All @@ -211,12 +224,20 @@ def __init__(
types: Mapping[str, AgentConfig],
hook: RunHook,
ledgers: UsageLedgers | None = None,
stored: StoredInstances | None = None,
) -> None:
self._runs = runs
self.policy = policy
self._types = dict(types)
self._hook = hook
self._ledgers = ledgers
self._stored = stored
# Each conversation's turn its stored instances were last read in
# (its span, ``current_turn_scope``: held here, so no later turn's
# can be the same object), and the lock a turn's parallel first runs
# wait at while one reads them.
self._numbered: dict[SessionRef, object] = {}
self._numbering = asyncio.Lock()

def sub_agents(self, agent: str) -> dict[str, AgentConfig]:
"""The types ``agent`` may start, by name, in its ``sub_agents``' order."""
Expand All @@ -236,6 +257,7 @@ async def delegate(self, request: DelegationRequest) -> Delegated:
except BriefError as error:
return Delegated(result={"success": False, "error": str(error)}, state={}, run=None)
conversation = get_active_turn()
await self._number_past_stored(conversation)
ref = self._runs.run_for(conversation, request.caller, request.call_id, config.name)
self._emit(WorkflowEvent.agent_run(
ref, RUN_STARTED, call_id=request.call_id, title=run_title(brief.title),
Expand Down Expand Up @@ -282,6 +304,39 @@ async def delegate(self, request: DelegationRequest) -> Delegated:
result = envelope(ref, RUN_COMPLETED, result=answer.text, partial=partial)
return Delegated(result=result, state=dict(answer.state), run=ref)

async def _number_past_stored(self, conversation: SessionRef | None) -> None:
"""Number ``conversation``'s instances past the ones its store keeps.

Asked once per conversation per turn (``stored``), before the turn's
first run is named; the turn's parallel first runs wait for that one
ask. Each turn asks again: between two turns another process may
have run the conversation (it holds it while its turn runs:
``ConversationLock``), and ``AgentRuns.number_from`` never lowers a
count. A store that cannot be read is warned of in the log and asked
again at the next run, and this one is numbered from what the
manager knows (a stored instance it meets is its backend's to
refuse); a cancel leaves it to be asked again too. With no turn's
span open (``current_turn_scope``), every run asks.
"""
if self._stored is None or conversation is None:
return
turn = current_turn_scope()
if turn is not None and self._numbered.get(conversation) is turn:
return
async with self._numbering:
if turn is not None and self._numbered.get(conversation) is turn:
return
try:
counts = await self._stored(conversation)
except Exception as error: # noqa: BLE001 - the run goes on: the next one asks again
logger.warning("subagent_instances_unread", session_id=conversation.session_id,
error=truncate(str(error), _MAX_LOGGED_ERROR))
logger.debug("subagent_instances_unread", exc_info=error)
return
self._runs.number_from(conversation, counts)
if turn is not None:
self._numbered[conversation] = turn

async def _in_run(
self,
ref: RunRef,
Expand Down
19 changes: 18 additions & 1 deletion src/agentic_cli/workflow/agents/runs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""A manager's sub-agent runs, kept by conversation.

Each run is named readably within its conversation: its instance
``<agent>-<n>`` (``researcher-2``, numbered per agent) and its run
``<agent>-<n>`` (``researcher-2``, numbered per agent, past the instances
the conversation's store already keeps: ``number_from``) and its run
``<instance>#<n>`` (each instance runs once: ``#1``). The framework names a
run as it starts it (``agents/delegate.py::Delegations``) with ``run_for``,
keyed by (conversation, parent run, the parent's call id): the same key
Expand Down Expand Up @@ -201,6 +202,22 @@ def run_for(
self._runs[key] = ref
return ref

def number_from(self, conversation: SessionRef | None, counts: Mapping[str, int]) -> None:
"""Number ``conversation``'s next instances of each agent past ``counts``.

``counts[agent]`` is the highest instance number of ``agent`` the
conversation's store already keeps (another manager's: before a
restart or a switch of backend or store, or another process's between
two turns), so ``run_for``'s next instance of it
is ``<agent>-<counts[agent] + 1>`` at the earliest. A count is never
lowered: an instance this manager numbered already stays numbered,
and an open run keeps its name.
"""
for agent, count in counts.items():
key = (conversation, agent)
if count > self._numbered.get(key, 0):
self._numbered[key] = count

def observe(self, event: WorkflowEvent) -> None:
"""Record what a run's ``AGENT_RUN`` event says, under the turn it is reported in.

Expand Down
Loading
Loading