Skip to content

chore(utils): migrate to shared random/ID utilities and add enforcement linting#4623

Merged
waleedlatif1 merged 6 commits into
stagingfrom
chore/expand-sim-utils
May 16, 2026
Merged

chore(utils): migrate to shared random/ID utilities and add enforcement linting#4623
waleedlatif1 merged 6 commits into
stagingfrom
chore/expand-sim-utils

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Replace all Math.random(), crypto.randomUUID(), crypto.randomBytes(), nanoid, and uuid usages with shared @sim/utils/random and @sim/utils/id helpers across 72 files
  • Add new @sim/utils exports: deepClone, omit, filterUndefined (object), truncate (string), backoffWithJitter, parseRetryAfter (retry), getErrorMessage (errors)
  • Sweep all getErrorMessage, inline sleep, JSON.parse(JSON.stringify(...)) callsites across 500+ files to use shared utilities
  • Add Biome noRestrictedImports rule to ban direct nanoid, uuid, crypto.randomUUID, crypto.randomBytes imports at lint time
  • Add scripts/check-utils-enforcement.ts to catch Math.random() and crypto.* global property access (which import analysis cannot catch), plus inline sleep/deepClone patterns
  • Add check:utils script to package.json

Type of Change

  • Improvement (code quality / consistency)

Testing

Tested manually — bun run check:utils passes clean with 0 violations

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

…nt linting

- Replace all Math.random(), crypto.randomUUID(), crypto.randomBytes(), nanoid, and uuid usages with shared @sim/utils/random and @sim/utils/id helpers across 72 files
- Add new @sim/utils exports: deepClone, omit, filterUndefined (object), truncate (string), backoffWithJitter, parseRetryAfter (retry), getErrorMessage (errors)
- Sweep all getErrorMessage, sleep, deepClone callsites across 500+ files to use shared utilities
- Add Biome noRestrictedImports rule to catch nanoid, uuid, and crypto named imports at lint time
- Add scripts/check-utils-enforcement.ts to catch Math.random and crypto.* global property access
- Add check:utils script to package.json
@vercel
Copy link
Copy Markdown

vercel Bot commented May 15, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment May 16, 2026 0:25am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 15, 2026

PR Summary

Medium Risk
Broad mechanical changes across many API routes and realtime handlers; low logical complexity but wide surface area could subtly change error messages or randomness/ID formatting in edge cases.

Overview
Standardizes error handling and randomness across the codebase by replacing inline instanceof Error message extraction with shared getErrorMessage() and swapping Math.random() usages for @sim/utils/random helpers (e.g. randomFloat, randomInt).

Also removes local getErrorMessage helpers and replaces ad-hoc random string/ID generation in file/lock identifiers with generateShortId(), plus updates project rules/docs (CLAUDE.md, .claude/rules/global.md) to enforce the shared utility patterns.

Reviewed by Cursor Bugbot for commit cbbf972. Configure here.

deepClone() was a one-line wrapper around structuredClone(), which is
universally available in Node 17+ and all modern browsers. Removing the
abstraction reduces indirection and means contributors don't need to
learn a project-specific name for a well-known built-in.

- Remove deepClone from packages/utils/src/object.ts and index.ts
- Replace all 17 call sites with structuredClone() directly
- Update check:utils script suggestion text
- Update CLAUDE.md and global.md docs
…uncate docs

- Add noRestrictedImports to biome.json under style — bans nanoid and uuid
  package imports at lint time (crypto.randomUUID/randomBytes are caught by
  the check:utils grep script which handles global property access)
- Correct truncate() TSDoc and parameter name: sliceLength makes it clear
  that total output length is sliceLength + suffix.length, matching the
  behavior all callers were already written to expect
The sweep agents added getErrorMessage calls without the corresponding
import in 4 files, causing test failures. Added the missing imports.
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Comment thread apps/sim/app/api/tools/crowdstrike/query/route.ts Outdated
…Turbopack issue

- Fix retry.ts cross-file import: Turbopack cannot resolve './random.js' for
  internal package imports; inline the jitter crypto call directly
- Add missing getErrorMessage imports to 32 files where the sweep added calls
  without the corresponding import (caught by type-check and test runs)
- Remove accidental getErrorMessage import from crowdstrike/query/route.ts
  which has its own domain-specific getErrorMessage for parsing CrowdStrike's
  JSON error format
- Fix use-sub-block-value.ts type error from structuredClone narrowing:
  add 'as T' cast at emitValue callsite (safe — valueCopy is always a
  structural copy of newValue)
…etErrorMessage

The catch block was calling the local getErrorMessage function which
parses CrowdStrike API JSON responses, not JavaScript Error objects.
Use toError(error).message to correctly extract the message from a
caught value in this context.
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit cbbf972. Configure here.

@waleedlatif1 waleedlatif1 merged commit 8d7bbbc into staging May 16, 2026
14 checks passed
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