feat: keep what agents deliver and serve it at signed links (ADR-0004)

The Orchestration engine keeps every completed agent Execution it relays,
by tenant, Customer case and Execution, and serves its record and files at
signed, expiring links. A KAB workflow step, which takes one output per
step, is given one JSON document of links (`deliver: links`); other callers
still get each file, and what they are given is kept either way.

- store.py, links.py, delivery.py, serving.py; the relay keeps and links
- the card declares `deliver`, `sources` and an output schema
- image: STORE_DIR on a /data volume; PUBLIC_URL and LINK_SECRET settings
- option 1's tests replaced by tests of the links document

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-21 20:05:47 +01:00
parent ce5e2f20cd
commit 6d32bc696d
17 changed files with 1235 additions and 205 deletions

View File

@@ -7,7 +7,7 @@ One directory per iteration. Each holds the plan for that iteration and, for fin
| # | Directory | Status | What it is |
| --- | --- | --- | --- |
| 1 | `iter-01-execution-engine/` | steps 1 to 6 built on 2026-10-13; 7 planned | The Execution engine: brought from the Reference, given a Workflow, and proved on a toy Workflow and on the Reference's own. |
| 2 | `iter-02-orchestration-engine/` | F1 proposed; F2 and F4 built on 2026-10-20 | The Orchestration engine: routes each request to the agent for its work and relays everything back. |
| 2 | `iter-02-orchestration-engine/` | F1 proposed; F2 and F4 built on 2026-10-20; K1 (ADR-0004) built on 2026-10-21 | The Orchestration engine: routes each request to the agent for its work and relays everything back. |
The plan across both repositories is `../tom_analysis/IMPLEMENTATION_PLAN.md`.

View File

@@ -2,7 +2,7 @@
**Iteration:** 2, the Orchestration engine
**Intent:** The service every request comes through. It decides which agent the work belongs to, dispatches it, and relays every question and Approval. First behind it: the operating model agent (`../tom_analysis`).
**Status:** F1 proposed; F2 and F4 built 2026-10-20
**Status:** F1 proposed; F2 and F4 built 2026-10-20; K1 built 2026-10-21
## Decided by the owner, 2026-10-20
@@ -13,6 +13,7 @@
- `../tom_analysis/IMPLEMENTATION_PLAN.md`, sections 2 and 4, and steps F1 to F5 in section 10.
- `docs/adr/0003-one-orchestration-engine-above-the-agents.md`.
- `docs/adr/0004-the-orchestration-engine-keeps-what-agents-deliver.md`.
- `../tom_analysis/.agents/plans/iter-02-platform-deployment/`: how an agent runs on the local KAB platform.
## Status
@@ -24,3 +25,6 @@
| F3 | The decision: readings in code, the Jev question, thresholds, the fallbacks, the record | A labelled set of messages is routed correctly; low confidence asks; no Jev key still routes what is named | planned |
| F4 | "Both": architecture, then its files as `sources` to the operating model agent | One request yields both sets of Deliverables under one Customer case | built against scripted agents (`steps/step-4.md`), ahead of F3 at the owner's request; not yet run with real models |
| F5 | Later messages to the agent that owns the work | A change to the architecture is followed by a change to the operating model | planned |
| K1 | What agents deliver, kept and served at signed links; a platform step given one document of links (ADR-0004, phase 1) | A platform step's output links to each delivered file, and each link serves it | built (`steps/step-K1.md`); the owner's run through the platform to follow |
| K2 | `sources_from`: a request names kept work instead of carrying its files; the sequence moved onto it | An operating model started from the latest architecture of a Customer case | planned |
| K3 | Object storage shared with the platform, the platform's sign-in, retention, each file as a platform artifact | A kept guide is listed among the project's artifacts | planned |

View File

