Skip to content

fix(documents): drop fetched state from a previous contract or type - #1155

Merged
lklimek merged 9 commits into
v1.0-devfrom
fix/document-action-stale-state
Oct 29, 2026
Merged

lklimek merged 9 commits into
v1.0-devfrom
fix/document-action-stale-state

Conversation

@Claudius-Maginificent

@Claudius-Maginificent Claudius-Maginificent commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR: Document actions (Replace, Purchase, Delete, Create) no longer keep a document, price, or field values fetched for one contract or document type after you pick another, and a slow fetch from an earlier pick no longer fills in the form.

User story

DOC-006 / DOC-009 (Replace a document; Purchase a document): as a developer, I want the document action form to show and send only what belongs to the contract and document type I have selected, so I never replace or buy the wrong document by mistake.

Scenario

Base flow

Open Contracts → Replace Document (or Purchase Document). Pick contract A and a document type, enter a document ID, and click Fetch (or Fetch Document Price). Then pick contract B or another document type.

Actual behavior

  • The fetched document, its field values, and the fetched price stay on the screen. Replace / Purchase stays enabled with data from contract A while contract B's type is submitted.
  • If the fetch for contract A was still running when you switched, its result still arrives and fills in the form for contract B. An error from that abandoned fetch still raises a banner.

Expected behavior

  • Picking another contract or document type clears everything fetched or entered for the old selection. You fetch again before you can replace or buy.
  • A fetch that was running when you changed the selection is dropped, both its result and any error. Only the fetch for the current selection fills in the form.

Detailed discussion

What was done

All changes are in src/ui/contracts_documents/document_action_screen.rs:

  • Correlated fetches. The three fetch buttons (Fetch Owned Documents, Fetch Document Price, Fetch) dispatch through dispatch_fetch(), which tracks the request in a PendingRequest (ui/state/pending_request.rs, the same pattern ViewTokenClaimsScreen uses). Each dispatch gets its own BackendTaskContext::Dispatched id, so two identical queries can still be told apart.
  • Result gating. display_backend_task_result applies a Documents result only when it settles the pending fetch; anything else is ignored. Non-fetch results (broadcast outcomes) pass through as before.
  • Error gating. should_suppress_backend_task_error hides errors from a dispatched document fetch the screen no longer waits on. display_backend_task_error ends the wait for the current fetch.
  • Selection change. On a contract or document-type change, clear_selection_state() abandons the pending fetch and clears original_doc, field_inputs, fetched_price, fetched_documents, and no_documents_found. It also resets a Fetching / Fetched status. Before this PR, only fetched_documents and no_documents_found were cleared.
  • Create fields reset on a type change. field_inputs is shared by Create and Replace, so Create also starts with empty fields after you pick another document type or contract. Those fields belonged to the old type's schema.
  • A running broadcast is left alone. Abandoning a fetch dismisses the progress banner and resets the status only while the screen still shows Fetching. A broadcast started meanwhile keeps Broadcasting and its "Broadcasting..." banner.
  • Identity change already cleared the owned-documents list. It now also abandons a pending fetch, so an owned-documents result for the previous identity cannot repopulate the list.
  • The typed document ID, price and recipient inputs are user input, not fetched state, and are kept.

Found by thepastaclaw while reviewing #1126. That code predates #1126, so the fix goes in its own PR against v1.0-dev.

Testing

  • RED → GREEN unit tests in ui::contracts_documents::document_action_screen::tests:
    • a_selection_change_discards_fetched_state: a fetched price, document, fields and owned-documents list are cleared, and Purchase can no longer be sent.
    • an_uncorrelated_fetch_result_is_ignored: a Documents result the screen did not request leaves the form untouched.
    • a_late_fetch_result_for_an_old_selection_is_ignored: fetch, change the selection, fetch again; the first fetch's result is dropped and the second one's price is applied.
    • only_the_current_fetch_error_reaches_the_banner: the abandoned fetch's error is suppressed, and the current fetch's error is shown and ends the wait.
    • a_selection_change_leaves_a_running_broadcast_alone: with a fetch pending and a broadcast running, a selection change keeps Broadcasting and the progress banner.
  • Call-site tests that drive the real widgets with egui_kittest:
    • picking_another_document_type_abandons_the_fetch: open the Doc Type combo, pick preorder instead of domain; the fetch is abandoned and the fetched document and fields are gone.
    • picking_another_identity_abandons_the_fetch: pick the other identity in the selector; the fetch is abandoned.
    • Both turn RED when the matching call (clear_selection_state() in the chooser, or abandon_pending_fetch() in the identity branch) is removed.
  • The existing document action screen tests and kittest contract_screen pass.
  • cargo clippy --all-features --lib --tests -- -D warnings clean; cargo fmt --all.

