@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
@@ -6,6 +6,8 @@ import { access, mkdir, readFile, rm, writeFile } from 'node:fs/promises';
6
6
  import { homedir } from 'node:os';
7
7
  import { join } from 'node:path';
8
8
  import { promisify } from 'node:util';
9
+ import { geminiApiKeyPresent } from './runtime-detection.mjs';
10
+ import { parseGrokStatus } from './adapters/grok.mjs';
9
11
 
10
12
  const execFileAsync = promisify(execFile);
11
13
  const stripAnsi = (text) => String(text ?? '').replace(/\[[0-9;]*[A-Za-z]/g, '');
@@ -225,6 +227,20 @@ export const AGENT_SETUP = {
225
227
  account: 'Signs in with your Cursor account and uses its plan. A free plan answers only with Auto, and only while it has use left.',
226
228
  paid: true,
227
229
  },
230
+ // Grok Build, xAI's own CLI (2026-11-06). Without a terminal, `grok login` already gives a code to
231
+ // confirm on xAI's page (accounts.x.ai/oauth2/device), and `--device-auth` asks for exactly that:
232
+ // the room shows the code, and it works the same when MADRE runs on another computer.
233
+ grok: {
234
+ install: ['npm install -g @xai-official/grok', 'or: curl -fsSL https://x.ai/cli/install.sh | bash'],
235
+ login: ['login', '--device-auth'],
236
+ loginNote: 'Gives you a code to confirm on xAI\'s page with your Grok account.',
237
+ browser: true,
238
+ room: { deviceCode: true },
239
+ terminal: 'grok login',
240
+ vendor: 'xAI',
241
+ account: 'Signs in with your Grok account (SuperGrok or an X subscription) and uses its plan.',
242
+ paid: true,
243
+ },
228
244
  };
229
245
 
230
246
  // Installing a CLI from the room itself. npm is the common denominator: every one of these
@@ -235,6 +251,8 @@ export const AGENT_PACKAGE = {
235
251
  claude: '@anthropic-ai/claude-code',
236
252
  gemini: '@google/gemini-cli',
237
253
  opencode: 'opencode-ai',
254
+ // Its install script fetches nothing: the binary comes as an npm dependency for each platform.
255
+ grok: '@xai-official/grok',
238
256
  };
239
257
  // One honest line per agent about the account it needs, for the bridge and for Settings › Agents.
240
258
  // Which agents take a pasted key instead of a browser flow.
