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.
TypeScript browser-shellcontrol-planefile-managerhomelabmobile-firstnextjsptyreactself-hostedserver-managementshadcn-uisysadmintailwindcssterminaltypescriptvpsweb-desktopweb-terminal
feat(workflows): close session self-improvement loop
Rahman7952093713c5
diff --git a/app/api/v1/agent-sessions/route.ts b/app/api/v1/agent-sessions/route.tsindex 069f3903..090d88a2 100644--- a/app/api/v1/agent-sessions/route.ts+++ b/app/api/v1/agent-sessions/route.ts@@ -11,6 +11,8 @@ import {updateAgentSessionLocation,} from "@/lib/agent/session-store";import { pruneAgentSessionArchives } from "@/lib/agent/session-archive";+import { sessionWorkflowDraftDefinition } from "@/lib/agent/session-workflow-draft";+import { createWorkflowGraph } from "@/lib/workflow/graph-store";import {ownerSessionSummaries,resolveAgentSessionOwnerRef,@@ -93,6 +95,7 @@ export async function POST(req: NextRequest) {history?: unknown[];cwd?: string;parentSessionId?: string;+ step_ref?: string;};try {body = await req.json();@@ -160,6 +163,16 @@ export async function POST(req: NextRequest) {session: await renameAgentSession(principal, body.id, body.title),});}+ if (body.action === "save-workflow-draft") {+ const ref = String(body.ref || body.id || "").trim();+ if (!ref) return NextResponse.json({ error: "session_reference_required" }, { status: 400 });+ const source = await resolveAgentSessionOwnerRef(ref);+ const context = await getSessionContext();+ if (!context?.session.device_id || context.role !== "owner") return NextResponse.json({ error: "owner_role_required" }, { status: 403 });+ const definition = sessionWorkflowDraftDefinition(source, body.step_ref);+ const graph = await createWorkflowGraph(`web:${context.session.device_id}`, definition, "create");+ return NextResponse.json({ graph }, { headers: { "Cache-Control": "private, no-store" } });+ }if (body.action === "prune-archives")return NextResponse.json(await pruneAgentSessionArchives());return NextResponse.json({ error: "unknown_action" }, { status: 400 });diff --git a/app/api/v1/workflows/route.ts b/app/api/v1/workflows/route.tsindex 675ad24e..32e2f3ac 100644--- a/app/api/v1/workflows/route.ts+++ b/app/api/v1/workflows/route.ts@@ -19,7 +19,8 @@ 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 } from "@/lib/orchestration/repo-memory-artifacts";+import { listAutomationScripts, readAutomationScript } from "@/lib/orchestration/repo-memory-artifacts";+import { listLearnedRecipes, recipeMaturity } from "@/lib/workflow";export const runtime="nodejs";export const dynamic="force-dynamic";const headers={"Cache-Control":"no-store, private"};const fail=(error:unknown,status=400)=>NextResponse.json({error:error instanceof Error?error.message.slice(0,500):String(error).slice(0,500)||"workflow request failed"},{status,headers});async function auth(minimum:"viewer"|"operator"|"owner"="viewer"){const context=await getSessionContext();if(!context?.session.device_id||!roleAtLeast(context.role,minimum))return null;return{context,principal:`web:${context.session.device_id}`};}@@ -27,6 +28,7 @@ function definition(graph:NonNullable<Awaited<ReturnType<typeof getWorkflowGraphexport async function GET(req:NextRequest){const q=req.nextUrl.searchParams,minimum=q.has("variables")?"operator":"viewer",session=await auth(minimum);if(!session)return fail("unauthorized",401);try{if(q.get("directory")==="1"){const query=(q.get("q")??"").toLowerCase().trim(),tools=msoCapabilityRuntime.list(maxScope()).filter((tool)=>!query||`${tool.name} ${tool.description} ${tool.scope}`.toLowerCase().includes(query)).slice(0,150),graphs=await listWorkflowGraphs(session.principal),sessions=await listAgentSessions(session.principal,50);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)}))},{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("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});diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.mdindex eb929236..46ba2ca6 100644--- a/docs/CHANGELOG.md+++ b/docs/CHANGELOG.md@@ -8,6 +8,7 @@ this is the *what*, and it is what Settings → About shows as “What's new”.**Added**+- `workflows` close session self-improvement loop- `mcp` add compact ChatGPT list surface- `plugins` add structured OpenAI skill contracts- `workflows` stabilize semantic action referencesdiff --git a/frontend/slices/workflows/app.tsx b/frontend/slices/workflows/app.tsxindex 4bbda3b6..1fa0e605 100644--- a/frontend/slices/workflows/app.tsx+++ b/frontend/slices/workflows/app.tsx@@ -17,7 +17,7 @@ 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 { cloneGraph, createFromTemplate, createGraph, deleteGraph, getGraph, getSessionGraph, listGraphs, resolveNode, runGraph, runStatus, updateGraph } from "./lib/api";+import { cloneGraph, createFromTemplate, createGraph, deleteGraph, getGraph, getSessionGraph, listGraphs, listWorkflowLearning, resolveNode, runGraph, runStatus, saveSessionWorkflowDraft, updateGraph, type WorkflowLearningRecipeSummary } from "./lib/api";type Definition = Omit<WorkflowGraph, "version" | "id" | "revision" | "createdAt" | "updatedAt">;type LibraryMode = "automations" | "sessions";@@ -33,6 +33,7 @@ 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]);@@ -50,15 +51,15 @@ export default function WorkflowsApp(props: AppProps) {const id = payloadSessionId(props.payload);if (!id) return;let alive = true;- 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"); });+ 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"); });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 = 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 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 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: Definition) => wrap(async () => { const next = await createGraph(definition); setLibraryMode("automations"); await load(next.id); setSelected(next.nodes[0]?.id ?? null); setLibraryOpen(false); });@@ -75,6 +76,10 @@ export default function WorkflowsApp(props: AppProps) {const openNode = async (id: string) => { if (!graph) return; const resolved = await resolveNode(graph.id, id); openWindow("files-manager", resolved.name, undefined, { path: resolved.path }, { multi: true }); };const openSessionTerminal = () => { if (!sessionView) return; setDetailsOpen(false); openWindow("os-terminal", "Terminal", undefined, { initialCwd: sessionView.session.cwd || "~" }, { multi: true }); };const openSessionCode = (path: string) => { setDetailsOpen(false); const name = path.split("/").filter(Boolean).at(-1) || "Code"; openWindow("code-editor", name, undefined, { path }, { multi: true }); };+ const saveSessionDraft = (stepRef?: string) => sessionView && void wrap(async () => {+ const next = await saveSessionWorkflowDraft(sessionView.session.id, stepRef);+ setGraphs(await listGraphs(true)); setGraph(next); setLibraryMode("automations"); setSelected(next.nodes[0]?.id ?? null); setDirty(false); setRun(null); setDetailsOpen(false); setMessage(`Saved ${stepRef ?? "session"} as review-first workflow draft`);+ });const chooseNode = (id: string | null) => { setSelected(id); if (!id) return; setPanel("inspector"); if (overlayPane) setDetailsOpen(true); };const chooseSessionNode = (id: string | null) => { setSessionSelected(id); if (id && overlayPane) setDetailsOpen(true); };const tidy = () => graph && mutate({ ...graph, nodes: tidyWorkflowNodes(graph) });@@ -83,7 +88,7 @@ export default function WorkflowsApp(props: AppProps) {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); }}/>;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}/> : graph ? <WorkflowDetails graph={graph} graphs={graphs} node={node} run={run} panel={panel} onPanel={setPanel} onNode={updateNode} onDeleteNode={(id) => deleteNodes([id])} onGraph={mutate} onRunSelect={(selectedRun) => { setRun(selectedRun); setPanel("run"); }} onRestored={(next) => { setGraph(next); setDirty(false); void listGraphs().then(setGraphs); }}/> : null;+ 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={setPanel} onNode={updateNode} onDeleteNode={(id) => deleteNodes([id])} onGraph={mutate} onRunSelect={(selectedRun) => { setRun(selectedRun); setPanel("run"); }} onRestored={(next) => { setGraph(next); setDirty(false); void listGraphs().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 },diff --git a/frontend/slices/workflows/components/workflow-session-details.tsx b/frontend/slices/workflows/components/workflow-session-details.tsxindex a848b091..9e8da2c3 100644--- a/frontend/slices/workflows/components/workflow-session-details.tsx+++ b/frontend/slices/workflows/components/workflow-session-details.tsx@@ -1,11 +1,12 @@"use client";-import { Code2, Copy, ExternalLink, FileCode2, TerminalSquare } from "lucide-react";+import { BrainCircuit, Code2, Copy, ExternalLink, FileCode2, 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 type { WorkflowLearningRecipeSummary } from "../lib/api";function value(config: Record<string, unknown>, key: string): string | undefined {const raw = config[key]; return typeof raw === "string" && raw ? raw : undefined;@@ -42,9 +43,22 @@ function actionGroups(step: SessionFlowStep, onOpenTerminal: () => void, onOpenC});}-export function WorkflowSessionDetails({ view, node, onOpenTerminal, onOpenCode }: { view: SessionGraphView; node: WorkflowGraphNode | null; onOpenTerminal: () => void; onOpenCode: (path: string) => void }) {+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[] }) {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>@@ -53,10 +67,11 @@ export function WorkflowSessionDetails({ view, node, onOpenTerminal, onOpenCode{view.omittedEvents ? <p className="mt-2 text-[10px] text-muted-foreground">Projection uses the latest {view.shownEvents} of {view.totalEvents} events to stay fast. Raw session history remains the source of truth.</p> : null}</div><ScrollArea className="min-h-0 flex-1"><div className="space-y-3 p-3">- {!node || node.id === "session-root" ? <><div><div className="text-[10px] uppercase text-muted-foreground">Flow</div><div className="mt-1 text-sm font-semibold">{view.session.title}</div></div><div className="space-y-1.5">{view.steps.map((item) => <div key={item.ref} className="rounded-lg border bg-background/35 p-2"><div className="flex items-center gap-2"><code className="rounded bg-muted px-1.5 py-0.5 text-[10px] font-semibold">{item.ref}</code><span className="text-xs font-medium">{item.title}</span><span className="ml-auto text-[10px] text-muted-foreground">{item.actions.length} actions</span></div><p className="mt-1 text-[10px] text-muted-foreground">{item.summary}</p></div>)}</div></> : step ? <>- <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></div>+ {!node || node.id === "session-root" ? <><div><div className="text-[10px] uppercase text-muted-foreground">Flow</div><div className="mt-1 text-sm font-semibold">{view.session.title}</div><Button data-slot="save-session-workflow-draft" type="button" size="sm" variant="outline" className="mt-2 h-7 text-[10px]" disabled={savingDraft} onClick={() => onSaveDraft()}><Save className="size-3.5"/>Save session as workflow draft</Button></div><div className="space-y-1.5">{view.steps.map((item) => <div key={item.ref} className="rounded-lg border bg-background/35 p-2"><div className="flex items-center gap-2"><code className="rounded bg-muted px-1.5 py-0.5 text-[10px] font-semibold">{item.ref}</code><span className="text-xs font-medium">{item.title}</span><span className="ml-auto text-[10px] text-muted-foreground">{item.actions.length} actions</span></div><p className="mt-1 text-[10px] text-muted-foreground">{item.summary}</p></div>)}</div></> : step ? <>+ <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, onOpenTerminal, onOpenCode)}</div></div></> : <p className="text-xs text-muted-foreground">Select a semantic step to inspect its actions.</p>}+ <SelfImprove recipes={linked}/>{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/lib/api.ts b/frontend/slices/workflows/lib/api.tsindex 54dfb068..daa5bc6a 100644--- a/frontend/slices/workflows/lib/api.ts+++ b/frontend/slices/workflows/lib/api.ts@@ -32,3 +32,14 @@ export async function listWorkflowDirectory(query=""){return cachedWorkflowResouexport type WorkflowScriptSummary = { id:string; intent:string; status:"candidate"|"tested"; stepCount:number; updatedAt:string; project?:string; tools:string[] };export async function listWorkflowScripts(project:string,query=""){const q=new URLSearchParams({scripts:"1",project});if(query.trim())q.set("q",query.trim());return cachedWorkflowResource(`scripts:${project}:${query}`,15000,async()=> (await json<{project:string;scripts:WorkflowScriptSummary[]}>(`/api/v1/workflows?${q}`)).scripts);}++export type WorkflowLearningRecipeSummary = {+ id:string; intent:string; project?:string; maturity:"observed"|"candidate"|"verified"; stage:"observed"|"candidate"|"verified"|"tested";+ scriptStatus?:"candidate"|"tested"; attempts:number; successes:number; failures:number; successRate:number; fastestDurationMs?:number; updatedAt:string;+ sourceSessions:Array<{label:string;actionRef:string;eventRef:string;artifactRefs:string[]}>; forge:{eligible:boolean;promotion:"explicit"};+};+export async function listWorkflowLearning(force=false){const load=async()=> (await json<{recipes:WorkflowLearningRecipeSummary[]}>("/api/v1/workflows?learning=1")).recipes;return force?load():cachedWorkflowResource("learning",10000,load);}+export async function saveSessionWorkflowDraft(id:string,stepRef?:string){+ const next=(await json<{graph:WorkflowGraph}>("/api/v1/agent-sessions",{method:"POST",body:JSON.stringify({action:"save-workflow-draft",id,...(stepRef?{step_ref:stepRef}:{})})})).graph;+ invalidateWorkflowResources("graphs"); seedWorkflowResource(`graph:${next.id}`,next,8000); return next;+}diff --git a/frontend/slices/workflows/session-graph-ui.test.ts b/frontend/slices/workflows/session-graph-ui.test.tsindex fbade42f..8f875d17 100644--- a/frontend/slices/workflows/session-graph-ui.test.ts+++ b/frontend/slices/workflows/session-graph-ui.test.ts@@ -32,6 +32,12 @@ describe("Workflow session graph UI contract", () => {expect(sessionDetails).toContain("Action groups");expect(sessionDetails).toContain("action.artifact.revisionRef");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(workflowApp).toContain("saveSessionWorkflowDraft");+ expect(workflowApp).toContain("listWorkflowLearning");expect(workflowCanvas).toContain('showMinimap={!readOnly}');});diff --git a/lib/agent/session-action-history.test.ts b/lib/agent/session-action-history.test.tsnew file mode 100644index 00000000..bb78395f--- /dev/null+++ b/lib/agent/session-action-history.test.ts@@ -0,0 +1,36 @@+import { afterAll, describe, expect, it } from "vitest";+import { createHash } from "node:crypto";+import { mkdtemp, rm } from "node:fs/promises";+import os from "node:os";+import path from "node:path";+import { normalizeSessionEventSemantics } from "./session-semantic";+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;+const history = await import("./session-action-history");++afterAll(async () => { await rm(root, { recursive: true, force: true }); });++describe("archive-backed session action lookup", () => {+ it("keeps evicted action refs resolvable without crossing principals", async () => {+ const principal = "mcp-client:owner", now = Date.now();+ const all = normalizeSessionEventSemantics(Array.from({ length: 401 }, (_, index) => ({+ at: new Date(now + index).toISOString(), kind: "tool" as const, tool: index === 0 ? "fs_read" : "fs_write", state: "completed", detail: index === 0 ? "src/old.ts token=secret-value" : `src/${index}.ts`,+ })));+ const original = resolveSessionFlowAction(all, "E1", "/srv/project", 0)!;+ const session: AgentSession = {+ id: "20260917_010101_deadbeef", principalHash: createHash("sha256").update(principal).digest("hex"), source: "mcp", name: "milo", title: "History", titleSource: "auto",+ createdAt: all[0]!.at, updatedAt: all.at(-1)!.at, cwd: "/srv/project", memorySnapshot: { capturedAt: all[0]!.at, user: "", memory: "" }, history: [],+ events: all.slice(1), eventSeqBase: 1, estimatedTokens: 0, lifetimeEstimatedTokens: 0, compactThresholdTokens: 700000, compactionCount: 0, archiveCount: 0,+ };+ await history.archiveDroppedSessionEvents(session, [all[0]!], 0);+ for (const ref of ["E1", original.action.ref, original.action.id]) {+ const resolved = await history.resolveHistoricalSessionAction(principal, session, ref);+ expect(resolved?.action.eventRef).toBe("E1");+ expect(JSON.stringify(resolved)).not.toContain("secret-value");+ }+ 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.tsnew file mode 100644index 00000000..bc8c05e2--- /dev/null+++ b/lib/agent/session-action-history.ts@@ -0,0 +1,172 @@+import { createHash, randomUUID } 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 { resolveSessionFlowAction } from "./session-flow";+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)));+}+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 } : {}),+ };+}+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); }+ });+}++async function resolveActionIndex(session: AgentSession, actionRef: string) {+ 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 resolved = resolveSessionFlowAction([row.event], actionRef, session.cwd ?? index.cwd, row.sequence - 1);+ if (resolved) return resolved;+ }+ return null;+}++/** Caller supplies an authenticated principal and its exact loaded session, never an arbitrary archive path. */+export async function resolveHistoricalSessionAction(principal: string, session: AgentSession, actionRef: string) {+ const owner = createHash("sha256").update(principal).digest("hex");+ if (session.principalHash !== owner) throw new Error("session not found");+ if (!/^(?:S\d+\.A\d+|E\d+|action_[a-f0-9]{20})$/i.test(actionRef.trim())) return null;+ const current = resolveSessionFlowAction(session.events, actionRef, session.cwd, session.eventSeqBase);+ if (current) return current;+ const indexed = await resolveActionIndex(session, actionRef);+ if (indexed) return indexed;+ if (!/^\d{8}_\d{6}_[a-f0-9]{8}$/.test(session.id)) return null;+ const root = agentSessionArchiveRoot();+ const owned = (stat: import("node:fs").Stats) => !(stat.mode & 0o077) && (typeof process.getuid !== "function" || stat.uid === process.getuid());+ try {+ 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 directory = await fs.opendir(root), names: string[] = [];+ let scanned = 0;+ for await (const entry of directory) {+ if (++scanned > 10000) break;+ if (entry.isFile() && entry.name.startsWith(`${session.id}__`) && /^\d{8}_\d{6}_[a-f0-9]{8}__\d{8}T\d{6}Z__[a-z0-9-]+\.json\.gz$/.test(entry.name)) names.push(entry.name);+ }+ for (const name of names.sort().reverse().slice(0, 64)) {+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;+ try {+ handle = await fs.open(path.join(root, name), constants.O_RDONLY | constants.O_NOFOLLOW);+ const stat = await handle.stat();+ if (!stat.isFile() || !owned(stat) || stat.size > MAX_BYTES || stat.mtimeMs < cutoff) continue;+ const buffer = Buffer.alloc(MAX_BYTES + 1), { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);+ if (bytesRead > MAX_BYTES) continue;+ const compressed = buffer.subarray(0, bytesRead), digest = createHash("sha256").update(compressed).digest("hex");+ 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;+ const base = archived.eventSeqBase ?? 0;+ if (!Number.isSafeInteger(base) || base < 0) continue;+ const resolved = resolveSessionFlowAction(archived.events, actionRef, session.cwd, base);+ if (resolved) return resolved;+ } catch { /* malformed/expired/unsafe archives do not block other matches */ }+ finally { await handle?.close().catch(() => undefined); }+ }+ return null;+}++export async function pruneSessionActionIndexes(now = Date.now()): Promise<number> {+ const root = await secureArchiveRoot(), cutoff = now - archiveRetentionDays() * 86_400_000;+ const names = await fs.readdir(root).catch(() => [] as string[]); let removed = 0;+ for (const name of names) {+ if (!INDEX_FILE.test(name)) continue;+ const file = path.join(root, name);+ try {+ const stat = await fs.lstat(file);+ if (!stat.isFile() || stat.isSymbolicLink()) continue;+ if (stat.mtimeMs < cutoff) { await fs.unlink(file); removed += 1; }+ } catch { /* ignore raced or malformed entries */ }+ }+ return removed;+}diff --git a/lib/agent/session-archive.ts b/lib/agent/session-archive.tsindex 88f49584..33a0197c 100644--- a/lib/agent/session-archive.ts+++ b/lib/agent/session-archive.ts@@ -71,5 +71,9 @@ export async function pruneAgentSessionArchives(now = Date.now()): Promise<{ remfinally { await handle?.close().catch(() => undefined); }await fs.unlink(file).then(() => { removed++; }).catch(() => undefined);}+ try {+ const { pruneSessionActionIndexes } = await import("./session-action-history");+ removed += await pruneSessionActionIndexes(now);+ } catch { /* action-index pruning is best-effort */ }return { removed, kept };}diff --git a/lib/agent/session-flow.ts b/lib/agent/session-flow.tsindex be684002..778b1e20 100644--- a/lib/agent/session-flow.ts+++ b/lib/agent/session-flow.ts@@ -108,7 +108,7 @@ function semanticTitle(category: SessionFlowCategory, groups: SessionFlowActionGreturn informative.length ? `${TITLES[category]} · ${informative.join(" + ")}` : TITLES[category];}-function buildActions(events: AgentSessionEvent[], cwd?: string, rawBase = 0): SessionFlowAction[] {+export function sessionFlowActions(events: AgentSessionEvent[], cwd?: string, rawBase = 0): SessionFlowAction[] {const normalized = normalizeSessionEventSemantics(events);const base = eventSequenceBase(rawBase);return normalized.map((row, index) => {@@ -165,7 +165,7 @@ function stepsFromActions(actions: SessionFlowAction[]): SessionFlowStep[] {export function semanticSessionFlow(events: AgentSessionEvent[], requestedLimit = SESSION_GRAPH_EVENT_LIMIT, cwd?: string, rawBase = 0) {const limit = Math.max(1, Math.min(SESSION_GRAPH_EVENT_LIMIT, Math.trunc(requestedLimit) || SESSION_GRAPH_EVENT_LIMIT));const base = eventSequenceBase(rawBase);- const allActions = buildActions(events, cwd, base);+ const allActions = sessionFlowActions(events, cwd, base);const windowActions = allActions.slice(-limit);const windowSteps = stepsFromActions(windowActions);const steps = windowSteps.slice(-STEP_LIMIT);@@ -175,7 +175,7 @@ export function semanticSessionFlow(events: AgentSessionEvent[], requestedLimit}export function resolveSessionFlowAction(events: AgentSessionEvent[], actionRef: string, cwd?: string, rawBase = 0): SessionFlowActionResolution | null {- const actions = buildActions(events, cwd, rawBase);+ const actions = sessionFlowActions(events, cwd, rawBase);const wanted = actionRef.trim();if (!wanted) return null;const upper = wanted.toUpperCase();diff --git a/lib/agent/session-mutations.ts b/lib/agent/session-mutations.tsindex 955f5b58..dd36b390 100644--- a/lib/agent/session-mutations.ts+++ b/lib/agent/session-mutations.ts@@ -5,6 +5,7 @@ import { requireAgentSessionName } from "./session-name";import { autoSessionTitle, estimateTokens, MAX_EVENTS, MAX_HISTORY, safeTitle } from "./session-policy";import { retainSessionEvents } from "./session-sequence";import { appendSemanticSessionEvent } from "./session-semantic";+import { archiveDroppedSessionEvents } from "./session-action-history";import { compactIfNeeded, requireOwned } from "./session-record";import type { AgentSession, AgentSessionEvent, AgentSessionTitleSource } from "./session-types";@@ -123,7 +124,10 @@ export async function appendAgentSessionEvent(}: {}),};- const retained = retainSessionEvents(appendSemanticSessionEvent(record.events, row), record.eventSeqBase, MAX_EVENTS);+ const expanded = appendSemanticSessionEvent(record.events, row);+ const retained = retainSessionEvents(expanded, record.eventSeqBase, MAX_EVENTS);+ const droppedCount = expanded.length - retained.events.length;+ if (droppedCount > 0) await archiveDroppedSessionEvents(record, expanded.slice(0, droppedCount), record.eventSeqBase ?? 0);record.events = retained.events;record.eventSeqBase = retained.eventSeqBase;record.updatedAt = at;diff --git a/lib/agent/session-workflow-draft.test.ts b/lib/agent/session-workflow-draft.test.tsnew file mode 100644index 00000000..9ab3db14--- /dev/null+++ b/lib/agent/session-workflow-draft.test.ts@@ -0,0 +1,39 @@+import { describe, expect, it } from "vitest";+import type { AgentSession } from "./session-types";+import { normalizeSessionEventSemantics } from "./session-semantic";+import { sessionWorkflowDraftDefinition } from "./session-workflow-draft";++function session(): AgentSession {+ const now = new Date().toISOString();+ return {+ id: "20260917_010101_deadbeef", principalHash: "a".repeat(64), source: "mcp", name: "milo", title: "Deploy app", titleSource: "auto",+ createdAt: now, updatedAt: now, cwd: "/srv/app", memorySnapshot: { capturedAt: now, user: "", memory: "" }, history: [],+ events: normalizeSessionEventSemantics([+ { at: now, kind: "tool", tool: "fs_read", state: "completed", detail: "src/app.ts token=secret-value" },+ { at: now, kind: "tool", tool: "fs_write", state: "completed", detail: "src/app.ts" },+ ]), eventSeqBase: 0, estimatedTokens: 0, lifetimeEstimatedTokens: 0, compactThresholdTokens: 700000, compactionCount: 0, archiveCount: 0,+ };+}++describe("session workflow drafts", () => {+ it("creates an inert private-review draft without raw details or internal session ids", () => {+ const source = session(), draft = sessionWorkflowDraftDefinition(source);+ expect(draft.status).toBe("draft");+ expect(draft.metadata.provenance).toBe("learned-from-session");+ const tools = draft.nodes.filter((node) => node.type === "tool");+ expect(tools).toHaveLength(2);+ expect(tools.every((node) => node.disabled === true && node.config.reviewRequired === true)).toBe(true);+ expect(JSON.stringify(draft)).not.toContain(source.id);+ expect(JSON.stringify(draft)).not.toContain("secret-value");+ expect(JSON.stringify(draft)).not.toContain("src/app.ts token");+ });++ it("can save one exact semantic step", () => {+ const source = session(), all = sessionWorkflowDraftDefinition(source);+ const ref = String(all.nodes.find((node) => node.type === "tool")!.config.sourceRef).split(".A")[0]!;+ const one = sessionWorkflowDraftDefinition(source, ref);+ expect(one.name).toContain(ref);+ expect(one.metadata.tags).toContain(ref);+ expect(one.nodes.filter((node) => node.type === "tool").every((node) => String(node.config.sourceRef).startsWith(`${ref}.A`))).toBe(true);+ });+});diff --git a/lib/agent/session-workflow-draft.ts b/lib/agent/session-workflow-draft.tsnew file mode 100644index 00000000..67caef82--- /dev/null+++ b/lib/agent/session-workflow-draft.ts@@ -0,0 +1,63 @@+import { createHash } from "node:crypto";+import type { WorkflowGraphDefinition } from "@/lib/workflow/graph-schema";+import { semanticSessionFlow } from "./session-flow";+import { agentSessionLabel } from "./session-name";+import type { AgentSession } from "./session-types";++function digest(value: string): string { return createHash("sha256").update(value).digest("hex"); }+function safeNodeId(value: string, index: number): string {+ return `learned-${value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "").slice(0, 40) || "action"}-${index + 1}`;+}++/**+ * Create an inert, review-first workflow definition from the bounded semantic+ * session projection. Learned tool nodes are disabled and contain no captured+ * arguments, command payloads, transcripts, or internal session ids.+ */+export function sessionWorkflowDraftDefinition(record: AgentSession, stepRef?: string): WorkflowGraphDefinition {+ const label = agentSessionLabel(record.name, record.title, record.cwd);+ const flow = semanticSessionFlow(record.events, 120, record.cwd, record.eventSeqBase);+ const steps = stepRef ? flow.steps.filter((step) => step.ref.toUpperCase() === stepRef.trim().toUpperCase()) : flow.steps;+ if (stepRef && !steps.length) throw new Error(`session semantic step not found: ${stepRef.toUpperCase()}`);+ if (!steps.length) throw new Error("session has no semantic actions to save");+ const actions = steps.flatMap((step) => step.actions).filter((action) => action.tool).slice(0, 120);+ if (!actions.length) throw new Error("session selection has no reusable tool actions");+ const learned = actions.map((action, index) => ({+ id: safeNodeId(action.ref, index),+ name: `${action.ref} · ${action.tool}`.slice(0, 120),+ type: "tool" as const,+ position: { x: 300 + (index % 4) * 260, y: 120 + Math.floor(index / 4) * 150 },+ disabled: true,+ config: {+ tool: action.tool!,+ arguments: {},+ sourceRef: action.ref,+ sourceEventRef: action.eventRef,+ sourceCategory: action.category,+ ...(action.artifact ? { sourceArtifactRefs: [action.artifact.ref] } : {}),+ reviewRequired: true,+ },+ }));+ const manual = { id: "manual", name: "Manual Trigger", type: "manual" as const, position: { x: 40, y: 120 }, config: {} };+ const output = { id: "output", name: "Output", type: "output" as const, position: { x: 300 + (learned.length % 4) * 260, y: 120 + Math.floor(learned.length / 4) * 150 }, config: {} };+ const nodes = [manual, ...learned, output];+ const edges = nodes.slice(1).map((node, index) => ({ id: `edge-${index + 1}`, source: nodes[index]!.id, target: node.id }));+ const selectedRefs = steps.map((step) => step.ref);+ const sourceDigest = digest(JSON.stringify({ label, refs: actions.map((action) => [action.ref, action.eventRef, action.tool]), updatedAt: record.updatedAt }));+ return {+ name: `${stepRef ? `${stepRef.toUpperCase()} · ` : ""}${record.title}`.slice(0, 160),+ description: `Review-first draft from ${stepRef ? `semantic step ${stepRef.toUpperCase()}` : "the visible semantic session flow"}. Learned actions are disabled until inputs are reviewed.`,+ status: "draft",+ inputs: {},+ nodes,+ edges,+ metadata: {+ intent: record.title,+ ...(record.cwd ? { project: record.cwd } : {}),+ provenance: "learned-from-session",+ fingerprint: digest(`session-draft\0${sourceDigest}`),+ sourceDigests: [sourceDigest],+ tags: ["session-draft", ...(stepRef ? ["semantic-step", stepRef.toUpperCase()] : ["semantic-session"]), ...selectedRefs.slice(0, 8)],+ },+ };+}diff --git a/lib/mcp/tools-agent.ts b/lib/mcp/tools-agent.tsindex bd73c55c..0e82d3d7 100644--- a/lib/mcp/tools-agent.ts+++ b/lib/mcp/tools-agent.ts@@ -4,7 +4,8 @@ import { agentSessionSummary, appendAgentSessionEvent, getAgentSession, listAgenimport { type McpTool, S, str } from "./tool-kit";import { resolveAgentSessionRef } from "@/lib/agent/session-query";import { agentSessionLabel } from "@/lib/agent/session-name";-import { resolveSessionFlowAction, semanticSessionFlow } from "@/lib/agent/session-flow";+import { semanticSessionFlow } from "@/lib/agent/session-flow";+import { resolveHistoricalSessionAction } from "@/lib/agent/session-action-history";function principal(context: { principal?: string }): string {if (!context.principal) throw new Error("agent session principal is unavailable");@@ -73,7 +74,7 @@ export const AGENT_TOOLS: McpTool[] = [const session = { label: agentSessionLabel(target.name, target.title, target.cwd), title: target.title, source: target.source, updatedAt: target.updatedAt, ...(target.cwd ? { cwd: target.cwd } : {}) };const wanted = optionalString(a, "action_ref");if (wanted) {- const resolved = resolveSessionFlowAction(target.events, wanted, target.cwd, target.eventSeqBase);+ const resolved = await resolveHistoricalSessionAction(owner, target, wanted);if (!resolved) throw new Error(`session action not found: ${wanted.toUpperCase()}`);return { session, ...resolved };}@@ -90,7 +91,7 @@ export const AGENT_TOOLS: McpTool[] = [}, ["session_ref", "action_ref"]),run: async (a, context) => {const owner = principal(context), target = await resolveAgentSessionRef(owner, str(a, "session_ref"));- const resolved = resolveSessionFlowAction(target.events, str(a, "action_ref"), target.cwd, target.eventSeqBase);+ const resolved = await resolveHistoricalSessionAction(owner, target, str(a, "action_ref"));if (!resolved) throw new Error(`session action not found: ${str(a, "action_ref").toUpperCase()}`);return {session: { label: agentSessionLabel(target.name, target.title, target.cwd), title: target.title, source: target.source, updatedAt: target.updatedAt, ...(target.cwd ? { cwd: target.cwd } : {}) },diff --git a/lib/mcp/tools-workflow-lifecycle.ts b/lib/mcp/tools-workflow-lifecycle.tsindex 87207a54..6b963034 100644--- a/lib/mcp/tools-workflow-lifecycle.ts+++ b/lib/mcp/tools-workflow-lifecycle.ts@@ -1,5 +1,7 @@import { inspectProject, resolveProjectHint } from "@/lib/host/projects-api";-import { activeWorkflowForActor, cancelWorkflow, finishWorkflow } from "@/lib/workflow";+import { activeWorkflowForActor, cancelWorkflow, finishWorkflow, workflowStepProvenance } from "@/lib/workflow";+import { getAgentSession } from "@/lib/agent/session-store";+import { agentSessionLabel } from "@/lib/agent/session-name";import { assessAutomationPromotion, buildAutomationScriptManifest, workflowCleanupGuidance } from "@/lib/orchestration/automation";import { classifyTask } from "@/lib/orchestration/classifier";import { buildEvidenceReceipt, validateEvidenceReceipt } from "@/lib/orchestration/evidence";@@ -92,8 +94,17 @@ export const WORKFLOW_LIFECYCLE_TOOLS: McpTool[] = [// hard gate. Legacy in-flight workflows can still close under their old contract.if (workflow.orchestration && !validation.valid) throw new Error(validation.errors.join("; "));+ let stepProvenance: ReturnType<typeof workflowStepProvenance> | undefined;+ if (context.principal && context.sessionId) {+ const sourceSession = await getAgentSession(context.principal, context.sessionId).catch(() => null);+ if (sourceSession) {+ stepProvenance = workflowStepProvenance(+ sourceSession, workflowId, agentSessionLabel(sourceSession.name, sourceSession.title, sourceSession.cwd), workflow.steps, receipt.ref,+ );+ }+ }const finished = await finishWorkflow({- actor, recipeActor: context.recipeActor ?? context.actor, workflowId, summary, success,+ actor, recipeActor: context.recipeActor ?? context.actor, workflowId, summary, success, stepProvenance,});const automation = assessAutomationPromotion(finished.recipe);const persistenceWarnings: string[] = [];diff --git a/lib/mcp/tools-workflow-start.ts b/lib/mcp/tools-workflow-start.tsindex 4e0ff97d..1bae6c36 100644--- a/lib/mcp/tools-workflow-start.ts+++ b/lib/mcp/tools-workflow-start.ts@@ -89,12 +89,15 @@ export const WORKFLOW_START_TOOL: McpTool =id: search.recommendedRecipe.id,attempts: search.recommendedRecipe.attempts ?? 0,successRate: search.recommendedRecipe.successRate ?? 0,+ maturity: search.recommendedRecipe.maturity ?? "candidate",steps: (search.recommendedRecipe.steps ?? []).slice(0, 12).map((step) => ({tool: step.tool, target: step.target, args: step.args,})),instruction: reusableScript- ? "Use the verified/candidate script path below instead of replanning."- : "Reuse this successful bounded route directly; replan only if current evidence or tool availability conflicts.",+ ? (reusableScript.status === "tested" ? "Prefer the tested script path below when current evidence is compatible." : "A script candidate exists; verify it before treating the route as tested.")+ : search.recommendedRecipe.maturity === "verified"+ ? "Prefer this verified bounded route when current evidence and tool availability remain compatible."+ : "This is a candidate route from repeated successes. Use it as a planning shortcut, but verify the current task independently.",} : undefined;const compactSearch = {engine: search.engine,@@ -103,7 +106,7 @@ export const WORKFLOW_START_TOOL: McpTool =hits: search.hits.slice(0, 5).map((hit) => ({kind: hit.kind, id: hit.id, name: hit.name, score: hit.score,description: hit.description.slice(0, 600), source: hit.source, trust: hit.trust, scope: hit.scope, project: hit.project,- successRate: hit.successRate, attempts: hit.attempts, missingTools: hit.missingTools, contract: hit.contract,+ successRate: hit.successRate, attempts: hit.attempts, maturity: hit.maturity, missingTools: hit.missingTools, contract: hit.contract,})),recommendedRecipe: search.recommendedRecipe ? {...search.recommendedRecipe,@@ -188,8 +191,8 @@ export const WORKFLOW_START_TOOL: McpTool =`[Risk] ${classification.risk} · ${classification.complexity} complexity · ${classification.contention} contention · ${classification.isolation}`,`[Catalog] ${intentRoute.catalogMatched ? intentRoute.routeIds.join(", ") : "semantic fallback"} · ${routedTools.length}/${tools.length} tool docs scored`,`[Knowledge] ${projectKnowledge?.exists ? `${projectKnowledge.bytes} bytes always-on` : "not configured"}`,- `[Memory] ${repoMemory.length} repo-local hit(s) · ${search.recommendedRecipe ? "recipe available" : "no verified recipe selected"} · ~${contextEstimateTokens} context tokens`,- ...(recipePlan ? [`[Recipe] ${recipePlan.attempts} attempts · ${recipePlan.successRate}% success · ${recipePlan.steps.length} reusable step(s)`] : []),+ `[Memory] ${repoMemory.length} repo-local hit(s) · ${search.recommendedRecipe ? `${search.recommendedRecipe.maturity ?? "candidate"} recipe available` : "no reusable recipe selected"} · ~${contextEstimateTokens} context tokens`,+ ...(recipePlan ? [`[Recipe] ${recipePlan.maturity} · ${recipePlan.attempts} attempts · ${recipePlan.successRate}% success · ${recipePlan.steps.length} reusable step(s)`] : []),...(graphAutomation ? [`[Workflow graph] ${graphAutomation.status} · ${graphAutomation.name} · ${graphAutomation.metadata.provenance ?? "private"}`] : [`[Workflow graph] no matching private graph; successful completion will seed a learned draft`]),...(reusableScript ? [`[Automation] ${reusableScript.status} script ${reusableScript.id} available`] : []),...(contention.conflictingWorkflowCount ? [`[Collision] ${contention.conflictingWorkflowCount} workflow(s) overlap declared paths/resources`] : []),diff --git a/lib/orchestration/automation.ts b/lib/orchestration/automation.tsindex 3fc8c261..19642bea 100644--- a/lib/orchestration/automation.ts+++ b/lib/orchestration/automation.ts@@ -1,4 +1,5 @@import type { LearnedRecipe, WorkflowStep } from "@/lib/workflow";+import { recipeMaturity } from "@/lib/workflow/maturity";import type { AutomationAssessment, AutomationScriptManifest } from "./types";export const REPLAY_SAFE_TOOLS = new Set([@@ -8,20 +9,6 @@ export const REPLAY_SAFE_TOOLS = new Set(["dokploy_projects_list", "cloudflare_zones_list",]);-function successfulTools(steps: WorkflowStep[]): string[] {- return steps.filter((step) => step.state === "completed").map((step) => step.tool);-}--function similarRoute(recipe: LearnedRecipe): boolean {- const best = successfulTools(recipe.bestSteps);- const last = successfulTools(recipe.lastSteps);- if (!best.length || !last.length) return false;- const compactLast = last.filter((tool, index) => index === 0 || tool !== last[index - 1]);- const compactBest = best.filter((tool, index) => index === 0 || tool !== best[index - 1]);- if (compactBest.length !== compactLast.length) return false;- return compactBest.every((tool, index) => tool === compactLast[index]);-}-export function isReplaySafeTool(tool: string): boolean { return REPLAY_SAFE_TOOLS.has(tool); }function replayable(step: WorkflowStep): boolean {@@ -34,14 +21,12 @@ export function assessAutomationPromotion(recipe: LearnedRecipe): AutomationAsseconst attempts = Math.max(0, recipe.attempts || 0);const successes = Math.max(0, recipe.successes || 0);const failures = Math.max(0, recipe.failures || 0);- const successRate = attempts ? successes / attempts : 0;- const stableSteps = attempts >= 2 && similarRoute(recipe);- let stage: AutomationAssessment["stage"] = "observed";+ const maturity = recipeMaturity(recipe);+ const successRate = maturity.successRate;+ const stableSteps = maturity.stableSteps;+ const stage: AutomationAssessment["stage"] = maturity.maturity;const reasons: string[] = [];- if (successes >= 2 && attempts >= 2) stage = "candidate";- if (successes >= 3 && successRate >= 0.8 && stableSteps) stage = "verified";-if (attempts < 2) reasons.push("needs repeated observations");if (successes < 2) reasons.push("needs at least two successful runs for a recipe candidate");if (!stableSteps) reasons.push("tool route is not stable across successful runs yet");diff --git a/lib/orchestration/evidence.ts b/lib/orchestration/evidence.tsindex 5a24cc37..1053fdd5 100644--- a/lib/orchestration/evidence.ts+++ b/lib/orchestration/evidence.ts@@ -1,3 +1,4 @@+import { createHash } from "node:crypto";import type { ActiveWorkflow } from "@/lib/workflow";import { redactStrings, redactText } from "./redaction";import type { EvidenceInput, EvidenceItem, EvidenceReceipt, RiskLevel } from "./types";@@ -16,8 +17,11 @@ export function buildEvidenceReceipt(input: {const orchestration = input.workflow.orchestration;const claims = redactStrings(evidence.claims);if (!claims.length) claims.push(redactText(input.summary, 1200) || (input.success ? "workflow completed" : "workflow failed"));+ const createdAt = new Date().toISOString();+ const ref = `evidence_${createHash("sha256").update(JSON.stringify([input.workflow.id, createdAt, claims])).digest("hex").slice(0, 20)}`;return {schemaVersion: 1,+ ref,workflow: input.workflow.id,...(input.workflow.project ? { repo: redactText(input.workflow.project, 240) } : {}),...(orchestration?.baseCommit ? { baseCommit: orchestration.baseCommit } : {}),@@ -33,7 +37,7 @@ export function buildEvidenceReceipt(input: {artifacts: redactStrings(evidence.artifacts),manualVerification: passed(evidence.manualVerification),knownRisks: redactStrings(evidence.knownRisks),- createdAt: new Date().toISOString(),+ createdAt,};}diff --git a/lib/orchestration/types.ts b/lib/orchestration/types.tsindex 85824798..53d888b8 100644--- a/lib/orchestration/types.ts+++ b/lib/orchestration/types.ts@@ -20,6 +20,7 @@ export type EvidenceItem = {export type EvidenceReceipt = {schemaVersion: 1;+ ref: string;workflow: string;repo?: string;baseCommit?: string;diff --git a/lib/skills/search-recipe-gate.test.ts b/lib/skills/search-recipe-gate.test.tsindex 796d620a..2f19a34d 100644--- a/lib/skills/search-recipe-gate.test.ts+++ b/lib/skills/search-recipe-gate.test.ts@@ -23,6 +23,14 @@ async function successfulRun() {await memory.finishWorkflow({ actor, recipeActor: actor, workflowId: started.workflow.id, summary: "healthy", success: true });}+async function failedRun() {+ const started = await memory.startWorkflow({ actor, scope: "read", intent });+ await memory.recordWorkflowStep(actor, started.workflow.id, {+ id: `failed-${Date.now()}-${Math.random()}`, tool: "sys_stats", state: "failed", durationMs: 4, ts: new Date().toISOString(),+ });+ await memory.finishWorkflow({ actor, recipeActor: actor, workflowId: started.workflow.id, summary: "failed", success: false });+}+async function search() {return searchSkillMemory(intent, {recipeAccess: { actor, scope: "read" },@@ -50,6 +58,14 @@ describe("reusable recipe recommendation gate", () => {await successfulRun();await successfulRun();const result = await search();- expect(result.recommendedRecipe).toMatchObject({ kind: "recipe", attempts: 2, successRate: 100 });+ expect(result.recommendedRecipe).toMatchObject({ kind: "recipe", attempts: 2, successRate: 100, maturity: "candidate" });+ });++ it("does not recommend a two-attempt 50% observed route", async () => {+ await successfulRun();+ await failedRun();+ const result = await search();+ expect(result.hits.find((hit) => hit.kind === "recipe")?.maturity).toBe("observed");+ expect(result.recommendedRecipe).toBeUndefined();});});diff --git a/lib/skills/search.ts b/lib/skills/search.tsindex 77c403fb..81ef05cb 100644--- a/lib/skills/search.ts+++ b/lib/skills/search.ts@@ -1,5 +1,6 @@import { catalogSkillsDetailed, readSkillFile, type ProjectRef, type SkillInfo, type SkillScanReport } from "./catalog";-import { listLearnedRecipes, type LearnedRecipe, type RecipeAccess } from "@/lib/workflow";+import { listLearnedRecipes, type LearnedRecipe, type RecipeAccess, type RecipeMaturity } from "@/lib/workflow";+import { recipeMaturity } from "@/lib/workflow/maturity";import { embedSkillText, hybridSemanticScore, prepareSemanticQuery, SKILL_EMBEDDING_VERSION } from "./semantic";import { compactSkillContract } from "./skill-contract";@@ -24,6 +25,7 @@ export type SkillSearchHit = {successRate?: number;fastestDurationMs?: number;attempts?: number;+ maturity?: RecipeMaturity;steps?: Array<{ tool: string; target?: string; args?: Record<string, string | number | boolean>; durationMs?: number }>;missingTools?: string[];contract?: ReturnType<typeof compactSkillContract>;@@ -71,6 +73,7 @@ export async function searchSkillMemory(query: string, options: SkillSearchOptioconst recipes = options.recipeAccess ? await listLearnedRecipes(options.recipeAccess) : [];for (const recipe of recipes) {+ const maturity = recipeMaturity(recipe);const text = [recipe.intent,recipe.project,@@ -92,6 +95,7 @@ export async function searchSkillMemory(query: string, options: SkillSearchOptiosuccessRate: recipe.attempts ? Math.round((recipe.successes / recipe.attempts) * 1000) / 10 : 0,fastestDurationMs: recipe.fastestDurationMs,attempts: recipe.attempts,+ maturity: maturity.maturity,steps: recipe.bestSteps.map((s) => ({ tool: s.tool, target: s.target, args: s.args, durationMs: s.durationMs })),...(missingTools.length ? { missingTools } : {}),});@@ -142,7 +146,7 @@ export async function searchSkillMemory(query: string, options: SkillSearchOptiohits: sorted,catalog: scan,recommendedRecipe: sorted.find((h) =>- h.kind === "recipe" && h.score >= 0.22 && (h.attempts ?? 0) >= 2 && (h.successRate ?? 0) >= 50,+ h.kind === "recipe" && h.score >= 0.22 && h.maturity !== "observed" && (h.attempts ?? 0) >= 2,),};}diff --git a/lib/workflow/index.ts b/lib/workflow/index.tsindex f37c2588..3c745127 100644--- a/lib/workflow/index.ts+++ b/lib/workflow/index.ts@@ -1,9 +1,11 @@export { startWorkflow, activeWorkflowForActor, recordWorkflowStep, cancelWorkflow } from "./lifecycle";export { summarizeProjectContention, countActiveWorkflowsForProject } from "./contention";-export { finishWorkflow, listLearnedRecipes, markRecipeUsed } from "./learning";+export { finishWorkflow, listLearnedRecipes, listArchivedRecipes, markRecipeUsed } from "./learning";export { resetWorkflowStoreCache } from "./storage";export { summarizeWorkflowQuality } from "./quality";+export { recipeMaturity } from "./maturity";+export { workflowStepProvenance } from "./session-provenance";export type {- WorkflowStepState, WorkflowStep, ActiveWorkflow, WorkflowQuality, LearnedRecipe,+ WorkflowStepState, WorkflowStep, WorkflowStepProvenance, RecipeMaturity, ActiveWorkflow, WorkflowQuality, LearnedRecipe,FinishWorkflowResult, CancelWorkflowResult, RecipeAccess, ProjectContentionSummary,} from "./types";diff --git a/lib/workflow/learning.ts b/lib/workflow/learning.tsindex 7838adb8..fae2c831 100644--- a/lib/workflow/learning.ts+++ b/lib/workflow/learning.ts@@ -6,8 +6,9 @@ import { closestRecipe, recipeText } from "./recipes";import { safeMemoryText } from "./sanitize";import { actorKey, removeActiveWorkflow, workflowFor } from "./state";import { loadWorkflowStore, persistWorkflowStore } from "./storage";-import type { FinishWorkflowResult, LearnedRecipe, RecipeAccess } from "./types";+import type { FinishWorkflowResult, LearnedRecipe, RecipeAccess, WorkflowStepProvenance } from "./types";import { ensureLearnedWorkflowGraph } from "./graph-store";+import { archiveLearnedRecipes, listArchivedLearnedRecipes } from "./recipe-archive";import { rememberAgentMemory } from "@/lib/agent/memory-store";export async function finishWorkflow(input: {@@ -16,6 +17,7 @@ export async function finishWorkflow(input: {workflowId: string;summary: string;success: boolean;+ stepProvenance?: Record<string, WorkflowStepProvenance>;}): Promise<FinishWorkflowResult> {const actor = actorKey(input.actor);const recipeOwner = actorKey(input.recipeActor ?? input.actor);@@ -31,7 +33,8 @@ export async function finishWorkflow(input: {const summary = safeMemoryText(input.summary, 1200) || (input.success ? "completed" : "failed");const vector = embedSkillText(recipeText(workflow.intent, workflow.project, summary));const timestamp = now.toISOString();- const compactSteps = compactRecipeSteps(workflow.steps);+ const learnedSteps = workflow.steps.map((step) => input.stepProvenance?.[step.id] ? { ...step, provenance: input.stepProvenance[step.id] } : step);+ const compactSteps = compactRecipeSteps(learnedSteps);const currentQuality = summarizeWorkflowQuality(workflow.steps);let recipe: LearnedRecipe;@@ -50,7 +53,7 @@ export async function finishWorkflow(input: {summary,embeddingVersion: SKILL_EMBEDDING_VERSION,embedding: vector,- lastSteps: workflow.steps,+ lastSteps: learnedSteps,bestSteps: faster ? compactSteps : (input.success ? enrichBestSteps(existing.bestSteps, compactSteps) : existing.bestSteps),attempts,successes,@@ -70,7 +73,7 @@ export async function finishWorkflow(input: {id: randomUUID(), actor: recipeOwner, scope: workflow.scope, intent: workflow.intent,normalizedIntent: normalizeSemanticText(workflow.intent), project: workflow.project, summary,embeddingVersion: SKILL_EMBEDDING_VERSION, embedding: vector,- bestSteps: input.success ? compactSteps : [], lastSteps: workflow.steps,+ bestSteps: input.success ? compactSteps : [], lastSteps: learnedSteps,attempts: 1, successes: input.success ? 1 : 0, failures: input.success ? 0 : 1,averageDurationMs: durationMs, fastestDurationMs: input.success ? durationMs : undefined, lastDurationMs: durationMs,averageWallDurationMs: wallMs, lastWallDurationMs: wallMs,@@ -83,14 +86,15 @@ export async function finishWorkflow(input: {removeActiveWorkflow(store, actor, input.workflowId);const recipes = Object.values(store.recipes);if (recipes.length > 200) {- recipes+ const evicted = recipes.sort((a, b) => {const qa = a.successes * 10 - a.failures + new Date(a.lastUsedAt ?? a.updatedAt).getTime() / 1e13;const qb = b.successes * 10 - b.failures + new Date(b.lastUsedAt ?? b.updatedAt).getTime() / 1e13;return qa - qb;})- .slice(0, recipes.length - 200)- .forEach((row) => delete store.recipes[row.id]);+ .slice(0, recipes.length - 200);+ // Archive before eviction. If archival cannot be proven, keep the recipe active rather than silently losing learned history.+ try { await archiveLearnedRecipes(evicted); evicted.forEach((row) => delete store.recipes[row.id]); } catch { /* retain overflow until a later successful archival pass */ }}await persistWorkflowStore(store);// Successful sanitized session routes automatically become private graph drafts.@@ -122,6 +126,10 @@ export async function listLearnedRecipes(access: RecipeAccess): Promise<LearnedRreturn visible.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());}+export async function listArchivedRecipes(access: RecipeAccess): Promise<LearnedRecipe[]> {+ return listArchivedLearnedRecipes(access);+}+export async function markRecipeUsed(id: string, access: RecipeAccess): Promise<void> {const store = await loadWorkflowStore();const recipe = store.recipes[id];diff --git a/lib/workflow/maturity.test.ts b/lib/workflow/maturity.test.tsnew file mode 100644index 00000000..82e17e97--- /dev/null+++ b/lib/workflow/maturity.test.ts@@ -0,0 +1,29 @@+import { describe, expect, it } from "vitest";+import type { LearnedRecipe, WorkflowStep } from "./types";+import { recipeMaturity } from "./maturity";++const step = (tool: string): WorkflowStep => ({ id: tool, tool, state: "completed", ts: new Date().toISOString() });+function recipe(overrides: Partial<LearnedRecipe> = {}): LearnedRecipe {+ return {+ id: "r", actor: "a", scope: "read", intent: "health", normalizedIntent: "health", summary: "ok", embeddingVersion: "x", embedding: [],+ bestSteps: [step("sys_stats")], lastSteps: [step("sys_stats")], attempts: 1, successes: 1, failures: 0,+ averageDurationMs: 1, fastestDurationMs: 1, lastDurationMs: 1, averageWallDurationMs: 1, lastWallDurationMs: 1,+ quality: { stepAttempts: 1, completedSteps: 1, failedSteps: 0, deniedSteps: 0, rateLimitedSteps: 0, invalidArgSteps: 0, retries: 0, rollbackSignals: 0, timedSteps: 0, totalStepDurationMs: 0, averageStepDurationMs: 0 },+ lastQuality: { stepAttempts: 1, completedSteps: 1, failedSteps: 0, deniedSteps: 0, rateLimitedSteps: 0, invalidArgSteps: 0, retries: 0, rollbackSignals: 0, timedSteps: 0, totalStepDurationMs: 0, averageStepDurationMs: 0 },+ qualityVersion: 1, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString(), ...overrides,+ };+}++describe("recipe maturity", () => {+ it("keeps one-off and 50% routes observed", () => {+ expect(recipeMaturity(recipe()).maturity).toBe("observed");+ expect(recipeMaturity(recipe({ attempts: 2, successes: 1, failures: 1 })).maturity).toBe("observed");+ });+ it("promotes repeated successes to candidate before verification", () => {+ expect(recipeMaturity(recipe({ attempts: 2, successes: 2 })).maturity).toBe("candidate");+ });+ it("requires stable route, >=3 successes and >=80% success for verified", () => {+ expect(recipeMaturity(recipe({ attempts: 3, successes: 3 })).maturity).toBe("verified");+ expect(recipeMaturity(recipe({ attempts: 4, successes: 3, failures: 1, lastSteps: [step("apps_list")] })).maturity).toBe("candidate");+ });+});diff --git a/lib/workflow/maturity.ts b/lib/workflow/maturity.tsnew file mode 100644index 00000000..57814c0c--- /dev/null+++ b/lib/workflow/maturity.ts@@ -0,0 +1,20 @@+import type { LearnedRecipe, RecipeMaturity, WorkflowStep } from "./types";++function successfulTools(steps: WorkflowStep[]): string[] {+ return steps.filter((step) => step.state === "completed").map((step) => step.tool);+}+export function recipeRouteStable(recipe: LearnedRecipe): boolean {+ const best = successfulTools(recipe.bestSteps), last = successfulTools(recipe.lastSteps);+ if (!best.length || !last.length) return false;+ const compact = (rows: string[]) => rows.filter((tool, index) => index === 0 || tool !== rows[index - 1]);+ const a = compact(best), b = compact(last);+ return a.length === b.length && a.every((tool, index) => tool === b[index]);+}+export function recipeMaturity(recipe: LearnedRecipe): { maturity: RecipeMaturity; successRate: number; stableSteps: boolean } {+ const attempts = Math.max(0, recipe.attempts || 0), successes = Math.max(0, recipe.successes || 0);+ const successRate = attempts ? successes / attempts : 0, stableSteps = attempts >= 2 && recipeRouteStable(recipe);+ let maturity: RecipeMaturity = "observed";+ if (successes >= 2 && attempts >= 2) maturity = "candidate";+ if (successes >= 3 && successRate >= 0.8 && stableSteps) maturity = "verified";+ return { maturity, successRate: Math.round(successRate * 1000) / 1000, stableSteps };+}diff --git a/lib/workflow/quality.ts b/lib/workflow/quality.tsindex 1eea8aa4..0f85d92d 100644--- a/lib/workflow/quality.ts+++ b/lib/workflow/quality.ts@@ -46,7 +46,7 @@ export function mergeQuality(a: WorkflowQuality, b: WorkflowQuality): WorkflowQuexport function enrichBestSteps(best: WorkflowStep[], current: WorkflowStep[]): WorkflowStep[] {if (best.length !== current.length || best.some((step, i) => step.tool !== current[i]?.tool)) return best;- return best.map((step, i) => ({ ...step, args: step.args ?? current[i]?.args, target: step.target ?? current[i]?.target }));+ return best.map((step, i) => ({ ...step, args: step.args ?? current[i]?.args, target: step.target ?? current[i]?.target, provenance: step.provenance ?? current[i]?.provenance }));}const MAX_RECIPE_STEPS = 24;diff --git a/lib/workflow/recipe-archive.test.ts b/lib/workflow/recipe-archive.test.tsnew file mode 100644index 00000000..1b3e1af7--- /dev/null+++ b/lib/workflow/recipe-archive.test.ts@@ -0,0 +1,28 @@+import { afterAll, describe, expect, it } from "vitest";+import { mkdtemp, rm } from "node:fs/promises";+import os from "node:os";+import path from "node:path";+import type { LearnedRecipe } from "./types";++const dir = await mkdtemp(path.join(os.tmpdir(), "mso-recipe-archive-"));+process.env.OS_SKILL_MEMORY_STORE = path.join(dir, "skill-memory.json");+const archive = await import("./recipe-archive");+const now = new Date().toISOString();+const recipe: LearnedRecipe = {+ id: "archived-recipe", actor: "actor-a", scope: "read", intent: "old route", normalizedIntent: "old route", summary: "ok", embeddingVersion: "x", embedding: [],+ bestSteps: [{ id: "a", tool: "sys_stats", state: "completed", ts: now }], lastSteps: [], attempts: 2, successes: 2, failures: 0,+ averageDurationMs: 1, fastestDurationMs: 1, lastDurationMs: 1, averageWallDurationMs: 1, lastWallDurationMs: 1,+ quality: { stepAttempts: 1, completedSteps: 1, failedSteps: 0, deniedSteps: 0, rateLimitedSteps: 0, invalidArgSteps: 0, retries: 0, rollbackSignals: 0, timedSteps: 0, totalStepDurationMs: 0, averageStepDurationMs: 0 },+ lastQuality: { stepAttempts: 1, completedSteps: 1, failedSteps: 0, deniedSteps: 0, rateLimitedSteps: 0, invalidArgSteps: 0, retries: 0, rollbackSignals: 0, timedSteps: 0, totalStepDurationMs: 0, averageStepDurationMs: 0 },+ qualityVersion: 1, createdAt: now, updatedAt: now,+};+afterAll(async () => { await rm(dir, { recursive: true, force: true }); });++describe("learned recipe archive", () => {+ it("keeps evicted recipes explicitly retrievable with owner/scope filtering", async () => {+ await archive.archiveLearnedRecipes([recipe]);+ expect(await archive.listArchivedLearnedRecipes({ actor: "actor-a", scope: "read" })).toMatchObject([{ id: recipe.id }]);+ expect(await archive.listArchivedLearnedRecipes({ actor: "actor-b", scope: "read" })).toEqual([]);+ expect(await archive.listArchivedLearnedRecipes({ ownerView: true })).toMatchObject([{ id: recipe.id }]);+ });+});diff --git a/lib/workflow/recipe-archive.ts b/lib/workflow/recipe-archive.tsnew file mode 100644index 00000000..0a7c55af--- /dev/null+++ b/lib/workflow/recipe-archive.ts@@ -0,0 +1,62 @@+import { createHash, randomUUID } from "node:crypto";+import { constants, promises as fs } from "node:fs";+import path from "node:path";+import { normalizeRecipes } from "./sanitize";+import { workflowStorePath } from "./storage";+import type { LearnedRecipe, RecipeAccess } from "./types";+import { allows } from "@/lib/capabilities/scope";++const SEGMENT_RE = /^segment-[a-f0-9]{32}\.json$/;+const MAX_SEGMENT_BYTES = 2 * 1024 * 1024;+const MAX_ARCHIVED_RECIPES = 5000;++type Segment = { schemaVersion: 1; archivedAt: string; recipes: Record<string, LearnedRecipe> };+function archiveDir(): string { return `${workflowStorePath()}.archive-v1`; }+function body(recipes: LearnedRecipe[], archivedAt: string): string {+ return JSON.stringify({ schemaVersion: 1, archivedAt, recipes: Object.fromEntries(recipes.map((recipe) => [recipe.id, recipe])) } satisfies Segment);+}+async function secureDir(): Promise<string> {+ const dir = archiveDir(); await fs.mkdir(dir, { recursive: true, mode: 0o700 }); await fs.chmod(dir, 0o700).catch(() => undefined);+ const stat = await fs.lstat(dir); if (!stat.isDirectory() || stat.isSymbolicLink() || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) throw new Error("unsafe workflow recipe archive directory");+ return dir;+}+async function readSegment(file: string): Promise<Segment> {+ let handle: Awaited<ReturnType<typeof fs.open>> | undefined;+ try {+ handle = await fs.open(file, constants.O_RDONLY | constants.O_NOFOLLOW); const stat = await handle.stat();+ if (!stat.isFile() || stat.size <= 0 || stat.size > MAX_SEGMENT_BYTES || (stat.mode & 0o077) || (typeof process.getuid === "function" && stat.uid !== process.getuid())) throw new Error("workflow recipe archive segment has an invalid file shape");+ const parsed = JSON.parse(await handle.readFile("utf8")) as Partial<Segment>;+ if (parsed.schemaVersion !== 1 || typeof parsed.archivedAt !== "string" || !Number.isFinite(Date.parse(parsed.archivedAt)) || !parsed.recipes || typeof parsed.recipes !== "object") throw new Error("workflow recipe archive segment has an invalid schema");+ const recipes = normalizeRecipes(parsed.recipes);+ return { schemaVersion: 1, archivedAt: new Date(parsed.archivedAt).toISOString(), recipes };+ } finally { await handle?.close().catch(() => undefined); }+}++export async function archiveLearnedRecipes(recipes: LearnedRecipe[]): Promise<{ recipes: number; segment?: string }> {+ if (!recipes.length) return { recipes: 0 };+ const archivedAt = new Date().toISOString(), snapshot = body(recipes, archivedAt);+ if (Buffer.byteLength(snapshot) > MAX_SEGMENT_BYTES) throw new Error("workflow recipe archive segment exceeds 2 MiB");+ const dir = await secureDir(), digest = createHash("sha256").update(snapshot).digest("hex").slice(0, 32), name = `segment-${digest}.json`, file = path.join(dir, name);+ try { const existing = await readSegment(file); if (Object.keys(existing.recipes).length === recipes.length) return { recipes: recipes.length, segment: name }; }+ catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; }+ const tmp = `${file}.${randomUUID()}.tmp`;+ try { await fs.writeFile(tmp, snapshot, { flag: "wx", mode: 0o600 }); await fs.rename(tmp, file); await fs.chmod(file, 0o600); }+ finally { await fs.unlink(tmp).catch(() => undefined); }+ return { recipes: recipes.length, segment: name };+}++export async function listArchivedLearnedRecipes(access: RecipeAccess): Promise<LearnedRecipe[]> {+ let names: string[]; try { names = (await fs.readdir(archiveDir())).filter((name) => SEGMENT_RE.test(name)).sort().reverse().slice(0, 4096); }+ catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return []; throw error; }+ const byId = new Map<string, LearnedRecipe>();+ for (const name of names) {+ const segment = await readSegment(path.join(archiveDir(), name));+ for (const recipe of Object.values(segment.recipes)) {+ const visible = access.ownerView || (recipe.actor === access.actor && allows(access.scope, recipe.scope));+ if (!visible) continue;+ const prior = byId.get(recipe.id); if (!prior || Date.parse(recipe.updatedAt) > Date.parse(prior.updatedAt)) byId.set(recipe.id, recipe);+ if (byId.size > MAX_ARCHIVED_RECIPES) throw new Error(`workflow recipe archive exceeds explicit retrieval limit of ${MAX_ARCHIVED_RECIPES}`);+ }+ }+ return [...byId.values()].sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt));+}diff --git a/lib/workflow/sanitize.ts b/lib/workflow/sanitize.tsindex 85feac1a..f4172291 100644--- a/lib/workflow/sanitize.ts+++ b/lib/workflow/sanitize.ts@@ -10,6 +10,7 @@ import type {WorkflowStep,WorkflowStepInput,WorkflowStepState,+ WorkflowStepProvenance,} from "./types";export function safeMemoryText(value: string, max: number): string {@@ -94,6 +95,21 @@ const SAFE_TOOL_ARGS: Record<string, readonly string[]> = {exec_run: ["cwd"], browser_power: ["on"],};+function safeProvenance(value: unknown): WorkflowStepProvenance | undefined {+ if (!value || typeof value !== "object") return undefined;+ const row = value as Partial<WorkflowStepProvenance>;+ const sessionLabel = typeof row.sessionLabel === "string" ? safeMemoryText(row.sessionLabel, 180) : "";+ const actionRef = typeof row.actionRef === "string" && /^S\d+\.A\d+$/.test(row.actionRef) ? row.actionRef : "";+ const eventRef = typeof row.eventRef === "string" && /^E\d+$/.test(row.eventRef) ? row.eventRef : "";+ const observedAt = typeof row.observedAt === "string" && Number.isFinite(Date.parse(row.observedAt)) ? new Date(row.observedAt).toISOString() : "";+ if (!sessionLabel || !actionRef || !eventRef || !observedAt) return undefined;+ const artifactRefs = Array.isArray(row.artifactRefs)+ ? row.artifactRefs.filter((item): item is string => typeof item === "string" && /^artifact_[a-f0-9]{20}$/.test(item)).slice(0, 8)+ : [];+ const evidenceRef = typeof row.evidenceRef === "string" && /^evidence_[a-f0-9]{20}$/.test(row.evidenceRef) ? row.evidenceRef : undefined;+ return { sessionLabel, actionRef, eventRef, ...(artifactRefs.length ? { artifactRefs } : {}), ...(evidenceRef ? { evidenceRef } : {}), observedAt };+}+function safeArgs(tool: string, args?: Record<string, unknown>): Record<string, string | number | boolean> | undefined {if (!args) return undefined;const keys = SAFE_TOOL_ARGS[tool];@@ -123,6 +139,7 @@ export function sanitizeStoredStep(value: unknown): WorkflowStep | null {target: safeTarget(row.tool, row.target), args: safeArgs(row.tool, row.args),...(durationMs != null ? { durationMs } : {}),ts: Number.isFinite(Date.parse(row.ts)) ? new Date(row.ts).toISOString() : new Date(0).toISOString(),+ ...(safeProvenance(row.provenance) ? { provenance: safeProvenance(row.provenance) } : {}),};}diff --git a/lib/workflow/session-provenance.test.ts b/lib/workflow/session-provenance.test.tsnew file mode 100644index 00000000..462791a1--- /dev/null+++ b/lib/workflow/session-provenance.test.ts@@ -0,0 +1,27 @@+import { expect, it } from "vitest";+import { createHash } from "node:crypto";+import { normalizeSessionEventSemantics } from "@/lib/agent/session-semantic";+import type { AgentSession } from "@/lib/agent/session-types";+import type { WorkflowStep } from "./types";+import { workflowStepProvenance } from "./session-provenance";++it("links workflow steps to human Sx.Ay/E# receipts without persisting internal session id", () => {+ const now = new Date().toISOString(), principal = "mcp-client:test";+ const session: AgentSession = {+ id: "20260917_010101_deadbeef", principalHash: createHash("sha256").update(principal).digest("hex"), source: "mcp", name: "milo", title: "Build", titleSource: "auto",+ createdAt: now, updatedAt: now, cwd: "/srv/project", memorySnapshot: { capturedAt: now, user: "", memory: "" }, history: [], eventSeqBase: 0,+ events: normalizeSessionEventSemantics([+ { at: now, kind: "tool", tool: "fs_read", state: "completed", workflowId: "wf-1", detail: "src/app.ts" },+ { at: now, kind: "tool", tool: "fs_write", state: "completed", workflowId: "wf-1", detail: "src/app.ts" },+ ]), estimatedTokens: 0, lifetimeEstimatedTokens: 0, compactThresholdTokens: 700000, compactionCount: 0, archiveCount: 0,+ };+ const steps: WorkflowStep[] = [+ { id: "one", tool: "fs_read", state: "completed", ts: now },+ { id: "two", tool: "fs_write", state: "completed", ts: now },+ ];+ const out = workflowStepProvenance(session, "wf-1", "milo-build", steps);+ expect(out.one).toMatchObject({ sessionLabel: "milo-build", eventRef: "E1" });+ expect(out.one?.actionRef).toMatch(/^S\d+\.A\d+$/);+ expect(out.one?.artifactRefs?.[0]).toMatch(/^artifact_[a-f0-9]{20}$/);+ expect(JSON.stringify(out)).not.toContain(session.id);+});diff --git a/lib/workflow/session-provenance.ts b/lib/workflow/session-provenance.tsnew file mode 100644index 00000000..6019bbee--- /dev/null+++ b/lib/workflow/session-provenance.ts@@ -0,0 +1,40 @@+import type { AgentSession } from "@/lib/agent/session-types";+import { sessionFlowActions } from "@/lib/agent/session-flow";+import type { WorkflowStep, WorkflowStepProvenance } from "./types";++/**+ * Correlate already-sanitized workflow steps with same-session tool receipts.+ * Matching is ordered and conservative: a provenance link is emitted only when+ * workflow id + tool (+ compatible state when present) agree.+ */+export function workflowStepProvenance(+ session: AgentSession,+ workflowId: string,+ sessionLabel: string,+ steps: WorkflowStep[],+ evidenceRef?: string,+): Record<string, WorkflowStepProvenance> {+ const actions = sessionFlowActions(session.events, session.cwd, session.eventSeqBase);+ const candidates = session.events.map((event, index) => ({ event, action: actions[index] }))+ .filter((row) => row.event.workflowId === workflowId && row.action?.tool);+ const out: Record<string, WorkflowStepProvenance> = {};+ let cursor = 0;+ for (const step of steps) {+ let match = -1;+ for (let index = cursor; index < candidates.length; index += 1) {+ const row = candidates[index]!;+ if (row.action!.tool !== step.tool) continue;+ if (row.action!.state && row.action!.state !== step.state) continue;+ match = index; break;+ }+ if (match < 0) continue;+ const action = candidates[match]!.action!;+ cursor = match + 1;+ out[step.id] = {+ sessionLabel: sessionLabel.slice(0, 180), actionRef: action.ref, eventRef: action.eventRef, observedAt: action.at,+ ...(action.artifact ? { artifactRefs: [action.artifact.ref] } : {}),+ ...(evidenceRef && /^evidence_[a-f0-9]{20}$/.test(evidenceRef) ? { evidenceRef } : {}),+ };+ }+ return out;+}diff --git a/lib/workflow/storage.ts b/lib/workflow/storage.tsindex 0616e1bd..996b2c25 100644--- a/lib/workflow/storage.ts+++ b/lib/workflow/storage.ts@@ -10,7 +10,7 @@ let cache: WorkflowStoreState | null = null, cachePath = "", baseSnapshot = "";let needsMigration = false;const loadInFlight = new Map<string, Promise<WorkflowStoreState>>();let writeChain: Promise<unknown> = Promise.resolve();-function storePath(): string {+export function workflowStorePath(): string {const env = process.env.OS_SKILL_MEMORY_STORE?.trim();if (process.env.VITEST && !env) return path.join(os.tmpdir(), `mso-skill-memory-test-${process.pid}.json`);return (env || path.join(os.homedir(), ".mso", "skill-memory.json")).replace(/^~(?=$|\/)/, os.homedir());@@ -18,7 +18,7 @@ function storePath(): string {function legacySnapshot(raw: string) { return "legacy:" + createHash("sha256").update(raw).digest("hex"); }/** The base file remains a complete version-3 snapshot. Matching sidecar supersedes only active state. */export async function loadWorkflowStore(): Promise<WorkflowStoreState> {- const file = storePath();+ const file = workflowStorePath();if (cache && cachePath === file) return cache;const current = loadInFlight.get(file); if (current) return current;const pending = (async () => {@@ -48,7 +48,7 @@ async function atomic(file: string, snapshot: string) {} finally { await fs.unlink(tmp).catch(() => undefined); }}export async function persistWorkflowStore(store: WorkflowStoreState, activeOnly = false): Promise<void> {- const file = storePath(), partial = activeOnly && Boolean(baseSnapshot) && !needsMigration;+ const file = workflowStorePath(), partial = activeOnly && Boolean(baseSnapshot) && !needsMigration;// Capture bytes now, before callers mutate the shared object again.if (!partial) { baseSnapshot = randomUUID(); needsMigration = false; }const snapshot = partial ? JSON.stringify({ version: 1, baseSnapshot, active: store.active }) : JSON.stringify({ ...store, snapshotId: baseSnapshot });diff --git a/lib/workflow/types.ts b/lib/workflow/types.tsindex 65f08c9a..49f26ccf 100644--- a/lib/workflow/types.ts+++ b/lib/workflow/types.ts@@ -2,6 +2,17 @@ import type { Scope } from "@/lib/capabilities/scope";import type { WorkflowOrchestrationSnapshot } from "@/lib/contracts/orchestration";export type WorkflowStepState = "completed" | "failed" | "denied" | "rate_limited" | "invalid_args";+export type RecipeMaturity = "observed" | "candidate" | "verified";++export type WorkflowStepProvenance = {+ /** Human-readable session label only; internal session ids never enter learned recipes. */+ sessionLabel: string;+ actionRef: string;+ eventRef: string;+ artifactRefs?: string[];+ evidenceRef?: string;+ observedAt: string;+};export type WorkflowStep = {id: string;@@ -11,6 +22,7 @@ export type WorkflowStep = {args?: Record<string, string | number | boolean>;durationMs?: number;ts: string;+ provenance?: WorkflowStepProvenance;};export type WorkflowStepInput = Omit<WorkflowStep, "args"> & { args?: Record<string, unknown> };diff --git a/scripts/e2e/mcp-sessions.mjs b/scripts/e2e/mcp-sessions.mjsindex c00e3924..1fa3da17 100644--- a/scripts/e2e/mcp-sessions.mjs+++ b/scripts/e2e/mcp-sessions.mjs@@ -75,7 +75,17 @@ export async function mcpSessionsJourney(page, fixture) {expect(await page.locator('[data-slot="workflows-feature"]').innerText()).not.toMatch(/\b\d{8}_\d{6}_[a-f0-9]{8}\b/);await terminalStepNode.click();await expect(page.getByText("Action groups", { exact: true }).last()).toBeVisible();+ await expect(page.getByRole("button", { name: `Save ${terminalStep.ref} as workflow draft`, exact: true })).toBeVisible();+ await expect(page.getByText("Self-improve", { exact: true }).last()).toBeVisible();expect(await terminalTabs.count()).toBe(terminalCountBefore); // Selecting a node must not launch tools.+ const draftResponse = await page.request.post(fixture.base + "/api/v1/agent-sessions", { data: { action: "save-workflow-draft", id: selectedMonitor.sessions[0].id, step_ref: terminalStep.ref } });+ expect(draftResponse.status()).toBe(200);+ const draftPayload = await draftResponse.json();+ expect(draftPayload.graph.status).toBe("draft");+ expect(draftPayload.graph.metadata.provenance).toBe("learned-from-session");+ expect(draftPayload.graph.nodes.filter(node => node.type === "tool").every(node => node.disabled === true && node.config.reviewRequired === true)).toBe(true);+ expect(JSON.stringify(draftPayload)).not.toMatch(/PRIVATE_TRANSCRIPT|PRIVATE_CONTEXT|FIXTURE_SECRET_MUST_NOT_LEAK|principalHash/);+ expect(JSON.stringify(draftPayload)).not.toContain(selectedMonitor.sessions[0].id);const terminalAction = terminalStep.actions.find(action => action.terminalContext);const terminalGroup = terminalStep.groups.find(group => group.actionRefs.includes(terminalAction.ref));expect(terminalGroup).toBeTruthy();@@ -111,5 +121,5 @@ export async function mcpSessionsJourney(page, fixture) {}// Mobile/full-screen navigation changes the active route; restore Settings so the caller can continue its recovery journey.await page.goto(fixture.base + "/settings");- console.log("PASS session labels, semantic Workflow handoff, explicit terminal action, redaction, handover, keyboard and accessibility");+ console.log("PASS session labels, semantic Workflow handoff, self-improve draft, explicit terminal action, redaction, handover, keyboard and accessibility");}