fix(tui): accept goal requests during active turns (#5981)

* fix(tui): accept goal requests during active turns

* fix(goal): reuse live input routing and remove busy guards

* test(goal): cover followup streams across iteration budgets

* refactor(goal): expand slash commands into hidden agent input

* fix(goal): keep generated input permission scoped to users

* refactor(agent): encapsulate goal input permission scope

* refactor(goal): preserve task text without prompt wrapper

* test(goal): consolidate coverage and exercise restart recovery

* test(goal): wait for recovery reply without task registry race
This commit is contained in:
chengyongru authored and GitHub committed 2026-09-30 13:08:13 +08:00
1 parent b46d4dfd7d
commit 99ba062a97
15 files changed
+613 -360

No files matched your search

+19 -25
View File
@@ -14,9 +14,10 @@ from typing import TYPE_CHECKING, Any, Literal, cast
from loguru import logger
from nanobot import __version__
from nanobot.bus.events import INBOUND_META_USER_SHELL, OutboundMessage
from nanobot.bus.events import INBOUND_META_USER_SHELL, InboundMessage, OutboundMessage
from nanobot.command.router import CommandContext, CommandRouter, normalize_command_text
from nanobot.providers.base import LLMUsage
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
from nanobot.utils.helpers import build_status_content
from nanobot.utils.restart import set_restart_notice_to_env
from nanobot.utils.workspace_prompts import initialize_workspace_prompt
@@ -901,10 +902,8 @@ async def cmd_history(ctx: CommandContext) -> OutboundMessage:
)
async def cmd_goal(ctx: CommandContext) -> OutboundMessage | None:
"""Mark this turn as an explicit sustained-goal request."""
from nanobot.agent.goal_permission import goal_mutation_permission
async def cmd_goal(ctx: CommandContext) -> InboundMessage | OutboundMessage:
"""Expand an explicit goal command into model-only input."""
goal = ctx.args.strip()
if not goal:
return OutboundMessage(
@@ -913,16 +912,6 @@ async def cmd_goal(ctx: CommandContext) -> OutboundMessage | None:
content="Usage: /goal <long-running task description>",
metadata={**dict(ctx.msg.metadata or {}), "render_as": "text"},
)
if ctx.session is None:
return OutboundMessage(
channel=ctx.msg.channel,
chat_id=ctx.msg.chat_id,
content=(
"A task is already running for this chat. "
"Use `/stop` first, then send `/goal <long-running task description>` again."
),
metadata={**dict(ctx.msg.metadata or {}), "render_as": "text"},
)
if not ctx.is_user_turn:
return OutboundMessage(
channel=ctx.msg.channel,
@@ -931,16 +920,21 @@ async def cmd_goal(ctx: CommandContext) -> OutboundMessage | None:
metadata={**dict(ctx.msg.metadata or {}), "render_as": "text"},
)
ctx.turn_scopes.append(goal_mutation_permission(True))
ctx.msg.metadata = {
**dict(ctx.msg.metadata or {}),
"original_command": "/goal",
"original_content": ctx.raw,
"goal_requested": True,
"goal_started_at": time.time(),
}
ctx.msg.content = ctx.raw
return None
session = ctx.session or ctx.loop.sessions.get_or_create(ctx.key)
session.add_message("user", ctx.msg.content, _command=True, media=list(ctx.msg.media))
ctx.loop.sessions.save(session)
return replace(
ctx.msg,
content=goal,
metadata={
**ctx.msg.metadata,
"original_command": "/goal",
"original_content": ctx.msg.content,
"goal_requested": True,
"goal_started_at": time.time(),
HIDDEN_HISTORY_META: {"kind": "goal_request"},
},
)
async def cmd_pairing(ctx: CommandContext) -> OutboundMessage:
+11 -5
View File
@@ -8,18 +8,22 @@ from dataclasses import dataclass, field
from difflib import get_close_matches
from typing import TYPE_CHECKING, Any, Awaitable, Callable
from nanobot.bus.events import OutboundMessage
from nanobot.bus.events import InboundMessage, OutboundMessage
if TYPE_CHECKING:
from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage
from nanobot.session.manager import Session
from nanobot.utils.llm_runtime import LLMRuntime
Handler = Callable[["CommandContext"], Awaitable["OutboundMessage | None"]]
Handler = Callable[["CommandContext"], Awaitable["InboundMessage | OutboundMessage | None"]]
_BOT_SUFFIX_RE = re.compile(r"^[A-Za-z0-9_]+$")
def command_text(message: InboundMessage) -> str:
"""Return routable text; command-generated agent input must not be dispatched again."""
return "" if message.metadata.get("original_command") else message.content.strip()
def normalize_command_text(text: str) -> str:
"""Normalize slash-command transport variants before routing.
@@ -99,7 +103,7 @@ class CommandRouter:
return True
return cmd.startswith("/")
async def dispatch_priority(self, ctx: CommandContext) -> OutboundMessage | None:
async def dispatch_priority(self, ctx: CommandContext) -> InboundMessage | OutboundMessage | None:
"""Dispatch a priority command. Called from run() without the lock."""
ctx.raw = normalize_command_text(ctx.raw)
handler = self._priority.get(ctx.raw.lower())
@@ -107,8 +111,10 @@ class CommandRouter:
return await handler(ctx)
return None
async def dispatch(self, ctx: CommandContext) -> OutboundMessage | None:
async def dispatch(self, ctx: CommandContext) -> InboundMessage | OutboundMessage | None:
"""Try exact and prefix handlers, then reject invalid slash commands."""
if ctx.msg.metadata.get("original_command"):
return None
ctx.raw = normalize_command_text(ctx.raw)
cmd = ctx.raw.lower()