rea-analysis-optimization
MOmorluto/rea485k近 7 天新增 7,690 个 star
Port or migrate TypeScript/JavaScript, Python, C, Go, Ruby, Java, Kotlin, PHP, C#, PowerShell and Swift to Rust with the rust-anything CLI or MCP tools, from one hot function up to a whole app rewritten as a pure Rust app - profile, plan, bridge one unit at a time, record the original's behavior, port, diff, bench, shadow and gate; batch hot loops so they run in Rust on all cores. Also writes a new CLI app in Rust spec first, checked against cases the user approved. Use when asked to rewrite, port or migrate code or an app to Rust, to plan such a migration, to make slow code faster with Rust, or to build a new command-line tool in Rust.
rust-anything gives you ground truth; you write the Rust. Port one unit at a time, even when the
goal is the whole app, and call a port done only when gate passes.
Whole app to a pure Rust app (the user wants the app rewritten in Rust, with the original kept only as reference): read whole-app.md first and follow it. It grills the user on the decisions, researches the dependencies, records the whole app, puts a Rust front door in front of the original, runs the steps below for every unit, then lifts the app to a standalone Rust binary (or, for a library, one pure Rust crate). CLIs, HTTP servers and libraries.
A new app in Rust (nothing to port: the user wants a new command-line tool): read
new-app.md and follow it. The user's approved cases stand in for the original:
rust-anything new starts the app with a spec, the user approves its cases, and spec check
decides when it is done.
The language is detected; --lang overrides it. The steps are the same for every language; the
bridge and the traps differ, and the original's callers keep their API. Before writing a port,
read the file for the unit's language in languages/: how its constructs map to
Rust, and the traps that made recorded cases differ.
typescript.md (TS and JS), python.md,
go.md, ruby.md, java.md,
kotlin.md, c.md, php.md,
csharp.md, powershell.md,
swift.md.
Tools: the rust-anything CLI, or the same steps as MCP tools (rust-anything mcp): go,
profile, analyze, bridge, refresh, record, build, diff, bench, shadow, gate,
enable, disable, report, rules, fuzz, map, log, package, selftest (checks that diff and gate refuse planted wrong ports; not part of a port), for a whole app app_record, app_replay, app_front_door, app_wire, app_lift, app_drift, and for a new app new and spec_check.
If the MCP tools are not available, use the CLI; if rust-anything is not installed, ask the user
to install it (cargo install rust-anything) and run
rust-anything setup, then restart the agent. When a step fails because a runtime, the agent
setup or the MCP server is missing or out of date, run rust-anything doctor: it checks each one
and prints the command that fixes it.
goWhen the user wants one slow command faster rather than a migration, go runs steps 0 to 5 for
the top hot function in one call:
rust-anything go -- python3 app.py # profile, bridge, record, write rust/<fn>-rs/TASK.md
# port src/port.rs as TASK.md says (build, diff, gate), then the same command again:
rust-anything go -- python3 app.py # build, diff, gate, and enable when all pass
It picks the outermost exported function of a portable unit that takes arguments, batched where
profile advises it (--function picks another), and records generated cases plus the calls the
workload makes. You are the agent: port only that function, and do not pass --agent (that hands
the port to another agent process). go switches the unit on only when every case matches, gate
passes and bench shows it is not slower as its callers call it; a slower stage names the
batch class to call from the loop or the caller to port with it. Report its stage and next step
to the user. rust-anything report <crate> then writes
report/REPORT.md and report/card.svg with measured numbers for the pull request.
When the goal is speed, measure before choosing anything:
rust-anything profile <repo> -- node bench.mjs # or: python3 bench.py, ruby bench.rb,
# java -cp out Main, go test -run X -bench . ./pkg
It runs a real workload under the runtime's own profiler and lists hot functions with their
callers and advice. Go profiles only through go test. C, Kotlin, PHP, C#, PowerShell and Swift
are not supported: profile refuses them and names the profiler to use instead (perf for C).
@njit, Cython, @jax.jit) runs as machine code: a Rust
port gains little speed. profile lists it under "Already compiled" and says so in the advice of
every hot function that calls it; tell the user before porting it for speed.rust-anything analyze <repo> -o plan.md # people
rust-anything analyze <repo> --json # you: schema rust-anything/analyze@1
start_here units (best first ports) and the order by package and wave.keep units (UI, editor code: they stay in the source language), blocked units and
their reason, and dependencies with fit none or unknown. Do not hand-roll a missing crate
silently: flag it.bridge --end-state records:
replace: the Rust replaces the original, which is deleted after cut-over.bindings: callers keep importing the unit through the bindings for good, so the binding
surface is public API.scaffold: the bindings are temporary and are deleted at cut-over.record compares what a function returns or throws; for TS/JS, Python, Ruby, Go, Java, Kotlin, PHP and C# functions it also
keeps the arguments after the call and what it printed to standard output, and diff compares
both (the port takes a changed argument as &mut and writes its output to stdout: &mut String).
In PowerShell and Swift, bridge keeps an export in the original (and says why) when it returns
nothing, prints or changes an argument in place; in every language when it reads the environment or
module state, or takes a callback or a class instance: a port that did nothing would pass every case. Tell the user which exports stayed and why; do not try
to bridge them with --force (it does not apply to them) or by editing the recording.rust-anything bridge <repo> <unit-id> --end-state <replace|bindings|scaffold> # crate in <repo>/rust/<name>; never overwrites
rust-anything record <crate> # the original's behavior on edge-case inputs -> cases.jsonl
record <crate> --from-tests -- npx jest
(TS/JS, any runner) or -- pytest (Python). Every call the suite makes to the unit's exported
functions becomes a case; the source is restored afterwards.--inputs inputs.jsonl ({"fn": name, "args": [...]} per line; a file of your own, not the recording) when
the generated ones cannot reach the interesting paths (structured arguments, domain strings).
A string argument there that holds JSON or base64 JSON gets cases with its inner values changed
(dates in other shapes, numbers, strings), and fuzz varies them further.record is the characterization suite: keep cases.jsonl.A port keeps behavior, bugs included. Port a known bug as it is, and fix it in its own change
once the port's gate passes, with each case the fix changes listed in known-diffs.jsonl and its
reason, but only after the user says yes (step 3): never to turn a red diff or gate green. A change that ports and fixes at once cannot be told apart from a regression.
Write the port in src/port.rs only; src/lib.rs and the wrappers are generated. After changing
a signature in port.rs, run rust-anything refresh <crate>.
Before writing code, run rust-anything rules <repo> and follow every rule it lists: behavior the
user already corrected in an earlier unit. Each time the user corrects how the port behaves, add
it right away with rust-anything rules <repo> --add "<the rule, in one sentence>", so the next
unit and the next agent keep it.
file:line, before writing code.Err("TypeName: message"); the bindings raise that type in the caller's
language (the language file says how names resolve and what diff compares). Inside
port.rs, a small error enum is clearer; turn it into that string only in the exported
function.indexmap where order reaches output.fancy-regex, not regex. Either way, \w, \b, \d
and case-insensitive matching treat non-ASCII text differently from the source language's
engine: passing the recorded cases is not enough. record adds regex traps (Turkish i, Kelvin
sign, joiners, non-ASCII digits and spaces) to functions in files that use regexes; ask the user
for real text the patterns see, record it with --inputs, and set unicode(false) or spell out
ASCII classes ([A-Za-z0-9_]) where the original's \w is ASCII-only (JS without u, Java).unsafe. If truly needed: minimal, isolated, with a // SAFETY: comment.port.rs for the cases you reason about. cargo test passing with no
tests proves nothing: the recorded cases are the proof.#[expect(lint, reason = "...")], not #[allow], so it is flagged once
it no longer applies.For a function called over many items (search, scoring, formatting a list):
rust-anything bridge <repo> <unit> --batch fn:param # or: rust-anything refresh <crate> --batch fn:param
This generates a class that takes the param values once and keeps them in Rust:
new FnBatch(items) (TS, Python, Java), Mod::FnBatch.new(items) (Ruby), NewFnBatch(items)
(Go). Per query, run(<other args>) returns every result as one array, and rank(<other args>, min) returns the item indexes, best first. It runs on all cores, and errors are the original's.
Close the batch when done in Go and Java (finalizers are only a fallback).
Array.from(order, fn) costs 20x more.pub struct FnIndex in
port.rs with new(items) and run(<other args>) -> Result<Vec<R>, String>. It must give the
same result as the function, item by item: test it against the function over every item.rank(<other args>, min) -> Result<Vec<u32>, String> (narrowing to the last query's matches as the user types, table lookups). It must
match ranking the function's results: score high to low, ties in item order.String, avoid intermediate copies. Keep diff passing.bridge --batch refuses them.--allocator rusty_alloc to
bridge if the target has no C compiler.rust-anything build <crate>
rust-anything diff <crate> # must be N/N
rust-anything bench <crate> # original vs Rust, bridge included, with a verdict
diff groups the mismatches into distinct differences (fix the largest group first) and names
where each first differs (at: the channel, the JSON path, the character). To hand a failing diff
to someone else, diff --redact shows only shapes and hashes, no arguments or outputs.
A mismatch is a behavior difference: fix the port, never the recording. An intended difference
(the bug fix that follows a passing gate, or a value Rust cannot hold, such as a lone surrogate)
needs the user's yes first, then one line per case in <crate>/known-diffs.jsonl:
{"fn", "args", "actual", "reason"}, with fn, args and actual copied from diff --json. That case then passes only with exactly that result,
and gate fails once it no longer differs, so remove the line then. If gate says the recording
is stale, the original changed: record again before porting further. After pulling upstream
changes, run rust-anything map (the map tool): every port marked changed needs record, then
diff, and the new mismatches are what changed upstream.
When diff says the port is stalled (its trend.stalled: three changes to port.rs in a row
gained no matching case), stop editing. Show the user the remaining mismatches and ask: they may
know the rule you are missing, or decide to keep those functions in the original for now. Each
failed gate check carries a fix: run that first.
Numeric code (physics, statistics, graphics) rarely gives bit-identical floats once ported:
operation order, fused multiply-add and the math library differ. When the user accepts that,
record with --float-tolerance 1e-9 (their number): floats then match within that share of the
larger value (absolute below 1), and diff and gate use it. Integers, strings and the same
value written differently (3 and 3.0) still must match exactly. Never widen a tolerance to make
a mismatch go away: a difference beyond it is a bug in the port.
Follow bench's verdict: ship, keep the slower functions in the source language, or port a
bigger unit (batch or include the callers). Do not ship a port that makes callers slower without
saying so.
rust-anything shadow <crate> # drop-in with the original's API
Load the shadow where the app loaded the original. RUST_ANYTHING_MODE=shadow (default) returns the
original's results and reports Rust mismatches; new switches to Rust; old turns Rust off.
The public API does not change. PHP, C#, PowerShell and Swift have no shadow module: shadow
refuses them and names what to do instead once diff passes (require or compile the generated
wrapper in place of the original; for PowerShell, enable).
For TS/JS, Python and PowerShell, rust-anything enable <crate> switches a gated unit on in place instead: one
marked block in the original files rebinds its functions to Rust for every caller.
rust-anything disable <crate> removes it byte for byte; RUST_ANYTHING_UNIT_<NAME>=original
runs the original for one run. Ask the user before enabling: it edits their source files.
rust-anything gate <crate>
fmt, clippy with warnings as errors, cargo tests, unsafe only with SAFETY comments, nothing
unimplemented!, every place the original fails on purpose (throw, raise, an error result)
reached by a recorded failing case (add one with record --inputs when gate names it), no
unwrap, expect or panic! in a function whose original returns an error (return Err; for a
state the original also treats as impossible, a // INFALLIBLE: <why> comment), the port at most
3x the original's lines plus 50 for imports and signatures (over that, use the crates analyze lists and fold repeated code), the
module builds, every export present, every recorded case matches. A Rust panic never matches a
recorded error: return Err where the original throws. Gate also fails a port that writes
recorded results into its code, has fewer tests or assertions than at its last passing gate, or
runs on an edited cases.jsonl: compute every result, and never weaken a test or the recording.
Never loosen a verdict to turn it green. These flags make a pass weaker, and only the user
decides to use them, after reading what they cover: --accept-known-diffs,
--accept-smaller-recording, --accept-dropped and record --allow-dropped, --float-tolerance
(above 1e-6 also --allow-large-tolerance), --ignore-key-order, and enable --force. Do not
add lines to known-diffs.jsonl, delete recorded-from.json or a recorded case, or lower
go --min-cases to get a pass. Each loosening is printed on the first line of the verdict; quote
it in your report. A pass whose comparison line lists a tolerance, accepted differences or
dropped inputs is a weaker pass: say so.
Then report to the user, and wait for their go-ahead before the next unit:
String refuses lone
surrogates), and what is left in the plan.MOmorluto/rea485k近 7 天新增 7,690 个 star
MAmattpocock/skills336k近 7 天新增 4,217 个 star
MAmattpocock/skills336k近 7 天新增 4,217 个 star
MAmattpocock/skills336k近 7 天新增 4,217 个 star
MAmattpocock/skills336k近 7 天新增 4,217 个 star