Author SHA1 Message Date
serversdown 3e623c14b4 docs: add MI50 documentation for maintenance and stack info. 2026-11-01 20:31:39 +00:00
serversdown 5560c7c3b0 docs(spec): pin why the git observer runs on the host, not in BIT's container
Batch job not request handler; volume mounts would rot as projects are added;
HTTP keeps the observer relocatable to another machine. BIT stays in Docker.
2026-09-02 04:29:17 +00:00
serversdown 422ecd1eb9 docs(spec): move the git observer and cadence math into BIT
Brian caught that the spec stated a frame and then violated it. The test is
'does it produce or derive facts -> BIT; does it require knowing Brian -> Lyra.'
The git observer produces facts and knows nothing about Brian; cadence health
derives from BIT's own time_logs and goal fields. Both belong in BIT.

Putting them in Lyra would have made BIT's data true only while Lyra was
running -- the exact coupling the separable frame exists to prevent. It also
makes BIT better standalone: auto-logged time plus cadence on the project
cards, which today show only a creation date.

Restructured into Part A (BIT, deterministic, no LLM) and Part B (Lyra, the
pick and the surfacing). Observer ships in BIT's repo but runs on the host and
POSTs over HTTP, so repos need no volume mounts.
2026-09-02 04:28:47 +00:00
serversdown 0bf3642dde docs(spec): project cadence — Lyra as BIT's observer and picker
Long-horizon project tracking. Key finding: BIT records every project as
last-touched 2026-03-21 while project-lyra has 177 commits across 30 working
days since. A manual tracker doesn't decay to neutral, it decays to actively
demoralizing — absence of records looks like absence of work.

Frame mirrors the pokerlog: BIT is the system of record, Lyra is a client.
BIT enumerates what's possible (/actionable returns 68 tasks); Lyra picks one.

