Repository navigation
feat(history --diff) + perf(tui): cancellable preview - #89
Merged
Merged
Conversation
`nbox history <kind> <ref> --diff` shows the full before/after JSON for the newest change (the compact `fields_changed` summary stays the default). Mirrors into MCP as `nbox_history` with `diff=true`. The ObjectChange model already carried `prechange_data`/`postchange_data` (kilobytes each); this surfaces them on opt-in only: - HistoryRow gains `before`/`after` (Option<Value>), `skip_serializing_if = None` → default output byte-identical (the compact list never carries them). - HistoryView::from_models(changes, diff): when `diff`, each row carries the full payloads; `fields_changed` is ALWAYS derived from them (no regression to the compact list). - CLI `--diff` implies `--limit 1` (an explicit `--limit` still wins) so the common case is one change's full diff; MCP `diff` pairs with the agent's `limit` (e.g. 1) for the same. - Plain render: an indented `before:`/`after:` pretty-JSON block per row (diff mode only); JSON output carries the raw payloads. Tests: 2 history_view unit tests (diff carries before/after; plain render blocks; compact list unchanged) + 1 MCP contract test (diff=false omits before/after, diff=true surfaces them). The existing `history_view_shape_is_pinned` contract (diff=false) stays byte-identical. Gate: 1145 all-features / 1050 no-default (+3), clippy -D warnings both legs, fmt clean. Docs: AGENTS.md history line gains [--diff]; docs/MCP.md nbox_history notes diff=true; ROADMAP history item -> done + --diff; CHANGELOG [Unreleased].
Scrolling the results list spawns a background preview fetch per settle; a second settle can land while the first fetch is still in flight (NetBox detail fetches take hundreds of ms to seconds), so the abandoned task ran to completion and was dropped on arrival by the stale-response suppression. The event loop now aborts the superseded task instead, freeing the connection + CPU. - Extract `spawn_preview(kind, id, client, cache, tx) -> JoinHandle` from dispatch's LoadPreview arm. - `dispatch` gains `preview_task: &mut Option<JoinHandle<()>>`; the LoadPreview arm aborts the old handle before storing the new one. All three dispatch call sites (two startup probes + the loop) thread the handle through. - Safe with the cache: get_or_fetch's per-key async mutex releases on future drop, so a concurrent open of the same object re-acquires and re-fetches — no deadlock, no poisoned entry. The stale-response suppression (PreviewLoaded dropped if the cursor moved on) stays as a backstop. - The debounce + dedup still coalesce a burst of j/k into one load; "preview warms open" (opening a row uses the warmed detail cache) is unaffected. Test: `preview_supersede_aborts_in_flight_fetch` (wiremock, tokio). Uses a single-GET kind (Asn) for a deterministic setup: id=1's mock hangs 30s, id=2 answers fast. Asserts the aborted handle resolves with a cancelled JoinError within 1s (not hanging on the slow response → the fetch was cancelled), the superseding preview delivers its event, and no extra events arrive (the cancelled task never reached tx.send). 10/10 deterministic runs. The dispatch arm (take/abort/store) is trivial by inspection; the test covers the cancellation mechanism + spawn_preview. Gate: 1146 all-features / 1051 no-default (+1), clippy -D warnings both legs, fmt clean. Docs: ROADMAP "Cheap/cancellable TUI preview" -> done; CHANGELOG [Unreleased].
…TS table The tool description and the AGENTS MCP table still said history rows do not include the full before/after JSON, which is stale now that diff=true returns them — and the description never mentioned the new arg, so an agent reading the schema wouldn't discover it. Mirror the (already-correct) docs/MCP.md wording.
Pins the load-bearing safety claim behind the TUI's cancellable preview (#89, commit 2): aborting a `get_or_fetch` mid-fetch releases the per-key single-flight lock (the `_guard` drops with the future), so a subsequent fetch for the same key re-acquires and completes — no deadlock, no poisoned entry. The preview cancellation test (`preview_supersede_aborts_in_flight_fetch`) used `Cache::disabled()`, so the enabled-cache per-key-mutex path — the actual hazardous surface (a std Mutex would poison here; tokio's AsyncMutex guard releases on drop) — wasn't exercised. This test targets it directly: - Spawn a `get_or_fetch` whose fetch hangs on a never-signalled `Notify` (the Arc stays alive, so it hangs rather than erroring) — the task acquires the single-flight guard and reaches `fetch().await`. - Abort the JoinHandle (exactly what `dispatch` does to a superseded preview), assert it resolves cancelled (not hung on the notify). - Re-fetch the SAME key: must succeed promptly. The re-fetch is wrapped in a 2s timeout so a deadlock regression fails fast instead of hanging the runner. - Assert the re-fetched value is served + a subsequent hit does not re-fetch (no poisoned/empty entry left behind). Mutation-verified: removing `first.abort()` makes the re-fetch deadlock → the 2s timeout fires → the test fails fast at the `expect` (1.08s, not a hang). Restoring the abort passes in 0.08s. 8/8 deterministic runs. The contract belongs in the cache tests (where `test_cache()` lives), not the TUI: `spawn_preview` just calls `get_or_fetch`; the safety is a cache property. Gate: 1147 all-features / 1052 no-default (+1), clippy -D warnings both legs, fmt clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two unrelated follow-ups in one PR, as two independent commits so each is reviewable (and the
--diffwin is cherry-pickable if the preview work needs iteration).Commit 1 —
feat(history): --diff for full before/after change payloads (CLI + MCP)Closes the loop on
nbox history(#84): theObjectChangemodel already carriedprechange_data/postchange_data(kilobytes each); this surfaces them on opt-in.HistoryRowgainsbefore/after(Option<Value>,skip_serializing_if = None) → default output byte-identical (the compact list never carries them).HistoryView::from_models(changes, diff): whendiff, each row carries the full payloads;fields_changedis always derived from them (no regression to the compact list — I caught and fixed this mid-build).--diffimplies--limit 1(an explicit--limitstill wins) → the common case is one change's full diff; MCPdiff=truepairs with the agent'slimit.before:/after:pretty-JSON block per row (diff mode only).Tests: 2
history_viewunit tests (diff carries before/after; plain render blocks; compact list unchanged) + 1 MCP contract test (diff=false omits, diff=true surfaces). The existinghistory_view_shape_is_pinnedcontract (diff=false) stays byte-identical.Commit 2 —
perf(tui): abort superseded in-flight preview fetchesScrolling spawns a background preview fetch per settle; a second settle while the first is still in flight (NetBox detail fetches take hundreds of ms–seconds) ran to completion and was dropped on arrival. Now the loop aborts the superseded task — freeing the connection + CPU.
spawn_preview(...) -> JoinHandle;dispatchgainspreview_task: &mut Option<JoinHandle>, the LoadPreview arm aborts the old handle before storing the new one.get_or_fetch's per-key async mutex releases on future drop → a concurrent open of the same object re-acquires and re-fetches (no deadlock, no poisoned entry). Stale-response suppression stays as a backstop.Test:
preview_supersede_aborts_in_flight_fetch(wiremock, tokio) — uses a single-GET kind (Asn) for a deterministic setup (id=1 hangs 30s, id=2 fast). Asserts the aborted handle resolves with a cancelledJoinErrorwithin 1s (not hanging → fetch cancelled), the superseding preview delivers, and no extra events arrive (cancelled task never reachedtx.send). 10/10 deterministic runs. The dispatch arm (take/abort/store) is trivial by inspection; the test covers the cancellation mechanism +spawn_preview.Verification
fmtclean;clippy --all-targets -D warningson--all-featuresand--no-default-features.Docs
--diff: AGENTS.md history line gains[--diff]; docs/MCP.mdnbox_historynotesdiff=true; ROADMAP history item ☑ +--diff; CHANGELOG[Unreleased]. Cancellation: ROADMAP "Cheap/cancellable TUI preview" ☑; CHANGELOG[Unreleased].Notes
--difftest caught a real regression mid-build (I'd accidentally gatedfields_changedondiff, which would have blanked the compact list's field summary) — fixed before commit, so the regression never landed.Ok(None)channel-closed was treated as an extra event); the drain version is correct and deterministic. The implementation was never wrong — the assertion was.