Serving the REPL#

The REPL page (doc/_static/repl) runs in a browser. NeuroLang answers it in one of two places:

  • in the browser, on Pyodide, from a bundle of NeuroLang’s wheels and an engine’s data: the page of the documentation, or neurolang-repl-serve without --local;

  • on a server, in its Python, with neurolang-repl-serve --local: the engines with all their analyses, as fast as the machine, and, with a users file, a session of its own for each tab of each user, the AI assistant’s keys kept on the server, an admin console and the ratings stored with the user who rates.

This page is about the second: how to set up such a server for a group of people. Contributing to NeuroLang tells how to run the page of a checkout for development.

Quick start#

On a Linux server, from a checkout of NeuroLang, with uv:

git clone https://github.com/NeuroLang/NeuroLang && cd NeuroLang
uv sync --extra apps --extra tui --extra llm

# the first user, an admin (asks their password; makes users.yaml)
uv run --extra apps neurolang-repl-serve --users users.yaml --add-user ann --groups admin

# its options in server.yaml, its secrets in secrets.yaml (chmod 600):
# see "Configuration files" below
cat > server.yaml <<EOF
local: true
host: 0.0.0.0
port: 8443
cert: cert.pem
key: key.pem
users: users.yaml
feedback_db: feedback.sqlite
activity_db: activity.sqlite
data_dir: /srv/neurolang_data
secrets: secrets.yaml
EOF

# serve it, as a service of this user, over HTTPS
uv run --extra apps --extra tui --extra llm neurolang-repl-serve \
    --daemon install --config server.yaml

loginctl enable-linger $USER   # keep it running after you log out

Then open https://SERVER:8443/, log in as ann, and open the admin console at https://SERVER:8443/admin to add the other users. The server answers at once and prepares each engine meanwhile, those the page offers first: it downloads its data into --data-dir and finds the tables it takes long to find, kept there, which takes a few minutes the first time (up to half an hour for the six engines of the Inria server after an update that changes the tables) and is quicker after. A session of an engine not yet prepared waits for it, its page telling that the server is preparing the engine; the log tells The engine NAME is ready. An engine it cannot prepare (a source down) is told in the log, and its first session tries again. A session then starts from the data kept, without the network: the first one of an engine takes the time of a build (about a minute for Neurosynth with --light), the next ones under a second.

Requirements#

  • Python 3.12 or later, and the workspace’s extras apps, tui (the REPL) and llm (the AI assistant): uv sync --extra apps --extra tui --extra llm. Use the same extras with every uv sync in that checkout: one without them removes their packages from its environment, which the service runs, and the service then fails to start (uv run without them leaves the packages there: it installs what it lacks, and removes nothing).

  • Memory for the engines: several GB for Neurosynth with all its analyses (lift, relative_risk…; --light leaves them out, as the page in the browser does). The sessions of the users share the memory of the engine’s data (see Sessions).

  • On Linux, forking the sessions (--users); macOS works too.

  • Access to the internet from the server, for the first download of the data and for the AI providers.

The command#

neurolang-repl-serve is the command of the workspace’s root project (tools/serve_repl); python -m serve_repl is the same. Its options, by what they do:

Serving#

--local

NeuroLang runs in this server’s Python; the page’s worker sends what it is asked to the server (/api/...). Without it, the server serves the page and a bundle for the browser (see Contributing to NeuroLang).

--data-dir DIR

The engines’ data directory (default neurolang_data, relative to where the command runs; give an absolute path for a service).

--no-prepare

Do not prepare the engines; the first session of each downloads what it lacks. Without it, the server with --users serves while it prepares them; one without --users prepares them before serving.

--no-preload-embeddings

Do not load an engine’s text embeddings when it is built (with --users, before forking its sessions): its first query of them does; see Sessions.

--no-prebuild

With --users, do not build the engines the page offers when the server starts: the first session of each builds it, and waits for it; see Sessions.

--prebuild-reserve FRACTION

With --users, build an engine ahead only while the share FRACTION of the memory stays for the queries of the sessions (default 0.35); see Sessions.

--resolution MM

The resolution of the engines’ MNI grid (default 2).

--light

Leave out the analyses a browser cannot hold, as the page does.

--engine NAME

The engines the page offers (repeat it; default: all of them).

--default-engine NAME

With --local, the engine the page starts with, one of --engine (default: the bundle’s, neurosynth_iit, if offered). The admin console may turn engines off and choose another (see The admin console).

--host HOST, --port PORT

Where it listens: 127.0.0.1 by default, this machine only; 0.0.0.0 for the network. A port below 1024 needs root, which the service does not have: use a port above, or a reverse proxy.

--server-name NAME

Without --users nor --password-file, the server answers only the requests sent to (whose Host is) localhost, 127.0.0.1, ::1, --host (unless 0.0.0.0 or ::) and the names of --server-name (repeat it): another site whose name it made lead here (DNS rebinding) would otherwise use it. Give the name, or the address, by which the other machines reach it. With users or a password, any name is answered.

--site DIR

Where the page is put (default ~/.cache/neurolang/repl-site).

--no-browser

Do not open the page in a browser (the service never does).

HTTPS#

--cert CERT.pem, --key KEY.pem

Serve HTTPS with this certificate (its chain after it) and its private key, which may be in the certificate’s file. Without a certificate, a password is refused unless the server listens on this machine only, as it would go in clear.

--key-password-file FILE

