Skip to content

[code-simplifier] simplify: extract traverseObjectTree and hasLabelIntentMetadata helpers#47908

Merged
pelikhan merged 2 commits into
mainfrom
simplify/ai-credits-add-labels-2026-07-25-ec8c345096c2cdee
Jul 25, 2026
Merged

[code-simplifier] simplify: extract traverseObjectTree and hasLabelIntentMetadata helpers#47908
pelikhan merged 2 commits into
mainfrom
simplify/ai-credits-add-labels-2026-07-25-ec8c345096c2cdee

Conversation

@github-actions

@github-actions github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Summary

Refactors two safe-output JavaScript modules to eliminate duplicated traversal and metadata-check logic by extracting shared helpers.

Changes

actions/setup/js/ai_credits_context.cjs

  • Extracts a new traverseObjectTree(entry, visitor) helper: a depth-first stack-based object traversal that accepts a visitor callback and supports early termination (visitor returns true to stop).
  • Refactors three functions to use it:
    • parseMaxAICreditsFromAuditEntry — finds first matching AI credits field value
    • parseAICreditsErrorInfoFromAuditEntry — collects AI credits and rate-limit error signals
    • parseUnknownModelAICreditsFromAuditEntry — detects unknown model credit type
  • Removes ~30 lines of duplicated while (stack.length > 0) loop boilerplate across those three functions.

actions/setup/js/add_labels.cjs

  • Extracts a new hasLabelIntentMetadata(spec) helper that checks whether a label spec has any of rationale, confidence, or suggest fields.
  • Replaces three inline Boolean(x.rationale || x.confidence || x.suggest) expressions with calls to the helper.

Scope

  • Breaking change: No
  • Behaviour change: No — pure refactor; logic is identical
  • Files changed: 2 (add_labels.cjs, ai_credits_context.cjs)
  • Commits: 2 (extraction + CJS typecheck fix / reviewer nits)

Generated by PR Description Updater for #47908 · sonnet46 · 26.4 AIC · ⌖ 7.44 AIC · ⊞ 4.8K ·

…a in safe-output JS modules

- ai_credits_context.cjs: extract shared traverseObjectTree() helper to eliminate
  three separate duplicated stack-based depth-first traversal loops (parseMaxAICredits,
  parseAICreditsErrorInfo, parseUnknownModelAICredits all shared identical traversal logic)
- add_labels.cjs: extract hasLabelIntentMetadata() helper to consolidate three inline
  Boolean(spec.rationale || spec.confidence || spec.suggest) checks at lines 134, 135, 220

Behavior is unchanged. make build and make lint pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan
pelikhan marked this pull request as ready for review July 25, 2026 04:34
Copilot AI review requested due to automatic review settings July 25, 2026 04:34
@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

Test Quality Sentinel completed test quality analysis.

No test files were added or modified in PR #47908. The changes only affected production JavaScript files (actions/setup/js/add_labels.cjs and actions/setup/js/ai_credits_context.cjs). Test Quality Sentinel analysis not required.

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

PR Code Quality Reviewer completed the code quality review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR removes duplicated traversal and metadata checks while preserving existing safe-output behavior.

Changes:

  • Extracts reusable nested-object traversal logic.
  • Centralizes label intent metadata detection.
Show a summary per file
File Description
actions/setup/js/ai_credits_context.cjs Reuses a shared depth-first traversal helper.
actions/setup/js/add_labels.cjs Reuses a label metadata predicate.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Medium

@github-actions

github-actions Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor Author

Design Decision Gate 🏗️ completed the design decision gate check.

No ADR enforcement needed: PR #47908 does not have the 'implementation' label and has 0 new lines of code in business logic directories (threshold: 100).

@github-actions github-actions Bot mentioned this pull request Jul 25, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Skills-Based Review 🧠

Applied /codebase-design — two minor clean-up suggestions, no blocking issues.

📋 Key Themes & Highlights

Positive Highlights

  • traverseObjectTree is a well-designed deep module: simple interface (visitor returning true to stop), rich behaviour (depth-first stack traversal). The three callers become much more readable.
  • hasLabelIntentMetadata eliminates a repeated boolean expression and gives it a meaningful name — good domain language move.
  • ✅ JSDoc on both new helpers is accurate and useful.

Minor Issues (non-blocking)

  1. || undefined idiom (ai_credits_context.cjs line 275) — correct but non-obvious; an explicit if/return true is clearer.
  2. ?? null coercion (add_labels.cjs line 142) — unnecessary since hasLabelIntentMetadata already guards with spec &&; can be hasLabelIntentMetadata(existing).

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · sonnet46 · 25.7 AIC · ⌖ 4.78 AIC · ⊞ 6.7K
Comment /matt to run again

return traverseObjectTree(entry, (_key, value) => value === UNKNOWN_MODEL_AI_CREDITS_TYPE || undefined);
}

/**

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[/codebase-design] The || undefined idiom is correct but non-obvious — readers may not immediately see why false is avoided here.

💡 Clearer alternatives

The visitor contract requires returning true to stop early; any other value (including undefined) continues traversal. Returning false also works (since false !== true), but the intent is obscured by using || undefined to coerce a falsy result.

Consider:

return traverseObjectTree(entry, (_key, value) => {
  if (value === UNKNOWN_MODEL_AI_CREDITS_TYPE) return true;
});

or explicitly ternary:

return traverseObjectTree(entry, (_key, value) =>
  value === UNKNOWN_MODEL_AI_CREDITS_TYPE ? true : undefined
);

Either form makes the early-exit signal explicit and avoids the || undefined surprise.

@copilot please address this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Updated in 7c4aa71. I replaced the || undefined callback with an explicit early-return form to make the intent clear and avoid the fragile idiom.

const existing = requestedLabelSpecByLowerName.get(key);
const newHasMetadata = Boolean(label.rationale || label.confidence || label.suggest);
const existingHasMetadata = existing && Boolean(existing.rationale || existing.confidence || existing.suggest);
const newHasMetadata = hasLabelIntentMetadata(label);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

[/codebase-design] hasLabelIntentMetadata(existing ?? null) — the ?? null coercion is unnecessary since the function already guards with spec &&.

💡 Simplification

hasLabelIntentMetadata starts with spec && (...), so passing undefined is handled identically to null. The ?? null adds noise without safety benefit.

const existingHasMetadata = hasLabelIntentMetadata(existing);

This aligns with the third call site at line 225 which passes labelSpec directly without coercion.

@copilot please address this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Updated in 7c4aa71. I removed the ?? null call-site coercion and adjusted the helper JSDoc to accept undefined, preserving behavior while keeping the call clean.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Review: Clean Refactor ✅

Both extractions are behaviorally equivalent to the original code. Key points verified:

  • traverseObjectTree: the visitor-returns-true-to-stop pattern is correctly implemented. The || undefined in parseUnknownModelAICreditsFromAuditEntry is intentional — undefined is not === true, so traversal continues normally when the value doesn't match.
  • hasLabelIntentMetadata: adds a null guard (spec &&) that is safe; existing ?? null correctly preserves the prior falsy-check semantics.
  • No edge case regressions observed.

Nice simplification.

🧵 Reviewed using Impeccable skills by Impeccable Skills Reviewer · sonnet46 · 14.7 AIC · ⌖ 5.2 AIC · ⊞ 5K

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Non-blocking: one low-severity idiom concern

The refactor is mechanically correct — deduplication is accurate, early-stop semantics are preserved for parseMaxAICreditsFromAuditEntry, and parseAICreditsErrorInfoFromAuditEntry is correctly never stopped early.

One low-severity concern flagged

parseUnknownModelAICreditsFromAuditEntry (line 146): The expression value === UNKNOWN_MODEL_AI_CREDITS_TYPE || undefined returns undefined on mismatch rather than false. This only works because traverseObjectTree uses strict === true to gate early-stop. An explicit if (value === ...) return true; form would make the intent unambiguous and remove the implicit dependency on that implementation detail. See inline comment for a suggested fix.

🔎 Code quality review by PR Code Quality Reviewer · sonnet46 · 33.5 AIC · ⌖ 4.66 AIC · ⊞ 5.7K
Comment /review to run again

traverseObjectTree(entry, (key, value) => {
if (AI_CREDITS_FIELDS.has(key)) {
const parsed = parsePositiveNumberString(value);
if (parsed) aiCredits = parsed;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fragile early-stop idiom: value === UNKNOWN_MODEL_AI_CREDITS_TYPE || undefined returns undefined on mismatch, only working coincidentally because traverseObjectTree checks === true.

💡 Suggested fix

Use an explicit callback instead:

function parseUnknownModelAICreditsFromAuditEntry(entry) {
  return traverseObjectTree(entry, (_key, value) => {
    if (value === UNKNOWN_MODEL_AI_CREDITS_TYPE) return true;
  });
}

When equality fails, false || undefined evaluates to undefined (not false), which happens to not trigger the early-stop only because traverseObjectTree uses strict === true. If that check is ever relaxed to truthy, this still works — but the intent is completely invisible. Making the return true explicit costs one line and removes the dependency on an internal implementation detail.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Addressed in 7c4aa71 with the same explicit callback change in parseUnknownModelAICreditsFromAuditEntry (if (...) return true).

@pelikhan

Copy link
Copy Markdown
Collaborator

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>

Copilot AI commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

@copilot run pr-finisher skill
Fix https://github.com/github/gh-aw/actions/runs/30144261781/job/89643026972

Fixed in 7c4aa71. I addressed the failing CJS typecheck by making the traversal callback return-path explicit and updated the two follow-up simplification nits in the same area.

Copilot AI requested a review from pelikhan July 25, 2026 04:55
@pelikhan
pelikhan merged commit 61c33d2 into main Jul 25, 2026
8 checks passed
@pelikhan
pelikhan deleted the simplify/ai-credits-add-labels-2026-07-25-ec8c345096c2cdee branch July 25, 2026 05:04
@github-actions

Copy link
Copy Markdown
Contributor Author

🎉 This pull request is included in a new release.

Release: v0.83.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants