feat(subagent): add session-owned task messaging and cancellation (#5985)
let's go!
This commit is contained in:
1 parent
f75470e72f
commit
6ff96f2a8f
93 files changed
+5632
-1156
No files matched your search
@@ -60,6 +60,14 @@ from nanobot.utils.runtime import (
|
||||
is_blank_text,
|
||||
)
|
||||
|
||||
|
||||
def _raise_if_cancelling() -> None:
|
||||
"""Do not resume model work when a dependency suppresses cancellation."""
|
||||
task = asyncio.current_task()
|
||||
if task is not None and task.cancelling():
|
||||
raise asyncio.CancelledError
|
||||
|
||||
|
||||
ContinuationCallback = Callable[[], str | None]
|
||||
CheckpointCallback = Callable[[dict[str, Any]], Awaitable[None]]
|
||||
InjectionCallback = Callable[[], Awaitable[Iterable[Any] | None]]
|
||||
@@ -863,6 +871,7 @@ class AgentRunner:
|
||||
malformed_retry: bool = False,
|
||||
transcript: list[dict[str, Any]] | None,
|
||||
) -> tuple[LLMResponse, LLMUsage]:
|
||||
_raise_if_cancelling()
|
||||
tool_definitions = spec.tools.get_definitions()
|
||||
messages, provider_context = await self.context_governor.prepare_request(
|
||||
request_state,
|
||||
@@ -876,6 +885,7 @@ class AgentRunner:
|
||||
messages,
|
||||
tools=tool_definitions,
|
||||
)
|
||||
_raise_if_cancelling()
|
||||
wants_streaming = hook.wants_streaming()
|
||||
provider_context = replace(
|
||||
provider_context or ProviderCallContext(),
|
||||
@@ -993,6 +1003,7 @@ class AgentRunner:
|
||||
request_started_at = time.perf_counter()
|
||||
try:
|
||||
response = await coro
|
||||
_raise_if_cancelling()
|
||||
except asyncio.CancelledError:
|
||||
_pause_generation()
|
||||
await _close_native_reasoning()
|
||||
@@ -1214,6 +1225,7 @@ class AgentRunner:
|
||||
request_state: ModelRequestState,
|
||||
transcript: list[dict[str, Any]] | None = None,
|
||||
) -> LLMResponse:
|
||||
_raise_if_cancelling()
|
||||
messages, provider_context = await self.context_governor.prepare_request(
|
||||
request_state,
|
||||
messages,
|
||||
@@ -1225,6 +1237,7 @@ class AgentRunner:
|
||||
messages,
|
||||
tools=None,
|
||||
)
|
||||
_raise_if_cancelling()
|
||||
response = await spec.runtime.provider.chat_stream_with_retry(
|
||||
**kwargs,
|
||||
provider_context=replace(
|
||||
@@ -1232,11 +1245,13 @@ class AgentRunner:
|
||||
response_preset=spec.runtime.model_preset or "",
|
||||
),
|
||||
)
|
||||
_raise_if_cancelling()
|
||||
await self.context_governor.summarize_provider_compaction(
|
||||
request_state,
|
||||
response,
|
||||
current_request_boundary=(len(transcript) if transcript is not None else None),
|
||||
)
|
||||
_raise_if_cancelling()
|
||||
request_state.provider_compaction_applied |= response.provider_compaction_applied
|
||||
return response
|
||||
|
||||
|
||||
Reference in new issue
Block a user