Prior work

  • refactor: build Platform types with their upstream constructors #1126 refuses a document type left over from another contract when building a new document; this PR covers the fetched Replace/Purchase/Delete state that review found next to it.
  • ViewTokenClaimsScreen already correlates document fetches with PendingRequest; this screen now follows the same pattern.

🤖 Generated with Claude Code

https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q

PR Hygiene · 62ea078

  • Bots — coderabbitai skipped after its own rate limit · thepastaclaw ✓
  • Self-review — not asked of a bot author
  • Build green
  • Approvals
    • files with no dedicated owner (src/ui/contracts_documents/document_action_screen.rs, src/ui/mod.rs) — approved by lklimek

When every merge requirement is met, the PR Hygiene check passes. Reviewer limits do not block merging; other required GitHub checks and protections still apply.

Summary by CodeRabbit

  • Bug Fixes
    • Document results and errors now affect the screen only when they belong to the fetch currently in progress; stale results are ignored and errors from abandoned requests are suppressed.
    • Changing the selection clears fetched and entered data and abandons pending fetches. Switching networks resets the screen for the new context.
    • Fetching is disabled during broadcasts, and broadcasts are blocked while a fetch is pending. A broadcast already in progress continues when the selection changes.

lklimek and others added 2 commits October 27, 2026 18:40
The document action screen kept the fetched document, its field values
and the fetched price after the user picked another contract or document
type, so Replace or Purchase could submit them with the new selection.
A fetch still running when the selection changed also filled in the form
when it finished, and its error still raised a banner.

Fetches now dispatch through a PendingRequest, and only the outstanding
fetch's Documents result or error reaches the form. A contract or
document-type change clears every fetched value and abandons the pending
fetch. An identity change also abandons the pending fetch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q
…pped

Abandoning a pending fetch dismissed the progress banner even when a
broadcast had started since then and owned it. The banner is now taken
only while the screen still shows the fetch.

Tests now drive the document-type chooser and the identity selector, so
dropping either call that abandons the fetch fails a test. They also
check that a running broadcast survives a selection change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012ujJpB9DBhvtbjhYB3aG9Q
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 46 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
Commits

Reviewing files that changed from the base of the PR and between 0eb1981 and 62ea078.

Files selected for processing: 1
  • src/ui/contracts_documents/document_action_screen.rs

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 028a95c and 0eb1981.

Files selected for processing: 2
  • src/ui/contracts_documents/document_action_screen.rs
  • src/ui/mod.rs

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

DocumentActionScreen now tracks pending document fetches and accepts results and errors only for the active request. Selection and network context changes clear related state. Fetch controls are disabled during broadcasts, and pending fetches block broadcasts.

Changes: Document action fetches

Layer / File(s) Summary
Fetch state and selection lifecycle
src/ui/contracts_documents/document_action_screen.rs, src/ui/mod.rs
The screen tracks a pending fetch. Contract, document-type, and identity changes abandon pending fetches and clear selection-dependent data. Network switches reset the screen with the new context.
Tracked fetch dispatch and broadcast gating
src/ui/contracts_documents/document_action_screen.rs
Delete, Purchase, and Replace fetches use shared tracked dispatch. Fetch controls are disabled during broadcasts, and pending fetches block broadcasts. Tests cover the button and broadcast behavior.
Fetch completion and validation
src/ui/contracts_documents/document_action_screen.rs
The screen ignores uncorrelated or stale results and suppresses errors from abandoned fetches. Tests cover current and stale results and errors.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Possibly related PRs

Suggested reviewers: pauldelucia, lklimek

Merge Risk: Minimal · previous review 0eb19

No actionable merge-blocking risk remains from the reviewed document-action changes.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: preventing fetched state from a previous contract or document type from persisting. It is concise and directly related to the changeset.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 18 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@thepastaclaw

thepastaclaw commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Final review complete — no blockers (commit 62ea078) · triage: normal · Phase 2 only (no Phase 1 for this repository)

@Claudius-Maginificent
Claudius-Maginificent marked this pull request as ready for review October 28, 2026 10:25
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai ✓ · thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Oct 28, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Final validation — Phase 2 only (no Phase 1 for this repository)

The PR correctly correlates document fetches and clears stale selection state, and the CI checks covering the change passed. One blocking concurrency defect remains: a pending fetch can still overwrite an in-flight or completed Delete broadcast, clearing its banner/status and potentially enabling a duplicate action.

🔴 1 blocking

Review provenance

Source: reviewer 1: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.1-sol (effort low) — This is a contained but substantial UI state-management change involving asynchronous request correlation, stale-result suppression, selection invalidation, and regression tests, but it does not modify a critical surface such as funds movement, cryptography, consensus, or storage migrations.
  • Phase 1 reviewers: not run (disabled for this repository)
  • Fresh verifier: gpt-6.1-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.1-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.1-sol — rust-quality (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `src/ui/contracts_documents/document_action_screen.rs`:
- [BLOCKING] src/ui/contracts_documents/document_action_screen.rs:1797-1802: Do not let a pending fetch overwrite an in-flight or completed broadcast
  The Delete form can dispatch Fetch Owned Documents and still allow Delete because its `can_broadcast()` branch only checks whether `document_id_input` is non-empty; it does not reject `BroadcastStatus::Fetching` or account for an outstanding `pending_fetch`. If Delete starts while that fetch is pending, the fetch result settles here and `display_task_result()` clears the `Broadcasting...` banner and changes the status to `Fetched`. If the Delete result arrives first, a late fetch result changes `Broadcasted` back to `Fetched`. Either ordering loses the broadcast progress/success state and can expose the broadcast button again, allowing a duplicate deletion. Disable broadcast controls while a fetch is pending, clear or abandon the pending fetch when a broadcast starts, and ignore any fetch result once a broadcast owns the form.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Oct 28, 2026
lklimek and others added 4 commits October 28, 2026 18:15
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Delete, Set Price and Transfer could be sent during a fetch, whose result
then cleared the broadcast's progress or replaced its success.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A fetch started during a broadcast could clear its progress banner or
replace its success when the fetch result arrived.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A fetch or broadcast still awaited from the previous network kept the
form's fetch or send controls disabled, and its selections stayed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai ✓ · thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added waiting-bots Waiting for the review bots to report on this head and removed waiting-self-review Waiting for the author to post /self-reviewed labels Oct 28, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review — Final validation — Phase 2 only (no Phase 1 for this repository)

The request correlation and contract/document-type reset logic is sound, and the previously reported fetch-versus-broadcast overwrite race is fixed. Two blocking races remain: a broadcast can be submitted repeatedly while the first one is still running, and changing the document ID can leave stale fetched data or price eligible for a different document. Test Suite is still in progress and Clippy is queued.

🔴 2 blocking

Review provenance

Source: reviewer 1: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.1-sol (effort low) — This is a substantial but contained UI state-management change involving asynchronous request correlation, result/error gating, selection invalidation, and regression tests, without modifying a critical surface such as funds movement, cryptography, consensus, or storage.
  • Phase 1 reviewers: not run (disabled for this repository)
  • Fresh final gate: an independent Phase-2 review ran after iterative findings were reconciled
  • Fresh verifier: gpt-6.1-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.1-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.1-sol — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.1-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.1-sol — rust-quality (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `src/ui/contracts_documents/document_action_screen.rs`:
- [BLOCKING] src/ui/contracts_documents/document_action_screen.rs:1701-1705: Do not allow a second broadcast while one is already running
  `can_broadcast()` rejects an outstanding fetch but does not reject `BroadcastStatus::Broadcasting`. After the first click changes the status to `Broadcasting`, the button remains active and the Create, Delete, Set Price, and Transfer branches still satisfy their input checks. A second click can therefore dispatch another paid document transition before the first result arrives. Include the broadcasting state in this guard so every document action is disabled until the in-flight broadcast settles.
- [BLOCKING] src/ui/contracts_documents/document_action_screen.rs:248-251: Invalidate fetched state when the document ID changes or a new fetch begins
  `dispatch_fetch()` replaces the pending request and sets the status to `Fetching`, but it leaves `original_doc`, `fetched_price`, and other document-derived state untouched. The document ID input is editable without invalidating that state. For Purchase, changing from document A to document B can leave A's price available for purchasing B; after a failed fetch for B, the status becomes `NotBroadcasted` and the stale price still passes `can_broadcast()`. For Replace, `create_replace_task()` uses `original_doc` rather than the edited document ID, so the form can display B while submitting a replacement for the previously fetched A. Clear document-derived state whenever the ID changes and when a replacement fetch starts, or track the queried ID and require it to match the current input before broadcasting.

@github-actions github-actions Bot added waiting-self-review Waiting for the author to post /self-reviewed and removed waiting-bots Waiting for the review bots to report on this head labels Oct 28, 2026
…ent shown

A document action could be sent again while the first was still being
broadcast, paying for it twice. The send button is now disabled and the
action refused until the running broadcast settles.

Editing the document ID kept the document fetched for the old ID, so
Purchase could buy the new ID at the old price and Replace could rewrite
the old document while the form showed the new ID. Editing the ID or
starting a new fetch now drops the fetched document, its price and the
fields filled in from it, and stops waiting on a fetch for the old ID.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai not yet · thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added waiting-bots Waiting for the review bots to report on this head and removed waiting-self-review Waiting for the author to post /self-reviewed labels Oct 29, 2026
Another task's error or message, such as a background scheduled-vote
sweep failure, reset the document action screen to NotBroadcasted while
its paid broadcast was still running, re-enabling the send button.

The broadcast is now dispatched under its own context
(PendingRequest), and only that dispatch's result or error ends
Broadcasting and clears its progress banner. The screen no longer
changes state in display_message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Waiting for bot review — coderabbitai skipped after its own rate limit · thepastaclaw not yet. Wait for the missing reviews, or a writer can post /skip-bots to proceed without them; blocking findings still need addressing.
Full checklist in the description.

@github-actions github-actions Bot added the bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. label Oct 29, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review — Final validation — Phase 2 only (no Phase 1 for this repository)

The current head correctly correlates document fetches and broadcasts with their originating dispatches, invalidates fetched state when the contract, document type, or fetched document ID changes, and resets network-bound state. The code and supplied CI evidence show no in-scope defects; Clippy and the Test Suite passed, while only the non-code PR Hygiene check remains pending.

🔴 0 blocking | 🟡 0 suggestion(s) | 💬 0 nitpick(s)

Review provenance

Source: reviewer 1: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 2: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); reviewer 3: gpt-6.1-sol (agent: phase2-reviewer, role: general); reviewer 4: gpt-6.1-sol (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6.1-sol (agent: sol-verifier, role: final-verifier)

  • Triage: normal by gpt-6.1-sol (effort low) — This is a substantial but contained UI state-management change involving asynchronous request correlation, result and error gating, selection invalidation, and tests, but it does not modify a critical surface such as funds movement, cryptography, consensus, or storage.
  • Phase 1 reviewers: not run (disabled for this repository)
  • Fresh final gate: an independent Phase-2 review ran after iterative findings were reconciled
  • Fresh verifier: gpt-6.1-sol — final-verifier; agent sol-verifier
  • Phase 2 reviewers: gpt-6.1-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.1-sol — rust-quality (completed, effort high); agent phase2-reviewer, gpt-6.1-sol — general (completed, effort high); agent phase2-reviewer, gpt-6.1-sol — rust-quality (completed, effort high); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify the current code and confirm that no unresolved issues remain.

No unresolved findings remain from the prior review on this head.

@github-actions

Copy link
Copy Markdown
Contributor

Ready for review — files with no dedicated owner: lklimek.
Full checklist in the description.

@github-actions github-actions Bot added ready-for-human Bots have reported, the author has self-reviewed, and the build is green: this needs a human. and removed waiting-bots Waiting for the review bots to report on this head labels Oct 29, 2026
@github-actions
github-actions Bot requested a review from lklimek October 29, 2026 16:05
@lklimek
lklimek enabled auto-merge (squash) October 29, 2026 16:34
@github-actions

Copy link
Copy Markdown
Contributor

Policy satisfied — this can merge.
Full checklist in the description.

@github-actions github-actions Bot removed the ready-for-human Bots have reported, the author has self-reviewed, and the build is green: this needs a human. label Oct 29, 2026
@lklimek
lklimek merged commit da53222 into v1.0-dev Oct 29, 2026
11 checks passed
@lklimek
lklimek deleted the fix/document-action-stale-state branch October 29, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot-review-skipped A required review bot did not report; it was skipped by the window or by a person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants