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

+27 -33
View File
@@ -5,7 +5,7 @@ import pytest
from nanobot.agent.goal_permission import goal_mutation_allowed
from nanobot.agent.loop import AgentLoop
from nanobot.bus.events import InboundMessage
from nanobot.bus.events import InboundMessage, OutboundMessage
from nanobot.bus.queue import MessageBus
from nanobot.command.builtin import (
build_help_text,
@@ -16,6 +16,7 @@ from nanobot.command.builtin import (
)
from nanobot.command.router import CommandContext, CommandRouter
from nanobot.config.schema import ModelPresetConfig
from nanobot.session.history_visibility import HIDDEN_HISTORY_META
from nanobot.session.model_selection import (
SESSION_MODEL_PRESET_METADATA_KEY,
model_preset_from_metadata,
@@ -64,7 +65,8 @@ def _ctx(loop: AgentLoop, raw: str, args: str = "") -> CommandContext:
def _ctx_session(loop: AgentLoop, raw: str, args: str = "") -> CommandContext:
msg = InboundMessage(channel="cli", sender_id="user", chat_id="direct", content=raw)
return CommandContext(
msg=msg, session=MagicMock(), key=msg.session_key, raw=raw, args=args, loop=loop,
msg=msg, session=loop.sessions.get_or_create(msg.session_key),
key=msg.session_key, raw=raw, args=args, loop=loop,
is_user_turn=True,
)
@@ -256,34 +258,27 @@ async def test_goal_command_shows_usage_without_args(tmp_path) -> None:
@pytest.mark.asyncio
async def test_goal_command_rejects_mid_turn_without_session(tmp_path) -> None:
@pytest.mark.parametrize("with_session", [False, True])
async def test_goal_command_preserves_task_and_visible_command(tmp_path, with_session) -> None:
loop = _make_loop(tmp_path)
out = await cmd_goal(_ctx(loop, "/goal do work", args="do work"))
assert out is not None
assert out.channel == "cli"
assert out.chat_id == "direct"
assert out.metadata == {"render_as": "text"}
assert out.content == (
"A task is already running for this chat. "
"Use `/stop` first, then send `/goal <long-running task description>` again."
)
@pytest.mark.asyncio
async def test_goal_command_marks_turn_and_preserves_explicit_request(tmp_path) -> None:
loop = _make_loop(tmp_path)
ctx = _ctx_session(loop, "/goal audit the repo", args="audit the repo")
task = "检查 /tmp/project\n保留现有文件。"
command = f"/goal {task}"
ctx = _ctx_session(loop, command, args=task)
if not with_session:
ctx.session = None
out = await cmd_goal(ctx)
assert out is None
assert ctx.msg.content == "/goal audit the repo"
assert ctx.msg.metadata.get("original_command") == "/goal"
assert ctx.msg.metadata.get("original_content") == "/goal audit the repo"
assert ctx.msg.metadata.get("goal_requested") is True
assert isinstance(ctx.msg.metadata.get("goal_started_at"), int | float)
assert len(ctx.turn_scopes) == 1
with ctx.turn_scopes[0]:
assert goal_mutation_allowed() is True
assert isinstance(out, InboundMessage)
assert ctx.msg.content == command
assert out.content == task
assert out.metadata["original_content"] == command
assert out.metadata["goal_requested"] is True
assert out.metadata[HIDDEN_HISTORY_META] == {"kind": "goal_request"}
assert goal_mutation_allowed() is False
session = loop.sessions.get_or_create(ctx.key)
assert [(row["role"], row["content"]) for row in session.messages] == [
("user", command),
]
@pytest.mark.asyncio
@@ -293,14 +288,13 @@ async def test_goal_command_registered_on_router(tmp_path) -> None:
loop = _make_loop(tmp_path)
ctx = _ctx_session(loop, "/goal ship it", args="ship it")
out = await router.dispatch(ctx)
assert out is None
assert "ship it" in ctx.msg.content
assert len(ctx.turn_scopes) == 1
with ctx.turn_scopes[0]:
assert goal_mutation_allowed() is True
assert isinstance(out, InboundMessage)
assert out.content == "ship it"
assert out.metadata["goal_requested"] is True
assert goal_mutation_allowed() is False
@pytest.mark.asyncio
async def test_goal_command_does_not_allow_internal_turn(tmp_path) -> None:
loop = _make_loop(tmp_path)
@@ -321,7 +315,7 @@ async def test_goal_command_does_not_allow_internal_turn(tmp_path) -> None:
out = await cmd_goal(ctx)
assert out is not None
assert isinstance(out, OutboundMessage)
assert "only be started by a user" in out.content
assert ctx.turn_scopes == []