diff --git a/README.md b/README.md index 48ebf91..f560650 100644 --- a/README.md +++ b/README.md @@ -268,10 +268,14 @@ pair-programming the sphere out loud, several people, quick turns. **Backends.** With the [Claude Agent SDK](https://pypi.org/project/claude-agent-sdk/) installed (`pip install -e '.[vibe]'`) and a `claude` CLI on PATH (logged -in, or `ANTHROPIC_API_KEY` set), each prompt runs as its own **Claude -Code session** in the checkout, read-only, shipping through one MCP -tool that validates on the spot and hands failures straight back — -the session iterates inside its own turn. It runs in Claude Code's +in, or `ANTHROPIC_API_KEY` set), one ongoing **Claude Code session** +in the checkout takes the prompts in turn — read-only, shipping +through one MCP tool that validates on the spot and hands failures +straight back, so it iterates inside its own turn — and it remembers +the night: what it shipped and what people asked, so "like #3 but +slower" works (every generation is on disk for it to read, too). A +session idle for twenty minutes closes; the next prompt starts a +fresh one. It runs in Claude Code's **auto** permission mode: its read-only tools and the ship tool are pre-approved, writes, shell and web are denied outright, and only a call outside both lists goes to the permission classifier — nothing diff --git a/docs/deploy.md b/docs/deploy.md index cae3a11..6cd48a7 100644 --- a/docs/deploy.md +++ b/docs/deploy.md @@ -142,9 +142,10 @@ seek. `/vibe` needs a coding model reachable from the box. Two backends, picked automatically (`LUMINARY_VIBE_BACKEND=session|api` forces one): -- **session** — each prompt is its own Claude Code session in the - checkout (read-only tools, one MCP tool to ship through). Needs the - Agent SDK and the CLI: +- **session** — one ongoing Claude Code session in the checkout takes + the prompts in turn (read-only tools, one MCP tool to ship through; it + remembers the night, closes after twenty idle minutes and reopens on + the next prompt). Needs the Agent SDK and the CLI: ```bash pip install -e '.[vibe]' # in the service's venv diff --git a/luminary/server/static/vibe.js b/luminary/server/static/vibe.js index 87be729..a1249ba 100644 --- a/luminary/server/static/vibe.js +++ b/luminary/server/static/vibe.js @@ -141,10 +141,12 @@ function genRow(g, playing) { } const who = document.createElement("span"); who.className = "who"; + const done = g.status === "ok" || g.status === "failed"; who.textContent = (g.author || "") + (g.status === "queued" ? " · queued" : g.status === "cooking" ? " · cooking…" : "") + - (g.model ? ` · ${g.model.replace(/^claude-/, "")}` : ""); + (g.model ? ` · ${g.model.replace(/^claude-/, "")}` : "") + + (done && g.seconds != null ? ` · ${Math.round(g.seconds)} s` : ""); head.appendChild(who); row.appendChild(head); const prompt = document.createElement("div"); diff --git a/luminary/vibe/core.py b/luminary/vibe/core.py index 7b14c17..c1b3584 100644 --- a/luminary/vibe/core.py +++ b/luminary/vibe/core.py @@ -145,6 +145,7 @@ def submit(self, raw: Dict[str, Any]) -> Dict[str, Any]: "status": "queued", "error": "", "file": "", + "seconds": None, "created": _now(), } self.generations.append(entry) @@ -178,6 +179,7 @@ def process_one(self, block: bool = False, timeout: float = 0.5) -> bool: entry["status"] = "cooking" self._working = entry self._save_log() + started = time.monotonic() try: self._cook(request, entry) except ( @@ -188,6 +190,7 @@ def process_one(self, block: bool = False, timeout: float = 0.5) -> bool: logger.exception("vibe #%d failed", entry["n"]) finally: with self._lock: + entry["seconds"] = round(time.monotonic() - started, 1) self._working = None self._save_log() return True @@ -283,6 +286,9 @@ def stop(self) -> None: if self._thread is not None: self._thread.join(timeout=2.0) self._thread = None + close = getattr(self.coder, "close", None) # a session backend holds one + if callable(close): + close() # ------------------------------------------------------------- helpers diff --git a/luminary/vibe/session.py b/luminary/vibe/session.py index 46258b9..2c3052b 100644 --- a/luminary/vibe/session.py +++ b/luminary/vibe/session.py @@ -1,38 +1,81 @@ -"""The vibe surface's Claude Code backend: one agent session per prompt. +"""The vibe surface's Claude Code backend: one ongoing session, prompts in turn. Where :class:`~luminary.vibe.coder.Coder` makes a single model call, -:class:`SessionCoder` hands the prompt to a real Claude Code session run -through the Agent SDK (the programmatic connector for a session): it -lives in the repo checkout with read-only tools, so it can consult -``patterns/README.md`` and the library while it writes, and it ships its -answer through one MCP tool of ours — ``ship_pattern`` — which validates -the module against the real lights on the spot and hands any failure -straight back, so the session iterates inside its own turn. Whatever it -says outside the tool is the note the crew sees. +:class:`SessionCoder` keeps ONE Claude Code session open through the +Agent SDK and hands it the crew's prompts one after another — so it +remembers the night: what it shipped, what people asked, what it has +already read. It lives in the repo checkout with read-only tools (the +craft notes, the library, and every generation on disk under +``var/vibe/``), and it ships through one MCP tool of ours — +``ship_pattern`` — which validates the module against the real lights on +the spot and hands any failure straight back, so it iterates inside its +own turn. Whatever it says outside the tool is the note the crew sees. + +The session runs on its own event loop in a background thread; the vibe +worker calls :meth:`draft` / :meth:`repair` synchronously, one at a time, +and waits. A session that breaks (the CLI died, a prompt ran past its +time) is dropped and the next prompt starts a fresh one; a session idle +for a while is closed. Both are lazy, so a quiet spell costs the startup +once, on the next prompt. Both backends share the system prompt, the reply format, and the rule -that every prompt ships a best try. The session runner is injectable, so -the tests drive this path with a fake session and never spawn the CLI. +that every prompt ships a best try. The session factory is injectable, +so the tests drive this path with a fake session and never spawn the CLI. """ from __future__ import annotations import asyncio +import logging import shutil +import threading +import time from pathlib import Path -from typing import Any, AsyncIterator, Awaitable, Callable, Dict, List, Optional +from typing import ( + Any, + AsyncIterator, + Callable, + Coroutine, + Dict, + List, + Optional, + Protocol, + TypeVar, +) from luminary.vibe.coder import SYSTEM, extract +logger = logging.getLogger(__name__) + +T = TypeVar("T") + Validator = Callable[[str], Optional[str]] # source -> error text, or None if it runs -Runner = Callable[[str, Dict[str, Any]], AsyncIterator[Any]] -MAX_TURNS = 14 + +class SessionLike(Protocol): + """What the coder needs from a session; ``ClaudeSDKClient`` has it all.""" + + async def connect(self) -> None: ... + + async def query(self, prompt: str) -> None: ... + + def receive_response(self) -> AsyncIterator[Any]: ... + + async def set_model(self, model: Optional[str]) -> None: ... + + async def disconnect(self) -> None: ... + + +SessionFactory = Callable[[Dict[str, Any]], SessionLike] + +TURN_TIMEOUT_S = 240.0 # a prompt still cooking after this is cut off +CONNECT_TIMEOUT_S = 60.0 +IDLE_S = 20 * 60.0 # a session nobody has used for this long is closed SESSION_ADDENDUM = """ -YOU ARE A CLAUDE CODE SESSION inside the Luminary checkout, with read-only tools. You may Read `patterns/README.md` (the craft notes), look at `patterns/book-two/*.py` for the registration idiom, and read the library under `luminary/patterns/` when you need a signature — but keep it quick: the crew is waiting at the sphere. +YOU ARE ONE ONGOING CLAUDE CODE SESSION inside the Luminary checkout, with read-only tools. Prompts arrive one at a time from the people around the sphere, each carrying the pattern that was showing when it was typed. What you shipped earlier and what they asked are your memory — build on it when a prompt refers back ("like #3 but slower"). Every generation is on disk as `var/vibe/vibe-NNNN.py` (generation #N): Read one when the crew names a number you don't remember. You may Read `patterns/README.md` (the craft notes), `patterns/book-two/*.py` for the registration idiom, and the library under `luminary/patterns/` when you need a signature — once is enough; the crew is waiting. -SHIP THROUGH THE TOOL: call `ship_pattern` with the complete module source (and your short note). The server loads it against the real lights and either accepts it or returns exactly what broke — fix and ship again, up to a few times. Do not write files yourself. Your final text reply is your note to the crew (at most two sentences: a question or a comment). Always ship a best try before you stop. +SHIP THROUGH THE TOOL, EVERY PROMPT: call `ship_pattern` with the complete module source (and your short note). The server loads it against the real lights and either accepts it or returns exactly what broke — fix and ship again, up to a few times. Do not write files yourself. Your final text reply is your note to the crew (at most two sentences: a question or a comment). Always ship a best try before you stop. """ READ_ONLY_TOOLS = ["Read", "Glob", "Grep"] @@ -64,83 +107,180 @@ def sdk_available() -> bool: return shutil.which("claude") is not None +class _Turn: + """One prompt's collection point: what the session shipped and said.""" + + def __init__(self) -> None: + self.shipped: Dict[str, str] = {} + self.said: List[str] = [] + self.failure = "" + + class SessionCoder: def __init__( self, repo: Path, model: Optional[str] = None, validator: Optional[Validator] = None, - runner: Optional[Runner] = None, + session_factory: Optional[SessionFactory] = None, permission_mode: Optional[str] = None, + turn_timeout: float = TURN_TIMEOUT_S, + idle_s: float = IDLE_S, ) -> None: self.repo = Path(repo) self.model = model self.validator = validator or (lambda source: None) - self._runner = runner self.permission_mode = permission_mode or DEFAULT_PERMISSION_MODE + self.turn_timeout = turn_timeout + self.idle_s = idle_s + self._factory: SessionFactory = session_factory or _sdk_session + self._injected = session_factory is not None + self.sessions_started = 0 + self._loop: Optional[asyncio.AbstractEventLoop] = None + self._thread: Optional[threading.Thread] = None + self._reaper: Optional["asyncio.Task[None]"] = None + self._session: Optional[SessionLike] = None + self._session_model: Optional[str] = None + self._turn: Optional[_Turn] = None + self._last_used = 0.0 + self._remembered: set[str] = set() # generations THIS session shipped @property def available(self) -> bool: - return self._runner is not None or sdk_available() + return self._injected or sdk_available() - # ------------------------------------------------------------ the turn + # ------------------------------------------------------------- the API def draft(self, request: Dict[str, Any]) -> tuple[str, str]: from luminary.vibe.coder import Coder - prompt = Coder.brief(request) - return asyncio.run(self._turn(prompt, request.get("model") or self.model)) + prompt = Coder.brief(self._recalled(request)) + return self._run(self._ask(prompt, self._model_for(request), request)) def repair(self, request: Dict[str, Any], code: str, error: str) -> tuple[str, str]: - """The session already iterates against the validator inside its - turn; a second full turn carries the failure forward.""" - from luminary.vibe.coder import Coder - + """A follow-up in the same conversation: what it shipped, what broke.""" prompt = ( - Coder.brief(request) - + "\n\nYour previous module:\n```python\n" - + code - + "```\nIt failed on the server:\n" + "The module you just shipped failed on the server:\n" + error.strip()[-1500:] - + "\nFix it and ship the corrected module." + + "\n\nThe module:\n```python\n" + + code + + "```\nFix it and ship the corrected module." ) - return asyncio.run(self._turn(prompt, request.get("model") or self.model)) - - async def _turn(self, prompt: str, model: Optional[str]) -> tuple[str, str]: - shipped: Dict[str, str] = {} - said: List[str] = [] - - async def ship(args: Dict[str, Any]) -> Dict[str, Any]: - code = str(args.get("code") or "") - note = str(args.get("note") or "") + return self._run(self._ask(prompt, self._model_for(request), request)) + + def close(self) -> None: + """Close the session, if any, and stop the loop thread.""" + loop, thread = self._loop, self._thread + if loop is None: + return + try: + self._run(self._shutdown()) + except Exception: # a wedged session must not hold up shutdown + logger.debug("vibe session shutdown", exc_info=True) + loop.call_soon_threadsafe(loop.stop) + if thread is not None: + thread.join(timeout=5.0) + self._loop = None + self._thread = None + + # -------------------------------------------------------- the prompt + + def _model_for(self, request: Dict[str, Any]) -> Optional[str]: + model = request.get("model") or self.model + return str(model) if model else None + + def _recalled(self, request: Dict[str, Any]) -> Dict[str, Any]: + """When the pattern showing is one THIS session shipped, a pointer + replaces its source in the brief — the session remembers it, and + the file is on disk if it doesn't.""" + shown = str(request.get("shown") or "") + if shown not in self._remembered: + return request + try: + n = int(shown.rsplit("-", 1)[-1]) + except ValueError: + return request + pointer = ( + f"# you shipped this one earlier in this session — generation #{n}, " + f"file var/vibe/vibe-{n:04d}.py; Read it if you need the exact code" + ) + return dict(request, shown_source=pointer) + + async def _ask( + self, prompt: str, model: Optional[str], request: Dict[str, Any] + ) -> tuple[str, str]: + turn = _Turn() + self._turn = turn + self._last_used = time.monotonic() + try: + session = await self._connected(model) + await asyncio.wait_for( + self._exchange(session, prompt, turn), self.turn_timeout + ) + except asyncio.TimeoutError: + await self._drop() + raise RuntimeError( + f"the session took longer than {self.turn_timeout:.0f} s and was " + "dropped; the next prompt starts a fresh one" + ) + except Exception as exc: + tail = _stderr_tail(self._session) + await self._drop() + if tail: + raise RuntimeError(f"the claude session failed: {exc}\n{tail}") from exc + raise + finally: + self._turn = None + self._last_used = time.monotonic() + note = " ".join(part.strip() for part in turn.said if part.strip())[:400] + shipped = turn.shipped.get("code") + if shipped is None: + # The session talked but never shipped: a fenced block in its + # words still counts (the core validates every draft anyway). + shipped, spoken = extract("\n".join(turn.said)) + note = spoken or note + if not shipped: + raise RuntimeError( + turn.failure or "the session ended without shipping a pattern" + ) + else: + note = turn.shipped.get("note") or note + self._remembered.add(str(request.get("pattern") or "")) + return shipped, note + + async def _exchange(self, session: SessionLike, prompt: str, turn: _Turn) -> None: + await session.query(prompt) + async for message in session.receive_response(): + turn.said.extend(_texts(message)) + if type(message).__name__ == "ResultMessage" and getattr( + message, "is_error", False + ): + turn.failure = str( + getattr(message, "result", None) + or getattr(message, "subtype", None) + or "the session reported an error" + ) + + async def _ship(self, args: Dict[str, Any]) -> Dict[str, Any]: + """The ``ship_pattern`` tool: validate now, answer ok or what broke.""" + turn = self._turn + code = str(args.get("code") or "") + note = str(args.get("note") or "") + if turn is None: + text = "no prompt is in progress — wait for the next one" + else: error = self.validator(code) if code.strip() else "empty module" if error is None: - shipped["code"] = code if code.endswith("\n") else code + "\n" - shipped["note"] = note + turn.shipped["code"] = code if code.endswith("\n") else code + "\n" + turn.shipped["note"] = note text = "ok: accepted — it runs on the sphere. Say your note and stop." else: text = "rejected:\n" + error.strip()[-1500:] - return {"content": [{"type": "text", "text": text}]} - - options = self._options(model, ship) - async for message in self._messages(prompt, options): - for text in _texts(message): - said.append(text) - note = " ".join(part.strip() for part in said if part.strip())[:400] - if "code" in shipped: - return shipped["code"], shipped.get("note") or note - # The session talked but never shipped: a fenced block in its - # words still counts (the core validates every draft anyway). - code, spoken = extract("\n".join(said)) - if code: - return code, spoken or note - raise RuntimeError("the session ended without shipping a pattern") - - def _options( - self, model: Optional[str], ship: Callable[[Dict[str, Any]], Awaitable[Any]] - ) -> Dict[str, Any]: - """Plain dict options; turned into ``ClaudeAgentOptions`` by the - SDK runner (a fake runner just reads them).""" + return {"content": [{"type": "text", "text": text}]} + + def _options(self, model: Optional[str]) -> Dict[str, Any]: + """Plain dict options; the SDK factory turns them into + ``ClaudeAgentOptions`` (a fake factory just reads them).""" return { "cwd": str(self.repo), "model": model, @@ -148,20 +288,94 @@ def _options( "allowed_tools": READ_ONLY_TOOLS + [SHIP_TOOL], "disallowed_tools": DENIED_TOOLS, "permission_mode": self.permission_mode, - "max_turns": MAX_TURNS, "setting_sources": [], - "ship": ship, + "ship": self._ship, } - def _messages(self, prompt: str, options: Dict[str, Any]) -> AsyncIterator[Any]: - if self._runner is not None: - return self._runner(prompt, options) - return _sdk_runner(prompt, options) + # ------------------------------------------------------- the session + + async def _connected(self, model: Optional[str]) -> SessionLike: + if self._session is None: + session = self._factory(self._options(model)) + try: + await asyncio.wait_for(session.connect(), CONNECT_TIMEOUT_S) + except Exception as exc: + tail = _stderr_tail(session) + try: + await session.disconnect() + except Exception: + pass + raise RuntimeError( + f"the claude session failed to start: {exc}\n{tail}".rstrip() + ) from exc + self._session = session + self._session_model = model + self._remembered.clear() + self.sessions_started += 1 + if self._reaper is None or self._reaper.done(): + self._reaper = asyncio.ensure_future(self._reap()) + elif model != self._session_model: + await asyncio.wait_for(self._session.set_model(model), CONNECT_TIMEOUT_S) + self._session_model = model + return self._session + + async def _drop(self) -> None: + session, self._session = self._session, None + self._session_model = None + self._remembered.clear() + if session is not None: + try: + await asyncio.wait_for(session.disconnect(), 15.0) + except Exception: # it was already gone, most likely + logger.debug("vibe session disconnect", exc_info=True) + + async def _shutdown(self) -> None: + if self._reaper is not None: + self._reaper.cancel() + self._reaper = None + await asyncio.sleep(0) + await self._drop() + + async def _reap(self) -> None: + """Close a session nobody has used for ``idle_s``; the next prompt + starts a fresh one.""" + nap = max(min(30.0, self.idle_s / 4.0), 0.01) + while True: + await asyncio.sleep(nap) + idle = time.monotonic() - self._last_used + if self._session is not None and self._turn is None and idle > self.idle_s: + logger.info("vibe: closing the session after %.0f s idle", idle) + await self._drop() + + # ------------------------------------------------------------ the loop + + def _run(self, coro: Coroutine[Any, Any, T]) -> T: + """Run ``coro`` on the session's loop and wait for it (the worker + is the only caller, and calls one at a time).""" + future = asyncio.run_coroutine_threadsafe(coro, self._ensure_loop()) + return future.result(timeout=self.turn_timeout + CONNECT_TIMEOUT_S + 30.0) + + def _ensure_loop(self) -> asyncio.AbstractEventLoop: + if self._loop is not None: + return self._loop + loop = asyncio.new_event_loop() + + def run() -> None: + asyncio.set_event_loop(loop) + try: + loop.run_forever() + finally: + loop.close() + + self._thread = threading.Thread(target=run, name="vibe-session", daemon=True) + self._thread.start() + self._loop = loop + return loop def _texts(message: Any) -> List[str]: - """Assistant text blocks out of an SDK message (duck-typed, so the - fake runner can yield plain objects).""" + """Assistant text blocks out of an SDK message (duck-typed, so a fake + session can yield plain objects).""" content = getattr(message, "content", None) if type(message).__name__ != "AssistantMessage" or not isinstance(content, list): return [] @@ -172,13 +386,44 @@ def _texts(message: Any) -> List[str]: ] -async def _sdk_runner(prompt: str, options: Dict[str, Any]) -> AsyncIterator[Any]: - """The real thing: a Claude Code session through the Agent SDK, our - ``ship_pattern`` tool served in-process.""" +def _stderr_tail(session: Any) -> str: + """The CLI's own words when it fails (not logged in…), if the session + kept them.""" + lines = getattr(session, "stderr", None) or [] + return "\n".join(str(line).strip() for line in lines if str(line).strip())[-800:] + + +class _SdkSession: + """``ClaudeSDKClient`` plus the CLI's stderr, for the error text.""" + + def __init__(self, client: Any, stderr: List[str]) -> None: + self.client = client + self.stderr = stderr + + async def connect(self) -> None: + await self.client.connect() + + async def query(self, prompt: str) -> None: + await self.client.query(prompt) + + def receive_response(self) -> AsyncIterator[Any]: + response: AsyncIterator[Any] = self.client.receive_response() + return response + + async def set_model(self, model: Optional[str]) -> None: + await self.client.set_model(model) + + async def disconnect(self) -> None: + await self.client.disconnect() + + +def _sdk_session(options: Dict[str, Any]) -> SessionLike: + """The real thing: a long-lived Claude Code session through the Agent + SDK's client, our ``ship_pattern`` tool served in-process.""" from claude_agent_sdk import ( ClaudeAgentOptions, + ClaudeSDKClient, create_sdk_mcp_server, - query, tool, ) @@ -199,7 +444,7 @@ async def ship_pattern(args: Dict[str, Any]) -> Dict[str, Any]: )(ship_pattern) server = create_sdk_mcp_server(name="luminary", tools=[ship_tool]) - stderr: List[str] = [] # the CLI's own words when it fails (not logged in…) + stderr: List[str] = [] sdk_options = ClaudeAgentOptions( cwd=options["cwd"], model=options["model"], @@ -207,16 +452,8 @@ async def ship_pattern(args: Dict[str, Any]) -> Dict[str, Any]: allowed_tools=options["allowed_tools"], disallowed_tools=options["disallowed_tools"], permission_mode=options["permission_mode"], - max_turns=options["max_turns"], setting_sources=options["setting_sources"], mcp_servers={"luminary": server}, stderr=stderr.append, ) - try: - async for message in query(prompt=prompt, options=sdk_options): - yield message - except Exception as exc: - tail = "\n".join(line.strip() for line in stderr if line.strip())[-800:] - if not tail: - raise - raise RuntimeError(f"the claude session failed: {exc}\n{tail}") from exc + return _SdkSession(ClaudeSDKClient(options=sdk_options), stderr) diff --git a/plan/spec/implementation-notes.md b/plan/spec/implementation-notes.md index f349005..15739b3 100644 --- a/plan/spec/implementation-notes.md +++ b/plan/spec/implementation-notes.md @@ -52,7 +52,7 @@ | `luminary/stage/web.py` | — | Stage web adapter (`/stage`, `/api/queue[/play_next…]`, `/api/repeats/…`, `/api/audio`, `WS /api/stage` + layout/patterns/chapters): thin routes over `StageCore`, mapping-web-shaped per-socket sinks and fps ticker that idles when no sinks + no audio + queue exhausted; mutating routes gated by `X-Stage-Key` when a stage key is configured (`--stage-key` / env `LUMINARY_STAGE_KEY`, flag wins; read-only never gated); `--stage-lights` resolver (default: the pentagon-4A-33 capture) | | `luminary/server/static/stage.html`, `stage.js` | — | The stage page: canvas viewer (imports `StreamView`/`WireStream` from `mapping.js`) + queue/repeats panels, add + play-next controls, chapter-path + liner-notes header, server-fetched chapter previews for queued compositions, stage-key field (localStorage / `#key=…` fragment); renders `/api/queue` state and sends commands, ~2 s poll — no client-side scheduling | | `luminary/vibe/core.py` | — | `VibeCore`: vibe mode's shared state — prompts numbered at submit (#1 onward, never reused), the pattern showing *at submit* captured as context (name + source), one worker cooking in order: coder draft → identity stamp (`assign_identity`, AST: `name = "vibe-N"` + `description`) → `validate` (throwaway registry on the stage's own lights: traceback, `(n,3)` shape, finite, stateless, frame ≤ 0.25 s) → one repair round → `var/vibe/vibe-NNNN.py` (never overwritten; rejected drafts kept as `_vibe-NNNN-.py`, which the registry skips) → registry reload → `StageCore.cut`. `log.json` is the thread (tmp+rename); queued/cooking entries are marked failed on restart, never dropped. The menu groups the repo's patterns by folder from the registry's origins | -| `luminary/vibe/coder.py`, `session.py`, `setup.py` | — | The coding model. `Coder`: one Messages API call (httpx; injectable transport) with the system prompt that carries the pattern contract, the library inventory and the craft rules, and the rule that every prompt ships a best try (optional ≤2-sentence note, then one fenced block; `extract`). `SessionCoder`: one Claude Code session per prompt through the Agent SDK — read-only tools in the checkout, a `ship_pattern` MCP tool that runs the core's validator in-turn and returns what broke, so the session iterates before it answers; auto permission mode (the allow/deny lists resolve first, the classifier only sees a call outside both, nothing waits on a prompt; `LUMINARY_VIBE_PERMISSION_MODE` overrides — `bypassPermissions` is refused by the CLI under root); the runner is injectable (tests never spawn the CLI). `setup.default_coder` picks session when the SDK and a `claude` CLI are present (`LUMINARY_VIBE_BACKEND` forces), `vibe_models` the select list (`LUMINARY_VIBE_MODELS`) | +| `luminary/vibe/coder.py`, `session.py`, `setup.py` | — | The coding model. `Coder`: one Messages API call (httpx; injectable transport) with the system prompt that carries the pattern contract, the library inventory and the craft rules, and the rule that every prompt ships a best try (optional ≤2-sentence note, then one fenced block; `extract`). `SessionCoder`: ONE ongoing Claude Code session through the Agent SDK's `ClaudeSDKClient`, prompts fed in turn on its own event-loop thread (the worker blocks on each) — read-only tools in the checkout, a `ship_pattern` MCP tool that runs the core's validator in-turn and returns what broke, so it iterates before it answers, and memory across prompts (a generation it shipped itself is referred to by number, not re-sent; every generation is on disk for it to Read); a prompt past its wall-clock cutoff or a broken session is dropped and lazily restarted, an idle session (20 min) is closed, the model switches in place per submitter; auto permission mode (the allow/deny lists resolve first, the classifier only sees a call outside both, nothing waits on a prompt; `LUMINARY_VIBE_PERMISSION_MODE` overrides — `bypassPermissions` is refused by the CLI under root); the session factory is injectable (tests never spawn the CLI). `setup.default_coder` picks session when the SDK and a `claude` CLI are present (`LUMINARY_VIBE_BACKEND` forces), `vibe_models` the select list (`LUMINARY_VIBE_MODELS`) | | `luminary/vibe/web.py`, `server/static/vibe.html`, `vibe.js` | — | Vibe web adapter: `GET /vibe`, `GET /api/vibe` (the whole snapshot), `POST /api/vibe` (202 + the numbered entry), `POST /api/vibe/select`; mutations share the stage's `stage_key_guard`. The page decodes the stage's own wire stream (no second render) and re-renders the snapshot every ~2 s and after each verb; menu (named / all / repo by folder, hideable), thread (status, note, context "while #N was showing"), prompt + name + author + model select + from-scratch. Mounted by `create_app` only when the stage is on and (`allow_pattern_upload` or a stage key) — the upload security model, §15.5.2 | | `luminary/cli.py` | §16 | `serve` / `play` / `capture` / `render` / `map` — every verb an adapter over the one engine | | `firmware/scorpio/lib/lumicodec/` | §13 | Plain-C++17 decoder core + Q14 fixed-point color; host-compilable, no Arduino deps | diff --git a/tests/test_vibe.py b/tests/test_vibe.py index 965a6d6..5e15f17 100644 --- a/tests/test_vibe.py +++ b/tests/test_vibe.py @@ -3,12 +3,13 @@ overwritten — and hot-cut onto the stage; every page shares that state. The model is a fake on both backends: ``Coder(transport=...)`` answers -with scripted replies, and ``SessionCoder(runner=...)`` is driven by a +with scripted replies, and ``SessionCoder(session_factory=...)`` gets a fake Claude Code session that calls the ``ship_pattern`` tool the way the SDK would. No network, no CLI, no real player — the stage is the same fake-clock core the stage tests use. """ +import asyncio import json import time @@ -410,91 +411,240 @@ def __init__(self, *blocks): self.content = list(blocks) -def test_session_backend_ships_through_the_tool(): - seen = {} +class ResultMessage: + def __init__(self, is_error=False, result=None, subtype="success"): + self.is_error, self.result, self.subtype = is_error, result, subtype - async def runner(prompt, options): - seen["prompt"], seen["options"] = prompt, options - ship = options["ship"] - first = await ship({"code": "BROKEN", "note": "try one"}) - assert first["content"][0]["text"].startswith("rejected:\nno good") - second = await ship({"code": "GOOD", "note": "try two"}) - assert second["content"][0]["text"].startswith("ok: accepted") - yield AssistantMessage(TextBlock("Slower this time — want it bluer?")) +class FakeSession: + """Stands in for ClaudeSDKClient: one scripted step per prompt, the + ship tool called the way the SDK would. A step is a dict: ``ship`` + ((code, note) pairs pushed through the tool), ``say`` (the + assistant's words), ``result`` (ResultMessage kwargs), ``raise`` (an + exception), ``sleep`` (seconds before answering).""" + + def __init__(self, options, steps, stderr=()): + self.options = options + self.steps = list(steps) + self.stderr = list(stderr) + self.prompts = [] + self.models = [] + self.results = [] + self.connected = False + self.disconnected = False + + async def connect(self): + self.connected = True + + async def query(self, prompt): + self.prompts.append(prompt) + + async def receive_response(self): + step = self.steps.pop(0) + if step.get("sleep"): + await asyncio.sleep(step["sleep"]) + if step.get("raise"): + raise step["raise"] + for code, note in step.get("ship", []): + reply = await self.options["ship"]({"code": code, "note": note}) + self.results.append(reply["content"][0]["text"].split("\n")[0]) + yield AssistantMessage(TextBlock(step.get("say", ""))) + yield ResultMessage(**step.get("result", {})) + + async def set_model(self, model): + self.models.append(model) + + async def disconnect(self): + self.disconnected = True + + +OK_REPLY = "ok: accepted — it runs on the sphere. Say your note and stop." + + +def scripted_sessions(*scripts, stderr=()): + """A session factory handing out one FakeSession per connect, each + with the next script; returns (factory, the sessions made).""" + made, pending = [], list(scripts) + + def factory(options): + session = FakeSession(options, pending.pop(0), stderr=stderr) + made.append(session) + return session + + return factory, made + + +def session_coder(*scripts, stderr=(), **kwargs): + factory, made = scripted_sessions(*scripts, stderr=stderr) coder = SessionCoder( "/repo", model="m-deep", validator=lambda code: None if code == "GOOD" else "no good", - runner=runner, + session_factory=factory, + **kwargs, + ) + return coder, made + + +def test_session_backend_keeps_one_session_across_prompts(): + coder, made = session_coder( + [ + {"ship": [("BROKEN", "try one"), ("GOOD", "try two")], "say": "Bluer?"}, + {"ship": [("GOOD", "again")], "say": "Done."}, + {"ship": [("GOOD", "")], "say": ""}, + ] ) assert coder.available is True - code, note = coder.draft({"prompt": "slower", "author": "ross", "shown": ""}) - assert code == "GOOD\n" and note == "try two" - assert seen["prompt"].startswith("Prompt from ross: slower") - options = seen["options"] + request = {"prompt": "slower", "author": "ross", "shown": "", "pattern": "vibe-1"} + assert coder.draft(request) == ("GOOD\n", "try two") + session = made[0] + assert session.connected and session.results == ["rejected:", OK_REPLY] + assert session.prompts[0].startswith("Prompt from ross: slower") + options = session.options assert options["cwd"] == "/repo" and options["model"] == "m-deep" assert SHIP_TOOL in options["allowed_tools"] and "Read" in options["allowed_tools"] assert ( "Bash" in options["disallowed_tools"] and "Write" in options["disallowed_tools"] ) assert options["permission_mode"] == "auto" # rules first, then the classifier - assert options["setting_sources"] == [] - assert "ship_pattern" in options["system_prompt"] - # The mode is a knob (LUMINARY_VIBE_PERMISSION_MODE); the tool lists are not. - manual = SessionCoder("/repo", runner=runner, permission_mode="default") - assert manual._options(None, ship=None)["permission_mode"] == "default" + assert options["setting_sources"] == [] and "max_turns" not in options + assert ( + "ONGOING" in options["system_prompt"] + and "ship_pattern" in options["system_prompt"] + ) + + # The next prompt goes to the SAME session — its memory is the point — + # on the model this submitter picked; a pattern it shipped itself is + # referred to by number, not re-sent. + request = { + "prompt": "more purple", + "model": "m-fast", + "shown": "vibe-1", + "shown_source": "SOURCE-OF-1", + "pattern": "vibe-2", + } + assert coder.draft(request) == ("GOOD\n", "again") + assert len(made) == 1 and coder.sessions_started == 1 + assert session.models == ["m-fast"] + assert "you shipped this one earlier" in session.prompts[1] assert ( - manual._options(None, ship=None)["disallowed_tools"] - == options["disallowed_tools"] + "vibe-0001.py" in session.prompts[1] and "SOURCE-OF-1" not in session.prompts[1] + ) + # A repo pattern's source still travels with the prompt. + coder.draft( + { + "prompt": "x", + "shown": "spiral", + "shown_source": "SPIRAL-SRC", + "pattern": "vibe-3", + } ) + assert "SPIRAL-SRC" in session.prompts[2] and session.models == ["m-fast", "m-deep"] + coder.close() + assert session.disconnected is True -def test_session_backend_falls_back_to_spoken_code_or_fails(): - async def talker(prompt, options): - yield AssistantMessage(TextBlock("Here you go.\n```python\nclass A: pass\n```")) - coder = SessionCoder("/repo", runner=talker) - assert coder.draft({"prompt": "x"}) == ("class A: pass\n", "Here you go.") +def test_session_backend_permission_mode_knob(): + coder, _made = session_coder([], permission_mode="default") + options = coder._options(None) + assert options["permission_mode"] == "default" + assert ( + options["disallowed_tools"] + == session_coder([])[0]._options(None)["disallowed_tools"] + ) - async def silent(prompt, options): - yield AssistantMessage(TextBlock("Hmm.")) - with pytest.raises(RuntimeError, match="without shipping"): - SessionCoder("/repo", runner=silent).draft({"prompt": "x"}) +def test_session_backend_drops_a_broken_session_and_starts_over(): + coder, made = session_coder( + [{"raise": RuntimeError("boom")}], + [{"ship": [("GOOD", "")], "say": "back"}], + stderr=["Not logged in"], + ) + with pytest.raises(RuntimeError, match="boom") as info: + coder.draft({"prompt": "x"}) + assert "Not logged in" in str(info.value) # the CLI's own words, kept + assert made[0].disconnected is True + assert coder.draft({"prompt": "x"}) == ("GOOD\n", "back") # a fresh one + assert len(made) == 2 and coder.sessions_started == 2 + coder.close() + + +def test_session_backend_cuts_off_a_runaway_prompt(): + coder, made = session_coder( + [{"sleep": 5.0}], + [{"ship": [("GOOD", "")], "say": "fresh"}], + turn_timeout=0.05, + ) + with pytest.raises(RuntimeError, match="longer than"): + coder.draft({"prompt": "x"}) + assert made[0].disconnected is True + assert coder.draft({"prompt": "x"}) == ("GOOD\n", "fresh") + assert len(made) == 2 + coder.close() + + +def test_session_backend_closes_an_idle_session(): + coder, made = session_coder( + [{"ship": [("GOOD", "")], "say": "one"}], + [{"ship": [("GOOD", "")], "say": "two"}], + idle_s=0.05, + ) + coder.draft({"prompt": "x"}) + deadline = time.monotonic() + 3.0 + while not made[0].disconnected and time.monotonic() < deadline: + time.sleep(0.02) + assert made[0].disconnected is True # nobody came back: closed + assert coder.draft({"prompt": "y"}) == ("GOOD\n", "two") # lazily reopened + assert len(made) == 2 + coder.close() - async def repairer(prompt, options): - assert "It failed on the server:" in prompt and "boom" in prompt - await options["ship"]({"code": "GOOD", "note": ""}) - yield AssistantMessage(TextBlock("fixed")) - coder = SessionCoder("/repo", validator=lambda c: None, runner=repairer) +def test_session_backend_falls_back_to_spoken_code_or_fails(): + coder, made = session_coder( + [ + {"say": "Here you go.\n```python\nclass A: pass\n```"}, + {"say": "Hmm."}, + { + "say": "gave up", + "result": {"is_error": True, "result": "API rate limited"}, + }, + {"ship": [("GOOD", "")], "say": "fixed"}, + ] + ) + assert coder.draft({"prompt": "x"}) == ("class A: pass\n", "Here you go.") + with pytest.raises(RuntimeError, match="without shipping"): + coder.draft({"prompt": "x"}) + with pytest.raises(RuntimeError, match="rate limited"): + coder.draft({"prompt": "x"}) + # A repair is a follow-up in the same conversation, not a new brief. assert coder.repair({"prompt": "x"}, "old", "boom") == ("GOOD\n", "fixed") + prompt = made[0].prompts[-1] + assert prompt.startswith("The module you just shipped failed") + assert "boom" in prompt and "old" in prompt + assert len(made) == 1 # none of that cost the session + coder.close() def test_session_backend_end_to_end_on_the_stage(tmp_path, lights): # noqa: F811 """The session validates inside its turn against the core's own - validator, and the core still runs its gates after.""" - attempts = [] - - async def runner(prompt, options): - for code in (BROKEN, GOOD): - result = await options["ship"]({"code": code, "note": "shipped"}) - attempts.append(result["content"][0]["text"].split("\n")[0]) - yield AssistantMessage(TextBlock("Two tries.")) - + validator, the core still runs its gates after, and stopping the + core closes the session.""" + factory, made = scripted_sessions( + [{"ship": [(BROKEN, "shipped"), (GOOD, "shipped")], "say": "Two tries."}] + ) core, stage, _registry = make_vibe( - tmp_path, lights, SessionCoder("/repo", runner=runner) + tmp_path, lights, SessionCoder("/repo", session_factory=factory) ) core.submit({"prompt": "tide"}) core.process_one() - assert attempts == [ - "rejected:", - "ok: accepted — it runs on the sphere. Say your note and stop.", - ] - assert core.generations[0]["status"] == "ok" - assert core.generations[0]["note"] == "shipped" + assert made[0].results == ["rejected:", OK_REPLY] + entry = core.generations[0] + assert entry["status"] == "ok" and entry["note"] == "shipped" + assert entry["seconds"] is not None and entry["seconds"] >= 0 assert stage.snapshot()["now"]["pattern"] == "vibe-1" + core.stop() + assert made[0].disconnected is True # ---------------------------------------------------------------- the web