20 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
What this is
A desktop pet (PySide6 window) that is a voice/click UI on top of an external
Bolt server's desk API — same brain, memory, tools, and persona as that
server's Discord bot and Linux desk client. This repo has no import
dependency on the server repo; it's a standalone HTTP client configured via
its own .env.
Pipeline: mic → openWakeWord ("thunderbolt", on-device) / push-to-talk / click → record utterance → Deepgram STT → + active-window context → POST /desk/converse → [server may relay a shell command to run on this machine, or a petctl pseudo-command that moves/emotes the pet instead] → reply → ElevenLabs streaming TTS (or offline pyttsx3 fallback) → speakers, with the
pet sprite/speech bubble reflecting state throughout, and playback
interruptible by talking over it (barge-in).
Side channels that let the pet act between turns: the heartbeat (proactive announcements), the desktop notification bridge, and autonomous wandering — all suppressed while it's napping (quiet hours / fullscreen DND).
Commands
# Setup + run (creates .venv and installs requirements.txt on first run)
./run.sh # macOS/Linux
run.bat # Windows
# Run tests (no pytest config file — tests self-insert repo root via sys.path).
# QT_QPA_PLATFORM=offscreen avoids a QApplication segfault on headless/no-display hosts.
QT_QPA_PLATFORM=offscreen .venv/bin/pytest tests/
.venv/bin/pytest tests/test_state.py::test_happy_path_transitions # single test
# Convert a grid sprite sheet into the per-frame-PNG convention sprite.py expects
python scripts/slice_spritesheet.py path/to/sheet.png assets/sprites/idle --cols 6 --rows 1
There is no lint/build step configured beyond pytest. cp .env.example .env
and fill in BOLT_SERVER_URL / DESK_API_KEY (+ DEEPGRAM_API_KEY,
ELEVENLABS_API_KEY) before running — without server config the controller
logs a missing-config message and exits its thread instead of starting.
Architecture
config.py— loads.envfrom the project root (not via python-dotenv; a small hand-rolled parser matching the server repo'sdesk_client/bolt_desk.pyconvention) into module-level constants. Everything else reads config from here, neveros.environdirectly.state.py—PetStateMachine, pure logic with no Qt/audio imports (kept that way deliberately for cheap unit testing). Enforces a transition table; notablyIDLE -> TALKINGis legal directly (no LISTENING/THINKING leg) because the heartbeat can make the pet speak proactively/unprompted.controller.py—PetController(QObject), the pipeline orchestrator. Runs on a backgroundQThread(wired inui/app.py) so audio I/O/network never blocks the Qt event loop; communicates with the UI only through Qt signals (state_changed,said,log,action,napping), never touches aQWidgetdirectly. Also drives the periodic heartbeat (_maybe_heartbeat, gated byHEARTBEAT_INTERVAL_SECONDS) which lets the server push proactive spoken announcements between user turns, and on the same tick re-evaluates nap state, checks for delivered files, and drains queued desktop notifications. It owns the live wake-word threshold (wake_threshold()is passed tolisten_for_wake_wordas a callable so the tray slider takes effect mid-listen) and the conversationhistory.server_client.py— HTTP client for the desk API, dependency-free beyondrequestsso it's easy to mock in tests.converse()loops relaying server-issued shell commands (run_local_command, executed viasubprocess.run(shell=True)as the desktop user, 30s default timeout) via/desk/tool_resultuntil the server sends a finalreply(capped at_MAX_RELAY_HOPS). This is the same "full desktop control" trust model as the server repo's other desk clients — commands only ever originate from the user's own voice/click requests in their own session.list_outbox_files/download_outbox_filehit the same/desk/filesand/desk/files/<id>endpoints the server'sdeliver_filestool queues onto — seefile_delivery.py.file_delivery.py— the filesystem half of receiving files the server queues via itsdeliver_filestool (ai/desk_api.pyin the main tmn-api repo — "send me that report" during a conversation spools the matched workspace files, zipping multiple into one, onto the session's outbox).controller._check_deliverieslists/desk/filesand downloads anything queued — right after a conversation/notification turn (the common case) and once per heartbeat tick for anything queued out-of-band — saving each underDELIVERED_FILES_DIR(default~/Downloads/Bolt). Downloading a file dequeues it server-side, so it's only ever handed out once;save()never overwrites an existing download, suffixing" (1)"," (2)", ... on a name collision.sanitize_filename()reduces a server-supplied name to its bare filename (Path(...).name), which is defense-in-depth against a delivered name that's secretly a path, since a per-user desk API key means the name isn't always coming from someone as trusted as the owner. Toggle off entirely withRECEIVE_FILES=false.audio/—mic.py(energy-based VAD utterance capture, ported from the server repo'sbolt_desk.py),wake_word.py(openWakeWordthunderbolt.onnxdetection +NearMissLogfor threshold tuning — see below),stt.py(Deepgram),tts.py(ElevenLabs, streaming by default —stream_pcm()+play_stream()start playback on the first chunk;chunks_to_int16()carries odd bytes across HTTP chunk boundaries, without which everything after the first split sample plays as static — falling back to whole-clip PCM then offlinepyttsx3),barge_in.py(two detectors behind onereset()/check()shape, chosen byBARGE_IN_MODEviamake_detector: wake (default) scores every frame with the same openWakeWord model the idle listener uses, so only the wake phrase cuts playback; energy is the original N-consecutive-loud-frames rule, threshold ~4x the VAD one because the mic hears the pet's own voice. Wake mode shares_default_modelwith the idle listener — the two never run concurrently — andreset()s it on detection so the tail of one reply can't count toward the next). Each accepts an injectable stream/model/protocol so tests don't need real audio hardware or a display.pet_actions.py—petctlpseudo-commands (petctl move top-left,petctl emote wave,say/wander/nap). The desk API has no "move the pet" payload type and this repo can't change the server, so these ride the existing shell-command relay:controller._handle_commandparses them and they never reachsubprocess; anything else is a real shell command exactly as before. Pure parsing; the UI half isPetWindow.apply_action.file_ops.py—filectlpseudo-commands, checked in_handle_commandright after petctl and before falling through to a real shell command. Executing arbitrary commands already worked via the shell relay (run_local_command— seeserver_client.pybelow); what filectl adds is a reliable way to do the read/write/edit/list slice of that, since getting the model to hand-roll a shell heredoc for multi-line content full of quotes/$/backticks is failure-prone — andlistexists as its own op (rather than relying on the model shelling out tols/dir) because this project is cross-platform and the model shouldn't have to guess which listing command applies on Windows vs. Linux vs. macOS; one glob-based op (pattern, default*;recursiveforrglobinstead ofglob) covers all three. Wire format isfilectl <json>where<json>is a single-line compact JSON object —{"op": "list"|"read"|"write"|"edit", "path": ..., ...}— not a multi-line marker block (an earlier design): the server relays this as the argument to the ordinarycommandtool marker, and that marker's extractor (ai/agents/default.pyin the main repo) only captures up to the next newline, so anything genuinely multi-line silently got truncated no matter how the prompt worded it. JSON sidesteps that for free —json.dumpsalready encodes embedded newlines as the two characters\n, not a real line break, so multi-line file content still fits on the one physical line the extractor sees.editrequires the old text to match exactly once — same discipline as this project's own code-editing tool — and raises rather than guessing if it's missing or ambiguous. This doesn't expand what the server can do to this machine (a relayed shell command could already overwrite anything the desktop user can write — see the security notes below); it's a safer path to the same capability. Pure parsing (parse) is separated from the filesystem I/O (execute), matching pet_actions.py's parse/describe split.screen_context.py— active-window title (xprop/xdotool, Win32, osascript) appended to each utterance viacontext_for(), plusis_fullscreen_active()for do-not-disturb. Text only — the desk API takes no images. Every probe is best-effort and returns None/False rather than raising; the parsing is split into pure functions that are tested without a display server.quiet.py— quiet-hours spec parsing (23:00-08:00, wraps midnight, comma-separated). Napping suppresses proactive noise and wandering only; wake word / click / push-to-talk still work.notifications.py— Linux/D-Bus notification bridge: tailsdbus-monitor, parses Notify calls (pureiter_notifications()), filters and rate-limits them (NotificationGate), and the controller forwards survivors throughconverse(). Off by default — each one is a round trip.sudo_askpass.py— makes server-relayedsudousable from a process with no terminal, by pointing sudo'sSUDO_ASKPASSat a GUI helper and rewriting baresudotosudo -A(add_askpass_flag, a conservative regex that skips anything already carrying a flag and anything inside quotes). Prefers a real askpass binary and falls back to generating a zenity/kdialog wrapper in~/.cache/bolt-pet/askpass.sh. Resolution order is injectable (is_executable/which) so it's testable on a machine with a different set installed. See the security notes — the dialog is the boundary.updater.py— self-update from the Gitea releases API. Polls<UPDATE_REPO_API>/releases/latestfor a tag newer thanbolt_pet.__version__and moves the checkout to it withgit fetch --tags+git checkout tags/<tag>, so "downloading an update" is just git and rolling back is one command. Three safety rules: a dirty working tree is skipped, never stashed (silently discarding your work-in-progress beats running an old version); everything after the checkout — dependency install, then an import smoke test in a subprocess (this process still has the old modules loaded, so importing in-process would prove nothing) — is guarded, and any failure rolls back to the exact ref that was live before, branch name or SHA; and the restart only happens once the new code imports, so a broken release costs a log line rather than a pet that won't start. Git goes through an injectablerun(args) -> (code, output)callable so apply/rollback is unit-tested against a fake git; version comparison and release parsing are pure.controller._maybe_updatedrives it from the wake-listener tick (so the pet is IDLE and between turns by construction) and the actualos.execvhappens inui/app.pyafterapp.exec()returns — that ordering is what guarantees the mic is released before the new process opens it.history.py— rolling transcript (HISTORY_LIMITturns) behind the tray's History window and click-to-copy on the bubble.hotkey.py— global push-to-talk viapynput; soft-fails with a logged reason (Wayland, missing package, macOS permissions) since the wake word is the primary trigger.speech_text.py— sanitizes server replies before they're heard/shown.for_speech()(called insidetts.speak(), so every path to the speakers is covered) strips markdown, emoji, URLs and stray symbols the voice would read literally ("asterisk asterisk"), turns bullet lists into full sentences, and words a few symbols (&→ "and").for_display()is the looser version for the speech bubble — markdown syntax gone, emoji kept.is_question()decides whether a reply leaves the pet waiting on an answer: it tests the spoken form (so a '?' inside a stripped code block or URL doesn't count) and only a trailing one counts, since a question asked in passing isn't awaiting a reply.controller._should_follow_upuses it to keep listening without the wake word, capped byFOLLOW_UP_MAX_TURNSso a server that ends every reply with a question can't loop forever off mic noise. Pure string logic, no Qt/audio imports.ui/—app.pywiresQApplication+PetWindow+PetTray+ the history/tuner windows + the push-to-talk hotkey + the controller thread together;pet_window.pyis the frameless/translucent/always-on-top sprite window + speech bubble (non-square frames are centered in the squarePET_SIZEwindow, seepaintEvent), and also owns:- wandering — a ~30fps timer walks the window toward a random on-screen
target every
PET_WANDER_INTERVAL_SECONDS(randomized), suppressed whenever the pet is non-IDLE, napping, dragged, or has a bubble up. A commandedpetctl moveoverrides all of that except the drag. - emotes —
emote_transform()is pure maths (dx, dy, rotation, scale from a 0..1 progress) kept out ofpaintEventso the curves are unit tested; every emote must return to the identity transform at progress 1.0 or the pet ends up permanently askew. - shaped input / click-through —
PET_SHAPED_INPUTmasks the window to the sprite's opaque pixels so the square window's transparent corners stop eating clicks (mask rebuilt only when the frame changes, and pinned to the resting position so a bob/spin doesn't thrash it);PET_CLICK_THROUGHmakes the pet ignore the mouse entirely. - edge snapping (
PET_EDGE_SNAP) after a drag or a stroll, and nap dimming (set_napping).sprite.pyloadsassets/sprites/<state>/*.png(filename-sorted, looping — currently Kenney's CC0 robot pack, seeassets/sprites/README.md) and falls back to a procedurally-drawn placeholder blob per state if a folder has no frames;tray.pyis the system tray menu (talk now / mute / nap / wander / click-through / history / wake-word tuning / quit) — the pet window has no title bar or taskbar entry;history_window.pyandwake_tuner.pyare the two dialogs it opens.
- wandering — a ~30fps timer walks the window toward a random on-screen
target every
Wake-word detection
audio/wake_word.py uses a custom-trained openWakeWord model,
thunderbolt.onnx (ships in the project root), the same way the server
repo's desk_client/bolt_desk.py uses bolt.onnx for "hey bolt" — same
runtime (openWakeWord, ONNX inference framework), same per-frame
predict()/reset() loop. Every mic frame is scored; any class score at or
above WAKE_WORD_THRESHOLD (default 0.5, in .env) counts as a
detection. Swap WAKE_MODEL_FILE to point at a differently-trained .onnx
model to change the wake phrase — everything downstream (STT, server call,
TTS) is unaffected.
openwakeword's Model.reset() is not enough to forget a detection. It
clears the prediction buffer only; the rolling audio window the classifier
actually scores lives in model.preprocessor (raw_data_buffer — 10s of raw
audio — plus melspectrogram_buffer and a ~120-frame feature_buffer) and
AudioFeatures has no reset method at all. So after a detection the wake
phrase is still in the window, and the next frame fed to the model re-fires on
it. Symptom when this bites: the pet cuts itself off a word into every reply,
because wake-mode barge-in resumes feeding the model and instantly matches the
"thunderbolt" that started the turn. wake_word.hard_reset(model) restores
the preprocessor to its as-constructed (silence) state and is what both
listen_for_wake_word and WakeWordBargeIn.reset() call — use it, not
reset(), anywhere a detection needs to be genuinely forgotten. The blank
state is cached on the preprocessor object (not in an id()-keyed dict —
CPython reuses ids after GC), since rebuilding it costs an ONNX pass over 10s
of silence.
The threshold is tunable at runtime: the tray's Wake word tuning… window
(ui/wake_tuner.py) shows the peak score seen and a rolling list of near
misses (frames within WAKE_NEAR_MISS_MARGIN below the threshold — i.e.
the times it nearly heard you), and its slider is read per frame because
listen_for_wake_word accepts a callable threshold. Set the threshold just
under the peak you can hit reliably, then persist it in .env.
Testing conventions
tests/ covers pure logic only (state machine, wake-word scoring loop, mic
VAD, HTTP client against mocks) — nothing there needs real audio hardware or
a display. Modules under test are written to accept fake streams/models/
on_command callables specifically to keep tests hardware-free; follow that
pattern (inject a Protocol-typed collaborator) rather than mocking at the
sounddevice/openwakeword import boundary when adding new testable logic.
test_controller.py, test_controller_features.py, test_wander.py and
test_pet_window_features.py need a QApplication, which segfaults without
a display unless run with QT_QPA_PLATFORM=offscreen.
Newer subsystems follow the same rule — the testable part is separated from
the part that needs hardware: dbus-monitor output is parsed by a pure
iter_notifications(lines), xprop output by pure parse_xprop_* functions,
HTTP chunk reassembly by chunks_to_int16, emote motion by
emote_transform. Tests that touch the controller monkeypatch
screen_context.context_for / is_fullscreen_active, otherwise they shell
out to xprop on a headless box.
Security notes
The server can relay a shell command back to this machine to execute as the
desktop user (see server_client.run_local_command). This is intentional
("full desktop control" for things like "open firefox" or disk checks) and
matches the trust model of the server repo's other desk clients. Keep
DESK_API_KEY private and don't expose the desk API port to the open
internet.
file_ops.py's filectl read/write/edit pseudo-commands ride that same
relay and are bound by the same trust model — no path is off-limits beyond
normal filesystem permissions for the desktop user, exactly like a relayed
cat/sed/rm already isn't. They don't grant the server anything a shell
command couldn't already do; they just make the read/write/edit path
reliable instead of relying on the model getting shell quoting right.
sudo_askpass.py widens that further, by design: with SUDO_ASKPASS_PROMPT
on (the default), a relayed bare sudo is rewritten to sudo -A and the
password is collected in a desktop dialog, so commands can escalate to root
instead of hanging on a tty the pet doesn't have. The dialog is the security
boundary — it's the only thing between the server deciding to run sudo and
it running, so the prompt is deliberately not suppressible per-command and
those commands get their own longer timeout (SUDO_COMMAND_TIMEOUT_SECONDS)
rather than being made non-interactive. Set SUDO_ASKPASS_PROMPT=false to
take the capability away entirely; sudo commands then fail. Note that
sudo -n / sudo -A / sudo -u … in a relayed command are never rewritten,
so an explicit non-interactive sudo stays non-interactive.
Don't run the pet as root. It needs no privileges of its own, PortAudio
can't reach the user's PipeWire socket from a root session (raw ALSA devices
reject the 16 kHz capture rate — paInvalidSampleRate), and every relayed
command would run unconstrained.
Two newer features widen what leaves this machine, both switchable in .env:
SCREEN_CONTEXT appends the focused window's title to each utterance
(titles often contain file paths, document names, or subject lines), and
NOTIFICATION_BRIDGE (off by default) forwards matching desktop
notifications to the server. Neither sends screenshots or notification
contents you haven't matched with NOTIFICATION_FILTER.