clanker-agent/packages/coding-agent/scripts/migrate-sessions.sh
Harivansh Rathi 67168d8289 chore: rebrand companion-os to clanker-agent
- Rename all package names from companion-* to clanker-*
- Update npm scopes from @mariozechner to @harivansh-afk
- Rename config directories .companion -> .clanker
- Rename environment variables COMPANION_* -> CLANKER_*
- Update all documentation, README files, and install scripts
- Rename package directories (companion-channels, companion-grind, companion-teams)
- Update GitHub URLs to harivansh-afk/clanker-agent
- Preserve full git history from companion-cloud monorepo
2026-03-26 16:22:52 -04:00

93 lines
2.2 KiB
Bash
Executable file

#!/usr/bin/env bash
#
# Migrate sessions from ~/.clanker/agent/*.jsonl to proper session directories.
# This fixes sessions created by the bug in v0.30.0 where sessions were
# saved to ~/.clanker/agent/ instead of ~/.clanker/agent/sessions/<encoded-cwd>/.
#
# Usage: ./migrate-sessions.sh [--dry-run]
#
set -e
AGENT_DIR="${CLANKER_AGENT_DIR:-$HOME/.clanker/agent}"
DRY_RUN=false
if [[ "$1" == "--dry-run" ]]; then
DRY_RUN=true
echo "Dry run mode - no files will be moved"
echo
fi
# Find all .jsonl files directly in agent dir (not in subdirectories)
shopt -s nullglob
files=("$AGENT_DIR"/*.jsonl)
shopt -u nullglob
if [[ ${#files[@]} -eq 0 ]]; then
echo "No session files found in $AGENT_DIR"
exit 0
fi
echo "Found ${#files[@]} session file(s) to migrate"
echo
migrated=0
failed=0
for file in "${files[@]}"; do
filename=$(basename "$file")
# Read first line and extract cwd using jq
if ! first_line=$(head -1 "$file" 2>/dev/null); then
echo "SKIP: $filename - cannot read file"
((failed++))
continue
fi
# Parse JSON and extract cwd
if ! cwd=$(echo "$first_line" | jq -r '.cwd // empty' 2>/dev/null); then
echo "SKIP: $filename - invalid JSON"
((failed++))
continue
fi
if [[ -z "$cwd" ]]; then
echo "SKIP: $filename - no cwd in session header"
((failed++))
continue
fi
# Encode cwd: remove leading slash, replace slashes with dashes, wrap with --
encoded=$(echo "$cwd" | sed 's|^/||' | sed 's|[/:\\]|-|g')
encoded="--${encoded}--"
target_dir="$AGENT_DIR/sessions/$encoded"
target_file="$target_dir/$filename"
if [[ -e "$target_file" ]]; then
echo "SKIP: $filename - target already exists"
((failed++))
continue
fi
echo "MIGRATE: $filename"
echo " cwd: $cwd"
echo " to: $target_dir/"
if [[ "$DRY_RUN" == false ]]; then
mkdir -p "$target_dir"
mv "$file" "$target_file"
fi
((migrated++))
echo
done
echo "---"
echo "Migrated: $migrated"
echo "Skipped: $failed"
if [[ "$DRY_RUN" == true && $migrated -gt 0 ]]; then
echo
echo "Run without --dry-run to perform the migration"
fi