diff --git a/.env.example b/.env.example index 06d645b..a020d8b 100644 --- a/.env.example +++ b/.env.example @@ -69,6 +69,10 @@ OLI_API_HOST="0.0.0.0" OLI_API_PORT=9734 OLI_API_PROFILE="default" OLI_API_MODE="agent" +# WebSocket permission policy: "prompt" asks the client, "auto" auto-approves. +# REST endpoints always auto-approve (no interactive channel). +OLI_API_PERMISSION_MODE="prompt" +OLI_API_PERMISSION_TIMEOUT=120 # Whether to use OAuth for authentication with third party MCP servers OLI_USE_OAUTH=false diff --git a/AGENTS.md b/AGENTS.md index 9fb9b10..6ee8072 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,20 +11,20 @@ This file is the top-level orientation map. Depth lives under [docs/](docs/). | Path | Purpose | | ---------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [oli_bot/chat.py](oli_bot/chat.py) | Textual TUI app (`OliBot`). Owns command handling, session UI, the `#command-suggestions` autocomplete `ListView`, and (when pooling is enabled) the "Active Sub-Agents" `Tree`. Tracks live context size (tokenizer-backed, shown in the `#status-bar`) plus cumulative session usage (persisted per session). Slash-command names come from the module-level `COMMANDS` tuple. | -| [oli_bot/api/](oli_bot/api/) | FastAPI package exposing the harness over an OpenAI-compatible REST API (`GET /v1/models`, `POST /v1/chat/completions` streaming + non-streaming, `GET /health`) plus a stateful `WS /v1/chat` WebSocket that relays every `AgentEvent` as a typed JSON envelope (`text_chunk`/`thinking`/`tool_call_executing`/`tool_call_result`/`assistant_response`/`usage`/`error`/`done`, plus `sub_agent_started`/`sub_agent_progress`/`sub_agent_completed` and `todo` frames) for real-time browser UIs. Sub-agent events are demuxed by `task_id`; `_wire_todo_relay()` pushes `builtin__todowrite` snapshots onto `mcp_manager.pending_todos`, drained by the socket loop. REST is stateless from the caller's POV; the WebSocket keeps per-connection history, backed by a server-persisted `ConversationStore` under the `"default"` server: turns are sent with an optional `session_id` (`{"content": "...", "session_id": "..."}`, `{"action": "clear", "session_id": "..."}` wipes it) and each completed turn is saved to disk, so browser and TUI share the same session files. Sessions are created/listed/loaded/renamed/deleted via `GET/POST /v1/sessions` and `GET/PUT/DELETE /v1/sessions/{id}`. A single process-private `Agent` is shared across requests and serialised with an `asyncio.Lock` held across `await` points (a `threading.RLock` would be per-thread reentrant and not serialize coroutines sharing the event loop); the WebSocket holds the lock for the duration of each run. Auto-approves permissions (no human), but offline/dry-run still apply. Also exposes `GET/POST /v1/mcp` plus `PUT/DELETE /v1/mcp/{name}` for the browser UI's MCP server configuration view (backed by `MCPClientManager` on `app.state.agent.mcp_manager`). `app.py` holds the `create_app()` factory + `init_state()`; `routers/` holds the route modules (`chat`, `ws`, `sessions`, `config`, `mcp`, `workspace`, `health`); `runner.py` implements the completion handoff, `harness.py` the agent construction + todo relay + `dispatch` handler, `session_service.py` the load/persist helpers, `errors.py` the OpenAI-style error handlers, `convert.py` the wire-message/event converters, `deps.py` the FastAPI dependencies, and `__main__.py` the `oli-server` entrypoint. [`oli_bot/api_server.py`](oli_bot/api_server.py) is a compat shim re-exporting the old single-module surface. See [docs/API_SERVER.md](docs/API_SERVER.md). | -| [oli_bot/agent.py](oli_bot/agent.py) | `Agent` — mode + system prompt owner; orchestrates the tool-calling loop and streams typed events (`TextChunk`, `ThinkingChunk`, `ToolCallChunk`, `ToolCallExecuting`, `ToolCallResult`, `StreamChunk`, `TokenCount`, `UsageEvent`, `Error`, `Done`). Emits a `TokenCount` snapshot at every checkpoint (`prompt`, `thinking`, `round`, `tool_result`, `final`) and aggregates per-call `UsageChunk`s from each backend round into a single per-run `UsageEvent`. Also hosts `sanitize_tool_history`, `_merge_usage`, `stream_sub_agent_run`, and `AgentPool` (built from `agents.yaml` when `--use-pool` is set; located via `$OLI_AGENTS_YAML`, the package dir, the repo root, the current working directory, or `~/.config/oli`). | -| [oli_bot/backends/](oli_bot/backends/) | Backend package — `ModelBackend` ABC, `OllamaBackend`, `OpenAIBackend`, `HuggingFaceBackend`, `TransformersBackend`, and the `create_model_backend()` factory. `OpenAIBackend` speaks both Chat Completions (default) and the Responses API, selected by the `OLI_OPENAI_RESPONSES_ENABLED` / `openai_responses_enabled` flag routed through `generate()` / `stream_generate()`. Also hosts the shared `_StreamingThinkParser` and per-backend message formatting (Ollama native `images`, OpenAI `image_url` or Bedrock-native blocks via `openai_vision_style`, Responses `input_text`/`input_image` items, textual placeholder for text-only backends). Every backend surfaces a trailing `UsageChunk`: exact counts from provider usage where available (OpenAI `usage`/`stream_options`, Responses `input_tokens`/`output_tokens`, Ollama `prompt_eval_count`/`eval_count`, HF `usage`), else a `~chars/4` estimate via `estimate_tokens`. Both `generate()` and `stream_generate()` accept a `max_tokens` cap; `Agent` threads the live `config.max_tokens` through streaming (and `_final_stream`) so a value set via `/config`/`settings.json` actually applies. Tool-call `arguments` JSON is decoded through `parse_tool_arguments()` / `tool_call_from_raw()` in `base.py`, which capture a truncated/malformed payload as `ToolCall.parse_error` instead of raising mid-stream. See [docs/BACKENDS.md](docs/BACKENDS.md). | -| [oli_bot/screens/](oli_bot/screens/) | All `ModalScreen` subclasses: `PermissionScreen`, `ConfirmScreen`, `ModelPickerScreen`, `ServerListScreen`, `MCPSetupScreen`, `SessionListScreen`, `WorkspaceListScreen`, `SubAgentViewScreen`, `ConfigScreen`, `InputPromptScreen`, `QuestionScreen` (answers the `builtin__question` tool — one modal listing the posed questions with options + per-question freeform fields and a Confirm button), plus `taglines.py` / `todo_widget.py`. | +| [oli_bot/chat.py](oli_bot/chat.py) | Textual TUI app (`OliBot`). Owns command handling, session UI, the `#command-suggestions` autocomplete `ListView`, and (when pooling is enabled) the "Active Sub-Agents" `Tree`. Tracks live context size (tokenizer-backed, shown in the `#status-bar`) plus cumulative session usage (persisted per session). Slash-command names come from the module-level `COMMANDS` tuple. `Ctrl+I` (and `Esc` while generating) interrupts the in-flight turn: `on_key` cancels the stored `@work(exclusive=True)` worker, dismisses a blocking `PermissionScreen`/`QuestionScreen`, marks partial panels `(interrupted)`, deletes the interrupted user message from history, and restores its text to the input box; a per-turn id guards the rollback so a superseding turn is never clobbered. | +| [oli_bot/api/](oli_bot/api/) | FastAPI package exposing the harness over an OpenAI-compatible REST API (`GET /v1/models`, `POST /v1/chat/completions` streaming + non-streaming, `GET /health`) plus a stateful `WS /v1/chat` WebSocket that relays every `AgentEvent` as a typed JSON envelope (`text_chunk`/`thinking`/`tool_call_executing`/`tool_call_result`/`assistant_response`/`usage`/`error`/`done`, plus `sub_agent_started`/`sub_agent_progress`/`sub_agent_completed` and `todo` frames) for real-time browser UIs. Sub-agent events are demuxed by `task_id`; `_wire_todo_relay()` pushes `builtin__todowrite` snapshots onto `mcp_manager.pending_todos`, drained by the socket loop. REST is stateless from the caller's POV; the WebSocket keeps per-connection history, backed by a server-persisted `ConversationStore` under the `"default"` server: turns are sent with an optional `session_id` (`{"content": "...", "session_id": "..."}`, `{"action": "clear", "session_id": "..."}` wipes it) and each completed turn is saved to disk, so browser and TUI share the same session files. Sessions are created/listed/loaded/renamed/deleted via `GET/POST /v1/sessions` and `GET/PUT/DELETE /v1/sessions/{id}`. A single process-private `Agent` is shared across requests and serialised with an `asyncio.Lock` held across `await` points (a `threading.RLock` would be per-thread reentrant and not serialize coroutines sharing the event loop); the WebSocket holds the lock for the duration of each run. REST has no interactive channel, so it auto-approves permissions (confirm callback returns `"session"`); the WebSocket instead prompts by default via `PermissionBroker` (`permissions.py`) — a `permission_request` frame with `once`/`session`/`deny` options and a `default` of `deny` plus a timeout (`permission_timeout` frame → deny) — with `api_server.permission_mode = "auto"` restoring auto-approval. Each turn runs as an `asyncio.Task` (`_run_turn`) so the receive loop can route `{"action": "permission_response"}` and `{"action": "interrupt"}` concurrently; interrupt cancels the turn, rolls the partial user turn back out of history, and emits `interrupted` then `done` (`interrupted: true`). Offline/dry-run still apply. Also exposes `GET/POST /v1/mcp` plus `PUT/DELETE /v1/mcp/{name}` for the browser UI's MCP server configuration view (backed by `MCPClientManager` on `app.state.agent.mcp_manager`). `app.py` holds the `create_app()` factory + `init_state()`; `routers/` holds the route modules (`chat`, `ws`, `sessions`, `config`, `mcp`, `workspace`, `health`); `runner.py` implements the completion handoff, `harness.py` the agent construction + todo relay + `dispatch` handler (and the active-confirm `ContextVar` that lets sub-agent tasks inherit the per-turn callback), `session_service.py` the load/persist helpers, `errors.py` the OpenAI-style error handlers, `convert.py` the wire-message/event converters, `deps.py` the FastAPI dependencies, and `__main__.py` the `oli-server` entrypoint. [`oli_bot/api_server.py`](oli_bot/api_server.py) is a compat shim re-exporting the old single-module surface. See [docs/API_SERVER.md](docs/API_SERVER.md). | +| [oli_bot/agent.py](oli_bot/agent.py) | `Agent` — mode + system prompt owner; orchestrates the tool-calling loop and streams typed events (`TextChunk`, `ThinkingChunk`, `ToolCallChunk`, `ToolCallExecuting`, `ToolCallResult`, `StreamChunk`, `TokenCount`, `UsageEvent`, `Error`, `Done`). Emits a `TokenCount` snapshot at every checkpoint (`prompt`, `thinking`, `round`, `tool_result`, `final`) and aggregates per-call `UsageChunk`s from each backend round into a single per-run `UsageEvent`. Also hosts `sanitize_tool_history`, `_merge_usage`, `stream_sub_agent_run`, and `AgentPool` (built from `agents.yaml` when `--use-pool` is set; located via `$OLI_AGENTS_YAML`, the package dir, the repo root, the current working directory, or `~/.config/oli`). `_run_dispatch_with_events` cancels its fan-out task and queue getter in a `finally`, so a Ctrl+I / WebSocket interrupt never leaves sub-agents running in the background. | +| [oli_bot/backends/](oli_bot/backends/) | Backend package — `ModelBackend` ABC, `OllamaBackend`, `OpenAIBackend`, `HuggingFaceBackend`, `TransformersBackend`, and the `create_model_backend()` factory. `OpenAIBackend` speaks both Chat Completions (default) and the Responses API, selected by the `OLI_OPENAI_RESPONSES_ENABLED` / `openai_responses_enabled` flag routed through `generate()` / `stream_generate()`. Also hosts the shared `_StreamingThinkParser` and per-backend message formatting (Ollama native `images`, OpenAI `image_url` or Bedrock-native blocks via `openai_vision_style`, Responses `input_text`/`input_image` items, textual placeholder for text-only backends). Every backend surfaces a trailing `UsageChunk`: exact counts from provider usage where available (OpenAI `usage`/`stream_options`, Responses `input_tokens`/`output_tokens`, Ollama `prompt_eval_count`/`eval_count`, HF `usage`), else a `~chars/4` estimate via `estimate_tokens`. Both `generate()` and `stream_generate()` accept a `max_tokens` cap; `Agent` threads the live `config.max_tokens` through streaming (and `_final_stream`) so a value set via `/config`/`settings.json` actually applies. Tool-call `arguments` JSON is decoded through `parse_tool_arguments()` / `tool_call_from_raw()` in `base.py`, which capture a truncated/malformed payload as `ToolCall.parse_error` instead of raising mid-stream. Cancellation is honoured promptly by the Ollama/OpenAI/HuggingFace backends; the local `TransformersBackend` uses a synchronous `TextIteratorStreamer` loop and only observes cancellation after the current generation pass finishes (interrupt deferred). See [docs/BACKENDS.md](docs/BACKENDS.md). | +| [oli_bot/screens/](oli_bot/screens/) | All `ModalScreen` subclasses: `PermissionScreen`, `ConfirmScreen`, `ModelPickerScreen`, `ServerListScreen`, `MCPSetupScreen`, `SessionListScreen`, `WorkspaceListScreen`, `SubAgentViewScreen`, `ConfigScreen`, `InputPromptScreen`, `QuestionScreen` (answers the `builtin__question` tool — one modal listing the posed questions with options + per-question freeform fields and a Confirm button), plus `taglines.py` / `todo_widget.py`. `ConfigScreen` renders one control per `settings.json` leaf (`CONFIG_FIELDS` must stay in sync with `settings.SETTINGS_FLAT_MAP`) and its `_save()` merges into the loaded settings so unknown/future keys survive a round-trip. | | [oli_bot/models.py](oli_bot/models.py) | Shared dataclasses: `Message`, `ToolCall`, `ModelResponse`, `HostConfig`, `MCPServerConfig`, `ProfileData`, `SubAgentRun`, `ImageAttachment`, `TodoItem` / `TodoListState`, plus `AgentEvent` variants and the `AgentRole` enum. Token accounting lives here too: `Usage` (prompt/completion/`estimated` flag), the per-call `UsageChunk` stream event, the per-run `UsageEvent`, and the live `TokenCount` snapshot. `Message.images` is in-memory only (dropped on session save); `ModelResponse.usage` is optionally set by backends. | | [oli_bot/tokens.py](oli_bot/tokens.py) | Token counting shared by the agent, TUI, and API: `estimate_tokens` (`~chars/4` fallback), `count_text_tokens` / `count_message_tokens` (tiktoken, with generic BPE fallback for unknown models and an `estimated` flag), and `ContextTokenTracker` (incremental prompt + in-flight completion accounting for live `TokenCount` snapshots). | | [oli_bot/config.py](oli_bot/config.py) | `AppConfig` — `pydantic_settings.BaseSettings`. Env vars prefixed `OLI_`, plus `.env` support and `OLI_TRUNCATION_SMALL` / `_LARGE` aliases via `AliasChoices`. Module-level `configs = AppConfig()` singleton. See [docs/CONFIGURE.md](docs/CONFIGURE.md). | -| [oli_bot/settings.py](oli_bot/settings.py) | `SettingsManager` — load/save/merge `~/.config/oli/settings.json`; precedence `settings.json` > `OLI_*` env > SDK-standard env (`OPENAI_API_KEY`, `OPENAI_BASE_URL`, `HUGGINGFACE_API_KEY`, `HF_TOKEN`) > declared defaults. Empty API-key strings in JSON fall through to env. | +| [oli_bot/settings.py](oli_bot/settings.py) | `SettingsManager` — load/save/merge `~/.config/oli/settings.json`; precedence `settings.json` > `OLI_*` env > SDK-standard env (`OPENAI_API_KEY`, `OPENAI_BASE_URL`, `HUGGINGFACE_API_KEY`, `HF_TOKEN`) > declared defaults. Empty API-key strings in JSON fall through to env. Also exports `DEFAULT_SETTINGS`, the flat `SETTINGS_FLAT_MAP` (one browser-friendly key per settings leaf, shared with `GET/PUT /v1/config` and `ConfigScreen.CONFIG_FIELDS`), and the `OLI_*` env↔nested mappings (`to_appconfig()` / `from_appconfig()`), including `api_server.permission_mode` / `permission_timeout`. | | [oli_bot/profiles/](oli_bot/profiles/) | `ProfileManifest` / `PermissionsManifest` (Pydantic) in `schema.py`; `ProfilePermissionEnforcer` (layered allow/deny glob patterns, base-profile inheritance, deny-overrides-allow) in `permissions.py`; `ProfileManager` (profile CRUD, manifest loading, circular-dependency detection) in `manager.py`. Built-in profiles ship as sibling directories. | | [oli_bot/mcp_client.py](oli_bot/mcp_client.py) | `MCPClientManager` — MCP server lifecycle (stdio/http via v2 `mcp.client.Client`, `mode="auto"` handshake), tool discovery + invocation, per-server tool-list cache, offline gating. HTTP servers authenticate via a static `api_token`, or OAuth 2.1 when `use_oauth` is enabled and no token is set. Uses v2 snake_case fields (`Tool.input_schema`, `CallToolResult.is_error`/`structured_content`). | | [oli_bot/auth/oauth.py](oli_bot/auth/oauth.py) | `OAuthHandler` — OAuth 2.1 client-side authorization for HTTP MCP servers. RFC 9728 protected-resource-metadata discovery, OIDC / OAuth authorization-server metadata discovery with fallback, RFC 7591 dynamic client registration, authorization-code + PKCE (S256) flow via a temporary localhost callback server and browser consent, token exchange/refresh, and Bearer-header injection. Wired into `MCPClientManager` when `use_oauth` is enabled and the server has no `api_token`. | | [oli_bot/tools/manager.py](oli_bot/tools/manager.py) | `BuiltinToolManager` — registration, profile + session permission gating, dry-run gating, offline gating, and `TruncationManager` post-processing. Awaits coroutine handlers. | -| [oli_bot/tools/](oli_bot/tools/) | Tool handlers: `files.py` (read/write/edit + `view_image` via Pillow), `directories.py` (glob/grep/list_directory/tree — filesystem work runs via `asyncio.to_thread` / `create_subprocess_exec`), `web.py` (search + fetch + specialised searches, all guarded by `_check_ssrf`), `shell.py` (allowlisted `run_command`, including read-only `git`), `parsing.py` (`compare`), `memory.py` (`think`, `todowrite`, `notebook`), `question.py` (pose questions to the user via a TUI modal and block for answers; no interactive callback — e.g. the API server — yields an error result), `truncation.py` (per-tier char budgets), `permissions.py` (sensitive-path detection). See [docs/TOOLS.md](docs/TOOLS.md). | +| [oli_bot/tools/](oli_bot/tools/) | Tool handlers: `files.py` (read/write/edit + `view_image` via Pillow), `directories.py` (glob/grep/list_directory/tree — filesystem work runs via `asyncio.to_thread` / `create_subprocess_exec`), `web.py` (search + fetch + specialised searches, all guarded by `_check_ssrf`), `shell.py` (allowlisted `run_command`, including read-only `git`; each command runs in its own process group via `start_new_session` and a timeout or task cancellation kills and reaps the whole pipeline), `parsing.py` (`compare`), `memory.py` (`think`, `todowrite`, `notebook`), `question.py` (pose questions to the user via a TUI modal and block for answers; no interactive callback — e.g. the API server — yields an error result), `truncation.py` (per-tier char budgets), `permissions.py` (sensitive-path detection). See [docs/TOOLS.md](docs/TOOLS.md). | | [oli_bot/sessions.py](oli_bot/sessions.py) | `Session` (permission gating) + `ConversationStore` (per-server JSON persistence under `~/.config/oli/sessions//`) + `WorkspaceManager`. `save_session()` returns the (possibly new) id so callers can rebind after a corrupt-file rewrite. Persisted messages preserve `tool_call_id`; loads pass through `sanitize_tool_history` so poisoned histories self-heal. | | [oli_bot/backends/upstream_manager.py](oli_bot/backends/upstream_manager.py) | `UpstreamManager` — multi-server lifecycle persisted to `hosts.json`, URL validation. | | [oli_bot/voice.py](oli_bot/voice.py) | `VoiceEngine` — optional, lazy-loaded mic → STT → TTS engine for the `/voice` command (faster-whisper, Piper TTS, WebRTC VAD, pyaudio). All I/O is blocking; `chat.py` calls it via `asyncio.to_thread`. `record()` accepts a `threading.Event` so `chat.py` can interrupt an in-progress recording the instant voice mode is toggled off, instead of waiting out the silence/max-duration timeout. All seven tunables (whisper/piper models, sample rate, VAD frame duration, VAD aggressiveness, silence timeout, max record seconds) are `AppConfig` fields (`OLI_VOICE_*` env / `settings.json` `voice` section / `/config` screen); `chat.py` passes them explicitly when constructing the engine, and saving `/config` drops the engine so the next `/voice` picks up new values. | @@ -74,9 +74,10 @@ See [docs/AGENT_POOLING.md](docs/AGENT_POOLING.md) for the `agents.yaml` schema, - **Tool loop** — `Agent._tool_loop` calls `backend.stream_generate(..., max_tokens=self.config.max_tokens)`, streaming text and thinking to the TUI while accumulating tool calls; after each round it calls `MCPClientManager.drain_builtin_attachments()` so `view_image` results ride into the next turn as a synthetic user-role `Message(images=...)` (kept **after** all `role=tool` messages so the assistant→tool block stays contiguous for `sanitize_tool_history`). A tool call whose streamed `arguments` failed to parse (`ToolCall.parse_error` set, e.g. the model hit the output-token limit mid-write) is **not** dispatched: the error is fed back as the tool result so the model can retry with a smaller payload. If iterations are exhausted, `_final_stream` does one text-only pass to guarantee a reply. - **Token accounting** — two complementary metrics. **Live context size** is a `TokenCount` snapshot emitted by `Agent.process()` at every checkpoint: `prompt` (user submitted, system + history + message), `thinking` (a thinking block finished), `round` (provider usage for a completed model call), `tool_result` (a tool result was appended), and `final` (turn complete). Counts come from `oli_bot/tokens.py` (tiktoken with a generic-BPE fallback, else the `~chars/4` heuristic; `estimated=True` when heuristic/image overhead is involved). The TUI renders it in the `#status-bar` as `ctx N tok` (`~` prefix when estimated) and recomputes it on session load/switch/clear/profile/mode/model/server changes. **Cumulative billed usage** is still produced by each backend's trailing `UsageChunk` (exact where the provider reports counts); the agent sums per-call chunks across a run and emits a single `UsageEvent` before `Done`. The TUI adds that to the session total, which persists in session JSON (`total_tokens` / `total_tokens_estimated`) and resets on `/clear`, `/home`, and new-session flows. The REST API returns the summed usage in non-streaming responses, and the WebSocket relays `token_count` frames alongside `usage`. - **Error paths** — a failed round yields `Error` for the UI then `Done(full_text="")`; the empty `full_text` deliberately trips the TUI's "skip empty assistant append" branch so error text never enters `self.messages` (prevents the Bedrock/Anthropic 400 poisoning loop). All `stream_generate()` implementations re-raise instead of swallowing. -- **Permission gating** — `BuiltinToolManager.call_tool()` runs the profile enforcer, then `Session.needs_permission()`, then (if needed) `confirm_callback(description)`; the TUI shows `PermissionScreen` and the callback returns `"once"`, `"session"`, or `"deny"`. Session grants persist per scope for the process lifetime. `glob`/`grep` targeting patterns like `.env*`, `*.pem`, `*secret*` trigger the `workspace_sensitive` scope even inside the workspace. +- **Permission gating** — `BuiltinToolManager.call_tool()` runs the profile enforcer, then `Session.needs_permission()`, then (if needed) `confirm_callback(description)`; the TUI shows `PermissionScreen` and the callback returns `"once"`, `"session"`, or `"deny"`. Session grants persist per scope for the process lifetime. `glob`/`grep` targeting patterns like `.env*`, `*.pem`, `*secret*` trigger the `workspace_sensitive` scope even inside the workspace. On the API, REST auto-returns `"session"` (no channel), while the WebSocket routes the prompt through `PermissionBroker` to the client (default deny on timeout; `api_server.permission_mode = "auto"` opts out). +- **Interrupt / cancellation** — `Ctrl+I` in the TUI and `{"action": "interrupt"}` over the API cancel the active `Agent.process()` turn. Cancellation unwinds the async generators, `_run_dispatch_with_events` cancels its sub-agent fan-out, `run_command` kills its process group, the partial turn is rolled back out of history, and the originating prompt is restored (TUI input box) or re-sendable (WebSocket `interrupted` + `done`). The local Transformers backend is the one exception: its synchronous streamer only observes cancellation after the current pass (deferred). - **Built-in tool naming** — registered as `builtin__` and dispatched by `MCPClientManager.call_tool`. -- **Settings round-trip** — runtime model changes (`/model set-large|set-small`, `/servers set-default-model`) persist back to `settings.json` via `OliBot._persist_model_to_settings`; the `/config` form pre-populates from effective runtime values (server overrides included) via `_sync_settings_from_runtime`. Backend construction honours `self.config` overrides so JSON beats env at startup. +- **Settings round-trip** — runtime model changes (`/model set-large|set-small`, `/servers set-default-model`) persist back to `settings.json` via `OliBot._persist_model_to_settings`; the `/config` form pre-populates from effective runtime values (server overrides included) via `_sync_settings_from_runtime` and writes back through `SettingsManager` (its `_save()` overlays onto the loaded settings, preserving unknown/future keys). Backend construction honours `self.config` overrides so JSON beats env at startup. - **Voice mode** — `/voice` toggles a background `@work(exclusive=False)` loop (`_run_voice_loop`) that cycles mic record → `VoiceEngine.transcribe` → the normal `_handle_user_message` chat pipeline → `VoiceEngine.speak`. Fully local (no network calls). A `threading.Event` (`_voice_stop_event`) is set the instant `/voice` toggles off so the blocking `record()` call returns immediately instead of running out the silence/max-duration timeout. For sequence diagrams and the full state machines, see [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md). @@ -142,7 +143,7 @@ Run the API server with `oli-server` (uvicorn on `0.0.0.0:9734`). Env vars: `OLI ## Configuration -`AppConfig` fields are overridable via `OLI_*` env vars or a `.env` file. Effective precedence: `settings.json` > `OLI_*` env > SDK-standard env (`OPENAI_API_KEY`, `OPENAI_BASE_URL`, `HUGGINGFACE_API_KEY`, `HF_TOKEN`) > declared defaults. Empty API-key strings in `settings.json` fall through to env. +`AppConfig` fields are overridable via `OLI_*` env vars or a `.env` file. Effective precedence: `settings.json` > `OLI_*` env > SDK-standard env (`OPENAI_API_KEY`, `OPENAI_BASE_URL`, `HUGGINGFACE_API_KEY`, `HF_TOKEN`) > declared defaults. Empty API-key strings in `settings.json` fall through to env. The `/config` screen exposes every leaf (`settings.SETTINGS_FLAT_MAP`), and the same flat keys are read/written by `GET/PUT /v1/config`; new settings must be added to that map, the screen's `CONFIG_FIELDS`, and the docs. For the full setting table, per-backend model tiers, truncation aliases (`OLI_TRUNCATION_SMALL` / `_LARGE`), and quick examples, see [docs/CONFIGURE.md](docs/CONFIGURE.md). @@ -205,4 +206,4 @@ can never be picked up. ## Commands (in-app) -`/help`, `/models [name]`, `/model large|small`, `/model set-large|set-small `, `/config`, `/context`, `/servers add|list|remove|default|switch|use-model`, `/mcp add|list|remove`, `/mode [ask|agent|chat|plan]`, `/profile list|load|create`, `/sessions [list|switch|delete|rename|purge]`, `/workspace list|set|unset`, `/offline`, `/dry-run`, `/voice`, `/clear`, `/home`, `Ctrl+Q`, `Ctrl+L`, `Ctrl+Y` +`/help`, `/models [name]`, `/model large|small`, `/model set-large|set-small `, `/config`, `/context`, `/servers add|list|remove|default|switch|use-model`, `/mcp add|list|remove`, `/mode [ask|agent|chat|plan]`, `/profile list|load|create`, `/sessions [list|switch|delete|rename|purge]`, `/workspace list|set|unset`, `/offline`, `/dry-run`, `/voice`, `/clear`, `/home`, `Ctrl+I` (interrupt), `Ctrl+Q`, `Ctrl+L`, `Ctrl+Y` diff --git a/README.md b/README.md index c0070e0..1537a88 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,13 @@ can pick up where you left off. | `/clear` | Clear the conversation | | `/home` | Return to the home screen | | `Ctrl+Q` / `Ctrl+L` / `Ctrl+Y` | Quit / Clear / Copy last message | +| `Ctrl+I` (or `Esc` while generating) | Interrupt the current response; the prompt is restored to the input box | + +## Interrupting a response + +Press `Ctrl+I` (or `Esc`) while the assistant is streaming or running tools to stop the turn. The partial response stays on screen marked `(interrupted)`, the originating user message is removed from history, and its text is restored to the input box so you can edit and resubmit. Shell commands started by the turn are killed, and a delegated `dispatch` fan-out is cancelled rather than left running in the background. + +> The Transformers backend cannot currently be interrupted mid-generation: its synchronous `TextIteratorStreamer` loop only observes cancellation once the current generation finishes. Local Transformers runs therefore stop at the next checkpoint, not instantly. Ollama, OpenAI, and HuggingFace backends cancel promptly. ## Voice mode (experimental) @@ -207,9 +214,9 @@ It listens on `0.0.0.0:9734` by default (override with `OLI_API_HOST`/`OLI_API_P | `WS /v1/chat` | Stateful WebSocket — per-connection history, relays every agent event as a typed JSON frame | | `GET /health` | Liveness probe | -REST conversations are **stateless** (like real OpenAI): each `/v1/chat/completions` request carries its full message history. The server holds a single process-private `Agent` instance (backend, tool registrations, MCP wiring) shared across requests, and serializes concurrent in-flight requests in-process. Because there is no human to prompt at permission time, the API auto-allows permission scopes for the current request; offline and dry-run gating from `AppConfig` still apply. +REST conversations are **stateless** (like real OpenAI): each `/v1/chat/completions` request carries its full message history. The server holds a single process-private `Agent` instance (backend, tool registrations, MCP wiring) shared across requests, and serializes concurrent in-flight requests in-process. Because a stateless REST request has no interactive channel to prompt at permission time, REST auto-allows permission scopes for the current request; offline and dry-run gating from `AppConfig` still apply. -The `WS /v1/chat` WebSocket is the stateful counterpart for real-time browser UIs: the server keeps a per-connection `messages` history, so a client sends each next turn as `{"content": "..."}` and receives every `AgentEvent` back as a typed JSON frame (`text_chunk`/`thinking`/`tool_call_executing`/`tool_call_result`/`assistant_response`/`usage`/`error`/`done`), plus `sub_agent_started`/`sub_agent_progress`/`sub_agent_completed` and `todo` frames for delegated runs and live task lists; `{"action": "clear"}` resets the history. See [docs/API_SERVER.md](docs/API_SERVER.md) for the full frame reference. +The `WS /v1/chat` WebSocket is the stateful counterpart for real-time browser UIs: the server keeps a per-connection `messages` history, so a client sends each next turn as `{"content": "..."}` and receives every `AgentEvent` back as a typed JSON frame (`text_chunk`/`thinking`/`tool_call_executing`/`tool_call_result`/`assistant_response`/`usage`/`error`/`done`), plus `sub_agent_started`/`sub_agent_progress`/`sub_agent_completed` and `todo` frames for delegated runs and live task lists. Unlike REST, the WebSocket **prompts for permission** by default: the server sends a `permission_request` frame and waits for `{"action": "permission_response", "id": ..., "decision": "once"|"session"|"deny"}` (120 s timeout → deny). Set `OLI_API_PERMISSION_MODE=auto` to restore the legacy auto-approve behaviour. `{"action": "clear"}` resets the history; `{"action": "interrupt"}` cancels the in-flight turn (the server emits `interrupted` then `done`, and rolls the partial user turn back out of history). See [docs/API_SERVER.md](docs/API_SERVER.md) for the full frame reference. ### curl @@ -256,12 +263,14 @@ The `openai` SDK needs an `api_key`; pass a placeholder — the server does not ### API configuration -| Env var | Default | Description | -| ----------------- | --------- | ------------------------------------------- | -| `OLI_API_HOST` | `0.0.0.0` | Bind address | -| `OLI_API_PORT` | `9734` | Listen port | -| `OLI_API_PROFILE` | `default` | Agent profile to load | -| `OLI_API_MODE` | `agent` | Agent mode (`agent`, `ask`, `plan`, `chat`) | +| Env var | Default | Description | +| --------------------------- | --------- | ----------------------------------------------------------------- | +| `OLI_API_HOST` | `0.0.0.0` | Bind address | +| `OLI_API_PORT` | `9734` | Listen port | +| `OLI_API_PROFILE` | `default` | Agent profile to load | +| `OLI_API_MODE` | `agent` | Agent mode (`agent`, `ask`, `plan`, `chat`) | +| `OLI_API_PERMISSION_MODE` | `prompt` | WebSocket permission policy: `prompt` (ask the client) or `auto` | +| `OLI_API_PERMISSION_TIMEOUT`| `120` | Seconds to wait for a WebSocket permission reply before denying | `max_tokens` / `temperature` request fields are accepted but best-effort: the agent tool loop reads them from `AppConfig` (configured via `OLI_*` env or `settings.json`). diff --git a/docs/API_SERVER.md b/docs/API_SERVER.md index c9b0eb1..674c682 100644 --- a/docs/API_SERVER.md +++ b/docs/API_SERVER.md @@ -72,10 +72,13 @@ with the same `OLI_*` env vars / `settings.json` the TUI uses. across `await` points. Concurrent requests queue. (A threading lock would not work: it is per-thread reentrant, and all coroutines sharing the event loop run on the same thread.) -- **No human in the loop** — the permission confirm-callback auto-defaults to - `"session"` for every scope (the REST equivalent of the TUI's "Allow for - session"). Offline mode and dry-run gating from `AppConfig` still apply - unchanged. +- **Permission handling** — REST has no interactive channel, so its confirm + callback auto-defaults to `"session"` for every scope (the REST equivalent of + the TUI's "Allow for session"). The stateful WebSocket does have a channel, so + it prompts the client by default (`permission_request` → `permission_response`) + and denies on timeout. Offline mode and dry-run gating from `AppConfig` still + apply unchanged on both surfaces. Set `OLI_API_PERMISSION_MODE=auto` to restore + auto-approval for WebSocket clients too. ## Endpoints @@ -251,6 +254,9 @@ A stateful WebSocket for real-time browser UIs. The server maintains a per-connection `messages` history, so a client sends each next user turn as `{"content": "..."}` and receives every `AgentEvent` back as a typed JSON frame. `{"action": "clear"}` resets the per-connection history. +`{"action": "interrupt"}` cancels the in-flight turn: the server emits an +`interrupted` frame followed by `done`, and rolls the partial user turn back out +of the history so the client can resubmit it. The server sends a `{"type": "connected"}` frame on accept, then one frame per inbound message: @@ -265,13 +271,47 @@ inbound message: | `token_count` | `{"prompt_tokens", "completion_tokens", "total_tokens", "estimated", "phase"}` — live context snapshot (`phase` is `prompt`/`thinking`/`round`/`tool_result`/`final`) | | `usage` | `{"prompt_tokens": ..., "completion_tokens": ..., ...}` | | `error` | `{"message": "..."}` | -| `done` | `{"full_text": "..."}` — run finished | +| `interrupted` | `{"full_text": "..."}` — partial text when a turn is interrupted | +| `done` | `{"full_text": "...", "interrupted": true?}` — run finished | | `cleared` | `{}` — history reset (reply to `action: clear`) | +| `permission_request` | `{"id", "description", "options", "default", "timeout"}` — reply required | +| `permission_timeout` | `{"id"}` — the prompt timed out and resolved to deny | | `sub_agent_started` | `{"task_id", "agent_name", "pool_name", "task"}` | | `sub_agent_progress` | `{"task_id", "agent_name", "activity", "status"}` | | `sub_agent_completed` | `{"task_id", "agent_name", "status", "full_text"}` | | `todo` | `{"todos": [...]}` (root) or with `task_id`/`agent_name` (sub) | +#### Permissions + +By default (`api_server.permission_mode = "prompt"`), a permission-gated tool +sends: + +```json +{ + "type": "permission_request", + "data": { + "id": "perm-…", + "description": "Write to /path/file.txt", + "options": ["once", "session", "deny"], + "default": "deny", + "timeout": 120.0 + } +} +``` + +The client replies with: + +```json +{"action": "permission_response", "id": "perm-…", "decision": "once"} +``` + +`once` allows just this call, `session` grants the scope for the rest of the +server process, and `deny` (the timeout default) returns a permission error to +the model. An unknown/expired `id` or an invalid decision produces an `error` +frame. Set `OLI_API_PERMISSION_MODE=auto` to skip prompts entirely. The pending +prompt holds the process-wide agent lock, so all other turns queue until the +client answers or the timeout elapses. + Sub-agent activity is relayed live while a `dispatch` tool call is in flight: frames carry the same shape as above (`text_chunk`, `tool_call_executing`, …) with `task_id` and `agent_name` attached so the client can demux by run. @@ -294,9 +334,18 @@ async def main(): break if frame["type"] == "text_chunk": print(frame["data"]["text"], end="") + elif frame["type"] == "permission_request": + prompt = frame["data"] + print("\nPERMISSION:", prompt["description"]) + await ws.send(json.dumps({ + "action": "permission_response", + "id": prompt["id"], + "decision": "deny", # or "once" / "session" + })) elif frame["type"] == "error": print("ERROR:", frame["data"]["message"]) # clear with: await ws.send(json.dumps({"action": "clear"})) + # interrupt with: await ws.send(json.dumps({"action": "interrupt"})) asyncio.run(main()) ``` @@ -314,7 +363,8 @@ field shape (`openai_model`, `truncation_max_chars_small`, `api_port`, ...). `GET /v1/config` returns the current flat config — one key per `settings.json` leaf, so every setting the TUI's `/config` screen edits is reachable here (`session_*`, `workspace_max_workspaces`, `ollama_context_window`, -`first_chunk_timeout`, `openai_responses_enabled`, ...): +`first_chunk_timeout`, `openai_responses_enabled`, `api_permission_mode`, +`api_permission_timeout`, ...): ```bash curl http://localhost:9734/v1/config @@ -325,7 +375,9 @@ curl http://localhost:9734/v1/config "backend": "ollama", "ollama_model": "llama3.1", "temperature": 0.7, - "api_port": 9734 + "api_port": 9734, + "api_permission_mode": "prompt", + "api_permission_timeout": 120.0 } ``` @@ -448,9 +500,10 @@ is what the TUI's `/profile load` does. - **Workspace** — the agent's workspace defaults to the process CWD unless the CWD is a sensitive path (see `is_sensitive_path` in - [`sessions.py`](../oli_bot/sessions.py)). File and shell tools that touch paths - outside the workspace are auto-approved in API mode but still respect - offline/dry-run gating. + [`sessions.py`](../oli_bot/sessions.py)). REST requests auto-approve file and + shell tools that touch paths outside the workspace; WebSocket clients receive + a `permission_request` instead (unless `api_permission_mode = "auto"`). Both + surfaces still respect offline/dry-run gating. - **Modes** — in `ask` / `plan` modes only read-only tools are exposed (plus MCP limits per mode); in `chat` mode no tools are offered at all. The mode also shapes the system prompt, exactly as in the TUI. @@ -461,5 +514,10 @@ is what the TUI's `/profile load` does. - **Concurrency** — requests are serialized on a process-wide lock; there is no horizontal multiplexing. For concurrent consumers, run multiple `oli-server` processes. +- **Interrupts** — `{"action": "interrupt"}` cancels the in-flight turn, kills + shell subprocesses started by it, and cancels a delegated `dispatch` fan-out. + The partial user turn is rolled back and the client receives `interrupted` + followed by `done` (`interrupted: true`). The local Transformers backend only + observes cancellation after its current generation pass finishes. - **Logging** — the server writes NDJSON to `AppConfig.log_file` via the shared logger in [`logger.py`](../oli_bot/logger.py). diff --git a/docs/CONFIGURE.md b/docs/CONFIGURE.md index d400004..49a7090 100644 --- a/docs/CONFIGURE.md +++ b/docs/CONFIGURE.md @@ -48,7 +48,8 @@ If `~/.config/oli/settings.json` does not exist, it is auto-created on first loa | `request_timeout` | `30.0` | `OLI_REQUEST_TIMEOUT` | HTTP request timeout (seconds) | | `max_messages` | `100` | `OLI_MAX_MESSAGES` | Auto-prune chat history when message count exceeds this | | `max_tool_iterations` | `25` | `OLI_MAX_TOOL_ITERATIONS` | Max tool-calling rounds per response | -| `stream_timeout` | `240.0` | `OLI_STREAM_TIMEOUT` | Seconds before streaming response times out | +| `stream_timeout` | `300.0` | `OLI_STREAM_TIMEOUT` | Seconds of inactivity before a streaming response times out | +| `first_chunk_timeout` | `600.0` | `OLI_FIRST_CHUNK_TIMEOUT` | Seconds to wait for the first streamed chunk (large local models can cold-start slowly) | | `model_filters` | `""` | `OLI_MODEL_FILTERS` | Comma-separated model name exclusion filters | | `truncation_max_chars_small` | `4000` | `OLI_TRUNCATION_SMALL` | Max chars for tool results under the small model tier | | `truncation_max_chars_large` | `100000` | `OLI_TRUNCATION_LARGE` | Max chars for tool results under the large model tier | @@ -66,6 +67,15 @@ If `~/.config/oli/settings.json` does not exist, it is auto-created on first loa | `voice_vad_aggressiveness` | `2` | `OLI_VOICE_VAD_AGGRESSIVENESS` | WebRTC VAD aggressiveness `0`–`3`; higher rejects more noise | | `voice_silence_timeout_ms` | `800` | `OLI_VOICE_SILENCE_TIMEOUT_MS` | Stop recording after this much consecutive silence | | `voice_max_record_seconds` | `15` | `OLI_VOICE_MAX_RECORD_SECONDS` | Hard cap on a single recording | +| `api_host` | `0.0.0.0` | `OLI_API_HOST` | `oli-server` bind address | +| `api_port` | `9734` | `OLI_API_PORT` | `oli-server` listen port | +| `api_profile` | `default` | `OLI_API_PROFILE` | Profile loaded by `oli-server` | +| `api_mode` | `agent` | `OLI_API_MODE` | Agent mode for `oli-server` (`agent`, `ask`, `plan`, `chat`) | +| `api_permission_mode` | `prompt` | `OLI_API_PERMISSION_MODE` | WebSocket permission policy: `prompt` asks the client; `auto` keeps the legacy auto-approve behaviour. REST always auto-approves. | +| `api_permission_timeout` | `120.0` | `OLI_API_PERMISSION_TIMEOUT` | Seconds to wait for a WebSocket `permission_response` before denying | + +> The `/config` screen renders every setting above, and `GET/PUT /v1/config` +> exposes the same flat field names for browser clients. ## Quick examples diff --git a/docs/SECURITY.md b/docs/SECURITY.md index b6c7b3d..72e311c 100644 --- a/docs/SECURITY.md +++ b/docs/SECURITY.md @@ -113,9 +113,14 @@ Agent pooling introduces a second execution context, and it inherits the permiss ## API server trust model -The OpenAI-compatible API server (`oli-server`) removes the human from the permission loop by necessity — there's no terminal to prompt. Consequently, **the API server auto-allows permission scopes for the current request.** Offline mode and dry-run gating from `AppConfig` still apply and are not bypassed, but the interactive allow/deny/session-grant system described above does not run in this mode. +The OpenAI-compatible API server (`oli-server`) exposes two surfaces with different permission behaviour: -This has a direct implication for any surface you place in front of the API server: **the API server's security boundary is "whoever can reach this port," not "whoever clicks allow."** If you expose `oli-server` to anything beyond localhost — a container network, a reverse proxy, a chat platform integration — the profile loaded via `OLI_API_PROFILE` is the only thing standing between an inbound request and full tool access at that profile's permission ceiling. Treat the profile bound to the API server the way you'd treat a service account: give it the minimum tool surface the intended use case requires, not the default profile's full access. +- **REST (`/v1/chat/completions`)** is stateless and has no interactive channel, so it **auto-allows permission scopes for the current request.** Offline mode and dry-run gating from `AppConfig` still apply and are not bypassed. +- **WebSocket (`/v1/chat`)** has a live client, so it **prompts for permission by default** using the same `once` / `session` / `deny` choices as the TUI. The server sends a `permission_request` frame and waits for a `permission_response`; an absent or old client cannot silently auto-approve because the prompt times out (default 120 s) to **deny**. `api_server.permission_mode = "auto"` (`OLI_API_PERMISSION_MODE=auto`) restores the legacy auto-approve behaviour when a trusted UI/front-end has no permission handling. + +This still has a direct implication for any surface you place in front of the API server: **the API server's security boundary is "whoever can reach this port," not "whoever clicks allow."** If you expose `oli-server` to anything beyond localhost — a container network, a reverse proxy, a chat platform integration — the profile loaded via `OLI_API_PROFILE` is the only thing standing between an inbound REST request and full tool access at that profile's permission ceiling. Treat the profile bound to the API server the way you'd treat a service account: give it the minimum tool surface the intended use case requires, not the default profile's full access. For WebSocket clients, also keep `permission_mode = "prompt"` unless the front-end is fully trusted and deliberately opts into auto-approval. + +Because the server shares one process-private `Agent`, a pending WebSocket permission prompt holds the process-wide agent lock until the client answers or the timeout denies it. Concurrent REST/WebSocket turns queue behind that lock. Keep the permission timeout short for untrusted clients. ## Known limitations @@ -126,6 +131,8 @@ Stated plainly, so nobody discovers these the hard way: - **No egress/data-sensitivity control yet.** `agents.yaml` currently has no field governing what data is allowed to leave a given backend or agent. - **No audit log of cross-boundary data flow.** There is currently no way to retroactively answer "what did a remote backend ever see," beyond reading session history manually. - **Session permission grants are process-lifetime only.** They are not persisted, but they are also not scoped more narrowly than "the rest of this process" — a session-granted tool stays granted for every subsequent call in that TUI process, not just the current task. +- **Transformers backend cancellation is not instant.** Ctrl+I / WebSocket interrupt cancels the surrounding task, but the local Transformers backend's synchronous `TextIteratorStreamer` loop only observes cancellation after the current generation finishes. Ollama, OpenAI, and HuggingFace backends cancel promptly; shell subprocesses and dispatch fan-outs are killed/cancelled immediately. +- **API auto-approval can still be turned back on.** `api_server.permission_mode = "auto"` restores the pre-prompt behaviour for all WebSocket clients. Treat that setting as equivalent to exposing the port with no human gate. ## Verifying this yourself @@ -135,3 +142,4 @@ Because a permission system is only as trustworthy as its weakest untested path, 2. **Sensitive-file check:** point `read_file` at a `.env` file inside an approved workspace and confirm it still prompts, rather than silently succeeding because the path is "trusted." 3. **SSRF check:** attempt a `fetch` against `http://169.254.169.254` and confirm it's rejected, rather than assuming the guard covers it. 4. **API server exposure check:** confirm whatever profile `OLI_API_PROFILE` points to has a permission manifest appropriate for "anyone who can reach this port," not the default profile, before exposing the server beyond localhost. +5. **WebSocket permission check:** connect a client, ask it to write a file, and confirm a `permission_request` arrives and a missing/ignored reply resolves to `deny` after the configured timeout. Repeat with `OLI_API_PERMISSION_MODE=auto` only if you deliberately want auto-approval. diff --git a/oli_bot/agent.py b/oli_bot/agent.py index 299b282..e4e0cda 100644 --- a/oli_bot/agent.py +++ b/oli_bot/agent.py @@ -594,6 +594,7 @@ async def _run_dispatch_with_events( permission_enforcer=self.permission_enforcer, ) ) + getter: Optional[asyncio.Task] = None try: while not task.done() or not queue.empty(): getter = asyncio.create_task(queue.get()) @@ -602,6 +603,7 @@ async def _run_dispatch_with_events( ) if getter in done: event = getter.result() + getter = None if isinstance( event, ( @@ -620,8 +622,23 @@ async def _run_dispatch_with_events( await getter except (asyncio.CancelledError, Exception): pass + getter = None finally: self.mcp_manager.sub_agent_queue = None + # Cancellation (Ctrl+I / WebSocket interrupt) or an early + # ``aclose()`` must not leave the dispatch fan-out running. + if getter is not None and not getter.done(): + getter.cancel() + try: + await getter + except (asyncio.CancelledError, Exception): + pass + if not task.done(): + task.cancel() + try: + await task + except (asyncio.CancelledError, Exception): + pass result_holder[0] = task.result() async def _final_stream( diff --git a/oli_bot/api/harness.py b/oli_bot/api/harness.py index 1dce806..6feb5b4 100644 --- a/oli_bot/api/harness.py +++ b/oli_bot/api/harness.py @@ -6,9 +6,10 @@ """ import logging +from contextvars import ContextVar, Token from datetime import datetime, timezone from pathlib import Path -from typing import Any, Callable, List +from typing import Any, Callable, List, Optional from ..agent import Agent, AgentPool, run_agent_dispatch from ..backends import ModelBackend, create_model_backend @@ -69,10 +70,31 @@ def _build_agent(config: AppConfig, mode: str, profile: str) -> Agent: async def _api_confirm(description: str) -> str: - """Auto-allow every permission scope (API mode has no interactive prompt).""" + """Auto-allow every permission scope (REST mode has no interactive prompt).""" return "session" +# The active turn's confirm callback. The WebSocket endpoint sets this to its +# permission broker before running the agent; the dispatch handler resolves it +# lazily so sub-agents inherit the same prompt channel. REST leaves it unset +# and falls back to ``_api_confirm``. +_ACTIVE_CONFIRM: ContextVar[Optional[Callable[[str], Any]]] = ContextVar( + "oli_api_active_confirm", default=None +) + + +def set_active_confirm(callback: Optional[Callable[[str], Any]]) -> Token: + return _ACTIVE_CONFIRM.set(callback) + + +def reset_active_confirm(token: Token) -> None: + _ACTIVE_CONFIRM.reset(token) + + +def get_active_confirm() -> Callable[[str], Any]: + return _ACTIVE_CONFIRM.get() or _api_confirm + + # Note: the ``builtin__question`` tool needs an interactive human. The API # server never registers a question callback on ``BuiltinToolManager``, so a # call to it returns an error result telling the model to proceed on its own. @@ -138,7 +160,7 @@ async def _dispatch_tasks(tasks: List[dict]) -> str: pool=pool, runs=runs, tools=sub_tools, - confirm_callback=_api_confirm, + confirm_callback=get_active_confirm(), event_sink=agent.mcp_manager.sub_agent_queue, ) diff --git a/oli_bot/api/permissions.py b/oli_bot/api/permissions.py new file mode 100644 index 0000000..8978efe --- /dev/null +++ b/oli_bot/api/permissions.py @@ -0,0 +1,104 @@ +"""Interactive permission broker for the stateful WebSocket endpoint. + +The TUI blocks on a modal for tool permissions. REST requests have no +interactive channel, so they keep auto-approving (see ``harness._api_confirm``). +A WebSocket client *does* have a channel: this broker sends a +``permission_request`` frame and waits for the matching ``permission_response`` +action, mirroring the TUI's "allow once / allow for session / deny" choices. +""" + +from __future__ import annotations + +import asyncio +import logging +import uuid +from typing import Awaitable, Callable, Dict, Optional + +logger = logging.getLogger(__name__) + +VALID_DECISIONS = frozenset({"once", "session", "deny"}) + + +class PermissionBroker: + """Track in-flight permission prompts for one WebSocket connection.""" + + def __init__( + self, + send: Callable[[dict], Awaitable[None]], + timeout: float = 120.0, + ): + self._send = send + self._timeout = max(0.1, float(timeout)) + self._pending: Dict[str, asyncio.Future] = {} + + @property + def pending_count(self) -> int: + return len(self._pending) + + async def request(self, description: str) -> str: + """Send a prompt and await the client's decision. + + A timeout resolves to ``"deny"`` so an unresponsive or old client can + never silently keep the legacy auto-approve behaviour. + """ + request_id = f"perm-{uuid.uuid4().hex}" + future: asyncio.Future = asyncio.get_running_loop().create_future() + self._pending[request_id] = future + try: + await self._send( + { + "type": "permission_request", + "data": { + "id": request_id, + "description": description, + "options": ["once", "session", "deny"], + "default": "deny", + "timeout": self._timeout, + }, + } + ) + try: + return await asyncio.wait_for(future, self._timeout) + except asyncio.TimeoutError: + logger.info( + "Permission request %s timed out after %.1fs; denying", + request_id, + self._timeout, + ) + await self._send( + {"type": "permission_timeout", "data": {"id": request_id}} + ) + return "deny" + finally: + self._pending.pop(request_id, None) + + def resolve(self, request_id: str, decision: str) -> bool: + """Resolve a pending prompt. + + Returns ``False`` for unknown/expired ids. Raises ``ValueError`` for a + decision outside ``once``/``session``/``deny``. + """ + if decision not in VALID_DECISIONS: + raise ValueError( + f"Invalid permission decision {decision!r}; " + f"expected one of {sorted(VALID_DECISIONS)}" + ) + future = self._pending.get(request_id) + if future is None or future.done(): + return False + future.set_result(decision) + return True + + def cancel_all(self, decision: str = "deny") -> int: + """Resolve every pending prompt (used on interrupt/disconnect).""" + if decision not in VALID_DECISIONS: + decision = "deny" + cancelled = 0 + for future in list(self._pending.values()): + if not future.done(): + future.set_result(decision) + cancelled += 1 + return cancelled + + +__all__ = ["PermissionBroker", "VALID_DECISIONS"] diff --git a/oli_bot/api/routers/config.py b/oli_bot/api/routers/config.py index d156e7b..40372dc 100644 --- a/oli_bot/api/routers/config.py +++ b/oli_bot/api/routers/config.py @@ -4,74 +4,14 @@ from fastapi import APIRouter, HTTPException, Request -from ...settings import SettingsManager +from ...settings import SETTINGS_FLAT_MAP, SettingsManager router = APIRouter(prefix="/v1/config", tags=["config"]) # Mapping between the browser UI's flat ``OliConfig`` keys and the nested -# settings.json format the ``SettingsManager`` persists. -_FLAT_TO_NESTED = { - "backend": ("", "backend"), - "openai_api_key": ("openai", "api_key"), - "openai_base_url": ("openai", "base_url"), - "openai_model": ("openai", "large_model"), - "openai_small_model": ("openai", "small_model"), - "openai_vision_style": ("openai", "vision_style"), - "openai_optional_headers": ("openai", "optional_headers"), - "openai_responses_enabled": ("openai", "responses_enabled"), - "ollama_base_url": ("ollama", "base_url"), - "ollama_model": ("ollama", "large_model"), - "ollama_small_model": ("ollama", "small_model"), - "ollama_context_window": ("ollama", "context_window"), - "huggingface_base_url": ("huggingface", "base_url"), - "huggingface_api_key": ("huggingface", "api_key"), - "huggingface_model": ("huggingface", "large_model"), - "huggingface_small_model": ("huggingface", "small_model"), - "huggingface_remote": ("huggingface", "remote"), - "transformers_model": ("transformers", "model"), - "transformers_small_model": ("transformers", "small_model"), - "transformers_device": ("transformers", "device"), - "transformers_dtype": ("transformers", "dtype"), - "transformers_is_multi_model": ("transformers", "is_multi_model"), - "voice_whisper_model": ("voice", "whisper_model"), - "voice_piper_model": ("voice", "piper_model"), - "voice_sample_rate": ("voice", "sample_rate"), - "voice_frame_duration_ms": ("voice", "frame_duration_ms"), - "voice_vad_aggressiveness": ("voice", "vad_aggressiveness"), - "voice_silence_timeout_ms": ("voice", "silence_timeout_ms"), - "voice_max_record_seconds": ("voice", "max_record_seconds"), - "max_tokens": ("model_params", "max_tokens"), - "temperature": ("model_params", "temperature"), - "max_retries": ("model_params", "max_retries"), - "retry_delay": ("model_params", "retry_delay"), - "request_timeout": ("model_params", "request_timeout"), - "max_messages": ("model_params", "max_messages"), - "max_tool_iterations": ("model_params", "max_tool_iterations"), - "stream_timeout": ("model_params", "stream_timeout"), - "first_chunk_timeout": ("model_params", "first_chunk_timeout"), - "model_filters": ("model_params", "model_filters"), - "truncation_max_chars_small": ("model_params", "truncation_max_chars_small"), - "truncation_max_chars_large": ("model_params", "truncation_max_chars_large"), - "max_write_chars": ("model_params", "max_write_chars"), - "dry_run": ("model_params", "dry_run"), - "offline_mode": ("model_params", "offline_mode"), - "use_oauth": ("model_params", "use_oauth"), - "use_agent_pool": ("model_params", "use_agent_pool"), - "agent_pool_size": ("model_params", "agent_pool_size"), - "agents_yaml": ("model_params", "agents_yaml"), - "log_level": ("logging", "log_level"), - "log_file": ("logging", "log_file"), - "profiles_dir": ("paths", "profiles_dir"), - "logs_dir": ("paths", "logs_dir"), - "default_profile": ("session", "default_profile"), - "session_auto_save": ("session", "auto_save"), - "session_resume_prompt": ("session", "resume_prompt"), - "workspace_max_workspaces": ("workspace", "max_workspaces"), - "api_host": ("api_server", "host"), - "api_port": ("api_server", "port"), - "api_profile": ("api_server", "profile"), - "api_mode": ("api_server", "mode"), -} +# settings.json format the ``SettingsManager`` persists. Kept as a module +# alias so existing imports/tests continue to work. +_FLAT_TO_NESTED = SETTINGS_FLAT_MAP def _flat_to_nested(flat: Dict[str, Any], settings: Dict[str, Any]) -> Dict[str, Any]: diff --git a/oli_bot/api/routers/ws.py b/oli_bot/api/routers/ws.py index 03dc95b..b6011fb 100644 --- a/oli_bot/api/routers/ws.py +++ b/oli_bot/api/routers/ws.py @@ -3,15 +3,20 @@ import asyncio import json import logging -from typing import Any, List +from typing import Any, Awaitable, Callable, List, Optional from fastapi import APIRouter, WebSocket, WebSocketDisconnect from ..attachments import attachment_note, save_attachments from ..convert import _event_to_frame -from ..harness import _api_confirm +from ..harness import ( + _api_confirm, + reset_active_confirm, + set_active_confirm, +) +from ..permissions import PermissionBroker from ...activity import attach_turn_activity -from ...models import Done, Message, UsageEvent +from ...models import AssistantResponse, Done, Message, StreamChunk, UsageEvent from ..runner import _resolve_tools from ..session_service import ( _load_conversation, @@ -25,96 +30,124 @@ async def _run_turn( - websocket: WebSocket, agent: Any, lock: Any, store: Any, messages: List[Message], requested_session: Any, connection_session_id: str, -) -> None: + user_index: int, + confirm_callback: Callable[[str], Any], + send: Callable[[dict], Awaitable[None]], +) -> str: """Execute one agent turn under the process-wide agent lock. - Extracted from ``websocket_chat`` so the per-session serialization can - wrap load->run->save as a single critical section without inflating the - surrounding function's indentation. + The caller appends the user message at ``user_index`` before starting + this coroutine. On cancellation the partial turn is rolled back so an + interrupted message behaves exactly like the TUI's Ctrl+I rollback. """ async with lock: + token = set_active_confirm(confirm_callback) try: - tools = await _resolve_tools(agent) - except Exception as e: - logger.warning("Failed to list tools: %s", e) - tools = None - total_tokens = 0 - tokens_estimated = False - root_todos: List[dict] = [] - turn_start = len(messages) - try: - async for event in agent.process( - messages, tools=tools, confirm_callback=_api_confirm - ): - await websocket.send_json(_event_to_frame(event)) - while agent.mcp_manager.pending_todos: - todo_data = agent.mcp_manager.pending_todos.pop(0) - if "task_id" not in todo_data: - root_todos = list(todo_data.get("todos") or []) - await websocket.send_json( - {"type": "todo", "data": todo_data} - ) - if isinstance(event, UsageEvent): - total_tokens += event.usage.total_tokens - tokens_estimated = tokens_estimated or event.usage.estimated - if isinstance(event, Done): - if event.full_text: - messages.append( - Message(role="assistant", content=event.full_text) - ) - attach_turn_activity(messages, turn_start, event.activity) - if requested_session and store is not None: - try: - new_id = _persist_session( - store, - agent, - connection_session_id, - messages, - total_tokens, - tokens_estimated, - root_todos, + try: + tools = await _resolve_tools(agent) + except Exception as e: + logger.warning("Failed to list tools: %s", e) + tools = None + total_tokens = 0 + tokens_estimated = False + root_todos: List[dict] = [] + partial_text = "" + turn_start = user_index + 1 + try: + async for event in agent.process( + messages, tools=tools, confirm_callback=confirm_callback + ): + await send(_event_to_frame(event)) + while agent.mcp_manager.pending_todos: + todo_data = agent.mcp_manager.pending_todos.pop(0) + if "task_id" not in todo_data: + root_todos = list(todo_data.get("todos") or []) + await send({"type": "todo", "data": todo_data}) + if isinstance(event, UsageEvent): + total_tokens += event.usage.total_tokens + tokens_estimated = tokens_estimated or event.usage.estimated + elif isinstance(event, StreamChunk): + partial_text += event.text + elif isinstance(event, AssistantResponse): + # A new round started; only the latest round's text is + # meaningful if the turn is interrupted mid-stream. + partial_text = "" + elif isinstance(event, Done): + if event.full_text: + messages.append( + Message(role="assistant", content=event.full_text) ) - if new_id != connection_session_id: - connection_session_id = new_id - await websocket.send_json( - _session_created_frame(store, new_id) + attach_turn_activity(messages, turn_start, event.activity) + if requested_session and store is not None: + try: + new_id = _persist_session( + store, + agent, + connection_session_id, + messages, + total_tokens, + tokens_estimated, + root_todos, ) - except Exception as e: - logger.warning( - "Failed to persist session %s: %s", - connection_session_id, - e, - ) - except WebSocketDisconnect: - raise - except Exception as e: - logger.exception("Agent process failed over websocket: %s", e) - await websocket.send_json( - {"type": "error", "data": {"message": str(e)}} - ) - # Publish the (possibly rotated) session id back to the caller via - # websocket.state so the outer loop can pick it up for the next turn. - websocket.state._connection_session_id = connection_session_id + if new_id != connection_session_id: + connection_session_id = new_id + await send(_session_created_frame(store, new_id)) + except Exception as e: + logger.warning( + "Failed to persist session %s: %s", + connection_session_id, + e, + ) + except asyncio.CancelledError: + # Roll the interrupted turn back out of the in-memory history + # (the session file already holds the pre-turn state, so no + # persistence is needed). Then tell the client the turn ended + # so it can release its "generating" state. + if len(messages) > user_index: + del messages[user_index:] + await send( + { + "type": "interrupted", + "data": {"full_text": partial_text}, + } + ) + await send( + { + "type": "done", + "data": {"full_text": partial_text, "interrupted": True}, + } + ) + raise + except Exception as e: + logger.exception("Agent process failed over websocket: %s", e) + await send({"type": "error", "data": {"message": str(e)}}) + finally: + reset_active_confirm(token) + return connection_session_id @router.websocket("/v1/chat") async def websocket_chat(websocket: WebSocket) -> None: """Stateful WebSocket chat endpoint. - The client may send ``{"content": ...}`` for a turn or - ``{"action": "clear"}`` to reset the connection history. When a - ``session_id`` is included, the conversation is persisted to the shared - ``ConversationStore`` (under ``SESSION_SERVER``) after every turn, - mirroring the TUI's per-turn save. Missing or corrupt session files are + The client may send ``{"content": ...}`` for a turn, + ``{"action": "clear"}`` to reset the connection history, or + ``{"action": "interrupt"}`` to cancel the in-flight turn. Permission + prompts are delivered as ``permission_request`` frames; the client + answers with ``{"action": "permission_response", "id": ..., "decision": + "once"|"session"|"deny"}``. ``api_server.permission_mode = "auto"`` + restores the legacy auto-approve behaviour. + + When a ``session_id`` is included, the conversation is persisted to the + shared ``ConversationStore`` (under ``SESSION_SERVER``) after every turn, + mirroring the TUI's per-turn save. Missing or corrupt session files are recreated and the client is notified via a ``session_created`` frame. - ``{"action": "clear", "session_id": ...}`` wipes the persisted session too. Runs are serialized on ``app.state.lock`` like the REST endpoints. """ await websocket.accept() @@ -134,26 +167,184 @@ def _session_lock(sid: str) -> asyncio.Lock: session_locks[sid] = existing return existing + config = getattr(websocket.app.state, "config", None) or getattr( + agent, "config", None + ) + permission_mode = str( + getattr(config, "api_permission_mode", "prompt") or "prompt" + ).lower() + permission_timeout = float( + getattr(config, "api_permission_timeout", 120.0) or 120.0 + ) + + send_lock = asyncio.Lock() + + async def send(frame: dict) -> None: + # A send racing a disconnect must not abort the agent turn; the + # receive loop observes the disconnect and cancels the turn. + async with send_lock: + try: + await websocket.send_json(frame) + except (WebSocketDisconnect, RuntimeError) as exc: + logger.debug("WebSocket send failed: %s", exc) + + broker = PermissionBroker(send, timeout=permission_timeout) + confirm_callback = _api_confirm if permission_mode == "auto" else broker.request + messages: List[Message] = [] connection_session_id = "" + turn_task: Optional[asyncio.Task] = None + + def _turn_active() -> bool: + return turn_task is not None and not turn_task.done() + + async def _handle_turn(data: dict) -> None: + nonlocal connection_session_id, messages + requested_session = data.get("session_id") + # Acquire a per-session lock so concurrent WebSocket connections on + # the same session_id serialize their load->run->save turns + # (last-write-wins race would otherwise clobber history). Fall back + # to a no-op lock when no session is requested. + sess_lock = ( + _session_lock(str(requested_session)) + if requested_session + else asyncio.Lock() + ) + async with sess_lock: + if requested_session and store is not None: + try: + connection_session_id, messages = _load_conversation( + store, agent, str(requested_session) + ) + if connection_session_id != str(requested_session): + await send(_session_created_frame(store, connection_session_id)) + except Exception as e: + logger.exception("Failed to load session %s", requested_session) + await send({"type": "error", "data": {"message": str(e)}}) + return + + content = data.get("content") + attachments = data.get("attachments") or [] + text = str(content) if content else "" + user_images = None + if attachments: + try: + images, saved = save_attachments( + config, connection_session_id, attachments + ) + except Exception as e: + logger.warning("Failed to save attachments: %s", e) + await send({"type": "error", "data": {"message": str(e)}}) + return + note = attachment_note(saved, images) + if note: + text = f"{text}\n\n{note}" if text.strip() else note + user_images = images or None + if not text.strip(): + await send({"type": "error", "data": {"message": "Empty message"}}) + return + + user_index = len(messages) + messages.append(Message(role="user", content=text, images=user_images)) + connection_session_id = await _run_turn( + agent, + lock, + store, + messages, + requested_session, + connection_session_id, + user_index, + confirm_callback, + send, + ) + + async def _turn_wrapper(data: dict) -> None: + nonlocal turn_task + try: + await _handle_turn(data) + except asyncio.CancelledError: + raise + except Exception as e: + logger.exception("WebSocket turn failed: %s", e) + await send({"type": "error", "data": {"message": str(e)}}) + finally: + if turn_task is asyncio.current_task(): + turn_task = None + try: - await websocket.send_json({"type": "connected", "data": {}}) + await send({"type": "connected", "data": {}}) while True: raw = await websocket.receive_text() try: data = json.loads(raw) except json.JSONDecodeError: - await websocket.send_json( + await send( {"type": "error", "data": {"message": "Invalid JSON payload"}} ) continue if not isinstance(data, dict): - await websocket.send_json( + await send( {"type": "error", "data": {"message": "Expected a JSON object"}} ) continue - if data.get("action") == "clear": + action = data.get("action") + if action == "permission_response": + request_id = data.get("id") + decision = data.get("decision") + if not isinstance(request_id, str) or not isinstance(decision, str): + await send( + { + "type": "error", + "data": { + "message": ( + "permission_response requires string 'id' " + "and 'decision'" + ) + }, + } + ) + continue + try: + resolved = broker.resolve(request_id, decision) + except ValueError as e: + await send({"type": "error", "data": {"message": str(e)}}) + continue + if not resolved: + await send( + { + "type": "error", + "data": { + "message": ("Unknown or expired permission request id") + }, + } + ) + continue + + if action == "interrupt": + if not _turn_active(): + await send( + { + "type": "error", + "data": {"message": "No active turn to interrupt"}, + } + ) + continue + broker.cancel_all("deny") + assert turn_task is not None + turn_task.cancel() + await asyncio.gather(turn_task, return_exceptions=True) + continue + + if action == "clear": + if _turn_active(): + await send( + { + "type": "error", + "data": {"message": "Cannot clear while a turn is running"}, + } + ) + continue messages = [] requested = data.get("session_id") if requested: @@ -168,96 +359,43 @@ def _session_lock(sid: str) -> asyncio.Lock: new_id = connection_session_id if new_id != connection_session_id: connection_session_id = new_id - await websocket.send_json( - _session_created_frame(store, new_id) - ) - await websocket.send_json({"type": "cleared", "data": {}}) + await send(_session_created_frame(store, new_id)) + await send({"type": "cleared", "data": {}}) continue - content = data.get("content") attachments = data.get("attachments") or [] if not isinstance(attachments, list): - await websocket.send_json( - {"type": "error", "data": {"message": "attachments must be a list"}} + await send( + { + "type": "error", + "data": {"message": "attachments must be a list"}, + } ) continue + content = data.get("content") has_text = bool(content and str(content).strip()) if not has_text and not attachments: - await websocket.send_json( - {"type": "error", "data": {"message": "Empty message"}} - ) + await send({"type": "error", "data": {"message": "Empty message"}}) continue - - requested_session = data.get("session_id") - # Acquire a per-session lock so concurrent WebSocket connections - # on the same session_id serialize their load->run->save turns - # (last-write-wins race would otherwise clobber history). - # Fall back to a no-op lock when no session is requested. - sess_lock = ( - _session_lock(str(requested_session)) - if requested_session - else asyncio.Lock() - ) - async with sess_lock: - if requested_session and store is not None: - try: - connection_session_id, messages = _load_conversation( - store, agent, str(requested_session) - ) - if connection_session_id != str(requested_session): - await websocket.send_json( - _session_created_frame(store, connection_session_id) + if _turn_active(): + await send( + { + "type": "error", + "data": { + "message": ( + "A turn is already running; send " + '{"action": "interrupt"} first' ) - except Exception as e: - logger.exception("Failed to load session %s", requested_session) - await websocket.send_json( - {"type": "error", "data": {"message": str(e)}} - ) - continue - - text = str(content) if content else "" - user_images = None - if attachments: - config = getattr(websocket.app.state, "config", None) or getattr( - agent, "config", None - ) - try: - images, saved = save_attachments( - config, connection_session_id, attachments - ) - except Exception as e: - logger.warning("Failed to save attachments: %s", e) - await websocket.send_json( - {"type": "error", "data": {"message": str(e)}} - ) - continue - note = attachment_note(saved, images) - if note: - text = f"{text}\n\n{note}" if text.strip() else note - user_images = images or None - if not text.strip(): - await websocket.send_json( - {"type": "error", "data": {"message": "Empty message"}} - ) - continue - - messages.append( - Message(role="user", content=text, images=user_images) - ) - - await _run_turn( - websocket, - agent, - lock, - store, - messages, - requested_session, - connection_session_id, - ) - # ``_run_turn`` may have swapped in a new session ID (e.g. - # on first persist); pick it up for subsequent turns. - connection_session_id = getattr( - websocket.state, "_connection_session_id", connection_session_id + }, + } ) + continue + turn_task = asyncio.create_task(_turn_wrapper(data)) except WebSocketDisconnect: logger.debug("WebSocket client disconnected from /v1/chat") + finally: + broker.cancel_all("deny") + if _turn_active(): + assert turn_task is not None + turn_task.cancel() + await asyncio.gather(turn_task, return_exceptions=True) diff --git a/oli_bot/chat.py b/oli_bot/chat.py index 9a63426..83125fd 100644 --- a/oli_bot/chat.py +++ b/oli_bot/chat.py @@ -474,6 +474,13 @@ def __init__( self.workspace_manager = WorkspaceManager() self.current_session_id: str = "" self._last_tool_widget: Static | None = None + # Turn interruption (Ctrl+I): the active worker, the turn it belongs + # to, and the user message that must be restored to the input box. + self._response_worker: object | None = None + self._turn_id: int = 0 + self._turn_user_index: int | None = None + self._turn_user_text: str = "" + self._interrupt_requested: int | None = None self._sub_runs: list[SubAgentRun] = [] self._sub_tree_nodes: dict = {} self._sub_todo_nodes: dict[str, list] = {} # task_id -> list of TreeNode @@ -755,7 +762,10 @@ def update_header(self) -> None: status = Table.grid(expand=True) status.add_column(ratio=1) status.add_column(justify="right") - hints = "[dim]^Q[/dim] quit [dim]^L[/dim] clear [dim]^Y[/dim] copy" + hints = ( + "[dim]^Q[/dim] quit [dim]^I[/dim] interrupt " + "[dim]^L[/dim] clear [dim]^Y[/dim] copy" + ) info = ( f"{self._status_badges()} {model_part} " f"[dim]:: {self.agent.profile_name}{server_part}[/dim]" @@ -816,6 +826,19 @@ def on_list_view_selected(self, event: ListView.Selected) -> None: self._accept_command_suggestion(self._command_matches[idx]) def on_key(self, event: events.Key) -> None: + # Ctrl+I is delivered as "tab" by terminals that cannot distinguish + # the two (and Textual aliases it), so accept both while generating. + # Escape is the universal fallback. Autocomplete owns Tab/Escape when + # its suggestion list is visible. + if ( + not self._command_matches + and self._response_active() + and event.key in ("ctrl+i", "tab", "escape") + ): + self.action_interrupt() + event.stop() + event.prevent_default() + return if not self._command_matches: return suggestions = self.query_one("#command-suggestions", ListView) @@ -919,6 +942,7 @@ def _handle_command(self, text: str) -> None: " [bold]/home[/bold] — go to home screen\n" " [bold]/help[/bold] — show this message\n" " [bold]Ctrl+Q[/bold] — quit\n" + " [bold]Ctrl+I[/bold] — interrupt the current response\n" " [bold]Ctrl+L[/bold] — clear", ) elif cmd == "/clear": @@ -2236,6 +2260,69 @@ def action_copy_last_message(self) -> None: return self.notify("No message to copy", severity="warning", timeout=2) + def _response_active(self) -> bool: + worker = self._response_worker + if worker is not None and not getattr(worker, "is_finished", True): + return True + return bool(getattr(self.agent, "_generating", False)) + + def _rollback_interrupted_turn(self, turn_id: int) -> bool: + """Drop the interrupted turn's user message and persist the rollback. + + Guarded by the turn id so a superseding turn — which may have taken + the same message index — is never clobbered. Returns whether anything + was removed. + """ + if self._turn_id != turn_id: + return False + user_index = self._turn_user_index + if ( + user_index is None + or not (0 <= user_index < len(self.messages)) + or self.messages[user_index].role != "user" + or self.messages[user_index].content != self._turn_user_text + ): + return False + del self.messages[user_index:] + self._refresh_context_tokens() + self._save_session() + return True + + def action_interrupt(self) -> None: + """Stop the in-flight response and put the prompt back in the input.""" + if not self._response_active(): + self.notify("No response to interrupt", severity="warning", timeout=2) + return + + turn_id = self._turn_id + user_text = self._turn_user_text + self._interrupt_requested = turn_id + + # Dismiss any modal the turn is blocked on so the permission/question + # tool can unwind instead of keeping the worker alive. + screen = self.screen + if isinstance(screen, PermissionScreen): + screen.dismiss("deny") + elif isinstance(screen, QuestionScreen): + screen.dismiss(None) + + worker = self._response_worker + if worker is not None: + try: + worker.cancel() + except Exception: + logger.warning("Failed to cancel response worker", exc_info=True) + + self._rollback_interrupted_turn(turn_id) + + if user_text: + chat_input = self.query_one("#chat-input", Input) + chat_input.value = user_text + chat_input.disabled = False + chat_input.focus() + + self.notify("Interrupted — prompt restored", timeout=2) + # --- Message handling --- def _role_color(self, role: str) -> str: @@ -2405,6 +2492,10 @@ async def _run_voice_loop(self) -> None: def _handle_user_message(self, text: str) -> None: ts = datetime.now(timezone.utc).isoformat() + self._turn_id += 1 + self._turn_user_index = len(self.messages) + self._turn_user_text = text + self._interrupt_requested = None self.messages.append(Message(role="user", content=text, timestamp=ts)) self._refresh_context_tokens() chat_log = self.query_one("#chat-log") @@ -2418,10 +2509,11 @@ def _handle_user_message(self, text: str) -> None: static.plain_text = text chat_log.mount(static) chat_log.scroll_end(animate=False) - self._generate_response() + self._response_worker = self._generate_response() @work(exclusive=True) async def _generate_response(self) -> None: + turn_id = self._turn_id input_widget = self.query_one("#chat-input") input_widget.disabled = True @@ -2509,10 +2601,13 @@ def stop_spinner() -> None: await start_spinner() turn_start = len(self.messages) + agen = self.agent.process( + self.messages, tools=tools, confirm_callback=self._permission_callback + ) try: - async for event in self.agent.process( - self.messages, tools=tools, confirm_callback=self._permission_callback - ): + async for event in agen: + if self._interrupt_requested == turn_id: + break match event: case ToolCallExecuting(name, parameters): stop_spinner() @@ -2559,7 +2654,10 @@ def stop_spinner() -> None: updated += f"\n [dim]{result_text}[/dim]" widget.update(updated) widget.plain_text = updated + self._last_tool_widget = None case AssistantResponse(content): + if self._interrupt_requested == turn_id: + break stop_spinner() if think_widget is not None: think_widget.title = "💭 Thinking" @@ -2660,6 +2758,8 @@ def stop_spinner() -> None: self.context_estimated = estimated self.update_header() case AgentDone(full_text, activity): + if self._interrupt_requested == turn_id: + break stop_spinner() if think_widget is not None: think_widget.title = "💭 Thinking" @@ -2668,6 +2768,8 @@ def stop_spinner() -> None: msg_widget = Static(classes="message") await chat_log.mount(msg_widget) chat_log.scroll_end(animate=False) + if self._interrupt_requested == turn_id: + break full_response = full_text if not full_response and not _had_error: # Only show the generic fallback when no more @@ -2723,6 +2825,44 @@ def stop_spinner() -> None: self.messages = sanitize_tool_history(pruned) self._refresh_context_tokens() finally: + try: + await agen.aclose() + except (RuntimeError, asyncio.CancelledError): + logger.debug("Async generator close after interrupt failed") + if self._interrupt_requested == turn_id: + stop_spinner() + if think_widget is not None: + think_widget.title = "💭 Thinking (interrupted)" + think_widget.collapsed = True + if msg_widget is not None: + try: + assistant_color = self._role_color("Assistant") + title = ( + f"[{assistant_color}]" + f"{self._role_icon('Assistant')} Assistant " + f"{ICON_SUCCESS} (interrupted)[/{assistant_color}]" + ) + body = Markdown( + (full_response + "\n\n*— interrupted —*") + if full_response + else "*— interrupted —*" + ) + msg_widget.update(self._flat(body, title)) + msg_widget.plain_text = full_response + except Exception: + logger.warning("Failed to mark response as interrupted") + if self._last_tool_widget is not None: + try: + base = getattr( + self._last_tool_widget, "_tool_base", "tool call" + ) + self._last_tool_widget.update( + f"[yellow]◐[/yellow] {base} [dim]· interrupted[/dim]" + ) + except Exception: + logger.warning("Failed to mark tool call as interrupted") + self._last_tool_widget = None + self._interrupt_requested = None self._save_session() stop_spinner() input_widget.disabled = False diff --git a/oli_bot/config.py b/oli_bot/config.py index 35207c1..50e40c8 100644 --- a/oli_bot/config.py +++ b/oli_bot/config.py @@ -154,6 +154,11 @@ class AppConfig(BaseSettings): api_port: int = Field(default=9734) api_profile: str = Field(default="default") api_mode: str = Field(default="agent") + # WebSocket permission policy: "prompt" asks connected UIs, "auto" keeps + # the legacy auto-approve behaviour. REST endpoints always auto-approve. + api_permission_mode: str = Field(default="prompt") + # Seconds to wait for a WebSocket permission reply before denying. + api_permission_timeout: float = Field(default=120.0) # Default profile loaded on TUI startup (overridden by --profile CLI flag) default_profile: str = Field(default="default") diff --git a/oli_bot/screens/config_screen.py b/oli_bot/screens/config_screen.py index 5c67c79..24734df 100644 --- a/oli_bot/screens/config_screen.py +++ b/oli_bot/screens/config_screen.py @@ -7,6 +7,74 @@ from textual.screen import ModalScreen from textual.widgets import Button, Checkbox, Input, Label, RadioButton, RadioSet +# Flat setting key -> widget selector. Kept in lockstep with +# ``oli_bot.settings.SETTINGS_FLAT_MAP`` by a unit test so a new setting +# cannot ship without a control. +CONFIG_FIELDS: dict[str, str] = { + "backend": "#config-backend", + "openai_api_key": "#cfg-openai-api-key", + "openai_base_url": "#cfg-openai-base-url", + "openai_model": "#cfg-openai-large-model", + "openai_small_model": "#cfg-openai-small-model", + "openai_vision_style": "#cfg-openai-vision-style", + "openai_optional_headers": "#cfg-openai-optional-headers", + "openai_responses_enabled": "#cfg-openai-responses", + "ollama_base_url": "#cfg-ollama-base-url", + "ollama_model": "#cfg-ollama-large-model", + "ollama_small_model": "#cfg-ollama-small-model", + "ollama_context_window": "#cfg-ollama-ctx-size", + "huggingface_base_url": "#cfg-hf-base-url", + "huggingface_api_key": "#cfg-hf-api-key", + "huggingface_model": "#cfg-hf-large-model", + "huggingface_small_model": "#cfg-hf-small-model", + "huggingface_remote": "#cfg-hf-remote", + "transformers_model": "#cfg-tr-model", + "transformers_small_model": "#cfg-tr-small-model", + "transformers_device": "#cfg-tr-device", + "transformers_dtype": "#cfg-tr-dtype", + "transformers_is_multi_model": "#cfg-tr-is-multi-model", + "voice_whisper_model": "#cfg-voice-whisper-model", + "voice_piper_model": "#cfg-voice-piper-model", + "voice_sample_rate": "#cfg-voice-sample-rate", + "voice_frame_duration_ms": "#cfg-voice-frame-duration-ms", + "voice_vad_aggressiveness": "#cfg-voice-vad-aggressiveness", + "voice_silence_timeout_ms": "#cfg-voice-silence-timeout-ms", + "voice_max_record_seconds": "#cfg-voice-max-record-seconds", + "max_tokens": "#cfg-max-tokens", + "temperature": "#cfg-temperature", + "max_retries": "#cfg-max-retries", + "retry_delay": "#cfg-retry-delay", + "request_timeout": "#cfg-request-timeout", + "max_messages": "#cfg-max-messages", + "max_tool_iterations": "#cfg-max-tool-iterations", + "stream_timeout": "#cfg-stream-timeout", + "first_chunk_timeout": "#cfg-first-chunk-timeout", + "model_filters": "#cfg-model-filters", + "truncation_max_chars_small": "#cfg-truncation-small", + "truncation_max_chars_large": "#cfg-truncation-large", + "max_write_chars": "#cfg-max-write-chars", + "dry_run": "#cfg-dry-run", + "offline_mode": "#cfg-offline-mode", + "use_oauth": "#cfg-use-oauth", + "use_agent_pool": "#cfg-use-agent-pool", + "agent_pool_size": "#cfg-agent-pool-size", + "agents_yaml": "#cfg-agents-yaml", + "log_level": "#cfg-log-level", + "log_file": "#cfg-log-file", + "profiles_dir": "#cfg-profiles-dir", + "logs_dir": "#cfg-logs-dir", + "default_profile": "#cfg-default-profile", + "session_auto_save": "#cfg-auto-save", + "session_resume_prompt": "#cfg-resume-prompt", + "workspace_max_workspaces": "#cfg-max-workspaces", + "api_host": "#cfg-api-host", + "api_port": "#cfg-api-port", + "api_profile": "#cfg-api-profile", + "api_mode": "#cfg-api-mode", + "api_permission_mode": "#cfg-api-permission-mode", + "api_permission_timeout": "#cfg-api-permission-timeout", +} + class ConfigScreen(ModalScreen[dict | None]): """Configure model params, backend, models, workspace, and session settings.""" @@ -173,6 +241,11 @@ def compose(self) -> ComposeResult: classes="config-input", value=json.dumps(op.get("optional_headers", {})), ) + yield Checkbox( + "Use the OpenAI Responses API (/v1/responses)", + id="cfg-openai-responses", + value=bool(op.get("responses_enabled", False)), + ) yield Label( "Ollama", id="ollama-section-title", classes="section-title" @@ -322,10 +395,16 @@ def compose(self) -> ComposeResult: value=str(mp.get("max_tool_iterations", 25)), ) yield Input( - placeholder=f"Stream timeout ({mp.get('stream_timeout', 240.0)})", + placeholder=f"Stream timeout ({mp.get('stream_timeout', 300.0)})", id="cfg-stream-timeout", classes="config-input", - value=str(mp.get("stream_timeout", 240.0)), + value=str(mp.get("stream_timeout", 300.0)), + ) + yield Input( + placeholder=f"First chunk timeout ({mp.get('first_chunk_timeout', 600.0)})", + id="cfg-first-chunk-timeout", + classes="config-input", + value=str(mp.get("first_chunk_timeout", 600.0)), ) yield Input( placeholder="Model filters (comma-sep, e.g. :cloud,-cloud)", @@ -482,6 +561,21 @@ def compose(self) -> ComposeResult: classes="config-input", value=api.get("mode", "agent"), ) + yield Label( + "Permission prompt mode (prompt or auto)", classes="config-label" + ) + yield Input( + placeholder=f"Permission mode ({api.get('permission_mode', 'prompt')})", + id="cfg-api-permission-mode", + classes="config-input", + value=api.get("permission_mode", "prompt"), + ) + yield Input( + placeholder=f"Permission timeout seconds ({api.get('permission_timeout', 120.0)})", + id="cfg-api-permission-timeout", + classes="config-input", + value=str(api.get("permission_timeout", 120.0)), + ) paths = s.get("paths", {}) yield Label("Paths", classes="section-title") @@ -589,6 +683,7 @@ def _save(self) -> None: "small_model": self._val("#cfg-openai-small-model"), "vision_style": vision_style, "optional_headers": self._json("#cfg-openai-optional-headers"), + "responses_enabled": self._bool("#cfg-openai-responses"), }, "ollama": { "base_url": self._val("#cfg-ollama-base-url"), @@ -618,7 +713,8 @@ def _save(self) -> None: "request_timeout": self._float("#cfg-request-timeout", 30.0), "max_messages": self._int("#cfg-max-messages", 100), "max_tool_iterations": self._int("#cfg-max-tool-iterations", 25), - "stream_timeout": self._float("#cfg-stream-timeout", 240.0), + "stream_timeout": self._float("#cfg-stream-timeout", 300.0), + "first_chunk_timeout": self._float("#cfg-first-chunk-timeout", 600.0), "model_filters": self._val("#cfg-model-filters"), "truncation_max_chars_small": self._int("#cfg-truncation-small", 4000), "truncation_max_chars_large": self._int( @@ -641,6 +737,8 @@ def _save(self) -> None: "port": self._int("#cfg-api-port", 9734), "profile": self._val("#cfg-api-profile"), "mode": self._val("#cfg-api-mode"), + "permission_mode": self._val("#cfg-api-permission-mode"), + "permission_timeout": self._float("#cfg-api-permission-timeout", 120.0), }, "paths": { "profiles_dir": self._val("#cfg-profiles-dir"), @@ -664,6 +762,17 @@ def _save(self) -> None: "default_profile": self._val("#cfg-default-profile"), }, } + # Preserve settings this form does not know about (e.g. a newer + # settings.json written by another client) instead of silently + # dropping them when the user clicks Save. + for section, values in self._settings.items(): + if isinstance(values, dict): + target = settings.setdefault(section, {}) + if isinstance(target, dict): + for key, value in values.items(): + target.setdefault(key, value) + else: + settings.setdefault(section, values) self.dismiss(settings) def _val(self, selector: str) -> str: diff --git a/oli_bot/settings.py b/oli_bot/settings.py index c9b7b31..f005c8d 100644 --- a/oli_bot/settings.py +++ b/oli_bot/settings.py @@ -71,6 +71,10 @@ "port": 9734, "profile": "default", "mode": "agent", + # WebSocket permission policy: "prompt" asks the UI, "auto" keeps the + # legacy auto-approve behaviour. REST endpoints always auto-approve. + "permission_mode": "prompt", + "permission_timeout": 120.0, }, "paths": { "profiles_dir": "profiles", @@ -95,6 +99,74 @@ }, } +# Single source of truth for the flat -> nested settings.json mapping shared by +# the browser UI's ``/v1/config`` routes and the TUI ``/config`` screen. +# ``(group, key)`` with an empty group denotes a top-level setting. +SETTINGS_FLAT_MAP: dict[str, tuple[str, str]] = { + "backend": ("", "backend"), + "openai_api_key": ("openai", "api_key"), + "openai_base_url": ("openai", "base_url"), + "openai_model": ("openai", "large_model"), + "openai_small_model": ("openai", "small_model"), + "openai_vision_style": ("openai", "vision_style"), + "openai_optional_headers": ("openai", "optional_headers"), + "openai_responses_enabled": ("openai", "responses_enabled"), + "ollama_base_url": ("ollama", "base_url"), + "ollama_model": ("ollama", "large_model"), + "ollama_small_model": ("ollama", "small_model"), + "ollama_context_window": ("ollama", "context_window"), + "huggingface_base_url": ("huggingface", "base_url"), + "huggingface_api_key": ("huggingface", "api_key"), + "huggingface_model": ("huggingface", "large_model"), + "huggingface_small_model": ("huggingface", "small_model"), + "huggingface_remote": ("huggingface", "remote"), + "transformers_model": ("transformers", "model"), + "transformers_small_model": ("transformers", "small_model"), + "transformers_device": ("transformers", "device"), + "transformers_dtype": ("transformers", "dtype"), + "transformers_is_multi_model": ("transformers", "is_multi_model"), + "voice_whisper_model": ("voice", "whisper_model"), + "voice_piper_model": ("voice", "piper_model"), + "voice_sample_rate": ("voice", "sample_rate"), + "voice_frame_duration_ms": ("voice", "frame_duration_ms"), + "voice_vad_aggressiveness": ("voice", "vad_aggressiveness"), + "voice_silence_timeout_ms": ("voice", "silence_timeout_ms"), + "voice_max_record_seconds": ("voice", "max_record_seconds"), + "max_tokens": ("model_params", "max_tokens"), + "temperature": ("model_params", "temperature"), + "max_retries": ("model_params", "max_retries"), + "retry_delay": ("model_params", "retry_delay"), + "request_timeout": ("model_params", "request_timeout"), + "max_messages": ("model_params", "max_messages"), + "max_tool_iterations": ("model_params", "max_tool_iterations"), + "stream_timeout": ("model_params", "stream_timeout"), + "first_chunk_timeout": ("model_params", "first_chunk_timeout"), + "model_filters": ("model_params", "model_filters"), + "truncation_max_chars_small": ("model_params", "truncation_max_chars_small"), + "truncation_max_chars_large": ("model_params", "truncation_max_chars_large"), + "max_write_chars": ("model_params", "max_write_chars"), + "dry_run": ("model_params", "dry_run"), + "offline_mode": ("model_params", "offline_mode"), + "use_oauth": ("model_params", "use_oauth"), + "use_agent_pool": ("model_params", "use_agent_pool"), + "agent_pool_size": ("model_params", "agent_pool_size"), + "agents_yaml": ("model_params", "agents_yaml"), + "log_level": ("logging", "log_level"), + "log_file": ("logging", "log_file"), + "profiles_dir": ("paths", "profiles_dir"), + "logs_dir": ("paths", "logs_dir"), + "default_profile": ("session", "default_profile"), + "session_auto_save": ("session", "auto_save"), + "session_resume_prompt": ("session", "resume_prompt"), + "workspace_max_workspaces": ("workspace", "max_workspaces"), + "api_host": ("api_server", "host"), + "api_port": ("api_server", "port"), + "api_profile": ("api_server", "profile"), + "api_mode": ("api_server", "mode"), + "api_permission_mode": ("api_server", "permission_mode"), + "api_permission_timeout": ("api_server", "permission_timeout"), +} + # Mapping: env var -> dot-separated path into settings dict ENV_TO_SETTINGS: dict[str, str] = { "OLI_BACKEND": "backend", @@ -144,6 +216,8 @@ "OLI_API_PORT": "api_server.port", "OLI_API_PROFILE": "api_server.profile", "OLI_API_MODE": "api_server.mode", + "OLI_API_PERMISSION_MODE": "api_server.permission_mode", + "OLI_API_PERMISSION_TIMEOUT": "api_server.permission_timeout", "OLI_DEFAULT_PROFILE": "session.default_profile", "OLI_PROFILES_DIR": "paths.profiles_dir", "OLI_LOGS_DIR": "paths.logs_dir", @@ -367,6 +441,8 @@ def to_appconfig(self, settings: dict) -> AppConfig: api_port=api.get("port", 9734), api_profile=api.get("profile", "default"), api_mode=api.get("mode", "agent"), + api_permission_mode=api.get("permission_mode", "prompt"), + api_permission_timeout=api.get("permission_timeout", 120.0), default_profile=ss.get("default_profile", "default"), profiles_dir=paths.get("profiles_dir", "profiles"), logs_dir=paths.get("logs_dir", "logs"), @@ -433,6 +509,8 @@ def from_appconfig(self, config: AppConfig) -> dict: settings["api_server"]["port"] = config.api_port settings["api_server"]["profile"] = config.api_profile settings["api_server"]["mode"] = config.api_mode + settings["api_server"]["permission_mode"] = config.api_permission_mode + settings["api_server"]["permission_timeout"] = config.api_permission_timeout settings["session"]["default_profile"] = config.default_profile settings["paths"]["profiles_dir"] = config.profiles_dir settings["paths"]["logs_dir"] = config.logs_dir diff --git a/oli_bot/tools/shell.py b/oli_bot/tools/shell.py index f31386c..278f6c7 100644 --- a/oli_bot/tools/shell.py +++ b/oli_bot/tools/shell.py @@ -1,13 +1,18 @@ from __future__ import annotations import asyncio +import logging +import os import re import shlex +import signal from pathlib import Path from typing import Optional from .manager import BuiltinToolManager +logger = logging.getLogger(__name__) + # Filesystem read + navigation (no writers here — see DENIED_ARGS for `find -delete` etc.). _ALLOWED_FILESYSTEM_READ: frozenset[str] = frozenset( { @@ -780,20 +785,41 @@ async def _run_command_handler( stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, cwd=str(cwd) if cwd else None, + # Run in its own process group so cancellation can reap the whole + # pipeline, not just the shell that spawned it. + start_new_session=(os.name == "posix"), ) except FileNotFoundError as e: return f"Error: Command not found: {e}" except OSError as e: return f"Error: Failed to execute command: {e}" + async def _kill_and_reap() -> None: + try: + if os.name == "posix": + os.killpg(proc.pid, signal.SIGKILL) + else: + proc.kill() + except (ProcessLookupError, PermissionError): + try: + proc.kill() + except ProcessLookupError: + pass + try: + await proc.wait() + except Exception: + logger.debug("Failed to reap cancelled subprocess", exc_info=True) + try: stdout_b, stderr_b = await asyncio.wait_for(proc.communicate(), timeout=timeout) except asyncio.TimeoutError: - try: - proc.kill() - except ProcessLookupError: - pass + await _kill_and_reap() return f"Error: Command timed out after {timeout} seconds." + except asyncio.CancelledError: + # Ctrl+I / WebSocket interrupt: never leave the child running once + # the tool call is abandoned. + await _kill_and_reap() + raise stdout = stdout_b.decode("utf-8", errors="replace") if stdout_b else "" stderr = stderr_b.decode("utf-8", errors="replace") if stderr_b else "" diff --git a/tests/unit/test_api_permissions.py b/tests/unit/test_api_permissions.py new file mode 100644 index 0000000..9762a14 --- /dev/null +++ b/tests/unit/test_api_permissions.py @@ -0,0 +1,75 @@ +"""Tests for the WebSocket permission broker.""" + +import asyncio + +import pytest + +from oli_bot.api.permissions import PermissionBroker + + +async def _wait_for(predicate, attempts=200): + for _ in range(attempts): + if predicate(): + return + await asyncio.sleep(0) + raise AssertionError("condition never became true") + + +async def test_broker_sends_request_and_resolves(): + sent = [] + + async def send(frame): + sent.append(frame) + + broker = PermissionBroker(send, timeout=5.0) + pending = asyncio.create_task(broker.request("Write to /tmp/x")) + await _wait_for(lambda: bool(sent)) + frame = sent[0] + assert frame["type"] == "permission_request" + data = frame["data"] + assert data["description"] == "Write to /tmp/x" + assert data["options"] == ["once", "session", "deny"] + assert data["default"] == "deny" + assert broker.pending_count == 1 + + assert broker.resolve(data["id"], "session") is True + assert await pending == "session" + assert broker.pending_count == 0 + + +async def test_broker_timeout_denies_and_notifies(): + sent = [] + + async def send(frame): + sent.append(frame) + + broker = PermissionBroker(send, timeout=0.01) + assert await broker.request("Read secret") == "deny" + assert [f["type"] for f in sent] == [ + "permission_request", + "permission_timeout", + ] + + +async def test_broker_rejects_invalid_decision_and_unknown_id(): + async def send(frame): + return None + + broker = PermissionBroker(send, timeout=1.0) + with pytest.raises(ValueError): + broker.resolve("missing", "allow") + assert broker.resolve("missing", "deny") is False + + +async def test_broker_cancel_all_resolves_pending_prompts(): + sent = [] + + async def send(frame): + sent.append(frame) + + broker = PermissionBroker(send, timeout=5.0) + pending = asyncio.create_task(broker.request("Do it")) + await _wait_for(lambda: bool(sent)) + assert broker.cancel_all("deny") == 1 + assert await pending == "deny" + assert broker.pending_count == 0 diff --git a/tests/unit/test_api_server.py b/tests/unit/test_api_server.py index 04a5729..2dbd89e 100644 --- a/tests/unit/test_api_server.py +++ b/tests/unit/test_api_server.py @@ -152,6 +152,75 @@ async def stream_generate(self, messages, tools=None, max_tokens=None): yield TextChunk("ok") +class _PermissionWriteStub: + """First round asks to write a file (always permission-gated).""" + + model = "stub-permission" + + def __init__(self, path): + self.path = str(path) + self.calls = 0 + + async def stream_generate(self, messages, tools=None, max_tokens=None): + self.calls += 1 + if self.calls == 1: + yield ToolCallChunk( + [ + ToolCall( + id="p1", + name="builtin__write_file", + parameters={"file_path": self.path, "content": "x"}, + ) + ] + ) + else: + yield TextChunk("write handled") + + +class _AutoReadStub: + """First round reads a file; auto permission mode must allow it.""" + + model = "stub-auto-read" + + def __init__(self, path, contents): + self.path = str(path) + self.contents = contents + self.calls = 0 + + async def stream_generate(self, messages, tools=None, max_tokens=None): + self.calls += 1 + if self.calls == 1: + yield ToolCallChunk( + [ + ToolCall( + id="r1", + name="builtin__read_file", + parameters={"file_path": self.path}, + ) + ] + ) + else: + yield TextChunk("read handled") + + +class _SlowStub: + """Streams one chunk, stalls, then streams another unless interrupted.""" + + model = "stub-slow" + + def __init__(self): + self.calls = 0 + + async def stream_generate(self, messages, tools=None, max_tokens=None): + self.calls += 1 + if self.calls == 1: + yield TextChunk("part1") + await asyncio.sleep(5) + yield TextChunk("part2") + else: + yield TextChunk("after interrupt") + + class _StubMCP: async def call_tool(self, name, params, confirm_callback=None, **kwargs): return "result-of-echo" @@ -637,6 +706,122 @@ def test_websocket_bad_attachment_keeps_connection(api, tmp_path): assert _recv_until_done(ws)[-1]["type"] == "done" +# --------------------------------------------------------------------------- # +# /v1/chat (WebSocket) — permissions and interruption # +# --------------------------------------------------------------------------- # + + +def _set_permission_config(api, mode="prompt", timeout=5.0): + api.application.state.config = AppConfig( + _env_file=None, + backend="ollama", + api_permission_mode=mode, + api_permission_timeout=timeout, + ) + + +def _recv_until(ws, frame_type): + frames = [] + while True: + frame = ws.receive_json() + frames.append(frame) + if frame.get("type") == frame_type: + return frames + + +def test_websocket_permission_prompt_and_deny(api, tmp_path): + api.reset(_PermissionWriteStub(tmp_path / "written.txt")) + _set_permission_config(api) + with api.client.websocket_connect("/v1/chat") as ws: + assert ws.receive_json()["type"] == "connected" + ws.send_json({"content": "write it"}) + frames = _recv_until(ws, "permission_request") + request = frames[-1]["data"] + assert "Write to" in request["description"] + assert request["options"] == ["once", "session", "deny"] + assert request["default"] == "deny" + + # Invalid decisions are rejected without resolving the prompt. + ws.send_json( + { + "action": "permission_response", + "id": request["id"], + "decision": "maybe", + } + ) + error = ws.receive_json() + assert error["type"] == "error" + assert "Invalid permission decision" in error["data"]["message"] + + ws.send_json( + { + "action": "permission_response", + "id": request["id"], + "decision": "deny", + } + ) + frames = _recv_until_done(ws) + types = [f["type"] for f in frames] + assert "tool_call_result" in types + result = next(f for f in frames if f["type"] == "tool_call_result") + assert "Permission denied" in result["data"]["result"] + assert frames[-1]["data"]["full_text"] == "write handled" + assert not (tmp_path / "written.txt").exists() + + +def test_websocket_permission_timeout_denies(api, tmp_path): + api.reset(_PermissionWriteStub(tmp_path / "written.txt")) + _set_permission_config(api, timeout=0.05) + with api.client.websocket_connect("/v1/chat") as ws: + assert ws.receive_json()["type"] == "connected" + ws.send_json({"content": "write it"}) + frames = _recv_until_done(ws) + types = [f["type"] for f in frames] + assert "permission_request" in types + assert "permission_timeout" in types + result = next(f for f in frames if f["type"] == "tool_call_result") + assert "Permission denied" in result["data"]["result"] + + +def test_websocket_permission_auto_mode(api, tmp_path): + path = tmp_path / "readme.txt" + path.write_text("auto-allowed contents", encoding="utf-8") + api.reset(_AutoReadStub(path, "auto-allowed contents")) + _set_permission_config(api, mode="auto") + with api.client.websocket_connect("/v1/chat") as ws: + assert ws.receive_json()["type"] == "connected" + ws.send_json({"content": "read it"}) + frames = _recv_until_done(ws) + types = [f["type"] for f in frames] + assert "permission_request" not in types + result = next(f for f in frames if f["type"] == "tool_call_result") + assert "auto-allowed contents" in result["data"]["result"] + assert frames[-1]["data"]["full_text"] == "read handled" + + +def test_websocket_interrupt_rolls_back_turn(api): + api.reset(_SlowStub()) + _set_permission_config(api) + with api.client.websocket_connect("/v1/chat") as ws: + assert ws.receive_json()["type"] == "connected" + ws.send_json({"content": "slow please"}) + frames = _recv_until(ws, "text_chunk") + assert frames[-1]["data"]["text"] == "part1" + + ws.send_json({"action": "interrupt"}) + frames = _recv_until_done(ws) + types = [f["type"] for f in frames] + assert "interrupted" in types + done = frames[-1] + assert done["type"] == "done" + assert done["data"]["interrupted"] is True + assert done["data"]["full_text"] == "part1" + + # The connection remains usable after an interrupt. + ws.send_json({"content": "again"}) + assert _recv_until_done(ws)[-1]["data"]["full_text"] == "after interrupt" + + # --------------------------------------------------------------------------- # # /v1/mcp (MCP server configuration) # # --------------------------------------------------------------------------- # diff --git a/tests/unit/test_chat_interrupt.py b/tests/unit/test_chat_interrupt.py new file mode 100644 index 0000000..937cf37 --- /dev/null +++ b/tests/unit/test_chat_interrupt.py @@ -0,0 +1,85 @@ +"""Tests for the TUI Ctrl+I interrupt rollback logic. + +The full Textual interaction is exercised manually; these tests pin the +rollback guard and the "is a response active?" check, which are the parts a +mistake would silently corrupt history with. +""" + +from oli_bot.chat import OliBot +from oli_bot.models import Message + + +def _bot(messages, *, turn_id, user_index, user_text): + bot = object.__new__(OliBot) + bot.messages = messages + bot._turn_id = turn_id + bot._turn_user_index = user_index + bot._turn_user_text = user_text + calls: list[str] = [] + bot._refresh_context_tokens = lambda: calls.append("refresh") + bot._save_session = lambda: calls.append("save") + return bot, calls + + +def test_rollback_removes_interrupted_turn_and_persists(): + bot, calls = _bot( + [ + Message(role="system", content="sys"), + Message(role="user", content="keep"), + Message(role="assistant", content="ok"), + Message(role="user", content="interrupt me"), + ], + turn_id=3, + user_index=3, + user_text="interrupt me", + ) + assert bot._rollback_interrupted_turn(3) is True + assert [m.content for m in bot.messages] == ["sys", "keep", "ok"] + assert calls == ["refresh", "save"] + + +def test_rollback_ignores_stale_turn_id(): + """A newer turn started before the old worker noticed the cancel.""" + bot, calls = _bot( + [Message(role="user", content="new prompt")], + turn_id=4, + user_index=0, + user_text="new prompt", + ) + assert bot._rollback_interrupted_turn(3) is False + assert [m.content for m in bot.messages] == ["new prompt"] + assert calls == [] + + +def test_rollback_ignores_replaced_message_at_same_index(): + bot, calls = _bot( + [Message(role="user", content="different")], + turn_id=1, + user_index=0, + user_text="original", + ) + assert bot._rollback_interrupted_turn(1) is False + assert [m.content for m in bot.messages] == ["different"] + assert calls == [] + + +def test_rollback_ignores_out_of_range_index(): + bot, calls = _bot([], turn_id=1, user_index=5, user_text="gone") + assert bot._rollback_interrupted_turn(1) is False + assert calls == [] + + +def test_response_active_reads_worker_and_agent_flag(): + bot = object.__new__(OliBot) + bot._response_worker = None + bot.agent = type("_Agent", (), {"_generating": False})() + assert bot._response_active() is False + + bot._response_worker = type("_Worker", (), {"is_finished": False})() + assert bot._response_active() is True + + bot._response_worker = type("_Worker", (), {"is_finished": True})() + assert bot._response_active() is False + + bot.agent._generating = True + assert bot._response_active() is True diff --git a/tests/unit/test_config_api.py b/tests/unit/test_config_api.py index 4ecad0e..6ef121a 100644 --- a/tests/unit/test_config_api.py +++ b/tests/unit/test_config_api.py @@ -34,7 +34,9 @@ def test_flat_mapping_covers_every_default_setting(): invisible to ``GET/PUT /v1/config``.""" defaults = _leaf_paths(DEFAULT_SETTINGS) mapped = _mapped_paths() - assert mapped == defaults, f"unmapped: {defaults - mapped}; stale: {mapped - defaults}" + assert ( + mapped == defaults + ), f"unmapped: {defaults - mapped}; stale: {mapped - defaults}" def test_flat_to_nested_and_back_round_trips(): @@ -66,6 +68,8 @@ def test_get_returns_session_workspace_and_backend_extras(client): assert data["workspace_max_workspaces"] == 20 assert data["use_oauth"] is False assert data["agents_yaml"] == "" + assert data["api_permission_mode"] == "prompt" + assert data["api_permission_timeout"] == 120.0 def test_put_persists_new_keys(tmp_path, client): @@ -77,6 +81,8 @@ def test_put_persists_new_keys(tmp_path, client): "session_auto_save": False, "session_resume_prompt": False, "workspace_max_workspaces": 5, + "api_permission_mode": "auto", + "api_permission_timeout": 30.0, } res = client.put("/v1/config", json=payload) assert res.status_code == 200 @@ -94,6 +100,8 @@ def test_put_persists_new_keys(tmp_path, client): "default_profile": "researcher", } assert saved["workspace"] == {"max_workspaces": 5} + assert saved["api_server"]["permission_mode"] == "auto" + assert saved["api_server"]["permission_timeout"] == 30.0 fetched = client.get("/v1/config").json() for key, value in payload.items(): diff --git a/tests/unit/test_config_screen.py b/tests/unit/test_config_screen.py index 3ee481c..84a4abc 100644 --- a/tests/unit/test_config_screen.py +++ b/tests/unit/test_config_screen.py @@ -3,6 +3,13 @@ from textual.app import App from oli_bot.screens import ConfigScreen +from oli_bot.screens.config_screen import CONFIG_FIELDS +from oli_bot.settings import SETTINGS_FLAT_MAP + + +def test_config_fields_cover_every_setting(): + """Every flat setting must have a control in the /config screen.""" + assert set(CONFIG_FIELDS) == set(SETTINGS_FLAT_MAP) def _settings() -> dict: @@ -199,3 +206,56 @@ async def test_config_screen_save_round_trips_new_sections(): assert result["paths"]["logs_dir"] == "L" assert result["session"]["auto_save"] is False assert result["session"]["resume_prompt"] is False + + +async def test_config_screen_round_trips_openai_responses_and_timeouts(): + settings = _settings() + settings["openai"]["responses_enabled"] = True + settings["model_params"] = { + "stream_timeout": 111.0, + "first_chunk_timeout": 222.0, + } + app = _HostApp(settings) + async with app.run_test() as pilot: + screen = app.screen + assert screen.query_one("#cfg-openai-responses").value is True + assert screen.query_one("#cfg-stream-timeout").value == "111.0" + assert screen.query_one("#cfg-first-chunk-timeout").value == "222.0" + screen._save() + await pilot.pause() + result = app.result + assert result["openai"]["responses_enabled"] is True + assert result["model_params"]["stream_timeout"] == 111.0 + assert result["model_params"]["first_chunk_timeout"] == 222.0 + + +async def test_config_screen_round_trips_api_permission_settings(): + settings = _settings() + settings["api_server"] = { + "permission_mode": "auto", + "permission_timeout": 15.0, + } + app = _HostApp(settings) + async with app.run_test() as pilot: + screen = app.screen + assert screen.query_one("#cfg-api-permission-mode").value == "auto" + assert screen.query_one("#cfg-api-permission-timeout").value == "15.0" + screen._save() + await pilot.pause() + api = app.result["api_server"] + assert api["permission_mode"] == "auto" + assert api["permission_timeout"] == 15.0 + + +async def test_config_screen_preserves_unknown_settings(): + settings = _settings() + settings["future_section"] = {"enabled": True} + settings["model_params"]["future_param"] = 42 + app = _HostApp(settings) + async with app.run_test() as pilot: + screen = app.screen + screen._save() + await pilot.pause() + result = app.result + assert result["future_section"] == {"enabled": True} + assert result["model_params"]["future_param"] == 42