Skip to content

fix(rivetkit): bind methods through createWriteThroughProxy#4987

Draft
abcxff wants to merge 1 commit into05-06-fix_log_error_on_failed_inspector_requestsfrom
05-06-fix_rivetkit_bind_methods_through_createwritethroughproxy
Draft

fix(rivetkit): bind methods through createWriteThroughProxy#4987
abcxff wants to merge 1 commit into05-06-fix_log_error_on_failed_inspector_requestsfrom
05-06-fix_rivetkit_bind_methods_through_createwritethroughproxy

Conversation

@abcxff
Copy link
Copy Markdown
Contributor

@abcxff abcxff commented May 6, 2026

Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@railway-app
Copy link
Copy Markdown

railway-app Bot commented May 6, 2026

🚅 Deployed to the rivet-pr-4987 environment in rivet-frontend

Service Status Web Updated (UTC)
website 😴 Sleeping (View Logs) Web May 7, 2026 at 2:22 am
frontend-inspector ❌ Build Failed (View Logs) Web May 6, 2026 at 10:05 pm
frontend-cloud ❌ Build Failed (View Logs) Web May 6, 2026 at 10:04 pm
kitchen-sink ❌ Build Failed (View Logs) Web May 6, 2026 at 10:03 pm
ladle ❌ Build Failed (View Logs) Web May 6, 2026 at 8:30 pm
mcp-hub ✅ Success (View Logs) Web May 6, 2026 at 8:30 pm

Copy link
Copy Markdown
Contributor Author

abcxff commented May 6, 2026

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@claude
Copy link
Copy Markdown
Contributor

claude Bot commented May 6, 2026

Review: fix(rivetkit): bind methods through createWriteThroughProxy

Overview

This 3-line change adds method binding inside createWriteThroughProxy's get trap so that functions retrieved through the proxy are bound to innerTarget (the raw object) rather than the proxy receiver. This is needed to make built-in methods (e.g., Map.prototype.set, Array.prototype.push) and class methods that access private fields (#field) work correctly when those objects are stored as actor/connection state.

Critical Issue: Mutation Tracking Breaks for Method-Based Mutations

The fix solves method callability, but at a significant cost: any state mutation that happens inside a bound method via this.property = value bypasses the proxy's set trap and will not trigger commit (the persistence callback).

Example with a private-field class:

class Inventory {
  #items: string[] = [];
  add(item: string) { this.#items.push(item); } // 'this' is innerTarget, not the proxy
}
this.state = new Inventory();
this.state.add('sword'); // mutation happens but commit() is NEVER called — NOT persisted

Example with a plain-object method:

const state = { count: 0, increment() { this.count++; } };
this.state = state;
this.state.increment(); // count incremented in-memory, but NOT persisted

This is the core purpose of the proxy. Silently swallowing mutations is worse than throwing an error: actors appear to work (mutations are visible in-memory) but lose data on sleep/restart.

Suggested approach: If the fix is specifically targeting private-field classes or built-ins like Map/Set, document this as an unsupported pattern for write-through tracking and throw an actionable error (per the fail-by-default rule in CLAUDE.md). Alternatively, restrict the proxy to plain objects (Object.getPrototypeOf(target) === Object.prototype) and reject class instances with an explicit error.

Performance: New Bound Function on Every Get

Every property access that returns a function now allocates a new bound function via .bind(innerTarget). The proxies WeakMap already caches proxy wrappers for nested objects. The same pattern should apply to bound functions to avoid repeated allocations per access, which matters more for connection state (ConnStateManager) that is accessed per request.

Missing Tests

There are no tests covering the scenario this fix addresses. A unit test (no real engine needed) demonstrating the fixed case would validate correctness and prevent regressions. The driver test suite (tests/driver/actor-save-state.test.ts) has good patterns to follow for persistence-level coverage.

Minor: Draft Checklist

The PR is in DRAFT with all checklist items unchecked. Please self-review against the style guidelines and address test coverage before marking ready.


Summary: The intent is correct. Method access on proxied state objects currently breaks for private-field classes and built-ins. However, binding to innerTarget silently breaks write-through tracking for method-based mutations, which is the proxy's core purpose. Silent data loss on sleep/restart is the failure mode. This needs either a redesign or explicit documentation plus an actionable error for unsupported patterns before merging.

@abcxff abcxff force-pushed the 05-06-fix_log_error_on_failed_inspector_requests branch from b150344 to fc4756e Compare May 6, 2026 22:03
@abcxff abcxff force-pushed the 05-06-fix_rivetkit_bind_methods_through_createwritethroughproxy branch from af6e0e4 to 3be897d Compare May 6, 2026 22:03
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