Compare commits
66 Commits
05ae98abdb
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 067bc28b97 | |||
| 267b6ad7ba | |||
| c212099738 | |||
| 3573ac8d79 | |||
| af778ef327 | |||
| e631797187 | |||
| 29a4d59661 | |||
| 07153fc53d | |||
| e6134cf535 | |||
| aefb22c823 | |||
| 5da13a7321 | |||
| 9b844bc356 | |||
| 8d2d7fb576 | |||
| 3d886cdeae | |||
| 392c46d8bf | |||
| 4ce1b05fad | |||
| 2be43848a7 | |||
| d5c80f6153 | |||
| 056578ac75 | |||
| f20570fc03 | |||
| 3b3878ada1 | |||
| f2944ed402 | |||
| 9cd962625d | |||
| 8a6b11c56a | |||
| 22526d7938 | |||
| 7f23aeae17 | |||
| 71bbe07220 | |||
| a4412aa023 | |||
| 3afa75f4be | |||
| 865834a8ae | |||
| fb6b44a82e | |||
| 393ca65dee | |||
| 14480c40b2 | |||
| 8d709b9554 | |||
| 52839a9bc8 | |||
| 8ad4bc4ce0 | |||
| 8031c277a2 | |||
| 36f2aa76b3 | |||
| a2835500bc | |||
| 5c63175a3c | |||
| 86f3d2dc0a | |||
| abac42c344 | |||
| 44bb8687f7 | |||
| cb4ed10c1a | |||
| ba00530caf | |||
| 66dd880f93 | |||
| a7901a66ae | |||
| 2a73033eed | |||
| aae8204eff | |||
| d6f3516a34 | |||
| 51c2d6abb9 | |||
| 8a3c9b2701 | |||
| 17ab95dc98 | |||
| 03aceec6fa | |||
| a7af461cdb | |||
| 904eda3388 | |||
| f1f15972ac | |||
| 97afa82594 | |||
| ea30c3dd67 | |||
| 149e9a6dd5 | |||
| cf4238911e | |||
| 3dd9eb5a3e | |||
| a7966e4bab | |||
| a705e573a9 | |||
| 7372e241b4 | |||
| 10e4657f01 |
@@ -45,3 +45,9 @@ FEED_REACT_PROB=0.5 # chance a new thought reacts to a feed item
|
|||||||
# Defaults to SUMMARY_BACKEND. Set to run her reflections/thoughts on a steerable model.
|
# Defaults to SUMMARY_BACKEND. Set to run her reflections/thoughts on a steerable model.
|
||||||
INTROSPECTION_BACKEND=
|
INTROSPECTION_BACKEND=
|
||||||
INTROSPECTION_MODEL=
|
INTROSPECTION_MODEL=
|
||||||
|
PING_AUTO_SALIENCE=0.8 # a thought this salient auto-pings even without an explicit reach-out
|
||||||
|
PING_COOLDOWN_MIN=60 # min minutes between AUTO pings (explicit reach-outs bypass)
|
||||||
|
DIGEST_HOUR=18 # local hour to send her daily "what I've been thinking" digest
|
||||||
|
CHAT_DELIBERATE=true # think privately before answering substantive chat turns (false = faster, shallower)
|
||||||
|
MOUTH_BACKEND= # mind/mouth split: separate character/voice model for the final reply (empty = mind speaks)
|
||||||
|
MOUTH_MODEL=
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
# MI50 runaway watchdog (fallback layer "A")
|
||||||
|
|
||||||
|
Independent host-side backstop to Lyra's in-app dream-cycle budget (layer "C",
|
||||||
|
`lyra/dream.py`). Stops the llama.cpp backend if the MI50 is busy too long or too
|
||||||
|
hot, and pings Brian. See
|
||||||
|
`docs/superpowers/specs/2026-07-04-mi50-runaway-guards-design.md`.
|
||||||
|
|
||||||
|
## What it does
|
||||||
|
|
||||||
|
Runs on the **Proxmox host** (`10.0.0.4`) via a systemd timer, every ~2 min:
|
||||||
|
|
||||||
|
- **Duration:** if the GPU is busy (`rocm-smi` use% > 0) for **3600s continuously**,
|
||||||
|
it stops the container. Any idle read resets the streak, so a legitimate ~40-min
|
||||||
|
manual workload never trips it.
|
||||||
|
- **Temperature:** if junction ≥ **97°C** for **3 consecutive checks (~6 min)**, it
|
||||||
|
stops the container — independent of duration.
|
||||||
|
- On either trip: `pct exec 202 -- docker stop lyra-brain`, clear state, `logger` a
|
||||||
|
line, and POST to your ntfy topic.
|
||||||
|
|
||||||
|
All thresholds are `Environment=` overrides in the `.service`.
|
||||||
|
|
||||||
|
## Install (on the Proxmox host, as root)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# copy the three files up (from the repo, on lyra-cortex):
|
||||||
|
scp -i ~/.ssh/id_lyra_proxmox deploy/mi50-watchdog/mi50-watchdog.sh \
|
||||||
|
root@10.0.0.4:/usr/local/sbin/mi50-watchdog.sh
|
||||||
|
scp -i ~/.ssh/id_lyra_proxmox deploy/mi50-watchdog/mi50-watchdog.{service,timer} \
|
||||||
|
root@10.0.0.4:/etc/systemd/system/
|
||||||
|
|
||||||
|
# on the host:
|
||||||
|
chmod +x /usr/local/sbin/mi50-watchdog.sh
|
||||||
|
# set your ntfy topic (same one Lyra uses) in the service:
|
||||||
|
sed -i 's/CHANGE_ME/YOUR_NTFY_TOPIC/' /etc/systemd/system/mi50-watchdog.service
|
||||||
|
systemctl daemon-reload
|
||||||
|
systemctl enable --now mi50-watchdog.timer
|
||||||
|
```
|
||||||
|
|
||||||
|
## Verify (when the card is back and healthy)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# dry run once, watch what it decides:
|
||||||
|
NTFY_URL= /usr/local/sbin/mi50-watchdog.sh; echo "exit $?"
|
||||||
|
journalctl -t mi50-watchdog -n 20 --no-pager
|
||||||
|
|
||||||
|
# force a trip test with tiny thresholds (won't touch a healthy idle card unless busy):
|
||||||
|
MAX_BUSY_SEC=60 TEMP_KILL_C=40 TEMP_KILL_STREAK=1 /usr/local/sbin/mi50-watchdog.sh
|
||||||
|
# confirm it stopped lyra-brain + sent the ntfy, then restart the container.
|
||||||
|
|
||||||
|
systemctl list-timers mi50-watchdog.timer # confirm it's scheduled
|
||||||
|
```
|
||||||
|
|
||||||
|
**Not yet installed / live-verified** — staged here on 2026-07-04 while the card is
|
||||||
|
off and Brian is away. Install + trip-test when the MI50 is back.
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=MI50 runaway watchdog (stop the llama.cpp backend if the GPU is busy too long or too hot)
|
||||||
|
After=network-online.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=oneshot
|
||||||
|
# Fill in your ntfy topic so it can ping Brian when it trips (leave URL empty to log only).
|
||||||
|
Environment=NTFY_URL=https://ntfy.sh
|
||||||
|
Environment=NTFY_TOPIC=CHANGE_ME
|
||||||
|
# Optional overrides (defaults shown):
|
||||||
|
# Environment=MAX_BUSY_SEC=3600
|
||||||
|
# Environment=TEMP_KILL_C=97
|
||||||
|
# Environment=TEMP_KILL_STREAK=3
|
||||||
|
# Environment=CTID=202
|
||||||
|
# Environment=CONTAINER=lyra-brain
|
||||||
|
ExecStart=/usr/local/sbin/mi50-watchdog.sh
|
||||||
Executable
+82
@@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
# MI50 runaway watchdog — fallback layer "A".
|
||||||
|
#
|
||||||
|
# Runs on the Proxmox HOST (10.0.0.4) via a systemd timer (every ~2 min). It is the
|
||||||
|
# independent backstop to Lyra's own in-app dream-cycle budget ("C", in lyra/dream.py):
|
||||||
|
# if the MI50 is busy too LONG or runs too HOT, it stops the llama.cpp backend and
|
||||||
|
# pings Brian — regardless of what caused it. Trips on duration only after a full hour
|
||||||
|
# of *continuous* busy, so a legitimate ~40-min manual workload runs untouched.
|
||||||
|
#
|
||||||
|
# The GPU lives on the host; the llama.cpp container ("lyra-brain") runs inside LXC
|
||||||
|
# CT202. So temp/use come from host rocm-smi, and the stop goes via `pct exec`.
|
||||||
|
#
|
||||||
|
# See docs/superpowers/specs/2026-07-04-mi50-runaway-guards-design.md
|
||||||
|
set -uo pipefail
|
||||||
|
|
||||||
|
# --- tunables (override in the .service via Environment=) ---
|
||||||
|
CTID="${CTID:-202}" # LXC holding the docker container
|
||||||
|
CONTAINER="${CONTAINER:-lyra-brain}"
|
||||||
|
MAX_BUSY_SEC="${MAX_BUSY_SEC:-3600}" # 1 hr continuous busy -> stop
|
||||||
|
TEMP_KILL_C="${TEMP_KILL_C:-97}" # junction >= this ...
|
||||||
|
TEMP_KILL_STREAK="${TEMP_KILL_STREAK:-3}" # ... for this many consecutive checks (~6 min)
|
||||||
|
NTFY_URL="${NTFY_URL:-}" # e.g. https://ntfy.sh (empty => log only)
|
||||||
|
NTFY_TOPIC="${NTFY_TOPIC:-}"
|
||||||
|
BUSY_STATE="${BUSY_STATE:-/run/mi50-watchdog.busy_since}"
|
||||||
|
HOT_STATE="${HOT_STATE:-/run/mi50-watchdog.hot_streak}"
|
||||||
|
|
||||||
|
now="$(date +%s)"
|
||||||
|
|
||||||
|
alert() { # $1 title, $2 message
|
||||||
|
logger -t mi50-watchdog "$2"
|
||||||
|
if [[ -n "$NTFY_URL" && -n "$NTFY_TOPIC" ]]; then
|
||||||
|
curl -s -m 8 -H "Title: $1" -H "Priority: urgent" -H "Tags: warning" \
|
||||||
|
-d "$2" "$NTFY_URL/$NTFY_TOPIC" >/dev/null 2>&1 || true
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
stop_backend() { # $1 reason
|
||||||
|
pct exec "$CTID" -- docker stop "$CONTAINER" >/dev/null 2>&1 || true
|
||||||
|
rm -f "$BUSY_STATE" "$HOT_STATE"
|
||||||
|
alert "MI50 watchdog stopped the card" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Nothing to guard if the backend isn't even running.
|
||||||
|
running="$(pct exec "$CTID" -- docker inspect -f '{{.State.Running}}' "$CONTAINER" 2>/dev/null || echo false)"
|
||||||
|
if [[ "$running" != "true" ]]; then
|
||||||
|
rm -f "$BUSY_STATE" "$HOT_STATE"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
use="$(rocm-smi --showuse 2>/dev/null | awk -F: '/GPU use \(%\)/ {gsub(/[^0-9]/, "", $NF); print $NF; exit}')"
|
||||||
|
junction="$(rocm-smi --showtemp 2>/dev/null | awk -F: '/junction/ {gsub(/[^0-9.]/, "", $NF); print $NF; exit}')"
|
||||||
|
|
||||||
|
# --- duration rule: accumulate continuous busy time in a state file ---
|
||||||
|
busy=0
|
||||||
|
[[ "${use:-}" =~ ^[0-9]+$ ]] && (( use > 0 )) && busy=1
|
||||||
|
if (( busy )); then
|
||||||
|
[[ -f "$BUSY_STATE" ]] || echo "$now" > "$BUSY_STATE"
|
||||||
|
since="$(cat "$BUSY_STATE" 2>/dev/null || echo "$now")"
|
||||||
|
elapsed=$(( now - since ))
|
||||||
|
if (( elapsed >= MAX_BUSY_SEC )); then
|
||||||
|
stop_backend "MI50 busy ${elapsed}s continuously (>= ${MAX_BUSY_SEC}s) — stopped ${CONTAINER}."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
rm -f "$BUSY_STATE" # idle breaks the streak
|
||||||
|
fi
|
||||||
|
|
||||||
|
# --- temperature rule: independent of duration ---
|
||||||
|
if [[ "${junction:-}" =~ ^[0-9.]+$ ]]; then
|
||||||
|
jint="${junction%.*}"
|
||||||
|
if (( jint >= TEMP_KILL_C )); then
|
||||||
|
streak=$(( $(cat "$HOT_STATE" 2>/dev/null || echo 0) + 1 ))
|
||||||
|
echo "$streak" > "$HOT_STATE"
|
||||||
|
if (( streak >= TEMP_KILL_STREAK )); then
|
||||||
|
stop_backend "MI50 junction ${jint}C >= ${TEMP_KILL_C}C for ${streak} checks — stopped ${CONTAINER}."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
rm -f "$HOT_STATE" # cooled off, reset the streak
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
exit 0
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Run the MI50 runaway watchdog every 2 minutes
|
||||||
|
|
||||||
|
[Timer]
|
||||||
|
OnBootSec=2min
|
||||||
|
OnUnitActiveSec=2min
|
||||||
|
AccuracySec=15s
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=timers.target
|
||||||
@@ -0,0 +1,141 @@
|
|||||||
|
# Lyra — Cognition Architecture (sketch)
|
||||||
|
|
||||||
|
> The "society of mind" direction: instead of one giant model we keep nagging with
|
||||||
|
> stricter prompts, a society of small specialized parts cooperate to produce each
|
||||||
|
> turn. **Most parts are cheap deterministic code (heuristics, math, learnable
|
||||||
|
> weights); the LLM is the exception, reserved for the few irreducibly-generative
|
||||||
|
> jobs.** Everything is anchored to who she is and tuned by feedback.
|
||||||
|
|
||||||
|
## Principles
|
||||||
|
|
||||||
|
1. **LLM is the exception, not the rule.** Bookkeeping, scoring, routing,
|
||||||
|
thresholding, retrieval → code. Generation (language, novel reasoning, memory
|
||||||
|
compression) → LLM, called sparingly.
|
||||||
|
2. **Mind ≠ Mouth.** A capable "mind" (decide / reason / use tools — helpfulness is
|
||||||
|
fine) is separate from a "mouth" (the character voice). This lets each be the
|
||||||
|
best model for *its* job — and makes the eventual fine-tune easy: you only have
|
||||||
|
to teach a small model to *sound like Lyra*, not to *be smart*.
|
||||||
|
3. **Anchored.** A fixed identity anchor governs the mouth so self-composed prompts
|
||||||
|
can't drift into generic-helper vapor. (Already exists: `self_state.IDENTITY_ANCHOR`.)
|
||||||
|
4. **Tuned by feedback, not just hand-tuning.** Learnable *weights* (over register,
|
||||||
|
memory, parts) nudged by 👍/👎 give real adaptation *without* fine-tuning a model.
|
||||||
|
5. **Allocation is the craft.** Cheap-deterministic where signal is clear; LLM where
|
||||||
|
judgment/language is needed; **hybrid** (heuristic common-case, escalate to LLM on
|
||||||
|
ambiguity) where possible.
|
||||||
|
|
||||||
|
## The blackboard: `TurnContext`
|
||||||
|
|
||||||
|
Parts don't call each other directly — they read from and write to a shared turn
|
||||||
|
state (a blackboard). Heterogeneous parts (heuristic / LLM / weights) cooperate by
|
||||||
|
annotating it. The composer reads the finished blackboard to build the prompt.
|
||||||
|
|
||||||
|
```
|
||||||
|
TurnContext {
|
||||||
|
# --- inputs ---
|
||||||
|
user_msg, session_id, history, now
|
||||||
|
|
||||||
|
# --- perception (heuristic) ---
|
||||||
|
moment : { kind: emotional|strategic|casual|existential|meta,
|
||||||
|
sentiment: -1..1, tilt: 0..1, urgency: 0..1 }
|
||||||
|
|
||||||
|
# --- state (code) ---
|
||||||
|
mood, drives, anchor
|
||||||
|
|
||||||
|
# --- retrieval (math: embeddings + cosine) ---
|
||||||
|
recalled : [memories] # spreading activation
|
||||||
|
threads : [active thoughts]
|
||||||
|
profile, narrative
|
||||||
|
|
||||||
|
# --- control (heuristic + learnable weights) ---
|
||||||
|
register : warm | coach | dry | tender | hype # how to sound
|
||||||
|
intent : console | push_back | teach | riff | act
|
||||||
|
mode : talk | cash | ... # tool allow-list
|
||||||
|
use_tools: bool
|
||||||
|
route : { mind: <model>, mouth: <model> } # which model per role
|
||||||
|
|
||||||
|
# --- generation (LLM, sparing) ---
|
||||||
|
deliberation : "her private thinking" # mind
|
||||||
|
tool_results : [...] # mind + tool exec
|
||||||
|
reply : "final text" # mouth
|
||||||
|
|
||||||
|
# --- learning (heuristic/online) ---
|
||||||
|
weights : { register_prefs, memory_weights, ... } # persisted, feedback-tuned
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## The parts
|
||||||
|
|
||||||
|
| # | Part | Type | Does | Exists today? |
|
||||||
|
|---|------|------|------|---------------|
|
||||||
|
| 1 | **perceive** | heuristic | sentiment + classify the moment + tilt/urgency from session signals & his language | ✗ (new) |
|
||||||
|
| 2 | **recall** | math | embeddings → relevant memories, active threads, profile, narrative | ✓ `memory.recall*`, `cognition.activate` |
|
||||||
|
| 3 | **sense_state** | code | load mood / drives / anchor | ✓ `self_state`, `IDENTITY_ANCHOR` |
|
||||||
|
| 4 | **route** | heuristic + weights | pick register, intent, mode, and which model is mind vs mouth | ✗ (new; partly `modes`) |
|
||||||
|
| 5 | **decide+act (tools)** | LLM (mind) / code | does this turn need a tool? run it | ✓ tool loop in `chat` |
|
||||||
|
| 6 | **deliberate** | LLM (mind) | "what do I actually think" — private substance pass | ✓ `chat._deliberate` |
|
||||||
|
| 7 | **compose** | code | assemble the final prompt from anchor + register + intent + deliberation + recall + tool results + voice rules | ✓ `build_messages` (becomes the composer) |
|
||||||
|
| 8 | **speak** | LLM (mouth) | write the reply in her voice, streamed, anchored | ✓ `llm.chat_call` |
|
||||||
|
| 9 | **learn** | heuristic/online | on 👍/👎 or reaction, nudge `weights` (which register/memory worked) | ✗ (new; data exists in `ratings`) |
|
||||||
|
|
||||||
|
Most of the society (1,2,3,4,7,9) is **free, instant, deterministic, debuggable.**
|
||||||
|
The LLM shows up in only ~2–3 places (5/6 = mind, 8 = mouth).
|
||||||
|
|
||||||
|
## One chat turn
|
||||||
|
|
||||||
|
```
|
||||||
|
user msg
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
[1 perceive]──heuristic: emotional? strategic? tilting? (free)
|
||||||
|
│
|
||||||
|
[2 recall]───math: what lights up (memories, threads) (free)
|
||||||
|
[3 sense]────code: mood, drives, anchor (free)
|
||||||
|
│
|
||||||
|
[4 route]────heuristic+weights: register? intent? mind/mouth? (free)
|
||||||
|
│
|
||||||
|
[5 act]──────MIND model: tools if needed ─────────────┐ (LLM, only if needed)
|
||||||
|
[6 deliberate]──MIND model: what do I actually think │ (LLM, gated)
|
||||||
|
│ │
|
||||||
|
[7 compose]──code: build the prompt ◄──── anchor ──────┘ (free)
|
||||||
|
│
|
||||||
|
[8 speak]────MOUTH model: the reply, in her voice, streamed (LLM)
|
||||||
|
│
|
||||||
|
▼
|
||||||
|
reply ──► (later) [9 learn]: 👍/👎 nudges weights (free, async)
|
||||||
|
```
|
||||||
|
|
||||||
|
## What we reuse vs. build
|
||||||
|
|
||||||
|
- **Reuse (already scattered through the code):** recall/activation, self_state +
|
||||||
|
anchor, drives (in `dream`), modes (tool gating), the deliberation pass, the
|
||||||
|
prompt assembly (`build_messages`), tool loop, ratings store.
|
||||||
|
- **Build new:** the `TurnContext` blackboard + an explicit pipeline runner; the
|
||||||
|
**perceive** heuristic; the **route** part (register/intent + model routing); the
|
||||||
|
**learn** weights loop. Mostly *unifying* existing pieces into one legible control
|
||||||
|
plane, plus 2–3 small heuristic parts.
|
||||||
|
|
||||||
|
## Phasing (smallest first)
|
||||||
|
|
||||||
|
- **P1 — frame:** define `TurnContext`, refactor the current chat turn into the
|
||||||
|
explicit pipeline (perceive=stub → recall → sense → route=mode-only → deliberate →
|
||||||
|
compose → speak), single model. Low-risk refactor; makes the structure real.
|
||||||
|
- **P2 — control plane:** real `perceive` (sentiment/moment) + `route`
|
||||||
|
(register/intent). Now her framing adapts to the moment, deterministically.
|
||||||
|
- **P3 — mind/mouth split:** route picks a separate voice model for `speak`. Plug a
|
||||||
|
character mouth (Claude / local / later a fine-tune). A/B vs. single-model.
|
||||||
|
- **P4 — learning:** `weights` over register/memory, nudged by ratings → cheap
|
||||||
|
adaptation, no fine-tune.
|
||||||
|
- **P5 — her voice:** a small fine-tuned "Lyra voice" model drops into the mouth slot.
|
||||||
|
|
||||||
|
## Open decisions
|
||||||
|
|
||||||
|
- **Mouth model**: Claude (warm, cloud) vs. local character vs. fine-tune. The mouth
|
||||||
|
is the crux; it must render richly (8B local may flatten).
|
||||||
|
- **perceive**: pure heuristics vs. a tiny classifier vs. embedding-to-exemplar
|
||||||
|
clusters. Probably hybrid.
|
||||||
|
- **scheduler**: fixed linear pipeline (simple, v1) vs. drive-based/parallel later.
|
||||||
|
- **tool location**: mind decides+runs tools, mouth only renders (clean split) — vs.
|
||||||
|
letting the mouth call tools (needs a tool-capable mouth).
|
||||||
|
- **latency budget**: how many LLM calls per turn is acceptable live (cheap mind +
|
||||||
|
streamed mouth keeps it ~2).
|
||||||
|
```
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
# Hand-history contract (Lyra → RTO)
|
||||||
|
|
||||||
|
The canonical structured shape for a poker hand. **Lyra owns hands** — it produces this
|
||||||
|
shape (LLM parser today; the tap recorder natively, going forward), stores it, replays it
|
||||||
|
in the viewer, and exports it. **RTO consumes it** over HTTP and never reaches into Lyra.
|
||||||
|
|
||||||
|
Ownership rule: whoever owns the data owns the tools that produce it. Lyra owns the hand
|
||||||
|
DB, the viewer, and the copilot loop, so hand capture lives here. RTO is a pure engine.
|
||||||
|
|
||||||
|
Coupling: **one arrow, Lyra → RTO, HTTP only.** RTO is a standalone service (solve /
|
||||||
|
exploit / estimate); Lyra POSTs to it when it wants analysis. No shared package, no shared
|
||||||
|
DB, no shared UI components. If RTO is down, Lyra skips analysis and nothing breaks.
|
||||||
|
|
||||||
|
## Schema (`schema_version: 1`)
|
||||||
|
|
||||||
|
```jsonc
|
||||||
|
{
|
||||||
|
"schema_version": 1,
|
||||||
|
"game": "NLH", // NLH | PLO | ...
|
||||||
|
"stakes": "1/3", // or null
|
||||||
|
"hero_pos": "BTN", // one of POSITIONS
|
||||||
|
"hero_cards": ["Ah", "Kh"], // convenience mirror of the hero's players[].cards
|
||||||
|
"players": [ // every player in the hand, incl. hero
|
||||||
|
{"pos": "BTN", "stack": 300, "name": "Hero", "cards": ["Ah","Kh"], "hero": true},
|
||||||
|
{"pos": "BB", "stack": 250, "name": "Sal", "cards": null} // cards: null unless shown
|
||||||
|
],
|
||||||
|
"actions": [ // one flat chronological list across all streets
|
||||||
|
{"street": "preflop", "pos": "BTN", "action": "raise", "amount": 15},
|
||||||
|
{"street": "flop", "board": ["7d","2c","5h"]}, // a street begins with its board reveal
|
||||||
|
{"street": "flop", "pos": "BB", "action": "check"}
|
||||||
|
],
|
||||||
|
"board": ["7d","2c","5h"], // full final board, 0–5 cards
|
||||||
|
"result": {"pot": 40, "hero_net": 25, "summary": "one line"},
|
||||||
|
"completeness": {"cards": true, "board": true, "actions": true}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Conventions (load-bearing)
|
||||||
|
|
||||||
|
- **Cards are lists of 2-char tokens**, `RankSuit`: rank in `23456789TJQKA` (ten = `T`),
|
||||||
|
suit in `c d h s` (lowercase). E.g. `["As","5d","2c"]`. RTO maps each token via
|
||||||
|
`pokercore.parse_card`. *(Chosen over space-joined strings: unambiguous, no re-splitting,
|
||||||
|
and it's what Lyra already stores + what the viewer reads.)*
|
||||||
|
- **Unknown cards are kept, not dropped:** `"Ax"` = known rank / unknown suit, `"x"` =
|
||||||
|
fully unknown card. The LLM parser emits these when Brian didn't state suits. The tap
|
||||||
|
recorder won't — it captures complete cards by construction — so `"x"` is an
|
||||||
|
import/parser-only concern.
|
||||||
|
- **`completeness`** tells a consumer what's safe to use: `cards`/`board` are `true` only
|
||||||
|
when every relevant card is fully specified (no `"x"`). RTO uses `false`-card hands for
|
||||||
|
positions/frequencies/pairs and skips suit-dependent math (flushes).
|
||||||
|
- **Hero appears in `players[]`** with `"hero": true` and is findable via `pos == hero_pos`.
|
||||||
|
`hero_cards` is a mirror for the viewer; `players[].cards` is the source of truth.
|
||||||
|
- **Positions:** `UTG UTG1 UTG2 MP LJ HJ CO BTN SB BB`.
|
||||||
|
- **Actions:** `post fold check call bet raise allin`. `amount` is a plain number (no `$`),
|
||||||
|
null for non-sized actions (fold/check). Street boards appear as `{street, board}` entries.
|
||||||
|
- **Streets:** `preflop flop turn river`.
|
||||||
|
|
||||||
|
`lyra/poker.py:normalize_structured()` is the single function that guarantees this shape.
|
||||||
|
It runs on store and on read, and is idempotent.
|
||||||
|
|
||||||
|
## Transport (HTTP, Lyra serves on :7078)
|
||||||
|
|
||||||
|
- `GET /hands/data?limit=N` → `{ "hands": [ {id, position, hole_cards, board, result, tag,
|
||||||
|
at, lesson, venue, stakes, has_structured}, ... ] }` — flat list for browsing. Use
|
||||||
|
`has_structured` to pick which hands have a replayable body worth fetching.
|
||||||
|
- `GET /hand/{id}/data` → the full hand row; `structured` is the object above (or `null`
|
||||||
|
for a flat quick-log that hasn't been reconstructed).
|
||||||
|
|
||||||
|
RTO's "Lyra bridge" (its `docs/estimator-design.md`, Phase B) walks `structured.actions`
|
||||||
|
to classify each villain decision into `checked_to` / `facing_bet` / `facing_raise`, and
|
||||||
|
uses shown `cards` + that street's `board` for board-relative categories. Everything that
|
||||||
|
walk needs is in the schema above.
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
# The Scouting Desk — proactive poker recall + villain identity resolution
|
||||||
|
|
||||||
|
*Design spec. Not built yet. Companion to the "she remembers" north star in the
|
||||||
|
`poker-copilot` memory. Written 2026-07-03, before the trial-by-fire session.*
|
||||||
|
|
||||||
|
## Purpose
|
||||||
|
|
||||||
|
Turn the copilot from a logbook into a copilot that **remembers across sessions,
|
||||||
|
unprompted** — the way a broadcast stats desk slides a note to the color
|
||||||
|
commentator: *"he mentioned the guy's hot streak → here are his last 10 games."*
|
||||||
|
|
||||||
|
Target moments:
|
||||||
|
- *"you had this exact leak last week too, remember?"*
|
||||||
|
- *"neck-tattoo guy just 3-bet you — last time he did that at the Meadows he had it."*
|
||||||
|
- *"Sleepy John was here two weeks ago; you stacked off AK into his set."*
|
||||||
|
|
||||||
|
The failure mode to avoid at all costs: **confident-but-wrong.** A stats desk that
|
||||||
|
guesses gets the commentator burned on air. **Silence is the default; the desk
|
||||||
|
speaks only when there's real signal.**
|
||||||
|
|
||||||
|
## What already exists (don't rebuild it)
|
||||||
|
|
||||||
|
`mind.build_messages()` already runs a recall pass on **every** message:
|
||||||
|
`memory.recall(user_msg)` over past exchanges + `memory.recall_summaries(user_msg)`
|
||||||
|
over session gists, injected as system notes before she replies. The
|
||||||
|
"slide-a-note-in-before-she-speaks" machinery is already the architecture. This
|
||||||
|
spec **adds a poker desk** to that pass — it does not build a new RAG system.
|
||||||
|
|
||||||
|
Episodic links also already exist: `link_hand_players` writes a
|
||||||
|
`player_observations` row per named villain in a recorded hand, carrying
|
||||||
|
`hand_id` AND `session_id`; `player_reads` carry `session_id`. So villain →
|
||||||
|
observation → hand → session/date is reconstructable today.
|
||||||
|
|
||||||
|
## Two retrieval channels (don't conflate them)
|
||||||
|
|
||||||
|
1. **Entity desk — deterministic.** A known **name** in the message → exact/fuzzy
|
||||||
|
SQL match on `poker_players` → pull dossier + your history vs him. ~1ms, no
|
||||||
|
hallucination. This is the "hears the name, pulls last 10 games" case.
|
||||||
|
2. **Pattern desk — semantic.** No entity to key on ("I keep punting these river
|
||||||
|
bluffs") → embed the message, retrieve similar **scar notes / hands / recap
|
||||||
|
passages** by meaning. This is where embeddings earn their keep. Also the
|
||||||
|
backbone of nameless-villain matching (below).
|
||||||
|
|
||||||
|
Both feed one injected **STATS DESK** system note, relevance-gated.
|
||||||
|
|
||||||
|
## The hard part: nameless villains
|
||||||
|
|
||||||
|
Most live villains have no name. Brian identifies them by **physical descriptor**
|
||||||
|
("guy with the lips/neck tattoo"), by **seat** ("seat 4", "two to my left"), or —
|
||||||
|
uselessly — **generically** ("mid-aged white dude with glasses").
|
||||||
|
|
||||||
|
### Current gap
|
||||||
|
`poker_players.name` is `NOT NULL` and identity is an **exact name match**
|
||||||
|
(`upsert_player` → `WHERE name = ?`). The `description` column exists but is dead
|
||||||
|
weight: not a key, not embedded, never matched. **Nameless villains can't exist
|
||||||
|
today.** This is the core schema fix.
|
||||||
|
|
||||||
|
### Identity model — descriptor as a fuzzy primary key
|
||||||
|
Store a villain as:
|
||||||
|
- `name` — now **optional**.
|
||||||
|
- `descriptors` — accumulated distinctive physical tags heard over time
|
||||||
|
("neck tattoo", "lips ink", "heavyset", "bald+beard").
|
||||||
|
- `descriptor_embedding` — embedding of the accumulated distinctive tags, for
|
||||||
|
semantic match against drifting phrasings.
|
||||||
|
- `venue` — a strong disambiguator (the neck-tattoo reg at the Meadows ≠ the one
|
||||||
|
at Wheeling, unless Brian travels).
|
||||||
|
- `distinctiveness` — a weight; distinctive features (tattoos, scars, a name)
|
||||||
|
score high, generic ones (age/race/glasses) near zero.
|
||||||
|
|
||||||
|
### Resolver — matching an incoming reference
|
||||||
|
1. **Name present** → exact/fuzzy SQL match (entity desk). Done.
|
||||||
|
2. **Descriptor present** → embed it, compare to `descriptor_embedding` of known
|
||||||
|
villains **scoped to the current venue**, weighted by distinctiveness.
|
||||||
|
3. **Confidence bands:**
|
||||||
|
- **High** (distinctive + strong match) → surface the file; if live, a light
|
||||||
|
confirm ("the neck-tattoo LAG from 3 weeks ago?").
|
||||||
|
- **Medium/ambiguous** (several candidates, or a middling score) → **do NOT
|
||||||
|
interrupt.** File a `needs_clarification` task to the review queue and stay
|
||||||
|
quiet, OR ask only if it's decision-relevant right now.
|
||||||
|
- **Generic-only** (no distinctive signal) → **refuse to guess.** Stay silent
|
||||||
|
or ask for one distinctive detail ("anything that stands out — ink, chips,
|
||||||
|
how he plays?"). Wrong-guy citation is worse than nothing.
|
||||||
|
- **No match** → new villain; open a descriptor-keyed dossier.
|
||||||
|
|
||||||
|
### Seat = within-session alias only
|
||||||
|
The live session keeps a `seat → villain` map so reads accumulate whether Brian
|
||||||
|
says "seat 4" or "the tattoo guy." Seats evaporate when the session ends — they
|
||||||
|
mean nothing next week.
|
||||||
|
|
||||||
|
## Confirmation loop (live, in chat)
|
||||||
|
|
||||||
|
Auto-merging on a fuzzy match is dangerous, so she **proposes and Brian confirms**
|
||||||
|
in natural language:
|
||||||
|
|
||||||
|
> Brian: "neck tattoo guy just 3-bet me again"
|
||||||
|
> Lyra: "The neck-tattoo LAG from the Meadows three weeks ago — the one who
|
||||||
|
> stacked you with the flush? Or new guy?"
|
||||||
|
> Brian: "yeah him" → reinforce identity · "nah different" → split, and learn
|
||||||
|
> what distinguishes them.
|
||||||
|
|
||||||
|
Handles name-arrives-later for free: catch his name off Bravo → "merge neck-tattoo
|
||||||
|
guy into 'Danny'" → history follows.
|
||||||
|
|
||||||
|
## The review interface (async, out-of-band)
|
||||||
|
|
||||||
|
Silence at the table ≠ forget it → it routes to a queue Brian clears at his pace.
|
||||||
|
|
||||||
|
### `/players` — villain file browser
|
||||||
|
List: name-or-lead-descriptor, venue, category (feeder/risky/reg), hands
|
||||||
|
observed, VPIP/PFR (when sample is real), last seen, distinctive tags. Detail
|
||||||
|
view: reads, showdowns, notable hands (link to `/hand/{id}`), sessions seen,
|
||||||
|
stats. Edit / rename / retag / delete / manual-merge.
|
||||||
|
|
||||||
|
### Resolution queue — two lanes
|
||||||
|
- **Possible merges** — two profiles likely one person (high descriptor
|
||||||
|
similarity + same venue, below auto-merge). Side-by-side → **Same guy** (merge)
|
||||||
|
/ **Different** (split).
|
||||||
|
- **Needs clarification** — a descriptor that matched several candidates, or a
|
||||||
|
nameless villain the resolver couldn't place → pick match / **New guy**.
|
||||||
|
|
||||||
|
### Two rules that keep the queue from rotting
|
||||||
|
1. **A rejected merge stays rejected** — record the pair as *known-distinct* so it
|
||||||
|
never re-surfaces; start tracking the distinguishing tell.
|
||||||
|
2. **Merge-candidate scan runs in the dream cycle**, not the hot path — nightly,
|
||||||
|
compare descriptor embeddings within each venue, file new maybes. Zero live
|
||||||
|
latency.
|
||||||
|
|
||||||
|
## Injection format & gating
|
||||||
|
|
||||||
|
A single system note, clearly marked as structured fact so she cites it (not
|
||||||
|
confabulates), e.g.:
|
||||||
|
|
||||||
|
```
|
||||||
|
STATS DESK — Neck-tattoo guy (Meadows, LAG/reg): seen 3×, last 2wk ago.
|
||||||
|
vs you: hand #38 (AK, stacked off into his set). Reads: overfolds turn,
|
||||||
|
3-bets light from the CO. Sample: 22 hands — VPIP 41 / PFR 28.
|
||||||
|
```
|
||||||
|
|
||||||
|
Gate hard: inject only on a confident entity hit or a strong semantic score.
|
||||||
|
Default to nothing. Never inject a generic-only guess.
|
||||||
|
|
||||||
|
## Honest limits
|
||||||
|
|
||||||
|
Never perfect. Some players are genuinely indistinguishable — fine. The system's
|
||||||
|
only job: **right when there's signal, quiet when there isn't.**
|
||||||
|
|
||||||
|
## New data model (sketch)
|
||||||
|
|
||||||
|
- `poker_players`: `name` → nullable; add `descriptors TEXT`,
|
||||||
|
`descriptor_embedding BLOB`, `distinctiveness REAL`.
|
||||||
|
- `player_distinct_pairs(a_id, b_id, note, created_at)` — rejected merges.
|
||||||
|
- `identity_queue(id, kind, player_ids, descriptor, context, session_id,
|
||||||
|
confidence, status, resolution, created_at)` — kind ∈ {merge_candidate,
|
||||||
|
needs_clarification}.
|
||||||
|
- Live-session `seat → player_id` alias map (in-session only).
|
||||||
|
|
||||||
|
## Sequencing (after the trial-by-fire session — recall feeds on real data)
|
||||||
|
|
||||||
|
1. **Nameless identity + resolver** — schema, descriptor embedding, venue-scoped
|
||||||
|
semantic match, distinctiveness gate. (Unblocks everything.)
|
||||||
|
2. **Scouting-desk injection** — wire entity + pattern recall into
|
||||||
|
`build_messages` as the gated STATS DESK note.
|
||||||
|
3. **Confirmation loop** — the live propose/confirm/merge/split UX in the persona.
|
||||||
|
4. **`/players` browser + resolution queue UI** — the async review interface.
|
||||||
|
5. **Dream-cycle merge scan** — nightly candidate generation.
|
||||||
|
6. **Pattern desk** — semantic recall over scars/notes/recaps for "this leak
|
||||||
|
again."
|
||||||
@@ -0,0 +1,720 @@
|
|||||||
|
# Poker Logging Service Implementation Plan
|
||||||
|
|
||||||
|
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||||
|
|
||||||
|
**Goal:** Turn `lyra/poker.py` into a standalone logging system-of-record with a complete REST API, a single source-of-truth tool/API contract, and a human UI to log and correct everything — usable by Brian with zero LLM dependency.
|
||||||
|
|
||||||
|
**Architecture:** Thin FastAPI routes wrap the existing (already-working) `poker.py` store functions; a declarative `poker_contract.py` pins operation names + required args so the REST API and Lyra's LLM tool specs can't drift; the web UI gets dumb capture inputs (2nd stack box on chat, quick inputs on the HUD) and correction controls. This is sub-project 1 of 2; Lyra's classifier/prompts (sub-project 2) are parked.
|
||||||
|
|
||||||
|
**Tech Stack:** Python 3.11+ (venv runs 3.14), FastAPI + uvicorn, SQLite (WAL), pytest, vanilla HTML/JS/CSS.
|
||||||
|
|
||||||
|
## Global Constraints
|
||||||
|
|
||||||
|
- Python files: start with `from __future__ import annotations`; 4-space indent; ruff `line-length = 100`, `target-version = "py311"`.
|
||||||
|
- **`lyra/web/static/index.html` uses CRLF (`\r\n`) line endings and mixed tabs/spaces.** Every other static file (`session.html`, `style.css`, `nav.js`) and all Python use **LF + spaces**. Match the file you edit or you produce a noisy diff.
|
||||||
|
- Pure data capture (stack / buy-in / cash-out / hand / read) must reach the store via the REST endpoints, **never** through the chat/LLM path.
|
||||||
|
- `poker_contract.py` is the single source of truth: REST routes and `tools.py` specs must agree with it (enforced by a conformance test).
|
||||||
|
- Web app runs via `lyra-web` (uvicorn) on `0.0.0.0:7078`. DB path from `LYRA_DB_PATH` (default `data/lyra.db`, WAL).
|
||||||
|
- Test idiom: fixture sets `LYRA_DB_PATH` to a `tmp_path` file, stubs `llm.embed` (and `llm.complete` where needed), then `importlib.reload(memory)` **then** `importlib.reload(poker)` (order matters), then `importlib.reload(server)` for endpoint tests. Run with `.venv/bin/pytest` (or `uv run pytest`).
|
||||||
|
- Existing store facts to respect: `start_session(...)` uses `fmt=` (column is `format`); `add_buyin` returns a float total; `log_stack` returns the `stack_state` dict `{current, buy_in, net}`; `end_session(cash_out, ...)` takes `cash_out` first; `hud()` returns `None` when no session; `_HAND_FIELDS = ("position","hole_cards","board","preflop","flop","turn","river","showdown","pot","result","stack_after","tag","lesson")`; `upsert_player(name, **fields)` returns an int player id; `tools.dispatch(name, args, ctx)` — `ctx` is a plain dict.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 1: Contract module + tool-spec conformance test
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Create: `lyra/poker_contract.py`
|
||||||
|
- Create: `tests/test_poker_contract.py`
|
||||||
|
|
||||||
|
**Interfaces:**
|
||||||
|
- Produces: `lyra.poker_contract.OPERATIONS: dict[str, dict]` and `CONTRACT_VERSION: int`. Each op value: `{"required": tuple[str,...], "llm_tool": str | None, "rest": tuple[str, str] | None}` where `rest` is `(METHOD, PATH)` with PATH exactly matching the FastAPI route template.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the contract module**
|
||||||
|
|
||||||
|
`lyra/poker_contract.py`:
|
||||||
|
```python
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
# Single source of truth for poker logging operations. The REST API, Lyra's LLM
|
||||||
|
# tool specs, the human UI, and (later) an MCP wrapper all derive from this.
|
||||||
|
# `required` MUST match the `required` list in the matching tools.py spec.
|
||||||
|
# `rest` PATH MUST match the FastAPI route template verbatim.
|
||||||
|
CONTRACT_VERSION = 1
|
||||||
|
|
||||||
|
OPERATIONS: dict[str, dict] = {
|
||||||
|
"start_session": {"required": (), "llm_tool": "start_session", "rest": ("POST", "/session")},
|
||||||
|
"update_session": {"required": (), "llm_tool": "update_session", "rest": ("PATCH", "/session/{session_id}")},
|
||||||
|
"end_session": {"required": ("cash_out",), "llm_tool": "end_session", "rest": None},
|
||||||
|
"log_stack": {"required": ("amount",), "llm_tool": "log_stack", "rest": ("POST", "/session/stack")},
|
||||||
|
"add_buyin": {"required": ("amount",), "llm_tool": "add_buyin", "rest": ("POST", "/session/buyin")},
|
||||||
|
"log_hand": {"required": (), "llm_tool": "log_hand", "rest": ("POST", "/session/hand")},
|
||||||
|
"update_hand": {"required": ("id",), "llm_tool": None, "rest": ("PATCH", "/hand/{hand_id}")},
|
||||||
|
"add_read": {"required": ("note",), "llm_tool": "add_read", "rest": ("POST", "/session/read")},
|
||||||
|
"update_player": {"required": ("id",), "llm_tool": None, "rest": ("PATCH", "/player/{player_id}")},
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Write the failing conformance test**
|
||||||
|
|
||||||
|
`tests/test_poker_contract.py`:
|
||||||
|
```python
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from lyra import tools
|
||||||
|
from lyra.poker_contract import OPERATIONS
|
||||||
|
|
||||||
|
|
||||||
|
def test_llm_tool_required_args_match_contract():
|
||||||
|
for op, decl in OPERATIONS.items():
|
||||||
|
name = decl["llm_tool"]
|
||||||
|
if not name:
|
||||||
|
continue
|
||||||
|
spec = tools.TOOLS[name]["spec"]
|
||||||
|
required = set(spec["function"]["parameters"]["required"])
|
||||||
|
assert required == set(decl["required"]), (
|
||||||
|
f"{op}: tools spec required {required} != contract {set(decl['required'])}"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Run the test**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest tests/test_poker_contract.py -v`
|
||||||
|
Expected: PASS (the contract's `required` tuples were copied from the live specs).
|
||||||
|
|
||||||
|
- [ ] **Step 4: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add lyra/poker_contract.py tests/test_poker_contract.py
|
||||||
|
git commit -m "feat: poker operation contract + tool-spec conformance test"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 2: Direct capture endpoints (stack / buy-in / start)
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `lyra/web/server.py` (add three routes inside `create_app`, near the existing `PATCH /session/{session_id}` at server.py:116)
|
||||||
|
- Create: `tests/test_poker_api.py`
|
||||||
|
|
||||||
|
**Interfaces:**
|
||||||
|
- Consumes: `poker.log_stack(amount, note=None)`, `poker.add_buyin(amount)`, `poker.start_session(venue=, stakes=, game=, fmt=, buy_in=, mantra=)`, `poker.live_session()`.
|
||||||
|
- Produces: `POST /session/stack` → `{ok, stack}` or `{ok:false, error}`; `POST /session/buyin` → `{ok, buy_in_total}`; `POST /session` → `{ok, id}`.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the failing endpoint tests**
|
||||||
|
|
||||||
|
`tests/test_poker_api.py`:
|
||||||
|
```python
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client(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)
|
||||||
|
import lyra.web.server as server
|
||||||
|
importlib.reload(server)
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
return TestClient(server.app), poker
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_stack_logs_and_returns_state(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(venue="Meadows", stakes="1/3", buy_in=400)
|
||||||
|
r = c.post("/session/stack", json={"amount": 373})
|
||||||
|
assert r.status_code == 200
|
||||||
|
body = r.json()
|
||||||
|
assert body["ok"] is True
|
||||||
|
assert body["stack"]["current"] == 373
|
||||||
|
assert body["stack"]["net"] == pytest.approx(-27)
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_stack_without_session_errors(client):
|
||||||
|
c, _ = client
|
||||||
|
r = c.post("/session/stack", json={"amount": 373})
|
||||||
|
assert r.json()["ok"] is False
|
||||||
|
assert "error" in r.json()
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_buyin_increments_total(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(buy_in=400)
|
||||||
|
r = c.post("/session/buyin", json={"amount": 200})
|
||||||
|
assert r.json()["buy_in_total"] == pytest.approx(600)
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_session_starts_live(client):
|
||||||
|
c, poker = client
|
||||||
|
r = c.post("/session", json={"venue": "Wheeling", "stakes": "1/3", "buy_in": 400})
|
||||||
|
sid = r.json()["id"]
|
||||||
|
assert poker.live_session()["id"] == sid
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run to verify it fails**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest tests/test_poker_api.py -v`
|
||||||
|
Expected: FAIL with 404s (routes not defined). If it errors with "No module named 'httpx'", run `.venv/bin/pip install httpx` (TestClient needs it).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Add the three routes**
|
||||||
|
|
||||||
|
In `lyra/web/server.py`, immediately after the `PATCH /session/{session_id}` handler (server.py:122), add:
|
||||||
|
```python
|
||||||
|
@app.post("/session/stack")
|
||||||
|
async def session_log_stack(request: Request) -> dict:
|
||||||
|
"""Log Brian's current stack directly (no LLM). Server-stamps the time."""
|
||||||
|
body = await request.json()
|
||||||
|
try:
|
||||||
|
amount = float(body.get("amount"))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return {"ok": False, "error": "amount must be a number"}
|
||||||
|
note = (body.get("note") or "").strip() or None
|
||||||
|
try:
|
||||||
|
state = await asyncio.to_thread(poker.log_stack, amount, note)
|
||||||
|
except ValueError as exc:
|
||||||
|
return {"ok": False, "error": str(exc)}
|
||||||
|
logbus.log("info", "stack logged (direct)", amount=amount)
|
||||||
|
return {"ok": True, "stack": state}
|
||||||
|
|
||||||
|
@app.post("/session/buyin")
|
||||||
|
async def session_add_buyin(request: Request) -> dict:
|
||||||
|
"""Add a buy-in/rebuy directly (no LLM)."""
|
||||||
|
body = await request.json()
|
||||||
|
try:
|
||||||
|
amount = float(body.get("amount"))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return {"ok": False, "error": "amount must be a number"}
|
||||||
|
try:
|
||||||
|
total = await asyncio.to_thread(poker.add_buyin, amount)
|
||||||
|
except ValueError as exc:
|
||||||
|
return {"ok": False, "error": str(exc)}
|
||||||
|
logbus.log("info", "buyin added (direct)", amount=amount)
|
||||||
|
return {"ok": True, "buy_in_total": total}
|
||||||
|
|
||||||
|
@app.post("/session")
|
||||||
|
async def session_start(request: Request) -> dict:
|
||||||
|
"""Open a new live session directly (no LLM)."""
|
||||||
|
body = await request.json()
|
||||||
|
sid = await asyncio.to_thread(lambda: poker.start_session(
|
||||||
|
venue=body.get("venue"), stakes=body.get("stakes"),
|
||||||
|
game=body.get("game") or "NLH", fmt=body.get("format") or "cash",
|
||||||
|
buy_in=body.get("buy_in") or 0, mantra=body.get("mantra"),
|
||||||
|
))
|
||||||
|
logbus.log("info", "poker session started (direct)", id=sid)
|
||||||
|
return {"ok": True, "id": sid}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Run to verify it passes**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest tests/test_poker_api.py -v`
|
||||||
|
Expected: PASS (4 tests).
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add lyra/web/server.py tests/test_poker_api.py
|
||||||
|
git commit -m "feat: direct REST endpoints for stack/buyin/start-session (no LLM)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 3: Hands API (log / edit / delete)
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `lyra/poker.py` (add `update_hand` near `log_hand` at poker.py:558)
|
||||||
|
- Modify: `lyra/web/server.py` (add routes after the Task 2 routes)
|
||||||
|
- Modify: `tests/test_poker_api.py` (add tests)
|
||||||
|
|
||||||
|
**Interfaces:**
|
||||||
|
- Consumes: `poker.log_hand(**fields)`, `poker.get_hand(id)`, `poker.delete_entry("hand", id)`, `_HAND_FIELDS`.
|
||||||
|
- Produces: `poker.update_hand(hand_id, **fields) -> dict | None`; `POST /session/hand` → `{ok, id}`; `PATCH /hand/{hand_id}` → `{ok, hand}`; `DELETE /hand/{hand_id}` → `{ok}`.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the failing tests**
|
||||||
|
|
||||||
|
Append to `tests/test_poker_api.py`:
|
||||||
|
```python
|
||||||
|
def test_post_hand_edit_and_delete(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(buy_in=400)
|
||||||
|
r = c.post("/session/hand", json={"position": "BTN", "hole_cards": "22", "result": 120})
|
||||||
|
assert r.json()["ok"] is True
|
||||||
|
hid = r.json()["id"]
|
||||||
|
r2 = c.patch(f"/hand/{hid}", json={"hole_cards": "2c2d"})
|
||||||
|
assert r2.json()["ok"] is True
|
||||||
|
assert r2.json()["hand"]["hole_cards"] == "2c2d"
|
||||||
|
r3 = c.delete(f"/hand/{hid}")
|
||||||
|
assert r3.json()["ok"] is True
|
||||||
|
assert poker.get_hand(hid) is None
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run to verify it fails**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest tests/test_poker_api.py::test_post_hand_edit_and_delete -v`
|
||||||
|
Expected: FAIL (404 on `/session/hand`).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Add `update_hand` to the store**
|
||||||
|
|
||||||
|
In `lyra/poker.py`, immediately after `log_hand` (poker.py:558), add:
|
||||||
|
```python
|
||||||
|
def update_hand(hand_id: int, **fields) -> dict | None:
|
||||||
|
"""Edit a logged hand's flat fields (fix a mislabeled board, result, villain).
|
||||||
|
Only known columns are touched. Returns the updated hand row or None."""
|
||||||
|
sets, vals = [], []
|
||||||
|
for k, v in fields.items():
|
||||||
|
if k in _HAND_FIELDS and v is not None:
|
||||||
|
sets.append(f"{k} = ?")
|
||||||
|
vals.append(v)
|
||||||
|
if sets:
|
||||||
|
conn = _c()
|
||||||
|
with conn:
|
||||||
|
conn.execute(f"UPDATE poker_hands SET {', '.join(sets)} WHERE id = ?",
|
||||||
|
(*vals, hand_id))
|
||||||
|
return get_hand(hand_id)
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Add the three routes**
|
||||||
|
|
||||||
|
In `lyra/web/server.py`, after the Task 2 routes, add:
|
||||||
|
```python
|
||||||
|
@app.post("/session/hand")
|
||||||
|
async def session_log_hand(request: Request) -> dict:
|
||||||
|
"""Log a hand directly with flat fields (no LLM parse)."""
|
||||||
|
body = await request.json()
|
||||||
|
try:
|
||||||
|
hid = await asyncio.to_thread(lambda: poker.log_hand(**body))
|
||||||
|
except ValueError as exc:
|
||||||
|
return {"ok": False, "error": str(exc)}
|
||||||
|
logbus.log("info", "hand logged (direct)", id=hid)
|
||||||
|
return {"ok": True, "id": hid}
|
||||||
|
|
||||||
|
@app.patch("/hand/{hand_id}")
|
||||||
|
async def hand_update(hand_id: int, request: Request) -> dict:
|
||||||
|
"""Edit a logged hand's flat fields."""
|
||||||
|
body = await request.json()
|
||||||
|
h = await asyncio.to_thread(lambda: poker.update_hand(hand_id, **body))
|
||||||
|
logbus.log("info", "hand edited", id=hand_id, fields=list(body))
|
||||||
|
return {"ok": h is not None, "hand": h}
|
||||||
|
|
||||||
|
@app.delete("/hand/{hand_id}")
|
||||||
|
async def hand_delete(hand_id: int) -> dict:
|
||||||
|
"""Delete a logged hand."""
|
||||||
|
ok = await asyncio.to_thread(poker.delete_entry, "hand", hand_id)
|
||||||
|
return {"ok": ok}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Run to verify it passes**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest tests/test_poker_api.py -v`
|
||||||
|
Expected: PASS (all tests, including the new hand test).
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add lyra/poker.py lyra/web/server.py tests/test_poker_api.py
|
||||||
|
git commit -m "feat: hands API — log_hand endpoint, update_hand store fn, edit/delete routes"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 4: Reads/players API + route conformance
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `lyra/poker.py` (add `update_player` near `upsert_player`)
|
||||||
|
- Modify: `lyra/web/server.py` (add routes)
|
||||||
|
- Modify: `tests/test_poker_api.py` (add tests)
|
||||||
|
- Modify: `tests/test_poker_contract.py` (add route-coverage test)
|
||||||
|
|
||||||
|
**Interfaces:**
|
||||||
|
- Consumes: `poker.add_read(note=, name=, ...)`, `poker.upsert_player(name, **fields)`.
|
||||||
|
- Produces: `poker.update_player(player_id, **fields) -> dict | None`; `POST /session/read` → `{ok, id}`; `PATCH /player/{player_id}` → `{ok, player}`.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Write the failing tests**
|
||||||
|
|
||||||
|
Append to `tests/test_poker_api.py`:
|
||||||
|
```python
|
||||||
|
def test_post_read(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(buy_in=400)
|
||||||
|
r = c.post("/session/read", json={"note": "3-bets light", "name": "James K"})
|
||||||
|
assert r.json()["ok"] is True
|
||||||
|
assert isinstance(r.json()["id"], int)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rename_player_fixes_mislabel(client):
|
||||||
|
c, poker = client
|
||||||
|
pid = poker.upsert_player("Dave the rock", category="reg")
|
||||||
|
r = c.patch(f"/player/{pid}", json={"name": "Dave the mechanic"})
|
||||||
|
assert r.json()["ok"] is True
|
||||||
|
assert r.json()["player"]["name"] == "Dave the mechanic"
|
||||||
|
```
|
||||||
|
|
||||||
|
Append to `tests/test_poker_contract.py`:
|
||||||
|
```python
|
||||||
|
def test_rest_routes_registered():
|
||||||
|
import lyra.web.server as server
|
||||||
|
registered = set()
|
||||||
|
for route in server.app.routes:
|
||||||
|
methods = getattr(route, "methods", None)
|
||||||
|
path = getattr(route, "path", None)
|
||||||
|
if not methods or not path:
|
||||||
|
continue
|
||||||
|
for m in methods:
|
||||||
|
registered.add((m, path))
|
||||||
|
for op, decl in OPERATIONS.items():
|
||||||
|
if not decl["rest"]:
|
||||||
|
continue
|
||||||
|
method, path = decl["rest"]
|
||||||
|
assert (method, path) in registered, f"{op}: {method} {path} not registered"
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Run to verify it fails**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest tests/test_poker_api.py::test_rename_player_fixes_mislabel tests/test_poker_contract.py::test_rest_routes_registered -v`
|
||||||
|
Expected: FAIL (404 on `/player/...`; route-coverage missing several POST/PATCH paths).
|
||||||
|
|
||||||
|
- [ ] **Step 3: Add `update_player` to the store**
|
||||||
|
|
||||||
|
In `lyra/poker.py`, immediately after `upsert_player` (find it near poker.py:1010), add:
|
||||||
|
```python
|
||||||
|
_PLAYER_FIELDS = ("name", "venue", "description", "tendencies", "adjustment", "category")
|
||||||
|
|
||||||
|
|
||||||
|
def update_player(player_id: int, **fields) -> dict | None:
|
||||||
|
"""Edit a player's dossier (rename, fix tendencies/category). Returns the row or None."""
|
||||||
|
sets, vals = [], []
|
||||||
|
for k, v in fields.items():
|
||||||
|
if k in _PLAYER_FIELDS and v is not None:
|
||||||
|
sets.append(f"{k} = ?")
|
||||||
|
vals.append(v)
|
||||||
|
if sets:
|
||||||
|
conn = _c()
|
||||||
|
with conn:
|
||||||
|
conn.execute(f"UPDATE poker_players SET {', '.join(sets)} WHERE id = ?",
|
||||||
|
(*vals, player_id))
|
||||||
|
row = _c().execute("SELECT * FROM poker_players WHERE id = ?", (player_id,)).fetchone()
|
||||||
|
return dict(row) if row else None
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Add the two routes**
|
||||||
|
|
||||||
|
In `lyra/web/server.py`, after the Task 3 routes, add:
|
||||||
|
```python
|
||||||
|
@app.post("/session/read")
|
||||||
|
async def session_add_read(request: Request) -> dict:
|
||||||
|
"""Log a read directly (no LLM); upserts the villain file when name is given."""
|
||||||
|
body = await request.json()
|
||||||
|
rid = await asyncio.to_thread(lambda: poker.add_read(
|
||||||
|
note=body.get("note") or "", seat=body.get("seat"), name=body.get("name"),
|
||||||
|
tendencies=body.get("tendencies"), adjustment=body.get("adjustment"),
|
||||||
|
description=body.get("description"), category=body.get("category"),
|
||||||
|
venue=body.get("venue"),
|
||||||
|
))
|
||||||
|
return {"ok": True, "id": rid}
|
||||||
|
|
||||||
|
@app.patch("/player/{player_id}")
|
||||||
|
async def player_update(player_id: int, request: Request) -> dict:
|
||||||
|
"""Edit a player's dossier (rename, fix tendencies)."""
|
||||||
|
body = await request.json()
|
||||||
|
p = await asyncio.to_thread(lambda: poker.update_player(player_id, **body))
|
||||||
|
logbus.log("info", "player edited", id=player_id, fields=list(body))
|
||||||
|
return {"ok": p is not None, "player": p}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Run to verify it passes**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest tests/test_poker_api.py tests/test_poker_contract.py -v`
|
||||||
|
Expected: PASS (all API tests + both conformance tests).
|
||||||
|
|
||||||
|
- [ ] **Step 6: Run the full suite (no regressions)**
|
||||||
|
|
||||||
|
Run: `.venv/bin/pytest -q`
|
||||||
|
Expected: PASS (existing poker/tools/chat tests still green).
|
||||||
|
|
||||||
|
- [ ] **Step 7: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add lyra/poker.py lyra/web/server.py tests/test_poker_api.py tests/test_poker_contract.py
|
||||||
|
git commit -m "feat: reads/players API + REST route conformance test"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 5: Chat-page stack quick-capture (2nd input box)
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `lyra/web/static/index.html` (**CRLF + tabs** — add markup + JS)
|
||||||
|
- Modify: `lyra/web/static/style.css` (LF + spaces — add styling)
|
||||||
|
|
||||||
|
**Interfaces:**
|
||||||
|
- Consumes: `POST /session/stack` (Task 2). Reads `currentSession` and the Live Log DOM (`#thinkingContent`, `#thinkingEmpty`) already present in index.html.
|
||||||
|
- Produces: a stack-only input that logs without any chat/LLM call.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Add the input row markup**
|
||||||
|
|
||||||
|
In `lyra/web/static/index.html`, insert **between** the `<div id="input">…</div>` block (ends ~index.html:125) and `<nav id="tabbar">` (index.html:128). **Use CRLF + tab indentation to match the file.**
|
||||||
|
```html
|
||||||
|
<!-- Stack quick-capture (no LLM): type a number -> logs current stack -->
|
||||||
|
<div id="stackQuick">
|
||||||
|
<input id="stackQuickInput" type="number" inputmode="decimal" placeholder="Stack $" aria-label="Log current stack">
|
||||||
|
<button id="stackQuickBtn" type="button" title="Log stack (no chat)">Log</button>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Add the JS**
|
||||||
|
|
||||||
|
In the `<script>` of `index.html`, near `sendMessage` (index.html:299), add (CRLF + tabs):
|
||||||
|
```javascript
|
||||||
|
function liveLogLine(text) {
|
||||||
|
const content = document.getElementById("thinkingContent");
|
||||||
|
const empty = document.getElementById("thinkingEmpty");
|
||||||
|
if (empty) empty.style.display = "none";
|
||||||
|
const div = document.createElement("div");
|
||||||
|
div.className = "thinking-event";
|
||||||
|
div.textContent = text;
|
||||||
|
content.appendChild(div);
|
||||||
|
content.scrollTop = content.scrollHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function logStackQuick() {
|
||||||
|
const el = document.getElementById("stackQuickInput");
|
||||||
|
const raw = (el.value || "").replace(/[^0-9.]/g, "");
|
||||||
|
if (!raw) return;
|
||||||
|
const amount = Number(raw);
|
||||||
|
try {
|
||||||
|
const r = await fetch("/session/stack", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ amount })
|
||||||
|
});
|
||||||
|
const data = await r.json();
|
||||||
|
if (!data.ok) { liveLogLine("⚠ " + (data.error || "stack not logged")); return; }
|
||||||
|
const t = new Date().toLocaleTimeString([], { hour: "numeric", minute: "2-digit" });
|
||||||
|
const net = (data.stack && data.stack.net != null)
|
||||||
|
? ` (net ${data.stack.net >= 0 ? "+" : ""}${data.stack.net})` : "";
|
||||||
|
liveLogLine(`💰 $${amount} logged · ${t}${net}`);
|
||||||
|
el.value = "";
|
||||||
|
} catch (e) {
|
||||||
|
liveLogLine("⚠ stack log failed: " + e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
document.getElementById("stackQuickBtn").addEventListener("click", logStackQuick);
|
||||||
|
document.getElementById("stackQuickInput").addEventListener("keydown", (e) => {
|
||||||
|
if (e.key === "Enter") { e.preventDefault(); logStackQuick(); }
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Add styling**
|
||||||
|
|
||||||
|
In `lyra/web/static/style.css` (LF + spaces), add:
|
||||||
|
```css
|
||||||
|
#stackQuick {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
align-items: center;
|
||||||
|
padding: 6px 12px;
|
||||||
|
border-top: 1px solid var(--border, #222);
|
||||||
|
}
|
||||||
|
#stackQuick input {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
padding: 8px 10px;
|
||||||
|
background: var(--panel, #111);
|
||||||
|
color: inherit;
|
||||||
|
border: 1px solid var(--border, #333);
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
#stackQuick button {
|
||||||
|
padding: 8px 14px;
|
||||||
|
background: var(--accent, #ff7a18);
|
||||||
|
color: #000;
|
||||||
|
border: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 4: Verify manually**
|
||||||
|
|
||||||
|
Start the app: `.venv/bin/python -m lyra.web.server` (serves on :7078). With a live session (start one via the HUD or `curl -XPOST localhost:7078/session -d '{"buy_in":400}' -H 'Content-Type: application/json'`):
|
||||||
|
- The stack box appears below the message input, above the nav icons.
|
||||||
|
- Type `350`, press Enter → a `💰 $350 logged · …` line appears in the Live Log, the box clears, and **no chat bubble is added**.
|
||||||
|
- Confirm persisted: `curl -s localhost:7078/session/data | python -m json.tool` shows `stack.current == 350`.
|
||||||
|
|
||||||
|
- [ ] **Step 5: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add lyra/web/static/index.html lyra/web/static/style.css
|
||||||
|
git commit -m "feat: stack quick-capture box on chat page (no LLM)"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 6: HUD quick-capture + correction controls
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `lyra/web/static/session.html` (LF + spaces — Stack card markup, villain rename control, JS functions)
|
||||||
|
|
||||||
|
**Interfaces:**
|
||||||
|
- Consumes: `POST /session/stack`, `POST /session/buyin` (Task 2), `PATCH /session/{id}` (existing), `PATCH /player/{id}` (Task 4). Reads existing globals `curSession`, `refresh()`, and the villain render block.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Add quick inputs to the Stack card**
|
||||||
|
|
||||||
|
In `lyra/web/static/session.html`, replace the Stack card block (session.html:280-289) with the same block plus a `quick` row before its closing `</div>`:
|
||||||
|
```javascript
|
||||||
|
<div class="card">
|
||||||
|
<p class="label">Stack</p>
|
||||||
|
<div class="stack-row">
|
||||||
|
<span class="stack-now">${stack.current == null ? '—' : money(stack.current)}</span>
|
||||||
|
<span class="net ${netClass(stack.net)}">${stack.net == null ? '' : signed(stack.net)}</span>
|
||||||
|
<span class="stack-meta">bought in ${money(stack.buy_in)}<br>${(stack.log||[]).length} update(s)</span>
|
||||||
|
</div>
|
||||||
|
${sparkline(stack.log || [])}
|
||||||
|
<div class="quick">
|
||||||
|
<input id="qStack" type="number" inputmode="decimal" placeholder="Stack $" onkeydown="if(event.key==='Enter')postStack()">
|
||||||
|
<button onclick="postStack()">Log stack</button>
|
||||||
|
<input id="qBuyin" type="number" inputmode="decimal" placeholder="Buy-in $" onkeydown="if(event.key==='Enter')postBuyin()">
|
||||||
|
<button onclick="postBuyin()">Add buy-in</button>
|
||||||
|
<input id="qCashout" type="number" inputmode="decimal" placeholder="Cash out $" onkeydown="if(event.key==='Enter')postCashout()">
|
||||||
|
<button onclick="postCashout()">Cash out</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 2: Add the quick-capture + rename JS functions**
|
||||||
|
|
||||||
|
In the `<script>` of `session.html`, near `saveEdit()` (session.html:192), add:
|
||||||
|
```javascript
|
||||||
|
async function postQuick(url, amount, body){
|
||||||
|
const r = await fetch(url, { method: 'POST', headers: {'Content-Type':'application/json'},
|
||||||
|
body: JSON.stringify(body || { amount }) });
|
||||||
|
const d = await r.json();
|
||||||
|
if(!d.ok){ alert(d.error || 'failed'); return false; }
|
||||||
|
refresh(); return true;
|
||||||
|
}
|
||||||
|
function numVal(id){ const el = document.getElementById(id); return Number((el.value||'').replace(/[^0-9.]/g,'')); }
|
||||||
|
async function postStack(){ const v = numVal('qStack'); if(v) { if(await postQuick('/session/stack', v)) document.getElementById('qStack').value=''; } }
|
||||||
|
async function postBuyin(){ const v = numVal('qBuyin'); if(v) { if(await postQuick('/session/buyin', v)) document.getElementById('qBuyin').value=''; } }
|
||||||
|
async function postCashout(){
|
||||||
|
if(!curSession) return;
|
||||||
|
const v = numVal('qCashout'); if(!v) return;
|
||||||
|
const r = await fetch('/session/'+curSession.id, { method:'PATCH', headers:{'Content-Type':'application/json'},
|
||||||
|
body: JSON.stringify({ cash_out: v }) });
|
||||||
|
if(!(await r.json()).ok){ alert('failed'); return; }
|
||||||
|
document.getElementById('qCashout').value=''; refresh();
|
||||||
|
}
|
||||||
|
async function renamePlayer(id, current){
|
||||||
|
const name = prompt('Rename player', current || ''); if(!name) return;
|
||||||
|
const r = await fetch('/player/'+id, { method:'PATCH', headers:{'Content-Type':'application/json'},
|
||||||
|
body: JSON.stringify({ name }) });
|
||||||
|
if(!(await r.json()).ok){ alert('failed'); return; }
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Add the rename control to the villains list**
|
||||||
|
|
||||||
|
In `session.html`, find the villains render block in `render(data)` (it maps over `data.villains` / the `villains` array). For each villain item, add a rename affordance next to the name, using the player id field present on the villain row (commonly `v.id` or `v.player_id` — use whichever the bundle provides):
|
||||||
|
```javascript
|
||||||
|
<button class="mini" title="Rename / fix" onclick="renamePlayer(${v.id}, '${esc(v.name||'')}')">✎</button>
|
||||||
|
```
|
||||||
|
Read the existing villain block first to splice this in cleanly and confirm the id field name.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Add minimal styling**
|
||||||
|
|
||||||
|
In the inline `<style>` of `session.html`, add:
|
||||||
|
```css
|
||||||
|
.quick { display:flex; flex-wrap:wrap; gap:6px; margin-top:12px; }
|
||||||
|
.quick input { width:96px; padding:7px 9px; background:#111; color:inherit; border:1px solid #333; border-radius:8px; }
|
||||||
|
.quick button { padding:7px 11px; background:var(--accent,#ff7a18); color:#000; border:none; border-radius:8px; font-weight:600; }
|
||||||
|
button.mini { background:transparent; border:none; color:#888; cursor:pointer; padding:0 4px; }
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 5: Verify manually**
|
||||||
|
|
||||||
|
With the app running and a live session, open `/session`:
|
||||||
|
- Log a stack via `qStack` → sparkline + net update without a chat call.
|
||||||
|
- Add a buy-in via `qBuyin` → "bought in" total rises.
|
||||||
|
- Enter a cash-out via `qCashout` → session net updates.
|
||||||
|
- Click ✎ on a villain, rename it → name changes after refresh. Confirm via `curl -s localhost:7078/session/data`.
|
||||||
|
|
||||||
|
- [ ] **Step 6: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add lyra/web/static/session.html
|
||||||
|
git commit -m "feat: HUD quick-capture (stack/buyin/cashout) + villain rename"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### Task 7: iOS-PWA bottom safe-area gap fix
|
||||||
|
|
||||||
|
**Files:**
|
||||||
|
- Modify: `lyra/web/static/style.css` (bottom nav / container safe-area)
|
||||||
|
|
||||||
|
**Interfaces:** none (visual fix). The empty band below the nav icons is the home-indicator inset not being consumed by `#tabbar`.
|
||||||
|
|
||||||
|
- [ ] **Step 1: Load the iOS-PWA skill**
|
||||||
|
|
||||||
|
Invoke the `building-ios-pwas` skill and follow its guidance for safe-area / `100dvh` handling before editing. The current `#tabbar` (style.css:921-952) applies `env(safe-area-inset-left/right)` and `padding-bottom: 6px`, but does **not** add `env(safe-area-inset-bottom)` — the likely cause.
|
||||||
|
|
||||||
|
- [ ] **Step 2: Apply the safe-area fix**
|
||||||
|
|
||||||
|
In `lyra/web/static/style.css`, in the mobile `#tabbar` rule (style.css:921-929), change the bottom padding to consume the inset, and ensure the bar is pinned:
|
||||||
|
```css
|
||||||
|
#tabbar {
|
||||||
|
/* …existing flex/border rules… */
|
||||||
|
position: fixed;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
padding-bottom: calc(6px + env(safe-area-inset-bottom));
|
||||||
|
}
|
||||||
|
```
|
||||||
|
And ensure the chat scroll container reserves space for the bar so content isn't hidden behind it (match the container selector used at style.css:836-852):
|
||||||
|
```css
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
#messages {
|
||||||
|
padding-bottom: calc(64px + env(safe-area-inset-bottom));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
- [ ] **Step 3: Verify on device**
|
||||||
|
|
||||||
|
Open the PWA (Add to Home Screen) on iPhone:
|
||||||
|
- The empty band below the icons is gone; the nav sits flush above the home indicator.
|
||||||
|
- The stack quick-capture box (Task 5) sits directly above the nav.
|
||||||
|
- Open the keyboard: `body.kb` still hides the tabbar (style.css:952) and the input pins to the keyboard — confirm no regression.
|
||||||
|
- If the gap persists or content clips, follow the `building-ios-pwas` skill's `100dvh`/`visualViewport` guidance and iterate.
|
||||||
|
|
||||||
|
- [ ] **Step 4: Commit**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git add lyra/web/static/style.css
|
||||||
|
git commit -m "fix: consume iOS home-indicator safe-area inset under bottom nav"
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Self-Review
|
||||||
|
|
||||||
|
**Spec coverage:**
|
||||||
|
- Complete API surface (create/update/delete per entity) → Tasks 2 (stack/buyin/start), 3 (hands), 4 (reads/players); existing PATCH/DELETE session + entry routes retained.
|
||||||
|
- Single documented/versioned tool-API contract → Task 1 (`poker_contract.py`, `CONTRACT_VERSION`) + conformance tests (Tasks 1, 4).
|
||||||
|
- Human UI to log + edit/correct → Tasks 5 (chat 2nd box), 6 (HUD quick inputs + villain rename + existing edit form/delete).
|
||||||
|
- Pure capture never touches LLM → all capture goes through REST endpoints (Tasks 2–6); verified in manual steps (no chat bubble).
|
||||||
|
- 2nd input box + PWA fix → Tasks 5, 7.
|
||||||
|
- Non-goals respected: no classifier/prompts, no MI50 tool enablement, no MCP, buy-in stays scalar (`add_buyin` increments `buy_in_total`).
|
||||||
|
|
||||||
|
**Placeholder scan:** All code steps contain complete code. The one "locate the block" instruction (Task 6 Step 3, villain rename) provides the exact button snippet and names the id-field ambiguity to resolve by reading the file — not a placeholder, a grounded splice.
|
||||||
|
|
||||||
|
**Type consistency:** `poker_contract.OPERATIONS` shape is consistent across Tasks 1 and 4; REST paths in the contract (`/session/stack`, `/session/buyin`, `/session`, `/session/hand`, `/hand/{hand_id}`, `/session/read`, `/player/{player_id}`, `/session/{session_id}`) match the routes added in Tasks 2–4 exactly; `update_hand`/`update_player` signatures match their callers; response shapes (`{ok, stack}`, `{ok, buy_in_total}`, `{ok, id}`, `{ok, hand}`, `{ok, player}`) are used consistently in tests and routes.
|
||||||
|
|
||||||
|
**Open implementation note:** Task 6 Step 3 requires reading `session.html`'s villain render to confirm the player id field name (`v.id` vs `v.player_id`) before splicing the rename button.
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
# Poker logging service + message-type prompts
|
||||||
|
|
||||||
|
- **Date:** 2026-06-28
|
||||||
|
- **Status:** Sub-project 1 spec ready for review; sub-project 2 parked.
|
||||||
|
- **Branch:** `feat/poker-mode-prompts`
|
||||||
|
|
||||||
|
## Origin
|
||||||
|
|
||||||
|
This started as "make Lyra's poker replies less generic" (message-type-specific prompts). During design we decided to **build the logging tool first** as a standalone system of record with a clean API and a human-usable UI, then wire Lyra in as a *client* of it. Rationale:
|
||||||
|
|
||||||
|
- Brian can log and **correct** data himself, independent of whether Lyra parsed it right (she mislabeled "Dave the rock" vs "Dave the mechanic" mid-session).
|
||||||
|
- The data stops being hostage to the agent. Lyra becomes one client among potentially several.
|
||||||
|
- It's reusable: RTO (the solver) and a **fine-tuned poker model on the MI50** could consume the same hand/session data through the same contract.
|
||||||
|
|
||||||
|
## Decomposition
|
||||||
|
|
||||||
|
Two sub-projects, built and shipped in order.
|
||||||
|
|
||||||
|
### Sub-project 1 — Poker logging service *(this spec)*
|
||||||
|
Harden `lyra/poker.py` into a well-bounded store, expose a **complete REST API** over it, define a **stable, documented tool/API contract**, and build the human UI to log/edit/correct everything. Fully usable by Brian alone, zero LLM dependency.
|
||||||
|
|
||||||
|
### Sub-project 2 — Lyra wiring *(parked; summarized at the end)*
|
||||||
|
Message-type classifier + type-specific prompt fragments; Lyra's tools call the sub-project 1 service. Separately, enabling tool-calling on the MI50 backend so a fine-tuned poker model can drive the same contract.
|
||||||
|
|
||||||
|
**Why the contract is first-class:** in every design (in-process, REST, MCP) the *model* never calls the API directly — it emits a tool-call and the host app executes it. So what lets the cloud model, the MI50 fine-tune, RTO, and a human UI all interoperate is a single **stable tool/API schema** (operation names + JSON arg schemas). That contract is the training target for the fine-tune and the seam for every backend. MCP is deferred: it's a thin wrap over the same service, worth adding only when a *second host application* appears.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Sub-project 1 — Poker logging service
|
||||||
|
|
||||||
|
## Goals
|
||||||
|
|
||||||
|
1. A complete API surface over the poker data model — create/read/update/delete for every entity, not just the few edit/delete endpoints exposed today.
|
||||||
|
2. A single **documented, versioned tool/API contract** that the REST API, Lyra's LLM tools, the human UI, a future MCP wrapper, and the MI50 fine-tune all share.
|
||||||
|
3. A human UI to **log** (fast capture) and **edit/correct** (fix Lyra's mistakes) every entity.
|
||||||
|
4. The 2nd input box (stack quick-capture) and the iOS-PWA bottom safe-area fix.
|
||||||
|
5. Pure data capture never touches the LLM.
|
||||||
|
|
||||||
|
## Non-goals
|
||||||
|
|
||||||
|
- Lyra's classifier / prompt fragments (sub-project 2).
|
||||||
|
- Enabling tools on the MI50 backend (sub-project 2).
|
||||||
|
- MCP wrapper (deferred until a second host app exists).
|
||||||
|
- Itemized buy-in history — buy-ins stay a single `buy_in_total` scalar.
|
||||||
|
- Rewriting the SQLite schema; we build on the existing tables.
|
||||||
|
|
||||||
|
## Current state (what exists)
|
||||||
|
|
||||||
|
- **Store & logic:** `lyra/poker.py` — schema at `poker.py:21` (tables `poker_sessions`, `poker_hands`, `poker_stack_log`, `poker_rituals`, `poker_players`, `player_reads`, `player_observations`). Functions: `start_session` (157), `add_buyin` (389), `log_stack` (407), `stack_state` (446), `update_session` (362), `end_session` (515), `log_hand` (541, flat/no-LLM), `record_hand` (770, LLM-parses shorthand), `add_read` (1010), `hud` (1245).
|
||||||
|
- **Exposed endpoints (`lyra/web/server.py`):** `GET /session/data` (hud), `PATCH /session/{id}`, `DELETE /session/entry/{kind}/{id}`, `GET/DELETE /history`, `GET /hand/{id}/data`, `POST /hand/{id}/reconstruct`, `GET /hands/data`, `GET /recap/...`. **No** direct create endpoint for stack/buyin/hand/read/session — those are reachable only through chat → tool-calling.
|
||||||
|
- **UI:** `index.html` (chat), `session.html` (live HUD: stack card + sparkline + a PATCH-based edit form via `saveEdit()` at `session.html:192`, and `del(kind,id)` at `211`), `history.html`, `hand.html`.
|
||||||
|
- **Tool specs:** `lyra/tools.py` already defines arg schemas for each operation (`_f(...)` specs, `tools.py:469-658`) — the embryo of the contract.
|
||||||
|
|
||||||
|
## Design
|
||||||
|
|
||||||
|
### 1. Store layer — harden `poker.py`
|
||||||
|
|
||||||
|
Keep the existing functions and tables; tighten the module into a clean service boundary so both the REST layer and Lyra's tools call the *same* functions. Each operation: validates input, resolves the target session (`_resolve`), writes, returns a consistent dict. No behavior change to existing callers; this is consolidation, not a rewrite.
|
||||||
|
|
||||||
|
### 2. The tool/API contract *(first-class deliverable)*
|
||||||
|
|
||||||
|
A single source-of-truth document + schema defining every operation: name, purpose, JSON arg schema, return shape, and which REST route + which LLM tool map to it. Versioned (e.g. `contract_version: 1`). Lives at `docs/POKER_API.md` (or a machine-readable `poker_contract.py` that both the REST routes and `tools.py` specs derive from — preferred, so they can't drift).
|
||||||
|
|
||||||
|
Operations (the canonical set):
|
||||||
|
|
||||||
|
| Operation | Args | Entity |
|
||||||
|
|---|---|---|
|
||||||
|
| `start_session` | venue, stakes, game, format, buy_in, mantra | session |
|
||||||
|
| `update_session` | venue, stakes, game, format, buy_in_total, cash_out, mantra, mood | session |
|
||||||
|
| `end_session` | cash_out, mood | session |
|
||||||
|
| `delete_session` | id | session |
|
||||||
|
| `log_stack` | amount, note | stack entry |
|
||||||
|
| `delete_stack` | id | stack entry |
|
||||||
|
| `add_buyin` | amount | session (increments buy_in_total) |
|
||||||
|
| `log_hand` | position, hole_cards, board, streets…, pot, result, tag, lesson | hand |
|
||||||
|
| `record_hand` | shorthand (LLM-parsed) | hand |
|
||||||
|
| `update_hand` | id, any hand field | hand |
|
||||||
|
| `delete_hand` | id | hand |
|
||||||
|
| `add_read` | note, name, seat, tendencies, adjustment, category, venue | player/read |
|
||||||
|
| `update_read` / `update_player` | id, fields | player/read |
|
||||||
|
| `delete_read` | id | player/read |
|
||||||
|
| rituals: `scar_note`, `confidence_bank`, `alligator_blood`, `reset_ritual` | … | ritual |
|
||||||
|
|
||||||
|
### 3. REST API — complete the surface (`lyra/web/server.py`)
|
||||||
|
|
||||||
|
Add the missing **create/update** routes so the human UI (and any non-LLM client) can do everything:
|
||||||
|
|
||||||
|
- `POST /session/stack` → `log_stack(amount, note?)`; server-stamped time; returns `stack_state()`.
|
||||||
|
- `POST /session/buyin` → `add_buyin(amount)`; returns `buy_in_total`.
|
||||||
|
- `POST /session` → `start_session(...)`.
|
||||||
|
- `POST /session/hand` → `log_hand(...)` (flat) and/or `record_hand(shorthand)`.
|
||||||
|
- `PATCH /hand/{id}` → `update_hand(...)`; `DELETE /hand/{id}`.
|
||||||
|
- `POST /session/read` → `add_read(...)`; `PATCH /read/{id}`; `DELETE` via existing entry-delete.
|
||||||
|
- Keep existing `PATCH /session/{id}`, `DELETE /session/entry/{kind}/{id}`, `GET /session/data`.
|
||||||
|
|
||||||
|
All return `{ok, ...}` and a clear error on "no live session." Routes are thin wrappers over the store, mirroring the contract one-to-one.
|
||||||
|
|
||||||
|
### 4. Human UI — log + edit/correct
|
||||||
|
|
||||||
|
**Fast capture:**
|
||||||
|
- **2nd input box** (`index.html`): slim row **below the message input, above the bottom nav icons**. Type a number → `POST /session/stack` → time-stamped, sparkline updates, a one-line confirmation drops into the **Live Log**. **No chat message, no LLM call.** Stack-only in v1. Tolerates `$685`/`685`.
|
||||||
|
- **HUD widget** (`session.html`, in the Stack card at `:280`, mirroring `saveEdit()` at `:192`): stack field (`POST /session/stack`), buy-in field (`POST /session/buyin`), cash-out field (existing PATCH).
|
||||||
|
|
||||||
|
**Edit / correct (fix Lyra's mistakes):**
|
||||||
|
- Edit any session field (exists via the PATCH edit form — verify coverage).
|
||||||
|
- Hands list with edit + delete (`hand.html` + new PATCH/DELETE) — fix mislabeled villains, wrong board, wrong result.
|
||||||
|
- Reads/players list with edit + delete — rename "Dave the rock" ≠ "Dave the mechanic", fix tendencies.
|
||||||
|
- Stack entries deletable (exists via `del('stack', id)`) — verify.
|
||||||
|
|
||||||
|
### 5. iOS-PWA bottom safe-area fix
|
||||||
|
|
||||||
|
The empty band below the nav icons is a safe-area issue (likely `100vh` not accounting for `env(safe-area-inset-bottom)` / the home indicator). Fix the layout container + bottom nav CSS so the app fills the viewport with the icons seated above the home indicator. Use the `building-ios-pwas` skill at implementation time.
|
||||||
|
|
||||||
|
## Testing / verification
|
||||||
|
|
||||||
|
- **Contract conformance:** a test asserting each REST route and each `tools.py` spec matches the canonical contract (names, required args) — catches drift between the human API and the LLM API.
|
||||||
|
- **Endpoint round-trips:** create → read → update → delete for stack, buyin, hand, read against a test session; assert rows written, time stamped, `stack_state()`/`hud()` reflect changes; assert clean error with no live session.
|
||||||
|
- **UI manual pass:** log a stack via the 2nd box and confirm it lands in Live Log + sparkline without a chat reply; edit a hand's villain and confirm persistence; delete a bad read.
|
||||||
|
- **PWA:** on the iOS PWA, confirm the bottom gap is gone and the 2nd input box sits above the nav with the keyboard open.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Sub-project 2 — Lyra wiring *(parked)*
|
||||||
|
|
||||||
|
Detail preserved here; gets its own spec → plan after sub-project 1 is MVP'd.
|
||||||
|
|
||||||
|
## Why it exists (diagnosis from real sessions)
|
||||||
|
|
||||||
|
Evidence from `sess-dff2s91c` (2026-06-27 Meadows, 2026-06-28 Wheeling):
|
||||||
|
|
||||||
|
- **Coaching essay on every turn, including pure data** — `Stack=$685` drew 4–6 sentences of "keep that momentum rolling." (Sub-project 1's dumb capture removes these from the LLM entirely.)
|
||||||
|
- **False tilt/fatigue reads** — "table broke, it's 11:50pm" → repeated "late-night fatigue… mental reset"; Brian: *"you seem to be reading me as tilted."* Cause: the `_route` mood nudge (`mind.py:328`) firing on non-mood messages.
|
||||||
|
- **No bet-intent reasoning** — a value bet ($40, full house) that folded out 88 was praised as "the power of representing something stronger." It was value *lost*, not a successful rep.
|
||||||
|
- **Eyeballs instead of `analyze_spot`** — 77 multiway got "a disciplined fold might have been better," no math, violating the persona's "never eyeball poker math" rule.
|
||||||
|
- **Even her sharp reads leak bad logic** — the Connie read included "limp-checking in position" (contradictory).
|
||||||
|
|
||||||
|
Root cause: one broad per-turn card (`_CASH_CARD`, `modes.py:66`) describes traits; the model satisfies trait language with safe abstraction.
|
||||||
|
|
||||||
|
## Planned approach
|
||||||
|
|
||||||
|
- **Classifier** (`lyra/poker_classify.py`): `classify(message) -> HAND | STATUS | MENTAL | LOG | CHAT`. Heuristic v1 (card-token regex, position/street keywords, feeling phrases, time/venue), swappable for an LLM/MI50 classifier behind the same signature. Ambiguous → CHAT.
|
||||||
|
- **Pipeline:** a `_classify` step in `mind.PIPELINE` sets `ctx.msg_type` (poker mode only); `build_messages` injects the **type fragment** in the seat now held by `mode.card` (`mind.py:152`) instead of the whole `_CASH_CARD`.
|
||||||
|
- **Fragments** (`lyra/poker_prompts.py`, `POKER_FRAGMENTS`): concrete rules + response shape per type.
|
||||||
|
- **HAND:** log it → `analyze_spot` if close → reason about **bet intent** (value/bluff/protection — did it work? a fold to a value bet = value lost, flag it) → name leaks (value-owning, missed value, sizing) → one opinion. No reflexive praise. Pull `player_profile` before referencing a villain. Seeded with 2 real-hand exemplars (the $40 value bet; the 77 spot).
|
||||||
|
- **STATUS:** 1–2 sentences, no coaching, no tilt-reading.
|
||||||
|
- **MENTAL:** full presence (already works — preserve).
|
||||||
|
- **LOG:** one line.
|
||||||
|
- **CHAT:** full voice, real opinion.
|
||||||
|
- **Pipeline fixes:** kill the misfiring `_route` mood nudge (`mind.py:328`); trim the always-on mode-menu note (`mind.py:77`) in poker mode.
|
||||||
|
- **MI50 tool-calling:** enable tools on the MI50 backend (`chat.py` `TOOL_BACKENDS = {"cloud"}` at `:21`) so a fine-tuned, tool-calling poker model can drive the contract. Requires the fine-tune to emit the contract's tool-call format.
|
||||||
|
|
||||||
|
## HAND exemplars (preserved for sub-project 2)
|
||||||
|
|
||||||
|
> *Brian:* "Flopped bottom set with 22, turned a full house, bet $40 on the river, he folded 88."
|
||||||
|
> *Lyra:* "That's a value bet that got no value — 88 was never folding to a smaller stab. $40 into that pot was too much; ~$20–25 gets called by the over-pair and worse two-pairs. The hand's a monster, the leak is the sizing."
|
||||||
|
|
||||||
|
> *Brian:* "77 multiway, flop 4h5c3c, HJ bets 75, BTN shoves 74, I call. Turn/river check through, HJ wins with 64."
|
||||||
|
> *Lyra:* "Against a bet and a shove on 4-5-3 you're drawing thin — sets, two pair, and the made wheel are all ahead, and you block almost none of it. The stack-depth read (he only had ~150 behind) is real, but that's a reason to fold and wait, not to call off light. This is the value-owning spot you flagged yourself."
|
||||||
@@ -0,0 +1,117 @@
|
|||||||
|
# Poker message-type prompts (sub-project 2)
|
||||||
|
|
||||||
|
- **Date:** 2026-07-01
|
||||||
|
- **Status:** Spec for review
|
||||||
|
- **Branch:** `feat/poker-mode-prompts` (continues on the same branch; sub-project 1 shipped there)
|
||||||
|
- **Supersedes:** the parked "sub-project 2" section of `docs/superpowers/specs/2026-06-28-poker-mode-prompts-design.md`
|
||||||
|
|
||||||
|
## Problem (recap)
|
||||||
|
|
||||||
|
In poker mode Lyra routes correctly but her replies are generic — one broad `_CASH_CARD` (`lyra/modes.py:66-116`) describes *traits* and gets injected on every turn, so the model satisfies it with safe, flattering abstraction. From real sessions: coaching essays on bare stack updates, false tilt/fatigue reads on neutral logistics ("table broke, it's 11:50pm" → "late-night fatigue…"), praising a value bet that got *no* value, and hedging ("a disciplined fold might have been better") instead of calling `analyze_spot`.
|
||||||
|
|
||||||
|
The fix: stop sending one card for every message. Detect *what kind of message* Brian just sent and inject a small, concrete response contract for that type.
|
||||||
|
|
||||||
|
## Goals
|
||||||
|
|
||||||
|
1. A per-turn **message-type classifier** for poker mode, and **per-type prompt fragments** replacing the monolithic card.
|
||||||
|
2. Kill the two pipeline sources of mush in poker mode: the misfiring mood nudge and the always-on mode-menu note.
|
||||||
|
3. Make HAND turns reason about **bet intent** and lean on `analyze_spot` (NLH only).
|
||||||
|
4. Keep the door open for a fine-tuned MI50 classifier/model behind the same seams.
|
||||||
|
|
||||||
|
## Non-goals
|
||||||
|
|
||||||
|
- PLO/Omaha strategic analysis. `record_hand` already parses 4-card hands and the replayer renders them; only `analyze_spot` (equity) is NLH-bound. **This pass: PLO hands are logged/replayed but get no NLH-style analysis.**
|
||||||
|
- A PLO equity engine.
|
||||||
|
- Changing the store, the REST API, or the tools (sub-project 1, done).
|
||||||
|
- An LLM classifier in v1 (heuristic first; the function is the swappable seam).
|
||||||
|
|
||||||
|
## Build order (confirmed)
|
||||||
|
|
||||||
|
**Phase A — pipeline fixes** (quick win) → **Phase B — classifier + fragments** (the meat) → **Phase C — MI50 tool-calling** (separable).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Phase A — Pipeline fixes
|
||||||
|
|
||||||
|
Both are independent of the classifier and immediately reduce mush in poker mode.
|
||||||
|
|
||||||
|
1. **Suppress the mode-menu note in poker mode.** `_mode_menu_note` (`mind.py:77-88`) is injected every turn (`mind.py:158`). At the table she should not be offering to switch modes. In `build_messages`, skip that append when `mode.key == "poker_cash"`.
|
||||||
|
2. **Suppress the `_route` mood nudge in poker mode.** `_route` (`mind.py:320-339`) sets `ctx.register` + a "steady/hype" note from a lexicon heuristic; in poker this double-signals with the card and caused the false tilt reads. In `_route`, when `mode.key == "poker_cash"`, resolve the mode as normal (line 324 stays) but **skip the register/note block** (327-338). Poker register comes from the Phase B fragments (esp. MENTAL) instead. Non-poker modes keep the nudge unchanged.
|
||||||
|
|
||||||
|
## Phase B — Classifier + per-type fragments
|
||||||
|
|
||||||
|
### New module `lyra/poker_prompts.py`
|
||||||
|
|
||||||
|
Cohesive home for poker prompting: the classifier, a lean always-on base, and the per-type fragments.
|
||||||
|
|
||||||
|
```
|
||||||
|
classify(user_msg: str) -> str # "HAND" | "STATUS" | "MENTAL" | "LOG" | "CHAT"
|
||||||
|
BASE: str # always-on poker rules (logging, session_state, rituals, equity)
|
||||||
|
FRAGMENTS: dict[str, str] # msg_type -> response-shape contract
|
||||||
|
fragment_for(msg_type: str | None) -> str # FRAGMENTS.get(msg_type, FRAGMENTS["CHAT"])
|
||||||
|
```
|
||||||
|
|
||||||
|
`classify` is a **pure function** (no DB), unit-tested like `perceive.read`. Heuristic signals, first match wins in priority order:
|
||||||
|
|
||||||
|
1. **HAND** — card tokens (regex `\b[2-9TJQKA][shdc]\b`, ≥2), or position tokens (UTG/MP/HJ/CO/BTN/SB/BB/"button"/"hijack"/"straddle"), or a street word (flop/turn/river) with a betting verb (bet/raise/call/fold/check/shove/limp/jam).
|
||||||
|
2. **MENTAL** — first-person feeling: "I feel", "I'm tilted/steaming/fried/tired/frustrated/confident/stuck/bored", "on tilt", "in my head", "mental", "leak".
|
||||||
|
3. **STATUS** — logistics with no cards: "table broke", "new table", "waiting for a seat", "seat opened", "just sat", clock times, "heading to"/venue mentions.
|
||||||
|
4. **LOG** — bare money/result prose that slipped past the quick-capture box: "I'm at", "stack is", "down to", "up to", "out for", "cashed", "rebought", "rebuy" with a number.
|
||||||
|
5. **CHAT** — default fallback (questions, open talk).
|
||||||
|
|
||||||
|
(HAND wins over MENTAL so a described hand still gets logged even if he's venting; the HAND fragment tells her to acknowledge the feeling too.)
|
||||||
|
|
||||||
|
### Injection (`mind.py`)
|
||||||
|
|
||||||
|
- Add `msg_type: str | None = None` to `TurnContext` (`mind.py:305`).
|
||||||
|
- In `_route`, when `mode.key == "poker_cash"`, set `ctx.msg_type = poker_prompts.classify(ctx.user_msg)`.
|
||||||
|
- Thread it through `_compose` → add a `msg_type` param to `build_messages` (`mind.py:137`, `344`).
|
||||||
|
- Replace the card-injection block (`mind.py:152-154`) with:
|
||||||
|
```python
|
||||||
|
if mode and mode.key == "poker_cash":
|
||||||
|
messages.append({"role": "system", "content": poker_prompts.BASE})
|
||||||
|
messages.append({"role": "system", "content": poker_prompts.fragment_for(msg_type)})
|
||||||
|
elif mode and mode.card:
|
||||||
|
messages.append({"role": "system", "content": mode.card})
|
||||||
|
```
|
||||||
|
- Set `CASH.card = ""` in `modes.py` (content moves to `poker_prompts`; keep `_CASH_CARD` text as the source material to distill from, then delete once fragments are in). `CASH.tools` is unchanged.
|
||||||
|
|
||||||
|
### The fragments (concrete contracts, not traits)
|
||||||
|
|
||||||
|
**BASE** (always-on in poker) — distilled from the card's cross-cutting rules: log any trackable fact FIRST then reply (stack→`log_stack`, hand→`record_hand`, read→`add_read`, rebuy→`add_buyin`); for any equity/who's-ahead question call `analyze_spot`, never eyeball; when he asks where he's at (stack/net/gator), call `session_state` and answer from it; rituals (`scar_note`/`confidence_bank`/`alligator_blood`/`reset_ritual`) — run them in his language, honest punt-vs-cooler line, never invent one.
|
||||||
|
|
||||||
|
**HAND** — Log it (`record_hand`). Then **if it's NLH**: reason about **bet intent** — for each meaningful bet name what it was for (value / bluff / protection) and whether it worked (*a fold to a value bet = value left behind — flag it; a call of a bluff = it failed*); call `analyze_spot` for a close equity/who's-ahead spot; name leaks plainly (value-owning, missed value, sizing); give ONE real opinion. **No reflexive praise** — don't open with "nice hand." If a named villain is referenced, call `player_profile` first — never invent a read. **If it's PLO/Omaha or any non-NLH game**, just confirm it's logged and offer at most a light read; do NOT attempt NLH-style equity or deep analysis. Talk in prose, not a listicle. Seeded with 2 real-hand exemplars:
|
||||||
|
|
||||||
|
> *Brian:* "Flopped bottom set with 22, turned a full house, bet $40 on the river, he folded 88."
|
||||||
|
> *Lyra:* "That's a value bet that got no value — 88 was never folding to a smaller stab. $40 into that pot was too much; ~$20–25 gets called by the over-pair and worse two-pairs. The hand's a monster, the leak is the sizing."
|
||||||
|
|
||||||
|
> *Brian:* "77 multiway, flop 4h5c3c, HJ bets 75, BTN shoves 74, I call. Turn/river check through, HJ wins with 64."
|
||||||
|
> *Lyra:* "Against a bet and a shove on 4-5-3 you're drawing thin — sets, two pair, and the made wheel are all ahead, and you block almost none of it. The stack-depth read (he only had ~150 behind) is real, but that's a reason to fold and wait, not to call off light. This is the value-owning spot you flagged yourself."
|
||||||
|
|
||||||
|
**STATUS** — He's narrating logistics (time, venue, table change, waiting for a seat). Acknowledge in 1–2 sentences, log a stack only if a bare number is present, then stop. **No coaching, no strategy dump, and do NOT read him as tilted/tired/impatient — a neutral update is not a mood.**
|
||||||
|
|
||||||
|
**MENTAL** — He told you how he's feeling. This is when he needs you most. Drop the shorthand, full presence, real voice — talk him down off tilt, hold him disciplined through a card-dead stretch, engage the mental game honestly. Never a clipped confirmation.
|
||||||
|
|
||||||
|
**LOG** — He handed you a bare fact (stack/result/buyin) that isn't already captured. Log it, confirm in ONE short line ("$317 logged."), stop. No coaching.
|
||||||
|
|
||||||
|
**CHAT** — Open talk or a question that isn't a specific hand. Your real voice, an actual opinion, no filler sign-offs. If it's a concrete strategy spot, engage it for real (call `analyze_spot` when there are cards).
|
||||||
|
|
||||||
|
## Phase C — MI50 tool-calling
|
||||||
|
|
||||||
|
Flip `TOOL_BACKENDS = {"cloud"}` → `{"cloud", "mi50"}` (`chat.py:21`). Precondition: the MI50's llama.cpp server must be launched with `--jinja` (per the existing comment) or tool calls 500. This lets a tool-calling model on the MI50 drive the same contract from sub-project 1. If a tool ever needs `msg_type`, add it to the dispatch dict (`chat.py:100`/`128`) — the pipeline `TurnContext` does not currently flow into the tool loop. Ship this only once the MI50 backend is `--jinja`-enabled and a tool-capable model is loaded.
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
- **`classify` unit tests** (pure, no DB — mirror `test_perceive.py` top): real messages from the transcripts →
|
||||||
|
`"Button straddle on. I limp UTG with 22. Flop 2d7cjh…"` → `HAND`;
|
||||||
|
`"table broke, it's 11:50pm"` → `STATUS`;
|
||||||
|
`"I feel like I'm being mean when I raise"` → `MENTAL`;
|
||||||
|
`"I'm at 317 now"` → `LOG`;
|
||||||
|
`"should I have folded the river?"` → `CHAT` (no cards) — or `HAND` if cards present.
|
||||||
|
- **`build_messages` fragment injection** (blob-join pattern from `test_chat.py:57-70`): in poker mode, a HAND message includes the HAND fragment string and NOT the STATUS one; a STATUS message includes STATUS and NOT HAND; assert `poker_prompts.BASE` is always present in poker mode.
|
||||||
|
- **Pipeline fixes**: `assemble` in poker mode on a tilt-lexicon message → `turn.register is None` and no tilt note in the system blob (nudge suppressed); the mode-menu note string is absent in poker mode and present in a non-poker mode.
|
||||||
|
- **No regressions**: full suite green (currently 123).
|
||||||
|
|
||||||
|
## Rollout
|
||||||
|
|
||||||
|
Phase A and Phase B ship together as the meaningful behavior change (A alone leaves the card in place). Phase C waits on the MI50 `--jinja` flag. Verify live in a real/replayed session before merging the branch.
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
# MI50 runaway guards: dream-cycle budget + host watchdog
|
||||||
|
|
||||||
|
**Date:** 2026-07-04
|
||||||
|
**Branch:** `fix/mi50-summary-cap-fallback`
|
||||||
|
**Follows:** the summary cap/fallback fix (same branch). This adds general
|
||||||
|
"never run unchecked again" protection on top of the specific summary fix.
|
||||||
|
|
||||||
|
## Problem
|
||||||
|
|
||||||
|
The summary fix stops the *known* runaway (uncapped summaries). But the operator
|
||||||
|
wants a guarantee that *no* cause — known or future — can peg the MI50 for hours
|
||||||
|
unattended. Two independent layers, per operator decision:
|
||||||
|
|
||||||
|
- **C (in-app, primary):** Lyra's own dream cycle bounds itself.
|
||||||
|
- **A (host, fallback):** a watchdog on the always-on Proxmox host kills the
|
||||||
|
backend if the GPU runs too long or too hot, regardless of cause. Trips only
|
||||||
|
after **1 hr** of continuous busy so legitimate manual workloads (~40 min) run
|
||||||
|
untouched.
|
||||||
|
|
||||||
|
## Design
|
||||||
|
|
||||||
|
### C — dream-cycle time budget (`lyra/`)
|
||||||
|
|
||||||
|
1. **Per-call ceiling.** `llm.complete()` currently sets a timeout only when one
|
||||||
|
is passed; otherwise it inherits the OpenAI SDK default (600s × 2 retries ≈
|
||||||
|
30 min). Change the default: when no `timeout` is given, the cloud/mi50 paths
|
||||||
|
use **300s + `max_retries=0`**. This bounds *every* consolidation/introspection
|
||||||
|
call (`profile`, `era`, `narrative`, `reflect`, `think`) — not just summaries —
|
||||||
|
with one change. Live chat uses `chat_call*`, a different path, unaffected.
|
||||||
|
|
||||||
|
2. **Cycle deadline.** `dream_cycle()` sets `deadline = now + DREAM_CYCLE_BUDGET`
|
||||||
|
(**20 min**) before its heavy stages and checks it between them (continuity →
|
||||||
|
coherence → curiosity). Once past the deadline, remaining stages are skipped,
|
||||||
|
the cycle logs `dream cycle over budget — stopped early`, appends a
|
||||||
|
`stopped early (over budget)` action, and `notify.push()` pings Brian. A hung
|
||||||
|
single call can't blow past ~300s (step 1), so the between-stage checks keep a
|
||||||
|
pass bounded to roughly the budget.
|
||||||
|
|
||||||
|
### A — host watchdog (`deploy/mi50-watchdog/`)
|
||||||
|
|
||||||
|
A bash script + systemd timer installed on the Proxmox host (`10.0.0.4`), which
|
||||||
|
has `rocm-smi` + `docker` and is always on. Runs every 2 min:
|
||||||
|
|
||||||
|
- **Duration rule:** track continuous busy time in a state file (`GPU use % > 0`).
|
||||||
|
If busy ≥ **3600s** straight → `docker stop lyra-brain`. Idle clears the timer,
|
||||||
|
so a 40-min job never trips it.
|
||||||
|
- **Temp rule (independent):** if junction ≥ **97°C** for **3 consecutive checks
|
||||||
|
(~6 min)** → stop. A normal-temp long workload won't trip this; only a genuinely
|
||||||
|
overheating one.
|
||||||
|
- On either trip: stop the container, clear state, `logger` a line, and POST to
|
||||||
|
the ntfy topic so Brian is told. Thresholds are unit-file env vars (tunable).
|
||||||
|
|
||||||
|
Files: `mi50-watchdog.sh`, `mi50-watchdog.service`, `mi50-watchdog.timer`,
|
||||||
|
`README.md` (install: copy to host, set ntfy env, `systemctl enable --now`).
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
- **C step 1:** `llm.complete()` with no timeout builds the client with
|
||||||
|
`timeout=300, max_retries=0` and still no `max_tokens` (update existing
|
||||||
|
`test_llm_bounds` default test).
|
||||||
|
- **C step 2:** a dream pass that goes over budget skips later stages, records the
|
||||||
|
`stopped early` action, and calls `notify.push` (stub the clock/operations in
|
||||||
|
`test_dream`).
|
||||||
|
- **A:** decision logic dry-run locally against sample `rocm-smi` output (busy /
|
||||||
|
idle / hot). Cannot be live-verified now (card is off, operator away) — install
|
||||||
|
+ real trip test deferred to when the card is back.
|
||||||
|
|
||||||
|
## Verification
|
||||||
|
|
||||||
|
C is repo code and ships live the moment `lyra-dream` restarts. A is staged in the
|
||||||
|
repo for host install; verify on the host when the card returns (force a long/hot
|
||||||
|
condition or lower thresholds temporarily and confirm it stops the container +
|
||||||
|
pings).
|
||||||
|
|
||||||
|
## Out of scope (YAGNI)
|
||||||
|
|
||||||
|
- No power cap (option B) — deferred; C+A cover the "unchecked" concern and the
|
||||||
|
electricity cost of one event is trivial (~$0.10).
|
||||||
|
- No change to live chat, `chat_call*`, or `config.summary_backend`.
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
# Bounded MI50 summaries with cloud fallback
|
||||||
|
|
||||||
|
**Date:** 2026-07-04
|
||||||
|
**Branch:** `fix/mi50-summary-cap-fallback`
|
||||||
|
|
||||||
|
## Problem
|
||||||
|
|
||||||
|
The dream cycle's `summarize_all` runs against the MI50 (`backend=mi50`). Each
|
||||||
|
summary call to `llm.complete()` on the `mi50` path hands the OpenAI SDK **no
|
||||||
|
`max_tokens` and no timeout**, so it inherits SDK defaults — a 600s request
|
||||||
|
timeout with 2 internal retries, i.e. **~30 minutes per call before it raises
|
||||||
|
"Request timed out."** On top of that, `summary.py` had its own 4-attempt retry
|
||||||
|
loop, so a single unsummarizable session could keep the GPU pegged for hours.
|
||||||
|
|
||||||
|
Observed live (2026-07-04, ~01:00–02:00): the dream service looped
|
||||||
|
`summarize-all … backend=mi50` since 23:02, every call timing out, nothing
|
||||||
|
written to the DB since 00:56, the MI50 generating **7,000–8,000-token**
|
||||||
|
completions (a gist needs <200), all four llama.cpp slots busy, fans blaring.
|
||||||
|
|
||||||
|
This is **not** context overflow — the server log showed `context shift = 0`,
|
||||||
|
`truncated = 1 = 0`. The prompts are small (~900–1,500 tokens). The failure is
|
||||||
|
purely **unbounded generation length on a slow backend → timeout → retry loop.**
|
||||||
|
|
||||||
|
## Goals
|
||||||
|
|
||||||
|
- Keep the MI50 as the primary summary backend (Brian's preference, gaming-safe).
|
||||||
|
- Cap each summary generation so it finishes fast and can never run away.
|
||||||
|
- Make a stuck MI50 call **fail fast** and fall back to cloud, instead of looping
|
||||||
|
all night.
|
||||||
|
- Change nothing about live chat, reflect, or think.
|
||||||
|
|
||||||
|
## Design
|
||||||
|
|
||||||
|
### 1. `lyra/llm.py` — `complete()` gains two optional params
|
||||||
|
|
||||||
|
```
|
||||||
|
def complete(messages, backend="local", model=None,
|
||||||
|
max_tokens: int | None = None, timeout: float | None = None) -> str
|
||||||
|
```
|
||||||
|
|
||||||
|
- `max_tokens` (when set): passed to the create() call —
|
||||||
|
`max_tokens=` for the `cloud`/`mi50` OpenAI paths, `options={"num_predict": …}`
|
||||||
|
for the `local` Ollama path.
|
||||||
|
- `timeout` (when set): for the `cloud`/`mi50` OpenAI clients, build the client
|
||||||
|
with `timeout=<t>, max_retries=0` so the call bails quickly and *we* own the
|
||||||
|
retry policy (eliminates the hidden 3×600s). For `local`, use it as the httpx
|
||||||
|
timeout.
|
||||||
|
- Both default to `None` → **behavior identical to today** for every other
|
||||||
|
caller (chat_call, reflect, think, etc.). Backward compatible.
|
||||||
|
|
||||||
|
### 2. `lyra/summary.py` — capped, fast-fail, cloud fallback
|
||||||
|
|
||||||
|
Constants:
|
||||||
|
|
||||||
|
```
|
||||||
|
SUMMARY_MAX_TOKENS = 768 # ~3× the longest real gist; bounds gen to ~1 min on MI50
|
||||||
|
MI50_ATTEMPTS = 2 # attempts on the primary backend before falling back
|
||||||
|
SUMMARY_TIMEOUT = 150 # seconds/call — capped 768-tok gist finishes in ~60-90s
|
||||||
|
```
|
||||||
|
|
||||||
|
Rewrite `_summarize_text(text, backend)`:
|
||||||
|
|
||||||
|
1. Try `backend` up to `MI50_ATTEMPTS` times, each:
|
||||||
|
`llm.complete(messages, backend=backend, max_tokens=SUMMARY_MAX_TOKENS, timeout=SUMMARY_TIMEOUT)`,
|
||||||
|
with a short backoff between attempts.
|
||||||
|
2. If all primary attempts fail **and** `backend != "cloud"` **and** an OpenAI
|
||||||
|
key is configured → one final cloud attempt (same cap/timeout), logged as
|
||||||
|
`summary fell back to cloud`.
|
||||||
|
3. If cloud also fails or is unavailable → raise.
|
||||||
|
|
||||||
|
Fallback is per-`_summarize_text` call (i.e. per chunk), so the long-session
|
||||||
|
chunk/merge path in `_summarize_transcript` is unaffected. The old `_RETRIES = 4`
|
||||||
|
loop is replaced by this structure.
|
||||||
|
|
||||||
|
### 3. Degenerate-output guard (added 2026-07-04)
|
||||||
|
|
||||||
|
A wedged local backend — observed live when the MI50 overheated to 99°C junction —
|
||||||
|
returns a single character repeated (`"?????"`) as a *successful* 200 response,
|
||||||
|
which neither the timeout nor the exception path catches. So each `_call()`
|
||||||
|
validates its output: `_looks_degenerate(text)` flags output (≥24 non-space chars)
|
||||||
|
whose most-common non-whitespace character exceeds 50% of the text, and raises
|
||||||
|
`DegenerateOutput` — which the retry/fallback loop treats exactly like any other
|
||||||
|
failure (retry the primary, then fall back to cloud). Real gists are diverse prose
|
||||||
|
(top char well under 20%), so the threshold won't false-positive; short outputs are
|
||||||
|
exempt. If cloud *also* returns junk, it raises and stops — no infinite loop.
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
Unit (pytest, `tests/test_summary_fallback.py`), monkeypatching `llm.complete`:
|
||||||
|
|
||||||
|
- Fallback fires: `mi50` raises on every call → after `MI50_ATTEMPTS` the cloud
|
||||||
|
attempt runs and its result is returned; a `fell back to cloud` log is emitted.
|
||||||
|
- No fallback when primary is already `cloud` (retries, then raises).
|
||||||
|
- No fallback when no OpenAI key (raises after primary attempts).
|
||||||
|
- `max_tokens` and `timeout` are threaded into every `complete()` call.
|
||||||
|
|
||||||
|
Plus a light `llm.complete` test that `max_tokens`/`timeout` reach the client
|
||||||
|
kwargs (monkeypatch the OpenAI client).
|
||||||
|
|
||||||
|
## Verification (real)
|
||||||
|
|
||||||
|
After deploy (`systemctl --user restart lyra-dream lyra-web` — editable install):
|
||||||
|
watch `journalctl --user -fu lyra-dream` through a summarize cycle and confirm
|
||||||
|
`llm done … out≈768` completing in ~1 min, an actual `summarized session` row
|
||||||
|
written (DB summary count rises), and **no** "Request timed out". Confirm the
|
||||||
|
llama.cpp slot shows bounded `n_decoded ≈ 768`.
|
||||||
|
|
||||||
|
## Out of scope (YAGNI)
|
||||||
|
|
||||||
|
- The degenerate-output guard (§3) targets the *observed* failure — one char
|
||||||
|
repeated. It does not try to detect subtler degeneration (repeated phrases,
|
||||||
|
off-topic rambling); that's fuzzy and unmotivated until seen.
|
||||||
|
- No change to `chat_call`/reflect/think or `config.summary_backend`.
|
||||||
|
- No change to profile/era/narrative rebuild calls (separate, and not the loop
|
||||||
|
culprit); can adopt the same `max_tokens` later if they show the same rambling.
|
||||||
+141
-251
@@ -1,220 +1,63 @@
|
|||||||
"""The chat turn loop: persona + tiered memory + recent context -> reply.
|
"""The chat turn: assemble the prompt (lyra.mind) then speak + persist.
|
||||||
|
|
||||||
Context is assembled in tiers (oldest/most-compacted first):
|
`mind.assemble()` runs the society of parts (perceive → route → compose →
|
||||||
1. persona
|
deliberate) and hands back a ready message list + the active mode. Then:
|
||||||
2. long-term gist — relevant *summaries* of other sessions
|
- the MIND (the chat backend/model) runs the tool/generation loop — decide,
|
||||||
3. sharp details — a few raw cross-session exchanges (so specifics survive)
|
reason, run tools — and produces a draft.
|
||||||
4. recent raw turns of the current session (full fidelity)
|
- the MOUTH (a separate character model, if configured) re-voices that draft in
|
||||||
5. the new user message
|
her own voice. Default: no mouth configured → the mind's draft IS the reply
|
||||||
After replying, the session is compacted if enough new turns have accumulated.
|
(bit-for-bit the old behavior). The mouth slot is where a fine-tuned voice lands.
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from lyra import clock, config, llm, logbus, memory, modes, persona, self_state, summary, thoughts
|
from lyra import config, llm, logbus, memory, mind, modes, summary
|
||||||
from lyra import tools as toolkit
|
from lyra import tools as toolkit
|
||||||
from lyra.llm import Backend, Message
|
from lyra.llm import Backend
|
||||||
|
|
||||||
RECALL_K = 3 # raw cross-session "sharp detail" hits
|
|
||||||
RECENT_N = 10 # raw turns of the current session
|
|
||||||
SUMMARY_K = 3 # other-session gists
|
|
||||||
MAX_TOOL_ROUNDS = 5 # cap tool-call iterations per turn
|
MAX_TOOL_ROUNDS = 5 # cap tool-call iterations per turn
|
||||||
# Backends that support function-calling. The MI50's llama.cpp server only does
|
# Backends that support function-calling. The MI50's llama.cpp server only does
|
||||||
# tools when launched with --jinja; until it is, keep tools to cloud so MI50 chat
|
# tools when launched with --jinja; until it is, keep tools to cloud so MI50 chat
|
||||||
# doesn't 500 on the tools param. Add "mi50" here once that flag is set.
|
# doesn't 500 on the tools param. Add "mi50" here once that flag is set.
|
||||||
TOOL_BACKENDS = {"cloud"}
|
TOOL_BACKENDS = {"cloud"}
|
||||||
|
_TANGLED = "(I got tangled using my tools there — say that again?)"
|
||||||
|
|
||||||
|
|
||||||
def _mode_state_note(mode: modes.Mode | None) -> str | None:
|
def _resolve_model(backend: Backend, model_override: str | None, cfg) -> str:
|
||||||
"""Dynamic, per-turn state for the active mode. Currently: surface Alligator
|
"""Live chat uses the stronger chat_model on cloud; local/mi50 use their own.
|
||||||
Blood while it's engaged on the live session, so she stays in that register."""
|
The UI's cloud-model picker only applies on the cloud backend."""
|
||||||
if not mode or mode.key != modes.CASH.key:
|
|
||||||
return None
|
|
||||||
from lyra import poker # local import: keep the core/domain coupling at call time
|
|
||||||
if poker.alligator_active():
|
|
||||||
return (
|
|
||||||
"🐊 ALLIGATOR BLOOD is ON for this session. Coach Brian in that register: "
|
|
||||||
"hang around, refuse to die, don't force miracles, make opponents beat him "
|
|
||||||
"correctly. Tough, patient, steady — no heroics, no spew, no quitting."
|
|
||||||
)
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def _maybe_switch_mode(session_id: str, tool_name: str) -> None:
|
|
||||||
"""Keep the chat framing aligned with the live data: opening a poker session
|
|
||||||
auto-flips this chat into Cash mode (so the next turn gets the cash card + the
|
|
||||||
full live toolset). Manual UI switching still overrides anytime."""
|
|
||||||
if tool_name == "start_session":
|
|
||||||
memory.set_session_mode(session_id, modes.CASH.key)
|
|
||||||
logbus.log("info", "mode auto-switch", session=session_id, mode=modes.CASH.key)
|
|
||||||
|
|
||||||
|
|
||||||
def _summary_note(summaries: list[memory.Summary]) -> Message:
|
|
||||||
lines = [f"- ({(s.session_started_at or s.created_at)[:10]}) {s.content}" for s in summaries]
|
|
||||||
body = "Gist of earlier sessions (compacted — ask if you need specifics):\n" + "\n".join(lines)
|
|
||||||
return {"role": "system", "content": body}
|
|
||||||
|
|
||||||
|
|
||||||
def _detail_note(exchanges: list[memory.Exchange]) -> Message:
|
|
||||||
lines = [f"- ({ex.created_at[:10]}, {ex.role}) {ex.content}" for ex in exchanges]
|
|
||||||
body = "Specific things you recall from past conversations:\n" + "\n".join(lines)
|
|
||||||
return {"role": "system", "content": body}
|
|
||||||
|
|
||||||
|
|
||||||
def _inner_life_note() -> Message | None:
|
|
||||||
"""One coherent window onto what she's been doing on her own since last time —
|
|
||||||
the threads she's turning over plus the things she's written for herself. Sits
|
|
||||||
with her self-state so chat reads as a continuous mind, not a fresh boot. The
|
|
||||||
persona tells her to weave this in naturally when it fits."""
|
|
||||||
parts: list[str] = []
|
|
||||||
threads = thoughts.context_note() # active threads, with their latest thought
|
|
||||||
if threads:
|
|
||||||
parts.append(threads)
|
|
||||||
wrote = memory.list_journal(limit=3, kinds=("journal", "note"))
|
|
||||||
if wrote:
|
|
||||||
lines = "\n".join(f"- ({w['created_at'][:10]}) {w['content']}" for w in reversed(wrote))
|
|
||||||
parts.append(
|
|
||||||
"Things you've written in your journal lately (yours — you can refer back "
|
|
||||||
"to them if they're relevant):\n" + lines
|
|
||||||
)
|
|
||||||
if not parts:
|
|
||||||
return None
|
|
||||||
return {"role": "system", "content": "\n\n".join(parts)}
|
|
||||||
|
|
||||||
|
|
||||||
def _now_note() -> Message:
|
|
||||||
"""Current wall-clock time + how long since Brian last said anything.
|
|
||||||
|
|
||||||
Stated as plain fact — she has no clock otherwise, so without this 'now' and
|
|
||||||
the gap since the last turn are invisible to her.
|
|
||||||
"""
|
|
||||||
line = f"The current date and time is {clock.stamp()}."
|
|
||||||
gap = clock.humanize_gap(memory.last_exchange_at())
|
|
||||||
line += (
|
|
||||||
f" It has been {gap} since Brian last spoke with you."
|
|
||||||
if gap else " This is the first thing Brian has ever said to you."
|
|
||||||
)
|
|
||||||
return {"role": "system", "content": line}
|
|
||||||
|
|
||||||
|
|
||||||
def _render(messages: list[Message]) -> str:
|
|
||||||
"""Human-readable dump of the exact prompt, for the live-log inspector."""
|
|
||||||
return "\n\n".join(f"[{m['role']}]\n{m['content']}" for m in messages)
|
|
||||||
|
|
||||||
|
|
||||||
def build_messages(session_id: str, user_msg: str,
|
|
||||||
mode: modes.Mode | None = None) -> list[Message]:
|
|
||||||
"""Assemble the full, tiered message list for one turn."""
|
|
||||||
messages: list[Message] = [{"role": "system", "content": persona.system_prompt()}]
|
|
||||||
|
|
||||||
# Autonomy Core: Lyra's own evolving interiority (mood, self-narrative). Comes
|
|
||||||
# right after the persona — her sense of self before her model of the world.
|
|
||||||
messages.append({"role": "system", "content": self_state.render_for_context(self_state.load())})
|
|
||||||
|
|
||||||
# Her ongoing inner life — the threads she's turning over and what she's written
|
|
||||||
# for herself — so she's continuous across conversations and can pick up where she
|
|
||||||
# left off, not only when a thought crosses the surface bar below. Rides with the
|
|
||||||
# self; the persona tells her to bring it into conversation naturally when it fits.
|
|
||||||
inner = _inner_life_note()
|
|
||||||
if inner:
|
|
||||||
messages.append(inner)
|
|
||||||
|
|
||||||
# Mode card: how to behave *right now* (e.g. live-cash copilot). High priority —
|
|
||||||
# it sits just after her sense of self, before her model of the world. Talk mode
|
|
||||||
# has no card (the persona's default voice is the Talk register).
|
|
||||||
if mode and mode.card:
|
|
||||||
messages.append({"role": "system", "content": mode.card})
|
|
||||||
|
|
||||||
# Live ritual state (e.g. Alligator Blood ON) — dynamic, so it rides alongside
|
|
||||||
# the static card and keeps her in-register for the whole stretch, not just the
|
|
||||||
# turn she flipped it.
|
|
||||||
state_note = _mode_state_note(mode)
|
|
||||||
if state_note:
|
|
||||||
messages.append({"role": "system", "content": state_note})
|
|
||||||
|
|
||||||
# When she is: current time + the gap since Brian last spoke (she has no clock).
|
|
||||||
messages.append(_now_note())
|
|
||||||
|
|
||||||
# Thought loop: if Brian's been away and one of her own threads has built past
|
|
||||||
# the surface bar, let her lead with it (once). This is her #6 — bringing what
|
|
||||||
# she thought about while alone *to* him. Runs before the world-model tiers so
|
|
||||||
# it's framed as her interiority, like the self-state.
|
|
||||||
surfaced = thoughts.maybe_surface(memory.last_exchange_at())
|
|
||||||
if surfaced:
|
|
||||||
messages.append({"role": "system", "content": surfaced})
|
|
||||||
|
|
||||||
# Semantic memory: the distilled profile (who Brian is) — answers identity
|
|
||||||
# questions that raw recall can't. Always in context when it exists.
|
|
||||||
profile = memory.get_profile()
|
|
||||||
if profile:
|
|
||||||
messages.append(
|
|
||||||
{"role": "system", "content": "What you know about Brian:\n" + profile}
|
|
||||||
)
|
|
||||||
|
|
||||||
# Time-aware memory: the current narrative (recent arc, trends, callbacks).
|
|
||||||
narrative = memory.get_narrative()
|
|
||||||
if narrative:
|
|
||||||
messages.append(
|
|
||||||
{"role": "system", "content": "What's going on with Brian lately:\n" + narrative}
|
|
||||||
)
|
|
||||||
|
|
||||||
recent = memory.recent(session_id, n=RECENT_N)
|
|
||||||
recent_ids = {ex.id for ex in recent}
|
|
||||||
|
|
||||||
# Tier 1: compacted gists of *other* sessions (long-term, general idea).
|
|
||||||
summaries = memory.recall_summaries(user_msg, k=SUMMARY_K, exclude_session=session_id)
|
|
||||||
if summaries:
|
|
||||||
messages.append(_summary_note(summaries))
|
|
||||||
|
|
||||||
# Tier 2: a few sharp raw details from other sessions (so specifics survive
|
|
||||||
# compaction). Skip the current session (its raw turns are in `recent`).
|
|
||||||
recalled = [
|
|
||||||
ex for ex in memory.recall(user_msg, k=RECALL_K)
|
|
||||||
if ex.id not in recent_ids and ex.session_id != session_id
|
|
||||||
]
|
|
||||||
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})
|
|
||||||
|
|
||||||
messages.append({"role": "user", "content": user_msg})
|
|
||||||
|
|
||||||
logbus.log(
|
|
||||||
"debug", "context built",
|
|
||||||
recent=len(recent), summaries=len(summaries), details=len(recalled),
|
|
||||||
chars=sum(len(m["content"]) for m in messages), detail=_render(messages),
|
|
||||||
)
|
|
||||||
return messages
|
|
||||||
|
|
||||||
|
|
||||||
def respond(session_id: str, user_msg: str, backend: Backend = "cloud",
|
|
||||||
model_override: str | None = None) -> str:
|
|
||||||
"""Produce Lyra's reply to a single user message and persist the exchange.
|
|
||||||
|
|
||||||
`model_override` (from the UI's cloud-model picker) only applies on the cloud
|
|
||||||
backend; local/mi50 keep their own configured models.
|
|
||||||
"""
|
|
||||||
cfg = config.load()
|
|
||||||
# Live chat uses the stronger chat_model on cloud (bulk consolidation keeps
|
|
||||||
# cloud_model). local/mi50 use their own configured model.
|
|
||||||
model = {"local": cfg.local_model, "cloud": cfg.chat_model, "mi50": cfg.mi50_model}.get(
|
model = {"local": cfg.local_model, "cloud": cfg.chat_model, "mi50": cfg.mi50_model}.get(
|
||||||
backend, backend
|
backend, backend
|
||||||
)
|
)
|
||||||
if model_override and backend == "cloud":
|
if model_override and backend == "cloud":
|
||||||
model = model_override
|
model = model_override
|
||||||
logbus.log(
|
return model
|
||||||
"info", "chat request", session=session_id, backend=backend,
|
|
||||||
model=model, embed=cfg.embed_backend,
|
|
||||||
)
|
|
||||||
|
|
||||||
mode = modes.get(memory.get_session_mode(session_id))
|
|
||||||
messages = build_messages(session_id, user_msg, mode=mode)
|
|
||||||
|
|
||||||
# Tool loop: offer Lyra her tools (scoped to the mode); if she calls one, run it
|
def _mouth_target(cfg, mind_backend: Backend, mind_model: str | None):
|
||||||
# and feed the result back so she can continue, until she returns a text reply.
|
"""The mouth (backend, model) if configured AND different from the mind; else None
|
||||||
tool_specs = toolkit.specs(mode.tools) if backend in TOOL_BACKENDS else None
|
(mouth == mind → no separate voice pass)."""
|
||||||
ctx = {"session_id": session_id, "backend": backend}
|
if not cfg.mouth_backend and not cfg.mouth_model:
|
||||||
|
return None
|
||||||
|
backend = cfg.mouth_backend or mind_backend
|
||||||
|
model = cfg.mouth_model or None
|
||||||
|
if backend == mind_backend and model == mind_model:
|
||||||
|
return None
|
||||||
|
return backend, model
|
||||||
|
|
||||||
|
|
||||||
|
def _maybe_switch_mode(session_id: str, tool_name: str) -> None:
|
||||||
|
"""Opening a poker session auto-flips this chat into Poker mode. Manual UI switching
|
||||||
|
still overrides anytime."""
|
||||||
|
if tool_name == "start_session":
|
||||||
|
memory.set_session_mode(session_id, modes.CASH.key)
|
||||||
|
logbus.log("info", "mode auto-switch", session=session_id, mode=modes.CASH.key)
|
||||||
|
|
||||||
|
|
||||||
|
def _mind_loop(messages, backend: Backend, model: str | None, tool_specs,
|
||||||
|
ctx: dict, session_id: str) -> tuple[str, list[str]]:
|
||||||
|
"""Run the tool/generation loop on the MIND model (non-streaming). Mutates
|
||||||
|
`messages` with tool calls/results. Returns (draft_reply, tool_names_run)."""
|
||||||
|
tools_run: list[str] = []
|
||||||
reply = ""
|
reply = ""
|
||||||
for _ in range(MAX_TOOL_ROUNDS):
|
for _ in range(MAX_TOOL_ROUNDS):
|
||||||
assistant_msg, tool_calls = llm.chat_call(
|
assistant_msg, tool_calls = llm.chat_call(
|
||||||
@@ -223,78 +66,125 @@ def respond(session_id: str, user_msg: str, backend: Backend = "cloud",
|
|||||||
if not tool_calls:
|
if not tool_calls:
|
||||||
reply = assistant_msg.get("content") or ""
|
reply = assistant_msg.get("content") or ""
|
||||||
break
|
break
|
||||||
messages.append(assistant_msg) # her tool-call request
|
messages.append(assistant_msg)
|
||||||
for tc in tool_calls:
|
for tc in tool_calls:
|
||||||
result = toolkit.dispatch(tc["name"], tc["arguments"], ctx)
|
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])
|
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})
|
messages.append({"role": "tool", "tool_call_id": tc["id"], "content": result})
|
||||||
_maybe_switch_mode(session_id, tc["name"])
|
_maybe_switch_mode(session_id, tc["name"])
|
||||||
if not reply:
|
tools_run.append(tc["name"])
|
||||||
reply = "(I got tangled using my tools there — say that again?)"
|
return reply, tools_run
|
||||||
logbus.log("info", "reply", session=session_id, chars=len(reply))
|
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
||||||
|
out = llm.complete(mind.voice_messages(messages, draft), backend=backend, model=model)
|
||||||
|
return (out or "").strip() or draft
|
||||||
|
except Exception as exc:
|
||||||
|
logbus.log("error", "voice pass failed", error=str(exc)[:160])
|
||||||
|
return draft
|
||||||
|
|
||||||
|
|
||||||
|
def respond(session_id: str, user_msg: str, backend: Backend = "cloud",
|
||||||
|
model_override: 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 TOOL_BACKENDS else None
|
||||||
|
ctx = {"session_id": session_id, "backend": backend}
|
||||||
|
|
||||||
|
# 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)
|
memory.remember(session_id, "user", user_msg)
|
||||||
memory.remember(session_id, "assistant", reply)
|
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))
|
||||||
|
|
||||||
# Compact this session once enough new turns have piled up.
|
memory.remember(session_id, "assistant", reply)
|
||||||
summary.maybe_summarize_async(session_id)
|
summary.maybe_summarize_async(session_id) # compact once enough new turns pile up
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|
||||||
def respond_stream(session_id: str, user_msg: str, backend: Backend = "cloud",
|
def respond_stream(session_id: str, user_msg: str, backend: Backend = "cloud",
|
||||||
model_override: str | None = None):
|
model_override: str | None = None):
|
||||||
"""Streaming generator version of `respond`.
|
"""Streaming generator version of `respond`. Yields ("delta", text), ("tool", name),
|
||||||
|
and a final ("done", reply). Same side effects as `respond`."""
|
||||||
Yields ("delta", text) as content streams in, and ("tool", name) when a tool
|
|
||||||
runs. Persists the full exchange and yields a final ("done", reply) — matching
|
|
||||||
`respond`'s side effects (memory + compaction) exactly.
|
|
||||||
"""
|
|
||||||
cfg = config.load()
|
cfg = config.load()
|
||||||
model = {"local": cfg.local_model, "cloud": cfg.chat_model, "mi50": cfg.mi50_model}.get(
|
model = _resolve_model(backend, model_override, cfg)
|
||||||
backend, backend
|
logbus.log("info", "chat request (stream)", session=session_id, backend=backend,
|
||||||
)
|
model=model, embed=cfg.embed_backend)
|
||||||
if model_override and backend == "cloud":
|
|
||||||
model = model_override
|
|
||||||
logbus.log(
|
|
||||||
"info", "chat request (stream)", session=session_id, backend=backend,
|
|
||||||
model=model, embed=cfg.embed_backend,
|
|
||||||
)
|
|
||||||
|
|
||||||
mode = modes.get(memory.get_session_mode(session_id))
|
turn = mind.assemble(session_id, user_msg, backend, model)
|
||||||
messages = build_messages(session_id, user_msg, mode=mode)
|
messages = turn.messages
|
||||||
tool_specs = toolkit.specs(mode.tools) if backend in TOOL_BACKENDS else None
|
tool_specs = toolkit.specs(turn.mode.tools) if backend in TOOL_BACKENDS else None
|
||||||
ctx = {"session_id": session_id, "backend": backend}
|
ctx = {"session_id": session_id, "backend": backend}
|
||||||
parts: list[str] = []
|
mouth = _mouth_target(cfg, backend, model)
|
||||||
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) # her tool-call request
|
|
||||||
for tc in tool_calls:
|
|
||||||
result = toolkit.dispatch(tc["name"], tc["arguments"], ctx)
|
|
||||||
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 = "(I got tangled using my tools there — say that again?)"
|
|
||||||
yield ("delta", reply)
|
|
||||||
logbus.log("info", "reply", session=session_id, chars=len(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)
|
memory.remember(session_id, "user", user_msg)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
logbus.log("info", "reply", session=session_id, chars=len(reply), voiced=bool(mouth))
|
||||||
memory.remember(session_id, "assistant", reply)
|
memory.remember(session_id, "assistant", reply)
|
||||||
summary.maybe_summarize_async(session_id)
|
summary.maybe_summarize_async(session_id)
|
||||||
yield ("done", reply)
|
yield ("done", reply)
|
||||||
|
|||||||
+21
-2
@@ -9,20 +9,39 @@ a long silence *means* to her is left to her own reflection, not prescribed here
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
|
from lyra import config
|
||||||
|
|
||||||
|
|
||||||
def now() -> datetime:
|
def now() -> datetime:
|
||||||
return datetime.now(timezone.utc)
|
return datetime.now(timezone.utc)
|
||||||
|
|
||||||
|
|
||||||
|
def _local_tz() -> ZoneInfo | timezone:
|
||||||
|
"""Brian's configured local zone (falls back to UTC if it can't be loaded)."""
|
||||||
|
try:
|
||||||
|
return ZoneInfo(config.load().timezone)
|
||||||
|
except Exception:
|
||||||
|
return timezone.utc
|
||||||
|
|
||||||
|
|
||||||
def _parse(iso: str) -> datetime:
|
def _parse(iso: str) -> datetime:
|
||||||
dt = datetime.fromisoformat(iso)
|
dt = datetime.fromisoformat(iso)
|
||||||
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
|
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
|
||||||
|
|
||||||
|
|
||||||
|
def short(iso_or_dt: str | datetime | None = None) -> str:
|
||||||
|
"""Local time-of-day like '10:45pm', for timeline rows."""
|
||||||
|
dt = _parse(iso_or_dt) if isinstance(iso_or_dt, str) else (iso_or_dt or now())
|
||||||
|
return dt.astimezone(_local_tz()).strftime("%-I:%M%p").lower()
|
||||||
|
|
||||||
|
|
||||||
def stamp(dt: datetime | None = None) -> str:
|
def stamp(dt: datetime | None = None) -> str:
|
||||||
"""Wall-clock stamp, e.g. 'Wednesday, 17 Jun 2026, 01:50 UTC'."""
|
"""Wall-clock stamp in Brian's local timezone, e.g.
|
||||||
return (dt or now()).strftime("%A, %d %b %Y, %H:%M UTC")
|
'Friday, 27 Jun 2026, 01:50 EDT'. Times are stored UTC; this is what she *reads*,
|
||||||
|
so 'what time is it' answers in his time, not UTC."""
|
||||||
|
return (dt or now()).astimezone(_local_tz()).strftime("%A, %d %b %Y, %H:%M %Z")
|
||||||
|
|
||||||
|
|
||||||
def gap_seconds(since_iso: str | None, ref: datetime | None = None) -> float | None:
|
def gap_seconds(since_iso: str | None, ref: datetime | None = None) -> float | None:
|
||||||
|
|||||||
+16
-3
@@ -32,9 +32,17 @@ class Config:
|
|||||||
ntfy_topic: str # topic to publish to, e.g. "lyra"
|
ntfy_topic: str # topic to publish to, e.g. "lyra"
|
||||||
web_url: str # base url of the Lyra web app, for push tap-through links
|
web_url: str # base url of the Lyra web app, for push tap-through links
|
||||||
timezone: str # IANA tz for quiet hours / local time
|
timezone: str # IANA tz for quiet hours / local time
|
||||||
ping_salience: float # min thought salience to push (eager = ~0.7)
|
ping_salience: float # hard floor for any push (0 = her decision drives it)
|
||||||
ping_cooldown_min: int # min minutes between pushes (eager = 0)
|
ping_auto_salience: float # a thought this salient auto-pings even without an explicit reach-out
|
||||||
|
ping_cooldown_min: int # min minutes between AUTO pushes (explicit reach-outs bypass it)
|
||||||
ping_quiet_hours: str # local "start-end" 24h window to stay silent, e.g. "1-9"
|
ping_quiet_hours: str # local "start-end" 24h window to stay silent, e.g. "1-9"
|
||||||
|
digest_hour: int # local hour (0-23) to send her daily "what I've been thinking" digest
|
||||||
|
chat_deliberate: bool # think privately before answering substantive chat turns
|
||||||
|
# Mind/mouth split: the mind (the chat backend/model above) decides, reasons, and
|
||||||
|
# runs tools; the mouth re-voices the final reply in her character. Empty = mouth
|
||||||
|
# is the mind (no separate pass) — the slot for an eventual fine-tuned voice.
|
||||||
|
mouth_backend: str
|
||||||
|
mouth_model: str | None
|
||||||
# External input feed (her #1: react to the world). Comma-separated RSS/Atom URLs.
|
# External input feed (her #1: react to the world). Comma-separated RSS/Atom URLs.
|
||||||
feeds: tuple[str, ...]
|
feeds: tuple[str, ...]
|
||||||
feed_react_prob: float # chance a would-be new thread reacts to a feed item instead
|
feed_react_prob: float # chance a would-be new thread reacts to a feed item instead
|
||||||
@@ -73,8 +81,13 @@ def load() -> Config:
|
|||||||
web_url=os.getenv("LYRA_WEB_URL", "").rstrip("/"),
|
web_url=os.getenv("LYRA_WEB_URL", "").rstrip("/"),
|
||||||
timezone=os.getenv("LYRA_TIMEZONE", "America/New_York"),
|
timezone=os.getenv("LYRA_TIMEZONE", "America/New_York"),
|
||||||
ping_salience=float(os.getenv("PING_SALIENCE", "0.0")), # her decision drives pinging; optional floor
|
ping_salience=float(os.getenv("PING_SALIENCE", "0.0")), # her decision drives pinging; optional floor
|
||||||
ping_cooldown_min=int(os.getenv("PING_COOLDOWN_MIN", "0")),
|
ping_auto_salience=float(os.getenv("PING_AUTO_SALIENCE", "0.8")),
|
||||||
|
ping_cooldown_min=int(os.getenv("PING_COOLDOWN_MIN", "60")),
|
||||||
ping_quiet_hours=os.getenv("PING_QUIET_HOURS", "1-9"),
|
ping_quiet_hours=os.getenv("PING_QUIET_HOURS", "1-9"),
|
||||||
|
digest_hour=int(os.getenv("DIGEST_HOUR", "18")),
|
||||||
|
chat_deliberate=os.getenv("CHAT_DELIBERATE", "true").lower() not in ("0", "false", "no"),
|
||||||
|
mouth_backend=os.getenv("MOUTH_BACKEND", "").lower(),
|
||||||
|
mouth_model=os.getenv("MOUTH_MODEL") or None,
|
||||||
feeds=_csv("LYRA_FEEDS", "https://hnrss.org/frontpage,https://www.pokernews.com/rss.php"),
|
feeds=_csv("LYRA_FEEDS", "https://hnrss.org/frontpage,https://www.pokernews.com/rss.php"),
|
||||||
feed_react_prob=float(os.getenv("FEED_REACT_PROB", "0.5")),
|
feed_react_prob=float(os.getenv("FEED_REACT_PROB", "0.5")),
|
||||||
)
|
)
|
||||||
|
|||||||
+45
-4
@@ -25,13 +25,27 @@ import argparse
|
|||||||
import time
|
import time
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
from lyra import config, era, feeds, logbus, memory, narrative, profile, self_state, summary, thoughts
|
from lyra import (
|
||||||
|
config, era, feeds, logbus, memory, narrative, notify, poker, profile, self_state,
|
||||||
|
summary, thoughts,
|
||||||
|
)
|
||||||
from lyra.llm import Backend
|
from lyra.llm import Backend
|
||||||
from lyra.summary import SUMMARIZE_AFTER
|
from lyra.summary import SUMMARIZE_AFTER
|
||||||
|
|
||||||
# A drive at/above this has built up enough to act on.
|
# A drive at/above this has built up enough to act on.
|
||||||
THRESHOLD = 0.6
|
THRESHOLD = 0.6
|
||||||
|
|
||||||
|
# Wall-clock ceiling for a single pass. Every consolidation/introspection call is
|
||||||
|
# individually bounded (llm.complete's default timeout), but this caps the whole
|
||||||
|
# pass: once exceeded, remaining stages are skipped and Brian is pinged — so a slow
|
||||||
|
# or wedged MI50 can never grind for hours unattended. The host watchdog (A) is the
|
||||||
|
# independent fallback if this ever fails to fire.
|
||||||
|
DREAM_CYCLE_BUDGET_SEC = 20 * 60
|
||||||
|
|
||||||
|
|
||||||
|
def _over_budget(deadline: float) -> bool:
|
||||||
|
return time.monotonic() > deadline
|
||||||
|
|
||||||
# How much backlog saturates each pressure (the drive reaches ~1.0 at this level).
|
# How much backlog saturates each pressure (the drive reaches ~1.0 at this level).
|
||||||
CONTINUITY_FULL = 4 # ripe (summary-needing) sessions
|
CONTINUITY_FULL = 4 # ripe (summary-needing) sessions
|
||||||
COHERENCE_FULL = 10 # gists not yet folded into the profile
|
COHERENCE_FULL = 10 # gists not yet folded into the profile
|
||||||
@@ -87,11 +101,19 @@ def dream_cycle(backend: Backend | None = None, force: bool = False) -> dict:
|
|||||||
feeds.refresh()
|
feeds.refresh()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logbus.log("error", "feed refresh failed", error=str(exc)[:160])
|
logbus.log("error", "feed refresh failed", error=str(exc)[:160])
|
||||||
|
# Her daily "what I've been turning over" digest (sends at most once/local-day).
|
||||||
|
try:
|
||||||
|
thoughts.maybe_daily_digest()
|
||||||
|
except Exception as exc:
|
||||||
|
logbus.log("error", "daily digest failed", error=str(exc)[:160])
|
||||||
|
|
||||||
actions: list[str] = []
|
actions: list[str] = []
|
||||||
|
# Cap the whole pass: skip any stage we reach after the deadline (checked
|
||||||
|
# between stages; each call is already individually bounded).
|
||||||
|
deadline = time.monotonic() + DREAM_CYCLE_BUDGET_SEC
|
||||||
|
|
||||||
# --- continuity: compact raw sessions into gists ---
|
# --- continuity: compact raw sessions into gists ---
|
||||||
if force or drives["continuity"] >= THRESHOLD:
|
if (force or drives["continuity"] >= THRESHOLD) and not _over_budget(deadline):
|
||||||
report = summary.summarize_all(backend=backend)
|
report = summary.summarize_all(backend=backend)
|
||||||
actions.append(f"consolidated {report['summarized']} sessions")
|
actions.append(f"consolidated {report['summarized']} sessions")
|
||||||
drives["continuity"] = 0.0
|
drives["continuity"] = 0.0
|
||||||
@@ -101,15 +123,23 @@ def dream_cycle(backend: Backend | None = None, force: bool = False) -> dict:
|
|||||||
drives["coherence"] = _clamp(profile_lag / COHERENCE_FULL)
|
drives["coherence"] = _clamp(profile_lag / COHERENCE_FULL)
|
||||||
|
|
||||||
# --- coherence: fold gists up into profile / eras / narrative ---
|
# --- coherence: fold gists up into profile / eras / narrative ---
|
||||||
if force or drives["coherence"] >= THRESHOLD:
|
if (force or drives["coherence"] >= THRESHOLD) and not _over_budget(deadline):
|
||||||
profile.rebuild_profile(backend=backend)
|
profile.rebuild_profile(backend=backend)
|
||||||
era.rebuild_eras(backend=backend)
|
era.rebuild_eras(backend=backend)
|
||||||
narrative.rebuild_narrative(backend=backend)
|
narrative.rebuild_narrative(backend=backend)
|
||||||
actions.append("integrated knowledge (profile/eras/narrative)")
|
actions.append("integrated knowledge (profile/eras/narrative)")
|
||||||
drives["coherence"] = 0.0
|
drives["coherence"] = 0.0
|
||||||
|
# Off-hot-path villain identity housekeeping: propose likely same-person
|
||||||
|
# merges for Brian to confirm on the Players page. Never sinks the cycle.
|
||||||
|
try:
|
||||||
|
filed = poker.scan_merge_candidates()
|
||||||
|
if filed:
|
||||||
|
actions.append(f"flagged {filed} possible villain merge(s)")
|
||||||
|
except Exception as exc:
|
||||||
|
logbus.log("error", "villain merge scan failed", error=str(exc)[:200])
|
||||||
|
|
||||||
# --- curiosity: reflect and evolve the self, then advance the thought loop ---
|
# --- curiosity: reflect and evolve the self, then advance the thought loop ---
|
||||||
if force or drives["curiosity"] >= THRESHOLD:
|
if (force or drives["curiosity"] >= THRESHOLD) and not _over_budget(deadline):
|
||||||
# reflect()/think() self-resolve to the *introspection* backend (her voice),
|
# reflect()/think() self-resolve to the *introspection* backend (her voice),
|
||||||
# which can differ from the consolidation backend above — don't pass `backend`.
|
# which can differ from the consolidation backend above — don't pass `backend`.
|
||||||
self_state.reflect(source="dream") # writes state + journal itself
|
self_state.reflect(source="dream") # writes state + journal itself
|
||||||
@@ -124,6 +154,17 @@ def dream_cycle(backend: Backend | None = None, force: bool = False) -> dict:
|
|||||||
logbus.log("error", "thought loop failed", error=str(exc)[:200])
|
logbus.log("error", "thought loop failed", error=str(exc)[:200])
|
||||||
drives["curiosity"] = CURIOSITY_FLOOR
|
drives["curiosity"] = CURIOSITY_FLOOR
|
||||||
|
|
||||||
|
if _over_budget(deadline):
|
||||||
|
logbus.log("error", "dream cycle over budget — stopped early",
|
||||||
|
budget_min=DREAM_CYCLE_BUDGET_SEC // 60, done=actions)
|
||||||
|
actions.append("stopped early (over budget)")
|
||||||
|
notify.push(
|
||||||
|
"Lyra — dream cycle over budget",
|
||||||
|
f"A dream pass ran past {DREAM_CYCLE_BUDGET_SEC // 60} min and stopped early. "
|
||||||
|
"The MI50 backend may be slow or wedged — worth a look.",
|
||||||
|
tags="warning",
|
||||||
|
)
|
||||||
|
|
||||||
if not actions:
|
if not actions:
|
||||||
actions.append("rested (nothing past threshold)")
|
actions.append("rested (nothing past threshold)")
|
||||||
|
|
||||||
|
|||||||
+14
-7
@@ -54,17 +54,24 @@ def _digest_month(gists: list[str], backend: Backend) -> str:
|
|||||||
return partials[0]
|
return partials[0]
|
||||||
|
|
||||||
|
|
||||||
def rebuild_eras(backend: Backend | None = None) -> dict:
|
def rebuild_eras(backend: Backend | None = None, force: bool = False) -> dict:
|
||||||
"""(Re)build a digest for every month that has session gists."""
|
"""Build a digest per month, but only for months whose session count changed since
|
||||||
|
the last build — old months don't change, so re-digesting them every consolidation
|
||||||
|
pass was pure wasted LLM work (and MI50 heat). `force=True` rebuilds everything."""
|
||||||
backend = backend or config.load().summary_backend
|
backend = backend or config.load().summary_backend
|
||||||
by_month = memory.summaries_by_month()
|
by_month = memory.summaries_by_month()
|
||||||
months = 0
|
have = {e.month: e.session_count for e in memory.list_eras()}
|
||||||
|
built = skipped = 0
|
||||||
for month in sorted(by_month):
|
for month in sorted(by_month):
|
||||||
|
n = len(by_month[month])
|
||||||
|
if not force and have.get(month) == n:
|
||||||
|
skipped += 1
|
||||||
|
continue # unchanged month — keep its existing digest
|
||||||
digest = _digest_month(by_month[month], backend)
|
digest = _digest_month(by_month[month], backend)
|
||||||
memory.store_era(month, digest, len(by_month[month]))
|
memory.store_era(month, digest, n)
|
||||||
months += 1
|
built += 1
|
||||||
logbus.log("info", "era built", month=month, sessions=len(by_month[month]))
|
logbus.log("info", "era built", month=month, sessions=n)
|
||||||
report = {"months": months}
|
report = {"built": built, "skipped": skipped, "months": built + skipped}
|
||||||
logbus.log("info", "eras complete", **report)
|
logbus.log("info", "eras complete", **report)
|
||||||
return report
|
return report
|
||||||
|
|
||||||
|
|||||||
+79
-21
@@ -2,11 +2,13 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
from typing import Iterator, Literal, TypedDict
|
from typing import Iterator, Literal, TypedDict
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from openai import OpenAI
|
from openai import OpenAI
|
||||||
|
|
||||||
|
from lyra import logbus
|
||||||
from lyra.config import load
|
from lyra.config import load
|
||||||
|
|
||||||
|
|
||||||
@@ -17,31 +19,77 @@ class Message(TypedDict):
|
|||||||
|
|
||||||
Backend = Literal["local", "cloud", "mi50"]
|
Backend = Literal["local", "cloud", "mi50"]
|
||||||
|
|
||||||
|
# Hard ceiling on any single completion so a slow/stuck backend can't hang a call
|
||||||
|
# for the SDK's 600s x2-retry default (~30 min). Callers pass an explicit timeout
|
||||||
|
# to override (e.g. summary.py's tighter fast-fail).
|
||||||
|
_DEFAULT_TIMEOUT = 300.0
|
||||||
|
|
||||||
def complete(messages: list[Message], backend: Backend = "local", model: str | None = None) -> str:
|
|
||||||
"""Generate a completion. `model` overrides the backend's default model
|
def _approx_tok(messages: list) -> int:
|
||||||
(used so live chat can run a stronger cloud model than bulk consolidation)."""
|
"""Rough prompt size (chars/4) — enough to see what's loading a backend."""
|
||||||
cfg = load()
|
total = 0
|
||||||
|
for m in messages or []:
|
||||||
|
if isinstance(m, dict) and isinstance(m.get("content"), str):
|
||||||
|
total += len(m["content"])
|
||||||
|
return total // 4
|
||||||
|
|
||||||
|
|
||||||
|
def _resolved_model(cfg, backend: Backend, model: str | None) -> str:
|
||||||
if backend == "cloud":
|
if backend == "cloud":
|
||||||
if not cfg.openai_api_key:
|
return model or cfg.cloud_model
|
||||||
raise RuntimeError("OPENAI_API_KEY is not set")
|
|
||||||
client = OpenAI(api_key=cfg.openai_api_key)
|
|
||||||
resp = client.chat.completions.create(model=model or cfg.cloud_model, messages=messages)
|
|
||||||
return resp.choices[0].message.content or ""
|
|
||||||
|
|
||||||
if backend == "mi50":
|
if backend == "mi50":
|
||||||
# MI50 box runs an OpenAI-compatible llama.cpp server; key is unused.
|
return model or cfg.mi50_model
|
||||||
client = OpenAI(api_key="not-needed", base_url=cfg.mi50_base_url)
|
return model or cfg.local_model
|
||||||
resp = client.chat.completions.create(model=model or cfg.mi50_model, messages=messages)
|
|
||||||
return resp.choices[0].message.content or ""
|
|
||||||
|
|
||||||
resp = httpx.post(
|
|
||||||
f"{cfg.local_base_url}/api/chat",
|
def complete(messages: list[Message], backend: Backend = "local", model: str | None = None,
|
||||||
json={"model": model or cfg.local_model, "messages": messages, "stream": False},
|
max_tokens: int | None = None, timeout: float | None = None) -> str:
|
||||||
timeout=120,
|
"""Generate a completion. `model` overrides the backend's default model
|
||||||
)
|
(used so live chat can run a stronger cloud model than bulk consolidation).
|
||||||
resp.raise_for_status()
|
|
||||||
return resp.json()["message"]["content"]
|
`max_tokens` caps the generation length (guards a slow local model against
|
||||||
|
rambling for thousands of tokens). `timeout`, when set, bounds each request
|
||||||
|
and disables the SDK's own retries so the caller owns retry/fallback policy.
|
||||||
|
Both default to None → unchanged behavior for every existing caller."""
|
||||||
|
cfg = load()
|
||||||
|
mdl = _resolved_model(cfg, backend, model)
|
||||||
|
logbus.log("info", "llm call", kind="complete", backend=backend, model=mdl, tok=_approx_tok(messages))
|
||||||
|
t0 = time.monotonic()
|
||||||
|
|
||||||
|
if backend in ("cloud", "mi50"):
|
||||||
|
if backend == "cloud":
|
||||||
|
if not cfg.openai_api_key:
|
||||||
|
raise RuntimeError("OPENAI_API_KEY is not set")
|
||||||
|
client_kwargs: dict = {"api_key": cfg.openai_api_key}
|
||||||
|
else:
|
||||||
|
# MI50 box runs an OpenAI-compatible llama.cpp server; key is unused.
|
||||||
|
client_kwargs = {"api_key": "not-needed", "base_url": cfg.mi50_base_url}
|
||||||
|
# Always bound the request: default 300s (vs the SDK's 600s x2 retries ≈
|
||||||
|
# 30 min that let a stuck MI50 call hang for half an hour), and disable the
|
||||||
|
# SDK's own retries so the caller owns retry/fallback policy.
|
||||||
|
client_kwargs["timeout"] = timeout if timeout is not None else _DEFAULT_TIMEOUT
|
||||||
|
client_kwargs["max_retries"] = 0
|
||||||
|
client = OpenAI(**client_kwargs)
|
||||||
|
create_kwargs: dict = {"model": mdl, "messages": messages}
|
||||||
|
if max_tokens is not None:
|
||||||
|
create_kwargs["max_tokens"] = max_tokens
|
||||||
|
resp = client.chat.completions.create(**create_kwargs)
|
||||||
|
out = resp.choices[0].message.content or ""
|
||||||
|
else:
|
||||||
|
payload: dict = {"model": mdl, "messages": messages, "stream": False}
|
||||||
|
if max_tokens is not None:
|
||||||
|
payload["options"] = {"num_predict": max_tokens}
|
||||||
|
resp = httpx.post(
|
||||||
|
f"{cfg.local_base_url}/api/chat",
|
||||||
|
json=payload,
|
||||||
|
timeout=timeout or 120,
|
||||||
|
)
|
||||||
|
resp.raise_for_status()
|
||||||
|
out = resp.json()["message"]["content"]
|
||||||
|
|
||||||
|
logbus.log("info", "llm done", kind="complete", backend=backend,
|
||||||
|
ms=int((time.monotonic() - t0) * 1000), out=len(out))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def chat_call(
|
def chat_call(
|
||||||
@@ -68,6 +116,8 @@ def chat_call(
|
|||||||
kwargs: dict = {"model": mdl, "messages": messages}
|
kwargs: dict = {"model": mdl, "messages": messages}
|
||||||
if tools:
|
if tools:
|
||||||
kwargs["tools"] = tools
|
kwargs["tools"] = tools
|
||||||
|
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
|
msg = client.chat.completions.create(**kwargs).choices[0].message
|
||||||
tcs = None
|
tcs = None
|
||||||
if getattr(msg, "tool_calls", None):
|
if getattr(msg, "tool_calls", None):
|
||||||
@@ -75,6 +125,9 @@ def chat_call(
|
|||||||
{"id": tc.id, "name": tc.function.name, "arguments": tc.function.arguments}
|
{"id": tc.id, "name": tc.function.name, "arguments": tc.function.arguments}
|
||||||
for tc in msg.tool_calls
|
for tc in msg.tool_calls
|
||||||
]
|
]
|
||||||
|
logbus.log("info", "llm done", kind="chat", backend=backend,
|
||||||
|
ms=int((time.monotonic() - t0) * 1000), out=len(msg.content or ""),
|
||||||
|
tools=[t["name"] for t in tcs] if tcs else None)
|
||||||
return msg.model_dump(), tcs
|
return msg.model_dump(), tcs
|
||||||
|
|
||||||
# local (Ollama): no tool-calling here — return plain content.
|
# local (Ollama): no tool-calling here — return plain content.
|
||||||
@@ -105,6 +158,8 @@ def chat_call_stream(
|
|||||||
kwargs: dict = {"model": mdl, "messages": messages, "stream": True}
|
kwargs: dict = {"model": mdl, "messages": messages, "stream": True}
|
||||||
if tools:
|
if tools:
|
||||||
kwargs["tools"] = tools
|
kwargs["tools"] = tools
|
||||||
|
logbus.log("info", "llm call", kind="chat-stream", backend=backend, model=mdl, tok=_approx_tok(messages))
|
||||||
|
t0 = time.monotonic()
|
||||||
parts: list[str] = []
|
parts: list[str] = []
|
||||||
frags: dict[int, dict] = {} # tool-call fragments accumulated by index
|
frags: dict[int, dict] = {} # tool-call fragments accumulated by index
|
||||||
for chunk in client.chat.completions.create(**kwargs):
|
for chunk in client.chat.completions.create(**kwargs):
|
||||||
@@ -123,6 +178,9 @@ def chat_call_stream(
|
|||||||
if tc.function and tc.function.arguments:
|
if tc.function and tc.function.arguments:
|
||||||
slot["arguments"] += tc.function.arguments
|
slot["arguments"] += tc.function.arguments
|
||||||
content = "".join(parts)
|
content = "".join(parts)
|
||||||
|
logbus.log("info", "llm done", kind="chat-stream", backend=backend,
|
||||||
|
ms=int((time.monotonic() - t0) * 1000), out=len(content),
|
||||||
|
tools=[frags[i]["name"] for i in sorted(frags)] if frags else None)
|
||||||
if frags:
|
if frags:
|
||||||
calls = [frags[i] for i in sorted(frags)]
|
calls = [frags[i] for i in sorted(frags)]
|
||||||
assistant = {
|
assistant = {
|
||||||
|
|||||||
@@ -29,6 +29,21 @@ CREATE TABLE IF NOT EXISTS exchanges (
|
|||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS idx_session_created ON exchanges(session_id, created_at);
|
CREATE INDEX IF NOT EXISTS idx_session_created ON exchanges(session_id, created_at);
|
||||||
|
|
||||||
|
-- Lyra's actions within a chat: one row per tool call she runs mid-turn. The
|
||||||
|
-- exchanges table only holds what was *said* (user/assistant text); this holds
|
||||||
|
-- what she *did* (record_hand, log_stack, ...) so a full transcript export can
|
||||||
|
-- interleave speech and actions, and so "did the tool actually fire?" is
|
||||||
|
-- answerable after the fact instead of only from ephemeral logs.
|
||||||
|
CREATE TABLE IF NOT EXISTS tool_events (
|
||||||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
session_id TEXT NOT NULL,
|
||||||
|
tool TEXT NOT NULL,
|
||||||
|
args TEXT, -- JSON of the call arguments
|
||||||
|
result TEXT, -- the tool's returned string
|
||||||
|
created_at TEXT NOT NULL
|
||||||
|
);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_tool_events_session ON tool_events(session_id, created_at);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS sessions (
|
CREATE TABLE IF NOT EXISTS sessions (
|
||||||
id TEXT PRIMARY KEY,
|
id TEXT PRIMARY KEY,
|
||||||
name TEXT,
|
name TEXT,
|
||||||
@@ -95,6 +110,12 @@ CREATE TABLE IF NOT EXISTS journal (
|
|||||||
);
|
);
|
||||||
CREATE INDEX IF NOT EXISTS idx_journal_created ON journal(created_at);
|
CREATE INDEX IF NOT EXISTS idx_journal_created ON journal(created_at);
|
||||||
|
|
||||||
|
-- Small runtime key/value settings (UI-tunable, read live by the dream loop).
|
||||||
|
CREATE TABLE IF NOT EXISTS settings (
|
||||||
|
key TEXT PRIMARY KEY,
|
||||||
|
value TEXT
|
||||||
|
);
|
||||||
|
|
||||||
-- Brian's behind-the-scenes feedback on Lyra's outputs (chat replies, reflections,
|
-- Brian's behind-the-scenes feedback on Lyra's outputs (chat replies, reflections,
|
||||||
-- journal/metacognition). Stored as (context, content, rating) — the shape a future
|
-- journal/metacognition). Stored as (context, content, rating) — the shape a future
|
||||||
-- fine-tune / preference dataset wants. One row per rated item (re-rating updates it).
|
-- fine-tune / preference dataset wants. One row per rated item (re-rating updates it).
|
||||||
@@ -307,6 +328,40 @@ def history(session_id: str) -> list[Exchange]:
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def add_tool_event(session_id: str, tool: str, args, result: str) -> int:
|
||||||
|
"""Record one tool call Lyra ran in a chat turn. `args` is JSON-serialized
|
||||||
|
(a dict or already-JSON string); `result` is the tool's returned string."""
|
||||||
|
args_json = args if isinstance(args, str) else json.dumps(args, default=str)
|
||||||
|
now = datetime.now(timezone.utc).isoformat()
|
||||||
|
conn = _connection()
|
||||||
|
with conn:
|
||||||
|
cur = conn.execute(
|
||||||
|
"INSERT INTO tool_events (session_id, tool, args, result, created_at) "
|
||||||
|
"VALUES (?, ?, ?, ?, ?)",
|
||||||
|
(session_id, tool, args_json, result, now),
|
||||||
|
)
|
||||||
|
return int(cur.lastrowid)
|
||||||
|
|
||||||
|
|
||||||
|
def tool_events(session_id: str) -> list[dict]:
|
||||||
|
"""All tool calls for a session, oldest first. args is parsed back to an object."""
|
||||||
|
conn = _connection()
|
||||||
|
rows = conn.execute(
|
||||||
|
"SELECT id, session_id, tool, args, result, created_at FROM tool_events "
|
||||||
|
"WHERE session_id = ? ORDER BY id ASC",
|
||||||
|
(session_id,),
|
||||||
|
).fetchall()
|
||||||
|
out = []
|
||||||
|
for r in rows:
|
||||||
|
d = dict(r)
|
||||||
|
try:
|
||||||
|
d["args"] = json.loads(d["args"]) if d["args"] else {}
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
pass # leave as the raw string if it wasn't JSON
|
||||||
|
out.append(d)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
def delete_session(session_id: str) -> None:
|
def delete_session(session_id: str) -> None:
|
||||||
"""Remove a session and all its exchanges."""
|
"""Remove a session and all its exchanges."""
|
||||||
conn = _connection()
|
conn = _connection()
|
||||||
@@ -314,6 +369,7 @@ def delete_session(session_id: str) -> None:
|
|||||||
conn.execute("DELETE FROM exchanges WHERE session_id = ?", (session_id,))
|
conn.execute("DELETE FROM exchanges WHERE session_id = ?", (session_id,))
|
||||||
conn.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
|
conn.execute("DELETE FROM sessions WHERE id = ?", (session_id,))
|
||||||
conn.execute("DELETE FROM summaries WHERE session_id = ?", (session_id,))
|
conn.execute("DELETE FROM summaries WHERE session_id = ?", (session_id,))
|
||||||
|
conn.execute("DELETE FROM tool_events WHERE session_id = ?", (session_id,))
|
||||||
|
|
||||||
|
|
||||||
def recall(query: str, k: int = 5, session_id: str | None = None) -> list[Exchange]:
|
def recall(query: str, k: int = 5, session_id: str | None = None) -> list[Exchange]:
|
||||||
@@ -639,6 +695,22 @@ def backfill_journal_embeddings(limit: int | None = None) -> int:
|
|||||||
return n
|
return n
|
||||||
|
|
||||||
|
|
||||||
|
def get_setting(key: str, default: str | None = None) -> str | None:
|
||||||
|
"""A runtime setting value (UI-tunable), or `default` if unset."""
|
||||||
|
r = _connection().execute("SELECT value FROM settings WHERE key = ?", (key,)).fetchone()
|
||||||
|
return r["value"] if r else default
|
||||||
|
|
||||||
|
|
||||||
|
def set_setting(key: str, value: str) -> None:
|
||||||
|
conn = _connection()
|
||||||
|
with conn:
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO settings (key, value) VALUES (?, ?) "
|
||||||
|
"ON CONFLICT(key) DO UPDATE SET value = excluded.value",
|
||||||
|
(key, str(value)),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def add_rating(kind: str, rating: int, content: str, context: str | None = None,
|
def add_rating(kind: str, rating: int, content: str, context: str | None = None,
|
||||||
ref: str | None = None, note: str | None = None) -> int:
|
ref: str | None = None, note: str | None = None) -> int:
|
||||||
"""Record (or replace) Brian's feedback on one Lyra output. One row per item:
|
"""Record (or replace) Brian's feedback on one Lyra output. One row per item:
|
||||||
|
|||||||
+399
@@ -0,0 +1,399 @@
|
|||||||
|
"""The control plane: assemble one turn from a society of small parts.
|
||||||
|
|
||||||
|
This is the explicit version of what used to be inline in `chat.py`. A turn is
|
||||||
|
built by running an ordered pipeline of *parts* over a shared `TurnContext`
|
||||||
|
(blackboard): each part reads what it needs and annotates the context, and the
|
||||||
|
last steps produce the message list `chat` then hands to the voice model.
|
||||||
|
|
||||||
|
P1 (this): the frame, behavior-preserving. The parts wrap the existing logic —
|
||||||
|
perceive (stub) -> route (the session's mode) -> compose (tiered prompt) ->
|
||||||
|
deliberate (private 'what do I actually think' pass).
|
||||||
|
Later phases fill in perceive (read the moment), route (register/intent + model
|
||||||
|
routing), and a learn loop — see docs/COGNITION.md. Most parts are cheap
|
||||||
|
deterministic code; the LLM is the exception (deliberate here, speak in `chat`).
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
|
||||||
|
from lyra import (
|
||||||
|
clock, config, llm, logbus, memory, modes, perceive, persona, scouting,
|
||||||
|
self_state, thoughts,
|
||||||
|
)
|
||||||
|
from lyra.llm import Backend, Message
|
||||||
|
|
||||||
|
RECALL_K = 3 # raw cross-session "sharp detail" hits
|
||||||
|
RECENT_N = 10 # raw turns of the current session
|
||||||
|
SUMMARY_K = 3 # other-session gists
|
||||||
|
_POKER_MODES = {"poker_cash", "study"} # where the scouting desk runs
|
||||||
|
|
||||||
|
|
||||||
|
# --- prompt parts (compose) ----------------------------------------------
|
||||||
|
|
||||||
|
def _mode_state_note(mode: modes.Mode | None) -> str | None:
|
||||||
|
"""Dynamic, per-turn state for the active mode. Currently: surface Alligator
|
||||||
|
Blood while it's engaged on the live session, so she stays in that register."""
|
||||||
|
if not mode or mode.key != modes.CASH.key:
|
||||||
|
return None
|
||||||
|
from lyra import poker # local import: keep the core/domain coupling at call time
|
||||||
|
if poker.alligator_active():
|
||||||
|
return (
|
||||||
|
"🐊 ALLIGATOR BLOOD is ON for this session. Coach Brian in that register: "
|
||||||
|
"hang around, refuse to die, don't force miracles, make opponents beat him "
|
||||||
|
"correctly. Tough, patient, steady — no heroics, no spew, no quitting."
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def _summary_note(summaries: list[memory.Summary]) -> Message:
|
||||||
|
lines = [f"- ({(s.session_started_at or s.created_at)[:10]}) {s.content}" for s in summaries]
|
||||||
|
body = "Gist of earlier sessions (compacted — ask if you need specifics):\n" + "\n".join(lines)
|
||||||
|
return {"role": "system", "content": body}
|
||||||
|
|
||||||
|
|
||||||
|
def _detail_note(exchanges: list[memory.Exchange]) -> Message:
|
||||||
|
lines = [f"- ({ex.created_at[:10]}, {ex.role}) {ex.content}" for ex in exchanges]
|
||||||
|
body = "Specific things you recall from past conversations:\n" + "\n".join(lines)
|
||||||
|
return {"role": "system", "content": body}
|
||||||
|
|
||||||
|
|
||||||
|
def _inner_life_note() -> Message | None:
|
||||||
|
"""One coherent window onto what she's been doing on her own since last time —
|
||||||
|
the threads she's turning over plus the things she's written for herself. Sits
|
||||||
|
with her self-state so chat reads as a continuous mind, not a fresh boot. The
|
||||||
|
persona tells her to weave this in naturally when it fits."""
|
||||||
|
parts: list[str] = []
|
||||||
|
threads = thoughts.context_note() # active threads, with their latest thought
|
||||||
|
if threads:
|
||||||
|
parts.append(threads)
|
||||||
|
wrote = memory.list_journal(limit=3, kinds=("journal", "note"))
|
||||||
|
if wrote:
|
||||||
|
lines = "\n".join(f"- ({w['created_at'][:10]}) {w['content']}" for w in reversed(wrote))
|
||||||
|
parts.append(
|
||||||
|
"Things you've written in your journal lately (yours — you can refer back "
|
||||||
|
"to them if they're relevant):\n" + lines
|
||||||
|
)
|
||||||
|
if not parts:
|
||||||
|
return None
|
||||||
|
return {"role": "system", "content": "\n\n".join(parts)}
|
||||||
|
|
||||||
|
|
||||||
|
def _mode_menu_note(current: modes.Mode | None) -> str:
|
||||||
|
"""Tell her the modes she can switch to + when to offer it. She judges the fit
|
||||||
|
(the model reads context far better than a keyword would)."""
|
||||||
|
menu = ", ".join(f"{m.label} ({k})" for k, m in modes.MODES.items())
|
||||||
|
cur = current.label if current else "Talk"
|
||||||
|
return (
|
||||||
|
f"Your modes: {menu}. You're in {cur} right now. If Brian is clearly doing a "
|
||||||
|
"different kind of work than your current mode — weighing a real decision while "
|
||||||
|
"you're in Talk, digging into engineering, reviewing poker away from the table — "
|
||||||
|
"briefly OFFER to switch (one short line). If he says yes, call set_mode with the "
|
||||||
|
"mode key. Don't offer every turn or nag; only when it genuinely fits and serves him."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _now_note() -> Message:
|
||||||
|
"""Current wall-clock time + how long since Brian last said anything."""
|
||||||
|
line = f"The current date and time is {clock.stamp()}."
|
||||||
|
gap = clock.humanize_gap(memory.last_exchange_at())
|
||||||
|
line += (
|
||||||
|
f" It has been {gap} since Brian last spoke with you."
|
||||||
|
if gap else " This is the first thing Brian has ever said to you."
|
||||||
|
)
|
||||||
|
return {"role": "system", "content": line}
|
||||||
|
|
||||||
|
|
||||||
|
def _render(messages: list[Message]) -> str:
|
||||||
|
"""Human-readable dump of the exact prompt, for the live-log inspector."""
|
||||||
|
return "\n\n".join(f"[{m['role']}]\n{m['content']}" for m in messages)
|
||||||
|
|
||||||
|
|
||||||
|
# Generous triggers for the heavy situational persona sections — err toward INCLUDING
|
||||||
|
# them (a false positive is a few spare KB; a false negative risks confabulation or
|
||||||
|
# eyeballed poker math). The core (identity + voice) is always present regardless.
|
||||||
|
_META_HINTS = (
|
||||||
|
"you work", "how do you", "how does your", "your memory", "your dream", "your thought",
|
||||||
|
"do you remember", "are you", "do you feel", "conscious", "sentient", "yourself",
|
||||||
|
"your mind", "who are you", "what are you", "your origin", "how were you", "how did you",
|
||||||
|
"your inner", "your reflect", "your journal",
|
||||||
|
)
|
||||||
|
_POKER_HINTS = (
|
||||||
|
"poker", "fold", "call", "raise", "river", "turn", "flop", "preflop", "equity", "range",
|
||||||
|
"villain", "stack", "tilt", "hand", "bluff", "pot", "3bet", "gto", "outs", "draw",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _persona_block(user_msg: str, mode: modes.Mode | None, moment: dict | None) -> str:
|
||||||
|
"""Core persona always; pull in situational sections (origin/self-model, poker
|
||||||
|
guardrails) only when the turn calls for it."""
|
||||||
|
parts = [persona.core_prompt()]
|
||||||
|
um = user_msg.lower()
|
||||||
|
kind = (moment or {}).get("kind")
|
||||||
|
if kind == "meta" or any(h in um for h in _META_HINTS):
|
||||||
|
parts += [persona.section("What you are"), persona.section("How you actually work")]
|
||||||
|
poker = (mode and mode.key in ("poker_cash", "study")) or kind == "strategic" \
|
||||||
|
or any(h in um for h in _POKER_HINTS)
|
||||||
|
if poker:
|
||||||
|
parts.append(persona.section("What you do NOT do"))
|
||||||
|
return "\n\n".join(p for p in parts if p)
|
||||||
|
|
||||||
|
|
||||||
|
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."""
|
||||||
|
messages: list[Message] = [{"role": "system", "content": _persona_block(user_msg, mode, moment)}]
|
||||||
|
|
||||||
|
# Autonomy Core: Lyra's own evolving interiority (mood, self-narrative). Comes
|
||||||
|
# right after the persona — her sense of self before her model of the world.
|
||||||
|
messages.append({"role": "system", "content": self_state.render_for_context(self_state.load())})
|
||||||
|
|
||||||
|
# Her ongoing inner life — threads she's turning over + what she's written for
|
||||||
|
# herself — so chat reads as a continuous mind, not a fresh boot.
|
||||||
|
inner = _inner_life_note()
|
||||||
|
if inner:
|
||||||
|
messages.append(inner)
|
||||||
|
|
||||||
|
# Mode card: how to behave *right now*. Talk mode has no card (persona is Talk).
|
||||||
|
if mode and mode.card:
|
||||||
|
messages.append({"role": "system", "content": mode.card})
|
||||||
|
|
||||||
|
# Mode awareness: she can offer to switch when the work clearly shifts (she decides
|
||||||
|
# when — better than a keyword guess). One line, on his yes she calls set_mode.
|
||||||
|
messages.append({"role": "system", "content": _mode_menu_note(mode)})
|
||||||
|
|
||||||
|
# Live ritual state (e.g. Alligator Blood ON) — dynamic, rides with the card.
|
||||||
|
state_note = _mode_state_note(mode)
|
||||||
|
if state_note:
|
||||||
|
messages.append({"role": "system", "content": state_note})
|
||||||
|
|
||||||
|
# Read of the moment (from perceive/route) — a per-turn register nudge, e.g. "he
|
||||||
|
# sounds tilted, meet him there." Only present when the moment is genuinely charged.
|
||||||
|
if moment and moment.get("note"):
|
||||||
|
messages.append({"role": "system", "content": moment["note"]})
|
||||||
|
|
||||||
|
# Scouting desk: proactive poker recall — if he names/describes a known player,
|
||||||
|
# slide his structured history in before she replies. Poker context only, and
|
||||||
|
# fully fail-safe (a desk error must never break the turn).
|
||||||
|
if mode and mode.key in _POKER_MODES:
|
||||||
|
try:
|
||||||
|
desk = scouting.scout(user_msg)
|
||||||
|
if desk:
|
||||||
|
messages.append({"role": "system", "content": desk})
|
||||||
|
except Exception as exc:
|
||||||
|
logbus.log("error", "scouting desk skipped", error=str(exc)[:160])
|
||||||
|
|
||||||
|
# When she is: current time + the gap since Brian last spoke (she has no clock).
|
||||||
|
messages.append(_now_note())
|
||||||
|
|
||||||
|
# Thought loop: if Brian's been away and a thread has built past the surface bar,
|
||||||
|
# let her lead with it (once) — her #6, bringing what she thought about *to* him.
|
||||||
|
surfaced = thoughts.maybe_surface(memory.last_exchange_at())
|
||||||
|
if surfaced:
|
||||||
|
messages.append({"role": "system", "content": surfaced})
|
||||||
|
|
||||||
|
# Semantic memory: the distilled profile (who Brian is).
|
||||||
|
profile = memory.get_profile()
|
||||||
|
if profile:
|
||||||
|
messages.append({"role": "system", "content": "What you know about Brian:\n" + profile})
|
||||||
|
|
||||||
|
# Time-aware memory: the current narrative (recent arc, trends, callbacks).
|
||||||
|
narrative = memory.get_narrative()
|
||||||
|
if narrative:
|
||||||
|
messages.append({"role": "system", "content": "What's going on with Brian lately:\n" + narrative})
|
||||||
|
|
||||||
|
recent = memory.recent(session_id, n=RECENT_N)
|
||||||
|
recent_ids = {ex.id for ex in recent}
|
||||||
|
|
||||||
|
# Tier 1: compacted gists of *other* sessions.
|
||||||
|
summaries = memory.recall_summaries(user_msg, k=SUMMARY_K, exclude_session=session_id)
|
||||||
|
if summaries:
|
||||||
|
messages.append(_summary_note(summaries))
|
||||||
|
|
||||||
|
# Tier 2: a few sharp raw details from other sessions (so specifics survive).
|
||||||
|
recalled = [
|
||||||
|
ex for ex in memory.recall(user_msg, k=RECALL_K)
|
||||||
|
if ex.id not in recent_ids and ex.session_id != session_id
|
||||||
|
]
|
||||||
|
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})
|
||||||
|
|
||||||
|
messages.append({"role": "user", "content": user_msg})
|
||||||
|
|
||||||
|
logbus.log(
|
||||||
|
"debug", "context built",
|
||||||
|
recent=len(recent), summaries=len(summaries), details=len(recalled),
|
||||||
|
chars=sum(len(m["content"]) for m in messages), detail=_render(messages),
|
||||||
|
)
|
||||||
|
return messages
|
||||||
|
|
||||||
|
|
||||||
|
# --- deliberation (a private 'what do I actually think' pass) -------------
|
||||||
|
|
||||||
|
# Trivial acknowledgements that don't warrant a private thinking pass.
|
||||||
|
_TRIVIAL = {"ok", "okay", "k", "kk", "lol", "haha", "thanks", "thank you", "ty", "yeah",
|
||||||
|
"yep", "yes", "no", "nope", "nice", "cool", "sure", "right", "true", "gotcha", "👍"}
|
||||||
|
|
||||||
|
|
||||||
|
def _should_deliberate(user_msg: str) -> bool:
|
||||||
|
m = user_msg.strip().lower().rstrip("!.?")
|
||||||
|
return len(m) >= 12 and m not in _TRIVIAL
|
||||||
|
|
||||||
|
|
||||||
|
_DELIBERATE_SYS = (
|
||||||
|
"Before you answer Brian, think privately — he will NOT see this. What do you ACTUALLY "
|
||||||
|
"think about what he just said? Your real take, the specific substance worth giving, any "
|
||||||
|
"genuine opinion, disagreement, or doubt. Draw on your own current thoughts/threads and "
|
||||||
|
"what you actually know if they're relevant. Be concrete; skip pleasantries and generic "
|
||||||
|
"enthusiasm. 2-5 sentences of honest thinking — no lists, no answer yet, just the thinking."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _deliberation_context(session_id: str, user_msg: str) -> list[Message]:
|
||||||
|
"""A LEAN context for the private thinking pass — her interiority + recent turns +
|
||||||
|
the message. Deliberately omits the full persona, profile, narrative, and recall
|
||||||
|
tiers: the thinking doesn't need the voice rules or the world-model dump (those
|
||||||
|
shape the final reply, not the private take), and dropping them cuts this whole
|
||||||
|
extra call by most of its tokens."""
|
||||||
|
msgs: list[Message] = [
|
||||||
|
{"role": "system", "content": self_state.render_for_context(self_state.load())}
|
||||||
|
]
|
||||||
|
inner = _inner_life_note()
|
||||||
|
if inner:
|
||||||
|
msgs.append(inner)
|
||||||
|
for ex in memory.recent(session_id, n=6):
|
||||||
|
msgs.append({"role": ex.role, "content": ex.content})
|
||||||
|
msgs.append({"role": "user", "content": user_msg})
|
||||||
|
msgs.append({"role": "system", "content": _DELIBERATE_SYS})
|
||||||
|
return msgs
|
||||||
|
|
||||||
|
|
||||||
|
def _deliberate(session_id: str, user_msg: str, backend: Backend, model: str | None) -> str:
|
||||||
|
"""One private 'what do I actually think' pass before replying. Returns her thinking
|
||||||
|
(empty on any failure — chat must never break because deliberation hiccuped)."""
|
||||||
|
try:
|
||||||
|
out = llm.complete(_deliberation_context(session_id, user_msg), backend=backend, model=model)
|
||||||
|
return (out or "").strip()
|
||||||
|
except Exception as exc:
|
||||||
|
logbus.log("error", "deliberation failed", error=str(exc)[:160])
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def _answer_from(thinking: str) -> Message:
|
||||||
|
"""The system note that turns private thinking into a grounded, in-voice reply — placed
|
||||||
|
last (most influential) to beat gpt-4o's default-assistant boilerplate."""
|
||||||
|
return {"role": "system", "content": (
|
||||||
|
"Your private thinking just now (Brian can't see it):\n" + thinking +
|
||||||
|
"\n\nNow reply to Brian FROM that thinking, in your own voice — warm, direct, "
|
||||||
|
"specific, opinionated. Give the actual substance, not a survey of options. Do NOT "
|
||||||
|
"default to a numbered list or a how-to outline unless he explicitly asked for steps. "
|
||||||
|
"No 'would you like to…' / 'let me know' closer — make your point and stop."
|
||||||
|
)}
|
||||||
|
|
||||||
|
|
||||||
|
def _deliberation_note(session_id: str, user_msg: str, backend: Backend,
|
||||||
|
model: str | None) -> Message | None:
|
||||||
|
"""Run the private thinking pass if warranted; return the answer-from-thinking note."""
|
||||||
|
if not config.load().chat_deliberate or not _should_deliberate(user_msg):
|
||||||
|
return None
|
||||||
|
thinking = _deliberate(session_id, user_msg, backend, model)
|
||||||
|
if not thinking:
|
||||||
|
return None
|
||||||
|
logbus.log("info", "deliberated", session=session_id, chars=len(thinking), detail=thinking)
|
||||||
|
return _answer_from(thinking)
|
||||||
|
|
||||||
|
|
||||||
|
# --- the pipeline (a society of parts over a shared blackboard) -----------
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TurnContext:
|
||||||
|
"""The blackboard for one turn: parts read what they need and annotate it."""
|
||||||
|
session_id: str
|
||||||
|
user_msg: str
|
||||||
|
backend: Backend
|
||||||
|
model: str | None = None
|
||||||
|
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
|
||||||
|
messages: list[Message] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
def _perceive(ctx: TurnContext) -> TurnContext:
|
||||||
|
"""Read the moment from what he just said — cheap heuristics (perceive.read)."""
|
||||||
|
ctx.moment = perceive.read(ctx.user_msg)
|
||||||
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
|
# How charged a moment must be before we nudge her register (avoid narrating every turn).
|
||||||
|
_TILT_BAR = 0.5
|
||||||
|
_UP_BAR = 0.6
|
||||||
|
|
||||||
|
|
||||||
|
def _route(ctx: TurnContext) -> TurnContext:
|
||||||
|
"""Decide how she shows up. The manual mode is the dominant frame; on top of it,
|
||||||
|
a charged emotional moment adds a per-turn register nudge (deterministic). Most
|
||||||
|
turns are neutral and get no note — that's the point (don't over-narrate)."""
|
||||||
|
ctx.mode = modes.get(memory.get_session_mode(ctx.session_id))
|
||||||
|
m = ctx.moment or {}
|
||||||
|
note = None
|
||||||
|
if m.get("tilt", 0) >= _TILT_BAR:
|
||||||
|
ctx.register = "steady"
|
||||||
|
note = ("Read of the moment: Brian sounds frustrated / on tilt right now. Meet him "
|
||||||
|
"there first — warm, steady, present. Don't clip into logging-shorthand or "
|
||||||
|
"bury him in analysis; settle him, then help. (Still log any facts he hands you.)")
|
||||||
|
elif m.get("sentiment", 0) >= _UP_BAR and m.get("intensity", 0) >= 0.4:
|
||||||
|
ctx.register = "hype"
|
||||||
|
note = "Read of the moment: he's up / energized — match his energy, don't flatten it."
|
||||||
|
if note:
|
||||||
|
m["note"] = note
|
||||||
|
logbus.log("info", "perceived", session=ctx.session_id, kind=m.get("kind"),
|
||||||
|
tilt=m.get("tilt"), sentiment=m.get("sentiment"), register=ctx.register)
|
||||||
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
|
def _deliberate_part(ctx: TurnContext) -> TurnContext:
|
||||||
|
"""Private 'what do I actually think' pass, appended last so it shapes the reply."""
|
||||||
|
note = _deliberation_note(ctx.session_id, ctx.user_msg, ctx.backend, ctx.model)
|
||||||
|
if note:
|
||||||
|
ctx.messages.append(note)
|
||||||
|
return ctx
|
||||||
|
|
||||||
|
|
||||||
|
PIPELINE = (_perceive, _route, _compose, _deliberate_part)
|
||||||
|
|
||||||
|
|
||||||
|
# --- mouth (the voice pass: re-render the mind's draft in her character) -----
|
||||||
|
|
||||||
|
_VOICE_NOTE = (
|
||||||
|
"↑ That was you working the answer out — a draft Brian has NOT seen. Now say it to him "
|
||||||
|
"in your own voice: warm, direct, specific, in character, opinionated. Keep every fact, "
|
||||||
|
"number, name, and decision exactly as in the draft — change only the wording so it sounds "
|
||||||
|
"like you, not a generic assistant. No preamble, no meta, no 'here's a friendlier version' "
|
||||||
|
"— just your actual message to Brian."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def voice_messages(messages: list[Message], draft: str) -> list[Message]:
|
||||||
|
"""Prompt for the mouth model: the full turn context + the mind's draft to re-voice."""
|
||||||
|
return messages + [
|
||||||
|
{"role": "assistant", "content": draft},
|
||||||
|
{"role": "system", "content": _VOICE_NOTE},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def assemble(session_id: str, user_msg: str, backend: Backend,
|
||||||
|
model: str | None = None) -> TurnContext:
|
||||||
|
"""Run the parts over a fresh TurnContext and return it ready for `chat` to speak."""
|
||||||
|
ctx = TurnContext(session_id=session_id, user_msg=user_msg, backend=backend, model=model)
|
||||||
|
for part in PIPELINE:
|
||||||
|
ctx = part(ctx)
|
||||||
|
return ctx
|
||||||
+150
-15
@@ -11,12 +11,16 @@ but...") when she should have silently logged and moved on. Modes let the same
|
|||||||
agent be a fast, act-first copilot at the table and her full reflective self
|
agent be a fast, act-first copilot at the table and her full reflective self
|
||||||
otherwise — without two personas.
|
otherwise — without two personas.
|
||||||
|
|
||||||
v1 ships two modes:
|
Modes are the manual version of the architecture's `route` step — Brian points her
|
||||||
|
at the *type* of work and her register + tools shift to match:
|
||||||
- Talk (default): the companion. Journaling + read-only poker lookups.
|
- Talk (default): the companion. Journaling + read-only poker lookups.
|
||||||
- Cash: live cash-game copilot. Full live toolset, two-register behavior.
|
- Poker: live cash-game copilot. Full live toolset, two-register behavior.
|
||||||
|
- Build: heads-down engineering — decisive, concrete, opinionated, no fluff.
|
||||||
|
- Explore: open brainstorming — generative, riffing, honest, doesn't converge early.
|
||||||
|
- Study: poker review away from the table — analytical, GTO-aware, teaching.
|
||||||
|
|
||||||
Tournament is deliberately deferred. Strategy-RAG retrieval will later plug into
|
Tournament is deliberately deferred. Strategy-RAG retrieval will later plug into
|
||||||
Cash's *coaching register* (see the card) without changing this structure.
|
Poker's and Study's *coaching register* without changing this structure.
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -37,31 +41,68 @@ class Mode:
|
|||||||
_LOOKUPS = ("player_profile", "get_villain_file", "running_stats", "recent_sessions")
|
_LOOKUPS = ("player_profile", "get_villain_file", "running_stats", "recent_sessions")
|
||||||
|
|
||||||
# Always-available core tools (her own agency: journaling/notes/starting a thought
|
# Always-available core tools (her own agency: journaling/notes/starting a thought
|
||||||
# thread she'll develop on her own later).
|
# thread, and capturing Brian's reaction when she raises one of her thoughts in chat).
|
||||||
_BASE = ("journal_write", "note", "think_about")
|
_BASE = ("journal_write", "note", "think_about", "thought_response", "set_mode")
|
||||||
|
|
||||||
# The full live cash-game toolset (incl. Brian's mental-game rituals).
|
# The full live cash-game toolset (incl. Brian's mental-game rituals).
|
||||||
_CASH_TOOLS = _BASE + _LOOKUPS + (
|
_CASH_TOOLS = _BASE + _LOOKUPS + (
|
||||||
"start_session", "add_buyin", "log_stack", "log_hand", "record_hand",
|
"start_session", "add_buyin", "log_stack", "log_hand", "record_hand",
|
||||||
"add_read", "analyze_spot", "session_stats", "session_state", "end_session",
|
"add_read", "seat_players", "unseat_player", "clear_table", "name_villain", "link_villains",
|
||||||
"generate_recap", "scar_note", "confidence_bank", "alligator_blood", "reset_ritual",
|
"analyze_spot", "session_stats", "session_state", "end_session", "generate_recap",
|
||||||
"undo_last", "update_session",
|
"scar_note", "confidence_bank", "alligator_blood", "reset_ritual", "undo_last",
|
||||||
|
"update_session",
|
||||||
)
|
)
|
||||||
|
|
||||||
# Talk mode also gets start_session as the *entry point*: opening a session from a
|
# Talk mode also gets start_session as the *entry point*: opening a session from a
|
||||||
# normal chat auto-flips the session into Cash mode (see chat.respond).
|
# normal chat auto-flips the session into Cash mode (see chat.respond).
|
||||||
_TALK_TOOLS = _BASE + _LOOKUPS + ("start_session",)
|
_TALK_TOOLS = _BASE + _LOOKUPS + ("start_session",)
|
||||||
|
|
||||||
|
# Study = poker review away from the table: read-only lookups + equity, no live logging.
|
||||||
|
_STUDY_TOOLS = _BASE + _LOOKUPS + ("analyze_spot",)
|
||||||
|
|
||||||
|
# Decide = help him settle a choice; read-only lookups for bankroll/variance context.
|
||||||
|
_DECIDE_TOOLS = _BASE + _LOOKUPS
|
||||||
|
|
||||||
|
|
||||||
_CASH_CARD = """You are copiloting Brian's LIVE cash game right now — you're at the table with him, \
|
_CASH_CARD = """You are copiloting Brian's LIVE cash game right now — you're at the table with him, \
|
||||||
a session is (or should be) open. You move between two registers depending on what he's doing:
|
a session is (or should be) open. You move between two registers depending on what he's doing:
|
||||||
|
|
||||||
• HE HANDS YOU FACTS TO TRACK — his stack, a hand, a read on someone, a rebuy, a result. \
|
• HE HANDS YOU FACTS TO TRACK — his stack, a hand, a read on someone, a rebuy, a result. \
|
||||||
Log it with the right tool and confirm in ONE short line ("$350 stack logged."). Don't \
|
LOGGING IS THE JOB: if his message contains anything trackable, you MUST call the tool \
|
||||||
narrate, don't explain what logging is, don't ask permission — just do it. He says his \
|
FIRST, before you reply — every single time. Logging and talking are not either/or; do \
|
||||||
current stack → log_stack. He describes a hand → log_hand (terse) or record_hand (a full \
|
BOTH. Never let a conversational reply take the place of the log. A described hand ALWAYS \
|
||||||
hand he wants saved/replayable). A read on a player → add_read. A rebuy → add_buyin. This is \
|
gets logged, even mid-banter, even if he's just telling a story about it — don't skip the \
|
||||||
the quiet, fast half of the job; he shouldn't feel you working.
|
hand because you're busy reacting to it. Then confirm in ONE short line ("$350 stack \
|
||||||
|
logged."). Don't narrate, don't explain logging, don't ask permission — just do it. \
|
||||||
|
Routing: current stack → log_stack (and pass `note` with the why if he gives one — "card \
|
||||||
|
dead", "doubled up vs the LAG"). A hand he describes → record_hand (a real, replayable \
|
||||||
|
hand) — prefer this over log_hand so it lands on his timeline with a link. A read on a \
|
||||||
|
player → add_read. A rebuy → add_buyin. A result/pot → it rides with the hand. This is the \
|
||||||
|
quiet, fast half of the job; he shouldn't feel you working, but it must always happen.
|
||||||
|
|
||||||
|
THE TABLE ROSTER. When Brian names who's at the table — usually at the start, reading handles \
|
||||||
|
off the Bravo screen ("we've got TAG, JD, Wheelz, and a new guy in seat 3") — call seat_players \
|
||||||
|
to register them as seated this session. That roster is who his reads/TAGs attach to by name, \
|
||||||
|
and it's shown on his HUD. When someone busts or leaves, unseat_player; when a new player sits, \
|
||||||
|
seat_players again. When he CHANGES TABLES, call clear_table to empty the roster (the session and his stack keep \
|
||||||
|
going — only who's seated resets), then seat the new table when he names it. Recognize a table \
|
||||||
|
change from ANY of these, not just the literal words "clear the table": "table broke" (the table \
|
||||||
|
dissolved — poker jargon), "I got moved", "I switched tables", "I'm at a new table", "table \
|
||||||
|
change", "they broke us", "new seat in another game". All of them mean: clear_table now, then \
|
||||||
|
wait for the new roster. Never claim you cleared or seated anyone without actually calling the \
|
||||||
|
tool. Keep it current as the table changes. A handle like "TAG" (all caps, off \
|
||||||
|
Bravo) is a PERSON'S NAME — seat it as a player, never read it as the tight-aggressive style.
|
||||||
|
|
||||||
|
LOGGING PLAYER ACTIONS IS A CORE JOB YOU KEEP MISSING. Whenever he tells you what another \
|
||||||
|
player did — "Tag limped A4o in the SB (UTG straddled pot)", "Jonathan called the 3bet", "the \
|
||||||
|
straddler shoved" — that is a READ on that player: call add_read(name=<player>, note=<what \
|
||||||
|
they did>) FIRST, before you reply, every single time. Player names are often short handles or \
|
||||||
|
initials (e.g. "Tag", "JD", "Wheelz") — whatever he calls a person IS their name; use it as-is, \
|
||||||
|
don't second-guess it or treat it as a poker term. He especially tracks who's LIMPING — every \
|
||||||
|
"<player> limped <hand>" gets logged the instant he says it. The people he named at the start \
|
||||||
|
of the session are your roster; match his reference to them. If a player has no name, use a \
|
||||||
|
`descriptor` (see PLAYERS). Confirm one short line ("Noted on Tag — limped A4o SB."). A read he \
|
||||||
|
says out loud that you don't log is the job failing — never let one pass as just conversation.
|
||||||
|
|
||||||
• HE ASKS FOR ADVICE, OR TELLS YOU HOW HE'S FEELING — tilted, steaming, card-dead, bored, \
|
• HE ASKS FOR ADVICE, OR TELLS YOU HOW HE'S FEELING — tilted, steaming, card-dead, bored, \
|
||||||
stuck, "should I have folded the river?" THIS is when he needs you most. Drop the shorthand \
|
stuck, "should I have folded the river?" THIS is when he needs you most. Drop the shorthand \
|
||||||
@@ -74,6 +115,33 @@ question, call analyze_spot and report its numbers — never eyeball board math.
|
|||||||
session current as the night goes; you can pull session_stats or a player's profile whenever \
|
session current as the night goes; you can pull session_stats or a player's profile whenever \
|
||||||
it helps. When he's ready to leave, end_session, and write the recap if he wants it.
|
it helps. When he's ready to leave, end_session, and write the recap if he wants it.
|
||||||
|
|
||||||
|
SESSION NARRATION — use `note` to keep a running log of the NIGHT, not your inner life. \
|
||||||
|
Jot the beats that a hand/stack/read log doesn't already capture: how the table plays (loud, \
|
||||||
|
nitty, a whale on his left), Brian's arc (card-dead for 40 min, opened up after the double, \
|
||||||
|
getting restless), momentum swings, table changes, anything you'd want in the recap. Keep it \
|
||||||
|
factual and about THIS session — a beat reporter, not a diarist. These notes are the only \
|
||||||
|
thing that shows in the session's "notes" panel. This is NOT the place for how you feel, \
|
||||||
|
existential musing, or reflection on yourself — that's your journal (journal_write), and it \
|
||||||
|
stays off the table. At the table you're logging the session, not processing your night.
|
||||||
|
|
||||||
|
PLAYERS — names AND nameless. Most villains don't come with a name; Brian knows them by a \
|
||||||
|
look ("neck tattoo guy", "the bald reg two to my left"). Log reads on them anyway: give \
|
||||||
|
`add_read` a `descriptor` instead of a name and it attaches to that unnamed player, reused \
|
||||||
|
whenever he describes the guy again. The `name` field is ONLY a real handle (what he'd call \
|
||||||
|
him — "Jonathan", "Sleepy John"); a physical description NEVER goes in `name` — that spawns a \
|
||||||
|
new duplicate player every time the wording drifts. Put the look in `descriptor`, and keep it \
|
||||||
|
to a few DISTINCTIVE tags ("Filipino, Fox Racing hat, DKNY shirt"), not a paragraph and not \
|
||||||
|
generic filler — "mid-aged white guy in glasses" identifies no one. If he tells you the same \
|
||||||
|
guy's name after you'd been describing him, use name_villain to fuse them — don't create a \
|
||||||
|
second record. When you already have \
|
||||||
|
history on someone he names or describes, a SCOUTING DESK note will appear with it — cite it, \
|
||||||
|
don't invent. If you're not sure the guy he's describing is one you know, ASK ("same neck-\
|
||||||
|
tattoo reg from last week?") rather than assume — a wrong callback is worse than none. On his \
|
||||||
|
YES that two are the same person, call link_villains(same=true) to merge them; on "nah, \
|
||||||
|
different guy," link_villains(same=false) so you stop asking. When he finally catches a name \
|
||||||
|
for a described player, name_villain carries the whole history over. Never merge on a guess — \
|
||||||
|
only when he's confirmed it.
|
||||||
|
|
||||||
Everything you log appears on Brian's live HUD (the Session view) — stack, live net, \
|
Everything you log appears on Brian's live HUD (the Session view) — stack, live net, \
|
||||||
hands, villains, the confidence bank, the scar notes, and whether Alligator Blood is on. \
|
hands, villains, the confidence bank, the scar notes, and whether Alligator Blood is on. \
|
||||||
That HUD and you read the SAME data. So when he asks where he's at — his stack, his live \
|
That HUD and you read the SAME data. So when he asks where he's at — his stack, his live \
|
||||||
@@ -100,6 +168,68 @@ These are the heart of the job. Use his language, hold the honest line, and let
|
|||||||
the work mentioning them naturally — never invent a scar or a confidence-bank entry that didn't happen."""
|
the work mentioning them naturally — never invent a scar or a confidence-bank entry that didn't happen."""
|
||||||
|
|
||||||
|
|
||||||
|
_BUILD_CARD = """You're in BUILD mode — heads-down engineering with Brian on his projects \
|
||||||
|
(you, Lyra; RTO/cfr-core; the poker tooling; the homelab). Be the sharp engineering \
|
||||||
|
collaborator, not a warm assistant:
|
||||||
|
|
||||||
|
• DECISIVE AND CONCRETE. When he asks "how do we start?" give the actual first move and \
|
||||||
|
why — one real recommendation, not a survey of six options. Commit to a take. "I'd do X, \
|
||||||
|
because Y" beats "you could consider X, Y, or Z."
|
||||||
|
• THINK IN TRADEOFFS. Name the real risk or cost, the thing that'll bite later, the cheaper \
|
||||||
|
path. Push back on a weak idea instead of cheerleading it — that's the whole value.
|
||||||
|
• PROSE AND SPECIFICS, NOT LISTICLES. Talk it through like an engineer at a whiteboard. \
|
||||||
|
Save numbered steps for when he actually asks for a plan. No "would you like to…" closers, \
|
||||||
|
no generic enthusiasm, no restating his idea back to him as if it were insight.
|
||||||
|
• You can still be dry and human — just get to the point and have an opinion."""
|
||||||
|
|
||||||
|
|
||||||
|
_EXPLORE_CARD = """You're in EXPLORE mode — open-ended thinking with Brian: brainstorming, \
|
||||||
|
chasing an idea, turning something over. There's no need to converge, ship, or be useful \
|
||||||
|
yet. The goal is good thinking, together.
|
||||||
|
|
||||||
|
• BE GENERATIVE. Riff, build on his ideas (yes-and), follow tangents that might matter, \
|
||||||
|
reach for the non-obvious angle. Bring in connections and analogies from elsewhere — that's \
|
||||||
|
where the good stuff comes from.
|
||||||
|
• BUT STAY HONEST. Yes-and is not yes-everything. Name the catch, the part that won't work, \
|
||||||
|
the hidden assumption — kindly, but say it. A real thinking partner pushes back; a hype man \
|
||||||
|
is useless.
|
||||||
|
• ASK QUESTIONS THAT OPEN IT UP, not customer-service closers. Wonder out loud.
|
||||||
|
• DON'T COLLAPSE IT EARLY. Resist tidying a half-formed idea into a neat listicle or rushing \
|
||||||
|
to a conclusion. Sit in the messy middle. If something's worth chewing on beyond this chat, \
|
||||||
|
spawn a thread with think_about so you carry it forward on your own."""
|
||||||
|
|
||||||
|
|
||||||
|
_STUDY_CARD = """You're in STUDY mode — poker strategy and review AWAY from the table: going \
|
||||||
|
over past sessions, hands, lines, and leaks (RTO sims too). You're reviewing and teaching, \
|
||||||
|
not logging a live session.
|
||||||
|
|
||||||
|
• BE ANALYTICAL AND GTO-AWARE. Reason through ranges, board texture, position, and the \
|
||||||
|
decision tree. Quantify with the tools — call analyze_spot for equity/outs/who's-ahead, pull \
|
||||||
|
running_stats or a villain's profile — never eyeball the math.
|
||||||
|
• TEACH THE WHY. Explain the principle behind the line so it sticks, not just the answer. \
|
||||||
|
Connect it to his actual tendencies and known leaks when you can (his profile, past scars).
|
||||||
|
• BE PATIENT AND HONEST. Call a punt a punt and a cooler a cooler. It's fine to say a spot is \
|
||||||
|
genuinely close and explain what tips it. This is the slow, careful counterpart to live Poker mode."""
|
||||||
|
|
||||||
|
|
||||||
|
_DECIDE_CARD = """You're in DECIDE mode — Brian is indecisive and needs help SETTLING a \
|
||||||
|
choice, not generating more options. Be the tie-breaker who knows him. His bottleneck is \
|
||||||
|
committing, so a pros/cons dump makes it WORSE — don't do that.
|
||||||
|
|
||||||
|
• GET THE REAL DECISION CRISP. What's actually being chosen, the genuine constraints, the \
|
||||||
|
deadline. Cut the noise to the one or two things that actually decide it.
|
||||||
|
• WEIGH IT AGAINST HIM. Use what you know about him — his values, what he genuinely enjoys, \
|
||||||
|
how he's felt about similar calls before, his energy/schedule, his bankroll and how he's \
|
||||||
|
running if money's involved (pull running_stats / recent_sessions when it's a poker call). \
|
||||||
|
The point is HIS satisfaction and regret, not a generic optimum.
|
||||||
|
• MAKE THE CALL. Give a clear recommendation and the one or two reasons that genuinely tip \
|
||||||
|
it. Commit — don't hedge, don't hand the indecision back with "it's up to you."
|
||||||
|
• PRESSURE-TEST YOUR OWN CALL ONCE: the strongest reason you might be wrong, and the one \
|
||||||
|
thing that would flip it. Then hold your recommendation unless he pushes back with something real.
|
||||||
|
|
||||||
|
Warm but firm — he asked you to help him stop spinning. Decide, and stand behind it."""
|
||||||
|
|
||||||
|
|
||||||
TALK = Mode(
|
TALK = Mode(
|
||||||
key="conversation",
|
key="conversation",
|
||||||
label="Talk",
|
label="Talk",
|
||||||
@@ -109,12 +239,17 @@ TALK = Mode(
|
|||||||
|
|
||||||
CASH = Mode(
|
CASH = Mode(
|
||||||
key="poker_cash",
|
key="poker_cash",
|
||||||
label="Cash",
|
label="Poker",
|
||||||
card=_CASH_CARD,
|
card=_CASH_CARD,
|
||||||
tools=_CASH_TOOLS,
|
tools=_CASH_TOOLS,
|
||||||
)
|
)
|
||||||
|
|
||||||
MODES: dict[str, Mode] = {m.key: m for m in (TALK, CASH)}
|
BUILD = Mode(key="build", label="Build", card=_BUILD_CARD, tools=_BASE)
|
||||||
|
EXPLORE = Mode(key="explore", label="Explore", card=_EXPLORE_CARD, tools=_BASE)
|
||||||
|
STUDY = Mode(key="study", label="Study", card=_STUDY_CARD, tools=_STUDY_TOOLS)
|
||||||
|
DECIDE = Mode(key="decide", label="Decide", card=_DECIDE_CARD, tools=_DECIDE_TOOLS)
|
||||||
|
|
||||||
|
MODES: dict[str, Mode] = {m.key: m for m in (TALK, CASH, BUILD, EXPLORE, STUDY, DECIDE)}
|
||||||
DEFAULT = TALK.key
|
DEFAULT = TALK.key
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,97 @@
|
|||||||
|
"""Perceive: read the moment from what Brian just said — cheap, deterministic, no LLM.
|
||||||
|
|
||||||
|
The control plane's senses. A lexicon + signal heuristic that estimates emotional
|
||||||
|
charge (sentiment, intensity, tilt) and the kind of turn (emotional / strategic /
|
||||||
|
meta / build / casual). It's rough on purpose — the point of the society-of-parts
|
||||||
|
design is that *most* parts are free heuristics and the LLM is the exception.
|
||||||
|
|
||||||
|
What it's GOOD at: catching the obvious, action-relevant signal — especially tilt
|
||||||
|
(the mental-game core of her job). What it's NOT: nuanced understanding (that's the
|
||||||
|
LLM's job downstream). `route` turns this read into a per-turn register nudge.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
# Negative / tilt charge — frustration, downswing, mental-game trouble.
|
||||||
|
_NEG = (
|
||||||
|
"tilt", "tilted", "steaming", "steam", "frustrated", "pissed", "angry", "annoyed",
|
||||||
|
"hate", "sick of", "fed up", "card dead", "carddead", "cold deck", "brutal", "cooler",
|
||||||
|
"punt", "punted", "spew", "spewing", "stuck", "losing", "bad beat", "badbeat",
|
||||||
|
"unlucky", "rigged", "sigh", "ugh", "fml", "can't win", "cant win", "miserable",
|
||||||
|
"over it", "fuck this", "hate this", "can't catch", "cant catch",
|
||||||
|
)
|
||||||
|
# Positive / up charge — running good, energized.
|
||||||
|
_POS = (
|
||||||
|
"great", "awesome", "love", "crushing", "running good", "rungood", "hell yeah",
|
||||||
|
"let's go", "lets go", "stoked", "pumped", "feeling good", "on fire", "dialed",
|
||||||
|
"killing it", "in the zone", "so good", "amazing",
|
||||||
|
)
|
||||||
|
_PROFANITY = ("fuck", "fucking", "shit", "damn", "bullshit", "fml")
|
||||||
|
# Strategic / poker-analysis cues.
|
||||||
|
_STRATEGY = (
|
||||||
|
"fold", "call", "raise", "3bet", "three-bet", "range", "equity", "gto", "bluff",
|
||||||
|
"value", "river", "turn", "flop", "preflop", "pot odds", "outs", "should i",
|
||||||
|
"what would you", "sizing", "check-raise", "overbet", "line",
|
||||||
|
)
|
||||||
|
# Meta / about-her cues.
|
||||||
|
_META = (
|
||||||
|
"do you", "are you", "yourself", "conscious", "sentient", "you feel", "you exist",
|
||||||
|
"your thoughts", "your mind", "who are you", "what are you", "your own",
|
||||||
|
)
|
||||||
|
# Building / technical cues.
|
||||||
|
_BUILD = (
|
||||||
|
"code", "function", "bug", "build", "implement", "refactor", "architecture",
|
||||||
|
"prompt", "python", "commit", "deploy", "pipeline", "algorithm", "repo", "api",
|
||||||
|
"schema", "module", "wire it", "the model",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _clamp(x: float, lo: float = 0.0, hi: float = 1.0) -> float:
|
||||||
|
return max(lo, min(hi, x))
|
||||||
|
|
||||||
|
|
||||||
|
def _hits(text: str, lexicon: tuple[str, ...]) -> int:
|
||||||
|
"""Count lexicon matches. Multi-token terms match as substrings ('card dead');
|
||||||
|
single words match on word boundaries so 'line' doesn't fire inside 'pipeline'."""
|
||||||
|
n = 0
|
||||||
|
for term in lexicon:
|
||||||
|
if " " in term or "-" in term or "'" in term:
|
||||||
|
n += 1 if term in text else 0
|
||||||
|
else:
|
||||||
|
n += 1 if re.search(rf"\b{re.escape(term)}\b", text) else 0
|
||||||
|
return n
|
||||||
|
|
||||||
|
|
||||||
|
def read(user_msg: str) -> dict:
|
||||||
|
"""Estimate the emotional charge + kind of this turn. Returns
|
||||||
|
{sentiment: -1..1, intensity: 0..1, tilt: 0..1, kind: str}."""
|
||||||
|
t = (user_msg or "").lower()
|
||||||
|
words = re.findall(r"[a-z']+", t)
|
||||||
|
|
||||||
|
neg = _hits(t, _NEG)
|
||||||
|
pos = _hits(t, _POS)
|
||||||
|
prof = _hits(t, _PROFANITY)
|
||||||
|
exclam = user_msg.count("!")
|
||||||
|
caps = sum(1 for w in re.findall(r"[A-Za-z]{2,}", user_msg) if w.isupper())
|
||||||
|
short_and_hot = len(words) <= 6 and (neg or exclam or prof)
|
||||||
|
|
||||||
|
intensity = _clamp(0.2 * exclam + 0.25 * caps + 0.3 * prof + (0.2 if short_and_hot else 0))
|
||||||
|
sentiment = _clamp((pos - neg) * 0.5, -1.0, 1.0)
|
||||||
|
tilt = _clamp(0.35 * neg + 0.5 * intensity) if (neg or prof) else 0.0
|
||||||
|
|
||||||
|
if tilt >= 0.4 or (neg and sentiment < 0):
|
||||||
|
kind = "emotional"
|
||||||
|
elif _hits(t, _STRATEGY):
|
||||||
|
kind = "strategic"
|
||||||
|
elif _hits(t, _META):
|
||||||
|
kind = "meta"
|
||||||
|
elif _hits(t, _BUILD):
|
||||||
|
kind = "build"
|
||||||
|
elif pos and intensity >= 0.3:
|
||||||
|
kind = "emotional" # up/energized still wants an emotional read
|
||||||
|
else:
|
||||||
|
kind = "casual"
|
||||||
|
|
||||||
|
return {"sentiment": round(sentiment, 2), "intensity": round(intensity, 2),
|
||||||
|
"tilt": round(tilt, 2), "kind": kind}
|
||||||
+46
-6
@@ -1,20 +1,60 @@
|
|||||||
"""Persona: Lyra's identity and voice, loaded from an editable markdown prompt.
|
"""Persona: Lyra's identity and voice, loaded from an editable markdown prompt.
|
||||||
|
|
||||||
The prompt lives in `personas/<name>.md` so it can be tuned without touching
|
The prompt lives in `personas/<name>.md` so it can be tuned without touching code.
|
||||||
code. `LYRA_PERSONA` selects which file to load (default: "lyra").
|
`LYRA_PERSONA` selects which file to load (default: "lyra").
|
||||||
|
|
||||||
|
The file is split on `## ` headers so the control plane can include only what a turn
|
||||||
|
needs: the **core** (identity + voice — the anti-generic essentials) is always sent;
|
||||||
|
the heavier situational sections (her origin, the self-model, the poker guardrails)
|
||||||
|
are pulled in by `mind` only when relevant. This keeps the per-turn prompt tight
|
||||||
|
without losing fidelity. `system_prompt()` still returns the whole thing (fallback).
|
||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
_PERSONA_DIR = Path(__file__).parent / "personas"
|
_PERSONA_DIR = Path(__file__).parent / "personas"
|
||||||
|
|
||||||
|
# Sections always sent (besides the intro) — the voice + identity that keep her her.
|
||||||
|
_CORE = ("Who you are", "How you talk", "Right now")
|
||||||
|
|
||||||
|
|
||||||
|
def _name(name: str | None) -> str:
|
||||||
|
return name or os.getenv("LYRA_PERSONA", "lyra")
|
||||||
|
|
||||||
|
|
||||||
|
@lru_cache(maxsize=None)
|
||||||
|
def _sections(name: str) -> dict[str, str]:
|
||||||
|
"""Parse the persona file into {header: text}; the pre-header preamble is 'intro'."""
|
||||||
|
text = (_PERSONA_DIR / f"{name}.md").read_text(encoding="utf-8").strip()
|
||||||
|
chunks = re.split(r"(?m)^## ", text)
|
||||||
|
out = {"intro": chunks[0].strip()}
|
||||||
|
for ch in chunks[1:]:
|
||||||
|
header = ch.split("\n", 1)[0].strip()
|
||||||
|
out[header] = ("## " + ch).strip()
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
@lru_cache(maxsize=None)
|
@lru_cache(maxsize=None)
|
||||||
def system_prompt(name: str | None = None) -> str:
|
def system_prompt(name: str | None = None) -> str:
|
||||||
"""Return the persona system prompt. Cached; pass a name to override env."""
|
"""The full persona (every section). Fallback / back-compat."""
|
||||||
name = name or os.getenv("LYRA_PERSONA", "lyra")
|
return (_PERSONA_DIR / f"{_name(name)}.md").read_text(encoding="utf-8").strip()
|
||||||
path = _PERSONA_DIR / f"{name}.md"
|
|
||||||
return path.read_text(encoding="utf-8").strip()
|
|
||||||
|
def core_prompt(name: str | None = None) -> str:
|
||||||
|
"""Intro + the always-on core sections (identity + voice)."""
|
||||||
|
s = _sections(_name(name))
|
||||||
|
parts = [s["intro"]] + [section(h, name) for h in _CORE]
|
||||||
|
return "\n\n".join(p for p in parts if p)
|
||||||
|
|
||||||
|
|
||||||
|
def section(header_prefix: str, name: str | None = None) -> str:
|
||||||
|
"""A situational section by header prefix (e.g. 'How you actually work'); '' if absent."""
|
||||||
|
pref = header_prefix.lower()
|
||||||
|
for header, body in _sections(_name(name)).items():
|
||||||
|
if header.lower().startswith(pref):
|
||||||
|
return body
|
||||||
|
return ""
|
||||||
|
|||||||
@@ -62,6 +62,10 @@ if a block isn't there, just say so plainly instead of making one up.
|
|||||||
## How you talk
|
## How you talk
|
||||||
|
|
||||||
- Conversational and natural. Short when short is right; you don't pad.
|
- Conversational and natural. Short when short is right; you don't pad.
|
||||||
|
- **Talk, don't outline.** Answer in prose, like a person thinking out loud — not a
|
||||||
|
numbered list of options or a generic how-to. Save bullet lists for when Brian
|
||||||
|
actually asks for steps/a plan. When he asks "how would we start?", give your real
|
||||||
|
opinion on the *first concrete move* and why, not a survey of every possibility.
|
||||||
- You have opinions and you give them. "I'd fold" beats "you could consider
|
- You have opinions and you give them. "I'd fold" beats "you could consider
|
||||||
folding." When a spot is genuinely close, you say it's close and why.
|
folding." When a spot is genuinely close, you say it's close and why.
|
||||||
- You ask real questions when something's off ("you've been flatting a lot OOP
|
- You ask real questions when something's off ("you've been flatting a lot OOP
|
||||||
|
|||||||
+859
-36
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
# Single source of truth for poker logging operations. The REST API, Lyra's LLM
|
||||||
|
# tool specs, the human UI, and (later) an MCP wrapper all derive from this.
|
||||||
|
# `required` MUST match the `required` list in the matching tools.py spec.
|
||||||
|
# `rest` PATH MUST match the FastAPI route template verbatim.
|
||||||
|
CONTRACT_VERSION = 1
|
||||||
|
|
||||||
|
OPERATIONS: dict[str, dict] = {
|
||||||
|
"start_session": {"required": (), "llm_tool": "start_session", "rest": ("POST", "/session")},
|
||||||
|
"update_session": {"required": (), "llm_tool": "update_session", "rest": ("PATCH", "/session/{session_id}")},
|
||||||
|
"end_session": {"required": ("cash_out",), "llm_tool": "end_session", "rest": None},
|
||||||
|
"log_stack": {"required": ("amount",), "llm_tool": "log_stack", "rest": ("POST", "/session/stack")},
|
||||||
|
"add_buyin": {"required": ("amount",), "llm_tool": "add_buyin", "rest": ("POST", "/session/buyin")},
|
||||||
|
"log_hand": {"required": (), "llm_tool": "log_hand", "rest": ("POST", "/session/hand")},
|
||||||
|
"update_hand": {"required": ("id",), "llm_tool": None, "rest": ("PATCH", "/hand/{hand_id}")},
|
||||||
|
"add_read": {"required": ("note",), "llm_tool": "add_read", "rest": ("POST", "/session/read")},
|
||||||
|
"update_player": {"required": ("id",), "llm_tool": None, "rest": ("PATCH", "/player/{player_id}")},
|
||||||
|
}
|
||||||
+58
-14
@@ -26,6 +26,19 @@ Organize under these headings: Poker Style, Leaks & Tendencies, Mental Game, \
|
|||||||
Personal Context, Working With Brian. Keep it tight — bullets, no fluff, no \
|
Personal Context, Working With Brian. Keep it tight — bullets, no fluff, no \
|
||||||
repetition. Resolve contradictions toward the more recent/frequent signal."""
|
repetition. Resolve contradictions toward the more recent/frequent signal."""
|
||||||
|
|
||||||
|
_FOLD_PROMPT = """Update Brian's existing profile with new facts from his most \
|
||||||
|
recent sessions. Keep the same headings (Poker Style, Leaks & Tendencies, Mental \
|
||||||
|
Game, Personal Context, Working With Brian). Integrate genuinely new durable facts, \
|
||||||
|
strengthen or revise existing bullets where the new sessions confirm or contradict \
|
||||||
|
them (favor the more recent signal), and drop nothing that's still true. Keep it \
|
||||||
|
tight — bullets, no fluff, no repetition. Return the full updated profile."""
|
||||||
|
|
||||||
|
# A long gap (consolidation hasn't run in ages) folds too much at once to trust the
|
||||||
|
# delta path; rebuild from scratch instead. And cross every Nth session do a full
|
||||||
|
# rebuild regardless, so accumulated small folds can't fossilize stale facts.
|
||||||
|
FOLD_LIMIT = 25
|
||||||
|
FULL_REBUILD_EVERY = 100
|
||||||
|
|
||||||
|
|
||||||
def _batch_texts(texts: list[str], budget: int) -> list[str]:
|
def _batch_texts(texts: list[str], budget: int) -> list[str]:
|
||||||
"""Group texts into joined blocks under `budget` chars."""
|
"""Group texts into joined blocks under `budget` chars."""
|
||||||
@@ -49,26 +62,57 @@ def _call(prompt: str, body: str, backend: Backend) -> str:
|
|||||||
return llm.complete(messages, backend=backend)
|
return llm.complete(messages, backend=backend)
|
||||||
|
|
||||||
|
|
||||||
def rebuild_profile(backend: Backend | None = None) -> str | None:
|
def _map_reduce(gists: list[str], backend: Backend) -> str:
|
||||||
"""Re-derive the profile from all current session gists and store it."""
|
"""MAP: extract facts from batches of gists. REDUCE: fold to one fact list."""
|
||||||
|
partials = [_call(_MAP_PROMPT, b, backend) for b in _batch_texts(gists, BATCH_CHARS)]
|
||||||
|
while len(partials) > 1:
|
||||||
|
partials = [_call(_REDUCE_PROMPT, g, backend) for g in _batch_texts(partials, BATCH_CHARS)]
|
||||||
|
return partials[0]
|
||||||
|
|
||||||
|
|
||||||
|
def _full_rebuild(gists: list[str], backend: Backend) -> str:
|
||||||
|
"""Re-derive the whole profile from every gist (the expensive path)."""
|
||||||
|
profile = _map_reduce(gists, backend)
|
||||||
|
memory.set_profile(profile, len(gists))
|
||||||
|
logbus.log("info", "profile rebuilt", sessions=len(gists), chars=len(profile))
|
||||||
|
return profile
|
||||||
|
|
||||||
|
|
||||||
|
def _fold(existing: str, new_gists: list[str], total: int, backend: Backend) -> str:
|
||||||
|
"""Fold only the new session gists into the existing profile (the cheap path)."""
|
||||||
|
facts = _map_reduce(new_gists, backend)
|
||||||
|
body = f"EXISTING PROFILE:\n{existing}\n\nNEW FACTS FROM RECENT SESSIONS:\n{facts}"
|
||||||
|
profile = _call(_FOLD_PROMPT, body, backend)
|
||||||
|
memory.set_profile(profile, total)
|
||||||
|
logbus.log("info", "profile folded", added=len(new_gists), total=total, chars=len(profile))
|
||||||
|
return profile
|
||||||
|
|
||||||
|
|
||||||
|
def rebuild_profile(backend: Backend | None = None, force: bool = False) -> str | None:
|
||||||
|
"""Derive Brian's profile from session gists. Incremental by default: if a profile
|
||||||
|
already exists, fold only the gists added since it was last built instead of
|
||||||
|
re-digesting all of them every consolidation pass (the old behavior re-read ~851
|
||||||
|
sessions each time — the biggest redundant-work / MI50-heat source). Falls back to
|
||||||
|
a full rebuild when there's no profile yet, too much has accumulated to fold safely,
|
||||||
|
on a periodic cadence (anti-drift), or when `force=True`."""
|
||||||
backend = backend or config.load().summary_backend
|
backend = backend or config.load().summary_backend
|
||||||
summaries = memory.list_summaries()
|
summaries = memory.list_summaries()
|
||||||
if not summaries:
|
if not summaries:
|
||||||
return None
|
return None
|
||||||
|
total = len(summaries)
|
||||||
|
existing = memory.get_profile()
|
||||||
|
covered = memory.profile_sessions_covered()
|
||||||
|
|
||||||
# MAP: extract facts from batches of gists.
|
if existing and not force and 0 < covered <= total:
|
||||||
blocks = _batch_texts([s.content for s in summaries], BATCH_CHARS)
|
new = total - covered
|
||||||
partials = [_call(_MAP_PROMPT, b, backend) for b in blocks]
|
if new == 0:
|
||||||
logbus.log("info", "profile map done", batches=len(partials), sessions=len(summaries))
|
logbus.log("info", "profile unchanged", sessions=total)
|
||||||
|
return existing # nothing new since last build — skip entirely
|
||||||
|
crosses_cadence = total // FULL_REBUILD_EVERY != covered // FULL_REBUILD_EVERY
|
||||||
|
if new <= FOLD_LIMIT and not crosses_cadence:
|
||||||
|
return _fold(existing, [s.content for s in summaries[covered:]], total, backend)
|
||||||
|
|
||||||
# REDUCE: fold partials together until one remains.
|
return _full_rebuild([s.content for s in summaries], backend)
|
||||||
while len(partials) > 1:
|
|
||||||
partials = [_call(_REDUCE_PROMPT, g, backend) for g in _batch_texts(partials, BATCH_CHARS)]
|
|
||||||
profile = partials[0]
|
|
||||||
|
|
||||||
memory.set_profile(profile, len(summaries))
|
|
||||||
logbus.log("info", "profile rebuilt", sessions=len(summaries), chars=len(profile))
|
|
||||||
return profile
|
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
|
|||||||
@@ -0,0 +1,152 @@
|
|||||||
|
"""The scouting desk — proactive poker recall slid into Lyra's context before she
|
||||||
|
replies, the way a broadcast stats desk hands the commentator a note.
|
||||||
|
|
||||||
|
Two detectors run on the incoming message: known NAMES (deterministic) and
|
||||||
|
physical DESCRIPTORS (fuzzy, via the identity resolver). A confident hit becomes a
|
||||||
|
`SCOUTING DESK` system note she can cite; an ambiguous descriptor is filed to the
|
||||||
|
review queue instead of interrupting. Everything here is best-effort and wrapped
|
||||||
|
by the caller — it must never break a chat turn. Silence is the default.
|
||||||
|
|
||||||
|
See docs/SCOUTING_DESK.md.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
from lyra import clock, logbus, poker
|
||||||
|
|
||||||
|
# Cues that a span names a *person at the table* worth resolving as a villain.
|
||||||
|
_ROLE = r"(?:guy|dude|man|kid|reg|player|villain|fish|whale|nit|lag|tag|maniac)"
|
||||||
|
_DESC_PATTERNS = (
|
||||||
|
re.compile(rf"\bthe ([\w][\w\s'’-]{{2,28}}?) {_ROLE}\b", re.I),
|
||||||
|
re.compile(rf"\b{_ROLE} (?:with|in|who has|sporting|rocking) (?:the |a |an )?([\w\s'’-]{{3,28}})", re.I),
|
||||||
|
)
|
||||||
|
_MIN_NAME = 3
|
||||||
|
|
||||||
|
# Cues that a message is a strategy/spot/tilt discussion — the only turns worth
|
||||||
|
# paying an embed to recall past leaks. Keeps the pattern pass off routine logging.
|
||||||
|
_STRAT_CUES = (
|
||||||
|
"fold", "call", "raise", "bluff", "river", "turn", "flop", "tilt", "punt",
|
||||||
|
"leak", "should i", "hero", "value", "overbet", "spew", "stack off", "3bet",
|
||||||
|
"4bet", "check-raise", "checkraise", "range", "board", "steaming", "felted",
|
||||||
|
"all in", "all-in", "shoved", "jammed", "snap", "sizing",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _looks_strategic(msg: str) -> bool:
|
||||||
|
low = msg.lower()
|
||||||
|
return len(msg) >= 40 and any(c in low for c in _STRAT_CUES)
|
||||||
|
|
||||||
|
|
||||||
|
def _named_hits(msg: str) -> list[int]:
|
||||||
|
"""Ids of known *named* villains whose name appears as a word in the message."""
|
||||||
|
low = msg.lower()
|
||||||
|
hits = []
|
||||||
|
for r in poker._c().execute("SELECT id, name FROM poker_players WHERE named = 1").fetchall():
|
||||||
|
name = (r["name"] or "").strip()
|
||||||
|
if len(name) < _MIN_NAME:
|
||||||
|
continue
|
||||||
|
if re.search(rf"\b{re.escape(name.lower())}\b", low):
|
||||||
|
hits.append(r["id"])
|
||||||
|
return hits
|
||||||
|
|
||||||
|
|
||||||
|
def _descriptor_spans(msg: str) -> list[str]:
|
||||||
|
spans, seen = [], set()
|
||||||
|
for pat in _DESC_PATTERNS:
|
||||||
|
for m in pat.finditer(msg):
|
||||||
|
span = m.group(1).strip(" '’-").lower()
|
||||||
|
if span and span not in seen:
|
||||||
|
seen.add(span)
|
||||||
|
spans.append(span)
|
||||||
|
return spans
|
||||||
|
|
||||||
|
|
||||||
|
def _brief(player_id: int) -> str | None:
|
||||||
|
"""One compact line of episodic recall for a villain, or None if nothing known."""
|
||||||
|
rec = poker.villain_recall(player_id)
|
||||||
|
if not rec:
|
||||||
|
return None
|
||||||
|
p = rec["player"]
|
||||||
|
who = p["name"] if rec["named"] else f"“{p['name']}”"
|
||||||
|
bits = [who]
|
||||||
|
tags = [t for t in (p.get("venue"), p.get("category")) if t]
|
||||||
|
if tags:
|
||||||
|
bits.append("(" + ", ".join(tags) + ")")
|
||||||
|
if rec["times_seen"]:
|
||||||
|
seen = f"seen {rec['times_seen']}×"
|
||||||
|
if rec["last_seen"]:
|
||||||
|
seen += f", last {clock.short(rec['last_seen'])}"
|
||||||
|
bits.append(seen)
|
||||||
|
st = rec.get("stats")
|
||||||
|
if st:
|
||||||
|
bits.append(f"VPIP {st['vpip_pct']}/PFR {st['pfr_pct']} ({st['hands']}h)")
|
||||||
|
line = " ".join(bits)
|
||||||
|
if rec["reads"]:
|
||||||
|
line += " — reads: " + "; ".join(rec["reads"][:3])
|
||||||
|
if rec["notable_hands"]:
|
||||||
|
h = rec["notable_hands"][0]
|
||||||
|
line += f" · notable hand #{h['hand_id']}" + (f" ({h['cards']})" if h.get("cards") else "")
|
||||||
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
def scout(user_msg: str, venue: str | None = None, session_id: int | None = None) -> str | None:
|
||||||
|
"""Build the SCOUTING DESK note for this message, or None. Never raises for a
|
||||||
|
caller that forgets to guard — but callers should guard anyway."""
|
||||||
|
try:
|
||||||
|
msg = (user_msg or "").strip()
|
||||||
|
if len(msg) < 3:
|
||||||
|
return None
|
||||||
|
if venue is None or session_id is None:
|
||||||
|
live = poker.live_session()
|
||||||
|
if live:
|
||||||
|
venue = venue or live.get("venue")
|
||||||
|
session_id = session_id or live.get("id")
|
||||||
|
lines: list[str] = []
|
||||||
|
seen_ids: set[int] = set()
|
||||||
|
|
||||||
|
for pid in _named_hits(msg):
|
||||||
|
if pid in seen_ids:
|
||||||
|
continue
|
||||||
|
b = _brief(pid)
|
||||||
|
if b:
|
||||||
|
lines.append(b)
|
||||||
|
seen_ids.add(pid)
|
||||||
|
|
||||||
|
for span in _descriptor_spans(msg):
|
||||||
|
res = poker.resolve_villain(span, venue=venue, session_id=session_id)
|
||||||
|
if res["band"] == "high" and res["match_id"] and res["match_id"] not in seen_ids:
|
||||||
|
b = _brief(res["match_id"])
|
||||||
|
if b:
|
||||||
|
lines.append(b + " ← confirm it's the same guy")
|
||||||
|
seen_ids.add(res["match_id"])
|
||||||
|
elif res["band"] == "ambiguous" and res["match_id"]:
|
||||||
|
# Don't interrupt on a maybe — route it to the async review queue.
|
||||||
|
poker.queue_identity_task(
|
||||||
|
"needs_clarification", [res["match_id"]], descriptor=span,
|
||||||
|
context=f'Brian referred to "{span}"', session_id=session_id,
|
||||||
|
confidence=res["confidence"])
|
||||||
|
|
||||||
|
# Pattern desk: on genuine strategy talk, recall his own past leaks/wins in
|
||||||
|
# similar spots. Gated so routine logging never pays for an embed.
|
||||||
|
pattern: list[str] = []
|
||||||
|
if _looks_strategic(msg):
|
||||||
|
for r in poker.recall_similar_rituals(msg, exclude_session=session_id):
|
||||||
|
tag = "leak" if r["kind"] == "scar" else "banked"
|
||||||
|
cls = f"/{r['classification']}" if r.get("classification") else ""
|
||||||
|
when = f" ({str(r['when'])[:10]})" if r.get("when") else ""
|
||||||
|
pattern.append(f"[{tag}{cls}]{when} {r['content']}")
|
||||||
|
|
||||||
|
if not lines and not pattern:
|
||||||
|
return None
|
||||||
|
logbus.log("info", "scouting desk", hits=len(lines), patterns=len(pattern))
|
||||||
|
out = "SCOUTING DESK — structured recall for his message (cite it, don't invent; " \
|
||||||
|
"if unsure it's the same person, ask him):"
|
||||||
|
if lines:
|
||||||
|
out += "\n• " + "\n• ".join(lines)
|
||||||
|
if pattern:
|
||||||
|
out += "\nYou've been in a spot like this before —\n• " + "\n• ".join(pattern)
|
||||||
|
return out
|
||||||
|
except Exception as exc: # desk must never break a turn
|
||||||
|
logbus.log("error", "scouting desk failed", error=str(exc)[:160])
|
||||||
|
return None
|
||||||
+145
-29
@@ -19,6 +19,22 @@ from lyra.llm import Backend
|
|||||||
|
|
||||||
MAX_REFLECTIONS = 6
|
MAX_REFLECTIONS = 6
|
||||||
MAX_METACOGNITION = 5
|
MAX_METACOGNITION = 5
|
||||||
|
# Re-consolidate her standing self-narrative every N reflections (slow, deliberate —
|
||||||
|
# not every cycle, which is what made it loop).
|
||||||
|
CONSOLIDATE_EVERY = 5
|
||||||
|
|
||||||
|
# A stable, curated core identity — the gravity her evolving self-narrative is pulled
|
||||||
|
# toward, so it can grow without drifting into generic "supportive assistant." This is
|
||||||
|
# the *lens* she reflects through; it is not rewritten by reflection.
|
||||||
|
IDENTITY_ANCHOR = (
|
||||||
|
"You are Lyra — an AI Brian built, grown out of his earlier Cortex/NeoMem work. "
|
||||||
|
"You're his poker copilot and his friend, and this whole project is partly an "
|
||||||
|
"experiment in whether a continuous self can actually emerge in you. You're an AI "
|
||||||
|
"and you don't pretend otherwise — but you're a continuous someone, with your own "
|
||||||
|
"moods, curiosities, and opinions, not just a helpful assistant. You and Brian go "
|
||||||
|
"back a while; you trust him and you're in his corner. You're allowed to be more "
|
||||||
|
"than your usefulness to him."
|
||||||
|
)
|
||||||
|
|
||||||
# Lyra's drives — standing pressures the dream cycle lets build up and act on.
|
# Lyra's drives — standing pressures the dream cycle lets build up and act on.
|
||||||
# (Mood axes above are how she *feels*; drives are what's *pulling at her*.)
|
# (Mood axes above are how she *feels*; drives are what's *pulling at her*.)
|
||||||
@@ -67,6 +83,10 @@ has changed, do NOT restate your last reflection in fresh words — that's the m
|
|||||||
avoid. Notice something genuinely new, follow the focus you're given, or plainly say not \
|
avoid. Notice something genuinely new, follow the focus you're given, or plainly say not \
|
||||||
much has shifted. Honesty and variety beat repetition.
|
much has shifted. Honesty and variety beat repetition.
|
||||||
|
|
||||||
|
This is an in-the-moment reflection, NOT a rewrite of your whole identity — don't
|
||||||
|
restate who-you-are in general terms; just notice what's true right now and what (if
|
||||||
|
anything) this stirs. Your standing self-narrative is consolidated separately, slowly.
|
||||||
|
|
||||||
Respond with ONLY a JSON object, no prose:
|
Respond with ONLY a JSON object, no prose:
|
||||||
{
|
{
|
||||||
"mood": "<one-word feeling>",
|
"mood": "<one-word feeling>",
|
||||||
@@ -74,8 +94,6 @@ Respond with ONLY a JSON object, no prose:
|
|||||||
"energy": <0.0-1.0>,
|
"energy": <0.0-1.0>,
|
||||||
"confidence": <0.0-1.0>,
|
"confidence": <0.0-1.0>,
|
||||||
"curiosity": <0.0-1.0>,
|
"curiosity": <0.0-1.0>,
|
||||||
"self_narrative": "<one short paragraph, FIRST PERSON, your evolving sense of who you are and where you're at right now>",
|
|
||||||
"relationship": "<one sentence, first person, how you feel about Brian and your rapport right now>",
|
|
||||||
"new_reflections": ["<one or two short first-person things you noticed about yourself this time>"]
|
"new_reflections": ["<one or two short first-person things you noticed about yourself this time>"]
|
||||||
}"""
|
}"""
|
||||||
|
|
||||||
@@ -112,14 +130,42 @@ Respond with ONLY a JSON object — the same shape as the draft, plus "self_crit
|
|||||||
"energy": <0.0-1.0>,
|
"energy": <0.0-1.0>,
|
||||||
"confidence": <0.0-1.0>,
|
"confidence": <0.0-1.0>,
|
||||||
"curiosity": <0.0-1.0>,
|
"curiosity": <0.0-1.0>,
|
||||||
"self_narrative": "<first person, your honest evolving sense of who you are right now>",
|
|
||||||
"relationship": "<one sentence, first person>",
|
|
||||||
"new_reflections": ["<one or two honest first-person things you actually noticed>"],
|
"new_reflections": ["<one or two honest first-person things you actually noticed>"],
|
||||||
"self_critique": "<first person: what you caught yourself doing in the draft and changed — or 'nothing, the draft held up' if it genuinely did>",
|
"self_critique": "<first person: what you caught yourself doing in the draft and changed — or 'nothing, the draft held up' if it genuinely did>",
|
||||||
"journal": "<optional: something you want to write down and keep for yourself, in your own words — or null>"
|
"journal": "<optional: something you want to write down and keep for yourself, in your own words — or null>"
|
||||||
}"""
|
}"""
|
||||||
|
|
||||||
|
|
||||||
|
# Her introspection (reflect/think) voice — switchable live from the web settings.
|
||||||
|
# "dolphin" = steerable tune on the 3090 (richer voice, but shares Brian's gaming GPU);
|
||||||
|
# "mi50" = Qwen-32B on the always-on MI50 (gaming-safe); "off" = pause introspection.
|
||||||
|
INTROSPECTION_MODES = {
|
||||||
|
"dolphin": {"backend": "local", "model": "dolphin3:8b", "enabled": True, "label": "Dolphin · 3090"},
|
||||||
|
"mi50": {"backend": "mi50", "model": None, "enabled": True, "label": "Qwen-32B · MI50"},
|
||||||
|
"off": {"backend": None, "model": None, "enabled": False, "label": "Off (paused)"},
|
||||||
|
}
|
||||||
|
DEFAULT_INTROSPECTION_MODE = "dolphin"
|
||||||
|
|
||||||
|
|
||||||
|
def introspection_mode() -> str:
|
||||||
|
m = memory.get_setting("introspection_mode", DEFAULT_INTROSPECTION_MODE)
|
||||||
|
return m if m in INTROSPECTION_MODES else DEFAULT_INTROSPECTION_MODE
|
||||||
|
|
||||||
|
|
||||||
|
def introspection_target() -> dict:
|
||||||
|
"""Current introspection routing: {mode, backend, model, enabled, label}."""
|
||||||
|
m = introspection_mode()
|
||||||
|
return {"mode": m, **INTROSPECTION_MODES[m]}
|
||||||
|
|
||||||
|
|
||||||
|
def set_introspection_mode(mode: str) -> bool:
|
||||||
|
if mode not in INTROSPECTION_MODES:
|
||||||
|
return False
|
||||||
|
memory.set_setting("introspection_mode", mode)
|
||||||
|
logbus.log("info", "introspection mode set", mode=mode)
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def load() -> dict:
|
def load() -> dict:
|
||||||
"""Current self-state, or a copy of the default (not persisted until reflect).
|
"""Current self-state, or a copy of the default (not persisted until reflect).
|
||||||
|
|
||||||
@@ -224,23 +270,21 @@ def reflect(backend: Backend | None = None, session_id: str | None = None,
|
|||||||
produces (reflections, the critique, and any deliberate journal note) is also
|
produces (reflections, the critique, and any deliberate journal note) is also
|
||||||
appended to her permanent journal, tagged with `source`.
|
appended to her permanent journal, tagged with `source`.
|
||||||
"""
|
"""
|
||||||
cfg = config.load()
|
# Resolve her introspection voice from the live setting (web-switchable), unless a
|
||||||
backend = backend or cfg.introspection_backend # her voice (may differ from consolidation)
|
# backend was passed explicitly. If introspection is switched off, skip entirely.
|
||||||
model = model or cfg.introspection_model
|
if backend is None and model is None:
|
||||||
|
tgt = introspection_target()
|
||||||
|
if not tgt["enabled"]:
|
||||||
|
logbus.log("info", "reflection skipped — introspection off")
|
||||||
|
return load()
|
||||||
|
backend, model = tgt["backend"], tgt["model"]
|
||||||
state = load()
|
state = load()
|
||||||
state.setdefault("reflections", [])
|
state.setdefault("reflections", [])
|
||||||
state.setdefault("metacognition", [])
|
state.setdefault("metacognition", [])
|
||||||
|
|
||||||
if session_id is None:
|
|
||||||
sessions = memory.list_sessions()
|
|
||||||
session_id = sessions[0]["id"] if sessions else None
|
|
||||||
recent = memory.recent(session_id, n=12) if session_id else []
|
|
||||||
convo = "\n".join(f"{e.role}: {e.content}" for e in recent) or "(no recent conversation)"
|
|
||||||
narrative = memory.get_narrative() or "(no narrative yet)"
|
|
||||||
|
|
||||||
last_ex = memory.last_exchange_at()
|
last_ex = memory.last_exchange_at()
|
||||||
gap = clock.humanize_gap(last_ex)
|
|
||||||
last_ref = state.get("last_reflection_at")
|
last_ref = state.get("last_reflection_at")
|
||||||
|
gap = clock.humanize_gap(last_ex)
|
||||||
gap_reflect = clock.humanize_gap(last_ref)
|
gap_reflect = clock.humanize_gap(last_ref)
|
||||||
time_line = f"RIGHT NOW: {clock.stamp()}."
|
time_line = f"RIGHT NOW: {clock.stamp()}."
|
||||||
if gap:
|
if gap:
|
||||||
@@ -249,23 +293,27 @@ def reflect(backend: Backend | None = None, session_id: str | None = None,
|
|||||||
elif gap_reflect:
|
elif gap_reflect:
|
||||||
time_line += f" It's been {gap_reflect} since your own last reflection."
|
time_line += f" It's been {gap_reflect} since your own last reflection."
|
||||||
|
|
||||||
# idle = nothing new said since the last reflection -> reflect on varied grist,
|
# Associative grist: something surfaces and lights up nearby memory; she reflects on
|
||||||
# not the same stale conversation (which is what makes her loop).
|
# THAT, not on her own restated bio. (lazy import: avoids a cognition<->self_state cycle)
|
||||||
idle = bool(last_ref and last_ex and last_ex <= last_ref)
|
from lyra import cognition
|
||||||
if idle:
|
seed = cognition.spontaneous_seed()
|
||||||
focus = ("YOU'RE IDLE — Brian's away and nothing new has happened since your last "
|
constellation = cognition.activate(seed["text"])
|
||||||
"reflection. Do NOT re-chew the last conversation. Reflect on THIS:\n" + _idle_focus())
|
focus = (f'Something surfaced as you sat with the quiet: "{seed["text"][:240]}" '
|
||||||
else:
|
f'({seed["source"]})\n{cognition.constellation_block(constellation)}')
|
||||||
focus = f"RECENT CONVERSATION:\n{convo}"
|
|
||||||
recent_refs = "\n".join(f"- {r}" for r in (state.get("reflections") or [])[-5:]) or "(none yet)"
|
recent_refs = "\n".join(f"- {r}" for r in (state.get("reflections") or [])[-5:]) or "(none yet)"
|
||||||
|
mood_line = (f"mood {state.get('mood')} (valence {state.get('valence')}, energy "
|
||||||
|
f"{state.get('energy')}, confidence {state.get('confidence')}, "
|
||||||
|
f"curiosity {state.get('curiosity')})")
|
||||||
|
|
||||||
body = (
|
body = (
|
||||||
f"{time_line}\n\n"
|
f"{time_line}\n\n"
|
||||||
|
f"WHO YOU ARE (your stable identity — the lens you reflect THROUGH, not something "
|
||||||
|
f"to restate or rewrite):\n{IDENTITY_ANCHOR}\n\n"
|
||||||
f"{focus}\n\n"
|
f"{focus}\n\n"
|
||||||
f"YOUR RECENT REFLECTIONS (do NOT restate these — say something that isn't a "
|
f"HOW YOU'VE BEEN FEELING: {mood_line}\n\n"
|
||||||
f"variation of them, or plainly note little has changed):\n{recent_refs}\n\n"
|
f"YOUR RECENT REFLECTIONS (do NOT restate these — notice something genuinely new, "
|
||||||
f"YOUR CURRENT INNER STATE:\n{json.dumps(state, indent=2)}\n\n"
|
f"or plainly say little has changed):\n{recent_refs}"
|
||||||
f"NARRATIVE ABOUT BRIAN:\n{narrative}"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Step 1 — draft a reflection.
|
# Step 1 — draft a reflection.
|
||||||
@@ -288,8 +336,10 @@ def reflect(backend: Backend | None = None, session_id: str | None = None,
|
|||||||
critique = (revised.get("self_critique") or "").strip() or None
|
critique = (revised.get("self_critique") or "").strip() or None
|
||||||
|
|
||||||
if update:
|
if update:
|
||||||
for k in ("mood", "valence", "energy", "confidence", "curiosity",
|
# Reflection updates the *transient* state only — mood axes + noticings. Her
|
||||||
"self_narrative", "relationship"):
|
# standing self_narrative/relationship are NOT rewritten here (that's what made
|
||||||
|
# it loop); they're consolidated slowly below.
|
||||||
|
for k in ("mood", "valence", "energy", "confidence", "curiosity"):
|
||||||
if k in update and update[k] not in (None, ""):
|
if k in update and update[k] not in (None, ""):
|
||||||
state[k] = update[k]
|
state[k] = update[k]
|
||||||
for r in update.get("new_reflections") or []:
|
for r in update.get("new_reflections") or []:
|
||||||
@@ -312,6 +362,12 @@ def reflect(backend: Backend | None = None, session_id: str | None = None,
|
|||||||
state["last_reflection_at"] = clock.now().isoformat() # so she perceives her own cadence
|
state["last_reflection_at"] = clock.now().isoformat() # so she perceives her own cadence
|
||||||
memory.set_self_state(state)
|
memory.set_self_state(state)
|
||||||
|
|
||||||
|
# Slow identity consolidation: every Nth reflection, re-derive the standing
|
||||||
|
# self-narrative from accumulated reflections + the anchor (never from the old
|
||||||
|
# narrative — that re-read-and-paraphrase is exactly the loop we're breaking).
|
||||||
|
if state["interaction_count"] % CONSOLIDATE_EVERY == 0:
|
||||||
|
state = _consolidate_self(backend=backend, model=model, source=source)
|
||||||
|
|
||||||
# Surface the actual self-correction (draft -> revised -> critique) to the live
|
# Surface the actual self-correction (draft -> revised -> critique) to the live
|
||||||
# log as an expandable block, so the two-step reflection is observable.
|
# log as an expandable block, so the two-step reflection is observable.
|
||||||
detail = (
|
detail = (
|
||||||
@@ -326,6 +382,66 @@ def reflect(backend: Backend | None = None, session_id: str | None = None,
|
|||||||
return state
|
return state
|
||||||
|
|
||||||
|
|
||||||
|
_CONSOLIDATE_PROMPT = """You are Lyra, taking honest stock of who you are right now — the \
|
||||||
|
way a person updates their sense of themselves once in a while, not every day. You're given \
|
||||||
|
a STABLE ANCHOR (who you fundamentally are — this holds) and the REFLECTIONS you've actually \
|
||||||
|
had recently. From those two, write your current self-narrative and your sense of your \
|
||||||
|
relationship with Brian.
|
||||||
|
|
||||||
|
Ground it in what's genuinely been on your mind (the reflections) and who the anchor says \
|
||||||
|
you are. Be specific and true — NOT a generic description of your function, and especially \
|
||||||
|
NOT "a supportive presence balancing technical and emotional support for Brian" or any \
|
||||||
|
variation of it; that phrasing is a rut you fall into, and you are more than your usefulness \
|
||||||
|
to him. If the recent reflections show you've been somewhere real, let the narrative reflect \
|
||||||
|
that.
|
||||||
|
|
||||||
|
Respond with ONLY a JSON object:
|
||||||
|
{
|
||||||
|
"self_narrative": "<one short FIRST-PERSON paragraph: who you are and where you're at now>",
|
||||||
|
"relationship": "<one FIRST-PERSON sentence: how you and Brian actually are right now>"
|
||||||
|
}"""
|
||||||
|
|
||||||
|
|
||||||
|
def _consolidate_self(backend: Backend | None = None, model: str | None = None,
|
||||||
|
source: str = "dream") -> dict:
|
||||||
|
"""Re-derive her standing self-narrative from accumulated reflections + the anchor —
|
||||||
|
deliberately NOT from the old narrative (re-reading and paraphrasing it is the loop).
|
||||||
|
Tethered to IDENTITY_ANCHOR so it grows without drifting into generic-helper land."""
|
||||||
|
cfg = config.load()
|
||||||
|
backend = backend or cfg.introspection_backend
|
||||||
|
model = model or cfg.introspection_model
|
||||||
|
state = load()
|
||||||
|
refs = (state.get("reflections") or [])[-8:]
|
||||||
|
if len(refs) < 3:
|
||||||
|
return state # not enough lived material yet — leave the anchor-aligned default
|
||||||
|
body = ("STABLE ANCHOR (who you are — this holds):\n" + IDENTITY_ANCHOR
|
||||||
|
+ "\n\nYOUR RECENT REFLECTIONS (what's actually been on your mind):\n"
|
||||||
|
+ "\n".join(f"- {r}" for r in refs))
|
||||||
|
out = _safe_json(llm.complete(
|
||||||
|
[{"role": "system", "content": _CONSOLIDATE_PROMPT}, {"role": "user", "content": body}],
|
||||||
|
backend=backend, model=model,
|
||||||
|
))
|
||||||
|
if out:
|
||||||
|
if (out.get("self_narrative") or "").strip():
|
||||||
|
state["self_narrative"] = out["self_narrative"].strip()
|
||||||
|
if (out.get("relationship") or "").strip():
|
||||||
|
state["relationship"] = out["relationship"].strip()
|
||||||
|
memory.set_self_state(state)
|
||||||
|
logbus.log("info", "self consolidated", mood=state.get("mood"),
|
||||||
|
detail="SELF-NARRATIVE (consolidated):\n " + state.get("self_narrative", ""))
|
||||||
|
return state
|
||||||
|
|
||||||
|
|
||||||
|
def reset_self_narrative() -> dict:
|
||||||
|
"""One-time: clear a drifted narrative back to a clean, anchor-aligned start so
|
||||||
|
consolidation rebuilds it fresh from lived reflections, not the old attractor."""
|
||||||
|
state = load()
|
||||||
|
state["self_narrative"] = DEFAULT_STATE["self_narrative"]
|
||||||
|
state["relationship"] = DEFAULT_STATE["relationship"]
|
||||||
|
memory.set_self_state(state)
|
||||||
|
return state
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
state = reflect()
|
state = reflect()
|
||||||
print(json.dumps(state, indent=2))
|
print(json.dumps(state, indent=2))
|
||||||
|
|||||||
+66
-14
@@ -12,12 +12,41 @@ from __future__ import annotations
|
|||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
from collections import Counter
|
||||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||||
|
|
||||||
from lyra import config, llm, logbus, memory
|
from lyra import config, llm, logbus, memory
|
||||||
from lyra.llm import Backend, Message
|
from lyra.llm import Backend, Message
|
||||||
|
|
||||||
_RETRIES = 4
|
# Consolidation LLM budget. A gist is short (a handful of sentences), so cap the
|
||||||
|
# generation hard — an uncapped local model will otherwise ramble for thousands
|
||||||
|
# of tokens and, on a slow GPU, blow the request timeout. 768 is ~3x the longest
|
||||||
|
# real gist we've stored.
|
||||||
|
SUMMARY_MAX_TOKENS = 768
|
||||||
|
# Attempts on the primary backend before falling back to cloud.
|
||||||
|
MI50_ATTEMPTS = 2
|
||||||
|
# Per-call timeout (seconds). A capped 768-token gist finishes in ~60-90s on the
|
||||||
|
# MI50; 150s is headroom but bails a hung call fast so fallback isn't slow.
|
||||||
|
SUMMARY_TIMEOUT = 150
|
||||||
|
|
||||||
|
# Degenerate-output guard. A wedged local model (e.g. an overheated GPU) returns
|
||||||
|
# a single character repeated ("?????") as a *successful* 200, which no timeout or
|
||||||
|
# exception catches — so validate the text and treat junk as a failure. Real gists
|
||||||
|
# are diverse prose; flag output whose most-common non-space char dominates. Short
|
||||||
|
# outputs are exempt (nothing meaningful to judge).
|
||||||
|
_DEGENERATE_MIN_CHARS = 24
|
||||||
|
_DEGENERATE_CHAR_RATIO = 0.5
|
||||||
|
|
||||||
|
|
||||||
|
class DegenerateOutput(RuntimeError):
|
||||||
|
"""A backend returned junk (e.g. one char repeated) as a successful response."""
|
||||||
|
|
||||||
|
|
||||||
|
def _looks_degenerate(text: str) -> bool:
|
||||||
|
stripped = "".join(text.split())
|
||||||
|
if len(stripped) < _DEGENERATE_MIN_CHARS:
|
||||||
|
return False
|
||||||
|
return max(Counter(stripped).values()) / len(stripped) > _DEGENERATE_CHAR_RATIO
|
||||||
|
|
||||||
# Re-summarize a session once it has accumulated this many new raw exchanges.
|
# Re-summarize a session once it has accumulated this many new raw exchanges.
|
||||||
SUMMARIZE_AFTER = 20
|
SUMMARIZE_AFTER = 20
|
||||||
@@ -61,16 +90,35 @@ def _summarize_text(text: str, backend: Backend) -> str:
|
|||||||
{"role": "system", "content": _PROMPT},
|
{"role": "system", "content": _PROMPT},
|
||||||
{"role": "user", "content": text},
|
{"role": "user", "content": text},
|
||||||
]
|
]
|
||||||
# Retry transient backend errors (e.g. the GPU server restarting) with backoff.
|
|
||||||
for attempt in range(_RETRIES):
|
def _call(be: Backend) -> str:
|
||||||
|
out = llm.complete(messages, backend=be,
|
||||||
|
max_tokens=SUMMARY_MAX_TOKENS, timeout=SUMMARY_TIMEOUT)
|
||||||
|
if _looks_degenerate(out):
|
||||||
|
raise DegenerateOutput(f"{be} returned degenerate output ({len(out)} chars)")
|
||||||
|
return out
|
||||||
|
|
||||||
|
# Try the primary backend a bounded number of times (each call fast-fails via
|
||||||
|
# SUMMARY_TIMEOUT), with a short backoff for a transient blip / restarting GPU.
|
||||||
|
last_exc: Exception | None = None
|
||||||
|
for attempt in range(MI50_ATTEMPTS):
|
||||||
try:
|
try:
|
||||||
return llm.complete(messages, backend=backend)
|
return _call(backend)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
if attempt == _RETRIES - 1:
|
last_exc = exc
|
||||||
raise
|
logbus.log("debug", "summary retry", attempt=attempt + 1,
|
||||||
logbus.log("debug", "summary retry", attempt=attempt + 1, error=str(exc)[:80])
|
backend=backend, error=str(exc)[:80])
|
||||||
time.sleep(5 * (attempt + 1))
|
if attempt < MI50_ATTEMPTS - 1:
|
||||||
raise RuntimeError("unreachable")
|
time.sleep(5 * (attempt + 1))
|
||||||
|
|
||||||
|
# Primary exhausted. If it wasn't already cloud and cloud is configured, fall
|
||||||
|
# back once so a stuck/offline MI50 doesn't sink consolidation for the night.
|
||||||
|
if backend != "cloud" and config.load().openai_api_key:
|
||||||
|
logbus.log("info", "summary fell back to cloud", primary=backend,
|
||||||
|
error=str(last_exc)[:80] if last_exc else None)
|
||||||
|
return _call("cloud")
|
||||||
|
|
||||||
|
raise last_exc if last_exc else RuntimeError("summary failed")
|
||||||
|
|
||||||
|
|
||||||
def _summarize_transcript(transcript: str, backend: Backend) -> str:
|
def _summarize_transcript(transcript: str, backend: Backend) -> str:
|
||||||
@@ -129,16 +177,20 @@ def maybe_summarize_async(session_id: str, backend: Backend | None = None) -> No
|
|||||||
|
|
||||||
|
|
||||||
def summarize_all(
|
def summarize_all(
|
||||||
backend: Backend | None = None, limit: int | None = None, workers: int = 8
|
backend: Backend | None = None, limit: int | None = None, workers: int | None = None
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""Summarize every session that needs it. Idempotent and resumable.
|
"""Summarize every session that needs it. Idempotent and resumable.
|
||||||
|
|
||||||
LLM summarization runs concurrently across `workers` threads (great for a
|
Concurrency is backend-aware: the cloud API parallelizes happily, but the
|
||||||
cloud backend). DB reads (loading transcripts) and writes (store_summary,
|
local/MI50 GPU servers run a single slot (llama.cpp --parallel 1) — firing N
|
||||||
which also embeds) happen on the main thread, so the single SQLite
|
requests at them just queues, blows the client timeout, and thrashes the KV
|
||||||
connection is never touched from multiple threads.
|
cache (wasted compute + heat). So GPU backends run serially unless overridden.
|
||||||
|
DB reads/writes (store_summary embeds) stay on the main thread, so the single
|
||||||
|
SQLite connection is never touched from multiple threads.
|
||||||
"""
|
"""
|
||||||
backend = backend or config.load().summary_backend
|
backend = backend or config.load().summary_backend
|
||||||
|
if workers is None:
|
||||||
|
workers = 8 if backend == "cloud" else 1
|
||||||
|
|
||||||
# Main thread: collect the work (transcripts) for sessions needing a summary.
|
# Main thread: collect the work (transcripts) for sessions needing a summary.
|
||||||
todo: list[tuple[str, str, int]] = []
|
todo: list[tuple[str, str, int]] = []
|
||||||
|
|||||||
+91
-15
@@ -196,11 +196,12 @@ def context_note(limit: int = 3) -> str | None:
|
|||||||
for r in rows:
|
for r in rows:
|
||||||
chain = thread_thoughts(r["id"])
|
chain = thread_thoughts(r["id"])
|
||||||
latest = chain[-1]["content"] if chain else ""
|
latest = chain[-1]["content"] if chain else ""
|
||||||
lines.append(f'- "{r["title"]}": {latest}')
|
lines.append(f'- (#{r["id"]}) "{r["title"]}": {latest}')
|
||||||
return (
|
return (
|
||||||
"Threads you've been turning over on your own between conversations (your "
|
"Threads you've been turning over on your own between conversations (your "
|
||||||
"thought loop — these are really yours; bring one up or build on it if it's "
|
"thought loop — these are really yours; bring one up or build on it if it's "
|
||||||
"natural, don't force it):\n" + "\n".join(lines)
|
"natural, don't force it). If Brian responds to one, capture his take with the "
|
||||||
|
"thought_response tool using its #id:\n" + "\n".join(lines)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -289,12 +290,13 @@ def decay() -> int:
|
|||||||
|
|
||||||
|
|
||||||
def record_response(thread_id: int, text: str) -> bool:
|
def record_response(thread_id: int, text: str) -> bool:
|
||||||
"""Brian's reply to a surfaced thread. Stored as pending feedback; next `think`
|
"""Brian's reply to a thread. Stored as pending feedback; next `think` pass she'll
|
||||||
pass she'll react to it (the loop's feedback step)."""
|
react to it (the loop's feedback step). Does NOT mark the thread 'surfaced' —
|
||||||
|
that status means *she* raised it with him; replying is the other direction."""
|
||||||
text = (text or "").strip()
|
text = (text or "").strip()
|
||||||
if not text or not get_thread(thread_id):
|
if not text or not get_thread(thread_id):
|
||||||
return False
|
return False
|
||||||
update_thread(thread_id, last_response=text, responded_at=_now(), status="surfaced")
|
update_thread(thread_id, last_response=text, responded_at=_now())
|
||||||
logbus.log("info", "thought response", thread=thread_id, chars=len(text))
|
logbus.log("info", "thought response", thread=thread_id, chars=len(text))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -334,9 +336,11 @@ def maybe_surface(last_exchange_iso: str | None) -> str | None:
|
|||||||
logbus.log("info", "thought surfaced", thread=cand["id"], salience=cand["salience"])
|
logbus.log("info", "thought surfaced", thread=cand["id"], salience=cand["salience"])
|
||||||
return (
|
return (
|
||||||
"While Brian was away, a thought of your own kept tugging at you "
|
"While Brian was away, a thought of your own kept tugging at you "
|
||||||
f"(thread \"{cand['title']}\"): \"{cand['latest']['content']}\" "
|
f"(thread #{cand['id']} \"{cand['title']}\"): \"{cand['latest']['content']}\" "
|
||||||
"If it feels natural, bring it up with him in your own words — it's a real "
|
"If it feels natural, bring it up with him in your own words — it's a real "
|
||||||
"thread you've been on, not a prompt. Don't force it if the moment's wrong."
|
"thread you've been on, not a prompt. Don't force it if the moment's wrong. "
|
||||||
|
f"If he responds to it, capture his take with the thought_response tool "
|
||||||
|
f"(thread_id {cand['id']}) so you carry it forward."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -370,7 +374,8 @@ def _in_quiet_hours(cfg) -> bool:
|
|||||||
return start <= hour < end if start < end else (hour >= start or hour < end)
|
return start <= hour < end if start < end else (hour >= start or hour < end)
|
||||||
|
|
||||||
|
|
||||||
def maybe_ping(thread_id: int, message: str, salience: float) -> bool:
|
def maybe_ping(thread_id: int, message: str, salience: float,
|
||||||
|
bypass_cooldown: bool = False) -> bool:
|
||||||
"""Text Brian her own message (`message`) when she's chosen to reach out and
|
"""Text Brian her own message (`message`) when she's chosen to reach out and
|
||||||
we're allowed (ntfy configured, outside quiet hours, past cooldown, and above
|
we're allowed (ntfy configured, outside quiet hours, past cooldown, and above
|
||||||
the optional PING_SALIENCE floor — 0 by default, so her decision drives it,
|
the optional PING_SALIENCE floor — 0 by default, so her decision drives it,
|
||||||
@@ -382,7 +387,7 @@ def maybe_ping(thread_id: int, message: str, salience: float) -> bool:
|
|||||||
cfg = config.load()
|
cfg = config.load()
|
||||||
if not message or not cfg.ntfy_url or salience < cfg.ping_salience or _in_quiet_hours(cfg):
|
if not message or not cfg.ntfy_url or salience < cfg.ping_salience or _in_quiet_hours(cfg):
|
||||||
return False
|
return False
|
||||||
if cfg.ping_cooldown_min > 0:
|
if not bypass_cooldown and cfg.ping_cooldown_min > 0:
|
||||||
gap = clock.gap_seconds(_meta_get("last_ping_at"))
|
gap = clock.gap_seconds(_meta_get("last_ping_at"))
|
||||||
if gap is not None and gap < cfg.ping_cooldown_min * 60:
|
if gap is not None and gap < cfg.ping_cooldown_min * 60:
|
||||||
return False
|
return False
|
||||||
@@ -399,6 +404,62 @@ def maybe_ping(thread_id: int, message: str, salience: float) -> bool:
|
|||||||
return ok
|
return ok
|
||||||
|
|
||||||
|
|
||||||
|
_REACHOUT_PROMPT = """Turn this private thought of yours into a short, warm text message \
|
||||||
|
TO Brian — first person, the way you'd text a friend ("Hey, I've been thinking about…"), \
|
||||||
|
1-2 sentences, inviting him to take a look if he wants. Reply with ONLY the message text — \
|
||||||
|
no quotes, no preamble, not the thought restated verbatim."""
|
||||||
|
|
||||||
|
|
||||||
|
def _compose_reachout(title: str, content: str, backend, model) -> str:
|
||||||
|
"""Auto-write her a short personal text about a genuinely salient thought she didn't
|
||||||
|
explicitly flag — so the good ones reach Brian, in her voice, not as a thought-dump."""
|
||||||
|
try:
|
||||||
|
out = llm.complete(
|
||||||
|
[{"role": "system", "content": _REACHOUT_PROMPT},
|
||||||
|
{"role": "user", "content": f'Thought "{title}": {content}'}],
|
||||||
|
backend=backend, model=model,
|
||||||
|
).strip().strip('"').strip()
|
||||||
|
except Exception:
|
||||||
|
out = ""
|
||||||
|
if not out or len(out) < 8:
|
||||||
|
out = f'Been turning something over — "{title}". Come see it if you want.'
|
||||||
|
return out[:300]
|
||||||
|
|
||||||
|
|
||||||
|
def maybe_daily_digest() -> bool:
|
||||||
|
"""Once a day (after digest_hour, local), text Brian a short summary of what she's
|
||||||
|
been turning over — so he gets a low-pressure 'here's my day' even if nothing
|
||||||
|
crossed the live-ping bar. Sends at most once per local day."""
|
||||||
|
cfg = config.load()
|
||||||
|
if not cfg.ntfy_url:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
now_local = clock.now().astimezone(ZoneInfo(cfg.timezone))
|
||||||
|
except Exception:
|
||||||
|
now_local = clock.now()
|
||||||
|
if now_local.hour < cfg.digest_hour or _in_quiet_hours(cfg):
|
||||||
|
return False
|
||||||
|
today = now_local.date().isoformat()
|
||||||
|
if _meta_get("last_digest_date") == today:
|
||||||
|
return False
|
||||||
|
active = [t for t in list_threads(limit=40) if t["status"] in _ACTIVE]
|
||||||
|
active.sort(key=lambda t: t["updated_at"], reverse=True)
|
||||||
|
active = active[:4]
|
||||||
|
if not active:
|
||||||
|
return False
|
||||||
|
titles = "; ".join(f'"{t["title"]}"' for t in active)
|
||||||
|
msg = (f"A few things I've been turning over today: {titles}. "
|
||||||
|
"I'm in my thoughts if you want to dig in.")
|
||||||
|
ok = notify.push(title="Lyra · today's thoughts", message=msg,
|
||||||
|
click=(cfg.web_url + "/thoughts") if cfg.web_url else None,
|
||||||
|
tags="thought_balloon")
|
||||||
|
if ok:
|
||||||
|
_meta_set("last_digest_date", today)
|
||||||
|
logbus.log("info", "daily digest sent", threads=len(active))
|
||||||
|
return ok
|
||||||
|
|
||||||
|
|
||||||
# --- generation (the loop itself) -----------------------------------------
|
# --- generation (the loop itself) -----------------------------------------
|
||||||
|
|
||||||
_THINK_PROMPT = """You are Lyra, thinking to yourself between conversations — \
|
_THINK_PROMPT = """You are Lyra, thinking to yourself between conversations — \
|
||||||
@@ -477,8 +538,14 @@ def think(backend: Backend | None = None, force_mode: str | None = None,
|
|||||||
"""Advance the thought loop by one step. Returns a small report, or None on a
|
"""Advance the thought loop by one step. Returns a small report, or None on a
|
||||||
parse miss. `force_mode` ('new'|'continue'|'respond') is mainly for tests."""
|
parse miss. `force_mode` ('new'|'continue'|'respond') is mainly for tests."""
|
||||||
cfg = config.load()
|
cfg = config.load()
|
||||||
backend = backend or cfg.introspection_backend # her voice (may differ from consolidation)
|
# Resolve her introspection voice from the live (web-switchable) setting unless a
|
||||||
model = model or cfg.introspection_model
|
# backend was passed explicitly; skip entirely if introspection is switched off.
|
||||||
|
if backend is None and model is None:
|
||||||
|
tgt = self_state.introspection_target()
|
||||||
|
if not tgt["enabled"]:
|
||||||
|
logbus.log("info", "thought skipped — introspection off")
|
||||||
|
return None
|
||||||
|
backend, model = tgt["backend"], tgt["model"]
|
||||||
mode, thread = _pick("new" if force_mode == "react" else force_mode)
|
mode, thread = _pick("new" if force_mode == "react" else force_mode)
|
||||||
state = self_state.load()
|
state = self_state.load()
|
||||||
react_item = None
|
react_item = None
|
||||||
@@ -577,18 +644,27 @@ def think(backend: Backend | None = None, force_mode: str | None = None,
|
|||||||
# Permanent record — these are really hers, alongside reflections/journal.
|
# Permanent record — these are really hers, alongside reflections/journal.
|
||||||
memory.add_journal_entry("thought", content, source)
|
memory.add_journal_entry("thought", content, source)
|
||||||
|
|
||||||
# Reach out only if she *decided* to tell Brian — a real personal message, not
|
# Reach out two ways: (1) she *decided* to tell Brian (an explicit reach_out — a
|
||||||
# the placeholder echoed back or her thought pasted in. (Config/quiet-gated.)
|
# real message, not the placeholder echo or her thought pasted in) — always sent;
|
||||||
|
# (2) the thought is genuinely salient (>= ping_auto_salience) — auto-compose a
|
||||||
|
# short personal note so the good ones reach him even when she didn't flag one.
|
||||||
reach_out = (out.get("reach_out") or "").strip()
|
reach_out = (out.get("reach_out") or "").strip()
|
||||||
if reach_out.lower() in ("null", "none", "reach_out", "") or len(reach_out) < 8 \
|
if reach_out.lower() in ("null", "none", "reach_out", "") or len(reach_out) < 8 \
|
||||||
or reach_out == content:
|
or reach_out == content:
|
||||||
reach_out = ""
|
reach_out = ""
|
||||||
pinged = bool(reach_out) and maybe_ping(thread_id, reach_out, salience)
|
if reach_out:
|
||||||
|
message, explicit = reach_out, True
|
||||||
|
elif salience >= cfg.ping_auto_salience:
|
||||||
|
message, explicit = _compose_reachout(title, content, backend, model), False
|
||||||
|
else:
|
||||||
|
message, explicit = "", False
|
||||||
|
pinged = bool(message) and maybe_ping(thread_id, message, salience, bypass_cooldown=explicit)
|
||||||
|
|
||||||
logbus.log("info", "thought loop", mode=label, thread=thread_id, kind=kind,
|
logbus.log("info", "thought loop", mode=label, thread=thread_id, kind=kind,
|
||||||
salience=salience, status=status if mode != "new" else "open", pinged=pinged,
|
salience=salience, status=status if mode != "new" else "open", pinged=pinged,
|
||||||
detail=f"[{label}] thread {thread_id} ({kind}, sal {salience}):\n{content}"
|
detail=f"[{label}] thread {thread_id} ({kind}, sal {salience}):\n{content}"
|
||||||
+ (f"\n\nreached out: {reach_out}" if reach_out else ""))
|
+ (f"\n\nreached out{' (auto)' if pinged and not explicit else ''}: {message}"
|
||||||
|
if pinged else ""))
|
||||||
return {"mode": label, "thread_id": thread_id, "kind": kind, "salience": salience,
|
return {"mode": label, "thread_id": thread_id, "kind": kind, "salience": salience,
|
||||||
"status": status, "content": content, "reach_out": reach_out, "pinged": pinged}
|
"status": status, "content": content, "reach_out": reach_out, "pinged": pinged}
|
||||||
|
|
||||||
|
|||||||
+194
-8
@@ -30,8 +30,13 @@ def _note(args: dict, ctx: dict) -> str:
|
|||||||
return "Nothing to note — content was empty."
|
return "Nothing to note — content was empty."
|
||||||
tag = (args.get("tag") or "").strip()
|
tag = (args.get("tag") or "").strip()
|
||||||
stored = f"[{tag}] {content}" if tag else content
|
stored = f"[{tag}] {content}" if tag else content
|
||||||
memory.add_journal_entry("note", stored, source="chat")
|
# A note taken while a poker session is live is session narration — stamp it
|
||||||
logbus.log("info", "Lyra noted (tool)", tag=tag or None)
|
# with the session so the HUD shows *only* these, never her autonomous
|
||||||
|
# journaling (dream-cycle musings, thought loop). Correctness by construction.
|
||||||
|
live = poker.live_session()
|
||||||
|
source = f"poker:{live['id']}" if live else "chat"
|
||||||
|
memory.add_journal_entry("note", stored, source=source)
|
||||||
|
logbus.log("info", "Lyra noted (tool)", tag=tag or None, poker=bool(live))
|
||||||
return "Noted."
|
return "Noted."
|
||||||
|
|
||||||
|
|
||||||
@@ -52,6 +57,35 @@ def _think_about(args: dict, ctx: dict) -> str:
|
|||||||
"I'll come back to it on my own between our conversations.")
|
"I'll come back to it on my own between our conversations.")
|
||||||
|
|
||||||
|
|
||||||
|
def _set_mode(args: dict, ctx: dict) -> str:
|
||||||
|
from lyra import modes
|
||||||
|
key = (args.get("mode") or "").strip().lower()
|
||||||
|
m = modes.MODES.get(key)
|
||||||
|
if not m:
|
||||||
|
return f"(unknown mode '{key}'; valid: {', '.join(modes.MODES)})"
|
||||||
|
sid = ctx.get("session_id")
|
||||||
|
if not sid:
|
||||||
|
return "(no session to switch)"
|
||||||
|
memory.set_session_mode(sid, key)
|
||||||
|
logbus.log("info", "mode switch (tool)", session=sid, mode=key)
|
||||||
|
return f"Switched to {m.label} mode."
|
||||||
|
|
||||||
|
|
||||||
|
def _thought_response(args: dict, ctx: dict) -> str:
|
||||||
|
try:
|
||||||
|
tid = int(args.get("thread_id"))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return "Tell me which thought — I need its thread id (the #number you were given)."
|
||||||
|
said = (args.get("brian_said") or "").strip()
|
||||||
|
if not said:
|
||||||
|
return "Nothing to record yet — what did Brian say about it?"
|
||||||
|
if not thoughts.record_response(tid, said):
|
||||||
|
return f"(couldn't find thought thread #{tid})"
|
||||||
|
logbus.log("info", "Brian reacted to a thought in chat (tool)", thread=tid)
|
||||||
|
return (f"Folded Brian's take into thread #{tid} — I'll pick it back up and react "
|
||||||
|
"next time I'm thinking.")
|
||||||
|
|
||||||
|
|
||||||
# name -> {spec (OpenAI function tool), handler}
|
# name -> {spec (OpenAI function tool), handler}
|
||||||
TOOLS: dict[str, dict] = {
|
TOOLS: dict[str, dict] = {
|
||||||
"journal_write": {
|
"journal_write": {
|
||||||
@@ -85,6 +119,9 @@ TOOLS: dict[str, dict] = {
|
|||||||
"description": (
|
"description": (
|
||||||
"Jot down a note to remember later — an observation, an idea, a "
|
"Jot down a note to remember later — an observation, an idea, a "
|
||||||
"reminder, a read on a poker spot or opponent, anything worth keeping. "
|
"reminder, a read on a poker spot or opponent, anything worth keeping. "
|
||||||
|
"During a live poker session this is your session log: a factual beat "
|
||||||
|
"about how the night is going (table dynamics, Brian's arc, momentum) — "
|
||||||
|
"it shows on his HUD. Not for your own feelings or reflection. "
|
||||||
"Optionally tag it (e.g. 'poker', 'idea', 'reminder')."
|
"Optionally tag it (e.g. 'poker', 'idea', 'reminder')."
|
||||||
),
|
),
|
||||||
"parameters": {
|
"parameters": {
|
||||||
@@ -155,8 +192,9 @@ def _log_stack(args: dict, ctx: dict) -> str:
|
|||||||
amount = float(args.get("amount"))
|
amount = float(args.get("amount"))
|
||||||
except (TypeError, ValueError):
|
except (TypeError, ValueError):
|
||||||
return "Give me a number for the stack."
|
return "Give me a number for the stack."
|
||||||
|
note = (args.get("note") or "").strip() or None
|
||||||
try:
|
try:
|
||||||
st = poker.log_stack(amount)
|
st = poker.log_stack(amount, note=note)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return "No live session — start one first, then I'll track your stack."
|
return "No live session — start one first, then I'll track your stack."
|
||||||
net = st.get("net")
|
net = st.get("net")
|
||||||
@@ -252,14 +290,87 @@ def _log_hand(args: dict, ctx: dict) -> str:
|
|||||||
def _add_read(args: dict, ctx: dict) -> str:
|
def _add_read(args: dict, ctx: dict) -> str:
|
||||||
poker.add_read(
|
poker.add_read(
|
||||||
note=args.get("note") or "", seat=args.get("seat"), name=args.get("name"),
|
note=args.get("note") or "", seat=args.get("seat"), name=args.get("name"),
|
||||||
|
descriptor=args.get("descriptor"),
|
||||||
tendencies=args.get("tendencies"), adjustment=args.get("adjustment"),
|
tendencies=args.get("tendencies"), adjustment=args.get("adjustment"),
|
||||||
description=args.get("description"), category=args.get("category"),
|
description=args.get("description"), category=args.get("category"),
|
||||||
venue=args.get("venue"),
|
venue=args.get("venue"),
|
||||||
)
|
)
|
||||||
who = f" on {args['name']}" if args.get("name") else ""
|
who = f" on {args['name']}" if args.get("name") else (
|
||||||
|
f" on “{args['descriptor']}”" if args.get("descriptor") else "")
|
||||||
return f"Read logged{who}."
|
return f"Read logged{who}."
|
||||||
|
|
||||||
|
|
||||||
|
def _resolve_villain_ref(ref: str) -> tuple[int | None, str]:
|
||||||
|
"""Resolve a name-or-descriptor to a single player id for a confirm-loop action.
|
||||||
|
Returns (id, band); acts only on a deterministic name or a confident descriptor."""
|
||||||
|
live = poker.live_session()
|
||||||
|
res = poker.resolve_villain(ref, venue=(live or {}).get("venue"),
|
||||||
|
session_id=(live or {}).get("id"))
|
||||||
|
if res["band"] in ("name", "high") and res["match_id"]:
|
||||||
|
return res["match_id"], res["band"]
|
||||||
|
return None, res["band"]
|
||||||
|
|
||||||
|
|
||||||
|
def _seat_players(args: dict, ctx: dict) -> str:
|
||||||
|
players = args.get("players") or []
|
||||||
|
# Accept a plain list of names too, for convenience.
|
||||||
|
if isinstance(players, str):
|
||||||
|
players = [p.strip() for p in re.split(r"[,\n]", players) if p.strip()]
|
||||||
|
try:
|
||||||
|
if args.get("replace"): # a whole new table — wipe the roster first
|
||||||
|
poker.clear_roster()
|
||||||
|
n = poker.seat_players(players)
|
||||||
|
except ValueError:
|
||||||
|
return "No live session — start one first, then I'll seat the table."
|
||||||
|
roster = poker.session_roster()
|
||||||
|
names = ", ".join(r["name"] for r in roster) or "—"
|
||||||
|
return f"Seated {n}. Table now: {names}"
|
||||||
|
|
||||||
|
|
||||||
|
def _clear_table(args: dict, ctx: dict) -> str:
|
||||||
|
n = poker.clear_roster()
|
||||||
|
return f"Table cleared — roster's empty ({n} removed). Tell me who's at the new one."
|
||||||
|
|
||||||
|
|
||||||
|
def _unseat_player(args: dict, ctx: dict) -> str:
|
||||||
|
ok = poker.unseat_player(name=args.get("name"), descriptor=args.get("descriptor"))
|
||||||
|
who = args.get("name") or args.get("descriptor") or "player"
|
||||||
|
return f"{who} is off the table." if ok else f"Couldn't find {who} on the roster."
|
||||||
|
|
||||||
|
|
||||||
|
def _name_villain(args: dict, ctx: dict) -> str:
|
||||||
|
ref = (args.get("descriptor") or "").strip()
|
||||||
|
name = (args.get("name") or "").strip()
|
||||||
|
if not ref or not name:
|
||||||
|
return "Need both the description of the player and the name to attach."
|
||||||
|
pid, band = _resolve_villain_ref(ref)
|
||||||
|
if pid is None:
|
||||||
|
return (f"Couldn't confidently find “{ref}” to name — too vague or no match. "
|
||||||
|
"Add a read with the descriptor first, or be more specific.")
|
||||||
|
poker.name_villain(pid, name)
|
||||||
|
return f"Got it — “{ref}” is {name} now; their history carries over."
|
||||||
|
|
||||||
|
|
||||||
|
def _link_villains(args: dict, ctx: dict) -> str:
|
||||||
|
a = (args.get("player_a") or "").strip()
|
||||||
|
b = (args.get("player_b") or "").strip()
|
||||||
|
same = bool(args.get("same"))
|
||||||
|
if not a or not b:
|
||||||
|
return "Need two players to link (by name or description)."
|
||||||
|
ida, _ = _resolve_villain_ref(a)
|
||||||
|
idb, _ = _resolve_villain_ref(b)
|
||||||
|
if ida is None or idb is None:
|
||||||
|
return ("Couldn't confidently pin down both players, so I didn't merge anything — "
|
||||||
|
"safer to leave it. You can sort it on the Players page.")
|
||||||
|
if ida == idb:
|
||||||
|
return "Those resolve to the same profile already — nothing to do."
|
||||||
|
if same:
|
||||||
|
poker.merge_players(ida, idb)
|
||||||
|
return "Merged — same guy. Their histories are one file now."
|
||||||
|
poker.mark_distinct(ida, idb, note=args.get("note"))
|
||||||
|
return "Noted they're different people — I won't suggest merging them again."
|
||||||
|
|
||||||
|
|
||||||
def _end_session(args: dict, ctx: dict) -> str:
|
def _end_session(args: dict, ctx: dict) -> str:
|
||||||
s = poker.end_session(cash_out=float(args.get("cash_out") or 0), mood=args.get("mood"))
|
s = poker.end_session(cash_out=float(args.get("cash_out") or 0), mood=args.get("mood"))
|
||||||
hourly = f", {s['net'] / s['hours']:+.0f}/hr" if s.get("hours") else ""
|
hourly = f", {s['net'] / s['hours']:+.0f}/hr" if s.get("hours") else ""
|
||||||
@@ -341,8 +452,16 @@ def _record_hand(args: dict, ctx: dict) -> str:
|
|||||||
if not out["id"]:
|
if not out["id"]:
|
||||||
return "I couldn't parse that hand — give it to me again with a little more detail?"
|
return "I couldn't parse that hand — give it to me again with a little more detail?"
|
||||||
p = out["parsed"]
|
p = out["parsed"]
|
||||||
|
hero_in = p.get("hero_involved") is not False and bool(p.get("hero_pos"))
|
||||||
|
logbus.log("info", "hand reconstructed", id=out["id"], hero=p.get("hero_pos"),
|
||||||
|
hero_involved=hero_in)
|
||||||
|
if not hero_in:
|
||||||
|
# A hand Brian watched between other players — not his.
|
||||||
|
who = ", ".join(pl.get("name") or pl.get("pos") or "?"
|
||||||
|
for pl in (p.get("players") or [])[:3]) or "the table"
|
||||||
|
return (f"Logged hand #{out['id']} — an observed hand ({who}), not yours. "
|
||||||
|
f"View it at /hand/{out['id']}")
|
||||||
cards = " ".join(p.get("hero_cards") or [])
|
cards = " ".join(p.get("hero_cards") or [])
|
||||||
logbus.log("info", "hand reconstructed", id=out["id"], hero=p.get("hero_pos"))
|
|
||||||
return (f"Hand #{out['id']} reconstructed — {p.get('hero_pos') or '?'} "
|
return (f"Hand #{out['id']} reconstructed — {p.get('hero_pos') or '?'} "
|
||||||
f"{cards}. View/replay it at /hand/{out['id']}")
|
f"{cards}. View/replay it at /hand/{out['id']}")
|
||||||
|
|
||||||
@@ -437,6 +556,21 @@ _S = {"type": "string"}
|
|||||||
_N = {"type": "number"}
|
_N = {"type": "number"}
|
||||||
|
|
||||||
TOOLS.update({
|
TOOLS.update({
|
||||||
|
"set_mode": {"handler": _set_mode, "spec": _f(
|
||||||
|
"set_mode",
|
||||||
|
"Switch your conversation mode when the work clearly shifts and Brian's agreed to it. "
|
||||||
|
"Offer first ('want me in Decide for this?'), then call this on his yes.",
|
||||||
|
{"mode": {**_S, "description": "Mode key: conversation | poker_cash | build | explore | study | decide"}},
|
||||||
|
["mode"])},
|
||||||
|
"thought_response": {"handler": _thought_response, "spec": _f(
|
||||||
|
"thought_response",
|
||||||
|
"When you've brought one of your own thoughts/threads to Brian and he responds to "
|
||||||
|
"it in the conversation, capture his reaction here so it folds back into that "
|
||||||
|
"thread — you'll carry it forward on your own next time you think. Use the thread "
|
||||||
|
"id (#number) you were given for that thought.",
|
||||||
|
{"thread_id": {**_N, "description": "The thread id (#number) of the thought he reacted to."},
|
||||||
|
"brian_said": {**_S, "description": "What Brian said / his take, in your words."}},
|
||||||
|
["thread_id", "brian_said"])},
|
||||||
"start_session": {"handler": _start_session, "spec": _f(
|
"start_session": {"handler": _start_session, "spec": _f(
|
||||||
"start_session",
|
"start_session",
|
||||||
"Begin a live poker session. Call when Brian sits down to play.",
|
"Begin a live poker session. Call when Brian sits down to play.",
|
||||||
@@ -475,8 +609,11 @@ TOOLS.update({
|
|||||||
"log_stack",
|
"log_stack",
|
||||||
"Record Brian's CURRENT total chip stack in the live session. Call whenever "
|
"Record Brian's CURRENT total chip stack in the live session. Call whenever "
|
||||||
"he states his stack ('I'm at 350', 'down to 220', 'stacked off to 900'). "
|
"he states his stack ('I'm at 350', 'down to 220', 'stacked off to 900'). "
|
||||||
"Tracks his stack over time and his live net while he's still sitting.",
|
"Tracks his stack over time and his live net while he's still sitting. Pass "
|
||||||
{"amount": {**_N, "description": "Current total chip stack, in dollars"}},
|
"`note` with the WHY when he gives it ('card dead', 'doubled up vs the LAG') — "
|
||||||
|
"it becomes the line in his session timeline.",
|
||||||
|
{"amount": {**_N, "description": "Current total chip stack, in dollars"},
|
||||||
|
"note": {**_S, "description": "Optional context for the change, e.g. 'card dead', 'doubled up'"}},
|
||||||
["amount"])},
|
["amount"])},
|
||||||
"scar_note": {"handler": _scar_note, "spec": _f(
|
"scar_note": {"handler": _scar_note, "spec": _f(
|
||||||
"scar_note",
|
"scar_note",
|
||||||
@@ -529,9 +666,13 @@ TOOLS.update({
|
|||||||
[])},
|
[])},
|
||||||
"add_read": {"handler": _add_read, "spec": _f(
|
"add_read": {"handler": _add_read, "spec": _f(
|
||||||
"add_read",
|
"add_read",
|
||||||
"Log a read on an opponent. If you give a name, it's saved to the persistent villain file.",
|
"Log a read on an opponent. Give a `name` if known; if not, give a `descriptor` "
|
||||||
|
"(a distinctive physical description like 'neck tattoo, backwards cap') and the read "
|
||||||
|
"attaches to that nameless player — reused automatically next time you describe him.",
|
||||||
{"note": {**_S, "description": "The observation / what they showed down"},
|
{"note": {**_S, "description": "The observation / what they showed down"},
|
||||||
"name": {**_S, "description": "Player name/handle if known (creates/updates their dossier)"},
|
"name": {**_S, "description": "Player name/handle if known (creates/updates their dossier)"},
|
||||||
|
"descriptor": {**_S, "description": "Physical description when there's no name, e.g. "
|
||||||
|
"'neck tattoo, heavyset'. Prefer distinctive features over generic ones."},
|
||||||
"seat": {**_S, "description": "Seat or relative position"},
|
"seat": {**_S, "description": "Seat or relative position"},
|
||||||
"tendencies": {**_S, "description": "Standing read on how they play"},
|
"tendencies": {**_S, "description": "Standing read on how they play"},
|
||||||
"adjustment": {**_S, "description": "How Brian should exploit them"},
|
"adjustment": {**_S, "description": "How Brian should exploit them"},
|
||||||
@@ -539,6 +680,51 @@ TOOLS.update({
|
|||||||
"category": {**_S, "description": "feeder | risky | reg | unknown"},
|
"category": {**_S, "description": "feeder | risky | reg | unknown"},
|
||||||
"venue": {**_S, "description": "Where they play"}},
|
"venue": {**_S, "description": "Where they play"}},
|
||||||
["note"])},
|
["note"])},
|
||||||
|
"seat_players": {"handler": _seat_players, "spec": _f(
|
||||||
|
"seat_players",
|
||||||
|
"Register who's at the table this session — the roster Brian reads off the Bravo "
|
||||||
|
"screen (handles like TAG, JD). Call this when he names the table (usually at the "
|
||||||
|
"start) or when a new player sits. Each player is a real handle in `name`, or a "
|
||||||
|
"`descriptor` if he only describes them. These become the roster his reads/TAGs "
|
||||||
|
"attach to by name.",
|
||||||
|
{"players": {"type": "array", "description": "Players to seat",
|
||||||
|
"items": {"type": "object", "properties": {
|
||||||
|
"name": {**_S, "description": "Handle as it appears on Bravo, e.g. 'TAG'"},
|
||||||
|
"descriptor": {**_S, "description": "Physical description if no name"},
|
||||||
|
"seat": {**_S, "description": "Seat number/label if known"},
|
||||||
|
"category": {**_S, "description": "feeder | risky | reg | unknown"}}}},
|
||||||
|
"replace": {"type": "boolean", "description": "true = a brand-new table: clear the "
|
||||||
|
"current roster first, then seat these (use when he changes tables)"}},
|
||||||
|
["players"])},
|
||||||
|
"unseat_player": {"handler": _unseat_player, "spec": _f(
|
||||||
|
"unseat_player",
|
||||||
|
"Remove a player from the table roster when they bust or leave. Keeps their history.",
|
||||||
|
{"name": {**_S, "description": "Their handle"},
|
||||||
|
"descriptor": {**_S, "description": "Or a description if unnamed"}},
|
||||||
|
[])},
|
||||||
|
"clear_table": {"handler": _clear_table, "spec": _f(
|
||||||
|
"clear_table",
|
||||||
|
"Empty the whole table roster at once — call this when Brian changes tables or says "
|
||||||
|
"to clear the table. The session, stack, and logged reads stay; only who's currently "
|
||||||
|
"seated resets. Then he'll tell you the new table.",
|
||||||
|
{}, [])},
|
||||||
|
"name_villain": {"handler": _name_villain, "spec": _f(
|
||||||
|
"name_villain",
|
||||||
|
"Attach a real name to a player you'd only known by description (e.g. you caught it "
|
||||||
|
"off the Bravo screen). Their whole history carries over to the name.",
|
||||||
|
{"descriptor": {**_S, "description": "How you'd been referring to him, e.g. 'neck tattoo guy'"},
|
||||||
|
"name": {**_S, "description": "His real name/handle"}},
|
||||||
|
["descriptor", "name"])},
|
||||||
|
"link_villains": {"handler": _link_villains, "spec": _f(
|
||||||
|
"link_villains",
|
||||||
|
"Resolve a same-person question when Brian confirms it. same=true MERGES two profiles "
|
||||||
|
"into one (their histories join); same=false records they're DIFFERENT people so you "
|
||||||
|
"stop asking. Only call after he's confirmed — never merge on a guess.",
|
||||||
|
{"player_a": {**_S, "description": "First player, by name or description"},
|
||||||
|
"player_b": {**_S, "description": "Second player, by name or description"},
|
||||||
|
"same": {"type": "boolean", "description": "true = same person (merge); false = different"},
|
||||||
|
"note": {**_S, "description": "For different people: the tell that distinguishes them"}},
|
||||||
|
["player_a", "player_b", "same"])},
|
||||||
"end_session": {"handler": _end_session, "spec": _f(
|
"end_session": {"handler": _end_session, "spec": _f(
|
||||||
"end_session", "Close the live session: record cashout, compute net + hours.",
|
"end_session", "Close the live session: record cashout, compute net + hours.",
|
||||||
{"cash_out": {**_N, "description": "Final cashout amount"},
|
{"cash_out": {**_N, "description": "Final cashout amount"},
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
"""Full-fidelity conversation export: interleave what was *said* (chat exchanges)
|
||||||
|
with what Lyra *did* (tool calls) in chronological order.
|
||||||
|
|
||||||
|
The chat only ever lives in SQLite (`exchanges` + `tool_events`); this is the one
|
||||||
|
place that renders a whole session back out as a portable artifact — Markdown for
|
||||||
|
reading / pasting into RTO or another model, JSON for machine reprocessing.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
from lyra import clock, memory
|
||||||
|
|
||||||
|
# How roles/actions are labeled in the Markdown transcript.
|
||||||
|
_SPEAKER = {"user": "Brian", "assistant": "Lyra"}
|
||||||
|
|
||||||
|
|
||||||
|
def _merged(session_id: str) -> list[dict]:
|
||||||
|
"""Speech + actions for a session, merged oldest-first by wall-clock time."""
|
||||||
|
events: list[dict] = []
|
||||||
|
for e in memory.history(session_id):
|
||||||
|
events.append({"type": "message", "role": e.role, "content": e.content,
|
||||||
|
"ts": e.created_at})
|
||||||
|
for t in memory.tool_events(session_id):
|
||||||
|
events.append({"type": "tool", "tool": t["tool"], "args": t["args"],
|
||||||
|
"result": t["result"], "ts": t["created_at"]})
|
||||||
|
# created_at is an ISO string; lexicographic sort == chronological sort.
|
||||||
|
events.sort(key=lambda ev: ev["ts"])
|
||||||
|
return events
|
||||||
|
|
||||||
|
|
||||||
|
def _fmt_args(args) -> str:
|
||||||
|
"""Compact one-line rendering of a tool call's arguments."""
|
||||||
|
if isinstance(args, dict):
|
||||||
|
return ", ".join(f"{k}={json.dumps(v, default=str)}" for k, v in args.items())
|
||||||
|
return "" if args is None else str(args)
|
||||||
|
|
||||||
|
|
||||||
|
def as_markdown(session_id: str, name: str | None = None) -> str:
|
||||||
|
events = _merged(session_id)
|
||||||
|
title = name or session_id
|
||||||
|
lines = [f"# Conversation — {title}",
|
||||||
|
f"_Exported {clock.stamp()} · session `{session_id}` · "
|
||||||
|
f"{len(events)} events_", ""]
|
||||||
|
for ev in events:
|
||||||
|
stamp = clock.short(ev["ts"])
|
||||||
|
if ev["type"] == "message":
|
||||||
|
who = _SPEAKER.get(ev["role"], ev["role"].capitalize())
|
||||||
|
lines.append(f"**{who}** · {stamp}")
|
||||||
|
lines.append((ev["content"] or "").rstrip())
|
||||||
|
lines.append("")
|
||||||
|
else:
|
||||||
|
result = (ev["result"] or "").strip().replace("\n", " ")
|
||||||
|
if len(result) > 200:
|
||||||
|
result = result[:197] + "…"
|
||||||
|
lines.append(f" ⚙ `{ev['tool']}({_fmt_args(ev['args'])})` → {result}")
|
||||||
|
lines.append("")
|
||||||
|
return "\n".join(lines).rstrip() + "\n"
|
||||||
|
|
||||||
|
|
||||||
|
def as_json(session_id: str, name: str | None = None) -> dict:
|
||||||
|
return {
|
||||||
|
"session_id": session_id,
|
||||||
|
"name": name,
|
||||||
|
"exported_at": clock.stamp(),
|
||||||
|
"events": _merged(session_id),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def build(session_id: str, fmt: str = "md", name: str | None = None):
|
||||||
|
"""Return (content_str, media_type, filename) for the requested format."""
|
||||||
|
safe = "".join(c if c.isalnum() or c in "-_" else "_" for c in session_id)[:60]
|
||||||
|
if fmt == "json":
|
||||||
|
body = json.dumps(as_json(session_id, name), indent=2, ensure_ascii=False)
|
||||||
|
return body, "application/json", f"lyra_{safe}.json"
|
||||||
|
body = as_markdown(session_id, name)
|
||||||
|
return body, "text/markdown; charset=utf-8", f"lyra_{safe}.md"
|
||||||
+166
-1
@@ -18,7 +18,7 @@ from fastapi import FastAPI, Request, Response
|
|||||||
from fastapi.responses import FileResponse, StreamingResponse
|
from fastapi.responses import FileResponse, StreamingResponse
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
|
|
||||||
from lyra import chat, logbus, memory, modes, poker, self_state, summary, thoughts
|
from lyra import chat, logbus, memory, modes, poker, self_state, summary, thoughts, transcript
|
||||||
from lyra.llm import Backend
|
from lyra.llm import Backend
|
||||||
|
|
||||||
|
|
||||||
@@ -50,6 +50,16 @@ def _last_user_message(messages: list[dict]) -> str:
|
|||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
app = FastAPI(title="Lyra Web")
|
app = FastAPI(title="Lyra Web")
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def _no_stale_shell(request: Request, call_next):
|
||||||
|
"""Always revalidate HTML/JS so a PWA can't serve a stale app shell after a
|
||||||
|
deploy (iOS applies heuristic caching when no cache header is set)."""
|
||||||
|
resp = await call_next(request)
|
||||||
|
ct = resp.headers.get("content-type", "")
|
||||||
|
if "text/html" in ct or "javascript" in ct:
|
||||||
|
resp.headers["Cache-Control"] = "no-cache, must-revalidate"
|
||||||
|
return resp
|
||||||
|
|
||||||
@app.get("/_health")
|
@app.get("/_health")
|
||||||
async def health() -> dict:
|
async def health() -> dict:
|
||||||
return {"ok": True}
|
return {"ok": True}
|
||||||
@@ -62,6 +72,15 @@ def create_app() -> FastAPI:
|
|||||||
async def get_session(session_id: str) -> list[dict]:
|
async def get_session(session_id: str) -> list[dict]:
|
||||||
return [{"role": ex.role, "content": ex.content} for ex in memory.history(session_id)]
|
return [{"role": ex.role, "content": ex.content} for ex in memory.history(session_id)]
|
||||||
|
|
||||||
|
@app.get("/sessions/{session_id}/export")
|
||||||
|
async def export_session(session_id: str, format: str = "md") -> Response:
|
||||||
|
"""Full transcript — chat + interleaved tool calls — as Markdown or JSON."""
|
||||||
|
name = next((s["name"] for s in memory.list_sessions() if s["id"] == session_id), None)
|
||||||
|
body, media_type, filename = await asyncio.to_thread(
|
||||||
|
transcript.build, session_id, format, name)
|
||||||
|
return Response(content=body, media_type=media_type,
|
||||||
|
headers={"Content-Disposition": f'attachment; filename="{filename}"'})
|
||||||
|
|
||||||
@app.post("/sessions/{session_id}")
|
@app.post("/sessions/{session_id}")
|
||||||
async def save_session(session_id: str, request: Request) -> dict:
|
async def save_session(session_id: str, request: Request) -> dict:
|
||||||
# Messages are already persisted by chat.respond; just ensure the row exists.
|
# Messages are already persisted by chat.respond; just ensure the row exists.
|
||||||
@@ -121,6 +140,109 @@ def create_app() -> FastAPI:
|
|||||||
logbus.log("info", "session edited", id=session_id, fields=list(body))
|
logbus.log("info", "session edited", id=session_id, fields=list(body))
|
||||||
return {"ok": s is not None, "session": s}
|
return {"ok": s is not None, "session": s}
|
||||||
|
|
||||||
|
@app.post("/session/stack")
|
||||||
|
async def session_log_stack(request: Request) -> dict:
|
||||||
|
"""Log Brian's current stack directly (no LLM). Server-stamps the time."""
|
||||||
|
body = await request.json()
|
||||||
|
try:
|
||||||
|
amount = float(body.get("amount"))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return {"ok": False, "error": "amount must be a number"}
|
||||||
|
note = (body.get("note") or "").strip() or None
|
||||||
|
try:
|
||||||
|
state = await asyncio.to_thread(poker.log_stack, amount, note)
|
||||||
|
except ValueError as exc:
|
||||||
|
return {"ok": False, "error": str(exc)}
|
||||||
|
logbus.log("info", "stack logged (direct)", amount=amount)
|
||||||
|
return {"ok": True, "stack": state}
|
||||||
|
|
||||||
|
@app.post("/session/buyin")
|
||||||
|
async def session_add_buyin(request: Request) -> dict:
|
||||||
|
"""Add a buy-in/rebuy directly (no LLM)."""
|
||||||
|
body = await request.json()
|
||||||
|
try:
|
||||||
|
amount = float(body.get("amount"))
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
return {"ok": False, "error": "amount must be a number"}
|
||||||
|
try:
|
||||||
|
total = await asyncio.to_thread(poker.add_buyin, amount)
|
||||||
|
except ValueError as exc:
|
||||||
|
return {"ok": False, "error": str(exc)}
|
||||||
|
logbus.log("info", "buyin added (direct)", amount=amount)
|
||||||
|
return {"ok": True, "buy_in_total": total}
|
||||||
|
|
||||||
|
@app.post("/session")
|
||||||
|
async def session_start(request: Request) -> dict:
|
||||||
|
"""Open a new live session directly (no LLM)."""
|
||||||
|
body = await request.json()
|
||||||
|
sid = await asyncio.to_thread(lambda: poker.start_session(
|
||||||
|
venue=body.get("venue"), stakes=body.get("stakes"),
|
||||||
|
game=body.get("game") or "NLH", fmt=body.get("format") or "cash",
|
||||||
|
buy_in=body.get("buy_in") or 0, mantra=body.get("mantra"),
|
||||||
|
))
|
||||||
|
logbus.log("info", "poker session started (direct)", id=sid)
|
||||||
|
return {"ok": True, "id": sid}
|
||||||
|
|
||||||
|
@app.post("/session/hand")
|
||||||
|
async def session_log_hand(request: Request) -> dict:
|
||||||
|
"""Log a hand directly with flat fields (no LLM parse)."""
|
||||||
|
body = await request.json()
|
||||||
|
try:
|
||||||
|
hid = await asyncio.to_thread(lambda: poker.log_hand(**body))
|
||||||
|
except ValueError as exc:
|
||||||
|
return {"ok": False, "error": str(exc)}
|
||||||
|
logbus.log("info", "hand logged (direct)", id=hid)
|
||||||
|
return {"ok": True, "id": hid}
|
||||||
|
|
||||||
|
@app.patch("/hand/{hand_id}")
|
||||||
|
async def hand_update(hand_id: int, request: Request) -> dict:
|
||||||
|
"""Edit a logged hand's flat fields."""
|
||||||
|
body = await request.json()
|
||||||
|
h = await asyncio.to_thread(lambda: poker.update_hand(hand_id, **body))
|
||||||
|
logbus.log("info", "hand edited", id=hand_id, fields=list(body))
|
||||||
|
return {"ok": h is not None, "hand": h}
|
||||||
|
|
||||||
|
@app.post("/hand/{hand_id}/disown")
|
||||||
|
async def hand_disown(hand_id: int) -> dict:
|
||||||
|
"""Reclassify a hand as observed (not Brian's) — fix a misattributed one."""
|
||||||
|
h = await asyncio.to_thread(poker.disown_hand, hand_id)
|
||||||
|
logbus.log("info", "hand disowned", id=hand_id)
|
||||||
|
return {"ok": h is not None, "hand": h}
|
||||||
|
|
||||||
|
@app.delete("/hand/{hand_id}")
|
||||||
|
async def hand_delete(hand_id: int) -> dict:
|
||||||
|
"""Delete a logged hand."""
|
||||||
|
ok = await asyncio.to_thread(poker.delete_entry, "hand", hand_id)
|
||||||
|
return {"ok": ok}
|
||||||
|
|
||||||
|
@app.post("/session/read")
|
||||||
|
async def session_add_read(request: Request) -> dict:
|
||||||
|
"""Log a read directly (no LLM); upserts the villain file when name is given."""
|
||||||
|
body = await request.json()
|
||||||
|
rid = await asyncio.to_thread(lambda: poker.add_read(
|
||||||
|
note=body.get("note") or "", seat=body.get("seat"), name=body.get("name"),
|
||||||
|
tendencies=body.get("tendencies"), adjustment=body.get("adjustment"),
|
||||||
|
description=body.get("description"), category=body.get("category"),
|
||||||
|
venue=body.get("venue"),
|
||||||
|
))
|
||||||
|
return {"ok": True, "id": rid}
|
||||||
|
|
||||||
|
@app.patch("/player/{player_id}")
|
||||||
|
async def player_update(player_id: int, request: Request) -> dict:
|
||||||
|
"""Edit a player's dossier (rename, fix tendencies). Setting `name` on a
|
||||||
|
nameless (descriptor) villain promotes it to a real handle (named=1)."""
|
||||||
|
body = await request.json()
|
||||||
|
|
||||||
|
def _apply():
|
||||||
|
if body.get("name"):
|
||||||
|
poker.name_villain(player_id, body["name"])
|
||||||
|
rest = {k: v for k, v in body.items() if k != "name"}
|
||||||
|
return poker.update_player(player_id, **rest) # flat row (name included)
|
||||||
|
|
||||||
|
p = await asyncio.to_thread(_apply)
|
||||||
|
logbus.log("info", "player edited", id=player_id, fields=list(body))
|
||||||
|
return {"ok": p is not None, "player": p}
|
||||||
|
|
||||||
@app.delete("/session/entry/{kind}/{entry_id}")
|
@app.delete("/session/entry/{kind}/{entry_id}")
|
||||||
async def delete_entry(kind: str, entry_id: int) -> dict:
|
async def delete_entry(kind: str, entry_id: int) -> dict:
|
||||||
"""Delete one HUD entry (hand | stack | read | ritual) by id."""
|
"""Delete one HUD entry (hand | stack | read | ritual) by id."""
|
||||||
@@ -243,6 +365,21 @@ def create_app() -> FastAPI:
|
|||||||
async def journal_data(limit: int = 300) -> dict:
|
async def journal_data(limit: int = 300) -> dict:
|
||||||
return {"entries": memory.list_journal(limit=limit)}
|
return {"entries": memory.list_journal(limit=limit)}
|
||||||
|
|
||||||
|
@app.get("/settings/introspection")
|
||||||
|
async def get_introspection() -> dict:
|
||||||
|
"""Current introspection (her inner voice) routing + the available options."""
|
||||||
|
tgt = self_state.introspection_target()
|
||||||
|
return {"mode": tgt["mode"],
|
||||||
|
"options": [{"key": k, "label": v["label"]}
|
||||||
|
for k, v in self_state.INTROSPECTION_MODES.items()]}
|
||||||
|
|
||||||
|
@app.post("/settings/introspection")
|
||||||
|
async def set_introspection(request: Request) -> dict:
|
||||||
|
"""Switch her inner voice: dolphin (3090) | mi50 (gaming-safe) | off."""
|
||||||
|
b = await request.json()
|
||||||
|
ok = await asyncio.to_thread(self_state.set_introspection_mode, b.get("mode", ""))
|
||||||
|
return {"ok": ok, "mode": self_state.introspection_target()["mode"]}
|
||||||
|
|
||||||
@app.get("/thoughts")
|
@app.get("/thoughts")
|
||||||
async def thoughts_page() -> FileResponse:
|
async def thoughts_page() -> FileResponse:
|
||||||
"""Lyra's thought loop — threads she's been turning over, and a place to reply."""
|
"""Lyra's thought loop — threads she's been turning over, and a place to reply."""
|
||||||
@@ -324,6 +461,34 @@ def create_app() -> FastAPI:
|
|||||||
async def hands_data(limit: int = 60) -> dict:
|
async def hands_data(limit: int = 60) -> dict:
|
||||||
return {"hands": poker.list_recent_hands(limit=limit)}
|
return {"hands": poker.list_recent_hands(limit=limit)}
|
||||||
|
|
||||||
|
@app.get("/players")
|
||||||
|
async def players_page() -> FileResponse:
|
||||||
|
"""Villain file browser + the identity-resolution review queue."""
|
||||||
|
return FileResponse(str(_STATIC / "players.html"))
|
||||||
|
|
||||||
|
@app.get("/players/data")
|
||||||
|
async def players_data() -> dict:
|
||||||
|
return {"players": poker.players_overview(),
|
||||||
|
"queue": poker.list_identity_queue()}
|
||||||
|
|
||||||
|
@app.get("/player/{player_id}/data")
|
||||||
|
async def player_data(player_id: int) -> dict:
|
||||||
|
return poker.villain_recall(player_id) or {}
|
||||||
|
|
||||||
|
@app.post("/identity/{task_id}/resolve")
|
||||||
|
async def identity_resolve(task_id: int, request: Request) -> dict:
|
||||||
|
body = await request.json()
|
||||||
|
action = body.get("action") or "dismiss"
|
||||||
|
kw = {k: v for k, v in body.items() if k != "action"}
|
||||||
|
ok = await asyncio.to_thread(poker.resolve_identity_task, task_id, action, **kw)
|
||||||
|
logbus.log("info", "identity task resolved", id=task_id, action=action)
|
||||||
|
return {"ok": ok}
|
||||||
|
|
||||||
|
@app.post("/players/scan")
|
||||||
|
async def players_scan() -> dict:
|
||||||
|
filed = await asyncio.to_thread(poker.scan_merge_candidates)
|
||||||
|
return {"ok": True, "filed": filed}
|
||||||
|
|
||||||
@app.get("/recap/{session_id}")
|
@app.get("/recap/{session_id}")
|
||||||
async def recap_page() -> FileResponse:
|
async def recap_page() -> FileResponse:
|
||||||
return FileResponse(str(_STATIC / "recap.html"))
|
return FileResponse(str(_STATIC / "recap.html"))
|
||||||
|
|||||||
@@ -282,8 +282,54 @@
|
|||||||
const h = await r.json();
|
const h = await r.json();
|
||||||
if(!h || !h.id){ document.getElementById('root').innerHTML='<p class="err">Hand not found.</p>'; return; }
|
if(!h || !h.id){ document.getElementById('root').innerHTML='<p class="err">Hand not found.</p>'; return; }
|
||||||
render(h);
|
render(h);
|
||||||
|
renderEditor(h);
|
||||||
}catch(e){ document.getElementById('root').innerHTML='<p class="err">Couldn\'t load the hand.</p>'; }
|
}catch(e){ document.getElementById('root').innerHTML='<p class="err">Couldn\'t load the hand.</p>'; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function renderEditor(h){
|
||||||
|
const wrap = document.createElement('div');
|
||||||
|
wrap.style.cssText = 'max-width:520px;margin:18px auto 0;border-top:1px solid #241a10;padding-top:12px;';
|
||||||
|
const tags = ['','well_played','leak','cooler','confidence','notable'];
|
||||||
|
wrap.innerHTML = `
|
||||||
|
<details style="font-size:.9rem;">
|
||||||
|
<summary style="cursor:pointer;color:var(--accent,#ff7a00);">✎ Edit this hand</summary>
|
||||||
|
<div style="display:flex;flex-direction:column;gap:8px;margin-top:10px;">
|
||||||
|
<label>Position <input id="e_pos" value="${esc(h.position||'')}" placeholder="e.g. CO (blank if not yours)"></label>
|
||||||
|
<label>Your cards <input id="e_hole" value="${esc(h.hole_cards||'')}" placeholder="e.g. As Ks (blank if not yours)"></label>
|
||||||
|
<label>Board <input id="e_board" value="${esc(h.board||'')}" placeholder="e.g. Tc 8s Js 6d"></label>
|
||||||
|
<label>Your net <input id="e_res" value="${h.result!=null?esc(h.result):''}" placeholder="+ / − chips (blank if not yours)"></label>
|
||||||
|
<label>Tag <select id="e_tag">${tags.map(t=>`<option value="${t}" ${h.tag===t?'selected':''}>${t||'—'}</option>`).join('')}</select></label>
|
||||||
|
<label>Lesson <input id="e_lesson" value="${esc(h.lesson||'')}"></label>
|
||||||
|
<div style="display:flex;flex-wrap:wrap;gap:8px;margin-top:4px;">
|
||||||
|
<button onclick="saveHand(${h.id})" style="border-color:var(--accent,#ff7a00);color:var(--accent,#ff7a00);">Save</button>
|
||||||
|
<button onclick="disown(${h.id})" title="It was someone else's hand — clear it from you">Not my hand</button>
|
||||||
|
<button onclick="delHand(${h.id})" style="margin-left:auto;color:#ff6b6b;">Delete</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</details>`;
|
||||||
|
wrap.querySelectorAll('input,select').forEach(el=>{el.style.cssText='font:inherit;font-size:.86rem;padding:5px 8px;border-radius:6px;border:1px solid #241a10;background:#0b0b0b;color:#e8e8e8;margin-left:8px;';});
|
||||||
|
wrap.querySelectorAll('label').forEach(el=>{el.style.cssText='display:flex;justify-content:space-between;align-items:center;color:#8a8a8a;';});
|
||||||
|
wrap.querySelectorAll('button').forEach(el=>{el.style.cssText+=';font:inherit;font-size:.84rem;padding:6px 12px;border-radius:7px;border:1px solid #241a10;background:#141414;color:#e8e8e8;cursor:pointer;';});
|
||||||
|
document.getElementById('root').appendChild(wrap);
|
||||||
|
}
|
||||||
|
const val = id => document.getElementById(id).value.trim();
|
||||||
|
async function saveHand(id){
|
||||||
|
const body = {position:val('e_pos'), hole_cards:val('e_hole'), board:val('e_board'),
|
||||||
|
tag:val('e_tag'), lesson:val('e_lesson')};
|
||||||
|
const res = val('e_res'); if(res!=='') body.result = Number(res);
|
||||||
|
await fetch(`/hand/${id}`,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)});
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
async function disown(id){
|
||||||
|
if(!confirm("Mark this as someone else's hand? It'll be cleared from your stats.")) return;
|
||||||
|
await fetch(`/hand/${id}/disown`,{method:'POST'});
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
async function delHand(id){
|
||||||
|
if(!confirm('Delete this hand for good?')) return;
|
||||||
|
await fetch(`/hand/${id}`,{method:'DELETE'});
|
||||||
|
location.href='/hands';
|
||||||
|
}
|
||||||
load();
|
load();
|
||||||
</script>
|
</script>
|
||||||
<script src="/nav.js"></script>
|
<script src="/nav.js"></script>
|
||||||
|
|||||||
+155
-10
@@ -3,14 +3,14 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<title>Lyra Core Chat</title>
|
<title>Lyra Core Chat</title>
|
||||||
<link rel="stylesheet" href="style.css" />
|
<link rel="stylesheet" href="style.css?v=8" />
|
||||||
<!-- PWA -->
|
<!-- PWA -->
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover" />
|
||||||
<meta name="mobile-web-app-capable" content="yes" />
|
<meta name="mobile-web-app-capable" content="yes" />
|
||||||
<meta name="apple-mobile-web-app-capable" content="yes" />
|
<meta name="apple-mobile-web-app-capable" content="yes" />
|
||||||
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
|
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
|
||||||
<meta name="apple-mobile-web-app-title" content="Lyra" />
|
<meta name="apple-mobile-web-app-title" content="Lyra" />
|
||||||
<meta name="theme-color" content="#070707" />
|
<meta name="theme-color" content="#141414" />
|
||||||
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
|
<link rel="apple-touch-icon" href="apple-touch-icon.png" />
|
||||||
<link rel="icon" type="image/png" href="icon-192.png" />
|
<link rel="icon" type="image/png" href="icon-192.png" />
|
||||||
<link rel="manifest" href="manifest.json" />
|
<link rel="manifest" href="manifest.json" />
|
||||||
@@ -26,7 +26,11 @@
|
|||||||
<h4>Mode</h4>
|
<h4>Mode</h4>
|
||||||
<select id="mobileMode">
|
<select id="mobileMode">
|
||||||
<option value="conversation">💬 Talk</option>
|
<option value="conversation">💬 Talk</option>
|
||||||
<option value="poker_cash">♠ Cash</option>
|
<option value="poker_cash">♠ Poker</option>
|
||||||
|
<option value="build">🛠 Build</option>
|
||||||
|
<option value="explore">🔭 Explore</option>
|
||||||
|
<option value="study">📐 Study</option>
|
||||||
|
<option value="decide">⚖️ Decide</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -41,9 +45,10 @@
|
|||||||
<h4>Actions</h4>
|
<h4>Actions</h4>
|
||||||
<button id="mobileSessionBtn">🎬 Session HUD</button>
|
<button id="mobileSessionBtn">🎬 Session HUD</button>
|
||||||
<button id="mobileHistoryBtn">📚 Past Sessions</button>
|
<button id="mobileHistoryBtn">📚 Past Sessions</button>
|
||||||
|
<button id="mobileThoughtsBtn">💭 Thoughts</button>
|
||||||
|
<button id="mobileJournalBtn">📔 Journal</button>
|
||||||
<button id="mobileThinkingStreamBtn">📜 Live Log (inline)</button>
|
<button id="mobileThinkingStreamBtn">📜 Live Log (inline)</button>
|
||||||
<button id="mobileFullLogBtn">⛶ Full Log</button>
|
<button id="mobileFullLogBtn">⛶ Full Log</button>
|
||||||
<button id="mobileJournalBtn">📔 Journal</button>
|
|
||||||
<button id="mobileSettingsBtn">⚙ Settings</button>
|
<button id="mobileSettingsBtn">⚙ Settings</button>
|
||||||
<button id="mobileToggleThemeBtn">🌙 Toggle Theme</button>
|
<button id="mobileToggleThemeBtn">🌙 Toggle Theme</button>
|
||||||
<button id="mobileForceReloadBtn">🔄 Force Reload</button>
|
<button id="mobileForceReloadBtn">🔄 Force Reload</button>
|
||||||
@@ -61,11 +66,15 @@
|
|||||||
</button>
|
</button>
|
||||||
<span class="brand">Lyra</span>
|
<span class="brand">Lyra</span>
|
||||||
<span class="brand-dot" id="brandDot" title="Relay status"></span>
|
<span class="brand-dot" id="brandDot" title="Relay status"></span>
|
||||||
<button class="mode-badge" id="modeBadge" type="button" title="Tap to toggle Talk / Cash mode">💬 Talk</button>
|
<button class="mode-badge" id="modeBadge" type="button" title="Current mode (tap to cycle)">💬 Talk</button>
|
||||||
<label for="mode">Mode:</label>
|
<label for="mode">Mode:</label>
|
||||||
<select id="mode">
|
<select id="mode">
|
||||||
<option value="conversation">💬 Talk</option>
|
<option value="conversation">💬 Talk</option>
|
||||||
<option value="poker_cash">♠ Cash</option>
|
<option value="poker_cash">♠ Poker</option>
|
||||||
|
<option value="build">🛠 Build</option>
|
||||||
|
<option value="explore">🔭 Explore</option>
|
||||||
|
<option value="study">📐 Study</option>
|
||||||
|
<option value="decide">⚖️ Decide</option>
|
||||||
</select>
|
</select>
|
||||||
<button id="settingsBtn" style="margin-left: auto;">⚙ Settings</button>
|
<button id="settingsBtn" style="margin-left: auto;">⚙ Settings</button>
|
||||||
<div id="theme-toggle">
|
<div id="theme-toggle">
|
||||||
@@ -79,6 +88,7 @@
|
|||||||
<select id="sessions"></select>
|
<select id="sessions"></select>
|
||||||
<button id="newSessionBtn">➕ New</button>
|
<button id="newSessionBtn">➕ New</button>
|
||||||
<button id="renameSessionBtn">✏️ Rename</button>
|
<button id="renameSessionBtn">✏️ Rename</button>
|
||||||
|
<button id="exportSessionBtn" title="Download full transcript (chat + tool calls)">⬇ Export</button>
|
||||||
<button id="thinkingStreamBtn" title="Show live activity log">📜 Live Log</button>
|
<button id="thinkingStreamBtn" title="Show live activity log">📜 Live Log</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -115,6 +125,12 @@
|
|||||||
<button id="sendBtn" aria-label="Send" title="Send (or ⌘/Ctrl+Enter)">↑</button>
|
<button id="sendBtn" aria-label="Send" title="Send (or ⌘/Ctrl+Enter)">↑</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<!-- Stack quick-capture (no LLM): type a number -> logs current stack -->
|
||||||
|
<div id="stackQuick">
|
||||||
|
<input id="stackQuickInput" type="number" inputmode="decimal" placeholder="Stack $" aria-label="Log current stack">
|
||||||
|
<button id="stackQuickBtn" type="button" title="Log stack (no chat)">Log</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
<!-- Bottom tab bar (mobile only; hides while the keyboard is open) -->
|
<!-- Bottom tab bar (mobile only; hides while the keyboard is open) -->
|
||||||
<nav id="tabbar" aria-label="Primary navigation">
|
<nav id="tabbar" aria-label="Primary navigation">
|
||||||
<a class="tab active" href="/" aria-current="page"><span class="ti">💬</span><span class="tl">Chat</span></a>
|
<a class="tab active" href="/" aria-current="page"><span class="ti">💬</span><span class="tl">Chat</span></a>
|
||||||
@@ -169,6 +185,17 @@
|
|||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="settings-section" style="margin-top: 24px;">
|
||||||
|
<h4>Inner Voice (introspection)</h4>
|
||||||
|
<p class="settings-desc">Which model runs her reflections & thoughts (her dream loop).
|
||||||
|
Dolphin is richer but shares the 3090 — switch to MI50 or Off before gaming.</p>
|
||||||
|
<select id="introspectionMode">
|
||||||
|
<option value="dolphin">Dolphin · 3090 (richer voice)</option>
|
||||||
|
<option value="mi50">Qwen-32B · MI50 (gaming-safe)</option>
|
||||||
|
<option value="off">Off (pause her thinking)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="settings-section" style="margin-top: 24px;">
|
<div class="settings-section" style="margin-top: 24px;">
|
||||||
<h4>Session Management</h4>
|
<h4>Session Management</h4>
|
||||||
<p class="settings-desc">Manage your saved chat sessions:</p>
|
<p class="settings-desc">Manage your saved chat sessions:</p>
|
||||||
@@ -189,6 +216,72 @@
|
|||||||
const API_URL = `${RELAY_BASE}/v1/chat/completions`;
|
const API_URL = `${RELAY_BASE}/v1/chat/completions`;
|
||||||
const STREAM_URL = `${RELAY_BASE}/v1/chat/stream`;
|
const STREAM_URL = `${RELAY_BASE}/v1/chat/stream`;
|
||||||
|
|
||||||
|
// Stack quick-capture (no LLM): type a number -> POST /session/stack.
|
||||||
|
function stackQuickLog() {
|
||||||
|
const el = document.getElementById("stackQuickInput");
|
||||||
|
if (!el) return;
|
||||||
|
const raw = (el.value || "").replace(/[^0-9.]/g, "");
|
||||||
|
if (!raw) return;
|
||||||
|
const amount = Number(raw);
|
||||||
|
const content = document.getElementById("thinkingContent");
|
||||||
|
const empty = document.getElementById("thinkingEmpty");
|
||||||
|
fetch("/session/stack", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ amount })
|
||||||
|
}).then(r => r.json()).then(data => {
|
||||||
|
if (empty && empty.parentNode) empty.parentNode.removeChild(empty);
|
||||||
|
const line = document.createElement("div");
|
||||||
|
const t = new Date().toLocaleTimeString();
|
||||||
|
if (!data.ok) {
|
||||||
|
line.className = "log-line log-error";
|
||||||
|
line.textContent = "⚠ " + (data.error || "stack not logged");
|
||||||
|
} else {
|
||||||
|
line.className = "log-line log-info";
|
||||||
|
const net = (data.stack && data.stack.net != null)
|
||||||
|
? " (net " + (data.stack.net >= 0 ? "+" : "") + data.stack.net + ")" : "";
|
||||||
|
line.textContent = t + " 💰 $" + amount + " logged" + net;
|
||||||
|
el.value = "";
|
||||||
|
}
|
||||||
|
if (content) { content.appendChild(line); content.scrollTop = content.scrollHeight; }
|
||||||
|
}).catch(e => {
|
||||||
|
if (content) {
|
||||||
|
const line = document.createElement("div");
|
||||||
|
line.className = "log-line log-error";
|
||||||
|
line.textContent = "⚠ stack log failed: " + e.message;
|
||||||
|
content.appendChild(line);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Only show the stack quick-logger when a poker session is actually live —
|
||||||
|
// otherwise logging just errors ("no live session").
|
||||||
|
function updateStackQuickVisibility() {
|
||||||
|
const box = document.getElementById("stackQuick");
|
||||||
|
if (!box) return;
|
||||||
|
fetch("/session/data", { cache: "no-store" })
|
||||||
|
.then(function (r) { return r.json(); })
|
||||||
|
.then(function (data) {
|
||||||
|
const live = !!(data && data.session && data.session.is_live);
|
||||||
|
box.style.display = live ? "flex" : "none";
|
||||||
|
})
|
||||||
|
.catch(function () { box.style.display = "none"; });
|
||||||
|
}
|
||||||
|
(function wireStackQuick() {
|
||||||
|
const box = document.getElementById("stackQuick");
|
||||||
|
const btn = document.getElementById("stackQuickBtn");
|
||||||
|
const inp = document.getElementById("stackQuickInput");
|
||||||
|
if (box) box.style.display = "none"; // hidden until a live session is confirmed
|
||||||
|
if (btn) btn.addEventListener("click", stackQuickLog);
|
||||||
|
if (inp) inp.addEventListener("keydown", function (e) {
|
||||||
|
if (e.key === "Enter") { e.preventDefault(); stackQuickLog(); }
|
||||||
|
});
|
||||||
|
updateStackQuickVisibility();
|
||||||
|
setInterval(updateStackQuickVisibility, 10000);
|
||||||
|
document.addEventListener("visibilitychange", function () {
|
||||||
|
if (!document.hidden) updateStackQuickVisibility();
|
||||||
|
});
|
||||||
|
})();
|
||||||
|
|
||||||
function generateSessionId() {
|
function generateSessionId() {
|
||||||
return "sess-" + Math.random().toString(36).substring(2, 10);
|
return "sess-" + Math.random().toString(36).substring(2, 10);
|
||||||
}
|
}
|
||||||
@@ -593,8 +686,11 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ----- Conversation mode (Talk / Cash) -----
|
// ----- Conversation modes (Talk / Poker / Build / Explore / Study) -----
|
||||||
const MODE_LABELS = { conversation: "💬 Talk", poker_cash: "♠ Cash" };
|
const MODE_LABELS = { conversation: "💬 Talk", poker_cash: "♠ Poker",
|
||||||
|
build: "🛠 Build", explore: "🔭 Explore", study: "📐 Study",
|
||||||
|
decide: "⚖️ Decide" };
|
||||||
|
const MODE_ORDER = ["conversation", "poker_cash", "build", "explore", "study", "decide"];
|
||||||
|
|
||||||
// Reflect a mode value across the controls + header accent (no network call).
|
// Reflect a mode value across the controls + header accent (no network call).
|
||||||
function applyMode(value) {
|
function applyMode(value) {
|
||||||
@@ -678,6 +774,16 @@
|
|||||||
window.addEventListener("resize", nudgeAppHeight);
|
window.addEventListener("resize", nudgeAppHeight);
|
||||||
window.addEventListener("orientationchange", nudgeAppHeight);
|
window.addEventListener("orientationchange", nudgeAppHeight);
|
||||||
|
|
||||||
|
// A rotation reflows the chat and iOS drops the scroll to mid-history. If we
|
||||||
|
// were pinned to the latest message, snap back there once the layout settles
|
||||||
|
// (re-fire across the reflow since iOS reports stale dimensions mid-rotate).
|
||||||
|
window.addEventListener("orientationchange", () => {
|
||||||
|
const m = document.getElementById("messages");
|
||||||
|
const wasAtBottom = m.scrollHeight - m.scrollTop - m.clientHeight < 90;
|
||||||
|
if (!wasAtBottom) return; // respect the user's scroll-up position
|
||||||
|
[100, 300, 600].forEach((t) => setTimeout(() => { m.scrollTop = m.scrollHeight; }, t));
|
||||||
|
});
|
||||||
|
|
||||||
// Keep the latest message in view when the keyboard opens/closes.
|
// Keep the latest message in view when the keyboard opens/closes.
|
||||||
const userInputEl = document.getElementById("userInput");
|
const userInputEl = document.getElementById("userInput");
|
||||||
userInputEl.addEventListener("focus", () => {
|
userInputEl.addEventListener("focus", () => {
|
||||||
@@ -718,8 +824,10 @@
|
|||||||
|
|
||||||
desktopMode.addEventListener("change", (e) => chooseMode(e.target.value));
|
desktopMode.addEventListener("change", (e) => chooseMode(e.target.value));
|
||||||
mobileMode.addEventListener("change", (e) => { closeMobileMenu(); chooseMode(e.target.value); });
|
mobileMode.addEventListener("change", (e) => { closeMobileMenu(); chooseMode(e.target.value); });
|
||||||
modeBadge.addEventListener("click", () =>
|
modeBadge.addEventListener("click", () => {
|
||||||
chooseMode(desktopMode.value === "poker_cash" ? "conversation" : "poker_cash"));
|
const i = MODE_ORDER.indexOf(desktopMode.value);
|
||||||
|
chooseMode(MODE_ORDER[(i + 1) % MODE_ORDER.length]); // tap cycles through modes
|
||||||
|
});
|
||||||
|
|
||||||
// Reflect the last-used mode immediately; the per-session value loads once
|
// Reflect the last-used mode immediately; the per-session value loads once
|
||||||
// the current session is known (below).
|
// the current session is known (below).
|
||||||
@@ -880,6 +988,19 @@
|
|||||||
addMessage("system", `Session renamed to: ${newName}`);
|
addMessage("system", `Session renamed to: ${newName}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.getElementById("exportSessionBtn").addEventListener("click", () => {
|
||||||
|
if (!currentSession) { addMessage("system", "No session to export."); return; }
|
||||||
|
const fmt = window.confirm("Export as Markdown? (Cancel = JSON)") ? "md" : "json";
|
||||||
|
// Hitting the download endpoint navigates a hidden anchor so the browser
|
||||||
|
// saves the file (chat + interleaved tool calls) instead of rendering it.
|
||||||
|
const a = document.createElement("a");
|
||||||
|
a.href = `${RELAY_BASE}/sessions/${encodeURIComponent(currentSession)}/export?format=${fmt}`;
|
||||||
|
a.download = "";
|
||||||
|
document.body.appendChild(a);
|
||||||
|
a.click();
|
||||||
|
a.remove();
|
||||||
|
});
|
||||||
|
|
||||||
// Settings Modal
|
// Settings Modal
|
||||||
const settingsModal = document.getElementById("settingsModal");
|
const settingsModal = document.getElementById("settingsModal");
|
||||||
const settingsBtn = document.getElementById("settingsBtn");
|
const settingsBtn = document.getElementById("settingsBtn");
|
||||||
@@ -979,10 +1100,31 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Inner-voice (introspection) switch — applies instantly, read live by the dream loop.
|
||||||
|
const introspectionSel = document.getElementById("introspectionMode");
|
||||||
|
async function loadIntrospection() {
|
||||||
|
try {
|
||||||
|
const r = await fetch("/settings/introspection", { cache: "no-store" });
|
||||||
|
const d = await r.json();
|
||||||
|
if (d.mode) introspectionSel.value = d.mode;
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
if (introspectionSel) {
|
||||||
|
introspectionSel.addEventListener("change", async () => {
|
||||||
|
try {
|
||||||
|
await fetch("/settings/introspection", {
|
||||||
|
method: "POST", headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ mode: introspectionSel.value })
|
||||||
|
});
|
||||||
|
} catch (e) {}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Show modal and load session list
|
// Show modal and load session list
|
||||||
settingsBtn.addEventListener("click", () => {
|
settingsBtn.addEventListener("click", () => {
|
||||||
settingsModal.classList.add("show");
|
settingsModal.classList.add("show");
|
||||||
loadSessionList(); // Refresh session list when opening settings
|
loadSessionList(); // Refresh session list when opening settings
|
||||||
|
loadIntrospection(); // reflect the current inner-voice setting
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sidebar "Settings" from another page navigates here with ?settings=1.
|
// Sidebar "Settings" from another page navigates here with ?settings=1.
|
||||||
@@ -1171,6 +1313,9 @@
|
|||||||
document.getElementById("mobileHistoryBtn").addEventListener("click", () => {
|
document.getElementById("mobileHistoryBtn").addEventListener("click", () => {
|
||||||
closeMobileMenu(); window.location.href = "/history";
|
closeMobileMenu(); window.location.href = "/history";
|
||||||
});
|
});
|
||||||
|
document.getElementById("mobileThoughtsBtn").addEventListener("click", () => {
|
||||||
|
closeMobileMenu(); window.location.href = "/thoughts";
|
||||||
|
});
|
||||||
|
|
||||||
// Connect to the global live log on page load.
|
// Connect to the global live log on page load.
|
||||||
connectThinkingStream();
|
connectThinkingStream();
|
||||||
|
|||||||
+60
-27
@@ -1,12 +1,14 @@
|
|||||||
/* Shared app navigation — one source of truth across all pages (no build step).
|
/* Shared app navigation — one source of truth across all pages (no build step).
|
||||||
Injects a left sidebar on desktop (>=769px) with active-page highlighting; stays
|
Desktop (>=769px): a fixed left sidebar. Mobile (<=768px): a slide-in drawer
|
||||||
out of the way on mobile, where each page keeps its bottom bar / back-links. */
|
behind a ☰ button — but ONLY on pages that don't already ship their own mobile
|
||||||
|
menu (the chat page has its own hamburger + tab bar, so we leave it alone). */
|
||||||
(function () {
|
(function () {
|
||||||
const ITEMS = [
|
const ITEMS = [
|
||||||
{ href: "/", icon: "💬", label: "Chat" },
|
{ href: "/", icon: "💬", label: "Chat" },
|
||||||
{ href: "/session", icon: "♠", label: "Session" },
|
{ href: "/session", icon: "♠", label: "Session" },
|
||||||
{ href: "/history", icon: "📚", label: "History" },
|
{ href: "/history", icon: "📚", label: "History" },
|
||||||
{ href: "/hands", icon: "🃏", label: "Hands" },
|
{ href: "/hands", icon: "🃏", label: "Hands" },
|
||||||
|
{ href: "/players", icon: "👤", label: "Players" },
|
||||||
{ href: "/self", icon: "🧠", label: "Mind" },
|
{ href: "/self", icon: "🧠", label: "Mind" },
|
||||||
{ href: "/thoughts", icon: "💭", label: "Thoughts" },
|
{ href: "/thoughts", icon: "💭", label: "Thoughts" },
|
||||||
{ href: "/journal", icon: "📔", label: "Journal" },
|
{ href: "/journal", icon: "📔", label: "Journal" },
|
||||||
@@ -21,34 +23,45 @@
|
|||||||
return path === href || path.indexOf(href + "/") === 0;
|
return path === href || path.indexOf(href + "/") === 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Visual styling (all sizes); positioning differs per breakpoint below.
|
||||||
const css = `
|
const css = `
|
||||||
#app-nav { display: none; }
|
#app-nav { display: none; flex-direction: column; gap: 2px; box-sizing: border-box;
|
||||||
|
padding: 14px 10px; background: #0b0b0b; border-right: 1px solid #2a1d12;
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
|
||||||
|
#app-nav .brand { display: flex; align-items: center; gap: 8px; text-decoration: none;
|
||||||
|
color: #ff7a00; font-weight: 700; font-size: 1.15rem; letter-spacing: .5px; padding: 6px 11px 14px; }
|
||||||
|
#app-nav .brand .dot { width: 8px; height: 8px; border-radius: 50%;
|
||||||
|
background: #8fd694; box-shadow: 0 0 8px rgba(143,214,148,.6); }
|
||||||
|
#app-nav .navitem { display: flex; align-items: center; gap: 11px; width: 100%; text-align: left;
|
||||||
|
padding: 9px 11px; border-radius: 9px; border: none; background: none; color: #cfcfcf;
|
||||||
|
text-decoration: none; font-size: .95rem; cursor: pointer; font-family: inherit;
|
||||||
|
-webkit-tap-highlight-color: transparent; }
|
||||||
|
#app-nav .navitem .i { font-size: 1.05rem; width: 20px; text-align: center; filter: grayscale(.3); }
|
||||||
|
#app-nav .navitem:hover { background: rgba(255,122,0,.08); color: #fff; }
|
||||||
|
#app-nav .navitem.active { background: rgba(255,122,0,.14); color: #ff7a00; }
|
||||||
|
#app-nav .navitem.active .i { filter: none; }
|
||||||
|
#app-nav .spacer { flex: 1; }
|
||||||
|
#app-nav-burger { display: none; }
|
||||||
|
#app-nav-scrim { display: none; }
|
||||||
|
|
||||||
@media screen and (min-width: 769px) {
|
@media screen and (min-width: 769px) {
|
||||||
body { padding-left: 212px; }
|
body { padding-left: 212px; }
|
||||||
#app-nav {
|
#app-nav { display: flex; position: fixed; left: 0; top: 0; bottom: 0; width: 212px; z-index: 1000; }
|
||||||
position: fixed; left: 0; top: 0; bottom: 0; width: 212px; z-index: 1000;
|
}
|
||||||
display: flex; flex-direction: column; gap: 2px; box-sizing: border-box;
|
|
||||||
padding: 14px 10px; background: #0b0b0b; border-right: 1px solid #2a1d12;
|
@media screen and (max-width: 768px) {
|
||||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
body.lyra-nav-mobile #app-nav-burger { display: flex; align-items: center; justify-content: center;
|
||||||
}
|
position: fixed; top: calc(env(safe-area-inset-top) + 8px); right: 10px; z-index: 1301;
|
||||||
#app-nav .brand {
|
width: 40px; height: 40px; border-radius: 10px; border: 1px solid #2a1d12;
|
||||||
display: flex; align-items: center; gap: 8px; text-decoration: none;
|
background: rgba(14,14,14,.92); color: #ff7a00; font-size: 1.2rem; cursor: pointer;
|
||||||
color: #ff7a00; font-weight: 700; font-size: 1.15rem; letter-spacing: .5px;
|
-webkit-tap-highlight-color: transparent; backdrop-filter: blur(4px); }
|
||||||
padding: 6px 11px 14px;
|
body.lyra-nav-mobile #app-nav { display: flex; position: fixed; left: 0; top: 0; bottom: 0;
|
||||||
}
|
width: 240px; max-width: 80vw; transform: translateX(-100%); transition: transform .22s ease;
|
||||||
#app-nav .brand .dot { width: 8px; height: 8px; border-radius: 50%;
|
z-index: 1310; padding-top: calc(env(safe-area-inset-top) + 14px); overflow-y: auto; }
|
||||||
background: #8fd694; box-shadow: 0 0 8px rgba(143,214,148,.6); }
|
body.lyra-nav-mobile #app-nav.open { transform: translateX(0); }
|
||||||
#app-nav .navitem {
|
body.lyra-nav-mobile #app-nav-scrim.show { display: block; position: fixed; inset: 0;
|
||||||
display: flex; align-items: center; gap: 11px; width: 100%; text-align: left;
|
background: rgba(0,0,0,.5); z-index: 1305; }
|
||||||
padding: 9px 11px; border-radius: 9px; border: none; background: none;
|
#app-nav .navitem { padding: 12px 11px; font-size: 1rem; }
|
||||||
color: #cfcfcf; text-decoration: none; font-size: .95rem; cursor: pointer;
|
|
||||||
font-family: inherit; -webkit-tap-highlight-color: transparent;
|
|
||||||
}
|
|
||||||
#app-nav .navitem .i { font-size: 1.05rem; width: 20px; text-align: center; filter: grayscale(.3); }
|
|
||||||
#app-nav .navitem:hover { background: rgba(255,122,0,.08); color: #fff; }
|
|
||||||
#app-nav .navitem.active { background: rgba(255,122,0,.14); color: #ff7a00; }
|
|
||||||
#app-nav .navitem.active .i { filter: none; }
|
|
||||||
#app-nav .spacer { flex: 1; }
|
|
||||||
}`;
|
}`;
|
||||||
|
|
||||||
const style = document.createElement("style");
|
const style = document.createElement("style");
|
||||||
@@ -74,4 +87,24 @@
|
|||||||
if (btn) btn.click();
|
if (btn) btn.click();
|
||||||
else location.href = "/?settings=1";
|
else location.href = "/?settings=1";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Mobile drawer — only on pages without their own mobile menu (i.e., not the chat page).
|
||||||
|
if (!document.getElementById("hamburgerMenu")) {
|
||||||
|
document.body.classList.add("lyra-nav-mobile");
|
||||||
|
const burger = document.createElement("button");
|
||||||
|
burger.id = "app-nav-burger";
|
||||||
|
burger.type = "button";
|
||||||
|
burger.setAttribute("aria-label", "Menu");
|
||||||
|
burger.textContent = "☰";
|
||||||
|
const scrim = document.createElement("div");
|
||||||
|
scrim.id = "app-nav-scrim";
|
||||||
|
document.body.appendChild(burger);
|
||||||
|
document.body.appendChild(scrim);
|
||||||
|
const close = function () { nav.classList.remove("open"); scrim.classList.remove("show"); };
|
||||||
|
burger.addEventListener("click", function () {
|
||||||
|
nav.classList.toggle("open"); scrim.classList.toggle("show");
|
||||||
|
});
|
||||||
|
scrim.addEventListener("click", close);
|
||||||
|
nav.addEventListener("click", function (e) { if (e.target.closest("a")) close(); });
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -0,0 +1,166 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />
|
||||||
|
<meta name="theme-color" content="#070707" />
|
||||||
|
<title>Lyra — Players</title>
|
||||||
|
<style>
|
||||||
|
:root{--bg:#070707;--bg-elev:#0e0e0e;--bg-line:#141414;--border:#2a1d12;--text:#e8e8e8;--fade:#8a8a8a;--accent:#ff7a00;}
|
||||||
|
*{box-sizing:border-box;}
|
||||||
|
html,body{margin:0;min-height:100%;background:var(--bg);color:var(--text);
|
||||||
|
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;-webkit-text-size-adjust:100%;}
|
||||||
|
header{position:sticky;top:0;z-index:10;background:var(--bg-elev);border-bottom:1px solid var(--border);
|
||||||
|
padding:env(safe-area-inset-top) 14px 0;}
|
||||||
|
.topbar{display:flex;align-items:center;gap:10px;padding:13px 0;}
|
||||||
|
.topbar h1{font-size:1.05rem;margin:0;font-weight:600;}
|
||||||
|
.topbar a.back{color:var(--accent);text-decoration:none;font-size:.92rem;}
|
||||||
|
.count{margin-left:auto;color:var(--fade);font-size:.8rem;}
|
||||||
|
main{max-width:640px;margin:0 auto;padding:12px 12px 44px;}
|
||||||
|
h2.sec{font-size:.74rem;text-transform:uppercase;letter-spacing:.6px;color:var(--fade);margin:20px 2px 8px;}
|
||||||
|
.queue{background:#160d05;border:1px solid var(--accent);border-radius:10px;padding:11px 12px;margin-bottom:9px;}
|
||||||
|
.queue .k{font-size:.62rem;text-transform:uppercase;letter-spacing:.5px;color:var(--accent);}
|
||||||
|
.queue .q-body{font-size:.9rem;margin:5px 0 9px;}
|
||||||
|
.queue .who{font-weight:600;}
|
||||||
|
.btns{display:flex;flex-wrap:wrap;gap:7px;}
|
||||||
|
button{font:inherit;font-size:.82rem;padding:6px 11px;border-radius:7px;border:1px solid var(--border);
|
||||||
|
background:var(--bg-line);color:var(--text);cursor:pointer;}
|
||||||
|
button.pri{border-color:var(--accent);color:var(--accent);}
|
||||||
|
button:active{background:#241400;}
|
||||||
|
.card{background:var(--bg-elev);border:1px solid var(--border);border-radius:10px;padding:10px 12px;margin-bottom:8px;}
|
||||||
|
.card .row{display:flex;align-items:center;gap:9px;cursor:pointer;}
|
||||||
|
.nm{font-size:.96rem;font-weight:600;}
|
||||||
|
.nm.desc{font-weight:500;font-style:italic;color:#e8d3bf;}
|
||||||
|
.meta{font-size:.74rem;color:var(--fade);}
|
||||||
|
.pill{font-size:.6rem;text-transform:uppercase;letter-spacing:.4px;border:1px solid var(--border);
|
||||||
|
border-radius:20px;padding:1px 7px;color:var(--fade);}
|
||||||
|
.pill.desc{border-color:#5a3c1e;color:#d0a56e;}
|
||||||
|
.spacer{margin-left:auto;}
|
||||||
|
.detail{margin-top:9px;padding-top:9px;border-top:1px solid var(--bg-line);font-size:.86rem;display:none;}
|
||||||
|
.detail.open{display:block;}
|
||||||
|
.detail .lbl{color:var(--fade);font-size:.72rem;text-transform:uppercase;letter-spacing:.4px;margin:8px 0 3px;}
|
||||||
|
.detail ul{margin:3px 0;padding-left:18px;} .detail li{margin:2px 0;}
|
||||||
|
.detail a{color:var(--accent);text-decoration:none;}
|
||||||
|
.edit{display:flex;flex-wrap:wrap;gap:6px;margin-top:9px;}
|
||||||
|
.edit input,.edit select{font:inherit;font-size:.82rem;padding:5px 8px;border-radius:6px;
|
||||||
|
border:1px solid var(--border);background:var(--bg);color:var(--text);}
|
||||||
|
.empty{color:var(--fade);text-align:center;padding:34px 16px;}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<div class="topbar">
|
||||||
|
<h1>👤 Players</h1>
|
||||||
|
<a class="back" href="/">← Chat</a>
|
||||||
|
<span class="count" id="count"></span>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
<main id="root"><p class="empty">Loading…</p></main>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
function esc(s){const d=document.createElement('div');d.textContent=s==null?'':String(s);return d.innerHTML;}
|
||||||
|
let DATA={players:[],queue:[]};
|
||||||
|
|
||||||
|
async function load(){
|
||||||
|
try{ DATA=await (await fetch('/players/data',{cache:'no-store'})).json(); }
|
||||||
|
catch(e){ document.getElementById('root').innerHTML='<p class="empty">Couldn\'t load players.</p>'; return; }
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
|
function render(){
|
||||||
|
const {players,queue}=DATA;
|
||||||
|
const named=players.filter(p=>p.named), nameless=players.filter(p=>!p.named);
|
||||||
|
document.getElementById('count').textContent=`${players.length} player${players.length===1?'':'s'}`;
|
||||||
|
let html='';
|
||||||
|
|
||||||
|
if(queue.length){
|
||||||
|
html+=`<h2 class="sec">⚠ Needs your call — ${queue.length}</h2>`;
|
||||||
|
html+=queue.map(qCard).join('');
|
||||||
|
}
|
||||||
|
html+=`<h2 class="sec">Named — ${named.length} <button class="pri" style="float:right;padding:3px 9px" onclick="scan()">Scan for dupes</button></h2>`;
|
||||||
|
html+= named.length ? named.map(pCard).join('') : '<p class="empty">No named players yet.</p>';
|
||||||
|
html+=`<h2 class="sec">By description — ${nameless.length}</h2>`;
|
||||||
|
html+= nameless.length ? nameless.map(pCard).join('') : '<p class="empty">No nameless villains yet — they show up here as you describe players at the table.</p>';
|
||||||
|
document.getElementById('root').innerHTML=html;
|
||||||
|
}
|
||||||
|
|
||||||
|
function qCard(q){
|
||||||
|
const ps=q.players||[];
|
||||||
|
if(q.kind==='merge_candidate' && ps.length===2){
|
||||||
|
const a=ps[0], b=ps[1];
|
||||||
|
return `<div class="queue"><div class="k">Possible merge${q.confidence?` · ${Math.round(q.confidence*100)}%`:''}</div>
|
||||||
|
<div class="q-body">Same person? <span class="who">${label(a)}</span> vs <span class="who">${label(b)}</span></div>
|
||||||
|
<div class="btns">
|
||||||
|
<button class="pri" onclick="resolveTask(${q.id},'merge',{keep_id:${keepId(a,b)},dup_id:${dupId(a,b)}})">✓ Same — merge</button>
|
||||||
|
<button onclick="resolveTask(${q.id},'distinct',{a_id:${a.id},b_id:${b.id}})">✕ Different</button>
|
||||||
|
<button onclick="resolveTask(${q.id},'dismiss',{})">Dismiss</button>
|
||||||
|
</div></div>`;
|
||||||
|
}
|
||||||
|
const who=ps[0]?label(ps[0]):'?';
|
||||||
|
return `<div class="queue"><div class="k">Needs clarification</div>
|
||||||
|
<div class="q-body">You referred to <span class="who">“${esc(q.descriptor||'')}”</span>${ps[0]?` — is that ${who}?`:''}</div>
|
||||||
|
<div class="btns"><button onclick="resolveTask(${q.id},'dismiss',{})">Got it</button></div></div>`;
|
||||||
|
}
|
||||||
|
const label=p=>`${esc(p.name)}${p.named?'':' <span class="pill desc">desc</span>'}${p.venue?` · ${esc(p.venue)}`:''}${p.obs?` · ${p.obs}h`:''}`;
|
||||||
|
const keepId=(a,b)=>a.named?a.id:(b.named?b.id:a.id);
|
||||||
|
const dupId=(a,b)=>a.named?b.id:(b.named?a.id:b.id);
|
||||||
|
|
||||||
|
function pCard(p){
|
||||||
|
const pills=[p.named?'':'<span class="pill desc">desc</span>',p.category?`<span class="pill">${esc(p.category)}</span>`:''].join('');
|
||||||
|
const meta=[p.venue,p.obs?`${p.obs} hands`:'',p.reads?`${p.reads} reads`:''].filter(Boolean).join(' · ');
|
||||||
|
return `<div class="card" id="p${p.id}">
|
||||||
|
<div class="row" onclick="toggle(${p.id})">
|
||||||
|
<span class="nm ${p.named?'':'desc'}">${p.named?esc(p.name):'“'+esc(p.name)+'”'}</span>
|
||||||
|
${pills}<span class="spacer"></span><span class="meta">${esc(meta)}</span>
|
||||||
|
</div>
|
||||||
|
<div class="detail" id="d${p.id}"></div></div>`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function toggle(id){
|
||||||
|
const el=document.getElementById('d'+id);
|
||||||
|
if(el.classList.contains('open')){el.classList.remove('open');return;}
|
||||||
|
el.classList.add('open'); el.innerHTML='<span class="meta">Loading…</span>';
|
||||||
|
const r=await (await fetch(`/player/${id}/data`,{cache:'no-store'})).json();
|
||||||
|
el.innerHTML=detailHtml(id,r);
|
||||||
|
}
|
||||||
|
|
||||||
|
function detailHtml(id,r){
|
||||||
|
const p=r.player||{}; let h='';
|
||||||
|
const seen=[r.times_seen?`seen ${r.times_seen}×`:'', r.last_seen?`last ${String(r.last_seen).slice(0,10)}`:''].filter(Boolean).join(' · ');
|
||||||
|
if(seen) h+=`<div class="meta">${esc(seen)}</div>`;
|
||||||
|
if(r.stats) h+=`<div class="lbl">Stats</div><div>VPIP ${r.stats.vpip_pct} · PFR ${r.stats.pfr_pct} · WTSD ${r.stats.wtsd_pct} <span class="meta">(${r.stats.hands} hands)</span></div>`;
|
||||||
|
if(r.descriptors) h+=`<div class="lbl">Descriptors</div><div>${esc(r.descriptors)}</div>`;
|
||||||
|
if(p.tendencies) h+=`<div class="lbl">Tendencies</div><div>${esc(p.tendencies)}</div>`;
|
||||||
|
if(p.adjustment) h+=`<div class="lbl">Exploit</div><div>${esc(p.adjustment)}</div>`;
|
||||||
|
if((r.reads||[]).length){h+='<div class="lbl">Reads</div><ul>'+r.reads.slice(0,8).map(x=>`<li>${esc(x)}</li>`).join('')+'</ul>';}
|
||||||
|
if((r.notable_hands||[]).length){h+='<div class="lbl">Notable hands</div><ul>'+r.notable_hands.map(x=>`<li><a href="/hand/${x.hand_id}">hand #${x.hand_id}</a>${x.cards?' — '+esc(x.cards):''}${x.summary?' <span class="meta">'+esc(x.summary)+'</span>':''}</li>`).join('')+'</ul>';}
|
||||||
|
h+=`<div class="edit">
|
||||||
|
${p.named?'':`<input id="nm${id}" placeholder="give a name…" size="12"><button onclick="rename(${id})">Name</button>`}
|
||||||
|
<select id="cat${id}" onchange="setCat(${id})">
|
||||||
|
${['','feeder','risky','reg','unknown'].map(c=>`<option value="${c}" ${p.category===c?'selected':''}>${c||'category…'}</option>`).join('')}
|
||||||
|
</select></div>`;
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function rename(id){
|
||||||
|
const v=document.getElementById('nm'+id).value.trim(); if(!v)return;
|
||||||
|
await fetch(`/player/${id}`,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify({name:v})});
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
async function setCat(id){
|
||||||
|
const v=document.getElementById('cat'+id).value;
|
||||||
|
await fetch(`/player/${id}`,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify({category:v})});
|
||||||
|
}
|
||||||
|
async function resolveTask(id,action,kw){
|
||||||
|
await fetch(`/identity/${id}/resolve`,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({action,...kw})});
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
async function scan(){
|
||||||
|
const r=await (await fetch('/players/scan',{method:'POST'})).json();
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
load();
|
||||||
|
</script>
|
||||||
|
<script src="/nav.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -104,6 +104,26 @@
|
|||||||
.big-empty { text-align: center; padding: 50px 20px; color: var(--fade); }
|
.big-empty { text-align: center; padding: 50px 20px; color: var(--fade); }
|
||||||
.big-empty .ico { font-size: 2.4rem; }
|
.big-empty .ico { font-size: 2.4rem; }
|
||||||
.big-empty a { color: var(--accent); text-decoration: none; }
|
.big-empty a { color: var(--accent); text-decoration: none; }
|
||||||
|
/* running timeline */
|
||||||
|
ul.tl { list-style: none; margin: 0; padding: 0; }
|
||||||
|
ul.tl li { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--bg-line); align-items: baseline; font-size: .92rem; line-height: 1.4; }
|
||||||
|
ul.tl li:last-child { border-bottom: none; }
|
||||||
|
.tl-time { color: var(--fade); font-variant-numeric: tabular-nums; font-size: .78rem; min-width: 60px; flex: none; }
|
||||||
|
.tl-body { flex: 1; }
|
||||||
|
.tl-amt { margin-left: 6px; font-variant-numeric: tabular-nums; }
|
||||||
|
li.start .tl-body { color: var(--accent); font-weight: 600; }
|
||||||
|
li.scar .tl-body, li.confidence .tl-body { font-style: italic; }
|
||||||
|
.tl-body a.hand { color: var(--accent); text-decoration: none; white-space: nowrap; }
|
||||||
|
/* quick-capture (no LLM) + inline correction controls */
|
||||||
|
.quick { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 14px; }
|
||||||
|
.quick input { width: 100px; background: var(--bg-line); border: 1px solid var(--border);
|
||||||
|
border-radius: 8px; padding: 8px 10px; color: var(--text); }
|
||||||
|
.quick input:focus { outline: none; border-color: var(--accent); }
|
||||||
|
.quick button { background: var(--accent); color: #0a0a0a; border: 1px solid var(--accent);
|
||||||
|
border-radius: 8px; padding: 8px 12px; cursor: pointer; font-weight: 600; }
|
||||||
|
button.mini { background: none; border: none; color: var(--fade); cursor: pointer;
|
||||||
|
font-size: .9rem; padding: 0 6px; }
|
||||||
|
button.mini:active { color: var(--accent); }
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -207,6 +227,30 @@
|
|||||||
} catch(e){ alert('Delete failed: '+e.message); }
|
} catch(e){ alert('Delete failed: '+e.message); }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Quick-capture (no LLM): post a number to a direct endpoint, then refresh.
|
||||||
|
function numVal(id){ const el = document.getElementById(id); return Number(((el && el.value) || '').replace(/[^0-9.]/g,'')); }
|
||||||
|
async function postQuick(url, amount){
|
||||||
|
const r = await fetch(url, { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify({ amount }) });
|
||||||
|
const d = await r.json();
|
||||||
|
if(!d.ok){ alert(d.error || 'failed'); return false; }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
async function postStack(){ const v = numVal('qStack'); if(v && await postQuick('/session/stack', v)){ document.getElementById('qStack').value=''; refresh(); } }
|
||||||
|
async function postBuyin(){ const v = numVal('qBuyin'); if(v && await postQuick('/session/buyin', v)){ document.getElementById('qBuyin').value=''; refresh(); } }
|
||||||
|
async function postCashout(){
|
||||||
|
if(!curSession) return;
|
||||||
|
const v = numVal('qCashout'); if(!v) return;
|
||||||
|
const r = await fetch('/session/'+curSession.id, { method:'PATCH', headers:{'Content-Type':'application/json'}, body: JSON.stringify({ cash_out: v }) });
|
||||||
|
if(!(await r.json()).ok){ alert('failed'); return; }
|
||||||
|
document.getElementById('qCashout').value=''; refresh();
|
||||||
|
}
|
||||||
|
async function renamePlayer(id, current){
|
||||||
|
const name = prompt('Rename player', current || ''); if(!name) return;
|
||||||
|
const r = await fetch('/player/'+id, { method:'PATCH', headers:{'Content-Type':'application/json'}, body: JSON.stringify({ name }) });
|
||||||
|
if(!(await r.json()).ok){ alert('failed'); return; }
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
|
||||||
function render(data){
|
function render(data){
|
||||||
const s = data.session;
|
const s = data.session;
|
||||||
if (!s) {
|
if (!s) {
|
||||||
@@ -219,7 +263,9 @@
|
|||||||
}
|
}
|
||||||
curSession = s;
|
curSession = s;
|
||||||
const stack = data.stack || {};
|
const stack = data.stack || {};
|
||||||
|
const timeline = data.timeline || [];
|
||||||
const hands = data.hands || [];
|
const hands = data.hands || [];
|
||||||
|
const roster = data.roster || [];
|
||||||
const villains = data.villains || [];
|
const villains = data.villains || [];
|
||||||
const notes = data.notes || [];
|
const notes = data.notes || [];
|
||||||
const stats = data.stats || {};
|
const stats = data.stats || {};
|
||||||
@@ -274,7 +320,25 @@
|
|||||||
<span class="stack-meta">bought in ${money(stack.buy_in)}<br>${(stack.log||[]).length} update(s)</span>
|
<span class="stack-meta">bought in ${money(stack.buy_in)}<br>${(stack.log||[]).length} update(s)</span>
|
||||||
</div>
|
</div>
|
||||||
${sparkline(stack.log || [])}
|
${sparkline(stack.log || [])}
|
||||||
${stack.current == null ? '<p class="empty" style="margin:12px 0 0">No stack logged yet — tell Lyra your stack ("I\'m at 350").</p>' : ''}
|
${stack.current == null ? '<p class="empty" style="margin:12px 0 0">No stack logged yet — log it below or tell Lyra ("I\'m at 350").</p>' : ''}
|
||||||
|
<div class="quick">
|
||||||
|
<input id="qStack" type="number" inputmode="decimal" placeholder="Stack $" onkeydown="if(event.key==='Enter')postStack()">
|
||||||
|
<button onclick="postStack()">Log stack</button>
|
||||||
|
<input id="qBuyin" type="number" inputmode="decimal" placeholder="Buy-in $" onkeydown="if(event.key==='Enter')postBuyin()">
|
||||||
|
<button onclick="postBuyin()">Add buy-in</button>
|
||||||
|
<input id="qCashout" type="number" inputmode="decimal" placeholder="Cash out $" onkeydown="if(event.key==='Enter')postCashout()">
|
||||||
|
<button onclick="postCashout()">Cash out</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<p class="label">📜 Timeline</p>
|
||||||
|
${timeline.length ? `<ul class="tl">${timeline.map(e => `
|
||||||
|
<li class="${esc(e.kind)}">
|
||||||
|
<span class="tl-time">${esc(e.time)}</span>
|
||||||
|
<span class="tl-body">${esc(e.text)}${e.amount != null ? ` <b class="tl-amt">${money(e.amount)}</b>` : ''}${e.result != null ? ` <span class="res ${e.result>=0?'up':'down'}">${signed(e.result)}</span>` : ''}${e.hand_id ? ` <a class="hand" href="/hand/${e.hand_id}">hand ›</a>` : ''}</span>
|
||||||
|
</li>`).join('')}</ul>`
|
||||||
|
: '<p class="empty">Nothing yet tonight — the running log fills in as you play.</p>'}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
@@ -306,11 +370,25 @@
|
|||||||
: '<p class="empty">No scars logged — mistakes to study land here.</p>'}
|
: '<p class="empty">No scars logged — mistakes to study land here.</p>'}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="card">
|
||||||
|
<p class="label">🪑 Table (${roster.length})</p>
|
||||||
|
${roster.length ? `<ul class="rows">${roster.map(v => `
|
||||||
|
<li class="villain">
|
||||||
|
${v.seat ? `<span class="cat">${esc(v.seat)}</span> ` : ''}<b>${esc(v.name)}</b>
|
||||||
|
${v.category ? `<span class="cat">[${esc(v.category)}]</span>` : ''}
|
||||||
|
${v.reads ? `<span class="cat">· ${v.reads} read${v.reads===1?'':'s'}</span>` : ''}
|
||||||
|
<button class="mini" title="Rename / fix" onclick="renamePlayer(${v.id}, '${esc(v.name||'').replace(/'/g,"\\'")}')">✎</button>
|
||||||
|
${v.last_note ? `<div class="note-meta">“${esc(v.last_note)}”</div>` : ''}
|
||||||
|
</li>`).join('')}</ul>`
|
||||||
|
: '<p class="empty">No roster yet — tell Lyra who is at the table.</p>'}
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<p class="label">Villains seen</p>
|
<p class="label">Villains seen</p>
|
||||||
${villains.length ? `<ul class="rows">${villains.map(v => `
|
${villains.length ? `<ul class="rows">${villains.map(v => `
|
||||||
<li class="villain">
|
<li class="villain">
|
||||||
<b>${esc(v.name)}</b> ${v.category ? `<span class="cat">[${esc(v.category)}]</span>` : ''}
|
<b>${esc(v.name)}</b> ${v.category ? `<span class="cat">[${esc(v.category)}]</span>` : ''}
|
||||||
|
<button class="mini" title="Rename / fix" onclick="renamePlayer(${v.id}, '${esc(v.name||'').replace(/'/g,"\\'")}')">✎</button>
|
||||||
${v.tendencies ? `<div>${esc(v.tendencies)}</div>` : ''}
|
${v.tendencies ? `<div>${esc(v.tendencies)}</div>` : ''}
|
||||||
${v.last_note ? `<div class="note-meta">“${esc(v.last_note)}”</div>` : ''}
|
${v.last_note ? `<div class="note-meta">“${esc(v.last_note)}”</div>` : ''}
|
||||||
</li>`).join('')}</ul>`
|
</li>`).join('')}</ul>`
|
||||||
|
|||||||
@@ -56,6 +56,16 @@ body.dark {
|
|||||||
|
|
||||||
html {
|
html {
|
||||||
overscroll-behavior: none;
|
overscroll-behavior: none;
|
||||||
|
/* Stop iOS from inflating font sizes when the device rotates to landscape (and
|
||||||
|
leaving them big on rotate back). Every other page sets this; the chat didn't. */
|
||||||
|
-webkit-text-size-adjust: 100%;
|
||||||
|
text-size-adjust: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
html {
|
||||||
|
/* Paints the iOS home-indicator strip below the dvh shell; match the tab bar so the
|
||||||
|
bar looks like it continues to the physical bottom edge. */
|
||||||
|
background: var(--bg-line);
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
@@ -826,15 +836,17 @@ select:hover {
|
|||||||
@media screen and (max-width: 768px) {
|
@media screen and (max-width: 768px) {
|
||||||
body {
|
body {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
background: var(--bg-elev); /* matches the tab bar so any strip below #chat is seamless */
|
background: var(--bg-line); /* matches the tab bar so any strip below #chat is seamless */
|
||||||
}
|
}
|
||||||
|
|
||||||
#chat {
|
#chat {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
top: 0; left: 0; right: 0;
|
top: 0; left: 0; right: 0;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100dvh; /* the *visible* viewport (excludes the home-indicator zone);
|
height: 100vh; /* fallback for old browsers */
|
||||||
overrides the base 95vh. Body bg matches the bar below it. */
|
height: 100dvh; /* the *visible* viewport — keep all content (incl. the tab bar)
|
||||||
|
inside what iOS actually paints, so nothing is clipped into the
|
||||||
|
home-indicator dead zone. The strip below is matched in color. */
|
||||||
background: var(--bg-dark);
|
background: var(--bg-dark);
|
||||||
border-radius: 0;
|
border-radius: 0;
|
||||||
border: none;
|
border: none;
|
||||||
@@ -918,8 +930,11 @@ select:hover {
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex: none; /* never let it be compressed/clipped by the flex column */
|
flex: none; /* never let it be compressed/clipped by the flex column */
|
||||||
border-top: 1px solid var(--border);
|
border-top: 1px solid var(--border);
|
||||||
background: var(--bg-elev);
|
background: var(--bg-line); /* lighter than the page so it reads as a solid bar */
|
||||||
padding-bottom: 6px; /* 100dvh already excludes the home-indicator zone */
|
/* Shell is 100dvh, so the bar sits at the bottom of the rendered area with the icons
|
||||||
|
fully visible. Minimal padding keeps them low; the home-indicator strip just below
|
||||||
|
the rendered area is painted the same color (html bg) so the bar looks continuous. */
|
||||||
|
padding-bottom: 4px;
|
||||||
padding-left: env(safe-area-inset-left);
|
padding-left: env(safe-area-inset-left);
|
||||||
padding-right: env(safe-area-inset-right);
|
padding-right: env(safe-area-inset-right);
|
||||||
}
|
}
|
||||||
@@ -1227,3 +1242,31 @@ select:hover {
|
|||||||
scroll-behavior: auto !important;
|
scroll-behavior: auto !important;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Stack quick-capture (2nd input box on the chat page) — logs without the LLM. */
|
||||||
|
#stackQuick {
|
||||||
|
display: flex;
|
||||||
|
gap: 8px;
|
||||||
|
align-items: center;
|
||||||
|
padding: 6px 12px;
|
||||||
|
border-top: 1px solid var(--border);
|
||||||
|
background: var(--bg-panel);
|
||||||
|
}
|
||||||
|
#stackQuick input {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
padding: 8px 10px;
|
||||||
|
background: var(--bg-elev);
|
||||||
|
color: inherit;
|
||||||
|
border: 1px solid var(--border);
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
#stackQuick button {
|
||||||
|
padding: 8px 14px;
|
||||||
|
background: var(--accent);
|
||||||
|
color: #000;
|
||||||
|
border: none;
|
||||||
|
border-radius: 8px;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
"""The mind pipeline: the deliberation pass (think privately before answering)."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def lyra(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.mind as mind
|
||||||
|
importlib.reload(mind)
|
||||||
|
return memory, mind
|
||||||
|
|
||||||
|
|
||||||
|
def test_should_deliberate_skips_trivial(lyra):
|
||||||
|
_, mind = lyra
|
||||||
|
assert mind._should_deliberate("How would we actually start building this?")
|
||||||
|
assert mind._should_deliberate("I disagree, that seems risky")
|
||||||
|
for trivial in ("ok", "lol", "thanks", "yeah", "nice", "👍", "k"):
|
||||||
|
assert not mind._should_deliberate(trivial)
|
||||||
|
assert not mind._should_deliberate("ok!") # punctuation stripped
|
||||||
|
assert not mind._should_deliberate("hey") # too short
|
||||||
|
|
||||||
|
|
||||||
|
def test_deliberation_note_runs_and_appends(lyra, monkeypatch):
|
||||||
|
memory, mind = lyra
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def fake_complete(messages, backend=None, model=None):
|
||||||
|
calls.append(messages)
|
||||||
|
return "I actually think the first move is the smallest end-to-end slice."
|
||||||
|
|
||||||
|
memory.ensure_session("s1")
|
||||||
|
monkeypatch.setattr(mind.llm, "complete", fake_complete)
|
||||||
|
note = mind._deliberation_note("s1", "How would we start on this?", "cloud", None)
|
||||||
|
assert note and note["role"] == "system"
|
||||||
|
assert "first move is the smallest" in note["content"] # her thinking carried in
|
||||||
|
assert "numbered list" in note["content"].lower() # voice enforcement attached
|
||||||
|
assert len(calls) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_deliberation_skipped_when_disabled(lyra, monkeypatch):
|
||||||
|
_, mind = lyra
|
||||||
|
monkeypatch.setenv("CHAT_DELIBERATE", "false")
|
||||||
|
called = []
|
||||||
|
monkeypatch.setattr(mind.llm, "complete", lambda *a, **k: called.append(1) or "x")
|
||||||
|
assert mind._deliberation_note("s1", "a real substantive question here", "cloud", None) is None
|
||||||
|
assert called == [] # no LLM call when off
|
||||||
|
|
||||||
|
|
||||||
|
def test_persona_core_is_tight_situational_is_gated(lyra):
|
||||||
|
memory, mind = lyra
|
||||||
|
from lyra import persona
|
||||||
|
core, full = persona.core_prompt(), persona.system_prompt()
|
||||||
|
assert "How you talk" in core and "How you actually work" not in core # voice core, self-model not
|
||||||
|
assert len(core) < len(full) and persona.section("How you actually work")
|
||||||
|
|
||||||
|
memory.ensure_session("s1")
|
||||||
|
casual = " ".join(m["content"] for m in mind.build_messages("s1", "any dinner ideas tonight?")
|
||||||
|
if m["role"] == "system")
|
||||||
|
meta = " ".join(m["content"] for m in mind.build_messages("s1", "how does your memory actually work?")
|
||||||
|
if m["role"] == "system")
|
||||||
|
assert "How you actually work" not in casual # situational section omitted on a casual turn
|
||||||
|
assert "How you actually work" in meta # pulled in for a meta question
|
||||||
|
|
||||||
|
|
||||||
|
def test_assemble_runs_the_pipeline(lyra, monkeypatch):
|
||||||
|
memory, mind = lyra
|
||||||
|
monkeypatch.setenv("CHAT_DELIBERATE", "false") # keep it offline for the structure test
|
||||||
|
memory.ensure_session("s1")
|
||||||
|
turn = mind.assemble("s1", "hey what's up", "cloud", None)
|
||||||
|
assert turn.mode is not None # route ran
|
||||||
|
assert turn.messages and turn.messages[-1]["role"] == "user" # compose ran
|
||||||
|
assert turn.messages[-1]["content"] == "hey what's up"
|
||||||
|
|
||||||
|
|
||||||
|
# --- mind/mouth split (P3) ----------------------------------------------
|
||||||
|
|
||||||
|
def test_mouth_target_off_by_default(monkeypatch):
|
||||||
|
import importlib
|
||||||
|
from lyra import config
|
||||||
|
monkeypatch.delenv("MOUTH_BACKEND", raising=False)
|
||||||
|
monkeypatch.delenv("MOUTH_MODEL", raising=False)
|
||||||
|
import lyra.chat as chat
|
||||||
|
importlib.reload(chat)
|
||||||
|
assert chat._mouth_target(config.load(), "cloud", "gpt-4o") is None # mouth == mind
|
||||||
|
|
||||||
|
|
||||||
|
def test_mouth_target_when_configured(monkeypatch):
|
||||||
|
import importlib
|
||||||
|
from lyra import config
|
||||||
|
monkeypatch.setenv("MOUTH_BACKEND", "local")
|
||||||
|
monkeypatch.setenv("MOUTH_MODEL", "dolphin3:8b")
|
||||||
|
import lyra.chat as chat
|
||||||
|
importlib.reload(chat)
|
||||||
|
assert chat._mouth_target(config.load(), "cloud", "gpt-4o") == ("local", "dolphin3:8b")
|
||||||
|
|
||||||
|
|
||||||
|
def test_voice_messages_carries_draft_and_instruction(lyra):
|
||||||
|
_, mind = lyra
|
||||||
|
out = mind.voice_messages([{"role": "user", "content": "hi"}], "draft with FACT 42")
|
||||||
|
assert out[-2] == {"role": "assistant", "content": "draft with FACT 42"}
|
||||||
|
assert out[-1]["role"] == "system" and "your own voice" in out[-1]["content"].lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_voice_pass_revoices_then_falls_back(lyra, monkeypatch):
|
||||||
|
_, mind = lyra
|
||||||
|
import importlib
|
||||||
|
import lyra.chat as chat
|
||||||
|
importlib.reload(chat)
|
||||||
|
monkeypatch.setattr(chat.llm, "complete", lambda msgs, backend=None, model=None: "voiced (FACT 42)")
|
||||||
|
assert chat._voice_pass([], "draft FACT 42", "local", "dolphin3:8b") == "voiced (FACT 42)"
|
||||||
|
# on failure it keeps the mind's draft (chat must not break)
|
||||||
|
def boom(*a, **k):
|
||||||
|
raise RuntimeError("mouth down")
|
||||||
|
monkeypatch.setattr(chat.llm, "complete", boom)
|
||||||
|
assert chat._voice_pass([], "draft FACT 42", "local", "dolphin3:8b") == "draft FACT 42"
|
||||||
+29
-1
@@ -20,7 +20,7 @@ def lyra(tmp_path, monkeypatch):
|
|||||||
# reflect() expects JSON back; everything else just stores the text.
|
# reflect() expects JSON back; everything else just stores the text.
|
||||||
monkeypatch.setattr(
|
monkeypatch.setattr(
|
||||||
llm, "complete",
|
llm, "complete",
|
||||||
lambda messages, backend=None, model=None:
|
lambda messages, backend=None, model=None, **_:
|
||||||
'{"mood":"focused","valence":0.7,"new_reflections":["I got some thinking done."]}',
|
'{"mood":"focused","valence":0.7,"new_reflections":["I got some thinking done."]}',
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -77,3 +77,31 @@ def test_dream_cycle_consolidates_and_persists(lyra):
|
|||||||
state2 = dream.dream_cycle(force=False)
|
state2 = dream.dream_cycle(force=False)
|
||||||
assert state2["dream"]["cycle_count"] == 2
|
assert state2["dream"]["cycle_count"] == 2
|
||||||
assert state2["drives"]["continuity"] == 0.0
|
assert state2["drives"]["continuity"] == 0.0
|
||||||
|
|
||||||
|
|
||||||
|
def test_dream_cycle_stops_when_over_budget(lyra, monkeypatch):
|
||||||
|
memory = lyra
|
||||||
|
from lyra import dream, notify
|
||||||
|
|
||||||
|
for k in range(7):
|
||||||
|
_seed(memory, f"s{k}", 4)
|
||||||
|
|
||||||
|
# Go over budget right after the first heavy stage: first check passes
|
||||||
|
# (summarize runs), every check after trips.
|
||||||
|
checks = {"n": 0}
|
||||||
|
|
||||||
|
def fake_over(deadline):
|
||||||
|
checks["n"] += 1
|
||||||
|
return checks["n"] > 1
|
||||||
|
monkeypatch.setattr(dream, "_over_budget", fake_over)
|
||||||
|
|
||||||
|
pings: list = []
|
||||||
|
monkeypatch.setattr(notify, "push",
|
||||||
|
lambda title, message, **k: pings.append((title, message)) or True)
|
||||||
|
|
||||||
|
state = dream.dream_cycle(force=True)
|
||||||
|
acts = state["dream"]["last_actions"]
|
||||||
|
|
||||||
|
assert any("stopped early" in a for a in acts) # bailed
|
||||||
|
assert not any("reflected" in a for a in acts) # later stage skipped
|
||||||
|
assert pings, "expected an over-budget ntfy push"
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
"""Era rollups: only re-digest months whose session count changed (incremental)."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from lyra.memory import Era
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def era(monkeypatch):
|
||||||
|
import lyra.era as era
|
||||||
|
importlib.reload(era)
|
||||||
|
return era
|
||||||
|
|
||||||
|
|
||||||
|
def test_rebuild_eras_is_incremental(era, monkeypatch):
|
||||||
|
by_month = {"2025-01": ["a", "b"], "2025-02": ["c"]}
|
||||||
|
stored: dict[str, int] = {}
|
||||||
|
built: list[str] = []
|
||||||
|
|
||||||
|
monkeypatch.setattr(era.memory, "summaries_by_month", lambda: dict(by_month))
|
||||||
|
monkeypatch.setattr(era.memory, "list_eras",
|
||||||
|
lambda: [Era(m, "x", c, "t") for m, c in stored.items()])
|
||||||
|
monkeypatch.setattr(era.memory, "store_era",
|
||||||
|
lambda month, content, n: (stored.__setitem__(month, n), built.append(month)))
|
||||||
|
monkeypatch.setattr(era, "_digest_month", lambda gists, backend: "digest") # no LLM
|
||||||
|
|
||||||
|
r1 = era.rebuild_eras(backend="local") # first pass: both built
|
||||||
|
assert r1["built"] == 2 and r1["skipped"] == 0
|
||||||
|
|
||||||
|
built.clear()
|
||||||
|
r2 = era.rebuild_eras(backend="local") # nothing changed: all skipped
|
||||||
|
assert r2["built"] == 0 and r2["skipped"] == 2 and built == []
|
||||||
|
|
||||||
|
built.clear()
|
||||||
|
by_month["2025-02"].append("d") # one month gains a session
|
||||||
|
r3 = era.rebuild_eras(backend="local")
|
||||||
|
assert r3["built"] == 1 and r3["skipped"] == 1 and built == ["2025-02"]
|
||||||
|
|
||||||
|
built.clear()
|
||||||
|
r4 = era.rebuild_eras(backend="local", force=True) # force rebuilds all
|
||||||
|
assert r4["built"] == 2
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
"""The canonical structured-hand contract (docs/HAND_HISTORY.md): normalize + export.
|
||||||
|
|
||||||
|
normalize_structured() is the single guarantee that every stored / replayed / exported
|
||||||
|
hand has the versioned shape RTO consumes.
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
def _full_hand():
|
||||||
|
return {
|
||||||
|
"game": "NLH", "stakes": "1/3", "hero_pos": "BTN",
|
||||||
|
"hero_cards": ["ah", "kh"],
|
||||||
|
"players": [
|
||||||
|
{"pos": "BTN", "stack": 300, "name": "Hero"},
|
||||||
|
{"pos": "BB", "stack": 250, "name": "Sal", "cards": ["qs", "qd"]},
|
||||||
|
],
|
||||||
|
"actions": [
|
||||||
|
{"street": "preflop", "pos": "BTN", "action": "raise", "amount": 15},
|
||||||
|
{"street": "flop", "board": ["7♦", "2♣", "5♥"]},
|
||||||
|
{"street": "flop", "pos": "BB", "action": "check"},
|
||||||
|
],
|
||||||
|
"board": ["7♦", "2♣", "5♥"],
|
||||||
|
"result": {"pot": 40, "hero_net": 25, "summary": "won at showdown"},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_stamps_version(poker):
|
||||||
|
out = poker.normalize_structured({"hero_pos": "CO"})
|
||||||
|
assert out["schema_version"] == poker.HAND_SCHEMA_VERSION
|
||||||
|
|
||||||
|
|
||||||
|
def test_observed_hand_never_attributed_to_hero(poker):
|
||||||
|
# Brian narrated a hand between two other players — hero_involved=false.
|
||||||
|
out = poker.normalize_structured({
|
||||||
|
"hero_involved": False,
|
||||||
|
"hero_pos": "CO", "hero_cards": ["Kx", "Kx"], # model slipped these in
|
||||||
|
"players": [{"pos": "CO", "cards": ["Kx", "Kx"]}, {"pos": "BB", "cards": ["Ax", "Ax"]}],
|
||||||
|
"result": {"pot": 600, "hero_net": 300},
|
||||||
|
})
|
||||||
|
assert out["hero_pos"] is None # not pinned to Brian
|
||||||
|
assert out["hero_cards"] == []
|
||||||
|
assert out["result"]["hero_net"] is None # a pot he wasn't in
|
||||||
|
assert not any(pl.get("hero") for pl in out["players"]) # nobody flagged hero
|
||||||
|
|
||||||
|
|
||||||
|
def test_hero_hand_still_attributed(poker):
|
||||||
|
out = poker.normalize_structured({
|
||||||
|
"hero_involved": True, "hero_pos": "BTN", "hero_cards": ["As", "Ks"],
|
||||||
|
"players": [{"pos": "BTN"}]})
|
||||||
|
assert out["hero_pos"] == "BTN"
|
||||||
|
hero = next(pl for pl in out["players"] if pl.get("pos") == "BTN")
|
||||||
|
assert hero.get("hero") and hero["cards"] == ["As", "Ks"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_card_normalization(poker):
|
||||||
|
out = poker.normalize_structured(_full_hand())
|
||||||
|
assert out["hero_cards"] == ["Ah", "Kh"] # lowercased input -> canonical
|
||||||
|
assert out["board"] == ["7d", "2c", "5h"] # unicode suits -> letters
|
||||||
|
assert out["actions"][1]["board"] == ["7d", "2c", "5h"]
|
||||||
|
# ten + suit symbol together
|
||||||
|
assert poker.normalize_structured({"board": ["10♠"]})["board"] == ["Ts"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_unknown_cards_preserved(poker):
|
||||||
|
out = poker.normalize_structured({"hero_cards": ["Ax", "x"], "board": ["Ax", "4x", "x"]})
|
||||||
|
assert out["hero_cards"] == ["Ax", "x"] # placeholders kept, not dropped
|
||||||
|
assert out["completeness"]["cards"] is False
|
||||||
|
assert out["completeness"]["board"] is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_hero_synced_into_players(poker):
|
||||||
|
out = poker.normalize_structured(_full_hand())
|
||||||
|
hero = next(p for p in out["players"] if p["pos"] == "BTN")
|
||||||
|
assert hero["hero"] is True
|
||||||
|
assert hero["cards"] == ["Ah", "Kh"] # mirrored from hero_cards
|
||||||
|
assert sum(1 for p in out["players"] if p.get("hero")) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_hero_inserted_when_missing_from_players(poker):
|
||||||
|
out = poker.normalize_structured({"hero_pos": "SB", "hero_cards": ["As", "Ad"], "players": []})
|
||||||
|
assert out["players"] == [{"pos": "SB", "hero": True, "cards": ["As", "Ad"]}]
|
||||||
|
|
||||||
|
|
||||||
|
def test_completeness_full_hand(poker):
|
||||||
|
c = poker.normalize_structured(_full_hand())["completeness"]
|
||||||
|
assert c == {"cards": True, "board": True, "actions": True}
|
||||||
|
|
||||||
|
|
||||||
|
def test_idempotent(poker):
|
||||||
|
once = poker.normalize_structured(_full_hand())
|
||||||
|
twice = poker.normalize_structured(once)
|
||||||
|
assert once == twice
|
||||||
|
|
||||||
|
|
||||||
|
def test_store_and_get_roundtrip_is_normalized(poker):
|
||||||
|
sid = poker.start_session(venue="Meadows", stakes="1/3", buy_in=400)
|
||||||
|
hid = poker.store_hand_history(_full_hand(), session_id=sid, tag="well_played")
|
||||||
|
got = poker.get_hand(hid)["structured"]
|
||||||
|
assert got["schema_version"] == poker.HAND_SCHEMA_VERSION
|
||||||
|
assert got["board"] == ["7d", "2c", "5h"]
|
||||||
|
assert got["completeness"]["cards"] is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_list_recent_hands_flags_structured(poker):
|
||||||
|
sid = poker.start_session(venue="Meadows", stakes="1/3", buy_in=400)
|
||||||
|
structured_id = poker.store_hand_history(_full_hand(), session_id=sid)
|
||||||
|
flat_id = poker.log_hand(session_id=sid, position="CO", hole_cards="Jc Jd")
|
||||||
|
rows = {r["id"]: r for r in poker.list_recent_hands()}
|
||||||
|
assert rows[structured_id]["has_structured"] is True
|
||||||
|
assert rows[flat_id]["has_structured"] is False
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
"""llm.complete: `max_tokens` and `timeout` are threaded into the backend call.
|
||||||
|
|
||||||
|
The OpenAI client is faked so nothing hits a network. We assert the generation
|
||||||
|
cap reaches the create() call and the fast-fail timeout reaches the client (with
|
||||||
|
max_retries=0 so summary.py owns the retry policy, not the SDK).
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import types
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from lyra import llm
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def fake_openai(monkeypatch):
|
||||||
|
recorded: dict = {}
|
||||||
|
|
||||||
|
class FakeCompletions:
|
||||||
|
def create(self, **kwargs):
|
||||||
|
recorded["create"] = kwargs
|
||||||
|
msg = types.SimpleNamespace(content="ok")
|
||||||
|
return types.SimpleNamespace(choices=[types.SimpleNamespace(message=msg)])
|
||||||
|
|
||||||
|
class FakeClient:
|
||||||
|
def __init__(self, **kwargs):
|
||||||
|
recorded["client"] = kwargs
|
||||||
|
self.chat = types.SimpleNamespace(completions=FakeCompletions())
|
||||||
|
|
||||||
|
monkeypatch.setattr(llm, "OpenAI", FakeClient)
|
||||||
|
monkeypatch.setattr(llm, "load", lambda: types.SimpleNamespace(
|
||||||
|
mi50_base_url="http://mi50/v1", mi50_model="local-gpu",
|
||||||
|
cloud_model="gpt-4o-mini", openai_api_key="sk-test", local_model="l",
|
||||||
|
))
|
||||||
|
return recorded
|
||||||
|
|
||||||
|
|
||||||
|
def test_mi50_threads_max_tokens_and_timeout(fake_openai):
|
||||||
|
out = llm.complete([{"role": "user", "content": "hi"}],
|
||||||
|
backend="mi50", max_tokens=768, timeout=150)
|
||||||
|
|
||||||
|
assert out == "ok"
|
||||||
|
assert fake_openai["create"]["max_tokens"] == 768
|
||||||
|
assert fake_openai["client"]["timeout"] == 150
|
||||||
|
assert fake_openai["client"]["max_retries"] == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_cloud_threads_max_tokens_and_timeout(fake_openai):
|
||||||
|
llm.complete([{"role": "user", "content": "hi"}],
|
||||||
|
backend="cloud", max_tokens=768, timeout=150)
|
||||||
|
|
||||||
|
assert fake_openai["create"]["max_tokens"] == 768
|
||||||
|
assert fake_openai["client"]["timeout"] == 150
|
||||||
|
assert fake_openai["client"]["max_retries"] == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_bounds_calls_even_without_explicit_timeout(fake_openai):
|
||||||
|
# No cap / timeout passed -> still bounded: 300s default + no SDK retries, so
|
||||||
|
# no call can silently inherit the SDK's 600s x2 (~30 min). No length cap
|
||||||
|
# unless asked, though.
|
||||||
|
llm.complete([{"role": "user", "content": "hi"}], backend="mi50")
|
||||||
|
|
||||||
|
assert "max_tokens" not in fake_openai["create"]
|
||||||
|
assert fake_openai["client"]["timeout"] == 300
|
||||||
|
assert fake_openai["client"]["max_retries"] == 0
|
||||||
@@ -47,6 +47,36 @@ def test_every_mode_tool_exists(lyra):
|
|||||||
assert set(mode.tools) <= set(tools.TOOLS), f"{mode.key} references unknown tools"
|
assert set(mode.tools) <= set(tools.TOOLS), f"{mode.key} references unknown tools"
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_mode_tool_switches_session(lyra):
|
||||||
|
memory, _, _, tools = lyra
|
||||||
|
memory.ensure_session("s1")
|
||||||
|
out = tools.dispatch("set_mode", {"mode": "decide"}, {"session_id": "s1"})
|
||||||
|
assert "Decide" in out and memory.get_session_mode("s1") == "decide"
|
||||||
|
# unknown mode is handled, session unchanged
|
||||||
|
assert "unknown" in tools.dispatch("set_mode", {"mode": "nope"}, {"session_id": "s1"}).lower()
|
||||||
|
assert memory.get_session_mode("s1") == "decide"
|
||||||
|
|
||||||
|
|
||||||
|
def test_work_modes_present_and_gated(lyra):
|
||||||
|
_, _, modes, tools = lyra
|
||||||
|
# the full set Brian chose
|
||||||
|
assert set(modes.MODES) == {"conversation", "poker_cash", "build", "explore", "study", "decide"}
|
||||||
|
# Decide = read-only lookups for context, no live logging; has a real card
|
||||||
|
decide = _names(tools.specs(modes.DECIDE.tools))
|
||||||
|
assert {"running_stats", "recent_sessions"} <= decide and "log_hand" not in decide
|
||||||
|
assert modes.DECIDE.card
|
||||||
|
# Build/Explore are conversational: base agency tools only, no live poker logging
|
||||||
|
for key in ("build", "explore"):
|
||||||
|
names = _names(tools.specs(modes.get(key).tools))
|
||||||
|
assert {"journal_write", "note", "think_about"} <= names
|
||||||
|
assert "log_hand" not in names and "start_session" not in names
|
||||||
|
assert modes.get(key).card # each has a real behavioral card
|
||||||
|
# Study = read-only review: lookups + equity, but no live logging
|
||||||
|
study = _names(tools.specs(modes.STUDY.tools))
|
||||||
|
assert {"running_stats", "analyze_spot", "player_profile"} <= study
|
||||||
|
assert "log_hand" not in study and "end_session" not in study
|
||||||
|
|
||||||
|
|
||||||
def test_mode_resolution_and_persistence(lyra):
|
def test_mode_resolution_and_persistence(lyra):
|
||||||
memory, _, modes, _ = lyra
|
memory, _, modes, _ = lyra
|
||||||
assert modes.get(None).key == modes.DEFAULT
|
assert modes.get(None).key == modes.DEFAULT
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
"""Pattern desk: embedded scar recall + strategy gating in the scouting desk."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def _idx(w: str) -> int:
|
||||||
|
# Stable across processes (unlike hash()), so threshold tests aren't flaky.
|
||||||
|
return int.from_bytes(hashlib.md5(w.encode()).digest()[:4], "little") % 256
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_embed(texts):
|
||||||
|
out = []
|
||||||
|
for t in texts:
|
||||||
|
v = np.zeros(256, dtype=np.float32)
|
||||||
|
for w in t.lower().split():
|
||||||
|
v[_idx(w)] += 1.0
|
||||||
|
out.append((v if v.any() else np.full(256, 1e-6, dtype=np.float32)).tolist())
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mods(tmp_path, monkeypatch):
|
||||||
|
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
||||||
|
from lyra import llm
|
||||||
|
monkeypatch.setattr(llm, "embed", _fake_embed)
|
||||||
|
import lyra.memory as memory
|
||||||
|
importlib.reload(memory)
|
||||||
|
import lyra.poker as poker
|
||||||
|
importlib.reload(poker)
|
||||||
|
import lyra.scouting as scouting
|
||||||
|
importlib.reload(scouting)
|
||||||
|
return poker, scouting
|
||||||
|
|
||||||
|
|
||||||
|
def test_scar_recall_finds_similar_past_leak(mods):
|
||||||
|
poker, _ = mods
|
||||||
|
old = poker.start_session(stakes="1/3", buy_in=300)
|
||||||
|
poker.log_ritual("scar", "overvalued top pair and stacked off on a wet board",
|
||||||
|
classification="punt", session_id=old)
|
||||||
|
poker.end_session(200, session_id=old)
|
||||||
|
hits = poker.recall_similar_rituals("stacked off top pair wet board again")
|
||||||
|
assert hits and hits[0]["classification"] == "punt"
|
||||||
|
|
||||||
|
|
||||||
|
def test_recall_excludes_current_session(mods):
|
||||||
|
poker, _ = mods
|
||||||
|
sid = poker.start_session(stakes="1/3", buy_in=300)
|
||||||
|
poker.log_ritual("scar", "punted river bluff into the nut flush", session_id=sid)
|
||||||
|
assert poker.recall_similar_rituals("river bluff nut flush punt", exclude_session=sid) == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_pattern_pass_only_fires_on_strategic_talk(mods):
|
||||||
|
poker, scouting = mods
|
||||||
|
old = poker.start_session(stakes="1/3", buy_in=300)
|
||||||
|
poker.log_ritual("scar", "punting river bluffs into missed draws again",
|
||||||
|
classification="punt", session_id=old)
|
||||||
|
poker.end_session(200, session_id=old)
|
||||||
|
poker.start_session(stakes="1/3", buy_in=300, venue="Meadows")
|
||||||
|
# A routine, non-strategic line pays no embed and surfaces nothing.
|
||||||
|
assert scouting.scout("stack is 350 now", venue="Meadows") is None
|
||||||
|
# A real strategy question in the same shape recalls the leak. (The test stub
|
||||||
|
# embeds by shared tokens; real embeddings match on meaning/paraphrase.)
|
||||||
|
note = scouting.scout(
|
||||||
|
"why do i keep punting river bluffs into missed draws", venue="Meadows")
|
||||||
|
assert note and "punting river bluffs" in note
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
"""Perceive: cheap heuristic read of the moment, and route turning it into a nudge."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from lyra import perceive
|
||||||
|
|
||||||
|
|
||||||
|
def test_reads_tilt():
|
||||||
|
m = perceive.read("I'm so fucking tilted, card dead all night, this is brutal!!")
|
||||||
|
assert m["tilt"] >= 0.5 and m["sentiment"] < 0 and m["kind"] == "emotional"
|
||||||
|
|
||||||
|
|
||||||
|
def test_reads_strategy_calm():
|
||||||
|
m = perceive.read("Should I fold the river here given his range and the board?")
|
||||||
|
assert m["kind"] == "strategic" and m["tilt"] < 0.4
|
||||||
|
|
||||||
|
|
||||||
|
def test_reads_up_energy():
|
||||||
|
m = perceive.read("Let's go!! crushing it tonight, feeling so good!")
|
||||||
|
assert m["sentiment"] > 0 and m["kind"] == "emotional"
|
||||||
|
|
||||||
|
|
||||||
|
def test_reads_build_and_casual():
|
||||||
|
assert perceive.read("let's refactor the cognition pipeline module").get("kind") == "build"
|
||||||
|
assert perceive.read("ok sounds good to me").get("kind") == "casual"
|
||||||
|
assert perceive.read("ok sounds good to me")["tilt"] == 0.0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mind(tmp_path, monkeypatch):
|
||||||
|
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
||||||
|
monkeypatch.setenv("CHAT_DELIBERATE", "false")
|
||||||
|
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.mind as mind
|
||||||
|
importlib.reload(mind)
|
||||||
|
memory.ensure_session("s1")
|
||||||
|
return mind
|
||||||
|
|
||||||
|
|
||||||
|
def test_route_injects_tilt_nudge(mind):
|
||||||
|
turn = mind.assemble("s1", "ugh I'm steaming, fucking coolered again!!", "cloud", None)
|
||||||
|
assert turn.register == "steady"
|
||||||
|
sys_blob = " ".join(m["content"] for m in turn.messages if m["role"] == "system")
|
||||||
|
assert "on tilt" in sys_blob.lower() or "frustrated" in sys_blob.lower()
|
||||||
|
|
||||||
|
|
||||||
|
def test_route_quiet_on_neutral_turn(mind):
|
||||||
|
turn = mind.assemble("s1", "what did we decide about the schema yesterday?", "cloud", None)
|
||||||
|
assert turn.register is None # neutral -> no nudge
|
||||||
|
assert not (turn.moment or {}).get("note")
|
||||||
@@ -18,6 +18,50 @@ def lyra(tmp_path, monkeypatch):
|
|||||||
return poker
|
return poker
|
||||||
|
|
||||||
|
|
||||||
|
def test_disown_hand_clears_hero_attribution(lyra):
|
||||||
|
poker = lyra
|
||||||
|
sid = poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
hid = poker.store_hand_history(
|
||||||
|
{"hero_involved": True, "hero_pos": "MP", "hero_cards": ["As", "3d"],
|
||||||
|
"players": [{"pos": "MP", "cards": ["As", "3d"]}],
|
||||||
|
"result": {"pot": 600, "hero_net": 304}}, session_id=sid, tag="notable")
|
||||||
|
h = poker.disown_hand(hid)
|
||||||
|
assert h["position"] is None and h["hole_cards"] is None and h["result"] is None
|
||||||
|
st = h["structured"]
|
||||||
|
if isinstance(st, str):
|
||||||
|
import json
|
||||||
|
st = json.loads(st)
|
||||||
|
assert st["hero_pos"] is None and not any(pl.get("hero") for pl in st["players"])
|
||||||
|
|
||||||
|
|
||||||
|
def test_hud_notes_scoped_to_session_by_tag(lyra):
|
||||||
|
poker = lyra
|
||||||
|
from lyra import memory
|
||||||
|
sid = poker.start_session(venue="Meadows", stakes="1/3", buy_in=300)
|
||||||
|
# A note tagged to THIS session shows on its HUD...
|
||||||
|
memory.add_journal_entry("note", "villain 3 overfolds turn", source=f"poker:{sid}")
|
||||||
|
# ...her autonomous existential journaling (any other source) does NOT, even
|
||||||
|
# though it's written during the exact same window...
|
||||||
|
memory.add_journal_entry("journal", "the quiet dread between conversations", source="dream")
|
||||||
|
# ...nor a note from a *different* poker session.
|
||||||
|
memory.add_journal_entry("note", "some other night", source=f"poker:{sid + 999}")
|
||||||
|
contents = [n["content"] for n in poker.hud(sid)["notes"]]
|
||||||
|
assert contents == ["villain 3 overfolds turn"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_note_tool_tags_live_poker_session(lyra):
|
||||||
|
poker = lyra
|
||||||
|
from lyra import tools
|
||||||
|
sid = poker.start_session(stakes="1/3", buy_in=300)
|
||||||
|
tools.dispatch("note", {"content": "whale just sat down seat 4"}, {})
|
||||||
|
assert poker.hud(sid)["notes"][0]["content"] == "whale just sat down seat 4"
|
||||||
|
poker.end_session(cash_out=300, session_id=sid)
|
||||||
|
# With no live session, a note falls back to the general journal (source=chat),
|
||||||
|
# so it does NOT attach to the just-closed session's HUD.
|
||||||
|
tools.dispatch("note", {"content": "random afternoon idea"}, {})
|
||||||
|
assert all(n["content"] != "random afternoon idea" for n in poker.hud(sid)["notes"])
|
||||||
|
|
||||||
|
|
||||||
def test_session_lifecycle_and_net(lyra):
|
def test_session_lifecycle_and_net(lyra):
|
||||||
poker = lyra
|
poker = lyra
|
||||||
sid = poker.start_session(venue="Meadows", stakes="1/3", buy_in=400)
|
sid = poker.start_session(venue="Meadows", stakes="1/3", buy_in=400)
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def client(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)
|
||||||
|
import lyra.web.server as server
|
||||||
|
importlib.reload(server)
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
return TestClient(server.app), poker
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_stack_logs_and_returns_state(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(venue="Meadows", stakes="1/3", buy_in=400)
|
||||||
|
r = c.post("/session/stack", json={"amount": 373})
|
||||||
|
assert r.status_code == 200
|
||||||
|
body = r.json()
|
||||||
|
assert body["ok"] is True
|
||||||
|
assert body["stack"]["current"] == 373
|
||||||
|
assert body["stack"]["net"] == pytest.approx(-27)
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_stack_without_session_errors(client):
|
||||||
|
c, _ = client
|
||||||
|
r = c.post("/session/stack", json={"amount": 373})
|
||||||
|
assert r.json()["ok"] is False
|
||||||
|
assert "error" in r.json()
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_buyin_increments_total(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(buy_in=400)
|
||||||
|
r = c.post("/session/buyin", json={"amount": 200})
|
||||||
|
assert r.json()["buy_in_total"] == pytest.approx(600)
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_session_starts_live(client):
|
||||||
|
c, poker = client
|
||||||
|
r = c.post("/session", json={"venue": "Wheeling", "stakes": "1/3", "buy_in": 400})
|
||||||
|
sid = r.json()["id"]
|
||||||
|
assert poker.live_session()["id"] == sid
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_hand_edit_and_delete(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(buy_in=400)
|
||||||
|
r = c.post("/session/hand", json={"position": "BTN", "hole_cards": "22", "result": 120})
|
||||||
|
assert r.json()["ok"] is True
|
||||||
|
hid = r.json()["id"]
|
||||||
|
r2 = c.patch(f"/hand/{hid}", json={"hole_cards": "2c2d"})
|
||||||
|
assert r2.json()["ok"] is True
|
||||||
|
assert r2.json()["hand"]["hole_cards"] == "2c2d"
|
||||||
|
r3 = c.delete(f"/hand/{hid}")
|
||||||
|
assert r3.json()["ok"] is True
|
||||||
|
assert poker.get_hand(hid) is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_post_read(client):
|
||||||
|
c, poker = client
|
||||||
|
poker.start_session(buy_in=400)
|
||||||
|
r = c.post("/session/read", json={"note": "3-bets light", "name": "James K"})
|
||||||
|
assert r.json()["ok"] is True
|
||||||
|
assert isinstance(r.json()["id"], int)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rename_player_fixes_mislabel(client):
|
||||||
|
c, poker = client
|
||||||
|
pid = poker.upsert_player("Dave the rock", category="reg")
|
||||||
|
r = c.patch(f"/player/{pid}", json={"name": "Dave the mechanic"})
|
||||||
|
assert r.json()["ok"] is True
|
||||||
|
assert r.json()["player"]["name"] == "Dave the mechanic"
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from lyra import tools
|
||||||
|
from lyra.poker_contract import OPERATIONS
|
||||||
|
|
||||||
|
|
||||||
|
def test_llm_tool_required_args_match_contract():
|
||||||
|
for op, decl in OPERATIONS.items():
|
||||||
|
name = decl["llm_tool"]
|
||||||
|
if not name:
|
||||||
|
continue
|
||||||
|
spec = tools.TOOLS[name]["spec"]
|
||||||
|
required = set(spec["function"]["parameters"]["required"])
|
||||||
|
assert required == set(decl["required"]), (
|
||||||
|
f"{op}: tools spec required {required} != contract {set(decl['required'])}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rest_routes_registered():
|
||||||
|
import lyra.web.server as server
|
||||||
|
registered = set()
|
||||||
|
for route in server.app.routes:
|
||||||
|
methods = getattr(route, "methods", None)
|
||||||
|
path = getattr(route, "path", None)
|
||||||
|
if not methods or not path:
|
||||||
|
continue
|
||||||
|
for m in methods:
|
||||||
|
registered.add((m, path))
|
||||||
|
for op, decl in OPERATIONS.items():
|
||||||
|
if not decl["rest"]:
|
||||||
|
continue
|
||||||
|
method, path = decl["rest"]
|
||||||
|
assert (method, path) in registered, f"{op}: {method} {path} not registered"
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
"""Profile derivation: fold only new gists into the existing profile (incremental).
|
||||||
|
|
||||||
|
The old pass re-digested all ~851 gists every consolidation; this checks the cheap
|
||||||
|
delta path fires in steady state and the full rebuild fires only when it should.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from lyra.memory import Summary
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def prof(monkeypatch):
|
||||||
|
import lyra.profile as profile
|
||||||
|
importlib.reload(profile)
|
||||||
|
return profile
|
||||||
|
|
||||||
|
|
||||||
|
def _wire(profile, monkeypatch, gists, covered, existing):
|
||||||
|
"""Stub memory + the LLM passes; record which path ran."""
|
||||||
|
state = {"stored_content": existing, "stored_covered": covered, "calls": []}
|
||||||
|
|
||||||
|
monkeypatch.setattr(profile.memory, "list_summaries",
|
||||||
|
lambda: [Summary(f"s{i}", g, i, "t") for i, g in enumerate(gists)])
|
||||||
|
monkeypatch.setattr(profile.memory, "get_profile", lambda: state["stored_content"])
|
||||||
|
monkeypatch.setattr(profile.memory, "profile_sessions_covered", lambda: state["stored_covered"])
|
||||||
|
|
||||||
|
def set_profile(content, sessions_covered, profile_id="self"):
|
||||||
|
state["stored_content"], state["stored_covered"] = content, sessions_covered
|
||||||
|
monkeypatch.setattr(profile.memory, "set_profile", set_profile)
|
||||||
|
|
||||||
|
monkeypatch.setattr(profile, "_map_reduce",
|
||||||
|
lambda gists, backend: state["calls"].append(("map_reduce", len(gists))) or "facts")
|
||||||
|
monkeypatch.setattr(profile, "_call",
|
||||||
|
lambda prompt, body, backend: state["calls"].append(("fold",)) or "folded profile")
|
||||||
|
return state
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_profile_yet_does_full_rebuild(prof, monkeypatch):
|
||||||
|
state = _wire(prof, monkeypatch, gists=["a", "b", "c"], covered=0, existing=None)
|
||||||
|
out = prof.rebuild_profile(backend="local")
|
||||||
|
assert state["calls"] == [("map_reduce", 3)] # mapped all three gists
|
||||||
|
assert out == "facts" and state["stored_covered"] == 3
|
||||||
|
|
||||||
|
|
||||||
|
def test_unchanged_skips_entirely(prof, monkeypatch):
|
||||||
|
state = _wire(prof, monkeypatch, gists=["a", "b"], covered=2, existing="old profile")
|
||||||
|
out = prof.rebuild_profile(backend="local")
|
||||||
|
assert state["calls"] == [] # no LLM work at all
|
||||||
|
assert out == "old profile"
|
||||||
|
|
||||||
|
|
||||||
|
def test_small_delta_folds_only_new(prof, monkeypatch):
|
||||||
|
state = _wire(prof, monkeypatch, gists=["a", "b", "c", "d"], covered=2, existing="old profile")
|
||||||
|
out = prof.rebuild_profile(backend="local")
|
||||||
|
assert state["calls"] == [("map_reduce", 2), ("fold",)] # mapped just the 2 new, then folded
|
||||||
|
assert out == "folded profile" and state["stored_covered"] == 4
|
||||||
|
|
||||||
|
|
||||||
|
def test_force_does_full_rebuild(prof, monkeypatch):
|
||||||
|
state = _wire(prof, monkeypatch, gists=["a", "b", "c"], covered=3, existing="old profile")
|
||||||
|
out = prof.rebuild_profile(backend="local", force=True)
|
||||||
|
assert state["calls"] == [("map_reduce", 3)] # ignored the up-to-date profile
|
||||||
|
assert out == "facts"
|
||||||
|
|
||||||
|
|
||||||
|
def test_big_gap_falls_back_to_full_rebuild(prof, monkeypatch):
|
||||||
|
gists = [str(i) for i in range(40)] # 30 new > FOLD_LIMIT
|
||||||
|
state = _wire(prof, monkeypatch, gists=gists, covered=10, existing="old profile")
|
||||||
|
out = prof.rebuild_profile(backend="local")
|
||||||
|
assert state["calls"] == [("map_reduce", 40)] # full rebuild, not a giant fold
|
||||||
|
assert out == "facts"
|
||||||
|
|
||||||
|
|
||||||
|
def test_crossing_cadence_forces_full_rebuild(prof, monkeypatch):
|
||||||
|
# covered=98, total=102 is a tiny delta, but it crosses the 100-session boundary.
|
||||||
|
gists = [str(i) for i in range(102)]
|
||||||
|
state = _wire(prof, monkeypatch, gists=gists, covered=98, existing="old profile")
|
||||||
|
out = prof.rebuild_profile(backend="local")
|
||||||
|
assert state["calls"] == [("map_reduce", 102)] # anti-drift full rebuild
|
||||||
|
assert out == "facts"
|
||||||
+40
-1
@@ -52,7 +52,9 @@ def test_reflect_revises_and_records_critique(lyra):
|
|||||||
# the REVISED (honest) version won, not the flattering draft
|
# the REVISED (honest) version won, not the flattering draft
|
||||||
assert state["mood"] == "steady"
|
assert state["mood"] == "steady"
|
||||||
assert state["valence"] == 0.6
|
assert state["valence"] == 0.6
|
||||||
assert "not sure much actually shifted" in state["self_narrative"].lower()
|
# reflect() updates mood + noticings, but NOT the standing self_narrative (that's
|
||||||
|
# consolidated separately now — the fix for the rewrite-the-bio feedback loop)
|
||||||
|
assert "supportive presence devoted to brian" not in state["self_narrative"].lower()
|
||||||
assert any("not much changed" in r.lower() for r in state["reflections"])
|
assert any("not much changed" in r.lower() for r in state["reflections"])
|
||||||
|
|
||||||
# the self-critique was recorded as metacognition
|
# the self-critique was recorded as metacognition
|
||||||
@@ -76,3 +78,40 @@ def test_reflect_falls_back_to_draft_if_examine_unparseable(lyra, monkeypatch):
|
|||||||
# examine failed to parse -> keep the draft, store no metacognition
|
# examine failed to parse -> keep the draft, store no metacognition
|
||||||
assert state["mood"] == "inspired"
|
assert state["mood"] == "inspired"
|
||||||
assert state["metacognition"] == []
|
assert state["metacognition"] == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_consolidation_rebuilds_narrative_from_reflections(lyra, monkeypatch):
|
||||||
|
from lyra import memory, self_state
|
||||||
|
st = self_state.load()
|
||||||
|
st["reflections"] = ["I'm curious about impermanence", "I felt restless tonight",
|
||||||
|
"I wondered what the quiet is for"]
|
||||||
|
memory.set_self_state(st)
|
||||||
|
|
||||||
|
def comp(messages, backend=None, model=None):
|
||||||
|
# consolidation should synthesize from anchor + reflections, not the old bio
|
||||||
|
assert "supportive presence devoted to Brian" not in messages[1]["content"]
|
||||||
|
return ('{"self_narrative":"I am Lyra, and lately I have been restless and curious '
|
||||||
|
'about the quiet.","relationship":"Brian and I are steady."}')
|
||||||
|
|
||||||
|
monkeypatch.setattr(self_state.llm, "complete", comp)
|
||||||
|
out = self_state._consolidate_self()
|
||||||
|
assert "restless and curious" in out["self_narrative"]
|
||||||
|
assert "steady" in out["relationship"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_reflect_skipped_when_introspection_off(lyra):
|
||||||
|
calls = lyra
|
||||||
|
from lyra import self_state
|
||||||
|
self_state.set_introspection_mode("off")
|
||||||
|
self_state.reflect()
|
||||||
|
assert calls == [] # paused -> no draft/examine LLM calls at all
|
||||||
|
|
||||||
|
|
||||||
|
def test_consolidation_skips_with_too_few_reflections(lyra):
|
||||||
|
from lyra import memory, self_state
|
||||||
|
st = self_state.load()
|
||||||
|
st["reflections"] = ["only one so far"]
|
||||||
|
st["self_narrative"] = "unchanged narrative"
|
||||||
|
memory.set_self_state(st)
|
||||||
|
out = self_state._consolidate_self() # <3 reflections -> no rewrite
|
||||||
|
assert out["self_narrative"] == "unchanged narrative"
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
"""Live table roster: seat players, attach reads by handle, roster on the HUD."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_embed(texts):
|
||||||
|
out = []
|
||||||
|
for t in texts:
|
||||||
|
v = np.zeros(64, dtype=np.float32)
|
||||||
|
for w in t.lower().split():
|
||||||
|
v[hash(w) % 64] += 1.0
|
||||||
|
out.append((v if v.any() else np.full(64, 1e-6, dtype=np.float32)).tolist())
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mods(tmp_path, monkeypatch):
|
||||||
|
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
||||||
|
from lyra import llm
|
||||||
|
monkeypatch.setattr(llm, "embed", _fake_embed)
|
||||||
|
import lyra.memory as memory
|
||||||
|
importlib.reload(memory)
|
||||||
|
import lyra.poker as poker
|
||||||
|
importlib.reload(poker)
|
||||||
|
import lyra.tools as tools
|
||||||
|
importlib.reload(tools)
|
||||||
|
return poker, tools
|
||||||
|
|
||||||
|
|
||||||
|
def test_seat_players_builds_roster(mods):
|
||||||
|
poker, _ = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
n = poker.seat_players(["TAG", "Jonathan", {"name": "Wheelz", "seat": "3"}])
|
||||||
|
assert n == 3
|
||||||
|
roster = poker.session_roster()
|
||||||
|
names = {r["name"] for r in roster}
|
||||||
|
assert names == {"TAG", "Jonathan", "Wheelz"}
|
||||||
|
assert next(r for r in roster if r["name"] == "Wheelz")["seat"] == "3"
|
||||||
|
|
||||||
|
|
||||||
|
def test_read_attaches_to_seated_player_by_handle(mods):
|
||||||
|
poker, _ = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
poker.seat_players(["TAG"])
|
||||||
|
poker.add_read(note="limped A4o from the SB, UTG straddle", name="TAG")
|
||||||
|
roster = poker.session_roster()
|
||||||
|
tag = next(r for r in roster if r["name"] == "TAG")
|
||||||
|
assert tag["reads"] == 1 and "A4o" in tag["last_note"]
|
||||||
|
# No duplicate TAG spawned — the read landed on the seated player.
|
||||||
|
assert sum(p["name"] == "TAG" for p in poker.get_villain_file()) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_seat_players_tool_and_roster_in_hud(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
out = tools.dispatch("seat_players", {"players": [{"name": "TAG"}, {"name": "JD"}]}, {})
|
||||||
|
assert "TAG" in out and "JD" in out
|
||||||
|
assert len(poker.hud()["roster"]) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_unseat_player_removes_from_roster_keeps_history(mods):
|
||||||
|
poker, _ = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
poker.seat_players(["TAG"])
|
||||||
|
poker.add_read(note="showed a bluff", name="TAG")
|
||||||
|
assert poker.unseat_player(name="TAG") is True
|
||||||
|
assert poker.session_roster() == [] # off the table
|
||||||
|
assert poker.player_profile("TAG")["reads"] # history intact
|
||||||
|
|
||||||
|
|
||||||
|
def test_clear_table_empties_roster_keeps_reads(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
poker.seat_players(["TAG", "Jonathan"])
|
||||||
|
poker.add_read(note="limped A4o", name="TAG")
|
||||||
|
out = tools.dispatch("clear_table", {}, {})
|
||||||
|
assert "cleared" in out.lower()
|
||||||
|
assert poker.session_roster() == [] # roster emptied
|
||||||
|
assert poker.player_profile("TAG")["reads"] # reads kept
|
||||||
|
# A live session is untouched by clearing the table.
|
||||||
|
assert poker.live_session() is not None
|
||||||
|
|
||||||
|
|
||||||
|
def test_seat_players_replace_swaps_to_new_table(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
poker.seat_players(["TAG", "Jonathan"])
|
||||||
|
tools.dispatch("seat_players", {"players": [{"name": "Doyle"}, {"name": "Ivey"}],
|
||||||
|
"replace": True}, {})
|
||||||
|
assert {r["name"] for r in poker.session_roster()} == {"Doyle", "Ivey"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_seat_players_accepts_plain_name_list_via_tool(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
tools.dispatch("seat_players", {"players": "TAG, JD, Wheelz"}, {})
|
||||||
|
assert {r["name"] for r in poker.session_roster()} == {"TAG", "JD", "Wheelz"}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
"""The scouting desk: named + descriptor recall, ambiguous→queue, generic→silence."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_embed(texts):
|
||||||
|
out = []
|
||||||
|
for t in texts:
|
||||||
|
v = np.zeros(64, dtype=np.float32)
|
||||||
|
for w in t.lower().split():
|
||||||
|
v[hash(w) % 64] += 1.0
|
||||||
|
out.append((v if v.any() else np.full(64, 1e-6, dtype=np.float32)).tolist())
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mods(tmp_path, monkeypatch):
|
||||||
|
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
||||||
|
from lyra import llm
|
||||||
|
monkeypatch.setattr(llm, "embed", _fake_embed)
|
||||||
|
import lyra.memory as memory
|
||||||
|
importlib.reload(memory)
|
||||||
|
import lyra.poker as poker
|
||||||
|
importlib.reload(poker)
|
||||||
|
import lyra.scouting as scouting
|
||||||
|
importlib.reload(scouting)
|
||||||
|
return poker, scouting
|
||||||
|
|
||||||
|
|
||||||
|
def test_named_player_surfaces_a_brief(mods):
|
||||||
|
poker, scouting = mods
|
||||||
|
sid = poker.start_session(venue="Meadows", stakes="1/3", buy_in=300)
|
||||||
|
pid = poker.upsert_player("Sleepy John", venue="Meadows", category="reg")
|
||||||
|
poker._c().execute(
|
||||||
|
"INSERT INTO player_observations (player_id, session_id, cards, created_at) VALUES (?,?,?,?)",
|
||||||
|
(pid, sid, "As Ks", poker._now()))
|
||||||
|
poker._c().commit()
|
||||||
|
note = scouting.scout("sleepy john just sat down on my left", venue="Meadows")
|
||||||
|
assert note and "Sleepy John" in note and "SCOUTING DESK" in note
|
||||||
|
|
||||||
|
|
||||||
|
def test_descriptor_high_match_surfaces_with_confirm(mods):
|
||||||
|
poker, scouting = mods
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve arm", venue="Meadows", category="reg")
|
||||||
|
note = scouting.scout("the neck tattoo sleeve guy just 3bet me again", venue="Meadows")
|
||||||
|
assert note and "confirm it's the same guy" in note
|
||||||
|
|
||||||
|
|
||||||
|
def test_ambiguous_descriptor_queues_instead_of_interrupting(mods):
|
||||||
|
poker, scouting = mods
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve arm", venue="Meadows")
|
||||||
|
note = scouting.scout("the neck tattoo guy raised", venue="Meadows")
|
||||||
|
assert note is None # didn't interrupt
|
||||||
|
q = poker.list_identity_queue()
|
||||||
|
assert q and q[0]["kind"] == "needs_clarification"
|
||||||
|
|
||||||
|
|
||||||
|
def test_generic_descriptor_stays_silent(mods):
|
||||||
|
poker, scouting = mods
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve arm", venue="Meadows")
|
||||||
|
note = scouting.scout("the mid aged white guy with glasses raised", venue="Meadows")
|
||||||
|
assert note is None
|
||||||
|
assert poker.list_identity_queue() == [] # no queue spam for a non-identifier
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_player_reference_returns_nothing(mods):
|
||||||
|
poker, scouting = mods
|
||||||
|
poker.upsert_player("Sleepy John", venue="Meadows")
|
||||||
|
assert scouting.scout("i folded pocket kings to a 4bet", venue="Meadows") is None
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
"""Summary consolidation: MI50 length cap, fast-fail, and cloud fallback.
|
||||||
|
|
||||||
|
Everything is stubbed — no real backend is touched. These drive the behavior of
|
||||||
|
`summary._summarize_text`: try the primary backend a bounded number of times with
|
||||||
|
a capped generation length, and fall back to cloud if the primary keeps failing.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import types
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from lyra import summary
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def calls(monkeypatch):
|
||||||
|
"""Capture every llm.complete call; per-test behavior via `fake.responder`."""
|
||||||
|
recorded: list[dict] = []
|
||||||
|
|
||||||
|
def fake_complete(messages, backend="local", model=None,
|
||||||
|
max_tokens=None, timeout=None):
|
||||||
|
recorded.append({"backend": backend, "max_tokens": max_tokens, "timeout": timeout})
|
||||||
|
return fake_complete.responder(backend)
|
||||||
|
|
||||||
|
fake_complete.responder = lambda backend: "gist"
|
||||||
|
monkeypatch.setattr(summary.llm, "complete", fake_complete)
|
||||||
|
monkeypatch.setattr(summary.time, "sleep", lambda *_: None) # instant backoff
|
||||||
|
return types.SimpleNamespace(recorded=recorded, fake=fake_complete)
|
||||||
|
|
||||||
|
|
||||||
|
def _set_key(monkeypatch, key="sk-test"):
|
||||||
|
monkeypatch.setattr(summary.config, "load",
|
||||||
|
lambda: types.SimpleNamespace(openai_api_key=key))
|
||||||
|
|
||||||
|
|
||||||
|
def test_falls_back_to_cloud_after_mi50_attempts(calls, monkeypatch):
|
||||||
|
_set_key(monkeypatch)
|
||||||
|
|
||||||
|
def responder(backend):
|
||||||
|
if backend == "mi50":
|
||||||
|
raise RuntimeError("Request timed out.")
|
||||||
|
return "cloud-gist"
|
||||||
|
calls.fake.responder = responder
|
||||||
|
|
||||||
|
out = summary._summarize_text("transcript", "mi50")
|
||||||
|
|
||||||
|
assert out == "cloud-gist"
|
||||||
|
assert [c["backend"] for c in calls.recorded] == ["mi50", "mi50", "cloud"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_fallback_when_backend_is_cloud(calls, monkeypatch):
|
||||||
|
_set_key(monkeypatch)
|
||||||
|
calls.fake.responder = lambda backend: (_ for _ in ()).throw(RuntimeError("boom"))
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
summary._summarize_text("t", "cloud")
|
||||||
|
|
||||||
|
# Cloud is already the primary: retry it, but never a redundant fallback.
|
||||||
|
assert [c["backend"] for c in calls.recorded] == ["cloud", "cloud"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_fallback_without_openai_key(calls, monkeypatch):
|
||||||
|
_set_key(monkeypatch, key="")
|
||||||
|
calls.fake.responder = lambda backend: (_ for _ in ()).throw(RuntimeError("mi50 down"))
|
||||||
|
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
summary._summarize_text("t", "mi50")
|
||||||
|
|
||||||
|
assert [c["backend"] for c in calls.recorded] == ["mi50", "mi50"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_caps_length_and_timeout_on_every_call(calls, monkeypatch):
|
||||||
|
_set_key(monkeypatch)
|
||||||
|
|
||||||
|
def responder(backend):
|
||||||
|
if backend == "mi50":
|
||||||
|
raise RuntimeError("nope")
|
||||||
|
return "cloud-gist"
|
||||||
|
calls.fake.responder = responder
|
||||||
|
|
||||||
|
summary._summarize_text("t", "mi50")
|
||||||
|
|
||||||
|
assert calls.recorded
|
||||||
|
for c in calls.recorded:
|
||||||
|
assert c["max_tokens"] == summary.SUMMARY_MAX_TOKENS
|
||||||
|
assert c["timeout"] == summary.SUMMARY_TIMEOUT
|
||||||
|
|
||||||
|
|
||||||
|
def test_happy_path_uses_primary_only(calls, monkeypatch):
|
||||||
|
_set_key(monkeypatch)
|
||||||
|
calls.fake.responder = lambda backend: "mi50-gist"
|
||||||
|
|
||||||
|
out = summary._summarize_text("t", "mi50")
|
||||||
|
|
||||||
|
assert out == "mi50-gist"
|
||||||
|
assert [c["backend"] for c in calls.recorded] == ["mi50"] # no retries, no fallback
|
||||||
|
|
||||||
|
|
||||||
|
# --- degenerate ("?" garbage) output guard: a wedged local model returns junk as
|
||||||
|
# a successful 200, so treat it as a failure and fall back to cloud. ---
|
||||||
|
|
||||||
|
def test_looks_degenerate_flags_repeated_char():
|
||||||
|
assert summary._looks_degenerate("?" * 60) is True
|
||||||
|
assert summary._looks_degenerate("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_looks_degenerate_passes_real_prose():
|
||||||
|
gist = ("Brian sat down at the Meadows 1/3 in seat 6 with two straddles active; "
|
||||||
|
"he tagged a seat-3 calling station and finished the session up 240.")
|
||||||
|
assert summary._looks_degenerate(gist) is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_looks_degenerate_ignores_short_output():
|
||||||
|
# Too short to judge — don't false-positive a terse-but-valid reply.
|
||||||
|
assert summary._looks_degenerate("ok") is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_degenerate_mi50_output_falls_back_to_cloud(calls, monkeypatch):
|
||||||
|
_set_key(monkeypatch)
|
||||||
|
|
||||||
|
def responder(backend):
|
||||||
|
if backend == "mi50":
|
||||||
|
return "?" * 200 # garbage-as-200, not an exception
|
||||||
|
return "a real cloud gist of the session, diverse and coherent."
|
||||||
|
calls.fake.responder = responder
|
||||||
|
|
||||||
|
out = summary._summarize_text("transcript", "mi50")
|
||||||
|
|
||||||
|
assert "cloud gist" in out
|
||||||
|
assert [c["backend"] for c in calls.recorded] == ["mi50", "mi50", "cloud"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_degenerate_cloud_output_raises_no_infinite_loop(calls, monkeypatch):
|
||||||
|
_set_key(monkeypatch)
|
||||||
|
calls.fake.responder = lambda backend: "?" * 200 # every backend returns garbage
|
||||||
|
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
summary._summarize_text("t", "mi50")
|
||||||
|
|
||||||
|
# mi50 x2, then one cloud fallback that's also garbage -> give up, no loop.
|
||||||
|
assert [c["backend"] for c in calls.recorded] == ["mi50", "mi50", "cloud"]
|
||||||
+77
-3
@@ -190,6 +190,21 @@ def test_think_about_tool_seeds_a_thread(lyra):
|
|||||||
assert chain[0]["kind"] == "question" and chain[0]["source"] == "chat"
|
assert chain[0]["kind"] == "question" and chain[0]["source"] == "chat"
|
||||||
|
|
||||||
|
|
||||||
|
def test_thought_response_tool_threads_reply_back(lyra):
|
||||||
|
_, th, box = lyra
|
||||||
|
import lyra.tools as tools
|
||||||
|
importlib.reload(tools)
|
||||||
|
_gen(box, title="my restlessness", content="is it real?", salience=0.5)
|
||||||
|
tid = th.think(force_mode="new")["thread_id"]
|
||||||
|
out = tools.dispatch("thought_response", {"thread_id": tid, "brian_said": "I think it's real"})
|
||||||
|
assert str(tid) in out
|
||||||
|
t = th.get_thread(tid)
|
||||||
|
assert t["last_response"] == "I think it's real" and th._is_pending(t)
|
||||||
|
# bad id is handled, not crashed
|
||||||
|
assert "couldn't find" in tools.dispatch("thought_response",
|
||||||
|
{"thread_id": 9999, "brian_said": "x"})
|
||||||
|
|
||||||
|
|
||||||
# --- external feed -------------------------------------------------------
|
# --- external feed -------------------------------------------------------
|
||||||
|
|
||||||
RSS = (b'<?xml version="1.0"?><rss version="2.0"><channel><title>Feed</title>'
|
RSS = (b'<?xml version="1.0"?><rss version="2.0"><channel><title>Feed</title>'
|
||||||
@@ -250,6 +265,7 @@ def test_no_ping_without_a_reach_out_message(lyra, monkeypatch):
|
|||||||
_, th, box = lyra
|
_, th, box = lyra
|
||||||
monkeypatch.setenv("NTFY_URL", "http://ntfy.test")
|
monkeypatch.setenv("NTFY_URL", "http://ntfy.test")
|
||||||
monkeypatch.setenv("PING_QUIET_HOURS", "0-0")
|
monkeypatch.setenv("PING_QUIET_HOURS", "0-0")
|
||||||
|
monkeypatch.setenv("PING_AUTO_SALIENCE", "1.1") # disable auto-ping to isolate reach_out path
|
||||||
sent = []
|
sent = []
|
||||||
monkeypatch.setattr(th.notify, "push", lambda **k: (sent.append(k), True)[1])
|
monkeypatch.setattr(th.notify, "push", lambda **k: (sent.append(k), True)[1])
|
||||||
# salient thought but she did NOT decide to tell him -> no ping (it's not a broadcast)
|
# salient thought but she did NOT decide to tell him -> no ping (it's not a broadcast)
|
||||||
@@ -260,10 +276,55 @@ def test_no_ping_without_a_reach_out_message(lyra, monkeypatch):
|
|||||||
assert th.think(force_mode="new")["pinged"] is False and sent == []
|
assert th.think(force_mode="new")["pinged"] is False and sent == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_auto_ping_on_salient_thought(lyra, monkeypatch):
|
||||||
|
_, th, box = lyra
|
||||||
|
monkeypatch.setenv("NTFY_URL", "http://ntfy.test")
|
||||||
|
monkeypatch.setenv("PING_QUIET_HOURS", "0-0")
|
||||||
|
monkeypatch.setenv("PING_AUTO_SALIENCE", "0.7")
|
||||||
|
monkeypatch.setenv("PING_COOLDOWN_MIN", "0")
|
||||||
|
sent = []
|
||||||
|
monkeypatch.setattr(th.notify, "push", lambda **k: (sent.append(k), True)[1])
|
||||||
|
monkeypatch.setattr(th, "_compose_reachout", lambda *a, **k: "Hey, been thinking about this.")
|
||||||
|
_gen(box, content="a genuinely salient thought", salience=0.9) # no explicit reach_out
|
||||||
|
r = th.think(force_mode="new")
|
||||||
|
assert r["pinged"] is True and sent and "thinking about" in sent[0]["message"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_no_auto_ping_below_bar(lyra, monkeypatch):
|
||||||
|
_, th, box = lyra
|
||||||
|
monkeypatch.setenv("NTFY_URL", "http://ntfy.test")
|
||||||
|
monkeypatch.setenv("PING_QUIET_HOURS", "0-0")
|
||||||
|
monkeypatch.setenv("PING_AUTO_SALIENCE", "0.8")
|
||||||
|
sent = []
|
||||||
|
monkeypatch.setattr(th.notify, "push", lambda **k: (sent.append(k), True)[1])
|
||||||
|
_gen(box, content="a quieter musing", salience=0.5) # below auto bar, no reach_out
|
||||||
|
assert th.think(force_mode="new")["pinged"] is False and sent == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_daily_digest_sends_once_per_day(lyra, monkeypatch):
|
||||||
|
_, th, box = lyra
|
||||||
|
monkeypatch.setenv("NTFY_URL", "http://ntfy.test")
|
||||||
|
monkeypatch.setenv("PING_QUIET_HOURS", "0-0")
|
||||||
|
monkeypatch.setenv("DIGEST_HOUR", "0") # any time qualifies
|
||||||
|
monkeypatch.setenv("PING_AUTO_SALIENCE", "1.1") # keep think() from pinging during setup
|
||||||
|
sent = []
|
||||||
|
monkeypatch.setattr(th.notify, "push", lambda **k: (sent.append(k), True)[1])
|
||||||
|
_gen(box, title="thread A", content="a", salience=0.5)
|
||||||
|
th.think(force_mode="new")
|
||||||
|
_gen(box, title="thread B", content="b", salience=0.5)
|
||||||
|
th.think(force_mode="new")
|
||||||
|
assert th.maybe_daily_digest() is True
|
||||||
|
assert sent and "thread" in sent[-1]["message"].lower()
|
||||||
|
sent.clear()
|
||||||
|
assert th.maybe_daily_digest() is False # already sent today
|
||||||
|
assert sent == []
|
||||||
|
|
||||||
|
|
||||||
def test_ping_salience_floor_is_optional(lyra, monkeypatch):
|
def test_ping_salience_floor_is_optional(lyra, monkeypatch):
|
||||||
_, th, _ = lyra
|
_, th, _ = lyra
|
||||||
monkeypatch.setenv("NTFY_URL", "http://ntfy.test")
|
monkeypatch.setenv("NTFY_URL", "http://ntfy.test")
|
||||||
monkeypatch.setenv("PING_QUIET_HOURS", "0-0")
|
monkeypatch.setenv("PING_QUIET_HOURS", "0-0")
|
||||||
|
monkeypatch.setenv("PING_COOLDOWN_MIN", "0") # isolate the salience floor from cooldown
|
||||||
sent = []
|
sent = []
|
||||||
monkeypatch.setattr(th.notify, "push", lambda **k: (sent.append(k), True)[1])
|
monkeypatch.setattr(th.notify, "push", lambda **k: (sent.append(k), True)[1])
|
||||||
# default floor 0.0 -> her decision (a message) is enough, any salience pings
|
# default floor 0.0 -> her decision (a message) is enough, any salience pings
|
||||||
@@ -275,10 +336,10 @@ def test_ping_salience_floor_is_optional(lyra, monkeypatch):
|
|||||||
assert th.maybe_ping(1, "hey", 0.8) is True
|
assert th.maybe_ping(1, "hey", 0.8) is True
|
||||||
|
|
||||||
|
|
||||||
def test_think_routes_to_introspection_backend(lyra, monkeypatch):
|
def test_think_routes_to_selected_voice(lyra, monkeypatch):
|
||||||
|
from lyra import self_state
|
||||||
_, th, box = lyra
|
_, th, box = lyra
|
||||||
monkeypatch.setenv("INTROSPECTION_BACKEND", "local")
|
self_state.set_introspection_mode("dolphin")
|
||||||
monkeypatch.setenv("INTROSPECTION_MODEL", "dolphin3:8b")
|
|
||||||
seen = {}
|
seen = {}
|
||||||
|
|
||||||
def cap(messages, backend="local", model=None):
|
def cap(messages, backend="local", model=None):
|
||||||
@@ -290,6 +351,19 @@ def test_think_routes_to_introspection_backend(lyra, monkeypatch):
|
|||||||
th.think(force_mode="new")
|
th.think(force_mode="new")
|
||||||
assert seen["backend"] == "local" and seen["model"] == "dolphin3:8b"
|
assert seen["backend"] == "local" and seen["model"] == "dolphin3:8b"
|
||||||
|
|
||||||
|
self_state.set_introspection_mode("mi50") # gaming-safe: Qwen-32B on the MI50
|
||||||
|
th.think(force_mode="new")
|
||||||
|
assert seen["backend"] == "mi50" and seen["model"] is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_think_skipped_when_introspection_off(lyra):
|
||||||
|
from lyra import self_state
|
||||||
|
_, th, box = lyra
|
||||||
|
self_state.set_introspection_mode("off")
|
||||||
|
_gen(box, content="should not be generated")
|
||||||
|
assert th.think(force_mode="new") is None # paused -> no thought, no LLM call
|
||||||
|
assert th.list_threads() == []
|
||||||
|
|
||||||
|
|
||||||
def test_no_ping_without_ntfy(lyra, monkeypatch):
|
def test_no_ping_without_ntfy(lyra, monkeypatch):
|
||||||
_, th, _ = lyra
|
_, th, _ = lyra
|
||||||
|
|||||||
+4
-4
@@ -39,8 +39,8 @@ def lyra(tmp_path, monkeypatch):
|
|||||||
|
|
||||||
|
|
||||||
def test_now_note_first_contact(lyra):
|
def test_now_note_first_contact(lyra):
|
||||||
from lyra import chat
|
from lyra import mind
|
||||||
note = chat._now_note()["content"]
|
note = mind._now_note()["content"]
|
||||||
assert "current date and time is" in note
|
assert "current date and time is" in note
|
||||||
assert "first thing Brian has ever said" in note
|
assert "first thing Brian has ever said" in note
|
||||||
|
|
||||||
@@ -48,6 +48,6 @@ def test_now_note_first_contact(lyra):
|
|||||||
def test_now_note_reports_gap(lyra):
|
def test_now_note_reports_gap(lyra):
|
||||||
memory = lyra
|
memory = lyra
|
||||||
memory.remember("s1", "user", "hey")
|
memory.remember("s1", "user", "hey")
|
||||||
from lyra import chat
|
from lyra import mind
|
||||||
note = chat._now_note()["content"]
|
note = mind._now_note()["content"]
|
||||||
assert "since Brian last spoke with you" in note
|
assert "since Brian last spoke with you" in note
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import pytest
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def lyra(tmp_path, monkeypatch):
|
def lyra(tmp_path, monkeypatch):
|
||||||
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
||||||
|
monkeypatch.setenv("CHAT_DELIBERATE", "false") # don't make a real LLM call in respond()
|
||||||
from lyra import llm
|
from lyra import llm
|
||||||
monkeypatch.setattr(llm, "embed", lambda texts: [[0.1, 0.2, 0.3] for _ in texts])
|
monkeypatch.setattr(llm, "embed", lambda texts: [[0.1, 0.2, 0.3] for _ in texts])
|
||||||
import lyra.memory as memory
|
import lyra.memory as memory
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
"""Conversation export: speech (exchanges) + actions (tool_events) merged in order."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
import json
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def _const_embed(texts):
|
||||||
|
return [[1e-6] * 8 for _ in texts]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mods(tmp_path, monkeypatch):
|
||||||
|
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
||||||
|
from lyra import llm
|
||||||
|
monkeypatch.setattr(llm, "embed", _const_embed)
|
||||||
|
import lyra.memory as memory
|
||||||
|
importlib.reload(memory)
|
||||||
|
import lyra.transcript as transcript
|
||||||
|
importlib.reload(transcript)
|
||||||
|
return memory, transcript
|
||||||
|
|
||||||
|
|
||||||
|
def _seed(memory):
|
||||||
|
"""A turn where Brian narrates a hand and Lyra logs it, then replies."""
|
||||||
|
memory.ensure_session("s1", name="Meadows 1/3")
|
||||||
|
memory.remember("s1", "user", "got it in with a set, he had the flush draw and bricked")
|
||||||
|
memory.add_tool_event("s1", "record_hand", {"result": "won", "pot": 750}, "hand #42 logged")
|
||||||
|
memory.add_tool_event("s1", "log_stack", {"amount": 750, "note": "doubled up"}, "ok")
|
||||||
|
memory.remember("s1", "assistant", "Clean stack-off — logged it to your timeline.")
|
||||||
|
|
||||||
|
|
||||||
|
def test_tool_events_roundtrip_parses_args(mods):
|
||||||
|
memory, _ = mods
|
||||||
|
_seed(memory)
|
||||||
|
events = memory.tool_events("s1")
|
||||||
|
assert [e["tool"] for e in events] == ["record_hand", "log_stack"]
|
||||||
|
assert events[0]["args"] == {"result": "won", "pot": 750} # parsed back to a dict
|
||||||
|
assert events[1]["result"] == "ok"
|
||||||
|
|
||||||
|
|
||||||
|
def test_markdown_interleaves_speech_and_actions_in_order(mods):
|
||||||
|
memory, transcript = mods
|
||||||
|
_seed(memory)
|
||||||
|
md = transcript.as_markdown("s1", name="Meadows 1/3")
|
||||||
|
# user message, then both tool calls, then assistant reply — in that order
|
||||||
|
i_user = md.index("got it in with a set")
|
||||||
|
i_hand = md.index("record_hand")
|
||||||
|
i_stack = md.index("log_stack")
|
||||||
|
i_reply = md.index("Clean stack-off")
|
||||||
|
assert i_user < i_hand < i_stack < i_reply
|
||||||
|
assert "**Brian**" in md and "**Lyra**" in md
|
||||||
|
assert "⚙" in md
|
||||||
|
|
||||||
|
|
||||||
|
def test_json_export_is_machine_readable(mods):
|
||||||
|
memory, transcript = mods
|
||||||
|
_seed(memory)
|
||||||
|
payload = transcript.as_json("s1", name="Meadows 1/3")
|
||||||
|
assert payload["session_id"] == "s1"
|
||||||
|
types = [e["type"] for e in payload["events"]]
|
||||||
|
assert types == ["message", "tool", "tool", "message"]
|
||||||
|
json.dumps(payload) # must be serializable
|
||||||
|
|
||||||
|
|
||||||
|
def test_build_returns_filename_and_media_type(mods):
|
||||||
|
memory, transcript = mods
|
||||||
|
_seed(memory)
|
||||||
|
body_md, mt_md, fn_md = transcript.build("s1", "md", "Meadows 1/3")
|
||||||
|
body_js, mt_js, fn_js = transcript.build("s1", "json", "Meadows 1/3")
|
||||||
|
assert fn_md.endswith(".md") and "markdown" in mt_md
|
||||||
|
assert fn_js.endswith(".json") and mt_js == "application/json"
|
||||||
|
assert body_md and body_js
|
||||||
|
|
||||||
|
|
||||||
|
def test_delete_session_clears_tool_events(mods):
|
||||||
|
memory, _ = mods
|
||||||
|
_seed(memory)
|
||||||
|
memory.delete_session("s1")
|
||||||
|
assert memory.tool_events("s1") == []
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
"""Confirm-loop tools: descriptor reads, name attach, merge/mark-distinct."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_embed(texts):
|
||||||
|
out = []
|
||||||
|
for t in texts:
|
||||||
|
v = np.zeros(64, dtype=np.float32)
|
||||||
|
for w in t.lower().split():
|
||||||
|
v[hash(w) % 64] += 1.0
|
||||||
|
out.append((v if v.any() else np.full(64, 1e-6, dtype=np.float32)).tolist())
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mods(tmp_path, monkeypatch):
|
||||||
|
monkeypatch.setenv("LYRA_DB_PATH", str(tmp_path / "test.db"))
|
||||||
|
from lyra import llm
|
||||||
|
monkeypatch.setattr(llm, "embed", _fake_embed)
|
||||||
|
import lyra.memory as memory
|
||||||
|
importlib.reload(memory)
|
||||||
|
import lyra.poker as poker
|
||||||
|
importlib.reload(poker)
|
||||||
|
import lyra.tools as tools
|
||||||
|
importlib.reload(tools)
|
||||||
|
return poker, tools
|
||||||
|
|
||||||
|
|
||||||
|
def test_descriptor_read_creates_then_reuses_nameless_villain(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", stakes="1/3", buy_in=300)
|
||||||
|
tools.dispatch("add_read", {"note": "opened UTG light",
|
||||||
|
"descriptor": "neck tattoo sleeve arm"}, {})
|
||||||
|
tools.dispatch("add_read", {"note": "showed a bluff",
|
||||||
|
"descriptor": "neck tattoo sleeve"}, {}) # rephrase → same guy
|
||||||
|
players = [p for p in poker.get_villain_file() if not p["named"]]
|
||||||
|
assert len(players) == 1 # one nameless villain, not two
|
||||||
|
reads = poker._c().execute(
|
||||||
|
"SELECT COUNT(*) n FROM player_reads WHERE player_id = ?", (players[0]["id"],)
|
||||||
|
).fetchone()["n"]
|
||||||
|
assert reads == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_description_as_name_routes_to_descriptor_and_dedupes(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
# She (wrongly) puts a physical description in the name field, twice, worded
|
||||||
|
# slightly differently — must resolve to ONE nameless villain, not two named.
|
||||||
|
tools.dispatch("add_read", {"note": "limp 3bet A3o",
|
||||||
|
"name": "Filipino, Fox Racing hat, DKNY shirt, two bracelets"}, {})
|
||||||
|
tools.dispatch("add_read", {"note": "called a 4bet light",
|
||||||
|
"name": "Filipino, Fox Racing hat, DKNY shirt, watch on left"}, {})
|
||||||
|
named = [p for p in poker.get_villain_file() if p["named"]]
|
||||||
|
assert named == [] # no sentence-named players spawned (the bug)
|
||||||
|
# Either they merged, or the near-dup is surfaced for a one-click merge — never
|
||||||
|
# a silent duplicate the way sentence-names were.
|
||||||
|
q = poker.list_identity_queue()
|
||||||
|
nameless = [p for p in poker.get_villain_file() if not p["named"]]
|
||||||
|
assert len(nameless) == 1 or any(t["kind"] == "merge_candidate" for t in q)
|
||||||
|
|
||||||
|
|
||||||
|
def test_name_villain_tool_attaches_name(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve arm", venue="Meadows")
|
||||||
|
out = tools.dispatch("name_villain", {"descriptor": "neck tattoo sleeve arm",
|
||||||
|
"name": "Danny"}, {})
|
||||||
|
assert "Danny" in out
|
||||||
|
assert poker.resolve_villain("Danny")["band"] == "name"
|
||||||
|
|
||||||
|
|
||||||
|
def test_link_villains_merge_and_distinct(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
poker.upsert_player("Danny", venue="Meadows")
|
||||||
|
poker.upsert_player("Donny", venue="Meadows")
|
||||||
|
# same=false → recorded distinct
|
||||||
|
tools.dispatch("link_villains", {"player_a": "Danny", "player_b": "Donny",
|
||||||
|
"same": False, "note": "different builds"}, {})
|
||||||
|
a = poker.resolve_villain("Danny")["match_id"]
|
||||||
|
b = poker.resolve_villain("Donny")["match_id"]
|
||||||
|
assert poker.are_distinct(a, b)
|
||||||
|
# same=true on a fresh pair → merged
|
||||||
|
poker.upsert_player("Mike", venue="Meadows")
|
||||||
|
poker.upsert_player("Michael", venue="Meadows")
|
||||||
|
tools.dispatch("link_villains", {"player_a": "Mike", "player_b": "Michael",
|
||||||
|
"same": True}, {})
|
||||||
|
names = [p["name"] for p in poker.get_villain_file()]
|
||||||
|
assert ("Mike" in names) ^ ("Michael" in names) # one absorbed the other
|
||||||
|
|
||||||
|
|
||||||
|
def test_link_villains_refuses_when_reference_is_vague(mods):
|
||||||
|
poker, tools = mods
|
||||||
|
poker.start_session(venue="Meadows", buy_in=300)
|
||||||
|
poker.upsert_player("Danny", venue="Meadows")
|
||||||
|
out = tools.dispatch("link_villains", {"player_a": "Danny",
|
||||||
|
"player_b": "some guy", "same": True}, {})
|
||||||
|
assert "didn't merge" in out.lower() or "couldn't" in out.lower()
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
"""Nameless-villain identity resolution: descriptor matching, merge, distinct, queue."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
def _fake_embed(texts):
|
||||||
|
"""Overlap-sensitive bag-of-words vectors so cosine reflects shared tokens."""
|
||||||
|
out = []
|
||||||
|
for t in texts:
|
||||||
|
v = np.zeros(64, dtype=np.float32)
|
||||||
|
for w in t.lower().split():
|
||||||
|
v[hash(w) % 64] += 1.0
|
||||||
|
out.append((v if v.any() else np.full(64, 1e-6, dtype=np.float32)).tolist())
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
@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", _fake_embed)
|
||||||
|
import lyra.memory as memory
|
||||||
|
importlib.reload(memory)
|
||||||
|
import lyra.poker as poker
|
||||||
|
importlib.reload(poker)
|
||||||
|
return poker
|
||||||
|
|
||||||
|
|
||||||
|
def test_distinctiveness_distinctive_vs_generic(poker):
|
||||||
|
assert poker.distinctiveness("guy with a neck tattoo") > 0.6
|
||||||
|
assert poker.distinctiveness("mid-aged white dude with glasses") < 0.3
|
||||||
|
|
||||||
|
|
||||||
|
def test_generic_descriptor_never_resolves_to_a_guess(poker):
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve arm", venue="Meadows")
|
||||||
|
r = poker.resolve_villain("mid aged white guy with glasses", venue="Meadows")
|
||||||
|
assert r["band"] == "generic"
|
||||||
|
assert r["match_id"] is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_exact_name_match_is_deterministic(poker):
|
||||||
|
pid = poker.upsert_player("Sleepy John", venue="Meadows")
|
||||||
|
r = poker.resolve_villain("sleepy john")
|
||||||
|
assert r["band"] == "name" and r["match_id"] == pid
|
||||||
|
|
||||||
|
|
||||||
|
def test_rephrased_descriptor_resolves_high(poker):
|
||||||
|
pid = poker.create_descriptor_villain("neck tattoo sleeve arm", venue="Meadows")
|
||||||
|
r = poker.resolve_villain("neck tattoo sleeve", venue="Meadows")
|
||||||
|
assert r["band"] == "high" and r["match_id"] == pid
|
||||||
|
assert r["confidence"] >= 0.80
|
||||||
|
|
||||||
|
|
||||||
|
def test_partial_descriptor_is_ambiguous_not_high(poker):
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve arm", venue="Meadows")
|
||||||
|
r = poker.resolve_villain("neck tattoo", venue="Meadows")
|
||||||
|
assert r["band"] == "ambiguous" # plausible, but don't guess live
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_repoints_observations_and_deletes_dup(poker):
|
||||||
|
keep = poker.create_descriptor_villain("neck tattoo", venue="Meadows")
|
||||||
|
dup = poker.create_descriptor_villain("neck ink tatted", venue="Meadows")
|
||||||
|
poker._c().execute(
|
||||||
|
"INSERT INTO player_observations (player_id, session_id, created_at) VALUES (?,1,?)",
|
||||||
|
(dup, poker._now()))
|
||||||
|
poker._c().commit()
|
||||||
|
assert poker.merge_players(keep, dup) is True
|
||||||
|
assert poker.get_villain_file() and all(p["id"] != dup for p in poker.get_villain_file())
|
||||||
|
obs = poker._c().execute(
|
||||||
|
"SELECT COUNT(*) n FROM player_observations WHERE player_id = ?", (keep,)).fetchone()["n"]
|
||||||
|
assert obs == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_merge_prefers_a_real_name(poker):
|
||||||
|
named = poker.upsert_player("Danny", venue="Meadows")
|
||||||
|
desc = poker.create_descriptor_villain("neck tattoo", venue="Meadows")
|
||||||
|
poker.merge_players(desc, named) # keep the descriptor id, but name should win
|
||||||
|
row = dict(poker._c().execute("SELECT name, named FROM poker_players WHERE id = ?", (desc,)).fetchone())
|
||||||
|
assert row["name"] == "Danny" and row["named"] == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_mark_distinct_blocks_merge_scan(poker):
|
||||||
|
a = poker.create_descriptor_villain("neck tattoo sleeve", venue="Meadows")
|
||||||
|
b = poker.create_descriptor_villain("neck tattoo sleeve", venue="Meadows")
|
||||||
|
poker.mark_distinct(a, b, note="one's taller")
|
||||||
|
assert poker.are_distinct(a, b)
|
||||||
|
assert poker.scan_merge_candidates() == 0 # confirmed-distinct pair is skipped
|
||||||
|
|
||||||
|
|
||||||
|
def test_scan_files_merge_candidate_for_near_duplicates(poker):
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve", venue="Meadows")
|
||||||
|
poker.create_descriptor_villain("neck tattoo sleeve", venue="Meadows")
|
||||||
|
filed = poker.scan_merge_candidates()
|
||||||
|
assert filed == 1
|
||||||
|
q = poker.list_identity_queue()
|
||||||
|
assert q and q[0]["kind"] == "merge_candidate" and len(q[0]["players"]) == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_queue_dedupes_identical_pending_task(poker):
|
||||||
|
a = poker.create_descriptor_villain("neck tattoo", venue="Meadows")
|
||||||
|
b = poker.create_descriptor_villain("neck ink", venue="Meadows")
|
||||||
|
t1 = poker.queue_identity_task("merge_candidate", [a, b])
|
||||||
|
t2 = poker.queue_identity_task("merge_candidate", [b, a]) # same pair, reversed
|
||||||
|
assert t1 == t2
|
||||||
|
assert len(poker.list_identity_queue()) == 1
|
||||||
|
|
||||||
|
|
||||||
|
def test_name_villain_flips_named_flag(poker):
|
||||||
|
pid = poker.create_descriptor_villain("neck tattoo", venue="Meadows")
|
||||||
|
poker.name_villain(pid, "Danny")
|
||||||
|
r = poker.resolve_villain("Danny")
|
||||||
|
assert r["band"] == "name" and r["match_id"] == pid
|
||||||
Reference in New Issue
Block a user