# suryakiran434/brewautomation architecture

> Architecture diagram of the GitHub repository suryakiran434/brewautomation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/suryakiran434/brewautomation
- Repository: https://github.com/suryakiran434/brewautomation

## Overview

1. **Overall shape**  
This is a macOS automation repository centered on `launchd`. It has two scheduled workflows: a **daily package-update pipeline** and a **twice-weekly system restart pipeline**, plus a **timezone watcher** that keeps scheduling aligned with local time changes.

2. **Main runtime boundary: launchd → shell scripts**  
The `.plist` files define how macOS triggers the scripts:
- `com.suryakiran.brewauto.plist` runs the Homebrew/uv/Python update flow as a **user LaunchAgent**.
- `com.suryakiran.restart.plist` runs the reboot flow as a **system LaunchDaemon**.
- `com.suryakiran.tzwatch.plist` polls for timezone changes and reloads the brew agent when needed.  
This means the orchestration layer is OS-native scheduling, not a long-running app.

3. **Brew update pipeline**  
The update path is split into a **guard stage** and an **execution stage**:
- `brew_autoupdate.sh` checks whether the job already ran today and enforces duplicate prevention via a lock file / stale-lock logic.
- If allowed, it hands off to `bubu_executor.sh`, which performs the actual upgrades.  
This separation is the core boundary between “should we run?” and “do the work.”

4. **Package upgrade execution**  
`bubu_executor.sh` is the main workhorse. It runs the actual tooling updates:
- `brew update`
- `brew upgrade`
- `brew upgrade --cask`
- `uv tool upgrade --all`
- `uv pip install --upgrade ...` for Python packages, using a resolved Python interpreter path  
It also handles iTerm2 execution if available, with a background-process fallback if not.

5. **Restart pipeline**  
`restart_script.sh` handles system restarts and notification around them. For scheduled runs it proceeds without user confirmation; for manual runs it has a short confirmation window unless `--force` is used. The actual system action is `shutdown -r now`, so the script is effectively the policy/notification wrapper around a privileged reboot.

6. **Notifications as a separate service boundary**  
`notify.py` is the email subsystem. It sends HTML and plain-text emails via Gmail SMTP and is used by both the update and restart flows. The README indicates it is responsible for:
- success/failure emails
- HTML escaping of package and metadata fields
- handling SMTP/config/network failures with distinct exit codes  
So the repo treats email as a reusable external service client, not as inline shell logic.

7. **Configuration and secrets**  
Runtime configuration comes from a local `.env` file, which stores sender/recipient email settings and tool paths like `BREW_PATH` and `UV_PATH`. The repository hardens this boundary by requiring restrictive file permissions and by passing credentials only in scoped environment variables to child processes, avoiding exposure in process listings.

8. **State, locking, and idempotency**  
The automation maintains local state files and locks in the project directory:
- lock files for duplicate prevention and stale-process detection
- completion markers and logs for daily run tracking
- timezone state files for watcher coordination  
The architecture relies on local filesystem state rather than a database or remote queue.

9. **Logging and observability**  
Each flow has dedicated logs, with separate files for automated vs manual runs and for stdout/stderr from launchd. This gives clear diagnostic boundaries:
- brew update logs
- error logs
- skip logs
- restart history logs
- LaunchAgent/LaunchDaemon system logs  
The README also indicates log rotation/retention policies, so log files are part of the operational design, not just debugging artifacts.

10. **Install/reload tooling**  
`reload.sh` and `reload_restart.sh` are deployment scripts for installing/loading the launchd jobs and preparing permissions/log files. They bridge the repo contents to the macOS runtime locations under `~/Library/LaunchAgents` and `/Library/LaunchDaemons`. In diagram terms, these are the provisioning path from repo checkout to scheduled system automation.

11. **Manual triggers share the same execution core**  
The repo supports manual invocation of the same underlying operations:
- `bubu_executor.sh --manual`
- `restart_script.sh --manual`  
Manual runs use separate logs and slightly different control flow, but they still rely on the same notification and tool-execution boundaries as the scheduled jobs.

12. **External dependencies that materially affect the architecture**  
The system depends on:
- macOS `launchd`
- Homebrew
- `uv`
- Python 3
- Gmail SMTP
- optional iTerm2
- optional pyenv for Python resolution  
These are not just tools; they define the runtime environment and failure modes of the automation pipeline.

