archive-mail-rebuild-threads

archive-mail-rebuild-threads is a command for Claude Code from PsychQuant/che-apple-mail-mcp. It costs 12 tokens per session (1,780 once invoked), scanned A, original, MIT.

A command that rebuilds the index connecting archived email files to their conversation threads. It reads the metadata at the top of each Markdown file and recreates the thread index file.

In plain words
What is it for?
Rebuilding thread indexes for archived email folders, migrating the index location, backing up the existing index, and checking that stored messages and thread data agree.
Why use it?
It repairs or checks the thread index after it is deleted, damaged, manually changed, or migrated from an older archive format.

Command for Claude Code

Written for Claude Code: allowed-tools in frontmatter.

Part of the che-apple-mail-mcp plugin — 3 skills, 5 commands, 1 hook shipped together

Good fit Rebuilding thread indexes for archived email folders, migrating the index location, backing up the existing index, and checking that stored messages and thread data agree.

Compare 6 commands from other repositories ↓
Install with agentmods
npx agentmods add commands/psychquant/che-apple-mail-mcp/archive-mail-rebuild-threads
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/PsychQuant/che-apple-mail-mcp

Made for: Claude Code.

Or install che-apple-mail-mcp, the plugin that ships this one along with the rest of its 3 skills, 5 commands, 1 hook.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for archive-mail-rebuild-threads

README.md
[![agentmods](https://agentmods.dev/badge/commands/psychquant/che-apple-mail-mcp/archive-mail-rebuild-threads/github.svg)](https://agentmods.dev/commands/psychquant/che-apple-mail-mcp/archive-mail-rebuild-threads)
Your own site
<a href="https://agentmods.dev/commands/psychquant/che-apple-mail-mcp/archive-mail-rebuild-threads"><img src="https://agentmods.dev/badge/commands/psychquant/che-apple-mail-mcp/archive-mail-rebuild-threads/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for archive-mail-rebuild-threads

Your own site · 80×15
<a href="https://agentmods.dev/commands/psychquant/che-apple-mail-mcp/archive-mail-rebuild-threads"><img src="https://agentmods.dev/badge/commands/psychquant/che-apple-mail-mcp/archive-mail-rebuild-threads.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 12 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,780 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00012 $0.01780
Opus 5 $0.00006 $0.00890
Sonnet 5 $0.00002 $0.00356
Haiku 4.5 $0.00001 $0.00178

Measured 12d ago against content hash d75bfcbc104a, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-12, from the pricing page.

Security

Grade A, and why

archive-mail-rebuild-threads scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 12d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

plugin/commands/archive-mail-rebuild-threads.md · 199 lines

How it starts

The opening of the file, as written. The whole thing — 199 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Archive Mail — Rebuild Threads Index

掃描 archive 目錄下所有 md 檔的 YAML frontmatter,從 canonical storage 重建 .threads.json

什麼時候用

  1. .threads.json 損壞或被刪除
  2. 手動修改過 md frontmatter 的 thread_key(例如拆分長 thread)
  3. 從舊版 archive 升級到 v2.6.0+(舊 md 沒有 frontmatter,需先 backfill frontmatter 再 rebuild)
  4. 想確認 .threads.json 和 md 檔一致(當作 sanity check)

使用方式

/archive-mail-rebuild-threads
/archive-mail-rebuild-threads communications/emails
  • 第一個參數(可選):archive 目錄,預設 communication/emails

執行步驟

Step 1: 解析參數

  • archive_dir: 預設 communication/emails

若目錄不存在,報錯退出。

Step 2: 解析 namespace 路徑 + 備份既有 threads.json(v2.8.0+)

NAMESPACE_DIR=".claude/.mail"
SLUG=$(echo "${archive_dir}" | tr '/' '-' | sed 's/^[-.]*//;s/[-.]*$//')
INDEX_DIR="${NAMESPACE_DIR}/state/archives/${SLUG}"
THREADS_FILE="${INDEX_DIR}/threads.json"

mkdir -p "${INDEX_DIR}"

# Auto-migrate legacy(silent)
if [ ! -f "${THREADS_FILE}" ] && [ -f "${archive_dir}/.threads.json" ]; then
  mv "${archive_dir}/.threads.json" "${THREADS_FILE}"
  echo "🔄 Migrated ${archive_dir}/.threads.json → ${THREADS_FILE}"
fi

# Backup existing threads.json before rebuild
if [ -f "${THREADS_FILE}" ]; then
  cp "${THREADS_FILE}" "${THREADS_FILE}.bak.$(date +%Y%m%d_%H%M%S)"
  echo "Backed up existing threads.json"
fi

備份不刪舊的,方便回滾。

Step 3: 掃描所有 md 檔

Glob "${archive_dir}/*.md" 列出所有歸檔 md。

對每一個 md:

  1. Read 檔案(只需要前 30 行,frontmatter 一定在這裡面)
  2. 解析 YAML frontmatter(位於檔案最前面的 --- 之間)
  3. 提取:
    • message_id(必需)
    • thread_key(必需)
    • in_reply_to(可選)
    • date(必需)
    • sender(必需)
  4. 若 frontmatter 缺失或必需欄位不全:
    • 記錄到 missing_frontmatter 清單
    • 不中斷流程,繼續下一檔

Step 4: 聚合成 threads 結構

建立新的 threads 物件:

new_threads = {}

for each valid md:
    key = md.thread_key
    if key not in new_threads:
        new_threads[key] = {
            "messages": [],
            "participants_set": set(),
            "first_message": null,
            "last_message": null
        }

    entry = new_threads[key]
    entry.messages.append({
        "message_id": md.message_id,
        "file": md.filename,
        "date": md.date,
        "sender": md.sender,
        "in_reply_to": md.in_reply_to or null
    })
    entry.participants_set.add(md.sender)
    if md.to/cc available in frontmatter → add to set
    entry.first_message = min(entry.first_message, md.date)
    entry.last_message = max(entry.last_message, md.date)

# 後處理
for key, entry in new_threads:
    entry.messages.sort(key=lambda m: m.date)  # 時序排序
    entry.message_count = len(entry.messages)
    entry.participants = sorted(list(entry.participants_set))
    del entry.participants_set

Read the full file on GitHub · 199 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 12d ago First seen · 199 lines · 12 tokens per session scan A d75bfcbc104a

Subscribe to this mod's changes

archive-mail-rebuild-threads is a command published in the GitHub repository PsychQuant/che-apple-mail-mcp (8 stars, last pushed today), licensed MIT. It adds 12 tokens to every session and 1,780 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.