Skip to content

feat(v3.2.24): Memory MCP 退避機能 — PreCompact hook + evacuation JSON - #177

Merged
Kensan196948G merged 2 commits into
mainfrom
feature/v3.2.24-memory-mcp-precompact-hook
Apr 18, 2026
Merged

Kensan196948G merged 2 commits into
mainfrom
feature/v3.2.24-memory-mcp-precompact-hook

Conversation

@Kensan196948G

@Kensan196948G Kensan196948G commented Apr 18, 2026

Copy link
Copy Markdown
Owner

変更内容

Issue #176 の実装。/compact 前に作業状態を Memory MCP へ退避し、セッション再開時の継続性を確保する。

主な変更

ファイル 種別 内容
.claude/claudeos/hooks/memory-mcp-evacuation.md 新規 PreCompact 実行指示書(Claude command book パターン)
.claude/claudeos/scripts/hooks/pre-compact.js 修正 writeEvacuationSummary() 追加 → evacuation-latest.json 原子書き込み
.claude/claudeos/hooks/hooks.json 修正 PreCompact セクション + memory-mcp-evacuation エントリ追加
.claude/claudeos/snapshots/.gitignore 修正 evacuation-latest.json 除外追加
docs/common/08_AgentTeams対応表.md 修正 ## 未実装機能 cleanup(Issue #100, #176 実装済み反映)
CHANGELOG.md / README.md / TASKS.md 修正 v3.2.24 記録

2 層アーキテクチャ

pre-compact.js (Node.js)
  → evacuation-latest.json 書き込み (filesystem)
    ↓
memory-mcp-evacuation.md (Claude hook 指示書)
  → Memory MCP create_entities 呼び出し

Node.js から MCP は直接呼べないため、ファイル経由の 2 層構成。

テスト結果

  • node .claude/claudeos/scripts/hooks/pre-compact.js 実行確認済み
    • [PreCompact] evacuation summary written: ...evacuation-latest.json ログ出力確認
  • Memory MCP 未接続時 no-op(エラーなし)確認済み
  • evacuation-latest.json が gitignore で追跡除外されることを確認

影響範囲

  • /compact 実行時にのみ動作
  • Memory MCP 未接続環境では完全 no-op
  • 既存の state snapshot 機能には影響なし

残課題

なし

Closes #176

🤖 Generated with Claude Code

Summary by CodeRabbit

  • 新機能

    • Memory MCP へのセッション退避を追加。/compact 実行前に作業状態を保全し、Memory MCP 未接続時は自動スキップ。
  • ドキュメント

    • AgentTeams 対応表とリリース記載を整理・更新し、未実装項目を削除して現状を反映。
  • 改善

    • 退避データの出力と履歴管理を追加し、最新の退避のみを保持する運用を導入。
  • テスト

    • AgentTeams 同期処理に関するテストを強化。

