feat(app): pkg_list, pkg_outdated and the three pin commands
The five commands S2's screens stand on, registered and wrapped. Each stays thin, per commands.rs's own rule — nothing here makes a decision the CLI would not inherit. `pkg_list` goes through `envs::probe`, not `Engine::list_installed`. The probe carries Requires-Dist, which `list --format=json` drops, and it is the only producer of `size_bytes`; `pipdock list` already made this choice, and diverging would hand the Installed screen a different package set than the uninstall guard that protects it. `pkg_list` and `pkg_outdated` both take a `PyEnv` rather than a path. It is what `env_scan` already returned, so the source chip survives the round trip instead of being re-guessed as Manual — and, more importantly, it means `pkg_outdated` does not have to probe just to build one. That matters because it is the slow, networked half of the pair: the table renders from `pkg_list` immediately and badges itself when this resolves, so a PyPI failure costs badges rather than the table. `pkg_outdated` reads the configured engine from the store on every call, so the Settings radio takes effect on the next refresh. The store guard is scoped and dropped before the engine runs — holding it across the await compiles, passes every test, and freezes settings_get and the pin commands for a whole network round trip. `pins::add` now shape-checks a Hold version. SECURITY §2 promises versions are validated before argv and nothing enforced it: `hold_requirements` turns a Hold pin into a `PinnedSpec` rendered as `name==version` in a mutating command, and `pin_add` is the first door through which a user-supplied version reaches it. Deliberately a character-class check, not a PEP 440 parse — refusing a legitimate version would make a package unpinnable, which is the worse failure, so the test pins down epochs, local versions and `v`-prefixes as things that must keep working. **On verifying the argument names.** A `#[tauri::command]` resolves arguments by name at runtime, `env_hash` is the first multi-word argument in the codebase, and a mismatch type-checks fine and fails only in the app. I tried to cover it with `tauri::test`, which drives the real IPC path — but any test binary that links Tauri's runtime dies at load on this toolchain with STATUS_ENTRYPOINT_NOT_FOUND, before a single test runs (a trivial integration test in the same crate loads fine, so it is the runtime, not the harness). Rather than ship a test nobody can run, the convention is confirmed from tauri-macros 2.6.3 itself: `argument_case` defaults to `Camel` and the key goes through `to_lower_camel_case()`, so `env_hash` is `envHash` on the wire, which is what the wrappers send. It still gets exercised for real when S2's screens call it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bd05157a9d
commit
e0ed4918a6
4 files changed
+201
-3
No files matched your search
@@ -97,13 +97,47 @@ pub fn list(store: &Store, env_hash: &str) -> Result<Vec<Pin>> {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Reject a held version that is not shaped like one.
|
||||
///
|
||||
/// SECURITY §2 promises versions are validated before they enter argv, and a `Hold` pin is
|
||||
/// exactly that path: [`hold_requirements`] turns it into a [`PinnedSpec`] rendered as
|
||||
/// `name==version` in a mutating command. `PkgName` enforces its own grammar on construction,
|
||||
/// but `Version` is a transparent newtype over whatever the engine reported — so the one place a
|
||||
/// *user* supplies a version is the one place that claim needs enforcing.
|
||||
///
|
||||
/// Deliberately a character-class check and not a full PEP 440 parse. The job is to refuse
|
||||
/// whitespace, quotes, path separators and control characters, not to second-guess an epoch or a
|
||||
/// local-version segment that the resolver understands better than PipDock does. Rejecting a
|
||||
/// legitimate version would be the worse failure: it would make a package unpinnable.
|
||||
fn validated_hold(version: &Version) -> Result<&str> {
|
||||
let raw = version.0.as_str();
|
||||
let invalid = || {
|
||||
PdError::new(
|
||||
Code::PkgNotFound,
|
||||
format!("invalid version for a pin: {raw:?}"),
|
||||
)
|
||||
};
|
||||
let bytes = raw.as_bytes();
|
||||
if bytes.is_empty() || !bytes[0].is_ascii_alphanumeric() {
|
||||
return Err(invalid());
|
||||
}
|
||||
if !bytes
|
||||
.iter()
|
||||
.all(|&b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'-' | b'_' | b'+' | b'!'))
|
||||
{
|
||||
return Err(invalid());
|
||||
}
|
||||
Ok(raw)
|
||||
}
|
||||
|
||||
/// Add or replace a pin.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-INT-001` when the write fails.
|
||||
/// `PD-PKG-002` when a `Hold` names a version that is not well formed; `PD-INT-001` when the
|
||||
/// write fails.
|
||||
pub fn add(store: &Store, env_hash: &str, pin: &Pin) -> Result<()> {
|
||||
let version = match &pin.mode {
|
||||
PinMode::Hold { version } => Some(version.0.as_str()),
|
||||
PinMode::Hold { version } => Some(validated_hold(version)?),
|
||||
PinMode::Exclude => None,
|
||||
};
|
||||
store
|
||||
@@ -220,6 +254,48 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_held_version_is_shape_checked_before_it_can_reach_argv() {
|
||||
let store = Store::in_memory().expect("store");
|
||||
let hold = |v: &str| Pin {
|
||||
pkg: pkg("numpy"),
|
||||
mode: PinMode::Hold {
|
||||
version: Version(v.into()),
|
||||
},
|
||||
reason: None,
|
||||
};
|
||||
|
||||
// Real versions, including the awkward corners of PEP 440, must stay pinnable: an
|
||||
// over-strict check would make a package impossible to hold, which is worse than lax.
|
||||
for good in [
|
||||
"1.26.4",
|
||||
"2.0.0rc1",
|
||||
"1!2.0",
|
||||
"1.0+local.7",
|
||||
"v1.2",
|
||||
"0.10.12",
|
||||
] {
|
||||
add(&store, "envA", &hold(good)).unwrap_or_else(|e| panic!("{good:?}: {e:?}"));
|
||||
}
|
||||
|
||||
for bad in [
|
||||
"",
|
||||
" 1.0",
|
||||
"1.0 ",
|
||||
"-1.0",
|
||||
"1.0;rm",
|
||||
"1.0/../x",
|
||||
"--upgrade",
|
||||
"1.0\n2.0",
|
||||
] {
|
||||
let err = add(&store, "envA", &hold(bad)).expect_err("{bad:?} should be rejected");
|
||||
assert_eq!(err.code.as_str(), "PD-PKG-002", "for {bad:?}");
|
||||
}
|
||||
|
||||
// An Exclude pin has no version, so it is unaffected either way.
|
||||
add(&store, "envA", &exclude("requests", None)).expect("exclude still adds");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pins_round_trip_through_the_store() {
|
||||
let store = Store::in_memory().expect("store");
|
||||
|
||||
@@ -8,8 +8,10 @@
|
||||
//! `Serialize` error type, and the frontend contract in `ui/src/ipc` is a flat
|
||||
//! `{ code, message, stderrTail }`. One conversion here beats a shape the UI has to unwrap.
|
||||
|
||||
use pipdock_core::engine;
|
||||
use pipdock_core::envs::{self, ScanProgress};
|
||||
use pipdock_core::model::EnvSource;
|
||||
use pipdock_core::pins::{self, Pin};
|
||||
use pipdock_core::settings::{self, Consent, Settings};
|
||||
use pipdock_core::{PdError, PyEnv};
|
||||
use tauri::Emitter as _;
|
||||
@@ -147,6 +149,92 @@ pub async fn env_probe(interpreter: String) -> Wire<EnvRow> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Everything installed in one environment — the Installed table (UI-SPEC §4).
|
||||
///
|
||||
/// Goes through `envs::probe`, **not** `Engine::list_installed`. The probe carries
|
||||
/// `requires_dist`, which `list --format=json` zeroes out, and it is the source the
|
||||
/// reverse-dependency graph and the uninstall guard are built from; it is also the only producer
|
||||
/// of `size_bytes`. `pipdock list` makes the same choice, and if these two disagreed the Installed
|
||||
/// screen and the guard protecting it would disagree about which packages exist (DATA-FLOW §7).
|
||||
///
|
||||
/// Takes the whole `PyEnv` rather than a path so it can be handed straight back from `env_scan`,
|
||||
/// and so the `source` chip survives the round trip instead of being re-guessed as `Manual`.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-ENV-001` when the interpreter has gone, `PD-ENV-003` when the probe output is unreadable.
|
||||
#[tauri::command]
|
||||
pub async fn pkg_list(env: PyEnv) -> Wire<Vec<pipdock_core::Dist>> {
|
||||
Ok(envs::probe(&env.interpreter, env.source).await?.dists)
|
||||
}
|
||||
|
||||
/// Installed packages with a newer release available.
|
||||
///
|
||||
/// Kept separate from [`pkg_list`] per ARCHITECTURE §7, and because it is the one of the two that
|
||||
/// touches the network: the Installed table renders from `pkg_list` immediately and badges itself
|
||||
/// when this resolves. It takes a `PyEnv` for the same reason — building one here would mean a
|
||||
/// second probe on the slower of the two calls.
|
||||
///
|
||||
/// The engine is the configured one, read from the store on every call so flipping the Settings
|
||||
/// radio takes effect on the next refresh. The store guard is dropped before the engine runs;
|
||||
/// holding it across the await would compile, pass every test, and freeze `settings_get` and the
|
||||
/// pin commands for the length of a network round trip.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-ENG-001` when the configured engine cannot be spawned — uv is a standalone binary and is
|
||||
/// often not on PATH — and the `PD-NET-*` family when the index is unreachable.
|
||||
#[tauri::command]
|
||||
pub async fn pkg_outdated(
|
||||
state: tauri::State<'_, AppState>,
|
||||
env: PyEnv,
|
||||
) -> Wire<Vec<pipdock_core::OutdatedDist>> {
|
||||
let engine = {
|
||||
let store = state.store.lock().await;
|
||||
engine::for_id(settings::load(&store)?.engine)
|
||||
};
|
||||
Ok(engine.list_outdated(&env).await?)
|
||||
}
|
||||
|
||||
/// Pins for an environment, ordered by package name.
|
||||
///
|
||||
/// The 🔒 chip and *Select all*'s "N pinned excluded" note both read this. Neither is the
|
||||
/// enforcement point: DATA-FLOW §9.5 is enforced by `pins::filter_upgrades` when a plan is built.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-INT-001` when the pin table cannot be read.
|
||||
#[tauri::command]
|
||||
pub async fn pin_list(state: tauri::State<'_, AppState>, env_hash: String) -> Wire<Vec<Pin>> {
|
||||
let store = state.store.lock().await;
|
||||
Ok(pins::list(&store, &env_hash)?)
|
||||
}
|
||||
|
||||
/// Add or replace a pin.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-PKG-002` when the name or the held version is not well formed — `Pin` arrives from the
|
||||
/// frontend, and a `Hold` pin becomes a `PinnedSpec` that reaches argv (SECURITY §2).
|
||||
/// `PD-INT-001` when the write fails.
|
||||
#[tauri::command]
|
||||
pub async fn pin_add(state: tauri::State<'_, AppState>, env_hash: String, pin: Pin) -> Wire<()> {
|
||||
let store = state.store.lock().await;
|
||||
pins::add(&store, &env_hash, &pin)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove a pin, reporting whether one existed.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-PKG-002` when `pkg` is not a valid distribution name; `PD-INT-001` when the delete fails.
|
||||
#[tauri::command]
|
||||
pub async fn pin_remove(
|
||||
state: tauri::State<'_, AppState>,
|
||||
env_hash: String,
|
||||
pkg: String,
|
||||
) -> Wire<bool> {
|
||||
let name = pipdock_core::PkgName::parse(&pkg)?;
|
||||
let store = state.store.lock().await;
|
||||
Ok(pins::remove(&store, &env_hash, &name)?)
|
||||
}
|
||||
|
||||
/// Read the stored settings.
|
||||
///
|
||||
/// # Errors
|
||||
|
||||
@@ -35,6 +35,11 @@ pub fn run() {
|
||||
commands::app_info,
|
||||
commands::env_scan,
|
||||
commands::env_probe,
|
||||
commands::pkg_list,
|
||||
commands::pkg_outdated,
|
||||
commands::pin_list,
|
||||
commands::pin_add,
|
||||
commands::pin_remove,
|
||||
commands::settings_get,
|
||||
commands::settings_set,
|
||||
commands::legal_consent_get,
|
||||
|
||||
+30
-1
@@ -14,7 +14,7 @@
|
||||
import { invoke } from '@tauri-apps/api/core'
|
||||
import { listen, type UnlistenFn } from '@tauri-apps/api/event'
|
||||
|
||||
import type { EngineId, EnvSource, PyEnv } from './generated'
|
||||
import type { Dist, EngineId, EnvSource, OutdatedDist, Pin, PyEnv } from './generated'
|
||||
|
||||
export type * from './generated'
|
||||
|
||||
@@ -141,6 +141,35 @@ export const envScan = (): Promise<EnvRow[]> => invoke('env_scan')
|
||||
export const envProbe = (interpreter: string): Promise<EnvRow> =>
|
||||
invoke('env_probe', { interpreter })
|
||||
|
||||
/**
|
||||
* Everything installed in `env` — the Installed table.
|
||||
*
|
||||
* Pass back the `PyEnv` from `envScan`/`envProbe` rather than a bare path: the source chip
|
||||
* survives the round trip, and the Rust side does not have to guess it. Re-probes, so it is the
|
||||
* fresh listing rather than whatever the last scan saw.
|
||||
*/
|
||||
export const pkgList = (env: PyEnv): Promise<Dist[]> => invoke('pkg_list', { env })
|
||||
|
||||
/**
|
||||
* Installed packages with a newer release available, via the configured engine.
|
||||
*
|
||||
* Separate from `pkgList` because it is the one that touches the network. Render the installed
|
||||
* rows from `pkgList` first and badge them when this resolves — a failure here costs badges, not
|
||||
* the table.
|
||||
*/
|
||||
export const pkgOutdated = (env: PyEnv): Promise<OutdatedDist[]> => invoke('pkg_outdated', { env })
|
||||
|
||||
/** Pins for an environment, ordered by package name. `envHash` comes from `EnvRow`. */
|
||||
export const pinList = (envHash: string): Promise<Pin[]> => invoke('pin_list', { envHash })
|
||||
|
||||
/** Add or replace a pin. Rejects with `PD-PKG-002` if the name or held version is malformed. */
|
||||
export const pinAdd = (envHash: string, pin: Pin): Promise<void> =>
|
||||
invoke('pin_add', { envHash, pin })
|
||||
|
||||
/** Remove a pin, resolving to whether one existed. */
|
||||
export const pinRemove = (envHash: string, pkg: string): Promise<boolean> =>
|
||||
invoke('pin_remove', { envHash, pkg })
|
||||
|
||||
export const settingsGet = (): Promise<Settings> => invoke('settings_get')
|
||||
|
||||
export const settingsSet = (settings: Settings): Promise<Settings> =>
|
||||
|
||||
Reference in new issue
Block a user