Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(memory): keep large typed projections mutable

Rahmana74d8a3fd310
diff --git a/docs/COGNITIVE-RUNTIME.md b/docs/COGNITIVE-RUNTIME.md
index 381f1253..a2f2ee0a 100644
--- a/docs/COGNITIVE-RUNTIME.md
+++ b/docs/COGNITIVE-RUNTIME.md
@@ -220,7 +220,7 @@ P1 keeps the two proven planes but upgrades stable agent memory from untyped Mar
- **Agent memory live ledger** — private `records-v1.json` claims keyed by client principal, with semantic/episodic/procedural kind, confidence, sensitivity, temporal validity, provenance authority/channel, supersession/retraction and conflict evidence. Hard safety limits remain 2,000 live records / 2 MiB.
- **Cold finished-history archive** — P10 moves only records that can no longer become effective now or in the future into owner-only content-addressed `archive-v1/segment-<sha>.json` files before the live-ledger swap. Normal resolved reads never load archive content. Explicit history or explicit temporal retrieval can read it, preserving provenance and historical resolution without inflating model-active context.
-- **Markdown compatibility projection** — `USER.md` / `MEMORY.md` remain the resolved materialized view consumed by older CLI/session code. Existing installations are read unchanged; the first structured mutation seeds the ledger from legacy `## key` sections instead of deleting or rewriting history.
+- **Markdown compatibility projection** — `USER.md` / `MEMORY.md` remain the resolved materialized view consumed by older CLI/session code. Existing unstructured legacy documents remain bounded to 64 KiB when imported; once a typed ledger exists, its materialized compatibility projection may grow up to the same 2 MiB safety envelope as the ledger so `remember`/`forget` cannot become impossible merely because resolved Markdown crossed the legacy import limit. Existing installations are read unchanged; the first structured mutation seeds the ledger from legacy `## key` sections instead of deleting or rewriting history.
- **Privacy-safe memory telemetry** — aggregate-only lifecycle diagnostics count live/archived/resolved/superseded/retracted/future/conflict records, correction-depth buckets, bytes and archive segments. They never emit memory values, raw keys, raw conversation ids, bearer data or secrets.
- **Experience memory** — verified workflow trajectories remain client-scoped and now accumulate P1 quality telemetry: completed/failed/denied/rate-limited/invalid-argument steps, retries, rollback/restore evidence and timed-step latency.
diff --git a/lib/agent/memory-ledger.ts b/lib/agent/memory-ledger.ts
index afb3bce0..ef71e20f 100644
--- a/lib/agent/memory-ledger.ts
+++ b/lib/agent/memory-ledger.ts
@@ -5,7 +5,7 @@ import { resolveMemoryLedger } from "./memory-resolution";
import type { AgentMemoryDocument, AgentMemoryLedger, AgentMemoryRecord } from "./memory-types";
const LEDGER_NAME = "records-v1.json";
-const MAX_LEDGER_BYTES = 2 * 1024 * 1024;
+export const MAX_MEMORY_LEDGER_BYTES = 2 * 1024 * 1024;
export const MAX_MEMORY_RECORDS = 2000;
export function ledgerFile(dir: string): string { return path.join(dir, LEDGER_NAME); }
@@ -16,7 +16,7 @@ export async function readMemoryLedger(dir: string): Promise<AgentMemoryLedger |
try {
handle = await fs.open(file, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
const stat = await handle.stat();
- if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_LEDGER_BYTES) throw new Error("agent memory ledger has an invalid file shape");
+ if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_MEMORY_LEDGER_BYTES) throw new Error("agent memory ledger has an invalid file shape");
if ((stat.mode & 0o077) !== 0) throw new Error("agent memory ledger permissions are too broad; expected 0600");
if (typeof process.getuid === "function" && stat.uid !== process.getuid()) throw new Error("agent memory ledger is not owned by the MSO user");
const parsed = JSON.parse(await handle.readFile("utf8")) as AgentMemoryLedger;
@@ -34,7 +34,7 @@ export async function writeMemoryLedger(dir: string, ledger: AgentMemoryLedger):
await fs.chmod(dir, 0o700).catch(() => undefined);
const file = ledgerFile(dir), tmp = `${file}.${randomUUID()}.tmp`;
const body = JSON.stringify(ledger, null, 2);
- if (Buffer.byteLength(body, "utf8") > MAX_LEDGER_BYTES) throw new Error("agent memory ledger exceeds 2 MiB");
+ if (Buffer.byteLength(body, "utf8") > MAX_MEMORY_LEDGER_BYTES) throw new Error("agent memory ledger exceeds 2 MiB");
await fs.writeFile(tmp, body, { encoding: "utf8", mode: 0o600, flag: "wx" });
await fs.chmod(tmp, 0o600); await fs.rename(tmp, file); await fs.chmod(file, 0o600);
}
diff --git a/lib/agent/memory-store.test.ts b/lib/agent/memory-store.test.ts
index 2679abaa..4f954acb 100644
--- a/lib/agent/memory-store.test.ts
+++ b/lib/agent/memory-store.test.ts
@@ -138,6 +138,22 @@ describe("typed provenance-aware agent memory", () => {
}
});
+ it("keeps typed memory mutable after the Markdown projection grows beyond the legacy 64 KiB limit", async () => {
+ const principal = "memory:large-projection";
+ const payload = "x".repeat(7_000);
+ for (let index = 0; index < 12; index++) {
+ await store.rememberAgentMemory(principal, "MEMORY.md", `Large entry ${index}`, `${index}:${payload}`);
+ }
+ const file = path.join(dirFor(principal), "MEMORY.md");
+ expect((await fs.stat(file)).size).toBeGreaterThan(64 * 1024);
+
+ const after = await store.forgetAgentMemory(principal, "MEMORY.md", "Large entry 11");
+ expect(after.memory).not.toContain("## Large entry 11");
+ expect(after.memory).toContain("## Large entry 10");
+ expect((await fs.stat(file)).size).toBeGreaterThan(64 * 1024);
+ expect((await fs.stat(file)).mode & 0o777).toBe(0o600);
+ });
+
it("rejects invalid confidence and invalid temporal windows", async () => {
await expect(store.rememberAgentMemory("memory:bad", "MEMORY.md", "Bad", "x", { confidence: 1.2 })).rejects.toThrow(/confidence/);
await expect(store.rememberAgentMemory("memory:bad", "MEMORY.md", "Bad", "x", { validFrom: "2026-09-02T00:00:00Z", validUntil: "2026-09-01T00:00:00Z" })).rejects.toThrow(/valid_until/);
diff --git a/lib/agent/memory-store.ts b/lib/agent/memory-store.ts
index 0b97b8cf..12453976 100644
--- a/lib/agent/memory-store.ts
+++ b/lib/agent/memory-store.ts
@@ -5,7 +5,7 @@ import os from "node:os";
import path from "node:path";
import { withSecurityStoreLock } from "@/lib/security-store-lock";
import { archiveMemoryRecords, readMemoryArchive } from "./memory-archive";
-import { ledgerFile, materializeDocuments, readMemoryLedger, seedLedger, writeMemoryLedger } from "./memory-ledger";
+import { ledgerFile, materializeDocuments, MAX_MEMORY_LEDGER_BYTES, readMemoryLedger, seedLedger, writeMemoryLedger } from "./memory-ledger";
import { queryMemoryLedger } from "./memory-query";
import { planMemoryRetention } from "./memory-retention";
import { recordCanBeEffectiveAtOrAfter, recordEffectiveAt, resolveMemoryKey } from "./memory-resolution";
@@ -17,7 +17,8 @@ export type { AgentMemoryTelemetry } from "./memory-telemetry";
export interface AgentMemorySnapshot { capturedAt: string; user: string; memory: string; schemaVersion?: 1; recordCount?: number; }
const ROOT = path.resolve(/* turbopackIgnore: true */ process.env.OS_AGENT_MEMORY_DIR || path.join(os.homedir(), ".mso", "agent-memory"));
-const MAX_DOC_BYTES = 64 * 1024;
+const MAX_LEGACY_DOC_BYTES = 64 * 1024;
+const MAX_PROJECTION_BYTES = MAX_MEMORY_LEDGER_BYTES;
const KEY_RE = /^[^\r\n]{1,80}$/;
function principalKey(principal: string): string {
@@ -32,7 +33,7 @@ async function readDocument(principal: string, document: AgentMemoryDocument): P
try {
handle = await fs.open(/* turbopackIgnore: true */ fileFor(principal, document), fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW);
const stat = await handle.stat();
- if (!stat.isFile() || stat.size > MAX_DOC_BYTES) throw new Error("agent memory document has an invalid file shape");
+ if (!stat.isFile() || stat.size > MAX_LEGACY_DOC_BYTES) throw new Error("agent memory document has an invalid file shape");
if ((stat.mode & 0o077) !== 0) throw new Error("agent memory permissions are too broad; expected 0600");
if (typeof process.getuid === "function" && stat.uid !== process.getuid()) throw new Error("agent memory is not owned by the MSO user");
return await handle.readFile("utf8");
@@ -43,7 +44,7 @@ async function readDocument(principal: string, document: AgentMemoryDocument): P
}
async function writeDocument(principal: string, document: AgentMemoryDocument, content: string): Promise<void> {
- if (Buffer.byteLength(content, "utf8") > MAX_DOC_BYTES) throw new Error("agent memory document exceeds 64 KiB");
+ if (Buffer.byteLength(content, "utf8") > MAX_PROJECTION_BYTES) throw new Error("agent memory projection exceeds 2 MiB");
const file = fileFor(principal, document), dir = path.dirname(file), tmp = `${file}.${randomUUID()}.tmp`;
await fs.mkdir(dir, { recursive: true, mode: 0o700 });
await fs.chmod(ROOT, 0o700).catch(() => undefined); await fs.chmod(dir, 0o700).catch(() => undefined);