@@ -290,6 +308,8 @@ export function installPlanFor(agent, { prefix = null } = {}) {
290
308
  // the providers in its `methods`, and its keys are pasted. Every plan also carries `terminal`:
291
309
  // the same sign-in typed by hand, for whoever prefers it.
292
310
  export function loginPlanFor(agent, { method = null } = {}) {
311
+ // Gemini answering through its API has no CLI to sign in with: its key is its sign-in.
312
+ if (agent?.viaApi) return null;
293
313
  const setup = AGENT_SETUP[agent.id];
294
314
  if (!setup) return null;
295
315
  const headless = Boolean(setup.browser) && setup.login.length > 0;
@@ -366,7 +386,14 @@ export function geminiSignIn({ env = process.env, verifyMs = 30000 } = {}) {
366
386
 
367
387
  export async function probeAgentAuth(agent) {
368
388
  if (agent.id === 'madre') return agent.ready ? { state: 'signed-in', detail: `local · ${agent.version ?? 'Ollama'}` } : { state: 'signed-out', detail: 'Ollama is not running or has no chat model' };
389
+ // Gemini without its CLI answers through Google's API with a Gemini key (adapters/gemini-rest.mjs):
390
+ // the key is its whole sign-in, checked against Google when it was pasted (credentials.mjs). The
391
+ // CLI's own files (a Google sign-in left behind, a Vertex setting) say nothing without the CLI.
392
+ if (agent.id === 'gemini' && !agent.path) return await geminiApiKeyPresent() ? { state: 'signed-in', detail: 'via API key · Google AI Studio API' } : { state: 'not-installed', detail: 'not found on this computer' };
393
+ if (agent.id === 'gemini') return probeGemini();
369
394
  if (!agent.detected || !agent.path) return { state: 'not-installed', detail: 'not found on this computer' };
395
+ // Another program with the agent's name (runtime-detection.mjs, `foreign`): nothing is asked of it.
396
+ if (agent.foreign) return { state: 'not-installed', detail: `another program is installed as ${agent.id}` };
370
397
  const answered = (result, read) => (result.timedOut ? UNANSWERED : read(result));
371
398
  switch (agent.id) {
372
399
  case 'codex': return answered(await run(agent.path, ['login', 'status']), parseCodexLoginStatus);
@@ -384,6 +411,11 @@ export async function probeAgentAuth(agent) {
384
411
  return openCodeOwnModels(own) ?? listed;
385
412
  }
386
413
  case 'cursor': return answered(await run(agent.path, ['status']), parseCursorStatus);
414
+ // Grok has no status command: `grok models` says whether there is a session (and exits 0 both ways).
415
+ case 'grok': {
416
+ const saved = await exists(join(process.env.GROK_HOME || join(homedir(), '.grok'), 'auth.json'));
417
+ return answered(await run(agent.path, ['models']), ({ stdout = '', stderr = '' }) => parseGrokStatus(stripAnsi(`${stdout}\n${stderr}`), process.env, { saved }));
418
+ }
387
419
  case 'gemini': return probeGemini();
388
420
  default: return { state: 'unknown', detail: 'no probe for this agent' };
389
421
  }
@@ -17,7 +17,7 @@ import { t } from '../i18n.mjs';
17
17
 
18
18
  // Every file the room's page loads: one missing here and app.js cannot import it, so the whole page
19
19
  // stops on the phone (test/phone-bridge.test.mjs reads app.js's imports to keep this whole).
20
- const STATIC = new Set(['/', '/app.js', '/icons.js', '/page-errors.js', '/activity.js', '/terms.js', '/nostromo-engine.js', '/brands.js', '/troubleshooting.js', '/inquiry.js', '/i18n.js', '/es.js', '/en.js', '/resay.js', '/syntax.js', '/editor.js', '/routing.js', '/abduction.js', '/styles.css', '/review-ask.js', '/loader.js', '/tour.js', '/tour-chapters.js', '/touch.js', '/attribution.js', '/power.js', '/wallpaper.js', '/neural.js', '/hardware.js', '/code-tools.js', '/market.js', '/market-view.js', '/market-art.js', '/mcp-catalog.js']);
20
+ const STATIC = new Set(['/', '/app.js', '/icons.js', '/page-errors.js', '/activity.js', '/notices.js', '/tips.js', '/news-scenes.js', '/terms.js', '/nostromo-engine.js', '/brands.js', '/troubleshooting.js', '/inquiry.js', '/i18n.js', '/es.js', '/en.js', '/resay.js', '/syntax.js', '/editor.js', '/routing.js', '/abduction.js', '/styles.css', '/review-ask.js', '/loader.js', '/tour.js', '/tour-chapters.js', '/touch.js', '/attribution.js', '/power.js', '/wallpaper.js', '/neural.js', '/hardware.js', '/code-tools.js', '/market.js', '/market-view.js', '/market-art.js', '/mcp-catalog.js']);
21
21
 
22
22
  const READ = new Set([
23
23
  '/api/state', '/api/events', '/api/chats', '/api/version', '/api/whats-new', '/api/glance', '/api/handoff',
@@ -44,6 +44,18 @@ export const CAPABILITIES = {
44
44
  imageGen: false,
45
45
  web: false,
46
46
  },
47
+ // Grok (adapters/grok.mjs, 2026-11-06): reads, and from the same day writes at #2 and #3 and runs
48
+ // commands at #4, each mode tried against the real CLI first. Attachments are named in the briefing.
49
+ grok: {
50
+ read: true,
51
+ imageIn: false,
52
+ write: { how: '--tools …,search_replace,write under --permission-mode dontAsk (#4: the terminal too, --always-approve)', note: 'Writes where the turn allows; commands only at #4. Every place nobody writes is denied one rule at a time, and MADRE locks and puts back .env, .git and its own folders, and at #2 what existed.' },
53
+ // Its own image tool (2026-11-08): the images land in Grok's folder; MADRE copies them into the turn's
54
+ // draft folder at #2 and into imagenes/ at #3 and #4 (src/lease.mjs, grokImagesDir).
55
+ imageGen: { how: 'image_gen, copied by MADRE into the project', note: 'Generated with the Grok account; MADRE copies each image into the draft folder at #2 and into imagenes/ at #3 and #4.' },
56
+ // Its own search and page reader (2026-11-07): off until Web access is turned on for it.
57
+ web: { how: '--tools …,web_search,web_fetch', note: 'Off until turned on in Settings › Agents › Web access.' },
58
+ },
47
59
  opencode: {
48
60
  read: true,
49
61
  imageIn: { how: '-f <file>', note: 'Files attach to the message.' },
@@ -53,6 +65,23 @@ export const CAPABILITIES = {
53
65
  },
54
66
  };
55
67
 
68
+ // An agent MADRE reaches through its provider's API instead of its CLI (Gemini with only a key,
69
+ // src/adapters/gemini-rest.mjs): a request with the briefing and an answer back. It does not read
70
+ // the project, see attachments, write, draw or search, so it answers at #1 at most.
71
+ export const API_ONLY = {
72
+ read: false,
73
+ imageIn: false,
74
+ write: false,
75
+ imageGen: false,
76
+ web: false,
77
+ note: 'Answers through the provider\'s API: it does not read the project or attachments, and writes nothing. Install the CLI for the rest.',
78
+ };
79
+ const viaApi = new Set();
80
+ export function setAgentsViaApi(agents = []) {
81
+ viaApi.clear();
82
+ for (const agent of agents) if (agent?.viaApi) viaApi.add(agent.id);
83
+ }
84
+
56
85
  // MADRE modules can add abilities a CLI lacks. Image Studio (MADRE's own MCP
57
86
  // image server on the Gemini API) gives Gemini CLI, Claude Code and OpenCode
58
87
  // image generation; Codex keeps its native one.
@@ -63,6 +92,7 @@ export function setImageModule(state) {
63
92
  export const imageModuleState = () => ({ ...imageModule });
64
93
 
65
94
  export const capabilityOf = (agentId) => {
95
+ if (viaApi.has(agentId)) return API_ONLY;
66
96
  const base = CAPABILITIES[agentId] ?? { read: true, imageIn: false, write: false, imageGen: false, web: false };
67
97
  if (!base.imageGen && imageModule.enabled && ['gemini', 'claude', 'opencode'].includes(agentId)) {
68
98
  return { ...base, imageGen: { how: 'MADRE Image Studio (MCP tool generate_image, Gemini API)', note: `Attached only inside a creation lease with the image scope on${imageModule.model ? ` · model ${imageModule.model}` : ''}.`, module: 'image-studio' } };
@@ -240,6 +240,44 @@ async function checkpointOne(root, { id, label, exclude = [] }) {
240
240
  return { commit, tree, head, via };
241
241
  }
242
242
 
243
+ // Ignored folders skipped when walking user data (L-5, L-5b).
244
+ const HEAVY = new Set(['node_modules', '.git', '.venv', 'venv', '__pycache__', 'dist', 'build', '.next', '.nuxt', 'target', '.cache', '.turbo', 'coverage', '.gradle', 'Pods', 'vendor']);
245
+
246
+ // What exists in light ignored folders (Carlos's `pacientes/`) before a turn begins (L-5b), each file
247
+ // with what identifies it (inode, size, modification time). `complete` is false when the walk stopped
248
+ // at its bounds: then a file missing from the list may still have been there, and nothing may be taken
249
+ // for new (review of 2026-11-06: past 500 files a patient record the agent edited was deleted by UNDO).
250
+ // Nested repositories are left out: their own restore point holds them.
251
+ export async function listIgnored(root, { via = undefined, exclude = [], limit = 5000, budget = 20000 } = {}) {
252
+ const listed = await git(root, ['ls-files', '--others', '--ignored', '--exclude-standard', '--directory', '-z'], { via }).catch(() => '');
253
+ const files = new Map();
254
+ const folders = [];
255
+ const nested = exclude.map((dir) => dir.replace(/\/$/, ''));
256
+ let visits = 0;
257
+ let complete = true;
258
+ const walk = async (path) => {
259
+ if (files.size >= limit || visits >= budget) { complete = false; return; }
260
+ visits += 1;
261
+ const bare = path.replace(/\/$/, '');
262
+ if (HEAVY.has(bare.split('/').pop()) || isForbidden(bare) || nested.some((dir) => bare === dir || bare.startsWith(`${dir}/`))) return;
263
+ const info = await lstat(join(root, bare)).catch(() => null);
264
+ if (info?.isDirectory()) {
265
+ folders.push(bare);
266
+ for (const entry of (await readdir(join(root, bare)).catch(() => [])).sort()) await walk(`${bare}/${entry}`);
267
+ } else if (info?.isFile()) files.set(bare, fileMark(info));
268
+ };
269
+ for (const path of listed.split('\0').filter(Boolean)) await walk(path);
270
+ return { files, folders, complete };
271
+ }
272
+
273
+ // What tells one file from another, and a file from itself after a write.
274
+ function fileMark(info) {
275
+ return { ino: info.ino, size: info.size, mtimeMs: info.mtimeMs };
276
+ }
277
+ function sameMark(a, b) {
278
+ return Boolean(a && b) && a.ino === b.ino && a.size === b.size && a.mtimeMs === b.mtimeMs;
279
+ }
280
+
243
281
  export async function createCheckpoint(root, { id, label = 'MADRE checkpoint' } = {}) {
244
282
  const dirs = await nestedRepos(root);
245
283
  const outer = await checkpointOne(root, { id, label, exclude: dirs });
@@ -249,7 +287,9 @@ export async function createCheckpoint(root, { id, label = 'MADRE checkpoint' }
249
287
  }
250
288
  const controls = [await photographControls(root, outer.via, outer.via ? 'MADRE · ' : '.git/')];
251
289
  for (const part of nested) controls.push(await photographControls(join(root, part.dir), part.via, `${part.dir}/.git/`));
252
- return { id, ...outer, nested, exclude: dirs, controls: controls.filter(Boolean), createdAt: new Date().toISOString() };
290
+ const createdAt = new Date().toISOString();
291
+ const ignoredBefore = await listIgnored(root, { via: outer.via, exclude: dirs });
292
+ return { id, ...outer, nested, exclude: dirs, controls: controls.filter(Boolean), ignoredExisting: ignoredBefore.files, ignoredComplete: ignoredBefore.complete, createdAt };
253
293
  }
254
294
 
255
295
  // After a turn that could write, before git runs again: the repositories' own settings, hooks and
@@ -289,6 +329,65 @@ async function diffOne(root, before, prefix = '', exclude = []) {
289
329
  return { files, afterTree: after, stat: prefix && stat ? stat.split('\n').map((line) => (line.startsWith(' ') ? ` ${prefix}${line.trimStart()}` : line)).join('\n') : stat };
290
330
  }
291
331
 
332
+ // What a turn wrote where git does not look (L-5, Carlos's server, 2026-11-04: patient records
333
+ // under an ignored `pacientes/`, and the card said the agent «changed nothing»). A restore point
334
+ // never holds ignored files, so these are named as outside it. Read by modification time from git's
335
+ // own list of what it ignores, without walking what is ignored for its size (dependencies, builds,
336
+ // caches), MADRE's own folders or the .env files the guard already puts back; bounded, because it
337
+ // is a note on the card and not a record.
338
+ export async function ignoredWrittenSince(root, checkpoint, since, { limit = 500, budget = 20000 } = {}) {
339
+ const listed = await git(root, ['ls-files', '--others', '--ignored', '--exclude-standard', '--directory', '-z'], { via: checkpoint?.via }).catch(() => '');
340
+ const nested = (checkpoint?.exclude ?? []).map((dir) => dir.replace(/\/$/, ''));
341
+ const found = [];
342
+ let visits = 0;
343
+ const walk = async (path) => {
344
+ if (found.length >= limit || visits >= budget) return;
345
+ visits += 1;
346
+ const bare = path.replace(/\/$/, '');
347
+ if (HEAVY.has(bare.split('/').pop()) || isForbidden(bare) || nested.some((dir) => bare === dir || bare.startsWith(`${dir}/`))) return;
348
+ const info = await lstat(join(root, bare)).catch(() => null);
349
+ if (info?.isDirectory()) {
350
+ for (const entry of (await readdir(join(root, bare)).catch(() => [])).sort()) await walk(`${bare}/${entry}`);
351
+ } else if (info?.isFile() && info.mtimeMs >= since) found.push(bare);
352
+ };
353
+ for (const path of listed.split('\0').filter(Boolean)) await walk(path);
354
+ return found;
355
+ }
356
+
357
+ // What a turn wrote where git does not look, sorted by what UNDO may do with it (L-5b). `created`:
358
+ // not in the photograph taken before the turn, and that photograph was whole; those, with how each
359
+ // looked when the turn ended, are what UNDO may remove. `modified`: there before and changed.
360
+ // `unknown`: written, but the photograph stopped at its bounds, so it cannot be told new from old,
361
+ // and UNDO leaves it. A file that was there before and is exactly as it was (a future modification
362
+ // time from an unpacked archive) is not reported at all.
363
+ export async function sortIgnoredWrites(root, checkpoint, paths) {
364
+ const existing = checkpoint?.ignoredExisting instanceof Map ? checkpoint.ignoredExisting : new Map();
365
+ const whole = checkpoint?.ignoredComplete === true;
366
+ const since = Date.parse(checkpoint?.createdAt ?? '');
367
+ const created = [];
368
+ const modified = [];
369
+ const unknown = [];
370
+ const marks = new Map();
371
+ for (const path of paths) {
372
+ const info = await lstat(join(root, path)).catch(() => null);
373
+ if (!info?.isFile()) continue;
374
+ const before = existing.get(path);
375
+ if (before) {
376
+ if (!sameMark(before, fileMark(info))) modified.push(path);
377
+ continue;
378
+ }
379
+ // Where the system says when a file was born, one born before the turn is not new, whatever
380
+ // the list says. Where it does not (birthtime 0), the whole photograph is the only word.
381
+ const born = info.birthtimeMs;
382
+ const bornBefore = born > 0 && Number.isFinite(since) && born < since;
383
+ if (whole && !bornBefore) {
384
+ created.push(path);
385
+ marks.set(path, fileMark(info));
386
+ } else unknown.push(path);
387
+ }
388
+ return { created, modified, unknown, marks };
389
+ }
390
+
292
391
  // What changed since the checkpoint, in the project and in every nested repository, paths
293
392
  // relative to the project. Forbidden zones are judged on the path inside each repository too.
294
393
  export async function diffCheckpoint(root, checkpoint) {
@@ -353,6 +452,21 @@ export async function restoreCheckpoint(root, checkpoint, { paths = null } = {})
353
452
  restored.push(file.path);
354
453
  }
355
454
  }
455
+ // When restoring the full checkpoint on UNDO (paths === null), also remove the files the turn
456
+ // created in light ignored folders (e.g. Carlos's pacientes/) (L-5b), but only each one still as
457
+ // the turn left it: one written again since (by the person, by another turn) is theirs now and stays.
458
+ if (!paths && Array.isArray(checkpoint?.ignoredCreated)) {
459
+ const marks = checkpoint.ignoredCreatedMarks instanceof Map ? checkpoint.ignoredCreatedMarks : new Map();
460
+ for (const path of checkpoint.ignoredCreated) {
461
+ const absolute = resolve(root, path);
462
+ if (!absolute.startsWith(canonicalRoot + sep)) continue;
463
+ if (isForbidden(path)) continue;
464
+ const info = await lstat(absolute).catch(() => null);
465
+ if (!info?.isFile() || !sameMark(marks.get(path), fileMark(info))) continue;
466
+ const done = await unlink(absolute).then(() => true, () => false);
467
+ if (done) removed.push(path);
468
+ }
469
+ }
356
470
  await dropFoldersBornAfter(root, removed, Date.parse(checkpoint.createdAt));
357
471
  return { removed, restored };
358
472
  }
@@ -0,0 +1,113 @@
1
+ // SPDX-License-Identifier: BUSL-1.1
2
+ // MADRE · © 2026 Jossue Alcalá · Business Source License 1.1: LICENSE
3
+ import { readFile } from 'node:fs/promises';
4
+ import { homedir } from 'node:os';
5
+ import { join } from 'node:path';
6
+ import { agentRouteFromEnvironment } from './runtime-detection.mjs';
7
+
8
+ // Only the routing fields matter here. Other TOML values can contain credentials, so neither
9
+ // their contents nor the base URL's path, query or user information ever leave this module.
10
+ function tomlString(value) {
11
+ const raw = String(value ?? '').trim();
12
+ if (raw.startsWith('"')) {
13
+ const match = raw.match(/^"(?:\\.|[^"\\])*"/);
14
+ if (!match) return null;
15
+ try { return JSON.parse(match[0]); } catch { return null; }
16
+ }
17
+ if (raw.startsWith("'")) return raw.match(/^'([^']*)'/)?.[1] ?? null;
18
+ return null;
19
+ }
20
+
21
+ function withoutComment(line) {
22
+ let quote = null;
23
+ let escaped = false;
24
+ for (let i = 0; i < line.length; i++) {
25
+ const char = line[i];
26
+ if (escaped) { escaped = false; continue; }
27
+ if (quote === '"' && char === '\\') { escaped = true; continue; }
28
+ if (char === quote) { quote = null; continue; }
29
+ if (!quote && (char === '"' || char === "'")) { quote = char; continue; }
30
+ if (!quote && char === '#') return line.slice(0, i);
31
+ }
32
+ return line;
33
+ }
34
+
35
+ export function codexConfigDestination(toml) {
36
+ let section = '';
37
+ let provider = 'openai';
38
+ let openaiUrl = null;
39
+ let unknown = false;
40
+ const urls = new Map();
41
+ for (const original of String(toml ?? '').split(/\r?\n/)) {
42
+ const line = withoutComment(original);
43
+ if (!line.trim()) continue;
44
+ const table = line.match(/^\s*\[([^\]]+)\]\s*(?:#.*)?$/);
45
+ if (table) { section = table[1].trim(); continue; }
46
+ const assignment = line.match(/^\s*([A-Za-z_][A-Za-z0-9_.-]*)\s*=\s*(.+)$/);
47
+ const mentionsRoute = /\b(?:model_provider|model_providers|openai_base_url)\b/.test(line);
48
+ if (!assignment) { if (mentionsRoute) unknown = true; continue; }
49
+ const [, key, value] = assignment;
50
+ let handled = false;
51
+ if (!section && key === 'model_provider') {
52
+ handled = true;
53
+ const parsed = tomlString(value);
54
+ if (parsed) provider = parsed; else unknown = true;
55
+ } else if (!section && key === 'openai_base_url') {
56
+ handled = true;
57
+ openaiUrl = tomlString(value);
58
+ if (!openaiUrl) unknown = true;
59
+ } else if (!section && key === 'model_providers') {
60
+ handled = true;
61
+ // An inline table could select another base URL. Hold it until it can be inspected.
62
+ unknown = true;
63
+ } else {
64
+ const dotted = !section && key.match(/^model_providers\.([A-Za-z0-9_-]+)\.base_url$/);
65
+ const tableProvider = section.match(/^model_providers\.(?:"([^"]+)"|'([^']+)'|([A-Za-z0-9_-]+))$/);
66
+ if ((dotted || tableProvider) && (dotted || key === 'base_url')) {
67
+ handled = true;
68
+ const parsed = tomlString(value);
69
+ if (parsed) urls.set(dotted?.[1] ?? tableProvider?.[1] ?? tableProvider?.[2] ?? tableProvider?.[3], parsed);
70
+ else unknown = true;
71
+ }
72
+ }
73
+ if (!handled && mentionsRoute) unknown = true;
74
+ }
75
+ if (unknown) return { source: 'Codex config.toml', url: null, unknown: true };
76
+ if (provider !== 'openai') return { source: 'Codex config.toml', url: urls.get(provider) ?? null, unknown: !urls.has(provider) };
77
+ return openaiUrl ? { source: 'Codex config.toml', url: openaiUrl, unknown: false } : null;
78
+ }
79
+
80
+ function routeForUrl(url, source, env) {
81
+ let parsed = null;
82
+ try { parsed = new URL(url); } catch { /* An invalid override is not an official destination. */ }
83
+ const host = parsed?.hostname.toLowerCase() ?? null;
84
+ const official = parsed?.protocol === 'https:'
85
+ && (!parsed.port || parsed.port === '443')
86
+ && !parsed.username && !parsed.password
87
+ && (host === 'api.openai.com' || host?.endsWith('.api.openai.com'));
88
+ if (official) return { kind: 'official', destination: 'OpenAI', source, host, custom: false, allowed: true };
89
+ // Let the existing route rule decide whether the person explicitly trusted Codex.
90
+ const allowed = Boolean(host) && agentRouteFromEnvironment('codex', { ...env, OPENAI_BASE_URL: 'not-a-url' }).allowed;
91
+ return { kind: 'custom', destination: host ? `custom endpoint · ${host}` : 'custom endpoint', source, host, custom: true, allowed };
92
+ }
93
+
94
+ // Recheck at dispatch, not just at room startup: the user can edit config.toml while MADRE is open.
95
+ // A missing file means the CLI's default; a file we cannot inspect never counts as official.
96
+ export async function codexRoute({ env = process.env } = {}) {
97
+ const environmentRoute = agentRouteFromEnvironment('codex', env);
98
+ const rawEnvironmentUrl = String(env.OPENAI_BASE_URL ?? '').trim();
99
+ if (rawEnvironmentUrl) {
100
+ const checked = routeForUrl(rawEnvironmentUrl, 'OPENAI_BASE_URL', env);
101
+ if (checked.custom) return checked;
102
+ }
103
+ const home = env.CODEX_HOME || join(homedir(), '.codex');
104
+ let config;
105
+ try { config = await readFile(join(home, 'config.toml'), 'utf8'); }
106
+ catch (error) {
107
+ if (error?.code === 'ENOENT') return environmentRoute;
108
+ return routeForUrl(null, 'Codex config.toml', env);
109
+ }
110
+ const destination = codexConfigDestination(config);
111
+ if (!destination) return environmentRoute;
112
+ return routeForUrl(destination.url, destination.source, env);
113
+ }
@@ -123,6 +123,7 @@ export function keyPlanFor(id) {
123
123
  providers: null,
124
124
  // The way in, ahead of the Google account: Google refuses free personal accounts on this
125
125
  // CLI (IneligibleTierError, 2026-10-17), and a key from AI Studio is what works for anyone.
126
+ // Taken out in 12b400b and put back (review of 2026-11-06): nothing else stood in its place.
126
127
  first: true,
127
128
  };
128
129
  }
@@ -148,7 +149,11 @@ async function restore(path, previous) {
148
149
  }
149
150
 
150
151
  // ---- Gemini: a line in ~/.gemini/.env, plus the auth type its settings must select.
151
- async function applyGeminiKey(key, { home, probe }) {
152
+ async function applyGeminiKey(key, { home, probe, verify = geminiTakesKey }) {
153
+ // Asked of Google first (review of 2026-11-06): without the CLI nothing else checks the key, and
154
+ // any text pasted here read as «connected» until the first turn failed.
155
+ const taken = await verify(key);
156
+ if (taken.ok === false) return { ok: false, error: t('Google did not accept this Gemini key (HTTP {status}): check you copied all of it, or make a new one at aistudio.google.com/app/apikey. Nothing was changed.', { status: taken.status }) };
152
157
  const dir = join(home, '.gemini');
153
158
  const envFile = join(dir, '.env');
154
159
  const settingsFile = join(dir, 'settings.json');
@@ -177,10 +182,38 @@ async function applyGeminiKey(key, { home, probe }) {
177
182
  }
178
183
  }
179
184
 
180
- // ---- Claude: `env.ANTHROPIC_API_KEY` in its own settings, confirmed by `claude auth status`.
181
- // The CLI reads that env block before it runs, which is why a key there is a session: its own
182
- // status answers `api_key` afterwards. Everything else in the file is left as it was.
183
- async function applyClaudeKey(key, { home, probe }) {
185
+ // Whether Anthropic takes a key (MR-R92AWX, a copy on Ubuntu over SSH, 2026-11-03): `claude auth
186
+ // status` answers «logged in» for any key it finds, a made-up one too, so MADRE said Claude was
187
+ // connected and the first message failed with «Invalid API key». Listing the models spends no
188
+ // credit. `ok: null` when Anthropic said nothing about the key (offline, its own trouble): that is
189
+ // not the key's fault, and the CLI's status decides as before. A key with no credit left passes
190
+ // here; its first turn says so (MU-059).
191
+ export async function anthropicTakesKey(key, { fetchImpl = globalThis.fetch, base = process.env.PULSE_ANTHROPIC_URL ?? 'https://api.anthropic.com', timeoutMs = 8000 } = {}) {
192
+ try {
193
+ const answer = await fetchImpl(`${base}/v1/models`, { headers: { 'x-api-key': key, 'anthropic-version': '2023-06-01' }, signal: AbortSignal.timeout(timeoutMs) });
194
+ if (answer.ok) return { ok: true };
195
+ return { ok: answer.status === 401 || answer.status === 403 ? false : null, status: answer.status };
196
+ } catch {
197
+ return { ok: null, status: null };
198
+ }
199
+ }
200
+
201
+ // Whether Google takes a Gemini key: listing one model spends nothing. A bad key is a 400 there
202
+ // (API_KEY_INVALID). `ok: null` when Google said nothing about it (offline, its own trouble).
203
+ export async function geminiTakesKey(key, { fetchImpl = globalThis.fetch, base = process.env.PULSE_GEMINI_API_URL ?? 'https://generativelanguage.googleapis.com', timeoutMs = 8000 } = {}) {
204
+ try {
205
+ const answer = await fetchImpl(`${base.replace(/\/$/, '')}/v1beta/models?pageSize=1`, { headers: { 'x-goog-api-key': key }, signal: AbortSignal.timeout(timeoutMs) });
206
+ if (answer.ok) return { ok: true };
207
+ return { ok: [400, 401, 403].includes(answer.status) ? false : null, status: answer.status };
208
+ } catch {
209
+ return { ok: null, status: null };
210
+ }
211
+ }
212
+
213
+ // ---- Claude: `env.ANTHROPIC_API_KEY` in its own settings, confirmed by Anthropic and by
214
+ // `claude auth status`. The CLI reads that env block before it runs, which is why a key there is a
215
+ // session: its own status answers `api_key` afterwards. Everything else in the file is left as it was.
216
+ async function applyClaudeKey(key, { home, probe, verify = anthropicTakesKey }) {
184
217
  const dir = join(home, '.claude');
185
218
  const file = join(dir, 'settings.json');
186
219
  await mkdir(dir, { recursive: true });
@@ -189,6 +222,11 @@ async function applyClaudeKey(key, { home, probe }) {
189
222
  let settings = {};
190
223
  try { settings = JSON.parse(before ?? '{}') ?? {}; } catch { settings = {}; }
191
224
  if (typeof settings !== 'object' || Array.isArray(settings)) settings = {};
225
+ // A Claude pointed at another address (a gateway of its own) takes keys Anthropic never sees.
226
+ if (!(settings.env?.ANTHROPIC_BASE_URL || process.env.ANTHROPIC_BASE_URL)) {
227
+ const taken = await verify(key);
228
+ if (taken.ok === false) return { ok: false, error: t('Anthropic did not accept this key (HTTP {status}): check you copied all of it, or make a new one at console.anthropic.com. Nothing was changed.', { status: taken.status }) };
229
+ }
192
230
  settings.env = { ...(settings.env ?? {}), ANTHROPIC_API_KEY: key };
193
231
  await writeFile(file, `${JSON.stringify(settings, null, 2)}\n`, { mode: OWNER_ONLY });
194
232
  await chmod(file, OWNER_ONLY).catch(() => {});
@@ -268,12 +306,12 @@ const runOpenCode = async (executable, args) => {
268
306
  };
269
307
 
270
308
  // Writes the key for one agent and answers whether that agent is signed in now.
271
- export async function applyKey({ agent, key, provider = null, home = homedir(), probe, executable = null, run = null }) {
309
+ export async function applyKey({ agent, key, provider = null, home = homedir(), probe, executable = null, run = null, verify = anthropicTakesKey, verifyGemini = geminiTakesKey }) {
272
310
  const checked = normalizeKey(key);
273
311
  if (!checked.ok) return checked;
274
312
  if (agent === 'codex') return applyCodexKey(checked.key, { home, probe, executable: executable ?? 'codex', run: run ?? runWithInput });
275
- if (agent === 'claude') return applyClaudeKey(checked.key, { home, probe });
276
- if (agent === 'gemini') return applyGeminiKey(checked.key, { home, probe });
313
+ if (agent === 'claude') return applyClaudeKey(checked.key, { home, probe, verify });
314
+ if (agent === 'gemini') return applyGeminiKey(checked.key, { home, probe, verify: verifyGemini });
277
315
  if (agent === 'opencode') return applyOpenCodeKey(checked.key, { home, provider, executable: executable ?? 'opencode', run: run ?? runOpenCode });
278
316
  return { ok: false, error: t('{agent} does not take a key here: it signs in with a click.', { agent }) };
279
317
  }
package/src/dataset.mjs CHANGED
@@ -10,7 +10,7 @@
10
10
  import { mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises';
11
11
  import { join } from 'node:path';
12
12
  import { redact } from './sentinel-errors.mjs';
13
- import { ABERRATION, VERDICTS, NEGATIVE_VERDICTS, POSITIVE_VERDICTS, travels } from './memory.mjs';
13
+ import { ABERRATION, VERDICTS, NEGATIVE_VERDICTS, POSITIVE_VERDICTS, travels, unsourced } from './memory.mjs';
14
14
 
15
15
  const MIN_ANSWER_CHARS = 40;
16
16
 
@@ -118,7 +118,8 @@ export function pairsFromNotes(notes, { project = 'project', home, user, privacy
118
118
  messages: [
119
119
  { role: 'system', content: `You are @madre, the memory of the MADRE room of the project "${project}". Answer only from what the room decided and recorded.` },
120
120
  { role: 'user', content: `What does the room remember about this? Kind: ${note.kind}. Topic: ${clean(redact(note.text, { home, user })).split(/[.;:]/)[0].slice(0, 80)}` },
121
- { role: 'assistant', content: `${clean(redact(note.text, { home, user }))} [#${note.fromSequence}${note.throughSequence !== note.fromSequence ? `–#${note.throughSequence}` : ''}]` },
121
+ // A note with no source is not taught with a citation it does not have (external review #15).
122
+ { role: 'assistant', content: `${clean(redact(note.text, { home, user }))}${unsourced(note) ? '' : ` [#${note.fromSequence}${note.throughSequence !== note.fromSequence ? `–#${note.throughSequence}` : ''}]`}` },
122
123
  ],
123
124
  }));
124
125
  }
@@ -234,29 +235,39 @@ export function preferencesFromAberrations(notes, { project = 'project', home, u
234
235
  return pairs;
235
236
  }
236
237
 
237
- // Deterministic split: the sequence decides, so re-exports keep lines on the same side.
238
+ // Deterministic split: the sequence decides, so re-exports keep lines on the same side. One in ten
239
+ // checks training as it goes (valid); another one in ten is never trained on, for the test after it
240
+ // (RX-30): `mlx_lm.lora --test` reads test.jsonl and stops without it, so train.sh ended in an error
241
+ // after training. A pair that was held out for checking stays where it was.
238
242
  export function split(pairs, { validEvery = 10 } = {}) {
239
243
  const train = [];
240
244
  const valid = [];
241
- for (const pair of pairs) (pair.sequence % validEvery === 0 ? valid : train).push(pair);
245
+ const test = [];
246
+ const half = Math.floor(validEvery / 2);
247
+ for (const pair of pairs) {
248
+ const at = pair.sequence % validEvery;
249
+ (at === 0 ? valid : at === half ? test : train).push(pair);
250
+ }
242
251
  if (!valid.length && train.length > 1) valid.push(train.pop());
243
- return { train, valid };
252
+ if (!test.length && train.length > 1) test.push(train.pop());
253
+ return { train, valid, test };
244
254
  }
245
255
 
246
256
  export async function exportDataset({ events, notes = [], dir, project = 'project', home, user, privacy = null, linked = [] }) {
247
257
  const pairs = [...pairsFromEvents(events, { project, home, user, privacy }), ...pairsFromNotes(notes, { project, home, user, privacy }), ...pairsFromLinked(linked, { home, user, privacy })].sort((a, b) => a.sequence - b.sequence);
248
258
  const preferences = preferencesFromAberrations(notes, { project, home, user, privacy });
249
- const { train, valid } = split(pairs);
259
+ const { train, valid, test } = split(pairs);
250
260
  await mkdir(dir, { recursive: true });
251
261
  const line = (pair) => JSON.stringify({ messages: pair.messages });
252
262
  await writeFile(join(dir, 'train.jsonl'), train.map(line).join('\n') + (train.length ? '\n' : ''));
253
263
  await writeFile(join(dir, 'valid.jsonl'), valid.map(line).join('\n') + (valid.length ? '\n' : ''));
264
+ await writeFile(join(dir, 'test.jsonl'), test.map(line).join('\n') + (test.length ? '\n' : ''));
254
265
  // Kept in a file of its own: it is a different shape and a different kind of training, and
255
266
  // nothing that reads the chat files should ever pick it up by accident.
256
267
  await writeFile(join(dir, 'preferences.jsonl'), preferences.map((pair) => JSON.stringify({ prompt: pair.prompt, chosen: pair.chosen, rejected: pair.rejected })).join('\n') + (preferences.length ? '\n' : ''));
257
268
  const byAgent = {};
258
269
  for (const pair of pairs) byAgent[pair.agent] = (byAgent[pair.agent] ?? 0) + 1;
259
- const manifest = { project, exportedAt: new Date().toISOString(), pairs: pairs.length, turns: pairs.filter((p) => p.kind === 'turn').length, delegated: pairs.filter((p) => p.kind === 'delegated').length, good: pairs.filter((p) => POSITIVE_VERDICTS.includes(p.rating)).length, notes: pairs.filter((p) => p.kind === 'note').length, linked: (linked ?? []).map((one) => ({ room: one.from, name: one.name, notes: pairs.filter((p) => p.kind === 'linked' && p.from === one.from).length, source: one.source ?? 'live', at: one.at ?? null })), aberrations: preferences.length, train: train.length, valid: valid.length, byAgent, files: ['train.jsonl', 'valid.jsonl', 'preferences.jsonl'], format: 'chat · {"messages":[{role,content}]} · mlx-lm / llama-factory / axolotl', preferenceFormat: 'preference · {"prompt","chosen","rejected"} · DPO / ORPO' };
270
+ const manifest = { project, exportedAt: new Date().toISOString(), pairs: pairs.length, turns: pairs.filter((p) => p.kind === 'turn').length, delegated: pairs.filter((p) => p.kind === 'delegated').length, good: pairs.filter((p) => POSITIVE_VERDICTS.includes(p.rating)).length, notes: pairs.filter((p) => p.kind === 'note').length, linked: (linked ?? []).map((one) => ({ room: one.from, name: one.name, notes: pairs.filter((p) => p.kind === 'linked' && p.from === one.from).length, source: one.source ?? 'live', at: one.at ?? null })), aberrations: preferences.length, train: train.length, valid: valid.length, test: test.length, byAgent, files: ['train.jsonl', 'valid.jsonl', 'test.jsonl', 'preferences.jsonl'], format: 'chat · {"messages":[{role,content}]} · mlx-lm / llama-factory / axolotl', preferenceFormat: 'preference · {"prompt","chosen","rejected"} · DPO / ORPO' };
260
271
  await writeFile(join(dir, 'manifest.json'), `${JSON.stringify(manifest, null, 2)}\n`);
261
272
  return { dir, ...manifest };
262
273
  }
@@ -27,8 +27,15 @@ function parseLines(text) {
27
27
  return { events, damaged };
28
28
  }
29
29
 
30
+ // How long a write or a read waits for another to let go of the ledger. It was 500 tries, about
31
+ // ten seconds: on a server with every core taken by local models (Carlos's, L-4, 2026-11-04) a
32
+ // write that fsyncs took longer, and the page's reader gave up with «Timed out waiting for event
33
+ // store lock». Thirty seconds of clock, however slow each try is.
34
+ export const LOCK_WAIT_MS = 30_000;
35
+
30
36
  export class EventStore {
31
37
  #file;
38
+ #lockWaitMs;
32
39
  #damaged = 0;
33
40
  #nextSequence = 1;
34
41
  #writeQueue = Promise.resolve();
@@ -42,8 +49,9 @@ export class EventStore {
42
49
  // It may be a function, read at every append, when other conversations of the project are
43
50
  // writing at the same time as this one: a number fixed when the ledger opened would hand out
44
51
  // a sequence another conversation has taken since.
45
- constructor(file, { floor = 0 } = {}) {
52
+ constructor(file, { floor = 0, lockWaitMs = LOCK_WAIT_MS } = {}) {
46
53
  this.#file = file;
54
+ this.#lockWaitMs = lockWaitMs;
47
55
  this.#floor = typeof floor === 'function' || Number.isFinite(floor) ? floor : 0;
48
56
  this.#lockDirectory = `${file}.lock`;
49
57
  }
@@ -121,7 +129,7 @@ export class EventStore {
121
129
  async #acquireLock() {
122
130
  const token = randomUUID();
123
131
  const ownerFile = join(this.#lockDirectory, 'owner.json');
124
- for (let attempt = 0; attempt < 500; attempt += 1) {
132
+ for (const deadline = Date.now() + this.#lockWaitMs; Date.now() < deadline;) {
125
133
  let created = false;
126
134
  try {
127
135
  await mkdir(this.#lockDirectory);