Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99e30d6d0f | ||
|
|
33b87c78a8 | ||
|
|
4ed87ee4e4 | ||
|
|
933c1195b4 | ||
|
|
7b460ab9c7 | ||
|
|
6501c5ed37 | ||
|
|
ab743ed98f | ||
|
|
c53b394bef | ||
|
|
f014ead3b9 |
47
CHANGELOG.md
47
CHANGELOG.md
@@ -9,6 +9,53 @@ 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.2]
|
||||
|
||||
### Fixed
|
||||
|
||||
- Compaction works with thinking models. Summaries were cut off at 4,096 output tokens and thrown
|
||||
away, so after three failures automatic compaction switched itself off and the window filled
|
||||
until the model could not answer. `/compact` now gives the real reason when it fails.
|
||||
- On Windows, a timed-out or cancelled command ends everything it started, not just its shell, and
|
||||
the agent's commands no longer rename your terminal tab.
|
||||
- On Windows, the file tools accept Git Bash paths (`/c/Users/...`), `glob` reads `src\*.py` as a
|
||||
folder, and the agent's Python commands run in UTF-8 mode, so printing characters outside your
|
||||
code page no longer crashes them.
|
||||
- On Windows, an npm tool installed as a `.cmd` shim (such as `pyright`) is found, and Python is
|
||||
looked up as `py -3`, then `python`.
|
||||
- The debugger checks that `debugpy` is installed before offering Python debugging, and
|
||||
`luxe doctor` says how to install it.
|
||||
- A new session started from the start screen keeps its working indicator and timer from the first
|
||||
moment.
|
||||
|
||||
### Changed
|
||||
|
||||
- The dock shows "compacting" while a summary is written, and its context reading drops as soon as
|
||||
it finishes. `--rpc` clients get the new `compaction_started` and `compaction_stopped` events.
|
||||
- An empty reply from the model at a nearly full context window suggests `/compact` instead of a
|
||||
retry.
|
||||
- Signing in with GitHub: the choices are clickable, and the device code has a Copy button (or
|
||||
press `c`).
|
||||
- The installer's closing message says how to start Luxe.
|
||||
|
||||
## [0.1.1]
|
||||
|
||||
### Fixed
|
||||
|
||||
- Editing a file with the edit or write tool, or format-on-save rewriting it, keeps the file's
|
||||
permissions, so an edited script stays executable. A kept mode is now exact rather than trimmed
|
||||
by the umask.
|
||||
- The changes pane's header keeps its position (`8 of 14`) whole, shortening the path instead, and
|
||||
keeps a gap between the line counts and the FILE / DIFF switch.
|
||||
|
||||
### Changed
|
||||
|
||||
- `grep` names each file once with its matching lines under it, rather than repeating the path on
|
||||
every line: 9–28% fewer bytes than `rg -n` on the searches measured. `scopes: true` also labels
|
||||
each run of matches with the function or type around it.
|
||||
- The install guide leads with `curl -fsSL https://luxe.dev/install.sh | sh` and
|
||||
`powershell -c "irm https://luxe.dev/install.ps1 | iex"`.
|
||||
|
||||
## [0.1.0]
|
||||
|
||||
The first public release.
|
||||
|
||||
@@ -16,6 +16,20 @@ option. Unless you explicitly state otherwise, any contribution intentionally su
|
||||
the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
|
||||
additional terms or conditions.
|
||||
|
||||
## AI assistance
|
||||
|
||||
AI help is welcome. Luxe is built with plenty of it, much of it Luxe working on itself. Three things
|
||||
to hold to:
|
||||
|
||||
- **Say so.** Name the tool and how much of the change it wrote in the pull request: "written with
|
||||
Luxe on Claude Sonnet 5.5, then reviewed and edited by me", or "asked ChatGPT how the cache works,
|
||||
wrote the fix myself". Tab completion doesn't count. A commit can carry the Linux kernel's
|
||||
`Assisted-by: AGENT:MODEL` trailer.
|
||||
- **Understand it.** You should be able to explain what your change does and how it fits without
|
||||
asking the tool. That's the bar for any change, and the one an agent makes easiest to skip.
|
||||
- **Write to people in your own words.** An issue or a pull request description is read by a person,
|
||||
so keep it short, and label anything a model wrote.
|
||||
|
||||
## The gate
|
||||
|
||||
```bash
|
||||
|
||||
17
Cargo.lock
generated
17
Cargo.lock
generated
@@ -1626,7 +1626,7 @@ checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
|
||||
|
||||
[[package]]
|
||||
name = "luxe-cli"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -1656,7 +1656,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-core"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base16ct",
|
||||
@@ -1687,7 +1687,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-dap"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"luxe-core",
|
||||
@@ -1701,7 +1701,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-lsp"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base16ct",
|
||||
@@ -1725,7 +1725,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-mcp"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"futures-util",
|
||||
@@ -1745,7 +1745,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-providers"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64",
|
||||
@@ -1764,7 +1764,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-tools"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-trait",
|
||||
@@ -1789,7 +1789,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "luxe-tui"
|
||||
version = "0.1.0"
|
||||
version = "0.1.2"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"arboard",
|
||||
@@ -1797,6 +1797,7 @@ dependencies = [
|
||||
"base64",
|
||||
"crossterm",
|
||||
"futures",
|
||||
"gif",
|
||||
"image",
|
||||
"luxe-core",
|
||||
"luxe-tui",
|
||||
|
||||
@@ -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.0"
|
||||
version = "0.1.2"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/evanwhittaker/luxe"
|
||||
|
||||
19
README.md
19
README.md
@@ -1,4 +1,9 @@
|
||||
<h1 align="center"><img src="web/src/assets/generated/wordmark-banner.svg" alt="Luxe" width="420"></h1>
|
||||
<h1 align="center">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="web/src/assets/generated/wordmark-dark.gif">
|
||||
<img src="web/src/assets/generated/wordmark-light.gif" alt="Luxe" width="440">
|
||||
</picture>
|
||||
</h1>
|
||||
|
||||
Luxe is a coding agent for your terminal, written in Rust. It works in your project with the model
|
||||
you choose, then reads back what it changed as a Guided Review, so you can comment, ask for fixes,
|
||||
@@ -50,16 +55,16 @@ and [the guide lists them](https://luxe.dev/docs/security/#what-luxe-sends-over-
|
||||
|
||||
```bash
|
||||
# macOS and Linux
|
||||
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/evanwhittaker/luxe/releases/latest/download/luxe-cli-installer.sh | sh
|
||||
curl -fsSL https://luxe.dev/install.sh | sh
|
||||
|
||||
# Homebrew
|
||||
brew install evanwhittaker/tap/luxe
|
||||
|
||||
# Windows (PowerShell)
|
||||
powershell -ExecutionPolicy Bypass -c "irm https://github.com/evanwhittaker/luxe/releases/latest/download/luxe-cli-installer.ps1 | iex"
|
||||
|
||||
# Cargo (Rust 1.89 or newer)
|
||||
cargo install --locked luxe-cli
|
||||
|
||||
# Windows (PowerShell)
|
||||
powershell -c "irm https://luxe.dev/install.ps1 | iex"
|
||||
```
|
||||
|
||||
To update, run `luxe update`. To build from source:
|
||||
@@ -156,6 +161,10 @@ Every page is also plain markdown in [`docs/guide/`](docs/guide/), and
|
||||
the site and the demos are made; [`AGENTS.md`](AGENTS.md) has the rules an agent working here holds
|
||||
to. Problems and questions go to [GitHub issues](https://github.com/evanwhittaker/luxe/issues).
|
||||
|
||||
Luxe is built with AI assistance, much of it Luxe working on itself. Every change still passes the
|
||||
full test suite and CI before it merges, and its maintainer decides what does. If AI helped with
|
||||
yours, [say so](CONTRIBUTING.md#ai-assistance).
|
||||
|
||||
Luxe draws on Pi, by earendil-works, and on OpenCode, and on the unidirectional data flow of Elm
|
||||
and Ratatui.
|
||||
|
||||
|
||||
@@ -18,13 +18,13 @@ name = "luxe"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.0" }
|
||||
luxe-providers = { path = "../luxe-providers", version = "0.1.0" }
|
||||
luxe-tools = { path = "../luxe-tools", version = "0.1.0" }
|
||||
luxe-lsp = { path = "../luxe-lsp", version = "0.1.0" }
|
||||
luxe-dap = { path = "../luxe-dap", version = "0.1.0" }
|
||||
luxe-mcp = { path = "../luxe-mcp", version = "0.1.0" }
|
||||
luxe-tui = { path = "../luxe-tui", version = "0.1.0" }
|
||||
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" }
|
||||
anyhow = { workspace = true }
|
||||
clap = { workspace = true }
|
||||
shlex = { workspace = true }
|
||||
|
||||
@@ -164,8 +164,9 @@ async fn main() -> anyhow::Result<()> {
|
||||
/// The tools a session registers, minus the ones that wait on a person or a debugger.
|
||||
fn registry(cwd: &std::path::Path) -> luxe_core::ToolRegistry {
|
||||
let mut registry = luxe_core::ToolRegistry::new();
|
||||
let tracker =
|
||||
luxe_tools::register_builtins(&mut registry, None, luxe_tools::web::WebConfig::from_env());
|
||||
let web = luxe_tools::web::WebConfig::from_env();
|
||||
let outline = Arc::new(luxe_lsp::syntax::SyntaxOutline);
|
||||
let tracker = luxe_tools::register_builtins(&mut registry, None, web, Some(outline));
|
||||
let lsp = Arc::new(luxe_lsp::WorkspaceLspClient::with_servers(Default::default()));
|
||||
let graph = Arc::new(luxe_lsp::graph::GraphService::new(cwd));
|
||||
registry.register(Arc::new(
|
||||
|
||||
@@ -160,15 +160,11 @@ fn debug_adapters() -> String {
|
||||
Vec::new()
|
||||
});
|
||||
for (adapter, from_config) in crate::engine_setup::merged_debug_adapters(&user) {
|
||||
let program = adapter.spec.command.first().cloned().unwrap_or_default();
|
||||
let state = match luxe_core::program::resolve(&program).is_some() {
|
||||
true => "ok ",
|
||||
false => "none ",
|
||||
};
|
||||
let (state, advice) = adapter_state(&adapter.spec.command);
|
||||
let origin = if from_config { " (debug-adapters.json)" } else { "" };
|
||||
let _ = writeln!(
|
||||
out,
|
||||
" {state} {:<12} {}{origin}",
|
||||
" {state} {:<12} {}{origin}{advice}",
|
||||
adapter.spec.id,
|
||||
adapter.spec.command.join(" ")
|
||||
);
|
||||
@@ -177,6 +173,17 @@ fn debug_adapters() -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether an adapter's `command` will start, and, when it is there but will not, what to do: the
|
||||
/// same judgment that decides whether `debug` is offered.
|
||||
fn adapter_state(command: &[String]) -> (&'static str, String) {
|
||||
use luxe_dap::adapter::Unready;
|
||||
match luxe_dap::adapter::unready(command) {
|
||||
None => ("ok ", String::new()),
|
||||
Some(why @ Unready::NoModule(..)) => ("none ", format!(" ({why})")),
|
||||
Some(Unready::Missing(_)) => ("none ", String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
/// The external programs the tools prefer, and what happens without each.
|
||||
fn external_programs() -> String {
|
||||
/// Program, what wants it, and what absence costs.
|
||||
|
||||
@@ -252,7 +252,10 @@ pub(super) fn build_tool_registry(
|
||||
clone_repositories: luxe_core::config::Config::load(state_root).tools.clone_repositories(),
|
||||
..luxe_tools::web::WebConfig::from_env()
|
||||
};
|
||||
let read_tracker = luxe_tools::register_builtins(&mut registry, Some(todo_root), web);
|
||||
// tree-sitter, so `grep` can say which function or type each match sits in
|
||||
let outline = Arc::new(luxe_lsp::syntax::SyntaxOutline);
|
||||
let read_tracker =
|
||||
luxe_tools::register_builtins(&mut registry, Some(todo_root), web, Some(outline));
|
||||
if loop_driven {
|
||||
registry.register(Arc::new(luxe_tools::loop_control::LoopControlTool::new()));
|
||||
}
|
||||
|
||||
@@ -2136,14 +2136,12 @@ On macOS and Linux, the install script puts `luxe` in `~/.local/bin`, or in `$LU
|
||||
when it is set, and adds that to your `PATH`:
|
||||
|
||||
```bash
|
||||
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/evanwhittaker/luxe/releases/latest/download/luxe-cli-installer.sh | sh
|
||||
curl -fsSL https://luxe.dev/install.sh | sh
|
||||
```
|
||||
|
||||
On Windows, in PowerShell:
|
||||
|
||||
```powershell
|
||||
powershell -ExecutionPolicy Bypass -c "irm https://github.com/evanwhittaker/luxe/releases/latest/download/luxe-cli-installer.ps1 | iex"
|
||||
```
|
||||
That address redirects to the script on the [latest
|
||||
release](https://github.com/evanwhittaker/luxe/releases/latest), the one it built and attested. To
|
||||
read it before running it, drop the `| sh`.
|
||||
|
||||
With Homebrew, on macOS or Linux:
|
||||
|
||||
@@ -2158,6 +2156,16 @@ one):
|
||||
cargo install --locked luxe-cli
|
||||
```
|
||||
|
||||
On Windows, in PowerShell:
|
||||
|
||||
```powershell
|
||||
powershell -c "irm https://luxe.dev/install.ps1 | iex"
|
||||
```
|
||||
|
||||
[That script](https://luxe.dev/install.ps1) is two lines: it lets this PowerShell window run the
|
||||
release's installer, which Windows' default execution policy would stop, then runs it. Nothing
|
||||
about your policy is saved.
|
||||
|
||||
Any of them installs the `luxe` command; `luxe --version` checks it worked.
|
||||
|
||||
To update, run `luxe update`, or `/update` inside Luxe. Luxe checks once a day for a newer release
|
||||
@@ -2273,6 +2281,14 @@ on `PATH`, and the agent needs a POSIX shell for most commands. Without one, com
|
||||
If WSL is installed, Luxe skips the `bash.exe` in `System32`, which would run commands inside Linux
|
||||
against a different filesystem.
|
||||
|
||||
Python on Windows is `py` or `python`. `python3` is usually the Microsoft Store's placeholder, so
|
||||
Luxe tries `py -3` first. For the Python debugger, run `py -m pip install debugpy`. The agent's
|
||||
commands run Python in UTF-8 mode (`PYTHONUTF8=1`) unless you have set it yourself, so output with
|
||||
characters outside your code page doesn't crash them.
|
||||
|
||||
Windows Terminal keeps a column at the right edge for its scrollbar. To give it to Luxe, set
|
||||
**Scrollbar visibility** to **Hidden** in your profile's **Appearance** settings.
|
||||
|
||||
## Where Luxe keeps files
|
||||
|
||||
| | Linux and macOS | Windows |
|
||||
@@ -3823,7 +3839,7 @@ what you do not recognize.
|
||||
| `refresh_sessions` | | Answered by `sessions_refreshed` |
|
||||
| `refresh_workspace` | | Answered by `workspace_updated` |
|
||||
| `request_context_composition` | | What fills the context window; answered by `context_composition` |
|
||||
| `compact_now` | `{"focus": "the API decisions"}`, or nothing | Summarize now; answered by `compaction_done` |
|
||||
| `compact_now` | `{"focus": "the API decisions"}`, or nothing | Summarize now; answered by `compaction_started`, then `compaction_done` or `compaction_stopped` |
|
||||
| `load_guided_review` | `{"request_id": "…", "target": {"kind": "working_tree"}}` | Capture a Guided Review; see [below](#guided-review) |
|
||||
| `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` |
|
||||
@@ -3954,6 +3970,7 @@ There are around sixty. These are the ones a front end is built on:
|
||||
| `review_started`, `review_finished` | A requested review began on `model`, and came back with a `verdict` (`continue`, `fix` or `unavailable`) and `findings`. On `fix` the findings also become the agent's next turn. |
|
||||
| `turn_usage`, `session_usage`, `plan_usage` | Cost per call, for the session, and what a subscription has left |
|
||||
| `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 |
|
||||
| `session_loaded`, `sessions_refreshed` | A transcript was restored; the session list changed |
|
||||
| `notice`, `error` | Something to show. `error` ends the turn. |
|
||||
@@ -4205,8 +4222,9 @@ your own terminal is how you give both. So when a step needs one, the agent puts
|
||||
in a code block, you copy it into another terminal and run it, then tell the agent it is done.
|
||||
|
||||
The same goes for a command that waits on you at a terminal, such as a password, a confirmation or
|
||||
an interactive installer. Commands the agent runs have no terminal at all, so one of those fails at
|
||||
once and says what it wanted, rather than waiting where nobody can answer.
|
||||
an interactive installer. Commands the agent runs have no terminal and nothing to read from, so one
|
||||
of those fails or carries on with empty input, rather than waiting where nobody can answer. Most,
|
||||
such as `sudo`, say what they wanted; a shell's `read` just gets an empty answer.
|
||||
|
||||
To allow it anyway, decide ahead of time in your global file. It only helps where no password is
|
||||
needed, such as a passwordless `sudo` rule for one program:
|
||||
@@ -4544,7 +4562,7 @@ group: "Using Luxe"
|
||||
| `write`, `edit` | Write or change a file. A file must be read before it is edited, and the card shows the diff, after [formatting](#format-on-save). |
|
||||
| `bash` | Run a shell command. Long output is trimmed, and the untouched output saved under `.luxe/artifacts/`: each stream whole up to 8 MB, or its first and last 4 MB past that. |
|
||||
| `test` | Run the project's tests and report each failure with its `file:line`, and which tests your change broke. Output is saved as `bash`'s is. |
|
||||
| `grep`, `glob` | Search file contents and names, with capped output |
|
||||
| `grep`, `glob` | Search file contents and names, with capped output. `grep` names each file once with its matching lines under it, and can say which function or type each match sits in. |
|
||||
| `ast_grep` | Search and replace by code structure. Needs [ast-grep](https://ast-grep.github.io). |
|
||||
| `git` | Status, diffs, history, commits, branches, and GitHub pull requests, issues and CI checks through `gh` |
|
||||
| `todo` | The agent's checklist, shown in the dock's **PLAN** |
|
||||
@@ -4871,4 +4889,7 @@ script -f /tmp/luxe.tty -c luxe # replay with `cat` or `scriptreplay`
|
||||
asciinema rec /tmp/luxe.cast -c luxe
|
||||
```
|
||||
|
||||
Windows has neither. Take a screenshot instead, and in Windows Terminal right-click the tab and
|
||||
choose **Export Text** to save what the screen shows.
|
||||
|
||||
Attach the recording and the log from the same time.
|
||||
|
||||
@@ -15,6 +15,21 @@ fn project_server(project: &Path) {
|
||||
std::fs::write(project.join(".mcp.json"), config.to_string()).unwrap();
|
||||
}
|
||||
|
||||
/// Whether Luxe launched the project's server. The marker is the proof when the child gets that far,
|
||||
/// but the child is this test binary starting up again, and on a slow runner Luxe can give up on the
|
||||
/// closed connection before that startup reaches the write; its report of the attempt is the proof
|
||||
/// then. A refused server is never launched, so neither can appear.
|
||||
fn started(project: &Path, stdout: &str) -> bool {
|
||||
project.join("mcp-ran").exists() || stdout.contains("project-marker: failed to connect")
|
||||
}
|
||||
|
||||
/// Whether Luxe refused to start the project's server: it says so, naming it. Checked alongside the
|
||||
/// absent marker, which alone would also pass for a server launched too slowly to have written it.
|
||||
fn refused(project: &Path, stdout: &str) -> bool {
|
||||
stdout.contains("Not starting project MCP servers: project-marker")
|
||||
&& !project.join("mcp-ran").exists()
|
||||
}
|
||||
|
||||
fn tools(project: &Path, home: &Path, trust: bool) -> String {
|
||||
run_mcp(project, home, trust, "tools")
|
||||
}
|
||||
@@ -56,8 +71,8 @@ fn tools_does_not_start_an_untrusted_project_server() {
|
||||
let home = tempfile::tempdir().unwrap();
|
||||
project_server(project.path());
|
||||
let stdout = tools(project.path(), home.path(), false);
|
||||
assert!(!project.path().join("mcp-ran").exists(), "the project command ran: {stdout}");
|
||||
assert!(stdout.contains("project-marker") && stdout.contains("--trust-project"), "{stdout}");
|
||||
assert!(refused(project.path(), &stdout), "the project command ran: {stdout}");
|
||||
assert!(stdout.contains("--trust-project"), "{stdout}");
|
||||
}
|
||||
|
||||
/// Refusing every project server would silently discard the CLI's explicit trust grant.
|
||||
@@ -67,7 +82,7 @@ fn tools_starts_a_project_server_with_explicit_trust() {
|
||||
let home = tempfile::tempdir().unwrap();
|
||||
project_server(project.path());
|
||||
let stdout = tools(project.path(), home.path(), true);
|
||||
assert!(project.path().join("mcp-ran").exists(), "the trusted command never ran: {stdout}");
|
||||
assert!(started(project.path(), &stdout), "the trusted command never ran: {stdout}");
|
||||
}
|
||||
|
||||
/// CLI tools must honor the same global opt-in the engine uses.
|
||||
@@ -78,7 +93,7 @@ fn tools_honors_the_user_always_preference() {
|
||||
project_server(project.path());
|
||||
tooling_preference(&home.path().join("luxe"), "always");
|
||||
let stdout = tools(project.path(), home.path(), false);
|
||||
assert!(project.path().join("mcp-ran").exists(), "the user opt-in was ignored: {stdout}");
|
||||
assert!(started(project.path(), &stdout), "the user opt-in was ignored: {stdout}");
|
||||
}
|
||||
|
||||
/// Explicit trust must not override the user's global refusal to start project tooling.
|
||||
@@ -89,7 +104,7 @@ fn tools_honors_the_user_never_preference_even_with_trust() {
|
||||
project_server(project.path());
|
||||
tooling_preference(&home.path().join("luxe"), "never");
|
||||
let stdout = tools(project.path(), home.path(), true);
|
||||
assert!(!project.path().join("mcp-ran").exists(), "the user refusal was ignored: {stdout}");
|
||||
assert!(refused(project.path(), &stdout), "the user refusal was ignored: {stdout}");
|
||||
}
|
||||
|
||||
/// A repository must not grant itself permission by setting the global-only tooling preference.
|
||||
@@ -100,7 +115,7 @@ fn tools_ignores_a_project_always_preference() {
|
||||
project_server(project.path());
|
||||
tooling_preference(&project.path().join(".luxe"), "always");
|
||||
let stdout = tools(project.path(), home.path(), false);
|
||||
assert!(!project.path().join("mcp-ran").exists(), "the project granted itself trust: {stdout}");
|
||||
assert!(refused(project.path(), &stdout), "the project granted itself trust: {stdout}");
|
||||
}
|
||||
|
||||
/// The trust filter must not block a server declared by the user, outside the project.
|
||||
@@ -112,5 +127,5 @@ fn tools_starts_a_user_server_without_project_trust() {
|
||||
std::fs::create_dir_all(home.path().join("luxe")).unwrap();
|
||||
std::fs::rename(project.path().join(".mcp.json"), home.path().join("luxe/mcp.json")).unwrap();
|
||||
let stdout = tools(project.path(), home.path(), false);
|
||||
assert!(project.path().join("mcp-ran").exists(), "the user command never ran: {stdout}");
|
||||
assert!(started(project.path(), &stdout), "the user command never ran: {stdout}");
|
||||
}
|
||||
|
||||
@@ -68,9 +68,15 @@ 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`.
|
||||
# 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`.
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Console"] }
|
||||
windows-sys = { version = "0.61", features = [
|
||||
"Win32_Foundation",
|
||||
"Win32_Security",
|
||||
"Win32_System_Console",
|
||||
"Win32_System_JobObjects",
|
||||
] }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
@@ -15,13 +15,13 @@ mod planner;
|
||||
mod prune;
|
||||
mod summary;
|
||||
|
||||
pub(crate) use budget::IMAGE_TOKEN_ESTIMATE;
|
||||
pub use budget::{
|
||||
clamp_max_tokens_to_context, compaction_threshold, context_fullness_percent, context_tokens,
|
||||
estimate_calibration, estimate_message_tokens, estimate_prefix_tokens, estimate_tokens,
|
||||
last_observed_context_tokens, prompt_tokens, usable_context_window, ContextBudget,
|
||||
CONTEXT_SAFETY_TOKENS,
|
||||
};
|
||||
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};
|
||||
|
||||
@@ -215,6 +215,27 @@ pub fn clamp_max_tokens_to_context(
|
||||
Some(capped)
|
||||
}
|
||||
|
||||
/// 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;
|
||||
|
||||
/// 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.
|
||||
pub(crate) fn summary_max_tokens(
|
||||
model: &crate::provider::ModelInfo,
|
||||
prompt_tokens: u64,
|
||||
) -> Option<u32> {
|
||||
let ceiling = match model.max_tokens {
|
||||
0 => SUMMARY_MAX_TOKENS,
|
||||
limit => limit.min(SUMMARY_MAX_TOKENS),
|
||||
};
|
||||
clamp_max_tokens_to_context(model.context_window, prompt_tokens, ceiling)
|
||||
}
|
||||
|
||||
/// Percent of the usable window currently occupied. Returns 0.0 when either input is 0 (e.g. no
|
||||
/// usage observed yet), so callers read "unknown" as empty rather than full.
|
||||
pub fn context_fullness_percent(used_tokens: u64, usable: u64) -> f64 {
|
||||
|
||||
@@ -43,6 +43,27 @@ fn the_output_cap_never_claims_room_the_prompt_is_using() {
|
||||
assert_eq!(clamp(8_192, 999_999, 32_000), Some(MIN_MAX_TOKENS));
|
||||
}
|
||||
|
||||
/// A summary gets room to think and then write, within the model's limit and the window.
|
||||
#[test]
|
||||
fn a_summary_gets_room_beyond_what_reasoning_spends() {
|
||||
let model = |context_window, max_tokens| crate::provider::ModelInfo {
|
||||
context_window,
|
||||
max_tokens,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
// Fails if the allowance drops back to where thinking alone could exhaust it
|
||||
let roomy = summary_max_tokens(&model(1_000_000, 128_000), 880_000).expect("a cap");
|
||||
assert!(roomy >= 16_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
|
||||
assert_eq!(summary_max_tokens(&model(1_000_000, 0), 10_000), Some(SUMMARY_MAX_TOKENS));
|
||||
// Fails if the window clamp is skipped
|
||||
let tight = 200_000 - 190_000 - CONTEXT_SAFETY_TOKENS;
|
||||
assert_eq!(summary_max_tokens(&model(200_000, 128_000), 190_000), Some(tight));
|
||||
}
|
||||
|
||||
/// The prompt figure the clamp runs on: measured where the provider told us, estimated only
|
||||
/// for the tail it never saw.
|
||||
#[test]
|
||||
|
||||
@@ -3,13 +3,11 @@
|
||||
use super::*;
|
||||
use crate::compaction::CompactionTrigger;
|
||||
|
||||
/// Output allowance for one summary. Structured prose over a whole window's worth of transcript;
|
||||
/// the clamp below trims it further when the slice being summarized leaves less than this.
|
||||
const SUMMARY_MAX_TOKENS: u32 = 4_096;
|
||||
mod summarizer;
|
||||
|
||||
impl Engine {
|
||||
/// Run the compaction check over the active path ending at `leaf`, executing it if the
|
||||
/// threshold is crossed. Failures are logged and non-fatal.
|
||||
/// threshold is crossed. Failures are announced and non-fatal.
|
||||
///
|
||||
/// Returns the leaf the turn should continue from. A compaction that runs appends its summary
|
||||
/// after `leaf` and becomes the new leaf itself — the caller has to adopt it, or the summary
|
||||
@@ -86,7 +84,8 @@ impl Engine {
|
||||
///
|
||||
/// `focus` is the user's optional steer for the summary ("keep the API decisions"), passed
|
||||
/// through to the summarizer. Announces the outcome either way: a compaction the user asked
|
||||
/// for and cannot see happened is indistinguishable from one that failed.
|
||||
/// for and cannot see happened is indistinguishable from one that failed, and a failure
|
||||
/// reported as "nothing to do" leaves no way to tell what went wrong.
|
||||
pub(crate) async fn compact_on_request(
|
||||
&self,
|
||||
focus: Option<String>,
|
||||
@@ -104,9 +103,8 @@ impl Engine {
|
||||
.await;
|
||||
return Ok(());
|
||||
};
|
||||
let compacted =
|
||||
self.compact_now(leaf.clone(), CompactionTrigger::Manual, focus, event_tx).await;
|
||||
if compacted == leaf && !self.is_cancelled() {
|
||||
let compacted = self.try_compact(leaf, CompactionTrigger::Manual, focus, event_tx).await?;
|
||||
if compacted.is_none() && !self.is_cancelled() {
|
||||
let _ = event_tx
|
||||
.send(Event::Error(
|
||||
"Nothing to compact — the conversation already fits within the recent-context \
|
||||
@@ -119,7 +117,7 @@ impl Engine {
|
||||
}
|
||||
|
||||
/// Plan and run one compaction over the path ending at `leaf`, returning the leaf to continue
|
||||
/// from. Failures are logged, announced, and non-fatal — a turn is still worth taking.
|
||||
/// from. A failure is announced and non-fatal: a turn is still worth taking.
|
||||
pub(super) async fn compact_now(
|
||||
&self,
|
||||
leaf: EntryId,
|
||||
@@ -127,32 +125,63 @@ impl Engine {
|
||||
focus: Option<String>,
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) -> EntryId {
|
||||
match self.try_compact(leaf.clone(), trigger, focus, event_tx).await {
|
||||
Ok(Some(compacted)) => compacted,
|
||||
Ok(None) => leaf,
|
||||
Err(error) => {
|
||||
self.announce_compaction_failure(&error, event_tx).await;
|
||||
leaf
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One compaction attempt: the new leaf when a summary was committed, `None` when there was
|
||||
/// nothing to do or a cancel stopped it, and the reason when it failed.
|
||||
async fn try_compact(
|
||||
&self,
|
||||
leaf: EntryId,
|
||||
trigger: CompactionTrigger,
|
||||
focus: Option<String>,
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) -> Result<Option<EntryId>, EngineError> {
|
||||
let sid = self.current_session_id().await;
|
||||
let entries = match self.sessions().active_path(&sid, &leaf) {
|
||||
Ok(e) => e,
|
||||
let entries = self.sessions().active_path(&sid, &leaf)?;
|
||||
let Some(plan) = self.plan_compaction(&entries, trigger).await? else { return Ok(None) };
|
||||
let before = crate::compaction::context_tokens(&entries);
|
||||
let _ = event_tx.send(Event::CompactionStarted { tokens_before: plan.tokens_before }).await;
|
||||
let outcome = self.execute_compaction(leaf, &plan, focus, event_tx).await;
|
||||
// Paired with the start on every ending, cancel included: a cancel unwinds the summary
|
||||
// call to an error rather than dropping this future, so this line always runs
|
||||
if outcome.is_err() {
|
||||
let _ = event_tx.send(Event::CompactionStopped).await;
|
||||
}
|
||||
let compacted = match outcome {
|
||||
Ok(compacted) => compacted,
|
||||
// The reader stopped it; nothing failed, so the breaker does not hear of it
|
||||
Err(_) if self.is_cancelled() => return Ok(None),
|
||||
Err(e) => {
|
||||
warn!(error = %e, "compaction: failed to load active path");
|
||||
return leaf;
|
||||
self.compaction_failures.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
let observed = self.last_context_tokens.load(std::sync::atomic::Ordering::Relaxed);
|
||||
// The budget belongs to the model the next turn will use: switching to a million-token
|
||||
// model mid-session has to raise the threshold, not keep compacting at the old one.
|
||||
let model = self.effective_model().await;
|
||||
let budget = crate::compaction::ContextBudget {
|
||||
context_window: model.context_window as u64,
|
||||
max_tokens: model.max_tokens,
|
||||
observed_tokens: (observed > 0).then_some(observed as u64),
|
||||
prefix_tokens: u64::from(
|
||||
self.last_prefix_tokens.load(std::sync::atomic::Ordering::Relaxed),
|
||||
),
|
||||
};
|
||||
match crate::compaction::find_compaction_point(
|
||||
&entries,
|
||||
&self.config.compaction_settings,
|
||||
budget,
|
||||
trigger,
|
||||
) {
|
||||
self.weigh_compaction(before, &compacted).await;
|
||||
// The summary now stands for everything it replaced, so the occupancy the next threshold
|
||||
// check reads has to be re-measured rather than carried over: a stale figure compacts
|
||||
// again immediately, every turn.
|
||||
self.last_context_tokens.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
self.set_leaf(compacted.clone()).await;
|
||||
Ok(Some(compacted))
|
||||
}
|
||||
|
||||
/// Where to cut `entries`, or `None` when there is nothing worth summarizing.
|
||||
async fn plan_compaction(
|
||||
&self,
|
||||
entries: &[crate::session::Entry],
|
||||
trigger: CompactionTrigger,
|
||||
) -> Result<Option<crate::compaction::CompactionPlan>, EngineError> {
|
||||
let settings = &self.config.compaction_settings;
|
||||
let budget = self.context_budget().await;
|
||||
match crate::compaction::find_compaction_point(entries, settings, budget, trigger) {
|
||||
Ok(plan) => {
|
||||
debug!(
|
||||
?trigger,
|
||||
@@ -160,131 +189,78 @@ impl Engine {
|
||||
messages_to_summarize = plan.messages_to_summarize.len(),
|
||||
"compaction triggered"
|
||||
);
|
||||
let before = crate::compaction::context_tokens(&entries);
|
||||
match self.execute_compaction(leaf.clone(), &plan, focus, event_tx).await {
|
||||
Ok(compacted) => {
|
||||
// A summary that freed almost nothing leaves occupancy over the line, so
|
||||
// the next iteration asks again — a provider call per tool call for the
|
||||
// rest of the turn. Counting it against the breaker keeps the transcript
|
||||
// (the summary is not *wrong*, only insufficient) while bounding how many
|
||||
// times a session may pay to learn the same thing.
|
||||
let after = self.estimated_context_tokens(&compacted).await;
|
||||
if crate::compaction::compaction_was_effective(before, after) {
|
||||
self.compaction_failures.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
} else {
|
||||
let failures = self
|
||||
.compaction_failures
|
||||
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
+ 1;
|
||||
warn!(
|
||||
before,
|
||||
after,
|
||||
failures,
|
||||
"compaction freed too little to be worth repeating; \
|
||||
raise the window or lower compaction.keep_recent"
|
||||
);
|
||||
}
|
||||
// The summary now stands for everything it replaced, so the occupancy the
|
||||
// next threshold check reads has to be re-measured rather than carried
|
||||
// over — a stale figure compacts again immediately, every turn.
|
||||
self.last_context_tokens.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
self.set_leaf(compacted.clone()).await;
|
||||
return compacted;
|
||||
}
|
||||
// The reader stopped it; nothing failed, so the breaker does not hear of it
|
||||
Err(_) if self.is_cancelled() => debug!("compaction cancelled"),
|
||||
Err(e) => {
|
||||
let failures = self
|
||||
.compaction_failures
|
||||
.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
|
||||
+ 1;
|
||||
warn!(error = %e, failures, "compaction execution failed");
|
||||
if crate::compaction::compaction_circuit_open(failures) {
|
||||
warn!(
|
||||
failures,
|
||||
"auto-compaction disabled for this session after repeated failures"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Some(plan))
|
||||
}
|
||||
Err(crate::compaction::CompactionError::Cancelled) => {
|
||||
debug!("compaction cancelled or below threshold");
|
||||
Ok(None)
|
||||
}
|
||||
Err(e) => warn!(error = %e, "compaction check failed"),
|
||||
Err(e) => Err(EngineError::Compaction(format!("could not plan the cut: {e}"))),
|
||||
}
|
||||
leaf
|
||||
}
|
||||
|
||||
/// The one-off request that asks the summarizer for `input`, the provider to send it to, and
|
||||
/// the model its cost is priced against.
|
||||
async fn summary_request(
|
||||
&self,
|
||||
input: &str,
|
||||
focus: Option<&str>,
|
||||
) -> (
|
||||
Arc<dyn crate::provider::Provider>,
|
||||
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.";
|
||||
|
||||
// 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.
|
||||
let user_message = match focus.map(str::trim).filter(|focus| !focus.is_empty()) {
|
||||
Some(focus) => format!(
|
||||
"Summarize the following conversation, paying particular attention to: {focus}\n\n{input}"
|
||||
/// The window the next turn's model gives the planner, and what of it is already measured.
|
||||
///
|
||||
/// The model the next turn will use, not the one that filled the window: switching to a
|
||||
/// million-token model mid-session has to raise the threshold, not keep compacting at the old
|
||||
/// one.
|
||||
async fn context_budget(&self) -> crate::compaction::ContextBudget {
|
||||
let observed = self.last_context_tokens.load(std::sync::atomic::Ordering::Relaxed);
|
||||
let model = self.effective_model().await;
|
||||
crate::compaction::ContextBudget {
|
||||
context_window: model.context_window as u64,
|
||||
max_tokens: model.max_tokens,
|
||||
observed_tokens: (observed > 0).then_some(observed as u64),
|
||||
prefix_tokens: u64::from(
|
||||
self.last_prefix_tokens.load(std::sync::atomic::Ordering::Relaxed),
|
||||
),
|
||||
None => format!("Summarize the following conversation:\n\n{input}"),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve the summarizer: the configured `summarizer` override when set, else the active
|
||||
// model. Route through the provider registry so a cross-provider summarizer works, falling
|
||||
// back to the default provider when the registry has no entry (tests/single-provider).
|
||||
let summarizer_model = crate::compaction::summarizer_model(
|
||||
self.config.compaction_settings.summarizer.as_ref(),
|
||||
&self.effective_model().await,
|
||||
/// Reset the failure breaker after a compaction that freed real room, or count one that did not.
|
||||
///
|
||||
/// A summary that freed almost nothing leaves occupancy over the line, so the next iteration
|
||||
/// asks again: a provider call per tool call for the rest of the turn. Counting it keeps the
|
||||
/// transcript (the summary is not *wrong*, only insufficient) while bounding how many times a
|
||||
/// session may pay to learn the same thing.
|
||||
async fn weigh_compaction(&self, before: u64, compacted: &EntryId) {
|
||||
let after = self.estimated_context_tokens(compacted).await;
|
||||
if crate::compaction::compaction_was_effective(before, after) {
|
||||
self.compaction_failures.store(0, std::sync::atomic::Ordering::Relaxed);
|
||||
return;
|
||||
}
|
||||
let failures =
|
||||
self.compaction_failures.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
|
||||
warn!(
|
||||
before,
|
||||
after,
|
||||
failures,
|
||||
"compaction freed too little to be worth repeating; \
|
||||
raise the window or lower compaction.keep_recent"
|
||||
);
|
||||
let provider = self.provider_for(&summarizer_model);
|
||||
}
|
||||
|
||||
let priced_model = summarizer_model.clone();
|
||||
let messages = vec![crate::message::Message::user(user_message)];
|
||||
// The one call whose prompt is a whole window by construction, so the summary's own
|
||||
// allowance has to come out of what is left rather than on top of it.
|
||||
let max_tokens = crate::compaction::clamp_max_tokens_to_context(
|
||||
summarizer_model.context_window,
|
||||
crate::compaction::estimate_message_tokens(&messages)
|
||||
+ crate::compaction::estimate_prefix_tokens(Some(system_prompt), &[]),
|
||||
SUMMARY_MAX_TOKENS,
|
||||
);
|
||||
let request = crate::provider::CompletionRequest {
|
||||
shared_system_len: 0,
|
||||
model: summarizer_model,
|
||||
messages,
|
||||
system: Some(system_prompt.to_string()),
|
||||
tools: vec![],
|
||||
deferred_tools: Vec::new(),
|
||||
thinking: crate::provider::ThinkingLevel::Off,
|
||||
max_tokens,
|
||||
reuse: crate::provider::PromptReuse::OneOff,
|
||||
/// Tell the reader a compaction failed, and that automatic compaction has stopped when this one
|
||||
/// tripped the breaker: otherwise the first they hear of it is a window too full to answer in.
|
||||
async fn announce_compaction_failure(
|
||||
&self,
|
||||
error: &EngineError,
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) {
|
||||
let failures = self.compaction_failures.load(std::sync::atomic::Ordering::Relaxed);
|
||||
warn!(%error, failures, "compaction failed");
|
||||
let reason = match error {
|
||||
EngineError::Compaction(reason) => reason.clone(),
|
||||
other => other.to_string(),
|
||||
};
|
||||
|
||||
(provider, priced_model, request)
|
||||
let mut notice = format!("Couldn't compact the conversation: {reason}.");
|
||||
if crate::compaction::compaction_circuit_open(failures) {
|
||||
warn!(failures, "auto-compaction disabled for this session after repeated failures");
|
||||
notice.push_str(
|
||||
" Automatic compaction is off for this session; run /compact to try again.",
|
||||
);
|
||||
}
|
||||
let _ = event_tx.send(Event::Notice(notice)).await;
|
||||
}
|
||||
|
||||
/// Execute compaction: call the summarizer, format the summary, append it after `leaf`.
|
||||
@@ -383,87 +359,4 @@ impl Engine {
|
||||
debug!(%compacted, tokens_before = plan.tokens_before, tokens_after, "compaction executed");
|
||||
Ok(compacted)
|
||||
}
|
||||
|
||||
/// Sends the summarizer its request, unless a cancel lands first.
|
||||
async fn open_summary(
|
||||
&self,
|
||||
provider: Arc<dyn Provider + Send + Sync>,
|
||||
request: crate::provider::CompletionRequest,
|
||||
) -> Result<futures::stream::BoxStream<'static, crate::provider::ProviderEvent>, EngineError>
|
||||
{
|
||||
let mut cancel = self.cancel_tx.subscribe();
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = cancel.wait_for(|cancelled| *cancelled) => Err(cancelled()),
|
||||
opened = provider.stream(request, self.cancel_tx.subscribe()) => {
|
||||
opened.map_err(EngineError::Provider)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the summarizer's reply, billing its usage. Only a reply that finished counts: an
|
||||
/// error, a length stop or a stream that just ends leave a fragment, however complete it looks.
|
||||
async fn collect_summary(
|
||||
&self,
|
||||
mut stream: futures::stream::BoxStream<'static, crate::provider::ProviderEvent>,
|
||||
priced_model: &crate::provider::ModelInfo,
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) -> Result<String, EngineError> {
|
||||
use futures::StreamExt;
|
||||
let mut summary_output = String::new();
|
||||
let mut finished = None;
|
||||
// Raced rather than left to the provider, since one mid-request may not look at its own
|
||||
// cancel receiver until the next byte arrives
|
||||
let mut cancel = self.cancel_tx.subscribe();
|
||||
loop {
|
||||
let event = tokio::select! {
|
||||
biased;
|
||||
_ = cancel.wait_for(|cancelled| *cancelled) => return Err(cancelled()),
|
||||
event = stream.next() => event,
|
||||
};
|
||||
let Some(event) = event else { break };
|
||||
match event {
|
||||
crate::provider::ProviderEvent::TextDelta { delta, .. } => {
|
||||
summary_output.push_str(&delta);
|
||||
}
|
||||
// the call nobody asked for, and often the most expensive one in a session
|
||||
crate::provider::ProviderEvent::Usage(usage) => {
|
||||
self.account_call(
|
||||
crate::usage::UsageSource::Compaction,
|
||||
priced_model,
|
||||
&usage,
|
||||
event_tx,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
crate::provider::ProviderEvent::Done { reason } => {
|
||||
finished = Some(reason);
|
||||
break;
|
||||
}
|
||||
crate::provider::ProviderEvent::Error { message, .. } => {
|
||||
return Err(EngineError::Compaction(format!(
|
||||
"the summarizer failed partway ({message}); the transcript stands"
|
||||
)));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
match finished {
|
||||
None => Err(EngineError::Compaction(
|
||||
"the summarizer's reply ended before it finished; the transcript stands".into(),
|
||||
)),
|
||||
Some(
|
||||
reason @ (crate::provider::StopReason::Length
|
||||
| crate::provider::StopReason::ContentFilter),
|
||||
) => Err(EngineError::Compaction(format!(
|
||||
"the summary stopped short ({reason:?}); the transcript stands"
|
||||
))),
|
||||
Some(_) => Ok(summary_output),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The error a cancelled summary unwinds with; callers tell it apart by the cancel flag.
|
||||
fn cancelled() -> EngineError {
|
||||
EngineError::Compaction("cancelled; the transcript stands".into())
|
||||
}
|
||||
|
||||
161
crates/luxe-core/src/engine/compact/summarizer.rs
Normal file
161
crates/luxe-core/src/engine/compact/summarizer.rs
Normal file
@@ -0,0 +1,161 @@
|
||||
//! Summarizer — the one-off request a compaction sends, and reading its reply.
|
||||
//!
|
||||
//! `compact.rs` decides *when* and *where* to cut; this owns the conversation with the model that
|
||||
//! writes the summary.
|
||||
|
||||
use super::*;
|
||||
|
||||
impl Engine {
|
||||
/// The one-off request that asks the summarizer for `input`, the provider to send it to, and
|
||||
/// the model its cost is priced against.
|
||||
pub(super) async fn summary_request(
|
||||
&self,
|
||||
input: &str,
|
||||
focus: Option<&str>,
|
||||
) -> (
|
||||
Arc<dyn crate::provider::Provider>,
|
||||
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.";
|
||||
|
||||
// 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.
|
||||
let user_message = match focus.map(str::trim).filter(|focus| !focus.is_empty()) {
|
||||
Some(focus) => format!(
|
||||
"Summarize the following conversation, paying particular attention to: {focus}\n\n{input}"
|
||||
),
|
||||
None => format!("Summarize the following conversation:\n\n{input}"),
|
||||
};
|
||||
|
||||
// Resolve the summarizer: the configured `summarizer` override when set, else the active
|
||||
// model. Route through the provider registry so a cross-provider summarizer works, falling
|
||||
// back to the default provider when the registry has no entry (tests/single-provider).
|
||||
let summarizer_model = crate::compaction::summarizer_model(
|
||||
self.config.compaction_settings.summarizer.as_ref(),
|
||||
&self.effective_model().await,
|
||||
);
|
||||
let provider = self.provider_for(&summarizer_model);
|
||||
|
||||
let priced_model = summarizer_model.clone();
|
||||
let messages = vec![crate::message::Message::user(user_message)];
|
||||
// The one call whose prompt is a whole window by construction, so the summary's own
|
||||
// allowance has to come out of what is left rather than on top of it.
|
||||
let max_tokens = crate::compaction::summary_max_tokens(
|
||||
&summarizer_model,
|
||||
crate::compaction::estimate_message_tokens(&messages)
|
||||
+ crate::compaction::estimate_prefix_tokens(Some(system_prompt), &[]),
|
||||
);
|
||||
let request = crate::provider::CompletionRequest {
|
||||
shared_system_len: 0,
|
||||
model: summarizer_model,
|
||||
messages,
|
||||
system: Some(system_prompt.to_string()),
|
||||
tools: vec![],
|
||||
deferred_tools: Vec::new(),
|
||||
thinking: crate::provider::ThinkingLevel::Off,
|
||||
max_tokens,
|
||||
reuse: crate::provider::PromptReuse::OneOff,
|
||||
};
|
||||
|
||||
(provider, priced_model, request)
|
||||
}
|
||||
|
||||
/// Sends the summarizer its request, unless a cancel lands first.
|
||||
pub(super) async fn open_summary(
|
||||
&self,
|
||||
provider: Arc<dyn Provider + Send + Sync>,
|
||||
request: crate::provider::CompletionRequest,
|
||||
) -> Result<futures::stream::BoxStream<'static, crate::provider::ProviderEvent>, EngineError>
|
||||
{
|
||||
let mut cancel = self.cancel_tx.subscribe();
|
||||
tokio::select! {
|
||||
biased;
|
||||
_ = cancel.wait_for(|cancelled| *cancelled) => Err(cancelled()),
|
||||
opened = provider.stream(request, self.cancel_tx.subscribe()) => {
|
||||
opened.map_err(EngineError::Provider)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read the summarizer's reply, billing its usage. Only a reply that finished counts: an
|
||||
/// error, a length stop or a stream that just ends leave a fragment, however complete it looks.
|
||||
pub(super) async fn collect_summary(
|
||||
&self,
|
||||
mut stream: futures::stream::BoxStream<'static, crate::provider::ProviderEvent>,
|
||||
priced_model: &crate::provider::ModelInfo,
|
||||
event_tx: &mpsc::Sender<Event>,
|
||||
) -> Result<String, EngineError> {
|
||||
use futures::StreamExt;
|
||||
let mut summary_output = String::new();
|
||||
let mut finished = None;
|
||||
// Raced rather than left to the provider, since one mid-request may not look at its own
|
||||
// cancel receiver until the next byte arrives
|
||||
let mut cancel = self.cancel_tx.subscribe();
|
||||
loop {
|
||||
let event = tokio::select! {
|
||||
biased;
|
||||
_ = cancel.wait_for(|cancelled| *cancelled) => return Err(cancelled()),
|
||||
event = stream.next() => event,
|
||||
};
|
||||
let Some(event) = event else { break };
|
||||
match event {
|
||||
crate::provider::ProviderEvent::TextDelta { delta, .. } => {
|
||||
summary_output.push_str(&delta);
|
||||
}
|
||||
// the call nobody asked for, and often the most expensive one in a session
|
||||
crate::provider::ProviderEvent::Usage(usage) => {
|
||||
self.account_call(
|
||||
crate::usage::UsageSource::Compaction,
|
||||
priced_model,
|
||||
&usage,
|
||||
event_tx,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
crate::provider::ProviderEvent::Done { reason } => {
|
||||
finished = Some(reason);
|
||||
break;
|
||||
}
|
||||
crate::provider::ProviderEvent::Error { message, .. } => {
|
||||
return Err(EngineError::Compaction(format!(
|
||||
"the summarizer failed partway ({message}); the transcript stands"
|
||||
)));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
match finished {
|
||||
None => Err(EngineError::Compaction(
|
||||
"the summarizer's reply ended before it finished; the transcript stands".into(),
|
||||
)),
|
||||
Some(
|
||||
reason @ (crate::provider::StopReason::Length
|
||||
| crate::provider::StopReason::ContentFilter),
|
||||
) => Err(EngineError::Compaction(format!(
|
||||
"the summary stopped short ({reason:?}); the transcript stands"
|
||||
))),
|
||||
Some(_) => Ok(summary_output),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The error a cancelled summary unwinds with; callers tell it apart by the cancel flag.
|
||||
fn cancelled() -> EngineError {
|
||||
EngineError::Compaction("cancelled; the transcript stands".into())
|
||||
}
|
||||
@@ -151,14 +151,10 @@ impl Engine {
|
||||
debug!("the provider stream was dropped by a cancel before it produced anything");
|
||||
return StopReason::Other("cancelled".to_string());
|
||||
}
|
||||
warn!("the provider returned an empty response; nothing was committed");
|
||||
let _ = event_tx
|
||||
.send(Event::Error(
|
||||
"the model returned an empty response — no text, no tool call. Nothing was added \
|
||||
to the conversation; send your message again to retry."
|
||||
.to_string(),
|
||||
))
|
||||
.await;
|
||||
let used = self.last_context_tokens.load(std::sync::atomic::Ordering::Relaxed);
|
||||
let window = self.effective_model().await.context_window;
|
||||
warn!(used, window, "the provider returned an empty response; nothing was committed");
|
||||
let _ = event_tx.send(Event::Error(empty_reply_message(used, window))).await;
|
||||
StopReason::Other("empty response".to_string())
|
||||
}
|
||||
|
||||
@@ -270,3 +266,47 @@ impl Engine {
|
||||
Ok(entry_id)
|
||||
}
|
||||
}
|
||||
|
||||
/// What an empty reply tells the reader. Near a full window the silence is most likely the model
|
||||
/// having no room left to answer in, and retrying the same message fails the same way until
|
||||
/// `/compact` makes room.
|
||||
fn empty_reply_message(used: u32, window: u32) -> String {
|
||||
let nearly_full = window > 0 && u64::from(used) * 10 >= u64::from(window) * 9;
|
||||
if !nearly_full {
|
||||
return "the model returned an empty response — no text, no tool call. Nothing was added \
|
||||
to the conversation; send your message again to retry."
|
||||
.to_string();
|
||||
}
|
||||
let percent = (u64::from(used) * 100 / u64::from(window)).min(100);
|
||||
format!(
|
||||
"the model returned an empty response, most likely because the context window is full \
|
||||
({percent}% used). Run /compact to summarize the conversation, then send your message \
|
||||
again."
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::empty_reply_message;
|
||||
|
||||
/// The silence at a full window looked like a retryable blip, and every retry failed alike.
|
||||
#[test]
|
||||
fn an_empty_reply_at_a_full_window_points_at_compact() {
|
||||
let message = empty_reply_message(195_000, 200_000);
|
||||
assert!(message.contains("/compact") && message.contains("97% used"), "{message}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_reply_with_room_left_says_retry() {
|
||||
let message = empty_reply_message(100_000, 200_000);
|
||||
assert!(!message.contains("/compact") && message.contains("again to retry"), "{message}");
|
||||
}
|
||||
|
||||
/// Before a first measurement, or for a model whose window is unknown, there is nothing to
|
||||
/// blame the silence on.
|
||||
#[test]
|
||||
fn an_unmeasured_window_is_not_called_full() {
|
||||
assert!(!empty_reply_message(0, 0).contains("/compact"));
|
||||
assert!(!empty_reply_message(5_000, 0).contains("/compact"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,10 +86,14 @@ pub(super) enum KnownEvent {
|
||||
session_id: String,
|
||||
turn_id: String,
|
||||
},
|
||||
CompactionStarted {
|
||||
tokens_before: u64,
|
||||
},
|
||||
CompactionDone {
|
||||
tokens_before: u64,
|
||||
tokens_after: u64,
|
||||
},
|
||||
CompactionStopped,
|
||||
ContextUsage {
|
||||
used_tokens: u32,
|
||||
context_window: u32,
|
||||
|
||||
@@ -12,8 +12,7 @@ use crate::session::EntryId;
|
||||
use crate::workspace::WorkspaceSnapshot;
|
||||
use serde::Serialize;
|
||||
|
||||
/// core→UI stream. `#[non_exhaustive]` so later phases can add variants.
|
||||
/// Unknown variants deserialize into [`Event::Other`] and are preserved verbatim.
|
||||
/// core→UI stream. Unknown variants deserialize into [`Event::Other`] and are preserved verbatim.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
#[serde(tag = "kind", content = "content", rename_all = "snake_case")]
|
||||
#[non_exhaustive]
|
||||
@@ -65,7 +64,6 @@ pub enum Event {
|
||||
session_id: String,
|
||||
/// Persisted entry which owns this message.
|
||||
entry_id: EntryId,
|
||||
/// Committed message content.
|
||||
message: Message,
|
||||
},
|
||||
/// A validated remediation report was extracted after its assistant transcript entry committed.
|
||||
@@ -102,7 +100,6 @@ pub enum Event {
|
||||
},
|
||||
/// A resumed session was loaded with its active-path transcript.
|
||||
SessionLoaded {
|
||||
/// Active session identity.
|
||||
session_id: String,
|
||||
/// Active-path messages paired with their persisted entry IDs.
|
||||
messages: Vec<SessionMessage>,
|
||||
@@ -118,8 +115,12 @@ pub enum Event {
|
||||
/// Opaque turn key accepted by `ReviewTarget::LastTurn`.
|
||||
turn_id: String,
|
||||
},
|
||||
/// A summary began, which can take a minute; `CompactionDone` or `CompactionStopped` ends it.
|
||||
CompactionStarted { tokens_before: u64 },
|
||||
/// Compaction completed.
|
||||
CompactionDone { tokens_before: u64, tokens_after: u64 },
|
||||
/// A started compaction failed or was cancelled; a failure's reason comes as a `Notice` or `Error`.
|
||||
CompactionStopped,
|
||||
/// Token usage after a provider call — lets UIs show context-window fullness.
|
||||
ContextUsage { used_tokens: u32, context_window: u32 },
|
||||
/// Token counts + cost for **one provider call**, as it completes.
|
||||
@@ -546,10 +547,7 @@ pub enum Event {
|
||||
///
|
||||
/// The workspace runtime paces its background GitHub refreshes on it: a terminal nobody has
|
||||
/// looked at for a while is checked less often, and one coming back is checked at once.
|
||||
TerminalFocusChanged {
|
||||
/// Whether it now has focus
|
||||
focused: bool,
|
||||
},
|
||||
TerminalFocusChanged { focused: bool },
|
||||
/// Project-local workspace summary changed.
|
||||
WorkspaceUpdated(Box<WorkspaceSnapshot>),
|
||||
/// Structured display state for a tool call changed.
|
||||
|
||||
@@ -42,7 +42,9 @@ published! {
|
||||
StateChanged => "state_changed",
|
||||
TurnEnded => "turn_ended",
|
||||
LastTurnAvailable => "last_turn_available",
|
||||
CompactionStarted => "compaction_started",
|
||||
CompactionDone => "compaction_done",
|
||||
CompactionStopped => "compaction_stopped",
|
||||
ContextUsage => "context_usage",
|
||||
TurnUsage => "turn_usage",
|
||||
SessionUsage => "session_usage",
|
||||
@@ -227,7 +229,9 @@ pub(super) fn every_event() -> Vec<Event> {
|
||||
Event::StateChanged(EngineState::default()),
|
||||
Event::TurnEnded { reason: StopReason::EndTurn },
|
||||
Event::LastTurnAvailable { session_id: s("s1"), turn_id: s("t1") },
|
||||
Event::CompactionStarted { tokens_before: 2 },
|
||||
Event::CompactionDone { tokens_before: 2, tokens_after: 1 },
|
||||
Event::CompactionStopped,
|
||||
Event::ContextUsage { used_tokens: 1, context_window: 8 },
|
||||
Event::TurnUsage {
|
||||
prompt_tokens: 8,
|
||||
|
||||
@@ -391,7 +391,7 @@ async fn run_via_stdin(spec: &FormatterSpec, path: &Path) -> Result<(), Formatte
|
||||
if std::fs::read(&path)? != input {
|
||||
return Ok(false);
|
||||
}
|
||||
crate::fs_atomic::replace(&path, &formatted).map(|()| true)
|
||||
crate::fs_atomic::replace_keeping_mode(&path, &formatted).map(|()| true)
|
||||
})
|
||||
.await
|
||||
.map_err(|error| io(spec, "write the file")(std::io::Error::other(error)))?
|
||||
|
||||
@@ -62,6 +62,15 @@ pub fn replace_preserving_mode(path: &Path, bytes: &[u8]) -> io::Result<()> {
|
||||
replace_with_mode(path, bytes, existing_mode(path))
|
||||
}
|
||||
|
||||
/// Replace `path` with `bytes` as an editor saves a file: one that exists keeps its mode, an
|
||||
/// executable script included, and a new one gets the platform's default.
|
||||
///
|
||||
/// For the user's own files, which the edit and write tools and the formatter rewrite. A rename
|
||||
/// puts a new file in the old one's place, so a mode not carried over is the new file's default.
|
||||
pub fn replace_keeping_mode(path: &Path, bytes: &[u8]) -> io::Result<()> {
|
||||
write_then_rename(path, bytes, existing_mode(path))
|
||||
}
|
||||
|
||||
/// Replace `path` with `bytes` atomically but without syncing: a reader never sees half of it, and
|
||||
/// a power cut may lose it.
|
||||
///
|
||||
@@ -214,10 +223,18 @@ fn open_staging(path: &Path, mode: Option<u32>) -> io::Result<File> {
|
||||
|
||||
let mut options = OpenOptions::new();
|
||||
options.write(true).create(true).truncate(true);
|
||||
// At creation too, so an owner-only file is never readable even for an instant
|
||||
if let Some(mode) = mode {
|
||||
options.mode(mode);
|
||||
}
|
||||
options.open(path)
|
||||
let file = options.open(path)?;
|
||||
// A mode given at creation is filtered through the umask; set it again so the file ends up
|
||||
// with exactly the mode asked for, a kept group-writable one included
|
||||
if let Some(mode) = mode {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
file.set_permissions(fs::Permissions::from_mode(mode))?;
|
||||
}
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
@@ -252,125 +269,4 @@ fn restrict_dir(_path: &Path) -> io::Result<()> {
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(clippy::unwrap_used)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// An unsynced replace still lands whole and leaves no staging file behind.
|
||||
#[test]
|
||||
fn an_unsynced_replace_lands_whole_and_leaves_nothing_behind() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let target = dir.path().join("cache").join("shard.json");
|
||||
replace_unsynced(&target, b"first").unwrap();
|
||||
replace_unsynced(&target, b"second").unwrap();
|
||||
assert_eq!(fs::read(&target).unwrap(), b"second");
|
||||
let left: Vec<_> = fs::read_dir(target.parent().unwrap()).unwrap().flatten().collect();
|
||||
assert_eq!(left.len(), 1, "only the shard: {left:?}");
|
||||
}
|
||||
|
||||
/// Every directory `create_private_dir` makes is owner-only, not just the last one; one that was
|
||||
/// already there keeps its mode.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn every_directory_created_is_private() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let leaf = dir.path().join("a").join("b").join("c");
|
||||
create_private_dir(&leaf).unwrap();
|
||||
for created in [dir.path().join("a"), dir.path().join("a/b"), leaf] {
|
||||
assert_eq!(mode(&created), 0o700, "{}", created.display());
|
||||
}
|
||||
assert_eq!(mode(dir.path()), 0o755, "an existing parent is left alone");
|
||||
}
|
||||
|
||||
/// A fresh private directory refuses to adopt one that exists.
|
||||
#[test]
|
||||
fn a_new_private_directory_is_never_an_existing_one() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let fresh = dir.path().join("fresh");
|
||||
create_private_dir_new(&fresh).unwrap();
|
||||
let error = create_private_dir_new(&fresh).unwrap_err();
|
||||
assert_eq!(error.kind(), io::ErrorKind::AlreadyExists);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_replacement_lands_and_leaves_no_staging_file_behind() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("state.json");
|
||||
replace(&path, b"first").unwrap();
|
||||
replace(&path, b"second").unwrap();
|
||||
|
||||
assert_eq!(fs::read(&path).unwrap(), b"second");
|
||||
let strays: Vec<_> = fs::read_dir(dir.path())
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.file_name().to_string_lossy().into_owned())
|
||||
.filter(|name| name != "state.json")
|
||||
.collect();
|
||||
assert!(strays.is_empty(), "left behind: {strays:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_parents_are_created() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("a").join("b").join("c.json");
|
||||
replace(&path, b"{}").unwrap();
|
||||
assert_eq!(fs::read(&path).unwrap(), b"{}");
|
||||
}
|
||||
|
||||
/// The staging name has to survive a real extension, or `main.rs` and `main.md` stage to the
|
||||
/// same file and the last writer wins a race nobody knew they were in.
|
||||
#[test]
|
||||
fn two_files_sharing_a_stem_do_not_share_a_staging_name() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let rust = staging_path(&dir.path().join("main.rs"));
|
||||
let markdown = staging_path(&dir.path().join("main.md"));
|
||||
assert_ne!(rust, markdown);
|
||||
assert_eq!(rust.parent(), Some(dir.path()), "staging stays on the target's filesystem");
|
||||
}
|
||||
|
||||
/// Two writers of the *same* file must not collide on the staging name either — one would
|
||||
/// truncate the other's half-written bytes and then rename them into place.
|
||||
#[test]
|
||||
fn concurrent_writers_of_one_file_get_distinct_staging_names() {
|
||||
let path = Path::new("/tmp/luxe/thing.json");
|
||||
assert_ne!(staging_path(path), staging_path(path));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_private_replacement_is_owner_only_from_the_moment_it_exists() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("auth.json");
|
||||
replace_private(&path, b"secret").unwrap();
|
||||
assert_eq!(fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
|
||||
let nested = dir.path().join("private");
|
||||
create_private_dir(&nested).unwrap();
|
||||
assert_eq!(fs::metadata(&nested).unwrap().permissions().mode() & 0o777, 0o700);
|
||||
}
|
||||
|
||||
/// A failed write must not leave a stray staging file, and must not damage what was there.
|
||||
#[test]
|
||||
fn a_failure_leaves_the_previous_contents_alone() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("kept.json");
|
||||
replace(&path, b"original").unwrap();
|
||||
|
||||
// A directory in the target's place: the rename cannot succeed.
|
||||
let blocked = dir.path().join("blocked.json");
|
||||
fs::create_dir(&blocked).unwrap();
|
||||
assert!(replace(&blocked, b"nope").is_err());
|
||||
|
||||
assert_eq!(fs::read(&path).unwrap(), b"original");
|
||||
let strays: Vec<_> = fs::read_dir(dir.path())
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.file_name().to_string_lossy().into_owned())
|
||||
.filter(|name| name.ends_with(".tmp"))
|
||||
.collect();
|
||||
assert!(strays.is_empty(), "left behind: {strays:?}");
|
||||
}
|
||||
}
|
||||
mod tests;
|
||||
|
||||
167
crates/luxe-core/src/fs_atomic/tests.rs
Normal file
167
crates/luxe-core/src/fs_atomic/tests.rs
Normal file
@@ -0,0 +1,167 @@
|
||||
use super::*;
|
||||
|
||||
#[cfg(unix)]
|
||||
fn mode_of(path: &Path) -> u32 {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
fs::metadata(path).unwrap().permissions().mode() & 0o777
|
||||
}
|
||||
|
||||
/// Editing a script keeps it a script. The rename puts a new file where the old one was, so a
|
||||
/// mode not carried over is lost: every edit of an executable left it unrunnable.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_replace_keeping_mode_leaves_a_script_executable() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let script = dir.path().join("run.sh");
|
||||
fs::write(&script, b"#!/bin/sh\n").unwrap();
|
||||
fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).unwrap();
|
||||
replace_keeping_mode(&script, b"#!/bin/sh\necho hi\n").unwrap();
|
||||
assert_eq!(mode_of(&script), 0o755);
|
||||
assert_eq!(fs::read(&script).unwrap(), b"#!/bin/sh\necho hi\n");
|
||||
}
|
||||
|
||||
/// The mode kept is the mode the file had, bit for bit, not one the umask trimmed on the way.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_kept_mode_is_exact_whatever_the_umask() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let shared = dir.path().join("shared");
|
||||
fs::write(&shared, b"a").unwrap();
|
||||
// World-writable on purpose: any umask that is not 000 strips at least one of these bits
|
||||
fs::set_permissions(&shared, fs::Permissions::from_mode(0o777)).unwrap();
|
||||
replace_keeping_mode(&shared, b"b").unwrap();
|
||||
assert_eq!(mode_of(&shared), 0o777);
|
||||
}
|
||||
|
||||
/// A file that did not exist gets the mode any new file would, not the owner-only one secrets get.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_new_file_kept_mode_gets_the_ordinary_default() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let ordinary = dir.path().join("ordinary");
|
||||
File::create(&ordinary).unwrap();
|
||||
let fresh = dir.path().join("fresh");
|
||||
replace_keeping_mode(&fresh, b"new").unwrap();
|
||||
assert_eq!(mode_of(&fresh), mode_of(&ordinary));
|
||||
}
|
||||
|
||||
/// An unsynced replace still lands whole and leaves no staging file behind.
|
||||
#[test]
|
||||
fn an_unsynced_replace_lands_whole_and_leaves_nothing_behind() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let target = dir.path().join("cache").join("shard.json");
|
||||
replace_unsynced(&target, b"first").unwrap();
|
||||
replace_unsynced(&target, b"second").unwrap();
|
||||
assert_eq!(fs::read(&target).unwrap(), b"second");
|
||||
let left: Vec<_> = fs::read_dir(target.parent().unwrap()).unwrap().flatten().collect();
|
||||
assert_eq!(left.len(), 1, "only the shard: {left:?}");
|
||||
}
|
||||
|
||||
/// Every directory `create_private_dir` makes is owner-only, not just the last one; one that was
|
||||
/// already there keeps its mode.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn every_directory_created_is_private() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
fs::set_permissions(dir.path(), fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let leaf = dir.path().join("a").join("b").join("c");
|
||||
create_private_dir(&leaf).unwrap();
|
||||
for created in [dir.path().join("a"), dir.path().join("a/b"), leaf] {
|
||||
assert_eq!(mode(&created), 0o700, "{}", created.display());
|
||||
}
|
||||
assert_eq!(mode(dir.path()), 0o755, "an existing parent is left alone");
|
||||
}
|
||||
|
||||
/// A fresh private directory refuses to adopt one that exists.
|
||||
#[test]
|
||||
fn a_new_private_directory_is_never_an_existing_one() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let fresh = dir.path().join("fresh");
|
||||
create_private_dir_new(&fresh).unwrap();
|
||||
let error = create_private_dir_new(&fresh).unwrap_err();
|
||||
assert_eq!(error.kind(), io::ErrorKind::AlreadyExists);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_replacement_lands_and_leaves_no_staging_file_behind() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("state.json");
|
||||
replace(&path, b"first").unwrap();
|
||||
replace(&path, b"second").unwrap();
|
||||
|
||||
assert_eq!(fs::read(&path).unwrap(), b"second");
|
||||
let strays: Vec<_> = fs::read_dir(dir.path())
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.file_name().to_string_lossy().into_owned())
|
||||
.filter(|name| name != "state.json")
|
||||
.collect();
|
||||
assert!(strays.is_empty(), "left behind: {strays:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_parents_are_created() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("a").join("b").join("c.json");
|
||||
replace(&path, b"{}").unwrap();
|
||||
assert_eq!(fs::read(&path).unwrap(), b"{}");
|
||||
}
|
||||
|
||||
/// The staging name has to survive a real extension, or `main.rs` and `main.md` stage to the
|
||||
/// same file and the last writer wins a race nobody knew they were in.
|
||||
#[test]
|
||||
fn two_files_sharing_a_stem_do_not_share_a_staging_name() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let rust = staging_path(&dir.path().join("main.rs"));
|
||||
let markdown = staging_path(&dir.path().join("main.md"));
|
||||
assert_ne!(rust, markdown);
|
||||
assert_eq!(rust.parent(), Some(dir.path()), "staging stays on the target's filesystem");
|
||||
}
|
||||
|
||||
/// Two writers of the *same* file must not collide on the staging name either — one would
|
||||
/// truncate the other's half-written bytes and then rename them into place.
|
||||
#[test]
|
||||
fn concurrent_writers_of_one_file_get_distinct_staging_names() {
|
||||
let path = Path::new("/tmp/luxe/thing.json");
|
||||
assert_ne!(staging_path(path), staging_path(path));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_private_replacement_is_owner_only_from_the_moment_it_exists() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("auth.json");
|
||||
replace_private(&path, b"secret").unwrap();
|
||||
assert_eq!(fs::metadata(&path).unwrap().permissions().mode() & 0o777, 0o600);
|
||||
|
||||
let nested = dir.path().join("private");
|
||||
create_private_dir(&nested).unwrap();
|
||||
assert_eq!(fs::metadata(&nested).unwrap().permissions().mode() & 0o777, 0o700);
|
||||
}
|
||||
|
||||
/// A failed write must not leave a stray staging file, and must not damage what was there.
|
||||
#[test]
|
||||
fn a_failure_leaves_the_previous_contents_alone() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("kept.json");
|
||||
replace(&path, b"original").unwrap();
|
||||
|
||||
// A directory in the target's place: the rename cannot succeed.
|
||||
let blocked = dir.path().join("blocked.json");
|
||||
fs::create_dir(&blocked).unwrap();
|
||||
assert!(replace(&blocked, b"nope").is_err());
|
||||
|
||||
assert_eq!(fs::read(&path).unwrap(), b"original");
|
||||
let strays: Vec<_> = fs::read_dir(dir.path())
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.map(|entry| entry.file_name().to_string_lossy().into_owned())
|
||||
.filter(|name| name.ends_with(".tmp"))
|
||||
.collect();
|
||||
assert!(strays.is_empty(), "left behind: {strays:?}");
|
||||
}
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
mod standards;
|
||||
|
||||
pub use standards::{FunctionSize, OutlineSource};
|
||||
pub use standards::{FunctionSize, OutlineSource, Scope};
|
||||
|
||||
use crate::formatter::FormatterRegistry;
|
||||
use crate::hook::{Hook, HookCtx, HookEvent, HookOutcome, HookPoint};
|
||||
|
||||
@@ -20,6 +20,24 @@ const MAX_NAMED: usize = 5;
|
||||
pub trait OutlineSource: Send + Sync {
|
||||
/// The functions and methods in `source`, or `None` for a language it cannot parse.
|
||||
fn functions(&self, path: &Path, source: &str) -> Option<Vec<FunctionSize>>;
|
||||
|
||||
/// The named scopes in `source` that can hold code: functions, methods, types and modules,
|
||||
/// not fields or constants. `None` for a language it cannot parse, which is also the default.
|
||||
/// A search labels what it found with the scopes around it.
|
||||
fn scopes(&self, _path: &Path, _source: &str) -> Option<Vec<Scope>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// One named scope and the lines it spans, 0-based and inclusive.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Scope {
|
||||
/// The bare name, `delete` or `QuerySet`
|
||||
pub name: String,
|
||||
/// Its first line, where its declaration starts
|
||||
pub start_line: usize,
|
||||
/// Its last line
|
||||
pub end_line: usize,
|
||||
}
|
||||
|
||||
/// One function's name, qualified by its container where it has one, and its length.
|
||||
|
||||
@@ -72,6 +72,30 @@ pub fn dir_name(path: &Path) -> String {
|
||||
.unwrap_or_else(|| display(path))
|
||||
}
|
||||
|
||||
/// A path as a model wrote it, made one this platform opens: on Windows, Git Bash's `/c/Users/me`
|
||||
/// is `C:/Users/me`.
|
||||
///
|
||||
/// The `bash` tool runs Git Bash there, and its `pwd` prints that form, so a model copies it into
|
||||
/// a `read` that then looked for `C:\c\Users\…` under the project. Elsewhere, and for every other
|
||||
/// path, the input comes back untouched: `/c` is a legal directory on unix.
|
||||
pub fn native(path: &str) -> std::borrow::Cow<'_, str> {
|
||||
native_on(path, cfg!(windows))
|
||||
}
|
||||
|
||||
fn native_on(path: &str, windows: bool) -> std::borrow::Cow<'_, str> {
|
||||
let bytes = path.as_bytes();
|
||||
let drive = windows
|
||||
&& bytes.len() >= 2
|
||||
&& bytes[0] == b'/'
|
||||
&& bytes[1].is_ascii_alphabetic()
|
||||
&& bytes.get(2).is_none_or(|&byte| byte == b'/');
|
||||
if !drive {
|
||||
return std::borrow::Cow::Borrowed(path);
|
||||
}
|
||||
let letter = char::from(bytes[1]).to_ascii_uppercase();
|
||||
std::borrow::Cow::Owned(format!("{letter}:/{}", path.get(3..).unwrap_or("")))
|
||||
}
|
||||
|
||||
/// Rewrite an already-rendered path string to forward slashes.
|
||||
///
|
||||
/// For the cases where the string arrived as a string. On unix this is the identity function on
|
||||
@@ -149,6 +173,29 @@ mod tests {
|
||||
assert_eq!(slashify_on(r"weird\name.txt", false), r"weird\name.txt");
|
||||
}
|
||||
|
||||
/// Git Bash's `pwd` form reaches the file tools on Windows; without this `read` looked for
|
||||
/// `C:\c\Users\…` under the project.
|
||||
#[test]
|
||||
fn a_git_bash_drive_path_opens_on_windows() {
|
||||
assert_eq!(native_on("/c/Users/dev/a.py", true), "C:/Users/dev/a.py");
|
||||
assert_eq!(native_on("/D/work", true), "D:/work", "either case of drive letter");
|
||||
assert_eq!(native_on("/c", true), "C:/", "the drive itself");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_a_single_letter_drive_is_rewritten() {
|
||||
assert_eq!(native_on("/cd/a.py", true), "/cd/a.py", "a longer first part is a folder");
|
||||
assert_eq!(native_on("/tmp/a.py", true), "/tmp/a.py");
|
||||
assert_eq!(native_on("src/a.py", true), "src/a.py", "relative paths are left alone");
|
||||
assert_eq!(native_on("/1/a.py", true), "/1/a.py", "a digit is no drive");
|
||||
}
|
||||
|
||||
/// `/c` is an ordinary directory on unix.
|
||||
#[test]
|
||||
fn a_unix_path_is_never_rewritten() {
|
||||
assert_eq!(native_on("/c/Users/dev", false), "/c/Users/dev");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn display_renders_through_the_platform_rule() {
|
||||
let path: PathBuf = ["src", "main.rs"].iter().collect();
|
||||
|
||||
@@ -112,8 +112,11 @@ fn is_path_tool(tool: &str) -> bool {
|
||||
}
|
||||
|
||||
/// Lexically normalize a path (resolve `.`/`..` without touching the filesystem, so it works for
|
||||
/// not-yet-created write targets). Relative paths are joined onto `root`.
|
||||
/// not-yet-created write targets). Relative paths are joined onto `root`. Translated by
|
||||
/// [`crate::paths::native`] first, as the file tools translate it, so the guard judges the file the
|
||||
/// tool will open.
|
||||
fn normalize(input: &str, root: &Path) -> PathBuf {
|
||||
let input = &*crate::paths::native(input);
|
||||
let joined =
|
||||
if Path::new(input).is_absolute() { PathBuf::from(input) } else { root.join(input) };
|
||||
let mut out = PathBuf::new();
|
||||
|
||||
@@ -147,6 +147,17 @@ fn the_external_target_matches_the_guard_that_fires_on_it() {
|
||||
assert_eq!(external_target("grep", &json!({"pattern": "/etc/hosts"}), &root()), None);
|
||||
}
|
||||
|
||||
/// The guard judges the file the tool opens. The file tools read Git Bash's `/c/…` as a drive
|
||||
/// path on Windows, so a guard that joined it onto the project judged a different file.
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn a_git_bash_path_is_judged_where_the_tool_opens_it() {
|
||||
let root = PathBuf::from(r"C:\Users\dev\project");
|
||||
let target = |path: &str| external_target("edit", &json!({ "path": path }), &root);
|
||||
assert_eq!(target("/c/Users/dev/project/src/x.rs"), None, "inside the project");
|
||||
assert_eq!(target("/c/Windows/x.ini"), Some(PathBuf::from(r"C:\Windows\x.ini")));
|
||||
}
|
||||
|
||||
/// Reading a file outside the project is worth a prompt but is not a change, so only the
|
||||
/// mutating tools reach the ledger.
|
||||
#[test]
|
||||
|
||||
@@ -29,7 +29,7 @@ pub use opener::{
|
||||
};
|
||||
pub use process::{
|
||||
captured_command, detach_from_terminal, group_members, hostname, kill_tree, process_is_alive,
|
||||
take_stdin, GroupMember,
|
||||
take_stdin, GroupMember, TreeGuard,
|
||||
};
|
||||
|
||||
/// Directories a package manager drops executables into, checked after `PATH`.
|
||||
@@ -111,26 +111,105 @@ pub fn resolve(program: &str) -> Option<PathBuf> {
|
||||
}
|
||||
|
||||
/// The runnable file at `base`, trying the platform's executable suffixes.
|
||||
fn runnable(base: &Path) -> Option<PathBuf> {
|
||||
runnable_as(base, cfg!(windows), &executable_suffixes(), is_executable)
|
||||
}
|
||||
|
||||
/// The runnable file at `base` when `windows` decides by extension, and `runs` says whether a
|
||||
/// file exists to be run.
|
||||
///
|
||||
/// On unix a program is its own filename. On Windows the name on disk carries an extension from
|
||||
/// `PATHEXT` — `rg` is `rg.exe`, a shim may be `rg.cmd` — so a bare-name lookup finds nothing at all,
|
||||
/// which reads as "ripgrep is not installed" on a machine where it plainly is.
|
||||
fn runnable(base: &Path) -> Option<PathBuf> {
|
||||
if is_executable(base) {
|
||||
/// `PATHEXT` — `rg` is `rg.exe`, a shim may be `rg.cmd` — and only those run. A bare name is taken
|
||||
/// as given only when it already carries one: npm installs an extensionless shell script beside
|
||||
/// every `.cmd` shim, and taking that file fails with "not a valid Win32 application".
|
||||
fn runnable_as(
|
||||
base: &Path,
|
||||
windows: bool,
|
||||
suffixes: &[String],
|
||||
runs: impl Fn(&Path) -> bool,
|
||||
) -> Option<PathBuf> {
|
||||
if !windows {
|
||||
return runs(base).then(|| base.to_path_buf());
|
||||
}
|
||||
if has_suffix(base, suffixes) && runs(base) {
|
||||
return Some(base.to_path_buf());
|
||||
}
|
||||
if !cfg!(windows) {
|
||||
return None;
|
||||
}
|
||||
for extension in executable_suffixes() {
|
||||
suffixes.iter().find_map(|suffix| {
|
||||
let mut name = base.as_os_str().to_os_string();
|
||||
name.push(&extension);
|
||||
name.push(suffix);
|
||||
let candidate = PathBuf::from(name);
|
||||
if is_executable(&candidate) {
|
||||
return Some(candidate);
|
||||
}
|
||||
runs(&candidate).then_some(candidate)
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `path` already ends in one of `suffixes`, `.CMD` matching `.cmd`.
|
||||
fn has_suffix(path: &Path, suffixes: &[String]) -> bool {
|
||||
let extension = path.extension().and_then(|extension| extension.to_str());
|
||||
extension.is_some_and(|extension| {
|
||||
suffixes.iter().any(|suffix| suffix.trim_start_matches('.').eq_ignore_ascii_case(extension))
|
||||
})
|
||||
}
|
||||
|
||||
/// The Python 3 that runs here, as the words that start it: `["py", "-3"]`, `["python3"]`.
|
||||
///
|
||||
/// On Windows `python3` and `python` can be the Microsoft Store's stand-in, a file that only prints
|
||||
/// "Python was not found", so there each candidate is asked for its version before it counts, the
|
||||
/// `py` launcher first; elsewhere finding the program is enough. Cached, since the answer holds for
|
||||
/// the life of the process and asking costs a process start.
|
||||
pub fn python() -> Option<Vec<String>> {
|
||||
static FOUND: std::sync::OnceLock<Option<Vec<String>>> = std::sync::OnceLock::new();
|
||||
FOUND
|
||||
.get_or_init(|| {
|
||||
python_candidates(cfg!(windows)).into_iter().find(|argv| match cfg!(windows) {
|
||||
true => answers_as_python3(argv),
|
||||
false => resolve(&argv[0]).is_some(),
|
||||
})
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
/// The ways to start Python 3, best first: Windows' `py` launcher is installed with Python itself,
|
||||
/// never as a stand-in, while elsewhere `python` can still be Python 2.
|
||||
fn python_candidates(windows: bool) -> Vec<Vec<String>> {
|
||||
let argv = |words: &[&str]| words.iter().map(|word| word.to_string()).collect::<Vec<_>>();
|
||||
match windows {
|
||||
true => vec![argv(&["py", "-3"]), argv(&["python"]), argv(&["python3"])],
|
||||
false => vec![argv(&["python3"]), argv(&["python"])],
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Whether `argv` starts a working Python 3, by asking it, for at most two seconds.
|
||||
fn answers_as_python3(argv: &[String]) -> bool {
|
||||
use std::process::{Command, Stdio};
|
||||
let Some(program) = resolve(&argv[0]) else { return false };
|
||||
let child = Command::new(program)
|
||||
.args(&argv[1..])
|
||||
.args(["-c", "import sys; print(sys.version_info[0])"])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn();
|
||||
let Ok(child) = child else { return false };
|
||||
wait_briefly(child, std::time::Duration::from_secs(2)).is_some_and(|out| {
|
||||
out.status.success() && String::from_utf8_lossy(&out.stdout).trim() == "3"
|
||||
})
|
||||
}
|
||||
|
||||
/// `child`'s output once it exits, or `None` when it has not within `limit`, killed then.
|
||||
fn wait_briefly(
|
||||
mut child: std::process::Child,
|
||||
limit: std::time::Duration,
|
||||
) -> Option<std::process::Output> {
|
||||
let deadline = std::time::Instant::now() + limit;
|
||||
while child.try_wait().ok()?.is_none() {
|
||||
if std::time::Instant::now() >= deadline {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
return None;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
}
|
||||
child.wait_with_output().ok()
|
||||
}
|
||||
|
||||
/// Windows executable suffixes, from `PATHEXT` when it is set.
|
||||
|
||||
@@ -47,7 +47,7 @@ pub async fn run_bounded_with_input(
|
||||
command.stdin(std::process::Stdio::piped());
|
||||
}
|
||||
let mut child = command.spawn().map_err(BoundedRunError::Spawn)?;
|
||||
let mut reaper = TreeReaper(child.id());
|
||||
let mut reaper = super::TreeGuard::arm(&child);
|
||||
let stdin = child.stdin.take().filter(|_| !input.is_empty());
|
||||
let feed = async {
|
||||
if let Some(mut stdin) = stdin {
|
||||
@@ -60,23 +60,10 @@ pub async fn run_bounded_with_input(
|
||||
let run = async { tokio::join!(feed, child.wait_with_output()).1 };
|
||||
match tokio::time::timeout(deadline, run).await {
|
||||
Ok(result) => {
|
||||
reaper.0 = None;
|
||||
reaper.disarm();
|
||||
result.map_err(BoundedRunError::Wait)
|
||||
}
|
||||
// the reaper takes the tree on the way out
|
||||
Err(_) => Err(BoundedRunError::Timeout { after: deadline }),
|
||||
}
|
||||
}
|
||||
|
||||
/// Kills a process tree when dropped armed: on a timeout, or when the future waiting on the run is
|
||||
/// dropped, as a cancelled turn or an abandoned capture drops it. `kill_on_drop` alone reaches the
|
||||
/// child and nothing it started.
|
||||
struct TreeReaper(Option<u32>);
|
||||
|
||||
impl Drop for TreeReaper {
|
||||
fn drop(&mut self) {
|
||||
if let Some(pid) = self.0.take() {
|
||||
super::kill_tree(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
#[cfg(windows)]
|
||||
mod job;
|
||||
|
||||
/// Kill the process `pid` started *and everything it started*.
|
||||
///
|
||||
/// Every timeout path in Luxe needs this, and none of them can use the child handle alone:
|
||||
@@ -136,22 +139,70 @@ pub fn hostname() -> String {
|
||||
/// probes for one takes its non-interactive path. Codex starts its commands the same way, and
|
||||
/// opencode, Pi and Claude Code get it from Node's `detached`.
|
||||
///
|
||||
/// A no-op on Windows, where there is no controlling terminal to leave and `taskkill /T` walks the
|
||||
/// parent-child links instead.
|
||||
/// On Windows, where there is no controlling terminal to leave, the child gets a console of its own
|
||||
/// that no window shows: one it shared with Luxe was Luxe's terminal tab, and a program that names
|
||||
/// its console, as npm does, renamed the tab. Its tree is reached through [`TreeGuard`]'s job.
|
||||
pub fn detach_from_terminal(command: &mut tokio::process::Command) {
|
||||
// SAFETY: the hook runs in the forked child before `exec`, where only async-signal-safe work is
|
||||
// allowed. `new_session` makes one or two raw syscalls through rustix and touches nothing else:
|
||||
// no allocation, no lock, no state shared with the parent. One of the workspace's two `unsafe`
|
||||
// blocks, beside `take_stdin`'s on Windows; `std`'s own `setsid` for `Command` is not stable.
|
||||
// no allocation, no lock, no state shared with the parent. `std`'s own `setsid` for `Command`
|
||||
// is not stable.
|
||||
#[cfg(unix)]
|
||||
#[allow(unsafe_code)]
|
||||
unsafe {
|
||||
command.pre_exec(new_session);
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
#[cfg(windows)]
|
||||
{
|
||||
// `CREATE_NO_WINDOW`: a console for the child, shown in no window
|
||||
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
|
||||
command.creation_flags(CREATE_NO_WINDOW);
|
||||
}
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
let _ = command;
|
||||
}
|
||||
|
||||
/// A started program's whole tree, killed when dropped armed: on a timeout, and on a cancel, which
|
||||
/// ends a call by dropping it. Disarm it once the program has exited on its own; nothing is killed
|
||||
/// then, and a background process it left keeps running, as one does from a terminal.
|
||||
///
|
||||
/// On unix the tree is the process group [`detach_from_terminal`] made it lead. On Windows it is a
|
||||
/// Job Object the child joins as it starts: `taskkill /T` walks parent links, and a shell killed
|
||||
/// first, as `kill_on_drop` kills it, left its children unlinked and running.
|
||||
pub struct TreeGuard {
|
||||
pid: Option<u32>,
|
||||
#[cfg(windows)]
|
||||
job: Option<job::Job>,
|
||||
}
|
||||
|
||||
impl TreeGuard {
|
||||
/// Arms a guard over `child` and everything it starts from here on.
|
||||
pub fn arm(child: &tokio::process::Child) -> Self {
|
||||
Self {
|
||||
pid: child.id().filter(|pid| *pid != 0),
|
||||
#[cfg(windows)]
|
||||
job: child.raw_handle().and_then(job::Job::holding),
|
||||
}
|
||||
}
|
||||
|
||||
/// The program exited on its own: leave what it started alone. A reaped id can be reused, and
|
||||
/// killing a tree by a recycled id is the one mistake worse than leaking one.
|
||||
pub fn disarm(&mut self) {
|
||||
self.pid = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TreeGuard {
|
||||
fn drop(&mut self) {
|
||||
let Some(pid) = self.pid.take() else { return };
|
||||
#[cfg(windows)]
|
||||
if let Some(job) = &self.job {
|
||||
job.terminate();
|
||||
}
|
||||
kill_tree(pid);
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes stdin for the caller alone, and gives every process Luxe starts the null device instead.
|
||||
///
|
||||
/// `luxe --rpc` reads its protocol from stdin for as long as it runs, and everything it starts
|
||||
|
||||
65
crates/luxe-core/src/program/process/job.rs
Normal file
65
crates/luxe-core/src/program/process/job.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
//! Job — a Windows Job Object holding a command and everything it starts, so a timeout or a cancel
|
||||
//! can end them all.
|
||||
//!
|
||||
//! Windows has no process group to signal. `taskkill /T` walks parent links, and a shell killed
|
||||
//! first, as `kill_on_drop` kills it, leaves its children unlinked and running. A process started
|
||||
//! inside a job stays in it at any depth, so ending the job ends the tree whatever died first.
|
||||
|
||||
use std::os::windows::io::RawHandle;
|
||||
use windows_sys::Win32::Foundation::{CloseHandle, HANDLE};
|
||||
use windows_sys::Win32::System::JobObjects::{
|
||||
AssignProcessToJobObject, CreateJobObjectW, TerminateJobObject,
|
||||
};
|
||||
|
||||
/// A job holding one process and what it starts. Dropping it closes the handle and leaves them
|
||||
/// running, as a command that finished leaves a background process it started; [`Job::terminate`]
|
||||
/// ends them.
|
||||
pub(crate) struct Job(HANDLE);
|
||||
|
||||
// SAFETY: a job handle names a kernel object, and every call on it is thread-safe.
|
||||
#[allow(unsafe_code)]
|
||||
unsafe impl Send for Job {}
|
||||
// SAFETY: as above; `terminate` and `drop` take no state but the handle.
|
||||
#[allow(unsafe_code)]
|
||||
unsafe impl Sync for Job {}
|
||||
|
||||
impl Job {
|
||||
/// A new job holding `process`, or `None` when Windows refuses one, as it can for a process
|
||||
/// already in a job that forbids nesting.
|
||||
#[allow(unsafe_code)]
|
||||
pub(crate) fn holding(process: RawHandle) -> Option<Self> {
|
||||
// SAFETY: `process` is a live process handle lent for this call; the job is created
|
||||
// unnamed with default security, so nothing else can open it, and is closed here if the
|
||||
// process cannot join it.
|
||||
unsafe {
|
||||
let job = CreateJobObjectW(std::ptr::null(), std::ptr::null());
|
||||
if job.is_null() {
|
||||
return None;
|
||||
}
|
||||
if AssignProcessToJobObject(job, process as HANDLE) == 0 {
|
||||
CloseHandle(job);
|
||||
return None;
|
||||
}
|
||||
Some(Self(job))
|
||||
}
|
||||
}
|
||||
|
||||
/// Ends every process in the job.
|
||||
#[allow(unsafe_code)]
|
||||
pub(crate) fn terminate(&self) {
|
||||
// SAFETY: `self.0` is a live job handle this value owns until it drops.
|
||||
unsafe {
|
||||
TerminateJobObject(self.0, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Job {
|
||||
#[allow(unsafe_code)]
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: closed once, here, by its only owner.
|
||||
unsafe {
|
||||
CloseHandle(self.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -329,3 +329,60 @@ fn an_activated_environment_answers_only_inside_its_own_tree() {
|
||||
None => std::env::remove_var("VIRTUAL_ENV"),
|
||||
}
|
||||
}
|
||||
|
||||
/// On Windows the `py` launcher comes first, since it is installed with Python itself and never as
|
||||
/// the Store's stand-in; elsewhere `python3` does, since `python` can still be Python 2.
|
||||
#[test]
|
||||
fn python_is_looked_for_in_the_platform_s_order() {
|
||||
let first = |windows| python_candidates(windows).remove(0).join(" ");
|
||||
assert_eq!(first(true), "py -3");
|
||||
assert_eq!(first(false), "python3");
|
||||
assert!(python_candidates(true).iter().any(|argv| argv == &["python"]), "then plain python");
|
||||
}
|
||||
|
||||
/// A program that is not Python does not pass for one, however it exits.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn something_that_is_not_python_is_not_taken_for_it() {
|
||||
assert!(!answers_as_python3(&["true".to_string()]), "exits 0 but prints no version");
|
||||
assert!(!answers_as_python3(&["luxe-no-such-program".to_string()]));
|
||||
// And a real one does, where one is installed
|
||||
if resolve("python3").is_some() {
|
||||
assert!(answers_as_python3(&["python3".to_string()]), "python3 says it is 3");
|
||||
}
|
||||
}
|
||||
|
||||
fn suffixes() -> Vec<String> {
|
||||
[".COM", ".EXE", ".BAT", ".CMD"].iter().map(|suffix| suffix.to_string()).collect()
|
||||
}
|
||||
|
||||
/// npm installs a language server three ways, an extensionless shell script, a `.cmd` and a `.ps1`.
|
||||
/// Windows runs only the `.cmd`, so it is the one found, though the script shares the bare name.
|
||||
#[test]
|
||||
fn on_windows_the_extensionless_npm_script_is_passed_over_for_its_cmd() {
|
||||
let on_disk =
|
||||
["npm/pyright-langserver", "npm/pyright-langserver.cmd", "npm/pyright-langserver.ps1"];
|
||||
// Windows names files without regard to case, and `PATHEXT` lists `.CMD`
|
||||
let runs =
|
||||
|path: &Path| on_disk.iter().any(|file| file.eq_ignore_ascii_case(path.to_str().unwrap()));
|
||||
let found = runnable_as(Path::new("npm/pyright-langserver"), true, &suffixes(), runs);
|
||||
let found = found.map(|path| path.to_string_lossy().to_lowercase());
|
||||
assert_eq!(found.as_deref(), Some("npm/pyright-langserver.cmd"));
|
||||
}
|
||||
|
||||
/// A name that already carries a runnable extension is taken as given, whatever its case.
|
||||
#[test]
|
||||
fn on_windows_a_name_with_its_extension_is_taken_as_given() {
|
||||
let runs = |path: &Path| path == Path::new("bin/rg.EXE");
|
||||
let found = runnable_as(Path::new("bin/rg.EXE"), true, &suffixes(), runs);
|
||||
assert_eq!(found, Some(PathBuf::from("bin/rg.EXE")));
|
||||
}
|
||||
|
||||
/// Elsewhere a program is its own filename, extension or none.
|
||||
#[test]
|
||||
fn off_windows_a_program_is_its_own_filename() {
|
||||
let runs = |path: &Path| path == Path::new("bin/pyright-langserver");
|
||||
let found = runnable_as(Path::new("bin/pyright-langserver"), false, &suffixes(), runs);
|
||||
assert_eq!(found, Some(PathBuf::from("bin/pyright-langserver")));
|
||||
assert_eq!(runnable_as(Path::new("bin/missing"), false, &suffixes(), runs), None);
|
||||
}
|
||||
|
||||
@@ -225,7 +225,9 @@ const PATTERNS: [(&str, &str, SecretConfidence, &str); 11] = [
|
||||
r"AIza[0-9A-Za-z_\-]{35}",
|
||||
"[REDACTED_GOOGLE_KEY]",
|
||||
SecretConfidence::Format,
|
||||
"AIzaabcdefghijklmnopqrstuvwxyz012345678",
|
||||
// Joined at compile time so the source holds no key-shaped run: a Google key has no
|
||||
// checksum, so secret scanners flag any literal of its shape, invented ones included
|
||||
concat!("AIza", "abcdefghijklmnopqrstuvwxyz012345678"),
|
||||
),
|
||||
(
|
||||
r"(?:sk|rk)_(?:live|test)_[0-9A-Za-z]{10,}",
|
||||
|
||||
@@ -221,6 +221,8 @@ fn walk_threads() -> usize {
|
||||
/// (`src/**/*.ts`) and is anchored so it matches at any depth. `**` crosses directory boundaries and
|
||||
/// `*` does not, which is the dialect every tool description in this repo promises.
|
||||
pub fn matcher(pattern: &str) -> Result<PathMatcher, globset::Error> {
|
||||
// On Windows `src\*.py` means a folder, not an escaped `*`; elsewhere a backslash stays an escape
|
||||
let pattern = &crate::paths::slashify(pattern);
|
||||
let spans_directories = pattern.contains('/');
|
||||
let compiled =
|
||||
if spans_directories { Glob::new(&anchor(pattern))? } else { Glob::new(pattern)? };
|
||||
@@ -267,6 +269,16 @@ fn anchor(pattern: &str) -> String {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A model on Windows writes `src\*.py`, and globset read the backslash as an escape, so the
|
||||
/// pattern matched nothing.
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn a_backslash_in_a_windows_glob_is_a_folder() {
|
||||
let matcher = matcher(r"src\*.py").unwrap();
|
||||
assert!(matcher.is_match(Path::new("src/a.py")));
|
||||
assert!(!matcher.is_match(Path::new("lib/a.py")));
|
||||
}
|
||||
|
||||
/// A walk that stops at its ceiling must say so, or a search over a huge tree reads as complete.
|
||||
#[test]
|
||||
fn a_walk_that_hits_its_ceiling_says_so() {
|
||||
|
||||
@@ -300,17 +300,23 @@ async fn compacting_on_request_ignores_the_threshold_and_says_what_it_freed() {
|
||||
);
|
||||
|
||||
action_tx.send(Action::CompactNow { focus: Some("the API decisions".into()) }).await.unwrap();
|
||||
let mut freed = None;
|
||||
let (mut started, mut freed) = (None, None);
|
||||
while let Ok(Some(event)) =
|
||||
tokio::time::timeout(std::time::Duration::from_secs(10), event_rx.recv()).await
|
||||
{
|
||||
if let Event::CompactionDone { tokens_before, tokens_after } = event {
|
||||
freed = Some((tokens_before, tokens_after));
|
||||
break;
|
||||
match event {
|
||||
Event::CompactionStarted { tokens_before } => started = Some(tokens_before),
|
||||
Event::CompactionDone { tokens_before, tokens_after } => {
|
||||
freed = Some((tokens_before, tokens_after));
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let (before, after) = freed.expect("a compaction_done event");
|
||||
assert!(before > after, "it reports what it freed: {before} -> {after}");
|
||||
// The summary call can take a minute, and without the start a client shows nothing meanwhile
|
||||
assert_eq!(started, Some(before), "a compaction_started came first, naming what it replaces");
|
||||
|
||||
let calls = calls.lock().await;
|
||||
let summarization =
|
||||
|
||||
@@ -141,6 +141,80 @@ impl Provider for CutOffSummaryProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/// A session on a summarizer that always stops short, `turns` turns in.
|
||||
async fn cut_off_session(
|
||||
turns: usize,
|
||||
tmp: &tempfile::TempDir,
|
||||
) -> (tokio::sync::mpsc::Sender<Action>, tokio::sync::mpsc::Receiver<Event>, Vec<Event>) {
|
||||
let mut store = JsonlSessionStore::new(tmp.path().join("sessions"));
|
||||
let session_id = store.create("/tmp/test").unwrap();
|
||||
let calls = Arc::new(Mutex::new(Vec::new()));
|
||||
let provider = Arc::new(CutOffSummaryProvider { calls, cut_off: CutOff::Length });
|
||||
let (action_tx, mut event_rx) = Engine::spawn(config(store, session_id, provider, tmp.path()));
|
||||
let mut seen = Vec::new();
|
||||
for turn in 0..turns {
|
||||
let prompt = bulky(&format!("TURN-{turn}"));
|
||||
let submit =
|
||||
Action::SubmitUserMessage { content: prompt, images: vec![], remediation: None };
|
||||
action_tx.send(submit).await.unwrap();
|
||||
seen.extend(crate::fixtures::events_until_turn_end(&mut event_rx).await);
|
||||
}
|
||||
(action_tx, event_rx, seen)
|
||||
}
|
||||
|
||||
/// `/compact` says why the summary it ran was refused.
|
||||
///
|
||||
/// The break this catches: a summarizer that ran out of output room failed every time, and
|
||||
/// `/compact` reported it as "Nothing to compact — the conversation already fits", at 100% of a
|
||||
/// million-token window, so the reader had no way to tell what was wrong.
|
||||
#[tokio::test]
|
||||
async fn compacting_on_request_reports_why_the_summary_was_refused() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (action_tx, mut event_rx, _) = cut_off_session(6, &tmp).await;
|
||||
|
||||
action_tx.send(Action::CompactNow { focus: None }).await.unwrap();
|
||||
let seen = crate::fixtures::events_until(&mut event_rx, "the compaction's outcome", |e| {
|
||||
matches!(e, Event::Error(_) | Event::CompactionDone { .. })
|
||||
})
|
||||
.await;
|
||||
|
||||
let Some(Event::Error(message)) = seen.last() else { panic!("no error: {seen:?}") };
|
||||
assert!(message.contains("stopped short"), "the real reason is given: {message}");
|
||||
assert!(!message.contains("Nothing to compact"), "not a no-op: {message}");
|
||||
// A client showing the compaction as under way needs its end, failure included
|
||||
let position = |wanted: fn(&Event) -> bool| seen.iter().position(wanted);
|
||||
let started = position(|e| matches!(e, Event::CompactionStarted { .. }));
|
||||
let stopped = position(|e| matches!(e, Event::CompactionStopped));
|
||||
assert!(started.is_some() && started < stopped, "started, then stopped: {seen:?}");
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// A failed automatic compaction is announced, and so is the breaker turning it off.
|
||||
///
|
||||
/// The break this catches: every summary failed for a whole session and only the log said so;
|
||||
/// the reader found out when the window was full and the model could no longer answer at all.
|
||||
#[tokio::test]
|
||||
async fn a_failing_automatic_compaction_tells_the_reader_and_says_when_it_stops() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (action_tx, _event_rx, seen) = cut_off_session(TURNS + 5, &tmp).await;
|
||||
|
||||
let notices: Vec<&String> = seen
|
||||
.iter()
|
||||
.filter_map(|event| match event {
|
||||
Event::Notice(message) => Some(message),
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
let breaker = luxe_core::compaction::MAX_COMPACTION_FAILURES as usize;
|
||||
let failures = notices.iter().filter(|notice| notice.contains("stopped short")).count();
|
||||
assert_eq!(failures, breaker, "one notice per failure, until it stops: {notices:?}");
|
||||
assert!(
|
||||
notices.iter().any(|notice| notice.contains("/compact")),
|
||||
"the last one says it is off and how to retry: {notices:?}"
|
||||
);
|
||||
action_tx.send(Action::Close).await.unwrap();
|
||||
}
|
||||
|
||||
/// A summary the summarizer did not finish must not replace the conversation it summarizes.
|
||||
///
|
||||
/// The break this catches: the reply loop took an error, a length stop or a dropped stream as
|
||||
|
||||
@@ -113,6 +113,11 @@ fn the_frames_a_client_cannot_work_without_are_pinned() {
|
||||
Event::ActionRejected { kind: None, reason: "line is not JSON".into() },
|
||||
r#"{"kind":"action_rejected","content":{"reason":"line is not JSON"}}"#,
|
||||
),
|
||||
(
|
||||
Event::CompactionStarted { tokens_before: 180_000 },
|
||||
r#"{"kind":"compaction_started","content":{"tokens_before":180000}}"#,
|
||||
),
|
||||
(Event::CompactionStopped, r#"{"kind":"compaction_stopped"}"#),
|
||||
(
|
||||
Event::PermissionRequested {
|
||||
id: request_id("perm:1"),
|
||||
@@ -558,7 +563,9 @@ fn every_known_event_roundtrips() {
|
||||
Event::StateChanged(EngineState::default()),
|
||||
Event::TurnEnded { reason: StopReason::EndTurn },
|
||||
Event::LastTurnAvailable { session_id: "session-1".into(), turn_id: "turn-1".into() },
|
||||
Event::CompactionStarted { tokens_before: 2_000 },
|
||||
Event::CompactionDone { tokens_before: 2_000, tokens_after: 1_000 },
|
||||
Event::CompactionStopped,
|
||||
Event::ContextUsage { used_tokens: 1_000, context_window: 8_000 },
|
||||
Event::TurnUsage {
|
||||
prompt_tokens: 800,
|
||||
|
||||
@@ -11,7 +11,7 @@ homepage.workspace = true
|
||||
# Provides a native Debug Adapter Protocol client + the `debug` tool.
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.0" }
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.2" }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -220,7 +220,19 @@ pub fn builtin_adapters() -> Vec<ConfiguredAdapter> {
|
||||
fn default_adapters() -> Vec<ConfiguredAdapter> {
|
||||
let customs: Vec<CustomAdapter> = serde_json::from_str(DEFAULT_ADAPTERS_JSON)
|
||||
.expect("bundled default-debug-adapters.json must be valid");
|
||||
customs.iter().map(ConfiguredAdapter::from_custom).collect()
|
||||
customs.iter().map(ConfiguredAdapter::from_custom).map(with_local_python).collect()
|
||||
}
|
||||
|
||||
/// The bundle's `python3` replaced by the Python that runs here, `py -3` on most Windows machines,
|
||||
/// where `python3` is often the Store's stand-in and debugging would fail at every launch. Kept as
|
||||
/// written when no Python answers, so the adapter reads as missing rather than as something else.
|
||||
fn with_local_python(mut adapter: ConfiguredAdapter) -> ConfiguredAdapter {
|
||||
if adapter.spec.command.first().map(String::as_str) == Some("python3") {
|
||||
if let Some(python) = luxe_core::program::python() {
|
||||
adapter.spec.command.splice(..1, python);
|
||||
}
|
||||
}
|
||||
adapter
|
||||
}
|
||||
|
||||
/// Whether any debug adapter — user-configured or built-in — is actually installed.
|
||||
@@ -230,10 +242,62 @@ fn default_adapters() -> Vec<ConfiguredAdapter> {
|
||||
/// this so that cost is paid only where the capability exists. Nothing is hidden by doing so —
|
||||
/// unlike deferring a working tool, an absent adapter means there is no debugging to offer.
|
||||
pub fn any_adapter_available(custom: &[ConfiguredAdapter]) -> bool {
|
||||
custom
|
||||
.iter()
|
||||
.chain(default_adapters().iter())
|
||||
.any(|c| c.spec.command.first().is_some_and(|program| on_path(program)))
|
||||
custom.iter().chain(default_adapters().iter()).any(|c| unready(&c.spec.command).is_none())
|
||||
}
|
||||
|
||||
/// Why an adapter cannot start.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Unready {
|
||||
/// Its program does not resolve. Args: `(program)`.
|
||||
Missing(String),
|
||||
/// Its Python cannot import the module it runs. Args: `(program, module)`.
|
||||
NoModule(String, String),
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Unready {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Missing(program) => {
|
||||
write!(formatter, "debug adapter '{program}' not found on PATH")
|
||||
}
|
||||
Self::NoModule(program, module) => write!(
|
||||
formatter,
|
||||
"the {module} module is not installed for {program}: pip install {module}"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Why an adapter's `command` cannot start, or `None` when it can.
|
||||
///
|
||||
/// Its program has to resolve, and a Python module adapter (`python -m debugpy.adapter`) has to be
|
||||
/// able to import its module: an interpreter without it starts, then fails every launch with an
|
||||
/// error that never says to install anything.
|
||||
pub fn unready(command: &[String]) -> Option<Unready> {
|
||||
let program = command.first()?;
|
||||
if !on_path(program) {
|
||||
return Some(Unready::Missing(program.clone()));
|
||||
}
|
||||
let at = command.iter().position(|word| word == "-m")?;
|
||||
let module = command.get(at + 1)?.split('.').next()?;
|
||||
(!imports(&command[..at], module))
|
||||
.then(|| Unready::NoModule(program.clone(), module.to_string()))
|
||||
}
|
||||
|
||||
/// Whether `python` (its argv) can import `module`.
|
||||
fn imports(python: &[String], module: &str) -> bool {
|
||||
let Some(program) = python.first().and_then(|program| luxe_core::program::resolve(program))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
std::process::Command::new(program)
|
||||
.args(&python[1..])
|
||||
.args(["-c", &format!("import {module}")])
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.status()
|
||||
.is_ok_and(|status| status.success())
|
||||
}
|
||||
|
||||
/// Resolves the adapter for a target program.
|
||||
@@ -279,13 +343,9 @@ pub fn resolve(
|
||||
}
|
||||
|
||||
fn ensure_installed(spec: AdapterSpec) -> Result<AdapterSpec, DapError> {
|
||||
if on_path(&spec.command[0]) {
|
||||
Ok(spec)
|
||||
} else {
|
||||
Err(DapError::AdapterUnavailable(format!(
|
||||
"debug adapter '{}' not found on PATH",
|
||||
spec.command[0]
|
||||
)))
|
||||
match unready(&spec.command) {
|
||||
None => Ok(spec),
|
||||
Some(why) => Err(DapError::AdapterUnavailable(why.to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -405,4 +465,24 @@ mod tests {
|
||||
});
|
||||
assert!(any_adapter_available(&[real]), "an installed command counts as available");
|
||||
}
|
||||
|
||||
/// An adapter is ready when its program resolves and, for a Python module adapter, its module
|
||||
/// imports; a missing module is named with the command that installs it.
|
||||
#[test]
|
||||
fn readiness_names_a_missing_program_or_module() {
|
||||
let command =
|
||||
|words: &[&str]| words.iter().map(|word| word.to_string()).collect::<Vec<_>>();
|
||||
let missing = unready(&command(&["luxe-no-such-adapter"]));
|
||||
assert_eq!(missing, Some(Unready::Missing("luxe-no-such-adapter".into())));
|
||||
assert_eq!(unready(&[installed_program()]), None, "no module to check");
|
||||
let Some(python) = luxe_core::program::python() else { return };
|
||||
let mut absent = python.clone();
|
||||
absent.extend(command(&["-m", "luxe_no_such_module.adapter"]));
|
||||
let Some(Unready::NoModule(_, module)) = unready(&absent) else { panic!("{absent:?}") };
|
||||
assert_eq!(module, "luxe_no_such_module");
|
||||
assert!(unready(&absent).unwrap().to_string().contains("pip install luxe_no_such_module"));
|
||||
let mut present = python;
|
||||
present.extend(command(&["-m", "json.tool"]));
|
||||
assert_eq!(unready(&present), None, "a module the standard library has imports");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ homepage.workspace = true
|
||||
# Provides a generic LSP client (rust-analyzer first).
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.0" }
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.2" }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -247,6 +247,17 @@ fn symbol_kind(def_kind: &str, container_kind: Option<&str>) -> u32 {
|
||||
}
|
||||
}
|
||||
|
||||
/// The kinds that can hold code, and so name where a line sits: not fields, variants or constants.
|
||||
const SCOPE_KINDS: [u32; 7] = [
|
||||
kinds::FUNCTION,
|
||||
kinds::METHOD,
|
||||
kinds::CLASS,
|
||||
kinds::STRUCT,
|
||||
kinds::ENUM,
|
||||
kinds::INTERFACE,
|
||||
kinds::MODULE,
|
||||
];
|
||||
|
||||
/// Function lengths for the size standards the edit hook holds, from the same outline
|
||||
/// `module_report` draws.
|
||||
pub struct SyntaxOutline;
|
||||
@@ -272,6 +283,25 @@ impl luxe_core::hooks::file_changed::OutlineSource for SyntaxOutline {
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn scopes(
|
||||
&self,
|
||||
path: &Path,
|
||||
source: &str,
|
||||
) -> Option<Vec<luxe_core::hooks::file_changed::Scope>> {
|
||||
let symbols = outline(path, source)?;
|
||||
Some(
|
||||
symbols
|
||||
.into_iter()
|
||||
.filter(|symbol| SCOPE_KINDS.contains(&symbol.kind))
|
||||
.map(|symbol| luxe_core::hooks::file_changed::Scope {
|
||||
name: symbol.name,
|
||||
start_line: symbol.start_line as usize,
|
||||
end_line: symbol.end_line as usize,
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -337,6 +367,19 @@ mod tests {
|
||||
assert_eq!(method.kind, kinds::METHOD);
|
||||
}
|
||||
|
||||
/// The scopes a search labels with are the ones that hold code, with the lines they span: a
|
||||
/// class and its method, not the attribute beside them.
|
||||
#[test]
|
||||
fn scopes_are_the_code_holding_symbols_and_their_spans() {
|
||||
use luxe_core::hooks::file_changed::{OutlineSource, Scope};
|
||||
let src = "class QuerySet:\n model = None\n\n def delete(self):\n return 1\n";
|
||||
let scopes = SyntaxOutline.scopes(Path::new("query.py"), src).expect("python parses");
|
||||
let scope =
|
||||
|name: &str, start_line, end_line| Scope { name: name.into(), start_line, end_line };
|
||||
assert_eq!(scopes, vec![scope("QuerySet", 0, 4), scope("delete", 3, 4)]);
|
||||
assert_eq!(SyntaxOutline.scopes(Path::new("notes.md"), "# hi"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_language_returns_none() {
|
||||
assert!(outline(Path::new("notes.md"), "# hi").is_none());
|
||||
|
||||
@@ -13,7 +13,7 @@ homepage.workspace = true
|
||||
# no-HTTP rule forbids in core itself. The low-level stdio transport stays in luxe-core.
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.0" }
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.2" }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -11,7 +11,7 @@ homepage.workspace = true
|
||||
# Provides LLM provider adapters (OpenAI-compat, Anthropic, Gemini).
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.0" }
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.2" }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -11,7 +11,7 @@ homepage.workspace = true
|
||||
# Provides tool implementations (read, write, edit, bash, grep, glob, etc.).
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.0" }
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.2" }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
@@ -437,22 +437,33 @@ mod tree_teardown {
|
||||
}
|
||||
|
||||
/// A shell that backgrounds a grandchild and waits on it. Killing the shell alone leaves the
|
||||
/// grandchild touching `marker` — which is how a survivor makes itself known.
|
||||
/// grandchild touching `marker` — which is how a survivor makes itself known. Forward slashes,
|
||||
/// which Git Bash takes on Windows, where a backslash would be an escape.
|
||||
fn script(marker: &std::path::Path) -> String {
|
||||
format!("(for _ in $(seq 1 40); do touch {}; sleep 0.25; done) & wait", marker.display())
|
||||
let marker = marker.display().to_string().replace('\\', "/");
|
||||
format!("(for _ in $(seq 1 40); do touch '{marker}'; sleep 0.25; done) & wait")
|
||||
}
|
||||
|
||||
/// Whether anything is still touching `marker` after it is removed.
|
||||
/// Whether anything is still touching `marker`, the loop, once a touch that was already under
|
||||
/// way has had its moment.
|
||||
///
|
||||
/// The kill reaches the whole group at once, but a `touch` forked as it lands can finish one
|
||||
/// write after it, and on macOS's slower teardown that write can follow the first clear. One
|
||||
/// straggling write is not a survivor; a loop still running touches again within a quarter
|
||||
/// second of the second clear.
|
||||
async fn survivor_still_running(marker: &std::path::Path) -> bool {
|
||||
let _ = std::fs::remove_file(marker);
|
||||
tokio::time::sleep(Duration::from_millis(1_200)).await;
|
||||
tokio::time::sleep(Duration::from_millis(300)).await;
|
||||
let _ = std::fs::remove_file(marker);
|
||||
tokio::time::sleep(Duration::from_millis(1_000)).await;
|
||||
marker.exists()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_dropped_call_takes_the_whole_tree_with_it() {
|
||||
if !cfg!(unix) {
|
||||
return; // the script is a POSIX one
|
||||
// The script is a POSIX one: Git Bash runs it on Windows, where the tree is a job's
|
||||
if !luxe_core::program::has_posix_shell() {
|
||||
return;
|
||||
}
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
let marker = dir.path().join("alive");
|
||||
@@ -472,7 +483,7 @@ mod tree_teardown {
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_timed_out_call_takes_the_whole_tree_with_it() {
|
||||
if !cfg!(unix) {
|
||||
if !luxe_core::program::has_posix_shell() {
|
||||
return;
|
||||
}
|
||||
let dir = tempfile::tempdir().expect("tempdir");
|
||||
|
||||
@@ -510,14 +510,17 @@ async fn under_a_terminal() {
|
||||
/// A suspended command could never finish, and the old advice was to give it longer. A terminal
|
||||
/// can no longer suspend one (see `a_command_that_touches_the_terminal_still_finishes`), so this
|
||||
/// stops one by hand.
|
||||
///
|
||||
/// The stop is sent a few times over the first quarter second: on macOS a `SIGSTOP` sent the instant
|
||||
/// a child is spawned can miss it, and the report then rightly lists a sleeper, not a stopped one.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn a_suspended_command_is_called_stuck_not_slow() {
|
||||
let tmp_dir = tempfile::tempdir().unwrap();
|
||||
let ctx = test_context(tmp_dir.path());
|
||||
let result = BashTool::new()
|
||||
.run(serde_json::json!({"command": "sleep 9 & kill -STOP $!; sleep 8", "timeout": 1}), &ctx)
|
||||
.await;
|
||||
let command = "sleep 9 & for _ in 1 2 3 4 5; do kill -STOP $!; sleep 0.05; done; sleep 8";
|
||||
let result =
|
||||
BashTool::new().run(serde_json::json!({"command": command, "timeout": 1}), &ctx).await;
|
||||
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
|
||||
assert!(text.contains("State T means suspended, not slow"), "{text}");
|
||||
assert!(!text.contains("pass `timeout`"), "more time cannot help a suspended command: {text}");
|
||||
|
||||
@@ -374,7 +374,7 @@ impl Tool for EditTool {
|
||||
Err(problem) => return ToolResultData::error(problem.message()),
|
||||
};
|
||||
|
||||
let full_path = cx.cwd.join(&request.path);
|
||||
let full_path = cx.cwd.join(&*luxe_core::paths::native(&request.path));
|
||||
|
||||
if let Err(refusal) = self.check_read_guard(cx, &full_path, &request.path) {
|
||||
return refusal;
|
||||
@@ -428,7 +428,9 @@ impl Tool for EditTool {
|
||||
let diff = crate::diff_preview::ranges_diff(&content, &preview);
|
||||
let result = apply(&content, &planned);
|
||||
|
||||
if let Err(problem) = luxe_core::fs_atomic::replace(&full_path, result.as_bytes()) {
|
||||
if let Err(problem) =
|
||||
luxe_core::fs_atomic::replace_keeping_mode(&full_path, result.as_bytes())
|
||||
{
|
||||
warn!(path = %full_path.display(), error = %problem, "failed to write edited file");
|
||||
return ToolResultData::error(format!(
|
||||
"Error writing file '{}': {problem}",
|
||||
|
||||
@@ -78,6 +78,23 @@ async fn a_reformatted_edit_shows_the_file_as_it_is_now() {
|
||||
assert!(text.contains("+1 fn b() {}"), "{text}");
|
||||
}
|
||||
|
||||
/// An edited script stays executable. Saving by rename replaced the file with a new one at the
|
||||
/// default mode, so every edit of a script left it unrunnable.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn an_edited_script_stays_executable() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let script = dir.path().join("run.sh");
|
||||
std::fs::write(&script, "#!/bin/sh\necho one\n").unwrap();
|
||||
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let args = json!({"path": "run.sh", "edits": [{"oldText": "one", "newText": "two"}]});
|
||||
let result = EditTool::new().run(args, &test_context(dir.path())).await;
|
||||
assert!(!result.is_error, "{:?}", result.content);
|
||||
let mode = std::fs::metadata(&script).unwrap().permissions().mode() & 0o777;
|
||||
assert_eq!(mode, 0o755, "still executable after the edit");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn edit_tool_basic() {
|
||||
let tmp_dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
|
||||
@@ -4,16 +4,21 @@
|
||||
//! it is opened, and the approved files are scanned in parallel with `regex`. An external `rg`
|
||||
//! could not do the middle step, which is the one that keeps denied content out of the answer.
|
||||
//!
|
||||
//! The result is bounded `file:line:text`, with optional context lines and a glob filter — or,
|
||||
//! where the question is coarser than the lines, the files that match or a count per file.
|
||||
//! The rows are bounded `file:line:text`, with optional context lines and a glob filter — or,
|
||||
//! where the question is coarser than the lines, the files that match or a count per file. Lines
|
||||
//! reach the model grouped under their file: the path once rather than on every line. Asked to
|
||||
//! (`scopes`), each run of matches is labeled with the function or type around it, where an
|
||||
//! outline can say, so where a match sits needs no read to find out.
|
||||
|
||||
use async_trait::async_trait;
|
||||
use luxe_core::hooks::file_changed::{OutlineSource, Scope};
|
||||
use luxe_core::{ContentBlock, Tool, ToolContext, ToolResultData, ToolSchema};
|
||||
use serde_json::json;
|
||||
use std::path::{Path, PathBuf};
|
||||
use tracing::warn;
|
||||
|
||||
mod guarded;
|
||||
mod present;
|
||||
#[cfg(test)]
|
||||
mod read_policy_tests;
|
||||
|
||||
@@ -21,7 +26,9 @@ mod read_policy_tests;
|
||||
fn grep_schema() -> ToolSchema {
|
||||
ToolSchema {
|
||||
name: "grep".to_string(),
|
||||
description: "Search file *contents* by regex. Returns `file:line:text`, bounded \
|
||||
description: "Search file *contents* by regex. Returns each matching file's path once \
|
||||
with its `line:text` rows under it; `scopes: true` also names the function \
|
||||
or type around each match, as `[QuerySet › delete]`. Bounded \
|
||||
— a broad pattern says it was capped instead of flooding your context. Case \
|
||||
is sensitive unless the pattern says otherwise: prefix `(?i)`. Skips ignored \
|
||||
paths (`.gitignore`, `.git/`, build output); set `all: true` to include \
|
||||
@@ -50,7 +57,7 @@ fn grep_schema() -> ToolSchema {
|
||||
"outputMode": {
|
||||
"type": "string",
|
||||
"enum": ["content", "files", "count"],
|
||||
"description": "What to return: 'content' (file:line:text, the default), \
|
||||
"description": "What to return: 'content' (matching lines under each file, the default), \
|
||||
'files' (one path per matching file), or 'count' (file:count \
|
||||
of matching lines). Prefer 'files' when you only need to know \
|
||||
where to look."
|
||||
@@ -76,6 +83,13 @@ fn grep_schema() -> ToolSchema {
|
||||
"type": "boolean",
|
||||
"description": "Return the lines that do NOT match the pattern. Default false."
|
||||
},
|
||||
"scopes": {
|
||||
"type": "boolean",
|
||||
"description": "Name the function or type each match sits in, for code a \
|
||||
parser reads. Costs a row per change of scope; worth it \
|
||||
when *where* a match is matters, as for call sites. \
|
||||
Default false."
|
||||
},
|
||||
"all": {
|
||||
"type": "boolean",
|
||||
"description": "Include ignored files (build output, vendored deps). Default false."
|
||||
@@ -148,6 +162,44 @@ impl OutputMode {
|
||||
}
|
||||
}
|
||||
|
||||
/// One row a search found, kept whole until the answer is laid out.
|
||||
#[derive(Debug, Clone)]
|
||||
struct Hit {
|
||||
/// The file, relative to where the search ran
|
||||
file: std::sync::Arc<str>,
|
||||
kind: HitKind,
|
||||
/// 1-based line, for a match or a context row
|
||||
line: usize,
|
||||
/// The line itself, or the number of matching lines for [`HitKind::Count`]
|
||||
text: String,
|
||||
/// The scopes around a match, outermost first, `QuerySet › delete`, where an outline can say
|
||||
scope: Option<String>,
|
||||
}
|
||||
|
||||
/// What a [`Hit`] is.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum HitKind {
|
||||
Match,
|
||||
Context,
|
||||
/// A file with a match, in the files mode
|
||||
File,
|
||||
/// A file's number of matching lines, in the count mode
|
||||
Count,
|
||||
}
|
||||
|
||||
impl Hit {
|
||||
/// The row as ripgrep prints it, which the caps measure: `file:line:text`, `file-line-text`
|
||||
/// for context, a bare path, or `file:count`.
|
||||
fn flat(&self) -> String {
|
||||
match self.kind {
|
||||
HitKind::Match => format!("{}:{}:{}", self.file, self.line, self.text),
|
||||
HitKind::Context => format!("{}-{}-{}", self.file, self.line, self.text),
|
||||
HitKind::File => self.file.to_string(),
|
||||
HitKind::Count => format!("{}:{}", self.file, self.text),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One parsed search.
|
||||
///
|
||||
/// `limit` is deliberately *not* consulted while searching. Stopping early would make the total the
|
||||
@@ -162,17 +214,22 @@ struct Search<'a> {
|
||||
after: usize,
|
||||
all: bool,
|
||||
invert: bool,
|
||||
/// Label each match with the scopes around it, where an outline can read the file
|
||||
scopes: bool,
|
||||
mode: OutputMode,
|
||||
limit: usize,
|
||||
}
|
||||
|
||||
/// What `search` finds under `cwd`, unguarded, as the tests ask it.
|
||||
/// What `search` finds under `cwd`, unguarded and as ripgrep would print it, as the tests ask it.
|
||||
#[cfg(test)]
|
||||
fn native_search(cwd: &Path, search: &Search<'_>) -> Result<Vec<String>, String> {
|
||||
native_search_files(cwd, search, crate::walk::files(cwd, search.path, search.all), || false)
|
||||
let paths = crate::walk::files(cwd, search.path, search.all);
|
||||
let hits = native_search_files(cwd, search, paths, None, || false)?;
|
||||
Ok(hits.iter().map(Hit::flat).collect())
|
||||
}
|
||||
|
||||
/// Scans `paths` (relative to `cwd`) for `search`, in parallel, rows in path order.
|
||||
/// Scans `paths` (relative to `cwd`) for `search`, in parallel, rows in path order, each match
|
||||
/// labeled with its scopes where `outline` can parse the file.
|
||||
///
|
||||
/// Each worker takes a contiguous run of the sorted paths and the runs are joined in order, so the
|
||||
/// answer reads the same however many threads produced it. `stopped` is checked between files.
|
||||
@@ -180,8 +237,9 @@ fn native_search_files(
|
||||
cwd: &Path,
|
||||
search: &Search<'_>,
|
||||
paths: Vec<PathBuf>,
|
||||
outline: Option<&dyn OutlineSource>,
|
||||
stopped: impl Fn() -> bool + Sync,
|
||||
) -> Result<Vec<String>, String> {
|
||||
) -> Result<Vec<Hit>, String> {
|
||||
let regex = regex::Regex::new(search.pattern)
|
||||
.map_err(|error| format!("'{}' is not a valid regex: {error}", search.pattern))?;
|
||||
let prefilter = prefilter(search);
|
||||
@@ -199,7 +257,7 @@ fn native_search_files(
|
||||
let workers = std::thread::available_parallelism().map_or(1, usize::from).min(16);
|
||||
let run = paths.len().div_ceil(workers).max(1);
|
||||
// a worker is `Err` when it panicked, `Ok(None)` when it was stopped
|
||||
let scanned: Vec<std::thread::Result<Option<Vec<String>>>> = std::thread::scope(|scope| {
|
||||
let scanned: Vec<std::thread::Result<Option<Vec<Hit>>>> = std::thread::scope(|scope| {
|
||||
let handles: Vec<_> = paths
|
||||
.chunks(run)
|
||||
.map(|chunk| {
|
||||
@@ -210,7 +268,8 @@ fn native_search_files(
|
||||
if stopped() {
|
||||
return None;
|
||||
}
|
||||
rows.extend(search_file(cwd, relative, (regex, prefilter), search));
|
||||
let matcher = (regex, prefilter);
|
||||
rows.extend(search_file(cwd, relative, matcher, search, outline));
|
||||
}
|
||||
Some(rows)
|
||||
})
|
||||
@@ -223,9 +282,9 @@ fn native_search_files(
|
||||
|
||||
/// The workers' rows joined in order; a crashed worker is a failure, a stopped one a cancel.
|
||||
fn combine(
|
||||
scanned: Vec<std::thread::Result<Option<Vec<String>>>>,
|
||||
scanned: Vec<std::thread::Result<Option<Vec<Hit>>>>,
|
||||
stopped: bool,
|
||||
) -> Result<Vec<String>, String> {
|
||||
) -> Result<Vec<Hit>, String> {
|
||||
if scanned.iter().any(Result::is_err) {
|
||||
return Err("a search worker failed; nothing it scanned is reported".into());
|
||||
}
|
||||
@@ -269,14 +328,15 @@ fn has_lone_carriage_return(text: &str) -> bool {
|
||||
bytes.iter().enumerate().any(|(at, byte)| *byte == b'\r' && bytes.get(at + 1) != Some(&b'\n'))
|
||||
}
|
||||
|
||||
/// The rows one file contributes, in the shape ripgrep prints them: `file:line:text` for a match,
|
||||
/// `file-line-text` for context, or the file's name or count for the coarser modes.
|
||||
/// The rows one file contributes: its matching and context lines, or its name or count for the
|
||||
/// coarser modes. A match is labeled with the scopes around it where `outline` parses the file.
|
||||
fn search_file(
|
||||
cwd: &Path,
|
||||
relative: &Path,
|
||||
(regex, prefilter): (®ex::Regex, Option<®ex::Regex>),
|
||||
search: &Search<'_>,
|
||||
) -> Vec<String> {
|
||||
outline: Option<&dyn OutlineSource>,
|
||||
) -> Vec<Hit> {
|
||||
let Ok(bytes) = std::fs::read(cwd.join(relative)) else { return Vec::new() };
|
||||
if bytes.iter().take(BINARY_SNIFF_BYTES).any(|byte| *byte == 0) {
|
||||
return Vec::new();
|
||||
@@ -287,7 +347,7 @@ fn search_file(
|
||||
if filtered.is_some_and(|prefilter| !prefilter.is_match(&text)) {
|
||||
return Vec::new();
|
||||
}
|
||||
let name = luxe_core::paths::display(relative);
|
||||
let file: std::sync::Arc<str> = luxe_core::paths::display(relative).into();
|
||||
let lines: Vec<&str> = text.lines().collect();
|
||||
let matched: Vec<usize> = lines
|
||||
.iter()
|
||||
@@ -298,45 +358,78 @@ fn search_file(
|
||||
if matched.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let row = |kind, line, text: String| Hit { file: file.clone(), kind, line, text, scope: None };
|
||||
match search.mode {
|
||||
OutputMode::Files => vec![name],
|
||||
OutputMode::Count => vec![format!("{name}:{}", matched.len())],
|
||||
OutputMode::Files => vec![row(HitKind::File, 0, String::new())],
|
||||
OutputMode::Count => vec![row(HitKind::Count, 0, matched.len().to_string())],
|
||||
OutputMode::Content => {
|
||||
// Decide *which* lines to print and *what each one is* separately, then print each
|
||||
// once. Doing it per match instead conflates the two: a match inside an earlier
|
||||
// match's trailing context gets emitted as context and never as the answer.
|
||||
let mut shown = vec![false; lines.len()];
|
||||
for index in &matched {
|
||||
let first = index.saturating_sub(search.before);
|
||||
let last = (index + search.after + 1).min(lines.len());
|
||||
shown[first..last].iter_mut().for_each(|slot| *slot = true);
|
||||
}
|
||||
let is_match = |index: usize| matched.binary_search(&index).is_ok();
|
||||
lines
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(index, _)| shown[*index])
|
||||
.map(|(index, line)| {
|
||||
// `-` marks a context line; the caller's parsers and the model both read the
|
||||
// `:` rows as the answer
|
||||
let separator = if is_match(index) { ':' } else { '-' };
|
||||
format!("{name}{separator}{}{separator}{line}", index + 1)
|
||||
let outline = outline.filter(|_| search.scopes);
|
||||
let scopes = outline.and_then(|outline| outline.scopes(relative, &text));
|
||||
line_hits(&lines, &matched, search)
|
||||
.map(|(index, is_match)| {
|
||||
let kind = if is_match { HitKind::Match } else { HitKind::Context };
|
||||
let mut hit = row(kind, index + 1, lines[index].to_string());
|
||||
hit.scope =
|
||||
scopes.as_deref().filter(|_| is_match).and_then(|s| scope_label(s, index));
|
||||
hit
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The lines to show and whether each is a match, in order.
|
||||
///
|
||||
/// Decides *which* lines to print and *what each one is* separately, then yields each once. Doing
|
||||
/// it per match instead conflates the two: a match inside an earlier match's trailing context gets
|
||||
/// emitted as context and never as the answer.
|
||||
fn line_hits<'a>(
|
||||
lines: &'a [&str],
|
||||
matched: &'a [usize],
|
||||
search: &Search<'_>,
|
||||
) -> impl Iterator<Item = (usize, bool)> + 'a {
|
||||
let mut shown = vec![false; lines.len()];
|
||||
for index in matched {
|
||||
let first = index.saturating_sub(search.before);
|
||||
let last = (index + search.after + 1).min(lines.len());
|
||||
shown[first..last].iter_mut().for_each(|slot| *slot = true);
|
||||
}
|
||||
(0..lines.len())
|
||||
.filter(move |index| shown[*index])
|
||||
.map(move |index| (index, matched.binary_search(&index).is_ok()))
|
||||
}
|
||||
|
||||
/// The scopes around 0-based line `line`, outermost first, as `QuerySet › delete`.
|
||||
///
|
||||
/// A scope whose declaration starts on the line is left out: the line itself names it, so
|
||||
/// `def delete(self):` is labeled `QuerySet`, not `QuerySet › delete`.
|
||||
fn scope_label(scopes: &[Scope], line: usize) -> Option<String> {
|
||||
let mut around: Vec<&Scope> =
|
||||
scopes.iter().filter(|scope| scope.start_line < line && line <= scope.end_line).collect();
|
||||
around.sort_by_key(|scope| (scope.start_line, std::cmp::Reverse(scope.end_line)));
|
||||
let names: Vec<&str> = around.iter().map(|scope| scope.name.as_str()).collect();
|
||||
(!names.is_empty()).then(|| names.join(" › "))
|
||||
}
|
||||
|
||||
/// The `grep` tool implementation.
|
||||
pub struct GrepTool {
|
||||
/// Shared read tracker; a file whose lines a search printed is recorded as read
|
||||
tracker: Option<std::sync::Arc<luxe_core::read_tracker::ReadTracker>>,
|
||||
/// What names the scopes a match sits in; without it lines come unlabeled
|
||||
outline: Option<std::sync::Arc<dyn OutlineSource>>,
|
||||
}
|
||||
|
||||
impl GrepTool {
|
||||
/// Create a new grep tool instance.
|
||||
pub fn new() -> Self {
|
||||
Self { tracker: None }
|
||||
Self { tracker: None, outline: None }
|
||||
}
|
||||
|
||||
/// Attach the outline that labels each match with the function or type around it, a parser
|
||||
/// that lives outside this crate.
|
||||
pub fn with_outline(mut self, outline: std::sync::Arc<dyn OutlineSource>) -> Self {
|
||||
self.outline = Some(outline);
|
||||
self
|
||||
}
|
||||
|
||||
/// Attach the shared read tracker, so the lines a search showed satisfy the edit guards the
|
||||
@@ -380,6 +473,8 @@ impl Tool for GrepTool {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod scope_tests;
|
||||
#[cfg(test)]
|
||||
mod search_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -48,6 +48,7 @@ pub(super) fn parse_search(args: &serde_json::Value) -> Result<Search<'_>, Strin
|
||||
after: side("after"),
|
||||
all: args.get("all").and_then(|value| value.as_bool()).unwrap_or(false),
|
||||
invert: args.get("invert").and_then(|value| value.as_bool()).unwrap_or(false),
|
||||
scopes: args.get("scopes").and_then(|value| value.as_bool()).unwrap_or(false),
|
||||
// camelCase is advertised; snake_case is what models reach for, and a silently ignored
|
||||
// mode returns the expensive answer
|
||||
mode: OutputMode::parse(
|
||||
@@ -106,16 +107,15 @@ async fn scan(
|
||||
args: serde_json::Value,
|
||||
cx: &ToolContext,
|
||||
paths: Vec<PathBuf>,
|
||||
stopped: Arc<AtomicBool>,
|
||||
) -> Result<String, String> {
|
||||
(outline, stopped): (Option<Arc<dyn OutlineSource>>, Arc<AtomicBool>),
|
||||
) -> Result<Vec<Hit>, String> {
|
||||
let cwd = cx.cwd.clone();
|
||||
let cancelled = cx.cancelled.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let search = parse_search(&args)?;
|
||||
super::native_search_files(&cwd, &search, paths, || {
|
||||
super::native_search_files(&cwd, &search, paths, outline.as_deref(), || {
|
||||
stopped.load(Ordering::Relaxed) || *cancelled.borrow()
|
||||
})
|
||||
.map(|rows| rows.join("\n"))
|
||||
})
|
||||
.await
|
||||
.map_err(|error| format!("grep scan worker failed: {error}"))?
|
||||
@@ -141,7 +141,7 @@ pub(super) async fn run(
|
||||
Ok(plan) => plan,
|
||||
Err(error) => return ToolResultData::error(error),
|
||||
};
|
||||
let found = scan(args.clone(), cx, plan.allowed, stopped).await;
|
||||
let found = scan(args.clone(), cx, plan.allowed, (tool.outline.clone(), stopped)).await;
|
||||
let mut result = finish(tool, cx, &search, found);
|
||||
if plan.denied + plan.asks > 0 {
|
||||
result.content.push(ContentBlock::Text(format!(
|
||||
@@ -159,20 +159,24 @@ pub(super) fn finish(
|
||||
tool: &GrepTool,
|
||||
cx: &ToolContext,
|
||||
search: &Search<'_>,
|
||||
found: Result<String, String>,
|
||||
found: Result<Vec<Hit>, String>,
|
||||
) -> ToolResultData {
|
||||
match found {
|
||||
Ok(matches) if matches.trim().is_empty() => ToolResultData::text("No matches found"),
|
||||
Ok(matches) => {
|
||||
let bounded = crate::output_limit::cap_lines_within(
|
||||
&matches,
|
||||
Ok(hits) if hits.is_empty() => ToolResultData::text("No matches found"),
|
||||
Ok(hits) if search.mode == OutputMode::Content => {
|
||||
let (text, shown) = super::present::content(&hits, search);
|
||||
record_matches(tool, cx, search, &hits[..shown]);
|
||||
ToolResultData::text(text)
|
||||
}
|
||||
Ok(hits) => {
|
||||
let rows: Vec<String> = hits.iter().map(Hit::flat).collect();
|
||||
ToolResultData::text(crate::output_limit::cap_lines_within(
|
||||
&rows.join("\n"),
|
||||
search.limit,
|
||||
GREP_MAX_CHARS,
|
||||
search.mode.unit(),
|
||||
search.mode.hint(),
|
||||
);
|
||||
record_matches(tool, cx, search, &bounded);
|
||||
ToolResultData::text(bounded)
|
||||
))
|
||||
}
|
||||
Err(problem) => {
|
||||
warn!(pattern = search.pattern, error = %problem, "grep failed");
|
||||
@@ -181,15 +185,18 @@ pub(super) fn finish(
|
||||
}
|
||||
}
|
||||
|
||||
fn record_matches(tool: &GrepTool, cx: &ToolContext, search: &Search<'_>, bounded: &str) {
|
||||
let (Some(tracker), OutputMode::Content) = (&tool.tracker, search.mode) else { return };
|
||||
/// Records as read every file a line of which the answer showed, the way a `read` of it would be.
|
||||
fn record_matches(tool: &GrepTool, cx: &ToolContext, search: &Search<'_>, shown: &[Hit]) {
|
||||
let Some(tracker) = &tool.tracker else { return };
|
||||
let searched = cx.cwd.join(search.path);
|
||||
let shown = if searched.is_file() {
|
||||
vec![searched]
|
||||
} else {
|
||||
crate::bash::seen::files_in_rows(bounded, &cx.cwd)
|
||||
let files: Vec<PathBuf> = match searched.is_file() {
|
||||
true => vec![searched],
|
||||
false => shown.iter().map(|hit| cx.cwd.join(&*hit.file)).collect(),
|
||||
};
|
||||
for path in shown {
|
||||
tracker.record(&cx.session_id, &path);
|
||||
let mut recorded = std::collections::HashSet::new();
|
||||
for path in files {
|
||||
if recorded.insert(path.clone()) {
|
||||
tracker.record(&cx.session_id, &path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
106
crates/luxe-tools/src/grep/present.rs
Normal file
106
crates/luxe-tools/src/grep/present.rs
Normal file
@@ -0,0 +1,106 @@
|
||||
//! Present — a content search laid out for reading: each file named once, its lines under it, and
|
||||
//! the scope around a run of matches where an outline can say.
|
||||
//!
|
||||
//! The rows are counted and capped as ripgrep would print them, `file:line:text`, so the bounds and
|
||||
//! the totals are the ones the other modes keep. Only the layout changes. A path repeated on every
|
||||
//! line is a large share of a broad search, and the function a match sits in is what the next
|
||||
//! `read` would have gone to find out.
|
||||
|
||||
use super::{Hit, HitKind, Search, GREP_MAX_CHARS};
|
||||
|
||||
/// The capped, grouped answer, and how many of `hits` it shows.
|
||||
pub(super) fn content(hits: &[Hit], search: &Search<'_>) -> (String, usize) {
|
||||
let lengths = hits.iter().map(|hit| hit.flat().chars().count());
|
||||
let kept = crate::output_limit::lines_within(lengths, search.limit, GREP_MAX_CHARS);
|
||||
let shown = grouped(&hits[..kept], search.before + search.after > 0);
|
||||
let text = match kept < hits.len() {
|
||||
true => crate::output_limit::with_note(
|
||||
&shown,
|
||||
hits.len() - kept,
|
||||
hits.len(),
|
||||
search.mode.unit(),
|
||||
search.mode.hint(),
|
||||
),
|
||||
false => crate::output_limit::clamp(&shown),
|
||||
};
|
||||
(text, kept)
|
||||
}
|
||||
|
||||
/// `hits` under their files: the path, then `line:text` for a match and `line-text` for context,
|
||||
/// ` [Scope › name]` before a match whose scope differs from the last one's, and `--` where
|
||||
/// context leaves a gap.
|
||||
fn grouped(hits: &[Hit], context: bool) -> String {
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
let mut previous: Option<&Hit> = None;
|
||||
let mut scope: Option<&str> = None;
|
||||
for hit in hits {
|
||||
match previous {
|
||||
Some(last) if last.file == hit.file => {
|
||||
if context && hit.line > last.line + 1 {
|
||||
out.push("--".into());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
out.push(hit.file.to_string());
|
||||
scope = None;
|
||||
}
|
||||
}
|
||||
if hit.kind == HitKind::Match && hit.scope.as_deref() != scope {
|
||||
out.push(scope_row(hit.scope.as_deref()));
|
||||
scope = hit.scope.as_deref();
|
||||
}
|
||||
let separator = if hit.kind == HitKind::Match { ':' } else { '-' };
|
||||
out.push(format!("{}{separator}{}", hit.line, hit.text));
|
||||
previous = Some(hit);
|
||||
}
|
||||
out.join("\n")
|
||||
}
|
||||
|
||||
/// ` [QuerySet › delete]`, or ` [top level]` for a match that has left the scopes before it.
|
||||
fn scope_row(scope: Option<&str>) -> String {
|
||||
format!(" [{}]", scope.unwrap_or("top level"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn hit(file: &str, kind: HitKind, line: usize, text: &str, scope: Option<&str>) -> Hit {
|
||||
let (file, text, scope) = (file.into(), text.to_string(), scope.map(String::from));
|
||||
Hit { file, kind, line, text, scope }
|
||||
}
|
||||
|
||||
/// Each file is named once, its lines under it, and a scope row comes only where it changes.
|
||||
#[test]
|
||||
fn matches_group_under_their_file_with_a_row_per_change_of_scope() {
|
||||
let hits = [
|
||||
hit("a.py", HitKind::Match, 3, " def delete(self):", Some("QuerySet")),
|
||||
hit("a.py", HitKind::Match, 9, " def delete_all(self):", Some("QuerySet")),
|
||||
hit("a.py", HitKind::Match, 20, "def delete(): pass", None),
|
||||
hit("b.py", HitKind::Match, 5, " delete()", Some("Model › save")),
|
||||
];
|
||||
let expected = "a.py\n [QuerySet]\n3: def delete(self):\n9: def delete_all(self):\n \
|
||||
[top level]\n20:def delete(): pass\nb.py\n [Model › save]\n5: delete()";
|
||||
assert_eq!(grouped(&hits, false), expected);
|
||||
}
|
||||
|
||||
/// A file no outline can read gets no scope rows at all, rather than a `[top level]` that
|
||||
/// would claim to know.
|
||||
#[test]
|
||||
fn an_unlabeled_file_gets_no_scope_rows() {
|
||||
let hits = [hit("notes.md", HitKind::Match, 1, "delete it", None)];
|
||||
assert_eq!(grouped(&hits, false), "notes.md\n1:delete it");
|
||||
}
|
||||
|
||||
/// Context lines take `-`, and a gap between runs of context is marked `--`.
|
||||
#[test]
|
||||
fn context_rows_take_a_dash_and_a_gap_is_marked() {
|
||||
let hits = [
|
||||
hit("a.rs", HitKind::Match, 2, "needle", None),
|
||||
hit("a.rs", HitKind::Context, 3, "after", None),
|
||||
hit("a.rs", HitKind::Match, 9, "needle", None),
|
||||
];
|
||||
assert_eq!(grouped(&hits, true), "a.rs\n2:needle\n3-after\n--\n9:needle");
|
||||
assert_eq!(grouped(&hits, false), "a.rs\n2:needle\n3-after\n9:needle", "no context asked");
|
||||
}
|
||||
}
|
||||
77
crates/luxe-tools/src/grep/scope_tests.rs
Normal file
77
crates/luxe-tools/src/grep/scope_tests.rs
Normal file
@@ -0,0 +1,77 @@
|
||||
use super::*;
|
||||
use luxe_core::hooks::file_changed::FunctionSize;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::{mpsc, watch};
|
||||
|
||||
/// A stand-in for the tree-sitter outline: in a `.py` file, a class over lines 1–9 (0-based 0–8)
|
||||
/// with a method over lines 3–6, and nothing it can read in any other file.
|
||||
struct OneClass;
|
||||
|
||||
impl OutlineSource for OneClass {
|
||||
fn functions(&self, _: &Path, _: &str) -> Option<Vec<FunctionSize>> {
|
||||
None
|
||||
}
|
||||
|
||||
fn scopes(&self, path: &Path, _: &str) -> Option<Vec<Scope>> {
|
||||
let scope =
|
||||
|name: &str, start_line, end_line| Scope { name: name.into(), start_line, end_line };
|
||||
(path.extension()? == "py").then(|| vec![scope("QuerySet", 0, 8), scope("delete", 2, 5)])
|
||||
}
|
||||
}
|
||||
|
||||
fn context(cwd: &Path) -> ToolContext {
|
||||
let (event_tx, _event_rx) = mpsc::channel(64);
|
||||
let (_cancel_tx, cancel_rx) = watch::channel(false);
|
||||
ToolContext {
|
||||
cwd: cwd.to_path_buf(),
|
||||
session_id: "test-session".to_string(),
|
||||
cancelled: Arc::new(cancel_rx),
|
||||
event_tx,
|
||||
hook_ctx: None,
|
||||
question_broker: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Each match is labeled with the scopes around it, outermost first, and only where the label
|
||||
/// changes. The definition line takes its parent's label, since it names itself; a file no outline
|
||||
/// can read gets no label at all; and the path comes once, above its lines.
|
||||
#[tokio::test]
|
||||
async fn a_match_is_labeled_with_the_scopes_around_it() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let python =
|
||||
"class QuerySet:\n \n def delete(self):\n self.delete_batch()\n \
|
||||
return self\n \n \nOTHER = 1\n \ndef delete():\n pass\n";
|
||||
std::fs::write(dir.path().join("a.py"), python).unwrap();
|
||||
std::fs::write(dir.path().join("notes.md"), "delete it\n").unwrap();
|
||||
let tool = GrepTool::new().with_outline(Arc::new(OneClass));
|
||||
let args = json!({"pattern": "delete", "scopes": true});
|
||||
let result = tool.run(args, &context(dir.path())).await;
|
||||
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
|
||||
let expected = "a.py\n [QuerySet]\n3: def delete(self):\n [QuerySet › delete]\n4: \
|
||||
self.delete_batch()\n [top level]\n10:def delete():\nnotes.md\n1:delete it";
|
||||
assert_eq!(text, expected);
|
||||
}
|
||||
|
||||
/// Labels cost a row each, so a search without `scopes` gets the grouped lines alone, outline or
|
||||
/// not.
|
||||
#[tokio::test]
|
||||
async fn a_search_is_unlabeled_unless_it_asks() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::fs::write(dir.path().join("a.py"), "class A:\n \n def delete(self):\n").unwrap();
|
||||
let tool = GrepTool::new().with_outline(Arc::new(OneClass));
|
||||
let result = tool.run(json!({"pattern": "delete"}), &context(dir.path())).await;
|
||||
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
|
||||
assert_eq!(text, "a.py\n3: def delete(self):");
|
||||
}
|
||||
|
||||
/// The coarse modes have no lines to label, so they ask nothing of the outline.
|
||||
#[tokio::test]
|
||||
async fn the_files_mode_carries_no_scopes() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::fs::write(dir.path().join("a.py"), "class A:\n def delete(self): pass\n").unwrap();
|
||||
let tool = GrepTool::new().with_outline(Arc::new(OneClass));
|
||||
let args = json!({"pattern": "delete", "outputMode": "files"});
|
||||
let result = tool.run(args, &context(dir.path())).await;
|
||||
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
|
||||
assert_eq!(text.trim(), "a.py");
|
||||
}
|
||||
@@ -17,6 +17,7 @@ fn base(pattern: &str) -> Search<'_> {
|
||||
after: 0,
|
||||
all: false,
|
||||
invert: false,
|
||||
scopes: false,
|
||||
mode: OutputMode::Content,
|
||||
limit: 500,
|
||||
}
|
||||
@@ -147,10 +148,20 @@ fn the_prefilter_never_hides_a_matching_line() {
|
||||
/// A worker that crashed is a failure the caller hears about; one that was stopped is a cancel.
|
||||
#[test]
|
||||
fn a_crashed_worker_is_a_failure_not_a_cancel() {
|
||||
let rows = |row: &str| Ok(Some(vec![row.to_string()]));
|
||||
let crashed: std::thread::Result<Option<Vec<String>>> = Err(Box::new("boom"));
|
||||
let rows = |file: &str| {
|
||||
let hit = Hit {
|
||||
file: file.into(),
|
||||
kind: HitKind::File,
|
||||
line: 0,
|
||||
text: String::new(),
|
||||
scope: None,
|
||||
};
|
||||
Ok(Some(vec![hit]))
|
||||
};
|
||||
let crashed: std::thread::Result<Option<Vec<Hit>>> = Err(Box::new("boom"));
|
||||
let failed = combine(vec![rows("a"), crashed], false).unwrap_err();
|
||||
assert!(failed.contains("worker failed"), "{failed}");
|
||||
assert_eq!(combine(vec![rows("a"), Ok(None)], false).unwrap_err(), "grep cancelled");
|
||||
assert_eq!(combine(vec![rows("a"), rows("b")], false).unwrap(), ["a", "b"]);
|
||||
let joined = combine(vec![rows("a"), rows("b")], false).unwrap();
|
||||
assert_eq!(joined.iter().map(Hit::flat).collect::<Vec<_>>(), ["a", "b"]);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,13 @@ fn test_context(tmp_dir: &std::path::Path) -> ToolContext {
|
||||
}
|
||||
}
|
||||
|
||||
/// The matching-line rows of a content answer, `12:text`, not the paths or scope rows around them.
|
||||
fn match_rows(text: &str) -> usize {
|
||||
text.lines()
|
||||
.filter(|line| line.split_once(':').is_some_and(|(n, _)| n.parse::<usize>().is_ok()))
|
||||
.count()
|
||||
}
|
||||
|
||||
/// A plain content search of `.`, for a test to vary one field of.
|
||||
fn search<'a>(pattern: &'a str) -> Search<'a> {
|
||||
Search {
|
||||
@@ -27,6 +34,7 @@ fn search<'a>(pattern: &'a str) -> Search<'a> {
|
||||
after: 0,
|
||||
all: false,
|
||||
invert: false,
|
||||
scopes: false,
|
||||
mode: OutputMode::Content,
|
||||
limit: GREP_MAX_LINES,
|
||||
}
|
||||
@@ -246,7 +254,7 @@ async fn a_zero_limit_is_an_absent_limit() {
|
||||
.run(json!({"pattern": "needle", "limit": 0}), &test_context(dir.path()))
|
||||
.await;
|
||||
let ContentBlock::Text(text) = &result.content[0] else { panic!("expected text") };
|
||||
assert_eq!(text.lines().count(), 20, "every match, not none: {text}");
|
||||
assert_eq!(match_rows(text), 20, "every match, not none: {text}");
|
||||
assert!(!text.contains("not shown"), "and nothing was cut: {text}");
|
||||
}
|
||||
|
||||
@@ -282,16 +290,12 @@ async fn a_limit_can_only_tighten_the_cap() {
|
||||
|
||||
let sampled = GrepTool::new().run(json!({"pattern": "needle", "limit": 5}), &cx).await;
|
||||
let ContentBlock::Text(text) = &sampled.content[0] else { panic!("expected text") };
|
||||
assert_eq!(text.lines().filter(|line| line.contains("many.txt")).count(), 5, "{text}");
|
||||
assert_eq!(match_rows(text), 5, "{text}");
|
||||
assert!(text.contains("295 more matching lines not shown (300 total)"), "{text}");
|
||||
|
||||
let greedy = GrepTool::new().run(json!({"pattern": "needle", "limit": 5000}), &cx).await;
|
||||
let ContentBlock::Text(text) = &greedy.content[0] else { panic!("expected text") };
|
||||
assert_eq!(
|
||||
text.lines().filter(|line| line.contains("many.txt")).count(),
|
||||
GREP_MAX_LINES,
|
||||
"a limit past the ceiling is the ceiling: {text}"
|
||||
);
|
||||
assert_eq!(match_rows(text), GREP_MAX_LINES, "a limit past the ceiling is the ceiling: {text}");
|
||||
}
|
||||
|
||||
/// The count a capped result reports is the count that exists, not the count that was kept.
|
||||
|
||||
@@ -49,6 +49,9 @@ pub mod write;
|
||||
/// The `todo` tool requires a store root (e.g. `.luxe/todos`). Pass `None` to skip registering it.
|
||||
/// `web` configures `web_search` and `web_fetch`; [`web::WebConfig::from_env`] is the usual start.
|
||||
///
|
||||
/// `outline` labels each `grep` match with the function or type around it; it is a parser this
|
||||
/// crate cannot depend on (tree-sitter lives in `luxe-lsp`), and `None` leaves matches unlabeled.
|
||||
///
|
||||
/// Returns the shared [`luxe_core::read_tracker::ReadTracker`] backing the read-before-edit /
|
||||
/// staleness / blind-write guards, so the caller can also wire it into the LSP funnel reads
|
||||
/// (`read_symbol`, `module_report`) that see file content but live in a crate that can't depend
|
||||
@@ -57,6 +60,7 @@ pub fn register_builtins(
|
||||
registry: &mut luxe_core::ToolRegistry,
|
||||
todo_store_root: Option<std::path::PathBuf>,
|
||||
web: web::WebConfig,
|
||||
outline: Option<std::sync::Arc<dyn luxe_core::hooks::file_changed::OutlineSource>>,
|
||||
) -> std::sync::Arc<luxe_core::read_tracker::ReadTracker> {
|
||||
use std::sync::Arc;
|
||||
// One shared read tracker backs the read-before-edit + staleness + blind-write guards across
|
||||
@@ -76,7 +80,11 @@ pub fn register_builtins(
|
||||
registry.register(Arc::new(write::WriteTool::new().with_tracker(read_tracker.clone())));
|
||||
registry.register(Arc::new(edit::EditTool::new().with_tracker(read_tracker.clone())));
|
||||
registry.register(Arc::new(bash::BashTool::new().with_tracker(read_tracker.clone())));
|
||||
registry.register(Arc::new(grep::GrepTool::new().with_tracker(read_tracker.clone())));
|
||||
let grep = grep::GrepTool::new().with_tracker(read_tracker.clone());
|
||||
registry.register(Arc::new(match outline {
|
||||
Some(outline) => grep.with_outline(outline),
|
||||
None => grep,
|
||||
}));
|
||||
registry.register(Arc::new(glob::GlobTool::new()));
|
||||
registry.register(Arc::new(question::QuestionTool::new(question::QuestionConfig::default())));
|
||||
registry.register(Arc::new(skill::SkillTool::new(skill::SkillConfig)));
|
||||
|
||||
@@ -37,24 +37,54 @@ pub(crate) fn cap_lines_within(
|
||||
hint: &str,
|
||||
) -> String {
|
||||
let total = text.lines().count();
|
||||
let mut kept_lines = 0usize;
|
||||
let mut spent = 0usize;
|
||||
for line in text.lines().take(max_lines) {
|
||||
let cost = line.chars().count().saturating_add(1);
|
||||
if kept_lines > 0 && spent.saturating_add(cost) > max_chars {
|
||||
break;
|
||||
}
|
||||
spent = spent.saturating_add(cost);
|
||||
kept_lines += 1;
|
||||
}
|
||||
let kept_lines =
|
||||
lines_within(text.lines().map(|line| line.chars().count()), max_lines, max_chars);
|
||||
if kept_lines >= total {
|
||||
return clamp_bytes(text);
|
||||
}
|
||||
let kept = text.lines().take(kept_lines).collect::<Vec<_>>().join("\n");
|
||||
let remaining = total - kept_lines;
|
||||
with_note(&kept, total - kept_lines, total, unit, hint)
|
||||
}
|
||||
|
||||
/// How many lines, given each one's length in characters, fit under both `max_lines` and
|
||||
/// `max_chars`; at least one when there is any, however long it is.
|
||||
///
|
||||
/// The cut [`cap_lines_within`] makes, for a tool that lays its lines out after choosing them.
|
||||
pub(crate) fn lines_within(
|
||||
lengths: impl IntoIterator<Item = usize>,
|
||||
max_lines: usize,
|
||||
max_chars: usize,
|
||||
) -> usize {
|
||||
let mut kept = 0usize;
|
||||
let mut spent = 0usize;
|
||||
for length in lengths.into_iter().take(max_lines) {
|
||||
let cost = length.saturating_add(1);
|
||||
if kept > 0 && spent.saturating_add(cost) > max_chars {
|
||||
break;
|
||||
}
|
||||
spent = spent.saturating_add(cost);
|
||||
kept += 1;
|
||||
}
|
||||
kept
|
||||
}
|
||||
|
||||
/// `kept`, then the note that `remaining` more of `total` were cut and how to see them, clamped to
|
||||
/// the byte backstop.
|
||||
pub(crate) fn with_note(
|
||||
kept: &str,
|
||||
remaining: usize,
|
||||
total: usize,
|
||||
unit: &str,
|
||||
hint: &str,
|
||||
) -> String {
|
||||
clamp_bytes(&format!("{kept}\n\n… {remaining} more {unit} not shown ({total} total). {hint}"))
|
||||
}
|
||||
|
||||
/// [`clamp_bytes`] for a tool that caps its own lines first.
|
||||
pub(crate) fn clamp(text: &str) -> String {
|
||||
clamp_bytes(text)
|
||||
}
|
||||
|
||||
/// Head+tail byte clamp for the rare single-huge-line case the line cap can't shrink.
|
||||
fn clamp_bytes(text: &str) -> String {
|
||||
if text.len() <= MAX_OUTPUT_BYTES {
|
||||
|
||||
@@ -76,10 +76,22 @@ fn shell_command(command: &str, cwd: &Path) -> Command {
|
||||
.stdin(std::process::Stdio::null())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped());
|
||||
if let Some(value) = python_utf8(std::env::var_os("PYTHONUTF8").as_deref(), cfg!(windows)) {
|
||||
child.env("PYTHONUTF8", value);
|
||||
}
|
||||
luxe_core::program::detach_from_terminal(&mut child);
|
||||
child
|
||||
}
|
||||
|
||||
/// The `PYTHONUTF8` a command gets, when Luxe sets one: on Windows, unless the user chose.
|
||||
///
|
||||
/// Python there writes to a pipe in the console's code page, cp1252, so a script printing `✓`
|
||||
/// fails with `UnicodeEncodeError` under Luxe and works in the user's terminal. UTF-8 mode is what
|
||||
/// Python does everywhere else.
|
||||
fn python_utf8(inherited: Option<&std::ffi::OsStr>, windows: bool) -> Option<&'static str> {
|
||||
(windows && inherited.is_none()).then_some("1")
|
||||
}
|
||||
|
||||
/// Whether commands here run through a POSIX shell, which decides how an argument is quoted.
|
||||
pub(crate) fn posix_shell() -> bool {
|
||||
luxe_core::program::shell().1 == "-c"
|
||||
@@ -106,7 +118,7 @@ pub(crate) fn quote_argument(value: &str, posix: bool) -> String {
|
||||
/// Runs `command` in `cwd` until it exits or `timeout` passes.
|
||||
///
|
||||
/// Every ending but a clean exit takes the whole tree with it, including the ones nobody writes
|
||||
/// code for: a cancel drops this future, and [`KillTreeOnDrop`] is what answers that.
|
||||
/// code for: a cancel drops this future, and a [`luxe_core::program::TreeGuard`] answers that.
|
||||
pub(crate) async fn run_captured(
|
||||
command: &str,
|
||||
cwd: &Path,
|
||||
@@ -115,8 +127,10 @@ pub(crate) async fn run_captured(
|
||||
let mut child = shell_command(command, cwd).spawn()?;
|
||||
let pid = child.id().unwrap_or(0);
|
||||
// Armed from here to the first normal exit: every early return, and every drop this function
|
||||
// never sees, takes the whole tree with it.
|
||||
let mut reaper = KillTreeOnDrop::arm(pid);
|
||||
// never sees, takes the whole tree with it. A cancel ends the call by dropping this future, so
|
||||
// there is no code path to hang an explicit kill on: pressing Cancel on a `cargo build` left
|
||||
// the build running, holding its locks and its CPU, with nothing left watching it.
|
||||
let mut reaper = luxe_core::program::TreeGuard::arm(&child);
|
||||
let pipes = Pipes::attach(&mut child);
|
||||
let (stdout, stderr) = pipes.sinks();
|
||||
// Not `tokio::time::timeout`: that drops the wait on expiry, and with it the shell
|
||||
@@ -141,35 +155,6 @@ pub(crate) async fn run_captured(
|
||||
}
|
||||
}
|
||||
|
||||
/// Kills the command's whole process group unless the command finished on its own.
|
||||
///
|
||||
/// `kill_on_drop` reaps the direct child — the shell — and orphans everything the shell started.
|
||||
/// A **cancel** ends the call by *dropping* its future, so there is no code path to hang an
|
||||
/// explicit kill on: pressing Cancel on a `cargo build` left the build running, holding its locks
|
||||
/// and its CPU, with nothing left watching it.
|
||||
///
|
||||
/// Disarmed once the child has been reaped, because a reaped pid can be reused and killing a
|
||||
/// *group* by a recycled id is the one mistake worse than leaking one.
|
||||
struct KillTreeOnDrop(Option<u32>);
|
||||
|
||||
impl KillTreeOnDrop {
|
||||
fn arm(pid: u32) -> Self {
|
||||
Self((pid != 0).then_some(pid))
|
||||
}
|
||||
|
||||
fn disarm(&mut self) {
|
||||
self.0 = None;
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for KillTreeOnDrop {
|
||||
fn drop(&mut self) {
|
||||
if let Some(pid) = self.0 {
|
||||
luxe_core::program::kill_tree(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One stream's bounded capture: its first bytes, its last bytes, and a count of what fell between.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Capture {
|
||||
|
||||
@@ -1,5 +1,22 @@
|
||||
use super::*;
|
||||
|
||||
/// Windows Python writes cp1252 into a pipe, so a `✓` in a test's output crashed it under Luxe.
|
||||
#[test]
|
||||
fn windows_commands_get_python_utf8_mode() {
|
||||
assert_eq!(python_utf8(None, true), Some("1"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_python_utf8_the_user_set_is_kept() {
|
||||
assert_eq!(python_utf8(Some(std::ffi::OsStr::new("0")), true), None);
|
||||
}
|
||||
|
||||
/// Elsewhere Python is UTF-8 already, and the environment is left as the user has it.
|
||||
#[test]
|
||||
fn unix_commands_get_no_python_setting() {
|
||||
assert_eq!(python_utf8(None, false), None);
|
||||
}
|
||||
|
||||
/// However much a stream prints, a capture holds its head and its tail and counts the rest.
|
||||
#[test]
|
||||
fn a_capture_holds_its_bound_and_counts_what_fell_between() {
|
||||
|
||||
@@ -186,8 +186,7 @@ impl ReadTool {
|
||||
limit: Option<usize>,
|
||||
cx: &ToolContext,
|
||||
) -> ToolResultData {
|
||||
// Resolve path relative to cwd.
|
||||
let full_path = cx.cwd.join(path_str);
|
||||
let full_path = cx.cwd.join(&*luxe_core::paths::native(path_str));
|
||||
|
||||
debug!(path = %full_path.display(), offset, limit = limit.unwrap_or(usize::MAX), "reading file");
|
||||
|
||||
|
||||
@@ -161,8 +161,7 @@ impl Tool for WriteTool {
|
||||
}
|
||||
};
|
||||
|
||||
// Resolve path relative to cwd.
|
||||
let full_path = cx.cwd.join(path_str);
|
||||
let full_path = cx.cwd.join(&*luxe_core::paths::native(path_str));
|
||||
|
||||
if let Err(refusal) = self.check_blind_overwrite(cx, &full_path, path_str) {
|
||||
return refusal;
|
||||
@@ -182,7 +181,7 @@ impl Tool for WriteTool {
|
||||
Err(_) => std::borrow::Cow::Borrowed(content),
|
||||
};
|
||||
|
||||
match luxe_core::fs_atomic::replace(&full_path, content.as_bytes()) {
|
||||
match luxe_core::fs_atomic::replace_keeping_mode(&full_path, content.as_bytes()) {
|
||||
Ok(()) => {
|
||||
debug!(path = %full_path.display(), "file written successfully");
|
||||
self.after_write(cx, &full_path, &content, existed).await
|
||||
|
||||
@@ -30,6 +30,22 @@ async fn a_reformatted_write_shows_the_file_as_it_is_now() {
|
||||
assert!(text.contains("+1 fn sub(a: i32) {}") && !text.contains("a:i32"), "{text}");
|
||||
}
|
||||
|
||||
/// Overwriting a script keeps it executable, as an editor's save does.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn an_overwritten_script_stays_executable() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let script = dir.path().join("run.sh");
|
||||
std::fs::write(&script, "#!/bin/sh\n").unwrap();
|
||||
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let args = serde_json::json!({"path": "run.sh", "content": "#!/bin/sh\necho hi\n"});
|
||||
let result = WriteTool::new().run(args, &test_context(dir.path())).await;
|
||||
assert!(!result.is_error, "{:?}", result.content);
|
||||
let mode = std::fs::metadata(&script).unwrap().permissions().mode() & 0o777;
|
||||
assert_eq!(mode, 0o755, "still executable after the write");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn write_tool_basic() {
|
||||
let tmp_dir_guard = tempfile::tempdir().expect("temp dir");
|
||||
|
||||
@@ -8,7 +8,7 @@ repository.workspace = true
|
||||
homepage.workspace = true
|
||||
|
||||
[dependencies]
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.0" }
|
||||
luxe-core = { path = "../luxe-core", version = "0.1.2" }
|
||||
# `unstable-rendered-line-info` enables Paragraph::line_count, used for precise wrapping-aware
|
||||
# mouse hit-testing over word-wrapped surfaces (guide/dock/conversation clicks). It adds no crate;
|
||||
# the API is marked unstable upstream but is stable within our pinned minor (re-check on upgrade).
|
||||
@@ -54,6 +54,8 @@ clipboard-images = ["clipboard"]
|
||||
# Integration tests link the library without `cfg(test)`; normal dependents keep fixtures off.
|
||||
luxe-tui = { path = ".", features = ["fixture"] }
|
||||
tempfile = "3"
|
||||
# The README's animated wordmark (examples/wordmark_gif). Already in the tree through `image`.
|
||||
gif = "0.14"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
@@ -71,6 +73,10 @@ test = true
|
||||
name = "wordmark_svg"
|
||||
test = true
|
||||
|
||||
[[example]]
|
||||
name = "wordmark_gif"
|
||||
test = true
|
||||
|
||||
[[example]]
|
||||
name = "theme_css"
|
||||
test = true
|
||||
|
||||
197
crates/luxe-tui/examples/wordmark_gif.rs
Normal file
197
crates/luxe-tui/examples/wordmark_gif.rs
Normal file
@@ -0,0 +1,197 @@
|
||||
//! wordmark_gif — the logotype as the terminal lights it, as a looping GIF for the README.
|
||||
//!
|
||||
//! Every frame comes from [`Wordmark::lines_at`], the same rows and the same two lights the
|
||||
//! dashboard draws, so the recording cannot drift from the product. GitHub shows a README image on
|
||||
//! its own page background, light or dark, so the background is transparent: the art is hard-edged
|
||||
//! cells, which GIF's one-bit transparency draws exactly.
|
||||
//!
|
||||
//! ```sh
|
||||
//! cargo run -p luxe-tui --example wordmark_gif -- dark > web/src/assets/generated/wordmark-dark.gif
|
||||
//! cargo run -p luxe-tui --example wordmark_gif -- light > web/src/assets/generated/wordmark-light.gif
|
||||
//! ```
|
||||
|
||||
use luxe_tui::main_loop::ANIMATION_TICK;
|
||||
use luxe_tui::widgets::{
|
||||
Wordmark, WordmarkInk, DRIFT_FRAME_TICKS, DRIFT_PERIOD_TICKS, SHINE_CYCLE_TICKS,
|
||||
SHINE_SWEEP_TICKS,
|
||||
};
|
||||
use ratatui::style::Color;
|
||||
use std::borrow::Cow;
|
||||
use std::io::Write;
|
||||
|
||||
/// Pixels per cell: the 10×22 grid `wordmark_svg` draws, twice over so the mark stays sharp on a
|
||||
/// high-density screen when the README shows it at half size.
|
||||
const CELL_W: usize = 20;
|
||||
const CELL_H: usize = 44;
|
||||
|
||||
fn main() {
|
||||
let dark = match std::env::args().nth(1).as_deref() {
|
||||
Some("dark") => true,
|
||||
Some("light") => false,
|
||||
other => {
|
||||
eprintln!("usage: wordmark_gif dark|light (got {other:?})");
|
||||
std::process::exit(2);
|
||||
}
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
write_gif(&mut out, ink(dark)).expect("encoding into memory cannot fail");
|
||||
std::io::stdout().write_all(&out).expect("write the GIF to stdout");
|
||||
}
|
||||
|
||||
/// The luxe theme's accent and canvas. `dark` sets how hard the shine pushes toward white: a light
|
||||
/// page washes the letters out sooner, so it gets the gentler peak the terminal uses on light
|
||||
/// themes. The canvas stays the dark one either way, so the drift deepens the brass on both.
|
||||
fn ink(dark: bool) -> WordmarkInk {
|
||||
let theme = luxe_core::theme::ThemeRegistry::new().resolve("luxe").expect("a built-in theme");
|
||||
let rgb = |token: &str| match theme.get(token) {
|
||||
Some(luxe_core::theme::ColorValue::Rgb(red, green, blue)) => {
|
||||
Color::Rgb(*red, *green, *blue)
|
||||
}
|
||||
other => panic!("the luxe theme's {token} is a color, not {other:?}"),
|
||||
};
|
||||
WordmarkInk { accent: rgb("accent"), canvas: Some(rgb("background")), dark }
|
||||
}
|
||||
|
||||
/// The ticks a frame is drawn at across one loop: every tick while the shine crosses, every drift
|
||||
/// frame while it rests, as the terminal repaints.
|
||||
///
|
||||
/// One loop is one drift period, which has to close on itself for the GIF to loop without a seam.
|
||||
/// The shine crosses once at its start and rests for the remainder.
|
||||
fn frame_ticks() -> Vec<u64> {
|
||||
(0..DRIFT_PERIOD_TICKS)
|
||||
.filter(|&tick| tick < SHINE_SWEEP_TICKS || tick % DRIFT_FRAME_TICKS == 0)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Each frame's delay in hundredths of a second, rounded against the running clock so the loop
|
||||
/// totals the drift period rather than gaining a rounding error per frame.
|
||||
fn delays(ticks: &[u64]) -> Vec<u16> {
|
||||
let tick_ms = ANIMATION_TICK.as_millis() as f64;
|
||||
let at = |tick: u64| (tick as f64 * tick_ms / 10.0).round() as i64;
|
||||
let ends = ticks.iter().skip(1).copied().chain([DRIFT_PERIOD_TICKS]);
|
||||
ticks.iter().zip(ends).map(|(&start, end)| (at(end) - at(start)).max(1) as u16).collect()
|
||||
}
|
||||
|
||||
fn write_gif(out: &mut Vec<u8>, ink: WordmarkInk) -> Result<(), gif::EncodingError> {
|
||||
let size = Wordmark::Full;
|
||||
let (width, height) = (usize::from(size.width()) * CELL_W, usize::from(size.height()) * CELL_H);
|
||||
let mut encoder = gif::Encoder::new(out, width as u16, height as u16, &[])?;
|
||||
encoder.set_repeat(gif::Repeat::Infinite)?;
|
||||
let ticks = frame_ticks();
|
||||
for (&tick, delay) in ticks.iter().zip(delays(&ticks)) {
|
||||
// The shine's own cycle is shorter than the loop; past it the shine rests
|
||||
let shine = (tick < SHINE_CYCLE_TICKS).then_some(tick);
|
||||
let (pixels, palette) = render(size, ink, tick, shine, width);
|
||||
encoder.write_frame(&frame((width, height), pixels, palette, delay))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// One whole-canvas frame, index 0 transparent.
|
||||
fn frame(
|
||||
(width, height): (usize, usize),
|
||||
pixels: Vec<u8>,
|
||||
palette: Vec<u8>,
|
||||
delay: u16,
|
||||
) -> gif::Frame<'static> {
|
||||
gif::Frame {
|
||||
width: width as u16,
|
||||
height: height as u16,
|
||||
buffer: Cow::Owned(pixels),
|
||||
palette: Some(palette),
|
||||
transparent: Some(0),
|
||||
delay,
|
||||
// Cleared between frames, or a cell turned transparent would keep the last frame's ink
|
||||
dispose: gif::DisposalMethod::Background,
|
||||
..gif::Frame::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// One frame as palette indices and the palette, index 0 transparent.
|
||||
fn render(
|
||||
size: Wordmark,
|
||||
ink: WordmarkInk,
|
||||
drift: u64,
|
||||
shine: Option<u64>,
|
||||
width: usize,
|
||||
) -> (Vec<u8>, Vec<u8>) {
|
||||
let mut palette = vec![0u8, 0, 0];
|
||||
let mut pixels = vec![0u8; width * usize::from(size.height()) * CELL_H];
|
||||
for (row, line) in size.lines_at(ink, drift, shine).iter().enumerate() {
|
||||
let cells = line
|
||||
.spans
|
||||
.iter()
|
||||
.flat_map(|span| span.content.chars().map(move |glyph| (glyph, span.style.fg)));
|
||||
for (column, (glyph, fg)) in cells.enumerate() {
|
||||
let Some(Color::Rgb(red, green, blue)) = fg else { continue };
|
||||
let index = palette_index(&mut palette, [red, green, blue]);
|
||||
ink_cell(&mut pixels, width, (column, row), glyph, index);
|
||||
}
|
||||
}
|
||||
(pixels, palette)
|
||||
}
|
||||
|
||||
/// Paints the part of cell `(column, row)` that `glyph` inks: all of it, or the top or bottom half.
|
||||
fn ink_cell(
|
||||
pixels: &mut [u8],
|
||||
width: usize,
|
||||
(column, row): (usize, usize),
|
||||
glyph: char,
|
||||
index: u8,
|
||||
) {
|
||||
let (top, bottom) = match glyph {
|
||||
'█' => (0, CELL_H),
|
||||
'▀' => (0, CELL_H / 2),
|
||||
'▄' => (CELL_H / 2, CELL_H),
|
||||
_ => return,
|
||||
};
|
||||
for y in row * CELL_H + top..row * CELL_H + bottom {
|
||||
pixels[y * width + column * CELL_W..][..CELL_W].fill(index);
|
||||
}
|
||||
}
|
||||
|
||||
/// The palette slot for `color`, adding it the first time it is seen.
|
||||
fn palette_index(palette: &mut Vec<u8>, color: [u8; 3]) -> u8 {
|
||||
let existing = palette.chunks(3).position(|entry| entry == color);
|
||||
let index = existing.unwrap_or_else(|| {
|
||||
palette.extend_from_slice(&color);
|
||||
palette.len() / 3 - 1
|
||||
});
|
||||
u8::try_from(index).expect("a frame of one hue lit two ways stays far under 256 colors")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The loop is one drift period to the hundredth of a second, so it closes without a jump.
|
||||
#[test]
|
||||
fn the_frames_add_up_to_one_drift_period() {
|
||||
let total: u32 = delays(&frame_ticks()).iter().map(|&delay| u32::from(delay)).sum();
|
||||
let period_cs =
|
||||
(DRIFT_PERIOD_TICKS as f64 * ANIMATION_TICK.as_millis() as f64 / 10.0).round();
|
||||
assert_eq!(f64::from(total), period_cs);
|
||||
}
|
||||
|
||||
/// The shine crosses frame by frame, and the rest repaints at the drift's slower cadence.
|
||||
#[test]
|
||||
fn the_shine_gets_every_tick_and_the_rest_every_drift_frame() {
|
||||
let ticks = frame_ticks();
|
||||
assert!((0..SHINE_SWEEP_TICKS).all(|tick| ticks.contains(&tick)));
|
||||
assert!(ticks
|
||||
.iter()
|
||||
.filter(|&&tick| tick >= SHINE_SWEEP_TICKS)
|
||||
.all(|tick| tick % DRIFT_FRAME_TICKS == 0));
|
||||
}
|
||||
|
||||
/// The background is transparent and the letters are ink: a frame has both.
|
||||
#[test]
|
||||
fn a_frame_is_transparent_around_inked_letters() {
|
||||
let size = Wordmark::Full;
|
||||
let width = usize::from(size.width()) * CELL_W;
|
||||
let (pixels, palette) = render(size, ink(true), 0, None, width);
|
||||
assert!(pixels.contains(&0), "transparent background");
|
||||
assert!(pixels.iter().any(|&index| index != 0), "inked letters");
|
||||
assert!(palette.len() / 3 > 1);
|
||||
}
|
||||
}
|
||||
@@ -167,11 +167,8 @@ pub struct App {
|
||||
pub plan_written: Option<String>,
|
||||
/// The id of the question modal when it is the plan's handoff, which the TUI answers itself
|
||||
pub plan_handoff: Option<luxe_core::question::QuestionRequestId>,
|
||||
/// What to send once a fresh session has loaded: the plan, built from scratch
|
||||
pub pending_build: Option<String>,
|
||||
/// Whether the composer's draft goes out once a fresh session has loaded — a prompt typed on
|
||||
/// the start screen, waiting for the session it asked for
|
||||
pub send_draft_on_load: bool,
|
||||
/// What waits on the session this TUI last asked the engine for
|
||||
pub(crate) arrival: navigation::SessionArrival,
|
||||
pub file_provider: crate::ref_provider::FileRefProvider,
|
||||
/// The `@` picker's file list being read again, off the render thread
|
||||
pub file_listing: FileListing,
|
||||
|
||||
@@ -157,6 +157,7 @@ impl App {
|
||||
self.refresh_configuration();
|
||||
}
|
||||
WizardOutcome::Finished(outcome) => self.finish_login(*outcome),
|
||||
WizardOutcome::CopyCode(code) => self.record_copy(code),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,8 +98,7 @@ impl App {
|
||||
mention_listings: Default::default(),
|
||||
plan_written: None,
|
||||
plan_handoff: None,
|
||||
pending_build: None,
|
||||
send_draft_on_load: false,
|
||||
arrival: Default::default(),
|
||||
permission_auto_approve: false,
|
||||
file_provider,
|
||||
file_listing: FileListing::default(),
|
||||
|
||||
@@ -8,6 +8,7 @@ use super::{App, ConvoBlock};
|
||||
use luxe_core::config::NotificationEvent;
|
||||
use luxe_core::event::Event;
|
||||
|
||||
mod compaction;
|
||||
mod github;
|
||||
mod review;
|
||||
mod transcript;
|
||||
@@ -116,16 +117,11 @@ pub(super) fn reduce(app: &mut App, event: Event) {
|
||||
Event::ContextComposition(report) => app.push_convo(ConvoBlock::Context(report)),
|
||||
// Compaction rewrites what the model can see and spends a provider call doing it. Saying
|
||||
// so is the difference between "the agent forgot" and "the conversation was summarized".
|
||||
Event::CompactionStarted { .. } => compaction::started(app),
|
||||
Event::CompactionDone { tokens_before, tokens_after } => {
|
||||
use luxe_core::usage::format_tokens;
|
||||
// The summary may not have kept a server's tool names, so the next reference sends them
|
||||
app.mcp_listed.clear();
|
||||
app.push_convo(ConvoBlock::System(format!(
|
||||
"Compacted the conversation — {} → {} of context.",
|
||||
format_tokens(tokens_before),
|
||||
format_tokens(tokens_after)
|
||||
)));
|
||||
compaction::done(app, tokens_before, tokens_after);
|
||||
}
|
||||
Event::CompactionStopped => compaction::ended(app),
|
||||
Event::WorkspaceUpdated(snapshot) => workspace_updated(app, *snapshot),
|
||||
Event::ToolActivityUpdated(activity) => app.apply_tool_activity(*activity),
|
||||
Event::PermissionRequested { id, tool, summary, always_patterns, .. } => {
|
||||
|
||||
40
crates/luxe-tui/src/app/events/compaction.rs
Normal file
40
crates/luxe-tui/src/app/events/compaction.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
//! Compaction — the dock while a summary is written, and its reading once one stands.
|
||||
//!
|
||||
//! A summary is one provider call that can take a minute, on request or mid-turn. Without a state
|
||||
//! of its own the dock showed nothing for `/compact` and the turn's label for an automatic one, and
|
||||
//! the context reading kept the full figure until the next provider call reported a new one.
|
||||
|
||||
use super::super::{App, AppState, ConvoBlock};
|
||||
|
||||
/// The engine began writing a summary.
|
||||
pub(super) fn started(app: &mut App) {
|
||||
app.status.compacting_mid_turn = matches!(app.status.state, AppState::Streaming);
|
||||
app.status.state = AppState::Compacting;
|
||||
}
|
||||
|
||||
/// The summary stands: the dock reads what it left, and the transcript says what happened.
|
||||
pub(super) fn done(app: &mut App, tokens_before: u64, tokens_after: u64) {
|
||||
use luxe_core::usage::format_tokens;
|
||||
ended(app);
|
||||
if app.status.context_window > 0 {
|
||||
let used = tokens_after as f32 / app.status.context_window as f32 * 100.0;
|
||||
app.status.context_pct = used.min(100.0);
|
||||
}
|
||||
// The summary may not have kept a server's tool names, so the next reference sends them
|
||||
app.mcp_listed.clear();
|
||||
app.push_convo(ConvoBlock::System(format!(
|
||||
"Compacted the conversation — {} → {} of context.",
|
||||
format_tokens(tokens_before),
|
||||
format_tokens(tokens_after)
|
||||
)));
|
||||
}
|
||||
|
||||
/// The compaction under way is over, with or without a summary; the turn it interrupted, if any,
|
||||
/// carries on.
|
||||
pub(super) fn ended(app: &mut App) {
|
||||
if !matches!(app.status.state, AppState::Compacting) {
|
||||
return;
|
||||
}
|
||||
app.status.state =
|
||||
if app.status.compacting_mid_turn { AppState::Streaming } else { AppState::Idle };
|
||||
}
|
||||
@@ -109,6 +109,8 @@ pub(super) fn failed(app: &mut App, msg: String) {
|
||||
/// A prompt can't outlive its turn (the engine already resolved or dropped it), and a card still
|
||||
/// spinning is settled as cancelled with `reason`.
|
||||
fn end_turn(app: &mut App, reason: &str) {
|
||||
// A load that failed sends no snapshot, and one that never comes must not mute the next real one
|
||||
app.arrival.owes_snapshot = false;
|
||||
app.interrupt_sent_at = None;
|
||||
app.streaming = false;
|
||||
app.streamed_thinking = false;
|
||||
@@ -132,7 +134,12 @@ pub(super) fn tool_started(app: &mut App, name: String) {
|
||||
}
|
||||
|
||||
pub(super) fn state_changed(app: &mut App, state: EngineState) {
|
||||
app.status.state = if state.streaming { AppState::Streaming } else { AppState::Idle };
|
||||
// The load's own snapshot was taken before the turn its `SessionLoaded` sent, so its idle is
|
||||
// about a moment that has passed; believed, it hides the running turn until the first token
|
||||
let stale = std::mem::take(&mut app.arrival.owes_snapshot) && app.turn_in_flight();
|
||||
if !stale {
|
||||
follow_reported_activity(app, state.streaming);
|
||||
}
|
||||
if !state.model.model.is_empty() {
|
||||
app.status.model = state.model.display().to_string();
|
||||
app.status.provider = state.model.provider.clone();
|
||||
@@ -142,7 +149,16 @@ pub(super) fn state_changed(app: &mut App, state: EngineState) {
|
||||
// Not marked in the transcript: the composer's title, the bar and the dock say which
|
||||
// mode is on, and a line per switch would fill the scrollback with every toggle
|
||||
app.status.mode = state.mode.clone();
|
||||
if !state.streaming {
|
||||
}
|
||||
|
||||
/// Takes the engine's word on whether a turn is running. A turn that is still running leaves a
|
||||
/// compaction inside it on show: the summary is the turn's work until its own end event lands.
|
||||
fn follow_reported_activity(app: &mut App, streaming: bool) {
|
||||
if streaming && matches!(app.status.state, AppState::Compacting) {
|
||||
return;
|
||||
}
|
||||
app.status.state = if streaming { AppState::Streaming } else { AppState::Idle };
|
||||
if !streaming {
|
||||
app.status.active_tool = None;
|
||||
app.status.tool_started = None;
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ fn ask_in_a_new_session(app: &mut App) {
|
||||
return;
|
||||
}
|
||||
start_new_session(app);
|
||||
app.send_draft_on_load = true;
|
||||
app.arrival.send_draft = true;
|
||||
}
|
||||
|
||||
/// Runs a command typed or found on the start screen.
|
||||
@@ -167,10 +167,24 @@ impl App {
|
||||
NextSession::Load(session_id) => (Action::LoadSession(session_id), "load a session"),
|
||||
NextSession::ResumeLatest => (Action::ResumeLatest, "resume the latest session"),
|
||||
};
|
||||
self.send_action(action, doing)
|
||||
let sent = self.send_action(action, doing);
|
||||
self.arrival.owes_snapshot = sent;
|
||||
sent
|
||||
}
|
||||
}
|
||||
|
||||
/// What waits on a session the TUI asked the engine for, settled as it arrives.
|
||||
#[derive(Debug, Default)]
|
||||
pub(crate) struct SessionArrival {
|
||||
/// The plan's build prompt, built from scratch in the new session
|
||||
pub(crate) build: Option<String>,
|
||||
/// Whether the composer's draft goes out once it loads: a prompt typed on the start screen
|
||||
pub(crate) send_draft: bool,
|
||||
/// Whether its closing state snapshot is still to come. It trails `SessionLoaded`, which may
|
||||
/// already have sent a turn, so its "idle" is stale
|
||||
pub(crate) owes_snapshot: bool,
|
||||
}
|
||||
|
||||
/// Where `/sessions` reads the project's sessions from, and how many reads it has asked for.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct SessionCatalog {
|
||||
|
||||
@@ -64,7 +64,8 @@ pub(super) fn answered(app: &mut App, answers: Option<Vec<String>>) {
|
||||
// Sent once the new session has loaded, or it would land in the one being left; parked
|
||||
// only once there is a new session coming to send it to
|
||||
if app.send_action(Action::NewSession, "start a new session") {
|
||||
app.pending_build = Some(build);
|
||||
app.arrival.build = Some(build);
|
||||
app.arrival.owes_snapshot = true;
|
||||
}
|
||||
}
|
||||
RUN_AS_LOOP => {
|
||||
@@ -87,11 +88,11 @@ pub(super) fn answered(app: &mut App, answers: Option<Vec<String>>) {
|
||||
pub(super) fn session_loaded(app: &mut App) {
|
||||
// a plan another session wrote is not this session's to offer
|
||||
app.plan_written = None;
|
||||
if let Some(build) = app.pending_build.take() {
|
||||
if let Some(build) = app.arrival.build.take() {
|
||||
submit(app, &build);
|
||||
}
|
||||
// A prompt typed on the start screen, which asked for this session to answer it
|
||||
if std::mem::take(&mut app.send_draft_on_load) {
|
||||
if std::mem::take(&mut app.arrival.send_draft) {
|
||||
super::input::submit_prompt(app);
|
||||
}
|
||||
}
|
||||
@@ -137,7 +138,7 @@ mod tests {
|
||||
fn building_fresh_parks_nothing_when_no_session_is_coming() {
|
||||
let mut app = seeded_app("nord", FixtureScenario::ChatIdle).unwrap();
|
||||
answered(&mut app, Some(vec![BUILD_FRESH.to_string()]));
|
||||
assert_eq!(app.pending_build, None);
|
||||
assert_eq!(app.arrival.build, None);
|
||||
}
|
||||
|
||||
/// Building fresh asks for a new session and sends the build prompt once that session loads.
|
||||
@@ -147,11 +148,12 @@ mod tests {
|
||||
answered(&mut app, Some(vec![BUILD_FRESH.to_string()]));
|
||||
let asked: Vec<Action> = std::iter::from_fn(|| engine.try_recv().ok()).collect();
|
||||
assert!(asked.iter().any(|action| matches!(action, Action::NewSession)), "{asked:?}");
|
||||
assert!(app.pending_build.is_some());
|
||||
assert!(app.arrival.build.is_some());
|
||||
|
||||
let loaded = Event::SessionLoaded { session_id: "fresh".into(), messages: Vec::new() };
|
||||
app.update(crate::app::Msg::Engine(loaded));
|
||||
assert!(app.pending_build.is_none(), "sent into the session that loaded");
|
||||
assert!(app.arrival.build.is_none(), "sent into the session that loaded");
|
||||
assert!(app.arrival.owes_snapshot, "and the load's closing snapshot is known to be stale");
|
||||
let sent: Vec<Action> = std::iter::from_fn(|| engine.try_recv().ok()).collect();
|
||||
let submitted = sent.iter().any(|action| {
|
||||
matches!(action, Action::SubmitUserMessage { content, .. } if content.contains("Build the plan"))
|
||||
|
||||
@@ -23,6 +23,9 @@ pub struct StatusModel {
|
||||
pub active_tool: Option<String>,
|
||||
/// Current state (streaming, idle, compacting).
|
||||
pub state: AppState,
|
||||
/// Whether the compaction under way started inside a turn, which carries on once it ends; one
|
||||
/// asked for with `/compact` returns to idle
|
||||
pub(crate) compacting_mid_turn: bool,
|
||||
/// Animation frame counter, advanced on every tick while streaming/compacting.
|
||||
///
|
||||
/// Monotonic within a turn: every consumer takes it modulo its own frame count, and the
|
||||
@@ -101,6 +104,7 @@ impl Default for StatusModel {
|
||||
cwd: None,
|
||||
active_tool: None,
|
||||
state: AppState::Idle,
|
||||
compacting_mid_turn: false,
|
||||
spinner_frame: 0,
|
||||
animating: false,
|
||||
turn_started: None,
|
||||
|
||||
@@ -262,6 +262,42 @@ fn a_prompt_typed_on_the_start_screen_is_sent_in_the_new_session() {
|
||||
assert!(app.editor.current_input().is_empty(), "and it left the composer");
|
||||
}
|
||||
|
||||
/// The turn a start-screen prompt starts shows as running from the moment it is sent.
|
||||
///
|
||||
/// The engine finishes a session load with an idle snapshot *after* `SessionLoaded`, which is what
|
||||
/// sends the draft, so that snapshot lands on a turn it never saw. The break this catches: taking it
|
||||
/// at its word hid the activity bar and the clock until the model's first token, a second or two
|
||||
/// of a screen that looked like nothing had happened.
|
||||
#[test]
|
||||
fn the_snapshot_finishing_a_load_does_not_stop_the_turn_the_load_started() {
|
||||
let (tx, _rx) = mpsc::channel(100);
|
||||
let mut app = App::new(tx, KeyMap::defaults(), Default::default());
|
||||
app.status.model = "qwen3.8-27b".into();
|
||||
app.enter_dashboard(crate::dashboard::DashboardState::new(Vec::new()));
|
||||
app.editor.insert_text("summarize the parser");
|
||||
app.handle_dashboard_outcome(crate::dashboard::DashOutcome::Ask);
|
||||
|
||||
app.update(Msg::Engine(Event::SessionLoaded { session_id: "s1".into(), messages: vec![] }));
|
||||
app.update(Msg::Engine(idle_snapshot("new session: s1")));
|
||||
assert!(crate::statusline::activity_parts(&app).is_some(), "the activity bar stays up");
|
||||
assert!(app.status.turn_started.is_some(), "and so does the clock");
|
||||
|
||||
// Only the one snapshot is stale: a later idle report is believed
|
||||
app.update(Msg::Engine(idle_snapshot("cancelled")));
|
||||
assert!(crate::statusline::activity_parts(&app).is_none(), "a later report still counts");
|
||||
}
|
||||
|
||||
/// The engine's at-rest state report, as it closes a session load or a cancel.
|
||||
fn idle_snapshot(status: &str) -> Event {
|
||||
Event::StateChanged(luxe_core::event::EngineState {
|
||||
model: luxe_core::event::ModelRef::new("lmstudio", "qwen3.8-27b"),
|
||||
streaming: false,
|
||||
status: status.into(),
|
||||
mode: luxe_core::mode::BUILD.into(),
|
||||
thinking: luxe_core::provider::ThinkingLevel::Off,
|
||||
})
|
||||
}
|
||||
|
||||
/// With no model chosen, asking opens the picker over the start screen and keeps the draft — the
|
||||
/// same courtesy the composer extends in a session.
|
||||
#[test]
|
||||
|
||||
@@ -346,6 +346,56 @@ fn test_status_model_format_cost() {
|
||||
assert_eq!(StatusModel::format_cost(Some(0.004)), "<$0.01");
|
||||
}
|
||||
|
||||
fn idle_app() -> App {
|
||||
let (tx, _rx) = mpsc::channel(8);
|
||||
App::new(tx, luxe_core::keymap::KeyMap::defaults(), luxe_core::config::Config::default())
|
||||
}
|
||||
|
||||
fn engine_reports_streaming(app: &mut App, streaming: bool) {
|
||||
app.update(Msg::Engine(Event::StateChanged(luxe_core::event::EngineState {
|
||||
streaming,
|
||||
..Default::default()
|
||||
})));
|
||||
}
|
||||
|
||||
/// `/compact` shows as under way, and its end leaves the dock idle reading what the summary left.
|
||||
///
|
||||
/// The break this catches: nothing set the compacting state, so `/compact` showed no sign of life
|
||||
/// for the minute a summary takes, and the context figure stayed full until the next turn.
|
||||
#[tokio::test]
|
||||
async fn compact_on_request_shows_while_it_runs_and_refreshes_the_context_reading() {
|
||||
let mut app = idle_app();
|
||||
app.update(Msg::Engine(Event::ContextUsage { used_tokens: 190_000, context_window: 200_000 }));
|
||||
app.update(Msg::Engine(Event::CompactionStarted { tokens_before: 190_000 }));
|
||||
assert!(matches!(app.status.state, AppState::Compacting), "{:?}", app.status.state);
|
||||
|
||||
app.update(Msg::Engine(Event::CompactionDone { tokens_before: 190_000, tokens_after: 20_000 }));
|
||||
assert!(matches!(app.status.state, AppState::Idle), "{:?}", app.status.state);
|
||||
assert!((app.status.context_pct - 10.0).abs() < 0.01, "got {}", app.status.context_pct);
|
||||
}
|
||||
|
||||
/// A compaction inside a turn hands the dock back to the turn, whether or not it succeeded.
|
||||
#[tokio::test]
|
||||
async fn a_stopped_compaction_mid_turn_returns_to_the_turn() {
|
||||
let mut app = idle_app();
|
||||
engine_reports_streaming(&mut app, true);
|
||||
app.update(Msg::Engine(Event::CompactionStarted { tokens_before: 190_000 }));
|
||||
app.update(Msg::Engine(Event::CompactionStopped));
|
||||
assert!(matches!(app.status.state, AppState::Streaming), "{:?}", app.status.state);
|
||||
}
|
||||
|
||||
/// The engine's turn reports leave a compaction on show while the turn runs, and end it with the turn.
|
||||
#[tokio::test]
|
||||
async fn turn_reports_keep_a_compaction_on_show_until_the_turn_ends() {
|
||||
let mut app = idle_app();
|
||||
engine_reports_streaming(&mut app, true);
|
||||
app.update(Msg::Engine(Event::CompactionStarted { tokens_before: 190_000 }));
|
||||
engine_reports_streaming(&mut app, true);
|
||||
assert!(matches!(app.status.state, AppState::Compacting), "{:?}", app.status.state);
|
||||
engine_reports_streaming(&mut app, false);
|
||||
assert!(matches!(app.status.state, AppState::Idle), "{:?}", app.status.state);
|
||||
}
|
||||
|
||||
pub(super) fn type_into(app: &mut App, text: &str) {
|
||||
for character in text.chars() {
|
||||
app.update(Msg::Key(KeyEvent::new(KeyCode::Char(character), KeyModifiers::NONE)));
|
||||
|
||||
@@ -123,6 +123,8 @@ pub enum WizardOutcome {
|
||||
Close,
|
||||
/// A credential landed. Args: `(outcome)`. The app closes the wizard and picks a model.
|
||||
Finished(Box<LoginOutcome>),
|
||||
/// Put the device code on the clipboard, which the app owns. Args: `(code)`.
|
||||
CopyCode(String),
|
||||
/// Key consumed; the wizard stays as it is.
|
||||
Consumed,
|
||||
}
|
||||
@@ -224,6 +226,17 @@ impl AuthWizardState {
|
||||
self.pending_prompt().is_some()
|
||||
}
|
||||
|
||||
/// The device code the flow is waiting to be entered, while nothing else is being asked.
|
||||
pub fn device_code(&self) -> Option<&str> {
|
||||
if self.pending.is_some() {
|
||||
return None;
|
||||
}
|
||||
self.lines().iter().rev().find_map(|line| match line {
|
||||
WizardLine::DeviceCode { code, .. } => Some(code.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
}
|
||||
|
||||
/// Transcript lines of a running (or finished) flow.
|
||||
pub fn lines(&self) -> &[WizardLine] {
|
||||
match &self.stage {
|
||||
@@ -305,10 +318,12 @@ impl AuthWizardState {
|
||||
}
|
||||
|
||||
fn running_key(&mut self, key: &KeyEvent) -> WizardOutcome {
|
||||
let code = self.device_code().map(str::to_string);
|
||||
let Some(current) = self.pending.as_mut() else {
|
||||
// Nothing to answer: Esc aborts the flow, everything else waits.
|
||||
return match key.code {
|
||||
KeyCode::Esc => WizardOutcome::Close,
|
||||
// Nothing to answer: Esc aborts the flow, `c` copies a code to enter, the rest waits
|
||||
return match (key.code, code) {
|
||||
(KeyCode::Esc, _) => WizardOutcome::Close,
|
||||
(KeyCode::Char('c'), Some(code)) => WizardOutcome::CopyCode(code),
|
||||
_ => WizardOutcome::Consumed,
|
||||
};
|
||||
};
|
||||
@@ -455,6 +470,8 @@ mod tests_support {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod click_tests;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
|
||||
114
crates/luxe-tui/src/auth_wizard/click_tests.rs
Normal file
114
crates/luxe-tui/src/auth_wizard/click_tests.rs
Normal file
@@ -0,0 +1,114 @@
|
||||
#![allow(clippy::unwrap_used)]
|
||||
|
||||
use super::tests_support::*;
|
||||
use super::*;
|
||||
use crate::mouse::{HitRegions, MouseTarget};
|
||||
use ratatui::{backend::TestBackend, buffer::Buffer, Terminal};
|
||||
use std::cell::RefCell;
|
||||
|
||||
fn theme() -> crate::theme::styles::ThemeStyles {
|
||||
let resolved = luxe_core::theme::ThemeRegistry::new().resolve("dark").expect("dark resolves");
|
||||
crate::theme::styles::ThemeStyles::from_resolved(&resolved, "dark")
|
||||
}
|
||||
|
||||
/// The wizard drawn as `/login` draws it, with what each cell would be hit as.
|
||||
fn draw(wizard: &AuthWizardState) -> (Buffer, RefCell<HitRegions>) {
|
||||
let hit = RefCell::new(HitRegions::default());
|
||||
let mut terminal = Terminal::new(TestBackend::new(90, 32)).unwrap();
|
||||
terminal.draw(|frame| wizard.view(frame, frame.area(), &theme(), &hit)).unwrap();
|
||||
(terminal.backend().buffer().clone(), hit)
|
||||
}
|
||||
|
||||
/// Where `needle` is drawn, as `(column, row)`.
|
||||
fn cell_of(buffer: &Buffer, needle: &str) -> (u16, u16) {
|
||||
for y in 0..buffer.area.height {
|
||||
let row: String = (0..buffer.area.width).map(|x| buffer[(x, y)].symbol()).collect();
|
||||
if let Some(byte) = row.find(needle) {
|
||||
return (row[..byte].chars().count() as u16, y);
|
||||
}
|
||||
}
|
||||
panic!("{needle} is not drawn");
|
||||
}
|
||||
|
||||
/// A running sign-in asking which GitHub, as Copilot's does.
|
||||
fn asking_which_github(
|
||||
) -> (AuthWizardState, tokio::sync::oneshot::Receiver<Result<SecretString, AuthError>>) {
|
||||
let mut wizard = AuthWizardState::open(providers(), LoginScope::Any);
|
||||
wizard.on_key(&key(KeyCode::Enter));
|
||||
let (reply, answer) = tokio::sync::oneshot::channel();
|
||||
let choice = |id: &str, label: &str, detail: &str| AuthChoice {
|
||||
id: id.into(),
|
||||
label: label.into(),
|
||||
detail: Some(detail.into()),
|
||||
};
|
||||
let choices = vec![
|
||||
choice("github", "GitHub", "github.com"),
|
||||
choice("enterprise", "GitHub Enterprise", "your company's server"),
|
||||
];
|
||||
wizard.apply(AuthUiMsg::Prompt { prompt: AuthPrompt::select("Which GitHub?", choices), reply });
|
||||
(wizard, answer)
|
||||
}
|
||||
|
||||
/// Each choice of a question the sign-in asks is clickable where it is drawn, its detail line
|
||||
/// included, as the provider and method lists are. It used to draw without registering anywhere,
|
||||
/// so a click on "GitHub" did nothing.
|
||||
#[test]
|
||||
fn a_question_s_choices_are_clickable_where_they_are_drawn() {
|
||||
let (wizard, _answer) = asking_which_github();
|
||||
let (buffer, hit) = draw(&wizard);
|
||||
let (x, y) = cell_of(&buffer, "GitHub Enterprise");
|
||||
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthMethodRow(1)));
|
||||
let (x, y) = cell_of(&buffer, "your company's server");
|
||||
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthMethodRow(1)), "the detail line too");
|
||||
let (x, y) = cell_of(&buffer, "github.com");
|
||||
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthMethodRow(0)));
|
||||
}
|
||||
|
||||
/// A click lands as the keyboard would: the row it hit becomes the answer.
|
||||
#[tokio::test]
|
||||
async fn a_clicked_choice_is_the_answer() {
|
||||
let (mut wizard, answer) = asking_which_github();
|
||||
wizard.select_method(1);
|
||||
wizard.on_key(&key(KeyCode::Enter));
|
||||
let chosen = answer.await.unwrap().unwrap();
|
||||
assert_eq!(secrecy::ExposeSecret::expose_secret(&chosen), "enterprise");
|
||||
}
|
||||
|
||||
/// A sign-in waiting on a device code offers it to copy, by click or by `c`, and says so.
|
||||
#[test]
|
||||
fn a_waiting_device_code_can_be_copied() {
|
||||
let mut wizard = AuthWizardState::open(providers(), LoginScope::Any);
|
||||
wizard.on_key(&key(KeyCode::Enter));
|
||||
wizard.apply(AuthUiMsg::Event(AuthEvent::DeviceCode {
|
||||
user_code: "WXYZ-9876".into(),
|
||||
verification_uri: "https://github.com/login/device".into(),
|
||||
expires_in_secs: 900,
|
||||
interval_secs: 5,
|
||||
}));
|
||||
let (buffer, hit) = draw(&wizard);
|
||||
let (x, y) = cell_of(&buffer, "Copy code WXYZ-9876");
|
||||
assert_eq!(hit.borrow().at(x, y), Some(&MouseTarget::AuthCopyCode));
|
||||
cell_of(&buffer, "c copy code");
|
||||
match wizard.on_key(&key(KeyCode::Char('c'))) {
|
||||
WizardOutcome::CopyCode(code) => assert_eq!(code, "WXYZ-9876"),
|
||||
other => panic!("expected the code to copy, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
/// While the flow is asking something, `c` is a letter of the answer, not a copy.
|
||||
#[test]
|
||||
fn c_types_into_a_question_rather_than_copying() {
|
||||
let mut wizard = AuthWizardState::open(providers(), LoginScope::Any);
|
||||
wizard.on_key(&key(KeyCode::Enter));
|
||||
wizard.apply(AuthUiMsg::Event(AuthEvent::DeviceCode {
|
||||
user_code: "WXYZ-9876".into(),
|
||||
verification_uri: "https://github.com/login/device".into(),
|
||||
expires_in_secs: 900,
|
||||
interval_secs: 5,
|
||||
}));
|
||||
let (reply, _answer) = tokio::sync::oneshot::channel();
|
||||
wizard.apply(AuthUiMsg::Prompt { prompt: AuthPrompt::text("Domain:", None), reply });
|
||||
assert!(matches!(wizard.on_key(&key(KeyCode::Char('c'))), WizardOutcome::Consumed));
|
||||
assert_eq!(wizard.pending_prompt().map(|(_, typed)| typed), Some("c"));
|
||||
assert_eq!(wizard.device_code(), None, "no button while a question is up");
|
||||
}
|
||||
@@ -79,7 +79,7 @@ impl AuthWizardState {
|
||||
Stage::Provider { query, .. } => self.render_providers(frame, inner, query, theme, hit),
|
||||
Stage::Method { .. } => self.render_methods(frame, inner, theme, hit),
|
||||
Stage::Running { provider, lines } => {
|
||||
self.render_running(frame, inner, provider, lines, theme)
|
||||
self.render_running(frame, inner, (provider, lines), theme, hit)
|
||||
}
|
||||
Stage::Failed(message) => self.render_failed(frame, inner, message, theme),
|
||||
}
|
||||
@@ -93,6 +93,7 @@ impl AuthWizardState {
|
||||
Stage::Running { .. } => match self.pending_prompt().map(|(prompt, _)| &prompt.kind) {
|
||||
Some(AuthPromptKind::Select(_)) => "enter confirm · ↑/↓ choose · esc cancel",
|
||||
Some(_) => "enter confirm · esc cancel",
|
||||
None if self.device_code().is_some() => "c copy code · esc cancel",
|
||||
None => "Working… · esc cancel",
|
||||
},
|
||||
Stage::Failed(_) => "r try again · esc close",
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
use super::*;
|
||||
|
||||
impl AuthWizardState {
|
||||
/// Rows the pending question needs, or zero when nothing is being asked.
|
||||
/// Rows the pending question needs, the copy-code button's while a code waits to be entered,
|
||||
/// or zero.
|
||||
pub(super) fn prompt_height(&self) -> u16 {
|
||||
match self.pending_prompt() {
|
||||
// A blank line, the question, and what each choice takes.
|
||||
@@ -12,6 +13,8 @@ impl AuthWizardState {
|
||||
// A blank line, the question, and the input row.
|
||||
_ => 3,
|
||||
},
|
||||
// A blank line and the button
|
||||
None if self.device_code().is_some() => 2,
|
||||
None => 0,
|
||||
}
|
||||
}
|
||||
@@ -20,9 +23,9 @@ impl AuthWizardState {
|
||||
&self,
|
||||
frame: &mut Frame,
|
||||
inner: Rect,
|
||||
provider: &str,
|
||||
lines: &[WizardLine],
|
||||
(provider, lines): (&str, &[WizardLine]),
|
||||
theme: &ThemeStyles,
|
||||
hit: &std::cell::RefCell<HitRegions>,
|
||||
) {
|
||||
let prompt = self.pending_prompt();
|
||||
let prompt_height = self.prompt_height();
|
||||
@@ -47,6 +50,9 @@ impl AuthWizardState {
|
||||
|
||||
let Some((prompt, input)) = prompt else {
|
||||
park_cursor(frame, inner);
|
||||
if let Some(code) = self.device_code() {
|
||||
render_copy_code(frame, chunks[2], code, theme, hit);
|
||||
}
|
||||
return;
|
||||
};
|
||||
let mut rows: Vec<Line> = vec![
|
||||
@@ -59,6 +65,7 @@ impl AuthWizardState {
|
||||
match &prompt.kind {
|
||||
AuthPromptKind::Select(choices) => {
|
||||
rows.extend(self.prompt_choice_lines(choices, chunks[2], theme));
|
||||
publish_choice_rows(choices, chunks[2], hit);
|
||||
}
|
||||
kind => rows.push(prompt_input_line(prompt, kind, input, theme)),
|
||||
}
|
||||
@@ -124,6 +131,43 @@ impl AuthWizardState {
|
||||
}
|
||||
}
|
||||
|
||||
/// Makes each choice clickable over every row it draws, its detail line included, under the blank
|
||||
/// line and the question. A click highlights the row and then answers, as Enter would.
|
||||
fn publish_choice_rows(
|
||||
choices: &[luxe_core::auth::AuthChoice],
|
||||
area: Rect,
|
||||
hit: &std::cell::RefCell<HitRegions>,
|
||||
) {
|
||||
let mut regions = hit.borrow_mut();
|
||||
let mut y = area.y.saturating_add(2);
|
||||
for (index, choice) in choices.iter().enumerate() {
|
||||
let rows = choice_rows(choice).min(area.bottom().saturating_sub(y));
|
||||
if rows == 0 {
|
||||
break;
|
||||
}
|
||||
regions.push(Rect::new(area.x, y, area.width, rows), MouseTarget::AuthMethodRow(index));
|
||||
y = y.saturating_add(rows);
|
||||
}
|
||||
}
|
||||
|
||||
/// The button that copies a device code, under the transcript that shows it: typing the code by
|
||||
/// hand into a browser is the step most likely to go wrong.
|
||||
fn render_copy_code(
|
||||
frame: &mut Frame,
|
||||
area: Rect,
|
||||
code: &str,
|
||||
theme: &ThemeStyles,
|
||||
hit: &std::cell::RefCell<HitRegions>,
|
||||
) {
|
||||
let label = format!(" Copy code {code} ");
|
||||
let button = Span::styled(label.clone(), theme.accent.patch(theme.selected_bg));
|
||||
let line = Line::from(vec![button, Span::styled(" or press c", theme.muted)]);
|
||||
let row = Rect { y: area.y.saturating_add(1), height: 1, ..area };
|
||||
frame.render_widget(Paragraph::new(line), row);
|
||||
let width = u16::try_from(UnicodeWidthStr::width(label.as_str())).unwrap_or(row.width);
|
||||
hit.borrow_mut().push(Rect { width: width.min(row.width), ..row }, MouseTarget::AuthCopyCode);
|
||||
}
|
||||
|
||||
/// The sign-in's transcript so far, its last [`MAX_LINES`] lines.
|
||||
fn transcript_lines(lines: &[WizardLine], theme: &ThemeStyles) -> Vec<Line<'static>> {
|
||||
let mut body: Vec<Line<'static>> = Vec::new();
|
||||
|
||||
@@ -127,7 +127,6 @@ impl SetupState {
|
||||
None => {
|
||||
format!("Follow the desktop ({})", self.desktop.clone().unwrap_or_default())
|
||||
}
|
||||
Some(name) if name == "luxe" => "luxe (Luxe's own)".to_string(),
|
||||
Some(name) => name.clone(),
|
||||
};
|
||||
let spans = vec![
|
||||
|
||||
@@ -42,6 +42,11 @@ use std::time::Duration;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::time::interval;
|
||||
|
||||
/// The animation tick, about 30 a second: what every timed effect counts in, the wordmark's lights
|
||||
/// and the spinner included. Public so a recording of one, the README's wordmark, keeps the same
|
||||
/// time.
|
||||
pub const ANIMATION_TICK: Duration = Duration::from_millis(33);
|
||||
|
||||
/// Why the TUI returned control to the CLI.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TuiExit {
|
||||
@@ -181,7 +186,7 @@ pub async fn run(
|
||||
let mut sources = Sources {
|
||||
event_rx,
|
||||
terminal_events: Some(EventStream::new()),
|
||||
tick: interval(Duration::from_millis(33)), // ~30fps
|
||||
tick: interval(ANIMATION_TICK),
|
||||
// One listener for the run, rather than a fresh registration on every pass of a loop that
|
||||
// turns over as fast as the terminal can deliver keys.
|
||||
interrupted: Box::pin(tokio::signal::ctrl_c()),
|
||||
|
||||
@@ -68,15 +68,13 @@ pub(super) fn clicked_target(app: &mut App, target: MouseTarget) -> RouteAction
|
||||
synthetic_enter()
|
||||
}
|
||||
MouseTarget::ThemeRow(index) => theme_row(app, index),
|
||||
MouseTarget::AuthCopyCode => copy_device_code(app),
|
||||
// Session/project pickers are route_key-owned (dashboard flow), so a click resolves
|
||||
// straight to the selection RouteAction rather than a synthetic key.
|
||||
MouseTarget::SessionRow(index)
|
||||
if app.current_view == crate::app::ViewMode::SessionPicker =>
|
||||
{
|
||||
match app.overlays.session_picker.as_ref().and_then(|p| p.session_id_at(index)) {
|
||||
Some(id) => RouteAction::SessionSelected(id),
|
||||
None => RouteAction::None,
|
||||
}
|
||||
session_row(app, index)
|
||||
}
|
||||
MouseTarget::ReviewDiffRow(row)
|
||||
if app.current_view == crate::app::ViewMode::GuidedReview =>
|
||||
@@ -216,6 +214,23 @@ fn configuration_page(app: &mut App, index: usize) -> RouteAction {
|
||||
}
|
||||
|
||||
/// Previews the clicked theme, then accepts it the way Enter would.
|
||||
/// Copies the sign-in wizard's device code, when it is showing one.
|
||||
fn copy_device_code(app: &mut App) -> RouteAction {
|
||||
let code = app.overlays.auth_wizard.as_ref().and_then(|w| w.device_code().map(String::from));
|
||||
if let Some(code) = code {
|
||||
app.record_copy(code);
|
||||
}
|
||||
RouteAction::None
|
||||
}
|
||||
|
||||
/// Opens the session on the clicked picker row.
|
||||
fn session_row(app: &App, index: usize) -> RouteAction {
|
||||
match app.overlays.session_picker.as_ref().and_then(|p| p.session_id_at(index)) {
|
||||
Some(id) => RouteAction::SessionSelected(id),
|
||||
None => RouteAction::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn theme_row(app: &mut App, index: usize) -> RouteAction {
|
||||
if let Some(name) = app.overlays.theme_picker.as_mut().and_then(|p| p.select(index)) {
|
||||
app.preview_theme(&name);
|
||||
|
||||
@@ -120,8 +120,10 @@ pub enum MouseTarget {
|
||||
ModelRow(usize),
|
||||
/// A login-wizard provider row, by its index into the filtered list.
|
||||
AuthProviderRow(usize),
|
||||
/// A login-wizard method row, by its index.
|
||||
/// A login-wizard method row, by its index; also a choice of the question a sign-in is asking.
|
||||
AuthMethodRow(usize),
|
||||
/// The login wizard's button that copies the device code a sign-in is waiting on.
|
||||
AuthCopyCode,
|
||||
/// A Configuration row — a way in, or a file to open — by its index in whichever is showing.
|
||||
ConfigRow(usize),
|
||||
/// A settings page in the list down the left, by its index.
|
||||
|
||||
@@ -19,25 +19,75 @@ use unicode_width::UnicodeWidthStr;
|
||||
/// there are others and that stepping is a thing you can do, which is what the footer's bracket
|
||||
/// keys then explain.
|
||||
pub(super) fn render_header(frame: &mut Frame, area: Rect, app: &App, view: &ChangesView) {
|
||||
let styles = &app.theme_styles;
|
||||
let entry = view.current();
|
||||
let width = usize::from(area.width);
|
||||
let line = header_line(app, view, area);
|
||||
frame.render_widget(Paragraph::new(line), Rect { height: 1, ..area });
|
||||
if area.height > 1 {
|
||||
frame.render_widget(
|
||||
Block::default().borders(Borders::TOP).border_style(app.theme_styles.border_muted),
|
||||
Rect { y: area.y + 1, height: 1, ..area },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
let position = match (view.position(), view.files.len()) {
|
||||
/// Cells kept between the file's details and the FILE / DIFF switch, so its slab never touches them.
|
||||
const SWITCH_GAP: usize = 2;
|
||||
/// The least of a path worth showing: below this the position goes before the path does.
|
||||
const PATH_FLOOR: usize = 8;
|
||||
|
||||
/// The header's one line, laid out so it fits: every fixed part is measured first and the path gets
|
||||
/// what they leave. When even a stub of path will not fit, the position goes whole, never `8 of`.
|
||||
fn header_line(app: &App, view: &ChangesView, area: Rect) -> Line<'static> {
|
||||
let width = usize::from(area.width);
|
||||
let entry = view.current();
|
||||
let lead = lead_span(app, view, entry);
|
||||
let details = detail_spans(app, view, entry);
|
||||
let tabs = match view.changed() {
|
||||
true => view_tabs(view.view, app),
|
||||
false => ViewTabs::default(),
|
||||
};
|
||||
let mut position = position_label(view);
|
||||
let fixed = |position: &str| {
|
||||
let tail = tabs.width() + position_cells(position);
|
||||
cells(std::slice::from_ref(&lead)) + cells(&details) + tail + SWITCH_GAP
|
||||
};
|
||||
if fixed(&position) + PATH_FLOOR > width {
|
||||
position.clear();
|
||||
}
|
||||
// Cut from the left: the tail of a path is the file, which is the thing being named. A file
|
||||
// outside the project arrives absolute and is said the way a person would say it
|
||||
let budget = width.saturating_sub(fixed(&position)).max(PATH_FLOOR);
|
||||
let path = truncate_path_left(&home_relative(&view.path), budget);
|
||||
let mut spans =
|
||||
vec![lead, Span::styled(path, app.theme_styles.text.add_modifier(Modifier::BOLD))];
|
||||
spans.extend(details);
|
||||
push_tail(&mut spans, (app, area), tabs, position);
|
||||
Line::from(spans)
|
||||
}
|
||||
|
||||
/// `8 of 14`, or nothing for a single file.
|
||||
fn position_label(view: &ChangesView) -> String {
|
||||
match (view.position(), view.files.len()) {
|
||||
(Some(index), total) if total > 1 => format!("{} of {total}", index + 1),
|
||||
_ => String::new(),
|
||||
};
|
||||
let counts = entry.and_then(count_label).unwrap_or_default();
|
||||
// Give the path whatever the fixed parts leave, and cut it from the left: the tail of a path
|
||||
// is the file, which is the thing being named. A file outside the project arrives absolute and
|
||||
// is said the way a person would say it, which is also what tells the reader at a glance that
|
||||
// they are not looking at project code.
|
||||
let reserved = position.chars().count() + counts.chars().count() + 6;
|
||||
let titled = home_relative(&view.path);
|
||||
let path = truncate_path_left(&titled, width.saturating_sub(reserved).max(8));
|
||||
}
|
||||
}
|
||||
|
||||
// A changed file leads with its git letter; any other file with the mark of its language
|
||||
let lead = match entry {
|
||||
/// The position's cells with the two that part it from the switch, or none when it is absent.
|
||||
fn position_cells(position: &str) -> usize {
|
||||
match position.is_empty() {
|
||||
true => 0,
|
||||
false => position.width() + 2,
|
||||
}
|
||||
}
|
||||
|
||||
fn cells(spans: &[Span<'_>]) -> usize {
|
||||
spans.iter().map(|span| UnicodeWidthStr::width(span.content.as_ref())).sum()
|
||||
}
|
||||
|
||||
/// A changed file leads with its git letter; any other file with the mark of its language.
|
||||
fn lead_span(app: &App, view: &ChangesView, entry: Option<&ChangedFile>) -> Span<'static> {
|
||||
let styles = &app.theme_styles;
|
||||
match entry {
|
||||
Some(file) => {
|
||||
Span::styled(format!("{} ", marker_of(file.status)), status_style(file.status, app))
|
||||
}
|
||||
@@ -49,10 +99,13 @@ pub(super) fn render_header(frame: &mut Frame, area: Rect, app: &App, view: &Cha
|
||||
crate::widgets::language_hue(icon, styles),
|
||||
)
|
||||
}
|
||||
};
|
||||
let mut spans = vec![lead];
|
||||
spans.push(Span::styled(path, styles.text.add_modifier(Modifier::BOLD)));
|
||||
// What the File view is showing: how long it is, and that it was cut short when it was
|
||||
}
|
||||
}
|
||||
|
||||
/// What follows the path: the File view's length (and that it was cut short), and `+6 −8`.
|
||||
fn detail_spans(app: &App, view: &ChangesView, entry: Option<&ChangedFile>) -> Vec<Span<'static>> {
|
||||
let styles = &app.theme_styles;
|
||||
let mut spans = Vec::new();
|
||||
if let (PeekView::File, crate::changes::TextLoad::Ready(file)) = (view.view, &view.text) {
|
||||
let lines = file.text.lines().count();
|
||||
let mut meta = format!(" {lines} line{}", if lines == 1 { "" } else { "s" });
|
||||
@@ -66,39 +119,39 @@ pub(super) fn render_header(frame: &mut Frame, area: Rect, app: &App, view: &Cha
|
||||
spans.push(Span::styled(format!(" +{additions}"), styles.success));
|
||||
spans.push(Span::styled(format!(" −{deletions}"), styles.error));
|
||||
}
|
||||
// A changed file has two faces, and the one not showing is a click away
|
||||
let tabs = match view.changed() {
|
||||
true => view_tabs(view.view, app),
|
||||
false => ViewTabs::default(),
|
||||
};
|
||||
let tabs_width: usize = tabs.spans.iter().map(|span| span.content.width()).sum();
|
||||
let used: usize = spans.iter().map(|span| UnicodeWidthStr::width(span.content.as_ref())).sum();
|
||||
let tail = tabs_width + position.chars().count() + usize::from(!position.is_empty()) * 2;
|
||||
if tail > 0 {
|
||||
let pad = width.saturating_sub(used + tail);
|
||||
spans.push(Span::raw(" ".repeat(pad)));
|
||||
let tabs_at = used + pad;
|
||||
if let Some((start, cells)) = tabs.other {
|
||||
if let (Ok(x), Ok(cells)) = (u16::try_from(tabs_at + start), u16::try_from(cells)) {
|
||||
app.hit.borrow_mut().push(
|
||||
Rect::new(area.x + x, area.y, cells, 1),
|
||||
MouseTarget::ChangesFooter(ChangesFooterAction::ToggleView),
|
||||
);
|
||||
}
|
||||
}
|
||||
spans.extend(tabs.spans);
|
||||
if !position.is_empty() {
|
||||
spans.push(Span::raw(" "));
|
||||
spans.push(Span::styled(position, styles.muted));
|
||||
}
|
||||
}
|
||||
spans
|
||||
}
|
||||
|
||||
frame.render_widget(Paragraph::new(Line::from(spans)), Rect { height: 1, ..area });
|
||||
if area.height > 1 {
|
||||
frame.render_widget(
|
||||
Block::default().borders(Borders::TOP).border_style(styles.border_muted),
|
||||
Rect { y: area.y + 1, height: 1, ..area },
|
||||
);
|
||||
/// The switch and the position, pushed to the right edge with at least [`SWITCH_GAP`] before them,
|
||||
/// and the hidden face of the switch published as the click that flips the view.
|
||||
fn push_tail(
|
||||
spans: &mut Vec<Span<'static>>,
|
||||
(app, area): (&App, Rect),
|
||||
tabs: ViewTabs,
|
||||
position: String,
|
||||
) {
|
||||
let tail = tabs.width() + position_cells(&position);
|
||||
if tail == 0 {
|
||||
return;
|
||||
}
|
||||
let used = cells(spans);
|
||||
let pad = usize::from(area.width).saturating_sub(used + tail).max(SWITCH_GAP);
|
||||
spans.push(Span::raw(" ".repeat(pad)));
|
||||
publish_switch(app, area, used + pad, tabs.other);
|
||||
spans.extend(tabs.spans);
|
||||
if !position.is_empty() {
|
||||
spans.push(Span::raw(" "));
|
||||
spans.push(Span::styled(position, app.theme_styles.muted));
|
||||
}
|
||||
}
|
||||
|
||||
/// Publishes the switch's hidden face, `other` cells into a switch drawn at column `at`, as the
|
||||
/// click that flips the view.
|
||||
fn publish_switch(app: &App, area: Rect, at: usize, other: Option<(usize, usize)>) {
|
||||
let Some((start, width)) = other else { return };
|
||||
if let (Ok(x), Ok(width)) = (u16::try_from(at + start), u16::try_from(width)) {
|
||||
let target = MouseTarget::ChangesFooter(ChangesFooterAction::ToggleView);
|
||||
app.hit.borrow_mut().push(Rect::new(area.x + x, area.y, width, 1), target);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,6 +163,12 @@ struct ViewTabs {
|
||||
other: Option<(usize, usize)>,
|
||||
}
|
||||
|
||||
impl ViewTabs {
|
||||
fn width(&self) -> usize {
|
||||
cells(&self.spans)
|
||||
}
|
||||
}
|
||||
|
||||
/// `FILE DIFF`, both cut into badges: the showing face lit, the other on the idle surface.
|
||||
///
|
||||
/// Both in capitals and both slabs, so the pair reads as a switch with two positions rather than a
|
||||
@@ -160,12 +219,6 @@ pub(super) fn sentence(message: &str) -> String {
|
||||
})
|
||||
}
|
||||
|
||||
/// `+6 −8`, or nothing at all when git could not count (a binary blob).
|
||||
fn count_label(file: &ChangedFile) -> Option<String> {
|
||||
let (additions, deletions) = file.additions.zip(file.deletions)?;
|
||||
Some(format!(" +{additions} −{deletions}"))
|
||||
}
|
||||
|
||||
fn marker_of(status: luxe_core::FileChangeStatus) -> &'static str {
|
||||
use luxe_core::FileChangeStatus as Status;
|
||||
match status {
|
||||
|
||||
@@ -13,9 +13,9 @@ DashboardMinimal@64x20: frame=939001adda3bcbb4 hits=0f39b6e30d251353
|
||||
Configuration@140x44: frame=73fbef3e76d36c7c hits=03493dafb9356d07
|
||||
Configuration@100x30: frame=644345f8e6340004 hits=04d865ae91d0aa35
|
||||
Configuration@64x20: frame=28ea511ad251f8c2 hits=77b9c8af75245265
|
||||
Setup@140x44: frame=1ec7d489bf6353bb hits=36aaf8db347382c4
|
||||
Setup@100x30: frame=4124eb2f29256470 hits=2d10b9f0a226a5e0
|
||||
Setup@64x20: frame=1c978a2a7cabba3b hits=1b0ef6d44f0cf1bd
|
||||
Setup@140x44: frame=d61d62a149232446 hits=36aaf8db347382c4
|
||||
Setup@100x30: frame=1a7c35ff007f3aef hits=2d10b9f0a226a5e0
|
||||
Setup@64x20: frame=2df799477067503a hits=1b0ef6d44f0cf1bd
|
||||
SetupTools@140x44: frame=7f80f916b3e06e49 hits=2c443d3127ccc230
|
||||
SetupTools@100x30: frame=1985e10dacdabc20 hits=ee965f337121049c
|
||||
SetupTools@64x20: frame=8b216818fd028690 hits=a19d86f6dbe23c71
|
||||
@@ -171,13 +171,13 @@ ChangesWide@100x30: frame=7f799b893667c928 hits=5ebcfa50c83c624f
|
||||
ChangesWide@64x20: frame=3531cf4c1d3f2402 hits=a39e334a4bdaa029
|
||||
ChangesNarrow@140x44: frame=4d45021ad9fb0fa7 hits=de2158bdae6d8dfb
|
||||
ChangesNarrow@100x30: frame=2ee7cc81d7896506 hits=6dca1cfbcba4056a
|
||||
ChangesNarrow@64x20: frame=903e59bd8b8a62f8 hits=98deec46c37135b8
|
||||
ChangesNarrow@64x20: frame=b82352043f680144 hits=65afb8e6ea7f4981
|
||||
ChangesFailed@140x44: frame=b8c525886e754d21 hits=88e878b841c4ee77
|
||||
ChangesFailed@100x30: frame=aba8595c8158cca4 hits=d3e7f3e0ab644214
|
||||
ChangesFailed@64x20: frame=69cdcefbe785c866 hits=27476cdcd9937bea
|
||||
ChangesFailed@64x20: frame=8d0caaba450a4f3a hits=e16f1ab1fbfea007
|
||||
ChangesExternal@140x44: frame=fc75aaae10ddb262 hits=d5c3115642e748ad
|
||||
ChangesExternal@100x30: frame=522c8250b0f50ac4 hits=3dd17e6200cfb4b5
|
||||
ChangesExternal@64x20: frame=a6d2706231364207 hits=6f0e8da295aaa965
|
||||
ChangesExternal@64x20: frame=d257ba5dbedfc85f hits=a8e0be5b4625e2bc
|
||||
ChatTools+palette: frame=85cc5ae977de2fa6 hits=ca2a2a735278a5bd
|
||||
ChatTools+slash: frame=a3ce4c3a7d03f8d0 hits=8adf06de80ecdcb2
|
||||
ChatTools+mention: frame=33c4a73f189a3dcc hits=c1b8d78b51a767b7
|
||||
@@ -219,7 +219,7 @@ ChatWideProse searching: frame=1878156c08a54641 hits=00e988aa75182c51
|
||||
ChatWideProse selecting: frame=8c1edde42c1f3700 hits=bddfef30519a2a3a
|
||||
ChatWideProse copied: frame=7757bb42f647c93e hits=bddfef30519a2a3a
|
||||
ChangesWide@180x44: frame=2f39ea02da654a31 hits=e9b01340cccc666e
|
||||
ChangesNarrow unwrapped: frame=d83dfbbe3d21abf8 hits=29eecb5dbf51e195
|
||||
ChangesNarrow unwrapped: frame=13da9745cf50215c hits=c84985bf30e3251e
|
||||
ChangesWide file: frame=4292536a789062da hits=da530d7bf2c94fdf
|
||||
ChangesWide file cursor: frame=aacfd7dfe8f072f0 hits=6293b97445fe71a6
|
||||
ChangesWide file unwrapped: frame=cbf79ee6830376c5 hits=316acbdf05625e0b
|
||||
@@ -251,7 +251,7 @@ login-subscription@100x30: frame=d0d2a22124d02530 hits=57ef1e7d5ac5daaf
|
||||
login-local@100x30: frame=2e2797e9080feb20 hits=9d3e38903152bd40
|
||||
login-method@100x30: frame=25f0337c7e66d84b hits=d7190c558c1be1a3
|
||||
login-secret@100x30: frame=8e5caa0414d23337 hits=b05996f46f500728
|
||||
login-device@100x30: frame=f57f8bc46fc07b6e hits=b05996f46f500728
|
||||
login-device@100x30: frame=9678dcaabe255bf8 hits=4f495f0ab9ddfec4
|
||||
login-failed@100x30: frame=7a68823bb655e435 hits=b05996f46f500728
|
||||
github@100x30: frame=29b83b4de4d5e45a hits=6ce218f729777fd8
|
||||
github-detail@100x30: frame=8bff1a38ff99681f hits=6ce218f729777fd8
|
||||
@@ -273,7 +273,7 @@ login-subscription@64x20: frame=36f5e865860fbdc2 hits=2d2883380c90c475
|
||||
login-local@64x20: frame=1e111e16e697b7d2 hits=7aff247fdf8c800d
|
||||
login-method@64x20: frame=c0818a1d2ffab01b hits=d068b54bba55b395
|
||||
login-secret@64x20: frame=f7cb80562502782e hits=a16a29ea4502d9fb
|
||||
login-device@64x20: frame=5d3e94107818674f hits=a16a29ea4502d9fb
|
||||
login-device@64x20: frame=d61ec0dc1534f2a5 hits=44ef8648f0a8302d
|
||||
login-failed@64x20: frame=8580d73e67e35808 hits=a16a29ea4502d9fb
|
||||
github@64x20: frame=59c575f5a335deb0 hits=7c38e58fb5f36453
|
||||
github-detail@64x20: frame=15178bfb7c3e702e hits=7c38e58fb5f36453
|
||||
|
||||
@@ -34,7 +34,8 @@ pub use slab::{
|
||||
pub use tool_glyph::{tool_glyph, tool_icon, GENERIC_TOOL};
|
||||
pub use wordmark::{
|
||||
banner_frame_due, render_wordmark, smoothstep, BannerClock, Wordmark, WordmarkInk, BAND,
|
||||
DRIFT_DEPTH, LEAN, PEAK_DARK, PEAK_LIGHT, SHINE_CYCLE_TICKS, SHINE_SWEEP_TICKS, STILL_TICK,
|
||||
DRIFT_DEPTH, DRIFT_FRAME_TICKS, DRIFT_PERIOD_TICKS, LEAN, PEAK_DARK, PEAK_LIGHT,
|
||||
SHINE_CYCLE_TICKS, SHINE_SWEEP_TICKS, STILL_TICK,
|
||||
};
|
||||
pub use wrap::{wrap_cells, wrap_line, wrap_lines, wrapped_row_starts};
|
||||
|
||||
|
||||
@@ -23,8 +23,11 @@
|
||||
//! wordmark in the flat accent and skip both rather than approximate them.
|
||||
|
||||
mod art;
|
||||
mod light;
|
||||
|
||||
use art::{column_groups, FULL_ART, LINE_ART, SMALL_ART};
|
||||
pub use light::smoothstep;
|
||||
use light::{blend_toward, Pass};
|
||||
use ratatui::layout::{Alignment, Rect};
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::text::{Line, Span};
|
||||
@@ -194,14 +197,23 @@ impl Wordmark {
|
||||
/// A still is a frame of the same animation rather than a separate code path, so what a reader
|
||||
/// sees with `banner_animation = false` is a moment of what everyone else sees moving.
|
||||
pub fn lines(self, ink: WordmarkInk, tick: Option<u64>) -> Vec<Line<'static>> {
|
||||
let (tick, pass) = match tick {
|
||||
Some(tick) => (tick, Pass::at(self, ink, tick)),
|
||||
None => (STILL_TICK, None),
|
||||
};
|
||||
match tick {
|
||||
Some(tick) => self.lines_at(ink, tick, Some(tick)),
|
||||
None => self.lines_at(ink, STILL_TICK, None),
|
||||
}
|
||||
}
|
||||
|
||||
/// The logotype's rows with each light set on its own: the drift at `drift`, and the shine at
|
||||
/// `shine`, or none when that is `None`.
|
||||
///
|
||||
/// The terminal runs both from one clock and keeps them out of step on purpose, so its pattern
|
||||
/// takes minutes to repeat. A recording has to loop, so it sets the two to meet.
|
||||
pub fn lines_at(self, ink: WordmarkInk, drift: u64, shine: Option<u64>) -> Vec<Line<'static>> {
|
||||
let pass = shine.and_then(|tick| Pass::at(self, ink, tick));
|
||||
self.rows()
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(row, text)| self.row_line(text, row as u16, ink, tick, pass.as_ref()))
|
||||
.map(|(row, text)| self.row_line(text, row as u16, ink, drift, pass.as_ref()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -412,78 +424,13 @@ pub fn render_wordmark(
|
||||
pub const SHINE_SWEEP_TICKS: u64 = SWEEP_TICKS;
|
||||
/// One pass plus its rest, in ticks.
|
||||
pub const SHINE_CYCLE_TICKS: u64 = CYCLE_TICKS;
|
||||
/// One whole pass of the drift, in ticks.
|
||||
pub const DRIFT_PERIOD_TICKS: u64 = DRIFT_TICKS;
|
||||
/// Ticks between drift frames while no shine runs.
|
||||
pub const DRIFT_FRAME_TICKS: u64 = DRIFT_FRAME;
|
||||
|
||||
fn shine_active(tick: u64) -> bool {
|
||||
tick % CYCLE_TICKS < SWEEP_TICKS
|
||||
}
|
||||
|
||||
/// One tick of one pass: where the highlight band sits, and how hard it pushes.
|
||||
struct Pass {
|
||||
center: f32,
|
||||
half: f32,
|
||||
peak: f32,
|
||||
lean: f32,
|
||||
}
|
||||
|
||||
impl Pass {
|
||||
/// The pass in flight at `tick`, or `None` while the logotype rests.
|
||||
fn at(size: Wordmark, ink: WordmarkInk, tick: u64) -> Option<Self> {
|
||||
let phase = tick % CYCLE_TICKS;
|
||||
if phase >= SWEEP_TICKS {
|
||||
return None;
|
||||
}
|
||||
let span = size.span();
|
||||
let half = (span * BAND).max(1.0);
|
||||
let travel = span + 2.0 * half;
|
||||
let progress = phase as f32 / SWEEP_TICKS as f32;
|
||||
|
||||
Some(Self {
|
||||
// The band starts and ends clear of the letters, so a pass arrives and leaves rather
|
||||
// than appearing half-drawn across them.
|
||||
center: -half + progress * travel,
|
||||
half,
|
||||
peak: if ink.dark { PEAK_DARK } else { PEAK_LIGHT },
|
||||
lean: LEAN,
|
||||
})
|
||||
}
|
||||
|
||||
/// How far this cell is lifted toward the highlight.
|
||||
fn lift(&self, column: u16, row: u16) -> f32 {
|
||||
let axis = f32::from(column) + self.lean * f32::from(row);
|
||||
let distance = ((axis - self.center).abs() / self.half).min(1.0);
|
||||
smoothstep(1.0 - distance) * self.peak
|
||||
}
|
||||
}
|
||||
|
||||
/// Smooth 0→1 with zero slope at both ends, so a band edge has no seam.
|
||||
///
|
||||
/// The shape of the highlight, and the whole reason it reads as light rather than as a stripe: a
|
||||
/// linear falloff leaves a crease along the ridge and a hard shoulder at each edge. Public so the
|
||||
/// website samples this curve rather than approximating it.
|
||||
pub fn smoothstep(t: f32) -> f32 {
|
||||
let t = t.clamp(0.0, 1.0);
|
||||
t * t * (3.0 - 2.0 * t)
|
||||
}
|
||||
|
||||
/// Alpha-blend `base` `amount` of the way toward `target`.
|
||||
///
|
||||
/// An ANSI/indexed palette has no channels to mix, so an unblendable color comes back untouched
|
||||
/// rather than as an invented RGB approximation of the terminal's own.
|
||||
fn blend_toward(base: Color, target: Color, amount: f32) -> Color {
|
||||
let (Color::Rgb(red, green, blue), Color::Rgb(tr, tg, tb)) = (base, target) else {
|
||||
return base;
|
||||
};
|
||||
if amount <= 0.0 {
|
||||
return base;
|
||||
}
|
||||
let amount = amount.clamp(0.0, 1.0);
|
||||
let lift = |channel: u8, toward: u8| {
|
||||
(f32::from(channel) + (f32::from(toward) - f32::from(channel)) * amount)
|
||||
.round()
|
||||
.clamp(0.0, 255.0) as u8
|
||||
};
|
||||
Color::Rgb(lift(red, tr), lift(green, tg), lift(blue, tb))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
75
crates/luxe-tui/src/widgets/wordmark/light.rs
Normal file
75
crates/luxe-tui/src/widgets/wordmark/light.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
//! Light — the shine's band, the curve it falls off along, and the blend both lights paint with.
|
||||
//!
|
||||
//! The math of the two lights on their own, apart from the art they land on: the parent decides
|
||||
//! which cells are ink and asks here how far each one is lifted.
|
||||
|
||||
use super::{Wordmark, WordmarkInk, BAND, CYCLE_TICKS, LEAN, PEAK_DARK, PEAK_LIGHT, SWEEP_TICKS};
|
||||
use ratatui::style::Color;
|
||||
|
||||
/// One tick of one pass: where the highlight band sits, and how hard it pushes.
|
||||
pub(super) struct Pass {
|
||||
center: f32,
|
||||
half: f32,
|
||||
peak: f32,
|
||||
lean: f32,
|
||||
}
|
||||
|
||||
impl Pass {
|
||||
/// The pass in flight at `tick`, or `None` while the logotype rests.
|
||||
pub(super) fn at(size: Wordmark, ink: WordmarkInk, tick: u64) -> Option<Self> {
|
||||
let phase = tick % CYCLE_TICKS;
|
||||
if phase >= SWEEP_TICKS {
|
||||
return None;
|
||||
}
|
||||
let span = size.span();
|
||||
let half = (span * BAND).max(1.0);
|
||||
let travel = span + 2.0 * half;
|
||||
let progress = phase as f32 / SWEEP_TICKS as f32;
|
||||
|
||||
Some(Self {
|
||||
// The band starts and ends clear of the letters, so a pass arrives and leaves rather
|
||||
// than appearing half-drawn across them.
|
||||
center: -half + progress * travel,
|
||||
half,
|
||||
peak: if ink.dark { PEAK_DARK } else { PEAK_LIGHT },
|
||||
lean: LEAN,
|
||||
})
|
||||
}
|
||||
|
||||
/// How far this cell is lifted toward the highlight.
|
||||
pub(super) fn lift(&self, column: u16, row: u16) -> f32 {
|
||||
let axis = f32::from(column) + self.lean * f32::from(row);
|
||||
let distance = ((axis - self.center).abs() / self.half).min(1.0);
|
||||
smoothstep(1.0 - distance) * self.peak
|
||||
}
|
||||
}
|
||||
|
||||
/// Smooth 0→1 with zero slope at both ends, so a band edge has no seam.
|
||||
///
|
||||
/// The shape of the highlight, and the whole reason it reads as light rather than as a stripe: a
|
||||
/// linear falloff leaves a crease along the ridge and a hard shoulder at each edge. Public so the
|
||||
/// website samples this curve rather than approximating it.
|
||||
pub fn smoothstep(t: f32) -> f32 {
|
||||
let t = t.clamp(0.0, 1.0);
|
||||
t * t * (3.0 - 2.0 * t)
|
||||
}
|
||||
|
||||
/// Alpha-blend `base` `amount` of the way toward `target`.
|
||||
///
|
||||
/// An ANSI/indexed palette has no channels to mix, so an unblendable color comes back untouched
|
||||
/// rather than as an invented RGB approximation of the terminal's own.
|
||||
pub(super) fn blend_toward(base: Color, target: Color, amount: f32) -> Color {
|
||||
let (Color::Rgb(red, green, blue), Color::Rgb(tr, tg, tb)) = (base, target) else {
|
||||
return base;
|
||||
};
|
||||
if amount <= 0.0 {
|
||||
return base;
|
||||
}
|
||||
let amount = amount.clamp(0.0, 1.0);
|
||||
let lift = |channel: u8, toward: u8| {
|
||||
(f32::from(channel) + (f32::from(toward) - f32::from(channel)) * amount)
|
||||
.round()
|
||||
.clamp(0.0, 255.0) as u8
|
||||
};
|
||||
Color::Rgb(lift(red, tr), lift(green, tg), lift(blue, tb))
|
||||
}
|
||||
@@ -517,3 +517,43 @@ fn the_pane_wraps_long_lines_without_being_asked() {
|
||||
assert!(!clipped.contains(tail), "clipped now: {clipped}");
|
||||
assert!(clipped.contains('…'), "and marked as clipped: {clipped}");
|
||||
}
|
||||
|
||||
/// The header's position is whole or absent, never `8 of`: the path is what gives way, and a gap
|
||||
/// always parts the line counts from the FILE / DIFF switch. The path used to be measured against
|
||||
/// everything but the switch, so the line ran long and lost its last part, the position.
|
||||
#[test]
|
||||
fn the_changes_header_keeps_its_position_whole_and_its_switch_apart() {
|
||||
use luxe_tui::fixture::FixtureScenario::{ChangesNarrow, ChangesWide};
|
||||
// Below about 128 columns the dock lies over the pane, so narrower widths drop the dock
|
||||
let wide = [(ChangesWide, 172u16), (ChangesWide, 140)];
|
||||
for (scenario, width) in wide.into_iter().chain((44..=110).map(|width| (ChangesNarrow, width)))
|
||||
{
|
||||
let (buffer, row, text) = changes_header(scenario, width);
|
||||
let end = text.trim_end();
|
||||
let cut = [" 8", " 8 o", " 8 of", " 8 of 1"].iter().any(|cut| end.ends_with(cut));
|
||||
assert!(!cut, "{width}: the position was cut: {text}");
|
||||
// Plain background in the two cells after the counts, before the FILE slab begins
|
||||
let counts = column_of(&text, "−9");
|
||||
let plain = buffer[(counts - 1, row)].bg;
|
||||
for x in counts + 2..counts + 4 {
|
||||
assert_eq!(buffer[(x, row)].bg, plain, "{width}: no gap before the switch: {text}");
|
||||
}
|
||||
}
|
||||
// As wide as the guide's shot, the position shows
|
||||
let wide = changes_header(ChangesWide, 172).2;
|
||||
assert!(wide.contains("8 of 14"), "{wide}");
|
||||
}
|
||||
|
||||
/// A guide diff scene at `width` columns: the frame, its header row, and that row's text.
|
||||
fn changes_header(
|
||||
scenario: luxe_tui::fixture::FixtureScenario,
|
||||
width: u16,
|
||||
) -> (ratatui::buffer::Buffer, u16, String) {
|
||||
let backend = test_backend(width, 50);
|
||||
let mut app = luxe_tui::fixture::seeded_app("luxe", scenario).expect("the fixture");
|
||||
app.update(Msg::TerminalResized { width, height: 50 });
|
||||
let buffer = render_app(&mut app, &backend);
|
||||
let row = row_containing(&buffer, "DIFF", width, 50);
|
||||
let text = row_text(&buffer, row, width);
|
||||
(buffer, row, text)
|
||||
}
|
||||
|
||||
@@ -25,6 +25,10 @@ installers = ["shell", "powershell", "homebrew"]
|
||||
install-path = ["$LUXE_INSTALL_DIR/", "~/.local/bin"]
|
||||
# The installer's own updater, beside the binary; `luxe update` runs it for an installer copy
|
||||
install-updater = true
|
||||
# The installers' last word. A new terminal, because one already open has the old PATH, which the
|
||||
# installer's own note then explains. Pasted into a double-quoted string in both scripts, so no
|
||||
# backticks (sh would run what they hold), no `$` and no double quotes
|
||||
install-success-msg = "Luxe is installed. To start, open a new terminal in your project's folder and run: luxe"
|
||||
tap = "evanwhittaker/homebrew-tap"
|
||||
# `brew install evanwhittaker/tap/luxe`, not `luxe-cli`: the command is `luxe`
|
||||
formula = "luxe"
|
||||
|
||||
@@ -517,7 +517,7 @@
|
||||
<text x="0" y="215.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan fill="#ddc084"> </tspan><tspan fill="#e4dfd6" font-weight="700">Welcome to Luxe</tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
<text x="0" y="237.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#12100e" font-weight="700">1 Theme</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#97938c">2 Provider</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#97938c">3 Tools</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#97938c">4 Start</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
<text x="0" y="259.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
<text x="0" y="281.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#ddc084" font-weight="700">❯ </tspan><tspan fill="#e4dfd6" font-weight="700">luxe (Luxe's own)</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#e4dfd6">Rename the dock's headings to match the rail</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
<text x="0" y="281.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#ddc084" font-weight="700">❯ </tspan><tspan fill="#e4dfd6" font-weight="700">luxe</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#e4dfd6">Rename the dock's headings to match the rail</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
<text x="0" y="303.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#e4dfd6">nord</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
<text x="0" y="325.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#e4dfd6">dracula</tspan><tspan> </tspan><tspan fill="#e4dfd6">Two headings drift from the rail's names; renaming both.</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
<text x="0" y="347.0" textLength="1100" lengthAdjust="spacingAndGlyphs" xml:space="preserve"><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan><tspan fill="#e4dfd6">gruvbox</tspan><tspan> </tspan><tspan> </tspan><tspan> </tspan></text>
|
||||
|
||||
|
Before Width: | Height: | Size: 65 KiB After Width: | Height: | Size: 65 KiB |
@@ -11,14 +11,12 @@ On macOS and Linux, the install script puts `luxe` in `~/.local/bin`, or in `$LU
|
||||
when it is set, and adds that to your `PATH`:
|
||||
|
||||
```bash
|
||||
curl --proto '=https' --tlsv1.2 -LsSf https://github.com/evanwhittaker/luxe/releases/latest/download/luxe-cli-installer.sh | sh
|
||||
curl -fsSL https://luxe.dev/install.sh | sh
|
||||
```
|
||||
|
||||
On Windows, in PowerShell:
|
||||
|
||||
```powershell
|
||||
powershell -ExecutionPolicy Bypass -c "irm https://github.com/evanwhittaker/luxe/releases/latest/download/luxe-cli-installer.ps1 | iex"
|
||||
```
|
||||
That address redirects to the script on the [latest
|
||||
release](https://github.com/evanwhittaker/luxe/releases/latest), the one it built and attested. To
|
||||
read it before running it, drop the `| sh`.
|
||||
|
||||
With Homebrew, on macOS or Linux:
|
||||
|
||||
@@ -33,6 +31,16 @@ one):
|
||||
cargo install --locked luxe-cli
|
||||
```
|
||||
|
||||
On Windows, in PowerShell:
|
||||
|
||||
```powershell
|
||||
powershell -c "irm https://luxe.dev/install.ps1 | iex"
|
||||
```
|
||||
|
||||
[That script](https://luxe.dev/install.ps1) is two lines: it lets this PowerShell window run the
|
||||
release's installer, which Windows' default execution policy would stop, then runs it. Nothing
|
||||
about your policy is saved.
|
||||
|
||||
Any of them installs the `luxe` command; `luxe --version` checks it worked.
|
||||
|
||||
To update, run `luxe update`, or `/update` inside Luxe. Luxe checks once a day for a newer release
|
||||
@@ -148,6 +156,14 @@ on `PATH`, and the agent needs a POSIX shell for most commands. Without one, com
|
||||
If WSL is installed, Luxe skips the `bash.exe` in `System32`, which would run commands inside Linux
|
||||
against a different filesystem.
|
||||
|
||||
Python on Windows is `py` or `python`. `python3` is usually the Microsoft Store's placeholder, so
|
||||
Luxe tries `py -3` first. For the Python debugger, run `py -m pip install debugpy`. The agent's
|
||||
commands run Python in UTF-8 mode (`PYTHONUTF8=1`) unless you have set it yourself, so output with
|
||||
characters outside your code page doesn't crash them.
|
||||
|
||||
Windows Terminal keeps a column at the right edge for its scrollbar. To give it to Luxe, set
|
||||
**Scrollbar visibility** to **Hidden** in your profile's **Appearance** settings.
|
||||
|
||||
## Where Luxe keeps files
|
||||
|
||||
| | Linux and macOS | Windows |
|
||||
|
||||
@@ -96,7 +96,7 @@ what you do not recognize.
|
||||
| `refresh_sessions` | | Answered by `sessions_refreshed` |
|
||||
| `refresh_workspace` | | Answered by `workspace_updated` |
|
||||
| `request_context_composition` | | What fills the context window; answered by `context_composition` |
|
||||
| `compact_now` | `{"focus": "the API decisions"}`, or nothing | Summarize now; answered by `compaction_done` |
|
||||
| `compact_now` | `{"focus": "the API decisions"}`, or nothing | Summarize now; answered by `compaction_started`, then `compaction_done` or `compaction_stopped` |
|
||||
| `load_guided_review` | `{"request_id": "…", "target": {"kind": "working_tree"}}` | Capture a Guided Review; see [below](#guided-review) |
|
||||
| `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` |
|
||||
@@ -227,6 +227,7 @@ There are around sixty. These are the ones a front end is built on:
|
||||
| `review_started`, `review_finished` | A requested review began on `model`, and came back with a `verdict` (`continue`, `fix` or `unavailable`) and `findings`. On `fix` the findings also become the agent's next turn. |
|
||||
| `turn_usage`, `session_usage`, `plan_usage` | Cost per call, for the session, and what a subscription has left |
|
||||
| `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 |
|
||||
| `session_loaded`, `sessions_refreshed` | A transcript was restored; the session list changed |
|
||||
| `notice`, `error` | Something to show. `error` ends the turn. |
|
||||
|
||||
@@ -176,8 +176,9 @@ your own terminal is how you give both. So when a step needs one, the agent puts
|
||||
in a code block, you copy it into another terminal and run it, then tell the agent it is done.
|
||||
|
||||
The same goes for a command that waits on you at a terminal, such as a password, a confirmation or
|
||||
an interactive installer. Commands the agent runs have no terminal at all, so one of those fails at
|
||||
once and says what it wanted, rather than waiting where nobody can answer.
|
||||
an interactive installer. Commands the agent runs have no terminal and nothing to read from, so one
|
||||
of those fails or carries on with empty input, rather than waiting where nobody can answer. Most,
|
||||
such as `sudo`, say what they wanted; a shell's `read` just gets an empty answer.
|
||||
|
||||
To allow it anyway, decide ahead of time in your global file. It only helps where no password is
|
||||
needed, such as a passwordless `sudo` rule for one program:
|
||||
|
||||
@@ -13,7 +13,7 @@ group: "Using Luxe"
|
||||
| `write`, `edit` | Write or change a file. A file must be read before it is edited, and the card shows the diff, after [formatting](#format-on-save). |
|
||||
| `bash` | Run a shell command. Long output is trimmed, and the untouched output saved under `.luxe/artifacts/`: each stream whole up to 8 MB, or its first and last 4 MB past that. |
|
||||
| `test` | Run the project's tests and report each failure with its `file:line`, and which tests your change broke. Output is saved as `bash`'s is. |
|
||||
| `grep`, `glob` | Search file contents and names, with capped output |
|
||||
| `grep`, `glob` | Search file contents and names, with capped output. `grep` names each file once with its matching lines under it, and can say which function or type each match sits in. |
|
||||
| `ast_grep` | Search and replace by code structure. Needs [ast-grep](https://ast-grep.github.io). |
|
||||
| `git` | Status, diffs, history, commits, branches, and GitHub pull requests, issues and CI checks through `gh` |
|
||||
| `todo` | The agent's checklist, shown in the dock's **PLAN** |
|
||||
|
||||
@@ -77,4 +77,7 @@ script -f /tmp/luxe.tty -c luxe # replay with `cat` or `scriptreplay`
|
||||
asciinema rec /tmp/luxe.cast -c luxe
|
||||
```
|
||||
|
||||
Windows has neither. Take a screenshot instead, and in Windows Terminal right-click the tab and
|
||||
choose **Export Text** to save what the screen shows.
|
||||
|
||||
Attach the recording and the log from the same time.
|
||||
|
||||
54
scripts/aur-pkgbuild.sh
Executable file
54
scripts/aur-pkgbuild.sh
Executable file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env bash
|
||||
# aur-pkgbuild — writes the PKGBUILD for the `luxe-bin` AUR package from a release.
|
||||
#
|
||||
# Usage: scripts/aur-pkgbuild.sh <version> [out-dir] e.g. scripts/aur-pkgbuild.sh 0.1.0 /tmp/aur
|
||||
#
|
||||
# `luxe-bin` repackages the release's own Linux archives, checked against the release's `.sha256`
|
||||
# files, so it ships exactly what the release built and attested. It installs to /usr/bin, which
|
||||
# `luxe update` reads as a package manager's copy and leaves to pacman. The updater the install
|
||||
# script adds is left out for the same reason. Generate .SRCINFO from the result with
|
||||
# `makepkg --printsrcinfo > .SRCINFO`.
|
||||
set -euo pipefail
|
||||
|
||||
version="${1:?usage: aur-pkgbuild.sh <version> [out-dir]}"
|
||||
out="${2:-.}"
|
||||
release="https://github.com/evanwhittaker/luxe/releases/download/v${version}"
|
||||
|
||||
# The release's own checksum for one target's archive
|
||||
checksum() {
|
||||
curl -fsSL "${release}/luxe-cli-$1-unknown-linux-gnu.tar.xz.sha256" | cut -d' ' -f1
|
||||
}
|
||||
x86_64="$(checksum x86_64)"
|
||||
aarch64="$(checksum aarch64)"
|
||||
|
||||
mkdir -p "${out}"
|
||||
cat >"${out}/PKGBUILD" <<PKGBUILD
|
||||
# Maintainer: Evan Whittaker <contact@luxe.dev>
|
||||
# Generated by scripts/aur-pkgbuild.sh in https://github.com/evanwhittaker/luxe
|
||||
|
||||
pkgname=luxe-bin
|
||||
pkgver=${version}
|
||||
pkgrel=1
|
||||
pkgdesc="A terminal coding agent: streaming chat, tools, guided diff review, and LSP/DAP code intelligence"
|
||||
arch=('x86_64' 'aarch64')
|
||||
url="https://luxe.dev"
|
||||
license=('MIT' 'Apache-2.0')
|
||||
depends=('gcc-libs' 'glibc')
|
||||
provides=('luxe')
|
||||
conflicts=('luxe')
|
||||
# The release's binary as it was built and attested: not stripped again, no debug split
|
||||
options=('!strip' '!debug')
|
||||
source_x86_64=("luxe-cli-x86_64-\${pkgver}.tar.xz::https://github.com/evanwhittaker/luxe/releases/download/v\${pkgver}/luxe-cli-x86_64-unknown-linux-gnu.tar.xz")
|
||||
source_aarch64=("luxe-cli-aarch64-\${pkgver}.tar.xz::https://github.com/evanwhittaker/luxe/releases/download/v\${pkgver}/luxe-cli-aarch64-unknown-linux-gnu.tar.xz")
|
||||
sha256sums_x86_64=('${x86_64}')
|
||||
sha256sums_aarch64=('${aarch64}')
|
||||
|
||||
package() {
|
||||
cd "luxe-cli-\${CARCH}-unknown-linux-gnu"
|
||||
install -Dm755 luxe "\${pkgdir}/usr/bin/luxe"
|
||||
install -Dm644 LICENSE-MIT "\${pkgdir}/usr/share/licenses/\${pkgname}/LICENSE-MIT"
|
||||
install -Dm644 LICENSE-APACHE "\${pkgdir}/usr/share/licenses/\${pkgname}/LICENSE-APACHE"
|
||||
install -Dm644 README.md "\${pkgdir}/usr/share/doc/\${pkgname}/README.md"
|
||||
}
|
||||
PKGBUILD
|
||||
echo "wrote ${out}/PKGBUILD for luxe-bin ${version}"
|
||||
@@ -3,13 +3,13 @@
|
||||
406 crates/luxe-tui/src/commands/built_in.rs built_in # the command catalog: one `Command` literal per entry, no logic to pull out, so a new command raises this by its own entry
|
||||
369 crates/luxe-tui/src/app/messages.rs reduce_inner # the `Msg` reducer, one arm per message and nearly all of them one call; the fat arms already live in `app::messages::*`
|
||||
353 crates/luxe-tui/src/app/commands.rs dispatch_action # maps each `UiAction` to the one call that handles it; arms are a line or two and a handler that grows moves out
|
||||
257 crates/luxe-tui/src/app/events.rs reduce # the `Event` reducer, one arm per engine event; the long arms delegate to `app::events::*`
|
||||
253 crates/luxe-tui/src/app/events.rs reduce # the `Event` reducer, one arm per engine event; the long arms delegate to `app::events::*`
|
||||
223 crates/luxe-core/src/engine/run.rs run # the engine action loop, a dispatch over `Action` where each arm is one handler call; the arms that did real work are methods
|
||||
204 crates/luxe-tui/src/app/commands/slash.rs run_slash # maps each slash command to the handler that runs it; the arms that grew are in `commands::*`
|
||||
128 crates/luxe-core/src/engine/turn.rs run_turn # the turn state machine: its `break`s and early returns share the loop state, and every step that could stand alone (compact, stream, commit, tools, wrap-up) already does
|
||||
127 crates/luxe-tui/src/main_loop/click.rs clicked_target # one arm per mouse target, each a view guard and one message or a call into a named handler
|
||||
125 crates/luxe-cli/src/cli/rpc/translate.rs to_engine_action # the RPC vocabulary mapped onto `Action`, one thin arm per variant
|
||||
109 crates/luxe-tui/src/app/construct.rs with_file_provider # the `App` literal, one field per line; nothing in it is logic to extract
|
||||
125 crates/luxe-tui/src/main_loop/click.rs clicked_target # one arm per mouse target, each a view guard and one message or a call into a named handler
|
||||
108 crates/luxe-tui/src/app/construct.rs with_file_provider # the `App` literal, one field per line; nothing in it is logic to extract
|
||||
97 crates/luxe-cli/src/cli/headless.rs headless_json_event # one arm per `Event` the JSON stream reports, each building its small object
|
||||
93 crates/luxe-core/src/engine/turn/stream.rs read_stream # one match over provider events whose arms share the accumulating reply and `break` the loop; the arms that did more are helpers
|
||||
90 crates/luxe-tui/src/view/guided_review/overlays/help.rs help_groups # the review keys card as a table of rows, one per line
|
||||
|
||||
@@ -140,28 +140,9 @@ wordmark() { cargo run --quiet --release -p luxe-tui --example wordmark_svg -- "
|
||||
tokens() { cargo run --quiet --release -p luxe-tui --example theme_css; }
|
||||
catalog() { cargo run --quiet --release -p luxe-tui --example theme_css -- --json; }
|
||||
shiki() { cargo run --quiet --release -p luxe-tui --example shiki_theme -- "$@"; }
|
||||
|
||||
# The README's wordmark. GitHub shows an image on its own, where the CSS variables the site paints
|
||||
# the letters with do not exist and every letter would fall back to black, so this copy carries
|
||||
# the theme's own colors and its background: the same on GitHub's light pages as on its dark ones.
|
||||
banner() {
|
||||
python3 - "$1/wordmark.svg" "$1/tokens.css" <<'PY'
|
||||
import re, sys
|
||||
svg, css = (open(path).read() for path in sys.argv[1:])
|
||||
color = lambda name: re.search(rf"--luxe-{name}:\s*(#[0-9a-f]{{6}})", css).group(1)
|
||||
svg = re.sub(r"var\(--luxe-(wordmark-\d), currentColor\)", lambda m: color(m.group(1)), svg)
|
||||
width, height = (int(n) for n in re.search(r'viewBox="0 0 (\d+) (\d+)"', svg).groups())
|
||||
pad_x, pad_y = 120, 72
|
||||
box = (width + 2 * pad_x, height + 2 * pad_y)
|
||||
svg = re.sub(
|
||||
r'viewBox="[^"]*" width="\d+" height="\d+"',
|
||||
f'viewBox="{-pad_x} {-pad_y} {box[0]} {box[1]}" width="{box[0]}" height="{box[1]}"',
|
||||
svg,
|
||||
)
|
||||
background = f'<rect x="{-pad_x}" y="{-pad_y}" width="{box[0]}" height="{box[1]}" rx="16" fill="{color("background")}"/>'
|
||||
print(svg.replace("</title>", "</title>\n " + background, 1), end="")
|
||||
PY
|
||||
}
|
||||
# The README's wordmark, animated as the terminal lights it, transparent for GitHub's light and dark
|
||||
# pages alike. Two variants: a light page washes the shine out sooner, so it gets the gentler one
|
||||
wordmark_gif() { cargo run --quiet --release -p luxe-tui --example wordmark_gif -- "$@"; }
|
||||
|
||||
generate() {
|
||||
local target="$1" guide_target="$2"
|
||||
@@ -171,7 +152,8 @@ generate() {
|
||||
wordmark mark >"${target}/mark.svg"
|
||||
wordmark tile >"${target}/tile.svg"
|
||||
tokens >"${target}/tokens.css"
|
||||
banner "${target}" >"${target}/wordmark-banner.svg"
|
||||
wordmark_gif dark >"${target}/wordmark-dark.gif"
|
||||
wordmark_gif light >"${target}/wordmark-light.gif"
|
||||
catalog >"${target}/themes.json"
|
||||
facts "${target}" >"${target}/facts.json"
|
||||
shiki luxe >"${target}/shiki-luxe.json"
|
||||
@@ -211,7 +193,8 @@ print(','.join(['luxe'] + names))
|
||||
# One release build up front. Every generator is a separate `cargo run`, and letting each discover
|
||||
# that for itself turns a five-second job into a minute of cargo deciding it has nothing to do.
|
||||
cargo build --quiet --release -p luxe-tui --features fixture \
|
||||
--example fixture_svg --example wordmark_svg --example theme_css --example shiki_theme
|
||||
--example fixture_svg --example wordmark_svg --example wordmark_gif --example theme_css \
|
||||
--example shiki_theme
|
||||
|
||||
if [[ "${check}" == true ]]; then
|
||||
scratch="$(mktemp -d)"
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user