geloforge 0.7.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -26
- package/VERSION +1 -1
- package/core/audit-provider.mjs +91 -22
- package/core/coding-loop.mjs +500 -0
- package/core/compatibility.json +1 -1
- package/core/context-cache.mjs +4 -3
- package/core/context-compiler.mjs +77 -10
- package/core/doctor.mjs +319 -0
- package/core/hooks-init.mjs +245 -0
- package/core/mcp-init.mjs +3 -2
- package/core/mcp-lock.mjs +488 -0
- package/core/mcp-server.mjs +70 -8
- package/core/project-lock.mjs +49 -17
- package/core/project-status.mjs +42 -0
- package/core/skills-advisor.mjs +944 -0
- package/core/text-utils.mjs +63 -5
- package/package.json +2 -1
- package/scripts/geloforge.mjs +420 -7
package/core/doctor.mjs
ADDED
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deterministic one-command diagnostics (`geloforge doctor`) for project health,
|
|
3
|
+
* harness compatibility, Git status, cooperative leases, skills lint, MCP pinning,
|
|
4
|
+
* and ambient Git/IDE hook setup. Zero external dependencies, zero network calls.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { checkManifestCompatibility, loadCompatibilityConfig } from "./compatibility.mjs";
|
|
11
|
+
import { getGitStatusScoped } from "./git-utils.mjs";
|
|
12
|
+
import { parseConfig } from "./mcp-init.mjs";
|
|
13
|
+
import { checkMcpLock, MCP_LOCK_PATH } from "./mcp-lock.mjs";
|
|
14
|
+
import { assertSafeWithinRoot, redactSecretContent } from "./path-safety.mjs";
|
|
15
|
+
import { loadProjectManifestAndState } from "./project-locator.mjs";
|
|
16
|
+
import { validateProject } from "./project-validator.mjs";
|
|
17
|
+
import { discoverSkills } from "./skills.mjs";
|
|
18
|
+
import { lintDiscoveredSkills } from "./skills-lint.mjs";
|
|
19
|
+
import { listTaskLeases } from "./task-manager.mjs";
|
|
20
|
+
|
|
21
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
22
|
+
const __dirname = path.dirname(__filename);
|
|
23
|
+
const DEFAULT_HARNESS_DIR = path.resolve(__dirname, "..");
|
|
24
|
+
|
|
25
|
+
function readSafeHookConfig(projectDir, relPath) {
|
|
26
|
+
const abs = assertSafeWithinRoot(projectDir, relPath);
|
|
27
|
+
const st = fs.lstatSync(abs, { throwIfNoEntry: false });
|
|
28
|
+
if (!st) return { status: "missing", content: null };
|
|
29
|
+
if (st.isSymbolicLink() || !st.isFile()) {
|
|
30
|
+
return { status: "unsafe", content: null };
|
|
31
|
+
}
|
|
32
|
+
return { status: "ok", content: fs.readFileSync(abs, "utf8") };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function inspectConfiguredHooks(projectDir) {
|
|
36
|
+
const configured = [];
|
|
37
|
+
const unsafe = [];
|
|
38
|
+
|
|
39
|
+
const gitHook = readSafeHookConfig(projectDir, ".git/hooks/pre-commit");
|
|
40
|
+
if (gitHook.status === "unsafe") unsafe.push(".git/hooks/pre-commit");
|
|
41
|
+
else if (gitHook.status === "ok" && gitHook.content.includes("geloforge:hooks-init:start")) {
|
|
42
|
+
configured.push("git");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const [label, relPath] of [
|
|
46
|
+
["claude", ".claude/settings.json"],
|
|
47
|
+
["gemini", ".gemini/settings.json"],
|
|
48
|
+
]) {
|
|
49
|
+
const file = readSafeHookConfig(projectDir, relPath);
|
|
50
|
+
if (file.status === "unsafe") {
|
|
51
|
+
unsafe.push(relPath);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (file.status !== "ok") continue;
|
|
55
|
+
try {
|
|
56
|
+
const { data } = parseConfig(file.content, relPath);
|
|
57
|
+
if (data.hooks && typeof data.hooks === "object" && /geloforge/i.test(JSON.stringify(data.hooks))) {
|
|
58
|
+
configured.push(label);
|
|
59
|
+
}
|
|
60
|
+
} catch {
|
|
61
|
+
// Ignore malformed JSON here; dedicated config tools surface syntax diagnostics
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return { configured, unsafe };
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Runs deterministic read-only project diagnostics and returns structured check results with exact fix commands.
|
|
70
|
+
*/
|
|
71
|
+
export async function runProjectDoctor({
|
|
72
|
+
projectDir: rawProjectDir,
|
|
73
|
+
harnessDir: rawHarnessDir = DEFAULT_HARNESS_DIR,
|
|
74
|
+
globalSkillsDir = undefined,
|
|
75
|
+
includeDrafts = false,
|
|
76
|
+
checkMcp = true,
|
|
77
|
+
timeoutMs = 5000,
|
|
78
|
+
} = {}) {
|
|
79
|
+
const harnessDir = path.resolve(rawHarnessDir || DEFAULT_HARNESS_DIR);
|
|
80
|
+
const loaded = loadProjectManifestAndState(rawProjectDir, harnessDir);
|
|
81
|
+
const { projectDir, manifest, projectPaths } = loaded;
|
|
82
|
+
const checks = [];
|
|
83
|
+
|
|
84
|
+
// 1. Node.js runtime
|
|
85
|
+
const nodeVersion = process.versions?.node || "0.0.0";
|
|
86
|
+
const nodeMajor = Number.parseInt(nodeVersion.split(".")[0], 10);
|
|
87
|
+
checks.push({
|
|
88
|
+
id: "node_runtime",
|
|
89
|
+
status: Number.isFinite(nodeMajor) && nodeMajor >= 20 ? "pass" : "fail",
|
|
90
|
+
summary:
|
|
91
|
+
nodeMajor >= 20
|
|
92
|
+
? `Node.js v${nodeVersion} satisfies >= 20.0.0`
|
|
93
|
+
: `Node.js v${nodeVersion} is below minimum 20.0.0`,
|
|
94
|
+
...(nodeMajor >= 20 ? {} : { fix: "Install Node.js 20+ (LTS) before running GeloForge" }),
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// 2. Second Brain project validation
|
|
98
|
+
const validation = validateProject(projectDir, { harnessDir });
|
|
99
|
+
if (!validation.valid) {
|
|
100
|
+
checks.push({
|
|
101
|
+
id: "project_validation",
|
|
102
|
+
status: "fail",
|
|
103
|
+
summary: `Project validation failed with ${validation.errors.length} error(s)`,
|
|
104
|
+
details: validation.errors.slice(0, 5),
|
|
105
|
+
fix: "geloforge validate",
|
|
106
|
+
});
|
|
107
|
+
} else if (validation.warnings.length > 0) {
|
|
108
|
+
checks.push({
|
|
109
|
+
id: "project_validation",
|
|
110
|
+
status: "warn",
|
|
111
|
+
summary: `Project valid (${validation.recordCount} records) with ${validation.warnings.length} warning(s)`,
|
|
112
|
+
details: validation.warnings.slice(0, 5),
|
|
113
|
+
fix: "geloforge validate",
|
|
114
|
+
});
|
|
115
|
+
} else {
|
|
116
|
+
checks.push({
|
|
117
|
+
id: "project_validation",
|
|
118
|
+
status: "pass",
|
|
119
|
+
summary: `Project valid (${validation.recordCount} records, ${validation.checkedMarkdownFiles} markdown files)`,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// 3. Harness compatibility & version currency
|
|
124
|
+
const compatConfig = loadCompatibilityConfig();
|
|
125
|
+
const compatResult = checkManifestCompatibility(manifest, compatConfig);
|
|
126
|
+
const manifestHarnessVersion = manifest?.harness_version || "unknown";
|
|
127
|
+
if (!compatResult.compatible) {
|
|
128
|
+
checks.push({
|
|
129
|
+
id: "harness_compatibility",
|
|
130
|
+
status: "fail",
|
|
131
|
+
summary: `Incompatible manifest (${compatResult.errors.join("; ")})`,
|
|
132
|
+
fix: "geloforge validate",
|
|
133
|
+
});
|
|
134
|
+
} else if (manifestHarnessVersion !== compatConfig.harness_version || compatResult.warnings.length > 0) {
|
|
135
|
+
checks.push({
|
|
136
|
+
id: "harness_compatibility",
|
|
137
|
+
status: "warn",
|
|
138
|
+
summary: `Project manifest harness_version "${manifestHarnessVersion}" is older than harness v${compatConfig.harness_version}`,
|
|
139
|
+
details: compatResult.warnings,
|
|
140
|
+
fix: `Update brain/manifest.yaml harness_version to "${compatConfig.harness_version}" and run geloforge validate`,
|
|
141
|
+
});
|
|
142
|
+
} else {
|
|
143
|
+
checks.push({
|
|
144
|
+
id: "harness_compatibility",
|
|
145
|
+
status: "pass",
|
|
146
|
+
summary: `Manifest matches harness v${compatConfig.harness_version} (schema ${manifest.schema_version})`,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// 4. Git repository state
|
|
151
|
+
const gitState = getGitStatusScoped(projectDir);
|
|
152
|
+
if (!gitState.available) {
|
|
153
|
+
checks.push({
|
|
154
|
+
id: "git_repository",
|
|
155
|
+
status: "warn",
|
|
156
|
+
summary: "Project is not inside a Git repository (diff-scoped context and worktrees unavailable)",
|
|
157
|
+
fix: "git init",
|
|
158
|
+
});
|
|
159
|
+
} else if (gitState.dirty) {
|
|
160
|
+
checks.push({
|
|
161
|
+
id: "git_repository",
|
|
162
|
+
status: "warn",
|
|
163
|
+
summary: `Git branch "${gitState.branch || "HEAD"}" has ${gitState.dirtyCount} uncommitted change(s)`,
|
|
164
|
+
});
|
|
165
|
+
} else {
|
|
166
|
+
checks.push({
|
|
167
|
+
id: "git_repository",
|
|
168
|
+
status: "pass",
|
|
169
|
+
summary: `Clean Git working tree on branch "${gitState.branch || "HEAD"}"`,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// 5. Task leases
|
|
174
|
+
const leaseInfo = listTaskLeases({ projectDir, harnessDir, includeExpired: true });
|
|
175
|
+
if (leaseInfo.expiredLeases.length > 0) {
|
|
176
|
+
checks.push({
|
|
177
|
+
id: "task_leases",
|
|
178
|
+
status: "warn",
|
|
179
|
+
summary: `${leaseInfo.activeLeases.length} active lease(s), ${leaseInfo.expiredLeases.length} expired lease(s)`,
|
|
180
|
+
fix: "geloforge task leases --include-expired",
|
|
181
|
+
});
|
|
182
|
+
} else {
|
|
183
|
+
checks.push({
|
|
184
|
+
id: "task_leases",
|
|
185
|
+
status: "pass",
|
|
186
|
+
summary: `${leaseInfo.activeLeases.length} active lease(s), 0 expired`,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 6. Portable skills & quarantined drafts
|
|
191
|
+
const discovery = discoverSkills({
|
|
192
|
+
projectDir,
|
|
193
|
+
globalSkillsDir,
|
|
194
|
+
proposalsDir: projectPaths.proposals_dir,
|
|
195
|
+
includeDrafts,
|
|
196
|
+
});
|
|
197
|
+
const lint = lintDiscoveredSkills(discovery, { strict: false, includeDrafts });
|
|
198
|
+
if (!lint.passed) {
|
|
199
|
+
checks.push({
|
|
200
|
+
id: "skills_health",
|
|
201
|
+
status: "fail",
|
|
202
|
+
summary: `Skills lint failed (critical=${lint.counts.critical}, error=${lint.counts.error})`,
|
|
203
|
+
fix: `geloforge skills lint${includeDrafts ? " --include-drafts" : ""}`,
|
|
204
|
+
});
|
|
205
|
+
} else if (lint.counts.high > 0 || lint.counts.warning > 0 || discovery.quarantinedSkills.length > 0) {
|
|
206
|
+
checks.push({
|
|
207
|
+
id: "skills_health",
|
|
208
|
+
status: "warn",
|
|
209
|
+
summary: `${discovery.activeSkills.length} active skill(s), ${discovery.quarantinedSkills.length} quarantined draft(s), warnings=${lint.counts.warning}, high=${lint.counts.high}`,
|
|
210
|
+
fix: "geloforge skills lint --include-drafts",
|
|
211
|
+
});
|
|
212
|
+
} else {
|
|
213
|
+
checks.push({
|
|
214
|
+
id: "skills_health",
|
|
215
|
+
status: "pass",
|
|
216
|
+
summary: `${discovery.activeSkills.length} active skill(s), 0 lint errors`,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// 7. MCP tool-surface lock
|
|
221
|
+
const lockAbs = assertSafeWithinRoot(projectDir, MCP_LOCK_PATH);
|
|
222
|
+
const lockStat = fs.lstatSync(lockAbs, { throwIfNoEntry: false });
|
|
223
|
+
if (lockStat && (lockStat.isSymbolicLink() || !lockStat.isFile())) {
|
|
224
|
+
checks.push({
|
|
225
|
+
id: "mcp_lock",
|
|
226
|
+
status: "fail",
|
|
227
|
+
summary: `Unsafe symbolic link or non-regular file at ${MCP_LOCK_PATH}`,
|
|
228
|
+
fix: `Remove ${MCP_LOCK_PATH} symlink/non-file and run geloforge mcp lock`,
|
|
229
|
+
});
|
|
230
|
+
} else if (!lockStat) {
|
|
231
|
+
checks.push({
|
|
232
|
+
id: "mcp_lock",
|
|
233
|
+
status: "warn",
|
|
234
|
+
summary: `MCP tool lockfile not present (${MCP_LOCK_PATH})`,
|
|
235
|
+
fix: "geloforge mcp lock",
|
|
236
|
+
});
|
|
237
|
+
} else if (!checkMcp) {
|
|
238
|
+
checks.push({
|
|
239
|
+
id: "mcp_lock",
|
|
240
|
+
status: "pass",
|
|
241
|
+
summary: `MCP lockfile present at ${MCP_LOCK_PATH} (live server probe skipped)`,
|
|
242
|
+
});
|
|
243
|
+
} else {
|
|
244
|
+
try {
|
|
245
|
+
const mcpCheck = await checkMcpLock({ projectDir, harnessDir, timeoutMs });
|
|
246
|
+
if (mcpCheck.passed) {
|
|
247
|
+
checks.push({
|
|
248
|
+
id: "mcp_lock",
|
|
249
|
+
status: "pass",
|
|
250
|
+
summary: `MCP lock check passed (${mcpCheck.checkedCount} pinned server(s))`,
|
|
251
|
+
});
|
|
252
|
+
} else {
|
|
253
|
+
checks.push({
|
|
254
|
+
id: "mcp_lock",
|
|
255
|
+
status: "fail",
|
|
256
|
+
summary: `MCP tool-surface drift or probe error detected (added=${mcpCheck.addedServers.length}, removed=${mcpCheck.removedServers.length}, drifted=${mcpCheck.driftedServers.length}, errors=${mcpCheck.errors.length})`,
|
|
257
|
+
fix: "geloforge mcp lock --check",
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
} catch (err) {
|
|
261
|
+
checks.push({
|
|
262
|
+
id: "mcp_lock",
|
|
263
|
+
status: "fail",
|
|
264
|
+
summary: redactSecretContent(err.message),
|
|
265
|
+
fix: "geloforge mcp lock --check",
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// 8. Ambient Git / IDE hooks
|
|
271
|
+
const hookStatus = inspectConfiguredHooks(projectDir);
|
|
272
|
+
if (hookStatus.unsafe.length > 0) {
|
|
273
|
+
checks.push({
|
|
274
|
+
id: "hooks_configured",
|
|
275
|
+
status: "fail",
|
|
276
|
+
summary: `Unsafe symbolic link in hook path(s): ${hookStatus.unsafe.join(", ")}`,
|
|
277
|
+
fix: "Replace symlinked hook config files with regular files before running geloforge hooks init",
|
|
278
|
+
});
|
|
279
|
+
} else if (hookStatus.configured.length > 0) {
|
|
280
|
+
checks.push({
|
|
281
|
+
id: "hooks_configured",
|
|
282
|
+
status: "pass",
|
|
283
|
+
summary: `Configured ambient hooks: ${hookStatus.configured.join(", ")}`,
|
|
284
|
+
});
|
|
285
|
+
} else {
|
|
286
|
+
checks.push({
|
|
287
|
+
id: "hooks_configured",
|
|
288
|
+
status: "warn",
|
|
289
|
+
summary: "No ambient Git or IDE GeloForge hooks configured yet",
|
|
290
|
+
fix: "geloforge hooks init --dry-run",
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
const counts = { pass: 0, warn: 0, fail: 0 };
|
|
295
|
+
for (const c of checks) {
|
|
296
|
+
counts[c.status] = (counts[c.status] || 0) + 1;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const nextActions = [];
|
|
300
|
+
const seenActions = new Set();
|
|
301
|
+
for (const severity of ["fail", "warn"]) {
|
|
302
|
+
for (const c of checks) {
|
|
303
|
+
if (c.status === severity && c.fix && !seenActions.has(c.fix)) {
|
|
304
|
+
seenActions.add(c.fix);
|
|
305
|
+
nextActions.push(c.fix);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return {
|
|
311
|
+
projectDir,
|
|
312
|
+
projectId: manifest?.project_id || null,
|
|
313
|
+
harnessVersion: compatConfig.harness_version,
|
|
314
|
+
healthy: counts.fail === 0,
|
|
315
|
+
counts,
|
|
316
|
+
checks,
|
|
317
|
+
nextActions,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lightweight, opt-in local hook installer (`geloforge hooks init`) for Git pre-commit,
|
|
3
|
+
* Claude Code, and Gemini CLI. Zero external dependencies, zero background daemons,
|
|
4
|
+
* safe non-destructive merging, and strict symlink/path confinement.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import fs from "node:fs";
|
|
8
|
+
import path from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
import { parseConfig } from "./mcp-init.mjs";
|
|
11
|
+
import { assertSafeWithinRoot } from "./path-safety.mjs";
|
|
12
|
+
import { loadProjectManifestAndState } from "./project-locator.mjs";
|
|
13
|
+
import { runMultiFileTransaction, withProjectLock } from "./project-lock.mjs";
|
|
14
|
+
import { assertSafeAncestorChain } from "./skills-history.mjs";
|
|
15
|
+
|
|
16
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
17
|
+
const __dirname = path.dirname(__filename);
|
|
18
|
+
const DEFAULT_HARNESS_DIR = path.resolve(__dirname, "..");
|
|
19
|
+
|
|
20
|
+
export const HOOK_TARGETS = ["git", "claude", "gemini"];
|
|
21
|
+
const GIT_START = "# >>> geloforge:hooks-init:start";
|
|
22
|
+
const GIT_END = "# <<< geloforge:hooks-init:end";
|
|
23
|
+
const AGENT_DOCTOR_COMMAND = "npx geloforge doctor --skip-mcp-check";
|
|
24
|
+
|
|
25
|
+
function normalizeTargets(rawTargets) {
|
|
26
|
+
if (!rawTargets || rawTargets.length === 0) {
|
|
27
|
+
return [...HOOK_TARGETS];
|
|
28
|
+
}
|
|
29
|
+
const items = Array.isArray(rawTargets) ? rawTargets : [rawTargets];
|
|
30
|
+
const parsed = items
|
|
31
|
+
.flatMap((entry) => String(entry).split(","))
|
|
32
|
+
.map((s) => s.trim().toLowerCase())
|
|
33
|
+
.filter(Boolean);
|
|
34
|
+
if (parsed.length === 0) {
|
|
35
|
+
return [...HOOK_TARGETS];
|
|
36
|
+
}
|
|
37
|
+
const unique = [...new Set(parsed)];
|
|
38
|
+
for (const target of unique) {
|
|
39
|
+
if (!HOOK_TARGETS.includes(target)) {
|
|
40
|
+
throw new Error(`Unknown hook target "${target}". Expected: ${HOOK_TARGETS.join(", ")}.`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return unique;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function readExistingHookFile(projectDir, relPath) {
|
|
47
|
+
const filePath = assertSafeWithinRoot(projectDir, relPath);
|
|
48
|
+
assertSafeAncestorChain(filePath, projectDir, relPath);
|
|
49
|
+
const stat = fs.lstatSync(filePath, { throwIfNoEntry: false });
|
|
50
|
+
if (stat && (stat.isSymbolicLink() || !stat.isFile())) {
|
|
51
|
+
throw new Error(`Refusing symbolic link or non-regular file at ${relPath}.`);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
filePath,
|
|
55
|
+
relPath,
|
|
56
|
+
original: stat ? fs.readFileSync(filePath, "utf8") : null,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function buildGitPreCommitBlock(eol) {
|
|
61
|
+
return [
|
|
62
|
+
GIT_START,
|
|
63
|
+
"if command -v geloforge >/dev/null 2>&1; then",
|
|
64
|
+
' GELOFORGE_BIN="geloforge"',
|
|
65
|
+
'elif [ -x "./node_modules/.bin/geloforge" ]; then',
|
|
66
|
+
' GELOFORGE_BIN="./node_modules/.bin/geloforge"',
|
|
67
|
+
"elif command -v npx >/dev/null 2>&1 && npx --no-install geloforge --version >/dev/null 2>&1; then",
|
|
68
|
+
' GELOFORGE_BIN="npx --no-install geloforge"',
|
|
69
|
+
"else",
|
|
70
|
+
' GELOFORGE_BIN=""',
|
|
71
|
+
"fi",
|
|
72
|
+
'if [ -n "$GELOFORGE_BIN" ]; then',
|
|
73
|
+
" $GELOFORGE_BIN validate || exit 1",
|
|
74
|
+
' if [ -f ".geloforge/mcp-lock.json" ]; then',
|
|
75
|
+
" $GELOFORGE_BIN mcp lock --check || exit 1",
|
|
76
|
+
" fi",
|
|
77
|
+
"else",
|
|
78
|
+
' echo "[geloforge] pre-commit skipped: geloforge binary not found in PATH or node_modules." >&2',
|
|
79
|
+
"fi",
|
|
80
|
+
GIT_END,
|
|
81
|
+
].join(eol);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function planGitHookEdit(projectDir) {
|
|
85
|
+
const gitAbs = assertSafeWithinRoot(projectDir, ".git");
|
|
86
|
+
const gitStat = fs.lstatSync(gitAbs, { throwIfNoEntry: false });
|
|
87
|
+
if (!gitStat || gitStat.isSymbolicLink() || !gitStat.isDirectory()) {
|
|
88
|
+
return {
|
|
89
|
+
target: "git",
|
|
90
|
+
path: ".git/hooks/pre-commit",
|
|
91
|
+
action: "skipped",
|
|
92
|
+
reason: "Not a real Git repository directory",
|
|
93
|
+
write: null,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const hooksDirAbs = assertSafeWithinRoot(projectDir, ".git/hooks");
|
|
98
|
+
const hooksStat = fs.lstatSync(hooksDirAbs, { throwIfNoEntry: false });
|
|
99
|
+
if (hooksStat && (hooksStat.isSymbolicLink() || !hooksStat.isDirectory())) {
|
|
100
|
+
throw new Error("Refusing symbolic link or non-directory .git/hooks path.");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const file = readExistingHookFile(projectDir, ".git/hooks/pre-commit");
|
|
104
|
+
const original = file.original;
|
|
105
|
+
const eol = original && original.includes("\r\n") ? "\r\n" : "\n";
|
|
106
|
+
const block = buildGitPreCommitBlock(eol);
|
|
107
|
+
|
|
108
|
+
let nextContent;
|
|
109
|
+
if (original === null) {
|
|
110
|
+
nextContent = `#!/usr/bin/env sh${eol}${eol}${block}${eol}`;
|
|
111
|
+
} else {
|
|
112
|
+
const startIdx = original.indexOf(GIT_START);
|
|
113
|
+
const endIdx = original.indexOf(GIT_END);
|
|
114
|
+
if (startIdx === -1 && endIdx === -1) {
|
|
115
|
+
const trimmed = original.replace(/[\r\n]+$/, "");
|
|
116
|
+
nextContent = trimmed.length > 0 ? `${trimmed}${eol}${eol}${block}${eol}` : `#!/usr/bin/env sh${eol}${eol}${block}${eol}`;
|
|
117
|
+
} else if (
|
|
118
|
+
startIdx === -1 ||
|
|
119
|
+
endIdx < startIdx ||
|
|
120
|
+
original.lastIndexOf(GIT_START) !== startIdx ||
|
|
121
|
+
original.lastIndexOf(GIT_END) !== endIdx
|
|
122
|
+
) {
|
|
123
|
+
throw new Error("Invalid GeloForge hook markers in .git/hooks/pre-commit.");
|
|
124
|
+
} else {
|
|
125
|
+
nextContent = original.slice(0, startIdx) + block + original.slice(endIdx + GIT_END.length);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const action = original === nextContent ? "unchanged" : original === null ? "create" : "update";
|
|
130
|
+
return {
|
|
131
|
+
target: "git",
|
|
132
|
+
path: file.relPath,
|
|
133
|
+
action,
|
|
134
|
+
write:
|
|
135
|
+
action === "unchanged"
|
|
136
|
+
? null
|
|
137
|
+
: { label: "git-hook", filePath: file.filePath, content: nextContent, mode: 0o755 },
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function hasGeloForgeHookEntry(list) {
|
|
142
|
+
return list.some((entry) => {
|
|
143
|
+
if (!entry || typeof entry !== "object") return false;
|
|
144
|
+
if (typeof entry.command === "string" && /\bgeloforge\b/i.test(entry.command)) return true;
|
|
145
|
+
if (Array.isArray(entry.hooks)) {
|
|
146
|
+
return entry.hooks.some((h) => h && typeof h.command === "string" && /\bgeloforge\b/i.test(h.command));
|
|
147
|
+
}
|
|
148
|
+
return false;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function planAgentHookEdit(projectDir, target, relPath, eventKey) {
|
|
153
|
+
const file = readExistingHookFile(projectDir, relPath);
|
|
154
|
+
const original = file.original;
|
|
155
|
+
let data = {};
|
|
156
|
+
if (original !== null && original.trim().length > 0) {
|
|
157
|
+
data = parseConfig(original, relPath).data;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let hooksObj = {};
|
|
161
|
+
if (Object.hasOwn(data, "hooks")) {
|
|
162
|
+
if (!data.hooks || Array.isArray(data.hooks) || typeof data.hooks !== "object") {
|
|
163
|
+
throw new Error(`Expected "hooks" to be a JSON object in ${relPath}.`);
|
|
164
|
+
}
|
|
165
|
+
hooksObj = { ...data.hooks };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
let eventList = [];
|
|
169
|
+
if (Object.hasOwn(hooksObj, eventKey)) {
|
|
170
|
+
if (!Array.isArray(hooksObj[eventKey])) {
|
|
171
|
+
throw new Error(`Expected "hooks.${eventKey}" to be an array in ${relPath}.`);
|
|
172
|
+
}
|
|
173
|
+
eventList = [...hooksObj[eventKey]];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (hasGeloForgeHookEntry(eventList)) {
|
|
177
|
+
return { target, path: relPath, action: "unchanged", write: null };
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
eventList.push({
|
|
181
|
+
hooks: [{ type: "command", command: AGENT_DOCTOR_COMMAND }],
|
|
182
|
+
});
|
|
183
|
+
hooksObj[eventKey] = eventList;
|
|
184
|
+
const nextData = { ...data, hooks: hooksObj };
|
|
185
|
+
const eol = original && original.includes("\r\n") ? "\r\n" : "\n";
|
|
186
|
+
const nextContent = `${JSON.stringify(nextData, null, 2).replace(/\n/g, eol)}${eol}`;
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
target,
|
|
190
|
+
path: relPath,
|
|
191
|
+
action: original === null ? "create" : "update",
|
|
192
|
+
write: { label: `${target}-hook`, filePath: file.filePath, content: nextContent },
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Installs or updates opt-in GeloForge verification/advisory hooks for Git, Claude Code, and Gemini CLI.
|
|
198
|
+
*/
|
|
199
|
+
export function initProjectHooks({
|
|
200
|
+
projectDir: rawProjectDir,
|
|
201
|
+
harnessDir: rawHarnessDir = DEFAULT_HARNESS_DIR,
|
|
202
|
+
targets = HOOK_TARGETS,
|
|
203
|
+
dryRun = false,
|
|
204
|
+
} = {}) {
|
|
205
|
+
const resolvedTargets = normalizeTargets(targets);
|
|
206
|
+
const harnessDir = path.resolve(rawHarnessDir || DEFAULT_HARNESS_DIR);
|
|
207
|
+
const loaded = loadProjectManifestAndState(rawProjectDir, harnessDir);
|
|
208
|
+
const { projectDir } = loaded;
|
|
209
|
+
|
|
210
|
+
const run = () => {
|
|
211
|
+
const plans = [];
|
|
212
|
+
|
|
213
|
+
for (const target of resolvedTargets) {
|
|
214
|
+
if (target === "git") {
|
|
215
|
+
plans.push(planGitHookEdit(projectDir));
|
|
216
|
+
} else if (target === "claude") {
|
|
217
|
+
plans.push(planAgentHookEdit(projectDir, "claude", ".claude/settings.json", "Stop"));
|
|
218
|
+
} else if (target === "gemini") {
|
|
219
|
+
plans.push(planAgentHookEdit(projectDir, "gemini", ".gemini/settings.json", "AfterAgent"));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (!dryRun) {
|
|
224
|
+
const writes = plans.map((p) => p.write).filter(Boolean);
|
|
225
|
+
if (writes.length > 0) {
|
|
226
|
+
// Agent settings may sit alongside MCP config; keep pre-image backups in memory rather than writing .tx-journal.json
|
|
227
|
+
runMultiFileTransaction(writes);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
projectDir,
|
|
233
|
+
dryRun: Boolean(dryRun),
|
|
234
|
+
targets: resolvedTargets,
|
|
235
|
+
files: plans.map(({ target, path: fileRel, action, reason }) => ({
|
|
236
|
+
target,
|
|
237
|
+
path: fileRel,
|
|
238
|
+
action,
|
|
239
|
+
...(reason ? { reason } : {}),
|
|
240
|
+
})),
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
return dryRun ? run() : withProjectLock(projectDir, run);
|
|
245
|
+
}
|
package/core/mcp-init.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { loadProjectManifestAndState } from "./project-locator.mjs";
|
|
|
6
6
|
import { runMultiFileTransaction, withProjectLock } from "./project-lock.mjs";
|
|
7
7
|
|
|
8
8
|
const DEFAULT_CLIENTS = ["cursor", "vscode", "claude", "gemini"];
|
|
9
|
-
const
|
|
9
|
+
export const MCP_CLIENTS = {
|
|
10
10
|
cursor: {
|
|
11
11
|
config: ".cursor/mcp.json", key: "mcpServers",
|
|
12
12
|
rules: ".cursor/rules/geloforge.mdc", frontmatter: "---\nalwaysApply: true\n---\n\n",
|
|
@@ -21,6 +21,7 @@ const CLIENTS = {
|
|
|
21
21
|
gemini: { config: ".gemini/settings.json", key: "mcpServers", rules: "GEMINI.md", appendRules: true },
|
|
22
22
|
antigravity: { config: ".agents/mcp_config.json", key: "mcpServers", rules: "AGENTS.md", appendRules: true },
|
|
23
23
|
};
|
|
24
|
+
const CLIENTS = MCP_CLIENTS;
|
|
24
25
|
|
|
25
26
|
const START = "<!-- geloforge:mcp-init:start -->";
|
|
26
27
|
const END = "<!-- geloforge:mcp-init:end -->";
|
|
@@ -33,7 +34,7 @@ function readConfigFile(projectDir, relativePath) {
|
|
|
33
34
|
return { filePath, relativePath, original: stat ? fs.readFileSync(filePath, "utf8") : null, mode: stat ? stat.mode & 0o777 : undefined };
|
|
34
35
|
}
|
|
35
36
|
|
|
36
|
-
function parseConfig(source, filename) {
|
|
37
|
+
export function parseConfig(source, filename) {
|
|
37
38
|
// Keep source offsets so we can edit server fields without rewriting comments and other settings.
|
|
38
39
|
const lexemes = [...source.matchAll(/"(?:[^"\\]|\\[\s\S])*"|\/\/[^\r\n]*|\/\*[\s\S]*?\*\/|[^\s]/g)];
|
|
39
40
|
const tokens = lexemes.filter(([token]) => !token.startsWith("//") && !token.startsWith("/*"));
|