From 42b663072bd5adc842f5358b2b52dfeb8311f8e5 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Tue, 13 Oct 2026 13:49:10 +0530 Subject: [PATCH 1/4] fix: serialize and recover consolidation publication --- README.md | 2 +- READMEs/README.ar-SA.md | 2 +- READMEs/README.bn-BD.md | 2 +- READMEs/README.cs-CZ.md | 2 +- READMEs/README.da-DK.md | 2 +- READMEs/README.de-DE.md | 2 +- READMEs/README.el-GR.md | 2 +- READMEs/README.es-ES.md | 2 +- READMEs/README.fi-FI.md | 2 +- READMEs/README.fr-FR.md | 2 +- READMEs/README.he-IL.md | 2 +- READMEs/README.hi-IN.md | 2 +- READMEs/README.hu-HU.md | 2 +- READMEs/README.id-ID.md | 2 +- READMEs/README.it-IT.md | 2 +- READMEs/README.ja-JP.md | 2 +- READMEs/README.ko-KR.md | 2 +- READMEs/README.nb-NO.md | 2 +- READMEs/README.nl-NL.md | 2 +- READMEs/README.pl-PL.md | 2 +- READMEs/README.pt-BR.md | 2 +- READMEs/README.pt-PT.md | 2 +- READMEs/README.ro-RO.md | 2 +- READMEs/README.ru-RU.md | 2 +- READMEs/README.sv-SE.md | 2 +- READMEs/README.th-TH.md | 2 +- READMEs/README.tl-PH.md | 2 +- READMEs/README.tr-TR.md | 2 +- READMEs/README.uk-UA.md | 2 +- READMEs/README.ur-PK.md | 2 +- READMEs/README.vi-VN.md | 2 +- READMEs/README.zh-CN.md | 2 +- READMEs/README.zh-TW.md | 2 +- scripts/docs-sync.state.json | 2 +- src/functions/consolidate.ts | 150 +++++++------ test/consolidate-project-scope.test.ts | 2 +- test/consolidate-publication.test.ts | 286 +++++++++++++++++++++++++ 37 files changed, 410 insertions(+), 96 deletions(-) create mode 100644 test/consolidate-publication.test.ts diff --git a/README.md b/README.md index 748cb5a598..49b285db6c 100644 --- a/README.md +++ b/README.md @@ -1435,7 +1435,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Custom plugin systems | `iii worker add ` | -**230 source files · ~54,000 LOC · 3,200+ tests · 312 functions · 62 KV scopes**, all on three primitives. No `agentmemory plugin install`. The plugin system is iii itself. +**230 source files · ~55,000 LOC · 3,200+ tests · 312 functions · 62 KV scopes**, all on three primitives. No `agentmemory plugin install`. The plugin system is iii itself. --- diff --git a/READMEs/README.ar-SA.md b/READMEs/README.ar-SA.md index 4d6ceb35c8..2e44223c70 100644 --- a/READMEs/README.ar-SA.md +++ b/READMEs/README.ar-SA.md @@ -1439,7 +1439,7 @@ fi | Prometheus / Grafana | OTEL في iii + مراقب الحالة الصحية | | أنظمة إضافات مخصصة | `iii worker add ` | -**230 ملف مصدري · ~54,000 سطر برمجي · 3,200+ اختبار · 312 دالة · 62 نطاق KV**، كل ذلك على ثلاث بنى أساسية. لا يوجد أمر `agentmemory plugin install`. نظام الإضافات هو iii نفسه. +**230 ملف مصدري · ~55,000 سطر برمجي · 3,200+ اختبار · 312 دالة · 62 نطاق KV**، كل ذلك على ثلاث بنى أساسية. لا يوجد أمر `agentmemory plugin install`. نظام الإضافات هو iii نفسه. --- diff --git a/READMEs/README.bn-BD.md b/READMEs/README.bn-BD.md index 6cacd5ef4d..e7af5228bd 100644 --- a/READMEs/README.bn-BD.md +++ b/READMEs/README.bn-BD.md @@ -1437,7 +1437,7 @@ fi | Prometheus / Grafana | iii OTEL + হেলথ মনিটর | | কাস্টম প্লাগইন সিস্টেম | `iii worker add ` | -**230 সোর্স ফাইল · ~54,000 LOC · 3,200+ টেস্ট · 312 ফাংশন · 62 KV স্কোপ**, সবকিছু তিনটি প্রিমিটিভের উপর। কোনো `agentmemory plugin install` নেই। প্লাগইন সিস্টেমটাই iii নিজে। +**230 সোর্স ফাইল · ~55,000 LOC · 3,200+ টেস্ট · 312 ফাংশন · 62 KV স্কোপ**, সবকিছু তিনটি প্রিমিটিভের উপর। কোনো `agentmemory plugin install` নেই। প্লাগইন সিস্টেমটাই iii নিজে। --- diff --git a/READMEs/README.cs-CZ.md b/READMEs/README.cs-CZ.md index 280889feb6..7329b48993 100644 --- a/READMEs/README.cs-CZ.md +++ b/READMEs/README.cs-CZ.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Vlastní systémy pluginů | `iii worker add ` | -**230 zdrojových souborů · ~54,000 řádků kódu · 3,200+ testů · 312 funkcí · 62 KV scopes**, vše na třech primitivech. Žádné `agentmemory plugin install`. Systém pluginů je samo iii. +**230 zdrojových souborů · ~55,000 řádků kódu · 3,200+ testů · 312 funkcí · 62 KV scopes**, vše na třech primitivech. Žádné `agentmemory plugin install`. Systém pluginů je samo iii. --- diff --git a/READMEs/README.da-DK.md b/READMEs/README.da-DK.md index d3dfdc6fdc..6c3149400c 100644 --- a/READMEs/README.da-DK.md +++ b/READMEs/README.da-DK.md @@ -1436,7 +1436,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Custom plugin-systemer | `iii worker add ` | -**230 kildefiler · ~54,000 LOC · 3,200+ tests · 312 funktioner · 62 KV-scopes**, alt på tre primitiver. Intet `agentmemory plugin install`. Plugin-systemet er iii selv. +**230 kildefiler · ~55,000 LOC · 3,200+ tests · 312 funktioner · 62 KV-scopes**, alt på tre primitiver. Intet `agentmemory plugin install`. Plugin-systemet er iii selv. --- diff --git a/READMEs/README.de-DE.md b/READMEs/README.de-DE.md index d83f5d5274..757028ecf9 100644 --- a/READMEs/README.de-DE.md +++ b/READMEs/README.de-DE.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + Health-Monitor | | Eigene Plugin-Systeme | `iii worker add ` | -**230 Quelldateien · ~54,000 LOC · 3,200+ Tests · 312 Funktionen · 62 KV-Scopes**, alles auf drei Primitiven. Kein `agentmemory plugin install`. Das Plugin-System ist iii selbst. +**230 Quelldateien · ~55,000 LOC · 3,200+ Tests · 312 Funktionen · 62 KV-Scopes**, alles auf drei Primitiven. Kein `agentmemory plugin install`. Das Plugin-System ist iii selbst. --- diff --git a/READMEs/README.el-GR.md b/READMEs/README.el-GR.md index 6ab0a05531..3d5c9a0e4a 100644 --- a/READMEs/README.el-GR.md +++ b/READMEs/README.el-GR.md @@ -1438,7 +1438,7 @@ fi | Prometheus / Grafana | iii OTEL + monitor υγείας | | Προσαρμοσμένα συστήματα plugin | `iii worker add ` | -**230 αρχεία πηγαίου κώδικα · ~54,000 LOC · 3,200+ δοκιμές · 312 functions · 62 KV scopes**, όλα πάνω σε τρία primitives. Δεν υπάρχει `agentmemory plugin install`. Το σύστημα plugin είναι το ίδιο το iii. +**230 αρχεία πηγαίου κώδικα · ~55,000 LOC · 3,200+ δοκιμές · 312 functions · 62 KV scopes**, όλα πάνω σε τρία primitives. Δεν υπάρχει `agentmemory plugin install`. Το σύστημα plugin είναι το ίδιο το iii. --- diff --git a/READMEs/README.es-ES.md b/READMEs/README.es-ES.md index cecca56c7e..c7aaf3405c 100644 --- a/READMEs/README.es-ES.md +++ b/READMEs/README.es-ES.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + monitor de salud | | Sistemas de plugins propios | `iii worker add ` | -**230 ficheros de código · ~54,000 LOC · 3,200+ tests · 312 funciones · 62 scopes KV**, todo sobre tres primitivos. No hay `agentmemory plugin install`. El sistema de plugins es iii mismo. +**230 ficheros de código · ~55,000 LOC · 3,200+ tests · 312 funciones · 62 scopes KV**, todo sobre tres primitivos. No hay `agentmemory plugin install`. El sistema de plugins es iii mismo. --- diff --git a/READMEs/README.fi-FI.md b/READMEs/README.fi-FI.md index 6460dccab3..3c79b516b5 100644 --- a/READMEs/README.fi-FI.md +++ b/READMEs/README.fi-FI.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + terveysmonitori | | Mukautetut plugin-järjestelmät | `iii worker add ` | -**230 lähdetiedostoa · ~54,000 LOC · 3,200+ testiä · 312 funktiota · 62 KV-scopea**, kaikki kolmen primitiivin päällä. Ei `agentmemory plugin install` -komentoa. Plugin-järjestelmä on iii itse. +**230 lähdetiedostoa · ~55,000 LOC · 3,200+ testiä · 312 funktiota · 62 KV-scopea**, kaikki kolmen primitiivin päällä. Ei `agentmemory plugin install` -komentoa. Plugin-järjestelmä on iii itse. --- diff --git a/READMEs/README.fr-FR.md b/READMEs/README.fr-FR.md index 92f054b630..071f0fd444 100644 --- a/READMEs/README.fr-FR.md +++ b/READMEs/README.fr-FR.md @@ -1448,7 +1448,7 @@ fi | Prometheus / Grafana | iii OTEL + moniteur de santé | | Systèmes de plugins custom | `iii worker add ` | -**230 fichiers sources · ~54,000 LOC · 3,200+ tests · 312 fonctions · 62 scopes KV**, tout sur trois primitives. Pas de `agentmemory plugin install`. Le système de plugins, c'est iii lui-même. +**230 fichiers sources · ~55,000 LOC · 3,200+ tests · 312 fonctions · 62 scopes KV**, tout sur trois primitives. Pas de `agentmemory plugin install`. Le système de plugins, c'est iii lui-même. --- diff --git a/READMEs/README.he-IL.md b/READMEs/README.he-IL.md index 671067f383..4743e4ffb6 100644 --- a/READMEs/README.he-IL.md +++ b/READMEs/README.he-IL.md @@ -1439,7 +1439,7 @@ fi | Prometheus / Grafana | iii OTEL + מוניטור health | | מערכות plugin מותאמות אישית | `iii worker add ` | -**230 קבצי מקור · ~54,000 LOC · 3,200+ בדיקות · 312 פונקציות · 62 תחומי KV**, הכול על שלושה primitives. אין `agentmemory plugin install`. מערכת ה-plugin היא iii עצמו. +**230 קבצי מקור · ~55,000 LOC · 3,200+ בדיקות · 312 פונקציות · 62 תחומי KV**, הכול על שלושה primitives. אין `agentmemory plugin install`. מערכת ה-plugin היא iii עצמו. --- diff --git a/READMEs/README.hi-IN.md b/READMEs/README.hi-IN.md index 6bf67f822a..bae2019073 100644 --- a/READMEs/README.hi-IN.md +++ b/READMEs/README.hi-IN.md @@ -1449,7 +1449,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Custom plugin systems | `iii worker add ` | -**230 source files · ~54,000 LOC · 3,200+ tests · 312 functions · 62 KV scopes**, सब कुछ तीन primitives पर। कोई `agentmemory plugin install` नहीं। Plugin system iii स्वयं है। +**230 source files · ~55,000 LOC · 3,200+ tests · 312 functions · 62 KV scopes**, सब कुछ तीन primitives पर। कोई `agentmemory plugin install` नहीं। Plugin system iii स्वयं है। --- diff --git a/READMEs/README.hu-HU.md b/READMEs/README.hu-HU.md index 94f8ea907e..12496fa7e8 100644 --- a/READMEs/README.hu-HU.md +++ b/READMEs/README.hu-HU.md @@ -1446,7 +1446,7 @@ A `/agentmemory/export` emellett elfogadja a `?maxSessions=`-t és a `?offset=`- | Prometheus / Grafana | iii OTEL + egészségfigyelő | | Egyéni plugin-rendszerek | `iii worker add ` | -**230 forrásfájl · ~54,000 LOC · 3,200+ teszt · 312 funkció · 62 KV-hatókör**, mindez három primitíven. Nincs `agentmemory plugin install`. A plugin-rendszer maga az iii. +**230 forrásfájl · ~55,000 LOC · 3,200+ teszt · 312 funkció · 62 KV-hatókör**, mindez három primitíven. Nincs `agentmemory plugin install`. A plugin-rendszer maga az iii. --- diff --git a/READMEs/README.id-ID.md b/READMEs/README.id-ID.md index c1c4da3f80..f085ede889 100644 --- a/READMEs/README.id-ID.md +++ b/READMEs/README.id-ID.md @@ -1447,7 +1447,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Sistem plugin custom | `iii worker add ` | -**230 file sumber · ~54,000 LOC · 3,200+ pengujian · 312 fungsi · 62 scope KV**, semua di atas tiga primitif. Tidak ada `agentmemory plugin install`. Sistem plugin-nya adalah iii itu sendiri. +**230 file sumber · ~55,000 LOC · 3,200+ pengujian · 312 fungsi · 62 scope KV**, semua di atas tiga primitif. Tidak ada `agentmemory plugin install`. Sistem plugin-nya adalah iii itu sendiri. --- diff --git a/READMEs/README.it-IT.md b/READMEs/README.it-IT.md index c2b55bfbca..16848b8326 100644 --- a/READMEs/README.it-IT.md +++ b/READMEs/README.it-IT.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + monitor di salute | | Sistemi di plugin personalizzati | `iii worker add ` | -**230 file sorgente · ~54,000 righe di codice · 3,200+ test · 312 funzioni · 62 scope KV**, tutto su tre primitive. Nessun `agentmemory plugin install`. Il sistema di plugin è iii stesso. +**230 file sorgente · ~55,000 righe di codice · 3,200+ test · 312 funzioni · 62 scope KV**, tutto su tre primitive. Nessun `agentmemory plugin install`. Il sistema di plugin è iii stesso. --- diff --git a/READMEs/README.ja-JP.md b/READMEs/README.ja-JP.md index ba64a24f0a..06fbb5c6a0 100644 --- a/READMEs/README.ja-JP.md +++ b/READMEs/README.ja-JP.md @@ -1445,7 +1445,7 @@ fi | Prometheus / Grafana | iii OTEL + ヘルスモニタ | | カスタムプラグインシステム | `iii worker add ` | -**230 ソースファイル · ~54,000 LOC · 3,200+ テスト · 312 functions · 62 KV スコープ** — すべて 3 つのプリミティブの上に。`agentmemory plugin install` はありません。プラグインシステムは iii そのものです。 +**230 ソースファイル · ~55,000 LOC · 3,200+ テスト · 312 functions · 62 KV スコープ** — すべて 3 つのプリミティブの上に。`agentmemory plugin install` はありません。プラグインシステムは iii そのものです。 --- diff --git a/READMEs/README.ko-KR.md b/READMEs/README.ko-KR.md index 53bb568fa1..001410a896 100644 --- a/READMEs/README.ko-KR.md +++ b/READMEs/README.ko-KR.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + health 모니터 | | 커스텀 플러그인 시스템 | `iii worker add ` | -**소스 파일 230개 · LOC ~54,000줄 · 테스트 3,200+개 · 함수 312개 · KV 스코프 62개**, 모두 세 가지 프리미티브 위에서 동작합니다. `agentmemory plugin install` 같은 것은 없습니다. 플러그인 시스템은 곧 iii 자체입니다. +**소스 파일 230개 · LOC ~55,000줄 · 테스트 3,200+개 · 함수 312개 · KV 스코프 62개**, 모두 세 가지 프리미티브 위에서 동작합니다. `agentmemory plugin install` 같은 것은 없습니다. 플러그인 시스템은 곧 iii 자체입니다. --- diff --git a/READMEs/README.nb-NO.md b/READMEs/README.nb-NO.md index c6efd9fcdb..e27f84ab1b 100644 --- a/READMEs/README.nb-NO.md +++ b/READMEs/README.nb-NO.md @@ -1447,7 +1447,7 @@ fi | Prometheus / Grafana | iii OTEL + helseovervåker | | Egendefinerte plugin-systemer | `iii worker add ` | -**230 kildefiler · ~54,000 LOC · 3,200+ tester · 312 funksjoner · 62 KV-scopes**, alt på tre primitiver. Ingen `agentmemory plugin install`. Plugin-systemet er iii selv. +**230 kildefiler · ~55,000 LOC · 3,200+ tester · 312 funksjoner · 62 KV-scopes**, alt på tre primitiver. Ingen `agentmemory plugin install`. Plugin-systemet er iii selv. --- diff --git a/READMEs/README.nl-NL.md b/READMEs/README.nl-NL.md index ac4a93f4b0..c107620514 100644 --- a/READMEs/README.nl-NL.md +++ b/READMEs/README.nl-NL.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Custom plugin-systemen | `iii worker add ` | -**230 bronbestanden · ~54,000 LOC · 3,200+ tests · 312 functies · 62 KV-scopes**, allemaal op drie primitieven. Geen `agentmemory plugin install`. Het pluginsysteem is iii zelf. +**230 bronbestanden · ~55,000 LOC · 3,200+ tests · 312 functies · 62 KV-scopes**, allemaal op drie primitieven. Geen `agentmemory plugin install`. Het pluginsysteem is iii zelf. --- diff --git a/READMEs/README.pl-PL.md b/READMEs/README.pl-PL.md index 48e061153a..28ccd098ab 100644 --- a/READMEs/README.pl-PL.md +++ b/READMEs/README.pl-PL.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + monitor stanu | | Niestandardowe systemy pluginów | `iii worker add ` | -**230 plików źródłowych · ~54,000 LOC · 3,200+ testów · 312 funkcji · 62 zakresy KV**, wszystko na trzech prymitywach. Nie ma `agentmemory plugin install`. Systemem pluginów jest sam iii. +**230 plików źródłowych · ~55,000 LOC · 3,200+ testów · 312 funkcji · 62 zakresy KV**, wszystko na trzech prymitywach. Nie ma `agentmemory plugin install`. Systemem pluginów jest sam iii. --- diff --git a/READMEs/README.pt-BR.md b/READMEs/README.pt-BR.md index 319de1bd5e..d85a2c1e03 100644 --- a/READMEs/README.pt-BR.md +++ b/READMEs/README.pt-BR.md @@ -1447,7 +1447,7 @@ fi | Prometheus / Grafana | iii OTEL + monitor de saúde | | Sistemas de plugin customizados | `iii worker add ` | -**230 arquivos de código · ~54,000 LOC · 3,200+ testes · 312 funções · 62 escopos KV**, tudo em cima de três primitivos. Sem `agentmemory plugin install`. O sistema de plugins é o próprio iii. +**230 arquivos de código · ~55,000 LOC · 3,200+ testes · 312 funções · 62 escopos KV**, tudo em cima de três primitivos. Sem `agentmemory plugin install`. O sistema de plugins é o próprio iii. --- diff --git a/READMEs/README.pt-PT.md b/READMEs/README.pt-PT.md index 28f7e0f808..6a6079691d 100644 --- a/READMEs/README.pt-PT.md +++ b/READMEs/README.pt-PT.md @@ -1446,7 +1446,7 @@ O `/agentmemory/export` também aceita `?maxSessions=` e `?offset=` para dividir | Prometheus / Grafana | iii OTEL + monitor de saúde | | Sistemas de plugins personalizados | `iii worker add ` | -**230 ficheiros de código-fonte · ~54,000 LOC · 3,200+ testes · 312 funções · 62 âmbitos de KV**, tudo sobre três primitivos. Sem `agentmemory plugin install`. O sistema de plugins é o próprio iii. +**230 ficheiros de código-fonte · ~55,000 LOC · 3,200+ testes · 312 funções · 62 âmbitos de KV**, tudo sobre três primitivos. Sem `agentmemory plugin install`. O sistema de plugins é o próprio iii. --- diff --git a/READMEs/README.ro-RO.md b/READMEs/README.ro-RO.md index 721775b828..c2e2f023cf 100644 --- a/READMEs/README.ro-RO.md +++ b/READMEs/README.ro-RO.md @@ -1430,7 +1430,7 @@ fi | Prometheus / Grafana | iii OTEL + monitor de sănătate | | Sisteme de plugin-uri personalizate | `iii worker add ` | -**230 fișiere sursă · ~54,000 LOC · 3,200+ teste · 312 funcții · 62 de domenii KV**, toate pe trei primitive. Niciun `agentmemory plugin install`. Sistemul de plugin-uri este chiar iii. +**230 fișiere sursă · ~55,000 LOC · 3,200+ teste · 312 funcții · 62 de domenii KV**, toate pe trei primitive. Niciun `agentmemory plugin install`. Sistemul de plugin-uri este chiar iii. --- diff --git a/READMEs/README.ru-RU.md b/READMEs/README.ru-RU.md index a6c80e5fb1..7e0fb08f03 100644 --- a/READMEs/README.ru-RU.md +++ b/READMEs/README.ru-RU.md @@ -1446,7 +1446,7 @@ fi | Prometheus / Grafana | iii OTEL + монитор состояния | | Самописные плагинные системы | `iii worker add ` | -**230 исходных файлов · ~54,000 LOC · 3,200+ тестов · 312 функций · 62 KV-scope**, всё на трёх примитивах. Никакого `agentmemory plugin install`. Плагинная система — это сам iii. +**230 исходных файлов · ~55,000 LOC · 3,200+ тестов · 312 функций · 62 KV-scope**, всё на трёх примитивах. Никакого `agentmemory plugin install`. Плагинная система — это сам iii. --- diff --git a/READMEs/README.sv-SE.md b/READMEs/README.sv-SE.md index 14e076fd67..82d0923c38 100644 --- a/READMEs/README.sv-SE.md +++ b/READMEs/README.sv-SE.md @@ -1437,7 +1437,7 @@ fi | Prometheus / Grafana | iii OTEL + hälsoövervakare | | Anpassade pluginsystem | `iii worker add ` | -**230 källkodsfiler · ~54,000 LOC · 3,200+ tester · 312 funktioner · 62 KV-scopes**, allt på tre primitiv. Inget `agentmemory plugin install`. Pluginsystemet är iii självt. +**230 källkodsfiler · ~55,000 LOC · 3,200+ tester · 312 funktioner · 62 KV-scopes**, allt på tre primitiv. Inget `agentmemory plugin install`. Pluginsystemet är iii självt. --- diff --git a/READMEs/README.th-TH.md b/READMEs/README.th-TH.md index fb90867f17..7834e5c725 100644 --- a/READMEs/README.th-TH.md +++ b/READMEs/README.th-TH.md @@ -1444,7 +1444,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Custom plugin systems | `iii worker add ` | -**230 source file · ~54,000 LOC · 3,200+ การทดสอบ · 312 function · 62 KV scope** ทั้งหมดอยู่บน primitive สามตัว ไม่มี `agentmemory plugin install` ระบบ plugin ก็คือ iii เองนั่นแหละ +**230 source file · ~55,000 LOC · 3,200+ การทดสอบ · 312 function · 62 KV scope** ทั้งหมดอยู่บน primitive สามตัว ไม่มี `agentmemory plugin install` ระบบ plugin ก็คือ iii เองนั่นแหละ --- diff --git a/READMEs/README.tl-PH.md b/READMEs/README.tl-PH.md index fa65be4389..e6cce952b5 100644 --- a/READMEs/README.tl-PH.md +++ b/READMEs/README.tl-PH.md @@ -1446,7 +1446,7 @@ Tinatanggap din ng `/agentmemory/export` ang `?maxSessions=` at `?offset=` para | Prometheus / Grafana | iii OTEL + health monitor | | Custom na plugin system | `iii worker add ` | -**230 na source file · ~54,000 LOC · 3,200+ na test · 312 na function · 62 na KV scope**, lahat sa tatlong primitive. Walang `agentmemory plugin install`. Ang plugin system mismo ay ang iii. +**230 na source file · ~55,000 LOC · 3,200+ na test · 312 na function · 62 na KV scope**, lahat sa tatlong primitive. Walang `agentmemory plugin install`. Ang plugin system mismo ay ang iii. --- diff --git a/READMEs/README.tr-TR.md b/READMEs/README.tr-TR.md index 584c504c2b..7605adc09c 100644 --- a/READMEs/README.tr-TR.md +++ b/READMEs/README.tr-TR.md @@ -1437,7 +1437,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Custom eklenti sistemleri | `iii worker add ` | -**230 kaynak dosya · ~54,000 LOC · 3,200+ test · 312 fonksiyon · 62 KV scope**, hepsi üç primitif üzerinde. `agentmemory plugin install` yok. Eklenti sistemi, iii'nin kendisidir. +**230 kaynak dosya · ~55,000 LOC · 3,200+ test · 312 fonksiyon · 62 KV scope**, hepsi üç primitif üzerinde. `agentmemory plugin install` yok. Eklenti sistemi, iii'nin kendisidir. --- diff --git a/READMEs/README.uk-UA.md b/READMEs/README.uk-UA.md index efcb570a89..848bee1315 100644 --- a/READMEs/README.uk-UA.md +++ b/READMEs/README.uk-UA.md @@ -1447,7 +1447,7 @@ fi | Prometheus / Grafana | iii OTEL + монітор стану здоров'я | | Власні системи плагінів | `iii worker add ` | -**230 вихідних файлів · ~54,000 LOC · 3,200+ тестів · 312 функцій · 62 KV-скоупів**, усе на трьох примітивах. Жодного `agentmemory plugin install`. Система плагінів — це сам iii. +**230 вихідних файлів · ~55,000 LOC · 3,200+ тестів · 312 функцій · 62 KV-скоупів**, усе на трьох примітивах. Жодного `agentmemory plugin install`. Система плагінів — це сам iii. --- diff --git a/READMEs/README.ur-PK.md b/READMEs/README.ur-PK.md index a8c4cea265..9b9959aa59 100644 --- a/READMEs/README.ur-PK.md +++ b/READMEs/README.ur-PK.md @@ -1432,7 +1432,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | کسٹم plugin سسٹمز | `iii worker add ` | -**230 سورس فائلز · ~54,000 لائنز کوڈ · 3,200+ ٹیسٹس · 312 فنکشنز · 62 KV اسکوپس**، سب تین primitives پر۔ کوئی `agentmemory plugin install` نہیں۔ plugin سسٹم خود iii ہی ہے۔ +**230 سورس فائلز · ~55,000 لائنز کوڈ · 3,200+ ٹیسٹس · 312 فنکشنز · 62 KV اسکوپس**، سب تین primitives پر۔ کوئی `agentmemory plugin install` نہیں۔ plugin سسٹم خود iii ہی ہے۔ --- diff --git a/READMEs/README.vi-VN.md b/READMEs/README.vi-VN.md index abcb83d828..8960957074 100644 --- a/READMEs/README.vi-VN.md +++ b/READMEs/README.vi-VN.md @@ -1437,7 +1437,7 @@ fi | Prometheus / Grafana | iii OTEL + health monitor | | Hệ thống plugin tùy chỉnh | `iii worker add ` | -**230 file source · ~54,000 LOC · 3,200+ test · 312 function · 62 KV scope**, tất cả trên ba primitive. Không có `agentmemory plugin install`. Hệ thống plugin chính là iii. +**230 file source · ~55,000 LOC · 3,200+ test · 312 function · 62 KV scope**, tất cả trên ba primitive. Không có `agentmemory plugin install`. Hệ thống plugin chính là iii. --- diff --git a/READMEs/README.zh-CN.md b/READMEs/README.zh-CN.md index 23357d756c..4f272fb752 100644 --- a/READMEs/README.zh-CN.md +++ b/READMEs/README.zh-CN.md @@ -1444,7 +1444,7 @@ fi | Prometheus / Grafana | iii OTEL + 健康监控 | | Custom plugin systems | `iii worker add ` | -**230 个源文件 · ~54,000 行代码 · 3,200+ 个测试 · 312 个函数 · 62 个 KV 作用域**,全部基于三种原语。没有 `agentmemory plugin install`。插件系统就是 iii 本身。 +**230 个源文件 · ~55,000 行代码 · 3,200+ 个测试 · 312 个函数 · 62 个 KV 作用域**,全部基于三种原语。没有 `agentmemory plugin install`。插件系统就是 iii 本身。 --- diff --git a/READMEs/README.zh-TW.md b/READMEs/README.zh-TW.md index fc376f1e6f..8d61a3f5f4 100644 --- a/READMEs/README.zh-TW.md +++ b/READMEs/README.zh-TW.md @@ -1430,7 +1430,7 @@ fi | Prometheus / Grafana | iii OTEL + 健康監控 | | 自訂外掛系統 | `iii worker add ` | -**230 個原始檔 · ~54,000 行程式碼 · 3,200+ 測試 · 312 個函式 · 62 個 KV 範圍**,全部基於三種原語。沒有 `agentmemory plugin install`。外掛系統就是 iii 本身。 +**230 個原始檔 · ~55,000 行程式碼 · 3,200+ 測試 · 312 個函式 · 62 個 KV 範圍**,全部基於三種原語。沒有 `agentmemory plugin install`。外掛系統就是 iii 本身。 --- diff --git a/scripts/docs-sync.state.json b/scripts/docs-sync.state.json index afd23a80b2..6985a97a36 100644 --- a/scripts/docs-sync.state.json +++ b/scripts/docs-sync.state.json @@ -29,7 +29,7 @@ "230" ], "loc": [ - "54000" + "55000" ], "kvScopes": [ "62" diff --git a/src/functions/consolidate.ts b/src/functions/consolidate.ts index 8924a03a1a..0765f35209 100644 --- a/src/functions/consolidate.ts +++ b/src/functions/consolidate.ts @@ -7,7 +7,9 @@ import type { } from "../types.js"; import { KV, generateId } from "../state/schema.js"; import { StateKV } from "../state/kv.js"; -import { recordAudit } from "./audit.js"; +import { safeAudit } from "./audit.js"; +import { withKeyedLocks } from "../state/keyed-mutex.js"; +import { stableStringify } from "../capture/event-id.js"; const CONSOLIDATION_SYSTEM = `You are a memory consolidation engine. Given a set of related observations from coding sessions, synthesize them into a single long-term memory. @@ -62,6 +64,11 @@ function parseMemoryXml( }; } +function sameIds(left: string[] | undefined, right: string[]): boolean { + const ids = new Set(left); + return ids.size === right.length && right.every((id) => ids.has(id)); +} + export function registerConsolidateFunction( sdk: IIIClient, kv: StateKV, @@ -111,10 +118,6 @@ export function registerConsolidateFunction( } let consolidated = 0; - const existingMemories = await kv.list(KV.memories); - const existingTitles = new Set( - existingMemories.map((m) => m.title.toLowerCase()), - ); const MAX_LLM_CALLS = 10; let llmCallCount = 0; @@ -138,17 +141,19 @@ export function registerConsolidateFunction( ) .join("\n\n"); + let timer: ReturnType | undefined; try { + llmCallCount++; const response = await Promise.race([ provider.compress( CONSOLIDATION_SYSTEM, `Concept: "${concept}"\n\nObservations:\n${prompt}`, ), - new Promise((_, reject) => - setTimeout(() => reject(new Error("compress timeout")), 30_000), - ), + new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error("compress timeout")), 30_000); + }), ]); - llmCallCount++; + clearTimeout(timer); const parsed = parseMemoryXml(response, sessionIds); if (!parsed) continue; @@ -159,75 +164,98 @@ export function registerConsolidateFunction( ? data.project.trim() : undefined; - // A scoped consolidation run must only evolve memories that belong - // to the same project. Without this guard, two projects that happen - // to consolidate observations into an identically-titled memory would - // cause one project's memory to silently evolve the other's — the - // exact class of cross-project corruption this fix is designed to - // prevent. An unscoped run (no data.project, background cron path) - // preserves the pre-existing behavior and may evolve any memory. - const existingMatch = existingMemories.find( - (m) => - m.title.toLowerCase() === parsed.title.toLowerCase() && - (!scopedProject || !m.project || m.project === scopedProject), - ); - - if (existingMatch) { - existingMatch.isLatest = false; - await kv.set(KV.memories, existingMatch.id, existingMatch); - await recordAudit(kv, "evolve", "mem::consolidate", [existingMatch.id], { - action: "mark_non_latest", - concept, - }); + const publicationLocks = ["mem:remember", ...sessionIds.map((sid) => `obs:${sid}`)]; + const published = await withKeyedLocks(publicationLocks, async () => { + for (const sid of sessionIds) { + const current = await kv.get(KV.sessions, sid); + const original = filtered.find((session) => session.id === sid)!; + if (!current || current.project !== original.project || current.agentId !== original.agentId) return false; + } + for (const { sid, ...original } of top) { + const current = await kv.get(KV.observations(sid), original.id); + if (!current || stableStringify(current) !== stableStringify(original)) return false; + } + const memories = await kv.list(KV.memories); + const candidates = memories.filter((memory) => + memory.isLatest !== false && + (!scopedProject || !memory.project || memory.project === scopedProject), + ); + const children = new Map(); + for (const memory of candidates) { + if (!memory.parentId) continue; + const siblings = children.get(memory.parentId) ?? []; + siblings.push(memory); + children.set(memory.parentId, siblings); + } + // Complete an interrupted successor publication only for unambiguous lineage. + for (const parent of candidates) { + const successors = children.get(parent.id); + if (successors?.length !== 1) continue; + const child = successors[0]; + if (child.title.toLowerCase() !== parent.title.toLowerCase() || + child.version !== (parent.version || 1) + 1 || + !child.supersedes?.includes(parent.id)) continue; + await kv.set(KV.memories, parent.id, { ...parent, isLatest: false }); + parent.isLatest = false; + await safeAudit(kv, "evolve", "mem::consolidate", [parent.id], { + action: "complete_publication", newId: child.id, + }); + } + const heads = candidates.filter((memory) => memory.isLatest !== false); + if (heads.some((memory) => + sameIds(memory.sourceObservationIds, obsIds) && + sameIds(memory.sessionIds, sessionIds), + )) return false; - const evolved: Memory = { - id: generateId("mem"), - createdAt: now, - updatedAt: now, - ...parsed, - version: (existingMatch.version || 1) + 1, - parentId: existingMatch.id, - supersedes: [ - existingMatch.id, - ...(existingMatch.supersedes || []), - ], - sourceObservationIds: obsIds, - isLatest: true, - ...(scopedProject !== undefined && { project: scopedProject }), - }; - await kv.set(KV.memories, evolved.id, evolved); - await recordAudit(kv, "evolve", "mem::consolidate", [evolved.id], { - action: "evolve_memory", - oldId: existingMatch.id, - newId: evolved.id, - concept, - }); - existingTitles.add(evolved.title.toLowerCase()); - consolidated++; - } else { + const previous = heads.find((memory) => + memory.title.toLowerCase() === parsed.title.toLowerCase(), + ); const memory: Memory = { id: generateId("mem"), createdAt: now, updatedAt: now, ...parsed, sourceObservationIds: obsIds, - version: 1, + version: previous ? (previous.version || 1) + 1 : 1, isLatest: true, + ...(previous && { + parentId: previous.id, + supersedes: [previous.id, ...(previous.supersedes || [])], + }), ...(scopedProject !== undefined && { project: scopedProject }), }; - await kv.set(KV.memories, memory.id, memory); - await recordAudit(kv, "remember", "mem::consolidate", [memory.id], { - action: "create_memory", + + // Keep the previous version readable until its successor is stored. + try { + await kv.set(KV.memories, memory.id, memory); + } catch (error) { + if (!(await kv.get(KV.memories, memory.id))) throw error; + } + if (previous) { + try { + await kv.set(KV.memories, previous.id, { ...previous, isLatest: false }); + } catch (error) { + // A write can commit before its acknowledgement fails. + await kv.set(KV.memories, previous.id, previous); + await kv.delete(KV.memories, memory.id); + throw error; + } + } + await safeAudit(kv, previous ? "evolve" : "remember", "mem::consolidate", [memory.id], { + action: previous ? "evolve_memory" : "create_memory", + ...(previous && { oldId: previous.id, newId: memory.id }), concept, }); - existingTitles.add(memory.title.toLowerCase()); - consolidated++; - } + return true; + }); + if (published) consolidated++; } catch (err) { logger.warn("Consolidation failed for concept", { concept, error: err instanceof Error ? err.message : String(err), }); + } finally { + clearTimeout(timer); } } diff --git a/test/consolidate-project-scope.test.ts b/test/consolidate-project-scope.test.ts index e8efc45c11..e36cd33ec3 100644 --- a/test/consolidate-project-scope.test.ts +++ b/test/consolidate-project-scope.test.ts @@ -5,7 +5,7 @@ vi.mock("../src/logger.js", () => ({ })); vi.mock("../src/functions/audit.js", () => ({ - recordAudit: vi.fn(), + safeAudit: vi.fn(), })); import { registerConsolidateFunction } from "../src/functions/consolidate.js"; diff --git a/test/consolidate-publication.test.ts b/test/consolidate-publication.test.ts new file mode 100644 index 0000000000..0a87486122 --- /dev/null +++ b/test/consolidate-publication.test.ts @@ -0,0 +1,286 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { registerConsolidateFunction } from "../src/functions/consolidate.js"; +import { KV } from "../src/state/schema.js"; +import { withKeyedLock } from "../src/state/keyed-mutex.js"; +import type { CompressedObservation, Memory, MemoryProvider, Session } from "../src/types.js"; + +vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn() } })); +vi.mock("../src/functions/audit.js", () => ({ safeAudit: vi.fn() })); + +function fixture() { + const rows = new Map>(); + const kv = { + get: async (scope: string, id: string): Promise => + structuredClone(rows.get(scope)?.get(id) as T ?? null), + list: async (scope: string): Promise => + structuredClone([...rows.get(scope)?.values() ?? []] as T[]), + set: vi.fn(async (scope: string, id: string, value: T): Promise => { + if (!rows.has(scope)) rows.set(scope, new Map()); + rows.get(scope)!.set(id, structuredClone(value)); + return value; + }), + delete: vi.fn(async (scope: string, id: string) => { rows.get(scope)?.delete(id); }), + }; + const functions = new Map Promise>(); + const sdk = { registerFunction: (id: string, handler: (data: unknown) => Promise) => functions.set(id, handler) }; + const compress = vi.fn(async () => xml("Shared title")); + registerConsolidateFunction(sdk as never, kv as never, { compress } as unknown as MemoryProvider); + const run = (project = "app") => functions.get("mem::consolidate")!({ project, minObservations: 1 }); + const memories = () => kv.list(KV.memories); + return { kv, compress, run, memories }; +} + +function xml(title: string) { + return `fact${title}Independent contentauth8`; +} + +async function seed(f: ReturnType, sessionId = "session-a", concept = "auth", project = "app") { + const session: Session = { id: sessionId, project, cwd: "/synthetic", startedAt: "2026-01-01T00:00:00Z", status: "completed", observationCount: 3 }; + await f.kv.set(KV.sessions, sessionId, session); + for (let i = 0; i < 3; i++) { + const id = `${sessionId}-${concept}-${i}`; + const observation: CompressedObservation = { + id, sessionId, timestamp: session.startedAt, type: "decision", title: `${concept} ${i}`, + narrative: `Evidence ${i}`, facts: [], concepts: [concept], files: [], importance: 8, + }; + await f.kv.set(KV.observations(sessionId), id, observation); + } +} + +function memory(id: string, extra: Partial = {}): Memory { + return { + id, createdAt: "2026-01-01T00:00:00Z", updatedAt: "2026-01-01T00:00:00Z", type: "fact", + title: "Shared title", content: "Previous content", concepts: [], files: [], sessionIds: [], + strength: 7, version: 1, isLatest: true, project: "app", ...extra, + }; +} + +afterEach(() => vi.useRealTimers()); + +describe("consolidation publication", () => { + it("publishes overlapping concurrent runs once even when model titles differ", async () => { + const f = fixture(); + await seed(f); + let release!: () => void; + const barrier = new Promise((resolve) => { release = resolve; }); + let started = 0; + f.compress.mockImplementation(async () => { + const title = `Title ${++started}`; + if (started === 2) release(); + await barrier; + return xml(title); + }); + const results = await Promise.all([f.run(), f.run()]); + expect(results.map((r) => (r as { consolidated: number }).consolidated).sort()).toEqual([0, 1]); + expect(await f.memories()).toHaveLength(1); + expect(f.compress).toHaveBeenCalledTimes(2); + }); + + it("uses the current latest version for multiple groups within one run", async () => { + const f = fixture(); + await seed(f, "session-a", "auth"); + await seed(f, "session-a", "tokens"); + await f.kv.set(KV.memories, "old", memory("old", { isLatest: false })); + await f.kv.set(KV.memories, "current", memory("current", { version: 2, parentId: "old", supersedes: ["old"] })); + expect(await f.run()).toMatchObject({ consolidated: 2 }); + const rows = await f.memories(); + const latest = rows.filter((m) => m.isLatest !== false); + expect(latest).toHaveLength(1); + expect(latest[0].version).toBe(4); + expect(latest[0].supersedes).toHaveLength(3); + expect(latest[0].supersedes).toContain("current"); + }); + + it.each(["delete", "edit", "move-project", "move-agent"])("discards model output after source changes: %s", async (change) => { + const f = fixture(); + await seed(f); + f.compress.mockImplementation(async () => { + if (change === "delete") { + await f.kv.delete(KV.observations("session-a"), "session-a-auth-0"); + } else if (change === "edit") { + const row = await f.kv.get(KV.observations("session-a"), "session-a-auth-0"); + await f.kv.set(KV.observations("session-a"), row!.id, { ...row, narrative: "Changed evidence" }); + } else { + const session = await f.kv.get(KV.sessions, "session-a"); + await f.kv.set(KV.sessions, "session-a", { ...session, + ...(change === "move-project" ? { project: "other" } : { agentId: "other-agent" }), + }); + } + return xml("Stale model result"); + }); + expect(await f.run()).toMatchObject({ consolidated: 0 }); + expect(await f.memories()).toHaveLength(0); + }); + + it.each(["session-a", "session-b"])("holds source locks through publication before a writer changes %s", async (sessionId) => { + const f = fixture(); + await seed(f, "session-b"); + await seed(f, "session-a"); + let entered!: () => void; + let release!: () => void; + const publicationEntered = new Promise((resolve) => { entered = resolve; }); + const publicationBarrier = new Promise((resolve) => { release = resolve; }); + const list = f.kv.list; + f.kv.list = async (scope: string): Promise => { + if (scope === KV.memories) { + entered(); + await publicationBarrier; + } + return list(scope); + }; + const publication = f.run(); + await publicationEntered; + let writerStarted = false; + const writer = withKeyedLock(`obs:${sessionId}`, async () => { + writerStarted = true; + await f.kv.delete(KV.observations(sessionId), `${sessionId}-auth-0`); + }); + try { + await Promise.resolve(); + expect(writerStarted).toBe(false); + } finally { + release(); + await Promise.all([publication, writer]); + } + expect(await publication).toMatchObject({ consolidated: 1 }); + expect(writerStarted).toBe(true); + expect(await f.memories()).toHaveLength(1); + expect(await f.kv.get(KV.observations(sessionId), `${sessionId}-auth-0`)).toBeNull(); + }); + + it("does not deduplicate equal observation IDs from different sessions", async () => { + const f = fixture(); + await seed(f); + await f.kv.set(KV.memories, "elsewhere", memory("elsewhere", { + sessionIds: ["another-session"], sourceObservationIds: ["session-a-auth-0", "session-a-auth-1", "session-a-auth-2"], + })); + expect(await f.run()).toMatchObject({ consolidated: 1 }); + expect((await f.memories()).filter((m) => m.isLatest !== false)).toHaveLength(1); + }); + + it("does not deduplicate another project's matching provenance", async () => { + const f = fixture(); + await seed(f); + await f.kv.set(KV.memories, "elsewhere", memory("elsewhere", { + project: "other", sessionIds: ["session-a"], sourceObservationIds: ["session-a-auth-0", "session-a-auth-1", "session-a-auth-2"], + })); + expect(await f.run()).toMatchObject({ consolidated: 1 }); + expect((await f.memories()).filter((m) => m.isLatest !== false)).toHaveLength(2); + }); + + it("preserves the previous latest memory when successor creation fails", async () => { + const f = fixture(); + await seed(f); + const previous = memory("previous"); + await f.kv.set(KV.memories, previous.id, previous); + const write = f.kv.set.getMockImplementation()!; + f.kv.set.mockImplementation(async (scope, id, value) => { + if (scope === KV.memories && id !== previous.id) throw new Error("storage unavailable"); + return write(scope, id, value); + }); + expect(await f.run()).toMatchObject({ consolidated: 0 }); + expect(await f.memories()).toEqual([previous]); + }); + + it("confirms a committed successor after its write acknowledgement fails", async () => { + const f = fixture(); + await seed(f); + await f.kv.set(KV.memories, "previous", memory("previous")); + const write = f.kv.set.getMockImplementation()!; + f.kv.set.mockImplementation(async (scope, id, value) => { + await write(scope, id, value); + if (scope === KV.memories && id !== "previous") throw new Error("lost acknowledgement"); + return value; + }); + expect(await f.run()).toMatchObject({ consolidated: 1 }); + const rows = await f.memories(); + expect(rows.filter((m) => m.isLatest !== false)).toHaveLength(1); + expect(rows.find((m) => m.id === "previous")?.isLatest).toBe(false); + }); + + it("repairs an interrupted publication before evolving changed evidence", async () => { + const f = fixture(); + await seed(f); + await f.kv.set(KV.memories, "previous", memory("previous")); + await f.kv.set(KV.memories, "committed", memory("committed", { + version: 2, parentId: "previous", supersedes: ["previous"], + sourceObservationIds: ["older-source"], sessionIds: ["session-a"], + })); + expect(await f.run()).toMatchObject({ consolidated: 1 }); + const rows = await f.memories(); + const latest = rows.filter((m) => m.isLatest !== false); + expect(latest).toHaveLength(1); + expect(latest[0]).toMatchObject({ version: 3, parentId: "committed" }); + }); + + it("does not repair ambiguous siblings or unrelated parent references", async () => { + const f = fixture(); + await seed(f); + for (const row of [memory("parent"), + memory("child-one", { version: 2, parentId: "parent", supersedes: ["parent"] }), + memory("child-two", { version: 2, parentId: "parent", supersedes: ["parent"] }), + memory("other", { title: "Other title" }), + memory("unrelated", { title: "Another title", version: 2, parentId: "other", supersedes: ["other"] }), + ]) await f.kv.set(KV.memories, row.id, row); + f.compress.mockResolvedValue(xml("New independent title")); + await f.run(); + expect((await f.memories()).filter((m) => m.isLatest !== false)).toHaveLength(6); + }); + + it.each([false, true])("rolls back a failed predecessor demotion (write committed: %s)", async (committed) => { + const f = fixture(); + await seed(f); + const previous = memory("previous"); + await f.kv.set(KV.memories, previous.id, previous); + const write = f.kv.set.getMockImplementation()!; + f.kv.set.mockImplementation(async (scope, id, value) => { + if (scope === KV.memories && id === previous.id && (value as Memory).isLatest === false) { + if (committed) await write(scope, id, value); + throw new Error("acknowledgement failed"); + } + return write(scope, id, value); + }); + expect(await f.run()).toMatchObject({ consolidated: 0 }); + expect(await f.memories()).toEqual([previous]); + }); + + it("keeps the successor if restoring the predecessor also fails", async () => { + const f = fixture(); + await seed(f); + await f.kv.set(KV.memories, "previous", memory("previous")); + const write = f.kv.set.getMockImplementation()!; + f.kv.set.mockImplementation(async (scope, id, value) => { + if (scope === KV.memories && id === "previous") { + if ((value as Memory).isLatest === false) await write(scope, id, value); + throw new Error("storage unavailable"); + } + return write(scope, id, value); + }); + await f.run(); + const latest = (await f.memories()).filter((m) => m.isLatest !== false); + expect(latest).toHaveLength(1); + expect(latest[0].parentId).toBe("previous"); + expect(f.kv.delete).not.toHaveBeenCalled(); + }); + + it("limits failed provider attempts and clears every timeout", async () => { + vi.useFakeTimers(); + const f = fixture(); + for (let i = 0; i < 15; i++) await seed(f, "session-a", `concept-${i}`); + f.compress.mockRejectedValue(new Error("provider unavailable")); + expect(await f.run()).toMatchObject({ consolidated: 0 }); + expect(f.compress).toHaveBeenCalledTimes(10); + expect(vi.getTimerCount()).toBe(0); + }); + + it("clears the timeout after a valid or malformed response", async () => { + vi.useFakeTimers(); + const f = fixture(); + await seed(f); + await f.run(); + expect(vi.getTimerCount()).toBe(0); + f.compress.mockResolvedValue("not XML"); + await f.run(); + expect(vi.getTimerCount()).toBe(0); + }); +}); From d85d8fb2c99236ebfba335a60ab3be660590c616 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sun, 8 Nov 2026 14:09:41 +0530 Subject: [PATCH 2/4] fix(consolidate): compare evidence, not bookkeeping, and adopt legacy heads The pre-publication source check now compares the same evidence fields the key hashes, so a compression retry that only updates bookkeeping during generation no longer discards the result. A legacy head that matches by source and session ids is stamped with the current key once instead of blocking every later run, so a later evidence change publishes a new version. --- src/functions/consolidate.ts | 27 ++++++++++++++++++--------- test/consolidate-publication.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/functions/consolidate.ts b/src/functions/consolidate.ts index 84dee66baf..ee0f8c251a 100644 --- a/src/functions/consolidate.ts +++ b/src/functions/consolidate.ts @@ -65,11 +65,15 @@ function parseMemoryXml( }; } +function evidence(o: CompressedObservation) { + return { id: o.id, type: o.type, title: o.title, narrative: o.narrative, files: o.files, importance: o.importance, concepts: o.concepts }; +} + function consolidationKey(sources: Array): string { - const evidence = sources - .map((o) => ({ sid: o.sid, id: o.id, type: o.type, title: o.title, narrative: o.narrative, files: o.files, importance: o.importance })) + const rows = sources + .map((o) => ({ sid: o.sid, ...evidence(o) })) .sort((a, b) => (a.sid === b.sid ? (a.id < b.id ? -1 : a.id > b.id ? 1 : 0) : a.sid < b.sid ? -1 : 1)); - return fingerprintId("cons", stableStringify(evidence)); + return fingerprintId("cons", stableStringify(rows)); } function sameIds(left: string[] | undefined, right: string[]): boolean { @@ -182,7 +186,7 @@ export function registerConsolidateFunction( } for (const { sid, ...original } of top) { const current = await kv.get(KV.observations(sid), original.id); - if (!current || stableStringify(current) !== stableStringify(original)) return false; + if (!current || stableStringify(evidence(current)) !== stableStringify(evidence(original))) return false; } const memories = await collectAll(kv, KV.memories); const candidates = memories.filter((memory) => @@ -213,11 +217,16 @@ export function registerConsolidateFunction( }); } const heads = candidates.filter((memory) => memory.isLatest !== false); - if (heads.some((memory) => - memory.consolidationKeys - ? memory.consolidationKeys[concept] === sourceKey - : sameIds(memory.sourceObservationIds, obsIds) && sameIds(memory.sessionIds, sessionIds), - )) return false; + if (heads.some((memory) => memory.consolidationKeys?.[concept] === sourceKey)) return false; + const legacy = heads.find((memory) => + !memory.consolidationKeys && + sameIds(memory.sourceObservationIds, obsIds) && + sameIds(memory.sessionIds, sessionIds), + ); + if (legacy) { + await kv.set(KV.memories, legacy.id, { ...legacy, consolidationKeys: { [concept]: sourceKey } }); + return false; + } const sameTitle = heads.filter((memory) => memory.title.toLowerCase() === parsed.title.toLowerCase(), diff --git a/test/consolidate-publication.test.ts b/test/consolidate-publication.test.ts index 7e446c037e..c13f697f90 100644 --- a/test/consolidate-publication.test.ts +++ b/test/consolidate-publication.test.ts @@ -385,4 +385,29 @@ describe("consolidation publication", () => { expect(await f.run()).toMatchObject({ consolidated: 1 }); expect((await f.kv.get(KV.memories, "global"))!.isLatest).toBe(true); }); + + it("publishes when only compression bookkeeping changes while the model runs", async () => { + const f = fixture(); + await seed(f); + f.compress.mockImplementation(async () => { + const row = await f.kv.get(KV.observations("session-a"), "session-a-auth-0"); + await f.kv.set(KV.observations("session-a"), row!.id, { ...row!, compression: { revision: "retry", attempts: 2 } } as CompressedObservation); + return xml("Shared title"); + }); + expect(await f.run()).toMatchObject({ consolidated: 1 }); + }); + + it("adopts a legacy memory once and publishes when its evidence later changes", async () => { + const f = fixture(); + await seed(f); + await f.kv.set(KV.memories, "legacy", memory("legacy", { + sourceObservationIds: ["session-a-auth-0", "session-a-auth-1", "session-a-auth-2"], + sessionIds: ["session-a"], + })); + expect(await f.run()).toMatchObject({ consolidated: 0 }); + expect((await f.kv.get(KV.memories, "legacy"))!.consolidationKeys?.auth).toBeTruthy(); + const row = await f.kv.get(KV.observations("session-a"), "session-a-auth-0"); + await f.kv.set(KV.observations("session-a"), row!.id, { ...row!, narrative: "Recompressed evidence" }); + expect(await f.run()).toMatchObject({ consolidated: 1 }); + }); }); From fe41da325a6a349eff80dacbcd2de80f98fea1ee Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sun, 8 Nov 2026 14:44:36 +0530 Subject: [PATCH 3/4] fix(consolidate): audit legacy memory adoption --- src/functions/consolidate.ts | 1 + test/consolidate-publication.test.ts | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/functions/consolidate.ts b/src/functions/consolidate.ts index ee0f8c251a..8f0e1bd00a 100644 --- a/src/functions/consolidate.ts +++ b/src/functions/consolidate.ts @@ -225,6 +225,7 @@ export function registerConsolidateFunction( ); if (legacy) { await kv.set(KV.memories, legacy.id, { ...legacy, consolidationKeys: { [concept]: sourceKey } }); + await safeAudit(kv, "evolve", "mem::consolidate", [legacy.id], { action: "adopt_legacy", concept }); return false; } diff --git a/test/consolidate-publication.test.ts b/test/consolidate-publication.test.ts index c13f697f90..9262df1e45 100644 --- a/test/consolidate-publication.test.ts +++ b/test/consolidate-publication.test.ts @@ -2,6 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { registerConsolidateFunction } from "../src/functions/consolidate.js"; import { KV } from "../src/state/schema.js"; import { withKeyedLock } from "../src/state/keyed-mutex.js"; +import { safeAudit } from "../src/functions/audit.js"; import type { CompressedObservation, Memory, MemoryProvider, Session } from "../src/types.js"; vi.mock("../src/logger.js", () => ({ logger: { info: vi.fn(), warn: vi.fn() } })); @@ -406,6 +407,7 @@ describe("consolidation publication", () => { })); expect(await f.run()).toMatchObject({ consolidated: 0 }); expect((await f.kv.get(KV.memories, "legacy"))!.consolidationKeys?.auth).toBeTruthy(); + expect(safeAudit).toHaveBeenCalledWith(f.kv, "evolve", "mem::consolidate", ["legacy"], { action: "adopt_legacy", concept: "auth" }); const row = await f.kv.get(KV.observations("session-a"), "session-a-auth-0"); await f.kv.set(KV.observations("session-a"), row!.id, { ...row!, narrative: "Recompressed evidence" }); expect(await f.run()).toMatchObject({ consolidated: 1 }); From 76950f310205eeb960683d9baff0015093475d58 Mon Sep 17 00:00:00 2001 From: Rohit Ghumare Date: Sun, 8 Nov 2026 15:05:55 +0530 Subject: [PATCH 4/4] fix(consolidate): record legacy adoption after releasing the publication locks --- src/functions/consolidate.ts | 6 +++++- test/consolidate-publication.test.ts | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/functions/consolidate.ts b/src/functions/consolidate.ts index 8f0e1bd00a..5f778af1dd 100644 --- a/src/functions/consolidate.ts +++ b/src/functions/consolidate.ts @@ -178,6 +178,7 @@ export function registerConsolidateFunction( : undefined; const publicationLocks = ["mem:remember", ...sessionIds.map((sid) => `obs:${sid}`)]; + let adoptedId: string | undefined; const published = await withKeyedLocks(publicationLocks, async () => { for (const sid of sessionIds) { const current = await kv.get(KV.sessions, sid); @@ -225,7 +226,7 @@ export function registerConsolidateFunction( ); if (legacy) { await kv.set(KV.memories, legacy.id, { ...legacy, consolidationKeys: { [concept]: sourceKey } }); - await safeAudit(kv, "evolve", "mem::consolidate", [legacy.id], { action: "adopt_legacy", concept }); + adoptedId = legacy.id; return false; } @@ -273,6 +274,9 @@ export function registerConsolidateFunction( }); return true; }); + if (adoptedId) { + await safeAudit(kv, "evolve", "mem::consolidate", [adoptedId], { action: "adopt_legacy", concept }); + } if (published) consolidated++; } catch (err) { logger.warn("Consolidation failed for concept", { diff --git a/test/consolidate-publication.test.ts b/test/consolidate-publication.test.ts index 9262df1e45..c31edae4cc 100644 --- a/test/consolidate-publication.test.ts +++ b/test/consolidate-publication.test.ts @@ -412,4 +412,22 @@ describe("consolidation publication", () => { await f.kv.set(KV.observations("session-a"), row!.id, { ...row!, narrative: "Recompressed evidence" }); expect(await f.run()).toMatchObject({ consolidated: 1 }); }); + + it("records the legacy adoption after releasing the publication locks", async () => { + const f = fixture(); + await seed(f); + await f.kv.set(KV.memories, "legacy", memory("legacy", { + sourceObservationIds: ["session-a-auth-0", "session-a-auth-1", "session-a-auth-2"], + sessionIds: ["session-a"], + })); + let lockFree = false; + vi.mocked(safeAudit).mockImplementationOnce(async () => { + lockFree = await Promise.race([ + withKeyedLock("mem:remember", async () => true), + new Promise((resolve) => setTimeout(() => resolve(false), 200)), + ]); + }); + await f.run(); + expect(lockFree).toBe(true); + }); });