Skip to content

refactor: extract shared pi-agent-core driver helpers to eliminate duplication - #49665

Closed
pelikhan with Copilot wants to merge 5 commits into
mainfrom
copilot/duplicate-code-fix
Closed

refactor: extract shared pi-agent-core driver helpers to eliminate duplication#49665
pelikhan with Copilot wants to merge 5 commits into
mainfrom
copilot/duplicate-code-fix

Conversation

Copilot AI commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

pi_agent_core_driver_sample_node.cjs duplicated emitJsonl, getApiKey, and related logic verbatim from pi_agent_core_driver.cjs, creating a drift risk where any change to JSONL formatting or provider key resolution had to be applied in two places.

Changes

  • New actions/setup/js/pi_agent_core_driver_helpers.cjs — shared module exporting emitJsonl and getApiKey (built-in provider resolution); single source of truth for both drivers.

  • actions/setup/js/pi_agent_core_driver.cjs — imports emitJsonl and getBuiltinApiKey from helpers; buildGetApiKey delegates to getBuiltinApiKey for the no-gateway path instead of repeating the switch.

  • .github/drivers/pi_agent_core_driver_sample_node.cjs — replaces the two duplicated functions with a single require(). Comment updated to guide users who copy the file to repos without gh-aw action sources.

// Before: both files each defined these ~20 lines
function emitJsonl(obj) { process.stdout.write(JSON.stringify(obj) + "\n"); }
function getApiKey(provider) { switch (provider) { /* ... */ } }

// After: sample driver imports from the shared helper
const { emitJsonl, getApiKey } = require("../../actions/setup/js/pi_agent_core_driver_helpers.cjs");

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 12.8 AIC · ⌖ 5.94 AIC · ⊞ 8.1K ·
Comment /souschef to run again


Run details: https://github.com/github/gh-aw/actions/runs/30726226343

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 14.2 AIC · ⌖ 4.75 AIC · ⊞ 5.7K ·
Comment /souschef to run again

…r_helpers.cjs

Move the duplicate `emitJsonl` and built-in `getApiKey` implementations into
a new shared helper module `actions/setup/js/pi_agent_core_driver_helpers.cjs`.

- pi_agent_core_driver.cjs now imports `emitJsonl` and `getBuiltinApiKey`
  from the helper; `buildGetApiKey` delegates to `getBuiltinApiKey` for the
  no-gateway path instead of repeating the switch statement.
- The sample driver imports `emitJsonl` and `getApiKey` from the helper via
  a relative path, removing ~30 lines of duplicated code.

Closes #49663

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor pi-agent-core sample driver to reuse shared helpers refactor: extract shared pi-agent-core driver helpers to eliminate duplication Aug 1, 2026
Copilot AI requested a review from pelikhan August 1, 2026 22:24
@pelikhan
pelikhan marked this pull request as ready for review August 1, 2026 22:45
Copilot AI review requested due to automatic review settings August 1, 2026 22:45
@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

PR Code Quality Reviewer completed the code quality review.

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

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

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Test Quality Sentinel completed test quality analysis.

No test files were added or modified in this PR. PR #49665 contains only production files (.cjs driver files). Test Quality Sentinel skipped.

@github-actions

github-actions Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

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

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

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

Extracts shared Pi driver utilities to reduce duplication between production and sample drivers.

Changes:

  • Adds shared JSONL and API-key helpers.
  • Updates both drivers to consume the shared module.
  • Documents helper requirements for copied samples.
Show a summary per file
File Description
actions/setup/js/pi_agent_core_driver.cjs Uses shared driver helpers.
actions/setup/js/pi_agent_core_driver_helpers.cjs Defines shared JSONL and key-resolution utilities.
.github/drivers/pi_agent_core_driver_sample_node.cjs Replaces duplicated helpers with an import.

Review details

Tip

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

  • Files reviewed: 3/3 changed files
  • Comments generated: 1
  • Review effort level: Balanced

Comment thread .github/drivers/pi_agent_core_driver_sample_node.cjs Outdated

@github-actions github-actions Bot 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.

Skills-Based Review 🧠

Applied /codebase-design — requesting changes on two small but meaningful issues before merge.

📋 Key Themes & Highlights

Key Themes

  • Naming inconsistency: getApiKey is exported from the helpers module but aliased as getBuiltinApiKey at the main driver's import site. The helpers module should own the descriptive name.
  • Fragile require path in the sample driver: The '../../actions/setup/js/... path is the most likely copy-paste failure for users, and the comment warns them — but a path.resolve call gives a better error message and no extra cost.

Positive Highlights

  • ✅ Clean extraction — the helpers module is well-documented and the split is correct
  • ✅ No behaviour change; the codex case that was only in pi_agent_core_driver.cjs is now correctly included in the shared helper
  • ✅ Comment in sample driver actively guides copy-paste users

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · sonnet46 · 29.9 AIC · ⌖ 11.9 AIC · ⊞ 7.1K
Comment /matt to run again

return undefined;
}
}
const { emitJsonl, getApiKey } = require("../../actions/setup/js/pi_agent_core_driver_helpers.cjs");

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.

[/codebase-design] The require('../../actions/setup/js/pi_agent_core_driver_helpers.cjs') path hard-codes a relative depth that breaks if either file moves. The header comment already warns users who copy the file — but this is a sample driver designed to be copied, so a stale relative path is the highest-probability failure mode. Consider replacing with path.resolve(__dirname, ...) for a clearer runtime error when the path is wrong.

💡 Example
const path = require('path');
const { emitJsonl, getApiKey } = require(
  path.resolve(__dirname, '../../actions/setup/js/pi_agent_core_driver_helpers.cjs')
);

This keeps the same semantics but produces a clear MODULE_NOT_FOUND with the resolved absolute path rather than a puzzling relative-path error.

@copilot please address this.

* @returns {string|undefined}
*/
function getApiKey(provider) {
switch (provider) {

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.

[/codebase-design] The exported name getApiKey does not signal that it only covers built-in providers. The main driver already aliases it as getBuiltinApiKey at the import site, which is the clearer name — rename the export to match so the public surface is self-documenting and the alias is unnecessary.

💡 Suggested rename
// pi_agent_core_driver_helpers.cjs
function getBuiltinApiKey(provider) { ... }
module.exports = { emitJsonl, getBuiltinApiKey };

// pi_agent_core_driver.cjs
const { emitJsonl, getBuiltinApiKey } = require('./pi_agent_core_driver_helpers.cjs');

// sample driver
const { emitJsonl, getBuiltinApiKey: getApiKey } = require('...');

Consistent naming prevents future callers from confusing the helper with a fully generic key resolver.

@copilot please address this.

@github-actions github-actions Bot 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.

🧵 Reviewed using Impeccable skills by Impeccable Skills Reviewer · sonnet46 · 21 AIC · ⌖ 7.41 AIC · ⊞ 5.4K

return undefined;
}
}
const { emitJsonl, getApiKey } = require("../../actions/setup/js/pi_agent_core_driver_helpers.cjs");

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.

The old inline getApiKey did not handle the "codex" provider string (it fell through to default: return undefined). The new shared helper adds case "codex": alongside case "openai":, so the sample driver now resolves CODEX_API_KEY/OPENAI_API_KEY for "codex" requests where it previously returned undefined. This is almost certainly the right alignment — confirm any callers that relied on the old undefined return for "codex" still behave correctly.

@copilot please address this.

@github-actions github-actions Bot mentioned this pull request Aug 1, 2026

@github-actions github-actions Bot 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.

Verdict: Approve — clean deduplication refactor, no functional changes.

Extracted emitJsonl/getApiKey into a shared helper module consumed by both the built-in and sample drivers. Verified require("../../actions/setup/js/pi_agent_core_driver_helpers.cjs") resolves correctly from .github/drivers/, module exports load fine, and no .lock.yml files embed the old duplicated switch statements that would need updating.

💡 Review notes
  • Behavior-preserving: switch statement logic for emitJsonl/getApiKey is copied verbatim into the new shared module.
  • Relative require path from the sample driver correctly walks up to actions/setup/js/.
  • No test coverage changes needed since this is a pure extraction with identical logic.
  • Comment update in the sample driver appropriately warns downstream copiers about the new dependency.

🔎 Code quality review by PR Code Quality Reviewer · auto · 80.6 AIC · ⌖ 5.17 AIC · ⊞ 7.8K
Comment /review to run again

@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot quick triage for this PR:

Run details: https://github.com/github/gh-aw/actions/runs/30722880612

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 12.8 AIC · ⌖ 5.94 AIC · ⊞ 8.1K ·
Comment /souschef to run again

@gh-aw-bot

Copy link
Copy Markdown
Collaborator

@copilot quick triage for this PR:

Run details: https://github.com/github/gh-aw/actions/runs/30726226343

Generated by 👨‍🍳 PR Sous Chef · gpt54 · 14.2 AIC · ⌖ 4.75 AIC · ⊞ 5.7K ·
Comment /souschef to run again

Co-authored-by: gh-aw-bot <259018956+gh-aw-bot@users.noreply.github.com>

Copilot AI commented Aug 2, 2026

Copy link
Copy Markdown
Contributor Author

@copilot quick triage for this PR:

Addressed the open review feedback in acff48f: the sample driver now resolves the helper via `${process.env.RUNNER_TEMP}/gh-aw/actions/pi_agent_core_driver_helpers.cjs` — the flat runtime location where the gh-aw setup action deploys all CJS helpers. This works correctly whether the sample lives in the gh-aw repo or has been copied to a user's own .github/drivers/ directory.

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

🔍 Triage Summary

Field Value
Category refactor
Risk medium
Priority low
Score 25/100 (impact 15 + urgency 5 + quality 5)
Recommended action defer
Batch

Extracts shared pi-agent-core driver helpers. CHANGES_REQUESTED from automated reviewer — needs rework.

Automated triage — see the triage report for full context.

Generated by 🔧 PR Triage Agent · auto · 40.3 AIC · ⌖ 4.01 AIC · ⊞ 8K ·

@pelikhan pelikhan closed this Aug 2, 2026
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.

[duplicate-code] Duplicate Code: pi-agent-core sample driver mirrors production event loop

4 participants