← Back to Pull requests

Hold jsg::Weak's kj::WeakRc in a generic kj-rs KjWeakRc #8759

Open
unknown opened this pull request with 280 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 20, 2026 22:35 UTC
Author

This is a follow-up to #8689, addressing this review comment: jsg::Weak's kj::WeakRc<Wrappable> now uses a generic KjWeakRc<T> in kj-rs instead of a jsg-specific mirror and shims.

Add KjWeakRc to kj-rs

  • kj_rs::KjWeakRc<T> mirrors kj::WeakRc<T> the way KjRc mirrors kj::Rc. You get one from KjRc::downgrade(). It has try_get(), is_alive(), upgrade() and Clone, and dropping it releases its reference to the weak cell.

  • Like the KjRc helpers, the C++ helpers treat the referent as kj::byte. That works because kj::WeakRc<T> holds the owner's weak cell and the referent pointer, and reaches the owner only through the cell. A static_assert checks its size and alignment.

  • KjWeakRc isn't a bridge type, so the cxx code generator is unchanged.

  • upgrade() is unsafe. KjRc::get_mut() counts only strong references, as kj::Refcounted::isShared() does, and KJ keeps the weak count private, so get_mut() can't refuse a referent that has weak references. The precondition is that no reference returned by get_mut() is live. Once upgrade() returns, the referent is shared and get_mut() refuses it.

  • KjWeakRc<T> is invariant in T, like KjRc<T>, because the KjRc<T> that upgrade() returns can write a T.
  • Hold a KjWeakRc in jsg::Weak

  • Removes the WrappableWeakRc layout mirror (Rust and C++), the five wrappable_weak_* shims and Wrappable::toWeakRc().

  • Only the condemned check stays in jsg. is_alive() calls the existing wrappable_is_condemned() on the referent try_get() returns, and upgrade() still calls try_condemn(). Nothing calls KjRc::get_mut() on a Wrappable, which satisfies upgrade()'s precondition.

  • is_alive() is now two FFI calls instead of one. I haven't benchmarked this.
  • Testing

  • New kj-rs test test_weak_rc covers downgrade, try_get, upgrade, clone, and expiry after the last Rc is dropped, including cloning an expired reference.

  • //src/rust/..., //src/workerd/jsg/... and //src/workerd/api/tests:all pass, with and without --config=rust-debug. Clippy is clean on //src/rust/....

  • The kj-rs commit passes //src/rust/cxx/... on its own.
  • Diff: 280 lines (+210 / -70) across 3 files Reviewer: nathanwhit Review duration: 11231m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.40) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 280 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.337
    Real PR Conversation Timeline
    1 comments
    "Review active on #8759"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.6% (MEDIUM) · Higher = larger blast radius
    39.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 21.1% · Safe to merge
    21.1%