@@ -0,0 +1,29 @@
# Step K1: what agents deliver, kept and linked (ADR-0004, phase 1)
Built 2026-10-21 on `feat/delivery-store`, the owner having chosen option 3 of three ways to make agents' outputs available in the KAB platform.
## What was built
- **`store.py`:** every completed agent Execution relayed is kept under `<STORE_DIR>/tenants/<tenant>/cases/<case>/executions/<id>/`: its files as the agent named them, and `record.json` (work, how it ended, the request, tenant, Customer case, project, the guide's name, the files, when). Written once; a second write is refused. File names that leave the Execution, and tenants or cases that are not slugs, are refused. `latest` is the newest Execution of a work that ended `completed`.
- **`links.py`:** a link is the path with `exp` and `sig`, an HMAC of both under `LINK_SECRET`; `LINK_TTL_SECONDS`, 30 days by default, 0 for never.
- **`delivery.py`:** keeping for the relay, with the tenant from the platform's `tenant_id` (`local` otherwise) and the Customer case (`unassigned` otherwise), and the links to a record, its guide, each file, the latest of its work and its Customer case. A failure to keep is logged and never fails the caller's task.
- **`serving.py`:** `GET /tenants/{tenant}/cases/{case}`, `…/executions/{id}`, `…/executions/{id}/files/{name}` and `…/latest/{work}`. Each checks its link's signature against the path it was made for, or answers 403; a record is served with fresh links to its files.
- **The relay:** a completed agent's files are kept, whatever the caller asked for. `deliver: links` gives the caller one artifact, `result.json`: the status, the work named, the Customer case, and links to the last agent's record, guide, files, latest and case, with each agent's under `results`. A KAB workflow step (it sends `workflow_id` and `step_number`) is given links unless it asks for `files`; any other caller, files. The guide is the file the agent sent as markdown, as the Execution engine sends its final Deliverable. `sources` given as data, as a platform step maps an earlier output, are sent to the agent as text.
- **The card:** `deliver` and `sources` among the inputs, and an `outputSchema` for the document of links.
- **Deployment:** `STORE_DIR=/data/store` and a `/data` volume in the image; in the dev kit, the `orchestration-data` volume, `PUBLIC_URL=http://localhost:10030`, and `LINK_SECRET` from `ORCHESTRATION_LINK_SECRET` in the kit's `.env`, which is not committed.
## What differed from the plan
- The tests of option 1 (`test_document.py`, on `main`) are replaced by `test_links.py`: a platform step is given links, not the files' content.
- A file's link carries the name as it was quoted, and is checked against the request's raw path.
## Verified
- 25 tests in `test_store.py` and `test_links.py`: keeping, media types, never changing what was kept, refusing names and slugs, `latest`, signing, expiry; a platform step given links that serve what was delivered; the guide; unsigned and changed links refused; files still kept when files are asked for; a sequence's links, and a stopped sequence's; `latest` and the case listing; sources as data sent as text; the card.
- 577 tests pass; `ruff` and `mypy` clean.
- Deployed on the local platform: healthy, the store on its volume, the secret and the public URL set, the card declaring `deliver` and the document. An unsigned link is refused.
## Not done
- No run through the platform yet: the owner runs it from a workflow.
- Phase 2, `sources_from` (a request naming kept work instead of carrying files); phase 3, object storage, the platform's sign-in, retention, and each file as a platform artifact.

View File

@@ -4,7 +4,9 @@
# podman build -f Dockerfile.orchestration -t orchestration-engine .
#
# Which agents it routes to is configuration, mounted at /app/agents.yaml
# (AGENTS_FILE); deploy/agents.local.yaml is the local KAB platform's.
# (AGENTS_FILE); deploy/agents.local.yaml is the local KAB platform's. What agents
# deliver is kept in /data/store (ADR-0004); give it a volume, a PUBLIC_URL and a
# LINK_SECRET.
FROM python:3.12-slim AS builder
@@ -21,7 +23,8 @@ ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
A2A_HOST=0.0.0.0 \
A2A_PORT=8080 \
AGENTS_FILE=/app/agents.yaml
AGENTS_FILE=/app/agents.yaml \
STORE_DIR=/data/store
WORKDIR /app
@@ -30,8 +33,10 @@ RUN groupadd -g 10001 appgroup && \
COPY --from=builder /install /usr/local
COPY deploy/agents.local.yaml /app/agents.yaml
RUN mkdir -p /data/store && chown -R appuser:appgroup /data
USER appuser
VOLUME ["/data"]
EXPOSE 8080
CMD ["python", "-m", "orchestration.main"]

View File

@@ -0,0 +1,33 @@
---
status: accepted
---
# The Orchestration engine keeps what agents deliver
An agent on the Execution engine delivers its work as named files: a guide, the data files behind it, infrastructure code. The KAB platform takes one output per workflow step and keeps it as one artifact; a set of named files reaches it as one unnamed string. Nothing delivered had an address of its own, so a guide could not be shared or cited, and a later request could not build on earlier work without the caller carrying its files.
We decide that **the Orchestration engine keeps every file an agent delivers through it, and serves each at a stable address.**
- **What is kept:** the files of every agent's Execution that completes, as the agent delivered them, with a record: the work, the status the agent ended with, the request, when, the Customer case, and the tenant and project a platform step names.
- **Where:** a store of its own, by tenant, Customer case and Execution. Files are copied in, not referred to in an agent's container, so they outlive any agent's deployment.
- **Never changed.** An Execution's files are written once. A Change request is a new Execution. Only `latest` moves: the newest completed Execution of a kind of work in a Customer case.
- **Addresses:** `/tenants/{tenant}/cases/{case}` lists a Customer case; `…/executions/{id}` is one Execution's record with links to its files; `…/executions/{id}/files/{path}` is a file with its media type; `…/latest/{work}` is the record of the newest completed Execution of that work.
- **Read only by signed link.** Every address carries an expiring signature the Orchestration engine made, because the files name customers, people and suppliers. The base of every link is a setting, as the address a browser uses is not the one inside the platform's network.
- **A caller that takes one output per step is given links.** A KAB workflow step, or any caller that asks (`deliver: links`), is given one JSON document of links to what was kept. A caller that asks for files is given them one by one, as before; they are kept either way.
This keeps ADR-0003's rule that the Orchestration engine produces nothing: it keeps what agents produced. It replaces ADR-0003's consequence that nothing outlives a restart for delivered work; tasks in flight are still held in memory.
Accepted by the owner on 2026-10-21, as option 3 of three ways to make agents' outputs available in the platform.
## Considered Options
- **One JSON document with every file's content (option 1):** rejected. Nothing has an address, a large architecture can pass the platform's inline limit, and earlier work is reusable only by wiring steps on a canvas.
- **The platform keeps each named artifact (option 2):** not rejected, and not ours to make: a change to how the platform's worker treats every agent. Kept as the platform team's to consider; a shared object store later lets each kept file become a platform artifact without moving it.
- **Links into each agent's own deliverables folder:** rejected. Those folders are private to a container and go with it.
## Consequences
- The Orchestration engine has a volume, a public base URL and a signing secret. Without a secret set, one is made at start-up and links stop working on a restart.
- Links expire (`LINK_TTL_SECONDS`, 30 days by default; 0 for never). A record fetched with a valid link carries fresh links to its files.
- A request may name earlier work by reference rather than carry its files (`sources_from`): the next step, with the sequence moved onto it.
- Object storage, the platform's own sign-in and retention follow, in that order.

View File

@@ -81,6 +81,42 @@ def build_card(
)
_LINK = {"type": ["string", "null"], "format": "uri"}
# The document of links a caller asking for links is given (ADR-0004).
OUTPUT_SCHEMA: dict[str, Any] = {
"type": "object",
"properties": {
"status": {
"type": "string",
"description": "completed, completed_with_warnings, or stopped when a "
"sequence did not run to its end.",
},
"work": {"type": "string", "description": "The work that was done."},
"customer_case": {"type": "string"},
"execution_id": {
"type": "string",
"description": "The last agent's Execution.",
},
"record": {**_LINK, "description": "The last Execution's record and links."},
"guide": {**_LINK, "description": "The last agent's guide."},
"files": {
"type": "object",
"description": "A signed link to each file the last agent delivered.",
},
"latest": {
**_LINK,
"description": "The newest completed Execution of this work.",
},
"case": {**_LINK, "description": "Every Execution kept for the Customer case."},
"results": {
"type": "object",
"description": "Each agent's Execution, how it ended and its links, by its work.",
},
"not_started": {"type": "array", "items": {"type": "string"}},
},
}
def input_contract(
agents: Sequence[Agent], sequences: Sequence[AgentSequence] = ()
) -> dict[str, Any]:
@@ -115,6 +151,22 @@ def input_contract(
"uiFieldType": "textarea",
"description": "The spec to start from, as a YAML or JSON document.",
},
"sources": {
"type": "object",
"description": (
"Files to start from, by name, such as an earlier step's "
"`files`: another agent's delivered work."
),
},
"deliver": {
"type": "string",
"enum": ["links", "files"],
"description": (
"links: one document of signed links to what the agents "
"delivered, kept here (a platform step is given this). "
"files: each file as the agent sent it."
),
},
"pre_approved": {
"type": "boolean",
"default": True,
@@ -126,6 +178,7 @@ def input_contract(
},
"required": [WORK, "customer_case"],
},
"outputSchema": OUTPUT_SCHEMA,
}

View File

@@ -26,3 +26,15 @@ class OrchestrationSettings(BaseSettings):
# How long to wait to connect to an agent. Reading waits as long as the
# agent's work takes.
CONNECT_TIMEOUT_SECONDS: float = 10
# What agents deliver is kept here and served at signed links (ADR-0004).
# Unset: nothing is kept, and a caller asking for links gets none.
STORE_DIR: str | None = None
# The base of every link: the address a browser reaches this service at, which
# is not its address inside the platform's network. Unset: A2A_PUBLIC_URL.
PUBLIC_URL: str | None = None
# The secret links are signed with. Unset: one is made at start-up, and links
# stop working when the service restarts.
LINK_SECRET: str | None = None
# How long a link works; 0 for always.
LINK_TTL_SECONDS: int = 30 * 24 * 3600

111
orchestration/delivery.py Normal file
View File

