CLAUDE.md · git:20261120.490541d · 2026-11-20 · sha256 61b2e2db1c734929

CLAUDE.md git:20261120.490541dA

Immutable. This exact content never changes and is served at /api/v1/blob/61b2e2db1c734929.

# CLAUDE.md

## Doc and Spec

Read files under `doc/` and follow them. If you find wrong content in any doc file, confirm with the user.

- `doc/product-spec.md` — vision, OS analogy, product goals, UX surface
- `doc/agentic-loop.md` — kernel: loop, interrupts, PTC, cancellation
- `doc/agent-spec.md` — process management: lifecycle, delegation, scheduling
- `doc/skill-spec.md` — dynamic extensions: format, discovery, triggers
- `doc/tool-spec.md` — syscall interface: built-in tools, safety
- `doc/chat-spec.md` — chat system: events, message model, rendering, APIs
- `doc/models.md` — hardware abstraction: providers, routing
- `doc/storage-spec.md` — filesystem layout: all persistent state, data formats
- `doc/cli.md` — CLI reference
- `doc/code-style.md` — architecture principles (one place decides and a lock holds it there; open what varies, close what does not) and code style — read before any non-trivial change
- `doc/session-spec.md` — session/context: creators, effective tools, prompt assembly
- `doc/memory-spec.md` — memory system: extraction, storage, two-tier loading
- `doc/mission-spec.md` — cron mission system
- `doc/plan-spec.md` — plan mode feature
- `doc/log-spec.md` — logging levels, throttling, output targets
- `doc/insight.md` — vision, roadmap, competitive positioning
- `doc/webrtc-spec.md` — WebRTC transport: P2P remote access, signaling, data channels
- `doc/browser-bridge-spec.md` — browser bridge: linggen-browser extension ↔ daemon, logged-in session reads (X), on-demand WS
- `doc/mcp-spec.md` — MCP front door: `/mcp`, one door, twelve tools (memory/agent/browser), Claude Code / Codex plugin distribution, retired-name history
- `doc/network-spec.md` — who talks to whom: the engine (since 2.3 it hosts memory on its one port; the old two-daemon edges are marked), every edge and its transport, where each address comes from
- `doc/mcp-client-spec.md` — ling as an MCP *client*: user-added servers, and memory as the built-in one (the engine's own `/mem/mcp`)
- `doc/room-spec.md` — rooms: community model sharing, credits, auto-dispatch
- `doc/permission-spec.md` — permission system: modes, layers, tool classification, remote trust
- `doc/yinyue-spec.md` — Yinyue companion: avatar, voice, session + memory model
- `doc/shared-session-spec.md` — shared sessions: several agents at one table, one thread/tools/permissions, per-agent persona + model
- `doc/one-ling-spec.md` — one Ling + one Yinyue on every device: who answers, Mac work continues in the app's latest Mac session as the same Ling, sessions stay per device
- `doc/plan-3.0.md` — PLAN (2026-11-15, agreed; its Progress table says what is built): 2.3 = review, build speed, ling-mem merged into ling, engine in modules, upgrades, app slimming; 2.4 = one Ling and one Yinyue (the daily session, one world state, few general tools); 2.5 = one MCP door; 3.0 = long-term, undecided
- `doc/architecture-review.md` — REVIEW (2026-11-15, read-only): the engine's layers and cycles, the target crates and split order, what the ling-mem merge touches and deletes, how a session is bound today; open questions for Hanli
- `doc/asset-pack-spec.md` — asset packs (designed 2026-11-15; engine side built, phone/publish/packaging not): a skill lists its heavy files; missing ones are fetched after install by content hash and served at the same address; first user is 灵境 art, Mac and phone
- `doc/dynamic-session-draft.md` — superseded by `plan-3.0.md` (2026-11-13 talk; the folding design was dropped)
- `doc/merge-and-slim-draft.md` — superseded by `plan-3.0.md` (2026-11-13 talk; measured sizes)
- `doc/yinyue-companion-spec.md` — Yinyue's proactive/interactive layer: senses, heralds, `agent_chat`, ambient life-signs
- `doc/perception-spec.md` — what a resident agent knows without being told: world state, the activity log, and when to speak
- `doc/phone-push-spec.md` — Mac → closed phone: skill-declared lines, her words, sealed per phone, rung through the linggen.dev APNs relay
- `doc/app-action-spec.md` — app actions as tools: one writer per mutation, phone tool registry, typed cross-device calls, destructive confirms
- `doc/background-tasks-spec.md` — background tasks: never block the chat, end as a turn, one session task list in the subagent pane, Claude runner lives while work does
- `doc/test-design.md` — testing: unit, hermetic system tests on fixtures + fake model, plugin evals, the draft→publish release gate (clean Mac VM), real-data smoke

## Build, Test, Run

Standard `cargo` and `npm` invocations throughout. The non-obvious pairs:

```bash
cargo run                          # Start background daemon + open browser (default)
cargo run -- --web --dev           # Dev mode: API only, proxies static assets to Vite
cargo run -- --root /path/to/proj  # Custom workspace root
```

Full-stack dev = `cargo run -- --web --dev` alongside `cd ui && npm run dev`
(HMR, proxies /api). Production: `cd ui && npm run build`, then `cargo run`
(embeds `ui/dist/` via rust-embed — a release binary serves stale UI until
you rebuild dist).

Inside a session the engine hosts, restart the live engine on a fresh build
with `target/fast/ling restart --binary target/fast/ling` (after
`cd ui && npm run build` and `cargo build --profile fast`). It waits for other
turns, keeps `ling.prev`, rolls back if unhealthy, and this session continues
in a new turn afterwards — do nothing else in the turn that calls it.
`scripts/restart-engine.sh` does the builds and this in one go.
Release builds (`cargo build --release`) are for shipping.

Run the tests with `scripts/scratch-test.sh` (unit) and
`scripts/scratch-test.sh system`: the engine's tests read and write their
home folder, and the script points it at a scratch one under `~/.cache` —
with the real one they would open the owner's own memory and sessions.

In a fresh `git worktree`, run `scripts/worktree-setup.sh` before the first
build: it clones (copy-on-write) the built UI, the avatar clips and the main
checkout's `target/fast`, so the first build there compiles only our own code.

## Architecture

Linggen is a local-first, multi-agent coding assistant. The binary is
`ling`. Default mode starts a background daemon + opens browser. Explore
the crates (below) and `ui/src/` directly for layout; the non-derivable anchors:

- The three extension types (skills / agents / missions) share one
  `record.rs` + `registry.rs` shape under `engine/`; their disk loaders all
  live under `extensions/`, not beside their types.
- `ui/src/stores/` (Zustand) is the primary UI state pattern; some Settings
  tabs still use local `useState` + direct `fetch` instead.
- Mission cron scheduling lives in `extensions/missions/scheduler.rs`, not
  under `engine/mission/`.

### Crates

One Cargo workspace, one version (`[workspace.package]`), one `Cargo.lock`:

- `.` — package `linggen`, the `ling` binary: `src/server/`, `src/cli/`,
  `src/main.rs`, the embedded `ui/dist/`, `agents/`, `missions/`, `shared/`
  (the engine embeds `agents/places/` itself).
- `crates/linggen-engine` — lib `linggen_engine`: every other module
  (`engine`, `extensions`, `config`, `provider`, `state_fs`, `memory_host`,
  `assets`, …). `main.rs` imports its modules at the crate root, so server
  and CLI code still says `crate::engine::…`. Only what the binary uses is
  `pub`; the rest stays `pub(crate)`. The engine never calls up into
  `server`; the server's tests reach its test helpers through the
  `test-support` feature.
- `crates/linggen-memory` — lib `ling_mem`, the memory store the engine hosts.

A bare `cargo build` / `test` / `clippy` covers the binary and the engine
(`default-members`); `cargo test -p linggen-memory` runs memory's tests.

### Configuration

Config search: `$LINGGEN_CONFIG` → `~/.linggen/config/linggen.runtime.toml` → `~/.linggen/config/linggen.toml` (`~/.linggen` is `$LINGGEN_HOME` when set).

Key sections: `[[models]]` (LLM providers), `[server]` (port), `[agent]` (max_iters, safety mode, default_permission_mode), `[logging]`, `[[agents]]` (agent spec references), `[routing]` (the default model chain and auto-fallback).

## Architecture Principles and Code Style

`doc/code-style.md` is the one place these rules are written — read it before
a non-trivial change; nothing here repeats it. In one line: a design is
measured by in how many places one thing is decided, not by how small its
pieces are.

## Key Design Patterns

- **Tool names are Claude Code-style**: `Read`, `Write`, `Edit`, `Bash`, `Glob`, `Grep` (capitalized).
- **Workspace-scoped file operations**: all paths are sandboxed to workspace root; parent traversal (`..`) is rejected.
- **Capability = tool list**: no separate policy system. If a session has Write/Edit tools, it can patch. If it has Task, it can delegate. See `session-spec.md`.
- **Real-time events**: server publishes events (`Token`, `Message`, `AgentStatus`, `SubagentSpawned`, `ToolStatus`, `PlanUpdate`, `AppLaunched`, etc.) over WebRTC data channels to the web UI.
- **App skills**: skills with `app` frontmatter section run directly (no model). Launcher types: `web` (static files served at `/apps/{name}/`), `bash` (script execution), `url` (external link). Model can also call `RunApp` tool.
- **Delegation depth**: configurable via `max_delegation_depth` (default 2). Any agent can delegate to any other agent.
- **Model routing**: default model chain with health tracking and auto-fallback on errors/rate limits.
- **Tool permissions**: session-scoped, path-aware permission model with four modes (chat/read/edit/admin) and a hardcoded deny floor for catastrophic commands. See `doc/permission-spec.md`.

When working on a task, read the relevant `doc/*.md` spec files for context — don't read all of them upfront.