95 lines
2.6 KiB
Bash
Executable File
95 lines
2.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
CODEX_HOME="${CODEX_HOME:-$HOME/.codex}"
|
|
STAMP="$(date +%Y%m%d-%H%M%S)"
|
|
BACKUP_DIR="$CODEX_HOME/backups/$STAMP"
|
|
MANAGED_CONFIG="$REPO_ROOT/templates/global/config-managed.toml"
|
|
|
|
mkdir -p "$CODEX_HOME" "$BACKUP_DIR" "$CODEX_HOME/memories"
|
|
|
|
if [[ -f "$CODEX_HOME/config.toml" ]]; then
|
|
cp "$CODEX_HOME/config.toml" "$BACKUP_DIR/config.toml.bak"
|
|
fi
|
|
|
|
if [[ -f "$CODEX_HOME/AGENTS.md" ]]; then
|
|
cp "$CODEX_HOME/AGENTS.md" "$BACKUP_DIR/AGENTS.md.bak"
|
|
fi
|
|
|
|
cp "$REPO_ROOT/templates/global/AGENTS.md" "$CODEX_HOME/AGENTS.md"
|
|
|
|
python3 - "$CODEX_HOME/config.toml" "$MANAGED_CONFIG" <<'PY'
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
config_path = Path(sys.argv[1])
|
|
managed_path = Path(sys.argv[2])
|
|
|
|
managed_start = "# >>> codex-ops-kit managed block >>>"
|
|
managed_end = "# <<< codex-ops-kit managed block <<<"
|
|
managed_root_keys = {
|
|
"model",
|
|
"model_reasoning_effort",
|
|
"profile",
|
|
"project_doc_max_bytes",
|
|
"web_search",
|
|
}
|
|
managed_sections = {
|
|
"history",
|
|
"profiles.economy",
|
|
"profiles.balanced",
|
|
"profiles.deep",
|
|
}
|
|
|
|
text = config_path.read_text() if config_path.exists() else ""
|
|
managed = managed_path.read_text().rstrip()
|
|
|
|
if managed_start in text and managed_end in text:
|
|
before = text.split(managed_start, 1)[0].rstrip()
|
|
else:
|
|
before = text.rstrip()
|
|
|
|
filtered_lines = []
|
|
current_section = None
|
|
|
|
for line in before.splitlines():
|
|
stripped = line.strip()
|
|
if stripped.startswith("[") and stripped.endswith("]"):
|
|
current_section = stripped[1:-1].strip()
|
|
if current_section in managed_sections:
|
|
continue
|
|
filtered_lines.append(line)
|
|
continue
|
|
|
|
if current_section in managed_sections:
|
|
continue
|
|
|
|
if "=" in line and not line.lstrip().startswith("#"):
|
|
key = line.split("=", 1)[0].strip()
|
|
if current_section is None and key in managed_root_keys:
|
|
continue
|
|
|
|
filtered_lines.append(line)
|
|
|
|
before = "\n".join(filtered_lines).strip()
|
|
|
|
block = f"{managed_start}\n{managed}\n{managed_end}\n"
|
|
|
|
if before:
|
|
config_path.write_text(before + "\n\n" + block)
|
|
else:
|
|
config_path.write_text(block)
|
|
PY
|
|
|
|
mkdir -p "$CODEX_HOME/memories/portable-kit"
|
|
cp "$REPO_ROOT"/memory/*.md "$CODEX_HOME/memories/portable-kit/"
|
|
|
|
echo "Installed Codex kit into: $CODEX_HOME"
|
|
echo "Backup created at: $BACKUP_DIR"
|
|
echo
|
|
echo "Next steps:"
|
|
echo "- Start Codex and use the balanced profile by default."
|
|
echo "- Switch to economy for lighter work and deep for hard tasks."
|
|
echo "- Run bash scripts/audit_codex_home.sh periodically."
|