@@ -0,0 +1,111 @@
"""Keeping what an agent delivered and linking to it (ADR-0004): the store, the
signer and the address a caller reaches them at, together."""
from __future__ import annotations
import logging
import re
from collections.abc import Mapping
from typing import Any
from urllib.parse import quote
from orchestration.links import Signer
from orchestration.store import Store
logger = logging.getLogger(__name__)
LOCAL_TENANT = "local" # what is kept for a caller that names no tenant
UNASSIGNED = "unassigned" # for a request that names no Customer case
_NOT_SLUG = re.compile(r"[^A-Za-z0-9._-]+")
def _slug(value: Any, default: str) -> str:
text = _NOT_SLUG.sub("-", str(value or "")).strip("-.")
return text or default
class Delivery:
def __init__(self, store: Store, signer: Signer, public_url: str) -> None:
self.store = store
self.signer = signer
self.public_url = public_url.rstrip("/")
@staticmethod
def place(fields: Mapping[str, Any]) -> tuple[str, str]:
"""The tenant and Customer case what a request delivers is kept under."""
return (
_slug(fields.get("tenant_id"), LOCAL_TENANT),
_slug(fields.get("customer_case"), UNASSIGNED),
)
def keep(
self,
fields: Mapping[str, Any],
*,
work: str,
execution_id: str,
status: str,
request: str,
files: Mapping[str, str],
guide: str | None,
) -> dict[str, Any] | None:
"""Keep an agent's Execution; None, logged, when it cannot be kept, so a
caller still gets the agent's answer."""
tenant, case = self.place(fields)
try:
return self.store.keep(
tenant=tenant,
case=case,
execution_id=execution_id,
work=work,
status=status,
request=request,
files=files,
guide=guide,
project_id=fields.get("project_id"),
)
except (OSError, ValueError):
logger.exception("Could not keep %s Execution %s", work, execution_id)
return None
# --- addresses -------------------------------------------------------------
@staticmethod
def case_path(tenant: str, case: str) -> str:
return f"/tenants/{tenant}/cases/{case}"
def execution_path(self, tenant: str, case: str, execution_id: str) -> str:
return f"{self.case_path(tenant, case)}/executions/{execution_id}"
def file_path(self, tenant: str, case: str, execution_id: str, name: str) -> str:
return f"{self.execution_path(tenant, case, execution_id)}/files/{quote(name)}"
def latest_path(self, tenant: str, case: str, work: str) -> str:
return f"{self.case_path(tenant, case)}/latest/{quote(work)}"
def link(self, path: str) -> str:
return self.signer.link(self.public_url, path)
def links(self, record: Mapping[str, Any]) -> dict[str, Any]:
"""Signed links to a kept Execution: its record, its guide, each file, the
latest of its work and its Customer case."""
tenant, case = record["tenant"], record["customer_case"]
execution_id = record["execution_id"]
files = {
name: self.link(self.file_path(tenant, case, execution_id, name))
for name in record["files"]
}
return {
"record": self.link(self.execution_path(tenant, case, execution_id)),
"guide": files.get(record.get("guide") or ""),
"files": files,
"latest": self.link(self.latest_path(tenant, case, record["work"])),
"case": self.link(self.case_path(tenant, case)),
}
def shown(self, record: Mapping[str, Any]) -> dict[str, Any]:
"""A record as it is served: what was kept, with links, without internals."""
return {
**{k: v for k, v in record.items() if k != "kept_ns"},
"links": self.links(record),
}

47
orchestration/links.py Normal file
View File

@@ -0,0 +1,47 @@
"""Signed links to what the Orchestration engine keeps (ADR-0004).
A link is a path with `exp` and `sig` in its query: `sig` is an HMAC of the path and
`exp` under the engine's secret. `exp` 0 never expires.
"""
from __future__ import annotations
import hashlib
import hmac
import time
from collections.abc import Callable
from urllib.parse import urlencode
class Signer:
def __init__(
self,
secret: str,
ttl_seconds: int,
now: Callable[[], float] = time.time,
) -> None:
self._secret = secret.encode()
self._ttl = ttl_seconds
self._now = now
def _signature(self, path: str, exp: int) -> str:
return hmac.new(
self._secret, f"{path}\n{exp}".encode(), hashlib.sha256
).hexdigest()
def sign(self, path: str) -> dict[str, str]:
"""The query that makes `path` readable."""
exp = int(self._now()) + self._ttl if self._ttl > 0 else 0
return {"exp": str(exp), "sig": self._signature(path, exp)}
def verify(self, path: str, exp: str | None = None, sig: str | None = None) -> bool:
if not exp or not sig or not exp.isdigit():
return False
expires = int(exp)
if expires and expires < self._now():
return False
return hmac.compare_digest(sig, self._signature(path, expires))
def link(self, base_url: str, path: str) -> str:
"""`path` under `base_url`, signed."""
return f"{base_url.rstrip('/')}{path}?{urlencode(self.sign(path))}"

View File

