ποΈ ZSHAND ArchitectureΒΆ
π― This document describes how the framework is structured, how it boots, how bundles work, and how user configuration integrates. Read this first if you're new to the codebase.
!!! abstract "π§ Design Principles" - β‘ Performance first β target <50ms cold start, <10ms warm - π§© Modular β every directory is independently compilable - π Override-friendly β users can replace any framework file - π‘οΈ Fail-safe β graceful degradation at every layer - π Convention-driven β numbered prefixes enforce load order
π Directory StructureΒΆ
zshand/
βββ π― zshrc.zsh Entry point (sourced by ~/.zshrc)
βββ π― setup.zsh First-time bootstrap & OS setup
βββ π¦ build/ Build system & profiling tools
β βββ compile_full_bundle.zsh Full bundle compiler
β βββ compile_profile_bundle.zsh Profiling-instrumented compiler
β βββ profile_bundle_report.zsh Performance report generator
β βββ ... Benchmarking, optimization, loading
βββ βοΈ core/ Framework core (numbered 02β90)
β βββ 02_vars.zsh Variables & identity (FIRST loaded)
β βββ 04_path.zsh PATH construction
β βββ 06_engine.zsh Execution engine (zprime, zrun)
β βββ 08_audit.zsh System integrity & telemetry
β βββ 10_options.zsh Zsh options (setopt)
β βββ 12_completions.zsh Completion system
β βββ 14_aliases.zsh Shell aliases
β βββ 16_widgets.zsh ZLE widget registration
β βββ 18_hooks.zsh Hook classification & loading
β βββ 20_plugins.zsh Plugin management
β βββ 90_functions.zsh Function autoloading
βββ π§± shared_functions/ Low-level utilities (01β18)
β βββ 01_stderr_error.zsh Error output to stderr
β βββ 06_time_millis.zsh Millisecond timing
β βββ 11_source_with_zwc.zsh Bytecode-aware sourcing
β βββ 15_load_file_timed.zsh Timed file loading with budget
β βββ 16_zrun.zsh Telemetry logging
β βββ 17_clipboard_copy.zsh Wayland clipboard abstraction
β βββ ... Assertions, debug helpers, cache
βββ π startup/ Early startup scripts (05β28)
β βββ 05_az_dev_mode_check.zsh Auto-rebuild in dev mode
β βββ 21_az_health_validate_tools.zsh Dependency validation
β βββ 24_az_safe_mode.zsh Emergency recovery
β βββ ... TOML config, keybindings, profiling
βββ π οΈ functions/ User-facing shell functions
β βββ dcopy.zsh Copy directory path to clipboard
β βββ zopt.zsh Zsh option management
β βββ ... ~25 utility functions
βββ β¨οΈ widgets/ ZLE widgets (keybinding-driven)
β βββ aicmit.zsh AI-assisted git commit
β βββ aidebug.zsh AI debugging assistant
β βββ ... ~20 interactive widgets
βββ πͺ¨ hooks/ Tool integration hooks (01β81)
β βββ 01_mise.zsh Polyglot runtime manager
β βββ 02_atuin.zsh Shell history sync
β βββ 05_ssh-agent.zsh SSH agent management
β βββ ... Docker, zoxide, git identity
βββ π bin/ Executable CLI tools (on $PATH)
β βββ aicontext AI context generator
β βββ zr Framework rebuild shortcut
β βββ ... ~30 standalone scripts
βββ π private_functions/ Internal framework functions
βββ π lib/ Library support modules
β βββ rebuild/ Incremental rebuild logic
βββ βοΈ config/ Framework-level configuration
β βββ profile-exceptions.conf Performance exception rules
βββ π₯ installers/ OS-specific system installers
βββ π§ͺ tests/ ShellSpec test suites
βββ π docs/ Documentation (this file lives here)
βββ π samples/ Example user config templates
π Numbering ConventionΒΆ
Files are numbered with 2-digit prefixes to enforce deterministic load order:
| Range | Purpose | Example |
|---|---|---|
01β09 | π§± Foundation / bootstrap | 02_vars.zsh, 01_mise.zsh |
10β19 | βοΈ Core configuration | 10_options.zsh, 14_aliases.zsh |
20β29 | π Plugins & integrations | 20_plugins.zsh, 20_docker.zsh |
30β49 | π οΈ Mid-priority tools | 30_zoxide.zsh |
50β79 | π¦ Optional / less critical | 50_gitid.zsh |
80β89 | π§Ή Cleanup & maintenance | 80_archclean.zsh |
90β99 | π Late-stage / finalization | 90_functions.zsh |
π¬ Even numbers are used by the framework. Odd numbers are reserved for user insertion (e.g., user can place
03_my_vars.zshinrc.d/).
π Boot SequenceΒΆ
The boot process is orchestrated by zshrc.zsh and follows a strict phase order. Multiple loading strategies exist depending on the mode:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β zshrc.zsh Entry Point β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β 0. π« Non-interactive check (early exit for scripts/cron) β
β 1. π Path & cache initialization β
β 2. π Loading strategy selection (see below) β
β 3. π¨ P10k instant prompt (zero-latency visual) β
β 4. π‘οΈ Safe mode check (emergency recovery) β
β 5. β
Health validation (system dependencies) β
β 6. βοΈ Core config loading (02β04β06β08β...β90) β
β 7. πͺ¨ Hook integration (critical sync, then lazy/deferred) β
β 8. π Staleness check & auto-recompile β
β 9. π€ User post.d/ scripts β
β 10. π Debug summary & cleanup β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π Loading StrategiesΒΆ
The framework picks the fastest available strategy at boot:
| Priority | Strategy | Trigger | Speed | Use Case |
|---|---|---|---|---|
| 1οΈβ£ | Profile Bundle | ZSHAND_PROFILE_BUNDLE=1 | ~100ms | Performance analysis |
| 2οΈβ£ | Full Bundle (fast path) | ZSHAND_AUTO_FULL=1 + bundle exists | β‘ <50ms | Production (fastest) |
| 3οΈβ£ | Full Bundle (auto-compile) | ZSHAND_AUTO_FULL=1 + no bundle | ~200ms first, then β‘ | First run |
| 4οΈβ£ | Monolithic Bundle | ZSHAND_USE_MONOLITHIC=1 | ~60ms | Framework-only bundle |
| 5οΈβ£ | Hybrid | Production default | ~80ms | Pre-compiled per-dir bundles |
| 6οΈβ£ | Individual Files | ZSHAND_DEV_MODE=1 or fallback | ~200ms | Development / debugging |
Which mode am I in?
Check echo $ZSHAND_DEV_MODE and echo $ZSHAND_AUTO_FULL. In production, the fast path loads a single .zsh file from cache β everything in one read.
π¦ Bundle SystemΒΆ
Bundles are the core performance optimization. The build system concatenates framework + user files into a single source file, then zcompiles it to .zwc bytecode for instant loading.
π Bundle TypesΒΆ
| Bundle | File | Contains | Built By |
|---|---|---|---|
| ποΈ Full | zshand-full.zsh / .zwc | Framework + user config | compile_full_bundle.zsh |
| π Profile | zshand-profile.zsh / .zwc | Full + timing instrumentation | compile_profile_bundle.zsh |
| π Per-directory | <dir>/.compiled/<dir>-all.zsh | Single directory contents | compile_single_directory.zsh |
π Bundle Compilation FlowΒΆ
Source Files βββ Merge (user overrides) βββ Concatenate βββ .zsh βββ zcompile βββ .zwc
β β
User config dir files Bytecode (instant load)
override framework files
with same basename
β‘ Compilation CommandsΒΆ
| Command | What It Does |
|---|---|
zprime | Compile all directories + reload shell |
zprime --quiet / zrq | Background compilation (no output) |
zprime --full / zfull | Compile full bundle |
zprime-dir <dir> | Rebuild single directory |
zr | Alias for zprime (convenience) |
Bundle staleness
Bundles are checked for staleness at boot. If source files are newer than the compiled bundle, you'll see a warning. Run zprime to rebuild.
π Override SystemΒΆ
Users can override any framework file by placing a file with the same basename in their config directory. The merge resolution is:
Framework: $ZSHAND/core/14_aliases.zsh
User: $ZSHAND_CONFIG_DIR/rc.d/14_aliases.zsh β wins (replaces framework)
π User Config DirectoryΒΆ
~/.config/zshand/ ($ZSHAND_CONFIG_DIR)
βββ env.zsh π Environment variables (auto-exported)
βββ secrets.zsh π API keys and tokens (chmod 600)
βββ aliases.zsh π Custom aliases
βββ path.txt π€οΈ Additional PATH entries (one per line)
βββ config.toml βοΈ Framework behavior settings
βββ bin/ π Personal scripts (added to $PATH)
βββ functions/ π οΈ Custom functions (override or extend)
βββ widgets/ β¨οΈ Custom ZLE widgets
βββ hooks/ πͺ¨ Custom integration hooks
βββ rc.d/ βοΈ Core config overrides (same numbering as core/)
βββ init.d/ π Pre-initialization (before framework loads)
βββ post.d/ π Post-initialization (after everything)
π User File Load OrderΒΆ
1. init.d/*.zsh Before framework (early overrides)
2. env.zsh + secrets.zsh Environment variables
3. path.txt + bin/ PATH configuration
4. Framework core (merged with rc.d/ overrides)
5. Framework hooks, functions, widgets
6. User hooks/, functions/, widgets/
7. aliases.zsh Custom aliases (can override framework)
8. post.d/*.zsh Final tweaks
βοΈ Key SubsystemsΒΆ
π§ Engine (core/06_engine.zsh)ΒΆ
The engine is the largest and most critical core module. It provides:
zprimeβ Framework compilation and shell reloadzrunβ Telemetry-aware command execution wrapper_z_system_initβ System initialization (Atuin, security, terminal title)- Incremental rebuilds β Only recompile changed directories
- Completion generation β Atomic completion for external tools
π§± Shared Functions (shared_functions/)ΒΆ
These are the lowest-level building blocks, loaded before everything else. They provide the primitives that all other code depends on:
| Function | Purpose |
|---|---|
stderr_error / stderr_warn | Colored error/warning output |
echo_info / echo_ok | Status messages |
time_millis | Millisecond-precision timing |
assert_dir_exists / assert_file_exists / assert_function_exists | Guard assertions |
source_with_zwc | Bytecode-aware file sourcing |
load_file_timed | Source file with timing + performance budget |
_zrun | Low-level telemetry logging |
clipboard_copy / clipboard_paste | Wayland clipboard abstraction |
π‘οΈ Safe Mode (startup/24_az_safe_mode.zsh)ΒΆ
Emergency recovery when the framework fails to initialize:
- Triggered by
ZSHAND_SAFE_MODE=1 exec zshor auto-detected via failure count - Provides minimal shell with basic PATH and no framework
- Recovery menu for diagnostics and repair
π Profiling PipelineΒΆ
ZSHAND_PROFILE_BUNDLE=1 exec zsh β Start profiled shell
β
βΌ
compile_profile_bundle.zsh β Builds instrumented bundle
β
βΌ
profile-timing.log β Raw timing data (TYPE|NAME|START|END)
β
βΌ
profile_bundle_report.zsh β Generates graded performance report
β
βΌ
Terminal output with grades AβF β Human-readable with bar charts
πͺ¨ Hook ClassificationΒΆ
Hooks are classified by core/18_hooks.zsh into two categories:
| Type | Loading | Purpose | Examples |
|---|---|---|---|
| β‘ Critical | Synchronous (immediate) | Must be ready at first prompt | 01_mise, 02_atuin, 05_ssh-agent |
| π Lazy | Deferred (1s delay) or sync | Can wait until after prompt | 20_docker, 30_zoxide, 50_gitid |
When ZSHAND_LAZY_LOAD=1, non-critical hooks load in a background subshell after a 1-second delay, keeping the prompt responsive.
π§ͺ Testing ArchitectureΒΆ
tests/
βββ run_all.zsh Master test runner
βββ bootstrap_env.zsh Test environment setup
βββ bin/ bin/* tool tests
βββ build/ Build system tests
βββ core/ Core module tests
βββ dependencies/ Dependency validation tests
- Framework: ShellSpec (BDD-style
Describe/Itblocks) - Coverage: Kcov via CI
- Runner:
just testorjust test spec/path/ - Spec mirroring:
functions/dcopy.zshβspec/functions/dcopy_spec.sh
π Environment VariablesΒΆ
ποΈ Framework ControlsΒΆ
| Variable | Type | Default | Purpose |
|---|---|---|---|
ZSHAND | string | auto-detected | Framework root directory |
ZSHAND_CONFIG_DIR | string | $XDG_CONFIG_HOME/zshand | User config directory |
ZSHAND_CACHE_DIR | string | $XDG_CACHE_HOME/zshand | Cache & compiled bundles |
ZSHAND_LOG_DIR | string | $HOME/logs/zshand | Log file directory |
ZSHAND_DEV_MODE | bool | 0 | Enable development mode |
ZSHAND_AUTO_FULL | bool | 0 | Auto-compile full bundle |
ZSHAND_DEBUG | bool | 0 | Verbose debug output |
ZSHAND_PROFILE_BUNDLE | bool | 0 | Enable profiling |
ZSHAND_LAZY_LOAD | bool | 0 | Defer non-critical hooks |
ZSHAND_SAFE_MODE | bool | 0 | Emergency recovery mode |
ZSHAND_AUDIT_MODE | bool | 0 | Full audit (recompile + test) |
ZSHAND_PERF_BUDGET | int | 100 | Max ms per file before warning |
π XDG ComplianceΒΆ
The framework follows the XDG Base Directory Specification:
| XDG Variable | ZSHAND Maps To | Default |
|---|---|---|
XDG_CONFIG_HOME | ZSHAND_CONFIG_DIR | ~/.config/zshand |
XDG_CACHE_HOME | ZSHAND_CACHE_DIR | ~/.cache/zshand |
XDG_DATA_HOME | β | (not currently used) |
π Dependency GraphΒΆ
zshrc.zsh
βββ shared_functions/* (loaded FIRST β primitives for everything)
βββ startup/* (loaded with shared_functions, alphabetically merged)
βββ build/detect_dev_mode.zsh (if not using bundle)
βββ core/02_vars.zsh β no deps (self-sufficient bootstrap)
β βββ core/04_path.zsh β depends on 02_vars (ZSHAND, config paths)
β βββ core/06_engine.zsh β depends on 02, 04 (paths, vars)
β βββ core/08_audit.zsh β depends on 02, 06 (LOG_DIR, _zrun)
β βββ core/10_options.zsh β 12 β 14 β 16 β 18 β 20 β 90
βββ hooks/* (after core; classified by 18_hooks.zsh)
βββ P10k theme (after bundles, before user config)
βββ post.d/* (last β user final overrides)
Circular dependency prevention
shared_functions/ and startup/ are loaded before any core/ files. Core files load in strict numeric order. No file may depend on a higher-numbered file in the same directory.
π Related DocumentsΒΆ
| Document | Purpose |
|---|---|
| π HEADER_STANDARD.md | Documentation header conventions |
| π README.md | Project overview and quick start |
| ποΈ build/README.md | Build system documentation |
| π samples/user-config/README.md | Example user configuration |