The passphrase of the key, if it has one, on the first line of FILE (readable by you only); without it, the passphrase is asked in the terminal, which a service has not.

If they cannot serve, the server says why: the certificate’s file has no key (give --key), the key is not that of the certificate, the passphrase is not that one, a file is missing, or they are not PEM. Without users nor a password, give the name of the certificate with --server-name too (see Serving). To check them:

head -1 cert.pem    # -----BEGIN CERTIFICATE-----
head -1 key.pem     # -----BEGIN PRIVATE KEY----- (or RSA / EC PRIVATE KEY);
                    # -----BEGIN ENCRYPTED PRIVATE KEY-----: it has a passphrase
openssl x509 -in cert.pem -noout -subject -enddate
openssl pkey -in key.pem -noout && echo "the key reads"
# the key is the certificate's if these two are the same:
openssl x509 -in cert.pem -noout -pubkey | openssl sha256
openssl pkey -in key.pem -pubout | openssl sha256

A binary (DER) file converts to PEM with openssl x509 -inform DER -in cert.der -out cert.pem (openssl pkey -inform DER for the key); a .p12 or .pfx bundle with openssl pkcs12 -in bundle.p12 -clcerts -nokeys -out cert.pem and openssl pkcs12 -in bundle.p12 -nocerts -out key.pem.

A certificate of Let’s Encrypt (certbot certonly --standalone -d SERVER) works: --cert /etc/letsencrypt/live/SERVER/fullchain.pem --key /etc/letsencrypt/live/SERVER/privkey.pem, readable by the user of the service. The server reads them when it starts: restart it after a renewal (--daemon reinstall, or systemctl --user restart neurolang-repl).

Who may use it#

--users users.yaml

The users, each with a name and a password, their groups, and their assistants (see Users and groups); with --local, each tab of each user has a session of its own.

--password-file FILE

Instead of users, one password (the first line of FILE), with any user name; the visitors share one session.

--max-sessions N, --idle-minutes M

With --users and --local: the sessions at most at once (8, at least 1), and the minutes after which an unused one ends (120, above 0).

--max-running N, --query-minutes M

With --users and --local: the queries running at once, of all the sessions (the machine’s CPUs, at least 1), and the minutes after which a query is stopped (60; 0, never); see Sessions.

--session-memory-mb MB

With --users and --local, on Linux: a query taking its session’s process more than MB megabytes of memory of its own (0, never) is stopped; see Sessions.

--uploads-dir DIR, --max-upload-mb MB, --max-uploads-mb MB

The files the pages upload (Upload data: tables, images): with --users and --local, a session keeps them in a folder of its own in DIR (~/.cache/neurolang/repl-uploads), removed when it ends, stopped or unused for --idle-minutes; the server empties DIR when it starts, and removes the folders of the sessions whose process died. A file takes at most MB megabytes (50), all those of a session together at most MB (200).

--mcp, --mcp-results-dir DIR

With --users and --local: answer MCP clients at /mcp, each user with the token of their account page, the files of the answers in DIR (~/.cache/neurolang/repl-mcp); see MCP clients.

--guests, --max-guest-sessions N

With --users and --local: let visitors in as guests, without an account, at most N sessions of guests at once (2); see Guests.

--secure-cookie

With --users behind a reverse proxy of HTTPS (no --cert): the login cookie goes over HTTPS only, as it does with --cert.

--orcid orcid.yaml

With --users: the users named by an ORCID iD sign in with ORCID, and an iD of no user may ask for an account (see Signing in with ORCID).

Configuration#

--config server.yaml, --secrets secrets.yaml

The options, and the secrets, in YAML files (see Configuration files).

Without either, anyone who reaches the server uses it.

The AI assistant#

--env-file llm.env

NAME=VALUE lines put in the server’s environment: the key of the provider of the server’s AI configuration (see The AI assistant’s keys), the default of those who log in. It needs --local, and --users or --password-file.

Storing the ratings#

--feedback-db feedback.sqlite

With --local, store the ratings of the page on the server (see Feedback).

--activity-db activity.sqlite

With --local and --users, log what each user runs, its result and the resources it took (see Activity and usage), which the admins read at /admin/activity (see The activity page).

--dashboard

With --activity-db, Datasette on the log and the ratings, for the admins, at /admin/logs/: every table and any read-only SQL (see Raw data and SQL).

Running as a service#

--daemon install|reinstall|remove|status, --python PATH

Install the server as a service of this user, or tell how it runs (see The service and Uptime).

Managing the users#

--list-users, --add-user, --set-password, --set-groups, --set-llm, --set-group-llm, --own-key, --claude-code, --set-key, --set-group-key, --remove-user and --hash-password change the users file from the command line (see Managing users).

Configuration files#

The server’s options go in a YAML file, --config server.yaml, each by its name (data_dir, or data-dir as the option is written), a list for an option given several times (engines: [neurosynth, neurostore]), true for a flag; a relative path is relative to the file. An option given on the command line replaces the file’s. The service installed with --config runs that file as it is, so that a change of it is the service’s at its next start (systemctl --user restart neurolang-repl). The users, their groups and their assistants stay in their own file (users), which the console writes. The options of the sessions, their log and the dashboard go there too:

