Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

Pack Installation Failing After #260 Fix - Environment Variable Sourcing Incomplete

Open fayerman-source opened this issue 11 months ago • 1 comments

Issue: Pack Installation Still Failing - #260 Fix Incomplete

Summary

Following up on Daniel's fix for issue #260 (commit 197f082, Dec 29): The environment variable sourcing and pack installation still fails despite the attempted fix. The .env file is created correctly, but Claude Code doesn't read the variables during pack installation, causing pack installation to fail with validation errors.

Environment

  • Platform: WSL2 Ubuntu 22.04 LTS
  • Node Runtime: Bun
  • Claude Code: Latest Pro
  • PAI Version: Latest (as of Dec 30, 20:58 UTC)
  • Commits: Based on current HEAD including 197f082 fix

What Daniel Fixed (Dec 29 - Commit 197f082)

Issue #260 addressed environment variable sourcing between wizard and packs:

// Create .env file for packs to read
const envFileContent = `DA="${config.daName}" ...`
await Bun.write(`${paiDir}/.env`, envFileContent);

// Export to current process for immediate use
process.env.DA = config.daName;
process.env.TIME_ZONE = config.timeZone;

The fix assumed: Variables would be available to Claude Code during pack installation.


The Problem Still Exists

Fresh installation (PAI4) completed successfully with .env file properly created:

✓ Created /home/elidev/.claude/.env

File contents verified:
DA="Pai4"
TIME_ZONE="America/New_York"
ELEVENLABS_API_KEY="[set]"
ELEVENLABS_VOICE_ID="[set]"

However, when Claude Code attempts to install kai-hook-system.md, it checks environment variables and finds them unset:

Environment variables (checked during pack installation):
  DA: Pai               # Expected: Pai4 (missing the "4")
  TIME_ZONE: 'NOT SET'  # Expected: America/New_York
  PAI_SOURCE_APP: 'NOT SET'

Root Cause Analysis

The .env file is created correctly in ~/.claude/.env, but:

  1. It's not being read by Claude Code — The file exists but Claude Code doesn't load it into its environment
  2. Process export in install.ts only affects the installer process — It doesn't propagate to Claude Code's session
  3. No mechanism loads .env into Claude Code — The fix creates the file but not the sourcing mechanism

The Gap in #260's Fix

The fix created .env but assumed it would be:

  • Automatically sourced into Claude Code's environment
  • Available during pack installation

Neither assumption holds true. Claude Code needs explicit sourcing of .env.


Attempted Pack Installation Failure

When user provides kai-hook-system.md to Claude Code with instruction "Install this pack into my system":

● Bash(echo "Environment variables:" && echo "  DA: ${DA:-'NOT SET'}" ...)
  ⎿  Environment variables:
       DA: Pai                    # Missing "4"
       TIME_ZONE: 'NOT SET'       # Should be America/New_York
       PAI_SOURCE_APP: 'NOT SET'

● Write(~/.claude/hooks/update-tab-titles.ts)
  ⎿  User rejected write to ../.claude/hooks/update-tab-titles.ts

The write rejection happens because:

  1. Claude Code can't find required environment variables
  2. Validation/pre-commit hook rejects incomplete configuration
  3. Pack installation halts before any hooks are created

Installation Flow Issues

The complete picture:

  1. ✅ Kai wizard creates .env file correctly
  2. ❌ Wizard doesn't source .env into Claude Code's runtime
  3. ✅ .env file exists with correct values
  4. ❌ Claude Code can't access those values during session
  5. ❌ Pack installation fails due to missing environment
  6. ❌ Chicken-and-egg: Can't install hooks that would handle this sourcing

What Needs to Happen

Option 1: Source .env in Wizard

After creating .env, the wizard should ensure Claude Code loads it:

# In Bundles/Kai/install.ts
await Bun.write(`${paiDir}/.env`, envFileContent);

// Also update .bashrc to source .env at session start
const bashrcEntry = `[ -f "$HOME/.claude/.env" ] && export $(cat "$HOME/.claude/.env" | xargs)`;

Option 2: Have kai-core-install Bootstrap Environment

The kai-core-install.md pack could handle environment setup as part of installation.

Option 3: Create settings.json During Wizard

Instead of relying on .env sourcing, create settings.json with configuration that Claude Code reads directly.


Test Reproduction

# Fresh WSL2 Ubuntu
git clone https://github.com/danielmiessler/PAI.git PAI-test
cd PAI-test/Bundles/Kai
bun run install.ts

# Respond: Eli, Pai4, America/New_York, No voice

# Verify .env exists
cat ~/.claude/.env
# Should show: DA="Pai4", TIME_ZONE="America/New_York"

# Now try to install pack in Claude Code
# cat ~/PAI-test/Packs/kai-hook-system.md
# [Provide to Claude Code with: "Install this pack into my system"]

# Observe: Environment variables report as unset despite .env file existing

Related Issues

  • #260 - Original environment variable sourcing issue (attempted fix)
  • #259 - Pack installation clarity
  • #261 - Pack manifest documentation

Impact

Severity: HIGH Blocker: Yes — Cannot proceed with pack installation after base setup Affected: All users following Quick Start instructions for fresh installations Workaround: None that maintains proper configuration


Questions

  1. Is .env supposed to be sourced automatically into Claude Code's session?
  2. Should the wizard add sourcing logic to .bashrc or shell profile?
  3. Should settings.json be created during wizard with configuration, rather than relying on environment variable sourcing?
  4. Is there a hook that should run early in session initialization to load .env?

Additional Context

This is post-fix testing on the same codebase Daniel committed to 2 days ago. The issue persists despite the attempted fix, suggesting the sourcing mechanism isn't complete or something else is preventing environment variable propagation to Claude Code.

Testing done on WSL2 provides a clean environment for validation.


Files for Reference

  • Installation successful: .env created at ~/.claude/.env with correct values
  • Pack installation attempted: kai-hook-system.md from Packs/
  • Failure point: Claude Code cannot read environment variables despite file existing

fayerman-source avatar Dec 31 '25 02:12 fayerman-source

Fix Implemented ✅

Thank you for the thorough analysis! You correctly identified the root cause: .env and shell profile updates don't propagate to Claude Code's running session.

The Fix

The installer now creates ~/.claude/settings.json with environment variables using Claude Code's native env var support:

{
  "env": {
    "DA": "YourAI",
    "TIME_ZONE": "Your/Timezone",
    "PAI_DIR": "~/.claude",
    "PAI_SOURCE_APP": "YourAI"
  }
}

Changes Made

  1. Added settings.json generation - Claude Code reads env vars from this file immediately, no shell sourcing needed
  2. Merge with existing settings - If settings.json already exists (e.g., with hooks), the env vars are merged in
  3. Fixed .env quote formatting - Removed quotes from values (.env format standard)

Why This Works

Claude Code's settings.json supports an env object that sets environment variables for the session. Unlike shell profile sourcing, these are available immediately when Claude Code starts.

For Existing Installations

If you've already run the installer, you can manually add to your ~/.claude/settings.json:

{
  "env": {
    "DA": "Pai4",
    "TIME_ZONE": "America/New_York",
    "PAI_DIR": "/home/elidev/.claude",
    "PAI_SOURCE_APP": "Pai4"
  }
}

Then restart Claude Code.

Thank you for the excellent debugging and proposed solutions - you were spot-on about needing settings.json!

danielmiessler avatar Dec 31 '25 06:12 danielmiessler