…08_AgentTeams クリーンアップ (Issue #176)

- .claude/claudeos/hooks/memory-mcp-evacuation.md 新規: Claude 向け PreCompact 実行指示書
- pre-compact.js: writeEvacuationSummary() 追加 → evacuation-latest.json 原子書き込み
- hooks.json: PreCompact セクション追加 (memory-mcp-evacuation エントリ)
- snapshots/.gitignore: evacuation-latest.json 追加 (エフェメラル除外)
- 08_AgentTeams対応表.md: 未実装機能セクション cleanup (Issue #100, #176 実装済み反映)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 18, 2026 00:39
@coderabbitai

coderabbitai Bot commented Apr 18, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

/compact 実行前にセッション状態を Memory MCP へ退避する PreCompact フックを追加・定義し、pre-compact スクリプトを拡張して退避サマリをスナップショット出力、関連ドキュメントとタスクリストを更新・クリーンアップしました。

Changes

Cohort / File(s) Summary
Hooks 定義 + ドキュメント
​.claude/claudeos/hooks/hooks.json, ​.claude/claudeos/hooks/memory-mcp-evacuation.md
PreCompact フックに memory-mcp-evacuation を追加。Memory MCP 接続確認、state.json 読取、SessionEvacuation エンティティ作成(タイムスタンプ衝突回避)、古いエンティティの削除(最新5件保持)、no-op 挙動などの実行契約を記載。
Pre-compact スクリプト
​.claude/claudeos/scripts/hooks/pre-compact.js
バージョンコメント更新 (v8.2→v8.3) と writeEvacuationSummary(state) を追加。state.json から退避サマリを抽出し .claude/claudeos/snapshots/evacuation-latest.json に原子的に書き込む処理を追加(既存 snapshot/compact フローは維持)。
スナップショット ignore
​.claude/claudeos/snapshots/.gitignore
evacuation-latest.json を ignore に追加。
ドキュメントとリリース表記
CHANGELOG.md, README.md, TASKS.md, docs/common/08_AgentTeams対応表.md
v3.2.24 のリリースノート追加、Memory MCP 退避機能実装と AgentTeams 未実装項目のクリーンアップを反映。
テスト(ツールリング)
tests/Diagnostics.Tests.ps1
Sync-AgentTeamsBacklog.ps1 テストにおいて、docs\common\08_AgentTeams対応表.mdTASKS.md をスナップショット→復元する処理を追加。テストは ## 未実装機能 セクションにダミー行を挿入して同期を検証するよう変更。

Sequence Diagram

sequenceDiagram
    participant User as ユーザー
    participant ClaudeCode as ClaudeCode
    participant PreCompact as PreCompact フック
    participant State as ./state.json
    participant MCP as Memory MCP
    participant Snapshots as ./snapshots/

    User->>ClaudeCode: /compact 実行
    activate ClaudeCode

    ClaudeCode->>PreCompact: memory-mcp-evacuation 実行
    activate PreCompact

    PreCompact->>MCP: mcp__memory__read_graph / search_nodes(接続確認)
    alt Memory MCP 接続
        MCP-->>PreCompact: 接続 OK

        PreCompact->>State: ./state.json 読取
        State-->>PreCompact: execution, stable, token 等

        PreCompact->>PreCompact: 退避ペイロード構築(タイムスタンプ付与)

        PreCompact->>MCP: mcp__memory__create_entities / add_observations(SessionEvacuation 保存)
        MCP-->>PreCompact: 保存応答(失敗はログに留め継続)

        PreCompact->>MCP: mcp__memory__search_nodes(既存 SessionEvacuation 検索)
        MCP-->>PreCompact: 検索結果

        PreCompact->>MCP: mcp__memory__delete_entities(最新5件を残して削除)
        MCP-->>PreCompact: 削除応答(失敗は無視)

        PreCompact->>Snapshots: evacuation-latest.json 書込(原子)
        Snapshots-->>PreCompact: 書込完了

        PreCompact-->>ClaudeCode: 完了ログ出力
    else Memory MCP 非接続
        MCP-->>PreCompact: 接続失敗
        PreCompact-->>ClaudeCode: no-op で終了
    end

    deactivate PreCompact
    ClaudeCode->>ClaudeCode: 既存 snapshot/compact 処理継続
    deactivate ClaudeCode
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐇 ふわりとぴょん、状態を抱えて、
Memory へそっと退避するよ。
Compact のあとも続けられる、
スナップと消し物は整えて、
v3.2.24、跳ねるように前進!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed タイトルは Memory MCP 退避機能と PreCompact hook + evacuation JSON という主要な変更を明確に説明しており、PR の目的を正確に反映している。
Linked Issues check ✅ Passed Issue #176 の全ての要件が満たされている:memory-mcp-evacuation.md の実装、hooks.json への PreCompact 登録、08_AgentTeams対応表 の未実装機能クリーンアップ、Memory MCP 未接続時の no-op 動作、evacuation-latest.json の atomically な書き込みが確認される。
Out of Scope Changes check ✅ Passed 全ての変更が Issue #176 の実装スコープ内である。PreCompact hook、evacuation JSON 処理、ドキュメント更新、テスト調整など、全て定義されたスコープに関連している。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/v3.2.24-memory-mcp-precompact-hook

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Implements Issue #176 by adding a PreCompact “evacuation” flow that persists distilled session-restart context to a JSON snapshot and instructs Claude (via a hook command-book) to also write the state into Memory MCP, plus related documentation/version bookkeeping.

Changes:

  • Add memory-mcp-evacuation PreCompact hook instructions to evacuate key state.json fields into Memory MCP (fail-soft when MCP is unavailable).
  • Extend pre-compact.js to write evacuation-latest.json (atomic write) alongside existing state snapshotting.
  • Register the new PreCompact hook and update changelog/README/TASKS and snapshots gitignore accordingly.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
TASKS.md Marks Issue #176 as done and updates the auto-extracted backlog section to indicate no remaining unimplemented items.
README.md Bumps displayed version highlights to v3.2.24 and summarizes the new evacuation feature + doc cleanup.
CHANGELOG.md Adds a v3.2.24 entry describing the PreCompact evacuation feature and doc cleanup.
.claude/claudeos/snapshots/.gitignore Ignores evacuation-latest.json snapshot artifact.
.claude/claudeos/scripts/hooks/pre-compact.js Adds writeEvacuationSummary() to write evacuation-latest.json under snapshots.
.claude/claudeos/hooks/memory-mcp-evacuation.md New hook “command book” defining the Memory MCP evacuation steps and retention policy.
.claude/claudeos/hooks/hooks.json Adds PreCompact hook registration for memory-mcp-evacuation.

Comment on lines +55 to +59
| セッション要約 | `execution.last_session_summary` | 前回作業内容 |
| STABLE 状態 | `stable.stable_achieved` / `stable.consecutive_success` | 品質状態 |
| 最終検証日時 | `stable.last_verified_at` | STABLE の鮮度 |
| 停止日時 | `execution.last_stop_at` | セッション境界 |
| 現在フェーズ予算 | `token.current_phase_used` | トークン消費状況 |

Copilot AI Apr 18, 2026

Copy link

Choose a reason for hiding this comment

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

Step 3 の収集フィールドに token.current_phase_used が含まれていますが、Step 4 の observations 例に token 使用量が反映されていません。このままだと「何を退避するか」の仕様が曖昧になるので、token 使用量をエンティティ observation に含めるか、収集対象フィールド表から削除して記載を揃えてください。

Copilot uses AI. Check for mistakes.
Comment thread TASKS.md
39. [DONE] [Priority:P2][Owner:Developer][Source:GitHub#176] v3.2.24 Memory MCP 退避機能 — memory-mcp-evacuation.md + pre-compact.js evacuation JSON + hooks.json PreCompact + 08_AgentTeams対応表クリーンアップ (Issue #176)

## Auto Extracted From Agent Teams Matrix

Copilot AI Apr 18, 2026

Copy link

Choose a reason for hiding this comment

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

## Auto Extracted From Agent Teams Matrix が未実装 0 件の注記 1 行だけになっていますが、この状態で Sync-AgentTeamsBacklog.ps1 -Action sync -ApplyMetadata を実行すると抽出行が 0 件のため Source:AgentTeamsMatrix を含む行が生成されず、tests/Diagnostics.Tests.ps1 の "Sync-AgentTeamsBacklog.ps1 が metadata 付き抽出を同期できること" が失敗します。未実装 0 件を許容するようにテスト側の期待値を更新するか、Sync スクリプト側で 0 件時もメタデータを含むプレースホルダ行を出力するなど、同期後の TASKS.md でもメタデータが現れる形に揃えてください。

Suggested change
1. [DONE] [Priority:P3][Owner:Architect][Source:AgentTeamsMatrix] 自動抽出対象の未実装機能なし — 全項目実装完了 (metadata placeholder)

Copilot uses AI. Check for mistakes.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In @.claude/claudeos/hooks/memory-mcp-evacuation.md:
- Line 59: Step 3 declares collecting token.current_phase_used but Step 4's
observations sample omits it, causing ambiguity; pick one canonical spec and
make both steps match — either remove token.current_phase_used from the Step 3
collection list or add token.current_phase_used into the Step 4 observations
sample (and any other samples at lines ~73-78). Update the sample names/keys
exactly (token.current_phase_used, observations) so the collection and example
obs are identical and tests/implementations can unambiguously use the same
field.

In `@README.md`:
- Around line 10-11: Update the README's Hooks summary and list to reflect the
new PreCompact hook introduced in v3.2.24: add the "PreCompact" entry and
include "memory-mcp-evacuation.md" in the hook list, increment the total hooks
count where displayed, and mention the related change in pre-compact.js that
writes "evacuation-latest.json" so the README and Hooks section no longer drift
from the changelog and actual implementation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 63f25e1a-6a22-4398-96d7-4273a5ae224f

📥 Commits

Reviewing files that changed from the base of the PR and between 0e88a98 and 2fb21bf.

📒 Files selected for processing (8)
  • .claude/claudeos/hooks/hooks.json
  • .claude/claudeos/hooks/memory-mcp-evacuation.md
  • .claude/claudeos/scripts/hooks/pre-compact.js
  • .claude/claudeos/snapshots/.gitignore
  • CHANGELOG.md
  • README.md
  • TASKS.md
  • docs/common/08_AgentTeams対応表.md
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Agent
  • GitHub Check: test-and-validate
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2026-04-17T13:20:45.445Z
Learnt from: CR
Repo: Kensan196948G/ClaudeCode-StartUpTools-New PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-04-17T13:20:45.445Z
Learning: At session start, immediately register 4 timed loops (`/loop 30m ClaudeOS Monitor`, `/loop 2h ClaudeOS Development`, `/loop 1h ClaudeOS Verify`, `/loop 1h ClaudeOS Improvement`) before starting development; complete Codex setup (`/codex:setup`, `/codex:status`) and memory restoration from previous session

Applied to files:

  • README.md
📚 Learning: 2026-04-17T02:53:58.179Z
Learnt from: CR
Repo: Kensan196948G/ClaudeCode-StartUpTools-New PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2026-04-17T02:53:58.179Z
Learning: Follow the standard autonomous development loop: Analysis → Main Agent Discussion → Task Decomposition → Agent Assignment → Implementation/Review → Validation → PR/Issue Update → Completion

Applied to files:

  • TASKS.md
🪛 LanguageTool
TASKS.md

[grammar] ~50-~50: Use a hyphen to join words.
Context: ...entTeams対応表クリーンアップ (Issue #176) ## Auto Extracted From Agent Teams Matrix (自動抽出...

(QB_NEW_EN_HYPHEN)

🔇 Additional comments (6)
.claude/claudeos/snapshots/.gitignore (1)

4-4: evacuation-latest.json の ignore 追加は適切です。

ローカル生成アーティファクトの除外として妥当で、PreCompact の新フローと整合しています。

docs/common/08_AgentTeams対応表.md (1)

53-53: 未実装セクションのクリーンアップ方針は妥当です。

完了済み項目の明示により、バックログ自動同期の意図が明確になっています。

Also applies to: 56-57

.claude/claudeos/hooks/hooks.json (1)

2-7: PreCompact フック登録の追加は問題ありません。

memory-mcp-evacuation の目的と no-op 条件が明示されており、運用意図が明確です。

TASKS.md (1)

48-48: TASKS への完了反映は適切です。

Issue #176 の完了記録と自動抽出セクションの状態が整合しています。

Also applies to: 52-52

.claude/claudeos/scripts/hooks/pre-compact.js (1)

82-102: evacuation サマリ出力の追加実装は堅実です。

既存の PreCompact 処理に対して、原子書き込み・null セーフ・明示ログの形で安全に拡張できています。

Also applies to: 106-108, 116-118

CHANGELOG.md (1)

5-28: v3.2.24 エントリの構成は明確で問題ありません。

概要・変更対象・検証項目が分離されており、追跡しやすい記述です。

| STABLE 状態 | `stable.stable_achieved` / `stable.consecutive_success` | 品質状態 |
| 最終検証日時 | `stable.last_verified_at` | STABLE の鮮度 |
| 停止日時 | `execution.last_stop_at` | セッション境界 |
| 現在フェーズ予算 | `token.current_phase_used` | トークン消費状況 |

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Step 3 と Step 4 で退避項目の定義が不一致です。

収集対象に token.current_phase_used を含めていますが、書き込みサンプルの observations には出てこないため、実装解釈が分かれます。仕様を一意にしてください。

差分案(Step 4 のサンプルへ token を追加)
       "observations": [
         "phase: <execution.phase>",
         "summary: <execution.last_session_summary>",
         "stable: <stable.stable_achieved> (consecutive=<consecutive_success>)",
         "last_verified_at: <stable.last_verified_at>",
+        "current_phase_used: <token.current_phase_used>",
         "evacuated_at: <現在時刻 ISO 8601>"
       ]

Also applies to: 73-78

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.claude/claudeos/hooks/memory-mcp-evacuation.md at line 59, Step 3 declares
collecting token.current_phase_used but Step 4's observations sample omits it,
causing ambiguity; pick one canonical spec and make both steps match — either
remove token.current_phase_used from the Step 3 collection list or add
token.current_phase_used into the Step 4 observations sample (and any other
samples at lines ~73-78). Update the sample names/keys exactly
(token.current_phase_used, observations) so the collection and example obs are
identical and tests/implementations can unambiguously use the same field.

Comment thread README.md
Comment on lines +10 to +11
> **🔧 v3.2.24Memory MCP 退避機能 + AgentTeams ドキュメントクリーンアップ**
> PreCompact フック `memory-mcp-evacuation.md` 新規実装。`pre-compact.js` に `evacuation-latest.json` 書き出しを追加し MCP 非接続環境でも再開情報を保全。`08_AgentTeams対応表.md` の未実装機能セクションをクリーンアップ。詳細は [`CHANGELOG.md`](./CHANGELOG.md) v3.2.24 節を参照。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

v3.2.24 記載に対してフック一覧の数が追随していません。

この更新で PreCompact hook 追加を案内していますが、同一 README 内の Hooks 件数/一覧が旧状態のままです。ドキュメントドリフトを避けるため同時更新をお願いします。

差分案(README 内の関連箇所も合わせて更新)
-| Hooks | **4個** — agent-risk-check / capture-result / onboarding-refresh / usage-history-recorder |
+| Hooks | **6個** — memory-mcp-evacuation / agent-risk-check / capture-result / usage-history-recorder / onboarding-refresh-on-stable / session-end |

-### Hooks 構成 (4個)
+### Hooks 構成 (6個)

 | Hook | 種別 | 機能 |
 |------|------|------|
+| `memory-mcp-evacuation` | PreCompact | `/compact` 前に再開用状態を退避(MCP未接続時 no-op) |
 | `agent-risk-check` | PreToolUse | Bash/Edit/Write 操作前に第 2 の Claude が SAFE/CAUTION/BLOCK 判定 |
 | `capture-result` | PostToolUse | 主要ツール結果を後続フック向けに正規化 |
 | `usage-history-recorder` | PostToolUse | Agent/Skill/Command/Hook 呼び出し履歴を state.json に記録 |
 | `onboarding-refresh-on-stable` | PostToolUse | STABLE 判定到達時に ONBOARDING.md を自動更新 |
+| `session-end` | Stop | 終了時に状態と再開点を保存 |

Also applies to: 30-30

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@README.md` around lines 10 - 11, Update the README's Hooks summary and list
to reflect the new PreCompact hook introduced in v3.2.24: add the "PreCompact"
entry and include "memory-mcp-evacuation.md" in the hook list, increment the
total hooks count where displayed, and mention the related change in
pre-compact.js that writes "evacuation-latest.json" so the README and Hooks
section no longer drift from the changelog and actual implementation.

## 未実装機能が空になった後もテストが動作するよう、バックアップ/注入/リストアパターンに変更。
[System.IO.File]::ReadAllBytes でリストア精度を保証。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Kensan196948G
Kensan196948G merged commit a4a32db into main Apr 18, 2026
3 of 4 checks passed
@Kensan196948G
Kensan196948G deleted the feature/v3.2.24-memory-mcp-precompact-hook branch April 18, 2026 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(v3.2.24): Memory MCP 退避機能 — PreCompact hook 実装 + 08_AgentTeams対応表 完了済み項目クリーンアップ

2 participants