@jossuealcala/madre 1.1.2 → 1.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.
Files changed (101) hide show
  1. package/CHANGELOG.en.md +21 -1
  2. package/CHANGELOG.md +21 -1
  3. package/README.en.md +3 -3
  4. package/README.md +3 -3
  5. package/SECURITY.md +11 -6
  6. package/bin/madre.mjs +74 -11
  7. package/docs/REFERENCE.md +13 -7
  8. package/docs/SDK.md +8 -8
  9. package/docs/en/REFERENCE.md +10 -6
  10. package/docs/en/SDK.md +8 -8
  11. package/docs/en/training/README.md +1 -1
  12. package/docs/sdk/connector-module.mjs +1 -1
  13. package/docs/sdk/hello-module.mjs +1 -1
  14. package/docs/training/README.md +1 -1
  15. package/docs/training/train.sh +3 -0
  16. package/package.json +4 -2
  17. package/public/activity.js +9 -0
  18. package/public/app.js +988 -171
  19. package/public/attribution.js +9 -0
  20. package/public/brands.js +27 -13
  21. package/public/en.js +1 -2
  22. package/public/es.js +834 -257
  23. package/public/index.html +3 -0
  24. package/public/macroverso.js +24 -5
  25. package/public/manual-content.js +32 -2
  26. package/public/market-view.js +116 -9
  27. package/public/market.js +15 -0
  28. package/public/news-scenes.js +185 -0
  29. package/public/notices.js +66 -0
  30. package/public/power.js +1 -1
  31. package/public/resay.js +3 -0
  32. package/public/salas-mac.css +229 -26
  33. package/public/salas-mac.html +3 -0
  34. package/public/salas-mac.js +454 -37
  35. package/public/styles.css +393 -17
  36. package/public/tips.js +124 -0
  37. package/public/tour-chapters.js +75 -75
  38. package/public/tour.js +36 -8
  39. package/public/troubleshooting.js +215 -22
  40. package/public/welcome-scene.js +358 -0
  41. package/public/welcome.js +514 -273
  42. package/src/adapters/gemini-rest.mjs +236 -0
  43. package/src/adapters/gemini.mjs +9 -7
  44. package/src/adapters/grok.mjs +288 -0
  45. package/src/adapters/process.mjs +14 -3
  46. package/src/auth-probe.mjs +32 -0
  47. package/src/bridge/policy.mjs +1 -1
  48. package/src/capabilities.mjs +30 -0
  49. package/src/checkpoint.mjs +115 -1
  50. package/src/codex-route.mjs +113 -0
  51. package/src/credentials.mjs +46 -8
  52. package/src/dataset.mjs +18 -7
  53. package/src/event-store.mjs +10 -2
  54. package/src/ficha.mjs +142 -0
  55. package/src/files.mjs +36 -2
  56. package/src/lease.mjs +5 -0
  57. package/src/market-check.mjs +1 -1
  58. package/src/market-remote.mjs +135 -1
  59. package/src/mcp/image-server.mjs +1 -1
  60. package/src/mcp/memory-server.mjs +10 -0
  61. package/src/memory-tools.mjs +3 -0
  62. package/src/memory.mjs +29 -4
  63. package/src/models.mjs +21 -3
  64. package/src/modules/ahp.mjs +8 -3
  65. package/src/modules/bishop-exam.mjs +41 -12
  66. package/src/modules/bishop.mjs +2 -2
  67. package/src/modules/choices.mjs +12 -10
  68. package/src/modules/fetch-alone.mjs +7 -0
  69. package/src/modules/helpers.mjs +23 -5
  70. package/src/modules/image-studio.mjs +13 -27
  71. package/src/modules/index.mjs +54 -11
  72. package/src/modules/isolated.mjs +160 -0
  73. package/src/modules/ollama.mjs +35 -6
  74. package/src/modules/playwright.mjs +46 -17
  75. package/src/modules/run-alone.mjs +122 -0
  76. package/src/modules/runs.mjs +11 -8
  77. package/src/modules/sdk.mjs +4 -2
  78. package/src/notices-key.mjs +8 -0
  79. package/src/notices.mjs +185 -0
  80. package/src/ollama.mjs +57 -15
  81. package/src/outbound.mjs +45 -8
  82. package/src/room/archivist.mjs +30 -5
  83. package/src/room/busy.mjs +16 -0
  84. package/src/room/control.mjs +36 -6
  85. package/src/room/guard.mjs +32 -2
  86. package/src/room/prompt.mjs +32 -5
  87. package/src/room/vectors.mjs +7 -1
  88. package/src/room.mjs +94 -28
  89. package/src/router.mjs +1 -1
  90. package/src/runs.mjs +39 -0
  91. package/src/runtime-detection.mjs +46 -6
  92. package/src/salas.mjs +151 -15
  93. package/src/server.mjs +577 -80
  94. package/src/service.mjs +116 -0
  95. package/src/setup-trouble.mjs +20 -1
  96. package/src/skill-check.mjs +269 -0
  97. package/src/skills-import.mjs +128 -0
  98. package/src/skills.mjs +492 -0
  99. package/src/tour.mjs +14 -3
  100. package/src/whats-new.mjs +48 -0
  101. package/src/rumbo.mjs +0 -337
