-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontext.mjs
More file actions
392 lines (334 loc) · 13.8 KB
/
context.mjs
File metadata and controls
392 lines (334 loc) · 13.8 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/env node
/**
* Shared context module — config, paths, tag constants, markdown parser, i18n cache.
*
* All consensus-loop scripts import from this module to avoid
* duplicate config parsing, path resolution, and function implementations.
*/
import { readFileSync, existsSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { execFileSync } from "node:child_process";
// ── Paths ─────────────────────────────────────────────────
export const HOOKS_DIR = dirname(fileURLToPath(import.meta.url));
/**
* Resolve the repository root directory.
*
* Worktree-aware: cwd-based git resolution runs first so that
* subagents running inside a git worktree see the worktree root,
* not the main repo root.
*
* Legacy layout: .claude/hooks/consensus-loop/ → 3 levels up (fallback)
* Plugin layout: installed anywhere → git rev-parse (primary)
*
* Falls back to process.cwd() when git is unavailable.
*/
function resolveRepoRoot() {
// 1. cwd-based git resolution — worktree-aware (primary)
try {
return execFileSync("git", ["rev-parse", "--show-toplevel"], {
cwd: process.cwd(),
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
}).trim();
} catch { /* git not available or not in a repo */ }
// 2. Legacy layout fallback: .claude/hooks/consensus-loop/ → 3 levels up
const legacyRoot = resolve(HOOKS_DIR, "..", "..", "..");
if (existsSync(resolve(legacyRoot, ".git"))) return legacyRoot;
// 3. Last resort
return process.cwd();
}
export const REPO_ROOT = resolveRepoRoot();
// ── Config ────────────────────────────────────────────────
/** Project-scoped config directory — survives plugin updates. */
export const PROJECT_CONFIG_DIR = resolve(REPO_ROOT, ".claude", "consensus-loop");
/**
* Find config.json path.
*
* Priority:
* 1. REPO_ROOT/.claude/consensus-loop/config.json — project-scoped (survives plugin updates)
* 2. $CLAUDE_PLUGIN_ROOT/config.json — plugin dir (cleared on update)
* 3. HOOKS_DIR/config.json — legacy / direct CLI invocation
*/
function findConfigPath() {
// 1. Project-scoped (persistent across plugin updates)
const projectConfig = resolve(PROJECT_CONFIG_DIR, "config.json");
if (existsSync(projectConfig)) return projectConfig;
// 2. Plugin root (set by hooks.json)
const pluginRoot = process.env.CLAUDE_PLUGIN_ROOT;
if (pluginRoot) {
const p = resolve(pluginRoot, "config.json");
if (existsSync(p)) return p;
}
// 3. Legacy fallback
const local = resolve(HOOKS_DIR, "config.json");
if (existsSync(local)) return local;
return null;
}
const _configPath = findConfigPath();
/** config.json이 존재하지 않으면 true. 훅에서 설정 안내를 출력할 때 사용. */
export const configMissing = _configPath === null;
/** config.json 미존재 시 훅이 crash하지 않도록 최소 기본값 제공. */
const DEFAULT_CONFIG = {
plugin: { locale: "en", hooks_enabled: {} },
consensus: {
watch_file: "docs/feedback/claude.md",
trigger_tag: "[REVIEW_NEEDED]",
agree_tag: "[APPROVED]",
pending_tag: "[CHANGES_REQUESTED]",
},
};
export const cfg = _configPath
? JSON.parse(readFileSync(_configPath, "utf8"))
: DEFAULT_CONFIG;
export const plugin = cfg.plugin ?? DEFAULT_CONFIG.plugin;
export const consensus = cfg.consensus ?? DEFAULT_CONFIG.consensus;
/**
* Resolve a plugin-relative path, checking project config dir first.
* This allows project-scoped templates/references to override plugin defaults.
*
* Priority: PROJECT_CONFIG_DIR → CLAUDE_PLUGIN_ROOT → HOOKS_DIR
*/
export function resolvePluginPath(relativePath) {
const projectPath = resolve(PROJECT_CONFIG_DIR, relativePath);
if (existsSync(projectPath)) return projectPath;
const pluginRoot = process.env.CLAUDE_PLUGIN_ROOT;
if (pluginRoot) {
const p = resolve(pluginRoot, relativePath);
if (existsSync(p)) return p;
}
return resolve(HOOKS_DIR, relativePath);
}
// ── Hook toggles ─────────────────────────────────────────
const _hooksEnabled = plugin.hooks_enabled ?? {};
/** 훅 활성화 여부 확인. config에 없으면 기본값 true. */
export function isHookEnabled(hookName) {
return _hooksEnabled[hookName] ?? true;
}
// ── Locale 검증 (path traversal 방지) ─────────────────────
const ALLOWED_LOCALES = new Set(["en", "ko"]);
const rawLocale = plugin.locale ?? "en";
export const safeLocale = ALLOWED_LOCALES.has(rawLocale) ? rawLocale : "en";
// ── Section name constants (English defaults; config overrides) ──
const S = consensus.sections ?? {};
export const SEC = {
auditScope: S.audit_scope ?? "Audit Scope",
finalVerdict: S.final_verdict ?? "Final Verdict",
agreedAnchor: S.agreed_anchor ?? "Agreed",
resetCriteria: S.reset_criteria ?? "Reset Criteria",
rejectCodes: S.reject_codes ?? "Reject Codes",
additionalTasks: S.additional_tasks ?? "Additional Tasks",
nextTask: S.next_task ?? "Next Task",
deprecatedProtocol: S.deprecated_protocol ?? "Improved Protocol",
promotionTarget: S.promotion_target ?? "Current Promotion Target",
changedFiles: S.changed_files ?? "Changed Files",
};
export const DOC_PATTERNS = consensus.doc_patterns ?? {};
// ── Tag constants + regex ─────────────────────────────────
export const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
export const triggerInner = consensus.trigger_tag.replace(/^\[|\]$/g, "");
export const agreeInner = consensus.agree_tag.replace(/^\[|\]$/g, "");
export const pendingInner = consensus.pending_tag.replace(/^\[|\]$/g, "");
const tagAlts = [agreeInner, pendingInner, triggerInner].map(escapeRe).join("|");
export const STATUS_TAG_RE = new RegExp(`\\[(${tagAlts})(?:[^\\]]*?)\\]`);
export const STATUS_TAG_RE_GLOBAL = new RegExp(
"`?\\[(" + tagAlts + ")(?:[^\\]]*?)\\]`?", "g",
);
// ── Path resolution (memoized) ────────────────────────────
let _watchPath = undefined;
let _respondPath = undefined;
function probeFile(subPath, name) {
const dirs = [resolve(HOOKS_DIR, subPath), resolve(REPO_ROOT, subPath)];
for (const dir of dirs) {
for (const v of [name, name.toUpperCase(), name.toLowerCase()]) {
const p = resolve(dir, v);
if (existsSync(p)) return p;
}
}
return null;
}
export function findWatchFile() {
if (_watchPath !== undefined && _watchPath !== null) return _watchPath;
const name = consensus.watch_file.split("/").pop();
const subPath = consensus.watch_file.split("/").slice(0, -1).join("/");
_watchPath = probeFile(subPath, name);
return _watchPath;
}
export function findRespondFile() {
if (_respondPath !== undefined && _respondPath !== null) return _respondPath;
const respondName = plugin.respond_file ?? "gpt.md";
const subPath = consensus.watch_file.split("/").slice(0, -1).join("/");
_respondPath = probeFile(subPath, respondName);
return _respondPath;
}
/** Reset memoization cache — for testing. */
export function resetPathCache() {
_watchPath = undefined;
_respondPath = undefined;
}
// ── i18n (cached) ─────────────────────────────────────────
const localeCache = new Map();
/** Resolve locales directory — prefer CLAUDE_PLUGIN_ROOT so worktree copies can find locale files. */
function findLocalesDir() {
const pluginRoot = process.env.CLAUDE_PLUGIN_ROOT;
if (pluginRoot) {
const p = resolve(pluginRoot, "locales");
if (existsSync(p)) return p;
}
return resolve(HOOKS_DIR, "locales");
}
export function createT(locale) {
if (localeCache.has(locale)) return localeCache.get(locale);
const localePath = resolve(findLocalesDir(), `${locale}.json`);
let messages = {};
try { messages = JSON.parse(readFileSync(localePath, "utf8")); } catch { /* fallback */ }
const t = (key, vars) => {
let msg = messages[key] ?? key;
if (vars) {
for (const [k, v] of Object.entries(vars)) {
msg = msg.split(`{${k}}`).join(String(v));
}
}
return msg;
};
localeCache.set(locale, t);
return t;
}
export const t = createT(safeLocale);
// ── Markdown parser ───────────────────────────────────────
/** Extract status tag from a line. When multiple tags exist, the last (newest) wins. */
export function extractStatusFromLine(line) {
const match = line.match(STATUS_TAG_RE);
if (!match) return null;
const innerRe = new RegExp(tagAlts, "g");
const statuses = [...match[0].matchAll(innerRe)].map((item) => item[0]);
return statuses.at(-1) ?? null;
}
/** Find a `## heading` section in markdown and return { start, end, lines }. */
export function readSection(markdown, heading) {
const lines = typeof markdown === "string" ? markdown.split(/\r?\n/) : markdown;
const escaped = escapeRe(heading);
const start = lines.findIndex((line) =>
new RegExp(`^##\\s+${escaped}\\s*$`).test((typeof line === "string" ? line : "").trim())
);
if (start < 0) return null;
const end = lines.findIndex((line, idx) =>
idx > start && /^##\s+/.test((typeof line === "string" ? line : "").trim())
);
return {
start,
end: end >= 0 ? end : lines.length,
lines: lines.slice(start, end >= 0 ? end : lines.length),
};
}
/** Replace a section. Appends to end of file if section not found. */
export function replaceSection(markdown, heading, replacementLines) {
const lines = markdown.split(/\r?\n/);
const section = readSection(lines, heading);
if (section) {
lines.splice(section.start, section.end - section.start, ...replacementLines);
return `${lines.join("\n")}\n`;
}
return `${markdown.replace(/\s*$/, "")}\n\n${replacementLines.join("\n")}\n`;
}
/** Remove a section. */
export function removeSection(markdown, heading) {
const lines = markdown.split(/\r?\n/);
const section = readSection(lines, heading);
if (!section) return markdown;
lines.splice(section.start, section.end - section.start);
return `${lines.join("\n").replace(/\n{3,}/g, "\n\n").replace(/\s*$/, "")}\n`;
}
/** Parse all lines containing status tags. */
export function parseStatusLines(markdown) {
const items = [];
for (const line of markdown.split(/\r?\n/)) {
const status = extractStatusFromLine(line);
if (!status) continue;
const key = line
.replace(STATUS_TAG_RE_GLOBAL, "")
.replace(/\*\*/g, "")
.replace(/`/g, "")
.replace(/^[\s-]*/, "")
.replace(/:\s*$/, "")
.trim();
items.push({ status, key, raw: line.trim() });
}
return items;
}
export function stripStatusFormatting(line) {
return line
.replace(STATUS_TAG_RE_GLOBAL, "")
.replace(/^[\s#-]*/, "")
.replace(/`/g, "")
.replace(/\*\*/g, "")
.replace(/:\s*$/, "")
.trim();
}
export function replaceStatusTag(line, status) {
return line.replace(STATUS_TAG_RE, `[${status}]`);
}
/** Extract IDs (e.g. TN-1, FE-6A, E1) from a line. Supports ranges (TN-1~TN-6). */
export function collectIdsFromLine(line) {
const ids = new Set();
const rangeRe = /\b([A-Z]{2,})-(\d+)([A-Z]?)\s*~\s*(?:\1-?)?(\d+)([A-Z]?)\b/g;
let m;
while ((m = rangeRe.exec(line)) !== null) {
const [, prefix, startStr, startSuffix, endStr, endSuffix] = m;
const start = Number(startStr), end = Number(endStr);
if (Number.isFinite(start) && Number.isFinite(end) && end >= start && startSuffix === endSuffix) {
for (let i = start; i <= end; i++) ids.add(`${prefix}-${i}${startSuffix}`);
}
}
const idRe = /\b([A-Z]{2,})-(\d+)([A-Z]?)\b/g;
while ((m = idRe.exec(line)) !== null) ids.add(`${m[1]}-${m[2]}${m[3] ?? ""}`);
const singleRe = /\b([A-Z])(\d{1,2})\b/g;
while ((m = singleRe.exec(line)) !== null) {
const id = `${m[1]}${m[2]}`;
if (!/^H[1-6]$/.test(id)) ids.add(id);
}
return [...ids];
}
/** Extract `- ` bullet items from a section. */
export function readBulletSection(markdown, heading) {
const section = readSection(markdown, heading);
if (!section) return [];
return section.lines
.map((line) => line.trim())
.filter((line) => line.startsWith("- "))
.map((line) => line.replace(/^- /, "").trim());
}
/** Check for empty markers (해당 없음, 없음, none). */
export function isEmptyMarker(line) {
return new RegExp(
`^\`?(${DOC_PATTERNS.empty_markers ?? "해당 없음|없음|none"})\`?$`, "i"
).test(line.trim());
}
/** Extract approved IDs from markdown. */
export function extractApprovedIds(markdown) {
const ids = new Set();
for (const line of markdown.split(/\r?\n/)) {
if (extractStatusFromLine(line) !== agreeInner) continue;
for (const id of collectIdsFromLine(line)) ids.add(id);
}
return ids;
}
/** Extract pending IDs from markdown. */
export function extractPendingIds(markdown) {
const ids = new Set();
for (const line of markdown.split(/\r?\n/)) {
if (extractStatusFromLine(line) !== pendingInner) continue;
for (const id of collectIdsFromLine(line)) ids.add(id);
}
return ids;
}
/** Extract approved IDs from a specific section. */
export function extractApprovedIdsFromSection(markdown, heading) {
const section = readSection(markdown, heading);
return section ? extractApprovedIds(section.lines.join("\n")) : new Set();
}
export function mergeIdSets(...sets) {
const merged = new Set();
for (const s of sets) for (const v of s) merged.add(v);
return merged;
}