Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions back2base-container/defaults/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"_back2base_schema": 5,
"syncClaudeAiPlugins": false,
"syncClaudeAiSkills": false,
"statusLine": {
"type": "command",
"command": "bash -c 'export COLUMNS=\"${COLUMNS:-120}\"; exec node /opt/back2base/claude-hud/index.js --extra-cmd /opt/back2base/statusline-extra.sh'",
Expand Down
39 changes: 26 additions & 13 deletions back2base-container/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ else
. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib/status.sh"
fi

# Verbose-only logger. ALWAYS returns 0 so it is safe as the last command of
# a function. The inline idiom
# [ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo "..."
# returns 1 when verbose is off; as a function's final command that return
# value propagates and, under the top-level `set -e`, aborts the entrypoint
# mid-startup. Route every verbose detail line through here instead.
_vlog() {
if [ "${BACK2BASE_VERBOSE:-0}" = "1" ]; then
echo "$@"
fi
return 0
}

# Once any phase fails, demote every subsequent phase to chatty mode so
# debugging output isn't lost behind the spinner.
_FANCY_FAILED=0
Expand Down Expand Up @@ -108,7 +121,7 @@ if [ -d /repos ]; then
REPO_DIR=$(find /repos -maxdepth 1 -mindepth 1 -type d | head -1)
if [ -n "$REPO_DIR" ]; then
cd "$REPO_DIR"
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: Working directory: $REPO_DIR"
_vlog ":: Working directory: $REPO_DIR"
fi
fi

Expand All @@ -134,7 +147,7 @@ if [ -z "${MEMORY_NAMESPACE:-}" ] && [ -d /workspace ]; then
fi
fi
export MEMORY_NAMESPACE
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: Memory namespace: $MEMORY_NAMESPACE"
_vlog ":: Memory namespace: $MEMORY_NAMESPACE"
fi

_phase2_workspace_setup() {
Expand Down Expand Up @@ -225,7 +238,7 @@ seed_if_missing() {
if [ ! -e "$target_path" ] && [ -e "$default_path" ]; then
mkdir -p "$(dirname "$target_path")"
cp "$default_path" "$target_path"
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: seeded $target_path from defaults"
_vlog ":: seeded $target_path from defaults"
fi
}

Expand Down Expand Up @@ -259,7 +272,7 @@ filter_mcp_by_profile() {
# ── Explicit profile (unchanged behavior) ──
profile="$BACK2BASE_PROFILE"
if [ "$profile" = "full" ]; then
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: MCP profile: full (all servers)"
_vlog ":: MCP profile: full (all servers)"
return 0
fi
if [ ! -f "$profiles_file" ] || [ ! -f "$mcp_file" ] || ! command -v jq &>/dev/null; then
Expand Down Expand Up @@ -298,7 +311,7 @@ filter_mcp_by_profile() {
mv -f "$tmp" "$mcp_file"
local count
count=$(jq '.mcpServers | length' "$mcp_file")
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: MCP profile: $profile ($count servers enabled)"
_vlog ":: MCP profile: $profile ($count servers enabled)"
else
rm -f "$tmp"
echo ":: ⚠ MCP filtering failed, keeping full server set"
Expand Down Expand Up @@ -330,7 +343,7 @@ pull_mcp_images() {
' "$mcp_file" 2>/dev/null | sort -u)

if [ -z "$images" ]; then
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: No Docker-based MCP servers in profile, skipping image pulls"
_vlog ":: No Docker-based MCP servers in profile, skipping image pulls"
return 0
fi

Expand All @@ -344,16 +357,16 @@ pull_mcp_images() {
done <<< "$images"

if [ -z "$missing" ]; then
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: MCP images already present, skipping pulls"
_vlog ":: MCP images already present, skipping pulls"
return 0
fi

[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: Pulling missing MCP server images..."
_vlog ":: Pulling missing MCP server images..."
for img in $missing; do
docker pull -q "$img" &
done
wait
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: MCP images up to date"
_vlog ":: MCP images up to date"
}

_phase "Pulling MCP server images" pull_mcp_images
Expand All @@ -370,7 +383,7 @@ seed_skills_if_missing() {
# outer if already verified the path is NOT a populated dir.
rm -rf "$HOME/.claude/skills"
cp -RL /opt/back2base/defaults/skills "$HOME/.claude/skills"
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: seeded $HOME/.claude/skills from image defaults"
_vlog ":: seeded $HOME/.claude/skills from image defaults"
fi
fi
}
Expand All @@ -383,7 +396,7 @@ seed_agents_if_missing() {
if [ -d "$defaults" ]; then
rm -rf "$HOME/.claude/agents"
cp -RL "$defaults" "$HOME/.claude/agents"
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: seeded $HOME/.claude/agents from image defaults"
_vlog ":: seeded $HOME/.claude/agents from image defaults"
fi
fi
}
Expand All @@ -395,7 +408,7 @@ seed_commands_if_missing() {
if [ -d /opt/back2base/defaults/commands ]; then
rm -rf "$HOME/.claude/commands"
cp -RL /opt/back2base/defaults/commands "$HOME/.claude/commands"
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: seeded $HOME/.claude/commands from image defaults"
_vlog ":: seeded $HOME/.claude/commands from image defaults"
fi
fi
}
Expand All @@ -412,7 +425,7 @@ seed_plugins_if_missing() {
if [ -d /opt/back2base/defaults/plugins ]; then
rm -rf "$HOME/.claude/plugins"
cp -RL /opt/back2base/defaults/plugins "$HOME/.claude/plugins"
[ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo ":: seeded $HOME/.claude/plugins from image defaults"
_vlog ":: seeded $HOME/.claude/plugins from image defaults"
fi
fi
}
Expand Down
28 changes: 28 additions & 0 deletions back2base-container/lib/prelaunch-overview.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ run_prelaunch_overview() {
return 0
fi

# Content-address the overview by the repo's HEAD tree hash. The overview
# output is non-deterministic, so regenerating it every start rewrites
# CLAUDE.md and makes the whole system prompt cache-cold. ~/.claude is a
# persistent host mount, so the stored hash survives container restarts.
# Known limitation: uncommitted worktree changes don't alter HEAD^{tree},
# so dirty-only changes won't regenerate — acceptable for a coarse overview.
local tree_hash hash_file
hash_file="$HOME/.claude/.overview-tree-hash"
tree_hash=$(git -C "$PWD" rev-parse 'HEAD^{tree}' 2>/dev/null || echo "")
if [ -n "$tree_hash" ] && [ -f "$hash_file" ] \
&& [ "$(cat "$hash_file" 2>/dev/null)" = "$tree_hash" ] \
&& grep -q 'back2base:overview-begin' "$claude_md" 2>/dev/null; then
echo ":: overview: repo unchanged (tree ${tree_hash:0:8}…) — keeping existing overview" >&2
return 0
Comment on lines +50 to +54

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the cached overview across startup regeneration

On normal default/auto-profile launches, this cache never hits: filter_mcp_by_profile in entrypoint.sh replaces .mcp.json on every start, making it newer than CLAUDE.md. The subsequent generate_claude_md invokes render-claude-md.py, whose mtime-based invalidation rebuilds the file from the template and removes the overview markers before this check runs. Consequently, an unchanged repository still incurs the Claude overview call and nondeterministic prompt rewrite every launch. Preserve the overview during base-file regeneration, or cache its body separately and splice it back without invoking Claude.

Useful? React with 👍 / 👎.

fi

local prompt
prompt="$(cat "$prompt_path")"

Expand All @@ -61,7 +77,14 @@ run_prelaunch_overview() {
fmt="json"
fi

# --no-session-persistence: a one-shot scan is not a session; without it every
# launch with overview on saves one and buries real sessions in /resume.
# --setting-sources user: the scan runs in /workspace, the host repo, whose
# .claude/settings.json can carry host-only hooks that fail here with "not
# found". Same flag as the interactive launch (compose.go).
claude -p "$prompt" \
--no-session-persistence \
--setting-sources user \
--output-format "$fmt" \
--model claude-haiku-4-5 \
--allowed-tools "Read,Glob,Grep,Bash" \
Expand Down Expand Up @@ -150,6 +173,11 @@ run_prelaunch_overview() {
return 0
fi

# Stamp the tree hash so the next start with an unchanged repo skips.
if [ -n "$tree_hash" ]; then
echo "$tree_hash" > "$hash_file" 2>/dev/null || true
fi

local lines
lines=$(wc -l < "$tmp_out" | tr -d ' ')
echo ":: overview: ${lines} lines, ${elapsed}s" >&2
Expand Down
6 changes: 6 additions & 0 deletions back2base-container/lib/render-hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ def render(settings_path, manifest_path):
return

d["hooks"] = hooks_block
# Claude Code syncs every plugin and skill enabled on the signed-in
# claude.ai account into the session. A plugin registered from the host
# machine would then run here with the host's absolute hook paths (exit
# 127), so both syncs are pinned off on every boot, not seeded once.
d["syncClaudeAiPlugins"] = False
d["syncClaudeAiSkills"] = False

# Atomic write — same pattern as migrate-settings.py.
try:
Expand Down
99 changes: 99 additions & 0 deletions back2base-container/test/entrypoint-phase-exit.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env bats
#
# Regression for the v0.38.2 "container exits the moment setup finishes" bug.
#
# Several phase functions ended with a bare verbose guard:
# [ "${BACK2BASE_VERBOSE:-0}" = "1" ] && echo "..."
# When BACK2BASE_VERBOSE is unset (the normal interactive launch) the test is
# false, the && short-circuits, and the compound command returns 1. As the
# LAST command of a function, that becomes the function's return value. The
# function is dispatched by _phase under the top-level `set -e`, so the call —
# a simple command returning 1 — trips set -e and aborts entrypoint.sh right
# after the previous phase's ✓. The container dies before `exec claude`.
#
# (At the top level `false && echo` is exempt from set -e, which is why the
# bug only manifested through a function call, not the inline guard itself.)
#
# Fix: route all verbose logging through _vlog, which always returns 0, and
# forbid inline verbose guards from creeping back in.

setup() {
REPO_DIR="$(cd "$BATS_TEST_DIRNAME/.." && pwd)"
ENTRYPOINT="$REPO_DIR/entrypoint.sh"
}

# Print the definition of a shell function from entrypoint.sh (sed range).
extract_fn() {
sed -n "/^$1()/,/^}/p" "$ENTRYPOINT"
}

@test "_vlog is defined in entrypoint.sh" {
run extract_fn _vlog
[ -n "$output" ]
}

@test "_vlog returns 0 with verbose off (safe as a function's last command)" {
fn="$(extract_fn _vlog)"
run bash -c "
set -e
unset BACK2BASE_VERBOSE
$fn
_vlog ':: some detail'
echo RC=\$?
"
[ "$status" -eq 0 ]
[[ "$output" == *"RC=0"* ]]
}

@test "_vlog still prints when verbose on" {
fn="$(extract_fn _vlog)"
run bash -c "
set -e
export BACK2BASE_VERBOSE=1
$fn
_vlog ':: visible detail'
"
[ "$status" -eq 0 ]
[[ "$output" == *"visible detail"* ]]
}

@test "_vlog is silent when verbose off" {
fn="$(extract_fn _vlog)"
run bash -c "
set -e
unset BACK2BASE_VERBOSE
$fn
_vlog ':: hidden detail'
echo DONE
"
[ "$status" -eq 0 ]
[[ "$output" != *"hidden detail"* ]]
[[ "$output" == *"DONE"* ]]
}

@test "a function ending in _vlog returns 0 under set -e with verbose off" {
vlog="$(extract_fn _vlog)"
run bash -c "
set -e
unset BACK2BASE_VERBOSE
$vlog
some_phase() {
local x=1
_vlog ':: tail detail'
}
some_phase
echo SURVIVED
"
[ "$status" -eq 0 ]
[[ "$output" == *"SURVIVED"* ]]
}

@test "no inline BACK2BASE_VERBOSE && guards remain (use _vlog instead)" {
# A bare `[ ... BACK2BASE_VERBOSE ... ] && echo` is the footgun. After the
# fix every such line must be gone — verbose logging goes through _vlog,
# which is exit-status-safe in any position.
# Exclude comment lines (the _vlog doc block intentionally shows the old
# idiom as the anti-pattern to avoid).
run grep -nE '^[^#]*\[ "\$\{BACK2BASE_VERBOSE:-0\}" = "1" \][[:space:]]*&&' "$ENTRYPOINT"
[ -z "$output" ]
}
39 changes: 39 additions & 0 deletions back2base-container/test/hooks-manifest.bats
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,42 @@ EOF
[ "$status" -eq 0 ]
diff -q "$SETTINGS" "$TEST_TMP/before.json"
}

# ── Account-synced plugins and skills are pinned off ─────────────────
# Claude Code syncs every plugin and skill enabled on the signed-in claude.ai
# account into the session; a plugin registered from the host would run in the
# container with the host's absolute hook paths.

@test "renderer: pins syncClaudeAiPlugins and syncClaudeAiSkills false when absent" {
seed_minimal
python3 "$RENDERER" "$SETTINGS" --manifest "$MANIFEST"
run python3 -c "
import json
s = json.load(open('$SETTINGS'))
print(s['syncClaudeAiPlugins'], s['syncClaudeAiSkills'])
"
[ "$status" -eq 0 ]
[ "$output" = "False False" ]
}

@test "renderer: overwrites syncClaudeAiPlugins/Skills true back to false" {
echo '{"_back2base_schema": 5, "syncClaudeAiPlugins": true, "syncClaudeAiSkills": true}' > "$SETTINGS"
python3 "$RENDERER" "$SETTINGS" --manifest "$MANIFEST"
run python3 -c "
import json
s = json.load(open('$SETTINGS'))
print(s['syncClaudeAiPlugins'], s['syncClaudeAiSkills'])
"
[ "$status" -eq 0 ]
[ "$output" = "False False" ]
}

@test "defaults/settings.json ships syncClaudeAiPlugins/Skills false (first boot)" {
run python3 -c "
import json
s = json.load(open('$BATS_TEST_DIRNAME/../defaults/settings.json'))
print(s['syncClaudeAiPlugins'], s['syncClaudeAiSkills'])
"
[ "$status" -eq 0 ]
[ "$output" = "False False" ]
}
Loading
Loading