Compare commits

..
Author SHA1 Message Date
Alexandre Teixeira faf9ba27f6 fix(chat): honor effective RAG policy in context building 2026-11-01 15:23:32 +00:00
Yu-Wei ChangandAlexandre Teixeira 8450793fe6 feat(admin): rename model endpoints inline (#6970)
* feat(admin): rename model endpoints inline

* fix(cookbook): keep renamed endpoint names on relaunch

Relaunching a model at an existing endpoint URL reset the endpoint name
to the launched repo's short name, undoing a rename made in Added Models.
Only replace the name when it is still a name the Cookbook generates for
one of the endpoint's pinned models, so endpoints that were never renamed
still follow the newly served model.

---------

Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 08:45:11 +00:00
c4fdd7ad5b tests: skip the searxng settings migration tests on Windows (#6882)
scripts/migrate_searxng_settings.py runs inside the searxng container
entrypoint and preserves the settings file's owner and mode with
os.fchmod/os.fchown, which do not exist on Windows, so every subprocess
run of the script in this module fails with AttributeError before the
behaviour under test is reached. Mark the module POSIX-only.

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 08:33:08 +00:00
JulianFriedleandAlexandre Teixeira 6fced41220 feat(mcp): let a deployment declare local MCP tools (#6330)
* feat(mcp): let a deployment declare local MCP tools

Tools served by a user-configured MCP server are absent from
TOOL_CAPABILITIES, so they are unknown and fail high. For a server with
both a read and a write tool that is unusable: the read result arms the
post-external gate, because an unknown result is not SYSTEM integrity,
and the write that follows is refused until separately authorized. Every
second call needs an approval, for a server that never leaves the host.

Add ODYSSEUS_LOCAL_MCP_READ_TOOLS and ODYSSEUS_LOCAL_MCP_WRITE_TOOLS.
Declared read tools become READ_PRIVATE with SYSTEM integrity, so their
output no longer arms the gate; declared write tools become WRITE_PRIVATE
and stay inside POST_EXTERNAL_BLOCKED_EFFECTS, so a run tainted by real
external content still requires the approval. Unset variables keep the
current behaviour exactly.

Fixes #6329

* fix(mcp): secure local MCP authorization and preserve external taint gate

Keep declared local MCP read tool results untrusted with EXTERNAL_UNTRUSTED
integrity so they continue to arm the external-context gate. Introduce a
narrowly scoped continuation exception for declared local MCP writes on
the same server when no external taint or explicit error payloads enter
the run. Preserves gate protection for shell execution, network egress,
email, and cross-server actions.

Fixes #6329

* fix(mcp): resolve local continuation against stdio transport and enforce built-in classifications

Resolve declared local MCP continuation operations against connected stdio
servers and enforce established built-in tool security classifications.
Ensure remote HTTP and SSE servers, unverified transports, replaced sessions,
stale configurations, and operations with dangerous effects or missing request
grants fail closed.

* fix(mcp): require sealed authority for local continuation

* fix(mcp): seal local tool admission and synchronize GPU stacks

---------

Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 03:19:59 +00:00
Yu-Wei ChangandAlexandre Teixeira 6f2b58c896 fix(cookbook): preserve endpoints across model launches (#6969)
* fix(cookbook): match stale endpoints by launched model, not display name

After registering an LLM endpoint, the Cookbook deleted every other local-*
endpoint with the same display name at a different URL. The display name is
the repo short name, which repos from different orgs share (for example
meta-llama/Llama-3.1-8B-Instruct and unsloth/Llama-3.1-8B-Instruct), so
launching one deleted the endpoint of the other while its server was running.

Match stale endpoints by the full model id they were last launched for,
which is the last entry of their pinned_models. Reusing an endpoint now
moves the launched model to the end of its pins so that entry stays current.

* fix(cookbook): guard endpoint cleanup by serve ownership

Preserve independently launched models instead of sweeping endpoints by display name or mutable model pins.

Bind Cookbook endpoint registrations to their serve sessions and prevent stale watchdogs or scheduled stops from deleting newer registrations.

Reuse the existing endpoint deletion contract to preserve settings, preferences, session cleanup, and cache invalidation.

Cover LLM and image endpoint lifecycle regressions.

---------

Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 03:06:13 +00:00
39303f19cd fix(editor): stabilize image generation, layer editing and controls (#6992)
* fix(editor): stabilize image workflows and editing controls

* docs(editor): add desktop and mobile visual validation evidence

* fix(editor): synchronize quick edit model events and register imagePreferences in test coordinator

* fix(settings): enforce authoritative global image-gen policy and non-admin preference persistence

- Enforce authoritative administrator global image_gen_enabled disable in get_user_setting, do_generate_image, and image_editing_provider_available, preventing non-admin user preference overrides while preserving user-disabled opt-outs and can_generate_images privilege checks across direct, gallery, and editor paths.
- Filter non-admin preference persistence in saveImagePreferences to user preference keys only (image_model, image_edit_model, image_quality), preventing global policy toggles from leaking into user preferences.
- Return accurate 200 OK Response from saveImagePreferences after fallback persistence succeeds so settings UI displays 'Saved' instead of false 403 error, while retaining visibility of real errors.
- Load per-user preferences via readImagePreferences in initImageSettings so non-admin selections persist and display correctly after page reload.
- Clear per-user model preferences on successful admin save (POST /api/auth/settings) via PUT /api/prefs/{key} with value: null to prevent stale per-user preferences from shadowing updated global settings.
- Support clearing keys in PUT /api/prefs/{key} when value is null.
- Add comprehensive Python regression tests for global-disabled/user-enabled, global-enabled/user-disabled, and authorized generation, plus Node and browser regression tests for non-admin persistence, toggle isolation, reload, and admin clearing.

* fix(settings): invalidate shared appConfig cache near auth settings write

* fix(test): isolate test_tool_policy unit suite from external model endpoint state

* fix(test): isolate web_fetch failure regression from contextual follow-up heuristics

* fix(test): isolate test_tool_policy from cross-test approval gate state

Ensure _patch_loop_basics disables TOOL_APPROVAL_GATE_ENABLED so routing tests in test_tool_policy run in the intended opt-out posture even when prior tests populate DATA_DIR with skills or untrusted context. Remove unnecessary forced_tools and contextual web mock workarounds.

* fix(settings): isolate non-admin image toggle and surface preference cleanup errors

- Restrict image generation availability toggle to admin users in imageModels.js, disabling the switch for non-admins and reverting unauthorized changes with an explicit message.
- Separate non-admin image model preference saves from global toggle updates.
- Track preference cleanup results on admin save in imagePreferences.js, propagating cleanup failures instead of ignoring them.
- Require valid preference keys on 403 fallback so toggle-only attempts reject cleanly.
- Add /api/prefs route mock to test_image_model_settings_sync_both_directions_and_survive_reopening.
- Add regression coverage for admin cleanup failures and non-admin toggle rejection.

* fix(settings): reconcile admin toggle on partial cleanup failure and report errors

- In imagePreferences.js, distinguish pre-persistence failures from post-save cleanup failures using err.globalSaved and err.phase.

- In imagePreferences.js, notify listeners of global policy updates on partial success before propagating cleanup failure.

- In imageModels.js, retain the persisted target state on admin toggle when global persistence succeeds but cleanup fails.

- In imageModels.js, reconcile admin toggle with authoritative server state via readImagePreferences() after ambiguous or partial failures.

- In imageModels.js, report partial success accurately without suppressing preference cleanup error messages.

- Add regression coverage in test_settings_save_response_ok_js.py for cleanup HTTP 500, cleanup network rejection, successful cleanup, and failed global POST.

---------

Co-authored-by: pewdiepie-archdaemon <pewdiepie-archdaemon@users.noreply.github.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 02:56:46 +00:00
5353241743 fix(chat): warn before regenerate, resend or edit deletes later messages; offer fork (#6225)
* fix(chat): warn before "regenerate" permanently deletes later messages

Both "regenerate" entry points — the per-message footer's ↻ "Regenerate
from here" button, and the vision editor's "Regenerate message" button —
call POST /api/session/{id}/truncate, which does a real, permanent SQL
delete of every message after the point clicked, then generates a new
reply. Clicking either on anything but the very last exchange did this with
zero warning: the button's own label doesn't say it also erases the rest
of the conversation, and neither button visually signals it's destructive
differently from any other message-footer action.

Caught live: a mis-click destroyed a real conversation with no way to
recover it (no backup covered the affected window). This adds a
confirmation prompt — via the existing uiModule.styledConfirm — whenever
the action would delete more than just the message(s) being regenerated,
stating the exact count of messages that would be lost. Regenerating the
most recent exchange (the everyday case, nothing exists after it) is
unaffected and still proceeds with zero friction.

New test: tests/test_regenerate_truncate_warning_js.py — drives the real
regenerateFrom/resendUserMessage functions under Node with a fake message
list DOM, confirming: the last-exchange case never prompts and truncates
directly; an earlier case prompts with the correct count; cancelling
aborts before any network call; confirming proceeds with the correct
keep_count. Also fixed a pre-existing, unrelated gap this surfaced while
verifying the full suite: test_pr6020_browser_review_regressions.py's
Node harness never stubbed `navigator` at all (sessions.js reads
navigator.platform at module load) — a gap on origin/dev itself, not
caused by this branch's diff, using the same Object.defineProperty fix
already applied on #6019/#6148 for the sibling issue there (Node 21+'s own
getter-only navigator global, not relevant to this specific failure but
the same defensive pattern).

Verified: full pytest suite in-container, 5798 passed / 11 pre-existing
unrelated failures (README/docs/agent-loop baseline) / 0 new.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

* test(chat): cover plain resend after dev's replace-by-default change

dev made plain resend (no options) replace from the clicked turn by
default, with append-only resend opt-in via { append: true } (see
test_resend_message_nondestructive.py). The footer "Resend message"
button calls resendUserMessage(el) with no options, so it now truncates
everything after the clicked turn and goes through this PR's warning.
Replace the old "plain resend never prompts" test, which assumed the
previous append-by-default behavior, with one asserting that plain
resend on an earlier pair prompts with the right count, and one
asserting that explicit append-only resend never truncates or prompts.

Also resolve ui.js/sessions.js through the same cache-busting query
string chat.js imports them with. ES modules cache by full URL, so the
harness's unversioned import of ui.js was a second module instance and
its uiMod.styledConfirm stub never reached the instance chat.js calls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(chat): warn before an edit deletes later messages; offer to fork instead

Sending an edited user message truncates the chat at that message, the
same unrecoverable server-side delete as regenerate and resend, but did
it with no prompt. It now goes through the same confirmation, worded for
the action ("Sending this edit will also permanently delete the N
messages after it"). Cancelling leaves the editor open with the draft.

The confirmation for all three actions (regenerate, resend, edit) also
offers "Fork from here": it forks the chat just before the user turn,
switches to the new chat, and replays the action there (the same message,
or the edited text), leaving the original chat untouched. forkFrom now
shares the same fork helper.

Tests drive the real editUserMessage Send handler under Node and cover
the edit prompt (count, cancel, last-message no-prompt) and the fork
choice for each action (fork keep_count, no truncate, replayed text).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(chat): cut and fork a chat by message id, not loaded-page position

Regenerate and edit sent keep_count, the clicked message's position among
the messages loaded in the page. A long chat loads only its newest messages
until the user scrolls up, so that position is smaller than the real one and
the truncate deleted real messages above the clicked one (regenerating the
latest reply in a ~50-message chat removed about a dozen earlier messages).
Resend already sent before_msg_id; regenerate and edit now do the same.

The fork options this PR adds had the same flaw. Fork now accepts
before_msg_id (fork before that message) and through_msg_id (include it),
resolved against the stored messages; keep_count stays as the fallback for a
message that has no id yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(chat): keep a regenerated turn's message id current; stop on a refused cut

Regenerate keeps the original user bubble on screen and resubmits its text
without drawing a new bubble. The resubmitted message is a new DB row, but
the bubble kept the deleted row's id, so the next regenerate's cut was
refused (404) and the page sent the turn anyway, stacking a second reply.

The stream now reports the saved user message's id (user_message_saved),
which the page attaches to the user bubble, or to the bubble a regenerate
kept. Regenerate and edit stop with an error when the server refuses the
cut instead of sending.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(chat): name the buttons in words instead of symbols

CONTRIBUTING asks for no Unicode emoji in UI or code; the test comments
used the regenerate and pencil glyphs to name buttons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(chat): share the fork-and-send step across edit, resend and regenerate

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(chat): pass the regenerate fork its attachment ids explicitly

Resend already hands its collected file ids to _forkAndSend; regenerate
relied on the earlier _pendingRegenAttachments assignment. Pass _regenIds
too so both fork paths read the same, and check the hand-off in the
regenerate tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(chat): assert fork choices keep the user message's attachments

Run regenerate and resend's "Fork from here" against a user message with
an uploaded file and check that the send fired in the fork carries its
file id, replacing the source-text check. chat.js exposes a read-only
_pendingRegenAttachmentIds() for this, like cookbook-hwfit.js's test hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(chat): preserve attachments across edit and regeneration

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 02:47:32 +00:00
Wojciech TaisnerandAlexandre Teixeira 285d05e72e fix(docker): forward ODYSSEUS_FIRST_TOKEN_TIMEOUT through compose (#6778)
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 02:28:58 +00:00
dc0fbbf7aa fix(compose): default ODYSSEUS_TTS_CACHE_MAX_BYTES so compose stops warning (#6619) (#6645)
The three shipped compose files forwarded the variable bare, so every
`docker compose` command on a default .env printed "variable is not set".
Default it to the 500 MiB that .env.example documents, like the other
limits, and add a guard test that rejects any bare ${VAR} in compose files.

Fixes #6619

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 02:03:06 +00:00
zai184andAlexandre Teixeira 12b9c5f2ad chore(compose): parameterize searxng host port via SEARXNG_HOST_PORT (#6598)
The searxng host binding was hardcoded to 127.0.0.1:8080 in all three
compose files, which collides with other stacks that legitimately use
8080 (e.g. a local llama.cpp server). Parameterize it the same way the
odysseus UI port already works:

  ports:
    - "127.0.0.1:${SEARXNG_HOST_PORT:-8080}:8080"

Default unchanged (8080). The UI->searxng connection uses the container
port over the docker network (SEARXNG_INSTANCE=http://searxng:8080), so
no other change is needed.

Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 01:55:36 +00:00
cb26ccd9a7 test: skip native bash tool tests on Windows (2 modules) (#6872)
* tests: skip native bash behavior test on Windows in test_production_external_bridge

On Windows the only containment mechanism (windows_tree) enforces wall-clock
only; ENFORCING mode refuses bash (filesystem/process_tree unavailable), so
exit_code is 1. The test asserts bubblewrap/process_group: POSIX-only by design.

Part of #4287

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* tests: skip native bash exit-status test on Windows in test_preview_execution_evidence

BashTool needs enforcing containment (filesystem/process_tree); Windows
windows_tree gives wall-clock only, so the command is not executed and
exit_code is 1 instead of 7. POSIX-only by design.

Part of #4287

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 01:47:57 +00:00
23a480571a tests: skip the 0o600 file-mode assert on Windows in the effect log fsync test (#6892)
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 01:39:56 +00:00
6a43cf23ea tests: make hardcoded /tmp assertions OS-neutral (#6864)
Root cause: _is_sensitive_path splits on os.sep, so POSIX literals missed
on Windows; /tmp is not a root on Windows (only $TMPDIR); inline-JSON test
compared against "/tmp/wf.txt" while Windows abspath is C:\tmp\wf.txt.
Change: native-separator helper, TMPDIR monkeypatch, abspath alternative.
Before: 9 failed; after: 0 failed (74 passed, 1 symlink skip).

Part of #4287

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 01:33:28 +00:00
64b457dc0a fix(tests): read static sources as UTF-8 in the static assertion tests (#6850)
On native Windows the default text encoding is the ANSI code page
(cp1252 on en/pt systems), so the module-level read_text() calls in
eight static tests fail at collection with UnicodeDecodeError on bytes
0x81/0x90 that appear in static/js/chat.js, chatRenderer.js, app.js,
index.html and src/visual_report.py. Pass encoding="utf-8" like the
other 700+ tests in the suite already do.

Found by running the suite natively on Windows 11 (Python 3.11).

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Co-authored-by: Alexandre Teixeira <general@alexandreteixeira.dev>
2026-11-01 01:26:11 +00:00
red person ef3467e8d4 fix(tasks): render daily brief calendars in local time (#6926) 2026-10-31 13:54:14 +00:00
red person 72e8520fa3 fix(models): honor visible pinned IDs in route selection (#6922) 2026-10-31 13:51:59 +00:00
red person 9276db9c11 fix(models): skip non-chat models when resolving chat defaults (#6907) 2026-10-31 13:49:26 +00:00
GrzegorzandClaude Opus 5.5 a797cee0b3 fix(cookbook): diagnose vLLM serves on an NVIDIA driver that is too old (#6727) (#6728)
- Add a diagnosis for "NVIDIA driver on your system is too old" and
  "CUDA driver version is insufficient for CUDA runtime version", the
  errors PyPI's CUDA 13 torch/vLLM builds hit on drivers older than 580.
- Remove the nested _diagnose_serve_output copy in cookbook_routes.py.
  It shadowed the helper and had drifted, so /api/cookbook/tasks/status
  never used the helper's newer diagnoses.
- Capture the tmux pane with -J in the status poll so lines wrapped at
  the pane width do not split error text mid-word.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 13:47:30 +00:00
Grzegorz 76061498c8 fix(gallery): validate album rename and image_ids input (#6822) (#6823) 2026-10-31 13:45:02 +00:00
Merajul Arefin 04f8546d9d fix(search): gallery library per-word search with LIKE escaping (#6584)
Gallery search built a single `%{search}%` LIKE, so multi-word queries only matched the exact adjacent phrase ("red car" missed "a car that is red") and literal % / _ acted as SQL wildcards. Split on whitespace and require each token to match (prompt OR tags OR ai_tags), escaping LIKE wildcards per token; same escaping for the tag filter. Promote _escape_like to public escape_like_pattern() shared helper.

Part of #2123.

Tests: tests/test_gallery_search_multiword.py (5 tests); 4 fail pre-fix, 5 pass post-fix. Neighbors (session_search, gallery null-user, shims): 25 passed.
2026-10-31 13:43:20 +00:00
Robert Vind-Gardoș 9835c1a7f9 fix(skills): refuse renaming a skill onto a name the owner already uses (#6941)
update_skill only checked whether the target directory existed. A skill
with that name in another category lives elsewhere on disk, so the
rename went through and left two skills with one name. Update and
delete look skills up by name, so the next delete removed whichever
file os.walk reached first.

Check the owner's skills in every category before renaming, and answer
409 from PUT /api/skills/{id} instead of the misleading 404.
2026-10-31 13:34:05 +00:00
Isharakandisharak7m 239f09cbc4 fix(docker): retry PyPI fetches in realesrgan wheel builder (#6407)
Replace urllib-based PyPI metadata retrieval with curl --retry 8 to
handle transient TLS/SSL EOF errors that abort the Docker image build.
The sdist download also gets retry logic. No functional change to the
patching or wheel-building steps.

Co-authored-by: isharak7m <192635824+isharak7m@users.noreply.github.com>
2026-10-31 13:32:28 +00:00
GrzegorzandClaude Opus 5.5 a1a7f8fc9a fix(agent): don't clamp an unknown context window to the small-server budget (#6678) (#6706)
budget_context_for_model() returns 0 when the endpoint does not prove its
context window, and compute_input_token_budget() answers 0 with the
conservative default budget (6000). The agent loop's small-local-server
clamp (`candidate_context <= 8192`) also matched 0 and cut the request to
max(1200, 0) tokens, so on any endpoint with an unreported window an open
document or a few thousand tokens of earlier context were trimmed away
before the model saw them ("5519 tokens > 1200 budget (ctx=1800)").

Apply the clamp only to a known window.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 13:30:29 +00:00
Prince 87193d23f5 fix(agent-loop): release buffered prose when '<' is not a tool tag (#6757) (#6758) 2026-10-31 13:26:43 +00:00
VykosandClaude 516f046fef fix(chat): label the vision description injected for text-only models (#6223)
When the chat model cannot see images, the separate vision call's description
is appended to the user's message. Nothing marked it as machine-derived, so the
chat model read it as the user's own prose and answered the description instead
of the question.

The label goes below the `[Image: <name>]` marker line because the client
strips the block by matching that exact prefix; renaming the marker would leave
the raw description rendered in the user's bubble.

Fixes #5573

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-31 13:24:32 +00:00
red person 48c3a7fc36 fix(pdf): convert CMYK images before vision analysis (#6917) 2026-10-31 13:22:55 +00:00
red person 70fc58225c fix(documents): treat library search punctuation literally (#6944) 2026-10-31 13:21:10 +00:00
red person 9efedc8b07 fix(documents): retain language extensions in ZIP exports (#6848) 2026-10-31 13:18:58 +00:00
gloss-stack 1d251ef867 Fix transcript search snippet positioning (#6206) 2026-10-31 13:05:35 +00:00
Nguyen Thanh Dat bf5bf57bc1 fix(diagnostics): report a failed config source instead of "disabled" (#6154) (#6169)
`_gather_inputs()` fails soft: a source that raises leaves its inventory as
`[]`. Every probe reads an empty inventory as "not configured" and answers
`disabled`, so a broken integrations file, email store, or endpoint database is
indistinguishable from a subsystem the operator deliberately turned off. With
the remaining subsystems disabled, `GET /api/diagnostics/services` then reports
`overall: "ok"` while three of its inputs failed to load — the exceptions appear
only in DEBUG logs.

Record each failure alongside the empty value (`failed`: source -> controlled
category) and report the affected subsystem through `_source_unavailable()`
rather than probing it with an inventory that is empty for the wrong reason.

- `degraded`, not `disabled`: the configured state is unknown, not known-off.
  Not `down` either — nothing reached the subsystem and failed; the inventory
  could not be read. Severity is non-zero either way, so a report containing a
  failed source can no longer roll up to `ok`.
- A source that loads and is genuinely empty still reports `disabled`.
- One broken source degrades one entry; the others are still probed.
- `meta` carries the source name and the controlled `_classify_error()`
  category only, never exception text, credentials, or database detail.
- `inputs.get("failed")` so an inputs payload without the key still probes
  normally.

Verified with the reporter's harness: settings loading with
`search_provider: "disabled"` while the other three sources raise now yields
`overall: "degraded"` with `searxng` still `disabled`; before, all five read
`disabled` and `overall` was `ok`.
2026-10-31 13:04:18 +00:00
MBGAandBolt4243 2019a97339 fix(skills): preserve escaped quotes in frontmatter lists (#6265)
* fix(skills): preserve escaped quotes in frontmatter lists

* test(skills): use descriptive frontmatter examples

---------

Co-authored-by: Bolt4243 <277300782+Bolt4243@users.noreply.github.com>
2026-10-31 13:02:02 +00:00
Aashish Mahato 9a1fdb192e fix(cookbook): resolve NameError in hf-gguf-files exception handler (#6341) (#6369)
In routes/cookbook_routes.py, the exception handler for GET /api/cookbook/hf-gguf-files referenced repo instead of the parameter repo_id, causing an unhandled NameError and 500 status when upstream requests to Hugging Face failed.

Fixes #6341.
2026-10-31 12:59:17 +00:00
76 changed files with 5283 additions and 479 deletions
+11
View File
@@ -105,6 +105,17 @@ SEARXNG_INSTANCE=http://localhost:8080
# trusted single-user setups that accept injected content reaching side-effect tools.
# ODYSSEUS_TOOL_APPROVAL_GATE=1
# Comma-separated list of exact qualified MCP read and write tool names
# on host-local servers. Only explicitly configured stdio MCP servers qualify.
# Declarations do not override built-in tool effects. Declared reads remain
# untrusted (EXTERNAL_UNTRUSTED) to prevent prompt injections from reaching shell
# or network tools, but allow continuation to declared non-destructive write tools
# on the exact same stdio server without repeated approvals. Stdio transport does
# not guarantee network isolation. Authorization must still originate from the
# trusted request. Automatic same-store writes carry residual prompt-injection risk.
# ODYSSEUS_LOCAL_MCP_READ_TOOLS=mcp__notes__search_notes,mcp__notes__read_note
# ODYSSEUS_LOCAL_MCP_WRITE_TOOLS=mcp__notes__create_note,mcp__notes__update_note
# Mark session cookies Secure. Left unset, this follows the request scheme:
# an HTTPS login gets a Secure cookie, a plain-HTTP one does not. Set true to
# force it on, or false to force it off while you still serve plain HTTP.
+2 -1
View File
@@ -66,10 +66,11 @@ External content that reaches the LLM is treated as untrusted via `src/prompt_se
It is on unless `ODYSSEUS_TOOL_APPROVAL_GATE` is set to a falsy value (`0`/`false`/`no`/`off`). Request authority narrows which tool families a turn may use, but within an admitted family it does not bind the exact action: a request to read email admits `send_email` and `delete_email`, and agent processes inherit the host network. Until those are covered by their own boundaries, this gate is what stops an injected instruction from reaching them. Turning it off lets a successful injection reach `bash`, `host_shell`, `send_email` and `delete_email` without a separate confirmation.
Two exemptions apply even when the gate is on, both deliberate:
Three exemptions apply even when the gate is on, all deliberate:
- Sources in `_CONTROL_PLANE_CONTEXT_SOURCES` (skills, runtime descriptors, the open editor document, the open email, uploaded files) are treated as control-plane metadata and still permit read-only tools.
- A TUI run that advertises a host shell bridge and declares `unattended_mode` exempts the local execution set in `TUI_CLIENT_TOOL_NAMES`. Personal, network and deployment-local tools are never exempted.
- Narrow continuation for local MCP tools declared via `ODYSSEUS_LOCAL_MCP_READ_TOOLS` and `ODYSSEUS_LOCAL_MCP_WRITE_TOOLS`: Only explicitly configured stdio MCP servers qualify; remote HTTP and SSE servers fail closed. Declarations never override built-in tool effects, so built-in email, browser, memory, image generation, and other built-in servers cannot receive continuation exceptions. Declared read tools remain untrusted (`EXTERNAL_UNTRUSTED`) to ensure that prompt injections in local data cannot reach shell, network, or unrelated tools without approval. Stdio transport does not guarantee network isolation, as local processes may still reach the host network. However, declared non-destructive write tools on the exact same stdio MCP server are permitted to continue without an approval prompt if the only untrusted context in the run was produced by declared local reads on that same server, without any explicit error or external taint. Genuinely external content (web search, web fetch, emails, external message context) blocks these writes. Authorization must still originate from the trusted request: request authority must permit the tool and retain the sealed backend resource grant. The residual same-store write risk (the agent writing to the same local store after reading a local prompt injection) remains subject to the user's initial request authority.
## Security Headers
+22
View File
@@ -568,6 +568,7 @@ class ModelEndpoint(TimestampMixin, Base):
hidden_models = Column(Text, nullable=True) # JSON list of model IDs that failed probing
cached_models = Column(Text, nullable=True) # JSON list of last-known model IDs (avoids probe on list)
pinned_models = Column(Text, nullable=True) # JSON list of admin-pinned model IDs (manual, may not appear in /v1/models)
cookbook_serve_session_id = Column(String, nullable=True) # Current Cookbook serve identity
model_type = Column(String, nullable=True, default="llm") # "llm" or "image"
# auto = classify by URL; local = self-hosted server; api/proxy = external
# OpenAI-compatible API even when reachable through a private/tailnet IP.
@@ -1378,6 +1379,26 @@ def _migrate_add_pinned_models_column():
except Exception:
pass
def _migrate_add_cookbook_serve_session_id_column():
"""Bind Cookbook cleanup to the serve session that owns the endpoint."""
from sqlalchemy import inspect
with engine.begin() as conn:
columns = {
column["name"]
for column in inspect(conn).get_columns("model_endpoints")
}
if "cookbook_serve_session_id" not in columns:
conn.execute(
text(
"ALTER TABLE model_endpoints "
"ADD COLUMN cookbook_serve_session_id VARCHAR"
)
)
def _migrate_add_notes_sort_order():
"""Add sort_order, image_url, repeat columns to notes if they don't exist."""
import sqlite3
@@ -2390,6 +2411,7 @@ def init_db():
_migrate_add_hidden_models_column()
_migrate_add_cached_models_column()
_migrate_add_pinned_models_column()
_migrate_add_cookbook_serve_session_id_column()
_migrate_add_notes_sort_order()
_migrate_add_model_type_column()
_migrate_add_model_endpoint_refresh_columns()
+6 -2
View File
@@ -73,6 +73,8 @@ services:
- ODYSSEUS_CAPTURE_MODEL_REQUESTS=${ODYSSEUS_CAPTURE_MODEL_REQUESTS:-0}
- ODYSSEUS_MCP_EMAIL_OWNER=${ODYSSEUS_MCP_EMAIL_OWNER:-}
- ODYSSEUS_SCRIPT_HOST=${ODYSSEUS_SCRIPT_HOST:-localhost}
- ODYSSEUS_LOCAL_MCP_READ_TOOLS=${ODYSSEUS_LOCAL_MCP_READ_TOOLS:-}
- ODYSSEUS_LOCAL_MCP_WRITE_TOOLS=${ODYSSEUS_LOCAL_MCP_WRITE_TOOLS:-}
- ODYSSEUS_CHAT_UPLOAD_MAX_BYTES=${ODYSSEUS_CHAT_UPLOAD_MAX_BYTES:-10485760}
- ODYSSEUS_GALLERY_UPLOAD_MAX_BYTES=${ODYSSEUS_GALLERY_UPLOAD_MAX_BYTES:-104857600}
- ODYSSEUS_GALLERY_TRANSFORM_UPLOAD_MAX_BYTES=${ODYSSEUS_GALLERY_TRANSFORM_UPLOAD_MAX_BYTES:-26214400}
@@ -82,13 +84,15 @@ services:
- ODYSSEUS_EDITOR_DRAFT_MAX_BYTES=${ODYSSEUS_EDITOR_DRAFT_MAX_BYTES:-268435456}
- ODYSSEUS_STT_MAX_AUDIO_BYTES=${ODYSSEUS_STT_MAX_AUDIO_BYTES:-26214400}
- ODYSSEUS_ICS_MAX_BYTES=${ODYSSEUS_ICS_MAX_BYTES:-10485760}
- ODYSSEUS_TTS_CACHE_MAX_BYTES=${ODYSSEUS_TTS_CACHE_MAX_BYTES}
- ODYSSEUS_TTS_CACHE_MAX_BYTES=${ODYSSEUS_TTS_CACHE_MAX_BYTES:-524288000}
# Host workspace translation is opt-in. Keep the public compose file
# user-neutral; configure these in a local .env or use the host-workspace
# overlay with ODYSSEUS_HOST_WORKSPACE_DIR.
- ODYSSEUS_WORKSPACE_HOST_ROOT=${ODYSSEUS_WORKSPACE_HOST_ROOT:-}
- ODYSSEUS_WORKSPACE_CONTAINER_ROOT=${ODYSSEUS_WORKSPACE_CONTAINER_ROOT:-/workspace}
- ODYSSEUS_WORKSPACE_DEFAULT=${ODYSSEUS_WORKSPACE_DEFAULT:-}
# Local first-token watchdog in seconds; 0 disables it. Empty keeps the code default (60s).
- ODYSSEUS_FIRST_TOKEN_TIMEOUT=${ODYSSEUS_FIRST_TOKEN_TIMEOUT:-}
- DATA_BRAVE_API_KEY=${DATA_BRAVE_API_KEY:-}
- GOOGLE_API_KEY=${GOOGLE_API_KEY:-}
- GOOGLE_PSE_CX=${GOOGLE_PSE_CX:-}
@@ -165,7 +169,7 @@ services:
/usr/local/searxng/.venv/bin/python /tmp/migrate-searxng-settings.py /etc/searxng/settings.yml || true
exec /usr/local/searxng/entrypoint.sh
ports:
- "127.0.0.1:8080:8080"
- "127.0.0.1:${SEARXNG_HOST_PORT:-8080}:8080"
volumes:
- searxng-data:/etc/searxng
- ./config/searxng/settings.yml:/tmp/searxng-settings.yml.template:ro,z
+6 -2
View File
@@ -72,6 +72,8 @@ services:
- ODYSSEUS_CAPTURE_MODEL_REQUESTS=${ODYSSEUS_CAPTURE_MODEL_REQUESTS:-0}
- ODYSSEUS_MCP_EMAIL_OWNER=${ODYSSEUS_MCP_EMAIL_OWNER:-}
- ODYSSEUS_SCRIPT_HOST=${ODYSSEUS_SCRIPT_HOST:-localhost}
- ODYSSEUS_LOCAL_MCP_READ_TOOLS=${ODYSSEUS_LOCAL_MCP_READ_TOOLS:-}
- ODYSSEUS_LOCAL_MCP_WRITE_TOOLS=${ODYSSEUS_LOCAL_MCP_WRITE_TOOLS:-}
- ODYSSEUS_CHAT_UPLOAD_MAX_BYTES=${ODYSSEUS_CHAT_UPLOAD_MAX_BYTES:-10485760}
- ODYSSEUS_GALLERY_UPLOAD_MAX_BYTES=${ODYSSEUS_GALLERY_UPLOAD_MAX_BYTES:-104857600}
- ODYSSEUS_GALLERY_TRANSFORM_UPLOAD_MAX_BYTES=${ODYSSEUS_GALLERY_TRANSFORM_UPLOAD_MAX_BYTES:-26214400}
@@ -81,13 +83,15 @@ services:
- ODYSSEUS_EDITOR_DRAFT_MAX_BYTES=${ODYSSEUS_EDITOR_DRAFT_MAX_BYTES:-268435456}
- ODYSSEUS_STT_MAX_AUDIO_BYTES=${ODYSSEUS_STT_MAX_AUDIO_BYTES:-26214400}
- ODYSSEUS_ICS_MAX_BYTES=${ODYSSEUS_ICS_MAX_BYTES:-10485760}
- ODYSSEUS_TTS_CACHE_MAX_BYTES=${ODYSSEUS_TTS_CACHE_MAX_BYTES}
- ODYSSEUS_TTS_CACHE_MAX_BYTES=${ODYSSEUS_TTS_CACHE_MAX_BYTES:-524288000}
# Host workspace translation is opt-in. Keep the public compose file
# user-neutral; configure these in a local .env or use the host-workspace
# overlay with ODYSSEUS_HOST_WORKSPACE_DIR.
- ODYSSEUS_WORKSPACE_HOST_ROOT=${ODYSSEUS_WORKSPACE_HOST_ROOT:-}
- ODYSSEUS_WORKSPACE_CONTAINER_ROOT=${ODYSSEUS_WORKSPACE_CONTAINER_ROOT:-/workspace}
- ODYSSEUS_WORKSPACE_DEFAULT=${ODYSSEUS_WORKSPACE_DEFAULT:-}
# Local first-token watchdog in seconds; 0 disables it. Empty keeps the code default (60s).
- ODYSSEUS_FIRST_TOKEN_TIMEOUT=${ODYSSEUS_FIRST_TOKEN_TIMEOUT:-}
- DATA_BRAVE_API_KEY=${DATA_BRAVE_API_KEY:-}
- GOOGLE_API_KEY=${GOOGLE_API_KEY:-}
- GOOGLE_PSE_CX=${GOOGLE_PSE_CX:-}
@@ -168,7 +172,7 @@ services:
/usr/local/searxng/.venv/bin/python /tmp/migrate-searxng-settings.py /etc/searxng/settings.yml || true
exec /usr/local/searxng/entrypoint.sh
ports:
- "127.0.0.1:8080:8080"
- "127.0.0.1:${SEARXNG_HOST_PORT:-8080}:8080"
volumes:
- searxng-data:/etc/searxng
- ./config/searxng/settings.yml:/tmp/searxng-settings.yml.template:ro,z
+6 -2
View File
@@ -61,6 +61,8 @@ services:
- ODYSSEUS_CAPTURE_MODEL_REQUESTS=${ODYSSEUS_CAPTURE_MODEL_REQUESTS:-0}
- ODYSSEUS_MCP_EMAIL_OWNER=${ODYSSEUS_MCP_EMAIL_OWNER:-}
- ODYSSEUS_SCRIPT_HOST=${ODYSSEUS_SCRIPT_HOST:-localhost}
- ODYSSEUS_LOCAL_MCP_READ_TOOLS=${ODYSSEUS_LOCAL_MCP_READ_TOOLS:-}
- ODYSSEUS_LOCAL_MCP_WRITE_TOOLS=${ODYSSEUS_LOCAL_MCP_WRITE_TOOLS:-}
- ODYSSEUS_CHAT_UPLOAD_MAX_BYTES=${ODYSSEUS_CHAT_UPLOAD_MAX_BYTES:-10485760}
- ODYSSEUS_GALLERY_UPLOAD_MAX_BYTES=${ODYSSEUS_GALLERY_UPLOAD_MAX_BYTES:-104857600}
- ODYSSEUS_GALLERY_TRANSFORM_UPLOAD_MAX_BYTES=${ODYSSEUS_GALLERY_TRANSFORM_UPLOAD_MAX_BYTES:-26214400}
@@ -70,13 +72,15 @@ services:
- ODYSSEUS_EDITOR_DRAFT_MAX_BYTES=${ODYSSEUS_EDITOR_DRAFT_MAX_BYTES:-268435456}
- ODYSSEUS_STT_MAX_AUDIO_BYTES=${ODYSSEUS_STT_MAX_AUDIO_BYTES:-26214400}
- ODYSSEUS_ICS_MAX_BYTES=${ODYSSEUS_ICS_MAX_BYTES:-10485760}
- ODYSSEUS_TTS_CACHE_MAX_BYTES=${ODYSSEUS_TTS_CACHE_MAX_BYTES}
- ODYSSEUS_TTS_CACHE_MAX_BYTES=${ODYSSEUS_TTS_CACHE_MAX_BYTES:-524288000}
# Host workspace translation is opt-in. Keep the public compose file
# user-neutral; configure these in a local .env or use the host-workspace
# overlay with ODYSSEUS_HOST_WORKSPACE_DIR.
- ODYSSEUS_WORKSPACE_HOST_ROOT=${ODYSSEUS_WORKSPACE_HOST_ROOT:-}
- ODYSSEUS_WORKSPACE_CONTAINER_ROOT=${ODYSSEUS_WORKSPACE_CONTAINER_ROOT:-/workspace}
- ODYSSEUS_WORKSPACE_DEFAULT=${ODYSSEUS_WORKSPACE_DEFAULT:-}
# Local first-token watchdog in seconds; 0 disables it. Empty keeps the code default (60s).
- ODYSSEUS_FIRST_TOKEN_TIMEOUT=${ODYSSEUS_FIRST_TOKEN_TIMEOUT:-}
- DATA_BRAVE_API_KEY=${DATA_BRAVE_API_KEY:-}
- GOOGLE_API_KEY=${GOOGLE_API_KEY:-}
- GOOGLE_PSE_CX=${GOOGLE_PSE_CX:-}
@@ -146,7 +150,7 @@ services:
/usr/local/searxng/.venv/bin/python /tmp/migrate-searxng-settings.py /etc/searxng/settings.yml || true
exec /usr/local/searxng/entrypoint.sh
ports:
- "127.0.0.1:8080:8080"
- "127.0.0.1:${SEARXNG_HOST_PORT:-8080}:8080"
volumes:
- searxng-data:/etc/searxng
- ./config/searxng/settings.yml:/tmp/searxng-settings.yml.template:ro,z
+33 -11
View File
@@ -31,21 +31,43 @@ for spec in $SPECS; do
name="${spec%%==*}"
ver="${spec##*==}"
# pip download builds metadata (and trips the same bug), so fetch the raw
# sdist URL from the PyPI JSON API instead.
url="$(python - "$name" "$ver" <<'PY'
import json, sys, urllib.request
name, ver = sys.argv[1], sys.argv[2]
data = json.load(urllib.request.urlopen(f"https://pypi.org/pypi/{name}/{ver}/json"))
for f in data["urls"]:
if f["packagetype"] == "sdist":
print(f["url"]); break
# sdist URL from the PyPI JSON API instead. Use curl with retries to
# survive transient TLS / network failures that urllib cannot recover from.
metadata="${name}-${ver}.json"
curl --fail --silent --show-error --location \
--retry 8 \
--retry-delay 3 \
--retry-max-time 120 \
--retry-all-errors \
"https://pypi.org/pypi/${name}/${ver}/json" \
-o "$metadata"
url="$(python - "$metadata" <<'PY'
import json, sys
with open(sys.argv[1], encoding="utf-8") as f:
data = json.load(f)
for item in data["urls"]:
if item["packagetype"] == "sdist":
print(item["url"])
break
else:
sys.exit(f"no sdist found for {name}=={ver}")
raise SystemExit("no sdist found")
PY
)"
)"
rm -f "$metadata"
echo ">> fetching ${name} ${ver}: ${url}"
curl -fsSL "$url" -o "${name}.tar.gz"
curl --fail --silent --show-error --location \
--retry 8 \
--retry-delay 3 \
--retry-max-time 120 \
--retry-all-errors \
"$url" \
-o "${name}.tar.gz"
tar xzf "${name}.tar.gz"
rm -f "${name}.tar.gz"
done
echo ">> patching get_version()"
+1 -2
View File
@@ -1276,6 +1276,7 @@ async def build_chat_context(
message=_ctx_msg,
session=sess,
use_web=use_web_val and not skip_web,
use_rag=use_rag_val,
use_memory=mem_enabled,
time_filter=time_filter,
preset_system_prompt=preset.system_prompt,
@@ -1286,8 +1287,6 @@ async def build_chat_context(
incognito=incognito,
use_skills=skills_enabled,
)
if use_rag is not None or is_research_spinoff or casual_low_signal:
_preface_kwargs["use_rag"] = use_rag_val
preface, rag_sources, web_sources = chat_processor.build_context_preface(**_preface_kwargs)
# Capture used memories immediately
+17 -1
View File
@@ -1901,7 +1901,7 @@ def _ts_or_zero(value) -> float:
def _recover_empty_session_model(sess, session_id: str, owner: str | None = None) -> bool:
"""Re-populate sess.model from the matching endpoint's cached models.
"""Recover sess.model from the endpoint's visible cached and pinned models.
Covers the window between endpoint setup and the first chat send: the
picker showed a model in the dropdown but the session record never got
@@ -1965,6 +1965,9 @@ def _recover_empty_session_model(sess, session_id: str, owner: str | None = None
visible = _visible_models(cached, getattr(ep, "hidden_models", None))
except Exception:
visible = cached
if not is_chatgpt_subscription:
from src.endpoint_resolver import _endpoint_enabled_models
visible = _endpoint_enabled_models(ep)
if current_model and current_model in {str(item).strip() for item in visible}:
return False
if is_chatgpt_subscription:
@@ -3923,6 +3926,19 @@ def setup_chat_routes(
if ctx.preprocessed.attachment_meta:
yield f"data: {json.dumps({'type': 'attachments', 'data': ctx.preprocessed.attachment_meta})}\n\n"
# Give the page the saved user message's id. Only the reply's id
# was sent back, so a message sent in the open page had none and
# a later regenerate/edit of it could not cut the chat by id.
try:
_last_user_msg = next((m for m in reversed(getattr(sess, "history", None) or [])
if getattr(m, "role", None) == "user"), None)
_user_db_id = ((getattr(_last_user_msg, "metadata", None) or {}).get("_db_id")
if _last_user_msg is not None else None)
if _user_db_id:
yield f"data: {json.dumps({'type': 'user_message_saved', 'id': _user_db_id})}\n\n"
except Exception:
pass
# Announce any docs auto-created during preprocess (e.g. fillable
# PDF → editable markdown) so the editor pane switches to them
# before the model starts streaming.
+14
View File
@@ -1491,6 +1491,20 @@ def _diagnose_serve_output(text: str) -> dict | None:
"Port is already in use.",
[{"label": "retry on port 8001", "op": "replace", "flag": "--port", "value": "8001"}],
),
(
# PyPI torch/vLLM wheels now target CUDA 13, which needs driver 580+.
# torch: "The NVIDIA driver on your system is too old (found version 12080)";
# vLLM kernels: "CUDA driver version is insufficient for CUDA runtime version".
r"NVIDIA driver on your system is too old|CUDA driver version is insufficient",
"The installed PyTorch/vLLM build needs a newer NVIDIA driver than this server has.",
[
{
"label": "install builds for the driver's CUDA version, e.g. vLLM's +cu129 wheel from its GitHub release with uv pip install … --torch-backend cu129",
"op": "manual",
},
{"label": "or update the NVIDIA driver (CUDA 13 builds need driver 580 or newer)", "op": "manual"},
],
),
(
r"No CUDA GPUs are available|no GPU.*found|CUDA_VISIBLE_DEVICES.*invalid",
"No GPUs are visible to the serve process.",
+100 -240
View File
@@ -468,175 +468,6 @@ def setup_cookbook_routes() -> APIRouter:
task["payload"].pop("hf_token", None)
return state
def _diagnose_serve_output(text: str) -> dict | None:
"""Server-side mirror of the Cookbook UI's common serve diagnoses.
The browser uses cookbook-diagnosis.js for clickable fixes. This gives
the agent/tool path the same structured signal so it can retry with an
adjusted command instead of guessing from raw tmux output.
"""
if not text:
return None
tail = text[-6000:]
patterns = [
(
r"No available memory for the cache blocks|Available KV cache memory:.*-",
"No GPU memory left for KV cache after loading model.",
[
{"label": "retry with GPU memory utilization 0.95", "op": "replace", "flag": "--gpu-memory-utilization", "value": "0.95"},
{"label": "retry with context 2048", "op": "replace", "flag": "--max-model-len", "value": "2048"},
],
),
(
r"CUDA out of memory|torch\.cuda\.OutOfMemoryError|CUDA error: out of memory|warming up sampler|max_num_seqs.*gpu_memory_utilization",
"GPU ran out of memory during startup or warmup.",
[
{"label": "retry with context 4096", "op": "replace", "flag": "--max-model-len", "value": "4096"},
{"label": "retry with GPU memory utilization 0.80", "op": "replace", "flag": "--gpu-memory-utilization", "value": "0.80"},
{"label": "retry with --enforce-eager", "op": "append", "arg": "--enforce-eager"},
],
),
(
r"not divisib|must be divisible|attention heads.*divisible",
"Tensor parallel size is incompatible with the model.",
[
{"label": "retry with tensor parallel size 1", "op": "replace", "flag": "--tensor-parallel-size", "value": "1"},
{"label": "retry with tensor parallel size 2", "op": "replace", "flag": "--tensor-parallel-size", "value": "2"},
],
),
(
r"KV cache.*too (small|large)|max_model_len.*exceeds|maximum.*context",
"Context length is too large for available GPU memory.",
[
{"label": "retry with context 8192", "op": "replace", "flag": "--max-model-len", "value": "8192"},
{"label": "retry with context 4096", "op": "replace", "flag": "--max-model-len", "value": "4096"},
],
),
(
r"enable-auto-tool-choice requires --tool-call-parser",
"Auto tool choice requires an explicit tool call parser.",
[{"label": "retry with Hermes tool parser", "op": "append", "arg": "--tool-call-parser hermes"}],
),
(
r"Please pass.*trust.remote.code=True|contains custom code which must be executed to correctly load|does not recognize this architecture|model type.*but Transformers does not",
"Model requires custom code or newer model support.",
[{"label": "retry with --trust-remote-code", "op": "append", "arg": "--trust-remote-code"}],
),
(
r"Either a revision or a version must be specified|transformers\.integrations\.hub_kernels|kernels/layer",
"vLLM/Transformers kernel package mismatch.",
[{"label": "update vLLM, Transformers, and kernels on this server", "op": "dependency", "package": "vllm transformers kernels"}],
),
(
r"Address already in use|bind.*address.*in use",
"Port is already in use.",
[{"label": "retry on port 8001", "op": "replace", "flag": "--port", "value": "8001"}],
),
(
r"No CUDA GPUs are available|no GPU.*found|CUDA_VISIBLE_DEVICES.*invalid",
"No GPUs are visible to the serve process.",
[{"label": "clear Cookbook GPU selection or choose available GPUs", "op": "settings", "field": "gpus", "value": ""}],
),
(
r"Failed to infer device type|NVML Shared Library Not Found|No module named 'amdsmi'|platform is not available",
"vLLM could not find a supported GPU (CUDA or ROCm). "
"This machine may have integrated or unsupported graphics only.",
[
{"label": "switch to llama.cpp (CPU/Metal, works without a discrete GPU)", "op": "manual"},
{"label": "switch to Ollama (CPU/Metal, works without a discrete GPU)", "op": "manual"},
],
),
(
r"vllm.*command not found|No module named vllm|ERROR: vLLM is not installed",
"vLLM is not installed or not in PATH on this server.",
[{"label": "install vLLM in Cookbook Dependencies", "op": "dependency", "package": "vllm"}],
),
(
r"sgl_kernel[\s\S]*(Python\.h|libnuma\.so\.1|common_ops|libnvrtc\.so)|"
r"(Python\.h|libnuma\.so\.1|common_ops|libnvrtc\.so)[\s\S]*sgl_kernel|"
r"Could not load any common_ops library|"
r"Please ensure sgl_kernel is properly installed",
"SGLang native kernel/runtime is missing or mismatched on this server.",
[
{"label": "repair sglang-kernel in this Python environment", "op": "dependency", "package": "sglang-kernel"},
{"label": "if libnvrtc is still missing, install the matching CUDA/NVRTC runtime on this host", "op": "manual"},
],
),
(
r"sglang.*command not found|No module named sglang|SGLang is not installed",
"SGLang is not installed or not in PATH on this server.",
[{"label": "install SGLang in Cookbook Dependencies", "op": "dependency", "package": "sglang[all]"}],
),
(
r"No module named ['\"]?mlx_lm|mlx_lm.*command not found|MLX is not installed|MLX LM is not installed",
"MLX LM is not installed on this server.",
[{"label": "install mlx-lm in Cookbook Dependencies", "op": "dependency", "package": "mlx-lm"}],
),
(
r"OmniGen2Pipeline|module diffusers has no attribute .*Pipeline|custom_pipeline=.*failed",
"This image model uses a custom Diffusers pipeline that the launch environment does not know yet.",
[{"label": "update Diffusers image dependencies", "op": "dependency", "package": "diffusers transformers accelerate"}],
),
(
r"Unable to quantize model of type <class ['\"]mlx_lm\.models\.switch_layers\.QuantizedSwitchLinear['\"]>|QuantizedSwitchLinear",
"MLX-LM tried to quantize an already-quantized DeepSeek switch layer.",
[
{"label": "relaunch from the cached local Hugging Face snapshot path on this Mac", "op": "manual"},
{"label": "Odysseus now rewrites MLX repo-id launches to a cached snapshot when one exists", "op": "manual"},
],
),
# System build deps come BEFORE the generic llama.cpp catch-all
# so cmake / build-essential / git missing → a specific OS-package
# remediation instead of "install llama-cpp-python[server]" (which
# itself fails to compile when cmake is absent).
(
r"cmake: command not found|cmake.*not found.*[Cc]ould not",
"cmake is required to build llama.cpp from source but isn't installed on this server.",
[{"label": "install build deps for llama.cpp (apt: cmake build-essential git / pacman: cmake base-devel git / dnf: cmake gcc-c++ make git / brew: cmake git)", "op": "dependency", "package": "llama-cpp-python[server]"}],
),
(
r"^(make|g\+\+|gcc): command not found|Could not find C\+\+ compiler",
"A C/C++ compiler (build-essential) is required to build llama.cpp from source.",
[{"label": "install build deps for llama.cpp on this server", "op": "dependency", "package": "llama-cpp-python[server]"}],
),
(
r"^git: command not found",
"git is required to clone the llama.cpp source tree.",
[{"label": "install build deps for llama.cpp on this server", "op": "dependency", "package": "llama-cpp-python[server]"}],
),
(
r"llama-server.*command not found|llama\.cpp.*not found|No module named.*llama_cpp|No module named 'starlette_context'",
"llama.cpp / llama-cpp-python dependencies are missing.",
[{"label": "install llama.cpp dependencies or llama-cpp-python[server]", "op": "dependency", "package": "llama-cpp-python[server]"}],
),
(
r"No GGUF found on this host|no \.gguf file|No GGUF file found",
"No GGUF file found for this model on this host. The llama.cpp backend needs a .gguf file.",
[{"label": "download a GGUF build of this model (repo name usually ends in -GGUF, file like Q4_K_M.gguf)", "op": "manual"}],
),
(
r"No module named 'torch'|No module named torch|No module named 'torchvision'|No module named torchvision|No module named 'diffusers'|No module named diffusers|No module named 'scipy'|No module named scipy|install scipy if you want to use beta sigmas|requires the Torchvision library",
"Diffusion serving requires PyTorch, Torchvision, Diffusers, Accelerate, and SciPy.",
[{"label": "install Diffusers image deps in Cookbook Dependencies", "op": "dependency", "package": "diffusers[torch] torchvision accelerate scipy python-multipart"}],
),
(
r"403 Forbidden|401 Unauthorized|Access to model.*is restricted|gated repo|not in the authorized list|awaiting a review",
"Model access is gated or unauthorized.",
[{"label": "set HF token and request model access on HuggingFace", "op": "manual"}],
),
]
for pattern, message, suggestions in patterns:
if re.search(pattern, tail, re.I):
return {"message": message, "suggestions": suggestions}
if re.search(r"Traceback \(most recent call last\)", tail, re.I) and not re.search(
r"Application startup complete|GET /v1/|Uvicorn running on", tail, re.I
):
return {
"message": "Python traceback detected during serve startup.",
"suggestions": [{"label": "inspect traceback and retry with adjusted backend/settings", "op": "manual"}],
}
return None
def _state_for_client(state):
"""Return cookbook state without raw secrets for browser clients."""
_strip_task_secrets(state)
@@ -1577,10 +1408,15 @@ def setup_cookbook_routes() -> APIRouter:
return {"models": models, "host": host or "local"}
def _auto_register_image_endpoint(req: ServeRequest, remote: str | None) -> str | None:
def _auto_register_image_endpoint(
req: ServeRequest,
remote: str | None,
serve_session_id: str | None = None,
) -> str | None:
"""Register a diffusion model as an image endpoint so it appears in the model selector."""
import re
from core.database import SessionLocal, ModelEndpoint
from routes.model_routes import _normalize_model_ids
from src.settings import load_settings, save_settings
# Parse port from command (--port NNNN), default 8100 for diffusion_server
@@ -1608,9 +1444,13 @@ def setup_cookbook_routes() -> APIRouter:
if existing:
existing.is_enabled = True
existing.model_type = "image"
existing.name = display_name
# Follow the newly served model unless the user renamed the endpoint.
pinned_names = {f"{m.split('/')[-1]} (image)" for m in _normalize_model_ids(existing.pinned_models)}
if existing.name in pinned_names:
existing.name = display_name
existing.endpoint_kind = "local"
existing.model_refresh_mode = "manual"
existing.cookbook_serve_session_id = serve_session_id
if pinned_models:
existing.cached_models = json.dumps(pinned_models)
existing.pinned_models = json.dumps(pinned_models)
@@ -1634,6 +1474,7 @@ def setup_cookbook_routes() -> APIRouter:
model_refresh_mode="manual",
cached_models=json.dumps(pinned_models) if pinned_models else None,
pinned_models=json.dumps(pinned_models) if pinned_models else None,
cookbook_serve_session_id=serve_session_id,
)
db.add(ep)
db.commit()
@@ -1779,43 +1620,71 @@ def setup_cookbook_routes() -> APIRouter:
# let the probe layer mark it offline if nothing's listening.
logger.info(f"crash-watchdog: serve {session_id} exited cleanly (0); leaving endpoint {endpoint_id}")
return
# Non-zero exit — drop the endpoint.
# A nonzero exit may delete only the endpoint owned by this serve.
try:
from core.database import SessionLocal as _SL, ModelEndpoint as _ME
from core.constants import internal_api_base as _internal_api_base
from core.middleware import INTERNAL_TOOL_HEADER, INTERNAL_TOOL_TOKEN
from urllib.parse import quote
import httpx
db = _SL()
try:
ep = db.query(_ME).filter(_ME.id == endpoint_id).first()
if ep:
# A scheduled serve can leave old non-zero exit markers
# in tmux scrollback while the current OpenAI endpoint is
# actually alive. Verify reachability before deleting the
# endpoint row; otherwise chats fall back even though the
# served model is ready.
try:
probe_url = ep.base_url.rstrip("/") + "/models"
with urllib.request.urlopen(probe_url, timeout=3) as resp:
if 200 <= getattr(resp, "status", 0) < 300:
logger.info(
f"crash-watchdog: serve {session_id} has exit marker {exit_code} "
f"but endpoint {ep.id} is reachable; leaving it registered"
)
return
except Exception:
pass
if not ep or ep.cookbook_serve_session_id != session_id:
logger.info(
f"crash-watchdog: dropping endpoint {endpoint_id} "
f"({ep.name} @ {ep.base_url}) — serve exited {exit_code}"
"crash-watchdog: endpoint %s no longer owned by %s",
endpoint_id, session_id,
)
db.delete(ep)
db.commit()
return
probe_url = ep.base_url.rstrip("/") + "/models"
finally:
db.close()
# Preserve an endpoint that is already healthy, including
# after an old runner emits a stale failure marker.
try:
with urllib.request.urlopen(probe_url, timeout=3) as resp:
if 200 <= getattr(resp, "status", 0) < 300:
logger.info(
"crash-watchdog: endpoint %s is reachable; keeping it",
endpoint_id,
)
return
except Exception:
pass
async with httpx.AsyncClient(timeout=8) as client:
response = await client.delete(
f"{_internal_api_base()}/api/model-endpoints/{quote(endpoint_id, safe='')}",
params={"expected_cookbook_serve_session_id": session_id},
headers={INTERNAL_TOOL_HEADER: INTERNAL_TOOL_TOKEN},
)
if response.status_code == 200:
logger.info(
"crash-watchdog: deleted endpoint %s after serve %s exited %s",
endpoint_id, session_id, exit_code,
)
elif response.status_code in (404, 409):
logger.info(
"crash-watchdog: endpoint %s absent or reassigned; keeping it",
endpoint_id,
)
else:
logger.warning(
"crash-watchdog: endpoint %s deletion returned HTTP %s",
endpoint_id, response.status_code,
)
except Exception as e:
logger.warning(f"crash-watchdog: endpoint cleanup failed: {e!r}")
logger.warning("crash-watchdog: endpoint cleanup failed: %r", e)
return
logger.debug(f"crash-watchdog: no exit marker for {session_id} within window; leaving endpoint {endpoint_id}")
def _auto_register_llm_endpoint(req: ServeRequest, remote: str | None) -> str | None:
def _auto_register_llm_endpoint(
req: ServeRequest,
remote: str | None,
serve_session_id: str | None = None,
) -> str | None:
"""Register a freshly-served LLM as a model endpoint so it appears in the
model picker without a manual /setup step — the text-model sibling of
_auto_register_image_endpoint.
@@ -1832,6 +1701,7 @@ def setup_cookbook_routes() -> APIRouter:
)
import re
from core.database import SessionLocal, ModelEndpoint
from routes.model_routes import _normalize_model_ids
# Port: ordered fallbacks so we match whatever the user actually
# asked for, not a hardcoded default:
@@ -1893,8 +1763,12 @@ def setup_cookbook_routes() -> APIRouter:
# deployment alias from /v1/models, and a stale server can answer on the
# same port while the new launch failed. Keeping the requested model id
# pinned makes the picker reflect the actual launch intent.
pinned_models = [mlx_shim_model_id] if mlx_shim_model_id else ([req.repo_id] if req.repo_id else [])
launched_model = mlx_shim_model_id or req.repo_id
pinned_models = [launched_model] if launched_model else []
# Launch registration must not delete other endpoints.
# Matching names, model pins, and failed probes do not prove that
# another serve has stopped. Lifecycle cleanup owns termination.
db = SessionLocal()
try:
# Reuse an endpoint already pointed at this URL instead of duplicating.
@@ -1902,19 +1776,22 @@ def setup_cookbook_routes() -> APIRouter:
if existing:
existing.is_enabled = True
existing.model_type = "llm"
existing.name = display_name
# Follow the newly served model unless the user renamed the endpoint.
pinned_names = {m.split("/")[-1] for m in _normalize_model_ids(existing.pinned_models)}
if existing.name in pinned_names:
existing.name = display_name
existing.endpoint_kind = "local"
existing.model_refresh_mode = "auto"
if pinned_models:
try:
existing_pinned = json.loads(existing.pinned_models or "[]")
except Exception:
existing_pinned = []
merged_pinned = []
for mid in [*existing_pinned, *pinned_models]:
if mid and mid not in merged_pinned:
merged_pinned.append(mid)
existing.pinned_models = json.dumps(merged_pinned) if merged_pinned else None
# A reused endpoint belongs to the newest serve session.
existing.cookbook_serve_session_id = serve_session_id
if launched_model:
# Pins represent model selection, not launch history.
existing_pins = _normalize_model_ids(
existing.pinned_models
)
if launched_model not in existing_pins:
existing_pins.append(launched_model)
existing.pinned_models = json.dumps(existing_pins)
if is_ollama_endpoint:
existing.endpoint_kind = "ollama"
if pinned_models:
@@ -1940,22 +1817,6 @@ def setup_cookbook_routes() -> APIRouter:
db.commit()
except Exception as _pe:
logger.warning(f"Re-probe failed for {base_url}: {_pe!r}")
# Sweep stale dupes: other endpoints with the same display name
# at DIFFERENT URLs (likely failed earlier-attempt ports) get
# deleted so the picker doesn't show an offline ghost next to
# the working one. Only sweeps endpoints whose id starts with
# `local-` so we never touch a user's hand-added DeepSeek/OpenAI/
# etc. entry with a coincidentally matching name.
stale = (db.query(ModelEndpoint)
.filter(ModelEndpoint.name == display_name)
.filter(ModelEndpoint.base_url != base_url)
.filter(ModelEndpoint.id.like("local-%"))
.all())
for s in stale:
logger.info(f"Sweeping stale local endpoint {s.id} ({s.base_url})")
db.delete(s)
if stale:
db.commit()
return existing.id
ep_id = f"local-{uuid.uuid4().hex[:8]}"
@@ -1971,22 +1832,11 @@ def setup_cookbook_routes() -> APIRouter:
cached_models=json.dumps(pinned_models) if pinned_models else None,
pinned_models=json.dumps(pinned_models) if pinned_models else None,
supports_tools=supports_tools,
cookbook_serve_session_id=serve_session_id,
)
db.add(ep)
db.commit()
logger.info(f"Auto-registered local model endpoint: {display_name} @ {base_url}")
# Same sweep on first-register path: drop any pre-existing local-*
# endpoints with this display name pointed elsewhere.
stale = (db.query(ModelEndpoint)
.filter(ModelEndpoint.name == display_name)
.filter(ModelEndpoint.id != ep_id)
.filter(ModelEndpoint.id.like("local-%"))
.all())
for s in stale:
logger.info(f"Sweeping stale local endpoint {s.id} ({s.base_url})")
db.delete(s)
if stale:
db.commit()
# Probe /v1/models NOW and write cached_models so the chat
# picker actually shows the model on the next /api/models
# call. Without this immediate probe, the endpoint has empty
@@ -2808,9 +2658,17 @@ def setup_cookbook_routes() -> APIRouter:
endpoint_id = None
is_image_endpoint = "diffusion_server.py" in req.cmd or "mlx_image_server.py" in req.cmd
if is_image_endpoint:
endpoint_id = _auto_register_image_endpoint(req, remote)
endpoint_id = _auto_register_image_endpoint(
req,
remote,
serve_session_id=session_id,
)
elif not is_pip_install:
endpoint_id = _auto_register_llm_endpoint(req, remote)
endpoint_id = _auto_register_llm_endpoint(
req,
remote,
serve_session_id=session_id,
)
# Crash watchdog: the auto-register above writes the endpoint row
# IMMEDIATELY (before the server has even bound its port) so the
@@ -3930,7 +3788,7 @@ def setup_cookbook_routes() -> APIRouter:
return {"ok": False, "files": [], "error": f"HF API HTTP {resp.status_code}"}
data = resp.json()
except Exception:
logger.exception("HF GGUF file scan failed for %s", repo)
logger.exception("HF GGUF file scan failed for %s", repo_id)
return {"ok": False, "files": [], "error": "HF API request failed"}
files = [
str(s.get("rfilename") or "")
@@ -4446,7 +4304,9 @@ def setup_cookbook_routes() -> APIRouter:
# fills the visible tail. Without this, output_tail ends up
# as just "Locale: C / Ubuntu_Odysseus ❯" and the agent
# can't diagnose the actual error.
capture_cmd = ssh_base + [remote, _remote_tmux_command("capture-pane", "-t", session_id, "-p", "-S", "-500")]
# -J joins lines tmux wrapped at the pane width, so an error
# split mid-word still matches the serve diagnosis patterns.
capture_cmd = ssh_base + [remote, _remote_tmux_command("capture-pane", "-t", session_id, "-p", "-J", "-S", "-500")]
elif IS_WINDOWS:
# LOCAL Windows task: launched as a detached process (no tmux).
# Liveness comes from the <session>.pid file, output from the
@@ -4455,7 +4315,7 @@ def setup_cookbook_routes() -> APIRouter:
capture_cmd = None
else:
check_cmd = ["tmux", "has-session", "-t", session_id]
capture_cmd = ["tmux", "capture-pane", "-t", session_id, "-p", "-S", "-500"]
capture_cmd = ["tmux", "capture-pane", "-t", session_id, "-p", "-J", "-S", "-500"]
local_win_task = (not remote) and IS_WINDOWS
+9 -4
View File
@@ -16,6 +16,7 @@ from core.database import SessionLocal, Document, DocumentVersion
from core.database import Session as DbSession
from src.auth_helpers import get_current_user, _auth_disabled
from src.constants import MAIL_ATTACHMENTS_DIR
from src.session_search import _escape_like
from src.upload_handler import reserve_upload_references
logger = logging.getLogger(__name__)
@@ -571,9 +572,10 @@ def setup_document_routes(session_manager, upload_handler=None) -> APIRouter:
# match docs containing both words regardless of position/order.
if search:
for tok in search.split():
term = f"%{tok}%"
term = f"%{_escape_like(tok)}%"
q = q.filter(
Document.title.ilike(term) | Document.current_content.ilike(term)
Document.title.ilike(term, escape="\\")
| Document.current_content.ilike(term, escape="\\")
)
# Language filter. "pdf" is a display language derived from the
@@ -812,10 +814,13 @@ def setup_document_routes(session_manager, upload_handler=None) -> APIRouter:
ext = _ext.get(doc.language or "text", ".txt")
base = (doc.title or "document").strip() or "document"
base = re.sub(r"[^\w\-. ]+", "", base)[:60].strip() or doc.id
name = base if "." in base else base + ext
has_ext = base.lower().endswith(ext)
stem = base[:-len(ext)] if has_ext else base
suffix = base[-len(ext):] if has_ext else ext
name = stem + suffix
i = 1
while name in used:
name = f"{base}-{i}" + ("" if "." in base else ext)
name = f"{stem}-{i}{suffix}"
i += 1
used.add(name)
content = doc.current_content or ""
+46 -18
View File
@@ -18,6 +18,7 @@ from fastapi import APIRouter, HTTPException, Query, Request
from core.database import SessionLocal, GalleryImage, GalleryAlbum, ModelEndpoint
from core.database import Session as DbSession
from src.auth_helpers import get_current_user, owner_filter, require_privilege
from src.session_search import escape_like_pattern
from src.upload_limits import (
read_upload_limited,
GALLERY_UPLOAD_MAX_BYTES,
@@ -323,6 +324,23 @@ def _b64_bytes(value: str, *, label: str = "image") -> bytes:
raise HTTPException(400, f"Invalid {label}") from exc
async def _json_object(request: Request) -> dict:
try:
data = await request.json()
except ValueError:
raise HTTPException(400, "Invalid JSON body")
if not isinstance(data, dict):
raise HTTPException(400, "JSON object body required")
return data
async def _image_ids(request: Request) -> list:
ids = (await _json_object(request)).get("image_ids", [])
if not isinstance(ids, list) or not all(isinstance(i, str) for i in ids):
raise HTTPException(400, "image_ids must be a list of image ids")
return ids
def _pil_image_to_b64(img, *, fmt: str = "PNG") -> str:
buf = io.BytesIO()
img.save(buf, format=fmt)
@@ -1203,15 +1221,18 @@ def setup_gallery_routes() -> APIRouter:
)
q = _owner_filter(q, user)
# Search filter (prompt + tags + ai_tags)
# Per-term AND search: a single `%foo bar%` LIKE only matched the
# exact phrase, so "red car" missed "a car that is red".
# LIKE wildcards are escaped per token.
if search:
term = f"%{search}%"
from sqlalchemy import or_
q = q.filter(or_(
GalleryImage.prompt.ilike(term),
GalleryImage.tags.ilike(term),
GalleryImage.ai_tags.ilike(term),
))
for tok in search.split():
term = f"%{escape_like_pattern(tok)}%"
q = q.filter(or_(
GalleryImage.prompt.ilike(term, escape="\\"),
GalleryImage.tags.ilike(term, escape="\\"),
GalleryImage.ai_tags.ilike(term, escape="\\"),
))
# Tag filter. The UI stacks multiple tag pills by passing them
# comma-separated — each tag adds a separate AND-filter so the
@@ -1222,9 +1243,10 @@ def setup_gallery_routes() -> APIRouter:
for one in (t.strip() for t in tag.split(",")):
if not one:
continue
safe_tag = f"%{escape_like_pattern(one)}%"
q = q.filter(_or(
GalleryImage.tags.ilike(f"%{one}%"),
GalleryImage.ai_tags.ilike(f"%{one}%"),
GalleryImage.tags.ilike(safe_tag, escape="\\"),
GalleryImage.ai_tags.ilike(safe_tag, escape="\\"),
))
# Model filter
@@ -2992,14 +3014,22 @@ def setup_gallery_routes() -> APIRouter:
@router.put("/api/gallery/albums/{album_id}")
async def update_album(request: Request, album_id: str):
user = get_current_user(request)
data = await request.json()
data = await _json_object(request)
name = data.get("name")
if name is not None:
if not isinstance(name, str) or not name.strip():
raise HTTPException(400, "Album name required")
name = name.strip()
description = data.get("description")
if description is not None and not isinstance(description, str):
raise HTTPException(400, "Album description must be a string")
db = SessionLocal()
try:
album = _get_or_404_album(db, album_id, user)
if data.get("name") is not None:
album.name = data["name"]
if data.get("description") is not None:
album.description = data["description"]
if name is not None:
album.name = name
if description is not None:
album.description = description
if data.get("cover_id") is not None:
cover_id = data["cover_id"] or None
if cover_id:
@@ -3029,8 +3059,7 @@ def setup_gallery_routes() -> APIRouter:
@router.post("/api/gallery/albums/{album_id}/add")
async def add_to_album(request: Request, album_id: str):
user = get_current_user(request)
data = await request.json()
ids = data.get("image_ids", [])
ids = await _image_ids(request)
db = SessionLocal()
try:
_get_or_404_album(db, album_id, user)
@@ -3047,8 +3076,7 @@ def setup_gallery_routes() -> APIRouter:
@router.post("/api/gallery/albums/{album_id}/remove")
async def remove_from_album(request: Request, album_id: str):
user = get_current_user(request)
data = await request.json()
ids = data.get("image_ids", [])
ids = await _image_ids(request)
db = SessionLocal()
try:
_get_or_404_album(db, album_id, user)
+17
View File
@@ -825,6 +825,23 @@ def setup_history_routes(session_manager, upload_handler=None) -> APIRouter:
try:
body = await request.json()
keep_count = body.get("keep_count", 0)
# A message id is exact; keep_count from the client counts only the
# messages loaded in the page. before_msg_id forks before that
# message, through_msg_id includes it.
before_msg_id = str(body.get("before_msg_id") or "").strip()
through_msg_id = str(body.get("through_msg_id") or "").strip()
if before_msg_id or through_msg_id:
db = SessionLocal()
try:
all_db_messages = db.query(DbChatMessage).filter(
DbChatMessage.session_id == session_id
).order_by(DbChatMessage.timestamp).all()
finally:
db.close()
resolved = _keep_count_before_message(all_db_messages, before_msg_id or through_msg_id)
if resolved is None:
raise HTTPException(404, "Message not found")
keep_count = resolved + (0 if before_msg_id else 1)
# Get the source session. keep_count indexes into source.history,
# so this must go through get_session — reading the cache directly
+30 -5
View File
@@ -659,7 +659,7 @@ _NON_CHAT_CONTAINS = (
"gliner", "riva", "-parse", "-embedqa", "-nemoretriever",
"topic-control", "calibration",
"ai-synthetic-video", "cosmos-reason2",
"bge", "llama-guard",
"bge", "llama-guard", "-embed", "rerank",
)
_NON_CHAT_EXACT_PREFIXES = (
"gpt-audio", # gpt-audio, gpt-audio-mini etc. (not gpt-4o-audio-preview which is chat)
@@ -2846,11 +2846,12 @@ def setup_model_routes(model_discovery):
return {"endpoint_id": "", "endpoint_url": "", "model": ""}
base = _normalize_base(ep.base_url)
chat_url = build_chat_url(base)
if model and not _is_chat_model(model):
model = ""
if not model and (getattr(ep, "cached_models", None) or getattr(ep, "pinned_models", None)):
try:
visible = _visible_models(ep.cached_models, getattr(ep, "hidden_models", None), getattr(ep, "pinned_models", None))
if visible:
model = visible[0]
model = next((mid for mid in visible if _is_chat_model(mid)), "")
except Exception:
pass
return {"endpoint_id": ep.id, "endpoint_url": chat_url, "model": model}
@@ -3043,20 +3044,44 @@ def setup_model_routes(model_discovery):
return {"dependents": _settings_using_endpoint(ep_id)}
@router.delete("/model-endpoints/{ep_id}")
def delete_model_endpoint(ep_id: str, request: Request):
def delete_model_endpoint(
ep_id: str,
request: Request,
expected_cookbook_serve_session_id: Optional[str] = None,
):
require_admin(request)
db = SessionLocal()
try:
ep = db.query(ModelEndpoint).filter(ModelEndpoint.id == ep_id).first()
if not ep or not _chatgpt_endpoint_visible(ep, request):
raise HTTPException(404, "Endpoint not found")
if expected_cookbook_serve_session_id is not None:
# The DELETE itself checks ownership. An old serve must never
# mutate the settings of an endpoint reused by another serve.
if (
not ep_id.startswith(("local-", "img-"))
or not expected_cookbook_serve_session_id
):
raise HTTPException(409, "Cookbook endpoint ownership mismatch")
deleted = (
db.query(ModelEndpoint)
.filter(
ModelEndpoint.id == ep_id,
ModelEndpoint.cookbook_serve_session_id
== expected_cookbook_serve_session_id,
)
.delete(synchronize_session=False)
)
if not deleted:
raise HTTPException(409, "Cookbook endpoint ownership changed")
# Clean up any settings that reference this endpoint
cleared = _clear_settings_for_endpoint(ep_id)
cleared_user_preferences = _clear_user_prefs_for_endpoint(ep_id)
cleared_sessions = _clear_sessions_for_endpoint(db, ep.base_url, ep.id, ep.owner)
cleared_loaded_sessions = _clear_loaded_sessions_for_endpoint(ep.base_url, ep.id, ep.owner)
auth_id = getattr(ep, "provider_auth_id", None)
db.delete(ep)
if expected_cookbook_serve_session_id is None:
db.delete(ep)
cleared_provider_auth = _delete_orphaned_provider_auth(db, auth_id, exclude_ep_id=ep_id)
db.commit()
_invalidate_models_cache()
+5 -14
View File
@@ -183,6 +183,7 @@ def _resolve_endpoint_runtime(ep, owner=None, model: Optional[str] = None):
OAuth tokens in ProviderAuthSession, so ep.api_key is intentionally empty.
"""
from src.endpoint_resolver import (
_endpoint_enabled_models,
build_chat_url,
build_headers,
resolve_endpoint_runtime as resolve_model_endpoint_runtime,
@@ -197,7 +198,7 @@ def _resolve_endpoint_runtime(ep, owner=None, model: Optional[str] = None):
ep_model = (model or "").strip()
if not ep_model:
try:
models = json.loads(ep.cached_models) if ep.cached_models else []
models = _endpoint_enabled_models(ep)
if models:
ep_model = _first_chat_model(models)
except Exception:
@@ -824,23 +825,13 @@ def setup_research_routes(research_handler, session_manager=None) -> APIRouter:
if not ep_url or not ep_model:
# Last resort: this user's enabled endpoint, plus legacy shared rows.
from src.database import SessionLocal
from src.endpoint_resolver import normalize_base, build_chat_url, build_headers
db = SessionLocal()
try:
ep = _owned_enabled_endpoint(db, user)
if ep:
base = normalize_base(ep.base_url)
fallback_url = build_chat_url(base)
fallback_headers = build_headers(ep.api_key, base)
fallback_model = ""
if ep.cached_models:
try:
models = json.loads(ep.cached_models)
if models:
fallback_model = _first_chat_model(models)
except Exception:
pass
_merge(fallback_url, fallback_model, fallback_headers)
resolved = _resolve_endpoint_runtime(ep, owner=user)
if resolved:
_merge(*resolved)
finally:
db.close()
+5
View File
@@ -15,6 +15,7 @@ import httpx
from fastapi import APIRouter, HTTPException, Request
from pydantic import BaseModel, Field
from services.memory.skill_format import slugify
from services.memory.skills import SkillsManager
from src.auth_helpers import get_current_user
from services.skills import evaluation as _evaluation
@@ -836,6 +837,10 @@ def setup_skills_routes(skills_manager: SkillsManager) -> APIRouter:
updates = body.dict(exclude_none=True)
if not updates:
return {"ok": True}
if "name" in updates:
new_name = slugify(updates["name"] or match.get("name"))
if new_name != match.get("name") and skills_manager.has_skill(new_name, owner=user):
raise HTTPException(409, f"A skill named '{new_name}' already exists")
ok = skills_manager.update_skill(match.get("name"), updates, owner=user)
if not ok:
raise HTTPException(404, "Skill not found")
+8 -8
View File
@@ -289,15 +289,15 @@ def _resolve_run_endpoint(db, task: ScheduledTask, run: TaskRun) -> str:
try:
from core.database import ModelEndpoint
eps = db.query(ModelEndpoint).filter(ModelEndpoint.is_enabled == True).all()
from src.auth_helpers import owner_filter
from src.endpoint_resolver import _endpoint_enabled_models
eps = owner_filter(
db.query(ModelEndpoint).filter(ModelEndpoint.is_enabled == True),
ModelEndpoint, getattr(task, "owner", None),
).all()
for ep in eps:
cached = []
if ep.cached_models:
try:
cached = json.loads(ep.cached_models) or []
except Exception:
cached = []
if model in cached:
if model in _endpoint_enabled_models(ep):
return ep.base_url or ""
except Exception:
pass
+19
View File
@@ -492,6 +492,25 @@ VARIABLE_NOTES: dict[str, tuple[str, str, str]] = {
"tools that execute code, mutate state or cause external side effects need "
"a separate approval. Set to 0 to opt out.",
),
"ODYSSEUS_LOCAL_MCP_READ_TOOLS": (
"Agent loop and tool execution", USER,
"Security-relevant. Comma-separated exact MCP read-tool names "
"on configured stdio MCP servers. Only stdio servers qualify. "
"Declarations do not override built-in tool effects. Output remains "
"untrusted to prevent privilege escalation, but allows continuation to "
"declared write tools on the same stdio server without external taint. "
"Stdio transport does not guarantee network isolation. Automatic "
"same-store writes carry residual prompt-injection risk.",
),
"ODYSSEUS_LOCAL_MCP_WRITE_TOOLS": (
"Agent loop and tool execution", USER,
"Security-relevant. Comma-separated exact MCP write-tool names "
"on configured stdio MCP servers. Only stdio servers qualify. "
"Declarations do not override built-in tool effects. Allowed to execute "
"after declared local reads on the same stdio server, but blocked once "
"external untrusted context enters the run. Authorization must still "
"originate from the trusted request grant.",
),
"ODYSSEUS_MCP_ALLOWED_COMMANDS": (
"Agent loop and tool execution", USER,
"Security-relevant. Comma-separated allowlist of MCP launcher basenames the "
+6 -1
View File
@@ -130,10 +130,15 @@ def _parse_scalar(raw: str, *, string_only: bool = False) -> Any:
def _split_top_level(s: str, sep: str) -> List[str]:
"""Split `s` on `sep` ignoring separators inside [] or quotes."""
out, buf, depth, quote = [], [], 0, None
escaped = False
for ch in s:
if quote:
buf.append(ch)
if ch == quote:
if escaped:
escaped = False
elif ch == "\\" and quote == '"':
escaped = True
elif ch == quote:
quote = None
continue
if ch in ("'", '"'):
+16
View File
@@ -500,6 +500,19 @@ class SkillsManager:
sk.path = self._skill_file(cat, nm)
return sk.to_dict()
def has_skill(self, name: str, owner: Optional[str] = None) -> bool:
"""Whether `owner` already has a skill called `name`, in any category.
Update and delete find a skill by name within its owner, so two of
one owner's skills sharing a name would make them act on whichever
file is walked first.
"""
for path in self._iter_skill_files():
sk = self._read_skill(path)
if sk and sk.name == name and (sk.owner or "") == (owner or ""):
return True
return False
def update_skill(self, skill_id: str, updates: Dict, owner: Optional[str] = None) -> bool:
"""`skill_id` is the slug name. Allows updating any field plus
renames if `name` changes (file is moved on disk).
@@ -551,6 +564,9 @@ class SkillsManager:
# Rename
new_name = slugify(updates.get("name") or sk.name)
if new_name != sk.name:
if self.has_skill(new_name, owner=owner):
logger.warning(f"Skill rename target name in use: {new_name}")
return False
sk.name = new_name
# Write to potentially new path
+23 -11
View File
@@ -24193,11 +24193,14 @@ async def stream_agent_loop(
budget_is_explicit,
hard_max=hard_max,
)
if candidate_context <= 8192:
if 0 < candidate_context <= 8192:
# Small local servers often tokenize chat wrappers and tool
# results much more generously than our rough estimator. Keep
# substantial headroom for those wrappers and generation so a
# follow-up tool round cannot exceed the server's n_ctx.
# 0 means the window is unknown, which already gets the
# conservative default budget; clamping it as well cut every
# request to 1200 tokens and dropped open documents (#6678).
effective_budget = min(
effective_budget,
max(1200, int(candidate_context * 0.40)),
@@ -27833,16 +27836,25 @@ async def stream_agent_loop(
if _streamed_tool_markup or _streamed_tool_markup_starts(_delta_text):
_streamed_tool_markup += _delta_text
if not _streamed_tool_markup_complete(_streamed_tool_markup):
continue
_visible_markup_tail = strip_tool_blocks(
_streamed_tool_markup,
skip_fenced=True,
).strip()
_streamed_tool_markup = ""
if not _visible_markup_tail:
continue
_delta_text = _visible_markup_tail
data["delta"] = _delta_text
if _streamed_tool_markup_starts(_streamed_tool_markup):
continue
# False alarm: a bare "<" (e.g. ``x < 2``) looked like
# a tag prefix, but the buffer can no longer become a
# tool tag. Release it as prose instead of holding the
# rest of the answer forever.
_delta_text = _streamed_tool_markup
_streamed_tool_markup = ""
data["delta"] = _delta_text
else:
_visible_markup_tail = strip_tool_blocks(
_streamed_tool_markup,
skip_fenced=True,
).strip()
_streamed_tool_markup = ""
if not _visible_markup_tail:
continue
_delta_text = _visible_markup_tail
data["delta"] = _delta_text
full_response += _delta_text
if not _suppress_unavailable_web_delta:
_qwen_buffered_route = bool(
+96
View File
@@ -371,6 +371,101 @@ def interpret_request(request_text, *, history=(), workspace=None, active_docume
return SemanticIntent(families, selected, required_read_operation_for_request(request_text, history))
def _trusted_local_mcp_request_tools(request_text: str, intent: SemanticIntent) -> set[str]:
"""Seal local MCP operations from operator declarations and trusted request intent.
Discovery validates an operator-declared tool but cannot grant it. For the
notes family, only an explicit request for local notes admits matching
read operations and explicitly requested non-destructive write verbs. Other
MCP domains require the user to name the qualified operation explicitly.
"""
from src.tool_capabilities import (
LOCAL_MCP_READ_TOOLS_ENV, LOCAL_MCP_WRITE_TOOLS_ENV,
_local_mcp_tools, _is_declared_local_mcp_read_tool,
_is_declared_local_mcp_write_tool, capabilities_for_tool, ToolEffect,
)
from src.tool_utils import get_mcp_manager
manager = get_mcp_manager()
if manager is None:
return set()
request = request_text.casefold()
local_notes = (
intent.selected_tools is None
and intent.required_read is None
and "notes" in intent.capabilities
and re.search(r"\b(?:local|on my computer|on this machine)\b", request)
)
write_verbs = {
"create": r"\b(?:create|add|make)\b",
"add": r"\b(?:add|create)\b",
"write": r"\bwrite\b",
"save": r"\bsave\b",
"update": r"\b(?:update|edit|modify|change)\b",
"edit": r"\b(?:edit|update|modify|change)\b",
"append": r"\bappend\b",
"modify": r"\b(?:modify|edit|update|change)\b",
}
negative_write = re.search(
r"\b(?:do not|don't|never|without|no)\s+(?:\w+\s+){0,2}"
r"(?:create|add|make|write|save|update|edit|append|modify)\b",
request,
)
declared = _local_mcp_tools(LOCAL_MCP_READ_TOOLS_ENV) | _local_mcp_tools(LOCAL_MCP_WRITE_TOOLS_ENV)
admitted = set()
for name in sorted(declared):
parts = name.split("__", 2)
if len(parts) != 3 or parts[0] != "mcp":
continue
_, server, bare = parts
if manager.is_builtin(server):
continue
conn = manager.get_server_status(server)
if conn.get("status") != "connected" or conn.get("transport") != "stdio":
continue
resource = manager.resource_identity(name)
if resource is None or resource.namespace != "mcp" or resource.endpoint_id != f"stdio:{server}":
continue
rows = manager._tools.get(server, [])
row = next((item for item in rows if item.get("name") == bare), None)
if row is None:
continue
read_ok = _is_declared_local_mcp_read_tool(name)
write_ok = _is_declared_local_mcp_write_tool(name)
if not (read_ok or write_ok):
continue
caps = capabilities_for_tool(name)
if not caps.known or caps.effects != frozenset({
ToolEffect.READ_PRIVATE if read_ok else ToolEffect.WRITE_PRIVATE
}):
continue
ann = row.get("annotations") or {}
destructive = ann.get("destructiveHint") if isinstance(ann, dict) else getattr(ann, "destructiveHint", None)
if destructive is True:
continue
if write_ok and re.match(r"^(?:delete|remove|erase|destroy|drop|purge)(?:_|$)", bare, re.I):
continue
# Exact qualified names in the trusted request permit any configured
# and validated local operation. Model/tool content is not an input.
explicitly_named = bool(re.search(
r"(?<![A-Za-z0-9_])" + re.escape(name.casefold()) + r"(?![A-Za-z0-9_])",
request,
))
if not explicitly_named:
if not local_notes or not re.search(r"(?:^|_)notes?(?:_|$)", bare, re.I):
continue
if write_ok:
action = bare.split("_", 1)[0].casefold()
if action not in write_verbs or not re.search(write_verbs[action], request):
continue
if negative_write:
continue
admitted.add(name)
return admitted
def create_request_authority(request_text, *, owner=None, session_id=None, workspace=None,
history=(), policy=None, active_document=False,
image_attachment=False, capabilities=None, client_runtime_context=None):
@@ -393,6 +488,7 @@ def create_request_authority(request_text, *, owner=None, session_id=None, works
tools = {canonical_tool(operation.tool)}
else:
operation = None
tools.update(_trusted_local_mcp_request_tools(request_text, intent))
grants = []
for name in sorted(tools | {"ask_user", "update_plan"}):
name = canonical_tool(name)
+37 -5
View File
@@ -10,7 +10,7 @@ import os
import json
import re
import time
from datetime import datetime
from datetime import datetime, timezone
from typing import Tuple
from src.auth_helpers import owner_filter
@@ -1957,6 +1957,23 @@ async def action_learn_sender_signatures(owner: str, **kwargs) -> Tuple[str, boo
return str(e), False
def _daily_brief_utc_boundary(local_dt, tz=None):
"""Convert a local wall-clock boundary using the offset at that boundary."""
if local_dt.tzinfo is None and tz is not None:
local_dt = local_dt.replace(tzinfo=tz)
return local_dt.astimezone(timezone.utc).replace(tzinfo=None)
def _daily_brief_local_start(event, tz=None):
"""Keep date-only/floating rows local; convert only stored UTC instants."""
start = event.dtstart
if event.is_utc and not event.all_day:
if start.tzinfo is None:
start = start.replace(tzinfo=timezone.utc)
return start.astimezone(tz).replace(tzinfo=None)
return start
async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
"""Build a short morning digest: today's calendar events, unread email count
+ top-N senders/subjects, active todos."""
@@ -1966,10 +1983,13 @@ async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
from core.database import SessionLocal, CalendarEvent, CalendarCal, Note
from routes.email_helpers import _imap_connect, _decode_header
from sqlalchemy import and_, or_
# ----- Calendar: today's events -----
today = _dt.now().replace(hour=0, minute=0, second=0, microsecond=0)
tomorrow = today + _td(days=1)
utc_today = _daily_brief_utc_boundary(today)
utc_tomorrow = _daily_brief_utc_boundary(tomorrow)
# v2 review HIGH-12: gate the OR-null branch on single-user
# (unconfigured) deploys only. In a multi-user deploy, one
# user's daily brief must not include another user's notes or
@@ -1982,13 +2002,25 @@ async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
db = SessionLocal()
try:
ev_q = db.query(CalendarEvent).join(CalendarCal).filter(
CalendarEvent.dtstart < tomorrow,
CalendarEvent.dtend > today,
or_(
and_(
CalendarEvent.is_utc.is_(True),
CalendarEvent.all_day.is_not(True),
CalendarEvent.dtstart < utc_tomorrow,
CalendarEvent.dtend > utc_today,
),
and_(
or_(CalendarEvent.is_utc.is_not(True), CalendarEvent.all_day.is_(True)),
CalendarEvent.dtstart < tomorrow,
CalendarEvent.dtend > today,
),
),
CalendarEvent.status != "cancelled",
)
if owner:
ev_q = owner_filter(ev_q, CalendarCal, owner, include_shared=_allow_null)
events = ev_q.order_by(CalendarEvent.dtstart).all()
events = ev_q.all()
events.sort(key=_daily_brief_local_start)
# ----- Notes: pinned + non-archived todos with at least one undone item -----
n_q = db.query(Note).filter(Note.archived == False) # noqa: E712
if owner:
@@ -2058,7 +2090,7 @@ async def action_daily_brief(owner: str, **kwargs) -> Tuple[str, bool]:
if events:
plain.append("Calendar:")
for e in events:
t = e.dtstart.strftime("%H:%M") if not e.all_day else "all day"
t = _daily_brief_local_start(e).strftime("%H:%M") if not e.all_day else "all day"
loc = f" @ {e.location}" if e.location else ""
plain.append(f" {t} {e.summary}{loc}")
plain.append("")
+7 -1
View File
@@ -282,7 +282,13 @@ class ChatHandler:
_sync_upload_vision_to_gallery(file_info, owner, vl_desc)
except Exception:
pass
enhanced_message = f"{enhanced_message}\n\n[Image: {file_info['name']}]\n{vl_desc}"
enhanced_message = f"{enhanced_message}\n\n[Image: {file_info['name']}]\n"
if vl_desc:
# A separate vision call wrote this, but the chat model sees
# one message and answers the prose as if the user wrote it
# (#5573). Label it below the marker line — the client strips
# the block by matching `[Image: <name>]\n`.
enhanced_message += f"[Image description — context only, not part of the user's message]:\n{vl_desc}"
# Surface the description to the client live so it renders as a
# collapsible "image description" on the user bubble (not just
# after a refresh that re-parses the stored message).
+31 -51
View File
@@ -31,63 +31,43 @@ def _internal_headers() -> dict:
async def _delete_endpoint_for_task(task: dict) -> None:
"""Drop the auto-registered model endpoint for a scheduled-stop serve.
"""Remove a stopped serve's endpoint using the normal deletion contract."""
import re
from urllib.parse import quote
Without this, killing the tmux session leaves the endpoint sitting in
the picker (probe goes offline; chats still try to route there) and
the user has to delete it by hand in Settings -> Endpoints.
"""
endpoint_id = (task.get("_endpointId") or task.get("endpointId") or "").strip()
if not endpoint_id:
logger.info(
"cookbook_serve_lifecycle: task %s has no endpoint id; skipping endpoint deletion",
task.get("sessionId") or task.get("id") or "",
)
endpoint_id = str(task.get("_endpointId") or task.get("endpointId") or "").strip()
serve_session_id = str(task.get("sessionId") or task.get("id") or "").strip()
if (
not re.fullmatch(r"(?:local|img)-[0-9a-f]{8}", endpoint_id)
or not serve_session_id
):
logger.info("cookbook_serve_lifecycle: missing endpoint/session identity")
return
import re as _re
payload = task.get("payload") or {}
cmd = str(payload.get("_cmd") or "")
remote = task.get("remoteHost") or ""
# Build host the same way _auto_register_llm_endpoint does so URL match wins.
if remote:
host = remote.split("@")[-1] if "@" in remote else remote
else:
host = "host.docker.internal"
port_match = _re.search(r"--port\s+(\d+)", cmd)
ollama_host_match = _re.search(r"OLLAMA_HOST=[^\s]*?:(\d+)", cmd)
if port_match:
port = int(port_match.group(1))
elif ollama_host_match:
port = int(ollama_host_match.group(1))
elif "ollama" in cmd:
port = 11434
else:
port = 8080
base_url = f"http://{host}:{port}/v1"
try:
async with httpx.AsyncClient(timeout=8) as client:
r = await client.get(
f"{internal_api_base()}/api/model-endpoints",
response = await client.delete(
f"{internal_api_base()}/api/model-endpoints/{quote(endpoint_id, safe='')}",
params={"expected_cookbook_serve_session_id": serve_session_id},
headers=_internal_headers(),
)
if r.status_code >= 400:
return
eps = r.json() if r.content else []
# Delete only the endpoint created by this scheduled serve. URL
# matching is unsafe because a later scheduled serve can reuse the
# same host:port after an older task has gone stale.
ep = next((e for e in eps if e.get("id") == endpoint_id), None)
if ep:
await client.delete(
f"{internal_api_base()}/api/model-endpoints/{ep['id']}",
headers=_internal_headers(),
)
logger.info(
f"cookbook_serve_lifecycle: deleted endpoint {ep.get('id')} "
f"({ep.get('base_url')}) after scheduled stop"
)
except Exception as e:
logger.warning(f"cookbook_serve_lifecycle: endpoint delete failed: {e}")
if response.status_code == 200:
logger.info(
"cookbook_serve_lifecycle: deleted endpoint %s for serve %s",
endpoint_id, serve_session_id,
)
elif response.status_code in (404, 409):
logger.info(
"cookbook_serve_lifecycle: endpoint %s absent or reassigned; keeping it",
endpoint_id,
)
else:
logger.warning(
"cookbook_serve_lifecycle: endpoint %s deletion returned HTTP %s",
endpoint_id, response.status_code,
)
except Exception as exc:
logger.warning("cookbook_serve_lifecycle: cleanup failed: %s", exc)
async def _stop_serve(session_id: str, remote_host: str = "", ssh_port: str = "") -> bool:
+4 -1
View File
@@ -142,7 +142,10 @@ def _process_pdf(
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
temp_img_path = tmp.name
try:
img.image.save(temp_img_path, "PNG") # pypdf -> PIL image
image = img.image
if image.mode == "CMYK":
image = image.convert("RGB")
image.save(temp_img_path, "PNG")
ocr_text = analyze_image_with_vl(temp_img_path, owner=owner)
if ocr_text and "unavailable" not in ocr_text.lower():
pdf_text += f"\n\n[Page {page_num + 1} image {img_index + 1} text]: {ocr_text}"
+4 -1
View File
@@ -770,8 +770,11 @@ class McpManager:
qualified.add(f"mcp__{server_id}__{tool['name']}")
return disabled_map, qualified
def is_builtin(self, server_id: str) -> bool:
@staticmethod
def is_builtin(server_id: str) -> bool:
"""Check if a server is a built-in (auto-registered) server."""
if not isinstance(server_id, str):
return False
return server_id.startswith("builtin_") or server_id in {
"image_gen",
"memory",
+63 -8
View File
@@ -76,9 +76,23 @@ _ERROR_DETAIL = {
"auth_or_protocol_error": "authentication or protocol error",
"no_models": "endpoint returned no models",
"no_host": "no host configured",
"config_source_error": "configuration source could not be read",
"error": "probe failed",
}
# Config/inventory source behind each network subsystem. `_gather_inputs()`
# fails soft, so a source that could not be read hands its probe an empty
# inventory — indistinguishable from "nothing configured", which every probe
# answers with `disabled`. The collector therefore reports the source failure
# instead of running a probe whose verdict would be a lie.
CONFIG_SOURCE_ERROR = "config_source_error"
_SUBSYSTEM_SOURCE = {
"searxng": "settings",
"ntfy": "integrations",
"email": "accounts",
"providers": "endpoints",
}
def _svc(name: str, status: str, detail: str, **meta: Any) -> Dict[str, Any]:
return {"name": name, "status": status, "detail": detail, "meta": dict(meta)}
@@ -411,26 +425,33 @@ def _gather_inputs() -> Dict[str, Any]:
"""Pull live config/account/endpoint lists from the app's data sources.
Each lookup fails soft: a broken source yields an empty/neutral value so a
single failure can't take down the whole health report.
single failure can't take down the whole health report. The failure is also
*recorded* under `failed` (source -> controlled category), because the empty
value alone cannot be told apart from a source that loaded and is genuinely
empty — and the probes read empty as `disabled`.
"""
settings: Dict[str, Any] = {}
integrations: List[Dict[str, Any]] = []
accounts: List[Dict[str, Any]] = []
endpoints: List[Dict[str, Any]] = []
failed: Dict[str, str] = {}
try:
from src.settings import load_settings
settings = load_settings() or {}
except Exception as e:
failed["settings"] = _classify_error(e)
logger.debug(f"service_health: settings load failed: {e}")
try:
from src.integrations import load_integrations
integrations = load_integrations() or []
except Exception as e:
failed["integrations"] = _classify_error(e)
logger.debug(f"service_health: integrations load failed: {e}")
try:
from routes.email_helpers import _list_email_accounts
accounts = _list_email_accounts() or []
except Exception as e:
failed["accounts"] = _classify_error(e)
logger.debug(f"service_health: email accounts load failed: {e}")
try:
from core.database import SessionLocal, ModelEndpoint
@@ -443,9 +464,10 @@ def _gather_inputs() -> Dict[str, Any]:
finally:
db.close()
except Exception as e:
failed["endpoints"] = _classify_error(e)
logger.debug(f"service_health: endpoint load failed: {e}")
return {"settings": settings, "integrations": integrations,
"accounts": accounts, "endpoints": endpoints}
"accounts": accounts, "endpoints": endpoints, "failed": failed}
async def _run_subsystem(name: str, fn: Callable, *args: Any) -> Dict[str, Any]:
@@ -464,6 +486,24 @@ async def _run_subsystem(name: str, fn: Callable, *args: Any) -> Dict[str, Any]:
return _svc(name, DOWN, _detail_for(category), error=category)
async def _source_unavailable(name: str, source: str,
category: str) -> Dict[str, Any]:
"""Controlled entry for a subsystem whose config source could not be read.
Deliberately not `disabled`: the configured state is *unknown*, not known to
be off. `degraded` rather than `down` because the failure is in reading the
inventory, not a probe that reached the subsystem and failed — but it still
carries non-zero severity, so a report containing one can never roll up to
`ok`. `meta` names the source and the controlled category of the underlying
exception; never its text.
"""
return _svc(name, DEGRADED,
f"Could not read configuration "
f"({_detail_for(CONFIG_SOURCE_ERROR)}).",
error=CONFIG_SOURCE_ERROR, source=source,
source_error=category)
async def collect_service_health(rag_manager: Any = None,
memory_vector: Any = None) -> Dict[str, Any]:
"""Run every probe and return {overall, services, timestamp}.
@@ -472,6 +512,10 @@ async def collect_service_health(rag_manager: Any = None,
four network subsystems run concurrently, each under `_SUBSYSTEM_DEADLINE`,
with an overall `_AGGREGATE_DEADLINE` backstop. Per-item probes inside
providers/email are themselves bounded by `_FANOUT_BUDGET`.
A subsystem whose config/inventory source failed to load is reported via
`_source_unavailable()` instead of being probed with an empty inventory,
which every probe would otherwise report as `disabled`.
"""
from datetime import datetime, timezone
@@ -482,12 +526,23 @@ async def collect_service_health(rag_manager: Any = None,
chroma = chromadb_health(rag_manager, memory_vector)
names = ["searxng", "ntfy", "email", "providers"]
coros = [
_run_subsystem("searxng", searxng_health, settings),
_run_subsystem("ntfy", ntfy_health, inputs["integrations"], settings),
_run_subsystem("email", email_health, inputs["accounts"]),
_run_subsystem("providers", providers_health, inputs["endpoints"]),
]
probes = {
"searxng": (searxng_health, (settings,)),
"ntfy": (ntfy_health, (inputs["integrations"], settings)),
"email": (email_health, (inputs["accounts"],)),
"providers": (providers_health, (inputs["endpoints"],)),
}
# A subsystem whose inventory could not be read is reported as such. The
# others are still probed normally — one broken source degrades one entry.
failed = inputs.get("failed") or {}
coros = []
for name in names:
source = _SUBSYSTEM_SOURCE[name]
if source in failed:
coros.append(_source_unavailable(name, source, failed[source]))
else:
fn, args = probes[name]
coros.append(_run_subsystem(name, fn, *args))
try:
results = await asyncio.wait_for(asyncio.gather(*coros),
timeout=_AGGREGATE_DEADLINE)
+33 -5
View File
@@ -57,25 +57,53 @@ def _message_to_context(msg: DBChatMessage) -> dict[str, Any]:
}
def _escape_like(value: str) -> str:
def escape_like_pattern(value: str) -> str:
"""Escape SQL LIKE wildcards (``%``, ``_``) and the escape char itself.
Public shared helper for building ``ilike(f"%{...}%", escape="\\\\")``
patterns from user input. Must escape backslash first.
"""
return value.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
def _escape_like(value: str) -> str:
return escape_like_pattern(value)
def _snippet(content: str, query: str, radius: int = 60) -> str:
content = content or ""
query = query or ""
if not query:
return content[: radius * 2]
idx = content.lower().find(query.lower())
if idx == -1:
match_start = -1
match_length = 0
lowered_content = content.lower()
for candidate in _query_terms(query):
idx = lowered_content.find(candidate.lower())
if idx != -1 and (match_start == -1 or idx < match_start):
match_start = idx
match_length = len(candidate)
if match_start == -1:
return content[: radius * 2]
start = max(0, idx - radius)
end = min(len(content), idx + len(query) + radius)
start = max(0, match_start - radius)
end = min(len(content), match_start + match_length + radius)
return ("..." if start > 0 else "") + content[start:end] + ("..." if end < len(content) else "")
def _query_terms(query: str) -> list[str]:
"""Return meaningful quoted phrases and tokens in user-entered order."""
terms: list[str] = []
for match in re.finditer(r'"([^"]+)"|[\w][\w._-]*', query, flags=re.UNICODE):
term = match.group(1) if match.group(1) is not None else match.group(0).strip("._-")
term = term.strip()
if term:
terms.append(term)
return terms
def _sanitize_fts_query(query: str) -> str | None:
"""Convert free text into a conservative FTS5 MATCH query.
+317
View File
@@ -334,6 +334,142 @@ _BROWSER_MCP_READ_TOOLS = frozenset(
)
LOCAL_MCP_READ_TOOLS_ENV = "ODYSSEUS_LOCAL_MCP_READ_TOOLS"
LOCAL_MCP_WRITE_TOOLS_ENV = "ODYSSEUS_LOCAL_MCP_WRITE_TOOLS"
_LOCAL_MCP_READ_CAPABILITIES = _capabilities(
ToolEffect.READ_PRIVATE,
result_integrity=ResultIntegrity.EXTERNAL_UNTRUSTED,
)
_LOCAL_MCP_WRITE_CAPABILITIES = _capabilities(
ToolEffect.WRITE_PRIVATE,
result_integrity=ResultIntegrity.EXTERNAL_UNTRUSTED,
)
def _local_mcp_tools(variable: str) -> frozenset[str]:
"""Tool names a deployment declares as touching only the local store.
Tools served by a user-configured MCP server are absent from the registry
above, so they are classified as unknown and fail high. For a server with
both a read and a write tool that is unusable: the read result arms the
post-external gate, because an unknown result is not SYSTEM integrity, and
the write that follows is refused until separately authorized. Every
second call needs an approval, for a server that never leaves the host.
Declaring the tools resolves that without widening the gate:
ODYSSEUS_LOCAL_MCP_READ_TOOLS=mcp__<server>__search_notes,mcp__<server>__read_note
ODYSSEUS_LOCAL_MCP_WRITE_TOOLS=mcp__<server>__create_note
Read tools become READ_PRIVATE with EXTERNAL_UNTRUSTED integrity. Their
results remain untrusted and still arm the gate, preventing prompt
injections from reaching shell, network, or unrelated tools. However,
declared write tools on the exact same MCP server receive a narrowly
scoped continuation exception as long as no external or error taint has
entered the run. Once genuinely external content has entered the run,
approvals remain required.
"""
raw = os.getenv(variable, "") or ""
return frozenset(name.strip() for name in raw.split(",") if name.strip())
def _current_mcp_manager():
"""Retrieve the current McpManager instance safely without hard circular binds."""
try:
from src import tool_execution
if hasattr(tool_execution, "get_mcp_manager"):
mgr = tool_execution.get_mcp_manager()
if mgr is not None:
return mgr
except Exception:
pass
try:
from src.tool_utils import get_mcp_manager
return get_mcp_manager()
except Exception:
return None
def _is_declared_local_mcp_tool(
tool_name: Any, server_id: str | None = None
) -> bool:
"""Return whether tool_name is an exact, well-formed declared local MCP tool."""
if not isinstance(tool_name, str) or not tool_name:
return False
parts = tool_name.split("__", 2)
if not (
len(parts) == 3
and parts[0] == "mcp"
and bool(parts[1])
and bool(parts[2])
and not any(char.isspace() for part in parts[1:] for char in part)
):
return False
sid = parts[1]
if server_id is not None and sid != server_id:
return False
from src.mcp_manager import McpManager
if McpManager.is_builtin(sid):
return False
if tool_name in TOOL_CAPABILITIES:
return False
declared_read = tool_name in _local_mcp_tools(LOCAL_MCP_READ_TOOLS_ENV)
declared_write = tool_name in _local_mcp_tools(LOCAL_MCP_WRITE_TOOLS_ENV)
return (declared_read or declared_write) and (declared_read != declared_write)
def _is_declared_local_mcp_read_tool(
tool_name: Any, server_id: str | None = None
) -> bool:
if not _is_declared_local_mcp_tool(tool_name, server_id):
return False
declared_read = tool_name in _local_mcp_tools(LOCAL_MCP_READ_TOOLS_ENV)
declared_write = tool_name in _local_mcp_tools(LOCAL_MCP_WRITE_TOOLS_ENV)
if not (declared_read and not declared_write):
return False
parts = tool_name.split("__", 2)
mgr = _current_mcp_manager()
if mgr is not None:
tools = mgr._tools.get(parts[1], [])
tool_row = next((r for r in tools if r.get("name") == parts[2]), None)
if tool_row is not None:
from src.mcp_manager import mcp_tool_is_readonly
if not mcp_tool_is_readonly(tool_row):
return False
else:
from src.mcp_manager import _MCP_READONLY_VERBS
if not parts[2].lower().startswith(_MCP_READONLY_VERBS):
return False
return True
def _is_declared_local_mcp_write_tool(
tool_name: Any, server_id: str | None = None
) -> bool:
if not _is_declared_local_mcp_tool(tool_name, server_id):
return False
declared_read = tool_name in _local_mcp_tools(LOCAL_MCP_READ_TOOLS_ENV)
declared_write = tool_name in _local_mcp_tools(LOCAL_MCP_WRITE_TOOLS_ENV)
if not (declared_write and not declared_read):
return False
parts = tool_name.split("__", 2)
mgr = _current_mcp_manager()
if mgr is not None:
tools = mgr._tools.get(parts[1], [])
tool_row = next((r for r in tools if r.get("name") == parts[2]), None)
if tool_row is not None:
ann = tool_row.get("annotations") or {}
destructive = (
ann.get("destructiveHint")
if isinstance(ann, dict)
else getattr(ann, "destructiveHint", None)
)
if destructive is True:
return False
return True
def capabilities_for_tool(tool_name: Any) -> ToolCapabilities:
"""Return deterministic capabilities; malformed and unknown tools fail high."""
if not isinstance(tool_name, str) or not tool_name:
@@ -348,6 +484,10 @@ def capabilities_for_tool(tool_name: Any) -> ToolCapabilities:
return capabilities
if tool_name in _BROWSER_MCP_READ_TOOLS:
return _BROWSER_MCP_READ_CAPABILITIES
if _is_declared_local_mcp_read_tool(tool_name):
return _LOCAL_MCP_READ_CAPABILITIES
if _is_declared_local_mcp_write_tool(tool_name):
return _LOCAL_MCP_WRITE_CAPABILITIES
return _UNKNOWN_CAPABILITIES
@@ -785,6 +925,149 @@ def delegated_tool_is_blocked(tool_name: Any, content: Any = None) -> bool:
)
_SERVER_OWNED_CONTROL_PLANE_SOURCES = frozenset(
{
"skills",
"client runtime context",
"backend runtime context",
"integrations",
"mcp tools",
"agents.md",
}
)
_PROHIBITED_CONTINUATION_EFFECTS = frozenset(
{
ToolEffect.NETWORK_EGRESS,
ToolEffect.EXTERNAL_SIDE_EFFECT,
ToolEffect.EXECUTE_CODE,
ToolEffect.ADMIN_CHANGE,
ToolEffect.UI_SIDE_EFFECT,
ToolEffect.DESTRUCTIVE,
ToolEffect.READ_WORKSPACE,
ToolEffect.WRITE_WORKSPACE,
ToolEffect.BROKERED_NETWORK_READ,
ToolEffect.READ_PUBLIC,
}
)
def _is_authorized_local_mcp_continuation(
tool_name: Any,
external_sources: Iterable[str],
content: Any = None,
) -> bool:
"""Evaluate narrowly scoped continuation for declared local MCP tools.
Allows a declared local MCP write or read tool when the tool is verified
against an explicitly configured and connected stdio MCP server, and the
only untrusted sources observed in the run are declared local MCP tools
belonging to the exact same MCP server (plus server-owned control plane
metadata).
"""
if not isinstance(tool_name, str) or not tool_name:
return False
parts = tool_name.split("__", 2)
if len(parts) != 3 or parts[0] != "mcp" or not parts[1] or not parts[2]:
return False
server_id = parts[1]
from src.mcp_manager import McpManager
if McpManager.is_builtin(server_id):
return False
if not _is_declared_local_mcp_tool(tool_name, server_id=server_id):
return False
capabilities = capabilities_for_action(tool_name, content)
if not capabilities.known:
return False
if capabilities.effects & _PROHIBITED_CONTINUATION_EFFECTS:
return False
if capabilities.effects - {ToolEffect.WRITE_PRIVATE, ToolEffect.READ_PRIVATE}:
return False
manager = _current_mcp_manager()
if manager is None:
return False
if manager.is_builtin(server_id):
return False
conn = manager.get_server_status(server_id)
if conn.get("status") != "connected":
return False
if conn.get("transport") != "stdio":
return False
resource_id = manager.resource_identity(tool_name)
if resource_id is None or resource_id.namespace != "mcp":
return False
if resource_id.endpoint_id != f"stdio:{server_id}":
return False
tools_list = manager._tools.get(server_id, [])
tool_row = next((r for r in tools_list if r.get("name") == parts[2]), None)
if not tool_row:
return False
ann = tool_row.get("annotations") or {}
destructive = (
ann.get("destructiveHint")
if isinstance(ann, dict)
else getattr(ann, "destructiveHint", None)
)
if destructive is True:
return False
from src.agent_runtime.authority import active_request_authority, ExactOperation, RequestAuthority
authority = active_request_authority()
if not isinstance(authority, RequestAuthority):
return False
if authority is not None:
try:
operation_input = content if isinstance(content, str) else "{}"
operation = ExactOperation.normalize(tool_name, operation_input)
if not authority.permits(operation):
return False
except (ValueError, TypeError):
return False
if resource_id not in authority.backend_resources:
return False
sources = list(external_sources or ())
if not sources:
return False
from src.mcp_manager import mcp_tool_is_readonly
has_local_tool = False
for source in sources:
if source in _SERVER_OWNED_CONTROL_PLANE_SOURCES:
continue
if not _is_declared_local_mcp_tool(source, server_id=server_id):
return False
source_caps = capabilities_for_tool(source)
if not source_caps.known or (source_caps.effects & _PROHIBITED_CONTINUATION_EFFECTS):
return False
if source_caps.effects - {ToolEffect.READ_PRIVATE, ToolEffect.WRITE_PRIVATE}:
return False
source_resource = manager.resource_identity(source)
if source_resource is None or source_resource.endpoint_id != resource_id.endpoint_id:
return False
if not str(source_resource.endpoint_id).startswith("stdio:"):
return False
source_parts = source.split("__", 2)
source_row = next((r for r in tools_list if r.get("name") == source_parts[2]), None)
if not source_row:
return False
if _is_declared_local_mcp_read_tool(source, server_id=server_id) and not mcp_tool_is_readonly(source_row):
return False
has_local_tool = True
return has_local_tool
@dataclass
class ToolRunSecurityContext:
"""Server-owned integrity state for one agent run."""
@@ -804,6 +1087,19 @@ class ToolRunSecurityContext:
# Driven by a bearer API token, not a person at a browser. Privileged
# tools are refused outright and no approval can lift that.
delegated_credential: bool = False
explicit_untrusted_seen: bool = False
prompt_external_untrusted_seen: bool = False
def __post_init__(self) -> None:
if self.external_untrusted_context_seen and (
not self.external_sources
or any(
not _is_declared_local_mcp_tool(s)
and s not in _SERVER_OWNED_CONTROL_PLANE_SOURCES
for s in self.external_sources
)
):
self.prompt_external_untrusted_seen = True
def observe_messages(self, messages: Iterable[dict]) -> None:
"""Apply server-owned chat scope and promote untrusted prompt context."""
@@ -834,8 +1130,10 @@ class ToolRunSecurityContext:
if source and source not in self.external_sources:
self.external_sources.append(source)
self.external_untrusted_context_seen = True
self.prompt_external_untrusted_seen = True
if messages_contain_external_untrusted_context(message_list):
self.external_untrusted_context_seen = True
self.prompt_external_untrusted_seen = True
def decision_for(self, tool_name: Any, content: Any = None) -> ToolGateDecision:
# Checked before the bypasses below, because neither may lift it, and
@@ -858,6 +1156,20 @@ class ToolRunSecurityContext:
if not self.external_untrusted_context_seen:
return ToolGateDecision(True)
# Scoped continuation exception for explicitly declared local MCP tools:
# A declared write or read tool on the same MCP server is allowed if
# the run's only untrusted context was introduced by declared local
# tools on that server, without any explicit/error taint or external
# message context.
if (
not self.prompt_external_untrusted_seen
and not self.explicit_untrusted_seen
and _is_authorized_local_mcp_continuation(
tool_name, self.external_sources, content=content
)
):
return ToolGateDecision(True)
# Skills and other server-owned descriptors are control-plane metadata
# rather than external result content; web/document/tool-result taint
# still gates actions that can mutate state or execute code.
@@ -906,6 +1218,11 @@ class ToolRunSecurityContext:
if not tool_result_should_arm_gate(tool_name, result, content):
return
self.external_untrusted_context_seen = True
if isinstance(result, dict) and (
result.get("untrusted_content") is True
or not tool_result_is_successful(result)
):
self.explicit_untrusted_seen = True
if isinstance(tool_name, str) and tool_name not in self.external_sources:
self.external_sources.append(tool_name)
+11 -4
View File
@@ -1917,10 +1917,17 @@ async def execute_tool_block(
)
if isinstance(security_context, ToolRunSecurityContext) and not approval_claimed:
decision = security_context.decision_for(
getattr(block, "tool_type", None),
getattr(block, "content", None),
)
if isinstance(authority, RequestAuthority):
with bind_request_authority(authority):
decision = security_context.decision_for(
getattr(block, "tool_type", None),
getattr(block, "content", None),
)
else:
decision = security_context.decision_for(
getattr(block, "tool_type", None),
getattr(block, "content", None),
)
if not decision.allowed:
logger.warning(
"External-context policy blocked tool=%r",
+6
View File
@@ -6440,6 +6440,12 @@ def resolve_turn_contract(*, capabilities: Iterable[str], schemas: Iterable[dict
if 'search_emails' in selected:
selected.update({'read_email', 'download_attachment', 'list_email_accounts'})
selected.update(canonical_tool(n) for n in always_available_tools)
# Offer only dynamic MCP operations already sealed by trusted request
# admission. Tool discovery and model routing never create these grants.
from src.agent_runtime.authority import active_request_authority
sealed_authority = active_request_authority()
if sealed_authority is not None:
selected.update(g.tool for g in sealed_authority.grants if g.tool.startswith("mcp__"))
selected.update(canonical_tool(n) for n in warm_tools if str(n or "").strip())
# Controls are neutral; enabling Web is permission, never a requested family.
if selected:
+39
View File
@@ -52,6 +52,45 @@
gap: 8px;
}
.admin-user-name { font-size: 13px; font-weight: 500; }
/* Inline endpoint rename: the input keeps the name's typography so the row doesn't shift */
.adm-ep-rename-btn {
background: none;
border: none;
padding: 0 2px;
color: inherit;
line-height: 1;
cursor: pointer;
opacity: 0.45;
}
.adm-ep-rename-btn:hover,
.adm-ep-rename-btn:focus-visible { opacity: 0.9; }
@media (hover: hover) {
.adm-ep-rename-btn { opacity: 0; }
.admin-user-row:hover .adm-ep-rename-btn { opacity: 0.45; }
.admin-user-row .adm-ep-rename-btn:hover,
.adm-ep-rename-btn:focus-visible { opacity: 0.9; }
}
.admin-user-row input.adm-ep-rename-input {
width: auto;
field-sizing: content;
max-width: 100%;
height: auto;
padding: 0 0 1px;
line-height: inherit;
color: inherit;
background: transparent;
border: none;
border-bottom: 1px solid var(--red);
border-radius: 0;
outline: none;
}
.admin-user-info:has(.adm-ep-name, .adm-ep-rename-input) { min-width: 0; }
.adm-ep-name {
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* Privilege panel */
.admin-priv-panel {
max-height: 600px;
+66 -1
View File
@@ -1074,7 +1074,8 @@ async function loadEndpoints() {
<span class="adm-check-dot adm-endpoint-select-dot" aria-hidden="true"></span>
</label>` : ''}
<span class="adm-ep-row-logo" style="display:inline-flex;align-items:center;justify-content:center;width:16px;height:16px;flex-shrink:0;opacity:0.9;">${providerLogoFromUrl(ep.base_url) || ''}</span>
<span class="admin-user-name">${esc(epTitle)}</span>
<span class="admin-user-name adm-ep-name" title="${esc(epTitle)}">${esc(epTitle)}</span>
${_endpointSelectMode || (isChatgptAccount && ep.account_label) ? '' : `<button type="button" class="adm-ep-rename-btn" data-adm-rename-ep="${esc(ep.id)}" data-adm-ep-title="${esc(epTitle)}" title="Rename" aria-label="Rename endpoint"><svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 3a2.83 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/></svg></button>`}
${ep.model_type === 'image' ? '<span class="admin-badge" style="background:color-mix(in srgb, var(--accent) 20%, transparent);color:var(--accent);">Image</span>' : ''}
${kindLabel ? `<span class="admin-badge">${esc(kindLabel)}</span>` : ''}
${statusBadge}
@@ -1204,6 +1205,70 @@ async function loadEndpoints() {
loadEndpoints();
});
});
queryAll('[data-adm-rename-ep]').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
const nameEl = btn.previousElementSibling;
if (!nameEl || !nameEl.classList.contains('admin-user-name')) return;
const original = btn.dataset.admEpTitle || '';
const input = document.createElement('input');
input.type = 'text';
input.value = original;
input.placeholder = 'Endpoint name';
input.setAttribute('aria-label', 'Endpoint name');
input.maxLength = 64;
input.autocomplete = 'off';
input.setAttribute('data-bwignore', '');
input.className = 'admin-user-name adm-ep-rename-input';
input.style.minWidth = `min(${nameEl.getBoundingClientRect().width}px, 100%)`;
nameEl.replaceWith(input);
btn.style.visibility = 'hidden';
input.focus();
input.select();
let done = false;
const restore = () => {
input.replaceWith(nameEl);
btn.style.visibility = '';
};
const commit = async () => {
if (done) return;
done = true;
const name = input.value.trim();
if (!name || name === original) { restore(); return; }
const epId = btn.dataset.admRenameEp;
if (data.some(ep => String(ep.id) !== epId && (ep.name || '').trim().toLowerCase() === name.toLowerCase())) {
uiModule.showError(`An endpoint named "${name}" already exists`);
done = false;
input.focus();
return;
}
input.disabled = true;
try {
const res = await fetch(`/api/model-endpoints/${encodeURIComponent(epId)}`, {
method: 'PATCH',
credentials: 'same-origin',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name }),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(data.detail || 'Failed to rename endpoint');
}
} catch (err) {
uiModule.showError(err.message || 'Failed to rename endpoint');
restore();
return;
}
await _refreshAfterEndpointChange();
loadEndpoints();
};
input.addEventListener('blur', commit);
input.addEventListener('keydown', (ev) => {
if (ev.key === 'Enter') { ev.preventDefault(); input.blur(); }
if (ev.key === 'Escape') { ev.preventDefault(); ev.stopPropagation(); done = true; restore(); }
});
});
});
queryAll('[data-adm-copy-url]').forEach(btn => {
btn.addEventListener('click', (e) => {
e.stopPropagation();
+159 -50
View File
@@ -336,6 +336,9 @@ import { invalidateSettings } from './appConfig.js';
let _sendInFlight = false; // covers the window from click → streaming start
let _displayOverride = null; // Override visible user bubble text (hides injected prompts)
let _hideUserBubble = false; // Skip user bubble entirely (e.g. continue after stop)
// The user bubble a regenerate keeps on screen. Its resubmitted message is
// a new DB row, so the saved id is attached here.
let _regenKeptUserBubble = null;
let _contextHeaderSeq = 0;
let _contextHeaderData = null;
let _contextHeaderBound = false;
@@ -4995,6 +4998,15 @@ import { invalidateSettings } from './appConfig.js';
refreshChatContextHeader('metrics');
}
} else if (json.type === 'user_message_saved') {
// The user bubble gets its DB id too, so regenerating or
// editing it later cuts the chat by id, not page position.
if (!_isBg && json.id) {
if (_userMsgEl) _userMsgEl.dataset.dbId = json.id;
else if (_regenKeptUserBubble) _regenKeptUserBubble.dataset.dbId = json.id;
_regenKeptUserBubble = null;
}
} else if (json.type === 'message_saved') {
// Wire the persisted DB id onto the just-streamed bubble so it
// can be edited/deleted immediately, without reloading the chat.
@@ -7236,6 +7248,22 @@ import { invalidateSettings } from './appConfig.js';
/**
* Edit a user message: show an input, truncate to before it, resubmit the edited text.
*/
// Recover attached file IDs from rendered user bubbles, including older
// image previews that were rendered before data-file-id was stamped.
function _userMessageAttachmentIds(msgElement) {
if (!msgElement) return [];
const els = msgElement.querySelectorAll('[data-file-id]');
const ids = Array.from(els).map(el => el.dataset.fileId).filter(Boolean);
if (!ids.length) {
const images = msgElement.querySelectorAll('.attach-image-preview img, .attach-card img');
for (const image of images) {
const match = (image.getAttribute('src') || '').match(/\/api\/upload\/([A-Za-z0-9_\-]+)/);
if (match && match[1] && !ids.includes(match[1])) ids.push(match[1]);
}
}
return ids;
}
export async function editUserMessage(userMsgElement) {
const box = document.getElementById('chat-history');
const allMsgs = Array.from(box.querySelectorAll('.msg'));
@@ -7283,23 +7311,49 @@ import { invalidateSettings } from './appConfig.js';
e.stopPropagation();
const newText = editor.value.trim();
if (!newText) return;
const editAttachmentIds = _userMessageAttachmentIds(userMsgElement);
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
// Everything after the original reply to this message would also be
// destroyed. Cancelling leaves the editor open with the draft intact.
const extraCount = allMsgs.length - msgIndex - 2;
const choice = await _confirmTruncateIfDestructive(extraCount, 'edit');
if (!choice) return;
if (choice === 'fork') {
// Branch just before this user turn and send the edit there; this
// chat keeps the original message and everything after it.
try {
await _forkAndSend(msgIndex, userMsgElement.dataset.dbId || '', newText, editAttachmentIds);
bodyEl.innerHTML = originalHTML;
} catch (err) {
console.error('Fork failed:', err);
if (uiModule) uiModule.showError('Fork failed: ' + err.message);
}
return;
}
const keepCount = msgIndex;
// Cut at the message's database id: msgIndex counts only the messages
// loaded in the page, so it is too small when older history has not
// been scrolled in yet.
const editBeforeId = userMsgElement.dataset.dbId || '';
try {
await fetch(`${API_BASE}/api/session/${sessionId}/truncate`, {
const editTruncate = await fetch(`${API_BASE}/api/session/${sessionId}/truncate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount })
body: JSON.stringify(editBeforeId ? { before_msg_id: editBeforeId } : { keep_count: keepCount })
});
if (!editTruncate.ok) throw new Error('Server error ' + editTruncate.status);
// Remove DOM elements from msgIndex onward
for (let i = allMsgs.length - 1; i >= msgIndex; i--) {
allMsgs[i].remove();
}
// The edited user turn still refers to its original attachments.
_pendingRegenAttachments = editAttachmentIds;
// Submit the edited text
const messageInput = uiModule.el('message');
messageInput.value = newText;
@@ -7323,6 +7377,59 @@ import { invalidateSettings } from './appConfig.js';
});
}
// Regenerate, resend and edit permanently delete (server-side truncate)
// everything after the point clicked. Ask first unless nothing comes after
// it (extraCount 0), offering a fork that leaves this chat untouched.
// Resolves to 'proceed', 'fork', or null (cancelled).
const _TRUNCATE_ACTIONS = {
regenerate: { lead: 'Regenerating here', confirmText: 'Delete and regenerate' },
resend: { lead: 'Resending this message', confirmText: 'Delete and resend' },
edit: { lead: 'Sending this edit', confirmText: 'Delete and send' },
};
async function _confirmTruncateIfDestructive(extraCount, action = 'regenerate') {
if (extraCount <= 0) return 'proceed';
const { lead, confirmText } = _TRUNCATE_ACTIONS[action] || _TRUNCATE_ACTIONS.regenerate;
const result = await uiModule.styledConfirm(
`${lead} will also permanently delete the ${extraCount} ` +
`message${extraCount === 1 ? '' : 's'} after it. This can't be undone. ` +
`Fork from here keeps this chat as it is and continues in a new one instead.`,
{ confirmText, alternateText: 'Fork from here', cancelText: 'Cancel', danger: true }
);
if (result === 'alternate') return 'fork';
return result ? 'proceed' : null;
}
// Copy the first `keepCount` messages of the current chat into a new chat
// and switch to it. Returns the new session id; throws on failure so callers
// that replay an action in the fork can stop before sending anything.
// Fork by message id when the element has one (before it, or through it);
// keepCount is only a fallback, since it counts loaded messages only.
async function _forkSessionAt(keepCount, beforeMsgId = '', throughMsgId = '') {
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) throw new Error('No active chat');
const res = await fetch(`${API_BASE}/api/session/${sessionId}/fork`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount,
...(beforeMsgId ? { before_msg_id: beforeMsgId } : throughMsgId ? { through_msg_id: throughMsgId } : {}) }),
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
await sessionModule.loadSessions();
await sessionModule.selectSession(data.id);
if (uiModule) uiModule.showToast(`Forked → ${data.name}`);
return data.id;
}
// Fork before a user message and send `text` there; this chat is untouched.
// `attachments` (if given) are re-sent only once the fork has succeeded.
async function _forkAndSend(keepCount, beforeMsgId, text, attachments) {
await _forkSessionAt(keepCount, beforeMsgId);
if (attachments !== undefined) _pendingRegenAttachments = attachments;
uiModule.el('message').value = text;
document.querySelector('.send-btn')?.click();
}
/**
* Resend a user message. By default this replaces from the selected user turn
* so failed attempts do not remain in model context or SFT traces. Callers can
@@ -7344,18 +7451,7 @@ import { invalidateSettings } from './appConfig.js';
let text = (userMsgElement.dataset.raw || (bodyEl ? bodyEl.textContent : '') || '').trim();
text = text.replace(/\s*\[\d+ attachment\(s\)\]$/, '');
// Collect file_ids attached to this user message so the resend re-carries
// the photos / docs (and the chat handler picks up the user-edited OCR
// text cached server-side under those file ids).
const _attachEls = userMsgElement.querySelectorAll('[data-file-id]');
let _ids = Array.from(_attachEls).map(el => el.dataset.fileId).filter(Boolean);
if (!_ids.length) {
const _imgs = userMsgElement.querySelectorAll('.attach-image-preview img, .attach-card img');
for (const _im of _imgs) {
const _m = (_im.getAttribute('src') || '').match(/\/api\/upload\/([A-Za-z0-9_\-]+)/);
if (_m && _m[1] && !_ids.includes(_m[1])) _ids.push(_m[1]);
}
}
const _ids = _userMessageAttachmentIds(userMsgElement);
// Rescue: legacy bubbles may have stored the filename as the message
// content (artifact of earlier broken resends). Don't re-send that as
@@ -7378,6 +7474,18 @@ import { invalidateSettings } from './appConfig.js';
try {
if (replaceFromHere) {
// Everything strictly after the immediate AI reply to this user
// message would also be destroyed — that's the part a click on
// Resend / "Regenerate message" doesn't expect to lose.
const extraCount = allMsgs.length - msgIndex - 2;
const choice = await _confirmTruncateIfDestructive(extraCount, 'resend');
if (!choice) return;
if (choice === 'fork') {
// Branch just before this user turn and send it again there; this
// chat keeps every message.
await _forkAndSend(msgIndex, userMsgElement.dataset.dbId || '', text, _ids);
return;
}
// Resend/regenerate trims history to this point before resubmitting so
// the replacement request is the only copy the backend sees.
const keepCount = msgIndex;
@@ -7442,25 +7550,14 @@ import { invalidateSettings } from './appConfig.js';
return;
}
// Collect any file_ids attached to the original user message so the
// regenerated send re-uses them. Without this the AI is regenerated on
// text alone — photos (and the user-edited OCR text cached server-side
// under that file_id) would be silently dropped.
const _attachEls = userMsgEl ? userMsgEl.querySelectorAll('[data-file-id]') : [];
let _regenIds = Array.from(_attachEls).map(el => el.dataset.fileId).filter(Boolean);
// Fallback for bubbles rendered before the data-file-id stamp landed:
// sniff the file id straight out of any `.attach-image-preview img`
// src URLs (matches /api/upload/<id>). Otherwise an older bubble would
// regen with zero attachments and the photo would be lost from the
// resulting message even though the file still exists on disk.
if (!_regenIds.length && userMsgEl) {
const _imgs = userMsgEl.querySelectorAll('.attach-image-preview img, .attach-card img');
for (const _im of _imgs) {
const _m = (_im.getAttribute('src') || '').match(/\/api\/upload\/([A-Za-z0-9_\-]+)/);
if (_m && _m[1] && !_regenIds.includes(_m[1])) _regenIds.push(_m[1]);
}
}
_pendingRegenAttachments = _regenIds;
// Everything strictly after the AI reply being regenerated would also
// be destroyed — that's the part a click on "Regenerate from here"
// doesn't expect to lose.
const extraCount = allMsgs.length - aiIndex - 1;
const choice = await _confirmTruncateIfDestructive(extraCount, 'regenerate');
if (!choice) return;
const _regenIds = _userMessageAttachmentIds(userMsgEl);
// Rescue: earlier-version regens (before the dataset.raw fix) stored the
// photo's filename as the user-message content. On a follow-up regen,
@@ -7469,7 +7566,7 @@ import { invalidateSettings } from './appConfig.js';
// an image file". If userText is just a bare image filename and we have
// attachments, drop it so the OCR text (or the image bytes for vision
// models) is what the model actually sees.
if (userText && _pendingRegenAttachments.length &&
if (userText && _regenIds.length &&
/^[^\n\r]{1,200}\.(png|jpe?g|gif|webp|svg|bmp|heic|heif)$/i.test(userText.trim())) {
userText = '';
}
@@ -7477,7 +7574,7 @@ import { invalidateSettings } from './appConfig.js';
// A photo-only message has empty user text — regen must still proceed,
// because the attachments themselves are the message. Bail only if there
// is no text AND no attachments to send.
if (!userText && !_pendingRegenAttachments.length) {
if (!userText && !_regenIds.length) {
if (uiModule) uiModule.showError('Nothing to regenerate — the user message has no text and no attachments');
return;
}
@@ -7485,6 +7582,20 @@ import { invalidateSettings } from './appConfig.js';
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
if (choice === 'fork') {
// Branch just before the user turn and send it again there, so the new
// chat gets a fresh reply to the same message; this chat keeps every
// message. No variants or hidden bubble: the fork starts without them.
try {
await _forkAndSend(userIndex, (userMsgEl && userMsgEl.dataset.dbId) || '', userText, _regenIds);
} catch (err) {
if (_pendingRegenAttachments === _regenIds) _pendingRegenAttachments = null;
console.error('Fork failed:', err);
if (uiModule) uiModule.showError('Fork failed: ' + err.message);
}
return;
}
// Save current response as a variant
const oldRaw = aiMsgElement.dataset.raw || aiMsgElement.querySelector('.body')?.textContent || '';
const oldHtml = aiMsgElement.querySelector('.body')?.innerHTML || '';
@@ -7496,13 +7607,19 @@ import { invalidateSettings } from './appConfig.js';
}
const keepCount = userIndex;
// Same as resend: cut at the user message's database id, because
// userIndex counts only the messages loaded in the page.
const regenBeforeId = (userMsgEl && userMsgEl.dataset.dbId) || '';
try {
await fetch(`${API_BASE}/api/session/${sessionId}/truncate`, {
const regenTruncate = await fetch(`${API_BASE}/api/session/${sessionId}/truncate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount })
body: JSON.stringify(regenBeforeId ? { before_msg_id: regenBeforeId } : { keep_count: keepCount })
});
// A refused cut (e.g. a stale id) must not stack a second reply.
if (!regenTruncate.ok) throw new Error('Server error ' + regenTruncate.status);
_pendingRegenAttachments = _regenIds;
// Keep the original user bubble, but remove every rendered trace after
// it, including agent-thread tool history between the user and AI bubble.
@@ -7519,12 +7636,14 @@ import { invalidateSettings } from './appConfig.js';
_pendingVariantLabel = 'regen';
_hideUserBubble = true;
_regenKeptUserBubble = userMsgEl;
const messageInput = uiModule.el('message');
messageInput.value = userText;
const submitBtn = document.querySelector('.send-btn');
if (submitBtn) submitBtn.click();
} catch (err) {
if (_pendingRegenAttachments === _regenIds) _pendingRegenAttachments = null;
console.error('Regenerate failed:', err);
if (uiModule) uiModule.showError('Regenerate failed: ' + err.message);
}
@@ -7536,6 +7655,8 @@ import { invalidateSettings } from './appConfig.js';
// File-ids carried over from the original user message during a regen, so
// photos / OCR overrides survive into the new send. Consumed once.
let _pendingRegenAttachments = null;
// Read-only view for tests.
export function _pendingRegenAttachmentIds() { return _pendingRegenAttachments; }
/**
* Called after streaming completes to attach variant navigation if this was a regen.
@@ -7675,20 +7796,8 @@ import { invalidateSettings } from './appConfig.js';
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
const keepCount = aiIndex + 1;
try {
const res = await fetch(`${API_BASE}/api/session/${sessionId}/fork`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount }),
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
await sessionModule.loadSessions();
await sessionModule.selectSession(data.id);
if (uiModule) uiModule.showToast(`Forked → ${data.name}`);
await _forkSessionAt(aiIndex + 1, '', aiMsgElement.dataset.dbId || '');
} catch (err) {
console.error('Fork failed:', err);
if (uiModule) uiModule.showError('Fork failed: ' + err.message);
+1 -1
View File
@@ -10,7 +10,7 @@ import unittest
path = Path(__file__).resolve().parents[1] / "routes/chat_routes.py"
tree = ast.parse(path.read_text())
tree = ast.parse(path.read_text(encoding="utf-8"))
node = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == "_AgentRenderState")
namespace = {}
exec(compile(ast.Module(body=[node], type_ignores=[]), str(path), "exec"), namespace)
@@ -0,0 +1,71 @@
"""An unknown context window must not be budgeted like a tiny local server (#6678).
``budget_context_for_model`` returns 0 when the endpoint does not prove its
window, and ``compute_input_token_budget`` answers 0 with the conservative
default budget. The agent loop's small-server clamp (``<= 8192``) also caught
0 and cut the request to 1200 tokens, so an open document or earlier context
of a few thousand tokens was trimmed away before the model saw it.
"""
import inspect
import json
import pytest
DONE = 'data: [DONE]\n\n'
MARKER = 'NIGHTJAR-7'
# ~3000 estimated tokens: over the old 1200 clamp, under the 6000 default.
CONTEXT = ''.join(
f'## Project-{n}\nOwner: team {n}. Status: active. Notes: routine maintenance '
'and quarterly review scheduled for every workstream in this registry.\n\n'
for n in range(60)
) + f'## Project-Alpha\nAlias: {MARKER}\n'
def _event(payload):
return 'data: ' + json.dumps(payload) + '\n\n'
async def _first_request(monkeypatch, tmp_path, context_window):
import src.agent_loop as al
from tests.test_agent_runtime_context import _patch_fake_skills
_patch_fake_skills(monkeypatch)
monkeypatch.setattr('src.tool_index.get_tool_index', lambda: None)
monkeypatch.setattr('src.model_context.budget_context_for_model',
lambda *args, **kwargs: context_window)
seen = []
async def provider(candidates, messages, **kwargs):
# Like llm_core: the per-candidate factory shapes and trims the request.
url, model, headers = candidates[0][:3]
request = kwargs['candidate_request_factory'](0, url, model, headers)
if inspect.isawaitable(request):
request = await request
seen.append((request or {}).get('messages', messages))
yield _event({'delta': 'ok'})
yield DONE
monkeypatch.setattr(al, 'stream_llm_with_fallback', provider)
history = [
{'role': 'user', 'content': 'Keep this registry for later:\n\n' + CONTEXT},
{'role': 'assistant', 'content': 'Saved.'},
{'role': 'user', 'content': 'What is the alias of Project-Alpha?'},
]
async for _ in al.stream_agent_loop('http://127.0.0.1:9/v1', 'model', history,
max_rounds=1, workspace=str(tmp_path)):
pass
assert seen, 'the provider was never called'
return json.dumps(seen[0])
@pytest.mark.asyncio
async def test_unknown_context_window_keeps_earlier_context(monkeypatch, tmp_path):
request = await _first_request(monkeypatch, tmp_path, 0)
assert MARKER in request
@pytest.mark.asyncio
async def test_known_small_window_is_still_clamped(monkeypatch, tmp_path):
# The clamp exists for small local servers whose window is known (n_ctx
# 2048 here); it must keep protecting them.
request = await _first_request(monkeypatch, tmp_path, 2048)
assert MARKER not in request
@@ -3,8 +3,8 @@ from tests.helpers.stylesheets import app_css
ROOT = Path(__file__).resolve().parents[1]
CHAT = (ROOT / "static/js/chat.js").read_text()
SESSIONS = (ROOT / "static/js/sessions.js").read_text()
CHAT = (ROOT / "static/js/chat.js").read_text(encoding="utf-8")
SESSIONS = (ROOT / "static/js/sessions.js").read_text(encoding="utf-8")
CSS = app_css()
+100
View File
@@ -0,0 +1,100 @@
"""Effective RAG policy reaches the real search and context consumer."""
from types import SimpleNamespace
from unittest.mock import Mock
import pytest
from src.chat_processor import ChatProcessor
from tests.test_kv_cache_invalidation_2927 import _build_context_harness
pytestmark = pytest.mark.area_routes
@pytest.mark.parametrize('mode', ['chat', 'agent', 'compare'])
@pytest.mark.parametrize('policy', [
'normal', 'incognito', 'preprocessing_disabled', 'research_spinoff', 'low_signal',
])
@pytest.mark.parametrize('use_rag', [None, 'true', 'false', True, False])
async def test_rag_policy_preserves_context_channels(monkeypatch, mode, policy, use_rag):
"""Exercise effective policy with real preface assembly and synthetic I/O.
Nonempty results prove permitted retrieval still injects context. Independent
memory, skills and web spies ensure a RAG disable cannot disable other channels.
"""
from routes import chat_helpers
from src import chat_processor, llm_core, user_time
sess, request, handler, _ = _build_context_harness(monkeypatch, chat_helpers, history=[])
monkeypatch.setattr(chat_helpers, 'load_prefs_for_user', lambda user: {
'memory_enabled': True, 'skills_enabled': True})
monkeypatch.setattr(user_time, 'current_datetime_context_message', lambda: {
'role': 'user', 'content': 'Synthetic current date context'})
incognito = policy == 'incognito'
preprocessing = policy != 'preprocessing_disabled'
if policy == 'research_spinoff':
sess.history = [{'role': 'system', 'metadata': {'research_spinoff_from': 'fixture-report'}}]
message = 'hi' if policy == 'low_signal' else 'Find the retention policy for archived project documents'
search = Mock(return_value=[{
'document': 'Synthetic retention document',
'metadata': {'filename': 'fixture.txt'}, 'similarity': 0.99,
}])
memory = Mock(return_value=[{
'id': 'fixture-memory', 'text': 'User identity synthetic memory',
'category': 'identity', 'pinned': True,
}])
skills = Mock(return_value=[{'name': 'fixture_skill', 'description': 'Synthetic skill'}])
web = Mock(return_value=('Synthetic web context', [{'url': 'https://example.invalid/fixture'}]))
query = Mock(return_value='retention policy')
monkeypatch.setattr(llm_core, 'llm_call', query)
monkeypatch.setattr(chat_processor, 'comprehensive_web_search', web)
processor = ChatProcessor(
memory_manager=SimpleNamespace(load=memory),
personal_docs_manager=SimpleNamespace(rag_manager=SimpleNamespace(search=search)),
skills_manager=SimpleNamespace(index_for=skills),
)
received = []
real_preface = processor.build_context_preface
def capture(**kwargs):
received.append(dict(kwargs))
return real_preface(**kwargs)
monkeypatch.setattr(processor, 'build_context_preface', capture)
ctx = await chat_helpers.build_chat_context(
sess=sess, request=request, chat_handler=handler, chat_processor=processor,
message=message, session_id='c5-policy', use_rag=use_rag,
incognito=incognito, allow_tool_preprocessing=preprocessing,
persist_user_message=False, agent_mode=mode == 'agent',
compare_mode=mode == 'compare', use_web='true',
search_context='Synthetic prefetched context' if mode == 'compare' else None,
)
rag_allowed = policy == 'normal' and str(use_rag).lower() != 'false'
assert len(received) == 1
assert search.call_count == int(rag_allowed)
assert received[0]['use_rag'] is rag_allowed
if rag_allowed:
search.assert_called_once_with(message, k=5, owner='tester')
assert ctx.rag_sources == [{
'filename': 'fixture.txt', 'snippet': 'Synthetic retention document', 'similarity': 0.99,
}]
else:
assert ctx.rag_sources == []
text = '\n'.join(m.get('content', '') for m in ctx.messages)
assert ('Synthetic retention document' in text) is rag_allowed
memory_allowed = policy == 'normal'
assert received[0]['use_memory'] is memory_allowed
assert memory.call_count == int(memory_allowed)
assert ('User identity synthetic memory' in text) is memory_allowed
assert bool(ctx.used_memories) is memory_allowed
skills_allowed = policy in {'normal', 'research_spinoff'}
assert received[0]['use_skills'] is skills_allowed
assert skills.call_count == int(skills_allowed and mode == 'agent')
assert ('fixture_skill' in text) is (skills_allowed and mode == 'agent')
web_allowed = preprocessing and policy != 'low_signal' and mode != 'compare'
assert received[0]['use_web'] is web_allowed
assert web.call_count == query.call_count == int(web_allowed)
assert ('Synthetic web context' in text) is web_allowed
assert bool(ctx.web_sources) is web_allowed
assert ('Synthetic prefetched context' in text) is (
mode == 'compare' and preprocessing and policy != 'low_signal')
+2 -2
View File
@@ -2,8 +2,8 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CHAT = (ROOT / "static/js/chat.js").read_text()
RENDERER = (ROOT / "static/js/chatRenderer.js").read_text()
CHAT = (ROOT / "static/js/chat.js").read_text(encoding="utf-8")
RENDERER = (ROOT / "static/js/chatRenderer.js").read_text(encoding="utf-8")
def test_processing_indicator_shows_live_ttft_timer():
+28
View File
@@ -0,0 +1,28 @@
"""Every variable a shipped compose file forwards must carry a default.
A bare ``${VAR}`` makes ``docker compose`` print "variable is not set" on every
command for a default ``.env`` (#6619), which greets fresh installs with a
warning that looks like a misconfiguration.
"""
import re
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
COMPOSE_FILES = sorted(ROOT.glob("docker-compose*.yml")) + sorted((ROOT / "docker").glob("*.yml"))
# ${VAR} with no ``:-``/``-``/``:?``/``?`` modifier.
BARE_VAR = re.compile(r"\$\{([A-Za-z_][A-Za-z0-9_]*)\}")
@pytest.mark.parametrize("path", COMPOSE_FILES, ids=lambda p: p.relative_to(ROOT).as_posix())
def test_compose_variables_have_defaults(path):
bare = [
f"{path.name}:{lineno}: ${{{name}}}"
for lineno, line in enumerate(path.read_text(encoding="utf-8").splitlines(), 1)
if not line.lstrip().startswith("#")
for name in BARE_VAR.findall(line)
]
assert not bare, "compose variables without a default:\n" + "\n".join(bare)
@@ -1,5 +1,9 @@
from pathlib import Path
import pytest
from tests.helpers.database import disposable_database
ROOT = Path(__file__).resolve().parents[1]
COOKBOOK_RUNNING = ROOT / "static" / "js" / "cookbookRunning.js"
@@ -28,3 +32,496 @@ def test_cookbook_advertised_bind_urls_keep_connectable_host():
assert "function _endpointFromAdvertisedUrl" in src
assert "_isAnyBindHost(u.hostname) ? currentHost" in src
assert "host = u.hostname || host;" not in src
@pytest.fixture
def endpoint_db(tmp_path, monkeypatch):
import core.database as database
import routes.model_routes as model_routes
with disposable_database(tmp_path) as factory:
monkeypatch.setattr(database, "SessionLocal", factory)
monkeypatch.setattr(model_routes, "_probe_endpoint", lambda *args, **kwargs: None)
yield factory
def _launch_llm(
repo_id: str,
port: int,
serve_session_id: str | None = None,
) -> str:
import routes.cookbook_routes as cookbook_routes
from routes.cookbook_helpers import ServeRequest
router = cookbook_routes.setup_cookbook_routes()
serve = next(
route.endpoint for route in router.routes
if route.path == "/api/model/serve" and "POST" in route.methods
)
cells = dict(zip(serve.__code__.co_freevars, serve.__closure__ or ()))
register = cells["_auto_register_llm_endpoint"].cell_contents
endpoint_id = register(
ServeRequest(repo_id=repo_id, cmd=f"vllm serve {repo_id} --port {port}"),
None,
serve_session_id=serve_session_id,
)
assert endpoint_id
return endpoint_id
def _endpoint(factory, endpoint_id: str):
import core.database as database
with factory() as db:
return db.get(database.ModelEndpoint, endpoint_id)
@pytest.mark.parametrize("reuse", [False, True])
def test_launch_keeps_endpoint_of_other_model_with_same_short_name(endpoint_db, reuse):
if reuse:
mirror = _launch_llm("unsloth/Llama-3.1-8B-Instruct", 8002)
original = _launch_llm("meta-llama/Llama-3.1-8B-Instruct", 8001)
launched = _launch_llm("unsloth/Llama-3.1-8B-Instruct", 8002)
if reuse:
assert launched == mirror
assert _endpoint(endpoint_db, original) is not None
assert _endpoint(endpoint_db, launched) is not None
@pytest.mark.parametrize("reuse", [False, True])
def test_relaunch_on_another_port_preserves_existing_endpoint(endpoint_db, reuse):
if reuse:
current = _launch_llm("meta-llama/Llama-3.1-8B-Instruct", 8002)
stale = _launch_llm("Qwen/Qwen3-8B", 8001)
launched = _launch_llm("Qwen/Qwen3-8B", 8002)
if reuse:
assert launched == current
assert _endpoint(endpoint_db, stale) is not None
assert _endpoint(endpoint_db, launched) is not None
def test_sweep_ignores_endpoint_now_serving_a_different_model(endpoint_db):
endpoint = _launch_llm("Qwen/Qwen3-8B", 8001)
_launch_llm("meta-llama/Llama-3.1-8B", 8001)
_launch_llm("Qwen/Qwen3-8B", 8001)
_launch_llm("meta-llama/Llama-3.1-8B", 8002)
assert _endpoint(endpoint_db, endpoint) is not None
def test_sweep_ignores_endpoints_not_created_by_cookbook(endpoint_db):
import core.database as database
with endpoint_db() as db:
db.add(database.ModelEndpoint(
id="manual", name="Qwen3-8B", base_url="http://localhost:9000/v1",
pinned_models='["Qwen/Qwen3-8B"]',
))
db.commit()
_launch_llm("Qwen/Qwen3-8B", 8002)
assert _endpoint(endpoint_db, "manual") is not None
def test_manual_pin_order_does_not_change_on_relaunch(endpoint_db):
import json
from core.database import ModelEndpoint
endpoint_id = _launch_llm("organization-a/Model-A", 8001)
with endpoint_db() as db:
endpoint = db.get(ModelEndpoint, endpoint_id)
endpoint.pinned_models = json.dumps([
"organization-a/Model-A",
"organization-b/Model-B",
])
db.commit()
_launch_llm("organization-a/Model-A", 8001)
with endpoint_db() as db:
endpoint = db.get(ModelEndpoint, endpoint_id)
assert json.loads(endpoint.pinned_models) == [
"organization-a/Model-A",
"organization-b/Model-B",
]
def test_manual_pins_cannot_trigger_endpoint_deletion(endpoint_db):
import json
from core.database import ModelEndpoint
original = _launch_llm("organization-a/Model-A", 8001)
with endpoint_db() as db:
endpoint = db.get(ModelEndpoint, original)
endpoint.pinned_models = json.dumps([
"organization-a/Model-A",
"organization-b/Model-B",
])
db.commit()
_launch_llm("organization-b/Model-B", 8002)
assert _endpoint(endpoint_db, original) is not None
def test_unavailable_model_probe_never_deletes_another_endpoint(
endpoint_db, monkeypatch
):
import routes.model_routes as model_routes
original = _launch_llm("organization-a/Model-A", 8001)
monkeypatch.setattr(
model_routes,
"_probe_endpoint",
lambda *args, **kwargs: [],
)
_launch_llm("organization-a/Model-A", 8002)
assert _endpoint(endpoint_db, original) is not None
@pytest.mark.asyncio
async def test_old_watchdog_cannot_delete_reused_endpoint(
endpoint_db,
monkeypatch,
endpoint_http_api,
):
import asyncio
import urllib.request
import routes.cookbook_routes as cookbook_routes
original = _launch_llm(
"organization-a/Model-A",
8001,
serve_session_id="serve-old",
)
reused = _launch_llm(
"organization-b/Model-B",
8001,
serve_session_id="serve-new",
)
assert original == reused
with endpoint_db() as db:
from core.database import ModelEndpoint
endpoint = db.get(ModelEndpoint, reused)
assert endpoint.cookbook_serve_session_id == "serve-new"
router = cookbook_routes.setup_cookbook_routes()
serve = next(
route.endpoint
for route in router.routes
if route.path == "/api/model/serve"
and "POST" in route.methods
)
cells = dict(
zip(
serve.__code__.co_freevars,
serve.__closure__ or (),
)
)
watchdog = cells["_serve_crash_watchdog"].cell_contents
class FakeProcess:
async def communicate(self):
return (
b"=== Process exited with code 1 ===\n",
b"",
)
async def fake_subprocess(*args, **kwargs):
return FakeProcess()
async def fake_sleep(seconds):
return None
def unavailable(*args, **kwargs):
raise OSError("Server still warming up")
monkeypatch.setattr(
asyncio,
"create_subprocess_exec",
fake_subprocess,
)
monkeypatch.setattr(asyncio, "sleep", fake_sleep)
monkeypatch.setattr(
urllib.request,
"urlopen",
unavailable,
)
await watchdog(
endpoint_id=original,
session_id="serve-old",
remote=None,
ssh_port=None,
is_windows=False,
)
assert _endpoint(endpoint_db, reused) is not None
# The current serve's own crash watchdog must still be able
# to remove its endpoint when it actually fails.
await watchdog(
endpoint_id=reused,
session_id="serve-new",
remote=None,
ssh_port=None,
is_windows=False,
)
assert _endpoint(endpoint_db, reused) is None
def test_cookbook_serve_session_migration_is_idempotent(
tmp_path,
monkeypatch,
):
from sqlalchemy import create_engine, inspect, text
import core.database as database
engine = create_engine(
f"sqlite:///{tmp_path / 'legacy-endpoints.db'}"
)
try:
with engine.begin() as conn:
conn.execute(
text(
"CREATE TABLE model_endpoints "
"(id VARCHAR PRIMARY KEY)"
)
)
monkeypatch.setattr(database, "engine", engine)
database._migrate_add_cookbook_serve_session_id_column()
database._migrate_add_cookbook_serve_session_id_column()
columns = {
column["name"]
for column in inspect(engine).get_columns(
"model_endpoints"
)
}
assert "cookbook_serve_session_id" in columns
finally:
engine.dispose()
@pytest.fixture
def endpoint_http_api(endpoint_db, monkeypatch):
"""Route deletion through a disposable in-process FastAPI app."""
import httpx
from fastapi import FastAPI
import routes.model_routes as model_routes
import routes.prefs_routes as prefs_routes
import core.constants as constants
import src.cookbook_serve_lifecycle as lifecycle
monkeypatch.setattr(model_routes, "SessionLocal", endpoint_db)
monkeypatch.setattr(model_routes, "require_admin", lambda request: None)
settings = {}
preferences = {}
monkeypatch.setattr(model_routes, "_load_settings", lambda: settings)
monkeypatch.setattr(model_routes, "_save_settings", lambda value: settings.update(value))
monkeypatch.setattr(prefs_routes, "_load", lambda: preferences)
monkeypatch.setattr(prefs_routes, "_save", lambda value: preferences.update(value))
# Endpoint deletion must not touch the user's running sessions.
import src.ai_interaction as ai_interaction
monkeypatch.setattr(ai_interaction, "get_session_manager", lambda: None)
app = FastAPI()
router = model_routes.setup_model_routes(None)
app.include_router(router)
original_client = httpx.AsyncClient
transport = httpx.ASGITransport(app=app)
def client_factory(*args, **kwargs):
kwargs["transport"] = transport
return original_client(*args, **kwargs)
monkeypatch.setattr(httpx, "AsyncClient", client_factory)
monkeypatch.setattr(constants, "internal_api_base", lambda: "http://testserver")
monkeypatch.setattr(lifecycle, "internal_api_base", lambda: "http://testserver")
return {"settings": settings, "preferences": preferences, "router": router}
@pytest.mark.asyncio
async def test_ownership_condition_preserves_settings_on_mismatch(
endpoint_db, endpoint_http_api,
):
import httpx
original = _launch_llm("organization-a/Model-A", 8001, "serve-old")
reused = _launch_llm("organization-b/Model-B", 8001, "serve-new")
assert reused == original
settings = endpoint_http_api["settings"]
settings.update({"default_endpoint_id": reused, "default_model": "organization-b/Model-B"})
async with httpx.AsyncClient() as client:
response = await client.delete(
f"http://testserver/api/model-endpoints/{reused}",
params={"expected_cookbook_serve_session_id": "serve-old"},
)
assert response.status_code == 409
assert _endpoint(endpoint_db, reused) is not None
assert settings["default_endpoint_id"] == reused
@pytest.mark.asyncio
async def test_owned_deletion_clears_settings_and_model_cache(
endpoint_db, endpoint_http_api,
):
import httpx
from core.database import Session as DbSession
endpoint_id = _launch_llm("organization-a/Model-A", 8001, "serve-current")
settings = endpoint_http_api["settings"]
settings.update({
"default_endpoint_id": endpoint_id,
"default_model": "organization-a/Model-A",
"foreground_model_fallbacks": [{"endpoint_id": endpoint_id, "model": "x"}],
})
with endpoint_db() as db:
db.add(DbSession(
id="owned-delete-session", name="Test", owner=None,
endpoint_url="http://localhost:8001/v1",
endpoint_id=endpoint_id, model="organization-a/Model-A",
headers={"Authorization": "Bearer test-only"},
))
db.commit()
# Seed the actual model listing cache captured by the route closure.
router = endpoint_http_api["router"]
route = next(r for r in router.routes if r.path == "/api/model-endpoints/{ep_id}" and "DELETE" in r.methods)
closure = dict(zip(route.endpoint.__code__.co_freevars, route.endpoint.__closure__ or ()))
invalidate = closure["_invalidate_models_cache"].cell_contents
cache_closure = dict(zip(invalidate.__code__.co_freevars, invalidate.__closure__ or ()))
models_cache = cache_closure["_models_cache"].cell_contents
models_cache["test"] = {"data": [], "time": 0}
async with httpx.AsyncClient() as client:
response = await client.delete(
f"http://testserver/api/model-endpoints/{endpoint_id}",
params={"expected_cookbook_serve_session_id": "serve-current"},
)
assert response.status_code == 200, response.text
assert _endpoint(endpoint_db, endpoint_id) is None
assert settings["default_endpoint_id"] == ""
assert settings["foreground_model_fallbacks"] == []
assert not models_cache
with endpoint_db() as db:
sess = db.get(DbSession, "owned-delete-session")
assert sess.headers == {}
@pytest.mark.asyncio
async def test_ordinary_admin_delete_does_not_require_serve_owner(
endpoint_db, endpoint_http_api,
):
import httpx
endpoint_id = _launch_llm("organization-a/Model-A", 8001, "serve-new")
async with httpx.AsyncClient() as client:
response = await client.delete(f"http://testserver/api/model-endpoints/{endpoint_id}")
assert response.status_code == 200, response.text
assert _endpoint(endpoint_db, endpoint_id) is None
def _launch_image(repo_id: str, port: int, monkeypatch) -> str:
import routes.cookbook_routes as cookbook_routes
import src.settings as settings
from routes.cookbook_helpers import ServeRequest
monkeypatch.setattr(settings, "load_settings", lambda: {"image_gen_enabled": True, "image_model": repo_id})
monkeypatch.setattr(settings, "save_settings", lambda _settings: None)
router = cookbook_routes.setup_cookbook_routes()
serve = next(
route.endpoint for route in router.routes
if route.path == "/api/model/serve" and "POST" in route.methods
)
cells = dict(zip(serve.__code__.co_freevars, serve.__closure__ or ()))
register = cells["_auto_register_image_endpoint"].cell_contents
endpoint_id = register(
ServeRequest(repo_id=repo_id, cmd=f"python diffusion_server.py --model {repo_id} --port {port}"), None
)
assert endpoint_id
return endpoint_id
def _rename(factory, endpoint_id: str, name: str) -> None:
import core.database as database
with factory() as db:
db.get(database.ModelEndpoint, endpoint_id).name = name
db.commit()
@pytest.mark.parametrize("relaunched", ["Qwen/Qwen3-8B", "meta-llama/Llama-3.1-8B"])
def test_relaunch_keeps_renamed_endpoint_name(endpoint_db, relaunched):
endpoint = _launch_llm("Qwen/Qwen3-8B", 8001)
_rename(endpoint_db, endpoint, "Workstation GPU")
assert _launch_llm(relaunched, 8001) == endpoint
assert _endpoint(endpoint_db, endpoint).name == "Workstation GPU"
def test_relaunch_names_endpoint_after_served_model_when_not_renamed(endpoint_db):
endpoint = _launch_llm("Qwen/Qwen3-8B", 8001)
for repo_id, name in [
("meta-llama/Llama-3.1-8B", "Llama-3.1-8B"),
("Qwen/Qwen3-8B", "Qwen3-8B"),
("meta-llama/Llama-3.1-8B", "Llama-3.1-8B"),
]:
_launch_llm(repo_id, 8001)
assert _endpoint(endpoint_db, endpoint).name == name
def test_launch_keeps_name_of_endpoint_without_pinned_models(endpoint_db):
import core.database as database
with endpoint_db() as db:
db.add(database.ModelEndpoint(id="manual", name="My vLLM box", base_url="http://localhost:8001/v1"))
db.commit()
assert _launch_llm("Qwen/Qwen3-8B", 8001) == "manual"
assert _endpoint(endpoint_db, "manual").name == "My vLLM box"
def test_image_relaunch_keeps_renamed_endpoint_name(endpoint_db, monkeypatch):
endpoint = _launch_image("black-forest-labs/FLUX.1-schnell", 8100, monkeypatch)
_rename(endpoint_db, endpoint, "Image box")
_launch_image("stabilityai/sdxl-turbo", 8100, monkeypatch)
assert _endpoint(endpoint_db, endpoint).name == "Image box"
def test_image_relaunch_names_endpoint_after_served_model_when_not_renamed(endpoint_db, monkeypatch):
endpoint = _launch_image("black-forest-labs/FLUX.1-schnell", 8100, monkeypatch)
_launch_image("stabilityai/sdxl-turbo", 8100, monkeypatch)
assert _endpoint(endpoint_db, endpoint).name == "sdxl-turbo (image)"
+60
View File
@@ -70,3 +70,63 @@ def test_no_gguf_found_pattern():
result = _diagnose_serve_output(out)
assert result is not None
assert "GGUF" in result["message"]
def test_cookbook_routes_uses_shared_diagnosis_helper():
# /api/cookbook/tasks/status must use the maintained helper. A nested copy in
# cookbook_routes.py shadowed the import and silently drifted (missing the
# ModelOpt, mflux and MLX bridge diagnoses).
import ast
from pathlib import Path
source = (Path(__file__).resolve().parents[1] / "routes" / "cookbook_routes.py").read_text(encoding="utf-8")
local_defs = [
node.lineno
for node in ast.walk(ast.parse(source))
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) and node.name == "_diagnose_serve_output"
]
assert local_defs == []
def test_modelopt_lm_head_checkpoint_returns_diagnosis():
out = "ValueError: There is no module or parameter named 'lm_head.input_scale' in Qwen3ForCausalLM"
result = _diagnose_serve_output(out)
assert result is not None
assert "ModelOpt" in result["message"]
def test_nvidia_driver_too_old_for_torch_returns_diagnosis():
# vLLM 0.31 from PyPI ships torch cu130; on driver 570 (CUDA 12.8) the engine dies here.
out = (
"(EngineCore pid=1) ERROR [core.py:1433] self.driver_worker.init_device()\n"
"(EngineCore pid=1) ERROR [core.py:1433] RuntimeError: The NVIDIA driver on your system is too old "
"(found version 12080). Please update your GPU driver by downloading and installing a new version\n"
"Traceback (most recent call last):\n"
"RuntimeError: Engine core initialization failed. See root cause above. Failed core proc(s): {}\n"
)
result = _diagnose_serve_output(out)
assert result is not None
assert "driver" in result["message"].lower()
assert any("cu129" in s["label"] for s in result["suggestions"])
def test_cuda_runtime_newer_than_driver_returns_diagnosis():
# torch switched to cu128 but the vLLM wheel's own kernels still need CUDA 13.
out = (
"RuntimeError: get_cuda_view_from_cpu_tensor, /workspace/csrc/libtorch_stable/cuda_view.cu:38, "
"cudaHostGetDevicePointer failed: CUDA driver version is insufficient for CUDA runtime version\n"
"Traceback (most recent call last):\n"
)
result = _diagnose_serve_output(out)
assert result is not None
assert "driver" in result["message"].lower()
def test_tasks_status_capture_joins_wrapped_tmux_lines():
# tmux wraps at the pane width (often mid-word), which hid errors like
# "insufficie|nt for CUDA runtime version" from the diagnosis patterns.
from pathlib import Path
source = (Path(__file__).resolve().parents[1] / "routes" / "cookbook_routes.py").read_text(encoding="utf-8")
assert '_remote_tmux_command("capture-pane", "-t", session_id, "-p", "-J", "-S", "-500")' in source
assert '["tmux", "capture-pane", "-t", session_id, "-p", "-J", "-S", "-500"]' in source
+62
View File
@@ -0,0 +1,62 @@
import pytest
import httpx
from unittest.mock import patch, AsyncMock
from fastapi import FastAPI
from fastapi.testclient import TestClient
from routes.cookbook_routes import setup_cookbook_routes
from src.auth_helpers import require_user
app = FastAPI()
router = setup_cookbook_routes()
app.include_router(router)
app.dependency_overrides[require_user] = lambda: "test_user"
client = TestClient(app)
def test_hf_gguf_files_success():
mock_resp = httpx.Response(
200,
json={
"siblings": [
{"rfilename": "model-q4_k_m.gguf"},
{"rfilename": "model-q8_0.gguf"},
{"rfilename": "README.md"},
]
},
request=httpx.Request("GET", "https://huggingface.co/api/models/TheBloke/Llama-2-7B-GGUF"),
)
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, return_value=mock_resp):
response = client.get("/api/cookbook/hf-gguf-files?repo_id=TheBloke/Llama-2-7B-GGUF")
assert response.status_code == 200
data = response.json()
assert data["ok"] is True
assert data["repo_id"] == "TheBloke/Llama-2-7B-GGUF"
assert data["files"] == ["model-q4_k_m.gguf", "model-q8_0.gguf"]
def test_hf_gguf_files_http_error():
mock_resp = httpx.Response(
404,
json={"error": "Model not found"},
request=httpx.Request("GET", "https://huggingface.co/api/models/invalid/repo"),
)
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, return_value=mock_resp):
response = client.get("/api/cookbook/hf-gguf-files?repo_id=invalid/repo")
assert response.status_code == 200
data = response.json()
assert data["ok"] is False
assert "HF API HTTP 404" in data["error"]
def test_hf_gguf_files_exception_handling():
with patch("httpx.AsyncClient.get", new_callable=AsyncMock, side_effect=httpx.ConnectError("Network unreachable")):
response = client.get("/api/cookbook/hf-gguf-files?repo_id=TheBloke/Llama-2-7B-GGUF")
# Should gracefully return JSON error payload, not raise NameError / 500
assert response.status_code == 200
data = response.json()
assert data["ok"] is False
assert data["error"] == "HF API request failed"
+223
View File
@@ -4,6 +4,13 @@ import pytest
from src import cookbook_serve_lifecycle as lifecycle
from tests.test_cookbook_endpoint_registration import (
endpoint_db,
endpoint_http_api,
_endpoint,
_launch_llm,
)
@pytest.mark.asyncio
async def test_tick_persists_only_successfully_stopped_serves(tmp_path, monkeypatch):
@@ -48,3 +55,219 @@ async def test_tick_persists_only_successfully_stopped_serves(tmp_path, monkeypa
assert tasks["stop-succeeds"]["_scheduledStopAtMs"] is None
assert tasks["stop-fails"]["status"] == "running"
assert tasks["stop-fails"]["_scheduledStopAtMs"] == 0
@pytest.mark.asyncio
async def test_scheduled_stop_preserves_endpoint_reused_by_newer_serve(
endpoint_db,
endpoint_http_api,
):
original = _launch_llm(
"organization-a/Model-A",
8001,
serve_session_id="serve-old",
)
reused = _launch_llm(
"organization-b/Model-B",
8001,
serve_session_id="serve-new",
)
assert original == reused
await lifecycle._delete_endpoint_for_task({
"_endpointId": original,
"sessionId": "serve-old",
"type": "serve",
})
assert _endpoint(endpoint_db, reused) is not None
await lifecycle._delete_endpoint_for_task({
"_endpointId": reused,
"sessionId": "serve-new",
"type": "serve",
})
assert _endpoint(endpoint_db, reused) is None
@pytest.mark.asyncio
async def test_scheduled_stop_preserves_legacy_endpoint_without_owner(
endpoint_db,
endpoint_http_api,
):
endpoint_id = _launch_llm(
"organization-a/Model-A",
8002,
)
await lifecycle._delete_endpoint_for_task({
"_endpointId": endpoint_id,
"sessionId": "serve-old",
"type": "serve",
})
assert _endpoint(endpoint_db, endpoint_id) is not None
@pytest.mark.asyncio
async def test_scheduled_stop_cannot_delete_manual_endpoint(
endpoint_db,
endpoint_http_api,
):
from core.database import ModelEndpoint
with endpoint_db() as db:
db.add(ModelEndpoint(
id="manual-endpoint",
name="Manual",
base_url="http://localhost:9000/v1",
cookbook_serve_session_id="serve-old",
))
db.commit()
await lifecycle._delete_endpoint_for_task({
"_endpointId": "manual-endpoint",
"sessionId": "serve-old",
"type": "serve",
})
assert _endpoint(endpoint_db, "manual-endpoint") is not None
@pytest.mark.asyncio
async def test_scheduled_stop_requires_session_identity(endpoint_db, endpoint_http_api):
endpoint_id = _launch_llm(
"organization-a/Model-A",
8003,
serve_session_id="serve-current",
)
await lifecycle._delete_endpoint_for_task({
"_endpointId": endpoint_id,
"type": "serve",
})
assert _endpoint(endpoint_db, endpoint_id) is not None
def _launch_image(repo_id, port, serve_session_id):
import routes.cookbook_routes as cookbook_routes
from routes.cookbook_helpers import ServeRequest
router = cookbook_routes.setup_cookbook_routes()
serve = next(
route.endpoint
for route in router.routes
if route.path == "/api/model/serve"
and "POST" in route.methods
)
cells = dict(
zip(
serve.__code__.co_freevars,
serve.__closure__ or (),
)
)
register = cells["_auto_register_image_endpoint"].cell_contents
endpoint_id = register(
ServeRequest(
repo_id=repo_id,
cmd=f"python scripts/diffusion_server.py --port {port}",
),
None,
serve_session_id=serve_session_id,
)
assert endpoint_id
return endpoint_id
@pytest.mark.asyncio
async def test_scheduled_stop_deletes_owned_image_endpoint(
endpoint_db,
monkeypatch,
endpoint_http_api,
):
import src.settings as settings
from core.database import ModelEndpoint
monkeypatch.setattr(settings, "load_settings", lambda: {})
monkeypatch.setattr(
settings,
"save_settings",
lambda value: None,
)
endpoint_id = _launch_image(
"organization-a/Image-A",
8101,
"serve-image-a",
)
assert endpoint_id.startswith("img-")
with endpoint_db() as db:
endpoint = db.get(ModelEndpoint, endpoint_id)
assert endpoint.cookbook_serve_session_id == "serve-image-a"
await lifecycle._delete_endpoint_for_task({
"_endpointId": endpoint_id,
"sessionId": "serve-image-a",
"type": "serve",
})
assert _endpoint(endpoint_db, endpoint_id) is None
@pytest.mark.asyncio
async def test_old_image_serve_cannot_delete_reused_endpoint(
endpoint_db,
monkeypatch,
endpoint_http_api,
):
import src.settings as settings
monkeypatch.setattr(settings, "load_settings", lambda: {})
monkeypatch.setattr(
settings,
"save_settings",
lambda value: None,
)
original = _launch_image(
"organization-a/Image-A",
8102,
"serve-old",
)
reused = _launch_image(
"organization-b/Image-B",
8102,
"serve-new",
)
assert original == reused
await lifecycle._delete_endpoint_for_task({
"_endpointId": original,
"sessionId": "serve-old",
"type": "serve",
})
assert _endpoint(endpoint_db, reused) is not None
await lifecycle._delete_endpoint_for_task({
"_endpointId": reused,
"sessionId": "serve-new",
"type": "serve",
})
assert _endpoint(endpoint_db, reused) is None
+103
View File
@@ -0,0 +1,103 @@
"""Daily brief must query and display each calendar storage kind in local time."""
import asyncio
import os
import time
from contextlib import contextmanager
from datetime import datetime, timedelta, timezone
from types import SimpleNamespace
from zoneinfo import ZoneInfo
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool
from core import database
from src import builtin_actions
@contextmanager
def system_timezone(name):
previous = os.environ.get('TZ')
os.environ['TZ'] = name
time.tzset()
try:
yield
finally:
if previous is None:
os.environ.pop('TZ', None)
else:
os.environ['TZ'] = previous
time.tzset()
@pytest.mark.skipif(not hasattr(time, 'tzset'), reason='requires changing the process local timezone')
@pytest.mark.parametrize('zone', ['Europe/Berlin', 'America/Los_Angeles'])
def test_daily_brief_selects_local_day_and_orders_mixed_storage(monkeypatch, zone):
engine = create_engine('sqlite://', poolclass=StaticPool)
for model in [database.CalendarCal, database.CalendarEvent, database.Note]:
model.__table__.create(engine)
sessions = sessionmaker(bind=engine)
monkeypatch.setattr(database, 'SessionLocal', sessions)
from core.auth import AuthManager
monkeypatch.setattr(AuthManager, 'is_configured', property(lambda self: True))
from routes import email_helpers
def no_email(*args):
raise RuntimeError('No email account in this calendar-only test')
monkeypatch.setattr(email_helpers, '_imap_connect', no_email)
try:
with system_timezone(zone):
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
def utc(value):
return value.astimezone(timezone.utc).replace(tzinfo=None)
with sessions() as db:
for owner in ['alice', 'bob', None]:
db.add(database.CalendarCal(id=owner or 'shared', owner=owner, name=owner or 'shared'))
db.flush()
def add(uid, start, end, *, utc_row=False, all_day=False, owner='alice', status='confirmed'):
db.add(database.CalendarEvent(uid=uid, calendar_id=owner or 'shared', summary=uid,
dtstart=utc(start) if utc_row and not all_day else start,
dtend=utc(end) if utc_row and not all_day else end,
is_utc=utc_row, all_day=all_day, status=status))
add('early', today+timedelta(minutes=30), today+timedelta(hours=1), utc_row=True)
add('utc-ten', today+timedelta(hours=10), today+timedelta(hours=11), utc_row=True)
add('local-nine', today+timedelta(hours=9), today+timedelta(hours=10))
add('all-day', today, today+timedelta(days=1), utc_row=True, all_day=True)
add('next-day', today+timedelta(days=1,minutes=30), today+timedelta(days=1,hours=1), utc_row=True)
add('ended', today-timedelta(hours=1), today, utc_row=True)
add('cancelled', today+timedelta(hours=12), today+timedelta(hours=13), utc_row=True, status='cancelled')
add('foreign', today+timedelta(hours=12), today+timedelta(hours=13), utc_row=True, owner='bob')
add('unowned', today+timedelta(hours=12), today+timedelta(hours=13), owner=None)
db.commit()
text, success = asyncio.run(builtin_actions.action_daily_brief('alice'))
assert success, text
lines = ['all day all-day', '00:30 early', '09:00 local-nine', '10:00 utc-ten']
assert all(line in text for line in lines), text
assert [text.index(line) for line in lines] == sorted(text.index(line) for line in lines), text
for excluded in ['next-day', 'ended', 'cancelled', 'foreign', 'unowned']:
assert excluded not in text, text
finally:
engine.dispose()
@pytest.mark.parametrize('day, hours', [('2026-03-29', 23), ('2026-10-25', 25)])
def test_utc_day_window_uses_offset_at_each_boundary(day, hours):
start = datetime.fromisoformat(day)
zone = ZoneInfo('Europe/Berlin')
begin = builtin_actions._daily_brief_utc_boundary(start, zone)
end = builtin_actions._daily_brief_utc_boundary(start+timedelta(days=1), zone)
assert end-begin == timedelta(hours=hours)
if hasattr(time, 'tzset'):
with system_timezone('Europe/Berlin'):
assert builtin_actions._daily_brief_utc_boundary(start) == begin
assert builtin_actions._daily_brief_utc_boundary(start+timedelta(days=1)) == end
@pytest.mark.parametrize('utc_row, all_day, expected', [
(True, False, datetime(2026, 6, 1, 10)),
(False, False, datetime(2026, 6, 1, 8)),
(True, True, datetime(2026, 6, 1, 8)),
])
def test_local_start_preserves_floating_and_date_only_values(utc_row, all_day, expected):
event = SimpleNamespace(dtstart=datetime(2026,6,1,8), is_utc=utc_row, all_day=all_day)
assert builtin_actions._daily_brief_local_start(event, ZoneInfo('Europe/Berlin')) == expected
+86
View File
@@ -0,0 +1,86 @@
"""Issue #2137: never preselect an embedding or other non-chat model."""
import json
from types import SimpleNamespace
import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient
from core.database import ModelEndpoint
from routes import model_routes, prefs_routes
from tests.helpers.database import disposable_database
@pytest.fixture
def defaults_api(monkeypatch, tmp_path):
defaults = {}
with disposable_database(tmp_path) as factory:
monkeypatch.setattr(model_routes, "SessionLocal", factory)
monkeypatch.setattr(model_routes, "_load_settings", lambda: {})
monkeypatch.setattr(prefs_routes, "_load_for_user", lambda owner: defaults)
app = FastAPI()
app.state.auth_manager = SimpleNamespace(is_admin=lambda owner: False)
@app.middleware("http")
async def owner(request, call_next):
request.state.current_user = "alice"
return await call_next(request)
app.include_router(model_routes.setup_model_routes(model_discovery=None))
with TestClient(app) as client:
yield client, factory, defaults
def _endpoint(factory, *, cached=(), pinned=(), hidden=(), owner="alice", endpoint_id="ep"):
with factory() as db:
db.add(ModelEndpoint(
id=endpoint_id, name="Fixture", base_url="http://model.invalid/v1", owner=owner,
is_enabled=True, cached_models=json.dumps(cached),
pinned_models=json.dumps(pinned), hidden_models=json.dumps(hidden),
))
db.commit()
@pytest.mark.parametrize("non_chat", [
"text-embedding-3-small", "nomic-embed-text:latest", "jina-reranker-v2-base",
"tts-1", "whisper-1", "gpt-image-1",
])
def test_automatic_default_skips_non_chat_models(defaults_api, non_chat):
client, factory, _ = defaults_api
_endpoint(factory, cached=[non_chat, "chat-model"])
assert client.get("/api/default-chat").json()["model"] == "chat-model"
def test_non_chat_saved_default_falls_back_to_visible_pinned_chat(defaults_api):
client, factory, defaults = defaults_api
_endpoint(factory, cached=["text-embedding-3-small", "hidden-chat"],
pinned=["jina-reranker-v2-base", "pinned-chat"], hidden=["hidden-chat"])
defaults.update(default_endpoint_id="ep", default_model="text-embedding-3-small")
assert client.get("/api/default-chat").json()["model"] == "pinned-chat"
def test_no_chat_candidate_leaves_model_empty(defaults_api):
client, factory, defaults = defaults_api
_endpoint(factory, cached=["text-embedding-3-small", "hidden-chat"],
pinned=["jina-reranker-v2-base"], hidden=["hidden-chat"])
defaults.update(default_endpoint_id="ep", default_model="text-embedding-3-small")
result = client.get("/api/default-chat").json()
assert result == {"endpoint_id": "ep", "endpoint_url": "http://model.invalid/v1/chat/completions", "model": ""}
def test_valid_explicit_model_survives_stale_catalog(defaults_api):
client, factory, defaults = defaults_api
_endpoint(factory, cached=["text-embedding-3-small", "other-chat"])
defaults.update(default_endpoint_id="ep", default_model="my-custom-chat")
assert client.get("/api/default-chat").json()["model"] == "my-custom-chat"
def test_model_filter_preserves_owner_scope(defaults_api):
client, factory, defaults = defaults_api
_endpoint(factory, owner="bob", endpoint_id="bobs-endpoint", cached=["bobs-chat"])
_endpoint(factory, cached=["text-embedding-3-small", "alices-chat"])
defaults.update(default_endpoint_id="bobs-endpoint")
result = client.get("/api/default-chat").json()
assert result["endpoint_id"] == "ep"
assert result["model"] == "alices-chat"
+8
View File
@@ -67,6 +67,14 @@ def test_compose_files_forward_companion_base_url():
assert "COMPANION_BASE_URL" in _compose_env_names(path), path.name
def test_compose_files_forward_first_token_timeout():
# The local first-event watchdog (src/llm_core.py) reads this from the
# process environment, so a Docker install can only reach it when the
# compose files forward it from the host .env.
for path in COMPOSE_FILES:
assert "ODYSSEUS_FIRST_TOKEN_TIMEOUT" in _compose_env_names(path), path.name
def test_default_compose_files_do_not_mount_host_docker_socket():
for path in COMPOSE_FILES:
text = path.read_text(encoding="utf-8")
@@ -0,0 +1,96 @@
"""ZIP exports keep language extensions and place collision suffixes before them."""
import io
import zipfile
import pytest
from fastapi import FastAPI, Request
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from core.database import Base, Document
from routes.document import document_routes
@pytest.fixture
def exports(tmp_path, monkeypatch):
engine = create_engine(f"sqlite:///{tmp_path / 'documents.db'}", connect_args={"check_same_thread": False})
Base.metadata.create_all(engine)
factory = sessionmaker(bind=engine)
monkeypatch.setattr(document_routes, "SessionLocal", factory)
monkeypatch.setenv("AUTH_ENABLED", "true")
app = FastAPI()
@app.middleware("http")
async def fixture_user(request: Request, call_next):
request.state.current_user = "alice"
return await call_next(request)
app.include_router(document_routes.setup_document_routes(None))
def add(doc_id, title, language, content="body\n", owner="alice"):
with factory() as db:
db.add(Document(id=doc_id, title=title, language=language,
current_content=content, owner=owner, is_active=True))
db.commit()
with TestClient(app) as client:
yield client, add
engine.dispose()
@pytest.mark.parametrize("title,language,filename", [
("config.local", "python", "config.local.py"),
("report.v2", "markdown", "report.v2.md"),
("data.2024", "json", "data.2024.json"),
("module.ts", "typescript", "module.ts"),
("SCRIPT.PY", "python", "SCRIPT.PY"),
("résumé.v2", "text", "résumé.v2.txt"),
("unknown.v2", "unknown", "unknown.v2.txt"),
("draft", None, "draft.txt"),
("project/report.v2", "markdown", "projectreport.v2.md"),
])
def test_export_uses_the_language_extension_even_for_dotted_titles(exports, title, language, filename):
client, add = exports
body = "Original text: α\n"
add("doc", title, language, body)
response = client.post("/api/documents/export-zip", json={"ids": ["doc"]})
assert response.status_code == 200
assert response.headers["content-type"] == "application/zip"
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
assert archive.namelist() == [filename]
assert archive.read(filename).decode() == body
@pytest.mark.parametrize("title", ["config.local", "config.local.py"])
def test_duplicate_names_keep_the_extension_at_the_end(exports, title):
client, add = exports
for i in range(3):
add(str(i), title, "python", f"body {i}")
response = client.post("/api/documents/export-zip", json={"ids": ["0", "1", "2"]})
assert response.status_code == 200
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
assert set(archive.namelist()) == {"config.local.py", "config.local-1.py", "config.local-2.py"}
assert {archive.read(name).decode() for name in archive.namelist()} == {"body 0", "body 1", "body 2"}
def test_added_extension_collides_safely_with_an_existing_extension(exports):
client, add = exports
add("a", "report.v2", "markdown", "first")
add("b", "report.v2.md", "markdown", "second")
response = client.post("/api/documents/export-zip", json={"ids": ["a", "b"]})
assert response.status_code == 200
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
assert set(archive.namelist()) == {"report.v2.md", "report.v2-1.md"}
assert {archive.read(name) for name in archive.namelist()} == {b"first", b"second"}
def test_foreign_documents_are_still_excluded_and_email_body_is_preserved(exports):
client, add = exports
add("owned", "reply.v2", "email", "Subject: Hello\n---\nBody")
add("foreign", "private", "python", "secret", owner="bob")
response = client.post("/api/documents/export-zip", json={"ids": ["owned", "foreign"]})
assert response.status_code == 200
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
assert archive.namelist() == ["reply.v2.eml"]
assert archive.read("reply.v2.eml") == b"Subject: Hello\r\n\r\nBody"
@@ -0,0 +1,74 @@
"""Literal punctuation in library queries must not become SQL wildcards."""
from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
from core.database import Document
from routes.document import document_routes
from tests.helpers.database import disposable_database
CASES = [
("100%", "100% complete", "1000 complete"),
("a_b", "a_b", "axb"),
("%", "price 10%", "price 100"),
("_", "a_b", "axb"),
(r"path\notes", r"path\notes", "pathnotes"),
(r"path\_tag", r"path\_tag", r"path\Xtag"),
("50%_done", "50%_done", "500Xdone"),
("can't", "can't stop", "cannot stop"),
("mixed case", "Case ordered Mixed", "Mixed without"),
]
@pytest.mark.parametrize("column", ["title", "current_content"])
@pytest.mark.parametrize("query,literal,lookalike", CASES)
@pytest.mark.asyncio
async def test_library_search_matches_literal_tokens(tmp_path, monkeypatch, column, query, literal, lookalike):
with disposable_database(tmp_path) as factory:
monkeypatch.setattr(document_routes, "SessionLocal", factory)
with factory() as db:
for doc_id, text, owner, active, archived in [
("literal", literal, "alice", True, False),
("lookalike", lookalike, "alice", True, False),
("foreign", literal, "bob", True, False),
("inactive", literal, "alice", False, False),
("archived", literal, "alice", True, True),
]:
fields = {"title": "Document", "current_content": "Body", column: text}
db.add(Document(id=doc_id, owner=owner, is_active=active, archived=archived,
language="text", version_count=1, **fields))
db.commit()
router = document_routes.setup_document_routes(MagicMock())
endpoint = next(r.endpoint for r in router.routes if r.path == "/api/documents/library")
request = SimpleNamespace(state=SimpleNamespace(current_user="alice"))
result = await endpoint(request, search=query, language=None, sort="recent", offset=0, limit=20, archived=False)
assert result["total"] == 1
assert [row["id"] for row in result["documents"]] == ["literal"]
archived = await endpoint(request, search=query, language=None, sort="recent", offset=0, limit=20, archived=True)
assert [row["id"] for row in archived["documents"]] == ["archived"]
@pytest.mark.asyncio
async def test_literal_search_keeps_cross_field_and_pagination_behavior(tmp_path, monkeypatch):
with disposable_database(tmp_path) as factory:
monkeypatch.setattr(document_routes, "SessionLocal", factory)
with factory() as db:
for doc_id, title, content in [
("first", "100% ready", "done"),
("second", "done", "100% ready"),
("lookalike", "1000 ready", "done"),
("missing-term", "100% ready", "waiting"),
]:
db.add(Document(id=doc_id, owner="alice", title=title, current_content=content,
is_active=True, archived=False, language="text", version_count=1))
db.commit()
router = document_routes.setup_document_routes(MagicMock())
endpoint = next(r.endpoint for r in router.routes if r.path == "/api/documents/library")
request = SimpleNamespace(state=SimpleNamespace(current_user="alice"))
results = [await endpoint(request, search="100% done", language="text", sort="alpha",
offset=offset, limit=1, archived=False) for offset in [0, 1]]
assert all(result["total"] == 2 for result in results)
assert {row["id"] for result in results for row in result["documents"]} == {"first", "second"}
+2 -1
View File
@@ -41,7 +41,8 @@ def test_claim_is_fsynced_to_disk_before_returning(tmp_path, target, monkeypatch
on_disk = records(log.path)
assert [r["type"] for r in on_disk] == ["claim"]
assert fx.EffectClaim.from_dict(on_disk[0]["record"]) == made
assert oct(log.path.stat().st_mode & 0o777) == "0o600"
if os.name != "nt": # Windows has no POSIX permission bits: os.open's 0o600 is not reflected in st_mode
assert oct(log.path.stat().st_mode & 0o777) == "0o600"
def test_claim_persistence_failure_raises_and_records_nothing(tmp_path, target):
+39
View File
@@ -90,3 +90,42 @@ def test_fork_does_not_corrupt_source_message_metadata(monkeypatch):
# ...and the source session's _db_id values are untouched.
assert source.history[0].metadata["_db_id"] == "src-0"
assert source.history[1].metadata["_db_id"] == "src-1"
def _fork_with(monkeypatch, body, db_ids):
"""Fork a 4-message source whose DB rows have the given ids."""
monkeypatch.setattr(mod, "_verify_session_owner", lambda *a, **k: None)
source = _FakeSession(name="Long chat")
for i, role in enumerate(["user", "assistant", "user", "assistant"]):
source.history.append(ChatMessage(role=role, content=f"m{i}", metadata={"_db_id": db_ids[i]}))
manager = _FakeSessionManager(source)
rows = [SimpleNamespace(id=i) for i in db_ids]
class _Query:
def filter(self, *a): return self
def order_by(self, *a): return self
def all(self): return rows
class _DB:
def query(self, *a): return _Query()
def close(self): pass
monkeypatch.setattr(mod, "SessionLocal", lambda: _DB())
router = mod.setup_history_routes(manager)
class _Req:
async def json(self): return body
asyncio.run(_fork_handler(router)(_Req(), "src-id"))
return [m.content for m in manager.created.history]
def test_fork_before_a_message_id_ignores_a_stale_keep_count(monkeypatch):
copied = _fork_with(monkeypatch, {"before_msg_id": "c", "keep_count": 0}, ["a", "b", "c", "d"])
assert copied == ["m0", "m1"]
def test_fork_through_a_message_id_includes_it(monkeypatch):
copied = _fork_with(monkeypatch, {"through_msg_id": "b", "keep_count": 0}, ["a", "b", "c", "d"])
assert copied == ["m0", "m1"]
+49
View File
@@ -0,0 +1,49 @@
import asyncio
import pytest
from fastapi import HTTPException
import routes.gallery_routes as gallery
def route(path, method):
return next(r for r in gallery.setup_gallery_routes().routes if r.path == path and method in r.methods)
def call(path, method, monkeypatch, body):
monkeypatch.setattr(gallery, 'get_current_user', lambda request: 'alice')
class Request:
async def json(self):
return body
return asyncio.run(route(path, method).endpoint(Request(), 'album-1'))
@pytest.mark.parametrize('body', [
{'name': ''},
{'name': ' '},
{'name': 123},
{'name': ['x']},
{'description': {'a': 1}},
[1],
])
def test_album_update_rejects_bad_input(body, monkeypatch):
with pytest.raises(HTTPException) as exc:
call('/api/gallery/albums/{album_id}', 'PUT', monkeypatch, body)
assert exc.value.status_code == 400
@pytest.mark.parametrize('op', ['add', 'remove'])
@pytest.mark.parametrize('body', [
{'image_ids': 'abc'},
{'image_ids': 5},
{'image_ids': None},
{'image_ids': [{'a': 1}]},
{'image_ids': ['ok', 1]},
[1],
])
def test_album_image_ids_must_be_a_list_of_strings(op, body, monkeypatch):
with pytest.raises(HTTPException) as exc:
call(f'/api/gallery/albums/{{album_id}}/{op}', 'POST', monkeypatch, body)
assert exc.value.status_code == 400
+135
View File
@@ -0,0 +1,135 @@
"""Regression for #2123 (gallery part): library search must match multi-word
queries in any order and treat % / _ literally.
Old code built a single ``%{search}%`` LIKE over prompt/tags/ai_tags, so
"red car" missed "a car that is red", and a literal "%" matched every row.
"""
import uuid
from fastapi import FastAPI
from fastapi.testclient import TestClient
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import NullPool
import core.database as cdb
from core.database import GalleryImage
import routes.gallery_routes as gallery_routes
def _client_with_gallery(monkeypatch, tmp_path):
engine = create_engine(
f"sqlite:///{tmp_path / 'gallery_search.db'}",
connect_args={"check_same_thread": False},
poolclass=NullPool,
)
cdb.Base.metadata.create_all(engine)
session_factory = sessionmaker(bind=engine, autoflush=False, autocommit=False)
monkeypatch.setattr(gallery_routes, "SessionLocal", session_factory)
db = session_factory()
try:
db.add_all(
[
GalleryImage(
id="img-red-car",
filename=f"{uuid.uuid4().hex}.png",
prompt="a car that is red",
model="model-a",
tags="",
ai_tags="",
is_active=True,
file_size=10,
),
GalleryImage(
id="img-boat",
filename=f"{uuid.uuid4().hex}.png",
prompt="blue boat on the lake",
model="model-a",
tags="",
ai_tags="",
is_active=True,
file_size=10,
),
GalleryImage(
id="img-percent",
filename=f"{uuid.uuid4().hex}.png",
prompt="100% cotton shirt",
model="model-a",
tags="sale_2024",
ai_tags="",
is_active=True,
file_size=10,
),
GalleryImage(
id="img-backslash",
filename=f"{uuid.uuid4().hex}.png",
prompt=r"C:\photos\sunset",
model="model-a",
tags="",
ai_tags="",
is_active=True,
file_size=10,
),
]
)
db.commit()
finally:
db.close()
app = FastAPI()
app.include_router(gallery_routes.setup_gallery_routes())
return TestClient(app)
def test_gallery_search_matches_words_in_any_order(monkeypatch, tmp_path):
monkeypatch.setenv("AUTH_ENABLED", "false")
client = _client_with_gallery(monkeypatch, tmp_path)
body = client.get("/api/gallery/library", params={"search": "red car"}).json()
ids = {item["id"] for item in body["items"]}
assert "img-red-car" in ids
assert "img-boat" not in ids
def test_gallery_search_escapes_percent_wildcard(monkeypatch, tmp_path):
monkeypatch.setenv("AUTH_ENABLED", "false")
client = _client_with_gallery(monkeypatch, tmp_path)
body = client.get("/api/gallery/library", params={"search": "%"}).json()
ids = {item["id"] for item in body["items"]}
assert ids == {"img-percent"}
def test_gallery_search_escapes_underscore_wildcard(monkeypatch, tmp_path):
monkeypatch.setenv("AUTH_ENABLED", "false")
client = _client_with_gallery(monkeypatch, tmp_path)
# Only img-percent has a literal underscore (in tags "sale_2024").
# Unescaped, "_" matches any single char so every row matches.
body = client.get("/api/gallery/library", params={"search": "_"}).json()
ids = {item["id"] for item in body["items"]}
assert ids == {"img-percent"}
def test_gallery_search_escapes_backslash(monkeypatch, tmp_path):
monkeypatch.setenv("AUTH_ENABLED", "false")
client = _client_with_gallery(monkeypatch, tmp_path)
# Only img-backslash has a literal backslash. The escape char itself
# must survive escaping instead of escaping the surrounding wildcards.
body = client.get("/api/gallery/library", params={"search": "\\"}).json()
ids = {item["id"] for item in body["items"]}
assert ids == {"img-backslash"}
def test_gallery_tag_filter_escapes_wildcards(monkeypatch, tmp_path):
monkeypatch.setenv("AUTH_ENABLED", "false")
client = _client_with_gallery(monkeypatch, tmp_path)
# Unescaped, "%" / "_" match every row; escaped, only literal matches.
assert client.get("/api/gallery/library", params={"tag": "%"}).json()["items"] == []
body = client.get("/api/gallery/library", params={"tag": "_"}).json()
assert {item["id"] for item in body["items"]} == {"img-percent"}
@@ -4,11 +4,11 @@ from tests.helpers.stylesheets import app_css
ROOT = Path(__file__).resolve().parents[1]
CHAT = (ROOT / "static/js/chat.js").read_text()
RENDERER = (ROOT / "static/js/chatRenderer.js").read_text()
CHAT = (ROOT / "static/js/chat.js").read_text(encoding="utf-8")
RENDERER = (ROOT / "static/js/chatRenderer.js").read_text(encoding="utf-8")
STYLE = app_css()
APP = (ROOT / "static/app.js").read_text()
INDEX = (ROOT / "static/index.html").read_text()
APP = (ROOT / "static/app.js").read_text(encoding="utf-8")
INDEX = (ROOT / "static/index.html").read_text(encoding="utf-8")
def test_live_interruption_controls_have_visible_resume_labels():
File diff suppressed because it is too large Load Diff
+98
View File
@@ -0,0 +1,98 @@
"""Actual embedded PDF images must reach vision as a decodable PNG."""
from pathlib import Path
from PIL import Image
from pypdf import PdfReader, PdfWriter
from pypdf.generic import DecodedStreamObject, DictionaryObject, NameObject, NumberObject
import pytest
from src import document_processor as dp
def make_pdf(tmp_path, mode):
colors = {"CMYK": (0, 255, 255, 0), "RGB": (255, 0, 0), "L": 127}
path = tmp_path / (mode + ".pdf")
image = Image.new(mode, (128, 96), colors[mode])
if mode == "CMYK":
# A raw DeviceCMYK XObject avoids JPEG/Adobe inversion differences in
# Pillow-generated PDFs; the extracted color samples are unambiguous.
writer = PdfWriter()
page = writer.add_blank_page(width=128, height=96)
stream = DecodedStreamObject()
stream.set_data(image.tobytes())
stream.update({NameObject(key): value for key, value in {
"/Type": NameObject("/XObject"), "/Subtype": NameObject("/Image"),
"/ColorSpace": NameObject("/DeviceCMYK"), "/BitsPerComponent": NumberObject(8),
"/Width": NumberObject(128), "/Height": NumberObject(96),
}.items()})
page[NameObject("/Resources")] = DictionaryObject({NameObject("/XObject"):
DictionaryObject({NameObject("/Im0"): writer._add_object(stream)})})
content = DecodedStreamObject()
content.set_data(b"q 128 0 0 96 0 0 cm /Im0 Do Q")
page[NameObject("/Contents")] = writer._add_object(content)
writer.write(path)
else:
image.save(path, "PDF")
assert PdfReader(path).pages[0].images[0].image.mode == mode
return path
@pytest.mark.parametrize("mode,expected", [("CMYK", "RGB"), ("RGB", "RGB"), ("L", "L")])
def test_pdf_image_reaches_vision_with_owner_and_png_pixels(tmp_path, monkeypatch, mode, expected):
pdf = make_pdf(tmp_path, mode)
calls = []
def analyze(path, owner=None):
with Image.open(path) as image:
image.load()
assert image.format == "PNG" and image.mode == expected
assert image.size == (128, 96)
if mode == "CMYK":
red, green, blue = image.getpixel((64, 48))
assert red > 250 and green < 5 and blue < 5
calls.append((Path(path), owner))
return "Synthetic OCR result"
monkeypatch.setattr(dp, "analyze_image_with_vl", analyze)
output = dp._process_pdf(str(pdf), owner="alice")
assert "[Page 1 image 1 text]: Synthetic OCR result" in output
assert len(calls) == 1 and calls[0][1] == "alice"
assert not calls[0][0].exists(), "Temporary PNG must be removed after analysis"
assert PdfReader(pdf).pages[0].images[0].image.mode == mode, "Source PDF is unchanged"
def test_mixed_pdf_keeps_images_on_both_pages(tmp_path, monkeypatch):
writer = PdfWriter()
for mode in ("CMYK", "RGB"):
writer.append(str(make_pdf(tmp_path, mode)))
pdf = tmp_path / "mixed.pdf"
writer.write(pdf)
calls = []
def analyze(path, owner=None):
with Image.open(path) as image:
image.load()
calls.append(image.mode)
return "Image content"
monkeypatch.setattr(dp, "analyze_image_with_vl", analyze)
output = dp._process_pdf(str(pdf), owner="alice")
assert calls == ["RGB", "RGB"]
assert "Page 1 image 1 text" in output and "Page 2 image 1 text" in output
def test_text_only_extraction_does_not_enable_vision(tmp_path, monkeypatch):
pdf = make_pdf(tmp_path, "CMYK")
def unexpected(*args, **kwargs):
pytest.fail("Text-only extraction must not call vision")
monkeypatch.setattr(dp, "analyze_image_with_vl", unexpected)
assert "no readable content" in dp.extract_local_document(str(pdf), owner="alice")
def test_vision_failure_still_removes_converted_png(tmp_path, monkeypatch):
pdf = make_pdf(tmp_path, "CMYK")
paths = []
def unavailable(path, owner=None):
paths.append(Path(path))
with Image.open(path) as image:
assert image.mode == "RGB" and image.format == "PNG"
raise RuntimeError("Synthetic unavailable vision endpoint")
monkeypatch.setattr(dp, "analyze_image_with_vl", unavailable)
assert "no readable content" in dp._process_pdf(str(pdf), owner="alice")
assert len(paths) == 1 and not paths[0].exists()
+108
View File
@@ -0,0 +1,108 @@
"""Route defaults must use manually pinned IDs without widening ownership."""
import json
from types import SimpleNamespace
import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
import core.database as cdb
import routes.chat_routes as chat
from routes.task.task_routes import _resolve_run_endpoint
from routes.research.research_routes import _resolve_endpoint_runtime
@pytest.fixture
def database(monkeypatch):
engine = create_engine("sqlite:///:memory:")
cdb.Base.metadata.create_all(engine)
factory = sessionmaker(bind=engine)
monkeypatch.setattr(chat, "SessionLocal", factory)
with factory() as db:
yield db
engine.dispose()
def endpoint(db, eid="owned", *, owner="alice", cached=(), pinned=("pinned-chat",), hidden=()):
row = cdb.ModelEndpoint(
id=eid, name=eid, owner=owner, is_enabled=True,
base_url=f"https://{eid}.example.test/v1", api_key=None,
cached_models=json.dumps(cached), pinned_models=json.dumps(pinned),
hidden_models=json.dumps(hidden),
)
db.add(row)
db.commit()
return row
@pytest.mark.parametrize("cached", [(), ("hidden-chat",)])
def test_empty_session_recovers_a_visible_pinned_model(database, cached):
ep = endpoint(database, cached=cached, hidden=("hidden-chat",))
database.add(cdb.Session(id="chat", name="Chat", owner="alice", model="", endpoint_url=ep.base_url, endpoint_id=ep.id))
database.commit()
session = SimpleNamespace(model="", endpoint_url=ep.base_url, endpoint_id=ep.id)
assert chat._recover_empty_session_model(session, "chat", "alice") is True
assert session.model == "pinned-chat"
database.expire_all()
assert database.get(cdb.Session, "chat").model == "pinned-chat"
def test_empty_session_does_not_recover_a_hidden_pin(database):
ep = endpoint(database, hidden=("pinned-chat",))
session = SimpleNamespace(model="", endpoint_url=ep.base_url, endpoint_id=ep.id)
assert chat._recover_empty_session_model(session, "missing", "alice") is False
assert session.model == ""
def test_subscription_recovery_still_requires_its_live_catalog(database):
ep = endpoint(database)
ep.base_url = "https://chatgpt.com/backend-api/codex"
database.commit()
session = SimpleNamespace(model="", endpoint_url=ep.base_url, endpoint_id=ep.id)
assert chat._recover_empty_session_model(session, "missing", "alice") is False
assert session.model == ""
def test_task_run_reopens_against_its_owners_pinned_endpoint(database):
endpoint(database, "foreign", owner="bob", cached=("pinned-chat",))
own = endpoint(database)
task = SimpleNamespace(owner="alice", endpoint_url="", session_id=None, model="pinned-chat")
assert _resolve_run_endpoint(database, task, SimpleNamespace(model="pinned-chat")) == own.base_url
@pytest.mark.parametrize("owner", ["alice", None])
def test_task_run_can_find_visible_pins_on_owned_or_shared_endpoints(database, owner):
ep = endpoint(database, owner=owner)
task = SimpleNamespace(owner="alice", endpoint_url="", session_id=None, model="pinned-chat")
assert _resolve_run_endpoint(database, task, SimpleNamespace(model=None)) == ep.base_url
def test_task_run_does_not_autoselect_a_hidden_pin(database):
endpoint(database, hidden=("pinned-chat",))
task = SimpleNamespace(owner="alice", endpoint_url="", session_id=None, model="pinned-chat")
assert _resolve_run_endpoint(database, task, SimpleNamespace(model="pinned-chat")) == ""
@pytest.mark.parametrize("cached", [(), ("hidden-chat",)])
def test_research_default_uses_the_visible_pinned_id(database, cached):
ep = endpoint(database, cached=cached, hidden=("hidden-chat",))
assert _resolve_endpoint_runtime(ep, owner="alice") == (
ep.base_url + "/chat/completions", "pinned-chat", {},
)
def test_research_default_does_not_autoselect_a_hidden_pin(database):
ep = endpoint(database, hidden=("pinned-chat",))
assert _resolve_endpoint_runtime(ep, owner="alice") is None
def test_research_explicit_model_selection_is_preserved(database):
ep = endpoint(database, cached=("cached-chat",))
assert _resolve_endpoint_runtime(ep, owner="alice", model="explicit-chat")[1] == "explicit-chat"
def test_research_cached_default_keeps_priority_over_pins(database):
ep = endpoint(database, cached=("cached-chat",))
assert _resolve_endpoint_runtime(ep, owner="alice")[1] == "cached-chat"
+2
View File
@@ -1,5 +1,6 @@
"""Execution outcomes must reach the model, not just the tool-event UI."""
import json
import os
import pytest
@@ -7,6 +8,7 @@ from src.agent_tools.subprocess_tools import BashTool
from src.clean_agent_preview import preview_tool_result_text
@pytest.mark.skipif(os.name == "nt", reason="POSIX-only: enforcing containment (bubblewrap/process group) needed to run native bash; Windows windows_tree gives wall-clock only")
@pytest.mark.asyncio
async def test_failed_shell_retains_exit_status_and_both_streams_for_followup(tmp_path):
from tests.process_resource_helpers import launch_authority
+2
View File
@@ -3,6 +3,7 @@
P2-2 invariant: external bridge execution != local containment.
"""
import asyncio
import os
from types import SimpleNamespace
from unittest.mock import patch
@@ -180,6 +181,7 @@ async def test_external_record_does_not_grant_authority(tmp_path):
await containment.run(grant, "id")
@pytest.mark.skipif(os.name == "nt", reason="POSIX-only: enforcing containment (bubblewrap/process group) needed to run native bash; Windows windows_tree gives wall-clock only")
@pytest.mark.asyncio
async def test_native_local_bash_python_behavior_unchanged(tmp_path, monkeypatch):
"""4. Native local Bash/Python behavior is unchanged."""
+1 -1
View File
@@ -2,7 +2,7 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CHAT = (ROOT / "static/js/chat.js").read_text()
CHAT = (ROOT / "static/js/chat.js").read_text(encoding="utf-8")
def test_queued_stack_is_consumed_as_one_ordered_message():
@@ -0,0 +1,541 @@
"""Executable regression for the "regenerate" data-loss warning.
`regenerateFrom` (the per-message regenerate footer button), `resendUserMessage`
(footer Resend and the vision editor's "Regenerate message" button) and
`editUserMessage` (sending an edited user message) all permanently delete
every message after the point clicked via POST /api/session/{id}/truncate —
a real, unrecoverable server-side delete. Using any of them on anything but
the very last exchange used to do this with no warning at all; a mis-click
could silently erase an entire conversation. This drives the real chat.js
functions under Node, confirming:
1. Regenerating the *last* exchange (nothing after it) never prompts and
truncates directly — the everyday case must not gain friction.
2. Regenerating anything earlier prompts first, with the correct count of
messages that would be destroyed in the message text.
3. Cancelling the prompt aborts before any network call — nothing is
deleted.
4. Confirming the prompt proceeds with the correct `keep_count`.
5. Choosing "Fork from here" deletes nothing: it forks the chat just
before the user turn and replays the action (same text, or the edited
text) in the new chat instead.
All three entry points are exercised the same way since they wrap the same
destructive operation.
Unlike test_pr6020_browser_review_regressions.py's `_chat_smoke_source`
(which appends extra code to a copy of chat.js's own source — fine for
defining an extra exported function, since ES module static imports are
hoisted and evaluated before any top-level code regardless of where in the
file it appears), this harness needs its global stubs (document, fetch,
etc.) in place *before* chat.js's own import tree evaluates, since some of
those transitive dependencies touch browser globals at their own module top
level. So this loads chat.js via a real dynamic `import()` of its actual
file, in a small wrapper script that sets the stubs up first as plain
synchronous statements — dynamic `import()` runs in program order, unlike a
static `import` declaration.
"""
import json
import re
import shutil
import subprocess
from pathlib import Path
import pytest
_REPO = Path(__file__).resolve().parents[1]
_JS_DIR = _REPO / "static" / "js"
_HAS_NODE = shutil.which("node") is not None
def _versioned_js_uri(entry_js: Path, target_name: str) -> str:
"""file:// URI for target_name with the same cache-busting query string
entry_js's own import of it uses. ES modules cache by full URL, so
importing 'ui.js' here while chat.js imports 'ui.js?v=X' yields two module
instances, and patching uiMod here would never reach chat.js."""
src = entry_js.read_text(encoding="utf-8")
m = re.search(rf"from ['\"]\./{re.escape(target_name)}(\?[^'\"]*)?['\"]", src)
suffix = (m.group(1) or "") if m else ""
return (entry_js.parent / target_name).resolve().as_uri() + suffix
def _run_node(source: str) -> dict:
proc = subprocess.run(
["node", "--input-type=module"],
input=source,
capture_output=True,
text=True,
cwd=_REPO,
timeout=30,
)
assert proc.returncode == 0, proc.stderr
return json.loads(proc.stdout.strip().splitlines()[-1])
_HARNESS_PREAMBLE = """
globalThis.window = globalThis;
Object.defineProperty(globalThis, "navigator", { value: { platform: "" }, writable: true, configurable: true });
globalThis.addEventListener = () => {};
globalThis.removeEventListener = () => {};
globalThis.dispatchEvent = () => {};
globalThis.requestAnimationFrame = () => 0;
globalThis.cancelAnimationFrame = () => {};
globalThis.localStorage = { getItem() { return null; }, setItem() {}, removeItem() {} };
globalThis.location = {};
globalThis.history = {};
globalThis.MutationObserver = class { observe() {} };
globalThis.CustomEvent = class {};
globalThis.Storage = class {};
const fetchCalls = [];
globalThis.fetch = async (url, opts) => {
fetchCalls.push({ url, body: opts && opts.body ? JSON.parse(opts.body) : null });
const data = url.includes('/fork') ? { id: 'forked-session', name: 'Forked chat' } : {};
// A test can make the server refuse the cut (e.g. a stale message id).
const ok = !(url.includes('/truncate') && globalThis.__refuseTruncate);
return { ok, status: ok ? 200 : 404, json: async () => data, text: async () => '', headers: { get() { return null; } } };
};
class Element {
constructor() {
this.children = [];
this.classList = { add() {}, remove() {}, toggle() {}, contains: () => false };
this.style = { setProperty() {} };
this.dataset = {};
this._listeners = {};
this._html = '';
}
get innerHTML() { return this._html; }
set innerHTML(v) { this._html = v; this.children = []; }
querySelector() { return null; }
querySelectorAll() { return []; }
appendChild(child) { this.children.push(child); return child; }
addEventListener(type, fn) { (this._listeners[type] ||= []).push(fn); }
removeEventListener() {}
focus() {}
// Fire a listener and wait for it — the edit Send handler is async.
fire(type) {
const ev = { stopPropagation() {}, preventDefault() {} };
return Promise.all((this._listeners[type] || []).map(fn => fn(ev)));
}
}
class HTMLInputElement extends Element {
get value() { return this._value || ''; }
set value(v) { this._value = v; }
}
globalThis.HTMLInputElement = HTMLInputElement;
// A minimal .msg-list DOM: `msgs` is an ordered array of
// { role: 'user'|'ai' } describing one conversation. Each entry
// becomes a fake message element good enough for regenerateFrom /
// resendUserMessage's own traversal (classList.contains, .dataset,
// querySelector('.body'), querySelectorAll('[data-file-id]')).
function buildMsgEls(msgs) {
return msgs.map((m, i) => {
const body = new Element();
body.textContent = m === 'user' ? `question ${i}` : `answer ${i}`;
body.innerHTML = body.textContent;
return {
classList: { contains: (c) => c === (m === 'user' ? 'msg-user' : 'msg-ai') },
dataset: { raw: m === 'user' ? `question ${i}` : '' },
querySelector: (sel) => sel === '.body' ? body : null,
querySelectorAll: () => [],
nextSibling: null,
remove() {},
};
});
}
const sendClicks = [];
const messageInputEl = new HTMLInputElement();
const sendBtnEl = { click: () => sendClicks.push(messageInputEl.value) };
globalThis.document = {
body: new Element(),
head: new Element(),
documentElement: new Element(),
getElementById(id) {
if (id === 'message') return messageInputEl;
if (id === 'chat-history') return this._box;
return null;
},
querySelector(sel) { return sel === '.send-btn' ? sendBtnEl : null; },
querySelectorAll() { return []; },
createElement(tag) { return tag === 'input' ? new HTMLInputElement() : new Element(); },
createTextNode(text) { return { textContent: text }; },
addEventListener() {},
removeEventListener() {},
};
"""
def _scenario_script(func_call_js: str, roles: list, target_index: int, confirm_resolution, then_js: str = "", db_ids: bool = False, refuse_truncate: bool = False, file_ids: dict | None = None) -> str:
# Globals are stubbed above as plain synchronous statements *before* the
# dynamic import()s below run — unlike a static `import` declaration,
# dynamic import() executes in program order, so chat.js's transitive
# dependency tree only evaluates once the stubs are already in place.
script = _HARNESS_PREAMBLE
script += f"""
const chat = await import({json.dumps((_JS_DIR / "chat.js").resolve().as_uri())});
const {{ default: uiMod }} = await import({json.dumps(_versioned_js_uri(_JS_DIR / "chat.js", "ui.js"))});
const {{ default: sessionMod }} = await import({json.dumps(_versioned_js_uri(_JS_DIR / "chat.js", "sessions.js"))});
let currentSession = 'test-session';
const selectedSessions = [];
sessionMod.getCurrentSessionId = () => currentSession;
sessionMod.loadSessions = async () => {{}};
sessionMod.selectSession = async (id) => {{ selectedSessions.push(id); currentSession = id; }};
const confirmCalls = [];
uiMod.styledConfirm = async (message, opts) => {{
confirmCalls.push({{ message, opts }});
return {json.dumps(confirm_resolution)};
}};
uiMod.showError = () => {{}};
uiMod.showToast = () => {{}};
globalThis.__refuseTruncate = {json.dumps(refuse_truncate)};
const msgs = buildMsgEls({json.dumps(roles)});
// Persisted messages carry their database id; the page may have loaded
// only the newest of them, so ids (not positions) identify a message.
if ({json.dumps(db_ids)}) msgs.forEach((m, i) => {{ m.dataset.dbId = `db-${{i + 40}}`; }});
// Attach uploaded files ({{ index: file_id }}) the way rendered bubbles do.
for (const [i, fileId] of Object.entries({json.dumps(file_ids or {})})) {{
msgs[i].querySelectorAll = (sel) => sel === '[data-file-id]' ? [{{ dataset: {{ fileId }} }}] : [];
}}
// Record the attachments queued for each send as the button is clicked.
const sentAttachments = [];
sendBtnEl.click = () => {{ sendClicks.push(messageInputEl.value); sentAttachments.push(chat._pendingRegenAttachmentIds()); }};
const box = {{ querySelectorAll: () => msgs, _isBox: true }};
globalThis.document._box = box;
const target = msgs[{target_index}];
await {func_call_js};
{then_js}
const forkCall = fetchCalls.find(c => c.url.includes('/fork'));
console.log(JSON.stringify({{
confirmShown: confirmCalls.length > 0,
confirmMessage: confirmCalls.length ? confirmCalls[0].message : null,
confirmOpts: confirmCalls.length ? confirmCalls[0].opts : null,
truncateCalled: fetchCalls.some(c => c.url.includes('/truncate')),
truncateKeepCount: (fetchCalls.find(c => c.url.includes('/truncate')) || {{}}).body?.keep_count ?? null,
forkCalled: Boolean(forkCall),
forkUrl: forkCall ? forkCall.url : null,
forkKeepCount: forkCall ? forkCall.body.keep_count : null,
forkBody: forkCall ? forkCall.body : null,
truncateBody: (fetchCalls.find(c => c.url.includes('/truncate')) || {{}}).body || null,
selectedSessions,
sendClicks,
sentAttachments,
pendingAttachments: chat._pendingRegenAttachmentIds(),
}}));
process.exit(0);
"""
return script
# ---------------------------------------------------------------------------
# regenerateFrom (main chat footer "Regenerate from here" button)
# ---------------------------------------------------------------------------
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_regenerate_from_last_message_skips_prompt_and_truncates():
roles = ["user", "ai"] # target (index 1) IS the last message
script = _scenario_script("chat.regenerateFrom(target)", roles, 1, confirm_resolution=True)
result = _run_node(script)
assert result["confirmShown"] is False
assert result["truncateCalled"] is True
assert result["truncateKeepCount"] == 0 # keep everything before the user turn (index 0)
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_regenerate_from_earlier_message_prompts_with_correct_count():
roles = ["user", "ai", "user", "ai", "user", "ai"] # target (index 1) has 4 messages after it
script = _scenario_script("chat.regenerateFrom(target)", roles, 1, confirm_resolution=True)
result = _run_node(script)
assert result["confirmShown"] is True
assert "4 messages" in result["confirmMessage"]
assert result["truncateCalled"] is True
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_regenerate_from_earlier_message_cancel_aborts_without_truncating():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.regenerateFrom(target)", roles, 1, confirm_resolution=False)
result = _run_node(script)
assert result["confirmShown"] is True
assert result["truncateCalled"] is False
# ---------------------------------------------------------------------------
# resendUserMessage(..., { replaceFromHere: true }) (vision editor "Regenerate message")
# ---------------------------------------------------------------------------
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_replace_from_here_last_pair_skips_prompt():
roles = ["user", "ai"] # target (index 0) is the user turn of the last pair
script = _scenario_script(
"chat.resendUserMessage(target, { replaceFromHere: true })", roles, 0, confirm_resolution=True,
)
result = _run_node(script)
assert result["confirmShown"] is False
assert result["truncateCalled"] is True
assert result["truncateKeepCount"] == 0
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_replace_from_here_earlier_pair_prompts_with_correct_count():
roles = ["user", "ai", "user", "ai", "user", "ai"] # target (index 0) has 4 messages after its own pair
script = _scenario_script(
"chat.resendUserMessage(target, { replaceFromHere: true })", roles, 0, confirm_resolution=True,
)
result = _run_node(script)
assert result["confirmShown"] is True
assert "4 messages" in result["confirmMessage"]
assert result["truncateCalled"] is True
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_replace_from_here_cancel_aborts_without_truncating():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script(
"chat.resendUserMessage(target, { replaceFromHere: true })", roles, 0, confirm_resolution=False,
)
result = _run_node(script)
assert result["confirmShown"] is True
assert result["truncateCalled"] is False
# ---------------------------------------------------------------------------
# resendUserMessage(el) with no options (footer "Resend message")
# ---------------------------------------------------------------------------
# `dev` made plain resend replace-by-default (see
# test_resend_message_nondestructive.py): no options means replaceFromHere,
# so the footer Resend button now truncates everything after the clicked
# turn too, and gets the same warning. Append-only resend must be explicit.
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_default_earlier_pair_prompts_with_correct_count():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.resendUserMessage(target)", roles, 0, confirm_resolution=True)
result = _run_node(script)
assert result["confirmShown"] is True
assert "4 messages" in result["confirmMessage"]
assert result["truncateCalled"] is True
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_append_only_never_truncates_or_prompts():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.resendUserMessage(target, { append: true })", roles, 0, confirm_resolution=True)
result = _run_node(script)
assert result["confirmShown"] is False
assert result["truncateCalled"] is False
# ---------------------------------------------------------------------------
# editUserMessage (the pencil edit button, then Send)
# ---------------------------------------------------------------------------
# The edit Send handler is wired up inside editUserMessage, so these open the
# editor, type into it, and click the editor's own Send button.
_EDIT_AND_SEND = """
const _body = target.querySelector('.body');
const [_editor, _btnRow] = _body.children;
_editor.value = 'edited question';
await _btnRow.children[0].fire('click');
"""
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_last_message_skips_prompt_and_truncates():
roles = ["user", "ai"]
script = _scenario_script("chat.editUserMessage(target)", roles, 0, True, _EDIT_AND_SEND)
result = _run_node(script)
assert result["confirmShown"] is False
assert result["truncateCalled"] is True
assert result["truncateKeepCount"] == 0
assert result["sendClicks"] == ["edited question"]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_earlier_message_prompts_with_correct_count():
roles = ["user", "ai", "user", "ai", "user", "ai"] # target (index 2) has 2 messages after its own pair
script = _scenario_script("chat.editUserMessage(target)", roles, 2, True, _EDIT_AND_SEND)
result = _run_node(script)
assert result["confirmShown"] is True
assert "2 messages" in result["confirmMessage"]
assert result["confirmOpts"]["confirmText"] == "Delete and send"
assert result["truncateCalled"] is True
assert result["truncateKeepCount"] == 2
assert result["sendClicks"] == ["edited question"]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_earlier_message_cancel_aborts_without_truncating_or_sending():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.editUserMessage(target)", roles, 0, False, _EDIT_AND_SEND)
result = _run_node(script)
assert result["confirmShown"] is True
assert result["truncateCalled"] is False
assert result["sendClicks"] == []
# ---------------------------------------------------------------------------
# "Fork from here" — the prompt's alternate choice
# ---------------------------------------------------------------------------
# styledConfirm resolves to 'alternate' for its third button. Forking must
# not truncate anything; it forks just before the user turn (so the new chat
# ends where the action starts), switches to the fork, and replays there.
def _assert_forked_instead(result, keep_count, sent_text):
assert result["confirmShown"] is True
assert result["confirmOpts"]["alternateText"] == "Fork from here"
assert result["truncateCalled"] is False
assert result["forkCalled"] is True
assert "/api/session/test-session/fork" in result["forkUrl"]
assert result["forkKeepCount"] == keep_count
assert result["selectedSessions"] == ["forked-session"]
assert result["sendClicks"] == [sent_text]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_regenerate_fork_choice_forks_before_user_turn_and_resends():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.regenerateFrom(target)", roles, 3, "alternate")
_assert_forked_instead(_run_node(script), keep_count=2, sent_text="question 2")
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_fork_choice_forks_before_user_turn_and_resends():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.resendUserMessage(target)", roles, 2, "alternate")
_assert_forked_instead(_run_node(script), keep_count=2, sent_text="question 2")
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_fork_choice_forks_before_user_turn_and_sends_edit():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.editUserMessage(target)", roles, 2, "alternate", _EDIT_AND_SEND)
_assert_forked_instead(_run_node(script), keep_count=2, sent_text="edited question")
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_regenerate_fork_choice_keeps_the_user_message_attachments():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.regenerateFrom(target)", roles, 3, "alternate", file_ids={2: "file-abc"})
result = _run_node(script)
_assert_forked_instead(result, keep_count=2, sent_text="question 2")
assert result["sentAttachments"] == [["file-abc"]]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_fork_choice_keeps_the_user_message_attachments():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.resendUserMessage(target)", roles, 2, "alternate", file_ids={2: "file-abc"})
result = _run_node(script)
_assert_forked_instead(result, keep_count=2, sent_text="question 2")
assert result["sentAttachments"] == [["file-abc"]]
# ---------------------------------------------------------------------------
# Cutting by message id
# ---------------------------------------------------------------------------
# A long chat loads only its newest messages until the user scrolls up, so a
# position counted in the page is smaller than the real one. Truncating by
# that position deleted real messages above the clicked one. Every action
# sends the clicked message's database id when it has one.
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_regenerate_truncates_before_the_user_message_by_id():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.regenerateFrom(target)", roles, 3, True, db_ids=True)
result = _run_node(script)
assert result["truncateBody"] == {"before_msg_id": "db-42"}
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_resend_truncates_before_the_user_message_by_id():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.resendUserMessage(target)", roles, 2, True, db_ids=True)
result = _run_node(script)
assert result["truncateBody"] == {"before_msg_id": "db-42"}
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_truncates_before_the_user_message_by_id():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.editUserMessage(target)", roles, 2, True, _EDIT_AND_SEND, db_ids=True)
result = _run_node(script)
assert result["truncateBody"] == {"before_msg_id": "db-42"}
assert result["sendClicks"] == ["edited question"]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_fork_choice_forks_before_the_user_message_by_id():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.regenerateFrom(target)", roles, 3, "alternate", db_ids=True)
result = _run_node(script)
assert result["truncateCalled"] is False
assert result["forkBody"]["before_msg_id"] == "db-42"
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_fork_conversation_forks_through_the_reply_by_id():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.forkFrom(target)", roles, 1, True, db_ids=True)
result = _run_node(script)
assert result["forkBody"]["through_msg_id"] == "db-41"
# A refused cut (the server can't find the message id, e.g. one a previous
# regenerate already deleted) must stop the action. Sending anyway stacked a
# second reply under the same message.
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_regenerate_stops_when_the_server_refuses_the_cut():
roles = ["user", "ai"]
script = _scenario_script("chat.regenerateFrom(target)", roles, 1, True, db_ids=True, refuse_truncate=True)
result = _run_node(script)
assert result["truncateCalled"] is True
assert result["sendClicks"] == []
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_stops_when_the_server_refuses_the_cut():
roles = ["user", "ai"]
script = _scenario_script("chat.editUserMessage(target)", roles, 0, True, _EDIT_AND_SEND, db_ids=True, refuse_truncate=True)
result = _run_node(script)
assert result["truncateCalled"] is True
assert result["sendClicks"] == []
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_fork_choice_keeps_original_attachments():
roles = ["user", "ai", "user", "ai", "user", "ai"]
script = _scenario_script("chat.editUserMessage(target)", roles, 2, "alternate", _EDIT_AND_SEND, file_ids={2: "file-abc"})
result = _run_node(script)
_assert_forked_instead(result, keep_count=2, sent_text="edited question")
assert result["sentAttachments"] == [["file-abc"]]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_edit_destructive_resend_keeps_original_attachments():
roles = ["user", "ai", "user", "ai"]
script = _scenario_script("chat.editUserMessage(target)", roles, 2, True, _EDIT_AND_SEND, file_ids={2: "file-abc"})
result = _run_node(script)
assert result["truncateCalled"] is True
assert result["sentAttachments"] == [["file-abc"]]
@pytest.mark.skipif(not _HAS_NODE, reason="node binary not on PATH")
def test_refused_regenerate_does_not_leak_queued_attachments():
roles = ["user", "ai"]
script = _scenario_script("chat.regenerateFrom(target)", roles, 1, True, db_ids=True,
refuse_truncate=True, file_ids={0: "file-abc"})
result = _run_node(script)
assert result["truncateCalled"] is True
assert result["sendClicks"] == []
assert result["pendingAttachments"] is None
+2 -1
View File
@@ -2,6 +2,7 @@
import importlib
import json
import os
import sys
import types
from types import SimpleNamespace
@@ -1202,7 +1203,7 @@ async def test_write_file_inline_json_args(monkeypatch):
for b in blocks:
await _execute_without_run_context(execute_tool_block, b, owner="admin", workspace="/tmp")
assert captured.get("path") == "/tmp/wf.txt", (
assert captured.get("path") in ("/tmp/wf.txt", os.path.abspath("/tmp/wf.txt")), (
f"write_file did not decode inline JSON args; got path {captured.get('path')!r}"
)
+7
View File
@@ -8,6 +8,13 @@ from pathlib import Path
import pytest
import yaml
# The migration runs inside the searxng container entrypoint and preserves the
# settings file's owner/mode with os.fchmod/os.fchown, which do not exist on
# Windows, so every subprocess run of the script fails there before it starts.
pytestmark = pytest.mark.skipif(
os.name == "nt", reason="POSIX-only: migrate_searxng_settings.py uses os.fchmod/os.fchown"
)
ROOT = Path(__file__).resolve().parent.parent
MIGRATION = ROOT / "scripts" / "migrate_searxng_settings.py"
+167
View File
@@ -137,3 +137,170 @@ def test_collect_aggregate_deadline_yields_controlled_result(monkeypatch):
net = [s for s in out["services"] if s["name"] != "chromadb"]
assert all(s["status"] == sh.DOWN and s["meta"].get("error") == "timeout"
for s in net)
# ── Discovery failure is not "disabled" (#6154) ──
#
# `_gather_inputs()` fails soft to an empty inventory, and every probe reads an
# empty inventory as `disabled`. Without a record of *why* it is empty, a broken
# integrations file / email store / endpoint DB is reported as an intentionally
# unconfigured subsystem — and, with the rest disabled, `overall: "ok"`.
def _boom(*_a, **_k):
raise RuntimeError("secret-bearing detail: postgres://u:p@db/x")
def test_gather_inputs_records_each_failed_source(monkeypatch):
import core.database
import routes.email_helpers
import src.integrations
import src.settings
monkeypatch.setattr(src.settings, "load_settings", lambda: {"a": 1})
monkeypatch.setattr(src.integrations, "load_integrations", _boom)
monkeypatch.setattr(routes.email_helpers, "_list_email_accounts", _boom)
monkeypatch.setattr(core.database, "SessionLocal", _boom)
out = sh._gather_inputs()
assert out["settings"] == {"a": 1}
assert set(out["failed"]) == {"integrations", "accounts", "endpoints"}
# Controlled categories only — never the exception text.
assert set(out["failed"].values()) <= set(sh._ERROR_DETAIL)
assert "postgres" not in repr(out)
def test_gather_inputs_reports_no_failures_when_sources_load(monkeypatch):
import core.database
import routes.email_helpers
import src.integrations
import src.settings
class _DB:
def query(self, *_a):
return self
def filter(self, *_a):
return self
def all(self):
return []
def close(self):
pass
monkeypatch.setattr(src.settings, "load_settings", lambda: {})
monkeypatch.setattr(src.integrations, "load_integrations", lambda: [])
monkeypatch.setattr(routes.email_helpers, "_list_email_accounts", lambda: [])
monkeypatch.setattr(core.database, "SessionLocal", lambda: _DB())
assert sh._gather_inputs()["failed"] == {}
@pytest.mark.parametrize("source,service", [
("settings", "searxng"),
("integrations", "ntfy"),
("accounts", "email"),
("endpoints", "providers"),
])
def test_failed_source_is_not_reported_as_disabled(monkeypatch, source, service):
import asyncio
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
"settings": {"search_provider": "disabled"},
"integrations": [], "accounts": [], "endpoints": [],
"failed": {source: "network_error"},
})
out = asyncio.run(sh.collect_service_health(None, None))
entry = next(s for s in out["services"] if s["name"] == service)
assert entry["status"] != sh.DISABLED
assert entry["meta"]["error"] == sh.CONFIG_SOURCE_ERROR
assert entry["meta"]["source"] == source
assert entry["meta"]["source_error"] == "network_error"
# A failed source must reach the aggregate verdict.
assert out["overall"] != sh.OK
assert set(out) == {"overall", "services", "timestamp"}
def test_all_sources_failing_cannot_report_overall_ok(monkeypatch):
# The exact shape from the report: settings loads and says search is off,
# the other three sources raise. Before the fix this was overall "ok" with
# five "disabled" entries.
import asyncio
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
"settings": {"search_provider": "disabled"},
"integrations": [], "accounts": [], "endpoints": [],
"failed": {"integrations": "error", "accounts": "error",
"endpoints": "error"},
})
out = asyncio.run(sh.collect_service_health(None, None))
by_name = {s["name"]: s for s in out["services"]}
assert out["overall"] == sh.DEGRADED
# Loaded-and-genuinely-empty still reads as disabled.
assert by_name["searxng"]["status"] == sh.DISABLED
for name in ("ntfy", "email", "providers"):
assert by_name[name]["status"] == sh.DEGRADED
def test_empty_inventory_without_failure_stays_disabled(monkeypatch):
import asyncio
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
"settings": {"search_provider": "disabled"},
"integrations": [], "accounts": [], "endpoints": [], "failed": {},
})
out = asyncio.run(sh.collect_service_health(_Store(True), _Store(True)))
assert out["overall"] == sh.OK
assert all(s["status"] == sh.DISABLED
for s in out["services"] if s["name"] != "chromadb")
def test_one_failed_source_still_probes_the_others(monkeypatch):
# A broken endpoint DB must not stop ntfy/email from being probed.
import asyncio
probed = []
def _probe(name):
def _fn(*_a, **_k):
probed.append(name)
return sh._svc(name, sh.OK, "probed")
return _fn
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
"settings": {}, "integrations": [], "accounts": [], "endpoints": [],
"failed": {"endpoints": "timeout"},
})
monkeypatch.setattr(sh, "searxng_health", _probe("searxng"))
monkeypatch.setattr(sh, "ntfy_health", _probe("ntfy"))
monkeypatch.setattr(sh, "email_health", _probe("email"))
monkeypatch.setattr(sh, "providers_health", _probe("providers"))
out = asyncio.run(sh.collect_service_health(None, None))
assert sorted(probed) == ["email", "ntfy", "searxng"]
assert "providers" not in probed
by_name = {s["name"]: s for s in out["services"]}
assert by_name["providers"]["status"] == sh.DEGRADED
assert by_name["ntfy"]["status"] == sh.OK
def test_source_failure_entry_carries_no_raw_detail(monkeypatch):
import asyncio
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
"settings": {}, "integrations": [], "accounts": [], "endpoints": [],
"failed": {"accounts": "auth_or_protocol_error"},
})
out = asyncio.run(sh.collect_service_health(None, None))
entry = next(s for s in out["services"] if s["name"] == "email")
assert entry["detail"] == (
"Could not read configuration (configuration source could not be read).")
assert set(entry["meta"]) == {"error", "source", "source_error"}
def test_collect_tolerates_inputs_without_a_failed_key(monkeypatch):
# `_gather_inputs()` is monkeypatched in a number of existing tests (and by
# anything calling the collector with a stub); a payload predating `failed`
# must still probe normally rather than raise.
import asyncio
monkeypatch.setattr(sh, "_gather_inputs", lambda: {
"settings": {"search_provider": "disabled"},
"integrations": [], "accounts": [], "endpoints": [],
})
out = asyncio.run(sh.collect_service_health(_Store(True), _Store(True)))
assert out["overall"] == sh.OK
+42 -1
View File
@@ -8,7 +8,7 @@ from sqlalchemy.orm import sessionmaker
from core.database import Base
from core.database import ChatMessage as DbChatMessage
from core.database import Session as DbSession
from src.session_search import SessionSearchResult, search_session_messages
from src.session_search import SessionSearchResult, _snippet, search_session_messages
def _db(with_fts=True):
@@ -61,6 +61,47 @@ def _has_fts(db):
)
def test_snippet_locates_quoted_phrase_instead_of_raw_query():
content = "A" * 100 + "The session search result is useful." + "Z" * 100
snippet = _snippet(content, '"session search"', radius=20)
assert "session search" in snippet
assert snippet.startswith("...")
assert snippet.endswith("...")
def test_snippet_locates_punctuation_bearing_term_case_insensitively():
content = "A" * 100 + "Use FOO_BAR.py for routing." + "Z" * 100
snippet = _snippet(content, "foo_bar.py!", radius=20)
assert "FOO_BAR.py" in snippet
def test_snippet_uses_first_meaningful_matching_term():
content = "A" * 100 + "second appears here; first appears later." + "Z" * 100
snippet = _snippet(content, "first second", radius=12)
assert "second appears" in snippet
assert "first appears" not in snippet
def test_snippet_falls_back_to_content_start_when_no_term_matches():
content = "Beginning of the transcript, followed by more detail."
assert _snippet(content, '"missing phrase"', radius=10) == content[:20]
def test_snippet_preserves_boundary_markers_and_truncation():
content = "A" * 50 + "target" + "Z" * 50
snippet = _snippet(content, "target", radius=5)
assert snippet == "...AAAAAtargetZZZZZ..."
def test_session_search_uses_fts_and_returns_context():
db = _db(with_fts=True)
try:
@@ -0,0 +1,54 @@
"""Inline frontmatter lists must preserve quoted values across save/load cycles."""
import importlib.util
from pathlib import Path
import sys
import unittest
# Load the dependency-free formatter without starting the services package.
_PATH = Path(__file__).resolve().parents[1] / "services" / "memory" / "skill_format.py"
_SPEC = importlib.util.spec_from_file_location("_skill_list_format_under_test", _PATH)
_FORMAT = importlib.util.module_from_spec(_SPEC)
sys.modules[_SPEC.name] = _FORMAT
_SPEC.loader.exec_module(_FORMAT)
class FrontmatterListEscapesTests(unittest.TestCase):
def test_skill_tags_survive_repeated_save_load_cycles(self):
tags = ['review "release, notes', 'use "search", then summarize', 'publish']
skill = _FORMAT.Skill(name="release-review", tags=tags)
for _ in range(5):
skill = _FORMAT.Skill.from_markdown(skill.to_markdown())
self.assertEqual(skill.tags, tags)
def test_escaped_quotes_preserve_each_list_field(self):
for field in ("tags", "platforms", "requires_toolsets", "fallback_for_toolsets"):
with self.subTest(field=field):
values = ['review "release, notes', 'publish']
skill = _FORMAT.Skill(name="release-review", **{field: values})
restored = _FORMAT.Skill.from_markdown(skill.to_markdown())
self.assertEqual(getattr(restored, field), values)
def test_backslash_parity_does_not_hide_quotes_or_list_separators(self):
for count in range(1, 5):
with self.subTest(backslashes=count):
values = ['review ' + '\\' * count + '"release, notes', 'path, ' + '\\' * count, 'publish']
text = _FORMAT.emit_frontmatter({"tags": values})
parsed, _ = _FORMAT.parse_frontmatter(f"---\n{text}\n---\n")
self.assertEqual(parsed["tags"], values)
def test_single_quoted_backslashes_remain_literal(self):
text = "---\ntags: ['C:\\', next]\n---\n"
parsed, _ = _FORMAT.parse_frontmatter(text)
self.assertEqual(parsed["tags"], ["C:\\", "next"])
def test_nested_lists_preserve_quoted_brackets_and_commas(self):
values = [['review "], notes', 'publish'], 'archive']
text = _FORMAT.emit_frontmatter({"tags": values})
parsed, _ = _FORMAT.parse_frontmatter(f"---\n{text}\n---\n")
self.assertEqual(parsed["tags"], values)
if __name__ == "__main__":
unittest.main()
+83
View File
@@ -0,0 +1,83 @@
"""Renaming a skill onto a name the owner already uses must be refused.
``update_skill`` only checked whether the target directory existed. A
skill in another category with the wanted name lives in a different
directory, so the rename went through and the owner ended up with two
skills sharing one name. Update and delete look skills up by name, so the
next DELETE removed whichever of the two ``os.walk`` reached first, which
was often the one the user did not pick.
"""
import pytest
from routes.skills_routes import SkillUpdateRequest, setup_skills_routes
from services.memory.skills import SkillsManager
from tests.test_skills_routes_owner_update import _request, _route_handler, _write_skill_md
def _two_skills(tmp_path, owner="alice"):
mgr = SkillsManager(str(tmp_path))
mgr.add_skill(name="deploy-app", description="Deploy the web app", category="ops",
source="user", procedure=["ship it"], owner=owner)
mgr.add_skill(name="draft-notes", description="Draft meeting notes", category="writing",
source="user", procedure=["write"], owner=owner)
return mgr
def _names(mgr, owner="alice"):
return sorted((s["name"], s["category"]) for s in mgr.load(owner=owner))
def test_rename_onto_a_name_in_another_category_is_refused(tmp_path):
mgr = _two_skills(tmp_path)
assert mgr.update_skill("draft-notes", {"name": "deploy-app"}, owner="alice") is False
assert _names(mgr) == [("deploy-app", "ops"), ("draft-notes", "writing")]
def test_delete_after_refused_rename_removes_only_the_named_skill(tmp_path):
mgr = _two_skills(tmp_path)
mgr.update_skill("draft-notes", {"name": "deploy-app"}, owner="alice")
assert mgr.delete_skill("draft-notes", owner="alice") is True
assert _names(mgr) == [("deploy-app", "ops")]
def test_rename_to_a_free_name_still_moves_the_skill(tmp_path):
mgr = _two_skills(tmp_path)
assert mgr.update_skill("draft-notes", {"name": "meeting-notes"}, owner="alice") is True
assert _names(mgr) == [("deploy-app", "ops"), ("meeting-notes", "writing")]
def test_another_owners_skill_does_not_block_the_rename(tmp_path):
skills_root = tmp_path / "skills"
_write_skill_md(skills_root, "bob-cat", "caveman-mode", "bob")
_write_skill_md(skills_root, "alice-cat", "terse-mode", "alice")
mgr = SkillsManager(str(tmp_path))
assert mgr.update_skill("terse-mode", {"name": "caveman-mode"}, owner="alice") is True
assert [s["name"] for s in mgr.load(owner="alice")] == ["caveman-mode"]
assert [s["name"] for s in mgr.load(owner="bob")] == ["caveman-mode"]
def test_keeping_the_same_name_is_not_a_conflict(tmp_path):
mgr = _two_skills(tmp_path)
assert mgr.update_skill("draft-notes", {"name": "Draft Notes", "status": "published"},
owner="alice") is True
assert [s["status"] for s in mgr.load(owner="alice") if s["name"] == "draft-notes"] == ["published"]
@pytest.mark.asyncio
async def test_put_route_answers_409_for_a_taken_name(tmp_path):
from fastapi import HTTPException
mgr = _two_skills(tmp_path)
update_route = _route_handler(setup_skills_routes(mgr), "/api/skills/{skill_id}", "PUT")
with pytest.raises(HTTPException) as exc:
await update_route(_request("alice"), "draft-notes", SkillUpdateRequest(name="deploy-app"))
assert exc.value.status_code == 409
assert _names(mgr) == [("deploy-app", "ops"), ("draft-notes", "writing")]
+99
View File
@@ -27,3 +27,102 @@ def test_dsml_tool_markup_strips_to_empty_visible_text():
def test_chunk_split_tool_call_prefix_is_buffered_and_not_persisted():
assert _streamed_tool_markup_starts('I will search<tool_cal')
assert _strip_incomplete_tool_markup_tail('I will search<tool_cal') == 'I will search'
def test_bare_less_than_and_comparison_operators_false_alarm_recognition():
# A bare "<" starts buffering because it could be a tool-call tag prefix like "<tool_call>"
assert _streamed_tool_markup_starts(' <')
assert _streamed_tool_markup_starts('x <')
assert _streamed_tool_markup_starts('<tool_call')
assert _streamed_tool_markup_starts('<invoke')
# Once subsequent characters arrive and cannot become a tool-call tag, false alarm is detected
assert not _streamed_tool_markup_starts(' < 2')
assert not _streamed_tool_markup_starts('x < 2')
assert not _streamed_tool_markup_starts('len(items) < 2')
assert not _streamed_tool_markup_starts(' < 2:')
assert not _streamed_tool_markup_starts('x <= y')
assert not _streamed_tool_markup_starts('a < b')
async def test_stream_agent_loop_does_not_truncate_at_less_than_operator(monkeypatch):
import json
import src.agent_loop as al
async def fake_stream(*args, **kwargs):
for d in ["def is_short(items):\n", " return len(items)", " <", " 2\n"]:
yield "data: " + json.dumps({"delta": d}) + "\n\n"
yield "data: [DONE]\n\n"
monkeypatch.setattr(al, "get_setting", lambda k, d=None: d, raising=False)
monkeypatch.setattr(al, "get_mcp_manager", lambda: None, raising=False)
monkeypatch.setattr(al, "estimate_tokens", lambda *a, **k: 10, raising=False)
monkeypatch.setattr(al, "stream_llm_with_fallback", fake_stream, raising=False)
deltas = []
async for chunk in al.stream_agent_loop(
"http://local.test/v1",
"qwen3.6-35b-a3b-heretic",
[{"role": "user", "content": "Write python function"}],
max_rounds=1,
owner="test_user",
):
for line in chunk.split("\n"):
line = line.strip()
if line.startswith("data: ") and line[6:] != "[DONE]":
try:
payload = json.loads(line[6:])
if "delta" in payload:
deltas.append(payload["delta"])
except Exception:
pass
full_text = "".join(deltas)
assert full_text == "def is_short(items):\n return len(items) < 2\n"
async def test_stream_agent_loop_still_hides_real_tool_markup(monkeypatch):
import json
import src.agent_loop as al
async def fake_stream(*args, **kwargs):
chunks = [
"Sure.",
" <",
"tool",
"_call",
'>{"name":"host_shell","arguments":{"command":"pwd"}}',
"</",
"tool",
"_call",
">",
" done",
]
for c in chunks:
yield "data: " + json.dumps({"delta": c}) + "\n\n"
yield "data: [DONE]\n\n"
monkeypatch.setattr(al, "get_setting", lambda k, d=None: d, raising=False)
monkeypatch.setattr(al, "get_mcp_manager", lambda: None, raising=False)
monkeypatch.setattr(al, "estimate_tokens", lambda *a, **k: 10, raising=False)
monkeypatch.setattr(al, "stream_llm_with_fallback", fake_stream, raising=False)
deltas = []
async for chunk in al.stream_agent_loop(
"http://local.test/v1",
"qwen3.6-35b-a3b-heretic",
[{"role": "user", "content": "run command"}],
max_rounds=1,
owner="test_user",
):
for line in chunk.split("\n"):
line = line.strip()
if line.startswith("data: ") and line[6:] != "[DONE]":
try:
payload = json.loads(line[6:])
if "delta" in payload:
deltas.append(payload["delta"])
except Exception:
pass
assert "".join(deltas) == "Sure. done"
+1 -1
View File
@@ -2,7 +2,7 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CHAT = (ROOT / "static/js/chat.js").read_text()
CHAT = (ROOT / "static/js/chat.js").read_text(encoding="utf-8")
def _visibility_handler() -> str:
+18 -11
View File
@@ -28,6 +28,11 @@ def standalone_dispatch_authority(monkeypatch):
server_authorized_executor(tool_execution.execute_tool_block))
def _native(path: str) -> str:
"""POSIX-style literal -> native separators (the policy splits on os.sep)."""
return path.replace("/", os.sep)
def _make_block(tool_type, content):
return SimpleNamespace(tool_type=tool_type, content=content)
@@ -36,27 +41,27 @@ def _make_block(tool_type, content):
def test_sensitive_ssh_dir():
from src.tool_execution import _is_sensitive_path
assert _is_sensitive_path("/home/user/.ssh/authorized_keys")
assert _is_sensitive_path(os.path.expanduser("~") + "/.ssh/config")
assert _is_sensitive_path(_native("/home/user/.ssh/authorized_keys"))
assert _is_sensitive_path(_native(os.path.expanduser("~") + "/.ssh/config"))
def test_sensitive_gnupg_dir():
from src.tool_execution import _is_sensitive_path
assert _is_sensitive_path("/home/user/.gnupg/pubring.kbx")
assert _is_sensitive_path(_native("/home/user/.gnupg/pubring.kbx"))
def test_sensitive_shell_rc():
from src.tool_execution import _is_sensitive_path
assert _is_sensitive_path("/home/user/.bashrc")
assert _is_sensitive_path("/home/user/.zshrc")
assert _is_sensitive_path("/home/user/.profile")
assert _is_sensitive_path(_native("/home/user/.bashrc"))
assert _is_sensitive_path(_native("/home/user/.zshrc"))
assert _is_sensitive_path(_native("/home/user/.profile"))
def test_sensitive_key_filenames():
from src.tool_execution import _is_sensitive_path
assert _is_sensitive_path("/tmp/id_rsa")
assert _is_sensitive_path("/tmp/id_ed25519")
assert _is_sensitive_path("/tmp/authorized_keys")
assert _is_sensitive_path(_native("/tmp/id_rsa"))
assert _is_sensitive_path(_native("/tmp/id_ed25519"))
assert _is_sensitive_path(_native("/tmp/authorized_keys"))
def test_non_sensitive_path():
@@ -186,9 +191,11 @@ def test_allows_agent_workspace(tmp_path):
os.unlink(target)
def test_allows_tmp(tmp_path):
def test_allows_tmp(tmp_path, monkeypatch):
"""Paths under /tmp (or its realpath) must resolve cleanly."""
from src.tool_execution import _resolve_tool_path
# Windows has no /tmp; its temp root is only allowed via $TMPDIR.
monkeypatch.setenv("TMPDIR", str(tmp_path))
f = tmp_path / "confinement-test.txt"
f.write_text("ok")
resolved = _resolve_tool_path(str(f))
@@ -358,4 +365,4 @@ async def test_write_file_dispatch_blocks_cron(monkeypatch):
def test_application_secrets_are_sensitive_paths(filename):
from src.tool_execution import _is_sensitive_path
assert _is_sensitive_path(f"/tmp/odysseus-data/{filename}")
assert _is_sensitive_path(_native(f"/tmp/odysseus-data/{filename}"))
+132
View File
@@ -0,0 +1,132 @@
"""Issue #5573 — the vision model's description must not read as the user's own words.
For a text-only chat model the description comes from a separate vision call and
is appended to the user's message. Unlabelled, the chat model answers the prose
instead of the image — reviewing the user's "writing" back to them.
"""
import importlib
from types import SimpleNamespace
import pytest
DESCRIPTION = "A chocolate cake, plated with a mirror glaze and gold leaf."
QUESTION = "What am I looking at?"
class _UploadHandler:
def __init__(self, file_info):
self._file_info = file_info
def resolve_upload(self, att_id, owner=None):
return self._file_info if att_id == self._file_info["id"] else None
def is_image_file(self, *_args, **_kwargs):
return True
def _no_vision_call(*_args, **_kwargs):
raise AssertionError("the vision model must not be called on this path")
@pytest.fixture
def vision_env(monkeypatch, tmp_path):
"""Text-only chat model with one image attachment, no DB and no real vision call.
Other tests drop src.chat_handler from sys.modules, so resolve it here rather
than at import time — otherwise the patches land on a different module object
than the one under test.
"""
module = importlib.import_module("src.chat_handler")
monkeypatch.setattr(module, "UPLOAD_DIR", str(tmp_path))
monkeypatch.setattr(module, "model_supports_vision", lambda *a, **k: False)
monkeypatch.setattr(module, "_sync_upload_vision_to_gallery", lambda *a, **k: None)
monkeypatch.setattr(module, "build_user_content", lambda text, *a, **k: text)
monkeypatch.setattr(
importlib.import_module("src.settings"), "get_setting", lambda key, default=None: default
)
return SimpleNamespace(module=module, tmp_path=tmp_path, file_info={
"id": "img-1", "name": "cake.jpg", "path": str(tmp_path / "cake.jpg"),
"mime": "image/jpeg", "size": 1, "hash": "h",
})
async def _preprocess(env):
handler = env.module.ChatHandler(
session_manager=None, memory_manager=None, chat_processor=None,
research_handler=None, preset_manager=None,
upload_handler=_UploadHandler(env.file_info),
)
sess = SimpleNamespace(model="chat-model", endpoint_url="", owner="user", id="s1")
enhanced, *_ = await handler.preprocess_message(
QUESTION, ["img-1"], sess, auto_opened_docs=[],
)
return enhanced
def _describe(env, monkeypatch, text):
monkeypatch.setattr(env.module, "analyze_image_with_vl_result",
lambda *a, **k: {"text": text, "model": "vl-model"})
def _label_above(enhanced, description):
"""The line the description is introduced by."""
return enhanced.split(description)[0].rstrip("\n").rsplit("\n", 1)[-1]
@pytest.mark.asyncio
async def test_description_is_marked_as_not_the_users_message(vision_env, monkeypatch):
_describe(vision_env, monkeypatch, DESCRIPTION)
enhanced = await _preprocess(vision_env)
assert QUESTION in enhanced, "the user's own words must survive untouched"
label = _label_above(enhanced, DESCRIPTION)
assert label.startswith("[") and "user" in label.lower(), (
"the description is appended as bare prose, so the chat model reads it as "
f"the user's own writing. Got:\n{enhanced}"
)
@pytest.mark.asyncio
async def test_cached_description_is_labelled_too(vision_env, monkeypatch):
"""The cache holds an auto description or a hand-corrected one, and the label
has to be true either way — it is a description of the image regardless."""
cache = vision_env.tmp_path / ".vision"
cache.mkdir()
(cache / "img-1.txt").write_text(DESCRIPTION, encoding="utf-8")
monkeypatch.setattr(vision_env.module, "analyze_image_with_vl_result", _no_vision_call)
enhanced = await _preprocess(vision_env)
label = _label_above(enhanced, DESCRIPTION)
assert label.startswith("[") and "user" in label.lower(), (
f"a cached description is injected with no label. Got:\n{enhanced}"
)
@pytest.mark.asyncio
async def test_client_side_image_marker_is_preserved(vision_env, monkeypatch):
"""chatRenderer.js and sessions.js strip the block by matching `[Image: <name>]\\n`.
Renaming that marker leaves the raw description rendered in the user's bubble.
"""
_describe(vision_env, monkeypatch, DESCRIPTION)
assert "[Image: cake.jpg]\n" in await _preprocess(vision_env)
@pytest.mark.asyncio
async def test_no_dangling_label_when_the_vision_model_returns_nothing(vision_env, monkeypatch):
_describe(vision_env, monkeypatch, "")
enhanced = await _preprocess(vision_env)
assert enhanced.endswith("[Image: cake.jpg]\n"), (
"an empty description must leave the bare marker, trailing newline included "
f"— the client's strip regex needs it. Got:\n{enhanced!r}"
)
@pytest.mark.asyncio
async def test_vision_capable_model_path_is_unchanged(vision_env, monkeypatch):
monkeypatch.setattr(vision_env.module, "model_supports_vision", lambda *a, **k: True)
monkeypatch.setattr(vision_env.module, "analyze_image_with_vl_result", _no_vision_call)
assert await _preprocess(vision_env) == f"{QUESTION}\n\n[Image attached: cake.jpg]"
+1 -1
View File
@@ -5,7 +5,7 @@ from bs4 import BeautifulSoup
from src.visual_report import generate_visual_report
SOURCE = (Path(__file__).resolve().parents[1] / "src/visual_report.py").read_text()
SOURCE = (Path(__file__).resolve().parents[1] / "src/visual_report.py").read_text(encoding="utf-8")
def test_export_bookmark_is_anchored_and_expands_on_demand():
@@ -2,8 +2,8 @@ from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
RENDERER = (ROOT / "static/js/chatRenderer.js").read_text()
CHAT = (ROOT / "static/js/chat.js").read_text()
RENDERER = (ROOT / "static/js/chatRenderer.js").read_text(encoding="utf-8")
CHAT = (ROOT / "static/js/chat.js").read_text(encoding="utf-8")
def test_saved_web_search_tool_icon_has_no_action_target():
+4 -2
View File
@@ -21,7 +21,7 @@ described as a switch that turns something off, the read rejects `0`, `false`,
`no` and `off` and treats everything else as on. The `Default` column is the
value the code falls back to when the variable is unset, quoted from the source.
The source tree reads **147** `ODYSSEUS_*` variables: 90 an operator may want to set, and 57 that are internal - sentinels, fixture switches, capture hooks and development tooling. The internal ones are listed too, in their own section, so this page can be checked against the source mechanically.
The source tree reads **149** `ODYSSEUS_*` variables: 92 an operator may want to set, and 57 that are internal - sentinels, fixture switches, capture hooks and development tooling. The internal ones are listed too, in their own section, so this page can be checked against the source mechanically.
> This page is generated. Edit `scripts/generate_env_reference.py` and
> re-run it; `tests/test_env_reference.py` enforces that the committed page
@@ -77,6 +77,8 @@ The source tree reads **147** `ODYSSEUS_*` variables: 90 an operator may want to
| Variable | Default | Read in | What it does |
|---|---|---|---|
| `ODYSSEUS_DISABLE_MCP` | `''` | `src/builtin_mcp.py` | Truthy disables MCP entirely, as an escape hatch for compatibility problems with a server. |
| `ODYSSEUS_LOCAL_MCP_READ_TOOLS` | *unset* | `src/tool_capabilities.py` | Security-relevant. Comma-separated exact MCP read-tool names on configured stdio MCP servers. Only stdio servers qualify. Declarations do not override built-in tool effects. Output remains untrusted to prevent privilege escalation, but allows continuation to declared write tools on the same stdio server without external taint. Stdio transport does not guarantee network isolation. Automatic same-store writes carry residual prompt-injection risk. |
| `ODYSSEUS_LOCAL_MCP_WRITE_TOOLS` | *unset* | `src/tool_capabilities.py` | Security-relevant. Comma-separated exact MCP write-tool names on configured stdio MCP servers. Only stdio servers qualify. Declarations do not override built-in tool effects. Allowed to execute after declared local reads on the same stdio server, but blocked once external untrusted context enters the run. Authorization must still originate from the trusted request grant. |
| `ODYSSEUS_MAX_VISUAL_EVIDENCE_FRAMES` | `'3'` | `src/agent_loop.py` | How many video frames one tool result may contribute. Clamped to 1-8. |
| `ODYSSEUS_MAX_VISUAL_EVIDENCE_IMAGES` | `'1'` | `src/agent_loop.py` | How many images one tool result may contribute to the model turn. Clamped to 1-8. |
| `ODYSSEUS_MCP_ALLOWED_COMMANDS` | `''` | `src/agent_tools/admin_tools.py` | Security-relevant. Comma-separated allowlist of MCP launcher basenames the agent may start. Empty by default, and the deny list still wins. |
@@ -300,7 +302,7 @@ reads three ways, because no single pattern covers the codebase:
lines, so one read lives inside a string literal.
The three passes are not redundancy. A line-based grep for a direct
`os.environ.get("ODYSSEUS_...` call finds 117 of the 147 variables on this
`os.environ.get("ODYSSEUS_...` call finds 117 of the 149 variables on this
page. What it misses is reads through an env-reader helper, reads whose call
spans more than one line, reads whose variable name is held in a module
constant, and reads through a mapping passed in as an argument - which is the