-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpost-compact.mjs
More file actions
82 lines (65 loc) · 2.55 KB
/
post-compact.mjs
File metadata and controls
82 lines (65 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env node
/* global process, Buffer */
/**
* PostCompact hook: restore audit state after context compaction.
*
* Complements pre-compact.mjs:
* PreCompact → saves snapshot to compaction-snapshot.json
* PostCompact → reads snapshot, injects context reinforcement
*
* This ensures audit state is not lost when Claude Code compresses context.
*/
import { readFileSync, existsSync, unlinkSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
const __dirname = dirname(fileURLToPath(import.meta.url));
// ── Read stdin (pass through) ────────────────────────────────
let raw;
try {
const chunks = [];
for await (const chunk of process.stdin) chunks.push(chunk);
raw = Buffer.concat(chunks).toString("utf8");
} catch {
process.exit(0);
}
// Pass through stdin unchanged
if (raw) process.stdout.write(raw);
// ── Restore from snapshot ────────────────────────────────────
let REPO_ROOT;
try {
const ctx = await import("./context.mjs");
REPO_ROOT = ctx.REPO_ROOT;
} catch {
process.exit(0);
}
const snapshotPath = resolve(REPO_ROOT, ".claude", "compaction-snapshot.json");
if (!existsSync(snapshotPath)) {
process.exit(0);
}
try {
const snapshot = JSON.parse(readFileSync(snapshotPath, "utf8"));
const parts = [];
parts.push(`<COMPACTION-RESTORE timestamp="${snapshot.saved_at}">`);
// Restore audit state
if (snapshot.audit_in_progress) {
parts.push("⚠️ Audit was in progress before compaction. Check .claude/audit.lock and audit-bg.log.");
}
if (snapshot.last_audit_status) {
parts.push(`📋 Last audit status: ${snapshot.last_audit_status}`);
}
// Restore retro marker state
if (snapshot.retro_marker?.retro_pending) {
parts.push("⚠️ Retrospective is pending. Complete the self-improvement protocol before proceeding.");
if (snapshot.retro_marker.agreed_items) {
parts.push(`Agreed items: ${snapshot.retro_marker.agreed_items}`);
}
}
parts.push("</COMPACTION-RESTORE>");
// Output restoration context to stderr (informational)
console.error(`[post-compact] Restored state from snapshot (${snapshot.saved_at})`);
// Note: PostCompact cannot inject additionalContext like SessionStart.
// The restoration is informational — SessionStart handles full context reinject on next session.
} catch (e) {
console.error(`[post-compact] Snapshot restore warning: ${e.message}`);
}
process.exit(0);