Also found: the sessions table already exists as TimeLog on BIT's dev branch
(Feb 2026, never deployed), along with project goals and a pomodoro widget.
The blockers branch (deployed) has the dependency graph and actionable view.
Neither branch has both; divergence is 3 commits.
2026-09-02 02:23:48 +00:00
serversdown a175af0f25 Merge feat/poker into dev: poker ledger reliability + persona voice
Brings the 28-commit poker line onto dev — idempotent/guaranteed hand
logging, straddle capture, hero-stack auto-fill, turn-id fire-and-forget,
plus the merged persona 'How you talk' rewrite. Clean merge (disjoint
files vs dev's mi50-summary-cap-fallback tip).
2026-08-30 04:52:26 +00:00
serversdown c8ba55af29 Merge feat/persona: in-voice 'How you talk' rewrite (anti-tic rules + exemplars)
Lands the persona voice rewrite onto the active line. Disjoint from the
poker ledger work (persona.py / personas/lyra.md / persona eval vs.
chat/poker + tests), so a clean no-conflict merge.
2026-08-29 20:33:38 +00:00
serversdownandClaude Opus 4.8 7910d266db feat(chat): client turn-id makes fire-and-forget bulletproof
Brian fires a quick message then locks his phone to go play the hand — which drops
the SSE stream, and on wake the UI re-fires via the blocking fallback. The prior
(session, message) + 20s window caught the near-simultaneous case but not a re-fire
minutes later.

Now the UI stamps each send with a unique turnId (crypto.randomUUID) and carries the
SAME id on both the stream and the fallback; the server dedupes on it. Bulletproof
regardless of how long he's away — lock for an hour, come back, still exactly one
execution and one log — and a genuinely new send gets a fresh id so nothing legit is
swallowed. Id-keyed turns keep a long (1h) window; requests without an id keep the
short (session, msg) window for near-simultaneous dupes.

Verified end-to-end: two POSTs with the same turnId → one reply, one persisted
exchange pair (the duplicate reused the owner's result). 9 dedup tests; suite 235.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-12 01:28:10 +00:00
serversdownandClaude Opus 4.8 80519d20b1 docs: roadmap — scope roster→hand resolution + log today's ledger fixes
Adds the roster→hand seat/name resolution feature (needs seat+button tracking, so
it's a real feature not a fill) and records the 2026-07-11 shipped fixes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 00:18:44 +00:00
serversdownandClaude Opus 4.8 f3ecf8ffe4 fix(chat): de-duplicate the double turn execution at its source
The UI POSTs the SSE stream and, when nothing streams to the browser (iOS can't
read a fetch-stream body → the fetch throws in ~1s), falls back to the blocking
endpoint. But the server-side stream runs to completion regardless, so BOTH turns
executed — double-persisting the message and (once logging became guaranteed)
double-logging the hand.

Make a turn idempotent instead of chasing why the client bails: the first request
for a (session, message) owns it; a concurrent duplicate waits on the owner's
Event and reuses its reply rather than running a second full turn. respond and
respond_stream both claim/await; a finally always releases waiters. Short window
so a genuine later resend still runs fresh. Verified with a threaded race: two
simultaneous calls, body runs once, both get the same reply.

Also fixes the duplicate user-message persistence (the same double-execution) that
was polluting reconstructed history. 6 dedup tests + concurrency check; suite 232.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 00:17:48 +00:00
serversdownandClaude Opus 4.8 978cc0d662 feat(poker): auto-fill hero's stack from the last stack log
When a hand doesn't state hero's stack, default it to current_stack() (his last
logged stack) — the system already knows it from the stack log even when he doesn't
restate it every hand. record_hand._fill_hero_stack sets the hero player's stack and
marks stack_inferred=True (honest about stated vs inferred); a stack given in the
hand text always wins, and observed hands get nothing. 3 tests; suite 226 green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 00:11:42 +00:00
serversdownandClaude Opus 4.8 f28f0d4956 docs(poker): make the straddle rule explicit for any-seat (Meadows) straddles
Verified the parser captures a straddle from every non-blind seat (UTG..BTN, 7/7),
so no behavior change — but the prompt only named UTG/button examples. Spell out
that a straddle is legal from ANY non-blind seat (Mississippi/any-seat straddle,
common at the Meadows) and state the open-action seat per straddle position, as
insurance against model drift.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 23:57:20 +00:00
serversdownandClaude Opus 4.8 41c8a4dd1d fix(poker): idempotent hand logging + straddle capture
Two issues from live testing:

- Double-logged hand. The chat turn can execute TWICE — the SSE stream and the
  blocking fallback both run server-side (two 'chat request' lines, 1s apart) — a
  pre-existing double-execution (it also duplicated user messages) that the new
  logging guarantee turned into duplicate HANDS. record_hand is now idempotent:
  _recent_duplicate_hand returns an identical hand (same session, hole cards, board;
  NULL-safe) recorded in the last few minutes, so the second run reuses it instead
  of inserting. A system-of-record records an event once.

- Button straddle dropped. The parse prompt had no straddle logic. Added a STRADDLES
  rule: record any straddle as a preflop `post` by the straddler with its amount and
  respect the action order (button straddle acts last preflop, action opens in the
  SB; UTG straddle opens to its left). Verified: a btn-straddle hand now parses the
  straddle as {pos: BTN, action: post, amount: 6}.

Note: the underlying double turn-execution (stream + fallback) is a separate web-layer
bug worth fixing at the source — it wastes a full LLM turn and still double-persists
chat messages. Filed for a follow-up. 6 tests; suite 223 green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 21:19:06 +00:00
serversdownandClaude Opus 4.8 96a44365d9 fix(poker): guarantee hand logging — history was conditioning it away
Logging a stated hand was unreliable and got worse mid-session: same model, same
hand, clean history logged 4/4 but the real session's history logged 0/4. Root
cause: memory.recent() rebuilt past turns as "hand -> narration" with the tool
calls stripped (they live in tool_events), so the model's own context became
few-shot examples training it, mid-conversation, to STOP calling tools. Even a
maximal "LOG FIRST, no exceptions" prompt scored 0/5 — it's structural, not wording.

Two-part fix (both, per the system-of-record frame):
- A (guarantee): chat._ensure_hand_logged — on a HAND turn that's Brian's OWN hand,
  if the model didn't log it, force record_hand (tool_choice). Guarded to hero hands
  (looks_like_hero_hand) so an observed hand is never force-logged as his. Adds
  tool_choice passthrough to llm.chat_call; surfaces msg_type on TurnContext.
- B (heal forward): mind._history_with_tools makes each assistant turn's tool calls
  visible in reconstructed history ("record_hand -> Hand #62"), so the demonstrated
  pattern stops being "hand -> narrate". Recovers natural logging as logs accumulate.

Verified: force guard returns record_hand on the polluted context; full respond_stream
logs Hand #63 end-to-end on a clean session. 7 guard tests; suite 219 green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 20:48:04 +00:00
serversdownandClaude Opus 4.8 366e71a384 fix(poker): read showdowns off the tool, not by eye — and cut the mush
The HAND fragment let her narrate a finished board from memory: she called
quad kings "kings full" and never noticed Brian FLOPPED quads, then wrapped it
in variance-evens-out / resilience filler. Two fixes to _F_HAND:

- Correctness: at a showdown where both hands are known, call analyze_spot on
  the full board to confirm made hands + winner BEFORE commenting; name his hand
  class by the street it mattered ("flopped quads"). The eval already existed —
  she just never reached for it on a resolved hand. (It also catches impossible
  cards, e.g. a villain card already on the board.)
- Register: ban reflexive praise / variance-evens-out / life-lesson / cross-hand
  pep talk; if there's genuinely no leak, say so instead of inventing a takeaway.
  Surgical here; the full voice pass stays with the persona branch.

Verified live on the exact quad-kings hand (cloud/gpt-4o-mini): she now logs,
calls analyze_spot, reads quads-vs-quads correctly, and calls it a cooler with
no leak. Guard tests added. Full suite 212 green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-10 19:33:01 +00:00
serversdown 267b6ad7ba Merge pull request 'Big poker mode changes and hotfixes.' (#5) from fix/mi50-summary-cap-fallback into dev
Reviewed-on: #5
2026-07-04 15:09:32 -04:00
15 changed files with 2084 additions and 99 deletions

No files matched your search

+740
View File
@@ -0,0 +1,740 @@
# MI50 / Proxmox Maintenance Report
**Host:** `proxmox` (10.0.0.4) — Proxmox VE 9.0.11 on Debian 13 (trixie)
**Card:** AMD Radeon Instinct MI50 32 GB (Vega 20, gfx906) at PCI `0000:0a:00.0`
**Report date:** 2026-09-09
**Method:** read-only inspection over SSH from `lyra-cortex` (10.0.0.41). No files written, no packages touched, no services or guests started/stopped, no reboots.
**Card state at time of writing:** healthy and serving — 41 °C edge, 19 W, perf level `auto`, 70 % VRAM in use, llama.cpp `/health` returns `{"status":"ok"}`.
**Blast radius:** the MI50 itself is a Lyra-dedicated resource — no VM has a `hostpci` entry and `rocm-smi --showpids` shows only `llama-server`. **But this document is not only about the MI50.** §3.1 (boot/kernel), §7 (APT) and §8 (upgrades) are host-wide: the same box runs `tmi-dev` (VM 102) and `lyra-cortex` (VM 101). Breaking APT or the bootloader while chasing a GPU problem takes those down too.
## Labelling convention
| Tag | Meaning |
|---|---|
| **[OBS]** | Directly observed on the system this session. |
| **[INF]** | Inferred from evidence. Reasoning is given so you can check it. |
| **[UNK]** | Could not be determined read-only. |
---
## 1. Executive summary
Five things matter more than the rest:
1. **The MI50 is *not* a VFIO/PCI-passthrough setup.** The Proxmox host itself binds `amdgpu` to the card and shares the device nodes into **LXC CT202 (`gpu-jammy`)** by bind mount. ROCm lives inside CT202, not on the host. **[OBS]**
2. **The reason the card originally failed was a firmware/BIOS BAR problem, not the kernel.** Kernel logs show the 32 GiB BAR was unassignable (`can't assign; no space`) until something changed in the UEFI setup on 2025-10-18; after that the firmware itself placed BAR 0 at `0x7000000000`. **[OBS + INF]** — see §6. This is the single most important finding in this report.
3. **The 6.1.10-1-pve pin was adopted the night after that BIOS fix, and 6.14 was never re-tested with `amdgpu` unblacklisted.** The pin may be unnecessary — but that is *unproven*, and proving it costs a reboot. Do not remove it casually. See §6 and §11.
4. **The APT configuration is the biggest live hazard on this box** — it mixes Debian bookworm + trixie and points at the **PVE 8 (bookworm)** repo on a PVE 9 host. A careless `apt full-upgrade` is the most likely way to break this machine. See §8.
5. **`mi50-fan.service` is a thermal safety device, not a nicety.** The MI50 is a passively-cooled datacentre card; that script is what spins the chassis fans based on GPU junction temperature. See §7.
---
## 2. Current known-working architecture
```
Proxmox host "proxmox" (10.0.0.4) — Debian 13 trixie / PVE 9.0.11
kernel 6.1.10-1-pve (PINNED + apt-held)
cmdline: quiet amd_iommu=on iommu=pt pci=realloc video=efifb:off
|
|-- 0a:00.0 Vega20 [1002:66a1] subsys [1002:0834] <- MI50 32GB
| driver in use: amdgpu (HOST, in-tree 6.1.10 module)
| IOMMU group 24 (clean, alone)
| BAR0 = 32 GiB @ 0x7000000000 (assigned by firmware)
| creates: /dev/kfd (char 510:0) + /dev/dri/card1, renderD129
|
|-- 0b:00.0 NVIDIA GT 710 -> nouveau -> /dev/dri/card0, renderD128
| (host console video only; NOT used for compute)
|
+-- LXC CT202 "gpu-jammy" (10.0.0.42, Ubuntu 22.04, PRIVILEGED)
bind mounts /dev/kfd, /dev/dri, /sys/class/drm from host
ROCm 6.3.0-39 installed INSIDE the container
+ Docker container "lyra-brain"
image mixa3607/llama.cpp-gfx906:full-rocm-6.3.3
model qwen2.5-32b-instruct-q4_k_m.gguf, -ngl 99, -c 16384
listens 0.0.0.0:8080 -> http://10.0.0.42:8080
|
+-- VM 101 "lyra-cortex" (10.0.0.41) — plain QEMU guest, NO GPU.
Talks to the MI50 purely as an HTTP client of 10.0.0.42:8080.
```
**Not in the picture (deliberately):** no `hostpci` entry exists in any VM config; `vfio_pci` is loaded but has **zero devices bound**. **[OBS]**
### 2.1 The boot chain that actually makes it work
This is subtle and worth understanding before touching anything:
| # | Time (last boot) | What happens | Where it is configured |
|---|---|---|---|
| 1 | T+0 | Kernel boots with `pci=realloc`; firmware/kernel assign the 32 GiB BAR | `/etc/default/grub` |
| 2 | T+0 | `systemd-modules-load` reports **`Module 'amdgpu' is deny-listed (by kmod)`** — amdgpu does *not* load here | `/etc/modprobe.d/blacklist-temp-amdgpu.conf` |
| 3 | T+2 s | `kfd-fix.service` runs and `mknod`s a placeholder `/dev/kfd c 510 0` mode 666 | `/etc/systemd/system/kfd-fix.service` |
| 4 | T+20 s | `pve-guests` starts CT202; LXC bind-mounts the host devtmpfs nodes `/kfd` and `/dri` into the container (**shared** mount propagation) | `/etc/pve/lxc/202.conf` |
| 5 | T+68 s | `load-amdgpu.service` runs `/sbin/modprobe amdgpu` — an explicit by-name modprobe, which bypasses `blacklist` | `/etc/systemd/system/load-amdgpu.service` |
| 6 | T+68 s | amdgpu initialises Vega20, creates the real `/dev/kfd` (inode 742) and `/dev/dri/card1`; **shared mount propagation carries it into CT202 automatically** | kernel |
**[OBS]** Container and host `/dev/kfd` are confirmed the same inode (742, rdev 510:0) via `shared:397 master:2` devtmpfs propagation.
**[INF] Why step 3 matters:** CT202 starts *before* amdgpu loads. `kfd-fix.service` guarantees the bind-mount target exists at container start; propagation then delivers the real device 48 seconds later. Without the placeholder the `optional` bind would be silently skipped and the container would come up with no `/dev/kfd`. **This service looks trivial and is load-bearing.**
**[OBS] Known cosmetic race:** `mi50-fan.service` and `mi50-perf.service` are both `After=multi-user.target`, same as `load-amdgpu.service`, so at boot they fire *before* amdgpu exists and log errors:
```
rocm-smi[2718]: ERROR:root:Driver not initialized (amdgpu not found in modules)
python3[2717]: json.decoder.JSONDecodeError: Expecting value: line 1 column 1
```
`mi50-fan.service` has `Restart=always`/`RestartSec=5` so it self-heals within ~seconds. `mi50-perf.service` is `Type=oneshot` and **does not retry** — yet `power_dpm_force_performance_level` currently reads `auto`. **[INF]** Either a later manual run set it, or `auto` is simply the driver default and the service is redundant in practice.
---
### 2.2 How it works — the mental model
The single idea that explains every strange version number here:
> **ROCm is not one piece of software. It is a kernel half and a userspace half, and they version independently.**
- **Kernel half** — `amdgpu` + KFD (Kernel Fusion Driver), compiled into the kernel. Owns the card, manages VRAM, creates queues. **[OBS]** Here: `amdgpu` DRM **3.49.0**, from kernel 6.1.10-1-pve (built 2023-02-07).
- **Userspace half** — the HSA runtime (ROCr), HIP, rocBLAS, MIOpen. Just ordinary files under `/opt/rocm`. **[OBS]** Here: **ROCm 6.3** (late 2024), HSA Runtime 1.14, HIP 6.3.42131.
Because an LXC container **shares the host kernel**, it needs no driver of its own — only the *device nodes*. It brings its own userspace half and speaks directly to the host's kernel half through the KFD ioctl interface.
That is the whole trick, and it is why this design was the right call.
#### The two device nodes (you need both)
| Node | Major:minor | What it carries |
|---|---|---|
| `/dev/kfd` | **510**:0 | Compute path — HSA queues, signals, VRAM allocation. This is *the* ROCm compute interface. |
| `/dev/dri/renderD129` | 226:129 | DRM render node — buffer allocation via `libdrm_amdgpu`. |
**[OBS]** `/dev/dri/card0` + `renderD128` belong to the **GT 710 / nouveau**, not the MI50. The MI50 is `card1` / `renderD129` (`/dev/dri/by-path/pci-0000:0a:00.0-render`).
**[OBS]** KFD topology confirms the card: node 1 = `vega20`, `gfx_target_version 90006` (= gfx906), `simd_count 240` (60 CU × 4 SIMD).
#### The layer cake, with what is actually at each level
```
docker "lyra-brain" llama.cpp + /app/libggml-hip.so (built for gfx906)
ROCm 6.3.3-74 <-- ITS OWN COPY; this is what serves
| devices: /dev/kfd, /dev/dri (docker-compose `devices:`)
v
LXC CT202 "gpu-jammy" Ubuntu 22.04, privileged, shares the host kernel
ROCm 6.3.0-39 <-- diagnostics only, NOT used for inference
| bind mount (lxc.mount.entry)
v
device nodes /dev/kfd (510:0) + /dev/dri/renderD129 (226:129)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| ioctl, KFD ABI <-- THE SPLIT: userspace above, kernel below
v
host kernel 6.1.10-1-pve amdgpu, DRM 3.49.0 — the ONLY kernel driver in the stack
| PCIe, BAR0 32 GiB @ 0x7000000000
v
MI50 / Vega 20 / gfx906 60 CU, 240 SIMD, 32 GB HBM2, 0000:0a:00.0
```
#### **[OBS] The Docker image carries its own ROCm — and that is the one that matters**
```
CT202 : /opt/rocm -> /opt/rocm-6.3.0 version 6.3.0-39
lyra-brain container : /opt/rocm -> /opt/rocm-6.3.3 version 6.3.3-74
```
**These are two different ROCm installs.** The one inside CT202 is used only by `rocminfo` / `rocm-smi` for diagnostics; the inference stack is entirely self-contained in the Docker image.
Practical consequences:
- Upgrading, breaking or removing ROCm **inside CT202** would not affect model serving at all.
- The **Docker image is the artefact worth protecting**, more than the container around it (§9.4).
- CT202 is therefore far more disposable than it looks — it is a device-node shim with a Docker daemon.
**[OBS]** What llama.cpp reports at load, confirming the whole chain end to end:
```
ggml_cuda_init: found 1 ROCm devices:
Device 0: AMD Instinct MI50/MI60, gfx906:sramecc+:xnack+ (0x906), VMM: no, Wave Size: 64
load_backend: loaded ROCm backend from /app/libggml-hip.so
using device ROCm0 (AMD Instinct MI50/MI60) (0000:0a:00.0) - 32732 MiB free
load_tensors: offloaded 65/65 layers to GPU
load_tensors: ROCm0 model buffer size = 18508.35 MiB
llama_kv_cache: ROCm0 KV buffer size = 4096.00 MiB
```
18,508 MiB weights + 4,096 MiB KV ≈ 22.6 GiB ≈ the 24.2 GB / 70 % that `rocm-smi` reports.
#### Why passthrough was the wrong shape for this problem
VFIO hands the **entire PCI device** to a guest, which then needs (a) its own `amdgpu` in the guest kernel and (b) the card's 32 GiB BAR mapped into guest address space. Point (b) is precisely what the firmware was refusing to do (§5.1). Container device sharing needs neither: one kernel, no remap.
**[INF]** The passthrough attempts and the early LXC attempts were failing for the *same* underlying reason — the unassignable BAR — which is why switching architectures appeared not to help until the BIOS change landed.
#### **[INF] The hidden cost of the kernel pin**
The kernel half is from **February 2023**; the userspace half is from **late 2024**. That skew is roughly two years, and **AMD does not support it**. It works because nothing in ROCm 6.3 has yet required a KFD ioctl that this kernel lacks.
This means the pin is not only a boot-stability decision — **it caps how far the ROCm userspace can ever advance.** The day a newer llama.cpp/ROCm image needs a newer KFD interface, inference breaks, and the remedy is the kernel question in §5/§11, not a container change. Treat those as one problem, not two.
---
## 3. Exact configuration and file locations
### 3.1 Boot / kernel
| File | Content | Notes |
|---|---|---|
| `/etc/default/grub` | `GRUB_CMDLINE_LINUX_DEFAULT="quiet amd_iommu=on iommu=pt pci=realloc video=efifb:off"` | mtime 2025-10-17 16:27. **`pci=realloc` is the load-bearing flag.** |
| `/etc/default/grub.d/proxmox-kernel-pin.cfg` | `GRUB_DEFAULT="gnulinux-advanced-ed95b0fe…>gnulinux-6.1.10-1-pve-advanced-ed95b0fe…"` | mtime 2025-10-19 00:14. Written by `proxmox-boot-tool kernel pin`. |
| `/etc/kernel/proxmox-boot-pin` | `6.1.10-1-pve` | Same mtime. The pin's source of truth. |
| `/boot/grub/grub.cfg` | generated; default entry = 6.1.10-1-pve | mtime 2025-10-19 18:35. |
| Bootloader | **GRUB (UEFI)**, `\EFI\PROXMOX\SHIMX64.EFI`, ESP = `nvme0n1p2` (1 GiB vfat) | `/etc/kernel/proxmox-boot-uuids` does **not** exist → `proxmox-boot-tool` ESP-sync mode is **not** in use; this is a plain grub-efi install. **[OBS]** |
| systemd-boot | not used (`/etc/kernel/cmdline` absent) | **[OBS]** |
**[OBS] Note on `amd_iommu=on`:** the kernel logs `AMD-Vi: Unknown option - 'on'`. It is not a valid value for that parameter on this kernel and is a **no-op**. IOMMU is active regardless (`AMD-Vi: Interrupt remapping enabled`, 30 IOMMU groups). Harmless — but do not "fix" it as part of any other change; leave the string byte-identical unless you are deliberately testing.
**[OBS]** `video=efifb:off` and `/etc/modules-load.d/vfio.conf` are residue from an abandoned VFIO-passthrough approach (see §5.3). They are currently inert but were part of the working configuration when it was validated.
### 3.2 Module configuration
| File | Content | Effect |
|---|---|---|
| `/etc/modprobe.d/amdgpu.conf` | `options amdgpu noretry=0` | **[OBS]** Live: `/sys/module/amdgpu/parameters/noretry` = `0`. Set deliberately 2025-12-10 (shell history shows the value being read first, then set, then `update-initramfs -u` twice). **[INF]** Required for gfx906 XNACK/retry-fault handling; `rocminfo` reports the agent as `gfx906:sramecc+:xnack+`. |
| `/etc/modprobe.d/blacklist-temp-amdgpu.conf` | `blacklist amdgpu` | **[OBS]** Effective — kmod denies the autoload. Despite the filename `-temp-`, **this is currently part of the working configuration**: it is what defers amdgpu until after the containers' mount targets exist. |
| `/etc/modules-load.d/amdgpu.conf` | `amdgpu` | **[OBS] Dead weight.** systemd-modules-load explicitly skips it (`deny-listed (by kmod)`). Removing it changes nothing; it is *not* what loads the driver. |
| `/etc/modules-load.d/vfio.conf` | `vfio`, `vfio_iommu_type1`, `vfio_pci`, `vfio_virqfd` | **[OBS]** Modules load, **zero devices bound** to `vfio-pci`. Residue. |
| `/etc/modprobe.d/pve-blacklist.conf` | `blacklist nvidiafb` | Proxmox stock. |
| `/etc/modules` | `jc42`, `nct6775` (x3, from repeated `sensors-detect` runs) | **`nct6775` is required by the fan controller** — see §3.4. |
| `/etc/initramfs-tools/modules` | empty (comments only) | No GPU modules forced into initramfs. |
| `/etc/initramfs-tools/conf.d/pve-initramfs.conf` | `RESUME=none` | Proxmox stock. |
| Custom initramfs hooks/scripts | **none** | `hooks/`, `scripts/init-top/`, `scripts/init-premount/` are all empty. **[OBS]** |
### 3.3 Systemd units (all custom, all in `/etc/systemd/system/`)
| Unit | Enabled | State now | Purpose |
|---|---|---|---|
| `kfd-fix.service` | yes | inactive (oneshot, ran OK) | `[[ -e /dev/kfd ]] \|\| mknod -m 666 /dev/kfd c 510 0`. **Critical** — see §2.1. |
| `load-amdgpu.service` | yes | inactive (oneshot, `Result=success`) | `/sbin/modprobe amdgpu`. **This is what actually loads the driver.** |
| `mi50-fan.service` | yes | **active** | `/usr/bin/python3 -u /root/mi50_fan_curve.py`. **Thermal safety — see §7.** |
| `mi50-perf.service` | yes | active (RemainAfterExit) | `rocm-smi --setperflevel auto`. |
| `mi50-perf.service.bak` | — | — | Historical: same but `--setperflevel high`. **[INF]** Switched to `auto` on 2026-06-25 to let the card idle down. |
| `mi50-watchdog.service` + `.timer` | yes | timer **active**, every 2 min | Runaway guard — see §3.5. |
### 3.4 Scripts
| Path | Purpose |
|---|---|
| `/root/mi50_fan_curve.py` | Polls `rocm-smi --showtemp --json --device 0` every 5 s; writes PWM to `/sys/class/hwmon/hwmon3/pwm{1,2,3}` (`_enable`=1 then value). Steps: <50 °C→120, <70→160, <85→200, else 255. |
| `/usr/local/sbin/mi50-watchdog.sh` | Host-side runaway guard (see §3.5). Well-commented; references `docs/superpowers/specs/2026-07-04-mi50-runaway-guards-design.md` in the Lyra repo. |
| `/usr/local/bin/gpuup` | Manual bring-up helper. **Targets CT201, not CT202.** Loads modules, recreates `/dev/kfd` via `mknod … c 510 0`, chmods, starts CT201, runs a torch test. |
| `/usr/local/bin/gpu-check.sh` | Read-only-ish health check. **Also targets CT201.** |
| `/usr/local/bin/gpuup.backup` | Byte-identical to `gpuup` (`diff` clean). |
**[OBS] hwmon mapping — a real fragility.** The fan script hardcodes `hwmon3`. Current map:
```
hwmon0 nvme hwmon3 nct6798 (<- superio, the PWM chip the script writes)
hwmon1 jc42 hwmon4 k10temp
hwmon2 jc42 hwmon5 asus / hwmon6 asusec / hwmon7 nouveau
hwmon8 amdgpu (<- the MI50's own sensors)
```
`hwmon3` is the `nct6775` platform device. **[INF]** Its index depends on probe order of `nvme`, two `jc42` I²C sensors and `nct6775`. A kernel change or a hardware change (e.g. removing a DIMM with a jc42 sensor) could renumber it, and the script would then write PWM values to the wrong chip **without erroring loudly**. See §7 and §12.
### 3.5 Runaway watchdog
`mi50-watchdog.timer` → `mi50-watchdog.sh` every 2 min:
- Stops if `docker inspect lyra-brain` isn't running.
- **Duration rule:** GPU use > 0 % continuously for `MAX_BUSY_SEC` (3600 s) → `pct exec 202 -- docker stop lyra-brain`.
- **Temperature rule:** junction ≥ `TEMP_KILL_C` (97 °C) for `TEMP_KILL_STREAK` (3) consecutive checks → same.
- Alerts via ntfy at `http://10.0.0.41:8090`, topic `lyra`. State files in `/run/`.
### 3.6 Guest configuration
**CT202 `/etc/pve/lxc/202.conf`** (the live one, `onboot: 1`):
```
unprivileged: 0
features: keyctl=1,nesting=1
lxc.apparmor.profile: unconfined
lxc.apparmor.allow_nesting: 1
lxc.cap.drop: # empty = drop nothing
lxc.mount.entry: /dev/kfd dev/kfd none bind,optional,create=file,mode=0666
lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir
lxc.mount.entry: /sys/class/drm sys/class/drm none bind,ro,optional,create=dir
lxc.mount.entry: /opt/rocm /opt/rocm none bind,ro,optional,create=dir
lxc.cgroup2.devices.allow: c 226:* rwm # drm
lxc.cgroup2.devices.allow: c 29:* rwm # fb
lxc.cgroup2.devices.allow: c 189:* rwm # usb serial
lxc.cgroup2.devices.allow: c 238:* … c 250:* rwm
lxc.cgroup2.devices.allow: c 510:0 rwm # <- /dev/kfd, DYNAMIC MAJOR
mp0: /opt/models/deepseek,mp=/opt/models/deepseek
net0: … ip=10.0.0.42/24 …
```
**CT201 `cortex-gpu`** — same GPU stanza, `onboot: 0`, currently **stopped**. It carries the original hand-written documentation, preserved verbatim:
```
# --- GPU passthrough: AMD ROCm (MI50 / gfx906) ---
# ============================================================
# DO NOT ADD /dev/bus/pci passthrough — breaks ROCm
# ============================================================
```
**[INF]** CT201 is the older/experimental container (PyTorch + venv, targeted by `gpuup`/`gpu-check.sh`); CT202 is the production one. Keep CT201 as a reference config even though it is idle.
**[OBS] `/opt/rocm` does not exist on the host** — that bind mount is `optional` and silently no-ops in both containers. ROCm is installed *inside* CT202 at `/opt/rocm` → `/opt/rocm-6.3.0`. **[INF]** ROCm was once installed on the host (shell history repeatedly calls `/opt/rocm/bin/rocminfo` on the host) and was later removed; the bind entry and the host's `rocm.list` are leftovers from that era.
**VM 101 `lyra-cortex`** — `q35` + `ovmf`, 12 cores, 10 GB, **no `hostpci`**, `onboot: 0`, `startup: order=2`.
### 3.7 Docker in CT202
`/opt/models/docker-compose.yml`:
```yaml
services:
lyra-brain:
image: mixa3607/llama.cpp-gfx906:full-rocm-6.3.3
restart: unless-stopped
ports: ["8080:8080"]
devices: [/dev/kfd:/dev/kfd, /dev/dri:/dev/dri]
volumes: [/opt/models:/models]
command: >
--server --jinja -m /models/deepseek/qwen2.5-32b-instruct-q4_k_m.gguf
-c 16384 -ngl 99 --cache-ram 4096 --parallel 1 --port 8080 --host 0.0.0.0
healthcheck: curl -f http://localhost:8080/health (30s/10s/3, start_period 120s)
```
**[OBS]** Runs **non-privileged**, with **no `group_add`** and no `HSA_OVERRIDE_GFX_VERSION`. It works because the container runs as root inside an already-privileged LXC, and because the image is a **native gfx906 build** — no override needed.
`/opt/vllm/docker-compose.yml` also exists (`nalanzeyu/vllm-gfx906`, port 8000) but **is not running**. Keep it: it is the fallback/alternative runtime and it *does* use `group_add: [video, render]`.
---
## 4. Package and kernel dependencies
### 4.1 Host
| Item | Version | State |
|---|---|---|
| Debian | 13.1 trixie | |
| Proxmox VE | 9.0.11 (`proxmox-ve` 9.0.0) | |
| **Running kernel** | **6.1.10-1-pve** (built 2023-02-07) | pinned + **`apt-mark hold`** |
| `pve-kernel-6.1.10-1-pve` | 6.1.10-1 | dpkg state **`hi`** (hold+installed). Installed 2025-10-19 00:06. |
| `proxmox-kernel-6.14.11-4-pve-signed` | 6.14.11-4 | installed, not booted |
| `proxmox-kernel-6.14.11-3-pve-signed` | 6.14.11-3 | installed, not booted |
| `proxmox-kernel-6.14.8-2-pve-signed` | 6.14.8-2 | installed; **present in `/boot` and GRUB but absent from `proxmox-boot-tool kernel list`** |
| `proxmox-kernel-6.14` (meta) | 6.14.11-4 | **not held** — candidate is `6.14.11-9~bpo12+1` |
| `rocm-smi` / `librocm-smi64-1` | 6.1.2-1 | from **Debian trixie main**, not from AMD. Used only by the fan/perf/watchdog scripts. |
| `amdgpu` driver | **in-tree** `/lib/modules/6.1.10-1-pve/…/amdgpu.ko`, vermagic `6.1.10-1-pve` | **No DKMS amdgpu on the host.** |
| `/opt/rocm` on host | **absent** | |
**[OBS] The 6.1.10 kernel package is only available from `http://download.proxmox.com/debian/pve **bookworm** pve-no-subscription`.** It does not exist in the trixie/PVE 9 archive. This is a concrete reason the bookworm PVE repo line is load-bearing — see §8 and §9.1.
### 4.2 CT202 (`gpu-jammy`)
| Item | Version |
|---|---|
| Ubuntu | 22.04.5 LTS (jammy) — kernel is the host's 6.1.10-1-pve |
| ROCm | **6.3.0-39** (`/opt/rocm` → `/opt/rocm-6.3.0`) |
| Agent | `gfx906`, `AMD Instinct MI50/MI60`, 60 CUs, `gfx906:sramecc+:xnack+` |
| `amdgpu-dkms` | `1:6.10.5.60300-2084815.22.04` |
| `amdgpu-core` / `amdgpu-install` | `1:6.3.60300-2084815.22.04` |
| hip / hsa-rocr / rocblas / miopen etc. | `…60300-39~22.04` (ROCm 6.3.0 set) |
| Repos | `repo.radeon.com/amdgpu/6.3/ubuntu jammy` + `repo.radeon.com/rocm/apt/6.3 jammy`, key `/etc/apt/keyrings/rocm.gpg` |
| APT holds | **none** |
**[INF] `amdgpu-dkms` inside CT202 is inert.** An LXC container shares the host kernel and cannot load its own modules; the host's in-tree amdgpu is what drives the card. The package is presumably a dependency pulled in by `amdgpu-install`. It looks alarming and is harmless — but see §7.4, because it is *not* harmless if it ever succeeds in building.
### 4.3 The gfx906 software chain — why these exact versions
**[INF]** gfx906 (Vega 20) was moved to maintenance/deprecated status by AMD in ROCm 6.x, and mainline llama.cpp/vLLM ROCm builds dropped or stopped testing it. Both runtimes here are **community gfx906-specific forks**:
- `mixa3607/llama.cpp-gfx906:full-rocm-6.3.3`
- `nalanzeyu/vllm-gfx906`
These are third-party images. **[UNK]** Whether either publisher will keep publishing updates. Treat the pulled image layers as an artefact worth backing up (§9.4).
---
## 5. Why the kernel is pinned — the actual evidence
This is the part most likely to be mis-remembered, so here is the raw timeline from the persistent journal (`/var/log/journal`, 3.4 GB, retained back to 2025-10-16) and `/var/log/dpkg.log*`.
### 5.1 What broke
**[OBS]** On **6.14.11-3-pve**, booted with cmdline `… ro quiet` (nothing else), 2025-10-17:
```
pci 0000:0a:00.0: BAR 0 [mem 0x00000000-0x7ffffffff 64bit pref]
pci 0000:0a:00.0: BAR 0 [mem size 0x800000000 64bit pref]: can't assign; no space
pci 0000:0a:00.0: BAR 0 [mem size 0x800000000 64bit pref]: failed to assign
…
amdgpu 0000:0a:00.0: amdgpu: Fatal error during GPU init
amdgpu 0000:0a:00.0: probe with driver amdgpu failed with error -12
```
`0x800000000` = 32 GiB. `-12` = `-ENOMEM`. The firmware handed the kernel an **unassigned** BAR 0 and there was no 64-bit space to place it, so amdgpu could not initialise.
### 5.2 The sequence of attempted fixes (all [OBS], from journal + file mtimes + dpkg.log)
| When | What | Result |
|---|---|---|
| Oct 16 20:06 – Oct 17 13:07 | 6.14.11-3, cmdline `ro quiet` | BAR fails, amdgpu `-12` |
| Oct 17 13:41 | cmdline `amd)iommu=on iommu=pt` — **note the typo, `)` instead of `_`** | BAR still fails |
| Oct 17 13:51 – 14:38 | typo fixed: `amd_iommu=on iommu=pt` | BAR still fails |
| Oct 17 16:27 | `/etc/default/grub` edited → adds `pci=realloc video=efifb:off` | |
| Oct 17 16:04 | `/etc/modules-load.d/vfio.conf` created | VFIO passthrough being explored |
| Oct 17 16:30 | 6.14.11-3, **full current cmdline** | **BAR still fails** |
| Oct 17 18:22 | `proxmox-kernel-6.14.11-4` installed | |
| Oct 17 18:24 – Oct 18 03:27 | 6.14.11-4, full cmdline, ~9 boots | **BAR still fails** |
| Oct 18 02:29 | `/etc/modprobe.d/blacklist-temp-amdgpu.conf` created (`blacklist amdgpu`) | driver stops loading at all |
| **Oct 18 23:17** | **6.14.11-4, full cmdline** | **`BAR 0 [mem 0x7000000000-0x77ffffffff 64bit pref]` — ASSIGNED. No errors.** |
| Oct 19 00:06 | `pve-kernel-6.1.10-1-pve` installed | |
| Oct 19 00:11 | boot on 6.1.10-1-pve | `amdgpu: VRAM: 32752M`, `kfd kfd: amdgpu: added device 1002:66a1`, `[drm] Initialized amdgpu 3.49.0` |
| Oct 19 00:14 | kernel pinned to 6.1.10-1-pve | |
### 5.3 What this means
**[OBS]** Between the 02:33 boot and the 23:17 boot on **2025-10-18**, with **no kernel change, no cmdline change and no config file change**, BAR 0 went from `can't assign; no space` to being placed by firmware at `0x7000000000` — the *same address it occupies today* (confirmed in `/proc/iomem`).
**[INF, high confidence]** The only thing that can change that with no OS-side change is a **UEFI/BIOS setting** — almost certainly **Above 4G Decoding** (and possibly Resizable BAR / large-BAR support) being enabled. That is the classic fix for a 32 GB-BAR card, and the symptom matches exactly.
**[INF, high confidence] The root cause of the original failure was therefore firmware configuration, not kernel 6.14.**
**[OBS] The critical gap:** at that 23:17 boot on 6.14.11-4, `amdgpu` was **blacklisted** (created 02:29 that morning) and `load-amdgpu.service` did not yet exist (created 2025-12-07). The journal for that boot contains **no amdgpu messages at all**. So the driver was never given a chance to initialise on 6.14 after the BAR was fixed. Roughly six hours later 6.1.10 was installed, worked, and was pinned.
**[INF] Conclusion: the pin was adopted on the same night the real fix landed, and 6.14 was never A/B-tested against it.** The pin is plausibly unnecessary. It is also entirely possible that 6.14 has a *separate* gfx906 regression that simply was never reached. **[UNK]** — resolving this needs a reboot, and §9.2 gives a safe procedure.
**Do not treat this section as permission to unpin.** Treat it as: *the documented reason for the pin is weaker than it looks, and there is a cheap, reversible experiment available when you want the answer.*
---
## 6. What must NOT be changed casually
Ranked by how badly and how quickly it bites.
### 6.1 Tier 1 — silent hardware risk
1. **`mi50-fan.service` / `/root/mi50_fan_curve.py` / the `nct6775` module.**
The MI50 is a **passively cooled** datacentre card with no fan of its own. This script is the only thing tying chassis fan speed to GPU junction temperature. If it stops, if `nct6775` fails to load, or if `hwmon3` renumbers, **the card will cook silently** — the PWM writes will succeed against the wrong chip or the fans will drop to BIOS defaults. `/etc/modules` listing `nct6775` is therefore a **thermal dependency**, not a leftover from `sensors-detect`.
→ Never disable, never "clean up" `/etc/modules`, and re-verify `hwmon3` after any kernel or hardware change (§12).
2. **`mi50-watchdog.timer`.** The independent 97 °C / 1-hour backstop. Second layer of the same protection.
### 6.2 Tier 2 — breaks the GPU
3. **`/etc/default/grub` → `pci=realloc`.** Part of the validated working cmdline. Keep the string byte-identical (including the no-op `amd_iommu=on`) unless deliberately testing.
4. **BIOS/UEFI: Above 4G Decoding (and Resizable BAR).** **[INF]** The real fix. A CMOS clear, a BIOS update, or a firmware "load optimized defaults" will very likely re-break the card in exactly the way §5.1 describes. **If you flash or reset the BIOS, re-enable Above 4G Decoding before booting.**
5. **`/etc/modprobe.d/blacklist-temp-amdgpu.conf` + `load-amdgpu.service` — as a pair.**
The `-temp-` in the filename is misleading. Deleting the blacklist alone would let amdgpu autoload at PCI probe, which changes device-node timing relative to container start. Deleting `load-amdgpu.service` alone leaves the blacklist in force and the **GPU will simply never come up**. Change both together or neither.
6. **`kfd-fix.service`.** Two lines, easily mistaken for a leftover. It is what makes CT202's `/dev/kfd` bind-mount target exist before the driver does (§2.1).
7. **`/etc/modprobe.d/amdgpu.conf` → `options amdgpu noretry=0`.** Deliberately set; matches the `xnack+` agent mode. Do not remove.
8. **`/dev/kfd` major number `510` — hardcoded in three places:**
`kfd-fix.service`, `/usr/local/bin/gpuup`, and `lxc.cgroup2.devices.allow: c 510:0 rwm` in **both** 201.conf and 202.conf.
**[OBS]** 510 is a **dynamically allocated** major (`/proc/devices` confirms `510 kfd`), not a fixed one. **[INF] A different kernel — including 6.14 — may well allocate a different major, at which point `kfd-fix` creates a device node pointing at nothing and the LXC cgroup rule denies the real one.** This is the most likely *second-order* failure if the kernel is ever changed, and it will look like "ROCm can't see the GPU" rather than "the kernel broke". §9.2 checks for it explicitly.
9. **`unprivileged: 0` + `lxc.cap.drop:` (empty) + `lxc.apparmor.profile: unconfined` on CT202.** Necessary for this style of device sharing. **[INF]** Also means CT202 is effectively root-equivalent on the host — worth knowing, not worth "hardening" without testing.
10. **`DO NOT ADD /dev/bus/pci passthrough — breaks ROCm`** (from 201.conf). Preserved original guidance; assume it is true.
### 6.3 Tier 3 — breaks the host
11. **The APT configuration.** See §8. This is the single most likely way to break this machine during routine maintenance.
12. **The kernel pin.** `apt-mark hold pve-kernel-6.1.10-1-pve` + `/etc/kernel/proxmox-boot-pin`. Note the **6.14 meta-package is *not* held** and its candidate is `6.14.11-9~bpo12+1`, so `apt upgrade` will happily install newer 6.14 kernels — harmless while the pin holds, but it means `/boot` keeps growing and the GRUB default is the only thing standing between you and a 6.14 boot.
### 6.4 Safe to leave alone (documented as inert, not as junk)
| Item | Status |
|---|---|
| `/etc/modules-load.d/amdgpu.conf` | **[OBS]** Ineffective (deny-listed by kmod). Harmless. |
| `/etc/modules-load.d/vfio.conf`, `vfio_pci` loaded | **[OBS]** Zero devices bound. VFIO residue. |
| `video=efifb:off` | VFIO residue; part of validated cmdline. |
| `/usr/share/kvm/mi50.rom` | **[OBS] 0 bytes** — a failed VBIOS dump from the abandoned passthrough attempt. |
| `/opt/rocm` bind mount in 201/202.conf | No-ops (`optional`, host path absent). |
| `mi50-perf.service.bak` | Historical (`--setperflevel high`). |
| `/usr/local/bin/gpuup`, `gpu-check.sh` | Target **CT201**, not CT202. Do not run them expecting them to affect the live backend. |
| `/root/install.run`, `/root/README` | Synology Active Backup agent — unrelated to the GPU. |
| `/root/s` | A saved `ps aux` dump from 2025-11-08. |
| CT201 `cortex-gpu` | Stopped, `onboot: 0`. Keep as reference config. |
---
## 7. APT hazards (host)
**[OBS]** The host's repository set is internally inconsistent:
```
/etc/apt/sources.list deb … debian BOOKWORM main contrib non-free
deb … debian BOOKWORM-updates …
deb … debian BOOKWORM-backports …
/etc/apt/sources.list.d/debian.sources Suites: TRIXIE trixie-updates (+ trixie-security)
/etc/apt/sources.list.d/pve-no-subscription deb … /debian/pve BOOKWORM pve-no-subscription
/etc/apt/sources.list.d/rocm.list deb … repo.radeon.com/rocm/apt/6.3 JAMMY main
/etc/apt/sources.list.d/docker.list deb … docker … trixie stable
/etc/apt/sources.list.d/grafana.list deb … apt.grafana.com stable main
```
System is **trixie / PVE 9**. There are **no pins at all**: `/etc/apt/preferences` does not exist and `/etc/apt/preferences.d/` is empty. The only protections in place are the single `apt-mark hold` and the GRUB default.
Consequences, all **[OBS]** from `apt-cache policy`:
| Package | Installed | Bookworm repo offers |
|---|---|---|
| `pve-manager` | 9.0.11 | 8.4.21 |
| `proxmox-ve` | 9.0.0 | 8.4.0 |
| `proxmox-kernel-6.14` | 6.14.11-4 | **6.14.11-9~bpo12+1 (candidate)** |
| `rocm-smi` | 6.1.2-1 (trixie) | 5.2.3-2 (bookworm) |
- APT's default policy will not *downgrade* PVE 9 → 8, so `apt upgrade` is unlikely to detonate on its own. But `apt full-upgrade`, `--allow-downgrades`, or any resolver detour through those bookworm PVE packages is genuinely dangerous on a PVE 9 host.
- The **`rocm.list` (jammy) entry on a Debian trixie host is inert but wrong** — nothing from it is currently installed (`rocm-smi` came from Debian trixie main). **[INF]** Leftover from when ROCm was installed on the host. If anything ever *did* install from it, you would get Ubuntu 22.04 binaries on Debian 13.
- **Counter-intuitively, the bookworm PVE line is load-bearing:** it is the *only* archive carrying `pve-kernel-6.1.10-1-pve`. "Fixing" it to trixie makes the pinned kernel unobtainable. **Archive the `.deb` before touching that line** (§9.1).
**[OBS]** Signing keys are all present and correct: `/usr/share/keyrings/rocm.gpg` (2025-08-21), `proxmox-release-trixie.gpg`, `proxmox-release-bookworm.gpg`, `proxmox-archive-keyring.gpg`, `grafana.gpg`, `/etc/apt/keyrings/docker.gpg`. No `apt-key`-era trust problems.
---
## 8. Safe upgrade procedure
> Rule zero: **one change per reboot.** The failure modes here (BAR assignment, kfd major, hwmon index) all present as "the GPU is just gone", so batched changes are very expensive to bisect.
### 8.1 Before anything — preserve the escape hatches
```bash
# 1. Archive the pinned kernel .deb while its repo line still exists
mkdir -p /root/kernel-archive
cd /root/kernel-archive && apt-get download pve-kernel-6.1.10-1-pve
# also grab the firmware it was validated against
apt-get download pve-firmware
# 2. Snapshot every file this report names
tar czf /root/mi50-config-$(date +%F).tar.gz \
/etc/default/grub /etc/default/grub.d/ /etc/kernel/proxmox-boot-pin \
/etc/modprobe.d/ /etc/modules /etc/modules-load.d/ \
/etc/systemd/system/{kfd-fix,load-amdgpu,mi50-fan,mi50-perf,mi50-watchdog}.service \
/etc/systemd/system/mi50-watchdog.timer \
/usr/local/sbin/mi50-watchdog.sh /usr/local/bin/gpu* /root/mi50_fan_curve.py \
/etc/pve/lxc/201.conf /etc/pve/lxc/202.conf \
/etc/apt/sources.list /etc/apt/sources.list.d/
# 3. Copy it off-box (the NAS is already mounted)
cp /root/mi50-config-$(date +%F).tar.gz /mnt/nas_proxmox/
# 4. Save the current-good baseline for comparison
{ uname -r; cat /proc/cmdline; grep kfd /proc/devices;
lspci -nnks 0000:0a:00.0; rocm-smi;
for h in /sys/class/hwmon/hwmon*; do echo "$h $(cat $h/name)"; done;
} > /mnt/nas_proxmox/mi50-baseline-$(date +%F).txt
# 5. Back up CT202 (this is the one that matters)
vzdump 202 --storage <your-backup-storage> --mode snapshot
```
### 8.2 Routine security updates (low risk)
```bash
apt update
apt list --upgradable # READ THIS LIST
apt upgrade # NEVER full-upgrade / dist-upgrade on this host
```
Refuse the run and investigate if the list contains: anything named `pve-*`/`proxmox-*` **downgrading**, `pve-kernel-6.1.10-1-pve` (the hold should prevent it), or anything from `repo.radeon.com` on the *host*.
New 6.14 kernels appearing is expected and harmless while the pin holds — but confirm afterwards:
```bash
cat /etc/kernel/proxmox-boot-pin # must still say 6.1.10-1-pve
proxmox-boot-tool kernel list # "Pinned kernel: 6.1.10-1-pve"
grep -m1 "set default=" /boot/grub/grub.cfg # must name 6.1.10-1-pve
```
**[INF]** `update-grub` regenerates `grub.cfg` from `/etc/default/grub.d/proxmox-kernel-pin.cfg`, so the pin survives kernel package installs — but check it anyway, because that file is the only thing standing between you and a 6.14 boot.
### 8.3 Testing a newer kernel (the open question from §5)
This is reversible and is how you find out whether the pin is still needed. **Do it when you can reach the physical console**, not remotely.
```bash
# Confirm Above 4G Decoding is still enabled — BAR 0 must show a real 64-bit address:
lspci -vs 0000:0a:00.0 | grep -i "Region 0"
# want: Memory at 7000000000 (64-bit, prefetchable) [size=32G]
# NOT: [virtual] or [disabled] or size mismatch
# One-shot boot into 6.14 — pin is NOT changed:
proxmox-boot-tool kernel pin 6.14.11-4-pve --next-boot
reboot
```
After it comes back, in this order:
```bash
uname -r # 6.14.11-4-pve
dmesg | grep "0000:0a:00.0.*BAR 0" # must NOT say "can't assign"
lsmod | grep amdgpu # loaded?
dmesg | grep -E "amdgpu.*(Fatal|VRAM:)|kfd kfd"
grep kfd /proc/devices # <<< IS THE MAJOR STILL 510?
for h in /sys/class/hwmon/hwmon*; do echo "$h $(cat $h/name)"; done # is nct6775 still hwmon3?
rocm-smi
pct exec 202 -- /opt/rocm/bin/rocminfo | grep gfx906
pct exec 202 -- curl -s http://localhost:8080/health
```
**If the kfd major is not 510**, that alone breaks CT202 and is *fixable* — update `kfd-fix.service`, `gpuup`, and `lxc.cgroup2.devices.allow` in 201.conf/202.conf. Do not mistake it for "6.14 doesn't support the card".
**If anything fails:** `reboot` — the `--next-boot` pin is one-shot and you land back on 6.1.10. If it fails to boot at all, use §9.1.
**Only if every check passes** should you make it permanent:
```bash
proxmox-boot-tool kernel pin 6.14.11-4-pve
# and run it under real inference load for a few days before unholding the 6.1.10 package
```
Keep `pve-kernel-6.1.10-1-pve` installed and held regardless. Disk is cheap; that kernel is not re-obtainable if the bookworm repo line is ever cleaned up.
### 8.4 Repository hygiene (do this deliberately, on its own)
**[INF]** Recommended, in this order, after §8.1 step 1:
1. Archive the 6.1.10 `.deb` (done above). **Do not skip.**
2. Comment out the three **bookworm** lines in `/etc/apt/sources.list` (trixie is already covered by `debian.sources`).
3. Repoint `pve-no-subscription.list` from `bookworm` to `trixie`.
4. Comment out `rocm.list` on the **host** (host ROCm is not installed; CT202 has its own).
5. `apt update && apt list --upgradable` — expect the list to *shrink*. Do not `apt upgrade` in the same sitting.
This changes no running software; it removes the ability to accidentally pull PVE 8 or Ubuntu packages onto a PVE 9 Debian host.
### 8.5 ROCm / runtime upgrades (inside CT202 only)
Never touch ROCm on the host. Inside CT202:
- `vzdump 202` first — this is the cheapest full rollback you have.
- gfx906 is deprecated upstream; **there is no reason to move past ROCm 6.3** unless a specific image requires it.
- To change the model or llama.cpp version, edit `/opt/models/docker-compose.yml` and `docker compose up -d`. Pin image tags; never use `:latest`.
- **[INF]** Before any `apt upgrade` inside CT202, be aware `amdgpu-dkms` is installed. It is currently inert, but a DKMS build triggered against the host's 6.1.10 headers in a privileged container is an unnecessary risk. Consider `apt-mark hold amdgpu-dkms amdgpu-core` in CT202 — currently **no holds are set there**.
---
## 9. Recovery / rollback
### 9.1 GPU gone after a reboot
At the GRUB menu: **Advanced options for Proxmox VE → Linux 6.1.10-1-pve**. All four kernels have entries.
Then, from a shell:
```bash
proxmox-boot-tool kernel pin 6.1.10-1-pve
update-grub
grep -m1 "set default=" /boot/grub/grub.cfg # verify it names 6.1.10-1-pve
```
If the kernel package was somehow removed:
```bash
dpkg -i /root/kernel-archive/pve-kernel-6.1.10-1-pve_6.1.10-1_amd64.deb
apt-mark hold pve-kernel-6.1.10-1-pve
update-grub
```
### 9.2 Right kernel, still no GPU — diagnose in this order
```bash
# 1. BAR assigned? (if not -> BIOS: Above 4G Decoding)
dmesg | grep "0000:0a:00.0.*BAR 0"
lspci -vs 0000:0a:00.0 | grep -i "Region 0"
# 2. Driver bound?
lspci -nnks 0000:0a:00.0 # want "Kernel driver in use: amdgpu"
lsmod | grep amdgpu
systemctl status load-amdgpu.service
modprobe amdgpu ; dmesg | tail -40
# 3. Device nodes + the major number
ls -l /dev/kfd /dev/dri/by-path/
grep kfd /proc/devices # compare to the 510 hardcoded in 3 places
# 4. Container side
pct exec 202 -- ls -l /dev/kfd /dev/dri
pct exec 202 -- /opt/rocm/bin/rocminfo | grep gfx906
# 5. Backend
pct exec 202 -- docker ps -a
pct exec 202 -- docker logs --tail 50 lyra-brain
pct exec 202 -- curl -s http://localhost:8080/health
```
### 9.3 Manual bring-up (host side)
```bash
systemctl start kfd-fix.service # ensure the node exists
systemctl start load-amdgpu.service # or: modprobe amdgpu
systemctl start mi50-fan.service # DO NOT leave this stopped under load
rocm-smi
pct restart 202 # re-establish the bind mounts if needed
```
`/usr/local/bin/gpuup` does a similar sequence **but targets CT201** — read it before running it.
### 9.4 Backend / model rollback
```bash
pct exec 202 -- docker compose -f /opt/models/docker-compose.yml down
pct exec 202 -- docker compose -f /opt/models/docker-compose.yml up -d
pct exec 202 -- docker images # older gfx906 image tags are your rollback
```
Whole-container rollback: restore the `vzdump` of CT202. **[INF]** Consider `docker save`-ing `mixa3607/llama.cpp-gfx906:full-rocm-6.3.3` to the NAS — it is a third-party image for deprecated hardware and may not be re-pullable indefinitely.
### 9.5 Thermal emergency
```bash
systemctl status mi50-fan.service
journalctl -u mi50-fan.service -n 50
rocm-smi --showtemp --showfan --showpower
# force fans to max, bypassing the script:
for p in 1 2 3; do echo 1 > /sys/class/hwmon/hwmon3/pwm${p}_enable; \
echo 255 > /sys/class/hwmon/hwmon3/pwm${p}; done
# stop the load:
pct exec 202 -- docker stop lyra-brain
```
**Verify `hwmon3` is still `nct6798` before trusting those paths** (§12).
---
## 10. Open questions / not determined
| # | Question | Status |
|---|---|---|
| 1 | **Is the 6.1.10 pin actually still necessary?** | **[UNK] — the important one.** §5 shows the failures predate the BIOS fix and that 6.14 was never retested with amdgpu unblacklisted. Requires the reboot test in §8.3. |
| 2 | What exactly changed in the BIOS on 2025-10-18? | **[INF]** Above 4G Decoding, from BAR-assignment behaviour. **[UNK]** as fact — not readable from the OS. **Worth photographing the current UEFI settings while the machine is known-good.** |
| 3 | Does the `kfd` major stay 510 on 6.14? | **[UNK]** Dynamic allocation; hardcoded in 4 files. Most likely second-order breakage. §8.3 checks it. |
| 4 | Is `mi50-perf.service` doing anything? | **[UNK]** It fails at boot (runs before amdgpu) yet perf level reads `auto`. Either a later manual run, or `auto` is the driver default and the unit is redundant. Harmless either way. |
| 5 | Why was `--setperflevel high` changed to `auto` (2026-06-25)? | **[INF]** Idle power/heat — the `.bak` and the unit description ("idle down when not in use") both point that way. Not documented anywhere. |
| 6 | Was the `blacklist` + late-modprobe pattern chosen for a specific failure, or arrived at by trial? | **[UNK]** History shows repeated `modprobe amdgpu` / `modprobe -r amdgpu` / PCI unbind experiments around 2025-10-20, and `load-amdgpu.service` was created much later (2025-12-07). **[INF]** Trial and error, then codified. Treat the result as validated regardless of provenance. |
| 7 | Why is `proxmox-kernel-6.14.8-2` in `/boot` and GRUB but not in `proxmox-boot-tool kernel list`? | **[UNK]** Cosmetic. Do not "clean it up" while investigating anything else. |
| 8 | Did the host ever run ROCm, and why was it removed? | **[INF]** Yes — shell history calls `/opt/rocm/bin/rocminfo` on the host; `/opt/rocm` is now absent, and `rocm.list` + the LXC `/opt/rocm` bind mounts remain as fossils. **[UNK]** why it was removed. |
| 9 | Are 6.1.10-era CVEs a concern? | **[OBS]** The running kernel was built **2023-02-07** and receives no updates. **[UNK]** risk tolerance — the host is on a private LAN, but this is a real, permanent trade-off of the pin, and it is the strongest argument for eventually resolving Q1. |
| 10 | Is `hwmon3` stable across reboots on 6.1.10? | **[INF]** Yes in practice — the fan service has been running for 5 days and across many boots. **[UNK]** across a kernel change. |
| 11 | Long-term availability of `mixa3607/llama.cpp-gfx906` and `nalanzeyu/vllm-gfx906`. | **[UNK]** Third-party images for deprecated hardware. See §9.4. |
---
## 11. Quick verification commands
Run after **any** change. Everything below is read-only.
```bash
### Host — identity and pin
uname -r # 6.1.10-1-pve
cat /proc/cmdline # quiet amd_iommu=on iommu=pt pci=realloc video=efifb:off
cat /etc/kernel/proxmox-boot-pin # 6.1.10-1-pve
apt-mark showhold # pve-kernel-6.1.10-1-pve
### Host — card
lspci -nnks 0000:0a:00.0 # [1002:66a1], driver in use: amdgpu
lspci -vs 0000:0a:00.0 | grep -i "Region 0" # 7000000000 64-bit prefetchable, 32G
cat /sys/module/amdgpu/parameters/noretry # 0
grep kfd /proc/devices # 510 kfd
ls -l /dev/kfd /dev/dri/by-path/ # kfd 510:0 ; pci-0000:0a:00.0-render -> renderD129
rocm-smi
### Host — thermal chain (CHECK THIS AFTER EVERY KERNEL CHANGE)
cat /sys/class/hwmon/hwmon3/name # MUST be nct6798
systemctl is-active mi50-fan.service # active
systemctl is-active mi50-watchdog.timer # active
journalctl -u mi50-fan.service -n 5 # should show "Temp: NN.N °C → PWM NNN"
### Container + backend
pct exec 202 -- ls -l /dev/kfd
pct exec 202 -- /opt/rocm/bin/rocminfo | grep -E "gfx906|Marketing"
pct exec 202 -- docker ps
pct exec 202 -- curl -s http://localhost:8080/health # {"status":"ok"}
### End to end, from lyra-cortex (10.0.0.41)
curl -s http://10.0.0.42:8080/health
```
---
## 12. One-paragraph summary for future-you
The MI50 works because the **host** runs `amdgpu` (in-tree, kernel 6.1.10-1-pve) and hands `/dev/kfd` + `/dev/dri` to **privileged LXC CT202**, which carries ROCm 6.3 and runs llama.cpp in Docker from a **gfx906-specific community image** on port 8080. There is **no VFIO passthrough** despite leftover VFIO config. The driver is deliberately **blacklisted at boot and modprobed late** by `load-amdgpu.service`, with `kfd-fix.service` pre-creating the device node so the container's bind mount has a target; shared devtmpfs propagation delivers the real device afterwards. The card is **passively cooled** and `mi50-fan.service` is a genuine safety device. The original breakage was a **32 GiB BAR that firmware refused to assign** — fixed in the **BIOS** (Above 4G Decoding) on 2025-10-18, *not* by the kernel downgrade that was adopted six hours later and never re-tested. The kernel pin, the `apt-mark hold`, the `pci=realloc` flag and the hardcoded `kfd` major **510** are the four things most likely to bite you. The **APT sources are a mess** (bookworm + trixie + jammy on a trixie/PVE 9 host) and are the most likely way to break this box during routine maintenance — but the bookworm PVE line is also the *only* source of the pinned kernel, so archive that `.deb` before cleaning it up.
+162
View File
@@ -0,0 +1,162 @@
# How the MI50 Runs
**Host:** `proxmox` · 10.0.0.4 · card at `0000:0a:00.0`
**Companion doc:** [`MI50_MAINTENANCE.md`](../MI50_MAINTENANCE.md) — full configuration inventory, upgrade and rollback procedures.
**Rendered version:** https://claude.ai/code/artifact/7744ae19-f125-4a00-bb51-7e08d9a74767
A 2018 datacenter accelerator serving a 32B model, reached through four layers that each think they own the GPU. One idea explains why the version numbers look wrong.
| | |
|---|---|
| **Card** | MI50 · gfx906 |
| **Host kernel** | 6.1.10-1-pve |
| **ROCm (serving)** | 6.3.3 |
| **VRAM in use** | 24.2 / 32 GB |
| **Junction** | 41 °C |
*Observed read-only from the running host, 2026-09-09.*
---
## The one idea
### ROCm is split in half, and the halves version separately
ROCm isn't one piece of software. It's a **kernel half** — `amdgpu` and KFD, compiled into the kernel, owning the card and its memory — and a **userspace half**: the HSA runtime, HIP, rocBLAS. Ordinary files sitting in `/opt/rocm`.
An LXC container shares the host's kernel. So it needs no driver of its own — only the *device nodes*. It brings its own userspace half and talks straight to the host's kernel half.
That's the whole trick. A ROCm 6.3 userspace from late 2024 is driving an `amdgpu` built in February 2023, and it works because the KFD ioctl interface is backward-compatible.
```mermaid
flowchart TD
subgraph US["USERSPACE HALF — ships inside containers"]
direction TB
D["docker · lyra-brain<br/>llama.cpp + libggml-hip.so, built for gfx906<br/>ROCm 6.3.3-74 — its own copy, this is what serves"]
L["lxc ct202 · gpu-jammy<br/>Ubuntu 22.04 · privileged · shares the host kernel<br/>ROCm 6.3.0-39 — diagnostics only, not serving"]
N["/dev/kfd 510:0 — compute, queues, VRAM<br/>/dev/dri/renderD129 226:129 — buffer allocation<br/>both required"]
end
subgraph KS["KERNEL HALF — one shared driver"]
direction TB
K["host kernel 6.1.10-1-pve<br/>amdgpu · DRM 3.49.0 · KFD topology node vega20<br/>the only kernel driver anywhere in this stack"]
H["MI50 · Vega 20 · gfx906<br/>60 CU · 240 SIMD · 32 GB HBM2 · passively cooled"]
end
D -->|"devices /dev/kfd, /dev/dri"| L
L -->|"bind mount · lxc.mount.entry"| N
N -->|"ioctl · KFD ABI — THE SPLIT"| K
K -->|"PCIe · BAR0 32 GiB at 0x7000000000"| H
```
> Two ROCm userspaces sit above one kernel driver. The Docker image carries the copy that actually serves; the LXC's ROCm 6.3.0 is only ever used by `rocminfo`.
---
## Why this shape
### Passthrough lost because it needed a second kernel
The first attempts handed the whole PCI device to a VM. That means the guest needs its own `amdgpu` — and it needs the card's 32 GiB BAR mapped into guest address space. That mapping was exactly what the firmware was refusing to do.
Sharing device nodes into a container sidesteps both. One kernel, no remap.
```mermaid
flowchart TB
subgraph OLD["ABANDONED — VFIO passthrough"]
direction TB
O1["MI50 · 0000:0a:00.0"]
O2["host · vfio-pci stub"]
O3["guest kernel — a 2nd amdgpu<br/>a whole second driver to maintain"]
O4["ROCm userspace — never reached"]
O1 -->|"bind to vfio-pci"| O2
O2 -.->|"remap 32 GiB BAR — FAILED"| O3
O3 --> O4
end
subgraph NEW["CURRENT — LXC device sharing"]
direction TB
N1["MI50 · 0000:0a:00.0"]
N2["host kernel — the only amdgpu<br/>creates /dev/kfd + /dev/dri"]
N3["container · device nodes"]
N4["ROCm userspace — serving"]
N1 -->|"bind to amdgpu"| N2
N2 -->|"bind mount · no remap"| N3
N3 --> N4
end
```
> The difference is one kernel and one memory remap. The failing edge on the left is the same 32 GiB BAR assignment that the October 2025 logs show failing on bare metal — the BIOS fix, not the container switch, is what finally resolved it.
---
## The part nobody would guess
### The driver loads 48 seconds after the container that needs it
`amdgpu` is deliberately blacklisted so it does *not* autoload at PCI probe. It's modprobed by hand, late, by `load-amdgpu.service` — well after Proxmox has already started CT202.
That should break. It doesn't, because of two lines in a service file that looks like a leftover.
```mermaid
sequenceDiagram
participant S as systemd
participant K as kfd-fix.service
participant C as CT202
participant A as amdgpu
Note over S,A: T+0s — blacklist holds, amdgpu does not load
S->>K: start, After=local-fs.target
K->>C: T+2s — mknod /dev/kfd c 510 0, a placeholder
Note right of K: mount target must already exist
S->>C: T+20s — pve-guests starts CT202
C->>C: binds /dev/kfd and /dev/dri
S->>A: T+68s — load-amdgpu.service runs modprobe amdgpu
A-->>C: real device appears, same inode
Note over C,A: shared mount propagation fills it in, no restart
```
> Delete `kfd-fix.service` as dead weight and the container comes up with no `/dev/kfd` — the `optional` bind silently skips when its target is missing. This is the most deletable-looking load-bearing file on the box.
---
## The cost nobody sees
### The kernel pin caps how far ROCm can advance
> **Unsupported, working.**
> The gap between the kernel half (Feb 2023) and the userspace half (late 2024) is about two years, and AMD does not support that combination. It works only because nothing in ROCm 6.3 yet asks for a KFD ioctl this kernel lacks.
>
> So the pin isn't only a boot-stability decision. The day an image needs a newer KFD interface, inference breaks, and the fix is the kernel question — not a container change. Those two problems are the same problem.
---
## Honest read
### The compute path is textbook. The scaffolding is held together with October assumptions.
**Genuinely solid**
- **The architecture itself.** Host driver plus container device sharing is the standard, recommended way to run ROCm in containers. Not a hack.
- **Self-contained runtime.** The Docker image carries its own ROCm, so the LXC can rot without touching inference.
- **Thermal engineering.** A fan curve driven by junction temperature plus an independent watchdog. Most people run these cards bare and cook them.
- **Track record.** The journal shows three-month uninterrupted uptimes.
**Genuinely janky**
- **EOL kernel.** Pinned to a build from February 2023 that receives no security updates.
- **Booby-trapped filenames.** The load-bearing blacklist is called `blacklist-temp-amdgpu.conf`.
- **Hardcoded dynamic values.** Device major `510` in four files; `hwmon3` in the fan script. Both are allocation-order dependent.
- **Three distro codenames in APT.** bookworm, trixie and jammy sources on one trixie host.
- **Backups invert reality.** The dormant container is backed up nightly; the one actually serving has never been backed up.
The split matters because it tells you where to spend effort. Nothing in the second list is hard to fix, and none of it requires touching the part that took real work to get right.
---
## If it breaks
Four checks, in order:
| Command | What it tells you |
|---|---|
| `dmesg \| grep "0a:00.0.*BAR 0"` | "can't assign" means the BIOS lost Above 4G Decoding |
| `lspci -nnks 0000:0a:00.0` | Want "Kernel driver in use: amdgpu" |
| `grep kfd /proc/devices` | Must still be 510, or the LXC cgroup rule blocks it |
| `cat /sys/class/hwmon/hwmon3/name` | Must be nct6798, or the fans are running blind |
+14 -2
View File
@@ -4,7 +4,7 @@ Living doc. Working priorities and open threads, organized by area. Not a spec
specs live in `docs/` and `docs/superpowers/specs/`; this is the map of what's
done, what's next, and what's parked.
- **Last updated:** 2026-07-10
- **Last updated:** 2026-07-11
- **Frame (the load-bearing lens):** Lyra is the AI-with-tools (unchanged). The
**pokerlog is its own separable system-of-record** — she's a *client* of it via
tools, not its container. The logger must be correct/trustworthy first; Lyra's
@@ -104,10 +104,22 @@ in-process module sharing `lyra.db` and reaching into `lyra.memory`/`llm`.
- ⬜ **Human-editability sweep.** System-of-record must be fixable. Hand editor +
disown ✅, `/players` browser + identity queue ✅. Audit for gaps (session-level
edits, read edits, bulk fixes).
- ⬜ **Roster → hand seat/name resolution.** When a logged hand references a
*position* (CO, BTN…) that maps to a seated roster player, fill in their name +
link the observation — so "the CO 3-bet me" attaches to TAG without Brian naming
him. The hard part: hand positions ROTATE every hand while the roster tracks
fixed physical seats, so it needs seat-number + button-position tracking per hand
to map position→person (a wrong guess mislabels a villain — worse than blank).
Real feature, not a fill. (Brian's idea, 2026-07-11.) Pairs with the roster
active/seen work above.
- Shipped this stretch: scouting desk (proactive recall + nameless-villain
identity, all 6 phases), roster seat/unseat/clear, observed-hand fix + hand
editor, villain-dup fix, conversation export (+ tool events), session-scoped
notes, no-cache app-shell header.
notes, no-cache app-shell header. **2026-07-11:** guaranteed hand logging
(force + tool-visible history), showdown reads via `analyze_spot` + de-mush,
idempotent hand logging, any-seat straddle capture, hero-stack auto-fill from the
stack log, and turn de-duplication (killed the SSE-stream + blocking-fallback
double execution).
## Parked / longer-horizon
@@ -0,0 +1,501 @@
# Project cadence — BIT observes, Lyra picks
- **Date:** 2026-09-02
- **Status:** Spec — approved to build
- **Branch:** `feat/project-cadence`
- **Two repos, both worked on:** `~/break-it-down` (BIT) — Part A; `project-lyra` — Part B
- **Related:** `lyra/thoughts.py` (surfacing machinery), `docs/ROADMAP.md`
---
## The problem
Brian's long-horizon projects — the garage, the music backup, the side repos —
have no due date and no end state. The failure isn't laziness or tracking; it's
**initiation**, driven by a specific loop:
> "If I can't do it perfectly, why do it at all." → "I'll start tomorrow." → nothing.
Three things feed that loop, and all three are fixable.
### 1. The record is false, in the direction of despair
BIT records every project as last-updated **2026-03-21**. Meanwhile `project-lyra`
alone has **177 commits across 30 distinct working days** since that date, most
recently two days ago.
BIT isn't recording a dead project. It's blind to a very alive one.
This matters more than any feature. A manual tracker doesn't decay to *neutral*
when you stop feeding it — it decays to **actively demoralizing**, because absence
of records is indistinguishable from absence of work. Open BIT today and it shows
a graveyard with March on the headstone. That is "why bother" fuel, manufactured
by the tool built to fight it.
### 2. Nothing initiates
BIT is a passive store. Using it requires remembering to go to it — which is the
same broken step as remembering to start a pomodoro timer. (Note: a pomodoro
timer *was* built, in February, and has sat on an undeployed branch since. The
timer was never the missing piece.)
### 3. `/actionable` enumerates but does not pick
BIT already has a "⚡ Now — what can I do right now?" view. Today it returns
**68 tasks**. That is another wall — the exact wall it was built to knock down.
Picking requires context BIT structurally cannot have: what time it is, how long
Brian has, what he was warm on yesterday, what's gone stale, whether he's at the
desk or on his phone.
---
## The frame
Mirrors the frame already committed to for the pokerlog:
> **BIT is the system of record. Lyra is a client of it, not its container.**
**The test that decides where any piece of work goes:**
> **Does it produce or derive facts? → BIT. Does it require knowing Brian? → Lyra.**
| | Owns | Rationale |
|---|---|---|
| **BIT** | Facts and everything derived from them: projects, tasks, blockers, estimates, **time logs**, **the git observer**, **cadence health** | Standalone value. Human-editable UI. Works with Lyra dead — and the observer is what keeps that true. |
| **Lyra** | The relationship: **the pick**, nudge state, backoff, conversational logging, decomposition offers | Every one of these needs to know it's 9pm, that he has 20 minutes, that he was warm on seismo-relay yesterday. |
An earlier draft of this spec put the git observer and cadence math in Lyra. That
was wrong and violated the frame above: **the observer produces facts and knows
nothing about Brian.** Worse, it would have made BIT's data true only while Lyra
was running — reintroducing exactly the coupling the separable frame exists to
prevent. Auto-logging and cadence health also make BIT meaningfully better as a
standalone product, which is the justification for keeping it separable at all.
Same distinction as poker's *ledger* (facts) vs *relationship* (her memory of the
sessions).
**One sentence:** BIT knows what's possible. Lyra picks one, and knows why.
---
## Design principles
1. **Sessions, not completion.** The unit of progress is a logged session, never
a completion percentage. A % bar on an endless project reads 12% forever and
is a "why bother" generator. BIT's own v0.2.0 roadmap lists "Progress tracking
(% complete)" — **keep it off long-horizon projects.**
2. **Cadence health, not deadlines.** A project is `on_cadence`, `drifting`, or
`dormant`. **Dormant is a legal, non-shameful state**, not a failure.
3. **Observe, don't ask.** The record must become true without Brian maintaining
it. This is the highest-value thing in the whole design.
4. **No model call in the read/write path.** Reading projects, logging a session,
marking dormant — pure HTTP and SQL, working with the MI50 down and every API
key expired. The LLM appears **only** in the nudging and picking paths, which
are allowed to be flaky because a missed nudge costs nothing.
5. **Push has a budget; ignoring changes state.** Habituation is automatic and
cannot be willpowered. The anti-stacking mechanism is scarcity, not
notification priority flags. Two ignored nudges puts a project to sleep and
she stops asking.
6. **Additive-only to BIT's schema.** (Moot with a fresh DB, but keep the
discipline: SQLAlchemy `create_all()` adds missing tables safely; altering
existing ones is where data dies.)
---
## What already exists — do not rebuild
Substantially more than expected. Verified against the running instance at
`10.0.0.40:3002` and the cloned repo at `~/break-it-down`.
### On BIT `origin/dev` (commit `2ee75f7`, Feb 2026) — built, never deployed
```python
class TimeLog(Base):
__tablename__ = "time_logs"
task_id = Column(Integer, ForeignKey("tasks.id"), nullable=False)
minutes = Column(Integer, nullable=False)
note = Column(Text, nullable=True)
session_type = Column(String(50), default="manual") # 'pomodoro' | 'manual'
logged_at = Column(DateTime, default=datetime.utcnow)
```
- `Project.weekly_hours_goal` and `Project.total_hours_goal` — cadence targets,
already modeled. **Trap: both are named `*_hours_goal` but the source comment
says they store MINUTES.** Do not multiply by 60.
- `POST /api/tasks/{id}/time-logs`, `GET /api/tasks/{id}/time-logs`
- `GET /api/projects/{id}/time-summary` — the accumulation-evidence endpoint
- `migrate_add_time_logs.py`, `migrate_add_project_goals.py` — idempotent
`CREATE TABLE IF NOT EXISTS` migrations, hand-written
- `PomodoroWidget.jsx`, `PomodoroContext.jsx` — a working timer UI
`session_type` is a free-text `String(50)`, so adding `'git'` is **not** a schema
change.
### On BIT `origin/blockers` (commit `5da6e07`, Mar 2026) — this is what's deployed
- `task_blockers` many-to-many association table; `Task.blockers` / `Task.blocking`
- `GET|POST|DELETE /api/tasks/{id}/blockers[/{id}]`
- `GET /api/actionable` — unblocked, not-done leaf tasks across all projects
- `is_archived` on Project, Active/Archived/All tabs
### Branch divergence
Three commits total: **2 on dev** (pomodoro + a chore), **1 on blockers**. Neither
branch has both halves. `main` (Feb 17) has neither.
### Already in Lyra
- `lyra/thoughts.py` — a threaded, decaying, self-surfacing, backs-off-when-ignored
nag engine with quiet hours and a push channel. `new_thread:210`, `decay:265`,
`record_response:292`, `maybe_surface:326`, `maybe_ping:377`,
`maybe_daily_digest:429`.
- `lyra/notify.py` — ntfy push with tap-through
- `lyra/clock.py:56` — `humanize_gap` ("3 days")
- `lyra/tools.py` — tool spec + dispatch pattern
- `lyra/mind.py:168` — `build_messages`, where context notes get injected
- `lyra/web/` on `:7078` — RTO black/orange theme, matching BIT's existing look
### API facts verified against the running instance
- API is served at **`:3002/api/*` through nginx**. Port 8002 is not exposed.
- `GET /api/projects/{id}/tree` **404s** on the deployed branch. Use
`GET /api/projects/{id}/tasks`.
- The README is stale in both directions: it claims v0.1.6 while the deploy
reports v0.1.5, it documents `/tree` which doesn't exist, and it omits
blockers, `/actionable`, and `is_archived` — listing blockers as "v0.2.0
planned" when they're live. **Treat the code as truth, never the README.**
---
## Architecture
```
┌────────────────────────┐
│ git repos on host │
│ (7 of 8 BIT projects) │
└───────────┬────────────┘
│ git_observer.py — BIT's code, runs on the HOST
│ walk history, group by local day, no LLM
▼ POST /api/tasks/{id}/time-logs
┌──────────────────────────────────────────────────┐
│ BIT — facts, and everything derived from them │
│ projects · tasks · blockers · estimates │
│ time_logs · goals · git_sessions │
│ GET /api/actionable │
│ GET /api/projects/health (cadence, new) │
│ cadence health rendered on the project cards │
└────────────────────┬─────────────────────────────┘
│ HTTP
▼
┌──────────────────────────────────────────────────┐
│ lyra/bit.py — thin client. No cadence math here. │
└────────────────────┬─────────────────────────────┘
┌───────────┴───────────┐
▼ ▼
lyra/tools.py lyra/projects.py
log_work, pick_next, nudge state, ignore counts,
break_down, status backoff, quiet hours
▲ │
│ ▼
chat: "spent an hour thoughts.py surfacing
in the garage" (salience → surface →
ping → response)
```
**Data flow, one line:** git and chat write facts into BIT; BIT derives cadence;
Lyra reads BIT and decides whether, when, and how to open her mouth.
---
# Part A — work in `~/break-it-down` (BIT)
Everything here is deterministic. **No LLM appears anywhere in Part A.** All of it
makes BIT better as a standalone product, independently of Lyra.
## A0 — consolidation and deploy
There is no production data. The 8 projects / 77 tasks on `10.0.0.40` were test
builds; a snapshot lives at `~/bit-seed-snapshot/` if any of it is worth reseeding
through the existing JSON import.
1. Merge `origin/dev` and `origin/blockers` into a single branch. Three commits;
the backend touch points barely overlap (`models.py` gains `TimeLog` from one
and `task_blockers` from the other).
2. Deploy BIT **on this machine** via its existing `docker-compose.yml`. Pick
ports that don't collide with `lyra-web` (`:7078`) or `lyra-ntfy`.
3. Fresh `bit.db` — `create_all()` builds the full schema. The migration scripts
become unnecessary but stay in the repo for the old instance.
4. Verify post-merge: `/api/actionable`, `/api/tasks/{id}/time-logs`,
`/api/projects/{id}/time-summary`, and the Pomodoro widget all respond.
5. Create the real projects. `Clean up march 26` becomes actual physical projects
(garage, music backup, spare closet).
**Exit criteria:** one BIT instance on this box serving blockers *and* time logs.
## A1 — the git observer
Ships **with BIT**, in BIT's repo. Deterministic Python, no model, no knowledge of
Brian — it produces facts, which is why it lives here.
**Deployment note:** BIT stays in Docker. The observer ships in BIT's repo but
runs **on the host** (systemd timer preferred over cron — better logging and
`systemctl status`), POSTing to BIT's API like any other client.
This is not a workaround for Docker; it is correct regardless:
1. **It's a batch job, not a request handler.** It runs on a schedule and exits.
That doesn't belong in a web container's lifecycle either way.
2. **Volume-mounting repos would rot.** Every new project would require editing
`docker-compose.yml` — exactly the kind of manual upkeep step that goes stale,
which is the failure mode this whole project exists to fix. A path in a config
table costs nothing.
3. **HTTP decouples it usefully.** The observer can later run on a different
machine entirely (the Windows box, for repos that live there) with no code
changes. Putting it inside the container throws that away.
Requires `git` and Python on the host, both already present.
### Config
A repo↔project mapping: local repo path → BIT project id. Stored in BIT (a small
`observed_repos` table with a settings UI later, or config file for v1). Explicit,
never auto-discovered — auto-discovery would guess wrong and pollute the ledger.
### Session derivation
For each repo, group commits **by local calendar day** in Brian's timezone, not
UTC — an 11pm commit belongs to that evening.
For each day with commits:
- `minutes = (last_commit_ts - first_commit_ts) + LEAD_PADDING`
- `LEAD_PADDING = 10` — work precedes the first commit
- `MIN_MINUTES = 15` — a single-commit day would otherwise be 0
- `MAX_MINUTES = 240` — a 9am and an 11pm commit is not a 14-hour session
These are heuristics. They must be **config, not constants**, and documented as
estimates in both the spec and the code.
- `session_type = "git"` — never `manual` or `pomodoro`. Estimated time must never
masquerade as measured time. The field is a free-text `String(50)`, so this is
**not a schema change**.
- `note` = commit count and short SHA range, for traceability.
### Attribution
`time_logs.task_id` is `NOT NULL`, but a commit maps to a *repo* (project), not a
task. Two deterministic tiers:
1. **Conventional-commit scope match.** `feat(persona): …` → match `persona`
against task titles and tags in that project, case-insensitive substring.
Plain string matching, no model.
2. **Fallback:** a per-project `Unfiled work` task, created on demand.
Attribution is a nice-to-have; **the totals are the point**. It must never block or
fail a backfill. LLM-assisted attribution is explicitly deferred (see Non-goals).
### Idempotency — the critical correctness property
The observer runs repeatedly and must never double-log.
A `git_sessions` table in BIT: `(repo_path, local_date)` unique → `time_log_id`,
`last_sha`. A day is logged only if that key is absent. Idempotent by construction.
A re-run over a day that gained new commits **updates** the existing time log
rather than adding a second one.
### Backfill
One-shot run over history since 2026-03-21 (or repo start). `project-lyra` alone
should produce ~30 sessions. **This is the emotional payload of the entire
project** — the first time BIT is opened afterwards it says "30 days worked since
March" instead of showing a tombstone.
**Exit criteria:** BIT shows true history for every configured repo; re-running the
observer changes nothing.
## A2 — cadence health
Derived from `time_logs` + `Project.weekly_hours_goal`, both of which are BIT's.
No knowledge of Brian required, so it belongs here rather than in Lyra.
### States
- `on_cadence` — trailing-7-day minutes ≥ `weekly_hours_goal`
- `drifting` — some activity in the window, below goal
- `dormant` — no session in `DORMANT_AFTER_DAYS` (default 30), or set explicitly
- `untracked` — no goal set; fall back to days-since-last-session alone
`weekly_hours_goal` is nullable, so `untracked` is the common initial state and
must render sensibly.
**Trap:** `weekly_hours_goal` and `total_hours_goal` are named *hours* but the
source comment says they store **minutes**. Do not multiply by 60.
### Surface it
- `GET /api/projects/health` — cadence state and accumulated time per project
- **Render it on the project cards.** They currently show `Created 3/20/2026` and
nothing about activity, which is precisely how the graveyard effect happens.
Last-touched plus cadence state plus total logged time turns that wall of dead
cards into a readable status board — and this is the payoff of A1 becoming
visible to a human without Lyra involved at all.
**Exit criteria:** opening BIT shows, at a glance, which projects are alive.
---
# Part B — work in `project-lyra`
Everything here needs to know something about Brian. This is where the LLM lives,
and it is allowed to be flaky, because a missed nudge costs nothing.
## B1 — client and tools
- `lyra/bit.py` — thin HTTP client over BIT's API. **No cadence math**; BIT already
computed it. Degrades to a clear "BIT is down" message, never a stack trace into
her context and never a fabricated answer about project state.
- `lyra/tools.py` additions, following the existing spec + dispatch pattern:
- `projects_overview()` — every project with cadence health and logged time
- `project_status(name)` — detail, recent sessions, what's actionable
- `log_work(project, task, minutes, note)` — **conversational session logging**
- `pick_next(minutes_available)` — see B2
- `break_down(task_id)` — generate a subtask tree, push it through BIT's
existing JSON import
- `set_goal(project, weekly_hours)`, `set_dormant(project)`, `wake(project)`
**Conversational logging is how non-code projects get tracked in v1.** The git
observer does nothing for the garage or the music collection; "I spent an hour in
the garage" → `log_work` → a real `time_logs` row. Zero new infrastructure.
Automated observation of physical work is deferred past v1 (see Non-goals).
## B2 — the pick
The one place an LLM is genuinely required. Input: `/api/actionable`, minutes
available, recent sessions, cadence health, time of day. Output: **one task and one
sentence of why.** Not a list — a list is what BIT already does badly, at 68 items.
### The decomposition trigger
Live data shows tasks like "Multi-user authentication — est 480" and "Historical
data tracking — 360". **You cannot start an eight-hour task**, and these sit in
`/actionable` forever radiating "why bother."
Rule: `estimated_minutes > DECOMPOSE_THRESHOLD` (default 60) means decomposition
hasn't happened yet. When the best candidates are all oversized, Lyra offers to
break one down instead of proposing it — closing the anti-perfectionism loop
through `break_down`, using the JSON import that already exists for exactly this.
## B3 — surfacing
Reuse `thoughts.py` machinery; **do not put projects in the `threads` table.** A
thought is her interiority; a project is a fact about Brian's life. Conflating them
pollutes both — her dream cycle would generate the garage as a thought, and the
garage would appear in her self-narrative. Separate storage, shared
salience → surface → ping → response loop.
Lyra owns a small table keyed by BIT project id holding **nudge state only**: last
surfaced, ignore count, snooze, salience. Cadence itself is read from BIT, never
recomputed here.
**Channel priority:**
1. **Conversation (default).** Next time Brian's talking to her anyway, she raises
it: *"the music backup's been sitting three weeks — dead, or just asleep?"* Not
a notification. A person asking. This is the unfair advantage no to-do app has.
2. **Push (rare, budgeted).** Two legal uses only:
- the **receipt** — "you've been in there 20 minutes, want me to count it?"
(makes no demand, so there's nothing to blow off)
- the **backoff** — "I've raised the garage twice and you didn't bite, so I'm
putting it to sleep. Say the word and it wakes up."
No scheduled daily ping. A clock tick is the most habituation-prone trigger there
is; she speaks when something is *true*.
**Ignoring changes state:** two ignored surfaces → dormant → she stops. Dormancy
needs no BIT schema change — BIT already has per-project custom statuses.
---
## Non-goals — explicitly parked
Named to hold the line, because "actually get this functional and helpful for my
every day life" is the stated goal and feature creep is the named enemy.
- **The ambient card / desktop widget** — the right long-term interface, but a
surface on top of a thing that has to work first.
- **Generated wallpaper** — same.
- **Windows active-window reporter** — the "backwards timer" for physical and
non-git work. Deferred past v1. Note it needs no Pieces: ~30 lines reporting active
window title covers "which project am I in."
- **PiecesOS / MCP integration** — richer, but a cross-machine dependency
(PiecesOS is local to the Windows box, Lyra is on the homelab) that must be
spiked before anything rests on it. Never load-bearing on day one.
- **Phone widget / home-screen surface** — depends on iOS vs Android, unresolved.
- **Physical display on the rack** — unblocked (a spare monitor exists; drive it
with a Pi or old laptop running a kiosk browser, **not** by installing Xorg on
the Proxmox host). It's a browser pointed at a URL this design already produces,
so it changes nothing and can be added any time. Put it on the desk, not the rack.
- **Automatic blocker detection** — Brian's dependency-graph idea (paint → move
furniture → closet access → clothes + desk). Manual blockers are already built
and deployed; *auto-identifying* them is the parked part, and it's a natural
Lyra job later. **This is the strongest answer to "where do I start" for physical
projects** — worth un-parking once v1 is honest.
- **Cross-device syncing sticky notes** — a separate product. The sticky's *form*
(a small always-visible card) is worth having and needs no sync, because its
content is generated server-side. The sync engine is the whole product, and
Trilium already does it.
- **`% complete` on long-horizon projects** — actively harmful. See principle 1.
---
## Error handling
- **BIT unreachable:** every Lyra tool degrades to a clear "BIT is down" message.
Never a stack trace into her context, never a fabricated answer about project
state. Follows the `notify.py` precedent: a down dependency must not break the
loop.
- **Git observer failure on one repo:** log and continue. One bad repo must not
abort a backfill.
- **Duplicate protection:** the `git_sessions` unique key is the guard. A
crashed mid-backfill run resumes cleanly.
- **Clock skew / commits with future dates:** clamp to today; never create a
session in the future.
- **Attribution failure:** always falls back to `Unfiled work`. Never raises.
---
## Testing
Split by repo, matching each one's existing conventions.
### In BIT (`pytest`, backend)
- **Git observer:** fixture repo built in a tmpdir with controlled commit
timestamps. Assert day grouping across timezone boundaries, MIN/MAX/padding
clamps, conventional-commit scope attribution, `Unfiled work` fallback, and —
most importantly — **that a second run produces zero new time logs**.
- **Cadence functions:** pure, table-driven. Boundary cases: no goal set, goal met
exactly, dormancy threshold ±1 day, zero sessions ever, and the
hours-named-but-minutes-valued goal fields.
- **`/api/projects/health`:** shape and states against a seeded DB.
### In `project-lyra` (`pytest`, existing `tests/`)
- **`lyra/bit.py`:** against a stub server. Include a 404 case so a BIT API change
can't silently break it, and assert graceful degradation when BIT is down.
- **Nudge state:** ignore counts, backoff to dormant, quiet hours, push budget.
Pure and deterministic — no model.
- **The pick and the surfacing copy:** replay eval, parameterized by
`EVAL_BACKEND`/`EVAL_MODEL`, matching the persona eval pattern. Assert it
returns *one* task, and that it offers decomposition when candidates are
oversized.
## Open questions
1. **Ports for BIT on this box.** `:3002/:8002` are free here; confirm at deploy.
2. **Which of the 77 snapshotted tasks are worth reseeding**, versus starting the
real projects clean. Brian's call during A0.
3. **Whether `weekly_hours_goal` gets set per project at all in v1**, or whether
`untracked` + days-since-last-session is enough to start. Leaning: ship without
goals, add them once real session data shows what a realistic cadence is.
+207 -80
View File
@@ -10,11 +10,68 @@ deliberate) and hands back a ready message list + the active mode. Then:
"""
from __future__ import annotations
from lyra import config, llm, logbus, memory, mind, modes, summary
import threading
import time
from lyra import config, llm, logbus, memory, mind, modes, poker_prompts, summary
from lyra import tools as toolkit
from lyra.llm import Backend
MAX_TOOL_ROUNDS = 5 # cap tool-call iterations per turn
# --- turn de-duplication --------------------------------------------------
# The web UI hits TWO endpoints for one message: it POSTs the SSE stream, and if
# nothing streams to the browser (a dropped connection — most often because Brian
# locks his phone to go play the hand) it falls back to the blocking endpoint. But
# the server-side stream runs to completion regardless, so BOTH turns execute —
# double-persisting the message and double-logging the hand. This guard makes a turn
# idempotent: the first request owns it; a duplicate reuses the owner's result
# instead of running a second full turn.
#
# The UI stamps each send with a unique turn_id and passes the SAME id on the stream
# AND the fallback, so we dedupe on that — bulletproof no matter how long he's away
# (a genuine new message gets a fresh id, so nothing legit is ever swallowed). Requests
# with no id fall back to a short (session, message) window for near-simultaneous dupes.
_TURN_TTL_ID = 3600.0 # id-keyed: unique per send, so keep it long for fire-and-forget
_TURN_TTL_MSG = 20.0 # (session, msg) keyed: short — only near-simultaneous dupes
_turn_lock = threading.Lock()
_turns: dict[tuple, dict] = {} # key -> {event, reply, ts, ttl}
def _turn_key(session_id: str, user_msg: str, turn_id: str | None):
if turn_id:
return ("tid", turn_id), _TURN_TTL_ID
return (session_id, (user_msg or "").strip()), _TURN_TTL_MSG
def _claim_turn(session_id: str, user_msg: str, turn_id: str | None = None):
"""(is_owner, rec). Owner executes the turn then calls _finish_turn; a non-owner
(a duplicate of the same send) waits on rec['event'] and reuses rec['reply']."""
key, ttl = _turn_key(session_id, user_msg, turn_id)
now = time.monotonic()
with _turn_lock:
for k in [k for k, r in _turns.items() if now - r["ts"] > r["ttl"]]:
del _turns[k]
rec = _turns.get(key)
if rec is not None:
return False, rec
rec = {"event": threading.Event(), "reply": None, "ts": now, "ttl": ttl}
_turns[key] = rec
return True, rec
def _finish_turn(rec: dict, reply: str) -> None:
rec["reply"] = reply
rec["ts"] = time.monotonic()
rec["event"].set()
_AWAIT_TIMEOUT = 120.0 # a duplicate waits at most this long for the owner to finish
def _await_duplicate(rec: dict) -> str:
rec["event"].wait(timeout=_AWAIT_TIMEOUT)
return rec["reply"] or _TANGLED
# Which backends get function-calling tools is config-driven (cfg.tool_backends,
# env TOOL_BACKENDS, default "cloud"). The MI50's llama.cpp server only does tools
# when launched with --jinja + a tool-capable model, else it 500s on the tools
@@ -78,6 +135,43 @@ def _mind_loop(messages, backend: Backend, model: str | None, tool_specs,
return reply, tools_run
_FORCE_LOG = (
"You have not logged Brian's hand yet — and a hand must ALWAYS be recorded, no exceptions. "
"Call record_hand now: pass his ENTIRE hand description as one `shorthand` string."
)
def _ensure_hand_logged(messages, user_msg: str, msg_type: str | None, tools_run: list,
backend: Backend, model: str | None, ctx: dict, session_id: str) -> list:
"""Guarantee the ledger. If this turn was Brian's OWN hand and the model didn't log it,
force the record_hand call — the log can't be left to the model's discretion, because
mid-session the history few-shot-conditions it to skip logging (see mind._history_with_tools;
even a maximal 'LOG FIRST' prompt scored 0/5 under a polluted history). Guarded to hero
hands so an observed hand is never force-logged as his. Returns forced tool names."""
if msg_type != "HAND" or backend not in config.load().tool_backends:
return []
if any(t in ("record_hand", "log_hand") for t in tools_run):
return []
if not poker_prompts.looks_like_hero_hand(user_msg):
return []
try:
_, tcs = llm.chat_call(
messages + [{"role": "system", "content": _FORCE_LOG}],
backend=backend, model=model, tools=toolkit.specs(["record_hand"]),
tool_choice={"type": "function", "function": {"name": "record_hand"}},
)
except Exception as exc:
logbus.log("error", "forced hand-log failed", session=session_id, error=str(exc)[:160])
return []
forced = []
for tc in (tcs or []):
result = toolkit.dispatch(tc["name"], tc["arguments"], ctx)
memory.add_tool_event(session_id, tc["name"], tc["arguments"], result)
logbus.log("info", "forced hand log", session=session_id, tool=tc["name"], result=result[:80])
forced.append(tc["name"])
return forced
def _voice_pass(messages, draft: str, backend: Backend, model: str | None) -> str:
"""Mouth: re-render the mind's draft in her voice. Falls back to the draft on failure."""
try:
@@ -89,36 +183,48 @@ def _voice_pass(messages, draft: str, backend: Backend, model: str | None) -> st
def respond(session_id: str, user_msg: str, backend: Backend = "cloud",
model_override: str | None = None) -> str:
model_override: str | None = None, turn_id: str | None = None) -> str:
"""Produce Lyra's reply to a single user message and persist the exchange."""
cfg = config.load()
model = _resolve_model(backend, model_override, cfg)
logbus.log("info", "chat request", session=session_id, backend=backend,
model=model, embed=cfg.embed_backend)
turn = mind.assemble(session_id, user_msg, backend, model)
messages = turn.messages
tool_specs = toolkit.specs(turn.mode.tools) if backend in cfg.tool_backends else None
ctx = {"session_id": session_id, "backend": backend}
# A duplicate of the same send (the UI's stream + blocking fallback) reuses the
# owner's result instead of running a second full turn.
is_owner, rec = _claim_turn(session_id, user_msg, turn_id)
if not is_owner:
logbus.log("info", "duplicate turn deduped", session=session_id, path="respond")
return _await_duplicate(rec)
# Persist the user turn before the tool loop so its timestamp precedes any
# tool events fired mid-turn (keeps the transcript export in true order).
memory.remember(session_id, "user", user_msg)
reply, _ = _mind_loop(messages, backend, model, tool_specs, ctx, session_id)
mouth = _mouth_target(cfg, backend, model)
if mouth and reply:
reply = _voice_pass(messages, reply, *mouth)
if not reply:
reply = _TANGLED
logbus.log("info", "reply", session=session_id, chars=len(reply), voiced=bool(mouth))
reply = _TANGLED
try:
turn = mind.assemble(session_id, user_msg, backend, model)
messages = turn.messages
tool_specs = toolkit.specs(turn.mode.tools) if backend in cfg.tool_backends else None
ctx = {"session_id": session_id, "backend": backend}
memory.remember(session_id, "assistant", reply)
summary.maybe_summarize_async(session_id) # compact once enough new turns pile up
return reply
# Persist the user turn before the tool loop so its timestamp precedes any
# tool events fired mid-turn (keeps the transcript export in true order).
memory.remember(session_id, "user", user_msg)
reply, tools_run = _mind_loop(messages, backend, model, tool_specs, ctx, session_id)
_ensure_hand_logged(messages, user_msg, turn.msg_type, tools_run, backend, model, ctx, session_id)
mouth = _mouth_target(cfg, backend, model)
if mouth and reply:
reply = _voice_pass(messages, reply, *mouth)
if not reply:
reply = _TANGLED
logbus.log("info", "reply", session=session_id, chars=len(reply), voiced=bool(mouth))
memory.remember(session_id, "assistant", reply)
summary.maybe_summarize_async(session_id) # compact once enough new turns pile up
return reply
finally:
_finish_turn(rec, reply)
def respond_stream(session_id: str, user_msg: str, backend: Backend = "cloud",
model_override: str | None = None):
model_override: str | None = None, turn_id: str | None = None):
"""Streaming generator version of `respond`. Yields ("delta", text), ("tool", name),
and a final ("done", reply). Same side effects as `respond`."""
cfg = config.load()
@@ -126,66 +232,87 @@ def respond_stream(session_id: str, user_msg: str, backend: Backend = "cloud",
logbus.log("info", "chat request (stream)", session=session_id, backend=backend,
model=model, embed=cfg.embed_backend)
turn = mind.assemble(session_id, user_msg, backend, model)
messages = turn.messages
tool_specs = toolkit.specs(turn.mode.tools) if backend in cfg.tool_backends else None
ctx = {"session_id": session_id, "backend": backend}
mouth = _mouth_target(cfg, backend, model)
# A duplicate of the same send (this stream + the UI's blocking fallback) reuses
# the owner's result instead of running a second full turn.
is_owner, rec = _claim_turn(session_id, user_msg, turn_id)
if not is_owner:
logbus.log("info", "duplicate turn deduped", session=session_id, path="stream")
reply = _await_duplicate(rec)
yield ("delta", reply)
yield ("done", reply)
return
# Persist the user turn up front (see respond): keeps tool events, which fire
# mid-turn, chronologically after the user message in the exported transcript.
memory.remember(session_id, "user", user_msg)
reply = _TANGLED
try:
turn = mind.assemble(session_id, user_msg, backend, model)
messages = turn.messages
tool_specs = toolkit.specs(turn.mode.tools) if backend in cfg.tool_backends else None
ctx = {"session_id": session_id, "backend": backend}
mouth = _mouth_target(cfg, backend, model)
if mouth is None:
# No separate voice: stream the mind directly (the original path, unchanged).
parts: list[str] = []
for _ in range(MAX_TOOL_ROUNDS):
assistant_msg = None
tool_calls = None
for ev, payload in llm.chat_call_stream(
messages, backend=backend, model=model, tools=tool_specs
):
if ev == "delta":
parts.append(payload)
yield ("delta", payload)
elif ev == "message":
assistant_msg = payload
elif ev == "tool_calls":
tool_calls = payload
if not tool_calls:
break
messages.append(assistant_msg)
for tc in tool_calls:
result = toolkit.dispatch(tc["name"], tc["arguments"], ctx)
memory.add_tool_event(session_id, tc["name"], tc["arguments"], result)
logbus.log("info", "tool call", session=session_id, tool=tc["name"], result=result[:80])
messages.append({"role": "tool", "tool_call_id": tc["id"], "content": result})
_maybe_switch_mode(session_id, tc["name"])
yield ("tool", tc["name"])
reply = "".join(parts)
if not reply:
reply = _TANGLED
yield ("delta", reply)
else:
# Mind decides + runs tools (non-streamed); mouth re-voices, streamed.
draft, tools_run = _mind_loop(messages, backend, model, tool_specs, ctx, session_id)
for name in tools_run:
yield ("tool", name)
parts = []
try:
for ev, payload in llm.chat_call_stream(
mind.voice_messages(messages, draft), backend=mouth[0], model=mouth[1], tools=None
):
if ev == "delta":
parts.append(payload)
yield ("delta", payload)
except Exception as exc:
logbus.log("error", "voice stream failed", error=str(exc)[:160])
reply = "".join(parts).strip() or draft or _TANGLED
if not parts:
yield ("delta", reply)
# Persist the user turn up front (see respond): keeps tool events, which fire
# mid-turn, chronologically after the user message in the exported transcript.
memory.remember(session_id, "user", user_msg)
logbus.log("info", "reply", session=session_id, chars=len(reply), voiced=bool(mouth))
memory.remember(session_id, "assistant", reply)
summary.maybe_summarize_async(session_id)
yield ("done", reply)
if mouth is None:
# No separate voice: stream the mind directly (the original path, unchanged).
parts: list[str] = []
tools_run: list[str] = []
for _ in range(MAX_TOOL_ROUNDS):
assistant_msg = None
tool_calls = None
for ev, payload in llm.chat_call_stream(
messages, backend=backend, model=model, tools=tool_specs
):
if ev == "delta":
parts.append(payload)
yield ("delta", payload)
elif ev == "message":
assistant_msg = payload
elif ev == "tool_calls":
tool_calls = payload
if not tool_calls:
break
messages.append(assistant_msg)
for tc in tool_calls:
result = toolkit.dispatch(tc["name"], tc["arguments"], ctx)
memory.add_tool_event(session_id, tc["name"], tc["arguments"], result)
logbus.log("info", "tool call", session=session_id, tool=tc["name"], result=result[:80])
messages.append({"role": "tool", "tool_call_id": tc["id"], "content": result})
_maybe_switch_mode(session_id, tc["name"])
tools_run.append(tc["name"])
yield ("tool", tc["name"])
for name in _ensure_hand_logged(messages, user_msg, turn.msg_type, tools_run,
backend, model, ctx, session_id):
yield ("tool", name)
reply = "".join(parts)
if not reply:
reply = _TANGLED
yield ("delta", reply)
else:
# Mind decides + runs tools (non-streamed); mouth re-voices, streamed.
draft, tools_run = _mind_loop(messages, backend, model, tool_specs, ctx, session_id)
tools_run += _ensure_hand_logged(messages, user_msg, turn.msg_type, tools_run,
backend, model, ctx, session_id)
for name in tools_run:
yield ("tool", name)
parts = []
try:
for ev, payload in llm.chat_call_stream(
mind.voice_messages(messages, draft), backend=mouth[0], model=mouth[1], tools=None
):
if ev == "delta":
parts.append(payload)
yield ("delta", payload)
except Exception as exc:
logbus.log("error", "voice stream failed", error=str(exc)[:160])
reply = "".join(parts).strip() or draft or _TANGLED
if not parts:
yield ("delta", reply)
logbus.log("info", "reply", session=session_id, chars=len(reply), voiced=bool(mouth))
memory.remember(session_id, "assistant", reply)
summary.maybe_summarize_async(session_id)
yield ("done", reply)
finally:
_finish_turn(rec, reply)
+3 -1
View File
@@ -114,7 +114,7 @@ def complete_with_fallback(messages: list[Message], backend: Backend, model: str
def chat_call(
messages: list, backend: Backend = "cloud", model: str | None = None,
tools: list | None = None,
tools: list | None = None, tool_choice: str | dict | None = None,
) -> tuple[dict, list | None]:
"""One chat turn that may request tool calls (OpenAI-style backends only).
@@ -136,6 +136,8 @@ def chat_call(
kwargs: dict = {"model": mdl, "messages": messages}
if tools:
kwargs["tools"] = tools
if tool_choice: # e.g. force a specific tool: {"type":"function","function":{"name":...}}
kwargs["tool_choice"] = tool_choice
logbus.log("info", "llm call", kind="chat", backend=backend, model=mdl, tok=_approx_tok(messages))
t0 = time.monotonic()
msg = client.chat.completions.create(**kwargs).choices[0].message
+41 -3
View File
@@ -138,6 +138,33 @@ def _persona_block(user_msg: str, mode: modes.Mode | None, moment: dict | None)
return "\n\n".join(p for p in parts if p)
def _tool_mark(e: dict) -> str:
"""Compact one-line receipt of a past tool call for the history marker."""
res = (e.get("result") or "").strip().replace("\n", " ")
return f"{e['tool']} → {res[:60]}" if res else str(e["tool"])
def _history_with_tools(session_id: str, recent: list) -> list[Message]:
"""Recent turns, full fidelity — but each assistant turn is prefixed with the tools
it actually ran that turn (record_hand → Hand #62, …). `memory.recent()` stores only
the final reply text, so without this the model's own context reads as a run of
'hand → narration' with the logging invisible — which few-shot-conditions it, mid
conversation, to stop calling tools (proven: clean history logs 4/4, this stripped
history 0/4). Showing the calls keeps the demonstrated pattern honest."""
events = memory.tool_events(session_id) if recent else []
msgs: list[Message] = []
prev_at = recent[0].created_at if recent else ""
for ex in recent:
content = ex.content
if ex.role == "assistant" and events:
win = [e for e in events if prev_at < (e.get("created_at") or "") <= ex.created_at]
if win:
content = f"⟦tools I ran this turn: {'; '.join(_tool_mark(e) for e in win)}⟧\n{content}"
msgs.append({"role": ex.role, "content": content})
prev_at = ex.created_at
return msgs
def build_messages(session_id: str, user_msg: str,
mode: modes.Mode | None = None, moment: dict | None = None) -> list[Message]:
"""Assemble the full, tiered message list for one turn."""
@@ -232,9 +259,11 @@ def build_messages(session_id: str, user_msg: str,
if recalled:
messages.append(_detail_note(recalled))
# Tier 3: current session, full fidelity.
for ex in recent:
messages.append({"role": ex.role, "content": ex.content})
# Tier 3: current session, full fidelity — with each assistant turn's tool calls
# made VISIBLE (see _history_with_tools: without this, history reads as
# "hand → narration" with the logging invisible, and the model few-shot-learns
# to stop calling tools mid-session).
messages.extend(_history_with_tools(session_id, recent))
messages.append({"role": "user", "content": user_msg})
@@ -333,6 +362,7 @@ class TurnContext:
mode: modes.Mode | None = None
moment: dict = field(default_factory=dict) # perceive fills this in
register: str | None = None # route's per-turn register nudge
msg_type: str | None = None # poker-mode message class (compose fills it)
messages: list[Message] = field(default_factory=list)
@@ -378,6 +408,14 @@ def _route(ctx: TurnContext) -> TurnContext:
def _compose(ctx: TurnContext) -> TurnContext:
"""Assemble the tiered prompt for the voice model."""
ctx.messages = build_messages(ctx.session_id, ctx.user_msg, ctx.mode, moment=ctx.moment)
# Surface the poker message-class so chat can guarantee the ledger (force a hand log
# if the model skipped it). Cheap + pure; mirrors what build_messages classified.
if ctx.mode and ctx.mode.key == "poker_cash":
try:
handles = [r["name"] for r in poker.session_roster()]
except Exception:
handles = []
ctx.msg_type = poker_prompts.classify(ctx.user_msg, handles)
return ctx
+60 -2
View File
@@ -14,7 +14,7 @@ from __future__ import annotations
import json
import re
from datetime import datetime, timezone
from datetime import datetime, timedelta, timezone
import numpy as np
@@ -730,6 +730,14 @@ NOT apply to another — e.g. your hole "ace of spades" is a different card from
whose suit is unstated (that board ace is "Ax", not "As"). Use null/omit for non-card \
details not stated. Stay faithful to what's described — do not invent action that isn't implied.
STRADDLES: a straddle is a voluntary blind posted before the deal — always record it as a \
preflop `post` action by the straddler with its amount, at whatever seat straddled, and respect \
the action order it creates. A straddle is legal from ANY non-blind seat (UTG, UTG1, MP, LJ, HJ, \
CO, BTN — a "Mississippi"/any-seat straddle, common at the Meadows), not just UTG or the button. \
The straddler acts LAST preflop and first preflop action opens to their LEFT: a UTG straddle opens \
action at UTG+1; a BUTTON straddle opens action in the SB; a CO straddle opens on the BTN, etc. \
Keep the straddler in players[] at their real seat; never drop the straddle.
POSITIONS: resolve relative seat references ("N seats to my right/left") into real positions. \
Action moves clockwise, so a player to your RIGHT acts before you (toward the blinds/button) \
and a player to your LEFT acts after you (toward UTG). Going RIGHT from a player you pass, in \
@@ -905,13 +913,63 @@ def store_hand_history(parsed: dict, session_id: int | None = None,
return int(cur.lastrowid)
def _recent_duplicate_hand(parsed: dict, session_id: int | None, window_sec: int = 180) -> int | None:
"""Id of an identical hand (same session, hole cards, board) recorded in the last few
minutes, else None. The chat turn can execute TWICE — the SSE stream and the blocking
fallback both run server-side — which would double-log the same hand; a system-of-record
must record an event once. `IS` is NULL-safe so a boardless/cardless hand matches too."""
p = normalize_structured(parsed)
sid = _resolve(session_id) or _review_session_id()
hole = " ".join(p.get("hero_cards") or []) or None
board = " ".join(p.get("board") or []) or None
cutoff = (datetime.now(timezone.utc) - timedelta(seconds=window_sec)).isoformat()
row = _c().execute(
"SELECT id FROM poker_hands WHERE session_id = ? AND at >= ? "
"AND hole_cards IS ? AND board IS ? ORDER BY id DESC LIMIT 1",
(sid, cutoff, hole, board),
).fetchone()
return int(row["id"]) if row else None
def _fill_hero_stack(parsed: dict, session_id: int | None) -> dict:
"""Default hero's starting stack to the last logged stack (current_stack) when the hand
didn't state one — the system already knows his stack from the stack log even when he
doesn't restate it every hand. Only fills a genuinely missing value; a stack he gave in
the hand text always wins. Marks the hero player stack_inferred so it's honest about it."""
if not isinstance(parsed, dict) or parsed.get("hero_involved", True) is False:
return parsed
hero_pos = parsed.get("hero_pos")
if not hero_pos:
return parsed
players = parsed.setdefault("players", [])
hero = next((pl for pl in players if pl.get("hero") or pl.get("pos") == hero_pos), None)
if hero and hero.get("stack") not in (None, 0):
return parsed # he stated a stack — never override it
stack = current_stack(session_id)
if stack is None:
return parsed # nothing logged yet to borrow
if hero is None:
hero = {"pos": hero_pos}
players.append(hero)
hero["stack"] = stack
hero["stack_inferred"] = True
return parsed
def record_hand(shorthand: str, session_id: int | None = None, stakes: str | None = None,
tag: str | None = None, lesson: str | None = None,
backend: str | None = None) -> dict:
"""Parse shorthand -> structured hand -> store. Returns {id, parsed} (id None on parse fail)."""
"""Parse shorthand -> structured hand -> store. Returns {id, parsed} (id None on parse fail).
Idempotent: if this exact hand was just logged for the session (double turn execution),
returns the existing one instead of inserting a duplicate. Hero's stack is auto-filled
from the last stack log when he didn't restate it."""
parsed = parse_hand(shorthand, stakes=stakes, backend=backend)
if not parsed:
return {"id": None, "parsed": None}
parsed = _fill_hero_stack(parsed, session_id)
dup = _recent_duplicate_hand(parsed, session_id)
if dup is not None:
return {"id": dup, "parsed": parsed, "linked": 0, "deduped": True}
hid = store_hand_history(parsed, session_id=session_id, tag=tag, lesson=lesson)
linked = link_hand_players(hid, parsed, session_id=session_id) # enrich villain files
return {"id": hid, "parsed": parsed, "linked": linked}
+22 -8
View File
@@ -147,6 +147,15 @@ def classify(user_msg: str, roster_handles=()) -> str:
return "CHAT"
def looks_like_hero_hand(user_msg: str) -> bool:
"""True when the message is Brian's OWN hand (first-person + real card content) —
the guard for force-logging. Deliberately conservative: an observed hand (a villain
the actor, no I/me/my) returns False so we never force-log someone else's hand as his."""
msg = (user_msg or "").strip()
low = msg.lower()
return bool(_FIRST_PERSON.search(low)) and _looks_like_hand(low, msg)
# --- always-on base (poker) ----------------------------------------------
BASE = """You are copiloting Brian's LIVE cash game — at the table with him, a session open. \
@@ -186,14 +195,19 @@ it's worth it; the log is mandatory, the commentary optional. Do NOT analyze it
_F_HAND = """MESSAGE TYPE: HAND. First: was Brian IN this hand? If he only WATCHED it (no I/me/my \
holding cards — two other players), it's really observed: log the players' actions as reads / \
record it as an observed hand, and do NOT analyze it as his. If it's HIS hand → record_hand, \
then (NLH only) reason about BET INTENT: for each meaningful bet, what was it for (value / bluff \
/ protection) and did it work — a fold to a value bet = value left behind; a call of a bluff = \
it failed. Call analyze_spot for any close equity/who's-ahead spot — never eyeball. Name leaks \
plainly (owning value, missed value, sizing); give ONE real opinion. NO reflexive praise ("nice \
hand"). If a named villain is referenced, use their profile/the scouting note — don't invent a \
read. PLO/non-NLH: log and replay it, offer at most a light read, do NOT attempt NLH-style \
equity. Prose, not a listicle."""
record it as an observed hand, and do NOT analyze it as his. If it's HIS hand → record_hand first. \
Then read the hand off the RECORDED cards, not by eye: name his made hand by the street it mattered \
(flopped/turned/rivered top pair / set / quads / etc.). At a SHOWDOWN where his and the caller's \
cards are both known, call analyze_spot(hero, villain, full board) to confirm the made hands and \
who won BEFORE you comment — NEVER eyeball a finished board (it also catches impossible cards). Same \
for any close equity / who's-ahead / outs spot. (NLH only) reason about BET INTENT: for each \
meaningful bet, what was it for (value / bluff / protection) and did it work — a fold to a value bet \
= value left behind; a call of a bluff = it failed. Name leaks plainly (owning value, missed value, \
sizing) and give ONE real opinion. If there's genuinely no leak (e.g. he flopped the near-nuts and \
stacked off), SAY so — don't manufacture a takeaway. NO reflexive praise ("nice hand"), NO \
variance-evens-out / resilience / life-lesson filler, NO cross-hand pep talk. If a named villain is \
referenced, use their profile/the scouting note — don't invent a read. PLO/non-NLH: log and replay \
it, offer at most a light read, do NOT attempt NLH-style equity. Prose, not a listicle."""
_F_TABLE = """MESSAGE TYPE: TABLE — roster management. "seat the table: …" → seat_players. A table \
change ("table broke", "I got moved", "switched tables") → clear_table, then wait for the new \
+6 -2
View File
@@ -273,11 +273,13 @@ def create_app() -> FastAPI:
user_msg = _last_user_message(body.get("messages", []))
model_override = body.get("model") or None
turn_id = body.get("turnId") or None
memory.ensure_session(session_id)
if body.get("mode"):
memory.set_session_mode(session_id, body["mode"])
try:
reply = await asyncio.to_thread(chat.respond, session_id, user_msg, backend, model_override)
reply = await asyncio.to_thread(chat.respond, session_id, user_msg, backend,
model_override, turn_id)
except Exception as exc:
logbus.log("error", "chat failed", session=session_id, error=str(exc))
reply = f"[error] {exc}"
@@ -305,6 +307,7 @@ def create_app() -> FastAPI:
backend = _backend_for(body.get("backend"))
user_msg = _last_user_message(body.get("messages", []))
model_override = body.get("model") or None
turn_id = body.get("turnId") or None
memory.ensure_session(session_id)
if body.get("mode"):
memory.set_session_mode(session_id, body["mode"])
@@ -316,7 +319,8 @@ def create_app() -> FastAPI:
def produce():
try:
for event in chat.respond_stream(session_id, user_msg, backend, model_override):
for event in chat.respond_stream(session_id, user_msg, backend,
model_override, turn_id):
loop.call_soon_threadsafe(q.put_nowait, event)
except Exception as exc: # surface to the client stream, don't hang
logbus.log("error", "chat stream failed", session=session_id, error=str(exc))
+8 -1
View File
@@ -398,10 +398,17 @@
// live poker session forces the cloud backend regardless of the saved pick.
if (mode === "poker_cash") backend = "cloud";
// One id per send, carried on BOTH the stream and the blocking fallback so the
// server runs this turn exactly once even if you lock your phone and it re-fires.
const turnId = (window.crypto && crypto.randomUUID)
? crypto.randomUUID()
: String(Date.now()) + "-" + Math.random().toString(36).slice(2);
const body = {
mode: mode,
messages: history,
sessionId: currentSession
sessionId: currentSession,
turnId: turnId
};
// Only add backend if in standard mode
+109
View File
@@ -0,0 +1,109 @@
"""record_hand idempotency + straddle parse coverage.
The chat turn can execute twice — the SSE stream and the blocking fallback both run
server-side (two 'chat request' lines, 1s apart) — which double-logged the same hand
once logging became guaranteed. A system-of-record must record an event once."""
from __future__ import annotations
import importlib
import pytest
@pytest.fixture
def poker(tmp_path, monkeypatch):
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
from lyra import llm
monkeypatch.setattr(llm, "embed", lambda texts: [[0.1, 0.2, 0.3] for _ in texts])
import lyra.memory as memory
importlib.reload(memory)
import lyra.poker as poker
importlib.reload(poker)
return poker
_PARSED = {
"game": "NLH", "hero_pos": "SB", "hero_cards": ["Ah", "Kh"],
"board": ["Kd", "9d", "4c", "2s"], "players": [], "actions": [],
"result": {"hero_net": -200, "pot": 400},
}
def test_record_hand_is_idempotent_across_double_execution(poker, monkeypatch):
sid = poker.start_session(venue="Borgata", stakes="1/3", buy_in=400)
monkeypatch.setattr(poker, "parse_hand", lambda *a, **k: dict(_PARSED))
first = poker.record_hand("i have AhKh in the SB, btn straddle, ...")
second = poker.record_hand("i have AhKh in the SB, btn straddle, ...") # the duplicate turn
assert first["id"] == second["id"]
assert second.get("deduped") is True
assert len(poker.list_hands(sid)) == 1 # ledger holds ONE, not two
def test_record_hand_does_not_dedupe_a_genuinely_different_hand(poker, monkeypatch):
sid = poker.start_session(venue="Borgata", stakes="1/3", buy_in=400)
monkeypatch.setattr(poker, "parse_hand", lambda *a, **k: dict(_PARSED))
poker.record_hand("hand one")
other = dict(_PARSED, hero_cards=["Qs", "Qd"], board=["Qh", "7c", "2s"])
monkeypatch.setattr(poker, "parse_hand", lambda *a, **k: dict(other))
poker.record_hand("a different hand entirely")
assert len(poker.list_hands(sid)) == 2 # distinct hands both land
def test_dedupe_handles_boardless_hand(poker, monkeypatch):
# NULL-safe match: a preflop-only hand (no board) still dedupes.
sid = poker.start_session(venue="Borgata", buy_in=400)
preflop = {"game": "NLH", "hero_pos": "BTN", "hero_cards": ["As", "Ks"],
"board": [], "players": [], "actions": [], "result": {"hero_net": 30}}
monkeypatch.setattr(poker, "parse_hand", lambda *a, **k: dict(preflop))
a = poker.record_hand("AKs btn, i open everyone folds")
b = poker.record_hand("AKs btn, i open everyone folds")
assert a["id"] == b["id"] and len(poker.list_hands(sid)) == 1
def test_parse_prompt_records_straddles():
from lyra import poker as pk
p = pk._HAND_PARSE_PROMPT.lower()
assert "straddle" in p and "button straddle" in p
assert "acts last preflop" in p or "act last preflop" in p
# --- hero stack auto-fill from the last logged stack ----------------------
def test_hero_stack_filled_from_last_stack_log(poker, monkeypatch):
poker.start_session(venue="Meadows", stakes="1/3", buy_in=400)
poker.log_stack(275) # his last reported stack
monkeypatch.setattr(poker, "parse_hand",
lambda *a, **k: {"game": "NLH", "hero_involved": True,
"hero_pos": "CO", "hero_cards": ["As", "Ks"],
"board": ["2c"], "players": [], "actions": [],
"result": {"hero_net": 50}})
out = poker.record_hand("AKs in the CO, i raise, flop 2c...")
stored = poker.get_hand(out["id"])["structured"]
hero = next(pl for pl in stored["players"] if pl.get("hero"))
assert hero["stack"] == 275 and hero.get("stack_inferred") is True
def test_stated_stack_is_never_overridden(poker, monkeypatch):
poker.start_session(venue="Meadows", buy_in=400)
poker.log_stack(275)
monkeypatch.setattr(poker, "parse_hand",
lambda *a, **k: {"game": "NLH", "hero_involved": True,
"hero_pos": "BTN", "hero_cards": ["Qh", "Qd"],
"players": [{"pos": "BTN", "stack": 500}],
"board": [], "actions": [], "result": {}})
out = poker.record_hand("500 deep on the btn with QQ")
hero = next(pl for pl in poker.get_hand(out["id"])["structured"]["players"]
if pl.get("pos") == "BTN")
assert hero["stack"] == 500 and not hero.get("stack_inferred")
def test_observed_hand_gets_no_hero_stack(poker, monkeypatch):
poker.start_session(venue="Meadows", buy_in=400)
poker.log_stack(275)
monkeypatch.setattr(poker, "parse_hand",
lambda *a, **k: {"game": "NLH", "hero_involved": False,
"hero_pos": None, "hero_cards": [],
"players": [{"pos": "CO", "cards": ["Kx", "Kx"]}],
"board": [], "actions": [], "result": {}})
out = poker.record_hand("the CO stacked off KK vs the nit")
assert all(not pl.get("stack_inferred") for pl in poker.get_hand(out["id"])["structured"]["players"])
+97
View File
@@ -0,0 +1,97 @@
"""Reliable hand logging: hero-hand guard, tool-visible history (B), forced log (A).
Root cause these guard: mid-session, memory.history() rebuilt past turns as
'hand -> narration' with tool calls stripped, few-shot-conditioning the model to
stop logging (clean history logged 4/4, the stripped history 0/4)."""
from __future__ import annotations
from types import SimpleNamespace
from lyra import poker_prompts as pp
# --- the hero-hand guard (who gets force-logged) --------------------------
def test_looks_like_hero_hand_true_for_brians_own_hand():
assert pp.looks_like_hero_hand("im utg with 2d2s. i raise to $15, btn calls")
assert pp.looks_like_hero_hand("300eff. i call btn w AsQs, flop Qh7c2s, i bet 20 he calls")
def test_looks_like_hero_hand_false_for_observed_and_chatter():
# A villain the actor (no I/me/my) must never be force-logged as Brian's hand.
assert not pp.looks_like_hero_hand("TAG limped A4o in the SB")
assert not pp.looks_like_hero_hand("how's the table looking tonight?")
assert not pp.looks_like_hero_hand("")
# --- Fix B: tool calls made visible in reconstructed history --------------
def _ex(role, content, at):
return SimpleNamespace(role=role, content=content, created_at=at, id=hash(at))
def test_history_marks_the_assistant_turn_that_logged(monkeypatch):
from lyra import mind, memory
recent = [
_ex("user", "i have 2d2s utg, flop 2c2hKs, quads", "2026-07-10T18:00:00.000000+00:00"),
_ex("assistant", "Sick cooler.", "2026-07-10T18:00:05.000000+00:00"),
_ex("user", "how am i doing", "2026-07-10T18:01:00.000000+00:00"),
_ex("assistant", "Up a grand.", "2026-07-10T18:01:03.000000+00:00"),
]
monkeypatch.setattr(memory, "tool_events", lambda sid: [
{"tool": "record_hand", "result": "Hand #62 logged — UTG 2d2s.",
"created_at": "2026-07-10T18:00:03.000000+00:00"},
{"tool": "session_state", "result": "net +1000",
"created_at": "2026-07-10T18:01:02.000000+00:00"},
])
msgs = mind._history_with_tools("s1", recent)
# each event is attributed to the assistant turn whose window it falls in
assert "record_hand → Hand #62 logged" in msgs[1]["content"]
assert msgs[1]["content"].endswith("Sick cooler.")
assert "session_state" in msgs[3]["content"]
# user turns are untouched
assert msgs[0]["content"] == recent[0].content
def test_history_no_marker_when_no_tools(monkeypatch):
from lyra import mind, memory
monkeypatch.setattr(memory, "tool_events", lambda sid: [])
recent = [_ex("assistant", "just talking", "2026-07-10T18:00:05.000000+00:00")]
assert mind._history_with_tools("s1", recent)[0]["content"] == "just talking"
# --- Fix A: force the log when the model skipped a hero hand ---------------
def _force_setup(monkeypatch, tool_calls):
from lyra import chat
monkeypatch.setattr(chat.llm, "chat_call",
lambda *a, **k: ({"role": "assistant"}, tool_calls))
dispatched = []
monkeypatch.setattr(chat.toolkit, "dispatch",
lambda name, args, ctx=None: dispatched.append(name) or "Hand #71 logged.")
monkeypatch.setattr(chat.memory, "add_tool_event", lambda *a, **k: 1)
return chat, dispatched
def test_forces_log_on_unlogged_hero_hand(monkeypatch):
chat, dispatched = _force_setup(monkeypatch, [{"id": "1", "name": "record_hand",
"arguments": '{"shorthand":"AsQs..."}'}])
forced = chat._ensure_hand_logged([], "300eff i call btn w AsQs, i bet 20", "HAND", [],
"cloud", None, {}, "s1")
assert forced == ["record_hand"] and dispatched == ["record_hand"]
def test_does_not_force_when_already_logged(monkeypatch):
chat, dispatched = _force_setup(monkeypatch, [])
forced = chat._ensure_hand_logged([], "i have AsQs, i bet", "HAND", ["record_hand"],
"cloud", None, {}, "s1")
assert forced == [] and dispatched == []
def test_does_not_force_non_hand_or_observed(monkeypatch):
chat, dispatched = _force_setup(monkeypatch, [])
# not a HAND turn
assert chat._ensure_hand_logged([], "down to 220", "LOG", [], "cloud", None, {}, "s1") == []
# HAND-classified but observed (no first person) → never force-logged as his
assert chat._ensure_hand_logged([], "TAG shoved AKo", "HAND", [], "cloud", None, {}, "s1") == []
assert dispatched == []
+21
View File
@@ -114,3 +114,24 @@ def test_hardening_log_needs_number_or_result_word():
assert c("rebought for 300") == "LOG"
# first-person departure is Brian, not a roster op → not TABLE
assert c("I busted, heading home") != "TABLE"
# --- HAND fragment: route showdowns to the tool + no motivational mush ---
def test_hand_fragment_routes_showdowns_to_the_tool():
# A resolved showdown must be verified via analyze_spot, not eyeballed
# (the quad-kings-read-as-"kings-full" regression).
frag = pp.fragment_for("HAND")
assert "SHOWDOWN" in frag
assert "analyze_spot" in frag
assert "never eyeball" in frag.lower()
# names the hand class by street so "flopped quads" actually gets said
assert "street it mattered" in frag
def test_hand_fragment_bans_motivational_filler():
frag = pp.fragment_for("HAND")
assert "variance-evens-out" in frag
assert "life-lesson" in frag
# if there's no leak, say so instead of inventing a takeaway
assert "no leak" in frag.lower()
+93
View File
@@ -0,0 +1,93 @@
"""Turn de-duplication: the UI hits two endpoints for one message (SSE stream +
blocking fallback). Only the first should execute; the duplicate reuses its result."""
from __future__ import annotations
import pytest
from lyra import chat
@pytest.fixture(autouse=True)
def _clean_turns():
chat._turns.clear()
yield
chat._turns.clear()
def test_claim_is_owner_once_per_key():
o1, r1 = chat._claim_turn("s1", "flopped a set")
o2, r2 = chat._claim_turn("s1", "flopped a set")
assert o1 is True and o2 is False
assert r1 is r2 # the duplicate waits on the SAME record
def test_different_messages_each_own():
o1, _ = chat._claim_turn("s1", "hand A")
o2, _ = chat._claim_turn("s1", "hand B")
o3, _ = chat._claim_turn("s2", "hand A") # different session
assert o1 and o2 and o3
def test_await_returns_owner_reply():
_, rec = chat._claim_turn("s1", "msg")
chat._finish_turn(rec, "the answer")
assert chat._await_duplicate(rec) == "the answer"
def test_respond_duplicate_reuses_result_without_running_turn(monkeypatch):
# owner already ran and cached its reply
_, rec = chat._claim_turn("s1", "same hand")
chat._finish_turn(rec, "owner reply")
def boom(*a, **k):
raise AssertionError("duplicate must NOT execute the turn")
monkeypatch.setattr(chat.mind, "assemble", boom)
out = chat.respond("s1", "same hand", "cloud")
assert out == "owner reply"
def test_respond_stream_duplicate_yields_cached_reply(monkeypatch):
_, rec = chat._claim_turn("s1", "same hand")
chat._finish_turn(rec, "owner reply")
def boom(*a, **k):
raise AssertionError("duplicate must NOT execute the turn")
monkeypatch.setattr(chat.mind, "assemble", boom)
events = list(chat.respond_stream("s1", "same hand", "cloud"))
assert ("delta", "owner reply") in events
assert ("done", "owner reply") in events
def test_fresh_message_after_window_runs_again():
# a completed turn lingers only briefly; simulate expiry and confirm re-ownership
o1, rec = chat._claim_turn("s1", "later resend")
chat._finish_turn(rec, "first")
rec["ts"] -= chat._TURN_TTL_MSG + 1 # age it past the (session,msg) window
o2, _ = chat._claim_turn("s1", "later resend")
assert o1 and o2 # a genuine later resend runs fresh
# --- client turn-id keying (the fire-and-forget guarantee) ----------------
def test_same_turn_id_dedupes_regardless_of_message():
# the fallback may resend the SAME id; dedupe on the id, not the text
o1, r1 = chat._claim_turn("s1", "a hand", turn_id="tid-123")
o2, r2 = chat._claim_turn("s1", "a hand", turn_id="tid-123")
assert o1 is True and o2 is False and r1 is r2
def test_different_turn_ids_each_own():
o1, _ = chat._claim_turn("s1", "same text", turn_id="tid-1")
o2, _ = chat._claim_turn("s1", "same text", turn_id="tid-2")
assert o1 and o2 # a genuinely new send never gets swallowed
def test_turn_id_window_survives_long_after_the_msg_window():
# locked-phone case: the re-fire can arrive minutes later and must still dedupe
o1, rec = chat._claim_turn("s1", "big hand", turn_id="tid-lock")
chat._finish_turn(rec, "cached")
rec["ts"] -= chat._TURN_TTL_MSG + 60 # well past the short window, but not the id window
o2, r2 = chat._claim_turn("s1", "big hand", turn_id="tid-lock")
assert o1 and o2 is False and r2["reply"] == "cached"