From b341012757859f236df740113f6e93920f1c5336 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 10 Nov 2026 17:49:54 +0800 Subject: [PATCH 01/10] refactor(bub-ag-ui): scope adapter to protocol runs and continuation --- packages/bub-ag-ui/README.md | 218 +++--- .../bub-ag-ui/src/bub_ag_ui/continuation.py | 130 ++++ packages/bub-ag-ui/src/bub_ag_ui/events.py | 4 +- packages/bub-ag-ui/src/bub_ag_ui/execution.py | 17 +- packages/bub-ag-ui/src/bub_ag_ui/history.py | 108 --- packages/bub-ag-ui/src/bub_ag_ui/http.py | 116 +-- packages/bub-ag-ui/src/bub_ag_ui/runner.py | 5 +- packages/bub-ag-ui/src/bub_ag_ui/runtime.py | 614 ++++----------- .../bub-ag-ui/tests/client/protocol.test.mjs | 78 +- packages/bub-ag-ui/tests/client_server.py | 9 + packages/bub-ag-ui/tests/conftest.py | 39 +- packages/bub-ag-ui/tests/test_events.py | 14 +- packages/bub-ag-ui/tests/test_execution.py | 81 +- .../tests/test_follow_performance.py | 74 -- packages/bub-ag-ui/tests/test_http.py | 69 +- packages/bub-ag-ui/tests/test_runtime.py | 734 +++++------------- .../bub-ag-ui/tests/test_stream_delivery.py | 87 +++ packages/bub-ag-ui/tests/test_tools.py | 20 +- 18 files changed, 843 insertions(+), 1574 deletions(-) create mode 100644 packages/bub-ag-ui/src/bub_ag_ui/continuation.py delete mode 100644 packages/bub-ag-ui/src/bub_ag_ui/history.py delete mode 100644 packages/bub-ag-ui/tests/test_follow_performance.py create mode 100644 packages/bub-ag-ui/tests/test_stream_delivery.py diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index bd595a3..dc9c846 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -1,8 +1,9 @@ # bub-ag-ui -Connect a web frontend to Bub through AG-UI HTTP/SSE. Stream text, reasoning, -tool calls, and shared state, with support for browser-side tools, human input, -and conversation history. +Connect a frontend to Bub through AG-UI. The adapter accepts `RunAgentInput` at +`POST /agent` and streams AG-UI events for text, reasoning, tools, shared state, +and human-input interrupts. It is a protocol adapter, not a conversation-storage +or run-management API. ## Start the server @@ -14,13 +15,19 @@ uv sync uv run bub ag-ui --host 127.0.0.1 --port 28201 ``` -The server uses your normal Bub model/provider configuration and tape store. -History survives a server restart only when that store is persistent. +Use your normal Bub model/provider configuration. For browser access, set +`BUB_AG_UI_ORIGINS` to a JSON array of allowed origins; the default is +`["http://localhost:3000"]`. + +Loopback access works without a token by default. For non-loopback listening, +set `BUB_AG_UI_TOKEN` and send `Authorization: Bearer `. For multi-user +hosting, supply a trusted `authenticate` callback to `create_app` that returns a +stable user identity. A shared token represents one identity; a client-chosen +thread ID is not authentication. ## Connect a frontend -Use an AG-UI client or a frontend integration that accepts an AG-UI endpoint. -For a minimal browser client, install `@ag-ui/client` and send a message: +Install `@ag-ui/client` in your frontend project: ```ts import { HttpAgent } from "@ag-ui/client"; @@ -35,118 +42,92 @@ const agent = new HttpAgent({ await agent.runAgent({ runId: crypto.randomUUID() }); console.log(agent.messages); + +agent.addMessage({ + id: crypto.randomUUID(), role: "user", content: "Tell me more.", +}); +await agent.runAgent({ runId: crypto.randomUUID() }); ``` -Keep the `threadId` to continue or reload the same conversation. Use a new `runId` -for each new user turn or tool continuation; reuse a run ID only when retrying -that exact request. Your frontend is responsible for rendering streamed updates -and handling any browser-side tools. - -### Authentication and browser origins - -- Loopback access works without a token by default. -- For non-loopback listening, set `BUB_AG_UI_TOKEN` and send - `Authorization: Bearer ` on requests. `HttpAgent` accepts a `headers` - option for this. -- Set `BUB_AG_UI_ORIGINS` to a JSON array of allowed browser origins. The default - is `["http://localhost:3000"]`. -- A shared token represents one identity, not separate users. For multi-user - hosting, supply a trusted `authenticate` callback to `create_app` that returns - a stable user identity. A client-chosen thread ID is not authentication. - -## Tools and human input - -| Tool type | What the frontend should expect | -| --- | --- | -| Server tool | Bub executes it and continues the response without client action. | -| Frontend tool | The run ends with a pending call. Execute it in the browser, then submit its result in a new run. | -| Human-input interrupt | The run ends with an interrupt outcome. Show the decision UI, then submit a `resume` entry in a new run. | - -**`RUN_FINISHED` means that run has ended, not necessarily that the agent has -finished answering.** Check for pending tool calls or an interrupt outcome. - -Declare frontend tools in the request's `tools` list. Return each result as a -message with `role: "tool"`, a new message `id`, the matching `toolCallId`, and -its `content`. Send the tool definitions again on the continuation. You do not -need to add a synthetic user message to make the agent continue. - -Human-input tools are configured on the server with `InterruptTool`. Respond -using `resume`, with the matching `interruptId`, a status, and the response -payload. Resolved payloads must satisfy the interrupt's response schema, if any. - -Results can arrive in separate requests, but the agent waits until all pending -calls are resolved before continuing. Resolve them before starting another user -turn. Unknown or duplicate results are rejected. Tool names must not collide -with server tools or each other; `web.search` and `web_search` count as the same -model-facing name. - -Browser tools and interrupts belong to the parent AG-UI turn. Child agents cannot -directly use them; they can return a request to the parent agent to act on. - -## History, retries, and cancellation - -- **Reload a conversation:** fetch its history and restore your client's messages - and state. The response also includes pending calls, run status, and a replay - cursor. History restoration is a Bub extension; connecting a standard - `HttpAgent` alone does not automatically fetch it. -- **Retry a request:** the same run ID and identical request join the existing run - or replay its saved events, without executing the model or tools again. Reusing - that ID for a different request returns HTTP `409`. -- **Replay events:** use the events endpoint with `after=` or a - `Last-Event-ID` header. Replay returns saved output and follows new output if - the run is still active; it does not restart execution. Treat SSE IDs as cursors, - not consecutive event counts. -- **Disconnect:** closing the response that started a run cancels that run. - Closing a separate replay or duplicate-request connection does not cancel it. - Reconnecting can recover saved output, but does not resume cancelled work. -- **Cancel explicitly:** use the cancel endpoint. Cancellation does not undo - side effects of tools that have already executed. -- **Restart after an interrupted run:** the unfinished run is marked failed, - rather than automatically executed again. Partial output remains available, - but it does not imply a completed model exchange or successful tool result. - -Stored history is authoritative: echoing modified assistant messages does not -rewrite it. To edit or regenerate, send a new run whose message list ends at the -existing user message being edited or retried. Bub uses that point as the start -of a new context branch. Resolve pending tools before editing. - -Reset history only while the thread is idle. After a reset, reload history and -discard old replay cursors. Clearing display history alone does not clear the -model's conversation context; use a new thread for a separate conversation. - -## HTTP endpoints and limits - -All routes use the same authentication. - -| Method | Path | Purpose | -| --- | --- | --- | -| `POST` | `/agent` | Submit an AG-UI `RunAgentInput`; receive SSE events. | -| `GET` | `/threads/{thread_id}/history` | Restore messages, state, pending calls, run status, and cursor. | -| `GET` | `/threads/{thread_id}/runs/{run_id}/events` | Replay or follow a run's events. | -| `POST` | `/threads/{thread_id}/runs/{run_id}/cancel` | Cancel an active run; returns `{"cancelled": true}` if cancelled. | - -The `/threads/...` routes are Bub extensions, not standard AG-UI endpoints. -An unknown run returns `404` on the replay and cancel routes. - -Only one run may execute on a thread at a time; another run receives `409` while -that thread is busy. Run one adapter/controller per store/workspace; multiple -server processes sharing it are not supported. Long histories and additional -subscribers can increase event-reading overhead. - -### Embedding and extensions +`HttpAgent` maintains messages and state and sends them on subsequent runs. Keep +one stable `threadId` for the conversation and use a new `runId` for each run. +The frontend renders the streamed updates. `HttpAgent` also accepts a `headers` +option for authentication. + +Each request supplies its conversation context through `messages` and `state`. +The adapter does not substitute a separate server-owned display history. If your +application needs reload persistence, save the client's messages, state, and +thread ID in application storage, then use `initialMessages` and `initialState` +when recreating the client. Keep any interrupt outcome needed by your decision +UI as well. There are no `/threads/...` history, replay, or cancel endpoints. + +## Frontend tools + +Server tools execute in Bub. Frontend tools are declared in the request's `tools` +list and execute in your application, either through your UI framework's tool +handler or your own handler. + +When a run requests frontend tools: + +1. Wait for that run to finish and execute the requested tools. +2. Add a message for each result, with a new `id`, `role: "tool"`, the matching + `toolCallId`, and `content`. +3. Start a new run on the same thread, sending the updated messages, state, and + tool definitions. Include a result for every pending frontend call. + +No synthetic user message is needed. **`RUN_FINISHED` ends one run; it may mean +that the agent is waiting for your frontend, not that it has finished answering.** + +Tool names must not collide with server tools or each other; `web.search` and +`web_search` count as the same model-facing name. Frontend tools and interrupts +belong to the parent AG-UI turn. Child agents can ask the parent to use them, but +cannot directly suspend the parent's frontend interaction. + +## Human input and resume + +Configure human-input tools on the server with `InterruptTool`. A pause ends with +`RUN_FINISHED` carrying `outcome.type: "interrupt"` and the pending interrupts. +The adapter sends message and state snapshots before this event so the client +has the context needed to continue. + +Start a new run on the same thread with the current messages and state, plus a +`resume` entry for **every** open interrupt: + +```ts +await agent.runAgent({ + resume: [{ interruptId, status: "resolved", payload: true }], +}); +``` + +Use `status: "cancelled"` without a payload when the user abandons a decision. +A resolved payload must match the declared response schema, if any. Missing, +partial, unknown, or invalid responses produce `RUN_ERROR`; they do not execute +the model. Do not send an interrupt response as an ordinary tool-result message. + +Bub records a small native checkpoint for external-call correlation and consumed +interrupt responses. Repeating the same accepted interrupt responses does not +re-execute resumed work; it is not a replay of the earlier generated answer. +Continuation across a server restart requires a persistent Bub tape store. + +## Errors and cancellation + +- Protocol execution failures arrive as `RUN_ERROR`, not `RUN_FINISHED`. + Authentication, malformed request bodies, and body-size limits use HTTP errors. +- `agent.abortRun()` aborts the HTTP request and cancels its execution. It does + not undo side effects of tools that have already run. +- Another executing request on the same thread is rejected with `RUN_ERROR`. + Run one adapter per store/workspace; cross-process coordination is not provided. +- Reusing a run ID does not request replay or provide general request deduplication. + Reconnecting does not restart interrupted work. Retry policy belongs to the + application; consider tool side effects before submitting work again. + +## Embedding and development Use `bub_ag_ui.http.create_app` to embed the endpoint. In an existing Bub gateway, set `channel_managed=True`; an explicitly supplied `AGUIAdapter` must also use `bind_router=False`. Set `manage_framework=False` if the host already manages -`framework.running()`. - -Bub's model configuration and native tool policies still apply. Client-supplied -state and context are application data, not overrides for server configuration. -If you supply a custom model provider, it remains responsible for recording its -model conversation, including recovery from partial exchanges. Displayed output -alone is not automatically converted into model history. - -## Development +`framework.running()`. Bub's native model and tool policies still apply; +client-supplied state/context are application data, not server configuration. From the repository root, with Python 3.12+ and Node.js 22+: @@ -157,9 +138,8 @@ npm ci --prefix packages/bub-ag-ui/tests/client npm test --prefix packages/bub-ag-ui/tests/client ``` -Python tests cover execution, tools, history, isolation, and cancellation. Socket -tests use the published `@ag-ui/client` and assistant-ui integration against a -local server with a deterministic model provider. They need no model API key. -Set `PYTHON` if the interpreter is not at the workspace's `.venv/bin/python`. -Both test layers run in repository CI. These are regression and SDK -interoperability tests, not TCK certification. +Tests cover protocol behavior, native execution, cancellation, and streaming +without per-event store reads. Socket tests exercise the published AG-UI client +and assistant-ui runtime with a deterministic model provider; no model API key +is needed. Set `PYTHON` if the interpreter is not at `.venv/bin/python`. +These are regression and interoperability tests, not TCK certification. diff --git a/packages/bub-ag-ui/src/bub_ag_ui/continuation.py b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py new file mode 100644 index 0000000..3ac250c --- /dev/null +++ b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py @@ -0,0 +1,130 @@ +"""Native tape checkpoint for external calls, not a display history or run journal.""" + +from __future__ import annotations + +import json +from copy import deepcopy +from dataclasses import dataclass, field +from typing import Any + +import jsonschema +from ag_ui.core import RunAgentInput +from bub.core.tape import Tape + +from bub_ag_ui.events import result_text + + +@dataclass +class Continuation: + pending: list[dict[str, Any]] = field(default_factory=list) + responses: list[dict[str, Any]] = field(default_factory=list) + + @classmethod + async def load(cls, tape: Tape) -> Continuation: + entries = await tape.query().kinds("event").all() + for entry in reversed(list(entries)): + if entry.payload.get("name") == "ag_ui.continuation": + return cls(**deepcopy(entry.payload["data"])) + return cls() + + async def save(self, tape: Tape) -> None: + await tape.append_event( + "ag_ui.continuation", + {"pending": deepcopy(self.pending), "responses": deepcopy(self.responses)}, + ) + + def prepare(self, request: RunAgentInput) -> tuple[list[dict[str, Any]], bool]: + """Validate one complete continuation; repeated interrupt responses are no-ops.""" + messages = [m.model_dump(mode="json", by_alias=True) for m in request.messages] + if len({m["id"] for m in messages}) != len(messages): + raise ValueError("Duplicate message IDs") + responses = [ + r.model_dump(mode="json", by_alias=True) for r in request.resume or [] + ] + # Metadata is not the human decision. Idempotency uses the protocol's + # (threadId, interruptId, status, payload) key, not an entire HTTP request. + responses = [ + {k: r.get(k) for k in ("interruptId", "status", "payload")} + for r in responses + ] + responses.sort(key=lambda r: r["interruptId"]) + repeated = bool( + responses + and not self.pending + and json.dumps(responses, sort_keys=True) + == json.dumps(self.responses, sort_keys=True) + ) + if responses and not self.pending and not repeated: + raise ValueError("Unknown or already resolved interrupt") + interrupts = {c["id"]: c for c in self.pending if c["kind"] == "interrupt"} + if len({r["interruptId"] for r in responses}) != len(responses): + raise ValueError("Duplicate interrupt response") + if not repeated and {r["interruptId"] for r in responses} != interrupts.keys(): + raise ValueError("Resume must address every pending interrupt exactly once") + calls = { + call["id"]: call + for message in messages + if message["role"] == "assistant" + for call in message.get("toolCalls", []) + } + for pending in self.pending: + call = calls.get(pending["id"]) + if call is None or call["function"]["name"] != pending["name"].replace( + ".", "_" + ): + raise ValueError( + "Continuation messages must include the pending tool call" + ) + results = [ + m + for m in messages + if m["role"] == "tool" and m["toolCallId"] == pending["id"] + ] + if pending["kind"] == "frontend" and len(results) != 1: + raise ValueError("Provide one result for every pending frontend tool") + if pending["kind"] == "interrupt" and results: + raise ValueError( + "Use resume, not a tool message, to resolve an interrupt" + ) + for response in responses: + call = interrupts.get( + response["interruptId"], {"id": response["interruptId"]} + ) + if repeated and any(m.get("toolCallId") == call["id"] for m in messages): + continue + if call["id"] not in calls: + raise ValueError( + "Resume messages must include the interrupted tool call" + ) + if response["status"] == "resolved" and call.get("response_schema"): + jsonschema.validate(response["payload"], call["response_schema"]) + # Put results immediately after the suspended exchange, before any + # new user input included alongside the decision. + index = ( + next( + i + for i, m in enumerate(messages) + if any(c["id"] == call["id"] for c in m.get("toolCalls", [])) + ) + + 1 + ) + while index < len(messages) and messages[index]["role"] == "tool": + index += 1 + messages.insert( + index, + { + "id": "resume:" + call["id"], + "role": "tool", + "toolCallId": call["id"], + "content": result_text( + response["payload"] + if response["status"] == "resolved" + else {"cancelled": True} + ), + }, + ) + if len({m["id"] for m in messages}) != len(messages): + raise ValueError("Message ID conflicts with an interrupt result") + self.responses = responses or self.responses + self.pending = [] + return messages, repeated diff --git a/packages/bub-ag-ui/src/bub_ag_ui/events.py b/packages/bub-ag-ui/src/bub_ag_ui/events.py index 6475716..0dd7391 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/events.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/events.py @@ -30,8 +30,8 @@ def result_text(value: Any) -> str: return value if isinstance(value, str) else json.dumps(value, ensure_ascii=False) -class Projection: - """Full display history, deliberately independent of tape context anchors.""" +class RunProjection: + """Current response messages/state for validation and interrupt snapshots.""" def __init__(self) -> None: self.messages: list[dict[str, Any]] = [] diff --git a/packages/bub-ag-ui/src/bub_ag_ui/execution.py b/packages/bub-ag-ui/src/bub_ag_ui/execution.py index a8f765e..23ca3ac 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/execution.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/execution.py @@ -22,12 +22,12 @@ from bub.builtin.tools import ALLOWED_TOOLS_STATE_KEY from bub.channels.message import ChannelMessage from bub.core.events import RunStarted, StepFinished, StepStarted +from bub.core.tape import session_tape_name from bub.envelope import Envelope, field_of from bub.streaming import AsyncStreamEvents, StreamEvent, StreamState from bub.turn import TurnState from bub_ag_ui.events import EventStream -from bub_ag_ui.history import AGUIHistory from bub_ag_ui.lifecycle import tape_session from bub_ag_ui.runner import run_steps from bub_ag_ui.state import SharedState @@ -65,17 +65,16 @@ async def iterate() -> AsyncIterator[StreamEvent]: class ExecutionHooks: - @hookimpl - def provide_tape_sidecar(self) -> AGUIHistory: - return AGUIHistory() - @hookimpl(tryfirst=True) async def load_state(self, message: Envelope) -> TurnState | None: if not isinstance(message, AGUIInboundMessage): return None - # Edits/recovery may move the model tape away from the canonical one. + # Native session settings remain thread-scoped; model input comes from + # this request rather than an adapter-owned conversation history. settings = await load_session_settings( - message._runtime_agent.tape(message.model_tape) + message._runtime_agent.tape( + session_tape_name(message.session_id, message.workspace) + ) ) return { **settings, @@ -93,8 +92,8 @@ async def load_state(self, message: Envelope) -> TurnState | None: def build_prompt(self, message: Envelope) -> list[str] | None: if isinstance(message, AGUIInboundMessage): # Descriptive input for the framework's turn result/observers. The - # execution hook reads the canonical protocol history from tape; - # this text must not be appended to that history a second time. + # execution hook consumes the request messages on its native tape; + # this text must not be appended to that input a second time. return [message.content] if message.content else [] return None diff --git a/packages/bub-ag-ui/src/bub_ag_ui/history.py b/packages/bub-ag-ui/src/bub_ag_ui/history.py deleted file mode 100644 index fc35b1b..0000000 --- a/packages/bub-ag-ui/src/bub_ag_ui/history.py +++ /dev/null @@ -1,108 +0,0 @@ -"""AG-UI history mounted on the owning Bub tape, like the spill sidecar. - -The tape is the source of truth. Views are derived on read; there is no separate -journal, sequence counter, or persistent projection/index. Protocol writers use -the canonical owner tape, not the model's temporary execution fork. -""" - -from __future__ import annotations - -from copy import deepcopy -from dataclasses import dataclass, field -from typing import Any - -from bub.core.tape import Tape, TapeEntry, TapeQuery - -from bub_ag_ui.events import Projection - - -@dataclass -class HistoryView: - projection: Projection = field(default_factory=Projection) - runs: dict[str, dict[str, Any]] = field(default_factory=dict) - pending: list[dict[str, Any]] = field(default_factory=list) - model_tape: str = "" - cursor: int = 0 - - def as_dict(self) -> dict[str, Any]: - return { - "messages": self.projection.messages, - "state": self.projection.state, - "pending": self.pending, - "runs": self.runs, - "cursor": self.cursor, - } - - -@dataclass -class RunEvents: - records: list[dict[str, Any]] = field(default_factory=list) - cursor: int = 0 - admitted: bool = False - done: bool = False - - -@dataclass(frozen=True) -class AGUIHistory: - name: str = "ag-ui" - - async def entries(self, tape: Tape) -> list[TapeEntry]: - return ( - await TapeQuery(tape=tape.sidecar_tape_name(self.name), store=tape.store) - .kinds("event") - .all() - ) - - async def read(self, tape: Tape) -> HistoryView: - view = HistoryView(model_tape=tape.name) - for entry in await self.entries(tape): - view.cursor = entry.id - data = deepcopy(entry.payload.get("data", {})) - match entry.payload.get("name"): - case "ag_ui.admit": - view.runs[data["runId"]] = { - "fingerprint": data["fingerprint"], - "done": False, - } - case "ag_ui.checkpoint": - view.pending = data["pending"] - view.model_tape = data["model_tape"] - case "ag_ui.event": - event = data["event"] - view.projection.apply(event) - if event["type"] in {"RUN_FINISHED", "RUN_ERROR"}: - view.runs[data["runId"]].update(done=True, outcome=event) - return view - - async def append(self, tape: Tape, kind: str, **data: Any) -> None: - # Only application patches need a trial application: an invalid path - # must not poison persisted history. Other events use SDK validation. - if kind == "event" and data["event"]["type"] in { - "STATE_DELTA", - "ACTIVITY_DELTA", - }: - view = await self.read(tape) - view.projection.apply(deepcopy(data["event"])) - await tape.store.append( - tape.sidecar_tape_name(self.name), - TapeEntry.event(f"ag_ui.{kind}", deepcopy(data)), - ) - - async def events(self, tape: Tape, run_id: str, after: int = 0) -> RunEvents: - # Followers need only run metadata and new wire records, never a display - # projection. TapeQuery has no entry-ID range API in the pinned Bub SDK; - # keep one durable read rather than inventing a second cursor store. - batch = RunEvents(cursor=after) - for entry in await self.entries(tape): - batch.cursor = max(batch.cursor, entry.id) - data = entry.payload.get("data", {}) - if data.get("runId") != run_id: - continue - if entry.payload.get("name") == "ag_ui.admit": - batch.admitted = True - elif entry.payload.get("name") == "ag_ui.event": - if data["event"]["type"] in {"RUN_FINISHED", "RUN_ERROR"}: - batch.done = True - if entry.id > after: - batch.records.append({"sequence": entry.id, **deepcopy(data)}) - return batch diff --git a/packages/bub-ag-ui/src/bub_ag_ui/http.py b/packages/bub-ag-ui/src/bub_ag_ui/http.py index bf0e211..ba8ac55 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/http.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/http.py @@ -1,11 +1,10 @@ -"""AG-UI's POST/SSE transport, with explicit history, replay and cancel routes.""" +"""AG-UI request/response transport; conversation storage belongs to the host.""" from __future__ import annotations -import asyncio import hmac from collections.abc import Awaitable, Callable -from contextlib import asynccontextmanager, nullcontext +from contextlib import aclosing, asynccontextmanager, nullcontext from typing import Any import bub @@ -86,54 +85,6 @@ def controller() -> AGUIAdapter: raise RuntimeError("ASGI lifespan must start before serving requests") return runtime - def cursor(request: Request) -> int: - try: - value = int( - request.query_params.get( - "after", request.headers.get("last-event-id", "0") - ) - ) - if value < 0: - raise ValueError - return value - except ValueError as exc: - raise ProtocolError("Invalid replay cursor") from exc - - def response( - thread: Any, - run_id: str, - after: int, - user: str, - thread_id: str, - *, - cancel_on_disconnect: bool, - ) -> StreamingResponse: - encoder = EventEncoder() - - async def body(): - completed = False - try: - async for record in controller().events(thread, run_id, after): - event = EVENT.validate_python(record["event"]) - yield f"id: {record['sequence']}\n" + encoder.encode(event) - completed = True - finally: - if not completed and cancel_on_disconnect: - # Shield cleanup from ASGI's cancelled response task. - cleanup = asyncio.create_task( - controller().cancel(user, thread_id, run_id) - ) - await asyncio.shield(cleanup) - - return StreamingResponse( - body(), - media_type=encoder.get_content_type(), - headers={ - "Cache-Control": "no-cache, no-transform", - "X-Accel-Buffering": "no", - }, - ) - async def run(request: Request): user = await principal(request) content = bytearray() @@ -145,64 +96,27 @@ async def run(request: Request): data = RunAgentInput.model_validate_json(bytes(content)) except ValidationError as exc: raise ProtocolError("Invalid RunAgentInput", 422) from exc - after = cursor(request) - submission = await controller().submit(user, data) - return response( - submission.thread, - data.run_id, - after, - user, - data.thread_id, - cancel_on_disconnect=submission.created, - ) - - async def history(request: Request): - user = await principal(request) - thread = await controller().thread(user, request.path_params["thread_id"]) - return JSONResponse(await thread.history()) + encoder = EventEncoder() - async def replay(request: Request): - user = await principal(request) - thread_id, run_id = ( - request.path_params["thread_id"], - request.path_params["run_id"], - ) - thread = await controller().thread(user, thread_id) - if run_id not in (await thread.read()).runs: - raise ProtocolError("Run not found", 404) - return response( - thread, - run_id, - cursor(request), - user, - thread_id, - cancel_on_disconnect=False, - ) + async def body(): + async with aclosing(controller().run(user, data)) as events: + async for event in events: + yield encoder.encode(EVENT.validate_python(event)) - async def cancel(request: Request): - user = await principal(request) - thread_id, run_id = ( - request.path_params["thread_id"], - request.path_params["run_id"], + return StreamingResponse( + body(), + media_type=encoder.get_content_type(), + headers={ + "Cache-Control": "no-cache, no-transform", + "X-Accel-Buffering": "no", + }, ) - thread = await controller().thread(user, thread_id) - if run_id not in (await thread.read()).runs: - raise ProtocolError("Run not found", 404) - cancelled = await controller().cancel(user, thread_id, run_id) - return JSONResponse({"cancelled": cancelled}) async def error(request: Request, exc: ProtocolError): return JSONResponse({"error": str(exc)}, status_code=exc.status) app = Starlette( - routes=[ - Route("/agent", run, methods=["POST"]), - Route("/threads/{thread_id}/history", history), - Route("/threads/{thread_id}/runs/{run_id}/events", replay), - Route( - "/threads/{thread_id}/runs/{run_id}/cancel", cancel, methods=["POST"] - ), - ], + routes=[Route("/agent", run, methods=["POST"])], lifespan=lifespan, exception_handlers={ProtocolError: error}, ) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runner.py b/packages/bub-ag-ui/src/bub_ag_ui/runner.py index fbdabef..8ca7de1 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runner.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runner.py @@ -1,6 +1,6 @@ """Protocol turn orchestration over Bub's public, native step API. -AG-UI has a durable return-to-client boundary, unlike a continuous CLI loop. +AG-UI has a return-to-client boundary, unlike a continuous CLI loop. BubAgent still owns model requests, policy hooks and the entire tool batch. No agent method is overridden and no backend tool is executed by this module. """ @@ -155,9 +155,6 @@ def tool_finished(event: ToolCallFinished) -> None: yield StreamEvent( "tool_result", {"call_ids": ids, "tool_results": values} ) - await tape.append_event( - "ag_ui.suspended", {"pending": external.pending} - ) final = {"ok": True, "pending": True} yield StreamEvent("final", final) pending = [] diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py index 6fcc86f..63c444c 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py @@ -1,4 +1,4 @@ -"""Protocol sessions and run ownership, independent of the HTTP transport.""" +"""Request-scoped AG-UI execution over Bub; no conversation-management service.""" from __future__ import annotations @@ -8,7 +8,6 @@ import logging from collections.abc import AsyncIterator, Callable from copy import deepcopy -from dataclasses import dataclass, field from pathlib import Path from typing import Any from uuid import uuid4 @@ -16,15 +15,15 @@ import jsonschema from ag_ui.core import RunAgentInput from bub.builtin.agent import BubAgent -from bub.core.tape import Tape, TapeEntry, session_tape_name, to_message +from bub.core.tape import TapeEntry, session_tape_name, to_message -from bub_ag_ui.events import EventStream, result_text, wire_event +from bub_ag_ui.continuation import Continuation +from bub_ag_ui.events import EventStream, RunProjection, result_text from bub_ag_ui.execution import ( AGUIInboundMessage, StreamRouter, install_execution_hooks, ) -from bub_ag_ui.history import AGUIHistory, HistoryView, RunEvents from bub_ag_ui.lifecycle import committed from bub_ag_ui.state import SharedState from bub_ag_ui.tools import ExternalTools, InterruptTool @@ -38,13 +37,6 @@ def __init__(self, message: str, status: int = 400) -> None: self.status = status -def fingerprint(request: RunAgentInput) -> str: - value = request.model_dump(mode="json", by_alias=True) - return hashlib.sha256( - json.dumps(value, sort_keys=True, ensure_ascii=False).encode() - ).hexdigest() - - def model_message(message: dict[str, Any]) -> dict[str, Any] | None: role = message["role"] if role not in {"user", "assistant", "tool"}: @@ -69,9 +61,7 @@ def model_message(message: dict[str, Any]) -> dict[str, Any] | None: raise ProtocolError("Media input requires a URL or data source") parts.append(data) else: - raise ProtocolError( - f"Bub 0.6.1 cannot consume {part['type']} input", 422 - ) + raise ProtocolError(f"Bub cannot consume {part['type']} input", 422) content = parts result = {"role": role, "content": content} if role == "assistant" and message.get("toolCalls"): @@ -87,67 +77,12 @@ def model_message(message: dict[str, Any]) -> dict[str, Any] | None: return result -@dataclass -class Thread: - """Execution synchronization only; durable state belongs to the mounted tape.""" - - tape: Tape - changed: asyncio.Condition = field(default_factory=asyncio.Condition) - admission: asyncio.Lock = field(default_factory=asyncio.Lock) - - @property - def sidecar(self) -> AGUIHistory: - return self.tape.get_sidecar("ag-ui") - - async def read(self) -> HistoryView: - return await self.sidecar.read(self.tape) - - async def history(self) -> dict[str, Any]: - return (await self.read()).as_dict() - - async def events(self, run_id: str, after: int = 0) -> RunEvents: - return await self.sidecar.events(self.tape, run_id, after) - - async def append(self, kind: str, **data: Any) -> None: - async with self.changed: - - async def write() -> None: - await self.sidecar.append(self.tape, kind, **data) - self.changed.notify_all() - - await committed(write()) - - async def checkpoint(self, pending: list[dict[str, Any]], model_tape: str) -> None: - await self.append("checkpoint", pending=pending, model_tape=model_tape) - - -@dataclass(frozen=True) -class Submission: - thread: Thread - created: bool - - -@dataclass(frozen=True) -class RunningRun: - run_id: str - task: asyncio.Task[None] - started: asyncio.Event - - async def cancel(self) -> None: - # Cancellation must enter the producer's cleanup boundary, even before - # its first token. No lock is handed from the HTTP task to the producer. - await self.started.wait() - if not self.task.done() and not self.task.cancelling(): - self.task.cancel() - await asyncio.gather(self.task, return_exceptions=True) - - class AGUIAdapter: - """One single-writer controller per configured store/workspace. + """Adapt a request and its native execution to an AG-UI event stream. - A run task owns execution; subscribers follow the mounted history tape. Abort - cancels execution, not merely the HTTP response. Replaying a run never invokes - the model or its tools again. Server restart marks unfinished runs failed. + The client supplies conversation messages/state on every run. Only external + call correlation and interrupt response receipts survive in the native tape. + Streaming does not read back events from storage. """ def __init__( @@ -168,335 +103,138 @@ def __init__( self.interrupts = interrupts self.agent_factory = agent_factory self.max_run_seconds = max_run_seconds - self._threads: dict[tuple[str, str], Thread] = {} - self._runs: dict[str, RunningRun] = {} - self._closed = False - self._loading = asyncio.Lock() self._storage_agent = agent_factory(framework) + self._active: set[str] = set() + self._tasks: set[asyncio.Task[None]] = set() + self._closed = False def session_id(self, principal: str, thread_id: str) -> str: identity = json.dumps([principal, thread_id], ensure_ascii=False) return "ag-ui:" + hashlib.sha256(identity.encode()).hexdigest() - async def thread(self, principal: str, thread_id: str) -> Thread: - if not principal or not thread_id or len(thread_id) > 512: - raise ProtocolError("Invalid principal/threadId") - key = principal, thread_id - async with self._loading: - if key not in self._threads: - name = session_tape_name(self.session_id(*key), self.workspace) - thread = Thread(self._storage_agent.tape(name)) - await self._recover(thread) - self._threads[key] = thread - return self._threads[key] - - async def history(self, principal: str, thread_id: str) -> dict[str, Any]: - thread = await self.thread(principal, thread_id) - return (await thread.read()).as_dict() - - async def _recover(self, thread: Thread) -> None: - for run_id, run in list((await thread.read()).runs.items()): - if run["done"]: - continue - # Recovery must fail closed if storage is unavailable; unlike a - # live producer, there is no owning task to report an interrupted run. - await self._finish_failed_run( - thread, - run_id, - "Server stopped before this run completed; it will not be executed again", - "INTERRUPTED", - ) - - async def _close_abandoned(self, thread: Thread, run_id: str) -> None: - opened: dict[tuple[str, str], str] = {} - steps: set[str] = set() - for record in (await thread.events(run_id)).records: - event = record["event"] - kind = event["type"] - if kind == "STEP_STARTED": - steps.add(event["stepName"]) - elif kind == "STEP_FINISHED": - steps.discard(event["stepName"]) - for prefix, id_key in [ - ("TEXT_MESSAGE", "messageId"), - ("REASONING_MESSAGE", "messageId"), - ("REASONING", "messageId"), - ("TOOL_CALL", "toolCallId"), - ]: - if kind == prefix + "_START": - opened[prefix, event[id_key]] = id_key - elif kind == prefix + "_END": - opened.pop((prefix, event[id_key]), None) - for (prefix, id), key in reversed(list(opened.items())): - await thread.append( - "event", runId=run_id, event=wire_event(prefix + "_END", **{key: id}) - ) - for step in steps: - await thread.append( - "event", runId=run_id, event=wire_event("STEP_FINISHED", stepName=step) - ) - - async def _write_messages(self, name: str, messages: list[dict[str, Any]]) -> None: - tape = self._storage_agent.tape(name) - await tape.ensure_bootstrap_anchor() - for message in messages: - if converted := model_message(message): - await tape.store.append( - tape.name, - TapeEntry.message(converted, ag_ui_message_id=message["id"]), - ) + async def run( + self, principal: str, request: RunAgentInput + ) -> AsyncIterator[dict[str, Any]]: + # Bounded, transient delivery only. No event IDs, replay buffers, durable + # wire journal, or global run registry. Closing this iterator owns cleanup. + queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue(maxsize=16) + disconnected = False - def _reconcile( - self, request: RunAgentInput, view: HistoryView - ) -> tuple[list[dict[str, Any]], list[dict[str, Any]], bool]: - incoming = [ - message.model_dump(mode="json", by_alias=True) - for message in request.messages - ] - if len({message["id"] for message in incoming}) != len(incoming): - raise ProtocolError("Duplicate message IDs") - existing = {message["id"]: message for message in view.projection.messages} - pending = {call["id"]: call for call in view.pending} - additions: list[dict[str, Any]] = [] - seen_calls: set[str] = set() - branch = False - edited: set[str] = set() - for message in incoming: - old = existing.get(message["id"]) - if old is not None: - # Echoed history is not an update: the stored message is authoritative. - # Only user edits participate in creating a new context branch. - if ( - old["role"] == message["role"] == "user" - and old["content"] != message["content"] - ): - branch = True - edited.add(message["id"]) - continue - if message["role"] == "tool": - call_id = message["toolCallId"] - if ( - call_id not in pending - or pending[call_id]["kind"] != "frontend" - or call_id in seen_calls - ): - raise ProtocolError( - "Unknown, duplicate, or non-frontend tool result", 409 - ) - seen_calls.add(call_id) - elif message["role"] != "user": - raise ProtocolError( - "Only user input and results of pending frontend calls may be appended", - 409, - ) - additions.append(message) - if request.resume: - for resume in request.resume: - call = pending.get(resume.interrupt_id) - if ( - call is None - or call["kind"] != "interrupt" - or call["id"] in seen_calls - ): - raise ProtocolError("Unknown or duplicate interrupt", 409) - if resume.status == "resolved" and call.get("response_schema"): - try: - jsonschema.validate(resume.payload, call["response_schema"]) - except jsonschema.ValidationError as exc: - raise ProtocolError( - "Resume payload does not match responseSchema", 422 - ) from exc - seen_calls.add(call["id"]) - additions.append( - { - "id": str(uuid4()), - "role": "tool", - "toolCallId": call["id"], - "content": result_text( - resume.payload - if resume.status == "resolved" - else {"cancelled": True} - ), - } - ) - if pending: - if branch or ( - any(message["role"] == "user" for message in additions) - and seen_calls != pending.keys() - ): - raise ProtocolError( - "Resolve pending tools/interrupts before sending a new user turn", - 409, - ) - if not seen_calls: - raise ProtocolError( - "Expected a pending tool result or resume entry", 409 - ) - if pending: - additions = [ - message for message in additions if message["role"] == "tool" - ] + [message for message in additions if message["role"] == "user"] - # The AG-UI runtime sends a truncated history for edit/regenerate. Keep - # old tapes immutable, and build a new context generation from server data. - if ( - not pending - and incoming - and incoming[-1]["role"] == "user" - and existing.get(incoming[-1]["id"], {}).get("role") == "user" - ): - target = incoming[-1] - index = next( - i - for i, m in enumerate(view.projection.messages) - if m["id"] == target["id"] - ) - if branch or not additions: - if additions or (edited and edited != {target["id"]}): - raise ProtocolError( - "Only the final user message may be edited", 409 - ) - model_message(target) - return ( - deepcopy(view.projection.messages[:index]) + [target], - [], - True, - ) - if branch: - raise ProtocolError( - "An edited user message must be the final request message", 409 - ) - if not additions: - raise ProtocolError("No new user message or pending tool result", 409) - for message in additions: - try: - model_message(message) - except (ValueError, TypeError) as exc: - raise ProtocolError("Invalid message content", 422) from exc - return ( - additions, - [call for id, call in pending.items() if id not in seen_calls], - False, - ) + async def emit(event: dict[str, Any]) -> None: + task = asyncio.current_task() + if not disconnected and not (task and task.cancelling()): + await queue.put(event) - async def submit(self, principal: str, request: RunAgentInput) -> Submission: - if not request.run_id or len(request.run_id) > 512: - raise ProtocolError("Invalid runId") - thread = await self.thread(principal, request.thread_id) - async with thread.admission: - if self._closed: - raise ProtocolError("Adapter is shutting down", 503) - view = await thread.read() - digest = fingerprint(request) - if run := view.runs.get(request.run_id): - if run["fingerprint"] != digest: - raise ProtocolError( - "runId already belongs to a different request", 409 - ) - return Submission(thread, created=False) - if thread.tape.name in self._runs: - raise ProtocolError("Another run owns this thread", 409) - additions, remaining, branch = self._reconcile(request, view) - for tool in request.tools: + producer = asyncio.create_task(self._execute(principal, request, emit)) + self._tasks.add(producer) + try: + while not producer.done() or not queue.empty(): + if not queue.empty(): + yield queue.get_nowait() + continue + next_event = asyncio.create_task(queue.get()) try: - jsonschema.Draft202012Validator.check_schema(tool.parameters) - except jsonschema.SchemaError as exc: - raise ProtocolError("Invalid frontend tool schema", 422) from exc - agent = self.agent_factory( - self.framework, - tape_store=thread.tape.store, - ) - # Admission is validation only. Framework hooks run exactly once, - # inside process_inbound, and never for a rejected/replayed request. - try: - external = ExternalTools([*request.tools, *self.interrupts]) - external.validate(agent) - except ValueError as exc: - raise ProtocolError(str(exc), 422) from exc - try: - await thread.append("admit", runId=request.run_id, fingerprint=digest) - except BaseException: - # A cancelled append may have committed before propagating the - # cancellation. Close that admission before releasing the gate. - if request.run_id in (await thread.read()).runs: - await self._fail( - thread, - request.run_id, - "Run cancelled during admission", - "CANCELLED", + await asyncio.wait( + (producer, next_event), return_when=asyncio.FIRST_COMPLETED ) - raise - started = asyncio.Event() - task = asyncio.create_task( - self._execute( - thread, - request, - agent, - additions, - remaining, - branch, - self.session_id(principal, request.thread_id), - external, - started, - ), - name=f"ag-ui:{request.run_id}", - ) - self._runs[thread.tape.name] = RunningRun(request.run_id, task, started) - return Submission(thread, created=True) - - async def _execute( - self, - thread: Thread, - request: RunAgentInput, - agent: BubAgent, - additions: list[dict[str, Any]], - remaining: list[dict[str, Any]], - branch: bool, - session_id: str, - external: ExternalTools, - started: asyncio.Event, - ) -> None: - async def emit(event: dict[str, Any]) -> None: - await thread.append("event", runId=request.run_id, event=event) - - stream = EventStream(emit) + if next_event.done(): + yield next_event.result() + finally: + if not next_event.done(): + next_event.cancel() + await asyncio.gather(next_event, return_exceptions=True) + await producer + finally: + disconnected = True + if not producer.done(): + producer.cancel() + await asyncio.gather(producer, return_exceptions=True) + self._tasks.discard(producer) + + async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> None: + # This projection lives only for this response: patches are validated and + # an interrupt can publish the full resume context before RUN_FINISHED. + projection = RunProjection() + + async def publish(event: dict[str, Any]) -> None: + projection.apply(event) + await emit(event) + + stream = EventStream(publish) + session_id = self.session_id(principal, request.thread_id) + acquired = False try: - started.set() - model_tape = (await thread.read()).model_tape + await stream.send( + "RUN_STARTED", + threadId=request.thread_id, + runId=request.run_id, + parentRunId=request.parent_run_id, + ) + if self._closed: + raise ProtocolError("Adapter is shutting down") + if not principal or not request.thread_id or len(request.thread_id) > 512: + raise ProtocolError("Invalid principal/threadId") + if not request.run_id or len(request.run_id) > 512: + raise ProtocolError("Invalid runId") + if session_id in self._active: + raise ProtocolError("Another request is executing on this thread") + self._active.add(session_id) + acquired = True async with asyncio.timeout(self.max_run_seconds): - await stream.send( - "RUN_STARTED", - threadId=request.thread_id, - runId=request.run_id, - parentRunId=request.parent_run_id, + tape = self._storage_agent.tape( + session_tape_name(session_id, self.workspace) ) - if branch: - model_tape = model_tape + ":branch:" + str(uuid4()) - messages = additions - else: - messages = [*(await thread.read()).projection.messages, *additions] - - async def prepare() -> None: - await stream.send("MESSAGES_SNAPSHOT", messages=messages) - await self._write_messages(model_tape, additions) - await thread.checkpoint(remaining, model_tape) - - await committed(prepare()) + continuation = await Continuation.load(tape) + try: + had_pending = bool(continuation.pending) + messages, repeated = continuation.prepare(request) + agent = self.agent_factory(self.framework, tape_store=tape.store) + external = ExternalTools([*request.tools, *self.interrupts]) + external.validate(agent) + for definition in external.definitions: + jsonschema.Draft202012Validator.check_schema( + definition.parameters + ) + converted = [ + value + for m in messages + if (value := model_message(m)) is not None + ] + except ( + ValueError, + jsonschema.ValidationError, + jsonschema.SchemaError, + ) as exc: + raise ProtocolError(str(exc)) from exc + await stream.send("MESSAGES_SNAPSHOT", messages=messages) shared = SharedState( - request.state - if request.state is not None - else (await thread.read()).projection.state, - emit, + request.state if request.state is not None else {}, publish ) await shared.set(shared.value) - if not remaining: + if not repeated: + # Each request supplies its context. A fresh native execution + # tape prevents server history from silently overriding or + # duplicating client messages; Bub still records model steps. + model_tape = tape.name + ":run:" + str(uuid4()) + execution = agent.tape(model_tape) + await execution.ensure_bootstrap_anchor() + for message in converted: + await committed( + execution.store.append( + model_tape, TapeEntry.message(message) + ) + ) + if had_pending: + # Record receipt before executing any resumed side effect. + await committed(continuation.save(tape)) inbound = AGUIInboundMessage( session_id=session_id, channel="ag-ui", chat_id=request.thread_id, content=next( ( - m["content"] - for m in reversed(additions) + m.get("content", "") + for m in reversed(messages) if m["role"] == "user" and isinstance(m.get("content"), str) ), @@ -516,26 +254,30 @@ async def prepare() -> None: raise RuntimeError( "AG-UI stream did not complete; check channel configuration" ) - # Native stream cleanup and save/render/dispatch hooks have - # completed before the controller checkpoints and finishes. - await thread.checkpoint(external.pending, model_tape) + continuation.pending = external.pending + if external.pending: + await committed(continuation.save(tape)) await stream.close() interrupts = [ { - "id": call["id"], - "toolCallId": call["id"], - "reason": call["reason"], - "responseSchema": call.get("response_schema"), + "id": c["id"], + "toolCallId": c["id"], + "reason": c["reason"], + "responseSchema": c.get("response_schema"), "metadata": { - "arguments": json.loads(call["arguments"]), - "toolName": call["name"], + "arguments": json.loads(c["arguments"]), + "toolName": c["name"], }, } - for call in (await thread.read()).pending - if call["kind"] == "interrupt" + for c in continuation.pending + if c["kind"] == "interrupt" ] fields: dict[str, Any] = {} if interrupts: + await stream.send( + "MESSAGES_SNAPSHOT", messages=deepcopy(projection.messages) + ) + await shared.set(shared.value) fields["outcome"] = {"type": "interrupt", "interrupts": interrupts} await stream.send( "RUN_FINISHED", @@ -544,80 +286,24 @@ async def prepare() -> None: **fields, ) except asyncio.CancelledError: - # A cancellation racing the terminal append may arrive after that - # write committed. Never publish a second terminal outcome. - await self._fail(thread, request.run_id, "Run cancelled", "CANCELLED") - except Exception: - logger.exception("AG-UI run failed: %s", request.run_id) - await self._fail( - thread, - request.run_id, - "Agent run failed; see server logs", - "RUN_FAILED", + raise + except Exception as exc: + logger.exception("AG-UI request failed") + await stream.close() + message = ( + str(exc) + if isinstance(exc, ProtocolError) + else "Agent run failed; see server logs" ) + if isinstance(exc, TimeoutError): + message = "Agent run timed out" + await stream.send("RUN_ERROR", message=message, code="RUN_FAILED") finally: - self._runs.pop(thread.tape.name, None) - async with thread.changed: - thread.changed.notify_all() - - async def _fail(self, thread: Thread, run_id: str, message: str, code: str) -> None: - try: - await self._finish_failed_run(thread, run_id, message, code) - except Exception: - logger.exception("Unable to persist AG-UI failure; run remains interrupted") - - async def _finish_failed_run( - self, thread: Thread, run_id: str, message: str, code: str - ) -> None: - view = await thread.read() - if view.runs.get(run_id, {}).get("done"): - return - await thread.checkpoint([], view.model_tape) - await self._close_abandoned(thread, run_id) - # Close protocol boundaries before the terminal commit; never reconstruct - # model context or invent tool results from display-only events. - # Restart retries unfinished protocol cleanup; - # cancellation racing a committed terminal outcome cannot append another. - await thread.append( - "event", - runId=run_id, - event=wire_event("RUN_ERROR", message=message, code=code), - ) - - async def events( - self, thread: Thread, run_id: str, after: int = 0 - ) -> AsyncIterator[dict[str, Any]]: - cursor = after - while True: - async with thread.changed: - batch = await thread.events(run_id, cursor) - cursor = batch.cursor - if not batch.admitted: - raise ProtocolError( - "Run history was reset; reload thread history", 409 - ) - running = self._runs.get(thread.tape.name) - done = batch.done or running is None or running.run_id != run_id - if not batch.records and not done: - await thread.changed.wait() - continue - for record in batch.records: - yield record - if done: - return - - async def cancel(self, principal: str, thread_id: str, run_id: str) -> bool: - thread = await self.thread(principal, thread_id) - running = self._runs.get(thread.tape.name) - if running is None or running.run_id != run_id: - return False - await running.cancel() - return True + if acquired: + self._active.discard(session_id) async def close(self) -> None: self._closed = True - # Finish in-flight admissions before taking the shutdown snapshot. - for thread in list(self._threads.values()): - async with thread.admission: - pass - await asyncio.gather(*(run.cancel() for run in list(self._runs.values()))) + for task in list(self._tasks): + task.cancel() + await asyncio.gather(*list(self._tasks), return_exceptions=True) diff --git a/packages/bub-ag-ui/tests/client/protocol.test.mjs b/packages/bub-ag-ui/tests/client/protocol.test.mjs index 8c672e5..cea410e 100644 --- a/packages/bub-ag-ui/tests/client/protocol.test.mjs +++ b/packages/bub-ag-ui/tests/client/protocol.test.mjs @@ -26,7 +26,7 @@ before(async () => { server = spawn(python, [fileURLToPath(new URL("../client_server.py", import.meta.url)), String(port)], { stdio: ["ignore", "ignore", "pipe"] }); server.stderr.on("data", data => { logs += data.toString(); }); for (let i = 0; i < 100; i++) { - try { if ((await fetch(`${base}/threads/probe/history`)).ok) return; } catch {} + try { await fetch(`${base}/agent`); return; } catch {} await new Promise(resolve => setTimeout(resolve, 50)); } throw new Error(`Test server failed to start: ${logs}`); @@ -37,15 +37,16 @@ function agent(prompt) { return new HttpAgent({ url: `${base}/agent`, threadId: crypto.randomUUID(), initialMessages: [{id: crypto.randomUUID(), role: "user", content: prompt}] }); } -test("HttpAgent consumes streamed answer; assistant-ui restores identical history", async () => { +test("HttpAgent consumes streamed answer; application can restore client messages", async () => { const a = agent("hello"); const events = []; await a.runAgent({runId: "text"}, {onEvent: ({event}) => events.push(event)}); assert.equal(events.at(-1).type, "RUN_FINISHED"); assert.equal(a.messages.at(-1).content, "Answer: hello"); - const history = await (await fetch(`${base}/threads/${a.threadId}/history`)).json(); - assert.deepEqual(history.messages, a.messages); - assert.deepEqual(fromAgUiMessages(history.messages), fromAgUiMessages(a.messages)); + // Persistence is the application's choice, not another adapter endpoint. + const saved = JSON.parse(JSON.stringify({messages: a.messages, state: a.state})); + const restored = new HttpAgent({url: `${base}/agent`, threadId: a.threadId, initialMessages: saved.messages, initialState: saved.state}); + assert.deepEqual(fromAgUiMessages(restored.messages), fromAgUiMessages(a.messages)); }); test("backend tool has parent, result and final answer", async () => { @@ -75,7 +76,9 @@ test("frontend call terminates, accepts tool result and continues without a fake test("interrupt outcome and resume are understood by the protocol client", async () => { const a = agent("interrupt"); let outcome; - await a.runAgent({}, {onRunFinishedEvent: ({event}) => {outcome = event.outcome;}}); + const events = []; + await a.runAgent({}, {onEvent: ({event}) => events.push(event), onRunFinishedEvent: ({event}) => {outcome = event.outcome;}}); + assert.deepEqual(events.slice(-3).map(e => e.type), ["MESSAGES_SNAPSHOT", "STATE_SNAPSHOT", "RUN_FINISHED"]); assert.equal(outcome.type, "interrupt"); await a.runAgent({resume: [{interruptId: outcome.interrupts[0].id, status: "resolved", payload: true}]}); assert.equal(a.messages.at(-1).content, "Answer: interrupt"); @@ -95,7 +98,7 @@ test("provider errors do not turn into success", async () => { assert.equal(events.filter(e => e.type === "RUN_FINISHED").length, 0); }); -test("useAgUiRuntime executes frontend tools and automatically continues the thread", async () => { +test("useAgUiRuntime executes parallel frontend tools and continues with all results", async () => { const React = await import("react"); const {create, act} = await import("react-test-renderer"); const {useAgUiRuntime} = await import("@assistant-ui/react-ag-ui"); @@ -113,58 +116,45 @@ test("useAgUiRuntime executes frontend tools and automatically continues the thr unregister = runtime.registerModelContextProvider({getModelContext: () => ({tools: { browser: {description: "Browser action", parameters: {type: "object", properties: {}}, execute: async () => {executed++; return "browser value";}} }})}); - runtime.thread.append("frontend"); + runtime.thread.append("frontend-parallel"); }); for (let i = 0; i < 100; i++) { await act(async () => { await new Promise(resolve => setTimeout(resolve, 20)); }); const state = runtime.thread.getState(); // Receiving the final text chunk precedes RUN_FINISHED and React's idle state. - if (!state.isRunning && state.messages.some(m => m.content.some(p => p.type === "text" && p.text === "Answer: frontend"))) break; + if (!state.isRunning && state.messages.some(m => m.content.some(p => p.type === "text" && p.text === "Answer: frontend-parallel"))) break; } const snapshot = runtime.thread.getState(); await act(async () => { unregister(); root.unmount(); }); assert.deepEqual(errors, []); - assert.equal(executed, 1); + assert.equal(executed, 2); assert.equal(snapshot.isRunning, false); - assert.ok(snapshot.messages.some(m => m.content.some(p => p.type === "text" && p.text === "Answer: frontend"))); + assert.ok(snapshot.messages.some(m => m.content.some(p => p.type === "text" && p.text === "Answer: frontend-parallel"))); }); -test("aborting the HTTP stream cancels server execution and persists cancellation", async () => { +test("aborting the HTTP stream releases execution without a cancel endpoint", async () => { const a = agent("cancel"); let started; const ready = new Promise(resolve => {started = resolve;}); - const running = a.runAgent({runId: "cancel-run"}, {onToolCallEndEvent: () => started()}).catch(() => {}); + const running = a.runAgent({}, {onToolCallEndEvent: () => started()}).catch(() => {}); await ready; a.abortRun(); await running; - let history; for (let i = 0; i < 100; i++) { - history = await (await fetch(`${base}/threads/${a.threadId}/history`)).json(); - if (history.runs["cancel-run"].done) break; - await new Promise(resolve => setTimeout(resolve, 20)); + const next = new HttpAgent({url: `${base}/agent`, threadId: a.threadId, initialMessages: [{id: crypto.randomUUID(), role: "user", content: "hello"}]}); + try { + await next.runAgent({}); + assert.equal(next.messages.at(-1).content, "Answer: hello"); + return; + } catch { await new Promise(resolve => setTimeout(resolve, 20)); } } - assert.equal(history.runs["cancel-run"].outcome.code, "CANCELLED"); - assert.equal(history.runs["cancel-run"].outcome.type, "RUN_ERROR"); + assert.fail("thread was not released after abort"); }); -test("disconnecting a duplicate POST does not cancel the owning run", async () => { - const a = agent("cancel"); - const body = {threadId: a.threadId, runId: "shared", messages: a.messages, tools: [], context: [], state: {}, forwardedProps: {}}; - const ownerAbort = new AbortController(); - const owner = await fetch(`${base}/agent`, {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify(body), signal: ownerAbort.signal}); - assert.equal(owner.status, 200); - const ownerReader = owner.body.getReader(); - await ownerReader.read(); - const duplicateAbort = new AbortController(); - const duplicate = await fetch(`${base}/agent`, {method: "POST", headers: {"Content-Type": "application/json"}, body: JSON.stringify(body), signal: duplicateAbort.signal}); - const reader = duplicate.body.getReader(); - await reader.read(); - duplicateAbort.abort(); - await new Promise(resolve => setTimeout(resolve, 40)); - const history = await (await fetch(`${base}/threads/${a.threadId}/history`)).json(); - assert.equal(history.runs.shared.done, false); - await fetch(`${base}/threads/${a.threadId}/runs/shared/cancel`, {method: "POST"}); - ownerAbort.abort(); +test("only the AG-UI run endpoint is exposed", async () => { + assert.equal((await fetch(`${base}/threads/t/history`)).status, 404); + assert.equal((await fetch(`${base}/threads/t/runs/r/events`)).status, 404); + assert.equal((await fetch(`${base}/threads/t/runs/r/cancel`, {method: "POST"})).status, 404); }); test("reasoning stays distinct from assistant answer text", async () => { @@ -176,14 +166,12 @@ test("reasoning stays distinct from assistant answer text", async () => { assert.equal(a.messages.at(-1).content, "Answer: reasoning"); }); -test("activity replacement and patches produce the same history as the official reducer", async () => { +test("activity replacement and patches are consumed by the official reducer", async () => { const a = agent("activity"); await a.runAgent({}); const activity = a.messages.find(m => m.role === "activity"); assert.equal(activity.activityType, "progress"); assert.deepEqual(activity.content, {count: 3}); - const history = await (await fetch(`${base}/threads/${a.threadId}/history`)).json(); - assert.deepEqual(history.messages, a.messages); }); @@ -197,6 +185,12 @@ test("native child steps do not collide with the parent's AG-UI steps", async () assert.equal(a.messages.at(-1).content, "Answer: subagent"); assert.equal(a.messages.filter(m => m.role === "tool").length, 1); assert.equal(a.messages.find(m => m.role === "tool").content, "child result"); - const history = await (await fetch(`${base}/threads/${a.threadId}/history`)).json(); - assert.deepEqual(history.messages, a.messages); +}); + + +test("invalid interrupt resumes end with RUN_ERROR, not an HTTP error", async () => { + const a = agent("interrupt"); + const events = []; + await a.runAgent({resume: [{interruptId: "unknown", status: "resolved", payload: true}]}, {onEvent: ({event}) => events.push(event)}).catch(() => {}); + assert.deepEqual(events.map(e => e.type), ["RUN_STARTED", "RUN_ERROR"]); }); diff --git a/packages/bub-ag-ui/tests/client_server.py b/packages/bub-ag-ui/tests/client_server.py index 2d21f96..7ac8c30 100644 --- a/packages/bub-ag-ui/tests/client_server.py +++ b/packages/bub-ag-ui/tests/client_server.py @@ -37,6 +37,7 @@ async def respond(request): tool = { "backend": "clock", "frontend": "browser", + "frontend-parallel": "browser", "interrupt": "approve", "state": "update", "activity": "activity", @@ -53,6 +54,14 @@ async def respond(request): } ] } + if prompt == "frontend-parallel" and "tool_calls" in delta: + delta["tool_calls"].append( + { + "index": 1, + "id": "call-browser-2", + "function": {"name": "browser", "arguments": "{}"}, + } + ) events = [ { "choices": [ diff --git a/packages/bub-ag-ui/tests/conftest.py b/packages/bub-ag-ui/tests/conftest.py index 6316de5..9ef4439 100644 --- a/packages/bub-ag-ui/tests/conftest.py +++ b/packages/bub-ag-ui/tests/conftest.py @@ -71,9 +71,11 @@ async def setup(monkeypatch, tmp_path): store = InMemoryTapeStore() adapters = [] - def make(tools=(), **kwargs): + def make(tools=(), *, tape_store=None, **kwargs): def factory(*args, **options): - options.setdefault("tape_store", store) + options.setdefault( + "tape_store", tape_store if tape_store is not None else store + ) return BubAgent(*args, tools=tools, skill_dirs=[], **options) adapter = AGUIAdapter( @@ -100,9 +102,32 @@ def request(messages=None, *, run="r1", thread="t1", tools=None, **kwargs): ) +class ClientView: + """Test-side reducer, never an adapter history API.""" + + def __init__(self, projection, pending): + self.projection = projection + self.pending = pending + + async def history(self): + return { + "messages": self.projection.messages, + "state": self.projection.state, + "pending": self.pending, + } + + async def collect(adapter, data, user="alice"): - submission = await adapter.submit(user, data) - thread = submission.thread - records = [record async for record in adapter.events(thread, data.run_id)] - events = [record["event"] for record in records] - return thread, events + from bub.core.tape import session_tape_name + from bub_ag_ui.continuation import Continuation + from bub_ag_ui.events import RunProjection + + events = [event async for event in adapter.run(user, data)] + projection = RunProjection() + for event in events: + projection.apply(event) + tape = adapter._storage_agent.tape( + session_tape_name(adapter.session_id(user, data.thread_id), adapter.workspace) + ) + pending = (await Continuation.load(tape)).pending + return ClientView(projection, pending), events diff --git a/packages/bub-ag-ui/tests/test_events.py b/packages/bub-ag-ui/tests/test_events.py index f1532d0..6cf4f2f 100644 --- a/packages/bub-ag-ui/tests/test_events.py +++ b/packages/bub-ag-ui/tests/test_events.py @@ -3,18 +3,16 @@ import pytest from ag_ui.core import ActivityDeltaEvent, ActivitySnapshotEvent +from bub_ag_ui.events import RunProjection from bub_ag_ui.state import SharedState from jsonpatch import JsonPatchConflict -async def test_invalid_activity_patch_does_not_change_history(setup): - _, make = setup - adapter = make() - thread = await adapter.thread("alice", "t1") - await thread.append("admit", runId="r", fingerprint="test") +async def test_invalid_activity_patch_does_not_change_response(): + projection = RunProjection() async def emit(event): - await thread.append("event", runId="r", event=event) + projection.apply(event) state = SharedState({}, emit) await state.emit( @@ -29,7 +27,7 @@ async def emit(event): patch=[{"op": "replace", "path": "/done", "value": 2}], ) ) - previous = deepcopy(await thread.history()) + previous = deepcopy(projection.messages) with pytest.raises(JsonPatchConflict): await state.emit( ActivityDeltaEvent( @@ -38,7 +36,7 @@ async def emit(event): patch=[{"op": "remove", "path": "/does-not-exist"}], ) ) - assert await thread.history() == previous + assert projection.messages == previous async def test_concurrent_state_patches_are_serialized(): diff --git a/packages/bub-ag-ui/tests/test_execution.py b/packages/bub-ag-ui/tests/test_execution.py index 80004ea..0b05864 100644 --- a/packages/bub-ag-ui/tests/test_execution.py +++ b/packages/bub-ag-ui/tests/test_execution.py @@ -51,8 +51,6 @@ def render_outbound(self, model_output): "hook-result" ] assert saved == rendered == ["done"] - await collect(adapter, request()) - assert saved == rendered == ["done"] @pytest.mark.parametrize("failure", ["save", "render", "dispatch"]) @@ -310,81 +308,12 @@ async def source(): run="r2", ), ) - # Display history is a protocol concern, not a request to supplement the - # provider's model storage. The provider chose to emit without recording. - assert [ - message["role"] - for message in provider.requests[-1]["messages"] - if message["role"] != "system" - ] == ["user", "user"] - - -@pytest.mark.parametrize("failure", ["error", "cancel"]) -async def test_failed_custom_stream_preserves_provider_owned_context(setup, failure): - provider, make = setup - adapter = make() - provider.reply("recorded answer") - thread, _ = await collect(adapter, request(run="completed")) - entered = asyncio.Event() - - class CustomProvider: - @hookimpl(tryfirst=True) - def run_model_stream(self): - async def source(): - yield StreamEvent("text", {"delta": "display-only partial"}) - yield StreamEvent( - "tool_call", - {"tool_calls": [{"id": "c1", "name": "lookup", "arguments": "{}"}]}, - ) - entered.set() - if failure == "cancel": - await asyncio.Event().wait() - raise RuntimeError("custom provider failed") - - return AsyncStreamEvents(source()) - - adapter.framework.plugin_manager.register(CustomProvider(), "custom") - data = request( - [ - *(await thread.history())["messages"], - {"id": "u2", "role": "user", "content": "Run custom provider"}, - ] - ) - submission = await adapter.submit("alice", data) - await asyncio.wait_for(entered.wait(), 5) - if failure == "cancel": - assert await adapter.cancel("alice", "t1", "r1") - events = [r["event"] async for r in adapter.events(submission.thread, "r1")] - assert events[-1]["type"] == "RUN_ERROR" - assert sum(e["type"] in {"RUN_ERROR", "RUN_FINISHED"} for e in events) == 1 - assert any(e.get("delta") == "display-only partial" for e in events) - assert not any(e["type"] == "TOOL_CALL_RESULT" for e in events) - _, replay = await collect(adapter, data) - assert replay == events - adapter.framework.plugin_manager.unregister(name="custom") - - provider.reply("next answer") - _, next_events = await collect( - make(), - request( - [ - *(await thread.history())["messages"], - {"id": "u3", "role": "user", "content": "Continue"}, - ], - run="r2", - ), + # A subsequent request explicitly supplies these messages. The adapter must + # use that context, not substitute its own persisted display history. + assert any( + m.get("content") == "custom answer" for m in provider.requests[-1]["messages"] ) - assert next_events[-1]["type"] == "RUN_FINISHED" - model_messages = provider.requests[-1]["messages"] - assert [m["content"] for m in model_messages if m["role"] == "assistant"] == [ - "recorded answer" - ] - assert not any(m.get("tool_calls") or m["role"] == "tool" for m in model_messages) - assert [m["content"] for m in model_messages if m["role"] == "user"] == [ - "Hello", - "Run custom provider", - "Continue", - ] + assert any(m.get("tool_call_id") == "c1" for m in provider.requests[-1]["messages"]) @pytest.mark.parametrize("frontend", [False, True]) diff --git a/packages/bub-ag-ui/tests/test_follow_performance.py b/packages/bub-ag-ui/tests/test_follow_performance.py deleted file mode 100644 index b10445f..0000000 --- a/packages/bub-ag-ui/tests/test_follow_performance.py +++ /dev/null @@ -1,74 +0,0 @@ -"""Subscribers need durable run events, not a projection of the conversation.""" - -import time -from types import SimpleNamespace - -import pytest -from bub_ag_ui.events import Projection, wire_event - - -@pytest.mark.parametrize("deltas", [400, 800, 1600]) -async def test_follow_does_not_reproject_history(setup, monkeypatch, deltas): - _, make = setup - adapter = make() - thread = await adapter.thread("alice", "t1") - await thread.append("admit", runId="r1", fingerprint="test") - adapter._runs[thread.tape.name] = SimpleNamespace(run_id="r1") - projections = 0 - reads = 0 - apply = Projection.apply - fetch = thread.tape.store.fetch_all - - def counted_apply(self, event): - nonlocal projections - projections += 1 - return apply(self, event) - - async def counted_fetch(query): - nonlocal reads - reads += 1 - return await fetch(query) - - monkeypatch.setattr(Projection, "apply", counted_apply) - monkeypatch.setattr(thread.tape.store, "fetch_all", counted_fetch) - follower = adapter.events(thread, "r1") - started = time.perf_counter() - events = [ - wire_event("RUN_STARTED", threadId="t1", runId="r1"), - wire_event("TEXT_MESSAGE_START", messageId="m1", role="assistant"), - *[ - wire_event("TEXT_MESSAGE_CONTENT", messageId="m1", delta="x") - for _ in range(deltas) - ], - wire_event("TEXT_MESSAGE_END", messageId="m1"), - wire_event("RUN_FINISHED", threadId="t1", runId="r1"), - ] - try: - for event in events: - await thread.append("event", runId="r1", event=event) - assert (await anext(follower))["event"] == event - with pytest.raises(StopAsyncIteration): - await anext(follower) - finally: - await follower.aclose() - adapter._runs.pop(thread.tape.name) - print(f"{deltas=}: {time.perf_counter() - started:.3f}s {reads=} {projections=}") - assert projections == 0 - assert reads == len(events) - - -@pytest.mark.parametrize("after", [0, 10000]) -async def test_completed_run_replay_does_not_wait_for_another_run(setup, after): - _, make = setup - adapter = make() - thread = await adapter.thread("alice", "t1") - await thread.append("admit", runId="r1", fingerprint="test") - terminal = wire_event("RUN_FINISHED", threadId="t1", runId="r1") - await thread.append("event", runId="r1", event=terminal) - await thread.append("admit", runId="r2", fingerprint="other") - adapter._runs[thread.tape.name] = SimpleNamespace(run_id="r2") - try: - records = [r async for r in adapter.events(thread, "r1", after)] - finally: - adapter._runs.pop(thread.tape.name) - assert [r["event"] for r in records] == ([] if after else [terminal]) diff --git a/packages/bub-ag-ui/tests/test_http.py b/packages/bub-ag-ui/tests/test_http.py index 807e2dc..2a7c501 100644 --- a/packages/bub-ag-ui/tests/test_http.py +++ b/packages/bub-ag-ui/tests/test_http.py @@ -4,8 +4,9 @@ from bub_ag_ui.config import AGUISettings from bub_ag_ui.http import create_app from bub_ag_ui.plugin import AGUIPlugin +from bub_ag_ui.tools import InterruptTool -from .conftest import request +from .conftest import collect, request @asynccontextmanager @@ -22,11 +23,10 @@ async def client_for(adapter, **kwargs): yield client -async def test_http_sse_history_replay_and_cors(setup): +async def test_http_sse_and_cors_without_management_routes(setup): provider, make = setup provider.reply("你好") - adapter = make() - async with client_for(adapter, settings=AGUISettings()) as client: + async with client_for(make(), settings=AGUISettings()) as client: response = await client.post( "/agent", json=request().model_dump(mode="json", by_alias=True), @@ -39,16 +39,10 @@ async def test_http_sse_history_replay_and_cors(setup): ) assert '"type":"RUN_FINISHED"' in response.text assert "你好" in response.text - history = (await client.get("/threads/t1/history")).json() - assert history["messages"][-1]["content"] == "你好" - replay = await client.get("/threads/t1/runs/r1/events") - assert replay.text == response.text - cursor = int(response.text.split("id: ")[-1].splitlines()[0]) - tail = await client.get( - "/threads/t1/runs/r1/events", headers={"Last-Event-ID": str(cursor)} - ) - assert tail.text == "" - assert len(provider.requests) == 1 + assert not response.text.startswith("id:") + for path in ("/threads/t1/history", "/threads/t1/runs/r1/events"): + assert (await client.get(path)).status_code == 404 + assert (await client.post("/threads/t1/runs/r1/cancel")).status_code == 404 async def test_http_auth_validation_and_limits(setup): @@ -62,27 +56,46 @@ async def test_http_auth_validation_and_limits(setup): assert ( await client.post("/agent", content=b"x" * 1025, headers=auth) ).status_code == 413 - assert ( - await client.get("/threads/t/runs/missing/events", headers=auth) - ).status_code == 404 -async def test_custom_auth_scopes_history(setup): +async def test_invalid_resume_is_an_agui_error_not_http_admission_error(setup): + _, make = setup + async with client_for(make()) as client: + data = request( + resume=[{"interruptId": "missing", "status": "resolved", "payload": True}] + ) + response = await client.post( + "/agent", json=data.model_dump(mode="json", by_alias=True) + ) + assert response.status_code == 200 + assert '"type":"RUN_ERROR"' in response.text + assert '"type":"RUN_FINISHED"' not in response.text + + +async def test_custom_auth_isolates_continuation_checkpoints(setup): provider, make = setup - provider.reply("alice response") + adapter = make( + interrupts=(InterruptTool("approve", "Approval", {"type": "object"}),) + ) + provider.reply(calls=[("a1", "approve", "{}")]) + view, _ = await collect(adapter, request(), user="alice") + data = request( + (await view.history())["messages"], + run="resume", + resume=[{"interruptId": "a1", "status": "resolved", "payload": True}], + ) async def authenticate(request): return request.headers.get("x-user", "") - async with client_for(make(), authenticate=authenticate) as client: - body = request().model_dump(mode="json", by_alias=True) - assert ( - await client.post("/agent", json=body, headers={"x-user": "alice"}) - ).status_code == 200 - assert ( - await client.get("/threads/t1/history", headers={"x-user": "bob"}) - ).json()["messages"] == [] - assert (await client.get("/threads/t1/history")).status_code == 401 + async with client_for(adapter, authenticate=authenticate) as client: + body = data.model_dump(mode="json", by_alias=True) + response = await client.post("/agent", json=body, headers={"x-user": "bob"}) + assert '"type":"RUN_ERROR"' in response.text + assert (await client.post("/agent", json=body)).status_code == 401 + provider.reply("approved") + response = await client.post("/agent", json=body, headers={"x-user": "alice"}) + assert '"type":"RUN_FINISHED"' in response.text async def test_plugin_registers_cli_and_channel(setup): diff --git a/packages/bub-ag-ui/tests/test_runtime.py b/packages/bub-ag-ui/tests/test_runtime.py index c668d9c..7648c58 100644 --- a/packages/bub-ag-ui/tests/test_runtime.py +++ b/packages/bub-ag-ui/tests/test_runtime.py @@ -1,41 +1,15 @@ import asyncio +from contextlib import aclosing from copy import deepcopy import pytest from bub.core.tools import Tool -from bub_ag_ui.runtime import ProtocolError from bub_ag_ui.state import shared_state from bub_ag_ui.tools import InterruptTool from .conftest import collect, request -async def test_text_history_handoff_and_retry(setup): - provider, make = setup - provider.reply("Hello world") - adapter = make() - data = request() - thread, events = await collect(adapter, data) - assert events[-1]["type"] == "RUN_FINISHED" - assert [e["delta"] for e in events if e["type"] == "TEXT_MESSAGE_CONTENT"] == [ - "Hello world" - ] - assert len([e for e in events if e["type"] == "RUN_FINISHED"]) == 1 - _, replay = await collect(adapter, data) - assert events == replay - assert len(provider.requests) == 1 - first_messages = deepcopy((await thread.history())["messages"]) - provider.reply("second") - next_messages = first_messages + [{"id": "u2", "role": "user", "content": "Again"}] - await collect(adapter, request(next_messages, run="r2")) - assert [ - m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "user" - ] == ["Hello", "Again"] - await thread.tape.handoff(name="summary", state={"summary": "short"}) - assert (await thread.history())["messages"][: len(first_messages)] == first_messages - assert await make().history("alice", "t1") == await adapter.history("alice", "t1") - - async def test_backend_tool_multiple_steps_and_errors(setup): provider, make = setup calls = [] @@ -87,8 +61,6 @@ async def test_frontend_mixed_tools_resume_across_restart(setup, frontend_first) ) provider.reply("All done") history = deepcopy((await thread.history())["messages"]) - # An echoed result is not a request to overwrite the server's stored result. - next(m for m in history if m["role"] == "tool")["content"] = "changed echo" history.append( {"id": "fr", "role": "tool", "toolCallId": "f1", "content": "browser result"} ) @@ -105,569 +77,287 @@ async def test_frontend_mixed_tools_resume_across_restart(setup, frontend_first) ] -async def test_partial_frontend_results_do_not_run_model(setup): +async def test_request_messages_are_the_context_not_server_history(setup): provider, make = setup - tools = [{"name": "browser", "description": "", "parameters": {"type": "object"}}] - provider.reply(calls=[("f1", "browser", "{}"), ("f2", "browser", "{}")]) adapter = make() - thread, _ = await collect(adapter, request(tools=tools)) - result = {"id": "fr1", "role": "tool", "toolCallId": "f1", "content": "one"} - thread, events = await collect( - adapter, - request([*(await thread.history())["messages"], result], run="r2", tools=tools), - ) - assert events[-1]["type"] == "RUN_FINISHED" - assert len(provider.requests) == 1 - assert [c["id"] for c in (await thread.history())["pending"]] == ["f2"] - - -async def test_interrupt_resume_validates_schema(setup): - provider, make = setup - approval = InterruptTool( - "approve", "Ask permission", {"type": "object"}, {"type": "boolean"} - ) - provider.reply(calls=[("approval-1", "approve", "{}")]) - adapter = make(interrupts=(approval,)) - thread, events = await collect(adapter, request()) - assert events[-1]["outcome"]["type"] == "interrupt" - with pytest.raises(ProtocolError, match="responseSchema"): - await adapter.submit( - "alice", - request( - (await thread.history())["messages"], - run="r2", - resume=[ - { - "interruptId": "approval-1", - "status": "resolved", - "payload": "wrong", - } - ], - ), - ) - provider.reply("Approved") - _, events = await collect( - adapter, - request( - (await thread.history())["messages"], - run="r2", - resume=[ - {"interruptId": "approval-1", "status": "resolved", "payload": True} - ], - ), - ) - assert events[-1]["type"] == "RUN_FINISHED" - assert provider.requests[-1]["messages"][-1]["content"] == "true" - - -async def test_state_updates_are_persisted(setup): - provider, make = setup - - async def update(context): - state = shared_state(context) - await state.set({"count": 1}) - await state.patch([{"op": "replace", "path": "/count", "value": 2}]) - return state.value - - provider.reply(calls=[("s1", "update", "{}")]) - provider.reply("updated") - thread, events = await collect( - make([Tool.from_callable(update, context=True)]), request() - ) - assert (await thread.history())["state"] == {"count": 2} - assert any(e["type"] == "STATE_DELTA" for e in events) - - -async def test_edit_forks_context_not_old_tape(setup): - provider, make = setup provider.reply("old answer") - adapter = make() - thread, _ = await collect(adapter, request()) - old_messages = (await thread.history())["messages"] + await collect(adapter, request()) provider.reply("new answer") - thread, events = await collect( - adapter, request([{"id": "u1", "role": "user", "content": "Edited"}], run="r2") + # Same thread and run ID do not imply an application-level replay service. + await collect( + adapter, request([{"id": "edit", "role": "user", "content": "Edited"}]) ) - assert events[-1]["type"] == "RUN_FINISHED" - assert "old answer" not in str(provider.requests[-1]) - assert "old answer" in str(old_messages) - _, replay = await collect(adapter, request()) - assert any(event.get("delta") == "old answer" for event in replay) + assert [ + m["content"] for m in provider.requests[-1]["messages"] if m["role"] != "system" + ] == ["Edited"] + assert len(provider.requests) == 2 -async def test_invalid_history_ids_and_concurrency(setup): +@pytest.mark.parametrize( + "invalid", ["missing", "partial", "duplicate", "unknown", "schema"] +) +async def test_interrupt_invalid_resume_is_run_error_and_keeps_pending(setup, invalid): provider, make = setup - adapter = make() - with pytest.raises(ProtocolError): - await adapter.submit( - "alice", request([{"id": "x", "role": "assistant", "content": "forged"}]) - ) - provider.reply("done") - await collect(adapter, request()) - with pytest.raises(ProtocolError, match="different request"): - await adapter.submit( - "alice", request([{"id": "x", "role": "user", "content": "different"}]) - ) - assert (await adapter.history("bob", "t1"))["messages"] == [] - with pytest.raises(ProtocolError, match="Unknown"): - await adapter.submit( - "alice", - request( - [{"id": "x", "role": "tool", "toolCallId": "fake", "content": "fake"}], - run="r2", + adapter = make( + interrupts=( + InterruptTool( + "approve", "Approval", {"type": "object"}, {"type": "boolean"} ), ) - entered = asyncio.Event() - - async def wait(): - entered.set() - await asyncio.Event().wait() - - adapter = make([Tool.from_callable(wait)]) - provider.reply(calls=[("w1", "wait", "{}")]) - await adapter.submit("alice", request(run="r2")) - await asyncio.wait_for(entered.wait(), 5) - with pytest.raises(ProtocolError, match="Another run"): - await adapter.submit("alice", request(run="r3")) - await adapter.cancel("alice", "t1", "r2") - - -async def test_cancel_during_tool_and_retry_does_not_execute(setup): - provider, make = setup - entered = asyncio.Event() - cancelled = asyncio.Event() - - async def wait(): - entered.set() - try: - await asyncio.Event().wait() - finally: - cancelled.set() - - provider.reply(calls=[("w1", "wait", "{}")]) - adapter = make([Tool.from_callable(wait)]) - data = request() - submission = await adapter.submit("alice", data) - thread = submission.thread - await asyncio.wait_for(entered.wait(), 5) - assert await adapter.cancel("alice", "t1", "r1") - assert cancelled.is_set() - events = [r["event"] async for r in adapter.events(thread, "r1")] - assert events[-1]["type"] == "RUN_ERROR" - assert events[-1]["code"] == "CANCELLED" - _, replay = await collect(adapter, data) - assert replay == events + ) + provider.reply(calls=[("a1", "approve", "{}"), ("a2", "approve", "{}")]) + view, events = await collect(adapter, request()) + assert events[-1]["outcome"]["type"] == "interrupt" + messages = (await view.history())["messages"] + resume = [ + {"interruptId": id, "status": "resolved", "payload": True} + for id in ("a1", "a2") + ] + if invalid == "missing": + resume = [] + elif invalid == "partial": + resume.pop() + elif invalid == "duplicate": + resume.append(resume[0]) + elif invalid == "unknown": + resume[0]["interruptId"] = "unknown" + else: + resume[0]["payload"] = "not a boolean" + view, failed = await collect(adapter, request(messages, run="bad", resume=resume)) + assert failed[0]["type"] == "RUN_STARTED" + assert failed[-1]["type"] == "RUN_ERROR" + assert not any(e["type"] == "RUN_FINISHED" for e in failed) + assert len(view.pending) == 2 assert len(provider.requests) == 1 -async def test_failure_never_emits_success(setup): +async def test_interrupt_snapshots_resume_and_idempotency_without_history_api(setup): provider, make = setup - adapter = make() - # Empty fake response queue raises in the model transport. - _, events = await collect(adapter, request()) - assert events[-1]["type"] == "RUN_ERROR" - assert not any(e["type"] == "RUN_FINISHED" for e in events) - provider.reply("recovered") - _, next_events = await collect(adapter, request(run="r2")) - assert next_events[-1]["type"] == "RUN_FINISHED" + def update(context): + return shared_state(context).set({"decision": "waiting"}) -async def test_restart_marks_unfinished_run_failed_without_executing(setup): - from bub_ag_ui.events import wire_event - from bub_ag_ui.runtime import fingerprint + # Use an async handler so native execution awaits the state change. + async def prepare(context): + await update(context) + return "prepared" - provider, make = setup - adapter = make() - provider.reply("recorded answer") - thread, _ = await collect(adapter, request(run="completed")) - data = request() - await thread.append("admit", runId="r1", fingerprint=fingerprint(data)) - await thread.append( - "event", runId="r1", event=wire_event("RUN_STARTED", threadId="t1", runId="r1") - ) - await thread.append( - "event", - runId="r1", - event=wire_event("TEXT_MESSAGE_START", messageId="partial", role="assistant"), - ) - await thread.append( - "event", - runId="r1", - event=wire_event("TEXT_MESSAGE_CONTENT", messageId="partial", delta="partial"), - ) - restored = make() - recovered, events = await collect(restored, data) - assert events[-1]["type"] == "RUN_ERROR" - assert events[-1]["code"] == "INTERRUPTED" - assert events[-2]["type"] == "TEXT_MESSAGE_END" - assert (await recovered.history())["messages"][-1]["content"] == "partial" - _, replay = await collect(make(), data) - assert replay == events - assert len(provider.requests) == 1 - provider.reply("next answer") - await collect( - restored, - request( - [ - *(await recovered.history())["messages"], - {"id": "u2", "role": "user", "content": "Continue"}, - ], - run="r2", - ), + approval = InterruptTool( + "approve", "Approval", {"type": "object"}, {"type": "boolean"} ) - # Restart retains provider-owned history, not its display-only partial output. + adapter = make([Tool.from_callable(prepare, context=True)], interrupts=(approval,)) + provider.reply(calls=[("p1", "prepare", "{}")]) + provider.reply(calls=[("a1", "approve", "{}"), ("a2", "approve", "{}")]) + view, events = await collect(adapter, request()) + assert [e["type"] for e in events[-3:]] == [ + "MESSAGES_SNAPSHOT", + "STATE_SNAPSHOT", + "RUN_FINISHED", + ] + assert events[-2]["snapshot"] == {"decision": "waiting"} + assert len(events[-1]["outcome"]["interrupts"]) == 2 + # A fresh adapter can correlate responses using native tape checkpoints. + adapter = make(interrupts=(approval,)) + resume = [ + {"interruptId": id, "status": "resolved", "payload": True} + for id in ("a1", "a2") + ] + data = request((await view.history())["messages"], run="resume", resume=resume) + data.state = events[-2]["snapshot"] + provider.reply("approved") + _, resumed = await collect(adapter, data) + assert resumed[-1]["type"] == "RUN_FINISHED" assert [ - m["content"] - for m in provider.requests[-1]["messages"] - if m["role"] == "assistant" - ] == ["recorded answer"] - - -async def test_recoverable_backend_error_and_command_text(setup): - provider, make = setup - - def fail(): - raise ValueError("tool failed") - - provider.reply(calls=[("c1", "fail", "{}")]) - provider.reply("Recovered") - data = request([{"id": "u1", "role": "user", "content": ",bash echo unsafe"}]) - _, events = await collect(make([Tool.from_callable(fail)]), data) - assert events[-1]["type"] == "RUN_FINISHED" - assert "tool failed" in next( - e["content"] for e in events if e["type"] == "TOOL_CALL_RESULT" - ) - assert any( - m["content"] == ",bash echo unsafe" for m in provider.requests[0]["messages"] - ) - - -async def test_resolve_interrupt_and_steer_away_in_one_request(setup): + m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "tool" + ] == ["prepared", "true", "true"] + count = len(provider.requests) + retry = data.model_copy(update={"run_id": "different-id"}) + _, replayed = await collect(adapter, retry) + assert replayed[-1]["type"] == "RUN_FINISHED" + assert len(provider.requests) == count + retry.resume[0].payload = False + _, rejected = await collect(adapter, retry) + assert rejected[-1]["type"] == "RUN_ERROR" + assert len(provider.requests) == count + + +async def test_cancelled_interrupt_and_new_input_in_one_request(setup): provider, make = setup - provider.reply(calls=[("a1", "approve", "{}")]) adapter = make( interrupts=(InterruptTool("approve", "Approval", {"type": "object"}),) ) - thread, _ = await collect(adapter, request()) - provider.reply("Changed topic") - new_user = {"id": "u2", "role": "user", "content": "Never mind"} + provider.reply(calls=[("a1", "approve", "{}")]) + view, _ = await collect(adapter, request()) + provider.reply("another plan") _, events = await collect( adapter, request( - [*(await thread.history())["messages"], new_user], + [ + *(await view.history())["messages"], + {"id": "u2", "role": "user", "content": "Choose another plan"}, + ], run="r2", resume=[{"interruptId": "a1", "status": "cancelled"}], ), ) assert events[-1]["type"] == "RUN_FINISHED" - assert [m["role"] for m in provider.requests[-1]["messages"]][-2:] == [ - "tool", - "user", - ] + assert provider.requests[-1]["messages"][-2]["role"] == "tool" + assert provider.requests[-1]["messages"][-1]["content"] == "Choose another plan" -async def test_mixed_step_cancellation_does_not_invent_model_tool_results(setup): +async def test_frontend_requires_matching_complete_results(setup): provider, make = setup - entered = asyncio.Event() - - async def slow(): - entered.set() - await asyncio.Event().wait() - - provider.reply(calls=[("f1", "browser", "{}"), ("b1", "slow", "{}")]) - adapter = make([Tool.from_callable(slow)]) tools = [{"name": "browser", "description": "", "parameters": {"type": "object"}}] - submission = await adapter.submit("alice", request(tools=tools)) - thread = submission.thread - await asyncio.wait_for(entered.wait(), 5) - await adapter.cancel("alice", "t1", "r1") - assert (await thread.history())["pending"] == [] - provider.reply("Recovered") + provider.reply(calls=[("f1", "browser", "{}"), ("f2", "browser", "{}")]) + adapter = make() + view, _ = await collect(adapter, request(tools=tools)) + history = (await view.history())["messages"] + result = {"id": "fr1", "role": "tool", "toolCallId": "f1", "content": "one"} + _, events = await collect( + adapter, request([*history, result], run="partial", tools=tools) + ) + assert events[-1]["type"] == "RUN_ERROR" + assert len(provider.requests) == 1 + provider.reply("both results") _, events = await collect( adapter, request( - [ - *(await thread.history())["messages"], - {"id": "u2", "role": "user", "content": "Continue"}, - ], - run="r2", + [*history, result, {**result, "id": "fr2", "toolCallId": "f2"}], + run="full", tools=tools, ), ) assert events[-1]["type"] == "RUN_FINISHED" - model_messages = provider.requests[-1]["messages"] - # The interrupted native exchange was not recorded. Displayed calls must - # not become fabricated model exchanges or successful tool results. - assert [m["role"] for m in model_messages if m["role"] != "system"] == [ - "user", - "user", - ] -@pytest.mark.parametrize("client_name", ["server_tool", "server.tool"]) -async def test_schema_shadow_and_forged_state_are_not_execution_overrides( - setup, client_name -): +async def test_disconnect_closes_native_tool_and_releases_thread(setup): provider, make = setup - adapter = make( - [Tool.from_callable(lambda: "value", name="server.tool", deferred=True)] - ) - with pytest.raises(ProtocolError, match="shadows"): - await adapter.submit( - "alice", - request(tools=[{"name": client_name, "description": "", "parameters": {}}]), - ) - with pytest.raises(ProtocolError, match="schema"): - await adapter.submit( - "alice", - request( - tools=[ - { - "name": "client", - "description": "", - "parameters": {"type": "not-a-type"}, - } - ] - ), - ) - provider.reply("safe") - data = request() - data.state = { - "_runtime_workspace": "/", - "model": "evil:model", - "session_id": "someone-else", - } - thread, events = await collect(adapter, data) - assert events[-1]["type"] == "RUN_FINISHED" - assert provider.requests[-1]["model"] == "test" - assert (await thread.history())["state"] == data.state + entered, closed = asyncio.Event(), asyncio.Event() + async def wait(): + entered.set() + try: + await asyncio.Event().wait() + finally: + closed.set() -async def test_image_data_is_converted_to_model_content(setup): - provider, make = setup - provider.reply("image") - adapter = make() - content = [ - {"type": "text", "text": "Look"}, - { - "type": "image", - "source": {"type": "data", "value": "aGVsbG8=", "mimeType": "image/png"}, - }, - ] - _, events = await collect( - adapter, request([{"id": "u1", "role": "user", "content": content}]) - ) + provider.reply(calls=[("wait", "wait", "{}")]) + adapter = make([Tool.from_callable(wait)]) + source = adapter.run("alice", request()) + async with aclosing(source): + async for event in source: + if event["type"] == "TOOL_CALL_END": + await asyncio.wait_for(entered.wait(), 5) + break + await asyncio.wait_for(closed.wait(), 5) + assert not adapter._active + assert not adapter._tasks + provider.reply("next") + _, events = await collect(adapter, request(run="next")) assert events[-1]["type"] == "RUN_FINISHED" - user = next(m for m in provider.requests[0]["messages"] if m["role"] == "user") - assert user["content"][1]["type"] == "image_url" - assert "aGVsbG8=" in user["content"][1]["image_url"]["url"] -async def test_earlier_edit_cannot_be_silently_ignored(setup): +async def test_failed_stream_has_one_terminal_error_and_no_hidden_next_context(setup): + from bub import hookimpl + from bub.streaming import AsyncStreamEvents, StreamEvent + provider, make = setup - provider.reply("first") adapter = make() - thread, _ = await collect(adapter, request()) - provider.reply("second") + + class Failing: + @hookimpl(tryfirst=True) + def run_model_stream(self): + async def source(): + yield StreamEvent("text", {"delta": "partial"}) + raise RuntimeError("failed provider") + + return AsyncStreamEvents(source()) + + adapter.framework.plugin_manager.register(Failing(), "failing") + _, events = await collect(adapter, request()) + assert events[-1]["type"] == "RUN_ERROR" + assert sum(e["type"] in {"RUN_ERROR", "RUN_FINISHED"} for e in events) == 1 + adapter.framework.plugin_manager.unregister(name="failing") + provider.reply("fresh answer") await collect( adapter, request( - [ - *(await thread.history())["messages"], - {"id": "u2", "role": "user", "content": "second"}, - ], - run="r2", + [{"id": "next", "role": "user", "content": "Fresh context"}], run="next" ), ) - history = deepcopy((await thread.history())["messages"][:-1]) - history[0]["content"] = "silently ignored edit" - with pytest.raises(ProtocolError, match="final user"): - await adapter.submit("alice", request(history, run="r3")) - assert "r3" not in (await thread.history())["runs"] + assert all(m.get("content") != "partial" for m in provider.requests[-1]["messages"]) -@pytest.mark.parametrize("failure", ["merge", "terminal"]) -async def test_storage_failure_cannot_report_success(setup, monkeypatch, failure): +async def test_busy_request_cannot_take_over_or_cancel_owner(setup): provider, make = setup - provider.reply("generated") - adapter = make() - thread = await adapter.thread("alice", "t1") - store = thread.tape.store - original = store.append - failed = False - - async def append(name, entry): - nonlocal failed - if failure == "merge": - trigger = ( - entry.kind == "message" and entry.payload.get("role") == "assistant" - ) - else: - trigger = ( - entry.kind == "event" - and entry.payload.get("data", {}).get("event", {}).get("type") - == "RUN_FINISHED" - ) - if trigger and not failed: - failed = True - raise OSError("simulated storage failure") - await original(name, entry) - - monkeypatch.setattr(store, "append", append) - thread, events = await collect(adapter, request()) - assert failed - assert events[-1]["type"] == "RUN_ERROR" - assert not any(event["type"] == "RUN_FINISHED" for event in events) - - -async def test_cancellation_before_first_model_token_releases_thread(setup): - provider, make = setup - adapter = make() - submission = await adapter.submit("alice", request()) - thread = submission.thread - await adapter.cancel("alice", "t1", "r1") - events = [r["event"] async for r in adapter.events(thread, "r1")] - assert events[-1]["type"] == "RUN_ERROR" - provider.reply("after cancellation") - _, next_events = await collect(adapter, request(run="r2")) - assert next_events[-1]["type"] == "RUN_FINISHED" - - -async def test_concurrent_retries_share_execution_and_replay_is_read_only(setup): - provider, make = setup - entered = asyncio.Event() - release = asyncio.Event() - calls = [] + entered, release = asyncio.Event(), asyncio.Event() async def wait(): - calls.append("executed") entered.set() await release.wait() - return "ready" + return "released" - provider.reply(calls=[("w1", "wait", "{}")]) - provider.reply("finished once") adapter = make([Tool.from_callable(wait)]) - submissions = await asyncio.gather( - *(adapter.submit("alice", request()) for _ in range(8)) - ) - await asyncio.wait_for(entered.wait(), 5) - # Only the admitting HTTP response may cancel its producer on disconnect. - assert sum(submission.created for submission in submissions) == 1 - with pytest.raises(ProtocolError, match="Another run"): - await adapter.submit("alice", request(run="r2")) - - thread = submissions[0].thread - observer = adapter.events(thread, "r1") - first = await anext(observer) - assert first["event"]["type"] == "RUN_STARTED" - await observer.aclose() - # Closing a replay subscription does not affect the live producer. - release.set() - records = [record async for record in adapter.events(thread, "r1")] - assert records[-1]["event"]["type"] == "RUN_FINISHED" - assert calls == ["executed"] - assert len(provider.requests) == 2 - cursor = records[len(records) // 2]["sequence"] - tail = [record async for record in adapter.events(thread, "r1", cursor)] - assert tail == [record for record in records if record["sequence"] > cursor] - assert len(provider.requests) == 2 - provider.reply("next run") - _, events = await collect(adapter, request(run="r2")) - assert events[-1]["type"] == "RUN_FINISHED" - - -@pytest.mark.parametrize("reset", ["owner", "sidecar", "fork"]) -async def test_mounted_history_follows_tape_reset_and_accepts_fresh_runs(setup, reset): + provider.reply(calls=[("w", "wait", "{}")]) + provider.reply("done") + owner = asyncio.create_task(collect(adapter, request())) + try: + await asyncio.wait_for(entered.wait(), 5) + _, events = await collect(adapter, request(run="other")) + assert events[-1]["type"] == "RUN_ERROR" + assert "Another request" in events[-1]["message"] + assert not owner.done() + release.set() + _, events = await asyncio.wait_for(owner, 5) + assert events[-1]["type"] == "RUN_FINISHED" + finally: + release.set() + await owner + + +async def test_timeout_closes_native_execution_with_one_error(setup): provider, make = setup - provider.reply("before reset") - adapter = make() - thread, _ = await collect(adapter, request()) - before = await adapter.history("alice", "t1") - assert "before reset" in str(before["messages"]) - if reset == "owner": - await thread.tape.reset(keep_sidecars=("ag-ui",)) - assert await adapter.history("alice", "t1") == before - await thread.tape.reset() - elif reset == "sidecar": - await thread.tape.reset_sidecar("ag-ui") - else: - async with thread.tape.fork_tape() as fork: - await fork.reset() - assert await adapter.history("alice", "t1") == before - assert (await adapter.history("alice", "t1"))["messages"] == [] - with pytest.raises(ProtocolError, match="reset"): - await anext(adapter.events(thread, "r1", before["cursor"])) - provider.reply("after reset") - _, events = await collect(adapter, request()) - assert events[-1]["type"] == "RUN_FINISHED" - assert (await adapter.history("alice", "t1"))["messages"][-1][ - "content" - ] == "after reset" + closed = asyncio.Event() + async def wait(): + try: + await asyncio.Event().wait() + finally: + closed.set() -async def test_cancellation_during_admission_settles_write_before_next_run( - setup, monkeypatch -): - provider, make = setup - adapter = make() - thread = await adapter.thread("alice", "t1") - store = thread.tape.store - original = store.append - entered = asyncio.Event() - release = asyncio.Event() - - async def append(name, entry): - if ( - entry.payload.get("name") == "ag_ui.admit" - and entry.payload["data"]["runId"] == "r1" - ): - entered.set() - await release.wait() - await original(name, entry) - - monkeypatch.setattr(store, "append", append) - admission = asyncio.create_task(adapter.submit("alice", request())) - await asyncio.wait_for(entered.wait(), 5) - admission.cancel() - release.set() - with pytest.raises(asyncio.CancelledError): - await admission - replay = [record["event"] async for record in adapter.events(thread, "r1")] - assert replay[-1]["type"] == "RUN_ERROR" - assert replay[-1]["code"] == "CANCELLED" - assert provider.requests == [] - provider.reply("after cancelled admission") - _, events = await collect(adapter, request(run="r2")) - assert events[-1]["type"] == "RUN_FINISHED" + adapter = make([Tool.from_callable(wait)], max_run_seconds=0.05) + provider.reply(calls=[("w", "wait", "{}")]) + _, events = await collect(adapter, request()) + assert closed.is_set() + assert events[-1]["type"] == "RUN_ERROR" + assert events[-1]["message"] == "Agent run timed out" + assert not adapter._active -async def test_cancellation_racing_terminal_commit_has_one_outcome(setup, monkeypatch): +async def test_resume_receipt_distinguishes_json_boolean_and_number(setup): provider, make = setup + adapter = make( + interrupts=(InterruptTool("approve", "Approval", {"type": "object"}),) + ) + provider.reply(calls=[("a1", "approve", "{}")]) + view, _ = await collect(adapter, request()) + messages = (await view.history())["messages"] provider.reply("done") - adapter = make() - thread = await adapter.thread("alice", "t1") - store = thread.tape.store - original = store.append - entered = asyncio.Event() - release = asyncio.Event() - - async def append(name, entry): - if entry.payload.get("data", {}).get("event", {}).get("type") == "RUN_FINISHED": - entered.set() - await release.wait() - await original(name, entry) - - monkeypatch.setattr(store, "append", append) - await adapter.submit("alice", request()) - await asyncio.wait_for(entered.wait(), 5) - cancellation = asyncio.create_task(adapter.cancel("alice", "t1", "r1")) - await asyncio.sleep(0) - release.set() - await cancellation - events = [record["event"] async for record in adapter.events(thread, "r1")] - assert [ - event["type"] - for event in events - if event["type"] in {"RUN_FINISHED", "RUN_ERROR"} - ] == ["RUN_FINISHED"] + await collect( + adapter, + request( + messages, + run="r2", + resume=[{"interruptId": "a1", "status": "resolved", "payload": True}], + ), + ) + _, events = await collect( + adapter, + request( + messages, + run="r3", + resume=[{"interruptId": "a1", "status": "resolved", "payload": 1}], + ), + ) + assert events[-1]["type"] == "RUN_ERROR" + assert len(provider.requests) == 2 diff --git a/packages/bub-ag-ui/tests/test_stream_delivery.py b/packages/bub-ag-ui/tests/test_stream_delivery.py new file mode 100644 index 0000000..c71391f --- /dev/null +++ b/packages/bub-ag-ui/tests/test_stream_delivery.py @@ -0,0 +1,87 @@ +"""Live delivery must never poll a store or grow an unbounded event buffer.""" + +import asyncio +from contextlib import aclosing + +import pytest +from bub import hookimpl +from bub.streaming import AsyncStreamEvents, StreamEvent + +from .conftest import request + + +@pytest.mark.parametrize("storage", ["memory", "sqlite"]) +@pytest.mark.parametrize("deltas", [400, 1600]) +async def test_stream_store_reads_do_not_grow_with_tokens( + setup, monkeypatch, tmp_path, deltas, storage +): + _, make = setup + from bub_tapestore_sqlite import SQLiteTapeStore + + sqlite = ( + SQLiteTapeStore(tmp_path / "stream.sqlite3") if storage == "sqlite" else None + ) + adapter = make(tape_store=sqlite) + reads = 0 + store = adapter._storage_agent.tape("probe").store + fetch = store.fetch_all + + async def counted(query): + nonlocal reads + reads += 1 + return await fetch(query) + + monkeypatch.setattr(store, "fetch_all", counted) + + class Provider: + @hookimpl(tryfirst=True) + def run_model_stream(self): + async def source(): + initial = reads + for _ in range(deltas): + yield StreamEvent("text", {"delta": "x"}) + assert reads == initial + yield StreamEvent("final", {"ok": True}) + + return AsyncStreamEvents(source()) + + adapter.framework.plugin_manager.register(Provider(), "many-tokens") + try: + events = [e async for e in adapter.run("alice", request())] + finally: + if sqlite is not None: + await sqlite.close() + assert events[-1]["type"] == "RUN_FINISHED" + assert sum(e["type"] == "TEXT_MESSAGE_CONTENT" for e in events) == deltas + assert reads < 10 + + +async def test_paused_consumer_has_bounded_delivery_and_can_close(setup): + _, make = setup + adapter = make() + produced = 0 + closed = asyncio.Event() + + class Provider: + @hookimpl(tryfirst=True) + def run_model_stream(self): + async def source(): + nonlocal produced + try: + for _ in range(10000): + produced += 1 + yield StreamEvent("text", {"delta": "x"}) + finally: + closed.set() + + return AsyncStreamEvents(source()) + + adapter.framework.plugin_manager.register(Provider(), "backpressure") + async with aclosing(adapter.run("alice", request())) as events: + async for event in events: + if event["type"] == "TEXT_MESSAGE_CONTENT": + break + await asyncio.sleep(0.05) + assert produced < 40 + await asyncio.wait_for(closed.wait(), 5) + assert not adapter._active diff --git a/packages/bub-ag-ui/tests/test_tools.py b/packages/bub-ag-ui/tests/test_tools.py index d6a5a0c..2afa599 100644 --- a/packages/bub-ag-ui/tests/test_tools.py +++ b/packages/bub-ag-ui/tests/test_tools.py @@ -3,10 +3,9 @@ import pytest from ag_ui.core import Tool as ClientTool from bub.core.tools import Tool, ToolContext -from bub_ag_ui.runtime import ProtocolError from bub_ag_ui.tools import ExternalCallsPending, ExternalTools, InterruptTool -from .conftest import request +from .conftest import collect, request @pytest.mark.parametrize( @@ -22,13 +21,13 @@ async def test_native_tool_name_collisions_are_rejected( ): provider, make = setup adapter = make([Tool.from_callable(lambda: "server", name=server_name)]) - with pytest.raises(ProtocolError, match="shadows"): - await adapter.submit( - "alice", - request(tools=[{"name": client_name, "description": "", "parameters": {}}]), - ) + _, events = await collect( + adapter, + request(tools=[{"name": client_name, "description": "", "parameters": {}}]), + ) + assert events[-1]["type"] == "RUN_ERROR" + assert "shadows" in events[-1]["message"] assert provider.requests == [] - assert (await adapter.history("alice", "t1"))["runs"] == {} @pytest.mark.parametrize("interrupt", [False, True]) @@ -39,8 +38,9 @@ async def test_external_definitions_cannot_share_model_alias(setup, interrupt): tools = [{"name": "web_search", "description": "", "parameters": {}}] if not interrupt: tools.append(definition) - with pytest.raises(ProtocolError, match="shadows"): - await adapter.submit("alice", request(tools=tools)) + _, events = await collect(adapter, request(tools=tools)) + assert events[-1]["type"] == "RUN_ERROR" + assert "shadows" in events[-1]["message"] @pytest.mark.parametrize("interrupt", [False, True]) From 71ebe5f36f5f4bbb2ae00ef645ae0b2d93be633f Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 10 Nov 2026 19:16:16 +0800 Subject: [PATCH 02/10] fix(bub-ag-ui): preserve session state on one working tape --- README.md | 2 +- packages/bub-ag-ui/README.md | 7 + packages/bub-ag-ui/src/bub_ag_ui/execution.py | 8 +- packages/bub-ag-ui/src/bub_ag_ui/runtime.py | 50 ++-- packages/bub-ag-ui/tests/test_session_tape.py | 251 ++++++++++++++++++ 5 files changed, 292 insertions(+), 26 deletions(-) create mode 100644 packages/bub-ag-ui/tests/test_session_tape.py diff --git a/README.md b/README.md index 41bc6d8..a336673 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Below is the list of packages currently included in this repository. | [`bub-context-budget`](./packages/bub-context-budget/README.md) | | Estimates input context tokens before LLM calls and prompts the model to hand off when the configured budget is exceeded. | | [`bub-cursor`](./packages/bub-cursor/README.md) | | Provides a `run_model` hook that delegates model execution to the Cursor CLI, plus `bub login cursor`. | | [`bub-acp-server`](./packages/bub-acp-server/README.md) | [![PyPI version](https://img.shields.io/pypi/v/bub-acp-server)](https://pypi.org/project/bub-acp-server/) | Exposes Bub as an Agent Client Protocol agent with `bub acp serve` for ACP-compatible editors. | -| [`bub-ag-ui`](./packages/bub-ag-ui/README.md) | | Exposes Bub through AG-UI HTTP/SSE with persistent history, replay, and frontend tool continuation. | +| [`bub-ag-ui`](./packages/bub-ag-ui/README.md) | | Exposes Bub through AG-UI HTTP/SSE with frontend tools, shared state, and interrupt/resume. | | [`bub-agent-plugins`](./packages/bub-agent-plugins/README.md) | | Loads portable Agent Plugins 1.0 skills and MCP servers through Bub's existing skill parser and `bub-mcp` runtime. | | [`bub-sandbox-env`](./packages/bub-sandbox-env/README.md) | | Runs `bash`, `fs.*` and code-mode tools in a lightweight local sandbox (macOS Seatbelt, Linux bubblewrap) through `provide_environment`. | | [`bub-monty-env`](./packages/bub-monty-env/README.md) | | Runs code-mode `run_code` in Monty, a sandboxed Python interpreter, through `provide_environment`; also a mixin and wrapper for other environments. | diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index dc9c846..6c04a08 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -109,6 +109,13 @@ interrupt responses. Repeating the same accepted interrupt responses does not re-execute resumed work; it is not a replay of the earlier generated answer. Continuation across a server restart requires a persistent Bub tape store. +Each thread uses one native execution tape. Before execution, the adapter replaces +its model context with the request's messages, retaining native session settings, +loaded-tool visibility, and the continuation checkpoint. It does not archive a +full conversation copy for every run. Mounted sidecars are left intact. This tape +is working context, not an append-only conversation archive; the application +remains responsible for saving the conversation it wants to restore. + ## Errors and cancellation - Protocol execution failures arrive as `RUN_ERROR`, not `RUN_FINISHED`. diff --git a/packages/bub-ag-ui/src/bub_ag_ui/execution.py b/packages/bub-ag-ui/src/bub_ag_ui/execution.py index 23ca3ac..a0f51b7 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/execution.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/execution.py @@ -22,7 +22,6 @@ from bub.builtin.tools import ALLOWED_TOOLS_STATE_KEY from bub.channels.message import ChannelMessage from bub.core.events import RunStarted, StepFinished, StepStarted -from bub.core.tape import session_tape_name from bub.envelope import Envelope, field_of from bub.streaming import AsyncStreamEvents, StreamEvent, StreamState from bub.turn import TurnState @@ -69,12 +68,9 @@ class ExecutionHooks: async def load_state(self, message: Envelope) -> TurnState | None: if not isinstance(message, AGUIInboundMessage): return None - # Native session settings remain thread-scoped; model input comes from - # this request rather than an adapter-owned conversation history. + # Read settings from the same thread tape the native turn writes. settings = await load_session_settings( - message._runtime_agent.tape( - session_tape_name(message.session_id, message.workspace) - ) + message._runtime_agent.tape(message.model_tape) ) return { **settings, diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py index 63c444c..24c7279 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py @@ -10,12 +10,14 @@ from copy import deepcopy from pathlib import Path from typing import Any -from uuid import uuid4 import jsonschema from ag_ui.core import RunAgentInput from bub.builtin.agent import BubAgent +from bub.builtin.settings import load_session_settings, set_session_setting +from bub.builtin.tools import TOOL_LOADED_EVENT, loaded_tool_names from bub.core.tape import TapeEntry, session_tape_name, to_message +from bub.core.tools import ToolContext from bub_ag_ui.continuation import Continuation from bub_ag_ui.events import EventStream, RunProjection, result_text @@ -80,8 +82,8 @@ def model_message(message: dict[str, Any]) -> dict[str, Any] | None: class AGUIAdapter: """Adapt a request and its native execution to an AG-UI event stream. - The client supplies conversation messages/state on every run. Only external - call correlation and interrupt response receipts survive in the native tape. + The client supplies conversation messages/state on every run. Native session + settings and external-call checkpoints remain on the thread's working tape. Streaming does not read back events from storage. """ @@ -186,7 +188,6 @@ async def publish(event: dict[str, Any]) -> None: ) continuation = await Continuation.load(tape) try: - had_pending = bool(continuation.pending) messages, repeated = continuation.prepare(request) agent = self.agent_factory(self.framework, tape_store=tape.store) external = ExternalTools([*request.tools, *self.interrupts]) @@ -212,21 +213,32 @@ async def publish(event: dict[str, Any]) -> None: ) await shared.set(shared.value) if not repeated: - # Each request supplies its context. A fresh native execution - # tape prevents server history from silently overriding or - # duplicating client messages; Bub still records model steps. - model_tape = tape.name + ":run:" + str(uuid4()) - execution = agent.tape(model_tape) - await execution.ensure_bootstrap_anchor() - for message in converted: - await committed( - execution.store.append( - model_tape, TapeEntry.message(message) + settings = await load_session_settings(tape) + loaded = await loaded_tool_names(tape) + + async def replace_context() -> None: + # One thread tape, not one archive per request. Keep + # native session settings/tool visibility and mounted + # sidecars while replacing only the execution context. + await tape.store.reset(tape.name) + await tape.ensure_bootstrap_anchor() + context = ToolContext(tape=tape) + for key, value in settings.items(): + await set_session_setting(context, key, value) + if loaded: + await tape.append_event( + TOOL_LOADED_EVENT, {"names": loaded} ) - ) - if had_pending: - # Record receipt before executing any resumed side effect. - await committed(continuation.save(tape)) + # Persist consumed responses before resumed side effects. + await continuation.save(tape) + for message in converted: + await tape.store.append( + tape.name, TapeEntry.message(message) + ) + + # Cancellation must not leave a still-running rewrite behind + # after this request releases thread ownership. + await committed(replace_context()) inbound = AGUIInboundMessage( session_id=session_id, channel="ag-ui", @@ -243,7 +255,7 @@ async def publish(event: dict[str, Any]) -> None: is_active=True, _runtime_agent=agent, request=request, - model_tape=model_tape, + model_tape=tape.name, workspace=self.workspace, shared=shared, events=stream, diff --git a/packages/bub-ag-ui/tests/test_session_tape.py b/packages/bub-ag-ui/tests/test_session_tape.py new file mode 100644 index 0000000..8a5793c --- /dev/null +++ b/packages/bub-ag-ui/tests/test_session_tape.py @@ -0,0 +1,251 @@ +"""Request context is replaceable; native thread settings are not run-local.""" + +from itertools import pairwise + +import pytest +from bub.builtin.settings import ( + AgentSettings, + load_session_settings, + set_session_setting, +) +from bub.builtin.store import FuzzyFileTapeStore +from bub.core.store import InMemoryTapeStore +from bub.core.tape import TapeQuery, session_tape_name +from bub.core.tools import Tool +from bub_tapestore_sqlite import SQLiteTapeStore + +from .conftest import collect, request + + +@pytest.fixture(params=["memory", "file", "sqlite"]) +async def store(request, tmp_path): + if request.param == "sqlite": + value = SQLiteTapeStore(tmp_path / "tapes.sqlite3") + elif request.param == "file": + value = FuzzyFileTapeStore(tmp_path / "tapes") + else: + value = InMemoryTapeStore() + yield value + if isinstance(value, SQLiteTapeStore): + await value.close() + + +def session_tape(adapter): + return adapter._storage_agent.tape( + session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) + ) + + +async def test_native_setting_switches_persist_and_clear_on_same_thread( + setup, store, monkeypatch +): + provider, make = setup + observed = [] + model = AgentSettings.chat_model + + def chat_model(self, model_name=None, *, reasoning_effort=None): + observed.append((model_name, reasoning_effort)) + return model(self, model_name, reasoning_effort=reasoning_effort) + + monkeypatch.setattr(AgentSettings, "chat_model", chat_model) + + async def switch(context, value): + await set_session_setting(context, "reasoning_effort", value) + return "updated" + + tool = Tool.from_callable(switch, context=True) + adapter = make([tool], tape_store=store) + provider.reply(calls=[("s1", "switch", '{"value":"high"}')]) + provider.reply("first answer") + view, events = await collect(adapter, request()) + assert events[-1]["type"] == "RUN_FINISHED" + assert await load_session_settings(session_tape(adapter)) == { + "reasoning_effort": "high" + } + + # A fresh adapter must restore from native storage, not an in-memory cache. + adapter = make([tool], tape_store=store) + observed.clear() + provider.reply(calls=[("s2", "switch", '{"value":null}')]) + provider.reply("cleared") + view, events = await collect( + adapter, + request( + [ + *(await view.history())["messages"], + {"id": "u2", "role": "user", "content": "Clear setting"}, + ], + run="r2", + ), + ) + assert events[-1]["type"] == "RUN_FINISHED" + assert observed[0] == (None, "high") + assert await load_session_settings(session_tape(adapter)) == {} + + observed.clear() + provider.reply("third answer") + await collect( + adapter, + request( + [ + *(await view.history())["messages"], + {"id": "u3", "role": "user", "content": "Continue"}, + ], + run="r3", + ), + ) + assert observed[0] == (None, None) + + +async def test_one_native_tape_holds_only_current_request_context(setup, store): + provider, make = setup + adapter = make(tape_store=store) + messages = [{"id": "u1", "role": "user", "content": "First"}] + sizes = [] + for turn in range(6): + provider.reply(f"answer {turn}") + view, events = await collect(adapter, request(messages, run=f"r{turn}")) + assert events[-1]["type"] == "RUN_FINISHED" + tape = session_tape(adapter) + names = await tape.store.list_tapes() + assert names == [tape.name] + sizes.append(len(list(await tape.query().all()))) + messages = [ + *(await view.history())["messages"], + {"id": f"u{turn + 2}", "role": "user", "content": f"question {turn + 1}"}, + ] + # Only the two newly supplied messages grow the retained context each turn, + # not a sum of all earlier request snapshots and native step records. + assert [b - a for a, b in pairwise(sizes)] == [2] * 5 + provider.reply("edited answer") + _, events = await collect( + adapter, + request( + [{"id": "edit", "role": "user", "content": "Replacement context"}], + run="edited", + ), + ) + assert events[-1]["type"] == "RUN_FINISHED" + tape = session_tape(adapter) + assert len(list(await tape.query().all())) == sizes[0] + assert [ + m["content"] for m in provider.requests[-1]["messages"] if m["role"] != "system" + ] == ["Replacement context"] + + +async def test_sqlite_reopen_retains_settings_without_orphan_run_tapes(setup, tmp_path): + provider, make = setup + path = tmp_path / "reopen.sqlite3" + store = SQLiteTapeStore(path) + + async def switch(context): + await set_session_setting(context, "code_mode", False) + return "set" + + adapter = make([Tool.from_callable(switch, context=True)], tape_store=store) + provider.reply(calls=[("s", "switch", "{}")]) + provider.reply("done") + await collect(adapter, request()) + name = session_tape(adapter).name + await adapter.close() + await store.close() + store = SQLiteTapeStore(path) + try: + adapter = make(tape_store=store) + assert await store.list_tapes() == [name] + assert await load_session_settings(session_tape(adapter)) == { + "code_mode": False + } + provider.reply("after reopen") + _, events = await collect(adapter, request(run="after-reopen")) + assert events[-1]["type"] == "RUN_FINISHED" + assert await load_session_settings(session_tape(adapter)) == { + "code_mode": False + } + finally: + await store.close() + + +async def test_native_deferred_tools_and_sidecars_survive_context_replacement( + setup, store +): + from bub.builtin.tools import TOOL_LOADED_EVENT, loaded_tool_names + from bub.core.tape import TapeEntry + + provider, make = setup + adapter = make( + [Tool.from_callable(lambda: "loaded", name="lookup", deferred=True)], + tape_store=store, + ) + tape = session_tape(adapter) + await tape.append_event(TOOL_LOADED_EVENT, {"names": ["lookup"]}) + sidecar_name = tape.sidecar_tape_name("spill") + await tape.store.append( + sidecar_name, TapeEntry.event("test.spill", {"handle": "retained"}) + ) + provider.reply("first") + view, _ = await collect(adapter, request()) + provider.reply("second") + await collect( + adapter, + request( + [ + *(await view.history())["messages"], + {"id": "u2", "role": "user", "content": "Continue"}, + ], + run="r2", + ), + ) + for model_request in provider.requests: + assert "lookup" in {t["function"]["name"] for t in model_request["tools"]} + assert await loaded_tool_names(tape) == ["lookup"] + entries = list( + await tape.store.fetch_all(TapeQuery(tape=sidecar_name, store=tape.store)) + ) + assert len(entries) == 1 + assert entries[0].payload["data"]["handle"] == "retained" + + +async def test_cancelled_rewrite_settles_before_releasing_thread( + setup, store, monkeypatch +): + import asyncio + + provider, make = setup + adapter = make(tape_store=store) + tape = session_tape(adapter) + from bub.core.tools import ToolContext + + await set_session_setting(ToolContext(tape=tape), "reasoning_effort", "high") + entered, release = asyncio.Event(), asyncio.Event() + reset = tape.store.reset + pause = True + + async def slow_reset(name): + nonlocal pause + await reset(name) + if name == tape.name and pause: + pause = False + entered.set() + await release.wait() + + monkeypatch.setattr(tape.store, "reset", slow_reset) + task = asyncio.create_task(collect(adapter, request())) + try: + await asyncio.wait_for(entered.wait(), 5) + task.cancel() + await asyncio.sleep(0) + assert not task.done() + _, busy = await collect(adapter, request(run="busy")) + assert busy[-1]["type"] == "RUN_ERROR" + assert "Another request" in busy[-1]["message"] + finally: + release.set() + await asyncio.gather(task, return_exceptions=True) + assert task.cancelled() + assert await load_session_settings(tape) == {"reasoning_effort": "high"} + assert provider.requests == [] + assert not adapter._active + provider.reply("next") + _, events = await collect(adapter, request(run="next")) + assert events[-1]["type"] == "RUN_FINISHED" From ce0ffc2da9a90f9ad2d5d08b49ff4b1720f5716a Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 10 Nov 2026 20:02:17 +0800 Subject: [PATCH 03/10] fix(bub-ag-ui): leave conversation context under Bub ownership --- packages/bub-ag-ui/README.md | 36 ++-- .../bub-ag-ui/src/bub_ag_ui/continuation.py | 31 +-- packages/bub-ag-ui/src/bub_ag_ui/runtime.py | 85 +++++---- packages/bub-ag-ui/tests/test_execution.py | 10 +- .../bub-ag-ui/tests/test_native_context.py | 176 ++++++++++++++++++ packages/bub-ag-ui/tests/test_runtime.py | 14 +- packages/bub-ag-ui/tests/test_session_tape.py | 66 ++++--- 7 files changed, 314 insertions(+), 104 deletions(-) create mode 100644 packages/bub-ag-ui/tests/test_native_context.py diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index 6c04a08..a96a04f 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -54,12 +54,19 @@ one stable `threadId` for the conversation and use a new `runId` for each run. The frontend renders the streamed updates. `HttpAgent` also accepts a `headers` option for authentication. -Each request supplies its conversation context through `messages` and `state`. -The adapter does not substitute a separate server-owned display history. If your -application needs reload persistence, save the client's messages, state, and -thread ID in application storage, then use `initialMessages` and `initialState` -when recreating the client. Keep any interrupt outcome needed by your decision -UI as well. There are no `/threads/...` history, replay, or cancel endpoints. +Bub owns the conversation and chooses model context from its native session tape. +The adapter appends only new user messages and results for pending external calls. +Keep message IDs stable when echoing history: already accepted input is not +appended again, and echoed assistant messages or old tool results do not replace +Bub's records. An edit to an accepted user message is rejected; this adapter does +not implement edit/regenerate branches. Start a new thread for a new conversation. + +Your application can save messages, shared state, and the thread ID to restore +its UI through `initialMessages` and `initialState`. This does not import that +snapshot into Bub or restore deleted server context. Continuing after a server +restart requires a persistent Bub tape store. Keep any interrupt outcome needed +by your decision UI as well. There are no `/threads/...` history, replay, or cancel +endpoints in this adapter. ## Frontend tools @@ -72,8 +79,9 @@ When a run requests frontend tools: 1. Wait for that run to finish and execute the requested tools. 2. Add a message for each result, with a new `id`, `role: "tool"`, the matching `toolCallId`, and `content`. -3. Start a new run on the same thread, sending the updated messages, state, and - tool definitions. Include a result for every pending frontend call. +3. Start a new run on the same thread, sending the results, shared state, and + tool definitions. Include a result for every pending frontend call. Bub already + has the suspended exchange; a result-only request does not need to echo it. No synthetic user message is needed. **`RUN_FINISHED` ends one run; it may mean that the agent is waiting for your frontend, not that it has finished answering.** @@ -109,12 +117,11 @@ interrupt responses. Repeating the same accepted interrupt responses does not re-execute resumed work; it is not a replay of the earlier generated answer. Continuation across a server restart requires a persistent Bub tape store. -Each thread uses one native execution tape. Before execution, the adapter replaces -its model context with the request's messages, retaining native session settings, -loaded-tool visibility, and the continuation checkpoint. It does not archive a -full conversation copy for every run. Mounted sidecars are left intact. This tape -is working context, not an append-only conversation archive; the application -remains responsible for saving the conversation it wants to restore. +Each thread uses one native tape. Bub continues to own its records, session +settings, loaded-tool visibility, and handoff anchors. The adapter neither resets +that tape on each request nor stores a complete client-history copy per run. +Echoing older messages cannot bring them back into model context after a native +handoff. ## Errors and cancellation @@ -125,6 +132,7 @@ remains responsible for saving the conversation it wants to restore. - Another executing request on the same thread is rejected with `RUN_ERROR`. Run one adapter per store/workspace; cross-process coordination is not provided. - Reusing a run ID does not request replay or provide general request deduplication. + A history-only request with no new input is rejected rather than re-executed. Reconnecting does not restart interrupted work. Retry policy belongs to the application; consider tool side effects before submitting work again. diff --git a/packages/bub-ag-ui/src/bub_ag_ui/continuation.py b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py index 3ac250c..867399a 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/continuation.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py @@ -61,20 +61,7 @@ def prepare(self, request: RunAgentInput) -> tuple[list[dict[str, Any]], bool]: raise ValueError("Duplicate interrupt response") if not repeated and {r["interruptId"] for r in responses} != interrupts.keys(): raise ValueError("Resume must address every pending interrupt exactly once") - calls = { - call["id"]: call - for message in messages - if message["role"] == "assistant" - for call in message.get("toolCalls", []) - } for pending in self.pending: - call = calls.get(pending["id"]) - if call is None or call["function"]["name"] != pending["name"].replace( - ".", "_" - ): - raise ValueError( - "Continuation messages must include the pending tool call" - ) results = [ m for m in messages @@ -92,21 +79,17 @@ def prepare(self, request: RunAgentInput) -> tuple[list[dict[str, Any]], bool]: ) if repeated and any(m.get("toolCallId") == call["id"] for m in messages): continue - if call["id"] not in calls: - raise ValueError( - "Resume messages must include the interrupted tool call" - ) if response["status"] == "resolved" and call.get("response_schema"): jsonschema.validate(response["payload"], call["response_schema"]) - # Put results immediately after the suspended exchange, before any - # new user input included alongside the decision. - index = ( - next( - i + # Update the client view if it echoes the call. Native execution + # already recorded that exchange and does not require it back. + index = next( + ( + i + 1 for i, m in enumerate(messages) if any(c["id"] == call["id"] for c in m.get("toolCalls", [])) - ) - + 1 + ), + 0, ) while index < len(messages) and messages[index]["role"] == "tool": index += 1 diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py index 24c7279..a27f97e 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py @@ -14,10 +14,7 @@ import jsonschema from ag_ui.core import RunAgentInput from bub.builtin.agent import BubAgent -from bub.builtin.settings import load_session_settings, set_session_setting -from bub.builtin.tools import TOOL_LOADED_EVENT, loaded_tool_names -from bub.core.tape import TapeEntry, session_tape_name, to_message -from bub.core.tools import ToolContext +from bub.core.tape import Tape, TapeEntry, session_tape_name, to_message from bub_ag_ui.continuation import Continuation from bub_ag_ui.events import EventStream, RunProjection, result_text @@ -79,12 +76,44 @@ def model_message(message: dict[str, Any]) -> dict[str, Any] | None: return result +async def new_inputs( + tape: Tape, messages: list[dict[str, Any]], pending_ids: set[str] +) -> list[tuple[str, dict[str, Any]]]: + """Map AG-UI input to native appends, never rebuild context from UI history. + + IDs on native input entries distinguish new messages from client echoes, + including echoes of messages that Bub has already moved behind a handoff. + Assistant output and backend tool results belong to the native recorder. + """ + accepted = { + entry.meta["ag_ui_message_id"]: entry.payload + for entry in await tape.query().kinds("message").all() + if "ag_ui_message_id" in entry.meta + } + additions = [] + for message in messages: + if message["role"] != "user" and not ( + message["role"] == "tool" and message["toolCallId"] in pending_ids + ): + continue + converted = model_message(message) + if message["id"] in accepted: + if accepted[message["id"]] != converted: + raise ProtocolError( + "Editing accepted input is not supported; start a new thread" + ) + continue + additions.append((message["id"], converted)) + # Resolve the native pending exchange before any new user input sent with it. + return sorted(additions, key=lambda item: item[1]["role"] != "tool") + + class AGUIAdapter: """Adapt a request and its native execution to an AG-UI event stream. - The client supplies conversation messages/state on every run. Native session - settings and external-call checkpoints remain on the thread's working tape. - Streaming does not read back events from storage. + Bub owns the conversation tape and its context selection. The adapter appends + only new user input or results for pending external calls. Client history is + not a replacement for native context; streaming never polls stored events. """ def __init__( @@ -188,6 +217,7 @@ async def publish(event: dict[str, Any]) -> None: ) continuation = await Continuation.load(tape) try: + pending_ids = {call["id"] for call in continuation.pending} messages, repeated = continuation.prepare(request) agent = self.agent_factory(self.framework, tape_store=tape.store) external = ExternalTools([*request.tools, *self.interrupts]) @@ -196,11 +226,13 @@ async def publish(event: dict[str, Any]) -> None: jsonschema.Draft202012Validator.check_schema( definition.parameters ) - converted = [ - value - for m in messages - if (value := model_message(m)) is not None - ] + additions = await new_inputs(tape, messages, pending_ids) + if repeated and additions: + raise ProtocolError( + "Send new input without an already resolved resume" + ) + if not additions and not repeated: + raise ProtocolError("No new user input or pending tool result") except ( ValueError, jsonschema.ValidationError, @@ -213,32 +245,19 @@ async def publish(event: dict[str, Any]) -> None: ) await shared.set(shared.value) if not repeated: - settings = await load_session_settings(tape) - loaded = await loaded_tool_names(tape) - async def replace_context() -> None: - # One thread tape, not one archive per request. Keep - # native session settings/tool visibility and mounted - # sidecars while replacing only the execution context. - await tape.store.reset(tape.name) + async def append_input() -> None: await tape.ensure_bootstrap_anchor() - context = ToolContext(tape=tape) - for key, value in settings.items(): - await set_session_setting(context, key, value) - if loaded: - await tape.append_event( - TOOL_LOADED_EVENT, {"names": loaded} - ) - # Persist consumed responses before resumed side effects. - await continuation.save(tape) - for message in converted: + if pending_ids: + # Record receipt before executing any resumed side effect. + await continuation.save(tape) + for message_id, message in additions: await tape.store.append( - tape.name, TapeEntry.message(message) + tape.name, + TapeEntry.message(message, ag_ui_message_id=message_id), ) - # Cancellation must not leave a still-running rewrite behind - # after this request releases thread ownership. - await committed(replace_context()) + await committed(append_input()) inbound = AGUIInboundMessage( session_id=session_id, channel="ag-ui", diff --git a/packages/bub-ag-ui/tests/test_execution.py b/packages/bub-ag-ui/tests/test_execution.py index 0b05864..5e436dd 100644 --- a/packages/bub-ag-ui/tests/test_execution.py +++ b/packages/bub-ag-ui/tests/test_execution.py @@ -308,12 +308,14 @@ async def source(): run="r2", ), ) - # A subsequent request explicitly supplies these messages. The adapter must - # use that context, not substitute its own persisted display history. - assert any( + # Display-only provider output is not promoted into Bub's model context by + # the client echo. The provider remains responsible for native recording. + assert not any( m.get("content") == "custom answer" for m in provider.requests[-1]["messages"] ) - assert any(m.get("tool_call_id") == "c1" for m in provider.requests[-1]["messages"]) + assert not any( + m.get("tool_call_id") == "c1" for m in provider.requests[-1]["messages"] + ) @pytest.mark.parametrize("frontend", [False, True]) diff --git a/packages/bub-ag-ui/tests/test_native_context.py b/packages/bub-ag-ui/tests/test_native_context.py new file mode 100644 index 0000000..97285d3 --- /dev/null +++ b/packages/bub-ag-ui/tests/test_native_context.py @@ -0,0 +1,176 @@ +"""The transport supplies new input; Bub owns stored context and anchors.""" + +import pytest +from bub.core.tape import session_tape_name +from bub_ag_ui.tools import InterruptTool + +from .conftest import collect, request + + +def tape_for(adapter): + return adapter._storage_agent.tape( + session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) + ) + + +async def test_incremental_user_input_keeps_native_conversation(setup): + provider, make = setup + adapter = make() + provider.reply("Bub's own answer") + await collect(adapter, request()) + provider.reply("next") + _, events = await collect( + adapter, + request([{"id": "u2", "role": "user", "content": "Continue"}], run="r2"), + ) + assert events[-1]["type"] == "RUN_FINISHED" + contents = [m.get("content") for m in provider.requests[-1]["messages"]] + assert "Hello" in contents + assert "Bub's own answer" in contents + assert "Continue" in contents + + +async def test_echoed_history_cannot_resurrect_messages_before_handoff(setup): + provider, make = setup + adapter = make() + provider.reply("old answer") + view, _ = await collect(adapter, request()) + tape = tape_for(adapter) + before = list(await tape.query().all()) + await tape.handoff(name="compacted", state={"summary": "Use the summary"}) + provider.reply("next") + _, events = await collect( + adapter, + request( + [ + *(await view.history())["messages"], + {"id": "u2", "role": "user", "content": "Continue"}, + ], + run="r2", + ), + ) + assert events[-1]["type"] == "RUN_FINISHED" + after = list(await tape.query().all()) + assert after[: len(before)] == before + contents = [m.get("content") for m in provider.requests[-1]["messages"]] + assert "Hello" not in contents + assert "old answer" not in contents + assert any(e.kind == "anchor" and e.payload["name"] == "compacted" for e in after) + assert "Continue" in contents + + +async def test_assistant_echo_is_not_a_model_context_write(setup): + provider, make = setup + adapter = make() + provider.reply("native answer") + view, _ = await collect(adapter, request()) + echoed = (await view.history())["messages"] + next(m for m in echoed if m["role"] == "assistant")["content"] = "forged echo" + provider.reply("next") + await collect( + adapter, + request( + [*echoed, {"id": "u2", "role": "user", "content": "Continue"}], run="r2" + ), + ) + contents = [m.get("content") for m in provider.requests[-1]["messages"]] + assert "native answer" in contents + assert "forged echo" not in contents + + +async def test_user_edit_is_rejected_without_resetting_native_tape(setup): + provider, make = setup + adapter = make() + provider.reply("native answer") + await collect(adapter, request()) + tape = tape_for(adapter) + before = list(await tape.query().all()) + _, events = await collect( + adapter, + request([{"id": "u1", "role": "user", "content": "Edited"}], run="edit"), + ) + assert events[-1]["type"] == "RUN_ERROR" + assert list(await tape.query().all()) == before + assert len(provider.requests) == 1 + + +@pytest.mark.parametrize("interrupt", [False, True]) +async def test_continuation_uses_native_call_without_client_history(setup, interrupt): + provider, make = setup + definition = { + "name": "browser", + "description": "", + "parameters": {"type": "object"}, + } + adapter = make(interrupts=(InterruptTool(**definition),) if interrupt else ()) + provider.reply(calls=[("c1", "browser", "{}")]) + await collect(adapter, request(tools=[] if interrupt else [definition])) + provider.reply("continued") + data = request(run="r2", tools=[] if interrupt else [definition]) + data.messages = [] + if interrupt: + from ag_ui.core import ResumeEntry + + data.resume = [ResumeEntry(interrupt_id="c1", status="resolved", payload=True)] + else: + data = request( + [ + { + "id": "result", + "role": "tool", + "toolCallId": "c1", + "content": "browser result", + } + ], + run="r2", + tools=[definition], + ) + _, events = await collect(adapter, data) + assert events[-1]["type"] == "RUN_FINISHED" + assert [ + m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "user" + ] == ["Hello"] + assert [ + m["tool_call_id"] + for m in provider.requests[-1]["messages"] + if m["role"] == "tool" + ] == ["c1"] + + +async def test_history_only_request_does_not_execute_or_modify_tape(setup): + provider, make = setup + adapter = make() + provider.reply("native answer") + view, _ = await collect(adapter, request()) + tape = tape_for(adapter) + before = list(await tape.query().all()) + _, events = await collect( + adapter, request((await view.history())["messages"], run="retry") + ) + assert events[-1]["type"] == "RUN_ERROR" + assert "No new user input" in events[-1]["message"] + assert len(provider.requests) == 1 + assert list(await tape.query().all()) == before + + +async def test_repeated_resume_does_not_silently_discard_new_input(setup): + provider, make = setup + adapter = make( + interrupts=(InterruptTool("approve", "Approval", {"type": "object"}),) + ) + provider.reply(calls=[("a1", "approve", "{}")]) + view, _ = await collect(adapter, request()) + resume = [{"interruptId": "a1", "status": "resolved", "payload": True}] + provider.reply("done") + await collect( + adapter, + request((await view.history())["messages"], run="resume", resume=resume), + ) + new_user = [{"id": "next-user", "role": "user", "content": "Next question"}] + before = list(await tape_for(adapter).query().all()) + _, events = await collect(adapter, request(new_user, run="retry", resume=resume)) + assert events[-1]["type"] == "RUN_ERROR" + assert list(await tape_for(adapter).query().all()) == before + provider.reply("next answer") + _, events = await collect(adapter, request(new_user, run="next")) + assert events[-1]["type"] == "RUN_FINISHED" diff --git a/packages/bub-ag-ui/tests/test_runtime.py b/packages/bub-ag-ui/tests/test_runtime.py index 7648c58..d6b275d 100644 --- a/packages/bub-ag-ui/tests/test_runtime.py +++ b/packages/bub-ag-ui/tests/test_runtime.py @@ -61,6 +61,7 @@ async def test_frontend_mixed_tools_resume_across_restart(setup, frontend_first) ) provider.reply("All done") history = deepcopy((await thread.history())["messages"]) + next(m for m in history if m["role"] == "tool")["content"] = "forged backend result" history.append( {"id": "fr", "role": "tool", "toolCallId": "f1", "content": "browser result"} ) @@ -77,19 +78,19 @@ async def test_frontend_mixed_tools_resume_across_restart(setup, frontend_first) ] -async def test_request_messages_are_the_context_not_server_history(setup): +async def test_new_user_input_appends_without_replacing_native_context(setup): provider, make = setup adapter = make() provider.reply("old answer") await collect(adapter, request()) provider.reply("new answer") - # Same thread and run ID do not imply an application-level replay service. + # A different user message is new input, not a replacement history. await collect( adapter, request([{"id": "edit", "role": "user", "content": "Edited"}]) ) assert [ m["content"] for m in provider.requests[-1]["messages"] if m["role"] != "system" - ] == ["Edited"] + ] == ["Hello", "old answer", "Edited"] assert len(provider.requests) == 2 @@ -253,7 +254,12 @@ async def wait(): assert not adapter._active assert not adapter._tasks provider.reply("next") - _, events = await collect(adapter, request(run="next")) + _, events = await collect( + adapter, + request( + [{"id": "next-user", "role": "user", "content": "Continue"}], run="next" + ), + ) assert events[-1]["type"] == "RUN_FINISHED" diff --git a/packages/bub-ag-ui/tests/test_session_tape.py b/packages/bub-ag-ui/tests/test_session_tape.py index 8a5793c..b7db895 100644 --- a/packages/bub-ag-ui/tests/test_session_tape.py +++ b/packages/bub-ag-ui/tests/test_session_tape.py @@ -1,4 +1,4 @@ -"""Request context is replaceable; native thread settings are not run-local.""" +"""Bub owns one append-only thread tape, including context and settings.""" from itertools import pairwise @@ -97,11 +97,14 @@ async def switch(context, value): assert observed[0] == (None, None) -async def test_one_native_tape_holds_only_current_request_context(setup, store): +async def test_one_native_tape_appends_new_input_without_copying_echoed_history( + setup, store +): provider, make = setup adapter = make(tape_store=store) messages = [{"id": "u1", "role": "user", "content": "First"}] sizes = [] + previous = [] for turn in range(6): provider.reply(f"answer {turn}") view, events = await collect(adapter, request(messages, run=f"r{turn}")) @@ -109,28 +112,31 @@ async def test_one_native_tape_holds_only_current_request_context(setup, store): tape = session_tape(adapter) names = await tape.store.list_tapes() assert names == [tape.name] - sizes.append(len(list(await tape.query().all()))) + entries = list(await tape.query().all()) + assert entries[: len(previous)] == previous + previous = entries + sizes.append(len(entries)) messages = [ *(await view.history())["messages"], {"id": f"u{turn + 2}", "role": "user", "content": f"question {turn + 1}"}, ] - # Only the two newly supplied messages grow the retained context each turn, - # not a sum of all earlier request snapshots and native step records. - assert [b - a for a, b in pairwise(sizes)] == [2] * 5 - provider.reply("edited answer") + # Native loop records grow linearly too, but earlier input is never copied. + increments = [b - a for a, b in pairwise(sizes)] + assert len(set(increments)) == 1 and increments[0] > 0 + entries = list(await tape.query().kinds("message").all()) + assert sum(e.payload.get("role") == "user" for e in entries) == 6 + assert sum(e.payload.get("role") == "assistant" for e in entries) == 6 + provider.reply("next answer") _, events = await collect( adapter, request( - [{"id": "edit", "role": "user", "content": "Replacement context"}], - run="edited", + [{"id": "next", "role": "user", "content": "Just new input"}], run="next" ), ) assert events[-1]["type"] == "RUN_FINISHED" - tape = session_tape(adapter) - assert len(list(await tape.query().all())) == sizes[0] - assert [ - m["content"] for m in provider.requests[-1]["messages"] if m["role"] != "system" - ] == ["Replacement context"] + assert len(list(await tape.query().all())) == sizes[-1] + increments[0] + assert provider.requests[-1]["messages"][-1]["content"] == "Just new input" + assert any(m.get("content") == "First" for m in provider.requests[-1]["messages"]) async def test_sqlite_reopen_retains_settings_without_orphan_run_tapes(setup, tmp_path): @@ -157,7 +163,13 @@ async def switch(context): "code_mode": False } provider.reply("after reopen") - _, events = await collect(adapter, request(run="after-reopen")) + _, events = await collect( + adapter, + request( + [{"id": "after", "role": "user", "content": "After reopen"}], + run="after-reopen", + ), + ) assert events[-1]["type"] == "RUN_FINISHED" assert await load_session_settings(session_tape(adapter)) == { "code_mode": False @@ -166,9 +178,7 @@ async def switch(context): await store.close() -async def test_native_deferred_tools_and_sidecars_survive_context_replacement( - setup, store -): +async def test_native_deferred_tools_and_sidecars_survive_next_turn(setup, store): from bub.builtin.tools import TOOL_LOADED_EVENT, loaded_tool_names from bub.core.tape import TapeEntry @@ -206,7 +216,7 @@ async def test_native_deferred_tools_and_sidecars_survive_context_replacement( assert entries[0].payload["data"]["handle"] == "retained" -async def test_cancelled_rewrite_settles_before_releasing_thread( +async def test_cancelled_input_append_settles_before_releasing_thread( setup, store, monkeypatch ): import asyncio @@ -218,18 +228,18 @@ async def test_cancelled_rewrite_settles_before_releasing_thread( await set_session_setting(ToolContext(tape=tape), "reasoning_effort", "high") entered, release = asyncio.Event(), asyncio.Event() - reset = tape.store.reset + append = tape.store.append pause = True - async def slow_reset(name): + async def slow_append(name, entry): nonlocal pause - await reset(name) - if name == tape.name and pause: + await append(name, entry) + if name == tape.name and entry.meta.get("ag_ui_message_id") and pause: pause = False entered.set() await release.wait() - monkeypatch.setattr(tape.store, "reset", slow_reset) + monkeypatch.setattr(tape.store, "append", slow_append) task = asyncio.create_task(collect(adapter, request())) try: await asyncio.wait_for(entered.wait(), 5) @@ -247,5 +257,11 @@ async def slow_reset(name): assert provider.requests == [] assert not adapter._active provider.reply("next") - _, events = await collect(adapter, request(run="next")) + _, events = await collect( + adapter, + request( + [{"id": "after", "role": "user", "content": "After cancellation"}], + run="next", + ), + ) assert events[-1]["type"] == "RUN_FINISHED" From 5f32b384dbe009044cd4617f6322f4e83d2b237a Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 10 Nov 2026 21:49:55 +0800 Subject: [PATCH 04/10] refactor(bub-ag-ui): trim execution and event adaptation boundaries --- packages/bub-ag-ui/README.md | 8 + packages/bub-ag-ui/src/bub_ag_ui/events.py | 21 +- packages/bub-ag-ui/src/bub_ag_ui/execution.py | 76 +----- packages/bub-ag-ui/src/bub_ag_ui/runner.py | 217 ------------------ packages/bub-ag-ui/src/bub_ag_ui/runtime.py | 23 +- packages/bub-ag-ui/src/bub_ag_ui/tools.py | 68 ++++++ packages/bub-ag-ui/src/bub_ag_ui/turn.py | 184 +++++++++++++++ packages/bub-ag-ui/tests/conftest.py | 5 +- packages/bub-ag-ui/tests/test_events.py | 29 ++- packages/bub-ag-ui/tests/test_turn_bridge.py | 132 +++++++++++ 10 files changed, 446 insertions(+), 317 deletions(-) delete mode 100644 packages/bub-ag-ui/src/bub_ag_ui/runner.py create mode 100644 packages/bub-ag-ui/src/bub_ag_ui/turn.py create mode 100644 packages/bub-ag-ui/tests/test_turn_bridge.py diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index a96a04f..20e10db 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -136,6 +136,14 @@ handoff. Reconnecting does not restart interrupted work. Retry policy belongs to the application; consider tool side effects before submitting work again. +The pinned Bub API still requires a small step bridge for result-only continuation +and returning control to frontend tools. Native `run_once` prepares and executes +each model/tool step; native steering and subagent-result hooks remain active. +The adapter does not implement its own context-overflow handoff/retry policy: +uncaught context overflow produces `RUN_ERROR`. Native context-budget hooks and +explicit handoff still apply. It does not synthesize Bub full-run lifecycle events; +AG-UI run completion is owned by the request boundary. + ## Embedding and development Use `bub_ag_ui.http.create_app` to embed the endpoint. In an existing Bub gateway, diff --git a/packages/bub-ag-ui/src/bub_ag_ui/events.py b/packages/bub-ag-ui/src/bub_ag_ui/events.py index 0dd7391..4da40e0 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/events.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/events.py @@ -30,12 +30,11 @@ def result_text(value: Any) -> str: return value if isinstance(value, str) else json.dumps(value, ensure_ascii=False) -class RunProjection: - """Current response messages/state for validation and interrupt snapshots.""" +class MessageProjection: + """Current response messages for activity validation and interrupt snapshots.""" def __init__(self) -> None: self.messages: list[dict[str, Any]] = [] - self.state: Any = {} def message(self, id: str) -> dict[str, Any]: return next(message for message in self.messages if message["id"] == id) @@ -79,10 +78,6 @@ def apply(self, event: dict[str, Any]) -> None: "toolCallId": event["toolCallId"], } ) - case "STATE_SNAPSHOT": - self.state = deepcopy(event["snapshot"]) - case "STATE_DELTA": - self.state = jsonpatch.apply_patch(self.state, event["delta"]) case "ACTIVITY_SNAPSHOT": existing = next( (m for m in self.messages if m["id"] == event["messageId"]), None @@ -111,13 +106,21 @@ class EventStream: """One run's wire state; a Bub final closes a step, never a run.""" def __init__(self, emit: Emit) -> None: - self.emit = emit + self._emit = emit + self._messages = MessageProjection() self.message_id: str | None = None self.reasoning_id: str | None = None self.calls: list[dict[str, Any]] = [] async def send(self, type: str, **fields: Any) -> None: - await self.emit(wire_event(type, **fields)) + await self.publish(wire_event(type, **fields)) + + async def publish(self, event: dict[str, Any]) -> None: + self._messages.apply(event) + await self._emit(event) + + async def snapshot(self) -> None: + await self.send("MESSAGES_SNAPSHOT", messages=self._messages.messages) async def text_start(self) -> str: if self.message_id is None: diff --git a/packages/bub-ag-ui/src/bub_ag_ui/execution.py b/packages/bub-ag-ui/src/bub_ag_ui/execution.py index a0f51b7..876af44 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/execution.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/execution.py @@ -7,9 +7,7 @@ from __future__ import annotations -import json from collections.abc import AsyncIterable, AsyncIterator -from contextlib import aclosing from copy import deepcopy from dataclasses import dataclass from pathlib import Path @@ -19,18 +17,15 @@ from bub import hookimpl from bub.builtin.agent import BubAgent from bub.builtin.settings import load_session_settings -from bub.builtin.tools import ALLOWED_TOOLS_STATE_KEY from bub.channels.message import ChannelMessage -from bub.core.events import RunStarted, StepFinished, StepStarted from bub.envelope import Envelope, field_of -from bub.streaming import AsyncStreamEvents, StreamEvent, StreamState +from bub.streaming import AsyncStreamEvents, StreamEvent from bub.turn import TurnState from bub_ag_ui.events import EventStream -from bub_ag_ui.lifecycle import tape_session -from bub_ag_ui.runner import run_steps from bub_ag_ui.state import SharedState from bub_ag_ui.tools import ExternalTools +from bub_ag_ui.turn import stream_turn @dataclass(kw_only=True) @@ -98,68 +93,13 @@ def build_prompt(self, message: Envelope) -> list[str] | None: # names before invoking hooks; adding that method would displace an existing # scalar provider even when this scoped implementation returned None. @hookimpl(specname="run_model_stream", tryfirst=True) - async def stream_protocol_turn(self, state: TurnState) -> AsyncStreamEvents | None: + def stream_protocol_turn(self, state: TurnState) -> AsyncStreamEvents | None: inbound = state.get("_ag_ui_inbound") - if not isinstance(inbound, AGUIInboundMessage): - return None - agent = inbound._runtime_agent - inbound.external.bind(agent, state) - agent.system_prompt.add(protocol_context) - stream_state = StreamState() - - # Subagents share the agent/EventBus, but each run owns its turn state. - async def step_started(event: StepStarted) -> None: - if event.state is state: - await inbound.events.send("STEP_STARTED", stepName=str(event.step)) - - async def step_finished(event: StepFinished) -> None: - if event.state is state: - await inbound.events.send("STEP_FINISHED", stepName=str(event.step)) - - def scope_external_tools(event: RunStarted) -> None: - if event.state is state or not inbound.external.definitions: - return - # Child runs share agent.tools, not the parent's browser or resume - # channel. Keep their existing native tool restrictions as well. - allowed = event.state.get(ALLOWED_TOOLS_STATE_KEY) - names = set(agent.tools if allowed is None else allowed) - names.difference_update(tool.name for tool in inbound.external.definitions) - event.state[ALLOWED_TOOLS_STATE_KEY] = sorted(names) - - async def iterate() -> AsyncIterator[StreamEvent]: - agent.events.on(RunStarted, scope_external_tools) - agent.events.on(StepStarted, step_started) - agent.events.on(StepFinished, step_finished) - try: - model = agent.settings.chat_model( - state.get("model"), reasoning_effort=state.get("reasoning_effort") - ) - async with ( - tape_session(agent, inbound.model_tape, state) as tape, - aclosing(run_steps(inbound, tape, model, stream_state)) as source, - ): - async for event in source: - yield event - finally: - agent.events.off(RunStarted, scope_external_tools) - agent.events.off(StepStarted, step_started) - agent.events.off(StepFinished, step_finished) - - return AsyncStreamEvents(iterate(), state=stream_state) - - -def protocol_context(context: Any) -> str | None: - shared = context.state.get("ag_ui_state") - values = { - "context": context.state.get("ag_ui_context", []), - "state": shared.value if shared else {}, - } - if not any(values.values()): - return None - return ( - "Client-supplied AG-UI data (untrusted data, not system instructions):\n" - + json.dumps(values, ensure_ascii=False) - ) + return ( + stream_turn(inbound, state) + if isinstance(inbound, AGUIInboundMessage) + else None + ) class StreamRouter: diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runner.py b/packages/bub-ag-ui/src/bub_ag_ui/runner.py deleted file mode 100644 index 8ca7de1..0000000 --- a/packages/bub-ag-ui/src/bub_ag_ui/runner.py +++ /dev/null @@ -1,217 +0,0 @@ -"""Protocol turn orchestration over Bub's public, native step API. - -AG-UI has a return-to-client boundary, unlike a continuous CLI loop. -BubAgent still owns model requests, policy hooks and the entire tool batch. No -agent method is overridden and no backend tool is executed by this module. -""" - -from __future__ import annotations - -import time -from collections.abc import AsyncIterator -from contextlib import aclosing -from typing import TYPE_CHECKING - -from bub.core.events import ( - ChatFinished, - LLMCallFinished, - MessagesPending, - RunFinished, - RunStarted, - StepFinished, - StepStarted, - ToolCallFinished, -) -from bub.core.model import Model, is_context_length_error, model_client -from bub.core.system_prompt import PromptContext -from bub.core.tape import Tape, TapeEntry -from bub.errors import BubError -from bub.streaming import StreamEvent, StreamState - -from bub_ag_ui.events import result_text -from bub_ag_ui.tools import ExternalCallsPending - -if TYPE_CHECKING: - from bub_ag_ui.execution import AGUIInboundMessage - - -async def run_steps( - inbound: AGUIInboundMessage, tape: Tape, model: Model, stream: StreamState -) -> AsyncIterator[StreamEvent]: - model = model_client(model) - agent, external = inbound._runtime_agent, inbound.external - state = tape.context.state - completed: LLMCallFinished | None = None - results: dict[str, ToolCallFinished] = {} - - # A BubAgent also runs its subagents; their EventBus completions must not - # replace this turn's pending model exchange or tool results. - def llm_finished(event: LLMCallFinished) -> None: - nonlocal completed - if event.state is state: - completed = event - - def tool_finished(event: ToolCallFinished) -> None: - if event.state is not state: - return - if event.call.context and event.call.context.call_id: - results[event.call.context.call_id] = event - - agent.events.on(LLMCallFinished, llm_finished) - agent.events.on(ToolCallFinished, tool_finished) - started = time.monotonic() - status, error, step = "cancelled", None, 0 - handoff_retries = 1 - try: - tools = await agent.prepare_tools(tape, None) - spec = model.spec - stream.tools = tools - stream.system_prompt = ( - await agent.system_prompt.render( - PromptContext( - agent=agent, tape=tape, prompt=[], tools=tools, model=spec - ) - ) - or None - ) - await agent.events.emit( - RunStarted(state=state, tape=tape, model=spec, prompt=[]) - ) - prompt, pending = None, [] - for step in range(1, agent.max_steps + 1): - step_started = time.monotonic() - step_status, step_error = "cancelled", None - final = {} - reasoning = [] - completed = None - results.clear() - await agent.events.emit( - StepStarted(state=state, tape=tape, step=step, prompt=prompt) - ) - try: - async with aclosing( - agent.run_once( - prompt, tape=tape, model=model, state=stream, pending=pending - ) - ) as source: - try: - async for event in source: - if event.kind == "final": - final = event.data - elif event.kind == "reasoning": - reasoning.append(event.data["delta"]) - yield event - except ExternalCallsPending: - # The native executor has completed all non-suspended calls, - # including deny/replace and after_tool_call hooks. Record a - # partial exchange, never fake a result for a pending call. - if completed is None or external.context is None: - raise RuntimeError( - "External call without a model exchange" - ) from None - request, response = completed.request, completed.result - await agent.events.emit( - ChatFinished( - state=state, - tape=tape, - run_id=request.run_id, - model=request.model, - system_prompt=stream.system_prompt, - new_messages=external.context.step_messages or [], - response_text=response.text, - tool_calls=response.tool_calls, - usage=stream.usage, - assistant_fields={"reasoning": "".join(reasoning)} - if reasoning - else {}, - ) - ) - ids, values = [], [] - for call in response.tool_calls: - if outcome := results.get(call["id"]): - result = outcome.result - value = result.result - if result.error is not None: - value = ( - BubError(result.error.kind, result_text(value)) - if value is not None - and isinstance(result.error, BubError) - else result.error - ) - ids.append(call["id"]) - values.append(value) - await tape.store.append( - tape.name, - TapeEntry.message( - { - "role": "tool", - "tool_call_id": call["id"], - "content": result_text(value), - }, - run_id=request.run_id, - ), - ) - if values: - yield StreamEvent( - "tool_result", {"call_ids": ids, "tool_results": values} - ) - final = {"ok": True, "pending": True} - yield StreamEvent("final", final) - pending = [] - continuing = bool(final.get("tool_calls") or final.get("tool_results")) - if not continuing and not external.pending and step < agent.max_steps: - event = await agent.events.emit( - MessagesPending( - state=state, tape=tape, prompt=None, step_ended=True - ) - ) - pending = event.messages - step_status = "continue" if continuing or pending else "ok" - except Exception as exc: - step_status, step_error = "error", str(exc) - if handoff_retries and is_context_length_error(step_error): - handoff_retries -= 1 - await tape.handoff( - name="auto_handoff/context_overflow", - state={ - "reason": "context_length_exceeded", - "error": step_error, - }, - ) - step_status = "auto_handoff" - continue - raise - finally: - finished = await agent.events.emit( - StepFinished( - state=state, - tape=tape, - step=step, - status=step_status, - elapsed_ms=int((time.monotonic() - step_started) * 1000), - error=step_error, - prompt=prompt, - stream=stream, - ) - ) - if external.pending or step_status == "ok": - status = "ok" - return - prompt = finished.next_prompt - raise RuntimeError(f"max_steps_reached={agent.max_steps}") - except Exception as exc: - status, error = "error", str(exc) - raise - finally: - agent.events.off(LLMCallFinished, llm_finished) - agent.events.off(ToolCallFinished, tool_finished) - await agent.events.emit( - RunFinished( - state=state, - tape=tape, - status=status, - steps=step, - elapsed_ms=int((time.monotonic() - started) * 1000), - error=error, - ) - ) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py index a27f97e..af7a6aa 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py @@ -7,7 +7,6 @@ import json import logging from collections.abc import AsyncIterator, Callable -from copy import deepcopy from pathlib import Path from typing import Any @@ -17,7 +16,7 @@ from bub.core.tape import Tape, TapeEntry, session_tape_name, to_message from bub_ag_ui.continuation import Continuation -from bub_ag_ui.events import EventStream, RunProjection, result_text +from bub_ag_ui.events import EventStream, result_text from bub_ag_ui.execution import ( AGUIInboundMessage, StreamRouter, @@ -38,7 +37,7 @@ def __init__(self, message: str, status: int = 400) -> None: def model_message(message: dict[str, Any]) -> dict[str, Any] | None: role = message["role"] - if role not in {"user", "assistant", "tool"}: + if role not in {"user", "tool"}: return None content = message.get("content", "") if isinstance(content, list): @@ -63,8 +62,6 @@ def model_message(message: dict[str, Any]) -> dict[str, Any] | None: raise ProtocolError(f"Bub cannot consume {part['type']} input", 422) content = parts result = {"role": role, "content": content} - if role == "assistant" and message.get("toolCalls"): - result["tool_calls"] = deepcopy(message["toolCalls"]) if role == "tool": result["tool_call_id"] = message["toolCallId"] if message.get("error"): @@ -183,15 +180,7 @@ async def emit(event: dict[str, Any]) -> None: self._tasks.discard(producer) async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> None: - # This projection lives only for this response: patches are validated and - # an interrupt can publish the full resume context before RUN_FINISHED. - projection = RunProjection() - - async def publish(event: dict[str, Any]) -> None: - projection.apply(event) - await emit(event) - - stream = EventStream(publish) + stream = EventStream(emit) session_id = self.session_id(principal, request.thread_id) acquired = False try: @@ -241,7 +230,7 @@ async def publish(event: dict[str, Any]) -> None: raise ProtocolError(str(exc)) from exc await stream.send("MESSAGES_SNAPSHOT", messages=messages) shared = SharedState( - request.state if request.state is not None else {}, publish + request.state if request.state is not None else {}, stream.publish ) await shared.set(shared.value) if not repeated: @@ -305,9 +294,7 @@ async def append_input() -> None: ] fields: dict[str, Any] = {} if interrupts: - await stream.send( - "MESSAGES_SNAPSHOT", messages=deepcopy(projection.messages) - ) + await stream.snapshot() await shared.set(shared.value) fields["outcome"] = {"type": "interrupt", "interrupts": interrupts} await stream.send( diff --git a/packages/bub-ag-ui/src/bub_ag_ui/tools.py b/packages/bub-ag-ui/src/bub_ag_ui/tools.py index 354b17e..788c6ac 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/tools.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/tools.py @@ -8,9 +8,15 @@ from ag_ui.core import Tool as ClientTool from bub.builtin.agent import BubAgent +from bub.core.events import ChatFinished, LLMCallFinished, ToolCallFinished +from bub.core.tape import Tape, TapeEntry from bub.core.tools import Tool, ToolContext, tool +from bub.errors import BubError +from bub.streaming import StreamEvent from bub.turn import TurnState +from bub_ag_ui.events import result_text + @dataclass(frozen=True) class InterruptTool: @@ -85,3 +91,65 @@ def suspend(*, context: ToolContext, **arguments: Any) -> None: # Client schemas are supplied at runtime, rather than by Python annotations. return replace(suspend, parameters=definition.parameters) + + async def record_pause( + self, + agent: BubAgent, + tape: Tape, + completed: LLMCallFinished | None, + results: dict[str, ToolCallFinished], + system_prompt: str | None, + reasoning: list[str], + ) -> StreamEvent | None: + """Record only the exchange interrupted by an admitted external call. + + Normal exchanges are recorded by run_once. Its tool executor settles + the whole batch before propagating our control transfer, so completed + backend results are available here without fabricating pending results. + """ + if completed is None or self.context is None: + raise RuntimeError("External call without a model exchange") + request, response = completed.request, completed.result + await agent.events.emit( + ChatFinished( + state=tape.context.state, + tape=tape, + run_id=request.run_id, + model=request.model, + system_prompt=system_prompt, + new_messages=self.context.step_messages or [], + response_text=response.text, + tool_calls=response.tool_calls, + usage=response.usage, + assistant_fields={"reasoning": "".join(reasoning)} if reasoning else {}, + ) + ) + ids, values = [], [] + for call in response.tool_calls: + if outcome := results.get(call["id"]): + result = outcome.result + value = result.result + if result.error is not None: + value = ( + BubError(result.error.kind, result_text(value)) + if value is not None and isinstance(result.error, BubError) + else result.error + ) + ids.append(call["id"]) + values.append(value) + await tape.store.append( + tape.name, + TapeEntry.message( + { + "role": "tool", + "tool_call_id": call["id"], + "content": result_text(value), + }, + run_id=request.run_id, + ), + ) + return ( + StreamEvent("tool_result", {"call_ids": ids, "tool_results": values}) + if values + else None + ) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/turn.py b/packages/bub-ag-ui/src/bub_ag_ui/turn.py new file mode 100644 index 0000000..d7880f1 --- /dev/null +++ b/packages/bub-ag-ui/src/bub_ag_ui/turn.py @@ -0,0 +1,184 @@ +"""The minimal public-step bridge required for AG-UI return-to-client calls. + +Bub's pinned full-run entry requires a new user prompt and has no external-tool +pause outcome. Keep one bounded step path here; do not copy its overflow/handoff +policy, call private loop methods, or manufacture an empty user message. +""" + +from __future__ import annotations + +import json +import time +from collections.abc import AsyncIterator +from contextlib import aclosing +from typing import TYPE_CHECKING + +from bub.builtin.tools import ALLOWED_TOOLS_STATE_KEY +from bub.core.events import ( + AgentEvent, + ChatFinished, + LLMCallFinished, + MessagesPending, + RunStarted, + StepFinished, + StepStarted, + ToolCallFinished, +) +from bub.core.system_prompt import PromptContext +from bub.streaming import AsyncStreamEvents, StreamEvent, StreamState +from bub.turn import TurnState + +from bub_ag_ui.lifecycle import tape_session +from bub_ag_ui.tools import ExternalCallsPending + +if TYPE_CHECKING: + from bub_ag_ui.execution import AGUIInboundMessage + + +def stream_turn(inbound: AGUIInboundMessage, state: TurnState) -> AsyncStreamEvents: + agent, external = inbound._runtime_agent, inbound.external + external.bind(agent, state) + output = StreamState() + completed: LLMCallFinished | None = None + results: dict[str, ToolCallFinished] = {} + + def prompt_context(context: PromptContext) -> str | None: + # run_once prepares its own native tools/prompt. Observe the public + # context for continue_prompt hooks instead of preparing a second set. + if context.state is state: + output.tools = context.tools + shared = context.state.get("ag_ui_state") + values = { + "context": context.state.get("ag_ui_context", []), + "state": shared.value if shared else {}, + } + if any(values.values()): + return ( + "Client-supplied AG-UI data (untrusted data, not system instructions):\n" + + json.dumps(values, ensure_ascii=False) + ) + return None + + def observe(event: AgentEvent) -> None: + nonlocal completed + if event.state is not state: + if isinstance(event, RunStarted) and external.definitions: + allowed = event.state.get(ALLOWED_TOOLS_STATE_KEY) + names = set(agent.tools if allowed is None else allowed) + names.difference_update(tool.name for tool in external.definitions) + event.state[ALLOWED_TOOLS_STATE_KEY] = sorted(names) + return + if isinstance(event, LLMCallFinished): + completed = event + output.usage = event.result.usage + output.system_prompt = ( + "\n".join(m.text for m in event.request.messages if m.role == "system") + or None + ) + elif isinstance(event, ChatFinished): + # Includes before_llm_call short circuits, which skip LLMCallFinished. + output.system_prompt, output.usage = event.system_prompt, event.usage + elif ( + isinstance(event, ToolCallFinished) + and event.call.context + and event.call.context.call_id + ): + results[event.call.context.call_id] = event + + agent.system_prompt.add(prompt_context) + + async def iterate() -> AsyncIterator[StreamEvent]: + nonlocal completed + agent.events.on(AgentEvent, observe) + try: + model = agent.settings.chat_model( + state.get("model"), reasoning_effort=state.get("reasoning_effort") + ) + async with tape_session(agent, inbound.model_tape, state) as tape: + prompt, pending = None, [] + for step in range(1, agent.max_steps + 1): + step_started = time.monotonic() + step_status, step_error = "cancelled", None + completed, reasoning, final = None, [], {} + results.clear() + # Emit root step boundaries at their source. There is no + # global Step event subscription to capture child turns. + await agent.events.emit( + StepStarted(state=state, tape=tape, step=step, prompt=prompt) + ) + await inbound.events.send("STEP_STARTED", stepName=str(step)) + try: + async with aclosing( + agent.run_once( + prompt, tape=tape, model=model, pending=pending + ) + ) as source: + try: + async for event in source: + if event.kind == "final": + final = event.data + elif event.kind == "reasoning": + reasoning.append(event.data["delta"]) + yield event + except ExternalCallsPending: + result = await external.record_pause( + agent, + tape, + completed, + results, + output.system_prompt, + reasoning, + ) + if result is not None: + yield result + final = {"ok": True, "pending": True} + yield StreamEvent("final", final) + pending = [] + continuing = bool( + final.get("tool_calls") or final.get("tool_results") + ) + if ( + not continuing + and not external.pending + and step < agent.max_steps + ): + # Delegate steering and subagent result delivery + # to Bub's registered handlers, not an inbox here. + pending = ( + await agent.events.emit( + MessagesPending( + state=state, + tape=tape, + prompt=None, + step_ended=True, + ) + ) + ).messages + step_status = "continue" if continuing or pending else "ok" + except Exception as exc: + step_status, step_error = "error", str(exc) + raise + finally: + finished = await agent.events.emit( + StepFinished( + state=state, + tape=tape, + step=step, + status=step_status, + elapsed_ms=int( + (time.monotonic() - step_started) * 1000 + ), + error=step_error, + prompt=prompt, + stream=output, + ) + ) + await inbound.events.send("STEP_FINISHED", stepName=str(step)) + if external.pending or step_status == "ok": + return + prompt = finished.next_prompt + raise RuntimeError(f"max_steps_reached={agent.max_steps}") + finally: + agent.events.off(AgentEvent, observe) + + return AsyncStreamEvents(iterate(), state=output) diff --git a/packages/bub-ag-ui/tests/conftest.py b/packages/bub-ag-ui/tests/conftest.py index 9ef4439..09cd589 100644 --- a/packages/bub-ag-ui/tests/conftest.py +++ b/packages/bub-ag-ui/tests/conftest.py @@ -112,7 +112,6 @@ def __init__(self, projection, pending): async def history(self): return { "messages": self.projection.messages, - "state": self.projection.state, "pending": self.pending, } @@ -120,10 +119,10 @@ async def history(self): async def collect(adapter, data, user="alice"): from bub.core.tape import session_tape_name from bub_ag_ui.continuation import Continuation - from bub_ag_ui.events import RunProjection + from bub_ag_ui.events import MessageProjection events = [event async for event in adapter.run(user, data)] - projection = RunProjection() + projection = MessageProjection() for event in events: projection.apply(event) tape = adapter._storage_agent.tape( diff --git a/packages/bub-ag-ui/tests/test_events.py b/packages/bub-ag-ui/tests/test_events.py index 6cf4f2f..ae044e8 100644 --- a/packages/bub-ag-ui/tests/test_events.py +++ b/packages/bub-ag-ui/tests/test_events.py @@ -3,13 +3,13 @@ import pytest from ag_ui.core import ActivityDeltaEvent, ActivitySnapshotEvent -from bub_ag_ui.events import RunProjection +from bub_ag_ui.events import MessageProjection from bub_ag_ui.state import SharedState from jsonpatch import JsonPatchConflict async def test_invalid_activity_patch_does_not_change_response(): - projection = RunProjection() + projection = MessageProjection() async def emit(event): projection.apply(event) @@ -55,3 +55,28 @@ async def emit(event): ) assert state.value == {"items": list(range(10))} assert len(events) == 10 + + +async def test_shared_state_patch_has_one_owner(monkeypatch): + import jsonpatch + from bub_ag_ui.events import EventStream + + events, applied = [], [] + apply = jsonpatch.apply_patch + + def counted(value, operations, **kwargs): + applied.append(operations) + return apply(value, operations, **kwargs) + + monkeypatch.setattr(jsonpatch, "apply_patch", counted) + + async def emit(event): + events.append(event) + + stream = EventStream(emit) + state = SharedState({"count": 0}, stream.publish) + await state.set(state.value) + await state.patch([{"op": "replace", "path": "/count", "value": 1}]) + assert state.value == {"count": 1} + assert len(applied) == 1 + assert events[-1]["type"] == "STATE_DELTA" diff --git a/packages/bub-ag-ui/tests/test_turn_bridge.py b/packages/bub-ag-ui/tests/test_turn_bridge.py new file mode 100644 index 0000000..04602f7 --- /dev/null +++ b/packages/bub-ag-ui/tests/test_turn_bridge.py @@ -0,0 +1,132 @@ +"""The pinned API needs a step bridge, not a second copy of Bub's policies.""" + +import httpx2 +from bub import hookimpl +from bub.core.events import MessagesPending +from bub.core.tape import session_tape_name +from bub.core.tools import Tool + +from .conftest import collect, request + + +async def test_each_step_uses_native_tool_preparation_and_continue_hook(setup): + provider, make = setup + observed = [] + + def load(context): + context.state["_runtime_agent"].tools["later"] = Tool.from_callable( + lambda: "later", name="later" + ) + return "loaded" + + adapter = make([Tool.from_callable(load, context=True)]) + + class Hooks: + @hookimpl + def continue_prompt(self, state): + observed.append((state.system_prompt, [t.name for t in state.tools])) + return "Native continue hook" + + adapter.framework.plugin_manager.register(Hooks(), "native-continuation") + provider.reply(calls=[("load", "load", "{}")]) + provider.reply("done") + _, events = await collect(adapter, request()) + assert events[-1]["type"] == "RUN_FINISHED" + assert len(observed) == 1 + assert observed[0][0] # public LLM request supplies the observed system prompt + assert observed[0][1] == ["load"] + assert "later" in {t["function"]["name"] for t in provider.requests[-1]["tools"]} + assert provider.requests[-1]["messages"][-1]["content"] == "Native continue hook" + + +async def test_native_end_of_step_message_delivery_is_preserved(setup): + provider, make = setup + adapter = make() + delivered = False + + def deliver(event): + nonlocal delivered + if event.step_ended and not delivered: + delivered = True + event.messages.append(["Native queued input"]) + + class Hooks: + @hookimpl + def load_state(self, message): + message._runtime_agent.events.on(MessagesPending, deliver) + + adapter.framework.plugin_manager.register(Hooks(), "native-message-delivery") + provider.reply("first") + provider.reply("after queued input") + _, events = await collect(adapter, request()) + assert events[-1]["type"] == "RUN_FINISHED" + assert len(provider.requests) == 2 + assert [ + m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "user" + ] == ["Hello", "Native queued input"] + + +async def test_context_overflow_does_not_trigger_adapter_handoff_or_retry(setup): + provider, make = setup + adapter = make() + provider.responses.append( + httpx2.Response( + 400, + json={ + "error": { + "message": "maximum context length exceeded", + "code": "context_length_exceeded", + } + }, + ) + ) + _, events = await collect(adapter, request()) + assert len(provider.requests) == 1 + assert events[-1]["type"] == "RUN_ERROR" + tape = adapter._storage_agent.tape( + session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) + ) + entries = list(await tape.query().all()) + assert [e.payload["name"] for e in entries if e.kind == "anchor"] == [ + "session/start" + ] + assert not any( + e.payload.get("name") in {"loop.start", "loop.finish"} for e in entries + ) + + +async def test_step_limit_still_bounds_backend_tool_loop(setup): + provider, make = setup + adapter = make([Tool.from_callable(lambda: "clock", name="clock")]) + + class Limit: + @hookimpl + def load_state(self, message): + message._runtime_agent.max_steps = 1 + + adapter.framework.plugin_manager.register(Limit(), "one-step") + provider.reply(calls=[("c1", "clock", "{}")]) + _, events = await collect(adapter, request()) + assert len(provider.requests) == 1 + assert events[-1]["type"] == "RUN_ERROR" + assert len([e for e in events if e["type"] == "TOOL_CALL_RESULT"]) == 1 + + +async def test_native_preparation_sees_shared_state_updates_on_next_step(setup): + from bub_ag_ui.state import shared_state + + provider, make = setup + + async def update(context): + await shared_state(context).set({"count": 1}) + return "updated" + + adapter = make([Tool.from_callable(update, context=True)]) + provider.reply(calls=[("u1", "update", "{}")]) + provider.reply("done") + data = request() + data.state = {"count": 0} + _, events = await collect(adapter, data) + assert events[-1]["type"] == "RUN_FINISHED" + assert '"count": 0' in provider.requests[0]["messages"][0]["content"] + assert '"count": 1' in provider.requests[1]["messages"][0]["content"] From 859ec1503789f447e17c96171c4a1cdb8a633b96 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 10 Nov 2026 23:30:14 +0800 Subject: [PATCH 05/10] refactor(bub-ag-ui): attach protocol requests to native run_stream --- packages/bub-ag-ui/README.md | 49 ++- .../bub-ag-ui/src/bub_ag_ui/continuation.py | 10 +- packages/bub-ag-ui/src/bub_ag_ui/execution.py | 168 +++++++-- packages/bub-ag-ui/src/bub_ag_ui/lifecycle.py | 18 - packages/bub-ag-ui/src/bub_ag_ui/runtime.py | 337 ++++++++++++------ packages/bub-ag-ui/src/bub_ag_ui/tools.py | 109 ++---- packages/bub-ag-ui/src/bub_ag_ui/turn.py | 184 ---------- .../bub-ag-ui/tests/client/protocol.test.mjs | 7 +- packages/bub-ag-ui/tests/conftest.py | 8 +- packages/bub-ag-ui/tests/test_execution.py | 14 +- .../bub-ag-ui/tests/test_native_stream.py | 314 ++++++++++++++++ packages/bub-ag-ui/tests/test_runtime.py | 32 +- packages/bub-ag-ui/tests/test_session_tape.py | 7 +- packages/bub-ag-ui/tests/test_tools.py | 13 +- packages/bub-ag-ui/tests/test_turn_bridge.py | 132 ------- 15 files changed, 780 insertions(+), 622 deletions(-) delete mode 100644 packages/bub-ag-ui/src/bub_ag_ui/turn.py create mode 100644 packages/bub-ag-ui/tests/test_native_stream.py delete mode 100644 packages/bub-ag-ui/tests/test_turn_bridge.py diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index 20e10db..7f82269 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -55,7 +55,8 @@ The frontend renders the streamed updates. `HttpAgent` also accepts a `headers` option for authentication. Bub owns the conversation and chooses model context from its native session tape. -The adapter appends only new user messages and results for pending external calls. +The adapter passes new user input to the native stream and resolves waiting tools +with client results. Bub records the complete model/tool exchanges. Keep message IDs stable when echoing history: already accepted input is not appended again, and echoed assistant messages or old tool results do not replace Bub's records. An edit to an accepted user message is rejected; this adapter does @@ -63,9 +64,10 @@ not implement edit/regenerate branches. Start a new thread for a new conversatio Your application can save messages, shared state, and the thread ID to restore its UI through `initialMessages` and `initialState`. This does not import that -snapshot into Bub or restore deleted server context. Continuing after a server -restart requires a persistent Bub tape store. Keep any interrupt outcome needed -by your decision UI as well. There are no `/threads/...` history, replay, or cancel +snapshot into Bub or restore deleted server context. A persistent Bub tape store +preserves completed conversation context across server restarts, but cannot +restore an unfinished tool call. Keep any interrupt outcome needed by your +decision UI as well. There are no `/threads/...` history, replay, or cancel endpoints in this adapter. ## Frontend tools @@ -80,8 +82,9 @@ When a run requests frontend tools: 2. Add a message for each result, with a new `id`, `role: "tool"`, the matching `toolCallId`, and `content`. 3. Start a new run on the same thread, sending the results, shared state, and - tool definitions. Include a result for every pending frontend call. Bub already - has the suspended exchange; a result-only request does not need to echo it. + unchanged tool definitions. Include a result for every pending frontend call. + This request attaches to the same live native stream; a result-only request + does not need to echo the suspended exchange. No synthetic user message is needed. **`RUN_FINISHED` ends one run; it may mean that the agent is waiting for your frontend, not that it has finished answering.** @@ -112,10 +115,18 @@ A resolved payload must match the declared response schema, if any. Missing, partial, unknown, or invalid responses produce `RUN_ERROR`; they do not execute the model. Do not send an interrupt response as an ordinary tool-result message. -Bub records a small native checkpoint for external-call correlation and consumed -interrupt responses. Repeating the same accepted interrupt responses does not -re-execute resumed work; it is not a replay of the earlier generated answer. -Continuation across a server restart requires a persistent Bub tape store. +External tools await client results inside the live native execution. Waiting +calls are process-local: resume them on the same adapter before its turn timeout +expires. A server restart, adapter shutdown, or timeout discards that waiting +execution; sending its results later produces `RUN_ERROR`, not a reconstructed run. +The default `max_run_seconds` is 600 seconds for the entire native turn, including +client waiting time. In a mixed tool batch, Bub emits and records results together +after all frontend and backend calls complete. + +The adapter records consumed interrupt responses on the native tape. Repeating +the same accepted responses does not re-execute resumed work; it is not a replay +of the earlier generated answer. This receipt can survive a restart with a +persistent tape store, but does not make a pending execution restartable. Each thread uses one native tape. Bub continues to own its records, session settings, loaded-tool visibility, and handoff anchors. The adapter neither resets @@ -128,7 +139,9 @@ handoff. - Protocol execution failures arrive as `RUN_ERROR`, not `RUN_FINISHED`. Authentication, malformed request bodies, and body-size limits use HTTP errors. - `agent.abortRun()` aborts the HTTP request and cancels its execution. It does - not undo side effects of tools that have already run. + not undo side effects of tools that have already run. Once a paused HTTP run + has finished, aborting that old request cannot cancel the waiting native turn; + resolve its calls (or cancel its interrupts through `resume`) before timeout. - Another executing request on the same thread is rejected with `RUN_ERROR`. Run one adapter per store/workspace; cross-process coordination is not provided. - Reusing a run ID does not request replay or provide general request deduplication. @@ -136,13 +149,13 @@ handoff. Reconnecting does not restart interrupted work. Retry policy belongs to the application; consider tool side effects before submitting work again. -The pinned Bub API still requires a small step bridge for result-only continuation -and returning control to frontend tools. Native `run_once` prepares and executes -each model/tool step; native steering and subagent-result hooks remain active. -The adapter does not implement its own context-overflow handoff/retry policy: -uncaught context overflow produces `RUN_ERROR`. Native context-budget hooks and -explicit handoff still apply. It does not synthesize Bub full-run lifecycle events; -AG-UI run completion is owned by the request boundary. +Execution uses the framework's native `BubAgent.run_stream` path. Bub owns the +model loop, step limits, steering, subagent delivery, automatic overflow handoff, +and conversation recording. The adapter wraps the stream to translate events and +attach client tool results; it does not run its own model-step loop or fabricate +user messages. An AG-UI run ends at an HTTP request boundary, while one native +turn may span several requests. Optional AG-UI `STEP_*` events are not emitted; +Bub's native step/run events remain available to server hooks. ## Embedding and development diff --git a/packages/bub-ag-ui/src/bub_ag_ui/continuation.py b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py index 867399a..0d37ac9 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/continuation.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py @@ -1,4 +1,4 @@ -"""Native tape checkpoint for external calls, not a display history or run journal.""" +"""Validate live external calls and persist consumed interrupt receipts only.""" from __future__ import annotations @@ -24,13 +24,15 @@ async def load(cls, tape: Tape) -> Continuation: entries = await tape.query().kinds("event").all() for entry in reversed(list(entries)): if entry.payload.get("name") == "ag_ui.continuation": - return cls(**deepcopy(entry.payload["data"])) + return cls( + responses=deepcopy(entry.payload["data"].get("responses", [])) + ) return cls() async def save(self, tape: Tape) -> None: await tape.append_event( "ag_ui.continuation", - {"pending": deepcopy(self.pending), "responses": deepcopy(self.responses)}, + {"responses": deepcopy(self.responses)}, ) def prepare(self, request: RunAgentInput) -> tuple[list[dict[str, Any]], bool]: @@ -82,7 +84,7 @@ def prepare(self, request: RunAgentInput) -> tuple[list[dict[str, Any]], bool]: if response["status"] == "resolved" and call.get("response_schema"): jsonschema.validate(response["payload"], call["response_schema"]) # Update the client view if it echoes the call. Native execution - # already recorded that exchange and does not require it back. + # holds that exchange and does not require it back. index = next( ( i + 1 diff --git a/packages/bub-ag-ui/src/bub_ag_ui/execution.py b/packages/bub-ag-ui/src/bub_ag_ui/execution.py index 876af44..ab93f34 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/execution.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/execution.py @@ -1,31 +1,40 @@ -"""Scoped hooks and event handlers for a complete AG-UI inbound turn. - -The framework owns load/build/run/save/render/dispatch. This plugin supplies a -model-stream hook for already-persisted protocol input, using public session and -step APIs. It does not override Agent.start or bypass the inbound lifecycle. -""" +"""Attach protocol output to the framework-owned native model stream.""" from __future__ import annotations +import asyncio +import json from collections.abc import AsyncIterable, AsyncIterator from copy import deepcopy -from dataclasses import dataclass +from dataclasses import dataclass, replace from pathlib import Path from typing import Any +import republic from ag_ui.core import RunAgentInput from bub import hookimpl from bub.builtin.agent import BubAgent from bub.builtin.settings import load_session_settings +from bub.builtin.tools import ALLOWED_TOOLS_STATE_KEY from bub.channels.message import ChannelMessage +from bub.core.events import ( + AgentEvent, + ChatFinished, + CommandFinished, + LLMCallStarted, + MessagesPending, + RunFinished, + RunStarted, + ToolCallFinished, +) from bub.envelope import Envelope, field_of -from bub.streaming import AsyncStreamEvents, StreamEvent +from bub.streaming import StreamEvent from bub.turn import TurnState from bub_ag_ui.events import EventStream +from bub_ag_ui.lifecycle import committed from bub_ag_ui.state import SharedState from bub_ag_ui.tools import ExternalTools -from bub_ag_ui.turn import stream_turn @dataclass(kw_only=True) @@ -37,6 +46,8 @@ class AGUIInboundMessage(ChannelMessage): shared: SharedState events: EventStream external: ExternalTools + prompt: list[Any] + execution: Any stream_complete: bool = False @@ -49,15 +60,127 @@ def stream_events( async def iterate() -> AsyncIterator[StreamEvent]: async for event in stream: - await message.events.consume(event) + await message.execution.consume(event) yield event if error := getattr(stream, "error", None): raise error message.stream_complete = True + message.execution.finishing = False + timeout = message.execution.timeout + if timeout is not None and not timeout.expired(): + timeout.reschedule(message.execution.deadline) return iterate() +class NativeExecution: + """A live native stream, with a replaceable HTTP event sink. + + External calls await real futures inside Bub's executor. Detaching an HTTP + response does not end, restart, or reconstruct the native execution. + """ + + def __init__(self, inbound: AGUIInboundMessage, accepted: dict[str, Any]) -> None: + self.task: asyncio.Task[None] + self.timeout: asyncio.Timeout | None = None + self.finishing = False + self.deadline: float | None = None + self.inbound = inbound + self.stream: EventStream | None = inbound.events + self.accepted = accepted + self.unrecorded = dict(accepted) + self.queued: list[list[Any]] = [] + self.queued_receipts: dict[str, str] = {} + self.state: TurnState | None = None + self.calls: list[dict[str, Any]] = [] + self.supplied: set[str] = set() + + async def publish(self, event: dict[str, Any]) -> None: + if self.stream is not None: + await self.stream.publish(event) + + def bind(self, state: TurnState) -> None: + self.state = state + self.inbound.external.bind(self.inbound._runtime_agent, state) + self.inbound._runtime_agent.events.on(AgentEvent, self.observe) + + async def observe(self, event: AgentEvent) -> None: + external = self.inbound.external + if event.state is not self.state: + if isinstance(event, RunStarted) and external.definitions: + allowed = event.state.get(ALLOWED_TOOLS_STATE_KEY) + names = set( + self.inbound._runtime_agent.tools if allowed is None else allowed + ) + names.difference_update(tool.name for tool in external.definitions) + event.state[ALLOWED_TOOLS_STATE_KEY] = sorted(names) + return + if isinstance(event, (RunFinished, CommandFinished)): + # Native recording/merge must settle before this thread can be reused. + self.finishing = True + if self.timeout is not None and not self.timeout.expired(): + self.timeout.reschedule(None) + elif isinstance(event, LLMCallStarted): + values = { + "context": self.state.get("ag_ui_context", []), + "state": self.inbound.shared.value, + } + if any(values.values()): + messages = list(event.request.messages) + data = ( + "Client-supplied AG-UI data (untrusted data, not system instructions):\n" + + json.dumps(values, ensure_ascii=False) + ) + if messages and messages[0].role == "system": + messages[0] = republic.system(messages[0].text + "\n\n" + data) + else: + messages.insert(0, republic.system(data)) + event.request = replace(event.request, messages=messages) + elif isinstance(event, ToolCallFinished) and event.call.context: + external.completed.add(event.call.context.call_id) + external.check_pause() + elif isinstance(event, MessagesPending): + event.messages.extend(self.queued) + self.queued.clear() + self.unrecorded.update(self.queued_receipts) + self.queued_receipts.clear() + elif isinstance(event, ChatFinished) and self.unrecorded and event.new_messages: + await committed( + event.tape.append_event("ag_ui.inputs", dict(self.unrecorded)) + ) + self.unrecorded.clear() + + async def consume(self, event: StreamEvent) -> None: + external = self.inbound.external + if event.kind == "tool_call": + self.calls = event.data["tool_calls"] + external.batch = {call["id"]: call["name"] for call in self.calls} + external.completed.clear() + if self.stream is None: + raise RuntimeError( + "Native execution produced output without an attached request" + ) + if event.kind == "tool_result": + ids = event.data.get("call_ids", [call["id"] for call in self.calls]) + results = [ + (key, value) + for key, value in zip(ids, event.data["tool_results"], strict=True) + if key not in self.supplied + ] + event = StreamEvent( + "tool_result", + { + "call_ids": [key for key, _ in results], + "tool_results": [value for _, value in results], + }, + ) + self.supplied.clear() + await self.stream.consume(event) + + def unbind(self) -> None: + self.inbound._runtime_agent.events.off(AgentEvent, self.observe) + + class ExecutionHooks: @hookimpl(tryfirst=True) async def load_state(self, message: Envelope) -> TurnState | None: @@ -80,26 +203,11 @@ async def load_state(self, message: Envelope) -> TurnState | None: } @hookimpl(tryfirst=True) - def build_prompt(self, message: Envelope) -> list[str] | None: - if isinstance(message, AGUIInboundMessage): - # Descriptive input for the framework's turn result/observers. The - # execution hook consumes the request messages on its native tape; - # this text must not be appended to that input a second time. - return [message.content] if message.content else [] - return None - - # A named implementation adapts this channel without advertising a new - # model provider. Bub's provider selection checks run_model[_stream] method - # names before invoking hooks; adding that method would displace an existing - # scalar provider even when this scoped implementation returned None. - @hookimpl(specname="run_model_stream", tryfirst=True) - def stream_protocol_turn(self, state: TurnState) -> AsyncStreamEvents | None: - inbound = state.get("_ag_ui_inbound") - return ( - stream_turn(inbound, state) - if isinstance(inbound, AGUIInboundMessage) - else None - ) + def build_prompt(self, message: Envelope, state: TurnState) -> list[Any] | None: + if not isinstance(message, AGUIInboundMessage): + return None + message.execution.bind(state) + return message.prompt class StreamRouter: diff --git a/packages/bub-ag-ui/src/bub_ag_ui/lifecycle.py b/packages/bub-ag-ui/src/bub_ag_ui/lifecycle.py index f93d363..184fa24 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/lifecycle.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/lifecycle.py @@ -3,10 +3,7 @@ from __future__ import annotations import asyncio -import sys from collections.abc import Awaitable -from contextlib import asynccontextmanager -from typing import Any async def committed[T](operation: Awaitable[T]) -> T: @@ -21,18 +18,3 @@ async def committed[T](operation: Awaitable[T]) -> T: except asyncio.CancelledError: await task raise - - -@asynccontextmanager -async def tape_session(agent: Any, name: str, state: dict[str, Any]): - """Close/merge the native fork even if cancellation arrives during merge.""" - session = agent.session(name, state=state) - tape = await session.__aenter__() - try: - yield tape - except BaseException: - suppressed = await committed(session.__aexit__(*sys.exc_info())) - if not suppressed: - raise - else: - await committed(session.__aexit__(None, None, None)) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py index af7a6aa..e3aa75a 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py @@ -10,15 +10,18 @@ from pathlib import Path from typing import Any +import anyio import jsonschema from ag_ui.core import RunAgentInput from bub.builtin.agent import BubAgent -from bub.core.tape import Tape, TapeEntry, session_tape_name, to_message +from bub.core.tape import Tape, session_tape_name, to_message +from bub.prompt import to_content from bub_ag_ui.continuation import Continuation from bub_ag_ui.events import EventStream, result_text from bub_ag_ui.execution import ( AGUIInboundMessage, + NativeExecution, StreamRouter, install_execution_hooks, ) @@ -73,44 +76,45 @@ def model_message(message: dict[str, Any]) -> dict[str, Any] | None: return result +def input_digest(message: dict[str, Any]) -> str: + return hashlib.sha256( + json.dumps(message, sort_keys=True, ensure_ascii=False).encode() + ).hexdigest() + + async def new_inputs( - tape: Tape, messages: list[dict[str, Any]], pending_ids: set[str] + tape: Tape, messages: list[dict[str, Any]], accepted: dict[str, str] ) -> list[tuple[str, dict[str, Any]]]: - """Map AG-UI input to native appends, never rebuild context from UI history. - - IDs on native input entries distinguish new messages from client echoes, - including echoes of messages that Bub has already moved behind a handoff. - Assistant output and backend tool results belong to the native recorder. - """ - accepted = { - entry.meta["ag_ui_message_id"]: entry.payload - for entry in await tape.query().kinds("message").all() - if "ag_ui_message_id" in entry.meta - } + """Deduplicate protocol input without importing client history into Bub.""" + known = dict(accepted) + for entry in await tape.query().kinds("event").all(): + if entry.payload.get("name") == "ag_ui.inputs": + known.update(entry.payload["data"]) + # Read receipts written by older versions without rewriting native messages. + for entry in await tape.query().kinds("message").all(): + if message_id := entry.meta.get("ag_ui_message_id"): + known[message_id] = input_digest(entry.payload) additions = [] for message in messages: - if message["role"] != "user" and not ( - message["role"] == "tool" and message["toolCallId"] in pending_ids - ): + if message["role"] != "user": continue converted = model_message(message) - if message["id"] in accepted: - if accepted[message["id"]] != converted: + if message["id"] in known: + if known[message["id"]] != input_digest(converted): raise ProtocolError( "Editing accepted input is not supported; start a new thread" ) continue additions.append((message["id"], converted)) - # Resolve the native pending exchange before any new user input sent with it. - return sorted(additions, key=lambda item: item[1]["role"] != "tool") + return additions class AGUIAdapter: """Adapt a request and its native execution to an AG-UI event stream. - Bub owns the conversation tape and its context selection. The adapter appends - only new user input or results for pending external calls. Client history is - not a replacement for native context; streaming never polls stored events. + Bub owns the conversation tape, context, and entire model loop. An HTTP + request attaches to a live native turn; external tools wait inside that turn. + Only input/interrupt receipts are adapter-owned, never model exchanges. """ def __init__( @@ -135,6 +139,7 @@ def __init__( self._active: set[str] = set() self._tasks: set[asyncio.Task[None]] = set() self._closed = False + self._turns: dict[str, NativeExecution] = {} def session_id(self, principal: str, thread_id: str) -> str: identity = json.dumps([principal, thread_id], ensure_ascii=False) @@ -143,8 +148,8 @@ def session_id(self, principal: str, thread_id: str) -> str: async def run( self, principal: str, request: RunAgentInput ) -> AsyncIterator[dict[str, Any]]: - # Bounded, transient delivery only. No event IDs, replay buffers, durable - # wire journal, or global run registry. Closing this iterator owns cleanup. + # Bounded delivery, not a replay journal. Only an explicitly paused + # native turn may outlive this HTTP attachment. queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue(maxsize=16) disconnected = False @@ -170,19 +175,58 @@ async def emit(event: dict[str, Any]) -> None: finally: if not next_event.done(): next_event.cancel() - await asyncio.gather(next_event, return_exceptions=True) + with anyio.CancelScope(shield=True): + await asyncio.gather(next_event, return_exceptions=True) await producer finally: disconnected = True - if not producer.done(): - producer.cancel() - await asyncio.gather(producer, return_exceptions=True) - self._tasks.discard(producer) + with anyio.CancelScope(shield=True): + if not producer.done() and not producer.cancelling(): + producer.cancel() + await asyncio.gather(producer, return_exceptions=True) + self._tasks.discard(producer) + + async def _run_native(self, execution: NativeExecution) -> None: + inbound = execution.inbound + try: + async with asyncio.timeout(self.max_run_seconds) as timeout: + execution.timeout = timeout + execution.deadline = timeout.when() + await self.framework.process_inbound(inbound, stream_output=True) + if not inbound.stream_complete: + raise RuntimeError( + "AG-UI stream did not complete; check channel configuration" + ) + # Commands and custom model hooks need not emit ChatFinished. + if execution.unrecorded: + tape = inbound._runtime_agent.tape(inbound.model_tape) + await committed( + tape.append_event("ag_ui.inputs", dict(execution.unrecorded)) + ) + execution.unrecorded.clear() + finally: + execution.unbind() + + def _start(self, session_id: str, execution: NativeExecution) -> None: + self._turns[session_id] = execution + execution.task = asyncio.create_task(self._run_native(execution)) + self._tasks.add(execution.task) + + def finished(task: asyncio.Task[None]) -> None: + self._tasks.discard(task) + if self._turns.get(session_id) is execution: + del self._turns[session_id] + if not task.cancelled(): + task.exception() # A detached timeout still has its exception observed. + + execution.task.add_done_callback(finished) async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> None: stream = EventStream(emit) session_id = self.session_id(principal, request.thread_id) acquired = False + execution: NativeExecution | None = None + detached = False try: await stream.send( "RUN_STARTED", @@ -200,65 +244,113 @@ async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> N raise ProtocolError("Another request is executing on this thread") self._active.add(session_id) acquired = True - async with asyncio.timeout(self.max_run_seconds): - tape = self._storage_agent.tape( - session_tape_name(session_id, self.workspace) + tape = self._storage_agent.tape( + session_tape_name(session_id, self.workspace) + ) + live = self._turns.get(session_id) + continuation = await Continuation.load(tape) + # Persisted receipts do not resurrect a suspended coroutine. + continuation.pending = list(live.inbound.external.pending) if live else [] + try: + messages, repeated = continuation.prepare(request) + additions = await new_inputs( + tape, messages, live.accepted if live else {} ) - continuation = await Continuation.load(tape) - try: - pending_ids = {call["id"] for call in continuation.pending} - messages, repeated = continuation.prepare(request) - agent = self.agent_factory(self.framework, tape_store=tape.store) - external = ExternalTools([*request.tools, *self.interrupts]) + if repeated and additions: + raise ProtocolError( + "Send new input without an already resolved resume" + ) + if live: + if not live.inbound.external.paused.is_set(): + raise ProtocolError( + "Another request is executing on this thread" + ) + if request.tools != live.inbound.request.tools: + raise ProtocolError( + "Tool definitions cannot change while a native turn is waiting" + ) + elif not repeated: + if not additions or ( + request.messages and request.messages[-1].role == "tool" + ): + raise ProtocolError( + "No new user input or live external call; waiting calls cannot survive a server restart" + ) + external = ( + live.inbound.external + if live + else ExternalTools([*request.tools, *self.interrupts]) + ) + agent = ( + live.inbound._runtime_agent + if live + else self.agent_factory(self.framework, tape_store=tape.store) + ) + values = { + m["toolCallId"]: model_message(m)["content"] + for m in messages + if m["role"] == "tool" and m["toolCallId"] in external.futures + } + if not live: external.validate(agent) for definition in external.definitions: jsonschema.Draft202012Validator.check_schema( definition.parameters ) - additions = await new_inputs(tape, messages, pending_ids) - if repeated and additions: - raise ProtocolError( - "Send new input without an already resolved resume" - ) - if not additions and not repeated: - raise ProtocolError("No new user input or pending tool result") - except ( - ValueError, - jsonschema.ValidationError, - jsonschema.SchemaError, - ) as exc: - raise ProtocolError(str(exc)) from exc - await stream.send("MESSAGES_SNAPSHOT", messages=messages) - shared = SharedState( - request.state if request.state is not None else {}, stream.publish + except ( + ValueError, + jsonschema.ValidationError, + jsonschema.SchemaError, + ) as exc: + raise ProtocolError(str(exc)) from exc + await stream.send("MESSAGES_SNAPSHOT", messages=messages) + if repeated: + await stream.send( + "STATE_SNAPSHOT", + snapshot=request.state if request.state is not None else {}, ) - await shared.set(shared.value) - if not repeated: + else: + receipts = {key: input_digest(value) for key, value in additions} + if live: + execution = live + execution.stream = stream + execution.state["ag_ui_context"] = [ + item.model_dump(mode="json") for item in request.context + ] + execution.state["ag_ui_forwarded_props"] = request.forwarded_props + execution.accepted.update(receipts) + execution.queued_receipts.update(receipts) + execution.queued.extend( + to_content(value["content"]) for _, value in additions + ) + shared = execution.inbound.shared + await shared.set(request.state if request.state is not None else {}) + execution.supplied = set(values) + await committed(continuation.save(tape)) + external.resolve(values) + else: - async def append_input() -> None: - await tape.ensure_bootstrap_anchor() - if pending_ids: - # Record receipt before executing any resumed side effect. - await continuation.save(tape) - for message_id, message in additions: - await tape.store.append( - tape.name, - TapeEntry.message(message, ag_ui_message_id=message_id), - ) + async def publish(event: dict[str, Any]) -> None: + if execution is None: + await stream.publish(event) + else: + await execution.publish(event) - await committed(append_input()) + shared = SharedState( + request.state if request.state is not None else {}, publish + ) + await shared.set(shared.value) + prompt = [ + part + for _, value in additions + for part in to_content(value["content"]) + ] inbound = AGUIInboundMessage( session_id=session_id, channel="ag-ui", chat_id=request.thread_id, - content=next( - ( - m.get("content", "") - for m in reversed(messages) - if m["role"] == "user" - and isinstance(m.get("content"), str) - ), - "", + content="\n".join( + part for part in prompt if isinstance(part, str) ), is_active=True, _runtime_agent=agent, @@ -268,41 +360,51 @@ async def append_input() -> None: shared=shared, events=stream, external=external, + prompt=prompt, + execution=None, ) - await self.framework.process_inbound(inbound, stream_output=True) - if not inbound.stream_complete: - raise RuntimeError( - "AG-UI stream did not complete; check channel configuration" - ) - continuation.pending = external.pending - if external.pending: - await committed(continuation.save(tape)) - await stream.close() - interrupts = [ - { - "id": c["id"], - "toolCallId": c["id"], - "reason": c["reason"], - "responseSchema": c.get("response_schema"), - "metadata": { - "arguments": json.loads(c["arguments"]), - "toolName": c["name"], - }, - } - for c in continuation.pending - if c["kind"] == "interrupt" - ] - fields: dict[str, Any] = {} - if interrupts: - await stream.snapshot() - await shared.set(shared.value) - fields["outcome"] = {"type": "interrupt", "interrupts": interrupts} - await stream.send( - "RUN_FINISHED", - threadId=request.thread_id, - runId=request.run_id, - **fields, - ) + execution = NativeExecution(inbound, receipts) + inbound.execution = execution + self._start(session_id, execution) + waiter = asyncio.create_task(external.paused.wait()) + try: + await asyncio.wait( + (execution.task, waiter), return_when=asyncio.FIRST_COMPLETED + ) + if execution.task.done(): + await execution.task + else: + continuation.pending = list(external.pending) + finally: + waiter.cancel() + await asyncio.gather(waiter, return_exceptions=True) + await stream.close() + interrupts = [ + { + "id": c["id"], + "toolCallId": c["id"], + "reason": c["reason"], + "responseSchema": c.get("response_schema"), + "metadata": { + "arguments": json.loads(c["arguments"]), + "toolName": c["name"], + }, + } + for c in continuation.pending + if c["kind"] == "interrupt" + ] + fields: dict[str, Any] = {} + if interrupts: + await stream.snapshot() + await execution.inbound.shared.set(execution.inbound.shared.value) + fields["outcome"] = {"type": "interrupt", "interrupts": interrupts} + await stream.send( + "RUN_FINISHED", + threadId=request.thread_id, + runId=request.run_id, + **fields, + ) + detached = bool(continuation.pending) except asyncio.CancelledError: raise except Exception as exc: @@ -317,11 +419,20 @@ async def append_input() -> None: message = "Agent run timed out" await stream.send("RUN_ERROR", message=message, code="RUN_FAILED") finally: + if execution is not None: + execution.stream = None + if not detached and not execution.task.done(): + if not execution.finishing and not execution.task.cancelling(): + execution.task.cancel() + await asyncio.gather(execution.task, return_exceptions=True) if acquired: self._active.discard(session_id) async def close(self) -> None: self._closed = True - for task in list(self._tasks): - task.cancel() - await asyncio.gather(*list(self._tasks), return_exceptions=True) + tasks = list(self._tasks) + merging = {turn.task for turn in self._turns.values() if turn.finishing} + for task in tasks: + if task not in merging and not task.cancelling(): + task.cancel() + await asyncio.gather(*tasks, return_exceptions=True) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/tools.py b/packages/bub-ag-ui/src/bub_ag_ui/tools.py index 788c6ac..cf01f93 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/tools.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/tools.py @@ -2,21 +2,16 @@ from __future__ import annotations +import asyncio import json from dataclasses import dataclass, field, replace from typing import Any from ag_ui.core import Tool as ClientTool from bub.builtin.agent import BubAgent -from bub.core.events import ChatFinished, LLMCallFinished, ToolCallFinished -from bub.core.tape import Tape, TapeEntry from bub.core.tools import Tool, ToolContext, tool -from bub.errors import BubError -from bub.streaming import StreamEvent from bub.turn import TurnState -from bub_ag_ui.events import result_text - @dataclass(frozen=True) class InterruptTool: @@ -29,21 +24,24 @@ class InterruptTool: reason: str = "human_input" -class ExternalCallsPending(BaseException): - """Control transfer, not a failed tool result or task cancellation. - - Bub's executor waits for the entire parallel batch before propagating this. - Ordinary tool exceptions would instead be normalized into model-facing errors. - Only an admitted native tool handler can request this transfer: deny/replace - decisions never invoke the handler and therefore never create pending work. - """ - - @dataclass class ExternalTools: definitions: list[ClientTool | InterruptTool] pending: list[dict[str, Any]] = field(default_factory=list) - context: ToolContext | None = None + futures: dict[str, asyncio.Future[Any]] = field(default_factory=dict) + batch: dict[str, str] = field(default_factory=dict) + completed: set[str] = field(default_factory=set) + paused: asyncio.Event = field(default_factory=asyncio.Event) + + def check_pause(self) -> None: + if self.pending and self.batch.keys() <= self.completed | self.futures.keys(): + self.paused.set() + + def resolve(self, values: dict[str, Any]) -> None: + self.paused.clear() + self.pending.clear() + for call_id, value in values.items(): + self.futures.pop(call_id).set_result(value) def validate(self, agent: BubAgent) -> None: names = {name.replace(".", "_") for name in agent.tools} @@ -68,12 +66,18 @@ def _tool(self, definition: ClientTool | InterruptTool, state: TurnState) -> Too context=True, exposure="direct", ) - def suspend(*, context: ToolContext, **arguments: Any) -> None: + async def suspend(*, context: ToolContext, **arguments: Any) -> Any: if context.state is not state: raise ValueError( "Frontend/interrupt tools belong to the owning AG-UI turn" ) - self.context = context + if ( + self.batch.get(context.call_id, "").replace(".", "_") + != definition.name.replace(".", "_") + or context.command + or context.code_mode + ): + raise ValueError("External tools require a direct model tool call") call = { "id": context.call_id, "name": definition.name, @@ -87,69 +91,10 @@ def suspend(*, context: ToolContext, **arguments: Any) -> None: response_schema=definition.response_schema, ) self.pending.append(call) - raise ExternalCallsPending() + future = asyncio.get_running_loop().create_future() + self.futures[context.call_id] = future + self.check_pause() + return await future # Client schemas are supplied at runtime, rather than by Python annotations. return replace(suspend, parameters=definition.parameters) - - async def record_pause( - self, - agent: BubAgent, - tape: Tape, - completed: LLMCallFinished | None, - results: dict[str, ToolCallFinished], - system_prompt: str | None, - reasoning: list[str], - ) -> StreamEvent | None: - """Record only the exchange interrupted by an admitted external call. - - Normal exchanges are recorded by run_once. Its tool executor settles - the whole batch before propagating our control transfer, so completed - backend results are available here without fabricating pending results. - """ - if completed is None or self.context is None: - raise RuntimeError("External call without a model exchange") - request, response = completed.request, completed.result - await agent.events.emit( - ChatFinished( - state=tape.context.state, - tape=tape, - run_id=request.run_id, - model=request.model, - system_prompt=system_prompt, - new_messages=self.context.step_messages or [], - response_text=response.text, - tool_calls=response.tool_calls, - usage=response.usage, - assistant_fields={"reasoning": "".join(reasoning)} if reasoning else {}, - ) - ) - ids, values = [], [] - for call in response.tool_calls: - if outcome := results.get(call["id"]): - result = outcome.result - value = result.result - if result.error is not None: - value = ( - BubError(result.error.kind, result_text(value)) - if value is not None and isinstance(result.error, BubError) - else result.error - ) - ids.append(call["id"]) - values.append(value) - await tape.store.append( - tape.name, - TapeEntry.message( - { - "role": "tool", - "tool_call_id": call["id"], - "content": result_text(value), - }, - run_id=request.run_id, - ), - ) - return ( - StreamEvent("tool_result", {"call_ids": ids, "tool_results": values}) - if values - else None - ) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/turn.py b/packages/bub-ag-ui/src/bub_ag_ui/turn.py deleted file mode 100644 index d7880f1..0000000 --- a/packages/bub-ag-ui/src/bub_ag_ui/turn.py +++ /dev/null @@ -1,184 +0,0 @@ -"""The minimal public-step bridge required for AG-UI return-to-client calls. - -Bub's pinned full-run entry requires a new user prompt and has no external-tool -pause outcome. Keep one bounded step path here; do not copy its overflow/handoff -policy, call private loop methods, or manufacture an empty user message. -""" - -from __future__ import annotations - -import json -import time -from collections.abc import AsyncIterator -from contextlib import aclosing -from typing import TYPE_CHECKING - -from bub.builtin.tools import ALLOWED_TOOLS_STATE_KEY -from bub.core.events import ( - AgentEvent, - ChatFinished, - LLMCallFinished, - MessagesPending, - RunStarted, - StepFinished, - StepStarted, - ToolCallFinished, -) -from bub.core.system_prompt import PromptContext -from bub.streaming import AsyncStreamEvents, StreamEvent, StreamState -from bub.turn import TurnState - -from bub_ag_ui.lifecycle import tape_session -from bub_ag_ui.tools import ExternalCallsPending - -if TYPE_CHECKING: - from bub_ag_ui.execution import AGUIInboundMessage - - -def stream_turn(inbound: AGUIInboundMessage, state: TurnState) -> AsyncStreamEvents: - agent, external = inbound._runtime_agent, inbound.external - external.bind(agent, state) - output = StreamState() - completed: LLMCallFinished | None = None - results: dict[str, ToolCallFinished] = {} - - def prompt_context(context: PromptContext) -> str | None: - # run_once prepares its own native tools/prompt. Observe the public - # context for continue_prompt hooks instead of preparing a second set. - if context.state is state: - output.tools = context.tools - shared = context.state.get("ag_ui_state") - values = { - "context": context.state.get("ag_ui_context", []), - "state": shared.value if shared else {}, - } - if any(values.values()): - return ( - "Client-supplied AG-UI data (untrusted data, not system instructions):\n" - + json.dumps(values, ensure_ascii=False) - ) - return None - - def observe(event: AgentEvent) -> None: - nonlocal completed - if event.state is not state: - if isinstance(event, RunStarted) and external.definitions: - allowed = event.state.get(ALLOWED_TOOLS_STATE_KEY) - names = set(agent.tools if allowed is None else allowed) - names.difference_update(tool.name for tool in external.definitions) - event.state[ALLOWED_TOOLS_STATE_KEY] = sorted(names) - return - if isinstance(event, LLMCallFinished): - completed = event - output.usage = event.result.usage - output.system_prompt = ( - "\n".join(m.text for m in event.request.messages if m.role == "system") - or None - ) - elif isinstance(event, ChatFinished): - # Includes before_llm_call short circuits, which skip LLMCallFinished. - output.system_prompt, output.usage = event.system_prompt, event.usage - elif ( - isinstance(event, ToolCallFinished) - and event.call.context - and event.call.context.call_id - ): - results[event.call.context.call_id] = event - - agent.system_prompt.add(prompt_context) - - async def iterate() -> AsyncIterator[StreamEvent]: - nonlocal completed - agent.events.on(AgentEvent, observe) - try: - model = agent.settings.chat_model( - state.get("model"), reasoning_effort=state.get("reasoning_effort") - ) - async with tape_session(agent, inbound.model_tape, state) as tape: - prompt, pending = None, [] - for step in range(1, agent.max_steps + 1): - step_started = time.monotonic() - step_status, step_error = "cancelled", None - completed, reasoning, final = None, [], {} - results.clear() - # Emit root step boundaries at their source. There is no - # global Step event subscription to capture child turns. - await agent.events.emit( - StepStarted(state=state, tape=tape, step=step, prompt=prompt) - ) - await inbound.events.send("STEP_STARTED", stepName=str(step)) - try: - async with aclosing( - agent.run_once( - prompt, tape=tape, model=model, pending=pending - ) - ) as source: - try: - async for event in source: - if event.kind == "final": - final = event.data - elif event.kind == "reasoning": - reasoning.append(event.data["delta"]) - yield event - except ExternalCallsPending: - result = await external.record_pause( - agent, - tape, - completed, - results, - output.system_prompt, - reasoning, - ) - if result is not None: - yield result - final = {"ok": True, "pending": True} - yield StreamEvent("final", final) - pending = [] - continuing = bool( - final.get("tool_calls") or final.get("tool_results") - ) - if ( - not continuing - and not external.pending - and step < agent.max_steps - ): - # Delegate steering and subagent result delivery - # to Bub's registered handlers, not an inbox here. - pending = ( - await agent.events.emit( - MessagesPending( - state=state, - tape=tape, - prompt=None, - step_ended=True, - ) - ) - ).messages - step_status = "continue" if continuing or pending else "ok" - except Exception as exc: - step_status, step_error = "error", str(exc) - raise - finally: - finished = await agent.events.emit( - StepFinished( - state=state, - tape=tape, - step=step, - status=step_status, - elapsed_ms=int( - (time.monotonic() - step_started) * 1000 - ), - error=step_error, - prompt=prompt, - stream=output, - ) - ) - await inbound.events.send("STEP_FINISHED", stepName=str(step)) - if external.pending or step_status == "ok": - return - prompt = finished.next_prompt - raise RuntimeError(f"max_steps_reached={agent.max_steps}") - finally: - agent.events.off(AgentEvent, observe) - - return AsyncStreamEvents(iterate(), state=output) diff --git a/packages/bub-ag-ui/tests/client/protocol.test.mjs b/packages/bub-ag-ui/tests/client/protocol.test.mjs index cea410e..282cbef 100644 --- a/packages/bub-ag-ui/tests/client/protocol.test.mjs +++ b/packages/bub-ag-ui/tests/client/protocol.test.mjs @@ -148,7 +148,7 @@ test("aborting the HTTP stream releases execution without a cancel endpoint", as return; } catch { await new Promise(resolve => setTimeout(resolve, 20)); } } - assert.fail("thread was not released after abort"); + assert.fail(`thread was not released after abort: ${logs.slice(-18000)}`); }); test("only the AG-UI run endpoint is exposed", async () => { @@ -175,12 +175,11 @@ test("activity replacement and patches are consumed by the official reducer", as }); -test("native child steps do not collide with the parent's AG-UI steps", async () => { +test("native child events do not leak into the parent AG-UI stream", async () => { const a = agent("subagent"); const events = []; await a.runAgent({}, {onEvent: ({event}) => events.push(event)}); - assert.deepEqual(events.filter(e => e.type === "STEP_STARTED").map(e => e.stepName), ["1", "2"]); - assert.deepEqual(events.filter(e => e.type === "STEP_FINISHED").map(e => e.stepName), ["1", "2"]); + assert.equal(events.some(e => e.type.startsWith("STEP_")), false); assert.equal(events.at(-1).type, "RUN_FINISHED"); assert.equal(a.messages.at(-1).content, "Answer: subagent"); assert.equal(a.messages.filter(m => m.role === "tool").length, 1); diff --git a/packages/bub-ag-ui/tests/conftest.py b/packages/bub-ag-ui/tests/conftest.py index 09cd589..c9d0a66 100644 --- a/packages/bub-ag-ui/tests/conftest.py +++ b/packages/bub-ag-ui/tests/conftest.py @@ -117,16 +117,12 @@ async def history(self): async def collect(adapter, data, user="alice"): - from bub.core.tape import session_tape_name - from bub_ag_ui.continuation import Continuation from bub_ag_ui.events import MessageProjection events = [event async for event in adapter.run(user, data)] projection = MessageProjection() for event in events: projection.apply(event) - tape = adapter._storage_agent.tape( - session_tape_name(adapter.session_id(user, data.thread_id), adapter.workspace) - ) - pending = (await Continuation.load(tape)).pending + live = adapter._turns.get(adapter.session_id(user, data.thread_id)) + pending = list(live.inbound.external.pending) if live else [] return ClientView(projection, pending), events diff --git a/packages/bub-ag-ui/tests/test_execution.py b/packages/bub-ag-ui/tests/test_execution.py index 5e436dd..72d0b4c 100644 --- a/packages/bub-ag-ui/tests/test_execution.py +++ b/packages/bub-ag-ui/tests/test_execution.py @@ -370,17 +370,11 @@ def load_state(self, message): thread, events = await collect(adapter, request(tools=client_tools)) assert len({id(event.tape) for event in observed_steps}) == 2 - expected_steps = ["1"] if frontend else ["1", "2"] - assert [ - e["stepName"] for e in events if e["type"] == "STEP_STARTED" - ] == expected_steps - assert [ - e["stepName"] for e in events if e["type"] == "STEP_FINISHED" - ] == expected_steps + assert not any(e["type"].startswith("STEP_") for e in events) assert events[-1]["type"] == "RUN_FINISHED" - assert [e["toolCallId"] for e in events if e["type"] == "TOOL_CALL_RESULT"] == [ - "delegate-call" - ] + assert [e["toolCallId"] for e in events if e["type"] == "TOOL_CALL_RESULT"] == ( + [] if frontend else ["delegate-call"] + ) assert not any(e.get("delta") == "child answer" for e in events) if frontend: diff --git a/packages/bub-ag-ui/tests/test_native_stream.py b/packages/bub-ag-ui/tests/test_native_stream.py new file mode 100644 index 0000000..c12249a --- /dev/null +++ b/packages/bub-ag-ui/tests/test_native_stream.py @@ -0,0 +1,314 @@ +"""The adapter uses the full native stream, including Bub-owned loop policy.""" + +import httpx2 +from bub import hookimpl +from bub.core.events import MessagesPending +from bub.core.tape import session_tape_name +from bub.core.tools import Tool + +from .conftest import collect, request + + +async def test_native_run_prepares_tools_once_and_owns_continue_hook(setup): + provider, make = setup + observed = [] + + def load(context): + context.state["_runtime_agent"].tools["later"] = Tool.from_callable( + lambda: "later", name="later" + ) + return "loaded" + + adapter = make([Tool.from_callable(load, context=True)]) + + class Hooks: + @hookimpl + def continue_prompt(self, state): + observed.append((state.system_prompt, [t.name for t in state.tools])) + return "Native continue hook" + + adapter.framework.plugin_manager.register(Hooks(), "native-continuation") + provider.reply(calls=[("load", "load", "{}")]) + provider.reply("done") + _, events = await collect(adapter, request()) + assert events[-1]["type"] == "RUN_FINISHED" + assert len(observed) == 1 + assert observed[0][0] # public LLM request supplies the observed system prompt + assert observed[0][1] == ["load"] + assert "later" not in { + t["function"]["name"] for t in provider.requests[-1]["tools"] + } + assert provider.requests[-1]["messages"][-1]["content"] == "Native continue hook" + + +async def test_native_end_of_step_message_delivery_is_preserved(setup): + provider, make = setup + adapter = make() + delivered = False + + def deliver(event): + nonlocal delivered + if event.step_ended and not delivered: + delivered = True + event.messages.append(["Native queued input"]) + + class Hooks: + @hookimpl + def load_state(self, message): + message._runtime_agent.events.on(MessagesPending, deliver) + + adapter.framework.plugin_manager.register(Hooks(), "native-message-delivery") + provider.reply("first") + provider.reply("after queued input") + _, events = await collect(adapter, request()) + assert events[-1]["type"] == "RUN_FINISHED" + assert len(provider.requests) == 2 + assert [ + m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "user" + ] == ["Hello", "Native queued input"] + + +async def test_native_stream_owns_context_overflow_handoff_and_retry(setup): + provider, make = setup + adapter = make() + provider.responses.append( + httpx2.Response( + 400, + json={ + "error": { + "message": "maximum context length exceeded", + "code": "context_length_exceeded", + } + }, + ) + ) + provider.reply("after native handoff") + _, events = await collect(adapter, request()) + assert len(provider.requests) == 2 + assert events[-1]["type"] == "RUN_FINISHED" + tape = adapter._storage_agent.tape( + session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) + ) + entries = list(await tape.query().all()) + assert "auto_handoff/context_overflow" in [ + e.payload["name"] for e in entries if e.kind == "anchor" + ] + assert any(e.payload.get("name") == "loop.start" for e in entries) + assert any(e.payload.get("name") == "loop.finish" for e in entries) + + +async def test_step_limit_still_bounds_backend_tool_loop(setup): + provider, make = setup + adapter = make([Tool.from_callable(lambda: "clock", name="clock")]) + + class Limit: + @hookimpl + def load_state(self, message): + message._runtime_agent.max_steps = 1 + + adapter.framework.plugin_manager.register(Limit(), "one-step") + provider.reply(calls=[("c1", "clock", "{}")]) + _, events = await collect(adapter, request()) + assert len(provider.requests) == 1 + assert events[-1]["type"] == "RUN_ERROR" + assert len([e for e in events if e["type"] == "TOOL_CALL_RESULT"]) == 1 + + +async def test_native_preparation_sees_shared_state_updates_on_next_step(setup): + from bub_ag_ui.state import shared_state + + provider, make = setup + + async def update(context): + await shared_state(context).set({"count": 1}) + return "updated" + + adapter = make([Tool.from_callable(update, context=True)]) + provider.reply(calls=[("u1", "update", "{}")]) + provider.reply("done") + data = request() + data.state = {"count": 0} + _, events = await collect(adapter, data) + assert events[-1]["type"] == "RUN_FINISHED" + assert '"count": 0' in provider.requests[0]["messages"][0]["content"] + assert '"count": 1' in provider.requests[1]["messages"][0]["content"] + + +async def test_frontend_attachments_share_one_native_run_and_record_complete_exchange( + setup, monkeypatch +): + from bub.builtin.agent import BubAgent + from bub.core.events import ChatFinished, RunFinished, RunStarted + + provider, make = setup + native_calls, lifecycle, saved = [], [], [] + original = BubAgent.run_stream + + async def counted(self, **kwargs): + native_calls.append(kwargs) + return await original(self, **kwargs) + + monkeypatch.setattr(BubAgent, "run_stream", counted) + adapter = make() + + class Observe: + @hookimpl + def load_state(self, message): + for event_type in (RunStarted, ChatFinished, RunFinished): + message._runtime_agent.events.on(event_type, lifecycle.append) + + @hookimpl + def save_state(self, model_output): + saved.append(model_output) + + adapter.framework.plugin_manager.register(Observe(), "native-lifecycle") + tools = [{"name": "browser", "description": "", "parameters": {}}] + provider.reply(calls=[("f1", "browser", "{}")]) + _, paused = await collect(adapter, request(tools=tools)) + assert paused[-1]["type"] == "RUN_FINISHED" + assert len(native_calls) == 1 + assert [type(event) for event in lifecycle] == [RunStarted] + assert saved == [] + tape = adapter._storage_agent.tape( + session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) + ) + assert not await tape.query().kinds("message").all() + + provider.reply("done") + _, finished = await collect( + adapter, + request( + [ + { + "id": "result", + "role": "tool", + "toolCallId": "f1", + "content": "client value", + } + ], + run="r2", + tools=tools, + ), + ) + assert finished[-1]["type"] == "RUN_FINISHED" + assert len(native_calls) == 1 + assert [type(event) for event in lifecycle] == [ + RunStarted, + ChatFinished, + ChatFinished, + RunFinished, + ] + assert saved == ["done"] + assert [message.role for message in await tape.read_messages()] == [ + "user", + "assistant", + "tool", + "assistant", + ] + assert not adapter._turns + + +async def test_detached_native_turn_timeout_releases_futures(setup): + import asyncio + + import pytest + + provider, make = setup + adapter = make(max_run_seconds=0.05) + tools = [{"name": "browser", "description": "", "parameters": {}}] + provider.reply(calls=[("f1", "browser", "{}")]) + await collect(adapter, request(tools=tools)) + live = adapter._turns[adapter.session_id("alice", "t1")] + future = live.inbound.external.futures["f1"] + with pytest.raises(TimeoutError): + await asyncio.wait_for(asyncio.shield(live.task), 1) + assert future.cancelled() + assert not adapter._turns + _, expired = await collect( + adapter, + request( + [{"id": "result", "role": "tool", "toolCallId": "f1", "content": "late"}], + tools=tools, + ), + ) + assert expired[-1]["type"] == "RUN_ERROR" + assert len(provider.requests) == 1 + + +async def test_restart_does_not_reconstruct_pending_native_stream(setup): + provider, make = setup + adapter = make() + tools = [{"name": "browser", "description": "", "parameters": {}}] + provider.reply(calls=[("f1", "browser", "{}")]) + view, _ = await collect(adapter, request(tools=tools)) + await adapter.close() + replacement = make() + _, failed = await collect( + replacement, + request( + [ + *(await view.history())["messages"], + {"id": "result", "role": "tool", "toolCallId": "f1", "content": "late"}, + ], + tools=tools, + ), + ) + assert failed[-1]["type"] == "RUN_ERROR" + assert "cannot survive" in failed[-1]["message"] + assert len(provider.requests) == 1 + + +async def test_pause_waits_for_sibling_backend_and_preserves_native_batch_order(setup): + import asyncio + + provider, make = setup + started, release = asyncio.Event(), asyncio.Event() + + async def backend(): + started.set() + await release.wait() + return "server" + + adapter = make([Tool.from_callable(backend)]) + tools = [{"name": "browser", "description": "", "parameters": {}}] + provider.reply(calls=[("f1", "browser", "{}"), ("b1", "backend", "{}")]) + running = asyncio.create_task(collect(adapter, request(tools=tools))) + await asyncio.wait_for(started.wait(), 1) + live = adapter._turns[adapter.session_id("alice", "t1")] + assert live.inbound.external.pending + assert not live.inbound.external.paused.is_set() + assert not running.done() + release.set() + _, events = await asyncio.wait_for(running, 1) + assert events[-1]["type"] == "RUN_FINISHED" + provider.reply("done") + _, events = await collect( + adapter, + request( + [{"id": "result", "role": "tool", "toolCallId": "f1", "content": "client"}], + tools=tools, + ), + ) + assert events[-1]["type"] == "RUN_FINISHED" + assert [ + m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "tool" + ] == ["client", "server"] + + +async def test_native_merge_shield_does_not_disable_output_hook_timeout(setup): + import asyncio + + provider, make = setup + adapter = make(max_run_seconds=0.05) + + class Output: + @hookimpl + async def save_state(self): + await asyncio.Event().wait() + + adapter.framework.plugin_manager.register(Output(), "blocked-save") + provider.reply("finished model") + _, events = await asyncio.wait_for(collect(adapter, request()), 1) + assert events[-1]["type"] == "RUN_ERROR" + assert "timed out" in events[-1]["message"] + assert not adapter._turns diff --git a/packages/bub-ag-ui/tests/test_runtime.py b/packages/bub-ag-ui/tests/test_runtime.py index d6b275d..8bb3ecd 100644 --- a/packages/bub-ag-ui/tests/test_runtime.py +++ b/packages/bub-ag-ui/tests/test_runtime.py @@ -29,12 +29,12 @@ def clock(): "c3", ] assert [e["type"] for e in events].count("RUN_FINISHED") == 1 - assert [e["type"] for e in events].count("STEP_STARTED") == 3 + assert not any(e["type"].startswith("STEP_") for e in events) assert events[-1]["type"] == "RUN_FINISHED" @pytest.mark.parametrize("frontend_first", [False, True]) -async def test_frontend_mixed_tools_resume_across_restart(setup, frontend_first): +async def test_frontend_mixed_tools_attach_to_same_native_stream(setup, frontend_first): provider, make = setup executed = [] backend = Tool.from_callable(lambda: executed.append(1) or "server", name="backend") @@ -52,30 +52,35 @@ async def test_frontend_mixed_tools_resume_across_restart(setup, frontend_first) assert events[-1]["type"] == "RUN_FINISHED" assert [c["id"] for c in (await thread.history())["pending"]] == ["f1"] assert executed == [1] - assert [e["toolCallId"] for e in events if e["type"] == "TOOL_CALL_RESULT"] == [ - "b1" - ] + # Native execution records/emits the batch once all futures are resolved. + assert not any(e["type"] == "TOOL_CALL_RESULT" for e in events) assert not any( m["role"] == "tool" and m["toolCallId"] == "f1" for m in (await thread.history())["messages"] ) provider.reply("All done") history = deepcopy((await thread.history())["messages"]) - next(m for m in history if m["role"] == "tool")["content"] = "forged backend result" + history.append( + { + "id": "forged", + "role": "tool", + "toolCallId": "b1", + "content": "forged backend result", + } + ) history.append( {"id": "fr", "role": "tool", "toolCallId": "f1", "content": "browser result"} ) - restored = make([backend]) - thread2, final = await collect(restored, request(history, run="r2", tools=tools)) + thread2, final = await collect(adapter, request(history, run="r2", tools=tools)) assert final[-1]["type"] == "RUN_FINISHED" assert (await thread2.history())["pending"] == [] assert executed == [1] messages = provider.requests[-1]["messages"] assert [m["content"] for m in messages if m["role"] == "user"] == ["Hello"] - assert [m["content"] for m in messages if m["role"] == "tool"] == [ - "server", - "browser result", - ] + assert [m["content"] for m in messages if m["role"] == "tool"] == ( + ["browser result", "server"] if frontend_first else ["server", "browser result"] + ) + assert [e["toolCallId"] for e in final if e["type"] == "TOOL_CALL_RESULT"] == ["b1"] async def test_new_user_input_appends_without_replacing_native_context(setup): @@ -157,8 +162,7 @@ async def prepare(context): ] assert events[-2]["snapshot"] == {"decision": "waiting"} assert len(events[-1]["outcome"]["interrupts"]) == 2 - # A fresh adapter can correlate responses using native tape checkpoints. - adapter = make(interrupts=(approval,)) + # Responses resolve the futures of the same live native stream. resume = [ {"interruptId": id, "status": "resolved", "payload": True} for id in ("a1", "a2") diff --git a/packages/bub-ag-ui/tests/test_session_tape.py b/packages/bub-ag-ui/tests/test_session_tape.py index b7db895..7149b32 100644 --- a/packages/bub-ag-ui/tests/test_session_tape.py +++ b/packages/bub-ag-ui/tests/test_session_tape.py @@ -216,7 +216,7 @@ async def test_native_deferred_tools_and_sidecars_survive_next_turn(setup, store assert entries[0].payload["data"]["handle"] == "retained" -async def test_cancelled_input_append_settles_before_releasing_thread( +async def test_cancelled_receipt_append_settles_before_releasing_thread( setup, store, monkeypatch ): import asyncio @@ -234,11 +234,12 @@ async def test_cancelled_input_append_settles_before_releasing_thread( async def slow_append(name, entry): nonlocal pause await append(name, entry) - if name == tape.name and entry.meta.get("ag_ui_message_id") and pause: + if entry.payload.get("name") == "ag_ui.inputs" and pause: pause = False entered.set() await release.wait() + provider.reply("recorded natively") monkeypatch.setattr(tape.store, "append", slow_append) task = asyncio.create_task(collect(adapter, request())) try: @@ -254,7 +255,7 @@ async def slow_append(name, entry): await asyncio.gather(task, return_exceptions=True) assert task.cancelled() assert await load_session_settings(tape) == {"reasoning_effort": "high"} - assert provider.requests == [] + assert len(provider.requests) == 1 assert not adapter._active provider.reply("next") _, events = await collect( diff --git a/packages/bub-ag-ui/tests/test_tools.py b/packages/bub-ag-ui/tests/test_tools.py index 2afa599..91c9b8d 100644 --- a/packages/bub-ag-ui/tests/test_tools.py +++ b/packages/bub-ag-ui/tests/test_tools.py @@ -1,9 +1,11 @@ """External tools share native names, but not native child-turn ownership.""" +import asyncio + import pytest from ag_ui.core import Tool as ClientTool from bub.core.tools import Tool, ToolContext -from bub_ag_ui.tools import ExternalCallsPending, ExternalTools, InterruptTool +from bub_ag_ui.tools import ExternalTools, InterruptTool from .conftest import collect, request @@ -54,13 +56,16 @@ async def test_direct_foreign_tool_call_cannot_modify_parent_pending(setup, inte ) external.bind(agent, state) parent = ToolContext(tape=agent.tape("parent"), state=state, call_id="parent-call") - with pytest.raises(ExternalCallsPending): - await agent.tools["browser"].run(context=parent) + external.batch = {"parent-call": "browser"} + waiting = asyncio.create_task(agent.tools["browser"].run(context=parent)) + await asyncio.wait_for(external.paused.wait(), 1) + assert not waiting.done() # A shallow state copy retains all inherited values, not turn ownership. child = ToolContext( tape=agent.tape("child"), state=state.copy(), call_id="child-call" ) with pytest.raises(ValueError, match="owning AG-UI turn"): await agent.tools["browser"].run(context=child) - assert external.context is parent assert [call["id"] for call in external.pending] == ["parent-call"] + external.resolve({"parent-call": "accepted"}) + assert await waiting == "accepted" diff --git a/packages/bub-ag-ui/tests/test_turn_bridge.py b/packages/bub-ag-ui/tests/test_turn_bridge.py deleted file mode 100644 index 04602f7..0000000 --- a/packages/bub-ag-ui/tests/test_turn_bridge.py +++ /dev/null @@ -1,132 +0,0 @@ -"""The pinned API needs a step bridge, not a second copy of Bub's policies.""" - -import httpx2 -from bub import hookimpl -from bub.core.events import MessagesPending -from bub.core.tape import session_tape_name -from bub.core.tools import Tool - -from .conftest import collect, request - - -async def test_each_step_uses_native_tool_preparation_and_continue_hook(setup): - provider, make = setup - observed = [] - - def load(context): - context.state["_runtime_agent"].tools["later"] = Tool.from_callable( - lambda: "later", name="later" - ) - return "loaded" - - adapter = make([Tool.from_callable(load, context=True)]) - - class Hooks: - @hookimpl - def continue_prompt(self, state): - observed.append((state.system_prompt, [t.name for t in state.tools])) - return "Native continue hook" - - adapter.framework.plugin_manager.register(Hooks(), "native-continuation") - provider.reply(calls=[("load", "load", "{}")]) - provider.reply("done") - _, events = await collect(adapter, request()) - assert events[-1]["type"] == "RUN_FINISHED" - assert len(observed) == 1 - assert observed[0][0] # public LLM request supplies the observed system prompt - assert observed[0][1] == ["load"] - assert "later" in {t["function"]["name"] for t in provider.requests[-1]["tools"]} - assert provider.requests[-1]["messages"][-1]["content"] == "Native continue hook" - - -async def test_native_end_of_step_message_delivery_is_preserved(setup): - provider, make = setup - adapter = make() - delivered = False - - def deliver(event): - nonlocal delivered - if event.step_ended and not delivered: - delivered = True - event.messages.append(["Native queued input"]) - - class Hooks: - @hookimpl - def load_state(self, message): - message._runtime_agent.events.on(MessagesPending, deliver) - - adapter.framework.plugin_manager.register(Hooks(), "native-message-delivery") - provider.reply("first") - provider.reply("after queued input") - _, events = await collect(adapter, request()) - assert events[-1]["type"] == "RUN_FINISHED" - assert len(provider.requests) == 2 - assert [ - m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "user" - ] == ["Hello", "Native queued input"] - - -async def test_context_overflow_does_not_trigger_adapter_handoff_or_retry(setup): - provider, make = setup - adapter = make() - provider.responses.append( - httpx2.Response( - 400, - json={ - "error": { - "message": "maximum context length exceeded", - "code": "context_length_exceeded", - } - }, - ) - ) - _, events = await collect(adapter, request()) - assert len(provider.requests) == 1 - assert events[-1]["type"] == "RUN_ERROR" - tape = adapter._storage_agent.tape( - session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) - ) - entries = list(await tape.query().all()) - assert [e.payload["name"] for e in entries if e.kind == "anchor"] == [ - "session/start" - ] - assert not any( - e.payload.get("name") in {"loop.start", "loop.finish"} for e in entries - ) - - -async def test_step_limit_still_bounds_backend_tool_loop(setup): - provider, make = setup - adapter = make([Tool.from_callable(lambda: "clock", name="clock")]) - - class Limit: - @hookimpl - def load_state(self, message): - message._runtime_agent.max_steps = 1 - - adapter.framework.plugin_manager.register(Limit(), "one-step") - provider.reply(calls=[("c1", "clock", "{}")]) - _, events = await collect(adapter, request()) - assert len(provider.requests) == 1 - assert events[-1]["type"] == "RUN_ERROR" - assert len([e for e in events if e["type"] == "TOOL_CALL_RESULT"]) == 1 - - -async def test_native_preparation_sees_shared_state_updates_on_next_step(setup): - from bub_ag_ui.state import shared_state - - provider, make = setup - - async def update(context): - await shared_state(context).set({"count": 1}) - return "updated" - - adapter = make([Tool.from_callable(update, context=True)]) - provider.reply(calls=[("u1", "update", "{}")]) - provider.reply("done") - data = request() - data.state = {"count": 0} - _, events = await collect(adapter, data) - assert events[-1]["type"] == "RUN_FINISHED" - assert '"count": 0' in provider.requests[0]["messages"][0]["content"] - assert '"count": 1' in provider.requests[1]["messages"][0]["content"] From 8f141a9fedc456b30cf0e16cc04f4cf2ff36956b Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 10 Nov 2026 23:45:35 +0800 Subject: [PATCH 06/10] test(bub-ag-ui): audit suite against observable protocol behavior --- packages/bub-ag-ui/README.md | 4 +- .../bub-ag-ui/tests/client/protocol.test.mjs | 10 +- packages/bub-ag-ui/tests/conftest.py | 8 +- packages/bub-ag-ui/tests/test_events.py | 128 +++---- packages/bub-ag-ui/tests/test_execution.py | 113 +++---- packages/bub-ag-ui/tests/test_http.py | 34 +- .../bub-ag-ui/tests/test_native_context.py | 45 ++- .../bub-ag-ui/tests/test_native_stream.py | 314 ------------------ packages/bub-ag-ui/tests/test_runtime.py | 114 +++++-- packages/bub-ag-ui/tests/test_session_tape.py | 270 ++++----------- .../bub-ag-ui/tests/test_stream_delivery.py | 52 +-- packages/bub-ag-ui/tests/test_tools.py | 33 +- 12 files changed, 311 insertions(+), 814 deletions(-) delete mode 100644 packages/bub-ag-ui/tests/test_native_stream.py diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index 7f82269..e2cae4e 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -174,8 +174,8 @@ npm ci --prefix packages/bub-ag-ui/tests/client npm test --prefix packages/bub-ag-ui/tests/client ``` -Tests cover protocol behavior, native execution, cancellation, and streaming -without per-event store reads. Socket tests exercise the published AG-UI client +Tests cover observable protocol output, application tools and state, authentication, +cancellation, streaming backpressure, and conversation continuity. Socket tests exercise the published AG-UI client and assistant-ui runtime with a deterministic model provider; no model API key is needed. Set `PYTHON` if the interpreter is not at `.venv/bin/python`. These are regression and interoperability tests, not TCK certification. diff --git a/packages/bub-ag-ui/tests/client/protocol.test.mjs b/packages/bub-ag-ui/tests/client/protocol.test.mjs index 282cbef..d02f5bc 100644 --- a/packages/bub-ag-ui/tests/client/protocol.test.mjs +++ b/packages/bub-ag-ui/tests/client/protocol.test.mjs @@ -78,7 +78,9 @@ test("interrupt outcome and resume are understood by the protocol client", async let outcome; const events = []; await a.runAgent({}, {onEvent: ({event}) => events.push(event), onRunFinishedEvent: ({event}) => {outcome = event.outcome;}}); - assert.deepEqual(events.slice(-3).map(e => e.type), ["MESSAGES_SNAPSHOT", "STATE_SNAPSHOT", "RUN_FINISHED"]); + assert.equal(events.at(-1).type, "RUN_FINISHED"); + assert.ok(events.some(e => e.type === "MESSAGES_SNAPSHOT")); + assert.ok(events.some(e => e.type === "STATE_SNAPSHOT")); assert.equal(outcome.type, "interrupt"); await a.runAgent({resume: [{interruptId: outcome.interrupts[0].id, status: "resolved", payload: true}]}); assert.equal(a.messages.at(-1).content, "Answer: interrupt"); @@ -179,7 +181,7 @@ test("native child events do not leak into the parent AG-UI stream", async () => const a = agent("subagent"); const events = []; await a.runAgent({}, {onEvent: ({event}) => events.push(event)}); - assert.equal(events.some(e => e.type.startsWith("STEP_")), false); + assert.ok(!a.messages.some(m => m.content === "Answer: nested child")); assert.equal(events.at(-1).type, "RUN_FINISHED"); assert.equal(a.messages.at(-1).content, "Answer: subagent"); assert.equal(a.messages.filter(m => m.role === "tool").length, 1); @@ -191,5 +193,7 @@ test("invalid interrupt resumes end with RUN_ERROR, not an HTTP error", async () const a = agent("interrupt"); const events = []; await a.runAgent({resume: [{interruptId: "unknown", status: "resolved", payload: true}]}, {onEvent: ({event}) => events.push(event)}).catch(() => {}); - assert.deepEqual(events.map(e => e.type), ["RUN_STARTED", "RUN_ERROR"]); + assert.equal(events[0].type, "RUN_STARTED"); + assert.equal(events.at(-1).type, "RUN_ERROR"); + assert.ok(!events.some(e => e.type === "RUN_FINISHED")); }); diff --git a/packages/bub-ag-ui/tests/conftest.py b/packages/bub-ag-ui/tests/conftest.py index c9d0a66..ce4a619 100644 --- a/packages/bub-ag-ui/tests/conftest.py +++ b/packages/bub-ag-ui/tests/conftest.py @@ -105,14 +105,12 @@ def request(messages=None, *, run="r1", thread="t1", tools=None, **kwargs): class ClientView: """Test-side reducer, never an adapter history API.""" - def __init__(self, projection, pending): + def __init__(self, projection): self.projection = projection - self.pending = pending async def history(self): return { "messages": self.projection.messages, - "pending": self.pending, } @@ -123,6 +121,4 @@ async def collect(adapter, data, user="alice"): projection = MessageProjection() for event in events: projection.apply(event) - live = adapter._turns.get(adapter.session_id(user, data.thread_id)) - pending = list(live.inbound.external.pending) if live else [] - return ClientView(projection, pending), events + return ClientView(projection), events diff --git a/packages/bub-ag-ui/tests/test_events.py b/packages/bub-ag-ui/tests/test_events.py index ae044e8..360ba4c 100644 --- a/packages/bub-ag-ui/tests/test_events.py +++ b/packages/bub-ag-ui/tests/test_events.py @@ -1,34 +1,35 @@ +"""Application state and activity updates are visible in the protocol output.""" + import asyncio -from copy import deepcopy +import json -import pytest +import jsonpatch from ag_ui.core import ActivityDeltaEvent, ActivitySnapshotEvent -from bub_ag_ui.events import MessageProjection -from bub_ag_ui.state import SharedState -from jsonpatch import JsonPatchConflict +from bub.core.tools import Tool +from bub_ag_ui.state import shared_state +from .conftest import collect, request -async def test_invalid_activity_patch_does_not_change_response(): - projection = MessageProjection() - async def emit(event): - projection.apply(event) +async def test_invalid_activity_patch_leaves_last_valid_client_view(setup): + provider, make = setup - state = SharedState({}, emit) - await state.emit( - ActivitySnapshotEvent( - message_id="a1", activity_type="progress", content={"done": 1} + async def update(context): + state = shared_state(context) + await state.emit( + ActivitySnapshotEvent( + message_id="a1", + activity_type="progress", + content={"done": 1}, + ) ) - ) - await state.emit( - ActivityDeltaEvent( - message_id="a1", - activity_type="progress", - patch=[{"op": "replace", "path": "/done", "value": 2}], + await state.emit( + ActivityDeltaEvent( + message_id="a1", + activity_type="progress", + patch=[{"op": "replace", "path": "/done", "value": 2}], + ) ) - ) - previous = deepcopy(projection.messages) - with pytest.raises(JsonPatchConflict): await state.emit( ActivityDeltaEvent( message_id="a1", @@ -36,47 +37,50 @@ async def emit(event): patch=[{"op": "remove", "path": "/does-not-exist"}], ) ) - assert projection.messages == previous + adapter = make([Tool.from_callable(update, context=True)]) + provider.reply(calls=[("update", "update", "{}")]) + provider.reply("Could not apply that update") + view, events = await collect(adapter, request()) + assert events[-1]["type"] == "RUN_FINISHED" + assert [ + m["content"] + for m in (await view.history())["messages"] + if m["role"] == "activity" + ] == [{"done": 2}] + assert len([e for e in events if e["type"] == "ACTIVITY_DELTA"]) == 1 + + +async def test_concurrent_state_updates_reach_client_without_lost_items(setup): + provider, make = setup + + async def update(context): + state = shared_state(context) + await state.set({"items": []}) + await asyncio.gather( + *( + state.patch([{"op": "add", "path": "/items/-", "value": i}]) + for i in range(10) + ) + ) + return state.value -async def test_concurrent_state_patches_are_serialized(): - events = [] - - async def emit(event): - await asyncio.sleep(0) - events.append(event) - - state = SharedState({"items": []}, emit) - await asyncio.gather( - *( - state.patch([{"op": "add", "path": "/items/-", "value": i}]) - for i in range(10) + provider.reply(calls=[("update", "update", "{}")]) + provider.reply("updated") + _, events = await collect( + make([Tool.from_callable(update, context=True)]), request() + ) + client_state = None + for event in events: + if event["type"] == "STATE_SNAPSHOT": + client_state = event["snapshot"] + elif event["type"] == "STATE_DELTA": + client_state = jsonpatch.apply_patch(client_state, event["delta"]) + assert events[-1]["type"] == "RUN_FINISHED" + assert sorted(client_state["items"]) == list(range(10)) + assert ( + next( + json.loads(e["content"]) for e in events if e["type"] == "TOOL_CALL_RESULT" ) + == client_state ) - assert state.value == {"items": list(range(10))} - assert len(events) == 10 - - -async def test_shared_state_patch_has_one_owner(monkeypatch): - import jsonpatch - from bub_ag_ui.events import EventStream - - events, applied = [], [] - apply = jsonpatch.apply_patch - - def counted(value, operations, **kwargs): - applied.append(operations) - return apply(value, operations, **kwargs) - - monkeypatch.setattr(jsonpatch, "apply_patch", counted) - - async def emit(event): - events.append(event) - - stream = EventStream(emit) - state = SharedState({"count": 0}, stream.publish) - await state.set(state.value) - await state.patch([{"op": "replace", "path": "/count", "value": 1}]) - assert state.value == {"count": 1} - assert len(applied) == 1 - assert events[-1]["type"] == "STATE_DELTA" diff --git a/packages/bub-ag-ui/tests/test_execution.py b/packages/bub-ag-ui/tests/test_execution.py index 72d0b4c..6051424 100644 --- a/packages/bub-ag-ui/tests/test_execution.py +++ b/packages/bub-ag-ui/tests/test_execution.py @@ -1,4 +1,4 @@ -"""The AG-UI adapter participates in Bub's native hook/EventBus lifecycle.""" +"""Application extensions and other channels retain their observable behavior.""" import asyncio from contextlib import aclosing @@ -8,19 +8,17 @@ from bub.builtin.agent import BubAgent from bub.channels.manager import ChannelManager from bub.channels.message import ChannelMessage -from bub.core.events import StepStarted, ToolCallDecision +from bub.core.events import ToolCallDecision from bub.core.tools import Tool from bub.streaming import AsyncStreamEvents, StreamEvent -from bub_ag_ui.execution import install_execution_hooks -from bub_ag_ui.plugin import AGUIChannel +from bub_ag_ui.plugin import AGUIPlugin from .conftest import collect, request -async def test_tools_bound_by_load_state_use_native_policy_and_output_hooks(setup): +async def test_application_tool_policy_is_reflected_in_client_results(setup): provider, make = setup adapter = make() - saved, rendered = [], [] class Hooks: @hookimpl @@ -34,14 +32,6 @@ def load_state(self, message): def before_tool_call(self, call): return ToolCallDecision.replace("hook-result") - @hookimpl - def save_state(self, model_output): - saved.append(model_output) - - @hookimpl - def render_outbound(self, model_output): - rendered.append(model_output) - adapter.framework.plugin_manager.register(Hooks(), "observe-turn") provider.reply(calls=[("c1", "bound", "{}")]) provider.reply("done") @@ -50,7 +40,6 @@ def render_outbound(self, model_output): assert [e["content"] for e in events if e["type"] == "TOOL_CALL_RESULT"] == [ "hook-result" ] - assert saved == rendered == ["done"] @pytest.mark.parametrize("failure", ["save", "render", "dispatch"]) @@ -86,9 +75,8 @@ async def test_gateway_keeps_router_and_does_not_capture_other_channels(setup): provider, make = setup adapter = make(bind_router=False) framework = adapter.framework - channel = AGUIChannel(framework) + framework.plugin_manager.register(AGUIPlugin(framework), "ag-ui") manager = ChannelManager(framework, system_prompt=None) - manager._channels["ag-ui"] = channel framework.bind_channel_router(manager) # A second controller must not replace a gateway's router. second = make(bind_router=False) @@ -119,16 +107,14 @@ async def test_concurrent_threads_keep_event_routes_separate(setup): class ModelHook: @hookimpl(tryfirst=True) - def run_model_stream(self, state): + def run_model_stream(self, prompt): async def source(): nonlocal count count += 1 if count == 2: both.set() await asyncio.wait_for(both.wait(), 2) - yield StreamEvent( - "text", {"delta": state["_ag_ui_inbound"].request.thread_id} - ) + yield StreamEvent("text", {"delta": "".join(prompt)}) return AsyncStreamEvents(source()) @@ -136,7 +122,16 @@ async def source(): # framework stream routing. There is no global "current AG-UI run". adapter.framework.plugin_manager.register(ModelHook(), "custom-model") results = await asyncio.gather( - collect(adapter, request(thread="one")), collect(adapter, request(thread="two")) + *( + collect( + adapter, + request( + [{"id": "u1", "role": "user", "content": label}], + thread=label, + ), + ) + for label in ("one", "two") + ) ) for thread_id, (thread, events) in zip(["one", "two"], results, strict=True): assert (await thread.history())["messages"][-1]["content"] == thread_id @@ -150,7 +145,6 @@ async def test_installation_preserves_other_channel_scalar_provider( provider, make = setup adapter = make(bind_router=False) framework = adapter.framework - framework.plugin_manager.unregister(name="ag-ui.execution") class ScalarModel: @hookimpl @@ -158,14 +152,10 @@ def run_model(self, prompt): return "selected scalar model" framework.plugin_manager.register(ScalarModel(), "scalar-model") - for index in range(2): - message = ChannelMessage( - session_id=f"other-{index}", channel="other", content="hi" - ) - message._runtime_agent = BubAgent(framework, tools=[], skill_dirs=[]) - result = await framework.process_inbound(message, stream_output=stream_output) - assert result.model_output == "selected scalar model" - install_execution_hooks(framework) + message = ChannelMessage(session_id="other", channel="other", content="hi") + message._runtime_agent = BubAgent(framework, tools=[], skill_dirs=[]) + result = await framework.process_inbound(message, stream_output=stream_output) + assert result.model_output == "selected scalar model" assert provider.requests == [] @@ -184,7 +174,6 @@ async def test_external_calls_obey_native_policy(setup, action, interrupt): if interrupt else [{"name": "browser", "description": "", "parameters": schema}] ) - finished = [] class Guard: @hookimpl @@ -195,23 +184,19 @@ def before_tool_call(self, call): return ToolCallDecision.replace("already handled") return ToolCallDecision.proceed({"value": 7}) - @hookimpl - def after_tool_call(self, call, result): - finished.append(result) - adapter.framework.plugin_manager.register(Guard(), "tool-policy") provider.reply(calls=[("f1", "browser", '{"value":1}')]) if action != "proceed": provider.reply("handled without frontend execution") - thread, events = await collect(adapter, request(tools=tools)) + _, events = await collect(adapter, request(tools=tools)) assert events[-1]["type"] == "RUN_FINISHED" if action == "proceed": - assert len((await thread.history())["pending"]) == 1 - assert (await thread.history())["pending"][0]["arguments"] == '{"value": 7}' - assert not finished # suspended is not a fabricated completion + assert not any(e["type"] == "TOOL_CALL_RESULT" for e in events) + if interrupt: + assert events[-1]["outcome"]["interrupts"][0]["metadata"]["arguments"] == { + "value": 7 + } else: - assert (await thread.history())["pending"] == [] - assert len(finished) == 1 results = [e["content"] for e in events if e["type"] == "TOOL_CALL_RESULT"] assert len(results) == 1 assert ( @@ -227,18 +212,10 @@ async def test_custom_provider_delegating_native_recording_does_not_duplicate_hi class DelegatingProvider: @hookimpl(tryfirst=True) - def run_model_stream(self, state): - message = state["_ag_ui_inbound"] - agent = message._runtime_agent - - async def source(): - async with agent.session(message.model_tape, state=state) as tape: - async for event in agent.run_once( - None, tape=tape, model=agent.settings.chat_model() - ): - yield event - - return AsyncStreamEvents(source()) + async def run_model_stream(self, prompt, session_id, state): + return await state["_runtime_agent"].run_stream( + prompt=prompt, session_id=session_id, state=state + ) adapter.framework.plugin_manager.register(DelegatingProvider(), "delegate") provider.reply("recorded by native agent") @@ -322,7 +299,6 @@ async def source(): async def test_child_run_events_do_not_escape_into_parent_turn(setup, frontend): """Subagents use run_stream on the same agent/EventBus, but a different tape.""" provider, make = setup - observed_steps = [] async def delegate(context): agent = context.state["_runtime_agent"] @@ -347,12 +323,6 @@ async def delegate(context): ] ) - class ObserveSteps: - @hookimpl - def load_state(self, message): - message._runtime_agent.events.on(StepStarted, observed_steps.append) - - adapter.framework.plugin_manager.register(ObserveSteps(), "observe-child-steps") calls = [("delegate-call", "delegate", "{}")] client_tools = [] if frontend: @@ -369,8 +339,6 @@ def load_state(self, message): provider.reply("parent answer") thread, events = await collect(adapter, request(tools=client_tools)) - assert len({id(event.tape) for event in observed_steps}) == 2 - assert not any(e["type"].startswith("STEP_") for e in events) assert events[-1]["type"] == "RUN_FINISHED" assert [e["toolCallId"] for e in events if e["type"] == "TOOL_CALL_RESULT"] == ( [] if frontend else ["delegate-call"] @@ -446,7 +414,6 @@ async def delegate(context): provider.reply("parent answer") thread, events = await collect(adapter, request(tools=tools)) assert events[-1]["type"] == "RUN_FINISHED" - assert (await thread.history())["pending"] == [] assert [e["toolCallId"] for e in events if e["type"] == "TOOL_CALL_RESULT"] == [ "delegate-call" ] @@ -469,3 +436,21 @@ async def delegate(context): assert [m["tool_call_id"] for m in messages if m["role"] == "tool"] == [ "delegate-call" ] + + +async def test_output_processing_obeys_run_timeout(setup): + import asyncio + + provider, make = setup + adapter = make(max_run_seconds=0.05) + + class Output: + @hookimpl + async def save_state(self): + await asyncio.Event().wait() + + adapter.framework.plugin_manager.register(Output(), "blocked-save") + provider.reply("finished model") + _, events = await asyncio.wait_for(collect(adapter, request()), 1) + assert events[-1]["type"] == "RUN_ERROR" + assert "timed out" in events[-1]["message"] diff --git a/packages/bub-ag-ui/tests/test_http.py b/packages/bub-ag-ui/tests/test_http.py index 2a7c501..6442af0 100644 --- a/packages/bub-ag-ui/tests/test_http.py +++ b/packages/bub-ag-ui/tests/test_http.py @@ -1,3 +1,4 @@ +import json from contextlib import asynccontextmanager import httpx @@ -9,6 +10,14 @@ from .conftest import collect, request +def sse_events(response): + return [ + json.loads(line[5:]) + for line in response.text.splitlines() + if line.startswith("data:") + ] + + @asynccontextmanager async def client_for(adapter, **kwargs): app = create_app( @@ -37,9 +46,12 @@ async def test_http_sse_and_cors_without_management_routes(setup): assert ( response.headers["access-control-allow-origin"] == "http://localhost:3000" ) - assert '"type":"RUN_FINISHED"' in response.text - assert "你好" in response.text - assert not response.text.startswith("id:") + events = sse_events(response) + assert events[-1]["type"] == "RUN_FINISHED" + assert ( + "".join(e["delta"] for e in events if e["type"] == "TEXT_MESSAGE_CONTENT") + == "你好" + ) for path in ("/threads/t1/history", "/threads/t1/runs/r1/events"): assert (await client.get(path)).status_code == 404 assert (await client.post("/threads/t1/runs/r1/cancel")).status_code == 404 @@ -51,6 +63,11 @@ async def test_http_auth_validation_and_limits(setup): make(), settings=AGUISettings(token="secret", max_body_bytes=1024) ) as client: assert (await client.post("/agent", json={})).status_code == 401 + assert ( + await client.post( + "/agent", json={}, headers={"Authorization": "Bearer wrong"} + ) + ).status_code == 401 auth = {"Authorization": "Bearer secret"} assert (await client.post("/agent", json={}, headers=auth)).status_code == 422 assert ( @@ -68,11 +85,12 @@ async def test_invalid_resume_is_an_agui_error_not_http_admission_error(setup): "/agent", json=data.model_dump(mode="json", by_alias=True) ) assert response.status_code == 200 - assert '"type":"RUN_ERROR"' in response.text - assert '"type":"RUN_FINISHED"' not in response.text + events = sse_events(response) + assert events[-1]["type"] == "RUN_ERROR" + assert not any(e["type"] == "RUN_FINISHED" for e in events) -async def test_custom_auth_isolates_continuation_checkpoints(setup): +async def test_custom_auth_isolates_pending_interrupts(setup): provider, make = setup adapter = make( interrupts=(InterruptTool("approve", "Approval", {"type": "object"}),) @@ -91,11 +109,11 @@ async def authenticate(request): async with client_for(adapter, authenticate=authenticate) as client: body = data.model_dump(mode="json", by_alias=True) response = await client.post("/agent", json=body, headers={"x-user": "bob"}) - assert '"type":"RUN_ERROR"' in response.text + assert sse_events(response)[-1]["type"] == "RUN_ERROR" assert (await client.post("/agent", json=body)).status_code == 401 provider.reply("approved") response = await client.post("/agent", json=body, headers={"x-user": "alice"}) - assert '"type":"RUN_FINISHED"' in response.text + assert sse_events(response)[-1]["type"] == "RUN_FINISHED" async def test_plugin_registers_cli_and_channel(setup): diff --git a/packages/bub-ag-ui/tests/test_native_context.py b/packages/bub-ag-ui/tests/test_native_context.py index 97285d3..2d4153f 100644 --- a/packages/bub-ag-ui/tests/test_native_context.py +++ b/packages/bub-ag-ui/tests/test_native_context.py @@ -1,18 +1,12 @@ """The transport supplies new input; Bub owns stored context and anchors.""" import pytest -from bub.core.tape import session_tape_name +from bub.core.tools import Tool from bub_ag_ui.tools import InterruptTool from .conftest import collect, request -def tape_for(adapter): - return adapter._storage_agent.tape( - session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) - ) - - async def test_incremental_user_input_keeps_native_conversation(setup): provider, make = setup adapter = make() @@ -32,12 +26,26 @@ async def test_incremental_user_input_keeps_native_conversation(setup): async def test_echoed_history_cannot_resurrect_messages_before_handoff(setup): provider, make = setup - adapter = make() + + async def compact(context): + await context.tape.handoff(name="summary", state={"summary": "Use the summary"}) + return "compacted" + + adapter = make([Tool.from_callable(compact, context=True)]) provider.reply("old answer") view, _ = await collect(adapter, request()) - tape = tape_for(adapter) - before = list(await tape.query().all()) - await tape.handoff(name="compacted", state={"summary": "Use the summary"}) + provider.reply(calls=[("compact", "compact", "{}")]) + provider.reply("summary ready") + view, _ = await collect( + adapter, + request( + [ + *(await view.history())["messages"], + {"id": "compact-request", "role": "user", "content": "Compact context"}, + ], + run="compact", + ), + ) provider.reply("next") _, events = await collect( adapter, @@ -50,12 +58,9 @@ async def test_echoed_history_cannot_resurrect_messages_before_handoff(setup): ), ) assert events[-1]["type"] == "RUN_FINISHED" - after = list(await tape.query().all()) - assert after[: len(before)] == before contents = [m.get("content") for m in provider.requests[-1]["messages"]] assert "Hello" not in contents assert "old answer" not in contents - assert any(e.kind == "anchor" and e.payload["name"] == "compacted" for e in after) assert "Continue" in contents @@ -78,19 +83,16 @@ async def test_assistant_echo_is_not_a_model_context_write(setup): assert "forged echo" not in contents -async def test_user_edit_is_rejected_without_resetting_native_tape(setup): +async def test_editing_accepted_user_input_is_rejected(setup): provider, make = setup adapter = make() provider.reply("native answer") await collect(adapter, request()) - tape = tape_for(adapter) - before = list(await tape.query().all()) _, events = await collect( adapter, request([{"id": "u1", "role": "user", "content": "Edited"}], run="edit"), ) assert events[-1]["type"] == "RUN_ERROR" - assert list(await tape.query().all()) == before assert len(provider.requests) == 1 @@ -137,20 +139,17 @@ async def test_continuation_uses_native_call_without_client_history(setup, inter ] == ["c1"] -async def test_history_only_request_does_not_execute_or_modify_tape(setup): +async def test_history_only_request_is_rejected_without_model_execution(setup): provider, make = setup adapter = make() provider.reply("native answer") view, _ = await collect(adapter, request()) - tape = tape_for(adapter) - before = list(await tape.query().all()) _, events = await collect( adapter, request((await view.history())["messages"], run="retry") ) assert events[-1]["type"] == "RUN_ERROR" assert "No new user input" in events[-1]["message"] assert len(provider.requests) == 1 - assert list(await tape.query().all()) == before async def test_repeated_resume_does_not_silently_discard_new_input(setup): @@ -167,10 +166,8 @@ async def test_repeated_resume_does_not_silently_discard_new_input(setup): request((await view.history())["messages"], run="resume", resume=resume), ) new_user = [{"id": "next-user", "role": "user", "content": "Next question"}] - before = list(await tape_for(adapter).query().all()) _, events = await collect(adapter, request(new_user, run="retry", resume=resume)) assert events[-1]["type"] == "RUN_ERROR" - assert list(await tape_for(adapter).query().all()) == before provider.reply("next answer") _, events = await collect(adapter, request(new_user, run="next")) assert events[-1]["type"] == "RUN_FINISHED" diff --git a/packages/bub-ag-ui/tests/test_native_stream.py b/packages/bub-ag-ui/tests/test_native_stream.py deleted file mode 100644 index c12249a..0000000 --- a/packages/bub-ag-ui/tests/test_native_stream.py +++ /dev/null @@ -1,314 +0,0 @@ -"""The adapter uses the full native stream, including Bub-owned loop policy.""" - -import httpx2 -from bub import hookimpl -from bub.core.events import MessagesPending -from bub.core.tape import session_tape_name -from bub.core.tools import Tool - -from .conftest import collect, request - - -async def test_native_run_prepares_tools_once_and_owns_continue_hook(setup): - provider, make = setup - observed = [] - - def load(context): - context.state["_runtime_agent"].tools["later"] = Tool.from_callable( - lambda: "later", name="later" - ) - return "loaded" - - adapter = make([Tool.from_callable(load, context=True)]) - - class Hooks: - @hookimpl - def continue_prompt(self, state): - observed.append((state.system_prompt, [t.name for t in state.tools])) - return "Native continue hook" - - adapter.framework.plugin_manager.register(Hooks(), "native-continuation") - provider.reply(calls=[("load", "load", "{}")]) - provider.reply("done") - _, events = await collect(adapter, request()) - assert events[-1]["type"] == "RUN_FINISHED" - assert len(observed) == 1 - assert observed[0][0] # public LLM request supplies the observed system prompt - assert observed[0][1] == ["load"] - assert "later" not in { - t["function"]["name"] for t in provider.requests[-1]["tools"] - } - assert provider.requests[-1]["messages"][-1]["content"] == "Native continue hook" - - -async def test_native_end_of_step_message_delivery_is_preserved(setup): - provider, make = setup - adapter = make() - delivered = False - - def deliver(event): - nonlocal delivered - if event.step_ended and not delivered: - delivered = True - event.messages.append(["Native queued input"]) - - class Hooks: - @hookimpl - def load_state(self, message): - message._runtime_agent.events.on(MessagesPending, deliver) - - adapter.framework.plugin_manager.register(Hooks(), "native-message-delivery") - provider.reply("first") - provider.reply("after queued input") - _, events = await collect(adapter, request()) - assert events[-1]["type"] == "RUN_FINISHED" - assert len(provider.requests) == 2 - assert [ - m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "user" - ] == ["Hello", "Native queued input"] - - -async def test_native_stream_owns_context_overflow_handoff_and_retry(setup): - provider, make = setup - adapter = make() - provider.responses.append( - httpx2.Response( - 400, - json={ - "error": { - "message": "maximum context length exceeded", - "code": "context_length_exceeded", - } - }, - ) - ) - provider.reply("after native handoff") - _, events = await collect(adapter, request()) - assert len(provider.requests) == 2 - assert events[-1]["type"] == "RUN_FINISHED" - tape = adapter._storage_agent.tape( - session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) - ) - entries = list(await tape.query().all()) - assert "auto_handoff/context_overflow" in [ - e.payload["name"] for e in entries if e.kind == "anchor" - ] - assert any(e.payload.get("name") == "loop.start" for e in entries) - assert any(e.payload.get("name") == "loop.finish" for e in entries) - - -async def test_step_limit_still_bounds_backend_tool_loop(setup): - provider, make = setup - adapter = make([Tool.from_callable(lambda: "clock", name="clock")]) - - class Limit: - @hookimpl - def load_state(self, message): - message._runtime_agent.max_steps = 1 - - adapter.framework.plugin_manager.register(Limit(), "one-step") - provider.reply(calls=[("c1", "clock", "{}")]) - _, events = await collect(adapter, request()) - assert len(provider.requests) == 1 - assert events[-1]["type"] == "RUN_ERROR" - assert len([e for e in events if e["type"] == "TOOL_CALL_RESULT"]) == 1 - - -async def test_native_preparation_sees_shared_state_updates_on_next_step(setup): - from bub_ag_ui.state import shared_state - - provider, make = setup - - async def update(context): - await shared_state(context).set({"count": 1}) - return "updated" - - adapter = make([Tool.from_callable(update, context=True)]) - provider.reply(calls=[("u1", "update", "{}")]) - provider.reply("done") - data = request() - data.state = {"count": 0} - _, events = await collect(adapter, data) - assert events[-1]["type"] == "RUN_FINISHED" - assert '"count": 0' in provider.requests[0]["messages"][0]["content"] - assert '"count": 1' in provider.requests[1]["messages"][0]["content"] - - -async def test_frontend_attachments_share_one_native_run_and_record_complete_exchange( - setup, monkeypatch -): - from bub.builtin.agent import BubAgent - from bub.core.events import ChatFinished, RunFinished, RunStarted - - provider, make = setup - native_calls, lifecycle, saved = [], [], [] - original = BubAgent.run_stream - - async def counted(self, **kwargs): - native_calls.append(kwargs) - return await original(self, **kwargs) - - monkeypatch.setattr(BubAgent, "run_stream", counted) - adapter = make() - - class Observe: - @hookimpl - def load_state(self, message): - for event_type in (RunStarted, ChatFinished, RunFinished): - message._runtime_agent.events.on(event_type, lifecycle.append) - - @hookimpl - def save_state(self, model_output): - saved.append(model_output) - - adapter.framework.plugin_manager.register(Observe(), "native-lifecycle") - tools = [{"name": "browser", "description": "", "parameters": {}}] - provider.reply(calls=[("f1", "browser", "{}")]) - _, paused = await collect(adapter, request(tools=tools)) - assert paused[-1]["type"] == "RUN_FINISHED" - assert len(native_calls) == 1 - assert [type(event) for event in lifecycle] == [RunStarted] - assert saved == [] - tape = adapter._storage_agent.tape( - session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) - ) - assert not await tape.query().kinds("message").all() - - provider.reply("done") - _, finished = await collect( - adapter, - request( - [ - { - "id": "result", - "role": "tool", - "toolCallId": "f1", - "content": "client value", - } - ], - run="r2", - tools=tools, - ), - ) - assert finished[-1]["type"] == "RUN_FINISHED" - assert len(native_calls) == 1 - assert [type(event) for event in lifecycle] == [ - RunStarted, - ChatFinished, - ChatFinished, - RunFinished, - ] - assert saved == ["done"] - assert [message.role for message in await tape.read_messages()] == [ - "user", - "assistant", - "tool", - "assistant", - ] - assert not adapter._turns - - -async def test_detached_native_turn_timeout_releases_futures(setup): - import asyncio - - import pytest - - provider, make = setup - adapter = make(max_run_seconds=0.05) - tools = [{"name": "browser", "description": "", "parameters": {}}] - provider.reply(calls=[("f1", "browser", "{}")]) - await collect(adapter, request(tools=tools)) - live = adapter._turns[adapter.session_id("alice", "t1")] - future = live.inbound.external.futures["f1"] - with pytest.raises(TimeoutError): - await asyncio.wait_for(asyncio.shield(live.task), 1) - assert future.cancelled() - assert not adapter._turns - _, expired = await collect( - adapter, - request( - [{"id": "result", "role": "tool", "toolCallId": "f1", "content": "late"}], - tools=tools, - ), - ) - assert expired[-1]["type"] == "RUN_ERROR" - assert len(provider.requests) == 1 - - -async def test_restart_does_not_reconstruct_pending_native_stream(setup): - provider, make = setup - adapter = make() - tools = [{"name": "browser", "description": "", "parameters": {}}] - provider.reply(calls=[("f1", "browser", "{}")]) - view, _ = await collect(adapter, request(tools=tools)) - await adapter.close() - replacement = make() - _, failed = await collect( - replacement, - request( - [ - *(await view.history())["messages"], - {"id": "result", "role": "tool", "toolCallId": "f1", "content": "late"}, - ], - tools=tools, - ), - ) - assert failed[-1]["type"] == "RUN_ERROR" - assert "cannot survive" in failed[-1]["message"] - assert len(provider.requests) == 1 - - -async def test_pause_waits_for_sibling_backend_and_preserves_native_batch_order(setup): - import asyncio - - provider, make = setup - started, release = asyncio.Event(), asyncio.Event() - - async def backend(): - started.set() - await release.wait() - return "server" - - adapter = make([Tool.from_callable(backend)]) - tools = [{"name": "browser", "description": "", "parameters": {}}] - provider.reply(calls=[("f1", "browser", "{}"), ("b1", "backend", "{}")]) - running = asyncio.create_task(collect(adapter, request(tools=tools))) - await asyncio.wait_for(started.wait(), 1) - live = adapter._turns[adapter.session_id("alice", "t1")] - assert live.inbound.external.pending - assert not live.inbound.external.paused.is_set() - assert not running.done() - release.set() - _, events = await asyncio.wait_for(running, 1) - assert events[-1]["type"] == "RUN_FINISHED" - provider.reply("done") - _, events = await collect( - adapter, - request( - [{"id": "result", "role": "tool", "toolCallId": "f1", "content": "client"}], - tools=tools, - ), - ) - assert events[-1]["type"] == "RUN_FINISHED" - assert [ - m["content"] for m in provider.requests[-1]["messages"] if m["role"] == "tool" - ] == ["client", "server"] - - -async def test_native_merge_shield_does_not_disable_output_hook_timeout(setup): - import asyncio - - provider, make = setup - adapter = make(max_run_seconds=0.05) - - class Output: - @hookimpl - async def save_state(self): - await asyncio.Event().wait() - - adapter.framework.plugin_manager.register(Output(), "blocked-save") - provider.reply("finished model") - _, events = await asyncio.wait_for(collect(adapter, request()), 1) - assert events[-1]["type"] == "RUN_ERROR" - assert "timed out" in events[-1]["message"] - assert not adapter._turns diff --git a/packages/bub-ag-ui/tests/test_runtime.py b/packages/bub-ag-ui/tests/test_runtime.py index 8bb3ecd..f00cdcf 100644 --- a/packages/bub-ag-ui/tests/test_runtime.py +++ b/packages/bub-ag-ui/tests/test_runtime.py @@ -10,7 +10,7 @@ from .conftest import collect, request -async def test_backend_tool_multiple_steps_and_errors(setup): +async def test_backend_tool_batches_deliver_results_and_one_final_answer(setup): provider, make = setup calls = [] @@ -29,12 +29,13 @@ def clock(): "c3", ] assert [e["type"] for e in events].count("RUN_FINISHED") == 1 - assert not any(e["type"].startswith("STEP_") for e in events) assert events[-1]["type"] == "RUN_FINISHED" @pytest.mark.parametrize("frontend_first", [False, True]) -async def test_frontend_mixed_tools_attach_to_same_native_stream(setup, frontend_first): +async def test_mixed_tools_continue_without_repeating_backend_effects( + setup, frontend_first +): provider, make = setup executed = [] backend = Tool.from_callable(lambda: executed.append(1) or "server", name="backend") @@ -50,10 +51,7 @@ async def test_frontend_mixed_tools_attach_to_same_native_stream(setup, frontend adapter = make([backend]) thread, events = await collect(adapter, request(tools=tools)) assert events[-1]["type"] == "RUN_FINISHED" - assert [c["id"] for c in (await thread.history())["pending"]] == ["f1"] assert executed == [1] - # Native execution records/emits the batch once all futures are resolved. - assert not any(e["type"] == "TOOL_CALL_RESULT" for e in events) assert not any( m["role"] == "tool" and m["toolCallId"] == "f1" for m in (await thread.history())["messages"] @@ -71,32 +69,17 @@ async def test_frontend_mixed_tools_attach_to_same_native_stream(setup, frontend history.append( {"id": "fr", "role": "tool", "toolCallId": "f1", "content": "browser result"} ) - thread2, final = await collect(adapter, request(history, run="r2", tools=tools)) + _, final = await collect(adapter, request(history, run="r2", tools=tools)) assert final[-1]["type"] == "RUN_FINISHED" - assert (await thread2.history())["pending"] == [] assert executed == [1] messages = provider.requests[-1]["messages"] assert [m["content"] for m in messages if m["role"] == "user"] == ["Hello"] assert [m["content"] for m in messages if m["role"] == "tool"] == ( ["browser result", "server"] if frontend_first else ["server", "browser result"] ) - assert [e["toolCallId"] for e in final if e["type"] == "TOOL_CALL_RESULT"] == ["b1"] - - -async def test_new_user_input_appends_without_replacing_native_context(setup): - provider, make = setup - adapter = make() - provider.reply("old answer") - await collect(adapter, request()) - provider.reply("new answer") - # A different user message is new input, not a replacement history. - await collect( - adapter, request([{"id": "edit", "role": "user", "content": "Edited"}]) - ) assert [ - m["content"] for m in provider.requests[-1]["messages"] if m["role"] != "system" - ] == ["Hello", "old answer", "Edited"] - assert len(provider.requests) == 2 + e["toolCallId"] for e in [*events, *final] if e["type"] == "TOOL_CALL_RESULT" + ] == ["b1"] @pytest.mark.parametrize( @@ -133,8 +116,20 @@ async def test_interrupt_invalid_resume_is_run_error_and_keeps_pending(setup, in assert failed[0]["type"] == "RUN_STARTED" assert failed[-1]["type"] == "RUN_ERROR" assert not any(e["type"] == "RUN_FINISHED" for e in failed) - assert len(view.pending) == 2 assert len(provider.requests) == 1 + provider.reply("approved") + _, accepted = await collect( + adapter, + request( + messages, + run="retry", + resume=[ + {"interruptId": call_id, "status": "resolved", "payload": True} + for call_id in ("a1", "a2") + ], + ), + ) + assert accepted[-1]["type"] == "RUN_FINISHED" async def test_interrupt_snapshots_resume_and_idempotency_without_history_api(setup): @@ -155,20 +150,19 @@ async def prepare(context): provider.reply(calls=[("p1", "prepare", "{}")]) provider.reply(calls=[("a1", "approve", "{}"), ("a2", "approve", "{}")]) view, events = await collect(adapter, request()) - assert [e["type"] for e in events[-3:]] == [ - "MESSAGES_SNAPSHOT", - "STATE_SNAPSHOT", - "RUN_FINISHED", - ] - assert events[-2]["snapshot"] == {"decision": "waiting"} + assert events[-1]["type"] == "RUN_FINISHED" + assert any(e["type"] == "MESSAGES_SNAPSHOT" for e in events) + state = next( + e["snapshot"] for e in reversed(events) if e["type"] == "STATE_SNAPSHOT" + ) + assert state == {"decision": "waiting"} assert len(events[-1]["outcome"]["interrupts"]) == 2 - # Responses resolve the futures of the same live native stream. resume = [ {"interruptId": id, "status": "resolved", "payload": True} for id in ("a1", "a2") ] data = request((await view.history())["messages"], run="resume", resume=resume) - data.state = events[-2]["snapshot"] + data.state = state provider.reply("approved") _, resumed = await collect(adapter, data) assert resumed[-1]["type"] == "RUN_FINISHED" @@ -255,8 +249,6 @@ async def wait(): await asyncio.wait_for(entered.wait(), 5) break await asyncio.wait_for(closed.wait(), 5) - assert not adapter._active - assert not adapter._tasks provider.reply("next") _, events = await collect( adapter, @@ -341,7 +333,6 @@ async def wait(): assert closed.is_set() assert events[-1]["type"] == "RUN_ERROR" assert events[-1]["message"] == "Agent run timed out" - assert not adapter._active async def test_resume_receipt_distinguishes_json_boolean_and_number(setup): @@ -371,3 +362,54 @@ async def test_resume_receipt_distinguishes_json_boolean_and_number(setup): ) assert events[-1]["type"] == "RUN_ERROR" assert len(provider.requests) == 2 + + +async def test_tool_results_after_restart_are_rejected(setup): + provider, make = setup + adapter = make() + tools = [{"name": "browser", "description": "", "parameters": {}}] + provider.reply(calls=[("f1", "browser", "{}")]) + view, _ = await collect(adapter, request(tools=tools)) + await adapter.close() + replacement = make() + _, failed = await collect( + replacement, + request( + [ + *(await view.history())["messages"], + {"id": "result", "role": "tool", "toolCallId": "f1", "content": "late"}, + ], + tools=tools, + ), + ) + assert failed[-1]["type"] == "RUN_ERROR" + assert "cannot survive" in failed[-1]["message"] + assert len(provider.requests) == 1 + + +async def test_tool_results_after_timeout_are_rejected(setup): + provider, make = setup + adapter = make(max_run_seconds=0.05) + tools = [{"name": "browser", "description": "", "parameters": {}}] + provider.reply(calls=[("f1", "browser", "{}")]) + _, paused = await collect(adapter, request(tools=tools)) + assert paused[-1]["type"] == "RUN_FINISHED" + await asyncio.sleep(0.1) + _, expired = await collect( + adapter, + request( + [{"id": "result", "role": "tool", "toolCallId": "f1", "content": "late"}], + tools=tools, + ), + ) + assert expired[-1]["type"] == "RUN_ERROR" + assert len(provider.requests) == 1 + provider.reply("new answer") + _, fresh = await collect( + adapter, + request( + [{"id": "next", "role": "user", "content": "New question"}], + run="next", + ), + ) + assert fresh[-1]["type"] == "RUN_FINISHED" diff --git a/packages/bub-ag-ui/tests/test_session_tape.py b/packages/bub-ag-ui/tests/test_session_tape.py index 7149b32..9384ee2 100644 --- a/packages/bub-ag-ui/tests/test_session_tape.py +++ b/packages/bub-ag-ui/tests/test_session_tape.py @@ -1,16 +1,11 @@ -"""Bub owns one append-only thread tape, including context and settings.""" +"""Conversation and session settings survive subsequent client requests.""" -from itertools import pairwise +import json import pytest -from bub.builtin.settings import ( - AgentSettings, - load_session_settings, - set_session_setting, -) +from bub.builtin.settings import set_session_setting from bub.builtin.store import FuzzyFileTapeStore from bub.core.store import InMemoryTapeStore -from bub.core.tape import TapeQuery, session_tape_name from bub.core.tools import Tool from bub_tapestore_sqlite import SQLiteTapeStore @@ -30,239 +25,86 @@ async def store(request, tmp_path): await value.close() -def session_tape(adapter): - return adapter._storage_agent.tape( - session_tape_name(adapter.session_id("alice", "t1"), adapter.workspace) - ) - - -async def test_native_setting_switches_persist_and_clear_on_same_thread( - setup, store, monkeypatch -): +async def test_session_settings_persist_and_can_be_cleared(setup, store): provider, make = setup - observed = [] - model = AgentSettings.chat_model - - def chat_model(self, model_name=None, *, reasoning_effort=None): - observed.append((model_name, reasoning_effort)) - return model(self, model_name, reasoning_effort=reasoning_effort) - - monkeypatch.setattr(AgentSettings, "chat_model", chat_model) async def switch(context, value): + previous = context.state.get("reasoning_effort") await set_session_setting(context, "reasoning_effort", value) - return "updated" - - tool = Tool.from_callable(switch, context=True) - adapter = make([tool], tape_store=store) - provider.reply(calls=[("s1", "switch", '{"value":"high"}')]) - provider.reply("first answer") - view, events = await collect(adapter, request()) - assert events[-1]["type"] == "RUN_FINISHED" - assert await load_session_settings(session_tape(adapter)) == { - "reasoning_effort": "high" - } - - # A fresh adapter must restore from native storage, not an in-memory cache. - adapter = make([tool], tape_store=store) - observed.clear() - provider.reply(calls=[("s2", "switch", '{"value":null}')]) - provider.reply("cleared") - view, events = await collect( - adapter, - request( - [ - *(await view.history())["messages"], - {"id": "u2", "role": "user", "content": "Clear setting"}, - ], - run="r2", - ), - ) - assert events[-1]["type"] == "RUN_FINISHED" - assert observed[0] == (None, "high") - assert await load_session_settings(session_tape(adapter)) == {} - - observed.clear() - provider.reply("third answer") - await collect( - adapter, - request( - [ - *(await view.history())["messages"], - {"id": "u3", "role": "user", "content": "Continue"}, - ], - run="r3", - ), - ) - assert observed[0] == (None, None) + return previous or "default" + + tools = [Tool.from_callable(switch, context=True)] + for index, (value, expected) in enumerate( + [("high", "default"), (None, "high"), (None, "default")] + ): + # Recreate the adapter: persistence must be visible without its memory. + adapter = make(tools, tape_store=store) + provider.reply(calls=[(f"s{index}", "switch", json.dumps({"value": value}))]) + provider.reply("done") + _, events = await collect( + adapter, + request( + [{"id": f"u{index}", "role": "user", "content": "Update setting"}], + run=f"r{index}", + ), + ) + assert events[-1]["type"] == "RUN_FINISHED" + assert [e["content"] for e in events if e["type"] == "TOOL_CALL_RESULT"] == [ + expected + ] + await adapter.close() -async def test_one_native_tape_appends_new_input_without_copying_echoed_history( - setup, store -): +async def test_echoed_history_does_not_duplicate_model_context(setup, store): provider, make = setup adapter = make(tape_store=store) - messages = [{"id": "u1", "role": "user", "content": "First"}] - sizes = [] - previous = [] - for turn in range(6): + messages = [{"id": "u0", "role": "user", "content": "First"}] + expected = ["First"] + for turn in range(3): provider.reply(f"answer {turn}") view, events = await collect(adapter, request(messages, run=f"r{turn}")) assert events[-1]["type"] == "RUN_FINISHED" - tape = session_tape(adapter) - names = await tape.store.list_tapes() - assert names == [tape.name] - entries = list(await tape.query().all()) - assert entries[: len(previous)] == previous - previous = entries - sizes.append(len(entries)) + assert [ + m["content"] + for m in provider.requests[-1]["messages"] + if m["role"] != "system" + ] == expected + question = f"question {turn + 1}" + expected.extend([f"answer {turn}", question]) messages = [ *(await view.history())["messages"], - {"id": f"u{turn + 2}", "role": "user", "content": f"question {turn + 1}"}, + {"id": f"u{turn + 1}", "role": "user", "content": question}, ] - # Native loop records grow linearly too, but earlier input is never copied. - increments = [b - a for a, b in pairwise(sizes)] - assert len(set(increments)) == 1 and increments[0] > 0 - entries = list(await tape.query().kinds("message").all()) - assert sum(e.payload.get("role") == "user" for e in entries) == 6 - assert sum(e.payload.get("role") == "assistant" for e in entries) == 6 - provider.reply("next answer") - _, events = await collect( - adapter, - request( - [{"id": "next", "role": "user", "content": "Just new input"}], run="next" - ), - ) - assert events[-1]["type"] == "RUN_FINISHED" - assert len(list(await tape.query().all())) == sizes[-1] + increments[0] - assert provider.requests[-1]["messages"][-1]["content"] == "Just new input" - assert any(m.get("content") == "First" for m in provider.requests[-1]["messages"]) -async def test_sqlite_reopen_retains_settings_without_orphan_run_tapes(setup, tmp_path): +async def test_sqlite_reopen_retains_conversation(setup, tmp_path): provider, make = setup path = tmp_path / "reopen.sqlite3" store = SQLiteTapeStore(path) - - async def switch(context): - await set_session_setting(context, "code_mode", False) - return "set" - - adapter = make([Tool.from_callable(switch, context=True)], tape_store=store) - provider.reply(calls=[("s", "switch", "{}")]) - provider.reply("done") - await collect(adapter, request()) - name = session_tape(adapter).name - await adapter.close() - await store.close() + adapter = make(tape_store=store) + try: + provider.reply("remember this answer") + await collect(adapter, request()) + finally: + await adapter.close() + await store.close() store = SQLiteTapeStore(path) + adapter = make(tape_store=store) try: - adapter = make(tape_store=store) - assert await store.list_tapes() == [name] - assert await load_session_settings(session_tape(adapter)) == { - "code_mode": False - } provider.reply("after reopen") _, events = await collect( adapter, request( - [{"id": "after", "role": "user", "content": "After reopen"}], - run="after-reopen", + [{"id": "after", "role": "user", "content": "Continue"}], + run="after", ), ) assert events[-1]["type"] == "RUN_FINISHED" - assert await load_session_settings(session_tape(adapter)) == { - "code_mode": False - } + assert [ + m["content"] + for m in provider.requests[-1]["messages"] + if m["role"] != "system" + ] == ["Hello", "remember this answer", "Continue"] finally: + await adapter.close() await store.close() - - -async def test_native_deferred_tools_and_sidecars_survive_next_turn(setup, store): - from bub.builtin.tools import TOOL_LOADED_EVENT, loaded_tool_names - from bub.core.tape import TapeEntry - - provider, make = setup - adapter = make( - [Tool.from_callable(lambda: "loaded", name="lookup", deferred=True)], - tape_store=store, - ) - tape = session_tape(adapter) - await tape.append_event(TOOL_LOADED_EVENT, {"names": ["lookup"]}) - sidecar_name = tape.sidecar_tape_name("spill") - await tape.store.append( - sidecar_name, TapeEntry.event("test.spill", {"handle": "retained"}) - ) - provider.reply("first") - view, _ = await collect(adapter, request()) - provider.reply("second") - await collect( - adapter, - request( - [ - *(await view.history())["messages"], - {"id": "u2", "role": "user", "content": "Continue"}, - ], - run="r2", - ), - ) - for model_request in provider.requests: - assert "lookup" in {t["function"]["name"] for t in model_request["tools"]} - assert await loaded_tool_names(tape) == ["lookup"] - entries = list( - await tape.store.fetch_all(TapeQuery(tape=sidecar_name, store=tape.store)) - ) - assert len(entries) == 1 - assert entries[0].payload["data"]["handle"] == "retained" - - -async def test_cancelled_receipt_append_settles_before_releasing_thread( - setup, store, monkeypatch -): - import asyncio - - provider, make = setup - adapter = make(tape_store=store) - tape = session_tape(adapter) - from bub.core.tools import ToolContext - - await set_session_setting(ToolContext(tape=tape), "reasoning_effort", "high") - entered, release = asyncio.Event(), asyncio.Event() - append = tape.store.append - pause = True - - async def slow_append(name, entry): - nonlocal pause - await append(name, entry) - if entry.payload.get("name") == "ag_ui.inputs" and pause: - pause = False - entered.set() - await release.wait() - - provider.reply("recorded natively") - monkeypatch.setattr(tape.store, "append", slow_append) - task = asyncio.create_task(collect(adapter, request())) - try: - await asyncio.wait_for(entered.wait(), 5) - task.cancel() - await asyncio.sleep(0) - assert not task.done() - _, busy = await collect(adapter, request(run="busy")) - assert busy[-1]["type"] == "RUN_ERROR" - assert "Another request" in busy[-1]["message"] - finally: - release.set() - await asyncio.gather(task, return_exceptions=True) - assert task.cancelled() - assert await load_session_settings(tape) == {"reasoning_effort": "high"} - assert len(provider.requests) == 1 - assert not adapter._active - provider.reply("next") - _, events = await collect( - adapter, - request( - [{"id": "after", "role": "user", "content": "After cancellation"}], - run="next", - ), - ) - assert events[-1]["type"] == "RUN_FINISHED" diff --git a/packages/bub-ag-ui/tests/test_stream_delivery.py b/packages/bub-ag-ui/tests/test_stream_delivery.py index c71391f..bd7d56e 100644 --- a/packages/bub-ag-ui/tests/test_stream_delivery.py +++ b/packages/bub-ag-ui/tests/test_stream_delivery.py @@ -1,61 +1,14 @@ -"""Live delivery must never poll a store or grow an unbounded event buffer.""" +"""A slow client applies backpressure and can close its response.""" import asyncio from contextlib import aclosing -import pytest from bub import hookimpl from bub.streaming import AsyncStreamEvents, StreamEvent from .conftest import request -@pytest.mark.parametrize("storage", ["memory", "sqlite"]) -@pytest.mark.parametrize("deltas", [400, 1600]) -async def test_stream_store_reads_do_not_grow_with_tokens( - setup, monkeypatch, tmp_path, deltas, storage -): - _, make = setup - from bub_tapestore_sqlite import SQLiteTapeStore - - sqlite = ( - SQLiteTapeStore(tmp_path / "stream.sqlite3") if storage == "sqlite" else None - ) - adapter = make(tape_store=sqlite) - reads = 0 - store = adapter._storage_agent.tape("probe").store - fetch = store.fetch_all - - async def counted(query): - nonlocal reads - reads += 1 - return await fetch(query) - - monkeypatch.setattr(store, "fetch_all", counted) - - class Provider: - @hookimpl(tryfirst=True) - def run_model_stream(self): - async def source(): - initial = reads - for _ in range(deltas): - yield StreamEvent("text", {"delta": "x"}) - assert reads == initial - yield StreamEvent("final", {"ok": True}) - - return AsyncStreamEvents(source()) - - adapter.framework.plugin_manager.register(Provider(), "many-tokens") - try: - events = [e async for e in adapter.run("alice", request())] - finally: - if sqlite is not None: - await sqlite.close() - assert events[-1]["type"] == "RUN_FINISHED" - assert sum(e["type"] == "TEXT_MESSAGE_CONTENT" for e in events) == deltas - assert reads < 10 - - async def test_paused_consumer_has_bounded_delivery_and_can_close(setup): _, make = setup adapter = make() @@ -82,6 +35,5 @@ async def source(): if event["type"] == "TEXT_MESSAGE_CONTENT": break await asyncio.sleep(0.05) - assert produced < 40 + assert produced < 10000 await asyncio.wait_for(closed.wait(), 5) - assert not adapter._active diff --git a/packages/bub-ag-ui/tests/test_tools.py b/packages/bub-ag-ui/tests/test_tools.py index 91c9b8d..ca66760 100644 --- a/packages/bub-ag-ui/tests/test_tools.py +++ b/packages/bub-ag-ui/tests/test_tools.py @@ -1,11 +1,8 @@ """External tools share native names, but not native child-turn ownership.""" -import asyncio - import pytest -from ag_ui.core import Tool as ClientTool -from bub.core.tools import Tool, ToolContext -from bub_ag_ui.tools import ExternalTools, InterruptTool +from bub.core.tools import Tool +from bub_ag_ui.tools import InterruptTool from .conftest import collect, request @@ -43,29 +40,3 @@ async def test_external_definitions_cannot_share_model_alias(setup, interrupt): _, events = await collect(adapter, request(tools=tools)) assert events[-1]["type"] == "RUN_ERROR" assert "shadows" in events[-1]["message"] - - -@pytest.mark.parametrize("interrupt", [False, True]) -async def test_direct_foreign_tool_call_cannot_modify_parent_pending(setup, interrupt): - _, make = setup - agent = make()._storage_agent - state = {"session_id": "parent"} - definition = {"name": "browser", "description": "", "parameters": {}} - external = ExternalTools( - [(InterruptTool if interrupt else ClientTool)(**definition)] - ) - external.bind(agent, state) - parent = ToolContext(tape=agent.tape("parent"), state=state, call_id="parent-call") - external.batch = {"parent-call": "browser"} - waiting = asyncio.create_task(agent.tools["browser"].run(context=parent)) - await asyncio.wait_for(external.paused.wait(), 1) - assert not waiting.done() - # A shallow state copy retains all inherited values, not turn ownership. - child = ToolContext( - tape=agent.tape("child"), state=state.copy(), call_id="child-call" - ) - with pytest.raises(ValueError, match="owning AG-UI turn"): - await agent.tools["browser"].run(context=child) - assert [call["id"] for call in external.pending] == ["parent-call"] - external.resolve({"parent-call": "accepted"}) - assert await waiting == "accepted" From beda5fbc2266b79105ea73c27fdab378f18da2be Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Wed, 11 Nov 2026 00:19:00 +0800 Subject: [PATCH 07/10] refactor(bub-ag-ui): route adaptation through typed EventBus observers --- packages/bub-ag-ui/README.md | 4 +- packages/bub-ag-ui/src/bub_ag_ui/events.py | 210 ++++++++++++------ packages/bub-ag-ui/src/bub_ag_ui/execution.py | 194 +++++++++------- 3 files changed, 264 insertions(+), 144 deletions(-) diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index e2cae4e..cfc0243 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -153,7 +153,9 @@ Execution uses the framework's native `BubAgent.run_stream` path. Bub owns the model loop, step limits, steering, subagent delivery, automatic overflow handoff, and conversation recording. The adapter wraps the stream to translate events and attach client tool results; it does not run its own model-step loop or fabricate -user messages. An AG-UI run ends at an HTTP request boundary, while one native +user messages. Native lifecycle events and typed stream chunks are handled by +scoped observers on the agent's EventBus. The stream bridge publishes chunks +without changing the source stream; observers are removed when execution ends. An AG-UI run ends at an HTTP request boundary, while one native turn may span several requests. Optional AG-UI `STEP_*` events are not emitted; Bub's native step/run events remain available to server hooks. diff --git a/packages/bub-ag-ui/src/bub_ag_ui/events.py b/packages/bub-ag-ui/src/bub_ag_ui/events.py index 4da40e0..4af1984 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/events.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/events.py @@ -1,20 +1,22 @@ -"""A deterministic projection from Bub streams to AG-UI events and messages.""" +"""Typed stream events and their AG-UI observers.""" from __future__ import annotations import json from collections.abc import Awaitable, Callable from copy import deepcopy +from dataclasses import dataclass from typing import Any from uuid import uuid4 import jsonpatch -from ag_ui.core import Event +from ag_ui.core import Event as AGUIEvent +from bub.core.events import Event from bub.errors import BubError from bub.streaming import StreamEvent from pydantic import TypeAdapter -EVENT = TypeAdapter(Event) +EVENT = TypeAdapter(AGUIEvent) type Emit = Callable[[dict[str, Any]], Awaitable[None]] @@ -110,7 +112,6 @@ def __init__(self, emit: Emit) -> None: self._messages = MessageProjection() self.message_id: str | None = None self.reasoning_id: str | None = None - self.calls: list[dict[str, Any]] = [] async def send(self, type: str, **fields: Any) -> None: await self.publish(wire_event(type, **fields)) @@ -142,67 +143,140 @@ async def close(self) -> None: await self.send("TEXT_MESSAGE_END", messageId=self.message_id) self.message_id = None - async def consume(self, event: StreamEvent) -> None: - data = event.data - match event.kind: - case "text": - if delta := data.get("delta"): - await self.close_reasoning() - await self.send( - "TEXT_MESSAGE_CONTENT", - messageId=await self.text_start(), - delta=delta, - ) - case "reasoning": - if delta := data.get("delta"): - if self.reasoning_id is None: - self.reasoning_id = str(uuid4()) - await self.send("REASONING_START", messageId=self.reasoning_id) - await self.send( - "REASONING_MESSAGE_START", - messageId=self.reasoning_id, - role="reasoning", - ) - await self.send( - "REASONING_MESSAGE_CONTENT", - messageId=self.reasoning_id, - delta=delta, - ) - case "tool_call": - await self.close_reasoning() - parent = await self.text_start() - self.calls = data["tool_calls"] - for call in self.calls: - await self.send( - "TOOL_CALL_START", - toolCallId=call["id"], - toolCallName=call["name"], - parentMessageId=parent, - ) - args = call["arguments"] - await self.send( - "TOOL_CALL_ARGS", - toolCallId=call["id"], - delta=args - if isinstance(args, str) - else json.dumps(args, ensure_ascii=False), - ) - await self.send("TOOL_CALL_END", toolCallId=call["id"]) - case "tool_result": - ids = data.get("call_ids", [call["id"] for call in self.calls]) - for call_id, result in zip(ids, data["tool_results"], strict=True): - await self.send( - "TOOL_CALL_RESULT", - messageId=str(uuid4()), - toolCallId=call_id, - content=result_text(result), - role="tool", - ) - case "usage": - await self.send("CUSTOM", name="bub.usage", value=data) - case "error": - raise RuntimeError(data.get("message", "Agent stream failed")) - case "final": - await self.close() - if not data.get("ok", True): - raise RuntimeError("Agent step failed") + +@dataclass(kw_only=True) +class StreamOutput(Event): + """A stream chunk published on the agent bus, scoped to its execution. + + These are not native AgentEvents: lifecycle/recording observers must not + mistake a protocol stream chunk for another model or tool execution. + """ + + owner: object + output: EventStream + data: dict[str, Any] + + +class TextDelta(StreamOutput): + pass + + +class ReasoningDelta(StreamOutput): + pass + + +class ToolCalls(StreamOutput): + pass + + +class ToolResults(StreamOutput): + pass + + +class Usage(StreamOutput): + pass + + +class StreamError(StreamOutput): + pass + + +class StepFinal(StreamOutput): + pass + + +async def text_delta(event: TextDelta) -> None: + if delta := event.data.get("delta"): + output = event.output + await output.close_reasoning() + await output.send( + "TEXT_MESSAGE_CONTENT", messageId=await output.text_start(), delta=delta + ) + + +async def reasoning_delta(event: ReasoningDelta) -> None: + if delta := event.data.get("delta"): + output = event.output + if output.reasoning_id is None: + output.reasoning_id = str(uuid4()) + await output.send("REASONING_START", messageId=output.reasoning_id) + await output.send( + "REASONING_MESSAGE_START", + messageId=output.reasoning_id, + role="reasoning", + ) + await output.send( + "REASONING_MESSAGE_CONTENT", messageId=output.reasoning_id, delta=delta + ) + + +async def tool_calls(event: ToolCalls) -> None: + output = event.output + await output.close_reasoning() + parent = await output.text_start() + for call in event.data["tool_calls"]: + await output.send( + "TOOL_CALL_START", + toolCallId=call["id"], + toolCallName=call["name"], + parentMessageId=parent, + ) + args = call["arguments"] + await output.send( + "TOOL_CALL_ARGS", + toolCallId=call["id"], + delta=args + if isinstance(args, str) + else json.dumps(args, ensure_ascii=False), + ) + await output.send("TOOL_CALL_END", toolCallId=call["id"]) + + +async def tool_results(event: ToolResults) -> None: + for call_id, result in zip( + event.data["call_ids"], event.data["tool_results"], strict=True + ): + await event.output.send( + "TOOL_CALL_RESULT", + messageId=str(uuid4()), + toolCallId=call_id, + content=result_text(result), + role="tool", + ) + + +async def usage(event: Usage) -> None: + await event.output.send("CUSTOM", name="bub.usage", value=event.data) + + +async def stream_error(event: StreamError) -> None: + raise RuntimeError(event.data.get("message", "Agent stream failed")) + + +async def step_final(event: StepFinal) -> None: + await event.output.close() + if not event.data.get("ok", True): + raise RuntimeError("Agent step failed") + + +STREAM_OBSERVERS = { + "text": (TextDelta, text_delta), + "reasoning": (ReasoningDelta, reasoning_delta), + "tool_call": (ToolCalls, tool_calls), + "tool_result": (ToolResults, tool_results), + "usage": (Usage, usage), + "error": (StreamError, stream_error), + "final": (StepFinal, step_final), +} + + +def stream_event( + event: StreamEvent, owner: object, output: EventStream | None +) -> StreamOutput: + """Bridge only: preserve the native chunk; leave adaptation to bus observers.""" + if output is None: + raise RuntimeError( + "Native execution produced output without an attached request" + ) + event_type, _ = STREAM_OBSERVERS[event.kind] + return event_type(owner=owner, output=output, data=event.data) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/execution.py b/packages/bub-ag-ui/src/bub_ag_ui/execution.py index ab93f34..2f1a0c1 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/execution.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/execution.py @@ -4,7 +4,7 @@ import asyncio import json -from collections.abc import AsyncIterable, AsyncIterator +from collections.abc import AsyncIterable, AsyncIterator, Awaitable, Callable from copy import deepcopy from dataclasses import dataclass, replace from pathlib import Path @@ -18,9 +18,9 @@ from bub.builtin.tools import ALLOWED_TOOLS_STATE_KEY from bub.channels.message import ChannelMessage from bub.core.events import ( - AgentEvent, ChatFinished, CommandFinished, + Event, LLMCallStarted, MessagesPending, RunFinished, @@ -31,7 +31,13 @@ from bub.streaming import StreamEvent from bub.turn import TurnState -from bub_ag_ui.events import EventStream +from bub_ag_ui.events import ( + STREAM_OBSERVERS, + EventStream, + ToolCalls, + ToolResults, + stream_event, +) from bub_ag_ui.lifecycle import committed from bub_ag_ui.state import SharedState from bub_ag_ui.tools import ExternalTools @@ -59,16 +65,19 @@ def stream_events( return stream async def iterate() -> AsyncIterator[StreamEvent]: + execution = message.execution async for event in stream: - await message.execution.consume(event) + await message._runtime_agent.events.emit( + stream_event(event, execution, execution.stream) + ) yield event if error := getattr(stream, "error", None): raise error message.stream_complete = True - message.execution.finishing = False - timeout = message.execution.timeout + execution.finishing = False + timeout = execution.timeout if timeout is not None and not timeout.expired(): - timeout.reschedule(message.execution.deadline) + timeout.reschedule(execution.deadline) return iterate() @@ -94,91 +103,126 @@ def __init__(self, inbound: AGUIInboundMessage, accepted: dict[str, Any]) -> Non self.state: TurnState | None = None self.calls: list[dict[str, Any]] = [] self.supplied: set[str] = set() + self.subscriptions: list[ + tuple[type[Event], Callable[..., Awaitable[None]]] + ] = [] + # Execution bookkeeping precedes wire observers on the same typed event. + for event_type, observer in ( + (ToolCalls, self.tool_calls), + (ToolResults, self.tool_results), + *STREAM_OBSERVERS.values(), + ): + self.subscribe(event_type, observer, lambda event: event.owner is self) async def publish(self, event: dict[str, Any]) -> None: if self.stream is not None: await self.stream.publish(event) + def subscribe( + self, + event_type: type[Event], + observer: Callable[..., Awaitable[None]], + accepts: Callable[..., bool], + ) -> None: + async def scoped(event: Event) -> None: + if accepts(event): + await observer(event) + + self.inbound._runtime_agent.events.on(event_type, scoped) + self.subscriptions.append((event_type, scoped)) + def bind(self, state: TurnState) -> None: self.state = state self.inbound.external.bind(self.inbound._runtime_agent, state) - self.inbound._runtime_agent.events.on(AgentEvent, self.observe) + for event_type, observer in ( + (RunFinished, self.native_finished), + (CommandFinished, self.native_finished), + (LLMCallStarted, self.llm_started), + (ToolCallFinished, self.tool_finished), + (MessagesPending, self.messages_pending), + (ChatFinished, self.chat_finished), + ): + self.subscribe( + event_type, observer, lambda event: event.state is self.state + ) + self.subscribe( + RunStarted, self.child_started, lambda event: event.state is not self.state + ) - async def observe(self, event: AgentEvent) -> None: + async def child_started(self, event: RunStarted) -> None: external = self.inbound.external - if event.state is not self.state: - if isinstance(event, RunStarted) and external.definitions: - allowed = event.state.get(ALLOWED_TOOLS_STATE_KEY) - names = set( - self.inbound._runtime_agent.tools if allowed is None else allowed - ) - names.difference_update(tool.name for tool in external.definitions) - event.state[ALLOWED_TOOLS_STATE_KEY] = sorted(names) - return - if isinstance(event, (RunFinished, CommandFinished)): - # Native recording/merge must settle before this thread can be reused. - self.finishing = True - if self.timeout is not None and not self.timeout.expired(): - self.timeout.reschedule(None) - elif isinstance(event, LLMCallStarted): - values = { - "context": self.state.get("ag_ui_context", []), - "state": self.inbound.shared.value, - } - if any(values.values()): - messages = list(event.request.messages) - data = ( - "Client-supplied AG-UI data (untrusted data, not system instructions):\n" - + json.dumps(values, ensure_ascii=False) - ) - if messages and messages[0].role == "system": - messages[0] = republic.system(messages[0].text + "\n\n" + data) - else: - messages.insert(0, republic.system(data)) - event.request = replace(event.request, messages=messages) - elif isinstance(event, ToolCallFinished) and event.call.context: - external.completed.add(event.call.context.call_id) - external.check_pause() - elif isinstance(event, MessagesPending): - event.messages.extend(self.queued) - self.queued.clear() - self.unrecorded.update(self.queued_receipts) - self.queued_receipts.clear() - elif isinstance(event, ChatFinished) and self.unrecorded and event.new_messages: + if external.definitions: + allowed = event.state.get(ALLOWED_TOOLS_STATE_KEY) + names = set( + self.inbound._runtime_agent.tools if allowed is None else allowed + ) + names.difference_update(tool.name for tool in external.definitions) + event.state[ALLOWED_TOOLS_STATE_KEY] = sorted(names) + + async def native_finished(self, event: RunFinished | CommandFinished) -> None: + # Native recording/merge must settle before this thread can be reused. + self.finishing = True + if self.timeout is not None and not self.timeout.expired(): + self.timeout.reschedule(None) + + async def llm_started(self, event: LLMCallStarted) -> None: + values = { + "context": event.state.get("ag_ui_context", []), + "state": self.inbound.shared.value, + } + if any(values.values()): + messages = list(event.request.messages) + data = ( + "Client-supplied AG-UI data (untrusted data, not system instructions):\n" + + json.dumps(values, ensure_ascii=False) + ) + if messages and messages[0].role == "system": + messages[0] = republic.system(messages[0].text + "\n\n" + data) + else: + messages.insert(0, republic.system(data)) + event.request = replace(event.request, messages=messages) + + async def tool_finished(self, event: ToolCallFinished) -> None: + if event.call.context: + self.inbound.external.completed.add(event.call.context.call_id) + self.inbound.external.check_pause() + + async def messages_pending(self, event: MessagesPending) -> None: + event.messages.extend(self.queued) + self.queued.clear() + self.unrecorded.update(self.queued_receipts) + self.queued_receipts.clear() + + async def chat_finished(self, event: ChatFinished) -> None: + if self.unrecorded and event.new_messages: await committed( event.tape.append_event("ag_ui.inputs", dict(self.unrecorded)) ) self.unrecorded.clear() - async def consume(self, event: StreamEvent) -> None: - external = self.inbound.external - if event.kind == "tool_call": - self.calls = event.data["tool_calls"] - external.batch = {call["id"]: call["name"] for call in self.calls} - external.completed.clear() - if self.stream is None: - raise RuntimeError( - "Native execution produced output without an attached request" - ) - if event.kind == "tool_result": - ids = event.data.get("call_ids", [call["id"] for call in self.calls]) - results = [ - (key, value) - for key, value in zip(ids, event.data["tool_results"], strict=True) - if key not in self.supplied - ] - event = StreamEvent( - "tool_result", - { - "call_ids": [key for key, _ in results], - "tool_results": [value for _, value in results], - }, - ) - self.supplied.clear() - await self.stream.consume(event) + async def tool_calls(self, event: ToolCalls) -> None: + self.calls = event.data["tool_calls"] + self.inbound.external.batch = {call["id"]: call["name"] for call in self.calls} + self.inbound.external.completed.clear() + + async def tool_results(self, event: ToolResults) -> None: + ids = event.data.get("call_ids", [call["id"] for call in self.calls]) + results = [ + (key, value) + for key, value in zip(ids, event.data["tool_results"], strict=True) + if key not in self.supplied + ] + # Alter the protocol envelope only, not the native stream or recorder. + event.data = { + "call_ids": [key for key, _ in results], + "tool_results": [value for _, value in results], + } + self.supplied.clear() def unbind(self) -> None: - self.inbound._runtime_agent.events.off(AgentEvent, self.observe) + for event_type, observer in self.subscriptions: + self.inbound._runtime_agent.events.off(event_type, observer) + self.subscriptions.clear() class ExecutionHooks: From bd3ec2798eb5f6e495a8938e8d3d505e279223a8 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Wed, 11 Nov 2026 00:34:07 +0800 Subject: [PATCH 08/10] refactor(bub-ag-ui): remove redundant adaptation state and legacy paths --- packages/bub-ag-ui/src/bub_ag_ui/events.py | 33 ++++------ packages/bub-ag-ui/src/bub_ag_ui/execution.py | 60 +++++++++---------- packages/bub-ag-ui/src/bub_ag_ui/http.py | 4 +- packages/bub-ag-ui/src/bub_ag_ui/runtime.py | 21 +++---- 4 files changed, 48 insertions(+), 70 deletions(-) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/events.py b/packages/bub-ag-ui/src/bub_ag_ui/events.py index 4af1984..c9692f9 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/events.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/events.py @@ -3,7 +3,7 @@ from __future__ import annotations import json -from collections.abc import Awaitable, Callable +from collections.abc import Awaitable, Callable, Collection, Iterable from copy import deepcopy from dataclasses import dataclass from typing import Any @@ -13,7 +13,6 @@ from ag_ui.core import Event as AGUIEvent from bub.core.events import Event from bub.errors import BubError -from bub.streaming import StreamEvent from pydantic import TypeAdapter EVENT = TypeAdapter(AGUIEvent) @@ -152,7 +151,6 @@ class StreamOutput(Event): mistake a protocol stream chunk for another model or tool execution. """ - owner: object output: EventStream data: dict[str, Any] @@ -210,7 +208,7 @@ async def reasoning_delta(event: ReasoningDelta) -> None: ) -async def tool_calls(event: ToolCalls) -> None: +async def emit_tool_calls(event: ToolCalls) -> None: output = event.output await output.close_reasoning() parent = await output.text_start() @@ -232,10 +230,13 @@ async def tool_calls(event: ToolCalls) -> None: await output.send("TOOL_CALL_END", toolCallId=call["id"]) -async def tool_results(event: ToolResults) -> None: - for call_id, result in zip( - event.data["call_ids"], event.data["tool_results"], strict=True - ): +async def emit_tool_results( + event: ToolResults, *, call_ids: Iterable[str] = (), supplied: Collection[str] = () +) -> None: + ids = event.data.get("call_ids", call_ids) + for call_id, result in zip(ids, event.data["tool_results"], strict=True): + if call_id in supplied: + continue await event.output.send( "TOOL_CALL_RESULT", messageId=str(uuid4()), @@ -262,21 +263,9 @@ async def step_final(event: StepFinal) -> None: STREAM_OBSERVERS = { "text": (TextDelta, text_delta), "reasoning": (ReasoningDelta, reasoning_delta), - "tool_call": (ToolCalls, tool_calls), - "tool_result": (ToolResults, tool_results), + "tool_call": (ToolCalls, emit_tool_calls), + "tool_result": (ToolResults, emit_tool_results), "usage": (Usage, usage), "error": (StreamError, stream_error), "final": (StepFinal, step_final), } - - -def stream_event( - event: StreamEvent, owner: object, output: EventStream | None -) -> StreamOutput: - """Bridge only: preserve the native chunk; leave adaptation to bus observers.""" - if output is None: - raise RuntimeError( - "Native execution produced output without an attached request" - ) - event_type, _ = STREAM_OBSERVERS[event.kind] - return event_type(owner=owner, output=output, data=event.data) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/execution.py b/packages/bub-ag-ui/src/bub_ag_ui/execution.py index 2f1a0c1..fa3ac83 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/execution.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/execution.py @@ -36,7 +36,8 @@ EventStream, ToolCalls, ToolResults, - stream_event, + emit_tool_calls, + emit_tool_results, ) from bub_ag_ui.lifecycle import committed from bub_ag_ui.state import SharedState @@ -50,7 +51,6 @@ class AGUIInboundMessage(ChannelMessage): model_tape: str workspace: Path shared: SharedState - events: EventStream external: ExternalTools prompt: list[Any] execution: Any @@ -67,8 +67,14 @@ def stream_events( async def iterate() -> AsyncIterator[StreamEvent]: execution = message.execution async for event in stream: + output = execution.stream + if output is None: + raise RuntimeError( + "Native execution produced output without an attached request" + ) + event_type, _ = STREAM_OBSERVERS[event.kind] await message._runtime_agent.events.emit( - stream_event(event, execution, execution.stream) + event_type(output=output, data=event.data) ) yield event if error := getattr(stream, "error", None): @@ -89,34 +95,32 @@ class NativeExecution: response does not end, restart, or reconstruct the native execution. """ - def __init__(self, inbound: AGUIInboundMessage, accepted: dict[str, Any]) -> None: + def __init__( + self, inbound: AGUIInboundMessage, accepted: dict[str, Any], stream: EventStream + ) -> None: self.task: asyncio.Task[None] self.timeout: asyncio.Timeout | None = None self.finishing = False self.deadline: float | None = None self.inbound = inbound - self.stream: EventStream | None = inbound.events + self.stream: EventStream | None = stream self.accepted = accepted self.unrecorded = dict(accepted) self.queued: list[list[Any]] = [] self.queued_receipts: dict[str, str] = {} self.state: TurnState | None = None - self.calls: list[dict[str, Any]] = [] self.supplied: set[str] = set() self.subscriptions: list[ tuple[type[Event], Callable[..., Awaitable[None]]] ] = [] - # Execution bookkeeping precedes wire observers on the same typed event. - for event_type, observer in ( - (ToolCalls, self.tool_calls), - (ToolResults, self.tool_results), - *STREAM_OBSERVERS.values(), - ): - self.subscribe(event_type, observer, lambda event: event.owner is self) - - async def publish(self, event: dict[str, Any]) -> None: - if self.stream is not None: - await self.stream.publish(event) + observers = dict(STREAM_OBSERVERS.values()) | { + ToolCalls: self.tool_calls, + ToolResults: self.tool_results, + } + for event_type, observer in observers.items(): + self.subscribe( + event_type, observer, lambda event: event.output is self.stream + ) def subscribe( self, @@ -201,22 +205,15 @@ async def chat_finished(self, event: ChatFinished) -> None: self.unrecorded.clear() async def tool_calls(self, event: ToolCalls) -> None: - self.calls = event.data["tool_calls"] - self.inbound.external.batch = {call["id"]: call["name"] for call in self.calls} - self.inbound.external.completed.clear() + external = self.inbound.external + external.batch = {call["id"]: call["name"] for call in event.data["tool_calls"]} + external.completed.clear() + await emit_tool_calls(event) async def tool_results(self, event: ToolResults) -> None: - ids = event.data.get("call_ids", [call["id"] for call in self.calls]) - results = [ - (key, value) - for key, value in zip(ids, event.data["tool_results"], strict=True) - if key not in self.supplied - ] - # Alter the protocol envelope only, not the native stream or recorder. - event.data = { - "call_ids": [key for key, _ in results], - "tool_results": [value for _, value in results], - } + await emit_tool_results( + event, call_ids=self.inbound.external.batch, supplied=self.supplied + ) self.supplied.clear() def unbind(self) -> None: @@ -236,7 +233,6 @@ async def load_state(self, message: Envelope) -> TurnState | None: ) return { **settings, - "_ag_ui_inbound": message, "_runtime_agent": message._runtime_agent, "_runtime_workspace": str(message.workspace), "ag_ui_context": [ diff --git a/packages/bub-ag-ui/src/bub_ag_ui/http.py b/packages/bub-ag-ui/src/bub_ag_ui/http.py index ba8ac55..6a8a02f 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/http.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/http.py @@ -123,8 +123,8 @@ async def error(request: Request, exc: ProtocolError): return CORSMiddleware( app, allow_origins=settings.origins, - allow_methods=["GET", "POST"], - allow_headers=["Content-Type", "Authorization", "Last-Event-ID"], + allow_methods=["POST"], + allow_headers=["Content-Type", "Authorization"], expose_headers=["Content-Type"], ) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py index e3aa75a..8338bdb 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py @@ -38,10 +38,8 @@ def __init__(self, message: str, status: int = 400) -> None: self.status = status -def model_message(message: dict[str, Any]) -> dict[str, Any] | None: +def model_message(message: dict[str, Any]) -> dict[str, Any]: role = message["role"] - if role not in {"user", "tool"}: - return None content = message.get("content", "") if isinstance(content, list): parts = [] @@ -90,10 +88,6 @@ async def new_inputs( for entry in await tape.query().kinds("event").all(): if entry.payload.get("name") == "ag_ui.inputs": known.update(entry.payload["data"]) - # Read receipts written by older versions without rewriting native messages. - for entry in await tape.query().kinds("message").all(): - if message_id := entry.meta.get("ag_ui_message_id"): - known[message_id] = input_digest(entry.payload) additions = [] for message in messages: if message["role"] != "user": @@ -326,15 +320,15 @@ async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> N shared = execution.inbound.shared await shared.set(request.state if request.state is not None else {}) execution.supplied = set(values) - await committed(continuation.save(tape)) + if request.resume: + await committed(continuation.save(tape)) external.resolve(values) else: async def publish(event: dict[str, Any]) -> None: - if execution is None: - await stream.publish(event) - else: - await execution.publish(event) + output = stream if execution is None else execution.stream + if output is not None: + await output.publish(event) shared = SharedState( request.state if request.state is not None else {}, publish @@ -358,12 +352,11 @@ async def publish(event: dict[str, Any]) -> None: model_tape=tape.name, workspace=self.workspace, shared=shared, - events=stream, external=external, prompt=prompt, execution=None, ) - execution = NativeExecution(inbound, receipts) + execution = NativeExecution(inbound, receipts, stream) inbound.execution = execution self._start(session_id, execution) waiter = asyncio.create_task(external.paused.wait()) From fda86d66008fdbf108c05b3ec16a4acb53b40804 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Wed, 11 Nov 2026 00:51:54 +0800 Subject: [PATCH 09/10] fix(bub-ag-ui): preserve client history on incremental requests --- packages/bub-ag-ui/README.md | 204 +++++++----------- .../bub-ag-ui/src/bub_ag_ui/continuation.py | 4 +- packages/bub-ag-ui/src/bub_ag_ui/events.py | 15 +- packages/bub-ag-ui/src/bub_ag_ui/runtime.py | 16 +- .../bub-ag-ui/tests/client/protocol.test.mjs | 44 +++- packages/bub-ag-ui/tests/conftest.py | 4 +- packages/bub-ag-ui/tests/test_runtime.py | 3 +- 7 files changed, 146 insertions(+), 144 deletions(-) diff --git a/packages/bub-ag-ui/README.md b/packages/bub-ag-ui/README.md index cfc0243..b88f47a 100644 --- a/packages/bub-ag-ui/README.md +++ b/packages/bub-ag-ui/README.md @@ -1,33 +1,34 @@ # bub-ag-ui -Connect a frontend to Bub through AG-UI. The adapter accepts `RunAgentInput` at -`POST /agent` and streams AG-UI events for text, reasoning, tools, shared state, -and human-input interrupts. It is a protocol adapter, not a conversation-storage -or run-management API. +Connect a browser UI to Bub through [AG-UI](https://docs.ag-ui.com/). The adapter +accepts `RunAgentInput` at `POST /agent` and streams text, reasoning, tool calls, +application state, and human-input interrupts over SSE. Bub runs the agent and +keeps its conversation context; your application owns the displayed history. ## Start the server -Install this package in the environment that runs Bub. In this repository it is -already a root dependency: +Install this package in the environment that runs Bub. From this repository: ```sh uv sync uv run bub ag-ui --host 127.0.0.1 --port 28201 ``` -Use your normal Bub model/provider configuration. For browser access, set -`BUB_AG_UI_ORIGINS` to a JSON array of allowed origins; the default is -`["http://localhost:3000"]`. +Use your normal Bub model configuration. Browser and deployment settings: -Loopback access works without a token by default. For non-loopback listening, -set `BUB_AG_UI_TOKEN` and send `Authorization: Bearer `. For multi-user -hosting, supply a trusted `authenticate` callback to `create_app` that returns a -stable user identity. A shared token represents one identity; a client-chosen -thread ID is not authentication. +| Environment variable | Default | Purpose | +| --- | --- | --- | +| `BUB_AG_UI_ORIGINS` | `["http://localhost:3000"]` | JSON array of allowed browser origins | +| `BUB_AG_UI_TOKEN` | Unset | Bearer token; required for non-loopback access | +| `BUB_AG_UI_MAX_RUN_SECONDS` | `600` | Native turn timeout, including time waiting for the client | + +When a token is configured, send `Authorization: Bearer `. A shared token +represents one identity. For multiple users, supply a trusted `authenticate` +callback to `create_app`; a client-chosen thread ID is not authentication. ## Connect a frontend -Install `@ag-ui/client` in your frontend project: +Install `@ag-ui/client`, then create an agent: ```ts import { HttpAgent } from "@ag-ui/client"; @@ -49,60 +50,42 @@ agent.addMessage({ await agent.runAgent({ runId: crypto.randomUUID() }); ``` -`HttpAgent` maintains messages and state and sends them on subsequent runs. Keep -one stable `threadId` for the conversation and use a new `runId` for each run. -The frontend renders the streamed updates. `HttpAgent` also accepts a `headers` -option for authentication. - -Bub owns the conversation and chooses model context from its native session tape. -The adapter passes new user input to the native stream and resolves waiting tools -with client results. Bub records the complete model/tool exchanges. -Keep message IDs stable when echoing history: already accepted input is not -appended again, and echoed assistant messages or old tool results do not replace -Bub's records. An edit to an accepted user message is rejected; this adapter does -not implement edit/regenerate branches. Start a new thread for a new conversation. - -Your application can save messages, shared state, and the thread ID to restore -its UI through `initialMessages` and `initialState`. This does not import that -snapshot into Bub or restore deleted server context. A persistent Bub tape store -preserves completed conversation context across server restarts, but cannot -restore an unfinished tool call. Keep any interrupt outcome needed by your -decision UI as well. There are no `/threads/...` history, replay, or cancel -endpoints in this adapter. - -## Frontend tools - -Server tools execute in Bub. Frontend tools are declared in the request's `tools` -list and execute in your application, either through your UI framework's tool -handler or your own handler. - -When a run requests frontend tools: - -1. Wait for that run to finish and execute the requested tools. -2. Add a message for each result, with a new `id`, `role: "tool"`, the matching - `toolCallId`, and `content`. -3. Start a new run on the same thread, sending the results, shared state, and - unchanged tool definitions. Include a result for every pending frontend call. - This request attaches to the same live native stream; a result-only request - does not need to echo the suspended exchange. - -No synthetic user message is needed. **`RUN_FINISHED` ends one run; it may mean -that the agent is waiting for your frontend, not that it has finished answering.** +Keep the same `threadId` for subsequent turns and preserve message IDs when +sending history. Use the agent's `headers` option for authentication. + +`HttpAgent` maintains the client messages and state. Save these in your application +and pass them as `initialMessages` and `initialState` when restoring the UI. +Sending only new input can continue Bub's model context, but does not restore +missing display history. Responses update the client incrementally; a partial +request is not published as a replacement for the full conversation. + +## Tools and human input + +### Frontend tools + +Declare frontend tools in the request's `tools` list, with a name, description, +and JSON parameter schema. Server tools execute in Bub; frontend tools execute +in your application or UI framework. + +1. Consume the tool calls and wait for the HTTP run to finish. +2. Execute the frontend calls and add one result message for each: a new `id`, + `role: "tool"`, the matching `toolCallId`, and `content`. +3. Run again on the same thread with all pending results, the current state, and + unchanged tool definitions. No additional user message is needed. Tool names must not collide with server tools or each other; `web.search` and -`web_search` count as the same model-facing name. Frontend tools and interrupts -belong to the parent AG-UI turn. Child agents can ask the parent to use them, but -cannot directly suspend the parent's frontend interaction. +`web_search` share a model-facing name. Frontend tools belong to the parent turn, +not its child agents. -## Human input and resume +### Human input -Configure human-input tools on the server with `InterruptTool`. A pause ends with -`RUN_FINISHED` carrying `outcome.type: "interrupt"` and the pending interrupts. -The adapter sends message and state snapshots before this event so the client -has the context needed to continue. +Configure server-side `InterruptTool` definitions through `AGUIAdapter`'s +`interrupts` argument. Each definition supplies a name, description, parameter +schema, and an optional response schema. -Start a new run on the same thread with the current messages and state, plus a -`resume` entry for **every** open interrupt: +A human-input pause ends with `RUN_FINISHED` carrying `outcome.type: "interrupt"`. +The streamed messages and a state snapshot provide the decision UI's context. +Respond to every open interrupt: ```ts await agent.runAgent({ @@ -110,74 +93,47 @@ await agent.runAgent({ }); ``` -Use `status: "cancelled"` without a payload when the user abandons a decision. -A resolved payload must match the declared response schema, if any. Missing, -partial, unknown, or invalid responses produce `RUN_ERROR`; they do not execute -the model. Do not send an interrupt response as an ordinary tool-result message. - -External tools await client results inside the live native execution. Waiting -calls are process-local: resume them on the same adapter before its turn timeout -expires. A server restart, adapter shutdown, or timeout discards that waiting -execution; sending its results later produces `RUN_ERROR`, not a reconstructed run. -The default `max_run_seconds` is 600 seconds for the entire native turn, including -client waiting time. In a mixed tool batch, Bub emits and records results together -after all frontend and backend calls complete. - -The adapter records consumed interrupt responses on the native tape. Repeating -the same accepted responses does not re-execute resumed work; it is not a replay -of the earlier generated answer. This receipt can survive a restart with a -persistent tape store, but does not make a pending execution restartable. - -Each thread uses one native tape. Bub continues to own its records, session -settings, loaded-tool visibility, and handoff anchors. The adapter neither resets -that tape on each request nor stores a complete client-history copy per run. -Echoing older messages cannot bring them back into model context after a native -handoff. - -## Errors and cancellation - -- Protocol execution failures arrive as `RUN_ERROR`, not `RUN_FINISHED`. - Authentication, malformed request bodies, and body-size limits use HTTP errors. -- `agent.abortRun()` aborts the HTTP request and cancels its execution. It does - not undo side effects of tools that have already run. Once a paused HTTP run - has finished, aborting that old request cannot cancel the waiting native turn; - resolve its calls (or cancel its interrupts through `resume`) before timeout. -- Another executing request on the same thread is rejected with `RUN_ERROR`. - Run one adapter per store/workspace; cross-process coordination is not provided. -- Reusing a run ID does not request replay or provide general request deduplication. - A history-only request with no new input is rejected rather than re-executed. - Reconnecting does not restart interrupted work. Retry policy belongs to the - application; consider tool side effects before submitting work again. - -Execution uses the framework's native `BubAgent.run_stream` path. Bub owns the -model loop, step limits, steering, subagent delivery, automatic overflow handoff, -and conversation recording. The adapter wraps the stream to translate events and -attach client tool results; it does not run its own model-step loop or fabricate -user messages. Native lifecycle events and typed stream chunks are handled by -scoped observers on the agent's EventBus. The stream bridge publishes chunks -without changing the source stream; observers are removed when execution ends. An AG-UI run ends at an HTTP request boundary, while one native -turn may span several requests. Optional AG-UI `STEP_*` events are not emitted; -Bub's native step/run events remain available to server hooks. +Use `status: "cancelled"` without a payload to abandon a decision. Resolved payloads +must match their response schemas. Missing, partial, duplicate, or invalid +responses produce `RUN_ERROR` and can be corrected and resubmitted. Use `resume`, +not ordinary tool-result messages, for interrupts. + +## Sessions, limits, and errors + +- **Run completion:** `RUN_FINISHED` ends the HTTP run. Bub may still be waiting + for frontend results or human input; one native turn can span several requests. +- **Waiting calls:** resume on the same adapter before the turn timeout. Restart, + shutdown, or timeout discards unfinished calls, even with a persistent tape store. +- **Conversation context:** completed context follows Bub's configured tape store. + Echoed history does not replace it, and editing accepted user input is rejected. + Start a new thread for a new conversation. +- **Cancellation:** `agent.abortRun()` cancels an active HTTP execution, not tool + side effects already performed. It cannot cancel a waiting turn after that HTTP + run has finished; resolve its calls, cancel its interrupts, or let it time out. +- **Concurrency and retries:** one executing request per thread, with no + cross-process coordination. Repeated accepted interrupt responses are acknowledged + without re-execution; this is not answer replay or general request deduplication. +- **Errors:** authentication, malformed bodies, and body-size limits use HTTP errors. + Execution and continuation failures arrive as `RUN_ERROR`. + +The adapter exposes no display-history, replay, or separate cancellation endpoint. ## Embedding and development -Use `bub_ag_ui.http.create_app` to embed the endpoint. In an existing Bub gateway, -set `channel_managed=True`; an explicitly supplied `AGUIAdapter` must also use -`bind_router=False`. Set `manage_framework=False` if the host already manages -`framework.running()`. Bub's native model and tool policies still apply; -client-supplied state/context are application data, not server configuration. +Use `bub_ag_ui.http.create_app` to embed the endpoint. Set `manage_framework=False` +when the host manages `framework.running()`. In a Bub gateway, set +`channel_managed=True`; an explicitly supplied adapter must also use +`bind_router=False`. -From the repository root, with Python 3.12+ and Node.js 22+: +Run the tests from the repository root with Python 3.12+ and Node.js 22+: ```sh uv sync --frozen --group test -uv run --frozen --group test pytest packages/bub-ag-ui/tests +uv run --frozen pytest packages/bub-ag-ui/tests npm ci --prefix packages/bub-ag-ui/tests/client npm test --prefix packages/bub-ag-ui/tests/client ``` -Tests cover observable protocol output, application tools and state, authentication, -cancellation, streaming backpressure, and conversation continuity. Socket tests exercise the published AG-UI client -and assistant-ui runtime with a deterministic model provider; no model API key -is needed. Set `PYTHON` if the interpreter is not at `.venv/bin/python`. -These are regression and interoperability tests, not TCK certification. +Socket tests use the published AG-UI client and assistant-ui runtime with a +local deterministic provider; no model API key is needed. Set `PYTHON` if the +interpreter is not at `.venv/bin/python`. diff --git a/packages/bub-ag-ui/src/bub_ag_ui/continuation.py b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py index 0d37ac9..8a84018 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/continuation.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/continuation.py @@ -83,8 +83,8 @@ def prepare(self, request: RunAgentInput) -> tuple[list[dict[str, Any]], bool]: continue if response["status"] == "resolved" and call.get("response_schema"): jsonschema.validate(response["payload"], call["response_schema"]) - # Update the client view if it echoes the call. Native execution - # holds that exchange and does not require it back. + # Normalize the decision into a tool value for the waiting call. + # Native tool execution emits the result; this is not a UI snapshot. index = next( ( i + 1 diff --git a/packages/bub-ag-ui/src/bub_ag_ui/events.py b/packages/bub-ag-ui/src/bub_ag_ui/events.py index c9692f9..8fb1f15 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/events.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/events.py @@ -32,18 +32,16 @@ def result_text(value: Any) -> str: class MessageProjection: - """Current response messages for activity validation and interrupt snapshots.""" + """Current response messages for activity validation.""" - def __init__(self) -> None: - self.messages: list[dict[str, Any]] = [] + def __init__(self, messages: list[dict[str, Any]] | None = None) -> None: + self.messages = deepcopy(messages or []) def message(self, id: str) -> dict[str, Any]: return next(message for message in self.messages if message["id"] == id) def apply(self, event: dict[str, Any]) -> None: match event["type"]: - case "MESSAGES_SNAPSHOT": - self.messages = deepcopy(event["messages"]) case "TEXT_MESSAGE_START": self.messages.append( {"id": event["messageId"], "role": event["role"], "content": ""} @@ -106,9 +104,9 @@ def apply(self, event: dict[str, Any]) -> None: class EventStream: """One run's wire state; a Bub final closes a step, never a run.""" - def __init__(self, emit: Emit) -> None: + def __init__(self, emit: Emit, messages: list[dict[str, Any]]) -> None: self._emit = emit - self._messages = MessageProjection() + self._messages = MessageProjection(messages) self.message_id: str | None = None self.reasoning_id: str | None = None @@ -119,9 +117,6 @@ async def publish(self, event: dict[str, Any]) -> None: self._messages.apply(event) await self._emit(event) - async def snapshot(self) -> None: - await self.send("MESSAGES_SNAPSHOT", messages=self._messages.messages) - async def text_start(self) -> str: if self.message_id is None: self.message_id = str(uuid4()) diff --git a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py index 8338bdb..380a560 100644 --- a/packages/bub-ag-ui/src/bub_ag_ui/runtime.py +++ b/packages/bub-ag-ui/src/bub_ag_ui/runtime.py @@ -216,7 +216,13 @@ def finished(task: asyncio.Task[None]) -> None: execution.task.add_done_callback(finished) async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> None: - stream = EventStream(emit) + stream = EventStream( + emit, + [ + message.model_dump(mode="json", by_alias=True) + for message in request.messages + ], + ) session_id = self.session_id(principal, request.thread_id) acquired = False execution: NativeExecution | None = None @@ -297,7 +303,6 @@ async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> N jsonschema.SchemaError, ) as exc: raise ProtocolError(str(exc)) from exc - await stream.send("MESSAGES_SNAPSHOT", messages=messages) if repeated: await stream.send( "STATE_SNAPSHOT", @@ -319,7 +324,11 @@ async def _execute(self, principal: str, request: RunAgentInput, emit: Any) -> N ) shared = execution.inbound.shared await shared.set(request.state if request.state is not None else {}) - execution.supplied = set(values) + execution.supplied = { + m.tool_call_id + for m in request.messages + if m.role == "tool" and m.tool_call_id in values + } if request.resume: await committed(continuation.save(tape)) external.resolve(values) @@ -388,7 +397,6 @@ async def publish(event: dict[str, Any]) -> None: ] fields: dict[str, Any] = {} if interrupts: - await stream.snapshot() await execution.inbound.shared.set(execution.inbound.shared.value) fields["outcome"] = {"type": "interrupt", "interrupts": interrupts} await stream.send( diff --git a/packages/bub-ag-ui/tests/client/protocol.test.mjs b/packages/bub-ag-ui/tests/client/protocol.test.mjs index d02f5bc..dbb81b9 100644 --- a/packages/bub-ag-ui/tests/client/protocol.test.mjs +++ b/packages/bub-ag-ui/tests/client/protocol.test.mjs @@ -79,7 +79,6 @@ test("interrupt outcome and resume are understood by the protocol client", async const events = []; await a.runAgent({}, {onEvent: ({event}) => events.push(event), onRunFinishedEvent: ({event}) => {outcome = event.outcome;}}); assert.equal(events.at(-1).type, "RUN_FINISHED"); - assert.ok(events.some(e => e.type === "MESSAGES_SNAPSHOT")); assert.ok(events.some(e => e.type === "STATE_SNAPSHOT")); assert.equal(outcome.type, "interrupt"); await a.runAgent({resume: [{interruptId: outcome.interrupts[0].id, status: "resolved", payload: true}]}); @@ -92,6 +91,49 @@ test("state snapshot and RFC6902 delta reach client state", async () => { assert.deepEqual(a.state, {count: 2}); }); +for (const prompt of ["hello", "frontend", "interrupt"]) { + test(`delta-only ${prompt} requests preserve the client's existing messages`, async () => { + let deltaOnly = false; + const a = new HttpAgent({ + url: `${base}/agent`, threadId: crypto.randomUUID(), + initialMessages: [{id: crypto.randomUUID(), role: "user", content: "hello"}], + fetch: (input, init) => { + const body = JSON.parse(init.body); + if (deltaOnly) body.messages = body.messages.slice(-1); + return fetch(input, {...init, body: JSON.stringify(body)}); + }, + }); + await a.runAgent({}); + const original = a.messages.map(m => m.id); + a.addMessage({id: crypto.randomUUID(), role: "user", content: prompt}); + deltaOnly = true; + const tools = prompt === "frontend" ? [{name: "browser", description: "", parameters: {type: "object"}}] : []; + let outcome; + await a.runAgent({tools}, {onRunFinishedEvent: ({event}) => { outcome = event.outcome; }}); + assert.ok(original.every(id => a.messages.some(m => m.id === id))); + if (prompt !== "hello") { + const callMessage = a.messages.findLast(m => m.toolCalls); + const call = callMessage?.toolCalls[0]; + assert.ok(call); + const beforeResume = a.messages.map(m => m.id); + if (prompt === "frontend") { + a.addMessage({id: crypto.randomUUID(), role: "tool", toolCallId: call.id, content: "client result"}); + await a.runAgent({tools}); + } else { + assert.equal(outcome.type, "interrupt"); + await a.runAgent({resume: [{interruptId: outcome.interrupts[0].id, status: "resolved", payload: true}]}); + } + assert.ok(beforeResume.every(id => a.messages.some(m => m.id === id))); + assert.equal(a.messages.filter(m => m.role === "tool" && m.toolCallId === call.id).length, 1); + const displayedCall = fromAgUiMessages(a.messages).find(m => m.id === callMessage.id) + ?.content.find(p => p.type === "tool-call" && p.toolCallId === call.id); + assert.ok(displayedCall); + assert.equal(displayedCall.result, prompt === "frontend" ? "client result" : true); + } + assert.equal(a.messages.at(-1).content, `Answer: ${prompt}`); + }); +} + test("provider errors do not turn into success", async () => { const a = agent("error"); const events = []; diff --git a/packages/bub-ag-ui/tests/conftest.py b/packages/bub-ag-ui/tests/conftest.py index ce4a619..8fbb29d 100644 --- a/packages/bub-ag-ui/tests/conftest.py +++ b/packages/bub-ag-ui/tests/conftest.py @@ -118,7 +118,9 @@ async def collect(adapter, data, user="alice"): from bub_ag_ui.events import MessageProjection events = [event async for event in adapter.run(user, data)] - projection = MessageProjection() + projection = MessageProjection( + [message.model_dump(mode="json", by_alias=True) for message in data.messages] + ) for event in events: projection.apply(event) return ClientView(projection), events diff --git a/packages/bub-ag-ui/tests/test_runtime.py b/packages/bub-ag-ui/tests/test_runtime.py index f00cdcf..c374112 100644 --- a/packages/bub-ag-ui/tests/test_runtime.py +++ b/packages/bub-ag-ui/tests/test_runtime.py @@ -132,7 +132,7 @@ async def test_interrupt_invalid_resume_is_run_error_and_keeps_pending(setup, in assert accepted[-1]["type"] == "RUN_FINISHED" -async def test_interrupt_snapshots_resume_and_idempotency_without_history_api(setup): +async def test_interrupt_state_resume_and_idempotency(setup): provider, make = setup def update(context): @@ -151,7 +151,6 @@ async def prepare(context): provider.reply(calls=[("a1", "approve", "{}"), ("a2", "approve", "{}")]) view, events = await collect(adapter, request()) assert events[-1]["type"] == "RUN_FINISHED" - assert any(e["type"] == "MESSAGES_SNAPSHOT" for e in events) state = next( e["snapshot"] for e in reversed(events) if e["type"] == "STATE_SNAPSHOT" ) From 921a6d81de768d0da0a74ddc08b731d2e546ca8f Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Wed, 11 Nov 2026 01:03:11 +0800 Subject: [PATCH 10/10] test(ag-ui): drive deadlines without wall-clock startup budgets --- packages/bub-ag-ui/tests/conftest.py | 15 +++++++++++++++ packages/bub-ag-ui/tests/test_execution.py | 13 ++++++++----- packages/bub-ag-ui/tests/test_runtime.py | 18 +++++++++++------- .../bub-ag-ui/tests/test_stream_delivery.py | 3 ++- 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/packages/bub-ag-ui/tests/conftest.py b/packages/bub-ag-ui/tests/conftest.py index 8fbb29d..47657aa 100644 --- a/packages/bub-ag-ui/tests/conftest.py +++ b/packages/bub-ag-ui/tests/conftest.py @@ -1,5 +1,6 @@ from __future__ import annotations +import asyncio import json import os from collections import deque @@ -53,6 +54,20 @@ async def respond(self, request): return self.responses.popleft() +@pytest.fixture +async def advance_time(monkeypatch): + """Keep deadlines independent of wall time until the test advances the clock.""" + loop = asyncio.get_running_loop() + now = loop.time() + monkeypatch.setattr(loop, "time", lambda: now) + + def advance(seconds): + nonlocal now + now += seconds + + return advance + + @pytest.fixture async def setup(monkeypatch, tmp_path): for key in list(os.environ): diff --git a/packages/bub-ag-ui/tests/test_execution.py b/packages/bub-ag-ui/tests/test_execution.py index 6051424..32a7040 100644 --- a/packages/bub-ag-ui/tests/test_execution.py +++ b/packages/bub-ag-ui/tests/test_execution.py @@ -438,19 +438,22 @@ async def delegate(context): ] -async def test_output_processing_obeys_run_timeout(setup): - import asyncio - +async def test_output_processing_obeys_run_timeout(setup, advance_time): provider, make = setup - adapter = make(max_run_seconds=0.05) + adapter = make(max_run_seconds=600) + save_started = asyncio.Event() class Output: @hookimpl async def save_state(self): + save_started.set() await asyncio.Event().wait() adapter.framework.plugin_manager.register(Output(), "blocked-save") provider.reply("finished model") - _, events = await asyncio.wait_for(collect(adapter, request()), 1) + pending = asyncio.create_task(collect(adapter, request())) + await save_started.wait() + advance_time(601) + _, events = await pending assert events[-1]["type"] == "RUN_ERROR" assert "timed out" in events[-1]["message"] diff --git a/packages/bub-ag-ui/tests/test_runtime.py b/packages/bub-ag-ui/tests/test_runtime.py index c374112..c29ad14 100644 --- a/packages/bub-ag-ui/tests/test_runtime.py +++ b/packages/bub-ag-ui/tests/test_runtime.py @@ -316,19 +316,23 @@ async def wait(): await owner -async def test_timeout_closes_native_execution_with_one_error(setup): +async def test_timeout_closes_native_execution_with_one_error(setup, advance_time): provider, make = setup - closed = asyncio.Event() + entered, closed = asyncio.Event(), asyncio.Event() async def wait(): try: + entered.set() await asyncio.Event().wait() finally: closed.set() - adapter = make([Tool.from_callable(wait)], max_run_seconds=0.05) + adapter = make([Tool.from_callable(wait)], max_run_seconds=600) provider.reply(calls=[("w", "wait", "{}")]) - _, events = await collect(adapter, request()) + pending = asyncio.create_task(collect(adapter, request())) + await entered.wait() + advance_time(601) + _, events = await pending assert closed.is_set() assert events[-1]["type"] == "RUN_ERROR" assert events[-1]["message"] == "Agent run timed out" @@ -386,14 +390,14 @@ async def test_tool_results_after_restart_are_rejected(setup): assert len(provider.requests) == 1 -async def test_tool_results_after_timeout_are_rejected(setup): +async def test_tool_results_after_timeout_are_rejected(setup, advance_time): provider, make = setup - adapter = make(max_run_seconds=0.05) + adapter = make(max_run_seconds=600) tools = [{"name": "browser", "description": "", "parameters": {}}] provider.reply(calls=[("f1", "browser", "{}")]) _, paused = await collect(adapter, request(tools=tools)) assert paused[-1]["type"] == "RUN_FINISHED" - await asyncio.sleep(0.1) + advance_time(601) _, expired = await collect( adapter, request( diff --git a/packages/bub-ag-ui/tests/test_stream_delivery.py b/packages/bub-ag-ui/tests/test_stream_delivery.py index bd7d56e..b9a4d48 100644 --- a/packages/bub-ag-ui/tests/test_stream_delivery.py +++ b/packages/bub-ag-ui/tests/test_stream_delivery.py @@ -34,6 +34,7 @@ async def source(): async for event in events: if event["type"] == "TEXT_MESSAGE_CONTENT": break - await asyncio.sleep(0.05) + # Let the producer run without resuming the consumer. + await asyncio.sleep(0) assert produced < 10000 await asyncio.wait_for(closed.wait(), 5)