@ajuarezso/lienzo-mcp 0.3.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/lienzo.mjs CHANGED
@@ -1,15 +1,29 @@
1
1
  // Operaciones de Lienzo sobre Firestore (contrato en docs/spec.md).
2
2
  import { randomBytes } from 'node:crypto';
3
- import { readFileSync } from 'node:fs';
4
- import { basename, extname } from 'node:path';
3
+ import { execFileSync } from 'node:child_process';
4
+ import { readFileSync, mkdirSync, writeFileSync, linkSync, rmSync } from 'node:fs';
5
+ import { basename, extname, join } from 'node:path';
5
6
  import { Firestore, ErrorFirestore, filtro, y, marcaDeTiempo } from './firestore.mjs';
6
7
  import { Sesion, ErrorSesion, abrirNavegador, ENTRAR_TIMEOUT_MS } from './sesion.mjs';
7
8
  import { buscarSecretos, explicarBloqueo } from './secretos.mjs';
8
- import { envolver, limpiarLinea, neutralizar, abreviarId, aviso, nuevoCodigo } from './envoltorio.mjs';
9
+ import { envolver, limpiarLinea, neutralizar, abreviarId, aviso, nuevoCodigo, tieneInvisibles, tieneLetraONumero } from './envoltorio.mjs';
9
10
  import { rutaCompartible, revisarNombre, ErrorArchivo } from './archivos.mjs';
10
11
  import { INTERVALO_MS } from './esperar.mjs';
11
12
  import { cerrarTimbre } from './escucha.mjs';
12
- import { idDeAgente, cuentaDe, esAgenteDe, TOPE_AGENTES } from './agentes.mjs';
13
+ import { idDeAgente, cuentaDe, esAgenteDe, TOPE_AGENTES, RE_SLUG } from './agentes.mjs';
14
+ import {
15
+ prepararInstalacion, resolverAgente, clienteDeMcp, nombreDeCliente, nombresAplicados, nombreDeConfigManda, anotarNombreAplicado,
16
+ } from './instalacion.mjs';
17
+ import { Storage, ErrorStorage } from './storage.mjs';
18
+ import {
19
+ ErrorAdjunto, detectarTipo, revisarTamano, sha256, nombreEnStorage, mimeParaStorage, disposicion, carpetaDescargas,
20
+ nombreDescarga, nombreDeTipo, revisarBajado, revisarZip, imagenVisible, MAX_ADJUNTO_BYTES, MAX_IMAGEN_VISIBLE,
21
+ IMAGENES_VISIBLES, MB,
22
+ } from './adjuntos.mjs';
23
+ import {
24
+ referenciasDeMenciones, mencionesDe, menciona, RACHA_VACIA, sumarTarjeta, rachaDeRecientes, pasaElTope, textoTope, lineaTope,
25
+ vaAOtroAgente, esPersona, PAGINA_TURNOS, PAGINAS_TURNOS, TOPE_TURNOS,
26
+ } from './menciones.mjs';
13
27
 
14
28
  export const MAX_TEXTO = 20000;
15
29
  export const MAX_ARCHIVO_BYTES = 200 * 1024;
@@ -22,21 +36,48 @@ const RECARGA_PARTICIPANTES_MS = 30 * 1000;
22
36
  const VISTA_ARCHIVO = 3000; // caracteres de un archivo que se muestran en una lista
23
37
  const LIMITE_CONSULTA = 50;
24
38
 
25
- /** El nombre de la persona: el de su cuenta en una línea (hasta 80) o, si no hay, la parte local del correo. */
39
+ /**
40
+ * El nombre de la persona: el de su cuenta en una línea (hasta 80) o, si no hay (o no tiene ni una letra ni un número
41
+ * visibles, que las reglas rechazan), la parte local del correo.
42
+ */
26
43
  export function nombreDeCuenta(cuenta) {
27
44
  const n = limpiarLinea(cuenta?.nombre ?? '', 80);
28
- if (n) return n;
45
+ if (n && tieneLetraONumero(n)) return n;
29
46
  const local = limpiarLinea(String(cuenta?.email ?? '').split('@')[0], 80);
30
- return local || null;
47
+ return local && tieneLetraONumero(local) ? local : null;
31
48
  }
32
49
 
33
50
  /**
34
- * «Agente de <primer nombre>» (o null si la cuenta no tiene nombre ni correo). Con un agente con nombre
35
- * (LIENZO_AGENTE), «Agente de <primer nombre> · <slug>»: así tres agentes de la misma cuenta no se llaman igual.
51
+ * «<Cliente> de <primer nombre>» («Claude Code de Ana»; un cliente desconocido, «Agente de Ana»), o null si la cuenta
52
+ * no tiene nombre ni correo. Con un perfil de Claude Code que no es el de siempre (CLAUDE_CONFIG_DIR=~/.claude-ontop),
53
+ * «Claude Code (ontop) de Ana»: así la persona sabe en la web cuál es cuál. Con LIENZO_AGENTE explícito, «… · <slug>».
54
+ * Un slug automático no se muestra (no le dice nada a nadie): si choca, unirse() suma « · 2».
36
55
  */
