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.

feat(memory): preserve session sources and expose owner second brain

Rahmanf41856765f76
diff --git a/app/api/v1/memory-graph/route.ts b/app/api/v1/memory-graph/route.ts
index e228efb0..6148592f 100644
--- a/app/api/v1/memory-graph/route.ts
+++ b/app/api/v1/memory-graph/route.ts
@@ -10,10 +10,11 @@ export async function GET(req: NextRequest) {
if (!(await verifyAuth(req))) return NextResponse.json({ error: "unauthorized" }, { status: 401 });
const context = await getSessionContext();
if (!context) return NextResponse.json({ error: "unauthorized" }, { status: 401 });
+ if (context.role !== "owner") return NextResponse.json({ error: "owner_required" }, { status: 403 });
const root = (req.nextUrl.searchParams.get("root") ?? process.env.OS_MEMORY_GRAPH_ROOT ?? "").trim().slice(0, 500);
const project = (req.nextUrl.searchParams.get("project") ?? "").trim().slice(0, 200);
try {
- const graph = await collectMemoryGraph({ root, project, principal: `cli:${context.session.device_id}` });
+ const graph = await collectMemoryGraph({ root, project, principal: `cli:${context.session.device_id}`, ownerView: true });
return NextResponse.json(graph, { headers: { "Cache-Control": "no-store" } });
} catch (error) {
const message = error instanceof Error ? error.message : "memory graph failed";
diff --git a/app/api/v1/sys/cleanup/route.test.ts b/app/api/v1/sys/cleanup/route.test.ts
new file mode 100644
index 00000000..89d7e867
--- /dev/null
+++ b/app/api/v1/sys/cleanup/route.test.ts
@@ -0,0 +1,20 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+const mocks = vi.hoisted(() => ({ role: "owner", run: vi.fn(async () => [{ id: "npm-cache", ok: true, freedBytes: 1 }]) }));
+vi.mock("@/lib/agent/server", () => ({ verifyAuth: async () => true }));
+vi.mock("@/lib/auth/require-session", () => ({ getSessionContext: async () => ({ role: mocks.role, session: { device_id: "test" } }) }));
+vi.mock("@/lib/host/request-api", () => ({ readJson: (req: Request) => req.json(), apiError: (_: string, error: Error) => Response.json({ error: error.message }, { status: 400 }) }));
+vi.mock("@/lib/host/audit-api", () => ({ audit: vi.fn() }));
+vi.mock("@/lib/host/cleanup", () => ({ runCleanup: mocks.run, scanCleanup: async () => [{ id: "npm-cache", available: true, bytes: 1 }, { id: "tmp-old", available: true, bytes: 1 }] }));
+import { GET, POST } from "./route";
+beforeEach(() => { mocks.role = "owner"; mocks.run.mockClear(); });
+const request = (body: unknown) => new Request("http://localhost/api/v1/sys/cleanup", { method: "POST", body: JSON.stringify(body) });
+describe("cleanup owner guard and preview", () => {
+ it("denies operators and never calls the cleanup kernel", async () => { mocks.role = "operator"; expect((await GET(request({}))).status).toBe(403); expect((await POST(request({ ids: ["npm-cache"], confirm: true }))).status).toBe(403); expect(mocks.run).not.toHaveBeenCalled(); });
+ it("blocks protected paths and requires a server-issued preview", async () => {
+ const preview = await (await GET(request({}))).json(); expect(preview.items.find((item: {id: string}) => item.id === "tmp-old").available).toBe(false);
+ expect((await POST(request({ ids: ["tmp-old"], preview_id: preview.preview.id, confirm: true }))).status).toBe(400);
+ expect((await POST(request({ ids: ["npm-cache"], preview_id: "forged", confirm: true }))).status).toBe(400);
+ expect(mocks.run).not.toHaveBeenCalled();
+ expect((await POST(request({ ids: ["npm-cache"], preview_id: preview.preview.id, confirm: true }))).status).toBe(200); expect(mocks.run).toHaveBeenCalledOnce();
+ });
+});
diff --git a/app/api/v1/sys/cleanup/route.ts b/app/api/v1/sys/cleanup/route.ts
index 5a808741..a8a0e3bd 100644
--- a/app/api/v1/sys/cleanup/route.ts
+++ b/app/api/v1/sys/cleanup/route.ts
@@ -1,47 +1,42 @@
import { NextResponse } from "next/server";
import { verifyAuth } from "@/lib/agent/server";
+import { getSessionContext } from "@/lib/auth/require-session";
import { apiError, readJson } from "@/lib/host/request-api";
import { audit } from "@/lib/host/audit-api";
import { scanCleanup, runCleanup } from "@/lib/host/cleanup";
+import { issueCleanupPreview, consumeCleanupPreview } from "@/lib/host/cleanup-preview";
export const dynamic = "force-dynamic";
+const protectedCategories = new Set(["tmp-old"]);
-// GET /sys/cleanup → reclaimable bytes per safe cleanup category.
-export async function GET(req: Request) {
- if (!(await verifyAuth(req)))
- return NextResponse.json({ error: "unauthorized" }, { status: 401 });
+async function owner(req: Request) {
+ return (await verifyAuth(req)) && (await getSessionContext())?.role === "owner";
+}
+export async function GET(req: Request) {
+ if (!(await owner(req))) return NextResponse.json({ error: "owner_required" }, { status: 403 });
try {
- return NextResponse.json({ items: await scanCleanup() });
- } catch (e) {
- return apiError("sys/cleanup", e);
- }
+ const items = (await scanCleanup()).map((item) => protectedCategories.has(item.id) ? {
+ ...item, available: false,
+ desc: "Protected: file age does not prove that sessions, evidence or builds no longer need it.",
+ } : item);
+ return NextResponse.json({
+ items, preview: issueCleanupPreview(items),
+ protected: ["session records", "memories", "evidence", "archives", "volumes", "unreviewed temporary files"],
+ }, { headers: { "Cache-Control": "private, no-store" } });
+ } catch (e) { return apiError("sys/cleanup", e); }
}
-// POST /sys/cleanup {ids:[...]} → run the selected categories. ids are matched
-// against the fixed server-side allowlist; anything else is reported back as
-// unknown, never executed.
export async function POST(req: Request) {
- if (!(await verifyAuth(req)))
- return NextResponse.json({ error: "unauthorized" }, { status: 401 });
-
+ if (!(await owner(req))) return NextResponse.json({ error: "owner_required" }, { status: 403 });
try {
- const body = (await readJson(req)) as { ids?: unknown };
- const ids = Array.isArray(body.ids)
- ? body.ids.filter((x): x is string => typeof x === "string").slice(0, 32)
- : [];
- if (!ids.length)
- return NextResponse.json({ error: "ids required" }, { status: 400 });
+ const body = (await readJson(req)) as { ids?: unknown; preview_id?: unknown; confirm?: unknown };
+ const ids = Array.isArray(body.ids) ? body.ids.filter((x): x is string => typeof x === "string").slice(0, 32) : [];
+ if (ids.some((id) => protectedCategories.has(id))) return NextResponse.json({ error: "protected_category" }, { status: 400 });
+ consumeCleanupPreview(body.preview_id, ids, body.confirm);
const results = await runCleanup(ids);
- const freed = results.reduce((a, r) => a + r.freedBytes, 0);
- audit({
- action: "sys.cleanup",
- target: ids.join(","),
- ok: results.every((r) => r.ok),
- detail: `freed ${freed} bytes`,
- });
+ const freed = results.reduce((sum, row) => sum + row.freedBytes, 0);
+ audit({ action: "sys.cleanup", target: ids.join(","), ok: results.every((row) => row.ok), detail: `freed ${freed} bytes` });
return NextResponse.json({ results });
- } catch (e) {
- return apiError("sys/cleanup", e);
- }
+ } catch (e) { return apiError("sys/cleanup", e); }
}
diff --git a/app/api/v1/sys/memory-backup/route.test.ts b/app/api/v1/sys/memory-backup/route.test.ts
new file mode 100644
index 00000000..25025849
--- /dev/null
+++ b/app/api/v1/sys/memory-backup/route.test.ts
@@ -0,0 +1,23 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+const mocks = vi.hoisted(() => ({ role: "owner", preview: vi.fn(async () => ({ files: 0 })), create: vi.fn(async () => ({ id: "snapshot" })), verify: vi.fn(async () => ({ id: "snapshot", integrity: true })) }));
+vi.mock("@/lib/agent/server", () => ({ verifyAuth: async () => true }));
+vi.mock("@/lib/auth/require-session", () => ({ getSessionContext: async () => ({ role: mocks.role, session: { device_id: "owner" } }) }));
+vi.mock("@/lib/host/request-api", () => ({ readJson: (req: Request) => req.json(), apiError: (_: string, error: Error) => Response.json({ error: error.message }, { status: 400 }) }));
+vi.mock("@/lib/host/audit-api", () => ({ audit: async () => undefined }));
+vi.mock("@/lib/host/limits-api", () => ({ rateLimited: () => false }));
+vi.mock("@/lib/host/memory-backup-api", () => ({ previewMemoryBackup: mocks.preview, createMemoryBackup: mocks.create, verifyMemoryBackup: mocks.verify }));
+import { GET, POST } from "./route";
+const req = (body: unknown) => new Request("http://localhost/api/v1/sys/memory-backup", { method: "POST", body: JSON.stringify(body) });
+beforeEach(() => { mocks.role = "owner"; vi.clearAllMocks(); });
+describe("server memory backup owner API", () => {
+ it("denies nonowners before preview, backup or restore", async () => { mocks.role = "operator"; expect((await GET(req({}))).status).toBe(403); expect((await POST(req({ action: "create", confirm: true }))).status).toBe(403); expect(mocks.create).not.toHaveBeenCalled(); expect(mocks.preview).not.toHaveBeenCalled(); });
+ it("requires explicit confirmation and never accepts client source paths", async () => {
+ expect((await POST(req({ action: "create" }))).status).toBe(400); expect(mocks.create).not.toHaveBeenCalled();
+ expect((await POST(req({ action: "create", confirm: true, source: "/etc", destination: "/tmp" }))).status).toBe(200);
+ expect(mocks.create).toHaveBeenCalledWith();
+ });
+ it("verifies only a referenced backup plus its expected manifest hash", async () => {
+ expect((await POST(req({ action: "verify", confirm: true, id: "backup-id", manifest_sha256: "a".repeat(64) }))).status).toBe(200);
+ expect(mocks.verify).toHaveBeenCalledWith("backup-id", "a".repeat(64));
+ });
+});
diff --git a/app/api/v1/sys/memory-backup/route.ts b/app/api/v1/sys/memory-backup/route.ts
new file mode 100644
index 00000000..8122ea5d
--- /dev/null
+++ b/app/api/v1/sys/memory-backup/route.ts
@@ -0,0 +1,33 @@
+import { NextResponse } from "next/server";
+import { verifyAuth } from "@/lib/agent/server";
+import { getSessionContext } from "@/lib/auth/require-session";
+import { readJson, apiError } from "@/lib/host/request-api";
+import { audit } from "@/lib/host/audit-api";
+import { rateLimited } from "@/lib/host/limits-api";
+import { previewMemoryBackup, createMemoryBackup, verifyMemoryBackup } from "@/lib/host/memory-backup-api";
+export const runtime = "nodejs";
+export const dynamic = "force-dynamic";
+const headers = { "Cache-Control": "private, no-store" };
+async function owner(req: Request) {
+ if (!(await verifyAuth(req))) return null;
+ const context = await getSessionContext();
+ return context?.role === "owner" ? context : null;
+}
+export async function GET(req: Request) {
+ if (!(await owner(req))) return NextResponse.json({ error: "owner_required" }, { status: 403, headers });
+ try { return NextResponse.json(await previewMemoryBackup(), { headers }); }
+ catch (error) { return apiError("sys/memory-backup", error); }
+}
+export async function POST(req: Request) {
+ const context = await owner(req);
+ if (!context) return NextResponse.json({ error: "owner_required" }, { status: 403, headers });
+ if (rateLimited(`memory-backup:${context.session.device_id}`, 3, 60_000)) return NextResponse.json({ error: "rate_limited" }, { status: 429, headers });
+ try {
+ const body = await readJson(req) as { action?: unknown; confirm?: unknown; id?: unknown; manifest_sha256?: unknown };
+ if (body.confirm !== true) return NextResponse.json({ error: "explicit_confirmation_required" }, { status: 400, headers });
+ if (body.action !== "create" && body.action !== "verify") return NextResponse.json({ error: "unknown_action" }, { status: 400, headers });
+ const result = body.action === "create" ? await createMemoryBackup() : await verifyMemoryBackup(String(body.id ?? ""), String(body.manifest_sha256 ?? ""));
+ await audit({ action: "sys.memory-backup", target: result.id, ok: true, detail: String(body.action) });
+ return NextResponse.json(result, { headers });
+ } catch (error) { return apiError("sys/memory-backup", error); }
+}
diff --git a/app/api/v1/workflows/route.ts b/app/api/v1/workflows/route.ts
index 1969f6d5..df121ac4 100644
--- a/app/api/v1/workflows/route.ts
+++ b/app/api/v1/workflows/route.ts
@@ -1,3 +1,4 @@
+import { learningPage } from "@/lib/workflow/learning-page";
import { discoverOwnerGraphs, cloneOwnerGraph } from "@/lib/workflow/owner-discovery";
import { NextRequest, NextResponse } from "next/server";
import { getSessionContext } from "@/lib/auth/require-session";
@@ -22,8 +23,7 @@ import { workflowTemplate, workflowTemplates } from "@/lib/workflow/templates";
import { suggestWorkflowGraph } from "@/lib/workflow/ai-assist";
import { workflowMatchesQuery } from "@/lib/workflow/search";
import { resolveProjectHint } from "@/lib/host/projects-api";
-import { listAutomationScripts, readAutomationScript } from "@/lib/orchestration/repo-memory-artifacts";
-import { listLearnedRecipes, recipeMaturity } from "@/lib/workflow";
+import { listAutomationScripts } from "@/lib/orchestration/repo-memory-artifacts";
import { createWorkflowDataTable, deleteWorkflowDataTable, deleteWorkflowDataTableRow, getWorkflowDataTable, listWorkflowDataTables, upsertWorkflowDataTableRow } from "@/lib/workflow/data-table-store";
import { optimizeWorkflowGraph } from "@/lib/workflow/graph-optimizer";
import { createJevWorkflowOptimizerEvaluator } from "@/lib/workflow/jev-optimizer";
@@ -43,7 +43,7 @@ export async function GET(req:NextRequest){const q=req.nextUrl.searchParams,mini
return NextResponse.json({tools,workflows:graphs.filter((graph)=>!query||`${graph.name} ${graph.description} ${(graph.metadata.tags??[]).join(" ")}`.toLowerCase().includes(query)).map((graph)=>({id:graph.id,name:graph.name,status:graph.status,nodeCount:graph.nodes.length,updatedAt:graph.updatedAt})),sessions:sessions.filter((row)=>!query||`${agentSessionLabel(row.name,row.title,row.cwd)} ${row.title} ${row.name} ${row.cwd??""}`.toLowerCase().includes(query)).map((row)=>({...row,label:agentSessionLabel(row.name,row.title,row.cwd)})),projects,skills},{headers});
}
if(q.get("scripts")==="1"){const hint=q.get("project")?.trim();if(!hint)throw new Error("project is required");const project=await resolveProjectHint(hint);if(!project||project.matchedBy==="fuzzy")throw new Error("exact project not found");const query=(q.get("q")??"").toLowerCase().trim(),scripts=(await listAutomationScripts(project.path)).filter((script)=>!query||`${script.id} ${script.intent} ${script.status} ${script.steps.map((step)=>step.tool).join(" ")}`.toLowerCase().includes(query)).map((script)=>({id:script.id,intent:script.intent,status:script.status,stepCount:script.steps.length,updatedAt:script.updatedAt,project:script.project,tools:script.steps.map((step)=>step.tool)}));return NextResponse.json({project:project.id,scripts},{headers});}
- if(q.get("learning")==="1"){if(!roleAtLeast(session.context.role,"owner"))return fail("owner_required",403);const recipes=(await listLearnedRecipes({ownerView:true})).slice(0,100);const rows=await Promise.all(recipes.map(async(recipe)=>{const maturity=recipeMaturity(recipe),sources=[...new Map(recipe.bestSteps.map((step)=>step.provenance).filter(Boolean).map((source)=>[source!.sessionLabel,source!])).values()];let scriptStatus:"candidate"|"tested"|undefined;if(recipe.project){const project=await resolveProjectHint(recipe.project).catch(()=>null);if(project&&project.matchedBy!=="fuzzy")scriptStatus=(await readAutomationScript(project.path,`script_${recipe.id}`).catch(()=>null))?.status;}return{id:recipe.id,intent:recipe.intent,project:recipe.project,maturity:maturity.maturity,stage:scriptStatus==="tested"?"tested":maturity.maturity,scriptStatus,attempts:recipe.attempts,successes:recipe.successes,failures:recipe.failures,successRate:Math.round(maturity.successRate*1000)/10,fastestDurationMs:recipe.fastestDurationMs,updatedAt:recipe.updatedAt,sourceSessions:sources.map((source)=>({label:source.sessionLabel,actionRef:source.actionRef,eventRef:source.eventRef,artifactRefs:source.artifactRefs??[]})),forge:{eligible:recipe.qualityVersion===1&&recipe.successes>=2&&recipe.attempts>0&&recipe.successes/recipe.attempts>=0.9&&recipe.bestSteps.length>0&&recipe.bestSteps.every((step)=>step.state==="completed"),promotion:"explicit" as const}};}));return NextResponse.json({recipes:rows},{headers});}
+ if(q.get("learning")==="1"){if(!roleAtLeast(session.context.role,"owner"))return fail("owner_required",403);return NextResponse.json(await learningPage({offset:Number(q.get("offset"))||0,limit:Number(q.get("limit"))||40,includeArchived:q.get("include_archived")==="1",sessionLabel:q.get("session_label")?.slice(0,200)||undefined}),{headers});}
if(q.get("catalog")==="1")return NextResponse.json({nodes:workflowNodeCatalog(q.get("q")??"")},{headers});
if(q.get("templates")==="1")return NextResponse.json({templates:workflowTemplates()},{headers});
if(q.get("variables")==="1")return NextResponse.json({variables:await listWorkflowVariables(session.principal)},{headers});
@@ -57,7 +57,7 @@ export async function GET(req:NextRequest){const q=req.nextUrl.searchParams,mini
const graphs=await listWorkflowGraphs(session.principal),query=q.get("q")??"",filters={tag:q.get("tag")||undefined,status:q.get("status")||undefined,project:q.get("project")||undefined,folder:q.get("folder")||undefined,node:q.get("node")||undefined}; return NextResponse.json({graphs:graphs.filter((graph)=>workflowMatchesQuery(graph,query,filters))},{headers});
}catch(error){return fail(error);}}
export async function POST(req:NextRequest){const session=await auth("operator");if(!session)return fail("operator_required",403);try{const body=await readSetupJson(req),action=typeof body.action==="string"?body.action:"";if(rateLimited(`workflow:${action}:${session.context.session.device_id}`,action==="run"?60:30,60_000))return fail("rate_limited",429);
- if(action==="owner_clone"){if(session.context.role!=="owner")return fail("owner_required",403);return NextResponse.json({graph:await cloneOwnerGraph(session.context.role,session.principal,String(body.origin_owner??""),String(body.graph_id??""))},{headers});}
+ if(action==="owner_clone"){if(session.context.role!=="owner")return fail("owner_required",403);if(body.confirm!==true)return fail("explicit_confirmation_required",400);return NextResponse.json({graph:await cloneOwnerGraph(session.context.role,session.principal,String(body.origin_owner??""),String(body.graph_id??""),String(body.expected_revision??""))},{headers});}
if(action==="create")return NextResponse.json({graph:await createWorkflowGraph(session.principal,body.graph,"create",{remember:true})},{headers});
if(action==="update")return NextResponse.json({graph:await updateWorkflowGraph(session.principal,String(body.graph_id??""),String(body.expected_revision??""),body.graph,"update",{remember:true})},{headers});
if(action==="delete")return NextResponse.json(await deleteWorkflowGraph(session.principal,String(body.graph_id??""),String(body.expected_revision??"")),{headers});
diff --git a/bin/mso b/bin/mso
index 18e79f05..d7707b02 100755
--- a/bin/mso
+++ b/bin/mso
@@ -39,7 +39,8 @@
# units [query] systemd service inventory (system + user)
# unit * logs | start | stop | restart for exact allowlisted units
# packages Cached package-update visibility (never applies updates)
-# cleanup [--run] Reclaimable junk; --run actually deletes it
+# cleanup [--run] Preview categories; writes require preview_id, confirm and selected ids
+# memory-backup * Local memory snapshot: preview | create | verify (memory-backup --help)
# status Public service status (no session needed)
# health Is this CLI's session valid?
# doctor [--fix] Check deps, HTTPS/login origin, service, session, device; --fix repairs safe local issues
@@ -194,4 +195,4 @@ source "$ROOT/scripts/mso-cli-agent.sh"
# shellcheck source=../scripts/cli/commands.sh
source "$ROOT/scripts/cli/commands.sh"
-mso_cli_main "$@"
+if [ "${1-}" = "memory-backup" ]; then source "$ROOT/scripts/cli/memory-backup.sh"; shift; mso_memory_backup "$@"; else mso_cli_main "$@"; fi
diff --git a/bin/mso.test.ts b/bin/mso.test.ts
index 744d531c..712194aa 100644
--- a/bin/mso.test.ts
+++ b/bin/mso.test.ts
@@ -11,6 +11,7 @@ const run = (...args: string[]) =>
execFileSync(CLI, args, { encoding: "utf8", env: { ...process.env, MSO_ENV: "/dev/null" } });
const commandOwnerFiles = [
"scripts/cli/commands-host.sh",
+ "scripts/cli/memory-backup.sh",
"scripts/cli/commands-runtime.sh",
"scripts/cli/flows.sh",
"scripts/cli/workflows.sh",
@@ -57,6 +58,8 @@ describe("bin/mso", () => {
expect(undocumented).toEqual([]);
});
+ it("documents the owner-only memory backup command", () => { expect(run("memory-backup", "--help")).toContain("create --confirm"); });
+
it("prints memory-graph usage instead of the assistant memory verb", () => {
expect(run("memory-graph", "--help")).toContain("memory-graph [project]");
});
diff --git a/docs/CLI.md b/docs/CLI.md
index e1e75a56..a2032c40 100644
--- a/docs/CLI.md
+++ b/docs/CLI.md
@@ -79,7 +79,8 @@ Commands:
units [query] systemd service inventory (system + user)
unit * logs | start | stop | restart for exact allowlisted units
packages Cached package-update visibility (never applies updates)
- cleanup [--run] Reclaimable junk; --run actually deletes it
+ cleanup [--run] Preview categories; writes require preview_id, confirm and selected ids
+ memory-backup * Local memory snapshot: preview | create | verify (memory-backup --help)
status Public service status (no session needed)
health Is this CLI's session valid?
doctor [--fix] Check deps, HTTPS/login origin, service, session, device; --fix repairs safe local issues
diff --git a/docs/MEMORY-GRAPH.md b/docs/MEMORY-GRAPH.md
index 7a88b026..f9ca52ef 100644
--- a/docs/MEMORY-GRAPH.md
+++ b/docs/MEMORY-GRAPH.md
@@ -4,7 +4,10 @@ Owner-only knowledge graph inside the MSO shell. Open it from Launchpad or Spotl
The graph is built on the server by `GET /api/v1/memory-graph` and rendered in the browser. It does not use Convex, and it does not require Obsidian. Layout ideas — web (force), radial rings, layered hop columns, a local neighbourhood, ghost nodes for unresolved `[[wikilinks]]`, and folder/group color — follow [open-silong](https://github.com/rahmanef63/open-silong) `frontend/slices/memory-graph` (MIT). The implementation is MSO-native.
-This is separate from the Workflow session graph.
+This is separate from the Workflow session graph. The pinned Memory app also
+projects bounded owner-only Organization, saved workflow and learned-recipe
+metadata with links back to their source apps. See [Memory safety](./MEMORY-SAFETY.md)
+for the exact scope, privacy boundaries and remaining limits.
## What becomes a node
diff --git a/docs/MEMORY-SAFETY.md b/docs/MEMORY-SAFETY.md
new file mode 100644
index 00000000..3c170043
--- /dev/null
+++ b/docs/MEMORY-SAFETY.md
@@ -0,0 +1,87 @@
+# Memory preservation and second-brain visibility
+
+## Source protection
+
+Age is not proof that a session was learned. The current preservation policy
+fails closed: unreviewed, pinned and reviewed session source files remain
+protected from automatic archive/artifact/action-index deletion. Review alone
+is not authorization to destroy source evidence. The record model supports
+review provenance, but there is no owner UI for releasing sources yet.
+
+Compaction still archives sanitized raw context. Evicted action-index records
+are stored in bounded immutable segments before leaving the hot index. Lookup
+is bounded; persisted history is not a promise of an unlimited single response.
+
+This deliberately grows storage until explicit release criteria and verified
+backup coverage are available. It is not a disk reclamation mechanism.
+
+## Cleanup
+
+Settings > Cleanup requires an Owner session, a fresh five-minute server preview,
+explicit category selection and confirmation. No category is preselected.
+Broad `/tmp` age-only cleanup is blocked by the owner API. Trash and old logs
+remain irreversible, and displayed estimates are not guaranteed freed bytes.
+Primary session records, memory stores and volumes are not cleanup targets.
+
+The underlying legacy Docker category still combines image and build-cache
+pruning. It must not be described as build-cache-only. CLI/raw API mutations
+must include the fresh `preview_id`, selected `ids`, and `confirm: true`.
+
+## Local server-memory backup
+
+Settings > Backup separates **Server memory backup** from **Browser backup**.
+The server endpoint is owner-only. It accepts no arbitrary client source or
+destination paths and never returns backed-up contents.
+
+```bash
+mso memory-backup preview
+mso memory-backup create --confirm
+mso memory-backup verify <id> <manifest-sha256> --confirm
+```
+
+The allowlist covers configured typed memory, session archives, recipes,
+organization data, canonical project `.agent`/knowledge sources, and sessions.
+Core memory/project evidence precede the larger session inventory. Credential
+stores, browser profiles, known config/lock/temp paths are excluded. Skill
+catalogs, databases and every other VPS directory are not covered by this scope.
+
+Snapshots are private local copies under `~/.mso/backups/memory/<id>` with
+compressed per-file checksums and a manifest checksum. Verification restores
+into a NEW isolated directory and checks the bytes again; it never overwrites
+originals. Missing sources, exclusions, rejected files and truncation are
+reported. A partial snapshot remains partial after an integrity-successful
+restore. Concurrent input files can change; this is a per-file verified copy,
+not a transactional point-in-time snapshot, and it is not offsite protection.
+
+Limits: 10,000 files, 40,000 scan entries, 1 GiB input, 32 MiB/file and a 45-second
+scan budget. Full resumable inventory/backup and snapshot-history UI remain open.
+No backup result grants deletion permission.
+
+## Visibility and access
+
+Workflows > Sources is owner-only discovery across graph principals. Metadata
+retains origin; existing client/MCP graph reads remain principal-private. A
+foreign workflow can only be copied explicitly into an inactive review draft
+with action nodes disabled and its source digest preserved. Source mutations
+and copying stale revisions are refused.
+
+Session details > Self-improve fetches linked active/archived recipes, filters
+best/latest-run provenance before pagination, and distinguishes errors from
+empty results. Graph-cap/persistence warnings have durable receipts when the
+receipt store is writable. This is not proof that every historical session was
+learned or that every procedural-memory write succeeded.
+
+Memory is pinned on the first home page. The owner projection includes bounded
+Organization, workflow, recipe and typed-agent metadata, source navigation and
+honest truncation notices. Private agent values are omitted. This does not
+widen retrieval grants for agents, operators or MCP clients.
+
+## Remaining integrations and release gates
+
+Jev currently remains a Workflow Optimizer adapter, not a memory-admission
+selector. Real endpoint/token verification and a separate review-first memory
+admission design are required. No model may authorize evidence deletion.
+
+New source behavior becomes active only after the normal verified build and
+release process. Passing unit/type/architecture tests alone is not production
+or browser acceptance. Keep tested, merged, deployed and live-verified distinct.
diff --git a/docs/PROGRESS.md b/docs/PROGRESS.md
index d705e513..ed04cb88 100644
--- a/docs/PROGRESS.md
+++ b/docs/PROGRESS.md
@@ -1,3 +1,17 @@
+## 2026-09-26 — Memory safety and second-brain visibility candidate (not released)
+
+Implemented fail-closed session-source preservation, preview/confirmation-first
+owner cleanup, private local server-memory snapshots with isolated restore
+verification, owner-only cross-principal graph discovery, paginated archived
+learning with explicit errors and graph-persistence warnings, and bounded
+Organization/workflow/recipe links in the pinned Memory app.
+
+This is a candidate, not a deployed or browser-verified release. Full resumable
+backup, source-release review UI, cache-only cleanup separation and Jev memory
+admission remain open. No session, archive, memory, evidence or Docker volume was
+deleted by this work. See [Memory safety](./MEMORY-SAFETY.md) for behavior,
+interfaces and honest limits; operational receipts remain outside public source.
+
## 2026-09-25 — Native Google Integrations foundation
Native Google OAuth app configuration, Search Console and Google Analytics 4 now
diff --git a/docs/README.md b/docs/README.md
index 245584fa..c771dac4 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -26,6 +26,7 @@ evidence and review freshness. The repository quality gate runs both automatical
| [`AGENT-BOOTSTRAP.md`](./AGENT-BOOTSTRAP.md) | Canonical first-call sequence and capability-parity matrix across CLI, MCP, and Alfa |
| [`RASMIC.md`](./RASMIC.md) | Risk-aware orchestration, repo-local memory/evidence, collision detection and recipe/script promotion |
| [`MEMORY-GRAPH.md`](./MEMORY-GRAPH.md) | Owner Memory app: vault notes, project knowledge, agent memory, layouts and limits |
+| [`MEMORY-SAFETY.md`](./MEMORY-SAFETY.md) | Session-source preservation, owner cleanup previews, local memory backups, learning/discovery visibility and remaining limits |
| [`COMPARISON.md`](./COMPARISON.md) | Generated product comparison methodology, evidence and notes |
| [`COMPETITIVE-ROADMAP.md`](./COMPETITIVE-ROADMAP.md) | Executed comparison plan, deliberate specialist boundaries and next investments |
| [`INSTALL.md`](./INSTALL.md) | Owner installation, TLS, updates, backup and uninstall |
diff --git a/frontend/slices/memory-graph/components/graph-inspector.tsx b/frontend/slices/memory-graph/components/graph-inspector.tsx
index f4ac30ff..acf104ef 100644
--- a/frontend/slices/memory-graph/components/graph-inspector.tsx
+++ b/frontend/slices/memory-graph/components/graph-inspector.tsx
@@ -13,7 +13,8 @@ export function GraphInspector({ node, onOpen }: { node: MemoryGraphNode | null;
<p className="text-xs text-muted-foreground">{node.kind} · {node.group} · {node.degree} links</p>
{node.kind === "ghost" ? <p className="text-xs">Unresolved wikilink. No note in this graph uses that title.</p> : null}
{node.excerpt ? <p className="text-xs leading-relaxed">{node.excerpt}</p> : null}
- {node.path ? <Button size="sm" onClick={() => onOpen(node)}>Open</Button> : null}
+ {node.origin ? <p className="break-all text-[10px] text-muted-foreground">Source: {node.origin}</p> : null}
+ {node.path || node.targetApp ? <Button size="sm" onClick={() => onOpen(node)}>{node.targetApp ? "Open source app" : "Open"}</Button> : null}
</aside>
);
}
diff --git a/frontend/slices/memory-graph/components/memory-graph-screen.tsx b/frontend/slices/memory-graph/components/memory-graph-screen.tsx
index 4e5f1819..1802fc22 100644
--- a/frontend/slices/memory-graph/components/memory-graph-screen.tsx
+++ b/frontend/slices/memory-graph/components/memory-graph-screen.tsx
@@ -63,6 +63,7 @@ export function MemoryGraphScreen() {
const current = visible?.nodes.find((node) => node.id === selected) ?? null;
const openNode = (node: MemoryGraphNode) => {
+ if (node.targetApp) { openWindow(node.targetApp, node.targetApp === "workflows" ? "Workflows" : "Organization", undefined, node.targetApp === "workflows" ? { view: "sources" } : undefined); return; }
if (!node.path) return;
openWindow("code-editor", node.title, { w: 860, h: 600 }, { path: node.path });
};
@@ -91,7 +92,7 @@ export function MemoryGraphScreen() {
/>
<div className="flex items-center gap-2 border-b px-3 py-1">
<input aria-label="Search graph" value={query} placeholder="Search titles" onChange={(event) => setQuery(event.target.value)} className="h-7 w-full bg-transparent text-xs outline-none" />
- <span className="shrink-0 text-[11px] text-muted-foreground">{busy ? "Loading" : `${visible?.nodes.length ?? 0} nodes`}</span>
+ <span className="shrink-0 text-[11px] text-muted-foreground">{busy ? "Loading" : `${visible?.nodes.length ?? 0} nodes${graph?.truncated ? " · partial" : ""}`}</span>
</div>
{error ? <p className="px-3 py-2 text-xs text-destructive">{error}</p> : null}
{graph?.warnings.length ? <p className="px-3 py-1 text-xs text-muted-foreground">{graph.warnings.join(" · ")}</p> : null}
diff --git a/frontend/slices/os-settings/components/backup-section.tsx b/frontend/slices/os-settings/components/backup-section.tsx
index 836f564e..d1ae6e86 100644
--- a/frontend/slices/os-settings/components/backup-section.tsx
+++ b/frontend/slices/os-settings/components/backup-section.tsx
@@ -1,4 +1,5 @@
"use client";
+import { ServerBackupSection } from "./server-backup-section";
import { useRef, useState } from "react";
import { AlertTriangle, Download, DatabaseBackup, Upload } from "lucide-react";
@@ -57,13 +58,14 @@ export function BackupSection() {
return (
<div className="space-y-4 sm:space-y-5">
+ <ServerBackupSection />
<SettingsSection
icon={<DatabaseBackup />}
- title="Backup"
+ title="Browser backup"
footnote="Covers Playbooks, Agents, Automations, the window layout, installed apps, desktop icons, widgets and wallpapers — everything stored in this browser only. The approved-device identity is deliberately left out, so a leaked backup can't skip device approval. Appearance and Quicklinks also sync to the server, and the server copy wins on the next sign-in."
>
- <SettingsActionRow label="Export backup…" icon={<Download />} onClick={doExport} />
- <SettingsActionRow label="Import backup…" icon={<Upload />} onClick={() => fileInput.current?.click()} />
+ <SettingsActionRow label="Export browser backup…" icon={<Download />} onClick={doExport} />
+ <SettingsActionRow label="Import browser backup…" icon={<Upload />} onClick={() => fileInput.current?.click()} />
{exported !== null && (
<SettingsBlock>
<p className="text-xs text-muted-foreground">
diff --git a/frontend/slices/os-settings/components/cleanup-section.tsx b/frontend/slices/os-settings/components/cleanup-section.tsx
index 03e88d25..0341382a 100644
--- a/frontend/slices/os-settings/components/cleanup-section.tsx
+++ b/frontend/slices/os-settings/components/cleanup-section.tsx
@@ -4,15 +4,13 @@ import { useCallback, useEffect, useState } from "react";
import { RefreshCw, Trash2 } from "lucide-react";
import { IS_DEMO } from "@/lib/demo";
import { Switch } from "@/components/ui/switch";
-import {
- SettingsSection,
- SettingsValueRow,
- SettingsActionRow,
-} from "@/features/shell-settings";
+import { Button } from "@/components/ui/button";
+import { FormDrawer } from "@/features/appshell";
+import { SettingsSection, SettingsValueRow, SettingsActionRow } from "@/features/shell-settings";
type Item = { id: string; label: string; desc: string; bytes: number; available: boolean };
+type Preview = { items: Item[]; preview: { id: string; expiresAt: string }; protected: string[] };
type Result = { id: string; ok: boolean; freedBytes: number; error?: string };
-
function fmt(n: number): string {
if (n < 1e3) return `${n} B`;
if (n < 1e6) return `${Math.round(n / 1e3)} kB`;
@@ -20,149 +18,74 @@ function fmt(n: number): string {
return `${(n / 1e9).toFixed(1)} GB`;
}
-// One-tap safe disk cleanup. Every row is a server-side allowlisted category
-// (caches, old logs, trash, dangling docker layers) that can be deleted without
-// breaking anything — the checklist is the filter; the single button runs it.
export function CleanupSection() {
- const [items, setItems] = useState<Item[] | null>(null);
- const [sel, setSel] = useState<Set<string>>(new Set());
+ const [data, setData] = useState<Preview | null>(null);
+ const [selected, setSelected] = useState<Set<string>>(new Set());
const [busy, setBusy] = useState<"scan" | "clean" | null>("scan");
+ const [confirming, setConfirming] = useState(false);
const [freed, setFreed] = useState<number | null>(null);
const [error, setError] = useState<string | null>(null);
-
- // Pure fetch (no setState) so effects can use the .then form
- // (react-hooks/set-state-in-effect).
- const fetchItems = useCallback(async (): Promise<Item[]> => {
- const r = await fetch("/api/v1/sys/cleanup", { cache: "no-store" });
- if (!r.ok) throw new Error(`scan failed (${r.status})`);
- return ((await r.json()) as { items: Item[] }).items;
+ const fetchPreview = useCallback(async (): Promise<Preview> => {
+ const response = await fetch("/api/v1/sys/cleanup", { cache: "no-store" });
+ const body = await response.json();
+ if (!response.ok) throw new Error(body.error || `scan failed (${response.status})`);
+ return body;
}, []);
-
- const applyScan = useCallback((list: Item[]) => {
- setItems(list);
- // Everything listed is safe — pre-check every row that has something to free.
- setSel(new Set(list.filter((i) => i.available && i.bytes > 0).map((i) => i.id)));
- setBusy(null);
+ const applyPreview = useCallback((next: Preview) => {
+ setData(next); setSelected(new Set()); setBusy(null);
}, []);
-
useEffect(() => {
if (IS_DEMO) return;
let alive = true;
- fetchItems().then(
- (list) => alive && applyScan(list),
- (e) => {
- if (!alive) return;
- setError(e instanceof Error ? e.message : String(e));
- setBusy(null);
- },
- );
- return () => {
- alive = false;
- };
- }, [fetchItems, applyScan]);
-
+ fetchPreview().then((next) => { if (alive) applyPreview(next); }, (cause: unknown) => {
+ if (alive) { setError(cause instanceof Error ? cause.message : "Preview unavailable"); setBusy(null); }
+ });
+ return () => { alive = false; };
+ }, [fetchPreview, applyPreview]);
const rescan = () => {
- setBusy("scan");
- setError(null);
- fetchItems().then(applyScan, (e) => {
- setError(e instanceof Error ? e.message : String(e));
- setBusy(null);
+ setBusy("scan"); setError(null);
+ fetchPreview().then(applyPreview, (cause: unknown) => {
+ setError(cause instanceof Error ? cause.message : "Preview unavailable"); setBusy(null);
});
};
-
const clean = async () => {
- setBusy("clean");
- setError(null);
- setFreed(null);
+ if (!data) return;
+ setConfirming(false); setBusy("clean"); setError(null); setFreed(null);
try {
- const r = await fetch("/api/v1/sys/cleanup", {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({ ids: [...sel] }),
+ const response = await fetch("/api/v1/sys/cleanup", {
+ method: "POST", headers: { "content-type": "application/json" },
+ body: JSON.stringify({ ids: [...selected], preview_id: data.preview.id, confirm: true }),
});
- if (!r.ok) throw new Error(`cleanup failed (${r.status})`);
- const { results } = (await r.json()) as { results: Result[] };
- setFreed(results.reduce((a, x) => a + x.freedBytes, 0));
- const failed = results.filter((x) => !x.ok);
- if (failed.length) setError(`${failed.length} item(s) failed: ${failed.map((f) => f.id).join(", ")}`);
- applyScan(await fetchItems());
- } catch (e) {
- setError(e instanceof Error ? e.message : String(e));
- setBusy(null);
- }
+ const body = await response.json();
+ if (!response.ok) throw new Error(body.error || `cleanup failed (${response.status})`);
+ const results = body.results as Result[];
+ setFreed(results.reduce((sum, result) => sum + result.freedBytes, 0));
+ const failed = results.filter((result) => !result.ok);
+ if (failed.length) setError(`Not completed: ${failed.map((result) => result.id).join(", ")}`);
+ applyPreview(await fetchPreview());
+ } catch (cause) { setError(cause instanceof Error ? cause.message : "Cleanup failed"); setBusy(null); }
};
-
- if (IS_DEMO)
- return (
- <SettingsSection
- icon={<Trash2 />}
- title="Cleanup"
- footnote="Disabled in the demo. In a live deployment this frees disk space from caches, old logs and trash — all safe to delete."
- >
- <SettingsValueRow label="Status" value="Disabled in demo" />
- </SettingsSection>
- );
-
- const toggle = (id: string, on: boolean) =>
- setSel((s) => {
- const next = new Set(s);
- if (on) next.add(id);
- else next.delete(id);
- return next;
- });
-
- const selectedBytes = (items ?? [])
- .filter((i) => sel.has(i.id))
- .reduce((a, i) => a + i.bytes, 0);
-
- return (
- <SettingsSection
- icon={<Trash2 />}
- title="Cleanup"
- footnote="Everything listed is safe to remove: download caches, logs older than 7 days, trash and unused docker layers. Your files, apps and running services are never touched."
- >
- {items === null && (
- <SettingsValueRow label="Scanning…" value={error ?? "measuring reclaimable space"} />
- )}
- {items?.map((i) => (
- <div
- key={i.id}
- data-slot="settings-row"
- className="relative flex min-h-[46px] items-center gap-3 px-4 py-[11px] after:absolute after:inset-x-0 after:bottom-0 after:left-4 after:h-px after:bg-border/60 last:after:hidden"
- >
- <div className="min-w-0 flex-1">
- <p className="text-sm text-foreground">{i.label}</p>
- <p className="truncate text-xs text-muted-foreground">
- {i.available ? i.desc : "Not available on this server"}
- </p>
- </div>
- <span className="shrink-0 text-[13px] tabular-nums text-muted-foreground">
- {i.available ? fmt(i.bytes) : "—"}
- </span>
- <Switch
- checked={sel.has(i.id)}
- disabled={!i.available || busy !== null}
- onCheckedChange={(on) => toggle(i.id, on)}
- aria-label={`Include ${i.label}`}
- />
- </div>
- ))}
- {freed !== null && <SettingsValueRow label="Last cleanup freed" value={fmt(freed)} />}
- {error && items !== null && <SettingsValueRow label="Problem" value={error} />}
- <SettingsActionRow
- label={sel.size ? `Clean up — free about ${fmt(selectedBytes)}` : "Nothing selected"}
- icon={<Trash2 />}
- onClick={clean}
- busy={busy === "clean"}
- disabled={!sel.size || busy !== null}
- />
- <SettingsActionRow
- label="Rescan"
- icon={<RefreshCw />}
- onClick={rescan}
- busy={busy === "scan"}
- disabled={busy !== null}
- />
+ const choices = (data?.items ?? []).filter((item) => selected.has(item.id));
+ const selectedBytes = choices.reduce((sum, item) => sum + item.bytes, 0);
+ if (IS_DEMO) return <SettingsSection icon={<Trash2 />} title="Cleanup" footnote="Preview and cleanup are disabled in the demo."><SettingsValueRow label="Status" value="Disabled in demo" /></SettingsSection>;
+ return <>
+ <SettingsSection icon={<Trash2 />} title="Cleanup" footnote="Nothing is selected automatically. Estimates may exceed actual reclaimed space. Trash and old logs are irreversible; broad temporary-file cleanup is blocked. Session records, memory stores and volumes are not cleanup targets.">
+ <SettingsValueRow label="Session evidence" value="Automatic session-source deletion disabled" />
+ {busy === "scan" ? <SettingsValueRow label="Scanning…" value="Preparing a fresh preview" /> : null}
+ {data?.items.map((item) => <div key={item.id} data-slot="settings-row" className="flex min-h-[46px] items-center gap-3 border-b px-4 py-3 last:border-b-0">
+ <div className="min-w-0 flex-1"><p className="text-sm">{item.label}</p><p className="text-xs text-muted-foreground">{item.desc}</p></div>
+ <span className="shrink-0 text-[13px] tabular-nums text-muted-foreground">{item.available ? fmt(item.bytes) : "Protected / unavailable"}</span>
+ <Switch checked={selected.has(item.id)} disabled={!item.available || busy !== null} aria-label={`Include ${item.label}`} onCheckedChange={(on) => setSelected((prior) => { const next = new Set(prior); if (on) next.add(item.id); else next.delete(item.id); return next; })} />
+ </div>)}
+ {freed !== null ? <SettingsValueRow label="Last cleanup freed" value={fmt(freed)} /> : null}
+ {error ? <SettingsValueRow label="Problem" value={error} /> : null}
+ <SettingsActionRow label={selected.size ? `Review ${selected.size} categories — up to ${fmt(selectedBytes)}` : "Select categories to review"} icon={<Trash2 />} onClick={() => setConfirming(true)} busy={busy === "clean"} disabled={!selected.size || busy !== null} />
+ <SettingsActionRow label="Rescan" icon={<RefreshCw />} onClick={rescan} busy={busy === "scan"} disabled={busy !== null} />
</SettingsSection>
- );
+ <FormDrawer open={confirming} onOpenChange={setConfirming} size="sm">
+ <FormDrawer.Header><FormDrawer.Title>Confirm selected cleanup</FormDrawer.Title><FormDrawer.Description>Only the selected categories below will run. This does not back up your VPS. Preview expires after five minutes.</FormDrawer.Description></FormDrawer.Header>
+ <FormDrawer.Body><div className="space-y-2 text-sm">{choices.map((item) => <p key={item.id}>{item.label} — up to {fmt(item.bytes)}</p>)}<p className="text-xs text-muted-foreground">Preserved: {data?.protected.join(", ")}. Do not treat logs or Trash as recoverable caches.</p></div></FormDrawer.Body>
+ <FormDrawer.Footer><Button variant="ghost" onClick={() => setConfirming(false)}>Cancel</Button><Button variant="destructive" onClick={() => void clean()}>Confirm cleanup</Button></FormDrawer.Footer>
+ </FormDrawer>
+ </>;
}
diff --git a/frontend/slices/os-settings/components/server-backup-section.tsx b/frontend/slices/os-settings/components/server-backup-section.tsx
new file mode 100644
index 00000000..fa2b638f
--- /dev/null
+++ b/frontend/slices/os-settings/components/server-backup-section.tsx
@@ -0,0 +1,46 @@
+"use client";
+
+import { useState } from "react";
+import { DatabaseBackup, ShieldCheck, Search } from "lucide-react";
+import { SettingsSection, SettingsActionRow, SettingsValueRow, SettingsBlock } from "@/features/shell-settings";
+import type { MemoryBackupSummary, MemoryBackupVerification, MemoryBackupScan } from "@/lib/contracts/memory-backup";
+
+type Preview = { scan: MemoryBackupScan; sourceDiscoveryTruncated: boolean; sourceCount: number; scope: string; directory: string };
+async function request<T>(body?: Record<string, unknown>): Promise<T> {
+ const response = await fetch("/api/v1/sys/memory-backup", {
+ cache: "no-store", ...(body ? { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) } : {}),
+ });
+ const value = await response.json();
+ if (!response.ok) throw new Error(value.error || `Memory backup failed (${response.status})`);
+ return value;
+}
+const size = (bytes: number) => `${(bytes / 1024 / 1024).toFixed(1)} MiB`;
+
+export function ServerBackupSection() {
+ const [preview, setPreview] = useState<Preview | null>(null);
+ const [snapshot, setSnapshot] = useState<MemoryBackupSummary | null>(null);
+ const [verification, setVerification] = useState<MemoryBackupVerification | null>(null);
+ const [busy, setBusy] = useState<string | null>(null);
+ const [error, setError] = useState("");
+ const perform = async (name: string, action: () => Promise<void>) => {
+ setBusy(name); setError("");
+ try { await action(); } catch (cause) { setError(cause instanceof Error ? cause.message : "Operation failed"); }
+ finally { setBusy(null); }
+ };
+ return <SettingsSection icon={<DatabaseBackup />} title="Server memory backup" footnote="Owner-only local snapshots of available memory, sessions, workflow and organization data. Not a full VPS/database backup or an offsite copy. Private content stays on the server. Restore check writes a new isolated folder; it never replaces sources or authorizes cleanup.">
+ <SettingsActionRow label="Preview server memory sources" icon={<Search />} busy={busy === "preview"} disabled={busy !== null} onClick={() => void perform("preview", async () => setPreview(await request<Preview>()))} />
+ {preview ? <>
+ <SettingsValueRow label="Available files" value={`${preview.scan.files} · ${size(preview.scan.bytes)}`} />
+ <SettingsValueRow label="Scope" value={preview.scan.truncated || preview.sourceDiscoveryTruncated ? "Partial scan — not a complete backup" : "Bounded allowlisted sources"} />
+ <SettingsValueRow label="Missing / rejected / excluded" value={`${preview.scan.missing} / ${preview.scan.rejected} / ${preview.scan.excluded}`} />
+ <SettingsActionRow label="Create local memory snapshot" icon={<DatabaseBackup />} busy={busy === "create"} disabled={busy !== null || preview.scan.files === 0} onClick={() => void perform("create", async () => { setVerification(null); setSnapshot(await request<MemoryBackupSummary>({ action: "create", confirm: true })); })} />
+ </> : null}
+ {snapshot ? <>
+ <SettingsValueRow label="Snapshot" value={`${snapshot.state} · ${snapshot.scan.files} files · ${size(snapshot.compressedBytes)}`} />
+ <SettingsBlock><p className="break-all text-xs text-muted-foreground">{snapshot.directory}</p><p className="mt-1 break-all font-mono text-xs">Manifest SHA-256: {snapshot.manifestSha256}</p></SettingsBlock>
+ <SettingsActionRow label="Verify checksum and rehearse restore" icon={<ShieldCheck />} busy={busy === "verify"} disabled={busy !== null} onClick={() => void perform("verify", async () => setVerification(await request<MemoryBackupVerification>({ action: "verify", id: snapshot.id, manifest_sha256: snapshot.manifestSha256, confirm: true })))} />
+ </> : null}
+ {verification ? <SettingsValueRow label="Restore verification" value={`${verification.restoredFiles} files verified · ${verification.complete ? "captured scope complete" : "partial snapshot only"} · source writes: ${verification.sourceWritesPerformed}`} /> : null}
+ {error ? <SettingsBlock><p role="alert" className="text-xs text-destructive-text">{error}</p></SettingsBlock> : null}
+ </SettingsSection>;
+}
diff --git a/frontend/slices/os-shell/memory-graph-app.test.ts b/frontend/slices/os-shell/memory-graph-app.test.ts
index 1d66ab27..00422427 100644
--- a/frontend/slices/os-shell/memory-graph-app.test.ts
+++ b/frontend/slices/os-shell/memory-graph-app.test.ts
@@ -9,6 +9,7 @@ describe("Memory graph registration", () => {
const index = (slug: string) => BUILTIN_APPS.findIndex((item) => item.slug === slug);
expect(index("docs")).toBeLessThan(24);
expect(index("settings")).toBeLessThan(24);
- expect(index("memory")).toBeGreaterThan(index("links"));
+ expect(index("memory")).toBeLessThan(24);
+ expect(app?.pinned).toBe(true);
});
});
diff --git a/frontend/slices/os-shell/shell.manifest.ts b/frontend/slices/os-shell/shell.manifest.ts
index a05ba57f..ab0c5a23 100644
--- a/frontend/slices/os-shell/shell.manifest.ts
+++ b/frontend/slices/os-shell/shell.manifest.ts
@@ -65,7 +65,6 @@ export const BUILTIN_APPS: AppDescriptor[] = [
withSlug(withArtwork(reelEditorApp), "reel"),
withSlug(withArtwork(mediaViewerApp), "viewer"),
withSlug(withArtwork(appStoreApp), "store"),
- withSlug(withArtwork(createAppApp), "create"),
pin(withSlug(withArtwork(systemMonitorApp), "monitor")),
withSlug(withArtwork(assistantApp), "assistant"),
withSlug(withArtwork(hermesApp), "hermes"),
@@ -75,6 +74,7 @@ export const BUILTIN_APPS: AppDescriptor[] = [
pin(withSlug(channelsApp, "channels")),
pin(withSlug(withArtwork(workflowsApp), "workflows")),
pin(withSlug(withArtwork(n8nApp), "n8n")),
+ pin(withSlug(memoryGraphApp, "memory")),
withSlug(withArtwork(organizationApp), "organization"),
withSlug(withArtwork(dokployApp), "dokploy"),
withSlug(withArtwork(cloudflareApp), "cloudflare"),
@@ -85,7 +85,7 @@ export const BUILTIN_APPS: AppDescriptor[] = [
withSlug(withArtwork(docsApp), "docs"),
pin(withSlug(withArtwork(osSettingsApp), "settings")),
withSlug(withArtwork(quicklinksApp), "links"),
- withSlug(memoryGraphApp, "memory"),
+ withSlug(withArtwork(createAppApp), "create"),
];
// Shell features — the generic brand-free set now lives INSIDE the appshell
diff --git a/frontend/slices/workflows/app.tsx b/frontend/slices/workflows/app.tsx
index d49afb70..c6b0810e 100644
--- a/frontend/slices/workflows/app.tsx
+++ b/frontend/slices/workflows/app.tsx
@@ -12,15 +12,16 @@ import { Input } from "@/components/ui/input";
import { WorkflowCanvas } from "./components/workflow-canvas";
import { WorkflowCreateDialog } from "./components/workflow-create-dialog";
import { WorkflowDetails, type WorkflowPanel } from "./components/workflow-details";
+import { WorkflowOwnerSources } from "./components/workflow-owner-sources";
import { WorkflowLibrary } from "./components/workflow-library";
import { WorkflowNodePalette } from "./components/workflow-node-palette";
import { WorkflowSessionDetails } from "./components/workflow-session-details";
import { WorkflowSessionLibrary } from "./components/workflow-session-library";
import { tidyWorkflowNodes } from "./lib/canvas-layout";
import { buildWorkflowPackage, workflowPackageFilename, type WorkflowDefinition } from "./lib/portability";
-import { cloneGraph, createFromTemplate, createGraph, deleteGraph, getGraph, getSessionGraph, listGraphs, listWorkflowLearning, resolveNode, runGraph, runStatus, saveSessionWorkflowDraft, updateGraph, type WorkflowLearningRecipeSummary } from "./lib/api";
+import { cloneGraph, createFromTemplate, createGraph, deleteGraph, getGraph, getSessionGraph, listGraphs, resolveNode, runGraph, runStatus, saveSessionWorkflowDraft, updateGraph } from "./lib/api";
-type LibraryMode = "automations" | "sessions";
+type LibraryMode = "automations" | "sessions" | "sources";
function starterGraph(): WorkflowDefinition { return { name: "New Workflow", description: "Private workflow graph.", status: "draft", inputs: {}, metadata: { provenance: "user" }, nodes: [{ id: "manual", name: "Manual Trigger", type: "manual", position: { x: 80, y: 160 }, config: {} }, { id: "output", name: "Output", type: "output", position: { x: 420, y: 160 }, config: {} }], edges: [{ id: "edge-start", source: "manual", target: "output" }] }; }
function payloadSessionId(payload: unknown): string | undefined { if (!payload || typeof payload !== "object") return undefined; const row = payload as { view?: unknown; sessionId?: unknown }; return row.view === "sessions" && typeof row.sessionId === "string" ? row.sessionId : undefined; }
@@ -33,7 +34,6 @@ export default function WorkflowsApp(props: AppProps) {
const [libraryOpen, setLibraryOpen] = useState(false), [detailsOpen, setDetailsOpen] = useState(false), [showLibrary, setShowLibrary] = useState(true), [showDetails, setShowDetails] = useState(true);
const [libraryMode, setLibraryMode] = useState<LibraryMode>(() => payloadSessionId(props.payload) ? "sessions" : "automations");
const [sessionView, setSessionView] = useState<SessionGraphView | null>(null), [sessionSelected, setSessionSelected] = useState<string | null>(null);
- const [learning, setLearning] = useState<WorkflowLearningRecipeSummary[]>([]);
const node = useMemo(() => graph?.nodes.find((item) => item.id === selected) ?? null, [graph, selected]);
const sessionNode = useMemo(() => sessionView?.graph.nodes.find((item) => item.id === sessionSelected) ?? null, [sessionSelected, sessionView]);
const runStates = useMemo(() => new Map(run?.nodes.map((item) => [item.id, item.state]) ?? []), [run]);
@@ -49,17 +49,23 @@ export default function WorkflowsApp(props: AppProps) {
}, [overlayPane]);
useEffect(() => {
const id = payloadSessionId(props.payload);
- if (!id) return;
+ if (!id) {
+ if (props.payload && typeof props.payload === "object" && "view" in props.payload && props.payload.view === "sources") {
+ const frame = requestAnimationFrame(() => { setLibraryMode("sources"); setLibraryOpen(true); });
+ return () => cancelAnimationFrame(frame);
+ }
+ return;
+ }
let alive = true;
- void Promise.all([getSessionGraph(id), listWorkflowLearning().catch(() => [])]).then(([next, recipes]) => { if (!alive) return; setLibraryMode("sessions"); setMessage(""); setSessionView(next); setLearning(recipes); setSessionSelected("session-root"); }).catch((error: unknown) => { if (alive) setMessage(error instanceof Error ? error.message : "Could not load session"); });
+ void getSessionGraph(id).then((next) => { if (!alive) return; setLibraryMode("sessions"); setMessage(""); setSessionView(next); setSessionSelected("session-root"); }).catch((error: unknown) => { if (alive) setMessage(error instanceof Error ? error.message : "Could not load session"); });
return () => { alive = false; };
}, [props.payload]);
const wrap = async (fn: () => Promise<void>) => { setBusy(true); setMessage(""); try { await fn(); } catch (error) { setMessage(error instanceof Error ? error.message : "Action failed"); } finally { setBusy(false); } };
const mutate = (next: WorkflowGraph) => { setGraph(next); setDirty(true); };
const selectGraph = async (id: string) => { setLibraryMode("automations"); setGraph(await getGraph(id)); setSelected(null); setRun(null); setDirty(false); setPanel("inspector"); setLibraryOpen(false); };
- const selectSession = async (id: string) => { const [next, recipes] = await Promise.all([getSessionGraph(id), listWorkflowLearning().catch(() => [])]); setLibraryMode("sessions"); setSessionView(next); setLearning(recipes); setSessionSelected("session-root"); setLibraryOpen(false); setDetailsOpen(false); };
- const refreshSession = async () => { if (!sessionView) return; const [next, recipes] = await Promise.all([getSessionGraph(sessionView.session.id), listWorkflowLearning(true).catch(() => [])]); setSessionView(next); setLearning(recipes); if (sessionSelected && !next.graph.nodes.some((item) => item.id === sessionSelected)) setSessionSelected("session-root"); };
+ const selectSession = async (id: string) => { const next = await getSessionGraph(id); setLibraryMode("sessions"); setSessionView(next); setSessionSelected("session-root"); setLibraryOpen(false); setDetailsOpen(false); };
+ const refreshSession = async () => { if (!sessionView) return; const next = await getSessionGraph(sessionView.session.id); setSessionView(next); if (sessionSelected && !next.graph.nodes.some((item) => item.id === sessionSelected)) setSessionSelected("session-root"); };
const createBlank = () => wrap(async () => { const next = await createGraph(starterGraph()); setLibraryMode("automations"); await load(next.id); setSelected("manual"); setLibraryOpen(false); });
const createTemplate = (id: string) => wrap(async () => { const next = await createFromTemplate(id); setLibraryMode("automations"); await load(next.id); setSelected(next.nodes[0]?.id ?? null); setLibraryOpen(false); });
const createAI = (definition: WorkflowDefinition) => wrap(async () => { const next = await createGraph(definition); setLibraryMode("automations"); await load(next.id); setSelected(next.nodes[0]?.id ?? null); setLibraryOpen(false); });
@@ -101,8 +107,8 @@ export default function WorkflowsApp(props: AppProps) {
const toggleDetails = () => overlayPane ? setDetailsOpen(true) : setShowDetails((value) => !value);
const automationLibrary = <WorkflowLibrary graphs={graphs} activeId={graph?.id} search={search} onSearch={setSearch} onSelect={(id) => void wrap(() => selectGraph(id))} onBlank={async () => { await createBlank(); }} onTemplate={async (id) => { await createTemplate(id); }} onAI={async (definition) => { await createAI(definition); }} onImport={async (definition) => { await createImported(definition); }}/>;
- const library = <div className="flex h-full min-h-0 flex-col"><div className="grid grid-cols-2 gap-1 border-b p-2">{(["automations", "sessions"] as const).map((mode) => <button key={mode} type="button" className={`rounded-md px-2 py-1.5 text-xs capitalize ${libraryMode === mode ? "bg-accent font-semibold" : "text-muted-foreground hover:bg-accent/60"}`} onClick={() => setLibraryMode(mode)}>{mode}</button>)}</div><div className="min-h-0 flex-1">{libraryMode === "automations" ? automationLibrary : <WorkflowSessionLibrary activeId={sessionView?.session.id} onSelect={(id) => void wrap(() => selectSession(id))}/>}</div></div>;
- const details = libraryMode === "sessions" && sessionView ? <WorkflowSessionDetails view={sessionView} node={sessionNode} onOpenTerminal={openSessionTerminal} onOpenCode={openSessionCode} onSaveDraft={saveSessionDraft} savingDraft={busy} learning={learning}/> : graph ? <WorkflowDetails graph={graph} graphs={graphs} node={node} run={run} panel={panel} onPanel={(next) => next === "optimizer" && dirty ? void openOptimizer() : setPanel(next)} onNode={updateNode} onDeleteNode={(id) => deleteNodes([id])} onGraph={mutate} onRunSelect={(selectedRun) => { setRun(selectedRun); setPanel("run"); }} onRestored={(next) => { setGraph(next); setDirty(false); void listGraphs().then(setGraphs); }} onOptimizerCloned={(next) => { setGraph(next); setSelected(null); setRun(null); setDirty(false); setPanel("optimizer"); void listGraphs(true).then(setGraphs); }}/> : null;
+ const library = <div className="flex h-full min-h-0 flex-col"><div className="grid grid-cols-3 gap-1 border-b p-2">{(["automations", "sessions", "sources"] as const).map((mode) => <button key={mode} type="button" className={`rounded-md px-2 py-1.5 text-xs capitalize ${libraryMode === mode ? "bg-accent font-semibold" : "text-muted-foreground hover:bg-accent/60"}`} onClick={() => setLibraryMode(mode)}>{mode}</button>)}</div><div className="min-h-0 flex-1">{libraryMode === "sources" ? <WorkflowOwnerSources onSelect={(id) => void wrap(() => selectGraph(id))} onCopied={(next) => void wrap(async () => { setLibraryMode("automations"); await load(next.id, true); setLibraryOpen(false); })}/> : libraryMode === "automations" ? automationLibrary : <WorkflowSessionLibrary activeId={sessionView?.session.id} onSelect={(id) => void wrap(() => selectSession(id))}/>}</div></div>;
+ const details = libraryMode === "sessions" && sessionView ? <WorkflowSessionDetails view={sessionView} node={sessionNode} onOpenTerminal={openSessionTerminal} onOpenCode={openSessionCode} onSaveDraft={saveSessionDraft} savingDraft={busy}/> : graph ? <WorkflowDetails graph={graph} graphs={graphs} node={node} run={run} panel={panel} onPanel={(next) => next === "optimizer" && dirty ? void openOptimizer() : setPanel(next)} onNode={updateNode} onDeleteNode={(id) => deleteNodes([id])} onGraph={mutate} onRunSelect={(selectedRun) => { setRun(selectedRun); setPanel("run"); }} onRestored={(next) => { setGraph(next); setDirty(false); void listGraphs().then(setGraphs); }} onOptimizerCloned={(next) => { setGraph(next); setSelected(null); setRun(null); setDirty(false); setPanel("optimizer"); void listGraphs(true).then(setGraphs); }}/> : null;
const toolbarItems: ToolbarItem[] = [
{ id: "library", label: "Workflow library", icon: PanelLeft, onClick: toggleLibrary, primary: true },
{ id: "refresh", label: "Refresh", icon: RefreshCw, onClick: () => void wrap(async () => { if (libraryMode === "sessions") await refreshSession(); else await load(undefined, true); }), disabled: busy },
@@ -126,7 +132,7 @@ export default function WorkflowsApp(props: AppProps) {
<div className={`grid h-full min-h-0 min-w-0 ${showLibraryPanel ? "grid-cols-[300px_minmax(0,1fr)] @min-[1200px]:grid-cols-[340px_minmax(0,1fr)]" : "grid-cols-1"}`}>
{showLibraryPanel ? <aside className="min-h-0 min-w-0 border-r">{library}</aside> : null}
<main className="flex min-h-0 min-w-0 overflow-hidden">
- {libraryMode === "automations" ? graph ? <div className="flex min-h-0 min-w-0 flex-1 flex-col"><div className="flex min-h-10 min-w-0 shrink-0 flex-wrap items-center gap-1.5 border-b px-2 py-1 @min-[520px]:flex-nowrap @min-[520px]:gap-2"><label className="flex shrink-0 items-center gap-1.5 text-[10px] text-muted-foreground"><span>Status</span><select aria-label="Workflow status" className="h-7 min-w-0 max-w-24 rounded border bg-background px-2 text-xs text-foreground" value={graph.status} onChange={(event) => mutate({ ...graph, status: event.target.value as WorkflowGraph["status"] })}><option value="draft">Draft</option><option value="active">Active</option><option value="archived">Archived</option></select></label><WorkflowNodePalette onAdd={addNode}/>{message ? <span className="w-full min-w-0 truncate text-[11px] text-muted-foreground @min-[520px]:ml-auto @min-[520px]:w-auto @min-[520px]:max-w-72">{message}</span> : null}</div><div className="min-h-0 min-w-0 flex-1 overflow-hidden"><WorkflowCanvas key={graph.id} graph={graph} selectedId={selected} onSelect={chooseNode} onCustomNodes={(customNodes) => mutate({ ...graph, metadata: { ...graph.metadata, customNodes } })} onMoveMany={(nodes) => mutate({ ...graph, nodes })} nodeStates={runStates} edgeStates={runEdgeStates} onOpen={(id) => void openNode(id)} onConnect={connect} onMove={(id, position) => mutate({ ...graph, nodes: graph.nodes.map((item) => item.id === id ? { ...item, position } : item) })} onDeleteNodes={deleteNodes} onDeleteEdges={deleteEdges} onTidy={tidy}/></div></div> : <div className="flex h-full min-w-0 flex-1 items-center justify-center px-4 text-center text-sm text-muted-foreground">Create or select an automation.</div>
+ {libraryMode === "sources" ? <div className="flex h-full flex-1 items-center justify-center p-6 text-center text-sm text-muted-foreground">Owner sources are listed in the library. Open an own workflow or copy a foreign source as a disabled draft for review.</div> : libraryMode === "automations" ? graph ? <div className="flex min-h-0 min-w-0 flex-1 flex-col"><div className="flex min-h-10 min-w-0 shrink-0 flex-wrap items-center gap-1.5 border-b px-2 py-1 @min-[520px]:flex-nowrap @min-[520px]:gap-2"><label className="flex shrink-0 items-center gap-1.5 text-[10px] text-muted-foreground"><span>Status</span><select aria-label="Workflow status" className="h-7 min-w-0 max-w-24 rounded border bg-background px-2 text-xs text-foreground" value={graph.status} onChange={(event) => mutate({ ...graph, status: event.target.value as WorkflowGraph["status"] })}><option value="draft">Draft</option><option value="active">Active</option><option value="archived">Archived</option></select></label><WorkflowNodePalette onAdd={addNode}/>{message ? <span className="w-full min-w-0 truncate text-[11px] text-muted-foreground @min-[520px]:ml-auto @min-[520px]:w-auto @min-[520px]:max-w-72">{message}</span> : null}</div><div className="min-h-0 min-w-0 flex-1 overflow-hidden"><WorkflowCanvas key={graph.id} graph={graph} selectedId={selected} onSelect={chooseNode} onCustomNodes={(customNodes) => mutate({ ...graph, metadata: { ...graph.metadata, customNodes } })} onMoveMany={(nodes) => mutate({ ...graph, nodes })} nodeStates={runStates} edgeStates={runEdgeStates} onOpen={(id) => void openNode(id)} onConnect={connect} onMove={(id, position) => mutate({ ...graph, nodes: graph.nodes.map((item) => item.id === id ? { ...item, position } : item) })} onDeleteNodes={deleteNodes} onDeleteEdges={deleteEdges} onTidy={tidy}/></div></div> : <div className="flex h-full min-w-0 flex-1 items-center justify-center px-4 text-center text-sm text-muted-foreground">Create an automation, or open Sources to discover workflows saved by MCP and other browser identities.</div>
: sessionView ? <div className="flex min-h-0 min-w-0 flex-1 flex-col"><div className="flex min-h-10 shrink-0 items-center gap-2 border-b px-3 text-[11px] text-muted-foreground [@media(max-height:520px)]:hidden"><span className="font-medium text-foreground">{sessionView.session.label}</span><span>read-only</span><span>·</span><span>{sessionView.steps.length} steps</span><span>·</span><span>{sessionView.shownEvents}/{sessionView.totalEvents} actions</span>{message ? <span className="ml-auto truncate">{message}</span> : null}</div><div className="min-h-0 min-w-0 flex-1 overflow-hidden"><WorkflowCanvas key={sessionView.graph.revision} graph={sessionView.graph} selectedId={sessionSelected} onSelect={chooseSessionNode} onConnect={() => undefined} onMove={() => undefined} readOnly/></div></div> : <div className="flex h-full min-w-0 flex-1 items-center justify-center px-4 text-center text-sm text-muted-foreground">Select a session to inspect its semantic execution flow.</div>}
{showDetailsPanel ? <aside className="w-[320px] shrink-0 border-l @min-[1200px]:w-[360px]">{details}</aside> : null}
</main>
diff --git a/frontend/slices/workflows/components/workflow-learning-panel.tsx b/frontend/slices/workflows/components/workflow-learning-panel.tsx
new file mode 100644
index 00000000..3d6348a3
--- /dev/null
+++ b/frontend/slices/workflows/components/workflow-learning-panel.tsx
@@ -0,0 +1,49 @@
+"use client";
+
+import { useEffect, useState } from "react";
+import { BrainCircuit } from "lucide-react";
+import { Button } from "@/components/ui/button";
+import { Badge } from "@/components/ui/badge";
+import type { WorkflowLearningRecipeSummary } from "../lib/api";
+
+type Page = { recipes: Array<WorkflowLearningRecipeSummary & { archived?: boolean; graphReceipt?: { state: string; warning?: string } }>; total: number; offset: number; nextOffset?: number; warnings: string[]; scope: string };
+
+export function WorkflowLearningPanel({ sessionLabel }: { sessionLabel: string }) {
+ const [offset, setOffset] = useState(0), [nonce, setNonce] = useState(0);
+ const [result, setResult] = useState<{ key: string; page?: Page; error?: string } | null>(null);
+ const key = `${sessionLabel}:${offset}:${nonce}`;
+ const page = result?.key === key ? result.page : undefined;
+ const error = result?.key === key ? result.error : undefined;
+ const loading = result?.key !== key;
+ useEffect(() => {
+ let alive = true;
+ const params = new URLSearchParams({ learning: "1", session_label: sessionLabel, include_archived: "1", offset: String(offset), limit: "20" });
+ void fetch(`/api/v1/workflows?${params}`, { cache: "no-store" }).then(async response => {
+ const body = await response.json();
+ if (!response.ok) throw new Error(body.error || `Learning unavailable (${response.status})`);
+ return body as Page;
+ }).then(value => { if (alive) setResult({ key, page: value }); }, (cause: unknown) => {
+ if (alive) setResult({ key, error: cause instanceof Error ? cause.message : "Learning could not be loaded" });
+ });
+ return () => { alive = false; };
+ }, [key, sessionLabel, offset]);
+ return <section data-slot="session-self-improve" className="space-y-2 border-t pt-3">
+ <h3 className="flex items-center gap-1.5 text-xs font-semibold"><BrainCircuit className="size-3.5" />Self-improve</h3>
+ {loading ? <p className="text-xs text-muted-foreground">Loading linked active and archived recipes…</p> : null}
+ {error ? <div role="alert" className="space-y-2 text-xs text-destructive-text"><p>{error}. This is a loading failure, not proof that the session was not learned.</p><Button size="sm" variant="outline" onClick={() => setNonce(value => value + 1)}>Retry learning</Button></div> : null}
+ {page ? <>
+ <p className="text-xs text-muted-foreground">{page.total} linked recipes · active and archived. {page.scope}</p>
+ {page.warnings.map(warning => <p key={warning} className="text-xs text-destructive-text">{warning}</p>)}
+ {page.recipes.map(recipe => <div key={recipe.id} className="space-y-1 rounded-lg border p-2.5">
+ <div className="flex flex-wrap gap-1"><Badge variant="outline">{recipe.stage}</Badge>{recipe.archived ? <Badge variant="secondary">Archived</Badge> : null}</div>
+ <p className="text-xs font-medium">{recipe.intent}</p>
+ <p className="text-xs text-muted-foreground">{recipe.attempts} attempts · {recipe.successRate}% success</p>
+ <div className="flex flex-wrap gap-1">{recipe.sourceSessions.filter(source => source.label === sessionLabel).slice(0, 8).map(source => <code key={`${source.label}:${source.actionRef}`} className="text-[10px]">{source.actionRef}</code>)}</div>
+ {recipe.graphReceipt?.warning ? <p className="text-xs text-destructive-text">{recipe.graphReceipt.warning}</p> : null}
+ <p className="text-[10px] text-muted-foreground">Recipe availability does not authorize evidence deletion or prove this whole session has been reviewed. Skill/tool promotion remains explicit through Tool Forge.</p>
+ </div>)}
+ {!page.recipes.length ? <p className="text-xs text-muted-foreground">No link was found in retained recipe provenance. This does not prove that no memory was saved.</p> : null}
+ <div className="flex gap-2"><Button size="sm" variant="outline" disabled={offset === 0} onClick={() => setOffset(value => Math.max(0, value - 20))}>Previous</Button><Button size="sm" variant="outline" disabled={page.nextOffset === undefined} onClick={() => setOffset(page.nextOffset ?? offset)}>Next</Button></div>
+ </> : null}
+ </section>;
+}
diff --git a/frontend/slices/workflows/components/workflow-owner-sources.tsx b/frontend/slices/workflows/components/workflow-owner-sources.tsx
new file mode 100644
index 00000000..72625651
--- /dev/null
+++ b/frontend/slices/workflows/components/workflow-owner-sources.tsx
@@ -0,0 +1,48 @@
+"use client";
+
+import { useEffect, useState } from "react";
+import { Button } from "@/components/ui/button";
+import { Input } from "@/components/ui/input";
+import type { WorkflowDiscoveryPage, WorkflowDiscoveryRow } from "@/lib/contracts/workflow-discovery";
+import type { WorkflowGraph } from "@/lib/contracts/workflow-graph";
+
+export function WorkflowOwnerSources({ onSelect, onCopied }: { onSelect: (id: string) => void; onCopied: (graph: WorkflowGraph) => void }) {
+ const [offset, setOffset] = useState(0), [query, setQuery] = useState("");
+ const [result, setResult] = useState<{ offset: number; page?: WorkflowDiscoveryPage; error?: string } | null>(null);
+ const [error, setError] = useState(""), [busy, setBusy] = useState(false);
+ const page = result?.offset === offset ? result.page : undefined;
+ useEffect(() => {
+ let alive = true;
+ void fetch(`/api/v1/workflows?owner_view=1&owner_offset=${offset}`, { cache: "no-store" }).then(async response => {
+ const body = await response.json(); if (!response.ok) throw new Error(body.error || "Owner sources unavailable"); return body as WorkflowDiscoveryPage;
+ }).then(value => { if (alive) setResult({ offset, page: value }); }, (cause: unknown) => {
+ if (alive) setResult({ offset, error: cause instanceof Error ? cause.message : "Owner sources unavailable" });
+ });
+ return () => { alive = false; };
+ }, [offset]);
+ const copy = async (row: WorkflowDiscoveryRow) => {
+ setBusy(true); setError("");
+ try {
+ const response = await fetch("/api/v1/workflows", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ action: "owner_clone", origin_owner: row.owner, graph_id: row.id, expected_revision: row.revision, confirm: true }) });
+ const body = await response.json(); if (!response.ok) throw new Error(body.error || "Copy failed"); onCopied(body.graph);
+ } catch (cause) { setError(cause instanceof Error ? cause.message : "Copy failed"); }
+ finally { setBusy(false); }
+ };
+ return <div className="flex h-full min-h-0 flex-col gap-2 p-3">
+ <h3 className="text-sm font-semibold">Owner sources</h3>
+ <p className="text-xs text-muted-foreground">Discover saved workflows from browser and MCP identities. Foreign sources are read-only. Copying creates an inactive draft with action nodes disabled for review; the original is unchanged.</p>
+ <Input aria-label="Search owner workflows" placeholder="Search this source page" value={query} onChange={event => setQuery(event.target.value)} />
+ {error || result?.error ? <p role="alert" className="text-xs text-destructive-text">{error || result?.error}</p> : null}
+ {!page && !result?.error ? <p className="text-xs text-muted-foreground">Loading sources…</p> : null}
+ <div className="min-h-0 flex-1 space-y-2 overflow-y-auto">
+ {page?.scan.warnings.map(warning => <p key={warning} className="text-xs text-destructive-text">{warning}</p>)}
+ {page?.graphs.filter(row => `${row.name} ${row.project ?? ""}`.toLowerCase().includes(query.toLowerCase())).map(row => <div key={`${row.owner}:${row.id}`} className="space-y-1.5 rounded-md border p-2">
+ <p className="text-xs font-medium">{row.name}</p>
+ <p className="break-all text-[10px] text-muted-foreground">{row.originPrincipal} · {row.status} · {row.nodeCount} nodes</p>
+ <Button size="sm" variant="outline" disabled={busy} onClick={() => row.readOnly ? void copy(row) : onSelect(row.id)}>{row.readOnly ? "Copy as review-only draft" : "Open own workflow"}</Button>
+ </div>)}
+ {page && !page.graphs.length ? <p className="text-xs text-muted-foreground">No saved graphs in this source page.</p> : null}
+ </div>
+ {page ? <div className="space-y-1 text-xs text-muted-foreground"><p>{page.scan.ownersScanned} identities on this page / {page.scan.totalOwners} discovered</p><div className="flex gap-2"><Button size="sm" variant="outline" disabled={offset === 0 || busy} onClick={() => setOffset(value => Math.max(0, value - 4))}>Previous</Button><Button size="sm" variant="outline" disabled={page.scan.nextOwnerOffset === undefined || busy} onClick={() => setOffset(page.scan.nextOwnerOffset ?? offset)}>Next</Button></div></div> : null}
+ </div>;
+}
diff --git a/frontend/slices/workflows/components/workflow-session-details.tsx b/frontend/slices/workflows/components/workflow-session-details.tsx
index 93d8bd11..fa9d1224 100644
--- a/frontend/slices/workflows/components/workflow-session-details.tsx
+++ b/frontend/slices/workflows/components/workflow-session-details.tsx
@@ -1,13 +1,14 @@
"use client";
import { useState } from "react";
-import { BrainCircuit, Code2, Copy, ExternalLink, FileCode2, GitCompareArrows, History, Save, TerminalSquare } from "lucide-react";
+import { WorkflowLearningPanel } from "./workflow-learning-panel";
+import { Code2, Copy, ExternalLink, FileCode2, GitCompareArrows, History, Save, TerminalSquare } from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { ScrollArea } from "@/components/ui/scroll-area";
import type { SessionFlowAction, SessionFlowStep, SessionGraphView } from "@/lib/contracts/session-monitor";
import type { WorkflowGraphNode } from "@/lib/contracts/workflow-graph";
-import { getSessionArtifactHistory, type SessionArtifactHistoryResponse, type WorkflowLearningRecipeSummary } from "../lib/api";
+import { getSessionArtifactHistory, type SessionArtifactHistoryResponse } from "../lib/api";
function value(config: Record<string, unknown>, key: string): string | undefined {
const raw = config[key]; return typeof raw === "string" && raw ? raw : undefined;
@@ -82,22 +83,9 @@ function actionGroups(step: SessionFlowStep, sessionId: string, onOpenTerminal:
});
}
-function SelfImprove({ recipes }: { recipes: WorkflowLearningRecipeSummary[] }) {
- return <div data-slot="session-self-improve" className="border-t pt-3">
- <div className="mb-2 flex items-center gap-1.5 text-[10px] font-semibold uppercase text-muted-foreground"><BrainCircuit className="size-3.5"/>Self-improve</div>
- {recipes.length ? <div className="space-y-2">{recipes.slice(0, 4).map((recipe) => <div key={recipe.id} className="rounded-lg border bg-background/35 p-2.5">
- <div className="flex flex-wrap items-center gap-1.5"><Badge variant="outline" className="h-5 text-[9px] capitalize">{recipe.stage}</Badge><span className="min-w-0 flex-1 truncate text-xs font-medium">{recipe.intent}</span></div>
- <div className="mt-1 flex flex-wrap gap-x-3 gap-y-1 text-[10px] text-muted-foreground"><span>{recipe.attempts} attempts</span><span>{recipe.successRate}% success</span>{recipe.fastestDurationMs != null ? <span>fastest {Math.max(1, Math.round(recipe.fastestDurationMs / 1000))}s</span> : null}</div>
- <div className="mt-1.5 flex flex-wrap gap-1">{recipe.sourceSessions.filter((source, index, rows) => rows.findIndex((row) => row.actionRef === source.actionRef) === index).slice(0, 8).map((source) => <code key={`${source.actionRef}-${source.eventRef}`} className="rounded bg-muted px-1.5 py-0.5 text-[9px]">{source.actionRef}</code>)}</div>
- <p className="mt-1.5 text-[9px] text-muted-foreground">{recipe.stage === "tested" ? "Tested automation is reusable when current evidence matches." : recipe.stage === "verified" ? "Verified route; current task still requires independent verification." : recipe.stage === "candidate" ? "Repeated route; use as a planning shortcut, not a guarantee." : "Observed route; more successful runs are needed."}{recipe.forge.eligible ? " Skill/tool promotion remains explicit through Tool Forge." : ""}</p>
- </div>)}</div> : <p className="rounded-lg border border-dashed p-2.5 text-[10px] text-muted-foreground">No learned recipe is linked to this session yet. A verified <code>workflow_finish</code> can attach future recipe steps to their <code>Sx.Ay</code> receipts.</p>}
- </div>;
-}
-
-export function WorkflowSessionDetails({ view, node, onOpenTerminal, onOpenCode, onSaveDraft, savingDraft = false, learning = [] }: { view: SessionGraphView; node: WorkflowGraphNode | null; onOpenTerminal: () => void; onOpenCode: (path: string) => void; onSaveDraft: (stepRef?: string) => void; savingDraft?: boolean; learning?: WorkflowLearningRecipeSummary[] }) {
+export function WorkflowSessionDetails({ view, node, onOpenTerminal, onOpenCode, onSaveDraft, savingDraft = false }: { view: SessionGraphView; node: WorkflowGraphNode | null; onOpenTerminal: () => void; onOpenCode: (path: string) => void; onSaveDraft: (stepRef?: string) => void; savingDraft?: boolean }) {
const ref = node ? value(node.config, "ref") : undefined;
const step: SessionFlowStep | undefined = ref ? view.steps.find((item) => item.ref === ref) : undefined;
- const linked = learning.filter((recipe) => recipe.sourceSessions.some((source) => source.label === view.session.label));
return <div className="flex h-full min-h-0 flex-col bg-card/20">
<div className="border-b p-3">
<div className="break-words text-sm font-semibold">{view.session.label}</div>
@@ -110,7 +98,7 @@ export function WorkflowSessionDetails({ view, node, onOpenTerminal, onOpenCode,
<div><div className="flex items-center gap-2"><code className="rounded bg-muted px-1.5 py-0.5 font-mono text-[10px] font-semibold">{step.ref}</code><Badge variant="outline" className="text-[9px] capitalize">{step.category}</Badge></div><div className="mt-2 break-words text-sm font-semibold">{step.title}</div><p className="mt-1 text-xs text-muted-foreground">{step.summary}</p><Button data-slot="save-step-workflow-draft" type="button" size="sm" variant="outline" className="mt-2 h-7 text-[10px]" disabled={savingDraft} onClick={() => onSaveDraft(step.ref)}><Save className="size-3.5"/>Save {step.ref} as workflow draft</Button></div>
<div className="border-t pt-3"><div className="mb-2 flex items-center justify-between"><div className="text-[10px] font-semibold uppercase text-muted-foreground">Action groups</div><span className="text-[10px] text-muted-foreground">{step.groups.length} groups · {step.actions.length} actions</span></div><div className="space-y-1.5">{actionGroups(step, view.session.id, onOpenTerminal, onOpenCode)}</div></div>
</> : <p className="text-xs text-muted-foreground">Select a semantic step to inspect its actions.</p>}
- <SelfImprove recipes={linked}/>
+ <WorkflowLearningPanel key={view.session.id} sessionLabel={view.session.label}/>
{view.session.cwd ? <div className="border-t pt-3"><div className="text-[10px] uppercase text-muted-foreground">Session context</div><p className="mt-1 break-all font-mono text-[10px] text-muted-foreground">{view.session.cwd}</p></div> : null}
</div></ScrollArea>
<div className="border-t p-3 text-[10px] text-muted-foreground">Use references such as <code>S3.A4</code> when discussing a specific action. MSO keeps internal session IDs hidden from this surface.</div>
diff --git a/frontend/slices/workflows/session-graph-ui.test.ts b/frontend/slices/workflows/session-graph-ui.test.ts
index dae1ed51..e35b26a0 100644
--- a/frontend/slices/workflows/session-graph-ui.test.ts
+++ b/frontend/slices/workflows/session-graph-ui.test.ts
@@ -8,6 +8,7 @@ const settingsDetail = readFileSync(new URL("../os-settings/components/mcp-sessi
const sessionGuide = readFileSync(new URL("../os-settings/components/mcp-session-guide.tsx", import.meta.url), "utf8");
const inspector = readFileSync(new URL("./components/workflow-inspector.tsx", import.meta.url), "utf8");
const sessionDetails = readFileSync(new URL("./components/workflow-session-details.tsx", import.meta.url), "utf8");
+const learningPanel = readFileSync(new URL("./components/workflow-learning-panel.tsx", import.meta.url), "utf8");
const sessionLibrary = readFileSync(new URL("./components/workflow-session-library.tsx", import.meta.url), "utf8");
describe("Workflow session graph UI contract", () => {
@@ -41,10 +42,13 @@ describe("Workflow session graph UI contract", () => {
expect(sessionDetails).toContain("S3.A4");
expect(sessionDetails).toContain('data-slot="save-session-workflow-draft"');
expect(sessionDetails).toContain('data-slot="save-step-workflow-draft"');
- expect(sessionDetails).toContain('data-slot="session-self-improve"');
- expect(sessionDetails).toContain("Self-improve");
+ expect(learningPanel).toContain('data-slot="session-self-improve"');
+ expect(learningPanel).toContain("Self-improve");
expect(workflowApp).toContain("saveSessionWorkflowDraft");
- expect(workflowApp).toContain("listWorkflowLearning");
+ expect(sessionDetails).toContain("WorkflowLearningPanel");
+ expect(workflowApp).not.toContain("listWorkflowLearning().catch(() => [])");
+ expect(learningPanel).toContain("include_archived");
+ expect(learningPanel).toContain('role="alert"');
expect(workflowCanvas).toContain('showMinimap={nodes.length > 3}');
expect(workflowCanvas).not.toContain('showMinimap={!readOnly}');
});
diff --git a/lib/agent/artifact-cleanup.ts b/lib/agent/artifact-cleanup.ts
index 749465f0..e765527e 100644
--- a/lib/agent/artifact-cleanup.ts
+++ b/lib/agent/artifact-cleanup.ts
@@ -1,4 +1,5 @@
import path from "node:path";
+import { sessionPreservation } from "./session-preservation";
import { promises as fs } from "node:fs";
import { withSecurityStoreLock } from "@/lib/security-store-lock";
import { readSessionFile, listSessionRecords, principalHash } from "./session-files";
@@ -19,6 +20,8 @@ export async function pruneSessionArtifacts(owner: ArtifactOwner, dryRun = true,
const latest = Math.max(Date.parse(session.updatedAt), Date.parse(m.updatedAt));
if (Date.parse(m.leaseUntil) > now || now - latest < artifactRetentionDays() * 86400000)
return { sessionId: owner.id, state: "active", bytes: 0 };
+ const preservation = await sessionPreservation(owner.id);
+ if (preservation.protected) return { sessionId: owner.id, state: "protected", reason: preservation.reason, bytes: 0 };
const rootEntries = await fs.readdir(p.directory);
if (rootEntries.some((n) => !["incoming", "screenshots", "reports", "manifest.json"].includes(n)))
return { sessionId: owner.id, state: "unknown-files", bytes: 0 };
diff --git a/lib/agent/artifacts.test.ts b/lib/agent/artifacts.test.ts
index 62a6f7e6..6036ac47 100644
--- a/lib/agent/artifacts.test.ts
+++ b/lib/agent/artifacts.test.ts
@@ -91,12 +91,12 @@ describe("session artifact storage",()=>{
describe("bounded artifact retention",()=>{
it("preserves live/leased files and defaults to a dry-run",async()=>{
await prepareSessionArtifacts(one);await saveSessionArtifact(one,png,meta);expect((await pruneSessionArtifacts(one)).state).toBe("active");
- const future=Date.now()+9*86400000;expect((await pruneSessionArtifacts(one,true,future)).state).toBe("would-remove");expect((await listSessionArtifacts(one)).total).toBe(1);
- expect((await pruneSessionArtifacts(one,false,future)).state).toBe("removed");expect(await readSessionFile(one.id)).not.toBeNull();
+ const future=Date.now()+9*86400000;expect((await pruneSessionArtifacts(one,true,future)).state).toBe("protected");expect((await listSessionArtifacts(one)).total).toBe(1);
+ expect((await pruneSessionArtifacts(one,false,future)).state).toBe("protected");expect(await readSessionFile(one.id)).not.toBeNull();expect((await listSessionArtifacts(one)).total).toBe(1);
});
it("never follows foreign files during cleanup and never removes another principal's session",async()=>{
await prepareSessionArtifacts(one);await saveSessionArtifact(one,png,meta);const p=artifactPaths(one);await fs.writeFile(path.join(p.directory,"unknown.txt"),"preserve",{mode:0o600});
- expect((await pruneSessionArtifacts(one,false,Date.now()+9*86400000)).state).toBe("unknown-files");
+ expect((await pruneSessionArtifacts(one,false,Date.now()+9*86400000)).state).toBe("protected");
const report=await cleanupSessionArtifacts({principal:other,dryRun:false});expect(report.results.some(r=>r.sessionId===one.id)).toBe(false);
});
});
diff --git a/lib/agent/session-action-history.test.ts b/lib/agent/session-action-history.test.ts
index a822b447..b16348fe 100644
--- a/lib/agent/session-action-history.test.ts
+++ b/lib/agent/session-action-history.test.ts
@@ -8,10 +8,11 @@ import { resolveSessionFlowAction } from "./session-flow";
import type { AgentSession } from "./session-types";
const root = await mkdtemp(path.join(os.tmpdir(), "mso-action-history-"));
-process.env.OS_AGENT_SESSION_ARCHIVE_DIR = root;
+process.env.OS_AGENT_SESSION_ARCHIVE_DIR = path.join(root, "archives");
+process.env.OS_AGENT_SESSIONS_DIR = path.join(root, "sessions");
const history = await import("./session-action-history");
-afterAll(async () => { await rm(root, { recursive: true, force: true }); });
+afterAll(async () => { delete process.env.OS_AGENT_SESSION_ARCHIVE_DIR; delete process.env.OS_AGENT_SESSIONS_DIR; await rm(root, { recursive: true, force: true }); });
describe("archive-backed session action lookup", () => {
it("keeps evicted action refs resolvable without crossing principals", async () => {
@@ -37,3 +38,24 @@ describe("archive-backed session action lookup", () => {
await expect(history.resolveHistoricalSessionAction("mcp-client:other", session, "E1")).rejects.toThrow("session not found");
});
});
+
+
+describe("preserved action index overflow", () => {
+ it("resolves an older event after more than 5000 records without crossing principals", async () => {
+ const principal = "mcp-client:overflow", now = Date.now();
+ const events = normalizeSessionEventSemantics(Array.from({ length: 5001 }, (_, index) => ({
+ at: new Date(now + index).toISOString(), kind: "tool" as const, tool: "fs_read", state: "completed", detail: `file-${index}.md`,
+ })));
+ const session: AgentSession = {
+ id: "20260925_020202_cafebabe", principalHash: createHash("sha256").update(principal).digest("hex"), source: "mcp", name: "overflow", title: "Overflow", titleSource: "auto",
+ createdAt: events[0]!.at, updatedAt: events.at(-1)!.at, cwd: "/srv/project", memorySnapshot: { capturedAt: events[0]!.at, user: "", memory: "" }, history: [],
+ events: [], eventSeqBase: 5001, estimatedTokens: 0, lifetimeEstimatedTokens: 0, compactThresholdTokens: 700000, compactionCount: 0, archiveCount: 0,
+ };
+ await history.archiveDroppedSessionEvents(session, events, 0);
+ const index = await import("./session-action-index");
+ expect((await index.readActionIndex(index.actionIndexPath(session.id)))?.records).toHaveLength(5000);
+ expect(await index.readActionSegments(session.id)).toHaveLength(1);
+ expect((await history.resolveHistoricalSessionAction(principal, session, "E1"))?.action.eventRef).toBe("E1");
+ await expect(history.resolveHistoricalSessionAction("mcp-client:other", session, "E1")).rejects.toThrow("session not found");
+ });
+});
diff --git a/lib/agent/session-action-history.ts b/lib/agent/session-action-history.ts
index 1f442649..c8dfc126 100644
--- a/lib/agent/session-action-history.ts
+++ b/lib/agent/session-action-history.ts
@@ -1,105 +1,13 @@
-import { createHash, randomUUID } from "node:crypto";
+import { createHash } from "node:crypto";
import { constants, promises as fs } from "node:fs";
import path from "node:path";
import { gunzipSync } from "node:zlib";
-import { withSecurityStoreLock } from "@/lib/security-store-lock";
-import { redactText } from "@/lib/security/redact-text";
import { agentSessionArchiveRoot, archiveRetentionDays } from "./session-archive";
+import { sessionPreservation } from "./session-preservation";
import { resolveSessionFlowAction } from "./session-flow";
-import { validSessionArtifactRevision } from "./session-artifact-history";
import type { AgentSession, AgentSessionEvent } from "./session-types";
-
-const MAX_BYTES = 4 * 1024 * 1024;
-const MAX_ACTION_RECORDS = 5000;
-const KINDS = new Set(["created", "resumed", "tool", "workflow", "note", "compacted", "archived"]);
-const INDEX_FILE = /^(\d{8}_\d{6}_[a-f0-9]{8})__actions\.json$/;
-
-type ArchivedActionRecord = { sequence: number; event: AgentSessionEvent };
-type ArchivedActionIndex = {
- schemaVersion: 1;
- sessionId: string;
- principalHash: string;
- cwd?: string;
- updatedAt: string;
- records: ArchivedActionRecord[];
-};
-
-function validEvent(value: unknown): value is AgentSessionEvent {
- if (!value || typeof value !== "object") return false;
- const row = value as AgentSessionEvent;
- return typeof row.at === "string" && Number.isFinite(Date.parse(row.at)) && KINDS.has(row.kind) &&
- ["tool", "state", "workflowId", "detail"].every((key) => row[key as keyof AgentSessionEvent] === undefined || (typeof row[key as keyof AgentSessionEvent] === "string" && String(row[key as keyof AgentSessionEvent]).length <= 16000)) &&
- (row.semantic === undefined || (row.semantic.version === 1 && Number.isSafeInteger(row.semantic.step) && row.semantic.step > 0 &&
- Number.isSafeInteger(row.semantic.action) && row.semantic.action > 0 &&
- ["context", "plan", "inspect", "implement", "verify", "integrate", "deploy", "result", "other"].includes(row.semantic.category))) &&
- (row.artifactRevision === undefined || validSessionArtifactRevision(row.artifactRevision));
-}
-function events(value: unknown): value is AgentSessionEvent[] {
- return Array.isArray(value) && value.length <= 10000 && value.every(validEvent);
-}
-function safeEvent(event: AgentSessionEvent): AgentSessionEvent {
- return {
- at: new Date(event.at).toISOString(), kind: event.kind,
- ...(event.tool ? { tool: event.tool.slice(0, 120) } : {}),
- ...(event.state ? { state: event.state.slice(0, 40) } : {}),
- ...(event.workflowId ? { workflowId: event.workflowId.slice(0, 80) } : {}),
- ...(event.detail ? { detail: redactText(event.detail, 500) } : {}),
- ...(event.semantic ? { semantic: event.semantic } : {}),
- ...(event.artifactRevision && validSessionArtifactRevision(event.artifactRevision) ? { artifactRevision: event.artifactRevision } : {}),
- };
-}
-function actionIndexPath(sessionId: string): string {
- if (!/^\d{8}_\d{6}_[a-f0-9]{8}$/.test(sessionId)) throw new Error("invalid agent session id");
- return path.join(agentSessionArchiveRoot(), `${sessionId}__actions.json`);
-}
-async function secureArchiveRoot(): Promise<string> {
- const root = agentSessionArchiveRoot();
- await fs.mkdir(root, { recursive: true, mode: 0o700 });
- await fs.chmod(root, 0o700).catch(() => undefined);
- const stat = await fs.lstat(root);
- if (!stat.isDirectory() || stat.isSymbolicLink() || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) throw new Error("unsafe agent session archive root");
- return root;
-}
-async function readActionIndex(target: string): Promise<ArchivedActionIndex | null> {
- let handle: Awaited<ReturnType<typeof fs.open>> | undefined;
- try {
- handle = await fs.open(target, constants.O_RDONLY | constants.O_NOFOLLOW);
- const stat = await handle.stat();
- if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_BYTES || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) return null;
- const body = await handle.readFile("utf8");
- const parsed = JSON.parse(body) as Partial<ArchivedActionIndex>;
- if (parsed.schemaVersion !== 1 || typeof parsed.sessionId !== "string" || typeof parsed.principalHash !== "string" || !Array.isArray(parsed.records) || parsed.records.length > MAX_ACTION_RECORDS) return null;
- const records = parsed.records.filter((row): row is ArchivedActionRecord => Boolean(row && Number.isSafeInteger(row.sequence) && row.sequence > 0 && validEvent(row.event)));
- return { schemaVersion: 1, sessionId: parsed.sessionId, principalHash: parsed.principalHash, ...(typeof parsed.cwd === "string" ? { cwd: parsed.cwd.slice(0, 4096) } : {}), updatedAt: typeof parsed.updatedAt === "string" ? parsed.updatedAt : new Date(0).toISOString(), records };
- } catch (error) {
- if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
- throw error;
- } finally { await handle?.close().catch(() => undefined); }
-}
-
-/** Persist only events that are about to leave the hot 400-event session ledger. */
-export async function archiveDroppedSessionEvents(session: Pick<AgentSession, "id" | "principalHash" | "cwd">, dropped: AgentSessionEvent[], eventSeqBase: number): Promise<void> {
- if (!dropped.length) return;
- await secureArchiveRoot();
- const target = actionIndexPath(session.id), cutoff = Date.now() - archiveRetentionDays() * 86_400_000;
- await withSecurityStoreLock(target, async () => {
- const current = await readActionIndex(target);
- if (current && (current.sessionId !== session.id || current.principalHash !== session.principalHash)) throw new Error("session action index owner mismatch");
- const existing = current?.records.filter((row) => Date.parse(row.event.at) >= cutoff) ?? [];
- const bySequence = new Map(existing.map((row) => [row.sequence, row]));
- dropped.forEach((event, index) => bySequence.set(eventSeqBase + index + 1, { sequence: eventSeqBase + index + 1, event: safeEvent(event) }));
- const records = [...bySequence.values()].sort((a, b) => a.sequence - b.sequence).slice(-MAX_ACTION_RECORDS);
- const next: ArchivedActionIndex = { schemaVersion: 1, sessionId: session.id, principalHash: session.principalHash, ...(session.cwd ? { cwd: session.cwd } : {}), updatedAt: new Date().toISOString(), records };
- const body = JSON.stringify(next) + "\n";
- if (Buffer.byteLength(body) > MAX_BYTES) throw new Error("session action index exceeds 4 MiB");
- const tmp = `${target}.${randomUUID()}.tmp`;
- try {
- await fs.writeFile(tmp, body, { flag: "wx", mode: 0o600 });
- await fs.rename(tmp, target);
- await fs.chmod(target, 0o600).catch(() => undefined);
- } finally { await fs.unlink(tmp).catch(() => undefined); }
- });
-}
+import { MAX_BYTES, INDEX_FILE, validActionEvents, actionIndexPath, secureArchiveRoot, readActionIndex, readActionSegments } from "./session-action-index";
+export { archiveDroppedSessionEvents } from "./session-action-index";
export type HistoricalSessionActionRecord = {
resolution: NonNullable<ReturnType<typeof resolveSessionFlowAction>>;
@@ -117,13 +25,16 @@ function resolveEventRecord(events: AgentSessionEvent[], actionRef: string, cwd:
}
async function resolveActionIndex(session: AgentSession, actionRef: string): Promise<HistoricalSessionActionRecord | null> {
- const index = await readActionIndex(actionIndexPath(session.id)).catch(() => null);
- if (!index || index.sessionId !== session.id || index.principalHash !== session.principalHash) return null;
- const cutoff = Date.now() - archiveRetentionDays() * 86_400_000;
- for (const row of index.records) {
- if (Date.parse(row.event.at) < cutoff) continue;
- const resolution = resolveSessionFlowAction([row.event], actionRef, session.cwd ?? index.cwd, row.sequence - 1);
- if (resolution) return { resolution, event: row.event, sequence: row.sequence };
+ const targets = [actionIndexPath(session.id), ...await readActionSegments(session.id)];
+ const cutoff = (await sessionPreservation(session.id)).protected ? -Infinity : Date.now() - archiveRetentionDays() * 86_400_000;
+ for (const target of targets) {
+ const index = await readActionIndex(target).catch(() => null);
+ if (!index || index.sessionId !== session.id || index.principalHash !== session.principalHash) continue;
+ for (const row of index.records) {
+ if (Date.parse(row.event.at) < cutoff) continue;
+ const resolution = resolveSessionFlowAction([row.event], actionRef, session.cwd ?? index.cwd, row.sequence - 1);
+ if (resolution) return { resolution, event: row.event, sequence: row.sequence };
+ }
}
return null;
}
@@ -142,7 +53,7 @@ async function resolveHistoricalSessionActionRecordOwned(session: AgentSession,
const stat = await fs.lstat(root);
if (!stat.isDirectory() || stat.isSymbolicLink() || !owned(stat)) return null;
} catch { return null; }
- const seen = new Set<string>(), cutoff = Date.now() - archiveRetentionDays() * 86400000;
+ const seen = new Set<string>(), cutoff = (await sessionPreservation(session.id)).protected ? -Infinity : Date.now() - archiveRetentionDays() * 86400000;
const directory = await fs.opendir(root), names: string[] = [];
let scanned = 0;
for await (const entry of directory) {
@@ -161,7 +72,7 @@ async function resolveHistoricalSessionActionRecordOwned(session: AgentSession,
if (seen.has(digest)) continue; seen.add(digest);
const payload = JSON.parse(gunzipSync(compressed, { maxOutputLength: MAX_BYTES }).toString("utf8"));
const archived = payload?.session;
- if (payload?.schemaVersion !== 1 || typeof payload.archivedAt !== "string" || !Number.isFinite(Date.parse(payload.archivedAt)) || Date.parse(payload.archivedAt) < cutoff || archived?.id !== session.id || archived?.principalHash !== owner || !events(archived.events)) continue;
+ if (payload?.schemaVersion !== 1 || typeof payload.archivedAt !== "string" || !Number.isFinite(Date.parse(payload.archivedAt)) || Date.parse(payload.archivedAt) < cutoff || archived?.id !== session.id || archived?.principalHash !== owner || !validActionEvents(archived.events)) continue;
const base = archived.eventSeqBase ?? 0;
if (!Number.isSafeInteger(base) || base < 0) continue;
const record = resolveEventRecord(archived.events, actionRef, session.cwd, base);
@@ -192,6 +103,7 @@ export async function pruneSessionActionIndexes(now = Date.now()): Promise<numbe
const names = await fs.readdir(root).catch(() => [] as string[]); let removed = 0;
for (const name of names) {
if (!INDEX_FILE.test(name)) continue;
+ if ((await sessionPreservation(name.split("__")[0])).protected) continue;
const file = path.join(root, name);
try {
const stat = await fs.lstat(file);
diff --git a/lib/agent/session-action-index.ts b/lib/agent/session-action-index.ts
new file mode 100644
index 00000000..5a35501a
--- /dev/null
+++ b/lib/agent/session-action-index.ts
@@ -0,0 +1,122 @@
+import { createHash, randomUUID } from "node:crypto";
+import { sessionPreservation } from "./session-preservation";
+import { writeWorkflowFile } from "@/lib/workflow/private-file";
+import { constants, promises as fs } from "node:fs";
+import path from "node:path";
+import { withSecurityStoreLock } from "@/lib/security-store-lock";
+import { redactText } from "@/lib/security/redact-text";
+import { agentSessionArchiveRoot, archiveRetentionDays } from "./session-archive";
+import { validSessionArtifactRevision } from "./session-artifact-history";
+import type { AgentSession, AgentSessionEvent } from "./session-types";
+
+export const MAX_BYTES = 4 * 1024 * 1024;
+const MAX_ACTION_RECORDS = 5000;
+const KINDS = new Set(["created", "resumed", "tool", "workflow", "note", "compacted", "archived"]);
+export const INDEX_FILE = /^(\d{8}_\d{6}_[a-f0-9]{8})__actions\.json$/;
+
+type ArchivedActionRecord = { sequence: number; event: AgentSessionEvent };
+type ArchivedActionIndex = {
+ schemaVersion: 1;
+ sessionId: string;
+ principalHash: string;
+ cwd?: string;
+ updatedAt: string;
+ records: ArchivedActionRecord[];
+};
+
+function validEvent(value: unknown): value is AgentSessionEvent {
+ if (!value || typeof value !== "object") return false;
+ const row = value as AgentSessionEvent;
+ return typeof row.at === "string" && Number.isFinite(Date.parse(row.at)) && KINDS.has(row.kind) &&
+ ["tool", "state", "workflowId", "detail"].every((key) => row[key as keyof AgentSessionEvent] === undefined || (typeof row[key as keyof AgentSessionEvent] === "string" && String(row[key as keyof AgentSessionEvent]).length <= 16000)) &&
+ (row.semantic === undefined || (row.semantic.version === 1 && Number.isSafeInteger(row.semantic.step) && row.semantic.step > 0 &&
+ Number.isSafeInteger(row.semantic.action) && row.semantic.action > 0 &&
+ ["context", "plan", "inspect", "implement", "verify", "integrate", "deploy", "result", "other"].includes(row.semantic.category))) &&
+ (row.artifactRevision === undefined || validSessionArtifactRevision(row.artifactRevision));
+}
+export function validActionEvents(value: unknown): value is AgentSessionEvent[] {
+ return Array.isArray(value) && value.length <= 10000 && value.every(validEvent);
+}
+function safeEvent(event: AgentSessionEvent): AgentSessionEvent {
+ return {
+ at: new Date(event.at).toISOString(), kind: event.kind,
+ ...(event.tool ? { tool: event.tool.slice(0, 120) } : {}),
+ ...(event.state ? { state: event.state.slice(0, 40) } : {}),
+ ...(event.workflowId ? { workflowId: event.workflowId.slice(0, 80) } : {}),
+ ...(event.detail ? { detail: redactText(event.detail, 500) } : {}),
+ ...(event.semantic ? { semantic: event.semantic } : {}),
+ ...(event.artifactRevision && validSessionArtifactRevision(event.artifactRevision) ? { artifactRevision: event.artifactRevision } : {}),
+ };
+}
+export function actionIndexPath(sessionId: string): string {
+ if (!/^\d{8}_\d{6}_[a-f0-9]{8}$/.test(sessionId)) throw new Error("invalid agent session id");
+ return path.join(agentSessionArchiveRoot(), `${sessionId}__actions.json`);
+}
+export async function secureArchiveRoot(): Promise<string> {
+ const root = agentSessionArchiveRoot();
+ await fs.mkdir(root, { recursive: true, mode: 0o700 });
+ await fs.chmod(root, 0o700).catch(() => undefined);
+ const stat = await fs.lstat(root);
+ if (!stat.isDirectory() || stat.isSymbolicLink() || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) throw new Error("unsafe agent session archive root");
+ return root;
+}
+export async function readActionIndex(target: string): Promise<ArchivedActionIndex | null> {
+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;
+ try {
+ handle = await fs.open(target, constants.O_RDONLY | constants.O_NOFOLLOW);
+ const stat = await handle.stat();
+ if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_BYTES || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) return null;
+ const body = await handle.readFile("utf8");
+ const parsed = JSON.parse(body) as Partial<ArchivedActionIndex>;
+ if (parsed.schemaVersion !== 1 || typeof parsed.sessionId !== "string" || typeof parsed.principalHash !== "string" || !Array.isArray(parsed.records) || parsed.records.length > MAX_ACTION_RECORDS) return null;
+ const records = parsed.records.filter((row): row is ArchivedActionRecord => Boolean(row && Number.isSafeInteger(row.sequence) && row.sequence > 0 && validEvent(row.event)));
+ return { schemaVersion: 1, sessionId: parsed.sessionId, principalHash: parsed.principalHash, ...(typeof parsed.cwd === "string" ? { cwd: parsed.cwd.slice(0, 4096) } : {}), updatedAt: typeof parsed.updatedAt === "string" ? parsed.updatedAt : new Date(0).toISOString(), records };
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
+ throw error;
+ } finally { await handle?.close().catch(() => undefined); }
+}
+
+/** Persist only events that are about to leave the hot 400-event session ledger. */
+export async function archiveDroppedSessionEvents(session: Pick<AgentSession, "id" | "principalHash" | "cwd">, dropped: AgentSessionEvent[], eventSeqBase: number): Promise<void> {
+ if (!dropped.length) return;
+ await secureArchiveRoot();
+ const target = actionIndexPath(session.id), cutoff = Date.now() - archiveRetentionDays() * 86_400_000;
+ await withSecurityStoreLock(target, async () => {
+ const current = await readActionIndex(target);
+ if (current && (current.sessionId !== session.id || current.principalHash !== session.principalHash)) throw new Error("session action index owner mismatch");
+ const preserve = (await sessionPreservation(session.id)).protected;
+ const existing = current?.records.filter((row) => preserve || Date.parse(row.event.at) >= cutoff) ?? [];
+ const bySequence = new Map(existing.map((row) => [row.sequence, row]));
+ dropped.forEach((event, index) => bySequence.set(eventSeqBase + index + 1, { sequence: eventSeqBase + index + 1, event: safeEvent(event) }));
+ const ordered = [...bySequence.values()].sort((a, b) => a.sequence - b.sequence);
+ if (preserve && ordered.length > MAX_ACTION_RECORDS) {
+ const overflow = ordered.slice(0, -MAX_ACTION_RECORDS);
+ const segment = JSON.stringify({ schemaVersion: 1, sessionId: session.id, principalHash: session.principalHash, cwd: session.cwd, updatedAt: new Date().toISOString(), records: overflow });
+ if (Buffer.byteLength(segment) > MAX_BYTES) throw new Error("preserved action segment exceeds limit");
+ await writeWorkflowFile(path.join(agentSessionArchiveRoot(), `${session.id}__actions-part-${createHash("sha256").update(segment).digest("hex")}.json`), segment);
+ }
+ const records = ordered.slice(-MAX_ACTION_RECORDS);
+ const next: ArchivedActionIndex = { schemaVersion: 1, sessionId: session.id, principalHash: session.principalHash, ...(session.cwd ? { cwd: session.cwd } : {}), updatedAt: new Date().toISOString(), records };
+ const body = JSON.stringify(next) + "\n";
+ if (Buffer.byteLength(body) > MAX_BYTES) throw new Error("session action index exceeds 4 MiB");
+ const tmp = `${target}.${randomUUID()}.tmp`;
+ try {
+ await fs.writeFile(tmp, body, { flag: "wx", mode: 0o600 });
+ await fs.rename(tmp, target);
+ await fs.chmod(target, 0o600).catch(() => undefined);
+ } finally { await fs.unlink(tmp).catch(() => undefined); }
+ });
+}
+
+/** Bounded immutable overflow segments; contents stay subject to the same index validation. */
+export async function readActionSegments(sessionId: string) {
+ const root = await secureArchiveRoot(), names: string[] = [];
+ let seen = 0;
+ for await (const entry of await fs.opendir(root)) {
+ if (++seen > 10000) break;
+ if (entry.isFile() && entry.name.startsWith(`${sessionId}__actions-part-`) &&
+ /^\d{8}_\d{6}_[a-f0-9]{8}__actions-part-[a-f0-9]{64}\.json$/.test(entry.name)) names.push(entry.name);
+ }
+ return names.sort().reverse().slice(0, 64).map(name => path.join(root, name));
+}
diff --git a/lib/agent/session-archive.ts b/lib/agent/session-archive.ts
index 33a0197c..4a18e97f 100644
--- a/lib/agent/session-archive.ts
+++ b/lib/agent/session-archive.ts
@@ -1,4 +1,5 @@
import { randomUUID } from "node:crypto";
+import { sessionPreservation } from "./session-preservation";
import { constants as fsConstants, promises as fs } from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -60,6 +61,7 @@ export async function pruneAgentSessionArchives(now = Date.now()): Promise<{ rem
let removed = 0, kept = 0;
for (const name of names) {
if (!ARCHIVE_FILE.test(name)) continue;
+ if ((await sessionPreservation(name.split("__")[0])).protected) { kept++; continue; }
const file = path.join(root, name);
let handle: Awaited<ReturnType<typeof fs.open>> | null = null;
try {
diff --git a/lib/agent/session-preservation.test.ts b/lib/agent/session-preservation.test.ts
new file mode 100644
index 00000000..8519c072
--- /dev/null
+++ b/lib/agent/session-preservation.test.ts
@@ -0,0 +1,22 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { mkdtemp, rm } from "node:fs/promises";
+import os from "node:os";
+import path from "node:path";
+import { preservationDecision, sessionPreservation, updateSessionPreservation } from "./session-preservation";
+const roots: string[] = [];
+afterEach(async () => { vi.unstubAllEnvs(); for (const root of roots.splice(0)) await rm(root, { recursive: true, force: true }); });
+describe("source preservation policy", () => {
+ it("never treats age or a learned/reviewed claim as permission to delete", () => {
+ expect(preservationDecision(null)).toMatchObject({ protected: true, reason: "unreviewed" });
+ expect(preservationDecision({ version: 1, sessionId: "20260925_010101_deadbeef", pinned: false, state: "reviewed", updatedAt: new Date().toISOString() })).toMatchObject({ protected: true, reason: "source-release-not-approved" });
+ });
+ it("persists pinning with optimistic revision checks", async () => {
+ const root = await mkdtemp(path.join(os.tmpdir(), "mso-preservation-")); roots.push(root); vi.stubEnv("OS_AGENT_SESSIONS_DIR", root);
+ const id = "20260925_010101_deadbeef", before = await sessionPreservation(id);
+ expect(before.reason).toBe("unreviewed");
+ await updateSessionPreservation(id, before.revision, { pinned: true });
+ expect(await sessionPreservation(id)).toMatchObject({ protected: true, reason: "pinned" });
+ await expect(updateSessionPreservation(id, before.revision, { pinned: false })).rejects.toThrow("refresh");
+ });
+ it("fails closed for malformed identities", async () => { expect(await sessionPreservation("../escape")).toMatchObject({ protected: true, reason: "preservation-unavailable" }); });
+});
diff --git a/lib/agent/session-preservation.ts b/lib/agent/session-preservation.ts
new file mode 100644
index 00000000..344cf5e9
--- /dev/null
+++ b/lib/agent/session-preservation.ts
@@ -0,0 +1,73 @@
+import { createHash } from "node:crypto";
+import path from "node:path";
+import { withSecurityStoreLock } from "@/lib/security-store-lock";
+import { readWorkflowJson, writeWorkflowFile } from "@/lib/workflow/private-file";
+import { agentSessionsDir, SESSION_ID } from "./session-paths";
+
+export type PreservationRecord = {
+ version: 1;
+ sessionId: string;
+ pinned: boolean;
+ state: "unreviewed" | "reviewed";
+ updatedAt: string;
+ reviewedThrough?: string;
+ evidenceRef?: string;
+};
+export type PreservationDecision = { protected: boolean; reason: string; record: PreservationRecord | null };
+const digest = (value: unknown) => createHash("sha256").update(JSON.stringify(value)).digest("hex");
+function file(id: string): string {
+ if (!SESSION_ID.test(id)) throw new Error("invalid session id");
+ return path.join(agentSessionsDir(), ".preservation", `${id}.json`);
+}
+
+export async function readSessionPreservation(id: string): Promise<PreservationRecord | null> {
+ try {
+ const row = await readWorkflowJson(file(id), 8192, "session preservation receipt") as PreservationRecord;
+ if (row.version !== 1 || row.sessionId !== id || typeof row.pinned !== "boolean" ||
+ !["unreviewed", "reviewed"].includes(row.state) || !Number.isFinite(Date.parse(row.updatedAt))) {
+ throw new Error("invalid session preservation receipt");
+ }
+ return row;
+ } catch (error) {
+ if ((error as NodeJS.ErrnoException).code === "ENOENT") return null;
+ throw error;
+ }
+}
+
+/** Review is knowledge provenance, NOT authorization to delete the source.
+ * Until a restore-verified, per-artifact release receipt exists, retention is
+ * deliberately fail-closed. No age, successful workflow, or model score can
+ * authorize destroying raw evidence. Old installations default to protected. */
+export function preservationDecision(record: PreservationRecord | null): PreservationDecision {
+ if (!record) return { protected: true, reason: "unreviewed", record };
+ if (record.pinned) return { protected: true, reason: "pinned", record };
+ return { protected: true, reason: record.state === "reviewed" ? "source-release-not-approved" : "unreviewed", record };
+}
+
+export async function sessionPreservation(id: string): Promise<PreservationDecision & { revision: string }> {
+ try {
+ const record = await readSessionPreservation(id);
+ return { ...preservationDecision(record), revision: digest(record) };
+ } catch {
+ return { protected: true, reason: "preservation-unavailable", record: null, revision: "unavailable" };
+ }
+}
+
+/** Caller must prove session access and an owner role before changing this receipt. */
+export async function updateSessionPreservation(id: string, expectedRevision: string,
+ change: { pinned?: boolean; reviewedThrough?: string; evidenceRef?: string }): Promise<PreservationRecord> {
+ return withSecurityStoreLock(file(id), async () => {
+ const prior = await readSessionPreservation(id);
+ if (expectedRevision !== digest(prior)) throw new Error("preservation changed; refresh before editing");
+ if (change.reviewedThrough !== undefined && !Number.isFinite(Date.parse(change.reviewedThrough))) throw new Error("invalid review timestamp");
+ if (change.reviewedThrough && (!change.evidenceRef || !/^[a-zA-Z0-9_.:-]{1,160}$/.test(change.evidenceRef))) throw new Error("review evidence reference required");
+ const next: PreservationRecord = {
+ version: 1, sessionId: id, pinned: change.pinned ?? prior?.pinned ?? false,
+ state: change.reviewedThrough ? "reviewed" : prior?.state ?? "unreviewed", updatedAt: new Date().toISOString(),
+ ...(prior?.reviewedThrough ? { reviewedThrough: prior.reviewedThrough, evidenceRef: prior.evidenceRef } : {}),
+ ...(change.reviewedThrough ? { reviewedThrough: change.reviewedThrough, evidenceRef: change.evidenceRef } : {}),
+ };
+ await writeWorkflowFile(file(id), JSON.stringify(next));
+ return next;
+ });
+}
diff --git a/lib/agent/session-store.test.ts b/lib/agent/session-store.test.ts
index e7d28c74..9ee9b73a 100644
--- a/lib/agent/session-store.test.ts
+++ b/lib/agent/session-store.test.ts
@@ -50,14 +50,15 @@ describe("durable agent session context policy", () => {
expect(archived).toContain("[redacted]");
});
- it("prunes archives older than the default 30-day retention window", async () => {
+ it("preserves unreviewed archives beyond the 30-day age window", async () => {
const [name] = (await fs.readdir(archives)).filter((row) => row.endsWith(".json.gz"));
const file = path.join(archives, name);
const old = new Date(Date.now() - 31 * 86_400_000);
await fs.utimes(file, old, old);
const result = await archive.pruneAgentSessionArchives();
- expect(result.removed).toBe(1);
- await expect(fs.stat(file)).rejects.toMatchObject({ code: "ENOENT" });
+ expect(result.removed).toBe(0);
+ expect(result.kept).toBeGreaterThan(0);
+ expect((await fs.stat(file)).isFile()).toBe(true);
});
it("allows an explicit rename to replace a manual title and refresh modified time", async () => {
diff --git a/lib/contracts/audit.ts b/lib/contracts/audit.ts
index 2796967e..68cc543a 100644
--- a/lib/contracts/audit.ts
+++ b/lib/contracts/audit.ts
@@ -14,6 +14,7 @@ export type AuditAction =
| "fs.upload"
| "fs.zip"
| "sys.cleanup"
+ | "sys.memory-backup"
| "sys.service"
/** The cockpit replacing its own code (Settings → About, or `mso update run`). */
| "sys.update"
diff --git a/lib/contracts/memory-backup.ts b/lib/contracts/memory-backup.ts
new file mode 100644
index 00000000..a17b101c
--- /dev/null
+++ b/lib/contracts/memory-backup.ts
@@ -0,0 +1,7 @@
+export type MemoryBackupScan = { files: number; bytes: number; missing: number; rejected: number; excluded: number; truncated: boolean };
+export type MemoryBackupSummary = {
+ id: string; state: "snapshot" | "partial"; createdAt: string; directory: string; manifestSha256: string;
+ scan: MemoryBackupScan; compressedBytes: number; sourceDiscoveryTruncated: boolean;
+ consistency: "per-file-verified-not-point-in-time"; offsite: false;
+};
+export type MemoryBackupVerification = { id: string; integrity: boolean; restoredFiles: number; restoreDirectory: string; complete: boolean; sourceWritesPerformed: 0 };
diff --git a/lib/contracts/workflow-discovery.ts b/lib/contracts/workflow-discovery.ts
index 5fa8aa7a..ddc39768 100644
--- a/lib/contracts/workflow-discovery.ts
+++ b/lib/contracts/workflow-discovery.ts
@@ -1,6 +1,6 @@
export type WorkflowDiscoveryRow = {
id: string; name: string; status: string; project?: string; updatedAt: string;
- originPrincipal: string; owner: string; readOnly: boolean; nodeCount: number;
+ originPrincipal: string; owner: string; revision: string; readOnly: boolean; nodeCount: number;
};
export type WorkflowDiscoveryPage = {
graphs: WorkflowDiscoveryRow[];
diff --git a/lib/host/cleanup-preview.test.ts b/lib/host/cleanup-preview.test.ts
new file mode 100644
index 00000000..766e5b76
--- /dev/null
+++ b/lib/host/cleanup-preview.test.ts
@@ -0,0 +1,17 @@
+import { describe, expect, it } from "vitest";
+import { consumeCleanupPreview, issueCleanupPreview } from "./cleanup-preview";
+const items = [{ id: "npm-cache", label: "npm", desc: "cache", bytes: 1, available: true }, { id: "tmp-old", label: "tmp", desc: "protected", bytes: 1, available: false }];
+describe("cleanup preview", () => {
+ it("requires explicit confirmation and makes the preview single-use", () => {
+ const preview = issueCleanupPreview(items, 1000);
+ expect(() => consumeCleanupPreview(preview.id, ["npm-cache"], false, 1001)).toThrow("confirmation");
+ consumeCleanupPreview(preview.id, ["npm-cache"], true, 1002);
+ expect(() => consumeCleanupPreview(preview.id, ["npm-cache"], true, 1003)).toThrow("expired");
+ });
+ it("refuses unavailable categories, duplicate selections and expired previews", () => {
+ const preview = issueCleanupPreview(items, 1000);
+ expect(() => consumeCleanupPreview(preview.id, ["tmp-old"], true, 1001)).toThrow("available");
+ expect(() => consumeCleanupPreview(preview.id, ["npm-cache", "npm-cache"], true, 1001)).toThrow("available");
+ expect(() => consumeCleanupPreview(preview.id, ["npm-cache"], true, 301001)).toThrow("expired");
+ });
+});
diff --git a/lib/host/cleanup-preview.ts b/lib/host/cleanup-preview.ts
new file mode 100644
index 00000000..9820b128
--- /dev/null
+++ b/lib/host/cleanup-preview.ts
@@ -0,0 +1,23 @@
+import { randomUUID } from "node:crypto";
+import type { CleanupItem } from "./cleanup";
+
+const TTL_MS = 5 * 60_000;
+const previews = new Map<string, { expiresAt: number; ids: Set<string> }>();
+export function issueCleanupPreview(items: CleanupItem[], now = Date.now()) {
+ for (const [key, value] of previews) if (value.expiresAt <= now) previews.delete(key);
+ if (previews.size >= 128) previews.delete(previews.keys().next().value!);
+ const id = randomUUID(), expiresAt = now + TTL_MS;
+ previews.set(id, { expiresAt, ids: new Set(items.filter((item) => item.available).map((item) => item.id)) });
+ return { id, expiresAt: new Date(expiresAt).toISOString() };
+}
+
+/** A fresh server preview + explicit choice is required, not a blanket delete. */
+export function consumeCleanupPreview(id: unknown, ids: string[], confirm: unknown, now = Date.now()): void {
+ if (confirm !== true || typeof id !== "string") throw new Error("preview and explicit confirmation required");
+ const preview = previews.get(id);
+ if (!preview || preview.expiresAt <= now) throw new Error("cleanup preview expired; rescan first");
+ if (!ids.length || new Set(ids).size !== ids.length || ids.some((item) => !preview.ids.has(item))) {
+ throw new Error("cleanup selection is not available in this preview");
+ }
+ previews.delete(id);
+}
diff --git a/lib/host/memory-backup-api.ts b/lib/host/memory-backup-api.ts
new file mode 100644
index 00000000..b5fe31c3
--- /dev/null
+++ b/lib/host/memory-backup-api.ts
@@ -0,0 +1 @@
+export { previewMemoryBackup, createMemoryBackup, verifyMemoryBackup } from "@/lib/memory-backup/service";
diff --git a/lib/memory-backup/scan.ts b/lib/memory-backup/scan.ts
index 432d39c0..fde54ad8 100644
--- a/lib/memory-backup/scan.ts
+++ b/lib/memory-backup/scan.ts
@@ -1,16 +1,14 @@
import { promises as fs } from "node:fs";
import path from "node:path";
-import { gunzipSync } from "node:zlib";
import { pinSecurityStorePath } from "@/lib/security-store-path";
-import { redactText } from "@/lib/security/redact-text";
import { existing, safeRelative, readBytes } from "./io";
import type { Source } from "./sources";
-export const LIMITS = { files: 5000, entries: 15000, bytes: 128 * 1024 * 1024, fileBytes: 8 * 1024 * 1024, ms: 20000 };
+export const LIMITS = { files: 10000, entries: 40000, bytes: 1024 * 1024 * 1024, fileBytes: 32 * 1024 * 1024, ms: 45000 };
export type Limits = typeof LIMITS;
-export type Scan = { files: number; bytes: number; missing: number; rejected: number; truncated: boolean };
-const excluded = /(?:^\.env|credential|secret|password|token|config|browser|profile|^\.ssh$|^\.git$|^node_modules$|^locks?$|\.lock(?:\.|$)|\.tmp$)/i;
+export type Scan = import("@/lib/contracts/memory-backup").MemoryBackupScan;
+const excluded = /(?:^\.env|credential|secret|password|token|config|browser|profile|^\.ssh$|^\.git$|^node_modules$|^\.?locks?$|workflow-variables|^variables\.json$|\.lock(?:\.|$)|\.tmp$)/i;
export async function scanSources(sources: Source[], limits: Limits, consume?: (name: string, data: Buffer) => Promise<void>): Promise<Scan> {
- const report: Scan = { files: 0, bytes: 0, missing: 0, rejected: 0, truncated: false };
+ const report: Scan = { files: 0, bytes: 0, missing: 0, rejected: 0, excluded: 0, truncated: false };
const deadline = Date.now() + limits.ms;
let entries = 0;
const stopped = () => {
@@ -30,23 +28,23 @@ export async function scanSources(sources: Source[], limits: Limits, consume?: (
const dir = await fs.opendir(path.dirname(pin.file));
for await (const entry of dir) {
if (stopped()) break;
- if (excluded.test(entry.name)) { entries++; continue; }
+ if (excluded.test(entry.name)) { entries++; report.excluded++; continue; }
await visit(path.join(file, entry.name), `${name}/${entry.name}`, depth + 1);
}
} finally { await pin.directory.close(); }
} else {
if (!stat.isFile() || excluded.test(path.basename(file))) { report.rejected++; return; }
if (stat.size > limits.fileBytes || stat.size + report.bytes > limits.bytes) { report.truncated = true; return; }
+ if (!consume) { report.files++; report.bytes += stat.size; return; }
const bytes = await readBytes(file, limits.fileBytes);
if (bytes.length + report.bytes > limits.bytes) { report.truncated = true; return; }
- // Never transform a snapshot silently: suspected embedded secrets omit the file.
- const text = (file.endsWith(".gz") ? gunzipSync(bytes, { maxOutputLength: limits.fileBytes }) : bytes).toString("utf8");
- if (/-----BEGIN [A-Z ]*PRIVATE KEY-----/.test(text) || redactText(text) !== text) { report.rejected++; return; }
- await consume?.(name, bytes);
+ // Private source bytes are copied exactly, never emitted to the caller.
+ // Credential/config/browser stores are outside the allowlisted sources.
+ await consume(name, bytes);
report.files++; report.bytes += bytes.length;
}
} catch (error) {
- if ((error as NodeJS.ErrnoException).code === "ENOENT") report.missing++;
+ if ((error as NodeJS.ErrnoException).code === "ENOENT" && depth === 0) report.missing++;
else report.rejected++;
}
}
diff --git a/lib/memory-backup/service.test.ts b/lib/memory-backup/service.test.ts
new file mode 100644
index 00000000..b087b86e
--- /dev/null
+++ b/lib/memory-backup/service.test.ts
@@ -0,0 +1,45 @@
+import { afterEach, describe, expect, it } from "vitest";
+import { promises as fs } from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { createSnapshot, verifySnapshot } from "./service";
+import { LIMITS, scanSources } from "./scan";
+const roots: string[] = [];
+async function fixture() {
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-memory-backup-")); roots.push(root);
+ const source = path.join(root, "source"); await fs.mkdir(source, { mode: 0o700 });
+ await fs.writeFile(path.join(source, "note.md"), "A durable decision", { mode: 0o600 });
+ return { root, source, destination: path.join(root, "backups") };
+}
+afterEach(async () => { for (const root of roots.splice(0)) await fs.rm(root, { recursive: true, force: true }); });
+describe("private memory snapshots", () => {
+ it("checksums exact copies and rehearses restore without changing sources", async () => {
+ const f = await fixture(), before = await fs.readFile(path.join(f.source, "note.md"));
+ const result = await createSnapshot([{ key: "memory", path: f.source }], f.destination);
+ expect(result.state).toBe("snapshot"); expect(result.scan.files).toBe(1);
+ const verified = await verifySnapshot(f.destination, result.id, result.manifestSha256);
+ expect(verified).toMatchObject({ integrity: true, complete: true, restoredFiles: 1, sourceWritesPerformed: 0 });
+ expect(await fs.readFile(path.join(f.source, "note.md"))).toEqual(before);
+ expect(await fs.readFile(path.join(verified.restoreDirectory, "memory", "note.md"))).toEqual(before);
+ expect((await fs.stat(path.join(verified.restoreDirectory, "memory", "note.md"))).mode & 0o777).toBe(0o600);
+ });
+ it("refuses altered files, wrong manifest checksums and traversal ids", async () => {
+ const f = await fixture(), result = await createSnapshot([{ key: "memory", path: f.source }], f.destination);
+ await expect(verifySnapshot(f.destination, result.id, "0".repeat(64))).rejects.toThrow("checksum");
+ await expect(verifySnapshot(f.destination, "../escape", result.manifestSha256)).rejects.toThrow("id");
+ await fs.writeFile(path.join(result.directory, "files", "memory", "note.md.gz"), "tampered");
+ await expect(verifySnapshot(f.destination, result.id, result.manifestSha256)).rejects.toThrow();
+ });
+ it("labels incomplete source scans and refuses symlinks without following them", async () => {
+ const f = await fixture(); await fs.symlink(path.join(f.source, "note.md"), path.join(f.source, "alias.md"));
+ const result = await createSnapshot([{ key: "memory", path: f.source }], f.destination);
+ expect(result.state).toBe("partial"); expect(result.scan.rejected).toBe(1);
+ const limited = await scanSources([{ key: "memory", path: f.source }], { ...LIMITS, bytes: 1 });
+ expect(limited.truncated).toBe(true);
+ });
+ it("excludes credential/config paths and reports the exclusions", async () => {
+ const f = await fixture(); await fs.writeFile(path.join(f.source, "credentials.json"), "private", { mode: 0o600 });
+ const result = await createSnapshot([{ key: "memory", path: f.source }], f.destination);
+ expect(result.scan.files).toBe(1); expect(result.scan.excluded).toBe(1);
+ });
+});
diff --git a/lib/memory-backup/service.ts b/lib/memory-backup/service.ts
new file mode 100644
index 00000000..0eb0f14f
--- /dev/null
+++ b/lib/memory-backup/service.ts
@@ -0,0 +1,99 @@
+import { promises as fs } from "node:fs";
+import path from "node:path";
+import os from "node:os";
+import { randomUUID } from "node:crypto";
+import { gzip, gunzipSync } from "node:zlib";
+import { promisify } from "node:util";
+import { memorySources, type Source } from "./sources";
+import { scanSources, LIMITS, type Limits, type Scan } from "./scan";
+import { digest, readBytes, safeRelative, writeExclusive } from "./io";
+import type { MemoryBackupSummary, MemoryBackupVerification } from "@/lib/contracts/memory-backup";
+
+const compress = promisify(gzip);
+const ID = /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/;
+type Entry = { name: string; bytes: number; sha256: string; compressedBytes: number; compressedSha256: string };
+type Manifest = { version: 1; id: string; createdAt: string; scan: Scan; sourceDiscoveryTruncated: boolean; entries: Entry[] };
+export const memoryBackupRoot = () => path.join(os.homedir(), ".mso", "backups", "memory");
+function target(root: string, id: string) {
+ if (!ID.test(id)) throw new Error("invalid memory backup id");
+ return path.join(root, id);
+}
+
+export async function previewMemoryBackup() {
+ const inventory = await memorySources();
+ const scan = await scanSources(inventory.sources, LIMITS);
+ return {
+ scan, sourceDiscoveryTruncated: inventory.incomplete, sourceCount: inventory.sources.length,
+ directory: memoryBackupRoot(), limits: LIMITS, offsite: false,
+ scope: "Existing allowlisted memory, session, workflow and organization sources; not a full VPS or database backup. Credential stores and browser profiles are excluded. Private source content remains private.",
+ consistency: "Per-file verified copies, not a transactionally consistent point-in-time snapshot.",
+ };
+}
+
+/** Core accepts only server-resolved sources. Never pass browser-provided paths here. */
+export async function createSnapshot(sources: Source[], root: string, sourceDiscoveryTruncated = false, limits: Limits = LIMITS): Promise<MemoryBackupSummary> {
+ const id = randomUUID(), directory = target(root, id), createdAt = new Date().toISOString();
+ const entries: Entry[] = [];
+ let compressedBytes = 0;
+ const scan = await scanSources(sources, limits, async (name, bytes) => {
+ const packed = await compress(bytes, { level: 3 });
+ await writeExclusive(path.join(directory, "files", `${name}.gz`), packed);
+ const check = await readBytes(path.join(directory, "files", `${name}.gz`), limits.fileBytes + 65536);
+ if (digest(check) !== digest(packed)) throw new Error("backup copy verification failed");
+ entries.push({ name, bytes: bytes.length, sha256: digest(bytes), compressedBytes: packed.length, compressedSha256: digest(packed) });
+ compressedBytes += packed.length;
+ });
+ const manifest: Manifest = { version: 1, id, createdAt, scan, sourceDiscoveryTruncated, entries };
+ const body = Buffer.from(JSON.stringify(manifest));
+ if (body.length > 8 * 1024 * 1024) throw new Error("backup manifest limit exceeded; partial files preserved");
+ await writeExclusive(path.join(directory, "manifest.json"), body);
+ const manifestSha256 = digest(await readBytes(path.join(directory, "manifest.json"), 8 * 1024 * 1024));
+ if (manifestSha256 !== digest(body)) throw new Error("backup manifest verification failed");
+ return { id, createdAt, directory, manifestSha256, scan, compressedBytes, sourceDiscoveryTruncated,
+ state: scan.files > 0 && !scan.truncated && !scan.rejected && !sourceDiscoveryTruncated ? "snapshot" : "partial",
+ consistency: "per-file-verified-not-point-in-time", offsite: false };
+}
+
+export async function createMemoryBackup() {
+ const space = await fs.statfs(os.homedir());
+ if (space.bavail * space.bsize < LIMITS.bytes * 3 + 1024 * 1024 * 1024) throw new Error("insufficient headroom for a backup and isolated restore; source files preserved");
+ const inventory = await memorySources();
+ return createSnapshot(inventory.sources, memoryBackupRoot(), inventory.incomplete);
+}
+
+/** Restore rehearsal writes a NEW isolated tree only; never restore over source. */
+export async function verifySnapshot(root: string, id: string, expectedManifestSha256: string): Promise<MemoryBackupVerification> {
+ if (!/^[a-f0-9]{64}$/.test(expectedManifestSha256)) throw new Error("manifest checksum required");
+ const directory = target(root, id), bytes = await readBytes(path.join(directory, "manifest.json"), 8 * 1024 * 1024);
+ if (digest(bytes) !== expectedManifestSha256) throw new Error("backup manifest checksum mismatch");
+ const manifest = JSON.parse(bytes.toString("utf8")) as Manifest;
+ if (manifest.version !== 1 || manifest.id !== id || !Array.isArray(manifest.entries) || manifest.entries.length > LIMITS.files || !manifest.scan) throw new Error("invalid backup manifest");
+ const names = new Set<string>();
+ let total = 0;
+ for (const row of manifest.entries) {
+ if (!row || !safeRelative(row.name) || names.has(row.name) || !Number.isSafeInteger(row.bytes) || row.bytes < 0 || row.bytes > LIMITS.fileBytes || !Number.isSafeInteger(row.compressedBytes) || row.compressedBytes < 0 || row.compressedBytes > LIMITS.fileBytes + 65536 || !/^[a-f0-9]{64}$/.test(row.sha256) || !/^[a-f0-9]{64}$/.test(row.compressedSha256)) throw new Error("unsafe backup entry");
+ names.add(row.name); total += row.bytes;
+ if (total > LIMITS.bytes) throw new Error("restore size limit exceeded");
+ }
+ const restoreDirectory = path.join(directory, `restore-check-${randomUUID()}`);
+ let restoredFiles = 0;
+ for (const row of manifest.entries) {
+ const packed = await readBytes(path.join(directory, "files", `${row.name}.gz`), row.compressedBytes);
+ if (digest(packed) !== row.compressedSha256) throw new Error("backup file checksum mismatch");
+ const plain = gunzipSync(packed, { maxOutputLength: LIMITS.fileBytes });
+ if (plain.length !== row.bytes || digest(plain) !== row.sha256) throw new Error("restored content checksum mismatch");
+ const destination = path.join(restoreDirectory, row.name);
+ await writeExclusive(destination, plain);
+ if (digest(await readBytes(destination, LIMITS.fileBytes)) !== row.sha256) throw new Error("restore rehearsal checksum mismatch");
+ restoredFiles++;
+ }
+ const result = { id, integrity: true, restoredFiles, restoreDirectory,
+ complete: restoredFiles > 0 && !manifest.scan.truncated && !manifest.scan.rejected && !manifest.sourceDiscoveryTruncated,
+ sourceWritesPerformed: 0 as const };
+ await writeExclusive(path.join(restoreDirectory, "verification.json"), Buffer.from(JSON.stringify(result)));
+ return result;
+}
+
+export async function verifyMemoryBackup(id: string, expectedManifestSha256: string) {
+ return verifySnapshot(memoryBackupRoot(), id, expectedManifestSha256);
+}
diff --git a/lib/memory-backup/sources.ts b/lib/memory-backup/sources.ts
index 76081bb0..79aa3b3f 100644
--- a/lib/memory-backup/sources.ts
+++ b/lib/memory-backup/sources.ts
@@ -1,4 +1,5 @@
import path from "node:path";
+import { createHash } from "node:crypto";
import os from "node:os";
import { agentSessionsDir } from "@/lib/agent/session-paths";
import { agentSessionArchiveRoot } from "@/lib/agent/session-archive";
@@ -9,17 +10,20 @@ export async function memorySources(): Promise<{ sources: Source[]; incomplete:
const home = path.join(os.homedir(), ".mso");
const recipes = workflowStorePath();
const sources: Source[] = [
- { key: "sessions", path: agentSessionsDir() },
{ key: "memory", path: path.resolve(process.env.OS_AGENT_MEMORY_DIR || path.join(home, "agent-memory")) },
{ key: "session-archive", path: agentSessionArchiveRoot() },
{ key: "recipes", path: recipes },
+ { key: "active-workflows", path: recipes + ".active.json" },
{ key: "recipe-archive", path: recipes + ".archive-v1" },
{ key: "organization", path: (process.env.OS_ORGANIZATION_STORE || path.join(home, "private", "organization.json")).replace(/^~(?=$|\/)/, os.homedir()) },
];
const projects = await listProjectDirs();
- projects.dirs.forEach(({ dir }, index) => {
+ projects.dirs.forEach(({ dir }) => {
+ const index = createHash("sha256").update(dir).digest("hex").slice(0, 16);
sources.push({ key: `project-${index}-agent`, path: path.join(dir, ".agent") });
sources.push({ key: `project-${index}-knowledge`, path: path.join(dir, ".mso", "KNOWLEDGE.md") });
});
- return { sources, incomplete: projects.scan.truncated || projects.scan.skippedRoots.length > 0 || projects.scan.skippedProjects > 0 };
+ // Preserve core memory and project evidence before the much larger session inventory.
+ sources.push({ key: "sessions", path: agentSessionsDir() });
+ return { sources, incomplete: projects.scan.truncated || projects.scan.skippedRoots.length > 0 };
}
diff --git a/lib/memory-graph/collect.ts b/lib/memory-graph/collect.ts
index e89ab07f..479d0188 100644
--- a/lib/memory-graph/collect.ts
+++ b/lib/memory-graph/collect.ts
@@ -2,6 +2,7 @@
// repo memory, the signed-in device's typed agent memory, and assistant memories.
import path from "node:path";
+import { workspaceSources } from "./workspace-sources";
import { queryAgentMemory } from "@/lib/agent/memory-store";
import { listMemories } from "@/lib/agent/legacy-owner-memory";
import type { AgentMemoryRecord } from "@/lib/agent/memory-types";
@@ -19,6 +20,7 @@ export interface CollectInput {
root: string;
project: string;
principal: string;
+ ownerView?: boolean;
}
function folderHubs(notes: GraphInputNode[]): { nodes: GraphInputNode[]; links: GraphInputLink[] } {
@@ -65,12 +67,12 @@ function agentPieces(records: Array<{ record: AgentMemoryRecord }>): { nodes: Gr
return { nodes, links };
}
-async function projectPieces(projectPath: string, projectId: string, projectName: string): Promise<{ nodes: GraphInputNode[]; links: GraphInputLink[] }> {
+async function projectPieces(projectPath: string, projectId: string, projectName: string): Promise<{ nodes: GraphInputNode[]; links: GraphInputLink[]; truncated: boolean }> {
const nodes: GraphInputNode[] = [];
const links: GraphInputLink[] = [];
const hub = `project:${projectId}`;
nodes.push({ id: hub, title: projectName, kind: "folder", group: projectName });
- const knowledge = await readProjectKnowledge(projectPath).catch(() => null);
+ const knowledge = await readProjectKnowledge(projectPath);
if (knowledge?.exists && knowledge.content.trim()) {
const id = `knowledge:${projectId}`;
nodes.push({
@@ -81,15 +83,15 @@ async function projectPieces(projectPath: string, projectId: string, projectName
});
links.push({ source: hub, targetId: id, kind: "contains" });
}
- const records = await listRepoMemoryRecords(projectPath, { limit: RECORD_CAP }).catch(() => []);
- for (const record of records) {
+ const records = await listRepoMemoryRecords(projectPath, { limit: RECORD_CAP + 1 });
+ for (const record of records.slice(0, RECORD_CAP)) {
const id = `memory:${projectId}:${record.id}`;
const summary = redactText(`${record.title}\n${record.summary}`, 160);
nodes.push({ id, title: record.title.slice(0, 80), kind: "memory", group: projectName, excerpt: summary, text: `${record.title}\n${record.summary}` });
links.push({ source: hub, targetId: id, kind: "contains" });
for (const prior of record.supersedes) links.push({ source: id, targetId: `memory:${projectId}:${prior}`, kind: "related" });
}
- return { nodes, links };
+ return { nodes, links, truncated: records.length > RECORD_CAP };
}
export async function collectMemoryGraph(input: CollectInput): Promise<MemoryGraphDocument> {
@@ -117,11 +119,13 @@ export async function collectMemoryGraph(input: CollectInput): Promise<MemoryGra
return { projects: [] as Array<{ id: string; name: string; path: string }> };
});
projectTargets.push(...listed.projects.slice(0, PROJECT_CAP));
+ if (("hasMore" in listed && listed.hasMore) || ("scan" in listed && listed.scan?.truncated)) truncated = true;
}
for (const project of projectTargets) {
try {
const piece = await projectPieces(project.path, project.id, project.name);
+ truncated ||= piece.truncated;
nodes.push(...piece.nodes);
links.push(...piece.links);
} catch (error) {
@@ -130,8 +134,9 @@ export async function collectMemoryGraph(input: CollectInput): Promise<MemoryGra
}
try {
- const agent = await queryAgentMemory(input.principal, { limit: 40 });
- const piece = agentPieces(agent.records);
+ const agent = await queryAgentMemory(input.principal, { limit: 41 });
+ if (agent.records.length > 40) truncated = true;
+ const piece = agentPieces(agent.records.slice(0, 40));
nodes.push(...piece.nodes);
links.push(...piece.links);
} catch (error) {
@@ -139,7 +144,9 @@ export async function collectMemoryGraph(input: CollectInput): Promise<MemoryGra
}
try {
- for (const memory of (await listMemories()).slice(0, 40)) {
+ const memories = await listMemories();
+ if (memories.length > 40) truncated = true;
+ for (const memory of memories.slice(0, 40)) {
nodes.push({
id: `owner:${memory.id}`,
title: memory.text.split("\n")[0]?.slice(0, 80) || "Memory",
@@ -153,6 +160,11 @@ export async function collectMemoryGraph(input: CollectInput): Promise<MemoryGra
warnings.push(error instanceof Error ? error.message.slice(0, 160) : "Assistant memory unavailable");
}
+ if (input.ownerView) {
+ const workspace = await workspaceSources("owner", input.principal, projectTargets, Boolean(input.project.trim()));
+ nodes.push(...workspace.nodes); links.push(...workspace.links); warnings.push(...workspace.warnings); truncated ||= workspace.truncated;
+ }
+ warnings.push("Bounded projection: up to 4 projects, 12 records/project, 40 device memories, 40 assistant memories, and 160 workspace nodes. Use Workflows > Sources and session Self-improve for paginated history.");
const graph = assembleMemoryGraph(nodes, links, { includeGhosts: true, includeTags: true });
return { ...graph, truncated, warnings, root: vault.root };
}
diff --git a/lib/memory-graph/types.ts b/lib/memory-graph/types.ts
index 68088563..fa45bf4c 100644
--- a/lib/memory-graph/types.ts
+++ b/lib/memory-graph/types.ts
@@ -15,6 +15,8 @@ export interface MemoryGraphNode {
/** Host path the Code app can open. Absent for ghosts and private claims. */
path?: string;
excerpt?: string;
+ targetApp?: "workflows" | "organization";
+ origin?: string;
}
export interface MemoryGraphEdge {
@@ -41,6 +43,8 @@ export interface GraphInputNode {
group: string;
path?: string;
excerpt?: string;
+ targetApp?: "workflows" | "organization";
+ origin?: string;
/** Link source only. Never copied onto the returned node. */
text?: string;
}
diff --git a/lib/memory-graph/workspace-sources.test.ts b/lib/memory-graph/workspace-sources.test.ts
new file mode 100644
index 00000000..61a800f5
--- /dev/null
+++ b/lib/memory-graph/workspace-sources.test.ts
@@ -0,0 +1,29 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+const mocks = vi.hoisted(() => ({ chart: vi.fn(), memory: vi.fn(), discovery: vi.fn(), recipes: vi.fn() }));
+vi.mock("@/lib/agent/organization-store", () => ({ getOrganizationChart: mocks.chart }));
+vi.mock("@/lib/agent/memory-store", () => ({ queryAgentMemory: mocks.memory }));
+vi.mock("@/lib/workflow/owner-discovery", () => ({ requireDiscoveryOwner: (role: string) => { if (role !== "owner") throw new Error("owner_required"); }, discoverOwnerGraphs: mocks.discovery }));
+vi.mock("@/lib/workflow/learning", () => ({ listLearnedRecipes: mocks.recipes }));
+import { workspaceSources } from "./workspace-sources";
+const project = { id: "root/mso", name: "mso", path: "/projects/mso" };
+beforeEach(() => {
+ vi.clearAllMocks();
+ mocks.chart.mockResolvedValue({ units: [{ id: "org", name: "Office", projectFlow: { nodes: [{ id: "project", title: "MSO", status: "active", summary: "Context", projectRef: project.id }], edges: [] } }] });
+ mocks.discovery.mockResolvedValue({ graphs: [{ id: "graph", owner: "a".repeat(64), name: "Saved workflow", status: "draft", nodeCount: 2, originPrincipal: "mcp-client:one", project: project.path }], scan: { warnings: [], ownersScanned: 1, totalOwners: 1 } });
+ mocks.recipes.mockResolvedValue([{ id: "recipe", actor: "mcp-client:one", intent: "Saved recipe", successes: 1, attempts: 1, project: project.path }]);
+ mocks.memory.mockResolvedValue({ records: [{ record: { id: "private", key: "workflow:key", kind: "procedural", document: "MEMORY.md", sensitivity: "private", value: "DO NOT EXPOSE PRIVATE VALUE" } }] });
+});
+describe("owner second-brain source projection", () => {
+ it("refuses nonowners before reading workspace sources", async () => { await expect(workspaceSources("operator", "web:test", [], false)).rejects.toThrow("owner_required"); expect(mocks.chart).not.toHaveBeenCalled(); });
+ it("links exact project references and withholds private agent values", async () => {
+ const result = await workspaceSources("owner", "web:test", [project], false);
+ expect(result.links.some(link => link.targetId === `project:${project.id}`)).toBe(true);
+ expect(result.nodes.some(node => node.targetApp === "organization")).toBe(true);
+ expect(JSON.stringify(result)).not.toContain("DO NOT EXPOSE PRIVATE VALUE");
+ });
+ it("does not mix unscoped actor memories into a selected project and reports truncation", async () => {
+ mocks.discovery.mockResolvedValue({ graphs: [], scan: { warnings: [], nextOwnerOffset: 4, totalOwners: 8, ownersScanned: 4 } });
+ const result = await workspaceSources("owner", "web:test", [project], true);
+ expect(mocks.memory).not.toHaveBeenCalled(); expect(result.truncated).toBe(true);
+ });
+});
diff --git a/lib/memory-graph/workspace-sources.ts b/lib/memory-graph/workspace-sources.ts
new file mode 100644
index 00000000..764d72dc
--- /dev/null
+++ b/lib/memory-graph/workspace-sources.ts
@@ -0,0 +1,76 @@
+import { getOrganizationChart } from "@/lib/agent/organization-store";
+import { queryAgentMemory } from "@/lib/agent/memory-store";
+import { discoverOwnerGraphs, requireDiscoveryOwner } from "@/lib/workflow/owner-discovery";
+import { listLearnedRecipes } from "@/lib/workflow/learning";
+import { redactText } from "@/lib/security/redact-text";
+import type { GraphInputNode, GraphInputLink } from "./types";
+
+type ProjectTarget = { id: string; name: string; path: string };
+const NODE_CAP = 160;
+
+/** Owner-console projection only. Does not alter agent retrieval scopes or grants. */
+export async function workspaceSources(role: string, principal: string, projects: ProjectTarget[], filtered: boolean) {
+ requireDiscoveryOwner(role);
+ const nodes: GraphInputNode[] = [], links: GraphInputLink[] = [], warnings: string[] = [];
+ let truncated = false;
+ const projectFor = (ref?: string) => projects.find(project => ref && [project.id, project.name, project.path].includes(ref));
+ const allowed = (ref?: string) => !filtered || Boolean(projectFor(ref));
+ const add = (node: GraphInputNode) => { if (nodes.length >= NODE_CAP) { truncated = true; return false; } nodes.push(node); return true; };
+ const linkProject = (source: string, ref?: string) => {
+ const project = projectFor(ref); if (project) links.push({ source, targetId: `project:${project.id}`, kind: "related" });
+ };
+ try {
+ const chart = await getOrganizationChart();
+ if (chart.units.length > 12) truncated = true;
+ for (const unit of chart.units.slice(0, 12)) {
+ const candidates = (unit.projectFlow?.nodes ?? []).filter(node => allowed(node.projectRef));
+ if (filtered && !candidates.length) continue;
+ const hub = `organization:${unit.id}`;
+ add({ id: hub, title: redactText(unit.name, 80), kind: "folder", group: "Organization", targetApp: "organization" });
+ if (candidates.length > 12) truncated = true;
+ const visible = new Set<string>();
+ for (const node of candidates.slice(0, 12)) {
+ const id = `${hub}:${node.id}`;
+ if (!add({ id, title: redactText(node.title, 80), kind: "knowledge", group: "Organization", targetApp: "organization", excerpt: redactText(`${node.status}: ${node.summary}`, 160) })) break;
+ visible.add(node.id); links.push({ source: hub, targetId: id, kind: "contains" }); linkProject(id, node.projectRef);
+ }
+ for (const edge of unit.projectFlow?.edges ?? []) if (visible.has(edge.source) && visible.has(edge.target)) links.push({ source: `${hub}:${edge.source}`, targetId: `${hub}:${edge.target}`, kind: "related" });
+ }
+ } catch { warnings.push("Organization metadata could not be loaded"); }
+ try {
+ const page = await discoverOwnerGraphs(role, principal);
+ warnings.push(...page.scan.warnings);
+ if (page.scan.nextOwnerOffset !== undefined) truncated = true;
+ const candidates = page.graphs.filter(graph => allowed(graph.project));
+ if (candidates.length > 40) truncated = true;
+ add({ id: "workspace:workflows", title: "Saved workflows", kind: "folder", group: "Workflows", targetApp: "workflows" });
+ for (const graph of candidates.slice(0, 40)) {
+ const id = `workflow:${graph.owner}:${graph.id}`;
+ if (!add({ id, title: redactText(graph.name, 80), kind: "memory", group: "Workflows", targetApp: "workflows", origin: graph.originPrincipal, excerpt: `${graph.status} · ${graph.nodeCount} nodes · source remains read-only` })) break;
+ links.push({ source: "workspace:workflows", targetId: id, kind: "contains" }); linkProject(id, graph.project);
+ }
+ } catch { warnings.push("Workflow source metadata could not be loaded"); }
+ try {
+ const recipes = (await listLearnedRecipes({ ownerView: true })).filter(recipe => allowed(recipe.project));
+ if (recipes.length > 24) truncated = true;
+ add({ id: "workspace:recipes", title: "Learned recipes", kind: "folder", group: "Recipes", targetApp: "workflows" });
+ for (const recipe of recipes.slice(0, 24)) {
+ const id = `recipe:${recipe.id}`;
+ if (!add({ id, title: redactText(recipe.intent, 80), kind: "memory", group: "Recipes", targetApp: "workflows", origin: recipe.actor, excerpt: `${recipe.successes}/${recipe.attempts} successful attempts; availability is not whole-session review proof` })) break;
+ links.push({ source: "workspace:recipes", targetId: id, kind: "contains" }); linkProject(id, recipe.project);
+ }
+ // Show provenance of non-browser typed memory without exporting private values.
+ const actors = [...new Set(recipes.map(recipe => recipe.actor))].filter(actor => actor !== principal);
+ if (actors.length > 2) truncated = true;
+ // Typed agent memory is not project-indexed. Do not mix it into an explicit project filter.
+ for (const actor of (filtered ? [] : actors.slice(0, 2))) {
+ const result = await queryAgentMemory(actor, { limit: 13 });
+ if (result.records.length > 12) truncated = true;
+ for (const { record } of result.records.slice(0, 12)) {
+ add({ id: `owner-agent:${record.id}`, title: redactText(record.key, 80), kind: "agent", group: "Owner agent sources", origin: actor,
+ excerpt: `${record.kind} · ${record.document} · ${record.sensitivity === "normal" ? redactText(record.value, 120) : "Private memory; value not included in this projection"}` });
+ }
+ }
+ } catch { warnings.push("Recipe or owner-agent metadata could not be loaded"); }
+ return { nodes, links, warnings, truncated };
+}
diff --git a/lib/workflow/learning-graph-receipt.test.ts b/lib/workflow/learning-graph-receipt.test.ts
new file mode 100644
index 00000000..d662b7dc
--- /dev/null
+++ b/lib/workflow/learning-graph-receipt.test.ts
@@ -0,0 +1,19 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { promises as fs } from "node:fs";
+import os from "node:os";
+import path from "node:path";
+const mocks = vi.hoisted(() => ({ ensure: vi.fn() }));
+vi.mock("./graph-store", () => ({ ensureLearnedWorkflowGraph: mocks.ensure, workflowGraphOwner: () => "a".repeat(64) }));
+import { learnedGraphReceipt, readLearningGraphReceipt } from "./learning-graph-receipt";
+import type { LearnedRecipe } from "./types";
+const roots: string[] = [];
+afterEach(async () => { vi.unstubAllEnvs(); for (const root of roots.splice(0)) await fs.rm(root, { recursive: true, force: true }); });
+describe("observable learning persistence", () => {
+ it("persists graph capacity failures as warnings without discarding recipes", async () => {
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-learning-receipt-")); roots.push(root); vi.stubEnv("OS_AGENT_SESSIONS_DIR", root);
+ mocks.ensure.mockRejectedValue(new Error("workflow_graph_capacity_reached: 200 graphs; recipe retained, no graphs deleted"));
+ const recipe = { id: "test-recipe", actor: "mcp-client:test" } as LearnedRecipe;
+ const receipt = await learnedGraphReceipt(recipe); expect(receipt.state).toBe("warning");
+ expect((await readLearningGraphReceipt(recipe))?.warning).toContain("recipe retained");
+ });
+});
diff --git a/lib/workflow/learning-graph-receipt.ts b/lib/workflow/learning-graph-receipt.ts
index 67fdc54b..fb73fa58 100644
--- a/lib/workflow/learning-graph-receipt.ts
+++ b/lib/workflow/learning-graph-receipt.ts
@@ -1,11 +1,31 @@
-import { ensureLearnedWorkflowGraph } from "./graph-store";
+import path from "node:path";
+import { ensureLearnedWorkflowGraph, workflowGraphOwner } from "./graph-store";
+import { agentSessionsDir } from "@/lib/agent/session-paths";
+import { readWorkflowJson, writeWorkflowFile } from "./private-file";
import type { FinishWorkflowResult, LearnedRecipe } from "./types";
-export async function learnedGraphReceipt(recipe: LearnedRecipe): Promise<NonNullable<FinishWorkflowResult["graphReceipt"]>> {
+
+type Receipt = NonNullable<FinishWorkflowResult["graphReceipt"]>;
+function receiptFile(recipe: Pick<LearnedRecipe, "id" | "actor">) {
+ if (!/^[a-zA-Z0-9_-]{1,120}$/.test(recipe.id)) throw new Error("invalid recipe id");
+ return path.join(agentSessionsDir(), ".learning-receipts", workflowGraphOwner(recipe.actor), `${recipe.id}.json`);
+}
+export async function readLearningGraphReceipt(recipe: Pick<LearnedRecipe, "id" | "actor">): Promise<Receipt | null> {
+ try {
+ const row = await readWorkflowJson(receiptFile(recipe), 8192, "learning graph receipt") as Receipt;
+ if (!["available", "skipped", "warning"].includes(row.state)) throw new Error("invalid learning graph receipt");
+ return row;
+ } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return null; throw error; }
+}
+export async function learnedGraphReceipt(recipe: LearnedRecipe): Promise<Receipt> {
+ let receipt: Receipt;
try {
const graph = await ensureLearnedWorkflowGraph(recipe);
- return graph ? { state: "available", graphId: graph.id } : { state: "skipped" };
+ receipt = graph ? { state: "available", graphId: graph.id } : { state: "skipped" };
} catch (error) {
- return { state: "warning", warning: error instanceof Error && error.message.startsWith("workflow_graph_capacity_reached")
+ receipt = { state: "warning", warning: error instanceof Error && error.message.startsWith("workflow_graph_capacity_reached")
? error.message : "Learned graph persistence failed; recipe retained. Inspect Workflows before retrying." };
}
+ try { await writeWorkflowFile(receiptFile(recipe), JSON.stringify({ ...receipt, updatedAt: new Date().toISOString() })); }
+ catch { receipt = { ...receipt, state: "warning", warning: `${receipt.warning ?? "Graph processed."} Receipt persistence failed; this result is not durably recorded.` }; }
+ return receipt;
}
diff --git a/lib/workflow/learning-page.test.ts b/lib/workflow/learning-page.test.ts
new file mode 100644
index 00000000..3813c0bc
--- /dev/null
+++ b/lib/workflow/learning-page.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it, vi } from "vitest";
+const mocks = vi.hoisted(() => ({ active: vi.fn(), archived: vi.fn() }));
+vi.mock("./learning", () => ({ listLearnedRecipes: mocks.active, listArchivedRecipes: mocks.archived }));
+vi.mock("./learning-graph-receipt", () => ({ readLearningGraphReceipt: async () => null }));
+vi.mock("./maturity", () => ({ recipeMaturity: () => ({ maturity: "observed", successRate: 1 }) }));
+import { learningPage } from "./learning-page";
+const recipe = (id: string, label: string) => ({ id, actor: "mcp-client:test", intent: id, updatedAt: `2026-09-25T00:00:00Z`, bestSteps: [], lastSteps: [{ state: "completed", provenance: { sessionLabel: label, actionRef: "S1.A1", eventRef: "E1" } }], attempts: 1, successes: 1, failures: 0 });
+describe("learning pagination and provenance", () => {
+ it("finds archived and latest-run links before paginating", async () => {
+ mocks.active.mockResolvedValue(Array.from({ length: 120 }, (_, i) => recipe(`active-${i}`, "unrelated")));
+ mocks.archived.mockResolvedValue([recipe("old-linked", "target")]);
+ const page = await learningPage({ includeArchived: true, sessionLabel: "target" });
+ expect(page.total).toBe(1); expect(page.recipes[0]).toMatchObject({ id: "old-linked", archived: true });
+ });
+ it("exposes nextOffset and never converts a read failure into empty data", async () => {
+ mocks.active.mockResolvedValue(Array.from({ length: 45 }, (_, i) => recipe(`active-${i}`, "target")));
+ mocks.archived.mockResolvedValue([]);
+ const page = await learningPage({ limit: 20 }); expect(page.recipes).toHaveLength(20); expect(page.nextOffset).toBe(20);
+ mocks.active.mockRejectedValue(new Error("store unavailable")); await expect(learningPage({})).rejects.toThrow("store unavailable");
+ });
+});
diff --git a/lib/workflow/learning-page.ts b/lib/workflow/learning-page.ts
new file mode 100644
index 00000000..144ffbf8
--- /dev/null
+++ b/lib/workflow/learning-page.ts
@@ -0,0 +1,45 @@
+import { listLearnedRecipes, listArchivedRecipes } from "./learning";
+import { recipeMaturity } from "./maturity";
+import { resolveProjectHint } from "@/lib/host/projects-api";
+import { readAutomationScript } from "@/lib/orchestration/repo-memory-artifacts";
+import type { LearnedRecipe } from "./types";
+import { readLearningGraphReceipt } from "./learning-graph-receipt";
+
+function sources(recipe: LearnedRecipe) {
+ return [...new Map([...recipe.bestSteps, ...recipe.lastSteps].map(step => step.provenance).filter(Boolean)
+ .map(source => [`${source!.sessionLabel}:${source!.actionRef}`, source!])).values()];
+}
+export async function learningPage(input: { offset?: number; limit?: number; includeArchived?: boolean; sessionLabel?: string }) {
+ const active = await listLearnedRecipes({ ownerView: true });
+ const archived = input.includeArchived ? await listArchivedRecipes({ ownerView: true }) : [];
+ const byId = new Map(archived.map(recipe => [recipe.id, recipe]));
+ active.forEach(recipe => byId.set(recipe.id, recipe));
+ const recipes = [...byId.values()].filter(recipe => !input.sessionLabel || sources(recipe).some(source => source.sessionLabel === input.sessionLabel))
+ .sort((a, b) => b.updatedAt.localeCompare(a.updatedAt) || a.id.localeCompare(b.id));
+ const offset = Number.isFinite(input.offset) ? Math.max(0, Math.trunc(input.offset!)) : 0;
+ const limit = Number.isFinite(input.limit) ? Math.max(1, Math.min(100, Math.trunc(input.limit!))) : 40;
+ const warnings: string[] = [];
+ const rows = await Promise.all(recipes.slice(offset, offset + limit).map(async recipe => {
+ const maturity = recipeMaturity(recipe);
+ let scriptStatus: "candidate" | "tested" | undefined;
+ if (recipe.project) {
+ try {
+ const project = await resolveProjectHint(recipe.project);
+ if (project && project.matchedBy !== "fuzzy") scriptStatus = (await readAutomationScript(project.path, `script_${recipe.id}`))?.status;
+ } catch { warnings.push(`Script status unavailable for recipe ${recipe.id}`); }
+ }
+ const graphReceipt = await readLearningGraphReceipt(recipe).catch(() => ({ state: "warning" as const, warning: "Graph receipt unavailable" }));
+ return { id: recipe.id, intent: recipe.intent, project: recipe.project, maturity: maturity.maturity,
+ stage: scriptStatus === "tested" ? "tested" : maturity.maturity, scriptStatus,
+ attempts: recipe.attempts, successes: recipe.successes, failures: recipe.failures,
+ successRate: Math.round(maturity.successRate * 1000) / 10, fastestDurationMs: recipe.fastestDurationMs,
+ updatedAt: recipe.updatedAt, archived: !active.some(row => row.id === recipe.id), graphReceipt,
+ sourceSessions: sources(recipe).map(source => ({ label: source.sessionLabel, actionRef: source.actionRef, eventRef: source.eventRef, artifactRefs: source.artifactRefs ?? [] })),
+ forge: { eligible: recipe.qualityVersion === 1 && recipe.successes >= 2 && recipe.attempts > 0 && recipe.successes / recipe.attempts >= 0.9 && recipe.bestSteps.length > 0 && recipe.bestSteps.every(step => step.state === "completed"), promotion: "explicit" as const },
+ };
+ }));
+ return { recipes: rows, total: recipes.length, offset, limit,
+ ...(offset + rows.length < recipes.length ? { nextOffset: offset + rows.length } : {}),
+ includeArchived: Boolean(input.includeArchived), warnings,
+ scope: "Recorded best and latest run provenance; absence is not proof that a session was never learned." };
+}
diff --git a/lib/workflow/learning.ts b/lib/workflow/learning.ts
index 83567d55..5e7d0daf 100644
--- a/lib/workflow/learning.ts
+++ b/lib/workflow/learning.ts
@@ -125,7 +125,7 @@ export async function finishWorkflow(input: {
await persistWorkflowStore(store);
// Successful sanitized session routes automatically become private graph drafts.
// This is best-effort learning: graph persistence must never turn workflow_finish into a failure.
- const graphReceipt = input.success ? await learnedGraphReceipt(recipe) : undefined;
+ const graphReceipt = input.success && process.env.NODE_ENV !== "test" ? await learnedGraphReceipt(recipe) : undefined;
if (input.success && process.env.NODE_ENV !== "test") {
const memoryKey = `workflow:${createHash("sha256").update(`${recipe.normalizedIntent}|${recipe.project ?? ""}`).digest("hex").slice(0, 20)}`;
const route = recipe.bestSteps.map((step) => step.tool).join(" → ").slice(0, 1200);
diff --git a/lib/workflow/owner-discovery.test.ts b/lib/workflow/owner-discovery.test.ts
new file mode 100644
index 00000000..c6f94f73
--- /dev/null
+++ b/lib/workflow/owner-discovery.test.ts
@@ -0,0 +1,23 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { promises as fs } from "node:fs";
+import path from "node:path";
+import os from "node:os";
+import { createWorkflowGraph, getWorkflowGraph, workflowGraphOwner } from "./graph-store";
+import { discoverOwnerGraphs, cloneOwnerGraph } from "./owner-discovery";
+const roots: string[] = [];
+afterEach(async () => { vi.unstubAllEnvs(); for (const root of roots.splice(0)) await fs.rm(root, { recursive: true, force: true }); });
+const definition = { name: "Source graph", description: "Owner test", status: "draft", inputs: {}, metadata: {}, nodes: [{ id: "start", name: "Start", type: "manual", position: { x: 0, y: 0 }, config: {} }, { id: "read", name: "Read", type: "tool", position: { x: 200, y: 0 }, config: { tool: "fs_read", arguments: { path: "README.md" } } }], edges: [{ id: "e1", source: "start", target: "read" }] };
+describe("owner-only workflow discovery", () => {
+ it("does not widen client reads and copies foreign graphs as disabled drafts", async () => {
+ const root = await fs.mkdtemp(path.join(os.tmpdir(), "mso-owner-discovery-")); roots.push(root); vi.stubEnv("OS_AGENT_SESSIONS_DIR", root);
+ const sourcePrincipal = "mcp-client:source", browser = "web:owner", graph = await createWorkflowGraph(sourcePrincipal, definition);
+ await expect(discoverOwnerGraphs("operator", browser)).rejects.toThrow("owner_required");
+ expect(await getWorkflowGraph(browser, graph.id)).toBeNull();
+ const discovered = await discoverOwnerGraphs("owner", browser);
+ expect(discovered.graphs[0]).toMatchObject({ id: graph.id, readOnly: true, originPrincipal: sourcePrincipal });
+ const copy = await cloneOwnerGraph("owner", browser, workflowGraphOwner(sourcePrincipal), graph.id, graph.revision);
+ expect(copy.status).toBe("draft"); expect(copy.nodes.find(node => node.id === "read")?.disabled).toBe(true);
+ expect(copy.metadata.sourceDigests).toHaveLength(1); expect((await getWorkflowGraph(sourcePrincipal, graph.id))?.revision).toBe(graph.revision);
+ await expect(cloneOwnerGraph("owner", browser, workflowGraphOwner(sourcePrincipal), graph.id, "stale")).rejects.toThrow("revision changed");
+ });
+});
diff --git a/lib/workflow/owner-discovery.ts b/lib/workflow/owner-discovery.ts
index adfdb8da..f2c3d3b1 100644
--- a/lib/workflow/owner-discovery.ts
+++ b/lib/workflow/owner-discovery.ts
@@ -1,3 +1,4 @@
+import { createHash } from "node:crypto";
// Browser-owner-only metadata discovery. Never used by client/MCP graph dispatch.
import path from "node:path";
import { agentSessionsDir } from "@/lib/agent/session-paths";
@@ -19,19 +20,21 @@ export async function discoverOwnerGraphs(role: string, principal: string, offse
const store = await readWorkflowGraphStore(owner);
if (!store.principal || workflowGraphOwner(store.principal) !== owner) { warnings.push(`Unresolved origin: ${owner}`); continue; }
graphs.push(...store.graphs.map(g => ({ id: g.id, name: g.name, status: g.status, project: g.metadata.project,
- updatedAt: g.updatedAt, originPrincipal: store.principal, owner, readOnly: store.principal !== principal, nodeCount: g.nodes.length })));
+ updatedAt: g.updatedAt, revision: g.revision, originPrincipal: store.principal, owner, readOnly: store.principal !== principal, nodeCount: g.nodes.length })));
} catch { warnings.push(`Graph metadata unavailable: ${owner}`); }
}
return { graphs, scan: { ownerOffset: start, ownersScanned: selected.length, totalOwners: owners.length,
...(start + selected.length < owners.length ? { nextOwnerOffset: start + selected.length } : {}), warnings } };
}
-export async function cloneOwnerGraph(role: string, principal: string, owner: string, id: string) {
+export async function cloneOwnerGraph(role: string, principal: string, owner: string, id: string, expectedRevision: string) {
requireDiscoveryOwner(role);
const store = await readWorkflowGraphStore(owner);
if (!store.principal || workflowGraphOwner(store.principal) !== owner) throw new Error("unresolved graph origin");
const graph = store.graphs.find(g => g.id === id);
if (!graph) throw new Error("workflow graph not found");
+ if (graph.revision !== expectedRevision) throw new Error("source revision changed; refresh before copying");
// Copy into caller namespace; the source and its immutable origin are untouched.
return createWorkflowGraph(principal, { ...graph, id: undefined, name: `${graph.name} Copy`.slice(0, 160), status: "draft",
- metadata: { ...graph.metadata, provenance: "clone", fingerprint: undefined } });
+ nodes: graph.nodes.map(node => ["manual", "output"].includes(node.type) ? node : { ...node, disabled: true }),
+ metadata: { ...graph.metadata, provenance: "clone", fingerprint: undefined, sourceDigests: [createHash("sha256").update(`${owner}:${id}:${graph.revision}`).digest("hex")], tags: [...new Set([...(graph.metadata.tags ?? []), "review-required"])].slice(0, 32) } });
}
diff --git a/scripts/cli/memory-backup.sh b/scripts/cli/memory-backup.sh
new file mode 100644
index 00000000..d510d020
--- /dev/null
+++ b/scripts/cli/memory-backup.sh
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+# Owner-only memory snapshots; uses the existing authenticated transport.
+mso_memory_backup() {
+ case "${1:-preview}" in
+ -h|--help|help) printf '%s\n' 'Usage: mso memory-backup preview | create --confirm | verify <id> <manifest-sha256> --confirm' 'Local allowlisted memory snapshot, not a full VPS/database or offsite backup. Verify restores only into a new isolated directory.' ;;
+ preview) jget "/api/v1/sys/memory-backup" ;;
+ create)
+ [ "${2-}" = "--confirm" ] && [ "$#" -eq 2 ] || die "memory-backup create requires --confirm"
+ jpost "/api/v1/sys/memory-backup" '{"action":"create","confirm":true}' ;;
+ verify)
+ [ "$#" -eq 4 ] && [ "${4-}" = "--confirm" ] || die "memory-backup verify requires <id> <manifest-sha256> --confirm"
+ jpost "/api/v1/sys/memory-backup" "$(jq -cn --arg id "$2" --arg sha "$3" '{action:"verify",id:$id,manifest_sha256:$sha,confirm:true}')" ;;
+ *) die "unknown memory-backup action; run mso memory-backup --help" ;;
+ esac
+}