Skip to content

refactor(rivetkit-core): merge ready+started into lifecycle_started, drop redundant napi calls#4784

Merged
NathanFlurry merged 2 commits intomainfrom
cleanup/lifecycle-flag-merge
Apr 27, 2026
Merged

refactor(rivetkit-core): merge ready+started into lifecycle_started, drop redundant napi calls#4784
NathanFlurry merged 2 commits intomainfrom
cleanup/lifecycle-flag-merge

Conversation

@NathanFlurry
Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Member Author

NathanFlurry commented Apr 26, 2026

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

@claude
Copy link
Copy Markdown

claude Bot commented Apr 26, 2026

Code Review: refactor(rivetkit-core): merge ready+started into lifecycle_started

Overview

This PR simplifies the actor lifecycle readiness model by collapsing two AtomicBool flags (ready + started) into a single lifecycle_started flag. It removes the NAPI-layer lifecycle stepping methods (markReady, markStarted, isReady, isStarted) and moves init_alarms / drain_overdue_scheduled_events out of the NAPI preamble (both calls remain in task.rs lines 1191 and 1196). Net result: -176 lines.


What is Correct

  • Merging the two flags is safe. In task.rs, both ready and started were always flipped together: set on LifecycleState::Started and cleared on every other state. There was never a window where one was true and the other false in production. The NAPI ordering guard (markStarted requires markReady) was protecting a sequencing core already enforced.
  • Removal of init_alarms / drain_overdue_scheduled_events from NAPI is correct. Both remain in task.rs (lines 1191 and 1196), so no regression.
  • registry/mod.rs swap from ctx.ready() to ctx.started() is semantically equivalent since the two flags were coextensive.
  • CLAUDE.md addition documenting the pre-Rust refactor reference commit is a useful anchor.

Broken Test: can_sleep_requires_ready_and_started

This test will fail. After the refactor, can_sleep() calls can_arm_sleep_timer(), which now has only one lifecycle guard:

if !self.0.sleep.lifecycle_started.load(Ordering::SeqCst) {
    return CanSleep::NotReady;
}

The post-diff test reads:

assert_eq!(ctx.can_sleep().await, CanSleep::NotReady); // lifecycle_started=false, ok

ctx.set_started(true);  // sets lifecycle_started=true
assert_eq!(ctx.can_sleep().await, CanSleep::NotReady); // FAILS: returns CanSleep::Yes now

ctx.set_started(true);  // no-op - already true
assert_eq!(ctx.can_sleep().await, CanSleep::Yes);

The default ActorConfig has no_sleep = false and a fresh context has no active connections or handlers, so after the first set_started(true) the function returns CanSleep::Yes immediately. The intermediate NotReady assertion panics.

Fix: collapse to a single-flag test, rename, and remove the dead intermediate state:

async fn can_sleep_requires_lifecycle_started() {
    let ctx = ActorContext::default();
    assert_eq!(ctx.can_sleep().await, CanSleep::NotReady);
    ctx.set_started(true);
    assert_eq!(ctx.can_sleep().await, CanSleep::Yes);
}

Minor Issues

1. Redundant double set_started(true) calls in tests

Several tests were migrated from ctx.set_ready(true); ctx.set_started(true); to two consecutive ctx.set_started(true); calls. The second call is a no-op and should be removed.

Affected: tests/modules/sleep.rs lines 26-27, 47-48, 61-62, 87-88; tests/modules/context.rs lines 106-107, 141-142, 179-180; tests/modules/websocket.rs lines 63-64.

2. Stale docs in rivetkit-core/CLAUDE.md

The sleep-state invariants bullet reads: Inputs are: ready/started, no_sleep, ... Should be updated to lifecycle_started.


Summary

The core logic change is sound. Before merging: fix the broken can_sleep_requires_ready_and_started test and remove the redundant double set_started calls throughout the test modules.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 26, 2026

Preview packages published to npm

Install with:

npm install rivetkit@pr-4784

All packages published as 0.0.0-pr.4784.f19a071 with tag pr-4784.

Engine binary is shipped via @rivetkit/engine-cli on linux-x64-musl, linux-arm64-musl, darwin-x64, and darwin-arm64. Windows users should use the release installer or set RIVET_ENGINE_BINARY.

Docker images:

docker pull rivetdev/engine:slim-f19a071
docker pull rivetdev/engine:full-f19a071
Individual packages
npm install rivetkit@pr-4784
npm install @rivetkit/react@pr-4784
npm install @rivetkit/rivetkit-napi@pr-4784
npm install @rivetkit/workflow-engine@pr-4784

@NathanFlurry NathanFlurry force-pushed the cleanup/lifecycle-flag-merge branch 2 times, most recently from 8de539b to c7773a5 Compare April 27, 2026 01:17
@NathanFlurry NathanFlurry changed the base branch from driver-fixes/sleep-db-poll-interval to graphite-base/4784 April 27, 2026 01:47
@NathanFlurry NathanFlurry force-pushed the cleanup/lifecycle-flag-merge branch from c7773a5 to 8f60327 Compare April 27, 2026 01:47
@NathanFlurry NathanFlurry changed the base branch from graphite-base/4784 to driver-fixes/native-sleep-side-task-drain April 27, 2026 01:47
@NathanFlurry NathanFlurry marked this pull request as ready for review April 27, 2026 01:50
@NathanFlurry NathanFlurry changed the base branch from driver-fixes/native-sleep-side-task-drain to graphite-base/4784 April 27, 2026 02:19
@NathanFlurry NathanFlurry force-pushed the cleanup/lifecycle-flag-merge branch from 8f60327 to 03ffafc Compare April 27, 2026 03:24
Base automatically changed from driver-fixes/native-sleep-side-task-drain to main April 27, 2026 07:13
@NathanFlurry NathanFlurry merged commit 03ffafc into main Apr 27, 2026
12 of 52 checks passed
@NathanFlurry NathanFlurry deleted the cleanup/lifecycle-flag-merge branch April 27, 2026 07:13
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