Skip to content

Audit and inline trivial single-use local aliases #4876

@cv

Description

@cv

Context

While resolving PR #4471, we found a trivial local variable that could be inlined safely:

await onboardRuntimeBoundary.recordStateResultsWithStepCompatibility(providerInferenceResult.stateResults);
session = providerInferenceResult.session;

const sandboxStateResult = await handleSandboxState({
  session,
  // ...
});

The reassignment only fed the next call and was clearer as:

await onboardRuntimeBoundary.recordStateResultsWithStepCompatibility(providerInferenceResult.stateResults);

const sandboxStateResult = await handleSandboxState({
  session: providerInferenceResult.session,
  // ...
});

This suggests there may be other low-risk single-use local aliases in the TypeScript/JavaScript codebase that can be simplified.

Initial exploration

A raw ast-grep scan for property-alias-shaped locals found many possible matches:

npx --package @ast-grep/cli ast-grep run \
  --lang ts \
  -p 'const $V = $OBJ.$PROP' \
  --json=stream src test scripts nemoclaw/src

At the time of exploration, this returned about 679 syntactic matches in the main code/test areas. A follow-up TypeScript-symbol filter narrowed that to about 378 conservative single-use const aliases across code and tests:

  • src: about 171
  • test: about 189
  • scripts: about 10
  • nemoclaw/src: about 7
  • nemoclaw-blueprint: about 1

Example candidates from the exploratory pass:

src/lib/domain/sandbox/logs.ts:157
  text = lines[lineIndex]
  use: parseLineTimestamp(text)

src/lib/inference/onboard-probes.ts:100
  fn = value.function
  use: hasValidFunctionCallPayload(fn)

src/lib/messaging/compiler/manifest-compiler.ts:302
  value = process.env[input.envKey]
  use: value?.replace(...)

src/lib/state/sandbox-session.ts:202
  sessionCount = matchingSessions.length
  use: sessionCount > 0

nemoclaw/src/blueprint/runner.ts:183
  version = value.version
  use: isOptionalString(version)

Not every match should be changed. Some single-use locals are useful documentation, avoid repeated complex expressions, improve test readability, or are required patterns such as exhaustiveness guards:

const _exhaustive: never = runtime;
return _exhaustive;

Proposed work

Create a focused cleanup PR, or a small series of cleanup PRs, that uses ast-grep plus symbol-aware filtering to find and manually review obvious single-use local aliases.

The goal is not to add a broad lint rule immediately. The first pass should establish which patterns are actually helpful to inline in this codebase.

Suggested approach

  1. Use ast-grep to find syntactic shapes such as:
    • const $V = $OBJ.$PROP
    • const $V = $OBJ[$KEY]
    • const $V = $EXPR as $TYPE
    • simple literal or identifier aliases used exactly once
  2. Add a TypeScript-aware filter, or equivalent review script, that only reports variables with exactly one local reference.
  3. Sort candidates by lowest risk:
    • declaration and use in adjacent statements
    • initializer is side-effect-free
    • use is a simple argument, condition, object property, or return expression
  4. Manually review candidates before editing.
  5. Avoid changes where the local name documents intent, improves tests, prevents long-line churn, or makes a complex expression easier to read.
  6. Prefer small reviewable patches over a large mechanical rewrite.

Acceptance criteria

  • A PR removes a set of clearly redundant single-use locals without changing behavior.
  • The PR does not mix this cleanup with unrelated feature or conflict-resolution work.
  • Tests and type-checks relevant to touched files pass.
  • Any helper script used for the scan is either:
    • kept outside the repo and documented in the PR, or
    • committed only if maintainers want a reusable cleanup/audit tool.
  • The PR explains the selection criteria and lists notable classes intentionally skipped.

Suggested labels / type

  • Native Issue Type: Task
  • Labels: area: architecture, help wanted

Metadata

Metadata

Assignees

Labels

area: architectureArchitecture, design debt, major refactors, or maintainabilityhelp wantedMaintainers welcome external contributionv0.0.62Release target

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions