Skip to content

Cut hashrate-weighted staleness in PRISM tip-refresh fanout - #98

Merged
RobertClarke merged 4 commits into
1.x.xfrom
prism-tip-refresh-opt
Jul 26, 2026
Merged

RobertClarke merged 4 commits into
1.x.xfrom
prism-tip-refresh-opt

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Jul 26, 2026 •

Copy link
Copy Markdown
Member

Summary

Three independent PRISM coordinator changes, one commit each, aimed at cutting hashrate-weighted staleness after chain-tip changes across the ~350-client mainnet fleet, plus visibility into the one unmeasured serial stage of the refresh path.

  • Order tip-refresh fanout admission by descending per-client hashrate proxy instead of job-less-first.
  • Stop flushing in-flight work on vardiff step-up retargets; step-downs keep flushing.
  • Time ensure_reorg_reconciled_for_tip on the poll path and report it as phase="reorg_reconcile" in the existing bundle-phase histogram.

1. Hashrate-ordered fanout admission

The tip-refresh wave admitted clients without an active job first and the rest in arbitrary set order. A job-less client burns zero hashrate while it waits for work, but a stale-job client burns its full rate on old-tip work, so newcomer-first admission anti-optimized hashrate-weighted staleness over the multi-second wave.

Admission now sorts stale-job clients by a descending per-client hashrate proxy (vardiff_difficulty_estimate, falling back to share_difficulty before an estimate exists) and admits job-less clients after them: a job-less client burns no hashrate while it waits, regardless of its configured difficulty. Vardiff drives every client toward the same share interval, so sustained difficulty is proportional to hashrate. Proxies are snapshotted under the coordinator lock in the same pass that snapshots expected active jobs, avoiding torn reads. Job-less clients keep their initial-delivery executor queue priority, and the separate initial-job executor path is untouched.

2. No-flush vardiff step-up retargets

Both retarget send sites passed clean_jobs=True, flushing the miner's in-flight work on every same-tip difficulty change. Per-job stamped share targets already validate old-job shares at the difficulty they were mined against, and non-clean sends keep old jobs registered — eviction happens only under the clean_jobs branches.

clean_jobs is now next_difficulty < current_difficulty: step-ups keep the old job submittable at its own stamped difficulty; step-downs still flush to protect firmware that applies mining.set_difficulty retroactively, which would otherwise submit sub-target shares against the old job. Idle retargets fire only on zero-share windows and therefore always step down, so their flush behavior is unchanged.

3. reorg_reconcile phase metric

ensure_reorg_reconciled_for_tip on the poll path was the only serial pre-build stage with no phase metric. It is now timed and reported as phase="reorg_reconcile" in qbit_prism_tip_refresh_bundle_phase_seconds. The observation lands in a finally block so blocked or failed reconciles show up too, not just successful ones.

Tests

  • Rewrote the fanout ordering test: a wave with a job-less newcomer, a slow stale client (share_difficulty fallback), and a fast stale client (vardiff estimate) must deliver fastest-first with the newcomer last, while the newcomer retains PRISM_DELIVERY_PRIORITY_INITIAL queue priority.
  • Step-up retarget test now asserts a non-clean paired send; a new step-down test asserts the clean flush is retained.
  • New end-to-end test drives a real 1→4 step-up through maybe_send_job: the old job stays in server.jobs and active_job_ids, and a share submitted against it is accepted at the old job's stamped difficulty with zero low-difficulty, unknown-job, or stale-job rejections.
  • New metric test: one poll records exactly one reorg_reconcile phase observation.

Validation

  • python3 -m unittest discover -s tests -p 'test_*.py' — 1,262 ran, OK, 3 skipped
  • python3 -m unittest tests.test_prism_coordinator_vardiff — 305 passed
  • python3 -m unittest tests.test_prism_coordinator_job_cache — 112 passed
  • python3 -m unittest tests.test_prism_first_job_latency — 20 passed
  • git diff --check — passed

Security and operational impact

No schema, dependency, configuration, authentication, or external API changes. Fanout ordering changes only intra-wave admission order; total work delivered is unchanged. Step-up retargets now leave the previous job submittable until the existing per-client retention cap or the next clean send prunes it; share validation against retained jobs is unchanged and already difficulty-stamped per job. The new histogram label adds a fixed set of series to an existing metric.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.


