fix(cli): explain session storage conflicts before startup
This commit is contained in:
1 parent
6f49eee2ac
commit
839992630c
5 files changed
+193
-1
No files matched your search
@@ -32,6 +32,49 @@ If `nanobot agent -m "Hello!"` fails, fix that before debugging WebUI, Telegram,
|
||||
`nanobot status` does not call the model. If provider/model setup is incomplete, it points to
|
||||
WebUI **Settings → Models** or the CLI setup wizard, then prints the command to check again.
|
||||
|
||||
## Session storage overlaps the workspace
|
||||
|
||||
If startup reports `session storage must be outside the agent workspace`, compare the
|
||||
printed Config, Workspace, and Sessions paths. Sessions live at
|
||||
`<config-dir>/sessions/<workspace-id>/`; that storage root must not equal the workspace
|
||||
or be inside it. This keeps internal conversation files out of the workspace exposed
|
||||
to agent tools. Moving files outside the workspace is not a substitute for restricting
|
||||
tool access.
|
||||
|
||||
`agents.defaults.workspace` sets the default workspace. `--workspace` takes precedence
|
||||
for the selected command but does not change the session storage root. For `agent` and
|
||||
`gateway` this override is temporary; `webui --workspace` saves the workspace setting
|
||||
as part of WebUI setup.
|
||||
|
||||
For an existing instance, stop its processes and back up both the workspace and config
|
||||
directory. Keep the workspace at its current path, including `.nanobot/workspace-id`,
|
||||
memory, and skills. Move the config file and its runtime data (including the entire
|
||||
`sessions` directory) to a separate directory outside that workspace. Update `--config`
|
||||
in your launch command or service. Check any relative paths in the config before restarting.
|
||||
Moving only `config.json` leaves the existing session history behind.
|
||||
|
||||
For example, these paths keep runtime data separate from an existing workspace:
|
||||
|
||||
```text
|
||||
bots/
|
||||
bot-a-data/
|
||||
config.json
|
||||
sessions/
|
||||
bot-a-workspace/
|
||||
.nanobot/workspace-id
|
||||
memory/
|
||||
skills/
|
||||
```
|
||||
|
||||
```bash
|
||||
nanobot gateway --config /path/to/bots/bot-a-data/config.json --workspace /path/to/bots/bot-a-workspace
|
||||
```
|
||||
|
||||
For a new instance, `bot/config.json`, `bot/sessions/`, and `bot/workspace/` also work.
|
||||
Changing an existing instance to a new nested workspace requires moving its workspace
|
||||
contents, including memory, skills, and `.nanobot/workspace-id`; merely changing the
|
||||
setting can start an empty workspace with a different session identity.
|
||||
|
||||
## How to Read `nanobot status`
|
||||
|
||||
`nanobot status` does not call a model. It checks the selected config and workspace,
|
||||
|
||||
@@ -347,6 +347,7 @@ def serve(
|
||||
config: str | None = typer.Option(None, "--config", "-c", help="Path to config file"),
|
||||
):
|
||||
"""Start the OpenAI-compatible API server (/v1/chat/completions)."""
|
||||
runtime_config = _load_runtime_config(config, workspace)
|
||||
try:
|
||||
from aiohttp import web # noqa: F401
|
||||
except ImportError:
|
||||
@@ -360,7 +361,6 @@ def serve(
|
||||
|
||||
_set_nanobot_logs(verbose)
|
||||
|
||||
runtime_config = _load_runtime_config(config, workspace)
|
||||
api_cfg = runtime_config.api
|
||||
host = host if host is not None else api_cfg.host
|
||||
port = port if port is not None else api_cfg.port
|
||||
|
||||
@@ -21,6 +21,7 @@ __all__ = [
|
||||
"_print_model_setup_steps",
|
||||
"_print_runtime_config_validation_error",
|
||||
"_provider_setup_error",
|
||||
"_validate_session_storage",
|
||||
]
|
||||
|
||||
console = Console()
|
||||
@@ -130,9 +131,44 @@ def _load_runtime_config(config: str | None = None, workspace: str | None = None
|
||||
loaded = _load_config_for_cli(config_path, resolve_env=True)
|
||||
if workspace:
|
||||
loaded.agents.defaults.workspace = workspace
|
||||
_validate_session_storage(loaded, workspace_override=workspace)
|
||||
return loaded
|
||||
|
||||
|
||||
def _validate_session_storage(config: Config, *, workspace_override: str | None = None) -> None:
|
||||
"""Reject overlapping session storage before startup creates workspace files."""
|
||||
from nanobot.config.loader import get_config_path
|
||||
|
||||
config_path = get_config_path().expanduser().resolve(strict=False)
|
||||
data_dir = config.runtime_data_dir or config_path.parent
|
||||
sessions = (data_dir / "sessions").resolve(strict=False)
|
||||
workspace = config.workspace_path.resolve(strict=False)
|
||||
if not sessions.is_relative_to(workspace):
|
||||
return
|
||||
|
||||
source = "--workspace" if workspace_override else "agents.defaults.workspace"
|
||||
console.print(Text("Cannot start: session storage must be outside the agent workspace.", style="red"))
|
||||
console.print(Text(f"Config: {config_path}"))
|
||||
console.print(Text(f"Workspace ({source}): {workspace}"))
|
||||
console.print(Text(f"Sessions: {sessions}"))
|
||||
console.print(
|
||||
"Sessions are stored beside the active config file. --workspace overrides the "
|
||||
"configured workspace; it does not move sessions."
|
||||
)
|
||||
console.print(
|
||||
"Stop this instance and back up its config directory and workspace. Move the "
|
||||
"config file and its runtime data, including sessions, to a directory outside the "
|
||||
"workspace, then restart with --config pointing to the moved config file."
|
||||
)
|
||||
console.print("Example layout: bot/config.json, bot/sessions/, bot/workspace/.")
|
||||
console.print(
|
||||
"Keep the existing workspace path and its .nanobot/workspace-id to preserve "
|
||||
"its session identity. See docs/troubleshooting.md#session-storage-overlaps-the-workspace "
|
||||
"for layout and migration options."
|
||||
)
|
||||
raise typer.Exit(1)
|
||||
|
||||
|
||||
def _load_inspection_config(
|
||||
config: str | None = None,
|
||||
workspace: str | None = None,
|
||||
|
||||
@@ -12,6 +12,7 @@ from nanobot.cli.runtime_config import (
|
||||
_print_config_error,
|
||||
_print_runtime_config_validation_error,
|
||||
_provider_setup_error,
|
||||
_validate_session_storage,
|
||||
)
|
||||
from nanobot.cli.webui_support import (
|
||||
_attach_to_background_gateway,
|
||||
@@ -142,6 +143,7 @@ def webui(
|
||||
_print_config_error(exc)
|
||||
raise typer.Exit(1) from exc
|
||||
|
||||
_validate_session_storage(resolved_setup_config, workspace_override=workspace)
|
||||
provider_error = _provider_setup_error(resolved_setup_config)
|
||||
if provider_error:
|
||||
console.print(f"[yellow]Model setup is incomplete: {provider_error}[/yellow]")
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from typer.testing import CliRunner
|
||||
|
||||
from nanobot.cli.commands import app
|
||||
from nanobot.cli.runtime_config import _load_runtime_config
|
||||
|
||||
runner = CliRunner()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def isolate_config_path(monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
monkeypatch.setattr("nanobot.config.loader._current_config_path", None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"args",
|
||||
[
|
||||
["agent", "--message", "hello"],
|
||||
["gateway", "--foreground"],
|
||||
["gateway", "--background"],
|
||||
["serve"],
|
||||
["webui", "--yes", "--no-open"],
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("override", [False, True])
|
||||
def test_startup_rejects_nested_sessions_before_writing_files(
|
||||
tmp_path: Path, args: list[str], override: bool
|
||||
) -> None:
|
||||
workspace = tmp_path / "work [personal]"
|
||||
config_path = workspace / "_nanobot" / "config.json"
|
||||
config_path.parent.mkdir(parents=True)
|
||||
configured_workspace = tmp_path / "safe-workspace" if override else workspace
|
||||
config_path.write_text(
|
||||
json.dumps({"agents": {"defaults": {"workspace": str(configured_workspace)}}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
before = config_path.read_bytes()
|
||||
command = [*args, "--config", str(config_path)]
|
||||
if override:
|
||||
command += ["--workspace", str(workspace)]
|
||||
|
||||
result = runner.invoke(app, command)
|
||||
output = "".join(result.stdout.splitlines())
|
||||
|
||||
assert result.exit_code == 1, result.output
|
||||
assert "session storage must be outside" in output
|
||||
assert str(config_path) in output
|
||||
assert str(workspace) in output
|
||||
assert str(config_path.parent / "sessions") in output
|
||||
source = "--workspace" if override else "agents.defaults.workspace"
|
||||
assert f"Workspace ({source})" in output
|
||||
assert "back up" in output
|
||||
assert "--config pointing to the moved config file" in output
|
||||
assert "Traceback" not in output
|
||||
assert config_path.read_bytes() == before
|
||||
assert set(workspace.rglob("*")) == {config_path.parent, config_path}
|
||||
|
||||
|
||||
def test_startup_rejects_sessions_equal_to_workspace(tmp_path: Path) -> None:
|
||||
config_path = tmp_path / "config.json"
|
||||
workspace = tmp_path / "sessions"
|
||||
config_path.write_text(
|
||||
json.dumps({"agents": {"defaults": {"workspace": str(workspace)}}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
result = runner.invoke(app, ["gateway", "--config", str(config_path)])
|
||||
|
||||
assert result.exit_code == 1
|
||||
assert "session storage must be outside" in result.stdout
|
||||
assert not workspace.exists()
|
||||
|
||||
|
||||
def test_workspace_override_can_resolve_conflict_without_saving(tmp_path: Path) -> None:
|
||||
config_path = tmp_path / "config.json"
|
||||
config_path.write_text(
|
||||
json.dumps({"agents": {"defaults": {"workspace": str(tmp_path)}}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
before = config_path.read_bytes()
|
||||
workspace = tmp_path / "workspace"
|
||||
|
||||
config = _load_runtime_config(str(config_path), str(workspace))
|
||||
|
||||
assert config.workspace_path == workspace
|
||||
assert config.runtime_data_dir == tmp_path
|
||||
assert config_path.read_bytes() == before
|
||||
assert not workspace.exists()
|
||||
assert not (tmp_path / "sessions").exists()
|
||||
|
||||
|
||||
def test_webui_checks_resolved_workspace_before_saving(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
config_path = tmp_path / "config.json"
|
||||
config_path.write_text(
|
||||
json.dumps({"agents": {"defaults": {"workspace": "${TEST_WORKSPACE}"}}}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setenv("TEST_WORKSPACE", str(tmp_path))
|
||||
before = config_path.read_bytes()
|
||||
|
||||
result = runner.invoke(app, ["webui", "--config", str(config_path), "--yes", "--no-open"])
|
||||
|
||||
assert result.exit_code == 1
|
||||
assert "session storage must be outside" in result.stdout
|
||||
assert config_path.read_bytes() == before
|
||||
assert set(tmp_path.iterdir()) == {config_path}
|
||||
Reference in new issue
Block a user