@@ -3,6 +3,7 @@
from __future__ import annotations
import logging
import secrets
from collections.abc import AsyncIterator, Sequence
from contextlib import asynccontextmanager
@@ -24,7 +25,13 @@ from orchestration.agents import Agent, load_routes
from orchestration.agents import Sequence as AgentSequence
from orchestration.card import A2A_PATH, build_card, card_document
from orchestration.config import OrchestrationSettings
from orchestration.delivery import Delivery
from orchestration.links import Signer
from orchestration.relay import AgentClients, RelayExecutor
from orchestration.serving import kept_routes
from orchestration.store import Store
logger = logging.getLogger(__name__)
LEGACY_CARD_PATH = "/.well-known/agent.json"
@@ -36,11 +43,16 @@ def create_app(
httpx_client: httpx.AsyncClient | None = None,
clients: AgentClients | None = None,
sequences: Sequence[AgentSequence] = (),
store: Store | None = None,
signer: Signer | None = None,
public_url: str | None = None,
) -> Starlette:
"""The server. `httpx_client` is how it reaches the agents; one is made
(and closed) here when none is given. `clients` replaces how agents are
called altogether, for tests."""
called altogether, for tests. `store`, `signer` and `public_url` replace what
the settings make of them."""
settings = settings or OrchestrationSettings()
delivery = _delivery(settings, store, signer, public_url)
own_client = httpx_client is None
client = httpx_client or httpx.AsyncClient(
timeout=httpx.Timeout(None, connect=settings.CONNECT_TIMEOUT_SECONDS)
@@ -53,6 +65,7 @@ def create_app(
clients or AgentClients(client),
settings.HEARTBEAT_SECONDS,
sequences,
delivery,
),
task_store=InMemoryTaskStore(),
agent_card=card,
@@ -93,12 +106,42 @@ def create_app(
Route("/health", health, methods=["GET"]),
Route(AGENT_CARD_WELL_KNOWN_PATH, agent_card, methods=["GET"]),
Route(LEGACY_CARD_PATH, agent_card, methods=["GET"]),
*(kept_routes(delivery) if delivery else []),
*jsonrpc,
],
lifespan=lifespan,
)
def _delivery(
settings: OrchestrationSettings,
store: Store | None,
signer: Signer | None,
public_url: str | None,
) -> Delivery | None:
"""Where what agents deliver is kept, and how it is linked to; None when
nothing is to be kept."""
if store is None and settings.STORE_DIR:
store = Store(settings.STORE_DIR)
if store is None:
return None
if signer is None:
secret = settings.LINK_SECRET
if not secret:
logger.warning(
"LINK_SECRET is not set: links made now stop working on a restart"
)
secret = secrets.token_hex(32)
signer = Signer(secret, settings.LINK_TTL_SECONDS)
base = (
public_url
or settings.PUBLIC_URL
or settings.A2A_PUBLIC_URL
or f"http://{settings.A2A_HOST}:{settings.A2A_PORT}"
)
return Delivery(store, signer, base)
@click.command()
@click.option("--host", default=None, help="Host to bind (default A2A_HOST)")
@click.option("--port", default=None, type=int, help="Port (default A2A_PORT)")

View File

@@ -44,6 +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.routing import WORK, route
logger = logging.getLogger(__name__)
@@ -64,6 +65,14 @@ _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"
# 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"
# The one artifact a caller given links receives.
DOCUMENT = "result.json"
# What a KAB workflow step sends with every call, by which it is known: it takes
# one output per step, so it is given links unless it asks otherwise.
_PLATFORM_STEP = ("workflow_id", "step_number")
@dataclass
@@ -71,9 +80,13 @@ class Plan:
"""What one of our tasks runs: the agents, in turn, and the caller's fields."""
steps: tuple[Agent, ...]
fields: dict[str, Any] # the request's metadata, but for `work`
fields: dict[str, Any] # the request's metadata, but for `work` and `deliver`
text: str # the request, as the agents are sent it
other_parts: list[Part] = field(default_factory=list) # data and files sent with it
work: str = "" # the work the caller named, an agent's or a sequence's
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)
def metadata(self, step: int, sources: Mapping[str, str] | None) -> dict[str, Any]:
"""The metadata for the agent at `step`."""
@@ -87,6 +100,8 @@ class Plan:
sent.update(own)
if sources is not None:
sent["sources"] = dict(sources)
if isinstance(sent.get("sources"), Mapping):
sent["sources"] = _as_files(sent["sources"])
return sent
@@ -130,7 +145,9 @@ class RelayExecutor(AgentExecutor):
clients: AgentClients,
heartbeat_seconds: float,
sequences: Sequence[AgentSequence] = (),
delivery: Delivery | None = None,
) -> None:
self._delivery = delivery
self._agents = list(agents)
self._sequences = list(sequences)
self._clients = clients
@@ -174,7 +191,7 @@ class RelayExecutor(AgentExecutor):
)
)
return
fields = {key: value for key, value in options.items() if key != WORK}
fields = {k: v for k, v in options.items() if k not in (WORK, DELIVER)}
texts = [part.text for part in parts if part.HasField("text")]
# A KAB step sends its fields as metadata and, as the text, all of them
# again as one JSON document. The agents are sent the request as their text.
@@ -183,6 +200,8 @@ class RelayExecutor(AgentExecutor):
fields=fields,
text=str(fields["request"]) if fields.get("request") else "\n".join(texts),
other_parts=[part for part in parts if not part.HasField("text")],
work=str(options.get(WORK) or decision.agents[0].work),
deliver=_deliver(options),
)
await self._run(task_id, plan, 0, self._opening(plan, 0, None), updater)
@@ -257,6 +276,8 @@ class RelayExecutor(AgentExecutor):
heartbeat = asyncio.create_task(self._beat(agent, updater))
relayed: set[str] = set() # artifact ids already passed on
files: dict[str, str] = {} # what it delivered, by name, for the next agent
guide: list[str] = [] # the file it sent as its document, if any
relay_files = plan.deliver == FILES
try:
client = await self._clients.for_agent(agent)
async for event in client.send_message(SendMessageRequest(message=message)):
@@ -264,11 +285,19 @@ class RelayExecutor(AgentExecutor):
if event.HasField("task"):
self._hold(task_id, plan, step, event.task)
for artifact in event.task.artifacts:
await self._artifact(agent, artifact, relayed, files, updater)
await self._artifact(
agent, artifact, relayed, files, guide, updater, relay_files
)
status = event.task.status
elif event.HasField("artifact_update"):
await self._artifact(
agent, event.artifact_update.artifact, relayed, files, updater
agent,
event.artifact_update.artifact,
relayed,
files,
guide,
updater,
relay_files,
)
elif event.HasField("status_update"):
status = event.status_update.status
@@ -282,11 +311,11 @@ class RelayExecutor(AgentExecutor):
return
if status is not None and status.state in _ENDS_A_TURN:
heartbeat.cancel()
waiting = self._waiting.get(task_id)
following = step + 1 < len(plan.steps)
if status.state == TaskState.TASK_STATE_COMPLETED and following:
await self._next(
task_id, plan, step, status, files, waiting, updater
if status.state == TaskState.TASK_STATE_COMPLETED:
waiting = self._waiting.get(task_id)
self._kept(plan, agent, waiting, status, files, guide)
await self._completed(
task_id, plan, step, status, files, updater
)
else:
await self._end_turn(task_id, status, updater)
@@ -325,50 +354,85 @@ class RelayExecutor(AgentExecutor):
finally:
heartbeat.cancel()
async def _next(
def _kept(
self,
plan: Plan,
agent: Agent,
waiting: Waiting | None,
status: TaskStatus,
files: Mapping[str, str],
guide: list[str],
) -> None:
"""Keep what the agent delivered and note it in the plan's results."""
execution_id = waiting.task_id if waiting else ""
ended = _ended(status)
result: dict[str, Any] = {"execution_id": execution_id, "status": ended}
record = (
self._delivery.keep(
plan.fields,
work=agent.work,
execution_id=execution_id or str(uuid.uuid4()),
status=ended,
request=plan.text,
files=files,
guide=guide[0] if guide else None,
)
if self._delivery is not None
else None
)
if record is not None and self._delivery is not None:
result.update(self._delivery.links(record))
else:
result["files"] = {name: None for name in sorted(files)}
plan.results[agent.work] = result
async def _completed(
self,
task_id: str,
plan: Plan,
step: int,
status: TaskStatus,
files: dict[str, str],
waiting: Waiting | None,
updater: TaskUpdater,
) -> None:
"""The agent at `step` completed: start the next with its files, unless it
ended with warnings, when nothing after it starts."""
ended with warnings, when nothing after it starts; or, after the last,
finish."""
agent = plan.steps[step]
parts = list(status.message.parts) if status.HasField("message") else []
said = next(
(data for data in get_data_parts(parts) if isinstance(data, Mapping)), {}
)
if said.get("status", "completed") != "completed":
self._waiting.pop(task_id, None)
ended = _ended(status)
if step + 1 == len(plan.steps):
await self._finish(task_id, plan, parts, ended, updater)
return
if ended != "completed":
not_started = [later.work for later in plan.steps[step + 1 :]]
await updater.complete(
updater.new_agent_message(
[
new_text_part(
f"The {agent.work} agent ended with warnings, so "
f"{', '.join(not_started)} was not started. Settle them "
"and ask again."
),
new_data_part(
{
"status": "stopped",
"after": agent.work,
"not_started": not_started,
}
),
*parts, # what the agent itself said
]
)
await self._finish(
task_id,
plan,
[
new_text_part(
f"The {agent.work} agent ended with warnings, so "
f"{', '.join(not_started)} was not started. Settle them "
"and ask again."
),
new_data_part(
{
"status": "stopped",
"after": agent.work,
"not_started": not_started,
}
),
*parts, # what the agent itself said
],
"stopped",
updater,
not_started,
)
return
record = {
"status": "completed",
"customer_case": plan.fields.get("customer_case"),
"execution_id": waiting.task_id if waiting else None,
"execution_id": plan.results[agent.work]["execution_id"] or None,
"workflow_request": plan.text,
"work": agent.work,
}
@@ -392,6 +456,48 @@ class RelayExecutor(AgentExecutor):
task_id, plan, step + 1, self._opening(plan, step + 1, sources), updater
)
async def _finish(
self,
task_id: str,
plan: Plan,
parts: list[Part],
ended: str,
updater: TaskUpdater,
not_started: list[str] | None = None,
) -> None:
"""Complete our task; a caller given links has one document of them."""
self._waiting.pop(task_id, None)
if plan.deliver == LINKS:
document = self._document(plan, ended, not_started)
await updater.add_artifact(
[new_text_part(json.dumps(document), media_type="application/json")],
name=DOCUMENT,
)
await updater.complete(updater.new_agent_message(parts))
def _document(
self, plan: Plan, ended: str, not_started: list[str] | None
) -> dict[str, Any]:
"""One document of links to what each agent delivered; at its top level,
the last agent's."""
last = list(plan.results.values())[-1] if plan.results else {}
_, case = Delivery.place(plan.fields)
document: dict[str, Any] = {
"status": ended,
"work": plan.work,
"customer_case": case,
"execution_id": last.get("execution_id"),
"record": last.get("record"),
"guide": last.get("guide"),
"files": last.get("files", {}),
"latest": last.get("latest"),
"case": last.get("case"),
"results": plan.results,
}
if not_started:
document["not_started"] = not_started
return document
def _hold(self, task_id: str, plan: Plan, step: int, task: Task) -> None:
if task.id:
self._waiting[task_id] = Waiting(plan, step, task.id, task.context_id)
@@ -422,7 +528,9 @@ class RelayExecutor(AgentExecutor):
artifact: Artifact,
relayed: set[str],
files: dict[str, str],
guide: list[str],
updater: TaskUpdater,
relay: bool,
) -> None:
if artifact.artifact_id in relayed:
return
@@ -430,6 +538,13 @@ class RelayExecutor(AgentExecutor):
text = "".join(part.text for part in artifact.parts if part.HasField("text"))
if artifact.name and text:
files[artifact.name] = text
# The Execution engine sends its final Deliverable as markdown.
if not guide and any(
part.media_type == "text/markdown" for part in artifact.parts
):
guide.append(artifact.name)
if not relay:
return
metadata = (
json_format.MessageToDict(artifact.metadata)
if artifact.HasField("metadata")
@@ -453,6 +568,34 @@ class RelayExecutor(AgentExecutor):
)
def _deliver(options: Mapping[str, Any]) -> str:
"""How the caller is given what agents delivered: as it asked, or links for a
KAB workflow step, which takes one output per step, and files for anyone else."""
asked = options.get(DELIVER)
if asked in (LINKS, FILES):
return str(asked)
return LINKS if all(key in options for key in _PLATFORM_STEP) else FILES
def _ended(status: TaskStatus) -> str:
"""How an agent says its work ended, from its last word: `completed` unless it
says otherwise, such as `completed_with_warnings`."""
parts = list(status.message.parts) if status.HasField("message") else []
said = next(
(data for data in get_data_parts(parts) if isinstance(data, Mapping)), {}
)
return str(said.get("status", "completed"))
def _as_files(sources: Mapping[str, Any]) -> dict[str, str]:
"""Sources as files by name: what was parsed on the way, as a platform step
does with an earlier step's output, is sent on as JSON text."""
return {
str(name): content if isinstance(content, str) else json.dumps(content)
for name, content in sources.items()
}
def _options(context: RequestContext) -> dict[str, Any]:
"""The request's metadata: the message's, then the request's own."""
options: dict[str, Any] = {}

76
orchestration/serving.py Normal file
View File

@@ -0,0 +1,76 @@
"""Serving what was kept at signed links (ADR-0004). Every route checks the link's
signature against the path it was made for; anything else is refused."""
from __future__ import annotations
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette.routing import Route
from orchestration.delivery import Delivery
def kept_routes(delivery: Delivery) -> list[Route]:
store = delivery.store
def signed(request: Request) -> bool:
# The path as the link carried it: a name is signed as it was quoted.
raw = request.scope.get("raw_path")
path = raw.decode("latin-1") if isinstance(raw, bytes) else request.url.path
return delivery.signer.verify(
path, request.query_params.get("exp"), request.query_params.get("sig")
)
def refused() -> JSONResponse:
return JSONResponse({"error": "This link is not valid."}, status_code=403)
def missing() -> JSONResponse:
return JSONResponse({"error": "Nothing is kept here."}, status_code=404)
async def case(request: Request) -> Response:
if not signed(request):
return refused()
tenant, name = request.path_params["tenant"], request.path_params["case"]
return JSONResponse(
{
"tenant": tenant,
"customer_case": name,
"executions": [delivery.shown(r) for r in store.case(tenant, name)],
}
)
async def execution(request: Request) -> Response:
if not signed(request):
return refused()
p = request.path_params
record = store.record(p["tenant"], p["case"], p["execution_id"])
return JSONResponse(delivery.shown(record)) if record else missing()
async def file(request: Request) -> Response:
if not signed(request):
return refused()
p = request.path_params
found = store.file(p["tenant"], p["case"], p["execution_id"], p["name"])
if found is None:
return missing()
content, media_type = found
return Response(content, media_type=media_type)
async def latest(request: Request) -> Response:
if not signed(request):
return refused()
p = request.path_params
record = store.latest(p["tenant"], p["case"], p["work"])
return JSONResponse(delivery.shown(record)) if record else missing()
case_path = "/tenants/{tenant}/cases/{case}"
return [
Route(case_path, case, methods=["GET"]),
Route(f"{case_path}/executions/{{execution_id}}", execution, methods=["GET"]),
Route(
f"{case_path}/executions/{{execution_id}}/files/{{name:path}}",
file,
methods=["GET"],
),
Route(f"{case_path}/latest/{{work}}", latest, methods=["GET"]),
]

157
orchestration/store.py Normal file
View File

@@ -0,0 +1,157 @@
"""What agents delivered, kept by tenant, Customer case and Execution (ADR-0004).
<root>/tenants/<tenant>/cases/<case>/executions/<id>/record.json
/files/<name as delivered>
An Execution is written once. Its files are what the agent delivered, by the names
it gave them; its record says what it was.
"""
from __future__ import annotations
import datetime as dt
import json
import re
import time
from collections.abc import Mapping
from pathlib import Path, PurePosixPath
from typing import Any
_SLUG = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*$")
_MEDIA_TYPES = {
".md": "text/markdown; charset=utf-8",
".json": "application/json",
".yaml": "application/yaml",
".yml": "application/yaml",
".html": "text/html; charset=utf-8",
".csv": "text/csv; charset=utf-8",
}
_TEXT = "text/plain; charset=utf-8"
def slug(value: str, what: str) -> str:
"""`value`, when it can name a folder: letters, digits, dot, dash, underscore."""
if not _SLUG.match(value or "") or value in {".", ".."}:
raise ValueError(f"{what} {value!r} must be letters, digits, '.', '-' or '_'")
return value
def file_name(name: str) -> PurePosixPath:
"""A delivered file's name as a path inside its Execution, or ValueError."""
path = PurePosixPath(name)
if (
not name
or "\\" in name
or path.is_absolute()
or any(part in {"", ".", ".."} for part in name.split("/"))
):
raise ValueError(f"{name!r} is not a file name inside an Execution")
return path
def media_type(name: str) -> str:
return _MEDIA_TYPES.get(PurePosixPath(name).suffix.lower(), _TEXT)
class Store:
def __init__(self, root: str | Path) -> None:
self.root = Path(root)
def _case(self, tenant: str, case: str) -> Path:
return (
self.root
/ "tenants"
/ slug(tenant, "Tenant")
/ "cases"
/ slug(case, "Case")
)
def _execution(self, tenant: str, case: str, execution_id: str) -> Path:
return self._case(tenant, case) / "executions" / slug(execution_id, "Execution")
def keep(
self,
*,
tenant: str,
case: str,
execution_id: str,
work: str,
status: str,
request: str,
files: Mapping[str, str],
guide: str | None = None,
project_id: str | None = None,
) -> dict[str, Any]:
"""Keep one Execution's files and record it. Written once: an Execution
already kept is refused."""
folder = self._execution(tenant, case, execution_id)
paths = {name: file_name(name) for name in files}
if folder.exists():
raise ValueError(f"Execution {execution_id} is already kept")
for name, path in paths.items():
target = folder / "files" / path
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text(files[name], encoding="utf-8")
record = {
"tenant": tenant,
"customer_case": case,
"execution_id": execution_id,
"work": work,
"status": status,
"request": request,
"project_id": project_id,
"guide": guide if guide in files else None,
"files": sorted(files),
"kept_at": dt.datetime.now(dt.UTC).isoformat(),
"kept_ns": time.time_ns(),
}
folder.mkdir(parents=True, exist_ok=True)
(folder / "record.json").write_text(json.dumps(record, indent=2), "utf-8")
return record
def record(
self, tenant: str, case: str, execution_id: str
) -> dict[str, Any] | None:
try:
path = self._execution(tenant, case, execution_id) / "record.json"
except ValueError:
return None
if not path.is_file():
return None
record: dict[str, Any] = json.loads(path.read_text("utf-8"))
return record
def file(
self, tenant: str, case: str, execution_id: str, name: str
) -> tuple[bytes, str] | None:
"""A kept file's content and media type, or None."""
try:
folder = self._execution(tenant, case, execution_id) / "files"
target = folder / file_name(name)
except ValueError:
return None
if not target.is_file() or folder.resolve() not in target.resolve().parents:
return None
return target.read_bytes(), media_type(name)
def case(self, tenant: str, case: str) -> list[dict[str, Any]]:
"""The Customer case's Executions, newest first."""
try:
folder = self._case(tenant, case) / "executions"
except ValueError:
return []
records = [
json.loads(path.read_text("utf-8")) for path in folder.glob("*/record.json")
]
return sorted(records, key=lambda r: r.get("kept_ns", 0), reverse=True)
def latest(self, tenant: str, case: str, work: str) -> dict[str, Any] | None:
"""The newest Execution of `work` in the Customer case that completed."""
return next(
(
record
for record in self.case(tenant, case)
if record["work"] == work and record["status"] == "completed"
),
None,
)

View File

@@ -1,162 +0,0 @@
"""Delivering one JSON document to a caller that takes one output per step, as a KAB
workflow step does, instead of each agent's files one by one."""
from __future__ import annotations
import json
from typing import Any
import httpx
from orchestration.main import create_app
from tests.orchestration.test_sequence import (
ARCHITECTURE,
BOTH,
OPERATIONS,
SETTINGS,
ScriptedAgents,
call,
finished,
struct,
)
# What a KAB workflow step sends with every call.
PLATFORM = {"workflow_id": "wf-1", "step_number": 1, "task_id": "t-1"}
GRAPH = '{"elements": []}'
def document(task: dict) -> dict[str, Any]:
(artifact,) = task["artifacts"]
assert artifact["name"] == "result.json"
return json.loads("".join(part["text"] for part in artifact["parts"]))
async def test_a_platform_step_is_given_one_document_of_the_agents_files():
agents = ScriptedAgents(
{"architecture": [finished("a1", {"graph.json": GRAPH, "guide.md": "# Guide"})]}
)
task, _ = await call(
agents,
[{"text": "A landing zone"}],
metadata={**PLATFORM, "work": "architecture", "customer_case": "acme"},
)
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
result = document(task)
assert result["status"] == "completed"
assert result["work"] == "architecture"
assert result["customer_case"] == "acme"
# Every file as the agent delivered it, ready to be the next step's sources.
assert result["files"]["graph.json"] == GRAPH
assert result["files"]["guide.md"] == "# Guide"
assert json.loads(result["files"]["execution.json"]) == {
"status": "completed",
"customer_case": "acme",
"execution_id": "a1",
"workflow_request": "A landing zone",
"work": "architecture",
}
assert result["results"]["architecture"]["status"] == "completed"
assert result["results"]["architecture"]["execution_id"] == "a1"
assert set(result["results"]["architecture"]["files"]) == {"graph.json", "guide.md"}
async def test_a_caller_that_asks_for_files_gets_them_one_by_one():
agents = ScriptedAgents({"architecture": [finished("a1", {"graph.json": GRAPH})]})
task, _ = await call(
agents,
[{"text": "A landing zone"}],
metadata={**PLATFORM, "work": "architecture", "deliver": "files"},
)
assert [a["name"] for a in task["artifacts"]] == ["graph.json"]
(sent,) = agents.received["architecture"]
assert "deliver" not in struct(sent)
async def test_any_caller_can_ask_for_the_document():
agents = ScriptedAgents({"architecture": [finished("a1", {"graph.json": GRAPH})]})
task, _ = await call(
agents,
[{"text": "A landing zone"}],
metadata={"work": "architecture", "deliver": "document"},
)
assert document(task)["files"]["graph.json"] == GRAPH
async def test_a_sequence_is_one_document_with_each_agents_results():
agents = ScriptedAgents(
{
"architecture": [finished("a1", {"graph.json": GRAPH})],
"operations": [finished("o1", {"model.json": "{}"})],
}
)
task, _ = await call(
agents,
[{"text": "A landing zone"}],
metadata={**PLATFORM, "work": "architecture_and_operations"},
)
result = document(task)
assert result["status"] == "completed"
assert set(result["results"]) == {"architecture", "operations"}
assert set(result["results"]["architecture"]["files"]) == {"graph.json"}
# The top-level files are the last agent's.
assert set(result["files"]) == {"model.json", "execution.json"}
assert json.loads(result["files"]["execution.json"])["work"] == "operations"
async def test_a_sequence_stopped_by_warnings_still_delivers_what_was_done():
agents = ScriptedAgents(
{
"architecture": [
finished("a1", {"graph.json": GRAPH}, status="completed_with_warnings")
],
"operations": [],
}
)
task, _ = await call(
agents,
[{"text": "A landing zone"}],
metadata={**PLATFORM, "work": "architecture_and_operations"},
)
result = document(task)
assert result["status"] == "stopped"
assert result["not_started"] == ["operations"]
assert result["results"]["architecture"]["status"] == "completed_with_warnings"
assert result["files"]["graph.json"] == GRAPH
async def test_sources_given_as_data_reach_the_agent_as_files():
# A platform step maps an earlier step's output into `sources`; whatever was
# parsed on the way is sent on as text, the form an agent reads files in.
agents = ScriptedAgents({"operations": [finished("o1", {"model.json": "{}"})]})
await call(
agents,
[{"text": "Operate it"}],
metadata={
"work": "operations",
"sources": {"graph.json": {"elements": []}, "guide.md": "# Guide"},
},
)
(sent,) = agents.received["operations"]
sources = struct(sent)["sources"]
assert json.loads(sources["graph.json"]) == {"elements": []}
assert sources["guide.md"] == "# Guide"
async def test_the_card_declares_the_document_and_the_choice_of_delivery():
app = create_app([ARCHITECTURE, OPERATIONS], SETTINGS, sequences=[BOTH])
async with httpx.AsyncClient(
transport=httpx.ASGITransport(app=app), base_url="http://orchestration"
) as caller:
skill = (await caller.get("/.well-known/agent-card.json")).json()["skills"][0]
inputs = skill["inputSchema"]["properties"]
assert inputs["deliver"]["enum"] == ["document", "files"]
assert inputs["sources"]["type"] == "object"
outputs = skill["outputSchema"]["properties"]
assert {"status", "work", "customer_case", "files", "results"} <= set(outputs)

View File

@@ -0,0 +1,344 @@
"""Delivered files kept and served at signed links (ADR-0004); a caller that takes
one output per step, as a KAB workflow step does, is given one document of links."""
from __future__ import annotations
import json
import uuid
from typing import Any
from urllib.parse import urlsplit
import httpx
from orchestration.links import Signer
from orchestration.main import create_app
from orchestration.store import Store
from tests.orchestration.test_sequence import (
ARCHITECTURE,
BOTH,
OPERATIONS,
SETTINGS,
ScriptedAgents,
finished,
struct,
)
BASE = "http://orchestration"
# What a KAB workflow step sends with every call.
PLATFORM = {
"workflow_id": "wf-1",
"step_number": 1,
"tenant_id": "t1",
"project_id": "p1",
}
GRAPH = '{"elements": []}'
class Harness:
"""The Orchestration engine in front of scripted agents, with a store and the
caller's client, both in this process."""
def __init__(self, tmp_path: Any, agents: ScriptedAgents) -> None:
self.agents = agents
self.store = Store(tmp_path / "store")
self.app = create_app(
[ARCHITECTURE, OPERATIONS],
SETTINGS,
clients=agents, # type: ignore[arg-type]
sequences=[BOTH],
store=self.store,
signer=Signer("secret", ttl_seconds=3600),
public_url=BASE,
)
def client(self) -> httpx.AsyncClient:
return httpx.AsyncClient(
transport=httpx.ASGITransport(app=self.app), base_url=BASE
)
async def send(self, text: str, metadata: dict[str, Any]) -> dict[str, Any]:
message = {
"messageId": str(uuid.uuid4()),
"role": "ROLE_USER",
"parts": [{"text": text}],
"metadata": metadata,
}
async with self.client() as caller:
response = await caller.post(
"/a2a",
json={
"jsonrpc": "2.0",
"id": 1,
"method": "SendMessage",
"params": {"message": message},
},
headers={"A2A-Version": "1.0"},
)
body = response.json()
assert "error" not in body, body
task: dict[str, Any] = body["result"]["task"]
return task
async def get(self, link: str) -> httpx.Response:
url = urlsplit(link)
async with self.client() as caller:
return await caller.get(
f"{url.path}?{url.query}" if url.query else url.path
)
def document(task: dict[str, Any]) -> dict[str, Any]:
(artifact,) = task["artifacts"]
assert artifact["name"] == "result.json"
loaded: dict[str, Any] = json.loads("".join(p["text"] for p in artifact["parts"]))
return loaded
async def test_a_platform_step_is_given_links_to_what_was_kept(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents(
{
"architecture": [
finished("a1", {"graph.json": GRAPH, "guide.md": "# Guide"})
]
}
),
)
task = await harness.send(
"A landing zone", {**PLATFORM, "work": "architecture", "customer_case": "acme"}
)
assert task["status"]["state"] == "TASK_STATE_COMPLETED"
result = document(task)
assert result["status"] == "completed"
assert result["work"] == "architecture"
assert result["customer_case"] == "acme"
assert result["execution_id"] == "a1"
assert set(result["files"]) == {"graph.json", "guide.md"}
assert result["record"].startswith(f"{BASE}/tenants/t1/cases/acme/executions/a1?")
# Each link serves what the agent delivered, with its media type.
guide = await harness.get(result["files"]["guide.md"])
assert guide.status_code == 200
assert guide.text == "# Guide"
assert guide.headers["content-type"].startswith("text/markdown")
assert (await harness.get(result["files"]["graph.json"])).json() == {"elements": []}
# The record names the platform's project and links to every file.
record = (await harness.get(result["record"])).json()
assert record["project_id"] == "p1"
assert record["request"] == "A landing zone"
assert set(record["links"]["files"]) == {"graph.json", "guide.md"}
async def test_the_guide_is_the_markdown_the_agent_sent_as_its_document(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents(
{
"architecture": [
finished(
"a1",
{"graph.json": GRAPH, "notes.md": "n", "guide.md": "# Guide"},
guide="guide.md",
)
]
}
),
)
result = document(
await harness.send("A landing zone", {**PLATFORM, "work": "architecture"})
)
assert result["guide"] == result["files"]["guide.md"]
async def test_a_link_that_is_not_signed_or_was_changed_is_refused(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents({"architecture": [finished("a1", {"guide.md": "# G"})]}),
)
result = document(
await harness.send(
"A landing zone",
{**PLATFORM, "work": "architecture", "customer_case": "acme"},
)
)
link = result["files"]["guide.md"]
assert (await harness.get(link.split("?")[0])).status_code == 403
assert (await harness.get(link.replace("guide.md", "other.md"))).status_code == 403
assert (await harness.get(link.replace("/t1/", "/t2/"))).status_code == 403
async def test_a_caller_that_asks_for_files_gets_them_and_they_are_kept(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents({"architecture": [finished("a1", {"graph.json": GRAPH})]}),
)
task = await harness.send(
"A landing zone",
{
**PLATFORM,
"work": "architecture",
"customer_case": "acme",
"deliver": "files",
},
)
assert [a["name"] for a in task["artifacts"]] == ["graph.json"]
assert harness.store.record("t1", "acme", "a1")["files"] == ["graph.json"]
(sent,) = harness.agents.received["architecture"]
assert "deliver" not in struct(sent)
async def test_without_a_tenant_or_case_what_is_kept_is_still_found(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents({"architecture": [finished("a1", {"graph.json": GRAPH})]}),
)
result = document(
await harness.send(
"A landing zone", {"work": "architecture", "deliver": "links"}
)
)
assert "/tenants/local/cases/unassigned/executions/a1" in result["record"]
assert (await harness.get(result["files"]["graph.json"])).status_code == 200
async def test_a_sequence_is_one_document_with_each_agents_links(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents(
{
"architecture": [finished("a1", {"graph.json": GRAPH})],
"operations": [finished("o1", {"model.json": "{}"})],
}
),
)
result = document(
await harness.send(
"A landing zone",
{
**PLATFORM,
"work": "architecture_and_operations",
"customer_case": "acme",
},
)
)
assert result["status"] == "completed"
assert result["work"] == "architecture_and_operations"
assert set(result["results"]) == {"architecture", "operations"}
assert set(result["results"]["architecture"]["files"]) == {"graph.json"}
# The top level is the last agent's.
assert result["execution_id"] == "o1"
assert set(result["files"]) == {"model.json"}
assert [r["work"] for r in harness.store.case("t1", "acme")] == [
"operations",
"architecture",
]
async def test_a_sequence_stopped_by_warnings_links_what_was_done(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents(
{
"architecture": [
finished(
"a1", {"graph.json": GRAPH}, status="completed_with_warnings"
)
],
"operations": [],
}
),
)
result = document(
await harness.send(
"A landing zone", {**PLATFORM, "work": "architecture_and_operations"}
)
)
assert result["status"] == "stopped"
assert result["not_started"] == ["operations"]
assert result["results"]["architecture"]["status"] == "completed_with_warnings"
assert set(result["files"]) == {"graph.json"}
async def test_latest_is_the_newest_completed_execution_of_the_work(tmp_path):
harness = Harness(
tmp_path,
ScriptedAgents(
{
"architecture": [
finished("a1", {"graph.json": "1"}),
finished("a2", {"graph.json": "2"}),
]
}
),
)
first = document(
await harness.send(
"v1", {**PLATFORM, "work": "architecture", "customer_case": "acme"}
)
)
await harness.send(
"v2", {**PLATFORM, "work": "architecture", "customer_case": "acme"}
)
latest = (await harness.get(first["latest"])).json()
assert latest["execution_id"] == "a2"
assert (await harness.get(latest["links"]["files"]["graph.json"])).text == "2"
case = (await harness.get(first["case"])).json()
assert [r["execution_id"] for r in case["executions"]] == ["a2", "a1"]
async def test_sources_given_as_data_reach_the_agent_as_files(tmp_path):
# A platform step maps an earlier step's output into `sources`; whatever was
# parsed on the way is sent on as text, the form an agent reads files in.
harness = Harness(
tmp_path, ScriptedAgents({"operations": [finished("o1", {"model.json": "{}"})]})
)
await harness.send(
"Operate it",
{
"work": "operations",
"sources": {"graph.json": {"elements": []}, "g.md": "# G"},
},
)
(sent,) = harness.agents.received["operations"]
sources = struct(sent)["sources"]
assert json.loads(sources["graph.json"]) == {"elements": []}
assert sources["g.md"] == "# G"
async def test_the_card_declares_the_links_document_and_the_choice_of_delivery(
tmp_path,
):
harness = Harness(tmp_path, ScriptedAgents({}))
async with harness.client() as caller:
skill = (await caller.get("/.well-known/agent-card.json")).json()["skills"][0]
inputs = skill["inputSchema"]["properties"]
assert inputs["deliver"]["enum"] == ["links", "files"]
assert inputs["sources"]["type"] == "object"
outputs = set(skill["outputSchema"]["properties"])
assert {
"status",
"work",
"customer_case",
"record",
"guide",
"files",
"results",
} <= outputs

View File

@@ -38,8 +38,13 @@ SETTINGS = OrchestrationSettings(_env_file=None, HEARTBEAT_SECONDS=3600) # type
def finished(
task_id: str, files: dict[str, str], status: str = "completed"
task_id: str,
files: dict[str, str],
status: str = "completed",
guide: str | None = None,
) -> StreamResponse:
"""A completed task with `files`; `guide` is sent as markdown, as the Execution
engine sends its final Deliverable."""
message = Message(
message_id=str(uuid.uuid4()),
role=Role.ROLE_AGENT,
@@ -57,7 +62,11 @@ def finished(
Artifact(
artifact_id=f"{task_id}-{name}",
name=name,
parts=[new_text_part(text)],
parts=[
new_text_part(text, media_type="text/markdown")
if name == guide
else new_text_part(text)
],
)
for name, text in files.items()
],

View File

@@ -0,0 +1,126 @@
"""The store of what agents delivered, and the signed links to it (ADR-0004)."""
from __future__ import annotations
import pytest
from orchestration.links import Signer
from orchestration.store import Store
FILES = {
"guide.md": "# Guide",
"graph.json": '{"elements": []}',
"terraform/main.tf": 'resource "x" "y" {}',
}
def kept(store: Store, execution_id: str = "a1", **overrides: object) -> dict:
values: dict = {
"tenant": "t1",
"case": "acme",
"execution_id": execution_id,
"work": "architecture",
"status": "completed",
"request": "A landing zone",
"files": FILES,
"guide": "guide.md",
"project_id": "p1",
}
values.update(overrides)
return store.keep(**values)
def test_an_execution_is_kept_with_its_record(tmp_path):
store = Store(tmp_path)
record = kept(store)
assert record["work"] == "architecture"
assert record["status"] == "completed"
assert record["customer_case"] == "acme"
assert record["project_id"] == "p1"
assert record["files"] == sorted(FILES)
assert store.record("t1", "acme", "a1") == record
def test_a_file_comes_back_with_its_media_type(tmp_path):
store = Store(tmp_path)
kept(store)
assert store.file("t1", "acme", "a1", "guide.md") == (
b"# Guide",
"text/markdown; charset=utf-8",
)
assert store.file("t1", "acme", "a1", "graph.json")[1] == "application/json"
assert (
store.file("t1", "acme", "a1", "terraform/main.tf")[0]
== FILES["terraform/main.tf"].encode()
)
assert store.file("t1", "acme", "a1", "missing.md") is None
def test_what_is_kept_is_never_changed(tmp_path):
store = Store(tmp_path)
kept(store)
with pytest.raises(ValueError, match="a1"):
kept(store, files={"guide.md": "# Another"})
@pytest.mark.parametrize(
"name", ["../escape.md", "/etc/passwd", "a/../../b", "a\\b", ""]
)
def test_a_file_name_outside_the_execution_is_refused(tmp_path, name):
store = Store(tmp_path)
with pytest.raises(ValueError):
kept(store, files={name: "x"})
assert store.file("t1", "acme", "a1", name) is None
@pytest.mark.parametrize("slug", ["..", "a/b", "", "a b"])
def test_a_tenant_or_case_that_is_not_a_slug_is_refused(tmp_path, slug):
with pytest.raises(ValueError):
kept(Store(tmp_path), case=slug)
def test_latest_is_the_newest_completed_execution_of_the_work(tmp_path):
store = Store(tmp_path)
kept(store, "a1")
kept(store, "a2")
kept(store, "a3", status="completed_with_warnings")
kept(store, "o1", work="operations")
assert store.latest("t1", "acme", "architecture")["execution_id"] == "a2"
assert store.latest("t1", "acme", "nothing") is None
assert [r["execution_id"] for r in store.case("t1", "acme")] == [
"o1",
"a3",
"a2",
"a1",
]
def test_a_signed_path_is_accepted_and_a_changed_one_is_not():
signer = Signer("secret", ttl_seconds=60, now=lambda: 1000)
query = signer.sign("/tenants/t1/cases/acme")
assert signer.verify("/tenants/t1/cases/acme", **query)
assert not signer.verify("/tenants/t1/cases/other", **query)
assert not signer.verify("/tenants/t1/cases/acme", exp=query["exp"], sig="0" * 64)
assert not Signer("other", ttl_seconds=60, now=lambda: 1000).verify(
"/tenants/t1/cases/acme", **query
)
def test_a_link_expires_unless_its_lifetime_is_unlimited():
clock = {"now": 1000}
expiring = Signer("secret", ttl_seconds=60, now=lambda: clock["now"])
forever = Signer("secret", ttl_seconds=0, now=lambda: clock["now"])
short, lasting = expiring.sign("/x"), forever.sign("/x")
clock["now"] = 1061
assert not expiring.verify("/x", **short)
assert forever.verify("/x", **lasting)