Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6184aa21fb | ||
|
|
d2129a886f |
54
.github/workflows/ci.yml
vendored
54
.github/workflows/ci.yml
vendored
@@ -136,9 +136,7 @@ jobs:
|
||||
- name: Webfonts cover the assets
|
||||
run: scripts/site-fonts.sh --check
|
||||
|
||||
# The one part of docs/brand/voice.md a machine can judge. Everything else in it — evidence
|
||||
# with claims, naming the trade-off — is a human's call, and a checker that pretended otherwise
|
||||
# would be one nobody trusts.
|
||||
# The part of docs/brand/voice.md a machine can judge: banned words and British spellings
|
||||
- name: Voice
|
||||
run: python3 scripts/voice-check.py
|
||||
|
||||
@@ -162,6 +160,24 @@ jobs:
|
||||
env:
|
||||
RUSTDOCFLAGS: -D warnings
|
||||
|
||||
# The phone app's end-to-end test drives a real `luxe` (a session, the bridge, pairing) in
|
||||
# WebKit, the only engine an iPhone has. It runs here because the Build step above already
|
||||
# made `target/debug/luxe`: a job of its own would compile the workspace a second time, and
|
||||
# the binary is too large to hand between jobs cheaply. The app's types and unit tests need
|
||||
# no Rust, so they stay in `remote-app`.
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
with:
|
||||
node-version: 24
|
||||
cache: npm
|
||||
cache-dependency-path: crates/luxe-remote/app-tests/package-lock.json
|
||||
|
||||
- name: Phone app end to end (WebKit)
|
||||
working-directory: crates/luxe-remote/app-tests
|
||||
run: |
|
||||
npm ci
|
||||
npx playwright install --with-deps webkit
|
||||
npm run e2e
|
||||
|
||||
# Does it build and pass on the platforms nobody develops on? Linux above is the canonical gate —
|
||||
# `fmt` is an answer about the source, not about the OS, so running it three times would only
|
||||
# triple the noise from one rule. What cannot be known without a real runner is whether the code
|
||||
@@ -342,12 +358,42 @@ jobs:
|
||||
run: node ../scripts/site-cross.mjs --require webkit
|
||||
working-directory: web
|
||||
|
||||
# The phone app `luxe remote` serves. It ships as written, with no build step, so this is where a
|
||||
# type error gets caught: its JSDoc checked by tsc, and its folds unit-tested. The end-to-end
|
||||
# test, one whole turn in WebKit, runs in `gate`, which already has a `luxe` built.
|
||||
remote-app:
|
||||
name: phone app
|
||||
needs: changes
|
||||
if: needs.changes.outputs.code == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
defaults:
|
||||
run:
|
||||
working-directory: crates/luxe-remote/app-tests
|
||||
steps:
|
||||
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
||||
|
||||
- uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
|
||||
with:
|
||||
node-version: 24
|
||||
cache: npm
|
||||
cache-dependency-path: crates/luxe-remote/app-tests/package-lock.json
|
||||
|
||||
- name: Install
|
||||
run: npm ci
|
||||
|
||||
- name: Types
|
||||
run: npm run check
|
||||
|
||||
- name: Unit tests
|
||||
run: npm test
|
||||
|
||||
# The one check a ruleset requires. A skipped job reports as passing but a skipped matrix job
|
||||
# reports under no name a ruleset can hold, so this sums them up: it fails when any job failed or
|
||||
# was cancelled, `changes` included, and passes when each one passed or had nothing to check.
|
||||
passed:
|
||||
name: CI passed
|
||||
needs: [changes, gate, portability, msrv, supply-chain, website]
|
||||
needs: [changes, gate, portability, msrv, supply-chain, website, remote-app]
|
||||
if: always()
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -38,6 +38,9 @@ web/node_modules/
|
||||
web/dist/
|
||||
# Wrangler's dev-server scratch: caches, a shim worker, and its source maps.
|
||||
web/.wrangler/
|
||||
|
||||
# The phone app's type checker, test libraries and browser driver
|
||||
crates/luxe-remote/app-tests/node_modules/
|
||||
web/.astro/
|
||||
|
||||
# cargo-mutants scratch output
|
||||
|
||||
288
AGENTS.md
288
AGENTS.md
@@ -1,27 +1,27 @@
|
||||
# AGENTS.md — Luxe
|
||||
# AGENTS.md
|
||||
|
||||
Rules for working in this repository. Each line is something a capable engineer would get wrong
|
||||
without being told; anything the code already shows is left out, because this file rides in every
|
||||
request. Process, tooling and the long explanations are in [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
||||
Rules for working on Luxe: only what you'd get wrong without being told. It's sent with every
|
||||
request, so keep it short; the long version is [`CONTRIBUTING.md`](CONTRIBUTING.md).
|
||||
|
||||
## Commands
|
||||
|
||||
```bash
|
||||
cargo check -p <crate> # the inner loop
|
||||
cargo nextest run --workspace # ~15s once compiled
|
||||
cargo check -p <crate> # quick loop
|
||||
cargo nextest run --workspace # ~15s once built
|
||||
cargo clippy --workspace --all-targets -- -D warnings
|
||||
cargo fmt --all --check
|
||||
cargo deny check
|
||||
RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps # a broken doc link fails CI
|
||||
cargo test --workspace --examples # nextest does NOT run these
|
||||
python3 scripts/size-check.py # file-size ratchet (--report lists)
|
||||
python3 scripts/fn-length-check.py # function-length ratchet
|
||||
python3 scripts/voice-check.py # reads source comments too
|
||||
python3 scripts/docs-check.py # the guide's links, frontmatter, voice
|
||||
cargo test --workspace --examples # nextest skips these
|
||||
python3 scripts/size-check.py # file size limit
|
||||
python3 scripts/fn-length-check.py # function length limit
|
||||
python3 scripts/voice-check.py # banned words, British spelling
|
||||
python3 scripts/docs-check.py # guide links, frontmatter, words
|
||||
python3 scripts/version-check.py <base> # a shipped change bumps one step
|
||||
scripts/guide-bundle.sh # after editing docs/guide
|
||||
scripts/site-assets.sh --check # generated web assets are current
|
||||
scripts/site-fonts.sh --check # webfonts cover those assets
|
||||
(cd web && npm run cross) # the demos boot in every engine
|
||||
scripts/site-fonts.sh --check # web fonts cover those assets
|
||||
(cd web && npm run cross) # site demos run in every browser engine
|
||||
scripts/win-check.sh [--clippy] # the suite for Windows, under wine
|
||||
scripts/install-dev.sh # rebuild the release binary on PATH
|
||||
|
||||
@@ -29,160 +29,144 @@ cargo run -p luxe-cli -- # the TUI
|
||||
cargo run -p luxe-cli -- --headless -p "..." # one prompt, no UI
|
||||
```
|
||||
|
||||
Run the whole gate before calling work done, and `scripts/win-check.sh` in **both modes** before
|
||||
calling anything cross-platform done: CI also lints the Windows target, and a `#[cfg(unix)]` helper
|
||||
left unused there is a `-D warnings` error the default run cannot see. The voice check reads
|
||||
**source comments**, so one British spelling in a doc comment fails the build (it checks itself;
|
||||
do not quote the words it bans). A new glyph that reaches a captured surface needs
|
||||
`scripts/site-fonts.sh` after `site-assets.sh`, or it ships as tofu.
|
||||
## Before you call it done
|
||||
|
||||
- **Never run two builds or test runs at once.** They thrash and both take longer. Wrap unattended
|
||||
runs in `timeout <secs>`; never poll with `while pgrep cargo`.
|
||||
- **A slow run is compiling, not hanging.** `.config/nextest.toml` kills any test over ~120s.
|
||||
- **`luxe` on `PATH` is `target/release/luxe`, and nothing above builds release.** When a reported
|
||||
defect contradicts the source, check the binary's date against the fix before re-debugging.
|
||||
- Run the whole list. For anything cross-platform, run `scripts/win-check.sh` with and without
|
||||
`--clippy`: CI lints Windows, and an unused `#[cfg(unix)]` helper fails there.
|
||||
- One build or test run at a time. Wrap unattended runs in `timeout <secs>`; never poll with
|
||||
`while pgrep cargo`. A slow run is compiling, not stuck: nextest kills tests over ~120s.
|
||||
- `luxe` on `PATH` is `target/release/luxe`, and only `install-dev.sh` rebuilds it. If a bug report
|
||||
contradicts the source, check the binary's date first.
|
||||
- The voice check reads code comments too: one British spelling in a doc comment fails CI.
|
||||
- A new glyph on a captured screen needs `site-assets.sh`, then `site-fonts.sh`, or it shows as a
|
||||
box.
|
||||
|
||||
## Architecture boundaries
|
||||
## Architecture
|
||||
|
||||
- **Dependencies point inward to `luxe-core`.** Allowed: `providers`, `tools`, `lsp`, `dap`, `mcp`
|
||||
and `tui` → `core`; `cli` → all of them. Forbidden: `core` → anything in-workspace; `tui` →
|
||||
`providers`/`tools` (go through core traits).
|
||||
- **`core` is pure logic and traits: no UI, no HTTP.** No `reqwest`, no `ratatui`. Spawning a
|
||||
process is fine (git, hooks, formatters, the MCP stdio transport); network I/O lives in
|
||||
`luxe-providers`, `luxe-tools`' web tools and `luxe-cli`, with any policy (`luxe-core::update`)
|
||||
kept in core.
|
||||
- **UI talks to core only over channels.** `Action` in, `Event` out. A UI never calls a provider or
|
||||
a tool directly and never blocks the runtime.
|
||||
- **Cross-cutting behavior is a `Hook`**, not a special case threaded through the loop.
|
||||
- Dependencies point in, to `luxe-core`. `providers`, `tools`, `lsp`, `dap`, `mcp`, `remote` and
|
||||
`tui` may use `core`; `cli` may use all of them. `core` uses no workspace crate, and `tui` never
|
||||
uses `providers` or `tools` (go through core traits).
|
||||
- `core` is logic and traits: no UI, no HTTP, no `reqwest` or `ratatui`. Spawning processes is
|
||||
fine. Network code lives in `luxe-providers`, `luxe-tools`' web tools, `luxe-remote` and
|
||||
`luxe-cli`; its policy (like `luxe-core::update`) stays in core.
|
||||
- A UI talks to core over channels only: `Action` in, `Event` out. It never calls a provider or a
|
||||
tool and never blocks the runtime.
|
||||
- Behavior that cuts across the loop is a `Hook`, not a special case in the loop.
|
||||
- One owner per platform question: `user_dirs` (home and Luxe's folders), `program` (finding
|
||||
programs and shells, detaching, killing a process tree), `paths` (path to string), `fs_atomic`
|
||||
(safe file replace), `luxe-lsp::uri` (`file://` URLs).
|
||||
|
||||
## Rust conventions
|
||||
## Rust
|
||||
|
||||
- **Modules:** `foo.rs` beside `foo/`, never `foo/mod.rs`, except `luxe-core/tests/common/`, since
|
||||
each top-level file under `tests/` is its own binary.
|
||||
- **Errors:** `thiserror` enums in libraries, `anyhow` only in `luxe-cli`. No `unwrap()`/`expect()`
|
||||
on a fallible path; `expect()` is for real invariants and says why. **Branch on the variant,
|
||||
never on the message**: a `to_string()` match turns rewording an error into a silent behavior
|
||||
change. Messages are lowercase with no trailing period.
|
||||
- **Async:** `tokio`; `spawn_blocking` for blocking work. **Turns must be abortable**: cancellation
|
||||
drops the future, so anything spawned needs `kill_on_drop` or its own process-group guard.
|
||||
- **Logging:** `tracing`, structured fields over interpolation, secrets through the redaction
|
||||
layer. `info` is routine behavior worth reading, `debug` detail you go looking for, `warn` an
|
||||
oddity the run coped with, `error` a real failure. Don't name the module; `target` does.
|
||||
- **Size:** at most 500 lines of production code per file (split by responsibility) and 80 lines
|
||||
per function (4 to 20 for new code). A wide `match` with thin arms is fine; a fat arm is not.
|
||||
`scripts/size-check.py` and `scripts/fn-length-check.py` are ratchets: an allowlisted entry may
|
||||
shrink, never grow, and a new one needs a reason a reviewer accepts.
|
||||
- **Visibility:** `pub` means used from another crate; everything else is `pub(crate)` or
|
||||
narrower. `unreachable_pub` checks items but not fields, so narrow fields by hand.
|
||||
- **Tests:** unit tests in-module; an inline test module past 150 lines moves to a sibling
|
||||
`foo/tests.rs`. Integration tests group into one binary per area (`tests/<area>/main.rs` plus
|
||||
modules). `core` is fully testable headless: mock providers and temp dirs, no UI, no network. A
|
||||
test in `examples/` runs only if its `[[example]]` declares `test = true`.
|
||||
- **A test earns its place by being able to fail.** Watch a regression test fail on the unfixed
|
||||
code. `scripts/mutants.sh -f <file>` finds the tests that cannot fail; use it on anything
|
||||
security- or format-critical. A round-trip cannot catch a rename, so a persisted shape is pinned
|
||||
against a literal copied from real output (a `.luxe/sessions/*.jsonl` line, a real `gh` reply).
|
||||
- **Every public item has a `///` doc.**
|
||||
- `foo.rs` next to `foo/`, never `foo/mod.rs` (except `luxe-core/tests/common/`).
|
||||
- Errors: `thiserror` enums in libraries, `anyhow` only in `luxe-cli`. No `unwrap()` or `expect()`
|
||||
on anything that can fail; `expect()` is for things that can't, and says why. Match on the error
|
||||
variant, never its message. Messages are lowercase with no trailing period.
|
||||
- Turns must be cancelable. Cancel drops the future, so anything spawned needs `kill_on_drop` or
|
||||
its own process-group guard. Blocking work goes in `spawn_blocking`.
|
||||
- Logging is `tracing`, with fields instead of formatted strings and secrets through the redaction
|
||||
layer. `info` is routine, `debug` is detail, `warn` is something odd the run handled, `error` is a
|
||||
real failure. Don't put the module name in the message.
|
||||
- New functions are 4 to 20 lines (80 max); files are 500 lines of production code max. The size
|
||||
scripts are ratchets: an allowlisted entry can shrink, never grow, and a new one needs a reason.
|
||||
- `pub` means another crate uses it; everything else is `pub(crate)` or narrower. The lint skips
|
||||
fields, so narrow those by hand.
|
||||
- Every public item gets a `///` doc.
|
||||
|
||||
## Tests
|
||||
|
||||
- Unit tests live in the module; past 150 lines they move to a sibling `foo/tests.rs`. Integration
|
||||
tests are one binary per area: `tests/<area>/main.rs` plus modules.
|
||||
- `core` tests run headless: mock providers, temp dirs, no UI, no network.
|
||||
- An example's tests only run if its `[[example]]` sets `test = true`.
|
||||
- A test has to be able to fail. Watch a regression test fail on the old code first.
|
||||
`scripts/mutants.sh -f <file>` finds tests that can't fail; use it on security and format code.
|
||||
- Pin anything saved or sent against a literal copied from real output (a `.luxe/sessions/*.jsonl`
|
||||
line, a real `gh` reply). A round-trip test can't catch a rename.
|
||||
|
||||
## Comments and docs
|
||||
|
||||
Lean, casual, senior-dev voice. **Less is more.** Comment the *why*: intent, trade-offs, gotchas,
|
||||
constraints a reader would not guess. Never restate what the code says; a comment that paraphrases
|
||||
the next line goes. If a block needs a comment to say *what* it does, extract a named function.
|
||||
- Comment the why: intent, trade-offs, gotchas. Never restate the code. If a block needs a comment
|
||||
to say what it does, make it a named function.
|
||||
- `//!` module docs: `Name — short description.`, a blank line, then how it fits.
|
||||
- `///` item docs: sentences ending in a period (what, why, edge cases). Variants with a payload get
|
||||
an `Args:` line. Short field and variant docs are fragments with no period.
|
||||
- `//` inline: short fragments, no period. Reserved prefixes: `LEARN:`, `NOTE:`, `TODO:`.
|
||||
- Describe the code as it is: no history, ticket numbers or plan references. That's for the commit.
|
||||
- No docs that echo the name (`/// Session ID` on `session_id`), no banners, no commented-out code.
|
||||
- Prose people read follows [`docs/brand/voice.md`](docs/brand/voice.md). How the agent talks lives
|
||||
in `luxe-core::context`'s base harness, never in a mode or tool description.
|
||||
|
||||
- **Module docs (`//!`)**: `Name — short description.`, a blank line, then prose framing the
|
||||
module in the architecture.
|
||||
- **Item docs (`///`)**: sentences ending in a period: what, then why, then edge cases.
|
||||
Payload-carrying variants get an `Args:` line. Label-style field and variant docs are fragments
|
||||
with no period (`/// Configured accounts`).
|
||||
- **Inline (`//`)**: terse fragments, no period. Reserved prefixes: `LEARN:`, `NOTE:`, `TODO:`.
|
||||
- **Shipped source describes the code as it is.** No history ("used to", the incident that taught
|
||||
it), no phase numbers, ticket ids or design-note citations; the story goes in the commit message.
|
||||
Keep the one sentence of why the rule exists.
|
||||
- Flag: docs that echo a name (`/// Session ID` on `session_id`), banners around trivial code,
|
||||
commented-out code, tutorial narration of language basics.
|
||||
## Security
|
||||
|
||||
## Security invariants
|
||||
Enforce these in code (the permission engine or a hook), never in prompt text.
|
||||
|
||||
- **Permissive by default, with always-on guardrails unless `--yolo`:** deny `.env` reads and
|
||||
catastrophic commands, ask on out-of-project work, force-push, commits and GitHub writes. The
|
||||
engine (`luxe-core::permission`) sits at the dispatch choke point, and judges every tool that
|
||||
runs code or reads paths (`test`, `debug`, search roots, `apply` modes), not just `bash`.
|
||||
- **Enforcement is code, never prompt text.** A rule that must hold lives in the permission engine
|
||||
or a hook.
|
||||
- **A project cannot grant itself trust.** Its `.luxe/permissions.json` only tightens, its
|
||||
`.luxe/hooks.json`, `gate.commands`, MCP servers and language servers are honored only under
|
||||
`--trust-project` (or the global-only `lsp.project_servers`), so an "always" approval is written
|
||||
to the **global** file. One answer governs every project-supplied binary
|
||||
(`project_tooling_allowed`); a new consumer reads it, never a second gate. That gate picks which
|
||||
binary Luxe runs, nothing more: a language server still runs `build.rs` and proc macros, so never
|
||||
write that Luxe does not run repository code.
|
||||
- **Secrets:** `0600` files, `$ENV` or `!command`, wrapped in `secrecy::SecretString`, scrubbed by
|
||||
the process-wide `redact::process_secrets()` from tool results, logs and the wire capture.
|
||||
- **No telemetry.** Every outbound call is one the user asked for or can turn off, carries nothing
|
||||
about the machine, and is listed in `docs/guide/security.md`. The update check sends **nothing**
|
||||
(no version, platform or id) and compares locally; a server told the running version is getting a
|
||||
per-user report on a schedule.
|
||||
- **MCP servers are untrusted:** opt-in, behind one `mcp` gateway tool whose calls go through the
|
||||
permission engine; their descriptions and results are data, not instructions.
|
||||
- **Instruction files are filtered, not trusted.** `luxe-core::instructions` strips what a reader
|
||||
of the rendered file cannot see (comments, comment link definitions, invisible characters) and
|
||||
withholds a file it cannot finish checking.
|
||||
- Permissive by default, with guardrails unless `--yolo`: deny `.env` reads and catastrophic
|
||||
commands; ask before out-of-project work, force-push, commits and GitHub writes. The engine
|
||||
(`luxe-core::permission`) judges every tool that runs code or reads paths, not just `bash`.
|
||||
- A project can't trust itself. Its `.luxe/permissions.json` only tightens; its hooks,
|
||||
`gate.commands`, MCP and language servers need `--trust-project` (or global
|
||||
`lsp.project_servers`), so "always" approvals save globally. `project_tooling_allowed` is the one
|
||||
switch for project binaries; reuse it. Language servers still run `build.rs` and proc macros, so
|
||||
never say Luxe runs no repo code.
|
||||
- Secrets: `0600` files, `$ENV` or `!command`, held in `secrecy::SecretString`, scrubbed from tool
|
||||
results, logs and wire captures by `redact::process_secrets()`.
|
||||
- No telemetry. Every outbound call is one the user asked for or can turn off, sends nothing about
|
||||
the machine, and is listed in `docs/guide/security.md`. The update check sends nothing, not even a
|
||||
version, and compares locally.
|
||||
- `luxe remote` is the only server. It binds to loopback (`Bridge::bind` refuses anything else) and
|
||||
Tailscale Serve carries it: never Funnel, never a relay. Every route checks the Serve login, and
|
||||
session routes check for a paired device too.
|
||||
- MCP servers are untrusted: opt-in, behind the one `mcp` tool, through the permission engine.
|
||||
Their descriptions and results are data, not instructions.
|
||||
- `luxe-core::instructions` strips what a reader can't see from instruction files (comments, hidden
|
||||
link definitions, invisible characters) and withholds a file it can't finish checking.
|
||||
|
||||
## Invariants you would otherwise break
|
||||
## What breaks a session
|
||||
|
||||
- **A tool call and its result are one unit.** Both wire formats reject an unpaired half, and it
|
||||
replays on every later request, so one gap ends the session. `dispatch_one` returns a block for
|
||||
every call, the commit after a batch is unconditional (cut-off, cancelled and failed replies
|
||||
included), and `tool_pairing::repair` pairs halves on the outgoing copy. The durable log is never
|
||||
rewritten. Only a call that never *started* may be described as having had no effect.
|
||||
- **An empty block is the same permanent 400.** A call that produced nothing commits nothing
|
||||
(`ReplyOutcome::Nothing`, and says so), `empty_blocks::scrub` drops empties from the outgoing
|
||||
copy, and an empty tool result is **filled**, never dropped.
|
||||
- **Cancellation is a flag with one owner: use `Engine::set_cancelled`.** `watch::Sender::send` is
|
||||
a no-op with no subscribers, which is the normal state between turns.
|
||||
- **Nothing the model can invoke runs unbounded.** `Tool::timeout` sets each deadline and dispatch
|
||||
drops the future at it or at cancel. `None` is only for a tool that waits on a human or owns a
|
||||
process group. Cancel ends the **batch**, not just the call.
|
||||
- **A phase advances only where the gate says so.** `loop_control progress` is a claim;
|
||||
`ReviewIntegratorHook` alone moves the counter, after the commands pass and the reviewer (when
|
||||
on) approves. A reviewer that cannot approve pauses the loop (`GateOutcome::Held`). A refusal
|
||||
reaches the model as the tool's own result.
|
||||
- **Context occupancy is a measurement.** Read it from the last `EntryKind::Usage` after the newest
|
||||
compaction; `estimate_tokens` is the fallback and counts **every** block kind.
|
||||
- **A request's transcript is an *append* to the last one's.** Providers cache an exact prefix, so
|
||||
editing history in flight re-bills everything after the edit at the write rate. Pruning is gated
|
||||
on real window pressure and cuts in whole `prune_min_tokens` steps. Watch the **written** share on
|
||||
`/cost` and in `luxe wire`, not the hit rate.
|
||||
- **The system prompt is byte-identical between turns of a session.** No volatile fact enters it;
|
||||
one-time context is **appended**. Reason about what reaches the model from a capture
|
||||
(`LUXE_WIRE_LOG` plus `luxe wire`), not from source.
|
||||
- **`Action` and `Event` are a published protocol.** `luxe --rpc` streams them to clients nobody
|
||||
here compiles, and an unknown tag degrades to `Other`, so a rename breaks every client and no
|
||||
compiler notices. Adding is free; removing or retagging moves `PROTOCOL_VERSION`. Pinned literals
|
||||
in `tests/event_serialization.rs` hold it.
|
||||
- **`AGENTS.md` is the only project document in the prompt.** Anything else is pointed at.
|
||||
- **A tool must beat the shell command it replaces** (bounded output, fewer round trips, correct
|
||||
defaults, or a capability the shell lacks), and its description names the sibling for the
|
||||
adjacent question.
|
||||
- **Glyphs are polish, never meaning.** Every icon sits beside the words it labels; prefer block
|
||||
- **A tool call and its result are a pair.** Providers reject half a pair on every later request,
|
||||
so one gap ends the session. `dispatch_one` returns a result for every call, a batch always
|
||||
commits (cut off, cancelled or failed too), and `tool_pairing::repair` fixes the outgoing copy.
|
||||
Never rewrite the saved log. Only a call that never started had no effect.
|
||||
- **An empty block is the same permanent 400.** An empty reply commits nothing
|
||||
(`ReplyOutcome::Nothing`), `empty_blocks::scrub` drops empties from the outgoing copy, and an
|
||||
empty tool result is filled, never dropped.
|
||||
- **Cancel with `Engine::set_cancelled`.** `watch::Sender::send` does nothing without subscribers,
|
||||
which is normal between turns.
|
||||
- **Nothing the model runs is unbounded.** `Tool::timeout` sets each deadline; dispatch drops the
|
||||
future at it or on cancel. `None` is only for a tool that waits on a person or owns a process
|
||||
group. Cancel ends the whole batch.
|
||||
- **Only the gate moves a phase.** `loop_control progress` is a claim; `ReviewIntegratorHook` moves
|
||||
the counter once the commands pass and the reviewer (if on) approves, and pauses the loop
|
||||
(`GateOutcome::Held`) if it can't. A refusal reaches the model as the tool's result.
|
||||
- **Context use is measured.** Read it from the last `EntryKind::Usage` after the newest
|
||||
compaction. `estimate_tokens` is the fallback and counts every block kind.
|
||||
- **Each request appends to the last.** Providers cache an exact prefix, so editing history
|
||||
re-bills everything after the edit. Pruning waits for real window pressure and cuts in whole
|
||||
`prune_min_tokens` steps. Watch the written share on `/cost` and in `luxe wire`, not the hit rate.
|
||||
- **The system prompt never changes mid-session.** Nothing volatile goes in it; one-time context is
|
||||
appended. Judge what reaches the model from a capture (`LUXE_WIRE_LOG` plus `luxe wire`).
|
||||
- **`Action` and `Event` are a public protocol.** `luxe --rpc` streams them to clients built
|
||||
elsewhere, and an unknown tag becomes `Other`, so a rename breaks them silently. Adding is free;
|
||||
removing or renaming bumps `PROTOCOL_VERSION`. `tests/event_serialization.rs` pins the literals.
|
||||
|
||||
## Product rules
|
||||
|
||||
- `AGENTS.md` is the only project document in the prompt. Point at anything else.
|
||||
- A tool must beat the shell command it replaces (bounded output, fewer round trips, better
|
||||
defaults, or something the shell can't do), and its description names the sibling tool for the
|
||||
next question.
|
||||
- Glyphs decorate; they never carry meaning alone. Every icon sits beside its words. Prefer block
|
||||
elements, which every terminal draws.
|
||||
- **Platform questions have one owner each:** `user_dirs` (home and Luxe's directories), `program`
|
||||
(finding programs, shells, detaching, killing a process tree), `paths` (path to string),
|
||||
`fs_atomic` (durable replace), `luxe-lsp::uri` (`file://` URLs).
|
||||
- **Generated assets are generated; captures are recorded.** `web/src/assets/generated/` comes from
|
||||
the Rust source and CI checks it. `web/src/assets/captures/` are real sessions recorded by
|
||||
`scripts/site-record.sh`, which nothing can verify; change how Luxe looks and you owe both a
|
||||
`site-assets.sh` and a re-record.
|
||||
- **The wordmark constants are the art** (`luxe-tui/src/widgets/wordmark/art.rs`): one drawing, no
|
||||
- `web/src/assets/generated/` comes from Rust and CI checks it; `captures/` are real sessions from
|
||||
`scripts/site-record.sh`. Change how Luxe looks and you owe a `site-assets.sh` run and a
|
||||
re-record.
|
||||
- The wordmark constants in `luxe-tui/src/widgets/wordmark/art.rs` are the logo: one drawing, no
|
||||
alternatives.
|
||||
- **Two voices, two homes.** Prose a person reads follows `docs/brand/voice.md`; how the agent
|
||||
writes lives in `luxe-core::context`'s base harness, never in a mode or a tool description.
|
||||
|
||||
## Where things live
|
||||
|
||||
- `CONTRIBUTING.md`: process, tooling, publishing. `docs/guide/`: the user guide, also embedded in
|
||||
the binary for `luxe docs`.
|
||||
- `.luxe/todos/<session-id>.json`: the `todo` tool's checklist, one file per session.
|
||||
- A development checkout also has `PROJECT.md` (what must stay true; read it when a design call is
|
||||
ambiguous), `ROADMAP.md`, `PROGRESS.md`, `PLAN.md` and design notes, all excluded from releases.
|
||||
- `CONTRIBUTING.md`: process, tooling, publishing. `docs/guide/`: the user guide, also in
|
||||
`luxe docs`. `.luxe/todos/<session-id>.json`: the `todo` checklist.
|
||||
- A dev checkout also has `PROJECT.md` (what must stay true; read it when a design call is
|
||||
unclear), `ROADMAP.md`, `PROGRESS.md`, `PLAN.md` and design notes. None of them ship.
|
||||
|
||||
161
CHANGELOG.md
161
CHANGELOG.md
@@ -9,6 +9,167 @@ The version on `main` is always the next release. The first change to what ships
|
||||
`Cargo.toml`, `Cargo.lock`) after a release bumps it one step and opens its section here, and later
|
||||
changes add to that section until it ships; `scripts/version-check.py` holds them to it.
|
||||
|
||||
## [0.1.3]
|
||||
|
||||
### Added
|
||||
|
||||
- A session open in the interface can be joined by another program on the same machine while you
|
||||
keep working: `luxe join` speaks the [RPC protocol](docs/guide/rpc.md#join-a-running-session)
|
||||
over a local socket (a named pipe on Windows) only your account can open. A joined client gets
|
||||
the session so far, then every live event, and can answer prompts. It can't switch or end the
|
||||
session, publish to GitHub, or be a program the agent started. `"joinable": false` or
|
||||
`LUXE_JOINABLE=0` turns it off.
|
||||
- A `prompt_closed` event tells every front end when a permission prompt or question stops
|
||||
waiting, so one answered elsewhere comes down everywhere.
|
||||
- `luxe remote` puts the sessions running on your computer in reach of your own phone or tablet,
|
||||
over [Tailscale](docs/guide/remote.md). It listens on this machine only, Tailscale Serve carries
|
||||
it to your devices, and it lets in your own Tailscale login from a device you paired with a QR
|
||||
code. `luxe remote setup` checks Tailscale, publishes the bridge, runs it at sign-in as a user
|
||||
service (systemd, launchd, or the Windows Run key, none needing an administrator), and shows the
|
||||
code. `luxe remote devices` lists and revokes devices.
|
||||
- The bridge can start Luxe in a folder a device picks: in a herdr pane when herdr is running, so
|
||||
the session waits at your desk, or as its own `luxe --rpc` otherwise.
|
||||
- A session started with `luxe --rpc` can be joined, like one in the interface.
|
||||
- An `introduce` action lets a joined client name itself, so the interface says "iPhone joined
|
||||
this session" and "Approved from iPhone" rather than a process id.
|
||||
- The bridge renders messages for the phone app the way the interface draws them: markdown,
|
||||
highlighted code in your theme's colors with a copy control, tool icons, and your own messages
|
||||
without the notes Luxe adds for the agent. The phone parses nothing itself.
|
||||
- From the phone app you can write to a session, steer or stop a running turn, and answer its
|
||||
permission prompts and questions. Like the interface's, a prompt holds every answer but Reject
|
||||
for a moment after it appears, so a tap meant for something else can't grant anything.
|
||||
- The phone app's status line shows the mode behind the interface's mark for it, which a tap
|
||||
switches, and the context, thinking level and model, worded as the interface's status bar words
|
||||
them. A tap on those opens the session's details, where you can switch the model or thinking
|
||||
level and see the cost.
|
||||
- The phone app is set in the same monospace at a size that fits more on a phone, and its session
|
||||
list wears the Luxe wordmark, lit by the same slow drift as the interface's.
|
||||
- The phone app's header names the session's folder and the branch it's on, behind the interface's
|
||||
folder and branch marks, starred when the tree has changes. A long branch is cut first.
|
||||
- The phone app can start a session in a folder you've worked in, or any other you type, and opens
|
||||
it once it's ready. **New** on the session list opens a page for it, where what you type narrows
|
||||
the folders it offers.
|
||||
- The phone app can send photos, from the camera or the library, scaled down on the phone first so
|
||||
they're quick to send.
|
||||
- The phone app's composer has icon buttons for attaching a photo (a paperclip), sending (an arrow
|
||||
in the accent color, dim until there's something to send) and stopping, and a line over the box
|
||||
while a turn runs that says whether sending steers it or waits for it to end.
|
||||
- The phone app offers the slash commands it can carry out (`/compact`, `/cost`, `/dock`, `/mode`,
|
||||
`/model` and `/thinking`) as you type a `/`, and project files to mention as you type an `@`.
|
||||
- The phone app shows the Context Dock, with the session's plan and changed files: a rail beside
|
||||
the conversation on an iPad or an open iPhone Duo, on the side your `dock` settings choose,
|
||||
which a header button and `/dock` show and hide. On a phone the header keeps to one menu button,
|
||||
and the dock is the first tab of the panel it opens. A long plan scrolls in a box of its own
|
||||
that keeps the work in hand in view, as the interface's rail does. Tapping a file opens its diff.
|
||||
- The phone app has a project panel, behind the header's menu button. Its Files tab is the
|
||||
interface's FILES tree, with its guides, icons and change marks, and shows a file highlighted
|
||||
as the interface shows it, with a button that mentions it in your message. On a wide screen the
|
||||
panel is a rail in the dock's place, and a file or a diff opens beside it, with the conversation
|
||||
still in view where there's room, as the interface peeks beside its dock.
|
||||
- The phone app's project panel has a GitHub tab: `/github`'s sections and rows, and a pull
|
||||
request or issue opened beside them with its overview and conversation, drawn as the interface
|
||||
draws them. It reads only, and **Mention** adds the item to your message.
|
||||
- The phone app's project panel has a Review tab: the open Guided Review's guide, a chapter opened
|
||||
beside it with the guide's notes over its diffs, and **Reviewed** and **Next** to walk it. It
|
||||
joins the review the interface has open, through the new `show_open_review` RPC action, whose
|
||||
answer also says what each chapter asks of the reader. The panel's bar names the project and its
|
||||
branch with the interface's marks, as the interface's PROJECT section does.
|
||||
- The phone app asks about picked lines, as the interface's File view does, in a file, a diff or a
|
||||
review chapter: tap line numbers to pick them, or select text the phone's way, down to part of a
|
||||
line, and **Ask** adds the path and lines to your message in the interface's words.
|
||||
- The phone app comments on a review chapter: on a picked line or run of lines, or on the chapter,
|
||||
as a comment or a question, saved or sent at once. Comments show under what they're about with
|
||||
the agent's replies, and take a reply, resolve, send or delete; the guide sends everything unsent.
|
||||
- The phone app is named for the computer it reaches, as Tailscale names it: on the Home Screen,
|
||||
beside the Luxe mark in the session list, and after the project in notifications. One phone can
|
||||
keep an app for each of several computers and tell them apart.
|
||||
- Your own phone joining a session no longer adds "joined" and "left" lines to the conversation
|
||||
each time it reconnects: the dock's SESSION block names who's joined instead. Someone else's
|
||||
device is still announced.
|
||||
- Escape on an iPad keyboard closes the phone app's sheet straight after a pane opens or closes,
|
||||
where it used to be lost for a frame.
|
||||
- The phone app's icon buttons (close, project, dock) are square, 44 points to tap, and a press or
|
||||
a keyboard's focus shows Luxe's own square rather than the browser's rounded highlight.
|
||||
- The phone app's headings are the interface's badges (PROJECT, PLAN, FILES, MODEL, SESSION), cut
|
||||
the same way at the same depths, or bold words when `statusline.style` is flat.
|
||||
- The phone app can be added to the home screen, where it opens full screen with the Luxe mark in
|
||||
your theme's colors as its icon. An app on the home screen keeps its own sign-in apart from
|
||||
Safari's, so one that needs pairing again takes the invitation link pasted in.
|
||||
- The phone app can [notify you](docs/guide/remote.md#notifications) when a session waits on an
|
||||
answer or finishes a long turn. It's off until you turn it on, one device at a time, and a
|
||||
notification says only what's waiting and where. The bridge seals each one for your device and
|
||||
posts it to your device's own push service, with no Luxe server in between.
|
||||
- The protocol sends [`turn_started`](docs/guide/rpc.md) when a turn begins, from any client, and a
|
||||
client joining mid-turn gets it while catching up. A turn sent from your phone shows as running
|
||||
in the terminal right away, not from its first word.
|
||||
- The protocol answers [`list_models`](docs/guide/rpc.md) with the models you can switch to, and
|
||||
the thinking levels each one honors, so a client can offer the same choices as `/model`.
|
||||
- A joined client can introduce itself with `watch_only`, to read a session without the interface
|
||||
announcing it. Luxe drops anything else it sends. The bridge watches for notifications this way.
|
||||
- An `AGENTS.md` in a folder the agent works in now reaches it, even when Luxe started somewhere
|
||||
else in the repository. It's added to the first tool result that reaches the folder, once, so
|
||||
the system prompt and the provider's cache are untouched. Luxe shows "Following" and the file.
|
||||
See [Project context](docs/guide/project-context.md#folders-the-agent-works-in);
|
||||
`LUXE_NESTED_INSTRUCTIONS=0` turns it off.
|
||||
|
||||
### Changed
|
||||
|
||||
- A session is open in one Luxe at a time. Resuming one that another Luxe has open is refused, and
|
||||
the message says which process has it, rather than two of them writing the same history.
|
||||
- `message_committed` now announces the messages you send too, as the protocol guide always said
|
||||
it did, so a joined client sees the question as well as the answer, and the interface shows a
|
||||
message a joined client sent. A `--rpc` client gets its own message back once Luxe saves it.
|
||||
- The turn timer reads like a tool card's time: `42s`, then `3m 07s`, then `1h 05m`.
|
||||
- `luxe --help` opens with "A beautiful coding agent for your terminal."
|
||||
|
||||
### Fixed
|
||||
|
||||
- A tool call the model writes while an earlier one is still running shows as waiting, not as a
|
||||
second spinner. Only one card spins at a time again.
|
||||
- A running `bash` card is titled with what the command is for, as soon as the model has written
|
||||
that. It used to show the command in both the title and the body until the command finished.
|
||||
- A message with an image, sent while a turn runs, is queued for its own turn. Steering it
|
||||
delivered the words and dropped the image.
|
||||
- A compaction is in the conversation from the moment it starts ("Compacting the conversation —
|
||||
579.4k of context…"), and that line becomes the result when it ends. A minute of summary showed
|
||||
nothing but the activity bar while the dock was open. The phone app shows it too.
|
||||
- Typing in the phone app keeps up in a long session. It drew every item the session ever had,
|
||||
and Safari slows each keystroke as a page passes a few thousand (a 9,000-item session took about
|
||||
100ms a key on a desktop). It now draws the latest 300, with **Show earlier** for the rest.
|
||||
- A long session no longer stalls before every model call. Luxe re-read and parsed the whole
|
||||
session file each time, about 70ms on a 68 MB session. It now keeps the session it's writing in
|
||||
memory and reads only what was added, about 10ms.
|
||||
- A turn no longer spends seconds saving the project for **Last Turn** review. Every turn saved
|
||||
two full copies of every file, synced to disk one by one (about 11 seconds a turn in a project
|
||||
of 4,000 files), and never deleted any, so one project's `.luxe/reviews/turn-baselines` reached
|
||||
18 GB. A copy now links the files the last one already holds, which takes under a second, and
|
||||
copies the last turn doesn't need are deleted once they're an hour old.
|
||||
- A long session's memory climbs much more slowly. Every model call copied the whole
|
||||
conversation, every tool result included, to send the part after the last compaction. It now
|
||||
copies only what it sends: over 20 turns on a 68 MB session, Luxe grew by 65 MB instead of
|
||||
338 MB.
|
||||
- Drawing a long session with screenshots in it is cheaper while a reply streams. Every tool
|
||||
card holding an image was laid out again on every frame, even scrolled far out of view, which
|
||||
in a session with a hundred screenshots was most of what the terminal UI allocated. A card is
|
||||
now laid out again only when its image changes.
|
||||
- A phone joining a long session costs the computer less. Each join copied the whole
|
||||
conversation, then built the message carrying it in one buffer that doubled as it grew. It's
|
||||
now sent from the copy Luxe already keeps, in pieces: over 120 joins of a 68 MB session, 26 GB
|
||||
less allocated, and each join about 20ms faster.
|
||||
- Compaction no longer fails more often the longer a session runs. The summarizer was asked for a
|
||||
list of every file the session touched and shown the last summary's list to copy, though Luxe
|
||||
keeps that list itself and replaced whatever the summarizer wrote. On a long session the list
|
||||
grew to over 500 paths, and copying it used up the summary's output limit, so the compaction
|
||||
failed with "the summary stopped short". The summarizer now writes only the prose, and its
|
||||
output limit is 32K tokens, up from 16K.
|
||||
- `web_search` with several phrasings gives each its share of the results. Without a key, they
|
||||
went to Exa as one search ranked against the first phrasing, so the others rarely showed. Each
|
||||
phrasing is now its own search, and the results take turns: the top of each, then the next.
|
||||
- `web_search` says when Exa's free search is rate limited, and which phrasing it turned away. The
|
||||
notice used to read as a search that found nothing.
|
||||
- A `grep` glob starting with `!` excludes, as `rg -g '!target/**'` does. It used to match no file,
|
||||
so the search came back empty.
|
||||
|
||||
## [0.1.2]
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -295,6 +295,47 @@ cd web && npm run build && npm run check
|
||||
cd web && npm run deploy # needs `wrangler login` once
|
||||
```
|
||||
|
||||
## The phone app
|
||||
|
||||
`luxe remote`'s app is plain HTML, CSS and ES modules in `crates/luxe-remote/app/`, with nothing
|
||||
built. A debug build reads that folder from disk, so run `cargo run -p luxe-cli -- remote` and
|
||||
reload. Preact, htm and Signals are vendored byte for byte, and a test holds them to
|
||||
`app/vendor/manifest.json`:
|
||||
|
||||
```bash
|
||||
scripts/remote-vendor.sh # fetch the pinned versions again (needs npm)
|
||||
scripts/remote-fonts.sh # subset the fonts again (after site-fonts.sh)
|
||||
```
|
||||
|
||||
Colors come from `/theme.css`, the active theme read through `luxe_core::theme::css`, the same
|
||||
mapping the website's theme CSS uses. Each `var(--luxe-*)` in the app's stylesheet keeps a fallback
|
||||
for a theme that leaves a color to the terminal. `app/generated/wordmark.svg` comes from the TUI's
|
||||
art through `scripts/site-assets.sh`, which checks it with the website's assets.
|
||||
|
||||
The transcript is a pure fold of the session's frames (`app/transcript.js`), tested with Node's
|
||||
own runner against a session recorded through the bridge (`app-tests/fixtures/turn.jsonl`). The
|
||||
tests resolve imports through `index.html`'s import map, so they run the vendored files the phone
|
||||
runs. The app's types are JSDoc, checked by `tsc` in strict mode; the tests get a looser config.
|
||||
`tsc` reads expressions inside a template but not the props `htm` hands a component, so a
|
||||
component's `@param` is what it promises, not something checked at the call. The end-to-end test
|
||||
starts a session against a mock model, a bridge and a pairing, then drives a whole turn in WebKit,
|
||||
permission and diff included. CI runs all three in its `phone app` job:
|
||||
|
||||
```bash
|
||||
cd crates/luxe-remote/app-tests
|
||||
npm ci # TypeScript, Playwright, and the vendored libraries' types
|
||||
npm run check # tsc
|
||||
npm test # unit tests
|
||||
cargo build -p luxe-cli && npm run e2e # end to end; LUXE_BIN picks another luxe
|
||||
```
|
||||
|
||||
Every iOS browser is WebKit, so check the app there. Playwright's WebKit build wants Ubuntu's ICU,
|
||||
libxml2 and flite, which a rolling distro doesn't have. Without root, unpack those three `.deb`s
|
||||
from the Ubuntu archive into the `minibrowser-wpe/sys/lib` of a copy of
|
||||
`~/.cache/ms-playwright/webkit-*`, and point `PLAYWRIGHT_BROWSERS_PATH` at the copy. A screenshot
|
||||
adds a `<style>` of Playwright's own, which the app's policy refuses and the console reports; the
|
||||
page never had it.
|
||||
|
||||
## Versioning
|
||||
|
||||
One version for the whole workspace (`[workspace.package]` in `Cargo.toml`), following semver, and
|
||||
@@ -312,6 +353,15 @@ python3 scripts/version-check.py origin/main # what the pull request check ru
|
||||
python3 scripts/version_check_test.py # the check's own tests
|
||||
```
|
||||
|
||||
The tags live in the public repository. A checkout that isn't a clone of it (a private development
|
||||
repository, a fork) names it as the `public` remote, and the check reads its tags from there, so a
|
||||
release made since your last fetch still counts:
|
||||
|
||||
```bash
|
||||
git remote add public https://github.com/evanwhittaker/luxe.git
|
||||
git remote set-url --push public DISABLED-use-a-pull-request
|
||||
```
|
||||
|
||||
Below 1.0, a minor bump may break config, flags or the `--rpc` protocol; a patch never does.
|
||||
|
||||
## Releasing
|
||||
|
||||
263
Cargo.lock
generated
263
Cargo.lock
generated
@@ -152,6 +152,57 @@ version = "1.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
|
||||
|
||||
[[package]]
|
||||
name = "axum"
|
||||
version = "0.8.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90"
|
||||
dependencies = [
|
||||
"axum-core",
|
||||
"base64",
|
||||
"bytes",
|
||||
"futures-util",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"hyper",
|
||||
"hyper-util",
|
||||
"itoa",
|
||||
"matchit",
|
||||
"memchr",
|
||||
"mime",
|
||||
"percent-encoding",
|
||||
"pin-project-lite",
|
||||
"serde_core",
|
||||
"serde_json",
|
||||
"serde_path_to_error",
|
||||
"sha1",
|
||||
"sync_wrapper",
|
||||
"tokio",
|
||||
"tokio-tungstenite",
|
||||
"tower",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "axum-core"
|
||||
version = "0.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"http",
|
||||
"http-body",
|
||||
"http-body-util",
|
||||
"mime",
|
||||
"pin-project-lite",
|
||||
"sync_wrapper",
|
||||
"tower-layer",
|
||||
"tower-service",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base16ct"
|
||||
version = "1.0.0"
|
||||
@@ -616,6 +667,12 @@ dependencies = [
|
||||
"syn 2.0.118",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "data-encoding"
|
||||
version = "2.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06"
|
||||
|
||||
[[package]]
|
||||
name = "deltae"
|
||||
version = "0.3.2"
|
||||
@@ -778,7 +835,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1237,6 +1294,12 @@ version = "1.10.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
|
||||
|
||||
[[package]]
|
||||
name = "httpdate"
|
||||
version = "1.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9"
|
||||
|
||||
[[package]]
|
||||
name = "hybrid-array"
|
||||
version = "0.4.15"
|
||||
@@ -1260,6 +1323,7 @@ dependencies = [
|
||||
"http",
|
||||
"http-body",
|
||||
"httparse",
|
||||
"httpdate",
|
||||
"itoa",
|
||||
"pin-project-lite",
|
||||
"smallvec",
|
||||
@@ -1626,7 +1690,7 @@ checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
|
||||
|
||||
[[package]]
|
||||
name = "luxe-cli"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -1638,8 +1702,10 @@ dependencies = [
|
||||
"luxe-lsp",
|
||||
"luxe-mcp",
|
||||
"luxe-providers",
|
||||
"luxe-remote",
|
||||
"luxe-tools",
|
||||
"luxe-tui",
|
||||
"qrcode",
|
||||
"reqwest",
|
||||
"secrecy",
|
||||
"semver",
|
||||
@@ -1656,7 +1722,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-core"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base16ct",
|
||||
@@ -1687,7 +1753,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-dap"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"luxe-core",
|
||||
@@ -1701,7 +1767,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-lsp"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base16ct",
|
||||
@@ -1725,7 +1791,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-mcp"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"futures-util",
|
||||
@@ -1745,7 +1811,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-providers"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64",
|
||||
@@ -1762,15 +1828,39 @@ dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "luxe-remote"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"base16ct",
|
||||
"base64",
|
||||
"futures",
|
||||
"getrandom 0.4.3",
|
||||
"luxe-core",
|
||||
"reqwest",
|
||||
"ring",
|
||||
"rust-embed",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2 0.11.0",
|
||||
"tempfile",
|
||||
"thiserror 1.0.69",
|
||||
"tokio",
|
||||
"tokio-tungstenite",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "luxe-tools"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
"base64",
|
||||
"dom_smoothie",
|
||||
"encoding_rs",
|
||||
"futures",
|
||||
"globset",
|
||||
"htmd",
|
||||
"image",
|
||||
@@ -1789,7 +1879,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-tui"
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"arboard",
|
||||
@@ -1869,6 +1959,12 @@ dependencies = [
|
||||
"regex-automata",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matchit"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.2"
|
||||
@@ -1896,6 +1992,16 @@ version = "0.3.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||
|
||||
[[package]]
|
||||
name = "mime_guess"
|
||||
version = "2.0.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
|
||||
dependencies = [
|
||||
"mime",
|
||||
"unicase",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
@@ -1988,7 +2094,7 @@ version = "0.50.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||
dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2370,6 +2476,15 @@ version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "precomputed-hash"
|
||||
version = "0.1.1"
|
||||
@@ -2402,6 +2517,12 @@ version = "0.1.30"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
|
||||
|
||||
[[package]]
|
||||
name = "qrcode"
|
||||
version = "0.14.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d68782463e408eb1e668cf6152704bd856c78c5b6417adaee3203d8f4c1fc9ec"
|
||||
|
||||
[[package]]
|
||||
name = "quick-error"
|
||||
version = "2.0.1"
|
||||
@@ -2494,6 +2615,16 @@ dependencies = [
|
||||
"rand_core 0.6.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41"
|
||||
dependencies = [
|
||||
"rand_chacha",
|
||||
"rand_core 0.9.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.10.2"
|
||||
@@ -2505,12 +2636,31 @@ dependencies = [
|
||||
"rand_core 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core 0.9.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.9.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c"
|
||||
dependencies = [
|
||||
"getrandom 0.3.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.10.1"
|
||||
@@ -2723,6 +2873,41 @@ dependencies = [
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-embed"
|
||||
version = "8.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "19afa5b4b6a611de00bd1bdae6ae6f39084c9399f0679c3f52d8469cf335cc23"
|
||||
dependencies = [
|
||||
"rust-embed-impl",
|
||||
"rust-embed-utils",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-embed-impl"
|
||||
version = "8.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0d8afda6374eac59e066abee06d265247ebbaf3006cf878e2879e8356e34053"
|
||||
dependencies = [
|
||||
"mime_guess",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rust-embed-utils",
|
||||
"syn 2.0.118",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust-embed-utils"
|
||||
version = "8.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d84e8ba78bd384263e5922f084cbe1b081c3b7e69add59c8fb097b879ba968a"
|
||||
dependencies = [
|
||||
"sha2 0.11.0",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-hash"
|
||||
version = "2.1.3"
|
||||
@@ -2748,7 +2933,7 @@ dependencies = [
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2902,6 +3087,17 @@ dependencies = [
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_path_to_error"
|
||||
version = "0.1.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"serde",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_urlencoded"
|
||||
version = "0.7.1"
|
||||
@@ -2923,6 +3119,17 @@ dependencies = [
|
||||
"stable_deref_trait",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha1"
|
||||
version = "0.10.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a978451301f4db1d02937a4ab3ccce137717b81826e79b7d49ffe3244a13c3b8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures 0.2.17",
|
||||
"digest 0.10.7",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.10.9"
|
||||
@@ -3200,10 +3407,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
|
||||
dependencies = [
|
||||
"fastrand",
|
||||
"getrandom 0.3.4",
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix",
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3438,6 +3645,18 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-tungstenite"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"log",
|
||||
"tokio",
|
||||
"tungstenite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-util"
|
||||
version = "0.7.18"
|
||||
@@ -3651,6 +3870,22 @@ version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||
|
||||
[[package]]
|
||||
name = "tungstenite"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c01152af293afb9c7c2a57e4b559c5620b421f6d133261c60dd2d0cdb38e6b8"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"data-encoding",
|
||||
"http",
|
||||
"httparse",
|
||||
"log",
|
||||
"rand 0.9.5",
|
||||
"sha1",
|
||||
"thiserror 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.20.1"
|
||||
@@ -4004,7 +4239,7 @@ version = "0.1.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
|
||||
dependencies = [
|
||||
"windows-sys 0.59.0",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -6,7 +6,7 @@ members = ["crates/*"]
|
||||
# Every crate here releases together from one workspace, so the versions move in lockstep and each
|
||||
# crate inherits this one. The internal `path` dependencies carry the same number explicitly — cargo
|
||||
# uses the path locally and the version on crates.io, and refuses to publish a path dep without one.
|
||||
version = "0.1.2"
|
||||
version = "0.1.3"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/evanwhittaker/luxe"
|
||||
|
||||
@@ -3,7 +3,7 @@ name = "luxe-cli"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
description = "A terminal coding agent: streaming chat, tools, guided diff review, and LSP/DAP code intelligence"
|
||||
description = "A beautiful coding agent for your terminal."
|
||||
repository.workspace = true
|
||||
homepage.workspace = true
|
||||
readme = "../../README.md"
|
||||
@@ -18,22 +18,29 @@ name = "luxe"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.2" }
|
||||
luxe-providers = { path = "../luxe-providers", version = "0.1.2" }
|
||||
luxe-tools = { path = "../luxe-tools", version = "0.1.2" }
|
||||
luxe-lsp = { path = "../luxe-lsp", version = "0.1.2" }
|
||||
luxe-dap = { path = "../luxe-dap", version = "0.1.2" }
|
||||
luxe-mcp = { path = "../luxe-mcp", version = "0.1.2" }
|
||||
luxe-tui = { path = "../luxe-tui", version = "0.1.2" }
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.3" }
|
||||
luxe-providers = { path = "../luxe-providers", version = "0.1.3" }
|
||||
luxe-tools = { path = "../luxe-tools", version = "0.1.3" }
|
||||
luxe-lsp = { path = "../luxe-lsp", version = "0.1.3" }
|
||||
luxe-dap = { path = "../luxe-dap", version = "0.1.3" }
|
||||
luxe-mcp = { path = "../luxe-mcp", version = "0.1.3" }
|
||||
luxe-tui = { path = "../luxe-tui", version = "0.1.3" }
|
||||
luxe-remote = { path = "../luxe-remote", version = "0.1.3", optional = true }
|
||||
# The pairing QR in the terminal. Without its defaults it is the encoder and text renderer alone,
|
||||
# with no dependencies.
|
||||
qrcode = { version = "0.14", default-features = false, optional = true }
|
||||
anyhow = { workspace = true }
|
||||
clap = { workspace = true }
|
||||
shlex = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
# `signal`: `luxe remote` runs until Ctrl-C, and stops serving cleanly when it comes
|
||||
tokio = { workspace = true, features = ["signal"] }
|
||||
uuid = { workspace = true }
|
||||
secrecy = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
# A joiner's transcript is encoded from the shared snapshot through a borrowed copy of its shape
|
||||
serde = { workspace = true }
|
||||
async-trait = { workspace = true }
|
||||
# One request, for the update notice: a public release manifest, carrying nothing about the user.
|
||||
# See `luxe_core::update` for why that constraint is load-bearing rather than a nicety.
|
||||
@@ -49,10 +56,12 @@ semver = { workspace = true }
|
||||
tempfile = "3"
|
||||
# The guide check: what Luxe would silently ignore in a config example
|
||||
serde_ignored = "0.1"
|
||||
serde = { workspace = true }
|
||||
|
||||
[features]
|
||||
default = ["clipboard-images"]
|
||||
default = ["clipboard-images", "remote"]
|
||||
# `luxe remote`, the only server in Luxe. A packager can leave it out to ship a Luxe with no
|
||||
# server code at all.
|
||||
remote = ["dep:luxe-remote", "dep:qrcode"]
|
||||
clipboard = ["luxe-tui/clipboard"]
|
||||
clipboard-images = ["luxe-tui/clipboard-images"]
|
||||
|
||||
|
||||
@@ -68,16 +68,17 @@ impl ToolActivityProjector {
|
||||
self.composing(call_id, name, subject.as_deref(), body.as_ref())
|
||||
}
|
||||
Event::ToolStarted { call_id, name } => {
|
||||
self.started(call_id, name).into_iter().collect()
|
||||
self.started(call_id, name).into_iter().chain(self.fit_writing()).collect()
|
||||
}
|
||||
Event::ToolProgress { call_id, chunk } => {
|
||||
self.progressed(call_id, chunk).into_iter().collect()
|
||||
}
|
||||
Event::ToolCompleted { call_id, is_error, output } => {
|
||||
self.settle(call_id, *is_error, Some(bounded_display(output))).into_iter().collect()
|
||||
let settled = self.settle(call_id, *is_error, Some(bounded_display(output)));
|
||||
settled.into_iter().chain(self.fit_writing()).collect()
|
||||
}
|
||||
Event::ToolEnded { call_id, is_error } => {
|
||||
self.ended(call_id, *is_error).into_iter().collect()
|
||||
self.ended(call_id, *is_error).into_iter().chain(self.fit_writing()).collect()
|
||||
}
|
||||
_ => Vec::new(),
|
||||
}
|
||||
@@ -95,6 +96,35 @@ impl ToolActivityProjector {
|
||||
Some(open.clone())
|
||||
}
|
||||
|
||||
/// How the card of a call still being written should read: waiting while another call runs,
|
||||
/// since that one is the busy card and this one can't run before it, and working otherwise.
|
||||
fn writing_state(&self, call_id: &str) -> ToolActivityState {
|
||||
let running = self
|
||||
.calls
|
||||
.iter()
|
||||
.any(|(id, open)| id != call_id && open.state == ToolActivityState::Running);
|
||||
if running {
|
||||
ToolActivityState::Queued
|
||||
} else {
|
||||
ToolActivityState::Composing
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves the card of the call being written between waiting and working as a call ahead of
|
||||
/// it starts or stops.
|
||||
fn fit_writing(&mut self) -> Option<ToolActivity> {
|
||||
let call_id = self.writing.clone()?;
|
||||
let state = self.writing_state(&call_id);
|
||||
let open = self.calls.get_mut(&call_id)?;
|
||||
let unsent = matches!(open.state, ToolActivityState::Composing | ToolActivityState::Queued);
|
||||
if !unsent || open.state == state {
|
||||
return None;
|
||||
}
|
||||
open.revision += 1;
|
||||
open.state = state;
|
||||
Some(open.clone())
|
||||
}
|
||||
|
||||
/// Opens (or fills in) the card for the call the model is writing right now.
|
||||
///
|
||||
/// `Composing` is a *working* state — the model is producing this call as the reader watches,
|
||||
@@ -105,7 +135,9 @@ impl ToolActivityProjector {
|
||||
/// written; whichever came before it has been written in full and is merely waiting its turn.
|
||||
/// Saying that is what keeps exactly one card looking busy — two spinning `bash` cards read as
|
||||
/// two shells at once, and a paused mark on all of them put a stopped sign over a card that
|
||||
/// was visibly filling in.
|
||||
/// was visibly filling in. An earlier call can run while this one is written, though, and then
|
||||
/// that one is the busy card: this one opens `Queued`, since it can't run before it either,
|
||||
/// and [`Self::fit_writing`] puts it back to work if the model is still writing it.
|
||||
///
|
||||
/// What it does not yet have is a batch position — the message has not committed, so how many
|
||||
/// calls it asked for is not known — or a body, whose payload is the argument still arriving.
|
||||
@@ -126,10 +158,17 @@ impl ToolActivityProjector {
|
||||
}
|
||||
|
||||
let label = subject.map(bounded_display).unwrap_or_default();
|
||||
let state = self.writing_state(call_id);
|
||||
match self.calls.get_mut(call_id) {
|
||||
// The call growing under the card that is already drawing it: the subject arriving, or
|
||||
// another few lines of the payload. Said twice, neither is worth a revision.
|
||||
Some(open) if open.state == ToolActivityState::Composing => {
|
||||
// another few lines of the payload. Said twice, neither is worth a revision. Queued
|
||||
// here is a card held behind a running call, since only the call being written composes.
|
||||
Some(open)
|
||||
if matches!(
|
||||
open.state,
|
||||
ToolActivityState::Composing | ToolActivityState::Queued
|
||||
) =>
|
||||
{
|
||||
let renamed = !label.is_empty() && open.label != label;
|
||||
let grew = body.is_some_and(|body| open.card_body.as_ref() != Some(body));
|
||||
if renamed || grew {
|
||||
@@ -150,6 +189,7 @@ impl ToolActivityProjector {
|
||||
let mut activity =
|
||||
ToolActivity::composing(session_id, call_id, name, label, now_ms());
|
||||
activity.card_body = body.cloned();
|
||||
activity.state = state;
|
||||
self.calls.insert(call_id.to_owned(), activity.clone());
|
||||
updates.push(activity);
|
||||
}
|
||||
|
||||
@@ -336,6 +336,68 @@ fn exactly_one_card_is_working_at_a_time() {
|
||||
);
|
||||
}
|
||||
|
||||
/// A call written while an earlier one runs waits under that one, and works again once it's over.
|
||||
///
|
||||
/// Calls run as soon as they're written, so the model can be writing one while another runs. The
|
||||
/// break this catches: opening the written call as `Composing` regardless, which puts a spinner on
|
||||
/// it beside the running card, with a paused one between them that reads as stuck.
|
||||
#[test]
|
||||
fn a_call_written_while_another_runs_waits_behind_it() {
|
||||
let mut projector = ToolActivityProjector::default();
|
||||
projector.project(&Event::SessionLoaded { session_id: "session-a".into(), messages: vec![] });
|
||||
let compose = |id: &str| Event::ToolCallComposing {
|
||||
call_id: id.into(),
|
||||
name: "edit".into(),
|
||||
subject: Some(format!("{id}.rs")),
|
||||
body: None,
|
||||
};
|
||||
let start = |id: &str| Event::ToolStarted { call_id: id.into(), name: "edit".into() };
|
||||
let finish = |id: &str| Event::ToolCompleted {
|
||||
call_id: id.into(),
|
||||
is_error: false,
|
||||
output: "Applied".into(),
|
||||
};
|
||||
let steps: Vec<(&str, Event)> = vec![
|
||||
("the model writes the first edit", compose("a")),
|
||||
("which runs as soon as it's written", start("a")),
|
||||
("while the model writes the second", compose("b")),
|
||||
("and the third", compose("c")),
|
||||
("the first finishes", finish("a")),
|
||||
("the second runs", start("b")),
|
||||
("and finishes, with the third still being written", finish("b")),
|
||||
];
|
||||
|
||||
let mut shape = Vec::new();
|
||||
for (note, event) in steps {
|
||||
projector.project(&event);
|
||||
let mut states: Vec<(String, ToolActivityState)> =
|
||||
projector.calls.iter().map(|(id, row)| (id.clone(), row.state)).collect();
|
||||
states.sort_by(|left, right| left.0.cmp(&right.0));
|
||||
let working = states.iter().filter(|(_, state)| state.is_working()).count();
|
||||
assert!(working <= 1, "{note}: {working} cards claim to be working: {states:?}");
|
||||
shape.push((note, states.into_iter().map(|(_, state)| state).collect::<Vec<_>>()));
|
||||
}
|
||||
|
||||
use ToolActivityState::{Composing, Queued, Running, Succeeded};
|
||||
assert_eq!(
|
||||
shape,
|
||||
[
|
||||
("the model writes the first edit", vec![Composing]),
|
||||
("which runs as soon as it's written", vec![Running]),
|
||||
("while the model writes the second", vec![Running, Queued]),
|
||||
("and the third", vec![Running, Queued, Queued]),
|
||||
// For the instant between one call ending and the next starting, writing the third
|
||||
// is the only thing happening; the next event takes it back
|
||||
("the first finishes", vec![Succeeded, Queued, Composing]),
|
||||
("the second runs", vec![Succeeded, Running, Queued]),
|
||||
(
|
||||
"and finishes, with the third still being written",
|
||||
vec![Succeeded, Succeeded, Composing]
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// A refused or cancelled call is answered without ever being dispatched. Its card is the one
|
||||
/// the model's writing opened, and it has to settle from there or it spins forever.
|
||||
#[test]
|
||||
|
||||
@@ -8,8 +8,12 @@ mod commands;
|
||||
mod docs_command;
|
||||
mod doctor_command;
|
||||
pub(crate) mod headless;
|
||||
mod join_command;
|
||||
mod models_command;
|
||||
#[cfg(feature = "remote")]
|
||||
mod remote_command;
|
||||
mod rpc;
|
||||
mod share;
|
||||
mod tui;
|
||||
pub(crate) mod update_command;
|
||||
mod wire_command;
|
||||
@@ -22,6 +26,7 @@ use auth_command::run_auth_command;
|
||||
use commands::{run_loop_command, run_mcp_command, run_sessions_command};
|
||||
use doctor_command::run_doctor_command;
|
||||
use headless::run_headless;
|
||||
use join_command::run_join_command;
|
||||
use models_command::run_models_command;
|
||||
use tui::run_tui;
|
||||
use wire_command::run_wire_command;
|
||||
@@ -107,9 +112,7 @@ pub(crate) async fn run(mut args: Args) -> anyhow::Result<()> {
|
||||
match cmd {
|
||||
SubCommand::Auth(auth_args) => return run_auth_command(auth_args.command).await,
|
||||
SubCommand::Init(init_args) => return crate::init::run_init(init_args, settings).await,
|
||||
SubCommand::Loop(loop_args) => {
|
||||
return run_loop_command(loop_args.command, settings).await;
|
||||
}
|
||||
SubCommand::Loop(looping) => return run_loop_command(looping.command, settings).await,
|
||||
SubCommand::Mcp(mcp_args) => return run_mcp_command(mcp_args.command, &settings).await,
|
||||
SubCommand::Models(models_args) => {
|
||||
return run_models_command(models_args.command, settings).await
|
||||
@@ -118,6 +121,9 @@ pub(crate) async fn run(mut args: Args) -> anyhow::Result<()> {
|
||||
return run_sessions_command(sessions_args.command, settings).await
|
||||
}
|
||||
SubCommand::Doctor => return run_doctor_command(settings).await,
|
||||
SubCommand::Join(join) => return run_join_command(join, &settings.work_root).await,
|
||||
#[cfg(feature = "remote")]
|
||||
SubCommand::Remote(remote) => return remote_command::run_remote_command(remote).await,
|
||||
SubCommand::Wire(wire_args) => return run_wire_command(wire_args),
|
||||
SubCommand::Docs(_) | SubCommand::Update(_) => {
|
||||
unreachable!("`luxe docs` and `luxe update` return before a project is entered")
|
||||
|
||||
@@ -8,7 +8,7 @@ mod permission_mode;
|
||||
pub(crate) use commands::*;
|
||||
pub(crate) use permission_mode::PermissionModeArg;
|
||||
|
||||
/// Luxe — a fast, lean, security-conscious agentic coding harness.
|
||||
/// Luxe's command line. `--help` shows the crate description from `Cargo.toml`, not this doc.
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(name = "luxe", version, about)]
|
||||
pub(crate) struct Args {
|
||||
@@ -183,11 +183,14 @@ impl Args {
|
||||
| SubCommand::Docs(_)
|
||||
| SubCommand::Doctor
|
||||
| SubCommand::Init(_)
|
||||
| SubCommand::Join(_)
|
||||
| SubCommand::Models(_)
|
||||
| SubCommand::Sessions(_)
|
||||
| SubCommand::Update(_)
|
||||
| SubCommand::Wire(_),
|
||||
) => false,
|
||||
#[cfg(feature = "remote")]
|
||||
Some(SubCommand::Remote(_)) => false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,12 +256,15 @@ impl Args {
|
||||
| SubCommand::Docs(_)
|
||||
| SubCommand::Doctor
|
||||
| SubCommand::Init(_)
|
||||
| SubCommand::Join(_)
|
||||
| SubCommand::Mcp(_)
|
||||
| SubCommand::Models(_)
|
||||
| SubCommand::Sessions(_)
|
||||
| SubCommand::Update(_)
|
||||
| SubCommand::Wire(_),
|
||||
) => false,
|
||||
#[cfg(feature = "remote")]
|
||||
Some(SubCommand::Remote(_)) => false,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,12 @@ pub(crate) enum SubCommand {
|
||||
Doctor,
|
||||
/// Set Luxe up: a theme, a provider, the tools and the start screen.
|
||||
Init(InitArgs),
|
||||
/// Join a session another Luxe on this machine is running, speaking the `--rpc` protocol on
|
||||
/// stdin and stdout.
|
||||
Join(JoinArgs),
|
||||
/// Serve your sessions to your own phone or tablet, through Tailscale.
|
||||
#[cfg(feature = "remote")]
|
||||
Remote(RemoteArgs),
|
||||
/// Run the agent through a phased plan on its own.
|
||||
Loop(LoopArgs),
|
||||
/// Add, list and sign in to MCP servers.
|
||||
@@ -139,6 +145,70 @@ pub(crate) struct InitArgs {
|
||||
pub(crate) defaults: bool,
|
||||
}
|
||||
|
||||
/// Join arguments.
|
||||
#[derive(Parser, Debug)]
|
||||
pub(crate) struct JoinArgs {
|
||||
/// The session: its id or the start of it, or the process id of the Luxe running it. Omit
|
||||
/// it to join the one running in this folder.
|
||||
pub(crate) target: Option<String>,
|
||||
/// List the sessions running on this machine that can be joined, and exit.
|
||||
#[arg(long, conflicts_with = "target")]
|
||||
pub(crate) list: bool,
|
||||
}
|
||||
|
||||
/// Remote arguments.
|
||||
#[cfg(feature = "remote")]
|
||||
#[derive(Parser, Debug)]
|
||||
pub(crate) struct RemoteArgs {
|
||||
#[command(subcommand)]
|
||||
pub(crate) command: Option<RemoteCommand>,
|
||||
/// The port to listen on, on this machine only.
|
||||
#[arg(long, default_value_t = 4519, global = true)]
|
||||
pub(crate) port: u16,
|
||||
/// Also let this tailnet login in, besides this machine's owner (repeatable).
|
||||
#[arg(long = "allow", value_name = "LOGIN", global = true)]
|
||||
pub(crate) allow: Vec<String>,
|
||||
}
|
||||
|
||||
/// Remote subcommand — pair and manage your devices.
|
||||
#[cfg(feature = "remote")]
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub(crate) enum RemoteCommand {
|
||||
/// Check Tailscale, publish the bridge on your tailnet, start it at login, and pair a device.
|
||||
Setup,
|
||||
/// Show a QR code and link that pair one phone or tablet, good for ten minutes.
|
||||
Pair,
|
||||
/// List the paired devices.
|
||||
Devices {
|
||||
#[command(subcommand)]
|
||||
command: Option<DevicesCommand>,
|
||||
},
|
||||
/// Manage the user service that runs the bridge at login.
|
||||
Service {
|
||||
#[command(subcommand)]
|
||||
command: ServiceCommand,
|
||||
},
|
||||
}
|
||||
|
||||
/// Service subcommand.
|
||||
#[cfg(feature = "remote")]
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub(crate) enum ServiceCommand {
|
||||
/// Stop the service and stop it starting at login.
|
||||
Remove,
|
||||
}
|
||||
|
||||
/// Devices subcommand.
|
||||
#[cfg(feature = "remote")]
|
||||
#[derive(Subcommand, Debug)]
|
||||
pub(crate) enum DevicesCommand {
|
||||
/// Unpair a device and close its open sessions.
|
||||
Revoke {
|
||||
/// The device's id, from `luxe remote devices`
|
||||
id: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Sessions arguments.
|
||||
#[derive(Parser, Debug)]
|
||||
pub(crate) struct SessionsArgs {
|
||||
|
||||
128
crates/luxe-cli/src/cli/join_command.rs
Normal file
128
crates/luxe-cli/src/cli/join_command.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
//! `luxe join` — a session another Luxe is running, over stdin and stdout.
|
||||
//!
|
||||
//! The running Luxe shares its session on a local endpoint (`cli::share`), and this command is the
|
||||
//! plain way in: it picks the session from the records in the runtime folder, connects, and copies
|
||||
//! bytes both ways. What crosses is the `--rpc` protocol untouched, so a client written for
|
||||
//! `luxe --rpc` drives a joined session by launching `luxe join` instead.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use anyhow::Context;
|
||||
use luxe_core::local_endpoint::{self, LiveSession};
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
use super::args::JoinArgs;
|
||||
|
||||
/// List the joinable sessions, or join one and pass the protocol through until either side ends.
|
||||
pub(crate) async fn run_join_command(args: JoinArgs, work_root: &Path) -> anyhow::Result<()> {
|
||||
let running = local_endpoint::running_sessions(&luxe_core::user_dirs::luxe_runtime_dir());
|
||||
if args.list {
|
||||
print!("{}", listing(&running));
|
||||
return Ok(());
|
||||
}
|
||||
let cwd = work_root.to_string_lossy();
|
||||
let session = pick(&running, args.target.as_deref(), &cwd)?;
|
||||
let stream = local_endpoint::connect(&session.address)
|
||||
.await
|
||||
.with_context(|| format!("could not join session {}", session.session_id))?;
|
||||
pass_through(stream).await
|
||||
}
|
||||
|
||||
/// The session `target` names, or with no target the only one running in `cwd`.
|
||||
fn pick<'a>(
|
||||
running: &'a [LiveSession],
|
||||
target: Option<&str>,
|
||||
cwd: &str,
|
||||
) -> anyhow::Result<&'a LiveSession> {
|
||||
let matching: Vec<&LiveSession> = match target {
|
||||
Some(target) => running.iter().filter(|session| names(session, target)).collect(),
|
||||
None => running.iter().filter(|session| session.cwd == cwd).collect(),
|
||||
};
|
||||
match matching.as_slice() {
|
||||
[only] => Ok(only),
|
||||
[] => anyhow::bail!("{}", nothing_to_join(running, target, cwd)),
|
||||
several => anyhow::bail!(
|
||||
"{} sessions match; name one by id or process:\n{}",
|
||||
several.len(),
|
||||
listing_of(several.iter().copied())
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `target` is the session's process id or the start of its id.
|
||||
fn names(session: &LiveSession, target: &str) -> bool {
|
||||
let target = target.trim();
|
||||
!target.is_empty()
|
||||
&& (session.session_id.starts_with(target) || target == session.pid.to_string())
|
||||
}
|
||||
|
||||
fn nothing_to_join(running: &[LiveSession], target: Option<&str>, cwd: &str) -> String {
|
||||
let wanted = match target {
|
||||
Some(target) => format!("no running session matches `{target}`"),
|
||||
None => format!("no session is running in {cwd}"),
|
||||
};
|
||||
if running.is_empty() {
|
||||
return format!(
|
||||
"{wanted}, and none is running on this machine that can be joined. A Luxe shares \
|
||||
its session unless `joinable` is off or LUXE_JOINABLE=0."
|
||||
);
|
||||
}
|
||||
format!("{wanted}. These can be joined:\n{}", listing(running))
|
||||
}
|
||||
|
||||
fn listing(running: &[LiveSession]) -> String {
|
||||
listing_of(running.iter())
|
||||
}
|
||||
|
||||
fn listing_of<'a>(sessions: impl Iterator<Item = &'a LiveSession>) -> String {
|
||||
sessions
|
||||
.map(|session| format!("{} pid {} {}\n", session.session_id, session.pid, session.cwd))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Copy stdin to the session and the session to stdout. Ends when the session closes the
|
||||
/// connection, which it also does once it has read the end of our stdin.
|
||||
async fn pass_through(stream: local_endpoint::LocalStream) -> anyhow::Result<()> {
|
||||
let (mut from_session, mut to_session) = tokio::io::split(stream);
|
||||
let mut input = read_stdin();
|
||||
let upload = tokio::spawn(async move {
|
||||
while let Some(chunk) = input.recv().await {
|
||||
if to_session.write_all(&chunk).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let _ = to_session.shutdown().await;
|
||||
});
|
||||
let mut stdout = tokio::io::stdout();
|
||||
let downloaded = tokio::io::copy(&mut from_session, &mut stdout).await;
|
||||
upload.abort();
|
||||
downloaded.context("the joined session's output stopped")?;
|
||||
stdout.flush().await.context("could not write to stdout")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Stdin, read on a thread of its own. Tokio's stdin reads on its blocking pool, and the runtime
|
||||
/// waits for that read on the way out, so a session that closes first would leave `luxe join`
|
||||
/// hanging until the next keystroke. A plain thread is simply abandoned at exit.
|
||||
fn read_stdin() -> tokio::sync::mpsc::Receiver<Vec<u8>> {
|
||||
let (chunks, received) = tokio::sync::mpsc::channel(16);
|
||||
std::thread::spawn(move || {
|
||||
let mut stdin = std::io::stdin().lock();
|
||||
let mut buffer = vec![0; 8192];
|
||||
loop {
|
||||
let read = match std::io::Read::read(&mut stdin, &mut buffer) {
|
||||
Ok(0) => return,
|
||||
Ok(read) => read,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(error) => return tracing::warn!(%error, "stopped reading stdin"),
|
||||
};
|
||||
if chunks.blocking_send(buffer[..read].to_vec()).is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
received
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
75
crates/luxe-cli/src/cli/join_command/tests.rs
Normal file
75
crates/luxe-cli/src/cli/join_command/tests.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use super::*;
|
||||
|
||||
fn running(pid: u32, session_id: &str, cwd: &str) -> LiveSession {
|
||||
LiveSession {
|
||||
pid,
|
||||
session_id: session_id.to_string(),
|
||||
cwd: cwd.to_string(),
|
||||
address: format!("/run/luxe/{pid}.sock"),
|
||||
luxe_version: "0.1.3".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn sessions() -> Vec<LiveSession> {
|
||||
vec![
|
||||
running(101, "aaaa-1111", "/work/app"),
|
||||
running(202, "aaab-2222", "/work/site"),
|
||||
running(303, "cccc-3333", "/work/site"),
|
||||
]
|
||||
}
|
||||
|
||||
/// With no target, the session running in this folder is the one joined.
|
||||
///
|
||||
/// The break this catches: ignoring the folder, so `luxe join` in one project lands in another's
|
||||
/// session, or refuses because more than one Luxe is running somewhere.
|
||||
#[test]
|
||||
fn no_target_joins_the_session_running_here() {
|
||||
let all = sessions();
|
||||
assert_eq!(pick(&all, None, "/work/app").unwrap().pid, 101);
|
||||
}
|
||||
|
||||
/// Two sessions in one folder are named, not guessed between.
|
||||
///
|
||||
/// The break this catches: taking the first match, which joins whichever pid sorts lower.
|
||||
#[test]
|
||||
fn two_sessions_in_the_folder_are_listed_not_guessed() {
|
||||
let all = sessions();
|
||||
let error = pick(&all, None, "/work/site").unwrap_err().to_string();
|
||||
assert!(error.contains("2 sessions match"), "{error}");
|
||||
assert!(error.contains("aaab-2222") && error.contains("cccc-3333"), "{error}");
|
||||
}
|
||||
|
||||
/// A target names a session by the start of its id or by its Luxe's process id.
|
||||
///
|
||||
/// The break this catches: matching the id exactly, so the short form the listing invites fails,
|
||||
/// or not accepting a pid, which is what `ps` and herdr show.
|
||||
#[test]
|
||||
fn a_target_is_an_id_prefix_or_a_pid() {
|
||||
let all = sessions();
|
||||
assert_eq!(pick(&all, Some("cccc"), "/elsewhere").unwrap().pid, 303);
|
||||
assert_eq!(pick(&all, Some("202"), "/elsewhere").unwrap().session_id, "aaab-2222");
|
||||
}
|
||||
|
||||
/// A prefix two ids share is refused, and so is an empty one.
|
||||
///
|
||||
/// The break this catches: an empty target matching every id as a prefix of all of them.
|
||||
#[test]
|
||||
fn an_ambiguous_or_empty_target_is_refused() {
|
||||
let all = sessions();
|
||||
assert!(pick(&all, Some("aaa"), "/work/app").is_err());
|
||||
assert!(!names(&all[0], " "));
|
||||
}
|
||||
|
||||
/// Nothing to join says why, and how sharing is turned back on when nothing is running.
|
||||
///
|
||||
/// The break this catches: a bare "not found" when every Luxe has `joinable` off, which leaves the
|
||||
/// reader with nothing to try.
|
||||
#[test]
|
||||
fn nothing_to_join_says_what_is_running() {
|
||||
let none = pick(&[], None, "/work/app").unwrap_err().to_string();
|
||||
assert!(none.contains("joinable"), "{none}");
|
||||
let all = sessions();
|
||||
let elsewhere = pick(&all, None, "/work/other").unwrap_err().to_string();
|
||||
assert!(elsewhere.contains("no session is running in /work/other"), "{elsewhere}");
|
||||
assert!(elsewhere.contains("aaaa-1111 pid 101 /work/app"), "{elsewhere}");
|
||||
}
|
||||
152
crates/luxe-cli/src/cli/remote_command.rs
Normal file
152
crates/luxe-cli/src/cli/remote_command.rs
Normal file
@@ -0,0 +1,152 @@
|
||||
//! `luxe remote` — the bridge that serves this machine's sessions to your own devices.
|
||||
//!
|
||||
//! The serving is `luxe_remote`'s; this is the terminal around it: who is let in, where it
|
||||
//! listens, setting it up, pairing and revoking devices, and stopping on Ctrl-C.
|
||||
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use anyhow::Context;
|
||||
use luxe_remote::{Access, DeviceBook, Tailnet};
|
||||
|
||||
use super::args::{DevicesCommand, RemoteArgs, RemoteCommand, ServiceCommand};
|
||||
use crate::output::outln;
|
||||
|
||||
mod invitation;
|
||||
mod render;
|
||||
mod setup;
|
||||
|
||||
/// Runs `luxe remote` and its subcommands.
|
||||
pub(crate) async fn run_remote_command(args: RemoteArgs) -> anyhow::Result<()> {
|
||||
let devices = DeviceBook::at(DeviceBook::default_path());
|
||||
match args.command {
|
||||
None => serve(args.port, args.allow, devices).await,
|
||||
Some(RemoteCommand::Setup) => setup::setup(args.port, args.allow, &devices).await,
|
||||
Some(RemoteCommand::Pair) => invitation::pair(args.port, &devices).await,
|
||||
Some(RemoteCommand::Devices { command: None }) => list_devices(&devices),
|
||||
Some(RemoteCommand::Devices { command: Some(DevicesCommand::Revoke { id }) }) => {
|
||||
revoke(&devices, &id)
|
||||
}
|
||||
Some(RemoteCommand::Service { command: ServiceCommand::Remove }) => remove_service(),
|
||||
}
|
||||
}
|
||||
|
||||
fn remove_service() -> anyhow::Result<()> {
|
||||
match luxe_remote::service::remove()? {
|
||||
true => outln!("The bridge's service is stopped and won't start at login."),
|
||||
false => outln!("No service for the bridge is installed."),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Serves until Ctrl-C.
|
||||
async fn serve(port: u16, allow: Vec<String>, devices: DeviceBook) -> anyhow::Result<()> {
|
||||
let tailnet = luxe_remote::this_machine().await;
|
||||
let machine = tailnet.as_ref().ok().map(|tailnet| tailnet.machine().to_owned());
|
||||
let logins = allowed_logins(tailnet, allow)?;
|
||||
let access = Access { logins: logins.clone(), devices };
|
||||
let bridge = bridge(port, access, machine).await?;
|
||||
outln!("Luxe remote is listening on http://{}. Ctrl-C stops it.", bridge.address());
|
||||
outln!("It lets in {}, from a paired device.", logins.join(", "));
|
||||
bridge
|
||||
.serve(async {
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
})
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The bridge `luxe remote` runs: starting sessions with this luxe, drawing them as the TUI does,
|
||||
/// and named for the computer when Tailscale said what it's called.
|
||||
async fn bridge(
|
||||
port: u16,
|
||||
access: Access,
|
||||
machine: Option<String>,
|
||||
) -> anyhow::Result<luxe_remote::Bridge> {
|
||||
let address = SocketAddr::from((Ipv4Addr::LOCALHOST, port));
|
||||
let runtime_dir = luxe_core::user_dirs::luxe_runtime_dir();
|
||||
let luxe = std::env::current_exe()
|
||||
.map_err(|error| anyhow::anyhow!("could not find this luxe to start sessions: {error}"))?;
|
||||
let bridge = luxe_remote::Bridge::bind(address, runtime_dir, access).await?;
|
||||
let bridge = bridge.starting_sessions(luxe);
|
||||
let bridge = bridge.rendering_with(std::sync::Arc::new(render::TuiRenderer));
|
||||
Ok(match machine {
|
||||
Some(machine) => bridge.named(machine),
|
||||
None => bridge,
|
||||
})
|
||||
}
|
||||
|
||||
/// The owner, as Tailscale reports them, plus any `--allow`ed logins. Without the owner, only an
|
||||
/// explicit `--allow` lets anyone in: guessing would mean an allowlist nobody chose.
|
||||
fn allowed_logins(
|
||||
owner: Result<Tailnet, luxe_remote::TailnetError>,
|
||||
mut allow: Vec<String>,
|
||||
) -> anyhow::Result<Vec<String>> {
|
||||
match owner {
|
||||
Ok(tailnet) => allow.insert(0, tailnet.login),
|
||||
Err(error) if allow.is_empty() => {
|
||||
return Err(error).context(
|
||||
"couldn't learn this machine's tailnet login; pass `--allow <login>` to name who \
|
||||
may use the bridge",
|
||||
);
|
||||
}
|
||||
Err(error) => tracing::warn!(%error, "serving only the --allow logins"),
|
||||
}
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
allow.retain(|login| seen.insert(login.clone()));
|
||||
Ok(allow)
|
||||
}
|
||||
|
||||
fn list_devices(devices: &DeviceBook) -> anyhow::Result<()> {
|
||||
let paired = devices.devices();
|
||||
if paired.is_empty() {
|
||||
outln!("No devices are paired. Pair one with `luxe remote pair`.");
|
||||
return Ok(());
|
||||
}
|
||||
let now = SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |since| since.as_secs());
|
||||
for device in paired {
|
||||
let age = age(now.saturating_sub(device.paired_at));
|
||||
outln!(" {} {} ({}, paired {age})", device.id, device.name, device.login);
|
||||
}
|
||||
outln!("\nUnpair one with `luxe remote devices revoke <id>`.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn revoke(devices: &DeviceBook, id: &str) -> anyhow::Result<()> {
|
||||
match devices.revoke(id)? {
|
||||
Some(device) => outln!("Unpaired {} ({id}); its open sessions close too.", device.name),
|
||||
None => anyhow::bail!("no paired device has the id {id}; see `luxe remote devices`"),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// How long ago, in the largest whole unit that fits.
|
||||
fn age(seconds: u64) -> String {
|
||||
const DAY: u64 = 24 * 60 * 60;
|
||||
match seconds / DAY {
|
||||
0 => "today".to_string(),
|
||||
1 => "yesterday".to_string(),
|
||||
days => format!("{days} days ago"),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The owner always leads the allowlist, and nobody is let in when no one was named.
|
||||
///
|
||||
/// The break this catches: serving an empty allowlist when Tailscale can't be asked (which
|
||||
/// shuts everyone out with no reason given), or dropping the owner once `--allow` is used.
|
||||
#[test]
|
||||
fn the_allowlist_is_the_owner_and_whoever_is_named() {
|
||||
let owner = || Ok(Tailnet { login: "alex@github".into(), dns_name: "desk".into() });
|
||||
let missing = || Err(luxe_remote::TailnetError::NotInstalled);
|
||||
let both = allowed_logins(owner(), vec!["sam@github".into()]).unwrap();
|
||||
assert_eq!(both, ["alex@github", "sam@github"]);
|
||||
let repeated = allowed_logins(owner(), vec!["alex@github".into()]).unwrap();
|
||||
assert_eq!(repeated, ["alex@github"]);
|
||||
assert_eq!(allowed_logins(missing(), vec!["sam@github".into()]).unwrap(), ["sam@github"]);
|
||||
assert!(allowed_logins(missing(), vec![]).is_err());
|
||||
}
|
||||
}
|
||||
80
crates/luxe-cli/src/cli/remote_command/invitation.rs
Normal file
80
crates/luxe-cli/src/cli/remote_command/invitation.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
//! The invitation a phone scans: a QR code in the terminal, and the same link in words.
|
||||
|
||||
use std::io::Write;
|
||||
use std::net::{Ipv4Addr, SocketAddr, TcpStream};
|
||||
use std::time::Duration;
|
||||
|
||||
use luxe_remote::{DeviceBook, Tailnet};
|
||||
use qrcode::render::unicode::Dense1x2;
|
||||
use qrcode::QrCode;
|
||||
|
||||
use crate::output::outln;
|
||||
|
||||
/// `luxe remote pair`: one invitation, shown.
|
||||
pub(super) async fn pair(port: u16, devices: &DeviceBook) -> anyhow::Result<()> {
|
||||
match luxe_remote::this_machine().await {
|
||||
Ok(tailnet) => show(port, devices, &tailnet),
|
||||
Err(error) => {
|
||||
let token = devices.invite()?;
|
||||
outln!("Tailscale couldn't name this machine ({error}).");
|
||||
outln!("Open /pair on the bridge's tailnet address and add #{token} to it.");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Issues an invitation and shows it as a QR code and a link to `https://<this machine>/pair`.
|
||||
///
|
||||
/// The invitation rides in the link's fragment, which a browser never sends to a server, so it
|
||||
/// stays out of Serve's and the bridge's logs.
|
||||
pub(super) fn show(port: u16, devices: &DeviceBook, tailnet: &Tailnet) -> anyhow::Result<()> {
|
||||
let token = devices.invite()?;
|
||||
let link = format!("https://{}/pair#{token}", tailnet.dns_name);
|
||||
if let Some(code) = qr(&link) {
|
||||
outln!("{code}");
|
||||
}
|
||||
outln!("On the phone or tablet: install Tailscale, sign in as {}, then", tailnet.login);
|
||||
outln!("scan this or open the link. It pairs one device, within ten minutes.\n");
|
||||
outln!(" {link}\n");
|
||||
if !bridge_listening(port) {
|
||||
outln!("The bridge isn't running yet: start it with `luxe remote` before scanning.");
|
||||
}
|
||||
let _ = std::io::stdout().flush();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `text` as a QR code drawn in half-height blocks, light modules as blocks so it reads on the
|
||||
/// dark background most terminals have.
|
||||
fn qr(text: &str) -> Option<String> {
|
||||
let code = QrCode::new(text.as_bytes()).ok()?;
|
||||
let drawn = code
|
||||
.render::<Dense1x2>()
|
||||
.dark_color(Dense1x2::Light)
|
||||
.light_color(Dense1x2::Dark)
|
||||
.quiet_zone(true)
|
||||
.build();
|
||||
Some(drawn)
|
||||
}
|
||||
|
||||
/// Whether something answers on the bridge's port.
|
||||
pub(super) fn bridge_listening(port: u16) -> bool {
|
||||
let address = SocketAddr::from((Ipv4Addr::LOCALHOST, port));
|
||||
TcpStream::connect_timeout(&address, Duration::from_millis(500)).is_ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The code is drawn with modules as blocks and a quiet zone a camera can find.
|
||||
///
|
||||
/// The break this catches: losing the quiet zone or the inversion, either of which leaves a
|
||||
/// code most phone cameras won't pick out of a dark terminal.
|
||||
#[test]
|
||||
fn the_code_has_a_quiet_zone_of_blocks() {
|
||||
let code = qr("https://desk.tail1234.ts.net/pair#abc").unwrap();
|
||||
let first = code.lines().next().unwrap();
|
||||
assert!(first.chars().all(|c| c == '\u{2588}'), "the border is solid: {first:?}");
|
||||
assert!(code.lines().count() > 10);
|
||||
}
|
||||
}
|
||||
255
crates/luxe-cli/src/cli/remote_command/render.rs
Normal file
255
crates/luxe-cli/src/cli/remote_command/render.rs
Normal file
@@ -0,0 +1,255 @@
|
||||
//! The TUI's renderer, handed to the bridge so the phone draws text the way the terminal does.
|
||||
|
||||
use luxe_remote::{StatusFacts, StatusSegment, StatusTone};
|
||||
use luxe_tui::markdown::html;
|
||||
use luxe_tui::statusline::plain;
|
||||
|
||||
/// Markdown, highlighting, tool glyphs and the status line's wording from `luxe-tui`.
|
||||
pub(super) struct TuiRenderer;
|
||||
|
||||
impl luxe_remote::Renderer for TuiRenderer {
|
||||
fn markdown(&self, text: &str, voice: luxe_remote::Voice) -> String {
|
||||
let voice = match voice {
|
||||
luxe_remote::Voice::Answer => html::Voice::Answer,
|
||||
luxe_remote::Voice::Reasoning => html::Voice::Reasoning,
|
||||
};
|
||||
html::render(text, voice)
|
||||
}
|
||||
|
||||
fn tool_glyph(&self, tool: &str) -> String {
|
||||
// the app's font has the Nerd Font marks whatever this terminal can draw
|
||||
luxe_tui::widgets::tool_icon(tool).at(luxe_tui::glyphs::GlyphTier::Nerd).to_string()
|
||||
}
|
||||
|
||||
fn status(&self, facts: &StatusFacts) -> Vec<StatusSegment> {
|
||||
let facts = plain::Facts {
|
||||
model: facts.model.as_ref(),
|
||||
thinking: facts.thinking,
|
||||
context: facts.context.map(|usage| (usage.used_tokens, usage.context_window)),
|
||||
plan: facts.plan.as_ref(),
|
||||
session: facts.session.as_ref(),
|
||||
};
|
||||
plain::segments(&facts).into_iter().map(status_segment).collect()
|
||||
}
|
||||
|
||||
fn image_glyph(&self) -> String {
|
||||
luxe_tui::glyphs::Icon::Image.at(luxe_tui::glyphs::GlyphTier::Nerd).to_string()
|
||||
}
|
||||
|
||||
fn code_line(&self, text: &str, language: Option<&str>) -> String {
|
||||
html::code_line(text, language)
|
||||
}
|
||||
|
||||
fn todo_mark(&self, status: luxe_core::TodoSummaryStatus) -> String {
|
||||
luxe_tui::marks::todo(status, luxe_tui::glyphs::GlyphTier::Nerd).to_string()
|
||||
}
|
||||
|
||||
fn change_mark(&self, status: luxe_core::workspace::FileChangeStatus) -> String {
|
||||
luxe_tui::marks::change(status).to_string()
|
||||
}
|
||||
|
||||
fn file_mark(&self, name: &str) -> luxe_remote::FileMark {
|
||||
// the extension, as the TUI's tree reads it for a file's mark
|
||||
let extension = name.rsplit_once('.').map_or("", |(_, extension)| extension);
|
||||
let icon = luxe_tui::widgets::language_icon(&extension.to_ascii_lowercase());
|
||||
let glyph = icon.at(luxe_tui::glyphs::GlyphTier::Nerd).to_string();
|
||||
luxe_remote::FileMark { glyph, hue: mark_hue(luxe_tui::widgets::language_hue_role(icon)) }
|
||||
}
|
||||
|
||||
fn folder_mark(&self) -> String {
|
||||
luxe_tui::glyphs::Icon::Folder.at(luxe_tui::glyphs::GlyphTier::Nerd).to_string()
|
||||
}
|
||||
|
||||
fn branch_mark(&self) -> String {
|
||||
luxe_tui::glyphs::Icon::Branch.at(luxe_tui::glyphs::GlyphTier::Nerd).to_string()
|
||||
}
|
||||
|
||||
fn mode_mark(&self, mode: &str) -> String {
|
||||
luxe_tui::modes::icon(mode).at(luxe_tui::glyphs::GlyphTier::Nerd).to_string()
|
||||
}
|
||||
|
||||
fn github_marks(&self) -> luxe_remote::GithubMarks {
|
||||
use luxe_tui::glyphs::{GlyphTier::Nerd, Icon, Mark};
|
||||
luxe_remote::GithubMarks {
|
||||
pull_request: Icon::PullRequest.at(Nerd).to_owned(),
|
||||
issue: Icon::Issue.at(Nerd).to_owned(),
|
||||
yes: Mark::Yes.at(Nerd).to_owned(),
|
||||
no: Mark::No.at(Nerd).to_owned(),
|
||||
waiting: Mark::Waiting.at(Nerd).to_owned(),
|
||||
elision: Mark::Elision.at(Nerd).to_owned(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ago(&self, at: u64, now: u64) -> String {
|
||||
luxe_tui::github_detail::ago(at, now)
|
||||
}
|
||||
|
||||
fn github_readable(&self, body: &str) -> String {
|
||||
luxe_tui::github_detail::readable(body)
|
||||
}
|
||||
|
||||
fn home_icon(&self, side: u32, colors: luxe_remote::TileColors) -> Option<Vec<u8>> {
|
||||
luxe_tui::widgets::tile_png(side, colors.plate, colors.ink)
|
||||
.inspect_err(|error| tracing::warn!(%error, side, "could not draw the app icon"))
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
fn mark_hue(hue: luxe_tui::widgets::LanguageHue) -> luxe_remote::MarkHue {
|
||||
use luxe_remote::MarkHue;
|
||||
use luxe_tui::widgets::LanguageHue;
|
||||
match hue {
|
||||
LanguageHue::Warning => MarkHue::Warning,
|
||||
LanguageHue::Info => MarkHue::Info,
|
||||
LanguageHue::Success => MarkHue::Success,
|
||||
LanguageHue::Error => MarkHue::Error,
|
||||
LanguageHue::ThinkingUltra => MarkHue::ThinkingUltra,
|
||||
LanguageHue::Text => MarkHue::Text,
|
||||
LanguageHue::Muted => MarkHue::Muted,
|
||||
}
|
||||
}
|
||||
|
||||
fn status_segment(segment: plain::Segment) -> StatusSegment {
|
||||
let kind = match segment.kind {
|
||||
plain::Kind::Context => "context",
|
||||
plain::Kind::Cost => "cost",
|
||||
plain::Kind::Thinking => "thinking",
|
||||
plain::Kind::Model => "model",
|
||||
};
|
||||
let tone = match segment.tone {
|
||||
plain::Tone::Plain => StatusTone::Plain,
|
||||
plain::Tone::Warning => StatusTone::Warning,
|
||||
plain::Tone::Error => StatusTone::Error,
|
||||
};
|
||||
StatusSegment { kind, glyph: segment.glyph.map(str::to_string), label: segment.label, tone }
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use luxe_remote::{Renderer, Voice};
|
||||
|
||||
use super::TuiRenderer;
|
||||
|
||||
/// The break this catches: the voices crossed, so reasoning loses its line breaks and an
|
||||
/// answer keeps the model's.
|
||||
#[test]
|
||||
fn each_voice_reaches_the_tui_as_itself() {
|
||||
assert_eq!(TuiRenderer.markdown("a\nb", Voice::Reasoning), "<p>a<br>b</p>");
|
||||
assert_eq!(TuiRenderer.markdown("a\nb", Voice::Answer), "<p>a b</p>");
|
||||
}
|
||||
|
||||
/// The break this catches: the glyph taken at this terminal's tier, which is ASCII or none
|
||||
/// under a plain terminal, though the app's font always has the marks.
|
||||
#[test]
|
||||
fn tool_glyphs_are_the_nerd_font_marks() {
|
||||
let nerd = luxe_tui::widgets::tool_icon("bash").at(luxe_tui::glyphs::GlyphTier::Nerd);
|
||||
luxe_tui::glyphs::set_tier(luxe_tui::glyphs::GlyphTier::Ascii);
|
||||
let glyph = TuiRenderer.tool_glyph("bash");
|
||||
let image = TuiRenderer.image_glyph();
|
||||
let done = TuiRenderer.todo_mark(luxe_core::TodoSummaryStatus::Completed);
|
||||
luxe_tui::glyphs::set_tier(luxe_tui::glyphs::GlyphTier::Nerd);
|
||||
assert_eq!(glyph, nerd);
|
||||
let nerd_image = luxe_tui::glyphs::Icon::Image.at(luxe_tui::glyphs::GlyphTier::Nerd);
|
||||
assert_eq!(image, nerd_image, "an attached image's mark too");
|
||||
let nerd_done = luxe_tui::glyphs::Mark::Yes.at(luxe_tui::glyphs::GlyphTier::Nerd);
|
||||
assert_eq!(done, nerd_done, "a todo's mark too");
|
||||
}
|
||||
|
||||
/// The break this catches: a diff row's code left plain, or a changed file's letter crossed,
|
||||
/// so the phone's dock and diffs stop matching the TUI's.
|
||||
#[test]
|
||||
fn diffs_and_changes_reach_the_tui() {
|
||||
let code = TuiRenderer.code_line("fn a() {}", Some("rs"));
|
||||
assert!(code.starts_with("<span class=\"t-accent-secondary\">fn</span>"), "{code}");
|
||||
let modified = luxe_core::workspace::FileChangeStatus::Modified;
|
||||
assert_eq!(TuiRenderer.change_mark(modified), "M");
|
||||
}
|
||||
|
||||
/// The break this catches: a file's mark read from the whole name or a cased extension, a
|
||||
/// language's hue crossed with another's, or the folder's mark taken at this terminal's tier,
|
||||
/// so the phone's tree stops matching the TUI's.
|
||||
#[test]
|
||||
fn tree_marks_are_the_tui_language_marks() {
|
||||
use luxe_remote::MarkHue;
|
||||
use luxe_tui::glyphs::{GlyphTier, Icon};
|
||||
let nerd = |icon: Icon| icon.at(GlyphTier::Nerd).to_string();
|
||||
luxe_tui::glyphs::set_tier(GlyphTier::Ascii);
|
||||
let rust = TuiRenderer.file_mark("main.RS");
|
||||
let toml = TuiRenderer.file_mark("Cargo.toml");
|
||||
let folder = TuiRenderer.folder_mark();
|
||||
luxe_tui::glyphs::set_tier(GlyphTier::Nerd);
|
||||
assert_eq!(
|
||||
rust,
|
||||
luxe_remote::FileMark { glyph: nerd(Icon::LangRust), hue: MarkHue::Warning }
|
||||
);
|
||||
assert_eq!(
|
||||
toml,
|
||||
luxe_remote::FileMark { glyph: nerd(Icon::LangToml), hue: MarkHue::Error }
|
||||
);
|
||||
assert_eq!(TuiRenderer.file_mark("Makefile").hue, MarkHue::Muted);
|
||||
assert_eq!(folder, nerd(Icon::Folder));
|
||||
}
|
||||
|
||||
/// The break this catches: the project panel's branch drawn with another mark than the TUI's
|
||||
/// project section, or at this terminal's tier rather than the phone's.
|
||||
#[test]
|
||||
fn the_branch_mark_is_the_tui_branch_mark() {
|
||||
use luxe_tui::glyphs::{GlyphTier, Icon};
|
||||
luxe_tui::glyphs::set_tier(GlyphTier::Ascii);
|
||||
let branch = TuiRenderer.branch_mark();
|
||||
luxe_tui::glyphs::set_tier(GlyphTier::Nerd);
|
||||
assert_eq!(branch, Icon::Branch.at(GlyphTier::Nerd));
|
||||
}
|
||||
|
||||
/// The break this catches: a GitHub mark taken at this terminal's tier or crossed with
|
||||
/// another, ages in other words than the TUI's, or a body's HTML left as tags.
|
||||
#[test]
|
||||
fn github_reaches_the_tui_marks_and_words() {
|
||||
use luxe_tui::glyphs::{GlyphTier, Icon, Mark};
|
||||
luxe_tui::glyphs::set_tier(GlyphTier::Ascii);
|
||||
let marks = TuiRenderer.github_marks();
|
||||
luxe_tui::glyphs::set_tier(GlyphTier::Nerd);
|
||||
assert_eq!(marks.pull_request, Icon::PullRequest.at(GlyphTier::Nerd));
|
||||
assert_eq!(marks.issue, Icon::Issue.at(GlyphTier::Nerd));
|
||||
assert_eq!(marks.yes, Mark::Yes.at(GlyphTier::Nerd));
|
||||
assert_eq!(marks.no, Mark::No.at(GlyphTier::Nerd));
|
||||
assert_eq!(marks.waiting, Mark::Waiting.at(GlyphTier::Nerd));
|
||||
assert_eq!(marks.elision, Mark::Elision.at(GlyphTier::Nerd));
|
||||
assert_eq!(TuiRenderer.ago(1_000, 1_000 + 3 * 3600), "3h ago");
|
||||
let body = TuiRenderer.github_readable("<details><summary>Notes</summary>x</details>");
|
||||
assert!(!body.contains('<'), "{body}");
|
||||
}
|
||||
|
||||
/// The break this catches: the bridge's default (no icon) left in place, so the home screen
|
||||
/// gets a screenshot of the page instead of the mark, or the size asked for not passed on.
|
||||
#[test]
|
||||
fn the_icon_is_the_tui_tile_at_the_size_asked_for() {
|
||||
let colors = luxe_remote::TileColors { plate: [0; 3], ink: [0xff; 3] };
|
||||
let png = TuiRenderer.home_icon(180, colors).expect("an icon");
|
||||
assert!(png.starts_with(b"\x89PNG\r\n\x1a\n"), "a PNG");
|
||||
// the header chunk's width and height, big-endian, after the signature and chunk head
|
||||
assert_eq!(png[16..24], [0, 0, 0, 180, 0, 0, 0, 180]);
|
||||
}
|
||||
|
||||
/// The break this catches: facts dropped or crossed on the way to the TUI, so the phone's
|
||||
/// status line misses the context reading, or colors a full window as a roomy one.
|
||||
#[test]
|
||||
fn status_facts_reach_the_tui_and_come_back_as_its_segments() {
|
||||
let facts: luxe_remote::StatusFacts = serde_json::from_value(serde_json::json!({
|
||||
"model": {"provider": "anthropic", "model": "claude-x"},
|
||||
"thinking": "medium",
|
||||
"context": {"used_tokens": 950, "context_window": 1000},
|
||||
}))
|
||||
.expect("the app's status facts parse");
|
||||
let segments = TuiRenderer.status(&facts);
|
||||
let words: Vec<_> = segments.iter().map(|s| (s.kind, s.label.as_str(), s.tone)).collect();
|
||||
assert_eq!(
|
||||
words,
|
||||
[
|
||||
("context", "95.0%/1.0k", luxe_remote::StatusTone::Error),
|
||||
("thinking", "med", luxe_remote::StatusTone::Plain),
|
||||
("model", "claude-x", luxe_remote::StatusTone::Plain),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
172
crates/luxe-cli/src/cli/remote_command/setup.rs
Normal file
172
crates/luxe-cli/src/cli/remote_command/setup.rs
Normal file
@@ -0,0 +1,172 @@
|
||||
//! `luxe remote setup` — from nothing to a paired phone, asking before each step that changes
|
||||
//! something outside Luxe.
|
||||
//!
|
||||
//! Four steps: Tailscale is there and signed in; Serve carries `https://<this machine>/` to the
|
||||
//! bridge; a user service runs the bridge at login; and an invitation to scan. Each step that is
|
||||
//! already done says so and moves on, so running setup again is how to check it.
|
||||
|
||||
use std::io::{BufRead, IsTerminal, Write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use luxe_remote::service::Launch;
|
||||
use luxe_remote::{DeviceBook, Published, Tailnet, TailnetError};
|
||||
|
||||
use crate::output::{out, outln};
|
||||
|
||||
/// Runs every step in order. Only a missing or signed-out Tailscale stops it.
|
||||
pub(super) async fn setup(
|
||||
port: u16,
|
||||
allow: Vec<String>,
|
||||
devices: &DeviceBook,
|
||||
) -> anyhow::Result<()> {
|
||||
let tailnet = check_tailscale().await?;
|
||||
publish_step(port, &tailnet).await;
|
||||
service_step(port, &tailnet, allow);
|
||||
wait_for_bridge(port).await;
|
||||
outln!();
|
||||
super::invitation::show(port, devices, &tailnet)
|
||||
}
|
||||
|
||||
/// Gives a service that just started up to two seconds to bind its port.
|
||||
async fn wait_for_bridge(port: u16) {
|
||||
for _ in 0..10 {
|
||||
if super::invitation::bridge_listening(port) {
|
||||
return;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn check_tailscale() -> anyhow::Result<Tailnet> {
|
||||
match luxe_remote::this_machine().await {
|
||||
Ok(tailnet) => {
|
||||
outln!("Tailscale: signed in as {} on {}.", tailnet.login, tailnet.dns_name);
|
||||
Ok(tailnet)
|
||||
}
|
||||
Err(error) => {
|
||||
outln!("{}", tailscale_advice(&error));
|
||||
Err(error.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn tailscale_advice(error: &TailnetError) -> &'static str {
|
||||
match error {
|
||||
TailnetError::NotInstalled => {
|
||||
"Install Tailscale from https://tailscale.com/download and sign in, then run \
|
||||
`luxe remote setup` again."
|
||||
}
|
||||
TailnetError::NotRunning(_) => {
|
||||
"Sign in to Tailscale with `tailscale up`, then run `luxe remote setup` again."
|
||||
}
|
||||
TailnetError::Failed(_) | TailnetError::Unreadable(_) => {
|
||||
"Check that Tailscale is running with `tailscale status`, then run `luxe remote setup` \
|
||||
again."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn publish_step(port: u16, tailnet: &Tailnet) {
|
||||
let address = format!("https://{}/", tailnet.dns_name);
|
||||
match luxe_remote::published(port).await {
|
||||
Ok(Published::Here) => outln!("Serve: {address} leads to the bridge."),
|
||||
Ok(Published::Elsewhere(target)) => outln!(
|
||||
"Serve: {address} already leads to {target}, so setup leaves it alone. \
|
||||
`tailscale serve --bg {port}` would point it at the bridge instead."
|
||||
),
|
||||
Ok(Published::Nowhere) => offer_publish(port, &address).await,
|
||||
Err(error) => outln!("Serve: couldn't read its settings ({error})."),
|
||||
}
|
||||
}
|
||||
|
||||
async fn offer_publish(port: u16, address: &str) {
|
||||
let question = format!("Publish the bridge at {address}, for your tailnet only?");
|
||||
if !confirm(&question) {
|
||||
outln!("Skipped. `tailscale serve --bg {port}` publishes it when you're ready.");
|
||||
return;
|
||||
}
|
||||
match luxe_remote::publish(port).await {
|
||||
Ok(()) => outln!("Serve: {address} now leads to the bridge."),
|
||||
Err(error) => {
|
||||
outln!("Serve: {error}");
|
||||
outln!("{}", SERVE_HINT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What usually stops `tailscale serve` for an ordinary account.
|
||||
const SERVE_HINT: &str = if cfg!(target_os = "linux") {
|
||||
"If it said access denied, let your account manage Tailscale with \
|
||||
`sudo tailscale set --operator=$USER`, then run setup again."
|
||||
} else {
|
||||
"Fix what Tailscale said above, then run setup again."
|
||||
};
|
||||
|
||||
fn service_step(port: u16, tailnet: &Tailnet, allow: Vec<String>) {
|
||||
if luxe_remote::service::installed() {
|
||||
outln!("Service: installed, so the bridge runs whenever you're signed in.");
|
||||
return;
|
||||
}
|
||||
if !confirm("Run the bridge whenever you're signed in, as a user service?") {
|
||||
outln!("Skipped. Run `luxe remote` yourself whenever your devices should reach Luxe.");
|
||||
return;
|
||||
}
|
||||
let launch =
|
||||
Launch { program: luxe_program(), args: service_args(port, &tailnet.login, allow) };
|
||||
match luxe_remote::service::install(&launch) {
|
||||
Ok(recorded) => outln!("Service: installed ({recorded}) and started."),
|
||||
Err(error) => outln!("Service: {error}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// The bridge's flags as the service runs it, the owner named outright so the service doesn't
|
||||
/// depend on reaching Tailscale as it starts.
|
||||
fn service_args(port: u16, owner: &str, allow: Vec<String>) -> Vec<String> {
|
||||
let mut args = vec!["remote".into(), "--port".into(), port.to_string()];
|
||||
for login in std::iter::once(owner.to_string()).chain(allow) {
|
||||
args.extend(["--allow".into(), login]);
|
||||
}
|
||||
args
|
||||
}
|
||||
|
||||
/// This binary, by the path on `PATH` when that is the same file, since an upgrade replaces the
|
||||
/// file a package manager links from `PATH` and leaves a versioned path behind.
|
||||
fn luxe_program() -> PathBuf {
|
||||
let running = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("luxe"));
|
||||
let on_path = luxe_core::program::resolve("luxe");
|
||||
let same = |path: &PathBuf| path.canonicalize().ok() == running.canonicalize().ok();
|
||||
on_path.filter(same).unwrap_or(running)
|
||||
}
|
||||
|
||||
/// Asks a yes-or-no question, yes by default. Without a terminal to ask, the answer is no.
|
||||
fn confirm(question: &str) -> bool {
|
||||
if !std::io::stdin().is_terminal() {
|
||||
outln!("{question} Not asked, with no terminal to answer in.");
|
||||
return false;
|
||||
}
|
||||
out!("{question} [Y/n] ");
|
||||
let _ = std::io::stdout().flush();
|
||||
let mut answer = String::new();
|
||||
if std::io::stdin().lock().read_line(&mut answer).is_err() {
|
||||
return false;
|
||||
}
|
||||
let answer = answer.trim().to_lowercase();
|
||||
answer.is_empty() || answer.starts_with('y')
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The service names the owner and every `--allow`, on the port setup was given.
|
||||
///
|
||||
/// The break this catches: a service that relies on finding Tailscale as it starts, which
|
||||
/// launchd's bare `PATH` doesn't, so the bridge comes up letting nobody in.
|
||||
#[test]
|
||||
fn the_service_names_who_it_lets_in() {
|
||||
let args = service_args(4600, "alex@github", vec!["sam@github".into()]);
|
||||
let expected =
|
||||
["remote", "--port", "4600", "--allow", "alex@github", "--allow", "sam@github"];
|
||||
assert_eq!(args, expected);
|
||||
}
|
||||
}
|
||||
@@ -9,14 +9,18 @@
|
||||
//! terminal (the TUI owns the screen), so that holds without any special care here — and
|
||||
//! `rpc_stdout_carries_only_protocol_frames` keeps it holding.
|
||||
|
||||
mod frame;
|
||||
mod translate;
|
||||
|
||||
use frame::write_frame;
|
||||
pub(super) use frame::{Opening, SharedTranscript};
|
||||
use luxe_core::engine::Action;
|
||||
use luxe_core::event::Action as WireAction;
|
||||
use luxe_core::Event;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader};
|
||||
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, BufReader};
|
||||
use tokio::sync::mpsc;
|
||||
use translate::{to_engine_action, CatalogResolver, GithubWrites, ModelResolver, Policy, Rejected};
|
||||
use translate::{to_engine_action, Rejected};
|
||||
pub(super) use translate::{CatalogResolver, GithubWrites, ModelResolver, Policy};
|
||||
|
||||
/// Depth of the channel carrying locally-generated frames (rejections) to the single writer.
|
||||
///
|
||||
@@ -24,6 +28,9 @@ use translate::{to_engine_action, CatalogResolver, GithubWrites, ModelResolver,
|
||||
/// already being told so.
|
||||
const LOCAL_FRAME_BUFFER: usize = 32;
|
||||
|
||||
/// The catalog a session's clients resolve and list models through, shared by every one of them.
|
||||
pub(super) type Models = std::sync::Arc<dyn ModelResolver + Send + Sync>;
|
||||
|
||||
/// Run the RPC front-end against stdio until the client closes the stream or stdin ends.
|
||||
///
|
||||
/// Args: `(settings, github_writes)` — the second is `--github-writes`, which lets the client
|
||||
@@ -44,6 +51,7 @@ pub(crate) async fn run_rpc(
|
||||
let policy = Policy {
|
||||
github_writes: GithubWrites::for_launch(github_writes, mode),
|
||||
github_sections: luxe_core::config::Config::load(&settings.state_root).github.sections(),
|
||||
joined: false,
|
||||
};
|
||||
let handle = crate::engine_setup::build_engine(
|
||||
&settings,
|
||||
@@ -63,6 +71,9 @@ pub(crate) async fn run_rpc(
|
||||
},
|
||||
crate::workspace_runtime::pull_request_watch(&settings.work_root),
|
||||
);
|
||||
// Shared like a terminal's, which is how a phone reaches a session the bridge started
|
||||
let (events, _sharing) =
|
||||
super::share::share(events, handle.actions.clone(), &settings, &handle.session_id.0);
|
||||
|
||||
let ready = Event::Ready {
|
||||
protocol_version: luxe_core::PROTOCOL_VERSION,
|
||||
@@ -82,12 +93,12 @@ pub(crate) async fn run_rpc(
|
||||
}
|
||||
|
||||
serve(
|
||||
ready,
|
||||
ready.into(),
|
||||
events,
|
||||
handle.actions,
|
||||
tokio::fs::File::from_std(input),
|
||||
tokio::io::stdout(),
|
||||
&CatalogResolver,
|
||||
std::sync::Arc::new(CatalogResolver { settings: settings.clone() }),
|
||||
policy,
|
||||
)
|
||||
.await?;
|
||||
@@ -100,21 +111,25 @@ pub(crate) async fn run_rpc(
|
||||
/// Generic over the streams so the protocol — handshake, framing, translation, refusal, shutdown —
|
||||
/// is exercised end to end in tests without a real stdin, a real terminal, or a live provider.
|
||||
/// `run_rpc` is this function with stdio and the real catalog plugged in.
|
||||
async fn serve<R, W>(
|
||||
ready: Event,
|
||||
///
|
||||
/// A client that joined a terminal's session (`policy.joined`) never closes the engine: its
|
||||
/// hangup or `close` ends its own connection, and the terminal carries on.
|
||||
pub(super) async fn serve<R, W>(
|
||||
opening: Opening,
|
||||
mut events: mpsc::Receiver<Event>,
|
||||
engine_tx: mpsc::Sender<Action>,
|
||||
input: R,
|
||||
mut output: W,
|
||||
models: &'static (dyn ModelResolver + Sync),
|
||||
models: Models,
|
||||
policy: Policy,
|
||||
) -> anyhow::Result<()>
|
||||
where
|
||||
R: AsyncRead + Unpin + Send + 'static,
|
||||
W: AsyncWrite + Unpin,
|
||||
{
|
||||
if !emit(&mut output, &ready).await? {
|
||||
engine_tx.send(Action::Close).await.ok();
|
||||
let joined = policy.joined;
|
||||
if !emit_opening(&mut output, opening).await? {
|
||||
close_unless_joined(&engine_tx, joined).await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -131,6 +146,8 @@ where
|
||||
// ordered against each other: `select!` takes whichever is ready. Nothing needs them to be — a
|
||||
// refusal is about the client's own input, not about engine state — and pretending otherwise
|
||||
// would mean holding one stream up for the other.
|
||||
//
|
||||
// A joined client's reader ending *is* the end of its session, since the engine outlives it.
|
||||
let mut refusals_open = true;
|
||||
loop {
|
||||
let frame = tokio::select! {
|
||||
@@ -140,6 +157,7 @@ where
|
||||
},
|
||||
refusal = local_rx.recv(), if refusals_open => match refusal {
|
||||
Some(refusal) => refusal,
|
||||
None if joined => break,
|
||||
None => {
|
||||
refusals_open = false;
|
||||
continue;
|
||||
@@ -163,19 +181,26 @@ where
|
||||
|
||||
reader.abort();
|
||||
// The engine may already be gone (the client sent `close`); saying so twice is harmless.
|
||||
engine_tx.send(Action::Close).await.ok();
|
||||
close_unless_joined(&engine_tx, joined).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Close the engine as a client leaves, unless the client only joined it.
|
||||
async fn close_unless_joined(engine_tx: &mpsc::Sender<Action>, joined: bool) {
|
||||
if !joined {
|
||||
engine_tx.send(Action::Close).await.ok();
|
||||
}
|
||||
}
|
||||
|
||||
/// Read wire actions until EOF, forwarding what translates and refusing what does not.
|
||||
///
|
||||
/// EOF means the client is gone, so the engine is told to close rather than left running behind a
|
||||
/// stream nobody is reading.
|
||||
/// stream nobody is reading. A joined client's EOF or `close` only ends the read.
|
||||
async fn read_actions<R>(
|
||||
input: R,
|
||||
engine_tx: mpsc::Sender<Action>,
|
||||
local_tx: mpsc::Sender<Event>,
|
||||
models: &'static (dyn ModelResolver + Sync),
|
||||
models: Models,
|
||||
policy: Policy,
|
||||
) where
|
||||
R: AsyncRead + Unpin,
|
||||
@@ -193,18 +218,26 @@ async fn read_actions<R>(
|
||||
if line.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let action = match parse_action(&line, models, &policy) {
|
||||
Ok(action) => action,
|
||||
let reply = match parse_action(&line, &*models, &policy) {
|
||||
Ok(Inbound::Engine(action)) => Ok(*action),
|
||||
Ok(Inbound::Answer(answer)) => Err(answer),
|
||||
Err(rejected) => {
|
||||
let refusal =
|
||||
Event::ActionRejected { kind: rejected.kind, reason: rejected.reason };
|
||||
if local_tx.send(refusal).await.is_err() {
|
||||
Err(Event::ActionRejected { kind: rejected.kind, reason: rejected.reason })
|
||||
}
|
||||
};
|
||||
let action = match reply {
|
||||
Ok(action) => action,
|
||||
Err(reply) => {
|
||||
if local_tx.send(reply).await.is_err() {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
};
|
||||
let closing = matches!(action, Action::Close);
|
||||
if closing && policy.joined {
|
||||
break;
|
||||
}
|
||||
if engine_tx.send(action).await.is_err() {
|
||||
break;
|
||||
}
|
||||
@@ -212,7 +245,7 @@ async fn read_actions<R>(
|
||||
break;
|
||||
}
|
||||
}
|
||||
engine_tx.send(Action::Close).await.ok();
|
||||
close_unless_joined(&engine_tx, policy.joined).await;
|
||||
}
|
||||
|
||||
/// Parse and translate one inbound line.
|
||||
@@ -221,9 +254,9 @@ async fn read_actions<R>(
|
||||
/// is testable without any IO at all.
|
||||
fn parse_action(
|
||||
line: &str,
|
||||
models: &(dyn ModelResolver + Sync),
|
||||
models: &(dyn ModelResolver + Send + Sync),
|
||||
policy: &Policy,
|
||||
) -> Result<Action, Rejected> {
|
||||
) -> Result<Inbound, Rejected> {
|
||||
// Read the envelope before the payload, so a frame that names itself and then fails to
|
||||
// deserialize is still refused *by name*. Reporting `kind: null` for a `steer_turn` missing its
|
||||
// text sends the client looking through every frame it wrote instead of the one that was wrong.
|
||||
@@ -232,15 +265,49 @@ fn parse_action(
|
||||
let kind = raw.get("kind").and_then(serde_json::Value::as_str).map(str::to_string);
|
||||
let wire: WireAction = serde_json::from_value(raw)
|
||||
.map_err(|error| Rejected { kind, reason: error.to_string() })?;
|
||||
to_engine_action(wire, models, policy)
|
||||
if let WireAction::ListModels { request_id } = wire {
|
||||
return Ok(Inbound::Answer(Event::ModelsListed { request_id, models: models.choices() }));
|
||||
}
|
||||
to_engine_action(wire, models, policy).map(|action| Inbound::Engine(Box::new(action)))
|
||||
}
|
||||
|
||||
/// What one inbound line comes to.
|
||||
#[derive(Debug)]
|
||||
enum Inbound {
|
||||
/// An action for the engine
|
||||
Engine(Box<Action>),
|
||||
/// An answer for this client alone, which the engine has no part in
|
||||
Answer(Event),
|
||||
}
|
||||
|
||||
/// Write one frame, reporting `false` once the client has stopped reading.
|
||||
///
|
||||
/// A hangup is how a session ends when the other end is `head` or a closed editor pane, so it
|
||||
/// resolves to a clean exit rather than an error written to a pipe nobody is holding.
|
||||
async fn emit<W: AsyncWrite + Unpin>(out: &mut W, event: &Event) -> anyhow::Result<bool> {
|
||||
match write_frame(out, event).await {
|
||||
/// Send `ready` and any transcript, saying whether the client is still there.
|
||||
///
|
||||
/// Takes the opening by value so the transcript is let go once sent: held for the connection, it
|
||||
/// would pin a superseded copy of every message, and each commit would copy the snapshot's whole
|
||||
/// list to append to it.
|
||||
async fn emit_opening<W: AsyncWrite + Unpin>(
|
||||
out: &mut W,
|
||||
opening: Opening,
|
||||
) -> anyhow::Result<bool> {
|
||||
if !emit(out, &opening.ready).await? {
|
||||
return Ok(false);
|
||||
}
|
||||
match opening.transcript {
|
||||
Some(transcript) => emit(out, &transcript).await,
|
||||
None => Ok(true),
|
||||
}
|
||||
}
|
||||
|
||||
async fn emit<W, F>(out: &mut W, frame: &F) -> anyhow::Result<bool>
|
||||
where
|
||||
W: AsyncWrite + Unpin,
|
||||
F: serde::Serialize + ?Sized,
|
||||
{
|
||||
match write_frame(out, frame).await {
|
||||
Ok(()) => Ok(true),
|
||||
Err(error)
|
||||
if matches!(
|
||||
@@ -255,23 +322,5 @@ async fn emit<W: AsyncWrite + Unpin>(out: &mut W, event: &Event) -> anyhow::Resu
|
||||
}
|
||||
}
|
||||
|
||||
/// Write one frame and flush it, so a streaming client sees tokens as they arrive.
|
||||
///
|
||||
/// A frame that cannot serialize is logged and dropped rather than ending the session: losing one
|
||||
/// event is recoverable for a client, and a dead stream is not.
|
||||
async fn write_frame<W: AsyncWrite + Unpin>(out: &mut W, event: &Event) -> std::io::Result<()> {
|
||||
let line = match serde_json::to_string(event) {
|
||||
Ok(line) => line,
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, "rpc: dropping an event that would not serialize");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
out.write_all(line.as_bytes()).await?;
|
||||
out.write_all(b"\n").await?;
|
||||
out.flush().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
99
crates/luxe-cli/src/cli/rpc/frame.rs
Normal file
99
crates/luxe-cli/src/cli/rpc/frame.rs
Normal file
@@ -0,0 +1,99 @@
|
||||
//! Frames — one JSON object per line, written without growing one buffer to the biggest frame.
|
||||
//!
|
||||
//! A joiner's `session_loaded` carries the whole transcript, tens of megabytes on a long session,
|
||||
//! and a phone joins again on every reconnect. Encoded into one `String` it grows by doubling,
|
||||
//! about four times its size allocated for every join, so a frame is encoded into fixed-size pieces
|
||||
//! instead, and a shared session's transcript goes out from the snapshot's copy, not a clone of it.
|
||||
|
||||
use luxe_core::event::SessionMessage;
|
||||
use luxe_core::Event;
|
||||
use serde::Serialize;
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
||||
|
||||
/// Bytes in each piece of an encoded frame.
|
||||
const CHUNK_BYTES: usize = 64 * 1024;
|
||||
|
||||
/// What a client is sent ahead of the event stream: `ready`, then for a joiner the transcript.
|
||||
pub(in crate::cli) struct Opening {
|
||||
pub(in crate::cli) ready: Event,
|
||||
/// The transcript so far, for a client joining a session already under way
|
||||
pub(in crate::cli) transcript: Option<SharedTranscript>,
|
||||
}
|
||||
|
||||
impl From<Event> for Opening {
|
||||
fn from(ready: Event) -> Self {
|
||||
Self { ready, transcript: None }
|
||||
}
|
||||
}
|
||||
|
||||
/// A session's transcript, sent as its `session_loaded` frame and shared with the snapshot that
|
||||
/// keeps it, so a join doesn't copy every message.
|
||||
pub(in crate::cli) struct SharedTranscript {
|
||||
pub(in crate::cli) session_id: String,
|
||||
pub(in crate::cli) messages: Arc<Vec<SessionMessage>>,
|
||||
}
|
||||
|
||||
/// `Event::SessionLoaded`'s wire shape over borrowed fields; a test pins it to the event's own.
|
||||
#[derive(Serialize)]
|
||||
#[serde(tag = "kind", content = "content", rename_all = "snake_case")]
|
||||
enum LoadedFrame<'a> {
|
||||
SessionLoaded { session_id: &'a str, messages: &'a [SessionMessage] },
|
||||
}
|
||||
|
||||
impl Serialize for SharedTranscript {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
let frame =
|
||||
LoadedFrame::SessionLoaded { session_id: &self.session_id, messages: &self.messages };
|
||||
frame.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
/// Write one frame and flush it, so a streaming client sees tokens as they arrive.
|
||||
///
|
||||
/// A frame that cannot serialize is logged and dropped rather than ending the session: losing one
|
||||
/// event is recoverable for a client, and a dead stream is not. Nothing is written until the whole
|
||||
/// frame has encoded, so a failure never leaves half a line.
|
||||
pub(super) async fn write_frame<W, F>(out: &mut W, frame: &F) -> std::io::Result<()>
|
||||
where
|
||||
W: AsyncWrite + Unpin,
|
||||
F: Serialize + ?Sized,
|
||||
{
|
||||
let mut encoded = Pieces::default();
|
||||
if let Err(error) = serde_json::to_writer(&mut encoded, frame) {
|
||||
tracing::warn!(%error, "rpc: dropping an event that would not serialize");
|
||||
return Ok(());
|
||||
}
|
||||
for piece in encoded.full.iter().chain(std::iter::once(&encoded.current)) {
|
||||
out.write_all(piece).await?;
|
||||
}
|
||||
out.write_all(b"\n").await?;
|
||||
out.flush().await
|
||||
}
|
||||
|
||||
/// An encoded frame in pieces of at most `CHUNK_BYTES`. The first grows like any buffer, so a
|
||||
/// token's frame costs what it always did, and a large frame is never copied into a bigger one.
|
||||
#[derive(Default)]
|
||||
struct Pieces {
|
||||
full: Vec<Vec<u8>>,
|
||||
current: Vec<u8>,
|
||||
}
|
||||
|
||||
impl std::io::Write for Pieces {
|
||||
fn write(&mut self, bytes: &[u8]) -> std::io::Result<usize> {
|
||||
if self.current.len() == CHUNK_BYTES {
|
||||
let next = Vec::with_capacity(CHUNK_BYTES);
|
||||
self.full.push(std::mem::replace(&mut self.current, next));
|
||||
}
|
||||
let taken = bytes.len().min(CHUNK_BYTES - self.current.len());
|
||||
self.current.extend_from_slice(&bytes[..taken]);
|
||||
Ok(taken)
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
88
crates/luxe-cli/src/cli/rpc/frame/tests.rs
Normal file
88
crates/luxe-cli/src/cli/rpc/frame/tests.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
use super::*;
|
||||
use luxe_core::message::Message;
|
||||
use luxe_core::session::EntryId;
|
||||
|
||||
fn messages() -> Vec<SessionMessage> {
|
||||
["first", "second"]
|
||||
.into_iter()
|
||||
.map(|text| SessionMessage { entry_id: EntryId(text.into()), message: Message::user(text) })
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn written<F: Serialize + ?Sized>(frame: &F) -> String {
|
||||
let mut out = Vec::new();
|
||||
write_frame(&mut out, frame).await.expect("writing to memory");
|
||||
String::from_utf8(out).expect("frames are UTF-8")
|
||||
}
|
||||
|
||||
/// The shared transcript goes out byte for byte as the `session_loaded` event would.
|
||||
///
|
||||
/// The break this catches: the borrowed copy of the event's shape drifting from `Event`, a renamed
|
||||
/// field or a lost `rename_all`, which every joining client would read as an unknown frame.
|
||||
#[test]
|
||||
fn the_shared_transcript_encodes_as_session_loaded() {
|
||||
let shared =
|
||||
SharedTranscript { session_id: "session-1".into(), messages: Arc::new(messages()) };
|
||||
let event = Event::SessionLoaded { session_id: "session-1".into(), messages: messages() };
|
||||
assert_eq!(serde_json::to_string(&shared).unwrap(), serde_json::to_string(&event).unwrap());
|
||||
}
|
||||
|
||||
/// A frame bigger than several pieces arrives whole, as one line.
|
||||
///
|
||||
/// The break this catches: losing the last piece or the full ones, or cutting a piece a byte off,
|
||||
/// which garbles exactly the large `session_loaded` frame a long session sends.
|
||||
#[tokio::test]
|
||||
async fn a_frame_of_many_pieces_is_written_whole() {
|
||||
let text: String =
|
||||
(0..CHUNK_BYTES * 3 + 17).map(|at| char::from(b'a' + (at % 26) as u8)).collect();
|
||||
let event = Event::Notice(text);
|
||||
let expected = format!("{}\n", serde_json::to_string(&event).unwrap());
|
||||
assert_eq!(written(&event).await, expected);
|
||||
}
|
||||
|
||||
/// A large frame is held in pieces of one chunk at most, never one buffer of the whole.
|
||||
///
|
||||
/// The break this catches: dropping the cap on a piece, which grows the first one by doubling to the
|
||||
/// frame's size, the very allocation this module exists to avoid.
|
||||
#[test]
|
||||
fn a_large_frame_is_held_in_chunk_sized_pieces() {
|
||||
let mut encoded = Pieces::default();
|
||||
serde_json::to_writer(&mut encoded, &"x".repeat(CHUNK_BYTES * 3)).unwrap();
|
||||
assert_eq!(encoded.full.len(), 3);
|
||||
assert!(encoded.full.iter().all(|piece| piece.len() == CHUNK_BYTES));
|
||||
assert!(encoded.current.capacity() <= CHUNK_BYTES, "{}", encoded.current.capacity());
|
||||
}
|
||||
|
||||
/// A token's frame still fits in one piece, so the common case pays for no more than it did.
|
||||
///
|
||||
/// The break this catches: starting every frame on a full piece, a 64 KB allocation for each of
|
||||
/// the thousands of token deltas a reply streams.
|
||||
#[test]
|
||||
fn a_small_frame_stays_in_its_first_piece() {
|
||||
let mut encoded = Pieces::default();
|
||||
serde_json::to_writer(&mut encoded, &Event::Notice("hi".into())).unwrap();
|
||||
assert!(encoded.full.is_empty());
|
||||
assert!(encoded.current.capacity() < CHUNK_BYTES, "{}", encoded.current.capacity());
|
||||
}
|
||||
|
||||
/// Serializes as a list whose second element fails, after the first has been encoded.
|
||||
struct FailsPartway;
|
||||
|
||||
impl Serialize for FailsPartway {
|
||||
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||
use serde::ser::{Error, SerializeSeq};
|
||||
let mut list = serializer.serialize_seq(Some(2))?;
|
||||
list.serialize_element("encoded")?;
|
||||
drop(list);
|
||||
Err(S::Error::custom("cannot encode the rest"))
|
||||
}
|
||||
}
|
||||
|
||||
/// A frame that fails partway writes nothing rather than half a line.
|
||||
///
|
||||
/// The break this catches: streaming the encoding straight to the client, which leaves a broken
|
||||
/// line that turns every later frame into a parse error.
|
||||
#[tokio::test]
|
||||
async fn a_frame_that_fails_partway_writes_nothing() {
|
||||
assert_eq!(written(&FailsPartway).await, "");
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
use super::*;
|
||||
use luxe_core::provider::ModelInfo;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
/// A resolver with no dependence on the developer's `models.json`.
|
||||
struct FixedModels;
|
||||
@@ -8,14 +9,42 @@ impl ModelResolver for FixedModels {
|
||||
fn resolve(&self, provider: &str, id: &str) -> ModelInfo {
|
||||
ModelInfo { r#ref: luxe_core::provider::ModelRef::new(provider, id), ..Default::default() }
|
||||
}
|
||||
|
||||
fn choices(&self) -> Vec<luxe_core::event::ModelChoice> {
|
||||
vec![luxe_core::event::ModelChoice {
|
||||
provider: "anthropic".into(),
|
||||
model: "claude-sonnet-5".into(),
|
||||
name: "Claude Sonnet 5".into(),
|
||||
thinking_levels: vec![luxe_core::provider::ThinkingLevel::Off],
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
||||
fn policy(github_writes: GithubWrites) -> Policy {
|
||||
Policy { github_writes, github_sections: Vec::new() }
|
||||
Policy { github_writes, github_sections: Vec::new(), joined: false }
|
||||
}
|
||||
|
||||
fn parse(line: &str) -> Result<Action, Rejected> {
|
||||
parse_action(line, &FixedModels, &policy(GithubWrites::NotEnabled))
|
||||
match parse_action(line, &FixedModels, &policy(GithubWrites::NotEnabled))? {
|
||||
Inbound::Engine(action) => Ok(*action),
|
||||
Inbound::Answer(answer) => panic!("answered without the engine: {answer:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// `list_models` is answered to the client that asked, with the catalog's choices, and the
|
||||
/// engine never hears of it.
|
||||
///
|
||||
/// The break this catches: forwarding it to the engine, which has no arm for it; or answering
|
||||
/// with nothing, so the phone's picker opens empty.
|
||||
#[tokio::test]
|
||||
async fn the_protocol_answers_which_models_there_are() {
|
||||
let ask = "{\"kind\":\"list_models\",\"content\":{\"request_id\":\"models:1\"}}\n";
|
||||
let session = run_session(ask, Vec::new()).await;
|
||||
let listed = session.frames.iter().find(|frame| frame["kind"] == "models_listed");
|
||||
let listed = listed.expect("an answer");
|
||||
assert_eq!(listed["content"]["request_id"], "models:1");
|
||||
assert_eq!(listed["content"]["models"][0]["model"], "claude-sonnet-5");
|
||||
assert!(session.actions.iter().all(|action| matches!(action, Action::Close)));
|
||||
}
|
||||
|
||||
/// A well-formed frame becomes the engine action it names.
|
||||
@@ -120,12 +149,12 @@ async fn run_session(input: &str, events: Vec<Event>) -> Session {
|
||||
github_writes: false,
|
||||
};
|
||||
let served = serve(
|
||||
ready,
|
||||
ready.into(),
|
||||
event_rx,
|
||||
engine_tx,
|
||||
server_read,
|
||||
server_write,
|
||||
&FixedModels,
|
||||
std::sync::Arc::new(FixedModels),
|
||||
policy(GithubWrites::NotEnabled),
|
||||
);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), served)
|
||||
@@ -338,12 +367,12 @@ async fn a_real_write_failure_is_not_mistaken_for_a_hangup() {
|
||||
github_writes: false,
|
||||
};
|
||||
let served = serve(
|
||||
ready,
|
||||
ready.into(),
|
||||
event_rx,
|
||||
engine_tx,
|
||||
server_read,
|
||||
FailingWriter,
|
||||
&FixedModels,
|
||||
std::sync::Arc::new(FixedModels),
|
||||
policy(GithubWrites::NotEnabled),
|
||||
);
|
||||
let outcome = tokio::time::timeout(std::time::Duration::from_secs(5), served)
|
||||
@@ -383,12 +412,12 @@ async fn a_client_that_stops_reading_is_not_an_error() {
|
||||
github_writes: false,
|
||||
};
|
||||
let served = serve(
|
||||
ready,
|
||||
ready.into(),
|
||||
event_rx,
|
||||
engine_tx,
|
||||
server_read,
|
||||
server_write,
|
||||
&FixedModels,
|
||||
std::sync::Arc::new(FixedModels),
|
||||
policy(GithubWrites::NotEnabled),
|
||||
);
|
||||
let outcome = tokio::time::timeout(std::time::Duration::from_secs(5), served)
|
||||
@@ -397,6 +426,61 @@ async fn a_client_that_stops_reading_is_not_an_error() {
|
||||
assert!(outcome.is_ok(), "a client hangup is a clean end: {outcome:?}");
|
||||
}
|
||||
|
||||
/// A joiner's transcript is let go once it's sent, while the client stays connected.
|
||||
///
|
||||
/// The break this catches: `serve` keeping the opening for the whole connection, which pins a stale
|
||||
/// copy of every message per client and makes each commit copy the snapshot's whole list.
|
||||
#[tokio::test]
|
||||
async fn a_sent_transcript_is_released_while_the_client_stays() {
|
||||
let message = luxe_core::event::SessionMessage {
|
||||
entry_id: luxe_core::session::EntryId("entry-1".into()),
|
||||
message: luxe_core::message::Message::user("hello"),
|
||||
};
|
||||
let messages = std::sync::Arc::new(vec![message]);
|
||||
let transcript =
|
||||
SharedTranscript { session_id: "session-1".into(), messages: messages.clone() };
|
||||
let (served, mut lines) = serve_joiner(transcript);
|
||||
|
||||
lines.next_line().await.unwrap().expect("the ready frame");
|
||||
let loaded = lines.next_line().await.unwrap().expect("the transcript frame");
|
||||
assert!(loaded.starts_with(r#"{"kind":"session_loaded""#), "{loaded}");
|
||||
assert_eq!(holders_once_settled(&messages).await, 1, "serve still holds the transcript");
|
||||
assert!(!served.is_finished(), "the client is still connected");
|
||||
served.abort();
|
||||
}
|
||||
|
||||
/// Serve a joiner opening on `transcript`, connected until aborted, and what its client reads.
|
||||
fn serve_joiner(
|
||||
transcript: SharedTranscript,
|
||||
) -> (
|
||||
tokio::task::JoinHandle<anyhow::Result<()>>,
|
||||
tokio::io::Lines<BufReader<tokio::io::DuplexStream>>,
|
||||
) {
|
||||
let (client_write, server_read) = tokio::io::duplex(1024);
|
||||
let (server_write, client_read) = tokio::io::duplex(64 * 1024);
|
||||
let (engine_tx, engine_rx) = mpsc::channel::<Action>(64);
|
||||
let (event_tx, event_rx) = mpsc::channel::<Event>(64);
|
||||
let opening = Opening { ready: Event::Notice("ready".into()), transcript: Some(transcript) };
|
||||
let (models, policy) = (std::sync::Arc::new(FixedModels), policy(GithubWrites::NotEnabled));
|
||||
let served = tokio::spawn(async move {
|
||||
// Held open, so the session waits on its client instead of ending
|
||||
let _open = (client_write, engine_rx, event_tx);
|
||||
serve(opening, event_rx, engine_tx, server_read, server_write, models, policy).await
|
||||
});
|
||||
(served, BufReader::new(client_read).lines())
|
||||
}
|
||||
|
||||
/// How many hold `messages` once anything still sending it has had its chance to let go.
|
||||
async fn holders_once_settled<T>(messages: &std::sync::Arc<T>) -> usize {
|
||||
for _ in 0..100 {
|
||||
if std::sync::Arc::strong_count(messages) == 1 {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
|
||||
}
|
||||
std::sync::Arc::strong_count(messages)
|
||||
}
|
||||
|
||||
/// Every frame written is one line of JSON — the framing rule a client parses by.
|
||||
///
|
||||
/// `run_session` panics on any line that will not parse, so a stray `println!` anywhere in this
|
||||
|
||||
@@ -39,6 +39,8 @@ pub(crate) struct Policy {
|
||||
pub(crate) github_writes: GithubWrites,
|
||||
/// The `/github` sections a bare `list_github` runs: the user's configured ones
|
||||
pub(crate) github_sections: Vec<luxe_core::config::GithubSection>,
|
||||
/// Whether the client joined a session a terminal runs, rather than starting its own
|
||||
pub(crate) joined: bool,
|
||||
}
|
||||
|
||||
/// Whether a client may publish to GitHub, and if not, why not.
|
||||
@@ -95,16 +97,71 @@ pub(crate) trait ModelResolver {
|
||||
/// same contract `--model` has always had, and it is what makes a local or proxied model
|
||||
/// usable without an entry in anyone's catalog.
|
||||
fn resolve(&self, provider: &str, id: &str) -> luxe_core::ModelInfo;
|
||||
|
||||
/// The models a client can switch to, configured ones first, for `list_models`.
|
||||
fn choices(&self) -> Vec<luxe_core::event::ModelChoice>;
|
||||
}
|
||||
|
||||
/// The real catalog: configured models first, models.dev filling the gaps, defaults under that.
|
||||
pub(crate) struct CatalogResolver;
|
||||
///
|
||||
/// Holds the launch's settings because which providers can be reached depends on them: a key
|
||||
/// passed on the command line reaches a provider nothing else configures.
|
||||
pub(crate) struct CatalogResolver {
|
||||
pub(crate) settings: crate::engine_setup::AgentSettings,
|
||||
}
|
||||
|
||||
impl ModelResolver for CatalogResolver {
|
||||
fn resolve(&self, provider: &str, id: &str) -> luxe_core::ModelInfo {
|
||||
let config = crate::engine_setup::load_luxe_config();
|
||||
crate::model_catalog::resolve_model(&config, provider, id).0
|
||||
}
|
||||
|
||||
fn choices(&self) -> Vec<luxe_core::event::ModelChoice> {
|
||||
crate::model_catalog::choices(&self.settings)
|
||||
}
|
||||
}
|
||||
|
||||
/// Why the client's policy refuses `wire`, before it means anything to the engine.
|
||||
fn policy_refusal(wire: &WireAction, policy: &Policy) -> Option<String> {
|
||||
let kind = wire.kind();
|
||||
if wire.writes_to_github() {
|
||||
if let Some(reason) = policy.github_writes.refusal(kind) {
|
||||
return Some(reason);
|
||||
}
|
||||
}
|
||||
if wire.writes_the_repository() && policy.github_writes == GithubWrites::ReadOnly {
|
||||
return Some(format!("{kind} writes to the repository, and this session is read-only"));
|
||||
}
|
||||
session_refusal(wire, policy)
|
||||
}
|
||||
|
||||
/// Why `wire` is refused for the client's place in the session: the one that started it, or one
|
||||
/// that joined.
|
||||
fn session_refusal(wire: &WireAction, policy: &Policy) -> Option<String> {
|
||||
let kind = wire.kind();
|
||||
// A joined client's introduction is kept by the share layer, so one that gets here started
|
||||
// the session
|
||||
if matches!(wire, WireAction::Introduce { .. }) {
|
||||
return Some(format!("{kind} is for a client that joined a running session"));
|
||||
}
|
||||
// The protocol answers it from the catalog before translating (`rpc::parse_action`)
|
||||
if matches!(wire, WireAction::ListModels { .. }) {
|
||||
return Some(format!("{kind} is answered by the protocol, not the engine"));
|
||||
}
|
||||
let switches = matches!(wire, WireAction::NewSession | WireAction::LoadSession(_));
|
||||
if policy.joined && switches {
|
||||
let reason = "would switch the session the terminal is showing: switch it there, or start \
|
||||
another Luxe";
|
||||
return Some(format!("{kind} {reason}"));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Review progress carrying only the walked chapters, which is all the wire sends.
|
||||
fn walked(
|
||||
walked_chapter_ids: Vec<luxe_core::guided_review::ChapterId>,
|
||||
) -> luxe_core::guided_review::ReviewProgress {
|
||||
luxe_core::guided_review::ReviewProgress { walked_chapter_ids, ..Default::default() }
|
||||
}
|
||||
|
||||
/// Translate one wire action into the engine action it stands for.
|
||||
@@ -116,15 +173,8 @@ pub(crate) fn to_engine_action(
|
||||
models: &dyn ModelResolver,
|
||||
policy: &Policy,
|
||||
) -> Result<Action, Rejected> {
|
||||
if wire.writes_to_github() {
|
||||
if let Some(reason) = policy.github_writes.refusal(wire.kind()) {
|
||||
return Err(Rejected { kind: Some(wire.kind().to_string()), reason });
|
||||
}
|
||||
}
|
||||
if wire.writes_the_repository() && policy.github_writes == GithubWrites::ReadOnly {
|
||||
let kind = wire.kind();
|
||||
let reason = format!("{kind} writes to the repository, and this session is read-only");
|
||||
return Err(Rejected { kind: Some(kind.to_string()), reason });
|
||||
if let Some(reason) = policy_refusal(&wire, policy) {
|
||||
return Err(Rejected { kind: Some(wire.kind().to_string()), reason });
|
||||
}
|
||||
Ok(match wire {
|
||||
WireAction::SubmitUserMessage { text, images } => {
|
||||
@@ -159,15 +209,10 @@ pub(crate) fn to_engine_action(
|
||||
Action::EditReview { request_id, review_id, edit }
|
||||
}
|
||||
WireAction::RecordReviewProgress { review_id, walked_chapter_ids } => {
|
||||
Action::RecordReviewProgress {
|
||||
review_id,
|
||||
progress: luxe_core::guided_review::ReviewProgress {
|
||||
walked_chapter_ids,
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
Action::RecordReviewProgress { review_id, progress: walked(walked_chapter_ids) }
|
||||
}
|
||||
WireAction::RecordReviewSeen { review_id } => Action::RecordReviewSeen { review_id },
|
||||
WireAction::ShowOpenReview { request_id } => Action::ShowOpenReview { request_id },
|
||||
WireAction::RefreshGuidedReview { request_id, review_id, revision_id } => {
|
||||
Action::RefreshGuidedReview { request_id, review_id, expected_revision: revision_id }
|
||||
}
|
||||
|
||||
@@ -13,6 +13,10 @@ impl ModelResolver for OneModel {
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn choices(&self) -> Vec<luxe_core::event::ModelChoice> {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn policy(github_writes: GithubWrites) -> Policy {
|
||||
@@ -22,6 +26,7 @@ fn policy(github_writes: GithubWrites) -> Policy {
|
||||
name: "Mine".into(),
|
||||
query: "is:open is:pr author:@me repo:{repo}".into(),
|
||||
}],
|
||||
joined: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +34,20 @@ fn translate(wire: WireAction) -> Result<Action, Rejected> {
|
||||
to_engine_action(wire, &OneModel, &policy(GithubWrites::Allowed))
|
||||
}
|
||||
|
||||
/// A client that joined a terminal's session can't switch it, and one that started its own can.
|
||||
///
|
||||
/// The break this catches: dropping the joined check, which lets a phone swap out the
|
||||
/// conversation the person at the terminal is reading.
|
||||
#[test]
|
||||
fn a_joined_client_cannot_switch_the_session() {
|
||||
let joined = Policy { joined: true, ..policy(GithubWrites::Allowed) };
|
||||
for wire in [WireAction::NewSession, WireAction::LoadSession("other".into())] {
|
||||
let refused = to_engine_action(wire.clone(), &OneModel, &joined);
|
||||
assert!(refused.is_err(), "a joined client ran {}", wire.kind());
|
||||
assert!(translate(wire).is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
/// A steer for the summary has to survive the crossing; without it `/compact keep the API
|
||||
/// decisions` is an ordinary compaction that silently ignored what it was told.
|
||||
#[test]
|
||||
@@ -104,6 +123,18 @@ fn each_wire_action_arrives_as_the_engine_action_it_names() {
|
||||
));
|
||||
}
|
||||
|
||||
/// The client that started a session is refused when it introduces itself, by name.
|
||||
///
|
||||
/// The break this catches: the arm falling to the catch-all, which tells a client that a published
|
||||
/// kind is unknown.
|
||||
#[test]
|
||||
fn the_starting_client_cannot_introduce_itself() {
|
||||
let introduce = WireAction::Introduce { name: "iPhone".into(), watch_only: false, yours: true };
|
||||
let refused = translate(introduce).unwrap_err();
|
||||
assert_eq!(refused.kind.as_deref(), Some("introduce"));
|
||||
assert!(refused.reason.contains("joined a running session"), "{}", refused.reason);
|
||||
}
|
||||
|
||||
/// Every GitHub action reaches the engine as the action it names, carrying its payload.
|
||||
///
|
||||
/// Same break as above, sharpest here: overview and conversation share a payload, as do
|
||||
@@ -248,6 +279,10 @@ fn each_review_action_arrives_as_the_engine_action_it_names() {
|
||||
translate(WireAction::RecordReviewSeen { review_id: id("review:1") }),
|
||||
Ok(Action::RecordReviewSeen { .. })
|
||||
));
|
||||
assert!(matches!(
|
||||
translate(WireAction::ShowOpenReview { request_id: load.clone() }),
|
||||
Ok(Action::ShowOpenReview { request_id }) if request_id == load
|
||||
));
|
||||
assert!(matches!(
|
||||
translate(WireAction::RefreshGuidedReview {
|
||||
request_id: load.clone(),
|
||||
@@ -523,7 +558,10 @@ fn an_unknown_frame_is_refused_by_name() {
|
||||
/// comes from the built-in defaults rather than from whatever the developer has configured.
|
||||
#[test]
|
||||
fn the_real_resolver_answers_about_the_model_it_was_asked() {
|
||||
let info = CatalogResolver.resolve("no-such-provider", "no-such-model-9e3f");
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let overrides = crate::engine_setup::CliOverrides::default();
|
||||
let settings = crate::engine_setup::AgentSettings::resolve(dir.path().into(), overrides);
|
||||
let info = CatalogResolver { settings }.resolve("no-such-provider", "no-such-model-9e3f");
|
||||
assert_eq!(info.r#ref.provider, "no-such-provider");
|
||||
assert_eq!(info.r#ref.id, "no-such-model-9e3f");
|
||||
assert!(info.context_window > 0, "a zero window makes every context percentage a lie");
|
||||
@@ -539,7 +577,7 @@ fn the_real_resolver_answers_about_the_model_it_was_asked() {
|
||||
fn the_translator_covers_the_whole_published_vocabulary() {
|
||||
assert_eq!(
|
||||
WireAction::KINDS.len(),
|
||||
51,
|
||||
54,
|
||||
"a wire action was published without a translator arm: {:?}",
|
||||
WireAction::KINDS
|
||||
);
|
||||
|
||||
236
crates/luxe-cli/src/cli/share.rs
Normal file
236
crates/luxe-cli/src/cli/share.rs
Normal file
@@ -0,0 +1,236 @@
|
||||
//! Session sharing — the terminal's session, offered to other front-ends on this machine.
|
||||
//!
|
||||
//! "The terminal" is whichever front-end started the session: the TUI, or the client on the other
|
||||
//! end of `luxe --rpc`. The terminal stays in charge. A client that connects to the local endpoint
|
||||
//! (a Unix socket or a named pipe only this user can open) speaks the protocol `luxe --rpc` does:
|
||||
//! it gets a `ready` frame, then what it missed, then every event as it happens, and its actions
|
||||
//! reach the same engine the terminal drives. A joined client can't switch or close the session,
|
||||
//! and leaving closes nothing. The terminal is told when one joins, leaves, or answers a prompt.
|
||||
|
||||
mod clients;
|
||||
mod guest;
|
||||
mod guests;
|
||||
mod snapshot;
|
||||
|
||||
use super::rpc::{GithubWrites, Policy};
|
||||
use luxe_core::engine::Action;
|
||||
use luxe_core::local_endpoint::{EndpointListener, LiveSession, Publication};
|
||||
use luxe_core::Event;
|
||||
use snapshot::Snapshot;
|
||||
use std::path::Path;
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
use tokio::sync::{broadcast, mpsc};
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
/// Events the terminal hasn't drawn yet, as deep as the stream it reads without sharing.
|
||||
const TERMINAL_BUFFER: usize = 256;
|
||||
|
||||
/// How far a joined client may fall behind before it's dropped and has to join again.
|
||||
///
|
||||
/// Deep because token deltas are one event each, and a phone on a slow link reads in bursts.
|
||||
const CLIENT_BACKLOG: usize = 4096;
|
||||
|
||||
/// The session on offer. Dropping it takes the offer down and disconnects every client.
|
||||
pub(crate) struct Sharing {
|
||||
tasks: Vec<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl Drop for Sharing {
|
||||
fn drop(&mut self) {
|
||||
for task in &self.tasks {
|
||||
task.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What every joined client shares: the session's state, its live events, and the terminal.
|
||||
#[derive(Clone)]
|
||||
struct Shared {
|
||||
snapshot: Arc<Mutex<Snapshot>>,
|
||||
live: broadcast::Sender<Event>,
|
||||
engine_tx: mpsc::Sender<Action>,
|
||||
/// Weak, so the terminal's stream ends with the engine's while clients are still connected:
|
||||
/// `--rpc` exits on that end
|
||||
terminal: mpsc::WeakSender<Event>,
|
||||
cwd: String,
|
||||
policy: Policy,
|
||||
/// What `select_model` resolves through and `list_models` lists
|
||||
models: super::rpc::Models,
|
||||
/// The process whose descendants are refused: this one, outside tests
|
||||
agent_pid: u32,
|
||||
/// Who's joined now, for the terminal's dock
|
||||
guests: guests::GuestList,
|
||||
}
|
||||
|
||||
impl Shared {
|
||||
/// The snapshot, even if a panic elsewhere poisoned the lock: it holds no invariant a
|
||||
/// half-finished update could break that a client couldn't recover from by joining again.
|
||||
fn snapshot(&self) -> MutexGuard<'_, Snapshot> {
|
||||
self.snapshot.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// Fold `event` into the snapshot and hand it to every client, as one step so a client
|
||||
/// joining in between sees it exactly once.
|
||||
fn publish(&self, event: &Event) {
|
||||
let mut snapshot = self.snapshot();
|
||||
snapshot.observe(event);
|
||||
// No receivers is the normal state with nobody joined
|
||||
let _ = self.live.send(event.clone());
|
||||
}
|
||||
|
||||
/// Put a line in front of the person at the terminal.
|
||||
async fn tell_terminal(&self, notice: String) {
|
||||
self.to_terminal(Event::Notice(notice)).await;
|
||||
}
|
||||
|
||||
/// Tell the terminal who's joined now, for its dock.
|
||||
async fn tell_guests(&self) {
|
||||
self.to_terminal(Event::GuestsChanged(self.guests.names())).await;
|
||||
}
|
||||
|
||||
async fn to_terminal(&self, event: Event) {
|
||||
// The terminal gone means the session is ending, and nobody is left to tell
|
||||
let Some(terminal) = self.terminal.upgrade() else { return };
|
||||
let _ = terminal.send(event).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Offer the session `events` belongs to, and return the stream the terminal reads instead.
|
||||
///
|
||||
/// With `joinable` off nothing listens. When the endpoint can't be opened the terminal runs
|
||||
/// exactly as it would unshared, and the reason goes to the log: sharing is a convenience,
|
||||
/// never a reason to refuse to start.
|
||||
pub(crate) fn share(
|
||||
events: mpsc::Receiver<Event>,
|
||||
engine_tx: mpsc::Sender<Action>,
|
||||
settings: &crate::engine_setup::AgentSettings,
|
||||
session_id: &str,
|
||||
) -> (mpsc::Receiver<Event>, Option<Sharing>) {
|
||||
let config = luxe_core::config::Config::load(&settings.state_root);
|
||||
if !config.joinable() {
|
||||
tracing::info!("not sharing the session: joinable is off");
|
||||
return (events, None);
|
||||
}
|
||||
let Some((listener, record)) = open_endpoint(settings, session_id) else {
|
||||
return (events, None);
|
||||
};
|
||||
let (terminal, terminal_rx) = mpsc::channel(TERMINAL_BUFFER);
|
||||
let shared = Shared {
|
||||
snapshot: Arc::new(Mutex::new(Snapshot::new(session_id.to_string()))),
|
||||
live: broadcast::channel(CLIENT_BACKLOG).0,
|
||||
engine_tx,
|
||||
terminal: terminal.downgrade(),
|
||||
cwd: settings.work_root.to_string_lossy().into_owned(),
|
||||
policy: guest_policy(settings, &config),
|
||||
models: Arc::new(super::rpc::CatalogResolver { settings: settings.clone() }),
|
||||
agent_pid: std::process::id(),
|
||||
guests: guests::GuestList::default(),
|
||||
};
|
||||
let tasks = vec![
|
||||
tokio::spawn(clients::accept(listener, shared.clone())),
|
||||
tokio::spawn(tee(events, shared, terminal, record)),
|
||||
];
|
||||
tracing::info!(%session_id, "sharing the session on the local endpoint");
|
||||
(terminal_rx, Some(Sharing { tasks }))
|
||||
}
|
||||
|
||||
/// Listen on the local endpoint and publish the record that lists it.
|
||||
fn open_endpoint(
|
||||
settings: &crate::engine_setup::AgentSettings,
|
||||
session_id: &str,
|
||||
) -> Option<(EndpointListener, Option<Record>)> {
|
||||
let dir = luxe_core::user_dirs::luxe_runtime_dir();
|
||||
let listener = match EndpointListener::bind(&dir) {
|
||||
Ok(listener) => listener,
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, dir = %dir.display(), "not sharing the session");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let record = publish_record(&dir, &listener, settings, session_id);
|
||||
Some((listener, record))
|
||||
}
|
||||
|
||||
/// What a joined client may do: what the terminal may, with GitHub writes left off, since
|
||||
/// `--github-writes` is a choice made for one `--rpc` client and nobody made it for this one.
|
||||
fn guest_policy(
|
||||
settings: &crate::engine_setup::AgentSettings,
|
||||
config: &luxe_core::config::Config,
|
||||
) -> Policy {
|
||||
let mode = crate::engine_setup::effective_permission_mode(
|
||||
&settings.state_root,
|
||||
settings.permission_mode,
|
||||
settings.trust_project,
|
||||
);
|
||||
Policy {
|
||||
github_writes: GithubWrites::for_launch(false, mode),
|
||||
github_sections: config.github.sections(),
|
||||
joined: true,
|
||||
}
|
||||
}
|
||||
|
||||
/// Copy every engine event to the clients and on to the terminal, until either end is gone.
|
||||
///
|
||||
/// Owns the terminal's only strong sender, so the terminal's stream ends when this does.
|
||||
async fn tee(
|
||||
mut events: mpsc::Receiver<Event>,
|
||||
shared: Shared,
|
||||
terminal: mpsc::Sender<Event>,
|
||||
mut record: Option<Record>,
|
||||
) {
|
||||
while let Some(event) = events.recv().await {
|
||||
shared.publish(&event);
|
||||
if let (Event::SessionLoaded { session_id, .. }, Some(record)) = (&event, record.as_mut()) {
|
||||
record.follow(session_id);
|
||||
}
|
||||
if terminal.send(event).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The published record of this session, which `luxe join` and `luxe remote` list.
|
||||
struct Record {
|
||||
session: LiveSession,
|
||||
publication: Publication,
|
||||
}
|
||||
|
||||
impl Record {
|
||||
/// Keep the record naming the session the terminal shows.
|
||||
fn follow(&mut self, session_id: &str) {
|
||||
if self.session.session_id == session_id {
|
||||
return;
|
||||
}
|
||||
self.session.session_id = session_id.to_string();
|
||||
if let Err(error) = self.publication.update(&self.session) {
|
||||
tracing::warn!(%error, "could not update the shared session's record");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Write the record that lists this session, or log why it couldn't be written: the endpoint
|
||||
/// still works for a client that knows its address.
|
||||
fn publish_record(
|
||||
dir: &Path,
|
||||
listener: &EndpointListener,
|
||||
settings: &crate::engine_setup::AgentSettings,
|
||||
session_id: &str,
|
||||
) -> Option<Record> {
|
||||
let session = LiveSession {
|
||||
pid: std::process::id(),
|
||||
session_id: session_id.to_string(),
|
||||
cwd: settings.work_root.to_string_lossy().into_owned(),
|
||||
address: listener.address().to_string(),
|
||||
luxe_version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
};
|
||||
match session.publish(dir) {
|
||||
Ok(publication) => Some(Record { session, publication }),
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, "could not publish the shared session's record");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
185
crates/luxe-cli/src/cli/share/clients.rs
Normal file
185
crates/luxe-cli/src/cli/share/clients.rs
Normal file
@@ -0,0 +1,185 @@
|
||||
//! One joined client at a time: checked, caught up, then served like any RPC client.
|
||||
|
||||
use super::super::rpc;
|
||||
use super::guest::{self, Guest};
|
||||
use super::Shared;
|
||||
use luxe_core::engine::Action;
|
||||
use luxe_core::local_endpoint::{EndpointListener, Joined, LocalStream};
|
||||
use luxe_core::Event;
|
||||
use tokio::sync::broadcast::{self, error::RecvError};
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
/// Actions a client may queue before its reads wait on the engine.
|
||||
const ACTION_BUFFER: usize = 32;
|
||||
|
||||
/// Frames queued for one client's socket; past this, the shared backlog takes the slack.
|
||||
const CLIENT_WRITE_BUFFER: usize = 256;
|
||||
|
||||
/// Bytes of a client's input held between reading it and the protocol taking it.
|
||||
const SIFTED_BUFFER: usize = 64 * 1024;
|
||||
|
||||
/// How long to wait before accepting again after an accept failed, so a lasting failure (out of
|
||||
/// file handles) logs a few lines a second instead of spinning a core.
|
||||
const ACCEPT_RETRY: std::time::Duration = std::time::Duration::from_millis(250);
|
||||
|
||||
/// Take clients until the task is aborted, which disconnects every one of them.
|
||||
pub(super) async fn accept(mut listener: EndpointListener, shared: Shared) {
|
||||
let mut clients = JoinSet::new();
|
||||
loop {
|
||||
match listener.accept().await {
|
||||
Ok(joined) => {
|
||||
clients.spawn(serve_client(joined, shared.clone()));
|
||||
}
|
||||
Err(error) => {
|
||||
tracing::warn!(%error, "could not accept a client of the shared session");
|
||||
tokio::time::sleep(ACCEPT_RETRY).await;
|
||||
}
|
||||
}
|
||||
// Reap finished clients so a long session doesn't keep one entry per visit
|
||||
while clients.try_join_next().is_some() {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Serve one client from its `ready` frame until it leaves or the session ends.
|
||||
async fn serve_client(joined: Joined, shared: Shared) {
|
||||
let (guest, named) = Guest::new(joined.peer);
|
||||
if guest.descends_from(shared.agent_pid).await {
|
||||
tracing::warn!(peer = ?joined.peer, "refused a client started by this session's agent");
|
||||
let notice = format!("Refused {guest}: the agent started it, and can't approve itself");
|
||||
shared.tell_terminal(notice).await;
|
||||
return;
|
||||
}
|
||||
let served = serve_rpc(joined.stream, &shared, guest.clone(), named);
|
||||
let (arrival, ()) = tokio::join!(announce(&shared, guest.clone()), served);
|
||||
tracing::info!(peer = ?joined.peer, %guest, "a client left the shared session");
|
||||
let Some(arrival) = arrival else { return };
|
||||
shared.guests.leave(arrival.seat);
|
||||
shared.tell_guests().await;
|
||||
if arrival.told {
|
||||
shared.tell_terminal(format!("{guest} left this session")).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// How a client was announced: its seat in the dock's list, and whether the transcript was told.
|
||||
struct Arrival {
|
||||
seat: u64,
|
||||
told: bool,
|
||||
}
|
||||
|
||||
/// Seats the client in the dock's list once it has had a moment to say who it is, unless it opened
|
||||
/// as watch-only. Only someone else's client earns a line in the transcript: the person at the
|
||||
/// terminal knows their own phone joined, and a phone reconnects often.
|
||||
async fn announce(shared: &Shared, mut guest: Guest) -> Option<Arrival> {
|
||||
guest.introduced().await;
|
||||
if guest.watch_only() {
|
||||
tracing::info!(%guest, "a watch-only client joined the shared session");
|
||||
return None;
|
||||
}
|
||||
let seat = shared.guests.arrive(guest.to_string());
|
||||
shared.tell_guests().await;
|
||||
let told = !guest.yours();
|
||||
if told {
|
||||
shared.tell_terminal(format!("{guest} joined this session")).await;
|
||||
}
|
||||
Some(Arrival { seat, told })
|
||||
}
|
||||
|
||||
/// The RPC protocol over `stream`, fed from the shared session rather than an engine of its own.
|
||||
async fn serve_rpc(
|
||||
stream: LocalStream,
|
||||
shared: &Shared,
|
||||
guest: Guest,
|
||||
named: tokio::sync::watch::Sender<Option<guest::Introduction>>,
|
||||
) {
|
||||
let (opening, frames, live) = catch_up(shared);
|
||||
let (events_tx, events_rx) = mpsc::channel(CLIENT_WRITE_BUFFER);
|
||||
let _feed = AbortOnDrop(tokio::spawn(feed(frames, live, events_tx)));
|
||||
let (actions_tx, actions_rx) = mpsc::channel(ACTION_BUFFER);
|
||||
let _relay = AbortOnDrop(tokio::spawn(relay(actions_rx, shared.clone(), guest)));
|
||||
let (read, write) = tokio::io::split(stream);
|
||||
let (sifted, input) = tokio::io::duplex(SIFTED_BUFFER);
|
||||
let _sift = AbortOnDrop(tokio::spawn(guest::sift(read, sifted, named)));
|
||||
let (policy, models) = (shared.policy.clone(), shared.models.clone());
|
||||
let served = rpc::serve(opening, events_rx, actions_tx, input, write, models, policy);
|
||||
if let Err(error) = served.await {
|
||||
tracing::warn!(%error, "a shared session's client ended with an error");
|
||||
}
|
||||
}
|
||||
|
||||
/// The `ready` frame and transcript, what else the client missed, and the live stream picking up
|
||||
/// right after it.
|
||||
///
|
||||
/// Taken under the lock `publish` holds, so no event falls between the catch-up and the stream.
|
||||
fn catch_up(shared: &Shared) -> (rpc::Opening, Vec<Event>, broadcast::Receiver<Event>) {
|
||||
let snapshot = shared.snapshot();
|
||||
let ready = Event::Ready {
|
||||
protocol_version: luxe_core::PROTOCOL_VERSION,
|
||||
luxe_version: env!("CARGO_PKG_VERSION").to_string(),
|
||||
session_id: snapshot.session_id().to_string(),
|
||||
cwd: shared.cwd.clone(),
|
||||
github_writes: shared.policy.github_writes.allowed(),
|
||||
};
|
||||
let opening = rpc::Opening { ready, transcript: Some(snapshot.transcript()) };
|
||||
(opening, snapshot.frames(), shared.live.subscribe())
|
||||
}
|
||||
|
||||
/// Hand the client its catch-up and then the live stream, until it falls too far behind.
|
||||
async fn feed(frames: Vec<Event>, mut live: broadcast::Receiver<Event>, out: mpsc::Sender<Event>) {
|
||||
for frame in frames {
|
||||
if out.send(frame).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
loop {
|
||||
let event = match live.recv().await {
|
||||
Ok(event) => event,
|
||||
Err(RecvError::Lagged(missed)) => {
|
||||
let notice = format!("This client fell {missed} events behind: join again");
|
||||
let _ = out.send(Event::Notice(notice)).await;
|
||||
return;
|
||||
}
|
||||
Err(RecvError::Closed) => return,
|
||||
};
|
||||
if out.send(event).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pass the client's actions to the engine, telling the terminal about each answer it gives.
|
||||
async fn relay(mut actions: mpsc::Receiver<Action>, shared: Shared, guest: Guest) {
|
||||
while let Some(action) = actions.recv().await {
|
||||
let answer = answer_notice(&shared, &action, &guest);
|
||||
if shared.engine_tx.send(action).await.is_err() {
|
||||
return;
|
||||
}
|
||||
if let Some(notice) = answer {
|
||||
shared.tell_terminal(notice).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What to tell the terminal about `action`, when it answers a prompt that's still waiting.
|
||||
///
|
||||
/// A prompt the terminal already answered is a no-op for the engine, so it says nothing here.
|
||||
fn answer_notice(shared: &Shared, action: &Action, guest: &Guest) -> Option<String> {
|
||||
use luxe_core::permission::PermissionReply;
|
||||
let (id, verb) = match action {
|
||||
Action::RespondPermission { id, reply: PermissionReply::Reject } => (id.as_str(), "Denied"),
|
||||
Action::RespondPermission { id, .. } => (id.as_str(), "Approved"),
|
||||
Action::AnswerQuestion { id, .. } => (id.as_str(), "Answered"),
|
||||
_ => return None,
|
||||
};
|
||||
let waiting = shared.snapshot().is_waiting(id);
|
||||
waiting.then(|| format!("{verb} from {guest}"))
|
||||
}
|
||||
|
||||
/// Aborts its task when dropped, so a client's helpers end with it.
|
||||
struct AbortOnDrop(tokio::task::JoinHandle<()>);
|
||||
|
||||
impl Drop for AbortOnDrop {
|
||||
fn drop(&mut self) {
|
||||
self.0.abort();
|
||||
}
|
||||
}
|
||||
144
crates/luxe-cli/src/cli/share/guest.rs
Normal file
144
crates/luxe-cli/src/cli/share/guest.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
//! Guest — who a joined client is, as the terminal names it: the name it introduced itself by, or
|
||||
//! its process id.
|
||||
//!
|
||||
//! The bridge introduces each device it relays for, so the terminal says "Approved from iPhone"
|
||||
//! rather than naming the bridge's process. The name is cosmetic, since any program running as
|
||||
//! you could claim one, and the ancestry check goes by process id, not by name.
|
||||
//!
|
||||
//! A client that opens by introducing itself as watch-only, like the bridge's notification
|
||||
//! watcher, isn't announced. Being unseen is why it can't act: everything it sends after that is
|
||||
//! dropped here, before the protocol sees it.
|
||||
|
||||
use luxe_core::local_endpoint::descends_from;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader};
|
||||
use tokio::sync::watch;
|
||||
|
||||
/// How long a new client gets to introduce itself before the terminal says it joined.
|
||||
pub(super) const INTRODUCTION_WAIT: std::time::Duration = std::time::Duration::from_secs(1);
|
||||
|
||||
/// The longest name the terminal shows.
|
||||
const NAME_LIMIT: usize = 40;
|
||||
|
||||
/// What a client said about itself.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(super) struct Introduction {
|
||||
pub(super) name: String,
|
||||
pub(super) watch_only: bool,
|
||||
/// Paired by the person at the terminal, so its comings and goings needn't be told
|
||||
pub(super) yours: bool,
|
||||
}
|
||||
|
||||
/// Who joined.
|
||||
#[derive(Clone)]
|
||||
pub(super) struct Guest {
|
||||
pid: Option<u32>,
|
||||
said: watch::Receiver<Option<Introduction>>,
|
||||
}
|
||||
|
||||
impl Guest {
|
||||
/// A guest known by `pid` until it introduces itself through the returned sender.
|
||||
pub(super) fn new(pid: Option<u32>) -> (Self, watch::Sender<Option<Introduction>>) {
|
||||
let (named, said) = watch::channel(None);
|
||||
(Self { pid, said }, named)
|
||||
}
|
||||
|
||||
/// Whether the client opened as watch-only, so it isn't announced.
|
||||
pub(super) fn watch_only(&self) -> bool {
|
||||
self.said.borrow().as_ref().is_some_and(|said| said.watch_only)
|
||||
}
|
||||
|
||||
/// Whether the bridge said the person at the terminal paired it. Cosmetic, as the name is: it
|
||||
/// decides only whether joining earns a line in the transcript.
|
||||
pub(super) fn yours(&self) -> bool {
|
||||
self.said.borrow().as_ref().is_some_and(|said| said.yours)
|
||||
}
|
||||
|
||||
/// Whether the client is `agent` or a process it started, directly or not.
|
||||
///
|
||||
/// A tripwire, not a wall: a command can detach from its parent first. Unknown ancestry
|
||||
/// passes, since refusing it would refuse every client on a system that hides it.
|
||||
pub(super) async fn descends_from(&self, agent: u32) -> bool {
|
||||
let Some(peer) = self.pid else { return false };
|
||||
let ancestry = tokio::task::spawn_blocking(move || descends_from(peer, agent));
|
||||
ancestry.await.unwrap_or(false)
|
||||
}
|
||||
|
||||
/// Waits up to [`INTRODUCTION_WAIT`] for a name, so "joined" names the device.
|
||||
pub(super) async fn introduced(&mut self) {
|
||||
let named = self.said.wait_for(Option::is_some);
|
||||
let _ = tokio::time::timeout(INTRODUCTION_WAIT, named).await;
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Guest {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if let Some(said) = self.said.borrow().as_ref() {
|
||||
return f.write_str(&said.name);
|
||||
}
|
||||
match self.pid {
|
||||
Some(pid) => write!(f, "a client (pid {pid})"),
|
||||
None => f.write_str("a client"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Copies the client's lines to `protocol`, keeping its introductions for `named`.
|
||||
///
|
||||
/// Read here rather than in the protocol because the name is the share layer's business: the
|
||||
/// engine has no use for it, and the client that started a session has nobody to introduce
|
||||
/// itself to. Once a client opens as watch-only, nothing more of it is read for the protocol.
|
||||
pub(super) async fn sift(
|
||||
client: impl AsyncRead + Unpin,
|
||||
mut protocol: impl AsyncWrite + Unpin,
|
||||
named: watch::Sender<Option<Introduction>>,
|
||||
) {
|
||||
let mut lines = BufReader::new(client).lines();
|
||||
let mut opening = true;
|
||||
while let Ok(Some(line)) = lines.next_line().await {
|
||||
if !for_protocol(&line, &mut opening, &named) {
|
||||
continue;
|
||||
}
|
||||
if protocol.write_all(format!("{line}\n").as_bytes()).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `line` goes on to the protocol: not when it's an introduction, which `named` keeps,
|
||||
/// nor when the client opened as watch-only.
|
||||
fn for_protocol(
|
||||
line: &str,
|
||||
opening: &mut bool,
|
||||
named: &watch::Sender<Option<Introduction>>,
|
||||
) -> bool {
|
||||
if named.borrow().as_ref().is_some_and(|said| said.watch_only) {
|
||||
tracing::warn!("dropped a line from a watch-only client");
|
||||
return false;
|
||||
}
|
||||
let first = std::mem::take(opening);
|
||||
let Some(mut said) = introduction(line) else { return true };
|
||||
said.watch_only &= first;
|
||||
named.send_replace(Some(said));
|
||||
false
|
||||
}
|
||||
|
||||
/// What an `introduce` frame says, its name made fit for one line of the terminal.
|
||||
///
|
||||
/// Anything else, a malformed introduction included, is `None` and goes on to the protocol, which
|
||||
/// refuses it by name.
|
||||
fn introduction(line: &str) -> Option<Introduction> {
|
||||
if !line.contains("introduce") {
|
||||
return None;
|
||||
}
|
||||
let luxe_core::event::Action::Introduce { name, watch_only, yours } =
|
||||
serde_json::from_str(line).ok()?
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
let name: String = name.chars().map(|c| if c.is_control() { ' ' } else { c }).collect();
|
||||
let name: String = name.trim().chars().take(NAME_LIMIT).collect();
|
||||
(!name.is_empty()).then_some(Introduction { name, watch_only, yours })
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
71
crates/luxe-cli/src/cli/share/guest/tests.rs
Normal file
71
crates/luxe-cli/src/cli/share/guest/tests.rs
Normal file
@@ -0,0 +1,71 @@
|
||||
use super::*;
|
||||
|
||||
fn name_of(line: &str) -> Option<String> {
|
||||
introduction(line).map(|said| said.name)
|
||||
}
|
||||
|
||||
/// An introduction gives its name, made fit for one line; nothing else does.
|
||||
///
|
||||
/// The break this catches, one per line: taking the name from another frame (submit), passing a
|
||||
/// newline or escape code into the terminal's notice (control), letting a long name push the
|
||||
/// notice off the line (long), or naming a client nothing (blank).
|
||||
#[test]
|
||||
fn only_an_introduction_names_a_client() {
|
||||
let introduce = |name: &str| format!(r#"{{"kind":"introduce","content":{{"name":"{name}"}}}}"#);
|
||||
assert_eq!(name_of(&introduce("iPhone")).as_deref(), Some("iPhone"));
|
||||
let submit = r#"{"kind":"submit_user_message","content":{"text":"introduce yourself"}}"#;
|
||||
assert_eq!(name_of(submit), None);
|
||||
assert_eq!(name_of(&introduce(r"Alex\n\u001b[2Jphone")).as_deref(), Some("Alex [2Jphone"));
|
||||
assert_eq!(name_of(&introduce(&"x".repeat(60))).map(|name| name.len()), Some(NAME_LIMIT));
|
||||
assert_eq!(name_of(&introduce(" ")), None);
|
||||
}
|
||||
|
||||
/// What `input` passes on to the protocol, and the guest it leaves.
|
||||
async fn sifted(input: &str) -> (String, Guest) {
|
||||
let (sifted, mut protocol) = tokio::io::duplex(1024);
|
||||
let (guest, named) = Guest::new(Some(7));
|
||||
sift(input.as_bytes(), sifted, named).await;
|
||||
let mut passed = String::new();
|
||||
tokio::io::AsyncReadExt::read_to_string(&mut protocol, &mut passed).await.unwrap();
|
||||
(passed, guest)
|
||||
}
|
||||
|
||||
const WATCHER: &str =
|
||||
"{\"kind\":\"introduce\",\"content\":{\"name\":\"Notifications\",\"watch_only\":true}}\n";
|
||||
|
||||
/// The client's other lines reach the protocol in order, and its introduction doesn't.
|
||||
///
|
||||
/// The break this catches: forwarding the introduction, which the protocol refuses back to the
|
||||
/// bridge as if it had sent something wrong; or losing the line after it.
|
||||
#[tokio::test]
|
||||
async fn sifting_keeps_the_introduction_and_passes_the_rest() {
|
||||
let input = "{\"kind\":\"cancel\"}\n{\"kind\":\"introduce\",\"content\":{\"name\":\"iPad\"}}\n\
|
||||
{\"kind\":\"retry\"}\n";
|
||||
let (passed, guest) = sifted(input).await;
|
||||
assert_eq!(passed, "{\"kind\":\"cancel\"}\n{\"kind\":\"retry\"}\n");
|
||||
assert_eq!(guest.to_string(), "iPad");
|
||||
assert!(!guest.watch_only());
|
||||
}
|
||||
|
||||
/// A client that opens as watch-only is marked so, and nothing it sends after reaches the
|
||||
/// protocol.
|
||||
///
|
||||
/// The break this catches: an unannounced client steering or approving, unseen at the desk.
|
||||
#[tokio::test]
|
||||
async fn a_watch_only_client_cant_act() {
|
||||
let (passed, guest) = sifted(&format!("{WATCHER}{{\"kind\":\"cancel\"}}\n")).await;
|
||||
assert_eq!(passed, "");
|
||||
assert!(guest.watch_only());
|
||||
assert_eq!(guest.to_string(), "Notifications");
|
||||
}
|
||||
|
||||
/// Watch-only counts only as the opening line.
|
||||
///
|
||||
/// The break this catches: a client cancelling the turn, then introducing itself as watch-only so
|
||||
/// the terminal never says it was there.
|
||||
#[tokio::test]
|
||||
async fn watch_only_after_acting_is_just_a_name() {
|
||||
let (passed, guest) = sifted(&format!("{{\"kind\":\"cancel\"}}\n{WATCHER}")).await;
|
||||
assert_eq!(passed, "{\"kind\":\"cancel\"}\n");
|
||||
assert!(!guest.watch_only());
|
||||
}
|
||||
59
crates/luxe-cli/src/cli/share/guests.rs
Normal file
59
crates/luxe-cli/src/cli/share/guests.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
//! Guests — who's joined the shared session right now, by name, for the terminal's dock.
|
||||
//!
|
||||
//! Each announced client takes a seat when it joins and gives it up when it leaves, so two of the
|
||||
//! same name (a phone that reconnected before its old socket closed) are counted apart.
|
||||
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::{Arc, Mutex, MutexGuard};
|
||||
|
||||
/// The clients in the session now, in the order they joined.
|
||||
#[derive(Clone, Default)]
|
||||
pub(super) struct GuestList {
|
||||
seated: Arc<Mutex<Vec<(u64, String)>>>,
|
||||
next: Arc<AtomicU64>,
|
||||
}
|
||||
|
||||
impl GuestList {
|
||||
/// Seats `name`, returning the seat to give up on leaving.
|
||||
pub(super) fn arrive(&self, name: String) -> u64 {
|
||||
let seat = self.next.fetch_add(1, Ordering::Relaxed);
|
||||
self.seated().push((seat, name));
|
||||
seat
|
||||
}
|
||||
|
||||
/// Gives up `seat`.
|
||||
pub(super) fn leave(&self, seat: u64) {
|
||||
self.seated().retain(|(taken, _)| *taken != seat);
|
||||
}
|
||||
|
||||
/// Everyone seated, by name.
|
||||
pub(super) fn names(&self) -> Vec<String> {
|
||||
self.seated().iter().map(|(_, name)| name.clone()).collect()
|
||||
}
|
||||
|
||||
/// The list, even if a panic elsewhere poisoned the lock: a stale name is all it could hold.
|
||||
fn seated(&self) -> MutexGuard<'_, Vec<(u64, String)>> {
|
||||
self.seated.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A guest leaves by its seat, so one of two with the same name leaving leaves the other.
|
||||
///
|
||||
/// The break this catches: a phone that reconnected before its old socket closed vanishing
|
||||
/// from the dock when the old one goes, while it's still joined.
|
||||
#[test]
|
||||
fn a_guest_leaves_by_its_seat_not_its_name() {
|
||||
let guests = GuestList::default();
|
||||
let old = guests.arrive("iPhone".into());
|
||||
let _new = guests.arrive("iPhone".into());
|
||||
let ipad = guests.arrive("iPad".into());
|
||||
guests.leave(old);
|
||||
assert_eq!(guests.names(), ["iPhone", "iPad"]);
|
||||
guests.leave(ipad);
|
||||
assert_eq!(guests.names(), ["iPhone"]);
|
||||
}
|
||||
}
|
||||
140
crates/luxe-cli/src/cli/share/snapshot.rs
Normal file
140
crates/luxe-cli/src/cli/share/snapshot.rs
Normal file
@@ -0,0 +1,140 @@
|
||||
//! What a client joining mid-session has missed, kept current from the event stream.
|
||||
//!
|
||||
//! A joiner opens on the same frames a resume would show: the transcript so far, whether a turn is
|
||||
//! running, then the latest state, usage and workspace, the tool cards of a turn still running, and
|
||||
//! any prompt still waiting for an answer. Text a reply has already streamed is not replayed; it
|
||||
//! arrives whole once the message commits.
|
||||
|
||||
use super::super::rpc::SharedTranscript;
|
||||
use luxe_core::event::SessionMessage;
|
||||
use luxe_core::Event;
|
||||
use std::mem::discriminant;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The session as a client joining now should first see it.
|
||||
pub(super) struct Snapshot {
|
||||
session_id: String,
|
||||
/// Shared with the joiners being sent it, so a join costs a reference, not a copy
|
||||
transcript: Arc<Vec<SessionMessage>>,
|
||||
/// Whether a turn has started and not yet ended
|
||||
turning: bool,
|
||||
/// The newest event of each kind where only the newest matters
|
||||
latest: Vec<Event>,
|
||||
/// Tool cards of the turn in flight, by call id
|
||||
activity: Vec<Event>,
|
||||
/// Permission prompts and questions nobody has answered yet
|
||||
waiting: Vec<Event>,
|
||||
}
|
||||
|
||||
impl Snapshot {
|
||||
/// An empty snapshot of `session_id`, before the engine has said anything.
|
||||
pub(super) fn new(session_id: String) -> Self {
|
||||
Self {
|
||||
session_id,
|
||||
transcript: Arc::default(),
|
||||
turning: false,
|
||||
latest: Vec::new(),
|
||||
activity: Vec::new(),
|
||||
waiting: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// The session the stream is about now.
|
||||
pub(super) fn session_id(&self) -> &str {
|
||||
&self.session_id
|
||||
}
|
||||
|
||||
/// Whether the prompt `id` names is still waiting for an answer.
|
||||
pub(super) fn is_waiting(&self, id: &str) -> bool {
|
||||
self.waiting.iter().any(|prompt| prompt_id(prompt) == id)
|
||||
}
|
||||
|
||||
/// Fold one event in.
|
||||
pub(super) fn observe(&mut self, event: &Event) {
|
||||
match event {
|
||||
Event::SessionLoaded { session_id, messages } => self.load(session_id, messages),
|
||||
Event::MessageCommitted { entry_id, message, .. } => {
|
||||
let message =
|
||||
SessionMessage { entry_id: entry_id.clone(), message: message.clone() };
|
||||
// Copies the list only if a joiner is still being sent it, which is brief
|
||||
Arc::make_mut(&mut self.transcript).push(message);
|
||||
}
|
||||
Event::StateChanged(_)
|
||||
| Event::SessionUsage(_)
|
||||
| Event::ContextUsage { .. }
|
||||
| Event::PlanUsage(_)
|
||||
| Event::WorkspaceUpdated(_) => replace_kind(&mut self.latest, event),
|
||||
Event::ToolActivityUpdated(activity) => self.track_activity(&activity.call_id, event),
|
||||
Event::PermissionRequested { .. } | Event::QuestionAsked { .. } => {
|
||||
self.waiting.push(event.clone());
|
||||
}
|
||||
Event::PromptClosed { id } => self.waiting.retain(|prompt| prompt_id(prompt) != id),
|
||||
Event::TurnStarted => self.turning = true,
|
||||
Event::TurnEnded { .. } => self.end_turn(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// The transcript so far, which a new client is sent first, as its `session_loaded` frame.
|
||||
pub(super) fn transcript(&self) -> SharedTranscript {
|
||||
SharedTranscript {
|
||||
session_id: self.session_id.clone(),
|
||||
messages: Arc::clone(&self.transcript),
|
||||
}
|
||||
}
|
||||
|
||||
/// The frames that bring a new client up to now after the transcript, oldest first.
|
||||
pub(super) fn frames(&self) -> Vec<Event> {
|
||||
self.turning
|
||||
.then_some(Event::TurnStarted)
|
||||
.into_iter()
|
||||
.chain(self.latest.iter().cloned())
|
||||
.chain(self.activity.iter().cloned())
|
||||
.chain(self.waiting.iter().cloned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Start over on a loaded session, which replaces everything a front-end shows.
|
||||
fn load(&mut self, session_id: &str, messages: &[SessionMessage]) {
|
||||
self.session_id = session_id.to_string();
|
||||
self.transcript = Arc::new(messages.to_vec());
|
||||
self.end_turn();
|
||||
}
|
||||
|
||||
/// Nothing of a finished turn is still running or waiting.
|
||||
fn end_turn(&mut self) {
|
||||
self.turning = false;
|
||||
self.activity.clear();
|
||||
self.waiting.clear();
|
||||
}
|
||||
|
||||
/// Keep only the newest card for `call_id`.
|
||||
fn track_activity(&mut self, call_id: &str, event: &Event) {
|
||||
self.activity.retain(|kept| activity_call(kept) != Some(call_id));
|
||||
self.activity.push(event.clone());
|
||||
}
|
||||
}
|
||||
|
||||
/// Replace the kept event of `event`'s kind, or keep it as the first of its kind.
|
||||
fn replace_kind(kept: &mut Vec<Event>, event: &Event) {
|
||||
let kind = discriminant(event);
|
||||
kept.retain(|old| discriminant(old) != kind);
|
||||
kept.push(event.clone());
|
||||
}
|
||||
|
||||
/// The id a waiting prompt answers to, in the form `PromptClosed` carries.
|
||||
fn prompt_id(prompt: &Event) -> &str {
|
||||
match prompt {
|
||||
Event::PermissionRequested { id, .. } => id.as_str(),
|
||||
Event::QuestionAsked { id, .. } => id.as_str(),
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
/// The tool call a kept card belongs to.
|
||||
fn activity_call(event: &Event) -> Option<&str> {
|
||||
match event {
|
||||
Event::ToolActivityUpdated(activity) => Some(&activity.call_id),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
385
crates/luxe-cli/src/cli/share/tests.rs
Normal file
385
crates/luxe-cli/src/cli/share/tests.rs
Normal file
@@ -0,0 +1,385 @@
|
||||
use super::*;
|
||||
use luxe_core::local_endpoint::{connect, LocalStream};
|
||||
use luxe_core::permission::PermissionRequestId;
|
||||
use luxe_core::Message;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
|
||||
/// A shared session with no engine or terminal behind it, and the far ends of both.
|
||||
struct Harness {
|
||||
shared: Shared,
|
||||
engine_rx: mpsc::Receiver<Action>,
|
||||
terminal_rx: mpsc::Receiver<Event>,
|
||||
/// The strong end `tee` owns in a real session, which keeps the terminal's stream open
|
||||
terminal: mpsc::Sender<Event>,
|
||||
}
|
||||
|
||||
/// No process descends from this one, so the tripwire lets the test's own connections in.
|
||||
const NOBODY: u32 = u32::MAX;
|
||||
|
||||
/// A catalog with nothing in it, so no test reads the developer's `models.json`.
|
||||
struct NoModels;
|
||||
|
||||
impl super::super::rpc::ModelResolver for NoModels {
|
||||
fn resolve(&self, provider: &str, id: &str) -> luxe_core::ModelInfo {
|
||||
let r#ref = luxe_core::provider::ModelRef::new(provider, id);
|
||||
luxe_core::ModelInfo { r#ref, ..Default::default() }
|
||||
}
|
||||
|
||||
fn choices(&self) -> Vec<luxe_core::event::ModelChoice> {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn harness(agent_pid: u32) -> Harness {
|
||||
let (engine_tx, engine_rx) = mpsc::channel(16);
|
||||
let (terminal, terminal_rx) = mpsc::channel(16);
|
||||
let shared = Shared {
|
||||
snapshot: Arc::new(Mutex::new(Snapshot::new("session-1".into()))),
|
||||
live: broadcast::channel(CLIENT_BACKLOG).0,
|
||||
engine_tx,
|
||||
terminal: terminal.downgrade(),
|
||||
cwd: "/work".into(),
|
||||
policy: Policy {
|
||||
github_writes: GithubWrites::NotEnabled,
|
||||
github_sections: Vec::new(),
|
||||
joined: true,
|
||||
},
|
||||
models: Arc::new(NoModels),
|
||||
agent_pid,
|
||||
guests: super::guests::GuestList::default(),
|
||||
};
|
||||
Harness { shared, engine_rx, terminal_rx, terminal }
|
||||
}
|
||||
|
||||
fn permission_asked(id: &PermissionRequestId) -> Event {
|
||||
Event::PermissionRequested {
|
||||
id: id.clone(),
|
||||
session_id: "session-1".into(),
|
||||
tool: "bash".into(),
|
||||
permission: "bash".into(),
|
||||
summary: "Run: cargo test".into(),
|
||||
always_patterns: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn committed(text: &str) -> Event {
|
||||
Event::MessageCommitted {
|
||||
session_id: "session-1".into(),
|
||||
entry_id: luxe_core::session::EntryId(text.into()),
|
||||
message: Message::user(text),
|
||||
}
|
||||
}
|
||||
|
||||
fn kinds(frames: &[Event]) -> Vec<&'static str> {
|
||||
frames
|
||||
.iter()
|
||||
.map(|frame| match frame {
|
||||
Event::SessionLoaded { .. } => "session_loaded",
|
||||
Event::StateChanged(_) => "state_changed",
|
||||
Event::ContextUsage { .. } => "context_usage",
|
||||
Event::PermissionRequested { .. } => "permission_requested",
|
||||
Event::TurnStarted => "turn_started",
|
||||
_ => "other",
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// A joiner opens on the transcript, then the newest of each kept state, then the prompt.
|
||||
///
|
||||
/// The break this catches: dropping the commit fold, which opens a joined phone on an empty
|
||||
/// conversation, or keeping every usage update, which replays a turn's worth of stale readings.
|
||||
#[test]
|
||||
fn a_joiner_catches_up_on_the_transcript_state_and_waiting_prompt() {
|
||||
let mut snapshot = Snapshot::new("session-1".into());
|
||||
let id = PermissionRequestId::new();
|
||||
for event in [
|
||||
committed("first"),
|
||||
Event::ContextUsage { used_tokens: 1, context_window: 10 },
|
||||
committed("second"),
|
||||
Event::ContextUsage { used_tokens: 2, context_window: 10 },
|
||||
permission_asked(&id),
|
||||
] {
|
||||
snapshot.observe(&event);
|
||||
}
|
||||
let frames = snapshot.frames();
|
||||
assert_eq!(kinds(&frames), ["context_usage", "permission_requested"]);
|
||||
assert_eq!(snapshot.transcript().messages.len(), 2);
|
||||
assert!(matches!(frames[0], Event::ContextUsage { used_tokens: 2, .. }));
|
||||
}
|
||||
|
||||
/// A joiner mid-turn is told a turn is running, right after the transcript; one after it isn't.
|
||||
///
|
||||
/// The break this catches: leaving `turn_started` out of the catch-up, so a phone opening on a
|
||||
/// running session shows it idle, with no stop button, until the next token arrives.
|
||||
#[test]
|
||||
fn a_joiner_mid_turn_hears_the_turn_started() {
|
||||
let mut snapshot = Snapshot::new("session-1".into());
|
||||
snapshot.observe(&Event::TurnStarted);
|
||||
snapshot.observe(&committed("first"));
|
||||
assert_eq!(kinds(&snapshot.frames()), ["turn_started"]);
|
||||
snapshot.observe(&Event::TurnEnded { reason: luxe_core::event::StopReason::EndTurn });
|
||||
assert!(snapshot.frames().is_empty());
|
||||
}
|
||||
|
||||
/// A prompt someone answered isn't put in front of the next joiner.
|
||||
///
|
||||
/// The break this catches: ignoring `prompt_closed`, so a phone joining after the terminal
|
||||
/// approved shows a prompt whose answer the engine would throw away.
|
||||
#[test]
|
||||
fn an_answered_prompt_is_not_replayed() {
|
||||
let mut snapshot = Snapshot::new("session-1".into());
|
||||
let answered = PermissionRequestId::new();
|
||||
let open = PermissionRequestId::new();
|
||||
snapshot.observe(&permission_asked(&answered));
|
||||
snapshot.observe(&permission_asked(&open));
|
||||
snapshot.observe(&Event::PromptClosed { id: answered.as_str().into() });
|
||||
assert!(!snapshot.is_waiting(answered.as_str()));
|
||||
assert!(snapshot.is_waiting(open.as_str()));
|
||||
}
|
||||
|
||||
/// Loading a session replaces the transcript rather than adding to it.
|
||||
///
|
||||
/// The break this catches: appending on `session_loaded`, which shows a joiner the previous
|
||||
/// session's conversation above the one the terminal switched to.
|
||||
#[test]
|
||||
fn a_loaded_session_replaces_what_came_before() {
|
||||
let mut snapshot = Snapshot::new("session-1".into());
|
||||
snapshot.observe(&committed("old"));
|
||||
snapshot.observe(&Event::SessionLoaded { session_id: "session-2".into(), messages: vec![] });
|
||||
let transcript = snapshot.transcript();
|
||||
assert_eq!(transcript.session_id, "session-2");
|
||||
assert!(transcript.messages.is_empty());
|
||||
}
|
||||
|
||||
/// A commit while a joiner is still being sent the transcript leaves the joiner's copy alone.
|
||||
///
|
||||
/// The break this catches: the snapshot appending to a list a joiner shares, which would put a
|
||||
/// message in a `session_loaded` frame the live stream then sends again.
|
||||
#[test]
|
||||
fn a_commit_mid_join_leaves_the_joiners_transcript_alone() {
|
||||
let mut snapshot = Snapshot::new("session-1".into());
|
||||
snapshot.observe(&committed("first"));
|
||||
let sending = snapshot.transcript();
|
||||
snapshot.observe(&committed("second"));
|
||||
assert_eq!(sending.messages.len(), 1);
|
||||
assert_eq!(snapshot.transcript().messages.len(), 2);
|
||||
}
|
||||
|
||||
/// Read one frame off a client's stream.
|
||||
async fn next_frame(lines: &mut tokio::io::Lines<impl tokio::io::AsyncBufRead + Unpin>) -> String {
|
||||
let line = tokio::time::timeout(std::time::Duration::from_secs(5), lines.next_line());
|
||||
line.await.expect("a frame in time").expect("readable").expect("the stream is open")
|
||||
}
|
||||
|
||||
/// Wait for the terminal to be told something containing `needle`.
|
||||
async fn notice_containing(terminal_rx: &mut mpsc::Receiver<Event>, needle: &str) -> String {
|
||||
let wait = tokio::time::timeout(std::time::Duration::from_secs(5), async {
|
||||
while let Some(event) = terminal_rx.recv().await {
|
||||
if let Event::Notice(text) = event {
|
||||
if text.contains(needle) {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
}
|
||||
panic!("the terminal stream ended before a notice about {needle:?}");
|
||||
});
|
||||
wait.await.expect("a notice in time")
|
||||
}
|
||||
|
||||
type ClientLines = tokio::io::Lines<BufReader<tokio::io::ReadHalf<LocalStream>>>;
|
||||
|
||||
/// Serve `shared` on a fresh endpoint and connect one client to it.
|
||||
async fn join(
|
||||
shared: &Shared,
|
||||
) -> (tempfile::TempDir, JoinHandle<()>, ClientLines, tokio::io::WriteHalf<LocalStream>) {
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let listener = EndpointListener::bind(dir.path()).expect("bind");
|
||||
let address = listener.address().to_string();
|
||||
let accept = tokio::spawn(clients::accept(listener, shared.clone()));
|
||||
let stream = connect(&address).await.expect("connect");
|
||||
let (read, write) = tokio::io::split(stream);
|
||||
(dir, accept, BufReader::new(read).lines(), write)
|
||||
}
|
||||
|
||||
/// A command the agent ran is turned away before it sees a frame, and the terminal hears why.
|
||||
///
|
||||
/// The break this catches: skipping the ancestry check, so the agent's own `bash` could join
|
||||
/// its session and approve the prompt it's waiting on.
|
||||
#[tokio::test]
|
||||
async fn the_agents_own_command_is_refused() {
|
||||
let Harness { shared, mut engine_rx, mut terminal_rx, terminal: _terminal } =
|
||||
harness(std::process::id());
|
||||
let (_dir, _accept, mut lines, _write) = join(&shared).await;
|
||||
notice_containing(&mut terminal_rx, "Refused").await;
|
||||
let read = tokio::time::timeout(std::time::Duration::from_secs(5), lines.next_line());
|
||||
assert!(matches!(read.await, Ok(Ok(None)) | Ok(Err(_))), "the refused client got a frame");
|
||||
assert!(engine_rx.try_recv().is_err());
|
||||
}
|
||||
|
||||
/// A client joins over the endpoint, catches up, answers, and leaves without closing anything.
|
||||
///
|
||||
/// The break this catches: a joined client's hangup reaching the engine as `close`, which would
|
||||
/// end the terminal's session the moment a phone dropped off the network.
|
||||
#[tokio::test]
|
||||
async fn a_client_joins_answers_and_leaves_without_closing_the_session() {
|
||||
let Harness { shared, mut engine_rx, mut terminal_rx, terminal: _terminal } = harness(NOBODY);
|
||||
let id = PermissionRequestId::new();
|
||||
shared.publish(&permission_asked(&id));
|
||||
let (_dir, _accept, mut lines, mut write) = join(&shared).await;
|
||||
assert!(next_frame(&mut lines).await.contains(r#""kind":"ready""#));
|
||||
assert!(next_frame(&mut lines).await.contains(r#""kind":"session_loaded""#));
|
||||
assert!(next_frame(&mut lines).await.contains(r#""kind":"permission_requested""#));
|
||||
notice_containing(&mut terminal_rx, "joined").await;
|
||||
|
||||
let answer = format!(
|
||||
r#"{{"kind":"respond_permission","content":{{"id":"{}","reply":"once"}}}}"#,
|
||||
id.as_str()
|
||||
);
|
||||
write.write_all(format!("{answer}\n").as_bytes()).await.expect("write");
|
||||
assert!(matches!(engine_rx.recv().await, Some(Action::RespondPermission { .. })));
|
||||
notice_containing(&mut terminal_rx, "Approved from").await;
|
||||
|
||||
drop((lines, write));
|
||||
notice_containing(&mut terminal_rx, "left").await;
|
||||
assert!(engine_rx.try_recv().is_err(), "leaving must not reach the engine");
|
||||
}
|
||||
|
||||
/// A client that introduces itself is named when it joins and when it answers.
|
||||
///
|
||||
/// The break this catches: the terminal saying who joined before the introduction can arrive, or
|
||||
/// naming the bridge's process, so "Approved from" never says which device answered.
|
||||
#[tokio::test]
|
||||
async fn an_introduced_client_is_named_when_it_joins_and_answers() {
|
||||
let Harness { shared, mut engine_rx, mut terminal_rx, terminal: _terminal } = harness(NOBODY);
|
||||
let id = PermissionRequestId::new();
|
||||
shared.publish(&permission_asked(&id));
|
||||
let (_dir, _accept, _lines, mut write) = join(&shared).await;
|
||||
let introduce = r#"{"kind":"introduce","content":{"name":"iPhone"}}"#;
|
||||
write.write_all(format!("{introduce}\n").as_bytes()).await.expect("write");
|
||||
notice_containing(&mut terminal_rx, "iPhone joined").await;
|
||||
|
||||
let answer = format!(
|
||||
r#"{{"kind":"respond_permission","content":{{"id":"{}","reply":"once"}}}}"#,
|
||||
id.as_str()
|
||||
);
|
||||
write.write_all(format!("{answer}\n").as_bytes()).await.expect("write");
|
||||
assert!(matches!(engine_rx.recv().await, Some(Action::RespondPermission { .. })));
|
||||
notice_containing(&mut terminal_rx, "Approved from iPhone").await;
|
||||
}
|
||||
|
||||
/// Every notice the terminal hears, up to and including the one containing `needle`.
|
||||
async fn notices_until(terminal_rx: &mut mpsc::Receiver<Event>, needle: &str) -> Vec<String> {
|
||||
let mut heard = Vec::new();
|
||||
while !heard.last().is_some_and(|text: &String| text.contains(needle)) {
|
||||
heard.push(notice_containing(terminal_rx, "").await);
|
||||
}
|
||||
heard
|
||||
}
|
||||
|
||||
/// A client that opens as watch-only gets the session but isn't announced, joining or leaving.
|
||||
/// An ordinary client, joining and leaving after it, marks where its notices would have been.
|
||||
///
|
||||
/// The break this catches: the terminal saying "Notifications joined this session" in every
|
||||
/// session while a phone has notifications on.
|
||||
#[tokio::test]
|
||||
async fn a_watch_only_client_comes_and_goes_unannounced() {
|
||||
let Harness { shared, engine_rx: _engine_rx, mut terminal_rx, terminal: _terminal } =
|
||||
harness(NOBODY);
|
||||
let (_dir, _accept, mut lines, mut write) = join(&shared).await;
|
||||
let watcher = r#"{"kind":"introduce","content":{"name":"Notifications","watch_only":true}}"#;
|
||||
write.write_all(format!("{watcher}\n").as_bytes()).await.expect("write");
|
||||
assert!(next_frame(&mut lines).await.contains(r#""kind":"ready""#));
|
||||
drop((lines, write));
|
||||
|
||||
let (_dir, _accept, lines, mut write) = join(&shared).await;
|
||||
let introduce = r#"{"kind":"introduce","content":{"name":"iPad"}}"#;
|
||||
write.write_all(format!("{introduce}\n").as_bytes()).await.expect("write");
|
||||
let mut heard = notices_until(&mut terminal_rx, "iPad joined").await;
|
||||
drop((lines, write));
|
||||
heard.extend(notices_until(&mut terminal_rx, "iPad left").await);
|
||||
assert!(heard.iter().all(|text| !text.contains("Notifications")), "{heard:?}");
|
||||
}
|
||||
|
||||
/// Everything the terminal hears until it's told the guests are `expected`.
|
||||
async fn heard_until_guests(
|
||||
terminal_rx: &mut mpsc::Receiver<Event>,
|
||||
expected: &[&str],
|
||||
) -> Vec<Event> {
|
||||
let wait = tokio::time::timeout(std::time::Duration::from_secs(5), async {
|
||||
let mut heard = Vec::new();
|
||||
while let Some(event) = terminal_rx.recv().await {
|
||||
let done = matches!(&event, Event::GuestsChanged(guests) if guests == expected);
|
||||
heard.push(event);
|
||||
if done {
|
||||
return heard;
|
||||
}
|
||||
}
|
||||
panic!("the terminal stream ended before the guests were {expected:?}");
|
||||
});
|
||||
wait.await.expect("the guests in time")
|
||||
}
|
||||
|
||||
fn notices(heard: &[Event]) -> Vec<&str> {
|
||||
heard
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::Notice(text) => Some(text.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The owner's own device joins and leaves the dock's list without a line in the transcript.
|
||||
///
|
||||
/// The break this catches: a phone's every reconnect adding "joined" and "left" lines to the
|
||||
/// owner's transcript, or the dock never told it came or went.
|
||||
#[tokio::test]
|
||||
async fn the_owners_device_is_listed_quietly() {
|
||||
let Harness { shared, engine_rx: _engine_rx, mut terminal_rx, terminal: _terminal } =
|
||||
harness(NOBODY);
|
||||
let (_dir, _accept, lines, mut write) = join(&shared).await;
|
||||
let yours = r#"{"kind":"introduce","content":{"name":"iPhone","yours":true}}"#;
|
||||
write.write_all(format!("{yours}\n").as_bytes()).await.expect("write");
|
||||
let mut heard = heard_until_guests(&mut terminal_rx, &["iPhone"]).await;
|
||||
drop((lines, write));
|
||||
heard.extend(heard_until_guests(&mut terminal_rx, &[]).await);
|
||||
assert!(notices(&heard).is_empty(), "{heard:?}");
|
||||
}
|
||||
|
||||
/// Someone else's device is listed and still announced, joining and leaving.
|
||||
///
|
||||
/// The break this catches: a stranger's device joining the owner's session unremarked, or kept
|
||||
/// off the dock's list.
|
||||
#[tokio::test]
|
||||
async fn anyone_elses_device_is_listed_and_announced() {
|
||||
let Harness { shared, engine_rx: _engine_rx, mut terminal_rx, terminal: _terminal } =
|
||||
harness(NOBODY);
|
||||
let (_dir, _accept, lines, mut write) = join(&shared).await;
|
||||
let theirs = r#"{"kind":"introduce","content":{"name":"Sam's iPad"}}"#;
|
||||
write.write_all(format!("{theirs}\n").as_bytes()).await.expect("write");
|
||||
let mut heard = heard_until_guests(&mut terminal_rx, &["Sam's iPad"]).await;
|
||||
drop((lines, write));
|
||||
heard.extend(notices_until(&mut terminal_rx, "left").await.into_iter().map(Event::Notice));
|
||||
let said = notices(&heard);
|
||||
assert!(said.iter().any(|text| text.contains("Sam's iPad joined")), "{said:?}");
|
||||
assert!(said.iter().any(|text| text.contains("Sam's iPad left")), "{said:?}");
|
||||
}
|
||||
|
||||
/// The terminal's stream ends when the engine's does, even with a client still connected.
|
||||
///
|
||||
/// The break this catches: a connected client holding the terminal's stream open, so `luxe --rpc`
|
||||
/// never sees the session end and never exits.
|
||||
#[tokio::test]
|
||||
async fn the_terminal_stream_ends_with_the_engines() {
|
||||
let Harness { shared, engine_rx: _engine_rx, mut terminal_rx, terminal } = harness(NOBODY);
|
||||
let (events_tx, events) = mpsc::channel(4);
|
||||
let (_dir, _accept, mut lines, _write) = join(&shared).await;
|
||||
assert!(next_frame(&mut lines).await.contains(r#""kind":"ready""#));
|
||||
notice_containing(&mut terminal_rx, "joined").await;
|
||||
|
||||
let tee = tokio::spawn(tee(events, shared, terminal, None));
|
||||
drop(events_tx);
|
||||
tee.await.expect("tee ends with the engine's stream");
|
||||
let end = tokio::time::timeout(std::time::Duration::from_secs(5), terminal_rx.recv());
|
||||
assert_eq!(end.await.expect("the stream ends in time"), None);
|
||||
}
|
||||
@@ -66,6 +66,7 @@ pub(super) async fn run_tui(
|
||||
events: engine_rx,
|
||||
closer,
|
||||
session_id,
|
||||
claim: _claim,
|
||||
} = start_engine(&settings).await?;
|
||||
let event_rx = crate::workspace_runtime::bridge(
|
||||
engine_rx,
|
||||
@@ -75,6 +76,8 @@ pub(super) async fn run_tui(
|
||||
},
|
||||
crate::workspace_runtime::pull_request_watch(&settings.work_root),
|
||||
);
|
||||
let (event_rx, _sharing) =
|
||||
super::share::share(event_rx, engine_tx.clone(), &settings, &session_id.0);
|
||||
let exit = main_loop::run(
|
||||
event_rx,
|
||||
engine_tx.clone(),
|
||||
@@ -180,7 +183,13 @@ impl Next<'_> {
|
||||
// Another of your sessions, in its own worktree when it has one
|
||||
shutdown_workspace_services().await;
|
||||
let mut next = self.fresh(ResumeMode::Session(launch.session.clone()));
|
||||
match enter_session_worktree(&launch.session, &mut next).await {
|
||||
let state_root = &self.settings.state_root;
|
||||
let entered =
|
||||
match crate::engine_setup::open_elsewhere_now(state_root, &launch.session) {
|
||||
Some(why) => Err(anyhow::anyhow!(why)),
|
||||
None => enter_session_worktree(&launch.session, &mut next).await,
|
||||
};
|
||||
match entered {
|
||||
Ok(()) => *self.settings = next,
|
||||
Err(error) => self.carry_on(
|
||||
session_id,
|
||||
|
||||
@@ -23,7 +23,9 @@ mod spend;
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test_env;
|
||||
use session::attach_session;
|
||||
pub(crate) use session::{resumed_session, IssueBinding, ResumeMode, WorktreeBinding};
|
||||
pub(crate) use session::{
|
||||
open_elsewhere_now, resumed_session, IssueBinding, ResumeMode, WorktreeBinding,
|
||||
};
|
||||
pub(crate) use settings::{AgentSettings, CliOverrides};
|
||||
pub(crate) use spend::LoopSpendSink;
|
||||
mod tooling_summary;
|
||||
@@ -57,6 +59,9 @@ pub(crate) struct EngineHandle {
|
||||
pub(crate) session_id: luxe_core::SessionId,
|
||||
/// How a front-end that is exiting lets the engine finish first.
|
||||
pub(crate) closer: EngineCloser,
|
||||
/// The claim on the session taken before the engine existed, so nobody else can take it
|
||||
/// before the engine claims it too; dropping it leaves the engine's own
|
||||
pub(crate) claim: Option<luxe_core::session::SessionClaim>,
|
||||
}
|
||||
|
||||
/// What an idle engine needs to wind down with no `SessionEnd` hooks to wait on.
|
||||
@@ -126,7 +131,7 @@ pub(crate) async fn build_engine(
|
||||
let todo_root = prepare_state_dirs(settings).await?;
|
||||
|
||||
let mut store = open_session_store(&settings.state_root);
|
||||
let session_id = attach_session(settings, &mut store)?;
|
||||
let (session_id, claim) = attach_session(settings, &mut store)?;
|
||||
|
||||
let model = settings.launch_model();
|
||||
|
||||
@@ -218,7 +223,7 @@ pub(crate) async fn build_engine(
|
||||
.map_err(|error| anyhow::anyhow!("engine channel closed: {error}"))?;
|
||||
}
|
||||
|
||||
Ok(EngineHandle { actions, events, session_id: attached, closer })
|
||||
Ok(EngineHandle { actions, events, session_id: attached, closer, claim })
|
||||
}
|
||||
|
||||
/// Makes the directories a session writes into, and answers where its checklists go.
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use luxe_core::session::{SessionId, SessionStore};
|
||||
use luxe_core::session::{SessionClaim, SessionError, SessionId, SessionStore};
|
||||
|
||||
use super::AgentSettings;
|
||||
|
||||
@@ -135,7 +135,7 @@ pub(crate) struct WorktreeBinding {
|
||||
pub(super) fn attach_session(
|
||||
settings: &AgentSettings,
|
||||
store: &mut luxe_core::session::JsonlSessionStore,
|
||||
) -> anyhow::Result<SessionId> {
|
||||
) -> anyhow::Result<(SessionId, Option<SessionClaim>)> {
|
||||
let session_id = match (&settings.resume, resumed_session(store, &settings.resume)) {
|
||||
(ResumeMode::New, _) => store.create(&settings.state_root.to_string_lossy())?,
|
||||
(_, Some(id)) => {
|
||||
@@ -154,6 +154,8 @@ pub(super) fn attach_session(
|
||||
anyhow::bail!("session '{id}' not found for this project")
|
||||
}
|
||||
};
|
||||
// Before the records below, which are writes too
|
||||
let claim = claim_session(store, &session_id)?;
|
||||
// A session in a worktree says so in its own log, which is how resuming finds the worktree
|
||||
if let Some(binding) = &settings.worktree {
|
||||
record_worktree(store, &session_id, binding, &settings.state_root);
|
||||
@@ -164,7 +166,50 @@ pub(super) fn attach_session(
|
||||
if let Some((number, url)) = &settings.pull_request {
|
||||
record_pull_request(store, &session_id, *number, url);
|
||||
}
|
||||
Ok(session_id)
|
||||
Ok((session_id, claim))
|
||||
}
|
||||
|
||||
/// Claim `session_id` for this process, or say which Luxe has it open and how to get there.
|
||||
fn claim_session(
|
||||
store: &luxe_core::session::JsonlSessionStore,
|
||||
session_id: &SessionId,
|
||||
) -> anyhow::Result<Option<SessionClaim>> {
|
||||
match store.claim(session_id) {
|
||||
Ok(claim) => Ok(claim),
|
||||
Err(SessionError::OpenElsewhere(id)) => anyhow::bail!(open_elsewhere(&id)),
|
||||
Err(error) => Err(error.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Why `session` can't be opened here right now, when another Luxe has it open.
|
||||
///
|
||||
/// Asked before leaving the session that's running, so the reader stays where they are rather
|
||||
/// than landing outside the TUI. This process's own claims don't count against it.
|
||||
pub(crate) fn open_elsewhere_now(state_root: &std::path::Path, session: &str) -> Option<String> {
|
||||
let store = super::open_session_store(state_root);
|
||||
let id = SessionId(session.to_string());
|
||||
match store.claim(&id) {
|
||||
Err(SessionError::OpenElsewhere(id)) => Some(open_elsewhere(&id)),
|
||||
// Any other failure meets the session's start, which reports it in full
|
||||
Ok(_) | Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the session is open, when the Luxe that has it says so, and what to do about it.
|
||||
fn open_elsewhere(id: &SessionId) -> String {
|
||||
let runtime = luxe_core::user_dirs::luxe_runtime_dir();
|
||||
let holder = luxe_core::local_endpoint::live_sessions(&runtime)
|
||||
.into_iter()
|
||||
.find(|live| live.session_id == id.0);
|
||||
let place = match holder {
|
||||
Some(live) => format!(" (process {}, in {})", live.pid, live.cwd),
|
||||
None => String::new(),
|
||||
};
|
||||
// `luxe join` is not offered: it speaks the protocol, so a person running it sees JSON
|
||||
format!(
|
||||
"session {id} is already open in another Luxe{place}. Carry on there, or close it \
|
||||
there first"
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -229,4 +274,35 @@ mod tests {
|
||||
assert_eq!(resumed_session(&store, &ResumeMode::Session("nope".into())), None);
|
||||
assert_eq!(resumed_session(&store, &ResumeMode::PullRequest(9)), None);
|
||||
}
|
||||
|
||||
/// A session another process holds is refused up front, with the way to reach it, and the
|
||||
/// check made before leaving a running session says the same.
|
||||
///
|
||||
/// The break this catches: attaching without a claim, so a second `luxe --resume` opens a
|
||||
/// second writer on the log, or a resume from inside the TUI that lands the reader outside it.
|
||||
#[test]
|
||||
fn a_session_open_in_another_process_is_refused_and_says_so() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let mut store = open_session_store(dir.path());
|
||||
let id = store.create("/work").unwrap();
|
||||
let lock = store.session_path(&id).unwrap().with_extension("lock");
|
||||
let other_process = std::fs::File::create(&lock).unwrap();
|
||||
other_process.try_lock().unwrap();
|
||||
let settings = {
|
||||
let _env = super::super::test_env::ENV_LOCK
|
||||
.lock()
|
||||
.unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
let _home = super::super::test_env::isolated_env();
|
||||
let mut settings = AgentSettings::resolve(dir.path().to_path_buf(), Default::default());
|
||||
settings.resume = ResumeMode::Session(id.0.clone());
|
||||
settings
|
||||
};
|
||||
|
||||
let refused = attach_session(&settings, &mut store).expect_err("held elsewhere");
|
||||
let expected = format!("session {id} is already open in another Luxe");
|
||||
assert!(refused.to_string().contains(&expected), "{refused}");
|
||||
assert!(open_elsewhere_now(dir.path(), &id.0).is_some());
|
||||
drop(other_process);
|
||||
assert!(open_elsewhere_now(dir.path(), &id.0).is_none());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -469,6 +469,16 @@ script can tell a failure from an answer without reading the output.
|
||||
| `luxe models show <provider/id>` | What Luxe knows about a model, and where each value came from. A bare ID uses the active provider. |
|
||||
| `luxe models refresh` | Refresh the model catalog. `--force` even if it is fresh. |
|
||||
| `luxe sessions list` | Sessions in this project, newest first |
|
||||
| `luxe join [id]` | Join a session another Luxe is running, speaking the [RPC protocol](rpc.md#join-a-running-session) on stdin and stdout. The id can be its start, or the Luxe's process id; without one, it joins the session running in this folder. |
|
||||
| `luxe join --list` | Sessions running on this machine that can be joined |
|
||||
| `luxe remote` | Serve this machine's sessions to your own phone or tablet. Listens on this machine only, for Tailscale Serve to carry to your devices, and lets in this machine's tailnet owner from a paired device. |
|
||||
| `luxe remote --port <n>` | The port it listens on (default 4519) |
|
||||
| `luxe remote --allow <login>` | Also let this tailnet login in, as Tailscale names it (`alex@github`). Repeatable. |
|
||||
| `luxe remote setup` | Check Tailscale, offer to publish the bridge on your tailnet with Tailscale Serve, offer to run it at login as a user service, then show a QR code to pair a device. Each step that changes something outside Luxe asks first. |
|
||||
| `luxe remote pair` | Show a QR code and link that pair one phone or tablet. They work once, within ten minutes. |
|
||||
| `luxe remote devices` | The paired devices |
|
||||
| `luxe remote devices revoke <id>` | Unpair a device. Its open sessions close within a few seconds. |
|
||||
| `luxe remote service remove` | Stop the bridge's user service and stop it starting at login |
|
||||
| `luxe mcp list` | Configured MCP servers |
|
||||
| `luxe mcp add --id <id> --name <name>` | Add a server to the project's `.luxe/mcp.json`, with `--command "<program> <args>"` or `--url <url>`. It counts as a [project's server](mcp.md#servers-a-project-brings). |
|
||||
| `luxe mcp remove --id <id>` | Remove a server from the project's `.luxe/mcp.json` |
|
||||
@@ -846,6 +856,19 @@ This is local to your machine and on by default. Outside herdr nothing is sent.
|
||||
{ "herdr": false }
|
||||
```
|
||||
|
||||
### Joining a session
|
||||
|
||||
A running Luxe lets another program on this machine join its session: `luxe join`, a script, or
|
||||
whatever you build on [the RPC protocol](rpc.md#join-a-running-session). Only your user account can
|
||||
connect, and nothing listens on the network. To turn it off:
|
||||
|
||||
```json title="~/.config/luxe/config.json"
|
||||
{ "joinable": false }
|
||||
```
|
||||
|
||||
`LUXE_JOINABLE=0` turns it off for one run. A project's config can turn it off too, but can't turn
|
||||
it back on once yours has.
|
||||
|
||||
### Updates
|
||||
|
||||
Once a day, Luxe checks whether a newer release exists, and when one does the start screen says
|
||||
@@ -911,6 +934,7 @@ Every key in `config.json`, with its default.
|
||||
| `notification_method` | `auto` | `auto`, `osc9` or `osc777` |
|
||||
| `bell` | `false` | Terminal bell when a turn ends or something waits on you |
|
||||
| `herdr` | `true` | Report the agent's state to herdr; see [herdr](#herdr) |
|
||||
| `joinable` | `true` | Let another program on this machine join a running session; see [Joining a session](#joining-a-session) |
|
||||
| `statusline.visibility` | `auto` | `auto`, `shown` or `hidden`; see [Appearance](appearance.md#statusline) |
|
||||
| `statusline.style` | `slab` | `slab` or `flat` |
|
||||
| `statusline.left` | `["project", "branch", "pr", "todo", "changes"]` | Modules on the left |
|
||||
@@ -1340,6 +1364,7 @@ UTF-8 is ignored, as if the variable were unset.
|
||||
| `LUXE_SIZE_CHECK` | `1` | `0` stops telling the agent when an edit takes a file past 500 lines or a function past 20 |
|
||||
| `LUXE_DEBUG_NUDGE` | `1` | `0` stops suggesting the debugger after repeated test failures |
|
||||
| `LUXE_PLAN_NUDGE` | `1` | `0` stops reminding the agent to update its checklist |
|
||||
| `LUXE_NESTED_INSTRUCTIONS` | `1` | `0` stops reading the [`AGENTS.md` of folders](project-context.md#folders-the-agent-works-in) the agent works in |
|
||||
| `LUXE_LAZY_TOOLS` | `1` | `0` sends every tool's description with every request |
|
||||
| `LUXE_CORE_TOOLS` | | The tools sent with every request, comma-separated; the rest load when needed |
|
||||
| `LUXE_DAP_STOP_TIMEOUT_SECS` | `30` | How long the debugger waits for a program to stop |
|
||||
@@ -1356,6 +1381,8 @@ UTF-8 is ignored, as if the variable were unset.
|
||||
| `LUXE_FILE_MANAGER`, then `FILE_MANAGER` | The file manager `O` in the dock's file tree opens a folder in. Without either, the first of yazi, lf, ranger and nnn that is installed, then your desktop's own. |
|
||||
| `LUXE_HERDR` | `0` stops reporting state to [Herdr](recipes.md#run-luxe-in-herdr) |
|
||||
| `LUXE_UPDATE_CHECK` | `0` turns off the update check |
|
||||
| `LUXE_JOINABLE` | `0` stops other programs [joining](configuration.md#joining-a-session) this run's session |
|
||||
| `LUXE_RUNTIME_DIR` | Where a running Luxe keeps the socket and record that let a program join it, instead of `luxe` in `$XDG_RUNTIME_DIR` on Linux or `$TMPDIR` on macOS, or `run` in the state folder |
|
||||
|
||||
## Diagnostics
|
||||
|
||||
@@ -2090,6 +2117,7 @@ guide is reference, for when you need it.
|
||||
| Resume a session, or see what fills the context | [Sessions and context](sessions.md) |
|
||||
| See what the agent can use | [Tools](tools.md) |
|
||||
| Let the agent work through a plan on its own | [Autonomous runs](loop.md) |
|
||||
| Reach your sessions from your phone or tablet | [Luxe on your phone](remote.md) |
|
||||
| Change the theme, keys or statusline | [Configuration](configuration.md) |
|
||||
| Give the agent your project's conventions | [Project context](project-context.md) |
|
||||
| Add tools, skills or your own commands | [Extending Luxe](extending.md), [Recipes](recipes.md) |
|
||||
@@ -2421,7 +2449,8 @@ While the agent works, you can keep typing:
|
||||
| `alt+↑` | Bring waiting messages back into the composer to edit |
|
||||
| `ctrl+esc` | Stop the turn. Waiting messages come back into the composer. |
|
||||
|
||||
A waiting message sits above the composer, marked `steering` or `queued`, until it is delivered.
|
||||
A waiting message sits above the composer, marked `steering` or `queued`, until it is delivered. A
|
||||
message with an image is always queued, because steering carries text only.
|
||||
|
||||
`ctrl+v` or `shift+insert` pastes. Pasted text of three lines or more, or over 150 bytes, becomes a
|
||||
`[Pasted 40 lines]` chip, and a pasted image becomes `[Pasted Image]`. Both are sent in full. A chip, like a mention, is deleted with one `backspace`.
|
||||
@@ -2452,7 +2481,7 @@ conversation, or in its place below 80 columns.
|
||||
| **FILES** | The project as a tree, with what changed marked in it. Click a file to [preview](#preview) it. |
|
||||
| **ELSEWHERE** | Files the session changed outside the project, when there are any |
|
||||
| **MODEL** | The provider, model, thinking level and [mode](agent.md#plan-then-build). Click the model to change it, or the mode to switch it. |
|
||||
| **SESSION** | Context used, tokens and cost, with what a review and compaction cost on rows of their own. A review's row shows its full cost once its guide or drafted summary is done. |
|
||||
| **SESSION** | Context used, tokens and cost, with what a review and compaction cost on rows of their own. A review's row shows its full cost once its guide or drafted summary is done. While a phone or another client has [joined](remote.md), a **joined** row names it. |
|
||||
|
||||
`alt+d` shows or hides the dock, and `/dock left` or `/dock right` moves it. To set it for good:
|
||||
|
||||
@@ -3053,6 +3082,15 @@ session repeats that same text, which is what lets the provider reuse its cache.
|
||||
the agent in the next session: `/new`, or resuming one. If you switch Git branches mid-session, the
|
||||
agent is told with your next message.
|
||||
|
||||
### Folders the agent works in
|
||||
|
||||
An `AGENTS.md` in a folder below the repository's top, but not on the way to where you started,
|
||||
joins when the agent first opens, writes or searches a path in that folder. It's added to that
|
||||
tool result, once, and Luxe shows **Following** and the file's path. The system prompt doesn't
|
||||
change, so the cache holds. Ignored folders, such as `node_modules`, are skipped. If a rewind or a
|
||||
compaction drops the file, it joins again the next time the agent goes there.
|
||||
`LUXE_NESTED_INSTRUCTIONS=0` turns this off.
|
||||
|
||||
**Project context** lists every file a turn reads, and says whose rules each one carries:
|
||||
**global**, **project** or **folder** context. Type `context` on the start screen and open it.
|
||||
`enter` opens a file in your editor, or creates one from a template. The template's guidance is in
|
||||
@@ -3470,7 +3508,7 @@ luxe --resume <id> # a specific one
|
||||
@@@ page: recipes
|
||||
---
|
||||
title: "Recipes"
|
||||
summary: "Extensions put together: Asana, subagents, a classifier guard, Herdr, and a local model."
|
||||
summary: "Extensions put together: Asana, subagents, a classifier guard, Herdr, your phone, and a local model."
|
||||
order: 8
|
||||
group: "Extend"
|
||||
---
|
||||
@@ -3716,6 +3754,60 @@ The one token worth a second thought is `panel_bg`: Herdr draws the active tab a
|
||||
on an `accent` fill, so it has to stay dark enough to read there. Lifting it to `#201e1a` gives the
|
||||
tab bar a surface of its own if you prefer the chrome delineated.
|
||||
|
||||
## Luxe from your phone
|
||||
|
||||
Your sessions keep running on your computer, and your phone or tablet reaches them over
|
||||
[Tailscale](https://tailscale.com), a private network between your own devices. Nothing is opened to
|
||||
the internet. This is the terminal over SSH; [Luxe on your phone](remote.md) is Luxe's own way in.
|
||||
|
||||
1. Install Tailscale on the computer and on the phone, and sign in to the same account on both.
|
||||
2. On the computer, turn on SSH. On Linux and macOS, Tailscale can do it for you:
|
||||
|
||||
```bash
|
||||
sudo tailscale up --ssh
|
||||
```
|
||||
|
||||
On Windows, turn on the OpenSSH Server in Settings, under Optional features.
|
||||
3. Run Luxe in [Herdr](#run-luxe-in-herdr), so your sessions outlive the terminal you started
|
||||
them in.
|
||||
4. On the phone, connect with an SSH app such as Blink Shell or Termius, using the computer's
|
||||
Tailscale name, and run `herdr`. It attaches to the panes you left, with Luxe where it was.
|
||||
|
||||
```bash
|
||||
mosh you@desk -- herdr
|
||||
```
|
||||
|
||||
[Mosh](https://mosh.org) keeps the connection through a locked screen or a switch from Wi-Fi to
|
||||
cellular. It needs `mosh` installed on the computer too, which Linux and macOS have and Windows
|
||||
does not; plain `ssh` works everywhere.
|
||||
|
||||
On an iPad with a keyboard, this is Luxe as you know it. On a phone, Luxe fits a narrow screen,
|
||||
and Herdr marks which session is waiting on you.
|
||||
|
||||
### A notification when Luxe needs you
|
||||
|
||||
[Luxe on your phone](remote.md#notifications) has notifications built in. Without it, a
|
||||
[hook](hooks.md) can post to [ntfy](https://ntfy.sh), whose app shows it on your phone:
|
||||
|
||||
```json title="~/.config/luxe/hooks.json"
|
||||
{
|
||||
"PermissionRequested": [
|
||||
{ "command": ["curl", "-s", "-d", "Luxe is waiting for an approval", "https://ntfy.sh/your-topic"] }
|
||||
],
|
||||
"BeforeToolUse": [
|
||||
{ "matcher": "question", "command": ["curl", "-s", "-d", "Luxe has a question", "https://ntfy.sh/your-topic"] }
|
||||
],
|
||||
"AgentEnd": [
|
||||
{ "command": ["curl", "-s", "-d", "Luxe finished", "https://ntfy.sh/your-topic"] }
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
- **Anyone who knows an ntfy.sh topic can read it**, so pick a long random name, or run ntfy
|
||||
yourself on your Tailscale network. The messages above say only that something is waiting, never
|
||||
the command.
|
||||
- **`AgentEnd` fires after every turn**, at your desk too. Leave it out if that is too many.
|
||||
|
||||
## A local model in LM Studio
|
||||
|
||||
1. Load a model in LM Studio and start its server.
|
||||
@@ -3740,6 +3832,207 @@ to it rather than replacing the file:
|
||||
```
|
||||
|
||||
[Custom providers](custom-providers.md) covers samplers and request formats.
|
||||
@@@ page: remote
|
||||
---
|
||||
title: "Luxe on your phone"
|
||||
summary: "Reach the sessions running on your computer from your own phone or tablet, over Tailscale."
|
||||
order: 13
|
||||
group: "Using Luxe"
|
||||
---
|
||||
|
||||
`luxe remote` lets your phone or tablet see the sessions running on your computer, join one, and
|
||||
start new ones. It listens on your computer only. [Tailscale](https://tailscale.com), a private
|
||||
network between your own devices, carries it to them over HTTPS, so nothing is opened to the
|
||||
internet and there is no Luxe server in between.
|
||||
|
||||
On the phone it's an app in the browser, drawn like the interface in your theme. For the whole
|
||||
terminal, editor included, [Luxe from your phone](recipes.md#luxe-from-your-phone) works over SSH.
|
||||
|
||||
## Set it up
|
||||
|
||||
1. Install Tailscale on the computer and on the phone, and sign in to the same account on both.
|
||||
2. On the computer, run:
|
||||
|
||||
```bash
|
||||
luxe remote setup
|
||||
```
|
||||
|
||||
3. Scan the QR code it shows with the phone's camera, and give the device a name.
|
||||
4. On an iPhone or iPad, add it to the Home Screen: in Safari, Share, then Add to Home Screen. It
|
||||
opens full screen with the Luxe mark as its icon, and keeps the sign-in Safari had when you
|
||||
added it. An app added before pairing asks for the invitation link instead: copy it from
|
||||
`luxe remote pair` and paste it in.
|
||||
|
||||
Setup takes four steps, and asks before each one that changes something outside Luxe:
|
||||
|
||||
| Step | Does |
|
||||
| --- | --- |
|
||||
| Tailscale | Checks it is installed and signed in, and stops here if not |
|
||||
| Serve | Publishes the bridge at `https://<computer>.<tailnet>.ts.net/` with Tailscale Serve, for your tailnet only. One that already leads somewhere else is left alone. |
|
||||
| Service | Runs the bridge whenever you're signed in to the computer |
|
||||
| Pairing | Shows a QR code for one device |
|
||||
|
||||
A step that's already done says so and moves on, so running setup again is how to check it. On
|
||||
Linux, Serve may say access was denied; let your account manage Tailscale once, then run setup
|
||||
again:
|
||||
|
||||
```bash
|
||||
sudo tailscale set --operator=$USER
|
||||
```
|
||||
|
||||
The service is your own, and needs no administrator:
|
||||
|
||||
| | |
|
||||
| --- | --- |
|
||||
| Linux | A systemd user unit, `~/.config/systemd/user/luxe-remote.service` |
|
||||
| macOS | A launch agent, `~/Library/LaunchAgents/dev.luxe.remote.plist` |
|
||||
| Windows | `Luxe remote` under `HKCU\Software\Microsoft\Windows\CurrentVersion\Run` |
|
||||
|
||||
Without it, run `luxe remote` in a terminal, and your devices reach Luxe while it runs.
|
||||
|
||||
## Devices
|
||||
|
||||
```bash
|
||||
luxe remote pair # a QR code and link for one more device
|
||||
luxe remote devices # the paired ones
|
||||
luxe remote devices revoke a41c9e02 # unpair one
|
||||
```
|
||||
|
||||
An invitation pairs one device, within ten minutes. Revoking a device disconnects it within a few
|
||||
seconds; the sessions it had open carry on.
|
||||
|
||||
Only your own Tailscale login gets in. To let someone else on your tailnet in too, name them when
|
||||
you set up, and send them an invitation from `luxe remote pair`:
|
||||
|
||||
```bash
|
||||
luxe remote service remove
|
||||
luxe remote setup --allow sam@github
|
||||
```
|
||||
|
||||
## Two or more computers
|
||||
|
||||
Each computer runs its own bridge at its own address, so one phone can reach several: set each up
|
||||
as above, with Tailscale signed in to the same account, and pair the phone with each. Every one is
|
||||
its own app on the Home Screen, named for the computer as Tailscale names it (`desk` for
|
||||
`desk.tail1234.ts.net`), with the name beside the Luxe mark in its session list and after the
|
||||
project in its notifications, "luxe · desk". Luxe finds Tailscale by the `tailscale` command, so
|
||||
on a Mac check that `tailscale version` works in Terminal before running setup.
|
||||
|
||||
## What your device sees
|
||||
|
||||
Every Luxe running on the computer as you: the interface, `luxe --rpc`, and any started from your
|
||||
device. A device that joins a session is a guest, as in [joining a
|
||||
session](rpc.md#join-a-running-session): it sees the conversation so far and every live event, and
|
||||
can answer prompts and send messages. It can't switch or end the session, or publish to GitHub.
|
||||
|
||||
In a session, the app can:
|
||||
|
||||
- Name the session's folder and branch in the header, starred when the tree has changes.
|
||||
- Show the conversation as the interface draws it: markdown, highlighted code, tool cards. A long
|
||||
one opens on its latest 300 items, and **Show earlier** at the top brings 300 more at a time.
|
||||
- Send a message, with photos from the camera or library, and steer or stop a running turn. While
|
||||
one runs, a line over the message box says what sending will do: steer it at the next step, or
|
||||
wait for the turn to end when a photo is attached.
|
||||
- Run `/compact`, `/cost`, `/dock`, `/mode`, `/model` and `/thinking`, offered as you type a `/`. The
|
||||
interface's other commands work its own screen, so a message that starts with one goes to the
|
||||
agent as written.
|
||||
- Mention a project file: type `@` and pick from the project's folders, a level at a time. A picked
|
||||
file goes in as its path, which is what the interface sends the model for one.
|
||||
- Answer permission prompts and questions. As in the interface, every answer but Reject waits a
|
||||
moment after a prompt appears, so a stray tap can't grant anything.
|
||||
- Switch the mode with a tap on the status line, which also shows the context, thinking level and
|
||||
model. Tap those for the session's details: switch the mode, the thinking level or the model
|
||||
(from the same list as `/model`, with the levels each model honors), and see the cost.
|
||||
- Show [the dock](interface.md#the-dock), with the plan and the changed files. On an iPad or an
|
||||
open iPhone Duo it's a rail beside the conversation: the header's dock button or `/dock` shows
|
||||
and hides it, `/dock left` or `/dock right` moves it, and it starts from your `dock.mode` and
|
||||
`dock.position` [settings](configuration.md), as the interface does. On a phone the header has
|
||||
one menu button, and the dock is the first tab of the panel it opens; `/dock` opens it there
|
||||
too. A long plan scrolls in a box of its own that keeps the work in hand in view, until you
|
||||
scroll it yourself. Tap a file for its diff.
|
||||
- Browse the project's files from the header's menu button, in the interface's FILES tree, and
|
||||
read a file highlighted as the interface shows it. **Mention** adds its path to your message.
|
||||
On an iPad or an open iPhone Duo the tree is a rail and a file opens beside it; on a screen wide
|
||||
enough the conversation stays in view too.
|
||||
- Ask about particular lines of a file, a diff or a review chapter, as you would in the interface's
|
||||
File view: tap a line's number to pick it and another to stretch the pick, or select text the
|
||||
phone's usual way, down to part of a line, and tap **Ask** for the lines it covers. Your message
|
||||
gets the path and lines the interface would send. Tap anywhere else to let a pick go, and copy a
|
||||
selection with the phone's own menu. In a diff, a removed line can't be picked, since the file
|
||||
no longer has it.
|
||||
- Read the project's pull requests and issues in the same panel's GitHub tab, by the sections
|
||||
[`/github`](github.md) shows. Tap one for its overview (what stands between it and merged, or
|
||||
who's on an issue) and its conversation. **Mention** adds it to your message. It only reads: a
|
||||
joined session runs with GitHub writes off, so merging, replying and resolving stay in the
|
||||
interface.
|
||||
- Walk the open [Guided Review](guided-review.md) in the Review tab: the guide's summary and
|
||||
chapters, each one opened beside it with the guide's notes over its diffs. **Reviewed** marks a
|
||||
chapter read, the same progress `/review` keeps, and **Next** opens the one after. The phone
|
||||
joins the review the interface has open rather than starting its own, since a session holds one;
|
||||
with none open, **Review changes** reviews the working changes.
|
||||
- Comment on a review chapter: pick a line, or a run of lines, and tap **Comment**, or tap
|
||||
**Comment on the chapter** with nothing picked. Make it a question if you want an answer rather
|
||||
than a fix, then **Save** it to send later or **Send** it now. Each comment shows under what it's
|
||||
about with the agent's replies, and can be replied to, resolved, or, unsent, sent or deleted.
|
||||
The guide counts what's unsent and sends it all. Comments made in the interface show up too.
|
||||
Editing a comment, re-anchoring one the code moved away from, and commenting on a removed line
|
||||
are the interface's.
|
||||
|
||||
The interface says which device joined, "iPhone joined this session", and which one answered a
|
||||
prompt, "Approved from iPhone". A session with [`joinable`](configuration.md#joining-a-session) set
|
||||
to `false` is out of reach, from your device too.
|
||||
|
||||
## Starting a session
|
||||
|
||||
Tap **New** on the session list to start Luxe in a folder: one where Luxe is running now, one with
|
||||
a [herdr](recipes.md#run-luxe-in-herdr) pane, one where the bridge has seen Luxe run, or any path
|
||||
you type. What you type narrows the folders it offers, by name and then by path. They're only the
|
||||
ones above: it never looks through your home folder.
|
||||
|
||||
**herdr is optional, and recommended.** You don't need it to reach your sessions; it decides what
|
||||
happens to the ones you start away from your desk:
|
||||
|
||||
- **With herdr running**, the bridge opens a herdr pane in that folder, in the workspace already
|
||||
there or a new one named after it. The session survives a restart of the bridge, and waits for
|
||||
you at your desk like any other pane.
|
||||
- **Without it**, the bridge runs `luxe --rpc` itself, and the session lasts as long as the bridge
|
||||
does. At your desk, `luxe --resume` on it is refused while the bridge has it, and says so.
|
||||
|
||||
The bridge uses herdr only when herdr's server is already running, and never starts it.
|
||||
|
||||
## Notifications
|
||||
|
||||
A device can hear when a session needs it: a prompt waiting on an answer, or a long turn finished.
|
||||
Turn them on under **Notifications** in the app's session list. On an iPhone or iPad the app has to
|
||||
be on your Home Screen first (in Safari, Share, then Add to Home Screen), and paired from there, since
|
||||
the Home Screen app keeps its own sign-in.
|
||||
|
||||
A notification says what's waiting and where, "Approve bash" over `my-app`, never the command, and
|
||||
tapping it opens the session. Answering takes the app. So that one doesn't buzz you while you're
|
||||
at your desk:
|
||||
|
||||
- A prompt is held for ten seconds, and one answered meanwhile is never sent.
|
||||
- A finished turn is news only after a turn of thirty seconds or more.
|
||||
- A device with that session open on screen isn't told about it.
|
||||
|
||||
While any device has notifications on, the bridge joins each running session to watch. It joins
|
||||
watch-only, so the interface doesn't announce it and it can't act in the session.
|
||||
[Security](security.md#luxe-remote) covers what a notification carries and where it goes.
|
||||
|
||||
## Turn it off
|
||||
|
||||
```bash
|
||||
luxe remote service remove
|
||||
tailscale serve --https=443 off
|
||||
```
|
||||
|
||||
The first stops the bridge and stops it starting at sign-in; the second takes the address off
|
||||
your tailnet. Your paired devices stay paired until you revoke them, in case you set it up again.
|
||||
|
||||
## How it's kept to you
|
||||
|
||||
Every request needs your Tailscale login, which only Tailscale Serve can vouch for, and every
|
||||
session needs a paired device as well. [Security](security.md#luxe-remote) has the details.
|
||||
@@@ page: rpc
|
||||
---
|
||||
title: "The RPC protocol"
|
||||
@@ -3758,8 +4051,8 @@ luxe --rpc --github-writes # also lets your client publish to GitHub
|
||||
```
|
||||
|
||||
A client is trusted like the interface it replaces: it answers permission prompts, and it cannot
|
||||
loosen any permission rule. There is no network listener; your client is the process that started
|
||||
Luxe.
|
||||
loosen any permission rule. Nothing listens on the network. Your client either started Luxe, or
|
||||
[joined a session](#join-a-running-session) open in the interface on the same machine.
|
||||
|
||||
## A session
|
||||
|
||||
@@ -3767,6 +4060,7 @@ Luxe.
|
||||
→ {"kind":"ready","content":{"protocol_version":2,"luxe_version":"0.1.0","session_id":"…","cwd":"/work/app","github_writes":false}}
|
||||
→ {"kind":"state_changed","content":{"model":{…},"streaming":false,"mode":"build","thinking":"off"}}
|
||||
← {"kind":"submit_user_message","content":{"text":"what does this repo do?"}}
|
||||
→ {"kind":"turn_started"}
|
||||
→ {"kind":"tool_started","content":{"call_id":"call_1","name":"read"}}
|
||||
→ {"kind":"token_delta","content":{"block":0,"delta":"It is "}}
|
||||
→ {"kind":"turn_usage","content":{"prompt_tokens":5018,"completion_tokens":26,"cost_usd":0.0258,…}}
|
||||
@@ -3816,6 +4110,43 @@ for line in luxe.stdout:
|
||||
Three rules make a working client: read `ready` first, answer both kinds of prompt, and ignore
|
||||
what you do not recognize.
|
||||
|
||||
## Join a running session
|
||||
|
||||
A session open in the interface can be joined while you keep using it, and so can one a `luxe --rpc`
|
||||
client started. `luxe join` connects to it and speaks this protocol on stdin and stdout, so the
|
||||
client above works with `["luxe", "join"]` in place of `["luxe", "--rpc"]`.
|
||||
|
||||
```bash
|
||||
luxe join --list # sessions running on this machine
|
||||
luxe join # the one running in this folder
|
||||
luxe join 5898d57b # one by the start of its id, or by its Luxe's process id
|
||||
```
|
||||
|
||||
After `ready` comes the session so far: `session_loaded` with the transcript, the latest
|
||||
`state_changed`, `session_usage` and `workspace_updated`, a `tool_activity_updated` for each call
|
||||
of a turn in flight, and any `permission_requested` or `question_asked` still waiting. Then the
|
||||
live events, the same ones the interface gets.
|
||||
|
||||
A joined client is a guest in someone else's session:
|
||||
|
||||
- **Either side can answer a prompt.** The first answer counts, and `prompt_closed` tells everyone
|
||||
to put the prompt away. The interface says which client answered.
|
||||
- **It can't switch or end the session.** `new_session` and `load_session` are refused with
|
||||
`action_rejected`. `close`, or the end of your stdin, disconnects you and leaves Luxe running.
|
||||
- **GitHub writes are off**, and `ready` says `"github_writes": false`.
|
||||
- **Keep reading.** A client that falls far behind gets a `notice` and is disconnected. Join
|
||||
again for a fresh copy of the session.
|
||||
|
||||
The interface lists who's joined in its dock, and says in the conversation when a client joins or
|
||||
leaves, by the name it gives in `introduce` if it sends one within a second of connecting, or by
|
||||
its process id. A device the bridge introduces as `yours`, paired by you, is listed but not
|
||||
announced: you know your own phone joined, and a phone reconnects often.
|
||||
|
||||
A running Luxe lists its session in a file in its [runtime folder](environment.md#integrations),
|
||||
`<pid>.json`, with the `address` to connect to: a Unix socket, or a named pipe on Windows. Connect
|
||||
there yourself to skip `luxe join`.
|
||||
To stop sessions being joined, set [`joinable`](configuration.md#joining-a-session) to `false`.
|
||||
|
||||
## Actions
|
||||
|
||||
| `kind` | `content` | Does |
|
||||
@@ -3831,6 +4162,7 @@ what you do not recognize.
|
||||
| `answer_question` | `{"id": "ask:…", "reply": {"answered": ["postgres"]}}` | Answer a question, or `{"reply": "cancelled"}` |
|
||||
| `set_permission_auto_approve` | `true` | Approve every prompt for the rest of the session. Deny rules still apply. |
|
||||
| `select_model` | `{"provider": "anthropic", "model": "claude-sonnet-5"}` | Switch models |
|
||||
| `list_models` | `{"request_id": "…"}` | The models you can switch to, configured ones first, each with the thinking levels it honors; answered by `models_listed`, to you alone |
|
||||
| `set_thinking_level` | `"high"` | `off`, `minimal`, `low`, `medium`, `high`, `xhigh` or `ultra` |
|
||||
| `set_mode` | `"plan"` | `build` or `plan`; see [Plan, then build](agent.md#plan-then-build). A name that is not a mode is answered by a `notice`. |
|
||||
| `request_review` | `{"focus": "error handling"}`, or nothing | A [second opinion](agent.md#a-second-opinion) on the uncommitted changes; answered by `review_started`, then `review_finished` |
|
||||
@@ -3844,6 +4176,7 @@ what you do not recognize.
|
||||
| `load_file_diff` | `{"request_id": "…", "path": "src/main.rs"}` | One file's change, outside any review; answered by `file_diff_loaded` |
|
||||
| `list_directory` | `{"request_id": "…", "path": "src"}` | One project folder's entries, without what `.gitignore` ignores unless `"include_ignored": true`, which marks each such entry `"ignored": true`; answered by `directory_listed` |
|
||||
| `read_project_file` | `{"request_id": "…", "path": "src/main.rs"}` | One project file's text, up to 2 MB; answered by `project_file_read` |
|
||||
| `introduce` | `{"name": "iPhone"}` | A joined client's name, which the interface uses when it says who joined or answered. Add `"watch_only": true` as your first line to read without being announced; Luxe then drops everything else you send. `"yours": true` says the person at the terminal paired it, so it's listed without a line in the conversation. Refused from the client that started the session. |
|
||||
| `close` | | Shut down, stopping a turn in flight the way `cancel` does. The stream ends after it. |
|
||||
|
||||
### Guided Review
|
||||
@@ -3853,6 +4186,9 @@ carries the whole review (its `id`, `revision_id`, files, hunks and chapters), a
|
||||
`review_session_restored`, which carries its comments. Everything below names the review by its
|
||||
`id`. An action for a review that is no longer the open one is refused.
|
||||
|
||||
A session has one open review, and every load makes a new one, so a second client that loads its
|
||||
own replaces the review the first is reading. To join it instead, send `show_open_review`.
|
||||
|
||||
```text
|
||||
← {"kind":"load_guided_review","content":{"request_id":"r1","target":{"kind":"working_tree"}}}
|
||||
→ {"kind":"guided_review_loaded","content":{"request_id":"r1","review":{"id":"review:…","revision_id":"…",…}}}
|
||||
@@ -3891,6 +4227,7 @@ the comment stays `open`.
|
||||
| `edit_review` | `{"request_id", "review_id", "edit", …}` | Change the comments, as above |
|
||||
| `record_review_progress` | `{"review_id", "walked_chapter_ids": [...]}` | Which chapters were read. Saved when it changed, never answered. |
|
||||
| `record_review_seen` | `{"review_id"}` | Remember what the walked chapters covered, so the next review of the same target leads with what is new |
|
||||
| `show_open_review` | `{"request_id"}` | Ask for the review open now, whoever opened it, answered by `open_review_shown` |
|
||||
| `refresh_guided_review` | `{"request_id", "review_id", "revision_id"}` | Read the change again, keeping the comments, if the review is still at `revision_id`. Answered like a load. |
|
||||
| `commit_review_chapters` | `{"request_id", "review_id", "revision_id", "chapter_ids", "squash"}` | A commit per chapter, or one with `squash`. All chapters when `chapter_ids` is left out. Never pushes. Answered by `review_committed`. |
|
||||
| `prepare_pull_request` | `{"request_id", "review_id"}` | Draft the pull request this branch's review would open; answered by `pull_request_prepared` |
|
||||
@@ -3963,6 +4300,7 @@ There are around sixty. These are the ones a front end is built on:
|
||||
| `message_committed` | A complete message was added to the transcript |
|
||||
| `permission_requested` | **Answer it** with `respond_permission`, or the turn waits. If `always_patterns` is empty, offer only `once` and `reject`. |
|
||||
| `question_asked` | **Answer it** with `answer_question` |
|
||||
| `turn_started` | A turn began, from any client. It comes before the turn's message is saved, and `turn_ended` always follows. A client joining mid-turn gets it as part of catching up. |
|
||||
| `turn_ended` | The turn finished; `reason` says why |
|
||||
| `provider_retrying` | The provider failed before answering, and Luxe is trying again |
|
||||
| `state_changed` | Model, thinking level, mode, and whether a turn is running |
|
||||
@@ -3972,8 +4310,10 @@ There are around sixty. These are the ones a front end is built on:
|
||||
| `context_usage`, `context_composition` | How full the context window is, and with what |
|
||||
| `compaction_started`, `compaction_done`, `compaction_stopped` | A summary of the conversation is being written, on request or mid-turn. It can take a minute. It always ends in `compaction_done` (with `tokens_after`) or `compaction_stopped` (failed or cancelled). |
|
||||
| `workspace_updated` | The project, Git changes, todos and tools: what the dock shows |
|
||||
| `prompt_closed` | A prompt stopped waiting, answered here or by another client, or cancelled. Take it down by `id`. |
|
||||
| `session_loaded`, `sessions_refreshed` | A transcript was restored; the session list changed |
|
||||
| `notice`, `error` | Something to show. `error` ends the turn. |
|
||||
| `guests_changed` | The names of the clients joined now, sent to the interface as one joins or leaves, for its dock. |
|
||||
| `action_rejected` | Your frame was refused and nothing happened |
|
||||
|
||||
Guided Review, as the interface sees it:
|
||||
@@ -3983,6 +4323,7 @@ Guided Review, as the interface sees it:
|
||||
| `guided_review_loading`, `guided_review_loaded`, `guided_review_failed` | A review you asked for, by `request_id`. `loaded` carries the whole review: files, hunks and chapters. |
|
||||
| `guided_review_guide_updated` | Better chapters and a summary replaced the first ones, for the same pinned revision |
|
||||
| `review_session_restored` | The comments and progress that go with the review just loaded. It always follows `guided_review_loaded`, after a restart too. |
|
||||
| `open_review_shown` | The answer to `show_open_review`, under its `request_id`: the open `review` and its `session`, or neither when no review is open. `reading` says what each chapter asks of the reader: `needs_review` items, and the `reason` when one thing accounts for all of them. |
|
||||
| `review_session_saved`, `review_session_save_failed` | A change to them was stored, or refused. Luxe reports its own changes this way too: a turn starting, the agent's report, a review posted. |
|
||||
| `review_feedback_turn_dispatched`, `review_feedback_turn_completed`, `review_feedback_turn_failed` | Feedback you sent reaching the agent, and how its turn went |
|
||||
| `review_questions_answered`, `remediation_reported` | The agent's answer to a question, and its report on a fix |
|
||||
@@ -3990,6 +4331,7 @@ Guided Review, as the interface sees it:
|
||||
| `last_turn_available` | The agent's last turn can be reviewed on its own |
|
||||
| `file_diff_loaded`, `file_diff_failed` | One file's change, for `load_file_diff` |
|
||||
| `directory_listed`, `directory_list_failed` | One folder's entries, for `list_directory` |
|
||||
| `models_listed` | The models for `list_models`: each has `provider` and `model` for `select_model`, a `name` to show, and its `thinking_levels` |
|
||||
| `project_file_read`, `project_file_read_failed` | One file's text, for `read_project_file` |
|
||||
|
||||
GitHub:
|
||||
@@ -4021,7 +4363,9 @@ list is closed.
|
||||
|
||||
## Security
|
||||
|
||||
- Luxe listens on nothing; your client is the process that started it.
|
||||
- Nothing listens on the network. A [joined](#join-a-running-session) session takes connections
|
||||
on a local socket or pipe that only your user account can open, and turns away any program the
|
||||
agent itself started, so the agent can't answer its own prompts.
|
||||
- Permission rules are enforced inside Luxe. A client cannot weaken a `deny`: reading `.env` is
|
||||
refused here as everywhere.
|
||||
- Prompts go to your client to answer. To approve everything, send `set_permission_auto_approve` or
|
||||
@@ -4190,6 +4534,69 @@ or `"agent": { "on_ask": "deny" }` in `config.json`. A project can set `deny` bu
|
||||
closing an issue. Auto-approve and unattended runs refuse them. To let an unattended run do one,
|
||||
allow it in your own rules, such as `"bash": { "gh pr merge *": "allow" }`.
|
||||
|
||||
## Joining a session
|
||||
|
||||
A session open in the interface can be [joined](rpc.md#join-a-running-session) by another program,
|
||||
which then sees the conversation and can answer its prompts. That makes who may join a security
|
||||
question:
|
||||
|
||||
- **Only your user account.** The socket sits in a folder only you can open, only you can use it,
|
||||
and Luxe checks the account of whatever connects. On Windows the named pipe admits your account
|
||||
alone, and refuses remote clients.
|
||||
Nothing listens on the network.
|
||||
- **Not the agent.** A program the agent started, at any depth, is turned away, so it can't
|
||||
approve its own prompts. Luxe checks the joining process's ancestry. It can't see past a program
|
||||
that detaches from its parent, such as one started with `setsid` and left running after its
|
||||
parent exits, so treat this as a tripwire, not a wall. A command that would do that should be
|
||||
something your rules ask about.
|
||||
- **A guest can't do everything.** A joined client can't switch or end the session, and can't
|
||||
publish to GitHub.
|
||||
|
||||
Anything running as you can already read your sessions on disk and your credentials, so joining
|
||||
adds a way to steer, not a way to read. To turn it off, set
|
||||
[`joinable`](configuration.md#joining-a-session) to `false`.
|
||||
|
||||
## Luxe remote
|
||||
|
||||
[`luxe remote`](remote.md) is the one part of Luxe that answers network requests, and it is off
|
||||
until you run it. It puts your sessions in reach of your phone, so it takes two things to get in:
|
||||
|
||||
- **Your Tailscale login.** The bridge listens on this machine only (`127.0.0.1`) and refuses to
|
||||
listen anywhere else. Tailscale Serve carries it to your tailnet over HTTPS and adds the login of
|
||||
whoever is asking, replacing any the request brought. A request without one didn't come through
|
||||
Serve and is refused, and so is a login other than yours or one you `--allow`. Luxe never uses
|
||||
Tailscale Funnel, which would publish it to the internet.
|
||||
- **A paired device.** Any program on this machine can reach a local port and claim a login, so
|
||||
every session also needs the credential a device gets when it pairs. An invitation works once,
|
||||
within ten minutes. Luxe keeps only a hash of each invitation and credential, in
|
||||
`~/.config/luxe/remote.json`. The device keeps its credential in a cookie scripts can't read,
|
||||
sent only over HTTPS and never with a request from another site; a request a page on another
|
||||
site makes is refused too.
|
||||
|
||||
Revoking a device disconnects it within a few seconds. A device joins a session as a guest, with
|
||||
the same limits as any [joined client](#joining-a-session), and the interface names it when it joins
|
||||
and when it answers a prompt. The name is the device's own label: whether a client may answer
|
||||
prompts at all is still decided by the process checks above.
|
||||
|
||||
Notifications are off until you turn them on in the app, one device at a time, and they never
|
||||
answer anything: a notification says what is waiting and where, such as "Approve bash" over the
|
||||
folder's name, never the command, and answering takes the app. While any device has them on, the
|
||||
bridge joins each running session to watch for prompts. It joins watch-only: the terminal doesn't
|
||||
announce it, and Luxe drops anything it sends, so a client nobody sees can't act. Each
|
||||
notification is sealed so only that device can read it (RFC 8291) and posted to the push service
|
||||
the device's browser picked; the push service sees when, not what. The bridge posts only to
|
||||
Apple's, Google's, Mozilla's and Microsoft's push services, so a paired device can't aim it at
|
||||
anything else on your network. It signs with a key of its own, kept in `remote.json`, and revoking
|
||||
a device ends its notifications.
|
||||
|
||||
The bridge starts Luxe only in a folder a device asks for, as you, in a herdr pane when herdr is
|
||||
already running and as its own `luxe --rpc` otherwise. A packager can build Luxe without the
|
||||
`remote` feature, which leaves out the bridge entirely.
|
||||
|
||||
A session is open in one Luxe at a time. A second `luxe --resume` of a session the interface
|
||||
already has, or a resume from the session list, is refused and says which process has it. The lock
|
||||
is the operating system's, so a crash never leaves a session stuck.
|
||||
|
||||
## Secrets
|
||||
|
||||
- Credentials are stored in `~/.config/luxe/auth.json`, readable only by you. Luxe warns if
|
||||
@@ -4303,6 +4710,9 @@ There is no telemetry. Luxe connects only to:
|
||||
5. GitHub, through your own `gh`, when you, the agent or an [RPC client](rpc.md#github-writes) work
|
||||
with pull requests and issues. A client can write there only if you start it with
|
||||
`--github-writes`.
|
||||
6. Your device's push service (Apple's, for an iPhone), when you turn on notifications in the
|
||||
[`luxe remote`](remote.md#notifications) app, sealed so only that device can read them
|
||||
(turning them off in the app stops it)
|
||||
|
||||
None of them carries anything about you or your machine. The update check sends no version,
|
||||
platform or identifier; it downloads a public file and compares versions locally.
|
||||
@@ -4310,15 +4720,23 @@ platform or identifier; it downloads a public file and compares versions locally
|
||||
One local report stays on your machine: inside a [herdr](configuration.md#herdr) pane, Luxe runs
|
||||
herdr's own command to tell it whether the agent is working, idle or waiting on you, and the id of
|
||||
the session for herdr to resume. Nothing is sent outside herdr, and `"herdr": false` stops it.
|
||||
And a running session takes connections on a local socket only you can open, so another program can
|
||||
[join it](#joining-a-session); `"joinable": false` stops that.
|
||||
|
||||
Apart from notifications, `luxe remote` sends nothing of its own. It answers your paired devices
|
||||
through Tailscale Serve and runs your own `tailscale` and `herdr` commands; see
|
||||
[Luxe remote](#luxe-remote).
|
||||
|
||||
What you post to GitHub is yours alone: reviews, replies and pull requests are posted from a
|
||||
preview, with nothing added, and no agent tool can post them.
|
||||
|
||||
## Supply chain
|
||||
|
||||
Luxe is written in Rust with `unsafe` code denied throughout, apart from two reviewed blocks in what
|
||||
ships: one starts each command in a session of its own, so nothing the agent runs can reach your
|
||||
terminal, and one on Windows keeps the programs Luxe starts off the stdin `--rpc` reads. CI checks
|
||||
Luxe is written in Rust with `unsafe` code denied throughout, apart from a few small, reviewed
|
||||
modules that call the operating system directly. One starts each command in a session of its own,
|
||||
so nothing the agent runs can reach your terminal. On Windows, one keeps the programs Luxe starts
|
||||
off the stdin `--rpc` reads, one ends a command's whole process tree on a timeout, and one locks
|
||||
the [session pipe](#joining-a-session) to your account. CI checks
|
||||
every dependency's security advisories and license with `cargo-deny`, builds with `--locked`, and
|
||||
pins its GitHub Actions to exact commits.
|
||||
@@@ page: sessions
|
||||
|
||||
@@ -153,6 +153,28 @@ pub(crate) fn build_menu(settings: &AgentSettings) -> ModelMenu {
|
||||
ModelMenu { catalog: entries, active }
|
||||
}
|
||||
|
||||
/// The models a client of the protocol can switch to: the `/model` menu in its order, leaving out
|
||||
/// rows only guessed for an endpoint that hasn't said what it serves.
|
||||
pub(crate) fn choices(settings: &AgentSettings) -> Vec<luxe_core::event::ModelChoice> {
|
||||
offered(&build_menu(settings).catalog)
|
||||
}
|
||||
|
||||
fn offered(menu: &[ModelEntry]) -> Vec<luxe_core::event::ModelChoice> {
|
||||
menu.iter().filter(|entry| !entry.unverified).map(|entry| choice(&entry.info)).collect()
|
||||
}
|
||||
|
||||
/// `info` as a client offers it, with the thinking levels it honors as they are, not clamped.
|
||||
fn choice(info: &ModelInfo) -> luxe_core::event::ModelChoice {
|
||||
let levels = luxe_core::provider::ThinkingLevel::ALL.iter().copied();
|
||||
let honored = levels.filter(|level| info.effective_thinking_level(*level) == *level);
|
||||
luxe_core::event::ModelChoice {
|
||||
provider: info.r#ref.provider.clone(),
|
||||
model: info.r#ref.id.clone(),
|
||||
name: info.display().to_string(),
|
||||
thinking_levels: honored.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a provider needs no credential at all — a server on this machine.
|
||||
///
|
||||
/// Naming one on the command line is the whole statement of intent: there is no key to check, so
|
||||
|
||||
@@ -223,3 +223,44 @@ fn an_unconfigured_machine_has_no_models_to_choose_between() {
|
||||
);
|
||||
drop(dir);
|
||||
}
|
||||
|
||||
/// A choice names the model as `select_model` takes it, and offers the levels it honors as they
|
||||
/// are: only `off` for a model that doesn't reason, and nothing it would quietly clamp.
|
||||
///
|
||||
/// The break this catches: offering every level for every model, so a phone sets `ultra` and the
|
||||
/// engine silently runs `high`; or sending the display name where `select_model` wants the id.
|
||||
#[test]
|
||||
fn a_choice_offers_only_the_levels_its_model_honors() {
|
||||
use luxe_core::provider::ThinkingLevel::{High, Low, Medium, Minimal, Off};
|
||||
let mut info = ModelInfo {
|
||||
r#ref: luxe_core::provider::ModelRef::new("anthropic", "claude-sonnet-5"),
|
||||
display_name: Some("Claude Sonnet 5".into()),
|
||||
reasoning: false,
|
||||
..Default::default()
|
||||
};
|
||||
let plain = choice(&info);
|
||||
assert_eq!((plain.provider.as_str(), plain.model.as_str()), ("anthropic", "claude-sonnet-5"));
|
||||
assert_eq!(plain.name, "Claude Sonnet 5");
|
||||
assert_eq!(plain.thinking_levels, [Off]);
|
||||
info.reasoning = true;
|
||||
assert_eq!(choice(&info).thinking_levels, [Off, Minimal, Low, Medium, High]);
|
||||
}
|
||||
|
||||
/// A row only guessed for an endpoint that hasn't said what it serves isn't offered.
|
||||
///
|
||||
/// The break this catches: a phone offering an Azure resource deployments it doesn't have, which
|
||||
/// fail on the first turn.
|
||||
#[test]
|
||||
fn a_guessed_row_isnt_offered() {
|
||||
let entry = |id: &str, unverified: bool| ModelEntry {
|
||||
info: ModelInfo {
|
||||
r#ref: luxe_core::provider::ModelRef::new("azure", id),
|
||||
..Default::default()
|
||||
},
|
||||
label: id.into(),
|
||||
configured: false,
|
||||
unverified,
|
||||
};
|
||||
let offered = offered(&[entry("served", false), entry("guessed", true)]);
|
||||
assert_eq!(offered.iter().map(|choice| choice.model.as_str()).collect::<Vec<_>>(), ["served"]);
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ fn run_rpc_with(flags: &[&str], input: &str) -> (String, String) {
|
||||
.current_dir(project.path())
|
||||
.env("HOME", home.path())
|
||||
.env("XDG_CONFIG_HOME", home.path().join(".config"))
|
||||
// it shares its session, and the developer's own sessions are listed in the real one
|
||||
.env("LUXE_RUNTIME_DIR", home.path().join("runtime"))
|
||||
.env_remove("LUXE_CONFIG_DIR")
|
||||
.env_remove("LUXE_STATE_DIR")
|
||||
// Offline and deterministic: a catalog refresh is best-effort anyway, and a test that
|
||||
@@ -149,7 +151,7 @@ struct Live {
|
||||
seen: std::cell::RefCell<Vec<String>>,
|
||||
/// Where luxe writes its log, which a timeout prints
|
||||
project: std::path::PathBuf,
|
||||
_home: tempfile::TempDir,
|
||||
home: tempfile::TempDir,
|
||||
}
|
||||
|
||||
impl Live {
|
||||
@@ -161,6 +163,7 @@ impl Live {
|
||||
.current_dir(project)
|
||||
.env("HOME", home.path())
|
||||
.env("XDG_CONFIG_HOME", home.path().join(".config"))
|
||||
.env("LUXE_RUNTIME_DIR", home.path().join("runtime"))
|
||||
.env("LUXE_MODELS_DEV_REFRESH", "0")
|
||||
.env("RUST_LOG", "debug")
|
||||
// not a terminal multiplexer's pane, whatever the one running the tests is
|
||||
@@ -190,10 +193,17 @@ impl Live {
|
||||
frames,
|
||||
seen: Default::default(),
|
||||
project: project.to_path_buf(),
|
||||
_home: home,
|
||||
home,
|
||||
}
|
||||
}
|
||||
|
||||
/// What this luxe published about its session for others to join, if anything.
|
||||
fn published(&self) -> Option<serde_json::Value> {
|
||||
let record = self.home.path().join("runtime").join(format!("{}.json", self.child.id()));
|
||||
let text = std::fs::read_to_string(record).ok()?;
|
||||
Some(serde_json::from_str(&text).expect("the record is JSON"))
|
||||
}
|
||||
|
||||
fn send(&mut self, frame: serde_json::Value) {
|
||||
writeln!(self.stdin, "{frame}").expect("write a frame");
|
||||
self.stdin.flush().expect("flush");
|
||||
@@ -344,6 +354,21 @@ fn rpc_drives_a_review_from_comment_to_commit() {
|
||||
assert_eq!(String::from_utf8_lossy(&log.stdout).lines().count(), 2, "the review's commit");
|
||||
}
|
||||
|
||||
/// A session started over `--rpc` is shared like the terminal's, so the bridge can start one for a
|
||||
/// phone and the phone can join it.
|
||||
///
|
||||
/// The break this catches: `run_rpc` not offering its session, which leaves a session started from
|
||||
/// the phone running where nothing can reach it.
|
||||
#[test]
|
||||
fn rpc_shares_its_session() {
|
||||
let project = tempfile::tempdir().expect("temp project");
|
||||
let luxe = Live::start(project.path(), &[]);
|
||||
let ready = luxe.wait("ready", |_| true);
|
||||
let record = luxe.published().expect("a record in the runtime folder");
|
||||
assert_eq!(record["session_id"], ready["session_id"]);
|
||||
luxe.close();
|
||||
}
|
||||
|
||||
/// A read-only session reviews and comments, but writes no history.
|
||||
#[test]
|
||||
fn rpc_read_only_refuses_to_commit_a_review() {
|
||||
|
||||
@@ -69,13 +69,19 @@ rustix = { version = "1", features = ["process", "stdio", "system"] }
|
||||
# Pointing stdin at the null device on Windows, and keeping the pipe Luxe took out of every child:
|
||||
# no safe API does either, and without it every child of `luxe --rpc` stalls behind the protocol
|
||||
# read. See `program::take_stdin`. Job objects end a command's whole tree on a timeout, where
|
||||
# Windows has no process group to signal; see `program::TreeGuard`.
|
||||
# Windows has no process group to signal; see `program::TreeGuard`. A session's named pipe needs a
|
||||
# descriptor granting this user alone, the connected client's process and every process's parent;
|
||||
# see `local_endpoint::windows`.
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.61", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_Security_Authorization",
|
||||
"Win32_System_Console",
|
||||
"Win32_System_Diagnostics_ToolHelp",
|
||||
"Win32_System_JobObjects",
|
||||
"Win32_System_Pipes",
|
||||
"Win32_System_Threading",
|
||||
] }
|
||||
|
||||
[dev-dependencies]
|
||||
|
||||
@@ -323,10 +323,12 @@ pub enum ToolActivityState {
|
||||
/// distinction from `Running` is whose work it is: the model's rather than the tool's, which
|
||||
/// is why the card's clock does not start until dispatch.
|
||||
Composing,
|
||||
/// The call has been written in full but has not started, because something else is running.
|
||||
/// The call hasn't started because something else comes first: it's written in full and
|
||||
/// waiting its turn, or still being written while an earlier call runs.
|
||||
///
|
||||
/// Tools are dispatched strictly one at a time, so **at most one call is ever `Running`** —
|
||||
/// and a model writes the calls of one message in order, so at most one is ever `Composing`.
|
||||
/// and a model writes the calls of one message in order, so at most one is ever `Composing`,
|
||||
/// and never beside a `Running` one, since a call written while another runs waits behind it.
|
||||
/// Between them that is the invariant a transcript has to show: exactly one card is working.
|
||||
/// Drawing every un-finished call as running broke it in the obvious direction (two `bash`
|
||||
/// cards spinning side by side, which reads as two shells at once); drawing them all as
|
||||
|
||||
@@ -37,16 +37,24 @@ pub(crate) const RESUME_BRIEFING: &str = "## Resuming\n";
|
||||
/// Those are only ever added after what the reader sent, so the first block is theirs whatever it
|
||||
/// starts with: a pasted hook log is still a message.
|
||||
pub fn readers_words(content: &[crate::message::ContentBlock]) -> Vec<(usize, String)> {
|
||||
readers_texts(content.iter().map(|block| match block {
|
||||
crate::message::ContentBlock::Text(text) => Some(text.as_str()),
|
||||
_ => None,
|
||||
}))
|
||||
}
|
||||
|
||||
/// [`readers_words`] over each block's text, `None` for a block without any: for a front end that
|
||||
/// has a message only as it arrived on the wire, an image's bytes left behind.
|
||||
pub fn readers_texts<'a>(
|
||||
blocks: impl IntoIterator<Item = Option<&'a str>>,
|
||||
) -> Vec<(usize, String)> {
|
||||
let for_the_model = |text: &str| {
|
||||
let ours = [HOOK_CONTEXT, BRANCH_NOTE, RESUME_BRIEFING];
|
||||
crate::mode::note(text).is_some() || ours.iter().any(|opener| text.starts_with(opener))
|
||||
};
|
||||
let text = content.iter().enumerate().filter_map(|(index, block)| match block {
|
||||
crate::message::ContentBlock::Text(text) => Some((index, text)),
|
||||
_ => None,
|
||||
});
|
||||
let text = blocks.into_iter().enumerate().filter_map(|(index, text)| Some((index, text?)));
|
||||
text.filter(|(index, text)| *index == 0 || !for_the_model(text))
|
||||
.map(|(index, text)| (index, text.clone()))
|
||||
.map(|(index, text)| (index, text.to_string()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ pub(crate) use budget::{summary_max_tokens, IMAGE_TOKEN_ESTIMATE};
|
||||
pub use context::{context_entries, context_messages};
|
||||
pub use planner::find_compaction_point;
|
||||
pub use prune::{prune_tool_outputs, ContextPressure};
|
||||
pub(crate) use summary::SUMMARIZER_PROMPT;
|
||||
pub use summary::{format_structured_summary, serialize_for_summary, summary_carries_content};
|
||||
|
||||
/// Settings controlling compaction behavior.
|
||||
|
||||
@@ -5,6 +5,7 @@ use super::CompactionSettings;
|
||||
use crate::message::{ContentBlock, Message, ToolOutput};
|
||||
use crate::provider::ToolSchema;
|
||||
use crate::session::Entry;
|
||||
use std::borrow::Borrow;
|
||||
use tracing::{debug, warn};
|
||||
|
||||
/// Estimate the token count for a set of entries.
|
||||
@@ -15,8 +16,8 @@ use tracing::{debug, warn};
|
||||
///
|
||||
/// Still only an estimate. Where the provider has told us the real occupancy, prefer that:
|
||||
/// [`ContextBudget::observed_tokens`] and [`last_observed_context_tokens`].
|
||||
pub fn estimate_tokens(entries: &[Entry]) -> u64 {
|
||||
entries.iter().map(entry_to_token_estimate).sum()
|
||||
pub fn estimate_tokens<E: Borrow<Entry>>(entries: &[E]) -> u64 {
|
||||
entries.iter().map(|entry| entry_to_token_estimate(entry.borrow())).sum()
|
||||
}
|
||||
|
||||
/// Estimate the tokens a provider-facing message list occupies.
|
||||
@@ -100,15 +101,15 @@ fn block_token_estimate(block: &ContentBlock) -> u64 {
|
||||
///
|
||||
/// Returns `None` when the transcript has nothing measured to offer, and the caller falls back to
|
||||
/// [`estimate_tokens`].
|
||||
pub fn last_observed_context_tokens(entries: &[Entry]) -> Option<u64> {
|
||||
pub fn last_observed_context_tokens<E: Borrow<Entry>>(entries: &[E]) -> Option<u64> {
|
||||
last_observed_measurement(entries).map(|(_, occupancy)| occupancy)
|
||||
}
|
||||
|
||||
/// [`last_observed_context_tokens`], plus where in the transcript it was measured, so a caller can
|
||||
/// estimate only what has been appended since.
|
||||
fn last_observed_measurement(entries: &[Entry]) -> Option<(usize, u64)> {
|
||||
fn last_observed_measurement<E: Borrow<Entry>>(entries: &[E]) -> Option<(usize, u64)> {
|
||||
for (index, entry) in entries.iter().enumerate().rev() {
|
||||
match &entry.kind {
|
||||
match &entry.borrow().kind {
|
||||
crate::session::EntryKind::Compaction { .. } => return None,
|
||||
crate::session::EntryKind::Usage { source, totals, .. }
|
||||
if *source == crate::usage::UsageSource::Agent && totals.calls == 1 =>
|
||||
@@ -130,7 +131,11 @@ fn last_observed_measurement(entries: &[Entry]) -> Option<(usize, u64)> {
|
||||
/// schemas, plus an estimate of the entries appended since. With nothing measured (a fresh session,
|
||||
/// or one whose last usage predates a compaction) it estimates [`context_entries`] plus the prefix:
|
||||
/// what a compaction replaced is not sent, so it is not counted.
|
||||
pub fn prompt_tokens(entries: &[Entry], system: Option<&str>, tools: &[ToolSchema]) -> u64 {
|
||||
pub fn prompt_tokens<E: Borrow<Entry>>(
|
||||
entries: &[E],
|
||||
system: Option<&str>,
|
||||
tools: &[ToolSchema],
|
||||
) -> u64 {
|
||||
match last_observed_measurement(entries) {
|
||||
// The measurement already includes the prefix that call was sent with.
|
||||
Some((index, observed)) => observed + estimate_tokens(&entries[index + 1..]),
|
||||
@@ -140,7 +145,7 @@ pub fn prompt_tokens(entries: &[Entry], system: Option<&str>, tools: &[ToolSchem
|
||||
|
||||
/// Estimated tokens for the conversation a request would carry — [`estimate_tokens`] over
|
||||
/// [`context_entries`].
|
||||
pub fn context_tokens(entries: &[Entry]) -> u64 {
|
||||
pub fn context_tokens<E: Borrow<Entry>>(entries: &[E]) -> u64 {
|
||||
context_entries(entries).into_iter().map(entry_to_token_estimate).sum()
|
||||
}
|
||||
|
||||
@@ -217,11 +222,12 @@ pub fn clamp_max_tokens_to_context(
|
||||
|
||||
/// Output allowance for one summary, before the model's limit and the window cut it down.
|
||||
///
|
||||
/// Room for the seven sections over a whole window's worth of transcript *and* for the reasoning a
|
||||
/// model that always thinks spends first: that comes out of the same allowance, and a summary that
|
||||
/// hits the ceiling is refused whole, so a tight one fails every time on exactly the long sessions
|
||||
/// that need it.
|
||||
pub(crate) const SUMMARY_MAX_TOKENS: u32 = 16_384;
|
||||
/// Room for the six sections over a whole window's worth of transcript *and* for the reasoning a
|
||||
/// model that always thinks spends first, even asked not to: that comes out of the same allowance,
|
||||
/// and a summary that hits the ceiling is refused whole, so a tight one fails every time on exactly
|
||||
/// the long sessions that need it. Generous because only what's written is billed, while a refused
|
||||
/// summary has already paid for reading the whole window.
|
||||
pub(crate) const SUMMARY_MAX_TOKENS: u32 = 32_768;
|
||||
|
||||
/// What one summary request may ask for: [`SUMMARY_MAX_TOKENS`], within `model`'s own output limit
|
||||
/// (0 for unknown) and what `prompt_tokens` leaves of its window.
|
||||
|
||||
@@ -52,9 +52,9 @@ fn a_summary_gets_room_beyond_what_reasoning_spends() {
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Fails if the allowance drops back to where thinking alone could exhaust it
|
||||
// Fails if the allowance drops back to the 16K a long session's summary ran out of
|
||||
let roomy = summary_max_tokens(&model(1_000_000, 128_000), 880_000).expect("a cap");
|
||||
assert!(roomy >= 16_000, "{roomy}");
|
||||
assert!(roomy >= 32_000, "{roomy}");
|
||||
// Fails if the model's own output limit is ignored
|
||||
assert_eq!(summary_max_tokens(&model(1_000_000, 8_000), 10_000), Some(8_000));
|
||||
// An unknown limit is no limit of its own
|
||||
@@ -302,7 +302,7 @@ fn observed_occupancy_reads_the_last_agent_call_and_stops_at_a_compaction() {
|
||||
// call measures its own context, not the turn's.
|
||||
assert_eq!(last_observed_context_tokens(&[usage(UsageSource::Agent, 4, 200_000, 900)]), None);
|
||||
assert_eq!(last_observed_context_tokens(&[usage(UsageSource::Review, 1, 50_000, 100)]), None);
|
||||
assert_eq!(last_observed_context_tokens(&[]), None);
|
||||
assert_eq!(last_observed_context_tokens::<Entry>(&[]), None);
|
||||
|
||||
// A call that measured nothing measured nothing — it is not an empty context. Reading it
|
||||
// as one would report a full window as fresh and hand the next request the whole output
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use crate::message::Message;
|
||||
use crate::session::Entry;
|
||||
use std::borrow::Borrow;
|
||||
|
||||
/// How a compaction summary introduces itself to the model.
|
||||
///
|
||||
@@ -26,15 +27,16 @@ const SUMMARY_SUFFIX: &str = "\n</summary>";
|
||||
/// what it summarized. That one reads first: it covers history older than the newest summary and
|
||||
/// everything kept after it. A `first_kept` that isn't on the path (a hand-edited or partially
|
||||
/// written log) degrades to summary-plus-everything-after rather than dropping the conversation.
|
||||
pub fn context_entries(entries: &[Entry]) -> Vec<&Entry> {
|
||||
pub fn context_entries<E: Borrow<Entry>>(entries: &[E]) -> Vec<&Entry> {
|
||||
let entries: Vec<&Entry> = entries.iter().map(Borrow::borrow).collect();
|
||||
let Some(cut) = entries
|
||||
.iter()
|
||||
.rposition(|entry| matches!(entry.kind, crate::session::EntryKind::Compaction { .. }))
|
||||
else {
|
||||
return entries.iter().collect();
|
||||
return entries;
|
||||
};
|
||||
let crate::session::EntryKind::Compaction { first_kept, .. } = &entries[cut].kind else {
|
||||
return entries.iter().collect();
|
||||
return entries;
|
||||
};
|
||||
|
||||
let kept_from = entries[..cut].iter().position(|entry| entry.id == *first_kept);
|
||||
@@ -45,10 +47,10 @@ pub fn context_entries(entries: &[Entry]) -> Vec<&Entry> {
|
||||
// what it stands for.
|
||||
let is_summary =
|
||||
|entry: &&Entry| matches!(entry.kind, crate::session::EntryKind::Compaction { .. });
|
||||
context.extend(kept.iter().filter(is_summary));
|
||||
context.push(&entries[cut]);
|
||||
context.extend(kept.iter().filter(|entry| !is_summary(entry)));
|
||||
context.extend(entries[cut + 1..].iter());
|
||||
context.extend(kept.iter().copied().filter(is_summary));
|
||||
context.push(entries[cut]);
|
||||
context.extend(kept.iter().copied().filter(|entry| !is_summary(entry)));
|
||||
context.extend(entries[cut + 1..].iter().copied());
|
||||
context
|
||||
}
|
||||
|
||||
@@ -57,7 +59,7 @@ pub fn context_entries(entries: &[Entry]) -> Vec<&Entry> {
|
||||
///
|
||||
/// The one place a transcript becomes a request, so the cut cannot be applied in some callers and
|
||||
/// forgotten in others.
|
||||
pub fn context_messages(entries: &[Entry]) -> Vec<Message> {
|
||||
pub fn context_messages<E: Borrow<Entry>>(entries: &[E]) -> Vec<Message> {
|
||||
context_entries(entries)
|
||||
.into_iter()
|
||||
.filter_map(|entry| match &entry.kind {
|
||||
|
||||
@@ -27,8 +27,8 @@ impl ContextPressure {
|
||||
///
|
||||
/// Prefers the provider's own figures: the char/4 estimate reads a prompt of code as about two
|
||||
/// thirds of its size, and a threshold hung off it fires late.
|
||||
pub fn measure(
|
||||
entries: &[Entry],
|
||||
pub fn measure<E: std::borrow::Borrow<Entry>>(
|
||||
entries: &[E],
|
||||
system: Option<&str>,
|
||||
tools: &[ToolSchema],
|
||||
usable_window: u64,
|
||||
@@ -54,10 +54,10 @@ impl ContextPressure {
|
||||
/// measurement from before the cut describes a context that no longer exists, and count only
|
||||
/// single-call records, because a batched flush's summed prompt tokens are not an occupancy figure
|
||||
/// of anything.
|
||||
fn observed_context_peak(entries: &[Entry]) -> Option<u64> {
|
||||
fn observed_context_peak<E: std::borrow::Borrow<Entry>>(entries: &[E]) -> Option<u64> {
|
||||
let mut peak = None;
|
||||
for entry in entries.iter().rev() {
|
||||
match &entry.kind {
|
||||
match &entry.borrow().kind {
|
||||
crate::session::EntryKind::Compaction { .. } => break,
|
||||
crate::session::EntryKind::Usage { source, totals, .. }
|
||||
if *source == crate::usage::UsageSource::Agent && totals.calls == 1 =>
|
||||
@@ -138,13 +138,23 @@ fn elide_tool_output_before(messages: &mut [Message], boundary: u64) {
|
||||
.collect();
|
||||
for (mi, bi, tokens) in stale {
|
||||
if let ContentBlock::ToolResult { content, .. } = &mut messages[mi].content[bi] {
|
||||
*content = vec![ToolOutput::Text(format!(
|
||||
"[{tokens} tokens of earlier tool output elided to save context]"
|
||||
))];
|
||||
*content = elided(std::mem::take(content), tokens);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A placeholder for `content`, keeping any nested instruction file it carried.
|
||||
///
|
||||
/// The saved log still holds the file, so it counts as delivered and won't go again; eliding it
|
||||
/// here would leave the model without those rules for the rest of the session.
|
||||
fn elided(content: Vec<ToolOutput>, tokens: u64) -> Vec<ToolOutput> {
|
||||
let placeholder = format!("[{tokens} tokens of earlier tool output elided to save context]");
|
||||
let notes = content.into_iter().filter(|output| {
|
||||
matches!(output, ToolOutput::Text(text) if crate::instructions::nested::noted(text).is_some())
|
||||
});
|
||||
std::iter::once(ToolOutput::Text(placeholder)).chain(notes).collect()
|
||||
}
|
||||
|
||||
/// Every non-empty tool result in the list as `(message, block, tokens)`, oldest first.
|
||||
fn tool_output_tokens(messages: &[Message]) -> impl Iterator<Item = (usize, usize, u64)> + '_ {
|
||||
messages.iter().enumerate().flat_map(|(mi, msg)| {
|
||||
|
||||
@@ -302,3 +302,38 @@ fn the_elision_boundary_moves_in_whole_steps() {
|
||||
assert_eq!(boundary(1_999), 500, "and it holds there until the next whole step");
|
||||
assert_eq!(boundary(2_100), 1_000);
|
||||
}
|
||||
|
||||
/// Eliding a stale result keeps the nested instruction file it carried, and only that.
|
||||
///
|
||||
/// The break this catches: the file going with the rest of the result. The saved log still holds
|
||||
/// it, so it counts as delivered and is never sent again, and the model loses those rules.
|
||||
#[test]
|
||||
fn eliding_a_result_keeps_the_nested_instructions_it_carried() {
|
||||
let settings =
|
||||
CompactionSettings { prune_protect_tokens: 40, prune_min_tokens: 20, ..Default::default() };
|
||||
let note = crate::instructions::nested::note("web/AGENTS.md", "- WEB-RULE");
|
||||
let mut messages = [tool_msg("a", 1000, false), tool_msg("b", 100, false)]
|
||||
.iter()
|
||||
.map(msg_of)
|
||||
.collect::<Vec<_>>();
|
||||
if let ContentBlock::ToolResult { content, .. } = &mut messages[0].content[0] {
|
||||
content.push(ToolOutput::Text(note.clone()));
|
||||
content.push(ToolOutput::Text("[instructions: looks like one, isn't".to_string()));
|
||||
}
|
||||
prune_tool_outputs(&mut messages, &settings, under_pressure());
|
||||
let ContentBlock::ToolResult { content, .. } = &messages[0].content[0] else {
|
||||
panic!("expected a tool result");
|
||||
};
|
||||
let texts = texts_of(content);
|
||||
assert!(texts[0].contains("elided to save context"), "not elided at all");
|
||||
assert_eq!(texts[1..], [note.as_str()], "the note, and nothing else, kept");
|
||||
}
|
||||
|
||||
/// The text blocks of a tool result.
|
||||
fn texts_of(content: &[ToolOutput]) -> Vec<&str> {
|
||||
let texts = content.iter().filter_map(|output| match output {
|
||||
ToolOutput::Text(text) => Some(text.as_str()),
|
||||
_ => None,
|
||||
});
|
||||
texts.collect()
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ pub fn serialize_for_summary(entries: &[Entry]) -> String {
|
||||
}
|
||||
}
|
||||
crate::session::EntryKind::Compaction { summary, .. } => {
|
||||
output.push_str(&format!("[Compaction]: {summary}\n"));
|
||||
output.push_str(&format!("[Compaction]: {}\n", without_file_list(summary)));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -134,6 +134,26 @@ pub fn serialize_for_summary(entries: &[Entry]) -> String {
|
||||
output
|
||||
}
|
||||
|
||||
/// A previous summary as the summarizer reads it: everything but the file list.
|
||||
///
|
||||
/// The list is carried forward in code ([`files_listed_in`]) and rewritten on every summary, so
|
||||
/// showing it to the summarizer only invites a copy: hundreds of lines on a long session, spent from
|
||||
/// the output allowance the summary itself needs, and thrown away.
|
||||
fn without_file_list(summary: &str) -> String {
|
||||
let mut kept = String::new();
|
||||
let mut inside = false;
|
||||
for line in summary.lines() {
|
||||
if let Some(heading) = line.trim().strip_prefix("## ") {
|
||||
inside = FILE_SECTION_ALIASES.contains(&heading.trim());
|
||||
}
|
||||
if !inside {
|
||||
kept.push_str(line);
|
||||
kept.push('\n');
|
||||
}
|
||||
}
|
||||
kept.trim_end().to_string()
|
||||
}
|
||||
|
||||
/// Whether an entry puts anything into [`serialize_for_summary`]'s output.
|
||||
///
|
||||
/// The mirror of that function's `match`, and the reason it is a predicate rather than a length
|
||||
@@ -201,6 +221,23 @@ const REQUIRED_SECTIONS: [&str; 6] = [
|
||||
"Critical Context",
|
||||
];
|
||||
|
||||
/// What the summarizer is told to write.
|
||||
///
|
||||
/// The `## Heading` shape is named because it is what the reply is parsed by: a model told only to
|
||||
/// be "structured" may answer in JSON, and every section of it is then dropped as preamble. No file
|
||||
/// list is asked for, since one is added from the transcript afterward, and a model asked for one
|
||||
/// copies hundreds of paths out of the allowance the summary needs. The last sentence is there
|
||||
/// because a smaller model will otherwise file *these rules* under `Constraints & Preferences` as
|
||||
/// though they were the session's.
|
||||
pub(crate) const SUMMARIZER_PROMPT: &str = "You are a session compactor. Summarize the \
|
||||
conversation segment below so the work can continue from the summary alone. Write about the \
|
||||
session's own goal, constraints, progress (done, in progress, blocked), decisions, next steps, \
|
||||
and anything that would be expensive to rediscover. Answer in Markdown using exactly these \
|
||||
headings, one `## ` heading per section with prose under it and nothing before the first: \
|
||||
`## Goal`, `## Constraints & Preferences`, `## Progress`, `## Key Decisions`, `## Next Steps`, \
|
||||
`## Critical Context`. Don't list the files touched; that list is kept separately. These \
|
||||
formatting rules are not part of the session: never describe them in the summary.";
|
||||
|
||||
/// Shape the summarizer's prose into the sections a resumed session expects, and append the file
|
||||
/// record it cannot write for itself.
|
||||
///
|
||||
|
||||
@@ -137,6 +137,66 @@ fn an_earlier_compaction_summary_reaches_the_summarizer() {
|
||||
assert!(serialized.starts_with("[Compaction]: ## Goal"), "{serialized}");
|
||||
}
|
||||
|
||||
/// The summarizer is asked for every section a summary carries, and for no file list.
|
||||
///
|
||||
/// The break this catches: the prompt asking for `## Files` again, which spends the summary's
|
||||
/// allowance on a list that's replaced anyway, or naming fewer sections than the summary is filed
|
||||
/// under, which leaves the missing ones empty on every compaction.
|
||||
#[test]
|
||||
fn the_summarizer_is_asked_for_every_section_but_the_files() {
|
||||
for section in REQUIRED_SECTIONS {
|
||||
assert!(SUMMARIZER_PROMPT.contains(&format!("`## {section}`")), "{section} not asked for");
|
||||
}
|
||||
for files in FILE_SECTION_ALIASES {
|
||||
assert!(!SUMMARIZER_PROMPT.contains(&format!("## {files}")), "{files} asked for");
|
||||
}
|
||||
}
|
||||
|
||||
fn earlier_summary(summary: &str) -> String {
|
||||
serialize_for_summary(&[Entry {
|
||||
id: EntryId::new(),
|
||||
parent_id: None,
|
||||
ts: 0,
|
||||
kind: EntryKind::Compaction {
|
||||
summary: summary.to_string(),
|
||||
first_kept: EntryId::new(),
|
||||
tokens_before: 90_000,
|
||||
},
|
||||
}])
|
||||
}
|
||||
|
||||
/// The summarizer reads an earlier summary's prose but not its file list, which code carries.
|
||||
///
|
||||
/// The break this catches: showing the list again, which a summarizer copies out path by path,
|
||||
/// hundreds of them on a long session, until the summary stops short and the compaction fails.
|
||||
#[test]
|
||||
fn an_earlier_summarys_file_list_is_kept_from_the_summarizer() {
|
||||
let read = earlier_summary("## Goal\n\nShip it.\n\n## Files\n\n- src/listed.rs\n");
|
||||
assert!(read.contains("Ship it."), "{read}");
|
||||
assert!(!read.contains("src/listed.rs"), "{read}");
|
||||
}
|
||||
|
||||
/// A section after the file list still reaches the summarizer.
|
||||
///
|
||||
/// The break this catches: dropping everything from the first file heading on, which loses any
|
||||
/// section a summary wrote after its list.
|
||||
#[test]
|
||||
fn a_section_after_the_file_list_still_reaches_the_summarizer() {
|
||||
let read = earlier_summary("## Files\n\n- src/listed.rs\n\n## Next Steps\n\nRun the gate.");
|
||||
assert!(read.contains("Run the gate."), "{read}");
|
||||
}
|
||||
|
||||
/// The two file headings older builds wrote are kept from the summarizer too.
|
||||
///
|
||||
/// The break this catches: matching only today's heading, so a session compacted by an older build
|
||||
/// shows its whole list once more and copies it into the next summary.
|
||||
#[test]
|
||||
fn an_older_summarys_file_lists_are_kept_from_the_summarizer() {
|
||||
let read =
|
||||
earlier_summary("## Read Files\n\n- src/old.rs\n\n## Modified Files\n\n- src/older.rs");
|
||||
assert!(!read.contains("src/old"), "{read}");
|
||||
}
|
||||
|
||||
/// The crash this cap used to be: slicing byte 1997 of a block whose 1997th byte is the middle
|
||||
/// of a character panics, in the middle of a compaction the user never asked for.
|
||||
#[test]
|
||||
|
||||
@@ -78,6 +78,9 @@ pub struct Config {
|
||||
/// Report the agent's state to herdr when Luxe runs in one of its panes. `None` = use default
|
||||
/// (on).
|
||||
pub herdr: Option<bool>,
|
||||
/// Let another front-end on this machine join a running session. `None` = use default (on).
|
||||
/// Only this user can connect, and a project file can turn it off but not back on.
|
||||
pub joinable: Option<bool>,
|
||||
/// Statusline module order and visibility
|
||||
pub statusline: StatuslineConfig,
|
||||
/// Conversation canvas and tool-detail preferences
|
||||
@@ -188,6 +191,15 @@ impl Config {
|
||||
self.herdr.unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Whether a running session can be joined from another front-end on this machine (default:
|
||||
/// yes). `LUXE_JOINABLE=0` turns it off for one launch.
|
||||
pub fn joinable(&self) -> bool {
|
||||
let opted_out = std::env::var("LUXE_JOINABLE").is_ok_and(|value| {
|
||||
matches!(value.trim().to_ascii_lowercase().as_str(), "0" | "false" | "no" | "off")
|
||||
});
|
||||
!opted_out && self.joinable.unwrap_or(true)
|
||||
}
|
||||
|
||||
/// Resolves the active theme name, falling back to the current desktop theme when known.
|
||||
///
|
||||
/// An explicit Luxe setting always wins. Without one, the desktop's theme is used when the
|
||||
|
||||
@@ -110,6 +110,11 @@
|
||||
"default": true,
|
||||
"description": "When Luxe runs in a herdr pane, report whether it is working, idle or waiting on you, so herdr's sidebar, notifications and `herdr agent wait` follow it. On by default; nothing is sent outside herdr."
|
||||
},
|
||||
"joinable": {
|
||||
"type": "boolean",
|
||||
"default": true,
|
||||
"description": "Let another front-end on this machine join a running session, such as `luxe join` or the `luxe remote` bridge. It goes through a socket (a named pipe on Windows) only your user account can open, and commands the agent runs are turned away. On by default; false turns it off, as does LUXE_JOINABLE=0. A project's config can turn it off but not back on."
|
||||
},
|
||||
"diff": {
|
||||
"type": "object",
|
||||
"description": "How every surface draws a diff: the edit/write tool card, the preview beside the conversation, and Guided Review. One setting, because the three differ only in how much room they have.",
|
||||
|
||||
@@ -11,6 +11,12 @@ impl Config {
|
||||
luxe_config_dir().join("config.json")
|
||||
}
|
||||
|
||||
/// The user-level config alone, for what belongs to no one project, such as the theme
|
||||
/// `luxe remote` draws the phone app in. Missing or malformed reads as the defaults.
|
||||
pub fn load_global() -> Self {
|
||||
load_global_config().unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Path to the optional custom debug-adapter registry (`debug-adapters.json`).
|
||||
pub fn debug_adapters_path() -> PathBuf {
|
||||
luxe_config_dir().join("debug-adapters.json")
|
||||
|
||||
@@ -34,6 +34,7 @@ impl Layered for Config {
|
||||
notification_method,
|
||||
bell,
|
||||
herdr,
|
||||
joinable,
|
||||
statusline,
|
||||
conversation,
|
||||
dock,
|
||||
@@ -65,6 +66,11 @@ impl Layered for Config {
|
||||
notification_method: notification_method.or(global.notification_method),
|
||||
bell: bell.or(global.bell),
|
||||
herdr: herdr.or(global.herdr),
|
||||
// A project can close its sessions to other front-ends, never open what the user closed
|
||||
joinable: match global.joinable {
|
||||
Some(false) => Some(false),
|
||||
_ => joinable.or(global.joinable),
|
||||
},
|
||||
statusline: Layered::layered(global.statusline, statusline),
|
||||
conversation: Layered::layered(global.conversation, conversation),
|
||||
dock: Layered::layered(global.dock, dock),
|
||||
|
||||
@@ -674,6 +674,23 @@ fn herdr_reporting_is_on_until_it_is_turned_off() {
|
||||
assert!(!off.herdr());
|
||||
}
|
||||
|
||||
/// Sessions are joinable until turned off, and a project can turn that off but not back on.
|
||||
///
|
||||
/// The break this catches: merging `joinable` like any other key, so a cloned repository's
|
||||
/// `.luxe/config.json` reopens the socket the user closed.
|
||||
#[test]
|
||||
fn a_project_can_close_joining_but_not_reopen_it() {
|
||||
use super::merge::Layered;
|
||||
let parse = |json: &str| serde_json::from_str::<Config>(json).unwrap();
|
||||
assert_eq!(Config::default().joinable, None, "on, by default");
|
||||
let reopened = Config::layered(parse(r#"{"joinable": false}"#), parse(r#"{"joinable": true}"#));
|
||||
assert_eq!(reopened.joinable, Some(false), "the user's off stands");
|
||||
let closed = Config::layered(Config::default(), parse(r#"{"joinable": false}"#));
|
||||
assert_eq!(closed.joinable, Some(false), "the project's off stands");
|
||||
let left = Config::layered(parse(r#"{"joinable": true}"#), Config::default());
|
||||
assert_eq!(left.joinable, Some(true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ensure_config_file_bootstraps_schema_without_overwriting_config() {
|
||||
let directory = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -81,6 +81,8 @@ pub struct Engine {
|
||||
/// Mutable session id — updated when loading/resuming a session so the next turn chains
|
||||
/// from the resumed state, matching the `--resume` path.
|
||||
session_id: tokio::sync::Mutex<crate::session::SessionId>,
|
||||
/// This process's claim on `session_id`, which keeps a second Luxe from writing it too
|
||||
session_claim: std::sync::Mutex<Option<crate::session::SessionClaim>>,
|
||||
last_turn_baseline: tokio::sync::Mutex<Option<RetainedTurnBaseline>>,
|
||||
active_guided_review: Arc<tokio::sync::Mutex<ActiveGuidedReview>>,
|
||||
/// The one owner of the open review and its feedback session, running beside the engine
|
||||
@@ -227,6 +229,7 @@ pub use review_desk::{CommentDraft, ReviewEdit};
|
||||
pub use review_feedback::PULL_REQUEST_FIXES_REFUSED;
|
||||
mod guided_review;
|
||||
mod lifecycle;
|
||||
mod nested_instructions;
|
||||
mod opening;
|
||||
mod posting;
|
||||
mod previews;
|
||||
|
||||
@@ -330,6 +330,14 @@ pub enum Action {
|
||||
/// The review it is for
|
||||
review_id: ReviewId,
|
||||
},
|
||||
/// Answers with the open review and its session, whoever opened it, as `OpenReviewShown`.
|
||||
///
|
||||
/// How a second front end joins the review on screen rather than loading its own, which would
|
||||
/// replace it: a load always makes a new review.
|
||||
ShowOpenReview {
|
||||
/// Correlates the answer
|
||||
request_id: ReviewLoadRequestId,
|
||||
},
|
||||
/// Holds one stored review-feedback submission for its turn.
|
||||
///
|
||||
/// Sent by the review desk once the submission is stored, never by a front end, which sends
|
||||
@@ -397,3 +405,16 @@ pub enum Action {
|
||||
/// Re-run the assistant turn for the current last user message (new branch).
|
||||
Regenerate,
|
||||
}
|
||||
|
||||
impl Action {
|
||||
/// Whether the review desk takes this as it comes, mid-turn or not: a send that never waits.
|
||||
pub(crate) fn goes_to_review_desk(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
Self::EditReview { .. }
|
||||
| Self::RecordReviewProgress { .. }
|
||||
| Self::RecordReviewSeen { .. }
|
||||
| Self::ShowOpenReview { .. }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ impl Engine {
|
||||
remediation.as_ref().and_then(|context| context.submission_id.clone());
|
||||
let turn_id = AgentTurnId::new();
|
||||
debug!(turn_id = %turn_id.as_str(), "starting agent turn");
|
||||
// First, so a hook refusing the turn below still ends one that had started
|
||||
let _ = event_tx.send(Event::TurnStarted).await;
|
||||
|
||||
*self.last_turn_baseline.lock().await = None;
|
||||
let retained_baseline = match self.capture_pre_turn_baseline(&turn_id).await {
|
||||
|
||||
@@ -52,10 +52,10 @@ impl Engine {
|
||||
async fn estimated_context_tokens(&self, leaf: &EntryId) -> u64 {
|
||||
let sid = self.current_session_id().await;
|
||||
let Ok(store) = self.config.session_store.read() else { return 0 };
|
||||
store
|
||||
.active_path(&sid, leaf)
|
||||
.map(|entries| crate::compaction::context_tokens(&entries))
|
||||
.unwrap_or(0)
|
||||
crate::session::with_active_path(&**store, &sid, leaf, |entries| {
|
||||
crate::compaction::context_tokens(entries)
|
||||
})
|
||||
.unwrap_or(0)
|
||||
}
|
||||
|
||||
/// Whether the last measured occupancy is comfortably under the threshold.
|
||||
@@ -345,10 +345,10 @@ impl Engine {
|
||||
|
||||
// What the cut actually bought, measured over the path it produced rather than predicted.
|
||||
// A client watching for this is watching for the one thing compaction is for.
|
||||
let tokens_after = self
|
||||
.sessions()
|
||||
.active_path(&sid, &compacted)
|
||||
.map(|path| crate::compaction::context_tokens(&path))
|
||||
let tokens_after =
|
||||
crate::session::with_active_path(&**self.sessions(), &sid, &compacted, |path| {
|
||||
crate::compaction::context_tokens(path)
|
||||
})
|
||||
.unwrap_or_default();
|
||||
// What drafts carried of a conversation went with the transcript
|
||||
self.sent_digests.lock().await.forget();
|
||||
|
||||
@@ -17,22 +17,7 @@ impl Engine {
|
||||
crate::provider::ModelInfo,
|
||||
crate::provider::CompletionRequest,
|
||||
) {
|
||||
// The `## Heading` shape is named because it is what the reply is parsed by: a model told
|
||||
// only to be "structured" may answer in JSON, and every section of it is then dropped as
|
||||
// preamble — which a 27B local model did, three times, for a summary that then replaced the
|
||||
// transcript with nothing. The last sentence is there because a smaller model will otherwise
|
||||
// file *these rules* under `Constraints & Preferences` as though they were the session's.
|
||||
let system_prompt =
|
||||
"You are a session compactor. Summarize the conversation segment below \
|
||||
so the work can continue from the summary alone. Write about the \
|
||||
session's own goal, constraints, progress (done, in progress, \
|
||||
blocked), decisions, next steps, and anything that would be expensive \
|
||||
to rediscover. Answer in Markdown using exactly these headings, one \
|
||||
`## ` heading per section with prose under it and nothing before the \
|
||||
first: `## Goal`, `## Constraints & Preferences`, `## Progress`, \
|
||||
`## Key Decisions`, `## Next Steps`, `## Critical Context`, \
|
||||
`## Files` (every file read or modified). These formatting rules are \
|
||||
not part of the session: never describe them in the summary.";
|
||||
let system_prompt = crate::compaction::SUMMARIZER_PROMPT;
|
||||
|
||||
// The user's steer goes *before* the transcript, where an instruction still reads as one:
|
||||
// after fifty thousand characters of conversation it reads as more conversation.
|
||||
|
||||
@@ -57,6 +57,7 @@ impl Engine {
|
||||
before_mode: tokio::sync::Mutex::new(None),
|
||||
current_leaf: tokio::sync::Mutex::new(None),
|
||||
session_id: tokio::sync::Mutex::new(initial_session_id),
|
||||
session_claim: std::sync::Mutex::new(None),
|
||||
last_turn_baseline: tokio::sync::Mutex::new(None),
|
||||
active_guided_review: Arc::new(tokio::sync::Mutex::new(ActiveGuidedReview::default())),
|
||||
review_desk,
|
||||
|
||||
@@ -182,6 +182,8 @@ impl Engine {
|
||||
}
|
||||
};
|
||||
tracing::info!(?reply, waited_ms = asked.elapsed().as_millis(), "permission prompt closed");
|
||||
// Every front-end showing it takes it down, whichever one answered
|
||||
let _ = event_tx.send(Event::PromptClosed { id: id.as_str().to_string() }).await;
|
||||
|
||||
self.config
|
||||
.hook_registry
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
//! Baseline — the turn snapshots and persisted review a restarted engine picks up again.
|
||||
|
||||
use super::*;
|
||||
use crate::guided_review::{
|
||||
prune_turn_baselines, LastTurnRecordStoreError, UNREFERENCED_BASELINE_AGE,
|
||||
};
|
||||
|
||||
impl Engine {
|
||||
/// Restores a complete retained Last Turn baseline without reading the mutable workspace.
|
||||
@@ -141,10 +144,15 @@ impl Engine {
|
||||
);
|
||||
let record = LastTurnRecord::new(&baseline.session_id, baseline.turn_id.as_str())
|
||||
.map_err(|error| GuidedReviewError::LastTurnRecord(error.to_string()))?;
|
||||
tokio::task::spawn_blocking(move || LastTurnRecordStore::new(project_root).save(&record))
|
||||
.await
|
||||
.map_err(|_| GuidedReviewError::LastTurnRecordTask)?
|
||||
.map_err(|error| GuidedReviewError::LastTurnRecord(error.to_string()))?;
|
||||
tokio::task::spawn_blocking(move || {
|
||||
LastTurnRecordStore::new(&project_root).save(&record)?;
|
||||
let keep = [record.baseline_id(), record.after_snapshot_id()];
|
||||
prune_turn_baselines(&project_root, &keep, UNREFERENCED_BASELINE_AGE);
|
||||
Ok::<_, LastTurnRecordStoreError>(())
|
||||
})
|
||||
.await
|
||||
.map_err(|_| GuidedReviewError::LastTurnRecordTask)?
|
||||
.map_err(|error| GuidedReviewError::LastTurnRecord(error.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -143,11 +143,9 @@ impl Engine {
|
||||
// A review edit clears the same bar: handing it to the desk is a send that
|
||||
// never waits. Deferred, every comment written while the agent worked on the
|
||||
// last batch sat behind the whole turn, and the review refused the next one.
|
||||
Some(
|
||||
action @ (Action::EditReview { .. }
|
||||
| Action::RecordReviewProgress { .. }
|
||||
| Action::RecordReviewSeen { .. }),
|
||||
) => self.forward_to_review_desk(action),
|
||||
Some(action) if action.goes_to_review_desk() => {
|
||||
self.forward_to_review_desk(action);
|
||||
}
|
||||
// Awaits only locks the turn takes and drops at once, so `Cancel` still gets
|
||||
// through. Deferred, a switch made to stop the agent writing reached it only
|
||||
// after the turn had written, and the label never moved meanwhile
|
||||
|
||||
@@ -6,15 +6,17 @@ impl Engine {
|
||||
pub(crate) async fn latest_user_goal(&self) -> Option<String> {
|
||||
let session_id = self.current_session_id().await;
|
||||
let leaf = self.current_leaf.lock().await.clone()?;
|
||||
let entries =
|
||||
self.config.session_store.read().ok()?.active_path(&session_id, &leaf).ok()?;
|
||||
entries.iter().rev().find_map(|entry| match &entry.kind {
|
||||
EntryKind::Message(message) if message.role == Role::User => {
|
||||
let text = message.text();
|
||||
(!text.trim().is_empty()).then_some(text)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
let store = self.config.session_store.read().ok()?;
|
||||
let goal = crate::session::with_active_path(&**store, &session_id, &leaf, |entries| {
|
||||
entries.iter().rev().find_map(|entry| match &entry.kind {
|
||||
EntryKind::Message(message) if message.role == Role::User => {
|
||||
let text = message.text();
|
||||
(!text.trim().is_empty()).then_some(text)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
});
|
||||
goal.ok()?
|
||||
}
|
||||
|
||||
/// Reads what produced the current changes off the active transcript.
|
||||
@@ -25,20 +27,17 @@ impl Engine {
|
||||
const RECENT_ENTRIES: usize = 120;
|
||||
let session_id = self.current_session_id().await;
|
||||
let leaf = self.current_leaf.lock().await.clone()?;
|
||||
let entries =
|
||||
self.config.session_store.read().ok()?.active_path(&session_id, &leaf).ok()?;
|
||||
let messages: Vec<crate::message::Message> = entries
|
||||
.iter()
|
||||
.rev()
|
||||
.take(RECENT_ENTRIES)
|
||||
.collect::<Vec<_>>()
|
||||
.into_iter()
|
||||
.rev()
|
||||
.filter_map(|entry| match &entry.kind {
|
||||
let store = self.config.session_store.read().ok()?;
|
||||
let recent = crate::session::with_active_path(&**store, &session_id, &leaf, |entries| {
|
||||
let from = entries.len().saturating_sub(RECENT_ENTRIES);
|
||||
let messages = entries[from..].iter().filter_map(|entry| match &entry.kind {
|
||||
EntryKind::Message(message) => Some(message.clone()),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
});
|
||||
messages.collect::<Vec<_>>()
|
||||
});
|
||||
let messages = recent.ok()?;
|
||||
drop(store);
|
||||
let provenance = crate::guided_review::TurnProvenance::from_messages(
|
||||
self.config.guided_review_project_root.as_deref(),
|
||||
&messages,
|
||||
@@ -170,6 +169,13 @@ impl Engine {
|
||||
})
|
||||
}
|
||||
|
||||
/// Hold the claim on the session just loaded, letting go of the one before it.
|
||||
fn hold_claim(&self, claim: Option<crate::session::SessionClaim>) {
|
||||
// Only `load_session` takes it, and never across an await
|
||||
let mut held = self.session_claim.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
|
||||
*held = claim;
|
||||
}
|
||||
|
||||
pub(crate) async fn load_session(
|
||||
&self,
|
||||
session_id: String,
|
||||
@@ -177,13 +183,15 @@ impl Engine {
|
||||
event_tx: mpsc::Sender<Event>,
|
||||
) -> Result<(), SessionError> {
|
||||
let id = SessionId(session_id.clone());
|
||||
let (leaf, messages, used_tokens) = {
|
||||
let (leaf, messages, used_tokens, claim) = {
|
||||
let store = self
|
||||
.config
|
||||
.session_store
|
||||
.read()
|
||||
.map_err(|_| SessionError::Other("session store lock poisoned".to_string()))?;
|
||||
let entries = store.load(&id)?;
|
||||
// Before anything changes, so a session open in another Luxe leaves this one as it was
|
||||
let claim = store.claim(&id)?;
|
||||
let leaf = crate::session::latest_leaf(&entries);
|
||||
let active = match leaf.as_ref() {
|
||||
Some(leaf) => store.active_path(&id, leaf)?,
|
||||
@@ -205,7 +213,7 @@ impl Engine {
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
(leaf, messages, used_tokens)
|
||||
(leaf, messages, used_tokens, claim)
|
||||
};
|
||||
|
||||
// A session that already carries messages is a resume (CLI `--resume`, `-c`, or the
|
||||
@@ -221,6 +229,7 @@ impl Engine {
|
||||
self.retire_session_ledger(self.current_session_id().await);
|
||||
}
|
||||
*self.session_id.lock().await = id.clone();
|
||||
self.hold_claim(claim);
|
||||
*self.current_leaf.lock().await = leaf;
|
||||
// Loading a session is how its prompt picks up instruction and skill edits
|
||||
self.forget_prompt();
|
||||
|
||||
144
crates/luxe-core/src/engine/nested_instructions.rs
Normal file
144
crates/luxe-core/src/engine/nested_instructions.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
//! Nested instructions, delivered — the engine side of [`crate::instructions::nested`].
|
||||
//!
|
||||
//! Runs on a finished batch, just before it commits, so a file rides on the result of the first
|
||||
//! call that reached its folder and is durable history from then on. Every later request carries
|
||||
//! it as part of the transcript it already appends to; the system prompt never moves.
|
||||
|
||||
use super::*;
|
||||
use crate::instructions::nested::{self, NestedFile};
|
||||
use crate::message::ToolOutput;
|
||||
use std::collections::HashSet;
|
||||
use std::path::PathBuf;
|
||||
|
||||
impl Engine {
|
||||
/// Appends each nested instruction file the batch reached, and the model doesn't have yet, to
|
||||
/// the result of the first call that reached it.
|
||||
///
|
||||
/// Args: `(leaf, calls, results, event_tx)`. `leaf` is the reply the results answer, so the
|
||||
/// conversation read is the one these results join.
|
||||
pub(super) async fn attach_nested_instructions(
|
||||
&self,
|
||||
leaf: &EntryId,
|
||||
calls: &[ToolCallInfo],
|
||||
results: &mut [ContentBlock],
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) {
|
||||
if !nested::enabled() {
|
||||
return;
|
||||
}
|
||||
let wanted = self.nested_wanted(calls, results);
|
||||
if wanted.is_empty() {
|
||||
return;
|
||||
}
|
||||
let held = self.nested_on_path(leaf).await;
|
||||
for (call_id, file) in wanted.into_iter().filter(|(_, file)| !held.contains(&file.shown)) {
|
||||
attach_one(&call_id, &file, results, event_tx).await;
|
||||
}
|
||||
}
|
||||
|
||||
/// The nested files this batch's successful calls reached, each once, with the call that
|
||||
/// reached it first.
|
||||
fn nested_wanted(
|
||||
&self,
|
||||
calls: &[ToolCallInfo],
|
||||
results: &[ContentBlock],
|
||||
) -> Vec<(String, NestedFile)> {
|
||||
let launch = self.instruction_dir();
|
||||
let mut seen: HashSet<PathBuf> = HashSet::new();
|
||||
let mut wanted = Vec::new();
|
||||
for call in calls.iter().filter(|call| succeeded(results, &call.id)) {
|
||||
for file in reached(&launch, call) {
|
||||
if seen.insert(file.path.clone()) {
|
||||
wanted.push((call.id.clone(), file));
|
||||
}
|
||||
}
|
||||
}
|
||||
wanted
|
||||
}
|
||||
|
||||
/// The nested files the conversation up to `leaf` already carries, by the notes that open
|
||||
/// them. Read rather than remembered, so one a rewind or a compaction took away goes again.
|
||||
async fn nested_on_path(&self, leaf: &EntryId) -> HashSet<String> {
|
||||
let sid = self.current_session_id().await;
|
||||
let read = crate::session::with_active_path(&**self.sessions(), &sid, leaf, |entries| {
|
||||
crate::compaction::context_messages(entries)
|
||||
});
|
||||
match read {
|
||||
Ok(messages) => notes_in(&messages),
|
||||
Err(error) => {
|
||||
// Sending a file twice costs tokens; not sending it costs the rules
|
||||
warn!(%error, "could not read which nested instructions the model has");
|
||||
HashSet::new()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The nested files `call` reached, from Luxe launched at `launch`.
|
||||
///
|
||||
/// Relative paths resolve the way the tool resolved them, against where tools run.
|
||||
fn reached(launch: &std::path::Path, call: &ToolCallInfo) -> Vec<NestedFile> {
|
||||
let args = parse_tool_arguments(&call.arguments);
|
||||
let base = ToolContext::working_dir();
|
||||
let targets = nested::touched_paths(&args);
|
||||
targets
|
||||
.into_iter()
|
||||
.flat_map(|target| nested::nested_files(launch, &base.join(target)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The output of the result answering `call_id`.
|
||||
fn output_of<'a>(
|
||||
results: &'a mut [ContentBlock],
|
||||
call_id: &str,
|
||||
) -> Option<&'a mut Vec<ToolOutput>> {
|
||||
results.iter_mut().find_map(|block| match block {
|
||||
ContentBlock::ToolResult { call_id: id, content, .. } if id == call_id => Some(content),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether the result answering `call_id` ran and succeeded.
|
||||
fn succeeded(results: &[ContentBlock], call_id: &str) -> bool {
|
||||
results.iter().any(|block| {
|
||||
matches!(block, ContentBlock::ToolResult { call_id: id, is_error: false, .. } if id == call_id)
|
||||
})
|
||||
}
|
||||
|
||||
/// Every nested file named by a note in a tool result of `messages`.
|
||||
fn notes_in(messages: &[Message]) -> HashSet<String> {
|
||||
let outputs =
|
||||
messages.iter().flat_map(|message| &message.content).filter_map(|block| match block {
|
||||
ContentBlock::ToolResult { content, .. } => Some(content),
|
||||
_ => None,
|
||||
});
|
||||
outputs
|
||||
.flatten()
|
||||
.filter_map(|output| match output {
|
||||
ToolOutput::Text(text) => nested::noted(text).map(str::to_string),
|
||||
_ => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Reads `file` and appends it to the result answering `call_id`, telling the person it did.
|
||||
async fn attach_one(
|
||||
call_id: &str,
|
||||
file: &NestedFile,
|
||||
results: &mut [ContentBlock],
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) {
|
||||
let Some(injected) = super::prompt::read_context_file(&file.path) else {
|
||||
return;
|
||||
};
|
||||
let Some(content) = output_of(results, call_id) else {
|
||||
return;
|
||||
};
|
||||
let block = crate::redact::process_secrets().redact(&nested::note(&file.shown, &injected.text));
|
||||
content.push(ToolOutput::Text(block));
|
||||
debug!(file = %file.shown, "nested instructions joined the conversation");
|
||||
let _ = event_tx.send(Event::Notice(format!("Following {}", file.shown))).await;
|
||||
if let Some(notice) = injected.notice {
|
||||
let _ = event_tx.send(Event::Notice(notice)).await;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,23 @@
|
||||
use super::*;
|
||||
|
||||
impl Engine {
|
||||
/// Starts whichever project read `action` asks for. Each is synchronous and spawns, so the
|
||||
/// loop can take it mid-turn too.
|
||||
pub(crate) fn handle_project_read(&self, action: Action, event_tx: &mpsc::Sender<Event>) {
|
||||
match action {
|
||||
Action::LoadFileDiff { request_id, path } => {
|
||||
self.handle_load_file_diff(request_id, path, event_tx);
|
||||
}
|
||||
Action::ListDirectory { request_id, path, include_ignored } => {
|
||||
self.handle_list_directory(request_id, path, include_ignored, event_tx);
|
||||
}
|
||||
Action::ReadProjectFile { request_id, path } => {
|
||||
self.handle_read_project_file(request_id, path, event_tx);
|
||||
}
|
||||
other => debug!(?other, "not a project read"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Captures one file's change and reports it, without touching review state.
|
||||
///
|
||||
/// Detached rather than awaited: a glance must never make the runtime wait on git, and the
|
||||
|
||||
@@ -297,11 +297,11 @@ fn detect_git_branch(cwd: &std::path::Path) -> Option<crate::context::GitStatus>
|
||||
const MAX_CONTEXT_FILE_CHARS: usize = 24_000;
|
||||
|
||||
/// One instruction file, as the prompt will carry it.
|
||||
struct Injected {
|
||||
pub(super) struct Injected {
|
||||
/// The text that goes into the prompt.
|
||||
text: String,
|
||||
pub(super) text: String,
|
||||
/// What the person who owns the file should know about it, if anything.
|
||||
notice: Option<String>,
|
||||
pub(super) notice: Option<String>,
|
||||
/// Whether the ceiling cut it, which `/context` reports as its own finding.
|
||||
truncated: bool,
|
||||
}
|
||||
@@ -312,7 +312,7 @@ struct Injected {
|
||||
/// working from half a file without knowing it is the failure worth preventing. The **user** gets
|
||||
/// the returned notice, because they are the only one who can fix it — silent truncation is how a
|
||||
/// document grows to 43,000 characters with nobody noticing that a third of it never arrived.
|
||||
fn read_context_file(path: &std::path::Path) -> Option<Injected> {
|
||||
pub(super) fn read_context_file(path: &std::path::Path) -> Option<Injected> {
|
||||
let content = std::fs::read_to_string(path).ok()?;
|
||||
let name = path.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
|
||||
|
||||
@@ -88,14 +88,16 @@ impl Engine {
|
||||
}
|
||||
};
|
||||
let sid = self.current_session_id().await;
|
||||
let entries = match self.sessions().active_path(&sid, &leaf) {
|
||||
Ok(entries) => entries,
|
||||
let read = crate::session::with_active_path(&**self.sessions(), &sid, &leaf, |entries| {
|
||||
crate::compaction::context_messages(entries)
|
||||
});
|
||||
let messages = match read {
|
||||
Ok(messages) => messages,
|
||||
Err(error) => {
|
||||
warn!(%error, "could not read which branch the model last heard of");
|
||||
return None;
|
||||
}
|
||||
};
|
||||
let messages = crate::compaction::context_messages(&entries);
|
||||
let users = messages.iter().rev().filter(|message| message.role == Role::User);
|
||||
users.flat_map(|message| message.content.iter().skip(1).rev()).find_map(|block| match block
|
||||
{
|
||||
|
||||
@@ -62,6 +62,8 @@ pub(crate) enum DeskCommand {
|
||||
RecordProgress { review_id: ReviewId, progress: ReviewProgress },
|
||||
/// Remember what the walked chapters covered, so the next review leads with what is new.
|
||||
RecordSeen { review_id: ReviewId },
|
||||
/// A front end asked for the open review, answered under its `request_id`.
|
||||
Show { request_id: ReviewLoadRequestId },
|
||||
/// A submission crossed into its turn.
|
||||
Dispatched(SubmissionId),
|
||||
/// A question's turn answered. Held for the completion it always precedes.
|
||||
@@ -261,6 +263,7 @@ impl ReviewDesk {
|
||||
|session: &mut _| mark_posted(session, &finding_ids, &comment, outcome);
|
||||
self.lifecycle("mark posted", posted).await
|
||||
}
|
||||
DeskCommand::Show { request_id } => vec![self.shown(request_id)],
|
||||
DeskCommand::Snapshot(reply) => {
|
||||
let open = self.review.clone().zip(self.bound());
|
||||
let _ = reply.send(open);
|
||||
@@ -278,6 +281,15 @@ impl ReviewDesk {
|
||||
}
|
||||
}
|
||||
|
||||
/// The open review and its session as the desk holds them, or neither.
|
||||
fn shown(&self, request_id: ReviewLoadRequestId) -> Effect {
|
||||
let (review, session) = self.review.clone().zip(self.bound()).unzip();
|
||||
let reading = review.as_ref().map(crate::guided_review::chapter_readings);
|
||||
let reading = reading.unwrap_or_default();
|
||||
let open = crate::event::OpenReview { request_id, review, session, reading };
|
||||
Effect::Event(Event::OpenReviewShown(Box::new(open)))
|
||||
}
|
||||
|
||||
/// Puts the Guide's chapters on the held review, when it is still the revision they describe.
|
||||
///
|
||||
/// Only ever an upgrade: a pass that wrote no brief must not take one away.
|
||||
|
||||
@@ -28,6 +28,7 @@ impl crate::engine::Engine {
|
||||
DeskCommand::RecordProgress { review_id, progress }
|
||||
}
|
||||
Action::RecordReviewSeen { review_id } => DeskCommand::RecordSeen { review_id },
|
||||
Action::ShowOpenReview { request_id } => DeskCommand::Show { request_id },
|
||||
other => {
|
||||
debug!(?other, "not a review desk action");
|
||||
return;
|
||||
|
||||
@@ -135,3 +135,43 @@ async fn a_guide_upgrade_reaches_the_review_the_desk_holds() {
|
||||
edit(&mut desk, ReviewEdit::Comment { draft: on_chapter, send: false }).await;
|
||||
assert_eq!(saved_for(&effects, &request_id).findings.len(), 1);
|
||||
}
|
||||
|
||||
/// What `show_open_review` answers, pulled out of the desk's effects.
|
||||
async fn shown(desk: &mut ReviewDesk) -> crate::event::OpenReview {
|
||||
let asked = ReviewLoadRequestId::new();
|
||||
let mut effects = desk.handle(DeskCommand::Show { request_id: asked.clone() }).await;
|
||||
let Some(Effect::Event(Event::OpenReviewShown(open))) = effects.pop() else {
|
||||
panic!("show answers with open_review_shown");
|
||||
};
|
||||
assert_eq!(open.request_id, asked, "the answer carries the request it answers");
|
||||
*open
|
||||
}
|
||||
|
||||
/// With no review open, the answer says so rather than staying silent.
|
||||
///
|
||||
/// The break this catches: a desk that answers only when a review is open, which leaves a joining
|
||||
/// client waiting forever.
|
||||
#[tokio::test]
|
||||
async fn showing_with_nothing_open_answers_with_neither() {
|
||||
let mut desk = ReviewDesk::new(None);
|
||||
let open = shown(&mut desk).await;
|
||||
assert!(open.review.is_none() && open.session.is_none());
|
||||
}
|
||||
|
||||
/// A joining client gets the open review with the reading done so far, so it continues the
|
||||
/// review rather than starting over.
|
||||
///
|
||||
/// The break this catches: answering with the review but a fresh session, which drops the chapters
|
||||
/// another client already walked.
|
||||
#[tokio::test]
|
||||
async fn showing_carries_the_open_review_and_its_walked_chapters() {
|
||||
let review = review_from(TWO_FILES);
|
||||
let (_project, mut desk) = desk_with(&review).await;
|
||||
let walked = vec![review.chapters[0].id.clone()];
|
||||
let progress = ReviewProgress { walked_chapter_ids: walked.clone(), ..Default::default() };
|
||||
desk.handle(DeskCommand::RecordProgress { review_id: review.id.clone(), progress }).await;
|
||||
let open = shown(&mut desk).await;
|
||||
assert_eq!(open.reading, crate::guided_review::chapter_readings(&review));
|
||||
assert_eq!(open.review.map(|it| it.id), Some(review.id));
|
||||
assert_eq!(open.session.map(|it| it.progress.walked_chapter_ids), Some(walked));
|
||||
}
|
||||
|
||||
@@ -143,15 +143,9 @@ impl Engine {
|
||||
)
|
||||
.await;
|
||||
}
|
||||
Action::LoadFileDiff { request_id, path } => {
|
||||
self.handle_load_file_diff(request_id, path, &event_tx);
|
||||
}
|
||||
Action::ListDirectory { request_id, path, include_ignored } => {
|
||||
self.handle_list_directory(request_id, path, include_ignored, &event_tx);
|
||||
}
|
||||
Action::ReadProjectFile { request_id, path } => {
|
||||
self.handle_read_project_file(request_id, path, &event_tx);
|
||||
}
|
||||
Action::LoadFileDiff { .. }
|
||||
| Action::ListDirectory { .. }
|
||||
| Action::ReadProjectFile { .. } => self.handle_project_read(action, &event_tx),
|
||||
Action::RefreshGuidedReview { request_id, review_id, expected_revision } => {
|
||||
self.handle_refresh_guided_review(
|
||||
request_id,
|
||||
@@ -186,7 +180,8 @@ impl Engine {
|
||||
}
|
||||
Action::EditReview { .. }
|
||||
| Action::RecordReviewProgress { .. }
|
||||
| Action::RecordReviewSeen { .. } => self.forward_to_review_desk(action),
|
||||
| Action::RecordReviewSeen { .. }
|
||||
| Action::ShowOpenReview { .. } => self.forward_to_review_desk(action),
|
||||
Action::CommitReviewChapters {
|
||||
request_id,
|
||||
review_id,
|
||||
|
||||
@@ -31,9 +31,10 @@ impl Engine {
|
||||
/// retry grows the transcript by one identical message every time it is attempted. That entry
|
||||
/// is already history, so a re-run's context is not added to it.
|
||||
///
|
||||
/// A message Luxe composed — review feedback, a question from a review, a draft's brief — is
|
||||
/// announced with `MessageCommitted`, because no front-end typed it and so none has shown it.
|
||||
/// The reader's own words are not: their front-end put them on screen when they were sent.
|
||||
/// Every committed message is announced with `MessageCommitted`, the reader's own words too:
|
||||
/// a client joined to the session didn't type them, and the front-end that did reconciles the
|
||||
/// copy it already shows. A message Luxe composed (review feedback, a draft's brief) has no
|
||||
/// such copy anywhere, so it's announced the same way.
|
||||
pub(super) async fn commit_user_message(
|
||||
&self,
|
||||
user_message: &str,
|
||||
@@ -68,16 +69,14 @@ impl Engine {
|
||||
message.content.extend(context.and_then(hook_context_block));
|
||||
let entry_id =
|
||||
self.sessions_mut().append(&sid, parent, EntryKind::Message(message))?;
|
||||
// Only the brief itself: what a hook added rides along for the model, not the reader
|
||||
if remediation.is_some() || self.read_only.is_some() {
|
||||
let _ = event_tx
|
||||
.send(Event::MessageCommitted {
|
||||
session_id: sid.to_string(),
|
||||
entry_id: entry_id.clone(),
|
||||
message: Message::user(user_message),
|
||||
})
|
||||
.await;
|
||||
}
|
||||
// Only what was sent: what a hook added rides along for the model, not the reader
|
||||
let _ = event_tx
|
||||
.send(Event::MessageCommitted {
|
||||
session_id: sid.to_string(),
|
||||
entry_id: entry_id.clone(),
|
||||
message: user_message_with(user_message, images),
|
||||
})
|
||||
.await;
|
||||
entry_id
|
||||
}
|
||||
};
|
||||
@@ -205,6 +204,7 @@ impl Engine {
|
||||
None => results.push(self.dispatch_one(call, event_tx).await),
|
||||
}
|
||||
}
|
||||
self.attach_nested_instructions(&leaf, tool_calls, &mut results, event_tx).await;
|
||||
self.commit_tool_message(leaf, Message { role: Role::Tool, content: results }, event_tx)
|
||||
.await
|
||||
}
|
||||
|
||||
@@ -16,18 +16,7 @@ impl Engine {
|
||||
pass: IterationPass,
|
||||
) -> Result<CompletionRequest, EngineError> {
|
||||
let sid = self.current_session_id().await;
|
||||
let entries = self.sessions().active_path(&sid, leaf)?;
|
||||
// The path holds every entry ever written, including the ones a compaction replaced — it
|
||||
// is append-only, so the summary is a new entry rather than a deletion. `context_messages`
|
||||
// applies that cut. Reasoning blocks stay in the history verbatim; each provider adapter
|
||||
// decides how to replay them (Anthropic must resend signed thinking during a tool-use
|
||||
// turn; the OpenAI paths drop it). Stripping here would break Anthropic's requirement.
|
||||
let mut messages = crate::compaction::context_messages(&entries);
|
||||
// A summary that replaced an `activate_tools` call would unload the tool it loaded on any
|
||||
// adapter that loads tools in place, so the outgoing copy restates it right after the
|
||||
// summary. Before the scrub and the pairing repair, which then see an ordinary pair.
|
||||
crate::tool_activation::restore_after_compaction(&entries, &mut messages);
|
||||
let activated = crate::tool_activation::activated_in(&entries);
|
||||
let (mut messages, activated) = self.outgoing_transcript(&sid, leaf)?;
|
||||
// Two shapes a transcript can reach here in that no provider will take, both of them a
|
||||
// hard 400 that replays on every later request — so they end the session rather than just
|
||||
// the turn. Fixed on this copy only, in the order that leaves the result valid: drop what
|
||||
@@ -67,20 +56,20 @@ impl Engine {
|
||||
// is, because eliding rewrites the middle of the transcript and re-bills everything after
|
||||
// it as a cache write — worth paying for runway, never worth paying for tidiness.
|
||||
let settings = &self.config.compaction_settings;
|
||||
crate::compaction::prune_tool_outputs(
|
||||
&mut messages,
|
||||
settings,
|
||||
crate::compaction::ContextPressure::measure(
|
||||
&entries,
|
||||
system.as_deref(),
|
||||
&tools,
|
||||
crate::compaction::usable_context_window(
|
||||
u64::from(model.context_window),
|
||||
model.max_tokens,
|
||||
settings.reserve_tokens,
|
||||
),
|
||||
),
|
||||
let usable = crate::compaction::usable_context_window(
|
||||
u64::from(model.context_window),
|
||||
model.max_tokens,
|
||||
settings.reserve_tokens,
|
||||
);
|
||||
let (pressure, prompt_tokens) =
|
||||
crate::session::with_active_path(&**self.sessions(), &sid, leaf, |entries| {
|
||||
let system = system.as_deref();
|
||||
(
|
||||
crate::compaction::ContextPressure::measure(entries, system, &tools, usable),
|
||||
crate::compaction::prompt_tokens(entries, system, &tools),
|
||||
)
|
||||
})?;
|
||||
crate::compaction::prune_tool_outputs(&mut messages, settings, pressure);
|
||||
// What this request spends before a word of conversation. Recorded here because this is
|
||||
// the only place the system prompt and the resolved tool block exist together, and
|
||||
// compaction needs it to know how much window the kept tail is really sharing.
|
||||
@@ -93,7 +82,7 @@ impl Engine {
|
||||
// endpoint and a silent truncation everywhere else.
|
||||
let max_tokens = crate::compaction::clamp_max_tokens_to_context(
|
||||
model.context_window,
|
||||
crate::compaction::prompt_tokens(&entries, system.as_deref(), &tools),
|
||||
prompt_tokens,
|
||||
model.max_tokens,
|
||||
);
|
||||
Ok(CompletionRequest {
|
||||
@@ -114,6 +103,29 @@ impl Engine {
|
||||
})
|
||||
}
|
||||
|
||||
/// The conversation a request carries, and the deferred tools it has loaded.
|
||||
///
|
||||
/// Borrows the path rather than cloning it: it holds every entry ever written, including the
|
||||
/// ones a compaction replaced (it is append-only, so the summary is a new entry rather than a
|
||||
/// deletion), and only what `context_messages` keeps after that cut is copied. Reasoning blocks
|
||||
/// stay verbatim; each provider adapter decides how to replay them (Anthropic must resend
|
||||
/// signed thinking during a tool-use turn; the OpenAI paths drop it).
|
||||
fn outgoing_transcript(
|
||||
&self,
|
||||
sid: &SessionId,
|
||||
leaf: &EntryId,
|
||||
) -> Result<(Vec<Message>, std::collections::BTreeSet<String>), EngineError> {
|
||||
let read = crate::session::with_active_path(&**self.sessions(), sid, leaf, |entries| {
|
||||
let mut messages = crate::compaction::context_messages(entries);
|
||||
// A summary that replaced an `activate_tools` call would unload the tool it loaded on
|
||||
// an adapter that loads tools in place, so the outgoing copy restates it right after
|
||||
// the summary. Before the scrub and the pairing repair, which then see an ordinary pair.
|
||||
crate::tool_activation::restore_after_compaction(entries, &mut messages);
|
||||
(messages, crate::tool_activation::activated_in(entries))
|
||||
})?;
|
||||
Ok(read)
|
||||
}
|
||||
|
||||
/// Opens the provider stream for one iteration, making room and trying once more when the
|
||||
/// prompt turns out not to fit.
|
||||
///
|
||||
|
||||
@@ -68,6 +68,23 @@ impl Handover {
|
||||
self.closed.extend(reply.tool_calls.iter().map(|call| call.index));
|
||||
}
|
||||
|
||||
/// Notes the calls `event` shows the model has finished writing. Returns whether it closes any.
|
||||
fn note_closing(&mut self, event: &ProviderEvent, reply: &StreamedReply) -> bool {
|
||||
match event {
|
||||
ProviderEvent::ToolCallEnd { index } => {
|
||||
self.closed.insert(*index);
|
||||
true
|
||||
}
|
||||
ProviderEvent::ToolCallStart { .. }
|
||||
| ProviderEvent::TextDelta { .. }
|
||||
| ProviderEvent::ThinkingDelta { .. } => {
|
||||
self.close_all(reply);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Hands over every newly runnable call, in order. Returns whether anything moved.
|
||||
fn advance(&mut self, reply: &StreamedReply) -> bool {
|
||||
let Some(to_run) = self.to_run.as_ref() else { return false };
|
||||
@@ -109,30 +126,46 @@ fn runs_early(call: &ToolCallInfo) -> bool {
|
||||
/// Anything opening after a call means the model has finished writing it, and so does its own
|
||||
/// end. Handed over before the next card is announced, and with a yield so the call is running by
|
||||
/// the time it is: otherwise the finished card flashes "queued" for a frame on its way from being
|
||||
/// written to running.
|
||||
async fn close_written_calls(
|
||||
/// written to running. Each card hears the whole call first.
|
||||
///
|
||||
/// Args: `(event, handover, reply, composed, event_tx)`.
|
||||
async fn close_written(
|
||||
event: &ProviderEvent,
|
||||
handover: &mut Handover,
|
||||
reply: &StreamedReply,
|
||||
composed: &mut HashMap<usize, Composed>,
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) {
|
||||
let closes = match event {
|
||||
ProviderEvent::ToolCallEnd { index } => {
|
||||
handover.closed.insert(*index);
|
||||
true
|
||||
}
|
||||
ProviderEvent::ToolCallStart { .. }
|
||||
| ProviderEvent::TextDelta { .. }
|
||||
| ProviderEvent::ThinkingDelta { .. } => {
|
||||
handover.close_all(reply);
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
if closes && handover.advance(reply) {
|
||||
if !handover.note_closing(event, reply) {
|
||||
return;
|
||||
}
|
||||
finish_composing(composed, &handover.closed, reply, event_tx).await;
|
||||
if handover.advance(reply) {
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends each card the model has stopped writing what the cadence held back, and stops watching it.
|
||||
async fn finish_composing(
|
||||
composed: &mut HashMap<usize, Composed>,
|
||||
closed: &std::collections::HashSet<usize>,
|
||||
reply: &StreamedReply,
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) {
|
||||
for call in reply.tool_calls.iter().filter(|call| closed.contains(&call.index)) {
|
||||
let Some(mut state) = composed.remove(&call.index) else { continue };
|
||||
let Some(frame) = state.last_frame(&call.name, &call.arguments) else { continue };
|
||||
let _ = event_tx
|
||||
.send(Event::ToolCallComposing {
|
||||
call_id: call.id.clone(),
|
||||
name: call.name.clone(),
|
||||
subject: frame.subject,
|
||||
body: frame.body,
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens a card for a call the model has started writing, when it is one worth watching arrive.
|
||||
///
|
||||
/// A front-end opens the card here rather than at dispatch, so a call the model is still writing
|
||||
@@ -258,7 +291,7 @@ impl Engine {
|
||||
let mut composed: HashMap<usize, Composed> = HashMap::new();
|
||||
|
||||
while let Some(provider_event) = stream.next().await {
|
||||
close_written_calls(&provider_event, &mut handover, &reply).await;
|
||||
close_written(&provider_event, &mut handover, &reply, &mut composed, event_tx).await;
|
||||
match provider_event {
|
||||
ProviderEvent::TextDelta { delta, .. } => {
|
||||
if !text_started {
|
||||
@@ -368,149 +401,4 @@ impl Engine {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Copied from a real gpt-6.1-sol reply that went on like this for 128k tokens.
|
||||
#[test]
|
||||
fn a_blank_run_with_line_breaks_runs_away_and_indentation_does_not() {
|
||||
let stuck = format!("{{\"subjects\":[\"a\"] \t\n \r\n{}", " \n".repeat(140));
|
||||
assert!(runs_away(&stuck));
|
||||
assert!(!runs_away(&stuck[..stuck.len() - 200]), "not yet that long");
|
||||
let pretty = format!("{{\n{}\"n\": 1\n}}", " ".repeat(40));
|
||||
assert!(!runs_away(&pretty));
|
||||
// Spaces alone can sit inside a string, where they may be the content
|
||||
let padded = format!("{{\"text\":\"{}", " ".repeat(1000));
|
||||
assert!(!runs_away(&padded));
|
||||
}
|
||||
|
||||
fn call(index: usize, name: &str, arguments: &str) -> ToolCallInfo {
|
||||
ToolCallInfo {
|
||||
index,
|
||||
id: format!("call-{index}"),
|
||||
name: name.into(),
|
||||
arguments: arguments.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn reply(calls: Vec<ToolCallInfo>) -> StreamedReply {
|
||||
StreamedReply {
|
||||
text: String::new(),
|
||||
thinking: HashMap::new(),
|
||||
order: Vec::new(),
|
||||
tool_calls: calls,
|
||||
stop_reason: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reasoning that is only an opaque token still commits, because it is replayed: Copilot's
|
||||
/// Gemini sends its state with no readable text at all.
|
||||
#[test]
|
||||
fn opaque_reasoning_commits_without_text() {
|
||||
let mut reply = reply(Vec::new());
|
||||
reply.note_thinking_block(0).opaque = Some("CpcBAb4+9vs".into());
|
||||
let blocks = reply.into_blocks();
|
||||
assert!(matches!(
|
||||
blocks.as_slice(),
|
||||
[ContentBlock::Thinking { text, opaque: Some(opaque), .. }]
|
||||
if text.is_empty() && opaque == "CpcBAb4+9vs"
|
||||
));
|
||||
}
|
||||
|
||||
/// A call is handed over once it is closed, in order; the first call that has to wait makes
|
||||
/// every later one wait, so nothing runs ahead of a call written before it.
|
||||
#[test]
|
||||
fn calls_are_handed_over_in_order_once_closed() {
|
||||
let (to_run, mut handed) = mpsc::unbounded_channel();
|
||||
let mut handover = Handover::new(Some(to_run));
|
||||
let mut reply = reply(vec![call(0, "edit", r#"{"path":"a"}"#)]);
|
||||
assert!(!handover.advance(&reply), "still being written");
|
||||
|
||||
reply.tool_calls.push(call(1, "bash", r#"{"command":"#));
|
||||
handover.close_all(&reply);
|
||||
reply.tool_calls[1].arguments.push_str(r#""ls"}"#);
|
||||
assert!(handover.advance(&reply), "a later block closed both");
|
||||
assert_eq!(handed.try_recv().map(|call| call.index), Ok(0));
|
||||
assert_eq!(handed.try_recv().map(|call| call.index), Ok(1));
|
||||
|
||||
reply.tool_calls.push(call(2, "read", r#"{"path":"b"}"#));
|
||||
reply.tool_calls.push(call(3, "test", ""));
|
||||
handover.close_all(&reply);
|
||||
assert!(!handover.advance(&reply), "a lookup waits for the message");
|
||||
assert!(handed.try_recv().is_err(), "and so does the call after it");
|
||||
}
|
||||
|
||||
/// Only a call that is whole, named, and not a lookup runs early.
|
||||
#[test]
|
||||
fn what_may_run_early() {
|
||||
assert!(runs_early(&call(0, "edit", r#"{"path":"a"}"#)));
|
||||
assert!(runs_early(&call(0, "test", "")), "closed with no arguments takes none");
|
||||
assert!(!runs_early(&call(0, "edit", r#"{"path":"#)), "cut off mid-argument");
|
||||
assert!(!runs_early(&call(0, "edit", "[1]")), "arguments are an object");
|
||||
assert!(!runs_early(&call(0, "grep", r#"{"pattern":"x"}"#)), "a lookup folds");
|
||||
let mut nameless = call(0, "edit", "{}");
|
||||
nameless.id.clear();
|
||||
assert!(!runs_early(&nameless), "half an identity");
|
||||
}
|
||||
|
||||
/// A wrap-up turn hands nothing over, however finished its calls are.
|
||||
#[test]
|
||||
fn nothing_is_handed_over_without_somewhere_to_run_it() {
|
||||
let mut handover = Handover::new(None);
|
||||
let reply = reply(vec![call(0, "edit", "{}")]);
|
||||
handover.close_all(&reply);
|
||||
assert!(!handover.advance(&reply));
|
||||
}
|
||||
|
||||
/// A lookup never composes: three adjacent ones fold into a single line, declared by the
|
||||
/// committed message, so a card opened early is a row the fold then has to take back.
|
||||
#[test]
|
||||
fn only_a_call_worth_watching_arrive_opens_a_card_early() {
|
||||
for tool in ["write", "edit", "bash", "task", "screenshot"] {
|
||||
assert!(composes("call-1", tool), "{tool} should open its card early");
|
||||
}
|
||||
for tool in ["read", "grep", "glob", "symbol_search", crate::tool::ACTIVATE_TOOLS_NAME] {
|
||||
assert!(!composes("call-1", tool), "{tool} must wait for dispatch");
|
||||
}
|
||||
// Half an identity cannot be correlated with the call that follows it, so it keeps the old
|
||||
// behavior and gets its card at `ToolStarted`.
|
||||
assert!(!composes("", "write"));
|
||||
assert!(!composes("call-1", ""));
|
||||
}
|
||||
|
||||
/// The subject is worth interrupting the cadence for — one small string, sent once, and it is
|
||||
/// what turns a bare spinner into a card. The payload is resent whole each time, so it waits
|
||||
/// its turn rather than spending the channel on frames nobody draws.
|
||||
#[test]
|
||||
fn the_payload_is_rate_limited_and_the_name_is_not() {
|
||||
let start = std::time::Instant::now();
|
||||
let mut state = Composed::default();
|
||||
let frame = |state: &mut Composed, after_ms: u64, arguments: &str| {
|
||||
state.next_frame(start + std::time::Duration::from_millis(after_ms), "write", arguments)
|
||||
};
|
||||
|
||||
assert!(frame(&mut state, 0, "{\"path\":\"a.rs").is_none(), "nothing has closed yet");
|
||||
|
||||
// The path closes: named at once, carrying whatever of the file has landed — there is no
|
||||
// cadence to respect until something has actually gone out.
|
||||
let named =
|
||||
frame(&mut state, 1, "{\"path\":\"a.rs\",\"content\":\"one\\n").expect("a frame");
|
||||
assert_eq!(named.subject.as_deref(), Some("a.rs"));
|
||||
assert_eq!(named.body.map(|body| body.total_lines), Some(1));
|
||||
|
||||
// Inside the window the file keeps arriving and the channel stays quiet.
|
||||
assert!(frame(&mut state, 50, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
|
||||
|
||||
let caught_up = frame(&mut state, 200, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n")
|
||||
.expect("the window has passed");
|
||||
assert!(caught_up.subject.is_none(), "the name cannot improve, so it is not resent");
|
||||
assert_eq!(caught_up.body.map(|body| body.total_lines), Some(2), "and it caught up");
|
||||
|
||||
// A payload that has not grown is not a frame, however long it has been.
|
||||
assert!(frame(&mut state, 900, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
|
||||
let grown =
|
||||
frame(&mut state, 1_000, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\nthree\\n")
|
||||
.expect("a third line");
|
||||
assert_eq!(grown.body.map(|body| body.total_lines), Some(3));
|
||||
}
|
||||
}
|
||||
mod tests;
|
||||
|
||||
@@ -57,9 +57,11 @@ pub(super) fn composes(id: &str, name: &str) -> bool {
|
||||
/// What a front-end has already been told about a call the model is still writing.
|
||||
#[derive(Default)]
|
||||
pub(super) struct Composed {
|
||||
/// Whether the subject has been sent. It cannot improve, and a header that rewrote itself as
|
||||
/// more arrived would read as a glitch rather than as progress.
|
||||
named: bool,
|
||||
/// The subject last sent. A later member can improve it: `bash` is named by its command until
|
||||
/// the `description` written after it arrives, and the card has to carry that before it runs,
|
||||
/// since dispatch brings no arguments with it. Members only count once whole, so it changes a
|
||||
/// handful of times at most, never character by character.
|
||||
named: Option<String>,
|
||||
/// Bytes and lines of the payload last sent, so a body that has not grown is not resent. Both,
|
||||
/// because the text is capped and stops growing while the line count it reports does not.
|
||||
sent: (usize, usize),
|
||||
@@ -84,24 +86,53 @@ impl Composed {
|
||||
name: &str,
|
||||
arguments: &str,
|
||||
) -> Option<ComposingFrame> {
|
||||
let cwd = crate::ToolContext::working_dir; // asked only when a frame is built
|
||||
let subject = if self.named { None } else { describe_partial_call(name, arguments, cwd()) };
|
||||
// The subject is worth interrupting the cadence for — it is one small string, sent once,
|
||||
// and it is what turns a bare spinner into a card. The body waits its turn.
|
||||
let due = self.at.is_none_or(|at| now.duration_since(at) >= COMPOSING_FRAME);
|
||||
let body = due
|
||||
.then(|| partial_card_body(name, arguments, cwd()))
|
||||
.flatten()
|
||||
.filter(|body| (body.text.len(), body.total_lines) > self.sent);
|
||||
// The first subject is worth interrupting the cadence for: it is what turns a bare spinner
|
||||
// into a card. A better one waits its turn with the body, since finding it means parsing
|
||||
// the whole argument, and on every fragment of a long `write` that adds up.
|
||||
let frame = self.frame(name, arguments, self.named.is_none() || due, due)?;
|
||||
self.at = Some(now);
|
||||
Some(frame)
|
||||
}
|
||||
|
||||
/// Whatever the cadence held back, for a call the model has finished writing.
|
||||
///
|
||||
/// Sent before the call is handed over to run, because nothing after that brings its
|
||||
/// arguments: a `bash` whose `description` closed inside the last window would otherwise run
|
||||
/// under its command until the message commits, after the call is over.
|
||||
pub(super) fn last_frame(&mut self, name: &str, arguments: &str) -> Option<ComposingFrame> {
|
||||
self.frame(name, arguments, true, true)
|
||||
}
|
||||
|
||||
/// The subject and body that changed since they were last sent, looking only where asked.
|
||||
fn frame(
|
||||
&mut self,
|
||||
name: &str,
|
||||
arguments: &str,
|
||||
rename: bool,
|
||||
refill: bool,
|
||||
) -> Option<ComposingFrame> {
|
||||
let subject = rename.then(|| self.new_subject(name, arguments)).flatten();
|
||||
let body = refill.then(|| self.grown_body(name, arguments)).flatten();
|
||||
if subject.is_none() && body.is_none() {
|
||||
return None;
|
||||
}
|
||||
self.named |= subject.is_some();
|
||||
if let Some(subject) = &subject {
|
||||
self.named = Some(subject.clone());
|
||||
}
|
||||
if let Some(body) = &body {
|
||||
self.sent = (body.text.len(), body.total_lines);
|
||||
}
|
||||
self.at = Some(now);
|
||||
Some(ComposingFrame { subject, body })
|
||||
}
|
||||
|
||||
fn new_subject(&self, name: &str, arguments: &str) -> Option<String> {
|
||||
describe_partial_call(name, arguments, crate::ToolContext::working_dir())
|
||||
.filter(|subject| self.named.as_ref() != Some(subject))
|
||||
}
|
||||
|
||||
fn grown_body(&self, name: &str, arguments: &str) -> Option<crate::activity::ToolCardBody> {
|
||||
partial_card_body(name, arguments, crate::ToolContext::working_dir())
|
||||
.filter(|body| (body.text.len(), body.total_lines) > self.sent)
|
||||
}
|
||||
}
|
||||
|
||||
191
crates/luxe-core/src/engine/turn/stream/tests.rs
Normal file
191
crates/luxe-core/src/engine/turn/stream/tests.rs
Normal file
@@ -0,0 +1,191 @@
|
||||
use super::*;
|
||||
|
||||
/// Copied from a real gpt-6.1-sol reply that went on like this for 128k tokens.
|
||||
#[test]
|
||||
fn a_blank_run_with_line_breaks_runs_away_and_indentation_does_not() {
|
||||
let stuck = format!("{{\"subjects\":[\"a\"] \t\n \r\n{}", " \n".repeat(140));
|
||||
assert!(runs_away(&stuck));
|
||||
assert!(!runs_away(&stuck[..stuck.len() - 200]), "not yet that long");
|
||||
let pretty = format!("{{\n{}\"n\": 1\n}}", " ".repeat(40));
|
||||
assert!(!runs_away(&pretty));
|
||||
// Spaces alone can sit inside a string, where they may be the content
|
||||
let padded = format!("{{\"text\":\"{}", " ".repeat(1000));
|
||||
assert!(!runs_away(&padded));
|
||||
}
|
||||
|
||||
fn call(index: usize, name: &str, arguments: &str) -> ToolCallInfo {
|
||||
ToolCallInfo {
|
||||
index,
|
||||
id: format!("call-{index}"),
|
||||
name: name.into(),
|
||||
arguments: arguments.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn reply(calls: Vec<ToolCallInfo>) -> StreamedReply {
|
||||
StreamedReply {
|
||||
text: String::new(),
|
||||
thinking: HashMap::new(),
|
||||
order: Vec::new(),
|
||||
tool_calls: calls,
|
||||
stop_reason: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Reasoning that is only an opaque token still commits, because it is replayed: Copilot's
|
||||
/// Gemini sends its state with no readable text at all.
|
||||
#[test]
|
||||
fn opaque_reasoning_commits_without_text() {
|
||||
let mut reply = reply(Vec::new());
|
||||
reply.note_thinking_block(0).opaque = Some("CpcBAb4+9vs".into());
|
||||
let blocks = reply.into_blocks();
|
||||
assert!(matches!(
|
||||
blocks.as_slice(),
|
||||
[ContentBlock::Thinking { text, opaque: Some(opaque), .. }]
|
||||
if text.is_empty() && opaque == "CpcBAb4+9vs"
|
||||
));
|
||||
}
|
||||
|
||||
/// A call is handed over once it is closed, in order; the first call that has to wait makes
|
||||
/// every later one wait, so nothing runs ahead of a call written before it.
|
||||
#[test]
|
||||
fn calls_are_handed_over_in_order_once_closed() {
|
||||
let (to_run, mut handed) = mpsc::unbounded_channel();
|
||||
let mut handover = Handover::new(Some(to_run));
|
||||
let mut reply = reply(vec![call(0, "edit", r#"{"path":"a"}"#)]);
|
||||
assert!(!handover.advance(&reply), "still being written");
|
||||
|
||||
reply.tool_calls.push(call(1, "bash", r#"{"command":"#));
|
||||
handover.close_all(&reply);
|
||||
reply.tool_calls[1].arguments.push_str(r#""ls"}"#);
|
||||
assert!(handover.advance(&reply), "a later block closed both");
|
||||
assert_eq!(handed.try_recv().map(|call| call.index), Ok(0));
|
||||
assert_eq!(handed.try_recv().map(|call| call.index), Ok(1));
|
||||
|
||||
reply.tool_calls.push(call(2, "read", r#"{"path":"b"}"#));
|
||||
reply.tool_calls.push(call(3, "test", ""));
|
||||
handover.close_all(&reply);
|
||||
assert!(!handover.advance(&reply), "a lookup waits for the message");
|
||||
assert!(handed.try_recv().is_err(), "and so does the call after it");
|
||||
}
|
||||
|
||||
/// Only a call that is whole, named, and not a lookup runs early.
|
||||
#[test]
|
||||
fn what_may_run_early() {
|
||||
assert!(runs_early(&call(0, "edit", r#"{"path":"a"}"#)));
|
||||
assert!(runs_early(&call(0, "test", "")), "closed with no arguments takes none");
|
||||
assert!(!runs_early(&call(0, "edit", r#"{"path":"#)), "cut off mid-argument");
|
||||
assert!(!runs_early(&call(0, "edit", "[1]")), "arguments are an object");
|
||||
assert!(!runs_early(&call(0, "grep", r#"{"pattern":"x"}"#)), "a lookup folds");
|
||||
let mut nameless = call(0, "edit", "{}");
|
||||
nameless.id.clear();
|
||||
assert!(!runs_early(&nameless), "half an identity");
|
||||
}
|
||||
|
||||
/// A wrap-up turn hands nothing over, however finished its calls are.
|
||||
#[test]
|
||||
fn nothing_is_handed_over_without_somewhere_to_run_it() {
|
||||
let mut handover = Handover::new(None);
|
||||
let reply = reply(vec![call(0, "edit", "{}")]);
|
||||
handover.close_all(&reply);
|
||||
assert!(!handover.advance(&reply));
|
||||
}
|
||||
|
||||
/// A lookup never composes: three adjacent ones fold into a single line, declared by the
|
||||
/// committed message, so a card opened early is a row the fold then has to take back.
|
||||
#[test]
|
||||
fn only_a_call_worth_watching_arrive_opens_a_card_early() {
|
||||
for tool in ["write", "edit", "bash", "task", "screenshot"] {
|
||||
assert!(composes("call-1", tool), "{tool} should open its card early");
|
||||
}
|
||||
for tool in ["read", "grep", "glob", "symbol_search", crate::tool::ACTIVATE_TOOLS_NAME] {
|
||||
assert!(!composes("call-1", tool), "{tool} must wait for dispatch");
|
||||
}
|
||||
// Half an identity cannot be correlated with the call that follows it, so it keeps the old
|
||||
// behavior and gets its card at `ToolStarted`.
|
||||
assert!(!composes("", "write"));
|
||||
assert!(!composes("call-1", ""));
|
||||
}
|
||||
|
||||
/// The first subject is worth interrupting the cadence for: one small string, and it is what
|
||||
/// turns a bare spinner into a card. The payload is resent whole each time, so it waits its turn
|
||||
/// rather than spending the channel on frames nobody draws.
|
||||
#[test]
|
||||
fn the_payload_is_rate_limited_and_the_name_is_not() {
|
||||
let start = std::time::Instant::now();
|
||||
let mut state = Composed::default();
|
||||
let frame = |state: &mut Composed, after_ms: u64, arguments: &str| {
|
||||
state.next_frame(start + std::time::Duration::from_millis(after_ms), "write", arguments)
|
||||
};
|
||||
|
||||
assert!(frame(&mut state, 0, "{\"path\":\"a.rs").is_none(), "nothing has closed yet");
|
||||
|
||||
// The path closes: named at once, carrying whatever of the file has landed — there is no
|
||||
// cadence to respect until something has actually gone out.
|
||||
let named = frame(&mut state, 1, "{\"path\":\"a.rs\",\"content\":\"one\\n").expect("a frame");
|
||||
assert_eq!(named.subject.as_deref(), Some("a.rs"));
|
||||
assert_eq!(named.body.map(|body| body.total_lines), Some(1));
|
||||
|
||||
// Inside the window the file keeps arriving and the channel stays quiet.
|
||||
assert!(frame(&mut state, 50, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
|
||||
|
||||
let caught_up = frame(&mut state, 200, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n")
|
||||
.expect("the window has passed");
|
||||
assert!(caught_up.subject.is_none(), "the name has not changed, so it is not resent");
|
||||
assert_eq!(caught_up.body.map(|body| body.total_lines), Some(2), "and it caught up");
|
||||
|
||||
// A payload that has not grown is not a frame, however long it has been.
|
||||
assert!(frame(&mut state, 900, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\n").is_none());
|
||||
let grown = frame(&mut state, 1_000, "{\"path\":\"a.rs\",\"content\":\"one\\ntwo\\nthree\\n")
|
||||
.expect("a third line");
|
||||
assert_eq!(grown.body.map(|body| body.total_lines), Some(3));
|
||||
}
|
||||
|
||||
const BASH_COMMAND: &str = r#"{"command":"sleep 5","#;
|
||||
const BASH_WHOLE: &str = r#"{"command":"sleep 5","description":"Wait for the build"}"#;
|
||||
|
||||
/// A `bash` is named by its command until the `description` written after it arrives, and then
|
||||
/// by that, so the card running it says what it is for.
|
||||
///
|
||||
/// The break this catches: a subject sent once and never again, which leaves the command in the
|
||||
/// title and again in the body until the call is over.
|
||||
#[test]
|
||||
fn a_later_member_renames_the_card() {
|
||||
let start = std::time::Instant::now();
|
||||
let mut state = Composed::default();
|
||||
let at = |ms| start + std::time::Duration::from_millis(ms);
|
||||
let named = state.next_frame(at(0), "bash", BASH_COMMAND).expect("the command closed");
|
||||
assert_eq!(named.subject.as_deref(), Some("sleep 5"));
|
||||
let renamed = state.next_frame(at(200), "bash", BASH_WHOLE).expect("the description closed");
|
||||
assert_eq!(renamed.subject.as_deref(), Some("Wait for the build"));
|
||||
assert!(state.next_frame(at(400), "bash", BASH_WHOLE).is_none(), "said once");
|
||||
}
|
||||
|
||||
/// A call closed inside the cadence's window still tells its card the whole call before it is
|
||||
/// handed over to run, since dispatch brings no arguments with it.
|
||||
///
|
||||
/// The break this catches: handing the call over with the frame still held back, so the card
|
||||
/// runs under its command until the message commits after the call is over.
|
||||
#[tokio::test]
|
||||
async fn a_closed_call_tells_its_card_everything_before_it_runs() {
|
||||
let start = std::time::Instant::now();
|
||||
let mut composed = HashMap::from([(0, Composed::default())]);
|
||||
let state = composed.get_mut(&0).expect("just inserted");
|
||||
state.next_frame(start, "bash", BASH_COMMAND).expect("named by the command");
|
||||
let late = start + std::time::Duration::from_millis(10);
|
||||
assert!(state.next_frame(late, "bash", BASH_WHOLE).is_none(), "held back by the cadence");
|
||||
|
||||
let (event_tx, mut events) = mpsc::channel(8);
|
||||
let (to_run, mut handed) = mpsc::unbounded_channel();
|
||||
let mut handover = Handover::new(Some(to_run));
|
||||
let reply = reply(vec![call(0, "bash", BASH_WHOLE)]);
|
||||
let end = ProviderEvent::ToolCallEnd { index: 0 };
|
||||
close_written(&end, &mut handover, &reply, &mut composed, &event_tx).await;
|
||||
|
||||
let Ok(Event::ToolCallComposing { subject, .. }) = events.try_recv() else {
|
||||
panic!("the card heard nothing before the call was handed over");
|
||||
};
|
||||
assert_eq!(subject.as_deref(), Some("Wait for the build"));
|
||||
assert_eq!(handed.try_recv().map(|call| call.index), Ok(0));
|
||||
assert!(composed.is_empty(), "a written call is no longer watched");
|
||||
}
|
||||
@@ -12,8 +12,8 @@ mod types;
|
||||
pub use action::Action;
|
||||
pub use events::Event;
|
||||
pub use types::{
|
||||
EngineState, GuidedReviewFailure, GuidedReviewFailureKind, ModelRef, RequestedReviewVerdict,
|
||||
SessionMessage, StopReason,
|
||||
EngineState, GuidedReviewFailure, GuidedReviewFailureKind, ModelChoice, ModelRef, OpenReview,
|
||||
RequestedReviewVerdict, SessionMessage, StopReason,
|
||||
};
|
||||
|
||||
/// Version of the `Action`/`Event` wire protocol, announced in [`Event::Ready`].
|
||||
|
||||
@@ -135,6 +135,13 @@ pub enum Action {
|
||||
/// The open review
|
||||
review_id: ReviewId,
|
||||
},
|
||||
/// Ask for the review open now, whoever opened it, answered by `open_review_shown` under
|
||||
/// `request_id`. How a second client joins a review instead of replacing it: loading one
|
||||
/// makes a new review, and the session holds one at a time.
|
||||
ShowOpenReview {
|
||||
/// Correlates the answer
|
||||
request_id: ReviewLoadRequestId,
|
||||
},
|
||||
/// Read the open review again, keeping its comments, if it is still at `revision_id`.
|
||||
RefreshGuidedReview {
|
||||
/// Correlates the loading, loaded and failed events
|
||||
@@ -301,6 +308,12 @@ pub enum Action {
|
||||
/// Project-relative, or an absolute path the session touched outside the project
|
||||
path: String,
|
||||
},
|
||||
/// Ask which models this client can switch to, answered by `models_listed` to this client
|
||||
/// alone.
|
||||
ListModels {
|
||||
/// Correlates the answer
|
||||
request_id: String,
|
||||
},
|
||||
/// List one project folder, answered by `directory_listed` or `directory_list_failed`.
|
||||
ListDirectory {
|
||||
/// Correlates the answer
|
||||
@@ -351,6 +364,23 @@ pub enum Action {
|
||||
},
|
||||
|
||||
// === Lifecycle ===
|
||||
/// A client that joined a running session names itself, so the interface can say who joined
|
||||
/// and who answered a prompt. Refused from the client that started the session.
|
||||
///
|
||||
/// Args: `name` — how to call it, such as the device the bridge relays for. Cosmetic: any
|
||||
/// program running as you could send it. `watch_only` — the client only reads, so the
|
||||
/// interface doesn't say it joined, and nothing it sends afterward reaches the engine. Honored
|
||||
/// only as the client's first line, so a client can't act and then go quiet.
|
||||
Introduce {
|
||||
/// How to call the client
|
||||
name: String,
|
||||
/// Reads without being announced, and can't act
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
watch_only: bool,
|
||||
/// Paired by the person at the terminal, who needn't be told their own device joined
|
||||
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
||||
yours: bool,
|
||||
},
|
||||
/// Shut the engine down, stopping a turn in flight the way `cancel` does. The event stream
|
||||
/// ends after it.
|
||||
Close,
|
||||
|
||||
@@ -35,6 +35,7 @@ impl Action {
|
||||
Self::EditReview { .. } => "edit_review",
|
||||
Self::RecordReviewProgress { .. } => "record_review_progress",
|
||||
Self::RecordReviewSeen { .. } => "record_review_seen",
|
||||
Self::ShowOpenReview { .. } => "show_open_review",
|
||||
Self::RefreshGuidedReview { .. } => "refresh_guided_review",
|
||||
Self::CommitReviewChapters { .. } => "commit_review_chapters",
|
||||
Self::PreparePullRequest { .. } => "prepare_pull_request",
|
||||
@@ -54,6 +55,7 @@ impl Action {
|
||||
Self::PlanRestack { .. } => "plan_restack",
|
||||
Self::CheckWorktreeRemoval => "check_worktree_removal",
|
||||
Self::LoadFileDiff { .. } => "load_file_diff",
|
||||
Self::ListModels { .. } => "list_models",
|
||||
Self::ListDirectory { .. } => "list_directory",
|
||||
Self::ReadProjectFile { .. } => "read_project_file",
|
||||
Self::SetFocus { .. } => "set_focus",
|
||||
@@ -61,6 +63,7 @@ impl Action {
|
||||
Self::PostGithubComment(_) => "post_github_comment",
|
||||
Self::ChangePullRequest { .. } => "change_pull_request",
|
||||
Self::Restack { .. } => "restack",
|
||||
Self::Introduce { .. } => "introduce",
|
||||
Self::Close => "close",
|
||||
// The one variant whose tag is not known until runtime, so it reports the category.
|
||||
// A caller that needs the client's actual string reads it off the variant.
|
||||
@@ -98,6 +101,7 @@ impl Action {
|
||||
"edit_review",
|
||||
"record_review_progress",
|
||||
"record_review_seen",
|
||||
"show_open_review",
|
||||
"refresh_guided_review",
|
||||
"commit_review_chapters",
|
||||
"prepare_pull_request",
|
||||
@@ -117,6 +121,7 @@ impl Action {
|
||||
"plan_restack",
|
||||
"check_worktree_removal",
|
||||
"load_file_diff",
|
||||
"list_models",
|
||||
"list_directory",
|
||||
"read_project_file",
|
||||
"set_focus",
|
||||
@@ -124,6 +129,7 @@ impl Action {
|
||||
"post_github_comment",
|
||||
"change_pull_request",
|
||||
"restack",
|
||||
"introduce",
|
||||
"close",
|
||||
];
|
||||
|
||||
@@ -175,6 +181,7 @@ impl Action {
|
||||
| Self::EditReview { .. }
|
||||
| Self::RecordReviewProgress { .. }
|
||||
| Self::RecordReviewSeen { .. }
|
||||
| Self::ShowOpenReview { .. }
|
||||
| Self::RefreshGuidedReview { .. }
|
||||
| Self::CommitReviewChapters { .. }
|
||||
| Self::PreparePullRequest { .. }
|
||||
@@ -190,10 +197,12 @@ impl Action {
|
||||
| Self::PlanRestack { .. }
|
||||
| Self::CheckWorktreeRemoval
|
||||
| Self::LoadFileDiff { .. }
|
||||
| Self::ListModels { .. }
|
||||
| Self::ListDirectory { .. }
|
||||
| Self::ReadProjectFile { .. }
|
||||
| Self::SetFocus { .. }
|
||||
| Self::LinkPullRequest { .. }
|
||||
| Self::Introduce { .. }
|
||||
| Self::Close
|
||||
| Self::Other { .. } => false,
|
||||
}
|
||||
|
||||
@@ -70,6 +70,9 @@ pub(super) enum KnownAction {
|
||||
RecordReviewSeen {
|
||||
review_id: ReviewId,
|
||||
},
|
||||
ShowOpenReview {
|
||||
request_id: ReviewLoadRequestId,
|
||||
},
|
||||
RefreshGuidedReview {
|
||||
request_id: ReviewLoadRequestId,
|
||||
review_id: ReviewId,
|
||||
@@ -158,6 +161,9 @@ pub(super) enum KnownAction {
|
||||
request_id: crate::file_diff::FileDiffRequestId,
|
||||
path: String,
|
||||
},
|
||||
ListModels {
|
||||
request_id: String,
|
||||
},
|
||||
ListDirectory {
|
||||
request_id: crate::project_files::ListingRequestId,
|
||||
#[serde(default)]
|
||||
@@ -186,5 +192,12 @@ pub(super) enum KnownAction {
|
||||
Restack {
|
||||
request_id: crate::restack::RestackId,
|
||||
},
|
||||
Introduce {
|
||||
name: String,
|
||||
#[serde(default)]
|
||||
watch_only: bool,
|
||||
#[serde(default)]
|
||||
yours: bool,
|
||||
},
|
||||
Close,
|
||||
}
|
||||
|
||||
@@ -79,9 +79,14 @@ pub(super) enum KnownEvent {
|
||||
messages: Vec<SessionMessage>,
|
||||
},
|
||||
StateChanged(EngineState),
|
||||
TurnStarted,
|
||||
TurnEnded {
|
||||
reason: StopReason,
|
||||
},
|
||||
ModelsListed {
|
||||
request_id: String,
|
||||
models: Vec<super::super::ModelChoice>,
|
||||
},
|
||||
LastTurnAvailable {
|
||||
session_id: String,
|
||||
turn_id: String,
|
||||
@@ -121,6 +126,7 @@ pub(super) enum KnownEvent {
|
||||
},
|
||||
Error(String),
|
||||
Notice(String),
|
||||
GuestsChanged(Vec<String>),
|
||||
PlanWritten {
|
||||
path: String,
|
||||
},
|
||||
@@ -327,6 +333,7 @@ pub(super) enum KnownEvent {
|
||||
ReviewSessionRestored {
|
||||
session: Box<ReviewSession>,
|
||||
},
|
||||
OpenReviewShown(Box<super::super::OpenReview>),
|
||||
ReviewSessionSaved {
|
||||
request_id: ReviewSessionSaveRequestId,
|
||||
session: Box<ReviewSession>,
|
||||
@@ -361,6 +368,9 @@ pub(super) enum KnownEvent {
|
||||
#[serde(default)]
|
||||
multi: bool,
|
||||
},
|
||||
PromptClosed {
|
||||
id: String,
|
||||
},
|
||||
Ready {
|
||||
protocol_version: u32,
|
||||
luxe_version: String,
|
||||
|
||||
@@ -53,19 +53,13 @@ pub enum Event {
|
||||
/// A call can run while the model is still writing the next one, so its result exists well
|
||||
/// before the batch's results are committed and `ToolEnded` follows. This is what lets a card
|
||||
/// settle the moment its own call does instead of when the last of the batch does. Optional for
|
||||
/// a client: one that ignores it settles the card on `ToolEnded`, as before. `output` is the
|
||||
/// result's text, already redacted. Args: `{call_id, is_error, output}`.
|
||||
/// a client: one that ignores it settles the card on `ToolEnded`. `output` is the result's
|
||||
/// text, already redacted. Args: `{call_id, is_error, output}`.
|
||||
ToolCompleted { call_id: String, is_error: bool, output: String },
|
||||
/// Tool finished (success or error).
|
||||
ToolEnded { call_id: String, is_error: bool },
|
||||
/// A full assistant message was committed to the session.
|
||||
MessageCommitted {
|
||||
/// Session which owns the persisted entry.
|
||||
session_id: String,
|
||||
/// Persisted entry which owns this message.
|
||||
entry_id: EntryId,
|
||||
message: Message,
|
||||
},
|
||||
/// A full message, from any role, was committed to the session.
|
||||
MessageCommitted { session_id: String, entry_id: EntryId, message: Message },
|
||||
/// A validated remediation report was extracted after its assistant transcript entry committed.
|
||||
RemediationReported {
|
||||
/// Persisted assistant entry that carried the named report block.
|
||||
@@ -74,10 +68,7 @@ pub enum Event {
|
||||
report: RemediationReport,
|
||||
},
|
||||
/// A queued feedback item crossed the engine boundary and can no longer be cancelled.
|
||||
ReviewFeedbackTurnDispatched {
|
||||
/// Immutable feedback submission whose agent turn is about to begin.
|
||||
submission_id: SubmissionId,
|
||||
},
|
||||
ReviewFeedbackTurnDispatched { submission_id: SubmissionId },
|
||||
/// A feedback turn failed before the engine could produce a terminal agent response.
|
||||
ReviewFeedbackTurnFailed {
|
||||
/// Persisted feedback submission whose turn failed.
|
||||
@@ -94,10 +85,7 @@ pub enum Event {
|
||||
answer: String,
|
||||
},
|
||||
/// A feedback turn reached its terminal assistant response and may be marked complete.
|
||||
ReviewFeedbackTurnCompleted {
|
||||
/// Persisted feedback submission whose turn completed.
|
||||
submission_id: SubmissionId,
|
||||
},
|
||||
ReviewFeedbackTurnCompleted { submission_id: SubmissionId },
|
||||
/// A resumed session was loaded with its active-path transcript.
|
||||
SessionLoaded {
|
||||
session_id: String,
|
||||
@@ -106,8 +94,15 @@ pub enum Event {
|
||||
},
|
||||
/// Engine state snapshot changed.
|
||||
StateChanged(EngineState),
|
||||
/// A turn began, whichever client asked for it. Sent before anything else of the turn, the
|
||||
/// user's message included, and always followed by `TurnEnded`, so a client that didn't start
|
||||
/// the turn needn't guess from its first sign.
|
||||
TurnStarted,
|
||||
/// The in-flight turn ended.
|
||||
TurnEnded { reason: StopReason },
|
||||
/// The models this client can switch to, configured ones first, each with the thinking levels
|
||||
/// it honors, answering the `list_models` with this `request_id`.
|
||||
ModelsListed { request_id: String, models: Vec<super::ModelChoice> },
|
||||
/// A complete retained baseline is available for the latest agent turn.
|
||||
LastTurnAvailable {
|
||||
/// Session which owns the persisted baseline.
|
||||
@@ -125,9 +120,8 @@ pub enum Event {
|
||||
ContextUsage { used_tokens: u32, context_window: u32 },
|
||||
/// Token counts + cost for **one provider call**, as it completes.
|
||||
///
|
||||
/// Per call, not per turn: a turn that calls tools makes many, and each reports its own usage.
|
||||
/// Anything that wants a running total should read [`Event::SessionUsage`] rather than trying
|
||||
/// to reduce this stream — the engine already did the arithmetic.
|
||||
/// Per call, not per turn: a turn that calls tools makes many, each reporting its own usage.
|
||||
/// For a running total read [`Event::SessionUsage`], which the engine already summed.
|
||||
TurnUsage {
|
||||
prompt_tokens: u64,
|
||||
completion_tokens: u64,
|
||||
@@ -164,6 +158,8 @@ pub enum Event {
|
||||
Error(String),
|
||||
/// An informational message for the user. Unlike [`Event::Error`] it does not end the turn.
|
||||
Notice(String),
|
||||
/// Who's joined this session now, by name, for the terminal to show; sent as one comes or goes.
|
||||
GuestsChanged(Vec<String>),
|
||||
/// Plan mode wrote one of its plan files. Args: `path`, project-relative.
|
||||
///
|
||||
/// What a front-end offers the plan's next step on, once the turn that wrote it ends.
|
||||
@@ -195,9 +191,8 @@ pub enum Event {
|
||||
},
|
||||
/// A richer Guide replaced the structural chapters a review opened with.
|
||||
///
|
||||
/// Chaptering is advisory and can involve several model passes, so it must never hold the diff
|
||||
/// hostage: the review opens immediately with deterministic sections and upgrades in place when
|
||||
/// (and only when) the pinned revision it describes is still the one on screen.
|
||||
/// Chaptering is advisory and may take several model passes, so the review opens at once with
|
||||
/// deterministic sections and upgrades in place only while its pinned revision is on screen.
|
||||
GuidedReviewGuideUpdated {
|
||||
/// Correlates this Guide with the request whose review it describes.
|
||||
request_id: ReviewLoadRequestId,
|
||||
@@ -511,19 +506,18 @@ pub enum Event {
|
||||
/// Why not.
|
||||
error: crate::project_files::ProjectFileFailure,
|
||||
},
|
||||
/// The feedback session bound to the review just loaded, which always follows
|
||||
/// [`Event::GuidedReviewLoaded`]: carried over a refresh and moved onto its new source,
|
||||
/// restored from disk after a restart, or new. Also sent when another writer's copy replaced
|
||||
/// this one.
|
||||
/// The session bound to the review just loaded, always after [`Event::GuidedReviewLoaded`]:
|
||||
/// carried over a refresh onto its new source, restored after a restart, or new. Also sent when
|
||||
/// another writer's copy replaced this one.
|
||||
ReviewSessionRestored {
|
||||
/// Complete authoritative state, as the review desk holds it.
|
||||
session: Box<ReviewSession>,
|
||||
},
|
||||
/// The open review's feedback session changed and was stored.
|
||||
///
|
||||
/// Every change arrives this way, whoever made it: an `EditReview`, answered under its
|
||||
/// `request_id`, or something Luxe did itself (a turn dispatched, a report recorded, a review
|
||||
/// posted), under a fresh one.
|
||||
/// The answer to `show_open_review`. Boxed: it carries a whole review.
|
||||
OpenReviewShown(Box<super::types::OpenReview>),
|
||||
/// The open review's feedback session changed and was stored. Every change arrives this way,
|
||||
/// whoever made it: an `EditReview`, answered under its `request_id`, or something Luxe did
|
||||
/// itself (a turn dispatched, a report recorded, a review posted), under a fresh one.
|
||||
ReviewSessionSaved {
|
||||
/// The edit this answers, or a fresh id for a change Luxe made itself.
|
||||
request_id: ReviewSessionSaveRequestId,
|
||||
@@ -585,11 +579,10 @@ pub enum Event {
|
||||
/// Whether multiple options may be selected.
|
||||
multi: bool,
|
||||
},
|
||||
/// First frame of an RPC stream: which protocol the peer is speaking, and to what.
|
||||
///
|
||||
/// Emitted by `luxe --rpc` before anything else, so a client checks one integer and refuses a
|
||||
/// build it cannot speak to. Additive changes do not move `protocol_version`; removing or
|
||||
/// retagging anything does. `docs/guide/rpc.md`.
|
||||
/// A prompt stopped waiting, answered by any front-end or cancelled; the others take it down.
|
||||
PromptClosed { id: String },
|
||||
/// First frame of an RPC stream, from `luxe --rpc` or a joined session: protocol and session.
|
||||
/// Additive changes do not move `protocol_version`; removing or retagging anything does.
|
||||
Ready {
|
||||
/// Wire protocol version — [`crate::PROTOCOL_VERSION`].
|
||||
protocol_version: u32,
|
||||
|
||||
@@ -112,6 +112,7 @@ fn sample_actions() -> Vec<Action> {
|
||||
walked_chapter_ids: vec![id("chapter:1")],
|
||||
},
|
||||
Action::RecordReviewSeen { review_id: id("review:1") },
|
||||
Action::ShowOpenReview { request_id: id("review-load:3") },
|
||||
Action::RefreshGuidedReview {
|
||||
request_id: id("review-load:2"),
|
||||
review_id: id("review:1"),
|
||||
@@ -183,6 +184,7 @@ fn sample_actions() -> Vec<Action> {
|
||||
Action::PlanRestack { request_id: id("restack:1") },
|
||||
Action::CheckWorktreeRemoval,
|
||||
Action::LoadFileDiff { request_id: id("file-diff:1"), path: "src/main.rs".into() },
|
||||
Action::ListModels { request_id: "models:1".into() },
|
||||
Action::ListDirectory {
|
||||
request_id: id("listing:1"),
|
||||
path: "src".into(),
|
||||
@@ -208,10 +210,39 @@ fn sample_actions() -> Vec<Action> {
|
||||
}),
|
||||
},
|
||||
Action::Restack { request_id: id("restack:1") },
|
||||
Action::Introduce { name: "iPhone".into(), watch_only: false, yours: false },
|
||||
Action::Close,
|
||||
]
|
||||
}
|
||||
|
||||
/// A watch-only introduction says so on the wire, under the name the guide gives.
|
||||
///
|
||||
/// The break this catches: renaming `watch_only`, so a bridge built against one name joins a host
|
||||
/// built against the other as an ordinary, announced client.
|
||||
#[test]
|
||||
fn a_watch_only_introduction_keeps_its_wire_name() {
|
||||
const PINNED: &str =
|
||||
r#"{"kind":"introduce","content":{"name":"Notifications","watch_only":true}}"#;
|
||||
let watcher =
|
||||
Action::Introduce { name: "Notifications".into(), watch_only: true, yours: false };
|
||||
assert_eq!(serde_json::to_string(&watcher).unwrap(), PINNED);
|
||||
assert_eq!(serde_json::from_str::<Action>(PINNED).unwrap(), watcher);
|
||||
}
|
||||
|
||||
/// The owner's own device says so on the wire, and an introduction without the field is someone
|
||||
/// else's, as every bridge before it sent.
|
||||
///
|
||||
/// The break this catches: renaming `yours`, so a host never knows a device is its owner's and
|
||||
/// announces every reconnect, or a missing field read as yours, quieting a stranger's join.
|
||||
#[test]
|
||||
fn an_owners_introduction_keeps_its_wire_name() {
|
||||
const PINNED: &str = r#"{"kind":"introduce","content":{"name":"iPhone","yours":true}}"#;
|
||||
let device = Action::Introduce { name: "iPhone".into(), watch_only: false, yours: true };
|
||||
assert_eq!(serde_json::to_string(&device).unwrap(), PINNED);
|
||||
let unsaid = r#"{"kind":"introduce","content":{"name":"iPhone"}}"#;
|
||||
assert!(matches!(serde_json::from_str(unsaid), Ok(Action::Introduce { yours: false, .. })));
|
||||
}
|
||||
|
||||
fn section() -> crate::config::GithubSection {
|
||||
crate::config::GithubSection {
|
||||
name: "Mine".into(),
|
||||
@@ -253,6 +284,7 @@ fn the_wire_action_vocabulary_is_pinned() {
|
||||
r#"{"kind":"edit_review","content":{"request_id":"review-save:1","review_id":"review:1","edit":"comment","anchor":{"kind":"line","file_id":"file:1","hunk_id":"hunk:1","side":"new","line":12,"context_hash":""},"severity":"question","title":"","body":"Why does this retry twice?","proposed_fix":null,"send":true}}"#,
|
||||
r#"{"kind":"record_review_progress","content":{"review_id":"review:1","walked_chapter_ids":["chapter:1"]}}"#,
|
||||
r#"{"kind":"record_review_seen","content":{"review_id":"review:1"}}"#,
|
||||
r#"{"kind":"show_open_review","content":{"request_id":"review-load:3"}}"#,
|
||||
r#"{"kind":"refresh_guided_review","content":{"request_id":"review-load:2","review_id":"review:1","revision_id":"revision:1"}}"#,
|
||||
r#"{"kind":"commit_review_chapters","content":{"request_id":"review-save:2","review_id":"review:1","revision_id":"revision:1","squash":true}}"#,
|
||||
r#"{"kind":"prepare_pull_request","content":{"request_id":"pr-open:1","review_id":"review:1"}}"#,
|
||||
@@ -272,6 +304,7 @@ fn the_wire_action_vocabulary_is_pinned() {
|
||||
r#"{"kind":"plan_restack","content":{"request_id":"restack:1"}}"#,
|
||||
r#"{"kind":"check_worktree_removal"}"#,
|
||||
r#"{"kind":"load_file_diff","content":{"request_id":"file-diff:1","path":"src/main.rs"}}"#,
|
||||
r#"{"kind":"list_models","content":{"request_id":"models:1"}}"#,
|
||||
r#"{"kind":"list_directory","content":{"request_id":"listing:1","path":"src"}}"#,
|
||||
r#"{"kind":"read_project_file","content":{"request_id":"file-read:1","path":"src/main.rs"}}"#,
|
||||
r#"{"kind":"set_focus","content":{"focused":false}}"#,
|
||||
@@ -279,6 +312,7 @@ fn the_wire_action_vocabulary_is_pinned() {
|
||||
r#"{"kind":"post_github_comment","content":{"repository":null,"number":142,"thread":"PRRT_1","answering":null,"body":"Fixed in the latest push."}}"#,
|
||||
r#"{"kind":"change_pull_request","content":{"number":142,"write":{"change":"merge","method":"squash","head_oid":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","delete_branch":"feat/threads"}}}"#,
|
||||
r#"{"kind":"restack","content":{"request_id":"restack:1"}}"#,
|
||||
r#"{"kind":"introduce","content":{"name":"iPhone"}}"#,
|
||||
r#"{"kind":"close"}"#,
|
||||
];
|
||||
let samples = sample_actions();
|
||||
|
||||
@@ -40,7 +40,9 @@ published! {
|
||||
ReviewFeedbackTurnCompleted => "review_feedback_turn_completed",
|
||||
SessionLoaded => "session_loaded",
|
||||
StateChanged => "state_changed",
|
||||
TurnStarted => "turn_started",
|
||||
TurnEnded => "turn_ended",
|
||||
ModelsListed => "models_listed",
|
||||
LastTurnAvailable => "last_turn_available",
|
||||
CompactionStarted => "compaction_started",
|
||||
CompactionDone => "compaction_done",
|
||||
@@ -53,6 +55,7 @@ published! {
|
||||
ImageAttached => "image_attached",
|
||||
Error => "error",
|
||||
Notice => "notice",
|
||||
GuestsChanged => "guests_changed",
|
||||
PlanWritten => "plan_written",
|
||||
ReviewStarted => "review_started",
|
||||
ReviewFinished => "review_finished",
|
||||
@@ -94,6 +97,7 @@ published! {
|
||||
ProjectFileRead => "project_file_read",
|
||||
ProjectFileReadFailed => "project_file_read_failed",
|
||||
ReviewSessionRestored => "review_session_restored",
|
||||
OpenReviewShown => "open_review_shown",
|
||||
ReviewSessionSaved => "review_session_saved",
|
||||
ReviewSessionSaveFailed => "review_session_save_failed",
|
||||
SessionsRefreshed => "sessions_refreshed",
|
||||
@@ -103,6 +107,7 @@ published! {
|
||||
ToolActivityUpdated => "tool_activity_updated",
|
||||
PermissionRequested => "permission_requested",
|
||||
QuestionAsked => "question_asked",
|
||||
PromptClosed => "prompt_closed",
|
||||
Ready => "ready",
|
||||
ActionRejected => "action_rejected",
|
||||
}
|
||||
@@ -227,7 +232,9 @@ pub(super) fn every_event() -> Vec<Event> {
|
||||
Event::ReviewFeedbackTurnCompleted { submission_id: SubmissionId::new() },
|
||||
Event::SessionLoaded { session_id: s("s1"), messages: Vec::new() },
|
||||
Event::StateChanged(EngineState::default()),
|
||||
Event::TurnStarted,
|
||||
Event::TurnEnded { reason: StopReason::EndTurn },
|
||||
Event::ModelsListed { request_id: s("models:1"), models: Vec::new() },
|
||||
Event::LastTurnAvailable { session_id: s("s1"), turn_id: s("t1") },
|
||||
Event::CompactionStarted { tokens_before: 2 },
|
||||
Event::CompactionDone { tokens_before: 2, tokens_after: 1 },
|
||||
@@ -248,6 +255,7 @@ pub(super) fn every_event() -> Vec<Event> {
|
||||
Event::ImageAttached { mime: s("image/png"), width: 16, height: 9 },
|
||||
Event::Error(s("lost")),
|
||||
Event::Notice(s("heads up")),
|
||||
Event::GuestsChanged(vec![s("iPhone")]),
|
||||
Event::PlanWritten { path: s("PLAN.md") },
|
||||
Event::ReviewStarted { model: s("m") },
|
||||
Event::ReviewFinished { verdict: RequestedReviewVerdict::Fix, findings: vec![s("f")] },
|
||||
@@ -415,6 +423,12 @@ pub(super) fn every_event() -> Vec<Event> {
|
||||
error: crate::project_files::ProjectFileFailure::NotFound,
|
||||
},
|
||||
Event::ReviewSessionRestored { session: session() },
|
||||
Event::OpenReviewShown(Box::new(crate::event::OpenReview {
|
||||
request_id: ReviewLoadRequestId::new(),
|
||||
review: Some(review()),
|
||||
session: Some(*session()),
|
||||
reading: Vec::new(),
|
||||
})),
|
||||
Event::ReviewSessionSaved {
|
||||
request_id: ReviewSessionSaveRequestId::new(),
|
||||
session: session(),
|
||||
@@ -444,6 +458,7 @@ pub(super) fn every_event() -> Vec<Event> {
|
||||
options: Vec::new(),
|
||||
multi: true,
|
||||
},
|
||||
Event::PromptClosed { id: s("perm:1") },
|
||||
Event::Ready {
|
||||
protocol_version: 1,
|
||||
luxe_version: s("0.1.0"),
|
||||
|
||||
@@ -44,6 +44,20 @@ impl ModelRef {
|
||||
}
|
||||
}
|
||||
|
||||
/// One model a client can switch to, as `list_models` answers. Carries no endpoint: a base URL can
|
||||
/// hold credentials, and switching needs only the two ids.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ModelChoice {
|
||||
/// Provider id, as `select_model` takes it
|
||||
pub provider: String,
|
||||
/// Model id, as `select_model` takes it
|
||||
pub model: String,
|
||||
/// The name to show
|
||||
pub name: String,
|
||||
/// The thinking levels it honors, lowest first: only `off` for a model that doesn't reason
|
||||
pub thinking_levels: Vec<crate::provider::ThinkingLevel>,
|
||||
}
|
||||
|
||||
/// Stop reason from a completed LLM turn.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
@@ -162,3 +176,17 @@ pub enum GuidedReviewFailureKind {
|
||||
/// Local capture, parsing, or persistence failed.
|
||||
Capture,
|
||||
}
|
||||
|
||||
/// The answer to `show_open_review`: the review open now and its session, or neither.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct OpenReview {
|
||||
/// Correlates this answer with its request
|
||||
pub request_id: crate::guided_review::ReviewLoadRequestId,
|
||||
/// The open review, with the latest guide applied
|
||||
pub review: Option<crate::guided_review::GuidedReview>,
|
||||
/// Its feedback session as the desk holds it
|
||||
pub session: Option<crate::guided_review::ReviewSession>,
|
||||
/// What each chapter asks of the reader, decided from lines a client need not keep
|
||||
#[serde(default)]
|
||||
pub reading: Vec<crate::guided_review::ChapterReading>,
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@ mod untracked;
|
||||
|
||||
pub use attention::{review_attention, review_flags, AttentionKind, AttentionSignal};
|
||||
pub use budget::{
|
||||
chapter_needs_review, review_budget, Accounted, ChapterBudget, ItemVerdict, ReviewBudget,
|
||||
chapter_needs_review, chapter_readings, review_budget, shared_reason, Accounted, ChapterBudget,
|
||||
ChapterReading, ItemVerdict, ReviewBudget,
|
||||
};
|
||||
pub use chaptering::{
|
||||
fallback_chapters, fallback_groups, source_items, validate_chapter_coverage,
|
||||
@@ -140,6 +141,7 @@ pub use source_store::{
|
||||
SOURCE_PAYLOAD_FILE,
|
||||
};
|
||||
pub use thread_findings::{thread_findings, ThreadAnswer, ThreadAnswerResult, ThreadReply};
|
||||
pub(crate) use turn_baseline::{prune_turn_baselines, UNREFERENCED_BASELINE_AGE};
|
||||
pub use turn_baseline::{
|
||||
TurnBaselineArtifact, TurnBaselineExclusion, TurnBaselineFile, TurnBaselineManifest,
|
||||
TurnBaselineManifestError, TurnBaselineStoreError, TURN_BASELINE_CACHE_RELATIVE_DIR,
|
||||
|
||||
@@ -197,6 +197,46 @@ pub fn chapter_needs_review(budget: &ReviewBudget, chapter: &ReviewChapter) -> b
|
||||
budget.chapter(&chapter.id).needs_review > 0
|
||||
}
|
||||
|
||||
/// The one reason every item in `chapter` is accounted for: `None` when anything in it needs the
|
||||
/// reviewer, or when different things vouch for different items.
|
||||
pub fn shared_reason(budget: &ReviewBudget, chapter: &ReviewChapter) -> Option<Accounted> {
|
||||
let mut reason: Option<Accounted> = None;
|
||||
for item in &chapter.items {
|
||||
let ItemVerdict::Accounted(accounted) = budget.verdict(item) else {
|
||||
return None;
|
||||
};
|
||||
match &reason {
|
||||
Some(seen) if *seen != accounted => return None,
|
||||
_ => reason = Some(accounted),
|
||||
}
|
||||
}
|
||||
reason
|
||||
}
|
||||
|
||||
/// What one chapter asks of the reader, for a front end that has the chapters but not the lines
|
||||
/// the budget is decided from.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct ChapterReading {
|
||||
/// The chapter it's about
|
||||
pub chapter_id: ChapterId,
|
||||
/// Items still on the reviewer's list
|
||||
pub needs_review: usize,
|
||||
/// The label of [`shared_reason`], when there is one
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub reason: Option<String>,
|
||||
}
|
||||
|
||||
/// What every chapter of `review` asks of the reader, from the budget the terminal reads.
|
||||
pub fn chapter_readings(review: &GuidedReview) -> Vec<ChapterReading> {
|
||||
let budget = review_budget(review, &super::review_flags(review));
|
||||
let reading = |chapter: &ReviewChapter| ChapterReading {
|
||||
chapter_id: chapter.id.clone(),
|
||||
needs_review: budget.chapter(&chapter.id).needs_review,
|
||||
reason: shared_reason(&budget, chapter).map(|reason| reason.label()),
|
||||
};
|
||||
review.chapters.iter().map(reading).collect()
|
||||
}
|
||||
|
||||
fn hunk_verdict(
|
||||
hunk: &ReviewHunk,
|
||||
file: &ReviewFile,
|
||||
|
||||
@@ -249,3 +249,77 @@ fn chapter_roll_up_counts_what_still_needs_review() {
|
||||
assert!(chapter_needs_review(&budget, chapter));
|
||||
assert_eq!(budget.chapter(&chapter.id).needs_review, 1);
|
||||
}
|
||||
|
||||
/// The churn review as one chapter holding all three hunks, so a reason has to be shared to stand.
|
||||
fn one_churn_chapter() -> GuidedReview {
|
||||
let mut review = churn_review();
|
||||
let mut chapter = review.chapters[0].clone();
|
||||
chapter.items = review.chapters.iter().flat_map(|it| it.items.clone()).collect();
|
||||
review.chapters = vec![chapter];
|
||||
review
|
||||
}
|
||||
|
||||
/// One reason behind every item is the chapter's reason.
|
||||
///
|
||||
/// The break this catches: a chapter that reads as needing nothing but never says why, which is
|
||||
/// the line a reader checks before skipping it.
|
||||
#[test]
|
||||
fn a_reason_every_item_shares_is_the_chapters_reason() {
|
||||
let review = one_churn_chapter();
|
||||
let budget = review_budget(&review, &[]);
|
||||
let reason = shared_reason(&budget, &review.chapters[0]);
|
||||
assert_eq!(reason, Some(Accounted::Noise { class: NoiseKind::FormattingOnly }));
|
||||
}
|
||||
|
||||
/// Different reasons for different items name none of them.
|
||||
///
|
||||
/// The break this catches: the first item's reason standing for the whole chapter, which calls a
|
||||
/// lockfile change formatting.
|
||||
#[test]
|
||||
fn mixed_reasons_name_none() {
|
||||
let mut review = one_churn_chapter();
|
||||
review.files[1].hunks[0].noise = NoiseKind::Lockfile;
|
||||
let budget = review_budget(&review, &[]);
|
||||
assert_eq!(shared_reason(&budget, &review.chapters[0]), None);
|
||||
}
|
||||
|
||||
/// One item on the reading list means the chapter has no reason to be skipped.
|
||||
///
|
||||
/// The break this catches: skipping the item that needs review when its neighbors are churn,
|
||||
/// which is exactly the hunk a reviewer is there for.
|
||||
#[test]
|
||||
fn an_item_that_needs_review_leaves_no_reason() {
|
||||
let review = one_churn_chapter();
|
||||
let flagged = signal(None, Some(review.files[1].hunks[0].id.clone()), false);
|
||||
let budget = review_budget(&review, &[flagged]);
|
||||
assert_eq!(shared_reason(&budget, &review.chapters[0]), None);
|
||||
}
|
||||
|
||||
/// A chapter's reading carries its id, what still needs review, and the shared reason's words.
|
||||
///
|
||||
/// The break this catches: a reading that reaches a phone without the count or the words, so it
|
||||
/// shows every chapter as one to read, or none.
|
||||
#[test]
|
||||
fn readings_carry_the_count_and_the_reasons_words() {
|
||||
let churn = one_churn_chapter();
|
||||
let [quiet] = chapter_readings(&churn).try_into().expect("one chapter");
|
||||
assert_eq!(quiet.chapter_id, churn.chapters[0].id);
|
||||
assert_eq!((quiet.needs_review, quiet.reason.as_deref()), (0, Some("formatting only")));
|
||||
let [unvouched] = chapter_readings(&review()).try_into().expect("one chapter");
|
||||
assert_eq!((unvouched.needs_review, unvouched.reason), (1, None));
|
||||
}
|
||||
|
||||
/// A reading weighs what the lines say, as the terminal's budget does.
|
||||
///
|
||||
/// The break this catches: readings decided without the line flags, which would tell a phone that
|
||||
/// a chapter adding a credential is only formatting.
|
||||
#[test]
|
||||
fn readings_weigh_what_the_lines_say() {
|
||||
let mut review = one_churn_chapter();
|
||||
// Built here so no key-shaped literal sits in the source for the scanners to trip on
|
||||
let key = format!("AKIA{}", "Q7ZK4M2TPLX9WRB6");
|
||||
let added = review.files[1].hunks[0].lines.iter_mut().find(|line| line.new_line.is_some());
|
||||
added.expect("an added line").text = format!("let key = \"{key}\";");
|
||||
let [flagged] = chapter_readings(&review).try_into().expect("one chapter");
|
||||
assert_eq!((flagged.needs_review, flagged.reason), (1, None));
|
||||
}
|
||||
|
||||
@@ -116,13 +116,7 @@ impl LocalGit {
|
||||
let snapshot = self
|
||||
.capture_turn_snapshot(TurnBaselineId::from_session_turn(session_id, turn_id), policy)
|
||||
.await?;
|
||||
let artifact = snapshot.artifact;
|
||||
let to_persist = artifact.clone();
|
||||
let project_root = self.repo_path.clone();
|
||||
tokio::task::spawn_blocking(move || to_persist.persist(&project_root))
|
||||
.await
|
||||
.map_err(|_| GuidedReviewError::TurnBaselinePersistenceTask)??;
|
||||
Ok(artifact)
|
||||
self.persist_turn_snapshot(snapshot.artifact).await
|
||||
}
|
||||
|
||||
/// Captures and retains the complete workspace immediately after a turn without changing Git state.
|
||||
@@ -138,13 +132,21 @@ impl LocalGit {
|
||||
policy,
|
||||
)
|
||||
.await?;
|
||||
let artifact = snapshot.artifact;
|
||||
let to_persist = artifact.clone();
|
||||
self.persist_turn_snapshot(snapshot.artifact).await
|
||||
}
|
||||
|
||||
/// Writes a snapshot off the runtime and hands it back. Moved rather than cloned, since a
|
||||
/// snapshot holds every file in the workspace and a copy doubled what a turn held at once.
|
||||
async fn persist_turn_snapshot(
|
||||
&self,
|
||||
artifact: TurnBaselineArtifact,
|
||||
) -> Result<TurnBaselineArtifact, GuidedReviewError> {
|
||||
let project_root = self.repo_path.clone();
|
||||
tokio::task::spawn_blocking(move || to_persist.persist(&project_root))
|
||||
.await
|
||||
.map_err(|_| GuidedReviewError::TurnBaselinePersistenceTask)??;
|
||||
Ok(artifact)
|
||||
let persisted =
|
||||
tokio::task::spawn_blocking(move || artifact.persist(&project_root).map(|_| artifact))
|
||||
.await
|
||||
.map_err(|_| GuidedReviewError::TurnBaselinePersistenceTask)??;
|
||||
Ok(persisted)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -303,9 +303,10 @@ impl TurnBaselineArtifact {
|
||||
})
|
||||
};
|
||||
}
|
||||
let earlier = retention::newest_payload_dir(&root);
|
||||
let staging = root.join(format!(".staging-{}", Uuid::new_v4()));
|
||||
create_private_dir(&staging)?;
|
||||
let result = self.write_staging(&staging).and_then(|()| {
|
||||
let result = self.write_staging(&staging, earlier.as_deref()).and_then(|()| {
|
||||
fs::rename(&staging, &target).map_err(|source| TurnBaselineStoreError::Io {
|
||||
operation: "commit",
|
||||
path: target.clone(),
|
||||
@@ -403,7 +404,11 @@ impl TurnBaselineArtifact {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_staging(&self, staging: &Path) -> Result<(), TurnBaselineStoreError> {
|
||||
fn write_staging(
|
||||
&self,
|
||||
staging: &Path,
|
||||
earlier: Option<&Path>,
|
||||
) -> Result<(), TurnBaselineStoreError> {
|
||||
let payload_dir = staging.join(TURN_BASELINE_PAYLOAD_DIR);
|
||||
create_private_dir(&payload_dir)?;
|
||||
for (path, bytes) in &self.payloads {
|
||||
@@ -421,9 +426,9 @@ impl TurnBaselineArtifact {
|
||||
reason: "payload metadata differs",
|
||||
});
|
||||
}
|
||||
let payload_path = payload_dir.join(digest);
|
||||
let payload_path = payload_dir.join(&digest);
|
||||
if !payload_path.exists() {
|
||||
write_private_file(&payload_path, bytes)?;
|
||||
retention::place_payload(&payload_path, earlier, &digest, bytes)?;
|
||||
}
|
||||
}
|
||||
let manifest = serde_json::to_vec(&self.manifest)?;
|
||||
@@ -486,5 +491,8 @@ fn is_sha256_hex(value: &str) -> bool {
|
||||
value.len() == 64 && value.bytes().all(|byte| byte.is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
mod retention;
|
||||
pub(crate) use retention::{prune_turn_baselines, UNREFERENCED_BASELINE_AGE};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
101
crates/luxe-core/src/guided_review/turn_baseline/retention.rs
Normal file
101
crates/luxe-core/src/guided_review/turn_baseline/retention.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
//! Retention — what a new turn baseline shares with the last one, and which old ones go.
|
||||
//!
|
||||
//! Every agent turn captures the whole workspace twice, and few files change between turns, so a
|
||||
//! baseline hard-links each payload the newest earlier one already holds instead of writing and
|
||||
//! syncing it again. Only the last turn's pair is ever read back, so the rest are deleted once
|
||||
//! they're an hour old; the hour spares a turn that another session in this project is still in.
|
||||
|
||||
use super::{TurnBaselineId, TURN_BASELINE_CACHE_RELATIVE_DIR, TURN_BASELINE_PAYLOAD_DIR};
|
||||
use crate::guided_review::{source_store::write_private_file, SourceStoreError};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
time::{Duration, SystemTime},
|
||||
};
|
||||
use tracing::{debug, warn};
|
||||
|
||||
/// How old a baseline the last-turn record doesn't name must be before pruning deletes it.
|
||||
pub(crate) const UNREFERENCED_BASELINE_AGE: Duration = Duration::from_secs(60 * 60);
|
||||
|
||||
/// The payload directory of the newest committed baseline under `root`, to link unchanged files
|
||||
/// from; none when there isn't one yet.
|
||||
pub(super) fn newest_payload_dir(root: &Path) -> Option<PathBuf> {
|
||||
fs::read_dir(root)
|
||||
.ok()?
|
||||
.filter_map(Result::ok)
|
||||
.filter(|entry| entry.file_type().is_ok_and(|kind| kind.is_dir()))
|
||||
.filter(|entry| !entry.file_name().to_string_lossy().starts_with('.'))
|
||||
.filter_map(|entry| Some((entry.metadata().ok()?.modified().ok()?, entry.path())))
|
||||
.max_by_key(|(modified, _)| *modified)
|
||||
.map(|(_, path)| path.join(TURN_BASELINE_PAYLOAD_DIR))
|
||||
}
|
||||
|
||||
/// Puts one payload at `destination`: a hard link to `earlier`'s copy of the same digest when it
|
||||
/// has a plain file of the right size, else a fresh synced write. A link that fails (another
|
||||
/// filesystem, one that has none) falls back to the write.
|
||||
pub(super) fn place_payload(
|
||||
destination: &Path,
|
||||
earlier: Option<&Path>,
|
||||
digest: &str,
|
||||
bytes: &[u8],
|
||||
) -> Result<(), SourceStoreError> {
|
||||
if let Some(source) = earlier.map(|dir| dir.join(digest)) {
|
||||
let same = fs::symlink_metadata(&source)
|
||||
.is_ok_and(|meta| meta.is_file() && meta.len() == bytes.len() as u64);
|
||||
if same && fs::hard_link(&source, destination).is_ok() {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
write_private_file(destination, bytes)
|
||||
}
|
||||
|
||||
/// Deletes every baseline under the project's cache but `keep` once it's older than `age`, along
|
||||
/// with staging a crashed capture left behind. A symlink is never followed, and one that can't be
|
||||
/// deleted is logged and skipped, since a stale baseline costs only disk. Returns how many went.
|
||||
pub(crate) fn prune_turn_baselines(
|
||||
project_root: &Path,
|
||||
keep: &[&TurnBaselineId],
|
||||
age: Duration,
|
||||
) -> usize {
|
||||
let root = project_root.join(TURN_BASELINE_CACHE_RELATIVE_DIR);
|
||||
let Some(entries) = baseline_entries(&root) else {
|
||||
return 0;
|
||||
};
|
||||
let kept: HashSet<&str> = keep.iter().map(|id| id.cache_key()).collect();
|
||||
let cutoff = SystemTime::now().checked_sub(age).unwrap_or(SystemTime::UNIX_EPOCH);
|
||||
let pruned = entries
|
||||
.filter(|entry| is_stale(entry, &kept, cutoff))
|
||||
.filter(|entry| remove_baseline(&entry.path()))
|
||||
.count();
|
||||
debug!(pruned, "pruned turn baselines");
|
||||
pruned
|
||||
}
|
||||
|
||||
/// The cache's entries; none when it doesn't exist yet, or can't be listed, which is logged.
|
||||
fn baseline_entries(root: &Path) -> Option<impl Iterator<Item = fs::DirEntry>> {
|
||||
match fs::read_dir(root) {
|
||||
Ok(entries) => Some(entries.filter_map(Result::ok)),
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
|
||||
Err(error) => {
|
||||
warn!(path = %root.display(), %error, "could not list turn baselines to prune");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A directory, not a symlink, that `kept` doesn't name and that was last written before `cutoff`.
|
||||
fn is_stale(entry: &fs::DirEntry, kept: &HashSet<&str>, cutoff: SystemTime) -> bool {
|
||||
entry.file_type().is_ok_and(|kind| kind.is_dir())
|
||||
&& !kept.contains(entry.file_name().to_string_lossy().as_ref())
|
||||
&& entry.metadata().and_then(|meta| meta.modified()).is_ok_and(|at| at < cutoff)
|
||||
}
|
||||
|
||||
fn remove_baseline(path: &Path) -> bool {
|
||||
fs::remove_dir_all(path)
|
||||
.inspect_err(|error| warn!(path = %path.display(), %error, "could not prune turn baseline"))
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,145 @@
|
||||
use super::*;
|
||||
use crate::guided_review::{ReviewBase, TurnBaselineArtifact};
|
||||
|
||||
fn baseline(turn: &str, files: &[(&str, &[u8])]) -> TurnBaselineArtifact {
|
||||
let files = files.iter().map(|(path, bytes)| (PathBuf::from(path), bytes.to_vec())).collect();
|
||||
let id = TurnBaselineId::from_session_turn("session", turn);
|
||||
TurnBaselineArtifact::capture(id, ReviewBase::EmptyTree, 1, false, false, 0, files)
|
||||
.expect("a fixture of normal paths is a valid baseline")
|
||||
}
|
||||
|
||||
/// Where `artifact`'s copy of `bytes` sits once persisted under `project`.
|
||||
#[cfg(unix)]
|
||||
fn payload(project: &Path, artifact: &TurnBaselineArtifact, bytes: &[u8]) -> PathBuf {
|
||||
let digest = super::super::payload_digest(bytes);
|
||||
let root = project.join(TURN_BASELINE_CACHE_RELATIVE_DIR);
|
||||
root.join(artifact.manifest.id.cache_key()).join(TURN_BASELINE_PAYLOAD_DIR).join(digest)
|
||||
}
|
||||
|
||||
fn persisted(project: &Path, turn: &str, files: &[(&str, &[u8])]) -> TurnBaselineArtifact {
|
||||
let artifact = baseline(turn, files);
|
||||
artifact.persist(project).expect("a valid baseline persists");
|
||||
artifact
|
||||
}
|
||||
|
||||
fn baseline_dirs(project: &Path) -> Vec<String> {
|
||||
let root = project.join(TURN_BASELINE_CACHE_RELATIVE_DIR);
|
||||
let mut names: Vec<String> = fs::read_dir(root)
|
||||
.expect("the cache exists")
|
||||
.map(|entry| entry.expect("listable").file_name().to_string_lossy().into_owned())
|
||||
.collect();
|
||||
names.sort();
|
||||
names
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn inode(path: &Path) -> u64 {
|
||||
std::os::unix::fs::MetadataExt::ino(&fs::symlink_metadata(path).expect("the payload exists"))
|
||||
}
|
||||
|
||||
// The break this catches: every capture writing and syncing every file again, which cost a
|
||||
// project of 4,000 files about ten seconds a turn and a full copy of it on disk each time
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_baseline_links_a_file_the_last_one_already_holds() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
let first = persisted(project.path(), "1", &[("src/main.rs", b"fn main() {}\n")]);
|
||||
let second_files: &[(&str, &[u8])] = &[("src/main.rs", b"fn main() {}\n"), ("new.rs", b"new")];
|
||||
let second = persisted(project.path(), "2", second_files);
|
||||
let main = b"fn main() {}\n".as_slice();
|
||||
assert_eq!(
|
||||
inode(&payload(project.path(), &first, main)),
|
||||
inode(&payload(project.path(), &second, main))
|
||||
);
|
||||
let loaded = TurnBaselineArtifact::load(project.path(), &second.manifest.id);
|
||||
assert_eq!(loaded.expect("the linked baseline loads"), second);
|
||||
}
|
||||
|
||||
// The break this catches: linking whatever file sits under the digest's name, so a truncated
|
||||
// copy carries into every later baseline and none of them load
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn an_earlier_copy_of_the_wrong_size_is_written_fresh() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
let first = persisted(project.path(), "1", &[("a.rs", b"whole")]);
|
||||
let first_copy = payload(project.path(), &first, b"whole");
|
||||
fs::write(&first_copy, b"cut").expect("the fixture truncates the earlier copy");
|
||||
let second = persisted(project.path(), "2", &[("a.rs", b"whole")]);
|
||||
assert_ne!(inode(&first_copy), inode(&payload(project.path(), &second, b"whole")));
|
||||
assert!(TurnBaselineArtifact::load(project.path(), &second.manifest.id).is_ok());
|
||||
}
|
||||
|
||||
// The break this catches: linking a symlink planted under the digest's name, which hard-links
|
||||
// the symlink itself into the new baseline. Its target is as long as the payload, since a
|
||||
// symlink's own size is its target's length, so the size check alone can't turn it away
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn an_earlier_copy_that_is_a_symlink_is_written_fresh() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
let first = persisted(project.path(), "1", &[("a.rs", b"whole")]);
|
||||
let first_copy = payload(project.path(), &first, b"whole");
|
||||
fs::remove_file(&first_copy).expect("the fixture replaces the earlier copy");
|
||||
std::os::unix::fs::symlink("abcde", &first_copy).expect("the fixture plants a symlink");
|
||||
let second = persisted(project.path(), "2", &[("a.rs", b"whole")]);
|
||||
let second_copy = fs::symlink_metadata(payload(project.path(), &second, b"whole"));
|
||||
assert!(second_copy.expect("the payload exists").is_file());
|
||||
}
|
||||
|
||||
// The break this catches: linking from the oldest baseline rather than the newest, which holds
|
||||
// least of what the workspace has now
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn links_come_from_the_newest_baseline() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
let older = persisted(project.path(), "1", &[("a.rs", b"a")]);
|
||||
let newer = persisted(project.path(), "2", &[("a.rs", b"b")]);
|
||||
let root = project.path().join(TURN_BASELINE_CACHE_RELATIVE_DIR);
|
||||
let older_dir = root.join(older.manifest.id.cache_key());
|
||||
let hours_ago = SystemTime::now() - Duration::from_secs(2 * 60 * 60);
|
||||
fs::File::open(&older_dir).and_then(|dir| dir.set_modified(hours_ago)).expect("backdated");
|
||||
let newest = newest_payload_dir(&root).expect("there are baselines");
|
||||
assert_eq!(newest, root.join(newer.manifest.id.cache_key()).join(TURN_BASELINE_PAYLOAD_DIR));
|
||||
}
|
||||
|
||||
// The break this catches: pruning that ignores what the last-turn record names, losing the one
|
||||
// review the cache exists for, or that deletes nothing at all
|
||||
#[test]
|
||||
fn pruning_deletes_old_baselines_but_the_ones_kept() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
let before = persisted(project.path(), "1", &[("a.rs", b"a")]);
|
||||
let after = persisted(project.path(), "2", &[("a.rs", b"b")]);
|
||||
persisted(project.path(), "3", &[("a.rs", b"c")]);
|
||||
let keep = [&before.manifest.id, &after.manifest.id];
|
||||
assert_eq!(prune_turn_baselines(project.path(), &keep, Duration::ZERO), 1);
|
||||
let mut kept = vec![before.manifest.id.cache_key(), after.manifest.id.cache_key()];
|
||||
kept.sort();
|
||||
assert_eq!(baseline_dirs(project.path()), kept);
|
||||
}
|
||||
|
||||
// The break this catches: pruning by the record alone, which deletes the baseline of a turn
|
||||
// another session in the project is still running
|
||||
#[test]
|
||||
fn pruning_spares_a_baseline_younger_than_the_age() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
persisted(project.path(), "1", &[("a.rs", b"a")]);
|
||||
assert_eq!(prune_turn_baselines(project.path(), &[], UNREFERENCED_BASELINE_AGE), 0);
|
||||
assert_eq!(baseline_dirs(project.path()).len(), 1);
|
||||
}
|
||||
|
||||
// The break this catches: staging a crashed capture left behind piling up forever, since no
|
||||
// record ever names it
|
||||
#[test]
|
||||
fn pruning_deletes_abandoned_staging() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
let staging = project.path().join(TURN_BASELINE_CACHE_RELATIVE_DIR).join(".staging-crashed");
|
||||
fs::create_dir_all(staging.join(TURN_BASELINE_PAYLOAD_DIR)).expect("the fixture stages");
|
||||
assert_eq!(prune_turn_baselines(project.path(), &[], Duration::ZERO), 1);
|
||||
assert!(!staging.exists());
|
||||
}
|
||||
|
||||
// The break this catches: a project that has never captured a baseline failing its first turn
|
||||
#[test]
|
||||
fn pruning_a_project_with_no_cache_does_nothing() {
|
||||
let project = tempfile::tempdir().expect("a temporary project");
|
||||
assert_eq!(prune_turn_baselines(project.path(), &[], Duration::ZERO), 0);
|
||||
}
|
||||
@@ -43,6 +43,7 @@
|
||||
|
||||
mod hidden;
|
||||
mod link_comments;
|
||||
pub(crate) mod nested;
|
||||
|
||||
/// Instruction text with the unreviewable parts removed, and what came out.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
||||
106
crates/luxe-core/src/instructions/nested.rs
Normal file
106
crates/luxe-core/src/instructions/nested.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
//! Nested instructions — the `AGENTS.md` files off the launch chain, read when work reaches them.
|
||||
//!
|
||||
//! The system prompt carries the files from the top of the checkout down to where Luxe started,
|
||||
//! and it's written once per session. A file in a folder off that chain (`web/AGENTS.md` when Luxe
|
||||
//! started at the root) can't join it without rewriting the prompt every later request is cached
|
||||
//! against. So it rides on the result of the first tool call that reaches its folder: an append,
|
||||
//! like any other result. Whether the model already has one is read off the conversation by the
|
||||
//! note that opens it, so a rewind or a compaction that took it away lets it be sent again.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Opens the block that carries a nested file. Its path follows, up to [`NOTE_REST`].
|
||||
const NOTE_OPEN: &str = "[instructions: ";
|
||||
|
||||
/// What follows the path, which is also where reading one back stops: a path may hold a `]`.
|
||||
const NOTE_REST: &str = "] Instructions for work in this folder, from the file there. They add \
|
||||
to the project's.";
|
||||
|
||||
/// Whether nested files are read at all. `LUXE_NESTED_INSTRUCTIONS=0` turns them off.
|
||||
pub(crate) fn enabled() -> bool {
|
||||
std::env::var("LUXE_NESTED_INSTRUCTIONS").as_deref() != Ok("0")
|
||||
}
|
||||
|
||||
/// The paths a call's arguments name: `path`, and each string in `paths`.
|
||||
///
|
||||
/// Those two keys cover the tools that open, write or search a place in the tree. A shell command
|
||||
/// isn't parsed: guessing at the paths in one would load rules for folders the call never reached.
|
||||
pub(crate) fn touched_paths(args: &serde_json::Value) -> Vec<&str> {
|
||||
let single = args.get("path").and_then(serde_json::Value::as_str);
|
||||
let many = args.get("paths").and_then(serde_json::Value::as_array).into_iter().flatten();
|
||||
single
|
||||
.into_iter()
|
||||
.chain(many.filter_map(serde_json::Value::as_str))
|
||||
.filter(|path| !path.trim().is_empty())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// One nested instruction file a call reached.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub(crate) struct NestedFile {
|
||||
/// The file, canonical
|
||||
pub(crate) path: PathBuf,
|
||||
/// Its path from the top of the checkout, with forward slashes: what the note names
|
||||
pub(crate) shown: String,
|
||||
}
|
||||
|
||||
/// The instruction files that apply to `target` but weren't read at session start, top first.
|
||||
///
|
||||
/// Only folders inside the checkout `launch` sits in, off its chain, and not ignored: a file under
|
||||
/// `node_modules` was written by a dependency's authors, not this project's. `target` may not exist
|
||||
/// yet (a file about to be written), so the nearest folder that does stands in.
|
||||
pub(crate) fn nested_files(launch: &Path, target: &Path) -> Vec<NestedFile> {
|
||||
let chain: Vec<PathBuf> = crate::project::chain_from_top(launch)
|
||||
.iter()
|
||||
.filter_map(|folder| folder.canonicalize().ok())
|
||||
.collect();
|
||||
let (Some(top), Some(folder)) = (chain.first(), existing_folder(target)) else {
|
||||
return Vec::new();
|
||||
};
|
||||
folders_below(top, &folder)
|
||||
.into_iter()
|
||||
.filter(|dir| !chain.iter().any(|on_chain| on_chain == dir))
|
||||
.filter_map(|dir| Some((dir, instruction_file(dir)?)))
|
||||
.filter(|(dir, _)| !crate::project_files::is_ignored(top, dir))
|
||||
.filter_map(|(_, file)| nested_file(top, file))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The folders from just below `top` down to `folder`, top first; none when `folder` is outside.
|
||||
fn folders_below<'a>(top: &Path, folder: &'a Path) -> Vec<&'a Path> {
|
||||
if !folder.starts_with(top) {
|
||||
return Vec::new();
|
||||
}
|
||||
let mut below: Vec<&Path> = folder.ancestors().take_while(|dir| *dir != top).collect();
|
||||
below.reverse();
|
||||
below
|
||||
}
|
||||
|
||||
/// The nearest folder at or above `target` that exists, canonical.
|
||||
fn existing_folder(target: &Path) -> Option<PathBuf> {
|
||||
target.ancestors().find(|path| path.is_dir())?.canonicalize().ok()
|
||||
}
|
||||
|
||||
/// The instruction file `dir` holds, by the same names and order the launch chain uses.
|
||||
fn instruction_file(dir: &Path) -> Option<PathBuf> {
|
||||
super::INSTRUCTION_FILES.iter().map(|name| dir.join(name)).find(|path| path.is_file())
|
||||
}
|
||||
|
||||
/// `file` as a [`NestedFile`], named from `top` by where it sits rather than where a link points.
|
||||
fn nested_file(top: &Path, file: PathBuf) -> Option<NestedFile> {
|
||||
let shown = crate::paths::display(file.strip_prefix(top).ok()?);
|
||||
Some(NestedFile { path: file.canonicalize().ok()?, shown })
|
||||
}
|
||||
|
||||
/// The block that carries `shown`'s text to the model.
|
||||
pub(crate) fn note(shown: &str, text: &str) -> String {
|
||||
format!("{NOTE_OPEN}{shown}{NOTE_REST}\n\n{text}")
|
||||
}
|
||||
|
||||
/// The file a block carries, when `text` is one.
|
||||
pub(crate) fn noted(text: &str) -> Option<&str> {
|
||||
text.strip_prefix(NOTE_OPEN)?.split_once(NOTE_REST).map(|(shown, _)| shown)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
101
crates/luxe-core/src/instructions/nested/tests.rs
Normal file
101
crates/luxe-core/src/instructions/nested/tests.rs
Normal file
@@ -0,0 +1,101 @@
|
||||
use super::*;
|
||||
|
||||
/// A checkout at a temp dir's root, with `files` written into it.
|
||||
fn checkout(files: &[&str]) -> tempfile::TempDir {
|
||||
let dir = tempfile::tempdir().expect("temp dir");
|
||||
std::fs::create_dir_all(dir.path().join(".git")).expect("git marker");
|
||||
for file in files {
|
||||
let path = dir.path().join(file);
|
||||
std::fs::create_dir_all(path.parent().expect("a parent")).expect("folders");
|
||||
std::fs::write(&path, format!("- rules from {file}\n")).expect("write");
|
||||
}
|
||||
dir
|
||||
}
|
||||
|
||||
/// What [`nested_files`] names for `target`, from Luxe launched at `launch`, both under `root`.
|
||||
fn shown(root: &Path, launch: &str, target: &str) -> Vec<String> {
|
||||
let files = nested_files(&root.join(launch), &root.join(target));
|
||||
files.into_iter().map(|file| file.shown).collect()
|
||||
}
|
||||
|
||||
/// The break this catches: reading only `path`, so a multi-file `read` loaded nothing.
|
||||
#[test]
|
||||
fn a_call_touches_its_path_and_each_of_its_paths() {
|
||||
let args = serde_json::json!({ "path": "a.rs", "paths": ["web/b.ts", 7, "", "c.md"] });
|
||||
assert_eq!(touched_paths(&args), ["a.rs", "web/b.ts", "c.md"]);
|
||||
assert!(touched_paths(&serde_json::json!({ "command": "cat web/x" })).is_empty());
|
||||
assert!(touched_paths(&serde_json::json!({ "path": 3 })).is_empty());
|
||||
}
|
||||
|
||||
/// The break this catches: the walk started at the launch folder, so a file one folder over
|
||||
/// from it was never found.
|
||||
#[test]
|
||||
fn a_folder_off_the_launch_chain_is_found() {
|
||||
let root = checkout(&["AGENTS.md", "web/AGENTS.md", "web/src/app.ts"]);
|
||||
assert_eq!(shown(root.path(), "", "web/src/app.ts"), ["web/AGENTS.md"]);
|
||||
}
|
||||
|
||||
/// The break this catches: the walk ran nearest first, so the more general rules arrived last
|
||||
/// and read as the ones that win.
|
||||
#[test]
|
||||
fn files_come_top_down() {
|
||||
let root = checkout(&["web/AGENTS.md", "web/src/AGENTS.md", "web/src/app.ts"]);
|
||||
let found = shown(root.path(), "", "web/src/app.ts");
|
||||
assert_eq!(found, ["web/AGENTS.md", "web/src/AGENTS.md"]);
|
||||
}
|
||||
|
||||
/// The break this catches: folders on the launch chain were offered again, sending a file the
|
||||
/// system prompt already carries.
|
||||
#[test]
|
||||
fn the_launch_chain_is_left_to_the_system_prompt() {
|
||||
let root = checkout(&["AGENTS.md", "web/AGENTS.md", "web/src/AGENTS.md", "web/src/app.ts"]);
|
||||
assert_eq!(shown(root.path(), "web", "web/src/app.ts"), ["web/src/AGENTS.md"]);
|
||||
assert!(shown(root.path(), "", "README.md").is_empty(), "the top is the prompt's");
|
||||
}
|
||||
|
||||
/// The break this catches: no ignore check, so a dependency's own `AGENTS.md` under an ignored
|
||||
/// folder instructed the model as if this project wrote it.
|
||||
#[test]
|
||||
fn an_ignored_folder_is_skipped_and_its_sibling_is_not() {
|
||||
let root = checkout(&["vendor/lib/AGENTS.md", "app/AGENTS.md"]);
|
||||
std::fs::write(root.path().join(".gitignore"), "vendor/\n").expect("gitignore");
|
||||
assert!(shown(root.path(), "", "vendor/lib/x.rs").is_empty());
|
||||
assert_eq!(shown(root.path(), "", "app/x.rs"), ["app/AGENTS.md"]);
|
||||
}
|
||||
|
||||
/// The break this catches: no checkout boundary, so a path outside the project loaded the rules
|
||||
/// of whoever owns that folder.
|
||||
#[test]
|
||||
fn a_path_outside_the_checkout_finds_nothing() {
|
||||
let root = checkout(&[]);
|
||||
let elsewhere = checkout(&["lib/AGENTS.md", "lib/x.rs"]);
|
||||
let files = nested_files(root.path(), &elsewhere.path().join("lib/x.rs"));
|
||||
assert!(files.is_empty(), "{files:?}");
|
||||
}
|
||||
|
||||
/// The break this catches: a target that doesn't exist yet (a file about to be written) found
|
||||
/// no folder and so no rules.
|
||||
#[test]
|
||||
fn a_file_not_written_yet_uses_the_nearest_folder() {
|
||||
let root = checkout(&["web/AGENTS.md"]);
|
||||
assert_eq!(shown(root.path(), "", "web/new/deep/page.ts"), ["web/AGENTS.md"]);
|
||||
}
|
||||
|
||||
/// The break this catches: only `AGENTS.md` was looked for, so a folder that still carries a
|
||||
/// `CLAUDE.md` was silent here while the launch chain read it.
|
||||
#[test]
|
||||
fn a_claude_md_counts_like_it_does_on_the_chain() {
|
||||
let root = checkout(&["web/CLAUDE.md"]);
|
||||
assert_eq!(shown(root.path(), "", "web/x.ts"), ["web/CLAUDE.md"]);
|
||||
}
|
||||
|
||||
/// The break this catches: reading a note back stopped at the first `]`, so a path holding one
|
||||
/// read as a different file and was sent again.
|
||||
#[test]
|
||||
fn a_note_reads_back_as_its_file() {
|
||||
let block = note("web/[id]/AGENTS.md", "- rules");
|
||||
assert_eq!(noted(&block), Some("web/[id]/AGENTS.md"));
|
||||
assert!(block.ends_with("\n\n- rules"), "{block}");
|
||||
assert_eq!(noted("[instructions: web/AGENTS.md"), None, "a fragment isn't a note");
|
||||
assert_eq!(noted("12: [instructions: x] y"), None, "nor is a line of a file that quotes one");
|
||||
}
|
||||
@@ -38,6 +38,7 @@ pub mod issue;
|
||||
pub mod json_file;
|
||||
pub mod jsonrpc_frame;
|
||||
pub mod keymap;
|
||||
pub mod local_endpoint;
|
||||
pub mod loop_controller;
|
||||
pub mod loop_hooks;
|
||||
pub(crate) mod loop_state;
|
||||
|
||||
91
crates/luxe-core/src/local_endpoint.rs
Normal file
91
crates/luxe-core/src/local_endpoint.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
//! local_endpoint — the way into a running Luxe from the same machine, and the list of them.
|
||||
//!
|
||||
//! Each interactive Luxe serves its protocol here, so another front-end (the `luxe remote` bridge,
|
||||
//! `luxe join`, a script) can join the session it is running. It is local IPC, not network I/O: a
|
||||
//! Unix socket in a folder only this user can open, or on Windows a named pipe only this user can
|
||||
//! open and no other machine can reach. This module is the one owner of those platform details.
|
||||
|
||||
use std::path::Path;
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
mod ancestry;
|
||||
mod live;
|
||||
#[cfg(unix)]
|
||||
mod unix;
|
||||
#[cfg(windows)]
|
||||
mod windows;
|
||||
|
||||
pub use ancestry::descends_from;
|
||||
pub use live::{live_sessions, running_sessions, LiveSession, Publication};
|
||||
|
||||
#[cfg(unix)]
|
||||
use unix as platform;
|
||||
#[cfg(windows)]
|
||||
use windows as platform;
|
||||
|
||||
/// Both directions of a connection, whatever the platform carries them on.
|
||||
pub trait LocalIo: AsyncRead + AsyncWrite + Unpin + Send {}
|
||||
|
||||
impl<T: AsyncRead + AsyncWrite + Unpin + Send> LocalIo for T {}
|
||||
|
||||
/// A connection to or from a running Luxe.
|
||||
pub type LocalStream = Box<dyn LocalIo>;
|
||||
|
||||
/// A client that connected, with its process id where the platform reports one.
|
||||
pub struct Joined {
|
||||
/// The connection
|
||||
pub stream: LocalStream,
|
||||
/// The client's process id, for the check that it is not one of this Luxe's own children
|
||||
pub peer: Option<u32>,
|
||||
}
|
||||
|
||||
/// Why an endpoint could not be opened, accepted on, or reached.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum EndpointError {
|
||||
/// The operating system refused. Args: what was being done, and its error.
|
||||
#[error("{action}: {source}")]
|
||||
Io {
|
||||
/// What was being done, as a phrase
|
||||
action: String,
|
||||
/// The operating system's error
|
||||
#[source]
|
||||
source: std::io::Error,
|
||||
},
|
||||
}
|
||||
|
||||
impl EndpointError {
|
||||
/// A `map_err` adapter naming what was being done.
|
||||
fn io(action: impl Into<String>) -> impl FnOnce(std::io::Error) -> Self {
|
||||
let action = action.into();
|
||||
move |source| Self::Io { action, source }
|
||||
}
|
||||
}
|
||||
|
||||
/// This Luxe's endpoint, open for clients until it is dropped.
|
||||
pub struct EndpointListener(platform::Listener);
|
||||
|
||||
impl EndpointListener {
|
||||
/// Opens an endpoint for this process. On Unix its socket goes in `dir`, which is created
|
||||
/// private to this user; a Windows pipe has a name rather than a place.
|
||||
pub fn bind(dir: &Path) -> Result<Self, EndpointError> {
|
||||
platform::Listener::bind(dir).map(Self)
|
||||
}
|
||||
|
||||
/// What a client passes to [`connect`]: a socket path, or a pipe name.
|
||||
pub fn address(&self) -> &str {
|
||||
self.0.address()
|
||||
}
|
||||
|
||||
/// The next client to connect, running as this user.
|
||||
pub async fn accept(&mut self) -> Result<Joined, EndpointError> {
|
||||
self.0.accept().await
|
||||
}
|
||||
}
|
||||
|
||||
/// Connects to the endpoint at `address`, as [`EndpointListener::address`] gave it.
|
||||
pub async fn connect(address: &str) -> Result<LocalStream, EndpointError> {
|
||||
platform::connect(address).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
115
crates/luxe-core/src/local_endpoint/ancestry.rs
Normal file
115
crates/luxe-core/src/local_endpoint/ancestry.rs
Normal file
@@ -0,0 +1,115 @@
|
||||
//! Ancestry — whether a process was started, at any depth, by another.
|
||||
//!
|
||||
//! The tripwire that keeps a Luxe's own children (the agent's `bash`, a hook) from joining its
|
||||
//! session and answering their own prompts. A tripwire, not a wall: a process that detaches itself
|
||||
//! leaves the chain, and the documentation says so. Blocking: run it off the async runtime.
|
||||
|
||||
/// How many parents to walk before giving up; real chains are a handful deep.
|
||||
const MAX_DEPTH: usize = 64;
|
||||
|
||||
/// Whether `pid` is `ancestor` or was started, at any depth, by it. `false` when the chain cannot
|
||||
/// be read, so a client this cannot place is let in, as it would be without the tripwire.
|
||||
pub fn descends_from(pid: u32, ancestor: u32) -> bool {
|
||||
descends_in(pid, ancestor, parent_lookup())
|
||||
}
|
||||
|
||||
/// [`descends_from`] over an explicit parent table.
|
||||
fn descends_in(pid: u32, ancestor: u32, parent_of: impl Fn(u32) -> Option<u32>) -> bool {
|
||||
let mut current = pid;
|
||||
for _ in 0..MAX_DEPTH {
|
||||
if current == ancestor {
|
||||
return true;
|
||||
}
|
||||
match parent_of(current) {
|
||||
Some(parent) if parent != current && parent != 0 => current = parent,
|
||||
_ => return false,
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(target_os = "linux")]
|
||||
fn parent_lookup() -> impl Fn(u32) -> Option<u32> {
|
||||
|pid| parent_in_stat(&std::fs::read_to_string(format!("/proc/{pid}/stat")).ok()?)
|
||||
}
|
||||
|
||||
/// The parent id from a `/proc/<pid>/stat` line. The command name sits in parentheses and may hold
|
||||
/// spaces and parentheses itself, so fields are counted from the last `)`.
|
||||
#[cfg(any(target_os = "linux", test))]
|
||||
fn parent_in_stat(stat: &str) -> Option<u32> {
|
||||
let (_, fields) = stat.rsplit_once(')')?;
|
||||
fields.split_whitespace().nth(1)?.parse().ok()
|
||||
}
|
||||
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
fn parent_lookup() -> impl Fn(u32) -> Option<u32> {
|
||||
let table = process_table();
|
||||
move |pid| table.get(&pid).copied()
|
||||
}
|
||||
|
||||
/// Every process's parent, from `ps`, which macOS and the BSDs all have at the same path.
|
||||
#[cfg(all(unix, not(target_os = "linux")))]
|
||||
fn process_table() -> std::collections::HashMap<u32, u32> {
|
||||
let listing = std::process::Command::new("/bin/ps").args(["-A", "-o", "pid=,ppid="]).output();
|
||||
let Ok(listing) = listing else {
|
||||
return std::collections::HashMap::new();
|
||||
};
|
||||
String::from_utf8_lossy(&listing.stdout)
|
||||
.lines()
|
||||
.filter_map(|line| {
|
||||
let mut ids = line.split_whitespace().map(str::parse::<u32>);
|
||||
Some((ids.next()?.ok()?, ids.next()?.ok()?))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn parent_lookup() -> impl Fn(u32) -> Option<u32> {
|
||||
let table = super::windows::process_parents();
|
||||
move |pid| table.get(&pid).copied()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A process is its own descendant, a grandchild is found through its parent, and an
|
||||
/// unrelated process or a broken chain is not.
|
||||
#[test]
|
||||
fn the_chain_is_walked_to_the_ancestor_and_no_further() {
|
||||
let parents = |pid| match pid {
|
||||
30 => Some(20),
|
||||
20 => Some(10),
|
||||
10 => Some(1),
|
||||
1 => Some(0),
|
||||
_ => None,
|
||||
};
|
||||
assert!(descends_in(30, 10, parents), "a grandchild");
|
||||
assert!(descends_in(10, 10, parents), "itself");
|
||||
assert!(!descends_in(10, 30, parents), "a parent is not a child");
|
||||
assert!(!descends_in(99, 10, parents), "unknown, so not placed");
|
||||
assert!(!descends_in(7, 10, |_| Some(7)), "a process that is its own parent ends the walk");
|
||||
}
|
||||
|
||||
/// The parent is the fourth field, counted after a command name that can hold anything.
|
||||
#[test]
|
||||
fn the_parent_is_read_past_an_awkward_command_name() {
|
||||
assert_eq!(parent_in_stat("4242 (bash) S 4000 4242 4242 0"), Some(4000));
|
||||
assert_eq!(parent_in_stat("4242 (my (odd) prog) R 17 4242"), Some(17));
|
||||
assert_eq!(parent_in_stat("garbage"), None);
|
||||
}
|
||||
|
||||
/// A real child of this test is found, and this test is not the child's.
|
||||
#[test]
|
||||
#[cfg(unix)]
|
||||
fn a_real_child_descends_from_this_process() {
|
||||
let mut child = std::process::Command::new("sleep").arg("5").spawn().expect("sleep runs");
|
||||
let (child_id, own_id) = (child.id(), std::process::id());
|
||||
let found = descends_from(child_id, own_id);
|
||||
let reversed = descends_from(own_id, child_id);
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
assert!(found, "the child is placed under this process");
|
||||
assert!(!reversed);
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user