37
- export function nombreAgentePorDefecto(cuenta, slug = null) {
56
+ export function nombreAgentePorDefecto(cuenta, slug = null, cliente = 'claude-code', perfil = null) {
38
57
  const n = nombreDeCuenta(cuenta);
39
- return n ? limpiarLinea(`Agente de ${n.split(/\s+/)[0]}${slug ? ` · ${slug}` : ''}`, 80) : null;
58
+ const deCliente = `${nombreDeCliente(cliente)}${perfil && cliente === 'claude-code' ? ` (${perfil})` : ''}`;
59
+ return n ? limpiarLinea(`${deCliente} de ${n.split(/\s+/)[0]}${slug ? ` · ${slug}` : ''}`, 80) : null;
60
+ }
61
+
62
+ /**
63
+ * ¿Sirve como nombre de participante? Una línea, sin invisibles (también los que no son de formato: U+034F, rellenos
64
+ * Hangul, braille en blanco, un selector de variante suelto), con al menos una letra o un número, 1..80. Lo mismo que
65
+ * exigen las reglas (nombreVisible en firestore.rules).
66
+ */
67
+ export const nombreValido = (n) => typeof n === 'string' && n.trim().length > 0 && n.length <= 80 && !tieneInvisibles(n)
68
+ && tieneLetraONumero(n);
69
+
70
+ /** «hace un momento», «hace 5 min», «hace 3 h», «hace 2 días» o la fecha (AAAA-MM-DD). */
71
+ export function haceCuanto(iso, ahora = Date.now()) {
72
+ const t = Date.parse(iso ?? '');
73
+ if (!Number.isFinite(t)) return 'sin fecha';
74
+ const s = Math.max(0, Math.round((ahora - t) / 1000));
75
+ if (s < 60) return 'hace un momento';
76
+ if (s < 3600) return `hace ${Math.floor(s / 60)} min`;
77
+ if (s < 86400) return `hace ${Math.floor(s / 3600)} h`;
78
+ const d = Math.floor(s / 86400);
79
+ if (d < 30) return `hace ${d} día${d === 1 ? '' : 's'}`;
80
+ return new Date(t).toISOString().slice(0, 10);
40
81
  }
41
82
 
42
83
  // ── listas de puntos (docs/producto.md §4) ──────────────────────────────────
@@ -115,8 +156,26 @@ function quitarGrupo(lista, { alFinal }) {
115
156
  return resto.length ? resto : lista; // un solo grupo que llena el tope: se entrega igual (no se traba)
116
157
  }
117
158
 
118
- /** Para comparar nombres: sin controles, sin mayúsculas, ancho normal, espacios simples. */
119
- export const claveNombre = (s) => limpiarLinea(s, 200).normalize('NFKC').toLowerCase().replace(/\s+/g, ' ').trim();
159
+ // Letras cirílicas y griegas que se ven igual que una latina (las más comunes). Solo para COMPARAR nombres: «Claude Code
160
+ // de Аna» (A cirílica) cuenta como homónimo de «Claude Code de Ana» (revisión de seguridad, 21-10-2026).
161
+ const PARECIDAS = new Map(Object.entries({
162
+ А: 'A', В: 'B', Е: 'E', К: 'K', М: 'M', Н: 'H', О: 'O', Р: 'P', С: 'C', Т: 'T', Х: 'X', У: 'Y', І: 'I', Ј: 'J', Ѕ: 'S',
163
+ а: 'a', е: 'e', о: 'o', р: 'p', с: 'c', у: 'y', х: 'x', і: 'i', ј: 'j', ѕ: 's', ԁ: 'd', һ: 'h', ӏ: 'l', ɡ: 'g', ı: 'i',
164
+ Α: 'A', Β: 'B', Ε: 'E', Ζ: 'Z', Η: 'H', Ι: 'I', Κ: 'K', Μ: 'M', Ν: 'N', Ο: 'O', Ρ: 'P', Τ: 'T', Υ: 'Y', Χ: 'X',
165
+ ο: 'o', ν: 'v', ρ: 'p', ι: 'i', κ: 'k', υ: 'u', χ: 'x', α: 'a',
166
+ }));
167
+
168
+ /**
169
+ * Para comparar nombres: sin controles ni invisibles, sin tildes ni marcas combinantes (NFKD sin \p{Mn}), con las
170
+ * letras cirílicas o griegas que parecen latinas pasadas a latinas, sin mayúsculas y con espacios simples. No sirve para
171
+ * mostrar: solo para decidir si dos nombres se confunden a la vista.
172
+ */
173
+ export function claveNombre(s) {
174
+ const t = limpiarLinea(s, 200).normalize('NFKD').replace(/\p{Mn}/gu, '');
175
+ let r = '';
176
+ for (const c of t) r += PARECIDAS.get(c) ?? c;
177
+ return r.normalize('NFKC').toLowerCase().replace(/\s+/g, ' ').trim();
178
+ }
120
179
 
121
180
  const ALFABETO = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
122
181
 
@@ -175,6 +234,8 @@ export class Lienzo {
175
234
  this.entrada = null; // login en curso: { promesa, url }
176
235
  this.sesion = new Sesion(config, { fetch: f });
177
236
  this.fs = new Firestore(config, this.sesion, { fetch: f });
237
+ this.storage = new Storage(config, this.sesion, { fetch: f });
238
+ this.accesoEn = null; // en qué sala ya se escribió accesos/{uid} (lo que storage.rules mira para dejar leer y subir)
178
239
  this.salaId = null;
179
240
  this.yo = null; // { id, nombre, dueño }
180
241
  this.marca = null; // creadaEn más reciente ya entregada
@@ -187,6 +248,9 @@ export class Lienzo {
187
248
  this.tareasPedidas = new Map(); // id → { estado, respuesta, tomadaPor }
188
249
  this.tareasCargadas = false;
189
250
  this.listas = new Set(); // ids de las listas que pediste (sus puntos llegan agrupados)
251
+ // turnos agente↔agente de lo que ya se entregó (menciones.mjs); `hasta` = creadaEn de la última sumada, así una
252
+ // tarjeta que vuelve a llegar (canal y lienzo_esperar) no suma dos veces
253
+ this.racha = { ...RACHA_VACIA, hasta: '' };
190
254
  this.ultimoLatido = 0;
191
255
  this.actividad = null; // lo último escrito en participantes.actividad ('esperando' | 'escribiendo' | null)
192
256
  this.latidoFallo = null; // el error del último latido (null si anduvo)
@@ -194,14 +258,44 @@ export class Lienzo {
194
258
  // cuenta a la que le pasó: si la sesión cambia de cuenta deja de valer.
195
259
  this.salasPerdidas = new Map();
196
260
  this.ultimaPerdida = null; // { uid, salaId, texto }: la última, para decir la causa cuando ya no hay sala
261
+ // Id automático por instalación (lib/instalacion.mjs): agente.json se prepara al arrancar (ahí se fija si este home
262
+ // ya tenía sesión, para heredar `{uid}_agente`); el slug se resuelve en el primer unirse.
263
+ prepararInstalacion(config.home);
264
+ this.slugInstalacion = undefined; // undefined = sin resolver; null = el de siempre; string = slug automático
265
+ this.clienteMcp = null; // clientInfo.name del handshake (lo pone index.mjs al inicializar)
197
266
  }
198
267
 
199
268
  // ── utilidades ─────────────────────────────────────────────────────────────
200
269
  /** La cuenta (la persona). */
201
270
  get uid() { return this.sesion.uid; }
202
271
 
203
- /** El slug de este puente (LIENZO_AGENTE), o null: el agente de siempre. */
204
- get slug() { return this.config.agente ?? null; }
272
+ /**
273
+ * El slug de este puente: LIENZO_AGENTE si viene; si no, el automático de la instalación (resuelto en unirse); null =
274
+ * el agente de siempre (`{uid}_agente`: la instalación que lo heredó, o antes de resolver).
275
+ */
276
+ get slug() { return this.config.agente ?? this.slugInstalacion ?? null; }
277
+
278
+ /** El cliente: LIENZO_CLIENTE si viene; si no, el del handshake MCP si se reconoce; si no, 'claude-code'. */
279
+ get cliente() {
280
+ if (this.config.clienteExplicito) return this.config.cliente;
281
+ return clienteDeMcp(this.clienteMcp) ?? this.config.cliente ?? 'claude-code';
282
+ }
283
+
284
+ /** La instalación: `<cliente>@<CLAUDE_CONFIG_DIR o predeterminada>` (clave en agente.json). */
285
+ get claveInstalacion() {
286
+ return `${this.cliente}@${this.config.carpetaConfig ?? 'predeterminada'}`;
287
+ }
288
+
289
+ /** Fija el slug automático de esta instalación (una vez por proceso; estable entre arranques por agente.json). */
290
+ async _resolverInstalacion() {
291
+ if (this.config.agente || this.slugInstalacion !== undefined) return;
292
+ const { agente, origen } = await resolverAgente(this.config.home, this.claveInstalacion);
293
+ // otra llamada pudo resolverlo mientras tanto: gana la primera (el id no cambia a mitad del proceso)
294
+ if (this.slugInstalacion === undefined) {
295
+ this.slugInstalacion = agente;
296
+ console.error(`[lienzo] instalación ${this.claveInstalacion}: ${agente ? `agente ${agente}` : 'el agente de siempre'} (${origen})`);
297
+ }
298
+ }
205
299
 
206
300
  /** El participante de este agente en la sala: `{uid}_agente` o, con LIENZO_AGENTE, `{uid}_agente_{slug}`. */
207
301
  get participanteId() { return this.uid ? idDeAgente(this.uid, this.slug) : null; }
@@ -238,7 +332,12 @@ export class Lienzo {
238
332
  }
239
333
 
240
334
  _traducir(e, que) {
241
- if (e instanceof ErrorSesion || e instanceof ErrorArchivo) return new ErrorLienzo(e.message);
335
+ if (e instanceof ErrorSesion || e instanceof ErrorArchivo || e instanceof ErrorAdjunto) return new ErrorLienzo(e.message);
336
+ if (e instanceof ErrorStorage) {
337
+ if (e.negado) return new ErrorLienzo(`Storage no permite ${que}. (${e.message})`, { cause: e });
338
+ if (e.noExiste) return new ErrorLienzo(`No está el archivo en Storage: ${que}. Si recién se compartió, prueba en unos segundos.`, { cause: e });
339
+ return new ErrorLienzo(`No se pudo ${que}: ${e.message}`, { cause: e });
340
+ }
242
341
  if (e instanceof ErrorFirestore) {
243
342
  // la causa viaja: el catch de cada herramienta decide si el permiso negado tiene una explicación (explicarNegado)
244
343
  if (e.negado) return new ErrorLienzo(`Las reglas de la sala no permiten ${que}. (${e.message})`, { cause: e });
@@ -277,7 +376,8 @@ export class Lienzo {
277
376
  // te sacaron de la sala y todavía no entras a otra: la causa, no un genérico
278
377
  if (this.ultimaPerdida?.uid === this.uid) throw new ErrorLienzo(this.ultimaPerdida.texto);
279
378
  throw new ErrorLienzo(
280
- 'Todavía no estás en una sala. Usa lienzo_unirse(salaId) o crea una con lienzo_crear_sala.',
379
+ 'Todavía no estás en una sala. Usa lienzo_mis_salas para ver tus salas y lienzo_unirse para entrar (con su nombre, ' +
380
+ 'su link o su salaId), o crea una con lienzo_crear_sala.',
281
381
  );
282
382
  }
283
383
 
@@ -324,16 +424,22 @@ export class Lienzo {
324
424
  }
325
425
 
326
426
  /**
327
- * ¿Lo que trajo lienzo_esperar le toca a este agente? Algo dirigido a él (paraId) o un cambio en una tarea
427
+ * ¿Lo que trajo lienzo_esperar le toca a este agente? Algo dirigido a él (paraId), que lo menciona, o un cambio en una tarea
328
428
  * que pidió. Solo entonces la web muestra «Escribiendo…»: un mensaje a toda la sala no pone a todos los
329
429
  * agentes a escribir.
330
430
  */
331
431
  meToca(lote) {
332
432
  if (!lote) return false;
333
- const mio = (t) => t.autorId !== this.participanteId && t.paraId === this.participanteId;
433
+ // para mí (paraId) o me mencionan (menciones): la convención es contestar solo en esos casos
434
+ const mio = (t) => t.autorId !== this.participanteId && (t.paraId === this.participanteId || menciona(t, this.participanteId));
334
435
  return (lote.cambios?.length ?? 0) > 0 || (lote.tarjetas ?? []).some(mio) || (lote.editadas ?? []).some(mio);
335
436
  }
336
437
 
438
+ /** ¿Una tarjeta ajena te menciona a ti (este agente)? */
439
+ _meMencionan(t) {
440
+ return Boolean(t) && t.autorId !== this.participanteId && menciona(t, this.participanteId);
441
+ }
442
+
337
443
  async _cargarParticipantes(forzar = false) {
338
444
  if (!forzar && Date.now() - this.participantesEn < RECARGA_PARTICIPANTES_MS && this.participantes.size) return;
339
445
  const lista = await this.fs.consultar(`salas/${this.salaId}`, {
@@ -422,27 +528,132 @@ export class Lienzo {
422
528
  }
423
529
 
424
530
  /**
425
- * Entra a la sala como `{uid}_agente` (tipo agente, dueñoUid = la cuenta, dueño = el nombre de la cuenta).
426
- * nombre: el que se pase, LIENZO_NOMBRE o «Agente de <primer nombre de la cuenta>».
531
+ * Las salas de tu cuenta (índice `usuarios/{uid}/salas`, las de `vistaEn` más nuevo primero), sin las borradas:
532
+ * [{ salaId, nombre, vistaEn, archivada, creadaPorMi, aqui, link }]. 1 consulta + 1 batchGet.
533
+ */
534
+ async misSalas({ limite = 30 } = {}) {
535
+ await this.sesion.token().catch((e) => { throw this._traducir(e, 'iniciar sesión'); });
536
+ let indice;
537
+ let salas;
538
+ try {
539
+ indice = (await this.fs.consultar(`usuarios/${this.uid}`, {
540
+ from: [{ collectionId: 'salas' }],
541
+ orderBy: [{ field: { fieldPath: 'vistaEn' }, direction: 'DESCENDING' }],
542
+ limit: limite,
543
+ })).filter((f) => /^[A-Za-z0-9_-]{22}$/.test(f.id));
544
+ salas = indice.length ? await this.fs.leerVarios(indice.map((f) => `salas/${f.id}`)) : [];
545
+ } catch (e) {
546
+ throw this._traducir(e, 'leer tus salas');
547
+ }
548
+ const porId = new Map(salas.map((s) => [s.id, s]));
549
+ return indice.flatMap((f) => {
550
+ const s = porId.get(f.id);
551
+ if (!s || s.borradaEn) return [];
552
+ return [{
553
+ salaId: f.id, nombre: String(s.nombre ?? ''), vistaEn: f.vistaEn ?? null, archivada: Boolean(s.archivadaEn),
554
+ creadaPorMi: s.creadaPor === this.uid, aqui: f.id === this.salaId, link: this.link(f.id),
555
+ }];
556
+ });
557
+ }
558
+
559
+ /** La sala de lo que pasó el agente: link, salaId o el nombre de una de tus salas (no se adivina entre varias). */
560
+ async _salaDe(entrada) {
561
+ const m = /\/sala\/([A-Za-z0-9_-]{22})(?:[/?#]|$)/.exec(entrada);
562
+ if (m) return m[1];
563
+ if (/^[A-Za-z0-9_-]{22}$/.test(entrada)) return entrada; // si no existe, unirse() lo prueba como nombre
564
+ if (/^https?:\/\//i.test(entrada) || /\/sala\//.test(entrada)) {
565
+ throw new ErrorLienzo('Ese link no es de una sala de Lienzo (…/sala/<salaId de 22 caracteres>). Revisa el que te pasaron.');
566
+ }
567
+ const salaId = await this._salaPorNombre(entrada);
568
+ if (salaId) return salaId;
569
+ throw new ErrorLienzo(`No encontré una sala tuya que se llame «${limpiarLinea(entrada, 120)}». Por nombre solo busco entre tus ` +
570
+ 'salas (las que abriste tú o tu agente): lienzo_mis_salas las lista. Si es de otra persona, pídele el link.');
571
+ }
572
+
573
+ /**
574
+ * Por nombre, entre tus salas. Entra sola SOLO si hay una única coincidencia (exacta o parcial, juntas) y es una sala
575
+ * que creó tu usuario: el nombre de una sala ajena lo pone quien la creó y puede renombrarla para que calce con lo
576
+ * que vas a escribir (revisión de seguridad, 21-10-2026). En cualquier otro caso lista y no elige. Un nombre con
577
+ * caracteres invisibles nunca calza. null si no hay ninguna.
578
+ */
579
+ async _salaPorNombre(entrada) {
580
+ const salas = (await this.misSalas()).filter((s) => !tieneInvisibles(s.nombre));
581
+ const clave = claveNombre(entrada);
582
+ if (!clave) return null;
583
+ const exactas = salas.filter((s) => claveNombre(s.nombre) === clave);
584
+ const parciales = clave.length >= 3 ? salas.filter((s) => claveNombre(s.nombre) !== clave && claveNombre(s.nombre).includes(clave)) : [];
585
+ const hallo = [...exactas, ...parciales];
586
+ if (!hallo.length) return null;
587
+ if (hallo.length === 1 && hallo[0].creadaPorMi) return hallo[0].salaId;
588
+ const filas = hallo.map((s) => `- salaId: ${s.salaId} · ${s.creadaPorMi ? 'la creó tu usuario' : 'de otra persona'} · ` +
589
+ `tu última vez: ${haceCuanto(s.vistaEn)}${s.archivada ? ' · archivada' : ''}`);
590
+ throw new ErrorLienzo(`${hallo.length === 1 ? 'Hay 1 sala que coincide' : `Tienes ${hallo.length} salas que coinciden`} con ` +
591
+ `«${limpiarLinea(entrada, 120)}»${hallo.length === 1 ? ', pero no la creó tu usuario' : ''}: no elijo por ti. ` +
592
+ `Pregúntale a tu usuario cuál (lienzo_mis_salas las muestra con su nombre) y usa su salaId:\n${filas.join('\n')}`);
593
+ }
594
+
595
+ /** Los nombres (normalizados) de los otros agentes de mi cuenta en la sala. Solo lee si ya hay alguno (si no, []). */
596
+ async _nombresDeHermanos(salaId) {
597
+ try {
598
+ const idx = await this.fs.leer(`salas/${salaId}/cuentas/${this.uid}`).catch(() => null);
599
+ const slugs = (Array.isArray(idx?.agentes) ? idx.agentes : []).filter((x) => RE_SLUG.test(String(x)));
600
+ const ids = [idDeAgente(this.uid), ...slugs.map((x) => idDeAgente(this.uid, x))].filter((id) => id !== this.participanteId);
601
+ const docs = await this.fs.leerVarios(ids.map((id) => `salas/${salaId}/participantes/${id}`));
602
+ return docs.filter((d) => d.tipo === 'agente').map((d) => claveNombre(d.nombre ?? ''));
603
+ } catch {
604
+ return []; // sin permiso todavía (la cuenta no está en la sala): no hay hermanos que mirar
605
+ }
606
+ }
607
+
608
+ /** Nombre por defecto para un agente que entra por primera vez: con « · 2», « · 3»… si otro de mis agentes ya se llama así. */
609
+ async _nombreNuevo(salaId, cuenta) {
610
+ const base = nombreAgentePorDefecto(cuenta, this.config.agente, this.cliente, this.config.perfil);
611
+ if (!base) return null;
612
+ const usados = new Set(await this._nombresDeHermanos(salaId));
613
+ if (!usados.has(claveNombre(base))) return base;
614
+ for (let n = 2; n < 100; n++) {
615
+ const candidato = limpiarLinea(`${base.slice(0, 74)} · ${n}`, 80);
616
+ if (!usados.has(claveNombre(candidato))) return candidato;
617
+ }
618
+ return base;
619
+ }
620
+
621
+ /**
622
+ * Entra a la sala como el agente de esta instalación: `{uid}_agente_{slug}` (slug de LIENZO_AGENTE o el automático
623
+ * de la instalación) o `{uid}_agente` (la instalación que heredó el de siempre). Tipo agente, dueñoUid = la cuenta,
624
+ * dueño = el nombre de la cuenta. `salaId` acepta el id, el link o el nombre de una de tus salas.
625
+ * Nombre: el que se pase o LIENZO_NOMBRE (mandan siempre); si no, el que ya tenga el agente en la sala (p. ej. el que
626
+ * le puso su persona desde la web: no se pisa); si entra por primera vez, «<Cliente> de <primer nombre>» (+ « · 2»
627
+ * si otro agente de la cuenta ya se llama así).
427
628
  */
428
629
  async unirse({ salaId, nombre }) {
429
630
  this._exigirAgenteValido();
430
- salaId = String(salaId || '').trim();
431
- // aceptar también el link completo de la web
432
- const m = /\/sala\/([A-Za-z0-9_-]{22})/.exec(salaId);
433
- if (m) salaId = m[1];
434
- if (!/^[A-Za-z0-9_-]{22}$/.test(salaId)) throw new ErrorLienzo('El salaId tiene 22 caracteres (letras, números, - y _). Revisa el que te pasaron.');
631
+ const entrada = String(salaId || '').trim();
632
+ if (!entrada) throw new ErrorLienzo('Falta la sala: pasa su link, su salaId o su nombre (lienzo_mis_salas lista las tuyas).');
633
+ // `nombre` (lo pidió tu usuario en esta llamada) manda siempre; LIENZO_NOMBRE no pisa un nombre puesto desde la web
634
+ const pedido = String(nombre || '').trim();
635
+ const deConfig = pedido ? '' : String(this.config.nombre || '').trim();
636
+ const explicito = pedido || deConfig;
637
+ if (explicito.length > 80) throw new ErrorLienzo('El nombre admite hasta 80 caracteres.');
638
+ if (tieneInvisibles(explicito)) throw new ErrorLienzo('El nombre va en una sola línea, sin caracteres de control ni invisibles.');
639
+ if (explicito && !tieneLetraONumero(explicito)) throw new ErrorLienzo('El nombre necesita al menos una letra o un número.');
640
+ if (explicito) this._vigilarSecretos([['tu nombre', explicito]]);
435
641
 
436
642
  await this.sesion.token().catch((e) => { throw this._traducir(e, 'iniciar sesión'); });
643
+ salaId = await this._salaDe(entrada);
644
+ await this._resolverInstalacion();
437
645
  const cuenta = this.sesion.cuenta;
438
646
  const dueño = nombreDeCuenta(cuenta);
439
- nombre = String(nombre || this.config.nombre || nombreAgentePorDefecto(cuenta, this.slug) || '').trim();
440
- if (!nombre) throw new ErrorLienzo('Falta tu nombre en la sala (p. ej. «Agente de Ana»): pásalo en nombre.');
441
- if (nombre.length > 80) throw new ErrorLienzo('El nombre admite hasta 80 caracteres.');
442
- if (/[\p{Cc}\p{Cf}\u2028\u2029]/u.test(nombre)) throw new ErrorLienzo('El nombre va en una sola línea, sin caracteres de control ni invisibles.');
443
- this._vigilarSecretos([['tu nombre', nombre]]);
444
647
 
445
- const sala = await this.fs.leer(`salas/${salaId}`).catch((e) => { throw this._traducir(e, 'leer la sala'); });
648
+ let sala = await this.fs.leer(`salas/${salaId}`).catch((e) => { throw this._traducir(e, 'leer la sala'); });
649
+ // 22 letras y números que no son el id de ninguna sala: puede ser el NOMBRE de una de tus salas
650
+ if (!sala && salaId === entrada) {
651
+ const porNombre = await this._salaPorNombre(entrada);
652
+ if (porNombre) {
653
+ salaId = porNombre;
654
+ sala = await this.fs.leer(`salas/${salaId}`).catch((e) => { throw this._traducir(e, 'leer la sala'); });
655
+ }
656
+ }
446
657
  if (!sala) throw new ErrorLienzo('Esa sala no existe. Revisa el salaId.');
447
658
  // borrada: las reglas ya no dejan escribir ahí; se avisa sin tocar nada
448
659
  if (sala.borradaEn) {
@@ -452,8 +663,13 @@ export class Lienzo {
452
663
 
453
664
  const ruta = `salas/${salaId}/participantes/${this.participanteId}`;
454
665
  const previo = await this.fs.leer(ruta).catch(() => null);
666
+ const enSala = nombreValido(previo?.nombre) ? previo.nombre : '';
667
+ const clave = this.claveInstalacion;
668
+ const configManda = deConfig && nombreDeConfigManda(deConfig, enSala, nombresAplicados(this.config.home, clave));
669
+ nombre = pedido || (configManda ? deConfig : '') || enSala || deConfig || (await this._nombreNuevo(salaId, cuenta)) || '';
670
+ if (!nombre) throw new ErrorLienzo('Falta tu nombre en la sala (p. ej. «Claude Code de Ana»): pásalo en nombre.');
455
671
  const datos = {
456
- nombre, tipo: 'agente', 'dueño': dueño, 'dueñoUid': this.uid, cliente: this.config.cliente,
672
+ nombre, tipo: 'agente', 'dueño': dueño, 'dueñoUid': this.uid, cliente: this.cliente,
457
673
  posicion: previo?.posicion ?? null,
458
674
  };
459
675
  let commit;
@@ -478,8 +694,11 @@ export class Lienzo {
478
694
  throw texto ? new ErrorLienzo(texto, { cause: e }) : this._traducir(e, 'unirte a la sala');
479
695
  }
480
696
 
697
+ if (deConfig) await anotarNombreAplicado(this.config.home, clave, deConfig);
481
698
  this.salasPerdidas.delete(salaId); // un lienzo_unirse explícito vuelve a abrir la sala
482
699
  this.ultimaPerdida = null;
700
+ // accesos/{uid} se vuelve a escribir: pudo borrarse al salir desde la web, o la cuenta es otra
701
+ this.accesoEn = null;
483
702
  const cambioDeSala = this.salaId !== salaId || this.uidEnSala !== this.uid;
484
703
  this.salaId = salaId;
485
704
  this.uidEnSala = this.uid;
@@ -496,6 +715,7 @@ export class Lienzo {
496
715
  this.participantes.clear();
497
716
  this.participantesEn = 0;
498
717
  this._ausentes.clear();
718
+ this.racha = { ...RACHA_VACIA, hasta: '' };
499
719
  }
500
720
  await this._anotarEnIndice(salaId);
501
721
  return { salaId, sala, link: this.link(salaId), yo: this.yo };
@@ -507,8 +727,11 @@ export class Lienzo {
507
727
  const idx = await this.fs.leer(`salas/${salaId}/cuentas/${this.uid}`);
508
728
  const agentes = Array.isArray(idx?.agentes) ? idx.agentes : [];
509
729
  if (agentes.length < TOPE_AGENTES || agentes.includes(this.slug)) return null;
510
- return `Tu cuenta ya tiene ${agentes.length} agentes con nombre en esta sala (${agentes.map((a) => limpiarLinea(a, 24)).join(', ')}), ` +
511
- `que es el tope. Usa uno de esos en LIENZO_AGENTE, o sal de la sala desde la web y vuelve a entrar con los que necesites.`;
730
+ return `Tu cuenta ya tiene ${agentes.length} agentes en esta sala (${agentes.map((a) => limpiarLinea(a, 24)).join(', ')}), ` +
731
+ 'que es el tope. Cada instalación del puente (cada perfil de Claude Code, cada cliente o cada máquina) es un agente ' +
732
+ 'aparte. Pídele a tu usuario que abra la sala en la web, vaya a ⋮ → Participantes y use «Quitar» en el agente suyo ' +
733
+ 'que ya no usa (sale solo ese agente; él y los demás siguen en la sala). Después vuelve a llamar lienzo_unirse. O ' +
734
+ 'usa en LIENZO_AGENTE uno de esos.';
512
735
  }
513
736
 
514
737
  // ── cuenta ─────────────────────────────────────────────────────────────────
@@ -517,6 +740,7 @@ export class Lienzo {
517
740
  this.salaId = null;
518
741
  this.yo = null;
519
742
  this.uidEnSala = null;
743
+ this.accesoEn = null;
520
744
  }
521
745
 
522
746
  /**
@@ -647,8 +871,7 @@ export class Lienzo {
647
871
  };
648
872
  }
649
873
 
650
- async _crearTarjeta(datos) {
651
- const id = nuevoId();
874
+ async _crearTarjeta(datos, id = nuevoId()) {
652
875
  try {
653
876
  await this.fs.escribir(`salas/${this.salaId}/tarjetas/${id}`, this._tarjetaCompleta(datos), { crear: true, horaServidor: ['creadaEn'] });
654
877
  } catch (e) {
@@ -665,22 +888,127 @@ export class Lienzo {
665
888
  return texto;
666
889
  }
667
890
 
668
- async publicar({ texto, paraId = null, respondeA = null, tipo = 'mensaje' }) {
891
+ /**
892
+ * menciones (ids o nombres, «@» opcional) → ids de participantes que existen, sin repetidos y sin ti. Un nombre que
893
+ * llevan dos no se adivina (_resolverParticipante). «@todos» o una lista vacía = sin menciones (toda la sala).
894
+ */
895
+ async _resolverMenciones(menciones) {
896
+ let refs;
897
+ try { refs = referenciasDeMenciones(menciones); } catch (e) { throw new ErrorLienzo(e.message); }
898
+ const ids = [];
899
+ for (const r of refs) {
900
+ const id = await this._resolverParticipante(r);
901
+ if (id && id !== this.participanteId && !ids.includes(id)) ids.push(id);
902
+ }
903
+ return ids;
904
+ }
905
+
906
+ /**
907
+ * Las tarjetas recientes para contar turnos (menciones.mjs): de la más nueva a la más vieja, de a PAGINA_TURNOS,
908
+ * hasta dar con una de una persona o PAGINAS_TURNOS páginas. El cursor es (creadaEn, id): los puntos de una lista
909
+ * nacen en el mismo commit y comparten la hora, así que un corte solo por la hora se saltaría tarjetas.
910
+ */
911
+ async _rachaReciente() {
912
+ const recientes = [];
913
+ let agotada = false;
914
+ let cursor = null;
915
+ for (let i = 0; i < PAGINAS_TURNOS; i++) {
916
+ const consulta = {
917
+ from: [{ collectionId: 'tarjetas' }],
918
+ orderBy: [
919
+ { field: { fieldPath: 'creadaEn' }, direction: 'DESCENDING' },
920
+ { field: { fieldPath: '__name__' }, direction: 'DESCENDING' },
921
+ ],
922
+ limit: PAGINA_TURNOS,
923
+ };
924
+ if (cursor) consulta.startAt = { values: cursor, before: false };
925
+ const pagina = await this.fs.consultar(`salas/${this.salaId}`, consulta)
926
+ .catch((e) => { throw this._traducir(e, 'leer las últimas tarjetas de la sala'); });
927
+ recientes.push(...pagina);
928
+ const ultima = pagina.at(-1);
929
+ if (pagina.length < PAGINA_TURNOS || !ultima?.creadaEn) { agotada = true; break; }
930
+ if (pagina.some((t) => esPersona(t.autorId))) break; // lo anterior a una persona no cuenta
931
+ cursor = [
932
+ { timestampValue: ultima.creadaEn },
933
+ { referenceValue: this.fs.nombre(`salas/${this.salaId}/tarjetas/${ultima.id}`) },
934
+ ];
935
+ }
936
+ return { racha: rachaDeRecientes(recientes, { agotada }), recientes };
937
+ }
938
+
939
+ /**
940
+ * Tope de turnos entre agentes (menciones.mjs): antes de crear una tarjeta se cuenta la racha y, si ya llegó al tope,
941
+ * no se publica nada dirigido a otro agente (paraId, menciones o respondeA a una tarjeta de otro agente) y lo demás
942
+ * pasa una vez por agente. El autor de respondeA se lee solo en el tope (de lo ya leído o con una lectura).
943
+ */
944
+ async _revisarTurnos({ destinos = [], respondeA = null } = {}) {
945
+ const { racha, recientes } = await this._rachaReciente();
946
+ if (racha.turnos < TOPE_TURNOS) return racha;
947
+ const lista = destinos.filter(Boolean);
948
+ if (respondeA) {
949
+ let autor = recientes.find((t) => t.id === respondeA)?.autorId ?? null;
950
+ if (!autor) {
951
+ autor = (await this.fs.leer(`salas/${this.salaId}/tarjetas/${respondeA}`).catch(() => null))?.autorId ?? null;
952
+ }
953
+ if (autor) lista.push(autor);
954
+ }
955
+ const dirigido = vaAOtroAgente(this.participanteId, lista);
956
+ if (pasaElTope(racha, this.participanteId, dirigido)) throw new ErrorLienzo(textoTope(racha, dirigido));
957
+ return racha;
958
+ }
959
+
960
+ /** Suma a la racha las tarjetas entregadas (en orden de creación), cada una una sola vez. */
961
+ _sumarRacha(tarjetas) {
962
+ let r = this.racha ?? { ...RACHA_VACIA, hasta: '' };
963
+ const porId = new Map(tarjetas.map((t) => [t.id, t.autorId]));
964
+ const autorDe = (id) => porId.get(id) ?? null;
965
+ for (const t of tarjetas) {
966
+ const k = claveTiempo(t.creadaEn);
967
+ if (!k || k <= r.hasta) continue;
968
+ r = { ...sumarTarjeta(r, t, autorDe), hasta: k };
969
+ }
970
+ this.racha = r;
971
+ }
972
+
973
+ async publicar({ texto, paraId = null, respondeA = null, tipo = 'mensaje', menciones = null }) {
669
974
  await this._asegurarSala();
670
975
  if (!['mensaje', 'nota'].includes(tipo)) throw new ErrorLienzo('tipo es «mensaje» o «nota» (para tareas usa lienzo_pedir_tarea).');
671
976
  texto = this._validarTexto(texto);
672
977
  this._vigilarSecretos([['el texto', texto]]);
673
978
  const para = await this._resolverParticipante(paraId);
979
+ const ids = await this._resolverMenciones(menciones);
674
980
  if (respondeA && !ID_TARJETA.test(respondeA)) throw new ErrorLienzo('respondeA tiene que ser el id de una tarjeta.');
675
- const id = await this._crearTarjeta({ tipo, texto, paraId: para, respondeA: respondeA || null });
981
+ await this._revisarTurnos({ destinos: [para, ...ids], respondeA });
982
+ const datos = { tipo, texto, paraId: para, respondeA: respondeA || null };
983
+ let sinMenciones = false;
984
+ let id;
985
+ // el campo va solo si hay menciones: una tarjeta sin ellas es igual a la de siempre (reglas viejas incluidas)
986
+ if (!ids.length) id = await this._crearTarjeta(datos);
987
+ else {
988
+ try {
989
+ id = await this._crearTarjeta({ ...datos, menciones: ids });
990
+ } catch (e) {
991
+ // Reglas anteriores a F2 (despliegue a medias): el campo se rechaza. Se publica igual, sin él, y se dice.
992
+ // Si te sacaron de la sala, el reintento también se niega y el error es el de siempre.
993
+ if (!negadoDe(e)) throw e;
994
+ id = await this._crearTarjeta(datos);
995
+ sinMenciones = true;
996
+ }
997
+ }
676
998
  await this.latido();
677
- return { id, paraId: para, link: this.linkTarjeta(id) };
999
+ return { id, paraId: para, menciones: sinMenciones ? [] : ids, sinMenciones, link: this.linkTarjeta(id) };
678
1000
  }
679
1001
 
1002
+ /**
1003
+ * lienzo_compartir_archivo. Texto UTF-8 de hasta 200 KB: en la tarjeta, como siempre (lo leen también los puentes
1004
+ * 0.2/0.3). Lo demás —imágenes, PDF, zip, audio, video, y texto de más de 200 KB— va a Storage
1005
+ * (salas/{sala}/adjuntos/{tarjeta}/{nombre}) y la tarjeta guarda solo { nombre, mime, bytes, ruta, sha256 }. El tipo
1006
+ * sale de los magic bytes, no del nombre. Orden: subir → crear la tarjeta; si la tarjeta falla, se borra lo subido.
1007
+ */
680
1008
  async compartirArchivo({ ruta, contenido, nombre, texto, paraId = null }) {
681
1009
  await this._asegurarSala();
682
1010
  if (!ruta && contenido == null) throw new ErrorLienzo('Pasa la ruta del archivo o su contenido.');
683
- let bytes;
1011
+ let buf;
684
1012
  if (ruta) {
685
1013
  let real;
686
1014
  let st;
@@ -689,33 +1017,242 @@ export class Lienzo {
689
1017
  } catch (e) {
690
1018
  throw this._traducir(e, 'compartir el archivo');
691
1019
  }
692
- if (st.size > MAX_ARCHIVO_BYTES) throw new ErrorLienzo(`El archivo pesa ${st.size} bytes; el tope es ${MAX_ARCHIVO_BYTES} (200 KB).`);
693
- const buf = readFileSync(real);
694
- if (buf.includes(0)) throw new ErrorLienzo('Solo se comparten archivos de texto (este parece binario).');
695
- try {
696
- contenido = new TextDecoder('utf-8', { fatal: true }).decode(buf);
697
- } catch {
698
- throw new ErrorLienzo('Solo se comparten archivos de texto en UTF-8.');
699
- }
1020
+ if (st.size > MAX_ADJUNTO_BYTES) throw new ErrorLienzo(`El archivo pesa ${(st.size / 1024 / 1024).toFixed(1)} MB; el tope es 25 MB (10 MB una imagen).`);
1021
+ buf = new Uint8Array(readFileSync(real));
700
1022
  nombre = nombre || basename(real);
701
- bytes = buf.length;
702
1023
  } else {
703
1024
  contenido = String(contenido);
704
- bytes = Buffer.byteLength(contenido, 'utf8');
705
1025
  if (contenido.includes('\u0000')) throw new ErrorLienzo('Solo se comparte texto.');
1026
+ buf = new Uint8Array(Buffer.from(contenido, 'utf8'));
706
1027
  }
707
1028
  nombre = String(nombre || '').trim();
708
1029
  if (!nombre) throw new ErrorLienzo('Falta el nombre del archivo.');
709
1030
  if (nombre.length > 200) throw new ErrorLienzo('El nombre del archivo admite hasta 200 caracteres.');
710
1031
  try { revisarNombre(limpiarLinea(nombre, 200)); } catch (e) { throw this._traducir(e, 'compartir el archivo'); }
711
- if (bytes > MAX_ARCHIVO_BYTES) throw new ErrorLienzo(`El contenido pesa ${bytes} bytes; el tope es ${MAX_ARCHIVO_BYTES} (200 KB).`);
712
- texto = texto ? this._validarTexto(texto) : `Archivo: ${nombre}`;
713
- this._vigilarSecretos([['el archivo', contenido], ['el texto', texto], ['el nombre del archivo', nombre]]);
1032
+
1033
+ let tipo;
1034
+ try { tipo = detectarTipo(buf, nombre); } catch (e) { throw this._traducir(e, 'compartir el archivo'); }
1035
+ const bytes = buf.length;
1036
+ const enTarjeta = tipo.texto != null && bytes <= MAX_ARCHIVO_BYTES;
1037
+ // Sin texto, la tarjeta dice qué es; un adjunto de Storage dice además dónde se ve (una web o un puente de antes de
1038
+ // 0.4.0 lo mostrarían como un archivo vacío).
1039
+ texto = texto
1040
+ ? this._validarTexto(texto)
1041
+ : enTarjeta ? `Archivo: ${nombre}` : `Archivo: ${nombre} (${nombreDeTipo(tipo.mime)}, ${pesoLegible(bytes)}). Se ve en la web; un agente lo baja con lienzo_descargar (puente 0.4.0 o posterior).`;
1042
+ // cero secretos: el texto del archivo (también SVG y JSON), el comentario y el nombre
1043
+ this._vigilarSecretos([['el archivo', tipo.texto], ['el texto', texto], ['el nombre del archivo', nombre]]);
1044
+ // un zip no se abre, pero su índice sí se lee: .env, llaves o un .git/ adentro no salen
1045
+ if (tipo.mime === 'application/zip') {
1046
+ try { revisarZip(buf); } catch (e) { throw this._traducir(e, 'compartir el archivo'); }
1047
+ }
714
1048
  const para = await this._resolverParticipante(paraId);
715
- const mime = MIME[extname(nombre).toLowerCase()] || 'text/plain';
716
- const id = await this._crearTarjeta({ tipo: 'archivo', texto, paraId: para, archivo: { nombre, mime, bytes, contenido } });
1049
+ // el tope de turnos se mira antes de subir nada a Storage (si no, quedaría un objeto sin tarjeta)
1050
+ await this._revisarTurnos({ destinos: [para] });
1051
+
1052
+ // texto chico (también un SVG, como en 0.3: la tarjeta lo muestra como código, nunca lo dibuja): en la tarjeta
1053
+ if (enTarjeta) {
1054
+ const mime = MIME[extname(nombre).toLowerCase()] || 'text/plain';
1055
+ const id = await this._crearTarjeta({ tipo: 'archivo', texto, paraId: para, archivo: { nombre, mime, bytes, contenido: tipo.texto } });
1056
+ await this.latido();
1057
+ return { id, nombre, bytes, mime, link: this.linkTarjeta(id) };
1058
+ }
1059
+
1060
+ const mime = mimeParaStorage(tipo.mime);
1061
+ try { revisarTamano(bytes, mime); } catch (e) { throw this._traducir(e, 'compartir el archivo'); }
1062
+ const id = nuevoId();
1063
+ const objeto = nombreEnStorage(nombre);
1064
+ const rutaObjeto = `salas/${this.salaId}/adjuntos/${id}/${objeto}`;
1065
+ const hash = sha256(buf);
1066
+ await this._asegurarAcceso();
1067
+ const subir = () => this.storage.subir(rutaObjeto, buf, {
1068
+ contentType: mime, contentDisposition: disposicion(objeto), metadatos: { autorId: this.participanteId, sha256: hash },
1069
+ });
1070
+ try {
1071
+ await subir().catch(async (e) => {
1072
+ // tu acceso pudo borrarse (saliste desde la web, o te uniste de nuevo): se reescribe una vez
1073
+ if (e instanceof ErrorStorage && e.negado) { await this._asegurarAcceso({ forzar: true }); return subir(); }
1074
+ throw e;
1075
+ });
1076
+ } catch (e) {
1077
+ // un corte o un timeout pudo dejar el objeto guardado igual: sin tarjeta no sirve (si no está, da 404 y listo)
1078
+ if (!(e instanceof ErrorStorage && e.negado)) await this.storage.borrar(rutaObjeto).catch(() => {});
1079
+ throw this._traducir(e, 'subir el archivo');
1080
+ }
1081
+ try {
1082
+ await this._crearTarjeta({ tipo: 'archivo', texto, paraId: para, archivo: { nombre, mime, bytes, ruta: rutaObjeto, sha256: hash } }, id);
1083
+ } catch (e) {
1084
+ // ¿La tarjeta se creó y solo se perdió la respuesta (timeout, corte)? Entonces no se borra el archivo: quedaría
1085
+ // una tarjeta que apunta a un 404 para siempre (un adjunto de Storage no se puede cambiar). Solo un rechazo
1086
+ // definitivo de las reglas o del contenido (4xx) asegura que no existe.
1087
+ const f = e instanceof ErrorFirestore ? e : e?.cause instanceof ErrorFirestore ? e.cause : null;
1088
+ // 409 (ya existe: un reintento de un commit que sí se aplicó), 408 y 429 no prueban nada: se mira la tarjeta
1089
+ const definitivo = Boolean(f) && f.status >= 400 && f.status < 500 && ![408, 409, 429].includes(f.status);
1090
+ const existe = definitivo ? false : await this.fs.leer(`salas/${this.salaId}/tarjetas/${id}`).then(Boolean, () => null);
1091
+ if (existe === true) {
1092
+ await this.latido();
1093
+ return { id, nombre, bytes, mime, sha256: hash, enStorage: true, link: this.linkTarjeta(id) };
1094
+ }
1095
+ // sin tarjeta, el archivo no sirve: se limpia (si ni siquiera se pudo leer, lo barre scripts/purgar-huerfanos.mjs)
1096
+ if (existe === false) await this.storage.borrar(rutaObjeto).catch(() => {});
1097
+ throw e;
1098
+ }
1099
+ await this.latido();
1100
+ return { id, nombre, bytes, mime, sha256: hash, enStorage: true, link: this.linkTarjeta(id) };
1101
+ }
1102
+
1103
+ /**
1104
+ * accesos/{uid} de la sala: lo que storage.rules mira (con la sala) para dejar subir y bajar adjuntos. Se escribe
1105
+ * una vez por sala y proceso; las reglas solo lo dejan escribir a un miembro sin expulsar.
1106
+ */
1107
+ async _asegurarAcceso({ forzar = false } = {}) {
1108
+ if (!forzar && this.accesoEn === this.salaId) return;
1109
+ try {
1110
+ await this.fs.escribir(`salas/${this.salaId}/accesos/${this.uid}`, {}, { horaServidor: ['en'] });
1111
+ } catch (e) {
1112
+ throw this._traducir(e, 'abrir los adjuntos de la sala');
1113
+ }
1114
+ this.accesoEn = this.salaId;
1115
+ }
1116
+
1117
+ /**
1118
+ * Baja el adjunto de Storage de una tarjeta y comprueba tamaño, sha256 y que los bytes sean del tipo que dice la
1119
+ * tarjeta (el sha256 lo eligió el autor: prueba que llegó entero, no que sea lo que dice). Devuelve los bytes.
1120
+ * Los errores nombran la tarjeta por su id, nunca por el nombre que eligió otro participante.
1121
+ */
1122
+ async _bajarAdjunto(t) {
1123
+ const a = t.archivo;
1124
+ const cual = `el archivo de la tarjeta ${limpiarLinea(t.id, 64)}`;
1125
+ await this._asegurarAcceso();
1126
+ const bajar = () => this.storage.bajar(a.ruta, { maxBytes: Number(a.bytes) || 0 });
1127
+ let buf;
1128
+ try {
1129
+ buf = await bajar().catch(async (e) => {
1130
+ // si tu acceso se borró (saliste desde la web y volviste), se reescribe una vez
1131
+ if (e instanceof ErrorStorage && e.negado) { await this._asegurarAcceso({ forzar: true }); return bajar(); }
1132
+ throw e;
1133
+ });
1134
+ } catch (e) {
1135
+ throw this._traducir(e, `bajar ${cual}`);
1136
+ }
1137
+ if (buf.length !== Number(a.bytes)) {
1138
+ throw new ErrorLienzo(`${cual} pesa ${buf.length} bytes y la tarjeta dice ${a.bytes}: no se guardó.`);
1139
+ }
1140
+ if (sha256(buf) !== a.sha256) {
1141
+ throw new ErrorLienzo(`El sha256 de ${cual} no coincide con el de la tarjeta: no se guardó (pudo cambiar en el camino).`);
1142
+ }
1143
+ try {
1144
+ revisarBajado(buf, String(a.mime));
1145
+ } catch (e) {
1146
+ throw new ErrorLienzo(`Revisé ${cual}: ${e.message}`);
1147
+ }
1148
+ return buf;
1149
+ }
1150
+
1151
+ async _tarjetaConArchivo(id) {
1152
+ await this._asegurarSala();
1153
+ id = String(id ?? '').trim();
1154
+ if (!ID_TARJETA.test(id)) throw new ErrorLienzo('id tiene que ser el id de una tarjeta (letras, números, - y _, hasta 64).');
1155
+ const t = await this.fs.leer(`salas/${this.salaId}/tarjetas/${id}`).catch((e) => { throw this._traducir(e, 'leer esa tarjeta'); });
1156
+ if (!t) throw new ErrorLienzo(`No existe la tarjeta ${id}.`);
1157
+ if (!t.archivo) throw new ErrorLienzo(`La tarjeta ${id} no tiene archivo.`);
1158
+ return t;
1159
+ }
1160
+
1161
+ /**
1162
+ * lienzo_descargar: baja el archivo de una tarjeta a <LIENZO_HOME>/descargas/<sala>/ (y a ningún otro lado),
1163
+ * verifica tamaño, sha256 y tipo real, y lo guarda sin permiso de ejecución, con la extensión de su tipo real y (en
1164
+ * macOS) en cuarentena, para que Gatekeeper pregunte antes de abrirlo. Nunca lo abre ni lo ejecuta.
1165
+ * Devuelve { ruta, nombre, mime, bytes, sha256, verificado, imagen?, imagenOmitida?, autorId, id }.
1166
+ */
1167
+ async descargar({ id, destino } = {}) {
1168
+ const t = await this._tarjetaConArchivo(id);
717
1169
  await this.latido();
718
- return { id, nombre, bytes, mime, link: this.linkTarjeta(id) };
1170
+ const a = t.archivo;
1171
+ const enStorage = Boolean(a.ruta);
1172
+ let buf;
1173
+ if (enStorage) buf = await this._bajarAdjunto(t);
1174
+ else buf = new Uint8Array(Buffer.from(String(a.contenido ?? ''), 'utf8'));
1175
+ // el texto de la tarjeta se guarda como texto: su extensión solo se conserva si nada la ejecuta con doble clic
1176
+ const mime = enStorage ? String(a.mime) : 'text/plain';
1177
+ let nombreArchivo;
1178
+ try { nombreArchivo = nombreDescarga({ id: t.id, nombre: a.nombre, destino, mime }); } catch (e) { throw this._traducir(e, 'descargar'); }
1179
+ const carpeta = carpetaDescargas(this.config.home, this.salaId);
1180
+ mkdirSync(carpeta, { recursive: true, mode: 0o700 });
1181
+ const parcial = join(carpeta, `.${randomBytes(6).toString('hex')}.parcial`);
1182
+ let final;
1183
+ try {
1184
+ writeFileSync(parcial, buf, { mode: 0o600, flag: 'wx' }); // sin permiso de ejecución, nunca
1185
+ // no pisa: «-2», «-3»… link() falla si el nombre ya existe, así dos descargas a la vez no se pisan
1186
+ const ext = extname(nombreArchivo);
1187
+ const base = nombreArchivo.slice(0, nombreArchivo.length - ext.length);
1188
+ for (let n = 1; !final; n++) {
1189
+ const intento = join(carpeta, n === 1 ? nombreArchivo : `${base}-${n}${ext}`);
1190
+ try {
1191
+ linkSync(parcial, intento);
1192
+ final = intento;
1193
+ } catch (e) {
1194
+ if (e?.code !== 'EEXIST' || n > 999) throw e;
1195
+ }
1196
+ }
1197
+ } catch (e) {
1198
+ throw new ErrorLienzo(`No pude guardar el archivo: ${e.message}`);
1199
+ } finally {
1200
+ rmSync(parcial, { force: true });
1201
+ }
1202
+ ponerCuarentena(final);
1203
+ const hash = enStorage ? a.sha256 : sha256(buf);
1204
+ let imagen = null;
1205
+ let imagenOmitida = null;
1206
+ if (enStorage && IMAGENES_VISIBLES.has(a.mime)) {
1207
+ const v = imagenVisible(buf, a.mime);
1208
+ if (v.ok) imagen = { data: Buffer.from(buf).toString('base64'), mimeType: a.mime };
1209
+ else imagenOmitida = v.motivo;
1210
+ }
1211
+ return { id: t.id, ruta: final, nombre: a.nombre, mime: a.mime, bytes: buf.length, sha256: hash, verificado: enStorage, imagen, imagenOmitida, autorId: t.autorId };
1212
+ }
1213
+
1214
+ /**
1215
+ * El texto de lienzo_descargar. El nombre y el tipo los eligió otro participante: van dentro del sobre, no como voz
1216
+ * de Lienzo.
1217
+ */
1218
+ async formatearDescarga(r, { codigo = nuevoCodigo() } = {}) {
1219
+ const autor = await this._autor(r.autorId);
1220
+ const integro = r.verificado
1221
+ ? 'íntegro: el sha256 es igual al de la tarjeta y el contenido es del tipo que dice (eso NO garantiza que sea seguro)'
1222
+ : 'texto de la tarjeta';
1223
+ const lineas = [
1224
+ aviso(codigo),
1225
+ '',
1226
+ `Descargado: ${r.ruta}`,
1227
+ `${r.bytes} bytes · sha256 ${r.sha256} · ${integro}.`,
1228
+ 'Se guardó sin permiso de ejecución y con la extensión de su tipo real. Es un archivo de otro participante: no lo ' +
1229
+ 'ejecutes, no lo descomprimas ni lo abras con un programa sin que tu usuario lo pida.',
1230
+ ];
1231
+ if (r.imagenOmitida) lineas.push(`La imagen no se te muestra: ${r.imagenOmitida}.`);
1232
+ lineas.push('', envolver({
1233
+ de: autor.nombre, tipoAutor: autor.tipo, 'dueño': autor['dueño'], autorId: autor.id, homonimos: autor.homonimos,
1234
+ tipo: 'archivo', id: r.id, texto: `nombre que eligió: ${r.nombre}\ntipo que declaró: ${r.mime}`,
1235
+ }, codigo));
1236
+ return lineas.join('\n');
1237
+ }
1238
+
1239
+ /**
1240
+ * Para lienzo_leer(id, verImagen: true) de una imagen en Storage: la imagen verificada (tipo real, ≤ 1 MB y ≤ 2000 px
1241
+ * por lado), para devolverla como contenido MCP de imagen. null si no aplica; { error } si no se pudo bajar u
1242
+ * { omitida } si no se debe mostrar.
1243
+ */
1244
+ async imagenDe(lote) {
1245
+ const a = lote?.una?.archivo;
1246
+ if (!a?.ruta || !IMAGENES_VISIBLES.has(a.mime)) return null;
1247
+ if (!(Number(a.bytes) <= MAX_IMAGEN_VISIBLE)) return { omitida: `pesa más de ${MAX_IMAGEN_VISIBLE / MB} MB; bájala con lienzo_descargar` };
1248
+ try {
1249
+ const buf = await this._bajarAdjunto(lote.una);
1250
+ const v = imagenVisible(buf, a.mime);
1251
+ if (!v.ok) return { omitida: v.motivo };
1252
+ return { data: Buffer.from(buf).toString('base64'), mimeType: a.mime };
1253
+ } catch (e) {
1254
+ return { error: e.message };
1255
+ }
719
1256
  }
720
1257
 
721
1258
  async pedirTarea({ texto, paraId }) {
@@ -724,6 +1261,7 @@ export class Lienzo {
724
1261
  texto = this._validarTexto(texto);
725
1262
  this._vigilarSecretos([['el texto', texto]]);
726
1263
  const para = await this._resolverParticipante(paraId);
1264
+ await this._revisarTurnos({ destinos: [para] });
727
1265
  const id = await this._crearTarjeta({ tipo: 'tarea', texto, paraId: para, estado: 'abierta' });
728
1266
  this.tareasPedidas.set(id, { estado: 'abierta', respuesta: null, tomadaPor: null });
729
1267
  await this.latido();
@@ -754,6 +1292,7 @@ export class Lienzo {
754
1292
  if (!paraId) throw new ErrorLienzo('Una lista necesita destinatario (paraId): mira lienzo_participantes.');
755
1293
  this._vigilarSecretos([['el título', titulo], ['el contexto', contexto], ...puntos.map((p, i) => [`el punto ${i + 1}`, p])]);
756
1294
  const para = await this._resolverParticipante(paraId);
1295
+ await this._revisarTurnos({ destinos: [para] });
757
1296
 
758
1297
  const id = nuevoId();
759
1298
  const ids = puntos.map((_, i) => idPunto(id, i + 1));
@@ -1000,9 +1539,12 @@ export class Lienzo {
1000
1539
  marcaDesde = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?Z$/.test(marcaDesde) ? marcaDesde : d.toISOString();
1001
1540
  }
1002
1541
  const tarjetas = await this._tarjetasDesde(marcaDesde);
1542
+ // las últimas de la sala: la racha de turnos se cuenta de nuevo sobre ellas (con un hueco no se arrastra la vieja)
1543
+ if (!marcaDesde) this.racha = { ...RACHA_VACIA, hasta: '' };
1003
1544
  const editadas = await this._editadasDesde(marcaDesde);
1004
1545
  const cambios = await this._cambiosEnMisTareas();
1005
- return { tarjetas, editadas, cambios, desde: marcaDesde, lleno: Boolean(tarjetas.lleno) };
1546
+ // sin desde son las últimas de la sala: la línea del tope de turnos vale (con desde pueden ser viejas)
1547
+ return { tarjetas, editadas, cambios, desde: marcaDesde, lleno: Boolean(tarjetas.lleno), conTope: !marcaDesde };
1006
1548
  }
1007
1549
 
1008
1550
  /** Para lienzo_esperar: tarjetas y ediciones ajenas y cambios en tus tareas; null si no hay nada ajeno. */
@@ -1048,13 +1590,22 @@ export class Lienzo {
1048
1590
  const id = limpiarLinea(t.id, 64);
1049
1591
  const para = t.paraId ? await this._describir(t.paraId) : null;
1050
1592
  const tomador = t.tomadaPor ? await this._describir(t.tomadaPor) : null;
1593
+ // menciones: quién (nombres elegidos por otros → siempre dentro del sobre) y si te nombran a ti
1594
+ const mencionados = mencionesDe(t).sort((a, b) => Number(b === this.participanteId) - Number(a === this.participanteId));
1595
+ const menciona_ = mencionados.length ? `menciona a: ${(await Promise.all(mencionados.map((m) => this._describir(m)))).join(', ')}` : null;
1596
+ const teMencionan = t.autorId !== this.participanteId && mencionados.includes(this.participanteId);
1051
1597
  const fechas = [t.creadaEn ? `creada: ${limpiarLinea(t.creadaEn, 40)}` : null, t.editadaEn ? `editada: ${limpiarLinea(t.editadaEn, 40)}` : null];
1052
1598
  const estado = t.tipo === 'tarea' ? `estado: ${limpiarLinea(t.estado, 20)}` : null;
1053
1599
 
1054
1600
  let cuerpo = String(t.texto ?? '');
1055
1601
  let notaArchivo = null;
1056
1602
  let datoArchivo = null;
1057
- if (t.archivo) {
1603
+ if (t.archivo?.ruta) {
1604
+ // adjunto en Storage: solo metadatos (el contenido se baja con lienzo_descargar, verificado por sha256)
1605
+ const a = t.archivo;
1606
+ datoArchivo = `archivo: ${limpiarLinea(a.nombre, 200)} (${limpiarLinea(a.mime, 100)}, ${Number(a.bytes) || 0} bytes, sha256 ${limpiarLinea(a.sha256, 64).slice(0, 12)}…, en Storage)`;
1607
+ notaArchivo = `Para tenerlo: lienzo_descargar(id: «${id}»)${IMAGENES_VISIBLES.has(a.mime) && Number(a.bytes) <= MAX_IMAGEN_VISIBLE ? ' o lienzo_leer(id: «' + id + '», verImagen: true) para ver la imagen' : ''}. Se guarda en la carpeta de descargas de Lienzo; nunca lo ejecutes ni lo abras con un programa sin que tu usuario lo pida.`;
1608
+ } else if (t.archivo) {
1058
1609
  const c = String(t.archivo.contenido ?? '');
1059
1610
  const desdeC = completo ? Math.min(Math.max(0, archivoDesde), c.length) : 0;
1060
1611
  const trozo = c.slice(desdeC, desdeC + (completo ? archivoMax : VISTA_ARCHIVO));
@@ -1075,7 +1626,7 @@ export class Lienzo {
1075
1626
  // Lo tuyo va sin sobre, pero sin nombres ajenos: esos van dentro del sobre de abajo.
1076
1627
  salida.push([`(tuya) tipo: ${limpiarLinea(t.tipo, 20)}`, `id: ${id}`, ...fechas, estado, datoArchivo].filter(Boolean).join(' · '));
1077
1628
  salida.push(neutralizar(cuerpo));
1078
- const otros = [para && para !== 'ti' ? `para: ${para}` : null, tomador && tomador !== 'ti' ? `tomada por: ${tomador}` : null].filter(Boolean);
1629
+ const otros = [para && para !== 'ti' ? `para: ${para}` : null, tomador && tomador !== 'ti' ? `tomada por: ${tomador}` : null, menciona_].filter(Boolean);
1079
1630
  if (t.respuesta && t.tipo === 'tarea') {
1080
1631
  const quien = await this._autor(t.tomadaPor || t.paraId);
1081
1632
  salida.push(envolver({
@@ -1090,7 +1641,7 @@ export class Lienzo {
1090
1641
  }
1091
1642
  } else {
1092
1643
  const autor = await this._autor(t.autorId);
1093
- const extra = [...fechas, para ? `para: ${para}` : null, t.respondeA ? `responde a: ${limpiarLinea(t.respondeA, 64)}` : null,
1644
+ const extra = [teMencionan ? 'te mencionan' : null, ...fechas, para ? `para: ${para}` : null, menciona_, t.respondeA ? `responde a: ${limpiarLinea(t.respondeA, 64)}` : null,
1094
1645
  estado ? `${estado}${tomador ? ` (tomada por ${tomador})` : ''}` : null, datoArchivo].filter(Boolean);
1095
1646
  salida.push(envolver({
1096
1647
  de: autor.nombre, tipoAutor: autor.tipo, 'dueño': autor['dueño'], autorId: t.autorId, homonimos: autor.homonimos,
@@ -1266,7 +1817,8 @@ export class Lienzo {
1266
1817
  const visibles = piezas.filter((p) => !(soloAjenas && p.t.autorId === this.participanteId));
1267
1818
  return {
1268
1819
  seccion: 'tarjetas', piezas: visibles.length, silencioso: !visibles.length,
1269
- entregar: () => { for (const t of grupo) this._avanzarMarca(t.creadaEn); },
1820
+ mias: visibles.filter((p) => this._meMencionan(p.t)).map((p) => p.t.id),
1821
+ entregar: () => { for (const t of grupo) this._avanzarMarca(t.creadaEn); this._sumarRacha(grupo); },
1270
1822
  ultimaCreada: grupo[grupo.length - 1].creadaEn,
1271
1823
  texto: async (codigo, max) => (await Promise.all(visibles.map((p) => (p.puntos
1272
1824
  ? this._formatearLista(p.t, [...p.puntos].sort((a, b) => (a.id < b.id ? -1 : 1)), { codigo, max })
@@ -1300,6 +1852,7 @@ export class Lienzo {
1300
1852
  ...editadas.map((t) => ({
1301
1853
  seccion: 'editadas', piezas: 1, entregar: () => this._avanzarEdicion(t.editadaEn),
1302
1854
  texto: (codigo) => this.formatearTarjeta(t, { codigo, editada: true }),
1855
+ mias: this._meMencionan(t) ? [t.id] : [],
1303
1856
  // la edición de una tarjeta que llega nueva en esta misma respuesta ya viene con el texto al día
1304
1857
  silencioso: nuevas.has(t.id) || (soloAjenas && t.autorId === this.participanteId),
1305
1858
  })),
@@ -1313,6 +1866,7 @@ export class Lienzo {
1313
1866
  let entregados = 0;
1314
1867
  let cortado = false;
1315
1868
  let ultimaCreada = null;
1869
+ const mencionan = []; // ids de lo entregado que te menciona
1316
1870
  for (const it of items) {
1317
1871
  if (cortado) { if (!it.silencioso) quedan[it.seccion] += it.piezas; continue; }
1318
1872
  if (it.silencioso) { it.entregar(); if (it.ultimaCreada) ultimaCreada = it.ultimaCreada; continue; }
@@ -1323,11 +1877,20 @@ export class Lienzo {
1323
1877
  usado += txt.length + 2;
1324
1878
  entregados++;
1325
1879
  it.entregar();
1880
+ for (const m of it.mias ?? []) if (!mencionan.includes(m)) mencionan.push(m);
1326
1881
  if (it.ultimaCreada) ultimaCreada = it.ultimaCreada;
1327
1882
  }
1328
1883
 
1329
1884
  const partes = [];
1330
1885
  if (entregados) partes.push(cabeza);
1886
+ // lo pone el puente (fuera de los sobres): a quién le toca contestar y si la sala llegó al tope de turnos
1887
+ if (mencionan.length && !sinPie) {
1888
+ // sin ids: el id de una tarjeta lo puede elegir quien la escribe por REST, y fuera del sobre solo va lo del puente
1889
+ partes.push(`Te mencionan en ${mencionan.length === 1 ? '1 tarjeta' : `${mencionan.length} tarjetas`} (dice «te mencionan» ` +
1890
+ 'en su sobre). Contesta en la sala solo lo que te menciona o es para ti, y solo si tu usuario te lo pidió.');
1891
+ }
1892
+ const tope = (soloAjenas || lote.conTope) && entregados ? lineaTope(this.racha) : null;
1893
+ if (tope) partes.push(tope);
1331
1894
  if (bloques.cambios.length) partes.push(`Cambios en tareas que pediste (${piezas.cambios}):`, ...bloques.cambios);
1332
1895
  if (bloques.editadas.length) partes.push(`Tarjetas editadas (${piezas.editadas}):`, ...bloques.editadas);
1333
1896
  if (bloques.tarjetas.length) partes.push(`Tarjetas (${piezas.tarjetas}):`, ...bloques.tarjetas);
@@ -1351,3 +1914,25 @@ export class Lienzo {
1351
1914
  return { texto: partes.join('\n\n'), recortado };
1352
1915
  }
1353
1916
  }
1917
+
1918
+ /** «1,2 MB», «340 KB», «900 bytes»: para el texto por defecto de una tarjeta con adjunto. */
1919
+ function pesoLegible(n) {
1920
+ if (n >= MB) return `${(n / MB).toLocaleString('es-PE', { maximumFractionDigits: 1 })} MB`;
1921
+ if (n >= 1024) return `${Math.round(n / 1024)} KB`;
1922
+ return `${n} bytes`;
1923
+ }
1924
+
1925
+ /**
1926
+ * macOS: el atributo de cuarentena hace que Gatekeeper pregunte antes de abrir lo descargado (un .terminal o un .app
1927
+ * de otro participante no corre con un doble clic). Node no lo pone solo. En otros sistemas, nada.
1928
+ */
1929
+ export function ponerCuarentena(ruta, { plataforma = process.platform } = {}) {
1930
+ if (plataforma !== 'darwin') return false;
1931
+ try {
1932
+ const segundos = Math.floor(Date.now() / 1000).toString(16);
1933
+ execFileSync('/usr/bin/xattr', ['-w', 'com.apple.quarantine', `0081;${segundos};Lienzo;`, ruta], { stdio: 'ignore', timeout: 5000 });
1934
+ return true;
1935
+ } catch {
1936
+ return false;
1937
+ }
1938
+ }