package/src/salas.mjs CHANGED
@@ -12,12 +12,13 @@
12
12
 
13
13
  import { spawn } from 'node:child_process';
14
14
  import { createHash } from 'node:crypto';
15
- import { closeSync, openSync, realpathSync } from 'node:fs';
16
- import { mkdir, readdir, readFile, rename, stat, writeFile } from 'node:fs/promises';
15
+ import { closeSync, realpathSync } from 'node:fs';
16
+ import { mkdir, open, readdir, readFile, rename, stat, writeFile } from 'node:fs/promises';
17
17
  import * as nodePath from 'node:path';
18
18
  import { basename, dirname, join, resolve } from 'node:path';
19
19
  import { fileURLToPath } from 'node:url';
20
20
  import { oneAtATime, writeWhole } from './json-file.mjs';
21
+ import { defaultStateRoot, fichaHeaders, openPrivateLog } from './ficha.mjs';
21
22
 
22
23
  const MADRE_BIN = fileURLToPath(new URL('../bin/madre.mjs', import.meta.url));
23
24
 
@@ -89,11 +90,55 @@ export function projectRootFor({ command, named = true, project, cwd, home, stat
89
90
 
90
91
  // Where the project is, kept beside its record (S-002): the room's id is a hash of the path and
91
92
  // cannot be turned back into it, and a room that is closed has to be found to be opened again.
92
- // Written whole or not at all.
93
- export async function markProject(roomDir, projectRoot, { now = new Date() } = {}) {
93
+ // Written whole or not at all. With the port it opened on (L-6): what else the record holds stays.
94
+ export async function markProject(roomDir, projectRoot, { now = new Date(), port = null, keepOpen = null } = {}) {
94
95
  await mkdir(roomDir, { recursive: true });
95
96
  const file = join(roomDir, 'project.json');
96
- await oneAtATime(file, () => writeWhole(file, `${JSON.stringify({ root: projectRoot, name: basename(projectRoot), lastOpened: now.toISOString() })}\n`));
97
+ await oneAtATime(file, async () => {
98
+ const was = await readJson(file);
99
+ await writeWhole(file, `${JSON.stringify({ ...(was ?? {}), root: projectRoot, name: basename(projectRoot), lastOpened: now.toISOString(), ...(Number.isInteger(port) ? { port } : {}), ...(typeof keepOpen === 'boolean' ? { keepOpen } : {}) })}\n`);
100
+ });
101
+ }
102
+
103
+ // Whether a project comes back when MADRE starts as a service (L-3): set when it opens, taken away
104
+ // only when the person closes it. A restart of the computer or a fall of MADRE leaves it on.
105
+ export async function keepOpenProject(stateRoot, id, keepOpen) {
106
+ if (typeof id !== 'string' || !/^[^/\\]+$/.test(id) || id === '.' || id === '..') return false;
107
+ const file = join(stateRoot, 'rooms', id, 'project.json');
108
+ return oneAtATime(file, async () => {
109
+ const was = await readJson(file);
110
+ if (!was) return false;
111
+ await writeWhole(file, `${JSON.stringify({ ...was, keepOpen: Boolean(keepOpen) })}\n`);
112
+ return true;
113
+ });
114
+ }
115
+
116
+ // Each project keeps its port (L-6, Carlos's server, 2026-11-04). A project opened from Home took
117
+ // the first free port from 4317, so its number depended on the order things were opened in and
118
+ // changed after a restart; on a server reached through a tunnel the new one fell outside the
119
+ // tunnel, and links between projects need the same number on both ends. What a project opened on
120
+ // is kept in its record; the next time it asks for that one first, and a project with none skips
121
+ // the ports other projects already keep.
122
+ export async function keptPorts(stateRoot, projectRoot) {
123
+ const mine = projectRoomId(projectRoot);
124
+ const dir = join(stateRoot, 'rooms');
125
+ const others = new Set();
126
+ let own = null;
127
+ for (const id of await readdir(dir).catch(() => [])) {
128
+ const port = (await readJson(join(dir, id, 'project.json')))?.port;
129
+ if (!Number.isInteger(port)) continue;
130
+ if (id === mine) own = port; else others.add(port);
131
+ }
132
+ return { mine: own, others };
133
+ }
134
+
135
+ // The port to try: its own when it has one and it is free, else the first free one from `base`
136
+ // that no other project keeps. `taken(port)` says whether something is listening there.
137
+ export async function choosePort({ base, mine = null, others = new Set(), taken }) {
138
+ if (Number.isInteger(mine) && !(await taken(mine))) return mine;
139
+ let port = base;
140
+ while (others.has(port) || await taken(port)) port += 1;
141
+ return port;
97
142
  }
98
143
 
99
144
  // ---- The lobby: adding projects (2026-10-10). A browser cannot hand MADRE the path of a folder,
@@ -155,12 +200,30 @@ export async function listFolders({ path = null, home, stateRoot }) {
155
200
  }
156
201
 
157
202
  // Folders that look like projects in the places people keep them, newest first, none already here.
158
- export async function suggestProjects({ home, stateRoot, limit = 24 }) {
203
+ // Since 1.1 (the welcome of 2026-11-08, docs/private/BIENVENIDA-1.1.md) also the folders the
204
+ // person already opened with another tool, each saying which: like moving to a new phone, what was
205
+ // there comes along. Only the folder paths those tools keep are read, never a conversation, a key or
206
+ // a setting (see foldersFromTools).
207
+ export async function suggestProjects({ home, stateRoot, limit = 24, tools = true, platform = process.platform }) {
159
208
  const top = realFolder(home);
160
209
  const known = await knownRoots(stateRoot);
161
210
  const state = realFolder(stateRoot);
162
211
  const seen = new Map();
163
212
  const places = new Set(); // `Projects` and `projects` are one folder on a Mac: read it once
213
+ const keep = async (full, from = null) => {
214
+ const real = realFolder(full);
215
+ if (known.has(real) || inside(real, state)) return;
216
+ const had = seen.get(real);
217
+ if (had) { if (from && !had.from.includes(from)) had.from.push(from); return; }
218
+ const info = await stat(real).catch(() => null);
219
+ if (!info?.isDirectory()) return;
220
+ seen.set(real, { name: basename(real), path: real, when: info.mtimeMs ?? 0, from: from ? [from] : [] });
221
+ };
222
+ if (tools) for (const { path, from } of await foldersFromTools({ home: top, platform })) {
223
+ const real = realFolder(path);
224
+ if (!toolFolderFits(real, top)) continue;
225
+ await keep(real, from);
226
+ }
164
227
  for (const place of PROJECT_HOMES) {
165
228
  const base = join(top, place);
166
229
  const info = await stat(base).catch(() => null);
@@ -170,12 +233,83 @@ export async function suggestProjects({ home, stateRoot, limit = 24 }) {
170
233
  for (const entry of names) {
171
234
  if (!entry.isDirectory() || entry.name.startsWith('.') || NOT_PROJECTS.has(entry.name)) continue;
172
235
  const full = join(base, entry.name);
173
- if (seen.has(full) || known.has(full) || inside(full, state) || !(await looksLikeProject(full))) continue;
174
- const when = (await stat(full).catch(() => null))?.mtimeMs ?? 0;
175
- seen.set(full, { name: entry.name, path: full, when });
236
+ if (seen.has(realFolder(full)) || !(await looksLikeProject(full))) continue;
237
+ await keep(full);
238
+ }
239
+ }
240
+ // What another tool already knows comes first, then the rest; the newest first in each.
241
+ return [...seen.values()]
242
+ .sort((a, b) => Number(b.from.length > 0) - Number(a.from.length > 0) || b.when - a.when)
243
+ .slice(0, limit)
244
+ .map(({ name, path, when, from }) => ({ name, path, when: when ? new Date(when).toISOString() : null, from }));
245
+ }
246
+
247
+ // A folder another tool knows is offered only inside the home, never the home itself, a folder
248
+ // hidden anywhere on its way (a worktree under .claude/, a tool's own state) or one of the places
249
+ // that hold everything (Documents, Desktop…): those are where projects live, not projects.
250
+ const HOLDERS = new Set(['documents', 'documentos', 'desktop', 'escritorio', 'downloads', 'descargas', ...PROJECT_HOMES.filter(Boolean).map((one) => one.toLowerCase())]);
251
+ export function toolFolderFits(folder, home, path = nodePath) {
252
+ if (!insideFolder(folder, home, path) || sameFolder(folder, home, path)) return false;
253
+ const parts = path.relative(home, folder).split(path.sep);
254
+ if (parts.some((part) => part.startsWith('.') || NOT_PROJECTS.has(part))) return false;
255
+ return !(parts.length === 1 && HOLDERS.has(parts[0].toLowerCase()));
256
+ }
257
+
258
+ // The folders other tools on this computer already worked in, by tool. Each one is read for paths
259
+ // and nothing else, and a tool that is not there, or writes its file differently, simply adds none:
260
+ // claude ~/.claude.json, the names of its `projects` (Claude Code keeps one entry per folder)
261
+ // codex ~/.codex/sessions, the `cwd` on the first line of the newest sessions
262
+ // gemini ~/.gemini/projects.json, the names of its `projects`
263
+ // cursor Cursor's storage.json, the folders of its recent windows
264
+ // vscode the same file of Visual Studio Code
265
+ // In WSL, Cursor and VS Code keep theirs on the Windows side: only Linux's own are read.
266
+ export const TOOL_NAMES = { claude: 'Claude Code', codex: 'Codex', gemini: 'Gemini', cursor: 'Cursor', vscode: 'VS Code' };
267
+ const CODEX_SESSIONS_READ = 300;
268
+ export async function foldersFromTools({ home, platform = process.platform }) {
269
+ const found = [];
270
+ const add = (path, from) => { if (typeof path === 'string' && nodePath.isAbsolute(path)) found.push({ path, from }); };
271
+ const claude = await readJson(join(home, '.claude.json'));
272
+ for (const path of Object.keys(claude?.projects ?? {})) add(path, 'claude');
273
+ const gemini = await readJson(join(home, '.gemini', 'projects.json'));
274
+ for (const path of Object.keys(gemini?.projects ?? {})) add(path, 'gemini');
275
+ for (const path of await codexFolders(join(home, '.codex', 'sessions'))) add(path, 'codex');
276
+ const appData = platform === 'darwin' ? join(home, 'Library', 'Application Support') : join(home, '.config');
277
+ for (const [from, app] of [['cursor', 'Cursor'], ['vscode', 'Code']]) {
278
+ const storage = await readJson(join(appData, app, 'User', 'globalStorage', 'storage.json'));
279
+ const uris = [...Object.keys(storage?.profileAssociations?.workspaces ?? {}), ...(storage?.backupWorkspaces?.folders ?? []).map((one) => one?.folderUri)];
280
+ for (const uri of uris) { if (typeof uri === 'string' && uri.startsWith('file://')) { try { add(fileURLToPath(uri), from); } catch { /* not a local folder */ } } }
281
+ }
282
+ return found;
283
+ }
284
+ // Codex writes one file per session under year/month/day; the newest are read, and of each only its
285
+ // first line, the one that says where the session ran.
286
+ async function codexFolders(root) {
287
+ const files = [];
288
+ const walk = async (dir, depth) => {
289
+ const entries = await readdir(dir, { withFileTypes: true }).catch(() => []);
290
+ for (const entry of entries.sort((a, b) => b.name.localeCompare(a.name))) {
291
+ if (files.length >= CODEX_SESSIONS_READ) return;
292
+ const full = join(dir, entry.name);
293
+ if (entry.isDirectory() && depth < 3) await walk(full, depth + 1);
294
+ else if (entry.isFile() && entry.name.endsWith('.jsonl')) files.push(full);
176
295
  }
296
+ };
297
+ await walk(root, 0);
298
+ const folders = new Set();
299
+ for (const file of files) {
300
+ // The first line may be longer than what is read: then the `cwd` is taken from the text itself.
301
+ const said = /"cwd":"((?:[^"\\]|\\.)*)"/.exec(await firstLine(file));
302
+ try { const cwd = said ? JSON.parse(`"${said[1]}"`) : null; if (cwd) folders.add(cwd); } catch { /* not a session */ }
177
303
  }
178
- return [...seen.values()].sort((a, b) => b.when - a.when).slice(0, limit).map(({ name, path }) => ({ name, path }));
304
+ return [...folders];
305
+ }
306
+ async function firstLine(file) {
307
+ const handle = await open(file, 'r').catch(() => null);
308
+ if (!handle) return '';
309
+ try {
310
+ const { buffer, bytesRead } = await handle.read(Buffer.alloc(16384), 0, 16384, 0);
311
+ return buffer.subarray(0, bytesRead).toString('utf8').split('\n')[0];
312
+ } finally { await handle.close(); }
179
313
  }
180
314
 
181
315
  // Associates one folder with the lobby: a project inside the home, not the home itself and not
@@ -194,7 +328,7 @@ export async function associateProject({ root, home, stateRoot, now = new Date()
194
328
  const file = join(roomDir, 'project.json');
195
329
  const before = await oneAtATime(file, async () => {
196
330
  const was = await readJson(file);
197
- await writeWhole(file, `${JSON.stringify({ root: folder, name: basename(folder), lastOpened: was?.lastOpened ?? null, added: was?.added ?? now.toISOString() })}\n`);
331
+ await writeWhole(file, `${JSON.stringify({ ...(was ?? {}), root: folder, name: basename(folder), lastOpened: was?.lastOpened ?? null, added: was?.added ?? now.toISOString() })}\n`);
198
332
  return was;
199
333
  });
200
334
  return { id, name: basename(folder), root: folder, already: Boolean(before) };
@@ -374,6 +508,7 @@ export async function knownRooms(stateRoot) {
374
508
  name: typeof where?.name === 'string' ? where.name : typeof mark?.project === 'string' ? mark.project : id.replace(/-[0-9a-f]{16}$/, ''),
375
509
  root: there ? root : null,
376
510
  lastOpened: where?.lastOpened ?? mark?.at ?? null,
511
+ keepOpen: where?.keepOpen === true,
377
512
  open,
378
513
  });
379
514
  }
@@ -384,7 +519,7 @@ export async function knownRooms(stateRoot) {
384
519
  // the way up lands in start.log beside its record: if it never comes up, that file says why.
385
520
  export function startRoom({ projectRoot, roomDir, env = process.env, spawnImpl = spawn, bin = MADRE_BIN }) {
386
521
  let log = 'ignore';
387
- try { log = openSync(join(roomDir, 'start.log'), 'a'); } catch { /* no witness, still start */ }
522
+ try { log = openPrivateLog(join(roomDir, 'start.log')); } catch { /* no witness, still start */ }
388
523
  // The new room is its own: what this room set for itself on the way up is not handed on.
389
524
  const own = { ...env };
390
525
  for (const name of ['PULSE_UPDATE_RESTART', 'PULSE_OUTBOUND_LOG']) delete own[name];
@@ -450,12 +585,13 @@ export function universeOf(research, { full = false } = {}) {
450
585
  }
451
586
 
452
587
  // What a room says about itself, read from its own page's address. A room on an older MADRE
453
- // answers 404 to the routes it does not have yet, and is said to be outdated (S-005).
454
- export async function askRoom(port, path, { method = 'GET', body = null, fetchImpl = globalThis.fetch, timeoutMs = 1500 } = {}) {
588
+ // answers 404 to the routes it does not have yet, and is said to be outdated (S-005). It goes with
589
+ // the ficha of this computer (src/ficha.mjs), as the page's own requests do.
590
+ export async function askRoom(port, path, { method = 'GET', body = null, fetchImpl = globalThis.fetch, timeoutMs = 1500, stateRoot = defaultStateRoot() } = {}) {
455
591
  try {
456
592
  const response = await fetchImpl(`http://127.0.0.1:${port}${path}`, {
457
593
  method,
458
- headers: body ? { 'content-type': 'application/json' } : {},
594
+ headers: { ...(await fichaHeaders(stateRoot)), ...(body ? { 'content-type': 'application/json' } : {}) },
459
595
  body: body ? JSON.stringify(body) : undefined,
460
596
  signal: AbortSignal.timeout(timeoutMs),
461
597
  });