feat(app): the project folder, on the row that already carries per-env data

`EnvRow` gains `healthProject`, read out of the `health_projects` table P3 added and the
CLI has been writing since. P4's screen needs to know where Health last ran without asking
for a command whose whole job is to return one string, which is the trade `pipVersion`
already makes on this struct.

`env_scan` reads every folder in one pass before the probe loop and joins afterwards. Not
tidiness: `Store` is not `Sync`, so holding the guard across `envs::probe` does not compile
at the command boundary — the hazard CLAUDE.md says has bitten twice, enforced this time by
the type system rather than by remembering.

`health_run` now writes the folder, so the GUI stops being the head that reads a table only
the CLI fills. Not with `?`: the report is in hand and P5 depends on it being parked, so a
failed bookkeeping write must not discard findings that cost three subprocesses.

Two things `env_probe` was getting wrong, both of the same shape, and both because
`upgradePip` replaces the row **wholesale**:

Its own doc comment warned that a refresh path omitting `pip_version` would blank the field
it was refreshed to update. `healthProject` is the second field to need that warning applied
— without it, upgrading pip made the Health screen forget where it was pointed.

`source` was hardcoded `EnvSource::Manual` for both of the function's uses. So refreshing a
registry-discovered interpreter relabelled it *Added manually* in the chip at
`PdEnvironments.tsx:31` — and, because `PyEnv` carries the source too, handed that
relabelled environment to every subsequent `pkg_list`. It is a parameter now: `None` means
*Browse…*, which really is manual, and a refresh passes back the source the row had.

The join the whole feature rests on gets a test rather than an argument.
`a_probed_env_hashes_the_same_as_the_path_it_was_discovered_at` pins that the write key
(`PyEnv.interpreter`, round-tripped through the frontend) and the read key (the discovered
candidate path, before any probe) hash to the same string. If they ever diverge the folder
is written under one key and read under another, and the only symptom is a screen that
keeps asking where the project is.

Verified by running: `pipdock health --path <sample>` then, from an unrelated working
directory, `pipdock health` with no `--path` — same project, out of the store.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
poli0981andClaude Opus 5 committed 2026-08-13 18:49:59 +07:00
1 parent 9c9a46e6aa
commit eec926862d
4 files changed
+143 -12

No files matched your search

