fix(agent): preserve goal continuation lifecycle

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-24 19:25:34 +08:00
1 parent 3c022ada6a
commit ce885fa31e
5 files changed
+75 -22

No files matched your search

+6 -8
View File
@@ -1703,7 +1703,7 @@ class AgentLoop:
pending_queue=pending,
delivery=delivery,
)
continuing = turn_continuation.internal_continuation_pending(msg.metadata)
continuing = delivery.continuation_pending
await delivery.complete(
response,
publish_completion=not continuing,
@@ -1713,6 +1713,8 @@ class AgentLoop:
coordinator.complete(msg, response=response)
except asyncio.CancelledError:
logger.info("Task cancelled for session {}", session_key)
# The queued continuation is cancelled with this session worker.
delivery.continuation_pending = False
try:
await delivery.abort_stream()
except Exception:
@@ -1745,15 +1747,11 @@ class AgentLoop:
logger.opt(exception=log_content).error(
"Error processing message for session {}", session_key,
)
await delivery.fail(
publish_completion=not turn_continuation.internal_continuation_pending(
msg.metadata
)
)
await delivery.fail(publish_completion=not delivery.continuation_pending)
for coordinator in self._automation_turn_coordinators:
coordinator.complete(msg, error=exc)
finally:
if not turn_continuation.internal_continuation_pending(msg.metadata):
if not delivery.continuation_pending:
await delivery.idle()
except asyncio.CancelledError:
if not completion_published and self.commands.requires_session_queue(command_text(msg)):
@@ -2353,7 +2351,7 @@ class AgentLoop:
ctx.delivery.record_usage(result.round_usages)
if ctx.kind is TurnKind.USER:
current = await self.sessions.state.read_metadata(ctx.session_key)
await turn_continuation.maybe_continue_turn(
ctx.delivery.continuation_pending = await turn_continuation.maybe_continue_turn(
ctx, session_metadata=current["metadata"] if current else {},
)
+1
View File
@@ -207,6 +207,7 @@ class TurnDelivery:
route: TurnRoute
route_admission: TurnRouteAdmission | None = None
enable_stream: bool = False
continuation_pending: bool = field(init=False, default=False)
delivery_message: InboundMessage = field(init=False)
lifecycle_message: InboundMessage = field(init=False)
_stream_base_id: str | None = field(init=False, default=None)
-8
View File
@@ -24,7 +24,6 @@ if TYPE_CHECKING:
INTERNAL_CONTINUATION_META = "_internal_continuation"
INTERNAL_CONTINUATION_KIND_META = "_internal_continuation_kind"
INTERNAL_CONTINUATION_PENDING_META = "_internal_continuation_pending"
INTERNAL_CONTINUATION_RUN_STARTED_AT_META = "_internal_continuation_run_started_at"
SKIP_USER_PERSIST_META = "_skip_user_persist"
@@ -33,7 +32,6 @@ _GOAL_CONTINUATION_SENDER = "system:continuation"
_GOAL_CONTINUATION_ROUNDS_KEY = "_sustained_goal_continuation_rounds"
_MAX_GOAL_CONTINUATION_ROUNDS = 12
_STRIPPED_INBOUND_META_KEYS = {
INTERNAL_CONTINUATION_PENDING_META,
"goal_requested",
"original_command",
}
@@ -53,11 +51,6 @@ def sustained_goal_continuation_inbound(metadata: Mapping[str, Any] | None) -> b
)
def internal_continuation_pending(metadata: Mapping[str, Any] | None) -> bool:
"""True when the current turn scheduled an invisible continuation slice."""
return bool(metadata and metadata.get(INTERNAL_CONTINUATION_PENDING_META) is True)
def internal_continuation_run_started_at(metadata: Mapping[str, Any] | None) -> float | None:
"""Return the user-visible run start propagated across continuation slices."""
if not metadata:
@@ -143,7 +136,6 @@ async def maybe_continue_turn(
_increment_goal_continuation_round(ctx.session.metadata)
logger.info("Turn budget reached; scheduling internal continuation")
ctx.msg.metadata[INTERNAL_CONTINUATION_PENDING_META] = True
ctx.final_content = ""
ctx.all_messages = messages
ctx.suppress_response = True