Skip to content

fix: deduplicate ChatRole type across lm and trl packages - #17

Merged
Brooooooklyn merged 1 commit into
mainfrom
fix/deduplicate-chat-role-type
Feb 14, 2026
Merged

Brooooooklyn merged 1 commit into
mainfrom
fix/deduplicate-chat-role-type

Conversation

@Brooooooklyn

@Brooooooklyn Brooooooklyn commented Feb 14, 2026 •

Copy link
Copy Markdown
Contributor

Import ChatRole from @mlx-node/lm instead of redefining it in @mlx-node/trl, establishing a single source of truth.


Note

Low Risk
Type-only change that centralizes a shared enum-like union; risk is limited to potential compile-time incompatibilities if downstream code relied on the old local definition.

Overview
packages/trl no longer defines its own ChatRole union type; it now imports and re-exports ChatRole from @mlx-node/lm so consumers use a single shared type.

All ChatMessage/completion typing in trl continues to reference ChatRole, but the source of the type is centralized to lm to avoid drift between packages.

Written by Cursor Bugbot for commit bf50bf6. This will update automatically on new commits. Configure here.

Summary by CodeRabbit

  • Refactor
    • Restructured internal type declarations for improved code organization and maintainability. No changes to public APIs or user-facing functionality.

Import ChatRole from @mlx-node/lm instead of redefining it in
@mlx-node/trl, establishing a single source of truth.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@coderabbitai

coderabbitai Bot commented Feb 14, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The ChatRole type definition was moved from a local union type to an external import from '@mlx-node/lm', centralizing type definitions across the codebase while maintaining the same public interface through re-export.

Changes

Cohort / File(s) Summary
Type Centralization
packages/trl/src/types.ts
Replaced locally defined ChatRole union type with import and re-export from '@mlx-node/lm' package. ChatMessage.role now derives from the external ChatRole type definition.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 Hopping through the types we go,
Centralizing what we know,
ChatRole finds a shared home,
No more scattered, it can roam!
Clean imports, exports bright, ✨

🚥 Pre-merge checks | ✅ 3 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Merge Conflict Detection ⚠️ Warning ❌ Merge conflicts detected (5 files):

⚔️ crates/mlx-core/src/array/mod.rs (content)
⚔️ crates/mlx-sys/src/lib.rs (content)
⚔️ crates/mlx-sys/src/mlx.cpp (content)
⚔️ packages/core/index.d.cts (content)
⚔️ packages/trl/src/types.ts (content)

These conflicts must be resolved before merging into main.
Resolve conflicts locally and push changes to this branch.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: deduplicating the ChatRole type by importing it from @mlx-node/lm instead of redefining it locally in @mlx-node/trl.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/deduplicate-chat-role-type
⚔️ Resolve merge conflicts (beta)
  • Auto-commit resolved conflicts to branch fix/deduplicate-chat-role-type
  • Create stacked PR with resolved conflicts
  • Post resolved changes as copyable diffs in a comment

No actionable comments were generated in the recent review. 🎉


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 and usage tips.

@Brooooooklyn
Brooooooklyn merged commit 0f1bb16 into main Feb 14, 2026
6 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/deduplicate-chat-role-type branch February 14, 2026 04:43
Brooooooklyn added a commit that referenced this pull request Apr 11, 2026
Import ChatRole from @mlx-node/lm instead of redefining it in
@mlx-node/trl, establishing a single source of truth.

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Type-only change that centralizes a shared enum-like union; risk is
limited to potential compile-time incompatibilities if downstream code
relied on the old local definition.
> 
> **Overview**
> `packages/trl` no longer defines its own `ChatRole` union type; it now
imports and re-exports `ChatRole` from `@mlx-node/lm` so consumers use a
single shared type.
> 
> All `ChatMessage`/completion typing in `trl` continues to reference
`ChatRole`, but the source of the type is centralized to `lm` to avoid
drift between packages.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
bf50bf6. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Restructured internal type declarations for improved code organization
and maintainability. No changes to public APIs or user-facing
functionality.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Co-authored-by: Claude Opus 4.6 <[email protected]>
Brooooooklyn added a commit that referenced this pull request Jul 28, 2026
…weep, no-follow reap, no-follow deferred walk

Fix-delta review of the review-#16 fixes surfaced two more symlink-safety
defects adjacent to those fixes. Both verified via control-flow analysis.

- Finding 1 (download.ts, data-loss): processJob ran sweepStaging BEFORE the
  no-follow ownership preflight, and the sweep's dead-owner backup reap decided
  via isModelInstalled(finalDir), which FOLLOWS symlinks. A live finalDir
  symlink into an external complete marked model therefore made the sweep
  permanently delete a recoverable rollback backup before the preflight could
  reject the symlink. Fixed two ways: (a) hoisted the no-follow ownership
  preflight to the top of the try body, before assertRealDirOrAbsent / mkdir /
  sweepStaging, so a foreign finalDir is refused before any recovery work; and
  (b) hardened the reap site to classify finalDir no-follow — reap only a
  proven owned-complete real directory, restore only when finalDir is absent,
  otherwise (foreign symlink / unowned / incomplete-owned) retain the backup
  untouched (this also closes the job.overwrite=true path that bypasses the
  preflight).