max_sessions: 8
idle_minutes: 120
max_running: 4              # queries at once (the machine's CPUs)
query_minutes: 60           # 0: no limit
session_memory_mb: 4096     # 0: no limit (Linux)
uploads_dir: /srv/neurolang/uploads  # a folder per session, removed when it ends
max_upload_mb: 50
max_uploads_mb: 200
activity_db: activity.sqlite
dashboard: true             # needs the dashboard extra
mcp: true                   # MCP clients at /mcp, with the users' tokens
engines: [neurosynth_iit, neurostore_iit]  # prepared and offered
default_engine: neurostore_iit             # the one the page starts with

Its secrets go in a second YAML file, readable by you only (the server refuses it otherwise: chmod 600 secrets.yaml), which server.yaml names (secrets: secrets.yaml), or --secrets FILE:

orcid:                      # the client of ORCID (see Signing in with ORCID)
  client_id: APP-XXXXXXXXXXXXXXXX
  client_secret: ...
env:                        # the server's variables (--env-file)
  ALBERT_API_KEY: ...
password: ...               # the one password of all (--password-file)
key_password: ...           # the passphrase of the key of cert (--key-password-file)

so that server.yaml holds no secret and may be shared or versioned. Each secret may still be given in a file of its own, as before (--orcid, --env-file, --password-file, --key-password-file), but not both ways at once.

Users and groups#

The users file is YAML, readable by its owner only (chmod 600; the server and the console keep it so):

users:
  ann:
    password: scrypt$16384$8$1$...    # made by --add-user, --set-password
    groups: [admin, lab]
  bob:
    password: scrypt$16384$8$1$...
    groups: [lab]
    llm:                              # bob's own assistant
      provider: anthropic
      model: claude-sonnet-5-5
      key: sk-ant-...
  eve:
    password: scrypt$16384$8$1$...
    groups: [guest]
    own_key: true                     # eve may give her own key
groups:
  lab:
    llm:                              # the assistant of the lab's users
      provider: openrouter
      model: anthropic/claude-sonnet-5.5
      key: sk-or-...
    own_key: false                    # the lab's users may not give theirs
  guest: {}

A user has:

password

The scrypt hash of their password (never the password).

groups

Labels. Those of admin have the admin console; the others are for the assistants (below) and are recorded with each rating.

llm

Their own assistant: a provider of the page’s form (anthropic, openai, openrouter, or custom, an OpenAI-compatible API at base_url, which it needs), its model (the provider’s by default) and its key. A change that gives no key keeps the one before, for the same provider and base_url only.

own_key

Whether they may give their own key, in the page and on their account page.

env

Variables of their sessions: a key for the server’s AI configuration (see The AI assistant’s keys).

A group has llm, own_key and env too, which its users have.

The server reads the file again when it changes: an edit by hand, or by the command line, reaches it without a restart, and the open pages of a user take a new assistant at their next start (the console’s and the account page’s changes reach them at once). The console and the command line write the file anew (its comments are lost), holding a lock of it (users.yaml.lock), so that their changes all stay; they refuse to write over a file edited by hand that the server cannot read (it says why), and keep a user in it. A file of the users alone, without users: and groups:, as an older server wrote it, is read as well.

The AI assistant’s keys#

The assistant of a session answers with, in order:

  1. the key the user gives in the page (the API key button), if they may give their own;

  2. their own llm;

  3. the llm of their groups: the first, by name, that has one;

  4. the server’s AI configuration, ~/.config/neurolang/llm.yaml (see LLM Query Assistant), with the key in the variable its provider names (api_key_env, e.g. ALBERT_API_KEY), taken from the user’s env, else their groups’, else the server’s --env-file;

  5. none: the page asks the user for a key.

A user may give their own key (1, and saving their llm on their account page) as their own_key says, else not if one of their groups says own_key: false, else they may. When they may not, the page hides its API key button and the server refuses a key from it.

The keys stay on the server: the page, the console and the account page never show one; a session has its user’s key in the environment of its own process only. An admin who wants to know which key a user has looks at the console’s AI assistant column: theirs, their group’s (which group), the server’s, or none.

Claude Code as the assistant#

The claude-code provider (see LLM Query Assistant) has Claude Code run the assistant’s agent, with the sign-in to claude of a person’s Anthropic account, no API key: for someone whose account has none. The server runs it on the engine of the session, in the server’s process; the sign-in is that of the user running the server, so what it needs, and who may use it, depends on how the server runs.

On your own machine (--local, no --users): the one who runs the server is the one who asks.

  1. Install Claude Code and sign in once, in a terminal: claude.

  2. Install the extra: pip install "neurolang[claude]" (in a checkout, uv run --extra apps --extra claude).

  3. In ~/.config/neurolang/llm.yaml, a provider of api: claude-code as the default (the example file has it as claude-agent):

    default_provider: claude-agent
    providers:
      claude-agent:
        api: claude-code
        model: sonnet
    
  4. Serve, and ask in the page as usual:

    neurolang-repl-serve --local
    

    The page is that of the REPL’s .ask: the same configuration, the same provider. To start with another provider, change default_provider; a user’s own key in the page, or their llm, still comes first (see The AI assistant’s keys).

On a shared server (--users): the provider is refused to everyone, since the sign-in would be the operator’s, not the user’s. An admin allows it, to a user or a group, knowing that those sessions then run on the sign-in of the account of whoever runs the server, and on the terms of Anthropic’s service for it:

neurolang-repl-serve --users users.yaml --claude-code group:lab yes
neurolang-repl-serve --users users.yaml --claude-code bob no        # or inherit

