nexakit 0.1.0 → 0.3.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/.claude-plugin/marketplace.json +18 -1
- package/README.md +51 -30
- package/assets/brand/README.md +0 -2
- package/assets/runtime/package-lock.json +2 -2
- package/bin/agent-task.js +12 -10
- package/bin/agent-toolkit-gemini.js +11 -9
- package/bin/agent-toolkit-inbox.js +12 -10
- package/bin/agent-toolkit-mcp.js +14 -8
- package/bin/agent-toolkit.js +12 -9
- package/bin/nexakit.js +12 -9
- package/bin/project-memory-mcp.js +12 -9
- package/bin/project-memory.js +5 -3
- package/catalogs/generadores.json +936 -0
- package/{skills/depurar → catalogs/skills/vercel-composition-patterns}/LICENSE.txt +6 -6
- package/catalogs/skills/vercel-react-best-practices/LICENSE.txt +27 -0
- package/catalogs/skills.json +70 -35
- package/docs/skills/evidencias/2026-10-28-recomendador-preparacion.md +1 -1
- package/package.json +5 -3
- package/plugins/nexakit-modelo/.claude-plugin/plugin.json +1 -0
- package/plugins/nexakit-modelo/hooks/hooks.json +1 -0
- package/plugins/nexakit-modelo/hooks/register.js +48 -0
- package/plugins/nexakit-modelo/hooks/status.js +8 -0
- package/plugins/nexakit-modelo/tests/register.test.ts +21 -0
- package/plugins/nexakit-panel/.claude-plugin/plugin.json +1 -0
- package/plugins/nexakit-panel/README.md +20 -0
- package/plugins/nexakit-panel/hooks/hooks.json +1 -0
- package/plugins/nexakit-panel/hooks/lienzo.js +382 -0
- package/plugins/nexakit-panel/hooks/panel.js +273 -0
- package/plugins/nexakit-panel/hooks/redact.js +9 -0
- package/plugins/nexakit-panel/hooks/register.tsx +248 -0
- package/plugins/nexakit-panel/tests/lienzo.test.ts +142 -0
- package/plugins/nexakit-panel/tests/panel.test.ts +240 -0
- package/plugins/nexakit-panel/tests/register.test.ts +167 -0
- package/plugins/nexakit-panel/types/index.d.ts +41 -0
- package/plugins/nexakit-sesion/.claude-plugin/plugin.json +1 -0
- package/plugins/nexakit-sesion/README.md +12 -0
- package/plugins/nexakit-sesion/hooks/aviso.js +38 -0
- package/plugins/nexakit-sesion/hooks/hooks.json +1 -0
- package/plugins/nexakit-sesion/hooks/register.js +28 -0
- package/plugins/nexakit-sesion/tests/register.test.ts +96 -0
- package/schemas/generators.schema.json +299 -0
- package/schemas/skill-catalog.schema.json +9 -2
- package/scripts/prepare-package.js +3 -0
- package/skills/cerrar-memoria/SKILL.md +6 -2
- package/skills/cerrar-memoria/references/cierre.md +5 -1
- package/skills/elegir-modelo/SKILL.md +68 -0
- package/skills/elegir-modelo/modelos.mjs +108 -0
- package/skills/gemini/SKILL.md +1 -1
- package/skills/project-handoff/SKILL.md +39 -12
- package/skills/project-handoff/references/handoff-template.md +24 -17
- package/skills/quality-gate/SKILL.md +6 -12
- package/skills/revisar/SKILL.md +6 -16
- package/skills/{desarrollo-con-pruebas/LICENSE.txt → revisar-skill/LICENSE-find-skills.txt} +5 -5
- package/skills/revisar-skill/SKILL.md +94 -0
- package/skills/revision-cruzada/SKILL.md +53 -475
- package/skills/web-design-guidelines/AGENTS.md +156 -0
- package/skills/{spec-driven-development → web-design-guidelines}/LICENSE.txt +1 -6
- package/skills/web-design-guidelines/SKILL.md +20 -30
- package/src/agent-models.js +53 -13
- package/src/brand.js +117 -0
- package/src/clean-command.js +29 -0
- package/src/clean-project-flow.js +105 -0
- package/src/clean-project.js +431 -0
- package/src/cli-executable.js +6 -4
- package/src/cli.js +16 -9
- package/src/connect.js +56 -4
- package/src/gemini-files.js +3 -3
- package/src/gemini-tools.js +23 -8
- package/src/generators-catalog.js +63 -0
- package/src/init.js +9 -1
- package/src/install-lock.js +127 -0
- package/src/install-registry.js +61 -0
- package/src/legacy-repair.js +71 -0
- package/src/link-states.js +172 -0
- package/src/memory-locator.js +118 -0
- package/src/missing-dependencies.js +21 -0
- package/src/model-command.js +147 -0
- package/src/panel-cli.js +182 -0
- package/src/project-root.js +60 -0
- package/src/project-signals.js +345 -0
- package/src/recommend-report.js +77 -0
- package/src/redact.js +9 -0
- package/src/review-instructions.js +211 -0
- package/src/review-policy.js +35 -0
- package/src/revision-adapters.js +157 -0
- package/src/revision-context.js +86 -0
- package/src/revision-isolation.js +222 -0
- package/src/revision-process.js +103 -0
- package/src/revision-provenance.js +89 -0
- package/src/revision-snapshot.js +120 -0
- package/src/revision.js +242 -0
- package/src/setup-apply.js +231 -0
- package/src/setup-plan.js +231 -0
- package/src/setup-state.js +284 -0
- package/src/setup-tui.js +647 -0
- package/src/skill-catalog-add.js +91 -0
- package/src/skill-catalog-audit.js +26 -27
- package/src/skill-files.js +39 -0
- package/src/skill-recommendations.js +344 -208
- package/src/skill-scan-report.js +14 -0
- package/src/skill-scan.js +178 -0
- package/src/stable-command.js +53 -0
- package/src/stable-install.js +222 -0
- package/src/superpowers.js +133 -0
- package/src/sync-apply.js +8 -5
- package/src/sync-files.js +2 -1
- package/src/terminal-text.js +68 -0
- package/src/toolkit-cli.js +397 -70
- package/src/toolkit-commands-guide.js +53 -39
- package/src/toolkit-setup.js +507 -110
- package/assets/brand/agent-toolkit-mark-v1.png +0 -0
- package/skills/depurar/SKILL.md +0 -69
- package/skills/depurar/agents/openai.yaml +0 -7
- package/skills/depurar/references/tecnicas-de-diagnostico.md +0 -37
- package/skills/desarrollo-con-pruebas/SKILL.md +0 -55
- package/skills/desarrollo-con-pruebas/agents/openai.yaml +0 -7
- package/skills/desarrollo-con-pruebas/references/buenas-pruebas.md +0 -33
- package/skills/estado/SKILL.md +0 -41
- package/skills/programar/SKILL.md +0 -19
- package/skills/spec-driven-development/SKILL.md +0 -48
- package/skills/spec-driven-development/agents/openai.yaml +0 -7
- package/skills/spec-driven-development/references/spec-template.md +0 -55
- package/skills/spec-driven-development/references/workflow.md +0 -70
- package/src/toolkit-tui.js +0 -961
package/src/gemini-tools.js
CHANGED
|
@@ -129,17 +129,32 @@ async function collectImages(cwd, conversationId, { brainDirectory = brain(), wa
|
|
|
129
129
|
} catch (error) { if (error.code !== "ENOENT") throw error; }
|
|
130
130
|
if (names.length) {
|
|
131
131
|
if (names.length > 16) throw new GeminiError("Demasiados artefactos de imagen.");
|
|
132
|
-
|
|
132
|
+
// Read the complete batch before publishing any file. A producer can still
|
|
133
|
+
// be writing when readdir first exposes an image; retry only transient reads.
|
|
134
|
+
const images = [];
|
|
135
|
+
let pending = false;
|
|
133
136
|
for (const name of names) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
137
|
+
try {
|
|
138
|
+
const bytes = await readRegular(path.join(artifacts, name));
|
|
139
|
+
if (!bytes.length) { pending = true; break; }
|
|
140
|
+
images.push({ name, bytes });
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (!['EFILECHANGED', 'ENOENT'].includes(error.code)) throw error;
|
|
143
|
+
pending = true; break;
|
|
139
144
|
}
|
|
140
|
-
files.push(destination);
|
|
141
145
|
}
|
|
142
|
-
|
|
146
|
+
if (!pending) {
|
|
147
|
+
const files = [];
|
|
148
|
+
for (const { name, bytes } of images) {
|
|
149
|
+
const destination = path.join(cwd, name);
|
|
150
|
+
try { await fs.writeFile(destination, bytes, { flag: "wx", mode: 0o600 }); }
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (error.code !== "EEXIST" || !(await readRegular(destination)).equals(bytes)) throw new GeminiError("No se pudo guardar la imagen sin sobrescribir.");
|
|
153
|
+
}
|
|
154
|
+
files.push(destination);
|
|
155
|
+
}
|
|
156
|
+
return files;
|
|
157
|
+
}
|
|
143
158
|
}
|
|
144
159
|
if (now() >= deadline) throw new GeminiError(`La generación no terminó; conversación ${conversationId}. Recupera sin repetir la consulta: agent-toolkit-gemini recuperar --directorio "${cwd}".`);
|
|
145
160
|
await sleep(Math.min(pollMs, Math.max(1, deadline-now())));
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import Ajv2020 from 'ajv/dist/2020.js';
|
|
4
|
+
import schema from '../schemas/generators.schema.json' with { type: 'json' };
|
|
5
|
+
import { SIGNAL_LABELS } from './project-signals.js';
|
|
6
|
+
|
|
7
|
+
const validate = new Ajv2020({ strict: true, allErrors: true }).compile(schema);
|
|
8
|
+
const placeholders = new Set(['dir', 'name', 'python', 'venvBin']);
|
|
9
|
+
export class GeneratorsCatalogError extends Error {
|
|
10
|
+
constructor(message) { super(message); this.name = 'GeneratorsCatalogError'; this.code = 'GENERATORS_CATALOG_INVALID'; }
|
|
11
|
+
}
|
|
12
|
+
const fail = message => { throw new GeneratorsCatalogError(message); };
|
|
13
|
+
function freeze(value) {
|
|
14
|
+
if (value && typeof value === 'object') { Object.values(value).forEach(freeze); Object.freeze(value); }
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
function markers(value) {
|
|
18
|
+
// Content may contain ordinary Python/JSON braces; only identifier markers are substituted.
|
|
19
|
+
for (const match of value.matchAll(/\{([A-Za-z_][A-Za-z0-9_]*)\}/g)) {
|
|
20
|
+
if (!placeholders.has(match[1])) fail(`Marcador desconocido: ${match[1]}.`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
function stepsFor(generator) {
|
|
24
|
+
return [...generator.steps, ...generator.install,
|
|
25
|
+
...Object.values(generator.database?.backends ?? {}).flatMap(b => b.install)];
|
|
26
|
+
}
|
|
27
|
+
export function validateGeneratorsCatalog(value) {
|
|
28
|
+
let copy;
|
|
29
|
+
try { copy = structuredClone(value); } catch { fail('El catálogo debe ser un objeto JSON válido.'); }
|
|
30
|
+
if (!validate(copy)) fail('El catálogo de generadores no cumple el esquema requerido.');
|
|
31
|
+
const ids = new Set();
|
|
32
|
+
for (const g of copy.generators) {
|
|
33
|
+
if (ids.has(g.id)) fail(`Generador duplicado: ${g.id}.`);
|
|
34
|
+
ids.add(g.id);
|
|
35
|
+
if (g.part !== 'database' && !g.signals.length) fail(`Falta una señal en ${g.id}.`);
|
|
36
|
+
for (const s of g.signals) if (!Object.hasOwn(SIGNAL_LABELS, s)) fail(`Señal desconocida: ${s}.`);
|
|
37
|
+
if ((g.part === 'database') !== Boolean(g.database)) fail(`Configuración de base de datos inválida: ${g.id}.`);
|
|
38
|
+
for (const step of stepsFor(g)) {
|
|
39
|
+
if (step.kind === 'write') {
|
|
40
|
+
if (g.id !== 'fastapi') fail('Solo fastapi admite pasos de escritura.');
|
|
41
|
+
if (step.path.includes('\\') || step.path.includes('\0') || path.posix.isAbsolute(step.path)
|
|
42
|
+
|| step.path.includes(':') || step.path.split('/').some(p => !p || p === '..' || p === '.')) fail(`Ruta de escritura inválida: ${step.path}.`);
|
|
43
|
+
markers(step.path); markers(step.content);
|
|
44
|
+
} else step.argv.forEach(markers);
|
|
45
|
+
}
|
|
46
|
+
for (const backend of Object.values(g.database?.backends ?? {})) {
|
|
47
|
+
if (backend.envExample !== undefined) markers(backend.envExample);
|
|
48
|
+
}
|
|
49
|
+
g.check.argv.forEach(markers);
|
|
50
|
+
}
|
|
51
|
+
for (const g of copy.generators) for (const id of Object.keys(g.database?.backends ?? {})) {
|
|
52
|
+
const target = copy.generators.find(e => e.id === id);
|
|
53
|
+
if (!target || !['frontend', 'backend'].includes(target.part)) fail(`Destino de base de datos desconocido: ${id}.`);
|
|
54
|
+
}
|
|
55
|
+
return freeze(copy);
|
|
56
|
+
}
|
|
57
|
+
export async function loadGeneratorsCatalog({ catalogUrl = new URL('../catalogs/generadores.json', import.meta.url) } = {}) {
|
|
58
|
+
try { return validateGeneratorsCatalog(JSON.parse(await fs.readFile(catalogUrl, 'utf8'))); }
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error instanceof GeneratorsCatalogError) throw error;
|
|
61
|
+
throw new GeneratorsCatalogError('No se pudo leer el catálogo de generadores.');
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/init.js
CHANGED
|
@@ -7,6 +7,7 @@ import { fileURLToPath } from "node:url";
|
|
|
7
7
|
const SOURCE_DIRECTORY = path.dirname(fileURLToPath(import.meta.url));
|
|
8
8
|
const TEMPLATE_ROOT = path.resolve(SOURCE_DIRECTORY, "../templates/project-memory");
|
|
9
9
|
const PROJECT_ID_PATTERN = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
10
|
+
const KNOWN_AGENTS = ["codex", "claude", "antigravity"];
|
|
10
11
|
const BRANCH_PATTERN = /^(?!.*(?:\.\.|\/\/|@\{|\\))[A-Za-z0-9][A-Za-z0-9._/-]*[A-Za-z0-9]$|^[A-Za-z0-9]$/;
|
|
11
12
|
const SYNC_MODES = new Set(["none", "git-manual", "git-automated"]);
|
|
12
13
|
|
|
@@ -73,6 +74,7 @@ function normalizeOptions(options, context) {
|
|
|
73
74
|
memoryBranch: "main",
|
|
74
75
|
mcpName: options.mcpName?.trim() || `${options.projectId?.trim()}-memory`,
|
|
75
76
|
syncMode,
|
|
77
|
+
agents: options.agents ?? ["codex", "claude"],
|
|
76
78
|
date: formatDate(now),
|
|
77
79
|
templateRoot: path.resolve(context.templateRoot ?? TEMPLATE_ROOT)
|
|
78
80
|
};
|
|
@@ -89,6 +91,12 @@ function validateConfig(config) {
|
|
|
89
91
|
errors.push("--project-id debe usar minúsculas, números y guiones.");
|
|
90
92
|
}
|
|
91
93
|
|
|
94
|
+
if (!Array.isArray(config.agents) || config.agents.length === 0 ||
|
|
95
|
+
config.agents.some((agent) => !KNOWN_AGENTS.includes(agent)) ||
|
|
96
|
+
new Set(config.agents).size !== config.agents.length) {
|
|
97
|
+
errors.push("Los agentes deben ser codex, claude o antigravity, sin repetir.");
|
|
98
|
+
}
|
|
99
|
+
|
|
92
100
|
if (!config.projectName || config.projectName.length > 100) {
|
|
93
101
|
errors.push("--project-name es obligatorio y admite máximo 100 caracteres.");
|
|
94
102
|
}
|
|
@@ -261,7 +269,7 @@ function createManifest(config) {
|
|
|
261
269
|
},
|
|
262
270
|
source,
|
|
263
271
|
memory,
|
|
264
|
-
agents:
|
|
272
|
+
agents: config.agents.map((id) => ({
|
|
265
273
|
id,
|
|
266
274
|
enabled: true,
|
|
267
275
|
skillName: config.projectId
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
|
|
6
|
+
const LOCK_FILE = 'bloqueo';
|
|
7
|
+
const MAX_RECOVERIES = 10;
|
|
8
|
+
|
|
9
|
+
export class LockError extends Error {
|
|
10
|
+
constructor(code, message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'LockError';
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function defaultIsAlive(pid) {
|
|
18
|
+
try { process.kill(pid, 0); return true; } catch (error) { return error.code === 'EPERM'; }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
|
22
|
+
const ignoreMissing = error => { if (error.code !== 'ENOENT') throw error; };
|
|
23
|
+
|
|
24
|
+
// The state is `missing`, `unreadable` (not valid JSON, wrong shape or not a regular file) or `ok`.
|
|
25
|
+
async function readOwner(file) {
|
|
26
|
+
let text;
|
|
27
|
+
try { text = await fs.readFile(file, 'utf8'); } catch (error) {
|
|
28
|
+
return error.code === 'ENOENT' ? { state: 'missing' } : { state: 'unreadable' };
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
const data = JSON.parse(text);
|
|
32
|
+
if (Number.isInteger(data?.pid) && data.pid > 0 && typeof data.equipo === 'string' && typeof data.marca === 'string') {
|
|
33
|
+
return { state: 'ok', owner: data };
|
|
34
|
+
}
|
|
35
|
+
} catch { /* falls through */ }
|
|
36
|
+
return { state: 'unreadable' };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Writes the record to a private file and hard-links it into place: the lock never exists half written.
|
|
40
|
+
async function createExclusive(file, record) {
|
|
41
|
+
const temporary = `${file}.nuevo-${crypto.randomUUID()}`;
|
|
42
|
+
await fs.writeFile(temporary, JSON.stringify(record), { mode: 0o600, flag: 'wx' });
|
|
43
|
+
try {
|
|
44
|
+
await fs.link(temporary, file);
|
|
45
|
+
return true;
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if (error.code === 'EEXIST') return false;
|
|
48
|
+
throw error;
|
|
49
|
+
} finally {
|
|
50
|
+
await fs.unlink(temporary).catch(() => {});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Called only for an owner judged dead. The removal happens under a second guard, so two recoverers can never
|
|
55
|
+
// act at once. Under the guard only the dead lock can be at `file`: acquirers create it only when it is absent,
|
|
56
|
+
// and its dead owner will not release it. The guard itself is never removed by anyone but its holder: a guard
|
|
57
|
+
// left by a process that died in the middle of a recovery (a few milliseconds) must be removed by hand, because
|
|
58
|
+
// removing it by reading and deleting would reopen the same race. Returns 'recuperado' or 'ocupado'.
|
|
59
|
+
async function recover(file, judgedMark, { record }) {
|
|
60
|
+
const guard = `${file}.recuperando`;
|
|
61
|
+
if (!(await createExclusive(guard, record))) return 'ocupado';
|
|
62
|
+
try {
|
|
63
|
+
const again = await readOwner(file);
|
|
64
|
+
if (again.state === 'ok' && again.owner.marca === judgedMark) await fs.unlink(file).catch(ignoreMissing);
|
|
65
|
+
return 'recuperado';
|
|
66
|
+
} finally {
|
|
67
|
+
const current = await readOwner(guard);
|
|
68
|
+
if (current.state === 'ok' && current.owner.marca === record.marca) await fs.unlink(guard).catch(ignoreMissing);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const busy = (file, unreadable) => new LockError('ocupado', unreadable
|
|
73
|
+
? `No se puede leer el bloqueo de nexakit. Si no hay ninguna instalación en curso, bórralo a mano: ${file}`
|
|
74
|
+
: 'Otra instalación de nexakit está en curso; vuelve a intentarlo cuando termine.');
|
|
75
|
+
|
|
76
|
+
export async function acquireLock(nexakitDir, {
|
|
77
|
+
waitMs = 30_000, pollMs = 250, hostname = os.hostname(), pid = process.pid, isAlive = defaultIsAlive
|
|
78
|
+
} = {}) {
|
|
79
|
+
await fs.mkdir(nexakitDir, { recursive: true, mode: 0o700 });
|
|
80
|
+
const file = path.join(nexakitDir, LOCK_FILE);
|
|
81
|
+
const token = crypto.randomUUID();
|
|
82
|
+
const record = { pid, equipo: hostname, hora: new Date().toISOString(), marca: token };
|
|
83
|
+
const deadline = Date.now() + waitMs;
|
|
84
|
+
let recoveries = 0;
|
|
85
|
+
|
|
86
|
+
while (true) {
|
|
87
|
+
if (await createExclusive(file, record)) return heldLock(file, token);
|
|
88
|
+
|
|
89
|
+
const current = await readOwner(file);
|
|
90
|
+
if (current.state === 'missing') continue;
|
|
91
|
+
if (current.state === 'ok') {
|
|
92
|
+
const { owner } = current;
|
|
93
|
+
if (owner.equipo !== hostname) {
|
|
94
|
+
throw new LockError('otro-equipo', `El bloqueo de nexakit lo creó otro equipo (${owner.equipo}). Si no hay ninguna instalación en curso, bórralo a mano: ${file}`);
|
|
95
|
+
}
|
|
96
|
+
// Age never decides: a live owner keeps the lock however long it takes.
|
|
97
|
+
if (!isAlive(owner.pid) && await recover(file, owner.marca, { record }) === 'recuperado') {
|
|
98
|
+
if (++recoveries > MAX_RECOVERIES) throw busy(file, false);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
if (Date.now() >= deadline) {
|
|
103
|
+
const guard = await readOwner(`${file}.recuperando`);
|
|
104
|
+
if (guard.state === 'ok' && guard.owner.equipo === hostname && !isAlive(guard.owner.pid)) {
|
|
105
|
+
throw new LockError('ocupado', `Se interrumpió una recuperación anterior del bloqueo de nexakit. Si no hay ninguna instalación en curso, borra a mano: ${file}.recuperando`);
|
|
106
|
+
}
|
|
107
|
+
throw busy(file, current.state === 'unreadable');
|
|
108
|
+
}
|
|
109
|
+
await sleep(Math.max(1, Math.min(pollMs, deadline - Date.now())));
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function heldLock(file, token) {
|
|
114
|
+
return {
|
|
115
|
+
token,
|
|
116
|
+
async assertHeld() {
|
|
117
|
+
const current = await readOwner(file);
|
|
118
|
+
if (!(current.state === 'ok' && current.owner.marca === token)) {
|
|
119
|
+
throw new LockError('perdido', 'Se perdió el bloqueo de nexakit; no se escribió nada más.');
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
async release() {
|
|
123
|
+
const current = await readOwner(file);
|
|
124
|
+
if (current.state === 'ok' && current.owner.marca === token) await fs.unlink(file).catch(ignoreMissing);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
|
|
5
|
+
const REGISTRY_FILE = 'instalaciones.json';
|
|
6
|
+
const MAX_REGISTRY_BYTES = 4 * 1024 * 1024;
|
|
7
|
+
const FIELDS = ['destino', 'apunta', 'raiz', 'fecha'];
|
|
8
|
+
|
|
9
|
+
const damaged = file => new Error(`El registro de instalaciones de nexakit está dañado: ${file}`);
|
|
10
|
+
const isText = value => typeof value === 'string' && value.length > 0;
|
|
11
|
+
const empty = () => ({ version: 1, entradas: [] });
|
|
12
|
+
|
|
13
|
+
// The registry is the only proof that a broken link belongs to nexakit. `apunta` must be what `readlink`
|
|
14
|
+
// returns for the link, since that is the value compared later.
|
|
15
|
+
export async function readRegistry(nexakitDir) {
|
|
16
|
+
const file = path.join(nexakitDir, REGISTRY_FILE);
|
|
17
|
+
let info;
|
|
18
|
+
try { info = await fs.lstat(file); } catch (error) {
|
|
19
|
+
if (error.code === 'ENOENT') return empty();
|
|
20
|
+
throw error;
|
|
21
|
+
}
|
|
22
|
+
if (!info.isFile() || info.size > MAX_REGISTRY_BYTES) throw damaged(file);
|
|
23
|
+
let data;
|
|
24
|
+
try { data = JSON.parse(await fs.readFile(file, 'utf8')); } catch { throw damaged(file); }
|
|
25
|
+
const valid = data?.version === 1 && Array.isArray(data.entradas) &&
|
|
26
|
+
data.entradas.every(entry => entry && typeof entry === 'object' && FIELDS.every(field => isText(entry[field])));
|
|
27
|
+
if (!valid) throw damaged(file);
|
|
28
|
+
return {
|
|
29
|
+
version: 1,
|
|
30
|
+
entradas: data.entradas.map(({ destino, apunta, raiz, fecha }) => ({ destino, apunta, raiz, fecha }))
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const isRegistered = (registry, target, literal) =>
|
|
35
|
+
registry.entradas.some(entry => entry.destino === target && entry.apunta === literal);
|
|
36
|
+
|
|
37
|
+
// Must run under the install lock: the lock is checked on entry and again right before the file is replaced.
|
|
38
|
+
export async function recordEntries(nexakitDir, entries, lock) {
|
|
39
|
+
if (!Array.isArray(entries) || !entries.every(entry => isText(entry?.destino) && isText(entry?.apunta) && isText(entry?.raiz))) {
|
|
40
|
+
throw new Error('Entrada del registro de instalaciones no válida.');
|
|
41
|
+
}
|
|
42
|
+
if (entries.length === 0) return;
|
|
43
|
+
await lock.assertHeld();
|
|
44
|
+
|
|
45
|
+
const registry = await readRegistry(nexakitDir);
|
|
46
|
+
const fecha = new Date().toISOString();
|
|
47
|
+
// A Map keeps the position of an entry that is replaced.
|
|
48
|
+
const byTarget = new Map(registry.entradas.map(entry => [entry.destino, entry]));
|
|
49
|
+
for (const { destino, apunta, raiz } of entries) byTarget.set(destino, { destino, apunta, raiz, fecha });
|
|
50
|
+
|
|
51
|
+
const file = path.join(nexakitDir, REGISTRY_FILE);
|
|
52
|
+
const temporary = `${file}.${crypto.randomUUID()}.tmp`;
|
|
53
|
+
try {
|
|
54
|
+
await fs.writeFile(temporary, `${JSON.stringify({ version: 1, entradas: [...byTarget.values()] }, null, 2)}\n`, { mode: 0o600, flag: 'wx' });
|
|
55
|
+
await lock.assertHeld();
|
|
56
|
+
await fs.rename(temporary, file);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
await fs.unlink(temporary).catch(() => {});
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { acquireLock } from './install-lock.js';
|
|
5
|
+
import { recordEntries } from './install-registry.js';
|
|
6
|
+
import { relinkIfUnchanged } from './link-states.js';
|
|
7
|
+
import { actualRoot, ensureStableInstall, nexakitDir } from './stable-install.js';
|
|
8
|
+
import { TOOLKIT_ROOT, packageVersion, registrationCommand, resolveNodeExecutable, setupToolkit } from './toolkit-setup.js';
|
|
9
|
+
|
|
10
|
+
const samePath = (a, b) => (process.platform === 'win32' ? a.toLowerCase() === b.toLowerCase() : a === b);
|
|
11
|
+
const exists = file => fs.access(file).then(() => true, () => false);
|
|
12
|
+
|
|
13
|
+
// Finds the global links that point into a root that no longer exists, `from`, and that nexakit cannot prove
|
|
14
|
+
// are its own (no entry in the install registry). Only skill names this version ships are considered.
|
|
15
|
+
// Nothing is written here: the person has to confirm what this lists.
|
|
16
|
+
export async function planLegacyRepair({
|
|
17
|
+
from, agent = 'all', homeDirectory = os.homedir(), toolkitRoot = TOOLKIT_ROOT, environment = process.env, codexHome
|
|
18
|
+
} = {}) {
|
|
19
|
+
const dead = path.resolve(from);
|
|
20
|
+
const setup = await setupToolkit({ agent, homeDirectory, toolkitRoot, environment, ...(codexHome && { codexHome }) });
|
|
21
|
+
const links = setup.skills
|
|
22
|
+
.filter(skill => skill.state === 'roto-ajeno' && skill.root && samePath(skill.root, dead))
|
|
23
|
+
.map(({ client, name, target, literal }) => ({ client, name, target, literal }))
|
|
24
|
+
.sort((a, b) => a.client.localeCompare(b.client) || a.name.localeCompare(b.name));
|
|
25
|
+
const mcp = {};
|
|
26
|
+
for (const [client, state] of Object.entries(setup.mcp.states)) {
|
|
27
|
+
if (state.state === 'roto-ajeno' && state.root && samePath(state.root, dead)) mcp[client] = { name: state.name, literal: state.literal };
|
|
28
|
+
}
|
|
29
|
+
return { from: dead, agent, activeRoot: setup.activeRoot, ephemeral: setup.ephemeral && !setup.otherCopy, links, mcp };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Repoints the links of the plan to the copy of nexakit in use and records them as nexakit's own.
|
|
33
|
+
// Each link is read again right before writing: one that changed since the plan is left alone.
|
|
34
|
+
export async function applyLegacyRepair(plan, {
|
|
35
|
+
homeDirectory = os.homedir(), toolkitRoot = TOOLKIT_ROOT, environment = process.env, runNpm, packageSpec, lockOptions,
|
|
36
|
+
acquire = acquireLock
|
|
37
|
+
} = {}) {
|
|
38
|
+
const home = await fs.realpath(homeDirectory);
|
|
39
|
+
const lock = await acquire(nexakitDir(home), lockOptions);
|
|
40
|
+
try {
|
|
41
|
+
if (plan.ephemeral && plan.links.length) {
|
|
42
|
+
await ensureStableInstall({
|
|
43
|
+
homeDirectory: home, version: await packageVersion(await fs.realpath(toolkitRoot)), lock,
|
|
44
|
+
...(runNpm && { runNpm }), ...(packageSpec && { packageSpec })
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const entries = [], skipped = [];
|
|
48
|
+
let changed = 0;
|
|
49
|
+
for (const link of plan.links) {
|
|
50
|
+
const source = path.join(plan.activeRoot, 'skills', link.name);
|
|
51
|
+
if (!(await exists(path.join(source, 'SKILL.md')))) { skipped.push(link.name); continue; }
|
|
52
|
+
await lock.assertHeld();
|
|
53
|
+
if (await relinkIfUnchanged({ target: link.target, expectedLiteral: link.literal, source }) === 'cambio') { changed++; continue; }
|
|
54
|
+
entries.push({ destino: link.target, apunta: await fs.readlink(link.target), raiz: plan.activeRoot });
|
|
55
|
+
}
|
|
56
|
+
await recordEntries(nexakitDir(home), entries, lock);
|
|
57
|
+
|
|
58
|
+
// The MCP entry is not rewritten here: its client owns that file, so only the exact commands are given.
|
|
59
|
+
const mcpCommands = [];
|
|
60
|
+
const script = path.join(plan.activeRoot, 'bin/agent-toolkit-mcp.js');
|
|
61
|
+
if (Object.keys(plan.mcp ?? {}).length && await exists(script)) {
|
|
62
|
+
const nodePath = await resolveNodeExecutable(environment);
|
|
63
|
+
for (const [client, current] of Object.entries(plan.mcp)) {
|
|
64
|
+
mcpCommands.push(registrationCommand(client, { state: 'roto-ajeno', name: current.name }, nodePath, script));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return { repaired: entries.length, changed, skipped, mcpCommands, stableRoot: actualRoot(home) };
|
|
68
|
+
} finally {
|
|
69
|
+
await lock.release();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
import fs from 'node:fs/promises';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { IS_WINDOWS } from './platform.js';
|
|
5
|
+
|
|
6
|
+
// Evaluated in this order; the first one that holds wins.
|
|
7
|
+
export const LINK_STATES = Object.freeze(['falta', 'conflicto', 'efimero', 'instalado', 'otra-copia', 'roto-propio', 'roto-ajeno', 'quitada']);
|
|
8
|
+
// What an apply acts on, and what counts as ready.
|
|
9
|
+
export const ACTIONABLE_LINK_STATES = Object.freeze(['falta', 'efimero', 'roto-propio']);
|
|
10
|
+
export const READY_LINK_STATES = Object.freeze(['instalado', 'otra-copia']);
|
|
11
|
+
// `quitada`: nexakit instaló el enlace y el usuario lo quitó. No es pendiente: se respeta hasta que pida `--reponer`.
|
|
12
|
+
export const REMOVED_BY_USER_STATE = 'quitada';
|
|
13
|
+
export const SETTLED_LINK_STATES = Object.freeze([...READY_LINK_STATES, REMOVED_BY_USER_STATE]);
|
|
14
|
+
|
|
15
|
+
const PACKAGE_NAMES = new Set(['nexakit', 'agent-toolkit']);
|
|
16
|
+
const MAX_PACKAGE_BYTES = 1_048_576;
|
|
17
|
+
const BROKEN_CODES = new Set(['ENOENT', 'ENOTDIR', 'ELOOP']);
|
|
18
|
+
|
|
19
|
+
const normalized = value => (IS_WINDOWS ? value.toLowerCase() : value);
|
|
20
|
+
const samePath = (a, b) => normalized(a) === normalized(b);
|
|
21
|
+
const lstatOrNull = file => fs.lstat(file).catch(error => {
|
|
22
|
+
if (error.code === 'ENOENT') return null;
|
|
23
|
+
throw error;
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const isEphemeralPath = value => path.resolve(value).split(path.sep).includes('_npx');
|
|
27
|
+
|
|
28
|
+
// A copy of nexakit inside an npx cache: `…/_npx/<hash>/node_modules/nexakit` (or the old name, agent-toolkit).
|
|
29
|
+
// Anything else under `_npx` belongs to another package and is never treated as nexakit's.
|
|
30
|
+
export function isEphemeralNexakitRoot(root) {
|
|
31
|
+
const parts = path.resolve(root).split(path.sep);
|
|
32
|
+
const n = parts.length;
|
|
33
|
+
return n >= 4 && parts[n - 4] === '_npx' && parts[n - 2] === 'node_modules' && PACKAGE_NAMES.has(parts[n - 1]);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function readLink(target) {
|
|
37
|
+
const info = await lstatOrNull(target);
|
|
38
|
+
if (!info) return { exists: false, isLink: false };
|
|
39
|
+
if (!info.isSymbolicLink()) return { exists: true, isLink: false };
|
|
40
|
+
const literal = await fs.readlink(target);
|
|
41
|
+
return { exists: true, isLink: true, literal, absolute: path.resolve(path.dirname(target), literal) };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Strips `suffix` from the end of `value`; null when it does not end with it.
|
|
45
|
+
function rootFrom(value, suffix) {
|
|
46
|
+
const tail = path.normalize(suffix);
|
|
47
|
+
const resolved = path.resolve(value);
|
|
48
|
+
if (!normalized(resolved).endsWith(normalized(path.sep + tail))) return null;
|
|
49
|
+
return resolved.slice(0, resolved.length - tail.length - 1) || null;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function recognizedRoot(resolvedPath, suffix) {
|
|
53
|
+
const root = rootFrom(resolvedPath, suffix);
|
|
54
|
+
if (!root) return null;
|
|
55
|
+
const file = path.join(root, 'package.json');
|
|
56
|
+
const info = await lstatOrNull(file);
|
|
57
|
+
if (!info?.isFile() || info.isSymbolicLink() || info.size > MAX_PACKAGE_BYTES) return null;
|
|
58
|
+
try {
|
|
59
|
+
const pkg = JSON.parse(await fs.readFile(file, 'utf8'));
|
|
60
|
+
return PACKAGE_NAMES.has(pkg?.name) ? root : null;
|
|
61
|
+
} catch { return null; }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function realpathOrNull(file) {
|
|
65
|
+
try { return await fs.realpath(file); } catch (error) {
|
|
66
|
+
if (BROKEN_CODES.has(error.code)) return null;
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export async function classifyLink({ target, suffix, activeRoots = [], isOwned = () => false }) {
|
|
72
|
+
const link = await readLink(target);
|
|
73
|
+
if (!link.exists) return { target, suffix, state: 'falta' };
|
|
74
|
+
if (!link.isLink) return { target, suffix, state: 'conflicto' };
|
|
75
|
+
return classifyReference({ target, literal: link.literal, absolute: link.absolute, suffix, activeRoots, isOwned });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Classifies a path that points into a copy of nexakit without being a link itself, such as the script
|
|
79
|
+
// an MCP entry runs. `target` identifies the reference for `isOwned`; `absolute` defaults to the literal.
|
|
80
|
+
export async function classifyReference({ target, literal, absolute = path.resolve(literal), suffix, activeRoots = [], isOwned = () => false }) {
|
|
81
|
+
const base = { target, suffix };
|
|
82
|
+
const resolved = await realpathOrNull(absolute);
|
|
83
|
+
|
|
84
|
+
// Before `instalado`, so a link into the cache this run came from is never taken as good. It must point at
|
|
85
|
+
// exactly `<copy of nexakit in an npx cache>/<suffix>`; another package there is not nexakit's to move.
|
|
86
|
+
const ephemeralRoot = [absolute, resolved].filter(Boolean)
|
|
87
|
+
.map(candidate => rootFrom(candidate, suffix)).find(root => root && isEphemeralNexakitRoot(root));
|
|
88
|
+
if (ephemeralRoot) return { ...base, state: 'efimero', literal, root: ephemeralRoot };
|
|
89
|
+
|
|
90
|
+
if (resolved) {
|
|
91
|
+
const tail = path.normalize(suffix);
|
|
92
|
+
for (const candidate of activeRoots) {
|
|
93
|
+
if (isEphemeralPath(candidate)) continue;
|
|
94
|
+
const real = await realpathOrNull(candidate);
|
|
95
|
+
if (real && samePath(resolved, path.join(real, tail))) return { ...base, state: 'instalado', literal, root: real };
|
|
96
|
+
}
|
|
97
|
+
const other = await recognizedRoot(resolved, suffix);
|
|
98
|
+
if (other) return { ...base, state: 'otra-copia', literal, root: other };
|
|
99
|
+
return { ...base, state: 'conflicto', literal };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const owned = await isOwned(target, literal);
|
|
103
|
+
return { ...base, state: owned ? 'roto-propio' : 'roto-ajeno', literal, root: rootFrom(absolute, suffix) ?? undefined };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function chooseActiveRoot(items, fallbackRoot) {
|
|
107
|
+
const counts = new Map();
|
|
108
|
+
for (const item of items) {
|
|
109
|
+
if (item.state !== 'otra-copia' || !item.root || isEphemeralPath(item.root)) continue;
|
|
110
|
+
counts.set(item.root, (counts.get(item.root) ?? 0) + 1);
|
|
111
|
+
}
|
|
112
|
+
let best = null;
|
|
113
|
+
for (const [root, count] of counts) {
|
|
114
|
+
if (count * 2 > items.length && (!best || count > best.count)) best = { root, count };
|
|
115
|
+
}
|
|
116
|
+
return best?.root ?? fallbackRoot;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function legacyGroups(items, { skillNames, mcpDeadRoot } = {}) {
|
|
120
|
+
const groups = new Map();
|
|
121
|
+
for (const item of items) {
|
|
122
|
+
if (item.state !== 'roto-ajeno' || !item.root || !skillNames?.has(path.basename(item.target))) continue;
|
|
123
|
+
groups.set(item.root, [...(groups.get(item.root) ?? []), item.target]);
|
|
124
|
+
}
|
|
125
|
+
return [...groups]
|
|
126
|
+
.filter(([root, targets]) => targets.length >= 3 || (mcpDeadRoot !== undefined && root === mcpDeadRoot))
|
|
127
|
+
.map(([root, targets]) => ({ root, targets }));
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// `expectedLiteral` is the readlink value the plan saw; null means the path must not exist yet.
|
|
131
|
+
// Re-reads right before writing and touches nothing when the link is no longer what the plan saw.
|
|
132
|
+
// `relative` writes the link relative to its own folder (Windows junctions are always absolute).
|
|
133
|
+
export async function relinkIfUnchanged({ target, expectedLiteral, source, relative = false }) {
|
|
134
|
+
const type = IS_WINDOWS ? 'junction' : 'dir';
|
|
135
|
+
const absoluteSource = path.resolve(source);
|
|
136
|
+
const linkValue = relative && !IS_WINDOWS ? path.relative(path.dirname(target), absoluteSource) : absoluteSource;
|
|
137
|
+
const info = await lstatOrNull(target);
|
|
138
|
+
|
|
139
|
+
if (expectedLiteral === null) {
|
|
140
|
+
if (info) return 'cambio';
|
|
141
|
+
try { await fs.symlink(linkValue, target, type); } catch (error) {
|
|
142
|
+
if (error.code === 'EEXIST') return 'cambio';
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
return 'hecho';
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!info?.isSymbolicLink() || await fs.readlink(target) !== expectedLiteral) return 'cambio';
|
|
149
|
+
|
|
150
|
+
if (IS_WINDOWS) {
|
|
151
|
+
// A junction cannot be renamed over; this window is not atomic there.
|
|
152
|
+
await fs.unlink(target);
|
|
153
|
+
await fs.symlink(linkValue, target, type);
|
|
154
|
+
return 'hecho';
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Build the new link beside the old one and rename over it, so the name is never missing.
|
|
158
|
+
const temporary = `${target}.nx-${crypto.randomUUID()}`;
|
|
159
|
+
await fs.symlink(linkValue, temporary, type);
|
|
160
|
+
try {
|
|
161
|
+
if (await fs.readlink(target) !== expectedLiteral) {
|
|
162
|
+
await fs.unlink(temporary);
|
|
163
|
+
return 'cambio';
|
|
164
|
+
}
|
|
165
|
+
await fs.rename(temporary, target);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
await fs.unlink(temporary).catch(() => {});
|
|
168
|
+
if (error.code === 'ENOENT' || error.code === 'EINVAL') return 'cambio';
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
return 'hecho';
|
|
172
|
+
}
|