Main entry points: [`bubu_executor.sh`](https://github.com/suryakiran434/brewautomation/blob/main/bubu_executor.sh) (Update exec), [`restart_script.sh`](https://github.com/suryakiran434/brewautomation/blob/main/restart_script.sh) (Reboot policy), [`notify.py`](https://github.com/suryakiran434/brewautomation/blob/main/notify.py) (Email service), [`brew_autoupdate.sh`](https://github.com/suryakiran434/brewautomation/blob/main/brew_autoupdate.sh) (Run guard)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_orchestration["Orchestration"]
  node_n_brew_plist["Brew agent<br/>launchd plist"]
  node_n_restart_plist["Restart daemon<br/>launchd plist"]
  node_n_tz_plist["TZ watcher<br/>launchd plist"]
end

subgraph group_g_brew["Brew update"]
  node_n_brew_guard["Run guard<br/>shell guard<br/>[brew_autoupdate.sh]"]
  node_n_brew_exec["Update exec<br/>shell executor<br/>[bubu_executor.sh]"]
end

subgraph group_g_restart["Restart"]
  node_n_restart_logic["Reboot policy<br/>shell policy<br/>[restart_script.sh]"]
end

subgraph group_g_support["Support"]
  node_n_notify["Email service<br/>python notifier<br/>[notify.py]"]
  node_n_reload_brew["Reload brew<br/>deploy script<br/>[reload.sh]"]
  node_n_reload_restart["Reload restart<br/>deploy script<br/>[reload_restart.sh]"]
  node_n_tz_reload["TZ reload<br/>watcher script<br/>[tzreload.sh]"]
  node_n_state[("Local state<br/>filesystem state")]
end

subgraph group_g_external["External"]
  node_n_launchd(("launchd<br/>macOS scheduler"))
  node_n_homebrew(("Homebrew<br/>package manager"))
  node_n_uv(("uv<br/>Python toolchain"))
  node_n_python(("Python 3<br/>runtime"))
  node_n_gmail(("Gmail SMTP<br/>mail service"))
  node_n_iterm2(("iTerm2<br/>optional terminal"))
  node_n_pyenv(("pyenv<br/>optional resolver"))
  node_n_shutdown(("shutdown<br/>system command"))
end

node_n_launchd -->|"starts"| node_n_brew_plist
node_n_launchd -->|"starts"| node_n_restart_plist
node_n_launchd -->|"starts"| node_n_tz_plist
node_n_brew_plist -->|"invokes"| node_n_brew_guard
node_n_brew_guard -->|"handoff"| node_n_brew_exec
node_n_brew_exec -->|"updates"| node_n_homebrew
node_n_brew_exec -->|"updates"| node_n_uv
node_n_brew_exec -->|"runs under"| node_n_python
node_n_brew_exec -->|"uses"| node_n_iterm2
node_n_brew_exec -->|"fallback"| node_n_pyenv
node_n_brew_guard -->|"reads/writes"| node_n_state
node_n_brew_exec -->|"logs"| node_n_state
node_n_brew_exec -->|"reports"| node_n_notify
node_n_restart_plist -->|"invokes"| node_n_restart_logic
node_n_restart_logic -->|"executes"| node_n_shutdown
node_n_restart_logic -->|"reports"| node_n_notify
node_n_restart_logic -->|"logs"| node_n_state
node_n_tz_plist -->|"invokes"| node_n_tz_reload
node_n_tz_reload -->|"reloads"| node_n_reload_brew
node_n_reload_brew -->|"loads"| node_n_brew_plist
node_n_reload_restart -->|"loads"| node_n_restart_plist
node_n_tz_reload -->|"tracks"| node_n_state
node_n_notify -->|"sends via"| node_n_gmail
node_n_notify -->|"runs on"| node_n_python

click node_n_brew_plist "https://github.com/suryakiran434/brewautomation/blob/main/com.suryakiran.brewauto.plist"
click node_n_restart_plist "https://github.com/suryakiran434/brewautomation/blob/main/com.suryakiran.restart.plist"
click node_n_tz_plist "https://github.com/suryakiran434/brewautomation/blob/main/com.suryakiran.tzwatch.plist"
click node_n_brew_guard "https://github.com/suryakiran434/brewautomation/blob/main/brew_autoupdate.sh"
click node_n_brew_exec "https://github.com/suryakiran434/brewautomation/blob/main/bubu_executor.sh"
click node_n_restart_logic "https://github.com/suryakiran434/brewautomation/blob/main/restart_script.sh"
click node_n_notify "https://github.com/suryakiran434/brewautomation/blob/main/notify.py"
click node_n_reload_brew "https://github.com/suryakiran434/brewautomation/blob/main/reload.sh"
click node_n_reload_restart "https://github.com/suryakiran434/brewautomation/blob/main/reload_restart.sh"
click node_n_tz_reload "https://github.com/suryakiran434/brewautomation/blob/main/tzreload.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_n_brew_plist,node_n_restart_plist,node_n_tz_plist toneBlue
class node_n_brew_guard,node_n_brew_exec toneAmber
class node_n_restart_logic toneMint
class node_n_notify,node_n_reload_brew,node_n_reload_restart,node_n_tz_reload,node_n_state toneRose
class node_n_launchd,node_n_homebrew,node_n_uv,node_n_python,node_n_gmail,node_n_iterm2,node_n_pyenv,node_n_shutdown toneIndigo
```

## Components

### Orchestration

macOS launchd jobs that trigger the automation on schedules and time-zone changes

- [Brew agent](https://github.com/suryakiran434/brewautomation/blob/main/com.suryakiran.brewauto.plist): launchd plist · User LaunchAgent that schedules the daily package-update workflow · `com.suryakiran.brewauto.plist`
- [Restart daemon](https://github.com/suryakiran434/brewautomation/blob/main/com.suryakiran.restart.plist): launchd plist · System LaunchDaemon that schedules the twice-weekly reboot workflow · `com.suryakiran.restart.plist`
- [TZ watcher](https://github.com/suryakiran434/brewautomation/blob/main/com.suryakiran.tzwatch.plist): launchd plist · Polling job that watches for timezone changes and realigns scheduling · `com.suryakiran.tzwatch.plist`

### Brew update

Daily package update pipeline and its execution stages

- [Run guard](https://github.com/suryakiran434/brewautomation/blob/main/brew_autoupdate.sh): shell guard · Preflight stage that prevents duplicate daily runs using lock and stale-state checks · `brew_autoupdate.sh`
- [Update exec](https://github.com/suryakiran434/brewautomation/blob/main/bubu_executor.sh): shell executor · Main workhorse that performs brew, uv, and Python package upgrades · `bubu_executor.sh`

### Restart

Scheduled reboot workflow and its confirmation policy

- [Reboot policy](https://github.com/suryakiran434/brewautomation/blob/main/restart_script.sh): shell policy · Handles manual confirmation, scheduled bypass, notification, and the actual reboot action · `restart_script.sh`

### Support

Shared deployment, notification, and local-state boundaries

- [Email service](https://github.com/suryakiran434/brewautomation/blob/main/notify.py): python notifier · Shared SMTP client for success/failure HTML and text notifications · `notify.py`
- [Reload brew](https://github.com/suryakiran434/brewautomation/blob/main/reload.sh): deploy script · Installs or reloads the brew LaunchAgent and prepares runtime permissions/logs · `reload.sh`
- [Reload restart](https://github.com/suryakiran434/brewautomation/blob/main/reload_restart.sh): deploy script · Installs or reloads the restart LaunchDaemon and its supporting files · `reload_restart.sh`
- [TZ reload](https://github.com/suryakiran434/brewautomation/blob/main/tzreload.sh): watcher script · Timezone coordination script that triggers a reload when local time changes · `tzreload.sh`
- **Local state**: filesystem state · Repository-local locks, completion markers, and logs used for idempotency and observability

### External

Runtime tools and services the repo depends on

- **launchd**: macOS scheduler · Native scheduling runtime that starts agents and daemons
- **Homebrew**: package manager · System package and cask upgrade target
- **uv**: Python toolchain · Used to upgrade uv tools and Python packages
- **Python 3**: runtime · Interpreter used for the email client and package resolution
- **Gmail SMTP**: mail service · Outbound email transport used by the notifier
- **iTerm2**: optional terminal · Optional interactive execution environment for the update flow
- **pyenv**: optional resolver · Optional Python interpreter resolution fallback
- **shutdown**: system command · Privileged restart action invoked by the reboot script

## Connections

- launchd → Brew agent: starts · launchd schedules the daily brew agent
- launchd → Restart daemon: starts · launchd schedules the reboot daemon
- launchd → TZ watcher: starts · launchd runs the timezone watcher
- Brew agent → Run guard: invokes · the agent triggers the duplicate-prevention guard
- Run guard → Update exec: handoff · allowed runs continue into the execution stage
- Update exec → Homebrew: updates · performs brew update and upgrade operations
- Update exec → uv: updates · upgrades uv tools and Python packages
- Update exec → Python 3: runs under · uses a resolved Python interpreter for package management and notifications
- Update exec → iTerm2: uses · prefers iTerm2 for interactive execution when available
- Update exec → pyenv: fallback · can resolve Python through pyenv when needed
- Run guard → Local state: reads/writes · lock files and daily completion state live in the repository-local filesystem
- Update exec → Local state: logs · execution emits dedicated run logs and error output
- Update exec → Email service: reports · success and failure notifications are sent after execution
- Restart daemon → Reboot policy: invokes · the daemon triggers the reboot policy wrapper
- Reboot policy → shutdown: executes · the script performs the actual system reboot
- Reboot policy → Email service: reports · restart completion and failures are emailed through the shared notifier
- Reboot policy → Local state: logs · manual vs scheduled runs and restart history are recorded locally
- TZ watcher → TZ reload: invokes · the watcher script is polled by launchd
- TZ reload → Reload brew: reloads · timezone changes cause the brew agent to be refreshed
- Reload brew → Brew agent: loads · deployment path installs or reloads the brew LaunchAgent
- Reload restart → Restart daemon: loads · deployment path installs or reloads the restart LaunchDaemon
- TZ reload → Local state: tracks · timezone coordination persists local watcher state
- Email service → Gmail SMTP: sends via · email delivery goes through Gmail SMTP
- Email service → Python 3: runs on · the notifier is a Python program
