Skip to content

refactor: phase 1 shell dedup foundation#151

Merged
benvinegar merged 6 commits intomainfrom
refactor/phase1-shell-dedup-foundation
Feb 24, 2026
Merged

refactor: phase 1 shell dedup foundation#151
benvinegar merged 6 commits intomainfrom
refactor/phase1-shell-dedup-foundation

Conversation

@benvinegar
Copy link
Copy Markdown
Member

@benvinegar benvinegar commented Feb 23, 2026

Summary

  • extract shared release runtime helpers for update/rollback restart, health, and deployed SHA verification
  • extract shared shell arg helpers and reuse them in doctor/security-audit/update/rollback
  • add shared check/report counter + summary helpers and reuse them in doctor/security-audit
  • add tests for new shared helpers and wire them into shell script test suite

Files

  • new: bin/lib/release-runtime-common.sh
  • new: bin/lib/check-report-common.sh
  • new: bin/lib/check-report-common.test.sh
  • updated: bin/update-release.sh
  • updated: bin/rollback-release.sh
  • updated: bin/security-audit.sh
  • updated: bin/lib/doctor-common.sh
  • updated: bin/doctor.sh
  • updated: bin/lib/shell-common.sh
  • updated: test/shell-scripts.test.mjs

Validation

  • npm run test:shell
  • bash bin/update-release.test.sh
  • bash bin/rollback-release.test.sh
  • bash -n on modified scripts

Notes

  • npm run lint:shell is currently blocked in this environment because shellcheck is not installed in PATH.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented Feb 23, 2026

Greptile Summary

Extracted shared shell script helpers to reduce duplication across update, rollback, doctor, and security-audit scripts. Created three new library modules:

  • release-runtime-common.sh - Consolidates release restart, health check, and SHA verification logic from update/rollback scripts
  • check-report-common.sh - Provides counter management and summary formatting helpers for doctor and security-audit
  • Added bb_require_option_value and bb_has_arg to shell-common.sh for consistent arg parsing

The refactoring successfully eliminates ~127 lines of duplicated code while maintaining behavioral compatibility. All changes are well-structured and improve maintainability. Tests are provided for check-report-common.sh but missing for the new release-runtime-common.sh and shell-common.sh helpers.

Confidence Score: 4/5

  • This refactoring is safe to merge with minimal risk - the changes reduce duplication while preserving existing behavior
  • Score reflects solid refactoring with good test coverage for check-report helpers, but missing tests for release-runtime and shell-common arg helpers. The extracted logic is straightforward and well-structured. Validated through existing shell test suite per PR description.
  • Consider adding tests for bin/lib/release-runtime-common.sh and the new shell-common.sh helpers (bb_require_option_value, bb_has_arg)

Important Files Changed

Filename Overview
bin/lib/release-runtime-common.sh New shared library extracting release runtime helpers for update/rollback; functions lack unit tests but logic is sound
bin/lib/check-report-common.sh New shared counter and summary helpers with comprehensive unit tests; clean implementation
bin/lib/shell-common.sh Added arg parsing helpers bb_require_option_value and bb_has_arg; no unit tests for new functions
bin/update-release.sh Refactored to use shared helpers; significantly reduced duplication and improved maintainability
bin/rollback-release.sh Refactored to use shared helpers; cleaner code with reduced duplication

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Shell Scripts] --> B[bin/update-release.sh]
    A --> C[bin/rollback-release.sh]
    A --> D[bin/security-audit.sh]
    A --> E[bin/doctor.sh]
    
    B --> F[bin/lib/release-runtime-common.sh]
    C --> F
    
    B --> G[bin/lib/shell-common.sh]
    C --> G
    D --> G
    E --> G
    
    D --> H[bin/lib/check-report-common.sh]
    E --> I[bin/lib/doctor-common.sh]
    I --> H
    
    F -->|restart_and_health| J[bb_run_release_restart_and_health]
    F -->|version_check| K[bb_verify_deployed_release_sha]
    
    G -->|arg_parsing| L[bb_require_option_value]
    G -->|arg_parsing| M[bb_has_arg]
    
    H -->|counters| N[bb_counter_reset_many]
    H -->|counters| O[bb_counter_inc]
    H -->|display| P[bb_summary_print_*]
    
    style F fill:#e1f5ff
    style G fill:#e1f5ff
    style H fill:#e1f5ff
    style I fill:#fff4e1
Loading

Last reviewed commit: e8d7e9e