or in the admin console: the Claude Code menu of the user’s and of the group’s form, and the column showing the result. A user’s own setting wins over their groups’; one allowing group is enough; guests never have it. Then the server’s configuration has the provider as above and the users allowed have it as their assistant (step 4 of The AI assistant’s keys); the others have their key, or are asked for one.

Where it runs as a service (see Running as a service), the sign-in is that of the service’s user: sign in as that user (sudo -u SERVICE_USER claude), and make sure claude is on the service’s PATH. The server checks the sign-in before each question and answers with an error that says what to do if there is none, or if it is an API key (use the anthropic provider for one). It runs without the key variables ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN, which the CLI would use before the sign-in. Only POSIX systems are supported.

The admin console#

/admin is for the users of the group admin; anyone else gets a refusal (403). It shows:

  • Users: their groups, their assistant (whose key: their own, their group’s, the server’s or none; its provider and model), their variables’ names, whether they may give their own key. It adds a user, changes their password, groups or own key, sets or removes their assistant, and removes them (ending their sessions). The last admin cannot be removed.

  • Groups: their members, assistant, variables and own key; it sets or removes a group’s assistant and own key.

  • Engines: with --local, the engines the server prepared (--engine), whether its page offers each, and the one it starts with (--default-engine). A change is the page’s when it is opened (or reloaded); the sessions running keep their engine, and the server starts none on an engine turned off. One engine at least stays offered, the default among them. The choice is kept next to the users file (users.yaml.engines.json), and holds over the configuration’s at the next start; an engine it does not name (added to engines since) is offered. Remove the file to go back to the configuration’s.

  • Sessions: the user, tab and engine of each, how long it has been unused, whether it works, the CPU seconds and the memory of its process (see Activity and usage); it ends one. Above them, the memory of the builders, which the sessions share.

  • Uploads: with --uploads-dir, the files the pages uploaded, by session (its user and tab, or session ended for a folder whose session is gone), with their size and age, the space they take and that free on the disk, and the limits; it erases a file, the files of a session or all of them (the server’s log tells who erased what). A session whose file is erased no longer has it: .load says so, and the page uploads it again after a Stop.

  • Usage by user: with --activity-db, each user’s inputs run and failed, their time and CPU seconds, the most memory of their sessions, and a link to their log.

  • Feedback: the latest 200 rated answers, with the user and their groups, and a link for each engine exporting the rated answers of its assistant as its examples, in YAML.

A change applies at once to the file and to the open pages of the users it changes. A password has 8 characters at least, here, on the account page and from the command line.

Logging in#

With --users, the server has a login page, /login: a page opened without a login leads there, and back once logged in (to a page of the server only). A login is a cookie signed by the server (HttpOnly, SameSite=Lax, Secure over HTTPS or with --secure-cookie), good for 7 days; it ends when the user logs out (Log out, at the top of the REPL with --local, /logout), in every browser where they logged in before (every copy of the cookie), when their password changes (the browser that changed it stays logged in), when they are removed, and when the server restarts (it signs with a key made at its start). Logging out, and opening the login page, forget the API keys the REPL kept in the browser. A page whose login expired opens the login page. Scripts may still give a user’s name and password with HTTP Basic (curl -u ann:...). With --password-file, the browser asks the password itself, and cannot log out.

Signing in with ORCID#

With --orcid orcid.yaml, the login page offers Sign in with ORCID. The server sends the visitor to ORCID’s page, where they sign in, and ORCID sends them back with a code, which the server exchanges for their ORCID iD and their name (OAuth 2.0 with the scope /authenticate: who they are, nothing else of their record).

  • A user whose name is that iD logs in, as with a password. Such a user needs no password (password is left out of their entry), and their name at ORCID is kept as their orcid_name, which the REPL, the console and their account page show:

    users:
      0000-0002-1825-0097:
        groups: [lab]
        orcid_name: Josiah Carberry
    
  • An iD of no user may ask for an account, with a message for the admins. The requests wait in users.yaml.requests.json, next to the users file; the console lists them under Account requests, where an admin approves one, adding the user named by the iD to the groups given, or declines it. Until then, the iD has no access.

The client is ORCID’s: register one at ORCID’s developer tools (its Public API is enough), with the redirect URI https://SERVER:PORT/login/orcid/callback, and give its id and secret as the orcid of the secrets (see Configuration files), or in a YAML file of its own, --orcid orcid.yaml (readable by you only):

client_id: APP-XXXXXXXXXXXXXXXX
client_secret: ...
# when the server is reached at another address than its requests name
# (behind a proxy), the redirect URI registered:
redirect_uri: https://repl.example.org/login/orcid/callback
# a client of ORCID's sandbox, for testing:
sandbox: false

From the command line:

neurolang-repl-serve --users users.yaml --add-user 0000-0002-1825-0097 --groups lab  # no password
neurolang-repl-serve --users users.yaml --list-requests
neurolang-repl-serve --users users.yaml --approve-request 0000-0002-1694-233X --groups lab
neurolang-repl-serve --users users.yaml --decline-request 0000-0002-1694-233X

In the console, Add with an iD as the user’s name and no password adds a user who signs in with ORCID.

Merging and separating. A user of a name of their own may sign in with an ORCID iD too: merging the ORCID user named by the iD into them (the console’s merge into…, or --merge-orcid ID USER) gives them orcid: ID, the groups of both, and the ORCID user’s assistant, variables and own key where they have none; the ORCID user is no more, and their sessions end. They then log in with their password, or with ORCID:

