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
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user