feat(ui): Fix N safely fixable issues, and a confirm that says what it cannot undo
**One dialog with two states, not two dialogs.** Two confirms would break the click budget, and the second is the one nobody reads. `PdDialog` renders Cancel first and focuses it, so Enter without Tab cancels — UI-SPEC §7's rule for a destructive confirm, inherited rather than re-implemented. `button[data-action='fix']` joins the forced-colors list in `styles.css`, because high contrast drops every tint and a destructive primary that looks like Cancel is worse than no colour at all. The unconditional line is *PipDock cannot undo this; only your own version control can* — true whether or not there is a repository. The **danger** block is reserved for a tree that really has uncommitted work, so a folder outside version control is not nagged about. That is the same reasoning `fix::dirty` documents for answering `None` rather than warning. `health_dirty` is a command of its own rather than a field on the report, because the answer expires: the user can commit, or start editing, between the run and the button. It is asked when the dialog opens, and `health_fix` asks **again** before writing — this answer decides what to render, that one decides what to allow. Every count comes off the report. The button names `fixable`, the dialog names `fixableFiles`, and neither is derived from the badges rendered beside them — the button, the CLI prompt and the server's own check all have to name one number, which is what `recount` exists for. `fix` is in `NO_HEALTH`, so a second Run closes an open confirm. The server replaces its parked report on a new run, and a dialog left open would let the user confirm a count that no longer exists on either side. On success the tab drops to `remaining` with **no second Run**, which is why the server re-parks its session rather than releasing it. On failure the dialog closes and the report stays: nothing was written, so it is still true, and every failure here is one the user has to act on elsewhere — commit, clear a read-only flag, or re-run. Six store tests, including the two that would pass silently: that `acknowledgedDirty` is true only when the tree really is dirty, and that a failed dirty check opens the confirm anyway rather than blocking the fix for a reason nobody can act on. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
545ca7fc89
commit
3e72ecf7c4
11 files changed
+267
-3
No files matched your search
@@ -940,6 +940,26 @@ pub async fn health_run(
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the parked report's project has uncommitted work, so the confirm can say so.
|
||||
///
|
||||
/// A command of its own rather than a field on `HealthReport`, because the answer expires: the
|
||||
/// user may commit, or start editing, between the run and the Fix button. Asked when the dialog
|
||||
/// opens; `health_fix` asks **again** before writing, because this one decides what to *render*
|
||||
/// and that one decides what to allow.
|
||||
///
|
||||
/// `null` means no repository, no git, or git failed — CODE-HEALTH-SPEC §5's check is a courtesy,
|
||||
/// not a precondition, and a folder outside version control is a choice rather than a warning.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-INT-001` when no run is parked.
|
||||
#[tauri::command]
|
||||
pub async fn health_dirty(state: tauri::State<'_, AppState>) -> Wire<Option<usize>> {
|
||||
let session = state.health.claim_one().await?;
|
||||
let found = pipdock_core::health::fix::dirty(&session.project).await;
|
||||
state.health.park(session).await;
|
||||
Ok(found.map(|tree| tree.entries))
|
||||
}
|
||||
|
||||
/// Apply ruff's safe fixes to the project the parked report was produced from.
|
||||
///
|
||||
/// **Takes no `project`, no `env`, no file list and no findings.** The project and environment
|
||||
|
||||
@@ -69,6 +69,7 @@ pub fn run() {
|
||||
commands::report_bug_url,
|
||||
commands::engine_info,
|
||||
commands::pip_upgrade,
|
||||
commands::health_dirty,
|
||||
commands::health_fix,
|
||||
commands::health_run,
|
||||
commands::health_save_report,
|
||||
|
||||
@@ -49,6 +49,8 @@ interface PdHealthReportProps {
|
||||
installed: readonly string[]
|
||||
/** Hand a distribution to the uninstall guard. Absent means the handoff is not wired yet. */
|
||||
onUninstall?: ((distribution: string) => void) | undefined
|
||||
/** Open the fix confirm. Absent means the write path is not available to this caller. */
|
||||
onFix?: (() => void) | undefined
|
||||
/** How many ruff findings to show; overridable so a test does not have to build 500 rows. */
|
||||
rowsShown?: number
|
||||
}
|
||||
@@ -171,6 +173,7 @@ export function PdHealthReport({
|
||||
onTab,
|
||||
installed,
|
||||
onUninstall,
|
||||
onFix,
|
||||
rowsShown = RUFF_ROWS_SHOWN,
|
||||
}: PdHealthReportProps) {
|
||||
const { t } = useTranslation()
|
||||
@@ -281,6 +284,18 @@ export function PdHealthReport({
|
||||
|
||||
{state === 'findings' && tab === 'ruff' ? (
|
||||
<>
|
||||
{/* The count is `fixable` off the report, never the badges rendered below: the button,
|
||||
the CLI prompt and the server's own check all have to name one number. */}
|
||||
{onFix !== undefined && report.ruff.fixable > 0 ? (
|
||||
<button
|
||||
type="button"
|
||||
data-action="fix"
|
||||
className="mb-3 rounded-pd bg-danger px-3 py-1 text-data text-bg"
|
||||
onClick={onFix}
|
||||
>
|
||||
{t('health.fix', { count: report.ruff.fixable })}
|
||||
</button>
|
||||
) : null}
|
||||
{capped.map((group) => (
|
||||
// Sectioned groups, as `PdPreviewDiff` groups its changes: a flat list of a
|
||||
// thousand lint findings answers no question anyone actually has.
|
||||
|
||||
@@ -69,6 +69,7 @@ export const COMMANDS = [
|
||||
'snapshot_rollback_preview',
|
||||
'snapshot_rollback',
|
||||
'health_run',
|
||||
'health_dirty',
|
||||
'health_fix',
|
||||
'health_save_report',
|
||||
'engine_info',
|
||||
@@ -414,6 +415,14 @@ export const healthRun = (env: PyEnv, project: string): Promise<HealthReport> =>
|
||||
* Rejects with `PD-RES-002` when the project changed since the confirm, and `PD-PRM-003` when a
|
||||
* target cannot be written — the latter before anything is.
|
||||
*/
|
||||
/**
|
||||
* Uncommitted entries in the parked report's project, or `null` when we cannot say.
|
||||
*
|
||||
* Asked when the fix dialog opens, so the confirm can name a number. `healthFix` asks again
|
||||
* server-side before writing: this answer decides what to *render*, that one decides what to allow.
|
||||
*/
|
||||
export const healthDirty = (): Promise<number | null> => invoke('health_dirty')
|
||||
|
||||
export const healthFix = (files: number, acknowledgedDirty: boolean): Promise<FixReport> =>
|
||||
invoke('health_fix', { files, acknowledgedDirty })
|
||||
|
||||
|
||||
@@ -251,7 +251,17 @@
|
||||
"log": "Log",
|
||||
"save": "Save report",
|
||||
"saveTitle": "Save the Code Health report",
|
||||
"saved": "Saved {{files}}"
|
||||
"saved": "Saved {{files}}",
|
||||
"fix_one": "Fix {{count}} safely fixable issue",
|
||||
"fix_other": "Fix {{count}} safely fixable issues",
|
||||
"fixTitle": "Rewrite your source files?",
|
||||
"fixBody_one": "ruff will apply its safe fixes to {{count}} file.",
|
||||
"fixBody_other": "ruff will apply its safe fixes to {{count}} files.",
|
||||
"fixCannotUndo": "PipDock cannot undo this. Only your own version control can.",
|
||||
"fixDirty_one": "This repository has {{count}} uncommitted change. `git checkout .` would take your own edits with it as well as the fix.",
|
||||
"fixDirty_other": "This repository has {{count}} uncommitted changes. `git checkout .` would take your own edits with them as well as the fix.",
|
||||
"fixConfirm": "Fix",
|
||||
"fixConfirmDirty": "Fix anyway"
|
||||
},
|
||||
"uninstall": {
|
||||
"title": "Remove packages",
|
||||
|
||||
@@ -232,7 +232,14 @@
|
||||
"log": "Nhật ký",
|
||||
"save": "Lưu báo cáo",
|
||||
"saveTitle": "Lưu báo cáo Sức khỏe mã",
|
||||
"saved": "Đã lưu {{files}}"
|
||||
"saved": "Đã lưu {{files}}",
|
||||
"fix_other": "Sửa {{count}} vấn đề sửa được an toàn",
|
||||
"fixTitle": "Ghi đè lên mã nguồn của bạn?",
|
||||
"fixBody_other": "ruff sẽ áp dụng các bản sửa an toàn cho {{count}} tệp.",
|
||||
"fixCannotUndo": "PipDock không hoàn tác được việc này. Chỉ hệ quản lý phiên bản của bạn mới làm được.",
|
||||
"fixDirty_other": "Kho này có {{count}} thay đổi chưa commit. `git checkout .` sẽ xoá cả các sửa đổi của bạn lẫn bản sửa này.",
|
||||
"fixConfirm": "Sửa",
|
||||
"fixConfirmDirty": "Vẫn sửa"
|
||||
},
|
||||
"uninstall": {
|
||||
"title": "Gỡ gói",
|
||||
|
||||
@@ -15,6 +15,7 @@ import { useEffect, useState } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
|
||||
import { PdConsoleDrawer } from '@/components/PdConsoleDrawer'
|
||||
import { PdDialog } from '@/components/PdDialog'
|
||||
import { PdEmptyState } from '@/components/PdEmptyState'
|
||||
import { PdErrorRow } from '@/components/PdErrorRow'
|
||||
import { PdHealthReport } from '@/components/PdHealthReport'
|
||||
@@ -64,6 +65,12 @@ export function PdHealth() {
|
||||
const setTab = useHealthStore((s) => s.setTab)
|
||||
const setConsoleOpen = useHealthStore((s) => s.setConsoleOpen)
|
||||
const run = useHealthStore((s) => s.run)
|
||||
const fix = useHealthStore((s) => s.fix)
|
||||
const fixBusy = useHealthStore((s) => s.fixBusy)
|
||||
const fixError = useHealthStore((s) => s.fixError)
|
||||
const openFix = useHealthStore((s) => s.openFix)
|
||||
const closeFix = useHealthStore((s) => s.closeFix)
|
||||
const confirmFix = useHealthStore((s) => s.confirmFix)
|
||||
|
||||
const row = rows.find((r) => r.interpreter === selected)
|
||||
// Hoisted, for `PdPackages`' reason: a narrowing that has to be re-derived inside a closure is
|
||||
@@ -175,6 +182,14 @@ export function PdHealth() {
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{/* A refused or failed fix. Separate from `error`, which is a run that failed: this one
|
||||
leaves the report on screen, because it is still true. */}
|
||||
{fixError !== null ? (
|
||||
<div className="mt-2 shrink-0">
|
||||
<PdErrorRow error={fixError} />
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{/* Above the tabs, and counted into `⚠ n`. A failed tool is a problem currently on
|
||||
screen, and there are at most three of them — this is not `PdSummarySheet`'s
|
||||
one-row-per-package case. Above rather than inside a tab, because the explanation
|
||||
@@ -205,6 +220,9 @@ export function PdHealth() {
|
||||
tab={tab}
|
||||
onTab={setTab}
|
||||
installed={dists.map((d) => d.name)}
|
||||
onFix={() => {
|
||||
void openFix()
|
||||
}}
|
||||
onUninstall={
|
||||
env === undefined
|
||||
? undefined
|
||||
@@ -221,6 +239,42 @@ export function PdHealth() {
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* One dialog with two states, not two dialogs: two confirms would break the click budget
|
||||
and the second is the one nobody reads. Cancel is rendered first and focused by
|
||||
`PdDialog`, so Enter without Tab cancels — UI-SPEC §7's rule for a destructive confirm. */}
|
||||
{fix !== null ? (
|
||||
<PdDialog
|
||||
label={t('health.fixTitle')}
|
||||
title={t('health.fixTitle')}
|
||||
cancelLabel={t('actions.cancel')}
|
||||
busy={fixBusy}
|
||||
onCancel={closeFix}
|
||||
actions={
|
||||
<button
|
||||
type="button"
|
||||
data-action="fix"
|
||||
className="rounded-pd bg-danger px-3 py-1 text-data text-bg disabled:opacity-50"
|
||||
disabled={fixBusy}
|
||||
onClick={() => {
|
||||
void confirmFix()
|
||||
}}
|
||||
>
|
||||
{fix.dirty === null ? t('health.fixConfirm') : t('health.fixConfirmDirty')}
|
||||
</button>
|
||||
}
|
||||
>
|
||||
<p className="text-data">{t('health.fixBody', { count: fix.files })}</p>
|
||||
{/* Unconditional, and true in every case — including no repository at all, which is
|
||||
deliberately not treated as a warning of its own. */}
|
||||
<p className="mt-2 text-data text-text-dim">{t('health.fixCannotUndo')}</p>
|
||||
{fix.dirty === null ? null : (
|
||||
<p className="mt-2 border-l-2 border-danger pl-2 text-data text-danger">
|
||||
{t('health.fixDirty', { count: fix.dirty })}
|
||||
</p>
|
||||
)}
|
||||
</PdDialog>
|
||||
) : null}
|
||||
|
||||
<PdConsoleDrawer
|
||||
lines={consoleLines}
|
||||
open={consoleOpen}
|
||||
|
||||
@@ -204,6 +204,85 @@ describe('useHealthStore', () => {
|
||||
expect(ipc.healthRun).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('names the file count off the report, and asks the server about the tree', async () => {
|
||||
vi.mocked(ipc.healthDirty).mockResolvedValue(4)
|
||||
useHealthStore.setState({ report: REPORT })
|
||||
await useHealthStore.getState().openFix()
|
||||
|
||||
// `fixableFiles`, not a count the UI derived: the dialog, the CLI prompt and the server's own
|
||||
// check all have to name one number, which is what `recount` exists for.
|
||||
expect(useHealthStore.getState().fix).toEqual({ files: REPORT.ruff.fixableFiles, dirty: 4 })
|
||||
})
|
||||
|
||||
it('opens the confirm anyway when the dirty check fails', async () => {
|
||||
// A failed check is the `null` case by definition. Refusing to open would block the fix for a
|
||||
// reason the user cannot act on.
|
||||
vi.mocked(ipc.healthDirty).mockRejectedValue(new Error('no git'))
|
||||
useHealthStore.setState({ report: REPORT })
|
||||
await useHealthStore.getState().openFix()
|
||||
expect(useHealthStore.getState().fix?.dirty).toBeNull()
|
||||
})
|
||||
|
||||
it('tells the server the tree was acknowledged only when it is dirty', async () => {
|
||||
vi.mocked(ipc.healthDirty).mockResolvedValue(2)
|
||||
vi.mocked(ipc.healthFix).mockResolvedValue({
|
||||
filesChanged: 2,
|
||||
remaining: { findings: [], fixable: 0, fixableFiles: 0 },
|
||||
notApplied: 0,
|
||||
fixedAt: '2026-08-13T10:00:00Z',
|
||||
})
|
||||
useHealthStore.setState({ report: REPORT })
|
||||
await useHealthStore.getState().openFix()
|
||||
await useHealthStore.getState().confirmFix()
|
||||
|
||||
expect(ipc.healthFix).toHaveBeenCalledWith(REPORT.ruff.fixableFiles, true)
|
||||
})
|
||||
|
||||
it('drops the tab to what remains, with no second run', async () => {
|
||||
const remaining = { findings: [REPORT.ruff.findings[2]!], fixable: 0, fixableFiles: 0 }
|
||||
vi.mocked(ipc.healthDirty).mockResolvedValue(null)
|
||||
vi.mocked(ipc.healthFix).mockResolvedValue({
|
||||
filesChanged: 2,
|
||||
remaining,
|
||||
notApplied: 0,
|
||||
fixedAt: '2026-08-13T10:00:00Z',
|
||||
})
|
||||
useHealthStore.setState({ report: REPORT })
|
||||
await useHealthStore.getState().openFix()
|
||||
await useHealthStore.getState().confirmFix()
|
||||
|
||||
const after = useHealthStore.getState()
|
||||
expect(after.report?.ruff.findings).toHaveLength(1)
|
||||
expect(after.ruffByFile).toHaveLength(1)
|
||||
expect(after.fix).toBeNull()
|
||||
expect(ipc.healthRun).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('closes the confirm and keeps the report when a fix is refused', async () => {
|
||||
vi.mocked(ipc.healthDirty).mockResolvedValue(null)
|
||||
vi.mocked(ipc.healthFix).mockRejectedValue({ code: 'PD-PRM-003', message: 'read-only' })
|
||||
useHealthStore.setState({ report: REPORT })
|
||||
await useHealthStore.getState().openFix()
|
||||
await useHealthStore.getState().confirmFix()
|
||||
|
||||
const after = useHealthStore.getState()
|
||||
expect(after.fix).toBeNull()
|
||||
expect(after.fixError?.code).toBe('PD-PRM-003')
|
||||
// The report is still true — nothing was written — so it stays on screen.
|
||||
expect(after.report).toBe(REPORT)
|
||||
})
|
||||
|
||||
it('closes an open confirm when a second run starts', async () => {
|
||||
// The server replaces the parked report on a new run, so a dialog left open would let the
|
||||
// user confirm a count that no longer exists on either side.
|
||||
vi.mocked(ipc.onHealthProgress).mockResolvedValue(() => undefined)
|
||||
vi.mocked(ipc.healthRun).mockResolvedValue(REPORT)
|
||||
useHealthStore.getState().setFolder('C:\\proj')
|
||||
useHealthStore.setState({ fix: { files: 2, dirty: null } })
|
||||
await useHealthStore.getState().run(ENV, 'aaa')
|
||||
expect(useHealthStore.getState().fix).toBeNull()
|
||||
})
|
||||
|
||||
it('does nothing without a folder', async () => {
|
||||
await useHealthStore.getState().run(ENV, 'aaa')
|
||||
expect(ipc.healthRun).not.toHaveBeenCalled()
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
import { create } from 'zustand'
|
||||
|
||||
import {
|
||||
healthDirty,
|
||||
healthFix,
|
||||
healthRun,
|
||||
isPdError,
|
||||
onHealthProgress,
|
||||
@@ -15,6 +17,7 @@ import {
|
||||
type PdError,
|
||||
type ProgressEvent,
|
||||
type PyEnv,
|
||||
type FixReport,
|
||||
type RuffFinding,
|
||||
} from '@/ipc'
|
||||
import { apply, type ConsoleLine } from '@/stores/plan'
|
||||
@@ -122,6 +125,21 @@ interface HealthState {
|
||||
current: string | null
|
||||
consoleOpen: boolean
|
||||
|
||||
/**
|
||||
* The fix confirm: `null` closed, otherwise what it is asking about.
|
||||
*
|
||||
* `dirty` is `null` when we cannot say — no repository, no git — which is deliberately **not** a
|
||||
* warning; the danger state is reserved for a tree that really has uncommitted work.
|
||||
*/
|
||||
fix: { files: number; dirty: number | null } | null
|
||||
fixBusy: boolean
|
||||
fixError: PdError | null
|
||||
/** Ask the server about the tree, then open the confirm. */
|
||||
openFix: () => Promise<void>
|
||||
closeFix: () => void
|
||||
/** Apply the fix and fold the result back into the report on screen. */
|
||||
confirmFix: () => Promise<void>
|
||||
|
||||
setFolder: (folder: string) => void
|
||||
setTab: (tab: HealthTab) => void
|
||||
setConsoleOpen: (open: boolean) => void
|
||||
@@ -141,6 +159,11 @@ interface HealthState {
|
||||
* open is a preference about the screen, not a property of a report.
|
||||
*/
|
||||
const NO_HEALTH = {
|
||||
// In the reset because a second Run replaces the report the dialog is asking about — leaving it
|
||||
// open would let the user confirm a count the server has already thrown away.
|
||||
fix: null,
|
||||
fixBusy: false,
|
||||
fixError: null,
|
||||
reportFor: null,
|
||||
report: null,
|
||||
ruffByFile: [] as RuffFileGroup[],
|
||||
@@ -166,6 +189,49 @@ export const useHealthStore = create<HealthState>((set, get) => ({
|
||||
consoleOpen: false,
|
||||
...NO_HEALTH,
|
||||
|
||||
openFix: async () => {
|
||||
const { report, fixBusy } = get()
|
||||
if (report === null || report.ruff.fixable === 0 || fixBusy) return
|
||||
set({ fixError: null })
|
||||
// The count comes off the report, never recounted here: the dialog, the CLI prompt and the
|
||||
// server's own check all have to name one number, and `recount` exists so they do.
|
||||
const files = report.ruff.fixableFiles
|
||||
try {
|
||||
set({ fix: { files, dirty: await healthDirty() } })
|
||||
} catch {
|
||||
// A dirty check that failed is the `null` case by definition, and refusing to open the
|
||||
// dialog over it would block the fix for a reason the user cannot act on.
|
||||
set({ fix: { files, dirty: null } })
|
||||
}
|
||||
},
|
||||
|
||||
closeFix: () => set({ fix: null, fixBusy: false }),
|
||||
|
||||
confirmFix: async () => {
|
||||
const { fix, report, reportFor, folder } = get()
|
||||
if (fix === null || report === null) return
|
||||
|
||||
set({ fixBusy: true, fixError: null })
|
||||
try {
|
||||
const fixed: FixReport = await healthFix(fix.files, fix.dirty !== null)
|
||||
// The tab count drops to what remains with no second Run — which is why the server re-parks
|
||||
// its session with the same findings rather than releasing it.
|
||||
const next = { ...report, ruff: fixed.remaining }
|
||||
set({
|
||||
report: next,
|
||||
ruffByFile: groupRuff(fixed.remaining.findings),
|
||||
reportFor,
|
||||
folder,
|
||||
fix: null,
|
||||
fixBusy: false,
|
||||
})
|
||||
} catch (e) {
|
||||
// The dialog closes: every failure here is one the user has to do something about
|
||||
// elsewhere — commit, clear a read-only flag, or re-run — and none is retryable in place.
|
||||
set({ fix: null, fixBusy: false, fixError: asPdError(e) })
|
||||
}
|
||||
},
|
||||
|
||||
setFolder: (folder) =>
|
||||
set((state) => (state.folder === folder ? {} : { folder, ...NO_HEALTH })),
|
||||
|
||||
|
||||
+2
-1
@@ -156,7 +156,8 @@
|
||||
button[data-action='force'],
|
||||
button[data-action='uninstall'],
|
||||
button[data-action='rollback'],
|
||||
button[data-action='upgrade-pip'] {
|
||||
button[data-action='upgrade-pip'],
|
||||
button[data-action='fix'] {
|
||||
border: 2px solid CanvasText;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,7 @@ export interface IpcMock {
|
||||
pickSavePath: ReturnType<typeof vi.fn>
|
||||
healthSaveReport: ReturnType<typeof vi.fn>
|
||||
healthFix: ReturnType<typeof vi.fn>
|
||||
healthDirty: ReturnType<typeof vi.fn>
|
||||
isPdError: (value: unknown) => boolean
|
||||
}
|
||||
|
||||
@@ -82,6 +83,7 @@ export function ipcMock(): IpcMock {
|
||||
pickSavePath: vi.fn().mockResolvedValue(null),
|
||||
healthSaveReport: vi.fn().mockResolvedValue([]),
|
||||
healthFix: vi.fn(),
|
||||
healthDirty: vi.fn().mockResolvedValue(null),
|
||||
// Real, not a spy: the stores decide whether a rejection carries a catalog code by asking it,
|
||||
// and a stub that always answered one way would make every error test agree with itself.
|
||||
isPdError: (value: unknown): boolean =>
|
||||
|
||||
Reference in new issue
Block a user