users:
  bob:
    password: scrypt$16384$8$1$...
    groups: [imaging, lab]
    orcid: 0000-0002-1694-233X
    orcid_name: Sofia Garcia

Users do it themselves on their account page too:

  • a user with a password links their iD (Link my ORCID iD): they sign in at ORCID, which proves the iD theirs, and log in with either then; an iD with an account of its own is merged into theirs, as above. They unlink it there (Unlink it), and log in with their password only.

  • a user who signs in with ORCID only merges their account into one of theirs with a password (Merge into it, giving its name and password): they are logged in as it then.

An iD that signs in another user is not linked: an admin decides.

Separating it (Separate ORCID, or --separate-orcid USER) makes the iD a user of its own again, named by it, in the user’s groups but admin, which an admin gives again if it is theirs. An iD signs in one user only: the server refuses a users file where it names a user and is another’s orcid.

Guests#

With --guests, the login page offers Continue as a guest: a visitor comes in without an account, as a guest of a name of its own (guest: and a random id, shown as guest), with sessions of their own, in the group guest. A guest’s AI assistant answers only with a key they give in the page: their sessions have no key of the server, of a group or of a user (the server’s keys are not even in their processes’ environment). A guest has no account page, no console, and their ratings are signed by their guest’s name and the group guest. The guests have at most --max-guest-sessions sessions at once (2), so that they cannot take those of the users. A guest’s login ends when they log out, after 7 days, and when the server restarts; a guest who logs in again is another guest, with sessions of their own.

MCP clients#

With --mcp, the server answers MCP clients, LLM agents such as Claude Code, at /mcp: the client’s model answers questions on the engines as the query assistant does, with its tools, in a session of the user’s own (see From an MCP client in LLM Query Assistant, the same server over the network). The login page says so, and each user creates their token on their account page, which also gives the command that adds the server to Claude Code:

claude mcp add --scope user --transport http neurolang \
    https://repl.example.org/mcp --header "Authorization: Bearer nlmcp_..."
  • The token is the user’s, shown once; only its hash is kept, in the users file (mcp_token), so it outlives the server’s restarts. A new one replaces it, and Revoke it removes it. /mcp takes no cookie, only the token; neither goes to the session.

  • The session is a session of the user’s like a tab’s (it counts in --max-sessions and ends unused after --idle-minutes), on the default engine, or another of the page’s with /mcp?engine=NAME; its REPL answers in plain text. A session that ended starts again at the next request, and its first ask gives the whole prompt again.

  • The files of the answers (the viewer, the images, the rows, the query and its graph) are kept in --mcp-results-dir, in a folder per user (the list_answers of an MCP client lists theirs only), a folder per session with a random name, removed after 7 days, and linked as https://HOST/mcp/files/...: they open for the users logged in. The pages (the viewer, the graph) are sandboxed, away from the site’s cookies; the other files are downloaded.

  • Limits: a session’s statements are confined as a tab’s, and its queries run within the assistant’s time and memory limits (llm.yaml) and the server’s --query-minutes. The answers are JSON, without event streams: a client’s cancel does not stop a query that runs.

Accounts#

With --local, the REPL shows the name of the user logged in at its top, linked to their account page, /account: their name and groups, whose assistant they have, a form that saves their own (provider, model, its address for custom, key; the key kept on the server and never shown again; it answers in their open pages at once), if they may give their own, and a form that changes their password (the current one first; the new one of 8 characters at least). A forgotten password is reset by an admin, in the console or with --set-password.

Managing users#

The same changes as the console, from the command line, on the file of --users; each asks a password or a key without echoing it (prefix them with uv run --extra apps in a checkout):

neurolang-repl-serve --users users.yaml --add-user ann --groups admin,lab   # makes the file
neurolang-repl-serve --users users.yaml --set-password ann
neurolang-repl-serve --users users.yaml --set-groups bob --groups lab
neurolang-repl-serve --users users.yaml --set-llm bob anthropic claude-sonnet-5-5   # empty key: removes it
neurolang-repl-serve --users users.yaml --set-group-llm lab openrouter anthropic/claude-sonnet-5.5
neurolang-repl-serve --users users.yaml --own-key group:lab no       # or NAME yes|no|inherit
neurolang-repl-serve --users users.yaml --claude-code group:lab yes  # or NAME yes|no|inherit
neurolang-repl-serve --users users.yaml --set-key bob ALBERT_API_KEY # a variable of env
neurolang-repl-serve --users users.yaml --set-group-key lab ALBERT_API_KEY
neurolang-repl-serve --users users.yaml --remove-user bob
neurolang-repl-serve --users users.yaml --list-users                 # no secrets

--claude-code (and the Claude Code column and menus of the admin console) decides who may use the claude-code provider of the query assistant, which runs on the sign-in to claude of whoever runs the server, not of the user. It is off for everyone, and for guests always, unless an admin allows it to a user, or to a group (a user’s own setting wins over their groups’; of several groups, one allowing is enough). It is an admin’s decision, on the terms of Anthropic’s service.

--hash-password prints the hash of a password, for a file written by hand.

Sessions#

