feat: build on kept work by reference with sources_from (ADR-0004 phase 2)

A request names work the Orchestration engine kept instead of carrying its
files: a record or latest link from an earlier document of links (checked,
and of the request's tenant and Customer case), latest/<work>, or an
Execution's id. The kept files and the record that the work completed are
the agent's sources. Nothing starts, and the caller is told why, when the
reference does not resolve to completed work it may use.

A sequence's next agent is given what was kept of the one before, the same
way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 21:00:26 +01:00
parent 6d32bc696d
commit 1ec939f4fd
8 changed files with 430 additions and 19 deletions

View File

@@ -44,7 +44,7 @@ from google.protobuf import json_format
from orchestration.agents import Agent
from orchestration.agents import Sequence as AgentSequence
from orchestration.delivery import Delivery
from orchestration.delivery import RECORD, UNASSIGNED, Delivery
from orchestration.routing import WORK, route
logger = logging.getLogger(__name__)
@@ -62,9 +62,8 @@ _PAUSES = {TaskState.TASK_STATE_INPUT_REQUIRED, TaskState.TASK_STATE_AUTH_REQUIR
# What every agent of a sequence is sent of the caller's fields. The rest go to the
# first agent only, or to the agent whose work names them: `operating_model: {...}`.
_CARRIED = ("customer_case", "pre_approved", "request")
# The Orchestration engine's own record that an agent's Execution completed, sent
# with its files to the next agent.
RECORD = "execution.json"
# Where a request names work kept earlier to build on (ADR-0004, phase 2).
SOURCES_FROM = "sources_from"
# How a caller is given what agents delivered (ADR-0004): one document of links to
# what was kept, or each file as the agent sent it.
DELIVER, LINKS, FILES = "deliver", "links", "files"
@@ -87,6 +86,8 @@ class Plan:
deliver: str = FILES
# Each agent that completed, by its work: its Execution, how it ended, links.
results: dict[str, dict[str, Any]] = field(default_factory=dict)
# What was kept of each, by its work, for the agent after it to build on.
kept: dict[str, dict[str, Any]] = field(default_factory=dict)
def metadata(self, step: int, sources: Mapping[str, str] | None) -> dict[str, Any]:
"""The metadata for the agent at `step`."""
@@ -203,8 +204,36 @@ class RelayExecutor(AgentExecutor):
work=str(options.get(WORK) or decision.agents[0].work),
deliver=_deliver(options),
)
if SOURCES_FROM in fields:
try:
self._build_on(fields)
except ValueError as exc:
await updater.complete(
updater.new_agent_message(
[
new_text_part(f"Nothing was started: {exc}"),
new_data_part(
{"status": "not_started", "reason": str(exc)}
),
]
)
)
return
await self._run(task_id, plan, 0, self._opening(plan, 0, None), updater)
def _build_on(self, fields: dict[str, Any]) -> None:
"""Make the work a request names in `sources_from` its sources, and its
Customer case the request's when it names none."""
reference = str(fields.pop(SOURCES_FROM) or "")
if self._delivery is None:
raise ValueError("Nothing is kept here to build on.")
if fields.get("sources"):
raise ValueError("Give sources or sources_from, not both.")
record = self._delivery.resolve(reference, fields)
if not fields.get("customer_case") and record["customer_case"] != UNASSIGNED:
fields["customer_case"] = record["customer_case"]
fields["sources"] = self._delivery.sources(record)
async def _resume(
self,
task_id: str,
@@ -381,6 +410,7 @@ class RelayExecutor(AgentExecutor):
else None
)
if record is not None and self._delivery is not None:
plan.kept[agent.work] = record
result.update(self._delivery.links(record))
else:
result["files"] = {name: None for name in sorted(files)}
@@ -429,17 +459,22 @@ class RelayExecutor(AgentExecutor):
not_started,
)
return
record = {
"status": "completed",
"customer_case": plan.fields.get("customer_case"),
"execution_id": plan.results[agent.work]["execution_id"] or None,
"workflow_request": plan.text,
"work": agent.work,
}
sources = {
**files,
RECORD: json.dumps({k: v for k, v in record.items() if v is not None}),
}
kept = plan.kept.get(agent.work)
if kept is not None and self._delivery is not None:
# What was kept, as a request naming it in sources_from would have it.
sources = self._delivery.sources(kept)
else:
record = {
"status": "completed",
"customer_case": plan.fields.get("customer_case"),
"execution_id": plan.results[agent.work]["execution_id"] or None,
"workflow_request": plan.text,
"work": agent.work,
}
sources = {
**files,
RECORD: json.dumps({k: v for k, v in record.items() if v is not None}),
}
following = plan.steps[step + 1]
await updater.update_status(
TaskState.TASK_STATE_WORKING,