## Summary Two things arrive together because the second is what made the first usable. **Run raven from a container.** A Dockerfile builds the page, the Python environment and the engine into one image; `cd docker && docker compose up` serves it. nginx serves the built page and proxies the RPC socket to the engine over the loopback they share -- the engine binds 127.0.0.1 only, so a proxy in a second container would have no address to dial. Compose publishes on 127.0.0.1, and the entrypoint prints a one-time sign-in link on boot. **A first run is no longer a dead end.** The gateway used to refuse to start until a provider was configured, while being the thing that serves the page where a provider gets configured. It now starts, says what is missing, and picks up a key saved in Settings on the next turn without a restart. The onboarding overlay no longer takes the page either: New chat, the model chip and Send send the reader to Settings > Models instead, and an empty model list is marked on the control that fills it. Also carried: the provider catalogue registry (capabilities, modalities and context windows per model), the reworked Settings > Models pane, and four fixes found in review of that work -- a sibling catalogue answering 200 with a non-catalogue body no longer crashes the whole fetch; adding a tag no longer erases the label and description already stored for that model; `authenticated` no longer demands a bearer token from a local provider reached by address; and registry rows are keyed by the spelling every lookup produces, which recovers 593 rows that no reader could reach. ## Type - [ ] Fix - [x] Feature - [x] Docs - [ ] CI / tooling - [ ] Refactor - [ ] Other ## Verification Commands run on the branch head, with their results: - `uv run pytest tests -q -k "provider or registry or catalog or model or rpc or gateway or docker or i18n"` -- 4389 passed, 46 skipped - `npx vitest run` in `ui-web` -- 1573 passed (101 files) - `npx vitest run` in `ui-tui` -- 2037 passed (142 files) - `uv run ruff check raven scripts tests` and `ruff format --check` -- clean - `npx tsc --noEmit` in `ui-web` and `npm run type-check` in `ui-tui` -- clean - `npm run lint:rpc` in `ui-tui` -- generated.ts in sync - `make check-source-language`, `make check-large-files` -- clean Each of the four review fixes was checked by reverting it and watching a named test go red, then restoring it. The container image itself has not been built by CI or by me -- no Docker daemon was available on the machine this was written on. It has been built and run by the author of the branch. - [x] Relevant tests pass locally - [x] Relevant lint / type checks pass locally - [x] User-facing docs or screenshots are updated when needed ## Risk Two deliberate security trade-offs in the container deployment, both documented in `docker/README.md`: - nginx rewrites the `Origin` header, because the engine accepts only an Origin naming its own loopback port and no browser reaching a published port can send one. This drops a defence-in-depth layer. The session cookie is SameSite=Strict, so an unauthenticated socket is still refused -- but SameSite ignores ports, so another page on localhost counts as same-site. - `RAVEN_AUTO_LOGIN=1` is the default: nginx attaches the session cookie to every page load, so anyone who can reach the published port is signed in. It is contained by Compose publishing on 127.0.0.1 only. Set it to 0 for any deployment reachable from elsewhere. These are two settings that must agree, and nothing warns when they do not. Behaviour changes outside the container: the gateway now starts without a configured provider rather than exiting 1, and the first-run onboarding overlay no longer opens by default (`?onboard=1` still forces it). Rollback is by revert; nothing here migrates state or changes an on-disk format. - [x] Security impact considered - [x] Backward compatibility considered - [x] Rollback path is clear for risky changes ## Related Issues N/A
207 lines
7.5 KiB
Bash
Executable File
207 lines
7.5 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
# Container entrypoint: nginx in front, the Raven engine behind it.
|
|
#
|
|
# docker compose up -> run (default)
|
|
# docker compose exec raven docker-entrypoint.sh signin
|
|
# -> print a fresh sign-in URL
|
|
# docker compose run --rm raven provider set anthropic --api-key sk-...
|
|
# -> any raven CLI command
|
|
#
|
|
# `run` and `signin` are dispatch keywords; anything else is handed to the
|
|
# `raven` CLI verbatim.
|
|
|
|
set -eu
|
|
|
|
# Defaults mirror the image's ENV. Repeated here so `set -u` cannot turn a
|
|
# variable someone dropped from .env into an unreadable failure.
|
|
PAGE_PORT="${RAVEN_PAGE_PORT:-18792}"
|
|
WEB_PORT="${RAVEN_WEB_PORT:-${PAGE_PORT}}"
|
|
AUTO_LOGIN="${RAVEN_AUTO_LOGIN:-1}"
|
|
RAVEN_HOME="${RAVEN_HOME:-/data/.raven}"
|
|
RAVEN_DIST_ROOT="${RAVEN_DIST_ROOT:-/app/ui-web/dist}"
|
|
RAVEN_PAGE_PORT="${PAGE_PORT}"
|
|
export RAVEN_HOME RAVEN_DIST_ROOT RAVEN_PAGE_PORT
|
|
BASE="http://127.0.0.1:${PAGE_PORT}"
|
|
|
|
log() { printf 'raven-docker: %s\n' "$*"; }
|
|
|
|
# The mark, on the container's first line of output. Quoted heredoc so the
|
|
# shell reads the art as bytes -- nothing in it is a shell metacharacter today,
|
|
# and this keeps that true if the drawing is ever redrawn. Lines carry no
|
|
# trailing spaces: the repo strips those on commit, and they render the same.
|
|
banner() {
|
|
cat <<'RAVEN_MARK'
|
|
░█████████
|
|
░██ ░██
|
|
░██ ░██ ░██████ ░██ ░██ ░███████ ░████████
|
|
░█████████ ░██ ░██ ░██ ░██ ░██ ░██ ░██
|
|
░██ ░██ ░███████ ░██ ░██ ░█████████ ░██ ░██
|
|
░██ ░██ ░██ ░██ ░██░██ ░██ ░██ ░██
|
|
░██ ░██ ░█████░██ ░███ ░███████ ░██ ░██
|
|
RAVEN_MARK
|
|
printf '\n'
|
|
}
|
|
|
|
# The engine writes {port, token, pid} here once it is listening; the token is
|
|
# what mints a sign-in nonce. Generated per boot rather than shipped in .env,
|
|
# so no deployment shares a secret with the repository.
|
|
serve_state() {
|
|
python - "$1" <<'PY'
|
|
import json, os, sys
|
|
path = os.path.join(os.environ.get("RAVEN_HOME", ""), "serve.json")
|
|
try:
|
|
with open(path, encoding="utf-8") as fh:
|
|
print(json.load(fh).get(sys.argv[1], ""))
|
|
except (OSError, ValueError):
|
|
print("")
|
|
PY
|
|
}
|
|
|
|
# 0 listening, 1 timed out, 2 the engine is gone.
|
|
wait_for_engine() {
|
|
# 120 s: a first boot builds the agent loop, the plugin stack and the
|
|
# memory store before it listens.
|
|
i=0
|
|
while [ "$i" -lt 240 ]; do
|
|
# Asked before the probe. A child that died on its first line answers
|
|
# nothing for the whole two minutes otherwise, and the container spends
|
|
# them looking like a slow start instead of a failed one -- `top` shows
|
|
# nginx, this shell and a `sleep`, and no engine.
|
|
if ! kill -0 "${engine}" 2>/dev/null; then
|
|
return 2
|
|
fi
|
|
if curl -fsS -m 2 "${BASE}/health" >/dev/null 2>&1; then
|
|
return 0
|
|
fi
|
|
i=$((i + 1))
|
|
sleep 0.5
|
|
done
|
|
return 1
|
|
}
|
|
|
|
sign_in_url() {
|
|
token="$(serve_state token)"
|
|
if [ -z "$token" ]; then
|
|
log "no serve.json yet -- is the engine running?"
|
|
return 1
|
|
fi
|
|
nonce="$(curl -fsS -m 5 -X POST -H "X-Raven-Token: ${token}" "${BASE}/auth/nonce" 2>/dev/null \
|
|
| python -c 'import json,sys; print(json.load(sys.stdin).get("nonce",""))' 2>/dev/null || true)"
|
|
if [ -z "$nonce" ]; then
|
|
log "could not mint a sign-in nonce"
|
|
return 1
|
|
fi
|
|
printf 'http://localhost:%s/auth#%s\n' "${WEB_PORT}" "${nonce}"
|
|
}
|
|
|
|
announce() {
|
|
url="$(sign_in_url || true)"
|
|
printf '\n'
|
|
log "the page is on http://localhost:${WEB_PORT}"
|
|
if [ -n "${url:-}" ]; then
|
|
log "sign in once with this one-time link (replace localhost if you are remote):"
|
|
printf '\n %s\n\n' "$url"
|
|
log "for another link: docker compose exec raven docker-entrypoint.sh signin"
|
|
fi
|
|
}
|
|
|
|
# A provider named in the environment is written into config on every boot:
|
|
# the container is declarative, so .env is the source of truth whenever it says
|
|
# anything. Leave RAVEN_PROVIDER empty and the browser wizard owns the config.
|
|
seed_provider() {
|
|
if [ -z "${RAVEN_PROVIDER:-}" ] \
|
|
|| { [ -z "${RAVEN_API_KEY:-}" ] && [ -z "${RAVEN_API_BASE:-}" ]; }; then
|
|
return 0
|
|
fi
|
|
log "configuring provider '${RAVEN_PROVIDER}' from the environment"
|
|
set -- provider set "${RAVEN_PROVIDER}"
|
|
if [ -n "${RAVEN_API_KEY:-}" ]; then
|
|
set -- "$@" --api-key "${RAVEN_API_KEY}"
|
|
fi
|
|
if [ -n "${RAVEN_API_BASE:-}" ]; then
|
|
set -- "$@" --api-base "${RAVEN_API_BASE}"
|
|
fi
|
|
raven "$@"
|
|
}
|
|
|
|
start_nginx() {
|
|
RAVEN_AUTO_LOGIN_COOKIE=""
|
|
if [ "${AUTO_LOGIN}" = "1" ] || [ "${AUTO_LOGIN}" = "true" ]; then
|
|
cookie="$(serve_state cookie)"
|
|
if [ -z "${cookie}" ]; then
|
|
log "the engine did not publish its browser session cookie"
|
|
return 1
|
|
fi
|
|
RAVEN_AUTO_LOGIN_COOKIE="raven_session_${PAGE_PORT}=${cookie}; Path=/; Max-Age=2592000; HttpOnly; SameSite=Strict"
|
|
fi
|
|
export RAVEN_AUTO_LOGIN_COOKIE
|
|
mkdir -p /etc/nginx/conf.d
|
|
# Only these values: nginx's own $variables must survive substitution.
|
|
envsubst '${RAVEN_DIST_ROOT} ${RAVEN_PAGE_PORT} ${RAVEN_AUTO_LOGIN_COOKIE}' \
|
|
< /etc/nginx/templates/raven.conf.template \
|
|
> /etc/nginx/conf.d/raven.conf
|
|
nginx -t
|
|
nginx
|
|
log "nginx serving ${RAVEN_DIST_ROOT} on :80, proxying ${BASE}"
|
|
}
|
|
|
|
run() {
|
|
# Printed here rather than at the top of the file: `signin` and the CLI
|
|
# passthrough are read by people and by scripts, and a banner ahead of a
|
|
# sign-in URL or a `raven status` is output nobody asked for.
|
|
banner
|
|
|
|
# The engine must land on exactly this port or nginx proxies into nothing;
|
|
# without strict mode it probes forward when the port looks taken.
|
|
RAVEN_SERVE_PORT_STRICT=1
|
|
export RAVEN_SERVE_PORT_STRICT
|
|
mkdir -p "${RAVEN_HOME}"
|
|
|
|
seed_provider
|
|
|
|
# Starts with no provider configured and warns; the page's Settings >
|
|
# Models is how one gets added, and the next turn picks it up.
|
|
raven gateway --page-port "${PAGE_PORT}" &
|
|
engine=$!
|
|
trap 'log "stopping"; kill -TERM "${engine}" 2>/dev/null || true' TERM INT
|
|
|
|
boot=0
|
|
wait_for_engine || boot=$?
|
|
if [ "$boot" -eq 0 ]; then
|
|
start_nginx
|
|
announce
|
|
elif [ "$boot" -eq 2 ]; then
|
|
log "the engine exited during start-up -- its error is above this line"
|
|
else
|
|
log "the engine did not answer ${BASE}/health within 120s; see the log above"
|
|
fi
|
|
|
|
# `wait` returns the moment a trap fires, with the child still shutting
|
|
# down -- a turn in flight and the memory store both want to finish. So the
|
|
# status is taken from the first wait and the exit is held until the process
|
|
# is actually gone.
|
|
status=0
|
|
wait "${engine}" || status=$?
|
|
while kill -0 "${engine}" 2>/dev/null; do
|
|
sleep 0.2
|
|
done
|
|
|
|
# Last, so the page keeps answering while the engine winds down, and the
|
|
# container never lingers serving a socket that is no longer there.
|
|
nginx -s quit 2>/dev/null || true
|
|
return "${status}"
|
|
}
|
|
|
|
case "${1:-run}" in
|
|
run)
|
|
shift 2>/dev/null || true
|
|
run
|
|
;;
|
|
signin)
|
|
sign_in_url
|
|
;;
|
|
*)
|
|
exec raven "$@"
|
|
;;
|
|
esac
|