@rosenfan/matrix 0.1.1 → 0.1.3
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/.claude/skills/matrix/SKILL.md +60 -17
- package/.claude/skills/matrix/agents/openai.yaml +4 -4
- package/.claude/skills/matrix/scripts/matrix_claude.py +3 -158
- package/.claude/skills/matrix/scripts/matrix_state.py +53 -107
- package/.claude/skills/matrix-archive/SKILL.md +23 -13
- package/.claude/skills/matrix-build/SKILL.md +24 -35
- package/.claude/skills/matrix-claude/SKILL.md +26 -24
- package/.claude/skills/matrix-design/SKILL.md +29 -26
- package/.claude/skills/matrix-hotfix/SKILL.md +8 -8
- package/.claude/skills/matrix-open/SKILL.md +25 -18
- package/.claude/skills/matrix-status/SKILL.md +3 -2
- package/.claude/skills/matrix-tweak/SKILL.md +8 -8
- package/.claude/skills/matrix-verify/SKILL.md +21 -30
- package/LICENSE +21 -21
- package/README.md +294 -255
- package/README.zh-CN.md +293 -255
- package/assets/skills-zh-CN/matrix/SKILL.md +80 -25
- package/assets/skills-zh-CN/matrix-archive/SKILL.md +33 -9
- package/assets/skills-zh-CN/matrix-build/SKILL.md +36 -10
- package/assets/skills-zh-CN/matrix-claude/SKILL.md +31 -28
- package/assets/skills-zh-CN/matrix-design/SKILL.md +19 -10
- package/assets/skills-zh-CN/matrix-hotfix/SKILL.md +8 -8
- package/assets/skills-zh-CN/matrix-open/SKILL.md +22 -7
- package/assets/skills-zh-CN/matrix-status/SKILL.md +15 -14
- package/assets/skills-zh-CN/matrix-tweak/SKILL.md +8 -8
- package/assets/skills-zh-CN/matrix-verify/SKILL.md +26 -10
- package/bin/matrix.js +15 -15
- package/package.json +56 -56
- package/src/catalog.js +92 -87
- package/src/cli.js +159 -101
- package/src/distribution.js +281 -186
- package/src/installer.js +45 -45
- package/src/matt.js +45 -45
- package/src/messages.js +33 -33
- package/src/self-update.js +81 -0
- package/src/ui.js +89 -89
- package/src/workflow-transaction.js +622 -0
- package/src/workflow.js +787 -82
package/src/distribution.js
CHANGED
|
@@ -1,192 +1,287 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import os from "node:os";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
MATRIX_SKILLS, MATT_SKILLS, PLATFORM_IDS, PLATFORMS, hashDirectory,
|
|
7
|
-
installationRoot, manifestPath, releaseCatalog, scopeBase, sourceSkillsRoot, workflowRuntimeSource
|
|
8
|
-
} from "./catalog.js";
|
|
9
|
-
import { createMattAdapter } from "./matt.js";
|
|
10
|
-
import { text } from "./messages.js";
|
|
11
|
-
|
|
12
|
-
const VERSION =
|
|
13
|
-
const digest = (value) => crypto.createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
14
|
-
const exists = (value) => fs.existsSync(value);
|
|
15
|
-
const pathExists = (value) => { try { fs.lstatSync(value); return true; } catch { return false; } };
|
|
16
|
-
const nowStamp = () => new Date().toISOString().replace(/[:.]/g, "-");
|
|
17
|
-
const transactionPath = (intent) => path.join(installationRoot(intent.scope, intent.projectRoot, intent.home), "transaction.json");
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
fs.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
import os from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import {
|
|
6
|
+
MATRIX_SKILLS, MATT_SKILLS, UNMANAGED_MATT_SKILLS, PLATFORM_IDS, PLATFORMS, hashDirectory,
|
|
7
|
+
installationRoot, manifestPath, releaseCatalog, scopeBase, sourceSkillsRoot, workflowRuntimeSource, workflowTransactionSource
|
|
8
|
+
} from "./catalog.js";
|
|
9
|
+
import { createMattAdapter } from "./matt.js";
|
|
10
|
+
import { text } from "./messages.js";
|
|
11
|
+
|
|
12
|
+
const VERSION = 2;
|
|
13
|
+
const digest = (value) => crypto.createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
14
|
+
const exists = (value) => fs.existsSync(value);
|
|
15
|
+
const pathExists = (value) => { try { fs.lstatSync(value); return true; } catch { return false; } };
|
|
16
|
+
const nowStamp = () => new Date().toISOString().replace(/[:.]/g, "-");
|
|
17
|
+
const transactionPath = (intent) => path.join(installationRoot(intent.scope, intent.projectRoot, intent.home), "transaction.json");
|
|
18
|
+
const configPath = (intent) => path.join(intent.projectRoot, ".matrix", "config.yaml");
|
|
19
|
+
const writeJson = (file, value) => {
|
|
20
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
21
|
+
const temporary = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
22
|
+
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`);
|
|
23
|
+
fs.renameSync(temporary, file);
|
|
24
|
+
};
|
|
25
|
+
const readJson = (file) => { try { return JSON.parse(fs.readFileSync(file, "utf8")); } catch { return null; } };
|
|
26
|
+
const readConfig = (file) => {
|
|
27
|
+
try {
|
|
28
|
+
return Object.fromEntries(fs.readFileSync(file, "utf8").split(/\r?\n/).filter((line) => line.includes(":") && !line.startsWith(" ")).map((line) => {
|
|
29
|
+
const [key, ...value] = line.split(":");
|
|
30
|
+
return [key.trim(), value.join(":").trim()];
|
|
31
|
+
}));
|
|
32
|
+
} catch { return {}; }
|
|
33
|
+
};
|
|
34
|
+
const configContent = (existing, defaultOrchestration, scope, language) => {
|
|
35
|
+
const values = {
|
|
36
|
+
...existing,
|
|
37
|
+
schema: "matrix/config/v1",
|
|
38
|
+
auto_transition: existing.auto_transition === "false" ? "false" : "true",
|
|
39
|
+
default_orchestration: defaultOrchestration,
|
|
40
|
+
installation_scope: scope,
|
|
41
|
+
language
|
|
42
|
+
};
|
|
43
|
+
const preferred = ["schema", "auto_transition", "default_orchestration", "installation_scope", "language"];
|
|
44
|
+
const keys = [...preferred, ...Object.keys(values).filter((key) => !preferred.includes(key)).sort()];
|
|
45
|
+
return `${keys.map((key) => `${key}: ${values[key]}`).join("\n")}\n`;
|
|
36
46
|
};
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
|
|
47
|
+
const removeEntry = (value) => { if (pathExists(value)) fs.rmSync(value, { recursive: true, force: true }); };
|
|
48
|
+
const removeStaging = (stagingRoot) => {
|
|
49
|
+
removeEntry(stagingRoot);
|
|
50
|
+
const parent = path.dirname(stagingRoot);
|
|
51
|
+
if (exists(parent) && fs.readdirSync(parent).length === 0) fs.rmdirSync(parent);
|
|
52
|
+
};
|
|
53
|
+
const compareVersions = (left, right) => {
|
|
54
|
+
const parse = (value) => String(value ?? "0.0.0").split("-")[0].split(".").map((part) => Number(part) || 0);
|
|
55
|
+
const a = parse(left); const b = parse(right);
|
|
56
|
+
for (let index = 0; index < Math.max(a.length, b.length); index += 1) if ((a[index] ?? 0) !== (b[index] ?? 0)) return (a[index] ?? 0) > (b[index] ?? 0) ? 1 : -1;
|
|
57
|
+
return 0;
|
|
58
|
+
};
|
|
59
|
+
const skillPresent = (root, skill) => exists(path.join(root, skill, "SKILL.md"));
|
|
60
|
+
const isBrokenLink = (value) => { try { return fs.lstatSync(value).isSymbolicLink() && !exists(value); } catch { return false; } };
|
|
61
|
+
|
|
40
62
|
function readManifest(intent) { return readJson(manifestPath(intent.scope, intent.projectRoot, intent.home)); }
|
|
41
63
|
|
|
42
|
-
function
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function observePlatform(intent, catalog, manifest, platformId) {
|
|
61
|
-
const platform = PLATFORMS[platformId]; const base = scopeBase(intent.scope, intent.projectRoot, intent.home);
|
|
62
|
-
const root = platform.skillRoot(base); const records = manifest?.platforms?.[platformId]?.skills ?? {};
|
|
63
|
-
const skills = MATRIX_SKILLS.map((skill) => {
|
|
64
|
-
const target = path.join(root, skill);
|
|
65
|
-
if (!pathExists(target)) return { skill, target, state: "missing" };
|
|
66
|
-
if (isBrokenLink(target) || !exists(path.join(target, "SKILL.md"))) return { skill, target, state: "damaged" };
|
|
67
|
-
const actual = hashDirectory(target); const recorded = records[skill]?.hash; const expected = catalog.skills[skill];
|
|
68
|
-
if (!recorded) return { skill, target, state: "user-modified", actual };
|
|
69
|
-
if (actual === expected) return { skill, target, state: "matching", actual };
|
|
70
|
-
if (actual === recorded) return { skill, target, state: compareVersions(manifest?.catalogVersion, catalog.version) > 0 ? "newer" : "outdated-unchanged", actual };
|
|
71
|
-
return { skill, target, state: "user-modified", actual };
|
|
72
|
-
});
|
|
73
|
-
const states = new Set(skills.map((item) => item.state));
|
|
74
|
-
const matrix = states.size === 1 && states.has("matching") ? "matching" : states.has("user-modified") ? "user-modified"
|
|
75
|
-
: states.has("missing") && states.size === 1 ? "missing" : states.has("newer") ? "newer"
|
|
76
|
-
: states.has("damaged") ? "damaged" : states.has("outdated-unchanged") ? "outdated-unchanged" : "partial";
|
|
77
|
-
const globalRoot = platform.skillRoot(intent.home);
|
|
78
|
-
const mattPresent = MATT_SKILLS.filter((skill) => skillPresent(root, skill));
|
|
79
|
-
const globalMatt = intent.scope === "project" ? MATT_SKILLS.filter((skill) => skillPresent(globalRoot, skill)) : [];
|
|
80
|
-
const availableMatt = new Set([...mattPresent, ...globalMatt]); const missing = MATT_SKILLS.filter((skill) => !availableMatt.has(skill));
|
|
81
|
-
const inherited = globalMatt.filter((skill) => !mattPresent.includes(skill)).length;
|
|
82
|
-
return { id: platformId, name: platform.name, root, skills, matrix, matt: { present: mattPresent.length, missing, total: MATT_SKILLS.length, inherited,
|
|
83
|
-
state: availableMatt.size === MATT_SKILLS.length ? "complete" : availableMatt.size ? "partial" : "missing" } };
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
function actionFor(observation, policy) {
|
|
87
|
-
if (observation.matrix === "matching" || observation.matrix === "newer") return "keep";
|
|
88
|
-
if (observation.matrix === "missing") return "install";
|
|
89
|
-
if (observation.matrix === "outdated-unchanged") return "backup-replace";
|
|
90
|
-
if (["damaged", "user-modified"].includes(observation.matrix)) return policy === "replace" ? "backup-replace" : "blocked";
|
|
91
|
-
return "repair";
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function recoverPendingTransaction(intent) {
|
|
95
|
-
const file = transactionPath(intent); const journal = readJson(file);
|
|
96
|
-
if (!journal) return { ok: true, recovered: false };
|
|
97
|
-
try {
|
|
98
|
-
if (journal.state === "committed") {
|
|
99
|
-
removeStaging(journal.stagingRoot);
|
|
100
|
-
for (const operation of journal.operations ?? []) removeEntry(operation.backup);
|
|
101
|
-
removeEntry(journal.manifest?.backup); removeEntry(file);
|
|
102
|
-
return { ok: true, recovered: true, receipt: `Finalized completed Matrix transaction ${journal.id}.` };
|
|
103
|
-
}
|
|
104
|
-
for (const operation of [...(journal.operations ?? [])].reverse()) {
|
|
105
|
-
if (operation.status !== "pending") removeEntry(operation.target);
|
|
106
|
-
if (operation.backup && pathExists(operation.backup)) { fs.mkdirSync(path.dirname(operation.target), { recursive: true }); fs.renameSync(operation.backup, operation.target); }
|
|
107
|
-
}
|
|
108
|
-
if (journal.manifest?.backup && pathExists(journal.manifest.backup)) {
|
|
109
|
-
removeEntry(journal.manifest.path); fs.mkdirSync(path.dirname(journal.manifest.path), { recursive: true }); fs.renameSync(journal.manifest.backup, journal.manifest.path);
|
|
110
|
-
} else if (journal.manifest?.written && !journal.manifest?.existed) removeEntry(journal.manifest.path);
|
|
111
|
-
removeStaging(journal.stagingRoot); removeEntry(file);
|
|
112
|
-
return { ok: true, recovered: true, receipt: `Recovered interrupted Matrix transaction ${journal.id}.` };
|
|
113
|
-
} catch (error) { return { ok: false, code: "RECOVERY_REQUIRED", recovery: `Keep ${file} and restore the paths recorded there before retrying.`, error: error.message }; }
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export function createDistribution({ failAt, mattAdapter = createMattAdapter() } = {}) {
|
|
117
|
-
const evaluate = (rawIntent) => {
|
|
118
|
-
const intent = normalizeIntent(rawIntent); const language = intent.language ?? rawIntent.language ?? "en";
|
|
119
|
-
if (intent.error) return { ok: false, code: intent.error, diagnostics: [{ code: intent.error, message: text(language, "noPlatform") }] };
|
|
120
|
-
if (!intent.platforms.length) { const detected = detectPlatforms(intent.projectRoot, intent.scope, intent.home); if (!detected.length) return { ok: false, code: "INPUT_REQUIRED", diagnostics: [{ code: "INPUT_REQUIRED", message: text(language, "noPlatform") }] }; intent.platforms = detected; }
|
|
121
|
-
let catalog; try { catalog = releaseCatalog(intent.language); } catch (error) { return { ok: false, code: "PACKAGE_INCOMPLETE", intent, diagnostics: [{ code: "PACKAGE_INCOMPLETE", message: error.message }] }; }
|
|
122
|
-
const manifest = readManifest(intent); const observations = intent.platforms.map((id) => observePlatform(intent, catalog, manifest, id));
|
|
123
|
-
const actions = observations.map((item) => ({ platform: item.id, matrix: actionFor(item, intent.matrixPolicy), matt: item.matt.state === "complete" || intent.matt === "none" ? { kind: "keep", skills: [] } : { kind: "install-missing", skills: item.matt.missing } }));
|
|
124
|
-
const blocked = actions.filter((item) => item.matrix === "blocked"); const plan = { version: VERSION, intent, catalogDigest: catalog.digest, manifestPath: manifestPath(intent.scope, intent.projectRoot, intent.home), actions, observations, preconditionDigest: digest(observations) };
|
|
125
|
-
plan.seal = digest({ intent, catalogDigest: plan.catalogDigest, actions, preconditionDigest: plan.preconditionDigest });
|
|
126
|
-
return { ok: blocked.length === 0, code: blocked.length ? "USER_MODIFIED_BLOCKED" : "OK", intent, observations, actions, plan, diagnostics: blocked.map((item) => ({ code: "USER_MODIFIED_BLOCKED", platform: item.platform, message: `${item.platform}: existing Matrix assets are not proven managed.` })), summary: `${text(intent.language, "preview")}: ${actions.map((item) => `${item.platform}: ${item.matrix}`).join(", ")}` };
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
return { evaluate,
|
|
130
|
-
diagnose(rawIntent) {
|
|
131
|
-
const evaluation = evaluate(rawIntent); if (!evaluation.intent) return evaluation;
|
|
132
|
-
const diagnostics = []; const pending = readJson(transactionPath(evaluation.intent)); const manifest = readManifest(evaluation.intent);
|
|
133
|
-
if (pending) diagnostics.push({ code: "RECOVERY_REQUIRED", message: text(evaluation.intent.language, "transactionPending") });
|
|
134
|
-
if (!manifest) diagnostics.push({ code: "MANIFEST_MISSING", message: text(evaluation.intent.language, "manifestMissing") });
|
|
135
|
-
for (const observation of evaluation.observations ?? []) {
|
|
136
|
-
diagnostics.push({ code: `MATRIX_${observation.matrix.toUpperCase().replaceAll("-", "_")}`, platform: observation.id, message: `${observation.name}: Matrix is ${observation.matrix}.` });
|
|
137
|
-
const matrixRoot = path.join(observation.root, "matrix");
|
|
138
|
-
if (pathExists(matrixRoot) && (!exists(matrixRoot) || !exists(path.join(matrixRoot, "scripts", "matrix-runtime.mjs")))) diagnostics.push({ code: "RUNTIME_MISSING", platform: observation.id, message: `${observation.name}: ${text(evaluation.intent.language, "runtimeMissing")}` });
|
|
139
|
-
for (const skill of observation.skills.filter((entry) => isBrokenLink(entry.target))) diagnostics.push({ code: "BROKEN_LINK", platform: observation.id, path: skill.target, message: `${observation.name}: ${text(evaluation.intent.language, "brokenLink")} ${skill.target}` });
|
|
140
|
-
if (observation.id === "codex" && PLATFORMS.codex.legacyRoots(scopeBase(evaluation.intent.scope, evaluation.intent.projectRoot, evaluation.intent.home)).some(exists)) diagnostics.push({ code: "CODEX_LEGACY_DETECTED", platform: observation.id, message: "Codex legacy .codex/skills was detected; Matrix will not write there." });
|
|
141
|
-
diagnostics.push({ code: `MATT_${observation.matt.state.toUpperCase()}`, platform: observation.id, message: `${observation.name}: Matt Pocock skills are ${observation.matt.state}.` });
|
|
142
|
-
if (observation.matt.inherited) diagnostics.push({ code: "MATT_INHERITED", platform: observation.id, message: `${observation.name}: ${observation.matt.inherited} ${text(evaluation.intent.language, "mattInherited")}` });
|
|
143
|
-
}
|
|
144
|
-
return { ...evaluation, diagnosis: diagnostics };
|
|
64
|
+
export function installedIntent({ projectRoot = process.cwd(), home = os.homedir() } = {}) {
|
|
65
|
+
const resolvedProjectRoot = path.resolve(projectRoot);
|
|
66
|
+
const config = readConfig(path.join(resolvedProjectRoot, ".matrix", "config.yaml"));
|
|
67
|
+
const scope = config.installation_scope === "global" ? "global" : "project";
|
|
68
|
+
const manifest = readJson(manifestPath(scope, resolvedProjectRoot, home));
|
|
69
|
+
const platforms = Object.keys(manifest?.platforms ?? {}).filter((id) => PLATFORM_IDS.includes(id));
|
|
70
|
+
if (!manifest || !platforms.length) return { ok: false, code: "MANIFEST_MISSING", scope, config };
|
|
71
|
+
const language = ["en", "zh-CN"].includes(manifest.language) ? manifest.language : (["en", "zh-CN"].includes(config.language) ? config.language : "en");
|
|
72
|
+
const defaultOrchestration = ["prim", "arch"].includes(manifest.orchestration?.default) ? manifest.orchestration.default : "prim";
|
|
73
|
+
const matt = manifest.orchestration?.available?.includes("arch") ? "missing" : "none";
|
|
74
|
+
return {
|
|
75
|
+
ok: true,
|
|
76
|
+
intent: {
|
|
77
|
+
projectRoot: resolvedProjectRoot, home, scope, platforms, language,
|
|
78
|
+
mode: ["copy", "symlink"].includes(manifest.mode) ? manifest.mode : "copy",
|
|
79
|
+
matt, defaultOrchestration, nonInteractive: true, matrixPolicy: "safe", dryRun: false
|
|
145
80
|
},
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
const recovery = recoverPendingTransaction(plan.intent); if (!recovery.ok) return recovery;
|
|
149
|
-
const refreshed = evaluate({ ...plan.intent, platforms: plan.intent.platforms });
|
|
150
|
-
if (!refreshed.plan || refreshed.plan.preconditionDigest !== plan.preconditionDigest || refreshed.plan.catalogDigest !== plan.catalogDigest) return { ok: false, code: "PLAN_STALE" };
|
|
151
|
-
if (!refreshed.ok) return { ok: false, code: refreshed.code, diagnostics: refreshed.diagnostics };
|
|
152
|
-
if (plan.intent.dryRun) return { ok: true, code: "DRY_RUN", actions: plan.actions };
|
|
153
|
-
const root = installationRoot(plan.intent.scope, plan.intent.projectRoot, plan.intent.home); const id = nowStamp(); const stagingRoot = path.join(root, "staging", id); const journalFile = transactionPath(plan.intent);
|
|
154
|
-
const journal = { id, state: "in-progress", stagingRoot, manifest: { path: plan.manifestPath, existed: pathExists(plan.manifestPath), backup: null, written: false }, operations: [] };
|
|
155
|
-
for (const item of refreshed.observations) if (plan.actions.find((entry) => entry.platform === item.id).matrix !== "keep") for (const skill of MATRIX_SKILLS) journal.operations.push({ target: path.join(item.root, skill), backup: null, status: "pending" });
|
|
156
|
-
const persist = () => writeJson(journalFile, journal);
|
|
157
|
-
try {
|
|
158
|
-
persist(); const catalog = releaseCatalog(plan.intent.language); const stagedPlatforms = new Map();
|
|
159
|
-
for (const item of refreshed.observations) {
|
|
160
|
-
if (plan.actions.find((entry) => entry.platform === item.id).matrix === "keep") continue;
|
|
161
|
-
const platformStage = path.join(stagingRoot, item.id);
|
|
162
|
-
for (const skill of MATRIX_SKILLS) { const staged = path.join(platformStage, skill); fs.mkdirSync(path.dirname(staged), { recursive: true }); fs.cpSync(path.join(catalog.root, skill), staged, { recursive: true }); fs.mkdirSync(path.join(staged, "scripts"), { recursive: true }); fs.copyFileSync(workflowRuntimeSource(), path.join(staged, "scripts", "matrix-runtime.mjs")); if (skill === "matrix") for (const filename of ["matrix_state.py", "matrix_claude.py"]) fs.copyFileSync(path.join(sourceSkillsRoot("en"), "matrix", "scripts", filename), path.join(staged, "scripts", filename)); if (!exists(path.join(staged, "SKILL.md"))) throw new Error(`Stage validation failed: ${item.id}/${skill}`); }
|
|
163
|
-
stagedPlatforms.set(item.id, platformStage);
|
|
164
|
-
}
|
|
165
|
-
failAt?.("after-stage"); let operationIndex = 0;
|
|
166
|
-
for (const item of refreshed.observations) {
|
|
167
|
-
if (plan.actions.find((entry) => entry.platform === item.id).matrix === "keep") continue;
|
|
168
|
-
fs.mkdirSync(item.root, { recursive: true });
|
|
169
|
-
for (const skill of MATRIX_SKILLS) {
|
|
170
|
-
const operation = journal.operations[operationIndex++]; const target = operation.target; operation.status = "backing-up";
|
|
171
|
-
if (pathExists(target)) operation.backup = path.join(root, "backups", id, item.id, skill); persist();
|
|
172
|
-
if (operation.backup) { fs.mkdirSync(path.dirname(operation.backup), { recursive: true }); fs.renameSync(target, operation.backup); }
|
|
173
|
-
operation.status = "backed-up"; persist(); failAt?.("after-backup", { platform: item.id, skill });
|
|
174
|
-
const staged = path.join(stagedPlatforms.get(item.id), skill);
|
|
175
|
-
if (plan.intent.mode === "symlink") { const store = path.join(root, "skills", catalog.digest, skill); if (!pathExists(store)) { fs.mkdirSync(path.dirname(store), { recursive: true }); fs.renameSync(staged, store); } fs.symlinkSync(store, target, process.platform === "win32" ? "junction" : "dir"); } else fs.renameSync(staged, target);
|
|
176
|
-
operation.status = "committed"; persist(); failAt?.("after-commit", { platform: item.id, skill });
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
if (journal.manifest.existed) { journal.manifest.backup = path.join(root, "backups", id, "installation.json"); persist(); fs.mkdirSync(path.dirname(journal.manifest.backup), { recursive: true }); fs.renameSync(plan.manifestPath, journal.manifest.backup); }
|
|
180
|
-
const records = Object.fromEntries(refreshed.observations.map((item) => [item.id, { root: item.root, skills: Object.fromEntries(MATRIX_SKILLS.map((skill) => [skill, { hash: catalog.skills[skill] }])) }]));
|
|
181
|
-
writeJson(plan.manifestPath, { version: VERSION, catalogVersion: catalog.version, language: plan.intent.language, mode: plan.intent.mode, catalogDigest: catalog.digest, platforms: records }); journal.manifest.written = true; persist(); failAt?.("after-manifest"); journal.state = "committed"; persist();
|
|
182
|
-
removeStaging(stagingRoot); removeEntry(journal.manifest.backup); removeEntry(journalFile);
|
|
183
|
-
const mattActions = plan.actions.filter((item) => item.matt.kind === "install-missing"); const mattResults = [];
|
|
184
|
-
for (const action of mattActions) { const result = mattAdapter.installMissing({ projectRoot: plan.intent.projectRoot, platforms: [action.platform], skills: action.matt.skills, global: plan.intent.scope === "global" }); mattResults.push({ platform: action.platform, ...result }); }
|
|
185
|
-
const postcondition = evaluate({ ...plan.intent, platforms: plan.intent.platforms }); const incomplete = mattActions.length ? postcondition.observations.filter((item) => item.matt.state !== "complete") : [];
|
|
186
|
-
const backups = journal.operations.filter((item) => item.backup).map((item) => ({ target: item.target, backup: item.backup }));
|
|
187
|
-
if (mattResults.some((item) => !item.ok) || incomplete.length) return { ok: true, code: "PARTIAL", committed: journal.operations.map((item) => item.target), backups, recovery: "Matrix is installed. Run matrix init --with-mattpocock after resolving the Matt skills installation.", matt: mattResults, recoveryReceipt: recovery.receipt };
|
|
188
|
-
return { ok: true, code: "OK", committed: journal.operations.map((item) => item.target), backups, matt: mattResults, recoveryReceipt: recovery.receipt };
|
|
189
|
-
} catch (error) { const restored = recoverPendingTransaction(plan.intent); return { ok: false, code: "MATRIX_COMMIT_FAILED", error: error.message, recovery: restored.ok ? "Matrix targets and installation manifest were restored." : restored.recovery }; }
|
|
190
|
-
}
|
|
81
|
+
manifest,
|
|
82
|
+
config
|
|
191
83
|
};
|
|
192
84
|
}
|
|
85
|
+
|
|
86
|
+
function detectPlatforms(projectRoot, scope, home) {
|
|
87
|
+
const bases = [scopeBase(scope, projectRoot, home)];
|
|
88
|
+
if (scope === "project") bases.push(home);
|
|
89
|
+
return PLATFORM_IDS.filter((id) => bases.some((base) => exists(PLATFORMS[id].skillRoot(base))));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function normalizeIntent(raw) {
|
|
93
|
+
const scope = raw.scope ?? "project";
|
|
94
|
+
const platforms = [...new Set(raw.platforms ?? [])];
|
|
95
|
+
if (!["project", "global"].includes(scope)) return { error: "INVALID_INTENT", detail: "scope" };
|
|
96
|
+
if (!["copy", "symlink"].includes(raw.mode ?? "copy")) return { error: "INVALID_INTENT", detail: "mode" };
|
|
97
|
+
if (!["en", "zh-CN"].includes(raw.language ?? "en")) return { error: "INVALID_INTENT", detail: "language" };
|
|
98
|
+
if (raw.defaultOrchestration != null && !["prim", "arch"].includes(raw.defaultOrchestration)) return { error: "INVALID_INTENT", detail: "default-orchestration" };
|
|
99
|
+
if (platforms.some((id) => !PLATFORM_IDS.includes(id))) return { error: "UNVERIFIED_PLATFORM" };
|
|
100
|
+
return { projectRoot: path.resolve(raw.projectRoot ?? process.cwd()), home: raw.home ?? os.homedir(), scope, platforms,
|
|
101
|
+
language: raw.language ?? "en", mode: raw.mode ?? "copy", matrixPolicy: raw.matrixPolicy ?? "safe",
|
|
102
|
+
matt: raw.matt ?? "none", defaultOrchestration: raw.defaultOrchestration ?? null,
|
|
103
|
+
nonInteractive: Boolean(raw.nonInteractive), dryRun: Boolean(raw.dryRun) };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function observePlatform(intent, catalog, manifest, platformId) {
|
|
107
|
+
const platform = PLATFORMS[platformId]; const base = scopeBase(intent.scope, intent.projectRoot, intent.home);
|
|
108
|
+
const root = platform.skillRoot(base); const records = manifest?.platforms?.[platformId]?.skills ?? {};
|
|
109
|
+
const skills = MATRIX_SKILLS.map((skill) => {
|
|
110
|
+
const target = path.join(root, skill);
|
|
111
|
+
if (!pathExists(target)) return { skill, target, state: "missing" };
|
|
112
|
+
if (isBrokenLink(target) || !exists(path.join(target, "SKILL.md"))) return { skill, target, state: "damaged" };
|
|
113
|
+
const actual = hashDirectory(target); const recorded = records[skill]?.hash; const expected = catalog.skills[skill];
|
|
114
|
+
if (!recorded) return { skill, target, state: "user-modified", actual };
|
|
115
|
+
if (actual === expected) return { skill, target, state: "matching", actual };
|
|
116
|
+
if (actual === recorded) return { skill, target, state: compareVersions(manifest?.catalogVersion, catalog.version) > 0 ? "newer" : "outdated-unchanged", actual };
|
|
117
|
+
return { skill, target, state: "user-modified", actual };
|
|
118
|
+
});
|
|
119
|
+
const states = new Set(skills.map((item) => item.state));
|
|
120
|
+
const matrix = states.size === 1 && states.has("matching") ? "matching" : states.has("user-modified") ? "user-modified"
|
|
121
|
+
: states.has("missing") && states.size === 1 ? "missing" : states.has("newer") ? "newer"
|
|
122
|
+
: states.has("damaged") ? "damaged" : states.has("outdated-unchanged") ? "outdated-unchanged" : "partial";
|
|
123
|
+
const globalRoot = platform.skillRoot(intent.home);
|
|
124
|
+
const mattPresent = MATT_SKILLS.filter((skill) => skillPresent(root, skill));
|
|
125
|
+
const globalMatt = intent.scope === "project" ? MATT_SKILLS.filter((skill) => skillPresent(globalRoot, skill)) : [];
|
|
126
|
+
const availableMatt = new Set([...mattPresent, ...globalMatt]); const missing = MATT_SKILLS.filter((skill) => !availableMatt.has(skill));
|
|
127
|
+
const inherited = globalMatt.filter((skill) => !mattPresent.includes(skill)).length;
|
|
128
|
+
const managedSkills = Object.fromEntries([...availableMatt].map((skill) => {
|
|
129
|
+
const target = mattPresent.includes(skill) ? path.join(root, skill) : path.join(globalRoot, skill);
|
|
130
|
+
return [skill, { root: target, hash: hashDirectory(target) }];
|
|
131
|
+
}));
|
|
132
|
+
const unmanaged = UNMANAGED_MATT_SKILLS.filter((skill) => skillPresent(root, skill) || (intent.scope === "project" && skillPresent(globalRoot, skill)));
|
|
133
|
+
return { id: platformId, name: platform.name, root, skills, matrix, matt: { present: mattPresent.length, missing, total: MATT_SKILLS.length, inherited,
|
|
134
|
+
state: availableMatt.size === MATT_SKILLS.length ? "complete" : availableMatt.size ? "partial" : "missing", skills: managedSkills, unmanaged } };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function actionFor(observation, policy) {
|
|
138
|
+
if (observation.matrix === "matching" || observation.matrix === "newer") return "keep";
|
|
139
|
+
if (observation.matrix === "missing") return "install";
|
|
140
|
+
if (observation.matrix === "outdated-unchanged") return "backup-replace";
|
|
141
|
+
if (["damaged", "user-modified"].includes(observation.matrix)) return policy === "replace" ? "backup-replace" : "blocked";
|
|
142
|
+
return "repair";
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function recoverPendingTransaction(intent) {
|
|
146
|
+
const file = transactionPath(intent); const journal = readJson(file);
|
|
147
|
+
if (!journal) return { ok: true, recovered: false };
|
|
148
|
+
const restoreMetadata = (entry) => {
|
|
149
|
+
if (!entry) return;
|
|
150
|
+
if (entry.backup && pathExists(entry.backup)) {
|
|
151
|
+
removeEntry(entry.path);
|
|
152
|
+
fs.mkdirSync(path.dirname(entry.path), { recursive: true });
|
|
153
|
+
fs.renameSync(entry.backup, entry.path);
|
|
154
|
+
} else if (entry.written && !entry.existed) removeEntry(entry.path);
|
|
155
|
+
};
|
|
156
|
+
try {
|
|
157
|
+
if (journal.state === "committed") {
|
|
158
|
+
removeStaging(journal.stagingRoot);
|
|
159
|
+
for (const operation of journal.operations ?? []) removeEntry(operation.backup);
|
|
160
|
+
removeEntry(journal.manifest?.backup); removeEntry(journal.config?.backup); removeEntry(file);
|
|
161
|
+
return { ok: true, recovered: true, receipt: `Finalized completed Matrix transaction ${journal.id}.` };
|
|
162
|
+
}
|
|
163
|
+
for (const operation of [...(journal.operations ?? [])].reverse()) {
|
|
164
|
+
if (operation.status !== "pending") removeEntry(operation.target);
|
|
165
|
+
if (operation.backup && pathExists(operation.backup)) { fs.mkdirSync(path.dirname(operation.target), { recursive: true }); fs.renameSync(operation.backup, operation.target); }
|
|
166
|
+
}
|
|
167
|
+
restoreMetadata(journal.config);
|
|
168
|
+
restoreMetadata(journal.manifest);
|
|
169
|
+
removeStaging(journal.stagingRoot); removeEntry(file);
|
|
170
|
+
return { ok: true, recovered: true, receipt: `Recovered interrupted Matrix transaction ${journal.id}.` };
|
|
171
|
+
} catch (error) { return { ok: false, code: "RECOVERY_REQUIRED", recovery: `Keep ${file} and restore the paths recorded there before retrying.`, error: error.message }; }
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function createDistribution({ failAt, mattAdapter = createMattAdapter() } = {}) {
|
|
175
|
+
const evaluate = (rawIntent) => {
|
|
176
|
+
const intent = normalizeIntent(rawIntent); const language = intent.language ?? rawIntent.language ?? "en";
|
|
177
|
+
if (intent.error) return { ok: false, code: intent.error, diagnostics: [{ code: intent.error, message: text(language, "noPlatform") }] };
|
|
178
|
+
if (!intent.platforms.length) { const detected = detectPlatforms(intent.projectRoot, intent.scope, intent.home); if (!detected.length) return { ok: false, code: "INPUT_REQUIRED", diagnostics: [{ code: "INPUT_REQUIRED", message: text(language, "noPlatform") }] }; intent.platforms = detected; }
|
|
179
|
+
let catalog; try { catalog = releaseCatalog(intent.language); } catch (error) { return { ok: false, code: "PACKAGE_INCOMPLETE", intent, diagnostics: [{ code: "PACKAGE_INCOMPLETE", message: error.message }] }; }
|
|
180
|
+
const manifest = readManifest(intent);
|
|
181
|
+
intent.platforms = [...new Set([...Object.keys(manifest?.platforms ?? {}), ...intent.platforms])];
|
|
182
|
+
const observations = intent.platforms.map((id) => observePlatform(intent, catalog, manifest, id));
|
|
183
|
+
const existingConfig = readConfig(configPath(intent));
|
|
184
|
+
const existingDefault = ["prim", "arch"].includes(existingConfig.default_orchestration) ? existingConfig.default_orchestration
|
|
185
|
+
: ["prim", "arch"].includes(manifest?.orchestration?.default) ? manifest.orchestration.default : null;
|
|
186
|
+
intent.defaultOrchestration = intent.defaultOrchestration ?? existingDefault ?? (intent.matt === "missing" && intent.nonInteractive ? "arch" : "prim");
|
|
187
|
+
const actions = observations.map((item) => ({ platform: item.id, matrix: actionFor(item, intent.matrixPolicy), matt: item.matt.state === "complete" || intent.matt === "none" ? { kind: "keep", skills: [] } : { kind: "install-missing", skills: item.matt.missing } }));
|
|
188
|
+
const blocked = actions.filter((item) => item.matrix === "blocked");
|
|
189
|
+
const archMustRemainAvailable = manifest?.orchestration?.available?.includes("arch") || intent.defaultOrchestration === "arch";
|
|
190
|
+
const incompleteWithoutInstall = archMustRemainAvailable && actions.some((item, index) => observations[index].matt.state !== "complete" && item.matt.kind !== "install-missing");
|
|
191
|
+
const plan = { version: VERSION, intent, catalogDigest: catalog.digest, manifestPath: manifestPath(intent.scope, intent.projectRoot, intent.home), configPath: configPath(intent), actions, observations, preconditionDigest: digest(observations) };
|
|
192
|
+
plan.seal = digest({ intent, catalogDigest: plan.catalogDigest, actions, preconditionDigest: plan.preconditionDigest });
|
|
193
|
+
const code = blocked.length ? "USER_MODIFIED_BLOCKED" : incompleteWithoutInstall ? "ARCH_INSTALLATION_INCOMPLETE" : "OK";
|
|
194
|
+
return { ok: code === "OK", code, intent, observations, actions, plan, diagnostics: [
|
|
195
|
+
...blocked.map((item) => ({ code: "USER_MODIFIED_BLOCKED", platform: item.platform, message: `${item.platform}: existing Matrix assets are not proven managed.` })),
|
|
196
|
+
...(incompleteWithoutInstall ? [{ code: "ARCH_INSTALLATION_INCOMPLETE", message: "Arch requires the complete managed cohort on every configured platform. Retry with --with-mattpocock." }] : [])
|
|
197
|
+
], summary: `${text(intent.language, "preview")}: ${actions.map((item) => `${item.platform}: ${item.matrix}`).join(", ")}` };
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
return { evaluate,
|
|
201
|
+
diagnose(rawIntent) {
|
|
202
|
+
const evaluation = evaluate(rawIntent); if (!evaluation.intent) return evaluation;
|
|
203
|
+
const diagnostics = []; const pending = readJson(transactionPath(evaluation.intent)); const manifest = readManifest(evaluation.intent);
|
|
204
|
+
if (pending) diagnostics.push({ code: "RECOVERY_REQUIRED", message: text(evaluation.intent.language, "transactionPending") });
|
|
205
|
+
if (!manifest) diagnostics.push({ code: "MANIFEST_MISSING", message: text(evaluation.intent.language, "manifestMissing") });
|
|
206
|
+
for (const observation of evaluation.observations ?? []) {
|
|
207
|
+
diagnostics.push({ code: `MATRIX_${observation.matrix.toUpperCase().replaceAll("-", "_")}`, platform: observation.id, message: `${observation.name}: Matrix is ${observation.matrix}.` });
|
|
208
|
+
const matrixRoot = path.join(observation.root, "matrix");
|
|
209
|
+
if (pathExists(matrixRoot) && (!exists(matrixRoot) || !exists(path.join(matrixRoot, "scripts", "matrix-runtime.mjs")) || !exists(path.join(matrixRoot, "scripts", "workflow-transaction.js")))) diagnostics.push({ code: "RUNTIME_MISSING", platform: observation.id, message: `${observation.name}: ${text(evaluation.intent.language, "runtimeMissing")}` });
|
|
210
|
+
for (const skill of observation.skills.filter((entry) => isBrokenLink(entry.target))) diagnostics.push({ code: "BROKEN_LINK", platform: observation.id, path: skill.target, message: `${observation.name}: ${text(evaluation.intent.language, "brokenLink")} ${skill.target}` });
|
|
211
|
+
if (observation.id === "codex" && PLATFORMS.codex.legacyRoots(scopeBase(evaluation.intent.scope, evaluation.intent.projectRoot, evaluation.intent.home)).some(exists)) diagnostics.push({ code: "CODEX_LEGACY_DETECTED", platform: observation.id, message: "Codex legacy .codex/skills was detected; Matrix will not write there." });
|
|
212
|
+
diagnostics.push({ code: `MATT_${observation.matt.state.toUpperCase()}`, platform: observation.id, message: `${observation.name}: Matt Pocock skills are ${observation.matt.state}.` });
|
|
213
|
+
if (observation.matt.inherited) diagnostics.push({ code: "MATT_INHERITED", platform: observation.id, message: `${observation.name}: ${observation.matt.inherited} ${text(evaluation.intent.language, "mattInherited")}` });
|
|
214
|
+
if (observation.matt.unmanaged.length) diagnostics.push({ code: "ARCH_UNMANAGED_EXTRA", platform: observation.id, skills: observation.matt.unmanaged, message: `${observation.name}: unmanaged extra Skills are preserved and will not be invoked: ${observation.matt.unmanaged.join(", ")}.` });
|
|
215
|
+
}
|
|
216
|
+
return { ...evaluation, diagnosis: diagnostics };
|
|
217
|
+
},
|
|
218
|
+
commit(plan) {
|
|
219
|
+
if (!plan?.seal || plan.seal !== digest({ intent: plan.intent, catalogDigest: plan.catalogDigest, actions: plan.actions, preconditionDigest: plan.preconditionDigest })) return { ok: false, code: "INVALID_INTENT" };
|
|
220
|
+
const recovery = recoverPendingTransaction(plan.intent); if (!recovery.ok) return recovery;
|
|
221
|
+
const refreshed = evaluate({ ...plan.intent, platforms: plan.intent.platforms });
|
|
222
|
+
if (!refreshed.plan || refreshed.plan.preconditionDigest !== plan.preconditionDigest || refreshed.plan.catalogDigest !== plan.catalogDigest) return { ok: false, code: "PLAN_STALE" };
|
|
223
|
+
if (!refreshed.ok) return { ok: false, code: refreshed.code, diagnostics: refreshed.diagnostics };
|
|
224
|
+
if (plan.intent.dryRun) return { ok: true, code: "DRY_RUN", actions: plan.actions };
|
|
225
|
+
const root = installationRoot(plan.intent.scope, plan.intent.projectRoot, plan.intent.home); const id = nowStamp(); const stagingRoot = path.join(root, "staging", id); const journalFile = transactionPath(plan.intent);
|
|
226
|
+
const journal = {
|
|
227
|
+
id, state: "in-progress", stagingRoot,
|
|
228
|
+
manifest: { path: plan.manifestPath, existed: pathExists(plan.manifestPath), backup: null, written: false },
|
|
229
|
+
config: { path: plan.configPath, existed: pathExists(plan.configPath), backup: null, written: false },
|
|
230
|
+
operations: []
|
|
231
|
+
};
|
|
232
|
+
for (const item of refreshed.observations) if (plan.actions.find((entry) => entry.platform === item.id).matrix !== "keep") for (const skill of MATRIX_SKILLS) journal.operations.push({ target: path.join(item.root, skill), backup: null, status: "pending" });
|
|
233
|
+
const persist = () => writeJson(journalFile, journal);
|
|
234
|
+
try {
|
|
235
|
+
persist(); const catalog = releaseCatalog(plan.intent.language); const stagedPlatforms = new Map();
|
|
236
|
+
for (const item of refreshed.observations) {
|
|
237
|
+
if (plan.actions.find((entry) => entry.platform === item.id).matrix === "keep") continue;
|
|
238
|
+
const platformStage = path.join(stagingRoot, item.id);
|
|
239
|
+
for (const skill of MATRIX_SKILLS) { const staged = path.join(platformStage, skill); fs.mkdirSync(path.dirname(staged), { recursive: true }); fs.cpSync(path.join(catalog.root, skill), staged, { recursive: true }); fs.mkdirSync(path.join(staged, "scripts"), { recursive: true }); fs.copyFileSync(workflowRuntimeSource(), path.join(staged, "scripts", "matrix-runtime.mjs")); fs.copyFileSync(workflowTransactionSource(), path.join(staged, "scripts", "workflow-transaction.js")); if (skill === "matrix") for (const filename of ["matrix_state.py", "matrix_claude.py"]) fs.copyFileSync(path.join(sourceSkillsRoot("en"), "matrix", "scripts", filename), path.join(staged, "scripts", filename)); if (!exists(path.join(staged, "SKILL.md"))) throw new Error(`Stage validation failed: ${item.id}/${skill}`); }
|
|
240
|
+
stagedPlatforms.set(item.id, platformStage);
|
|
241
|
+
}
|
|
242
|
+
failAt?.("after-stage"); let operationIndex = 0;
|
|
243
|
+
for (const item of refreshed.observations) {
|
|
244
|
+
if (plan.actions.find((entry) => entry.platform === item.id).matrix === "keep") continue;
|
|
245
|
+
fs.mkdirSync(item.root, { recursive: true });
|
|
246
|
+
for (const skill of MATRIX_SKILLS) {
|
|
247
|
+
const operation = journal.operations[operationIndex++]; const target = operation.target; operation.status = "backing-up";
|
|
248
|
+
if (pathExists(target)) operation.backup = path.join(root, "backups", id, item.id, skill); persist();
|
|
249
|
+
if (operation.backup) { fs.mkdirSync(path.dirname(operation.backup), { recursive: true }); fs.renameSync(target, operation.backup); }
|
|
250
|
+
operation.status = "backed-up"; persist(); failAt?.("after-backup", { platform: item.id, skill });
|
|
251
|
+
const staged = path.join(stagedPlatforms.get(item.id), skill);
|
|
252
|
+
if (plan.intent.mode === "symlink") { const store = path.join(root, "skills", catalog.digest, skill); if (!pathExists(store)) { fs.mkdirSync(path.dirname(store), { recursive: true }); fs.renameSync(staged, store); } fs.symlinkSync(store, target, process.platform === "win32" ? "junction" : "dir"); } else fs.renameSync(staged, target);
|
|
253
|
+
operation.status = "committed"; persist(); failAt?.("after-commit", { platform: item.id, skill });
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const mattActions = plan.actions.filter((item) => item.matt.kind === "install-missing"); const mattResults = [];
|
|
257
|
+
for (const action of mattActions) { const result = mattAdapter.installMissing({ projectRoot: plan.intent.projectRoot, platforms: [action.platform], skills: action.matt.skills, global: plan.intent.scope === "global" }); mattResults.push({ platform: action.platform, ...result }); }
|
|
258
|
+
const postObservations = plan.intent.platforms.map((platform) => observePlatform(plan.intent, catalog, readManifest(plan.intent), platform));
|
|
259
|
+
const archRequired = plan.intent.defaultOrchestration === "arch" || readManifest(plan.intent)?.orchestration?.available?.includes("arch");
|
|
260
|
+
const incomplete = archRequired || mattActions.length ? postObservations.filter((item) => item.matt.state !== "complete") : [];
|
|
261
|
+
const backups = journal.operations.filter((item) => item.backup).map((item) => ({ target: item.target, backup: item.backup }));
|
|
262
|
+
if (mattResults.some((item) => !item.ok) || incomplete.length) {
|
|
263
|
+
const restored = recoverPendingTransaction(plan.intent);
|
|
264
|
+
return { ok: true, code: "PARTIAL", committed: [], backups: [], recovery: "The platform was not added. Repair the Arch Skill installation and rerun matrix init --with-mattpocock.", matt: mattResults, recoveryReceipt: restored.receipt ?? recovery.receipt };
|
|
265
|
+
}
|
|
266
|
+
if (journal.manifest.existed) { journal.manifest.backup = path.join(root, "backups", id, "installation.json"); persist(); fs.mkdirSync(path.dirname(journal.manifest.backup), { recursive: true }); fs.renameSync(plan.manifestPath, journal.manifest.backup); }
|
|
267
|
+
if (journal.config.existed) { journal.config.backup = path.join(root, "backups", id, "config.yaml"); persist(); fs.mkdirSync(path.dirname(journal.config.backup), { recursive: true }); fs.renameSync(plan.configPath, journal.config.backup); }
|
|
268
|
+
const records = Object.fromEntries(postObservations.map((item) => [item.id, {
|
|
269
|
+
root: item.root,
|
|
270
|
+
skills: Object.fromEntries(MATRIX_SKILLS.map((skill) => [skill, { hash: hashDirectory(path.join(item.root, skill)) }])),
|
|
271
|
+
matt: { state: item.matt.state, skills: item.matt.skills }
|
|
272
|
+
}]));
|
|
273
|
+
const available = postObservations.every((item) => item.matt.state === "complete") ? ["prim", "arch"] : ["prim"];
|
|
274
|
+
const previousConfig = readConfig(journal.config.backup ?? plan.configPath);
|
|
275
|
+
fs.mkdirSync(path.dirname(plan.configPath), { recursive: true });
|
|
276
|
+
fs.writeFileSync(plan.configPath, configContent(previousConfig, plan.intent.defaultOrchestration, plan.intent.scope, plan.intent.language)); journal.config.written = true; persist();
|
|
277
|
+
writeJson(plan.manifestPath, {
|
|
278
|
+
version: VERSION, catalogVersion: catalog.version, language: plan.intent.language, mode: plan.intent.mode, scope: plan.intent.scope,
|
|
279
|
+
catalogDigest: catalog.digest, orchestration: { default: plan.intent.defaultOrchestration, available }, platforms: records
|
|
280
|
+
});
|
|
281
|
+
journal.manifest.written = true; persist(); failAt?.("after-manifest"); journal.state = "committed"; persist();
|
|
282
|
+
removeStaging(stagingRoot); removeEntry(journal.manifest.backup); removeEntry(journal.config.backup); removeEntry(journalFile);
|
|
283
|
+
return { ok: true, code: "OK", committed: journal.operations.map((item) => item.target), backups, matt: mattResults, recoveryReceipt: recovery.receipt };
|
|
284
|
+
} catch (error) { const restored = recoverPendingTransaction(plan.intent); return { ok: false, code: "MATRIX_COMMIT_FAILED", error: error.message, recovery: restored.ok ? "Matrix targets and installation manifest were restored." : restored.recovery }; }
|
|
285
|
+
}
|
|
286
|
+
};
|
|
287
|
+
}
|
package/src/installer.js
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { createDistribution } from "./distribution.js";
|
|
4
|
-
import { MATRIX_SKILLS, MATT_SKILLS, PLATFORMS, sourceSkillsRoot } from "./catalog.js";
|
|
5
|
-
import { createMattAdapter } from "./matt.js";
|
|
6
|
-
|
|
7
|
-
export { MATRIX_SKILLS, MATT_SKILLS, sourceSkillsRoot };
|
|
8
|
-
|
|
9
|
-
export function destinationFor({ scope, projectRoot, platform = "claude-code", home }) {
|
|
10
|
-
const base = scope === "global" ? (home ?? process.env.USERPROFILE ?? process.env.HOME) : projectRoot;
|
|
11
|
-
return PLATFORMS[platform].skillRoot(base);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function inspectInstallation(destination) {
|
|
15
|
-
const inspect = (names) => names.filter((skill) => {
|
|
16
|
-
try { return Boolean(destination) && requireExists(destination, skill); } catch { return false; }
|
|
17
|
-
});
|
|
18
|
-
const installed = inspect(MATRIX_SKILLS);
|
|
19
|
-
const mattInstalled = inspect(MATT_SKILLS);
|
|
20
|
-
return { installed, missing: MATRIX_SKILLS.filter((skill) => !installed.includes(skill)), mattInstalled, mattMissing: MATT_SKILLS.filter((skill) => !mattInstalled.includes(skill)) };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function requireExists(destination, skill) {
|
|
24
|
-
// Avoid exposing a second installation policy through this legacy helper.
|
|
25
|
-
return fs.existsSync(path.join(destination, skill, "SKILL.md"));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export function installMatrix({ destination, mode = "copy", conflict = "error", dryRun = false }) {
|
|
29
|
-
const platform = destination?.includes(".agents") ? "codex" : "claude-code";
|
|
30
|
-
const projectRoot = path.resolve(destination, "..", "..");
|
|
31
|
-
const distribution = createDistribution();
|
|
32
|
-
const evaluation = distribution.evaluate({ projectRoot, scope: "project", platforms: [platform], mode, matrixPolicy: conflict === "replace" ? "replace" : "safe", dryRun });
|
|
33
|
-
if (!evaluation.ok) throw new Error(evaluation.diagnostics?.[0]?.message ?? evaluation.code);
|
|
34
|
-
const result = distribution.commit(evaluation.plan);
|
|
35
|
-
if (!result.ok) throw new Error(result.error ?? result.code);
|
|
36
|
-
return { installed: result.committed ?? MATRIX_SKILLS, skipped: [], existing: [], backupRoot: result.backups?.[0] ? result.backups[0].backup : null };
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export function installMattSkills({ projectRoot, platforms = ["claude-code"], skills = MATT_SKILLS, global = false, timeout = 300_000 }) {
|
|
40
|
-
const result = createMattAdapter().installMissing({ projectRoot, platforms, skills, global, timeout });
|
|
41
|
-
if (!result.ok) throw new Error(`${result.code}: ${result.message}`);
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function findPython() { return null; }
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { createDistribution } from "./distribution.js";
|
|
4
|
+
import { MATRIX_SKILLS, MATT_SKILLS, PLATFORMS, sourceSkillsRoot } from "./catalog.js";
|
|
5
|
+
import { createMattAdapter } from "./matt.js";
|
|
6
|
+
|
|
7
|
+
export { MATRIX_SKILLS, MATT_SKILLS, sourceSkillsRoot };
|
|
8
|
+
|
|
9
|
+
export function destinationFor({ scope, projectRoot, platform = "claude-code", home }) {
|
|
10
|
+
const base = scope === "global" ? (home ?? process.env.USERPROFILE ?? process.env.HOME) : projectRoot;
|
|
11
|
+
return PLATFORMS[platform].skillRoot(base);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function inspectInstallation(destination) {
|
|
15
|
+
const inspect = (names) => names.filter((skill) => {
|
|
16
|
+
try { return Boolean(destination) && requireExists(destination, skill); } catch { return false; }
|
|
17
|
+
});
|
|
18
|
+
const installed = inspect(MATRIX_SKILLS);
|
|
19
|
+
const mattInstalled = inspect(MATT_SKILLS);
|
|
20
|
+
return { installed, missing: MATRIX_SKILLS.filter((skill) => !installed.includes(skill)), mattInstalled, mattMissing: MATT_SKILLS.filter((skill) => !mattInstalled.includes(skill)) };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function requireExists(destination, skill) {
|
|
24
|
+
// Avoid exposing a second installation policy through this legacy helper.
|
|
25
|
+
return fs.existsSync(path.join(destination, skill, "SKILL.md"));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function installMatrix({ destination, mode = "copy", conflict = "error", dryRun = false }) {
|
|
29
|
+
const platform = destination?.includes(".agents") ? "codex" : "claude-code";
|
|
30
|
+
const projectRoot = path.resolve(destination, "..", "..");
|
|
31
|
+
const distribution = createDistribution();
|
|
32
|
+
const evaluation = distribution.evaluate({ projectRoot, scope: "project", platforms: [platform], mode, matrixPolicy: conflict === "replace" ? "replace" : "safe", dryRun });
|
|
33
|
+
if (!evaluation.ok) throw new Error(evaluation.diagnostics?.[0]?.message ?? evaluation.code);
|
|
34
|
+
const result = distribution.commit(evaluation.plan);
|
|
35
|
+
if (!result.ok) throw new Error(result.error ?? result.code);
|
|
36
|
+
return { installed: result.committed ?? MATRIX_SKILLS, skipped: [], existing: [], backupRoot: result.backups?.[0] ? result.backups[0].backup : null };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function installMattSkills({ projectRoot, platforms = ["claude-code"], skills = MATT_SKILLS, global = false, timeout = 300_000 }) {
|
|
40
|
+
const result = createMattAdapter().installMissing({ projectRoot, platforms, skills, global, timeout });
|
|
41
|
+
if (!result.ok) throw new Error(`${result.code}: ${result.message}`);
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function findPython() { return null; }
|