The AI assistant (developer)
Read-only tools and the connection server
flextool/assistant/ holds read-only tools that let an AI assistant answer questions about one project: input data
by scenario, results, run logs and slack, and the documentation; the connection server that gives them to an AI
agent; and FlexTool's own chat with an AI service ("AI assistant chat" below). User page:
The AI assistant.
flextool/assistant/__init__.py imports nothing; every module imports what it needs when first used.
- Registry (
registry.py). Each tool module (tools/orientation,discovery,inputs,results,diagnostics) lists itsTools inTOOLS(name, title, description ending with the data-not-instructions sentence, a closed JSON schema without ranges, handler, area,small_tier).call_tool(ctx, name, args)is the only way to run one and never raises: it clamps the caps arguments (caps.py:--result-cappresets and hard ceilings), validates against the schema withjsonschema(mcp does not validate on stdio), runs the handler and fits the payload withfit_json(compact JSON, whole trailing rows dropped with atruncatednote). AToolErrorcarries a message key oferrors.MESSAGES, the one table of user-facing texts; any other exception becomes the fault message, with the traceback on stderr only.tools_for_tier("small")is the basic set. - Redaction (
redact.py) runs once, infit_json, on every string of every payload and every message: the project folder →<project>, home →~, host and user names, other absolute paths → their last part. - Inputs (
inputs/_db.py). One read-only, percent-encoded SQLite URI open per file (mode=rocounts only withuri=true; a raw#or%makes SQLite create another file),sqlite_timeout=5; a fixed list of item types is fetched and the mapping closed, so no file handle stays open. The scenario view (rank order,entity_alternative,is_enabled) is computed in our code; spinedb-api's scenario filter is only the oracle of its test. Input sources and their numbers come fromgui.input_sources/gui.settings_io(Tk-free), read only; a spreadsheet is read through its conversionintermediate/<stem>.sqlite. - Results (
results/). A run folder is offered only whenrun_record.results_usablesays so (architecture.md Run record); the message comes fromunusable_code. Tables are read and aggregated wide (flextool/lean_parquet.pyfooter layout); only a reduced frame is unpivoted.check_aggregationrefuses sums with no meaning (power over time, annual values over periods, levels, ratios). Units are worded only byunit_wording.py, as the default convention (units are not enforced by FlexTool). Comparison files are used while their_metadata.jsonmembers match the current successful runs, else the member folders. - Docs (
docs_store.py,flextool/docs_build/). The build step_flextool_build.BuildPycopies the curated Markdown (docs_build/scope.CURATED_DOCS) intoflextool/assistant/_docs/md/; an editable install reads the clone'sdocs/. On first use the formulation page, the results catalogue and the BM25 index are generated by the installed FlexTool into the user cache folder, keyed by FlexTool version + source hash. The web docs generate the same two pages with the same generator (tools/mkdocs_hooks.py), plus the web-only parameter catalogue (scope.WEB_ONLY_PAGES,docs_build/parameters.py: one large table per class, whichdescribe_parameteralready serves, so it stays out of the assistant's index);python -m flextool.docs_build --xlsxwrites it as an Excel data dictionary.
The connection server for agents (flextool-assistant --mcp, mcp_server.py, mcp 2.x low-level Server,
telemetry middleware removed). The protocol uses stdin and stdout, so flextool_assistant_entry.py, a
standard-library-only top-level module, keeps private duplicates of fd 0 and fd 1 for the protocol, then points
fd 1 and sys.stdout at stderr and fd 0 at the null device (on Windows also the Win32 standard handles, and
binary mode on the duplicates) before import flextool, whose dependencies and start-up checks may print.
The server's streams are explicit anyio wrappers over those duplicates (UTF-8, \n), so mcp's own stdio claim
is skipped. Tool calls run in a worker thread that takes the context's lock, so ping and tools/list answer
during a slow call. The server sends the instructions of prompt.instructions (data use, units convention,
vocabulary), lists the tools with read-only annotations, serves the docs pages as flextool-docs:///<page>
resources and the level texts (prompt.ADDENDA) as the prompts basic, standard, advanced. --tier basic
lists and runs only the basic set and defaults to the small result cap.
When import flextool stops with CompatRuntimeRequired (env_check: this processor needs the compatible polars
build), the entry module loads mcp_server.py by path (it imports only the standard library at module level) and
serves one tool, flextool_setup_problem, that returns the fix. The assistant options have no environment-variable
twins.
--call TOOL [JSON | @file.json] runs one tool through the same call_tool and prints its text (support and
testing). Tests: tests/assistant/ (test_server_stdio.py drives the installed command over real stdio and fails
on any non-protocol byte on stdout); CI smoke: .github/scripts/mcp_smoke.py (wheel check in publish.yml, the
Toolbox bundle, the old-CPU emulation's degraded server).
AI assistant chat
flextool-assistant without --mcp / --call is the chat (chat_cli.py); the same layers serve the FlexTool
window ("AI assistant in the FlexTool window" below). --mcp and --call import none of the service libraries (anthropic, openai, keyring)
nor the chat (tests/assistant/test_import_hygiene.py).
- Neutral model (
llm_adapter.py):Message,ToolCall,Request,Reply,Usage, the events a front end shows (TextDelta,ToolDone,Waiting,RetryWait, …) and theBackendprotocol. Data only one provider understands (Anthropic thinking blocks, Gemini thought signatures, OpenRouter reasoning details) travels inprovider_extra, stamped with its origin (preset, base URL, model), and is replayed only to that origin; on a switch it is dropped and tool-call ids are re-mapped. Ollama's reasoning string (the preset'sreasoning_wire_key,delta.reasoningjoined) is kept asprovider_extra["reasoning_text"]: never shown, saved with the conversation, and sent back asreasoningonly on the assistant messages after the last user message (gpt-oss's template renders it only there; without it the model leaks its analysis into tool calls). It does not make a turn's history uneditable, and like all message data a compaction drops it — also one in the middle of a turn, after which this turn's earlier reasoning is not sent again. - Backends (
backends/): Anthropic Messages and OpenAI Chat Completions (also Mistral, OpenRouter, Gemini, Ollama, LM Studio and any compatible endpoint). The provider libraries are imported only inmake_backend.net.pybuilds each library's HTTP client: headers rebuilt from an allow-list (the libraries' identification headers and the variables they read add nothing), our own proxy decision (a local endpoint is never proxied), no redirects, cancel by shutting the open sockets. Presets (providers.py) name no models; they say which request fields a service accepts and which privacy fields are sent (store: false, OpenRouterprovider.data_collection). - Errors and retries (
llm_errors.py,llm_retry.py): every library error becomes oneLLMErrorsubclass with a plain-English message; our own retry loop (no library retries), never after the first streamed output. - Probes (
probes.py): before the first question, the model's context and tool support (Ollama/api/show, load,/api/ps; LM Studio; the OpenRouter model list; Anthropic limits), standard library only and proxy-less for local servers. A model Ollama forwards to another host (its cloud) gives a remote route. - Session (
session.py): the agent loop — rounds of tool calls per question capped by level, tool calls under the process-wide tool lock, the history kept valid for every provider after a cut-off, refusal, stop or error; compaction points (everycompaction_everyquestions, a context overflow, a rejected signature, a resume with changed instructions) drop all provider data.TurnWorkerruns turns in a daemon thread; a stop returns at once. - Prompt (
prompt.py): five byte-stable layers (core rules, orientation, documentation map, level text, front-end note) and the context block (project, input source, scenario) at the start of a user message; the agent connection's instructions reuse the same rules. - Levels (
tiers.py, standard library only):resolve_tierplaces a model fromTIER_RULES(data, first match wins: small-model words, the reported or named size, family words, GPT major ≥ 5, a fallback guess by route) and caps it by the context window (WINDOW_LEVELS; belowMIN_WINDOWit raisesWindowTooSmall). The automatic level is the lower of the two;allowed_levelsis it and below, or up to the window's level for an unrecognised model (ALLOW_HIGHER_WHEN_UNRECOGNISED). There is no bypass in shipped code: a stored or--tierlevel above the allowed ones is lowered with a notice (-qwith--tierexits 2). Before a probe the front ends useprobes.cached_facts(user cache); the settings dialog never probes. The level changes only the prompt (prompt.ADDENDA), the tool list (registry.tools_for_tier; basic = thesmall_tiertools, any other name refused by the session before the registry) and the budget — never what a tool returns. Agents choose a level with the connection prompts or--mcp --tier(not checked against their model). - Budgets (
budget.py, standard library only):LevelBudgetper level (LEVEL_BUDGETS, the free plan'sFREE_TIER_BUDGETS): rounds, tool calls per question and per round, the output limit (≤ window / 4 for a known window), planned calls and the prefix budget the shipped layers and schemas must fit (tests/assistant/test_prompt_budget.py).resolve_budgetadds the window's class (WINDOW_CLASSES: result-cap ceiling,fit_capsfor windows under 32 768, and the compaction rhythm; settingscompaction_every/keep_recent_turnsnull= automatic).approx_tokensis a digit-aware estimate (JSON_CHARS_PER_TOKEN, pinned bytests/assistant/llm/test_token_estimate.pyagainst real tokenizer counts) corrected per session by the reported prompt tokens (TokenCorrection); the session's pre-send fit compacts or, for origins with replay data, asks for an answer without tools, and sends nothing when the estimate plus the answer limit stays above 95 % of the window (a reported one, or the assumed local one). The free plan (free_tier_on: the setting, else on only for an OpenRouter:freeid) lowers rounds and calls, caps results at medium and, in the FlexTool window, gives the chat's gated backend a limit of one request at a time; the first rate-limit reply on a route with it off shows the hint once (settings.free_tier_suggested). - Local files (
_files.py,settings.py,store.py,usage.py,keys.py,endpoint.py), inplatformdirs.user_config_dir("flextool")/assistant/: settings, conversations per project (lock file per open conversation, purge by age at every start), the usage ledger, the key file (opt-in; the keyring first, the preset's environment variable before both) andacknowledgements.json. Every read-modify-write runs under a lock file; every time is UTC. - Data notice (
endpoint.py): the route (loopback, local network, remote; the proxy; the forwarded host), the indicator line, and the notice a remote route needs before the first question. An acceptance is kept per preset, normalised address, proxy host, forwarded host and notice wording. - Terminal (
chat_cli.py): the REPL (a terminal only) and-q(only the answer on stdout; exit codes 0 / 1 / 2 / 3 / 130). The question and the probe run in daemon threads, so Ctrl-C stops them at once. The banner gives the level and why (level_lines);/contextthe window, the result cap and the compaction rhythm of the session's budget;--free-tier on|off|autooverridessettings.free_tierfor the session.
AI assistant in the FlexTool window
flextool/gui/assistant/ is the window's front end on the same session, store, settings, keys, notices and ledger
(user page: The AI assistant, "Chat in the FlexTool window"). Everything without Tk — the
worker, the provider gate, the Markdown parser, the window geometry, the strings — is tested in the sharded job
(tests/assistant/gui_support/); the Tk modules in the gui-tests job (Testing, "GUI tests").
- Entry and lifecycle (
entry.py,AssistantEntry, created byMainWindow): "AI assistant…" + ⚙ in side-menu row 5. Building it only runsimportlib.util.find_spec; the chat window, the dialogs, the SDKs, the keyring and the store are imported on the first click. It closes the chat window on a project switch, rename or deletion (one window per project) and waits for its workers (5 s; the Project dialog and the project combo refuse a rename or deletion while one still runs a tool call,before_project_change);project_utils.rename_projectmoves the saved chats (store.rename_project),notify_assistant_project_deleteddeletes them (store.forget_project). At FlexTool close (MainWindow._on_close, before the settings save) it withdraws the windows, shuts the workers down, deletes the runtime file and purges, within 3 s; at start (after 3 s) it purges and removes runtime files of gone processes. - Threads: one
AssistantWorkerdaemon thread per chat tab owns itsSession, conversation and lock; the Tk thread never calls the network, keyring, probes, store or ledger (except reading the settings and the welcome flag when a window opens). Worker → Tk only through queues drained by oneafter(50)poll. The process-wideProviderGate(gate.py) letsparallel_requests(1–4) model requests per provider run at once (a free-plan chat's ownlimitis 1), gives slots in arrival order and shares a rate-limit cool-down between chats; tool calls run outside it, under the process-wide tool lock. A shut-down worker saves a checkpoint in which a running tool call is "Not run: FlexTool was closed…", so the file is always a resumable history. A project rename or deletion moves or deletes its chats on one FIFO daemon thread (project_utils); every store user waits for the queued hooks first (wait_project_hooks). The Tk thread's blocking waits (project change, close) pumpdooneevent(FILE_EVENTS | DONT_WAIT), so a worker's Tcl call (a finalizer collected on its thread) finishes; closed assistant dialogs retire their Tk variables on the Tk thread. - Window (
chat_window.py,chat_tab.py,transcript.py,notices.py,settings_dialog.py): chats asttk.Notebooktabs with an overflow budget (the Chats menu shows the hidden ones), restored when the window opens again (store.open_chats); one data notice at a time; the size per monitor configuration inGlobalSettings.assistant_window_geometry; a key shield on every assistant Toplevel so the main window'sbind_allshortcuts never fire from it. The settings' level rows list "Automatic" and onlyTierChoice.allowed(from cached facts; ttk cannot grey single entries) withTierChoice.lines(), and the free plan as a three-entry list (automatic / on / off →free_tiernull / true / false); the worker's setup fingerprint includesfree_tier, so a saved change applies from the next question. - What the window shows (
gui_state.py):snapshot_from_mainreads the main window's checks and highlights andResultViewer.view_state()on the Tk thread (each part on its own, about 2 ms); every Send takes its own snapshot (ContextSnapshot.gui), which the prompt renders as the context block. While a project is open,ContextPublisherpolls it every second and hands a changed payload to theRuntimeContextWriter(flextool/assistant/runtime_context.py): one file per process,<config>/assistant/runtime/<project key>.<pid>.json, names and selections only, rewritten every 30 s while the poll ticks (a hung Tk thread's file goes stale), deleted by the writer thread at close. An agent'sget_gui_context(--mcp,--call) reads the file of the window in use (read_runtime_context: process alive, written within 120 s; the latest focusactive_at, thenchanged_at, thenwritten_at); a live process with only a stale file reads as open but not answering (responding: false).
Tests: tests/assistant/llm/ — no network (a socket guard refuses anything but loopback), synthetic provider
payloads through httpx2.MockTransport, a scripted FakeBackend for the session and the chat, loopback servers
for the probes and the cancel. Real provider replies are recorded by hand with tools/record_llm_replies.py on
the synthetic test project only, scrubbed, checked and replayed in CI (Testing, "Recorded provider
replies").