geloforge 0.0.0-stage → 0.7.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/LICENSE +21 -0
- package/README.md +72 -2
- package/THIRD_PARTY_NOTICES.md +33 -0
- package/VERSION +1 -0
- package/adapters/n8n/staging-audit-error-workflow.json +132 -0
- package/core/adopt-project.mjs +458 -0
- package/core/audit-learning.mjs +68 -0
- package/core/audit-provider.mjs +88 -0
- package/core/autonomous-audit.mjs +214 -0
- package/core/checkpoint.mjs +448 -0
- package/core/codebase-map.mjs +886 -0
- package/core/compatibility.json +16 -0
- package/core/compatibility.mjs +123 -0
- package/core/context-cache.mjs +113 -0
- package/core/context-compiler.mjs +1315 -0
- package/core/evidence-capture.mjs +347 -0
- package/core/git-utils.mjs +167 -0
- package/core/handoff.mjs +815 -0
- package/core/json-schema-lite.mjs +119 -0
- package/core/mcp-init.mjs +251 -0
- package/core/mcp-server.mjs +915 -0
- package/core/operation-approval.mjs +102 -0
- package/core/path-safety.mjs +508 -0
- package/core/project-locator.mjs +231 -0
- package/core/project-lock.mjs +438 -0
- package/core/project-status.mjs +635 -0
- package/core/project-validator.mjs +544 -0
- package/core/promotion.mjs +90 -0
- package/core/runtime-files.mjs +40 -0
- package/core/skills-history.mjs +572 -0
- package/core/skills-lifecycle.mjs +2225 -0
- package/core/skills-lint.mjs +365 -0
- package/core/skills.mjs +1449 -0
- package/core/staging-audit.mjs +454 -0
- package/core/staging-evidence.mjs +127 -0
- package/core/task-manager.mjs +1044 -0
- package/core/template-utils.mjs +190 -0
- package/core/text-utils.mjs +48 -0
- package/core/verify-task.mjs +48 -0
- package/core/worktrees.mjs +64 -0
- package/core/yaml-subset.mjs +572 -0
- package/package.json +50 -4
- package/policies/approval-boundaries.md +42 -0
- package/policies/memory-policy.md +53 -0
- package/policies/token-efficiency.md +30 -0
- package/schemas/README.md +43 -0
- package/schemas/adr.schema.json +64 -0
- package/schemas/agent-handoff.schema.json +139 -0
- package/schemas/approval-record.schema.json +110 -0
- package/schemas/conflict-record.schema.json +60 -0
- package/schemas/context-pack-manifest.schema.json +194 -0
- package/schemas/evidence-manifest.schema.json +83 -0
- package/schemas/incident-record.schema.json +53 -0
- package/schemas/project-manifest.schema.json +90 -0
- package/schemas/project-state.schema.json +64 -0
- package/schemas/recovery-checkpoint.schema.json +49 -0
- package/schemas/research-record.schema.json +76 -0
- package/schemas/task-contract.schema.json +95 -0
- package/scripts/bootstrap-project.mjs +357 -0
- package/scripts/geloforge.mjs +2512 -0
- package/templates/profiles/README.md +19 -0
- package/templates/profiles/ai-system/brain/profile.md +21 -0
- package/templates/profiles/ai-system/gitignore.append +5 -0
- package/templates/profiles/ai-system/profile.json +6 -0
- package/templates/profiles/automation/brain/profile.md +19 -0
- package/templates/profiles/automation/gitignore.append +5 -0
- package/templates/profiles/automation/profile.json +6 -0
- package/templates/profiles/desktop/brain/profile.md +21 -0
- package/templates/profiles/desktop/gitignore.append +7 -0
- package/templates/profiles/desktop/profile.json +6 -0
- package/templates/profiles/mobile/brain/profile.md +20 -0
- package/templates/profiles/mobile/gitignore.append +8 -0
- package/templates/profiles/mobile/profile.json +6 -0
- package/templates/profiles/web/brain/profile.md +21 -0
- package/templates/profiles/web/gitignore.append +6 -0
- package/templates/profiles/web/profile.json +6 -0
- package/templates/project-starter/AGENTS.md +27 -0
- package/templates/project-starter/GEMINI.md +16 -0
- package/templates/project-starter/README.md +26 -0
- package/templates/project-starter/brain/INDEX.md +40 -0
- package/templates/project-starter/brain/architecture/overview.md +30 -0
- package/templates/project-starter/brain/decisions/.gitkeep +0 -0
- package/templates/project-starter/brain/manifest.yaml +36 -0
- package/templates/project-starter/brain/requirements/constraints.md +29 -0
- package/templates/project-starter/brain/requirements/prd.md +37 -0
- package/templates/project-starter/brain/state/current.yaml +19 -0
- package/templates/project-starter/gitignore.template +25 -0
- package/templates/project-starter/src/.gitkeep +0 -0
- package/templates/project-starter/tasks/.gitkeep +0 -0
- package/templates/project-starter/tests/.gitkeep +0 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Zero-dependency JSON Schema Draft 2020-12 subset validator.
|
|
3
|
+
* Supports: type (single or union array), const, enum, pattern, minLength, maxLength,
|
|
4
|
+
* minimum, maximum, required, properties, additionalProperties, items, minItems, uniqueItems.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
function getJsonType(value) {
|
|
8
|
+
if (value === null) {
|
|
9
|
+
return "null";
|
|
10
|
+
}
|
|
11
|
+
if (Array.isArray(value)) {
|
|
12
|
+
return "array";
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === "number") {
|
|
15
|
+
return Number.isInteger(value) ? "integer" : "number";
|
|
16
|
+
}
|
|
17
|
+
return typeof value;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function matchesType(value, expectedType) {
|
|
21
|
+
const actual = getJsonType(value);
|
|
22
|
+
if (expectedType === "number") {
|
|
23
|
+
return actual === "number" || actual === "integer";
|
|
24
|
+
}
|
|
25
|
+
return actual === expectedType;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function validateNode(value, schema, jsonPath, errors) {
|
|
29
|
+
if (schema.type !== undefined) {
|
|
30
|
+
const allowed = Array.isArray(schema.type) ? schema.type : [schema.type];
|
|
31
|
+
const ok = allowed.some((t) => matchesType(value, t));
|
|
32
|
+
if (!ok) {
|
|
33
|
+
errors.push(`${jsonPath}: expected type ${allowed.join("|")}, got ${getJsonType(value)}`);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (schema.const !== undefined && value !== schema.const) {
|
|
39
|
+
errors.push(`${jsonPath}: expected constant ${JSON.stringify(schema.const)}, got ${JSON.stringify(value)}`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (Array.isArray(schema.enum)) {
|
|
43
|
+
const matched = schema.enum.some((opt) => opt === value);
|
|
44
|
+
if (!matched) {
|
|
45
|
+
errors.push(`${jsonPath}: value ${JSON.stringify(value)} not in enum [${schema.enum.join(", ")}]`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (typeof value === "string") {
|
|
50
|
+
if (schema.minLength !== undefined && value.length < schema.minLength) {
|
|
51
|
+
errors.push(`${jsonPath}: string length ${value.length} < minLength ${schema.minLength}`);
|
|
52
|
+
}
|
|
53
|
+
if (schema.maxLength !== undefined && value.length > schema.maxLength) {
|
|
54
|
+
errors.push(`${jsonPath}: string length ${value.length} > maxLength ${schema.maxLength}`);
|
|
55
|
+
}
|
|
56
|
+
if (schema.pattern !== undefined) {
|
|
57
|
+
const re = new RegExp(schema.pattern, "u");
|
|
58
|
+
if (!re.test(value)) {
|
|
59
|
+
errors.push(`${jsonPath}: "${value}" does not match pattern ${schema.pattern}`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (typeof value === "number") {
|
|
65
|
+
if (schema.minimum !== undefined && value < schema.minimum) {
|
|
66
|
+
errors.push(`${jsonPath}: ${value} < minimum ${schema.minimum}`);
|
|
67
|
+
}
|
|
68
|
+
if (schema.maximum !== undefined && value > schema.maximum) {
|
|
69
|
+
errors.push(`${jsonPath}: ${value} > maximum ${schema.maximum}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (Array.isArray(value)) {
|
|
74
|
+
if (schema.minItems !== undefined && value.length < schema.minItems) {
|
|
75
|
+
errors.push(`${jsonPath}: array length ${value.length} < minItems ${schema.minItems}`);
|
|
76
|
+
}
|
|
77
|
+
if (schema.uniqueItems === true) {
|
|
78
|
+
const seen = new Set(value.map((v) => JSON.stringify(v)));
|
|
79
|
+
if (seen.size !== value.length) {
|
|
80
|
+
errors.push(`${jsonPath}: array items must be unique`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (schema.items !== undefined) {
|
|
84
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
85
|
+
validateNode(value[i], schema.items, `${jsonPath}[${i}]`, errors);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
|
|
91
|
+
if (Array.isArray(schema.required)) {
|
|
92
|
+
for (const reqKey of schema.required) {
|
|
93
|
+
if (!Object.hasOwn(value, reqKey)) {
|
|
94
|
+
errors.push(`${jsonPath}: missing required property "${reqKey}"`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const props = schema.properties || {};
|
|
100
|
+
for (const [key, propVal] of Object.entries(value)) {
|
|
101
|
+
if (Object.hasOwn(props, key)) {
|
|
102
|
+
validateNode(propVal, props[key], `${jsonPath}.${key}`, errors);
|
|
103
|
+
} else if (schema.additionalProperties === false) {
|
|
104
|
+
errors.push(`${jsonPath}: unexpected property "${key}" (additionalProperties: false)`);
|
|
105
|
+
} else if (typeof schema.additionalProperties === "object" && schema.additionalProperties !== null) {
|
|
106
|
+
validateNode(propVal, schema.additionalProperties, `${jsonPath}.${key}`, errors);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function validateAgainstSchema(data, schema) {
|
|
113
|
+
const errors = [];
|
|
114
|
+
validateNode(data, schema, "$", errors);
|
|
115
|
+
return {
|
|
116
|
+
valid: errors.length === 0,
|
|
117
|
+
errors,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isDeepStrictEqual } from "node:util";
|
|
4
|
+
import { assertSafeWithinRoot, isForbiddenSecretPath } from "./path-safety.mjs";
|
|
5
|
+
import { loadProjectManifestAndState } from "./project-locator.mjs";
|
|
6
|
+
import { runMultiFileTransaction, withProjectLock } from "./project-lock.mjs";
|
|
7
|
+
|
|
8
|
+
const DEFAULT_CLIENTS = ["cursor", "vscode", "claude", "gemini"];
|
|
9
|
+
const CLIENTS = {
|
|
10
|
+
cursor: {
|
|
11
|
+
config: ".cursor/mcp.json", key: "mcpServers",
|
|
12
|
+
rules: ".cursor/rules/geloforge.mdc", frontmatter: "---\nalwaysApply: true\n---\n\n",
|
|
13
|
+
},
|
|
14
|
+
vscode: {
|
|
15
|
+
config: ".vscode/mcp.json", key: "servers", type: "stdio",
|
|
16
|
+
rules: ".github/instructions/geloforge.instructions.md", frontmatter: "---\napplyTo: '**'\n---\n\n",
|
|
17
|
+
},
|
|
18
|
+
claude: {
|
|
19
|
+
config: ".mcp.json", key: "mcpServers", type: "stdio", rules: ".claude/rules/geloforge.md",
|
|
20
|
+
},
|
|
21
|
+
gemini: { config: ".gemini/settings.json", key: "mcpServers", rules: "GEMINI.md", appendRules: true },
|
|
22
|
+
antigravity: { config: ".agents/mcp_config.json", key: "mcpServers", rules: "AGENTS.md", appendRules: true },
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const START = "<!-- geloforge:mcp-init:start -->";
|
|
26
|
+
const END = "<!-- geloforge:mcp-init:end -->";
|
|
27
|
+
const RELEASE_ARCHIVE = /^https:\/\/github\.com\/63L0\/GeloForge\/releases\/download\/v(\d+\.\d+\.\d+)\/geloforge-\1\.tgz$/;
|
|
28
|
+
|
|
29
|
+
function readConfigFile(projectDir, relativePath) {
|
|
30
|
+
const filePath = assertSafeWithinRoot(projectDir, relativePath);
|
|
31
|
+
const stat = fs.lstatSync(filePath, { throwIfNoEntry: false });
|
|
32
|
+
if (stat && !stat.isFile()) throw new Error(`Expected a regular project file at ${relativePath}.`);
|
|
33
|
+
return { filePath, relativePath, original: stat ? fs.readFileSync(filePath, "utf8") : null, mode: stat ? stat.mode & 0o777 : undefined };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function parseConfig(source, filename) {
|
|
37
|
+
// Keep source offsets so we can edit server fields without rewriting comments and other settings.
|
|
38
|
+
const lexemes = [...source.matchAll(/"(?:[^"\\]|\\[\s\S])*"|\/\/[^\r\n]*|\/\*[\s\S]*?\*\/|[^\s]/g)];
|
|
39
|
+
const tokens = lexemes.filter(([token]) => !token.startsWith("//") && !token.startsWith("/*"));
|
|
40
|
+
const trailingCommas = new Set(tokens.filter(([token], index) =>
|
|
41
|
+
token === "," && ["}", "]"].includes(tokens[index + 1]?.[0])));
|
|
42
|
+
let clean = "";
|
|
43
|
+
let offset = 0;
|
|
44
|
+
for (const token of lexemes) {
|
|
45
|
+
const [raw] = token;
|
|
46
|
+
if (raw.startsWith("//") || raw.startsWith("/*") || trailingCommas.has(token)) {
|
|
47
|
+
clean += source.slice(offset, token.index) + raw.replace(/[^\r\n]/g, " ");
|
|
48
|
+
offset = token.index + raw.length;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
clean += source.slice(offset);
|
|
52
|
+
let data;
|
|
53
|
+
try {
|
|
54
|
+
data = JSON.parse(clean.replace(/^\uFEFF/, ""));
|
|
55
|
+
} catch {
|
|
56
|
+
throw new Error(`Invalid JSON in ${filename}. Fix the project configuration and retry.`);
|
|
57
|
+
}
|
|
58
|
+
if (!data || Array.isArray(data) || typeof data !== "object") {
|
|
59
|
+
throw new Error(`Expected a JSON object in ${filename}.`);
|
|
60
|
+
}
|
|
61
|
+
return { tokens, data };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function readObject(tokens, open, key) {
|
|
65
|
+
let index = open + 1;
|
|
66
|
+
let last = null;
|
|
67
|
+
let member = null;
|
|
68
|
+
while (tokens[index][0] !== "}") {
|
|
69
|
+
const name = JSON.parse(tokens[index][0]);
|
|
70
|
+
const value = index + 2;
|
|
71
|
+
let end = value;
|
|
72
|
+
let depth = 0;
|
|
73
|
+
while (end < tokens.length) {
|
|
74
|
+
const [token] = tokens[end];
|
|
75
|
+
if (depth === 0 && [",", "}"].includes(token)) break;
|
|
76
|
+
if (token === "{" || token === "[") depth += 1;
|
|
77
|
+
if (token === "}" || token === "]") depth -= 1;
|
|
78
|
+
end += 1;
|
|
79
|
+
}
|
|
80
|
+
const comma = tokens[end][0] === ",";
|
|
81
|
+
last = { keyToken: tokens[index], value, lastValueToken: tokens[end - 1], comma };
|
|
82
|
+
if (name === key) {
|
|
83
|
+
if (member) throw new Error(`Project MCP configuration contains duplicate ${key} keys.`);
|
|
84
|
+
member = last;
|
|
85
|
+
}
|
|
86
|
+
index = comma ? end + 1 : end;
|
|
87
|
+
}
|
|
88
|
+
return { member, last, close: tokens[index] };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function editProperty(source, filename, keys, value) {
|
|
92
|
+
const { tokens } = parseConfig(source, filename);
|
|
93
|
+
let open = 0;
|
|
94
|
+
for (const key of keys.slice(0, -1)) open = readObject(tokens, open, key).member.value;
|
|
95
|
+
const key = keys.at(-1);
|
|
96
|
+
const { member, last, close } = readObject(tokens, open, key);
|
|
97
|
+
const eol = source.includes("\r\n") ? "\r\n" : "\n";
|
|
98
|
+
const lineStart = source.lastIndexOf("\n", close.index - 1) + 1;
|
|
99
|
+
const closingIndent = source.slice(lineStart, close.index);
|
|
100
|
+
const ownLine = /^\s*$/.test(closingIndent);
|
|
101
|
+
const indent = ownLine ? closingIndent : "";
|
|
102
|
+
const propertyIndent = `${indent} `;
|
|
103
|
+
const rendered = JSON.stringify(value, null, 2).replaceAll("\n", `${eol}${propertyIndent}`);
|
|
104
|
+
if (member) {
|
|
105
|
+
const valueEnd = member.lastValueToken.index + member.lastValueToken[0].length;
|
|
106
|
+
return source.slice(0, tokens[member.value].index) + rendered + source.slice(valueEnd);
|
|
107
|
+
}
|
|
108
|
+
const insert = ownLine ? lineStart : close.index;
|
|
109
|
+
const commaAt = last ? last.lastValueToken.index + last.lastValueToken[0].length : insert;
|
|
110
|
+
const separator = last && !last.comma ? "," : "";
|
|
111
|
+
return source.slice(0, commaAt) + separator + source.slice(commaAt, insert) +
|
|
112
|
+
(ownLine ? "" : eol) + `${propertyIndent}${JSON.stringify(key)}: ${rendered}${eol}` + source.slice(insert);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function isGeloForgeServer(server) {
|
|
116
|
+
if (!server || typeof server.command !== "string" || !Array.isArray(server.args)) return false;
|
|
117
|
+
if (server.type && server.type !== "stdio") return false;
|
|
118
|
+
const command = server.command.split(/[\\/]/).at(-1);
|
|
119
|
+
const args = server.args;
|
|
120
|
+
if (/^geloforge(?:\.cmd)?$/i.test(command)) return args[0] === "mcp";
|
|
121
|
+
if (/^npx(?:\.cmd)?$/i.test(command)) return args.some((arg) => /^geloforge(?:@\d.*)?$/.test(arg)) && args.includes("mcp");
|
|
122
|
+
if (!/^node(?:\.exe)?$/i.test(command)) return false;
|
|
123
|
+
return (args[1] === "mcp" && /(?:^|[\\/])geloforge\.mjs$/.test(args[0])) ||
|
|
124
|
+
(args[1] === "exec" && args.some((arg) => /^geloforge@\d/.test(arg) || RELEASE_ARCHIVE.test(arg)) && args.includes("mcp"));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function updateServer(file, client, server) {
|
|
128
|
+
const filename = file.relativePath;
|
|
129
|
+
const source = file.original ?? "{}\n";
|
|
130
|
+
const { tokens, data } = parseConfig(source, filename);
|
|
131
|
+
if (!Object.hasOwn(data, client.key)) {
|
|
132
|
+
return { data, content: editProperty(source, filename, [client.key], { geloforge: server }) };
|
|
133
|
+
}
|
|
134
|
+
const entries = data[client.key];
|
|
135
|
+
if (!entries || Array.isArray(entries) || typeof entries !== "object") {
|
|
136
|
+
throw new Error(`Expected ${client.key} to be an object in ${filename}.`);
|
|
137
|
+
}
|
|
138
|
+
// Check relevant duplicate keys even when the effective values already match.
|
|
139
|
+
const map = readObject(tokens, 0, client.key).member;
|
|
140
|
+
const target = readObject(tokens, map.value, "geloforge").member;
|
|
141
|
+
if (!target) return { data, content: editProperty(source, filename, [client.key, "geloforge"], server) };
|
|
142
|
+
if (!isGeloForgeServer(entries.geloforge)) {
|
|
143
|
+
throw new Error(`The geloforge server name in ${filename} already belongs to a different command.`);
|
|
144
|
+
}
|
|
145
|
+
let content = source;
|
|
146
|
+
for (const [key, value] of Object.entries(server)) {
|
|
147
|
+
readObject(tokens, target.value, key);
|
|
148
|
+
if (!isDeepStrictEqual(entries.geloforge[key], value)) {
|
|
149
|
+
content = editProperty(content, filename, [client.key, "geloforge", key], value);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return { data, content };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function updateRules(file, { frontmatter = "", appendRules = false, readOnly }) {
|
|
156
|
+
const original = file.original ?? "";
|
|
157
|
+
const eol = original.includes("\r\n") ? "\r\n" : "\n";
|
|
158
|
+
const block = [
|
|
159
|
+
START,
|
|
160
|
+
"## GeloForge workflow",
|
|
161
|
+
"",
|
|
162
|
+
"- At the start of project work, call `geloforge_recommend` and review pending approvals.",
|
|
163
|
+
"- Before auditing, debugging, or making changes, call `geloforge_context` with the task ID or a short query and `skills: true`.",
|
|
164
|
+
"- Follow relevant skills returned with the context. If none match, continue with the project rules. Record any skills used in the handoff.",
|
|
165
|
+
...(readOnly ? [
|
|
166
|
+
"- Use `geloforge_task` and `geloforge_checkpoint` to read task and checkpoint history.",
|
|
167
|
+
] : [
|
|
168
|
+
"- Use `geloforge_task` to find or create the task and claim the intended file scope before editing. Respect lease collisions.",
|
|
169
|
+
"- Use `geloforge_checkpoint` to record progress and verified evidence before handoff.",
|
|
170
|
+
]),
|
|
171
|
+
END,
|
|
172
|
+
].join(eol);
|
|
173
|
+
const start = original.indexOf(START);
|
|
174
|
+
const end = original.indexOf(END);
|
|
175
|
+
if (start === -1 && end === -1) {
|
|
176
|
+
if (original && !appendRules) throw new Error(`Existing rule ${file.relativePath} has no GeloForge section.`);
|
|
177
|
+
return original ? `${original}${eol}${eol}${block}${eol}` : `${frontmatter}${block}${eol}`;
|
|
178
|
+
}
|
|
179
|
+
if (start === -1 || end < start || original.lastIndexOf(START) !== start || original.lastIndexOf(END) !== end) {
|
|
180
|
+
throw new Error(`Invalid GeloForge section markers in ${file.relativePath}.`);
|
|
181
|
+
}
|
|
182
|
+
return original.slice(0, start) + block + original.slice(end + END.length);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function launchCommand({ projectDir, harnessDir, readOnly }) {
|
|
186
|
+
const args = ["mcp", "--project", projectDir];
|
|
187
|
+
if (readOnly) args.push("--read-only");
|
|
188
|
+
const npm = process.env.npm_execpath;
|
|
189
|
+
if (harnessDir.split(path.sep).includes("_npx") && npm && path.isAbsolute(npm) && fs.existsSync(npm)) {
|
|
190
|
+
let installed;
|
|
191
|
+
try {
|
|
192
|
+
const lock = JSON.parse(fs.readFileSync(path.join(harnessDir, "..", "..", "package-lock.json"), "utf8"));
|
|
193
|
+
installed = lock.packages?.["node_modules/geloforge"];
|
|
194
|
+
} catch {
|
|
195
|
+
// An npm cache without installation metadata still has the local CLI.
|
|
196
|
+
}
|
|
197
|
+
const version = installed?.version;
|
|
198
|
+
const archive = `geloforge-${version}.tgz`;
|
|
199
|
+
const npmArchive = `https://registry.npmjs.org/geloforge/-/${archive}`;
|
|
200
|
+
const githubArchive = `https://github.com/63L0/GeloForge/releases/download/v${version}/${archive}`;
|
|
201
|
+
const source = installed?.resolved === npmArchive ? `geloforge@${version}` : installed?.resolved;
|
|
202
|
+
if (/^\d+\.\d+\.\d+$/.test(version) && (installed.resolved === npmArchive || source === githubArchive)) {
|
|
203
|
+
return { command: process.execPath, args: [npm, "exec", "--yes", "--package", source, "--", "geloforge", ...args] };
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return { command: process.execPath, args: [path.join(harnessDir, "scripts", "geloforge.mjs"), ...args] };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function initMcpClients({ projectDir, harnessDir, clients, dryRun = false, readOnly = false }) {
|
|
210
|
+
const names = clients?.length ? [...new Set(clients)] : DEFAULT_CLIENTS;
|
|
211
|
+
for (const name of names) {
|
|
212
|
+
if (!Object.hasOwn(CLIENTS, name)) throw new Error(`Unknown MCP client "${name}". Use cursor, vscode, claude, gemini, or antigravity.`);
|
|
213
|
+
}
|
|
214
|
+
const run = () => {
|
|
215
|
+
loadProjectManifestAndState(projectDir, harnessDir);
|
|
216
|
+
const launch = launchCommand({ projectDir, harnessDir, readOnly });
|
|
217
|
+
const edits = names.flatMap((name) => {
|
|
218
|
+
const client = CLIENTS[name];
|
|
219
|
+
const config = readConfigFile(projectDir, client.config);
|
|
220
|
+
const entry = client.type ? { type: client.type, ...launch } : launch;
|
|
221
|
+
const { data, content } = updateServer(config, client, entry);
|
|
222
|
+
const gemini = name === "gemini";
|
|
223
|
+
const customContext = gemini ? data.context?.fileName : null;
|
|
224
|
+
const contextName = Array.isArray(customContext) ? customContext[0] : customContext;
|
|
225
|
+
const rulesPath = contextName || client.rules;
|
|
226
|
+
if (typeof rulesPath !== "string" || !/\.mdc?$/i.test(rulesPath) || isForbiddenSecretPath(rulesPath)) {
|
|
227
|
+
throw new Error(`Invalid project instruction file name for ${name}.`);
|
|
228
|
+
}
|
|
229
|
+
const rules = readConfigFile(projectDir, rulesPath);
|
|
230
|
+
return [
|
|
231
|
+
{ ...config, content },
|
|
232
|
+
{ ...rules, content: updateRules(rules, { frontmatter: client.frontmatter, appendRules: client.appendRules, readOnly }) },
|
|
233
|
+
];
|
|
234
|
+
});
|
|
235
|
+
const destinations = edits.map(({ filePath }) => process.platform === "win32" ? filePath.toLowerCase() : filePath);
|
|
236
|
+
if (new Set(destinations).size !== edits.length) {
|
|
237
|
+
throw new Error("MCP clients reference the same project instruction file. Select separate instruction files for each client.");
|
|
238
|
+
}
|
|
239
|
+
const files = edits.map(({ relativePath, original, content }) => ({
|
|
240
|
+
path: relativePath,
|
|
241
|
+
action: original === content ? "unchanged" : original === null ? "create" : "update",
|
|
242
|
+
}));
|
|
243
|
+
if (!dryRun) {
|
|
244
|
+
// Configs may contain other servers' credentials. Keep rollback data in memory.
|
|
245
|
+
const writes = edits.filter(({ original, content }) => original !== content);
|
|
246
|
+
runMultiFileTransaction(writes.map(({ filePath, content, mode }) => ({ filePath, content, mode })));
|
|
247
|
+
}
|
|
248
|
+
return { projectDir, clients: names, dryRun, readOnly, files };
|
|
249
|
+
};
|
|
250
|
+
return dryRun ? run() : withProjectLock(projectDir, run);
|
|
251
|
+
}
|