+29
View File
@@ -477,6 +477,35 @@ mod tests {
assert!(got.dists.iter().all(|d| d.size_bytes.is_none()));
}
/// The join P4's remembered project folder rests on.
///
/// `env_scan` keys the store lookup off the **discovered candidate path**, before any probe has
/// run; `health_run` keys the store *write* off `PyEnv.interpreter`, which comes back through
/// the frontend afterwards. If those two ever hash differently the folder is written under one
/// key and read under another, and the only symptom is a Health screen that keeps asking where
/// the project is — no error, nothing in a log.
///
/// They agree because `parse_probe` carries the path it was given through untouched, and
/// `env_hash` canonicalizes whatever it is handed. Both halves are load-bearing, so both are
/// asserted here rather than reasoned about at the call site.
#[test]
fn a_probed_env_hashes_the_same_as_the_path_it_was_discovered_at() {
let doc = r#"{"python":"3.12.4","prefix":"C:\\proj\\.venv","externally_managed":false,
"hidden_user_site":null,"dists":[]}"#;
let discovered = Path::new(r"C:\Proj\.venv\Scripts\Python.exe");
let got = parse_probe(doc, discovered, EnvSource::VenvScan).expect("parse");
assert_eq!(
got.env.interpreter, discovered,
"the path is carried, not rewritten"
);
assert_eq!(
env_hash(discovered),
env_hash(&got.env.interpreter),
"the write key and the read key must be the same string"
);
}
#[test]
fn a_hidden_user_site_marks_the_listing_partial() {
let doc = r#"{"python":"3.14.6","prefix":"C:\\Python314","externally_managed":false,
+91 -9
View File
@@ -100,12 +100,27 @@ pub struct EnvRow {
/// `hidden_user_site`). It is not "unknown".
#[serde(skip_serializing_if = "Option::is_none")]
pub pip_version: Option<String>,
/// The project folder Code Health last ran in for this environment (CODE-HEALTH-SPEC §3).
///
/// Carried on the row for `pip_version`'s reason — the alternative is a command whose entire
/// job is to return one string the caller is about to be handed anyway. `None` means Health
/// has never run here, which is what makes the first Run cost one extra click.
///
/// Read out of the store **before** the probe loop and joined afterwards: `Store` is not
/// `Sync`, so holding the guard across `envs::probe` does not even compile at the command
/// boundary — the good kind of enforcement.
#[serde(skip_serializing_if = "Option::is_none")]
pub health_project: Option<String>,
/// Present when the probe failed; the row renders as unusable with this code.
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<WireError>,
}
async fn probe_row(path: std::path::PathBuf, source: EnvSource) -> EnvRow {
async fn probe_row(
path: std::path::PathBuf,
source: EnvSource,
health_project: Option<String>,
) -> EnvRow {
let env_hash = envs::env_hash(&path);
let interpreter = path.display().to_string();
match envs::probe(&path, source).await {
@@ -115,6 +130,7 @@ async fn probe_row(path: std::path::PathBuf, source: EnvSource) -> EnvRow {
env_hash,
packages: Some(probed.dists.len()),
pip_version: pip_version_of(&probed.dists),
health_project,
env: Some(probed.env),
error: None,
},
@@ -125,6 +141,9 @@ async fn probe_row(path: std::path::PathBuf, source: EnvSource) -> EnvRow {
env: None,
packages: None,
pip_version: None,
// Kept on a failed row too. The probe failing says nothing about where Health ran,
// and dropping it would make a transiently broken interpreter forget the folder.
health_project,
error: Some(e.into()),
},
}
@@ -146,7 +165,10 @@ fn pip_version_of(dists: &[pipdock_core::Dist]) -> Option<String> {
/// # Errors
/// Never fails as a whole — a probe failure is reported on its own row.
#[tauri::command]
pub async fn env_scan(app: tauri::AppHandle) -> Wire<Vec<EnvRow>> {
pub async fn env_scan(
app: tauri::AppHandle,
state: tauri::State<'_, AppState>,
) -> Wire<Vec<EnvRow>> {
let emitter = app.clone();
let candidates = envs::scan_reporting(&move |progress: ScanProgress| {
// A UI that stopped listening is not a reason to abandon the scan.
@@ -154,9 +176,30 @@ pub async fn env_scan(app: tauri::AppHandle) -> Wire<Vec<EnvRow>> {
})
.await;
// Every remembered folder, read in one pass with the guard taken and dropped before the first
// probe. One query per row inside the loop would hold the store across N subprocess spawns.
let mut folders: std::collections::HashMap<String, String> = {
let store = state.store.lock().await;
candidates
.iter()
.filter_map(|c| {
let hash = envs::env_hash(&c.path);
// A store read failing is not a reason to fail discovery: the cost is a Health
// screen that asks for the folder again, and the alternative is no Environments
// screen at all.
store
.health_project(&hash)
.ok()
.flatten()
.map(|f| (hash, f))
})
.collect()
};
let mut rows = Vec::with_capacity(candidates.len());
for candidate in candidates {
rows.push(probe_row(candidate.path, candidate.source).await);
let folder = folders.remove(&envs::env_hash(&candidate.path));
rows.push(probe_row(candidate.path, candidate.source, folder).await);
}
Ok(rows)
}
@@ -165,22 +208,46 @@ pub async fn env_scan(app: tauri::AppHandle) -> Wire<Vec<EnvRow>> {
///
/// The second use is what P0-10 needs: after `pip_upgrade` the Environments row must show the new
/// version without a full rescan, and one probe is the cheapest honest way to get it. That makes
/// carrying `pip_version` here load-bearing rather than tidy — a row refreshed through a path that
/// omitted it would blank the field it was refreshed to update.
/// carrying **every** per-row field here load-bearing rather than tidy — a row refreshed through a
/// path that omitted one would blank the field, and `upgradePip` replaces the row wholesale.
///
/// That warning was written about `pip_version` and then not applied twice over:
///
/// * `health_project` is read here for the same reason, or *Upgrade pip* makes the Health screen
/// forget where it was pointed.
/// * **`source` used to be hardcoded `Manual`**, so refreshing a registry-discovered interpreter
/// relabelled it *Added manually* in the chip at `PdEnvironments.tsx:31` — and, because
/// `PyEnv` carries the source too, handed that relabelled env to every later `pkg_list`. It is
/// now a parameter: `None` is the *Browse…* path, which really is manual; a refresh passes the
/// source the row already had.
///
/// # Errors
/// `PD-ENV-001` when there is no usable interpreter there, `PD-ENV-003` when the probe output
/// cannot be read.
#[tauri::command]
pub async fn env_probe(interpreter: String) -> Wire<EnvRow> {
pub async fn env_probe(
state: tauri::State<'_, AppState>,
interpreter: String,
source: Option<EnvSource>,
) -> Wire<EnvRow> {
let path = std::path::PathBuf::from(&interpreter);
let probed = envs::probe(&path, EnvSource::Manual).await?;
let env_hash = envs::env_hash(&path);
let source = source.unwrap_or(EnvSource::Manual);
let folder = {
let store = state.store.lock().await;
store.health_project(&env_hash).ok().flatten()
};
// Not `probe_row`, despite the shape: that one folds a probe failure onto the row because
// `env_scan` must not let one broken interpreter hide the rest. Here the caller asked about
// exactly this interpreter, so the failure is the answer and `?` is right.
let probed = envs::probe(&path, source).await?;
Ok(EnvRow {
interpreter,
source: EnvSource::Manual,
env_hash: envs::env_hash(&path),
source,
env_hash,
packages: Some(probed.dists.len()),
pip_version: pip_version_of(&probed.dists),
health_project: folder,
env: Some(probed.env),
error: None,
})
@@ -839,6 +906,21 @@ pub async fn health_run(
match outcome {
Ok(report) => {
// Remembered here rather than through a command of its own, so `EnvRow.healthProject`
// has something to carry on the next scan and the second Run costs one click fewer.
// Mirrors the CLI, which writes at the same point (`run::health`).
//
// Deliberately not `?`. The report the user asked for is in hand and P5 depends on it
// being parked; failing the whole command over a bookkeeping write would throw away
// findings that already cost three subprocesses.
{
let store = state.store.lock().await;
let _ = store.set_health_project(
&envs::env_hash(&env.interpreter),
&project.display().to_string(),
&jiff::Timestamp::now().to_string(),
);
}
// Parked rather than dropped: P5 checks its consent against this exact report, and the
// folder it ran in is what a fix would rewrite.
state
+18 -2
View File
@@ -133,6 +133,14 @@ export interface EnvRow {
* user-site install), not "not yet known".
*/
pipVersion?: string
/**
* The project folder Code Health last ran in for this environment.
*
* Absent means Health has never run here — which is a real state the screen renders as *choose a
* folder*, not "not yet known". Carried on the row rather than fetched: the same trade `pipVersion`
* makes, and the reason P4 needed no command of its own.
*/
healthProject?: string
error?: PdError
}
@@ -170,8 +178,16 @@ export const appInfo = (): Promise<AppInfo> => invoke('app_info')
export const envScan = (): Promise<EnvRow[]> => invoke('env_scan')
export const envProbe = (interpreter: string): Promise<EnvRow> =>
invoke('env_probe', { interpreter })
/**
* Re-probe one interpreter and rebuild its row.
*
* **Pass `source` when refreshing an existing row.** Omitting it means *Browse…*, and the row comes
* back labelled `manual` — which is what used to happen to every row `upgradePip` refreshed,
* silently relabelling a registry-discovered Python in the chip and in the `PyEnv` handed to every
* later `pkgList`.
*/
export const envProbe = (interpreter: string, source?: EnvSource): Promise<EnvRow> =>
invoke('env_probe', source === undefined ? { interpreter } : { interpreter, source })
/**
* Everything installed in `env` — the Installed table.
+5 -1
View File
@@ -316,7 +316,11 @@ export const useEnvStore = create<EnvState>((set, get) => ({
// A whole new array, not a mutated element: zustand compares references, so writing
// `row.pipVersion = …` in place would leave the screen showing the old number until some
// unrelated field happened to change.
const fresh = await envProbe(interpreter)
//
// The row replaces wholesale, so the probe has to reproduce *every* field the row carried.
// `row.source` is passed for that reason: without it the refreshed row came back `manual`,
// and a registry-discovered interpreter changed its chip because its pip was upgraded.
const fresh = await envProbe(interpreter, row.source)
set((state) => ({
rows: state.rows.map((r) => (r.interpreter === interpreter ? fresh : r)),
upgradingPip: null,