Note

Medium Risk
Changes tip-refresh delivery order and vardiff retarget flushing behavior on live stratum paths; share validation logic is unchanged but miners may retain more concurrent jobs until caps or clean sends.

Overview
Reduces hashrate-weighted staleness after tip changes on large fleets by changing how tip-refresh fanout admits clients, how vardiff retargets flush work, and what the refresh path measures.

Tip-refresh fanout no longer admits job-less clients before everyone else. Clients are sorted by stale-job presence and descending hashrate proxy (vardiff_difficulty_estimate, else share_difficulty), snapshotted under the coordinator lock with job snapshots. Job-less miners still get initial-delivery queue priority when admitted, but fast stale clients are served first in the wave.

Vardiff same-tip retargets set clean_jobs only on step-down (next_difficulty < current_difficulty). Step-ups send a non-clean job so in-flight work on the old stamped target stays valid; step-downs still flush for firmware that applies difficulty retroactively.

Poll path times ensure_reorg_reconciled_for_tip and records phase="reorg_reconcile" in qbit_prism_tip_refresh_bundle_phase_seconds (including failures via finally).

Reviewed by Cursor Bugbot for commit e452ca0. Bugbot is set up for automated code reviews on this repo. Configure here.

RobertClarke and others added 3 commits July 26, 2026 10:25
The tip-refresh wave admitted job-less clients first and the rest in
arbitrary set order. A job-less client burns zero hashrate while it
waits, but a stale client burns its full rate on old-tip work, so
serving newcomers first anti-optimized hashrate-weighted staleness
across the multi-second wave.

Admit clients in descending per-client hashrate order instead, using
the vardiff difficulty estimate (share_difficulty fallback) as the
proxy: vardiff targets a uniform share interval, so sustained
difficulty is proportional to hashrate. Proxies are snapshotted under
the coordinator lock in the same pass as the expected-active-jobs
snapshot to avoid torn reads. Job-less clients keep their
initial-delivery queue priority; the separate initial-job executor is
untouched.

Co-Authored-By: Claude Fable 5 <[email protected]>
Both retarget send sites passed clean_jobs=True, so every same-tip
difficulty change discarded the miner's in-flight work even though
per-job stamped share targets already validate old-job shares at the
difficulty they were mined against.

Flush only on a step-down (clean_jobs = next < current): firmware that
applies mining.set_difficulty retroactively would otherwise submit
sub-target shares against the old job. Step-ups now send a non-clean
job, keeping the old job registered and submittable at its own stamped
difficulty. Idle retargets only fire on zero-share windows and thus
always step down, so their flush behavior is unchanged.

Co-Authored-By: Claude Fable 5 <[email protected]>
ensure_reorg_reconciled_for_tip on the poll path was the only serial
pre-build stage with no phase metric, leaving ops blind to whether it
holds material wall time in the refresh critical path.

Time the call and report it as phase="reorg_reconcile" in the existing
qbit_prism_tip_refresh_bundle_phase_seconds histogram. The observation
lands in a finally block so blocked or failed reconciles are visible
too, not just successful ones.

Co-Authored-By: Claude Fable 5 <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 8c14f8f. Configure here.

Comment thread lab/prism/prism_coordinator.py Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8c14f8f8af

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lab/prism/prism_coordinator.py Resolved
A job-less client burns zero hashrate while it waits regardless of its
configured difficulty, so a high static-difficulty newcomer must not
outrank stale active miners in fanout admission. Rank stale-job clients
by descending hashrate proxy and admit job-less clients after them;
their initial-delivery queue priority is unchanged.

Reported by Cursor Bugbot on PR #98.

Co-Authored-By: Claude Fable 5 <[email protected]>
@RobertClarke

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: e452ca0db5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@RobertClarke
RobertClarke merged commit 0c5fad8 into 1.x.x Jul 26, 2026
9 checks passed
RobertClarke pushed a commit that referenced this pull request Jul 29, 2026
This branch already carries the #99 content (based on prism-cache-persist
at b289550, the exact head that squash-merged) plus the earlier #98
merge, so the 'ours' tree is the correct union by construction; the
merge only records ancestry and collapses the PR diff to this branch's
own changes.

Co-Authored-By: Claude Fable 5 <[email protected]>
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