With --users and --local, each tab of each user has a session of its own, in a process of its own (neurolang_apps.utils.tui.sessions):

  • Each engine has a builder process of its own, which builds the engine once, then forks a process per session from it, in under two seconds. The sessions share the memory of the engine’s data, as forked processes do until they write it. A coordinator process, started anew before the server answers, starts the builder of an engine, anew too, at its first request, and passes the requests on. One engine builds at a time, in the order asked (the builds’ memory does not add up); a session of an engine built starts at once, whatever builds meanwhile, rather than wait for that build (up to 90 s for neurostore_iit).

  • A builder that dies (out of memory, most often) ends its engine only: the starts it had not answered fail, which their pages tell, the log says The builder of the engine NAME ended, and the sessions it forked, and the other engines, go on. The next session of that engine has a new builder, which builds the engine again.

  • When the server starts, the builders build the engines the page offers (those the admin console did not turn off), the default first, and an engine the console turns on when it does: a first session finds its engine built (about 25 s for neurosynth_iit, 75 s for neurostore_iit, from the tables kept in --data-dir), unless it comes while its builder is still at work. The builder also finds the names of the engine’s relations (its terms, studies, regions), which a query’s notes on a misspelled name look up, so that no session finds them again at its first query (12 s for neurostore_iit). The engines then hold their memory from the start, as they would after their first session; an engine turned off stays built until the server restarts. --no-prebuild (prebuild: false) leaves each engine to its first session. An engine that cannot be built is told in the log, and its first session tries again.

  • The coordinator has an engine built ahead only while it leaves the share --prebuild-reserve (prebuild_reserve, 0.35) of the memory to the queries: the memory Linux says is available (MemAvailable, or the limit of the service’s control group), less one and a half times the most a build took (each builder tells it). An engine past it is told in the log (“Did not build the engine …”) and built by its first session. 35% of 31 GB is 11 GB: the 8 queries that run at once on 8 CPUs (max_running) decoding an image on neurosynth_iit take 1.4 GB each.

  • The engines of the builders share their tables through files, as each is a process of its own: the tables an engine derives from its own while it is built (activated, the voxels within reach of each peak) and its large source tables (the studies of the regions of the atlases, the TF-IDF of the terms, the peaks, the bootstrap replicates) are kept as files of DATA_DIR/derived_tables that the builders and the sessions map, rather than copy: the system holds one copy of a table that neurostore and neurostore_iit have in common (see Engine Configuration (engines.yaml)).

  • Before its first fork, the builder loads the engine’s text embeddings (those of its documents, from their cache, and the query model), which its sessions then share, rather than each load them, in memory of its own, at its first query_similarity (unless --no-preload-embeddings, or preload_embeddings: false; the single-user --local server loads them when it builds an engine, and preparing the engines writes their cache). The query model is the numpy one (specter2_numpy) when its weights are next to the model’s copies, {data_dir}/{engine}/embeddings/models/*.npz (for an engine extending

neurosynth or neurostore, those of the engine it extends): it loads in half a

second, without torch. Otherwise it is torch’s, on the CPU, which needs the embeddings extra, as computing the documents’ embeddings does when their cache is missing (hours on a CPU: python -m neurolang.utils.text_embeddings --engine NAME does it ahead of time, best on a GPU). What cannot be loaded is logged, and loaded by each session at its first query, as before.

  • After each query, once it has replied, a session frees what the query left (gc.collect, then, on Linux, malloc_trim): 13 ms, and a session that decoded an image on neurosynth_iit keeps 90 MB of its own rather than 0.8 GB.

  • Each session has its own rules, assistant, queries (which run at the same time as those of the others) and files; Stop and Restart end that session’s process only, and the page starts it again.

  • A session is that of a user and a tab: another user cannot reach it. A reload keeps the tab’s session; a duplicated tab has one of its own.

  • At most --max-sessions run at once: past them, the one unused the longest ends, unless it is at work or was used in the last minute (then the new tab is told to wait). One unused for --idle-minutes ends.

  • A session the server ended (unused, made room for another, its query ran too long) comes back at the next input of its page: the page starts it again, runs again the rules the session defined (as after Stop, which the page keeps; not its queries, nor the questions to the assistant), then that input. If it ends again while its rules come back, or at once after, the page says so rather than try again. A restart of the server ends the logins too: the page opens the login page, then starts anew, without the rules of the session. With --activity-db, the rules run again are logged again, as inputs.

  • At most --max-running queries (and downloads of results) run at once, of all the sessions: one more waits its turn, its page saying so, so that the queries of many users share the machine’s CPUs rather than slow each other down. A session that is not running a query costs no CPU, only its memory.

  • A query running longer than --query-minutes is stopped: its session’s process ends (which frees its CPU and memory at once, as no thread of Python can be stopped otherwise), and its page says so; its rules come back at the next input.

  • A query taking its session’s process more than --session-memory-mb of memory of its own (the private memory of /proc/PID/smaps_rollup, which leaves out the pages the session shares with the builder; read every half second, on Linux, nothing elsewhere) is interrupted as the Stop button does: the page says how much it took and the cap, and the session stays, its rules too, its memory given back. A query that allocates faster than the reading, in one native call, may still go over before it stops. Set it so that --max-sessions times the cap, plus the builder’s memory (the engines’ data), fits in the machine’s memory: otherwise the kernel’s OOM killer picks, perhaps the builder or the server. As a second line, in the service run by systemd, limit the whole unit (systemctl --user edit neurolang-repl):

    [Service]
    MemoryHigh=48G   # above it, the unit is slowed down and its memory reclaimed
    MemoryMax=56G    # above it, the kernel kills in the unit, not the machine's other services
    

    The user manager must have the memory controller delegated (systemctl --user show -p Delegate; on many distributions only system units may set these).

Without --users, one session serves everyone, in the server’s process.

Activity and usage#

The server takes, every 10 seconds, the CPU seconds and the memory of each session’s process and of the builders (with their coordinator, summed), which the console shows. A session’s memory is its own, private memory: what it adds to the machine’s beyond the engine’s data, which it shares with its engine’s builder (its resident memory, rss, counts those shared pages, and so overstates it). The builders’ resident memory is that of the engines’ data. This is read from /proc and so on Linux only; elsewhere the console shows -. Together they tell how many sessions a machine holds: about (its memory − the builders’) / a session’s own, at its most.

With --activity-db activity.sqlite, the server also logs, in that SQLite database (in WAL mode, as the feedback’s), for each user: the start of each session and its engine; each input they run, with its result as text (its output and the first 50 rows of each table, 20,000 characters at most), whether it failed, how long it took, for a question to the assistant (.ask) the tokens it sent and received, its provider and model and whose key paid (page: the key the user gave in the page; server: one of the server, of their group or saved in their account), the CPU seconds and the private memory of its process after it; and the end of each session, and why (stopped, restarted, idle, made room for another, ended by an admin, the server stopped), with the CPU seconds of its process in all and the most memory of the session, from the samples. The page tells its users that what they run is logged. A question to the assistant (.ask) is an input too, logged with its answer; the keys are never logged. The console shows the totals of each user and gives their log as JSON lines (/admin/activity.jsonl?user=NAME); SQLite reads it too:

sqlite3 activity.sqlite "SELECT created, kind, text, seconds, cpu, memory FROM activity WHERE user = 'bob'"

The activity page#

With --activity-db, the admins have /admin/activity (Activity, linked from the console): what the users ran and what it took, read at a glance.

  • Filters, one row above everything they scope: the period (24 hours, 7 days, 30 days, all), a user, an engine. The view is in the page’s address, so that it can be kept and shared; times are in the browser’s time zone.

  • Figures of the period: the uptime, the users and the sessions they started, the inputs and the share that failed, the CPU time, the LLM tokens and the share spent on the server’s keys, the most memory of one session.

  • Uptime: a strip of the runs of the server over the period (a gap, a time it was down; a tick, a run that ended without stopping), and the latest runs. A period starts when the log began, if later: nothing is known before, which is not a time the server was down.

  • Over time: the inputs, the CPU time, the LLM tokens and the active users, by hour over 2 days at most, else by day, each in a chart of its own on the same time axis, with its table.

  • Users: each user’s inputs, failures, time, CPU, tokens, most memory and last input, sortable; choosing one shows only theirs, and a link to their whole log.

  • Inputs: the latest, slowest, or those that took the most CPU or tokens, searched by their text, the failed ones only if asked; each opens on its result, its memory and the assistant’s model and key.

  • Why sessions ended.

The charts are drawn with Chart.js (MIT), which the server serves itself (tools/serve_repl/vendor/chartjs): the page needs nothing from another site. It follows the computer’s light or dark theme, or the one chosen at its foot.

Raw data and SQL#

With --dashboard (and --activity-db), the admins also explore the log at /admin/logs/ (Raw data and SQL, linked from the console): Datasette on the activity log and the ratings (--feedback-db), with its charts (datasette-vega). Install them with the dashboard extra:

uv run --extra apps --extra tui --extra llm --extra dashboard neurolang-repl-serve \
    --daemon reinstall ... --activity-db activity.sqlite --dashboard

Datasette browses, filters, sorts and searches every table (activity, runs, the ratings’ answers and feedback), exports any view as CSV or JSON, and runs any read-only SQL. Its queries, on the page of the activity database, each over the last days (empty: all):

  • What the users ran, by hour and by day: the inputs, failures, users, CPU seconds and the most memory;

  • What each user ran, What ran on each engine;

  • The tokens of the assistant, by user, model and key, to tell what the keys of the server spend;

  • The slowest inputs, The inputs that failed (with their result);

  • Why the sessions ended;

  • The uptime of the server (in percent, over 30 days by default) and The runs of the server (see Uptime).

Show charting options, above a result, draws it (a line of the inputs by hour, a bar of the CPU by user…); the queries by time and by user link to their charts.

Datasette runs in a process of its own, started and ended with the server, listening on a unix socket in a directory only the server’s user reads, not on a port; the server passes it the GET requests of the admins only (the dashboard is read only).

Uptime#

Whether the server is up, and since when:

  • GET /health, open without a login, for a monitor: 200 {"ok": true, "uptime_seconds": N}, or 503 if the sessions can no longer start (their builder ended); no answer if the server is down. Point an external monitor at it (Uptime Kuma, UptimeRobot, healthchecks.io…), or a cron job of another machine:

    */5 * * * * curl -fsS --max-time 20 https://SERVER:8443/health > /dev/null || echo "the REPL is down" | mail -s REPL you@example.org
    
  • neurolang-repl-serve --daemon status: what the service manager says, whether it runs, since when, how many times it restarted (systemd starts it again 5 seconds after it fails) and how its last run ended.

  • With --activity-db, each run of the server is logged in its table runs: its start, the last minute it was alive and its stop. A run without a stop ended otherwise (killed, crashed, the machine stopped). The dashboard’s The uptime of the server gives the share of the last days the runs cover, and The runs of the server lists them. A stop by the service manager (systemctl --user stop/restart) is a stop.

Feedback#

With --feedback-db feedback.sqlite, a rating given in the page (Did this query do what you wanted?) is stored on the server, with the query and its first rows or its error, and, for an answer of the assistant, the question and its steps, signed by the user’s name and groups. The database is that of neurolang-feedback-server (SQLite, in WAL mode, which sessions write at once): keep it on a local disk, not a network one. The console shows the latest ratings and exports, for each engine, the rated answers of the assistant as YAML, in the form of its examples (LLM Query Assistant): one per question, the query approved by most ratings. Without it, the page sends its ratings where its feedback.json says, if anywhere.

The service#

--daemon install installs the server as a service of the user who runs it, started now and at each login (or boot, with linger), serving with the other options given:

  • on Linux, a unit of systemd’s user manager, ~/.config/systemd/user/neurolang-repl.service; loginctl enable-linger $USER keeps it running when you are logged out. A process killed for lack of memory (a session’s query) does not stop the service (OOMPolicy=continue): its session ends, the others go on. If the builder itself is killed, the server stops, and systemd starts it again;

  • on macOS, a launchd agent, ~/Library/LaunchAgents/org.neurolang.repl.plist.

It runs the Python of the environment that installs it (that of uv run, the checkout’s .venv/bin/python; --python PATH for another), with that environment’s PATH, the paths of the options made absolute, --no-browser, and nothing built again. install first checks that this Python can import what it serves with (it says which extras to add), checks the certificate, the password file and the keys, and builds the bundle (without --local), here, where you see the errors; reinstall does all that before it removes the service installed, which a failure leaves as it was. It does not copy your shell’s other variables: an API key in your ~/.bashrc does not reach it; put it in --env-file or the users file.