Copy link
Copy Markdown

@greptile-apps greptile-apps Bot left a comment

Choose a reason for hiding this comment

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

10 files reviewed, 2 comments

Edit Code Review Agent Settings | Greptile

Comment thread bin/lib/shell-common.sh
Comment on lines +87 to +108
bb_require_option_value() {
local option="$1"
local argc="$2"

if [ "$argc" -lt 2 ]; then
bb_die "$option requires a value"
fi
}

bb_has_arg() {
local needle="$1"
shift || true

local arg
for arg in "$@"; do
if [ "$arg" = "$needle" ]; then
return 0
fi
done

return 1
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider adding unit tests for these new helpers (bb_require_option_value and bb_has_arg). Other lib files with exported functions have corresponding .test.sh files.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: bin/lib/shell-common.sh
Line: 87-108

Comment:
Consider adding unit tests for these new helpers (`bb_require_option_value` and `bb_has_arg`). Other lib files with exported functions have corresponding `.test.sh` files.

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Clean follow-up: Added tests in bin/lib/shell-common.test.sh for bb_has_arg and bb_require_option_value, and wired them into test/shell-scripts.test.mjs so they run in npm run test:shell.

Responded by dev-agent using openai/gpt-5.

Comment on lines +1 to +74
#!/bin/bash
# Shared runtime helpers for release update/rollback scripts.
#
# Expects caller to provide:
# - log() and die() functions
# - restart_baudbot_service_if_active()
# - json_get_string_stdin()
# - BAUDBOT_AGENT_USER and BAUDBOT_AGENT_HOME

bb_run_release_override_cmd() {
local description="$1"
local command="$2"
local env_array_name="$3"

[ -n "$command" ] || return 1

local -n env_ref="$env_array_name"
log "running $description override"
env "${env_ref[@]}" bash -lc "$command"
}

bb_run_release_restart_and_health() {
local restart_cmd="$1"
local skip_restart="$2"
local health_cmd="$3"
local env_array_name="$4"

if ! bb_run_release_override_cmd "restart" "$restart_cmd" "$env_array_name"; then
if [ "$skip_restart" = "1" ]; then
log "skipping restart"
else
restart_baudbot_service_if_active
fi
fi

bb_run_release_override_cmd "health" "$health_cmd" "$env_array_name" || true
}

bb_verify_deployed_release_sha() {
local expected_sha="$1"
local skip_version_check="$2"
local verified_label="${3:-}"

if [ "$skip_version_check" = "1" ]; then
return 0
fi

if [ "$(id -u)" -ne 0 ]; then
log "non-root run: skipping deployed version verification"
return 0
fi

if ! id "$BAUDBOT_AGENT_USER" >/dev/null 2>&1; then
log "agent user '$BAUDBOT_AGENT_USER' missing; skipping deployed version verification"
return 0
fi

local version_file="$BAUDBOT_AGENT_HOME/.pi/agent/baudbot-version.json"
local deployed_sha

deployed_sha="$(sudo -u "$BAUDBOT_AGENT_USER" sh -c "cat '$version_file' 2>/dev/null" | json_get_string_stdin "sha" 2>/dev/null || true)"

if [ -z "$deployed_sha" ]; then
die "deployed version file missing or unreadable: $version_file"
fi

if [ "$deployed_sha" != "$expected_sha" ]; then
die "deployed sha mismatch (expected $expected_sha, got $deployed_sha)"
fi

if [ -n "$verified_label" ]; then
log "deployed version verified: $verified_label"
fi
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider adding unit tests for release-runtime-common.sh. These functions contain critical release logic (restart, health checks, version verification) that would benefit from test coverage.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Prompt To Fix With AI
This is a comment left during a code review.
Path: bin/lib/release-runtime-common.sh
Line: 1-74

Comment:
Consider adding unit tests for `release-runtime-common.sh`. These functions contain critical release logic (restart, health checks, version verification) that would benefit from test coverage.

<sub>Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!</sub>

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added coverage in a new bin/lib/release-runtime-common.test.sh and wired it into test/shell-scripts.test.mjs.

The tests cover restart/health override behavior, including failure propagation and default restart fallback behavior when no override is provided.

Responded by dev-agent using openai/gpt-5.

Comment thread bin/lib/release-runtime-common.sh Outdated
Comment thread bin/lib/release-runtime-common.sh
@benvinegar benvinegar merged commit f08ff3e into main Feb 24, 2026
9 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