Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: harden persistent mind accounting

[._.]/ Adam Eivye1bc022456b0
diff --git a/server/services/agentRunEventLog.js b/server/services/agentRunEventLog.js
index c1c23d0b09..7ff66be68c 100644
--- a/server/services/agentRunEventLog.js
+++ b/server/services/agentRunEventLog.js
@@ -578,6 +578,7 @@ export async function getRunDiagnostic(id) {
* are held to, so "why is this run missing" has an answer that isn't a guess.
*/
export async function getRunEventLedgerStats() {
+ await hydrate();
await schedulePrune();
const [archive, active, mindArchive, mindActive] = await Promise.all([
readJSONLFile(ARCHIVE_PATH),
@@ -588,8 +589,8 @@ export async function getRunEventLedgerStats() {
// Counted through the SAME predicate the read path uses, so "stats say 40
// events" and "the events endpoint returns 40" can never disagree.
const cutoff = expiryCutoff();
- const freshArchive = archive.filter((e) => isRetained(e, cutoff));
- const freshActive = active.filter((e) => isRetained(e, cutoff));
+ const freshArchive = archive.filter((e) => isRetained(e, cutoff) && !isMindShapedEvent(e));
+ const freshActive = active.filter((e) => isRetained(e, cutoff) && !isMindShapedEvent(e));
const oldest = freshArchive[0]?.at ?? freshActive[0]?.at ?? null;
return {
activeEvents: freshActive.length,
@@ -597,8 +598,8 @@ export async function getRunEventLedgerStats() {
maxActiveEvents: MAX_ACTIVE_EVENTS,
maxRetainedEvents: MAX_ACTIVE_EVENTS * 2,
maxEventAgeDays: MAX_EVENT_AGE_DAYS,
- mindActiveEvents: mindActive.filter((event) => isStoredRunEvent(event) && isPersistentMindEventKind(event.kind)).length,
- mindArchivedEvents: mindArchive.filter((event) => isStoredRunEvent(event) && isPersistentMindEventKind(event.kind)).length,
+ mindActiveEvents: mindActive.filter(isMindShapedEvent).length,
+ mindArchivedEvents: mindArchive.filter(isMindShapedEvent).length,
maxActiveMindEvents: MAX_ACTIVE_MIND_EVENTS,
maxRetainedMindEvents: MAX_ACTIVE_MIND_EVENTS * 2,
persistentMindAgeBounded: false,
diff --git a/server/services/agentRunEventLog.test.js b/server/services/agentRunEventLog.test.js
index cdd21fec38..dbc2889c13 100644
--- a/server/services/agentRunEventLog.test.js
+++ b/server/services/agentRunEventLog.test.js
@@ -344,6 +344,12 @@ describe('retention / rotation bound', () => {
writeFileSync(ACTIVE, `${JSON.stringify(ordinary)}\n${JSON.stringify(legacyMind)}\n`);
restartServer();
+ expect(await getRunEventLedgerStats()).toMatchObject({
+ archivedEvents: 0,
+ activeEvents: 1,
+ mindArchivedEvents: 0,
+ mindActiveEvents: 1,
+ });
expect((await readRunEvents()).map((event) => event.runId)).toEqual(['ordinary']);
expect((await readPersistentMindHistory()).map((event) => event.eventId)).toEqual(['legacy-mind']);
expect(countLines(ACTIVE)).toBe(1);
@@ -365,6 +371,7 @@ describe('retention / rotation bound', () => {
restartServer();
expect((await readPersistentMindHistory()).map((event) => event.kind)).toEqual(['mind.future-boundary']);
+ expect((await getRunEventLedgerStats()).mindActiveEvents).toBe(1);
});
});
diff --git a/server/services/persistentMindContext.js b/server/services/persistentMindContext.js
index 5d8cd380d5..07758e7822 100644
--- a/server/services/persistentMindContext.js
+++ b/server/services/persistentMindContext.js
@@ -113,7 +113,7 @@ export async function preparePersistentMindContext({
const older = history.slice(0, Math.max(0, history.length - Math.max(1, recentEventLimit)));
let coverageGap = null;
- if (older.length > 0 && typeof summarize === 'function') {
+ if (older.length > 0) {
const previous = latestReadyRollup(rollups, promptVersion);
const coveredThrough = previous?.source.toSequence ?? -1;
const rangeEvents = older.filter((event) => event.sequence > coveredThrough);
@@ -143,7 +143,7 @@ export async function preparePersistentMindContext({
};
const rollupId = `${mindId}:${source.fromSequence}-${source.toSequence}:v${promptVersion}`;
const alreadyAttempted = rollups.some((rollup) => rollup.id === rollupId);
- if (!coverageGap && (forceSummary || !alreadyAttempted)) {
+ if (!coverageGap && typeof summarize === 'function' && (forceSummary || !alreadyAttempted)) {
const outcome = await summaryOutcome(summarize, {
mindId,
source,
diff --git a/server/services/persistentMindContext.test.js b/server/services/persistentMindContext.test.js
index bd9cef9aac..6b36d79a0e 100644
--- a/server/services/persistentMindContext.test.js
+++ b/server/services/persistentMindContext.test.js
@@ -180,11 +180,9 @@ describe('persistent mind rollups', () => {
it('reports a retained-history gap instead of claiming complete rollup coverage', async () => {
mock.history = [event(2), event(3), event(4)];
- const summarize = vi.fn(async () => 'This must not be recorded as complete.');
- const context = await preparePersistentMindContext({ recentEventLimit: 1, summarize });
+ const context = await preparePersistentMindContext({ recentEventLimit: 1 });
- expect(summarize).not.toHaveBeenCalled();
expect(await readPersistentMindRollups()).toEqual([]);
expect(context).toMatchObject({
summaryState: 'gap',