neurolang-repl-serve --daemon install   OPTIONS   # refuses if one is installed
neurolang-repl-serve --daemon reinstall OPTIONS   # replaces it, with these options,
                                                  # once they are checked and built
neurolang-repl-serve --daemon remove              # stops and removes it

neurolang-repl-serve --daemon status              # runs? since when? restarts?
systemctl --user status neurolang-repl            # Linux
systemctl --user restart neurolang-repl
journalctl --user -u neurolang-repl -n 80         # its log
tail ~/Library/Logs/neurolang-repl.log            # macOS

Security#

  • With --local, anyone who logs in runs queries and commands in the server’s Python. With users or a password, their statements are confined (neurolang.confine): .load, .image and .load_tractogram load the files they upload (Upload data, kept in the session’s folder of --uploads-dir), and no other path; .load_csv, .load_atlas, .import, .load_tractogram (a file not uploaded) and .view read the files of the data directory only (--data-dir) and fetch no address; .save, .transcript and index_tracts write no file (the page downloads the results instead). The engines themselves are built unconfined (their datalog_init), and the modules they ship are still imported. --unconfined lifts it, for a server of one person who trusts its users with its account. Run the server as a user of its own all the same: the confinement guards the commands, not the Python of an engine.

  • Use HTTPS (--cert) whenever the server listens on the network: the password and the login cookie go over it.

  • The server takes a POST only from its own page (in JSON, from its origin), so that another site cannot make a logged-in browser change anything.

  • There is no limit on the attempts to log in: give long passwords, or put the server behind a reverse proxy that limits them.

  • Keep the users file, --env-file, the key and the feedback database readable by the service’s user only.

Behind a reverse proxy#

The server is the standard library’s HTTP server, threaded: enough for a group, and the queries run in the sessions’ processes. For rate limits, certificates renewed by the proxy, or a port below 1024, put a proxy in front, e.g. Caddy:

repl.example.org {
    reverse_proxy 127.0.0.1:8766 {
        flush_interval -1        # the events of the REPL stream
    }
}

with the server on --host 127.0.0.1 --port 8766 (no --cert: the proxy has it) and --secure-cookie, so that the login cookie goes over HTTPS only. The proxy must pass the Authorization, Cookie, Origin and Host headers unchanged (a POST whose Origin is not the Host is refused), and not buffer /api/events (a stream of server-sent events). Without --users nor --password-file, give the proxy’s name with --server-name repl.example.org: the server answers only the names it knows (see Serving).

Troubleshooting#

The service does not start

journalctl --user -u neurolang-repl -n 80. No module named 'rich' (or another): the environment lacks an extra; uv sync --extra apps --extra tui --extra llm and --daemon reinstall.

The page says The engine could not start

Open Technical details on the page; the full traceback is in the service’s log. The log tells, before The REPL is at, whether each engine was prepared; one that was not downloads its data into --data-dir at its first session: check that the server reaches the internet (and its sources, OSF and GitHub, are up) and has room.

The builder of the engines ended

The process that starts the engines’ builders died: no session starts until the server restarts. See the log, and give the server more memory or --light.

The builder of NAME ended

The process of that engine died (out of memory, most often): the next session of the engine builds it again; the other engines go on.

The assistant does not answer

The console’s AI assistant column says whose key a user has; none means that they give theirs in the page. A key saved on the account page or in the console answers at once in the user’s open pages, which say whose assistant answers under the question’s box.

Too many sessions

Raise --max-sessions, or lower --idle-minutes.

Queries wait for long

The console’s Sessions show which are at work: raise --max-running if the machine’s CPUs are not all busy, or lower --query-minutes.