fix: preserve subtask request attribution after restart (#6247)
* fix: preserve request origin across recovery continuations * fix: bind request origin to each continuation * fix: capture origin when queuing continuation messages * chore: remove redundant request context comment * fix: persist origin for independent system turns
This commit is contained in:
1 parent
6d84786efa
commit
33ff520d12
7 files changed
+266
-5
No files matched your search
+16
-2
@@ -92,10 +92,13 @@ from nanobot.session.model_selection import (
|
||||
from nanobot.session.recovery import (
|
||||
PENDING_FOLLOWUP_ID_KEY,
|
||||
RECOVERY_INBOUND_METADATA_KEY,
|
||||
REQUEST_ORIGIN_METADATA_KEY,
|
||||
RecoveryAdmission,
|
||||
RequestOrigin,
|
||||
acknowledge_pending_followups,
|
||||
pending_followups,
|
||||
record_pending_followup,
|
||||
request_origin_from_metadata,
|
||||
restore_pending_interruption,
|
||||
restore_runtime_checkpoint,
|
||||
)
|
||||
@@ -730,6 +733,17 @@ class AgentLoop:
|
||||
|
||||
async def _request_context_for_turn(self, ctx: TurnContext) -> RequestContext:
|
||||
assert ctx.session is not None
|
||||
origin: RequestOrigin = {
|
||||
"message_id": ctx.msg.metadata.get("message_id"),
|
||||
"turn_id": ctx.delivery.route.turn_id or ctx.turn_id,
|
||||
}
|
||||
if turn_continuation.internal_continuation_inbound(ctx.msg.metadata):
|
||||
origin = request_origin_from_metadata(ctx.msg.metadata) or origin
|
||||
if origin != request_origin_from_metadata(ctx.session.metadata):
|
||||
ctx.session.metadata[REQUEST_ORIGIN_METADATA_KEY] = origin
|
||||
if ctx.original_user_text is None:
|
||||
# No new user row will persist this turn's owner before execution.
|
||||
self.sessions.save(ctx.session)
|
||||
scope = await self.runner.prepare_prompt(
|
||||
self.workspace_scopes.for_turn,
|
||||
channel=ctx.delivery.route.channel,
|
||||
@@ -739,14 +753,14 @@ class AgentLoop:
|
||||
return RequestContext(
|
||||
channel=ctx.delivery.route.channel,
|
||||
chat_id=ctx.delivery.route.chat_id,
|
||||
message_id=ctx.msg.metadata.get("message_id"),
|
||||
message_id=origin["message_id"],
|
||||
session_key=ctx.session_key,
|
||||
original_user_text=ctx.original_user_text,
|
||||
runtime=ctx.runtime,
|
||||
metadata=dict(ctx.msg.metadata or {}),
|
||||
attributes=dict(ctx.attributes),
|
||||
sender_id=ctx.msg.sender_id,
|
||||
turn_id=ctx.delivery.route.turn_id or ctx.turn_id,
|
||||
turn_id=origin["turn_id"],
|
||||
workspace=scope.project_path,
|
||||
workspace_scope=scope,
|
||||
log_content=ctx.session.policy.log_content and not ctx.ephemeral,
|
||||
|
||||
@@ -6,7 +6,7 @@ import asyncio
|
||||
import json
|
||||
from collections.abc import Iterable, Mapping
|
||||
from datetime import datetime
|
||||
from typing import Any, Protocol, cast
|
||||
from typing import Any, Protocol, TypedDict, cast
|
||||
from uuid import uuid4
|
||||
|
||||
from loguru import logger
|
||||
@@ -16,6 +16,7 @@ from nanobot.session.manager import Session
|
||||
|
||||
RUNTIME_CHECKPOINT_KEY = "runtime_checkpoint"
|
||||
PENDING_USER_TURN_KEY = "pending_user_turn"
|
||||
REQUEST_ORIGIN_METADATA_KEY = "request_origin"
|
||||
RECOVERY_METADATA_KEY = "webui_recovery"
|
||||
RECOVERY_INBOUND_METADATA_KEY = "_webui_recovery_id"
|
||||
PENDING_FOLLOWUPS_KEY = "pending_user_followups"
|
||||
@@ -30,6 +31,26 @@ KNOWN_CHECKPOINT_PHASES = frozenset(
|
||||
)
|
||||
|
||||
|
||||
class RequestOrigin(TypedDict):
|
||||
message_id: str | None
|
||||
turn_id: str
|
||||
|
||||
|
||||
def request_origin_from_metadata(metadata: Mapping[str, Any]) -> RequestOrigin | None:
|
||||
"""Read the request identity carried by continuation or recovery metadata."""
|
||||
value = metadata.get(REQUEST_ORIGIN_METADATA_KEY)
|
||||
if not isinstance(value, dict):
|
||||
return None
|
||||
origin = cast(dict[str, object], value)
|
||||
message_id = origin.get("message_id")
|
||||
turn_id = origin.get("turn_id")
|
||||
if message_id is not None and not isinstance(message_id, str):
|
||||
return None
|
||||
if not isinstance(turn_id, str) or not turn_id:
|
||||
return None
|
||||
return {"message_id": message_id, "turn_id": turn_id}
|
||||
|
||||
|
||||
class RecoveryAdmission(Protocol):
|
||||
"""Narrow AgentLoop boundary for explicit recovery validation."""
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ from nanobot.session.goal_state import (
|
||||
sustained_goal_active,
|
||||
sustained_goal_turn,
|
||||
)
|
||||
from nanobot.session.recovery import REQUEST_ORIGIN_METADATA_KEY
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from nanobot.agent.loop import TurnContext
|
||||
@@ -129,6 +130,12 @@ async def maybe_continue_turn(ctx: TurnContext) -> bool:
|
||||
ctx.msg.metadata,
|
||||
run_started_at=ctx.visible_run_started_at,
|
||||
)
|
||||
request = ctx.request_context
|
||||
assert request is not None and request.turn_id is not None
|
||||
metadata[REQUEST_ORIGIN_METADATA_KEY] = {
|
||||
"message_id": request.message_id,
|
||||
"turn_id": request.turn_id,
|
||||
}
|
||||
content = _goal_continuation_prompt(ctx.session.metadata)
|
||||
messages = _strip_terminal_assistant(ctx.all_messages, ctx.final_content)
|
||||
_increment_goal_continuation_round(ctx.session.metadata)
|
||||
|
||||
@@ -27,12 +27,14 @@ from nanobot.session.recovery import (
|
||||
PENDING_USER_TURN_KEY,
|
||||
RECOVERY_INBOUND_METADATA_KEY,
|
||||
RECOVERY_METADATA_KEY,
|
||||
REQUEST_ORIGIN_METADATA_KEY,
|
||||
RUNTIME_CHECKPOINT_KEY,
|
||||
UNCERTAIN_TOOL_PHASES,
|
||||
append_recovery_interruption,
|
||||
discard_runtime_checkpoint,
|
||||
pending_followups,
|
||||
recovery_state_from_metadata,
|
||||
request_origin_from_metadata,
|
||||
restore_pending_interruption,
|
||||
restore_runtime_checkpoint,
|
||||
runtime_checkpoint_is_well_formed,
|
||||
@@ -389,6 +391,7 @@ class RecoveryCoordinator:
|
||||
state: Mapping[str, Any],
|
||||
) -> None:
|
||||
recovery_id = cast(str, state["recovery_id"])
|
||||
origin = request_origin_from_metadata(session.metadata)
|
||||
await self.bus.publish_inbound(
|
||||
InboundMessage(
|
||||
channel="websocket",
|
||||
@@ -405,6 +408,7 @@ class RecoveryCoordinator:
|
||||
RECOVERY_INBOUND_METADATA_KEY: recovery_id,
|
||||
turn_continuation.INTERNAL_CONTINUATION_META: True,
|
||||
turn_continuation.SKIP_USER_PERSIST_META: True,
|
||||
**({REQUEST_ORIGIN_METADATA_KEY: origin} if origin is not None else {}),
|
||||
},
|
||||
session_key_override=session.key,
|
||||
require_existing_session=True,
|
||||
|
||||
Reference in new issue
Block a user