- Finding 2 (models.ts walkDirStats): the iterative walker lstat'd a popped
  path for the dev:ino guard but never rejected a non-directory before
  opendirSync, which follows symlinks — so a queued real directory swapped to a
  symlink between enqueue and pop was followed into its external target. Added
  `if (!self.isDirectory()) continue;` after the lstat. No regression:
  discoverLocalModels already filters symlink roots, so every walk root is a
  real directory.

Gate: typecheck 0; dashboard 200/200 (×2); lint 0. Dashboard-only — native and
api.ts unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
Brooooooklyn added a commit that referenced this pull request Jul 28, 2026
…eflight after restore, document display-only sizing

Fix-delta review of the review-#17 fixes surfaced two recovery-ordering
defects (one a regression from the #17 preflight hoist) plus one display-only
residual. All verified via control-flow analysis.

- Finding 3 (download.ts, regression): the #17 hoist put the ownership
  preflight above sweepStaging, so a preflight-refused job (unowned/foreign
  finalDir) threw before the only dead-private-staging-tree reap ran, leaking
  crashed multi-GB staging indefinitely. Split sweepStaging into
  reapDeadStagingTrees() (process-wide, pid-gated dead-`<slug>@<sha>.<pid>`
  reap, run BEFORE the preflight so a refused job still frees its tree) and
  recoverBackup(slug, finalDir) (slug-scoped backup restore/reap, run AFTER the
  preflight so a foreign finalDir link is already refused and never followed).

- Finding 1 (download.ts): the backup restore branch renamed any backup back
  onto an absent finalDir; a foreign-symlink backup (from a prior overwrite
  crash) would be restored after the only ownership check and then followed by
  skip-detection. Restore now requires the backup itself be an owned real
  directory (no-follow lstat + our marker), and the no-follow ownership
  preflight is re-run after recovery, before skip-detection.

- Finding 2 (models.ts + docs, document-and-accept): walkDirStats traverses
  lexical paths, so a concurrent ancestor/TOCTOU symlink swap during the walk
  can redirect sizing. Its output is display-only (the Models table + the
  delete-confirmation "(X GB)" hint) and never drives a destructive decision
  (deletion uses the path-safe name, not the size), so the worst case is a
  wrong size number under adversarial mutation of the user's own store. A
  robust fix needs descriptor-relative traversal node:fs does not expose;
  documented as an accepted residual in code and docs/dashboard.md.

Gate: typecheck 0; dashboard 202/202 (×2); lint 0. Dashboard-only — native and
api.ts unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
Brooooooklyn added a commit that referenced this pull request Jul 28, 2026
…weep, no-follow reap, no-follow deferred walk

Fix-delta review of the review-#16 fixes surfaced two more symlink-safety
defects adjacent to those fixes. Both verified via control-flow analysis.

- Finding 1 (download.ts, data-loss): processJob ran sweepStaging BEFORE the
  no-follow ownership preflight, and the sweep's dead-owner backup reap decided
  via isModelInstalled(finalDir), which FOLLOWS symlinks. A live finalDir
  symlink into an external complete marked model therefore made the sweep
  permanently delete a recoverable rollback backup before the preflight could
  reject the symlink. Fixed two ways: (a) hoisted the no-follow ownership
  preflight to the top of the try body, before assertRealDirOrAbsent / mkdir /
  sweepStaging, so a foreign finalDir is refused before any recovery work; and
  (b) hardened the reap site to classify finalDir no-follow — reap only a
  proven owned-complete real directory, restore only when finalDir is absent,
  otherwise (foreign symlink / unowned / incomplete-owned) retain the backup
  untouched (this also closes the job.overwrite=true path that bypasses the
  preflight).

- Finding 2 (models.ts walkDirStats): the iterative walker lstat'd a popped
  path for the dev:ino guard but never rejected a non-directory before
  opendirSync, which follows symlinks — so a queued real directory swapped to a
  symlink between enqueue and pop was followed into its external target. Added
  `if (!self.isDirectory()) continue;` after the lstat. No regression:
  discoverLocalModels already filters symlink roots, so every walk root is a
  real directory.

Gate: typecheck 0; dashboard 200/200 (×2); lint 0. Dashboard-only — native and
api.ts unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
Brooooooklyn added a commit that referenced this pull request Jul 28, 2026
…eflight after restore, document display-only sizing

Fix-delta review of the review-#17 fixes surfaced two recovery-ordering
defects (one a regression from the #17 preflight hoist) plus one display-only
residual. All verified via control-flow analysis.

- Finding 3 (download.ts, regression): the #17 hoist put the ownership
  preflight above sweepStaging, so a preflight-refused job (unowned/foreign
  finalDir) threw before the only dead-private-staging-tree reap ran, leaking
  crashed multi-GB staging indefinitely. Split sweepStaging into
  reapDeadStagingTrees() (process-wide, pid-gated dead-`<slug>@<sha>.<pid>`
  reap, run BEFORE the preflight so a refused job still frees its tree) and
  recoverBackup(slug, finalDir) (slug-scoped backup restore/reap, run AFTER the
  preflight so a foreign finalDir link is already refused and never followed).

- Finding 1 (download.ts): the backup restore branch renamed any backup back
  onto an absent finalDir; a foreign-symlink backup (from a prior overwrite
  crash) would be restored after the only ownership check and then followed by
  skip-detection. Restore now requires the backup itself be an owned real
  directory (no-follow lstat + our marker), and the no-follow ownership
  preflight is re-run after recovery, before skip-detection.

- Finding 2 (models.ts + docs, document-and-accept): walkDirStats traverses
  lexical paths, so a concurrent ancestor/TOCTOU symlink swap during the walk
  can redirect sizing. Its output is display-only (the Models table + the
  delete-confirmation "(X GB)" hint) and never drives a destructive decision
  (deletion uses the path-safe name, not the size), so the worst case is a
  wrong size number under adversarial mutation of the user's own store. A
  robust fix needs descriptor-relative traversal node:fs does not expose;
  documented as an accepted residual in code and docs/dashboard.md.

Gate: typecheck 0; dashboard 202/202 (×2); lint 0. Dashboard-only — native and
api.ts unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
Brooooooklyn added a commit that referenced this pull request Jul 28, 2026
…weep, no-follow reap, no-follow deferred walk

Fix-delta review of the review-#16 fixes surfaced two more symlink-safety
defects adjacent to those fixes. Both verified via control-flow analysis.

- Finding 1 (download.ts, data-loss): processJob ran sweepStaging BEFORE the
  no-follow ownership preflight, and the sweep's dead-owner backup reap decided
  via isModelInstalled(finalDir), which FOLLOWS symlinks. A live finalDir
  symlink into an external complete marked model therefore made the sweep
  permanently delete a recoverable rollback backup before the preflight could
  reject the symlink. Fixed two ways: (a) hoisted the no-follow ownership
  preflight to the top of the try body, before assertRealDirOrAbsent / mkdir /
  sweepStaging, so a foreign finalDir is refused before any recovery work; and
  (b) hardened the reap site to classify finalDir no-follow — reap only a
  proven owned-complete real directory, restore only when finalDir is absent,
  otherwise (foreign symlink / unowned / incomplete-owned) retain the backup
  untouched (this also closes the job.overwrite=true path that bypasses the
  preflight).

- Finding 2 (models.ts walkDirStats): the iterative walker lstat'd a popped
  path for the dev:ino guard but never rejected a non-directory before
  opendirSync, which follows symlinks — so a queued real directory swapped to a
  symlink between enqueue and pop was followed into its external target. Added
  `if (!self.isDirectory()) continue;` after the lstat. No regression:
  discoverLocalModels already filters symlink roots, so every walk root is a
  real directory.

Gate: typecheck 0; dashboard 200/200 (×2); lint 0. Dashboard-only — native and
api.ts unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
Brooooooklyn added a commit that referenced this pull request Jul 28, 2026
…eflight after restore, document display-only sizing

Fix-delta review of the review-#17 fixes surfaced two recovery-ordering
defects (one a regression from the #17 preflight hoist) plus one display-only
residual. All verified via control-flow analysis.

- Finding 3 (download.ts, regression): the #17 hoist put the ownership
  preflight above sweepStaging, so a preflight-refused job (unowned/foreign
  finalDir) threw before the only dead-private-staging-tree reap ran, leaking
  crashed multi-GB staging indefinitely. Split sweepStaging into
  reapDeadStagingTrees() (process-wide, pid-gated dead-`<slug>@<sha>.<pid>`
  reap, run BEFORE the preflight so a refused job still frees its tree) and
  recoverBackup(slug, finalDir) (slug-scoped backup restore/reap, run AFTER the
  preflight so a foreign finalDir link is already refused and never followed).

- Finding 1 (download.ts): the backup restore branch renamed any backup back
  onto an absent finalDir; a foreign-symlink backup (from a prior overwrite
  crash) would be restored after the only ownership check and then followed by
  skip-detection. Restore now requires the backup itself be an owned real
  directory (no-follow lstat + our marker), and the no-follow ownership
  preflight is re-run after recovery, before skip-detection.

- Finding 2 (models.ts + docs, document-and-accept): walkDirStats traverses
  lexical paths, so a concurrent ancestor/TOCTOU symlink swap during the walk
  can redirect sizing. Its output is display-only (the Models table + the
  delete-confirmation "(X GB)" hint) and never drives a destructive decision
  (deletion uses the path-safe name, not the size), so the worst case is a
  wrong size number under adversarial mutation of the user's own store. A
  robust fix needs descriptor-relative traversal node:fs does not expose;
  documented as an accepted residual in code and docs/dashboard.md.

Gate: typecheck 0; dashboard 202/202 (×2); lint 0. Dashboard-only — native and
api.ts unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01VKtaz9j1ih1YtSPBf5TtQm
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