Public AI skill
Codex Chat Archiver
Opening Codex from my phone had started taking minutes. After I archived the old task backlog, it took about 20 seconds on my setup. This skill keeps chats used in the last 48 hours active and archives the rest.
The skill source is ready to copy or download.
Set it up
Run it where Codex runs.
Use the ChatGPT desktop app with local Codex tasks. The computer and app must stay on for a scheduled local run.
ChatGPT desktop appLocal Codex tasks48-hour idle window
Workflow
How the skill works.
01
Find idle chats
Read each local task's timestamp and top-level ID. Leave message content alone.
02
Protect live work
Keep pinned chats, running tasks, approval requests, and the cleanup task itself active.
03
Archive through Codex
Use the supported archive action in small batches. Never delete or move transcript files directly.
04
Check the result
Count archived, protected, failed, and remaining chats without exposing titles or message text.
Result
Minutes became about 20 seconds on my setup.
- On my setup, mobile Codex startup fell from minutes to about 20 seconds after I archived the backlog.
- Chats stay recoverable in Settings under Archived chats.
- A daily schedule can keep the active task list small.
- Results vary with the machine, task count, and Codex version.
Source
The complete SKILL.md.
---
name: archive-idle-codex-chats
description: Archive reversible local Codex chats that have been unused for more than 48 hours while protecting pinned and active work on macOS, Linux, and Windows through WSL. Use when Codex Remote or the desktop task list has become slow, when asked to clean up old Codex sessions, or from a scheduled local maintenance task.
---
# Archive Idle Codex Chats
Keep the active local task index small without deleting chat history. Discover candidates from transcript metadata, then archive them through the Codex app API.
## Safety rules
- Treat `archive`, `clean up`, or an enabled scheduled run as authorization to archive. Treat `check`, `count`, `preview`, or `dry run` as read-only.
- Archive only top-level local chats whose transcript has not changed for more than 48 hours.
- Exclude every pinned chat and every chat that is running, waiting for approval, or otherwise non-terminal.
- Exclude the task executing this workflow.
- Never delete, rename, or manually move files under `$CODEX_HOME/sessions` or `$CODEX_HOME/archived_sessions`.
- Use `codex_app__set_thread_archived` so the operation remains reversible from Settings > Data Controls > Archived chats.
- Stop if the Codex app archive tool is unavailable. Do not imitate it with filesystem changes.
## 1. Resolve the local session directory
Run this read-only Bash check. It supports macOS, Linux, and Codex desktop on Windows with WSL.
```bash
set -o pipefail
platform=$(uname -s)
codex_home="${CODEX_HOME:-$HOME/.codex}"
if [ ! -d "$codex_home/sessions" ] && [ "$platform" = "Linux" ] && command -v powershell.exe >/dev/null 2>&1; then
windows_profile=$(powershell.exe -NoProfile -Command '[Environment]::GetFolderPath("UserProfile")' | tr -d '\r')
if [ -n "$windows_profile" ]; then
windows_codex_home="$(wslpath "$windows_profile")/.codex"
if [ -d "$windows_codex_home/sessions" ]; then
codex_home="$windows_codex_home"
fi
fi
fi
if [ ! -d "$codex_home/sessions" ] && [ "$platform" = "Darwin" ]; then
mac_candidates=()
for candidate in \
"$HOME/Library/Application Support"/*/sessions \
"$HOME/Library/Application Support"/*/*/sessions \
"$HOME/Library/Application Support"/*/*/*/sessions; do
[ -d "$candidate" ] || continue
[ ! -L "$candidate" ] || continue
mac_candidates[${#mac_candidates[@]}]="$candidate"
done
if [ "${#mac_candidates[@]}" -eq 1 ]; then
codex_home=$(dirname "${mac_candidates[0]}")
elif [ "${#mac_candidates[@]}" -gt 1 ]; then
printf 'Ambiguous Codex session directories:\n' >&2
printf ' %s\n' "${mac_candidates[@]}" >&2
exit 1
fi
fi
sessions_dir="$codex_home/sessions"
test -d "$sessions_dir"
printf 'sessions_dir=%s\n' "$sessions_dir"
```
On macOS, the normal location is `~/.codex/sessions`. The bounded `Library/Application Support` search is only a fallback for an app-managed location. Stop if the resolved directory is missing, empty, outside the resolved Codex home, or ambiguous. Do not scan the rest of the home directory.
## 2. Discover idle top-level chats
Use transcript modification time as the last-used signal. Read only the first JSONL record, which contains session metadata. Do not read message content. This version avoids GNU-only `date -d`, `find -newermt`, `xargs -r`, and `stat -c` assumptions.
```bash
set -o pipefail
platform=$(uname -s)
cutoff_epoch=$(( $(date +%s) - 48 * 60 * 60 ))
file_modified_epoch() {
case "$platform" in
Darwin) stat -f %m "$1" ;;
Linux) stat -c %Y "$1" ;;
*)
printf 'Unsupported platform: %s\n' "$platform" >&2
return 1
;;
esac
}
session_id_from_metadata() {
if command -v jq >/dev/null 2>&1; then
jq -r 'select(.type == "session_meta" and .payload.parent_thread_id == null) | .payload.id // empty'
elif command -v python3 >/dev/null 2>&1; then
python3 -c 'import json, sys
record = json.loads(sys.stdin.readline())
payload = record.get("payload", {})
if record.get("type") == "session_meta" and payload.get("parent_thread_id") is None:
print(payload.get("id", ""))'
else
printf 'Install jq or make python3 available to parse session metadata.\n' >&2
return 1
fi
}
while IFS= read -r -d '' file; do
modified=$(file_modified_epoch "$file") || exit 1
[ "$modified" -le "$cutoff_epoch" ] || continue
first=$(head -n 1 "$file") || exit 1
id=$(printf '%s\n' "$first" | session_id_from_metadata) || exit 1
if [ -n "$id" ]; then
printf '%s\t%s\t%s\n' "$id" "$modified" "$file"
fi
done < <(find "$sessions_dir" -type f -name '*.jsonl' -print0)
```
Validate the result before taking action:
1. Require unique UUID-shaped IDs.
2. Require every modification epoch to be less than or equal to `cutoff_epoch`.
3. Require every path to resolve beneath `sessions_dir` and to remain a regular `.jsonl` file.
4. Query `codex_app__list_threads` with `limit: 50`. Remove all IDs in `pinnedThreads` and all returned chats with a non-terminal status.
5. Remove the calling task ID.
For a preview, report the cutoff time, candidate count, protected count, oldest activity, and newest eligible activity. Do not include titles, prompts, or message text.
## 3. Revalidate and archive
Immediately before each archive request, recheck that the exact file still exists, remains under `sessions_dir`, and has a modification epoch no newer than `cutoff_epoch`. Drop a candidate if it changed after discovery.
Call `codex_app__set_thread_archived` with `archived: true`, `hostId: "local"`, and the validated thread ID. Use bounded batches of no more than 12 requests. Collect successes and failures. Retry a failed request once, then leave it active and report the failure.
Do not archive any newly discovered chat in the same run. Only act on the validated snapshot.
## 4. Verify
Run the discovery command again after the API requests finish. Confirm that no unprotected chat from the validated snapshot remains eligible. A pinned idle chat may still appear in the raw discovery output and must be counted as protected, not failed.
Report:
- eligible chats found
- chats protected
- chats archived
- failed requests
- eligible unprotected chats remaining
State that archiving is reversible. If any request failed or any validated unprotected chat remains, finish with the affected IDs only and do not modify transcript files.