magus-cli 7.4.0 → 7.5.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 +9 -8
- package/package.json +4 -4
- package/src/__tests__/blind/c19-paste-routing.test.ts +252 -0
- package/src/__tests__/blind/c20-registry-and-file-safety.test.ts +560 -0
- package/src/__tests__/install-command.test.ts +4 -3
- package/src/__tests__/models-cli.test.ts +4 -7
- package/src/__tests__/models-manager.test.ts +52 -40
- package/src/__tests__/profile-absorb.test.ts +1138 -0
- package/src/__tests__/profile-command.test.ts +90 -32
- package/src/__tests__/profile-migration-offer.test.ts +18 -15
- package/src/__tests__/profile-migration.test.ts +246 -117
- package/src/__tests__/profile-render.test.ts +141 -0
- package/src/__tests__/profile-skills.test.ts +425 -0
- package/src/__tests__/profile-startup.test.ts +44 -14
- package/src/__tests__/profile-state.test.ts +795 -0
- package/src/__tests__/settings-file.test.ts +5 -7
- package/src/__tests__/stale-load-repair.test.ts +5 -4
- package/src/__tests__/styles-manager.test.ts +47 -74
- package/src/__tests__/styles-screen-state.test.ts +11 -10
- package/src/cli/bootstrap.ts +87 -27
- package/src/cli/doctor.ts +152 -44
- package/src/cli/install.ts +92 -127
- package/src/cli/models.ts +21 -1
- package/src/cli/profile.ts +48 -77
- package/src/cli/router.ts +32 -6
- package/src/cli/update.ts +43 -6
- package/src/data/gitignore-defaults.ts +3 -3
- package/src/services/active-closure.ts +3 -3
- package/src/services/claude-settings.ts +283 -125
- package/src/services/cli-tool-commands.ts +30 -0
- package/src/services/conventions-manager.ts +88 -0
- package/src/services/git-worktree.ts +21 -0
- package/src/services/gitignore-service.ts +39 -22
- package/src/services/manifest.ts +4 -5
- package/src/services/models-manager.ts +36 -73
- package/src/services/profile-absorb.ts +1532 -0
- package/src/services/profile-activate.ts +348 -96
- package/src/services/profile-live.ts +825 -0
- package/src/services/profile-render.ts +115 -0
- package/src/services/profile-skills.ts +462 -0
- package/src/services/profile-startup.ts +88 -16
- package/src/services/profile-state.ts +622 -0
- package/src/services/profiles.ts +20 -32
- package/src/services/resolver.ts +6 -1
- package/src/services/settings-file.ts +14 -8
- package/src/services/skills-manager.ts +14 -8
- package/src/services/styles-manager.ts +45 -28
- package/src/services/tool-probe.ts +48 -3
- package/src/types/index.ts +15 -9
- package/src/ui/App.tsx +25 -2
- package/src/ui/components/modals/ModalContainer.tsx +9 -1
- package/src/ui/hooks/useProfileStartupModal.ts +20 -15
- package/src/ui/input/KeyRouter.tsx +147 -23
- package/src/ui/input/key-dispatch.ts +48 -0
- package/src/ui/keymap.ts +8 -1
- package/src/ui/renderers/cliToolRenderers.tsx +65 -27
- package/src/ui/screens/CliToolsScreen.tsx +74 -13
- package/src/ui/screens/PluginsScreen.tsx +6 -2
- package/src/ui/screens/ProfilesScreen.tsx +75 -27
- package/src/ui/screens/StylesScreen.tsx +2 -1
- package/src/ui/state/types.ts +1 -1
- package/src/__tests__/profile-materializer.test.ts +0 -238
- package/src/__tests__/profile-sync.test.ts +0 -154
- package/src/__tests__/symlink-manager.test.ts +0 -180
- package/src/services/profile-materializer.ts +0 -133
- package/src/services/profile-sync.ts +0 -158
- package/src/services/symlink-manager.ts +0 -343
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Profile symlink manager.
|
|
3
|
-
*
|
|
4
|
-
* Materialized profiles live at `.claude/_profiles/<name>/` (generated,
|
|
5
|
-
* gitignored). The active profile is made "live" by symlinking the shared
|
|
6
|
-
* config files into place:
|
|
7
|
-
*
|
|
8
|
-
* .claude/settings.json -> .claude/_profiles/<name>/settings.json
|
|
9
|
-
* .claude/skills -> .claude/_profiles/<name>/skills
|
|
10
|
-
* .mcp.json -> .claude/_profiles/<name>/mcp.json
|
|
11
|
-
* .claude/models.json -> .claude/_profiles/<name>/models.json
|
|
12
|
-
*
|
|
13
|
-
* Switching profiles just repoints these links — no network, no reinstall.
|
|
14
|
-
* settings.local.json is NEVER linked: it holds personal credentials and must
|
|
15
|
-
* survive switches untouched.
|
|
16
|
-
*
|
|
17
|
-
* models.json is the one link whose target is OPTIONAL: a profile that declares
|
|
18
|
-
* no routing has none, and the rule below (remove a link into _profiles/ whose
|
|
19
|
-
* target is gone) is what keeps a dangling `.claude/models.json` out of the
|
|
20
|
-
* repo. A dangling one would not be harmless — `.claude/` is committed, so it
|
|
21
|
-
* would ship to every teammate.
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
import type { Stats } from "node:fs";
|
|
25
|
-
import path from "node:path";
|
|
26
|
-
import fs from "fs-extra";
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Paths that must be gitignored once a project uses profiles: the generated
|
|
30
|
-
* build output plus the active-profile symlinks. Only .claude/profiles.json is
|
|
31
|
-
* the committed source of truth. settings.local.json is intentionally NOT here
|
|
32
|
-
* (it's already gitignored elsewhere and holds personal credentials).
|
|
33
|
-
*/
|
|
34
|
-
export const PROFILE_GITIGNORE_ENTRIES = [
|
|
35
|
-
".claude/_profiles/",
|
|
36
|
-
".claude/.magus-backups/",
|
|
37
|
-
".claude/settings.json",
|
|
38
|
-
".claude/skills",
|
|
39
|
-
".claude/models.json",
|
|
40
|
-
".mcp.json",
|
|
41
|
-
];
|
|
42
|
-
|
|
43
|
-
/** The generated, gitignored directory holding materialized profiles. */
|
|
44
|
-
export function profilesRoot(projectPath?: string): string {
|
|
45
|
-
const base = projectPath ?? process.cwd();
|
|
46
|
-
return path.join(base, ".claude", "_profiles");
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/** The materialized directory for one profile. */
|
|
50
|
-
export function profileDir(name: string, projectPath?: string): string {
|
|
51
|
-
return path.join(profilesRoot(projectPath), name);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/** The artifacts an active profile links into place. */
|
|
55
|
-
export type ProfileLinkKey = "settings" | "skills" | "mcp" | "models";
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Where each linked artifact lives, and what it is called inside a profile.
|
|
59
|
-
*
|
|
60
|
-
* One table, because the link paths do NOT depend on which profile is active
|
|
61
|
-
* and two places listing them is two places to forget one. `linkTargets` adds
|
|
62
|
-
* the profile dir; `linkPaths` needs no profile at all.
|
|
63
|
-
*/
|
|
64
|
-
const LINKED_ARTIFACTS: Array<{
|
|
65
|
-
key: ProfileLinkKey;
|
|
66
|
-
/** Path segments under the project root. */
|
|
67
|
-
at: string[];
|
|
68
|
-
/** File or directory name inside `_profiles/<name>/`. */
|
|
69
|
-
inProfile: string;
|
|
70
|
-
}> = [
|
|
71
|
-
{
|
|
72
|
-
key: "settings",
|
|
73
|
-
at: [".claude", "settings.json"],
|
|
74
|
-
inProfile: "settings.json",
|
|
75
|
-
},
|
|
76
|
-
{ key: "skills", at: [".claude", "skills"], inProfile: "skills" },
|
|
77
|
-
{ key: "mcp", at: [".mcp.json"], inProfile: "mcp.json" },
|
|
78
|
-
{ key: "models", at: [".claude", "models.json"], inProfile: "models.json" },
|
|
79
|
-
];
|
|
80
|
-
|
|
81
|
-
/** The set of links an active profile owns, as [linkPath, targetPath] pairs. */
|
|
82
|
-
export function linkTargets(
|
|
83
|
-
name: string,
|
|
84
|
-
projectPath?: string,
|
|
85
|
-
): Array<{ key: ProfileLinkKey; link: string; target: string }> {
|
|
86
|
-
const base = projectPath ?? process.cwd();
|
|
87
|
-
const dir = profileDir(name, projectPath);
|
|
88
|
-
return LINKED_ARTIFACTS.map(({ key, at, inProfile }) => ({
|
|
89
|
-
key,
|
|
90
|
-
link: path.join(base, ...at),
|
|
91
|
-
target: path.join(dir, inProfile),
|
|
92
|
-
}));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/** The paths activation writes links at, whichever profile is chosen. */
|
|
96
|
-
export function linkPaths(projectPath?: string): string[] {
|
|
97
|
-
const base = projectPath ?? process.cwd();
|
|
98
|
-
return LINKED_ARTIFACTS.map(({ at }) => path.join(base, ...at));
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Real artifacts that activating ANY profile would have to move aside.
|
|
103
|
-
*
|
|
104
|
-
* Empty means activating is lossless — a fresh clone, where the only things at
|
|
105
|
-
* these paths are nothing at all or links magus wrote itself. That is what
|
|
106
|
-
* makes it safe to honour the manifest's declared default without asking: there
|
|
107
|
-
* is no work of the user's to replace, so there is no decision to put to them.
|
|
108
|
-
*
|
|
109
|
-
* Non-empty means a human put something there, and the choice is theirs.
|
|
110
|
-
*/
|
|
111
|
-
export async function displaceableArtifacts(
|
|
112
|
-
projectPath?: string,
|
|
113
|
-
): Promise<string[]> {
|
|
114
|
-
const found: string[] = [];
|
|
115
|
-
for (const link of linkPaths(projectPath)) {
|
|
116
|
-
// A link we own is generated state the manifest reproduces on demand.
|
|
117
|
-
if (await isProfileLink(link, projectPath)) continue;
|
|
118
|
-
try {
|
|
119
|
-
await fs.lstat(link);
|
|
120
|
-
found.push(link);
|
|
121
|
-
} catch {
|
|
122
|
-
// Nothing there.
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
return found;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Where activation moves a real file it had to displace.
|
|
130
|
-
*
|
|
131
|
-
* Beside `_profiles/`, never inside it. `_profiles/` is generated and is
|
|
132
|
-
* documented as regenerable from the manifest, so it is the first thing a
|
|
133
|
-
* cleanup — a user's, or ours — deletes. A backup stored there is the only copy
|
|
134
|
-
* of a hand-written file sitting in the one directory everybody is told is safe
|
|
135
|
-
* to remove. Gitignored all the same: these are recovery copies, not something
|
|
136
|
-
* to commit.
|
|
137
|
-
*/
|
|
138
|
-
export function backupRoot(projectPath?: string): string {
|
|
139
|
-
const base = projectPath ?? process.cwd();
|
|
140
|
-
return path.join(base, ".claude", ".magus-backups");
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** One artifact activation moved aside to make room for its symlink. */
|
|
144
|
-
export interface DisplacedArtifact {
|
|
145
|
-
/** The path that is now a profile symlink. */
|
|
146
|
-
link: string;
|
|
147
|
-
/** Where its previous contents were moved. */
|
|
148
|
-
savedTo: string;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** A directory name that sorts chronologically and is legal on every platform. */
|
|
152
|
-
function backupStamp(): string {
|
|
153
|
-
return new Date().toISOString().replace(/[:.]/g, "-");
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Move whatever sits at `link` into `backupDir`, unless it is generated state.
|
|
158
|
-
*
|
|
159
|
-
* Activation replaces four paths with symlinks, and replacing means removing
|
|
160
|
-
* what is there. Three of those paths are ordinary files a project writes by
|
|
161
|
-
* hand and commits. `fs.remove` on `.claude/settings.json` is unrecoverable, and
|
|
162
|
-
* it ran on every activation — which is why adoption deliberately stopped short
|
|
163
|
-
* of activating and left migrations half-done.
|
|
164
|
-
*
|
|
165
|
-
* A symlink magus itself wrote is skipped: it points into `_profiles/`, which
|
|
166
|
-
* the manifest regenerates on demand, so copying it would archive nothing and
|
|
167
|
-
* every `profile switch` would leave another directory behind. A symlink
|
|
168
|
-
* pointing ANYWHERE ELSE is somebody's deliberate wiring and is preserved like
|
|
169
|
-
* a real file — the link is moved, never its target.
|
|
170
|
-
*/
|
|
171
|
-
async function displace(
|
|
172
|
-
link: string,
|
|
173
|
-
backupDir: string,
|
|
174
|
-
projectPath?: string,
|
|
175
|
-
): Promise<DisplacedArtifact | null> {
|
|
176
|
-
if (await isProfileLink(link, projectPath)) return null;
|
|
177
|
-
let stat: Stats;
|
|
178
|
-
try {
|
|
179
|
-
stat = await fs.lstat(link);
|
|
180
|
-
} catch {
|
|
181
|
-
return null; // Nothing there — the common case on a first activation.
|
|
182
|
-
}
|
|
183
|
-
// The four link paths have four distinct basenames, so one flat directory
|
|
184
|
-
// holds a whole activation without collisions.
|
|
185
|
-
const savedTo = path.join(backupDir, path.basename(link));
|
|
186
|
-
await fs.ensureDir(backupDir);
|
|
187
|
-
|
|
188
|
-
if (stat.isSymbolicLink()) {
|
|
189
|
-
// A symlink's target is resolved against the directory the LINK sits in,
|
|
190
|
-
// so moving the link two levels deeper silently repoints it: a relative
|
|
191
|
-
// `../shared/mcp.json` that worked becomes dangling, and the "preserved"
|
|
192
|
-
// copy preserves nothing. Rewrite it as absolute at its original meaning.
|
|
193
|
-
const target = await fs.readlink(link);
|
|
194
|
-
const absolute = path.resolve(path.dirname(link), target);
|
|
195
|
-
await fs.remove(savedTo);
|
|
196
|
-
await fs.symlink(absolute, savedTo);
|
|
197
|
-
await fs.remove(link);
|
|
198
|
-
return { link, savedTo };
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
await fs.move(link, savedTo, { overwrite: true });
|
|
202
|
-
return { link, savedTo };
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Point `link` at `target` with a relative symlink. Any existing file, dir, or
|
|
207
|
-
* link at `link` is removed first. The target need not exist yet (dangling
|
|
208
|
-
* links are tolerated so a profile can be activated before every artifact is
|
|
209
|
-
* materialized), but its parent is created.
|
|
210
|
-
*/
|
|
211
|
-
async function relink(link: string, target: string): Promise<void> {
|
|
212
|
-
await fs.ensureDir(path.dirname(link));
|
|
213
|
-
await fs.remove(link);
|
|
214
|
-
const rel = path.relative(path.dirname(link), target);
|
|
215
|
-
await fs.symlink(rel, link);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* Activate a profile: repoint every shared-config symlink into its
|
|
220
|
-
* materialized directory. Only links whose target exists in the profile dir
|
|
221
|
-
* are created (e.g. a profile with no skills/ dir gets no skills symlink).
|
|
222
|
-
*
|
|
223
|
-
* Returns whatever had to be moved aside to make room — empty for an ordinary
|
|
224
|
-
* `profile switch`, one entry per hand-written artifact the first time a
|
|
225
|
-
* project adopts profiles. Callers report it so the move is never silent.
|
|
226
|
-
*/
|
|
227
|
-
export async function activateProfile(
|
|
228
|
-
name: string,
|
|
229
|
-
projectPath?: string,
|
|
230
|
-
/**
|
|
231
|
-
* Called the instant an artifact is moved, before the next link is touched.
|
|
232
|
-
*
|
|
233
|
-
* The return value alone is not enough, and the difference is data the user
|
|
234
|
-
* cannot otherwise find: activation moves up to four artifacts in sequence,
|
|
235
|
-
* so a throw on the second discards the array describing the first — leaving
|
|
236
|
-
* `.claude/settings.json` gone from where they put it and no message saying
|
|
237
|
-
* where it went. Reporting as it happens is what makes a half-finished
|
|
238
|
-
* activation recoverable by hand.
|
|
239
|
-
*/
|
|
240
|
-
onDisplace?: (artifact: DisplacedArtifact) => void,
|
|
241
|
-
): Promise<DisplacedArtifact[]> {
|
|
242
|
-
const dir = profileDir(name, projectPath);
|
|
243
|
-
if (!(await fs.pathExists(dir))) {
|
|
244
|
-
throw new Error(
|
|
245
|
-
`Profile "${name}" is not materialized (${dir} missing). Run \`magus install ${name}\`.`,
|
|
246
|
-
);
|
|
247
|
-
}
|
|
248
|
-
// One stamped directory per activation, so the four artifacts that were live
|
|
249
|
-
// together stay together and can be restored as the set they were.
|
|
250
|
-
const backupDir = path.join(backupRoot(projectPath), backupStamp());
|
|
251
|
-
const displaced: DisplacedArtifact[] = [];
|
|
252
|
-
for (const { link, target } of linkTargets(name, projectPath)) {
|
|
253
|
-
const moved = await syncLink(link, target, projectPath, backupDir);
|
|
254
|
-
if (moved) {
|
|
255
|
-
displaced.push(moved);
|
|
256
|
-
onDisplace?.(moved);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
return displaced;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Point one link at its target, or remove it when the profile has no such
|
|
264
|
-
* artifact. A link is only removed when it points into `_profiles/` — a real
|
|
265
|
-
* file the user put there is never clobbered.
|
|
266
|
-
*/
|
|
267
|
-
async function syncLink(
|
|
268
|
-
link: string,
|
|
269
|
-
target: string,
|
|
270
|
-
projectPath: string | undefined,
|
|
271
|
-
// Required, not optional. An optional backup directory keeps a
|
|
272
|
-
// replace-without-backup path expressible, and that path is the whole defect
|
|
273
|
-
// this function was changed to close — a future caller omitting the argument
|
|
274
|
-
// would reintroduce it silently, and type-check clean while doing so.
|
|
275
|
-
backupDir: string,
|
|
276
|
-
): Promise<DisplacedArtifact | null> {
|
|
277
|
-
if (await fs.pathExists(target)) {
|
|
278
|
-
const moved = await displace(link, backupDir, projectPath);
|
|
279
|
-
await relink(link, target);
|
|
280
|
-
return moved;
|
|
281
|
-
}
|
|
282
|
-
if (await isProfileLink(link, projectPath)) await fs.remove(link);
|
|
283
|
-
return null;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* Sync ONE of the active profile's links, leaving the others alone.
|
|
288
|
-
*
|
|
289
|
-
* `activateProfile` repoints all four, which is right when switching profiles
|
|
290
|
-
* and wrong when only one artifact changed: `magus models use` would replace
|
|
291
|
-
* a project's real `.mcp.json` with a symlink as a side effect of choosing a
|
|
292
|
-
* model. This is the narrow door for that case.
|
|
293
|
-
*/
|
|
294
|
-
export async function syncProfileLink(
|
|
295
|
-
name: string,
|
|
296
|
-
key: ProfileLinkKey,
|
|
297
|
-
projectPath?: string,
|
|
298
|
-
): Promise<DisplacedArtifact | null> {
|
|
299
|
-
const entry = linkTargets(name, projectPath).find((t) => t.key === key);
|
|
300
|
-
if (!entry) return null;
|
|
301
|
-
// Backed up for the same reason activation is: `magus models use` on a
|
|
302
|
-
// project with a hand-written `.claude/models.json` would otherwise delete it
|
|
303
|
-
// as a side effect of choosing a model.
|
|
304
|
-
const backupDir = path.join(backupRoot(projectPath), backupStamp());
|
|
305
|
-
return syncLink(entry.link, entry.target, projectPath, backupDir);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/** Is `link` a symlink pointing into `.claude/_profiles/`? */
|
|
309
|
-
async function isProfileLink(
|
|
310
|
-
link: string,
|
|
311
|
-
projectPath?: string,
|
|
312
|
-
): Promise<boolean> {
|
|
313
|
-
try {
|
|
314
|
-
const stat = await fs.lstat(link);
|
|
315
|
-
if (!stat.isSymbolicLink()) return false;
|
|
316
|
-
const resolved = path.resolve(path.dirname(link), await fs.readlink(link));
|
|
317
|
-
return resolved.startsWith(profilesRoot(projectPath) + path.sep);
|
|
318
|
-
} catch {
|
|
319
|
-
return false;
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* The name of the currently active profile, derived from where
|
|
325
|
-
* .claude/settings.json points. Returns null if it isn't a profile symlink.
|
|
326
|
-
*/
|
|
327
|
-
export async function activeProfile(
|
|
328
|
-
projectPath?: string,
|
|
329
|
-
): Promise<string | null> {
|
|
330
|
-
const base = projectPath ?? process.cwd();
|
|
331
|
-
const link = path.join(base, ".claude", "settings.json");
|
|
332
|
-
try {
|
|
333
|
-
const stat = await fs.lstat(link);
|
|
334
|
-
if (!stat.isSymbolicLink()) return null;
|
|
335
|
-
const resolved = path.resolve(path.dirname(link), await fs.readlink(link));
|
|
336
|
-
const root = profilesRoot(projectPath) + path.sep;
|
|
337
|
-
if (!resolved.startsWith(root)) return null;
|
|
338
|
-
// resolved = <root>/<name>/settings.json → take the first segment.
|
|
339
|
-
return resolved.slice(root.length).split(path.sep)[0] ?? null;
|
|
340
|
-
} catch {
|
|
341
|
-
return null;
|
|
342
|
-
}
|
|
343
|
-
}
|