@dotrino/remote-agent 0.31.0 → 0.32.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/remote-agent",
3
- "version": "0.31.0",
3
+ "version": "0.32.1",
4
4
  "description": "Middleware compartido del ecosistema Dotrino para apps de agente remoto cifrado punto a punto (handshake E2E, emparejamiento SAS con vault, revocación, descubrimiento). Lo consumen dotrino-terminal (PTY) y dotrino-ia (chat con IA).",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -49,12 +49,12 @@
49
49
  ],
50
50
  "peerDependencies": {
51
51
  "@dotrino/identity": ">=0.121.7",
52
- "@dotrino/proxy-client": ">=0.36.1",
52
+ "@dotrino/proxy-client": ">=0.46.0",
53
53
  "@dotrino/vault": ">=0.115.0"
54
54
  },
55
55
  "devDependencies": {
56
56
  "@dotrino/identity": "0.135.0",
57
- "@dotrino/proxy-client": "0.45.3",
57
+ "@dotrino/proxy-client": "0.46.0",
58
58
  "@dotrino/vault": "0.115.0"
59
59
  }
60
60
  }
package/src/agent.js CHANGED
@@ -332,9 +332,38 @@ export async function startRemoteAgent (opts = {}) {
332
332
  link.actaTrusted = true
333
333
  try { saveLink(dir, link) } catch (_) {}
334
334
  evictOutsiders()
335
+ shareActa(acta)
335
336
  return true
336
337
  }
337
338
 
339
+ /**
340
+ * LA CACHÉ DE ACTAS DEL PROXIO (websocket-proxy ≥ 1.10.0). Quien adopta un acta nueva se la
341
+ * deja al proxio, así el siguiente aparato de la cuenta se entera sin despertar a la bóveda;
342
+ * y antes de preguntarle a la bóveda se mira ahí. Lo que llega del proxio pasa por
343
+ * `recordActa`, o sea que se adopta solo si encadena desde la de confianza: el proxio no
344
+ * puede colar nada, como mucho no tener la más nueva.
345
+ */
346
+ const actaCacheProblems = new Set()
347
+ function noteActaCache (what, e) {
348
+ const code = e?.code || 'error'
349
+ if (actaCacheProblems.has(what + code)) return
350
+ actaCacheProblems.add(what + code)
351
+ if (!opts.quiet) console.error(`[remote-agent] record cache on the proxy (${what}): ${code} · ${e?.message || ''}`)
352
+ }
353
+ function shareActa (acta) {
354
+ // Por detrás y sin tumbar a quien adopta: si falla, se dice una vez y sigue.
355
+ Promise.resolve().then(() => client.putActa(acta)).catch((e) => noteActaCache('put', e))
356
+ }
357
+ /** Adopta la más nueva del proxio que encadene. `true` si el acta avanzó. */
358
+ async function actaFromProxy () {
359
+ const profileId = trustedActa()?.profileId
360
+ if (!profileId) return false
361
+ let list
362
+ try { list = await client.getActas(profileId, { sinceSeq: link.actaSeq ?? -1 }) } catch (e) { noteActaCache('get', e); return false }
363
+ for (const a of list) if (await recordActa(a)) return true
364
+ return false
365
+ }
366
+
338
367
  /**
339
368
  * CIERRA LAS SESIONES DE QUIEN YA NO PUEDE ESTAR: el acta de confianza ya no lo nombra (o
340
369
  * le quitó `sign`), o su papel se retiró. Se juzgaba solo al saludar, así que un aparato
@@ -366,7 +395,8 @@ export async function startRemoteAgent (opts = {}) {
366
395
 
367
396
  const vaultTick = async () => { await refreshRevocations(); await renewCertIfNeeded() }
368
397
  /** Trae el acta ahora (con freno) y dice si AVANZÓ: solo entonces merece juzgar otra vez. */
369
- const catchUp = makeCatchUp({ refresh: vaultTick, seq: () => link.actaSeq })
398
+ // Ponerse al día mira primero la caché del proxio y, si no trae nada que encadene, la bóveda.
399
+ const catchUp = makeCatchUp({ refresh: async () => { if (!await actaFromProxy()) await vaultTick() }, seq: () => link.actaSeq })
370
400
  vaultTick()
371
401
  const revTimer = setInterval(vaultTick, REVOKE_REFRESH_MS); revTimer.unref?.()
372
402
 
package/src/client.js CHANGED
@@ -21,7 +21,7 @@
21
21
  * `on('message', cb)` recibe los payloads del agente, ya descifrados.
22
22
  */
23
23
  import { verifyChain } from '@dotrino/identity/capabilities'
24
- import { sealersOf, samePubkey } from '@dotrino/identity/acta'
24
+ import { sealersOf, samePubkey, canAdopt } from '@dotrino/identity/acta'
25
25
  import { makeEphemeral, deriveKey, seal, open } from '../e2e.js'
26
26
  import { canGreet } from './membership.js'
27
27
  import { HS, ACK, DATA, PING, PONG, ERROR, DEFAULT_PROXY } from '../protocol.js'
@@ -207,10 +207,28 @@ export class RemoteAgentClient {
207
207
  try { local = this.trustedActa?.() || null } catch (_) {}
208
208
  if (local) {
209
209
  try { return await this._judgeAck(res, eph, local) } catch (e) { if (e.code !== 'unauthorized') throw e }
210
+ // Va atrás: antes que la bóveda, la caché de actas del proxio (websocket-proxy ≥ 1.10.0).
211
+ const newer = await this._actaFromProxy(local)
212
+ if (newer) {
213
+ try { return await this._judgeAck(res, eph, newer) } catch (e) { if (e.code !== 'unauthorized') throw e }
214
+ }
210
215
  }
211
216
  return this._judgeAck(res, eph, await this._acta())
212
217
  }
213
218
 
219
+ /**
220
+ * La más nueva de la caché del proxio que ENCADENE desde `trusted` (`canAdopt`), o `null`.
221
+ * El proxio no es de fiar: lo que no encadena se ignora. Si no se pudo preguntar, el motivo
222
+ * queda en `actaCacheError` y se sigue con la bóveda, que es quien manda.
223
+ */
224
+ async _actaFromProxy (trusted) {
225
+ if (!trusted?.profileId) return null
226
+ let list
227
+ try { list = await this.client.getActas(trusted.profileId, { sinceSeq: trusted.seq }) } catch (e) { this.actaCacheError = e; return null }
228
+ for (const a of list) if ((await canAdopt({ candidate: a, current: trusted })).adopt) return a
229
+ return null
230
+ }
231
+
214
232
  /** Juzga el ack con `acta` y, si pasa, deja `sid` y `key`. */
215
233
  async _judgeAck (res, eph, acta) {
216
234