Skip to content

feat(history --diff) + perf(tui): cancellable preview - #89

Merged
lance0 merged 4 commits into
masterfrom
feat/history-diff-and-cancellable-preview
Jun 25, 2026
Merged

lance0 merged 4 commits into
masterfrom
feat/history-diff-and-cancellable-preview

Conversation

@lance0

@lance0 lance0 commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Two unrelated follow-ups in one PR, as two independent commits so each is reviewable (and the --diff win 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): the ObjectChange model already carried prechange_data/postchange_data (kilobytes each); this surfaces them on opt-in.

  • 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 — I caught and fixed this mid-build).
  • CLI --diff implies --limit 1 (an explicit --limit still wins) → the common case is one change's full diff; MCP diff=true pairs with the agent's limit.
  • Plain render: an indented before:/after: pretty-JSON block per row (diff mode only).

Tests: 2 history_view unit tests (diff carries before/after; plain render blocks; compact list unchanged) + 1 MCP contract test (diff=false omits, diff=true surfaces). The existing history_view_shape_is_pinned contract (diff=false) stays byte-identical.

Commit 2 — perf(tui): abort superseded in-flight preview fetches

Scrolling 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.

  • Extract spawn_preview(...) -> JoinHandle; dispatch gains preview_task: &mut Option<JoinHandle>, the LoadPreview arm aborts the old handle before storing the new one.
  • Safe with the cache: 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.
  • Debounce + dedup + "preview warms open" all unaffected.

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 cancelled JoinError within 1s (not hanging → fetch cancelled), the superseding preview delivers, and no extra events arrive (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.

Verification

  • fmt clean; clippy --all-targets -D warnings on --all-features and --no-default-features.
  • 1146 all-features (was 1145 after commit 1, +1 cancellation test) / 1051 no-default, 0 failed.

Docs

--diff: AGENTS.md history line gains [--diff]; docs/MCP.md nbox_history notes diff=true; ROADMAP history item ☑ + --diff; CHANGELOG [Unreleased]. Cancellation: ROADMAP "Cheap/cancellable TUI preview" ☑; CHANGELOG [Unreleased].

Notes

  • The --diff test caught a real regression mid-build (I'd accidentally gated fields_changed on diff, which would have blanked the compact list's field summary) — fixed before commit, so the regression never landed.
  • The cancellation test's first draft had a buggy assertion (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.

lance0 added 4 commits June 25, 2026 12:45
`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.
@lance0
lance0 merged commit 04d9f01 into master Jun 25, 2026
4 checks passed
@lance0
lance0 deleted the feat/history-diff-and-cancellable-preview branch June 26, 2026 20:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant