refactor: split agent-service.ts into focused modules#2635
Conversation
Extract buildAgentEnvironment (414 lines) into agent-environment.ts and buildAgentVolumes (402 lines) into agent-volumes.ts. Agent-service.ts retains buildAgentService + buildIptablesInitService with re-exports for backwards compatibility. Split matching test file into agent-environment.test.ts (1,025 lines), agent-volumes.test.ts (617 lines), and agent-service.test.ts (411 lines). All 127 tests pass. Build passes. Closes #2622 Closes #2626 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
✅ Coverage Check PassedOverall Coverage
📁 Per-file Coverage Changes (2 files)
✨ New Files (2 files)
Coverage comparison generated by |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Pull request overview
This PR refactors the agent container configuration code by splitting the previously large src/services/agent-service.ts into two new focused modules (agent-environment.ts and agent-volumes.ts) while keeping agent-service.ts as the service-definition layer and re-export surface for backwards compatibility. It also mirrors this split in the Jest test suite to keep tests aligned with production responsibilities.
Changes:
- Extracted environment-variable construction into
src/services/agent-environment.tsand volume/bind-mount construction intosrc/services/agent-volumes.ts. - Updated
src/services/agent-service.tsto re-export the moved symbols while keepingbuildAgentService()/buildIptablesInitService()in place. - Split the original monolithic agent service test file into
agent-environment.test.ts,agent-volumes.test.ts, and a reducedagent-service.test.ts.
Show a summary per file
| File | Description |
|---|---|
| src/services/agent-environment.ts | New module containing buildAgentEnvironment() and related types. |
| src/services/agent-volumes.ts | New module containing buildAgentVolumes() and related types. |
| src/services/agent-service.ts | Now focuses on service definitions and re-exports env/volume builders for compatibility. |
| src/services/agent-environment.test.ts | New test file for environment-variable behavior. |
| src/services/agent-volumes.test.ts | New test file for volume/mount behavior. |
| src/services/agent-service.test.ts | Reduced to service-definition-focused tests + remaining integration-style tests. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 6/6 changed files
- Comments generated: 3
| // System paths (read-only) - required for binaries and libraries | ||
| agentVolumes.push( | ||
| '/usr:/host/usr:ro', | ||
| '/bin:/host/bin:ro', | ||
| '/sbin:/host/sbin:ro', | ||
| ); | ||
|
|
||
| // Handle /lib and /lib64 - may be symlinks on some systems | ||
| // Always mount them to ensure library resolution works | ||
| agentVolumes.push('/lib:/host/lib:ro'); | ||
| agentVolumes.push('/lib64:/host/lib64:ro'); | ||
|
|
||
| // Tool cache - language runtimes from GitHub runners (read-only) | ||
| // /opt/hostedtoolcache contains Python, Node, Ruby, Go, Java, etc. | ||
| agentVolumes.push('/opt:/host/opt:ro'); | ||
|
|
||
| // Special filesystem mounts for chroot (needed for devices and runtime introspection) | ||
| // NOTE: /proc is NOT bind-mounted here. Instead, a fresh container-scoped procfs is | ||
| // mounted at /host/proc in entrypoint.sh via 'mount -t proc'. This provides: | ||
| // - Dynamic /proc/self/exe (required by .NET CLR and other runtimes) | ||
| // - /proc/cpuinfo, /proc/meminfo (required by JVM, .NET GC) | ||
| // - Container-scoped only (does not expose host process info) | ||
| // The mount requires SYS_ADMIN capability, which is dropped before user code runs. |
| for (const domain of config.allowedDomains) { | ||
| // Skip patterns that aren't resolvable hostnames | ||
| if (domain.startsWith('*.') || domain.startsWith('.') || domain.includes('*')) continue; | ||
| // Skip if already in hosts file | ||
| if (hostsContent.includes(domain)) continue; |
| // Also hide credentials at /host paths (chroot mounts home at /host$HOME too) | ||
| logger.debug('Hiding credential files at /host paths'); | ||
|
|
||
| // Note: In chroot mode, effectiveHome === getRealUserHome() (see line 433), |
|
@copilot address the review feedback |
- Fix extra indentation on lines 43-262 (dedent by 2 spaces for readability) - Fix hostsContent.includes(domain) false positives with line/field-based matching - Fix stale 'see line 433' comment reference, replace with function/module name" Agent-Logs-Url: https://github.com/github/gh-aw-firewall/sessions/5a4df47e-0521-44a6-8306-a18f28f92ff2
Done in commit b12a1ee. Three fixes applied to
|
|
@copilot merge main |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…service Co-authored-by: lpcox <15877973+lpcox@users.noreply.github.com>
This comment has been minimized.
This comment has been minimized.
Done — merged |
🔬 Smoke Test Results
Overall: PASS (2/2 verifiable tests passed) PR author: @lpcox · Reviewer:
|
Smoke Test Results✅ GitHub MCP: Last 2 merged PRs fetched successfully Overall: PASS
|
🔥 Smoke Test: Copilot BYOK (Offline) Mode
Running in BYOK offline mode ( PR by @lpcox · Reviewer: Overall: PASS (core BYOK path validated; pre-step template vars were not substituted for tests 2–3)
|
🧪 Chroot Smoke Test Results
Overall: ❌ FAILED — Python and Node.js versions differ between host and chroot environment.
|
🏗️ Build Test Suite Results
Overall: 8/8 ecosystems passed — ✅ PASS
|
Smoke Test
Warning Firewall blocked 1 domainThe following domain was blocked by the firewall during workflow execution:
network:
allowed:
- defaults
- "registry.npmjs.org"See Network Configuration for more information.
|
Smoke Test Results
Overall: FAIL — service containers are not reachable from this runner environment.
|
Summary
Splits
src/services/agent-service.ts(1,103 lines) into three focused modules:agent-environment.tsbuildAgentEnvironment()— env vars, proxy config, secretsagent-volumes.tsbuildAgentVolumes()— bind mounts, home whitelisting, SSLagent-service.tsbuildAgentService()+buildIptablesInitService()+ re-exportsAlso splits the matching test file (
agent-service.test.ts, 1,998 lines) into three focused test modules that mirror the production code structure.Backwards Compatibility
agent-service.tsre-exports all public symbols from the new modules, so existing imports continue to work unchanged.Verification
npm run build)Closes #2622
Closes #2626