@venizia/vulcan 0.3.5 → 0.3.7
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 +2 -2
- package/base/agents.json +232 -0
- package/base/claude/settings.json +12 -0
- package/base/hooks/guard-package.ts +29 -0
- package/base/rules.md +1 -0
- package/base/templates/AGENTS.md +4 -3
- package/package.json +3 -3
- package/src/agents/atlas-block.ts +54 -0
- package/src/agents/common/constants.ts +38 -0
- package/src/agents/common/index.ts +3 -0
- package/src/agents/common/schema.ts +42 -0
- package/src/agents/common/types.ts +30 -0
- package/src/agents/detect.ts +36 -0
- package/src/agents/exclude.ts +87 -0
- package/src/agents/index.ts +9 -0
- package/src/agents/local.ts +65 -0
- package/src/agents/prompt.ts +22 -0
- package/src/agents/render.ts +229 -0
- package/src/agents/resolve.ts +145 -0
- package/src/agents/table.ts +35 -0
- package/src/commands/agents.command.ts +154 -0
- package/src/commands/doctor.command.ts +16 -2
- package/src/commands/help.command.ts +5 -1
- package/src/commands/index.ts +2 -0
- package/src/commands/sync.command.ts +112 -3
- package/src/common/constants.ts +9 -1
- package/src/config/common/constants.ts +11 -1
- package/src/config/packages.ts +2 -0
- package/src/config/schema.ts +16 -1
- package/src/create/add-app.ts +110 -4
- package/src/create/add-lib.ts +317 -0
- package/src/create/common/constants.ts +3 -0
- package/src/create/common/types.ts +18 -2
- package/src/create/dockerignore.ts +47 -0
- package/src/create/init-monorepo.ts +22 -4
- package/src/create/lib-preset.ts +154 -0
- package/src/create/preset.ts +22 -3
- package/src/create/workspace.ts +94 -3
- package/src/doctor/checks.ts +234 -12
- package/src/doctor/common/constants.ts +26 -0
- package/src/doctor/runner.ts +37 -0
- package/src/hooks/guard-package.ts +211 -0
- package/src/hooks/index.ts +1 -0
- package/src/init/scaffold.ts +8 -5
- package/src/manifest/render.ts +4 -0
- package/src/router.ts +10 -1
- package/src/sync/agent-files.ts +112 -0
- package/src/sync/agent-removal.ts +241 -0
- package/src/sync/atlas-block.ts +55 -0
- package/src/sync/common/constants.ts +2 -0
- package/src/sync/exclude.ts +89 -0
- package/src/sync/index.ts +5 -0
- package/src/sync/local-choice.ts +55 -0
- package/src/sync/planner.ts +55 -19
- package/src/sync/settings.ts +26 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { basename, dirname, isAbsolute, join, normalize, relative, sep } from 'node:path';
|
|
3
|
+
import { GuardDecisions, HookConstants } from './common/index.ts';
|
|
4
|
+
import type { IManifestHookInput } from './common/index.ts';
|
|
5
|
+
|
|
6
|
+
const MANIFEST_FILE = '.agents/vulcan.manifest.json';
|
|
7
|
+
const PACKAGE_FILE = 'package.json';
|
|
8
|
+
const NODE_MODULES = 'node_modules';
|
|
9
|
+
const ADD = 'bun x --no-install vulcan add';
|
|
10
|
+
const NO_HAND_WRITING = 'do not hand-write package.json, tsconfig or scripts';
|
|
11
|
+
|
|
12
|
+
export interface IPackageVerdict {
|
|
13
|
+
/** `deny` only when a real `vulcan add` command exists for the folder; otherwise `warn`. */
|
|
14
|
+
kind: 'deny' | 'warn';
|
|
15
|
+
message: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface IRecordedPreset {
|
|
19
|
+
spec: string;
|
|
20
|
+
side: string;
|
|
21
|
+
lib: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface IProjectView {
|
|
25
|
+
root: string;
|
|
26
|
+
layout: Record<string, string>;
|
|
27
|
+
presets: IRecordedPreset[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const isRecord = (opts: { value: unknown }): opts is { value: Record<string, unknown> } =>
|
|
31
|
+
typeof opts.value === 'object' && opts.value !== null && !Array.isArray(opts.value);
|
|
32
|
+
|
|
33
|
+
const toFolder = (opts: { value: unknown }): string | undefined =>
|
|
34
|
+
typeof opts.value === 'string' && opts.value !== ''
|
|
35
|
+
? normalize(opts.value).replace(/\/+$/, '')
|
|
36
|
+
: undefined;
|
|
37
|
+
|
|
38
|
+
/** The nearest project above `start`: the folder holding the generated manifest. */
|
|
39
|
+
const findRoot = (opts: { start: string }): string | undefined => {
|
|
40
|
+
let current = opts.start;
|
|
41
|
+
|
|
42
|
+
for (;;) {
|
|
43
|
+
if (existsSync(join(current, MANIFEST_FILE))) {
|
|
44
|
+
return current;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const parent = dirname(current);
|
|
48
|
+
|
|
49
|
+
if (parent === current) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
current = parent;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** What the manifest says of layout and presets; `undefined` when it is missing or not readable. */
|
|
58
|
+
const readProject = (opts: { start: string }): IProjectView | undefined => {
|
|
59
|
+
const root = findRoot({ start: opts.start });
|
|
60
|
+
|
|
61
|
+
if (root === undefined) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let manifest: unknown;
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
manifest = JSON.parse(readFileSync(join(root, MANIFEST_FILE), 'utf8'));
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.error(
|
|
71
|
+
`[readProject] unreadable manifest - letting the call through | ${error instanceof Error ? error.message : String(error)}`,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const holder = { value: manifest };
|
|
78
|
+
|
|
79
|
+
if (!isRecord(holder)) {
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const layoutHolder = { value: holder.value['layout'] };
|
|
84
|
+
const layout: Record<string, string> = {};
|
|
85
|
+
|
|
86
|
+
if (isRecord(layoutHolder)) {
|
|
87
|
+
for (const key of ['backend', 'frontend', 'shared']) {
|
|
88
|
+
const folder = toFolder({ value: layoutHolder.value[key] });
|
|
89
|
+
|
|
90
|
+
if (folder !== undefined) {
|
|
91
|
+
layout[key] = folder;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const recorded = Array.isArray(holder.value['presets']) ? holder.value['presets'] : [];
|
|
97
|
+
const presets = recorded.flatMap((item: unknown): IRecordedPreset[] => {
|
|
98
|
+
const entry = { value: item };
|
|
99
|
+
|
|
100
|
+
if (!isRecord(entry)) {
|
|
101
|
+
return [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const { spec, side, lib } = entry.value;
|
|
105
|
+
|
|
106
|
+
return typeof spec === 'string' && typeof side === 'string'
|
|
107
|
+
? [{ spec, side, lib: typeof lib === 'string' }]
|
|
108
|
+
: [];
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
return { root, layout, presets };
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/** `services/api3` from `services/api3/package.json`, as the folder it sits in and its name. */
|
|
115
|
+
const newPackageOf = (opts: {
|
|
116
|
+
root: string;
|
|
117
|
+
path: string;
|
|
118
|
+
}): { folder: string; name: string } | undefined => {
|
|
119
|
+
const parts = normalize(relative(opts.root, opts.path)).split(sep);
|
|
120
|
+
|
|
121
|
+
if (parts.length < 3 || parts.includes('..') || parts.includes(NODE_MODULES)) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const name = parts[parts.length - 2];
|
|
126
|
+
const folder = parts.slice(0, -2).join('/');
|
|
127
|
+
|
|
128
|
+
return { folder, name };
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const appCommand = (opts: { preset: IRecordedPreset; name: string }): string =>
|
|
132
|
+
`${ADD} --preset ${opts.preset.spec} --app ${opts.name}${opts.preset.side === 'frontend' ? ' --api <backend app> | --no-api' : ''}`;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Whether this tool call creates the `package.json` of a new app or shared package in one of the
|
|
136
|
+
* project's layout folders. A real `vulcan add` command for that folder makes it a `deny`; with no
|
|
137
|
+
* preset behind the folder it is only a `warn`: the guard never blocks without a command to offer.
|
|
138
|
+
*/
|
|
139
|
+
export const inspectPackageWrite = (opts: {
|
|
140
|
+
input: IManifestHookInput;
|
|
141
|
+
}): IPackageVerdict | undefined => {
|
|
142
|
+
const { tool, filePath, cwd } = opts.input;
|
|
143
|
+
|
|
144
|
+
if (tool !== 'Write' || filePath === undefined || basename(filePath) !== PACKAGE_FILE) {
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const absolute = normalize(
|
|
149
|
+
isAbsolute(filePath) ? filePath : join(cwd ?? process.cwd(), filePath),
|
|
150
|
+
);
|
|
151
|
+
|
|
152
|
+
if (absolute.split(sep).includes(NODE_MODULES) || existsSync(absolute)) {
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const project = readProject({ start: dirname(absolute) });
|
|
157
|
+
const created =
|
|
158
|
+
project === undefined ? undefined : newPackageOf({ root: project.root, path: absolute });
|
|
159
|
+
|
|
160
|
+
if (project === undefined || created === undefined) {
|
|
161
|
+
return undefined;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const entry = Object.entries(project.layout).find(([, folder]) => folder === created.folder);
|
|
165
|
+
|
|
166
|
+
if (entry === undefined) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const [key] = entry;
|
|
171
|
+
const isLib = key === 'shared';
|
|
172
|
+
const what = isLib ? 'lib' : `${key} app`;
|
|
173
|
+
const commands = isLib
|
|
174
|
+
? project.presets
|
|
175
|
+
.filter(preset => preset.lib)
|
|
176
|
+
.map(preset => `${ADD} --preset ${preset.spec} --lib ${created.name}`)
|
|
177
|
+
: project.presets
|
|
178
|
+
.filter(preset => preset.side === key)
|
|
179
|
+
.map(preset => appCommand({ preset, name: created.name }));
|
|
180
|
+
|
|
181
|
+
if (commands.length > 0) {
|
|
182
|
+
return {
|
|
183
|
+
kind: GuardDecisions.DENY,
|
|
184
|
+
message: `${created.folder}/${created.name} is a new ${what}: create it with a preset, not by hand - run ${commands.join(' or ')} (ask atlas \`presets\` for the choice); ${NO_HAND_WRITING}`,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
kind: 'warn',
|
|
190
|
+
message:
|
|
191
|
+
project.presets.length === 0
|
|
192
|
+
? `${created.folder}/${created.name} looks like a new ${what} and this project recorded no preset: ask the user which preset to use (\`vulcan add --preset <name|file:path> --app <name>\`, --lib <name> for a shared package); ${NO_HAND_WRITING}`
|
|
193
|
+
: `${created.folder}/${created.name} looks like a new ${what} but no recorded preset makes a ${what}: ask the user how it should be made; ${NO_HAND_WRITING}`,
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
export const buildPackageDecision = (opts: { verdict: IPackageVerdict }) =>
|
|
198
|
+
opts.verdict.kind === GuardDecisions.DENY
|
|
199
|
+
? {
|
|
200
|
+
hookSpecificOutput: {
|
|
201
|
+
hookEventName: HookConstants.PRE_TOOL_USE,
|
|
202
|
+
permissionDecision: GuardDecisions.DENY,
|
|
203
|
+
permissionDecisionReason: opts.verdict.message,
|
|
204
|
+
},
|
|
205
|
+
}
|
|
206
|
+
: {
|
|
207
|
+
hookSpecificOutput: {
|
|
208
|
+
hookEventName: HookConstants.PRE_TOOL_USE,
|
|
209
|
+
additionalContext: opts.verdict.message,
|
|
210
|
+
},
|
|
211
|
+
};
|
package/src/hooks/index.ts
CHANGED
package/src/init/scaffold.ts
CHANGED
|
@@ -4,13 +4,14 @@ import { BASE_DIRECTORY, ProjectLayout, readProperty } from '../common/index.ts'
|
|
|
4
4
|
import { ConfigConstants, findConfigFiles } from '../config/index.ts';
|
|
5
5
|
import type { TConfigExtension } from '../config/index.ts';
|
|
6
6
|
import { InitConstants, InitStatuses } from './common/index.ts';
|
|
7
|
+
import { renderDockerignore } from '../create/dockerignore.ts';
|
|
7
8
|
import type { IInitEntry, IScaffoldFile } from './common/index.ts';
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Whether a `.mcp.json` already serves atlas: a server named `atlas`, or any server whose arguments run
|
|
11
12
|
* `atlas mcp` under another name, as separate arguments or inside a `sh -c` string. A file that is not JSON falls back to looking for the name.
|
|
12
13
|
*/
|
|
13
|
-
const runsAtlas = (opts: { text: string }): boolean => {
|
|
14
|
+
export const runsAtlas = (opts: { text: string }): boolean => {
|
|
14
15
|
try {
|
|
15
16
|
const parsed: { mcpServers?: Record<string, { args?: unknown }> } = JSON.parse(opts.text);
|
|
16
17
|
const servers = parsed.mcpServers ?? {};
|
|
@@ -45,9 +46,9 @@ const SCAFFOLD_FILES: IScaffoldFile[] = [
|
|
|
45
46
|
|
|
46
47
|
/** The lint configs of a single-package project. A workspaces project keeps them in each app. */
|
|
47
48
|
const LINT_CONFIG_FILES: IScaffoldFile[] = [
|
|
48
|
-
{ path: '.configs/oxlint.config.mts', template: 'oxlint.config.mts' },
|
|
49
|
-
{ path: '.configs/eslint.config.mjs', template: 'eslint.config.mjs' },
|
|
50
|
-
{ path: '.configs/oxfmt.config.mts', template: 'oxfmt.config.mts' },
|
|
49
|
+
{ path: '.configs/lints/oxlint.config.mts', template: 'oxlint.config.mts' },
|
|
50
|
+
{ path: '.configs/lints/eslint.config.mjs', template: 'eslint.config.mjs' },
|
|
51
|
+
{ path: '.configs/formatters/oxfmt.config.mts', template: 'oxfmt.config.mts' },
|
|
51
52
|
];
|
|
52
53
|
|
|
53
54
|
export interface IScaffoldResult {
|
|
@@ -163,7 +164,7 @@ export const scaffoldProject = (opts: {
|
|
|
163
164
|
if (isWorkspaces) {
|
|
164
165
|
entries.push({
|
|
165
166
|
status: InitStatuses.SKIPPED,
|
|
166
|
-
subject: `.configs/{
|
|
167
|
+
subject: `.configs/{lints,formatters}: a workspaces project keeps them per app (apps/<name>/.configs/lints/, apps/<name>/.configs/formatters/); the templates are in node_modules/@venizia/vulcan/base/templates/`,
|
|
167
168
|
});
|
|
168
169
|
}
|
|
169
170
|
|
|
@@ -188,6 +189,8 @@ export const scaffoldProject = (opts: {
|
|
|
188
189
|
);
|
|
189
190
|
}
|
|
190
191
|
|
|
192
|
+
entries.push(writeIfAbsent({ root, path: '.dockerignore', content: renderDockerignore() }));
|
|
193
|
+
|
|
191
194
|
const makefile = join(root, 'Makefile');
|
|
192
195
|
|
|
193
196
|
if (!existsSync(makefile)) {
|
package/src/manifest/render.ts
CHANGED
|
@@ -63,7 +63,11 @@ export const renderManifest = (opts: { root: string; config: TResolvedConfig }):
|
|
|
63
63
|
path: entry.path,
|
|
64
64
|
aliases: entry.aliases,
|
|
65
65
|
retired: entry.retired,
|
|
66
|
+
...(entry.kind === undefined ? {} : { kind: entry.kind }),
|
|
66
67
|
})),
|
|
68
|
+
// Where each kind of app goes and the presets that make one: what an agent asked for a new app needs.
|
|
69
|
+
...(config.layout === undefined ? {} : { layout: config.layout }),
|
|
70
|
+
...(config.presets === undefined ? {} : { presets: config.presets }),
|
|
67
71
|
knowledge: {
|
|
68
72
|
bundle: config.knowledge.bundle,
|
|
69
73
|
...(wiki === undefined ? {} : { wiki }),
|
package/src/router.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AgentConstants } from './agents/index.ts';
|
|
1
2
|
import { COMMAND_HANDLERS } from './commands/index.ts';
|
|
2
3
|
import { runHelpCommand } from './commands/help.command.ts';
|
|
3
4
|
import { ExitCodes, UpgradeFlags, VulcanCommands } from './common/index.ts';
|
|
@@ -37,10 +38,18 @@ const ACCEPTED_ARGS: Record<string, IAcceptedArgs> = {
|
|
|
37
38
|
CreateFlags.FRONTEND_DIR,
|
|
38
39
|
CreateFlags.APP,
|
|
39
40
|
CreateFlags.API,
|
|
41
|
+
CreateFlags.LIB,
|
|
42
|
+
CreateFlags.DIR,
|
|
43
|
+
CreateFlags.SCOPE,
|
|
40
44
|
],
|
|
41
45
|
positionals: 0,
|
|
42
46
|
},
|
|
43
|
-
[VulcanCommands.SYNC]: {
|
|
47
|
+
[VulcanCommands.SYNC]: {
|
|
48
|
+
flags: ['--dry-run', '--prune'],
|
|
49
|
+
values: [AgentConstants.FLAG],
|
|
50
|
+
positionals: 0,
|
|
51
|
+
},
|
|
52
|
+
[VulcanCommands.AGENTS]: { flags: ['--dry-run'], values: [], positionals: 2 },
|
|
44
53
|
[VulcanCommands.DOCTOR]: { flags: [], values: [], positionals: 0 },
|
|
45
54
|
[VulcanCommands.UPGRADE]: {
|
|
46
55
|
flags: [UpgradeFlags.STABLE, UpgradeFlags.NIGHTLY],
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, join } from 'node:path';
|
|
3
|
+
import {
|
|
4
|
+
AgentRenderers,
|
|
5
|
+
AgentScopes,
|
|
6
|
+
atlasServerEntry,
|
|
7
|
+
mergeCodexToml,
|
|
8
|
+
mergeMcpServers,
|
|
9
|
+
readAgentTable,
|
|
10
|
+
} from '../agents/index.ts';
|
|
11
|
+
import type { TAgentEntry } from '../agents/index.ts';
|
|
12
|
+
import { SyncStatuses } from './common/index.ts';
|
|
13
|
+
import type { ISyncAction } from './common/index.ts';
|
|
14
|
+
|
|
15
|
+
const NO_CHANGE = (): void => {};
|
|
16
|
+
|
|
17
|
+
/** A file git tracks cannot be hidden by an exclude entry, and a per-developer edit would show as a diff. */
|
|
18
|
+
const isTracked = (opts: { root: string; path: string }): boolean => {
|
|
19
|
+
try {
|
|
20
|
+
return (
|
|
21
|
+
Bun.spawnSync(['git', 'ls-files', '--error-unmatch', opts.path], {
|
|
22
|
+
cwd: opts.root,
|
|
23
|
+
stdout: 'ignore',
|
|
24
|
+
stderr: 'ignore',
|
|
25
|
+
}).exitCode === 0
|
|
26
|
+
);
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const merge = (opts: { entry: TAgentEntry; root: string; text: string | undefined }) => {
|
|
33
|
+
const server = atlasServerEntry({ root: opts.root });
|
|
34
|
+
|
|
35
|
+
switch (opts.entry.mcp.renderer) {
|
|
36
|
+
case AgentRenderers.CODEX_TOML: {
|
|
37
|
+
return mergeCodexToml({ text: opts.text, root: opts.root });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
case AgentRenderers.SERVERS: {
|
|
41
|
+
return mergeMcpServers({
|
|
42
|
+
text: opts.text,
|
|
43
|
+
key: 'servers',
|
|
44
|
+
entry: { type: 'stdio', ...server },
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
default: {
|
|
49
|
+
return mergeMcpServers({ text: opts.text, key: 'mcpServers', entry: server });
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** The atlas entry in the agent's own MCP file; an agent on the shared `.mcp.json` needs no file. */
|
|
55
|
+
const planAgentMcp = (opts: { root: string; entry: TAgentEntry }): ISyncAction[] => {
|
|
56
|
+
if (opts.entry.mcp.scope !== AgentScopes.PROJECT) {
|
|
57
|
+
return [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const path = join(opts.root, opts.entry.mcp.path);
|
|
61
|
+
const text = existsSync(path) ? readFileSync(path, 'utf8') : undefined;
|
|
62
|
+
const merged = merge({ entry: opts.entry, root: opts.root, text });
|
|
63
|
+
|
|
64
|
+
if (!merged.ok) {
|
|
65
|
+
return [
|
|
66
|
+
{
|
|
67
|
+
path,
|
|
68
|
+
status: SyncStatuses.SKIPPED,
|
|
69
|
+
note: `${merged.reason} - left untouched; fix it, then re-run sync`,
|
|
70
|
+
apply: NO_CHANGE,
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!merged.changed) {
|
|
76
|
+
return [{ path, status: SyncStatuses.UNCHANGED, apply: NO_CHANGE }];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (text !== undefined && isTracked({ root: opts.root, path: opts.entry.mcp.path })) {
|
|
80
|
+
return [
|
|
81
|
+
{
|
|
82
|
+
path,
|
|
83
|
+
status: SyncStatuses.SKIPPED,
|
|
84
|
+
note: `tracked by git - left alone; add the atlas server by hand: bun ${join(opts.root, 'node_modules', '.bin', 'vulcan')} x atlas mcp --root ${opts.root}`,
|
|
85
|
+
apply: NO_CHANGE,
|
|
86
|
+
},
|
|
87
|
+
];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return [
|
|
91
|
+
{
|
|
92
|
+
path,
|
|
93
|
+
status: text === undefined ? SyncStatuses.CREATED : SyncStatuses.REPLACED,
|
|
94
|
+
apply: () => {
|
|
95
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
96
|
+
writeFileSync(path, merged.text);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
];
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** The MCP files of every chosen agent, in table order. */
|
|
103
|
+
export const planAgentFiles = (opts: { root: string; agents: string[] }): ISyncAction[] =>
|
|
104
|
+
readAgentTable()
|
|
105
|
+
.filter(entry => opts.agents.includes(entry.id))
|
|
106
|
+
.flatMap(entry => planAgentMcp({ root: opts.root, entry }));
|
|
107
|
+
|
|
108
|
+
/** One line per note of a chosen agent (a trust prompt only the user can answer). */
|
|
109
|
+
export const agentNotes = (opts: { agents: string[] }): { agent: string; note: string }[] =>
|
|
110
|
+
readAgentTable()
|
|
111
|
+
.filter(entry => opts.agents.includes(entry.id))
|
|
112
|
+
.flatMap(entry => entry.notes.map(note => ({ agent: entry.id, note })));
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import {
|
|
2
|
+
existsSync,
|
|
3
|
+
lstatSync,
|
|
4
|
+
readdirSync,
|
|
5
|
+
readFileSync,
|
|
6
|
+
readlinkSync,
|
|
7
|
+
rmdirSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from 'node:fs';
|
|
11
|
+
import { dirname, join } from 'node:path';
|
|
12
|
+
import {
|
|
13
|
+
AgentConstants,
|
|
14
|
+
AgentRenderers,
|
|
15
|
+
AgentScopes,
|
|
16
|
+
readAgentTable,
|
|
17
|
+
removeCodexBlock,
|
|
18
|
+
removeMcpEntry,
|
|
19
|
+
} from '../agents/index.ts';
|
|
20
|
+
import type { TAgentEntry } from '../agents/index.ts';
|
|
21
|
+
import { BASE_DIRECTORY, stableBaseDirectoryOf } from '../common/index.ts';
|
|
22
|
+
import { SyncConstants, SyncStatuses } from './common/index.ts';
|
|
23
|
+
import type { ISyncAction, TSettings } from './common/index.ts';
|
|
24
|
+
import { isSettingsObject, removeManagedSettings } from './settings.ts';
|
|
25
|
+
|
|
26
|
+
const NO_CHANGE = (): void => {};
|
|
27
|
+
const CLAUDE_SKILLS = join(SyncConstants.CLAUDE_DIRECTORY, SyncConstants.SKILLS_DIRECTORY);
|
|
28
|
+
/** Parents sync may delete once empty: its own agent folders, never `.agents` or the project. */
|
|
29
|
+
const OWN_FOLDERS = new Set(['.cursor', '.codex', '.pi', '.vscode', '.claude']);
|
|
30
|
+
|
|
31
|
+
const isSymlink = (opts: { path: string }): boolean =>
|
|
32
|
+
lstatSync(opts.path, { throwIfNoEntry: false })?.isSymbolicLink() ?? false;
|
|
33
|
+
|
|
34
|
+
const removeIfEmpty = (opts: { path: string }): void => {
|
|
35
|
+
try {
|
|
36
|
+
if (existsSync(opts.path) && readdirSync(opts.path).length === 0) {
|
|
37
|
+
rmdirSync(opts.path);
|
|
38
|
+
}
|
|
39
|
+
} catch {
|
|
40
|
+
// A folder that cannot be removed stays; it is the user's to clean.
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const readJsonObject = (opts: { path: string }): TSettings | undefined => {
|
|
45
|
+
try {
|
|
46
|
+
const parsed = { value: JSON.parse(readFileSync(opts.path, 'utf8')) };
|
|
47
|
+
|
|
48
|
+
return isSettingsObject(parsed) ? parsed.value : undefined;
|
|
49
|
+
} catch {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** A skill link sync made: a symlink into the shipped skills, stable or in the package, or into `.agents/skills`. */
|
|
55
|
+
const isSkillLink = (opts: { root: string; path: string }): boolean => {
|
|
56
|
+
if (!isSymlink({ path: opts.path })) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const target = readlinkSync(opts.path);
|
|
61
|
+
const shipped = [
|
|
62
|
+
join(stableBaseDirectoryOf(opts), SyncConstants.SKILLS_DIRECTORY),
|
|
63
|
+
join(BASE_DIRECTORY, SyncConstants.SKILLS_DIRECTORY),
|
|
64
|
+
join(opts.root, '.agents', SyncConstants.SKILLS_DIRECTORY),
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
return shipped.some(directory => target === directory || target.startsWith(`${directory}/`));
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const removeFile = (opts: { path: string; note?: string; parent?: string }): ISyncAction => ({
|
|
71
|
+
path: opts.path,
|
|
72
|
+
status: SyncStatuses.REMOVED,
|
|
73
|
+
note: opts.note,
|
|
74
|
+
apply: () => {
|
|
75
|
+
rmSync(opts.path, { force: true });
|
|
76
|
+
|
|
77
|
+
if (opts.parent !== undefined) {
|
|
78
|
+
removeIfEmpty({ path: opts.parent });
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const planMcpRemoval = (opts: { root: string; entry: TAgentEntry }): ISyncAction[] => {
|
|
84
|
+
if (opts.entry.mcp.scope !== AgentScopes.PROJECT) {
|
|
85
|
+
return [];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const path = join(opts.root, opts.entry.mcp.path);
|
|
89
|
+
|
|
90
|
+
if (!existsSync(path)) {
|
|
91
|
+
return [];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const text = readFileSync(path, 'utf8');
|
|
95
|
+
const folder = dirname(opts.entry.mcp.path);
|
|
96
|
+
const parent = OWN_FOLDERS.has(folder) ? join(opts.root, folder) : undefined;
|
|
97
|
+
|
|
98
|
+
if (opts.entry.mcp.renderer === AgentRenderers.CODEX_TOML) {
|
|
99
|
+
const removed = removeCodexBlock({ text });
|
|
100
|
+
|
|
101
|
+
if (!removed.changed) {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return removed.text === undefined
|
|
106
|
+
? [removeFile({ path, parent })]
|
|
107
|
+
: [
|
|
108
|
+
{
|
|
109
|
+
path,
|
|
110
|
+
status: SyncStatuses.REPLACED,
|
|
111
|
+
note: 'the atlas block taken out',
|
|
112
|
+
apply: () => writeFileSync(path, removed.text ?? ''),
|
|
113
|
+
},
|
|
114
|
+
];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const key = opts.entry.mcp.renderer === AgentRenderers.SERVERS ? 'servers' : 'mcpServers';
|
|
118
|
+
const removed = removeMcpEntry({ text, key });
|
|
119
|
+
|
|
120
|
+
if (!removed.ok) {
|
|
121
|
+
return [{ path, status: SyncStatuses.SKIPPED, note: removed.reason, apply: NO_CHANGE }];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (removed.outcome === 'absent') {
|
|
125
|
+
return [];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return removed.text === undefined
|
|
129
|
+
? [removeFile({ path, parent })]
|
|
130
|
+
: [
|
|
131
|
+
{
|
|
132
|
+
path,
|
|
133
|
+
status: SyncStatuses.REPLACED,
|
|
134
|
+
note: 'the atlas entry taken out',
|
|
135
|
+
apply: () => writeFileSync(path, removed.text ?? ''),
|
|
136
|
+
},
|
|
137
|
+
];
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const planSkillLinkRemoval = (opts: { root: string; directory: string }): ISyncAction[] => {
|
|
141
|
+
const target = join(opts.root, opts.directory);
|
|
142
|
+
|
|
143
|
+
if (!existsSync(target) || isSymlink({ path: target })) {
|
|
144
|
+
return [];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const links = readdirSync(target)
|
|
148
|
+
.map(name => join(target, name))
|
|
149
|
+
.filter(path => isSkillLink({ root: opts.root, path }));
|
|
150
|
+
|
|
151
|
+
return links.map((path, index) => ({
|
|
152
|
+
path,
|
|
153
|
+
status: SyncStatuses.REMOVED,
|
|
154
|
+
apply: () => {
|
|
155
|
+
rmSync(path, { force: true });
|
|
156
|
+
|
|
157
|
+
if (index === links.length - 1) {
|
|
158
|
+
removeIfEmpty({ path: target });
|
|
159
|
+
removeIfEmpty({
|
|
160
|
+
path: dirname(target) === join(opts.root, '.agents') ? '' : dirname(target),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
},
|
|
164
|
+
}));
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const planClaudeRemoval = (opts: { root: string }): ISyncAction[] => {
|
|
168
|
+
const actions: ISyncAction[] = [];
|
|
169
|
+
const shim = join(opts.root, SyncConstants.CLAUDE_TOOL_FILE);
|
|
170
|
+
|
|
171
|
+
if (existsSync(shim) && readFileSync(shim, 'utf8').trim() === SyncConstants.CLAUDE_IMPORT_LINE) {
|
|
172
|
+
actions.push(removeFile({ path: shim }));
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const directory = join(opts.root, SyncConstants.CLAUDE_DIRECTORY);
|
|
176
|
+
const settingsPath = join(directory, SyncConstants.SETTINGS_FILE);
|
|
177
|
+
const managedPath = join(directory, SyncConstants.MANAGED_FILE);
|
|
178
|
+
const settings = readJsonObject({ path: settingsPath });
|
|
179
|
+
const managed = readJsonObject({ path: managedPath });
|
|
180
|
+
|
|
181
|
+
if (settings !== undefined && managed !== undefined) {
|
|
182
|
+
const remaining = removeManagedSettings({ settings, managed });
|
|
183
|
+
|
|
184
|
+
actions.push(
|
|
185
|
+
Object.keys(remaining).length === 0
|
|
186
|
+
? removeFile({ path: settingsPath, note: 'only what sync wrote was in it' })
|
|
187
|
+
: {
|
|
188
|
+
path: settingsPath,
|
|
189
|
+
status: SyncStatuses.REPLACED,
|
|
190
|
+
note: 'the settings sync wrote taken out, yours kept',
|
|
191
|
+
apply: () => writeFileSync(settingsPath, `${JSON.stringify(remaining, null, 2)}\n`),
|
|
192
|
+
},
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (existsSync(managedPath)) {
|
|
197
|
+
actions.push(removeFile({ path: managedPath }));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
actions.push(...planSkillLinkRemoval({ root: opts.root, directory: CLAUDE_SKILLS }));
|
|
201
|
+
|
|
202
|
+
return actions;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* What sync created for the agents that are no longer chosen, taken back: the Claude shim and managed
|
|
207
|
+
* settings, a file sync wrote whole, the atlas key it merged into a file that holds more, the Codex
|
|
208
|
+
* block, and the skill links no remaining agent still reads. A file the user changed so the entry is
|
|
209
|
+
* no longer sync's is left and reported.
|
|
210
|
+
*/
|
|
211
|
+
export const planAgentRemoval = (opts: {
|
|
212
|
+
root: string;
|
|
213
|
+
removed: string[];
|
|
214
|
+
remaining: string[];
|
|
215
|
+
}): ISyncAction[] => {
|
|
216
|
+
const table = readAgentTable();
|
|
217
|
+
const kept = new Set(
|
|
218
|
+
table.filter(entry => opts.remaining.includes(entry.id)).map(entry => entry.skills.dir),
|
|
219
|
+
);
|
|
220
|
+
const cleaned = new Set<string>();
|
|
221
|
+
const actions: ISyncAction[] = [];
|
|
222
|
+
|
|
223
|
+
for (const entry of table.filter(item => opts.removed.includes(item.id))) {
|
|
224
|
+
if (entry.id === AgentConstants.DEFAULT_AGENT) {
|
|
225
|
+
actions.push(...planClaudeRemoval({ root: opts.root }));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
actions.push(...planMcpRemoval({ root: opts.root, entry }));
|
|
229
|
+
|
|
230
|
+
if (
|
|
231
|
+
entry.skills.dir !== CLAUDE_SKILLS &&
|
|
232
|
+
!kept.has(entry.skills.dir) &&
|
|
233
|
+
!cleaned.has(entry.skills.dir)
|
|
234
|
+
) {
|
|
235
|
+
cleaned.add(entry.skills.dir);
|
|
236
|
+
actions.push(...planSkillLinkRemoval({ root: opts.root, directory: entry.skills.dir }));
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return actions;
|
|
241
|
+
};
|